xref: /titanic_51/usr/src/uts/common/io/sata/impl/sata.c (revision 2f22a2ee9f3b5a57b607f784378339d9bbd5d8e3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 /*
26  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
27  * Copyright 2016 Argo Technologies SA
28  */
29 
30 /*
31  * SATA Framework
32  * Generic SATA Host Adapter Implementation
33  */
34 
35 #include <sys/conf.h>
36 #include <sys/file.h>
37 #include <sys/ddi.h>
38 #include <sys/sunddi.h>
39 #include <sys/modctl.h>
40 #include <sys/cmn_err.h>
41 #include <sys/errno.h>
42 #include <sys/thread.h>
43 #include <sys/kstat.h>
44 #include <sys/note.h>
45 #include <sys/sysevent.h>
46 #include <sys/sysevent/eventdefs.h>
47 #include <sys/sysevent/dr.h>
48 #include <sys/taskq.h>
49 #include <sys/disp.h>
50 #include <sys/sdt.h>
51 
52 #include <sys/sata/impl/sata.h>
53 #include <sys/sata/sata_hba.h>
54 #include <sys/sata/sata_defs.h>
55 #include <sys/sata/sata_cfgadm.h>
56 #include <sys/sata/sata_blacklist.h>
57 #include <sys/sata/sata_satl.h>
58 
59 #include <sys/scsi/impl/spc3_types.h>
60 
61 /*
62  * FMA header files
63  */
64 #include <sys/ddifm.h>
65 #include <sys/fm/protocol.h>
66 #include <sys/fm/util.h>
67 #include <sys/fm/io/ddi.h>
68 
69 /* Debug flags - defined in sata.h */
70 int	sata_debug_flags = 0;
71 int	sata_msg = 0;
72 
73 /*
74  * Flags enabling selected SATA HBA framework functionality
75  */
76 #define	SATA_ENABLE_QUEUING		1
77 #define	SATA_ENABLE_NCQ			2
78 #define	SATA_ENABLE_PROCESS_EVENTS	4
79 #define	SATA_ENABLE_PMULT_FBS		8 /* FIS-Based Switching */
80 int sata_func_enable =
81 	SATA_ENABLE_PROCESS_EVENTS | SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ;
82 
83 /*
84  * Global variable setting default maximum queue depth (NCQ or TCQ)
85  * Note:minimum queue depth is 1
86  */
87 int sata_max_queue_depth = SATA_MAX_QUEUE_DEPTH; /* max NCQ/TCQ queue depth */
88 
89 /*
90  * Currently used default NCQ/TCQ queue depth. It is set-up during the driver
91  * initialization, using value from sata_max_queue_depth
92  * It is adjusted to minimum supported by the controller and by the device,
93  * if queueing is enabled.
94  */
95 static	int sata_current_max_qdepth;
96 
97 /*
98  * Global variable determining the default behavior after device hotpluggin.
99  * If non-zero, the hotplugged device is onlined (if possible) without explicit
100  * IOCTL request (AP_CONFIGURE).
101  * If zero, hotplugged device is identified, but not onlined.
102  * Enabling (AP_CONNECT) device port with an attached device does not result
103  * in device onlining regardless of the flag setting
104  */
105 int sata_auto_online = 0;
106 
107 #ifdef SATA_DEBUG
108 
109 #define	SATA_LOG_D(args)	sata_log args
110 uint64_t mbuf_count = 0;
111 uint64_t mbuffail_count = 0;
112 
113 sata_atapi_cmd_t sata_atapi_trace[64];
114 uint32_t sata_atapi_trace_index = 0;
115 int sata_atapi_trace_save = 1;
116 static	void sata_save_atapi_trace(sata_pkt_txlate_t *, int);
117 #define	SATAATAPITRACE(spx, count)	if (sata_atapi_trace_save) \
118     sata_save_atapi_trace(spx, count);
119 
120 #else
121 #define	SATA_LOG_D(args)	sata_trace_log args
122 #define	SATAATAPITRACE(spx, count)
123 #endif
124 
125 #if 0
126 static void
127 sata_test_atapi_packet_command(sata_hba_inst_t *, int);
128 #endif
129 
130 #ifdef SATA_INJECT_FAULTS
131 
132 #define		SATA_INJECT_PKT_FAULT	1
133 uint32_t	sata_inject_fault = 0;
134 
135 uint32_t	sata_inject_fault_count = 0;
136 uint32_t	sata_inject_fault_pause_count = 0;
137 uint32_t	sata_fault_type = 0;
138 uint32_t	sata_fault_cmd = 0;
139 dev_info_t	*sata_fault_ctrl = NULL;
140 sata_device_t	sata_fault_device;
141 
142 static	void sata_inject_pkt_fault(sata_pkt_t *, int *, int);
143 
144 #endif
145 
146 #define	LEGACY_HWID_LEN	64	/* Model (40) + Serial (20) + pad */
147 
148 static char sata_rev_tag[] = {"1.46"};
149 
150 /*
151  * SATA cb_ops functions
152  */
153 static 	int sata_hba_open(dev_t *, int, int, cred_t *);
154 static 	int sata_hba_close(dev_t, int, int, cred_t *);
155 static 	int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *,	int *);
156 
157 /*
158  * SCSA required entry points
159  */
160 static	int sata_scsi_tgt_init(dev_info_t *, dev_info_t *,
161     scsi_hba_tran_t *, struct scsi_device *);
162 static	int sata_scsi_tgt_probe(struct scsi_device *,
163     int (*callback)(void));
164 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *,
165     scsi_hba_tran_t *, struct scsi_device *);
166 static 	int sata_scsi_start(struct scsi_address *, struct scsi_pkt *);
167 static 	int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *);
168 static 	int sata_scsi_reset(struct scsi_address *, int);
169 static 	int sata_scsi_getcap(struct scsi_address *, char *, int);
170 static 	int sata_scsi_setcap(struct scsi_address *, char *, int, int);
171 static 	struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *,
172     struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t),
173     caddr_t);
174 static 	void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *);
175 static 	void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *);
176 static 	void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *);
177 
178 /*
179  * SATA HBA interface functions are defined in sata_hba.h header file
180  */
181 
182 /* Event processing functions */
183 static	void sata_event_daemon(void *);
184 static	void sata_event_thread_control(int);
185 static	void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst);
186 static	void sata_process_pmult_events(sata_hba_inst_t *, uint8_t);
187 static	void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *);
188 static	void sata_process_pmdevice_reset(sata_hba_inst_t *, sata_address_t *);
189 static	void sata_process_port_failed_event(sata_hba_inst_t *,
190     sata_address_t *);
191 static	void sata_process_port_link_events(sata_hba_inst_t *,
192     sata_address_t *);
193 static	void sata_process_pmport_link_events(sata_hba_inst_t *,
194     sata_address_t *);
195 static	void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *);
196 static	void sata_process_pmdevice_detached(sata_hba_inst_t *,
197     sata_address_t *);
198 static	void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *);
199 static	void sata_process_pmdevice_attached(sata_hba_inst_t *,
200     sata_address_t *);
201 static	void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *);
202 static	void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *);
203 static	void sata_process_target_node_cleanup(sata_hba_inst_t *,
204     sata_address_t *);
205 static	void sata_process_device_autoonline(sata_hba_inst_t *,
206     sata_address_t *saddr);
207 
208 /*
209  * Local translation functions
210  */
211 static	int sata_txlt_inquiry(sata_pkt_txlate_t *);
212 static	int sata_txlt_test_unit_ready(sata_pkt_txlate_t *);
213 static	int sata_txlt_start_stop_unit(sata_pkt_txlate_t *);
214 static	int sata_txlt_read_capacity(sata_pkt_txlate_t *);
215 static	int sata_txlt_read_capacity16(sata_pkt_txlate_t *);
216 static  int sata_txlt_unmap(sata_pkt_txlate_t *);
217 static	int sata_txlt_request_sense(sata_pkt_txlate_t *);
218 static	int sata_txlt_read(sata_pkt_txlate_t *);
219 static	int sata_txlt_write(sata_pkt_txlate_t *);
220 static	int sata_txlt_log_sense(sata_pkt_txlate_t *);
221 static	int sata_txlt_log_select(sata_pkt_txlate_t *);
222 static	int sata_txlt_mode_sense(sata_pkt_txlate_t *);
223 static	int sata_txlt_mode_select(sata_pkt_txlate_t *);
224 static	int sata_txlt_ata_pass_thru(sata_pkt_txlate_t *);
225 static	int sata_txlt_synchronize_cache(sata_pkt_txlate_t *);
226 static	int sata_txlt_write_buffer(sata_pkt_txlate_t *);
227 static	int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *);
228 
229 static	int sata_hba_start(sata_pkt_txlate_t *, int *);
230 static	int sata_txlt_invalid_command(sata_pkt_txlate_t *);
231 static	int sata_txlt_check_condition(sata_pkt_txlate_t *, uchar_t, uchar_t);
232 static	int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *);
233 static	int sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *);
234 static  int sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *);
235 static	void sata_txlt_rw_completion(sata_pkt_t *);
236 static	void sata_txlt_nodata_cmd_completion(sata_pkt_t *);
237 static	void sata_txlt_apt_completion(sata_pkt_t *sata_pkt);
238 static	void sata_txlt_unmap_completion(sata_pkt_t *sata_pkt);
239 static	void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *);
240 static	int sata_emul_rw_completion(sata_pkt_txlate_t *);
241 static	void sata_fill_ata_return_desc(sata_pkt_t *, uint8_t, uint8_t,
242     uint8_t);
243 static	struct scsi_extended_sense *sata_immediate_error_response(
244     sata_pkt_txlate_t *, int);
245 static	struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *);
246 
247 static	int sata_txlt_atapi(sata_pkt_txlate_t *);
248 static	void sata_txlt_atapi_completion(sata_pkt_t *);
249 
250 /*
251  * Local functions for ioctl
252  */
253 static	int32_t sata_get_port_num(sata_hba_inst_t *,  struct devctl_iocdata *);
254 static	void sata_cfgadm_state(sata_hba_inst_t *, int32_t,
255     devctl_ap_state_t *);
256 static	dev_info_t *sata_get_target_dip(dev_info_t *, uint8_t, uint8_t);
257 static	dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *);
258 static	dev_info_t *sata_devt_to_devinfo(dev_t);
259 static	int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *);
260 static	int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *);
261 static	int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *);
262 static	int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *);
263 static	int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *);
264 static	int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *);
265 static	int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *);
266 static	int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *);
267 static	int sata_ioctl_reset_all(sata_hba_inst_t *);
268 static	int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *);
269 static	int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *,
270     sata_ioctl_data_t *, int mode);
271 static	int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *,
272     sata_ioctl_data_t *, int mode);
273 static	int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *,
274     sata_ioctl_data_t *, int mode);
275 static	int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *,
276     sata_ioctl_data_t *, int mode);
277 static	int sata_ioctl_get_serialnumber_info(sata_hba_inst_t *,
278     sata_device_t *, sata_ioctl_data_t *, int mode);
279 
280 /*
281  * Local functions
282  */
283 static 	void sata_remove_hba_instance(dev_info_t *);
284 static 	int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *);
285 static 	void sata_probe_ports(sata_hba_inst_t *);
286 static	void sata_probe_pmports(sata_hba_inst_t *, uint8_t);
287 static 	int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int);
288 static 	int sata_reprobe_pmult(sata_hba_inst_t *, sata_device_t *, int);
289 static 	int sata_reprobe_pmport(sata_hba_inst_t *, sata_device_t *, int);
290 static	int sata_alloc_pmult(sata_hba_inst_t *, sata_device_t *);
291 static	void sata_free_pmult(sata_hba_inst_t *, sata_device_t *);
292 static 	int sata_add_device(dev_info_t *, sata_hba_inst_t *, sata_device_t *);
293 static	int sata_offline_device(sata_hba_inst_t *, sata_device_t *,
294     sata_drive_info_t *);
295 static 	dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *,
296     sata_address_t *);
297 static 	void sata_remove_target_node(sata_hba_inst_t *,
298     sata_address_t *);
299 static 	int sata_validate_scsi_address(sata_hba_inst_t *,
300     struct scsi_address *, sata_device_t *);
301 static 	int sata_validate_sata_address(sata_hba_inst_t *, int, int, int);
302 static	sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t));
303 static	void sata_pkt_free(sata_pkt_txlate_t *);
304 static	int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t),
305     caddr_t, ddi_dma_attr_t *);
306 static	void sata_common_free_dma_rsrcs(sata_pkt_txlate_t *);
307 static	int sata_probe_device(sata_hba_inst_t *, sata_device_t *);
308 static	sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *,
309     sata_device_t *);
310 static 	int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *);
311 static	void sata_reidentify_device(sata_pkt_txlate_t *);
312 static	struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int);
313 static 	void sata_free_local_buffer(sata_pkt_txlate_t *);
314 static 	uint64_t sata_check_capacity(sata_drive_info_t *);
315 void 	sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *,
316     ddi_dma_attr_t *);
317 static 	int sata_fetch_device_identify_data(sata_hba_inst_t *,
318     sata_drive_info_t *);
319 static	void sata_update_port_info(sata_hba_inst_t *, sata_device_t *);
320 static	void sata_update_pmport_info(sata_hba_inst_t *, sata_device_t *);
321 static	void sata_update_port_scr(sata_port_scr_t *, sata_device_t *);
322 static	int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *);
323 static	int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int);
324 static	int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int);
325 static	int sata_set_drive_features(sata_hba_inst_t *,
326     sata_drive_info_t *, int flag);
327 static	void sata_init_write_cache_mode(sata_drive_info_t *sdinfo);
328 static	int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *);
329 static	void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *,
330     uint8_t *);
331 static	int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *,
332     struct scsi_inquiry *);
333 static	int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *);
334 static	int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *);
335 static	int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *);
336 static	int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *);
337 static	int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *);
338 static	int sata_mode_select_page_8(sata_pkt_txlate_t *,
339     struct mode_cache_scsi3 *, int, int *, int *, int *);
340 static	int sata_mode_select_page_1a(sata_pkt_txlate_t *,
341     struct mode_info_power_cond *, int, int *, int *, int *);
342 static	int sata_mode_select_page_1c(sata_pkt_txlate_t *,
343     struct mode_info_excpt_page *, int, int *, int *, int *);
344 static	int sata_mode_select_page_30(sata_pkt_txlate_t *,
345     struct mode_acoustic_management *, int, int *, int *, int *);
346 
347 static	int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *);
348 static	int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *,
349     sata_hba_inst_t *);
350 static	int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *,
351     sata_hba_inst_t *);
352 static	int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *,
353     sata_hba_inst_t *);
354 static	int sata_build_lsense_page_0e(sata_drive_info_t *, uint8_t *,
355     sata_pkt_txlate_t *);
356 
357 static	void sata_set_arq_data(sata_pkt_t *);
358 static	void sata_build_read_verify_cmd(sata_cmd_t *, uint16_t, uint64_t);
359 static	void sata_build_generic_cmd(sata_cmd_t *, uint8_t);
360 static	uint8_t sata_get_standby_timer(uint8_t *timer);
361 
362 static	void sata_save_drive_settings(sata_drive_info_t *);
363 static	void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *);
364 static	void sata_show_pmult_info(sata_hba_inst_t *, sata_device_t *);
365 static	void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...);
366 static	void sata_trace_log(sata_hba_inst_t *, uint_t, const char *fmt, ...);
367 static	int sata_fetch_smart_return_status(sata_hba_inst_t *,
368     sata_drive_info_t *);
369 static	int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *,
370     struct smart_data *);
371 static	int sata_smart_selftest_log(sata_hba_inst_t *,
372     sata_drive_info_t *,
373     struct smart_selftest_log *);
374 static	int sata_ext_smart_selftest_read_log(sata_hba_inst_t *,
375     sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t);
376 static	int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *,
377     uint8_t *, uint8_t, uint8_t);
378 static	int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *,
379     struct read_log_ext_directory *);
380 static	void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int);
381 static	void sata_xlate_errors(sata_pkt_txlate_t *);
382 static	void sata_decode_device_error(sata_pkt_txlate_t *,
383     struct scsi_extended_sense *);
384 static	void sata_set_device_removed(dev_info_t *);
385 static	boolean_t sata_check_device_removed(dev_info_t *);
386 static	void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *);
387 static	int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *,
388     sata_drive_info_t *);
389 static	int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *,
390     sata_drive_info_t *);
391 static	void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *);
392 static	void sata_fixed_sense_data_preset(struct scsi_extended_sense *);
393 static  void sata_target_devid_register(dev_info_t *, sata_drive_info_t *);
394 static  int sata_check_modser(char *, int);
395 
396 /*
397  * FMA
398  */
399 static boolean_t sata_check_for_dma_error(dev_info_t *, sata_pkt_txlate_t *);
400 
401 
402 /*
403  * SATA Framework will ignore SATA HBA driver cb_ops structure and
404  * register following one with SCSA framework.
405  * Open & close are provided, so scsi framework will not use its own
406  */
407 static struct cb_ops sata_cb_ops = {
408 	sata_hba_open,			/* open */
409 	sata_hba_close,			/* close */
410 	nodev,				/* strategy */
411 	nodev,				/* print */
412 	nodev,				/* dump */
413 	nodev,				/* read */
414 	nodev,				/* write */
415 	sata_hba_ioctl,			/* ioctl */
416 	nodev,				/* devmap */
417 	nodev,				/* mmap */
418 	nodev,				/* segmap */
419 	nochpoll,			/* chpoll */
420 	ddi_prop_op,			/* cb_prop_op */
421 	0,				/* streamtab */
422 	D_NEW | D_MP,			/* cb_flag */
423 	CB_REV,				/* rev */
424 	nodev,				/* aread */
425 	nodev				/* awrite */
426 };
427 
428 
429 extern struct mod_ops mod_miscops;
430 extern uchar_t	scsi_cdb_size[];
431 
432 static struct modlmisc modlmisc = {
433 	&mod_miscops,			/* Type of module */
434 	"SATA Module"			/* module name */
435 };
436 
437 
438 static struct modlinkage modlinkage = {
439 	MODREV_1,
440 	(void *)&modlmisc,
441 	NULL
442 };
443 
444 /*
445  * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero,
446  * i.e. when scsi_pkt has not timeout specified.
447  */
448 static int sata_default_pkt_time = 60;	/* 60 seconds */
449 
450 /*
451  * Intermediate buffer device access attributes - they are required,
452  * but not necessarily used.
453  */
454 static ddi_device_acc_attr_t sata_acc_attr = {
455 	DDI_DEVICE_ATTR_V0,
456 	DDI_STRUCTURE_LE_ACC,
457 	DDI_STRICTORDER_ACC
458 };
459 
460 
461 /*
462  * Mutexes protecting structures in multithreaded operations.
463  * Because events are relatively rare, a single global mutex protecting
464  * data structures should be sufficient. To increase performance, add
465  * separate mutex per each sata port and use global mutex only to protect
466  * common data structures.
467  */
468 static	kmutex_t sata_mutex;		/* protects sata_hba_list */
469 static	kmutex_t sata_log_mutex;	/* protects log */
470 
471 static 	char sata_log_buf[256];
472 
473 /*
474  * sata trace debug
475  */
476 static	sata_trace_rbuf_t *sata_debug_rbuf;
477 static	sata_trace_dmsg_t *sata_trace_dmsg_alloc(void);
478 static	void sata_trace_dmsg_free(void);
479 static	void sata_trace_rbuf_alloc(void);
480 static	void sata_trace_rbuf_free(void);
481 
482 int	dmsg_ring_size = DMSG_RING_SIZE;
483 
484 /* Default write cache setting for SATA hard disks */
485 int	sata_write_cache = 1;		/* enabled */
486 
487 /* Default write cache setting for SATA ATAPI CD/DVD */
488 int	sata_atapicdvd_write_cache = 1; /* enabled */
489 
490 /* Default write cache setting for SATA ATAPI tape */
491 int	sata_atapitape_write_cache = 1; /* enabled */
492 
493 /* Default write cache setting for SATA ATAPI disk */
494 int	sata_atapidisk_write_cache = 1;	/* enabled */
495 
496 /*
497  * Linked list of HBA instances
498  */
499 static 	sata_hba_inst_t *sata_hba_list = NULL;
500 static 	sata_hba_inst_t *sata_hba_list_tail = NULL;
501 /*
502  * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran
503  * structure and in sata soft state.
504  */
505 
506 /*
507  * Event daemon related variables
508  */
509 static 	kmutex_t sata_event_mutex;
510 static 	kcondvar_t sata_event_cv;
511 static 	kthread_t *sata_event_thread = NULL;
512 static 	int sata_event_thread_terminate = 0;
513 static 	int sata_event_pending = 0;
514 static 	int sata_event_thread_active = 0;
515 extern 	pri_t minclsyspri;
516 
517 /*
518  * NCQ error recovery command
519  */
520 static const sata_cmd_t sata_rle_cmd = {
521 	SATA_CMD_REV,
522 	NULL,
523 	{
524 		SATA_DIR_READ
525 	},
526 	ATA_ADDR_LBA48,
527 	0,
528 	0,
529 	0,
530 	0,
531 	0,
532 	1,
533 	READ_LOG_EXT_NCQ_ERROR_RECOVERY,
534 	0,
535 	0,
536 	0,
537 	SATAC_READ_LOG_EXT,
538 	0,
539 	0,
540 	0,
541 };
542 
543 /*
544  * ATAPI error recovery CDB
545  */
546 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = {
547 	SCMD_REQUEST_SENSE,
548 	0,			/* Only fixed RQ format is supported */
549 	0,
550 	0,
551 	SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */
552 	0
553 };
554 
555 
556 /* Warlock directives */
557 
558 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran))
559 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device))
560 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops))
561 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense))
562 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status))
563 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr))
564 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t))
565 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state))
566 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state))
567 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list))
568 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list))
569 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next))
570 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev))
571 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \
572     sata_hba_inst::satahba_scsi_tran))
573 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran))
574 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip))
575 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached))
576 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port))
577 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex,
578     sata_hba_inst::satahba_event_flags))
579 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
580     sata_cport_info::cport_devp))
581 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp))
582 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr))
583 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
584     sata_cport_info::cport_dev_type))
585 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type))
586 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
587     sata_cport_info::cport_state))
588 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state))
589 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
590     sata_pmport_info::pmport_state))
591 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state))
592 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
593     sata_pmport_info::pmport_dev_type))
594 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type))
595 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
596     sata_pmport_info::pmport_sata_drive))
597 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
598     sata_pmport_info::pmport_tgtnode_clean))
599 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
600     sata_pmport_info::pmport_event_flags))
601 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive))
602 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port))
603 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports))
604 #ifdef SATA_DEBUG
605 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count))
606 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count))
607 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace))
608 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index))
609 #endif
610 
611 /* End of warlock directives */
612 
613 /* ************** loadable module configuration functions ************** */
614 
615 int
616 _init()
617 {
618 	int rval;
619 
620 	mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL);
621 	mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL);
622 	mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL);
623 	cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL);
624 	sata_trace_rbuf_alloc();
625 	if ((rval = mod_install(&modlinkage)) != 0) {
626 #ifdef SATA_DEBUG
627 		cmn_err(CE_WARN, "sata: _init: mod_install failed\n");
628 #endif
629 		sata_trace_rbuf_free();
630 		mutex_destroy(&sata_log_mutex);
631 		cv_destroy(&sata_event_cv);
632 		mutex_destroy(&sata_event_mutex);
633 		mutex_destroy(&sata_mutex);
634 	}
635 	return (rval);
636 }
637 
638 int
639 _fini()
640 {
641 	int rval;
642 
643 	if ((rval = mod_remove(&modlinkage)) != 0)
644 		return (rval);
645 
646 	sata_trace_rbuf_free();
647 	mutex_destroy(&sata_log_mutex);
648 	cv_destroy(&sata_event_cv);
649 	mutex_destroy(&sata_event_mutex);
650 	mutex_destroy(&sata_mutex);
651 	return (rval);
652 }
653 
654 int
655 _info(struct modinfo *modinfop)
656 {
657 	return (mod_info(&modlinkage, modinfop));
658 }
659 
660 
661 
662 /* ********************* SATA HBA entry points ********************* */
663 
664 
665 /*
666  * Called by SATA HBA from _init().
667  * Registers HBA driver instance/sata framework pair with scsi framework, by
668  * calling scsi_hba_init().
669  *
670  * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used
671  * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver
672  * cb_ops pointer in SATA HBA driver dev_ops structure.
673  * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors.
674  *
675  * Return status of the scsi_hba_init() is returned to a calling SATA HBA
676  * driver.
677  */
678 int
679 sata_hba_init(struct modlinkage *modlp)
680 {
681 	int rval;
682 	struct dev_ops *hba_ops;
683 
684 	SATADBG1(SATA_DBG_HBA_IF, NULL,
685 	    "sata_hba_init: name %s \n",
686 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
687 	/*
688 	 * Fill-up cb_ops and dev_ops when necessary
689 	 */
690 	hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
691 	/*
692 	 * Provide pointer to SATA dev_ops
693 	 */
694 	hba_ops->devo_cb_ops = &sata_cb_ops;
695 
696 	/*
697 	 * Register SATA HBA with SCSI framework
698 	 */
699 	if ((rval = scsi_hba_init(modlp)) != 0) {
700 		SATADBG1(SATA_DBG_HBA_IF, NULL,
701 		    "sata_hba_init: scsi hba init failed\n", NULL);
702 		return (rval);
703 	}
704 
705 	return (0);
706 }
707 
708 
709 /* HBA attach stages */
710 #define	HBA_ATTACH_STAGE_SATA_HBA_INST	1
711 #define	HBA_ATTACH_STAGE_SCSI_ATTACHED	2
712 #define	HBA_ATTACH_STAGE_SETUP		4
713 #define	HBA_ATTACH_STAGE_LINKED		8
714 
715 
716 /*
717  *
718  * Called from SATA HBA driver's attach routine to attach an instance of
719  * the HBA.
720  *
721  * For DDI_ATTACH command:
722  * sata_hba_inst structure is allocated here and initialized with pointers to
723  * SATA framework implementation of required scsi tran functions.
724  * The scsi_tran's tran_hba_private field is used by SATA Framework to point
725  * to the soft structure (sata_hba_inst) allocated by SATA framework for
726  * SATA HBA instance related data.
727  * The scsi_tran's tran_hba_private field is used by SATA framework to
728  * store a pointer to per-HBA-instance of sata_hba_inst structure.
729  * The sata_hba_inst structure is cross-linked to scsi tran structure.
730  * Among other info, a pointer to sata_hba_tran structure is stored in
731  * sata_hba_inst. The sata_hba_inst structures for different HBA instances are
732  * linked together into the list, pointed to by sata_hba_list.
733  * On the first HBA instance attach the sata event thread is initialized.
734  * Attachment points are created for all SATA ports of the HBA being attached.
735  * All HBA instance's SATA ports are probed and type of plugged devices is
736  * determined. For each device of a supported type, a target node is created.
737  *
738  * DDI_SUCCESS is returned when attachment process is successful,
739  * DDI_FAILURE is returned otherwise.
740  *
741  * For DDI_RESUME command:
742  * Not implemented at this time (postponed until phase 2 of the development).
743  */
744 int
745 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran,
746     ddi_attach_cmd_t cmd)
747 {
748 	sata_hba_inst_t	*sata_hba_inst;
749 	scsi_hba_tran_t *scsi_tran = NULL;
750 	int hba_attach_state = 0;
751 	char taskq_name[MAXPATHLEN];
752 
753 	SATADBG3(SATA_DBG_HBA_IF, NULL,
754 	    "sata_hba_attach: node %s (%s%d)\n",
755 	    ddi_node_name(dip), ddi_driver_name(dip),
756 	    ddi_get_instance(dip));
757 
758 	if (cmd == DDI_RESUME) {
759 		/*
760 		 * Postponed until phase 2 of the development
761 		 */
762 		return (DDI_FAILURE);
763 	}
764 
765 	if (cmd != DDI_ATTACH) {
766 		return (DDI_FAILURE);
767 	}
768 
769 	/* cmd == DDI_ATTACH */
770 
771 	if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) {
772 		SATA_LOG_D((NULL, CE_WARN,
773 		    "sata_hba_attach: invalid sata_hba_tran"));
774 		return (DDI_FAILURE);
775 	}
776 	/*
777 	 * Allocate and initialize SCSI tran structure.
778 	 * SATA copy of tran_bus_config is provided to create port nodes.
779 	 */
780 	scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
781 	if (scsi_tran == NULL)
782 		return (DDI_FAILURE);
783 	/*
784 	 * Allocate soft structure for SATA HBA instance.
785 	 * There is a separate softstate for each HBA instance.
786 	 */
787 	sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP);
788 	ASSERT(sata_hba_inst != NULL); /* this should not fail */
789 	mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL);
790 	hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST;
791 
792 	/*
793 	 * scsi_trans's tran_hba_private is used by SATA Framework to point to
794 	 * soft structure allocated by SATA framework for
795 	 * SATA HBA instance related data.
796 	 */
797 	scsi_tran->tran_hba_private	= sata_hba_inst;
798 	scsi_tran->tran_tgt_private	= NULL;
799 
800 	scsi_tran->tran_tgt_init	= sata_scsi_tgt_init;
801 	scsi_tran->tran_tgt_probe	= sata_scsi_tgt_probe;
802 	scsi_tran->tran_tgt_free	= sata_scsi_tgt_free;
803 
804 	scsi_tran->tran_start		= sata_scsi_start;
805 	scsi_tran->tran_reset		= sata_scsi_reset;
806 	scsi_tran->tran_abort		= sata_scsi_abort;
807 	scsi_tran->tran_getcap		= sata_scsi_getcap;
808 	scsi_tran->tran_setcap		= sata_scsi_setcap;
809 	scsi_tran->tran_init_pkt	= sata_scsi_init_pkt;
810 	scsi_tran->tran_destroy_pkt	= sata_scsi_destroy_pkt;
811 
812 	scsi_tran->tran_dmafree		= sata_scsi_dmafree;
813 	scsi_tran->tran_sync_pkt	= sata_scsi_sync_pkt;
814 
815 	scsi_tran->tran_reset_notify	= NULL;
816 	scsi_tran->tran_get_bus_addr	= NULL;
817 	scsi_tran->tran_quiesce		= NULL;
818 	scsi_tran->tran_unquiesce	= NULL;
819 	scsi_tran->tran_bus_reset	= NULL;
820 
821 	if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr,
822 	    scsi_tran, 0) != DDI_SUCCESS) {
823 #ifdef SATA_DEBUG
824 		cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed",
825 		    ddi_driver_name(dip), ddi_get_instance(dip));
826 #endif
827 		goto fail;
828 	}
829 	hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED;
830 
831 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) {
832 		if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
833 		    "sata", 1) != DDI_PROP_SUCCESS) {
834 			SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: "
835 			    "failed to create hba sata prop"));
836 			goto fail;
837 		}
838 	}
839 
840 	/*
841 	 * Save pointers in hba instance soft state.
842 	 */
843 	sata_hba_inst->satahba_scsi_tran = scsi_tran;
844 	sata_hba_inst->satahba_tran = sata_tran;
845 	sata_hba_inst->satahba_dip = dip;
846 
847 	/*
848 	 * Create a task queue to handle emulated commands completion
849 	 * Use node name, dash, instance number as the queue name.
850 	 */
851 	taskq_name[0] = '\0';
852 	(void) strlcat(taskq_name, DEVI(dip)->devi_node_name,
853 	    sizeof (taskq_name));
854 	(void) snprintf(taskq_name + strlen(taskq_name),
855 	    sizeof (taskq_name) - strlen(taskq_name),
856 	    "-%d", DEVI(dip)->devi_instance);
857 	sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1,
858 	    minclsyspri, 1, sata_tran->sata_tran_hba_num_cports * 4,
859 	    TASKQ_DYNAMIC);
860 
861 	hba_attach_state |= HBA_ATTACH_STAGE_SETUP;
862 
863 	/*
864 	 * Create events thread if not created yet.
865 	 */
866 	sata_event_thread_control(1);
867 
868 	/*
869 	 * Link this hba instance into the list.
870 	 */
871 	mutex_enter(&sata_mutex);
872 
873 	if (sata_hba_list == NULL) {
874 		/*
875 		 * The first instance of HBA is attached.
876 		 * Set current/active default maximum NCQ/TCQ queue depth for
877 		 * all SATA devices. It is done here and now, to eliminate the
878 		 * possibility of the dynamic, programatic modification of the
879 		 * queue depth via global (and public) sata_max_queue_depth
880 		 * variable (this would require special handling in HBA drivers)
881 		 */
882 		sata_current_max_qdepth = sata_max_queue_depth;
883 		if (sata_current_max_qdepth > 32)
884 			sata_current_max_qdepth = 32;
885 		else if (sata_current_max_qdepth < 1)
886 			sata_current_max_qdepth = 1;
887 	}
888 
889 	sata_hba_inst->satahba_next = NULL;
890 	sata_hba_inst->satahba_prev = sata_hba_list_tail;
891 	if (sata_hba_list == NULL) {
892 		sata_hba_list = sata_hba_inst;
893 	}
894 	if (sata_hba_list_tail != NULL) {
895 		sata_hba_list_tail->satahba_next = sata_hba_inst;
896 	}
897 	sata_hba_list_tail = sata_hba_inst;
898 	mutex_exit(&sata_mutex);
899 	hba_attach_state |= HBA_ATTACH_STAGE_LINKED;
900 
901 	/*
902 	 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl
903 	 * SATA HBA driver should not use its own open/close entry points.
904 	 *
905 	 * Make sure that instance number doesn't overflow
906 	 * when forming minor numbers.
907 	 */
908 	ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT));
909 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
910 	    INST2DEVCTL(ddi_get_instance(dip)),
911 	    DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) {
912 #ifdef SATA_DEBUG
913 		cmn_err(CE_WARN, "sata_hba_attach: "
914 		    "cannot create devctl minor node");
915 #endif
916 		goto fail;
917 	}
918 
919 
920 	/*
921 	 * Set-up kstats here, if necessary.
922 	 * (postponed until future phase of the development).
923 	 */
924 
925 	/*
926 	 * Indicate that HBA is attached. This will enable events processing
927 	 * for this HBA.
928 	 */
929 	sata_hba_inst->satahba_attached = 1;
930 	/*
931 	 * Probe controller ports. This operation will describe a current
932 	 * controller/port/multipliers/device configuration and will create
933 	 * attachment points.
934 	 * We may end-up with just a controller with no devices attached.
935 	 * For the ports with a supported device attached, device target nodes
936 	 * are created and devices are initialized.
937 	 */
938 	sata_probe_ports(sata_hba_inst);
939 
940 	return (DDI_SUCCESS);
941 
942 fail:
943 	if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) {
944 		(void) sata_remove_hba_instance(dip);
945 		if (sata_hba_list == NULL)
946 			sata_event_thread_control(0);
947 	}
948 
949 	if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) {
950 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
951 		taskq_destroy(sata_hba_inst->satahba_taskq);
952 	}
953 
954 	if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED)
955 		(void) scsi_hba_detach(dip);
956 
957 	if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) {
958 		mutex_destroy(&sata_hba_inst->satahba_mutex);
959 		kmem_free((void *)sata_hba_inst,
960 		    sizeof (struct sata_hba_inst));
961 		scsi_hba_tran_free(scsi_tran);
962 	}
963 
964 	sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed",
965 	    ddi_driver_name(dip), ddi_get_instance(dip));
966 
967 	return (DDI_FAILURE);
968 }
969 
970 
971 /*
972  * Called by SATA HBA from to detach an instance of the driver.
973  *
974  * For DDI_DETACH command:
975  * Free local structures allocated for SATA HBA instance during
976  * sata_hba_attach processing.
977  *
978  * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise.
979  *
980  * For DDI_SUSPEND command:
981  * Not implemented at this time (postponed until phase 2 of the development)
982  * Returnd DDI_SUCCESS.
983  *
984  * When the last HBA instance is detached, the event daemon is terminated.
985  *
986  * NOTE: Port multiplier is supported.
987  */
988 int
989 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
990 {
991 	dev_info_t	*tdip;
992 	sata_hba_inst_t	*sata_hba_inst;
993 	scsi_hba_tran_t *scsi_hba_tran;
994 	sata_cport_info_t *cportinfo;
995 	sata_pmult_info_t *pminfo;
996 	sata_drive_info_t *sdinfo;
997 	sata_device_t	sdevice;
998 	int ncport, npmport;
999 
1000 	SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n",
1001 	    ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip));
1002 
1003 	switch (cmd) {
1004 	case DDI_DETACH:
1005 
1006 		if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1007 			return (DDI_FAILURE);
1008 
1009 		sata_hba_inst = scsi_hba_tran->tran_hba_private;
1010 		if (sata_hba_inst == NULL)
1011 			return (DDI_FAILURE);
1012 
1013 		if (scsi_hba_detach(dip) == DDI_FAILURE) {
1014 			sata_hba_inst->satahba_attached = 1;
1015 			return (DDI_FAILURE);
1016 		}
1017 
1018 		/*
1019 		 * Free all target nodes - at this point
1020 		 * devices should be at least offlined
1021 		 * otherwise scsi_hba_detach() should not be called.
1022 		 */
1023 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
1024 		    ncport++) {
1025 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
1026 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
1027 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
1028 				if (sdinfo != NULL) {
1029 					tdip = sata_get_target_dip(dip,
1030 					    ncport, 0);
1031 					if (tdip != NULL) {
1032 						if (ndi_devi_offline(tdip,
1033 						    NDI_DEVI_REMOVE) !=
1034 						    NDI_SUCCESS) {
1035 							SATA_LOG_D((
1036 							    sata_hba_inst,
1037 							    CE_WARN,
1038 							    "sata_hba_detach: "
1039 							    "Target node not "
1040 							    "removed !"));
1041 							return (DDI_FAILURE);
1042 						}
1043 					}
1044 				}
1045 			} else { /* SATA_DTYPE_PMULT */
1046 				mutex_enter(&cportinfo->cport_mutex);
1047 				pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
1048 
1049 				if (pminfo == NULL) {
1050 					SATA_LOG_D((sata_hba_inst, CE_WARN,
1051 					    "sata_hba_detach: Port multiplier "
1052 					    "not ready yet!"));
1053 					mutex_exit(&cportinfo->cport_mutex);
1054 					return (DDI_FAILURE);
1055 				}
1056 
1057 				/*
1058 				 * Detach would fail if removal of any of the
1059 				 * target nodes is failed - albeit in that
1060 				 * case some of them may have been removed.
1061 				 */
1062 				for (npmport = 0; npmport < SATA_NUM_PMPORTS(
1063 				    sata_hba_inst, ncport); npmport++) {
1064 					tdip = sata_get_target_dip(dip, ncport,
1065 					    npmport);
1066 					if (tdip != NULL) {
1067 						if (ndi_devi_offline(tdip,
1068 						    NDI_DEVI_REMOVE) !=
1069 						    NDI_SUCCESS) {
1070 							SATA_LOG_D((
1071 							    sata_hba_inst,
1072 							    CE_WARN,
1073 							    "sata_hba_detach: "
1074 							    "Target node not "
1075 							    "removed !"));
1076 							mutex_exit(&cportinfo->
1077 							    cport_mutex);
1078 							return (DDI_FAILURE);
1079 						}
1080 					}
1081 				}
1082 				mutex_exit(&cportinfo->cport_mutex);
1083 			}
1084 		}
1085 		/*
1086 		 * Disable sata event daemon processing for this HBA
1087 		 */
1088 		sata_hba_inst->satahba_attached = 0;
1089 
1090 		/*
1091 		 * Remove event daemon thread, if it is last HBA instance.
1092 		 */
1093 
1094 		mutex_enter(&sata_mutex);
1095 		if (sata_hba_list->satahba_next == NULL) {
1096 			mutex_exit(&sata_mutex);
1097 			sata_event_thread_control(0);
1098 			mutex_enter(&sata_mutex);
1099 		}
1100 		mutex_exit(&sata_mutex);
1101 
1102 		/* Remove this HBA instance from the HBA list */
1103 		sata_remove_hba_instance(dip);
1104 
1105 		/*
1106 		 * At this point there should be no target nodes attached.
1107 		 * Detach and destroy device and port info structures.
1108 		 */
1109 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
1110 		    ncport++) {
1111 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
1112 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
1113 				sdinfo =
1114 				    cportinfo->cport_devp.cport_sata_drive;
1115 				if (sdinfo != NULL) {
1116 					/* Release device structure */
1117 					kmem_free(sdinfo,
1118 					    sizeof (sata_drive_info_t));
1119 				}
1120 				/* Release cport info */
1121 				mutex_destroy(&cportinfo->cport_mutex);
1122 				kmem_free(cportinfo,
1123 				    sizeof (sata_cport_info_t));
1124 			} else { /* SATA_DTYPE_PMULT */
1125 				sdevice.satadev_addr.cport = (uint8_t)ncport;
1126 				sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
1127 				sata_free_pmult(sata_hba_inst, &sdevice);
1128 			}
1129 		}
1130 
1131 		scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran);
1132 
1133 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
1134 
1135 		taskq_destroy(sata_hba_inst->satahba_taskq);
1136 
1137 		mutex_destroy(&sata_hba_inst->satahba_mutex);
1138 		kmem_free((void *)sata_hba_inst,
1139 		    sizeof (struct sata_hba_inst));
1140 
1141 		return (DDI_SUCCESS);
1142 
1143 	case DDI_SUSPEND:
1144 		/*
1145 		 * Postponed until phase 2
1146 		 */
1147 		return (DDI_FAILURE);
1148 
1149 	default:
1150 		return (DDI_FAILURE);
1151 	}
1152 }
1153 
1154 
1155 /*
1156  * Called by an HBA drive from _fini() routine.
1157  * Unregisters SATA HBA instance/SATA framework pair from the scsi framework.
1158  */
1159 void
1160 sata_hba_fini(struct modlinkage *modlp)
1161 {
1162 	SATADBG1(SATA_DBG_HBA_IF, NULL,
1163 	    "sata_hba_fini: name %s\n",
1164 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
1165 
1166 	scsi_hba_fini(modlp);
1167 }
1168 
1169 
1170 /*
1171  * Default open and close routine for sata_hba framework.
1172  *
1173  */
1174 /*
1175  * Open devctl node.
1176  *
1177  * Returns:
1178  * 0 if node was open successfully, error code otherwise.
1179  *
1180  *
1181  */
1182 
1183 static int
1184 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1185 {
1186 #ifndef __lock_lint
1187 	_NOTE(ARGUNUSED(credp))
1188 #endif
1189 	int rv = 0;
1190 	dev_info_t *dip;
1191 	scsi_hba_tran_t *scsi_hba_tran;
1192 	sata_hba_inst_t	*sata_hba_inst;
1193 
1194 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL);
1195 
1196 	if (otyp != OTYP_CHR)
1197 		return (EINVAL);
1198 
1199 	dip = sata_devt_to_devinfo(*devp);
1200 	if (dip == NULL)
1201 		return (ENXIO);
1202 
1203 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1204 		return (ENXIO);
1205 
1206 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1207 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1208 		return (ENXIO);
1209 
1210 	mutex_enter(&sata_mutex);
1211 	if (flags & FEXCL) {
1212 		if (sata_hba_inst->satahba_open_flag != 0) {
1213 			rv = EBUSY;
1214 		} else {
1215 			sata_hba_inst->satahba_open_flag =
1216 			    SATA_DEVCTL_EXOPENED;
1217 		}
1218 	} else {
1219 		if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) {
1220 			rv = EBUSY;
1221 		} else {
1222 			sata_hba_inst->satahba_open_flag =
1223 			    SATA_DEVCTL_SOPENED;
1224 		}
1225 	}
1226 	mutex_exit(&sata_mutex);
1227 
1228 	return (rv);
1229 }
1230 
1231 
1232 /*
1233  * Close devctl node.
1234  * Returns:
1235  * 0 if node was closed successfully, error code otherwise.
1236  *
1237  */
1238 
1239 static int
1240 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
1241 {
1242 #ifndef __lock_lint
1243 	_NOTE(ARGUNUSED(credp))
1244 	_NOTE(ARGUNUSED(flag))
1245 #endif
1246 	dev_info_t *dip;
1247 	scsi_hba_tran_t *scsi_hba_tran;
1248 	sata_hba_inst_t	*sata_hba_inst;
1249 
1250 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL);
1251 
1252 	if (otyp != OTYP_CHR)
1253 		return (EINVAL);
1254 
1255 	dip = sata_devt_to_devinfo(dev);
1256 	if (dip == NULL)
1257 		return (ENXIO);
1258 
1259 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1260 		return (ENXIO);
1261 
1262 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1263 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1264 		return (ENXIO);
1265 
1266 	mutex_enter(&sata_mutex);
1267 	sata_hba_inst->satahba_open_flag = 0;
1268 	mutex_exit(&sata_mutex);
1269 	return (0);
1270 }
1271 
1272 
1273 
1274 /*
1275  * Standard IOCTL commands for SATA hotplugging.
1276  * Implemented DEVCTL_AP commands:
1277  * DEVCTL_AP_CONNECT
1278  * DEVCTL_AP_DISCONNECT
1279  * DEVCTL_AP_CONFIGURE
1280  * DEVCTL_UNCONFIGURE
1281  * DEVCTL_AP_CONTROL
1282  *
1283  * Commands passed to default ndi ioctl handler:
1284  * DEVCTL_DEVICE_GETSTATE
1285  * DEVCTL_DEVICE_ONLINE
1286  * DEVCTL_DEVICE_OFFLINE
1287  * DEVCTL_DEVICE_REMOVE
1288  * DEVCTL_DEVICE_INSERT
1289  * DEVCTL_BUS_GETSTATE
1290  *
1291  * All other cmds are passed to HBA if it provide ioctl handler, or failed
1292  * if not.
1293  *
1294  * Returns:
1295  * 0 if successful,
1296  * error code if operation failed.
1297  *
1298  * Port Multiplier support is supported now.
1299  *
1300  * NOTE: qual should be SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT
1301  */
1302 
1303 static int
1304 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1305     int *rvalp)
1306 {
1307 #ifndef __lock_lint
1308 	_NOTE(ARGUNUSED(credp))
1309 	_NOTE(ARGUNUSED(rvalp))
1310 #endif
1311 	int rv = 0;
1312 	int32_t	comp_port = -1;
1313 	dev_info_t *dip;
1314 	devctl_ap_state_t ap_state;
1315 	struct devctl_iocdata *dcp = NULL;
1316 	scsi_hba_tran_t *scsi_hba_tran;
1317 	sata_hba_inst_t *sata_hba_inst;
1318 	sata_device_t sata_device;
1319 	sata_cport_info_t *cportinfo;
1320 	int cport, pmport, qual;
1321 	int rval = SATA_SUCCESS;
1322 
1323 	dip = sata_devt_to_devinfo(dev);
1324 	if (dip == NULL)
1325 		return (ENXIO);
1326 
1327 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1328 		return (ENXIO);
1329 
1330 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1331 	if (sata_hba_inst == NULL)
1332 		return (ENXIO);
1333 
1334 	if (sata_hba_inst->satahba_tran == NULL)
1335 		return (ENXIO);
1336 
1337 	switch (cmd) {
1338 
1339 	case DEVCTL_DEVICE_GETSTATE:
1340 	case DEVCTL_DEVICE_ONLINE:
1341 	case DEVCTL_DEVICE_OFFLINE:
1342 	case DEVCTL_DEVICE_REMOVE:
1343 	case DEVCTL_BUS_GETSTATE:
1344 		/*
1345 		 * There may be more cases that we want to pass to default
1346 		 * handler rather than fail them.
1347 		 */
1348 		return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
1349 	}
1350 
1351 	/* read devctl ioctl data */
1352 	if (cmd != DEVCTL_AP_CONTROL) {
1353 		if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1354 			return (EFAULT);
1355 
1356 		if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) ==
1357 		    -1) {
1358 			if (dcp)
1359 				ndi_dc_freehdl(dcp);
1360 			return (EINVAL);
1361 		}
1362 
1363 		/*
1364 		 * According to SCSI_TO_SATA_ADDR_QUAL, qual should be either
1365 		 * SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT.
1366 		 */
1367 		cport = SCSI_TO_SATA_CPORT(comp_port);
1368 		pmport = SCSI_TO_SATA_PMPORT(comp_port);
1369 		qual = SCSI_TO_SATA_ADDR_QUAL(comp_port);
1370 
1371 		if (sata_validate_sata_address(sata_hba_inst, cport, pmport,
1372 		    qual) != 0) {
1373 			ndi_dc_freehdl(dcp);
1374 			return (EINVAL);
1375 		}
1376 
1377 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1378 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1379 		    cport_mutex);
1380 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1381 			/*
1382 			 * Cannot process ioctl request now. Come back later.
1383 			 */
1384 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1385 			    cport_mutex);
1386 			ndi_dc_freehdl(dcp);
1387 			return (EBUSY);
1388 		}
1389 		/* Block event processing for this port */
1390 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1391 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1392 
1393 		sata_device.satadev_addr.cport = cport;
1394 		sata_device.satadev_addr.pmport = pmport;
1395 		sata_device.satadev_addr.qual = qual;
1396 		sata_device.satadev_rev = SATA_DEVICE_REV;
1397 	}
1398 
1399 	switch (cmd) {
1400 
1401 	case DEVCTL_AP_DISCONNECT:
1402 
1403 		/*
1404 		 * Normally, cfgadm sata plugin will try to offline
1405 		 * (unconfigure) device before this request. Nevertheless,
1406 		 * if a device is still configured, we need to
1407 		 * attempt to offline and unconfigure device first, and we will
1408 		 * deactivate the port regardless of the unconfigure
1409 		 * operation results.
1410 		 *
1411 		 */
1412 		rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device);
1413 
1414 		break;
1415 
1416 	case DEVCTL_AP_UNCONFIGURE:
1417 
1418 		/*
1419 		 * The unconfigure operation uses generic nexus operation to
1420 		 * offline a device. It leaves a target device node attached.
1421 		 * and obviously sata_drive_info attached as well, because
1422 		 * from the hardware point of view nothing has changed.
1423 		 */
1424 		rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device);
1425 		break;
1426 
1427 	case DEVCTL_AP_CONNECT:
1428 	{
1429 		/*
1430 		 * The sata cfgadm pluging will invoke this operation only if
1431 		 * port was found in the disconnect state (failed state
1432 		 * is also treated as the disconnected state).
1433 		 * If port activation is successful and a device is found
1434 		 * attached to the port, the initialization sequence is
1435 		 * executed to probe the port and attach
1436 		 * a device structure to a port structure. The device is not
1437 		 * set in configured state (system-wise) by this operation.
1438 		 */
1439 
1440 		rv = sata_ioctl_connect(sata_hba_inst, &sata_device);
1441 
1442 		break;
1443 	}
1444 
1445 	case DEVCTL_AP_CONFIGURE:
1446 	{
1447 		/*
1448 		 * A port may be in an active or shutdown state.
1449 		 * If port is in a failed state, operation is aborted.
1450 		 * If a port is in a shutdown state, sata_tran_port_activate()
1451 		 * is invoked prior to any other operation.
1452 		 *
1453 		 * Onlining the device involves creating a new target node.
1454 		 * If there is an old target node present (belonging to
1455 		 * previously removed device), the operation is aborted - the
1456 		 * old node has to be released and removed before configure
1457 		 * operation is attempted.
1458 		 */
1459 
1460 		rv = sata_ioctl_configure(sata_hba_inst, &sata_device);
1461 
1462 		break;
1463 	}
1464 
1465 	case DEVCTL_AP_GETSTATE:
1466 
1467 		sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state);
1468 
1469 		ap_state.ap_last_change = (time_t)-1;
1470 		ap_state.ap_error_code = 0;
1471 		ap_state.ap_in_transition = 0;
1472 
1473 		/* Copy the return AP-state information to the user space */
1474 		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
1475 			rv = EFAULT;
1476 		}
1477 		break;
1478 
1479 	case DEVCTL_AP_CONTROL:
1480 	{
1481 		/*
1482 		 * Generic devctl for hardware specific functionality
1483 		 */
1484 		sata_ioctl_data_t	ioc;
1485 
1486 		ASSERT(dcp == NULL);
1487 
1488 		/* Copy in user ioctl data first */
1489 #ifdef _MULTI_DATAMODEL
1490 		if (ddi_model_convert_from(mode & FMODELS) ==
1491 		    DDI_MODEL_ILP32) {
1492 
1493 			sata_ioctl_data_32_t	ioc32;
1494 
1495 			if (ddi_copyin((void *)arg, (void *)&ioc32,
1496 			    sizeof (ioc32), mode) != 0) {
1497 				rv = EFAULT;
1498 				break;
1499 			}
1500 			ioc.cmd 	= (uint_t)ioc32.cmd;
1501 			ioc.port	= (uint_t)ioc32.port;
1502 			ioc.get_size	= (uint_t)ioc32.get_size;
1503 			ioc.buf		= (caddr_t)(uintptr_t)ioc32.buf;
1504 			ioc.bufsiz	= (uint_t)ioc32.bufsiz;
1505 			ioc.misc_arg	= (uint_t)ioc32.misc_arg;
1506 		} else
1507 #endif /* _MULTI_DATAMODEL */
1508 		if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
1509 		    mode) != 0) {
1510 			return (EFAULT);
1511 		}
1512 
1513 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
1514 		    "sata_hba_ioctl: DEVCTL_AP_CONTROL "
1515 		    "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port);
1516 
1517 		/*
1518 		 * To avoid BE/LE and 32/64 issues, a get_size always returns
1519 		 * a 32-bit number.
1520 		 */
1521 		if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
1522 			return (EINVAL);
1523 		}
1524 		/* validate address */
1525 		cport = SCSI_TO_SATA_CPORT(ioc.port);
1526 		pmport = SCSI_TO_SATA_PMPORT(ioc.port);
1527 		qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port);
1528 
1529 		SATADBG3(SATA_DBG_IOCTL_IF, sata_hba_inst,
1530 		    "sata_hba_ioctl: target port is %d:%d (%d)",
1531 		    cport, pmport, qual);
1532 
1533 		if (sata_validate_sata_address(sata_hba_inst, cport,
1534 		    pmport, qual) != 0)
1535 			return (EINVAL);
1536 
1537 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1538 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1539 		    cport_mutex);
1540 		/* Is the port locked by event processing daemon ? */
1541 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1542 			/*
1543 			 * Cannot process ioctl request now. Come back later
1544 			 */
1545 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1546 			    cport_mutex);
1547 			return (EBUSY);
1548 		}
1549 		/* Block event processing for this port */
1550 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1551 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1552 
1553 
1554 		sata_device.satadev_addr.cport = cport;
1555 		sata_device.satadev_addr.pmport = pmport;
1556 		sata_device.satadev_addr.qual = qual;
1557 		sata_device.satadev_rev = SATA_DEVICE_REV;
1558 
1559 		switch (ioc.cmd) {
1560 
1561 		case SATA_CFGA_RESET_PORT:
1562 			/*
1563 			 * There is no protection for configured device.
1564 			 */
1565 			rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device);
1566 			break;
1567 
1568 		case SATA_CFGA_RESET_DEVICE:
1569 			/*
1570 			 * There is no protection for configured device.
1571 			 */
1572 			rv = sata_ioctl_reset_device(sata_hba_inst,
1573 			    &sata_device);
1574 			break;
1575 
1576 		case SATA_CFGA_RESET_ALL:
1577 			/*
1578 			 * There is no protection for configured devices.
1579 			 */
1580 			rv = sata_ioctl_reset_all(sata_hba_inst);
1581 			/*
1582 			 * We return here, because common return is for
1583 			 * a single port operation - we have already unlocked
1584 			 * all ports and no dc handle was allocated.
1585 			 */
1586 			return (rv);
1587 
1588 		case SATA_CFGA_PORT_DEACTIVATE:
1589 			/*
1590 			 * Arbitrarily unconfigure attached device, if any.
1591 			 * Even if the unconfigure fails, proceed with the
1592 			 * port deactivation.
1593 			 */
1594 			rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device);
1595 
1596 			break;
1597 
1598 		case SATA_CFGA_PORT_ACTIVATE:
1599 
1600 			rv = sata_ioctl_activate(sata_hba_inst, &sata_device);
1601 			break;
1602 
1603 		case SATA_CFGA_PORT_SELF_TEST:
1604 
1605 			rv = sata_ioctl_port_self_test(sata_hba_inst,
1606 			    &sata_device);
1607 			break;
1608 
1609 		case SATA_CFGA_GET_DEVICE_PATH:
1610 
1611 			rv = sata_ioctl_get_device_path(sata_hba_inst,
1612 			    &sata_device, &ioc, mode);
1613 			break;
1614 
1615 		case SATA_CFGA_GET_AP_TYPE:
1616 
1617 			rv = sata_ioctl_get_ap_type(sata_hba_inst,
1618 			    &sata_device, &ioc, mode);
1619 			break;
1620 
1621 		case SATA_CFGA_GET_MODEL_INFO:
1622 
1623 			rv = sata_ioctl_get_model_info(sata_hba_inst,
1624 			    &sata_device, &ioc, mode);
1625 			break;
1626 
1627 		case SATA_CFGA_GET_REVFIRMWARE_INFO:
1628 
1629 			rv = sata_ioctl_get_revfirmware_info(sata_hba_inst,
1630 			    &sata_device, &ioc, mode);
1631 			break;
1632 
1633 		case SATA_CFGA_GET_SERIALNUMBER_INFO:
1634 
1635 			rv = sata_ioctl_get_serialnumber_info(sata_hba_inst,
1636 			    &sata_device, &ioc, mode);
1637 			break;
1638 
1639 		default:
1640 			rv = EINVAL;
1641 			break;
1642 
1643 		} /* End of DEVCTL_AP_CONTROL cmd switch */
1644 
1645 		break;
1646 	}
1647 
1648 	default:
1649 	{
1650 		/*
1651 		 * If we got here, we got an IOCTL that SATA HBA Framework
1652 		 * does not recognize. Pass ioctl to HBA driver, in case
1653 		 * it could process it.
1654 		 */
1655 		sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran;
1656 		dev_info_t	*mydip = SATA_DIP(sata_hba_inst);
1657 
1658 		SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1659 		    "IOCTL 0x%2x not supported in SATA framework, "
1660 		    "passthrough to HBA", cmd);
1661 
1662 		if (sata_tran->sata_tran_ioctl == NULL) {
1663 			rv = EINVAL;
1664 			break;
1665 		}
1666 		rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg);
1667 		if (rval != 0) {
1668 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1669 			    "IOCTL 0x%2x failed in HBA", cmd);
1670 			rv = rval;
1671 		}
1672 		break;
1673 	}
1674 
1675 	} /* End of main IOCTL switch */
1676 
1677 	if (dcp) {
1678 		ndi_dc_freehdl(dcp);
1679 	}
1680 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1681 	cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
1682 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1683 
1684 	return (rv);
1685 }
1686 
1687 
1688 /*
1689  * Create error retrieval sata packet
1690  *
1691  * A sata packet is allocated and set-up to contain specified error retrieval
1692  * command and appropriate dma-able data buffer.
1693  * No association with any scsi packet is made and no callback routine is
1694  * specified.
1695  *
1696  * Returns a pointer to sata packet upon successful packet creation.
1697  * Returns NULL, if packet cannot be created.
1698  */
1699 sata_pkt_t *
1700 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device,
1701     int pkt_type)
1702 {
1703 	sata_hba_inst_t	*sata_hba_inst;
1704 	sata_pkt_txlate_t *spx;
1705 	sata_pkt_t *spkt;
1706 	sata_drive_info_t *sdinfo;
1707 
1708 	mutex_enter(&sata_mutex);
1709 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1710 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1711 		if (SATA_DIP(sata_hba_inst) == dip)
1712 			break;
1713 	}
1714 	mutex_exit(&sata_mutex);
1715 	ASSERT(sata_hba_inst != NULL);
1716 
1717 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
1718 	if (sdinfo == NULL) {
1719 		sata_log(sata_hba_inst, CE_WARN,
1720 		    "sata: error recovery request for non-attached device at "
1721 		    "cport %d", sata_device->satadev_addr.cport);
1722 		return (NULL);
1723 	}
1724 
1725 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1726 	spx->txlt_sata_hba_inst = sata_hba_inst;
1727 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
1728 	spkt = sata_pkt_alloc(spx, NULL);
1729 	if (spkt == NULL) {
1730 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1731 		return (NULL);
1732 	}
1733 	/* address is needed now */
1734 	spkt->satapkt_device.satadev_addr = sata_device->satadev_addr;
1735 
1736 	switch (pkt_type) {
1737 	case SATA_ERR_RETR_PKT_TYPE_NCQ:
1738 		if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) {
1739 			if (sata_check_for_dma_error(dip, spx)) {
1740 				ddi_fm_service_impact(dip,
1741 				    DDI_SERVICE_UNAFFECTED);
1742 				break;
1743 			}
1744 			return (spkt);
1745 		}
1746 		break;
1747 
1748 	case SATA_ERR_RETR_PKT_TYPE_ATAPI:
1749 		if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) {
1750 			if (sata_check_for_dma_error(dip, spx)) {
1751 				ddi_fm_service_impact(dip,
1752 				    DDI_SERVICE_UNAFFECTED);
1753 				break;
1754 			}
1755 			return (spkt);
1756 		}
1757 		break;
1758 
1759 	default:
1760 		break;
1761 	}
1762 
1763 	sata_pkt_free(spx);
1764 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1765 	return (NULL);
1766 
1767 }
1768 
1769 
1770 /*
1771  * Free error retrieval sata packet
1772  *
1773  * Free sata packet and any associated resources allocated previously by
1774  * sata_get_error_retrieval_pkt().
1775  *
1776  * Void return.
1777  */
1778 void
1779 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt)
1780 {
1781 	sata_pkt_txlate_t *spx =
1782 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1783 
1784 	ASSERT(sata_pkt != NULL);
1785 
1786 	sata_free_local_buffer(spx);
1787 	sata_pkt_free(spx);
1788 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1789 
1790 }
1791 
1792 /*
1793  * Create READ PORT MULTIPLIER and WRITE PORT MULTIPLIER sata packet
1794  *
1795  * No association with any scsi packet is made and no callback routine is
1796  * specified.
1797  *
1798  * Returns a pointer to sata packet upon successful packet creation.
1799  * Returns NULL, if packet cannot be created.
1800  *
1801  * NOTE: Input/Output value includes 64 bits accoring to SATA Spec 2.6,
1802  * only lower 32 bits are available currently.
1803  */
1804 sata_pkt_t *
1805 sata_get_rdwr_pmult_pkt(dev_info_t *dip, sata_device_t *sd,
1806     uint8_t regn, uint32_t regv, uint32_t type)
1807 {
1808 	sata_hba_inst_t	*sata_hba_inst;
1809 	sata_pkt_txlate_t *spx;
1810 	sata_pkt_t *spkt;
1811 	sata_cmd_t *scmd;
1812 
1813 	/* Only READ/WRITE commands are accepted. */
1814 	ASSERT(type == SATA_RDWR_PMULT_PKT_TYPE_READ ||
1815 	    type == SATA_RDWR_PMULT_PKT_TYPE_WRITE);
1816 
1817 	mutex_enter(&sata_mutex);
1818 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1819 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1820 		if (SATA_DIP(sata_hba_inst) == dip)
1821 			break;
1822 	}
1823 	mutex_exit(&sata_mutex);
1824 	ASSERT(sata_hba_inst != NULL);
1825 
1826 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1827 	spx->txlt_sata_hba_inst = sata_hba_inst;
1828 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
1829 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
1830 	if (spkt == NULL) {
1831 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1832 		return (NULL);
1833 	}
1834 
1835 	/*
1836 	 * NOTE: We need to send this command to the port multiplier,
1837 	 * that means send to SATA_PMULT_HOSTPORT(0xf) pmport
1838 	 *
1839 	 * sata_device contains the address of actual target device, and the
1840 	 * pmport number in the command comes from the sata_device structure.
1841 	 */
1842 	spkt->satapkt_device.satadev_addr = sd->satadev_addr;
1843 	spkt->satapkt_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
1844 	spkt->satapkt_device.satadev_addr.qual = SATA_ADDR_PMULT;
1845 
1846 	/* Fill sata_pkt */
1847 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_POLLING;
1848 	spkt->satapkt_comp = NULL; /* Synchronous mode, no callback */
1849 	spkt->satapkt_time = 10; /* Timeout 10s */
1850 
1851 	/* Build READ PORT MULTIPLIER cmd in the sata_pkt */
1852 	scmd = &spkt->satapkt_cmd;
1853 	scmd->satacmd_features_reg = regn & 0xff;
1854 	scmd->satacmd_features_reg_ext = (regn >> 8) & 0xff;
1855 	scmd->satacmd_device_reg = sd->satadev_addr.pmport;
1856 	scmd->satacmd_addr_type = 0;		/* N/A */
1857 
1858 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
1859 
1860 	if (type == SATA_RDWR_PMULT_PKT_TYPE_READ) {
1861 		scmd->satacmd_cmd_reg = SATAC_READ_PORTMULT;
1862 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
1863 		scmd->satacmd_flags.sata_special_regs = 1;
1864 		scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1;
1865 		scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1;
1866 		scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1;
1867 		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1;
1868 	} else if (type == SATA_RDWR_PMULT_PKT_TYPE_WRITE) {
1869 		scmd->satacmd_cmd_reg = SATAC_WRITE_PORTMULT;
1870 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
1871 		scmd->satacmd_sec_count_lsb = regv & 0xff;
1872 		scmd->satacmd_lba_low_lsb = regv >> 8 & 0xff;
1873 		scmd->satacmd_lba_mid_lsb = regv >> 16 & 0xff;
1874 		scmd->satacmd_lba_high_lsb = regv >> 24 & 0xff;
1875 	}
1876 
1877 	return (spkt);
1878 }
1879 
1880 /*
1881  * Free sata packet and any associated resources allocated previously by
1882  * sata_get_rdwr_pmult_pkt().
1883  *
1884  * Void return.
1885  */
1886 void
1887 sata_free_rdwr_pmult_pkt(sata_pkt_t *sata_pkt)
1888 {
1889 	sata_pkt_txlate_t *spx =
1890 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1891 
1892 	/* Free allocated resources */
1893 	sata_pkt_free(spx);
1894 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1895 }
1896 
1897 /*
1898  * Register a port multiplier to framework.
1899  * 1) Store the GSCR values in the previous allocated pmult_info strctures.
1900  * 2) Search in the blacklist and update the number of the device ports of the
1901  * port multiplier.
1902  *
1903  * Void return.
1904  */
1905 void
1906 sata_register_pmult(dev_info_t *dip, sata_device_t *sd, sata_pmult_gscr_t *sg)
1907 {
1908 	sata_hba_inst_t *sata_hba_inst = NULL;
1909 	sata_pmult_info_t *pmultinfo;
1910 	sata_pmult_bl_t *blp;
1911 	int cport = sd->satadev_addr.cport;
1912 
1913 	mutex_enter(&sata_mutex);
1914 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1915 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1916 		if (SATA_DIP(sata_hba_inst) == dip)
1917 			if (sata_hba_inst->satahba_attached == 1)
1918 				break;
1919 	}
1920 	mutex_exit(&sata_mutex);
1921 	/* HBA not attached? */
1922 	if (sata_hba_inst == NULL)
1923 		return;
1924 
1925 	/* Number of pmports */
1926 	sd->satadev_add_info = sg->gscr2 & SATA_PMULT_PORTNUM_MASK;
1927 
1928 	/* Check the blacklist */
1929 	for (blp = sata_pmult_blacklist; blp->bl_gscr0; blp++) {
1930 		if (sg->gscr0 != blp->bl_gscr0 && blp->bl_gscr0)
1931 			continue;
1932 		if (sg->gscr1 != blp->bl_gscr1 && blp->bl_gscr1)
1933 			continue;
1934 		if (sg->gscr2 != blp->bl_gscr2 && blp->bl_gscr2)
1935 			continue;
1936 
1937 		cmn_err(CE_WARN, "!Port multiplier is on the blacklist.");
1938 		sd->satadev_add_info = blp->bl_flags;
1939 		break;
1940 	}
1941 
1942 	/* Register the port multiplier GSCR */
1943 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
1944 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
1945 	if (pmultinfo != NULL) {
1946 		pmultinfo->pmult_gscr = *sg;
1947 		pmultinfo->pmult_num_dev_ports =
1948 		    sd->satadev_add_info & SATA_PMULT_PORTNUM_MASK;
1949 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
1950 		    "Port multiplier registered at port %d", cport);
1951 	}
1952 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
1953 }
1954 
1955 /*
1956  * sata_split_model splits the model ID into vendor and product IDs.
1957  * It assumes that a vendor ID cannot be longer than 8 characters, and
1958  * that vendor and product ID are separated by a whitespace.
1959  */
1960 void
1961 sata_split_model(char *model, char **vendor, char **product)
1962 {
1963 	int i, modlen;
1964 	char *vid, *pid;
1965 
1966 	/*
1967 	 * remove whitespace at the end of model
1968 	 */
1969 	for (i = SATA_ID_MODEL_LEN; i > 0; i--)
1970 		if (model[i] == ' ' || model[i] == '\t' || model[i] == '\0')
1971 			model[i] = '\0';
1972 		else
1973 			break;
1974 
1975 	/*
1976 	 * try to split model into into vid/pid
1977 	 */
1978 	modlen = strlen(model);
1979 	for (i = 0, pid = model; i < modlen; i++, pid++)
1980 		if ((*pid == ' ') || (*pid == '\t'))
1981 			break;
1982 
1983 	/*
1984 	 * only use vid if it is less than 8 chars (as in SCSI)
1985 	 */
1986 	if (i < modlen && i <= 8) {
1987 		vid = model;
1988 		/*
1989 		 * terminate vid, establish pid
1990 		 */
1991 		*pid++ = '\0';
1992 	} else {
1993 		/*
1994 		 * vid will stay "ATA     "
1995 		 */
1996 		vid = NULL;
1997 		/*
1998 		 * model is all pid
1999 		 */
2000 		pid = model;
2001 	}
2002 
2003 	*vendor = vid;
2004 	*product = pid;
2005 }
2006 
2007 /*
2008  * sata_name_child is for composing the name of the node
2009  * the format of the name is "target,0".
2010  */
2011 static int
2012 sata_name_child(dev_info_t *dip, char *name, int namelen)
2013 {
2014 	int target;
2015 
2016 	target = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
2017 	    DDI_PROP_DONTPASS, "target", -1);
2018 	if (target == -1)
2019 		return (DDI_FAILURE);
2020 	(void) snprintf(name, namelen, "%x,0", target);
2021 	return (DDI_SUCCESS);
2022 }
2023 
2024 
2025 
2026 /* ****************** SCSA required entry points *********************** */
2027 
2028 /*
2029  * Implementation of scsi tran_tgt_init.
2030  * sata_scsi_tgt_init() initializes scsi_device structure
2031  *
2032  * If successful, DDI_SUCCESS is returned.
2033  * DDI_FAILURE is returned if addressed device does not exist
2034  */
2035 
2036 static int
2037 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2038     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2039 {
2040 #ifndef __lock_lint
2041 	_NOTE(ARGUNUSED(hba_dip))
2042 	_NOTE(ARGUNUSED(tgt_dip))
2043 #endif
2044 	sata_device_t		sata_device;
2045 	sata_drive_info_t	*sdinfo;
2046 	struct sata_id		*sid;
2047 	sata_hba_inst_t		*sata_hba_inst;
2048 	char			model[SATA_ID_MODEL_LEN + 1];
2049 	char			fw[SATA_ID_FW_LEN + 1];
2050 	char			*vid, *pid;
2051 
2052 	/*
2053 	 * Fail tran_tgt_init for .conf stub node
2054 	 */
2055 	if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
2056 		(void) ndi_merge_node(tgt_dip, sata_name_child);
2057 		ddi_set_name_addr(tgt_dip, NULL);
2058 		return (DDI_FAILURE);
2059 	}
2060 
2061 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2062 
2063 	/* Validate scsi device address */
2064 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2065 	    &sata_device) != 0)
2066 		return (DDI_FAILURE);
2067 
2068 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2069 	    sata_device.satadev_addr.cport)));
2070 
2071 	/* sata_device now contains a valid sata address */
2072 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2073 	if (sdinfo == NULL) {
2074 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2075 		    sata_device.satadev_addr.cport)));
2076 		return (DDI_FAILURE);
2077 	}
2078 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2079 	    sata_device.satadev_addr.cport)));
2080 
2081 	/*
2082 	 * Check if we need to create a legacy devid (i.e cmdk style) for
2083 	 * the target disks.
2084 	 *
2085 	 * HBA devinfo node will have the property "use-cmdk-devid-format"
2086 	 * if we need to create cmdk-style devid for all the disk devices
2087 	 * attached to this controller. This property may have been set
2088 	 * from HBA driver's .conf file or by the HBA driver in its
2089 	 * attach(9F) function.
2090 	 */
2091 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2092 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2093 	    "use-cmdk-devid-format", 0) == 1)) {
2094 		/* register a legacy devid for this target node */
2095 		sata_target_devid_register(tgt_dip, sdinfo);
2096 	}
2097 
2098 
2099 	/*
2100 	 * 'Identify Device Data' does not always fit in standard SCSI
2101 	 * INQUIRY data, so establish INQUIRY_* properties with full-form
2102 	 * of information.
2103 	 */
2104 	sid = &sdinfo->satadrv_id;
2105 #ifdef	_LITTLE_ENDIAN
2106 	swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
2107 	swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
2108 #else	/* _LITTLE_ENDIAN */
2109 	bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN);
2110 	bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN);
2111 #endif	/* _LITTLE_ENDIAN */
2112 	model[SATA_ID_MODEL_LEN] = 0;
2113 	fw[SATA_ID_FW_LEN] = 0;
2114 
2115 	sata_split_model(model, &vid, &pid);
2116 
2117 	if (vid)
2118 		(void) scsi_device_prop_update_inqstring(sd, INQUIRY_VENDOR_ID,
2119 		    vid, strlen(vid));
2120 	if (pid)
2121 		(void) scsi_device_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID,
2122 		    pid, strlen(pid));
2123 	(void) scsi_device_prop_update_inqstring(sd, INQUIRY_REVISION_ID,
2124 	    fw, strlen(fw));
2125 
2126 	return (DDI_SUCCESS);
2127 }
2128 
2129 /*
2130  * Implementation of scsi tran_tgt_probe.
2131  * Probe target, by calling default scsi routine scsi_hba_probe()
2132  */
2133 static int
2134 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void))
2135 {
2136 	sata_hba_inst_t *sata_hba_inst =
2137 	    (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private);
2138 	int rval;
2139 	uint32_t pm_cap;
2140 
2141 	rval = scsi_hba_probe(sd, callback);
2142 	pm_cap = SATA_CAP_POWER_CONDITON | SATA_CAP_SMART_PAGE |
2143 	    SATA_CAP_LOG_SENSE;
2144 
2145 	if (rval == SCSIPROBE_EXISTS) {
2146 		/*
2147 		 * Set property "pm-capable" on the target device node, so that
2148 		 * the target driver will not try to fetch scsi cycle counters
2149 		 * before enabling device power-management.
2150 		 */
2151 		if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
2152 		    "pm-capable", pm_cap)) != DDI_PROP_SUCCESS) {
2153 			sata_log(sata_hba_inst, CE_WARN,
2154 			    "SATA device at port %d: "
2155 			    "will not be power-managed ",
2156 			    SCSI_TO_SATA_CPORT(sd->sd_address.a_target));
2157 			SATA_LOG_D((sata_hba_inst, CE_WARN,
2158 			    "failure updating pm-capable property"));
2159 		}
2160 	}
2161 	return (rval);
2162 }
2163 
2164 /*
2165  * Implementation of scsi tran_tgt_free.
2166  * Release all resources allocated for scsi_device
2167  */
2168 static void
2169 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2170     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2171 {
2172 #ifndef __lock_lint
2173 	_NOTE(ARGUNUSED(hba_dip))
2174 #endif
2175 	sata_device_t		sata_device;
2176 	sata_drive_info_t	*sdinfo;
2177 	sata_hba_inst_t		*sata_hba_inst;
2178 	ddi_devid_t		devid;
2179 
2180 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2181 
2182 	/* Validate scsi device address */
2183 	/*
2184 	 * Note: tgt_free relates to the SCSA view of a device. If called, there
2185 	 * was a device at this address, so even if the sata framework internal
2186 	 * resources were alredy released because a device was detached,
2187 	 * this function should be executed as long as its actions do
2188 	 * not require the internal sata view of a device and the address
2189 	 * refers to a valid sata address.
2190 	 * Validating the address here means that we do not trust SCSA...
2191 	 */
2192 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2193 	    &sata_device) == -1)
2194 		return;
2195 
2196 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2197 	    sata_device.satadev_addr.cport)));
2198 
2199 	/* sata_device now should contain a valid sata address */
2200 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2201 	if (sdinfo == NULL) {
2202 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2203 		    sata_device.satadev_addr.cport)));
2204 		return;
2205 	}
2206 	/*
2207 	 * We did not allocate any resources in sata_scsi_tgt_init()
2208 	 * other than few properties.
2209 	 * Free them.
2210 	 */
2211 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2212 	    sata_device.satadev_addr.cport)));
2213 	(void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable");
2214 
2215 	/*
2216 	 * If devid was previously created but not freed up from
2217 	 * sd(7D) driver (i.e during detach(9F)) then do it here.
2218 	 */
2219 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2220 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2221 	    "use-cmdk-devid-format", 0) == 1) &&
2222 	    (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) {
2223 		ddi_devid_unregister(tgt_dip);
2224 		ddi_devid_free(devid);
2225 	}
2226 }
2227 
2228 /*
2229  * Implementation of scsi tran_init_pkt
2230  * Upon successful return, scsi pkt buffer has DMA resources allocated.
2231  *
2232  * It seems that we should always allocate pkt, even if the address is
2233  * for non-existing device - just use some default for dma_attr.
2234  * The reason is that there is no way to communicate this to a caller here.
2235  * Subsequent call to sata_scsi_start may fail appropriately.
2236  * Simply returning NULL does not seem to discourage a target driver...
2237  *
2238  * Returns a pointer to initialized scsi_pkt, or NULL otherwise.
2239  */
2240 static struct scsi_pkt *
2241 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
2242     struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
2243     int (*callback)(caddr_t), caddr_t arg)
2244 {
2245 	sata_hba_inst_t *sata_hba_inst =
2246 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2247 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
2248 	sata_device_t sata_device;
2249 	sata_drive_info_t *sdinfo;
2250 	sata_pkt_txlate_t *spx;
2251 	ddi_dma_attr_t cur_dma_attr;
2252 	int rval;
2253 	boolean_t new_pkt = B_TRUE;
2254 
2255 	ASSERT(ap->a_hba_tran->tran_hba_dip == dip);
2256 
2257 	/*
2258 	 * We need to translate the address, even if it could be
2259 	 * a bogus one, for a non-existing device
2260 	 */
2261 	sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
2262 	sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target);
2263 	sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2264 	sata_device.satadev_rev = SATA_DEVICE_REV;
2265 
2266 	if (pkt == NULL) {
2267 		/*
2268 		 * Have to allocate a brand new scsi packet.
2269 		 * We need to operate with auto request sense enabled.
2270 		 */
2271 		pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen,
2272 		    MAX(statuslen, SATA_MAX_SENSE_LEN),
2273 		    tgtlen, sizeof (sata_pkt_txlate_t), callback, arg);
2274 
2275 		if (pkt == NULL)
2276 			return (NULL);
2277 
2278 		/* Fill scsi packet structure */
2279 		pkt->pkt_comp		= (void (*)())NULL;
2280 		pkt->pkt_time		= 0;
2281 		pkt->pkt_resid		= 0;
2282 		pkt->pkt_statistics	= 0;
2283 		pkt->pkt_reason		= 0;
2284 
2285 		/*
2286 		 * pkt_hba_private will point to sata pkt txlate structure
2287 		 */
2288 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2289 		bzero(spx, sizeof (sata_pkt_txlate_t));
2290 
2291 		spx->txlt_scsi_pkt = pkt;
2292 		spx->txlt_sata_hba_inst = sata_hba_inst;
2293 
2294 		/* Allocate sata_pkt */
2295 		spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback);
2296 		if (spx->txlt_sata_pkt == NULL) {
2297 			/* Could not allocate sata pkt */
2298 			scsi_hba_pkt_free(ap, pkt);
2299 			return (NULL);
2300 		}
2301 		/* Set sata address */
2302 		spx->txlt_sata_pkt->satapkt_device.satadev_addr =
2303 		    sata_device.satadev_addr;
2304 		spx->txlt_sata_pkt->satapkt_device.satadev_rev =
2305 		    sata_device.satadev_rev;
2306 
2307 		if ((bp == NULL) || (bp->b_bcount == 0))
2308 			return (pkt);
2309 
2310 		spx->txlt_total_residue = bp->b_bcount;
2311 	} else {
2312 		new_pkt = B_FALSE;
2313 		/*
2314 		 * Packet was preallocated/initialized by previous call
2315 		 */
2316 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2317 
2318 		if ((bp == NULL) || (bp->b_bcount == 0)) {
2319 			return (pkt);
2320 		}
2321 
2322 		/* Pkt is available already: spx->txlt_scsi_pkt == pkt; */
2323 	}
2324 
2325 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
2326 
2327 	/*
2328 	 * We use an adjusted version of the dma_attr, to account
2329 	 * for device addressing limitations.
2330 	 * sata_adjust_dma_attr() will handle sdinfo == NULL which may
2331 	 * happen when a device is not yet configured.
2332 	 */
2333 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2334 	    sata_device.satadev_addr.cport)));
2335 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2336 	    &spx->txlt_sata_pkt->satapkt_device);
2337 	/* NULL sdinfo may be passsed to sata_adjust_dma_attr() */
2338 	sata_adjust_dma_attr(sdinfo,
2339 	    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
2340 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2341 	    sata_device.satadev_addr.cport)));
2342 	/*
2343 	 * Allocate necessary DMA resources for the packet's data buffer
2344 	 * NOTE:
2345 	 * In case of read/write commands, DMA resource allocation here is
2346 	 * based on the premise that the transfer length specified in
2347 	 * the read/write scsi cdb will match exactly DMA resources -
2348 	 * returning correct packet residue is crucial.
2349 	 */
2350 	if ((rval = sata_dma_buf_setup(spx, flags, callback, arg,
2351 	    &cur_dma_attr)) != DDI_SUCCESS) {
2352 		/*
2353 		 * If a DMA allocation request fails with
2354 		 * DDI_DMA_NOMAPPING, indicate the error by calling
2355 		 * bioerror(9F) with bp and an error code of EFAULT.
2356 		 * If a DMA allocation request fails with
2357 		 * DDI_DMA_TOOBIG, indicate the error by calling
2358 		 * bioerror(9F) with bp and an error code of EINVAL.
2359 		 * For DDI_DMA_NORESOURCES, we may have some of them allocated.
2360 		 * Request may be repeated later - there is no real error.
2361 		 */
2362 		switch (rval) {
2363 		case DDI_DMA_NORESOURCES:
2364 			bioerror(bp, 0);
2365 			break;
2366 		case DDI_DMA_NOMAPPING:
2367 		case DDI_DMA_BADATTR:
2368 			bioerror(bp, EFAULT);
2369 			break;
2370 		case DDI_DMA_TOOBIG:
2371 		default:
2372 			bioerror(bp, EINVAL);
2373 			break;
2374 		}
2375 		goto fail;
2376 	}
2377 
2378 	if (sata_check_for_dma_error(dip, spx)) {
2379 		ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED);
2380 		bioerror(bp, EFAULT);
2381 		goto fail;
2382 	}
2383 
2384 success:
2385 	/* Set number of bytes that are not yet accounted for */
2386 	pkt->pkt_resid = spx->txlt_total_residue;
2387 	ASSERT(pkt->pkt_resid >= 0);
2388 
2389 	return (pkt);
2390 
2391 fail:
2392 	if (new_pkt == B_TRUE) {
2393 		/*
2394 		 * Since this is a new packet, we can clean-up
2395 		 * everything
2396 		 */
2397 		sata_scsi_destroy_pkt(ap, pkt);
2398 	} else {
2399 		/*
2400 		 * This is a re-used packet. It will be target driver's
2401 		 * responsibility to eventually destroy it (which
2402 		 * will free allocated resources).
2403 		 * Here, we just "complete" the request, leaving
2404 		 * allocated resources intact, so the request may
2405 		 * be retried.
2406 		 */
2407 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2408 		sata_pkt_free(spx);
2409 	}
2410 	return (NULL);
2411 }
2412 
2413 /*
2414  * Implementation of scsi tran_start.
2415  * Translate scsi cmd into sata operation and return status.
2416  * ATAPI CDBs are passed to ATAPI devices - the device determines what commands
2417  * are supported.
2418  * For SATA hard disks, supported scsi commands:
2419  * SCMD_INQUIRY
2420  * SCMD_TEST_UNIT_READY
2421  * SCMD_START_STOP
2422  * SCMD_READ_CAPACITY
2423  * SCMD_SVC_ACTION_IN_G4 (READ CAPACITY (16))
2424  * SCMD_REQUEST_SENSE
2425  * SCMD_LOG_SENSE_G1
2426  * SCMD_LOG_SELECT_G1
2427  * SCMD_MODE_SENSE	(specific pages)
2428  * SCMD_MODE_SENSE_G1	(specific pages)
2429  * SCMD_MODE_SELECT	(specific pages)
2430  * SCMD_MODE_SELECT_G1	(specific pages)
2431  * SCMD_SYNCHRONIZE_CACHE
2432  * SCMD_SYNCHRONIZE_CACHE_G1
2433  * SCMD_READ
2434  * SCMD_READ_G1
2435  * SCMD_READ_G4
2436  * SCMD_READ_G5
2437  * SCMD_WRITE
2438  * SCMD_WRITE_BUFFER
2439  * SCMD_WRITE_G1
2440  * SCMD_WRITE_G4
2441  * SCMD_WRITE_G5
2442  * SCMD_SEEK		(noop)
2443  * SCMD_SDIAG
2444  *
2445  * All other commands are rejected as unsupported.
2446  *
2447  * Returns:
2448  * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver
2449  * for execution. TRAN_ACCEPT may be returned also if device was removed but
2450  * a callback could be scheduled.
2451  * TRAN_BADPKT if cmd was directed to invalid address.
2452  * TRAN_FATAL_ERROR is command was rejected due to hardware error, including
2453  * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device
2454  * was removed and there was no callback specified in scsi pkt.
2455  * TRAN_BUSY if command could not be executed becasue HBA driver or SATA
2456  * framework was busy performing some other operation(s).
2457  *
2458  */
2459 static int
2460 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
2461 {
2462 	sata_hba_inst_t *sata_hba_inst =
2463 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2464 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2465 	sata_device_t *sdevice = &spx->txlt_sata_pkt->satapkt_device;
2466 	sata_drive_info_t *sdinfo;
2467 	struct buf *bp;
2468 	uint8_t cport, pmport;
2469 	boolean_t dev_gone = B_FALSE;
2470 	int rval;
2471 
2472 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2473 	    "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]);
2474 
2475 	ASSERT(spx != NULL &&
2476 	    spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL);
2477 
2478 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
2479 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2480 
2481 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2482 
2483 	if (sdevice->satadev_addr.qual == SATA_ADDR_DCPORT) {
2484 		sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2485 		if (sdinfo == NULL ||
2486 		    SATA_CPORT_INFO(sata_hba_inst, cport)->
2487 		    cport_tgtnode_clean == B_FALSE ||
2488 		    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2489 			dev_gone = B_TRUE;
2490 		}
2491 	} else if (sdevice->satadev_addr.qual == SATA_ADDR_DPMPORT) {
2492 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
2493 		    SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst,
2494 		    cport) == NULL) {
2495 			dev_gone = B_TRUE;
2496 		} else if (SATA_PMPORT_INFO(sata_hba_inst, cport,
2497 		    pmport) == NULL) {
2498 			dev_gone = B_TRUE;
2499 		} else {
2500 			mutex_enter(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2501 			    cport, pmport)));
2502 			sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2503 			if (sdinfo == NULL ||
2504 			    SATA_PMPORT_INFO(sata_hba_inst, cport, pmport)->
2505 			    pmport_tgtnode_clean == B_FALSE ||
2506 			    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2507 				dev_gone = B_TRUE;
2508 			}
2509 			mutex_exit(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2510 			    cport, pmport)));
2511 		}
2512 	}
2513 
2514 	if (dev_gone == B_TRUE) {
2515 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2516 		pkt->pkt_reason = CMD_DEV_GONE;
2517 		/*
2518 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2519 		 * only in callback function (for normal requests) and
2520 		 * in the dump code path.
2521 		 * So, if the callback is available, we need to do
2522 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2523 		 */
2524 		if (pkt->pkt_comp != NULL) {
2525 			/* scsi callback required */
2526 			if (servicing_interrupt()) {
2527 				if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2528 				    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2529 				    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
2530 				    NULL) {
2531 					return (TRAN_BUSY);
2532 				}
2533 			} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2534 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2535 			    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
2536 				/* Scheduling the callback failed */
2537 				return (TRAN_BUSY);
2538 			}
2539 			return (TRAN_ACCEPT);
2540 		}
2541 		/* No callback available */
2542 		return (TRAN_FATAL_ERROR);
2543 	}
2544 
2545 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
2546 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2547 		rval = sata_txlt_atapi(spx);
2548 		SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2549 		    "sata_scsi_start atapi: rval %d\n", rval);
2550 		return (rval);
2551 	}
2552 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2553 
2554 	/*
2555 	 * Checking for power state, if it was on
2556 	 * STOPPED state, then the drive is not capable
2557 	 * of processing media access command.  And
2558 	 * TEST_UNIT_READY, REQUEST_SENSE has special handling
2559 	 * in the function for different power state.
2560 	 */
2561 	if (((sdinfo->satadrv_power_level == SATA_POWER_STANDBY) ||
2562 	    (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)) &&
2563 	    (SATA_IS_MEDIUM_ACCESS_CMD(pkt->pkt_cdbp[0]))) {
2564 		return (sata_txlt_check_condition(spx, KEY_NOT_READY,
2565 		    SD_SCSI_ASC_LU_NOT_READY));
2566 	}
2567 
2568 	/* ATA Disk commands processing starts here */
2569 
2570 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2571 
2572 	switch (pkt->pkt_cdbp[0]) {
2573 
2574 	case SCMD_INQUIRY:
2575 		/* Mapped to identify device */
2576 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2577 			bp_mapin(bp);
2578 		rval = sata_txlt_inquiry(spx);
2579 		break;
2580 
2581 	case SCMD_TEST_UNIT_READY:
2582 		/*
2583 		 * SAT "SATA to ATA Translation" doc specifies translation
2584 		 * to ATA CHECK POWER MODE.
2585 		 */
2586 		rval = sata_txlt_test_unit_ready(spx);
2587 		break;
2588 
2589 	case SCMD_START_STOP:
2590 		/* Mapping depends on the command */
2591 		rval = sata_txlt_start_stop_unit(spx);
2592 		break;
2593 
2594 	case SCMD_READ_CAPACITY:
2595 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2596 			bp_mapin(bp);
2597 		rval = sata_txlt_read_capacity(spx);
2598 		break;
2599 
2600 	case SCMD_SVC_ACTION_IN_G4:		/* READ CAPACITY (16) */
2601 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2602 			bp_mapin(bp);
2603 		rval = sata_txlt_read_capacity16(spx);
2604 		break;
2605 
2606 	case SCMD_REQUEST_SENSE:
2607 		/*
2608 		 * Always No Sense, since we force ARQ
2609 		 */
2610 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2611 			bp_mapin(bp);
2612 		rval = sata_txlt_request_sense(spx);
2613 		break;
2614 
2615 	case SCMD_LOG_SENSE_G1:
2616 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2617 			bp_mapin(bp);
2618 		rval = sata_txlt_log_sense(spx);
2619 		break;
2620 
2621 	case SCMD_LOG_SELECT_G1:
2622 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2623 			bp_mapin(bp);
2624 		rval = sata_txlt_log_select(spx);
2625 		break;
2626 
2627 	case SCMD_MODE_SENSE:
2628 	case SCMD_MODE_SENSE_G1:
2629 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2630 			bp_mapin(bp);
2631 		rval = sata_txlt_mode_sense(spx);
2632 		break;
2633 
2634 
2635 	case SCMD_MODE_SELECT:
2636 	case SCMD_MODE_SELECT_G1:
2637 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2638 			bp_mapin(bp);
2639 		rval = sata_txlt_mode_select(spx);
2640 		break;
2641 
2642 	case SCMD_SYNCHRONIZE_CACHE:
2643 	case SCMD_SYNCHRONIZE_CACHE_G1:
2644 		rval = sata_txlt_synchronize_cache(spx);
2645 		break;
2646 
2647 	case SCMD_READ:
2648 	case SCMD_READ_G1:
2649 	case SCMD_READ_G4:
2650 	case SCMD_READ_G5:
2651 		rval = sata_txlt_read(spx);
2652 		break;
2653 	case SCMD_WRITE_BUFFER:
2654 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2655 			bp_mapin(bp);
2656 		rval = sata_txlt_write_buffer(spx);
2657 		break;
2658 
2659 	case SCMD_WRITE:
2660 	case SCMD_WRITE_G1:
2661 	case SCMD_WRITE_G4:
2662 	case SCMD_WRITE_G5:
2663 		rval = sata_txlt_write(spx);
2664 		break;
2665 
2666 	case SCMD_SEEK:
2667 		rval = sata_txlt_nodata_cmd_immediate(spx);
2668 		break;
2669 
2670 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12:
2671 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16:
2672 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2673 			bp_mapin(bp);
2674 		rval = sata_txlt_ata_pass_thru(spx);
2675 		break;
2676 
2677 		/* Other cases will be filed later */
2678 		/* postponed until phase 2 of the development */
2679 	case SPC3_CMD_UNMAP:
2680 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2681 			bp_mapin(bp);
2682 		rval = sata_txlt_unmap(spx);
2683 		break;
2684 	default:
2685 		rval = sata_txlt_invalid_command(spx);
2686 		break;
2687 	}
2688 
2689 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2690 	    "sata_scsi_start: rval %d\n", rval);
2691 
2692 	return (rval);
2693 }
2694 
2695 /*
2696  * Implementation of scsi tran_abort.
2697  * Abort specific pkt or all packets.
2698  *
2699  * Returns 1 if one or more packets were aborted, returns 0 otherwise
2700  *
2701  * May be called from an interrupt level.
2702  */
2703 static int
2704 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt)
2705 {
2706 	sata_hba_inst_t *sata_hba_inst =
2707 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2708 	sata_device_t	sata_device;
2709 	sata_pkt_t	*sata_pkt;
2710 
2711 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2712 	    "sata_scsi_abort: %s at target: 0x%x\n",
2713 	    scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target);
2714 
2715 	/* Validate address */
2716 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0)
2717 		/* Invalid address */
2718 		return (0);
2719 
2720 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2721 	    sata_device.satadev_addr.cport)));
2722 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2723 		/* invalid address */
2724 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2725 		    sata_device.satadev_addr.cport)));
2726 		return (0);
2727 	}
2728 	if (scsi_pkt == NULL) {
2729 		/*
2730 		 * Abort all packets.
2731 		 * Although we do not have specific packet, we still need
2732 		 * dummy packet structure to pass device address to HBA.
2733 		 * Allocate one, without sleeping. Fail if pkt cannot be
2734 		 * allocated.
2735 		 */
2736 		sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP);
2737 		if (sata_pkt == NULL) {
2738 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2739 			    sata_device.satadev_addr.cport)));
2740 			SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: "
2741 			    "could not allocate sata_pkt"));
2742 			return (0);
2743 		}
2744 		sata_pkt->satapkt_rev = SATA_PKT_REV;
2745 		sata_pkt->satapkt_device = sata_device;
2746 		sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
2747 	} else {
2748 		if (scsi_pkt->pkt_ha_private == NULL) {
2749 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2750 			    sata_device.satadev_addr.cport)));
2751 			return (0); /* Bad scsi pkt */
2752 		}
2753 		/* extract pointer to sata pkt */
2754 		sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)->
2755 		    txlt_sata_pkt;
2756 	}
2757 
2758 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2759 	    sata_device.satadev_addr.cport)));
2760 	/* Send abort request to HBA */
2761 	if ((*SATA_ABORT_FUNC(sata_hba_inst))
2762 	    (SATA_DIP(sata_hba_inst), sata_pkt,
2763 	    scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) ==
2764 	    SATA_SUCCESS) {
2765 		if (scsi_pkt == NULL)
2766 			kmem_free(sata_pkt, sizeof (sata_pkt_t));
2767 		/* Success */
2768 		return (1);
2769 	}
2770 	/* Else, something did not go right */
2771 	if (scsi_pkt == NULL)
2772 		kmem_free(sata_pkt, sizeof (sata_pkt_t));
2773 	/* Failure */
2774 	return (0);
2775 }
2776 
2777 
2778 /*
2779  * Implementation of scsi tran_reset.
2780  * RESET_ALL request is translated into port reset.
2781  * RESET_TARGET requests is translated into a device reset,
2782  * RESET_LUN request is accepted only for LUN 0 and translated into
2783  * device reset.
2784  * The target reset should cause all HBA active and queued packets to
2785  * be terminated and returned with pkt reason SATA_PKT_RESET prior to
2786  * the return. HBA should report reset event for the device.
2787  *
2788  * Returns 1 upon success, 0 upon failure.
2789  */
2790 static int
2791 sata_scsi_reset(struct scsi_address *ap, int level)
2792 {
2793 	sata_hba_inst_t	*sata_hba_inst =
2794 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2795 	sata_device_t	sata_device;
2796 	int		val;
2797 
2798 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2799 	    "sata_scsi_reset: level %d target: 0x%x\n",
2800 	    level, ap->a_target);
2801 
2802 	/* Validate address */
2803 	val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device);
2804 	if (val == -1)
2805 		/* Invalid address */
2806 		return (0);
2807 
2808 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2809 	    sata_device.satadev_addr.cport)));
2810 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2811 		/* invalid address */
2812 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2813 		    sata_device.satadev_addr.cport)));
2814 		return (0);
2815 	}
2816 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2817 	    sata_device.satadev_addr.cport)));
2818 	if (level == RESET_ALL) {
2819 		/* port reset */
2820 		if (sata_device.satadev_addr.qual == SATA_ADDR_DCPORT)
2821 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2822 		else
2823 			sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
2824 
2825 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2826 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2827 			return (1);
2828 		else
2829 			return (0);
2830 
2831 	} else if (val == 0 &&
2832 	    (level == RESET_TARGET || level == RESET_LUN)) {
2833 		/* reset device (device attached) */
2834 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2835 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2836 			return (1);
2837 		else
2838 			return (0);
2839 	}
2840 	return (0);
2841 }
2842 
2843 
2844 /*
2845  * Implementation of scsi tran_getcap (get transport/device capabilities).
2846  * Supported capabilities for SATA hard disks:
2847  * auto-rqsense		(always supported)
2848  * tagged-qing		(supported if HBA supports it)
2849  * untagged-qing	(could be supported if disk supports it, but because
2850  *			 caching behavior allowing untagged queuing actually
2851  *			 results in reduced performance.  sd tries to throttle
2852  *			 back to only 3 outstanding commands, which may
2853  *			 work for real SCSI disks, but with read ahead
2854  *			 caching, having more than 1 outstanding command
2855  *			 results in cache thrashing.)
2856  * sector_size
2857  * dma_max
2858  * interconnect-type	(INTERCONNECT_SATA)
2859  *
2860  * Supported capabilities for ATAPI CD/DVD devices:
2861  * auto-rqsense		(always supported)
2862  * sector_size
2863  * dma_max
2864  * max-cdb-length
2865  * interconnect-type	(INTERCONNECT_SATA)
2866  *
2867  * Supported capabilities for ATAPI TAPE devices:
2868  * auto-rqsense		(always supported)
2869  * dma_max
2870  * max-cdb-length
2871  *
2872  * Supported capabilities for SATA ATAPI hard disks:
2873  * auto-rqsense		(always supported)
2874  * interconnect-type	(INTERCONNECT_SATA)
2875  * max-cdb-length
2876  *
2877  * Request for other capabilities is rejected as unsupported.
2878  *
2879  * Returns supported capability value, or -1 if capability is unsuppported or
2880  * the address is invalid - no device.
2881  */
2882 
2883 static int
2884 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
2885 {
2886 
2887 	sata_hba_inst_t 	*sata_hba_inst =
2888 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2889 	sata_device_t		sata_device;
2890 	sata_drive_info_t	*sdinfo;
2891 	ddi_dma_attr_t		adj_dma_attr;
2892 	int 			rval;
2893 
2894 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2895 	    "sata_scsi_getcap: target: 0x%x, cap: %s\n",
2896 	    ap->a_target, cap);
2897 
2898 	/*
2899 	 * We want to process the capabilities on per port granularity.
2900 	 * So, we are specifically restricting ourselves to whom != 0
2901 	 * to exclude the controller wide handling.
2902 	 */
2903 	if (cap == NULL || whom == 0)
2904 		return (-1);
2905 
2906 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2907 		/* Invalid address */
2908 		return (-1);
2909 	}
2910 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2911 	    sata_device.satadev_addr.cport)));
2912 	if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) ==
2913 	    NULL) {
2914 		/* invalid address */
2915 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2916 		    sata_device.satadev_addr.cport)));
2917 		return (-1);
2918 	}
2919 
2920 	switch (scsi_hba_lookup_capstr(cap)) {
2921 	case SCSI_CAP_ARQ:
2922 		rval = 1;		/* ARQ supported, turned on */
2923 		break;
2924 
2925 	case SCSI_CAP_SECTOR_SIZE:
2926 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK)
2927 			rval = SATA_DISK_SECTOR_SIZE;	/* fixed size */
2928 		else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD)
2929 			rval = SATA_ATAPI_SECTOR_SIZE;
2930 		else rval = -1;
2931 		break;
2932 
2933 	/*
2934 	 * untagged queuing cause a performance inversion because of
2935 	 * the way sd operates.  Because of this reason we do not
2936 	 * use it when available.
2937 	 */
2938 	case SCSI_CAP_UNTAGGED_QING:
2939 		if (sdinfo->satadrv_features_enabled &
2940 		    SATA_DEV_F_E_UNTAGGED_QING)
2941 			rval = 1;	/* Untagged queuing available */
2942 		else
2943 			rval = -1;	/* Untagged queuing not available */
2944 		break;
2945 
2946 	case SCSI_CAP_TAGGED_QING:
2947 		if ((sdinfo->satadrv_features_enabled &
2948 		    SATA_DEV_F_E_TAGGED_QING) &&
2949 		    (sdinfo->satadrv_max_queue_depth > 1))
2950 			rval = 1;	/* Tagged queuing available */
2951 		else
2952 			rval = -1;	/* Tagged queuing not available */
2953 		break;
2954 
2955 	case SCSI_CAP_DMA_MAX:
2956 		sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst),
2957 		    &adj_dma_attr);
2958 		rval = (int)adj_dma_attr.dma_attr_maxxfer;
2959 		/* We rely on the fact that dma_attr_maxxfer < 0x80000000 */
2960 		break;
2961 
2962 	case SCSI_CAP_INTERCONNECT_TYPE:
2963 		rval = INTERCONNECT_SATA;	/* SATA interconnect type */
2964 		break;
2965 
2966 	case SCSI_CAP_CDB_LEN:
2967 		if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI)
2968 			rval = sdinfo->satadrv_atapi_cdb_len;
2969 		else
2970 			rval = -1;
2971 		break;
2972 
2973 	default:
2974 		rval = -1;
2975 		break;
2976 	}
2977 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2978 	    sata_device.satadev_addr.cport)));
2979 	return (rval);
2980 }
2981 
2982 /*
2983  * Implementation of scsi tran_setcap
2984  *
2985  * Only SCSI_CAP_UNTAGGED_QING and  SCSI_CAP_TAGGED_QING are changeable.
2986  *
2987  */
2988 static int
2989 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
2990 {
2991 	sata_hba_inst_t	*sata_hba_inst =
2992 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2993 	sata_device_t	sata_device;
2994 	sata_drive_info_t	*sdinfo;
2995 	int		rval;
2996 
2997 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2998 	    "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap);
2999 
3000 	/*
3001 	 * We want to process the capabilities on per port granularity.
3002 	 * So, we are specifically restricting ourselves to whom != 0
3003 	 * to exclude the controller wide handling.
3004 	 */
3005 	if (cap == NULL || whom == 0) {
3006 		return (-1);
3007 	}
3008 
3009 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
3010 		/* Invalid address */
3011 		return (-1);
3012 	}
3013 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
3014 	    sata_device.satadev_addr.cport)));
3015 	if ((sdinfo = sata_get_device_info(sata_hba_inst,
3016 	    &sata_device)) == NULL) {
3017 		/* invalid address */
3018 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3019 		    sata_device.satadev_addr.cport)));
3020 		return (-1);
3021 	}
3022 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3023 	    sata_device.satadev_addr.cport)));
3024 
3025 	switch (scsi_hba_lookup_capstr(cap)) {
3026 	case SCSI_CAP_ARQ:
3027 	case SCSI_CAP_SECTOR_SIZE:
3028 	case SCSI_CAP_DMA_MAX:
3029 	case SCSI_CAP_INTERCONNECT_TYPE:
3030 		rval = 0;
3031 		break;
3032 	case SCSI_CAP_UNTAGGED_QING:
3033 		if (SATA_QDEPTH(sata_hba_inst) > 1) {
3034 			rval = 1;
3035 			if (value == 1) {
3036 				sdinfo->satadrv_features_enabled |=
3037 				    SATA_DEV_F_E_UNTAGGED_QING;
3038 			} else if (value == 0) {
3039 				sdinfo->satadrv_features_enabled &=
3040 				    ~SATA_DEV_F_E_UNTAGGED_QING;
3041 			} else {
3042 				rval = -1;
3043 			}
3044 		} else {
3045 			rval = 0;
3046 		}
3047 		break;
3048 	case SCSI_CAP_TAGGED_QING:
3049 		/* This can TCQ or NCQ */
3050 		if (sata_func_enable & SATA_ENABLE_QUEUING &&
3051 		    ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ &&
3052 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) ||
3053 		    (sata_func_enable & SATA_ENABLE_NCQ &&
3054 		    sdinfo->satadrv_features_support & SATA_DEV_F_NCQ &&
3055 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) &&
3056 		    (sdinfo->satadrv_max_queue_depth > 1)) {
3057 			rval = 1;
3058 			if (value == 1) {
3059 				sdinfo->satadrv_features_enabled |=
3060 				    SATA_DEV_F_E_TAGGED_QING;
3061 			} else if (value == 0) {
3062 				sdinfo->satadrv_features_enabled &=
3063 				    ~SATA_DEV_F_E_TAGGED_QING;
3064 			} else {
3065 				rval = -1;
3066 			}
3067 		} else {
3068 			rval = 0;
3069 		}
3070 		break;
3071 	default:
3072 		rval = -1;
3073 		break;
3074 	}
3075 	return (rval);
3076 }
3077 
3078 /*
3079  * Implementations of scsi tran_destroy_pkt.
3080  * Free resources allocated by sata_scsi_init_pkt()
3081  */
3082 static void
3083 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3084 {
3085 	sata_pkt_txlate_t *spx;
3086 
3087 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3088 
3089 	sata_common_free_dma_rsrcs(spx);
3090 
3091 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
3092 	sata_pkt_free(spx);
3093 
3094 	scsi_hba_pkt_free(ap, pkt);
3095 }
3096 
3097 /*
3098  * Implementation of scsi tran_dmafree.
3099  * Free DMA resources allocated by sata_scsi_init_pkt()
3100  */
3101 
3102 static void
3103 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
3104 {
3105 #ifndef __lock_lint
3106 	_NOTE(ARGUNUSED(ap))
3107 #endif
3108 	sata_pkt_txlate_t *spx;
3109 
3110 	ASSERT(pkt != NULL);
3111 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3112 
3113 	sata_common_free_dma_rsrcs(spx);
3114 }
3115 
3116 /*
3117  * Implementation of scsi tran_sync_pkt.
3118  *
3119  * The assumption below is that pkt is unique - there is no need to check ap
3120  *
3121  * Synchronize DMA buffer and, if the intermediate buffer is used, copy data
3122  * into/from the real buffer.
3123  */
3124 static void
3125 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3126 {
3127 #ifndef __lock_lint
3128 	_NOTE(ARGUNUSED(ap))
3129 #endif
3130 	int rval;
3131 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3132 	struct buf *bp;
3133 	int direction;
3134 
3135 	ASSERT(spx != NULL);
3136 	if (spx->txlt_buf_dma_handle != NULL) {
3137 		direction = spx->txlt_sata_pkt->
3138 		    satapkt_cmd.satacmd_flags.sata_data_direction;
3139 		if (spx->txlt_sata_pkt != NULL &&
3140 		    direction != SATA_DIR_NODATA_XFER) {
3141 			if (spx->txlt_tmp_buf != NULL) {
3142 				/* Intermediate DMA buffer used */
3143 				bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3144 
3145 				if (direction & SATA_DIR_WRITE) {
3146 					bcopy(bp->b_un.b_addr,
3147 					    spx->txlt_tmp_buf, bp->b_bcount);
3148 				}
3149 			}
3150 			/* Sync the buffer for device or for CPU */
3151 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle,   0, 0,
3152 			    (direction & SATA_DIR_WRITE) ?
3153 			    DDI_DMA_SYNC_FORDEV :  DDI_DMA_SYNC_FORCPU);
3154 			ASSERT(rval == DDI_SUCCESS);
3155 			if (spx->txlt_tmp_buf != NULL &&
3156 			    !(direction & SATA_DIR_WRITE)) {
3157 				/* Intermediate DMA buffer used for read */
3158 				bcopy(spx->txlt_tmp_buf,
3159 				    bp->b_un.b_addr, bp->b_bcount);
3160 			}
3161 
3162 		}
3163 	}
3164 }
3165 
3166 
3167 
3168 /* *******************  SATA - SCSI Translation functions **************** */
3169 /*
3170  * SCSI to SATA pkt and command translation and SATA to SCSI status/error
3171  * translation.
3172  */
3173 
3174 /*
3175  * Checks if a device exists and can be access and translates common
3176  * scsi_pkt data to sata_pkt data.
3177  *
3178  * Flag argument indicates that a non-read/write ATA command may be sent
3179  * to HBA in arbitrary SYNC mode to execute this packet.
3180  *
3181  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and
3182  * sata_pkt was set-up.
3183  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not
3184  * exist and pkt_comp callback was scheduled.
3185  * Returns other TRAN_XXXXX values when error occured and command should be
3186  * rejected with the returned TRAN_XXXXX value.
3187  *
3188  * This function should be called with port mutex held.
3189  */
3190 static int
3191 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason, int flag)
3192 {
3193 	sata_drive_info_t *sdinfo;
3194 	sata_device_t sata_device;
3195 	const struct sata_cmd_flags sata_initial_cmd_flags = {
3196 		SATA_DIR_NODATA_XFER,
3197 		/* all other values to 0/FALSE */
3198 	};
3199 	/*
3200 	 * Pkt_reason has to be set if the pkt_comp callback is invoked,
3201 	 * and that implies TRAN_ACCEPT return value. Any other returned value
3202 	 * indicates that the scsi packet was not accepted (the reason will not
3203 	 * be checked by the scsi target driver).
3204 	 * To make debugging easier, we set pkt_reason to know value here.
3205 	 * It may be changed later when different completion reason is
3206 	 * determined.
3207 	 */
3208 	spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
3209 	*reason = CMD_TRAN_ERR;
3210 
3211 	/* Validate address */
3212 	switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst,
3213 	    &spx->txlt_scsi_pkt->pkt_address, &sata_device)) {
3214 
3215 	case -1:
3216 		/* Invalid address or invalid device type */
3217 		return (TRAN_BADPKT);
3218 	case 2:
3219 		/*
3220 		 * Valid address but device type is unknown - Chack if it is
3221 		 * in the reset state and therefore in an indeterminate state.
3222 		 */
3223 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3224 		    &spx->txlt_sata_pkt->satapkt_device);
3225 		if (sdinfo != NULL && (sdinfo->satadrv_event_flags &
3226 		    (SATA_EVNT_DEVICE_RESET |
3227 		    SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3228 			if (!ddi_in_panic()) {
3229 				spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3230 				*reason = CMD_INCOMPLETE;
3231 				SATADBG1(SATA_DBG_SCSI_IF,
3232 				    spx->txlt_sata_hba_inst,
3233 				    "sata_scsi_start: rejecting command "
3234 				    "because of device reset state\n", NULL);
3235 				return (TRAN_BUSY);
3236 			}
3237 		}
3238 		/* FALLTHROUGH */
3239 	case 1:
3240 		/* valid address but no valid device - it has disappeared */
3241 		spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE;
3242 		*reason = CMD_DEV_GONE;
3243 		/*
3244 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
3245 		 * only in callback function (for normal requests) and
3246 		 * in the dump code path.
3247 		 * So, if the callback is available, we need to do
3248 		 * the callback rather than returning TRAN_FATAL_ERROR here.
3249 		 */
3250 		if (spx->txlt_scsi_pkt->pkt_comp != NULL) {
3251 			/* scsi callback required */
3252 			if (servicing_interrupt()) {
3253 				if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3254 				    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3255 				    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3256 				    NULL) {
3257 					return (TRAN_BUSY);
3258 				}
3259 			} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3260 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3261 			    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3262 				/* Scheduling the callback failed */
3263 				return (TRAN_BUSY);
3264 			}
3265 
3266 			return (TRAN_ACCEPT);
3267 		}
3268 		return (TRAN_FATAL_ERROR);
3269 	default:
3270 		/* all OK; pkt reason will be overwritten later */
3271 		break;
3272 	}
3273 	/*
3274 	 * If pkt is to be executed in polling mode and a command will not be
3275 	 * emulated in SATA module (requires sending a non-read/write ATA
3276 	 * command to HBA driver in arbitrary SYNC mode) and we are in the
3277 	 * interrupt context and not in the panic dump, then reject the packet
3278 	 * to avoid a possible interrupt stack overrun or hang caused by
3279 	 * a potentially blocked interrupt.
3280 	 */
3281 	if (((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0 || flag != 0) &&
3282 	    servicing_interrupt() && !ddi_in_panic()) {
3283 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
3284 		    "sata_scsi_start: rejecting synchronous command because "
3285 		    "of interrupt context\n", NULL);
3286 		return (TRAN_BUSY);
3287 	}
3288 
3289 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3290 	    &spx->txlt_sata_pkt->satapkt_device);
3291 
3292 	/*
3293 	 * If device is in reset condition, reject the packet with
3294 	 * TRAN_BUSY, unless:
3295 	 * 1. system is panicking (dumping)
3296 	 * In such case only one thread is running and there is no way to
3297 	 * process reset.
3298 	 * 2. cfgadm operation is is progress (internal APCTL lock is set)
3299 	 * Some cfgadm operations involve drive commands, so reset condition
3300 	 * needs to be ignored for IOCTL operations.
3301 	 */
3302 	if ((sdinfo->satadrv_event_flags &
3303 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3304 
3305 		if (!ddi_in_panic() &&
3306 		    ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst,
3307 		    sata_device.satadev_addr.cport) &
3308 		    SATA_APCTL_LOCK_PORT_BUSY) == 0)) {
3309 			spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3310 			*reason = CMD_INCOMPLETE;
3311 			SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3312 			    "sata_scsi_start: rejecting command because "
3313 			    "of device reset state\n", NULL);
3314 			return (TRAN_BUSY);
3315 		}
3316 	}
3317 
3318 	/*
3319 	 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by
3320 	 * sata_scsi_pkt_init() because pkt init had to work also with
3321 	 * non-existing devices.
3322 	 * Now we know that the packet was set-up for a real device, so its
3323 	 * type is known.
3324 	 */
3325 	spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type;
3326 
3327 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags;
3328 	if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst,
3329 	    sata_device.satadev_addr.cport)->cport_event_flags &
3330 	    SATA_APCTL_LOCK_PORT_BUSY) != 0) {
3331 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3332 		    sata_ignore_dev_reset = B_TRUE;
3333 	}
3334 	/*
3335 	 * At this point the generic translation routine determined that the
3336 	 * scsi packet should be accepted. Packet completion reason may be
3337 	 * changed later when a different completion reason is determined.
3338 	 */
3339 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3340 	*reason = CMD_CMPLT;
3341 
3342 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
3343 		/* Synchronous execution */
3344 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH |
3345 		    SATA_OPMODE_POLLING;
3346 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3347 		    sata_ignore_dev_reset = ddi_in_panic();
3348 	} else {
3349 		/* Asynchronous execution */
3350 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH |
3351 		    SATA_OPMODE_INTERRUPTS;
3352 	}
3353 	/* Convert queuing information */
3354 	if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG)
3355 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag =
3356 		    B_TRUE;
3357 	else if (spx->txlt_scsi_pkt->pkt_flags &
3358 	    (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD))
3359 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag =
3360 		    B_TRUE;
3361 
3362 	/* Always limit pkt time */
3363 	if (spx->txlt_scsi_pkt->pkt_time == 0)
3364 		spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time;
3365 	else
3366 		/* Pass on scsi_pkt time */
3367 		spx->txlt_sata_pkt->satapkt_time =
3368 		    spx->txlt_scsi_pkt->pkt_time;
3369 
3370 	return (TRAN_ACCEPT);
3371 }
3372 
3373 
3374 /*
3375  * Translate ATA Identify Device data to SCSI Inquiry data.
3376  * This function may be called only for ATA devices.
3377  * This function should not be called for ATAPI devices - they
3378  * respond directly to SCSI Inquiry command.
3379  *
3380  * SATA Identify Device data has to be valid in sata_drive_info.
3381  * Buffer has to accomodate the inquiry length (36 bytes).
3382  *
3383  * This function should be called with a port mutex held.
3384  */
3385 static	void
3386 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst,
3387     sata_drive_info_t *sdinfo, uint8_t *buf)
3388 {
3389 
3390 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
3391 	struct sata_id *sid = &sdinfo->satadrv_id;
3392 
3393 	/* Start with a nice clean slate */
3394 	bzero((void *)inq, sizeof (struct scsi_inquiry));
3395 
3396 	/*
3397 	 * Rely on the dev_type for setting paripheral qualifier.
3398 	 * Assume that  DTYPE_RODIRECT applies to CD/DVD R/W devices.
3399 	 * It could be that DTYPE_OPTICAL could also qualify in the future.
3400 	 * ATAPI Inquiry may provide more data to the target driver.
3401 	 */
3402 	inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3403 	    DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */
3404 
3405 	/* CFA type device is not a removable media device */
3406 	inq->inq_rmb = ((sid->ai_config != SATA_CFA_TYPE) &&
3407 	    (sid->ai_config & SATA_REM_MEDIA)) ? 1 : 0;
3408 	inq->inq_qual = 0;	/* Device type qualifier (obsolete in SCSI3? */
3409 	inq->inq_iso = 0;	/* ISO version */
3410 	inq->inq_ecma = 0;	/* ECMA version */
3411 	inq->inq_ansi = 3;	/* ANSI version - SCSI 3 */
3412 	inq->inq_aenc = 0;	/* Async event notification cap. */
3413 	inq->inq_trmiop = 0;	/* Supports TERMINATE I/O PROC msg - NO */
3414 	inq->inq_normaca = 0;	/* setting NACA bit supported - NO */
3415 	inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */
3416 	inq->inq_len = 31;	/* Additional length */
3417 	inq->inq_dualp = 0;	/* dual port device - NO */
3418 	inq->inq_reladdr = 0;	/* Supports relative addressing - NO */
3419 	inq->inq_sync = 0;	/* Supports synchronous data xfers - NO */
3420 	inq->inq_linked = 0;	/* Supports linked commands - NO */
3421 				/*
3422 				 * Queuing support - controller has to
3423 				 * support some sort of command queuing.
3424 				 */
3425 	if (SATA_QDEPTH(sata_hba_inst) > 1)
3426 		inq->inq_cmdque = 1; /* Supports command queueing - YES */
3427 	else
3428 		inq->inq_cmdque = 0; /* Supports command queueing - NO */
3429 	inq->inq_sftre = 0;	/* Supports Soft Reset option - NO ??? */
3430 	inq->inq_wbus32 = 0;	/* Supports 32 bit wide data xfers - NO */
3431 	inq->inq_wbus16 = 0;	/* Supports 16 bit wide data xfers - NO */
3432 
3433 #ifdef	_LITTLE_ENDIAN
3434 	/* Swap text fields to match SCSI format */
3435 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3436 	swab(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3437 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3438 		swab(sid->ai_fw, inq->inq_revision, 4);	/* Revision level */
3439 	else
3440 		swab(&sid->ai_fw[4], inq->inq_revision, 4);	/* Rev. level */
3441 #else	/* _LITTLE_ENDIAN */
3442 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3443 	bcopy(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3444 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3445 		bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
3446 	else
3447 		bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
3448 #endif	/* _LITTLE_ENDIAN */
3449 }
3450 
3451 
3452 /*
3453  * Scsi response set up for invalid command (command not supported)
3454  *
3455  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3456  */
3457 static int
3458 sata_txlt_invalid_command(sata_pkt_txlate_t *spx)
3459 {
3460 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3461 	struct scsi_extended_sense *sense;
3462 
3463 	scsipkt->pkt_reason = CMD_CMPLT;
3464 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3465 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3466 
3467 	*scsipkt->pkt_scbp = STATUS_CHECK;
3468 
3469 	sense = sata_arq_sense(spx);
3470 	sense->es_key = KEY_ILLEGAL_REQUEST;
3471 	sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE;
3472 
3473 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3474 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3475 
3476 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3477 	    scsipkt->pkt_comp != NULL) {
3478 		/* scsi callback required */
3479 		if (servicing_interrupt()) {
3480 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3481 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3482 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3483 				return (TRAN_BUSY);
3484 			}
3485 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3486 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3487 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3488 			/* Scheduling the callback failed */
3489 			return (TRAN_BUSY);
3490 		}
3491 	}
3492 	return (TRAN_ACCEPT);
3493 }
3494 
3495 /*
3496  * Scsi response set up for check condition with special sense key
3497  * and additional sense code.
3498  *
3499  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3500  */
3501 static int
3502 sata_txlt_check_condition(sata_pkt_txlate_t *spx, uchar_t key, uchar_t code)
3503 {
3504 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3505 	int cport = SATA_TXLT_CPORT(spx);
3506 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3507 	struct scsi_extended_sense *sense;
3508 
3509 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3510 	scsipkt->pkt_reason = CMD_CMPLT;
3511 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3512 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3513 
3514 	*scsipkt->pkt_scbp = STATUS_CHECK;
3515 
3516 	sense = sata_arq_sense(spx);
3517 	sense->es_key = key;
3518 	sense->es_add_code = code;
3519 
3520 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3521 
3522 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3523 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3524 
3525 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3526 	    scsipkt->pkt_comp != NULL) {
3527 		/* scsi callback required */
3528 		if (servicing_interrupt()) {
3529 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3530 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3531 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3532 				return (TRAN_BUSY);
3533 			}
3534 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3535 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3536 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3537 			/* Scheduling the callback failed */
3538 			return (TRAN_BUSY);
3539 		}
3540 	}
3541 	return (TRAN_ACCEPT);
3542 }
3543 
3544 /*
3545  * Scsi response setup for
3546  * emulated non-data command that requires no action/return data
3547  *
3548  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3549  */
3550 static	int
3551 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx)
3552 {
3553 	int rval;
3554 	int reason;
3555 	kmutex_t *cport_mutex =  &(SATA_TXLT_CPORT_MUTEX(spx));
3556 
3557 	mutex_enter(cport_mutex);
3558 
3559 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
3560 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3561 		mutex_exit(cport_mutex);
3562 		return (rval);
3563 	}
3564 	mutex_exit(cport_mutex);
3565 
3566 	spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3567 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3568 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3569 	*(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD;
3570 
3571 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3572 	    "Scsi_pkt completion reason %x\n",
3573 	    spx->txlt_scsi_pkt->pkt_reason);
3574 
3575 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 &&
3576 	    spx->txlt_scsi_pkt->pkt_comp != NULL) {
3577 		/* scsi callback required */
3578 		if (servicing_interrupt()) {
3579 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3580 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3581 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3582 				return (TRAN_BUSY);
3583 			}
3584 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3585 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3586 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3587 			/* Scheduling the callback failed */
3588 			return (TRAN_BUSY);
3589 		}
3590 	}
3591 	return (TRAN_ACCEPT);
3592 }
3593 
3594 
3595 /*
3596  * SATA translate command: Inquiry / Identify Device
3597  * Use cached Identify Device data for now, rather than issuing actual
3598  * Device Identify cmd request. If device is detached and re-attached,
3599  * asynchronous event processing should fetch and refresh Identify Device
3600  * data.
3601  * VPD pages supported now:
3602  * Vital Product Data page
3603  * Unit Serial Number page
3604  * Block Device Characteristics Page
3605  * ATA Information Page
3606  *
3607  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3608  */
3609 
3610 #define	EVPD			1	/* Extended Vital Product Data flag */
3611 #define	CMDDT			2	/* Command Support Data - Obsolete */
3612 #define	INQUIRY_SUP_VPD_PAGE	0	/* Supported VPD Pages Page Code */
3613 #define	INQUIRY_USN_PAGE	0x80	/* Unit Serial Number Page Code */
3614 #define	INQUIRY_BDC_PAGE	0xB1	/* Block Device Characteristics Page */
3615 					/* Code */
3616 #define	INQUIRY_ATA_INFO_PAGE	0x89	/* ATA Information Page Code */
3617 #define	INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */
3618 
3619 static int
3620 sata_txlt_inquiry(sata_pkt_txlate_t *spx)
3621 {
3622 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3623 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3624 	sata_drive_info_t *sdinfo;
3625 	struct scsi_extended_sense *sense;
3626 	int count;
3627 	uint8_t *p;
3628 	int i, j;
3629 	uint8_t page_buf[1024]; /* Max length */
3630 	int rval, reason;
3631 	ushort_t rate;
3632 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
3633 
3634 	mutex_enter(cport_mutex);
3635 
3636 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
3637 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3638 		mutex_exit(cport_mutex);
3639 		return (rval);
3640 	}
3641 
3642 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3643 	    &spx->txlt_sata_pkt->satapkt_device);
3644 
3645 	ASSERT(sdinfo != NULL);
3646 
3647 	scsipkt->pkt_reason = CMD_CMPLT;
3648 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3649 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3650 
3651 	/* Reject not supported request */
3652 	if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */
3653 		*scsipkt->pkt_scbp = STATUS_CHECK;
3654 		sense = sata_arq_sense(spx);
3655 		sense->es_key = KEY_ILLEGAL_REQUEST;
3656 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3657 		goto done;
3658 	}
3659 
3660 	/* Valid Inquiry request */
3661 	*scsipkt->pkt_scbp = STATUS_GOOD;
3662 
3663 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3664 
3665 		/*
3666 		 * Because it is fully emulated command storing data
3667 		 * programatically in the specified buffer, release
3668 		 * preallocated DMA resources before storing data in the buffer,
3669 		 * so no unwanted DMA sync would take place.
3670 		 */
3671 		sata_scsi_dmafree(NULL, scsipkt);
3672 
3673 		if (!(scsipkt->pkt_cdbp[1] & EVPD)) {
3674 			/* Standard Inquiry Data request */
3675 			struct scsi_inquiry inq;
3676 			unsigned int bufsize;
3677 
3678 			sata_identdev_to_inquiry(spx->txlt_sata_hba_inst,
3679 			    sdinfo, (uint8_t *)&inq);
3680 			/* Copy no more than requested */
3681 			count = MIN(bp->b_bcount,
3682 			    sizeof (struct scsi_inquiry));
3683 			bufsize = scsipkt->pkt_cdbp[4];
3684 			bufsize |= scsipkt->pkt_cdbp[3] << 8;
3685 			count = MIN(count, bufsize);
3686 			bcopy(&inq, bp->b_un.b_addr, count);
3687 
3688 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
3689 			scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3690 			    bufsize - count : 0;
3691 		} else {
3692 			/*
3693 			 * peripheral_qualifier = 0;
3694 			 *
3695 			 * We are dealing only with HD and will be
3696 			 * dealing with CD/DVD devices soon
3697 			 */
3698 			uint8_t peripheral_device_type =
3699 			    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3700 			    DTYPE_DIRECT : DTYPE_RODIRECT;
3701 
3702 			bzero(page_buf, sizeof (page_buf));
3703 
3704 			switch ((uint_t)scsipkt->pkt_cdbp[2]) {
3705 			case INQUIRY_SUP_VPD_PAGE:
3706 				/*
3707 				 * Request for supported Vital Product Data
3708 				 * pages.
3709 				 */
3710 				page_buf[0] = peripheral_device_type;
3711 				page_buf[1] = INQUIRY_SUP_VPD_PAGE;
3712 				page_buf[2] = 0;
3713 				page_buf[3] = 4; /* page length */
3714 				page_buf[4] = INQUIRY_SUP_VPD_PAGE;
3715 				page_buf[5] = INQUIRY_USN_PAGE;
3716 				page_buf[6] = INQUIRY_BDC_PAGE;
3717 				page_buf[7] = INQUIRY_ATA_INFO_PAGE;
3718 				/* Copy no more than requested */
3719 				count = MIN(bp->b_bcount, 8);
3720 				bcopy(page_buf, bp->b_un.b_addr, count);
3721 				break;
3722 
3723 			case INQUIRY_USN_PAGE:
3724 				/*
3725 				 * Request for Unit Serial Number page.
3726 				 * Set-up the page.
3727 				 */
3728 				page_buf[0] = peripheral_device_type;
3729 				page_buf[1] = INQUIRY_USN_PAGE;
3730 				page_buf[2] = 0;
3731 				/* remaining page length */
3732 				page_buf[3] = SATA_ID_SERIAL_LEN;
3733 
3734 				/*
3735 				 * Copy serial number from Identify Device data
3736 				 * words into the inquiry page and swap bytes
3737 				 * when necessary.
3738 				 */
3739 				p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser);
3740 #ifdef	_LITTLE_ENDIAN
3741 				swab(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3742 #else
3743 				bcopy(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3744 #endif
3745 				/*
3746 				 * Least significant character of the serial
3747 				 * number shall appear as the last byte,
3748 				 * according to SBC-3 spec.
3749 				 * Count trailing spaces to determine the
3750 				 * necessary shift length.
3751 				 */
3752 				p = &page_buf[SATA_ID_SERIAL_LEN + 4 - 1];
3753 				for (j = 0; j < SATA_ID_SERIAL_LEN; j++) {
3754 					if (*(p - j) != '\0' &&
3755 					    *(p - j) != '\040')
3756 						break;
3757 				}
3758 
3759 				/*
3760 				 * Shift SN string right, so that the last
3761 				 * non-blank character would appear in last
3762 				 * byte of SN field in the page.
3763 				 * 'j' is the shift length.
3764 				 */
3765 				for (i = 0;
3766 				    i < (SATA_ID_SERIAL_LEN - j) && j != 0;
3767 				    i++, p--)
3768 					*p = *(p - j);
3769 
3770 				/*
3771 				 * Add leading spaces - same number as the
3772 				 * shift size
3773 				 */
3774 				for (; j > 0; j--)
3775 					page_buf[4 + j - 1] = '\040';
3776 
3777 				count = MIN(bp->b_bcount,
3778 				    SATA_ID_SERIAL_LEN + 4);
3779 				bcopy(page_buf, bp->b_un.b_addr, count);
3780 				break;
3781 
3782 			case INQUIRY_BDC_PAGE:
3783 				/*
3784 				 * Request for Block Device Characteristics
3785 				 * page.  Set-up the page.
3786 				 */
3787 				page_buf[0] = peripheral_device_type;
3788 				page_buf[1] = INQUIRY_BDC_PAGE;
3789 				page_buf[2] = 0;
3790 				/* remaining page length */
3791 				page_buf[3] = SATA_ID_BDC_LEN;
3792 
3793 				rate = sdinfo->satadrv_id.ai_medrotrate;
3794 				page_buf[4] = (rate >> 8) & 0xff;
3795 				page_buf[5] = rate & 0xff;
3796 				page_buf[6] = 0;
3797 				page_buf[7] = sdinfo->satadrv_id.
3798 				    ai_nomformfactor & 0xf;
3799 
3800 				count = MIN(bp->b_bcount,
3801 				    SATA_ID_BDC_LEN + 4);
3802 				bcopy(page_buf, bp->b_un.b_addr, count);
3803 				break;
3804 
3805 			case INQUIRY_ATA_INFO_PAGE:
3806 				/*
3807 				 * Request for ATA Information page.
3808 				 */
3809 				page_buf[0] = peripheral_device_type;
3810 				page_buf[1] = INQUIRY_ATA_INFO_PAGE;
3811 				page_buf[2] = (SATA_ID_ATA_INFO_LEN >> 8) &
3812 				    0xff;
3813 				page_buf[3] = SATA_ID_ATA_INFO_LEN & 0xff;
3814 				/* page_buf[4-7] reserved */
3815 #ifdef  _LITTLE_ENDIAN
3816 				bcopy("ATA     ", &page_buf[8], 8);
3817 				swab(sdinfo->satadrv_id.ai_model,
3818 				    &page_buf[16], 16);
3819 				if (strncmp(&sdinfo->satadrv_id.ai_fw[4],
3820 				    "    ", 4) == 0) {
3821 					swab(sdinfo->satadrv_id.ai_fw,
3822 					    &page_buf[32], 4);
3823 				} else {
3824 					swab(&sdinfo->satadrv_id.ai_fw[4],
3825 					    &page_buf[32], 4);
3826 				}
3827 #else   /* _LITTLE_ENDIAN */
3828 				bcopy("ATA     ", &page_buf[8], 8);
3829 				bcopy(sdinfo->satadrv_id.ai_model,
3830 				    &page_buf[16], 16);
3831 				if (strncmp(&sdinfo->satadrv_id.ai_fw[4],
3832 				    "    ", 4) == 0) {
3833 					bcopy(sdinfo->satadrv_id.ai_fw,
3834 					    &page_buf[32], 4);
3835 				} else {
3836 					bcopy(&sdinfo->satadrv_id.ai_fw[4],
3837 					    &page_buf[32], 4);
3838 				}
3839 #endif  /* _LITTLE_ENDIAN */
3840 				/*
3841 				 * page_buf[36-55] which defines the device
3842 				 * signature is not defined at this
3843 				 * time.
3844 				 */
3845 
3846 				/* Set the command code */
3847 				if (sdinfo->satadrv_type ==
3848 				    SATA_DTYPE_ATADISK) {
3849 					page_buf[56] = SATAC_ID_DEVICE;
3850 				} else if (sdinfo->satadrv_type ==
3851 				    SATA_DTYPE_ATAPI) {
3852 					page_buf[56] = SATAC_ID_PACKET_DEVICE;
3853 				}
3854 				/*
3855 				 * If the command code, page_buf[56], is not
3856 				 * zero and if one of the identify commands
3857 				 * succeeds, return the identify data.
3858 				 */
3859 				if ((page_buf[56] != 0) &&
3860 				    (sata_fetch_device_identify_data(
3861 				    spx->txlt_sata_hba_inst, sdinfo) ==
3862 				    SATA_SUCCESS)) {
3863 					bcopy(&sdinfo->satadrv_id,
3864 					    &page_buf[60], sizeof (sata_id_t));
3865 				}
3866 
3867 				/* Need to copy out the page_buf to bp */
3868 				count = MIN(bp->b_bcount,
3869 				    SATA_ID_ATA_INFO_LEN + 4);
3870 				bcopy(page_buf, bp->b_un.b_addr, count);
3871 				break;
3872 
3873 			case INQUIRY_DEV_IDENTIFICATION_PAGE:
3874 				/*
3875 				 * We may want to implement this page, when
3876 				 * identifiers are common for SATA devices
3877 				 * But not now.
3878 				 */
3879 				/*FALLTHROUGH*/
3880 
3881 			default:
3882 				/* Request for unsupported VPD page */
3883 				*scsipkt->pkt_scbp = STATUS_CHECK;
3884 				sense = sata_arq_sense(spx);
3885 				sense->es_key = KEY_ILLEGAL_REQUEST;
3886 				sense->es_add_code =
3887 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3888 				goto done;
3889 			}
3890 		}
3891 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3892 		scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3893 		    scsipkt->pkt_cdbp[4] - count : 0;
3894 	}
3895 done:
3896 	mutex_exit(cport_mutex);
3897 
3898 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3899 	    "Scsi_pkt completion reason %x\n",
3900 	    scsipkt->pkt_reason);
3901 
3902 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3903 	    scsipkt->pkt_comp != NULL) {
3904 		/* scsi callback required */
3905 		if (servicing_interrupt()) {
3906 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3907 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3908 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
3909 				return (TRAN_BUSY);
3910 			}
3911 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3912 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3913 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
3914 			/* Scheduling the callback failed */
3915 			return (TRAN_BUSY);
3916 		}
3917 	}
3918 	return (TRAN_ACCEPT);
3919 }
3920 
3921 /*
3922  * SATA translate command: Request Sense.
3923  *
3924  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3925  * At the moment this is an emulated command (ATA version for SATA hard disks).
3926  * May be translated into Check Power Mode command in the future.
3927  *
3928  * Note: There is a mismatch between already implemented Informational
3929  * Exception Mode Select page 0x1C and this function.
3930  * When MRIE bit is set in page 0x1C, Request Sense is supposed to return
3931  * NO SENSE and set additional sense code to the exception code - this is not
3932  * implemented here.
3933  */
3934 static int
3935 sata_txlt_request_sense(sata_pkt_txlate_t *spx)
3936 {
3937 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3938 	struct scsi_extended_sense sense;
3939 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3940 	sata_drive_info_t *sdinfo;
3941 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3942 	int rval, reason, power_state = 0;
3943 	kmutex_t *cport_mutex;
3944 
3945 	cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
3946 	mutex_enter(cport_mutex);
3947 
3948 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
3949 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3950 		mutex_exit(cport_mutex);
3951 		return (rval);
3952 	}
3953 
3954 	scsipkt->pkt_reason = CMD_CMPLT;
3955 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3956 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3957 	*scsipkt->pkt_scbp = STATUS_GOOD;
3958 
3959 	/*
3960 	 * when CONTROL field's NACA bit == 1
3961 	 * return ILLEGAL_REQUEST
3962 	 */
3963 	if (scsipkt->pkt_cdbp[5] & CTL_BYTE_NACA_MASK) {
3964 		mutex_exit(cport_mutex);
3965 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
3966 		    SD_SCSI_ASC_CMD_SEQUENCE_ERR));
3967 	}
3968 
3969 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3970 	    &spx->txlt_sata_pkt->satapkt_device);
3971 	ASSERT(sdinfo != NULL);
3972 
3973 	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
3974 
3975 	sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
3976 	scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
3977 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3978 	if (sata_hba_start(spx, &rval) != 0) {
3979 		mutex_exit(cport_mutex);
3980 		return (rval);
3981 	}
3982 	if (scmd->satacmd_error_reg != 0) {
3983 		mutex_exit(cport_mutex);
3984 		return (sata_txlt_check_condition(spx, KEY_NO_SENSE,
3985 		    SD_SCSI_ASC_NO_ADD_SENSE));
3986 	}
3987 
3988 	switch (scmd->satacmd_sec_count_lsb) {
3989 	case SATA_PWRMODE_STANDBY: /* device in standby mode */
3990 		if (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)
3991 			power_state = SATA_POWER_STOPPED;
3992 		else {
3993 			power_state = SATA_POWER_STANDBY;
3994 			sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
3995 		}
3996 		break;
3997 	case SATA_PWRMODE_IDLE: /* device in idle mode */
3998 		power_state = SATA_POWER_IDLE;
3999 		sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4000 		break;
4001 	case SATA_PWRMODE_ACTIVE: /* device in active or idle mode */
4002 	default:		  /* 0x40, 0x41 active mode */
4003 		if (sdinfo->satadrv_power_level == SATA_POWER_IDLE)
4004 			power_state = SATA_POWER_IDLE;
4005 		else {
4006 			power_state = SATA_POWER_ACTIVE;
4007 			sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4008 		}
4009 		break;
4010 	}
4011 
4012 	mutex_exit(cport_mutex);
4013 
4014 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4015 		/*
4016 		 * Because it is fully emulated command storing data
4017 		 * programatically in the specified buffer, release
4018 		 * preallocated DMA resources before storing data in the buffer,
4019 		 * so no unwanted DMA sync would take place.
4020 		 */
4021 		int count = MIN(bp->b_bcount,
4022 		    sizeof (struct scsi_extended_sense));
4023 		sata_scsi_dmafree(NULL, scsipkt);
4024 		bzero(&sense, sizeof (struct scsi_extended_sense));
4025 		sense.es_valid = 0;	/* Valid LBA */
4026 		sense.es_class = 7;	/* Response code 0x70 - current err */
4027 		sense.es_key = KEY_NO_SENSE;
4028 		sense.es_add_len = 6;	/* Additional length */
4029 		/* Copy no more than requested */
4030 		bcopy(&sense, bp->b_un.b_addr, count);
4031 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4032 		scsipkt->pkt_resid = 0;
4033 		switch (power_state) {
4034 		case SATA_POWER_IDLE:
4035 		case SATA_POWER_STANDBY:
4036 			sense.es_add_code =
4037 			    SD_SCSI_ASC_LOW_POWER_CONDITION_ON;
4038 			break;
4039 		case SATA_POWER_STOPPED:
4040 			sense.es_add_code = SD_SCSI_ASC_NO_ADD_SENSE;
4041 			break;
4042 		case SATA_POWER_ACTIVE:
4043 		default:
4044 			break;
4045 		}
4046 	}
4047 
4048 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4049 	    "Scsi_pkt completion reason %x\n",
4050 	    scsipkt->pkt_reason);
4051 
4052 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4053 	    scsipkt->pkt_comp != NULL) {
4054 		/* scsi callback required */
4055 		if (servicing_interrupt()) {
4056 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4057 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4058 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4059 				return (TRAN_BUSY);
4060 			}
4061 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4062 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4063 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4064 			/* Scheduling the callback failed */
4065 			return (TRAN_BUSY);
4066 		}
4067 	}
4068 	return (TRAN_ACCEPT);
4069 }
4070 
4071 /*
4072  * SATA translate command: Test Unit Ready
4073  * (ATA version for SATA hard disks).
4074  * It is translated into the Check Power Mode command.
4075  *
4076  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4077  */
4078 static int
4079 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx)
4080 {
4081 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4082 	struct scsi_extended_sense *sense;
4083 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4084 	sata_drive_info_t *sdinfo;
4085 	int power_state;
4086 	int rval, reason;
4087 	kmutex_t *cport_mutex =  &(SATA_TXLT_CPORT_MUTEX(spx));
4088 
4089 	mutex_enter(cport_mutex);
4090 
4091 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
4092 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4093 		mutex_exit(cport_mutex);
4094 		return (rval);
4095 	}
4096 
4097 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4098 	    &spx->txlt_sata_pkt->satapkt_device);
4099 	ASSERT(sdinfo != NULL);
4100 
4101 	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
4102 
4103 	/* send CHECK POWER MODE command */
4104 	sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
4105 	scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
4106 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4107 	if (sata_hba_start(spx, &rval) != 0) {
4108 		mutex_exit(cport_mutex);
4109 		return (rval);
4110 	}
4111 
4112 	if (scmd->satacmd_error_reg != 0) {
4113 		mutex_exit(cport_mutex);
4114 		return (sata_txlt_check_condition(spx, KEY_NOT_READY,
4115 		    SD_SCSI_ASC_LU_NOT_RESPONSE));
4116 	}
4117 
4118 	power_state = scmd->satacmd_sec_count_lsb;
4119 
4120 	/*
4121 	 * return NOT READY when device in STOPPED mode
4122 	 */
4123 	if (power_state == SATA_PWRMODE_STANDBY &&
4124 	    sdinfo->satadrv_power_level == SATA_POWER_STOPPED) {
4125 		*scsipkt->pkt_scbp = STATUS_CHECK;
4126 		sense = sata_arq_sense(spx);
4127 		sense->es_key = KEY_NOT_READY;
4128 		sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY;
4129 	} else {
4130 		/*
4131 		 * For other power mode, return GOOD status
4132 		 */
4133 		*scsipkt->pkt_scbp = STATUS_GOOD;
4134 	}
4135 
4136 	scsipkt->pkt_reason = CMD_CMPLT;
4137 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4138 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4139 
4140 	mutex_exit(cport_mutex);
4141 
4142 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4143 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4144 
4145 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4146 	    scsipkt->pkt_comp != NULL) {
4147 		/* scsi callback required */
4148 		if (servicing_interrupt()) {
4149 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4150 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4151 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4152 				return (TRAN_BUSY);
4153 			}
4154 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4155 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4156 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4157 			/* Scheduling the callback failed */
4158 			return (TRAN_BUSY);
4159 		}
4160 	}
4161 
4162 	return (TRAN_ACCEPT);
4163 }
4164 
4165 /*
4166  * SATA translate command: Start Stop Unit
4167  * Translation depends on a command:
4168  *
4169  * Power condition bits will be supported
4170  * and the power level should be maintained by SATL,
4171  * When SATL received a command, it will check the
4172  * power level firstly, and return the status according
4173  * to SAT2 v2.6 and SAT-2 Standby Modifications
4174  *
4175  * SPC-4/SBC-3      SATL    ATA power condition  SATL      SPC/SBC
4176  * -----------------------------------------------------------------------
4177  * SSU_PC1 Active   <==>     ATA  Active         <==>     SSU:start_bit =1
4178  * SSU_PC2 Idle     <==>     ATA  Idle           <==>     N/A
4179  * SSU_PC3 Standby  <==>     ATA  Standby        <==>     N/A
4180  * SSU_PC4 Stopped  <==>     ATA  Standby        <==>     SSU:start_bit = 0
4181  *
4182  *	Unload Media / NOT SUPPORTED YET
4183  *	Load Media / NOT SUPPROTED YET
4184  *	Immediate bit / NOT SUPPORTED YET (deferred error)
4185  *
4186  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4187  * appropriate values in scsi_pkt fields.
4188  */
4189 static int
4190 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx)
4191 {
4192 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4193 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4194 	int rval, reason;
4195 	sata_drive_info_t *sdinfo;
4196 	sata_id_t *sata_id;
4197 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4198 
4199 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4200 	    "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1);
4201 
4202 	mutex_enter(cport_mutex);
4203 
4204 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
4205 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4206 		mutex_exit(cport_mutex);
4207 		return (rval);
4208 	}
4209 
4210 	if (scsipkt->pkt_cdbp[1] & START_STOP_IMMED_MASK) {
4211 		/* IMMED bit - not supported */
4212 		mutex_exit(cport_mutex);
4213 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4214 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4215 	}
4216 
4217 	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
4218 	spx->txlt_sata_pkt->satapkt_comp = NULL;
4219 
4220 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4221 	    &spx->txlt_sata_pkt->satapkt_device);
4222 	ASSERT(sdinfo != NULL);
4223 	sata_id = &sdinfo->satadrv_id;
4224 
4225 	switch ((scsipkt->pkt_cdbp[4] & START_STOP_POWER_COND_MASK) >> 4) {
4226 	case 0:
4227 		if (scsipkt->pkt_cdbp[4] & START_STOP_LOEJ_MASK) {
4228 			/* Load/Unload Media - invalid request */
4229 			goto err_out;
4230 		}
4231 		if (scsipkt->pkt_cdbp[4] & START_STOP_START_MASK) {
4232 			/* Start Unit */
4233 			sata_build_read_verify_cmd(scmd, 1, 5);
4234 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4235 			/* Transfer command to HBA */
4236 			if (sata_hba_start(spx, &rval) != 0) {
4237 				/* Pkt not accepted for execution */
4238 				mutex_exit(cport_mutex);
4239 				return (rval);
4240 			}
4241 			if (scmd->satacmd_error_reg != 0) {
4242 				goto err_out;
4243 			}
4244 			sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4245 		} else {
4246 			/* Stop Unit */
4247 			sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4248 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4249 			if (sata_hba_start(spx, &rval) != 0) {
4250 				mutex_exit(cport_mutex);
4251 				return (rval);
4252 			} else {
4253 				if (scmd->satacmd_error_reg != 0) {
4254 					goto err_out;
4255 				}
4256 			}
4257 			/* ata standby immediate command */
4258 			sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
4259 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4260 			if (sata_hba_start(spx, &rval) != 0) {
4261 				mutex_exit(cport_mutex);
4262 				return (rval);
4263 			}
4264 			if (scmd->satacmd_error_reg != 0) {
4265 				goto err_out;
4266 			}
4267 			sdinfo->satadrv_power_level = SATA_POWER_STOPPED;
4268 		}
4269 		break;
4270 	case 0x1:
4271 		sata_build_generic_cmd(scmd, SATAC_IDLE);
4272 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4273 		if (sata_hba_start(spx, &rval) != 0) {
4274 			mutex_exit(cport_mutex);
4275 			return (rval);
4276 		}
4277 		if (scmd->satacmd_error_reg != 0) {
4278 			goto err_out;
4279 		}
4280 		sata_build_read_verify_cmd(scmd, 1, 5);
4281 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4282 		/* Transfer command to HBA */
4283 		if (sata_hba_start(spx, &rval) != 0) {
4284 			/* Pkt not accepted for execution */
4285 			mutex_exit(cport_mutex);
4286 			return (rval);
4287 		} else {
4288 			if (scmd->satacmd_error_reg != 0) {
4289 				goto err_out;
4290 			}
4291 		}
4292 		sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4293 		break;
4294 	case 0x2:
4295 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4296 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4297 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4298 			if (sata_hba_start(spx, &rval) != 0) {
4299 				mutex_exit(cport_mutex);
4300 				return (rval);
4301 			}
4302 			if (scmd->satacmd_error_reg != 0) {
4303 				goto err_out;
4304 			}
4305 		}
4306 		sata_build_generic_cmd(scmd, SATAC_IDLE);
4307 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4308 		if (sata_hba_start(spx, &rval) != 0) {
4309 			mutex_exit(cport_mutex);
4310 			return (rval);
4311 		}
4312 		if (scmd->satacmd_error_reg != 0) {
4313 			goto err_out;
4314 		}
4315 		if ((scsipkt->pkt_cdbp[3] & START_STOP_MODIFIER_MASK)) {
4316 			/*
4317 			 *  POWER CONDITION MODIFIER bit set
4318 			 *  to 0x1 or larger it will be handled
4319 			 *  on the same way as bit = 0x1
4320 			 */
4321 			if (!(sata_id->ai_cmdset84 &
4322 			    SATA_IDLE_UNLOAD_SUPPORTED)) {
4323 				sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4324 				break;
4325 			}
4326 			sata_build_generic_cmd(scmd, SATAC_IDLE_IM);
4327 			scmd->satacmd_features_reg = 0x44;
4328 			scmd->satacmd_lba_low_lsb = 0x4c;
4329 			scmd->satacmd_lba_mid_lsb = 0x4e;
4330 			scmd->satacmd_lba_high_lsb = 0x55;
4331 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4332 			if (sata_hba_start(spx, &rval) != 0) {
4333 				mutex_exit(cport_mutex);
4334 				return (rval);
4335 			}
4336 			if (scmd->satacmd_error_reg != 0) {
4337 				goto err_out;
4338 			}
4339 		}
4340 		sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4341 		break;
4342 	case 0x3:
4343 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4344 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4345 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4346 			if (sata_hba_start(spx, &rval) != 0) {
4347 				mutex_exit(cport_mutex);
4348 				return (rval);
4349 			}
4350 			if (scmd->satacmd_error_reg != 0) {
4351 				goto err_out;
4352 			}
4353 		}
4354 		sata_build_generic_cmd(scmd, SATAC_STANDBY);
4355 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4356 		if (sata_hba_start(spx, &rval) != 0) {
4357 			mutex_exit(cport_mutex);
4358 			return (rval);
4359 		}
4360 		if (scmd->satacmd_error_reg != 0) {
4361 			goto err_out;
4362 		}
4363 		sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
4364 		break;
4365 	case 0x7:
4366 		sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
4367 		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
4368 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4369 		if (sata_hba_start(spx, &rval) != 0) {
4370 			mutex_exit(cport_mutex);
4371 			return (rval);
4372 		}
4373 		if (scmd->satacmd_error_reg != 0) {
4374 			goto err_out;
4375 		}
4376 		switch (scmd->satacmd_sec_count_lsb) {
4377 		case SATA_PWRMODE_STANDBY:
4378 			sata_build_generic_cmd(scmd, SATAC_STANDBY);
4379 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4380 			    sdinfo->satadrv_standby_timer);
4381 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4382 			if (sata_hba_start(spx, &rval) != 0) {
4383 				mutex_exit(cport_mutex);
4384 				return (rval);
4385 			} else {
4386 				if (scmd->satacmd_error_reg != 0) {
4387 					goto err_out;
4388 				}
4389 			}
4390 			break;
4391 		case SATA_PWRMODE_IDLE:
4392 			sata_build_generic_cmd(scmd, SATAC_IDLE);
4393 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4394 			    sdinfo->satadrv_standby_timer);
4395 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4396 			if (sata_hba_start(spx, &rval) != 0) {
4397 				mutex_exit(cport_mutex);
4398 				return (rval);
4399 			} else {
4400 				if (scmd->satacmd_error_reg != 0) {
4401 					goto err_out;
4402 				}
4403 			}
4404 			break;
4405 		case SATA_PWRMODE_ACTIVE_SPINDOWN:
4406 		case SATA_PWRMODE_ACTIVE_SPINUP:
4407 		case SATA_PWRMODE_ACTIVE:
4408 			sata_build_generic_cmd(scmd, SATAC_IDLE);
4409 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4410 			    sdinfo->satadrv_standby_timer);
4411 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4412 			if (sata_hba_start(spx, &rval) != 0) {
4413 				mutex_exit(cport_mutex);
4414 				return (rval);
4415 			}
4416 			if (scmd->satacmd_error_reg != 0) {
4417 				goto err_out;
4418 			}
4419 			sata_build_read_verify_cmd(scmd, 1, 5);
4420 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4421 			if (sata_hba_start(spx, &rval) != 0) {
4422 				mutex_exit(cport_mutex);
4423 				return (rval);
4424 			}
4425 			if (scmd->satacmd_error_reg != 0) {
4426 				goto err_out;
4427 			}
4428 			break;
4429 		default:
4430 			goto err_out;
4431 		}
4432 		break;
4433 	case 0xb:
4434 		if ((sata_get_standby_timer(sdinfo->satadrv_standby_timer) ==
4435 		    0) || (!(sata_id->ai_cap & SATA_STANDBYTIMER))) {
4436 			mutex_exit(cport_mutex);
4437 			return (sata_txlt_check_condition(spx,
4438 			    KEY_ILLEGAL_REQUEST,
4439 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4440 		}
4441 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4442 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4443 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4444 			if (sata_hba_start(spx, &rval) != 0) {
4445 				mutex_exit(cport_mutex);
4446 				return (rval);
4447 			}
4448 			if (scmd->satacmd_error_reg != 0) {
4449 				goto err_out;
4450 			}
4451 			sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
4452 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4453 			if (sata_hba_start(spx, &rval) != 0) {
4454 				mutex_exit(cport_mutex);
4455 				return (rval);
4456 			}
4457 			if (scmd->satacmd_error_reg != 0) {
4458 				goto err_out;
4459 			}
4460 		}
4461 		bzero(sdinfo->satadrv_standby_timer, sizeof (uchar_t) * 4);
4462 		break;
4463 	default:
4464 err_out:
4465 		mutex_exit(cport_mutex);
4466 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4467 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4468 	}
4469 
4470 	/*
4471 	 * Since it was a synchronous command,
4472 	 * a callback function will be called directly.
4473 	 */
4474 	mutex_exit(cport_mutex);
4475 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4476 	    "synchronous execution status %x\n",
4477 	    spx->txlt_sata_pkt->satapkt_reason);
4478 
4479 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4480 	    scsipkt->pkt_comp != NULL) {
4481 		sata_set_arq_data(spx->txlt_sata_pkt);
4482 		if (servicing_interrupt()) {
4483 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4484 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4485 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4486 				return (TRAN_BUSY);
4487 			}
4488 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4489 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4490 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4491 			/* Scheduling the callback failed */
4492 			return (TRAN_BUSY);
4493 		}
4494 	}
4495 	else
4496 
4497 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
4498 
4499 	return (TRAN_ACCEPT);
4500 
4501 }
4502 
4503 /*
4504  * SATA translate command:  Read Capacity.
4505  * Emulated command for SATA disks.
4506  * Capacity is retrieved from cached Idenifty Device data.
4507  * Identify Device data shows effective disk capacity, not the native
4508  * capacity, which may be limitted by Set Max Address command.
4509  * This is ATA version for SATA hard disks.
4510  *
4511  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4512  */
4513 static int
4514 sata_txlt_read_capacity(sata_pkt_txlate_t *spx)
4515 {
4516 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4517 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4518 	sata_drive_info_t *sdinfo;
4519 	uint64_t val;
4520 	uint32_t lbsize = DEV_BSIZE;
4521 	uchar_t *rbuf;
4522 	int rval, reason;
4523 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4524 
4525 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4526 	    "sata_txlt_read_capacity: ", NULL);
4527 
4528 	mutex_enter(cport_mutex);
4529 
4530 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4531 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4532 		mutex_exit(cport_mutex);
4533 		return (rval);
4534 	}
4535 
4536 	scsipkt->pkt_reason = CMD_CMPLT;
4537 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4538 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4539 	*scsipkt->pkt_scbp = STATUS_GOOD;
4540 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4541 		/*
4542 		 * Because it is fully emulated command storing data
4543 		 * programatically in the specified buffer, release
4544 		 * preallocated DMA resources before storing data in the buffer,
4545 		 * so no unwanted DMA sync would take place.
4546 		 */
4547 		sata_scsi_dmafree(NULL, scsipkt);
4548 
4549 		sdinfo = sata_get_device_info(
4550 		    spx->txlt_sata_hba_inst,
4551 		    &spx->txlt_sata_pkt->satapkt_device);
4552 
4553 		/*
4554 		 * As per SBC-3, the "returned LBA" is either the highest
4555 		 * addressable LBA or 0xffffffff, whichever is smaller.
4556 		 */
4557 		val = MIN(sdinfo->satadrv_capacity - 1, UINT32_MAX);
4558 
4559 		if (sdinfo->satadrv_id.ai_phys_sect_sz & SATA_L2PS_CHECK_BIT) {
4560 			/* physical/logical sector size word is valid */
4561 
4562 			if (sdinfo->satadrv_id.ai_phys_sect_sz &
4563 			    SATA_L2PS_BIG_SECTORS) {
4564 				/* if this set 117-118 words are valid */
4565 				lbsize = sdinfo->satadrv_id.ai_words_lsec[0] |
4566 				    (sdinfo->satadrv_id.ai_words_lsec[1] << 16);
4567 				lbsize <<= 1; /* convert from words to bytes */
4568 			}
4569 		}
4570 		rbuf = (uchar_t *)bp->b_un.b_addr;
4571 		/* Need to swap endians to match scsi format */
4572 		rbuf[0] = (val >> 24) & 0xff;
4573 		rbuf[1] = (val >> 16) & 0xff;
4574 		rbuf[2] = (val >> 8) & 0xff;
4575 		rbuf[3] = val & 0xff;
4576 		rbuf[4] = (lbsize >> 24) & 0xff;
4577 		rbuf[5] = (lbsize >> 16) & 0xff;
4578 		rbuf[6] = (lbsize >> 8) & 0xff;
4579 		rbuf[7] = lbsize & 0xff;
4580 
4581 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4582 		scsipkt->pkt_resid = 0;
4583 
4584 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n",
4585 		    sdinfo->satadrv_capacity -1);
4586 	}
4587 	mutex_exit(cport_mutex);
4588 	/*
4589 	 * If a callback was requested, do it now.
4590 	 */
4591 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4592 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4593 
4594 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4595 	    scsipkt->pkt_comp != NULL) {
4596 		/* scsi callback required */
4597 		if (servicing_interrupt()) {
4598 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4599 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4600 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4601 				return (TRAN_BUSY);
4602 			}
4603 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4604 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4605 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4606 			/* Scheduling the callback failed */
4607 			return (TRAN_BUSY);
4608 		}
4609 	}
4610 
4611 	return (TRAN_ACCEPT);
4612 }
4613 
4614 /*
4615  * SATA translate command:  Read Capacity (16).
4616  * Emulated command for SATA disks.
4617  * Info is retrieved from cached Identify Device data.
4618  * Implemented to SBC-3 (draft 21) and SAT-2 (final) specifications.
4619  *
4620  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4621  */
4622 static int
4623 sata_txlt_read_capacity16(sata_pkt_txlate_t *spx)
4624 {
4625 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4626 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4627 	sata_drive_info_t *sdinfo;
4628 	uint64_t val;
4629 	uint16_t l2p_exp;
4630 	uint32_t lbsize = DEV_BSIZE;
4631 	uchar_t *rbuf;
4632 	int rval, reason;
4633 #define	TPE	0x80
4634 #define	TPRZ	0x40
4635 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4636 
4637 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4638 	    "sata_txlt_read_capacity: ", NULL);
4639 
4640 	mutex_enter(cport_mutex);
4641 
4642 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4643 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4644 		mutex_exit(cport_mutex);
4645 		return (rval);
4646 	}
4647 
4648 	scsipkt->pkt_reason = CMD_CMPLT;
4649 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4650 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4651 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4652 		/*
4653 		 * Because it is fully emulated command storing data
4654 		 * programatically in the specified buffer, release
4655 		 * preallocated DMA resources before storing data in the buffer,
4656 		 * so no unwanted DMA sync would take place.
4657 		 */
4658 		sata_scsi_dmafree(NULL, scsipkt);
4659 
4660 		/* Check SERVICE ACTION field */
4661 		if ((scsipkt->pkt_cdbp[1] & 0x1f) !=
4662 		    SSVC_ACTION_READ_CAPACITY_G4) {
4663 			mutex_exit(cport_mutex);
4664 			return (sata_txlt_check_condition(spx,
4665 			    KEY_ILLEGAL_REQUEST,
4666 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4667 		}
4668 
4669 		/* Check LBA field */
4670 		if ((scsipkt->pkt_cdbp[2] != 0) ||
4671 		    (scsipkt->pkt_cdbp[3] != 0) ||
4672 		    (scsipkt->pkt_cdbp[4] != 0) ||
4673 		    (scsipkt->pkt_cdbp[5] != 0) ||
4674 		    (scsipkt->pkt_cdbp[6] != 0) ||
4675 		    (scsipkt->pkt_cdbp[7] != 0) ||
4676 		    (scsipkt->pkt_cdbp[8] != 0) ||
4677 		    (scsipkt->pkt_cdbp[9] != 0)) {
4678 			mutex_exit(cport_mutex);
4679 			return (sata_txlt_check_condition(spx,
4680 			    KEY_ILLEGAL_REQUEST,
4681 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4682 		}
4683 
4684 		/* Check PMI bit */
4685 		if (scsipkt->pkt_cdbp[14] & 0x1) {
4686 			mutex_exit(cport_mutex);
4687 			return (sata_txlt_check_condition(spx,
4688 			    KEY_ILLEGAL_REQUEST,
4689 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4690 		}
4691 
4692 		*scsipkt->pkt_scbp = STATUS_GOOD;
4693 
4694 		sdinfo = sata_get_device_info(
4695 		    spx->txlt_sata_hba_inst,
4696 		    &spx->txlt_sata_pkt->satapkt_device);
4697 
4698 		/* last logical block address */
4699 		val = MIN(sdinfo->satadrv_capacity - 1,
4700 		    SCSI_READ_CAPACITY16_MAX_LBA);
4701 
4702 		/* logical to physical block size exponent */
4703 		l2p_exp = 0;
4704 		if (sdinfo->satadrv_id.ai_phys_sect_sz & SATA_L2PS_CHECK_BIT) {
4705 			/* physical/logical sector size word is valid */
4706 
4707 			if (sdinfo->satadrv_id.ai_phys_sect_sz &
4708 			    SATA_L2PS_HAS_MULT) {
4709 				/* multiple logical sectors per phys sectors */
4710 				l2p_exp =
4711 				    sdinfo->satadrv_id.ai_phys_sect_sz &
4712 				    SATA_L2PS_EXP_MASK;
4713 			}
4714 
4715 			if (sdinfo->satadrv_id.ai_phys_sect_sz &
4716 			    SATA_L2PS_BIG_SECTORS) {
4717 				/* if this set 117-118 words are valid */
4718 				lbsize = sdinfo->satadrv_id.ai_words_lsec[0] |
4719 				    (sdinfo->satadrv_id.ai_words_lsec[1] << 16);
4720 				lbsize <<= 1; /* convert from words to bytes */
4721 			}
4722 		}
4723 
4724 		rbuf = (uchar_t *)bp->b_un.b_addr;
4725 		bzero(rbuf, bp->b_bcount);
4726 
4727 		/* returned logical block address */
4728 		rbuf[0] = (val >> 56) & 0xff;
4729 		rbuf[1] = (val >> 48) & 0xff;
4730 		rbuf[2] = (val >> 40) & 0xff;
4731 		rbuf[3] = (val >> 32) & 0xff;
4732 		rbuf[4] = (val >> 24) & 0xff;
4733 		rbuf[5] = (val >> 16) & 0xff;
4734 		rbuf[6] = (val >> 8) & 0xff;
4735 		rbuf[7] = val & 0xff;
4736 		rbuf[8] = (lbsize >> 24) & 0xff;
4737 		rbuf[9] = (lbsize >> 16) & 0xff;
4738 		rbuf[10] = (lbsize >> 8) & 0xff;
4739 		rbuf[11] = lbsize & 0xff;
4740 
4741 		/* p_type, prot_en, unspecified by SAT-2 */
4742 		/* rbuf[12] = 0; */
4743 
4744 		/* p_i_exponent, undefined by SAT-2 */
4745 		/* logical blocks per physical block exponent */
4746 		rbuf[13] = l2p_exp;
4747 
4748 		/* lowest aligned logical block address = 0 (for now) */
4749 		/* tpe and tprz as defined in T10/10-079 r0 */
4750 		if (sdinfo->satadrv_id.ai_addsupported &
4751 		    SATA_DETERMINISTIC_READ) {
4752 			if (sdinfo->satadrv_id.ai_addsupported &
4753 			    SATA_READ_ZERO) {
4754 				rbuf[14] |= TPRZ;
4755 			} else {
4756 				rbuf[14] |= TPE;
4757 			}
4758 		}
4759 		/* rbuf[15] = 0; */
4760 
4761 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4762 		scsipkt->pkt_resid = 0;
4763 
4764 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%llu\n",
4765 		    sdinfo->satadrv_capacity -1);
4766 	}
4767 
4768 	mutex_exit(cport_mutex);
4769 
4770 	/*
4771 	 * If a callback was requested, do it now.
4772 	 */
4773 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4774 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4775 
4776 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4777 	    scsipkt->pkt_comp != NULL) {
4778 		/* scsi callback required */
4779 		if (servicing_interrupt()) {
4780 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4781 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4782 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
4783 				return (TRAN_BUSY);
4784 			}
4785 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4786 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4787 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
4788 			/* Scheduling the callback failed */
4789 			return (TRAN_BUSY);
4790 		}
4791 	}
4792 
4793 	return (TRAN_ACCEPT);
4794 }
4795 
4796 /*
4797  * Translate command: UNMAP
4798  *
4799  * The function cannot be called in interrupt context since it may sleep.
4800  */
4801 static int
4802 sata_txlt_unmap(sata_pkt_txlate_t *spx)
4803 {
4804 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4805 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4806 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4807 	uint16_t count = 0;
4808 	int synch;
4809 	int rval, reason;
4810 	int i, x;
4811 	int bdlen = 0;
4812 	int ranges = 0;
4813 	int paramlen = 8;
4814 	uint8_t *data, *tmpbd;
4815 	sata_drive_info_t *sdinfo;
4816 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4817 #define	TRIM	0x1
4818 
4819 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4820 	    "sata_txlt_unmap: ", NULL);
4821 
4822 	mutex_enter(cport_mutex);
4823 
4824 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4825 	    &spx->txlt_sata_pkt->satapkt_device);
4826 	if (sdinfo != NULL) {
4827 		SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4828 		    "DSM support 0x%x, max number of 512 byte blocks of LBA "
4829 		    "range entries 0x%x\n", sdinfo->satadrv_id.ai_dsm,
4830 		    sdinfo->satadrv_id.ai_maxcount);
4831 	}
4832 
4833 	rval = sata_txlt_generic_pkt_info(spx, &reason, 1);
4834 	if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4835 		mutex_exit(cport_mutex);
4836 		return (rval);
4837 	}
4838 
4839 	/*
4840 	 * Need to modify bp to have TRIM data instead of UNMAP data.
4841 	 * Start by getting the block descriptor data length by subtracting
4842 	 * the 8 byte parameter list header from the parameter list length.
4843 	 * The block descriptor size has to be a multiple of 16 bytes.
4844 	 */
4845 	bdlen = scsipkt->pkt_cdbp[7];
4846 	bdlen = (bdlen << 8) + scsipkt->pkt_cdbp[8] - paramlen;
4847 	if ((bdlen < 0) || ((bdlen % 16) != 0) ||
4848 	    (bdlen > (bp->b_bcount - paramlen))) {
4849 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4850 		    "sata_txlt_unmap: invalid block descriptor length", NULL);
4851 		mutex_exit(cport_mutex);
4852 		return ((sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4853 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB)));
4854 	}
4855 	/*
4856 	 * If there are no parameter data or block descriptors, it is not
4857 	 * considered an error so just complete the command without sending
4858 	 * TRIM.
4859 	 */
4860 	if ((bdlen == 0) || (bp == NULL) || (bp->b_un.b_addr == NULL) ||
4861 	    (bp->b_bcount == 0)) {
4862 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4863 		    "sata_txlt_unmap: no parameter data or block descriptors",
4864 		    NULL);
4865 		mutex_exit(cport_mutex);
4866 		return (sata_txlt_unmap_nodata_cmd(spx));
4867 	}
4868 	tmpbd = (uint8_t *)bp->b_un.b_addr + paramlen;
4869 	data = kmem_zalloc(bdlen, KM_SLEEP);
4870 
4871 	/*
4872 	 * Loop through all the UNMAP block descriptors and convert the data
4873 	 * into TRIM format.
4874 	 */
4875 	for (i = 0, x = 0; i < bdlen; i += 16, x += 8) {
4876 		/* get range length */
4877 		data[x] = tmpbd[i+7];
4878 		data[x+1] = tmpbd[i+6];
4879 		/* get LBA */
4880 		data[x+2] = tmpbd[i+5];
4881 		data[x+3] = tmpbd[i+4];
4882 		data[x+4] = tmpbd[i+3];
4883 		data[x+5] = tmpbd[i+2];
4884 		data[x+6] = tmpbd[i+11];
4885 		data[x+7] = tmpbd[i+10];
4886 
4887 		ranges++;
4888 	}
4889 
4890 	/*
4891 	 * The TRIM command expects the data buffer to be a multiple of
4892 	 * 512-byte blocks of range entries.  This means that the UNMAP buffer
4893 	 * may be too small.  Free the original DMA resources and create a
4894 	 * local buffer.
4895 	 */
4896 	sata_common_free_dma_rsrcs(spx);
4897 
4898 	/*
4899 	 * Get count of 512-byte blocks of range entries.  The length
4900 	 * of a range entry is 8 bytes which means one count has 64 range
4901 	 * entries.
4902 	 */
4903 	count = (ranges + 63)/64;
4904 
4905 	/* Allocate a buffer that is a multiple of 512 bytes. */
4906 	mutex_exit(cport_mutex);
4907 	bp = sata_alloc_local_buffer(spx, count * 512);
4908 	if (bp == NULL) {
4909 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
4910 		    "sata_txlt_unmap: "
4911 		    "cannot allocate buffer for TRIM command", NULL);
4912 		kmem_free(data, bdlen);
4913 		return (TRAN_BUSY);
4914 	}
4915 	bp_mapin(bp); /* make data buffer accessible */
4916 	mutex_enter(cport_mutex);
4917 
4918 	bzero(bp->b_un.b_addr, bp->b_bcount);
4919 	bcopy(data, bp->b_un.b_addr, x);
4920 	kmem_free(data, bdlen);
4921 	rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
4922 	    DDI_DMA_SYNC_FORDEV);
4923 	ASSERT(rval == DDI_SUCCESS);
4924 
4925 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4926 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4927 	scmd->satacmd_cmd_reg = SATAC_DSM;
4928 	scmd->satacmd_sec_count_msb = (count >> 8) & 0xff;
4929 	scmd->satacmd_sec_count_lsb = count & 0xff;
4930 	scmd->satacmd_features_reg = TRIM;
4931 	scmd->satacmd_device_reg = SATA_ADH_LBA;
4932 	scmd->satacmd_status_reg = 0;
4933 	scmd->satacmd_error_reg = 0;
4934 
4935 	/* Start processing command */
4936 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4937 		spx->txlt_sata_pkt->satapkt_comp =
4938 		    sata_txlt_unmap_completion;
4939 		synch = FALSE;
4940 	} else {
4941 		synch = TRUE;
4942 	}
4943 
4944 	if (sata_hba_start(spx, &rval) != 0) {
4945 		mutex_exit(cport_mutex);
4946 		return (rval);
4947 	}
4948 
4949 	mutex_exit(cport_mutex);
4950 
4951 	if (synch) {
4952 		sata_txlt_unmap_completion(spx->txlt_sata_pkt);
4953 	}
4954 
4955 	return (TRAN_ACCEPT);
4956 }
4957 
4958 /*
4959  * SATA translate command: Mode Sense.
4960  * Translated into appropriate SATA command or emulated.
4961  * Saved Values Page Control (03) are not supported.
4962  *
4963  * NOTE: only caching mode sense page is currently implemented.
4964  *
4965  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4966  */
4967 
4968 #define	LLBAA	0x10	/* Long LBA Accepted */
4969 
4970 static int
4971 sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
4972 {
4973 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4974 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4975 	sata_drive_info_t *sdinfo;
4976 	sata_id_t *sata_id;
4977 	struct scsi_extended_sense *sense;
4978 	int 		len, bdlen, count, alc_len;
4979 	int		pc;	/* Page Control code */
4980 	uint8_t		*buf;	/* mode sense buffer */
4981 	int		rval, reason;
4982 	kmutex_t	*cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4983 
4984 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4985 	    "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
4986 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4987 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4988 
4989 	if (servicing_interrupt()) {
4990 		buf = kmem_zalloc(1024, KM_NOSLEEP);
4991 		if (buf == NULL) {
4992 			return (TRAN_BUSY);
4993 		}
4994 	} else {
4995 		buf = kmem_zalloc(1024, KM_SLEEP);
4996 	}
4997 
4998 	mutex_enter(cport_mutex);
4999 
5000 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
5001 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5002 		mutex_exit(cport_mutex);
5003 		kmem_free(buf, 1024);
5004 		return (rval);
5005 	}
5006 
5007 	scsipkt->pkt_reason = CMD_CMPLT;
5008 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5009 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5010 
5011 	pc = scsipkt->pkt_cdbp[2] >> 6;
5012 
5013 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
5014 		/*
5015 		 * Because it is fully emulated command storing data
5016 		 * programatically in the specified buffer, release
5017 		 * preallocated DMA resources before storing data in the buffer,
5018 		 * so no unwanted DMA sync would take place.
5019 		 */
5020 		sata_scsi_dmafree(NULL, scsipkt);
5021 
5022 		len = 0;
5023 		bdlen = 0;
5024 		if (!(scsipkt->pkt_cdbp[1] & 8)) {
5025 			if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
5026 			    (scsipkt->pkt_cdbp[1] & LLBAA))
5027 				bdlen = 16;
5028 			else
5029 				bdlen = 8;
5030 		}
5031 		/* Build mode parameter header */
5032 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5033 			/* 4-byte mode parameter header */
5034 			buf[len++] = 0;		/* mode data length */
5035 			buf[len++] = 0;		/* medium type */
5036 			buf[len++] = 0;		/* dev-specific param */
5037 			buf[len++] = bdlen;	/* Block Descriptor length */
5038 		} else {
5039 			/* 8-byte mode parameter header */
5040 			buf[len++] = 0;		/* mode data length */
5041 			buf[len++] = 0;
5042 			buf[len++] = 0;		/* medium type */
5043 			buf[len++] = 0;		/* dev-specific param */
5044 			if (bdlen == 16)
5045 				buf[len++] = 1;	/* long lba descriptor */
5046 			else
5047 				buf[len++] = 0;
5048 			buf[len++] = 0;
5049 			buf[len++] = 0;		/* Block Descriptor length */
5050 			buf[len++] = bdlen;
5051 		}
5052 
5053 		sdinfo = sata_get_device_info(
5054 		    spx->txlt_sata_hba_inst,
5055 		    &spx->txlt_sata_pkt->satapkt_device);
5056 
5057 		/* Build block descriptor only if not disabled (DBD) */
5058 		if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
5059 			/* Block descriptor - direct-access device format */
5060 			if (bdlen == 8) {
5061 				/* build regular block descriptor */
5062 				buf[len++] =
5063 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
5064 				buf[len++] =
5065 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
5066 				buf[len++] =
5067 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
5068 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
5069 				buf[len++] = 0; /* density code */
5070 				buf[len++] = 0;
5071 				if (sdinfo->satadrv_type ==
5072 				    SATA_DTYPE_ATADISK)
5073 					buf[len++] = 2;
5074 				else
5075 					/* ATAPI */
5076 					buf[len++] = 8;
5077 				buf[len++] = 0;
5078 			} else if (bdlen == 16) {
5079 				/* Long LBA Accepted */
5080 				/* build long lba block descriptor */
5081 #ifndef __lock_lint
5082 				buf[len++] =
5083 				    (sdinfo->satadrv_capacity >> 56) & 0xff;
5084 				buf[len++] =
5085 				    (sdinfo->satadrv_capacity >> 48) & 0xff;
5086 				buf[len++] =
5087 				    (sdinfo->satadrv_capacity >> 40) & 0xff;
5088 				buf[len++] =
5089 				    (sdinfo->satadrv_capacity >> 32) & 0xff;
5090 #endif
5091 				buf[len++] =
5092 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
5093 				buf[len++] =
5094 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
5095 				buf[len++] =
5096 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
5097 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
5098 				buf[len++] = 0;
5099 				buf[len++] = 0; /* density code */
5100 				buf[len++] = 0;
5101 				buf[len++] = 0;
5102 				if (sdinfo->satadrv_type ==
5103 				    SATA_DTYPE_ATADISK)
5104 					buf[len++] = 2;
5105 				else
5106 					/* ATAPI */
5107 					buf[len++] = 8;
5108 				buf[len++] = 0;
5109 			}
5110 		}
5111 
5112 		sata_id = &sdinfo->satadrv_id;
5113 
5114 		/*
5115 		 * Add requested pages.
5116 		 * Page 3 and 4 are obsolete and we are not supporting them.
5117 		 * We deal now with:
5118 		 * caching (read/write cache control).
5119 		 * We should eventually deal with following mode pages:
5120 		 * error recovery  (0x01),
5121 		 * power condition (0x1a),
5122 		 * exception control page (enables SMART) (0x1c),
5123 		 * enclosure management (ses),
5124 		 * protocol-specific port mode (port control).
5125 		 */
5126 		switch (scsipkt->pkt_cdbp[2] & 0x3f) {
5127 		case MODEPAGE_RW_ERRRECOV:
5128 			/* DAD_MODE_ERR_RECOV */
5129 			/* R/W recovery */
5130 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
5131 			break;
5132 		case MODEPAGE_CACHING:
5133 			/* DAD_MODE_CACHE */
5134 			/* Reject not supported request for saved parameters */
5135 			if (pc == 3) {
5136 				*scsipkt->pkt_scbp = STATUS_CHECK;
5137 				sense = sata_arq_sense(spx);
5138 				sense->es_key = KEY_ILLEGAL_REQUEST;
5139 				sense->es_add_code =
5140 				    SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
5141 				goto done;
5142 			}
5143 
5144 			/* caching */
5145 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
5146 			break;
5147 		case MODEPAGE_INFO_EXCPT:
5148 			/* exception cntrl */
5149 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
5150 				len += sata_build_msense_page_1c(sdinfo, pc,
5151 				    buf+len);
5152 			}
5153 			else
5154 				goto err;
5155 			break;
5156 		case MODEPAGE_POWER_COND:
5157 			/* DAD_MODE_POWER_COND */
5158 			/* power condition */
5159 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
5160 			break;
5161 
5162 		case MODEPAGE_ACOUSTIC_MANAG:
5163 			/* acoustic management */
5164 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
5165 			break;
5166 		case MODEPAGE_ALLPAGES:
5167 			/* all pages */
5168 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
5169 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
5170 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
5171 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
5172 				len += sata_build_msense_page_1c(sdinfo, pc,
5173 				    buf+len);
5174 			}
5175 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
5176 			break;
5177 		default:
5178 		err:
5179 			/* Invalid request */
5180 			*scsipkt->pkt_scbp = STATUS_CHECK;
5181 			sense = sata_arq_sense(spx);
5182 			sense->es_key = KEY_ILLEGAL_REQUEST;
5183 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5184 			goto done;
5185 		}
5186 
5187 		/* fix total mode data length */
5188 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5189 			/* 4-byte mode parameter header */
5190 			buf[0] = len - 1;	/* mode data length */
5191 		} else {
5192 			buf[0] = (len -2) >> 8;
5193 			buf[1] = (len -2) & 0xff;
5194 		}
5195 
5196 
5197 		/* Check allocation length */
5198 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5199 			alc_len = scsipkt->pkt_cdbp[4];
5200 		} else {
5201 			alc_len = scsipkt->pkt_cdbp[7];
5202 			alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
5203 		}
5204 		/*
5205 		 * We do not check for possible parameters truncation
5206 		 * (alc_len < len) assuming that the target driver works
5207 		 * correctly. Just avoiding overrun.
5208 		 * Copy no more than requested and possible, buffer-wise.
5209 		 */
5210 		count = MIN(alc_len, len);
5211 		count = MIN(bp->b_bcount, count);
5212 		bcopy(buf, bp->b_un.b_addr, count);
5213 
5214 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
5215 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
5216 	}
5217 	*scsipkt->pkt_scbp = STATUS_GOOD;
5218 done:
5219 	mutex_exit(cport_mutex);
5220 	(void) kmem_free(buf, 1024);
5221 
5222 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5223 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5224 
5225 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5226 	    scsipkt->pkt_comp != NULL) {
5227 		/* scsi callback required */
5228 		if (servicing_interrupt()) {
5229 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5230 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5231 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
5232 				return (TRAN_BUSY);
5233 			}
5234 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5235 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5236 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
5237 			/* Scheduling the callback failed */
5238 			return (TRAN_BUSY);
5239 		}
5240 	}
5241 
5242 	return (TRAN_ACCEPT);
5243 }
5244 
5245 
5246 /*
5247  * SATA translate command: Mode Select.
5248  * Translated into appropriate SATA command or emulated.
5249  * Saving parameters is not supported.
5250  * Changing device capacity is not supported (although theoretically
5251  * possible by executing SET FEATURES/SET MAX ADDRESS)
5252  *
5253  * Assumption is that the target driver is working correctly.
5254  *
5255  * More than one SATA command may be executed to perform operations specified
5256  * by mode select pages. The first error terminates further execution.
5257  * Operations performed successully are not backed-up in such case.
5258  *
5259  * NOTE: Implemented pages:
5260  * - caching page
5261  * - informational exception page
5262  * - acoustic management page
5263  * - power condition page
5264  * Caching setup is remembered so it could be re-stored in case of
5265  * an unexpected device reset.
5266  *
5267  * Returns TRAN_XXXX.
5268  * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields.
5269  */
5270 
5271 static int
5272 sata_txlt_mode_select(sata_pkt_txlate_t *spx)
5273 {
5274 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5275 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5276 	struct scsi_extended_sense *sense;
5277 	int len, pagelen, count, pllen;
5278 	uint8_t *buf;	/* mode select buffer */
5279 	int rval, stat, reason;
5280 	uint_t nointr_flag;
5281 	int dmod = 0;
5282 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5283 
5284 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5285 	    "sata_txlt_mode_select, pc %x page code 0x%02x\n",
5286 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5287 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5288 
5289 	mutex_enter(cport_mutex);
5290 
5291 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
5292 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5293 		mutex_exit(cport_mutex);
5294 		return (rval);
5295 	}
5296 
5297 	rval = TRAN_ACCEPT;
5298 
5299 	scsipkt->pkt_reason = CMD_CMPLT;
5300 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5301 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5302 	nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
5303 
5304 	/* Reject not supported request */
5305 	if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
5306 		*scsipkt->pkt_scbp = STATUS_CHECK;
5307 		sense = sata_arq_sense(spx);
5308 		sense->es_key = KEY_ILLEGAL_REQUEST;
5309 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5310 		goto done;
5311 	}
5312 
5313 	if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
5314 		pllen = scsipkt->pkt_cdbp[4];
5315 	} else {
5316 		pllen = scsipkt->pkt_cdbp[7];
5317 		pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
5318 	}
5319 
5320 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
5321 
5322 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
5323 		buf = (uint8_t *)bp->b_un.b_addr;
5324 		count = MIN(bp->b_bcount, pllen);
5325 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
5326 		scsipkt->pkt_resid = 0;
5327 		pllen = count;
5328 
5329 		/*
5330 		 * Check the header to skip the block descriptor(s) - we
5331 		 * do not support setting device capacity.
5332 		 * Existing macros do not recognize long LBA dscriptor,
5333 		 * hence manual calculation.
5334 		 */
5335 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
5336 			/* 6-bytes CMD, 4 bytes header */
5337 			if (count <= 4)
5338 				goto done;		/* header only */
5339 			len = buf[3] + 4;
5340 		} else {
5341 			/* 10-bytes CMD, 8 bytes header */
5342 			if (count <= 8)
5343 				goto done;		/* header only */
5344 			len = buf[6];
5345 			len = (len << 8) + buf[7] + 8;
5346 		}
5347 		if (len >= count)
5348 			goto done;	/* header + descriptor(s) only */
5349 
5350 		pllen -= len;		/* remaining data length */
5351 
5352 		/*
5353 		 * We may be executing SATA command and want to execute it
5354 		 * in SYNCH mode, regardless of scsi_pkt setting.
5355 		 * Save scsi_pkt setting and indicate SYNCH mode
5356 		 */
5357 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5358 		    scsipkt->pkt_comp != NULL) {
5359 			scsipkt->pkt_flags |= FLAG_NOINTR;
5360 		}
5361 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
5362 
5363 		/*
5364 		 * len is now the offset to a first mode select page
5365 		 * Process all pages
5366 		 */
5367 		while (pllen > 0) {
5368 			switch ((int)buf[len]) {
5369 			case MODEPAGE_CACHING:
5370 				/* No support for SP (saving) */
5371 				if (scsipkt->pkt_cdbp[1] & 0x01) {
5372 					*scsipkt->pkt_scbp = STATUS_CHECK;
5373 					sense = sata_arq_sense(spx);
5374 					sense->es_key = KEY_ILLEGAL_REQUEST;
5375 					sense->es_add_code =
5376 					    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5377 					goto done;
5378 				}
5379 				stat = sata_mode_select_page_8(spx,
5380 				    (struct mode_cache_scsi3 *)&buf[len],
5381 				    pllen, &pagelen, &rval, &dmod);
5382 				/*
5383 				 * The pagelen value indicates the number of
5384 				 * parameter bytes already processed.
5385 				 * The rval is the return value from
5386 				 * sata_tran_start().
5387 				 * The stat indicates the overall status of
5388 				 * the operation(s).
5389 				 */
5390 				if (stat != SATA_SUCCESS)
5391 					/*
5392 					 * Page processing did not succeed -
5393 					 * all error info is already set-up,
5394 					 * just return
5395 					 */
5396 					pllen = 0; /* this breaks the loop */
5397 				else {
5398 					len += pagelen;
5399 					pllen -= pagelen;
5400 				}
5401 				break;
5402 
5403 			case MODEPAGE_INFO_EXCPT:
5404 				stat = sata_mode_select_page_1c(spx,
5405 				    (struct mode_info_excpt_page *)&buf[len],
5406 				    pllen, &pagelen, &rval, &dmod);
5407 				/*
5408 				 * The pagelen value indicates the number of
5409 				 * parameter bytes already processed.
5410 				 * The rval is the return value from
5411 				 * sata_tran_start().
5412 				 * The stat indicates the overall status of
5413 				 * the operation(s).
5414 				 */
5415 				if (stat != SATA_SUCCESS)
5416 					/*
5417 					 * Page processing did not succeed -
5418 					 * all error info is already set-up,
5419 					 * just return
5420 					 */
5421 					pllen = 0; /* this breaks the loop */
5422 				else {
5423 					len += pagelen;
5424 					pllen -= pagelen;
5425 				}
5426 				break;
5427 
5428 			case MODEPAGE_ACOUSTIC_MANAG:
5429 				stat = sata_mode_select_page_30(spx,
5430 				    (struct mode_acoustic_management *)
5431 				    &buf[len], pllen, &pagelen, &rval, &dmod);
5432 				/*
5433 				 * The pagelen value indicates the number of
5434 				 * parameter bytes already processed.
5435 				 * The rval is the return value from
5436 				 * sata_tran_start().
5437 				 * The stat indicates the overall status of
5438 				 * the operation(s).
5439 				 */
5440 				if (stat != SATA_SUCCESS)
5441 					/*
5442 					 * Page processing did not succeed -
5443 					 * all error info is already set-up,
5444 					 * just return
5445 					 */
5446 					pllen = 0; /* this breaks the loop */
5447 				else {
5448 					len += pagelen;
5449 					pllen -= pagelen;
5450 				}
5451 
5452 				break;
5453 			case MODEPAGE_POWER_COND:
5454 				stat = sata_mode_select_page_1a(spx,
5455 				    (struct mode_info_power_cond *)&buf[len],
5456 				    pllen, &pagelen, &rval, &dmod);
5457 				/*
5458 				 * The pagelen value indicates the number of
5459 				 * parameter bytes already processed.
5460 				 * The rval is the return value from
5461 				 * sata_tran_start().
5462 				 * The stat indicates the overall status of
5463 				 * the operation(s).
5464 				 */
5465 				if (stat != SATA_SUCCESS)
5466 					/*
5467 					 * Page processing did not succeed -
5468 					 * all error info is already set-up,
5469 					 * just return
5470 					 */
5471 					pllen = 0; /* this breaks the loop */
5472 				else {
5473 					len += pagelen;
5474 					pllen -= pagelen;
5475 				}
5476 				break;
5477 			default:
5478 				*scsipkt->pkt_scbp = STATUS_CHECK;
5479 				sense = sata_arq_sense(spx);
5480 				sense->es_key = KEY_ILLEGAL_REQUEST;
5481 				sense->es_add_code =
5482 				    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
5483 				goto done;
5484 			}
5485 		}
5486 	}
5487 done:
5488 	mutex_exit(cport_mutex);
5489 	/*
5490 	 * If device parameters were modified, fetch and store the new
5491 	 * Identify Device data. Since port mutex could have been released
5492 	 * for accessing HBA driver, we need to re-check device existence.
5493 	 */
5494 	if (dmod != 0) {
5495 		sata_drive_info_t new_sdinfo, *sdinfo;
5496 		int rv = 0;
5497 
5498 		/*
5499 		 * Following statement has to be changed if this function is
5500 		 * used for devices other than SATA hard disks.
5501 		 */
5502 		new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
5503 
5504 		new_sdinfo.satadrv_addr =
5505 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr;
5506 		rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
5507 		    &new_sdinfo);
5508 
5509 		mutex_enter(cport_mutex);
5510 		/*
5511 		 * Since port mutex could have been released when
5512 		 * accessing HBA driver, we need to re-check that the
5513 		 * framework still holds the device info structure.
5514 		 */
5515 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
5516 		    &spx->txlt_sata_pkt->satapkt_device);
5517 		if (sdinfo != NULL) {
5518 			/*
5519 			 * Device still has info structure in the
5520 			 * sata framework. Copy newly fetched info
5521 			 */
5522 			if (rv == 0) {
5523 				sdinfo->satadrv_id = new_sdinfo.satadrv_id;
5524 				sata_save_drive_settings(sdinfo);
5525 			} else {
5526 				/*
5527 				 * Could not fetch new data - invalidate
5528 				 * sata_drive_info. That makes device
5529 				 * unusable.
5530 				 */
5531 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
5532 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
5533 			}
5534 		}
5535 		if (rv != 0 || sdinfo == NULL) {
5536 			/*
5537 			 * This changes the overall mode select completion
5538 			 * reason to a failed one !!!!!
5539 			 */
5540 			*scsipkt->pkt_scbp = STATUS_CHECK;
5541 			sense = sata_arq_sense(spx);
5542 			scsipkt->pkt_reason = CMD_INCOMPLETE;
5543 			rval = TRAN_ACCEPT;
5544 		}
5545 		mutex_exit(cport_mutex);
5546 	}
5547 	/* Restore the scsi pkt flags */
5548 	scsipkt->pkt_flags &= ~FLAG_NOINTR;
5549 	scsipkt->pkt_flags |= nointr_flag;
5550 
5551 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5552 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5553 
5554 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5555 	    scsipkt->pkt_comp != NULL) {
5556 		/* scsi callback required */
5557 		if (servicing_interrupt()) {
5558 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5559 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5560 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
5561 				return (TRAN_BUSY);
5562 			}
5563 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5564 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5565 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
5566 			/* Scheduling the callback failed */
5567 			return (TRAN_BUSY);
5568 		}
5569 	}
5570 
5571 	return (rval);
5572 }
5573 
5574 /*
5575  * Translate command: ATA Pass Through
5576  * Incomplete implementation.  Only supports No-Data, PIO Data-In, and
5577  * PIO Data-Out protocols.  Also supports CK_COND bit.
5578  *
5579  * Mapping of the incoming CDB bytes to the outgoing satacmd bytes is
5580  * described in Table 111 of SAT-2 (Draft 9).
5581  */
5582 static  int
5583 sata_txlt_ata_pass_thru(sata_pkt_txlate_t *spx)
5584 {
5585 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5586 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5587 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5588 	int extend;
5589 	uint64_t lba;
5590 	uint16_t feature, sec_count;
5591 	int t_len, synch;
5592 	int rval, reason;
5593 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5594 
5595 	mutex_enter(cport_mutex);
5596 
5597 	rval = sata_txlt_generic_pkt_info(spx, &reason, 1);
5598 	if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5599 		mutex_exit(cport_mutex);
5600 		return (rval);
5601 	}
5602 
5603 	/* T_DIR bit */
5604 	if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_T_DIR)
5605 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
5606 	else
5607 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
5608 
5609 	/* MULTIPLE_COUNT field.  If non-zero, invalid command (for now). */
5610 	if (((scsipkt->pkt_cdbp[1] >> 5) & 0x7) != 0) {
5611 		mutex_exit(cport_mutex);
5612 		return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5613 	}
5614 
5615 	/* OFFLINE field. If non-zero, invalid command (for now). */
5616 	if (((scsipkt->pkt_cdbp[2] >> 6) & 0x3) != 0) {
5617 		mutex_exit(cport_mutex);
5618 		return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5619 	}
5620 
5621 	/* PROTOCOL field */
5622 	switch ((scsipkt->pkt_cdbp[1] >> 1) & 0xf) {
5623 	case SATL_APT_P_HW_RESET:
5624 	case SATL_APT_P_SRST:
5625 	case SATL_APT_P_DMA:
5626 	case SATL_APT_P_DMA_QUEUED:
5627 	case SATL_APT_P_DEV_DIAG:
5628 	case SATL_APT_P_DEV_RESET:
5629 	case SATL_APT_P_UDMA_IN:
5630 	case SATL_APT_P_UDMA_OUT:
5631 	case SATL_APT_P_FPDMA:
5632 	case SATL_APT_P_RET_RESP:
5633 		/* Not yet implemented */
5634 	default:
5635 		mutex_exit(cport_mutex);
5636 		return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5637 
5638 	case SATL_APT_P_NON_DATA:
5639 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
5640 		break;
5641 
5642 	case SATL_APT_P_PIO_DATA_IN:
5643 		/* If PROTOCOL disagrees with T_DIR, invalid command */
5644 		if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_WRITE) {
5645 			mutex_exit(cport_mutex);
5646 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5647 		}
5648 
5649 		/* if there is a buffer, release its DMA resources */
5650 		if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) {
5651 			sata_scsi_dmafree(NULL, scsipkt);
5652 		} else {
5653 			/* if there is no buffer, how do you PIO in? */
5654 			mutex_exit(cport_mutex);
5655 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5656 		}
5657 
5658 		break;
5659 
5660 	case SATL_APT_P_PIO_DATA_OUT:
5661 		/* If PROTOCOL disagrees with T_DIR, invalid command */
5662 		if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_READ) {
5663 			mutex_exit(cport_mutex);
5664 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5665 		}
5666 
5667 		/* if there is a buffer, release its DMA resources */
5668 		if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) {
5669 			sata_scsi_dmafree(NULL, scsipkt);
5670 		} else {
5671 			/* if there is no buffer, how do you PIO out? */
5672 			mutex_exit(cport_mutex);
5673 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5674 		}
5675 
5676 		break;
5677 	}
5678 
5679 	/* Parse the ATA cmd fields, transfer some straight to the satacmd */
5680 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
5681 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12:
5682 		feature = scsipkt->pkt_cdbp[3];
5683 
5684 		sec_count = scsipkt->pkt_cdbp[4];
5685 
5686 		lba = scsipkt->pkt_cdbp[8] & 0xf;
5687 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5688 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
5689 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5690 
5691 		scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] & 0xf0;
5692 		scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[9];
5693 
5694 		break;
5695 
5696 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16:
5697 		if (scsipkt->pkt_cdbp[1] & SATL_APT_BM_EXTEND) {
5698 			extend = 1;
5699 
5700 			feature = scsipkt->pkt_cdbp[3];
5701 			feature = (feature << 8) | scsipkt->pkt_cdbp[4];
5702 
5703 			sec_count = scsipkt->pkt_cdbp[5];
5704 			sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[6];
5705 
5706 			lba = scsipkt->pkt_cdbp[11];
5707 			lba = (lba << 8) | scsipkt->pkt_cdbp[12];
5708 			lba = (lba << 8) | scsipkt->pkt_cdbp[9];
5709 			lba = (lba << 8) | scsipkt->pkt_cdbp[10];
5710 			lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5711 			lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5712 
5713 			scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13];
5714 			scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14];
5715 		} else {
5716 			feature = scsipkt->pkt_cdbp[3];
5717 
5718 			sec_count = scsipkt->pkt_cdbp[5];
5719 
5720 			lba = scsipkt->pkt_cdbp[13] & 0xf;
5721 			lba = (lba << 8) | scsipkt->pkt_cdbp[12];
5722 			lba = (lba << 8) | scsipkt->pkt_cdbp[10];
5723 			lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5724 
5725 			scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] &
5726 			    0xf0;
5727 			scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14];
5728 		}
5729 
5730 		break;
5731 	}
5732 
5733 	/* CK_COND bit */
5734 	if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) {
5735 		if (extend) {
5736 			scmd->satacmd_flags.sata_copy_out_sec_count_msb = 1;
5737 			scmd->satacmd_flags.sata_copy_out_lba_low_msb = 1;
5738 			scmd->satacmd_flags.sata_copy_out_lba_mid_msb = 1;
5739 			scmd->satacmd_flags.sata_copy_out_lba_high_msb = 1;
5740 		}
5741 
5742 		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1;
5743 		scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1;
5744 		scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1;
5745 		scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1;
5746 		scmd->satacmd_flags.sata_copy_out_device_reg = 1;
5747 		scmd->satacmd_flags.sata_copy_out_error_reg = 1;
5748 	}
5749 
5750 	/* Transfer remaining parsed ATA cmd values to the satacmd */
5751 	if (extend) {
5752 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
5753 
5754 		scmd->satacmd_features_reg_ext = (feature >> 8) & 0xff;
5755 		scmd->satacmd_sec_count_msb = (sec_count >> 8) & 0xff;
5756 		scmd->satacmd_lba_low_msb = (lba >> 8) & 0xff;
5757 		scmd->satacmd_lba_mid_msb = (lba >> 8) & 0xff;
5758 		scmd->satacmd_lba_high_msb = lba >> 40;
5759 	} else {
5760 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
5761 
5762 		scmd->satacmd_features_reg_ext = 0;
5763 		scmd->satacmd_sec_count_msb = 0;
5764 		scmd->satacmd_lba_low_msb = 0;
5765 		scmd->satacmd_lba_mid_msb = 0;
5766 		scmd->satacmd_lba_high_msb = 0;
5767 	}
5768 
5769 	scmd->satacmd_features_reg = feature & 0xff;
5770 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
5771 	scmd->satacmd_lba_low_lsb = lba & 0xff;
5772 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
5773 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
5774 
5775 	/* Determine transfer length */
5776 	switch (scsipkt->pkt_cdbp[2] & 0x3) {		/* T_LENGTH field */
5777 	case 1:
5778 		t_len = feature;
5779 		break;
5780 	case 2:
5781 		t_len = sec_count;
5782 		break;
5783 	default:
5784 		t_len = 0;
5785 		break;
5786 	}
5787 
5788 	/* Adjust transfer length for the Byte Block bit */
5789 	if ((scsipkt->pkt_cdbp[2] >> 2) & 1)
5790 		t_len *= SATA_DISK_SECTOR_SIZE;
5791 
5792 	/* Start processing command */
5793 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5794 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_apt_completion;
5795 		synch = FALSE;
5796 	} else {
5797 		synch = TRUE;
5798 	}
5799 
5800 	if (sata_hba_start(spx, &rval) != 0) {
5801 		mutex_exit(cport_mutex);
5802 		return (rval);
5803 	}
5804 
5805 	mutex_exit(cport_mutex);
5806 
5807 	if (synch) {
5808 		sata_txlt_apt_completion(spx->txlt_sata_pkt);
5809 	}
5810 
5811 	return (TRAN_ACCEPT);
5812 }
5813 
5814 /*
5815  * Translate command: Log Sense
5816  */
5817 static 	int
5818 sata_txlt_log_sense(sata_pkt_txlate_t *spx)
5819 {
5820 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
5821 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5822 	sata_drive_info_t *sdinfo;
5823 	struct scsi_extended_sense *sense;
5824 	int 		len, count, alc_len;
5825 	int		pc;	/* Page Control code */
5826 	int		page_code;	/* Page code */
5827 	uint8_t		*buf;	/* log sense buffer */
5828 	int		rval, reason;
5829 #define	MAX_LOG_SENSE_PAGE_SIZE	512
5830 	kmutex_t	*cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5831 
5832 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5833 	    "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
5834 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5835 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5836 
5837 	if (servicing_interrupt()) {
5838 		buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_NOSLEEP);
5839 		if (buf == NULL) {
5840 			return (TRAN_BUSY);
5841 		}
5842 	} else {
5843 		buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
5844 	}
5845 
5846 	mutex_enter(cport_mutex);
5847 
5848 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
5849 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5850 		mutex_exit(cport_mutex);
5851 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
5852 		return (rval);
5853 	}
5854 
5855 	scsipkt->pkt_reason = CMD_CMPLT;
5856 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5857 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5858 
5859 	pc = scsipkt->pkt_cdbp[2] >> 6;
5860 	page_code = scsipkt->pkt_cdbp[2] & 0x3f;
5861 
5862 	/* Reject not supported request for all but cumulative values */
5863 	switch (pc) {
5864 	case PC_CUMULATIVE_VALUES:
5865 		break;
5866 	default:
5867 		*scsipkt->pkt_scbp = STATUS_CHECK;
5868 		sense = sata_arq_sense(spx);
5869 		sense->es_key = KEY_ILLEGAL_REQUEST;
5870 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5871 		goto done;
5872 	}
5873 
5874 	switch (page_code) {
5875 	case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5876 	case PAGE_CODE_SELF_TEST_RESULTS:
5877 	case PAGE_CODE_INFORMATION_EXCEPTIONS:
5878 	case PAGE_CODE_SMART_READ_DATA:
5879 	case PAGE_CODE_START_STOP_CYCLE_COUNTER:
5880 		break;
5881 	default:
5882 		*scsipkt->pkt_scbp = STATUS_CHECK;
5883 		sense = sata_arq_sense(spx);
5884 		sense->es_key = KEY_ILLEGAL_REQUEST;
5885 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5886 		goto done;
5887 	}
5888 
5889 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
5890 		/*
5891 		 * Because log sense uses local buffers for data retrieval from
5892 		 * the devices and sets the data programatically in the
5893 		 * original specified buffer, release preallocated DMA
5894 		 * resources before storing data in the original buffer,
5895 		 * so no unwanted DMA sync would take place.
5896 		 */
5897 		sata_id_t *sata_id;
5898 
5899 		sata_scsi_dmafree(NULL, scsipkt);
5900 
5901 		len = 0;
5902 
5903 		/* Build log parameter header */
5904 		buf[len++] = page_code;	/* page code as in the CDB */
5905 		buf[len++] = 0;		/* reserved */
5906 		buf[len++] = 0;		/* Zero out page length for now (MSB) */
5907 		buf[len++] = 0;		/* (LSB) */
5908 
5909 		sdinfo = sata_get_device_info(
5910 		    spx->txlt_sata_hba_inst,
5911 		    &spx->txlt_sata_pkt->satapkt_device);
5912 
5913 		/*
5914 		 * Add requested pages.
5915 		 */
5916 		switch (page_code) {
5917 		case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5918 			len = sata_build_lsense_page_0(sdinfo, buf + len);
5919 			break;
5920 		case PAGE_CODE_SELF_TEST_RESULTS:
5921 			sata_id = &sdinfo->satadrv_id;
5922 			if ((! (sata_id->ai_cmdset84 &
5923 			    SATA_SMART_SELF_TEST_SUPPORTED)) ||
5924 			    (! (sata_id->ai_features87 &
5925 			    SATA_SMART_SELF_TEST_SUPPORTED))) {
5926 				*scsipkt->pkt_scbp = STATUS_CHECK;
5927 				sense = sata_arq_sense(spx);
5928 				sense->es_key = KEY_ILLEGAL_REQUEST;
5929 				sense->es_add_code =
5930 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5931 
5932 				goto done;
5933 			}
5934 			len = sata_build_lsense_page_10(sdinfo, buf + len,
5935 			    spx->txlt_sata_hba_inst);
5936 			break;
5937 		case PAGE_CODE_INFORMATION_EXCEPTIONS:
5938 			sata_id = &sdinfo->satadrv_id;
5939 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5940 				*scsipkt->pkt_scbp = STATUS_CHECK;
5941 				sense = sata_arq_sense(spx);
5942 				sense->es_key = KEY_ILLEGAL_REQUEST;
5943 				sense->es_add_code =
5944 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5945 
5946 				goto done;
5947 			}
5948 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5949 				*scsipkt->pkt_scbp = STATUS_CHECK;
5950 				sense = sata_arq_sense(spx);
5951 				sense->es_key = KEY_ABORTED_COMMAND;
5952 				sense->es_add_code =
5953 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5954 				sense->es_qual_code =
5955 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5956 
5957 				goto done;
5958 			}
5959 
5960 			len = sata_build_lsense_page_2f(sdinfo, buf + len,
5961 			    spx->txlt_sata_hba_inst);
5962 			break;
5963 		case PAGE_CODE_SMART_READ_DATA:
5964 			sata_id = &sdinfo->satadrv_id;
5965 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5966 				*scsipkt->pkt_scbp = STATUS_CHECK;
5967 				sense = sata_arq_sense(spx);
5968 				sense->es_key = KEY_ILLEGAL_REQUEST;
5969 				sense->es_add_code =
5970 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5971 
5972 				goto done;
5973 			}
5974 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5975 				*scsipkt->pkt_scbp = STATUS_CHECK;
5976 				sense = sata_arq_sense(spx);
5977 				sense->es_key = KEY_ABORTED_COMMAND;
5978 				sense->es_add_code =
5979 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5980 				sense->es_qual_code =
5981 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5982 
5983 				goto done;
5984 			}
5985 
5986 			/* This page doesn't include a page header */
5987 			len = sata_build_lsense_page_30(sdinfo, buf,
5988 			    spx->txlt_sata_hba_inst);
5989 			goto no_header;
5990 		case PAGE_CODE_START_STOP_CYCLE_COUNTER:
5991 			sata_id = &sdinfo->satadrv_id;
5992 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5993 				*scsipkt->pkt_scbp = STATUS_CHECK;
5994 				sense = sata_arq_sense(spx);
5995 				sense->es_key = KEY_ILLEGAL_REQUEST;
5996 				sense->es_add_code =
5997 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5998 
5999 				goto done;
6000 			}
6001 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
6002 				*scsipkt->pkt_scbp = STATUS_CHECK;
6003 				sense = sata_arq_sense(spx);
6004 				sense->es_key = KEY_ABORTED_COMMAND;
6005 				sense->es_add_code =
6006 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
6007 				sense->es_qual_code =
6008 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
6009 
6010 				goto done;
6011 			}
6012 			len = sata_build_lsense_page_0e(sdinfo, buf, spx);
6013 			goto no_header;
6014 		default:
6015 			/* Invalid request */
6016 			*scsipkt->pkt_scbp = STATUS_CHECK;
6017 			sense = sata_arq_sense(spx);
6018 			sense->es_key = KEY_ILLEGAL_REQUEST;
6019 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6020 			goto done;
6021 		}
6022 
6023 		/* set parameter log sense data length */
6024 		buf[2] = len >> 8;	/* log sense length (MSB) */
6025 		buf[3] = len & 0xff;	/* log sense length (LSB) */
6026 
6027 		len += SCSI_LOG_PAGE_HDR_LEN;
6028 		ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
6029 
6030 no_header:
6031 		/* Check allocation length */
6032 		alc_len = scsipkt->pkt_cdbp[7];
6033 		alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
6034 
6035 		/*
6036 		 * We do not check for possible parameters truncation
6037 		 * (alc_len < len) assuming that the target driver works
6038 		 * correctly. Just avoiding overrun.
6039 		 * Copy no more than requested and possible, buffer-wise.
6040 		 */
6041 		count = MIN(alc_len, len);
6042 		count = MIN(bp->b_bcount, count);
6043 		bcopy(buf, bp->b_un.b_addr, count);
6044 
6045 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
6046 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
6047 	}
6048 	*scsipkt->pkt_scbp = STATUS_GOOD;
6049 done:
6050 	mutex_exit(cport_mutex);
6051 	(void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
6052 
6053 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6054 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6055 
6056 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6057 	    scsipkt->pkt_comp != NULL) {
6058 		/* scsi callback required */
6059 		if (servicing_interrupt()) {
6060 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6061 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6062 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
6063 				return (TRAN_BUSY);
6064 			}
6065 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6066 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6067 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
6068 			/* Scheduling the callback failed */
6069 			return (TRAN_BUSY);
6070 		}
6071 	}
6072 
6073 	return (TRAN_ACCEPT);
6074 }
6075 
6076 /*
6077  * Translate command: Log Select
6078  * Not implemented at this time - returns invalid command response.
6079  */
6080 static	int
6081 sata_txlt_log_select(sata_pkt_txlate_t *spx)
6082 {
6083 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6084 	    "sata_txlt_log_select\n", NULL);
6085 
6086 	return (sata_txlt_invalid_command(spx));
6087 }
6088 
6089 
6090 /*
6091  * Translate command: Read (various types).
6092  * Translated into appropriate type of ATA READ command
6093  * for SATA hard disks.
6094  * Both the device capabilities and requested operation mode are
6095  * considered.
6096  *
6097  * Following scsi cdb fields are ignored:
6098  * rdprotect, dpo, fua, fua_nv, group_number.
6099  *
6100  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
6101  * enable variable sata_func_enable), the capability of the controller and
6102  * capability of a device are checked and if both support queueing, read
6103  * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
6104  * command rather than plain READ_XXX command.
6105  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
6106  * both the controller and device suport such functionality, the read
6107  * request will be translated to READ_FPDMA_QUEUED command.
6108  * In both cases the maximum queue depth is derived as minimum of:
6109  * HBA capability,device capability and sata_max_queue_depth variable setting.
6110  * The value passed to HBA driver is decremented by 1, because only 5 bits are
6111  * used to pass max queue depth value, and the maximum possible queue depth
6112  * is 32.
6113  *
6114  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6115  * appropriate values in scsi_pkt fields.
6116  */
6117 static int
6118 sata_txlt_read(sata_pkt_txlate_t *spx)
6119 {
6120 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6121 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6122 	sata_drive_info_t *sdinfo;
6123 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
6124 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6125 	uint16_t sec_count;
6126 	uint64_t lba;
6127 	int rval, reason;
6128 	int synch;
6129 
6130 	mutex_enter(cport_mutex);
6131 
6132 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
6133 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6134 		mutex_exit(cport_mutex);
6135 		return (rval);
6136 	}
6137 
6138 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6139 	    &spx->txlt_sata_pkt->satapkt_device);
6140 
6141 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
6142 	/*
6143 	 * Extract LBA and sector count from scsi CDB.
6144 	 */
6145 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6146 	case SCMD_READ:
6147 		/* 6-byte scsi read cmd : 0x08 */
6148 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
6149 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
6150 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6151 		sec_count = scsipkt->pkt_cdbp[4];
6152 		/* sec_count 0 will be interpreted as 256 by a device */
6153 		break;
6154 	case SCMD_READ_G1:
6155 		/* 10-bytes scsi read command : 0x28 */
6156 		lba = scsipkt->pkt_cdbp[2];
6157 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6158 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6159 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6160 		sec_count = scsipkt->pkt_cdbp[7];
6161 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6162 		break;
6163 	case SCMD_READ_G5:
6164 		/* 12-bytes scsi read command : 0xA8 */
6165 		lba = scsipkt->pkt_cdbp[2];
6166 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6167 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6168 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6169 		sec_count = scsipkt->pkt_cdbp[6];
6170 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
6171 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6172 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
6173 		break;
6174 	case SCMD_READ_G4:
6175 		/* 16-bytes scsi read command : 0x88 */
6176 		lba = scsipkt->pkt_cdbp[2];
6177 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6178 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6179 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6180 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
6181 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
6182 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
6183 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
6184 		sec_count = scsipkt->pkt_cdbp[10];
6185 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
6186 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
6187 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
6188 		break;
6189 	default:
6190 		/* Unsupported command */
6191 		mutex_exit(cport_mutex);
6192 		return (sata_txlt_invalid_command(spx));
6193 	}
6194 
6195 	/*
6196 	 * Check if specified address exceeds device capacity
6197 	 */
6198 	if ((lba >= sdinfo->satadrv_capacity) ||
6199 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
6200 		/* LBA out of range */
6201 		mutex_exit(cport_mutex);
6202 		return (sata_txlt_lba_out_of_range(spx));
6203 	}
6204 
6205 	/*
6206 	 * For zero-length transfer, emulate good completion of the command
6207 	 * (reasons for rejecting the command were already checked).
6208 	 * No DMA resources were allocated.
6209 	 */
6210 	if (spx->txlt_dma_cookie_list == NULL) {
6211 		mutex_exit(cport_mutex);
6212 		return (sata_emul_rw_completion(spx));
6213 	}
6214 
6215 	/*
6216 	 * Build cmd block depending on the device capability and
6217 	 * requested operation mode.
6218 	 * Do not bother with non-dma mode - we are working only with
6219 	 * devices supporting DMA.
6220 	 */
6221 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
6222 	scmd->satacmd_device_reg = SATA_ADH_LBA;
6223 	scmd->satacmd_cmd_reg = SATAC_READ_DMA;
6224 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6225 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
6226 		scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT;
6227 		scmd->satacmd_sec_count_msb = sec_count >> 8;
6228 #ifndef __lock_lint
6229 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
6230 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
6231 		scmd->satacmd_lba_high_msb = lba >> 40;
6232 #endif
6233 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
6234 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
6235 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
6236 	}
6237 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
6238 	scmd->satacmd_lba_low_lsb = lba & 0xff;
6239 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
6240 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
6241 	scmd->satacmd_features_reg = 0;
6242 	scmd->satacmd_status_reg = 0;
6243 	scmd->satacmd_error_reg = 0;
6244 
6245 	/*
6246 	 * Check if queueing commands should be used and switch
6247 	 * to appropriate command if possible
6248 	 */
6249 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
6250 		boolean_t using_queuing;
6251 
6252 		/* Queuing supported by controller and device? */
6253 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
6254 		    (sdinfo->satadrv_features_support &
6255 		    SATA_DEV_F_NCQ) &&
6256 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6257 		    SATA_CTLF_NCQ)) {
6258 			using_queuing = B_TRUE;
6259 
6260 			/* NCQ supported - use FPDMA READ */
6261 			scmd->satacmd_cmd_reg =
6262 			    SATAC_READ_FPDMA_QUEUED;
6263 			scmd->satacmd_features_reg_ext =
6264 			    scmd->satacmd_sec_count_msb;
6265 			scmd->satacmd_sec_count_msb = 0;
6266 		} else if ((sdinfo->satadrv_features_support &
6267 		    SATA_DEV_F_TCQ) &&
6268 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6269 		    SATA_CTLF_QCMD)) {
6270 			using_queuing = B_TRUE;
6271 
6272 			/* Legacy queueing */
6273 			if (sdinfo->satadrv_features_support &
6274 			    SATA_DEV_F_LBA48) {
6275 				scmd->satacmd_cmd_reg =
6276 				    SATAC_READ_DMA_QUEUED_EXT;
6277 				scmd->satacmd_features_reg_ext =
6278 				    scmd->satacmd_sec_count_msb;
6279 				scmd->satacmd_sec_count_msb = 0;
6280 			} else {
6281 				scmd->satacmd_cmd_reg =
6282 				    SATAC_READ_DMA_QUEUED;
6283 			}
6284 		} else	/* NCQ nor legacy queuing not supported */
6285 			using_queuing = B_FALSE;
6286 
6287 		/*
6288 		 * If queuing, the sector count goes in the features register
6289 		 * and the secount count will contain the tag.
6290 		 */
6291 		if (using_queuing) {
6292 			scmd->satacmd_features_reg =
6293 			    scmd->satacmd_sec_count_lsb;
6294 			scmd->satacmd_sec_count_lsb = 0;
6295 			scmd->satacmd_flags.sata_queued = B_TRUE;
6296 
6297 			/* Set-up maximum queue depth */
6298 			scmd->satacmd_flags.sata_max_queue_depth =
6299 			    sdinfo->satadrv_max_queue_depth - 1;
6300 		} else if (sdinfo->satadrv_features_enabled &
6301 		    SATA_DEV_F_E_UNTAGGED_QING) {
6302 			/*
6303 			 * Although NCQ/TCQ is not enabled, untagged queuing
6304 			 * may be still used.
6305 			 * Set-up the maximum untagged queue depth.
6306 			 * Use controller's queue depth from sata_hba_tran.
6307 			 * SATA HBA drivers may ignore this value and rely on
6308 			 * the internal limits.For drivers that do not
6309 			 * ignore untaged queue depth, limit the value to
6310 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
6311 			 * largest value that can be passed via
6312 			 * satacmd_flags.sata_max_queue_depth.
6313 			 */
6314 			scmd->satacmd_flags.sata_max_queue_depth =
6315 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
6316 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
6317 
6318 		} else {
6319 			scmd->satacmd_flags.sata_max_queue_depth = 0;
6320 		}
6321 	} else
6322 		scmd->satacmd_flags.sata_max_queue_depth = 0;
6323 
6324 	SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst,
6325 	    "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n",
6326 	    scmd->satacmd_cmd_reg, lba, sec_count);
6327 
6328 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6329 		/* Need callback function */
6330 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
6331 		synch = FALSE;
6332 	} else
6333 		synch = TRUE;
6334 
6335 	/* Transfer command to HBA */
6336 	if (sata_hba_start(spx, &rval) != 0) {
6337 		/* Pkt not accepted for execution */
6338 		mutex_exit(cport_mutex);
6339 		return (rval);
6340 	}
6341 	mutex_exit(cport_mutex);
6342 	/*
6343 	 * If execution is non-synchronous,
6344 	 * a callback function will handle potential errors, translate
6345 	 * the response and will do a callback to a target driver.
6346 	 * If it was synchronous, check execution status using the same
6347 	 * framework callback.
6348 	 */
6349 	if (synch) {
6350 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6351 		    "synchronous execution status %x\n",
6352 		    spx->txlt_sata_pkt->satapkt_reason);
6353 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
6354 	}
6355 	return (TRAN_ACCEPT);
6356 }
6357 
6358 
6359 /*
6360  * SATA translate command: Write (various types)
6361  * Translated into appropriate type of ATA WRITE command
6362  * for SATA hard disks.
6363  * Both the device capabilities and requested operation mode are
6364  * considered.
6365  *
6366  * Following scsi cdb fields are ignored:
6367  * rwprotect, dpo, fua, fua_nv, group_number.
6368  *
6369  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
6370  * enable variable sata_func_enable), the capability of the controller and
6371  * capability of a device are checked and if both support queueing, write
6372  * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT
6373  * command rather than plain WRITE_XXX command.
6374  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
6375  * both the controller and device suport such functionality, the write
6376  * request will be translated to WRITE_FPDMA_QUEUED command.
6377  * In both cases the maximum queue depth is derived as minimum of:
6378  * HBA capability,device capability and sata_max_queue_depth variable setting.
6379  * The value passed to HBA driver is decremented by 1, because only 5 bits are
6380  * used to pass max queue depth value, and the maximum possible queue depth
6381  * is 32.
6382  *
6383  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6384  * appropriate values in scsi_pkt fields.
6385  */
6386 static int
6387 sata_txlt_write(sata_pkt_txlate_t *spx)
6388 {
6389 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6390 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6391 	sata_drive_info_t *sdinfo;
6392 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
6393 	uint16_t sec_count;
6394 	uint64_t lba;
6395 	int rval, reason;
6396 	int synch;
6397 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6398 
6399 	mutex_enter(cport_mutex);
6400 
6401 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
6402 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6403 		mutex_exit(cport_mutex);
6404 		return (rval);
6405 	}
6406 
6407 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6408 	    &spx->txlt_sata_pkt->satapkt_device);
6409 
6410 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
6411 	/*
6412 	 * Extract LBA and sector count from scsi CDB
6413 	 */
6414 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6415 	case SCMD_WRITE:
6416 		/* 6-byte scsi read cmd : 0x0A */
6417 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
6418 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
6419 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6420 		sec_count = scsipkt->pkt_cdbp[4];
6421 		/* sec_count 0 will be interpreted as 256 by a device */
6422 		break;
6423 	case SCMD_WRITE_G1:
6424 		/* 10-bytes scsi write command : 0x2A */
6425 		lba = scsipkt->pkt_cdbp[2];
6426 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6427 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6428 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6429 		sec_count = scsipkt->pkt_cdbp[7];
6430 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6431 		break;
6432 	case SCMD_WRITE_G5:
6433 		/* 12-bytes scsi read command : 0xAA */
6434 		lba = scsipkt->pkt_cdbp[2];
6435 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6436 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6437 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6438 		sec_count = scsipkt->pkt_cdbp[6];
6439 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
6440 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6441 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
6442 		break;
6443 	case SCMD_WRITE_G4:
6444 		/* 16-bytes scsi write command : 0x8A */
6445 		lba = scsipkt->pkt_cdbp[2];
6446 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6447 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6448 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6449 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
6450 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
6451 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
6452 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
6453 		sec_count = scsipkt->pkt_cdbp[10];
6454 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
6455 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
6456 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
6457 		break;
6458 	default:
6459 		/* Unsupported command */
6460 		mutex_exit(cport_mutex);
6461 		return (sata_txlt_invalid_command(spx));
6462 	}
6463 
6464 	/*
6465 	 * Check if specified address and length exceeds device capacity
6466 	 */
6467 	if ((lba >= sdinfo->satadrv_capacity) ||
6468 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
6469 		/* LBA out of range */
6470 		mutex_exit(cport_mutex);
6471 		return (sata_txlt_lba_out_of_range(spx));
6472 	}
6473 
6474 	/*
6475 	 * For zero-length transfer, emulate good completion of the command
6476 	 * (reasons for rejecting the command were already checked).
6477 	 * No DMA resources were allocated.
6478 	 */
6479 	if (spx->txlt_dma_cookie_list == NULL) {
6480 		mutex_exit(cport_mutex);
6481 		return (sata_emul_rw_completion(spx));
6482 	}
6483 
6484 	/*
6485 	 * Build cmd block depending on the device capability and
6486 	 * requested operation mode.
6487 	 * Do not bother with non-dma mode- we are working only with
6488 	 * devices supporting DMA.
6489 	 */
6490 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
6491 	scmd->satacmd_device_reg = SATA_ADH_LBA;
6492 	scmd->satacmd_cmd_reg = SATAC_WRITE_DMA;
6493 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6494 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
6495 		scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT;
6496 		scmd->satacmd_sec_count_msb = sec_count >> 8;
6497 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
6498 #ifndef __lock_lint
6499 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
6500 		scmd->satacmd_lba_high_msb = lba >> 40;
6501 #endif
6502 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
6503 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
6504 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
6505 	}
6506 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
6507 	scmd->satacmd_lba_low_lsb = lba & 0xff;
6508 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
6509 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
6510 	scmd->satacmd_features_reg = 0;
6511 	scmd->satacmd_status_reg = 0;
6512 	scmd->satacmd_error_reg = 0;
6513 
6514 	/*
6515 	 * Check if queueing commands should be used and switch
6516 	 * to appropriate command if possible
6517 	 */
6518 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
6519 		boolean_t using_queuing;
6520 
6521 		/* Queuing supported by controller and device? */
6522 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
6523 		    (sdinfo->satadrv_features_support &
6524 		    SATA_DEV_F_NCQ) &&
6525 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6526 		    SATA_CTLF_NCQ)) {
6527 			using_queuing = B_TRUE;
6528 
6529 			/* NCQ supported - use FPDMA WRITE */
6530 			scmd->satacmd_cmd_reg =
6531 			    SATAC_WRITE_FPDMA_QUEUED;
6532 			scmd->satacmd_features_reg_ext =
6533 			    scmd->satacmd_sec_count_msb;
6534 			scmd->satacmd_sec_count_msb = 0;
6535 		} else if ((sdinfo->satadrv_features_support &
6536 		    SATA_DEV_F_TCQ) &&
6537 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6538 		    SATA_CTLF_QCMD)) {
6539 			using_queuing = B_TRUE;
6540 
6541 			/* Legacy queueing */
6542 			if (sdinfo->satadrv_features_support &
6543 			    SATA_DEV_F_LBA48) {
6544 				scmd->satacmd_cmd_reg =
6545 				    SATAC_WRITE_DMA_QUEUED_EXT;
6546 				scmd->satacmd_features_reg_ext =
6547 				    scmd->satacmd_sec_count_msb;
6548 				scmd->satacmd_sec_count_msb = 0;
6549 			} else {
6550 				scmd->satacmd_cmd_reg =
6551 				    SATAC_WRITE_DMA_QUEUED;
6552 			}
6553 		} else	/*  NCQ nor legacy queuing not supported */
6554 			using_queuing = B_FALSE;
6555 
6556 		if (using_queuing) {
6557 			scmd->satacmd_features_reg =
6558 			    scmd->satacmd_sec_count_lsb;
6559 			scmd->satacmd_sec_count_lsb = 0;
6560 			scmd->satacmd_flags.sata_queued = B_TRUE;
6561 			/* Set-up maximum queue depth */
6562 			scmd->satacmd_flags.sata_max_queue_depth =
6563 			    sdinfo->satadrv_max_queue_depth - 1;
6564 		} else if (sdinfo->satadrv_features_enabled &
6565 		    SATA_DEV_F_E_UNTAGGED_QING) {
6566 			/*
6567 			 * Although NCQ/TCQ is not enabled, untagged queuing
6568 			 * may be still used.
6569 			 * Set-up the maximum untagged queue depth.
6570 			 * Use controller's queue depth from sata_hba_tran.
6571 			 * SATA HBA drivers may ignore this value and rely on
6572 			 * the internal limits. For drivera that do not
6573 			 * ignore untaged queue depth, limit the value to
6574 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
6575 			 * largest value that can be passed via
6576 			 * satacmd_flags.sata_max_queue_depth.
6577 			 */
6578 			scmd->satacmd_flags.sata_max_queue_depth =
6579 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
6580 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
6581 
6582 		} else {
6583 			scmd->satacmd_flags.sata_max_queue_depth = 0;
6584 		}
6585 	} else
6586 		scmd->satacmd_flags.sata_max_queue_depth = 0;
6587 
6588 	SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6589 	    "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n",
6590 	    scmd->satacmd_cmd_reg, lba, sec_count);
6591 
6592 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6593 		/* Need callback function */
6594 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
6595 		synch = FALSE;
6596 	} else
6597 		synch = TRUE;
6598 
6599 	/* Transfer command to HBA */
6600 	if (sata_hba_start(spx, &rval) != 0) {
6601 		/* Pkt not accepted for execution */
6602 		mutex_exit(cport_mutex);
6603 		return (rval);
6604 	}
6605 	mutex_exit(cport_mutex);
6606 
6607 	/*
6608 	 * If execution is non-synchronous,
6609 	 * a callback function will handle potential errors, translate
6610 	 * the response and will do a callback to a target driver.
6611 	 * If it was synchronous, check execution status using the same
6612 	 * framework callback.
6613 	 */
6614 	if (synch) {
6615 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6616 		    "synchronous execution status %x\n",
6617 		    spx->txlt_sata_pkt->satapkt_reason);
6618 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
6619 	}
6620 	return (TRAN_ACCEPT);
6621 }
6622 
6623 
6624 /*
6625  * Implements SCSI SBC WRITE BUFFER command download microcode option
6626  */
6627 static int
6628 sata_txlt_write_buffer(sata_pkt_txlate_t *spx)
6629 {
6630 #define	WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE			4
6631 #define	WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE			5
6632 
6633 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6634 	struct sata_pkt *sata_pkt = spx->txlt_sata_pkt;
6635 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6636 
6637 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
6638 	struct scsi_extended_sense *sense;
6639 	int rval, mode, sector_count, reason;
6640 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6641 
6642 	mode = scsipkt->pkt_cdbp[1] & 0x1f;
6643 
6644 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6645 	    "sata_txlt_write_buffer, mode 0x%x\n", mode);
6646 
6647 	mutex_enter(cport_mutex);
6648 
6649 	if ((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
6650 	    TRAN_ACCEPT) {
6651 		mutex_exit(cport_mutex);
6652 		return (rval);
6653 	}
6654 
6655 	/* Use synchronous mode */
6656 	spx->txlt_sata_pkt->satapkt_op_mode
6657 	    |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
6658 
6659 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
6660 
6661 	scsipkt->pkt_reason = CMD_CMPLT;
6662 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6663 	    STATE_SENT_CMD | STATE_GOT_STATUS;
6664 
6665 	/*
6666 	 * The SCSI to ATA translation specification only calls
6667 	 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE.
6668 	 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but
6669 	 * ATA 8 (draft) got rid of download microcode for temp
6670 	 * and it is even optional for ATA 7, so it may be aborted.
6671 	 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as
6672 	 * it is not specified and the buffer offset for SCSI is a 16-bit
6673 	 * value in bytes, but for ATA it is a 16-bit offset in 512 byte
6674 	 * sectors.  Thus the offset really doesn't buy us anything.
6675 	 * If and when ATA 8 is stabilized and the SCSI to ATA specification
6676 	 * is revised, this can be revisisted.
6677 	 */
6678 	/* Reject not supported request */
6679 	switch (mode) {
6680 	case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE:
6681 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP;
6682 		break;
6683 	case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE:
6684 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE;
6685 		break;
6686 	default:
6687 		goto bad_param;
6688 	}
6689 
6690 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
6691 
6692 	scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE;
6693 	if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0)
6694 		goto bad_param;
6695 	sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE;
6696 	scmd->satacmd_sec_count_lsb = (uint8_t)sector_count;
6697 	scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8;
6698 	scmd->satacmd_lba_mid_lsb = 0;
6699 	scmd->satacmd_lba_high_lsb = 0;
6700 	scmd->satacmd_device_reg = 0;
6701 	spx->txlt_sata_pkt->satapkt_comp = NULL;
6702 	scmd->satacmd_addr_type = 0;
6703 
6704 	/* Transfer command to HBA */
6705 	if (sata_hba_start(spx, &rval) != 0) {
6706 		/* Pkt not accepted for execution */
6707 		mutex_exit(cport_mutex);
6708 		return (rval);
6709 	}
6710 
6711 	mutex_exit(cport_mutex);
6712 
6713 	/* Then we need synchronous check the status of the disk */
6714 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6715 	    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
6716 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
6717 		scsipkt->pkt_reason = CMD_CMPLT;
6718 
6719 		/* Download commmand succeed, so probe and identify device */
6720 		sata_reidentify_device(spx);
6721 	} else {
6722 		/* Something went wrong, microcode download command failed */
6723 		scsipkt->pkt_reason = CMD_INCOMPLETE;
6724 		*scsipkt->pkt_scbp = STATUS_CHECK;
6725 		sense = sata_arq_sense(spx);
6726 		switch (sata_pkt->satapkt_reason) {
6727 		case SATA_PKT_PORT_ERROR:
6728 			/*
6729 			 * We have no device data. Assume no data transfered.
6730 			 */
6731 			sense->es_key = KEY_HARDWARE_ERROR;
6732 			break;
6733 
6734 		case SATA_PKT_DEV_ERROR:
6735 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
6736 			    SATA_STATUS_ERR) {
6737 				/*
6738 				 * determine dev error reason from error
6739 				 * reg content
6740 				 */
6741 				sata_decode_device_error(spx, sense);
6742 				break;
6743 			}
6744 			/* No extended sense key - no info available */
6745 			break;
6746 
6747 		case SATA_PKT_TIMEOUT:
6748 			scsipkt->pkt_reason = CMD_TIMEOUT;
6749 			scsipkt->pkt_statistics |=
6750 			    STAT_TIMEOUT | STAT_DEV_RESET;
6751 			/* No extended sense key ? */
6752 			break;
6753 
6754 		case SATA_PKT_ABORTED:
6755 			scsipkt->pkt_reason = CMD_ABORTED;
6756 			scsipkt->pkt_statistics |= STAT_ABORTED;
6757 			/* No extended sense key ? */
6758 			break;
6759 
6760 		case SATA_PKT_RESET:
6761 			/* pkt aborted by an explicit reset from a host */
6762 			scsipkt->pkt_reason = CMD_RESET;
6763 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
6764 			break;
6765 
6766 		default:
6767 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
6768 			    "sata_txlt_nodata_cmd_completion: "
6769 			    "invalid packet completion reason %d",
6770 			    sata_pkt->satapkt_reason));
6771 			scsipkt->pkt_reason = CMD_TRAN_ERR;
6772 			break;
6773 		}
6774 
6775 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6776 		    "scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6777 
6778 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6779 			/* scsi callback required */
6780 			scsi_hba_pkt_comp(scsipkt);
6781 	}
6782 	return (TRAN_ACCEPT);
6783 
6784 bad_param:
6785 	mutex_exit(cport_mutex);
6786 	*scsipkt->pkt_scbp = STATUS_CHECK;
6787 	sense = sata_arq_sense(spx);
6788 	sense->es_key = KEY_ILLEGAL_REQUEST;
6789 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6790 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6791 	    scsipkt->pkt_comp != NULL) {
6792 		/* scsi callback required */
6793 		if (servicing_interrupt()) {
6794 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6795 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6796 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
6797 				return (TRAN_BUSY);
6798 			}
6799 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6800 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6801 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
6802 			/* Scheduling the callback failed */
6803 			return (TRAN_BUSY);
6804 		}
6805 	}
6806 	return (rval);
6807 }
6808 
6809 /*
6810  * Re-identify device after doing a firmware download.
6811  */
6812 static void
6813 sata_reidentify_device(sata_pkt_txlate_t *spx)
6814 {
6815 #define	DOWNLOAD_WAIT_TIME_SECS	60
6816 #define	DOWNLOAD_WAIT_INTERVAL_SECS	1
6817 	int rval;
6818 	int retry_cnt;
6819 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6820 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
6821 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
6822 	sata_drive_info_t *sdinfo;
6823 
6824 	/*
6825 	 * Before returning good status, probe device.
6826 	 * Device probing will get IDENTIFY DEVICE data, if possible.
6827 	 * The assumption is that the new microcode is applied by the
6828 	 * device. It is a caller responsibility to verify this.
6829 	 */
6830 	for (retry_cnt = 0;
6831 	    retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS;
6832 	    retry_cnt++) {
6833 		rval = sata_probe_device(sata_hba_inst, &sata_device);
6834 
6835 		if (rval == SATA_SUCCESS) { /* Set default features */
6836 			sdinfo = sata_get_device_info(sata_hba_inst,
6837 			    &sata_device);
6838 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
6839 			    SATA_SUCCESS) {
6840 				/* retry */
6841 				rval = sata_initialize_device(sata_hba_inst,
6842 				    sdinfo);
6843 				if (rval == SATA_RETRY)
6844 					sata_log(sata_hba_inst, CE_WARN,
6845 					    "SATA device at port %d pmport %d -"
6846 					    " default device features could not"
6847 					    " be set. Device may not operate "
6848 					    "as expected.",
6849 					    sata_device.satadev_addr.cport,
6850 					    sata_device.satadev_addr.pmport);
6851 			}
6852 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6853 				scsi_hba_pkt_comp(scsipkt);
6854 			return;
6855 		} else if (rval == SATA_RETRY) {
6856 			delay(drv_usectohz(1000000 *
6857 			    DOWNLOAD_WAIT_INTERVAL_SECS));
6858 			continue;
6859 		} else	/* failed - no reason to retry */
6860 			break;
6861 	}
6862 
6863 	/*
6864 	 * Something went wrong, device probing failed.
6865 	 */
6866 	SATA_LOG_D((sata_hba_inst, CE_WARN,
6867 	    "Cannot probe device after downloading microcode\n"));
6868 
6869 	/* Reset device to force retrying the probe. */
6870 	(void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst))
6871 	    (SATA_DIP(sata_hba_inst), &sata_device);
6872 
6873 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6874 		scsi_hba_pkt_comp(scsipkt);
6875 }
6876 
6877 
6878 /*
6879  * Translate command: Synchronize Cache.
6880  * Translates into Flush Cache command for SATA hard disks.
6881  *
6882  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6883  * appropriate values in scsi_pkt fields.
6884  */
6885 static 	int
6886 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx)
6887 {
6888 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6889 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6890 	int rval, reason;
6891 	int synch;
6892 
6893 	mutex_enter(cport_mutex);
6894 
6895 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
6896 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6897 		mutex_exit(cport_mutex);
6898 		return (rval);
6899 	}
6900 
6901 	scmd->satacmd_addr_type = 0;
6902 	scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE;
6903 	scmd->satacmd_device_reg = 0;
6904 	scmd->satacmd_sec_count_lsb = 0;
6905 	scmd->satacmd_lba_low_lsb = 0;
6906 	scmd->satacmd_lba_mid_lsb = 0;
6907 	scmd->satacmd_lba_high_lsb = 0;
6908 	scmd->satacmd_features_reg = 0;
6909 	scmd->satacmd_status_reg = 0;
6910 	scmd->satacmd_error_reg = 0;
6911 
6912 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6913 	    "sata_txlt_synchronize_cache\n", NULL);
6914 
6915 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6916 		/* Need to set-up a callback function */
6917 		spx->txlt_sata_pkt->satapkt_comp =
6918 		    sata_txlt_nodata_cmd_completion;
6919 		synch = FALSE;
6920 	} else
6921 		synch = TRUE;
6922 
6923 	/* Transfer command to HBA */
6924 	if (sata_hba_start(spx, &rval) != 0) {
6925 		/* Pkt not accepted for execution */
6926 		mutex_exit(cport_mutex);
6927 		return (rval);
6928 	}
6929 	mutex_exit(cport_mutex);
6930 
6931 	/*
6932 	 * If execution non-synchronous, it had to be completed
6933 	 * a callback function will handle potential errors, translate
6934 	 * the response and will do a callback to a target driver.
6935 	 * If it was synchronous, check status, using the same
6936 	 * framework callback.
6937 	 */
6938 	if (synch) {
6939 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6940 		    "synchronous execution status %x\n",
6941 		    spx->txlt_sata_pkt->satapkt_reason);
6942 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
6943 	}
6944 	return (TRAN_ACCEPT);
6945 }
6946 
6947 
6948 /*
6949  * Send pkt to SATA HBA driver
6950  *
6951  * This function may be called only if the operation is requested by scsi_pkt,
6952  * i.e. scsi_pkt is not NULL.
6953  *
6954  * This function has to be called with cport mutex held. It does release
6955  * the mutex when it calls HBA driver sata_tran_start function and
6956  * re-acquires it afterwards.
6957  *
6958  * If return value is 0, pkt was accepted, -1 otherwise
6959  * rval is set to appropriate sata_scsi_start return value.
6960  *
6961  * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not
6962  * have called the sata_pkt callback function for this packet.
6963  *
6964  * The scsi callback has to be performed by the caller of this routine.
6965  */
6966 static int
6967 sata_hba_start(sata_pkt_txlate_t *spx, int *rval)
6968 {
6969 	int stat;
6970 	uint8_t cport = SATA_TXLT_CPORT(spx);
6971 	uint8_t pmport = SATA_TXLT_PMPORT(spx);
6972 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
6973 	sata_drive_info_t *sdinfo;
6974 	sata_pmult_info_t *pminfo;
6975 	sata_pmport_info_t *pmportinfo = NULL;
6976 	sata_device_t *sata_device = NULL;
6977 	uint8_t cmd;
6978 	struct sata_cmd_flags cmd_flags;
6979 
6980 	ASSERT(spx->txlt_sata_pkt != NULL);
6981 
6982 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6983 
6984 	sdinfo = sata_get_device_info(sata_hba_inst,
6985 	    &spx->txlt_sata_pkt->satapkt_device);
6986 	ASSERT(sdinfo != NULL);
6987 
6988 	/* Clear device reset state? */
6989 	/* qual should be XXX_DPMPORT, but add XXX_PMPORT in case */
6990 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT ||
6991 	    sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT) {
6992 
6993 		/*
6994 		 * Get the pmult_info of the its parent port multiplier, all
6995 		 * sub-devices share a common device reset flags on in
6996 		 * pmult_info.
6997 		 */
6998 		pminfo = SATA_PMULT_INFO(sata_hba_inst, cport);
6999 		pmportinfo = pminfo->pmult_dev_port[pmport];
7000 		ASSERT(pminfo != NULL);
7001 		if (pminfo->pmult_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) {
7002 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
7003 			    sata_clear_dev_reset = B_TRUE;
7004 			pminfo->pmult_event_flags &=
7005 			    ~SATA_EVNT_CLEAR_DEVICE_RESET;
7006 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7007 			    "sata_hba_start: clearing device reset state"
7008 			    "on pmult.\n", NULL);
7009 		}
7010 	} else {
7011 		if (sdinfo->satadrv_event_flags &
7012 		    SATA_EVNT_CLEAR_DEVICE_RESET) {
7013 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
7014 			    sata_clear_dev_reset = B_TRUE;
7015 			sdinfo->satadrv_event_flags &=
7016 			    ~SATA_EVNT_CLEAR_DEVICE_RESET;
7017 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7018 			    "sata_hba_start: clearing device reset state\n",
7019 			    NULL);
7020 		}
7021 	}
7022 
7023 	cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg;
7024 	cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags;
7025 	sata_device = &spx->txlt_sata_pkt->satapkt_device;
7026 
7027 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7028 
7029 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7030 	    "Sata cmd 0x%2x\n", cmd);
7031 
7032 	stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
7033 	    spx->txlt_sata_pkt);
7034 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7035 	/*
7036 	 * If sata pkt was accepted and executed in asynchronous mode, i.e.
7037 	 * with the sata callback, the sata_pkt could be already destroyed
7038 	 * by the time we check ther return status from the hba_start()
7039 	 * function, because sata_scsi_destroy_pkt() could have been already
7040 	 * called (perhaps in the interrupt context). So, in such case, there
7041 	 * should be no references to it. In other cases, sata_pkt still
7042 	 * exists.
7043 	 */
7044 	if (stat == SATA_TRAN_ACCEPTED) {
7045 		/*
7046 		 * pkt accepted for execution.
7047 		 * If it was executed synchronously, it is already completed
7048 		 * and pkt completion_reason indicates completion status.
7049 		 */
7050 		*rval = TRAN_ACCEPT;
7051 		return (0);
7052 	}
7053 
7054 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
7055 	switch (stat) {
7056 	case SATA_TRAN_QUEUE_FULL:
7057 		/*
7058 		 * Controller detected queue full condition.
7059 		 */
7060 		SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst,
7061 		    "sata_hba_start: queue full\n", NULL);
7062 
7063 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
7064 		*spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL;
7065 
7066 		*rval = TRAN_BUSY;
7067 		break;
7068 
7069 	case SATA_TRAN_PORT_ERROR:
7070 		/*
7071 		 * Communication/link with device or general port error
7072 		 * detected before pkt execution begun.
7073 		 */
7074 		if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
7075 		    SATA_ADDR_CPORT ||
7076 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
7077 		    SATA_ADDR_DCPORT)
7078 			sata_log(sata_hba_inst, CE_CONT,
7079 			    "SATA port %d error",
7080 			    sata_device->satadev_addr.cport);
7081 		else
7082 			sata_log(sata_hba_inst, CE_CONT,
7083 			    "SATA port %d:%d error\n",
7084 			    sata_device->satadev_addr.cport,
7085 			    sata_device->satadev_addr.pmport);
7086 
7087 		/*
7088 		 * Update the port/device structure.
7089 		 * sata_pkt should be still valid. Since port error is
7090 		 * returned, sata_device content should reflect port
7091 		 * state - it means, that sata address have been changed,
7092 		 * because original packet's sata address refered to a device
7093 		 * attached to some port.
7094 		 */
7095 		if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT ||
7096 		    sata_device->satadev_addr.qual == SATA_ADDR_PMPORT) {
7097 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7098 			mutex_enter(&pmportinfo->pmport_mutex);
7099 			sata_update_pmport_info(sata_hba_inst, sata_device);
7100 			mutex_exit(&pmportinfo->pmport_mutex);
7101 			mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7102 		} else {
7103 			sata_update_port_info(sata_hba_inst, sata_device);
7104 		}
7105 
7106 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
7107 		*rval = TRAN_FATAL_ERROR;
7108 		break;
7109 
7110 	case SATA_TRAN_CMD_UNSUPPORTED:
7111 		/*
7112 		 * Command rejected by HBA as unsupported. It was HBA driver
7113 		 * that rejected the command, command was not sent to
7114 		 * an attached device.
7115 		 */
7116 		if ((sdinfo != NULL) &&
7117 		    (sdinfo->satadrv_state & SATA_DSTATE_RESET))
7118 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7119 			    "sat_hba_start: cmd 0x%2x rejected "
7120 			    "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd);
7121 
7122 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7123 		(void) sata_txlt_invalid_command(spx);
7124 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7125 
7126 		*rval = TRAN_ACCEPT;
7127 		break;
7128 
7129 	case SATA_TRAN_BUSY:
7130 		/*
7131 		 * Command rejected by HBA because other operation prevents
7132 		 * accepting the packet, or device is in RESET condition.
7133 		 */
7134 		if (sdinfo != NULL) {
7135 			sdinfo->satadrv_state =
7136 			    spx->txlt_sata_pkt->satapkt_device.satadev_state;
7137 
7138 			if (sdinfo->satadrv_state & SATA_DSTATE_RESET) {
7139 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7140 				    "sata_hba_start: cmd 0x%2x rejected "
7141 				    "because of device reset condition\n",
7142 				    cmd);
7143 			} else {
7144 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7145 				    "sata_hba_start: cmd 0x%2x rejected "
7146 				    "with SATA_TRAN_BUSY status\n",
7147 				    cmd);
7148 			}
7149 		}
7150 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
7151 		*rval = TRAN_BUSY;
7152 		break;
7153 
7154 	default:
7155 		/* Unrecognized HBA response */
7156 		SATA_LOG_D((sata_hba_inst, CE_WARN,
7157 		    "sata_hba_start: unrecognized HBA response "
7158 		    "to cmd : 0x%2x resp 0x%x", cmd, rval));
7159 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
7160 		*rval = TRAN_FATAL_ERROR;
7161 		break;
7162 	}
7163 
7164 	/*
7165 	 * If we got here, the packet was rejected.
7166 	 * Check if we need to remember reset state clearing request
7167 	 */
7168 	if (cmd_flags.sata_clear_dev_reset) {
7169 		/*
7170 		 * Check if device is still configured - it may have
7171 		 * disapeared from the configuration
7172 		 */
7173 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
7174 		if (sdinfo != NULL) {
7175 			/*
7176 			 * Restore the flag that requests clearing of
7177 			 * the device reset state,
7178 			 * so the next sata packet may carry it to HBA.
7179 			 */
7180 			if (sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT ||
7181 			    sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT) {
7182 				pminfo->pmult_event_flags |=
7183 				    SATA_EVNT_CLEAR_DEVICE_RESET;
7184 			} else {
7185 				sdinfo->satadrv_event_flags |=
7186 				    SATA_EVNT_CLEAR_DEVICE_RESET;
7187 			}
7188 		}
7189 	}
7190 	return (-1);
7191 }
7192 
7193 /*
7194  * Scsi response setup for invalid LBA
7195  *
7196  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
7197  */
7198 static int
7199 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx)
7200 {
7201 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7202 	struct scsi_extended_sense *sense;
7203 
7204 	scsipkt->pkt_reason = CMD_CMPLT;
7205 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7206 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7207 	*scsipkt->pkt_scbp = STATUS_CHECK;
7208 
7209 	*scsipkt->pkt_scbp = STATUS_CHECK;
7210 	sense = sata_arq_sense(spx);
7211 	sense->es_key = KEY_ILLEGAL_REQUEST;
7212 	sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7213 
7214 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7215 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7216 
7217 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7218 	    scsipkt->pkt_comp != NULL) {
7219 		/* scsi callback required */
7220 		if (servicing_interrupt()) {
7221 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7222 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7223 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
7224 				return (TRAN_BUSY);
7225 			}
7226 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7227 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7228 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7229 			/* Scheduling the callback failed */
7230 			return (TRAN_BUSY);
7231 		}
7232 	}
7233 	return (TRAN_ACCEPT);
7234 }
7235 
7236 
7237 /*
7238  * Analyze device status and error registers and translate them into
7239  * appropriate scsi sense codes.
7240  * NOTE: non-packet commands only for now
7241  */
7242 static void
7243 sata_decode_device_error(sata_pkt_txlate_t *spx,
7244     struct scsi_extended_sense *sense)
7245 {
7246 	uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg;
7247 
7248 	ASSERT(sense != NULL);
7249 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
7250 	    SATA_STATUS_ERR);
7251 
7252 
7253 	if (err_reg & SATA_ERROR_ICRC) {
7254 		sense->es_key = KEY_ABORTED_COMMAND;
7255 		sense->es_add_code = 0x08; /* Communication failure */
7256 		return;
7257 	}
7258 
7259 	if (err_reg & SATA_ERROR_UNC) {
7260 		sense->es_key = KEY_MEDIUM_ERROR;
7261 		/* Information bytes (LBA) need to be set by a caller */
7262 		return;
7263 	}
7264 
7265 	/* ADD HERE: MC error bit handling for ATAPI CD/DVD */
7266 	if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) {
7267 		sense->es_key = KEY_UNIT_ATTENTION;
7268 		sense->es_add_code = 0x3a; /* No media present */
7269 		return;
7270 	}
7271 
7272 	if (err_reg & SATA_ERROR_IDNF) {
7273 		if (err_reg & SATA_ERROR_ABORT) {
7274 			sense->es_key = KEY_ABORTED_COMMAND;
7275 		} else {
7276 			sense->es_key = KEY_ILLEGAL_REQUEST;
7277 			sense->es_add_code = 0x21; /* LBA out of range */
7278 		}
7279 		return;
7280 	}
7281 
7282 	if (err_reg & SATA_ERROR_ABORT) {
7283 		ASSERT(spx->txlt_sata_pkt != NULL);
7284 		sense->es_key = KEY_ABORTED_COMMAND;
7285 		return;
7286 	}
7287 }
7288 
7289 /*
7290  * Extract error LBA from sata_pkt.satapkt_cmd register fields
7291  */
7292 static void
7293 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba)
7294 {
7295 	sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd;
7296 
7297 	*lba = 0;
7298 	if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) {
7299 		*lba = sata_cmd->satacmd_lba_high_msb;
7300 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb;
7301 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb;
7302 	} else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) {
7303 		*lba = sata_cmd->satacmd_device_reg & 0xf;
7304 	}
7305 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb;
7306 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb;
7307 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb;
7308 }
7309 
7310 /*
7311  * This is fixed sense format - if LBA exceeds the info field size,
7312  * no valid info will be returned (valid bit in extended sense will
7313  * be set to 0).
7314  */
7315 static struct scsi_extended_sense *
7316 sata_arq_sense(sata_pkt_txlate_t *spx)
7317 {
7318 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7319 	struct scsi_arq_status *arqs;
7320 	struct scsi_extended_sense *sense;
7321 
7322 	/* Fill ARQ sense data */
7323 	scsipkt->pkt_state |= STATE_ARQ_DONE;
7324 	arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp;
7325 	*(uchar_t *)&arqs->sts_status = STATUS_CHECK;
7326 	*(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD;
7327 	arqs->sts_rqpkt_reason = CMD_CMPLT;
7328 	arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7329 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
7330 	arqs->sts_rqpkt_resid = 0;
7331 	sense = &arqs->sts_sensedata;
7332 	bzero(sense, sizeof (struct scsi_extended_sense));
7333 	sata_fixed_sense_data_preset(sense);
7334 	return (sense);
7335 }
7336 
7337 /*
7338  * ATA Pass Through support
7339  * Sets flags indicating that an invalid value was found in some
7340  * field in the command.  It could be something illegal according to
7341  * the SAT-2 spec or it could be a feature that is not (yet?)
7342  * supported.
7343  */
7344 static int
7345 sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *spx)
7346 {
7347 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7348 	struct scsi_extended_sense *sense = sata_arq_sense(spx);
7349 
7350 	scsipkt->pkt_reason = CMD_CMPLT;
7351 	*scsipkt->pkt_scbp = STATUS_CHECK;
7352 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7353 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7354 
7355 	sense = sata_arq_sense(spx);
7356 	sense->es_key = KEY_ILLEGAL_REQUEST;
7357 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
7358 
7359 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7360 	    scsipkt->pkt_comp != NULL) {
7361 		/* scsi callback required */
7362 		if (servicing_interrupt()) {
7363 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7364 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7365 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
7366 				return (TRAN_BUSY);
7367 			}
7368 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7369 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7370 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7371 			/* Scheduling the callback failed */
7372 			return (TRAN_BUSY);
7373 		}
7374 	}
7375 
7376 	return (TRAN_ACCEPT);
7377 }
7378 
7379 /*
7380  * The UNMAP command considers it not to be an error if the parameter length
7381  * or block descriptor length is 0.  For this case, there is nothing for TRIM
7382  * to do so just complete the command.
7383  */
7384 static int
7385 sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *spx)
7386 {
7387 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7388 
7389 	scsipkt->pkt_reason = CMD_CMPLT;
7390 	*scsipkt->pkt_scbp = STATUS_GOOD;
7391 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7392 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7393 
7394 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7395 	    scsipkt->pkt_comp != NULL) {
7396 		/* scsi callback required */
7397 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7398 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7399 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7400 			/* Scheduling the callback failed */
7401 			return (TRAN_BUSY);
7402 		}
7403 	}
7404 
7405 	return (TRAN_ACCEPT);
7406 }
7407 
7408 /*
7409  * Emulated SATA Read/Write command completion for zero-length requests.
7410  * This request always succedes, so in synchronous mode it always returns
7411  * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the
7412  * callback cannot be scheduled.
7413  */
7414 static int
7415 sata_emul_rw_completion(sata_pkt_txlate_t *spx)
7416 {
7417 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7418 
7419 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7420 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7421 	scsipkt->pkt_reason = CMD_CMPLT;
7422 	*scsipkt->pkt_scbp = STATUS_GOOD;
7423 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
7424 		/* scsi callback required - have to schedule it */
7425 		if (servicing_interrupt()) {
7426 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7427 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7428 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) == NULL) {
7429 				return (TRAN_BUSY);
7430 			}
7431 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7432 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7433 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == NULL) {
7434 			/* Scheduling the callback failed */
7435 			return (TRAN_BUSY);
7436 		}
7437 	}
7438 	return (TRAN_ACCEPT);
7439 }
7440 
7441 
7442 /*
7443  * Translate completion status of SATA read/write commands into scsi response.
7444  * pkt completion_reason is checked to determine the completion status.
7445  * Do scsi callback if necessary.
7446  *
7447  * Note: this function may be called also for synchronously executed
7448  * commands.
7449  * This function may be used only if scsi_pkt is non-NULL.
7450  */
7451 static void
7452 sata_txlt_rw_completion(sata_pkt_t *sata_pkt)
7453 {
7454 	sata_pkt_txlate_t *spx =
7455 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7456 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7457 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7458 	struct scsi_extended_sense *sense;
7459 	uint64_t lba;
7460 	struct buf *bp;
7461 	int rval;
7462 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7463 		/* Normal completion */
7464 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7465 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7466 		scsipkt->pkt_reason = CMD_CMPLT;
7467 		*scsipkt->pkt_scbp = STATUS_GOOD;
7468 		if (spx->txlt_tmp_buf != NULL) {
7469 			/* Temporary buffer was used */
7470 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7471 			if (bp->b_flags & B_READ) {
7472 				rval = ddi_dma_sync(
7473 				    spx->txlt_buf_dma_handle, 0, 0,
7474 				    DDI_DMA_SYNC_FORCPU);
7475 				ASSERT(rval == DDI_SUCCESS);
7476 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7477 				    bp->b_bcount);
7478 			}
7479 		}
7480 	} else {
7481 		/*
7482 		 * Something went wrong - analyze return
7483 		 */
7484 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7485 		    STATE_SENT_CMD | STATE_GOT_STATUS;
7486 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7487 		*scsipkt->pkt_scbp = STATUS_CHECK;
7488 		sense = sata_arq_sense(spx);
7489 		ASSERT(sense != NULL);
7490 
7491 		/*
7492 		 * SATA_PKT_DEV_ERROR is the only case where we may be able to
7493 		 * extract from device registers the failing LBA.
7494 		 */
7495 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7496 			if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) &&
7497 			    (scmd->satacmd_lba_mid_msb != 0 ||
7498 			    scmd->satacmd_lba_high_msb != 0)) {
7499 				/*
7500 				 * We have problem reporting this cmd LBA
7501 				 * in fixed sense data format, because of
7502 				 * the size of the scsi LBA fields.
7503 				 */
7504 				sense->es_valid = 0;
7505 			} else {
7506 				sata_extract_error_lba(spx, &lba);
7507 				sense->es_info_1 = (lba & 0xFF000000) >> 24;
7508 				sense->es_info_2 = (lba & 0xFF0000) >> 16;
7509 				sense->es_info_3 = (lba & 0xFF00) >> 8;
7510 				sense->es_info_4 = lba & 0xFF;
7511 			}
7512 		} else {
7513 			/* Invalid extended sense info */
7514 			sense->es_valid = 0;
7515 		}
7516 
7517 		switch (sata_pkt->satapkt_reason) {
7518 		case SATA_PKT_PORT_ERROR:
7519 			/* We may want to handle DEV GONE state as well */
7520 			/*
7521 			 * We have no device data. Assume no data transfered.
7522 			 */
7523 			sense->es_key = KEY_HARDWARE_ERROR;
7524 			break;
7525 
7526 		case SATA_PKT_DEV_ERROR:
7527 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
7528 			    SATA_STATUS_ERR) {
7529 				/*
7530 				 * determine dev error reason from error
7531 				 * reg content
7532 				 */
7533 				sata_decode_device_error(spx, sense);
7534 				if (sense->es_key == KEY_MEDIUM_ERROR) {
7535 					switch (scmd->satacmd_cmd_reg) {
7536 					case SATAC_READ_DMA:
7537 					case SATAC_READ_DMA_EXT:
7538 					case SATAC_READ_DMA_QUEUED:
7539 					case SATAC_READ_DMA_QUEUED_EXT:
7540 					case SATAC_READ_FPDMA_QUEUED:
7541 						/* Unrecovered read error */
7542 						sense->es_add_code =
7543 						    SD_SCSI_ASC_UNREC_READ_ERR;
7544 						break;
7545 					case SATAC_WRITE_DMA:
7546 					case SATAC_WRITE_DMA_EXT:
7547 					case SATAC_WRITE_DMA_QUEUED:
7548 					case SATAC_WRITE_DMA_QUEUED_EXT:
7549 					case SATAC_WRITE_FPDMA_QUEUED:
7550 						/* Write error */
7551 						sense->es_add_code =
7552 						    SD_SCSI_ASC_WRITE_ERR;
7553 						break;
7554 					default:
7555 						/* Internal error */
7556 						SATA_LOG_D((
7557 						    spx->txlt_sata_hba_inst,
7558 						    CE_WARN,
7559 						    "sata_txlt_rw_completion :"
7560 						    "internal error - invalid "
7561 						    "command 0x%2x",
7562 						    scmd->satacmd_cmd_reg));
7563 						break;
7564 					}
7565 				}
7566 				break;
7567 			}
7568 			/* No extended sense key - no info available */
7569 			scsipkt->pkt_reason = CMD_INCOMPLETE;
7570 			break;
7571 
7572 		case SATA_PKT_TIMEOUT:
7573 			scsipkt->pkt_reason = CMD_TIMEOUT;
7574 			scsipkt->pkt_statistics |=
7575 			    STAT_TIMEOUT | STAT_DEV_RESET;
7576 			sense->es_key = KEY_ABORTED_COMMAND;
7577 			break;
7578 
7579 		case SATA_PKT_ABORTED:
7580 			scsipkt->pkt_reason = CMD_ABORTED;
7581 			scsipkt->pkt_statistics |= STAT_ABORTED;
7582 			sense->es_key = KEY_ABORTED_COMMAND;
7583 			break;
7584 
7585 		case SATA_PKT_RESET:
7586 			scsipkt->pkt_reason = CMD_RESET;
7587 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
7588 			sense->es_key = KEY_ABORTED_COMMAND;
7589 			break;
7590 
7591 		default:
7592 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7593 			    "sata_txlt_rw_completion: "
7594 			    "invalid packet completion reason"));
7595 			scsipkt->pkt_reason = CMD_TRAN_ERR;
7596 			break;
7597 		}
7598 	}
7599 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7600 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7601 
7602 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7603 		/* scsi callback required */
7604 		scsi_hba_pkt_comp(scsipkt);
7605 }
7606 
7607 
7608 /*
7609  * Translate completion status of non-data commands (i.e. commands returning
7610  * no data).
7611  * pkt completion_reason is checked to determine the completion status.
7612  * Do scsi callback if necessary (FLAG_NOINTR == 0)
7613  *
7614  * Note: this function may be called also for synchronously executed
7615  * commands.
7616  * This function may be used only if scsi_pkt is non-NULL.
7617  */
7618 
7619 static	void
7620 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt)
7621 {
7622 	sata_pkt_txlate_t *spx =
7623 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7624 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7625 
7626 	sata_set_arq_data(sata_pkt);
7627 
7628 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7629 		/* scsi callback required */
7630 		scsi_hba_pkt_comp(scsipkt);
7631 }
7632 
7633 /*
7634  * Completion handler for ATA Pass Through command
7635  */
7636 static void
7637 sata_txlt_apt_completion(sata_pkt_t *sata_pkt)
7638 {
7639 	sata_pkt_txlate_t *spx =
7640 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7641 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7642 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7643 	struct buf *bp;
7644 	uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0;
7645 
7646 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7647 		/* Normal completion */
7648 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7649 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7650 		scsipkt->pkt_reason = CMD_CMPLT;
7651 		*scsipkt->pkt_scbp = STATUS_GOOD;
7652 
7653 		/*
7654 		 * If the command has CK_COND set
7655 		 */
7656 		if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) {
7657 			*scsipkt->pkt_scbp = STATUS_CHECK;
7658 			sata_fill_ata_return_desc(sata_pkt,
7659 			    KEY_RECOVERABLE_ERROR,
7660 			    SD_SCSI_ASC_APT_INFO_AVAIL, 0x1d);
7661 		}
7662 
7663 		if (spx->txlt_tmp_buf != NULL) {
7664 			/* Temporary buffer was used */
7665 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7666 			if (bp->b_flags & B_READ) {
7667 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7668 				    bp->b_bcount);
7669 			}
7670 		}
7671 	} else {
7672 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7673 		    STATE_SENT_CMD | STATE_GOT_STATUS;
7674 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7675 		*scsipkt->pkt_scbp = STATUS_CHECK;
7676 
7677 		/*
7678 		 * If DF or ERR was set, the HBA should have copied out the
7679 		 * status and error registers to the satacmd structure.
7680 		 */
7681 		if (scmd->satacmd_status_reg & SATA_STATUS_DF) {
7682 			sense_key = KEY_HARDWARE_ERROR;
7683 			addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE;
7684 			addl_sense_qual = 0;
7685 		} else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
7686 			if (scmd->satacmd_error_reg & SATA_ERROR_NM) {
7687 				sense_key = KEY_NOT_READY;
7688 				addl_sense_code =
7689 				    SD_SCSI_ASC_MEDIUM_NOT_PRESENT;
7690 				addl_sense_qual = 0;
7691 			} else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) {
7692 				sense_key = KEY_MEDIUM_ERROR;
7693 				addl_sense_code = SD_SCSI_ASC_UNREC_READ_ERR;
7694 				addl_sense_qual = 0;
7695 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) {
7696 				sense_key = KEY_DATA_PROTECT;
7697 				addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED;
7698 				addl_sense_qual = 0;
7699 			} else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) {
7700 				sense_key = KEY_ILLEGAL_REQUEST;
7701 				addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7702 				addl_sense_qual = 0;
7703 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
7704 				sense_key = KEY_ABORTED_COMMAND;
7705 				addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE;
7706 				addl_sense_qual = 0;
7707 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MC) {
7708 				sense_key = KEY_UNIT_ATTENTION;
7709 				addl_sense_code =
7710 				    SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED;
7711 				addl_sense_qual = 0;
7712 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) {
7713 				sense_key = KEY_UNIT_ATTENTION;
7714 				addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ;
7715 				addl_sense_qual = 0;
7716 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) {
7717 				sense_key = KEY_ABORTED_COMMAND;
7718 				addl_sense_code =
7719 				    SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR;
7720 				addl_sense_qual = 0;
7721 			}
7722 		}
7723 
7724 		sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code,
7725 		    addl_sense_qual);
7726 	}
7727 
7728 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7729 		/* scsi callback required */
7730 		scsi_hba_pkt_comp(scsipkt);
7731 }
7732 
7733 /*
7734  * Completion handler for unmap translation command
7735  */
7736 static void
7737 sata_txlt_unmap_completion(sata_pkt_t *sata_pkt)
7738 {
7739 	sata_pkt_txlate_t *spx =
7740 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7741 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7742 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7743 	struct buf *bp;
7744 	uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0;
7745 
7746 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7747 		/* Normal completion */
7748 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7749 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7750 		scsipkt->pkt_reason = CMD_CMPLT;
7751 		*scsipkt->pkt_scbp = STATUS_GOOD;
7752 
7753 		if (spx->txlt_tmp_buf != NULL) {
7754 			/* Temporary buffer was used */
7755 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7756 			if (bp->b_flags & B_READ) {
7757 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7758 				    bp->b_bcount);
7759 			}
7760 		}
7761 	} else {
7762 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7763 		    STATE_SENT_CMD | STATE_GOT_STATUS;
7764 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7765 		*scsipkt->pkt_scbp = STATUS_CHECK;
7766 
7767 		/*
7768 		 * If DF or ERR was set, the HBA should have copied out the
7769 		 * status and error registers to the satacmd structure.
7770 		 */
7771 		if (scmd->satacmd_status_reg & SATA_STATUS_DF) {
7772 			sense_key = KEY_HARDWARE_ERROR;
7773 			addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE;
7774 			addl_sense_qual = 0;
7775 		} else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
7776 			if (scmd->satacmd_error_reg & SATA_ERROR_NM) {
7777 				sense_key = KEY_NOT_READY;
7778 				addl_sense_code =
7779 				    SD_SCSI_ASC_MEDIUM_NOT_PRESENT;
7780 				addl_sense_qual = 0;
7781 			} else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) {
7782 				sense_key = KEY_MEDIUM_ERROR;
7783 				addl_sense_code = SD_SCSI_ASC_WRITE_ERR;
7784 				addl_sense_qual = 0;
7785 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) {
7786 				sense_key = KEY_DATA_PROTECT;
7787 				addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED;
7788 				addl_sense_qual = 0;
7789 			} else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) {
7790 				sense_key = KEY_ILLEGAL_REQUEST;
7791 				addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7792 				addl_sense_qual = 0;
7793 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
7794 				sense_key = KEY_ABORTED_COMMAND;
7795 				addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE;
7796 				addl_sense_qual = 0;
7797 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MC) {
7798 				sense_key = KEY_UNIT_ATTENTION;
7799 				addl_sense_code =
7800 				    SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED;
7801 				addl_sense_qual = 0;
7802 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) {
7803 				sense_key = KEY_UNIT_ATTENTION;
7804 				addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ;
7805 				addl_sense_qual = 0;
7806 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) {
7807 				sense_key = KEY_ABORTED_COMMAND;
7808 				addl_sense_code =
7809 				    SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR;
7810 				addl_sense_qual = 0;
7811 			}
7812 		}
7813 
7814 		sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code,
7815 		    addl_sense_qual);
7816 	}
7817 
7818 	sata_free_local_buffer(spx);
7819 
7820 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7821 		/* scsi callback required */
7822 		scsi_hba_pkt_comp(scsipkt);
7823 }
7824 
7825 /*
7826  *
7827  */
7828 static void
7829 sata_fill_ata_return_desc(sata_pkt_t *sata_pkt, uint8_t sense_key,
7830     uint8_t addl_sense_code, uint8_t addl_sense_qual)
7831 {
7832 	sata_pkt_txlate_t *spx =
7833 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7834 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7835 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7836 	struct sata_apt_sense_data *apt_sd =
7837 	    (struct sata_apt_sense_data *)scsipkt->pkt_scbp;
7838 	struct scsi_descr_sense_hdr *sds = &(apt_sd->apt_sd_hdr);
7839 	struct scsi_ata_status_ret_sense_descr *ata_ret_desc =
7840 	    &(apt_sd->apt_sd_sense);
7841 	int extend = 0;
7842 
7843 	if ((scsipkt->pkt_cdbp[0] == SPC3_CMD_ATA_COMMAND_PASS_THROUGH16) &&
7844 	    (scsipkt->pkt_cdbp[2] & SATL_APT_BM_EXTEND))
7845 		extend = 1;
7846 
7847 	scsipkt->pkt_state |= STATE_ARQ_DONE;
7848 
7849 	/* update the residual count */
7850 	*(uchar_t *)&apt_sd->apt_status = STATUS_CHECK;
7851 	*(uchar_t *)&apt_sd->apt_rqpkt_status = STATUS_GOOD;
7852 	apt_sd->apt_rqpkt_reason = CMD_CMPLT;
7853 	apt_sd->apt_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7854 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
7855 	apt_sd->apt_rqpkt_resid = scsipkt->pkt_scblen -
7856 	    sizeof (struct sata_apt_sense_data);
7857 
7858 	/*
7859 	 * Fill in the Descriptor sense header
7860 	 */
7861 	bzero(sds, sizeof (struct scsi_descr_sense_hdr));
7862 	sds->ds_code = CODE_FMT_DESCR_CURRENT;
7863 	sds->ds_class = CLASS_EXTENDED_SENSE;
7864 	sds->ds_key = sense_key & 0xf;
7865 	sds->ds_add_code = addl_sense_code;
7866 	sds->ds_qual_code = addl_sense_qual;
7867 	sds->ds_addl_sense_length =
7868 	    sizeof (struct scsi_ata_status_ret_sense_descr);
7869 
7870 	/*
7871 	 * Fill in the ATA Return descriptor sense data
7872 	 */
7873 	bzero(ata_ret_desc, sizeof (struct scsi_ata_status_ret_sense_descr));
7874 	ata_ret_desc->ars_descr_type = DESCR_ATA_STATUS_RETURN;
7875 	ata_ret_desc->ars_addl_length = 0xc;
7876 	ata_ret_desc->ars_error = scmd->satacmd_error_reg;
7877 	ata_ret_desc->ars_sec_count_lsb = scmd->satacmd_sec_count_lsb;
7878 	ata_ret_desc->ars_lba_low_lsb = scmd->satacmd_lba_low_lsb;
7879 	ata_ret_desc->ars_lba_mid_lsb = scmd->satacmd_lba_mid_lsb;
7880 	ata_ret_desc->ars_lba_high_lsb = scmd->satacmd_lba_high_lsb;
7881 	ata_ret_desc->ars_device = scmd->satacmd_device_reg;
7882 	ata_ret_desc->ars_status = scmd->satacmd_status_reg;
7883 
7884 	if (extend == 1) {
7885 		ata_ret_desc->ars_extend = 1;
7886 		ata_ret_desc->ars_sec_count_msb = scmd->satacmd_sec_count_msb;
7887 		ata_ret_desc->ars_lba_low_msb = scmd->satacmd_lba_low_msb;
7888 		ata_ret_desc->ars_lba_mid_msb = scmd->satacmd_lba_mid_msb;
7889 		ata_ret_desc->ars_lba_high_msb = scmd->satacmd_lba_high_msb;
7890 	} else {
7891 		ata_ret_desc->ars_extend = 0;
7892 		ata_ret_desc->ars_sec_count_msb = 0;
7893 		ata_ret_desc->ars_lba_low_msb = 0;
7894 		ata_ret_desc->ars_lba_mid_msb = 0;
7895 		ata_ret_desc->ars_lba_high_msb = 0;
7896 	}
7897 }
7898 
7899 static	void
7900 sata_set_arq_data(sata_pkt_t *sata_pkt)
7901 {
7902 	sata_pkt_txlate_t *spx =
7903 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7904 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7905 	struct scsi_extended_sense *sense;
7906 
7907 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7908 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7909 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7910 		/* Normal completion */
7911 		scsipkt->pkt_reason = CMD_CMPLT;
7912 		*scsipkt->pkt_scbp = STATUS_GOOD;
7913 	} else {
7914 		/* Something went wrong */
7915 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7916 		*scsipkt->pkt_scbp = STATUS_CHECK;
7917 		sense = sata_arq_sense(spx);
7918 		switch (sata_pkt->satapkt_reason) {
7919 		case SATA_PKT_PORT_ERROR:
7920 			/*
7921 			 * We have no device data. Assume no data transfered.
7922 			 */
7923 			sense->es_key = KEY_HARDWARE_ERROR;
7924 			break;
7925 
7926 		case SATA_PKT_DEV_ERROR:
7927 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
7928 			    SATA_STATUS_ERR) {
7929 				/*
7930 				 * determine dev error reason from error
7931 				 * reg content
7932 				 */
7933 				sata_decode_device_error(spx, sense);
7934 				break;
7935 			}
7936 			/* No extended sense key - no info available */
7937 			break;
7938 
7939 		case SATA_PKT_TIMEOUT:
7940 			scsipkt->pkt_reason = CMD_TIMEOUT;
7941 			scsipkt->pkt_statistics |=
7942 			    STAT_TIMEOUT | STAT_DEV_RESET;
7943 			/* No extended sense key ? */
7944 			break;
7945 
7946 		case SATA_PKT_ABORTED:
7947 			scsipkt->pkt_reason = CMD_ABORTED;
7948 			scsipkt->pkt_statistics |= STAT_ABORTED;
7949 			/* No extended sense key ? */
7950 			break;
7951 
7952 		case SATA_PKT_RESET:
7953 			/* pkt aborted by an explicit reset from a host */
7954 			scsipkt->pkt_reason = CMD_RESET;
7955 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
7956 			break;
7957 
7958 		default:
7959 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7960 			    "sata_txlt_nodata_cmd_completion: "
7961 			    "invalid packet completion reason %d",
7962 			    sata_pkt->satapkt_reason));
7963 			scsipkt->pkt_reason = CMD_TRAN_ERR;
7964 			break;
7965 		}
7966 
7967 	}
7968 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7969 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7970 }
7971 
7972 
7973 /*
7974  * Build Mode sense R/W recovery page
7975  * NOT IMPLEMENTED
7976  */
7977 
7978 static int
7979 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
7980 {
7981 #ifndef __lock_lint
7982 	_NOTE(ARGUNUSED(sdinfo))
7983 	_NOTE(ARGUNUSED(pcntrl))
7984 	_NOTE(ARGUNUSED(buf))
7985 #endif
7986 	return (0);
7987 }
7988 
7989 /*
7990  * Build Mode sense caching page  -  scsi-3 implementation.
7991  * Page length distinguishes previous format from scsi-3 format.
7992  * buf must have space for 0x12 bytes.
7993  * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable.
7994  *
7995  */
7996 static int
7997 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
7998 {
7999 	struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf;
8000 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8001 
8002 	/*
8003 	 * Most of the fields are set to 0, being not supported and/or disabled
8004 	 */
8005 	bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3);
8006 
8007 	/* Saved paramters not supported */
8008 	if (pcntrl == 3)
8009 		return (0);
8010 	if (pcntrl == 0 || pcntrl == 2) {
8011 		/*
8012 		 * For now treat current and default parameters as same
8013 		 * That may have to change, if target driver will complain
8014 		 */
8015 		page->mode_page.code = MODEPAGE_CACHING;	/* PS = 0 */
8016 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
8017 
8018 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id) &&
8019 		    !SATA_READ_AHEAD_ENABLED(*sata_id)) {
8020 			page->dra = 1;		/* Read Ahead disabled */
8021 			page->rcd = 1;		/* Read Cache disabled */
8022 		}
8023 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id) &&
8024 		    SATA_WRITE_CACHE_ENABLED(*sata_id))
8025 			page->wce = 1;		/* Write Cache enabled */
8026 	} else {
8027 		/* Changeable parameters */
8028 		page->mode_page.code = MODEPAGE_CACHING;
8029 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
8030 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
8031 			page->dra = 1;
8032 			page->rcd = 1;
8033 		}
8034 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id))
8035 			page->wce = 1;
8036 	}
8037 	return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
8038 	    sizeof (struct mode_page));
8039 }
8040 
8041 /*
8042  * Build Mode sense exception cntrl page
8043  */
8044 static int
8045 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8046 {
8047 	struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf;
8048 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8049 
8050 	/*
8051 	 * Most of the fields are set to 0, being not supported and/or disabled
8052 	 */
8053 	bzero(buf, PAGELENGTH_INFO_EXCPT);
8054 
8055 	page->mode_page.code = MODEPAGE_INFO_EXCPT;
8056 	page->mode_page.length = PAGELENGTH_INFO_EXCPT;
8057 
8058 	/* Indicate that this is page is saveable */
8059 	page->mode_page.ps = 1;
8060 
8061 	/*
8062 	 * We will return the same data for default, current and saved page.
8063 	 * The only changeable bit is dexcpt and that bit is required
8064 	 * by the ATA specification to be preserved across power cycles.
8065 	 */
8066 	if (pcntrl != 1) {
8067 		page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED);
8068 		page->mrie = MRIE_ONLY_ON_REQUEST;
8069 	}
8070 	else
8071 		page->dexcpt = 1;	/* Only changeable parameter */
8072 
8073 	return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page));
8074 }
8075 
8076 
8077 static int
8078 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8079 {
8080 	struct mode_acoustic_management *page =
8081 	    (struct mode_acoustic_management *)buf;
8082 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8083 
8084 	/*
8085 	 * Most of the fields are set to 0, being not supported and/or disabled
8086 	 */
8087 	bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT);
8088 
8089 	switch (pcntrl) {
8090 	case P_CNTRL_DEFAULT:
8091 		/*  default paramters not supported */
8092 		return (0);
8093 
8094 	case P_CNTRL_CURRENT:
8095 	case P_CNTRL_SAVED:
8096 		/* Saved and current are supported and are identical */
8097 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
8098 		page->mode_page.length =
8099 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
8100 		page->mode_page.ps = 1;
8101 
8102 		/* Word 83 indicates if feature is supported */
8103 		/* If feature is not supported */
8104 		if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) {
8105 			page->acoustic_manag_enable =
8106 			    ACOUSTIC_DISABLED;
8107 		} else {
8108 			page->acoustic_manag_enable =
8109 			    ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT)
8110 			    != 0);
8111 			/* Word 94 inidicates the value */
8112 #ifdef	_LITTLE_ENDIAN
8113 			page->acoustic_manag_level =
8114 			    (uchar_t)sata_id->ai_acoustic;
8115 			page->vendor_recommended_value =
8116 			    sata_id->ai_acoustic >> 8;
8117 #else
8118 			page->acoustic_manag_level =
8119 			    sata_id->ai_acoustic >> 8;
8120 			page->vendor_recommended_value =
8121 			    (uchar_t)sata_id->ai_acoustic;
8122 #endif
8123 		}
8124 		break;
8125 
8126 	case P_CNTRL_CHANGEABLE:
8127 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
8128 		page->mode_page.length =
8129 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
8130 		page->mode_page.ps = 1;
8131 
8132 		/* Word 83 indicates if the feature is supported */
8133 		if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) {
8134 			page->acoustic_manag_enable =
8135 			    ACOUSTIC_ENABLED;
8136 			page->acoustic_manag_level = 0xff;
8137 		}
8138 		break;
8139 	}
8140 	return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8141 	    sizeof (struct mode_page));
8142 }
8143 
8144 
8145 /*
8146  * Build Mode sense power condition page.
8147  */
8148 static int
8149 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8150 {
8151 	struct mode_info_power_cond *page = (struct mode_info_power_cond *)buf;
8152 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8153 
8154 	/*
8155 	 * Most of the fields are set to 0, being not supported and/or disabled
8156 	 * power condition page length was 0x0a
8157 	 */
8158 	bzero(buf, sizeof (struct mode_info_power_cond));
8159 
8160 	if (pcntrl == P_CNTRL_DEFAULT) {
8161 		/*  default paramters not supported */
8162 		return (0);
8163 	}
8164 
8165 	page->mode_page.code = MODEPAGE_POWER_COND;
8166 	page->mode_page.length = sizeof (struct mode_info_power_cond);
8167 
8168 	if (sata_id->ai_cap & SATA_STANDBYTIMER) {
8169 		page->standby = 1;
8170 		bcopy(sdinfo->satadrv_standby_timer, page->standby_cond_timer,
8171 		    sizeof (uchar_t) * 4);
8172 	}
8173 
8174 	return (sizeof (struct mode_info_power_cond));
8175 }
8176 
8177 /*
8178  * Process mode select caching page 8 (scsi3 format only).
8179  * Read Ahead (same as read cache) and Write Cache may be turned on and off
8180  * if these features are supported by the device. If these features are not
8181  * supported, the command will be terminated with STATUS_CHECK.
8182  * This function fails only if the SET FEATURE command sent to
8183  * the device fails. The page format is not verified, assuming that the
8184  * target driver operates correctly - if parameters length is too short,
8185  * we just drop the page.
8186  * Two command may be sent if both Read Cache/Read Ahead and Write Cache
8187  * setting have to be changed.
8188  * SET FEATURE command is executed synchronously, i.e. we wait here until
8189  * it is completed, regardless of the scsi pkt directives.
8190  *
8191  * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e.
8192  * changing DRA will change RCD.
8193  *
8194  * More than one SATA command may be executed to perform operations specified
8195  * by mode select pages. The first error terminates further execution.
8196  * Operations performed successully are not backed-up in such case.
8197  *
8198  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
8199  * If operation resulted in changing device setup, dmod flag should be set to
8200  * one (1). If parameters were not changed, dmod flag should be set to 0.
8201  * Upon return, if operation required sending command to the device, the rval
8202  * should be set to the value returned by sata_hba_start. If operation
8203  * did not require device access, rval should be set to TRAN_ACCEPT.
8204  * The pagelen should be set to the length of the page.
8205  *
8206  * This function has to be called with a port mutex held.
8207  *
8208  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8209  */
8210 int
8211 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page,
8212     int parmlen, int *pagelen, int *rval, int *dmod)
8213 {
8214 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8215 	sata_drive_info_t *sdinfo;
8216 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8217 	sata_id_t *sata_id;
8218 	struct scsi_extended_sense *sense;
8219 	int wce, dra;	/* Current settings */
8220 
8221 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8222 	    &spx->txlt_sata_pkt->satapkt_device);
8223 	sata_id = &sdinfo->satadrv_id;
8224 	*dmod = 0;
8225 
8226 	/* Verify parameters length. If too short, drop it */
8227 	if ((PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
8228 	    sizeof (struct mode_page)) > parmlen) {
8229 		*scsipkt->pkt_scbp = STATUS_CHECK;
8230 		sense = sata_arq_sense(spx);
8231 		sense->es_key = KEY_ILLEGAL_REQUEST;
8232 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8233 		*pagelen = parmlen;
8234 		*rval = TRAN_ACCEPT;
8235 		return (SATA_FAILURE);
8236 	}
8237 
8238 	*pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page);
8239 
8240 	/* Current setting of Read Ahead (and Read Cache) */
8241 	if (SATA_READ_AHEAD_ENABLED(*sata_id))
8242 		dra = 0;	/* 0 == not disabled */
8243 	else
8244 		dra = 1;
8245 	/* Current setting of Write Cache */
8246 	if (SATA_WRITE_CACHE_ENABLED(*sata_id))
8247 		wce = 1;
8248 	else
8249 		wce = 0;
8250 
8251 	if (page->dra == dra && page->wce == wce && page->rcd == dra) {
8252 		/* nothing to do */
8253 		*rval = TRAN_ACCEPT;
8254 		return (SATA_SUCCESS);
8255 	}
8256 
8257 	/*
8258 	 * Need to flip some setting
8259 	 * Set-up Internal SET FEATURES command(s)
8260 	 */
8261 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8262 	scmd->satacmd_addr_type = 0;
8263 	scmd->satacmd_device_reg = 0;
8264 	scmd->satacmd_status_reg = 0;
8265 	scmd->satacmd_error_reg = 0;
8266 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
8267 	if (page->dra != dra || page->rcd != dra) {
8268 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
8269 			/* Need to flip read ahead setting */
8270 			if (dra == 0)
8271 				/* Disable read ahead / read cache */
8272 				scmd->satacmd_features_reg =
8273 				    SATAC_SF_DISABLE_READ_AHEAD;
8274 			else
8275 				/* Enable read ahead  / read cache */
8276 				scmd->satacmd_features_reg =
8277 				    SATAC_SF_ENABLE_READ_AHEAD;
8278 
8279 			/* Transfer command to HBA */
8280 			if (sata_hba_start(spx, rval) != 0)
8281 				/*
8282 				 * Pkt not accepted for execution.
8283 				 */
8284 				return (SATA_FAILURE);
8285 
8286 			*dmod = 1;
8287 
8288 			/* Now process return */
8289 			if (spx->txlt_sata_pkt->satapkt_reason !=
8290 			    SATA_PKT_COMPLETED) {
8291 				goto failure;	/* Terminate */
8292 			}
8293 		} else {
8294 			*scsipkt->pkt_scbp = STATUS_CHECK;
8295 			sense = sata_arq_sense(spx);
8296 			sense->es_key = KEY_ILLEGAL_REQUEST;
8297 			sense->es_add_code =
8298 			    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8299 			*pagelen = parmlen;
8300 			*rval = TRAN_ACCEPT;
8301 			return (SATA_FAILURE);
8302 		}
8303 	}
8304 
8305 	/* Note that the packet is not removed, so it could be re-used */
8306 	if (page->wce != wce) {
8307 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id)) {
8308 			/* Need to flip Write Cache setting */
8309 			if (page->wce == 1)
8310 				/* Enable write cache */
8311 				scmd->satacmd_features_reg =
8312 				    SATAC_SF_ENABLE_WRITE_CACHE;
8313 			else
8314 				/* Disable write cache */
8315 				scmd->satacmd_features_reg =
8316 				    SATAC_SF_DISABLE_WRITE_CACHE;
8317 
8318 			/* Transfer command to HBA */
8319 			if (sata_hba_start(spx, rval) != 0)
8320 				/*
8321 				 * Pkt not accepted for execution.
8322 				 */
8323 				return (SATA_FAILURE);
8324 
8325 			*dmod = 1;
8326 
8327 			/* Now process return */
8328 			if (spx->txlt_sata_pkt->satapkt_reason !=
8329 			    SATA_PKT_COMPLETED) {
8330 				goto failure;
8331 			}
8332 		} else {
8333 			*scsipkt->pkt_scbp = STATUS_CHECK;
8334 			sense = sata_arq_sense(spx);
8335 			sense->es_key = KEY_ILLEGAL_REQUEST;
8336 			sense->es_add_code =
8337 			    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8338 			*pagelen = parmlen;
8339 			*rval = TRAN_ACCEPT;
8340 			return (SATA_FAILURE);
8341 		}
8342 	}
8343 	return (SATA_SUCCESS);
8344 
8345 failure:
8346 	sata_xlate_errors(spx);
8347 
8348 	return (SATA_FAILURE);
8349 }
8350 
8351 /*
8352  * Process mode select informational exceptions control page 0x1c
8353  *
8354  * The only changeable bit is dexcpt (disable exceptions).
8355  * MRIE (method of reporting informational exceptions) must be
8356  * "only on request".
8357  * This page applies to informational exceptions that report
8358  * additional sense codes with the ADDITIONAL SENSE CODE field set to 5Dh
8359  * (e.g.,FAILURE PREDICTION THRESHOLD EXCEEDED) or 0Bh (e.g., WARNING_).
8360  * Informational exception conditions occur as the result of background scan
8361  * errors, background self-test errors, or vendor specific events within a
8362  * logical unit. An informational exception condition may occur asynchronous
8363  * to any commands.
8364  *
8365  * Returns: SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
8366  * If operation resulted in changing device setup, dmod flag should be set to
8367  * one (1). If parameters were not changed, dmod flag should be set to 0.
8368  * Upon return, if operation required sending command to the device, the rval
8369  * should be set to the value returned by sata_hba_start. If operation
8370  * did not require device access, rval should be set to TRAN_ACCEPT.
8371  * The pagelen should be set to the length of the page.
8372  *
8373  * This function has to be called with a port mutex held.
8374  *
8375  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8376  *
8377  * Cannot be called in the interrupt context.
8378  */
8379 static	int
8380 sata_mode_select_page_1c(
8381 	sata_pkt_txlate_t *spx,
8382 	struct mode_info_excpt_page *page,
8383 	int parmlen,
8384 	int *pagelen,
8385 	int *rval,
8386 	int *dmod)
8387 {
8388 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8389 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8390 	sata_drive_info_t *sdinfo;
8391 	sata_id_t *sata_id;
8392 	struct scsi_extended_sense *sense;
8393 
8394 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8395 	    &spx->txlt_sata_pkt->satapkt_device);
8396 	sata_id = &sdinfo->satadrv_id;
8397 
8398 	*dmod = 0;
8399 
8400 	/* Verify parameters length. If too short, drop it */
8401 	if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) > parmlen) ||
8402 	    page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) {
8403 		*scsipkt->pkt_scbp = STATUS_CHECK;
8404 		sense = sata_arq_sense(spx);
8405 		sense->es_key = KEY_ILLEGAL_REQUEST;
8406 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8407 		*pagelen = parmlen;
8408 		*rval = TRAN_ACCEPT;
8409 		return (SATA_FAILURE);
8410 	}
8411 
8412 	*pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page);
8413 
8414 	if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
8415 		*scsipkt->pkt_scbp = STATUS_CHECK;
8416 		sense = sata_arq_sense(spx);
8417 		sense->es_key = KEY_ILLEGAL_REQUEST;
8418 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
8419 		*pagelen = parmlen;
8420 		*rval = TRAN_ACCEPT;
8421 		return (SATA_FAILURE);
8422 	}
8423 
8424 	/* If already in the state requested, we are done */
8425 	if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
8426 		/* nothing to do */
8427 		*rval = TRAN_ACCEPT;
8428 		return (SATA_SUCCESS);
8429 	}
8430 
8431 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8432 
8433 	/* Build SMART_ENABLE or SMART_DISABLE command */
8434 	scmd->satacmd_addr_type = 0;		/* N/A */
8435 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
8436 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
8437 	scmd->satacmd_features_reg = page->dexcpt ?
8438 	    SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS;
8439 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
8440 	scmd->satacmd_cmd_reg = SATAC_SMART;
8441 
8442 	/* Transfer command to HBA */
8443 	if (sata_hba_start(spx, rval) != 0)
8444 		/*
8445 		 * Pkt not accepted for execution.
8446 		 */
8447 		return (SATA_FAILURE);
8448 
8449 	*dmod = 1;	/* At least may have been modified */
8450 
8451 	/* Now process return */
8452 	if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED)
8453 		return (SATA_SUCCESS);
8454 
8455 	/* Packet did not complete successfully */
8456 	sata_xlate_errors(spx);
8457 
8458 	return (SATA_FAILURE);
8459 }
8460 
8461 /*
8462  * Process mode select acoustic management control page 0x30
8463  *
8464  *
8465  * This function has to be called with a port mutex held.
8466  *
8467  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8468  *
8469  * Cannot be called in the interrupt context.
8470  */
8471 int
8472 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct
8473     mode_acoustic_management *page, int parmlen, int *pagelen,
8474     int *rval, int *dmod)
8475 {
8476 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8477 	sata_drive_info_t *sdinfo;
8478 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8479 	sata_id_t *sata_id;
8480 	struct scsi_extended_sense *sense;
8481 
8482 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8483 	    &spx->txlt_sata_pkt->satapkt_device);
8484 	sata_id = &sdinfo->satadrv_id;
8485 	*dmod = 0;
8486 
8487 	/* If parmlen is too short or the feature is not supported, drop it */
8488 	if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8489 	    sizeof (struct mode_page)) > parmlen) ||
8490 	    (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) {
8491 		*scsipkt->pkt_scbp = STATUS_CHECK;
8492 		sense = sata_arq_sense(spx);
8493 		sense->es_key = KEY_ILLEGAL_REQUEST;
8494 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8495 		*pagelen = parmlen;
8496 		*rval = TRAN_ACCEPT;
8497 		return (SATA_FAILURE);
8498 	}
8499 
8500 	*pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8501 	    sizeof (struct mode_page);
8502 
8503 	/*
8504 	 * We can enable and disable acoustice management and
8505 	 * set the acoustic management level.
8506 	 */
8507 
8508 	/*
8509 	 * Set-up Internal SET FEATURES command(s)
8510 	 */
8511 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8512 	scmd->satacmd_addr_type = 0;
8513 	scmd->satacmd_device_reg = 0;
8514 	scmd->satacmd_status_reg = 0;
8515 	scmd->satacmd_error_reg = 0;
8516 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
8517 	if (page->acoustic_manag_enable) {
8518 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC;
8519 		scmd->satacmd_sec_count_lsb = page->acoustic_manag_level;
8520 	} else {	/* disabling acoustic management */
8521 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC;
8522 	}
8523 
8524 	/* Transfer command to HBA */
8525 	if (sata_hba_start(spx, rval) != 0)
8526 		/*
8527 		 * Pkt not accepted for execution.
8528 		 */
8529 		return (SATA_FAILURE);
8530 
8531 	/* Now process return */
8532 	if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) {
8533 		sata_xlate_errors(spx);
8534 		return (SATA_FAILURE);
8535 	}
8536 
8537 	*dmod = 1;
8538 
8539 	return (SATA_SUCCESS);
8540 }
8541 
8542 /*
8543  * Process mode select power condition page 0x1a
8544  *
8545  * This function has to be called with a port mutex held.
8546  *
8547  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8548  *
8549  * Cannot be called in the interrupt context.
8550  */
8551 int
8552 sata_mode_select_page_1a(sata_pkt_txlate_t *spx, struct
8553     mode_info_power_cond *page, int parmlen, int *pagelen,
8554     int *rval, int *dmod)
8555 {
8556 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8557 	sata_drive_info_t *sdinfo;
8558 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8559 	sata_id_t *sata_id;
8560 	struct scsi_extended_sense *sense;
8561 	uint8_t ata_count;
8562 	int i, len;
8563 
8564 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8565 	    &spx->txlt_sata_pkt->satapkt_device);
8566 	sata_id = &sdinfo->satadrv_id;
8567 	*dmod = 0;
8568 
8569 	len = sizeof (struct mode_info_power_cond);
8570 	len += sizeof (struct mode_page);
8571 
8572 	/* If parmlen is too short or the feature is not supported, drop it */
8573 	if ((len < parmlen) || (page->idle == 1) ||
8574 	    (!(sata_id->ai_cap & SATA_STANDBYTIMER) && page->standby == 1)) {
8575 		*scsipkt->pkt_scbp = STATUS_CHECK;
8576 		sense = sata_arq_sense(spx);
8577 		sense->es_key = KEY_ILLEGAL_REQUEST;
8578 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8579 		*pagelen = parmlen;
8580 		*rval = TRAN_ACCEPT;
8581 		return (SATA_FAILURE);
8582 	}
8583 
8584 	*pagelen = len;
8585 
8586 	/*
8587 	 * Set-up Internal STANDBY command(s)
8588 	 */
8589 	if (page->standby == 0)
8590 		goto out;
8591 
8592 	ata_count = sata_get_standby_timer(page->standby_cond_timer);
8593 
8594 	scmd->satacmd_addr_type = 0;
8595 	scmd->satacmd_sec_count_lsb = ata_count;
8596 	scmd->satacmd_lba_low_lsb = 0;
8597 	scmd->satacmd_lba_mid_lsb = 0;
8598 	scmd->satacmd_lba_high_lsb = 0;
8599 	scmd->satacmd_features_reg = 0;
8600 	scmd->satacmd_device_reg = 0;
8601 	scmd->satacmd_status_reg = 0;
8602 	scmd->satacmd_cmd_reg = SATAC_STANDBY;
8603 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
8604 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
8605 
8606 	/* Transfer command to HBA */
8607 	if (sata_hba_start(spx, rval) != 0) {
8608 		return (SATA_FAILURE);
8609 	} else {
8610 		if ((scmd->satacmd_error_reg != 0) ||
8611 		    (spx->txlt_sata_pkt->satapkt_reason !=
8612 		    SATA_PKT_COMPLETED)) {
8613 			sata_xlate_errors(spx);
8614 			return (SATA_FAILURE);
8615 		}
8616 	}
8617 
8618 	for (i = 0; i < 4; i++) {
8619 		sdinfo->satadrv_standby_timer[i] = page->standby_cond_timer[i];
8620 	}
8621 out:
8622 	*dmod = 1;
8623 	return (SATA_SUCCESS);
8624 }
8625 
8626 /*
8627  * sata_build_lsense_page0() is used to create the
8628  * SCSI LOG SENSE page 0 (supported log pages)
8629  *
8630  * Currently supported pages are 0, 0x10, 0x2f, 0x30 and 0x0e
8631  * (supported log pages, self-test results, informational exceptions
8632  * Sun vendor specific ATA SMART data, and start stop cycle counter).
8633  *
8634  * Takes a sata_drive_info t * and the address of a buffer
8635  * in which to create the page information.
8636  *
8637  * Returns the number of bytes valid in the buffer.
8638  */
8639 static	int
8640 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf)
8641 {
8642 	struct log_parameter *lpp = (struct log_parameter *)buf;
8643 	uint8_t *page_ptr = (uint8_t *)lpp->param_values;
8644 	int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */
8645 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8646 
8647 	lpp->param_code[0] = 0;
8648 	lpp->param_code[1] = 0;
8649 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
8650 	*page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES;
8651 
8652 	if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
8653 		if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) {
8654 			*page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS;
8655 			++num_pages_supported;
8656 		}
8657 		*page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS;
8658 		++num_pages_supported;
8659 		*page_ptr++ = PAGE_CODE_SMART_READ_DATA;
8660 		++num_pages_supported;
8661 		*page_ptr++ = PAGE_CODE_START_STOP_CYCLE_COUNTER;
8662 		++num_pages_supported;
8663 	}
8664 
8665 	lpp->param_len = num_pages_supported;
8666 
8667 	return ((&lpp->param_values[0] - (uint8_t *)lpp) +
8668 	    num_pages_supported);
8669 }
8670 
8671 /*
8672  * sata_build_lsense_page_10() is used to create the
8673  * SCSI LOG SENSE page 0x10 (self-test results)
8674  *
8675  * Takes a sata_drive_info t * and the address of a buffer
8676  * in which to create the page information as well as a sata_hba_inst_t *.
8677  *
8678  * Returns the number of bytes valid in the buffer.
8679  *
8680  * Note: Self test and SMART data is accessible in device log pages.
8681  * The log pages can be accessed by SMART READ/WRITE LOG (up to 255 sectors
8682  * of data can be transferred by a single command), or by the General Purpose
8683  * Logging commands (GPL) READ LOG EXT and WRITE LOG EXT (up to 65,535 sectors
8684  * - approximately 33MB - can be transferred by a single command.
8685  * The SCT Command response (either error or command) is the same for both
8686  * the SMART and GPL methods of issuing commands.
8687  * This function uses READ LOG EXT command when drive supports LBA48, and
8688  * SMART READ command otherwise.
8689  *
8690  * Since above commands are executed in a synchronous mode, this function
8691  * should not be called in an interrupt context.
8692  */
8693 static	int
8694 sata_build_lsense_page_10(
8695 	sata_drive_info_t *sdinfo,
8696 	uint8_t *buf,
8697 	sata_hba_inst_t *sata_hba_inst)
8698 {
8699 	struct log_parameter *lpp = (struct log_parameter *)buf;
8700 	int rval;
8701 
8702 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
8703 		struct smart_ext_selftest_log *ext_selftest_log;
8704 
8705 		ext_selftest_log = kmem_zalloc(
8706 		    sizeof (struct smart_ext_selftest_log), KM_SLEEP);
8707 
8708 		rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo,
8709 		    ext_selftest_log, 0);
8710 		if (rval == 0) {
8711 			int index, start_index;
8712 			struct smart_ext_selftest_log_entry *entry;
8713 			static const struct smart_ext_selftest_log_entry empty =
8714 			    {0};
8715 			uint16_t block_num;
8716 			int count;
8717 			boolean_t only_one_block = B_FALSE;
8718 
8719 			index = ext_selftest_log->
8720 			    smart_ext_selftest_log_index[0];
8721 			index |= ext_selftest_log->
8722 			    smart_ext_selftest_log_index[1] << 8;
8723 			if (index == 0)
8724 				goto out;
8725 
8726 			--index;	/* Correct for 0 origin */
8727 			start_index = index;	/* remember where we started */
8728 			block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8729 			if (block_num != 0) {
8730 				rval = sata_ext_smart_selftest_read_log(
8731 				    sata_hba_inst, sdinfo, ext_selftest_log,
8732 				    block_num);
8733 				if (rval != 0)
8734 					goto out;
8735 			}
8736 			index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8737 			entry =
8738 			    &ext_selftest_log->
8739 			    smart_ext_selftest_log_entries[index];
8740 
8741 			for (count = 1;
8742 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
8743 			    ++count) {
8744 				uint8_t status;
8745 				uint8_t code;
8746 				uint8_t sense_key;
8747 				uint8_t add_sense_code;
8748 				uint8_t add_sense_code_qual;
8749 
8750 				/* If this is an unused entry, we are done */
8751 				if (bcmp(entry, &empty, sizeof (empty)) == 0) {
8752 					/* Broken firmware on some disks */
8753 					if (index + 1 ==
8754 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK) {
8755 						--entry;
8756 						--index;
8757 						if (bcmp(entry, &empty,
8758 						    sizeof (empty)) == 0)
8759 							goto out;
8760 					} else
8761 						goto out;
8762 				}
8763 
8764 				if (only_one_block &&
8765 				    start_index == index)
8766 					goto out;
8767 
8768 				lpp->param_code[0] = 0;
8769 				lpp->param_code[1] = count;
8770 				lpp->param_ctrl_flags =
8771 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
8772 				lpp->param_len =
8773 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
8774 
8775 				status = entry->smart_ext_selftest_log_status;
8776 				status >>= 4;
8777 				switch (status) {
8778 				case 0:
8779 				default:
8780 					sense_key = KEY_NO_SENSE;
8781 					add_sense_code =
8782 					    SD_SCSI_ASC_NO_ADD_SENSE;
8783 					add_sense_code_qual = 0;
8784 					break;
8785 				case 1:
8786 					sense_key = KEY_ABORTED_COMMAND;
8787 					add_sense_code =
8788 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8789 					add_sense_code_qual = SCSI_COMPONENT_81;
8790 					break;
8791 				case 2:
8792 					sense_key = KEY_ABORTED_COMMAND;
8793 					add_sense_code =
8794 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8795 					add_sense_code_qual = SCSI_COMPONENT_82;
8796 					break;
8797 				case 3:
8798 					sense_key = KEY_ABORTED_COMMAND;
8799 					add_sense_code =
8800 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8801 					add_sense_code_qual = SCSI_COMPONENT_83;
8802 					break;
8803 				case 4:
8804 					sense_key = KEY_HARDWARE_ERROR;
8805 					add_sense_code =
8806 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8807 					add_sense_code_qual = SCSI_COMPONENT_84;
8808 					break;
8809 				case 5:
8810 					sense_key = KEY_HARDWARE_ERROR;
8811 					add_sense_code =
8812 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8813 					add_sense_code_qual = SCSI_COMPONENT_85;
8814 					break;
8815 				case 6:
8816 					sense_key = KEY_HARDWARE_ERROR;
8817 					add_sense_code =
8818 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8819 					add_sense_code_qual = SCSI_COMPONENT_86;
8820 					break;
8821 				case 7:
8822 					sense_key = KEY_MEDIUM_ERROR;
8823 					add_sense_code =
8824 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8825 					add_sense_code_qual = SCSI_COMPONENT_87;
8826 					break;
8827 				case 8:
8828 					sense_key = KEY_HARDWARE_ERROR;
8829 					add_sense_code =
8830 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8831 					add_sense_code_qual = SCSI_COMPONENT_88;
8832 					break;
8833 				}
8834 				code = 0;	/* unspecified */
8835 				status |= (code << 4);
8836 				lpp->param_values[0] = status;
8837 				lpp->param_values[1] = 0; /* unspecified */
8838 				lpp->param_values[2] = entry->
8839 				    smart_ext_selftest_log_timestamp[1];
8840 				lpp->param_values[3] = entry->
8841 				    smart_ext_selftest_log_timestamp[0];
8842 				if (status != 0) {
8843 					lpp->param_values[4] = 0;
8844 					lpp->param_values[5] = 0;
8845 					lpp->param_values[6] = entry->
8846 					    smart_ext_selftest_log_failing_lba
8847 					    [5];
8848 					lpp->param_values[7] = entry->
8849 					    smart_ext_selftest_log_failing_lba
8850 					    [4];
8851 					lpp->param_values[8] = entry->
8852 					    smart_ext_selftest_log_failing_lba
8853 					    [3];
8854 					lpp->param_values[9] = entry->
8855 					    smart_ext_selftest_log_failing_lba
8856 					    [2];
8857 					lpp->param_values[10] = entry->
8858 					    smart_ext_selftest_log_failing_lba
8859 					    [1];
8860 					lpp->param_values[11] = entry->
8861 					    smart_ext_selftest_log_failing_lba
8862 					    [0];
8863 				} else {	/* No bad block address */
8864 					lpp->param_values[4] = 0xff;
8865 					lpp->param_values[5] = 0xff;
8866 					lpp->param_values[6] = 0xff;
8867 					lpp->param_values[7] = 0xff;
8868 					lpp->param_values[8] = 0xff;
8869 					lpp->param_values[9] = 0xff;
8870 					lpp->param_values[10] = 0xff;
8871 					lpp->param_values[11] = 0xff;
8872 				}
8873 
8874 				lpp->param_values[12] = sense_key;
8875 				lpp->param_values[13] = add_sense_code;
8876 				lpp->param_values[14] = add_sense_code_qual;
8877 				lpp->param_values[15] = 0; /* undefined */
8878 
8879 				lpp = (struct log_parameter *)
8880 				    (((uint8_t *)lpp) +
8881 				    SCSI_LOG_PARAM_HDR_LEN +
8882 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
8883 
8884 				--index;	/* Back up to previous entry */
8885 				if (index < 0) {
8886 					if (block_num > 0) {
8887 						--block_num;
8888 					} else {
8889 						struct read_log_ext_directory
8890 						    logdir;
8891 
8892 						rval =
8893 						    sata_read_log_ext_directory(
8894 						    sata_hba_inst, sdinfo,
8895 						    &logdir);
8896 						if (rval == -1)
8897 							goto out;
8898 						if ((logdir.read_log_ext_vers
8899 						    [0] == 0) &&
8900 						    (logdir.read_log_ext_vers
8901 						    [1] == 0))
8902 							goto out;
8903 						block_num =
8904 						    logdir.read_log_ext_nblks
8905 						    [EXT_SMART_SELFTEST_LOG_PAGE
8906 						    - 1][0];
8907 						block_num |= logdir.
8908 						    read_log_ext_nblks
8909 						    [EXT_SMART_SELFTEST_LOG_PAGE
8910 						    - 1][1] << 8;
8911 						--block_num;
8912 						only_one_block =
8913 						    (block_num == 0);
8914 					}
8915 					rval = sata_ext_smart_selftest_read_log(
8916 					    sata_hba_inst, sdinfo,
8917 					    ext_selftest_log, block_num);
8918 					if (rval != 0)
8919 						goto out;
8920 
8921 					index =
8922 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK -
8923 					    1;
8924 				}
8925 				index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8926 				entry = &ext_selftest_log->
8927 				    smart_ext_selftest_log_entries[index];
8928 			}
8929 		}
8930 out:
8931 		kmem_free(ext_selftest_log,
8932 		    sizeof (struct smart_ext_selftest_log));
8933 	} else {
8934 		struct smart_selftest_log *selftest_log;
8935 
8936 		selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log),
8937 		    KM_SLEEP);
8938 
8939 		rval = sata_smart_selftest_log(sata_hba_inst, sdinfo,
8940 		    selftest_log);
8941 
8942 		if (rval == 0) {
8943 			int index;
8944 			int count;
8945 			struct smart_selftest_log_entry *entry;
8946 			static const struct smart_selftest_log_entry empty =
8947 			    { 0 };
8948 
8949 			index = selftest_log->smart_selftest_log_index;
8950 			if (index == 0)
8951 				goto done;
8952 			--index;	/* Correct for 0 origin */
8953 			entry = &selftest_log->
8954 			    smart_selftest_log_entries[index];
8955 			for (count = 1;
8956 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
8957 			    ++count) {
8958 				uint8_t status;
8959 				uint8_t code;
8960 				uint8_t sense_key;
8961 				uint8_t add_sense_code;
8962 				uint8_t add_sense_code_qual;
8963 
8964 				if (bcmp(entry, &empty, sizeof (empty)) == 0)
8965 					goto done;
8966 
8967 				lpp->param_code[0] = 0;
8968 				lpp->param_code[1] = count;
8969 				lpp->param_ctrl_flags =
8970 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
8971 				lpp->param_len =
8972 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
8973 
8974 				status = entry->smart_selftest_log_status;
8975 				status >>= 4;
8976 				switch (status) {
8977 				case 0:
8978 				default:
8979 					sense_key = KEY_NO_SENSE;
8980 					add_sense_code =
8981 					    SD_SCSI_ASC_NO_ADD_SENSE;
8982 					break;
8983 				case 1:
8984 					sense_key = KEY_ABORTED_COMMAND;
8985 					add_sense_code =
8986 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8987 					add_sense_code_qual = SCSI_COMPONENT_81;
8988 					break;
8989 				case 2:
8990 					sense_key = KEY_ABORTED_COMMAND;
8991 					add_sense_code =
8992 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8993 					add_sense_code_qual = SCSI_COMPONENT_82;
8994 					break;
8995 				case 3:
8996 					sense_key = KEY_ABORTED_COMMAND;
8997 					add_sense_code =
8998 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8999 					add_sense_code_qual = SCSI_COMPONENT_83;
9000 					break;
9001 				case 4:
9002 					sense_key = KEY_HARDWARE_ERROR;
9003 					add_sense_code =
9004 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
9005 					add_sense_code_qual = SCSI_COMPONENT_84;
9006 					break;
9007 				case 5:
9008 					sense_key = KEY_HARDWARE_ERROR;
9009 					add_sense_code =
9010 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
9011 					add_sense_code_qual = SCSI_COMPONENT_85;
9012 					break;
9013 				case 6:
9014 					sense_key = KEY_HARDWARE_ERROR;
9015 					add_sense_code =
9016 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
9017 					add_sense_code_qual = SCSI_COMPONENT_86;
9018 					break;
9019 				case 7:
9020 					sense_key = KEY_MEDIUM_ERROR;
9021 					add_sense_code =
9022 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
9023 					add_sense_code_qual = SCSI_COMPONENT_87;
9024 					break;
9025 				case 8:
9026 					sense_key = KEY_HARDWARE_ERROR;
9027 					add_sense_code =
9028 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
9029 					add_sense_code_qual = SCSI_COMPONENT_88;
9030 					break;
9031 				}
9032 				code = 0;	/* unspecified */
9033 				status |= (code << 4);
9034 				lpp->param_values[0] = status;
9035 				lpp->param_values[1] = 0; /* unspecified */
9036 				lpp->param_values[2] = entry->
9037 				    smart_selftest_log_timestamp[1];
9038 				lpp->param_values[3] = entry->
9039 				    smart_selftest_log_timestamp[0];
9040 				if (status != 0) {
9041 					lpp->param_values[4] = 0;
9042 					lpp->param_values[5] = 0;
9043 					lpp->param_values[6] = 0;
9044 					lpp->param_values[7] = 0;
9045 					lpp->param_values[8] = entry->
9046 					    smart_selftest_log_failing_lba[3];
9047 					lpp->param_values[9] = entry->
9048 					    smart_selftest_log_failing_lba[2];
9049 					lpp->param_values[10] = entry->
9050 					    smart_selftest_log_failing_lba[1];
9051 					lpp->param_values[11] = entry->
9052 					    smart_selftest_log_failing_lba[0];
9053 				} else {	/* No block address */
9054 					lpp->param_values[4] = 0xff;
9055 					lpp->param_values[5] = 0xff;
9056 					lpp->param_values[6] = 0xff;
9057 					lpp->param_values[7] = 0xff;
9058 					lpp->param_values[8] = 0xff;
9059 					lpp->param_values[9] = 0xff;
9060 					lpp->param_values[10] = 0xff;
9061 					lpp->param_values[11] = 0xff;
9062 				}
9063 				lpp->param_values[12] = sense_key;
9064 				lpp->param_values[13] = add_sense_code;
9065 				lpp->param_values[14] = add_sense_code_qual;
9066 				lpp->param_values[15] = 0; /* undefined */
9067 
9068 				lpp = (struct log_parameter *)
9069 				    (((uint8_t *)lpp) +
9070 				    SCSI_LOG_PARAM_HDR_LEN +
9071 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
9072 				--index;	/* back up to previous entry */
9073 				if (index < 0) {
9074 					index =
9075 					    NUM_SMART_SELFTEST_LOG_ENTRIES - 1;
9076 				}
9077 				entry = &selftest_log->
9078 				    smart_selftest_log_entries[index];
9079 			}
9080 		}
9081 done:
9082 		kmem_free(selftest_log, sizeof (struct smart_selftest_log));
9083 	}
9084 
9085 	return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) *
9086 	    SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS);
9087 }
9088 
9089 /*
9090  * sata_build_lsense_page_2f() is used to create the
9091  * SCSI LOG SENSE page 0x2f (informational exceptions)
9092  *
9093  * Takes a sata_drive_info t * and the address of a buffer
9094  * in which to create the page information as well as a sata_hba_inst_t *.
9095  *
9096  * Returns the number of bytes valid in the buffer.
9097  *
9098  * Because it invokes function(s) that send synchronously executed command
9099  * to the HBA, it cannot be called in the interrupt context.
9100  */
9101 static	int
9102 sata_build_lsense_page_2f(
9103 	sata_drive_info_t *sdinfo,
9104 	uint8_t *buf,
9105 	sata_hba_inst_t *sata_hba_inst)
9106 {
9107 	struct log_parameter *lpp = (struct log_parameter *)buf;
9108 	int rval;
9109 	uint8_t *smart_data;
9110 	uint8_t temp;
9111 	sata_id_t *sata_id;
9112 #define	SMART_NO_TEMP	0xff
9113 
9114 	lpp->param_code[0] = 0;
9115 	lpp->param_code[1] = 0;
9116 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
9117 
9118 	/* Now get the SMART status w.r.t. threshold exceeded */
9119 	rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo);
9120 	switch (rval) {
9121 	case 1:
9122 		lpp->param_values[0] = SCSI_PREDICTED_FAILURE;
9123 		lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE;
9124 		break;
9125 	case 0:
9126 	case -1:	/* failed to get data */
9127 		lpp->param_values[0] = 0;	/* No failure predicted */
9128 		lpp->param_values[1] = 0;
9129 		break;
9130 #if defined(SATA_DEBUG)
9131 	default:
9132 		cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value");
9133 		/* NOTREACHED */
9134 #endif
9135 	}
9136 
9137 	sata_id = &sdinfo->satadrv_id;
9138 	if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP))
9139 		temp = SMART_NO_TEMP;
9140 	else {
9141 		/* Now get the temperature */
9142 		smart_data = kmem_zalloc(512, KM_SLEEP);
9143 		rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data,
9144 		    SCT_STATUS_LOG_PAGE, 1);
9145 		if (rval == -1)
9146 			temp = SMART_NO_TEMP;
9147 		else {
9148 			temp = smart_data[200];
9149 			if (temp & 0x80) {
9150 				if (temp & 0x7f)
9151 					temp = 0;
9152 				else
9153 					temp = SMART_NO_TEMP;
9154 			}
9155 		}
9156 		kmem_free(smart_data, 512);
9157 	}
9158 
9159 	lpp->param_values[2] = temp;	/* most recent temperature */
9160 	lpp->param_values[3] = 0;	/* required vendor specific byte */
9161 
9162 	lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN;
9163 
9164 
9165 	return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN);
9166 }
9167 
9168 /*
9169  * sata_build_lsense_page_30() is used to create the
9170  * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data).
9171  *
9172  * Takes a sata_drive_info t * and the address of a buffer
9173  * in which to create the page information as well as a sata_hba_inst_t *.
9174  *
9175  * Returns the number of bytes valid in the buffer.
9176  */
9177 static int
9178 sata_build_lsense_page_30(
9179 	sata_drive_info_t *sdinfo,
9180 	uint8_t *buf,
9181 	sata_hba_inst_t *sata_hba_inst)
9182 {
9183 	struct smart_data *smart_data = (struct smart_data *)buf;
9184 	int rval;
9185 
9186 	/* Now do the SMART READ DATA */
9187 	rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data);
9188 	if (rval == -1)
9189 		return (0);
9190 
9191 	return (sizeof (struct smart_data));
9192 }
9193 
9194 /*
9195  * sata_build_lsense_page_0e() is used to create the
9196  * SCSI LOG SENSE page 0e (start-stop cycle counter page)
9197  *
9198  * Date of Manufacture (0x0001)
9199  *	YEAR = "0000"
9200  *	WEEK = "00"
9201  * Accounting Date (0x0002)
9202  *	6 ASCII space character(20h)
9203  * Specified cycle count over device lifetime
9204  *	VALUE - THRESH - the delta between max and min;
9205  * Accumulated start-stop cycles
9206  *	VALUE - WORST - the accumulated cycles;
9207  *
9208  * ID FLAG THRESH VALUE WORST RAW on start/stop counter attribute
9209  *
9210  * Takes a sata_drive_info t * and the address of a buffer
9211  * in which to create the page information as well as a sata_hba_inst_t *.
9212  *
9213  * Returns the number of bytes valid in the buffer.
9214  */
9215 static	int
9216 sata_build_lsense_page_0e(sata_drive_info_t *sdinfo, uint8_t *buf,
9217 	sata_pkt_txlate_t *spx)
9218 {
9219 	struct start_stop_cycle_counter_log *log_page;
9220 	int i, rval, index;
9221 	uint8_t smart_data[512], id, value, worst, thresh;
9222 	uint32_t max_count, cycles;
9223 
9224 	/* Now do the SMART READ DATA */
9225 	rval = sata_fetch_smart_data(spx->txlt_sata_hba_inst, sdinfo,
9226 	    (struct smart_data *)smart_data);
9227 	if (rval == -1)
9228 		return (0);
9229 	for (i = 0, id = 0; i < SMART_START_STOP_COUNT_ID * 2; i++) {
9230 		index = (i * 12) + 2;
9231 		id = smart_data[index];
9232 		if (id != SMART_START_STOP_COUNT_ID)
9233 			continue;
9234 		else {
9235 			thresh = smart_data[index + 2];
9236 			value = smart_data[index + 3];
9237 			worst = smart_data[index + 4];
9238 			break;
9239 		}
9240 	}
9241 	if (id != SMART_START_STOP_COUNT_ID)
9242 		return (0);
9243 	max_count = value - thresh;
9244 	cycles = value - worst;
9245 
9246 	log_page = (struct start_stop_cycle_counter_log *)buf;
9247 	bzero(log_page, sizeof (struct start_stop_cycle_counter_log));
9248 	log_page->code = 0x0e;
9249 	log_page->page_len_low = 0x24;
9250 
9251 	log_page->manufactor_date_low = 0x1;
9252 	log_page->param_1.fmt_link = 0x1; /* 01b */
9253 	log_page->param_len_1 = 0x06;
9254 	for (i = 0; i < 4; i++) {
9255 		log_page->year_manu[i] = 0x30;
9256 		if (i < 2)
9257 			log_page->week_manu[i] = 0x30;
9258 	}
9259 
9260 	log_page->account_date_low = 0x02;
9261 	log_page->param_2.fmt_link = 0x01; /* 01b */
9262 	log_page->param_len_2 = 0x06;
9263 	for (i = 0; i < 4; i++) {
9264 		log_page->year_account[i] = 0x20;
9265 		if (i < 2)
9266 			log_page->week_account[i] = 0x20;
9267 	}
9268 
9269 	log_page->lifetime_code_low = 0x03;
9270 	log_page->param_3.fmt_link = 0x03; /* 11b */
9271 	log_page->param_len_3 = 0x04;
9272 	/* VALUE - THRESH - the delta between max and min */
9273 	log_page->cycle_code_low = 0x04;
9274 	log_page->param_4.fmt_link = 0x03; /* 11b */
9275 	log_page->param_len_4 = 0x04;
9276 	/* WORST - THRESH - the distance from 'now' to min */
9277 
9278 	for (i = 0; i < 4; i++) {
9279 		log_page->cycle_lifetime[i] =
9280 		    (max_count >> (8 * (3 - i))) & 0xff;
9281 		log_page->cycle_accumulated[i] =
9282 		    (cycles >> (8 * (3 - i))) & 0xff;
9283 	}
9284 
9285 	return (sizeof (struct start_stop_cycle_counter_log));
9286 }
9287 
9288 /*
9289  * This function was used for build a ATA read verify sector command
9290  */
9291 static void
9292 sata_build_read_verify_cmd(sata_cmd_t *scmd, uint16_t sec, uint64_t lba)
9293 {
9294 	scmd->satacmd_cmd_reg = SATAC_RDVER;
9295 	scmd->satacmd_addr_type = ATA_ADDR_LBA28;
9296 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
9297 
9298 	scmd->satacmd_sec_count_lsb = sec & 0xff;
9299 	scmd->satacmd_lba_low_lsb = lba & 0xff;
9300 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
9301 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
9302 	scmd->satacmd_device_reg = (SATA_ADH_LBA | (lba >> 24) & 0xf);
9303 	scmd->satacmd_features_reg = 0;
9304 	scmd->satacmd_status_reg = 0;
9305 	scmd->satacmd_error_reg = 0;
9306 }
9307 
9308 /*
9309  * This function was used for building an ATA
9310  * command, and only command register need to
9311  * be defined, other register will be zero or na.
9312  */
9313 static void
9314 sata_build_generic_cmd(sata_cmd_t *scmd, uint8_t cmd)
9315 {
9316 	scmd->satacmd_addr_type = 0;
9317 	scmd->satacmd_cmd_reg = cmd;
9318 	scmd->satacmd_device_reg = 0;
9319 	scmd->satacmd_sec_count_lsb = 0;
9320 	scmd->satacmd_lba_low_lsb = 0;
9321 	scmd->satacmd_lba_mid_lsb = 0;
9322 	scmd->satacmd_lba_high_lsb = 0;
9323 	scmd->satacmd_features_reg = 0;
9324 	scmd->satacmd_status_reg = 0;
9325 	scmd->satacmd_error_reg = 0;
9326 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
9327 }
9328 
9329 /*
9330  * This function was used for changing the standby
9331  * timer format from SCSI to ATA.
9332  */
9333 static uint8_t
9334 sata_get_standby_timer(uint8_t *timer)
9335 {
9336 	uint32_t i = 0, count = 0;
9337 	uint8_t ata_count;
9338 
9339 	for (i = 0; i < 4; i++) {
9340 		count = count << 8 | timer[i];
9341 	}
9342 
9343 	if (count == 0)
9344 		return (0);
9345 
9346 	if (count >= 1 && count <= 12000)
9347 		ata_count = (count -1) / 50 + 1;
9348 	else if (count > 12000 && count <= 12600)
9349 		ata_count = 0xfc;
9350 	else if (count > 12601 && count <= 12750)
9351 		ata_count = 0xff;
9352 	else if (count > 12750 && count <= 17999)
9353 		ata_count = 0xf1;
9354 	else if (count > 18000 && count <= 198000)
9355 		ata_count = count / 18000 + 240;
9356 	else
9357 		ata_count = 0xfd;
9358 	return (ata_count);
9359 }
9360 
9361 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */
9362 
9363 /*
9364  * Start command for ATAPI device.
9365  * This function processes scsi_pkt requests.
9366  * Now CD/DVD, tape and ATAPI disk devices are supported.
9367  * Most commands are packet without any translation into Packet Command.
9368  * Some may be trapped and executed as SATA commands (not clear which one).
9369  *
9370  * Returns TRAN_ACCEPT if command is accepted for execution (or completed
9371  * execution).
9372  * Returns other TRAN_XXXX codes if command is not accepted or completed
9373  * (see return values for sata_hba_start()).
9374  *
9375  * Note:
9376  * Inquiry cdb format differs between transport version 2 and 3.
9377  * However, the transport version 3 devices that were checked did not adhere
9378  * to the specification (ignored MSB of the allocation length). Therefore,
9379  * the transport version is not checked, but Inquiry allocation length is
9380  * truncated to 255 bytes if the original allocation length set-up by the
9381  * target driver is greater than 255 bytes.
9382  */
9383 static int
9384 sata_txlt_atapi(sata_pkt_txlate_t *spx)
9385 {
9386 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
9387 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
9388 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9389 	sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx);
9390 	sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba,
9391 	    &spx->txlt_sata_pkt->satapkt_device);
9392 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
9393 	int cdblen;
9394 	int rval, reason;
9395 	int synch;
9396 	union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp;
9397 
9398 	mutex_enter(cport_mutex);
9399 
9400 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
9401 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
9402 		mutex_exit(cport_mutex);
9403 		return (rval);
9404 	}
9405 
9406 	/*
9407 	 * ATAPI device executes some ATA commands in addition to those
9408 	 * commands sent via PACKET command. These ATA commands may be
9409 	 * executed by the regular SATA translation functions. None needs
9410 	 * to be captured now.
9411 	 *
9412 	 * Commands sent via PACKET command include:
9413 	 *	MMC command set for ATAPI CD/DVD device
9414 	 *	SSC command set for ATAPI TAPE device
9415 	 *	SBC command set for ATAPI disk device
9416 	 *
9417 	 */
9418 
9419 	/* Check the size of cdb */
9420 
9421 	switch (GETGROUP(cdbp)) {
9422 	case CDB_GROUPID_3:   /* Reserved, per SPC-4 */
9423 		/*
9424 		 * opcodes 0x7e and 0x7f identify variable-length CDBs and
9425 		 * therefore require special handling.  Return failure, for now.
9426 		 */
9427 		mutex_exit(cport_mutex);
9428 		return (TRAN_BADPKT);
9429 
9430 	case CDB_GROUPID_6:   /* Vendor-specific, per SPC-4 */
9431 	case CDB_GROUPID_7:   /* Vendor-specific, per SPC-4 */
9432 		/* obtain length from the scsi_pkt */
9433 		cdblen = scsipkt->pkt_cdblen;
9434 		break;
9435 
9436 	default:
9437 		/* CDB's length is statically known, per SPC-4 */
9438 		cdblen = scsi_cdb_size[GETGROUP(cdbp)];
9439 		break;
9440 	}
9441 
9442 	if (cdblen <= 0 || cdblen > sdinfo->satadrv_atapi_cdb_len) {
9443 		sata_log(NULL, CE_WARN,
9444 		    "sata: invalid ATAPI cdb length %d",
9445 		    cdblen);
9446 		mutex_exit(cport_mutex);
9447 		return (TRAN_BADPKT);
9448 	}
9449 
9450 	SATAATAPITRACE(spx, cdblen);
9451 
9452 	/*
9453 	 * For non-read/write commands we need to
9454 	 * map buffer
9455 	 */
9456 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
9457 	case SCMD_READ:
9458 	case SCMD_READ_G1:
9459 	case SCMD_READ_G5:
9460 	case SCMD_READ_G4:
9461 	case SCMD_WRITE:
9462 	case SCMD_WRITE_G1:
9463 	case SCMD_WRITE_G5:
9464 	case SCMD_WRITE_G4:
9465 		break;
9466 	default:
9467 		if (bp != NULL) {
9468 			if (bp->b_flags & (B_PHYS | B_PAGEIO))
9469 				bp_mapin(bp);
9470 		}
9471 		break;
9472 	}
9473 	/*
9474 	 * scmd->satacmd_flags.sata_data_direction default -
9475 	 * SATA_DIR_NODATA_XFER - is set by
9476 	 * sata_txlt_generic_pkt_info().
9477 	 */
9478 	if (scmd->satacmd_bp) {
9479 		if (scmd->satacmd_bp->b_flags & B_READ) {
9480 			scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9481 		} else {
9482 			scmd->satacmd_flags.sata_data_direction =
9483 			    SATA_DIR_WRITE;
9484 		}
9485 	}
9486 
9487 	/*
9488 	 * Set up ATAPI packet command.
9489 	 */
9490 
9491 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
9492 
9493 	/* Copy cdb into sata_cmd */
9494 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9495 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
9496 	bcopy(cdbp, scmd->satacmd_acdb, cdblen);
9497 
9498 	/* See note in the command header */
9499 	if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) {
9500 		if (scmd->satacmd_acdb[3] != 0)
9501 			scmd->satacmd_acdb[4] = 255;
9502 	}
9503 
9504 #ifdef SATA_DEBUG
9505 	if (sata_debug_flags & SATA_DBG_ATAPI) {
9506 		uint8_t *p = scmd->satacmd_acdb;
9507 		char buf[3 * SATA_ATAPI_MAX_CDB_LEN];
9508 
9509 		(void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN,
9510 		    "%02x %02x %02x %02x %02x %02x %02x %02x "
9511 		    "%2x %02x %02x %02x %02x %02x %02x %02x",
9512 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9513 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9514 		buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0';
9515 		cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf);
9516 	}
9517 #endif
9518 
9519 	/*
9520 	 * Preset request sense data to NO SENSE.
9521 	 * If there is no way to get error information via Request Sense,
9522 	 * the packet request sense data would not have to be modified by HBA,
9523 	 * but it could be returned as is.
9524 	 */
9525 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
9526 	sata_fixed_sense_data_preset(
9527 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
9528 
9529 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
9530 		/* Need callback function */
9531 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion;
9532 		synch = FALSE;
9533 	} else
9534 		synch = TRUE;
9535 
9536 	/* Transfer command to HBA */
9537 	if (sata_hba_start(spx, &rval) != 0) {
9538 		/* Pkt not accepted for execution */
9539 		mutex_exit(cport_mutex);
9540 		return (rval);
9541 	}
9542 	mutex_exit(cport_mutex);
9543 	/*
9544 	 * If execution is non-synchronous,
9545 	 * a callback function will handle potential errors, translate
9546 	 * the response and will do a callback to a target driver.
9547 	 * If it was synchronous, use the same framework callback to check
9548 	 * an execution status.
9549 	 */
9550 	if (synch) {
9551 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
9552 		    "synchronous execution status %x\n",
9553 		    spx->txlt_sata_pkt->satapkt_reason);
9554 		sata_txlt_atapi_completion(spx->txlt_sata_pkt);
9555 	}
9556 	return (TRAN_ACCEPT);
9557 }
9558 
9559 
9560 /*
9561  * ATAPI Packet command completion.
9562  *
9563  * Failure of the command passed via Packet command are considered device
9564  * error. SATA HBA driver would have to retrieve error data (via Request
9565  * Sense command delivered via error retrieval sata packet) and copy it
9566  * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data.
9567  */
9568 static void
9569 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt)
9570 {
9571 	sata_pkt_txlate_t *spx =
9572 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
9573 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
9574 	struct scsi_extended_sense *sense;
9575 	struct buf *bp;
9576 	int rval;
9577 
9578 #ifdef SATA_DEBUG
9579 	uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense;
9580 #endif
9581 
9582 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
9583 	    STATE_SENT_CMD | STATE_GOT_STATUS;
9584 
9585 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
9586 		/* Normal completion */
9587 		if (sata_pkt->satapkt_cmd.satacmd_bp != NULL)
9588 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
9589 		scsipkt->pkt_reason = CMD_CMPLT;
9590 		*scsipkt->pkt_scbp = STATUS_GOOD;
9591 		if (spx->txlt_tmp_buf != NULL) {
9592 			/* Temporary buffer was used */
9593 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9594 			if (bp->b_flags & B_READ) {
9595 				rval = ddi_dma_sync(
9596 				    spx->txlt_buf_dma_handle, 0, 0,
9597 				    DDI_DMA_SYNC_FORCPU);
9598 				ASSERT(rval == DDI_SUCCESS);
9599 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
9600 				    bp->b_bcount);
9601 			}
9602 		}
9603 	} else {
9604 		/*
9605 		 * Something went wrong - analyze return
9606 		 */
9607 		*scsipkt->pkt_scbp = STATUS_CHECK;
9608 		sense = sata_arq_sense(spx);
9609 
9610 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
9611 			/*
9612 			 * pkt_reason should be CMD_CMPLT for DEVICE ERROR.
9613 			 * Under this condition ERR bit is set for ATA command,
9614 			 * and CHK bit set for ATAPI command.
9615 			 *
9616 			 * Please check st_intr & sdintr about how pkt_reason
9617 			 * is used.
9618 			 */
9619 			scsipkt->pkt_reason = CMD_CMPLT;
9620 
9621 			/*
9622 			 * We may not have ARQ data if there was a double
9623 			 * error. But sense data in sata packet was pre-set
9624 			 * with NO SENSE so it is valid even if HBA could
9625 			 * not retrieve a real sense data.
9626 			 * Just copy this sense data into scsi pkt sense area.
9627 			 */
9628 			bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense,
9629 			    SATA_ATAPI_MIN_RQSENSE_LEN);
9630 #ifdef SATA_DEBUG
9631 			if (sata_debug_flags & SATA_DBG_SCSI_IF) {
9632 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
9633 				    "sata_txlt_atapi_completion: %02x\n"
9634 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
9635 				    "          %02x %02x %02x %02x %02x %02x "
9636 				    "          %02x %02x %02x %02x %02x %02x\n",
9637 				    scsipkt->pkt_reason,
9638 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
9639 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
9640 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
9641 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
9642 				    rqsp[16], rqsp[17]);
9643 			}
9644 #endif
9645 		} else {
9646 			switch (sata_pkt->satapkt_reason) {
9647 			case SATA_PKT_PORT_ERROR:
9648 				/*
9649 				 * We have no device data.
9650 				 */
9651 				scsipkt->pkt_reason = CMD_INCOMPLETE;
9652 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
9653 				    STATE_GOT_TARGET | STATE_SENT_CMD |
9654 				    STATE_GOT_STATUS);
9655 				sense->es_key = KEY_HARDWARE_ERROR;
9656 				break;
9657 
9658 			case SATA_PKT_TIMEOUT:
9659 				scsipkt->pkt_reason = CMD_TIMEOUT;
9660 				scsipkt->pkt_statistics |=
9661 				    STAT_TIMEOUT | STAT_DEV_RESET;
9662 				/*
9663 				 * Need to check if HARDWARE_ERROR/
9664 				 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more
9665 				 * appropriate.
9666 				 */
9667 				break;
9668 
9669 			case SATA_PKT_ABORTED:
9670 				scsipkt->pkt_reason = CMD_ABORTED;
9671 				scsipkt->pkt_statistics |= STAT_ABORTED;
9672 				/* Should we set key COMMAND_ABPRTED? */
9673 				break;
9674 
9675 			case SATA_PKT_RESET:
9676 				scsipkt->pkt_reason = CMD_RESET;
9677 				scsipkt->pkt_statistics |= STAT_DEV_RESET;
9678 				/*
9679 				 * May be we should set Unit Attention /
9680 				 * Reset. Perhaps the same should be
9681 				 * returned for disks....
9682 				 */
9683 				sense->es_key = KEY_UNIT_ATTENTION;
9684 				sense->es_add_code = SD_SCSI_ASC_RESET;
9685 				break;
9686 
9687 			default:
9688 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9689 				    "sata_txlt_atapi_completion: "
9690 				    "invalid packet completion reason"));
9691 				scsipkt->pkt_reason = CMD_TRAN_ERR;
9692 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
9693 				    STATE_GOT_TARGET | STATE_SENT_CMD |
9694 				    STATE_GOT_STATUS);
9695 				break;
9696 			}
9697 		}
9698 	}
9699 
9700 	SATAATAPITRACE(spx, 0);
9701 
9702 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
9703 	    scsipkt->pkt_comp != NULL) {
9704 		/* scsi callback required */
9705 		(*scsipkt->pkt_comp)(scsipkt);
9706 	}
9707 }
9708 
9709 /*
9710  * Set up error retrieval sata command for ATAPI Packet Command error data
9711  * recovery.
9712  *
9713  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
9714  * returns SATA_FAILURE otherwise.
9715  */
9716 
9717 static int
9718 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
9719 {
9720 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
9721 	sata_cmd_t *scmd;
9722 	struct buf *bp;
9723 
9724 	/*
9725 	 * Allocate dma-able buffer error data.
9726 	 * Buffer allocation will take care of buffer alignment and other DMA
9727 	 * attributes.
9728 	 */
9729 	bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN);
9730 	if (bp == NULL) {
9731 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
9732 		    "sata_get_err_retrieval_pkt: "
9733 		    "cannot allocate buffer for error data", NULL);
9734 		return (SATA_FAILURE);
9735 	}
9736 	bp_mapin(bp); /* make data buffer accessible */
9737 
9738 	/* Operation modes are up to the caller */
9739 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
9740 
9741 	/* Synchronous mode, no callback - may be changed by the caller */
9742 	spkt->satapkt_comp = NULL;
9743 	spkt->satapkt_time = sata_default_pkt_time;
9744 
9745 	scmd = &spkt->satapkt_cmd;
9746 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9747 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
9748 
9749 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
9750 
9751 	/*
9752 	 * Set-up acdb. Request Sense CDB (packet command content) is
9753 	 * not in DMA-able buffer. Its handling is HBA-specific (how
9754 	 * it is transfered into packet FIS).
9755 	 */
9756 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9757 	bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN);
9758 	/* Following zeroing of pad bytes may not be necessary */
9759 	bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN],
9760 	    sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN);
9761 
9762 	/*
9763 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
9764 	 * before accessing it. Handle is in usual place in translate struct.
9765 	 */
9766 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
9767 
9768 	/*
9769 	 * Preset request sense data to NO SENSE.
9770 	 * Here it is redundant, only for a symetry with scsi-originated
9771 	 * packets. It should not be used for anything but debugging.
9772 	 */
9773 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
9774 	sata_fixed_sense_data_preset(
9775 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
9776 
9777 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
9778 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
9779 
9780 	return (SATA_SUCCESS);
9781 }
9782 
9783 /*
9784  * Set-up ATAPI packet command.
9785  * Data transfer direction has to be set-up in sata_cmd structure prior to
9786  * calling this function.
9787  *
9788  * Returns void
9789  */
9790 
9791 static void
9792 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo)
9793 {
9794 	scmd->satacmd_addr_type = 0;		/* N/A */
9795 	scmd->satacmd_sec_count_lsb = 0;	/* no tag */
9796 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
9797 	scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ;
9798 	scmd->satacmd_lba_high_lsb =
9799 	    (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8);
9800 	scmd->satacmd_cmd_reg = SATAC_PACKET;	/* Command */
9801 
9802 	/*
9803 	 * We want all data to be transfered via DMA.
9804 	 * But specify it only if drive supports DMA and DMA mode is
9805 	 * selected - some drives are sensitive about it.
9806 	 * Hopefully it wil work for all drives....
9807 	 */
9808 	if (sdinfo->satadrv_settings & SATA_DEV_DMA)
9809 		scmd->satacmd_features_reg = SATA_ATAPI_F_DMA;
9810 
9811 	/*
9812 	 * Features register requires special care for devices that use
9813 	 * Serial ATA bridge - they need an explicit specification of
9814 	 * the data transfer direction for Packet DMA commands.
9815 	 * Setting this bit is harmless if DMA is not used.
9816 	 *
9817 	 * Many drives do not implement word 80, specifying what ATA/ATAPI
9818 	 * spec they follow.
9819 	 * We are arbitrarily following the latest SerialATA 2.6 spec,
9820 	 * which uses ATA/ATAPI 6 specification for Identify Data, unless
9821 	 * ATA/ATAPI-7 support is explicitly indicated.
9822 	 */
9823 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
9824 	    sdinfo->satadrv_id.ai_majorversion != 0xffff &&
9825 	    (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) {
9826 		/*
9827 		 * Specification of major version is valid and version 7
9828 		 * is supported. It does automatically imply that all
9829 		 * spec features are supported. For now, we assume that
9830 		 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete.
9831 		 */
9832 		if ((sdinfo->satadrv_id.ai_dirdma &
9833 		    SATA_ATAPI_ID_DMADIR_REQ) != 0) {
9834 			if (scmd->satacmd_flags.sata_data_direction ==
9835 			    SATA_DIR_READ)
9836 			scmd->satacmd_features_reg |=
9837 			    SATA_ATAPI_F_DATA_DIR_READ;
9838 		}
9839 	}
9840 }
9841 
9842 
9843 #ifdef SATA_DEBUG
9844 
9845 /* Display 18 bytes of Inquiry data */
9846 static void
9847 sata_show_inqry_data(uint8_t *buf)
9848 {
9849 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
9850 	uint8_t *p;
9851 
9852 	cmn_err(CE_NOTE, "Inquiry data:");
9853 	cmn_err(CE_NOTE, "device type %x", inq->inq_dtype);
9854 	cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb);
9855 	cmn_err(CE_NOTE, "version %x", inq->inq_ansi);
9856 	cmn_err(CE_NOTE, "ATAPI transport version %d",
9857 	    SATA_ATAPI_TRANS_VERSION(inq));
9858 	cmn_err(CE_NOTE, "response data format %d, aenc %d",
9859 	    inq->inq_rdf, inq->inq_aenc);
9860 	cmn_err(CE_NOTE, " additional length %d", inq->inq_len);
9861 	cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs);
9862 	p = (uint8_t *)inq->inq_vid;
9863 	cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x "
9864 	    "%02x %02x %02x %02x",
9865 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
9866 	p = (uint8_t *)inq->inq_vid;
9867 	cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c",
9868 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
9869 
9870 	p = (uint8_t *)inq->inq_pid;
9871 	cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x "
9872 	    "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
9873 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9874 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9875 	p = (uint8_t *)inq->inq_pid;
9876 	cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c "
9877 	    "%c %c %c %c %c %c %c %c",
9878 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9879 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9880 
9881 	p = (uint8_t *)inq->inq_revision;
9882 	cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x",
9883 	    p[0], p[1], p[2], p[3]);
9884 	p = (uint8_t *)inq->inq_revision;
9885 	cmn_err(CE_NOTE, "revision: %c %c %c %c",
9886 	    p[0], p[1], p[2], p[3]);
9887 
9888 }
9889 
9890 
9891 static void
9892 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count)
9893 {
9894 	struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt;
9895 
9896 	if (scsi_pkt == NULL)
9897 		return;
9898 	if (count != 0) {
9899 		/* saving cdb */
9900 		bzero(sata_atapi_trace[sata_atapi_trace_index].acdb,
9901 		    SATA_ATAPI_MAX_CDB_LEN);
9902 		bcopy(scsi_pkt->pkt_cdbp,
9903 		    sata_atapi_trace[sata_atapi_trace_index].acdb, count);
9904 	} else {
9905 		bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)->
9906 		    sts_sensedata,
9907 		    sata_atapi_trace[sata_atapi_trace_index].arqs,
9908 		    SATA_ATAPI_MIN_RQSENSE_LEN);
9909 		sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason =
9910 		    scsi_pkt->pkt_reason;
9911 		sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason =
9912 		    spx->txlt_sata_pkt->satapkt_reason;
9913 
9914 		if (++sata_atapi_trace_index >= 64)
9915 			sata_atapi_trace_index = 0;
9916 	}
9917 }
9918 
9919 #endif
9920 
9921 /*
9922  * Fetch inquiry data from ATAPI device
9923  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
9924  *
9925  * Note:
9926  * inqb pointer does not point to a DMA-able buffer. It is a local buffer
9927  * where the caller expects to see the inquiry data.
9928  *
9929  */
9930 
9931 static int
9932 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba,
9933     sata_address_t *saddr, struct scsi_inquiry *inq)
9934 {
9935 	sata_pkt_txlate_t *spx;
9936 	sata_pkt_t *spkt;
9937 	struct buf *bp;
9938 	sata_drive_info_t *sdinfo;
9939 	sata_cmd_t *scmd;
9940 	int rval;
9941 	uint8_t *rqsp;
9942 	dev_info_t *dip = SATA_DIP(sata_hba);
9943 #ifdef SATA_DEBUG
9944 	char msg_buf[MAXPATHLEN];
9945 #endif
9946 	kmutex_t *cport_mutex;
9947 
9948 	ASSERT(sata_hba != NULL);
9949 
9950 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
9951 	spx->txlt_sata_hba_inst = sata_hba;
9952 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
9953 	spkt = sata_pkt_alloc(spx, NULL);
9954 	if (spkt == NULL) {
9955 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
9956 		return (SATA_FAILURE);
9957 	}
9958 	/* address is needed now */
9959 	spkt->satapkt_device.satadev_addr = *saddr;
9960 
9961 	/* scsi_inquiry size buffer */
9962 	bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry));
9963 	if (bp == NULL) {
9964 		sata_pkt_free(spx);
9965 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
9966 		SATA_LOG_D((sata_hba, CE_WARN,
9967 		    "sata_get_atapi_inquiry_data: "
9968 		    "cannot allocate data buffer"));
9969 		return (SATA_FAILURE);
9970 	}
9971 	bp_mapin(bp); /* make data buffer accessible */
9972 
9973 	scmd = &spkt->satapkt_cmd;
9974 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
9975 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
9976 
9977 	/* Use synchronous mode */
9978 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
9979 	spkt->satapkt_comp = NULL;
9980 	spkt->satapkt_time = sata_default_pkt_time;
9981 
9982 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
9983 
9984 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9985 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
9986 
9987 	cport_mutex = &(SATA_CPORT_MUTEX(sata_hba, saddr->cport));
9988 	mutex_enter(cport_mutex);
9989 	sdinfo = sata_get_device_info(sata_hba,
9990 	    &spx->txlt_sata_pkt->satapkt_device);
9991 	if (sdinfo == NULL) {
9992 		/* we have to be carefull about the disapearing device */
9993 		mutex_exit(cport_mutex);
9994 		rval = SATA_FAILURE;
9995 		goto cleanup;
9996 	}
9997 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
9998 
9999 	/*
10000 	 * Set-up acdb. This works for atapi transport version 2 and later.
10001 	 */
10002 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
10003 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
10004 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
10005 	scmd->satacmd_acdb[1] = 0x00;
10006 	scmd->satacmd_acdb[2] = 0x00;
10007 	scmd->satacmd_acdb[3] = 0x00;
10008 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
10009 	scmd->satacmd_acdb[5] = 0x00;
10010 
10011 	sata_fixed_sense_data_preset(
10012 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
10013 
10014 	/* Transfer command to HBA */
10015 	if (sata_hba_start(spx, &rval) != 0) {
10016 		/* Pkt not accepted for execution */
10017 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
10018 		    "sata_get_atapi_inquiry_data: "
10019 		    "Packet not accepted for execution - ret: %02x", rval);
10020 		mutex_exit(cport_mutex);
10021 		rval = SATA_FAILURE;
10022 		goto cleanup;
10023 	}
10024 	mutex_exit(cport_mutex);
10025 
10026 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10027 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
10028 		    "sata_get_atapi_inquiry_data: "
10029 		    "Packet completed successfully - ret: %02x", rval);
10030 		if (spx->txlt_buf_dma_handle != NULL) {
10031 			/*
10032 			 * Sync buffer. Handle is in usual place in translate
10033 			 * struct.
10034 			 */
10035 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10036 			    DDI_DMA_SYNC_FORCPU);
10037 			ASSERT(rval == DDI_SUCCESS);
10038 		}
10039 
10040 		if (sata_check_for_dma_error(dip, spx)) {
10041 			ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED);
10042 			rval = SATA_FAILURE;
10043 		} else {
10044 			/*
10045 			 * Normal completion - copy data into caller's buffer
10046 			 */
10047 			bcopy(bp->b_un.b_addr, (uint8_t *)inq,
10048 			    sizeof (struct scsi_inquiry));
10049 #ifdef SATA_DEBUG
10050 			if (sata_debug_flags & SATA_DBG_ATAPI) {
10051 				sata_show_inqry_data((uint8_t *)inq);
10052 			}
10053 #endif
10054 			rval = SATA_SUCCESS;
10055 		}
10056 	} else {
10057 		/*
10058 		 * Something went wrong - analyze return - check rqsense data
10059 		 */
10060 		rval = SATA_FAILURE;
10061 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
10062 			/*
10063 			 * ARQ data hopefull show something other than NO SENSE
10064 			 */
10065 			rqsp = scmd->satacmd_rqsense;
10066 #ifdef SATA_DEBUG
10067 			if (sata_debug_flags & SATA_DBG_ATAPI) {
10068 				msg_buf[0] = '\0';
10069 				(void) snprintf(msg_buf, MAXPATHLEN,
10070 				    "ATAPI packet completion reason: %02x\n"
10071 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x\n"
10072 				    "          %02x %02x %02x %02x %02x %02x\n"
10073 				    "          %02x %02x %02x %02x %02x %02x",
10074 				    spkt->satapkt_reason,
10075 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
10076 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
10077 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
10078 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
10079 				    rqsp[16], rqsp[17]);
10080 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
10081 				    "%s", msg_buf);
10082 			}
10083 #endif
10084 		} else {
10085 			switch (spkt->satapkt_reason) {
10086 			case SATA_PKT_PORT_ERROR:
10087 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10088 				    "sata_get_atapi_inquiry_data: "
10089 				    "packet reason: port error", NULL);
10090 				break;
10091 
10092 			case SATA_PKT_TIMEOUT:
10093 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10094 				    "sata_get_atapi_inquiry_data: "
10095 				    "packet reason: timeout", NULL);
10096 				break;
10097 
10098 			case SATA_PKT_ABORTED:
10099 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10100 				    "sata_get_atapi_inquiry_data: "
10101 				    "packet reason: aborted", NULL);
10102 				break;
10103 
10104 			case SATA_PKT_RESET:
10105 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10106 				    "sata_get_atapi_inquiry_data: "
10107 				    "packet reason: reset\n", NULL);
10108 				break;
10109 			default:
10110 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10111 				    "sata_get_atapi_inquiry_data: "
10112 				    "invalid packet reason: %02x\n",
10113 				    spkt->satapkt_reason);
10114 				break;
10115 			}
10116 		}
10117 	}
10118 cleanup:
10119 	sata_free_local_buffer(spx);
10120 	sata_pkt_free(spx);
10121 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
10122 	return (rval);
10123 }
10124 
10125 
10126 
10127 
10128 
10129 #if 0
10130 #ifdef SATA_DEBUG
10131 
10132 /*
10133  * Test ATAPI packet command.
10134  * Single threaded test: send packet command in synch mode, process completion
10135  *
10136  */
10137 static void
10138 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport)
10139 {
10140 	sata_pkt_txlate_t *spx;
10141 	sata_pkt_t *spkt;
10142 	struct buf *bp;
10143 	sata_device_t sata_device;
10144 	sata_drive_info_t *sdinfo;
10145 	sata_cmd_t *scmd;
10146 	int rval;
10147 	uint8_t *rqsp;
10148 
10149 	ASSERT(sata_hba_inst != NULL);
10150 	sata_device.satadev_addr.cport = cport;
10151 	sata_device.satadev_addr.pmport = 0;
10152 	sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
10153 	sata_device.satadev_rev = SATA_DEVICE_REV;
10154 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10155 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
10156 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10157 	if (sdinfo == NULL) {
10158 		sata_log(sata_hba_inst, CE_WARN,
10159 		    "sata_test_atapi_packet_command: "
10160 		    "no device info for cport %d",
10161 		    sata_device.satadev_addr.cport);
10162 		return;
10163 	}
10164 
10165 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10166 	spx->txlt_sata_hba_inst = sata_hba_inst;
10167 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
10168 	spkt = sata_pkt_alloc(spx, NULL);
10169 	if (spkt == NULL) {
10170 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10171 		return;
10172 	}
10173 	/* address is needed now */
10174 	spkt->satapkt_device.satadev_addr = sata_device.satadev_addr;
10175 
10176 	/* 1024k buffer */
10177 	bp = sata_alloc_local_buffer(spx, 1024);
10178 	if (bp == NULL) {
10179 		sata_pkt_free(spx);
10180 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10181 		sata_log(sata_hba_inst, CE_WARN,
10182 		    "sata_test_atapi_packet_command: "
10183 		    "cannot allocate data buffer");
10184 		return;
10185 	}
10186 	bp_mapin(bp); /* make data buffer accessible */
10187 
10188 	scmd = &spkt->satapkt_cmd;
10189 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
10190 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
10191 
10192 	/* Use synchronous mode */
10193 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10194 
10195 	/* Synchronous mode, no callback - may be changed by the caller */
10196 	spkt->satapkt_comp = NULL;
10197 	spkt->satapkt_time = sata_default_pkt_time;
10198 
10199 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
10200 
10201 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
10202 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10203 
10204 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
10205 
10206 	/* Set-up acdb. */
10207 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
10208 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
10209 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
10210 	scmd->satacmd_acdb[1] = 0x00;
10211 	scmd->satacmd_acdb[2] = 0x00;
10212 	scmd->satacmd_acdb[3] = 0x00;
10213 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
10214 	scmd->satacmd_acdb[5] = 0x00;
10215 
10216 	sata_fixed_sense_data_preset(
10217 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
10218 
10219 	/* Transfer command to HBA */
10220 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10221 	if (sata_hba_start(spx, &rval) != 0) {
10222 		/* Pkt not accepted for execution */
10223 		sata_log(sata_hba_inst, CE_WARN,
10224 		    "sata_test_atapi_packet_command: "
10225 		    "Packet not accepted for execution - ret: %02x", rval);
10226 		mutex_exit(
10227 		    &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10228 		goto cleanup;
10229 	}
10230 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10231 
10232 	if (spx->txlt_buf_dma_handle != NULL) {
10233 		/*
10234 		 * Sync buffer. Handle is in usual place in translate struct.
10235 		 */
10236 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10237 		    DDI_DMA_SYNC_FORCPU);
10238 		ASSERT(rval == DDI_SUCCESS);
10239 	}
10240 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10241 		sata_log(sata_hba_inst, CE_WARN,
10242 		    "sata_test_atapi_packet_command: "
10243 		    "Packet completed successfully");
10244 		/*
10245 		 * Normal completion - show inquiry data
10246 		 */
10247 		sata_show_inqry_data((uint8_t *)bp->b_un.b_addr);
10248 	} else {
10249 		/*
10250 		 * Something went wrong - analyze return - check rqsense data
10251 		 */
10252 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
10253 			/*
10254 			 * ARQ data hopefull show something other than NO SENSE
10255 			 */
10256 			rqsp = scmd->satacmd_rqsense;
10257 			sata_log(spx->txlt_sata_hba_inst, CE_WARN,
10258 			    "ATAPI packet completion reason: %02x\n"
10259 			    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
10260 			    "          %02x %02x %02x %02x %02x %02x "
10261 			    "          %02x %02x %02x %02x %02x %02x\n",
10262 			    spkt->satapkt_reason,
10263 			    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
10264 			    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
10265 			    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
10266 			    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
10267 			    rqsp[16], rqsp[17]);
10268 		} else {
10269 			switch (spkt->satapkt_reason) {
10270 			case SATA_PKT_PORT_ERROR:
10271 				sata_log(sata_hba_inst, CE_WARN,
10272 				    "sata_test_atapi_packet_command: "
10273 				    "packet reason: port error\n");
10274 				break;
10275 
10276 			case SATA_PKT_TIMEOUT:
10277 				sata_log(sata_hba_inst, CE_WARN,
10278 				    "sata_test_atapi_packet_command: "
10279 				    "packet reason: timeout\n");
10280 				break;
10281 
10282 			case SATA_PKT_ABORTED:
10283 				sata_log(sata_hba_inst, CE_WARN,
10284 				    "sata_test_atapi_packet_command: "
10285 				    "packet reason: aborted\n");
10286 				break;
10287 
10288 			case SATA_PKT_RESET:
10289 				sata_log(sata_hba_inst, CE_WARN,
10290 				    "sata_test_atapi_packet_command: "
10291 				    "packet reason: reset\n");
10292 				break;
10293 			default:
10294 				sata_log(sata_hba_inst, CE_WARN,
10295 				    "sata_test_atapi_packet_command: "
10296 				    "invalid packet reason: %02x\n",
10297 				    spkt->satapkt_reason);
10298 				break;
10299 			}
10300 		}
10301 	}
10302 cleanup:
10303 	sata_free_local_buffer(spx);
10304 	sata_pkt_free(spx);
10305 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
10306 }
10307 
10308 #endif /* SATA_DEBUG */
10309 #endif /* 1 */
10310 
10311 
10312 /* ************************** LOCAL HELPER FUNCTIONS *********************** */
10313 
10314 /*
10315  * Validate sata_tran info
10316  * SATA_FAILURE returns if structure is inconsistent or structure revision
10317  * does not match one used by the framework.
10318  *
10319  * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains
10320  * required function pointers.
10321  * Returns SATA_FAILURE otherwise.
10322  */
10323 static int
10324 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran)
10325 {
10326 	/*
10327 	 * SATA_TRAN_HBA_REV is the current (highest) revision number
10328 	 * of the SATA interface.
10329 	 */
10330 	if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) {
10331 		sata_log(NULL, CE_WARN,
10332 		    "sata: invalid sata_hba_tran version %d for driver %s",
10333 		    sata_tran->sata_tran_hba_rev, ddi_driver_name(dip));
10334 		return (SATA_FAILURE);
10335 	}
10336 
10337 	if (dip != sata_tran->sata_tran_hba_dip) {
10338 		SATA_LOG_D((NULL, CE_WARN,
10339 		    "sata: inconsistent sata_tran_hba_dip "
10340 		    "%p / %p", sata_tran->sata_tran_hba_dip, dip));
10341 		return (SATA_FAILURE);
10342 	}
10343 
10344 	if (sata_tran->sata_tran_probe_port == NULL ||
10345 	    sata_tran->sata_tran_start == NULL ||
10346 	    sata_tran->sata_tran_abort == NULL ||
10347 	    sata_tran->sata_tran_reset_dport == NULL ||
10348 	    sata_tran->sata_tran_hotplug_ops == NULL ||
10349 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL ||
10350 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate ==
10351 	    NULL) {
10352 		SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing "
10353 		    "required functions"));
10354 	}
10355 	return (SATA_SUCCESS);
10356 }
10357 
10358 /*
10359  * Remove HBA instance from sata_hba_list.
10360  */
10361 static void
10362 sata_remove_hba_instance(dev_info_t *dip)
10363 {
10364 	sata_hba_inst_t	*sata_hba_inst;
10365 
10366 	mutex_enter(&sata_mutex);
10367 	for (sata_hba_inst = sata_hba_list;
10368 	    sata_hba_inst != (struct sata_hba_inst *)NULL;
10369 	    sata_hba_inst = sata_hba_inst->satahba_next) {
10370 		if (sata_hba_inst->satahba_dip == dip)
10371 			break;
10372 	}
10373 
10374 	if (sata_hba_inst == (struct sata_hba_inst *)NULL) {
10375 #ifdef SATA_DEBUG
10376 		cmn_err(CE_WARN, "sata_remove_hba_instance: "
10377 		    "unknown HBA instance\n");
10378 #endif
10379 		ASSERT(FALSE);
10380 	}
10381 	if (sata_hba_inst == sata_hba_list) {
10382 		sata_hba_list = sata_hba_inst->satahba_next;
10383 		if (sata_hba_list) {
10384 			sata_hba_list->satahba_prev =
10385 			    (struct sata_hba_inst *)NULL;
10386 		}
10387 		if (sata_hba_inst == sata_hba_list_tail) {
10388 			sata_hba_list_tail = NULL;
10389 		}
10390 	} else if (sata_hba_inst == sata_hba_list_tail) {
10391 		sata_hba_list_tail = sata_hba_inst->satahba_prev;
10392 		if (sata_hba_list_tail) {
10393 			sata_hba_list_tail->satahba_next =
10394 			    (struct sata_hba_inst *)NULL;
10395 		}
10396 	} else {
10397 		sata_hba_inst->satahba_prev->satahba_next =
10398 		    sata_hba_inst->satahba_next;
10399 		sata_hba_inst->satahba_next->satahba_prev =
10400 		    sata_hba_inst->satahba_prev;
10401 	}
10402 	mutex_exit(&sata_mutex);
10403 }
10404 
10405 /*
10406  * Probe all SATA ports of the specified HBA instance.
10407  * The assumption is that there are no target and attachment point minor nodes
10408  * created by the boot subsystems, so we do not need to prune device tree.
10409  *
10410  * This function is called only from sata_hba_attach(). It does not have to
10411  * be protected by controller mutex, because the hba_attached flag is not set
10412  * yet and no one would be touching this HBA instance other than this thread.
10413  * Determines if port is active and what type of the device is attached
10414  * (if any). Allocates necessary structures for each port.
10415  *
10416  * An AP (Attachement Point) node is created for each SATA device port even
10417  * when there is no device attached.
10418  */
10419 
10420 static 	void
10421 sata_probe_ports(sata_hba_inst_t *sata_hba_inst)
10422 {
10423 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
10424 	int			ncport;
10425 	sata_cport_info_t 	*cportinfo;
10426 	sata_drive_info_t	*drive;
10427 	sata_device_t		sata_device;
10428 	int			rval;
10429 	dev_t			minor_number;
10430 	char			name[16];
10431 	clock_t			start_time, cur_time;
10432 
10433 	/*
10434 	 * Probe controller ports first, to find port status and
10435 	 * any port multiplier attached.
10436 	 */
10437 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
10438 		/* allocate cport structure */
10439 		cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP);
10440 		ASSERT(cportinfo != NULL);
10441 		mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL);
10442 
10443 		mutex_enter(&cportinfo->cport_mutex);
10444 
10445 		cportinfo->cport_addr.cport = ncport;
10446 		cportinfo->cport_addr.pmport = 0;
10447 		cportinfo->cport_addr.qual = SATA_ADDR_CPORT;
10448 		cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
10449 		cportinfo->cport_state |= SATA_STATE_PROBING;
10450 		SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo;
10451 
10452 		/*
10453 		 * Regardless if a port is usable or not, create
10454 		 * an attachment point
10455 		 */
10456 		mutex_exit(&cportinfo->cport_mutex);
10457 		minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip),
10458 		    ncport, 0, SATA_ADDR_CPORT);
10459 		(void) sprintf(name, "%d", ncport);
10460 		if (ddi_create_minor_node(dip, name, S_IFCHR,
10461 		    minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) !=
10462 		    DDI_SUCCESS) {
10463 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
10464 			    "cannot create SATA attachment point for port %d",
10465 			    ncport);
10466 		}
10467 
10468 		/* Probe port */
10469 		start_time = ddi_get_lbolt();
10470 	reprobe_cport:
10471 		sata_device.satadev_addr.cport = ncport;
10472 		sata_device.satadev_addr.pmport = 0;
10473 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
10474 		sata_device.satadev_rev = SATA_DEVICE_REV;
10475 
10476 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10477 		    (dip, &sata_device);
10478 
10479 		mutex_enter(&cportinfo->cport_mutex);
10480 		cportinfo->cport_scr = sata_device.satadev_scr;
10481 		if (rval != SATA_SUCCESS) {
10482 			/* Something went wrong? Fail the port */
10483 			cportinfo->cport_state = SATA_PSTATE_FAILED;
10484 			mutex_exit(&cportinfo->cport_mutex);
10485 			continue;
10486 		}
10487 		cportinfo->cport_state &= ~SATA_STATE_PROBING;
10488 		cportinfo->cport_state |= SATA_STATE_PROBED;
10489 		cportinfo->cport_dev_type = sata_device.satadev_type;
10490 
10491 		cportinfo->cport_state |= SATA_STATE_READY;
10492 		if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) {
10493 			mutex_exit(&cportinfo->cport_mutex);
10494 			continue;
10495 		}
10496 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
10497 			/*
10498 			 * There is some device attached.
10499 			 * Allocate device info structure
10500 			 */
10501 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) {
10502 				mutex_exit(&cportinfo->cport_mutex);
10503 				SATA_CPORTINFO_DRV_INFO(cportinfo) =
10504 				    kmem_zalloc(sizeof (sata_drive_info_t),
10505 				    KM_SLEEP);
10506 				mutex_enter(&cportinfo->cport_mutex);
10507 			}
10508 			drive = SATA_CPORTINFO_DRV_INFO(cportinfo);
10509 			drive->satadrv_addr = cportinfo->cport_addr;
10510 			drive->satadrv_addr.qual = SATA_ADDR_DCPORT;
10511 			drive->satadrv_type = cportinfo->cport_dev_type;
10512 			drive->satadrv_state = SATA_STATE_UNKNOWN;
10513 
10514 			mutex_exit(&cportinfo->cport_mutex);
10515 			if (sata_add_device(dip, sata_hba_inst, &sata_device) !=
10516 			    SATA_SUCCESS) {
10517 				/*
10518 				 * Plugged device was not correctly identified.
10519 				 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT
10520 				 */
10521 				cur_time = ddi_get_lbolt();
10522 				if ((cur_time - start_time) <
10523 				    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
10524 					/* sleep for a while */
10525 					delay(drv_usectohz(
10526 					    SATA_DEV_RETRY_DLY));
10527 					goto reprobe_cport;
10528 				}
10529 			}
10530 		} else { /* SATA_DTYPE_PMULT */
10531 			mutex_exit(&cportinfo->cport_mutex);
10532 
10533 			/* Allocate sata_pmult_info and sata_pmport_info */
10534 			if (sata_alloc_pmult(sata_hba_inst, &sata_device) !=
10535 			    SATA_SUCCESS)
10536 				continue;
10537 
10538 			/* Log the information of the port multiplier */
10539 			sata_show_pmult_info(sata_hba_inst, &sata_device);
10540 
10541 			/* Probe its pmports */
10542 			sata_probe_pmports(sata_hba_inst, ncport);
10543 		}
10544 	}
10545 }
10546 
10547 /*
10548  * Probe all device ports behind a port multiplier.
10549  *
10550  * PMult-related structure should be allocated before by sata_alloc_pmult().
10551  *
10552  * NOTE1: Only called from sata_probe_ports()
10553  * NOTE2: No mutex should be hold.
10554  */
10555 static void
10556 sata_probe_pmports(sata_hba_inst_t *sata_hba_inst, uint8_t ncport)
10557 {
10558 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
10559 	sata_pmult_info_t	*pmultinfo = NULL;
10560 	sata_pmport_info_t 	*pmportinfo = NULL;
10561 	sata_drive_info_t	*drive = NULL;
10562 	sata_device_t		sata_device;
10563 
10564 	clock_t			start_time, cur_time;
10565 	int			npmport;
10566 	int			rval;
10567 
10568 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, ncport);
10569 
10570 	/* Probe Port Multiplier ports */
10571 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; npmport++) {
10572 		pmportinfo = pmultinfo->pmult_dev_port[npmport];
10573 		start_time = ddi_get_lbolt();
10574 reprobe_pmport:
10575 		sata_device.satadev_addr.cport = ncport;
10576 		sata_device.satadev_addr.pmport = npmport;
10577 		sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
10578 		sata_device.satadev_rev = SATA_DEVICE_REV;
10579 
10580 		/* Let HBA driver probe it. */
10581 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10582 		    (dip, &sata_device);
10583 		mutex_enter(&pmportinfo->pmport_mutex);
10584 
10585 		pmportinfo->pmport_scr = sata_device.satadev_scr;
10586 
10587 		if (rval != SATA_SUCCESS) {
10588 			pmportinfo->pmport_state =
10589 			    SATA_PSTATE_FAILED;
10590 			mutex_exit(&pmportinfo->pmport_mutex);
10591 			continue;
10592 		}
10593 		pmportinfo->pmport_state &= ~SATA_STATE_PROBING;
10594 		pmportinfo->pmport_state |= SATA_STATE_PROBED;
10595 		pmportinfo->pmport_dev_type = sata_device.satadev_type;
10596 
10597 		pmportinfo->pmport_state |= SATA_STATE_READY;
10598 		if (pmportinfo->pmport_dev_type ==
10599 		    SATA_DTYPE_NONE) {
10600 			SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
10601 			    "no device found at port %d:%d", ncport, npmport);
10602 			mutex_exit(&pmportinfo->pmport_mutex);
10603 			continue;
10604 		}
10605 		/* Port multipliers cannot be chained */
10606 		ASSERT(pmportinfo->pmport_dev_type != SATA_DTYPE_PMULT);
10607 		/*
10608 		 * There is something attached to Port
10609 		 * Multiplier device port
10610 		 * Allocate device info structure
10611 		 */
10612 		if (pmportinfo->pmport_sata_drive == NULL) {
10613 			mutex_exit(&pmportinfo->pmport_mutex);
10614 			pmportinfo->pmport_sata_drive =
10615 			    kmem_zalloc(sizeof (sata_drive_info_t), KM_SLEEP);
10616 			mutex_enter(&pmportinfo->pmport_mutex);
10617 		}
10618 		drive = pmportinfo->pmport_sata_drive;
10619 		drive->satadrv_addr.cport = pmportinfo->pmport_addr.cport;
10620 		drive->satadrv_addr.pmport = npmport;
10621 		drive->satadrv_addr.qual = SATA_ADDR_DPMPORT;
10622 		drive->satadrv_type = pmportinfo-> pmport_dev_type;
10623 		drive->satadrv_state = SATA_STATE_UNKNOWN;
10624 
10625 		mutex_exit(&pmportinfo->pmport_mutex);
10626 		rval = sata_add_device(dip, sata_hba_inst, &sata_device);
10627 
10628 		if (rval != SATA_SUCCESS) {
10629 			/*
10630 			 * Plugged device was not correctly identified.
10631 			 * Retry, within the SATA_DEV_IDENTIFY_TIMEOUT
10632 			 */
10633 			cur_time = ddi_get_lbolt();
10634 			if ((cur_time - start_time) < drv_usectohz(
10635 			    SATA_DEV_IDENTIFY_TIMEOUT)) {
10636 				/* sleep for a while */
10637 				delay(drv_usectohz(SATA_DEV_RETRY_DLY));
10638 				goto reprobe_pmport;
10639 			}
10640 		}
10641 	}
10642 }
10643 
10644 /*
10645  * Add SATA device for specified HBA instance & port (SCSI target
10646  * device nodes).
10647  * This function is called (indirectly) only from sata_hba_attach().
10648  * A target node is created when there is a supported type device attached,
10649  * but may be removed if it cannot be put online.
10650  *
10651  * This function cannot be called from an interrupt context.
10652  *
10653  * Create target nodes for disk, CD/DVD, Tape and ATAPI disk devices
10654  *
10655  * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when
10656  * device identification failed - adding a device could be retried.
10657  *
10658  */
10659 static 	int
10660 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst,
10661     sata_device_t *sata_device)
10662 {
10663 	sata_cport_info_t 	*cportinfo;
10664 	sata_pmult_info_t	*pminfo;
10665 	sata_pmport_info_t	*pmportinfo;
10666 	dev_info_t		*cdip;		/* child dip */
10667 	sata_address_t		*saddr = &sata_device->satadev_addr;
10668 	uint8_t			cport, pmport;
10669 	int			rval;
10670 
10671 	cport = saddr->cport;
10672 	pmport = saddr->pmport;
10673 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
10674 	ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE);
10675 
10676 	/*
10677 	 * Some device is attached to a controller port.
10678 	 * We rely on controllers distinquishing between no-device,
10679 	 * attached port multiplier and other kind of attached device.
10680 	 * We need to get Identify Device data and determine
10681 	 * positively the dev type before trying to attach
10682 	 * the target driver.
10683 	 */
10684 	sata_device->satadev_rev = SATA_DEVICE_REV;
10685 	switch (saddr->qual) {
10686 	case SATA_ADDR_CPORT:
10687 		/*
10688 		 * Add a non-port-multiplier device at controller port.
10689 		 */
10690 		saddr->qual = SATA_ADDR_DCPORT;
10691 
10692 		rval = sata_probe_device(sata_hba_inst, sata_device);
10693 		if (rval != SATA_SUCCESS ||
10694 		    sata_device->satadev_type == SATA_DTYPE_UNKNOWN)
10695 			return (SATA_FAILURE);
10696 
10697 		mutex_enter(&cportinfo->cport_mutex);
10698 		sata_show_drive_info(sata_hba_inst,
10699 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
10700 
10701 		if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) {
10702 			/*
10703 			 * Could not determine device type or
10704 			 * a device is not supported.
10705 			 * Degrade this device to unknown.
10706 			 */
10707 			cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
10708 			mutex_exit(&cportinfo->cport_mutex);
10709 			return (SATA_SUCCESS);
10710 		}
10711 		cportinfo->cport_dev_type = sata_device->satadev_type;
10712 		cportinfo->cport_tgtnode_clean = B_TRUE;
10713 		mutex_exit(&cportinfo->cport_mutex);
10714 
10715 		/*
10716 		 * Initialize device to the desired state. Even if it
10717 		 * fails, the device will still attach but syslog
10718 		 * will show the warning.
10719 		 */
10720 		if (sata_initialize_device(sata_hba_inst,
10721 		    SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) {
10722 			/* Retry */
10723 			rval = sata_initialize_device(sata_hba_inst,
10724 			    SATA_CPORTINFO_DRV_INFO(cportinfo));
10725 
10726 			if (rval == SATA_RETRY)
10727 				sata_log(sata_hba_inst, CE_WARN,
10728 				    "SATA device at port %d - "
10729 				    "default device features could not be set."
10730 				    " Device may not operate as expected.",
10731 				    cport);
10732 		}
10733 
10734 		cdip = sata_create_target_node(pdip, sata_hba_inst, saddr);
10735 		if (cdip == NULL) {
10736 			/*
10737 			 * Attaching target node failed.
10738 			 * We retain sata_drive_info structure...
10739 			 */
10740 			return (SATA_SUCCESS);
10741 		}
10742 
10743 		mutex_enter(&cportinfo->cport_mutex);
10744 		(SATA_CPORTINFO_DRV_INFO(cportinfo))->
10745 		    satadrv_state = SATA_STATE_READY;
10746 		mutex_exit(&cportinfo->cport_mutex);
10747 
10748 		break;
10749 
10750 	case SATA_ADDR_PMPORT:
10751 		saddr->qual = SATA_ADDR_DPMPORT;
10752 
10753 		mutex_enter(&cportinfo->cport_mutex);
10754 		/* It must be a Port Multiplier at the controller port */
10755 		ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT);
10756 
10757 		pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
10758 		pmportinfo = pminfo->pmult_dev_port[saddr->pmport];
10759 		mutex_exit(&cportinfo->cport_mutex);
10760 
10761 		rval = sata_probe_device(sata_hba_inst, sata_device);
10762 		if (rval != SATA_SUCCESS ||
10763 		    sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
10764 			return (SATA_FAILURE);
10765 		}
10766 
10767 		mutex_enter(&pmportinfo->pmport_mutex);
10768 		sata_show_drive_info(sata_hba_inst,
10769 		    SATA_PMPORTINFO_DRV_INFO(pmportinfo));
10770 
10771 		if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) {
10772 			/*
10773 			 * Could not determine device type.
10774 			 * Degrade this device to unknown.
10775 			 */
10776 			pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
10777 			mutex_exit(&pmportinfo->pmport_mutex);
10778 			return (SATA_SUCCESS);
10779 		}
10780 		pmportinfo->pmport_dev_type = sata_device->satadev_type;
10781 		pmportinfo->pmport_tgtnode_clean = B_TRUE;
10782 		mutex_exit(&pmportinfo->pmport_mutex);
10783 
10784 		/*
10785 		 * Initialize device to the desired state.
10786 		 * Even if it fails, the device will still
10787 		 * attach but syslog will show the warning.
10788 		 */
10789 		if (sata_initialize_device(sata_hba_inst,
10790 		    pmportinfo->pmport_sata_drive) != SATA_SUCCESS) {
10791 			/* Retry */
10792 			rval = sata_initialize_device(sata_hba_inst,
10793 			    pmportinfo->pmport_sata_drive);
10794 
10795 			if (rval == SATA_RETRY)
10796 				sata_log(sata_hba_inst, CE_WARN,
10797 				    "SATA device at port %d:%d - "
10798 				    "default device features could not be set."
10799 				    " Device may not operate as expected.",
10800 				    cport, pmport);
10801 		}
10802 
10803 		cdip = sata_create_target_node(pdip, sata_hba_inst, saddr);
10804 		if (cdip == NULL) {
10805 			/*
10806 			 * Attaching target node failed.
10807 			 * We retain sata_drive_info structure...
10808 			 */
10809 			return (SATA_SUCCESS);
10810 		}
10811 		mutex_enter(&pmportinfo->pmport_mutex);
10812 		pmportinfo->pmport_sata_drive->satadrv_state |=
10813 		    SATA_STATE_READY;
10814 		mutex_exit(&pmportinfo->pmport_mutex);
10815 
10816 		break;
10817 
10818 	default:
10819 		return (SATA_FAILURE);
10820 	}
10821 
10822 	return (SATA_SUCCESS);
10823 }
10824 
10825 /*
10826  * Clean up target node at specific address.
10827  *
10828  * NOTE: No Mutex should be hold.
10829  */
10830 static int
10831 sata_offline_device(sata_hba_inst_t *sata_hba_inst,
10832     sata_device_t *sata_device, sata_drive_info_t *sdinfo)
10833 {
10834 	uint8_t cport, pmport, qual;
10835 	dev_info_t *tdip;
10836 
10837 	cport = sata_device->satadev_addr.cport;
10838 	pmport = sata_device->satadev_addr.pmport;
10839 	qual = sata_device->satadev_addr.qual;
10840 
10841 	if (qual == SATA_ADDR_DCPORT) {
10842 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10843 		    "sata_hba_ioctl: disconnect device at port %d", cport));
10844 	} else {
10845 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10846 		    "sata_hba_ioctl: disconnect device at port %d:%d",
10847 		    cport, pmport));
10848 	}
10849 
10850 	/* We are addressing attached device, not a port */
10851 	sata_device->satadev_addr.qual =
10852 	    sdinfo->satadrv_addr.qual;
10853 	tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
10854 	    &sata_device->satadev_addr);
10855 	if (tdip != NULL && ndi_devi_offline(tdip,
10856 	    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
10857 		/*
10858 		 * Problem :
10859 		 * The target node remained attached.
10860 		 * This happens when the device file was open
10861 		 * or a node was waiting for resources.
10862 		 * Cannot do anything about it.
10863 		 */
10864 		if (qual == SATA_ADDR_DCPORT) {
10865 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10866 			    "sata_hba_ioctl: disconnect: could "
10867 			    "not unconfigure device before "
10868 			    "disconnecting the SATA port %d",
10869 			    cport));
10870 		} else {
10871 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10872 			    "sata_hba_ioctl: disconnect: could "
10873 			    "not unconfigure device before "
10874 			    "disconnecting the SATA port %d:%d",
10875 			    cport, pmport));
10876 		}
10877 		/*
10878 		 * Set DEVICE REMOVED state in the target
10879 		 * node. It will prevent access to the device
10880 		 * even when a new device is attached, until
10881 		 * the old target node is released, removed and
10882 		 * recreated for a new  device.
10883 		 */
10884 		sata_set_device_removed(tdip);
10885 
10886 		/*
10887 		 * Instruct event daemon to try the target
10888 		 * node cleanup later.
10889 		 */
10890 		sata_set_target_node_cleanup(
10891 		    sata_hba_inst, &sata_device->satadev_addr);
10892 	}
10893 
10894 
10895 	return (SATA_SUCCESS);
10896 }
10897 
10898 
10899 /*
10900  * Create scsi target node for attached device, create node properties and
10901  * attach the node.
10902  * The node could be removed if the device onlining fails.
10903  *
10904  * A dev_info_t pointer is returned if operation is successful, NULL is
10905  * returned otherwise.
10906  */
10907 
10908 static dev_info_t *
10909 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst,
10910 			sata_address_t *sata_addr)
10911 {
10912 	dev_info_t *cdip = NULL;
10913 	int rval;
10914 	char *nname = NULL;
10915 	char **compatible = NULL;
10916 	int ncompatible;
10917 	struct scsi_inquiry inq;
10918 	sata_device_t sata_device;
10919 	sata_drive_info_t *sdinfo;
10920 	int target;
10921 	int i;
10922 
10923 	sata_device.satadev_rev = SATA_DEVICE_REV;
10924 	sata_device.satadev_addr = *sata_addr;
10925 
10926 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport)));
10927 
10928 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
10929 
10930 	target = SATA_TO_SCSI_TARGET(sata_addr->cport,
10931 	    sata_addr->pmport, sata_addr->qual);
10932 
10933 	if (sdinfo == NULL) {
10934 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10935 		    sata_addr->cport)));
10936 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10937 		    "sata_create_target_node: no sdinfo for target %x",
10938 		    target));
10939 		return (NULL);
10940 	}
10941 
10942 	/*
10943 	 * create or get scsi inquiry data, expected by
10944 	 * scsi_hba_nodename_compatible_get()
10945 	 * SATA hard disks get Identify Data translated into Inguiry Data.
10946 	 * ATAPI devices respond directly to Inquiry request.
10947 	 */
10948 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10949 		sata_identdev_to_inquiry(sata_hba_inst, sdinfo,
10950 		    (uint8_t *)&inq);
10951 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10952 		    sata_addr->cport)));
10953 	} else { /* Assume supported ATAPI device */
10954 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10955 		    sata_addr->cport)));
10956 		if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr,
10957 		    &inq) == SATA_FAILURE)
10958 			return (NULL);
10959 		/*
10960 		 * Save supported ATAPI transport version
10961 		 */
10962 		sdinfo->satadrv_atapi_trans_ver =
10963 		    SATA_ATAPI_TRANS_VERSION(&inq);
10964 	}
10965 
10966 	/* determine the node name and compatible */
10967 	scsi_hba_nodename_compatible_get(&inq, NULL,
10968 	    inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
10969 
10970 #ifdef SATA_DEBUG
10971 	if (sata_debug_flags & SATA_DBG_NODES) {
10972 		if (nname == NULL) {
10973 			cmn_err(CE_NOTE, "sata_create_target_node: "
10974 			    "cannot determine nodename for target %d\n",
10975 			    target);
10976 		} else {
10977 			cmn_err(CE_WARN, "sata_create_target_node: "
10978 			    "target %d nodename: %s\n", target, nname);
10979 		}
10980 		if (compatible == NULL) {
10981 			cmn_err(CE_WARN,
10982 			    "sata_create_target_node: no compatible name\n");
10983 		} else {
10984 			for (i = 0; i < ncompatible; i++) {
10985 				cmn_err(CE_WARN, "sata_create_target_node: "
10986 				    "compatible name: %s\n", compatible[i]);
10987 			}
10988 		}
10989 	}
10990 #endif
10991 
10992 	/* if nodename can't be determined, log error and exit */
10993 	if (nname == NULL) {
10994 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10995 		    "sata_create_target_node: cannot determine nodename "
10996 		    "for target %d\n", target));
10997 		scsi_hba_nodename_compatible_free(nname, compatible);
10998 		return (NULL);
10999 	}
11000 	/*
11001 	 * Create scsi target node
11002 	 */
11003 	ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip);
11004 	rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
11005 	    "device-type", "scsi");
11006 
11007 	if (rval != DDI_PROP_SUCCESS) {
11008 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
11009 		    "updating device_type prop failed %d", rval));
11010 		goto fail;
11011 	}
11012 
11013 	/*
11014 	 * Create target node properties: target & lun
11015 	 */
11016 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target);
11017 	if (rval != DDI_PROP_SUCCESS) {
11018 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
11019 		    "updating target prop failed %d", rval));
11020 		goto fail;
11021 	}
11022 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0);
11023 	if (rval != DDI_PROP_SUCCESS) {
11024 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
11025 		    "updating target prop failed %d", rval));
11026 		goto fail;
11027 	}
11028 
11029 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
11030 		/*
11031 		 * Add "variant" property
11032 		 */
11033 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
11034 		    "variant", "atapi");
11035 		if (rval != DDI_PROP_SUCCESS) {
11036 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11037 			    "sata_create_target_node: variant atapi "
11038 			    "property could not be created: %d", rval));
11039 			goto fail;
11040 		}
11041 	}
11042 	/* decorate the node with compatible */
11043 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible",
11044 	    compatible, ncompatible) != DDI_PROP_SUCCESS) {
11045 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11046 		    "sata_create_target_node: FAIL compatible props cdip 0x%p",
11047 		    (void *)cdip));
11048 		goto fail;
11049 	}
11050 
11051 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
11052 		/*
11053 		 * Add "sata-phy" property
11054 		 */
11055 		if (ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "sata-phy",
11056 		    (int)sata_addr->cport) != DDI_PROP_SUCCESS) {
11057 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11058 			    "sata_create_target_node: failed to create "
11059 			    "\"sata-phy\" property: port %d",
11060 			    sata_addr->cport));
11061 		}
11062 	}
11063 
11064 
11065 	/*
11066 	 * Now, try to attach the driver. If probing of the device fails,
11067 	 * the target node may be removed
11068 	 */
11069 	rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH);
11070 
11071 	scsi_hba_nodename_compatible_free(nname, compatible);
11072 
11073 	if (rval == NDI_SUCCESS)
11074 		return (cdip);
11075 
11076 	/* target node was removed - are we sure? */
11077 	return (NULL);
11078 
11079 fail:
11080 	scsi_hba_nodename_compatible_free(nname, compatible);
11081 	ddi_prop_remove_all(cdip);
11082 	rval = ndi_devi_free(cdip);
11083 	if (rval != NDI_SUCCESS) {
11084 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
11085 		    "node removal failed %d", rval));
11086 	}
11087 	sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: "
11088 	    "cannot create target node for SATA device at port %d",
11089 	    sata_addr->cport);
11090 	return (NULL);
11091 }
11092 
11093 /*
11094  * Remove a target node.
11095  */
11096 static void
11097 sata_remove_target_node(sata_hba_inst_t *sata_hba_inst,
11098 			sata_address_t *sata_addr)
11099 {
11100 	dev_info_t *tdip;
11101 	uint8_t cport = sata_addr->cport;
11102 	uint8_t pmport = sata_addr->pmport;
11103 	uint8_t qual = sata_addr->qual;
11104 
11105 	/* Note the sata daemon uses the address of the port/pmport */
11106 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
11107 
11108 	/* Remove target node */
11109 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), cport, pmport);
11110 	if (tdip != NULL) {
11111 		/*
11112 		 * Target node exists.  Unconfigure device
11113 		 * then remove the target node (one ndi
11114 		 * operation).
11115 		 */
11116 		if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
11117 			/*
11118 			 * PROBLEM - no device, but target node remained. This
11119 			 * happens when the file was open or node was waiting
11120 			 * for resources.
11121 			 */
11122 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11123 			    "sata_remove_target_node: "
11124 			    "Failed to remove target node for "
11125 			    "detached SATA device."));
11126 			/*
11127 			 * Set target node state to DEVI_DEVICE_REMOVED. But
11128 			 * re-check first that the node still exists.
11129 			 */
11130 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
11131 			    cport, pmport);
11132 			if (tdip != NULL) {
11133 				sata_set_device_removed(tdip);
11134 				/*
11135 				 * Instruct event daemon to retry the cleanup
11136 				 * later.
11137 				 */
11138 				sata_set_target_node_cleanup(sata_hba_inst,
11139 				    sata_addr);
11140 			}
11141 		}
11142 
11143 		if (qual == SATA_ADDR_CPORT)
11144 			sata_log(sata_hba_inst, CE_WARN,
11145 			    "SATA device detached at port %d", cport);
11146 		else
11147 			sata_log(sata_hba_inst, CE_WARN,
11148 			    "SATA device detached at port %d:%d",
11149 			    cport, pmport);
11150 	}
11151 #ifdef SATA_DEBUG
11152 	else {
11153 		if (qual == SATA_ADDR_CPORT)
11154 			sata_log(sata_hba_inst, CE_WARN,
11155 			    "target node not found at port %d", cport);
11156 		else
11157 			sata_log(sata_hba_inst, CE_WARN,
11158 			    "target node not found at port %d:%d",
11159 			    cport, pmport);
11160 	}
11161 #endif
11162 }
11163 
11164 
11165 /*
11166  * Re-probe sata port, check for a device and attach info
11167  * structures when necessary. Identify Device data is fetched, if possible.
11168  * Assumption: sata address is already validated.
11169  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
11170  * the presence of a device and its type.
11171  *
11172  * flag arg specifies that the function should try multiple times to identify
11173  * device type and to initialize it, or it should return immediately on failure.
11174  * SATA_DEV_IDENTIFY_RETRY - retry
11175  * SATA_DEV_IDENTIFY_NORETRY - no retry
11176  *
11177  * SATA_FAILURE is returned if one of the operations failed.
11178  *
11179  * This function cannot be called in interrupt context - it may sleep.
11180  *
11181  * Note: Port multiplier is supported.
11182  */
11183 static int
11184 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11185     int flag)
11186 {
11187 	sata_cport_info_t *cportinfo;
11188 	sata_pmult_info_t *pmultinfo;
11189 	sata_drive_info_t *sdinfo, *osdinfo;
11190 	boolean_t init_device = B_FALSE;
11191 	int prev_device_type = SATA_DTYPE_NONE;
11192 	int prev_device_settings = 0;
11193 	int prev_device_state = 0;
11194 	clock_t start_time;
11195 	int retry = B_FALSE;
11196 	uint8_t cport = sata_device->satadev_addr.cport;
11197 	int rval_probe, rval_init;
11198 
11199 	/*
11200 	 * If target is pmport, sata_reprobe_pmport() will handle it.
11201 	 */
11202 	if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT ||
11203 	    sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT)
11204 		return (sata_reprobe_pmport(sata_hba_inst, sata_device, flag));
11205 
11206 	/* We only care about host sata cport for now */
11207 	cportinfo = SATA_CPORT_INFO(sata_hba_inst,
11208 	    sata_device->satadev_addr.cport);
11209 
11210 	/*
11211 	 * If a port multiplier was previously attached (we have no idea it
11212 	 * still there or not), sata_reprobe_pmult() will handle it.
11213 	 */
11214 	if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT)
11215 		return (sata_reprobe_pmult(sata_hba_inst, sata_device, flag));
11216 
11217 	/* Store sata_drive_info when a non-pmult device was attached. */
11218 	osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11219 	if (osdinfo != NULL) {
11220 		/*
11221 		 * We are re-probing port with a previously attached device.
11222 		 * Save previous device type and settings.
11223 		 */
11224 		prev_device_type = cportinfo->cport_dev_type;
11225 		prev_device_settings = osdinfo->satadrv_settings;
11226 		prev_device_state = osdinfo->satadrv_state;
11227 	}
11228 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
11229 		start_time = ddi_get_lbolt();
11230 		retry = B_TRUE;
11231 	}
11232 retry_probe:
11233 
11234 	/* probe port */
11235 	mutex_enter(&cportinfo->cport_mutex);
11236 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11237 	cportinfo->cport_state |= SATA_STATE_PROBING;
11238 	mutex_exit(&cportinfo->cport_mutex);
11239 
11240 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11241 	    (SATA_DIP(sata_hba_inst), sata_device);
11242 
11243 	mutex_enter(&cportinfo->cport_mutex);
11244 	if (rval_probe != SATA_SUCCESS) {
11245 		cportinfo->cport_state = SATA_PSTATE_FAILED;
11246 		mutex_exit(&cportinfo->cport_mutex);
11247 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: "
11248 		    "SATA port %d probing failed",
11249 		    cportinfo->cport_addr.cport));
11250 		return (SATA_FAILURE);
11251 	}
11252 
11253 	/*
11254 	 * update sata port state and set device type
11255 	 */
11256 	sata_update_port_info(sata_hba_inst, sata_device);
11257 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
11258 
11259 	/*
11260 	 * Sanity check - Port is active? Is the link active?
11261 	 * Is there any device attached?
11262 	 */
11263 	if ((cportinfo->cport_state &
11264 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11265 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11266 	    SATA_PORT_DEVLINK_UP) {
11267 		/*
11268 		 * Port in non-usable state or no link active/no device.
11269 		 * Free info structure if necessary (direct attached drive
11270 		 * only, for now!
11271 		 */
11272 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11273 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11274 		/* Add here differentiation for device attached or not */
11275 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11276 		mutex_exit(&cportinfo->cport_mutex);
11277 		if (sdinfo != NULL)
11278 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11279 		return (SATA_SUCCESS);
11280 	}
11281 
11282 	cportinfo->cport_state |= SATA_STATE_READY;
11283 	cportinfo->cport_state |= SATA_STATE_PROBED;
11284 
11285 	cportinfo->cport_dev_type = sata_device->satadev_type;
11286 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11287 
11288 	/*
11289 	 * If we are re-probing the port, there may be
11290 	 * sata_drive_info structure attached
11291 	 */
11292 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
11293 
11294 		/*
11295 		 * There is no device, so remove device info structure,
11296 		 * if necessary.
11297 		 */
11298 		/* Device change: Drive -> None */
11299 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11300 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11301 		if (sdinfo != NULL) {
11302 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11303 			sata_log(sata_hba_inst, CE_WARN,
11304 			    "SATA device detached "
11305 			    "from port %d", cportinfo->cport_addr.cport);
11306 		}
11307 		mutex_exit(&cportinfo->cport_mutex);
11308 		return (SATA_SUCCESS);
11309 
11310 	}
11311 
11312 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
11313 
11314 		/* Device (may) change: Drive -> Drive */
11315 		if (sdinfo == NULL) {
11316 			/*
11317 			 * There is some device attached, but there is
11318 			 * no sata_drive_info structure - allocate one
11319 			 */
11320 			mutex_exit(&cportinfo->cport_mutex);
11321 			sdinfo = kmem_zalloc(
11322 			    sizeof (sata_drive_info_t), KM_SLEEP);
11323 			mutex_enter(&cportinfo->cport_mutex);
11324 			/*
11325 			 * Recheck, that the port state did not change when we
11326 			 * released mutex.
11327 			 */
11328 			if (cportinfo->cport_state & SATA_STATE_READY) {
11329 				SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo;
11330 				sdinfo->satadrv_addr = cportinfo->cport_addr;
11331 				sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT;
11332 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11333 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
11334 			} else {
11335 				/*
11336 				 * Port is not in ready state, we
11337 				 * cannot attach a device.
11338 				 */
11339 				mutex_exit(&cportinfo->cport_mutex);
11340 				kmem_free(sdinfo, sizeof (sata_drive_info_t));
11341 				return (SATA_SUCCESS);
11342 			}
11343 			/*
11344 			 * Since we are adding device, presumably new one,
11345 			 * indicate that it  should be initalized,
11346 			 * as well as some internal framework states).
11347 			 */
11348 			init_device = B_TRUE;
11349 		}
11350 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11351 		sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
11352 	} else {
11353 		/* Device change: Drive -> PMult */
11354 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11355 		if (sdinfo != NULL) {
11356 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11357 			sata_log(sata_hba_inst, CE_WARN,
11358 			    "SATA device detached "
11359 			    "from port %d", cportinfo->cport_addr.cport);
11360 		}
11361 
11362 		sata_log(sata_hba_inst, CE_WARN,
11363 		    "SATA port multiplier detected at port %d",
11364 		    cportinfo->cport_addr.cport);
11365 
11366 		mutex_exit(&cportinfo->cport_mutex);
11367 		if (sata_alloc_pmult(sata_hba_inst, sata_device) !=
11368 		    SATA_SUCCESS)
11369 			return (SATA_FAILURE);
11370 		sata_show_pmult_info(sata_hba_inst, sata_device);
11371 		mutex_enter(&cportinfo->cport_mutex);
11372 
11373 		/*
11374 		 * Mark all the port multiplier port behind the port
11375 		 * multiplier behind with link events, so that the sata daemon
11376 		 * will update their status.
11377 		 */
11378 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
11379 		pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET;
11380 		mutex_exit(&cportinfo->cport_mutex);
11381 		return (SATA_SUCCESS);
11382 	}
11383 	mutex_exit(&cportinfo->cport_mutex);
11384 
11385 	/*
11386 	 * Figure out what kind of device we are really
11387 	 * dealing with. Failure of identifying device does not fail this
11388 	 * function.
11389 	 */
11390 	rval_probe = sata_probe_device(sata_hba_inst, sata_device);
11391 	rval_init = SATA_FAILURE;
11392 	mutex_enter(&cportinfo->cport_mutex);
11393 	if (rval_probe == SATA_SUCCESS) {
11394 		/*
11395 		 * If we are dealing with the same type of a device as before,
11396 		 * restore its settings flags.
11397 		 */
11398 		if (osdinfo != NULL &&
11399 		    sata_device->satadev_type == prev_device_type)
11400 			sdinfo->satadrv_settings = prev_device_settings;
11401 
11402 		mutex_exit(&cportinfo->cport_mutex);
11403 		rval_init = SATA_SUCCESS;
11404 		/* Set initial device features, if necessary */
11405 		if (init_device == B_TRUE) {
11406 			rval_init = sata_initialize_device(sata_hba_inst,
11407 			    sdinfo);
11408 		}
11409 		if (rval_init == SATA_SUCCESS)
11410 			return (rval_init);
11411 		/* else we will retry if retry was asked for */
11412 
11413 	} else {
11414 		/*
11415 		 * If there was some device info before we probe the device,
11416 		 * restore previous device setting, so we can retry from scratch
11417 		 * later. Providing, of course, that device has not disapear
11418 		 * during probing process.
11419 		 */
11420 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11421 			if (osdinfo != NULL) {
11422 				cportinfo->cport_dev_type = prev_device_type;
11423 				sdinfo->satadrv_type = prev_device_type;
11424 				sdinfo->satadrv_state = prev_device_state;
11425 			}
11426 		} else {
11427 			/* device is gone */
11428 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11429 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11430 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11431 			mutex_exit(&cportinfo->cport_mutex);
11432 			return (SATA_SUCCESS);
11433 		}
11434 		mutex_exit(&cportinfo->cport_mutex);
11435 	}
11436 
11437 	if (retry) {
11438 		clock_t cur_time = ddi_get_lbolt();
11439 		/*
11440 		 * A device was not successfully identified or initialized.
11441 		 * Track retry time for device identification.
11442 		 */
11443 		if ((cur_time - start_time) <
11444 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11445 			/* sleep for a while */
11446 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11447 			goto retry_probe;
11448 		}
11449 		/* else no more retries */
11450 		mutex_enter(&cportinfo->cport_mutex);
11451 		if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
11452 			if (rval_init == SATA_RETRY) {
11453 				/*
11454 				 * Setting drive features have failed, but
11455 				 * because the drive is still accessible,
11456 				 * keep it and emit a warning message.
11457 				 */
11458 				sata_log(sata_hba_inst, CE_WARN,
11459 				    "SATA device at port %d - desired "
11460 				    "drive features could not be set. "
11461 				    "Device may not operate as expected.",
11462 				    cportinfo->cport_addr.cport);
11463 			} else {
11464 				SATA_CPORTINFO_DRV_INFO(cportinfo)->
11465 				    satadrv_state = SATA_DSTATE_FAILED;
11466 			}
11467 		}
11468 		mutex_exit(&cportinfo->cport_mutex);
11469 	}
11470 	return (SATA_SUCCESS);
11471 }
11472 
11473 /*
11474  * Reprobe a controller port that connected to a port multiplier.
11475  *
11476  * NOTE: No Mutex should be hold.
11477  */
11478 static int
11479 sata_reprobe_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11480     int flag)
11481 {
11482 	_NOTE(ARGUNUSED(flag))
11483 	sata_cport_info_t *cportinfo;
11484 	sata_pmult_info_t *pmultinfo;
11485 	uint8_t cport = sata_device->satadev_addr.cport;
11486 	int rval_probe;
11487 
11488 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11489 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
11490 
11491 	/* probe port */
11492 	mutex_enter(&cportinfo->cport_mutex);
11493 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11494 	cportinfo->cport_state |= SATA_STATE_PROBING;
11495 	mutex_exit(&cportinfo->cport_mutex);
11496 
11497 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11498 	    (SATA_DIP(sata_hba_inst), sata_device);
11499 
11500 	mutex_enter(&cportinfo->cport_mutex);
11501 	if (rval_probe != SATA_SUCCESS) {
11502 		cportinfo->cport_state = SATA_PSTATE_FAILED;
11503 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmult: "
11504 		    "SATA port %d probing failed", cport));
11505 		sata_log(sata_hba_inst, CE_WARN,
11506 		    "SATA port multiplier detached at port %d", cport);
11507 		mutex_exit(&cportinfo->cport_mutex);
11508 		sata_free_pmult(sata_hba_inst, sata_device);
11509 		return (SATA_FAILURE);
11510 	}
11511 
11512 	/*
11513 	 * update sata port state and set device type
11514 	 */
11515 	sata_update_port_info(sata_hba_inst, sata_device);
11516 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
11517 	cportinfo->cport_state |= SATA_STATE_PROBED;
11518 
11519 	/*
11520 	 * Sanity check - Port is active? Is the link active?
11521 	 * Is there any device attached?
11522 	 */
11523 	if ((cportinfo->cport_state &
11524 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11525 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11526 	    SATA_PORT_DEVLINK_UP ||
11527 	    (sata_device->satadev_type == SATA_DTYPE_NONE)) {
11528 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11529 		mutex_exit(&cportinfo->cport_mutex);
11530 		sata_free_pmult(sata_hba_inst, sata_device);
11531 		sata_log(sata_hba_inst, CE_WARN,
11532 		    "SATA port multiplier detached at port %d", cport);
11533 		return (SATA_SUCCESS);
11534 	}
11535 
11536 	/*
11537 	 * Device changed: PMult -> Non-PMult
11538 	 *
11539 	 * This situation is uncommon, most possibly being caused by errors
11540 	 * after which the port multiplier is not correct initialized and
11541 	 * recognized. In that case the new device will be marked as unknown
11542 	 * and will not be automatically probed in this routine. Instead
11543 	 * system administrator could manually restart it via cfgadm(1M).
11544 	 */
11545 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
11546 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11547 		mutex_exit(&cportinfo->cport_mutex);
11548 		sata_free_pmult(sata_hba_inst, sata_device);
11549 		sata_log(sata_hba_inst, CE_WARN,
11550 		    "SATA port multiplier detached at port %d", cport);
11551 		return (SATA_FAILURE);
11552 	}
11553 
11554 	/*
11555 	 * Now we know it is a port multiplier. However, if this is not the
11556 	 * previously attached port multiplier - they may have different
11557 	 * pmport numbers - we need to re-allocate data structures for every
11558 	 * pmport and drive.
11559 	 *
11560 	 * Port multipliers of the same model have identical values in these
11561 	 * registers, so it is still necessary to update the information of
11562 	 * all drives attached to the previous port multiplier afterwards.
11563 	 */
11564 	/* Device changed: PMult -> another PMult */
11565 	mutex_exit(&cportinfo->cport_mutex);
11566 	sata_free_pmult(sata_hba_inst, sata_device);
11567 	if (sata_alloc_pmult(sata_hba_inst, sata_device) != SATA_SUCCESS)
11568 		return (SATA_FAILURE);
11569 	mutex_enter(&cportinfo->cport_mutex);
11570 
11571 	SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
11572 	    "SATA port multiplier [changed] at port %d", cport);
11573 	sata_log(sata_hba_inst, CE_WARN,
11574 	    "SATA port multiplier detected at port %d", cport);
11575 
11576 	/*
11577 	 * Mark all the port multiplier port behind the port
11578 	 * multiplier behind with link events, so that the sata daemon
11579 	 * will update their status.
11580 	 */
11581 	pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET;
11582 	mutex_exit(&cportinfo->cport_mutex);
11583 
11584 	return (SATA_SUCCESS);
11585 }
11586 
11587 /*
11588  * Re-probe a port multiplier port, check for a device and attach info
11589  * structures when necessary. Identify Device data is fetched, if possible.
11590  * Assumption: sata address is already validated as port multiplier port.
11591  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
11592  * the presence of a device and its type.
11593  *
11594  * flag arg specifies that the function should try multiple times to identify
11595  * device type and to initialize it, or it should return immediately on failure.
11596  * SATA_DEV_IDENTIFY_RETRY - retry
11597  * SATA_DEV_IDENTIFY_NORETRY - no retry
11598  *
11599  * SATA_FAILURE is returned if one of the operations failed.
11600  *
11601  * This function cannot be called in interrupt context - it may sleep.
11602  *
11603  * NOTE: Should be only called by sata_probe_port() in case target port is a
11604  *       port multiplier port.
11605  * NOTE: No Mutex should be hold.
11606  */
11607 static int
11608 sata_reprobe_pmport(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11609     int flag)
11610 {
11611 	sata_cport_info_t *cportinfo = NULL;
11612 	sata_pmport_info_t *pmportinfo = NULL;
11613 	sata_drive_info_t *sdinfo, *osdinfo;
11614 	sata_device_t sdevice;
11615 	boolean_t init_device = B_FALSE;
11616 	int prev_device_type = SATA_DTYPE_NONE;
11617 	int prev_device_settings = 0;
11618 	int prev_device_state = 0;
11619 	clock_t start_time;
11620 	uint8_t cport = sata_device->satadev_addr.cport;
11621 	uint8_t pmport = sata_device->satadev_addr.pmport;
11622 	int rval;
11623 
11624 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11625 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11626 	osdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11627 
11628 	if (osdinfo != NULL) {
11629 		/*
11630 		 * We are re-probing port with a previously attached device.
11631 		 * Save previous device type and settings.
11632 		 */
11633 		prev_device_type = pmportinfo->pmport_dev_type;
11634 		prev_device_settings = osdinfo->satadrv_settings;
11635 		prev_device_state = osdinfo->satadrv_state;
11636 	}
11637 
11638 	start_time = ddi_get_lbolt();
11639 
11640 	/* check parent status */
11641 	mutex_enter(&cportinfo->cport_mutex);
11642 	if ((cportinfo->cport_state &
11643 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11644 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11645 	    SATA_PORT_DEVLINK_UP) {
11646 		mutex_exit(&cportinfo->cport_mutex);
11647 		return (SATA_FAILURE);
11648 	}
11649 	mutex_exit(&cportinfo->cport_mutex);
11650 
11651 retry_probe_pmport:
11652 
11653 	/* probe port */
11654 	mutex_enter(&pmportinfo->pmport_mutex);
11655 	pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11656 	pmportinfo->pmport_state |= SATA_STATE_PROBING;
11657 	mutex_exit(&pmportinfo->pmport_mutex);
11658 
11659 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11660 	    (SATA_DIP(sata_hba_inst), sata_device);
11661 
11662 	/* might need retry because we cannot touch registers. */
11663 	if (rval == SATA_FAILURE) {
11664 		mutex_enter(&pmportinfo->pmport_mutex);
11665 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
11666 		mutex_exit(&pmportinfo->pmport_mutex);
11667 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: "
11668 		    "SATA port %d:%d probing failed",
11669 		    cport, pmport));
11670 		return (SATA_FAILURE);
11671 	} else if (rval == SATA_RETRY) {
11672 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: "
11673 		    "SATA port %d:%d probing failed, retrying...",
11674 		    cport, pmport));
11675 		clock_t cur_time = ddi_get_lbolt();
11676 		/*
11677 		 * A device was not successfully identified or initialized.
11678 		 * Track retry time for device identification.
11679 		 */
11680 		if ((cur_time - start_time) <
11681 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11682 			/* sleep for a while */
11683 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11684 			goto retry_probe_pmport;
11685 		} else {
11686 			mutex_enter(&pmportinfo->pmport_mutex);
11687 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL)
11688 				SATA_PMPORTINFO_DRV_INFO(pmportinfo)->
11689 				    satadrv_state = SATA_DSTATE_FAILED;
11690 			mutex_exit(&pmportinfo->pmport_mutex);
11691 			return (SATA_SUCCESS);
11692 		}
11693 	}
11694 
11695 	/*
11696 	 * Sanity check - Controller port is active? Is the link active?
11697 	 * Is it still a port multiplier?
11698 	 */
11699 	if ((cportinfo->cport_state &
11700 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11701 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11702 	    SATA_PORT_DEVLINK_UP ||
11703 	    (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) {
11704 		/*
11705 		 * Port in non-usable state or no link active/no
11706 		 * device. Free info structure.
11707 		 */
11708 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11709 
11710 		sdevice.satadev_addr.cport = cport;
11711 		sdevice.satadev_addr.pmport = pmport;
11712 		sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
11713 		mutex_exit(&cportinfo->cport_mutex);
11714 
11715 		sata_free_pmult(sata_hba_inst, &sdevice);
11716 		return (SATA_FAILURE);
11717 	}
11718 
11719 	/* SATA_SUCCESS NOW */
11720 	/*
11721 	 * update sata port state and set device type
11722 	 */
11723 	mutex_enter(&pmportinfo->pmport_mutex);
11724 	sata_update_pmport_info(sata_hba_inst, sata_device);
11725 	pmportinfo->pmport_state &= ~SATA_STATE_PROBING;
11726 
11727 	/*
11728 	 * Sanity check - Port is active? Is the link active?
11729 	 * Is there any device attached?
11730 	 */
11731 	if ((pmportinfo->pmport_state &
11732 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11733 	    (pmportinfo->pmport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11734 	    SATA_PORT_DEVLINK_UP) {
11735 		/*
11736 		 * Port in non-usable state or no link active/no device.
11737 		 * Free info structure if necessary (direct attached drive
11738 		 * only, for now!
11739 		 */
11740 		sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11741 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11742 		/* Add here differentiation for device attached or not */
11743 		pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11744 		mutex_exit(&pmportinfo->pmport_mutex);
11745 		if (sdinfo != NULL)
11746 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11747 		return (SATA_SUCCESS);
11748 	}
11749 
11750 	pmportinfo->pmport_state |= SATA_STATE_READY;
11751 	pmportinfo->pmport_dev_type = sata_device->satadev_type;
11752 	sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11753 
11754 	/*
11755 	 * If we are re-probing the port, there may be
11756 	 * sata_drive_info structure attached
11757 	 * (or sata_pm_info, if PMult is supported).
11758 	 */
11759 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
11760 		/*
11761 		 * There is no device, so remove device info structure,
11762 		 * if necessary.
11763 		 */
11764 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11765 		pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11766 		if (sdinfo != NULL) {
11767 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11768 			sata_log(sata_hba_inst, CE_WARN,
11769 			    "SATA device detached from port %d:%d",
11770 			    cport, pmport);
11771 		}
11772 		mutex_exit(&pmportinfo->pmport_mutex);
11773 		return (SATA_SUCCESS);
11774 	}
11775 
11776 	/* this should not be a pmult */
11777 	ASSERT(sata_device->satadev_type != SATA_DTYPE_PMULT);
11778 	if (sdinfo == NULL) {
11779 		/*
11780 		 * There is some device attached, but there is
11781 		 * no sata_drive_info structure - allocate one
11782 		 */
11783 		mutex_exit(&pmportinfo->pmport_mutex);
11784 		sdinfo = kmem_zalloc(sizeof (sata_drive_info_t),
11785 		    KM_SLEEP);
11786 		mutex_enter(&pmportinfo->pmport_mutex);
11787 		/*
11788 		 * Recheck, that the port state did not change when we
11789 		 * released mutex.
11790 		 */
11791 		if (pmportinfo->pmport_state & SATA_STATE_READY) {
11792 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = sdinfo;
11793 			sdinfo->satadrv_addr = pmportinfo->pmport_addr;
11794 			sdinfo->satadrv_addr.qual = SATA_ADDR_DPMPORT;
11795 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11796 			sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
11797 		} else {
11798 			/*
11799 			 * Port is not in ready state, we
11800 			 * cannot attach a device.
11801 			 */
11802 			mutex_exit(&pmportinfo->pmport_mutex);
11803 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11804 			return (SATA_SUCCESS);
11805 		}
11806 		/*
11807 		 * Since we are adding device, presumably new one,
11808 		 * indicate that it  should be initalized,
11809 		 * as well as some internal framework states).
11810 		 */
11811 		init_device = B_TRUE;
11812 	}
11813 
11814 	pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
11815 	sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
11816 
11817 	mutex_exit(&pmportinfo->pmport_mutex);
11818 	/*
11819 	 * Figure out what kind of device we are really
11820 	 * dealing with.
11821 	 */
11822 	rval = sata_probe_device(sata_hba_inst, sata_device);
11823 
11824 	mutex_enter(&pmportinfo->pmport_mutex);
11825 	if (rval == SATA_SUCCESS) {
11826 		/*
11827 		 * If we are dealing with the same type of a device as before,
11828 		 * restore its settings flags.
11829 		 */
11830 		if (osdinfo != NULL &&
11831 		    sata_device->satadev_type == prev_device_type)
11832 			sdinfo->satadrv_settings = prev_device_settings;
11833 
11834 		mutex_exit(&pmportinfo->pmport_mutex);
11835 		/* Set initial device features, if necessary */
11836 		if (init_device == B_TRUE) {
11837 			rval = sata_initialize_device(sata_hba_inst, sdinfo);
11838 		}
11839 		if (rval == SATA_SUCCESS)
11840 			return (rval);
11841 	} else {
11842 		/*
11843 		 * If there was some device info before we probe the device,
11844 		 * restore previous device setting, so we can retry from scratch
11845 		 * later. Providing, of course, that device has not disappeared
11846 		 * during probing process.
11847 		 */
11848 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11849 			if (osdinfo != NULL) {
11850 				pmportinfo->pmport_dev_type = prev_device_type;
11851 				sdinfo->satadrv_type = prev_device_type;
11852 				sdinfo->satadrv_state = prev_device_state;
11853 			}
11854 		} else {
11855 			/* device is gone */
11856 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11857 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11858 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11859 			mutex_exit(&pmportinfo->pmport_mutex);
11860 			return (SATA_SUCCESS);
11861 		}
11862 		mutex_exit(&pmportinfo->pmport_mutex);
11863 	}
11864 
11865 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
11866 		clock_t cur_time = ddi_get_lbolt();
11867 		/*
11868 		 * A device was not successfully identified or initialized.
11869 		 * Track retry time for device identification.
11870 		 */
11871 		if ((cur_time - start_time) <
11872 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11873 			/* sleep for a while */
11874 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11875 			goto retry_probe_pmport;
11876 		} else {
11877 			mutex_enter(&pmportinfo->pmport_mutex);
11878 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL)
11879 				SATA_PMPORTINFO_DRV_INFO(pmportinfo)->
11880 				    satadrv_state = SATA_DSTATE_FAILED;
11881 			mutex_exit(&pmportinfo->pmport_mutex);
11882 		}
11883 	}
11884 	return (SATA_SUCCESS);
11885 }
11886 
11887 /*
11888  * Allocated related structure for a port multiplier and its device ports
11889  *
11890  * Port multiplier should be ready and probed, and related information like
11891  * the number of the device ports should be store in sata_device_t.
11892  *
11893  * NOTE: No Mutex should be hold.
11894  */
11895 static int
11896 sata_alloc_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
11897 {
11898 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
11899 	sata_cport_info_t *cportinfo = NULL;
11900 	sata_pmult_info_t *pmultinfo = NULL;
11901 	sata_pmport_info_t *pmportinfo = NULL;
11902 	sata_device_t sd;
11903 	dev_t minor_number;
11904 	char name[16];
11905 	uint8_t cport = sata_device->satadev_addr.cport;
11906 	int rval;
11907 	int npmport;
11908 
11909 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11910 
11911 	/* This function might be called while a port-mult is hot-plugged. */
11912 	mutex_enter(&cportinfo->cport_mutex);
11913 
11914 	/* dev_type's not updated when get called from sata_reprobe_port() */
11915 	if (SATA_CPORTINFO_PMULT_INFO(cportinfo) == NULL) {
11916 		/* Create a pmult_info structure */
11917 		SATA_CPORTINFO_PMULT_INFO(cportinfo) =
11918 		    kmem_zalloc(sizeof (sata_pmult_info_t), KM_SLEEP);
11919 	}
11920 	pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
11921 
11922 	pmultinfo->pmult_addr = sata_device->satadev_addr;
11923 	pmultinfo->pmult_addr.qual = SATA_ADDR_PMULT;
11924 	pmultinfo->pmult_state = SATA_STATE_PROBING;
11925 
11926 	/*
11927 	 * Probe the port multiplier with qualifier SATA_ADDR_PMULT_SPEC,
11928 	 * The HBA driver should initialize and register the port multiplier,
11929 	 * sata_register_pmult() will fill following fields,
11930 	 *   + sata_pmult_info.pmult_gscr
11931 	 *   + sata_pmult_info.pmult_num_dev_ports
11932 	 */
11933 	sd.satadev_addr = sata_device->satadev_addr;
11934 	sd.satadev_addr.qual = SATA_ADDR_PMULT_SPEC;
11935 	mutex_exit(&cportinfo->cport_mutex);
11936 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11937 	    (SATA_DIP(sata_hba_inst), &sd);
11938 	mutex_enter(&cportinfo->cport_mutex);
11939 
11940 	if (rval != SATA_SUCCESS ||
11941 	    (sd.satadev_type != SATA_DTYPE_PMULT) ||
11942 	    !(sd.satadev_state & SATA_DSTATE_PMULT_INIT)) {
11943 		SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL;
11944 		kmem_free(pmultinfo, sizeof (sata_pmult_info_t));
11945 		cportinfo->cport_state = SATA_PSTATE_FAILED;
11946 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11947 		mutex_exit(&cportinfo->cport_mutex);
11948 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
11949 		    "sata_alloc_pmult: failed to initialize pmult "
11950 		    "at port %d.", cport)
11951 		return (SATA_FAILURE);
11952 	}
11953 
11954 	/* Initialize pmport_info structure */
11955 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports;
11956 	    npmport++) {
11957 
11958 		/* if everything is allocated, skip */
11959 		if (SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) != NULL)
11960 			continue;
11961 
11962 		pmportinfo = kmem_zalloc(sizeof (sata_pmport_info_t), KM_SLEEP);
11963 		mutex_init(&pmportinfo->pmport_mutex, NULL, MUTEX_DRIVER, NULL);
11964 		mutex_exit(&cportinfo->cport_mutex);
11965 
11966 		mutex_enter(&pmportinfo->pmport_mutex);
11967 		pmportinfo->pmport_addr.cport = cport;
11968 		pmportinfo->pmport_addr.pmport = (uint8_t)npmport;
11969 		pmportinfo->pmport_addr.qual = SATA_ADDR_PMPORT;
11970 		pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11971 		mutex_exit(&pmportinfo->pmport_mutex);
11972 
11973 		mutex_enter(&cportinfo->cport_mutex);
11974 		SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) = pmportinfo;
11975 
11976 		/* Create an attachment point */
11977 		minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip),
11978 		    cport, (uint8_t)npmport, SATA_ADDR_PMPORT);
11979 		(void) sprintf(name, "%d.%d", cport, npmport);
11980 
11981 		if (ddi_create_minor_node(dip, name, S_IFCHR, minor_number,
11982 		    DDI_NT_SATA_ATTACHMENT_POINT, 0) != DDI_SUCCESS) {
11983 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
11984 			    "cannot create SATA attachment point for "
11985 			    "port %d:%d", cport, npmport);
11986 		}
11987 	}
11988 
11989 	pmultinfo->pmult_state &= ~SATA_STATE_PROBING;
11990 	pmultinfo->pmult_state |= (SATA_STATE_PROBED|SATA_STATE_READY);
11991 	cportinfo->cport_dev_type = SATA_DTYPE_PMULT;
11992 
11993 	mutex_exit(&cportinfo->cport_mutex);
11994 	return (SATA_SUCCESS);
11995 }
11996 
11997 /*
11998  * Free data structures when a port multiplier is removed.
11999  *
12000  * NOTE: No Mutex should be hold.
12001  */
12002 static void
12003 sata_free_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
12004 {
12005 	sata_cport_info_t *cportinfo;
12006 	sata_pmult_info_t *pmultinfo;
12007 	sata_pmport_info_t *pmportinfo;
12008 	sata_device_t pmport_device;
12009 	sata_drive_info_t *sdinfo;
12010 	dev_info_t *tdip;
12011 	char name[16];
12012 	uint8_t cport = sata_device->satadev_addr.cport;
12013 	int npmport;
12014 
12015 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
12016 
12017 	/* This function might be called while port-mult is hot plugged. */
12018 	mutex_enter(&cportinfo->cport_mutex);
12019 
12020 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
12021 	pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
12022 	ASSERT(pmultinfo != NULL);
12023 
12024 	/* Free pmport_info structure */
12025 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports;
12026 	    npmport++) {
12027 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport);
12028 		if (pmportinfo == NULL)
12029 			continue;
12030 		mutex_exit(&cportinfo->cport_mutex);
12031 
12032 		mutex_enter(&pmportinfo->pmport_mutex);
12033 		sdinfo = pmportinfo->pmport_sata_drive;
12034 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
12035 		mutex_exit(&pmportinfo->pmport_mutex);
12036 
12037 		/* Remove attachment point. */
12038 		name[0] = '\0';
12039 		(void) sprintf(name, "%d.%d", cport, npmport);
12040 		ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name);
12041 		sata_log(sata_hba_inst, CE_NOTE,
12042 		    "Remove attachment point of port %d:%d",
12043 		    cport, npmport);
12044 
12045 		/*
12046 		 * Rumove target node
12047 		 */
12048 		bzero(&pmport_device, sizeof (sata_device_t));
12049 		pmport_device.satadev_rev = SATA_DEVICE_REV;
12050 		pmport_device.satadev_addr.cport = cport;
12051 		pmport_device.satadev_addr.pmport = (uint8_t)npmport;
12052 		pmport_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
12053 
12054 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
12055 		    &(pmport_device.satadev_addr));
12056 		if (tdip != NULL && ndi_devi_offline(tdip,
12057 		    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
12058 			/*
12059 			 * Problem :
12060 			 * The target node remained attached.
12061 			 * This happens when the device file was open
12062 			 * or a node was waiting for resources.
12063 			 * Cannot do anything about it.
12064 			 */
12065 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12066 			    "sata_free_pmult: could not unconfigure device "
12067 			    "before disconnecting the SATA port %d:%d",
12068 			    cport, npmport));
12069 
12070 			/*
12071 			 * Set DEVICE REMOVED state in the target
12072 			 * node. It will prevent access to the device
12073 			 * even when a new device is attached, until
12074 			 * the old target node is released, removed and
12075 			 * recreated for a new  device.
12076 			 */
12077 			sata_set_device_removed(tdip);
12078 
12079 			/*
12080 			 * Instruct event daemon to try the target
12081 			 * node cleanup later.
12082 			 */
12083 			sata_set_target_node_cleanup(
12084 			    sata_hba_inst, &(pmport_device.satadev_addr));
12085 
12086 		}
12087 		mutex_enter(&cportinfo->cport_mutex);
12088 
12089 		/*
12090 		 * Add here differentiation for device attached or not
12091 		 */
12092 		if (sdinfo != NULL)  {
12093 			sata_log(sata_hba_inst, CE_WARN,
12094 			    "SATA device detached from port %d:%d",
12095 			    cport, npmport);
12096 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
12097 		}
12098 
12099 		mutex_destroy(&pmportinfo->pmport_mutex);
12100 		kmem_free(pmportinfo, sizeof (sata_pmport_info_t));
12101 	}
12102 
12103 	kmem_free(pmultinfo, sizeof (sata_pmult_info_t));
12104 
12105 	cportinfo->cport_devp.cport_sata_pmult = NULL;
12106 
12107 	sata_log(sata_hba_inst, CE_WARN,
12108 	    "SATA port multiplier detached at port %d", cport);
12109 
12110 	mutex_exit(&cportinfo->cport_mutex);
12111 }
12112 
12113 /*
12114  * Initialize device
12115  * Specified device is initialized to a default state.
12116  *
12117  * Returns SATA_SUCCESS if all device features are set successfully,
12118  * SATA_RETRY if device is accessible but device features were not set
12119  * successfully, and SATA_FAILURE otherwise.
12120  */
12121 static int
12122 sata_initialize_device(sata_hba_inst_t *sata_hba_inst,
12123     sata_drive_info_t *sdinfo)
12124 {
12125 	int rval;
12126 
12127 	sata_save_drive_settings(sdinfo);
12128 
12129 	sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
12130 
12131 	sata_init_write_cache_mode(sdinfo);
12132 
12133 	rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0);
12134 
12135 	/* Determine current data transfer mode */
12136 	if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) {
12137 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
12138 	} else if ((sdinfo->satadrv_id.ai_validinfo &
12139 	    SATA_VALIDINFO_88) != 0 &&
12140 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) {
12141 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
12142 	} else if ((sdinfo->satadrv_id.ai_dworddma &
12143 	    SATA_MDMA_SEL_MASK) != 0) {
12144 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
12145 	} else
12146 		/* DMA supported, not no DMA transfer mode is selected !? */
12147 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
12148 
12149 	if ((sdinfo->satadrv_id.ai_cmdset83 & 0x20) &&
12150 	    (sdinfo->satadrv_id.ai_features86 & 0x20))
12151 		sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
12152 	else
12153 		sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
12154 
12155 	return (rval);
12156 }
12157 
12158 
12159 /*
12160  * Initialize write cache mode.
12161  *
12162  * The default write cache setting for SATA HDD is provided by sata_write_cache
12163  * static variable. ATAPI CD/DVDs devices have write cache default is
12164  * determined by sata_atapicdvd_write_cache static variable.
12165  * ATAPI tape devices have write cache default is determined by
12166  * sata_atapitape_write_cache static variable.
12167  * ATAPI disk devices have write cache default is determined by
12168  * sata_atapidisk_write_cache static variable.
12169  * 1 - enable
12170  * 0 - disable
12171  * any other value - current drive setting
12172  *
12173  * Although there is not reason to disable write cache on CD/DVD devices,
12174  * tape devices and ATAPI disk devices, the default setting control is provided
12175  * for the maximun flexibility.
12176  *
12177  * In the future, it may be overridden by the
12178  * disk-write-cache-enable property setting, if it is defined.
12179  * Returns SATA_SUCCESS if all device features are set successfully,
12180  * SATA_FAILURE otherwise.
12181  */
12182 static void
12183 sata_init_write_cache_mode(sata_drive_info_t *sdinfo)
12184 {
12185 	switch (sdinfo->satadrv_type) {
12186 	case SATA_DTYPE_ATADISK:
12187 		if (sata_write_cache == 1)
12188 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12189 		else if (sata_write_cache == 0)
12190 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12191 		/*
12192 		 * When sata_write_cache value is not 0 or 1,
12193 		 * a current setting of the drive's write cache is used.
12194 		 */
12195 		break;
12196 	case SATA_DTYPE_ATAPICD:
12197 		if (sata_atapicdvd_write_cache == 1)
12198 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12199 		else if (sata_atapicdvd_write_cache == 0)
12200 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12201 		/*
12202 		 * When sata_atapicdvd_write_cache value is not 0 or 1,
12203 		 * a current setting of the drive's write cache is used.
12204 		 */
12205 		break;
12206 	case SATA_DTYPE_ATAPITAPE:
12207 		if (sata_atapitape_write_cache == 1)
12208 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12209 		else if (sata_atapitape_write_cache == 0)
12210 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12211 		/*
12212 		 * When sata_atapitape_write_cache value is not 0 or 1,
12213 		 * a current setting of the drive's write cache is used.
12214 		 */
12215 		break;
12216 	case SATA_DTYPE_ATAPIDISK:
12217 		if (sata_atapidisk_write_cache == 1)
12218 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12219 		else if (sata_atapidisk_write_cache == 0)
12220 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12221 		/*
12222 		 * When sata_atapidisk_write_cache value is not 0 or 1,
12223 		 * a current setting of the drive's write cache is used.
12224 		 */
12225 		break;
12226 	}
12227 }
12228 
12229 
12230 /*
12231  * Validate sata address.
12232  * Specified cport, pmport and qualifier has to match
12233  * passed sata_scsi configuration info.
12234  * The presence of an attached device is not verified.
12235  *
12236  * Returns 0 when address is valid, -1 otherwise.
12237  */
12238 static int
12239 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport,
12240 	int pmport, int qual)
12241 {
12242 	if (qual == SATA_ADDR_DCPORT && pmport != 0)
12243 		goto invalid_address;
12244 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
12245 		goto invalid_address;
12246 	if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) &&
12247 	    ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) ||
12248 	    (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) ||
12249 	    (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport))))
12250 		goto invalid_address;
12251 
12252 	return (0);
12253 
12254 invalid_address:
12255 	return (-1);
12256 
12257 }
12258 
12259 /*
12260  * Validate scsi address
12261  * SCSI target address is translated into SATA cport/pmport and compared
12262  * with a controller port/device configuration. LUN has to be 0.
12263  * Returns 0 if a scsi target refers to an attached device,
12264  * returns 1 if address is valid but no valid device is attached,
12265  * returns 2 if address is valid but device type is unknown (not valid device),
12266  * returns -1 if bad address or device is of an unsupported type.
12267  * Upon return sata_device argument is set.
12268  *
12269  * Port multiplier is supported now.
12270  */
12271 static int
12272 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst,
12273 	struct scsi_address *ap, sata_device_t *sata_device)
12274 {
12275 	int cport, pmport, qual, rval;
12276 
12277 	rval = -1;	/* Invalid address */
12278 	if (ap->a_lun != 0)
12279 		goto out;
12280 
12281 	qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
12282 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
12283 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
12284 
12285 	if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT)
12286 		goto out;
12287 
12288 	if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) ==
12289 	    0) {
12290 
12291 		sata_cport_info_t *cportinfo;
12292 		sata_pmult_info_t *pmultinfo;
12293 		sata_drive_info_t *sdinfo = NULL;
12294 
12295 		sata_device->satadev_addr.qual = qual;
12296 		sata_device->satadev_addr.cport = cport;
12297 		sata_device->satadev_addr.pmport = pmport;
12298 		sata_device->satadev_rev = SATA_DEVICE_REV_1;
12299 
12300 		rval = 1;	/* Valid sata address */
12301 
12302 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
12303 		if (qual == SATA_ADDR_DCPORT) {
12304 			if (cportinfo == NULL ||
12305 			    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
12306 				goto out;
12307 
12308 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
12309 			if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN &&
12310 			    sdinfo != NULL) {
12311 				rval = 2;
12312 				goto out;
12313 			}
12314 
12315 			if ((cportinfo->cport_dev_type &
12316 			    SATA_VALID_DEV_TYPE) == 0) {
12317 				rval = -1;
12318 				goto out;
12319 			}
12320 
12321 		} else if (qual == SATA_ADDR_DPMPORT) {
12322 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
12323 			if (pmultinfo == NULL) {
12324 				rval = -1;
12325 				goto out;
12326 			}
12327 			if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) ==
12328 			    NULL ||
12329 			    SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12330 			    pmport) == SATA_DTYPE_NONE)
12331 				goto out;
12332 
12333 			sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport,
12334 			    pmport);
12335 			if (SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12336 			    pmport) == SATA_DTYPE_UNKNOWN && sdinfo != NULL) {
12337 				rval = 2;
12338 				goto out;
12339 			}
12340 
12341 			if ((SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12342 			    pmport) & SATA_VALID_DEV_TYPE) == 0) {
12343 				rval = -1;
12344 				goto out;
12345 			}
12346 
12347 		} else {
12348 			rval = -1;
12349 			goto out;
12350 		}
12351 		if ((sdinfo == NULL) ||
12352 		    (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0)
12353 			goto out;
12354 
12355 		sata_device->satadev_type = sdinfo->satadrv_type;
12356 
12357 		return (0);
12358 	}
12359 out:
12360 	if (rval > 0) {
12361 		SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
12362 		    "sata_validate_scsi_address: no valid target %x lun %x",
12363 		    ap->a_target, ap->a_lun);
12364 	}
12365 	return (rval);
12366 }
12367 
12368 /*
12369  * Find dip corresponding to passed device number
12370  *
12371  * Returns NULL if invalid device number is passed or device cannot be found,
12372  * Returns dip is device is found.
12373  */
12374 static dev_info_t *
12375 sata_devt_to_devinfo(dev_t dev)
12376 {
12377 	dev_info_t *dip;
12378 #ifndef __lock_lint
12379 	struct devnames *dnp;
12380 	major_t major = getmajor(dev);
12381 	int instance = SATA_MINOR2INSTANCE(getminor(dev));
12382 
12383 	if (major >= devcnt)
12384 		return (NULL);
12385 
12386 	dnp = &devnamesp[major];
12387 	LOCK_DEV_OPS(&(dnp->dn_lock));
12388 	dip = dnp->dn_head;
12389 	while (dip && (ddi_get_instance(dip) != instance)) {
12390 		dip = ddi_get_next(dip);
12391 	}
12392 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
12393 #endif
12394 
12395 	return (dip);
12396 }
12397 
12398 
12399 /*
12400  * Probe device.
12401  * This function issues Identify Device command and initializes local
12402  * sata_drive_info structure if the device can be identified.
12403  * The device type is determined by examining Identify Device
12404  * command response.
12405  * If the sata_hba_inst has linked drive info structure for this
12406  * device address, the Identify Device data is stored into sata_drive_info
12407  * structure linked to the port info structure.
12408  *
12409  * sata_device has to refer to the valid sata port(s) for HBA described
12410  * by sata_hba_inst structure.
12411  *
12412  * Returns:
12413  *	SATA_SUCCESS if device type was successfully probed and port-linked
12414  *		drive info structure was updated;
12415  * 	SATA_FAILURE if there is no device, or device was not probed
12416  *		successully;
12417  *	SATA_RETRY if device probe can be retried later.
12418  * If a device cannot be identified, sata_device's dev_state and dev_type
12419  * fields are set to unknown.
12420  * There are no retries in this function. Any retries should be managed by
12421  * the caller.
12422  */
12423 
12424 
12425 static int
12426 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
12427 {
12428 	sata_pmport_info_t *pmportinfo;
12429 	sata_drive_info_t *sdinfo;
12430 	sata_drive_info_t new_sdinfo;	/* local drive info struct */
12431 	int rval;
12432 
12433 	ASSERT((SATA_CPORT_STATE(sata_hba_inst,
12434 	    sata_device->satadev_addr.cport) &
12435 	    (SATA_STATE_PROBED | SATA_STATE_READY)) != 0);
12436 
12437 	sata_device->satadev_type = SATA_DTYPE_NONE;
12438 
12439 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12440 	    sata_device->satadev_addr.cport)));
12441 
12442 	if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT) {
12443 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
12444 		    sata_device->satadev_addr.cport,
12445 		    sata_device->satadev_addr.pmport);
12446 		ASSERT(pmportinfo != NULL);
12447 	}
12448 
12449 	/* Get pointer to port-linked sata device info structure */
12450 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12451 	if (sdinfo != NULL) {
12452 		sdinfo->satadrv_state &=
12453 		    ~(SATA_STATE_PROBED | SATA_STATE_READY);
12454 		sdinfo->satadrv_state |= SATA_STATE_PROBING;
12455 	} else {
12456 		/* No device to probe */
12457 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12458 		    sata_device->satadev_addr.cport)));
12459 		sata_device->satadev_type = SATA_DTYPE_NONE;
12460 		sata_device->satadev_state = SATA_STATE_UNKNOWN;
12461 		return (SATA_FAILURE);
12462 	}
12463 	/*
12464 	 * Need to issue both types of identify device command and
12465 	 * determine device type by examining retreived data/status.
12466 	 * First, ATA Identify Device.
12467 	 */
12468 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
12469 	new_sdinfo.satadrv_addr = sata_device->satadev_addr;
12470 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12471 	    sata_device->satadev_addr.cport)));
12472 	new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
12473 	rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
12474 	if (rval == SATA_RETRY) {
12475 		/* We may try to check for ATAPI device */
12476 		if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) {
12477 			/*
12478 			 * HBA supports ATAPI - try to issue Identify Packet
12479 			 * Device command.
12480 			 */
12481 			new_sdinfo.satadrv_type = SATA_DTYPE_ATAPI;
12482 			rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
12483 		}
12484 	}
12485 	if (rval == SATA_SUCCESS) {
12486 		/*
12487 		 * Got something responding positively to ATA Identify Device
12488 		 * or to Identify Packet Device cmd.
12489 		 * Save last used device type.
12490 		 */
12491 		sata_device->satadev_type = new_sdinfo.satadrv_type;
12492 
12493 		/* save device info, if possible */
12494 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12495 		    sata_device->satadev_addr.cport)));
12496 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12497 		if (sdinfo == NULL) {
12498 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12499 			    sata_device->satadev_addr.cport)));
12500 			return (SATA_FAILURE);
12501 		}
12502 		/*
12503 		 * Copy drive info into the port-linked drive info structure.
12504 		 */
12505 		*sdinfo = new_sdinfo;
12506 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
12507 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
12508 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
12509 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
12510 			    sata_device->satadev_addr.cport) =
12511 			    sdinfo->satadrv_type;
12512 		else { /* SATA_ADDR_DPMPORT */
12513 			mutex_enter(&pmportinfo->pmport_mutex);
12514 			SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12515 			    sata_device->satadev_addr.cport,
12516 			    sata_device->satadev_addr.pmport) =
12517 			    sdinfo->satadrv_type;
12518 			mutex_exit(&pmportinfo->pmport_mutex);
12519 		}
12520 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12521 		    sata_device->satadev_addr.cport)));
12522 		return (SATA_SUCCESS);
12523 	}
12524 
12525 	/*
12526 	 * It may be SATA_RETRY or SATA_FAILURE return.
12527 	 * Looks like we cannot determine the device type at this time.
12528 	 */
12529 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12530 	    sata_device->satadev_addr.cport)));
12531 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12532 	if (sdinfo != NULL) {
12533 		sata_device->satadev_type = SATA_DTYPE_UNKNOWN;
12534 		sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12535 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
12536 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
12537 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
12538 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
12539 			    sata_device->satadev_addr.cport) =
12540 			    SATA_DTYPE_UNKNOWN;
12541 		else {
12542 			/* SATA_ADDR_DPMPORT */
12543 			mutex_enter(&pmportinfo->pmport_mutex);
12544 			if ((SATA_PMULT_INFO(sata_hba_inst,
12545 			    sata_device->satadev_addr.cport) != NULL) &&
12546 			    (SATA_PMPORT_INFO(sata_hba_inst,
12547 			    sata_device->satadev_addr.cport,
12548 			    sata_device->satadev_addr.pmport) != NULL))
12549 				SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12550 				    sata_device->satadev_addr.cport,
12551 				    sata_device->satadev_addr.pmport) =
12552 				    SATA_DTYPE_UNKNOWN;
12553 			mutex_exit(&pmportinfo->pmport_mutex);
12554 		}
12555 	}
12556 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12557 	    sata_device->satadev_addr.cport)));
12558 	return (rval);
12559 }
12560 
12561 
12562 /*
12563  * Get pointer to sata_drive_info structure.
12564  *
12565  * The sata_device has to contain address (cport, pmport and qualifier) for
12566  * specified sata_scsi structure.
12567  *
12568  * Returns NULL if device address is not valid for this HBA configuration.
12569  * Otherwise, returns a pointer to sata_drive_info structure.
12570  *
12571  * This function should be called with a port mutex held.
12572  */
12573 static sata_drive_info_t *
12574 sata_get_device_info(sata_hba_inst_t *sata_hba_inst,
12575     sata_device_t *sata_device)
12576 {
12577 	uint8_t cport = sata_device->satadev_addr.cport;
12578 	uint8_t pmport = sata_device->satadev_addr.pmport;
12579 	uint8_t qual = sata_device->satadev_addr.qual;
12580 
12581 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
12582 		return (NULL);
12583 
12584 	if (!(SATA_CPORT_STATE(sata_hba_inst, cport) &
12585 	    (SATA_STATE_PROBED | SATA_STATE_READY)))
12586 		/* Port not probed yet */
12587 		return (NULL);
12588 
12589 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE)
12590 		return (NULL);
12591 
12592 	if (qual == SATA_ADDR_DCPORT) {
12593 		/* Request for a device on a controller port */
12594 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
12595 		    SATA_DTYPE_PMULT)
12596 			/* Port multiplier attached */
12597 			return (NULL);
12598 		return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport));
12599 	}
12600 	if (qual == SATA_ADDR_DPMPORT) {
12601 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
12602 		    SATA_DTYPE_PMULT)
12603 			return (NULL);
12604 
12605 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport))
12606 			return (NULL);
12607 
12608 		if (!(SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) &
12609 		    (SATA_STATE_PROBED | SATA_STATE_READY)))
12610 			/* Port multiplier port not probed yet */
12611 			return (NULL);
12612 
12613 		return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport));
12614 	}
12615 
12616 	/* we should not get here */
12617 	return (NULL);
12618 }
12619 
12620 
12621 /*
12622  * sata_identify_device.
12623  * Send Identify Device command to SATA HBA driver.
12624  * If command executes successfully, update sata_drive_info structure pointed
12625  * to by sdinfo argument, including Identify Device data.
12626  * If command fails, invalidate data in sata_drive_info.
12627  *
12628  * Cannot be called from interrupt level.
12629  *
12630  * Returns:
12631  * SATA_SUCCESS if the device was identified as a supported device,
12632  * SATA_RETRY if the device was not identified but could be retried,
12633  * SATA_FAILURE if the device was not identified and identify attempt
12634  *	should not be retried.
12635  */
12636 static int
12637 sata_identify_device(sata_hba_inst_t *sata_hba_inst,
12638     sata_drive_info_t *sdinfo)
12639 {
12640 	uint16_t cfg_word;
12641 	int rval;
12642 
12643 	/* fetch device identify data */
12644 	if ((rval = sata_fetch_device_identify_data(sata_hba_inst,
12645 	    sdinfo)) != SATA_SUCCESS)
12646 		goto fail_unknown;
12647 
12648 	cfg_word = sdinfo->satadrv_id.ai_config;
12649 
12650 	/* Set the correct device type */
12651 	if ((cfg_word & SATA_ATA_TYPE_MASK) == SATA_ATA_TYPE) {
12652 		sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
12653 	} else if (cfg_word == SATA_CFA_TYPE) {
12654 		/* It's a Compact Flash media via CF-to-SATA HDD adapter */
12655 		sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
12656 	} else if ((cfg_word & SATA_ATAPI_TYPE_MASK) == SATA_ATAPI_TYPE) {
12657 		switch (cfg_word & SATA_ATAPI_ID_DEV_TYPE) {
12658 		case SATA_ATAPI_CDROM_DEV:
12659 			sdinfo->satadrv_type = SATA_DTYPE_ATAPICD;
12660 			break;
12661 		case SATA_ATAPI_SQACC_DEV:
12662 			sdinfo->satadrv_type = SATA_DTYPE_ATAPITAPE;
12663 			break;
12664 		case SATA_ATAPI_DIRACC_DEV:
12665 			sdinfo->satadrv_type = SATA_DTYPE_ATAPIDISK;
12666 			break;
12667 		case SATA_ATAPI_PROC_DEV:
12668 			sdinfo->satadrv_type = SATA_DTYPE_ATAPIPROC;
12669 			break;
12670 		default:
12671 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12672 		}
12673 	} else {
12674 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12675 	}
12676 
12677 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12678 		if (sdinfo->satadrv_capacity == 0) {
12679 			/* Non-LBA disk. Too bad... */
12680 			sata_log(sata_hba_inst, CE_WARN,
12681 			    "SATA disk device at port %d does not support LBA",
12682 			    sdinfo->satadrv_addr.cport);
12683 			rval = SATA_FAILURE;
12684 			goto fail_unknown;
12685 		}
12686 	}
12687 #if 0
12688 	/* Left for historical reason */
12689 	/*
12690 	 * Some initial version of SATA spec indicated that at least
12691 	 * UDMA mode 4 has to be supported. It is not metioned in
12692 	 * SerialATA 2.6, so this restriction is removed.
12693 	 */
12694 	/* Check for Ultra DMA modes 6 through 0 being supported */
12695 	for (i = 6; i >= 0; --i) {
12696 		if (sdinfo->satadrv_id.ai_ultradma & (1 << i))
12697 			break;
12698 	}
12699 
12700 	/*
12701 	 * At least UDMA 4 mode has to be supported. If mode 4 or
12702 	 * higher are not supported by the device, fail this
12703 	 * device.
12704 	 */
12705 	if (i < 4) {
12706 		/* No required Ultra DMA mode supported */
12707 		sata_log(sata_hba_inst, CE_WARN,
12708 		    "SATA disk device at port %d does not support UDMA "
12709 		    "mode 4 or higher", sdinfo->satadrv_addr.cport);
12710 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12711 		    "mode 4 or higher required, %d supported", i));
12712 		rval = SATA_FAILURE;
12713 		goto fail_unknown;
12714 	}
12715 #endif
12716 
12717 	/*
12718 	 * For Disk devices, if it doesn't support UDMA mode, we would
12719 	 * like to return failure directly.
12720 	 */
12721 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
12722 	    !((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
12723 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0)) {
12724 		sata_log(sata_hba_inst, CE_WARN,
12725 		    "SATA disk device at port %d does not support UDMA",
12726 		    sdinfo->satadrv_addr.cport);
12727 		rval = SATA_FAILURE;
12728 		goto fail_unknown;
12729 	}
12730 
12731 	return (SATA_SUCCESS);
12732 
12733 fail_unknown:
12734 	/* Invalidate sata_drive_info ? */
12735 	sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12736 	sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
12737 	return (rval);
12738 }
12739 
12740 /*
12741  * Log/display device information
12742  */
12743 static void
12744 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst,
12745     sata_drive_info_t *sdinfo)
12746 {
12747 	int valid_version;
12748 	char msg_buf[MAXPATHLEN];
12749 	int i;
12750 
12751 	/* Show HBA path */
12752 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf);
12753 
12754 	cmn_err(CE_CONT, "?%s :\n", msg_buf);
12755 
12756 	switch (sdinfo->satadrv_type) {
12757 	case SATA_DTYPE_ATADISK:
12758 		(void) sprintf(msg_buf, "SATA disk device at");
12759 		break;
12760 
12761 	case SATA_DTYPE_ATAPICD:
12762 		(void) sprintf(msg_buf, "SATA CD/DVD (ATAPI) device at");
12763 		break;
12764 
12765 	case SATA_DTYPE_ATAPITAPE:
12766 		(void) sprintf(msg_buf, "SATA tape (ATAPI) device at");
12767 		break;
12768 
12769 	case SATA_DTYPE_ATAPIDISK:
12770 		(void) sprintf(msg_buf, "SATA disk (ATAPI) device at");
12771 		break;
12772 
12773 	case SATA_DTYPE_ATAPIPROC:
12774 		(void) sprintf(msg_buf, "SATA processor (ATAPI) device at");
12775 		break;
12776 
12777 	case SATA_DTYPE_UNKNOWN:
12778 		(void) sprintf(msg_buf,
12779 		    "Unsupported SATA device type (cfg 0x%x) at ",
12780 		    sdinfo->satadrv_id.ai_config);
12781 		break;
12782 	}
12783 
12784 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT)
12785 		cmn_err(CE_CONT, "?\t%s port %d\n",
12786 		    msg_buf, sdinfo->satadrv_addr.cport);
12787 	else
12788 		cmn_err(CE_CONT, "?\t%s port %d:%d\n",
12789 		    msg_buf, sdinfo->satadrv_addr.cport,
12790 		    sdinfo->satadrv_addr.pmport);
12791 
12792 	bcopy(&sdinfo->satadrv_id.ai_model, msg_buf,
12793 	    sizeof (sdinfo->satadrv_id.ai_model));
12794 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model));
12795 	msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
12796 	cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf);
12797 
12798 	bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf,
12799 	    sizeof (sdinfo->satadrv_id.ai_fw));
12800 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw));
12801 	msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
12802 	cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf);
12803 
12804 	bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf,
12805 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12806 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser));
12807 	msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
12808 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12809 		cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
12810 	} else {
12811 		/*
12812 		 * Some drives do not implement serial number and may
12813 		 * violate the spec by providing spaces rather than zeros
12814 		 * in serial number field. Scan the buffer to detect it.
12815 		 */
12816 		for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) {
12817 			if (msg_buf[i] != '\0' && msg_buf[i] != ' ')
12818 				break;
12819 		}
12820 		if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) {
12821 			cmn_err(CE_CONT, "?\tserial number - none\n");
12822 		} else {
12823 			cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
12824 		}
12825 	}
12826 
12827 #ifdef SATA_DEBUG
12828 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
12829 	    sdinfo->satadrv_id.ai_majorversion != 0xffff) {
12830 		int i;
12831 		for (i = 14; i >= 2; i--) {
12832 			if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) {
12833 				valid_version = i;
12834 				break;
12835 			}
12836 		}
12837 		cmn_err(CE_CONT,
12838 		    "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
12839 		    valid_version,
12840 		    sdinfo->satadrv_id.ai_majorversion,
12841 		    sdinfo->satadrv_id.ai_minorversion);
12842 	}
12843 #endif
12844 	/* Log some info */
12845 	cmn_err(CE_CONT, "?\tsupported features:\n");
12846 	msg_buf[0] = '\0';
12847 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12848 		if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48)
12849 			(void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN);
12850 		else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28)
12851 			(void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN);
12852 	}
12853 	if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA)
12854 		(void) strlcat(msg_buf, "DMA", MAXPATHLEN);
12855 	if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ)
12856 		(void) strlcat(msg_buf, ", Native Command Queueing",
12857 		    MAXPATHLEN);
12858 	if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)
12859 		(void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN);
12860 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) &&
12861 	    (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED))
12862 		(void) strlcat(msg_buf, ", SMART", MAXPATHLEN);
12863 	if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) &&
12864 	    (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED))
12865 		(void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN);
12866 	cmn_err(CE_CONT, "?\t %s\n", msg_buf);
12867 	if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA3)
12868 		cmn_err(CE_CONT, "?\tSATA Gen3 signaling speed (6.0Gbps)\n");
12869 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2)
12870 		cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n");
12871 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1)
12872 		cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n");
12873 	if (sdinfo->satadrv_features_support &
12874 	    (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) {
12875 		msg_buf[0] = '\0';
12876 		(void) snprintf(msg_buf, MAXPATHLEN,
12877 		    "Supported queue depth %d",
12878 		    sdinfo->satadrv_queue_depth);
12879 		if (!(sata_func_enable &
12880 		    (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ)))
12881 			(void) strlcat(msg_buf,
12882 			    " - queueing disabled globally", MAXPATHLEN);
12883 		else if (sdinfo->satadrv_queue_depth >
12884 		    sdinfo->satadrv_max_queue_depth) {
12885 			(void) snprintf(&msg_buf[strlen(msg_buf)],
12886 			    MAXPATHLEN - strlen(msg_buf), ", limited to %d",
12887 			    (int)sdinfo->satadrv_max_queue_depth);
12888 		}
12889 		cmn_err(CE_CONT, "?\t%s\n", msg_buf);
12890 	}
12891 
12892 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12893 #ifdef __i386
12894 		(void) sprintf(msg_buf, "\tcapacity = %llu sectors\n",
12895 		    sdinfo->satadrv_capacity);
12896 #else
12897 		(void) sprintf(msg_buf, "\tcapacity = %lu sectors\n",
12898 		    sdinfo->satadrv_capacity);
12899 #endif
12900 		cmn_err(CE_CONT, "?%s", msg_buf);
12901 	}
12902 }
12903 
12904 /*
12905  * Log/display port multiplier information
12906  * No Mutex should be hold.
12907  */
12908 static void
12909 sata_show_pmult_info(sata_hba_inst_t *sata_hba_inst,
12910     sata_device_t *sata_device)
12911 {
12912 	_NOTE(ARGUNUSED(sata_hba_inst))
12913 
12914 	int cport = sata_device->satadev_addr.cport;
12915 	sata_pmult_info_t *pmultinfo;
12916 	char msg_buf[MAXPATHLEN];
12917 	uint32_t gscr0, gscr1, gscr2, gscr64;
12918 
12919 	mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12920 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
12921 	if (pmultinfo == NULL) {
12922 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12923 		return;
12924 	}
12925 
12926 	gscr0 = pmultinfo->pmult_gscr.gscr0;
12927 	gscr1 = pmultinfo->pmult_gscr.gscr1;
12928 	gscr2 = pmultinfo->pmult_gscr.gscr2;
12929 	gscr64 = pmultinfo->pmult_gscr.gscr64;
12930 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12931 
12932 	cmn_err(CE_CONT, "?Port Multiplier %d device-ports found at port %d",
12933 	    sata_device->satadev_add_info, sata_device->satadev_addr.cport);
12934 
12935 	(void) sprintf(msg_buf, "\tVendor_ID 0x%04x, Module_ID 0x%04x",
12936 	    gscr0 & 0xffff, (gscr0 >> 16) & 0xffff);
12937 	cmn_err(CE_CONT, "?%s", msg_buf);
12938 
12939 	(void) strcpy(msg_buf, "\tSupport SATA PMP Spec ");
12940 	if (gscr1 & (1 << 3))
12941 		(void) strlcat(msg_buf, "1.2", MAXPATHLEN);
12942 	else if (gscr1 & (1 << 2))
12943 		(void) strlcat(msg_buf, "1.1", MAXPATHLEN);
12944 	else if (gscr1 & (1 << 1))
12945 		(void) strlcat(msg_buf, "1.0", MAXPATHLEN);
12946 	else
12947 		(void) strlcat(msg_buf, "unknown", MAXPATHLEN);
12948 	cmn_err(CE_CONT, "?%s", msg_buf);
12949 
12950 	(void) strcpy(msg_buf, "\tSupport ");
12951 	if (gscr64 & (1 << 3))
12952 		(void) strlcat(msg_buf, "Asy-Notif, ",
12953 		    MAXPATHLEN);
12954 	if (gscr64 & (1 << 2))
12955 		(void) strlcat(msg_buf, "Dyn-SSC, ", MAXPATHLEN);
12956 	if (gscr64 & (1 << 1))
12957 		(void) strlcat(msg_buf, "Iss-PMREQ, ", MAXPATHLEN);
12958 	if (gscr64 & (1 << 0))
12959 		(void) strlcat(msg_buf, "BIST", MAXPATHLEN);
12960 	if ((gscr64 & 0xf) == 0)
12961 		(void) strlcat(msg_buf, "nothing", MAXPATHLEN);
12962 	cmn_err(CE_CONT, "?%s", msg_buf);
12963 
12964 	(void) sprintf(msg_buf, "\tNumber of exposed device fan-out ports: %d",
12965 	    gscr2 & SATA_PMULT_PORTNUM_MASK);
12966 	cmn_err(CE_CONT, "?%s", msg_buf);
12967 }
12968 
12969 /*
12970  * sata_save_drive_settings extracts current setting of the device and stores
12971  * it for future reference, in case the device setup would need to be restored
12972  * after the device reset.
12973  *
12974  * For all devices read ahead and write cache settings are saved, if the
12975  * device supports these features at all.
12976  * For ATAPI devices the Removable Media Status Notification setting is saved.
12977  */
12978 static void
12979 sata_save_drive_settings(sata_drive_info_t *sdinfo)
12980 {
12981 	if (SATA_READ_AHEAD_SUPPORTED(sdinfo->satadrv_id) ||
12982 	    SATA_WRITE_CACHE_SUPPORTED(sdinfo->satadrv_id)) {
12983 
12984 		/* Current setting of Read Ahead (and Read Cache) */
12985 		if (SATA_READ_AHEAD_ENABLED(sdinfo->satadrv_id))
12986 			sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
12987 		else
12988 			sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD;
12989 
12990 		/* Current setting of Write Cache */
12991 		if (SATA_WRITE_CACHE_ENABLED(sdinfo->satadrv_id))
12992 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12993 		else
12994 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12995 	}
12996 
12997 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
12998 		if (SATA_RM_NOTIFIC_SUPPORTED(sdinfo->satadrv_id))
12999 			sdinfo->satadrv_settings |= SATA_DEV_RMSN;
13000 		else
13001 			sdinfo->satadrv_settings &= ~SATA_DEV_RMSN;
13002 	}
13003 }
13004 
13005 
13006 /*
13007  * sata_check_capacity function determines a disk capacity
13008  * and addressing mode (LBA28/LBA48) by examining a disk identify device data.
13009  *
13010  * NOTE: CHS mode is not supported! If a device does not support LBA,
13011  * this function is not called.
13012  *
13013  * Returns device capacity in number of blocks, i.e. largest addressable LBA+1
13014  */
13015 static uint64_t
13016 sata_check_capacity(sata_drive_info_t *sdinfo)
13017 {
13018 	uint64_t capacity = 0;
13019 	int i;
13020 
13021 	if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK ||
13022 	    !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT)
13023 		/* Capacity valid only for LBA-addressable disk devices */
13024 		return (0);
13025 
13026 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) &&
13027 	    (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) &&
13028 	    (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) {
13029 		/* LBA48 mode supported and enabled */
13030 		sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 |
13031 		    SATA_DEV_F_LBA28;
13032 		for (i = 3;  i >= 0;  --i) {
13033 			capacity <<= 16;
13034 			capacity += sdinfo->satadrv_id.ai_addrsecxt[i];
13035 		}
13036 	} else {
13037 		capacity = sdinfo->satadrv_id.ai_addrsec[1];
13038 		capacity <<= 16;
13039 		capacity += sdinfo->satadrv_id.ai_addrsec[0];
13040 		if (capacity >= 0x1000000)
13041 			/* LBA28 mode */
13042 			sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28;
13043 	}
13044 	return (capacity);
13045 }
13046 
13047 
13048 /*
13049  * Allocate consistent buffer for DMA transfer
13050  *
13051  * Cannot be called from interrupt level or with mutex held - it may sleep.
13052  *
13053  * Returns pointer to allocated buffer structure, or NULL if allocation failed.
13054  */
13055 static struct buf *
13056 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len)
13057 {
13058 	struct scsi_address ap;
13059 	struct buf *bp;
13060 	ddi_dma_attr_t	cur_dma_attr;
13061 
13062 	ASSERT(spx->txlt_sata_pkt != NULL);
13063 	ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran;
13064 	ap.a_target = SATA_TO_SCSI_TARGET(
13065 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport,
13066 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport,
13067 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual);
13068 	ap.a_lun = 0;
13069 
13070 	bp = scsi_alloc_consistent_buf(&ap, NULL, len,
13071 	    B_READ, SLEEP_FUNC, NULL);
13072 
13073 	if (bp != NULL) {
13074 		/* Allocate DMA resources for this buffer */
13075 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
13076 		/*
13077 		 * We use a local version of the dma_attr, to account
13078 		 * for a device addressing limitations.
13079 		 * sata_adjust_dma_attr() will handle sdinfo == NULL which
13080 		 * will cause dma attributes to be adjusted to a lowest
13081 		 * acceptable level.
13082 		 */
13083 		sata_adjust_dma_attr(NULL,
13084 		    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
13085 
13086 		if (sata_dma_buf_setup(spx, PKT_CONSISTENT,
13087 		    SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) {
13088 			scsi_free_consistent_buf(bp);
13089 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
13090 			bp = NULL;
13091 		}
13092 	}
13093 	return (bp);
13094 }
13095 
13096 /*
13097  * Release local buffer (consistent buffer for DMA transfer) allocated
13098  * via sata_alloc_local_buffer().
13099  */
13100 static void
13101 sata_free_local_buffer(sata_pkt_txlate_t *spx)
13102 {
13103 	ASSERT(spx->txlt_sata_pkt != NULL);
13104 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL);
13105 
13106 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0;
13107 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL;
13108 
13109 	sata_common_free_dma_rsrcs(spx);
13110 
13111 	/* Free buffer */
13112 	scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp);
13113 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
13114 }
13115 
13116 /*
13117  * Allocate sata_pkt
13118  * Pkt structure version and embedded strcutures version are initialized.
13119  * sata_pkt and sata_pkt_txlate structures are cross-linked.
13120  *
13121  * Since this may be called in interrupt context by sata_scsi_init_pkt,
13122  * callback argument determines if it can sleep or not.
13123  * Hence, it should not be called from interrupt context.
13124  *
13125  * If successful, non-NULL pointer to a sata pkt is returned.
13126  * Upon failure, NULL pointer is returned.
13127  */
13128 static sata_pkt_t *
13129 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t))
13130 {
13131 	sata_pkt_t *spkt;
13132 	int kmsflag;
13133 
13134 	kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
13135 	spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag);
13136 	if (spkt == NULL) {
13137 		SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13138 		    "sata_pkt_alloc: failed"));
13139 		return (NULL);
13140 	}
13141 	spkt->satapkt_rev = SATA_PKT_REV;
13142 	spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV;
13143 	spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
13144 	spkt->satapkt_framework_private = spx;
13145 	spx->txlt_sata_pkt = spkt;
13146 	return (spkt);
13147 }
13148 
13149 /*
13150  * Free sata pkt allocated via sata_pkt_alloc()
13151  */
13152 static void
13153 sata_pkt_free(sata_pkt_txlate_t *spx)
13154 {
13155 	ASSERT(spx->txlt_sata_pkt != NULL);
13156 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL);
13157 	kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t));
13158 	spx->txlt_sata_pkt = NULL;
13159 }
13160 
13161 
13162 /*
13163  * Adjust DMA attributes.
13164  * SCSI cmds block count is up to 24 bits, SATA cmd block count vary
13165  * from 8 bits to 16 bits, depending on a command being used.
13166  * Limiting max block count arbitrarily to 256 for all read/write
13167  * commands may affects performance, so check both the device and
13168  * controller capability before adjusting dma attributes.
13169  */
13170 void
13171 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr,
13172     ddi_dma_attr_t *adj_dma_attr)
13173 {
13174 	uint32_t count_max;
13175 
13176 	/* Copy original attributes */
13177 	*adj_dma_attr = *dma_attr;
13178 	/*
13179 	 * Things to consider: device addressing capability,
13180 	 * "excessive" controller DMA capabilities.
13181 	 * If a device is being probed/initialized, there are
13182 	 * no device info - use default limits then.
13183 	 */
13184 	if (sdinfo == NULL) {
13185 		count_max = dma_attr->dma_attr_granular * 0x100;
13186 		if (dma_attr->dma_attr_count_max > count_max)
13187 			adj_dma_attr->dma_attr_count_max = count_max;
13188 		if (dma_attr->dma_attr_maxxfer > count_max)
13189 			adj_dma_attr->dma_attr_maxxfer = count_max;
13190 		return;
13191 	}
13192 
13193 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
13194 		if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) {
13195 			/*
13196 			 * 16-bit sector count may be used - we rely on
13197 			 * the assumption that only read and write cmds
13198 			 * will request more than 256 sectors worth of data
13199 			 */
13200 			count_max = adj_dma_attr->dma_attr_granular * 0x10000;
13201 		} else {
13202 			/*
13203 			 * 8-bit sector count will be used - default limits
13204 			 * for dma attributes
13205 			 */
13206 			count_max = adj_dma_attr->dma_attr_granular * 0x100;
13207 		}
13208 		/*
13209 		 * Adjust controler dma attributes, if necessary
13210 		 */
13211 		if (dma_attr->dma_attr_count_max > count_max)
13212 			adj_dma_attr->dma_attr_count_max = count_max;
13213 		if (dma_attr->dma_attr_maxxfer > count_max)
13214 			adj_dma_attr->dma_attr_maxxfer = count_max;
13215 	}
13216 }
13217 
13218 
13219 /*
13220  * Allocate DMA resources for the buffer
13221  * This function handles initial DMA resource allocation as well as
13222  * DMA window shift and may be called repeatedly for the same DMA window
13223  * until all DMA cookies in the DMA window are processed.
13224  * To guarantee that there is always a coherent set of cookies to process
13225  * by SATA HBA driver (observing alignment, device granularity, etc.),
13226  * the number of slots for DMA cookies is equal to lesser of  a number of
13227  * cookies in a DMA window and a max number of scatter/gather entries.
13228  *
13229  * Returns DDI_SUCCESS upon successful operation.
13230  * Return failure code of a failing command or DDI_FAILURE when
13231  * internal cleanup failed.
13232  */
13233 static int
13234 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags,
13235     int (*callback)(caddr_t), caddr_t arg,
13236     ddi_dma_attr_t *cur_dma_attr)
13237 {
13238 	int	rval;
13239 	off_t	offset;
13240 	size_t	size;
13241 	int	max_sg_len, req_len, i;
13242 	uint_t	dma_flags;
13243 	struct buf	*bp;
13244 	uint64_t	cur_txfer_len;
13245 
13246 
13247 	ASSERT(spx->txlt_sata_pkt != NULL);
13248 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
13249 	ASSERT(bp != NULL);
13250 
13251 
13252 	if (spx->txlt_buf_dma_handle == NULL) {
13253 		/*
13254 		 * No DMA resources allocated so far - this is a first call
13255 		 * for this sata pkt.
13256 		 */
13257 		rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst),
13258 		    cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle);
13259 
13260 		if (rval != DDI_SUCCESS) {
13261 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13262 			    "sata_dma_buf_setup: no buf DMA resources %x",
13263 			    rval));
13264 			return (rval);
13265 		}
13266 
13267 		if (bp->b_flags & B_READ)
13268 			dma_flags = DDI_DMA_READ;
13269 		else
13270 			dma_flags = DDI_DMA_WRITE;
13271 
13272 		if (flags & PKT_CONSISTENT)
13273 			dma_flags |= DDI_DMA_CONSISTENT;
13274 
13275 		if (flags & PKT_DMA_PARTIAL)
13276 			dma_flags |= DDI_DMA_PARTIAL;
13277 
13278 		/*
13279 		 * Check buffer alignment and size against dma attributes
13280 		 * Consider dma_attr_align only. There may be requests
13281 		 * with the size lower than device granularity, but they
13282 		 * will not read/write from/to the device, so no adjustment
13283 		 * is necessary. The dma_attr_minxfer theoretically should
13284 		 * be considered, but no HBA driver is checking it.
13285 		 */
13286 		if (IS_P2ALIGNED(bp->b_un.b_addr,
13287 		    cur_dma_attr->dma_attr_align)) {
13288 			rval = ddi_dma_buf_bind_handle(
13289 			    spx->txlt_buf_dma_handle,
13290 			    bp, dma_flags, callback, arg,
13291 			    &spx->txlt_dma_cookie,
13292 			    &spx->txlt_curwin_num_dma_cookies);
13293 		} else { /* Buffer is not aligned */
13294 
13295 			int	(*ddicallback)(caddr_t);
13296 			size_t	bufsz;
13297 
13298 			/* Check id sleeping is allowed */
13299 			ddicallback = (callback == NULL_FUNC) ?
13300 			    DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
13301 
13302 			SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13303 			    "mis-aligned buffer: addr=0x%p, cnt=%lu",
13304 			    (void *)bp->b_un.b_addr, bp->b_bcount);
13305 
13306 			if (bp->b_flags & (B_PAGEIO|B_PHYS))
13307 				/*
13308 				 * CPU will need to access data in the buffer
13309 				 * (for copying) so map it.
13310 				 */
13311 				bp_mapin(bp);
13312 
13313 			ASSERT(spx->txlt_tmp_buf == NULL);
13314 
13315 			/* Buffer may be padded by ddi_dma_mem_alloc()! */
13316 			rval = ddi_dma_mem_alloc(
13317 			    spx->txlt_buf_dma_handle,
13318 			    bp->b_bcount,
13319 			    &sata_acc_attr,
13320 			    DDI_DMA_STREAMING,
13321 			    ddicallback, NULL,
13322 			    &spx->txlt_tmp_buf,
13323 			    &bufsz,
13324 			    &spx->txlt_tmp_buf_handle);
13325 
13326 			if (rval != DDI_SUCCESS) {
13327 				/* DMA mapping failed */
13328 				(void) ddi_dma_free_handle(
13329 				    &spx->txlt_buf_dma_handle);
13330 				spx->txlt_buf_dma_handle = NULL;
13331 #ifdef SATA_DEBUG
13332 				mbuffail_count++;
13333 #endif
13334 				SATADBG1(SATA_DBG_DMA_SETUP,
13335 				    spx->txlt_sata_hba_inst,
13336 				    "sata_dma_buf_setup: "
13337 				    "buf dma mem alloc failed %x\n", rval);
13338 				return (rval);
13339 			}
13340 			ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf,
13341 			    cur_dma_attr->dma_attr_align));
13342 
13343 #ifdef SATA_DEBUG
13344 			mbuf_count++;
13345 
13346 			if (bp->b_bcount != bufsz)
13347 				/*
13348 				 * This will require special handling, because
13349 				 * DMA cookies will be based on the temporary
13350 				 * buffer size, not the original buffer
13351 				 * b_bcount, so the residue may have to
13352 				 * be counted differently.
13353 				 */
13354 				SATADBG2(SATA_DBG_DMA_SETUP,
13355 				    spx->txlt_sata_hba_inst,
13356 				    "sata_dma_buf_setup: bp size %x != "
13357 				    "bufsz %x\n", bp->b_bcount, bufsz);
13358 #endif
13359 			if (dma_flags & DDI_DMA_WRITE) {
13360 				/*
13361 				 * Write operation - copy data into
13362 				 * an aligned temporary buffer. Buffer will be
13363 				 * synced for device by ddi_dma_addr_bind_handle
13364 				 */
13365 				bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf,
13366 				    bp->b_bcount);
13367 			}
13368 
13369 			rval = ddi_dma_addr_bind_handle(
13370 			    spx->txlt_buf_dma_handle,
13371 			    NULL,
13372 			    spx->txlt_tmp_buf,
13373 			    bufsz, dma_flags, ddicallback, 0,
13374 			    &spx->txlt_dma_cookie,
13375 			    &spx->txlt_curwin_num_dma_cookies);
13376 		}
13377 
13378 		switch (rval) {
13379 		case DDI_DMA_PARTIAL_MAP:
13380 			SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13381 			    "sata_dma_buf_setup: DMA Partial Map\n", NULL);
13382 			/*
13383 			 * Partial DMA mapping.
13384 			 * Retrieve number of DMA windows for this request.
13385 			 */
13386 			if (ddi_dma_numwin(spx->txlt_buf_dma_handle,
13387 			    &spx->txlt_num_dma_win) != DDI_SUCCESS) {
13388 				if (spx->txlt_tmp_buf != NULL) {
13389 					ddi_dma_mem_free(
13390 					    &spx->txlt_tmp_buf_handle);
13391 					spx->txlt_tmp_buf = NULL;
13392 				}
13393 				(void) ddi_dma_unbind_handle(
13394 				    spx->txlt_buf_dma_handle);
13395 				(void) ddi_dma_free_handle(
13396 				    &spx->txlt_buf_dma_handle);
13397 				spx->txlt_buf_dma_handle = NULL;
13398 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13399 				    "sata_dma_buf_setup: numwin failed\n"));
13400 				return (DDI_FAILURE);
13401 			}
13402 			SATADBG2(SATA_DBG_DMA_SETUP,
13403 			    spx->txlt_sata_hba_inst,
13404 			    "sata_dma_buf_setup: windows: %d, cookies: %d\n",
13405 			    spx->txlt_num_dma_win,
13406 			    spx->txlt_curwin_num_dma_cookies);
13407 			spx->txlt_cur_dma_win = 0;
13408 			break;
13409 
13410 		case DDI_DMA_MAPPED:
13411 			/* DMA fully mapped */
13412 			spx->txlt_num_dma_win = 1;
13413 			spx->txlt_cur_dma_win = 0;
13414 			SATADBG1(SATA_DBG_DMA_SETUP,
13415 			    spx->txlt_sata_hba_inst,
13416 			    "sata_dma_buf_setup: windows: 1 "
13417 			    "cookies: %d\n", spx->txlt_curwin_num_dma_cookies);
13418 			break;
13419 
13420 		default:
13421 			/* DMA mapping failed */
13422 			if (spx->txlt_tmp_buf != NULL) {
13423 				ddi_dma_mem_free(
13424 				    &spx->txlt_tmp_buf_handle);
13425 				spx->txlt_tmp_buf = NULL;
13426 			}
13427 			(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
13428 			spx->txlt_buf_dma_handle = NULL;
13429 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13430 			    "sata_dma_buf_setup: buf dma handle binding "
13431 			    "failed %x\n", rval));
13432 			return (rval);
13433 		}
13434 		spx->txlt_curwin_processed_dma_cookies = 0;
13435 		spx->txlt_dma_cookie_list = NULL;
13436 	} else {
13437 		/*
13438 		 * DMA setup is reused. Check if we need to process more
13439 		 * cookies in current window, or to get next window, if any.
13440 		 */
13441 
13442 		ASSERT(spx->txlt_curwin_processed_dma_cookies <=
13443 		    spx->txlt_curwin_num_dma_cookies);
13444 
13445 		if (spx->txlt_curwin_processed_dma_cookies ==
13446 		    spx->txlt_curwin_num_dma_cookies) {
13447 			/*
13448 			 * All cookies from current DMA window were processed.
13449 			 * Get next DMA window.
13450 			 */
13451 			spx->txlt_cur_dma_win++;
13452 			if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) {
13453 				(void) ddi_dma_getwin(spx->txlt_buf_dma_handle,
13454 				    spx->txlt_cur_dma_win, &offset, &size,
13455 				    &spx->txlt_dma_cookie,
13456 				    &spx->txlt_curwin_num_dma_cookies);
13457 				spx->txlt_curwin_processed_dma_cookies = 0;
13458 			} else {
13459 				/* No more windows! End of request! */
13460 				/* What to do? - panic for now */
13461 				ASSERT(spx->txlt_cur_dma_win >=
13462 				    spx->txlt_num_dma_win);
13463 
13464 				spx->txlt_curwin_num_dma_cookies = 0;
13465 				spx->txlt_curwin_processed_dma_cookies = 0;
13466 				spx->txlt_sata_pkt->
13467 				    satapkt_cmd.satacmd_num_dma_cookies = 0;
13468 				return (DDI_SUCCESS);
13469 			}
13470 		}
13471 	}
13472 	/* There better be at least one DMA cookie outstanding */
13473 	ASSERT((spx->txlt_curwin_num_dma_cookies -
13474 	    spx->txlt_curwin_processed_dma_cookies) > 0);
13475 
13476 	if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) {
13477 		/* The default cookie slot was used in previous run */
13478 		ASSERT(spx->txlt_curwin_processed_dma_cookies == 0);
13479 		spx->txlt_dma_cookie_list = NULL;
13480 		spx->txlt_dma_cookie_list_len = 0;
13481 	}
13482 	if (spx->txlt_curwin_processed_dma_cookies == 0) {
13483 		/*
13484 		 * Processing a new DMA window - set-up dma cookies list.
13485 		 * We may reuse previously allocated cookie array if it is
13486 		 * possible.
13487 		 */
13488 		if (spx->txlt_dma_cookie_list != NULL &&
13489 		    spx->txlt_dma_cookie_list_len <
13490 		    spx->txlt_curwin_num_dma_cookies) {
13491 			/*
13492 			 * New DMA window contains more cookies than
13493 			 * the previous one. We need larger cookie list - free
13494 			 * the old one.
13495 			 */
13496 			(void) kmem_free(spx->txlt_dma_cookie_list,
13497 			    spx->txlt_dma_cookie_list_len *
13498 			    sizeof (ddi_dma_cookie_t));
13499 			spx->txlt_dma_cookie_list = NULL;
13500 			spx->txlt_dma_cookie_list_len = 0;
13501 		}
13502 		if (spx->txlt_dma_cookie_list == NULL) {
13503 			/*
13504 			 * Calculate lesser of number of cookies in this
13505 			 * DMA window and number of s/g entries.
13506 			 */
13507 			max_sg_len = cur_dma_attr->dma_attr_sgllen;
13508 			req_len = MIN(max_sg_len,
13509 			    spx->txlt_curwin_num_dma_cookies);
13510 
13511 			/* Allocate new dma cookie array if necessary */
13512 			if (req_len == 1) {
13513 				/* Only one cookie - no need for a list */
13514 				spx->txlt_dma_cookie_list =
13515 				    &spx->txlt_dma_cookie;
13516 				spx->txlt_dma_cookie_list_len = 1;
13517 			} else {
13518 				/*
13519 				 * More than one cookie - try to allocate space.
13520 				 */
13521 				spx->txlt_dma_cookie_list = kmem_zalloc(
13522 				    sizeof (ddi_dma_cookie_t) * req_len,
13523 				    callback == NULL_FUNC ? KM_NOSLEEP :
13524 				    KM_SLEEP);
13525 				if (spx->txlt_dma_cookie_list == NULL) {
13526 					SATADBG1(SATA_DBG_DMA_SETUP,
13527 					    spx->txlt_sata_hba_inst,
13528 					    "sata_dma_buf_setup: cookie list "
13529 					    "allocation failed\n", NULL);
13530 					/*
13531 					 * We could not allocate space for
13532 					 * neccessary number of dma cookies in
13533 					 * this window, so we fail this request.
13534 					 * Next invocation would try again to
13535 					 * allocate space for cookie list.
13536 					 * Note:Packet residue was not modified.
13537 					 */
13538 					return (DDI_DMA_NORESOURCES);
13539 				} else {
13540 					spx->txlt_dma_cookie_list_len = req_len;
13541 				}
13542 			}
13543 		}
13544 		/*
13545 		 * Fetch DMA cookies into cookie list in sata_pkt_txlate.
13546 		 * First cookie was already fetched.
13547 		 */
13548 		*(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie;
13549 		cur_txfer_len =
13550 		    (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size;
13551 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1;
13552 		spx->txlt_curwin_processed_dma_cookies++;
13553 		for (i = 1; (i < spx->txlt_dma_cookie_list_len) &&
13554 		    (i < spx->txlt_curwin_num_dma_cookies); i++) {
13555 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
13556 			    &spx->txlt_dma_cookie_list[i]);
13557 			cur_txfer_len +=
13558 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
13559 			spx->txlt_curwin_processed_dma_cookies++;
13560 			spx->txlt_sata_pkt->
13561 			    satapkt_cmd.satacmd_num_dma_cookies += 1;
13562 		}
13563 	} else {
13564 		SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13565 		    "sata_dma_buf_setup: sliding within DMA window, "
13566 		    "cur cookie %d, total cookies %d\n",
13567 		    spx->txlt_curwin_processed_dma_cookies,
13568 		    spx->txlt_curwin_num_dma_cookies);
13569 
13570 		/*
13571 		 * Not all cookies from the current dma window were used because
13572 		 * of s/g limitation.
13573 		 * There is no need to re-size the list - it was set at
13574 		 * optimal size, or only default entry is used (s/g = 1).
13575 		 */
13576 		if (spx->txlt_dma_cookie_list == NULL) {
13577 			spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie;
13578 			spx->txlt_dma_cookie_list_len = 1;
13579 		}
13580 		/*
13581 		 * Since we are processing remaining cookies in a DMA window,
13582 		 * there may be less of them than the number of entries in the
13583 		 * current dma cookie list.
13584 		 */
13585 		req_len = MIN(spx->txlt_dma_cookie_list_len,
13586 		    (spx->txlt_curwin_num_dma_cookies -
13587 		    spx->txlt_curwin_processed_dma_cookies));
13588 
13589 		/* Fetch the next batch of cookies */
13590 		for (i = 0, cur_txfer_len = 0; i < req_len; i++) {
13591 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
13592 			    &spx->txlt_dma_cookie_list[i]);
13593 			cur_txfer_len +=
13594 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
13595 			spx->txlt_sata_pkt->
13596 			    satapkt_cmd.satacmd_num_dma_cookies++;
13597 			spx->txlt_curwin_processed_dma_cookies++;
13598 		}
13599 	}
13600 
13601 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0);
13602 
13603 	/* Point sata_cmd to the cookie list */
13604 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list =
13605 	    &spx->txlt_dma_cookie_list[0];
13606 
13607 	/* Remember number of DMA cookies passed in sata packet */
13608 	spx->txlt_num_dma_cookies =
13609 	    spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies;
13610 
13611 	ASSERT(cur_txfer_len != 0);
13612 	if (cur_txfer_len <= bp->b_bcount)
13613 		spx->txlt_total_residue -= cur_txfer_len;
13614 	else {
13615 		/*
13616 		 * Temporary DMA buffer has been padded by
13617 		 * ddi_dma_mem_alloc()!
13618 		 * This requires special handling, because DMA cookies are
13619 		 * based on the temporary buffer size, not the b_bcount,
13620 		 * and we have extra bytes to transfer - but the packet
13621 		 * residue has to stay correct because we will copy only
13622 		 * the requested number of bytes.
13623 		 */
13624 		spx->txlt_total_residue -= bp->b_bcount;
13625 	}
13626 
13627 	return (DDI_SUCCESS);
13628 }
13629 
13630 /*
13631  * Common routine for releasing DMA resources
13632  */
13633 static void
13634 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx)
13635 {
13636 	if (spx->txlt_buf_dma_handle != NULL) {
13637 		if (spx->txlt_tmp_buf != NULL)  {
13638 			/*
13639 			 * Intermediate DMA buffer was allocated.
13640 			 * Free allocated buffer and associated access handle.
13641 			 */
13642 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
13643 			spx->txlt_tmp_buf = NULL;
13644 		}
13645 		/*
13646 		 * Free DMA resources - cookies and handles
13647 		 */
13648 		/* ASSERT(spx->txlt_dma_cookie_list != NULL); */
13649 		if (spx->txlt_dma_cookie_list != NULL) {
13650 			if (spx->txlt_dma_cookie_list !=
13651 			    &spx->txlt_dma_cookie) {
13652 				(void) kmem_free(spx->txlt_dma_cookie_list,
13653 				    spx->txlt_dma_cookie_list_len *
13654 				    sizeof (ddi_dma_cookie_t));
13655 				spx->txlt_dma_cookie_list = NULL;
13656 			}
13657 		}
13658 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
13659 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
13660 		spx->txlt_buf_dma_handle = NULL;
13661 	}
13662 }
13663 
13664 /*
13665  * Free DMA resources
13666  * Used by the HBA driver to release DMA resources that it does not use.
13667  *
13668  * Returns Void
13669  */
13670 void
13671 sata_free_dma_resources(sata_pkt_t *sata_pkt)
13672 {
13673 	sata_pkt_txlate_t *spx;
13674 
13675 	if (sata_pkt == NULL)
13676 		return;
13677 
13678 	spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
13679 
13680 	sata_common_free_dma_rsrcs(spx);
13681 }
13682 
13683 /*
13684  * Fetch Device Identify data.
13685  * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type)
13686  * command to a device and get the device identify data.
13687  * The device_info structure has to be set to device type (for selecting proper
13688  * device identify command).
13689  *
13690  * Returns:
13691  * SATA_SUCCESS if cmd succeeded
13692  * SATA_RETRY if cmd was rejected and could be retried,
13693  * SATA_FAILURE if cmd failed and should not be retried (port error)
13694  *
13695  * Cannot be called in an interrupt context.
13696  */
13697 
13698 static int
13699 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst,
13700     sata_drive_info_t *sdinfo)
13701 {
13702 	struct buf *bp;
13703 	sata_pkt_t *spkt;
13704 	sata_cmd_t *scmd;
13705 	sata_pkt_txlate_t *spx;
13706 	int rval;
13707 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
13708 
13709 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13710 	spx->txlt_sata_hba_inst = sata_hba_inst;
13711 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13712 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13713 	if (spkt == NULL) {
13714 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13715 		return (SATA_RETRY); /* may retry later */
13716 	}
13717 	/* address is needed now */
13718 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13719 
13720 	/*
13721 	 * Allocate buffer for Identify Data return data
13722 	 */
13723 	bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t));
13724 	if (bp == NULL) {
13725 		sata_pkt_free(spx);
13726 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13727 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13728 		    "sata_fetch_device_identify_data: "
13729 		    "cannot allocate buffer for ID"));
13730 		return (SATA_RETRY); /* may retry later */
13731 	}
13732 
13733 	/* Fill sata_pkt */
13734 	sdinfo->satadrv_state = SATA_STATE_PROBING;
13735 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13736 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13737 	/* Synchronous mode, no callback */
13738 	spkt->satapkt_comp = NULL;
13739 	/* Timeout 30s */
13740 	spkt->satapkt_time = sata_default_pkt_time;
13741 
13742 	scmd = &spkt->satapkt_cmd;
13743 	scmd->satacmd_bp = bp;
13744 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13745 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
13746 
13747 	/* Build Identify Device cmd in the sata_pkt */
13748 	scmd->satacmd_addr_type = 0;		/* N/A */
13749 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
13750 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
13751 	scmd->satacmd_lba_mid_lsb = 0;		/* N/A */
13752 	scmd->satacmd_lba_high_lsb = 0;		/* N/A */
13753 	scmd->satacmd_features_reg = 0;		/* N/A */
13754 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13755 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
13756 		/* Identify Packet Device cmd */
13757 		scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE;
13758 	} else {
13759 		/* Identify Device cmd - mandatory for all other devices */
13760 		scmd->satacmd_cmd_reg = SATAC_ID_DEVICE;
13761 	}
13762 
13763 	/* Send pkt to SATA HBA driver */
13764 	rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt);
13765 
13766 #ifdef SATA_INJECT_FAULTS
13767 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
13768 #endif
13769 
13770 	if (rval == SATA_TRAN_ACCEPTED &&
13771 	    spkt->satapkt_reason == SATA_PKT_COMPLETED) {
13772 		if (spx->txlt_buf_dma_handle != NULL) {
13773 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13774 			    DDI_DMA_SYNC_FORKERNEL);
13775 			ASSERT(rval == DDI_SUCCESS);
13776 			if (sata_check_for_dma_error(dip, spx)) {
13777 				ddi_fm_service_impact(dip,
13778 				    DDI_SERVICE_UNAFFECTED);
13779 				rval = SATA_RETRY;
13780 				goto fail;
13781 			}
13782 
13783 		}
13784 		if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config &
13785 		    SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) {
13786 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13787 			    "SATA disk device at port %d - "
13788 			    "partial Identify Data",
13789 			    sdinfo->satadrv_addr.cport));
13790 			rval = SATA_RETRY; /* may retry later */
13791 			goto fail;
13792 		}
13793 		/* Update sata_drive_info */
13794 		bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id,
13795 		    sizeof (sata_id_t));
13796 
13797 		sdinfo->satadrv_features_support = 0;
13798 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
13799 			/*
13800 			 * Retrieve capacity (disks only) and addressing mode
13801 			 */
13802 			sdinfo->satadrv_capacity = sata_check_capacity(sdinfo);
13803 		} else {
13804 			/*
13805 			 * For ATAPI devices one would have to issue
13806 			 * Get Capacity cmd for media capacity. Not here.
13807 			 */
13808 			sdinfo->satadrv_capacity = 0;
13809 			/*
13810 			 * Check what cdb length is supported
13811 			 */
13812 			if ((sdinfo->satadrv_id.ai_config &
13813 			    SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B)
13814 				sdinfo->satadrv_atapi_cdb_len = 16;
13815 			else
13816 				sdinfo->satadrv_atapi_cdb_len = 12;
13817 		}
13818 		/* Setup supported features flags */
13819 		if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT)
13820 			sdinfo->satadrv_features_support |= SATA_DEV_F_DMA;
13821 
13822 		/* Check for SATA GEN and NCQ support */
13823 		if (sdinfo->satadrv_id.ai_satacap != 0 &&
13824 		    sdinfo->satadrv_id.ai_satacap != 0xffff) {
13825 			/* SATA compliance */
13826 			if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ)
13827 				sdinfo->satadrv_features_support |=
13828 				    SATA_DEV_F_NCQ;
13829 			if (sdinfo->satadrv_id.ai_satacap &
13830 			    (SATA_1_SPEED | SATA_2_SPEED | SATA_3_SPEED)) {
13831 				if (sdinfo->satadrv_id.ai_satacap &
13832 				    SATA_3_SPEED)
13833 					sdinfo->satadrv_features_support |=
13834 					    SATA_DEV_F_SATA3;
13835 				if (sdinfo->satadrv_id.ai_satacap &
13836 				    SATA_2_SPEED)
13837 					sdinfo->satadrv_features_support |=
13838 					    SATA_DEV_F_SATA2;
13839 				if (sdinfo->satadrv_id.ai_satacap &
13840 				    SATA_1_SPEED)
13841 					sdinfo->satadrv_features_support |=
13842 					    SATA_DEV_F_SATA1;
13843 			} else {
13844 				sdinfo->satadrv_features_support |=
13845 				    SATA_DEV_F_SATA1;
13846 			}
13847 		}
13848 		if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) &&
13849 		    (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD))
13850 			sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ;
13851 
13852 		sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth;
13853 		if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) ||
13854 		    (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) {
13855 			++sdinfo->satadrv_queue_depth;
13856 			/* Adjust according to controller capabilities */
13857 			sdinfo->satadrv_max_queue_depth = MIN(
13858 			    sdinfo->satadrv_queue_depth,
13859 			    SATA_QDEPTH(sata_hba_inst));
13860 			/* Adjust according to global queue depth limit */
13861 			sdinfo->satadrv_max_queue_depth = MIN(
13862 			    sdinfo->satadrv_max_queue_depth,
13863 			    sata_current_max_qdepth);
13864 			if (sdinfo->satadrv_max_queue_depth == 0)
13865 				sdinfo->satadrv_max_queue_depth = 1;
13866 		} else
13867 			sdinfo->satadrv_max_queue_depth = 1;
13868 
13869 		rval = SATA_SUCCESS;
13870 	} else {
13871 		/*
13872 		 * Woops, no Identify Data.
13873 		 */
13874 		if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) {
13875 			rval = SATA_RETRY; /* may retry later */
13876 		} else if (rval == SATA_TRAN_ACCEPTED) {
13877 			if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR ||
13878 			    spkt->satapkt_reason == SATA_PKT_ABORTED ||
13879 			    spkt->satapkt_reason == SATA_PKT_TIMEOUT ||
13880 			    spkt->satapkt_reason == SATA_PKT_RESET)
13881 				rval = SATA_RETRY; /* may retry later */
13882 			else
13883 				rval = SATA_FAILURE;
13884 		} else {
13885 			rval = SATA_FAILURE;
13886 		}
13887 	}
13888 fail:
13889 	/* Free allocated resources */
13890 	sata_free_local_buffer(spx);
13891 	sata_pkt_free(spx);
13892 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13893 
13894 	return (rval);
13895 }
13896 
13897 
13898 /*
13899  * Some devices may not come-up with default DMA mode (UDMA or MWDMA).
13900  * UDMA mode is checked first, followed by MWDMA mode.
13901  * set correctly, so this function is setting it to the highest supported level.
13902  * Older SATA spec required that the device supports at least DMA 4 mode and
13903  * UDMA mode is selected.  It is not mentioned in SerialATA 2.6, so this
13904  * restriction has been removed.
13905  *
13906  * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported.
13907  * Returns SATA_FAILURE if proper DMA mode could not be selected.
13908  *
13909  * NOTE: This function should be called only if DMA mode is supported.
13910  */
13911 static int
13912 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo)
13913 {
13914 	sata_pkt_t *spkt;
13915 	sata_cmd_t *scmd;
13916 	sata_pkt_txlate_t *spx;
13917 	int i, mode;
13918 	uint8_t subcmd;
13919 	int rval = SATA_SUCCESS;
13920 
13921 	ASSERT(sdinfo != NULL);
13922 	ASSERT(sata_hba_inst != NULL);
13923 
13924 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
13925 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) {
13926 		/* Find highest Ultra DMA mode supported */
13927 		for (mode = 6; mode >= 0; --mode) {
13928 			if (sdinfo->satadrv_id.ai_ultradma & (1 << mode))
13929 				break;
13930 		}
13931 #if 0
13932 		/* Left for historical reasons */
13933 		/*
13934 		 * Some initial version of SATA spec indicated that at least
13935 		 * UDMA mode 4 has to be supported. It is not mentioned in
13936 		 * SerialATA 2.6, so this restriction is removed.
13937 		 */
13938 		if (mode < 4)
13939 			return (SATA_FAILURE);
13940 #endif
13941 
13942 		/*
13943 		 * For disk, we're still going to set DMA mode whatever is
13944 		 * selected by default
13945 		 *
13946 		 * We saw an old maxtor sata drive will select Ultra DMA and
13947 		 * Multi-Word DMA simultaneouly by default, which is going
13948 		 * to cause DMA command timed out, so we need to select DMA
13949 		 * mode even when it's already done by default
13950 		 */
13951 		if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) {
13952 
13953 			/* Find UDMA mode currently selected */
13954 			for (i = 6; i >= 0; --i) {
13955 				if (sdinfo->satadrv_id.ai_ultradma &
13956 				    (1 << (i + 8)))
13957 					break;
13958 			}
13959 			if (i >= mode)
13960 				/* Nothing to do */
13961 				return (SATA_SUCCESS);
13962 		}
13963 
13964 		subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA;
13965 
13966 	} else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) {
13967 		/* Find highest MultiWord DMA mode supported */
13968 		for (mode = 2; mode >= 0; --mode) {
13969 			if (sdinfo->satadrv_id.ai_dworddma & (1 << mode))
13970 				break;
13971 		}
13972 
13973 		/*
13974 		 * For disk, We're still going to set DMA mode whatever is
13975 		 * selected by default
13976 		 *
13977 		 * We saw an old maxtor sata drive will select Ultra DMA and
13978 		 * Multi-Word DMA simultaneouly by default, which is going
13979 		 * to cause DMA command timed out, so we need to select DMA
13980 		 * mode even when it's already done by default
13981 		 */
13982 		if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) {
13983 
13984 			/* Find highest MultiWord DMA mode selected */
13985 			for (i = 2; i >= 0; --i) {
13986 				if (sdinfo->satadrv_id.ai_dworddma &
13987 				    (1 << (i + 8)))
13988 					break;
13989 			}
13990 			if (i >= mode)
13991 				/* Nothing to do */
13992 				return (SATA_SUCCESS);
13993 		}
13994 
13995 		subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA;
13996 	} else
13997 		return (SATA_SUCCESS);
13998 
13999 	/*
14000 	 * Set DMA mode via SET FEATURES COMMAND.
14001 	 * Prepare packet for SET FEATURES COMMAND.
14002 	 */
14003 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14004 	spx->txlt_sata_hba_inst = sata_hba_inst;
14005 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
14006 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14007 	if (spkt == NULL) {
14008 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14009 		    "sata_set_dma_mode: could not set DMA mode %d", mode));
14010 		rval = SATA_FAILURE;
14011 		goto done;
14012 	}
14013 	/* Fill sata_pkt */
14014 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14015 	/* Timeout 30s */
14016 	spkt->satapkt_time = sata_default_pkt_time;
14017 	/* Synchronous mode, no callback, interrupts */
14018 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14019 	spkt->satapkt_comp = NULL;
14020 	scmd = &spkt->satapkt_cmd;
14021 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14022 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14023 	scmd->satacmd_addr_type = 0;
14024 	scmd->satacmd_device_reg = 0;
14025 	scmd->satacmd_status_reg = 0;
14026 	scmd->satacmd_error_reg = 0;
14027 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14028 	scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE;
14029 	scmd->satacmd_sec_count_lsb = subcmd | mode;
14030 
14031 	/* Transfer command to HBA */
14032 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
14033 	    spkt) != SATA_TRAN_ACCEPTED ||
14034 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
14035 		/* Pkt execution failed */
14036 		rval = SATA_FAILURE;
14037 	}
14038 done:
14039 
14040 	/* Free allocated resources */
14041 	if (spkt != NULL)
14042 		sata_pkt_free(spx);
14043 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14044 
14045 	return (rval);
14046 }
14047 
14048 
14049 /*
14050  * Set device caching mode.
14051  * One of the following operations should be specified:
14052  * SATAC_SF_ENABLE_READ_AHEAD
14053  * SATAC_SF_DISABLE_READ_AHEAD
14054  * SATAC_SF_ENABLE_WRITE_CACHE
14055  * SATAC_SF_DISABLE_WRITE_CACHE
14056  *
14057  * If operation fails, system log messgage is emitted.
14058  * Returns SATA_SUCCESS when the operation succeeds, SATA_RETRY if
14059  * command was sent but did not succeed, and SATA_FAILURE otherwise.
14060  */
14061 
14062 static int
14063 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
14064     int cache_op)
14065 {
14066 	sata_pkt_t *spkt;
14067 	sata_cmd_t *scmd;
14068 	sata_pkt_txlate_t *spx;
14069 	int rval = SATA_SUCCESS;
14070 	int hba_rval;
14071 	char *infop;
14072 
14073 	ASSERT(sdinfo != NULL);
14074 	ASSERT(sata_hba_inst != NULL);
14075 	ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD ||
14076 	    cache_op == SATAC_SF_DISABLE_READ_AHEAD ||
14077 	    cache_op == SATAC_SF_ENABLE_WRITE_CACHE ||
14078 	    cache_op == SATAC_SF_DISABLE_WRITE_CACHE);
14079 
14080 
14081 	/* Prepare packet for SET FEATURES COMMAND */
14082 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14083 	spx->txlt_sata_hba_inst = sata_hba_inst;
14084 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
14085 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14086 	if (spkt == NULL) {
14087 		rval = SATA_FAILURE;
14088 		goto failure;
14089 	}
14090 	/* Fill sata_pkt */
14091 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14092 	/* Timeout 30s */
14093 	spkt->satapkt_time = sata_default_pkt_time;
14094 	/* Synchronous mode, no callback, interrupts */
14095 	spkt->satapkt_op_mode =
14096 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14097 	spkt->satapkt_comp = NULL;
14098 	scmd = &spkt->satapkt_cmd;
14099 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14100 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14101 	scmd->satacmd_addr_type = 0;
14102 	scmd->satacmd_device_reg = 0;
14103 	scmd->satacmd_status_reg = 0;
14104 	scmd->satacmd_error_reg = 0;
14105 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14106 	scmd->satacmd_features_reg = cache_op;
14107 
14108 	/* Transfer command to HBA */
14109 	hba_rval = (*SATA_START_FUNC(sata_hba_inst))(
14110 	    SATA_DIP(sata_hba_inst), spkt);
14111 
14112 #ifdef SATA_INJECT_FAULTS
14113 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
14114 #endif
14115 
14116 	if ((hba_rval != SATA_TRAN_ACCEPTED) ||
14117 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
14118 		/* Pkt execution failed */
14119 		switch (cache_op) {
14120 		case SATAC_SF_ENABLE_READ_AHEAD:
14121 			infop = "enabling read ahead failed";
14122 			break;
14123 		case SATAC_SF_DISABLE_READ_AHEAD:
14124 			infop = "disabling read ahead failed";
14125 			break;
14126 		case SATAC_SF_ENABLE_WRITE_CACHE:
14127 			infop = "enabling write cache failed";
14128 			break;
14129 		case SATAC_SF_DISABLE_WRITE_CACHE:
14130 			infop = "disabling write cache failed";
14131 			break;
14132 		}
14133 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
14134 		rval = SATA_RETRY;
14135 	}
14136 failure:
14137 	/* Free allocated resources */
14138 	if (spkt != NULL)
14139 		sata_pkt_free(spx);
14140 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14141 	return (rval);
14142 }
14143 
14144 /*
14145  * Set Removable Media Status Notification (enable/disable)
14146  * state == 0 , disable
14147  * state != 0 , enable
14148  *
14149  * If operation fails, system log messgage is emitted.
14150  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
14151  */
14152 
14153 static int
14154 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
14155     int state)
14156 {
14157 	sata_pkt_t *spkt;
14158 	sata_cmd_t *scmd;
14159 	sata_pkt_txlate_t *spx;
14160 	int rval = SATA_SUCCESS;
14161 	char *infop;
14162 
14163 	ASSERT(sdinfo != NULL);
14164 	ASSERT(sata_hba_inst != NULL);
14165 
14166 	/* Prepare packet for SET FEATURES COMMAND */
14167 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14168 	spx->txlt_sata_hba_inst = sata_hba_inst;
14169 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
14170 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14171 	if (spkt == NULL) {
14172 		rval = SATA_FAILURE;
14173 		goto failure;
14174 	}
14175 	/* Fill sata_pkt */
14176 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14177 	/* Timeout 30s */
14178 	spkt->satapkt_time = sata_default_pkt_time;
14179 	/* Synchronous mode, no callback, interrupts */
14180 	spkt->satapkt_op_mode =
14181 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14182 	spkt->satapkt_comp = NULL;
14183 	scmd = &spkt->satapkt_cmd;
14184 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14185 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14186 	scmd->satacmd_addr_type = 0;
14187 	scmd->satacmd_device_reg = 0;
14188 	scmd->satacmd_status_reg = 0;
14189 	scmd->satacmd_error_reg = 0;
14190 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14191 	if (state == 0)
14192 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN;
14193 	else
14194 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN;
14195 
14196 	/* Transfer command to HBA */
14197 	if (((*SATA_START_FUNC(sata_hba_inst))(
14198 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
14199 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
14200 		/* Pkt execution failed */
14201 		if (state == 0)
14202 			infop = "disabling Removable Media Status "
14203 			    "Notification failed";
14204 		else
14205 			infop = "enabling Removable Media Status "
14206 			    "Notification failed";
14207 
14208 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
14209 		rval = SATA_FAILURE;
14210 	}
14211 failure:
14212 	/* Free allocated resources */
14213 	if (spkt != NULL)
14214 		sata_pkt_free(spx);
14215 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14216 	return (rval);
14217 }
14218 
14219 
14220 /*
14221  * Update state and copy port ss* values from passed sata_device structure.
14222  * sata_address is validated - if not valid, nothing is changed in sata_scsi
14223  * configuration struct.
14224  *
14225  * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function
14226  * regardless of the state in device argument.
14227  *
14228  * Port mutex should be held while calling this function.
14229  */
14230 static void
14231 sata_update_port_info(sata_hba_inst_t *sata_hba_inst,
14232     sata_device_t *sata_device)
14233 {
14234 	sata_cport_info_t *cportinfo;
14235 
14236 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT ||
14237 	    sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
14238 		if (SATA_NUM_CPORTS(sata_hba_inst) <=
14239 		    sata_device->satadev_addr.cport)
14240 			return;
14241 
14242 		cportinfo = SATA_CPORT_INFO(sata_hba_inst,
14243 		    sata_device->satadev_addr.cport);
14244 
14245 		ASSERT(mutex_owned(&cportinfo->cport_mutex));
14246 		cportinfo->cport_scr = sata_device->satadev_scr;
14247 
14248 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
14249 		cportinfo->cport_state &= ~(SATA_PSTATE_PWRON |
14250 		    SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
14251 		cportinfo->cport_state |=
14252 		    sata_device->satadev_state & SATA_PSTATE_VALID;
14253 	}
14254 }
14255 
14256 void
14257 sata_update_pmport_info(sata_hba_inst_t *sata_hba_inst,
14258     sata_device_t *sata_device)
14259 {
14260 	sata_pmport_info_t *pmportinfo;
14261 
14262 	if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT &&
14263 	    sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) ||
14264 	    SATA_NUM_PMPORTS(sata_hba_inst,
14265 	    sata_device->satadev_addr.cport) <
14266 	    sata_device->satadev_addr.pmport) {
14267 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
14268 		    "sata_update_port_info: error address %p.",
14269 		    &sata_device->satadev_addr);
14270 		return;
14271 	}
14272 
14273 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
14274 	    sata_device->satadev_addr.cport,
14275 	    sata_device->satadev_addr.pmport);
14276 
14277 	ASSERT(mutex_owned(&pmportinfo->pmport_mutex));
14278 	pmportinfo->pmport_scr = sata_device->satadev_scr;
14279 
14280 	/* Preserve SATA_PSTATE_SHUTDOWN flag */
14281 	pmportinfo->pmport_state &=
14282 	    ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
14283 	pmportinfo->pmport_state |=
14284 	    sata_device->satadev_state & SATA_PSTATE_VALID;
14285 }
14286 
14287 /*
14288  * Extract SATA port specification from an IOCTL argument.
14289  *
14290  * This function return the port the user land send us as is, unless it
14291  * cannot retrieve port spec, then -1 is returned.
14292  *
14293  * Support port multiplier.
14294  */
14295 static int32_t
14296 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp)
14297 {
14298 	int32_t port;
14299 
14300 	/* Extract port number from nvpair in dca structure  */
14301 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
14302 		SATA_LOG_D((sata_hba_inst, CE_NOTE,
14303 		    "sata_get_port_num: invalid port spec 0x%x in ioctl",
14304 		    port));
14305 		port = -1;
14306 	}
14307 
14308 	return (port);
14309 }
14310 
14311 /*
14312  * Get dev_info_t pointer to the device node pointed to by port argument.
14313  * NOTE: target argument is a value used in ioctls to identify
14314  * the AP - it is not a sata_address.
14315  * It is a combination of cport, pmport and address qualifier, encodded same
14316  * way as a scsi target number.
14317  * At this moment it carries only cport number.
14318  *
14319  * PMult hotplug is supported now.
14320  *
14321  * Returns dev_info_t pointer if target device was found, NULL otherwise.
14322  */
14323 
14324 static dev_info_t *
14325 sata_get_target_dip(dev_info_t *dip, uint8_t cport, uint8_t pmport)
14326 {
14327 	dev_info_t	*cdip = NULL;
14328 	int		target, tgt;
14329 	int 		circ;
14330 	uint8_t		qual;
14331 
14332 	sata_hba_inst_t	*sata_hba_inst;
14333 	scsi_hba_tran_t *scsi_hba_tran;
14334 
14335 	/* Get target id */
14336 	scsi_hba_tran = ddi_get_driver_private(dip);
14337 	if (scsi_hba_tran == NULL)
14338 		return (NULL);
14339 
14340 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
14341 
14342 	if (sata_hba_inst == NULL)
14343 		return (NULL);
14344 
14345 	/* Identify a port-mult by cport_info.cport_dev_type */
14346 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT)
14347 		qual = SATA_ADDR_DPMPORT;
14348 	else
14349 		qual = SATA_ADDR_DCPORT;
14350 
14351 	target = SATA_TO_SCSI_TARGET(cport, pmport, qual);
14352 
14353 	/* Retrieve target dip */
14354 	ndi_devi_enter(dip, &circ);
14355 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
14356 		dev_info_t *next = ddi_get_next_sibling(cdip);
14357 
14358 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
14359 		    DDI_PROP_DONTPASS, "target", -1);
14360 		if (tgt == -1) {
14361 			/*
14362 			 * This is actually an error condition, but not
14363 			 * a fatal one. Just continue the search.
14364 			 */
14365 			cdip = next;
14366 			continue;
14367 		}
14368 
14369 		if (tgt == target)
14370 			break;
14371 
14372 		cdip = next;
14373 	}
14374 	ndi_devi_exit(dip, circ);
14375 
14376 	return (cdip);
14377 }
14378 
14379 /*
14380  * Get dev_info_t pointer to the device node pointed to by port argument.
14381  * NOTE: target argument is a value used in ioctls to identify
14382  * the AP - it is not a sata_address.
14383  * It is a combination of cport, pmport and address qualifier, encoded same
14384  * way as a scsi target number.
14385  *
14386  * Returns dev_info_t pointer if target device was found, NULL otherwise.
14387  */
14388 
14389 static dev_info_t *
14390 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr)
14391 {
14392 	dev_info_t	*cdip = NULL;
14393 	int		target, tgt;
14394 	int 		circ;
14395 
14396 	target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual);
14397 
14398 	ndi_devi_enter(dip, &circ);
14399 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
14400 		dev_info_t *next = ddi_get_next_sibling(cdip);
14401 
14402 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
14403 		    DDI_PROP_DONTPASS, "target", -1);
14404 		if (tgt == -1) {
14405 			/*
14406 			 * This is actually an error condition, but not
14407 			 * a fatal one. Just continue the search.
14408 			 */
14409 			cdip = next;
14410 			continue;
14411 		}
14412 
14413 		if (tgt == target)
14414 			break;
14415 
14416 		cdip = next;
14417 	}
14418 	ndi_devi_exit(dip, circ);
14419 
14420 	return (cdip);
14421 }
14422 
14423 /*
14424  * Process sata port disconnect request.
14425  * Normally, cfgadm sata plugin will try to offline (unconfigure) the device
14426  * before this request. Nevertheless, if a device is still configured,
14427  * we need to attempt to offline and unconfigure device.
14428  * Regardless of the unconfigure operation results the port is marked as
14429  * deactivated and no access to the attached device is possible.
14430  * If the target node remains because unconfigure operation failed, its state
14431  * will be set to DEVICE_REMOVED, preventing it to be used again when a device
14432  * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure
14433  * the device and remove old target node.
14434  *
14435  * This function invokes sata_hba_inst->satahba_tran->
14436  * sata_tran_hotplug_ops->sata_tran_port_deactivate().
14437  * If successful, the device structure (if any) attached to the specified port
14438  * is removed and state of the port marked appropriately.
14439  * Failure of the port_deactivate may keep port in the physically active state,
14440  * or may fail the port.
14441  *
14442  * NOTE: Port multiplier is supported.
14443  */
14444 
14445 static int
14446 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst,
14447     sata_device_t *sata_device)
14448 {
14449 	sata_drive_info_t *sdinfo = NULL, *subsdinfo = NULL;
14450 	sata_cport_info_t *cportinfo = NULL;
14451 	sata_pmport_info_t *pmportinfo = NULL;
14452 	sata_pmult_info_t *pmultinfo = NULL;
14453 	sata_device_t subsdevice;
14454 	int cport, pmport, qual;
14455 	int rval = SATA_SUCCESS;
14456 	int npmport = 0;
14457 	int rv = 0;
14458 
14459 	cport = sata_device->satadev_addr.cport;
14460 	pmport = sata_device->satadev_addr.pmport;
14461 	qual = sata_device->satadev_addr.qual;
14462 
14463 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
14464 	if (qual == SATA_ADDR_DCPORT)
14465 		qual = SATA_ADDR_CPORT;
14466 	else
14467 		qual = SATA_ADDR_PMPORT;
14468 
14469 	/*
14470 	 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran->
14471 	 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
14472 	 * Do the sanity check.
14473 	 */
14474 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
14475 		/* No physical port deactivation supported. */
14476 		return (EINVAL);
14477 	}
14478 
14479 	/* Check the current state of the port */
14480 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14481 	    (SATA_DIP(sata_hba_inst), sata_device);
14482 
14483 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
14484 
14485 	/*
14486 	 * Processing port mulitiplier
14487 	 */
14488 	if (qual == SATA_ADDR_CPORT &&
14489 	    SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) {
14490 		mutex_enter(&cportinfo->cport_mutex);
14491 
14492 		/* Check controller port status */
14493 		sata_update_port_info(sata_hba_inst, sata_device);
14494 		if (rval != SATA_SUCCESS ||
14495 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14496 			/*
14497 			 * Device port status is unknown or it is in failed
14498 			 * state
14499 			 */
14500 			SATA_CPORT_STATE(sata_hba_inst, cport) =
14501 			    SATA_PSTATE_FAILED;
14502 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14503 			    "sata_hba_ioctl: connect: failed to deactivate "
14504 			    "SATA port %d", cport);
14505 			mutex_exit(&cportinfo->cport_mutex);
14506 			return (EIO);
14507 		}
14508 
14509 		/* Disconnect all sub-devices. */
14510 		pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
14511 		if (pmultinfo != NULL) {
14512 
14513 			for (npmport = 0; npmport < SATA_NUM_PMPORTS(
14514 			    sata_hba_inst, cport); npmport ++) {
14515 				subsdinfo = SATA_PMPORT_DRV_INFO(
14516 				    sata_hba_inst, cport, npmport);
14517 				if (subsdinfo == NULL)
14518 					continue;
14519 
14520 				subsdevice.satadev_addr = subsdinfo->
14521 				    satadrv_addr;
14522 
14523 				mutex_exit(&cportinfo->cport_mutex);
14524 				if (sata_ioctl_disconnect(sata_hba_inst,
14525 				    &subsdevice) == SATA_SUCCESS) {
14526 					SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
14527 					"[Remove] device at port %d:%d "
14528 					"successfully.", cport, npmport);
14529 				}
14530 				mutex_enter(&cportinfo->cport_mutex);
14531 			}
14532 		}
14533 
14534 		/* Disconnect the port multiplier */
14535 		cportinfo->cport_state &= ~SATA_STATE_READY;
14536 		mutex_exit(&cportinfo->cport_mutex);
14537 
14538 		sata_device->satadev_addr.qual = qual;
14539 		rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
14540 		    (SATA_DIP(sata_hba_inst), sata_device);
14541 
14542 		sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14543 		    SE_NO_HINT);
14544 
14545 		mutex_enter(&cportinfo->cport_mutex);
14546 		sata_update_port_info(sata_hba_inst, sata_device);
14547 		if (rval != SATA_SUCCESS &&
14548 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
14549 			cportinfo->cport_state = SATA_PSTATE_FAILED;
14550 			rv = EIO;
14551 		} else {
14552 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
14553 		}
14554 		mutex_exit(&cportinfo->cport_mutex);
14555 
14556 		return (rv);
14557 	}
14558 
14559 	/*
14560 	 * Process non-port-multiplier device - it could be a drive connected
14561 	 * to a port multiplier port or a controller port.
14562 	 */
14563 	if (qual == SATA_ADDR_PMPORT) {
14564 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14565 		mutex_enter(&pmportinfo->pmport_mutex);
14566 		sata_update_pmport_info(sata_hba_inst, sata_device);
14567 		if (rval != SATA_SUCCESS ||
14568 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14569 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
14570 			    SATA_PSTATE_FAILED;
14571 			SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
14572 			    "sata_hba_ioctl: connect: failed to deactivate "
14573 			    "SATA port %d:%d", cport, pmport);
14574 			mutex_exit(&pmportinfo->pmport_mutex);
14575 			return (EIO);
14576 		}
14577 
14578 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
14579 			sdinfo = pmportinfo->pmport_sata_drive;
14580 			ASSERT(sdinfo != NULL);
14581 		}
14582 
14583 		/*
14584 		 * Set port's dev_state to not ready - this will disable
14585 		 * an access to a potentially attached device.
14586 		 */
14587 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
14588 
14589 		/* Remove and release sata_drive info structure. */
14590 		if (sdinfo != NULL) {
14591 			if ((sdinfo->satadrv_type &
14592 			    SATA_VALID_DEV_TYPE) != 0) {
14593 				/*
14594 				 * If a target node exists, try to offline
14595 				 * a device and remove target node.
14596 				 */
14597 				mutex_exit(&pmportinfo->pmport_mutex);
14598 				(void) sata_offline_device(sata_hba_inst,
14599 				    sata_device, sdinfo);
14600 				mutex_enter(&pmportinfo->pmport_mutex);
14601 			}
14602 
14603 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
14604 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
14605 			(void) kmem_free((void *)sdinfo,
14606 			    sizeof (sata_drive_info_t));
14607 		}
14608 		mutex_exit(&pmportinfo->pmport_mutex);
14609 
14610 	} else if (qual == SATA_ADDR_CPORT) {
14611 		mutex_enter(&cportinfo->cport_mutex);
14612 		sata_update_port_info(sata_hba_inst, sata_device);
14613 		if (rval != SATA_SUCCESS ||
14614 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14615 			/*
14616 			 * Device port status is unknown or it is in failed
14617 			 * state
14618 			 */
14619 			SATA_CPORT_STATE(sata_hba_inst, cport) =
14620 			    SATA_PSTATE_FAILED;
14621 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14622 			    "sata_hba_ioctl: connect: failed to deactivate "
14623 			    "SATA port %d", cport);
14624 			mutex_exit(&cportinfo->cport_mutex);
14625 			return (EIO);
14626 		}
14627 
14628 		if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
14629 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
14630 			ASSERT(pmultinfo != NULL);
14631 		} else if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
14632 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14633 			ASSERT(sdinfo != NULL);
14634 		}
14635 		cportinfo->cport_state &= ~SATA_STATE_READY;
14636 
14637 		if (sdinfo != NULL) {
14638 			if ((sdinfo->satadrv_type &
14639 			    SATA_VALID_DEV_TYPE) != 0) {
14640 				/*
14641 				 * If a target node exists, try to offline
14642 				 * a device and remove target node.
14643 				 */
14644 				mutex_exit(&cportinfo->cport_mutex);
14645 				(void) sata_offline_device(sata_hba_inst,
14646 				    sata_device, sdinfo);
14647 				mutex_enter(&cportinfo->cport_mutex);
14648 			}
14649 
14650 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14651 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14652 			(void) kmem_free((void *)sdinfo,
14653 			    sizeof (sata_drive_info_t));
14654 		}
14655 		mutex_exit(&cportinfo->cport_mutex);
14656 	}
14657 
14658 	/* Just ask HBA driver to deactivate port */
14659 	sata_device->satadev_addr.qual = qual;
14660 
14661 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
14662 	    (SATA_DIP(sata_hba_inst), sata_device);
14663 
14664 	/*
14665 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14666 	 * without the hint (to force listener to investivate the state).
14667 	 */
14668 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14669 	    SE_NO_HINT);
14670 
14671 	if (qual == SATA_ADDR_PMPORT) {
14672 		mutex_enter(&pmportinfo->pmport_mutex);
14673 		sata_update_pmport_info(sata_hba_inst, sata_device);
14674 
14675 		if (rval != SATA_SUCCESS &&
14676 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
14677 			/*
14678 			 * Port deactivation failure - do not change port
14679 			 * state unless the state returned by HBA indicates a
14680 			 * port failure.
14681 			 *
14682 			 * NOTE: device structures were released, so devices
14683 			 * now are invisible! Port reset is needed to
14684 			 * re-enumerate devices.
14685 			 */
14686 			pmportinfo->pmport_state = SATA_PSTATE_FAILED;
14687 			rv = EIO;
14688 		} else {
14689 			/*
14690 			 * Deactivation succeded. From now on the sata framework
14691 			 * will not care what is happening to the device, until
14692 			 * the port is activated again.
14693 			 */
14694 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
14695 		}
14696 		mutex_exit(&pmportinfo->pmport_mutex);
14697 	} else if (qual == SATA_ADDR_CPORT) {
14698 		mutex_enter(&cportinfo->cport_mutex);
14699 		sata_update_port_info(sata_hba_inst, sata_device);
14700 
14701 		if (rval != SATA_SUCCESS &&
14702 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
14703 			cportinfo->cport_state = SATA_PSTATE_FAILED;
14704 			rv = EIO;
14705 		} else {
14706 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
14707 		}
14708 		mutex_exit(&cportinfo->cport_mutex);
14709 	}
14710 
14711 	return (rv);
14712 }
14713 
14714 
14715 
14716 /*
14717  * Process sata port connect request
14718  * The sata cfgadm pluging will invoke this operation only if port was found
14719  * in the disconnect state (failed state is also treated as the disconnected
14720  * state).
14721  * DEVCTL_AP_CONNECT would invoke  sata_hba_inst->satahba_tran->
14722  * sata_tran_hotplug_ops->sata_tran_port_activate().
14723  * If successful and a device is found attached to the port,
14724  * the initialization sequence is executed to attach a device structure to
14725  * a port structure. The state of the port and a device would be set
14726  * appropriately.
14727  * The device is not set in configured state (system-wise) by this operation.
14728  *
14729  * Note, that activating the port may generate link events,
14730  * so it is important that following processing and the
14731  * event processing does not interfere with each other!
14732  *
14733  * This operation may remove port failed state and will
14734  * try to make port active and in good standing.
14735  *
14736  * NOTE: Port multiplier is supported.
14737  */
14738 
14739 static int
14740 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst,
14741     sata_device_t *sata_device)
14742 {
14743 	sata_pmport_info_t	*pmportinfo = NULL;
14744 	uint8_t cport, pmport, qual;
14745 	int rv = 0;
14746 
14747 	cport = sata_device->satadev_addr.cport;
14748 	pmport = sata_device->satadev_addr.pmport;
14749 	qual = sata_device->satadev_addr.qual;
14750 
14751 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
14752 	if (qual == SATA_ADDR_DCPORT)
14753 		qual = SATA_ADDR_CPORT;
14754 	else
14755 		qual = SATA_ADDR_PMPORT;
14756 
14757 	if (qual == SATA_ADDR_PMPORT)
14758 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14759 
14760 	/*
14761 	 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->
14762 	 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate().
14763 	 * Perform sanity check now.
14764 	 */
14765 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) {
14766 		/* No physical port activation supported. */
14767 		return (EINVAL);
14768 	}
14769 
14770 	/* Just ask HBA driver to activate port */
14771 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
14772 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
14773 		/*
14774 		 * Port activation failure.
14775 		 */
14776 		if (qual == SATA_ADDR_CPORT) {
14777 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14778 			    cport)->cport_mutex);
14779 			sata_update_port_info(sata_hba_inst, sata_device);
14780 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
14781 				SATA_CPORT_STATE(sata_hba_inst, cport) =
14782 				    SATA_PSTATE_FAILED;
14783 				SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14784 				    "sata_hba_ioctl: connect: failed to "
14785 				    "activate SATA port %d", cport);
14786 			}
14787 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14788 			    cport)->cport_mutex);
14789 		} else { /* port multiplier device port */
14790 			mutex_enter(&pmportinfo->pmport_mutex);
14791 			sata_update_pmport_info(sata_hba_inst, sata_device);
14792 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
14793 				SATA_PMPORT_STATE(sata_hba_inst, cport,
14794 				    pmport) = SATA_PSTATE_FAILED;
14795 				SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
14796 				    "sata_hba_ioctl: connect: failed to "
14797 				    "activate SATA port %d:%d", cport, pmport);
14798 			}
14799 			mutex_exit(&pmportinfo->pmport_mutex);
14800 		}
14801 		return (EIO);
14802 	}
14803 
14804 	/* Virgin port state - will be updated by the port re-probe. */
14805 	if (qual == SATA_ADDR_CPORT) {
14806 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14807 		    cport)->cport_mutex);
14808 		SATA_CPORT_STATE(sata_hba_inst, cport) = 0;
14809 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14810 		    cport)->cport_mutex);
14811 	} else { /* port multiplier device port */
14812 		mutex_enter(&pmportinfo->pmport_mutex);
14813 		SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0;
14814 		mutex_exit(&pmportinfo->pmport_mutex);
14815 	}
14816 
14817 	/*
14818 	 * Probe the port to find its state and attached device.
14819 	 */
14820 	if (sata_reprobe_port(sata_hba_inst, sata_device,
14821 	    SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE)
14822 		rv = EIO;
14823 
14824 	/*
14825 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14826 	 * without the hint
14827 	 */
14828 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14829 	    SE_NO_HINT);
14830 
14831 	/*
14832 	 * If there is a device attached to the port, emit
14833 	 * a message.
14834 	 */
14835 	if (sata_device->satadev_type != SATA_DTYPE_NONE) {
14836 
14837 		if (qual == SATA_ADDR_CPORT) {
14838 			if (sata_device->satadev_type == SATA_DTYPE_PMULT) {
14839 				sata_log(sata_hba_inst, CE_WARN,
14840 				    "SATA port multiplier detected "
14841 				    "at port %d", cport);
14842 			} else {
14843 				sata_log(sata_hba_inst, CE_WARN,
14844 				    "SATA device detected at port %d", cport);
14845 				if (sata_device->satadev_type ==
14846 				    SATA_DTYPE_UNKNOWN) {
14847 				/*
14848 				 * A device was not successfully identified
14849 				 */
14850 				sata_log(sata_hba_inst, CE_WARN,
14851 				    "Could not identify SATA "
14852 				    "device at port %d", cport);
14853 				}
14854 			}
14855 		} else { /* port multiplier device port */
14856 			sata_log(sata_hba_inst, CE_WARN,
14857 			    "SATA device detected at port %d:%d",
14858 			    cport, pmport);
14859 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
14860 				/*
14861 				 * A device was not successfully identified
14862 				 */
14863 				sata_log(sata_hba_inst, CE_WARN,
14864 				    "Could not identify SATA "
14865 				    "device at port %d:%d", cport, pmport);
14866 			}
14867 		}
14868 	}
14869 
14870 	return (rv);
14871 }
14872 
14873 
14874 /*
14875  * Process sata device unconfigure request.
14876  * The unconfigure operation uses generic nexus operation to
14877  * offline a device. It leaves a target device node attached.
14878  * and obviously sata_drive_info attached as well, because
14879  * from the hardware point of view nothing has changed.
14880  */
14881 static int
14882 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst,
14883     sata_device_t *sata_device)
14884 {
14885 	int rv = 0;
14886 	dev_info_t *tdip;
14887 
14888 	/* We are addressing attached device, not a port */
14889 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
14890 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
14891 	else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT)
14892 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
14893 
14894 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
14895 	    &sata_device->satadev_addr)) != NULL) {
14896 
14897 		if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) {
14898 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14899 			    "sata_hba_ioctl: unconfigure: "
14900 			    "failed to unconfigure device at SATA port %d:%d",
14901 			    sata_device->satadev_addr.cport,
14902 			    sata_device->satadev_addr.pmport));
14903 			rv = EIO;
14904 		}
14905 		/*
14906 		 * The target node devi_state should be marked with
14907 		 * DEVI_DEVICE_OFFLINE by ndi_devi_offline().
14908 		 * This would be the indication for cfgadm that
14909 		 * the AP node occupant state is 'unconfigured'.
14910 		 */
14911 
14912 	} else {
14913 		/*
14914 		 * This would indicate a failure on the part of cfgadm
14915 		 * to detect correct state of the node prior to this
14916 		 * call - one cannot unconfigure non-existing device.
14917 		 */
14918 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14919 		    "sata_hba_ioctl: unconfigure: "
14920 		    "attempt to unconfigure non-existing device "
14921 		    "at SATA port %d:%d",
14922 		    sata_device->satadev_addr.cport,
14923 		    sata_device->satadev_addr.pmport));
14924 		rv = ENXIO;
14925 	}
14926 	return (rv);
14927 }
14928 
14929 /*
14930  * Process sata device configure request
14931  * If port is in a failed state, operation is aborted - one has to use
14932  * an explicit connect or port activate request to try to get a port into
14933  * non-failed mode. Port reset wil also work in such situation.
14934  * If the port is in disconnected (shutdown) state, the connect operation is
14935  * attempted prior to any other action.
14936  * When port is in the active state, there is a device attached and the target
14937  * node exists, a device was most likely offlined.
14938  * If target node does not exist, a new target node is created. In both cases
14939  * an attempt is made to online (configure) the device.
14940  *
14941  * NOTE: Port multiplier is supported.
14942  */
14943 static int
14944 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst,
14945     sata_device_t *sata_device)
14946 {
14947 	int cport, pmport, qual;
14948 	int rval;
14949 	boolean_t target = B_TRUE;
14950 	sata_cport_info_t *cportinfo;
14951 	sata_pmport_info_t *pmportinfo = NULL;
14952 	dev_info_t *tdip;
14953 	sata_drive_info_t *sdinfo;
14954 
14955 	cport = sata_device->satadev_addr.cport;
14956 	pmport = sata_device->satadev_addr.pmport;
14957 	qual = sata_device->satadev_addr.qual;
14958 
14959 	/* Get current port state */
14960 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14961 	    (SATA_DIP(sata_hba_inst), sata_device);
14962 
14963 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
14964 	if (qual == SATA_ADDR_DPMPORT) {
14965 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14966 		mutex_enter(&pmportinfo->pmport_mutex);
14967 		sata_update_pmport_info(sata_hba_inst, sata_device);
14968 		if (rval != SATA_SUCCESS ||
14969 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14970 			/*
14971 			 * Obviously, device on a failed port is not visible
14972 			 */
14973 			mutex_exit(&pmportinfo->pmport_mutex);
14974 			return (ENXIO);
14975 		}
14976 		mutex_exit(&pmportinfo->pmport_mutex);
14977 	} else {
14978 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14979 		    cport)->cport_mutex);
14980 		sata_update_port_info(sata_hba_inst, sata_device);
14981 		if (rval != SATA_SUCCESS ||
14982 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14983 			/*
14984 			 * Obviously, device on a failed port is not visible
14985 			 */
14986 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14987 			    cport)->cport_mutex);
14988 			return (ENXIO);
14989 		}
14990 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14991 		    cport)->cport_mutex);
14992 	}
14993 
14994 	if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) {
14995 		/* need to activate port */
14996 		target = B_FALSE;
14997 
14998 		/* Sanity check */
14999 		if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
15000 			return (ENXIO);
15001 
15002 		/* Just let HBA driver to activate port */
15003 		if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
15004 		    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15005 			/*
15006 			 * Port activation failure - do not change port state
15007 			 * unless the state returned by HBA indicates a port
15008 			 * failure.
15009 			 */
15010 			if (qual == SATA_ADDR_DPMPORT) {
15011 				mutex_enter(&pmportinfo->pmport_mutex);
15012 				sata_update_pmport_info(sata_hba_inst,
15013 				    sata_device);
15014 				if (sata_device->satadev_state &
15015 				    SATA_PSTATE_FAILED)
15016 					pmportinfo->pmport_state =
15017 					    SATA_PSTATE_FAILED;
15018 				mutex_exit(&pmportinfo->pmport_mutex);
15019 			} else {
15020 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15021 				    cport)->cport_mutex);
15022 				sata_update_port_info(sata_hba_inst,
15023 				    sata_device);
15024 				if (sata_device->satadev_state &
15025 				    SATA_PSTATE_FAILED)
15026 					cportinfo->cport_state =
15027 					    SATA_PSTATE_FAILED;
15028 				mutex_exit(&SATA_CPORT_INFO(
15029 				    sata_hba_inst, cport)->cport_mutex);
15030 			}
15031 		}
15032 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15033 		    "sata_hba_ioctl: configure: "
15034 		    "failed to activate SATA port %d:%d",
15035 		    cport, pmport));
15036 		return (EIO);
15037 	}
15038 	/*
15039 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15040 	 * without the hint.
15041 	 */
15042 	sata_gen_sysevent(sata_hba_inst,
15043 	    &sata_device->satadev_addr, SE_NO_HINT);
15044 
15045 	/* Virgin port state */
15046 	if (qual == SATA_ADDR_DPMPORT) {
15047 		mutex_enter(&pmportinfo->pmport_mutex);
15048 		pmportinfo->pmport_state = 0;
15049 		mutex_exit(&pmportinfo->pmport_mutex);
15050 	} else {
15051 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15052 		    cport)-> cport_mutex);
15053 		cportinfo->cport_state = 0;
15054 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15055 		    cport)->cport_mutex);
15056 	}
15057 	/*
15058 	 * Always reprobe port, to get current device info.
15059 	 */
15060 	if (sata_reprobe_port(sata_hba_inst, sata_device,
15061 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
15062 		return (EIO);
15063 
15064 	if (sata_device->satadev_type != SATA_DTYPE_NONE && target == B_FALSE) {
15065 		if (qual == SATA_ADDR_DPMPORT) {
15066 			/*
15067 			 * That's the transition from "inactive" port
15068 			 * to active one with device attached.
15069 			 */
15070 			sata_log(sata_hba_inst, CE_WARN,
15071 			    "SATA device detected at port %d:%d",
15072 			    cport, pmport);
15073 		} else {
15074 			/*
15075 			 * When PM is attached to the cport and cport is
15076 			 * activated, every PM device port needs to be reprobed.
15077 			 * We need to emit message for all devices detected
15078 			 * at port multiplier's device ports.
15079 			 * Add such code here.
15080 			 * For now, just inform about device attached to
15081 			 * cport.
15082 			 */
15083 			sata_log(sata_hba_inst, CE_WARN,
15084 			    "SATA device detected at port %d", cport);
15085 		}
15086 	}
15087 
15088 	/*
15089 	 * This is where real configuration operation starts.
15090 	 *
15091 	 * When PM is attached to the cport and cport is activated,
15092 	 * devices attached PM device ports may have to be configured
15093 	 * explicitly. This may change when port multiplier is supported.
15094 	 * For now, configure only disks and other valid target devices.
15095 	 */
15096 	if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) {
15097 		if (qual == SATA_ADDR_DCPORT) {
15098 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
15099 				/*
15100 				 * A device was not successfully identified
15101 				 */
15102 				sata_log(sata_hba_inst, CE_WARN,
15103 				    "Could not identify SATA "
15104 				    "device at port %d", cport);
15105 			}
15106 		} else { /* port multiplier device port */
15107 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
15108 				/*
15109 				 * A device was not successfully identified
15110 				 */
15111 				sata_log(sata_hba_inst, CE_WARN,
15112 				    "Could not identify SATA "
15113 				    "device at port %d:%d", cport, pmport);
15114 			}
15115 		}
15116 		return (ENXIO);		/* No device to configure */
15117 	}
15118 
15119 	/*
15120 	 * Here we may have a device in reset condition,
15121 	 * but because we are just configuring it, there is
15122 	 * no need to process the reset other than just
15123 	 * to clear device reset condition in the HBA driver.
15124 	 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will
15125 	 * cause a first command sent the HBA driver with the request
15126 	 * to clear device reset condition.
15127 	 */
15128 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15129 	if (qual == SATA_ADDR_DPMPORT)
15130 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15131 	else
15132 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
15133 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
15134 	if (sdinfo == NULL) {
15135 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15136 		return (ENXIO);
15137 	}
15138 	if (sdinfo->satadrv_event_flags &
15139 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
15140 		sdinfo->satadrv_event_flags = 0;
15141 	}
15142 	sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
15143 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15144 
15145 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
15146 	    &sata_device->satadev_addr)) != NULL) {
15147 		/*
15148 		 * Target node exists. Verify, that it belongs
15149 		 * to existing, attached device and not to
15150 		 * a removed device.
15151 		 */
15152 		if (sata_check_device_removed(tdip) == B_TRUE) {
15153 			if (qual == SATA_ADDR_DPMPORT)
15154 				sata_log(sata_hba_inst, CE_WARN,
15155 				    "SATA device at port %d cannot be "
15156 				    "configured. "
15157 				    "Application(s) accessing "
15158 				    "previously attached device "
15159 				    "have to release it before newly "
15160 				    "inserted device can be made accessible.",
15161 				    cport);
15162 			else
15163 				sata_log(sata_hba_inst, CE_WARN,
15164 				    "SATA device at port %d:%d cannot be"
15165 				    "configured. "
15166 				    "Application(s) accessing "
15167 				    "previously attached device "
15168 				    "have to release it before newly "
15169 				    "inserted device can be made accessible.",
15170 				    cport, pmport);
15171 			return (EIO);
15172 		}
15173 		/*
15174 		 * Device was not removed and re-inserted.
15175 		 * Try to online it.
15176 		 */
15177 		if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) {
15178 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15179 			    "sata_hba_ioctl: configure: "
15180 			    "onlining device at SATA port "
15181 			    "%d:%d failed", cport, pmport));
15182 			return (EIO);
15183 		}
15184 
15185 		if (qual == SATA_ADDR_DPMPORT) {
15186 			mutex_enter(&pmportinfo->pmport_mutex);
15187 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
15188 			mutex_exit(&pmportinfo->pmport_mutex);
15189 		} else {
15190 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15191 			    cport)->cport_mutex);
15192 			cportinfo-> cport_tgtnode_clean = B_TRUE;
15193 			mutex_exit(&SATA_CPORT_INFO(
15194 			    sata_hba_inst, cport)->cport_mutex);
15195 		}
15196 	} else {
15197 		/*
15198 		 * No target node - need to create a new target node.
15199 		 */
15200 		if (qual == SATA_ADDR_DPMPORT) {
15201 			mutex_enter(&pmportinfo->pmport_mutex);
15202 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
15203 			mutex_exit(&pmportinfo->pmport_mutex);
15204 		} else {
15205 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15206 			    cport_mutex);
15207 			cportinfo-> cport_tgtnode_clean = B_TRUE;
15208 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15209 			    cport_mutex);
15210 		}
15211 
15212 		tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
15213 		    sata_hba_inst, &sata_device->satadev_addr);
15214 		if (tdip == NULL) {
15215 			/* Configure operation failed */
15216 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15217 			    "sata_hba_ioctl: configure: "
15218 			    "configuring SATA device at port %d:%d "
15219 			    "failed", cport, pmport));
15220 			return (EIO);
15221 		}
15222 	}
15223 	return (0);
15224 }
15225 
15226 
15227 /*
15228  * Process ioctl deactivate port request.
15229  * Arbitrarily unconfigure attached device, if any.
15230  * Even if the unconfigure fails, proceed with the
15231  * port deactivation.
15232  *
15233  * NOTE: Port Multiplier is supported now.
15234  */
15235 
15236 static int
15237 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst,
15238     sata_device_t *sata_device)
15239 {
15240 	int cport, pmport, qual;
15241 	int rval, rv = 0;
15242 	int npmport;
15243 	sata_cport_info_t *cportinfo;
15244 	sata_pmport_info_t *pmportinfo;
15245 	sata_pmult_info_t *pmultinfo;
15246 	dev_info_t *tdip;
15247 	sata_drive_info_t *sdinfo = NULL;
15248 	sata_device_t subsdevice;
15249 
15250 	/* Sanity check */
15251 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL)
15252 		return (ENOTSUP);
15253 
15254 	cport = sata_device->satadev_addr.cport;
15255 	pmport = sata_device->satadev_addr.pmport;
15256 	qual = sata_device->satadev_addr.qual;
15257 
15258 	/* SCSI_TO_SATA_ADDR_QUAL() translate ap_id into a device qualifier */
15259 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
15260 	if (qual == SATA_ADDR_DCPORT)
15261 		qual = SATA_ADDR_CPORT;
15262 	else
15263 		qual = SATA_ADDR_PMPORT;
15264 
15265 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
15266 	if (qual == SATA_ADDR_PMPORT)
15267 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
15268 
15269 	/*
15270 	 * Processing port multiplier
15271 	 */
15272 	if (qual == SATA_ADDR_CPORT &&
15273 	    SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) {
15274 		mutex_enter(&cportinfo->cport_mutex);
15275 
15276 		/* Deactivate all sub-deices */
15277 		pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
15278 		if (pmultinfo != NULL) {
15279 			for (npmport = 0; npmport < SATA_NUM_PMPORTS(
15280 			    sata_hba_inst, cport); npmport++) {
15281 
15282 				subsdevice.satadev_addr.cport = cport;
15283 				subsdevice.satadev_addr.pmport =
15284 				    (uint8_t)npmport;
15285 				subsdevice.satadev_addr.qual =
15286 				    SATA_ADDR_DPMPORT;
15287 
15288 				SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
15289 				    "sata_hba_ioctl: deactivate: trying to "
15290 				    "deactivate SATA port %d:%d",
15291 				    cport, npmport);
15292 
15293 				mutex_exit(&cportinfo->cport_mutex);
15294 				if (sata_ioctl_deactivate(sata_hba_inst,
15295 				    &subsdevice) == SATA_SUCCESS) {
15296 					SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
15297 					    "[Deactivate] device at port %d:%d "
15298 					    "successfully.", cport, npmport);
15299 				}
15300 				mutex_enter(&cportinfo->cport_mutex);
15301 			}
15302 		}
15303 
15304 		/* Deactivate the port multiplier now. */
15305 		cportinfo->cport_state &= ~SATA_STATE_READY;
15306 		mutex_exit(&cportinfo->cport_mutex);
15307 
15308 		sata_device->satadev_addr.qual = qual;
15309 		rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
15310 		    (SATA_DIP(sata_hba_inst), sata_device);
15311 
15312 		sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15313 		    SE_NO_HINT);
15314 
15315 		mutex_enter(&cportinfo->cport_mutex);
15316 		sata_update_port_info(sata_hba_inst, sata_device);
15317 		if (rval != SATA_SUCCESS) {
15318 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15319 				cportinfo->cport_state = SATA_PSTATE_FAILED;
15320 			}
15321 			rv = EIO;
15322 		} else {
15323 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
15324 		}
15325 		mutex_exit(&cportinfo->cport_mutex);
15326 
15327 		return (rv);
15328 	}
15329 
15330 	/*
15331 	 * Process non-port-multiplier device - it could be a drive connected
15332 	 * to a port multiplier port or a controller port.
15333 	 */
15334 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15335 	if (qual == SATA_ADDR_CPORT) {
15336 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
15337 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
15338 			/* deal only with valid devices */
15339 			if ((cportinfo->cport_dev_type &
15340 			    SATA_VALID_DEV_TYPE) != 0)
15341 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
15342 		}
15343 		cportinfo->cport_state &= ~SATA_STATE_READY;
15344 	} else {
15345 		/* Port multiplier device port */
15346 		mutex_enter(&pmportinfo->pmport_mutex);
15347 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15348 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
15349 		    (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0)
15350 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
15351 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
15352 		mutex_exit(&pmportinfo->pmport_mutex);
15353 	}
15354 
15355 	if (sdinfo != NULL) {
15356 		/*
15357 		 * If a target node exists, try to offline a device and
15358 		 * to remove a target node.
15359 		 */
15360 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15361 		    cport_mutex);
15362 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
15363 		    &sata_device->satadev_addr);
15364 		if (tdip != NULL) {
15365 			/* target node exist */
15366 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
15367 			    "sata_hba_ioctl: port deactivate: "
15368 			    "target node exists.", NULL);
15369 
15370 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) !=
15371 			    NDI_SUCCESS) {
15372 				SATA_LOG_D((sata_hba_inst, CE_WARN,
15373 				    "sata_hba_ioctl: port deactivate: "
15374 				    "failed to unconfigure device at port "
15375 				    "%d:%d before deactivating the port",
15376 				    cport, pmport));
15377 				/*
15378 				 * Set DEVICE REMOVED state in the target
15379 				 * node. It will prevent an access to
15380 				 * the device even when a new device is
15381 				 * attached, until the old target node is
15382 				 * released, removed and recreated for a new
15383 				 * device.
15384 				 */
15385 				sata_set_device_removed(tdip);
15386 
15387 				/*
15388 				 * Instruct the event daemon to try the
15389 				 * target node cleanup later.
15390 				 */
15391 				sata_set_target_node_cleanup(sata_hba_inst,
15392 				    &sata_device->satadev_addr);
15393 			}
15394 		}
15395 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15396 		    cport_mutex);
15397 		/*
15398 		 * In any case, remove and release sata_drive_info
15399 		 * structure.
15400 		 */
15401 		if (qual == SATA_ADDR_CPORT) {
15402 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
15403 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
15404 		} else { /* port multiplier device port */
15405 			mutex_enter(&pmportinfo->pmport_mutex);
15406 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
15407 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
15408 			mutex_exit(&pmportinfo->pmport_mutex);
15409 		}
15410 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
15411 	}
15412 
15413 	if (qual == SATA_ADDR_CPORT) {
15414 		cportinfo->cport_state &= ~(SATA_STATE_PROBED |
15415 		    SATA_STATE_PROBING);
15416 	} else if (qual == SATA_ADDR_PMPORT) {
15417 		mutex_enter(&pmportinfo->pmport_mutex);
15418 		pmportinfo->pmport_state &= ~(SATA_STATE_PROBED |
15419 		    SATA_STATE_PROBING);
15420 		mutex_exit(&pmportinfo->pmport_mutex);
15421 	}
15422 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15423 
15424 	/* Just let HBA driver to deactivate port */
15425 	sata_device->satadev_addr.qual = qual;
15426 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
15427 	    (SATA_DIP(sata_hba_inst), sata_device);
15428 
15429 	/*
15430 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15431 	 * without the hint
15432 	 */
15433 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15434 	    SE_NO_HINT);
15435 
15436 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15437 	sata_update_port_info(sata_hba_inst, sata_device);
15438 	if (qual == SATA_ADDR_CPORT) {
15439 		if (rval != SATA_SUCCESS) {
15440 			/*
15441 			 * Port deactivation failure - do not change port state
15442 			 * unless the state returned by HBA indicates a port
15443 			 * failure.
15444 			 */
15445 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15446 				SATA_CPORT_STATE(sata_hba_inst, cport) =
15447 				    SATA_PSTATE_FAILED;
15448 			}
15449 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15450 			    "sata_hba_ioctl: port deactivate: "
15451 			    "cannot deactivate SATA port %d", cport));
15452 			rv = EIO;
15453 		} else {
15454 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
15455 		}
15456 	} else {
15457 		mutex_enter(&pmportinfo->pmport_mutex);
15458 		if (rval != SATA_SUCCESS) {
15459 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15460 				SATA_PMPORT_STATE(sata_hba_inst, cport,
15461 				    pmport) = SATA_PSTATE_FAILED;
15462 			}
15463 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15464 			    "sata_hba_ioctl: port deactivate: "
15465 			    "cannot deactivate SATA port %d:%d",
15466 			    cport, pmport));
15467 			rv = EIO;
15468 		} else {
15469 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
15470 		}
15471 		mutex_exit(&pmportinfo->pmport_mutex);
15472 	}
15473 
15474 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15475 
15476 	return (rv);
15477 }
15478 
15479 /*
15480  * Process ioctl port activate request.
15481  *
15482  * NOTE: Port multiplier is supported now.
15483  */
15484 static int
15485 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst,
15486     sata_device_t *sata_device)
15487 {
15488 	int cport, pmport, qual;
15489 	sata_cport_info_t *cportinfo;
15490 	sata_pmport_info_t *pmportinfo = NULL;
15491 	boolean_t dev_existed = B_TRUE;
15492 
15493 	/* Sanity check */
15494 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
15495 		return (ENOTSUP);
15496 
15497 	cport = sata_device->satadev_addr.cport;
15498 	pmport = sata_device->satadev_addr.pmport;
15499 	qual = sata_device->satadev_addr.qual;
15500 
15501 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
15502 
15503 	/*
15504 	 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL())
15505 	 * is a device. But what we are dealing with is port/pmport.
15506 	 */
15507 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
15508 	if (qual == SATA_ADDR_DCPORT)
15509 		sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT;
15510 	else
15511 		sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT;
15512 
15513 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15514 	if (qual == SATA_ADDR_PMPORT) {
15515 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
15516 		if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN ||
15517 		    pmportinfo->pmport_dev_type == SATA_DTYPE_NONE)
15518 			dev_existed = B_FALSE;
15519 	} else { /* cport */
15520 		if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN ||
15521 		    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
15522 			dev_existed = B_FALSE;
15523 	}
15524 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15525 
15526 	/* Just let HBA driver to activate port, if necessary */
15527 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
15528 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15529 		/*
15530 		 * Port activation failure - do not change port state unless
15531 		 * the state returned by HBA indicates a port failure.
15532 		 */
15533 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15534 		    cport)->cport_mutex);
15535 		sata_update_port_info(sata_hba_inst, sata_device);
15536 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15537 			if (qual == SATA_ADDR_PMPORT) {
15538 				mutex_enter(&pmportinfo->pmport_mutex);
15539 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
15540 				mutex_exit(&pmportinfo->pmport_mutex);
15541 			} else
15542 				cportinfo->cport_state = SATA_PSTATE_FAILED;
15543 
15544 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15545 			    cport)->cport_mutex);
15546 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15547 			    "sata_hba_ioctl: port activate: cannot activate "
15548 			    "SATA port %d:%d", cport, pmport));
15549 			return (EIO);
15550 		}
15551 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15552 	}
15553 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15554 	if (qual == SATA_ADDR_PMPORT) {
15555 		mutex_enter(&pmportinfo->pmport_mutex);
15556 		pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN;
15557 		mutex_exit(&pmportinfo->pmport_mutex);
15558 	} else
15559 		cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN;
15560 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15561 
15562 	/*
15563 	 * Re-probe port to find its current state and possibly attached device.
15564 	 * Port re-probing may change the cportinfo device type if device is
15565 	 * found attached.
15566 	 * If port probing failed, the device type would be set to
15567 	 * SATA_DTYPE_NONE.
15568 	 */
15569 	(void) sata_reprobe_port(sata_hba_inst, sata_device,
15570 	    SATA_DEV_IDENTIFY_RETRY);
15571 
15572 	/*
15573 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15574 	 * without the hint.
15575 	 */
15576 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15577 	    SE_NO_HINT);
15578 
15579 	if (dev_existed == B_FALSE) {
15580 		if (qual == SATA_ADDR_PMPORT &&
15581 		    pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
15582 			/*
15583 			 * That's the transition from the "inactive" port state
15584 			 * or the active port without a device attached to the
15585 			 * active port state with a device attached.
15586 			 */
15587 			sata_log(sata_hba_inst, CE_WARN,
15588 			    "SATA device detected at port %d:%d",
15589 			    cport, pmport);
15590 		} else if (qual == SATA_ADDR_CPORT &&
15591 		    cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
15592 			/*
15593 			 * That's the transition from the "inactive" port state
15594 			 * or the active port without a device attached to the
15595 			 * active port state with a device attached.
15596 			 */
15597 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
15598 				sata_log(sata_hba_inst, CE_WARN,
15599 				    "SATA device detected at port %d", cport);
15600 			} else {
15601 				sata_log(sata_hba_inst, CE_WARN,
15602 				    "SATA port multiplier detected at port %d",
15603 				    cport);
15604 			}
15605 		}
15606 	}
15607 	return (0);
15608 }
15609 
15610 
15611 
15612 /*
15613  * Process ioctl reset port request.
15614  *
15615  * NOTE: Port-Multiplier is supported.
15616  */
15617 static int
15618 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst,
15619     sata_device_t *sata_device)
15620 {
15621 	int cport, pmport, qual;
15622 	int rv = 0;
15623 
15624 	cport = sata_device->satadev_addr.cport;
15625 	pmport = sata_device->satadev_addr.pmport;
15626 	qual = sata_device->satadev_addr.qual;
15627 
15628 	/*
15629 	 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL())
15630 	 * is a device. But what we are dealing with is port/pmport.
15631 	 */
15632 	if (qual == SATA_ADDR_DCPORT)
15633 		sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT;
15634 	else
15635 		sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT;
15636 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
15637 
15638 	/* Sanity check */
15639 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15640 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15641 		    "sata_hba_ioctl: sata_hba_tran missing required "
15642 		    "function sata_tran_reset_dport"));
15643 		return (ENOTSUP);
15644 	}
15645 
15646 	/* Ask HBA to reset port */
15647 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
15648 	    sata_device) != SATA_SUCCESS) {
15649 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15650 		    "sata_hba_ioctl: reset port: failed %d:%d",
15651 		    cport, pmport));
15652 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15653 		    cport_mutex);
15654 		sata_update_port_info(sata_hba_inst, sata_device);
15655 		if (qual == SATA_ADDR_CPORT)
15656 			SATA_CPORT_STATE(sata_hba_inst, cport) =
15657 			    SATA_PSTATE_FAILED;
15658 		else {
15659 			mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport,
15660 			    pmport));
15661 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
15662 			    SATA_PSTATE_FAILED;
15663 			mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport,
15664 			    pmport));
15665 		}
15666 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15667 		    cport_mutex);
15668 		rv = EIO;
15669 	}
15670 
15671 	return (rv);
15672 }
15673 
15674 /*
15675  * Process ioctl reset device request.
15676  *
15677  * NOTE: Port multiplier is supported.
15678  */
15679 static int
15680 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst,
15681     sata_device_t *sata_device)
15682 {
15683 	sata_drive_info_t *sdinfo = NULL;
15684 	sata_pmult_info_t *pmultinfo = NULL;
15685 	int cport, pmport;
15686 	int rv = 0;
15687 
15688 	/* Sanity check */
15689 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15690 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15691 		    "sata_hba_ioctl: sata_hba_tran missing required "
15692 		    "function sata_tran_reset_dport"));
15693 		return (ENOTSUP);
15694 	}
15695 
15696 	cport = sata_device->satadev_addr.cport;
15697 	pmport = sata_device->satadev_addr.pmport;
15698 
15699 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15700 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
15701 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
15702 		    SATA_DTYPE_PMULT)
15703 			pmultinfo = SATA_CPORT_INFO(sata_hba_inst, cport)->
15704 			    cport_devp.cport_sata_pmult;
15705 		else
15706 			sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
15707 			    sata_device->satadev_addr.cport);
15708 	} else { /* port multiplier */
15709 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15710 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
15711 		    sata_device->satadev_addr.cport,
15712 		    sata_device->satadev_addr.pmport);
15713 	}
15714 	if (sdinfo == NULL && pmultinfo == NULL) {
15715 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15716 		return (EINVAL);
15717 	}
15718 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15719 
15720 	/* Ask HBA to reset device */
15721 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
15722 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15723 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15724 		    "sata_hba_ioctl: reset device: failed at port %d:%d",
15725 		    cport, pmport));
15726 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15727 		    cport_mutex);
15728 		sata_update_port_info(sata_hba_inst, sata_device);
15729 		/*
15730 		 * Device info structure remains attached. Another device reset
15731 		 * or port disconnect/connect and re-probing is
15732 		 * needed to change it's state
15733 		 */
15734 		if (sdinfo != NULL) {
15735 			sdinfo->satadrv_state &= ~SATA_STATE_READY;
15736 			sdinfo->satadrv_state |= SATA_DSTATE_FAILED;
15737 		} else if (pmultinfo != NULL) {
15738 			pmultinfo->pmult_state &= ~SATA_STATE_READY;
15739 			pmultinfo->pmult_state |= SATA_DSTATE_FAILED;
15740 		}
15741 
15742 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15743 		rv = EIO;
15744 	}
15745 	/*
15746 	 * If attached device was a port multiplier, some extra processing
15747 	 * may be needed to bring it back. SATA specification requies a
15748 	 * mandatory software reset on host port to reliably enumerate a port
15749 	 * multiplier, the HBA driver should handle that after reset
15750 	 * operation.
15751 	 */
15752 	return (rv);
15753 }
15754 
15755 
15756 /*
15757  * Process ioctl reset all request.
15758  */
15759 static int
15760 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst)
15761 {
15762 	sata_device_t sata_device;
15763 	int rv = 0;
15764 	int tcport;
15765 
15766 	sata_device.satadev_rev = SATA_DEVICE_REV;
15767 
15768 	/*
15769 	 * There is no protection here for configured devices.
15770 	 */
15771 	/* Sanity check */
15772 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15773 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15774 		    "sata_hba_ioctl: sata_hba_tran missing required "
15775 		    "function sata_tran_reset_dport"));
15776 		return (ENOTSUP);
15777 	}
15778 
15779 	/*
15780 	 * Need to lock all ports, not just one.
15781 	 * If any port is locked by event processing, fail the whole operation.
15782 	 * One port is already locked, but for simplicity lock it again.
15783 	 */
15784 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
15785 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15786 		    cport_mutex);
15787 		if (((SATA_CPORT_INFO(sata_hba_inst, tcport)->
15788 		    cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) {
15789 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15790 			    cport_mutex);
15791 			rv = EBUSY;
15792 			break;
15793 		} else {
15794 			/*
15795 			 * It is enough to lock cport in command-based
15796 			 * switching mode.
15797 			 */
15798 			SATA_CPORT_INFO(sata_hba_inst, tcport)->
15799 			    cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
15800 		}
15801 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15802 		    cport_mutex);
15803 	}
15804 
15805 	if (rv == 0) {
15806 		/*
15807 		 * All cports were successfully locked.
15808 		 * Reset main SATA controller.
15809 		 * Set the device address to port 0, to have a valid device
15810 		 * address.
15811 		 */
15812 		sata_device.satadev_addr.qual = SATA_ADDR_CNTRL;
15813 		sata_device.satadev_addr.cport = 0;
15814 		sata_device.satadev_addr.pmport = 0;
15815 
15816 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
15817 		    (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) {
15818 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15819 			    "sata_hba_ioctl: reset controller failed"));
15820 			return (EIO);
15821 		}
15822 	}
15823 	/*
15824 	 * Unlock all ports
15825 	 */
15826 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
15827 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15828 		    cport_mutex);
15829 		SATA_CPORT_INFO(sata_hba_inst, tcport)->
15830 		    cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
15831 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15832 		    cport_mutex);
15833 	}
15834 
15835 	/*
15836 	 * This operation returns EFAULT if either reset
15837 	 * controller failed or a re-probing of any port failed.
15838 	 */
15839 	return (rv);
15840 }
15841 
15842 
15843 /*
15844  * Process ioctl port self test request.
15845  *
15846  * NOTE: Port multiplier code is not completed nor tested.
15847  */
15848 static int
15849 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst,
15850     sata_device_t *sata_device)
15851 {
15852 	int cport, pmport, qual;
15853 	int rv = 0;
15854 
15855 	/* Sanity check */
15856 	if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL)
15857 		return (ENOTSUP);
15858 
15859 	cport = sata_device->satadev_addr.cport;
15860 	pmport = sata_device->satadev_addr.pmport;
15861 	qual = sata_device->satadev_addr.qual;
15862 
15863 	/*
15864 	 * There is no protection here for a configured
15865 	 * device attached to this port.
15866 	 */
15867 
15868 	if ((*SATA_SELFTEST_FUNC(sata_hba_inst))
15869 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15870 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15871 		    "sata_hba_ioctl: port selftest: "
15872 		    "failed port %d:%d", cport, pmport));
15873 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15874 		    cport_mutex);
15875 		sata_update_port_info(sata_hba_inst, sata_device);
15876 		if (qual == SATA_ADDR_CPORT)
15877 			SATA_CPORT_STATE(sata_hba_inst, cport) =
15878 			    SATA_PSTATE_FAILED;
15879 		else { /* port multiplier device port */
15880 			mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst,
15881 			    cport, pmport));
15882 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
15883 			    SATA_PSTATE_FAILED;
15884 			mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst,
15885 			    cport, pmport));
15886 		}
15887 
15888 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15889 		    cport_mutex);
15890 		return (EIO);
15891 	}
15892 	/*
15893 	 * Beacuse the port was reset in the course of testing, it should be
15894 	 * re-probed and attached device state should be restored. At this
15895 	 * point the port state is unknown - it's state is HBA-specific.
15896 	 * Force port re-probing to get it into a known state.
15897 	 */
15898 	if (sata_reprobe_port(sata_hba_inst, sata_device,
15899 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
15900 		rv = EIO;
15901 	return (rv);
15902 }
15903 
15904 
15905 /*
15906  * sata_cfgadm_state:
15907  * Use the sata port state and state of the target node to figure out
15908  * the cfgadm_state.
15909  *
15910  * The port argument is a value with encoded cport,
15911  * pmport and address qualifier, in the same manner as a scsi target number.
15912  * SCSI_TO_SATA_CPORT macro extracts cport number,
15913  * SCSI_TO_SATA_PMPORT extracts pmport number and
15914  * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag.
15915  *
15916  * Port multiplier is supported.
15917  */
15918 
15919 static void
15920 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port,
15921     devctl_ap_state_t *ap_state)
15922 {
15923 	uint8_t		cport, pmport, qual;
15924 	uint32_t	port_state, pmult_state;
15925 	uint32_t	dev_type;
15926 	sata_drive_info_t *sdinfo;
15927 
15928 	cport = SCSI_TO_SATA_CPORT(port);
15929 	pmport = SCSI_TO_SATA_PMPORT(port);
15930 	qual = SCSI_TO_SATA_ADDR_QUAL(port);
15931 
15932 	/* Check cport state */
15933 	port_state = SATA_CPORT_STATE(sata_hba_inst, cport);
15934 	if (port_state & SATA_PSTATE_SHUTDOWN ||
15935 	    port_state & SATA_PSTATE_FAILED) {
15936 		ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
15937 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15938 		if (port_state & SATA_PSTATE_FAILED)
15939 			ap_state->ap_condition = AP_COND_FAILED;
15940 		else
15941 			ap_state->ap_condition = AP_COND_UNKNOWN;
15942 
15943 		return;
15944 	}
15945 
15946 	/* cport state is okay. Now check pmport state */
15947 	if (qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) {
15948 		/* Sanity check */
15949 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
15950 		    SATA_DTYPE_PMULT || SATA_PMPORT_INFO(sata_hba_inst,
15951 		    cport, pmport) == NULL)
15952 			return;
15953 		port_state = SATA_PMPORT_STATE(sata_hba_inst, cport, pmport);
15954 		if (port_state & SATA_PSTATE_SHUTDOWN ||
15955 		    port_state & SATA_PSTATE_FAILED) {
15956 			ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
15957 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15958 			if (port_state & SATA_PSTATE_FAILED)
15959 				ap_state->ap_condition = AP_COND_FAILED;
15960 			else
15961 				ap_state->ap_condition = AP_COND_UNKNOWN;
15962 
15963 			return;
15964 		}
15965 	}
15966 
15967 	/* Port is enabled and ready */
15968 	if (qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_CPORT)
15969 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, cport);
15970 	else
15971 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, pmport);
15972 
15973 	switch (dev_type) {
15974 	case SATA_DTYPE_NONE:
15975 	{
15976 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15977 		ap_state->ap_condition = AP_COND_OK;
15978 		/* No device attached */
15979 		ap_state->ap_rstate = AP_RSTATE_EMPTY;
15980 		break;
15981 	}
15982 	case SATA_DTYPE_PMULT:
15983 	{
15984 		/* Need to check port multiplier state */
15985 		ASSERT(qual == SATA_ADDR_DCPORT);
15986 		pmult_state = SATA_PMULT_INFO(sata_hba_inst, cport)->
15987 		    pmult_state;
15988 		if (pmult_state & (SATA_PSTATE_SHUTDOWN|SATA_PSTATE_FAILED)) {
15989 			ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
15990 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15991 			if (pmult_state & SATA_PSTATE_FAILED)
15992 				ap_state->ap_condition = AP_COND_FAILED;
15993 			else
15994 				ap_state->ap_condition = AP_COND_UNKNOWN;
15995 
15996 			return;
15997 		}
15998 
15999 		/* Port multiplier is not configurable */
16000 		ap_state->ap_ostate = AP_OSTATE_CONFIGURED;
16001 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
16002 		ap_state->ap_condition = AP_COND_OK;
16003 		break;
16004 	}
16005 
16006 	case SATA_DTYPE_ATADISK:
16007 	case SATA_DTYPE_ATAPICD:
16008 	case SATA_DTYPE_ATAPITAPE:
16009 	case SATA_DTYPE_ATAPIDISK:
16010 	{
16011 		dev_info_t *tdip = NULL;
16012 		dev_info_t *dip = NULL;
16013 		int circ;
16014 
16015 		dip = SATA_DIP(sata_hba_inst);
16016 		tdip = sata_get_target_dip(dip, cport, pmport);
16017 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
16018 		if (tdip != NULL) {
16019 			ndi_devi_enter(dip, &circ);
16020 			mutex_enter(&(DEVI(tdip)->devi_lock));
16021 			if (DEVI_IS_DEVICE_REMOVED(tdip)) {
16022 				/*
16023 				 * There could be the case where previously
16024 				 * configured and opened device was removed
16025 				 * and unknown device was plugged.
16026 				 * In such case we want to show a device, and
16027 				 * its configured or unconfigured state but
16028 				 * indicate unusable condition untill the
16029 				 * old target node is released and removed.
16030 				 */
16031 				ap_state->ap_condition = AP_COND_UNUSABLE;
16032 			} else {
16033 				mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst,
16034 				    cport));
16035 				sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16036 				    cport);
16037 				if (sdinfo != NULL) {
16038 					if ((sdinfo->satadrv_state &
16039 					    SATA_DSTATE_FAILED) != 0)
16040 						ap_state->ap_condition =
16041 						    AP_COND_FAILED;
16042 					else
16043 						ap_state->ap_condition =
16044 						    AP_COND_OK;
16045 				} else {
16046 					ap_state->ap_condition =
16047 					    AP_COND_UNKNOWN;
16048 				}
16049 				mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst,
16050 				    cport));
16051 			}
16052 			if ((DEVI_IS_DEVICE_OFFLINE(tdip)) ||
16053 			    (DEVI_IS_DEVICE_DOWN(tdip))) {
16054 				ap_state->ap_ostate =
16055 				    AP_OSTATE_UNCONFIGURED;
16056 			} else {
16057 				ap_state->ap_ostate =
16058 				    AP_OSTATE_CONFIGURED;
16059 			}
16060 			mutex_exit(&(DEVI(tdip)->devi_lock));
16061 			ndi_devi_exit(dip, circ);
16062 		} else {
16063 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16064 			ap_state->ap_condition = AP_COND_UNKNOWN;
16065 		}
16066 		break;
16067 	}
16068 	case SATA_DTYPE_ATAPIPROC:
16069 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
16070 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16071 		ap_state->ap_condition = AP_COND_OK;
16072 		break;
16073 	default:
16074 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
16075 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16076 		ap_state->ap_condition = AP_COND_UNKNOWN;
16077 		/*
16078 		 * This is actually internal error condition (non fatal),
16079 		 * because we have already checked all defined device types.
16080 		 */
16081 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16082 		    "sata_cfgadm_state: Internal error: "
16083 		    "unknown device type"));
16084 		break;
16085 	}
16086 }
16087 
16088 
16089 /*
16090  * Process ioctl get device path request.
16091  *
16092  * NOTE: Port multiplier has no target dip. Devices connected to port
16093  * multiplier have target node attached to the HBA node. The only difference
16094  * between them and the directly-attached device node is a target address.
16095  */
16096 static int
16097 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst,
16098     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16099 {
16100 	char path[MAXPATHLEN];
16101 	uint32_t size;
16102 	dev_info_t *tdip;
16103 
16104 	(void) strcpy(path, "/devices");
16105 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
16106 	    &sata_device->satadev_addr)) == NULL) {
16107 		/*
16108 		 * No such device. If this is a request for a size, do not
16109 		 * return EINVAL for non-existing target, because cfgadm
16110 		 * will then indicate a meaningless ioctl failure.
16111 		 * If this is a request for a path, indicate invalid
16112 		 * argument.
16113 		 */
16114 		if (ioc->get_size == 0)
16115 			return (EINVAL);
16116 	} else {
16117 		(void) ddi_pathname(tdip, path + strlen(path));
16118 	}
16119 	size = strlen(path) + 1;
16120 
16121 	if (ioc->get_size != 0) {
16122 		if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz,
16123 		    mode) != 0)
16124 			return (EFAULT);
16125 	} else {
16126 		if (ioc->bufsiz != size)
16127 			return (EINVAL);
16128 
16129 		else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz,
16130 		    mode) != 0)
16131 			return (EFAULT);
16132 	}
16133 	return (0);
16134 }
16135 
16136 /*
16137  * Process ioctl get attachment point type request.
16138  *
16139  * NOTE: Port multiplier is supported.
16140  */
16141 static	int
16142 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst,
16143     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16144 {
16145 	uint32_t	type_len;
16146 	const char	*ap_type;
16147 	int		dev_type;
16148 
16149 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16150 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst,
16151 		    sata_device->satadev_addr.cport);
16152 	else /* pmport */
16153 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst,
16154 		    sata_device->satadev_addr.cport,
16155 		    sata_device->satadev_addr.pmport);
16156 
16157 	switch (dev_type) {
16158 	case SATA_DTYPE_NONE:
16159 		ap_type = "port";
16160 		break;
16161 
16162 	case SATA_DTYPE_ATADISK:
16163 	case SATA_DTYPE_ATAPIDISK:
16164 		ap_type = "disk";
16165 		break;
16166 
16167 	case SATA_DTYPE_ATAPICD:
16168 		ap_type = "cd/dvd";
16169 		break;
16170 
16171 	case SATA_DTYPE_ATAPITAPE:
16172 		ap_type = "tape";
16173 		break;
16174 
16175 	case SATA_DTYPE_ATAPIPROC:
16176 		ap_type = "processor";
16177 		break;
16178 
16179 	case SATA_DTYPE_PMULT:
16180 		ap_type = "sata-pmult";
16181 		break;
16182 
16183 	case SATA_DTYPE_UNKNOWN:
16184 		ap_type = "unknown";
16185 		break;
16186 
16187 	default:
16188 		ap_type = "unsupported";
16189 		break;
16190 
16191 	} /* end of dev_type switch */
16192 
16193 	type_len = strlen(ap_type) + 1;
16194 
16195 	if (ioc->get_size) {
16196 		if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz,
16197 		    mode) != 0)
16198 			return (EFAULT);
16199 	} else {
16200 		if (ioc->bufsiz != type_len)
16201 			return (EINVAL);
16202 
16203 		if (ddi_copyout((void *)ap_type, ioc->buf,
16204 		    ioc->bufsiz, mode) != 0)
16205 			return (EFAULT);
16206 	}
16207 	return (0);
16208 
16209 }
16210 
16211 /*
16212  * Process ioctl get device model info request.
16213  * This operation should return to cfgadm the device model
16214  * information string
16215  *
16216  * NOTE: Port multiplier is supported.
16217  */
16218 static	int
16219 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst,
16220     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16221 {
16222 	sata_drive_info_t *sdinfo;
16223 	uint32_t info_len;
16224 	char ap_info[SATA_ID_MODEL_LEN + 1];
16225 
16226 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16227 	    sata_device->satadev_addr.cport)->cport_mutex);
16228 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16229 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16230 		    sata_device->satadev_addr.cport);
16231 	else /* port multiplier */
16232 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16233 		    sata_device->satadev_addr.cport,
16234 		    sata_device->satadev_addr.pmport);
16235 	if (sdinfo == NULL) {
16236 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16237 		    sata_device->satadev_addr.cport)->cport_mutex);
16238 		return (EINVAL);
16239 	}
16240 
16241 #ifdef	_LITTLE_ENDIAN
16242 	swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
16243 #else	/* _LITTLE_ENDIAN */
16244 	bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
16245 #endif	/* _LITTLE_ENDIAN */
16246 
16247 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16248 	    sata_device->satadev_addr.cport)->cport_mutex);
16249 
16250 	ap_info[SATA_ID_MODEL_LEN] = '\0';
16251 
16252 	info_len = strlen(ap_info) + 1;
16253 
16254 	if (ioc->get_size) {
16255 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16256 		    mode) != 0)
16257 			return (EFAULT);
16258 	} else {
16259 		if (ioc->bufsiz < info_len)
16260 			return (EINVAL);
16261 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16262 		    mode) != 0)
16263 			return (EFAULT);
16264 	}
16265 	return (0);
16266 }
16267 
16268 
16269 /*
16270  * Process ioctl get device firmware revision info request.
16271  * This operation should return to cfgadm the device firmware revision
16272  * information string
16273  *
16274  * Port multiplier is supported.
16275  */
16276 static	int
16277 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst,
16278     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16279 {
16280 	sata_drive_info_t *sdinfo;
16281 	uint32_t info_len;
16282 	char ap_info[SATA_ID_FW_LEN + 1];
16283 
16284 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16285 	    sata_device->satadev_addr.cport)->cport_mutex);
16286 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16287 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16288 		    sata_device->satadev_addr.cport);
16289 	else /* port multiplier */
16290 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16291 		    sata_device->satadev_addr.cport,
16292 		    sata_device->satadev_addr.pmport);
16293 	if (sdinfo == NULL) {
16294 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16295 		    sata_device->satadev_addr.cport)->cport_mutex);
16296 		return (EINVAL);
16297 	}
16298 
16299 #ifdef	_LITTLE_ENDIAN
16300 	swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
16301 #else	/* _LITTLE_ENDIAN */
16302 	bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
16303 #endif	/* _LITTLE_ENDIAN */
16304 
16305 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16306 	    sata_device->satadev_addr.cport)->cport_mutex);
16307 
16308 	ap_info[SATA_ID_FW_LEN] = '\0';
16309 
16310 	info_len = strlen(ap_info) + 1;
16311 
16312 	if (ioc->get_size) {
16313 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16314 		    mode) != 0)
16315 			return (EFAULT);
16316 	} else {
16317 		if (ioc->bufsiz < info_len)
16318 			return (EINVAL);
16319 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16320 		    mode) != 0)
16321 			return (EFAULT);
16322 	}
16323 	return (0);
16324 }
16325 
16326 
16327 /*
16328  * Process ioctl get device serial number info request.
16329  * This operation should return to cfgadm the device serial number string.
16330  *
16331  * NOTE: Port multiplier is supported.
16332  */
16333 static	int
16334 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst,
16335     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16336 {
16337 	sata_drive_info_t *sdinfo;
16338 	uint32_t info_len;
16339 	char ap_info[SATA_ID_SERIAL_LEN + 1];
16340 
16341 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16342 	    sata_device->satadev_addr.cport)->cport_mutex);
16343 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16344 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16345 		    sata_device->satadev_addr.cport);
16346 	else /* port multiplier */
16347 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16348 		    sata_device->satadev_addr.cport,
16349 		    sata_device->satadev_addr.pmport);
16350 	if (sdinfo == NULL) {
16351 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16352 		    sata_device->satadev_addr.cport)->cport_mutex);
16353 		return (EINVAL);
16354 	}
16355 
16356 #ifdef	_LITTLE_ENDIAN
16357 	swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
16358 #else	/* _LITTLE_ENDIAN */
16359 	bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
16360 #endif	/* _LITTLE_ENDIAN */
16361 
16362 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16363 	    sata_device->satadev_addr.cport)->cport_mutex);
16364 
16365 	ap_info[SATA_ID_SERIAL_LEN] = '\0';
16366 
16367 	info_len = strlen(ap_info) + 1;
16368 
16369 	if (ioc->get_size) {
16370 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16371 		    mode) != 0)
16372 			return (EFAULT);
16373 	} else {
16374 		if (ioc->bufsiz < info_len)
16375 			return (EINVAL);
16376 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16377 		    mode) != 0)
16378 			return (EFAULT);
16379 	}
16380 	return (0);
16381 }
16382 
16383 
16384 /*
16385  * Preset scsi extended sense data (to NO SENSE)
16386  * First 18 bytes of the sense data are preset to current valid sense
16387  * with a key NO SENSE data.
16388  *
16389  * Returns void
16390  */
16391 static void
16392 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense)
16393 {
16394 	sense->es_valid = 1;		/* Valid sense */
16395 	sense->es_class = CLASS_EXTENDED_SENSE;	/* 0x70 - current err */
16396 	sense->es_key = KEY_NO_SENSE;
16397 	sense->es_info_1 = 0;
16398 	sense->es_info_2 = 0;
16399 	sense->es_info_3 = 0;
16400 	sense->es_info_4 = 0;
16401 	sense->es_add_len = 10;	/* Additional length - replace with a def */
16402 	sense->es_cmd_info[0] = 0;
16403 	sense->es_cmd_info[1] = 0;
16404 	sense->es_cmd_info[2] = 0;
16405 	sense->es_cmd_info[3] = 0;
16406 	sense->es_add_code = 0;
16407 	sense->es_qual_code = 0;
16408 }
16409 
16410 /*
16411  * Register a legacy cmdk-style devid for the target (disk) device.
16412  *
16413  * Note: This function is called only when the HBA devinfo node has the
16414  * property "use-cmdk-devid-format" set. This property indicates that
16415  * devid compatible with old cmdk (target) driver is to be generated
16416  * for any target device attached to this controller. This will take
16417  * precedence over the devid generated by sd (target) driver.
16418  * This function is derived from cmdk_devid_setup() function in cmdk.c.
16419  */
16420 static void
16421 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo)
16422 {
16423 	char	*hwid;
16424 	int	modlen;
16425 	int	serlen;
16426 	int	rval;
16427 	ddi_devid_t	devid;
16428 
16429 	/*
16430 	 * device ID is a concatanation of model number, "=", serial number.
16431 	 */
16432 	hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP);
16433 	bcopy(&sdinfo->satadrv_id.ai_model, hwid,
16434 	    sizeof (sdinfo->satadrv_id.ai_model));
16435 	swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model));
16436 	modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model));
16437 	if (modlen == 0)
16438 		goto err;
16439 	hwid[modlen++] = '=';
16440 	bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen],
16441 	    sizeof (sdinfo->satadrv_id.ai_drvser));
16442 	swab(&hwid[modlen], &hwid[modlen],
16443 	    sizeof (sdinfo->satadrv_id.ai_drvser));
16444 	serlen = sata_check_modser(&hwid[modlen],
16445 	    sizeof (sdinfo->satadrv_id.ai_drvser));
16446 	if (serlen == 0)
16447 		goto err;
16448 	hwid[modlen + serlen] = 0; /* terminate the hwid string */
16449 
16450 	/* initialize/register devid */
16451 	if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL,
16452 	    (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS) {
16453 		rval = ddi_devid_register(dip, devid);
16454 		/*
16455 		 * Free up the allocated devid buffer.
16456 		 * NOTE: This doesn't mean unregistering devid.
16457 		 */
16458 		ddi_devid_free(devid);
16459 	}
16460 
16461 	if (rval != DDI_SUCCESS)
16462 		cmn_err(CE_WARN, "sata: failed to create devid for the disk"
16463 		    " on port %d", sdinfo->satadrv_addr.cport);
16464 err:
16465 	kmem_free(hwid, LEGACY_HWID_LEN);
16466 }
16467 
16468 /*
16469  * valid model/serial string must contain a non-zero non-space characters.
16470  * trim trailing spaces/NULLs.
16471  */
16472 static int
16473 sata_check_modser(char *buf, int buf_len)
16474 {
16475 	boolean_t ret;
16476 	char *s;
16477 	int i;
16478 	int tb;
16479 	char ch;
16480 
16481 	ret = B_FALSE;
16482 	s = buf;
16483 	for (i = 0; i < buf_len; i++) {
16484 		ch = *s++;
16485 		if (ch != ' ' && ch != '\0')
16486 			tb = i + 1;
16487 		if (ch != ' ' && ch != '\0' && ch != '0')
16488 			ret = B_TRUE;
16489 	}
16490 
16491 	if (ret == B_FALSE)
16492 		return (0); /* invalid string */
16493 
16494 	return (tb); /* return length */
16495 }
16496 
16497 /*
16498  * sata_set_drive_features function compares current device features setting
16499  * with the saved device features settings and, if there is a difference,
16500  * it restores device features setting to the previously saved state.
16501  * It also arbitrarily tries to select the highest supported DMA mode.
16502  * Device Identify or Identify Packet Device data has to be current.
16503  * At the moment read ahead and write cache are considered for all devices.
16504  * For atapi devices, Removable Media Status Notification is set in addition
16505  * to common features.
16506  *
16507  * This function cannot be called in the interrupt context (it may sleep).
16508  *
16509  * The input argument sdinfo should point to the drive info structure
16510  * to be updated after features are set. Note, that only
16511  * device (packet) identify data is updated, not the flags indicating the
16512  * supported features.
16513  *
16514  * Returns SATA_SUCCESS if successful or there was nothing to do.
16515  * Device Identify data in the drive info structure pointed to by the sdinfo
16516  * arguments is updated even when no features were set or changed.
16517  *
16518  * Returns SATA_FAILURE if device features could not be set or DMA mode
16519  * for a disk cannot be set and device identify data cannot be fetched.
16520  *
16521  * Returns SATA_RETRY if device features could not be set (other than disk
16522  * DMA mode) but the device identify data was fetched successfully.
16523  *
16524  * Note: This function may fail the port, making it inaccessible.
16525  * In such case the explicit port disconnect/connect or physical device
16526  * detach/attach is required to re-evaluate port state again.
16527  */
16528 
16529 static int
16530 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst,
16531     sata_drive_info_t *sdinfo, int restore)
16532 {
16533 	int rval = SATA_SUCCESS;
16534 	int rval_set;
16535 	sata_drive_info_t new_sdinfo;
16536 	char *finfo = "sata_set_drive_features: cannot";
16537 	char *finfox;
16538 	int cache_op;
16539 
16540 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
16541 	new_sdinfo.satadrv_addr = sdinfo->satadrv_addr;
16542 	new_sdinfo.satadrv_type = sdinfo->satadrv_type;
16543 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
16544 		/*
16545 		 * Cannot get device identification - caller may retry later
16546 		 */
16547 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16548 		    "%s fetch device identify data\n", finfo);
16549 		return (SATA_FAILURE);
16550 	}
16551 	finfox = (restore != 0) ? " restore device features" :
16552 	    " initialize device features\n";
16553 
16554 	switch (sdinfo->satadrv_type) {
16555 	case SATA_DTYPE_ATADISK:
16556 		/* Arbitrarily set UDMA mode */
16557 		if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
16558 		    SATA_SUCCESS) {
16559 			SATA_LOG_D((sata_hba_inst, CE_WARN,
16560 			    "%s set UDMA mode\n", finfo));
16561 			return (SATA_FAILURE);
16562 		}
16563 		break;
16564 	case SATA_DTYPE_ATAPICD:
16565 	case SATA_DTYPE_ATAPITAPE:
16566 	case SATA_DTYPE_ATAPIDISK:
16567 		/*  Set Removable Media Status Notification, if necessary */
16568 		if (SATA_RM_NOTIFIC_SUPPORTED(new_sdinfo.satadrv_id) &&
16569 		    restore != 0) {
16570 			if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) &&
16571 			    (!SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id)))||
16572 			    ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) &&
16573 			    SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id))) {
16574 				/* Current setting does not match saved one */
16575 				if (sata_set_rmsn(sata_hba_inst, sdinfo,
16576 				    sdinfo->satadrv_settings &
16577 				    SATA_DEV_RMSN) != SATA_SUCCESS)
16578 					rval = SATA_FAILURE;
16579 			}
16580 		}
16581 		/*
16582 		 * We have to set Multiword DMA or UDMA, if it is supported, as
16583 		 * we want to use DMA transfer mode whenever possible.
16584 		 * Some devices require explicit setting of the DMA mode.
16585 		 */
16586 		if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) {
16587 			/* Set highest supported DMA mode */
16588 			if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
16589 			    SATA_SUCCESS) {
16590 				SATA_LOG_D((sata_hba_inst, CE_WARN,
16591 				    "%s set UDMA mode\n", finfo));
16592 				rval = SATA_FAILURE;
16593 			}
16594 		}
16595 		break;
16596 	}
16597 
16598 	if (!SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id) &&
16599 	    !SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
16600 		/*
16601 		 * neither READ AHEAD nor WRITE CACHE is supported
16602 		 * - do nothing
16603 		 */
16604 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16605 		    "settable features not supported\n", NULL);
16606 		goto update_sdinfo;
16607 	}
16608 
16609 	if ((SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id) &&
16610 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) &&
16611 	    (SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id) &&
16612 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
16613 		/*
16614 		 * both READ AHEAD and WRITE CACHE are enabled
16615 		 * - Nothing to do
16616 		 */
16617 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16618 		    "no device features to set\n", NULL);
16619 		goto update_sdinfo;
16620 	}
16621 
16622 	cache_op = 0;
16623 
16624 	if (SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id)) {
16625 		if ((sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
16626 		    !SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
16627 			/* Enable read ahead / read cache */
16628 			cache_op = SATAC_SF_ENABLE_READ_AHEAD;
16629 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16630 			    "enabling read cache\n", NULL);
16631 		} else if (!(sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
16632 		    SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
16633 			/* Disable read ahead  / read cache */
16634 			cache_op = SATAC_SF_DISABLE_READ_AHEAD;
16635 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16636 			    "disabling read cache\n", NULL);
16637 		}
16638 
16639 		if (cache_op != 0) {
16640 			/* Try to set read cache mode */
16641 			rval_set = sata_set_cache_mode(sata_hba_inst,
16642 			    &new_sdinfo, cache_op);
16643 			if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
16644 				rval = rval_set;
16645 		}
16646 	}
16647 
16648 	cache_op = 0;
16649 
16650 	if (SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
16651 		if ((sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
16652 		    !SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
16653 			/* Enable write cache */
16654 			cache_op = SATAC_SF_ENABLE_WRITE_CACHE;
16655 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16656 			    "enabling write cache\n", NULL);
16657 		} else if (!(sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
16658 		    SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
16659 			/* Disable write cache */
16660 			cache_op = SATAC_SF_DISABLE_WRITE_CACHE;
16661 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16662 			    "disabling write cache\n", NULL);
16663 		}
16664 
16665 		if (cache_op != 0) {
16666 			/* Try to set write cache mode */
16667 			rval_set = sata_set_cache_mode(sata_hba_inst,
16668 			    &new_sdinfo, cache_op);
16669 			if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
16670 				rval = rval_set;
16671 		}
16672 	}
16673 	if (rval != SATA_SUCCESS)
16674 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16675 		    "%s %s", finfo, finfox));
16676 
16677 update_sdinfo:
16678 	/*
16679 	 * We need to fetch Device Identify data again
16680 	 */
16681 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
16682 		/*
16683 		 * Cannot get device identification - retry later
16684 		 */
16685 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16686 		    "%s re-fetch device identify data\n", finfo));
16687 		rval = SATA_FAILURE;
16688 	}
16689 	/* Copy device sata info. */
16690 	sdinfo->satadrv_id = new_sdinfo.satadrv_id;
16691 
16692 	return (rval);
16693 }
16694 
16695 
16696 /*
16697  *
16698  * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if
16699  * unable to determine.
16700  *
16701  * Cannot be called in an interrupt context.
16702  *
16703  * Called by sata_build_lsense_page_2f()
16704  */
16705 
16706 static int
16707 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst,
16708     sata_drive_info_t *sdinfo)
16709 {
16710 	sata_pkt_t *spkt;
16711 	sata_cmd_t *scmd;
16712 	sata_pkt_txlate_t *spx;
16713 	int rval;
16714 
16715 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16716 	spx->txlt_sata_hba_inst = sata_hba_inst;
16717 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
16718 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16719 	if (spkt == NULL) {
16720 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16721 		return (-1);
16722 	}
16723 	/* address is needed now */
16724 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16725 
16726 
16727 	/* Fill sata_pkt */
16728 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16729 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16730 	/* Synchronous mode, no callback */
16731 	spkt->satapkt_comp = NULL;
16732 	/* Timeout 30s */
16733 	spkt->satapkt_time = sata_default_pkt_time;
16734 
16735 	scmd = &spkt->satapkt_cmd;
16736 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
16737 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
16738 
16739 	/* Set up which registers need to be returned */
16740 	scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE;
16741 	scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE;
16742 
16743 	/* Build SMART_RETURN_STATUS cmd in the sata_pkt */
16744 	scmd->satacmd_addr_type = 0;		/* N/A */
16745 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
16746 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
16747 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
16748 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
16749 	scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS;
16750 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
16751 	scmd->satacmd_cmd_reg = SATAC_SMART;
16752 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16753 	    sdinfo->satadrv_addr.cport)));
16754 
16755 
16756 	/* Send pkt to SATA HBA driver */
16757 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16758 	    SATA_TRAN_ACCEPTED ||
16759 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16760 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16761 		    sdinfo->satadrv_addr.cport)));
16762 		/*
16763 		 * Whoops, no SMART RETURN STATUS
16764 		 */
16765 		rval = -1;
16766 	} else {
16767 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16768 		    sdinfo->satadrv_addr.cport)));
16769 		if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
16770 			rval = -1;
16771 			goto fail;
16772 		}
16773 		if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
16774 			rval = -1;
16775 			goto fail;
16776 		}
16777 		if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) &&
16778 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2))
16779 			rval = 0;
16780 		else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) &&
16781 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4))
16782 			rval = 1;
16783 		else {
16784 			rval = -1;
16785 			goto fail;
16786 		}
16787 	}
16788 fail:
16789 	/* Free allocated resources */
16790 	sata_pkt_free(spx);
16791 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
16792 
16793 	return (rval);
16794 }
16795 
16796 /*
16797  *
16798  * Returns 0 if succeeded, -1 otherwise
16799  *
16800  * Cannot be called in an interrupt context.
16801  *
16802  */
16803 static int
16804 sata_fetch_smart_data(
16805 	sata_hba_inst_t *sata_hba_inst,
16806 	sata_drive_info_t *sdinfo,
16807 	struct smart_data *smart_data)
16808 {
16809 	sata_pkt_t *spkt;
16810 	sata_cmd_t *scmd;
16811 	sata_pkt_txlate_t *spx;
16812 	int rval;
16813 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
16814 
16815 #if ! defined(lint)
16816 	ASSERT(sizeof (struct smart_data) == 512);
16817 #endif
16818 
16819 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16820 	spx->txlt_sata_hba_inst = sata_hba_inst;
16821 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
16822 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16823 	if (spkt == NULL) {
16824 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16825 		return (-1);
16826 	}
16827 	/* address is needed now */
16828 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16829 
16830 
16831 	/* Fill sata_pkt */
16832 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16833 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16834 	/* Synchronous mode, no callback */
16835 	spkt->satapkt_comp = NULL;
16836 	/* Timeout 30s */
16837 	spkt->satapkt_time = sata_default_pkt_time;
16838 
16839 	scmd = &spkt->satapkt_cmd;
16840 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
16841 
16842 	/*
16843 	 * Allocate buffer for SMART data
16844 	 */
16845 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
16846 	    sizeof (struct smart_data));
16847 	if (scmd->satacmd_bp == NULL) {
16848 		sata_pkt_free(spx);
16849 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16850 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16851 		    "sata_fetch_smart_data: "
16852 		    "cannot allocate buffer"));
16853 		return (-1);
16854 	}
16855 
16856 
16857 	/* Build SMART_READ_DATA cmd in the sata_pkt */
16858 	scmd->satacmd_addr_type = 0;		/* N/A */
16859 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
16860 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
16861 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
16862 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
16863 	scmd->satacmd_features_reg = SATA_SMART_READ_DATA;
16864 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
16865 	scmd->satacmd_cmd_reg = SATAC_SMART;
16866 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16867 	    sdinfo->satadrv_addr.cport)));
16868 
16869 	/* Send pkt to SATA HBA driver */
16870 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16871 	    SATA_TRAN_ACCEPTED ||
16872 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16873 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16874 		    sdinfo->satadrv_addr.cport)));
16875 		/*
16876 		 * Whoops, no SMART DATA available
16877 		 */
16878 		rval = -1;
16879 		goto fail;
16880 	} else {
16881 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16882 		    sdinfo->satadrv_addr.cport)));
16883 		if (spx->txlt_buf_dma_handle != NULL) {
16884 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
16885 			    DDI_DMA_SYNC_FORKERNEL);
16886 			ASSERT(rval == DDI_SUCCESS);
16887 			if (sata_check_for_dma_error(dip, spx)) {
16888 				ddi_fm_service_impact(dip,
16889 				    DDI_SERVICE_UNAFFECTED);
16890 				rval = -1;
16891 				goto fail;
16892 			}
16893 		}
16894 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data,
16895 		    sizeof (struct smart_data));
16896 	}
16897 
16898 fail:
16899 	/* Free allocated resources */
16900 	sata_free_local_buffer(spx);
16901 	sata_pkt_free(spx);
16902 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
16903 
16904 	return (rval);
16905 }
16906 
16907 /*
16908  * Used by LOG SENSE page 0x10
16909  * Reads (in synchronous mode) the self test log data using Read Log Ext cmd.
16910  * Note: cannot be called in the interrupt context.
16911  *
16912  * return 0 for success, -1 otherwise
16913  *
16914  */
16915 static int
16916 sata_ext_smart_selftest_read_log(
16917 	sata_hba_inst_t *sata_hba_inst,
16918 	sata_drive_info_t *sdinfo,
16919 	struct smart_ext_selftest_log *ext_selftest_log,
16920 	uint16_t block_num)
16921 {
16922 	sata_pkt_txlate_t *spx;
16923 	sata_pkt_t *spkt;
16924 	sata_cmd_t *scmd;
16925 	int rval;
16926 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
16927 
16928 #if ! defined(lint)
16929 	ASSERT(sizeof (struct smart_ext_selftest_log) == 512);
16930 #endif
16931 
16932 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16933 	spx->txlt_sata_hba_inst = sata_hba_inst;
16934 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
16935 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16936 	if (spkt == NULL) {
16937 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16938 		return (-1);
16939 	}
16940 	/* address is needed now */
16941 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16942 
16943 
16944 	/* Fill sata_pkt */
16945 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16946 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16947 	/* Synchronous mode, no callback */
16948 	spkt->satapkt_comp = NULL;
16949 	/* Timeout 30s */
16950 	spkt->satapkt_time = sata_default_pkt_time;
16951 
16952 	scmd = &spkt->satapkt_cmd;
16953 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
16954 
16955 	/*
16956 	 * Allocate buffer for SMART extended self-test log
16957 	 */
16958 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
16959 	    sizeof (struct smart_ext_selftest_log));
16960 	if (scmd->satacmd_bp == NULL) {
16961 		sata_pkt_free(spx);
16962 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16963 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16964 		    "sata_ext_smart_selftest_log: "
16965 		    "cannot allocate buffer"));
16966 		return (-1);
16967 	}
16968 
16969 	/* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */
16970 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
16971 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of selftest log */
16972 	scmd->satacmd_sec_count_msb = 0;	/* One sector of selftest log */
16973 	scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE;
16974 	scmd->satacmd_lba_low_msb = 0;
16975 	scmd->satacmd_lba_mid_lsb = block_num & 0xff;
16976 	scmd->satacmd_lba_mid_msb = block_num >> 8;
16977 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
16978 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
16979 
16980 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16981 	    sdinfo->satadrv_addr.cport)));
16982 
16983 	/* Send pkt to SATA HBA driver */
16984 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16985 	    SATA_TRAN_ACCEPTED ||
16986 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16987 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16988 		    sdinfo->satadrv_addr.cport)));
16989 
16990 		/*
16991 		 * Whoops, no SMART selftest log info available
16992 		 */
16993 		rval = -1;
16994 		goto fail;
16995 	} else {
16996 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16997 		    sdinfo->satadrv_addr.cport)));
16998 
16999 		if (spx->txlt_buf_dma_handle != NULL) {
17000 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17001 			    DDI_DMA_SYNC_FORKERNEL);
17002 			ASSERT(rval == DDI_SUCCESS);
17003 			if (sata_check_for_dma_error(dip, spx)) {
17004 				ddi_fm_service_impact(dip,
17005 				    DDI_SERVICE_UNAFFECTED);
17006 				rval = -1;
17007 				goto fail;
17008 			}
17009 		}
17010 		bcopy(scmd->satacmd_bp->b_un.b_addr,
17011 		    (uint8_t *)ext_selftest_log,
17012 		    sizeof (struct smart_ext_selftest_log));
17013 		rval = 0;
17014 	}
17015 
17016 fail:
17017 	/* Free allocated resources */
17018 	sata_free_local_buffer(spx);
17019 	sata_pkt_free(spx);
17020 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17021 
17022 	return (rval);
17023 }
17024 
17025 /*
17026  * Returns 0 for success, -1 otherwise
17027  *
17028  * SMART self-test log data is returned in buffer pointed to by selftest_log
17029  */
17030 static int
17031 sata_smart_selftest_log(
17032 	sata_hba_inst_t *sata_hba_inst,
17033 	sata_drive_info_t *sdinfo,
17034 	struct smart_selftest_log *selftest_log)
17035 {
17036 	sata_pkt_t *spkt;
17037 	sata_cmd_t *scmd;
17038 	sata_pkt_txlate_t *spx;
17039 	int rval;
17040 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
17041 
17042 #if ! defined(lint)
17043 	ASSERT(sizeof (struct smart_selftest_log) == 512);
17044 #endif
17045 
17046 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17047 	spx->txlt_sata_hba_inst = sata_hba_inst;
17048 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
17049 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17050 	if (spkt == NULL) {
17051 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17052 		return (-1);
17053 	}
17054 	/* address is needed now */
17055 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17056 
17057 
17058 	/* Fill sata_pkt */
17059 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17060 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17061 	/* Synchronous mode, no callback */
17062 	spkt->satapkt_comp = NULL;
17063 	/* Timeout 30s */
17064 	spkt->satapkt_time = sata_default_pkt_time;
17065 
17066 	scmd = &spkt->satapkt_cmd;
17067 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17068 
17069 	/*
17070 	 * Allocate buffer for SMART SELFTEST LOG
17071 	 */
17072 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
17073 	    sizeof (struct smart_selftest_log));
17074 	if (scmd->satacmd_bp == NULL) {
17075 		sata_pkt_free(spx);
17076 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17077 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17078 		    "sata_smart_selftest_log: "
17079 		    "cannot allocate buffer"));
17080 		return (-1);
17081 	}
17082 
17083 	/* Build SMART_READ_LOG cmd in the sata_pkt */
17084 	scmd->satacmd_addr_type = 0;		/* N/A */
17085 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of SMART log */
17086 	scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE;
17087 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
17088 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
17089 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
17090 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
17091 	scmd->satacmd_cmd_reg = SATAC_SMART;
17092 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17093 	    sdinfo->satadrv_addr.cport)));
17094 
17095 	/* Send pkt to SATA HBA driver */
17096 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17097 	    SATA_TRAN_ACCEPTED ||
17098 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17099 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17100 		    sdinfo->satadrv_addr.cport)));
17101 		/*
17102 		 * Whoops, no SMART DATA available
17103 		 */
17104 		rval = -1;
17105 		goto fail;
17106 	} else {
17107 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17108 		    sdinfo->satadrv_addr.cport)));
17109 		if (spx->txlt_buf_dma_handle != NULL) {
17110 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17111 			    DDI_DMA_SYNC_FORKERNEL);
17112 			ASSERT(rval == DDI_SUCCESS);
17113 			if (sata_check_for_dma_error(dip, spx)) {
17114 				ddi_fm_service_impact(dip,
17115 				    DDI_SERVICE_UNAFFECTED);
17116 				rval = -1;
17117 				goto fail;
17118 			}
17119 		}
17120 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log,
17121 		    sizeof (struct smart_selftest_log));
17122 		rval = 0;
17123 	}
17124 
17125 fail:
17126 	/* Free allocated resources */
17127 	sata_free_local_buffer(spx);
17128 	sata_pkt_free(spx);
17129 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17130 
17131 	return (rval);
17132 }
17133 
17134 
17135 /*
17136  * Returns 0 for success, -1 otherwise
17137  *
17138  * SMART READ LOG data is returned in buffer pointed to by smart_log
17139  */
17140 static int
17141 sata_smart_read_log(
17142 	sata_hba_inst_t *sata_hba_inst,
17143 	sata_drive_info_t *sdinfo,
17144 	uint8_t *smart_log,		/* where the data should be returned */
17145 	uint8_t which_log,		/* which log should be returned */
17146 	uint8_t log_size)		/* # of 512 bytes in log */
17147 {
17148 	sata_pkt_t *spkt;
17149 	sata_cmd_t *scmd;
17150 	sata_pkt_txlate_t *spx;
17151 	int rval;
17152 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
17153 
17154 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17155 	spx->txlt_sata_hba_inst = sata_hba_inst;
17156 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
17157 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17158 	if (spkt == NULL) {
17159 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17160 		return (-1);
17161 	}
17162 	/* address is needed now */
17163 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17164 
17165 
17166 	/* Fill sata_pkt */
17167 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17168 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17169 	/* Synchronous mode, no callback */
17170 	spkt->satapkt_comp = NULL;
17171 	/* Timeout 30s */
17172 	spkt->satapkt_time = sata_default_pkt_time;
17173 
17174 	scmd = &spkt->satapkt_cmd;
17175 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17176 
17177 	/*
17178 	 * Allocate buffer for SMART READ LOG
17179 	 */
17180 	scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512);
17181 	if (scmd->satacmd_bp == NULL) {
17182 		sata_pkt_free(spx);
17183 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17184 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17185 		    "sata_smart_read_log: " "cannot allocate buffer"));
17186 		return (-1);
17187 	}
17188 
17189 	/* Build SMART_READ_LOG cmd in the sata_pkt */
17190 	scmd->satacmd_addr_type = 0;		/* N/A */
17191 	scmd->satacmd_sec_count_lsb = log_size;	/* what the caller asked for */
17192 	scmd->satacmd_lba_low_lsb = which_log;	/* which log page */
17193 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
17194 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
17195 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
17196 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
17197 	scmd->satacmd_cmd_reg = SATAC_SMART;
17198 
17199 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17200 	    sdinfo->satadrv_addr.cport)));
17201 
17202 	/* Send pkt to SATA HBA driver */
17203 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17204 	    SATA_TRAN_ACCEPTED ||
17205 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17206 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17207 		    sdinfo->satadrv_addr.cport)));
17208 
17209 		/*
17210 		 * Whoops, no SMART DATA available
17211 		 */
17212 		rval = -1;
17213 		goto fail;
17214 	} else {
17215 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17216 		    sdinfo->satadrv_addr.cport)));
17217 
17218 		if (spx->txlt_buf_dma_handle != NULL) {
17219 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17220 			    DDI_DMA_SYNC_FORKERNEL);
17221 			ASSERT(rval == DDI_SUCCESS);
17222 			if (sata_check_for_dma_error(dip, spx)) {
17223 				ddi_fm_service_impact(dip,
17224 				    DDI_SERVICE_UNAFFECTED);
17225 				rval = -1;
17226 				goto fail;
17227 			}
17228 		}
17229 		bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512);
17230 		rval = 0;
17231 	}
17232 
17233 fail:
17234 	/* Free allocated resources */
17235 	sata_free_local_buffer(spx);
17236 	sata_pkt_free(spx);
17237 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17238 
17239 	return (rval);
17240 }
17241 
17242 /*
17243  * Used by LOG SENSE page 0x10
17244  *
17245  * return 0 for success, -1 otherwise
17246  *
17247  */
17248 static int
17249 sata_read_log_ext_directory(
17250 	sata_hba_inst_t *sata_hba_inst,
17251 	sata_drive_info_t *sdinfo,
17252 	struct read_log_ext_directory *logdir)
17253 {
17254 	sata_pkt_txlate_t *spx;
17255 	sata_pkt_t *spkt;
17256 	sata_cmd_t *scmd;
17257 	int rval;
17258 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
17259 
17260 #if ! defined(lint)
17261 	ASSERT(sizeof (struct read_log_ext_directory) == 512);
17262 #endif
17263 
17264 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17265 	spx->txlt_sata_hba_inst = sata_hba_inst;
17266 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
17267 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17268 	if (spkt == NULL) {
17269 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17270 		return (-1);
17271 	}
17272 
17273 	/* Fill sata_pkt */
17274 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17275 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17276 	/* Synchronous mode, no callback */
17277 	spkt->satapkt_comp = NULL;
17278 	/* Timeout 30s */
17279 	spkt->satapkt_time = sata_default_pkt_time;
17280 
17281 	scmd = &spkt->satapkt_cmd;
17282 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17283 
17284 	/*
17285 	 * Allocate buffer for SMART READ LOG EXTENDED command
17286 	 */
17287 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
17288 	    sizeof (struct read_log_ext_directory));
17289 	if (scmd->satacmd_bp == NULL) {
17290 		sata_pkt_free(spx);
17291 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17292 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17293 		    "sata_read_log_ext_directory: "
17294 		    "cannot allocate buffer"));
17295 		return (-1);
17296 	}
17297 
17298 	/* Build READ LOG EXT w/ log directory cmd in the  sata_pkt */
17299 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
17300 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of directory */
17301 	scmd->satacmd_sec_count_msb = 0;	/* One sector of directory */
17302 	scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY;
17303 	scmd->satacmd_lba_low_msb = 0;
17304 	scmd->satacmd_lba_mid_lsb = 0;
17305 	scmd->satacmd_lba_mid_msb = 0;
17306 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
17307 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
17308 
17309 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17310 	    sdinfo->satadrv_addr.cport)));
17311 
17312 	/* Send pkt to SATA HBA driver */
17313 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17314 	    SATA_TRAN_ACCEPTED ||
17315 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17316 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17317 		    sdinfo->satadrv_addr.cport)));
17318 		/*
17319 		 * Whoops, no SMART selftest log info available
17320 		 */
17321 		rval = -1;
17322 		goto fail;
17323 	} else {
17324 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17325 		    sdinfo->satadrv_addr.cport)));
17326 		if (spx->txlt_buf_dma_handle != NULL) {
17327 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17328 			    DDI_DMA_SYNC_FORKERNEL);
17329 			ASSERT(rval == DDI_SUCCESS);
17330 			if (sata_check_for_dma_error(dip, spx)) {
17331 				ddi_fm_service_impact(dip,
17332 				    DDI_SERVICE_UNAFFECTED);
17333 				rval = -1;
17334 				goto fail;
17335 			}
17336 		}
17337 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir,
17338 		    sizeof (struct read_log_ext_directory));
17339 		rval = 0;
17340 	}
17341 
17342 fail:
17343 	/* Free allocated resources */
17344 	sata_free_local_buffer(spx);
17345 	sata_pkt_free(spx);
17346 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17347 
17348 	return (rval);
17349 }
17350 
17351 /*
17352  * Set up error retrieval sata command for NCQ command error data
17353  * recovery.
17354  *
17355  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
17356  * returns SATA_FAILURE otherwise.
17357  */
17358 static int
17359 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
17360 {
17361 #ifndef __lock_lint
17362 	_NOTE(ARGUNUSED(sdinfo))
17363 #endif
17364 
17365 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
17366 	sata_cmd_t *scmd;
17367 	struct buf *bp;
17368 
17369 	/* Operation modes are up to the caller */
17370 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17371 
17372 	/* Synchronous mode, no callback - may be changed by the caller */
17373 	spkt->satapkt_comp = NULL;
17374 	spkt->satapkt_time = sata_default_pkt_time;
17375 
17376 	scmd = &spkt->satapkt_cmd;
17377 	bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t));
17378 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
17379 
17380 	/*
17381 	 * Allocate dma_able buffer error data.
17382 	 * Buffer allocation will take care of buffer alignment and other DMA
17383 	 * attributes.
17384 	 */
17385 	bp = sata_alloc_local_buffer(spx,
17386 	    sizeof (struct sata_ncq_error_recovery_page));
17387 	if (bp == NULL)
17388 		return (SATA_FAILURE);
17389 
17390 	bp_mapin(bp); /* make data buffer accessible */
17391 	scmd->satacmd_bp = bp;
17392 
17393 	/*
17394 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
17395 	 * before accessing it. Handle is in usual place in translate struct.
17396 	 */
17397 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
17398 
17399 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
17400 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
17401 
17402 	return (SATA_SUCCESS);
17403 }
17404 
17405 /*
17406  * sata_xlate_errors() is used to translate (S)ATA error
17407  * information to SCSI information returned in the SCSI
17408  * packet.
17409  */
17410 static void
17411 sata_xlate_errors(sata_pkt_txlate_t *spx)
17412 {
17413 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
17414 	struct scsi_extended_sense *sense;
17415 
17416 	scsipkt->pkt_reason = CMD_INCOMPLETE;
17417 	*scsipkt->pkt_scbp = STATUS_CHECK;
17418 	sense = sata_arq_sense(spx);
17419 
17420 	switch (spx->txlt_sata_pkt->satapkt_reason) {
17421 	case SATA_PKT_PORT_ERROR:
17422 		/*
17423 		 * We have no device data. Assume no data transfered.
17424 		 */
17425 		sense->es_key = KEY_HARDWARE_ERROR;
17426 		break;
17427 
17428 	case SATA_PKT_DEV_ERROR:
17429 		if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
17430 		    SATA_STATUS_ERR) {
17431 			/*
17432 			 * determine dev error reason from error
17433 			 * reg content
17434 			 */
17435 			sata_decode_device_error(spx, sense);
17436 			break;
17437 		}
17438 		/* No extended sense key - no info available */
17439 		break;
17440 
17441 	case SATA_PKT_TIMEOUT:
17442 		scsipkt->pkt_reason = CMD_TIMEOUT;
17443 		scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET;
17444 		/* No extended sense key */
17445 		break;
17446 
17447 	case SATA_PKT_ABORTED:
17448 		scsipkt->pkt_reason = CMD_ABORTED;
17449 		scsipkt->pkt_statistics |= STAT_ABORTED;
17450 		/* No extended sense key */
17451 		break;
17452 
17453 	case SATA_PKT_RESET:
17454 		/*
17455 		 * pkt aborted either by an explicit reset request from
17456 		 * a host, or due to error recovery
17457 		 */
17458 		scsipkt->pkt_reason = CMD_RESET;
17459 		scsipkt->pkt_statistics |= STAT_DEV_RESET;
17460 		break;
17461 
17462 	default:
17463 		scsipkt->pkt_reason = CMD_TRAN_ERR;
17464 		break;
17465 	}
17466 }
17467 
17468 
17469 
17470 
17471 /*
17472  * Log sata message
17473  * dev pathname msg line preceeds the logged message.
17474  */
17475 
17476 static	void
17477 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...)
17478 {
17479 	char pathname[128];
17480 	dev_info_t *dip = NULL;
17481 	va_list ap;
17482 
17483 	mutex_enter(&sata_log_mutex);
17484 
17485 	va_start(ap, fmt);
17486 	(void) vsprintf(sata_log_buf, fmt, ap);
17487 	va_end(ap);
17488 
17489 	if (sata_hba_inst != NULL) {
17490 		dip = SATA_DIP(sata_hba_inst);
17491 		(void) ddi_pathname(dip, pathname);
17492 	} else {
17493 		pathname[0] = 0;
17494 	}
17495 	if (level == CE_CONT) {
17496 		if (sata_debug_flags == 0)
17497 			cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf);
17498 		else
17499 			cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf);
17500 	} else {
17501 		if (level != CE_NOTE) {
17502 			cmn_err(level, "%s:\n %s", pathname, sata_log_buf);
17503 		} else if (sata_msg) {
17504 			cmn_err(level, "%s:\n %s", pathname,
17505 			    sata_log_buf);
17506 		}
17507 	}
17508 
17509 	/* sata trace debug */
17510 	sata_trace_debug(dip, sata_log_buf);
17511 
17512 	mutex_exit(&sata_log_mutex);
17513 }
17514 
17515 
17516 /* ******** Asynchronous HBA events handling & hotplugging support ******** */
17517 
17518 /*
17519  * Start or terminate the thread, depending on flag arg and current state
17520  */
17521 static void
17522 sata_event_thread_control(int startstop)
17523 {
17524 	static 	int sata_event_thread_terminating = 0;
17525 	static 	int sata_event_thread_starting = 0;
17526 	int i;
17527 
17528 	mutex_enter(&sata_event_mutex);
17529 
17530 	if (startstop == 0 && (sata_event_thread_starting == 1 ||
17531 	    sata_event_thread_terminating == 1)) {
17532 		mutex_exit(&sata_event_mutex);
17533 		return;
17534 	}
17535 	if (startstop == 1 && sata_event_thread_starting == 1) {
17536 		mutex_exit(&sata_event_mutex);
17537 		return;
17538 	}
17539 	if (startstop == 1 && sata_event_thread_terminating == 1) {
17540 		sata_event_thread_starting = 1;
17541 		/* wait til terminate operation completes */
17542 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
17543 		while (sata_event_thread_terminating == 1) {
17544 			if (i-- <= 0) {
17545 				sata_event_thread_starting = 0;
17546 				mutex_exit(&sata_event_mutex);
17547 #ifdef SATA_DEBUG
17548 				cmn_err(CE_WARN, "sata_event_thread_control: "
17549 				    "timeout waiting for thread to terminate");
17550 #endif
17551 				return;
17552 			}
17553 			mutex_exit(&sata_event_mutex);
17554 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
17555 			mutex_enter(&sata_event_mutex);
17556 		}
17557 	}
17558 	if (startstop == 1) {
17559 		if (sata_event_thread == NULL) {
17560 			sata_event_thread = thread_create(NULL, 0,
17561 			    (void (*)())sata_event_daemon,
17562 			    &sata_hba_list, 0, &p0, TS_RUN, minclsyspri);
17563 		}
17564 		sata_event_thread_starting = 0;
17565 		mutex_exit(&sata_event_mutex);
17566 		return;
17567 	}
17568 
17569 	/*
17570 	 * If we got here, thread may need to be terminated
17571 	 */
17572 	if (sata_event_thread != NULL) {
17573 		int i;
17574 		/* Signal event thread to go away */
17575 		sata_event_thread_terminating = 1;
17576 		sata_event_thread_terminate = 1;
17577 		cv_signal(&sata_event_cv);
17578 		/*
17579 		 * Wait til daemon terminates.
17580 		 */
17581 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
17582 		while (sata_event_thread_terminate == 1) {
17583 			mutex_exit(&sata_event_mutex);
17584 			if (i-- <= 0) {
17585 				/* Daemon did not go away !!! */
17586 #ifdef SATA_DEBUG
17587 				cmn_err(CE_WARN, "sata_event_thread_control: "
17588 				    "cannot terminate event daemon thread");
17589 #endif
17590 				mutex_enter(&sata_event_mutex);
17591 				break;
17592 			}
17593 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
17594 			mutex_enter(&sata_event_mutex);
17595 		}
17596 		sata_event_thread_terminating = 0;
17597 	}
17598 	ASSERT(sata_event_thread_terminating == 0);
17599 	ASSERT(sata_event_thread_starting == 0);
17600 	mutex_exit(&sata_event_mutex);
17601 }
17602 
17603 
17604 /*
17605  * SATA HBA event notification function.
17606  * Events reported by SATA HBA drivers per HBA instance relate to a change in
17607  * a port and/or device state or a controller itself.
17608  * Events for different addresses/addr types cannot be combined.
17609  * A warning message is generated for each event type.
17610  * Events are not processed by this function, so only the
17611  * event flag(s)is set for an affected entity and the event thread is
17612  * waken up. Event daemon thread processes all events.
17613  *
17614  * NOTE: Since more than one event may be reported at the same time, one
17615  * cannot determine a sequence of events when opposite event are reported, eg.
17616  * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing
17617  * is taking precedence over reported events, i.e. may cause ignoring some
17618  * events.
17619  */
17620 #define	SATA_EVENT_MAX_MSG_LENGTH	79
17621 
17622 void
17623 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event)
17624 {
17625 	sata_hba_inst_t *sata_hba_inst = NULL;
17626 	sata_address_t *saddr;
17627 	sata_pmult_info_t *pmultinfo;
17628 	sata_drive_info_t *sdinfo;
17629 	sata_port_stats_t *pstats;
17630 	sata_cport_info_t *cportinfo;
17631 	sata_pmport_info_t *pmportinfo;
17632 	int cport, pmport;
17633 	char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1];
17634 	char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1];
17635 	char *lcp;
17636 	static char *err_msg_evnt_1 =
17637 	    "sata_hba_event_notify: invalid port event 0x%x ";
17638 	static char *err_msg_evnt_2 =
17639 	    "sata_hba_event_notify: invalid device event 0x%x ";
17640 	int linkevent;
17641 
17642 	/*
17643 	 * There is a possibility that an event will be generated on HBA
17644 	 * that has not completed attachment or is detaching. We still want
17645 	 * to process events until HBA is detached.
17646 	 */
17647 	mutex_enter(&sata_mutex);
17648 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
17649 	    sata_hba_inst = sata_hba_inst->satahba_next) {
17650 		if (SATA_DIP(sata_hba_inst) == dip)
17651 			if (sata_hba_inst->satahba_attached == 1)
17652 				break;
17653 	}
17654 	mutex_exit(&sata_mutex);
17655 	if (sata_hba_inst == NULL)
17656 		/* HBA not attached */
17657 		return;
17658 
17659 	ASSERT(sata_device != NULL);
17660 
17661 	/*
17662 	 * Validate address before - do not proceed with invalid address.
17663 	 */
17664 	saddr = &sata_device->satadev_addr;
17665 	if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst))
17666 		return;
17667 
17668 	cport = saddr->cport;
17669 	pmport = saddr->pmport;
17670 
17671 	buf1[0] = buf2[0] = '\0';
17672 
17673 	/*
17674 	 * If event relates to port or device, check port state.
17675 	 * Port has to be initialized, or we cannot accept an event.
17676 	 */
17677 	if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT |
17678 	    SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT | SATA_ADDR_PMULT)) != 0) {
17679 		mutex_enter(&sata_hba_inst->satahba_mutex);
17680 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
17681 		mutex_exit(&sata_hba_inst->satahba_mutex);
17682 		if (cportinfo == NULL || cportinfo->cport_state == 0)
17683 			return;
17684 	}
17685 
17686 	if ((saddr->qual & (SATA_ADDR_PMULT | SATA_ADDR_PMPORT |
17687 	    SATA_ADDR_DPMPORT)) != 0) {
17688 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
17689 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17690 			    "sata_hba_event_notify: Non-pmult device (0x%x)"
17691 			    "is attached to port %d, ignore pmult/pmport "
17692 			    "event 0x%x", cportinfo->cport_dev_type,
17693 			    cport, event));
17694 			return;
17695 		}
17696 
17697 		mutex_enter(&cportinfo->cport_mutex);
17698 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
17699 		mutex_exit(&cportinfo->cport_mutex);
17700 
17701 		/*
17702 		 * The daemon might be processing attachment of port
17703 		 * multiplier, in that case we should ignore events on its
17704 		 * sub-devices.
17705 		 *
17706 		 * NOTE: Only pmult_state is checked in sata_hba_event_notify.
17707 		 * The pmport_state is checked by sata daemon.
17708 		 */
17709 		if (pmultinfo == NULL ||
17710 		    pmultinfo->pmult_state == SATA_STATE_UNKNOWN) {
17711 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17712 			    "sata_hba_event_notify: pmult is not"
17713 			    "available at port %d:%d, ignore event 0x%x",
17714 			    cport, pmport, event));
17715 			return;
17716 		}
17717 	}
17718 
17719 	if ((saddr->qual &
17720 	    (SATA_ADDR_PMPORT | SATA_ADDR_DPMPORT)) != 0) {
17721 
17722 		mutex_enter(&cportinfo->cport_mutex);
17723 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) {
17724 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17725 			    "sata_hba_event_notify: invalid/"
17726 			    "un-implemented port %d:%d (%d ports), "
17727 			    "ignore event 0x%x", cport, pmport,
17728 			    SATA_NUM_PMPORTS(sata_hba_inst, cport), event));
17729 			mutex_exit(&cportinfo->cport_mutex);
17730 			return;
17731 		}
17732 		mutex_exit(&cportinfo->cport_mutex);
17733 
17734 		mutex_enter(&sata_hba_inst->satahba_mutex);
17735 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
17736 		    cport, pmport);
17737 		mutex_exit(&sata_hba_inst->satahba_mutex);
17738 
17739 		/* pmport is implemented/valid? */
17740 		if (pmportinfo == NULL) {
17741 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17742 			    "sata_hba_event_notify: invalid/"
17743 			    "un-implemented port %d:%d, ignore "
17744 			    "event 0x%x", cport, pmport, event));
17745 			return;
17746 		}
17747 	}
17748 
17749 	/*
17750 	 * Events refer to devices, ports and controllers - each has
17751 	 * unique address. Events for different addresses cannot be combined.
17752 	 */
17753 	if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) {
17754 
17755 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17756 
17757 		/* qualify this event(s) */
17758 		if ((event & SATA_EVNT_PORT_EVENTS) == 0) {
17759 			/* Invalid event for the device port */
17760 			(void) sprintf(buf2, err_msg_evnt_1,
17761 			    event & SATA_EVNT_PORT_EVENTS);
17762 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17763 			goto event_info;
17764 		}
17765 		if (saddr->qual == SATA_ADDR_CPORT) {
17766 			/* Controller's device port event */
17767 
17768 			(SATA_CPORT_INFO(sata_hba_inst, cport))->
17769 			    cport_event_flags |=
17770 			    event & SATA_EVNT_PORT_EVENTS;
17771 			pstats =
17772 			    &(SATA_CPORT_INFO(sata_hba_inst, cport))->
17773 			    cport_stats;
17774 		} else {
17775 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17776 			mutex_enter(&pmportinfo->pmport_mutex);
17777 			/* Port multiplier's device port event */
17778 			(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
17779 			    pmport_event_flags |=
17780 			    event & SATA_EVNT_PORT_EVENTS;
17781 			pstats =
17782 			    &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
17783 			    pmport_stats;
17784 			mutex_exit(&pmportinfo->pmport_mutex);
17785 			mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17786 		}
17787 
17788 		/*
17789 		 * Add to statistics and log the message. We have to do it
17790 		 * here rather than in the event daemon, because there may be
17791 		 * multiple events occuring before they are processed.
17792 		 */
17793 		linkevent = event &
17794 		    (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED);
17795 		if (linkevent) {
17796 			if (linkevent == (SATA_EVNT_LINK_LOST |
17797 			    SATA_EVNT_LINK_ESTABLISHED)) {
17798 				/* This is likely event combination */
17799 				(void) strlcat(buf1, "link lost/established, ",
17800 				    SATA_EVENT_MAX_MSG_LENGTH);
17801 
17802 				if (pstats->link_lost < 0xffffffffffffffffULL)
17803 					pstats->link_lost++;
17804 				if (pstats->link_established <
17805 				    0xffffffffffffffffULL)
17806 					pstats->link_established++;
17807 				linkevent = 0;
17808 			} else if (linkevent & SATA_EVNT_LINK_LOST) {
17809 				(void) strlcat(buf1, "link lost, ",
17810 				    SATA_EVENT_MAX_MSG_LENGTH);
17811 
17812 				if (pstats->link_lost < 0xffffffffffffffffULL)
17813 					pstats->link_lost++;
17814 			} else {
17815 				(void) strlcat(buf1, "link established, ",
17816 				    SATA_EVENT_MAX_MSG_LENGTH);
17817 				if (pstats->link_established <
17818 				    0xffffffffffffffffULL)
17819 					pstats->link_established++;
17820 			}
17821 		}
17822 		if (event & SATA_EVNT_DEVICE_ATTACHED) {
17823 			(void) strlcat(buf1, "device attached, ",
17824 			    SATA_EVENT_MAX_MSG_LENGTH);
17825 			if (pstats->device_attached < 0xffffffffffffffffULL)
17826 				pstats->device_attached++;
17827 		}
17828 		if (event & SATA_EVNT_DEVICE_DETACHED) {
17829 			(void) strlcat(buf1, "device detached, ",
17830 			    SATA_EVENT_MAX_MSG_LENGTH);
17831 			if (pstats->device_detached < 0xffffffffffffffffULL)
17832 				pstats->device_detached++;
17833 		}
17834 		if (event & SATA_EVNT_PWR_LEVEL_CHANGED) {
17835 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
17836 			    "port %d power level changed", cport);
17837 			if (pstats->port_pwr_changed < 0xffffffffffffffffULL)
17838 				pstats->port_pwr_changed++;
17839 		}
17840 
17841 		if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) {
17842 			/* There should be no other events for this address */
17843 			(void) sprintf(buf2, err_msg_evnt_1,
17844 			    event & ~SATA_EVNT_PORT_EVENTS);
17845 		}
17846 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17847 
17848 	} else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) {
17849 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17850 
17851 		/* qualify this event */
17852 		if ((event & SATA_EVNT_DEVICE_RESET) == 0) {
17853 			/* Invalid event for a device */
17854 			(void) sprintf(buf2, err_msg_evnt_2,
17855 			    event & SATA_EVNT_DEVICE_RESET);
17856 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17857 			goto event_info;
17858 		}
17859 		/* drive event */
17860 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
17861 		if (sdinfo != NULL) {
17862 			if (event & SATA_EVNT_DEVICE_RESET) {
17863 				(void) strlcat(buf1, "device reset, ",
17864 				    SATA_EVENT_MAX_MSG_LENGTH);
17865 				if (sdinfo->satadrv_stats.drive_reset <
17866 				    0xffffffffffffffffULL)
17867 					sdinfo->satadrv_stats.drive_reset++;
17868 				sdinfo->satadrv_event_flags |=
17869 				    SATA_EVNT_DEVICE_RESET;
17870 			}
17871 		}
17872 		if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) {
17873 			/* Invalid event for a device */
17874 			(void) sprintf(buf2, err_msg_evnt_2,
17875 			    event & ~SATA_EVNT_DRIVE_EVENTS);
17876 		}
17877 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17878 	} else if (saddr->qual == SATA_ADDR_PMULT) {
17879 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17880 
17881 		/* qualify this event */
17882 		if ((event & (SATA_EVNT_DEVICE_RESET |
17883 		    SATA_EVNT_PMULT_LINK_CHANGED)) == 0) {
17884 			/* Invalid event for a port multiplier */
17885 			(void) sprintf(buf2, err_msg_evnt_2,
17886 			    event & SATA_EVNT_DEVICE_RESET);
17887 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17888 			goto event_info;
17889 		}
17890 
17891 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
17892 
17893 		if (event & SATA_EVNT_DEVICE_RESET) {
17894 
17895 			SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
17896 			    "[Reset] port-mult on cport %d", cport);
17897 			pmultinfo->pmult_event_flags |=
17898 			    SATA_EVNT_DEVICE_RESET;
17899 			(void) strlcat(buf1, "pmult reset, ",
17900 			    SATA_EVENT_MAX_MSG_LENGTH);
17901 		}
17902 
17903 		if (event & SATA_EVNT_PMULT_LINK_CHANGED) {
17904 
17905 			SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
17906 			    "pmult link changed on cport %d", cport);
17907 			pmultinfo->pmult_event_flags |=
17908 			    SATA_EVNT_PMULT_LINK_CHANGED;
17909 			(void) strlcat(buf1, "pmult link changed, ",
17910 			    SATA_EVENT_MAX_MSG_LENGTH);
17911 		}
17912 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17913 
17914 	} else {
17915 		if (saddr->qual != SATA_ADDR_NULL) {
17916 			/* Wrong address qualifier */
17917 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17918 			    "sata_hba_event_notify: invalid address 0x%x",
17919 			    *(uint32_t *)saddr));
17920 			return;
17921 		}
17922 		if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 ||
17923 		    (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) {
17924 			/* Invalid event for the controller */
17925 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17926 			    "sata_hba_event_notify: invalid event 0x%x for "
17927 			    "controller",
17928 			    event & SATA_EVNT_CONTROLLER_EVENTS));
17929 			return;
17930 		}
17931 		buf1[0] = '\0';
17932 		/* This may be a frequent and not interesting event */
17933 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
17934 		    "controller power level changed\n", NULL);
17935 
17936 		mutex_enter(&sata_hba_inst->satahba_mutex);
17937 		if (sata_hba_inst->satahba_stats.ctrl_pwr_change <
17938 		    0xffffffffffffffffULL)
17939 			sata_hba_inst->satahba_stats.ctrl_pwr_change++;
17940 
17941 		sata_hba_inst->satahba_event_flags |=
17942 		    SATA_EVNT_PWR_LEVEL_CHANGED;
17943 		mutex_exit(&sata_hba_inst->satahba_mutex);
17944 	}
17945 	/*
17946 	 * If we got here, there is something to do with this HBA
17947 	 * instance.
17948 	 */
17949 	mutex_enter(&sata_hba_inst->satahba_mutex);
17950 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
17951 	mutex_exit(&sata_hba_inst->satahba_mutex);
17952 	mutex_enter(&sata_mutex);
17953 	sata_event_pending |= SATA_EVNT_MAIN;	/* global event indicator */
17954 	mutex_exit(&sata_mutex);
17955 
17956 	/* Tickle event thread */
17957 	mutex_enter(&sata_event_mutex);
17958 	if (sata_event_thread_active == 0)
17959 		cv_signal(&sata_event_cv);
17960 	mutex_exit(&sata_event_mutex);
17961 
17962 event_info:
17963 	if (buf1[0] != '\0') {
17964 		lcp = strrchr(buf1, ',');
17965 		if (lcp != NULL)
17966 			*lcp = '\0';
17967 	}
17968 	if (saddr->qual == SATA_ADDR_CPORT ||
17969 	    saddr->qual == SATA_ADDR_DCPORT) {
17970 		if (buf1[0] != '\0') {
17971 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
17972 			    cport, buf1);
17973 		}
17974 		if (buf2[0] != '\0') {
17975 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
17976 			    cport, buf2);
17977 		}
17978 	} else if (saddr->qual == SATA_ADDR_PMPORT ||
17979 	    saddr->qual == SATA_ADDR_DPMPORT) {
17980 		if (buf1[0] != '\0') {
17981 			sata_log(sata_hba_inst, CE_NOTE,
17982 			    "port %d pmport %d: %s\n", cport, pmport, buf1);
17983 		}
17984 		if (buf2[0] != '\0') {
17985 			sata_log(sata_hba_inst, CE_NOTE,
17986 			    "port %d pmport %d: %s\n", cport, pmport, buf2);
17987 		}
17988 	}
17989 }
17990 
17991 
17992 /*
17993  * Event processing thread.
17994  * Arg is a pointer to the sata_hba_list pointer.
17995  * It is not really needed, because sata_hba_list is global and static
17996  */
17997 static void
17998 sata_event_daemon(void *arg)
17999 {
18000 #ifndef __lock_lint
18001 	_NOTE(ARGUNUSED(arg))
18002 #endif
18003 	sata_hba_inst_t *sata_hba_inst;
18004 	clock_t delta;
18005 
18006 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18007 	    "SATA event daemon started\n", NULL);
18008 loop:
18009 	/*
18010 	 * Process events here. Walk through all registered HBAs
18011 	 */
18012 	mutex_enter(&sata_mutex);
18013 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
18014 	    sata_hba_inst = sata_hba_inst->satahba_next) {
18015 		ASSERT(sata_hba_inst != NULL);
18016 		mutex_enter(&sata_hba_inst->satahba_mutex);
18017 		if (sata_hba_inst->satahba_attached == 0 ||
18018 		    (sata_hba_inst->satahba_event_flags &
18019 		    SATA_EVNT_SKIP) != 0) {
18020 			mutex_exit(&sata_hba_inst->satahba_mutex);
18021 			continue;
18022 		}
18023 		if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) {
18024 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP;
18025 			mutex_exit(&sata_hba_inst->satahba_mutex);
18026 			mutex_exit(&sata_mutex);
18027 			/* Got the controller with pending event */
18028 			sata_process_controller_events(sata_hba_inst);
18029 			/*
18030 			 * Since global mutex was released, there is a
18031 			 * possibility that HBA list has changed, so start
18032 			 * over from the top. Just processed controller
18033 			 * will be passed-over because of the SKIP flag.
18034 			 */
18035 			goto loop;
18036 		}
18037 		mutex_exit(&sata_hba_inst->satahba_mutex);
18038 	}
18039 	/* Clear SKIP flag in all controllers */
18040 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
18041 	    sata_hba_inst = sata_hba_inst->satahba_next) {
18042 		mutex_enter(&sata_hba_inst->satahba_mutex);
18043 		sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP;
18044 		mutex_exit(&sata_hba_inst->satahba_mutex);
18045 	}
18046 	mutex_exit(&sata_mutex);
18047 
18048 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18049 	    "SATA EVENT DAEMON suspending itself", NULL);
18050 
18051 #ifdef SATA_DEBUG
18052 	if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) {
18053 		sata_log(sata_hba_inst, CE_WARN,
18054 		    "SATA EVENTS PROCESSING DISABLED\n");
18055 		thread_exit(); /* Daemon will not run again */
18056 	}
18057 #endif
18058 	mutex_enter(&sata_event_mutex);
18059 	sata_event_thread_active = 0;
18060 	mutex_exit(&sata_event_mutex);
18061 	/*
18062 	 * Go to sleep/suspend itself and wake up either because new event or
18063 	 * wait timeout. Exit if there is a termination request (driver
18064 	 * unload).
18065 	 */
18066 	delta = drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME);
18067 	do {
18068 		mutex_enter(&sata_event_mutex);
18069 		(void) cv_reltimedwait(&sata_event_cv, &sata_event_mutex,
18070 		    delta, TR_CLOCK_TICK);
18071 
18072 		if (sata_event_thread_active != 0) {
18073 			mutex_exit(&sata_event_mutex);
18074 			continue;
18075 		}
18076 
18077 		/* Check if it is time to go away */
18078 		if (sata_event_thread_terminate == 1) {
18079 			/*
18080 			 * It is up to the thread setting above flag to make
18081 			 * sure that this thread is not killed prematurely.
18082 			 */
18083 			sata_event_thread_terminate = 0;
18084 			sata_event_thread = NULL;
18085 			mutex_exit(&sata_event_mutex);
18086 			SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18087 			    "SATA_EVENT_DAEMON_TERMINATING", NULL);
18088 			thread_exit();  { _NOTE(NOT_REACHED) }
18089 		}
18090 		mutex_exit(&sata_event_mutex);
18091 	} while (!(sata_event_pending & SATA_EVNT_MAIN));
18092 
18093 	mutex_enter(&sata_event_mutex);
18094 	sata_event_thread_active = 1;
18095 	mutex_exit(&sata_event_mutex);
18096 
18097 	mutex_enter(&sata_mutex);
18098 	sata_event_pending &= ~SATA_EVNT_MAIN;
18099 	mutex_exit(&sata_mutex);
18100 
18101 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18102 	    "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL);
18103 
18104 	goto loop;
18105 }
18106 
18107 /*
18108  * Specific HBA instance event processing.
18109  *
18110  * NOTE: At the moment, device event processing is limited to hard disks
18111  * only.
18112  * Port multiplier is supported now.
18113  */
18114 static void
18115 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst)
18116 {
18117 	int ncport;
18118 	uint32_t event_flags;
18119 	sata_address_t *saddr;
18120 	sata_cport_info_t *cportinfo;
18121 	sata_pmult_info_t *pmultinfo;
18122 
18123 	SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst,
18124 	    "Processing controller %d event(s)",
18125 	    ddi_get_instance(SATA_DIP(sata_hba_inst)));
18126 
18127 	mutex_enter(&sata_hba_inst->satahba_mutex);
18128 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN;
18129 	event_flags = sata_hba_inst->satahba_event_flags;
18130 	mutex_exit(&sata_hba_inst->satahba_mutex);
18131 	/*
18132 	 * Process controller power change first
18133 	 * HERE
18134 	 */
18135 	if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED)
18136 		sata_process_cntrl_pwr_level_change(sata_hba_inst);
18137 
18138 	/*
18139 	 * Search through ports/devices to identify affected port/device.
18140 	 * We may have to process events for more than one port/device.
18141 	 */
18142 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
18143 		/*
18144 		 * Not all ports may be processed in attach by the time we
18145 		 * get an event. Check if port info is initialized.
18146 		 */
18147 		mutex_enter(&sata_hba_inst->satahba_mutex);
18148 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
18149 		mutex_exit(&sata_hba_inst->satahba_mutex);
18150 		if (cportinfo == NULL || cportinfo->cport_state == NULL)
18151 			continue;
18152 
18153 		/* We have initialized controller port info */
18154 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18155 		event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))->
18156 		    cport_event_flags;
18157 		/* Check if port was locked by IOCTL processing */
18158 		if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) {
18159 			/*
18160 			 * We ignore port events because port is busy
18161 			 * with AP control processing. Set again
18162 			 * controller and main event flag, so that
18163 			 * events may be processed by the next daemon
18164 			 * run.
18165 			 */
18166 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18167 			mutex_enter(&sata_hba_inst->satahba_mutex);
18168 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
18169 			mutex_exit(&sata_hba_inst->satahba_mutex);
18170 			mutex_enter(&sata_mutex);
18171 			sata_event_pending |= SATA_EVNT_MAIN;
18172 			mutex_exit(&sata_mutex);
18173 			SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst,
18174 			    "Event processing postponed until "
18175 			    "AP control processing completes",
18176 			    NULL);
18177 			/* Check other ports */
18178 			continue;
18179 		} else {
18180 			/*
18181 			 * Set BSY flag so that AP control would not
18182 			 * interfere with events processing for
18183 			 * this port.
18184 			 */
18185 			(SATA_CPORT_INFO(sata_hba_inst, ncport))->
18186 			    cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY;
18187 		}
18188 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18189 
18190 		saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr;
18191 
18192 		if ((event_flags &
18193 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
18194 			/*
18195 			 * Got port event.
18196 			 * We need some hierarchy of event processing as they
18197 			 * are affecting each other:
18198 			 * 1. port failed
18199 			 * 2. device detached/attached
18200 			 * 3. link events - link events may trigger device
18201 			 *    detached or device attached events in some
18202 			 *    circumstances.
18203 			 * 4. port power level changed
18204 			 */
18205 			if (event_flags & SATA_EVNT_PORT_FAILED) {
18206 				sata_process_port_failed_event(sata_hba_inst,
18207 				    saddr);
18208 			}
18209 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
18210 				sata_process_device_detached(sata_hba_inst,
18211 				    saddr);
18212 			}
18213 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
18214 				sata_process_device_attached(sata_hba_inst,
18215 				    saddr);
18216 			}
18217 			if (event_flags &
18218 			    (SATA_EVNT_LINK_ESTABLISHED |
18219 			    SATA_EVNT_LINK_LOST)) {
18220 				sata_process_port_link_events(sata_hba_inst,
18221 				    saddr);
18222 			}
18223 			if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) {
18224 				sata_process_port_pwr_change(sata_hba_inst,
18225 				    saddr);
18226 			}
18227 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
18228 				sata_process_target_node_cleanup(
18229 				    sata_hba_inst, saddr);
18230 			}
18231 			if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) {
18232 				sata_process_device_autoonline(
18233 				    sata_hba_inst, saddr);
18234 			}
18235 		}
18236 
18237 
18238 		/*
18239 		 * Scan port multiplier and all its sub-ports event flags.
18240 		 * The events are marked by
18241 		 * (1) sata_pmult_info.pmult_event_flags
18242 		 * (2) sata_pmport_info.pmport_event_flags
18243 		 */
18244 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18245 		if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
18246 			/*
18247 			 * There should be another extra check: this
18248 			 * port multiplier still exists?
18249 			 */
18250 			pmultinfo = SATA_PMULT_INFO(sata_hba_inst,
18251 			    ncport);
18252 
18253 			if (pmultinfo != NULL) {
18254 				mutex_exit(&(SATA_CPORT_MUTEX(
18255 				    sata_hba_inst, ncport)));
18256 				sata_process_pmult_events(
18257 				    sata_hba_inst, ncport);
18258 				mutex_enter(&(SATA_CPORT_MUTEX(
18259 				    sata_hba_inst, ncport)));
18260 			} else {
18261 				SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
18262 				    "Port-multiplier is gone. "
18263 				    "Ignore all sub-device events "
18264 				    "at port %d.", ncport);
18265 			}
18266 		}
18267 
18268 		if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) !=
18269 		    SATA_DTYPE_NONE) &&
18270 		    (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) {
18271 			if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)->
18272 			    satadrv_event_flags &
18273 			    (SATA_EVNT_DEVICE_RESET |
18274 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
18275 				/* Have device event */
18276 				sata_process_device_reset(sata_hba_inst,
18277 				    saddr);
18278 			}
18279 		}
18280 		/* Release PORT_BUSY flag */
18281 		(SATA_CPORT_INFO(sata_hba_inst, ncport))->
18282 		    cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
18283 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18284 
18285 	} /* End of loop through the controller SATA ports */
18286 }
18287 
18288 /*
18289  * Specific port multiplier instance event processing. At the moment, device
18290  * event processing is limited to link/attach event only.
18291  *
18292  * NOTE: power management event is not supported yet.
18293  */
18294 static void
18295 sata_process_pmult_events(sata_hba_inst_t *sata_hba_inst, uint8_t cport)
18296 {
18297 	sata_cport_info_t *cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
18298 	sata_pmult_info_t *pmultinfo;
18299 	sata_pmport_info_t *pmportinfo;
18300 	sata_address_t *saddr;
18301 	sata_device_t sata_device;
18302 	uint32_t event_flags;
18303 	int npmport;
18304 	int rval;
18305 
18306 	SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst,
18307 	    "Processing pmult event(s) on cport %d of controller %d",
18308 	    cport, ddi_get_instance(SATA_DIP(sata_hba_inst)));
18309 
18310 	/* First process events on port multiplier */
18311 	mutex_enter(&cportinfo->cport_mutex);
18312 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
18313 	event_flags = pmultinfo->pmult_event_flags;
18314 
18315 	/*
18316 	 * Reset event (of port multiplier) has higher priority because the
18317 	 * port multiplier itself might be failed or removed after reset.
18318 	 */
18319 	if (event_flags & SATA_EVNT_DEVICE_RESET) {
18320 		/*
18321 		 * The status of the sub-links are uncertain,
18322 		 * so mark all sub-ports as RESET
18323 		 */
18324 		for (npmport = 0; npmport < SATA_NUM_PMPORTS(
18325 		    sata_hba_inst, cport); npmport ++) {
18326 			pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
18327 			    cport, npmport);
18328 			if (pmportinfo == NULL) {
18329 				/* That's weird. */
18330 				SATA_LOG_D((sata_hba_inst, CE_WARN,
18331 				    "sata_hba_event_notify: "
18332 				    "invalid/un-implemented "
18333 				    "port %d:%d (%d ports), ",
18334 				    cport, npmport, SATA_NUM_PMPORTS(
18335 				    sata_hba_inst, cport)));
18336 				continue;
18337 			}
18338 
18339 			mutex_enter(&pmportinfo->pmport_mutex);
18340 
18341 			/* Mark all pmport to unknow state. */
18342 			pmportinfo->pmport_state = SATA_STATE_UNKNOWN;
18343 			/* Mark all pmports with link events. */
18344 			pmportinfo->pmport_event_flags =
18345 			    (SATA_EVNT_LINK_ESTABLISHED|SATA_EVNT_LINK_LOST);
18346 			mutex_exit(&pmportinfo->pmport_mutex);
18347 		}
18348 
18349 	} else if (event_flags & SATA_EVNT_PMULT_LINK_CHANGED) {
18350 		/*
18351 		 * We need probe the port multiplier to know what has
18352 		 * happened.
18353 		 */
18354 		bzero(&sata_device, sizeof (sata_device_t));
18355 		sata_device.satadev_rev = SATA_DEVICE_REV;
18356 		sata_device.satadev_addr.cport = cport;
18357 		sata_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
18358 		sata_device.satadev_addr.qual = SATA_ADDR_PMULT;
18359 
18360 		mutex_exit(&cportinfo->cport_mutex);
18361 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18362 		    (SATA_DIP(sata_hba_inst), &sata_device);
18363 		mutex_enter(&cportinfo->cport_mutex);
18364 		if (rval != SATA_SUCCESS) {
18365 			/* Something went wrong? Fail the port */
18366 			cportinfo->cport_state = SATA_PSTATE_FAILED;
18367 			mutex_exit(&cportinfo->cport_mutex);
18368 			SATA_LOG_D((sata_hba_inst, CE_WARN,
18369 			    "SATA port %d probing failed", cport));
18370 
18371 			/* PMult structure must be released.  */
18372 			sata_free_pmult(sata_hba_inst, &sata_device);
18373 			return;
18374 		}
18375 
18376 		sata_update_port_info(sata_hba_inst, &sata_device);
18377 
18378 		/*
18379 		 * Sanity check - Port is active? Is the link active?
18380 		 * The device is still a port multiplier?
18381 		 */
18382 		if ((cportinfo->cport_state &
18383 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
18384 		    ((cportinfo->cport_scr.sstatus &
18385 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) ||
18386 		    (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) {
18387 			mutex_exit(&cportinfo->cport_mutex);
18388 
18389 			/* PMult structure must be released.  */
18390 			sata_free_pmult(sata_hba_inst, &sata_device);
18391 			return;
18392 		}
18393 
18394 		/* Probed succeed, set port ready. */
18395 		cportinfo->cport_state |=
18396 		    SATA_STATE_PROBED | SATA_STATE_READY;
18397 	}
18398 
18399 	/* Release port multiplier event flags. */
18400 	pmultinfo->pmult_event_flags &=
18401 	    ~(SATA_EVNT_DEVICE_RESET|SATA_EVNT_PMULT_LINK_CHANGED);
18402 	mutex_exit(&cportinfo->cport_mutex);
18403 
18404 	/*
18405 	 * Check all sub-links.
18406 	 */
18407 	for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst, cport);
18408 	    npmport ++) {
18409 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport);
18410 		mutex_enter(&pmportinfo->pmport_mutex);
18411 		event_flags = pmportinfo->pmport_event_flags;
18412 		mutex_exit(&pmportinfo->pmport_mutex);
18413 		saddr = &pmportinfo->pmport_addr;
18414 
18415 		if ((event_flags &
18416 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
18417 			/*
18418 			 * Got port multiplier port event.
18419 			 * We need some hierarchy of event processing as they
18420 			 * are affecting each other:
18421 			 * 1. device detached/attached
18422 			 * 2. link events - link events may trigger device
18423 			 *    detached or device attached events in some
18424 			 *    circumstances.
18425 			 */
18426 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
18427 				sata_process_pmdevice_detached(sata_hba_inst,
18428 				    saddr);
18429 			}
18430 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
18431 				sata_process_pmdevice_attached(sata_hba_inst,
18432 				    saddr);
18433 			}
18434 			if (event_flags & SATA_EVNT_LINK_ESTABLISHED ||
18435 			    event_flags & SATA_EVNT_LINK_LOST) {
18436 				sata_process_pmport_link_events(sata_hba_inst,
18437 				    saddr);
18438 			}
18439 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
18440 				sata_process_target_node_cleanup(
18441 				    sata_hba_inst, saddr);
18442 			}
18443 		}
18444 
18445 		/* Checking drive event(s). */
18446 		mutex_enter(&pmportinfo->pmport_mutex);
18447 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
18448 		    pmportinfo->pmport_sata_drive != NULL) {
18449 			event_flags = pmportinfo->pmport_sata_drive->
18450 			    satadrv_event_flags;
18451 			if (event_flags & (SATA_EVNT_DEVICE_RESET |
18452 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
18453 
18454 				/* Have device event */
18455 				sata_process_pmdevice_reset(sata_hba_inst,
18456 				    saddr);
18457 			}
18458 		}
18459 		mutex_exit(&pmportinfo->pmport_mutex);
18460 
18461 		/* Release PORT_BUSY flag */
18462 		mutex_enter(&cportinfo->cport_mutex);
18463 		cportinfo->cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
18464 		mutex_exit(&cportinfo->cport_mutex);
18465 	}
18466 
18467 	SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst,
18468 	    "[DONE] pmult event(s) on cport %d of controller %d",
18469 	    cport, ddi_get_instance(SATA_DIP(sata_hba_inst)));
18470 }
18471 
18472 /*
18473  * Process HBA power level change reported by HBA driver.
18474  * Not implemented at this time - event is ignored.
18475  */
18476 static void
18477 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst)
18478 {
18479 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18480 	    "Processing controller power level change", NULL);
18481 
18482 	/* Ignoring it for now */
18483 	mutex_enter(&sata_hba_inst->satahba_mutex);
18484 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
18485 	mutex_exit(&sata_hba_inst->satahba_mutex);
18486 }
18487 
18488 /*
18489  * Process port power level change reported by HBA driver.
18490  * Not implemented at this time - event is ignored.
18491  */
18492 static void
18493 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst,
18494     sata_address_t *saddr)
18495 {
18496 	sata_cport_info_t *cportinfo;
18497 
18498 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18499 	    "Processing port power level change", NULL);
18500 
18501 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18502 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18503 	/* Reset event flag */
18504 	cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
18505 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18506 }
18507 
18508 /*
18509  * Process port failure reported by HBA driver.
18510  * cports support only - no pmports.
18511  */
18512 static void
18513 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst,
18514     sata_address_t *saddr)
18515 {
18516 	sata_cport_info_t *cportinfo;
18517 
18518 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18519 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18520 	/* Reset event flag first */
18521 	cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED;
18522 	/* If the port is in SHUTDOWN or FAILED state, ignore this event. */
18523 	if ((cportinfo->cport_state &
18524 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) {
18525 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18526 		    cport_mutex);
18527 		return;
18528 	}
18529 	/* Fail the port */
18530 	cportinfo->cport_state = SATA_PSTATE_FAILED;
18531 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18532 	sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport);
18533 }
18534 
18535 /*
18536  * Device Reset Event processing.
18537  * The sequence is managed by 3 stage flags:
18538  * - reset event reported,
18539  * - reset event being processed,
18540  * - request to clear device reset state.
18541  *
18542  * NOTE: This function has to be entered with cport mutex held. It exits with
18543  * mutex held as well, but can release mutex during the processing.
18544  */
18545 static void
18546 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst,
18547     sata_address_t *saddr)
18548 {
18549 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
18550 	sata_drive_info_t *sdinfo;
18551 	sata_cport_info_t *cportinfo;
18552 	sata_device_t sata_device;
18553 	int rval_probe, rval_set;
18554 
18555 	/* We only care about host sata cport for now */
18556 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18557 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18558 	/*
18559 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
18560 	 * state, ignore reset event.
18561 	 */
18562 	if (((cportinfo->cport_state &
18563 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
18564 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
18565 		sdinfo->satadrv_event_flags &=
18566 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
18567 		return;
18568 	}
18569 
18570 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) ==
18571 	    SATA_DTYPE_PMULT)) {
18572 		/*
18573 		 * Should not happened: this is already handled in
18574 		 * sata_hba_event_notify()
18575 		 */
18576 		mutex_exit(&cportinfo->cport_mutex);
18577 		goto done;
18578 	}
18579 
18580 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) &
18581 	    SATA_VALID_DEV_TYPE) == 0) {
18582 		/*
18583 		 * This should not happen - coding error.
18584 		 * But we can recover, so do not panic, just clean up
18585 		 * and if in debug mode, log the message.
18586 		 */
18587 #ifdef SATA_DEBUG
18588 		sata_log(sata_hba_inst, CE_WARN,
18589 		    "sata_process_device_reset: "
18590 		    "Invalid device type with sdinfo!", NULL);
18591 #endif
18592 		sdinfo->satadrv_event_flags = 0;
18593 		return;
18594 	}
18595 
18596 #ifdef SATA_DEBUG
18597 	if ((sdinfo->satadrv_event_flags &
18598 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
18599 		/* Nothing to do */
18600 		/* Something is weird - why we are processing dev reset? */
18601 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18602 		    "No device reset event!!!!", NULL);
18603 
18604 		return;
18605 	}
18606 	if ((sdinfo->satadrv_event_flags &
18607 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
18608 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
18609 		/* Something is weird - new device reset event */
18610 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18611 		    "Overlapping device reset events!", NULL);
18612 	}
18613 #endif
18614 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18615 	    "Processing port %d device reset", saddr->cport);
18616 
18617 	/* Clear event flag */
18618 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
18619 
18620 	/* It seems that we always need to check the port state first */
18621 	sata_device.satadev_rev = SATA_DEVICE_REV;
18622 	sata_device.satadev_addr = *saddr;
18623 	/*
18624 	 * We have to exit mutex, because the HBA probe port function may
18625 	 * block on its own mutex.
18626 	 */
18627 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18628 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18629 	    (SATA_DIP(sata_hba_inst), &sata_device);
18630 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18631 	sata_update_port_info(sata_hba_inst, &sata_device);
18632 	if (rval_probe != SATA_SUCCESS) {
18633 		/* Something went wrong? Fail the port */
18634 		cportinfo->cport_state = SATA_PSTATE_FAILED;
18635 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18636 		if (sdinfo != NULL)
18637 			sdinfo->satadrv_event_flags = 0;
18638 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18639 		    cport_mutex);
18640 		SATA_LOG_D((sata_hba_inst, CE_WARN,
18641 		    "SATA port %d probing failed",
18642 		    saddr->cport));
18643 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
18644 		    saddr->cport)->cport_mutex);
18645 		return;
18646 	}
18647 	if ((sata_device.satadev_scr.sstatus  &
18648 	    SATA_PORT_DEVLINK_UP_MASK) !=
18649 	    SATA_PORT_DEVLINK_UP ||
18650 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
18651 		/*
18652 		 * No device to process, anymore. Some other event processing
18653 		 * would or have already performed port info cleanup.
18654 		 * To be safe (HBA may need it), request clearing device
18655 		 * reset condition.
18656 		 */
18657 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18658 		if (sdinfo != NULL) {
18659 			sdinfo->satadrv_event_flags &=
18660 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18661 			sdinfo->satadrv_event_flags |=
18662 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18663 		}
18664 		return;
18665 	}
18666 
18667 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18668 	if (sdinfo == NULL) {
18669 		return;
18670 	}
18671 	if ((sdinfo->satadrv_event_flags &
18672 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
18673 		/*
18674 		 * Start tracking time for device feature restoration and
18675 		 * identification. Save current time (lbolt value).
18676 		 */
18677 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
18678 	}
18679 	/* Mark device reset processing as active */
18680 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
18681 
18682 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
18683 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18684 
18685 	rval_set = sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1);
18686 
18687 	if (rval_set  != SATA_SUCCESS) {
18688 		/*
18689 		 * Restoring drive setting failed.
18690 		 * Probe the port first, to check if the port state has changed
18691 		 */
18692 		sata_device.satadev_rev = SATA_DEVICE_REV;
18693 		sata_device.satadev_addr = *saddr;
18694 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
18695 		/* probe port */
18696 		rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18697 		    (SATA_DIP(sata_hba_inst), &sata_device);
18698 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18699 		    cport_mutex);
18700 		if (rval_probe == SATA_SUCCESS &&
18701 		    (sata_device.satadev_state &
18702 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
18703 		    (sata_device.satadev_scr.sstatus  &
18704 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
18705 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
18706 			/*
18707 			 * We may retry this a bit later - in-process reset
18708 			 * condition should be already set.
18709 			 * Track retry time for device identification.
18710 			 */
18711 			if ((cportinfo->cport_dev_type &
18712 			    SATA_VALID_DEV_TYPE) != 0 &&
18713 			    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL &&
18714 			    sdinfo->satadrv_reset_time != 0) {
18715 				clock_t cur_time = ddi_get_lbolt();
18716 				/*
18717 				 * If the retry time limit was not
18718 				 * exceeded, retry.
18719 				 */
18720 				if ((cur_time - sdinfo->satadrv_reset_time) <
18721 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
18722 					mutex_enter(
18723 					    &sata_hba_inst->satahba_mutex);
18724 					sata_hba_inst->satahba_event_flags |=
18725 					    SATA_EVNT_MAIN;
18726 					mutex_exit(
18727 					    &sata_hba_inst->satahba_mutex);
18728 					mutex_enter(&sata_mutex);
18729 					sata_event_pending |= SATA_EVNT_MAIN;
18730 					mutex_exit(&sata_mutex);
18731 					return;
18732 				}
18733 				if (rval_set == SATA_RETRY) {
18734 					/*
18735 					 * Setting drive features failed, but
18736 					 * the drive is still accessible,
18737 					 * so emit a warning message before
18738 					 * return.
18739 					 */
18740 					mutex_exit(&SATA_CPORT_INFO(
18741 					    sata_hba_inst,
18742 					    saddr->cport)->cport_mutex);
18743 					goto done;
18744 				}
18745 			}
18746 			/* Fail the drive */
18747 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
18748 
18749 			sata_log(sata_hba_inst, CE_WARN,
18750 			    "SATA device at port %d - device failed",
18751 			    saddr->cport);
18752 
18753 			DTRACE_PROBE(port_failed_f);
18754 		}
18755 		/*
18756 		 * No point of retrying - device failed or some other event
18757 		 * processing or already did or will do port info cleanup.
18758 		 * To be safe (HBA may need it),
18759 		 * request clearing device reset condition.
18760 		 */
18761 		sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
18762 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
18763 		sdinfo->satadrv_reset_time = 0;
18764 		return;
18765 	}
18766 done:
18767 	/*
18768 	 * If setting of drive features failed, but the drive is still
18769 	 * accessible, emit a warning message.
18770 	 */
18771 	if (rval_set == SATA_RETRY) {
18772 		sata_log(sata_hba_inst, CE_WARN,
18773 		    "SATA device at port %d - desired setting could not be "
18774 		    "restored after reset. Device may not operate as expected.",
18775 		    saddr->cport);
18776 	}
18777 	/*
18778 	 * Raise the flag indicating that the next sata command could
18779 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
18780 	 * reset is reported.
18781 	 */
18782 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18783 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
18784 		sdinfo->satadrv_reset_time = 0;
18785 		if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) {
18786 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
18787 			sdinfo->satadrv_event_flags &=
18788 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18789 			sdinfo->satadrv_event_flags |=
18790 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18791 		}
18792 	}
18793 }
18794 
18795 
18796 /*
18797  * Port Multiplier Port Device Reset Event processing.
18798  *
18799  * NOTE: This function has to be entered with pmport mutex held. It exits with
18800  * mutex held as well, but can release mutex during the processing.
18801  */
18802 static void
18803 sata_process_pmdevice_reset(sata_hba_inst_t *sata_hba_inst,
18804     sata_address_t *saddr)
18805 {
18806 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
18807 	sata_drive_info_t *sdinfo = NULL;
18808 	sata_cport_info_t *cportinfo = NULL;
18809 	sata_pmport_info_t *pmportinfo = NULL;
18810 	sata_pmult_info_t *pminfo = NULL;
18811 	sata_device_t sata_device;
18812 	uint8_t cport = saddr->cport;
18813 	uint8_t pmport = saddr->pmport;
18814 	int rval;
18815 
18816 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18817 	    "Processing drive reset at port %d:%d", cport, pmport);
18818 
18819 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
18820 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
18821 	sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport);
18822 
18823 	/*
18824 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
18825 	 * state, ignore reset event.
18826 	 */
18827 	if (((cportinfo->cport_state &
18828 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
18829 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
18830 		sdinfo->satadrv_event_flags &=
18831 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
18832 		return;
18833 	}
18834 
18835 	if ((pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
18836 		/*
18837 		 * This should not happen - coding error.
18838 		 * But we can recover, so do not panic, just clean up
18839 		 * and if in debug mode, log the message.
18840 		 */
18841 #ifdef SATA_DEBUG
18842 		sata_log(sata_hba_inst, CE_WARN,
18843 		    "sata_process_pmdevice_reset: "
18844 		    "Invalid device type with sdinfo!", NULL);
18845 #endif
18846 		sdinfo->satadrv_event_flags = 0;
18847 		return;
18848 	}
18849 
18850 #ifdef SATA_DEBUG
18851 	if ((sdinfo->satadrv_event_flags &
18852 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
18853 		/* Nothing to do */
18854 		/* Something is weird - why we are processing dev reset? */
18855 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18856 		    "No device reset event!!!!", NULL);
18857 
18858 		return;
18859 	}
18860 	if ((sdinfo->satadrv_event_flags &
18861 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
18862 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
18863 		/* Something is weird - new device reset event */
18864 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18865 		    "Overlapping device reset events!", NULL);
18866 	}
18867 #endif
18868 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18869 	    "Processing port %d:%d device reset", cport, pmport);
18870 
18871 	/* Clear event flag */
18872 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
18873 
18874 	/* It seems that we always need to check the port state first */
18875 	sata_device.satadev_rev = SATA_DEVICE_REV;
18876 	sata_device.satadev_addr = *saddr;
18877 	/*
18878 	 * We have to exit mutex, because the HBA probe port function may
18879 	 * block on its own mutex.
18880 	 */
18881 	mutex_exit(&pmportinfo->pmport_mutex);
18882 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18883 	    (SATA_DIP(sata_hba_inst), &sata_device);
18884 	mutex_enter(&pmportinfo->pmport_mutex);
18885 
18886 	sata_update_pmport_info(sata_hba_inst, &sata_device);
18887 	if (rval != SATA_SUCCESS) {
18888 		/* Something went wrong? Fail the port */
18889 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
18890 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18891 		    saddr->pmport);
18892 		if (sdinfo != NULL)
18893 			sdinfo->satadrv_event_flags = 0;
18894 		mutex_exit(&pmportinfo->pmport_mutex);
18895 		SATA_LOG_D((sata_hba_inst, CE_WARN,
18896 		    "SATA port %d:%d probing failed",
18897 		    saddr->cport, saddr->pmport));
18898 		mutex_enter(&pmportinfo->pmport_mutex);
18899 		return;
18900 	}
18901 	if ((sata_device.satadev_scr.sstatus  &
18902 	    SATA_PORT_DEVLINK_UP_MASK) !=
18903 	    SATA_PORT_DEVLINK_UP ||
18904 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
18905 		/*
18906 		 * No device to process, anymore. Some other event processing
18907 		 * would or have already performed port info cleanup.
18908 		 * To be safe (HBA may need it), request clearing device
18909 		 * reset condition.
18910 		 */
18911 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18912 		    saddr->pmport);
18913 		if (sdinfo != NULL) {
18914 			sdinfo->satadrv_event_flags &=
18915 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18916 			/* must clear flags on cport */
18917 			pminfo = SATA_PMULT_INFO(sata_hba_inst,
18918 			    saddr->cport);
18919 			pminfo->pmult_event_flags |=
18920 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18921 		}
18922 		return;
18923 	}
18924 
18925 	sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18926 	    saddr->pmport);
18927 	if (sdinfo == NULL) {
18928 		return;
18929 	}
18930 	if ((sdinfo->satadrv_event_flags &
18931 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
18932 		/*
18933 		 * Start tracking time for device feature restoration and
18934 		 * identification. Save current time (lbolt value).
18935 		 */
18936 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
18937 	}
18938 	/* Mark device reset processing as active */
18939 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
18940 
18941 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
18942 	mutex_exit(&pmportinfo->pmport_mutex);
18943 
18944 	if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) ==
18945 	    SATA_FAILURE) {
18946 		/*
18947 		 * Restoring drive setting failed.
18948 		 * Probe the port first, to check if the port state has changed
18949 		 */
18950 		sata_device.satadev_rev = SATA_DEVICE_REV;
18951 		sata_device.satadev_addr = *saddr;
18952 		sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
18953 
18954 		/* probe port */
18955 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18956 		    (SATA_DIP(sata_hba_inst), &sata_device);
18957 		mutex_enter(&pmportinfo->pmport_mutex);
18958 		if (rval == SATA_SUCCESS &&
18959 		    (sata_device.satadev_state &
18960 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
18961 		    (sata_device.satadev_scr.sstatus  &
18962 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
18963 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
18964 			/*
18965 			 * We may retry this a bit later - in-process reset
18966 			 * condition should be already set.
18967 			 * Track retry time for device identification.
18968 			 */
18969 			if ((pmportinfo->pmport_dev_type &
18970 			    SATA_VALID_DEV_TYPE) != 0 &&
18971 			    SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL &&
18972 			    sdinfo->satadrv_reset_time != 0) {
18973 				clock_t cur_time = ddi_get_lbolt();
18974 				/*
18975 				 * If the retry time limit was not
18976 				 * exceeded, retry.
18977 				 */
18978 				if ((cur_time - sdinfo->satadrv_reset_time) <
18979 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
18980 					mutex_enter(
18981 					    &sata_hba_inst->satahba_mutex);
18982 					sata_hba_inst->satahba_event_flags |=
18983 					    SATA_EVNT_MAIN;
18984 					mutex_exit(
18985 					    &sata_hba_inst->satahba_mutex);
18986 					mutex_enter(&sata_mutex);
18987 					sata_event_pending |= SATA_EVNT_MAIN;
18988 					mutex_exit(&sata_mutex);
18989 					return;
18990 				}
18991 			}
18992 			/* Fail the drive */
18993 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
18994 
18995 			sata_log(sata_hba_inst, CE_WARN,
18996 			    "SATA device at port %d:%d - device failed",
18997 			    saddr->cport, saddr->pmport);
18998 		} else {
18999 			/*
19000 			 * No point of retrying - some other event processing
19001 			 * would or already did port info cleanup.
19002 			 * To be safe (HBA may need it),
19003 			 * request clearing device reset condition.
19004 			 */
19005 			sdinfo->satadrv_event_flags |=
19006 			    SATA_EVNT_CLEAR_DEVICE_RESET;
19007 		}
19008 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
19009 		sdinfo->satadrv_reset_time = 0;
19010 		return;
19011 	}
19012 	/*
19013 	 * Raise the flag indicating that the next sata command could
19014 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
19015 	 * reset is reported.
19016 	 */
19017 	mutex_enter(&pmportinfo->pmport_mutex);
19018 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
19019 		sdinfo->satadrv_reset_time = 0;
19020 		if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) {
19021 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19022 			sdinfo->satadrv_event_flags &=
19023 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
19024 			/* must clear flags on cport */
19025 			pminfo = SATA_PMULT_INFO(sata_hba_inst,
19026 			    saddr->cport);
19027 			pminfo->pmult_event_flags |=
19028 			    SATA_EVNT_CLEAR_DEVICE_RESET;
19029 		}
19030 	}
19031 }
19032 
19033 /*
19034  * Port Link Events processing.
19035  * Every link established event may involve device reset (due to
19036  * COMRESET signal, equivalent of the hard reset) so arbitrarily
19037  * set device reset event for an attached device (if any).
19038  * If the port is in SHUTDOWN or FAILED state, ignore link events.
19039  *
19040  * The link established event processing varies, depending on the state
19041  * of the target node, HBA hotplugging capabilities, state of the port.
19042  * If the link is not active, the link established event is ignored.
19043  * If HBA cannot detect device attachment and there is no target node,
19044  * the link established event triggers device attach event processing.
19045  * Else, link established event triggers device reset event processing.
19046  *
19047  * The link lost event processing varies, depending on a HBA hotplugging
19048  * capability and the state of the port (link active or not active).
19049  * If the link is active, the lost link event is ignored.
19050  * If HBA cannot detect device removal, the lost link event triggers
19051  * device detached event processing after link lost timeout.
19052  * Else, the event is ignored.
19053  *
19054  * NOTE: Port multiplier ports events are handled by
19055  * sata_process_pmport_link_events();
19056  */
19057 static void
19058 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst,
19059     sata_address_t *saddr)
19060 {
19061 	sata_device_t sata_device;
19062 	sata_cport_info_t *cportinfo;
19063 	sata_drive_info_t *sdinfo;
19064 	uint32_t event_flags;
19065 	int rval;
19066 
19067 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19068 	    "Processing port %d link event(s)", saddr->cport);
19069 
19070 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19071 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19072 	event_flags = cportinfo->cport_event_flags;
19073 
19074 	/* Reset event flags first */
19075 	cportinfo->cport_event_flags &=
19076 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
19077 
19078 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
19079 	if ((cportinfo->cport_state &
19080 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19081 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19082 		    cport_mutex);
19083 		return;
19084 	}
19085 
19086 	/*
19087 	 * For the sanity sake get current port state.
19088 	 * Set device address only. Other sata_device fields should be
19089 	 * set by HBA driver.
19090 	 */
19091 	sata_device.satadev_rev = SATA_DEVICE_REV;
19092 	sata_device.satadev_addr = *saddr;
19093 	/*
19094 	 * We have to exit mutex, because the HBA probe port function may
19095 	 * block on its own mutex.
19096 	 */
19097 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19098 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19099 	    (SATA_DIP(sata_hba_inst), &sata_device);
19100 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19101 	sata_update_port_info(sata_hba_inst, &sata_device);
19102 	if (rval != SATA_SUCCESS) {
19103 		/* Something went wrong? Fail the port */
19104 		cportinfo->cport_state = SATA_PSTATE_FAILED;
19105 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19106 		    cport_mutex);
19107 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19108 		    "SATA port %d probing failed",
19109 		    saddr->cport));
19110 		/*
19111 		 * We may want to release device info structure, but
19112 		 * it is not necessary.
19113 		 */
19114 		return;
19115 	} else {
19116 		/* port probed successfully */
19117 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19118 	}
19119 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
19120 
19121 		if ((sata_device.satadev_scr.sstatus &
19122 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
19123 			/* Ignore event */
19124 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19125 			    "Ignoring port %d link established event - "
19126 			    "link down",
19127 			    saddr->cport);
19128 			goto linklost;
19129 		}
19130 
19131 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19132 		    "Processing port %d link established event",
19133 		    saddr->cport);
19134 
19135 		/*
19136 		 * For the sanity sake check if a device is attached - check
19137 		 * return state of a port probing.
19138 		 */
19139 		if (sata_device.satadev_type != SATA_DTYPE_NONE) {
19140 			/*
19141 			 * HBA port probe indicated that there is a device
19142 			 * attached. Check if the framework had device info
19143 			 * structure attached for this device.
19144 			 */
19145 			if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
19146 				ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) !=
19147 				    NULL);
19148 
19149 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19150 				if ((sdinfo->satadrv_type &
19151 				    SATA_VALID_DEV_TYPE) != 0) {
19152 					/*
19153 					 * Dev info structure is present.
19154 					 * If dev_type is set to known type in
19155 					 * the framework's drive info struct
19156 					 * then the device existed before and
19157 					 * the link was probably lost
19158 					 * momentarily - in such case
19159 					 * we may want to check device
19160 					 * identity.
19161 					 * Identity check is not supported now.
19162 					 *
19163 					 * Link established event
19164 					 * triggers device reset event.
19165 					 */
19166 					(SATA_CPORTINFO_DRV_INFO(cportinfo))->
19167 					    satadrv_event_flags |=
19168 					    SATA_EVNT_DEVICE_RESET;
19169 				}
19170 			} else if (cportinfo->cport_dev_type ==
19171 			    SATA_DTYPE_NONE) {
19172 				/*
19173 				 * We got new device attached! If HBA does not
19174 				 * generate device attached events, trigger it
19175 				 * here.
19176 				 */
19177 				if (!(SATA_FEATURES(sata_hba_inst) &
19178 				    SATA_CTLF_HOTPLUG)) {
19179 					cportinfo->cport_event_flags |=
19180 					    SATA_EVNT_DEVICE_ATTACHED;
19181 				}
19182 			}
19183 			/* Reset link lost timeout */
19184 			cportinfo->cport_link_lost_time = 0;
19185 		}
19186 	}
19187 linklost:
19188 	if (event_flags & SATA_EVNT_LINK_LOST) {
19189 		if ((sata_device.satadev_scr.sstatus &
19190 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
19191 			/* Ignore event */
19192 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19193 			    "Ignoring port %d link lost event - link is up",
19194 			    saddr->cport);
19195 			goto done;
19196 		}
19197 #ifdef SATA_DEBUG
19198 		if (cportinfo->cport_link_lost_time == 0) {
19199 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19200 			    "Processing port %d link lost event",
19201 			    saddr->cport);
19202 		}
19203 #endif
19204 		/*
19205 		 * When HBA cannot generate device attached/detached events,
19206 		 * we need to track link lost time and eventually generate
19207 		 * device detach event.
19208 		 */
19209 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
19210 			/* We are tracking link lost time */
19211 			if (cportinfo->cport_link_lost_time == 0) {
19212 				/* save current time (lbolt value) */
19213 				cportinfo->cport_link_lost_time =
19214 				    ddi_get_lbolt();
19215 				/* just keep link lost event */
19216 				cportinfo->cport_event_flags |=
19217 				    SATA_EVNT_LINK_LOST;
19218 			} else {
19219 				clock_t cur_time = ddi_get_lbolt();
19220 				if ((cur_time -
19221 				    cportinfo->cport_link_lost_time) >=
19222 				    drv_usectohz(
19223 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
19224 					/* trigger device detach event */
19225 					cportinfo->cport_event_flags |=
19226 					    SATA_EVNT_DEVICE_DETACHED;
19227 					cportinfo->cport_link_lost_time = 0;
19228 					SATADBG1(SATA_DBG_EVENTS,
19229 					    sata_hba_inst,
19230 					    "Triggering port %d "
19231 					    "device detached event",
19232 					    saddr->cport);
19233 				} else {
19234 					/* keep link lost event */
19235 					cportinfo->cport_event_flags |=
19236 					    SATA_EVNT_LINK_LOST;
19237 				}
19238 			}
19239 		}
19240 		/*
19241 		 * We could change port state to disable/delay access to
19242 		 * the attached device until the link is recovered.
19243 		 */
19244 	}
19245 done:
19246 	event_flags = cportinfo->cport_event_flags;
19247 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19248 	if (event_flags != 0) {
19249 		mutex_enter(&sata_hba_inst->satahba_mutex);
19250 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19251 		mutex_exit(&sata_hba_inst->satahba_mutex);
19252 		mutex_enter(&sata_mutex);
19253 		sata_event_pending |= SATA_EVNT_MAIN;
19254 		mutex_exit(&sata_mutex);
19255 	}
19256 }
19257 
19258 /*
19259  * Port Multiplier Port Link Events processing.
19260  */
19261 static void
19262 sata_process_pmport_link_events(sata_hba_inst_t *sata_hba_inst,
19263     sata_address_t *saddr)
19264 {
19265 	sata_device_t sata_device;
19266 	sata_pmport_info_t *pmportinfo = NULL;
19267 	sata_drive_info_t *sdinfo = NULL;
19268 	uint32_t event_flags;
19269 	uint8_t cport = saddr->cport;
19270 	uint8_t pmport = saddr->pmport;
19271 	int rval;
19272 
19273 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19274 	    "Processing port %d:%d link event(s)",
19275 	    cport, pmport);
19276 
19277 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
19278 	mutex_enter(&pmportinfo->pmport_mutex);
19279 	event_flags = pmportinfo->pmport_event_flags;
19280 
19281 	/* Reset event flags first */
19282 	pmportinfo->pmport_event_flags &=
19283 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
19284 
19285 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
19286 	if ((pmportinfo->pmport_state &
19287 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19288 		mutex_exit(&pmportinfo->pmport_mutex);
19289 		return;
19290 	}
19291 
19292 	/*
19293 	 * For the sanity sake get current port state.
19294 	 * Set device address only. Other sata_device fields should be
19295 	 * set by HBA driver.
19296 	 */
19297 	sata_device.satadev_rev = SATA_DEVICE_REV;
19298 	sata_device.satadev_addr = *saddr;
19299 	/*
19300 	 * We have to exit mutex, because the HBA probe port function may
19301 	 * block on its own mutex.
19302 	 */
19303 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19304 	    saddr->pmport));
19305 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19306 	    (SATA_DIP(sata_hba_inst), &sata_device);
19307 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19308 	    saddr->pmport));
19309 	sata_update_pmport_info(sata_hba_inst, &sata_device);
19310 	if (rval != SATA_SUCCESS) {
19311 		/* Something went wrong? Fail the port */
19312 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
19313 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19314 		    saddr->pmport));
19315 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19316 		    "SATA port %d:%d probing failed",
19317 		    saddr->cport, saddr->pmport));
19318 		/*
19319 		 * We may want to release device info structure, but
19320 		 * it is not necessary.
19321 		 */
19322 		return;
19323 	} else {
19324 		/* port probed successfully */
19325 		pmportinfo->pmport_state |=
19326 		    SATA_STATE_PROBED | SATA_STATE_READY;
19327 	}
19328 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst,
19329 	    saddr->cport, saddr->pmport));
19330 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst,
19331 	    saddr->cport, saddr->pmport));
19332 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
19333 
19334 		if ((sata_device.satadev_scr.sstatus &
19335 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
19336 			/* Ignore event */
19337 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19338 			    "Ignoring port %d:%d link established event - "
19339 			    "link down",
19340 			    saddr->cport, saddr->pmport);
19341 			goto linklost;
19342 		}
19343 
19344 		SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19345 		    "Processing port %d:%d link established event",
19346 		    cport, pmport);
19347 
19348 		/*
19349 		 * For the sanity sake check if a device is attached - check
19350 		 * return state of a port probing.
19351 		 */
19352 		if (sata_device.satadev_type != SATA_DTYPE_NONE &&
19353 		    sata_device.satadev_type != SATA_DTYPE_PMULT) {
19354 			/*
19355 			 * HBA port probe indicated that there is a device
19356 			 * attached. Check if the framework had device info
19357 			 * structure attached for this device.
19358 			 */
19359 			if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
19360 				ASSERT(SATA_PMPORTINFO_DRV_INFO(pmportinfo) !=
19361 				    NULL);
19362 
19363 				sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19364 				if ((sdinfo->satadrv_type &
19365 				    SATA_VALID_DEV_TYPE) != 0) {
19366 					/*
19367 					 * Dev info structure is present.
19368 					 * If dev_type is set to known type in
19369 					 * the framework's drive info struct
19370 					 * then the device existed before and
19371 					 * the link was probably lost
19372 					 * momentarily - in such case
19373 					 * we may want to check device
19374 					 * identity.
19375 					 * Identity check is not supported now.
19376 					 *
19377 					 * Link established event
19378 					 * triggers device reset event.
19379 					 */
19380 					(SATA_PMPORTINFO_DRV_INFO(pmportinfo))->
19381 					    satadrv_event_flags |=
19382 					    SATA_EVNT_DEVICE_RESET;
19383 				}
19384 			} else if (pmportinfo->pmport_dev_type ==
19385 			    SATA_DTYPE_NONE) {
19386 				/*
19387 				 * We got new device attached! If HBA does not
19388 				 * generate device attached events, trigger it
19389 				 * here.
19390 				 */
19391 				if (!(SATA_FEATURES(sata_hba_inst) &
19392 				    SATA_CTLF_HOTPLUG)) {
19393 					pmportinfo->pmport_event_flags |=
19394 					    SATA_EVNT_DEVICE_ATTACHED;
19395 				}
19396 			}
19397 			/* Reset link lost timeout */
19398 			pmportinfo->pmport_link_lost_time = 0;
19399 		}
19400 	}
19401 linklost:
19402 	if (event_flags & SATA_EVNT_LINK_LOST) {
19403 #ifdef SATA_DEBUG
19404 		if (pmportinfo->pmport_link_lost_time == 0) {
19405 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19406 			    "Processing port %d:%d link lost event",
19407 			    saddr->cport, saddr->pmport);
19408 		}
19409 #endif
19410 		if ((sata_device.satadev_scr.sstatus &
19411 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
19412 			/* Ignore event */
19413 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19414 			    "Ignoring port %d:%d link lost event - link is up",
19415 			    saddr->cport, saddr->pmport);
19416 			goto done;
19417 		}
19418 		/*
19419 		 * When HBA cannot generate device attached/detached events,
19420 		 * we need to track link lost time and eventually generate
19421 		 * device detach event.
19422 		 */
19423 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
19424 			/* We are tracking link lost time */
19425 			if (pmportinfo->pmport_link_lost_time == 0) {
19426 				/* save current time (lbolt value) */
19427 				pmportinfo->pmport_link_lost_time =
19428 				    ddi_get_lbolt();
19429 				/* just keep link lost event */
19430 				pmportinfo->pmport_event_flags |=
19431 				    SATA_EVNT_LINK_LOST;
19432 			} else {
19433 				clock_t cur_time = ddi_get_lbolt();
19434 				if ((cur_time -
19435 				    pmportinfo->pmport_link_lost_time) >=
19436 				    drv_usectohz(
19437 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
19438 					/* trigger device detach event */
19439 					pmportinfo->pmport_event_flags |=
19440 					    SATA_EVNT_DEVICE_DETACHED;
19441 					pmportinfo->pmport_link_lost_time = 0;
19442 					SATADBG2(SATA_DBG_EVENTS,
19443 					    sata_hba_inst,
19444 					    "Triggering port %d:%d "
19445 					    "device detached event",
19446 					    saddr->cport, saddr->pmport);
19447 				} else {
19448 					/* keep link lost event */
19449 					pmportinfo->pmport_event_flags |=
19450 					    SATA_EVNT_LINK_LOST;
19451 				}
19452 			}
19453 		}
19454 		/*
19455 		 * We could change port state to disable/delay access to
19456 		 * the attached device until the link is recovered.
19457 		 */
19458 	}
19459 done:
19460 	event_flags = pmportinfo->pmport_event_flags;
19461 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19462 	    saddr->pmport));
19463 	if (event_flags != 0) {
19464 		mutex_enter(&sata_hba_inst->satahba_mutex);
19465 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19466 		mutex_exit(&sata_hba_inst->satahba_mutex);
19467 		mutex_enter(&sata_mutex);
19468 		sata_event_pending |= SATA_EVNT_MAIN;
19469 		mutex_exit(&sata_mutex);
19470 	}
19471 }
19472 
19473 /*
19474  * Device Detached Event processing.
19475  * Port is probed to find if a device is really gone. If so,
19476  * the device info structure is detached from the SATA port info structure
19477  * and released.
19478  * Port status is updated.
19479  *
19480  * NOTE: Port multiplier ports events are handled by
19481  * sata_process_pmdevice_detached()
19482  */
19483 static void
19484 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst,
19485     sata_address_t *saddr)
19486 {
19487 	sata_cport_info_t *cportinfo;
19488 	sata_pmport_info_t *pmportinfo;
19489 	sata_drive_info_t *sdevinfo;
19490 	sata_device_t sata_device;
19491 	sata_address_t pmport_addr;
19492 	char name[16];
19493 	uint8_t cport = saddr->cport;
19494 	int npmport;
19495 	int rval;
19496 
19497 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19498 	    "Processing port %d device detached", saddr->cport);
19499 
19500 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19501 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19502 	/* Clear event flag */
19503 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
19504 
19505 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
19506 	if ((cportinfo->cport_state &
19507 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19508 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19509 		    cport_mutex);
19510 		return;
19511 	}
19512 	/* For sanity, re-probe the port */
19513 	sata_device.satadev_rev = SATA_DEVICE_REV;
19514 	sata_device.satadev_addr = *saddr;
19515 
19516 	/*
19517 	 * We have to exit mutex, because the HBA probe port function may
19518 	 * block on its own mutex.
19519 	 */
19520 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19521 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19522 	    (SATA_DIP(sata_hba_inst), &sata_device);
19523 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19524 	sata_update_port_info(sata_hba_inst, &sata_device);
19525 	if (rval != SATA_SUCCESS) {
19526 		/* Something went wrong? Fail the port */
19527 		cportinfo->cport_state = SATA_PSTATE_FAILED;
19528 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19529 		    cport_mutex);
19530 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19531 		    "SATA port %d probing failed",
19532 		    saddr->cport));
19533 		/*
19534 		 * We may want to release device info structure, but
19535 		 * it is not necessary.
19536 		 */
19537 		return;
19538 	} else {
19539 		/* port probed successfully */
19540 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19541 	}
19542 	/*
19543 	 * Check if a device is still attached. For sanity, check also
19544 	 * link status - if no link, there is no device.
19545 	 */
19546 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
19547 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
19548 	    SATA_DTYPE_NONE) {
19549 		/*
19550 		 * Device is still attached - ignore detach event.
19551 		 */
19552 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19553 		    cport_mutex);
19554 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19555 		    "Ignoring detach - device still attached to port %d",
19556 		    sata_device.satadev_addr.cport);
19557 		return;
19558 	}
19559 	/*
19560 	 * We need to detach and release device info structure here
19561 	 */
19562 	if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
19563 		/*
19564 		 * A port-multiplier is removed.
19565 		 *
19566 		 * Calling sata_process_pmdevice_detached() does not work
19567 		 * here. The port multiplier is gone, so we cannot probe
19568 		 * sub-port any more and all pmult-related data structure must
19569 		 * be de-allocated immediately. Following structure of every
19570 		 * implemented sub-port behind the pmult are required to
19571 		 * released.
19572 		 *
19573 		 *   - attachment point
19574 		 *   - target node
19575 		 *   - sata_drive_info
19576 		 *   - sata_pmport_info
19577 		 */
19578 		for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst,
19579 		    cport); npmport ++) {
19580 			SATADBG2(SATA_DBG_PMULT|SATA_DBG_EVENTS_PROC,
19581 			    sata_hba_inst,
19582 			    "Detaching target node at port %d:%d",
19583 			    cport, npmport);
19584 
19585 			mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
19586 
19587 			/* Remove attachment point. */
19588 			name[0] = '\0';
19589 			(void) sprintf(name, "%d.%d", cport, npmport);
19590 			ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name);
19591 			sata_log(sata_hba_inst, CE_NOTE,
19592 			    "Remove attachment point of port %d:%d",
19593 			    cport, npmport);
19594 
19595 			/* Remove target node */
19596 			pmport_addr.cport = cport;
19597 			pmport_addr.pmport = (uint8_t)npmport;
19598 			pmport_addr.qual = SATA_ADDR_PMPORT;
19599 			sata_remove_target_node(sata_hba_inst, &pmport_addr);
19600 
19601 			mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
19602 
19603 			/* Release sata_pmport_info & sata_drive_info. */
19604 			pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
19605 			    cport, npmport);
19606 			ASSERT(pmportinfo != NULL);
19607 
19608 			sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19609 			if (sdevinfo != NULL) {
19610 				(void) kmem_free((void *) sdevinfo,
19611 				    sizeof (sata_drive_info_t));
19612 			}
19613 
19614 			/* Release sata_pmport_info at last */
19615 			(void) kmem_free((void *) pmportinfo,
19616 			    sizeof (sata_pmport_info_t));
19617 		}
19618 
19619 		/* Finally, release sata_pmult_info */
19620 		(void) kmem_free((void *)
19621 		    SATA_CPORTINFO_PMULT_INFO(cportinfo),
19622 		    sizeof (sata_pmult_info_t));
19623 		SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL;
19624 
19625 		sata_log(sata_hba_inst, CE_WARN,
19626 		    "SATA port-multiplier detached at port %d", cport);
19627 
19628 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19629 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19630 		    saddr->cport)->cport_mutex);
19631 	} else {
19632 		if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19633 			sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19634 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
19635 			(void) kmem_free((void *)sdevinfo,
19636 			    sizeof (sata_drive_info_t));
19637 		}
19638 		sata_log(sata_hba_inst, CE_WARN,
19639 		    "SATA device detached at port %d", cport);
19640 
19641 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19642 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19643 		    saddr->cport)->cport_mutex);
19644 
19645 		/*
19646 		 * Try to offline a device and remove target node
19647 		 * if it still exists
19648 		 */
19649 		sata_remove_target_node(sata_hba_inst, saddr);
19650 	}
19651 
19652 
19653 	/*
19654 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19655 	 * with the hint: SE_HINT_REMOVE
19656 	 */
19657 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
19658 }
19659 
19660 /*
19661  * Port Multiplier Port Device Deattached Event processing.
19662  *
19663  * NOTE: No Mutex should be hold.
19664  */
19665 static void
19666 sata_process_pmdevice_detached(sata_hba_inst_t *sata_hba_inst,
19667     sata_address_t *saddr)
19668 {
19669 	sata_pmport_info_t *pmportinfo;
19670 	sata_drive_info_t *sdevinfo;
19671 	sata_device_t sata_device;
19672 	int rval;
19673 	uint8_t cport, pmport;
19674 
19675 	cport = saddr->cport;
19676 	pmport = saddr->pmport;
19677 
19678 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19679 	    "Processing port %d:%d device detached",
19680 	    cport, pmport);
19681 
19682 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
19683 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19684 
19685 	/* Clear event flag */
19686 	pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
19687 
19688 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
19689 	if ((pmportinfo->pmport_state &
19690 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19691 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19692 		return;
19693 	}
19694 	/* For sanity, re-probe the port */
19695 	sata_device.satadev_rev = SATA_DEVICE_REV;
19696 	sata_device.satadev_addr = *saddr;
19697 
19698 	/*
19699 	 * We have to exit mutex, because the HBA probe port function may
19700 	 * block on its own mutex.
19701 	 */
19702 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19703 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19704 	    (SATA_DIP(sata_hba_inst), &sata_device);
19705 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19706 	sata_update_pmport_info(sata_hba_inst, &sata_device);
19707 	if (rval != SATA_SUCCESS) {
19708 		/* Something went wrong? Fail the port */
19709 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
19710 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19711 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19712 		    "SATA port %d:%d probing failed",
19713 		    saddr->pmport));
19714 		/*
19715 		 * We may want to release device info structure, but
19716 		 * it is not necessary.
19717 		 */
19718 		return;
19719 	} else {
19720 		/* port probed successfully */
19721 		pmportinfo->pmport_state |=
19722 		    SATA_STATE_PROBED | SATA_STATE_READY;
19723 	}
19724 	/*
19725 	 * Check if a device is still attached. For sanity, check also
19726 	 * link status - if no link, there is no device.
19727 	 */
19728 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
19729 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
19730 	    SATA_DTYPE_NONE) {
19731 		/*
19732 		 * Device is still attached - ignore detach event.
19733 		 */
19734 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19735 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19736 		    "Ignoring detach - device still attached to port %d",
19737 		    sata_device.satadev_addr.pmport);
19738 		return;
19739 	}
19740 	/*
19741 	 * We need to detach and release device info structure here
19742 	 */
19743 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
19744 		sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19745 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
19746 		(void) kmem_free((void *)sdevinfo,
19747 		    sizeof (sata_drive_info_t));
19748 	}
19749 	pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
19750 	/*
19751 	 * Device cannot be reached anymore, even if the target node may be
19752 	 * still present.
19753 	 */
19754 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19755 
19756 	/*
19757 	 * Try to offline a device and remove target node if it still exists
19758 	 */
19759 	sata_remove_target_node(sata_hba_inst, saddr);
19760 
19761 	/*
19762 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19763 	 * with the hint: SE_HINT_REMOVE
19764 	 */
19765 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
19766 }
19767 
19768 
19769 /*
19770  * Device Attached Event processing.
19771  * Port state is checked to verify that a device is really attached. If so,
19772  * the device info structure is created and attached to the SATA port info
19773  * structure.
19774  *
19775  * If attached device cannot be identified or set-up, the retry for the
19776  * attach processing is set-up. Subsequent daemon run would try again to
19777  * identify the device, until the time limit is reached
19778  * (SATA_DEV_IDENTIFY_TIMEOUT).
19779  *
19780  * This function cannot be called in interrupt context (it may sleep).
19781  *
19782  * NOTE: Port multiplier ports events are handled by
19783  * sata_process_pmdevice_attached()
19784  */
19785 static void
19786 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst,
19787     sata_address_t *saddr)
19788 {
19789 	sata_cport_info_t *cportinfo = NULL;
19790 	sata_drive_info_t *sdevinfo = NULL;
19791 	sata_pmult_info_t *pmultinfo = NULL;
19792 	sata_pmport_info_t *pmportinfo = NULL;
19793 	sata_device_t sata_device;
19794 	dev_info_t *tdip;
19795 	uint32_t event_flags = 0, pmult_event_flags = 0;
19796 	int rval;
19797 	int npmport;
19798 
19799 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19800 	    "Processing port %d device attached", saddr->cport);
19801 
19802 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19803 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19804 
19805 	/* Clear attach event flag first */
19806 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
19807 
19808 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
19809 	if ((cportinfo->cport_state &
19810 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19811 		cportinfo->cport_dev_attach_time = 0;
19812 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19813 		    cport_mutex);
19814 		return;
19815 	}
19816 
19817 	/*
19818 	 * If the sata_drive_info structure is found attached to the port info,
19819 	 * despite the fact the device was removed and now it is re-attached,
19820 	 * the old drive info structure was not removed.
19821 	 * Arbitrarily release device info structure.
19822 	 */
19823 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19824 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19825 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
19826 		(void) kmem_free((void *)sdevinfo,
19827 		    sizeof (sata_drive_info_t));
19828 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19829 		    "Arbitrarily detaching old device info.", NULL);
19830 	}
19831 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19832 
19833 	/* For sanity, re-probe the port */
19834 	sata_device.satadev_rev = SATA_DEVICE_REV;
19835 	sata_device.satadev_addr = *saddr;
19836 
19837 	/*
19838 	 * We have to exit mutex, because the HBA probe port function may
19839 	 * block on its own mutex.
19840 	 */
19841 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19842 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19843 	    (SATA_DIP(sata_hba_inst), &sata_device);
19844 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19845 	sata_update_port_info(sata_hba_inst, &sata_device);
19846 	if (rval != SATA_SUCCESS) {
19847 		/* Something went wrong? Fail the port */
19848 		cportinfo->cport_state = SATA_PSTATE_FAILED;
19849 		cportinfo->cport_dev_attach_time = 0;
19850 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19851 		    cport_mutex);
19852 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19853 		    "SATA port %d probing failed",
19854 		    saddr->cport));
19855 		return;
19856 	} else {
19857 		/* port probed successfully */
19858 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19859 	}
19860 	/*
19861 	 * Check if a device is still attached. For sanity, check also
19862 	 * link status - if no link, there is no device.
19863 	 */
19864 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
19865 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
19866 	    SATA_DTYPE_NONE) {
19867 		/*
19868 		 * No device - ignore attach event.
19869 		 */
19870 		cportinfo->cport_dev_attach_time = 0;
19871 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19872 		    cport_mutex);
19873 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19874 		    "Ignoring attach - no device connected to port %d",
19875 		    sata_device.satadev_addr.cport);
19876 		return;
19877 	}
19878 
19879 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19880 	/*
19881 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19882 	 * with the hint: SE_HINT_INSERT
19883 	 */
19884 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
19885 
19886 	/*
19887 	 * Port reprobing will take care of the creation of the device
19888 	 * info structure and determination of the device type.
19889 	 */
19890 	sata_device.satadev_addr = *saddr;
19891 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
19892 	    SATA_DEV_IDENTIFY_NORETRY);
19893 
19894 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19895 	    cport_mutex);
19896 	if ((cportinfo->cport_state & SATA_STATE_READY) &&
19897 	    (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) {
19898 		/* Some device is attached to the port */
19899 		if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) {
19900 			/*
19901 			 * A device was not successfully attached.
19902 			 * Track retry time for device identification.
19903 			 */
19904 			if (cportinfo->cport_dev_attach_time != 0) {
19905 				clock_t cur_time = ddi_get_lbolt();
19906 				/*
19907 				 * If the retry time limit was not exceeded,
19908 				 * reinstate attach event.
19909 				 */
19910 				if ((cur_time -
19911 				    cportinfo->cport_dev_attach_time) <
19912 				    drv_usectohz(
19913 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
19914 					/* OK, restore attach event */
19915 					cportinfo->cport_event_flags |=
19916 					    SATA_EVNT_DEVICE_ATTACHED;
19917 				} else {
19918 					/* Timeout - cannot identify device */
19919 					cportinfo->cport_dev_attach_time = 0;
19920 					sata_log(sata_hba_inst,
19921 					    CE_WARN,
19922 					    "Could not identify SATA device "
19923 					    "at port %d",
19924 					    saddr->cport);
19925 				}
19926 			} else {
19927 				/*
19928 				 * Start tracking time for device
19929 				 * identification.
19930 				 * Save current time (lbolt value).
19931 				 */
19932 				cportinfo->cport_dev_attach_time =
19933 				    ddi_get_lbolt();
19934 				/* Restore attach event */
19935 				cportinfo->cport_event_flags |=
19936 				    SATA_EVNT_DEVICE_ATTACHED;
19937 			}
19938 		} else if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
19939 			cportinfo->cport_dev_attach_time = 0;
19940 			sata_log(sata_hba_inst, CE_NOTE,
19941 			    "SATA port-multiplier detected at port %d",
19942 			    saddr->cport);
19943 
19944 			if (SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL) {
19945 				/* Log the info of new port multiplier */
19946 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19947 				    saddr->cport)->cport_mutex);
19948 				sata_show_pmult_info(sata_hba_inst,
19949 				    &sata_device);
19950 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19951 				    saddr->cport)->cport_mutex);
19952 			}
19953 
19954 			ASSERT(SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL);
19955 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
19956 			for (npmport = 0; npmport <
19957 			    pmultinfo->pmult_num_dev_ports; npmport++) {
19958 				pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
19959 				    saddr->cport, npmport);
19960 				ASSERT(pmportinfo != NULL);
19961 
19962 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19963 				    saddr->cport)->cport_mutex);
19964 				mutex_enter(&pmportinfo->pmport_mutex);
19965 				/* Marked all pmports with link events. */
19966 				pmportinfo->pmport_event_flags =
19967 				    SATA_EVNT_LINK_ESTABLISHED;
19968 				pmult_event_flags |=
19969 				    pmportinfo->pmport_event_flags;
19970 				mutex_exit(&pmportinfo->pmport_mutex);
19971 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19972 				    saddr->cport)->cport_mutex);
19973 			}
19974 			/* Auto-online is not available for PMult now. */
19975 
19976 		} else {
19977 			/*
19978 			 * If device was successfully attached, the subsequent
19979 			 * action depends on a state of the
19980 			 * sata_auto_online variable. If it is set to zero.
19981 			 * an explicit 'configure' command will be needed to
19982 			 * configure it. If its value is non-zero, we will
19983 			 * attempt to online (configure) the device.
19984 			 * First, log the message indicating that a device
19985 			 * was attached.
19986 			 */
19987 			cportinfo->cport_dev_attach_time = 0;
19988 			sata_log(sata_hba_inst, CE_WARN,
19989 			    "SATA device detected at port %d", saddr->cport);
19990 
19991 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19992 				sata_drive_info_t new_sdinfo;
19993 
19994 				/* Log device info data */
19995 				new_sdinfo = *(SATA_CPORTINFO_DRV_INFO(
19996 				    cportinfo));
19997 				sata_show_drive_info(sata_hba_inst,
19998 				    &new_sdinfo);
19999 			}
20000 
20001 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20002 			    saddr->cport)->cport_mutex);
20003 
20004 			/*
20005 			 * Make sure that there is no target node for that
20006 			 * device. If so, release it. It should not happen,
20007 			 * unless we had problem removing the node when
20008 			 * device was detached.
20009 			 */
20010 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
20011 			    saddr->cport, saddr->pmport);
20012 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20013 			    saddr->cport)->cport_mutex);
20014 			if (tdip != NULL) {
20015 
20016 #ifdef SATA_DEBUG
20017 				if ((cportinfo->cport_event_flags &
20018 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
20019 					sata_log(sata_hba_inst, CE_WARN,
20020 					    "sata_process_device_attached: "
20021 					    "old device target node exists!");
20022 #endif
20023 				/*
20024 				 * target node exists - try to unconfigure
20025 				 * device and remove the node.
20026 				 */
20027 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20028 				    saddr->cport)->cport_mutex);
20029 				rval = ndi_devi_offline(tdip,
20030 				    NDI_DEVI_REMOVE);
20031 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20032 				    saddr->cport)->cport_mutex);
20033 
20034 				if (rval == NDI_SUCCESS) {
20035 					cportinfo->cport_event_flags &=
20036 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20037 					cportinfo->cport_tgtnode_clean = B_TRUE;
20038 				} else {
20039 					/*
20040 					 * PROBLEM - the target node remained
20041 					 * and it belongs to a previously
20042 					 * attached device.
20043 					 * This happens when the file was open
20044 					 * or the node was waiting for
20045 					 * resources at the time the
20046 					 * associated device was removed.
20047 					 * Instruct event daemon to retry the
20048 					 * cleanup later.
20049 					 */
20050 					sata_log(sata_hba_inst,
20051 					    CE_WARN,
20052 					    "Application(s) accessing "
20053 					    "previously attached SATA "
20054 					    "device have to release "
20055 					    "it before newly inserted "
20056 					    "device can be made accessible.",
20057 					    saddr->cport);
20058 					cportinfo->cport_event_flags |=
20059 					    SATA_EVNT_TARGET_NODE_CLEANUP;
20060 					cportinfo->cport_tgtnode_clean =
20061 					    B_FALSE;
20062 				}
20063 			}
20064 			if (sata_auto_online != 0) {
20065 				cportinfo->cport_event_flags |=
20066 				    SATA_EVNT_AUTOONLINE_DEVICE;
20067 			}
20068 
20069 		}
20070 	} else {
20071 		cportinfo->cport_dev_attach_time = 0;
20072 	}
20073 
20074 	event_flags = cportinfo->cport_event_flags;
20075 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20076 	if (event_flags != 0 || pmult_event_flags != 0) {
20077 		mutex_enter(&sata_hba_inst->satahba_mutex);
20078 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20079 		mutex_exit(&sata_hba_inst->satahba_mutex);
20080 		mutex_enter(&sata_mutex);
20081 		sata_event_pending |= SATA_EVNT_MAIN;
20082 		mutex_exit(&sata_mutex);
20083 	}
20084 }
20085 
20086 /*
20087  * Port Multiplier Port Device Attached Event processing.
20088  *
20089  * NOTE: No Mutex should be hold.
20090  */
20091 static void
20092 sata_process_pmdevice_attached(sata_hba_inst_t *sata_hba_inst,
20093     sata_address_t *saddr)
20094 {
20095 	sata_pmport_info_t *pmportinfo;
20096 	sata_drive_info_t *sdinfo;
20097 	sata_device_t sata_device;
20098 	dev_info_t *tdip;
20099 	uint32_t event_flags;
20100 	uint8_t cport = saddr->cport;
20101 	uint8_t pmport = saddr->pmport;
20102 	int rval;
20103 
20104 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20105 	    "Processing port %d:%d device attached", cport, pmport);
20106 
20107 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
20108 
20109 	mutex_enter(&pmportinfo->pmport_mutex);
20110 
20111 	/* Clear attach event flag first */
20112 	pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
20113 
20114 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
20115 	if ((pmportinfo->pmport_state &
20116 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
20117 		pmportinfo->pmport_dev_attach_time = 0;
20118 		mutex_exit(&pmportinfo->pmport_mutex);
20119 		return;
20120 	}
20121 
20122 	/*
20123 	 * If the sata_drive_info structure is found attached to the port info,
20124 	 * despite the fact the device was removed and now it is re-attached,
20125 	 * the old drive info structure was not removed.
20126 	 * Arbitrarily release device info structure.
20127 	 */
20128 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20129 		sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
20130 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
20131 		(void) kmem_free((void *)sdinfo,
20132 		    sizeof (sata_drive_info_t));
20133 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20134 		    "Arbitrarily detaching old device info.", NULL);
20135 	}
20136 	pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
20137 
20138 	/* For sanity, re-probe the port */
20139 	sata_device.satadev_rev = SATA_DEVICE_REV;
20140 	sata_device.satadev_addr = *saddr;
20141 
20142 	/*
20143 	 * We have to exit mutex, because the HBA probe port function may
20144 	 * block on its own mutex.
20145 	 */
20146 	mutex_exit(&pmportinfo->pmport_mutex);
20147 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
20148 	    (SATA_DIP(sata_hba_inst), &sata_device);
20149 	mutex_enter(&pmportinfo->pmport_mutex);
20150 
20151 	sata_update_pmport_info(sata_hba_inst, &sata_device);
20152 	if (rval != SATA_SUCCESS) {
20153 		/* Something went wrong? Fail the port */
20154 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
20155 		pmportinfo->pmport_dev_attach_time = 0;
20156 		mutex_exit(&pmportinfo->pmport_mutex);
20157 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20158 		    "SATA port %d:%d probing failed", cport, pmport));
20159 		return;
20160 	} else {
20161 		/* pmport probed successfully */
20162 		pmportinfo->pmport_state |=
20163 		    SATA_STATE_PROBED | SATA_STATE_READY;
20164 	}
20165 	/*
20166 	 * Check if a device is still attached. For sanity, check also
20167 	 * link status - if no link, there is no device.
20168 	 */
20169 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
20170 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
20171 	    SATA_DTYPE_NONE) {
20172 		/*
20173 		 * No device - ignore attach event.
20174 		 */
20175 		pmportinfo->pmport_dev_attach_time = 0;
20176 		mutex_exit(&pmportinfo->pmport_mutex);
20177 		SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20178 		    "Ignoring attach - no device connected to port %d:%d",
20179 		    cport, pmport);
20180 		return;
20181 	}
20182 
20183 	mutex_exit(&pmportinfo->pmport_mutex);
20184 	/*
20185 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
20186 	 * with the hint: SE_HINT_INSERT
20187 	 */
20188 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
20189 
20190 	/*
20191 	 * Port reprobing will take care of the creation of the device
20192 	 * info structure and determination of the device type.
20193 	 */
20194 	sata_device.satadev_addr = *saddr;
20195 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
20196 	    SATA_DEV_IDENTIFY_NORETRY);
20197 
20198 	mutex_enter(&pmportinfo->pmport_mutex);
20199 	if ((pmportinfo->pmport_state & SATA_STATE_READY) &&
20200 	    (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE)) {
20201 		/* Some device is attached to the port */
20202 		if (pmportinfo->pmport_dev_type == SATA_DTYPE_UNKNOWN) {
20203 			/*
20204 			 * A device was not successfully attached.
20205 			 * Track retry time for device identification.
20206 			 */
20207 			if (pmportinfo->pmport_dev_attach_time != 0) {
20208 				clock_t cur_time = ddi_get_lbolt();
20209 				/*
20210 				 * If the retry time limit was not exceeded,
20211 				 * reinstate attach event.
20212 				 */
20213 				if ((cur_time -
20214 				    pmportinfo->pmport_dev_attach_time) <
20215 				    drv_usectohz(
20216 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
20217 					/* OK, restore attach event */
20218 					pmportinfo->pmport_event_flags |=
20219 					    SATA_EVNT_DEVICE_ATTACHED;
20220 				} else {
20221 					/* Timeout - cannot identify device */
20222 					pmportinfo->pmport_dev_attach_time = 0;
20223 					sata_log(sata_hba_inst, CE_WARN,
20224 					    "Could not identify SATA device "
20225 					    "at port %d:%d",
20226 					    cport, pmport);
20227 				}
20228 			} else {
20229 				/*
20230 				 * Start tracking time for device
20231 				 * identification.
20232 				 * Save current time (lbolt value).
20233 				 */
20234 				pmportinfo->pmport_dev_attach_time =
20235 				    ddi_get_lbolt();
20236 				/* Restore attach event */
20237 				pmportinfo->pmport_event_flags |=
20238 				    SATA_EVNT_DEVICE_ATTACHED;
20239 			}
20240 		} else {
20241 			/*
20242 			 * If device was successfully attached, the subsequent
20243 			 * action depends on a state of the
20244 			 * sata_auto_online variable. If it is set to zero.
20245 			 * an explicit 'configure' command will be needed to
20246 			 * configure it. If its value is non-zero, we will
20247 			 * attempt to online (configure) the device.
20248 			 * First, log the message indicating that a device
20249 			 * was attached.
20250 			 */
20251 			pmportinfo->pmport_dev_attach_time = 0;
20252 			sata_log(sata_hba_inst, CE_WARN,
20253 			    "SATA device detected at port %d:%d",
20254 			    cport, pmport);
20255 
20256 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20257 				sata_drive_info_t new_sdinfo;
20258 
20259 				/* Log device info data */
20260 				new_sdinfo = *(SATA_PMPORTINFO_DRV_INFO(
20261 				    pmportinfo));
20262 				sata_show_drive_info(sata_hba_inst,
20263 				    &new_sdinfo);
20264 			}
20265 
20266 			mutex_exit(&pmportinfo->pmport_mutex);
20267 
20268 			/*
20269 			 * Make sure that there is no target node for that
20270 			 * device. If so, release it. It should not happen,
20271 			 * unless we had problem removing the node when
20272 			 * device was detached.
20273 			 */
20274 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
20275 			    saddr->cport, saddr->pmport);
20276 			mutex_enter(&pmportinfo->pmport_mutex);
20277 			if (tdip != NULL) {
20278 
20279 #ifdef SATA_DEBUG
20280 				if ((pmportinfo->pmport_event_flags &
20281 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
20282 					sata_log(sata_hba_inst, CE_WARN,
20283 					    "sata_process_device_attached: "
20284 					    "old device target node exists!");
20285 #endif
20286 				/*
20287 				 * target node exists - try to unconfigure
20288 				 * device and remove the node.
20289 				 */
20290 				mutex_exit(&pmportinfo->pmport_mutex);
20291 				rval = ndi_devi_offline(tdip,
20292 				    NDI_DEVI_REMOVE);
20293 				mutex_enter(&pmportinfo->pmport_mutex);
20294 
20295 				if (rval == NDI_SUCCESS) {
20296 					pmportinfo->pmport_event_flags &=
20297 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20298 					pmportinfo->pmport_tgtnode_clean =
20299 					    B_TRUE;
20300 				} else {
20301 					/*
20302 					 * PROBLEM - the target node remained
20303 					 * and it belongs to a previously
20304 					 * attached device.
20305 					 * This happens when the file was open
20306 					 * or the node was waiting for
20307 					 * resources at the time the
20308 					 * associated device was removed.
20309 					 * Instruct event daemon to retry the
20310 					 * cleanup later.
20311 					 */
20312 					sata_log(sata_hba_inst,
20313 					    CE_WARN,
20314 					    "Application(s) accessing "
20315 					    "previously attached SATA "
20316 					    "device have to release "
20317 					    "it before newly inserted "
20318 					    "device can be made accessible."
20319 					    "at port %d:%d",
20320 					    cport, pmport);
20321 					pmportinfo->pmport_event_flags |=
20322 					    SATA_EVNT_TARGET_NODE_CLEANUP;
20323 					pmportinfo->pmport_tgtnode_clean =
20324 					    B_FALSE;
20325 				}
20326 			}
20327 			if (sata_auto_online != 0) {
20328 				pmportinfo->pmport_event_flags |=
20329 				    SATA_EVNT_AUTOONLINE_DEVICE;
20330 			}
20331 
20332 		}
20333 	} else {
20334 		pmportinfo->pmport_dev_attach_time = 0;
20335 	}
20336 
20337 	event_flags = pmportinfo->pmport_event_flags;
20338 	mutex_exit(&pmportinfo->pmport_mutex);
20339 	if (event_flags != 0) {
20340 		mutex_enter(&sata_hba_inst->satahba_mutex);
20341 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20342 		mutex_exit(&sata_hba_inst->satahba_mutex);
20343 		mutex_enter(&sata_mutex);
20344 		sata_event_pending |= SATA_EVNT_MAIN;
20345 		mutex_exit(&sata_mutex);
20346 	}
20347 
20348 	/* clear the reset_in_progress events */
20349 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20350 		if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) {
20351 			/* must clear flags on cport */
20352 			sata_pmult_info_t *pminfo =
20353 			    SATA_PMULT_INFO(sata_hba_inst,
20354 			    saddr->cport);
20355 			pminfo->pmult_event_flags |=
20356 			    SATA_EVNT_CLEAR_DEVICE_RESET;
20357 		}
20358 	}
20359 }
20360 
20361 /*
20362  * Device Target Node Cleanup Event processing.
20363  * If the target node associated with a sata port device is in
20364  * DEVI_DEVICE_REMOVED state, an attempt is made to remove it.
20365  * If the target node cannot be removed, the event flag is left intact,
20366  * so that event daemon may re-run this function later.
20367  *
20368  * This function cannot be called in interrupt context (it may sleep).
20369  *
20370  * NOTE: Processes cport events only, not port multiplier ports.
20371  */
20372 static void
20373 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
20374     sata_address_t *saddr)
20375 {
20376 	sata_cport_info_t *cportinfo;
20377 	dev_info_t *tdip;
20378 
20379 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20380 	    "Processing port %d device target node cleanup", saddr->cport);
20381 
20382 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
20383 
20384 	/*
20385 	 * Check if there is target node for that device and it is in the
20386 	 * DEVI_DEVICE_REMOVED state. If so, release it.
20387 	 */
20388 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport,
20389 	    saddr->pmport);
20390 	if (tdip != NULL) {
20391 		/*
20392 		 * target node exists - check if it is target node of
20393 		 * a removed device.
20394 		 */
20395 		if (sata_check_device_removed(tdip) == B_TRUE) {
20396 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20397 			    "sata_process_target_node_cleanup: "
20398 			    "old device target node exists!", NULL);
20399 			/*
20400 			 * Unconfigure and remove the target node
20401 			 */
20402 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) ==
20403 			    NDI_SUCCESS) {
20404 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20405 				    saddr->cport)->cport_mutex);
20406 				cportinfo->cport_event_flags &=
20407 				    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20408 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20409 				    saddr->cport)->cport_mutex);
20410 				return;
20411 			}
20412 			/*
20413 			 * Event daemon will retry the cleanup later.
20414 			 */
20415 			mutex_enter(&sata_hba_inst->satahba_mutex);
20416 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20417 			mutex_exit(&sata_hba_inst->satahba_mutex);
20418 			mutex_enter(&sata_mutex);
20419 			sata_event_pending |= SATA_EVNT_MAIN;
20420 			mutex_exit(&sata_mutex);
20421 		}
20422 	} else {
20423 		if (saddr->qual == SATA_ADDR_CPORT ||
20424 		    saddr->qual == SATA_ADDR_DCPORT) {
20425 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20426 			    saddr->cport)->cport_mutex);
20427 			cportinfo->cport_event_flags &=
20428 			    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20429 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20430 			    saddr->cport)->cport_mutex);
20431 		} else {
20432 			/* sanity check */
20433 			if (SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) !=
20434 			    SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst,
20435 			    saddr->cport) == NULL)
20436 				return;
20437 			if (SATA_PMPORT_INFO(sata_hba_inst, saddr->cport,
20438 			    saddr->pmport) == NULL)
20439 				return;
20440 
20441 			mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst,
20442 			    saddr->cport, saddr->pmport)->pmport_mutex);
20443 			SATA_PMPORT_INFO(sata_hba_inst, saddr->cport,
20444 			    saddr->pmport)->pmport_event_flags &=
20445 			    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20446 			mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst,
20447 			    saddr->cport, saddr->pmport)->pmport_mutex);
20448 		}
20449 	}
20450 }
20451 
20452 /*
20453  * Device AutoOnline Event processing.
20454  * If attached device is to be onlined, an attempt is made to online this
20455  * device, but only if there is no lingering (old) target node present.
20456  * If the device cannot be onlined, the event flag is left intact,
20457  * so that event daemon may re-run this function later.
20458  *
20459  * This function cannot be called in interrupt context (it may sleep).
20460  *
20461  * NOTE: Processes cport events only, not port multiplier ports.
20462  */
20463 static void
20464 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst,
20465     sata_address_t *saddr)
20466 {
20467 	sata_cport_info_t *cportinfo;
20468 	sata_drive_info_t *sdinfo;
20469 	sata_device_t sata_device;
20470 	dev_info_t *tdip;
20471 
20472 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20473 	    "Processing port %d attached device auto-onlining", saddr->cport);
20474 
20475 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
20476 
20477 	/*
20478 	 * Check if device is present and recognized. If not, reset event.
20479 	 */
20480 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20481 	if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
20482 		/* Nothing to online */
20483 		cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
20484 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20485 		    saddr->cport)->cport_mutex);
20486 		return;
20487 	}
20488 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20489 
20490 	/*
20491 	 * Check if there is target node for this device and if it is in the
20492 	 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep
20493 	 * the event for later processing.
20494 	 */
20495 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport,
20496 	    saddr->pmport);
20497 	if (tdip != NULL) {
20498 		/*
20499 		 * target node exists - check if it is target node of
20500 		 * a removed device.
20501 		 */
20502 		if (sata_check_device_removed(tdip) == B_TRUE) {
20503 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20504 			    "sata_process_device_autoonline: "
20505 			    "old device target node exists!", NULL);
20506 			/*
20507 			 * Event daemon will retry device onlining later.
20508 			 */
20509 			mutex_enter(&sata_hba_inst->satahba_mutex);
20510 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20511 			mutex_exit(&sata_hba_inst->satahba_mutex);
20512 			mutex_enter(&sata_mutex);
20513 			sata_event_pending |= SATA_EVNT_MAIN;
20514 			mutex_exit(&sata_mutex);
20515 			return;
20516 		}
20517 		/*
20518 		 * If the target node is not in the 'removed" state, assume
20519 		 * that it belongs to this device. There is nothing more to do,
20520 		 * but reset the event.
20521 		 */
20522 	} else {
20523 
20524 		/*
20525 		 * Try to online the device
20526 		 * If there is any reset-related event, remove it. We are
20527 		 * configuring the device and no state restoring is needed.
20528 		 */
20529 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20530 		    saddr->cport)->cport_mutex);
20531 		sata_device.satadev_addr = *saddr;
20532 		if (saddr->qual == SATA_ADDR_CPORT)
20533 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
20534 		else
20535 			sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
20536 		sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
20537 		if (sdinfo != NULL) {
20538 			if (sdinfo->satadrv_event_flags &
20539 			    (SATA_EVNT_DEVICE_RESET |
20540 			    SATA_EVNT_INPROC_DEVICE_RESET))
20541 				sdinfo->satadrv_event_flags = 0;
20542 			sdinfo->satadrv_event_flags |=
20543 			    SATA_EVNT_CLEAR_DEVICE_RESET;
20544 
20545 			/* Need to create a new target node. */
20546 			cportinfo->cport_tgtnode_clean = B_TRUE;
20547 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20548 			    saddr->cport)->cport_mutex);
20549 			tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
20550 			    sata_hba_inst, &sata_device.satadev_addr);
20551 			if (tdip == NULL) {
20552 				/*
20553 				 * Configure (onlining) failed.
20554 				 * We will NOT retry
20555 				 */
20556 				SATA_LOG_D((sata_hba_inst, CE_WARN,
20557 				    "sata_process_device_autoonline: "
20558 				    "configuring SATA device at port %d failed",
20559 				    saddr->cport));
20560 			}
20561 		} else {
20562 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20563 			    saddr->cport)->cport_mutex);
20564 		}
20565 
20566 	}
20567 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20568 	cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
20569 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20570 	    saddr->cport)->cport_mutex);
20571 }
20572 
20573 
20574 static void
20575 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr,
20576     int hint)
20577 {
20578 	char ap[MAXPATHLEN];
20579 	nvlist_t *ev_attr_list = NULL;
20580 	int err;
20581 
20582 	/* Allocate and build sysevent attribute list */
20583 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP);
20584 	if (err != 0) {
20585 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20586 		    "sata_gen_sysevent: "
20587 		    "cannot allocate memory for sysevent attributes\n"));
20588 		return;
20589 	}
20590 	/* Add hint attribute */
20591 	err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint));
20592 	if (err != 0) {
20593 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20594 		    "sata_gen_sysevent: "
20595 		    "failed to add DR_HINT attr for sysevent"));
20596 		nvlist_free(ev_attr_list);
20597 		return;
20598 	}
20599 	/*
20600 	 * Add AP attribute.
20601 	 * Get controller pathname and convert it into AP pathname by adding
20602 	 * a target number.
20603 	 */
20604 	(void) snprintf(ap, MAXPATHLEN, "/devices");
20605 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap));
20606 	(void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d",
20607 	    SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual));
20608 
20609 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap);
20610 	if (err != 0) {
20611 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20612 		    "sata_gen_sysevent: "
20613 		    "failed to add DR_AP_ID attr for sysevent"));
20614 		nvlist_free(ev_attr_list);
20615 		return;
20616 	}
20617 
20618 	/* Generate/log sysevent */
20619 	err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR,
20620 	    ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP);
20621 	if (err != DDI_SUCCESS) {
20622 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20623 		    "sata_gen_sysevent: "
20624 		    "cannot log sysevent, err code %x\n", err));
20625 	}
20626 
20627 	nvlist_free(ev_attr_list);
20628 }
20629 
20630 
20631 
20632 
20633 /*
20634  * Set DEVI_DEVICE_REMOVED state in the SATA device target node.
20635  */
20636 static void
20637 sata_set_device_removed(dev_info_t *tdip)
20638 {
20639 	int circ;
20640 
20641 	ASSERT(tdip != NULL);
20642 
20643 	ndi_devi_enter(tdip, &circ);
20644 	mutex_enter(&DEVI(tdip)->devi_lock);
20645 	DEVI_SET_DEVICE_REMOVED(tdip);
20646 	mutex_exit(&DEVI(tdip)->devi_lock);
20647 	ndi_devi_exit(tdip, circ);
20648 }
20649 
20650 
20651 /*
20652  * Set internal event instructing event daemon to try
20653  * to perform the target node cleanup.
20654  */
20655 static void
20656 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
20657     sata_address_t *saddr)
20658 {
20659 	if (saddr->qual == SATA_ADDR_CPORT ||
20660 	    saddr->qual == SATA_ADDR_DCPORT) {
20661 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20662 		    saddr->cport)->cport_mutex);
20663 		SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |=
20664 		    SATA_EVNT_TARGET_NODE_CLEANUP;
20665 		SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
20666 		    cport_tgtnode_clean = B_FALSE;
20667 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20668 		    saddr->cport)->cport_mutex);
20669 	} else {
20670 		mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst,
20671 		    saddr->cport, saddr->pmport)->pmport_mutex);
20672 		SATA_PMPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport,
20673 		    saddr->pmport) |= SATA_EVNT_TARGET_NODE_CLEANUP;
20674 		SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, saddr->pmport)->
20675 		    pmport_tgtnode_clean = B_FALSE;
20676 		mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst,
20677 		    saddr->cport, saddr->pmport)->pmport_mutex);
20678 	}
20679 	mutex_enter(&sata_hba_inst->satahba_mutex);
20680 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20681 	mutex_exit(&sata_hba_inst->satahba_mutex);
20682 	mutex_enter(&sata_mutex);
20683 	sata_event_pending |= SATA_EVNT_MAIN;
20684 	mutex_exit(&sata_mutex);
20685 }
20686 
20687 
20688 /*
20689  * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state,
20690  * i.e. check if the target node state indicates that it belongs to a removed
20691  * device.
20692  *
20693  * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state,
20694  * B_FALSE otherwise.
20695  */
20696 static boolean_t
20697 sata_check_device_removed(dev_info_t *tdip)
20698 {
20699 	ASSERT(tdip != NULL);
20700 
20701 	if (DEVI_IS_DEVICE_REMOVED(tdip))
20702 		return (B_TRUE);
20703 	else
20704 		return (B_FALSE);
20705 }
20706 
20707 
20708 /*
20709  * Check for DMA error. Return B_TRUE if error, B_FALSE otherwise.
20710  */
20711 static boolean_t
20712 sata_check_for_dma_error(dev_info_t *dip, sata_pkt_txlate_t *spx)
20713 {
20714 	int fm_capability = ddi_fm_capable(dip);
20715 	ddi_fm_error_t de;
20716 
20717 	if (fm_capability & DDI_FM_DMACHK_CAPABLE) {
20718 		if (spx->txlt_buf_dma_handle != NULL) {
20719 			ddi_fm_dma_err_get(spx->txlt_buf_dma_handle, &de,
20720 			    DDI_FME_VERSION);
20721 			if (de.fme_status != DDI_SUCCESS)
20722 				return (B_TRUE);
20723 		}
20724 	}
20725 	return (B_FALSE);
20726 }
20727 
20728 
20729 /* ************************ FAULT INJECTTION **************************** */
20730 
20731 #ifdef SATA_INJECT_FAULTS
20732 
20733 static	uint32_t sata_fault_count = 0;
20734 static	uint32_t sata_fault_suspend_count = 0;
20735 
20736 /*
20737  * Inject sata pkt fault
20738  * It modifies returned values of the sata packet.
20739  * It returns immediately if:
20740  * pkt fault injection is not enabled (via sata_inject_fault,
20741  * sata_inject_fault_count), or invalid fault is specified (sata_fault_type),
20742  * or pkt does not contain command to be faulted (set in sata_fault_cmd), or
20743  * pkt is not directed to specified fault controller/device
20744  * (sata_fault_ctrl_dev and sata_fault_device).
20745  * If fault controller is not specified, fault injection applies to all
20746  * controllers and devices.
20747  *
20748  * First argument is the pointer to the executed sata packet.
20749  * Second argument is a pointer to a value returned by the HBA tran_start
20750  * function.
20751  * Third argument specifies injected error. Injected sata packet faults
20752  * are the satapkt_reason values.
20753  * SATA_PKT_BUSY		-1	Not completed, busy
20754  * SATA_PKT_DEV_ERROR		1	Device reported error
20755  * SATA_PKT_QUEUE_FULL		2	Not accepted, queue full
20756  * SATA_PKT_PORT_ERROR		3	Not completed, port error
20757  * SATA_PKT_CMD_UNSUPPORTED	4	Cmd unsupported
20758  * SATA_PKT_ABORTED		5	Aborted by request
20759  * SATA_PKT_TIMEOUT		6	Operation timeut
20760  * SATA_PKT_RESET		7	Aborted by reset request
20761  *
20762  * Additional global variables affecting the execution:
20763  *
20764  * sata_inject_fault_count variable specifies number of times in row the
20765  * error is injected. Value of -1 specifies permanent fault, ie. every time
20766  * the fault injection point is reached, the fault is injected and a pause
20767  * between fault injection specified by sata_inject_fault_pause_count is
20768  * ignored). Fault injection routine decrements sata_inject_fault_count
20769  * (if greater than zero) until it reaches 0. No fault is injected when
20770  * sata_inject_fault_count is 0 (zero).
20771  *
20772  * sata_inject_fault_pause_count variable specifies number of times a fault
20773  * injection is bypassed (pause between fault injections).
20774  * If set to 0, a fault is injected only a number of times specified by
20775  * sata_inject_fault_count.
20776  *
20777  * The fault counts are static, so for periodic errors they have to be manually
20778  * reset to start repetition sequence from scratch.
20779  * If the original value returned by the HBA tran_start function is not
20780  * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error
20781  * is injected (to avoid masking real problems);
20782  *
20783  * NOTE: In its current incarnation, this function should be invoked only for
20784  * commands executed in SYNCHRONOUS mode.
20785  */
20786 
20787 
20788 static void
20789 sata_inject_pkt_fault(sata_pkt_t *spkt, int *rval, int fault)
20790 {
20791 
20792 	if (sata_inject_fault != SATA_INJECT_PKT_FAULT)
20793 		return;
20794 
20795 	if (sata_inject_fault_count == 0)
20796 		return;
20797 
20798 	if (fault == 0)
20799 		return;
20800 
20801 	if (sata_fault_cmd != spkt->satapkt_cmd.satacmd_cmd_reg)
20802 		return;
20803 
20804 	if (sata_fault_ctrl != NULL) {
20805 		sata_pkt_txlate_t *spx =
20806 		    (sata_pkt_txlate_t *)spkt->satapkt_framework_private;
20807 
20808 		if (sata_fault_ctrl != NULL && sata_fault_ctrl !=
20809 		    spx->txlt_sata_hba_inst->satahba_dip)
20810 			return;
20811 
20812 		if (sata_fault_device.satadev_addr.cport !=
20813 		    spkt->satapkt_device.satadev_addr.cport ||
20814 		    sata_fault_device.satadev_addr.pmport !=
20815 		    spkt->satapkt_device.satadev_addr.pmport ||
20816 		    sata_fault_device.satadev_addr.qual !=
20817 		    spkt->satapkt_device.satadev_addr.qual)
20818 			return;
20819 	}
20820 
20821 	/* Modify pkt return parameters */
20822 	if (*rval != SATA_TRAN_ACCEPTED ||
20823 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
20824 		sata_fault_count = 0;
20825 		sata_fault_suspend_count = 0;
20826 		return;
20827 	}
20828 	if (sata_fault_count == 0 && sata_fault_suspend_count != 0) {
20829 		/* Pause in the injection */
20830 		sata_fault_suspend_count -= 1;
20831 		return;
20832 	}
20833 
20834 	if (sata_fault_count == 0 && sata_fault_suspend_count == 0) {
20835 		/*
20836 		 * Init inject fault cycle. If fault count is set to -1,
20837 		 * it is a permanent fault.
20838 		 */
20839 		if (sata_inject_fault_count != -1) {
20840 			sata_fault_count = sata_inject_fault_count;
20841 			sata_fault_suspend_count =
20842 			    sata_inject_fault_pause_count;
20843 			if (sata_fault_suspend_count == 0)
20844 				sata_inject_fault_count = 0;
20845 		}
20846 	}
20847 
20848 	if (sata_fault_count != 0)
20849 		sata_fault_count -= 1;
20850 
20851 	switch (fault) {
20852 	case SATA_PKT_BUSY:
20853 		*rval = SATA_TRAN_BUSY;
20854 		spkt->satapkt_reason = SATA_PKT_BUSY;
20855 		break;
20856 
20857 	case SATA_PKT_QUEUE_FULL:
20858 		*rval = SATA_TRAN_QUEUE_FULL;
20859 		spkt->satapkt_reason = SATA_PKT_QUEUE_FULL;
20860 		break;
20861 
20862 	case SATA_PKT_CMD_UNSUPPORTED:
20863 		*rval = SATA_TRAN_CMD_UNSUPPORTED;
20864 		spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED;
20865 		break;
20866 
20867 	case SATA_PKT_PORT_ERROR:
20868 		/* This is "rejected" command */
20869 		*rval = SATA_TRAN_PORT_ERROR;
20870 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
20871 		/* Additional error setup could be done here - port state */
20872 		break;
20873 
20874 	case SATA_PKT_DEV_ERROR:
20875 		spkt->satapkt_reason = SATA_PKT_DEV_ERROR;
20876 		/*
20877 		 * Additional error setup could be done here
20878 		 */
20879 		break;
20880 
20881 	case SATA_PKT_ABORTED:
20882 		spkt->satapkt_reason = SATA_PKT_ABORTED;
20883 		break;
20884 
20885 	case SATA_PKT_TIMEOUT:
20886 		spkt->satapkt_reason = SATA_PKT_TIMEOUT;
20887 		/* Additional error setup could be done here */
20888 		break;
20889 
20890 	case SATA_PKT_RESET:
20891 		spkt->satapkt_reason = SATA_PKT_RESET;
20892 		/*
20893 		 * Additional error setup could be done here - device reset
20894 		 */
20895 		break;
20896 
20897 	default:
20898 		break;
20899 	}
20900 }
20901 
20902 #endif
20903 
20904 /*
20905  * SATA Trace Ring Buffer
20906  * ----------------------
20907  *
20908  * Overview
20909  *
20910  * The SATA trace ring buffer is a ring buffer created and managed by
20911  * the SATA framework module that can be used by any module or driver
20912  * within the SATA framework to store debug messages.
20913  *
20914  * Ring Buffer Interfaces:
20915  *
20916  *	sata_vtrace_debug()	<-- Adds debug message to ring buffer
20917  *	sata_trace_debug()	<-- Wraps varargs into sata_vtrace_debug()
20918  *
20919  *	Note that the sata_trace_debug() interface was created to give
20920  *	consumers the flexibilty of sending debug messages to ring buffer
20921  *	as variable arguments.  Consumers can send type va_list debug
20922  *	messages directly to sata_vtrace_debug(). The sata_trace_debug()
20923  *	and sata_vtrace_debug() relationship is similar to that of
20924  *	cmn_err(9F) and vcmn_err(9F).
20925  *
20926  * Below is a diagram of the SATA trace ring buffer interfaces and
20927  * sample consumers:
20928  *
20929  * +---------------------------------+
20930  * |    o  o  SATA Framework Module  |
20931  * | o  SATA  o     +------------------+      +------------------+
20932  * |o   Trace  o <--|sata_vtrace_debug/|<-----|SATA HBA Driver #1|
20933  * |o   R-Buf  o    |sata_trace_debug  |<--+  +------------------+
20934  * | o        o     +------------------+   |  +------------------+
20935  * |    o  o                ^        |     +--|SATA HBA Driver #2|
20936  * |                        |        |        +------------------+
20937  * |           +------------------+  |
20938  * |           |SATA Debug Message|  |
20939  * |           +------------------+  |
20940  * +---------------------------------+
20941  *
20942  * Supporting Routines:
20943  *
20944  *	sata_trace_rbuf_alloc()	<-- Initializes ring buffer
20945  *	sata_trace_rbuf_free()	<-- Destroys ring buffer
20946  *	sata_trace_dmsg_alloc() <-- Creates or reuses buffer in ring buffer
20947  *	sata_trace_dmsg_free()	<-- Destroys content of ring buffer
20948  *
20949  * The default SATA trace ring buffer size is defined by DMSG_RING_SIZE.
20950  * The ring buffer size can be adjusted by setting dmsg_ring_size in
20951  * /etc/system to desired size in unit of bytes.
20952  *
20953  * The individual debug message size in the ring buffer is restricted
20954  * to DMSG_BUF_SIZE.
20955  */
20956 void
20957 sata_vtrace_debug(dev_info_t *dip, const char *fmt, va_list ap)
20958 {
20959 	sata_trace_dmsg_t *dmsg;
20960 
20961 	if (sata_debug_rbuf == NULL) {
20962 		return;
20963 	}
20964 
20965 	/*
20966 	 * If max size of ring buffer is smaller than size
20967 	 * required for one debug message then just return
20968 	 * since we have no room for the debug message.
20969 	 */
20970 	if (sata_debug_rbuf->maxsize < (sizeof (sata_trace_dmsg_t))) {
20971 		return;
20972 	}
20973 
20974 	mutex_enter(&sata_debug_rbuf->lock);
20975 
20976 	/* alloc or reuse on ring buffer */
20977 	dmsg = sata_trace_dmsg_alloc();
20978 
20979 	if (dmsg == NULL) {
20980 		/* resource allocation failed */
20981 		mutex_exit(&sata_debug_rbuf->lock);
20982 		return;
20983 	}
20984 
20985 	dmsg->dip = dip;
20986 	gethrestime(&dmsg->timestamp);
20987 
20988 	(void) vsnprintf(dmsg->buf, sizeof (dmsg->buf), fmt, ap);
20989 
20990 	mutex_exit(&sata_debug_rbuf->lock);
20991 }
20992 
20993 void
20994 sata_trace_debug(dev_info_t *dip, const char *fmt, ...)
20995 {
20996 	va_list ap;
20997 
20998 	va_start(ap, fmt);
20999 	sata_vtrace_debug(dip, fmt, ap);
21000 	va_end(ap);
21001 }
21002 
21003 /*
21004  * This routine is used to manage debug messages
21005  * on ring buffer.
21006  */
21007 static sata_trace_dmsg_t *
21008 sata_trace_dmsg_alloc(void)
21009 {
21010 	sata_trace_dmsg_t *dmsg_alloc, *dmsg = sata_debug_rbuf->dmsgp;
21011 
21012 	if (sata_debug_rbuf->looped == TRUE) {
21013 		sata_debug_rbuf->dmsgp = dmsg->next;
21014 		return (sata_debug_rbuf->dmsgp);
21015 	}
21016 
21017 	/*
21018 	 * If we're looping for the first time,
21019 	 * connect the ring.
21020 	 */
21021 	if (((sata_debug_rbuf->size + (sizeof (sata_trace_dmsg_t))) >
21022 	    sata_debug_rbuf->maxsize) && (sata_debug_rbuf->dmsgh != NULL)) {
21023 		dmsg->next = sata_debug_rbuf->dmsgh;
21024 		sata_debug_rbuf->dmsgp = sata_debug_rbuf->dmsgh;
21025 		sata_debug_rbuf->looped = TRUE;
21026 		return (sata_debug_rbuf->dmsgp);
21027 	}
21028 
21029 	/* If we've gotten this far then memory allocation is needed */
21030 	dmsg_alloc = kmem_zalloc(sizeof (sata_trace_dmsg_t), KM_NOSLEEP);
21031 	if (dmsg_alloc == NULL) {
21032 		sata_debug_rbuf->allocfailed++;
21033 		return (dmsg_alloc);
21034 	} else {
21035 		sata_debug_rbuf->size += sizeof (sata_trace_dmsg_t);
21036 	}
21037 
21038 	if (sata_debug_rbuf->dmsgp != NULL) {
21039 		dmsg->next = dmsg_alloc;
21040 		sata_debug_rbuf->dmsgp = dmsg->next;
21041 		return (sata_debug_rbuf->dmsgp);
21042 	} else {
21043 		/*
21044 		 * We should only be here if we're initializing
21045 		 * the ring buffer.
21046 		 */
21047 		if (sata_debug_rbuf->dmsgh == NULL) {
21048 			sata_debug_rbuf->dmsgh = dmsg_alloc;
21049 		} else {
21050 			/* Something is wrong */
21051 			kmem_free(dmsg_alloc, sizeof (sata_trace_dmsg_t));
21052 			return (NULL);
21053 		}
21054 
21055 		sata_debug_rbuf->dmsgp = dmsg_alloc;
21056 		return (sata_debug_rbuf->dmsgp);
21057 	}
21058 }
21059 
21060 
21061 /*
21062  * Free all messages on debug ring buffer.
21063  */
21064 static void
21065 sata_trace_dmsg_free(void)
21066 {
21067 	sata_trace_dmsg_t *dmsg_next, *dmsg = sata_debug_rbuf->dmsgh;
21068 
21069 	while (dmsg != NULL) {
21070 		dmsg_next = dmsg->next;
21071 		kmem_free(dmsg, sizeof (sata_trace_dmsg_t));
21072 
21073 		/*
21074 		 * If we've looped around the ring than we're done.
21075 		 */
21076 		if (dmsg_next == sata_debug_rbuf->dmsgh) {
21077 			break;
21078 		} else {
21079 			dmsg = dmsg_next;
21080 		}
21081 	}
21082 }
21083 
21084 
21085 /*
21086  * This function can block
21087  */
21088 static void
21089 sata_trace_rbuf_alloc(void)
21090 {
21091 	sata_debug_rbuf = kmem_zalloc(sizeof (sata_trace_rbuf_t), KM_SLEEP);
21092 
21093 	mutex_init(&sata_debug_rbuf->lock, NULL, MUTEX_DRIVER, NULL);
21094 
21095 	if (dmsg_ring_size > 0) {
21096 		sata_debug_rbuf->maxsize = (size_t)dmsg_ring_size;
21097 	}
21098 }
21099 
21100 
21101 static void
21102 sata_trace_rbuf_free(void)
21103 {
21104 	sata_trace_dmsg_free();
21105 	mutex_destroy(&sata_debug_rbuf->lock);
21106 	kmem_free(sata_debug_rbuf, sizeof (sata_trace_rbuf_t));
21107 }
21108 
21109 /*
21110  * If SATA_DEBUG is not defined then this routine is called instead
21111  * of sata_log() via the SATA_LOG_D macro.
21112  */
21113 static void
21114 sata_trace_log(sata_hba_inst_t *sata_hba_inst, uint_t level,
21115     const char *fmt, ...)
21116 {
21117 #ifndef __lock_lint
21118 	_NOTE(ARGUNUSED(level))
21119 #endif
21120 
21121 	dev_info_t *dip = NULL;
21122 	va_list ap;
21123 
21124 	if (sata_hba_inst != NULL) {
21125 		dip = SATA_DIP(sata_hba_inst);
21126 	}
21127 
21128 	va_start(ap, fmt);
21129 	sata_vtrace_debug(dip, fmt, ap);
21130 	va_end(ap);
21131 }
21132