xref: /illumos-gate/usr/src/uts/common/io/sata/impl/sata.c (revision 8a2b682e57a046b828f37bcde1776f131ef4629f)
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 2017 Nexenta Systems, Inc.  All rights reserved.
27  * Copyright 2016 Argo Technologies SA
28  * Copyright 2019 Joyent, Inc.
29  */
30 
31 /*
32  * SATA Framework
33  * Generic SATA Host Adapter Implementation
34  */
35 
36 #include <sys/conf.h>
37 #include <sys/file.h>
38 #include <sys/ddi.h>
39 #include <sys/sunddi.h>
40 #include <sys/modctl.h>
41 #include <sys/cmn_err.h>
42 #include <sys/errno.h>
43 #include <sys/thread.h>
44 #include <sys/kstat.h>
45 #include <sys/note.h>
46 #include <sys/sysevent.h>
47 #include <sys/sysevent/eventdefs.h>
48 #include <sys/sysevent/dr.h>
49 #include <sys/taskq.h>
50 #include <sys/disp.h>
51 #include <sys/sdt.h>
52 
53 #include <sys/sata/impl/sata.h>
54 #include <sys/sata/sata_hba.h>
55 #include <sys/sata/sata_defs.h>
56 #include <sys/sata/sata_cfgadm.h>
57 #include <sys/sata/sata_blacklist.h>
58 #include <sys/sata/sata_satl.h>
59 
60 #include <sys/scsi/impl/spc3_types.h>
61 
62 /*
63  * FMA header files
64  */
65 #include <sys/ddifm.h>
66 #include <sys/fm/protocol.h>
67 #include <sys/fm/util.h>
68 #include <sys/fm/io/ddi.h>
69 
70 /* Debug flags - defined in sata.h */
71 int	sata_debug_flags = 0;
72 int	sata_msg = 0;
73 
74 /*
75  * Flags enabling selected SATA HBA framework functionality
76  */
77 #define	SATA_ENABLE_QUEUING		1
78 #define	SATA_ENABLE_NCQ			2
79 #define	SATA_ENABLE_PROCESS_EVENTS	4
80 #define	SATA_ENABLE_PMULT_FBS		8 /* FIS-Based Switching */
81 int sata_func_enable =
82 	SATA_ENABLE_PROCESS_EVENTS | SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ;
83 
84 /*
85  * Global variable setting default maximum queue depth (NCQ or TCQ)
86  * Note:minimum queue depth is 1
87  */
88 int sata_max_queue_depth = SATA_MAX_QUEUE_DEPTH; /* max NCQ/TCQ queue depth */
89 
90 /*
91  * Currently used default NCQ/TCQ queue depth. It is set-up during the driver
92  * initialization, using value from sata_max_queue_depth
93  * It is adjusted to minimum supported by the controller and by the device,
94  * if queueing is enabled.
95  */
96 static	int sata_current_max_qdepth;
97 
98 /*
99  * Global variable determining the default behavior after device hotpluggin.
100  * If non-zero, the hotplugged device is onlined (if possible) without explicit
101  * IOCTL request (AP_CONFIGURE).
102  * If zero, hotplugged device is identified, but not onlined.
103  * Enabling (AP_CONNECT) device port with an attached device does not result
104  * in device onlining regardless of the flag setting
105  */
106 int sata_auto_online = 0;
107 
108 #ifdef SATA_DEBUG
109 
110 #define	SATA_LOG_D(args)	sata_log args
111 uint64_t mbuf_count = 0;
112 uint64_t mbuffail_count = 0;
113 
114 sata_atapi_cmd_t sata_atapi_trace[64];
115 uint32_t sata_atapi_trace_index = 0;
116 int sata_atapi_trace_save = 1;
117 static	void sata_save_atapi_trace(sata_pkt_txlate_t *, int);
118 #define	SATAATAPITRACE(spx, count)	if (sata_atapi_trace_save) \
119     sata_save_atapi_trace(spx, count);
120 
121 #else
122 #define	SATA_LOG_D(args)	sata_trace_log args
123 #define	SATAATAPITRACE(spx, count)
124 #endif
125 
126 #if 0
127 static void
128 sata_test_atapi_packet_command(sata_hba_inst_t *, int);
129 #endif
130 
131 #ifdef SATA_INJECT_FAULTS
132 
133 #define		SATA_INJECT_PKT_FAULT	1
134 uint32_t	sata_inject_fault = 0;
135 
136 uint32_t	sata_inject_fault_count = 0;
137 uint32_t	sata_inject_fault_pause_count = 0;
138 uint32_t	sata_fault_type = 0;
139 uint32_t	sata_fault_cmd = 0;
140 dev_info_t	*sata_fault_ctrl = NULL;
141 sata_device_t	sata_fault_device;
142 
143 static	void sata_inject_pkt_fault(sata_pkt_t *, int *, int);
144 
145 #endif
146 
147 #define	LEGACY_HWID_LEN	64	/* Model (40) + Serial (20) + pad */
148 
149 static char sata_rev_tag[] = {"1.46"};
150 
151 /*
152  * SATA cb_ops functions
153  */
154 static	int sata_hba_open(dev_t *, int, int, cred_t *);
155 static	int sata_hba_close(dev_t, int, int, cred_t *);
156 static	int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *,	int *);
157 
158 /*
159  * SCSA required entry points
160  */
161 static	int sata_scsi_tgt_init(dev_info_t *, dev_info_t *,
162     scsi_hba_tran_t *, struct scsi_device *);
163 static	int sata_scsi_tgt_probe(struct scsi_device *,
164     int (*callback)(void));
165 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *,
166     scsi_hba_tran_t *, struct scsi_device *);
167 static	int sata_scsi_start(struct scsi_address *, struct scsi_pkt *);
168 static	int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *);
169 static	int sata_scsi_reset(struct scsi_address *, int);
170 static	int sata_scsi_getcap(struct scsi_address *, char *, int);
171 static	int sata_scsi_setcap(struct scsi_address *, char *, int, int);
172 static	struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *,
173     struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t),
174     caddr_t);
175 static	void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *);
176 static	void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *);
177 static	void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *);
178 
179 /*
180  * SATA HBA interface functions are defined in sata_hba.h header file
181  */
182 
183 /* Event processing functions */
184 static	void sata_event_daemon(void *);
185 static	void sata_event_thread_control(int);
186 static	void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst);
187 static	void sata_process_pmult_events(sata_hba_inst_t *, uint8_t);
188 static	void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *);
189 static	void sata_process_pmdevice_reset(sata_hba_inst_t *, sata_address_t *);
190 static	void sata_process_port_failed_event(sata_hba_inst_t *,
191     sata_address_t *);
192 static	void sata_process_port_link_events(sata_hba_inst_t *,
193     sata_address_t *);
194 static	void sata_process_pmport_link_events(sata_hba_inst_t *,
195     sata_address_t *);
196 static	void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *);
197 static	void sata_process_pmdevice_detached(sata_hba_inst_t *,
198     sata_address_t *);
199 static	void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *);
200 static	void sata_process_pmdevice_attached(sata_hba_inst_t *,
201     sata_address_t *);
202 static	void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *);
203 static	void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *);
204 static	void sata_process_target_node_cleanup(sata_hba_inst_t *,
205     sata_address_t *);
206 static	void sata_process_device_autoonline(sata_hba_inst_t *,
207     sata_address_t *saddr);
208 
209 /*
210  * Local translation functions
211  */
212 static	int sata_txlt_inquiry(sata_pkt_txlate_t *);
213 static	int sata_txlt_test_unit_ready(sata_pkt_txlate_t *);
214 static	int sata_txlt_start_stop_unit(sata_pkt_txlate_t *);
215 static	int sata_txlt_read_capacity(sata_pkt_txlate_t *);
216 static	int sata_txlt_read_capacity16(sata_pkt_txlate_t *);
217 static  int sata_txlt_unmap(sata_pkt_txlate_t *);
218 static	int sata_txlt_request_sense(sata_pkt_txlate_t *);
219 static	int sata_txlt_read(sata_pkt_txlate_t *);
220 static	int sata_txlt_write(sata_pkt_txlate_t *);
221 static	int sata_txlt_log_sense(sata_pkt_txlate_t *);
222 static	int sata_txlt_log_select(sata_pkt_txlate_t *);
223 static	int sata_txlt_mode_sense(sata_pkt_txlate_t *);
224 static	int sata_txlt_mode_select(sata_pkt_txlate_t *);
225 static	int sata_txlt_ata_pass_thru(sata_pkt_txlate_t *);
226 static	int sata_txlt_synchronize_cache(sata_pkt_txlate_t *);
227 static	int sata_txlt_write_buffer(sata_pkt_txlate_t *);
228 static	int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *);
229 
230 static	int sata_hba_start(sata_pkt_txlate_t *, int *);
231 static	int sata_txlt_invalid_command(sata_pkt_txlate_t *);
232 static	int sata_txlt_check_condition(sata_pkt_txlate_t *, uchar_t, uchar_t);
233 static	int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *);
234 static	int sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *);
235 static  int sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *);
236 static	void sata_txlt_rw_completion(sata_pkt_t *);
237 static	void sata_txlt_nodata_cmd_completion(sata_pkt_t *);
238 static	void sata_txlt_apt_completion(sata_pkt_t *sata_pkt);
239 static	void sata_txlt_unmap_completion(sata_pkt_t *sata_pkt);
240 static	void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *);
241 static	int sata_emul_rw_completion(sata_pkt_txlate_t *);
242 static	void sata_fill_ata_return_desc(sata_pkt_t *, uint8_t, uint8_t,
243     uint8_t);
244 static	struct scsi_extended_sense *sata_immediate_error_response(
245     sata_pkt_txlate_t *, int);
246 static	struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *);
247 
248 static	int sata_txlt_atapi(sata_pkt_txlate_t *);
249 static	void sata_txlt_atapi_completion(sata_pkt_t *);
250 
251 /*
252  * Local functions for ioctl
253  */
254 static	int32_t sata_get_port_num(sata_hba_inst_t *,  struct devctl_iocdata *);
255 static	void sata_cfgadm_state(sata_hba_inst_t *, int32_t,
256     devctl_ap_state_t *);
257 static	dev_info_t *sata_get_target_dip(dev_info_t *, uint8_t, uint8_t);
258 static	dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *);
259 static	dev_info_t *sata_devt_to_devinfo(dev_t);
260 static	int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *);
261 static	int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *);
262 static	int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *);
263 static	int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *);
264 static	int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *);
265 static	int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *);
266 static	int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *);
267 static	int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *);
268 static	int sata_ioctl_reset_all(sata_hba_inst_t *);
269 static	int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *);
270 static	int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *,
271     sata_ioctl_data_t *, int mode);
272 static	int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *,
273     sata_ioctl_data_t *, int mode);
274 static	int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *,
275     sata_ioctl_data_t *, int mode);
276 static	int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *,
277     sata_ioctl_data_t *, int mode);
278 static	int sata_ioctl_get_serialnumber_info(sata_hba_inst_t *,
279     sata_device_t *, sata_ioctl_data_t *, int mode);
280 
281 /*
282  * Local functions
283  */
284 static	void sata_remove_hba_instance(dev_info_t *);
285 static	int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *);
286 static	void sata_probe_ports(sata_hba_inst_t *);
287 static	void sata_probe_pmports(sata_hba_inst_t *, uint8_t);
288 static	int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int);
289 static	int sata_reprobe_pmult(sata_hba_inst_t *, sata_device_t *, int);
290 static	int sata_reprobe_pmport(sata_hba_inst_t *, sata_device_t *, int);
291 static	int sata_alloc_pmult(sata_hba_inst_t *, sata_device_t *);
292 static	void sata_free_pmult(sata_hba_inst_t *, sata_device_t *);
293 static	int sata_add_device(dev_info_t *, sata_hba_inst_t *, sata_device_t *);
294 static	int sata_offline_device(sata_hba_inst_t *, sata_device_t *,
295     sata_drive_info_t *);
296 static	dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *,
297     sata_address_t *);
298 static	void sata_remove_target_node(sata_hba_inst_t *,
299     sata_address_t *);
300 static	int sata_validate_scsi_address(sata_hba_inst_t *,
301     struct scsi_address *, sata_device_t *);
302 static	int sata_validate_sata_address(sata_hba_inst_t *, int, int, int);
303 static	sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t));
304 static	void sata_pkt_free(sata_pkt_txlate_t *);
305 static	int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t),
306     caddr_t, ddi_dma_attr_t *);
307 static	void sata_common_free_dma_rsrcs(sata_pkt_txlate_t *);
308 static	int sata_probe_device(sata_hba_inst_t *, sata_device_t *);
309 static	sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *,
310     sata_device_t *);
311 static	int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *);
312 static	void sata_reidentify_device(sata_pkt_txlate_t *);
313 static	struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int);
314 static	void sata_free_local_buffer(sata_pkt_txlate_t *);
315 static	uint64_t sata_check_capacity(sata_drive_info_t *);
316 void	sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *,
317     ddi_dma_attr_t *);
318 static	int sata_fetch_device_identify_data(sata_hba_inst_t *,
319     sata_drive_info_t *);
320 static	void sata_update_port_info(sata_hba_inst_t *, sata_device_t *);
321 static	void sata_update_pmport_info(sata_hba_inst_t *, sata_device_t *);
322 static	void sata_update_port_scr(sata_port_scr_t *, sata_device_t *);
323 static	int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *);
324 static	int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int);
325 static	int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int);
326 static	int sata_set_drive_features(sata_hba_inst_t *,
327     sata_drive_info_t *, int flag);
328 static	void sata_init_write_cache_mode(sata_drive_info_t *sdinfo);
329 static	int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *);
330 static	void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *,
331     uint8_t *);
332 static	int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *,
333     struct scsi_inquiry *);
334 static	int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *);
335 static	int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *);
336 static	int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *);
337 static	int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *);
338 static	int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *);
339 static	int sata_mode_select_page_8(sata_pkt_txlate_t *,
340     struct mode_cache_scsi3 *, int, int *, int *, int *);
341 static	int sata_mode_select_page_1a(sata_pkt_txlate_t *,
342     struct mode_info_power_cond *, int, int *, int *, int *);
343 static	int sata_mode_select_page_1c(sata_pkt_txlate_t *,
344     struct mode_info_excpt_page *, int, int *, int *, int *);
345 static	int sata_mode_select_page_30(sata_pkt_txlate_t *,
346     struct mode_acoustic_management *, int, int *, int *, int *);
347 
348 static	int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *);
349 static	int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *,
350     sata_hba_inst_t *);
351 static	int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *,
352     sata_hba_inst_t *);
353 static	int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *,
354     sata_hba_inst_t *);
355 static	int sata_build_lsense_page_0e(sata_drive_info_t *, uint8_t *,
356     sata_pkt_txlate_t *);
357 
358 static	void sata_set_arq_data(sata_pkt_t *);
359 static	void sata_build_read_verify_cmd(sata_cmd_t *, uint16_t, uint64_t);
360 static	void sata_build_generic_cmd(sata_cmd_t *, uint8_t);
361 static	uint8_t sata_get_standby_timer(uint8_t *timer);
362 
363 static	void sata_save_drive_settings(sata_drive_info_t *);
364 static	void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *);
365 static	void sata_show_pmult_info(sata_hba_inst_t *, sata_device_t *);
366 static	void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...);
367 static	void sata_trace_log(sata_hba_inst_t *, uint_t, const char *fmt, ...);
368 static	int sata_fetch_smart_return_status(sata_hba_inst_t *,
369     sata_drive_info_t *);
370 static	int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *,
371     struct smart_data *);
372 static	int sata_smart_selftest_log(sata_hba_inst_t *,
373     sata_drive_info_t *,
374     struct smart_selftest_log *);
375 static	int sata_ext_smart_selftest_read_log(sata_hba_inst_t *,
376     sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t);
377 static	int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *,
378     uint8_t *, uint8_t, uint8_t);
379 static	int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *,
380     struct read_log_ext_directory *);
381 static	void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int);
382 static	void sata_xlate_errors(sata_pkt_txlate_t *);
383 static	void sata_decode_device_error(sata_pkt_txlate_t *,
384     struct scsi_extended_sense *);
385 static	void sata_set_device_removed(dev_info_t *);
386 static	boolean_t sata_check_device_removed(dev_info_t *);
387 static	void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *);
388 static	int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *,
389     sata_drive_info_t *);
390 static	int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *,
391     sata_drive_info_t *);
392 static	void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *);
393 static	void sata_fixed_sense_data_preset(struct scsi_extended_sense *);
394 static  void sata_target_devid_register(dev_info_t *, sata_drive_info_t *);
395 static  int sata_check_modser(char *, int);
396 
397 /*
398  * FMA
399  */
400 static boolean_t sata_check_for_dma_error(dev_info_t *, sata_pkt_txlate_t *);
401 
402 
403 /*
404  * SATA Framework will ignore SATA HBA driver cb_ops structure and
405  * register following one with SCSA framework.
406  * Open & close are provided, so scsi framework will not use its own
407  */
408 static struct cb_ops sata_cb_ops = {
409 	sata_hba_open,			/* open */
410 	sata_hba_close,			/* close */
411 	nodev,				/* strategy */
412 	nodev,				/* print */
413 	nodev,				/* dump */
414 	nodev,				/* read */
415 	nodev,				/* write */
416 	sata_hba_ioctl,			/* ioctl */
417 	nodev,				/* devmap */
418 	nodev,				/* mmap */
419 	nodev,				/* segmap */
420 	nochpoll,			/* chpoll */
421 	ddi_prop_op,			/* cb_prop_op */
422 	0,				/* streamtab */
423 	D_NEW | D_MP,			/* cb_flag */
424 	CB_REV,				/* rev */
425 	nodev,				/* aread */
426 	nodev				/* awrite */
427 };
428 
429 
430 extern struct mod_ops mod_miscops;
431 extern uchar_t	scsi_cdb_size[];
432 
433 static struct modlmisc modlmisc = {
434 	&mod_miscops,			/* Type of module */
435 	"SATA Module"			/* module name */
436 };
437 
438 
439 static struct modlinkage modlinkage = {
440 	MODREV_1,
441 	(void *)&modlmisc,
442 	NULL
443 };
444 
445 /*
446  * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero,
447  * i.e. when scsi_pkt has not timeout specified.
448  */
449 static int sata_default_pkt_time = 60;	/* 60 seconds */
450 
451 /*
452  * Intermediate buffer device access attributes - they are required,
453  * but not necessarily used.
454  */
455 static ddi_device_acc_attr_t sata_acc_attr = {
456 	DDI_DEVICE_ATTR_V0,
457 	DDI_STRUCTURE_LE_ACC,
458 	DDI_STRICTORDER_ACC
459 };
460 
461 
462 /*
463  * Mutexes protecting structures in multithreaded operations.
464  * Because events are relatively rare, a single global mutex protecting
465  * data structures should be sufficient. To increase performance, add
466  * separate mutex per each sata port and use global mutex only to protect
467  * common data structures.
468  */
469 static	kmutex_t sata_mutex;		/* protects sata_hba_list */
470 static	kmutex_t sata_log_mutex;	/* protects log */
471 
472 static	char sata_log_buf[256];
473 
474 /*
475  * sata trace debug
476  */
477 static	sata_trace_rbuf_t *sata_debug_rbuf;
478 static	sata_trace_dmsg_t *sata_trace_dmsg_alloc(void);
479 static	void sata_trace_dmsg_free(void);
480 static	void sata_trace_rbuf_alloc(void);
481 static	void sata_trace_rbuf_free(void);
482 
483 int	dmsg_ring_size = DMSG_RING_SIZE;
484 
485 /* Default write cache setting for SATA hard disks */
486 int	sata_write_cache = 1;		/* enabled */
487 
488 /* Default write cache setting for SATA ATAPI CD/DVD */
489 int	sata_atapicdvd_write_cache = 1; /* enabled */
490 
491 /* Default write cache setting for SATA ATAPI tape */
492 int	sata_atapitape_write_cache = 1; /* enabled */
493 
494 /* Default write cache setting for SATA ATAPI disk */
495 int	sata_atapidisk_write_cache = 1;	/* enabled */
496 
497 /*
498  * Linked list of HBA instances
499  */
500 static	sata_hba_inst_t *sata_hba_list = NULL;
501 static	sata_hba_inst_t *sata_hba_list_tail = NULL;
502 /*
503  * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran
504  * structure and in sata soft state.
505  */
506 
507 /*
508  * Event daemon related variables
509  */
510 static	kmutex_t sata_event_mutex;
511 static	kcondvar_t sata_event_cv;
512 static	kthread_t *sata_event_thread = NULL;
513 static	int sata_event_thread_terminate = 0;
514 static	int sata_event_pending = 0;
515 static	int sata_event_thread_active = 0;
516 extern	pri_t minclsyspri;
517 
518 /*
519  * NCQ error recovery command
520  */
521 static const sata_cmd_t sata_rle_cmd = {
522 	SATA_CMD_REV,
523 	NULL,
524 	{
525 		SATA_DIR_READ
526 	},
527 	ATA_ADDR_LBA48,
528 	0,
529 	0,
530 	0,
531 	0,
532 	0,
533 	1,
534 	READ_LOG_EXT_NCQ_ERROR_RECOVERY,
535 	0,
536 	0,
537 	0,
538 	SATAC_READ_LOG_EXT,
539 	0,
540 	0,
541 	0,
542 };
543 
544 /*
545  * ATAPI error recovery CDB
546  */
547 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = {
548 	SCMD_REQUEST_SENSE,
549 	0,			/* Only fixed RQ format is supported */
550 	0,
551 	0,
552 	SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */
553 	0
554 };
555 
556 
557 /* Warlock directives */
558 
559 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran))
560 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device))
561 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops))
562 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense))
563 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status))
564 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr))
565 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t))
566 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state))
567 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state))
568 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list))
569 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list))
570 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next))
571 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev))
572 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \
573     sata_hba_inst::satahba_scsi_tran))
574 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran))
575 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip))
576 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached))
577 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port))
578 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex,
579     sata_hba_inst::satahba_event_flags))
580 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
581     sata_cport_info::cport_devp))
582 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp))
583 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr))
584 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
585     sata_cport_info::cport_dev_type))
586 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type))
587 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
588     sata_cport_info::cport_state))
589 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state))
590 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
591     sata_pmport_info::pmport_state))
592 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state))
593 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
594     sata_pmport_info::pmport_dev_type))
595 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type))
596 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
597     sata_pmport_info::pmport_sata_drive))
598 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
599     sata_pmport_info::pmport_tgtnode_clean))
600 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
601     sata_pmport_info::pmport_event_flags))
602 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive))
603 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port))
604 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports))
605 #ifdef SATA_DEBUG
606 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count))
607 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count))
608 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace))
609 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index))
610 #endif
611 
612 /* End of warlock directives */
613 
614 /* ************** loadable module configuration functions ************** */
615 
616 int
617 _init()
618 {
619 	int rval;
620 
621 	mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL);
622 	mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL);
623 	mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL);
624 	cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL);
625 	sata_trace_rbuf_alloc();
626 	if ((rval = mod_install(&modlinkage)) != 0) {
627 #ifdef SATA_DEBUG
628 		cmn_err(CE_WARN, "sata: _init: mod_install failed\n");
629 #endif
630 		sata_trace_rbuf_free();
631 		mutex_destroy(&sata_log_mutex);
632 		cv_destroy(&sata_event_cv);
633 		mutex_destroy(&sata_event_mutex);
634 		mutex_destroy(&sata_mutex);
635 	}
636 	return (rval);
637 }
638 
639 int
640 _fini()
641 {
642 	int rval;
643 
644 	if ((rval = mod_remove(&modlinkage)) != 0)
645 		return (rval);
646 
647 	sata_trace_rbuf_free();
648 	mutex_destroy(&sata_log_mutex);
649 	cv_destroy(&sata_event_cv);
650 	mutex_destroy(&sata_event_mutex);
651 	mutex_destroy(&sata_mutex);
652 	return (rval);
653 }
654 
655 int
656 _info(struct modinfo *modinfop)
657 {
658 	return (mod_info(&modlinkage, modinfop));
659 }
660 
661 
662 
663 /* ********************* SATA HBA entry points ********************* */
664 
665 
666 /*
667  * Called by SATA HBA from _init().
668  * Registers HBA driver instance/sata framework pair with scsi framework, by
669  * calling scsi_hba_init().
670  *
671  * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used
672  * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver
673  * cb_ops pointer in SATA HBA driver dev_ops structure.
674  * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors.
675  *
676  * Return status of the scsi_hba_init() is returned to a calling SATA HBA
677  * driver.
678  */
679 int
680 sata_hba_init(struct modlinkage *modlp)
681 {
682 	int rval;
683 	struct dev_ops *hba_ops;
684 
685 	SATADBG1(SATA_DBG_HBA_IF, NULL,
686 	    "sata_hba_init: name %s \n",
687 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
688 	/*
689 	 * Fill-up cb_ops and dev_ops when necessary
690 	 */
691 	hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
692 	/*
693 	 * Provide pointer to SATA dev_ops
694 	 */
695 	hba_ops->devo_cb_ops = &sata_cb_ops;
696 
697 	/*
698 	 * Register SATA HBA with SCSI framework
699 	 */
700 	if ((rval = scsi_hba_init(modlp)) != 0) {
701 		SATADBG1(SATA_DBG_HBA_IF, NULL,
702 		    "sata_hba_init: scsi hba init failed\n", NULL);
703 		return (rval);
704 	}
705 
706 	return (0);
707 }
708 
709 
710 /* HBA attach stages */
711 #define	HBA_ATTACH_STAGE_SATA_HBA_INST	1
712 #define	HBA_ATTACH_STAGE_SCSI_ATTACHED	2
713 #define	HBA_ATTACH_STAGE_SETUP		4
714 #define	HBA_ATTACH_STAGE_LINKED		8
715 
716 
717 /*
718  *
719  * Called from SATA HBA driver's attach routine to attach an instance of
720  * the HBA.
721  *
722  * For DDI_ATTACH command:
723  * sata_hba_inst structure is allocated here and initialized with pointers to
724  * SATA framework implementation of required scsi tran functions.
725  * The scsi_tran's tran_hba_private field is used by SATA Framework to point
726  * to the soft structure (sata_hba_inst) allocated by SATA framework for
727  * SATA HBA instance related data.
728  * The scsi_tran's tran_hba_private field is used by SATA framework to
729  * store a pointer to per-HBA-instance of sata_hba_inst structure.
730  * The sata_hba_inst structure is cross-linked to scsi tran structure.
731  * Among other info, a pointer to sata_hba_tran structure is stored in
732  * sata_hba_inst. The sata_hba_inst structures for different HBA instances are
733  * linked together into the list, pointed to by sata_hba_list.
734  * On the first HBA instance attach the sata event thread is initialized.
735  * Attachment points are created for all SATA ports of the HBA being attached.
736  * All HBA instance's SATA ports are probed and type of plugged devices is
737  * determined. For each device of a supported type, a target node is created.
738  *
739  * DDI_SUCCESS is returned when attachment process is successful,
740  * DDI_FAILURE is returned otherwise.
741  *
742  * For DDI_RESUME command:
743  * Not implemented at this time (postponed until phase 2 of the development).
744  */
745 int
746 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran,
747     ddi_attach_cmd_t cmd)
748 {
749 	sata_hba_inst_t	*sata_hba_inst;
750 	scsi_hba_tran_t *scsi_tran = NULL;
751 	int hba_attach_state = 0;
752 	char taskq_name[MAXPATHLEN];
753 
754 	SATADBG3(SATA_DBG_HBA_IF, NULL,
755 	    "sata_hba_attach: node %s (%s%d)\n",
756 	    ddi_node_name(dip), ddi_driver_name(dip),
757 	    ddi_get_instance(dip));
758 
759 	if (cmd == DDI_RESUME) {
760 		/*
761 		 * Postponed until phase 2 of the development
762 		 */
763 		return (DDI_FAILURE);
764 	}
765 
766 	if (cmd != DDI_ATTACH) {
767 		return (DDI_FAILURE);
768 	}
769 
770 	/* cmd == DDI_ATTACH */
771 
772 	if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) {
773 		SATA_LOG_D((NULL, CE_WARN,
774 		    "sata_hba_attach: invalid sata_hba_tran"));
775 		return (DDI_FAILURE);
776 	}
777 	/*
778 	 * Allocate and initialize SCSI tran structure.
779 	 * SATA copy of tran_bus_config is provided to create port nodes.
780 	 */
781 	scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
782 	if (scsi_tran == NULL)
783 		return (DDI_FAILURE);
784 	/*
785 	 * Allocate soft structure for SATA HBA instance.
786 	 * There is a separate softstate for each HBA instance.
787 	 */
788 	sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP);
789 	ASSERT(sata_hba_inst != NULL); /* this should not fail */
790 	mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL);
791 	hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST;
792 
793 	/*
794 	 * scsi_trans's tran_hba_private is used by SATA Framework to point to
795 	 * soft structure allocated by SATA framework for
796 	 * SATA HBA instance related data.
797 	 */
798 	scsi_tran->tran_hba_private	= sata_hba_inst;
799 	scsi_tran->tran_tgt_private	= NULL;
800 
801 	scsi_tran->tran_tgt_init	= sata_scsi_tgt_init;
802 	scsi_tran->tran_tgt_probe	= sata_scsi_tgt_probe;
803 	scsi_tran->tran_tgt_free	= sata_scsi_tgt_free;
804 
805 	scsi_tran->tran_start		= sata_scsi_start;
806 	scsi_tran->tran_reset		= sata_scsi_reset;
807 	scsi_tran->tran_abort		= sata_scsi_abort;
808 	scsi_tran->tran_getcap		= sata_scsi_getcap;
809 	scsi_tran->tran_setcap		= sata_scsi_setcap;
810 	scsi_tran->tran_init_pkt	= sata_scsi_init_pkt;
811 	scsi_tran->tran_destroy_pkt	= sata_scsi_destroy_pkt;
812 
813 	scsi_tran->tran_dmafree		= sata_scsi_dmafree;
814 	scsi_tran->tran_sync_pkt	= sata_scsi_sync_pkt;
815 
816 	scsi_tran->tran_reset_notify	= NULL;
817 	scsi_tran->tran_get_bus_addr	= NULL;
818 	scsi_tran->tran_quiesce		= NULL;
819 	scsi_tran->tran_unquiesce	= NULL;
820 	scsi_tran->tran_bus_reset	= NULL;
821 
822 	if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr,
823 	    scsi_tran, 0) != DDI_SUCCESS) {
824 #ifdef SATA_DEBUG
825 		cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed",
826 		    ddi_driver_name(dip), ddi_get_instance(dip));
827 #endif
828 		goto fail;
829 	}
830 	hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED;
831 
832 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) {
833 		if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
834 		    "sata", 1) != DDI_PROP_SUCCESS) {
835 			SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: "
836 			    "failed to create hba sata prop"));
837 			goto fail;
838 		}
839 	}
840 
841 	/*
842 	 * Save pointers in hba instance soft state.
843 	 */
844 	sata_hba_inst->satahba_scsi_tran = scsi_tran;
845 	sata_hba_inst->satahba_tran = sata_tran;
846 	sata_hba_inst->satahba_dip = dip;
847 
848 	/*
849 	 * Create a task queue to handle emulated commands completion
850 	 * Use node name, dash, instance number as the queue name.
851 	 */
852 	taskq_name[0] = '\0';
853 	(void) strlcat(taskq_name, DEVI(dip)->devi_node_name,
854 	    sizeof (taskq_name));
855 	(void) snprintf(taskq_name + strlen(taskq_name),
856 	    sizeof (taskq_name) - strlen(taskq_name),
857 	    "-%d", DEVI(dip)->devi_instance);
858 	sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1,
859 	    minclsyspri, 1, sata_tran->sata_tran_hba_num_cports * 4,
860 	    TASKQ_DYNAMIC);
861 
862 	hba_attach_state |= HBA_ATTACH_STAGE_SETUP;
863 
864 	/*
865 	 * Create events thread if not created yet.
866 	 */
867 	sata_event_thread_control(1);
868 
869 	/*
870 	 * Link this hba instance into the list.
871 	 */
872 	mutex_enter(&sata_mutex);
873 
874 	if (sata_hba_list == NULL) {
875 		/*
876 		 * The first instance of HBA is attached.
877 		 * Set current/active default maximum NCQ/TCQ queue depth for
878 		 * all SATA devices. It is done here and now, to eliminate the
879 		 * possibility of the dynamic, programatic modification of the
880 		 * queue depth via global (and public) sata_max_queue_depth
881 		 * variable (this would require special handling in HBA drivers)
882 		 */
883 		sata_current_max_qdepth = sata_max_queue_depth;
884 		if (sata_current_max_qdepth > 32)
885 			sata_current_max_qdepth = 32;
886 		else if (sata_current_max_qdepth < 1)
887 			sata_current_max_qdepth = 1;
888 	}
889 
890 	sata_hba_inst->satahba_next = NULL;
891 	sata_hba_inst->satahba_prev = sata_hba_list_tail;
892 	if (sata_hba_list == NULL) {
893 		sata_hba_list = sata_hba_inst;
894 	}
895 	if (sata_hba_list_tail != NULL) {
896 		sata_hba_list_tail->satahba_next = sata_hba_inst;
897 	}
898 	sata_hba_list_tail = sata_hba_inst;
899 	mutex_exit(&sata_mutex);
900 	hba_attach_state |= HBA_ATTACH_STAGE_LINKED;
901 
902 	/*
903 	 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl
904 	 * SATA HBA driver should not use its own open/close entry points.
905 	 *
906 	 * Make sure that instance number doesn't overflow
907 	 * when forming minor numbers.
908 	 */
909 	ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT));
910 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
911 	    INST2DEVCTL(ddi_get_instance(dip)),
912 	    DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) {
913 #ifdef SATA_DEBUG
914 		cmn_err(CE_WARN, "sata_hba_attach: "
915 		    "cannot create devctl minor node");
916 #endif
917 		goto fail;
918 	}
919 
920 
921 	/*
922 	 * Set-up kstats here, if necessary.
923 	 * (postponed until future phase of the development).
924 	 */
925 
926 	/*
927 	 * Indicate that HBA is attached. This will enable events processing
928 	 * for this HBA.
929 	 */
930 	sata_hba_inst->satahba_attached = 1;
931 	/*
932 	 * Probe controller ports. This operation will describe a current
933 	 * controller/port/multipliers/device configuration and will create
934 	 * attachment points.
935 	 * We may end-up with just a controller with no devices attached.
936 	 * For the ports with a supported device attached, device target nodes
937 	 * are created and devices are initialized.
938 	 */
939 	sata_probe_ports(sata_hba_inst);
940 
941 	return (DDI_SUCCESS);
942 
943 fail:
944 	if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) {
945 		(void) sata_remove_hba_instance(dip);
946 		if (sata_hba_list == NULL)
947 			sata_event_thread_control(0);
948 	}
949 
950 	if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) {
951 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
952 		taskq_destroy(sata_hba_inst->satahba_taskq);
953 	}
954 
955 	if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED)
956 		(void) scsi_hba_detach(dip);
957 
958 	if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) {
959 		mutex_destroy(&sata_hba_inst->satahba_mutex);
960 		kmem_free((void *)sata_hba_inst,
961 		    sizeof (struct sata_hba_inst));
962 		scsi_hba_tran_free(scsi_tran);
963 	}
964 
965 	sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed",
966 	    ddi_driver_name(dip), ddi_get_instance(dip));
967 
968 	return (DDI_FAILURE);
969 }
970 
971 
972 /*
973  * Called by SATA HBA from to detach an instance of the driver.
974  *
975  * For DDI_DETACH command:
976  * Free local structures allocated for SATA HBA instance during
977  * sata_hba_attach processing.
978  *
979  * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise.
980  *
981  * For DDI_SUSPEND command:
982  * Not implemented at this time (postponed until phase 2 of the development)
983  * Returnd DDI_SUCCESS.
984  *
985  * When the last HBA instance is detached, the event daemon is terminated.
986  *
987  * NOTE: Port multiplier is supported.
988  */
989 int
990 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
991 {
992 	dev_info_t	*tdip;
993 	sata_hba_inst_t	*sata_hba_inst;
994 	scsi_hba_tran_t *scsi_hba_tran;
995 	sata_cport_info_t *cportinfo;
996 	sata_pmult_info_t *pminfo;
997 	sata_drive_info_t *sdinfo;
998 	sata_device_t	sdevice;
999 	int ncport, npmport;
1000 
1001 	SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n",
1002 	    ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip));
1003 
1004 	switch (cmd) {
1005 	case DDI_DETACH:
1006 
1007 		if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1008 			return (DDI_FAILURE);
1009 
1010 		sata_hba_inst = scsi_hba_tran->tran_hba_private;
1011 		if (sata_hba_inst == NULL)
1012 			return (DDI_FAILURE);
1013 
1014 		if (scsi_hba_detach(dip) == DDI_FAILURE) {
1015 			sata_hba_inst->satahba_attached = 1;
1016 			return (DDI_FAILURE);
1017 		}
1018 
1019 		/*
1020 		 * Free all target nodes - at this point
1021 		 * devices should be at least offlined
1022 		 * otherwise scsi_hba_detach() should not be called.
1023 		 */
1024 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
1025 		    ncport++) {
1026 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
1027 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
1028 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
1029 				if (sdinfo != NULL) {
1030 					tdip = sata_get_target_dip(dip,
1031 					    ncport, 0);
1032 					if (tdip != NULL) {
1033 						if (ndi_devi_offline(tdip,
1034 						    NDI_DEVI_REMOVE) !=
1035 						    NDI_SUCCESS) {
1036 							SATA_LOG_D((
1037 							    sata_hba_inst,
1038 							    CE_WARN,
1039 							    "sata_hba_detach: "
1040 							    "Target node not "
1041 							    "removed !"));
1042 							return (DDI_FAILURE);
1043 						}
1044 					}
1045 				}
1046 			} else { /* SATA_DTYPE_PMULT */
1047 				mutex_enter(&cportinfo->cport_mutex);
1048 				pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
1049 
1050 				if (pminfo == NULL) {
1051 					SATA_LOG_D((sata_hba_inst, CE_WARN,
1052 					    "sata_hba_detach: Port multiplier "
1053 					    "not ready yet!"));
1054 					mutex_exit(&cportinfo->cport_mutex);
1055 					return (DDI_FAILURE);
1056 				}
1057 
1058 				/*
1059 				 * Detach would fail if removal of any of the
1060 				 * target nodes is failed - albeit in that
1061 				 * case some of them may have been removed.
1062 				 */
1063 				for (npmport = 0; npmport < SATA_NUM_PMPORTS(
1064 				    sata_hba_inst, ncport); npmport++) {
1065 					tdip = sata_get_target_dip(dip, ncport,
1066 					    npmport);
1067 					if (tdip != NULL) {
1068 						if (ndi_devi_offline(tdip,
1069 						    NDI_DEVI_REMOVE) !=
1070 						    NDI_SUCCESS) {
1071 							SATA_LOG_D((
1072 							    sata_hba_inst,
1073 							    CE_WARN,
1074 							    "sata_hba_detach: "
1075 							    "Target node not "
1076 							    "removed !"));
1077 							mutex_exit(&cportinfo->
1078 							    cport_mutex);
1079 							return (DDI_FAILURE);
1080 						}
1081 					}
1082 				}
1083 				mutex_exit(&cportinfo->cport_mutex);
1084 			}
1085 		}
1086 		/*
1087 		 * Disable sata event daemon processing for this HBA
1088 		 */
1089 		sata_hba_inst->satahba_attached = 0;
1090 
1091 		/*
1092 		 * Remove event daemon thread, if it is last HBA instance.
1093 		 */
1094 
1095 		mutex_enter(&sata_mutex);
1096 		if (sata_hba_list->satahba_next == NULL) {
1097 			mutex_exit(&sata_mutex);
1098 			sata_event_thread_control(0);
1099 			mutex_enter(&sata_mutex);
1100 		}
1101 		mutex_exit(&sata_mutex);
1102 
1103 		/* Remove this HBA instance from the HBA list */
1104 		sata_remove_hba_instance(dip);
1105 
1106 		/*
1107 		 * At this point there should be no target nodes attached.
1108 		 * Detach and destroy device and port info structures.
1109 		 */
1110 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
1111 		    ncport++) {
1112 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
1113 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
1114 				sdinfo =
1115 				    cportinfo->cport_devp.cport_sata_drive;
1116 				if (sdinfo != NULL) {
1117 					/* Release device structure */
1118 					kmem_free(sdinfo,
1119 					    sizeof (sata_drive_info_t));
1120 				}
1121 				/* Release cport info */
1122 				mutex_destroy(&cportinfo->cport_mutex);
1123 				kmem_free(cportinfo,
1124 				    sizeof (sata_cport_info_t));
1125 			} else { /* SATA_DTYPE_PMULT */
1126 				sdevice.satadev_addr.cport = (uint8_t)ncport;
1127 				sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
1128 				sata_free_pmult(sata_hba_inst, &sdevice);
1129 			}
1130 		}
1131 
1132 		scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran);
1133 
1134 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
1135 
1136 		taskq_destroy(sata_hba_inst->satahba_taskq);
1137 
1138 		mutex_destroy(&sata_hba_inst->satahba_mutex);
1139 		kmem_free((void *)sata_hba_inst,
1140 		    sizeof (struct sata_hba_inst));
1141 
1142 		return (DDI_SUCCESS);
1143 
1144 	case DDI_SUSPEND:
1145 		/*
1146 		 * Postponed until phase 2
1147 		 */
1148 		return (DDI_FAILURE);
1149 
1150 	default:
1151 		return (DDI_FAILURE);
1152 	}
1153 }
1154 
1155 
1156 /*
1157  * Called by an HBA drive from _fini() routine.
1158  * Unregisters SATA HBA instance/SATA framework pair from the scsi framework.
1159  */
1160 void
1161 sata_hba_fini(struct modlinkage *modlp)
1162 {
1163 	SATADBG1(SATA_DBG_HBA_IF, NULL,
1164 	    "sata_hba_fini: name %s\n",
1165 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
1166 
1167 	scsi_hba_fini(modlp);
1168 }
1169 
1170 
1171 /*
1172  * Default open and close routine for sata_hba framework.
1173  *
1174  */
1175 /*
1176  * Open devctl node.
1177  *
1178  * Returns:
1179  * 0 if node was open successfully, error code otherwise.
1180  *
1181  *
1182  */
1183 
1184 static int
1185 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1186 {
1187 #ifndef __lock_lint
1188 	_NOTE(ARGUNUSED(credp))
1189 #endif
1190 	int rv = 0;
1191 	dev_info_t *dip;
1192 	scsi_hba_tran_t *scsi_hba_tran;
1193 	sata_hba_inst_t	*sata_hba_inst;
1194 
1195 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL);
1196 
1197 	if (otyp != OTYP_CHR)
1198 		return (EINVAL);
1199 
1200 	dip = sata_devt_to_devinfo(*devp);
1201 	if (dip == NULL)
1202 		return (ENXIO);
1203 
1204 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1205 		return (ENXIO);
1206 
1207 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1208 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1209 		return (ENXIO);
1210 
1211 	mutex_enter(&sata_mutex);
1212 	if (flags & FEXCL) {
1213 		if (sata_hba_inst->satahba_open_flag != 0) {
1214 			rv = EBUSY;
1215 		} else {
1216 			sata_hba_inst->satahba_open_flag =
1217 			    SATA_DEVCTL_EXOPENED;
1218 		}
1219 	} else {
1220 		if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) {
1221 			rv = EBUSY;
1222 		} else {
1223 			sata_hba_inst->satahba_open_flag =
1224 			    SATA_DEVCTL_SOPENED;
1225 		}
1226 	}
1227 	mutex_exit(&sata_mutex);
1228 
1229 	return (rv);
1230 }
1231 
1232 
1233 /*
1234  * Close devctl node.
1235  * Returns:
1236  * 0 if node was closed successfully, error code otherwise.
1237  *
1238  */
1239 
1240 static int
1241 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
1242 {
1243 #ifndef __lock_lint
1244 	_NOTE(ARGUNUSED(credp))
1245 	_NOTE(ARGUNUSED(flag))
1246 #endif
1247 	dev_info_t *dip;
1248 	scsi_hba_tran_t *scsi_hba_tran;
1249 	sata_hba_inst_t	*sata_hba_inst;
1250 
1251 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL);
1252 
1253 	if (otyp != OTYP_CHR)
1254 		return (EINVAL);
1255 
1256 	dip = sata_devt_to_devinfo(dev);
1257 	if (dip == NULL)
1258 		return (ENXIO);
1259 
1260 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1261 		return (ENXIO);
1262 
1263 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1264 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1265 		return (ENXIO);
1266 
1267 	mutex_enter(&sata_mutex);
1268 	sata_hba_inst->satahba_open_flag = 0;
1269 	mutex_exit(&sata_mutex);
1270 	return (0);
1271 }
1272 
1273 
1274 
1275 /*
1276  * Standard IOCTL commands for SATA hotplugging.
1277  * Implemented DEVCTL_AP commands:
1278  * DEVCTL_AP_CONNECT
1279  * DEVCTL_AP_DISCONNECT
1280  * DEVCTL_AP_CONFIGURE
1281  * DEVCTL_UNCONFIGURE
1282  * DEVCTL_AP_CONTROL
1283  *
1284  * Commands passed to default ndi ioctl handler:
1285  * DEVCTL_DEVICE_GETSTATE
1286  * DEVCTL_DEVICE_ONLINE
1287  * DEVCTL_DEVICE_OFFLINE
1288  * DEVCTL_DEVICE_REMOVE
1289  * DEVCTL_DEVICE_INSERT
1290  * DEVCTL_BUS_GETSTATE
1291  *
1292  * All other cmds are passed to HBA if it provide ioctl handler, or failed
1293  * if not.
1294  *
1295  * Returns:
1296  * 0 if successful,
1297  * error code if operation failed.
1298  *
1299  * Port Multiplier support is supported now.
1300  *
1301  * NOTE: qual should be SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT
1302  */
1303 
1304 static int
1305 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1306     int *rvalp)
1307 {
1308 #ifndef __lock_lint
1309 	_NOTE(ARGUNUSED(credp))
1310 	_NOTE(ARGUNUSED(rvalp))
1311 #endif
1312 	int rv = 0;
1313 	int32_t	comp_port = -1;
1314 	dev_info_t *dip;
1315 	devctl_ap_state_t ap_state;
1316 	struct devctl_iocdata *dcp = NULL;
1317 	scsi_hba_tran_t *scsi_hba_tran;
1318 	sata_hba_inst_t *sata_hba_inst;
1319 	sata_device_t sata_device;
1320 	sata_cport_info_t *cportinfo;
1321 	int cport, pmport, qual;
1322 	int rval = SATA_SUCCESS;
1323 
1324 	dip = sata_devt_to_devinfo(dev);
1325 	if (dip == NULL)
1326 		return (ENXIO);
1327 
1328 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1329 		return (ENXIO);
1330 
1331 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1332 	if (sata_hba_inst == NULL)
1333 		return (ENXIO);
1334 
1335 	if (sata_hba_inst->satahba_tran == NULL)
1336 		return (ENXIO);
1337 
1338 	switch (cmd) {
1339 
1340 	case DEVCTL_DEVICE_GETSTATE:
1341 	case DEVCTL_DEVICE_ONLINE:
1342 	case DEVCTL_DEVICE_OFFLINE:
1343 	case DEVCTL_DEVICE_REMOVE:
1344 	case DEVCTL_BUS_GETSTATE:
1345 		/*
1346 		 * There may be more cases that we want to pass to default
1347 		 * handler rather than fail them.
1348 		 */
1349 		return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
1350 	}
1351 
1352 	/* read devctl ioctl data */
1353 	if (cmd != DEVCTL_AP_CONTROL && cmd >= DEVCTL_IOC &&
1354 	    cmd <= DEVCTL_IOC_MAX) {
1355 		if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1356 			return (EFAULT);
1357 
1358 		if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) ==
1359 		    -1) {
1360 			if (dcp)
1361 				ndi_dc_freehdl(dcp);
1362 			return (EINVAL);
1363 		}
1364 
1365 		/*
1366 		 * According to SCSI_TO_SATA_ADDR_QUAL, qual should be either
1367 		 * SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT.
1368 		 */
1369 		cport = SCSI_TO_SATA_CPORT(comp_port);
1370 		pmport = SCSI_TO_SATA_PMPORT(comp_port);
1371 		qual = SCSI_TO_SATA_ADDR_QUAL(comp_port);
1372 
1373 		if (sata_validate_sata_address(sata_hba_inst, cport, pmport,
1374 		    qual) != 0) {
1375 			ndi_dc_freehdl(dcp);
1376 			return (EINVAL);
1377 		}
1378 
1379 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1380 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1381 		    cport_mutex);
1382 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1383 			/*
1384 			 * Cannot process ioctl request now. Come back later.
1385 			 */
1386 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1387 			    cport_mutex);
1388 			ndi_dc_freehdl(dcp);
1389 			return (EBUSY);
1390 		}
1391 		/* Block event processing for this port */
1392 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1393 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1394 
1395 		sata_device.satadev_addr.cport = cport;
1396 		sata_device.satadev_addr.pmport = pmport;
1397 		sata_device.satadev_addr.qual = qual;
1398 		sata_device.satadev_rev = SATA_DEVICE_REV;
1399 	}
1400 
1401 	switch (cmd) {
1402 
1403 	case DEVCTL_AP_DISCONNECT:
1404 
1405 		/*
1406 		 * Normally, cfgadm sata plugin will try to offline
1407 		 * (unconfigure) device before this request. Nevertheless,
1408 		 * if a device is still configured, we need to
1409 		 * attempt to offline and unconfigure device first, and we will
1410 		 * deactivate the port regardless of the unconfigure
1411 		 * operation results.
1412 		 *
1413 		 */
1414 		rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device);
1415 
1416 		break;
1417 
1418 	case DEVCTL_AP_UNCONFIGURE:
1419 
1420 		/*
1421 		 * The unconfigure operation uses generic nexus operation to
1422 		 * offline a device. It leaves a target device node attached.
1423 		 * and obviously sata_drive_info attached as well, because
1424 		 * from the hardware point of view nothing has changed.
1425 		 */
1426 		rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device);
1427 		break;
1428 
1429 	case DEVCTL_AP_CONNECT:
1430 	{
1431 		/*
1432 		 * The sata cfgadm pluging will invoke this operation only if
1433 		 * port was found in the disconnect state (failed state
1434 		 * is also treated as the disconnected state).
1435 		 * If port activation is successful and a device is found
1436 		 * attached to the port, the initialization sequence is
1437 		 * executed to probe the port and attach
1438 		 * a device structure to a port structure. The device is not
1439 		 * set in configured state (system-wise) by this operation.
1440 		 */
1441 
1442 		rv = sata_ioctl_connect(sata_hba_inst, &sata_device);
1443 
1444 		break;
1445 	}
1446 
1447 	case DEVCTL_AP_CONFIGURE:
1448 	{
1449 		/*
1450 		 * A port may be in an active or shutdown state.
1451 		 * If port is in a failed state, operation is aborted.
1452 		 * If a port is in a shutdown state, sata_tran_port_activate()
1453 		 * is invoked prior to any other operation.
1454 		 *
1455 		 * Onlining the device involves creating a new target node.
1456 		 * If there is an old target node present (belonging to
1457 		 * previously removed device), the operation is aborted - the
1458 		 * old node has to be released and removed before configure
1459 		 * operation is attempted.
1460 		 */
1461 
1462 		rv = sata_ioctl_configure(sata_hba_inst, &sata_device);
1463 
1464 		break;
1465 	}
1466 
1467 	case DEVCTL_AP_GETSTATE:
1468 
1469 		sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state);
1470 
1471 		ap_state.ap_last_change = (time_t)-1;
1472 		ap_state.ap_error_code = 0;
1473 		ap_state.ap_in_transition = 0;
1474 
1475 		/* Copy the return AP-state information to the user space */
1476 		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
1477 			rv = EFAULT;
1478 		}
1479 		break;
1480 
1481 	case DEVCTL_AP_CONTROL:
1482 	{
1483 		/*
1484 		 * Generic devctl for hardware specific functionality
1485 		 */
1486 		sata_ioctl_data_t	ioc;
1487 
1488 		ASSERT(dcp == NULL);
1489 
1490 		/* Copy in user ioctl data first */
1491 #ifdef _MULTI_DATAMODEL
1492 		if (ddi_model_convert_from(mode & FMODELS) ==
1493 		    DDI_MODEL_ILP32) {
1494 
1495 			sata_ioctl_data_32_t	ioc32;
1496 
1497 			if (ddi_copyin((void *)arg, (void *)&ioc32,
1498 			    sizeof (ioc32), mode) != 0) {
1499 				rv = EFAULT;
1500 				break;
1501 			}
1502 			ioc.cmd		= (uint_t)ioc32.cmd;
1503 			ioc.port	= (uint_t)ioc32.port;
1504 			ioc.get_size	= (uint_t)ioc32.get_size;
1505 			ioc.buf		= (caddr_t)(uintptr_t)ioc32.buf;
1506 			ioc.bufsiz	= (uint_t)ioc32.bufsiz;
1507 			ioc.misc_arg	= (uint_t)ioc32.misc_arg;
1508 		} else
1509 #endif /* _MULTI_DATAMODEL */
1510 		if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
1511 		    mode) != 0) {
1512 			return (EFAULT);
1513 		}
1514 
1515 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
1516 		    "sata_hba_ioctl: DEVCTL_AP_CONTROL "
1517 		    "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port);
1518 
1519 		/*
1520 		 * To avoid BE/LE and 32/64 issues, a get_size always returns
1521 		 * a 32-bit number.
1522 		 */
1523 		if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
1524 			return (EINVAL);
1525 		}
1526 		/* validate address */
1527 		cport = SCSI_TO_SATA_CPORT(ioc.port);
1528 		pmport = SCSI_TO_SATA_PMPORT(ioc.port);
1529 		qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port);
1530 
1531 		SATADBG3(SATA_DBG_IOCTL_IF, sata_hba_inst,
1532 		    "sata_hba_ioctl: target port is %d:%d (%d)",
1533 		    cport, pmport, qual);
1534 
1535 		if (sata_validate_sata_address(sata_hba_inst, cport,
1536 		    pmport, qual) != 0)
1537 			return (EINVAL);
1538 
1539 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1540 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1541 		    cport_mutex);
1542 		/* Is the port locked by event processing daemon ? */
1543 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1544 			/*
1545 			 * Cannot process ioctl request now. Come back later
1546 			 */
1547 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1548 			    cport_mutex);
1549 			return (EBUSY);
1550 		}
1551 		/* Block event processing for this port */
1552 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1553 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1554 
1555 
1556 		sata_device.satadev_addr.cport = cport;
1557 		sata_device.satadev_addr.pmport = pmport;
1558 		sata_device.satadev_addr.qual = qual;
1559 		sata_device.satadev_rev = SATA_DEVICE_REV;
1560 
1561 		switch (ioc.cmd) {
1562 
1563 		case SATA_CFGA_RESET_PORT:
1564 			/*
1565 			 * There is no protection for configured device.
1566 			 */
1567 			rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device);
1568 			break;
1569 
1570 		case SATA_CFGA_RESET_DEVICE:
1571 			/*
1572 			 * There is no protection for configured device.
1573 			 */
1574 			rv = sata_ioctl_reset_device(sata_hba_inst,
1575 			    &sata_device);
1576 			break;
1577 
1578 		case SATA_CFGA_RESET_ALL:
1579 			/*
1580 			 * There is no protection for configured devices.
1581 			 */
1582 			rv = sata_ioctl_reset_all(sata_hba_inst);
1583 			/*
1584 			 * We return here, because common return is for
1585 			 * a single port operation - we have already unlocked
1586 			 * all ports and no dc handle was allocated.
1587 			 */
1588 			return (rv);
1589 
1590 		case SATA_CFGA_PORT_DEACTIVATE:
1591 			/*
1592 			 * Arbitrarily unconfigure attached device, if any.
1593 			 * Even if the unconfigure fails, proceed with the
1594 			 * port deactivation.
1595 			 */
1596 			rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device);
1597 
1598 			break;
1599 
1600 		case SATA_CFGA_PORT_ACTIVATE:
1601 
1602 			rv = sata_ioctl_activate(sata_hba_inst, &sata_device);
1603 			break;
1604 
1605 		case SATA_CFGA_PORT_SELF_TEST:
1606 
1607 			rv = sata_ioctl_port_self_test(sata_hba_inst,
1608 			    &sata_device);
1609 			break;
1610 
1611 		case SATA_CFGA_GET_DEVICE_PATH:
1612 
1613 			rv = sata_ioctl_get_device_path(sata_hba_inst,
1614 			    &sata_device, &ioc, mode);
1615 			break;
1616 
1617 		case SATA_CFGA_GET_AP_TYPE:
1618 
1619 			rv = sata_ioctl_get_ap_type(sata_hba_inst,
1620 			    &sata_device, &ioc, mode);
1621 			break;
1622 
1623 		case SATA_CFGA_GET_MODEL_INFO:
1624 
1625 			rv = sata_ioctl_get_model_info(sata_hba_inst,
1626 			    &sata_device, &ioc, mode);
1627 			break;
1628 
1629 		case SATA_CFGA_GET_REVFIRMWARE_INFO:
1630 
1631 			rv = sata_ioctl_get_revfirmware_info(sata_hba_inst,
1632 			    &sata_device, &ioc, mode);
1633 			break;
1634 
1635 		case SATA_CFGA_GET_SERIALNUMBER_INFO:
1636 
1637 			rv = sata_ioctl_get_serialnumber_info(sata_hba_inst,
1638 			    &sata_device, &ioc, mode);
1639 			break;
1640 
1641 		default:
1642 			rv = EINVAL;
1643 			break;
1644 
1645 		} /* End of DEVCTL_AP_CONTROL cmd switch */
1646 
1647 		break;
1648 	}
1649 
1650 	default:
1651 	{
1652 		/*
1653 		 * If we got here, we got an IOCTL that SATA HBA Framework
1654 		 * does not recognize. Pass ioctl to HBA driver, in case
1655 		 * it could process it.
1656 		 */
1657 		sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran;
1658 		dev_info_t	*mydip = SATA_DIP(sata_hba_inst);
1659 
1660 		SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1661 		    "IOCTL 0x%2x not supported in SATA framework, "
1662 		    "passthrough to HBA", cmd);
1663 
1664 		if (sata_tran->sata_tran_ioctl == NULL) {
1665 			rv = EINVAL;
1666 			break;
1667 		}
1668 		rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg);
1669 		if (rval != 0) {
1670 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1671 			    "IOCTL 0x%2x failed in HBA", cmd);
1672 			rv = rval;
1673 		}
1674 		break;
1675 	}
1676 
1677 	} /* End of main IOCTL switch */
1678 
1679 	if (dcp) {
1680 		ndi_dc_freehdl(dcp);
1681 	}
1682 
1683 	if (cmd >= DEVCTL_IOC && cmd <= DEVCTL_IOC_MAX) {
1684 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
1685 		    cport)->cport_mutex);
1686 		cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
1687 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1688 	}
1689 
1690 	return (rv);
1691 }
1692 
1693 
1694 /*
1695  * Create error retrieval sata packet
1696  *
1697  * A sata packet is allocated and set-up to contain specified error retrieval
1698  * command and appropriate dma-able data buffer.
1699  * No association with any scsi packet is made and no callback routine is
1700  * specified.
1701  *
1702  * Returns a pointer to sata packet upon successful packet creation.
1703  * Returns NULL, if packet cannot be created.
1704  */
1705 sata_pkt_t *
1706 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device,
1707     int pkt_type)
1708 {
1709 	sata_hba_inst_t	*sata_hba_inst;
1710 	sata_pkt_txlate_t *spx;
1711 	sata_pkt_t *spkt;
1712 	sata_drive_info_t *sdinfo;
1713 
1714 	mutex_enter(&sata_mutex);
1715 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1716 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1717 		if (SATA_DIP(sata_hba_inst) == dip)
1718 			break;
1719 	}
1720 	mutex_exit(&sata_mutex);
1721 	ASSERT(sata_hba_inst != NULL);
1722 
1723 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
1724 	if (sdinfo == NULL) {
1725 		sata_log(sata_hba_inst, CE_WARN,
1726 		    "sata: error recovery request for non-attached device at "
1727 		    "cport %d", sata_device->satadev_addr.cport);
1728 		return (NULL);
1729 	}
1730 
1731 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1732 	spx->txlt_sata_hba_inst = sata_hba_inst;
1733 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
1734 	spkt = sata_pkt_alloc(spx, NULL);
1735 	if (spkt == NULL) {
1736 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1737 		return (NULL);
1738 	}
1739 	/* address is needed now */
1740 	spkt->satapkt_device.satadev_addr = sata_device->satadev_addr;
1741 
1742 	switch (pkt_type) {
1743 	case SATA_ERR_RETR_PKT_TYPE_NCQ:
1744 		if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) {
1745 			if (sata_check_for_dma_error(dip, spx)) {
1746 				ddi_fm_service_impact(dip,
1747 				    DDI_SERVICE_UNAFFECTED);
1748 				break;
1749 			}
1750 			return (spkt);
1751 		}
1752 		break;
1753 
1754 	case SATA_ERR_RETR_PKT_TYPE_ATAPI:
1755 		if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) {
1756 			if (sata_check_for_dma_error(dip, spx)) {
1757 				ddi_fm_service_impact(dip,
1758 				    DDI_SERVICE_UNAFFECTED);
1759 				break;
1760 			}
1761 			return (spkt);
1762 		}
1763 		break;
1764 
1765 	default:
1766 		break;
1767 	}
1768 
1769 	sata_pkt_free(spx);
1770 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1771 	return (NULL);
1772 
1773 }
1774 
1775 
1776 /*
1777  * Free error retrieval sata packet
1778  *
1779  * Free sata packet and any associated resources allocated previously by
1780  * sata_get_error_retrieval_pkt().
1781  *
1782  * Void return.
1783  */
1784 void
1785 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt)
1786 {
1787 	sata_pkt_txlate_t *spx =
1788 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1789 
1790 	ASSERT(sata_pkt != NULL);
1791 
1792 	sata_free_local_buffer(spx);
1793 	sata_pkt_free(spx);
1794 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1795 
1796 }
1797 
1798 /*
1799  * Create READ PORT MULTIPLIER and WRITE PORT MULTIPLIER sata packet
1800  *
1801  * No association with any scsi packet is made and no callback routine is
1802  * specified.
1803  *
1804  * Returns a pointer to sata packet upon successful packet creation.
1805  * Returns NULL, if packet cannot be created.
1806  *
1807  * NOTE: Input/Output value includes 64 bits accoring to SATA Spec 2.6,
1808  * only lower 32 bits are available currently.
1809  */
1810 sata_pkt_t *
1811 sata_get_rdwr_pmult_pkt(dev_info_t *dip, sata_device_t *sd,
1812     uint8_t regn, uint32_t regv, uint32_t type)
1813 {
1814 	sata_hba_inst_t	*sata_hba_inst;
1815 	sata_pkt_txlate_t *spx;
1816 	sata_pkt_t *spkt;
1817 	sata_cmd_t *scmd;
1818 
1819 	/* Only READ/WRITE commands are accepted. */
1820 	ASSERT(type == SATA_RDWR_PMULT_PKT_TYPE_READ ||
1821 	    type == SATA_RDWR_PMULT_PKT_TYPE_WRITE);
1822 
1823 	mutex_enter(&sata_mutex);
1824 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1825 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1826 		if (SATA_DIP(sata_hba_inst) == dip)
1827 			break;
1828 	}
1829 	mutex_exit(&sata_mutex);
1830 	ASSERT(sata_hba_inst != NULL);
1831 
1832 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1833 	spx->txlt_sata_hba_inst = sata_hba_inst;
1834 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
1835 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
1836 	if (spkt == NULL) {
1837 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1838 		return (NULL);
1839 	}
1840 
1841 	/*
1842 	 * NOTE: We need to send this command to the port multiplier,
1843 	 * that means send to SATA_PMULT_HOSTPORT(0xf) pmport
1844 	 *
1845 	 * sata_device contains the address of actual target device, and the
1846 	 * pmport number in the command comes from the sata_device structure.
1847 	 */
1848 	spkt->satapkt_device.satadev_addr = sd->satadev_addr;
1849 	spkt->satapkt_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
1850 	spkt->satapkt_device.satadev_addr.qual = SATA_ADDR_PMULT;
1851 
1852 	/* Fill sata_pkt */
1853 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_POLLING;
1854 	spkt->satapkt_comp = NULL; /* Synchronous mode, no callback */
1855 	spkt->satapkt_time = 10; /* Timeout 10s */
1856 
1857 	/* Build READ PORT MULTIPLIER cmd in the sata_pkt */
1858 	scmd = &spkt->satapkt_cmd;
1859 	scmd->satacmd_features_reg = regn & 0xff;
1860 	scmd->satacmd_features_reg_ext = (regn >> 8) & 0xff;
1861 	scmd->satacmd_device_reg = sd->satadev_addr.pmport;
1862 	scmd->satacmd_addr_type = 0;		/* N/A */
1863 
1864 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
1865 
1866 	if (type == SATA_RDWR_PMULT_PKT_TYPE_READ) {
1867 		scmd->satacmd_cmd_reg = SATAC_READ_PORTMULT;
1868 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
1869 		scmd->satacmd_flags.sata_special_regs = 1;
1870 		scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1;
1871 		scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1;
1872 		scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1;
1873 		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1;
1874 	} else if (type == SATA_RDWR_PMULT_PKT_TYPE_WRITE) {
1875 		scmd->satacmd_cmd_reg = SATAC_WRITE_PORTMULT;
1876 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
1877 		scmd->satacmd_sec_count_lsb = regv & 0xff;
1878 		scmd->satacmd_lba_low_lsb = regv >> 8 & 0xff;
1879 		scmd->satacmd_lba_mid_lsb = regv >> 16 & 0xff;
1880 		scmd->satacmd_lba_high_lsb = regv >> 24 & 0xff;
1881 	}
1882 
1883 	return (spkt);
1884 }
1885 
1886 /*
1887  * Free sata packet and any associated resources allocated previously by
1888  * sata_get_rdwr_pmult_pkt().
1889  *
1890  * Void return.
1891  */
1892 void
1893 sata_free_rdwr_pmult_pkt(sata_pkt_t *sata_pkt)
1894 {
1895 	sata_pkt_txlate_t *spx =
1896 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1897 
1898 	/* Free allocated resources */
1899 	sata_pkt_free(spx);
1900 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1901 }
1902 
1903 /*
1904  * Register a port multiplier to framework.
1905  * 1) Store the GSCR values in the previous allocated pmult_info strctures.
1906  * 2) Search in the blacklist and update the number of the device ports of the
1907  * port multiplier.
1908  *
1909  * Void return.
1910  */
1911 void
1912 sata_register_pmult(dev_info_t *dip, sata_device_t *sd, sata_pmult_gscr_t *sg)
1913 {
1914 	sata_hba_inst_t *sata_hba_inst = NULL;
1915 	sata_pmult_info_t *pmultinfo;
1916 	sata_pmult_bl_t *blp;
1917 	int cport = sd->satadev_addr.cport;
1918 
1919 	mutex_enter(&sata_mutex);
1920 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1921 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1922 		if (SATA_DIP(sata_hba_inst) == dip)
1923 			if (sata_hba_inst->satahba_attached == 1)
1924 				break;
1925 	}
1926 	mutex_exit(&sata_mutex);
1927 	/* HBA not attached? */
1928 	if (sata_hba_inst == NULL)
1929 		return;
1930 
1931 	/* Number of pmports */
1932 	sd->satadev_add_info = sg->gscr2 & SATA_PMULT_PORTNUM_MASK;
1933 
1934 	/* Check the blacklist */
1935 	for (blp = sata_pmult_blacklist; blp->bl_gscr0; blp++) {
1936 		if (sg->gscr0 != blp->bl_gscr0 && blp->bl_gscr0)
1937 			continue;
1938 		if (sg->gscr1 != blp->bl_gscr1 && blp->bl_gscr1)
1939 			continue;
1940 		if (sg->gscr2 != blp->bl_gscr2 && blp->bl_gscr2)
1941 			continue;
1942 
1943 		cmn_err(CE_WARN, "!Port multiplier is on the blacklist.");
1944 		sd->satadev_add_info = blp->bl_flags;
1945 		break;
1946 	}
1947 
1948 	/* Register the port multiplier GSCR */
1949 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
1950 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
1951 	if (pmultinfo != NULL) {
1952 		pmultinfo->pmult_gscr = *sg;
1953 		pmultinfo->pmult_num_dev_ports =
1954 		    sd->satadev_add_info & SATA_PMULT_PORTNUM_MASK;
1955 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
1956 		    "Port multiplier registered at port %d", cport);
1957 	}
1958 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
1959 }
1960 
1961 /*
1962  * sata_split_model splits the model ID into vendor and product IDs.
1963  * It assumes that a vendor ID cannot be longer than 8 characters, and
1964  * that vendor and product ID are separated by a whitespace.
1965  */
1966 void
1967 sata_split_model(char *model, char **vendor, char **product)
1968 {
1969 	int i, modlen;
1970 	char *vid, *pid;
1971 
1972 	/*
1973 	 * remove whitespace at the end of model
1974 	 */
1975 	for (i = SATA_ID_MODEL_LEN; i > 0; i--)
1976 		if (model[i] == ' ' || model[i] == '\t' || model[i] == '\0')
1977 			model[i] = '\0';
1978 		else
1979 			break;
1980 
1981 	/*
1982 	 * try to split model into into vid/pid
1983 	 */
1984 	modlen = strlen(model);
1985 	for (i = 0, pid = model; i < modlen; i++, pid++)
1986 		if ((*pid == ' ') || (*pid == '\t'))
1987 			break;
1988 
1989 	/*
1990 	 * only use vid if it is less than 8 chars (as in SCSI)
1991 	 */
1992 	if (i < modlen && i <= 8) {
1993 		vid = model;
1994 		/*
1995 		 * terminate vid, establish pid
1996 		 */
1997 		*pid++ = '\0';
1998 	} else {
1999 		/*
2000 		 * vid will stay "ATA     "
2001 		 */
2002 		vid = NULL;
2003 		/*
2004 		 * model is all pid
2005 		 */
2006 		pid = model;
2007 	}
2008 
2009 	*vendor = vid;
2010 	*product = pid;
2011 }
2012 
2013 /*
2014  * sata_name_child is for composing the name of the node
2015  * the format of the name is "target,0".
2016  */
2017 static int
2018 sata_name_child(dev_info_t *dip, char *name, int namelen)
2019 {
2020 	int target;
2021 
2022 	target = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
2023 	    DDI_PROP_DONTPASS, "target", -1);
2024 	if (target == -1)
2025 		return (DDI_FAILURE);
2026 	(void) snprintf(name, namelen, "%x,0", target);
2027 	return (DDI_SUCCESS);
2028 }
2029 
2030 
2031 
2032 /* ****************** SCSA required entry points *********************** */
2033 
2034 /*
2035  * Implementation of scsi tran_tgt_init.
2036  * sata_scsi_tgt_init() initializes scsi_device structure
2037  *
2038  * If successful, DDI_SUCCESS is returned.
2039  * DDI_FAILURE is returned if addressed device does not exist
2040  */
2041 
2042 static int
2043 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2044     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2045 {
2046 #ifndef __lock_lint
2047 	_NOTE(ARGUNUSED(hba_dip))
2048 	_NOTE(ARGUNUSED(tgt_dip))
2049 #endif
2050 	sata_device_t		sata_device;
2051 	sata_drive_info_t	*sdinfo;
2052 	struct sata_id		*sid;
2053 	sata_hba_inst_t		*sata_hba_inst;
2054 	char			model[SATA_ID_MODEL_LEN + 1];
2055 	char			fw[SATA_ID_FW_LEN + 1];
2056 	char			*vid, *pid;
2057 
2058 	/*
2059 	 * Fail tran_tgt_init for .conf stub node
2060 	 */
2061 	if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
2062 		(void) ndi_merge_node(tgt_dip, sata_name_child);
2063 		ddi_set_name_addr(tgt_dip, NULL);
2064 		return (DDI_FAILURE);
2065 	}
2066 
2067 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2068 
2069 	/* Validate scsi device address */
2070 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2071 	    &sata_device) != 0)
2072 		return (DDI_FAILURE);
2073 
2074 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2075 	    sata_device.satadev_addr.cport)));
2076 
2077 	/* sata_device now contains a valid sata address */
2078 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2079 	if (sdinfo == NULL) {
2080 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2081 		    sata_device.satadev_addr.cport)));
2082 		return (DDI_FAILURE);
2083 	}
2084 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2085 	    sata_device.satadev_addr.cport)));
2086 
2087 	/*
2088 	 * Check if we need to create a legacy devid (i.e cmdk style) for
2089 	 * the target disks.
2090 	 *
2091 	 * HBA devinfo node will have the property "use-cmdk-devid-format"
2092 	 * if we need to create cmdk-style devid for all the disk devices
2093 	 * attached to this controller. This property may have been set
2094 	 * from HBA driver's .conf file or by the HBA driver in its
2095 	 * attach(9F) function.
2096 	 */
2097 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2098 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2099 	    "use-cmdk-devid-format", 0) == 1)) {
2100 		/* register a legacy devid for this target node */
2101 		sata_target_devid_register(tgt_dip, sdinfo);
2102 	}
2103 
2104 
2105 	/*
2106 	 * 'Identify Device Data' does not always fit in standard SCSI
2107 	 * INQUIRY data, so establish INQUIRY_* properties with full-form
2108 	 * of information.
2109 	 */
2110 	sid = &sdinfo->satadrv_id;
2111 #ifdef	_LITTLE_ENDIAN
2112 	swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
2113 	swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
2114 #else	/* _LITTLE_ENDIAN */
2115 	bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN);
2116 	bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN);
2117 #endif	/* _LITTLE_ENDIAN */
2118 	model[SATA_ID_MODEL_LEN] = 0;
2119 	fw[SATA_ID_FW_LEN] = 0;
2120 
2121 	sata_split_model(model, &vid, &pid);
2122 
2123 	if (vid)
2124 		(void) scsi_device_prop_update_inqstring(sd, INQUIRY_VENDOR_ID,
2125 		    vid, strlen(vid));
2126 	if (pid)
2127 		(void) scsi_device_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID,
2128 		    pid, strlen(pid));
2129 	(void) scsi_device_prop_update_inqstring(sd, INQUIRY_REVISION_ID,
2130 	    fw, strlen(fw));
2131 
2132 	return (DDI_SUCCESS);
2133 }
2134 
2135 /*
2136  * Implementation of scsi tran_tgt_probe.
2137  * Probe target, by calling default scsi routine scsi_hba_probe()
2138  */
2139 static int
2140 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void))
2141 {
2142 	sata_hba_inst_t *sata_hba_inst =
2143 	    (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private);
2144 	int rval;
2145 	uint32_t pm_cap;
2146 
2147 	rval = scsi_hba_probe(sd, callback);
2148 	pm_cap = SATA_CAP_POWER_CONDITON | SATA_CAP_SMART_PAGE |
2149 	    SATA_CAP_LOG_SENSE;
2150 
2151 	if (rval == SCSIPROBE_EXISTS) {
2152 		/*
2153 		 * Set property "pm-capable" on the target device node, so that
2154 		 * the target driver will not try to fetch scsi cycle counters
2155 		 * before enabling device power-management.
2156 		 */
2157 		if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
2158 		    "pm-capable", pm_cap)) != DDI_PROP_SUCCESS) {
2159 			sata_log(sata_hba_inst, CE_WARN,
2160 			    "SATA device at port %d: "
2161 			    "will not be power-managed ",
2162 			    SCSI_TO_SATA_CPORT(sd->sd_address.a_target));
2163 			SATA_LOG_D((sata_hba_inst, CE_WARN,
2164 			    "failure updating pm-capable property"));
2165 		}
2166 	}
2167 	return (rval);
2168 }
2169 
2170 /*
2171  * Implementation of scsi tran_tgt_free.
2172  * Release all resources allocated for scsi_device
2173  */
2174 static void
2175 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2176     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2177 {
2178 #ifndef __lock_lint
2179 	_NOTE(ARGUNUSED(hba_dip))
2180 #endif
2181 	sata_device_t		sata_device;
2182 	sata_drive_info_t	*sdinfo;
2183 	sata_hba_inst_t		*sata_hba_inst;
2184 	ddi_devid_t		devid;
2185 
2186 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2187 
2188 	/* Validate scsi device address */
2189 	/*
2190 	 * Note: tgt_free relates to the SCSA view of a device. If called, there
2191 	 * was a device at this address, so even if the sata framework internal
2192 	 * resources were alredy released because a device was detached,
2193 	 * this function should be executed as long as its actions do
2194 	 * not require the internal sata view of a device and the address
2195 	 * refers to a valid sata address.
2196 	 * Validating the address here means that we do not trust SCSA...
2197 	 */
2198 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2199 	    &sata_device) == -1)
2200 		return;
2201 
2202 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2203 	    sata_device.satadev_addr.cport)));
2204 
2205 	/* sata_device now should contain a valid sata address */
2206 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2207 	if (sdinfo == NULL) {
2208 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2209 		    sata_device.satadev_addr.cport)));
2210 		return;
2211 	}
2212 	/*
2213 	 * We did not allocate any resources in sata_scsi_tgt_init()
2214 	 * other than few properties.
2215 	 * Free them.
2216 	 */
2217 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2218 	    sata_device.satadev_addr.cport)));
2219 	(void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable");
2220 
2221 	/*
2222 	 * If devid was previously created but not freed up from
2223 	 * sd(7D) driver (i.e during detach(9F)) then do it here.
2224 	 */
2225 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2226 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2227 	    "use-cmdk-devid-format", 0) == 1) &&
2228 	    (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) {
2229 		ddi_devid_unregister(tgt_dip);
2230 		ddi_devid_free(devid);
2231 	}
2232 }
2233 
2234 /*
2235  * Implementation of scsi tran_init_pkt
2236  * Upon successful return, scsi pkt buffer has DMA resources allocated.
2237  *
2238  * It seems that we should always allocate pkt, even if the address is
2239  * for non-existing device - just use some default for dma_attr.
2240  * The reason is that there is no way to communicate this to a caller here.
2241  * Subsequent call to sata_scsi_start may fail appropriately.
2242  * Simply returning NULL does not seem to discourage a target driver...
2243  *
2244  * Returns a pointer to initialized scsi_pkt, or NULL otherwise.
2245  */
2246 static struct scsi_pkt *
2247 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
2248     struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
2249     int (*callback)(caddr_t), caddr_t arg)
2250 {
2251 	sata_hba_inst_t *sata_hba_inst =
2252 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2253 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
2254 	sata_device_t sata_device;
2255 	sata_drive_info_t *sdinfo;
2256 	sata_pkt_txlate_t *spx;
2257 	ddi_dma_attr_t cur_dma_attr;
2258 	int rval;
2259 	boolean_t new_pkt = B_TRUE;
2260 
2261 	ASSERT(ap->a_hba_tran->tran_hba_dip == dip);
2262 
2263 	/*
2264 	 * We need to translate the address, even if it could be
2265 	 * a bogus one, for a non-existing device
2266 	 */
2267 	sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
2268 	sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target);
2269 	sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2270 	sata_device.satadev_rev = SATA_DEVICE_REV;
2271 
2272 	if (pkt == NULL) {
2273 		/*
2274 		 * Have to allocate a brand new scsi packet.
2275 		 * We need to operate with auto request sense enabled.
2276 		 */
2277 		pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen,
2278 		    MAX(statuslen, SATA_MAX_SENSE_LEN),
2279 		    tgtlen, sizeof (sata_pkt_txlate_t), callback, arg);
2280 
2281 		if (pkt == NULL)
2282 			return (NULL);
2283 
2284 		/* Fill scsi packet structure */
2285 		pkt->pkt_comp		= (void (*)())NULL;
2286 		pkt->pkt_time		= 0;
2287 		pkt->pkt_resid		= 0;
2288 		pkt->pkt_statistics	= 0;
2289 		pkt->pkt_reason		= 0;
2290 
2291 		/*
2292 		 * pkt_hba_private will point to sata pkt txlate structure
2293 		 */
2294 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2295 		bzero(spx, sizeof (sata_pkt_txlate_t));
2296 
2297 		spx->txlt_scsi_pkt = pkt;
2298 		spx->txlt_sata_hba_inst = sata_hba_inst;
2299 
2300 		/* Allocate sata_pkt */
2301 		spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback);
2302 		if (spx->txlt_sata_pkt == NULL) {
2303 			/* Could not allocate sata pkt */
2304 			scsi_hba_pkt_free(ap, pkt);
2305 			return (NULL);
2306 		}
2307 		/* Set sata address */
2308 		spx->txlt_sata_pkt->satapkt_device.satadev_addr =
2309 		    sata_device.satadev_addr;
2310 		spx->txlt_sata_pkt->satapkt_device.satadev_rev =
2311 		    sata_device.satadev_rev;
2312 
2313 		if ((bp == NULL) || (bp->b_bcount == 0))
2314 			return (pkt);
2315 
2316 		spx->txlt_total_residue = bp->b_bcount;
2317 	} else {
2318 		new_pkt = B_FALSE;
2319 		/*
2320 		 * Packet was preallocated/initialized by previous call
2321 		 */
2322 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2323 
2324 		if ((bp == NULL) || (bp->b_bcount == 0)) {
2325 			return (pkt);
2326 		}
2327 
2328 		/* Pkt is available already: spx->txlt_scsi_pkt == pkt; */
2329 	}
2330 
2331 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
2332 
2333 	/*
2334 	 * We use an adjusted version of the dma_attr, to account
2335 	 * for device addressing limitations.
2336 	 * sata_adjust_dma_attr() will handle sdinfo == NULL which may
2337 	 * happen when a device is not yet configured.
2338 	 */
2339 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2340 	    sata_device.satadev_addr.cport)));
2341 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2342 	    &spx->txlt_sata_pkt->satapkt_device);
2343 	/* NULL sdinfo may be passsed to sata_adjust_dma_attr() */
2344 	sata_adjust_dma_attr(sdinfo,
2345 	    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
2346 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2347 	    sata_device.satadev_addr.cport)));
2348 	/*
2349 	 * Allocate necessary DMA resources for the packet's data buffer
2350 	 * NOTE:
2351 	 * In case of read/write commands, DMA resource allocation here is
2352 	 * based on the premise that the transfer length specified in
2353 	 * the read/write scsi cdb will match exactly DMA resources -
2354 	 * returning correct packet residue is crucial.
2355 	 */
2356 	if ((rval = sata_dma_buf_setup(spx, flags, callback, arg,
2357 	    &cur_dma_attr)) != DDI_SUCCESS) {
2358 		/*
2359 		 * If a DMA allocation request fails with
2360 		 * DDI_DMA_NOMAPPING, indicate the error by calling
2361 		 * bioerror(9F) with bp and an error code of EFAULT.
2362 		 * If a DMA allocation request fails with
2363 		 * DDI_DMA_TOOBIG, indicate the error by calling
2364 		 * bioerror(9F) with bp and an error code of EINVAL.
2365 		 * For DDI_DMA_NORESOURCES, we may have some of them allocated.
2366 		 * Request may be repeated later - there is no real error.
2367 		 */
2368 		switch (rval) {
2369 		case DDI_DMA_NORESOURCES:
2370 			bioerror(bp, 0);
2371 			break;
2372 		case DDI_DMA_NOMAPPING:
2373 		case DDI_DMA_BADATTR:
2374 			bioerror(bp, EFAULT);
2375 			break;
2376 		case DDI_DMA_TOOBIG:
2377 		default:
2378 			bioerror(bp, EINVAL);
2379 			break;
2380 		}
2381 		goto fail;
2382 	}
2383 
2384 	if (sata_check_for_dma_error(dip, spx)) {
2385 		ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED);
2386 		bioerror(bp, EFAULT);
2387 		goto fail;
2388 	}
2389 
2390 success:
2391 	/* Set number of bytes that are not yet accounted for */
2392 	pkt->pkt_resid = spx->txlt_total_residue;
2393 	ASSERT(pkt->pkt_resid >= 0);
2394 
2395 	return (pkt);
2396 
2397 fail:
2398 	if (new_pkt == B_TRUE) {
2399 		/*
2400 		 * Since this is a new packet, we can clean-up
2401 		 * everything
2402 		 */
2403 		sata_scsi_destroy_pkt(ap, pkt);
2404 	} else {
2405 		/*
2406 		 * This is a re-used packet. It will be target driver's
2407 		 * responsibility to eventually destroy it (which
2408 		 * will free allocated resources).
2409 		 * Here, we just "complete" the request, leaving
2410 		 * allocated resources intact, so the request may
2411 		 * be retried.
2412 		 */
2413 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2414 		sata_pkt_free(spx);
2415 	}
2416 	return (NULL);
2417 }
2418 
2419 /*
2420  * Implementation of scsi tran_start.
2421  * Translate scsi cmd into sata operation and return status.
2422  * ATAPI CDBs are passed to ATAPI devices - the device determines what commands
2423  * are supported.
2424  * For SATA hard disks, supported scsi commands:
2425  * SCMD_INQUIRY
2426  * SCMD_TEST_UNIT_READY
2427  * SCMD_START_STOP
2428  * SCMD_READ_CAPACITY
2429  * SCMD_SVC_ACTION_IN_G4 (READ CAPACITY (16))
2430  * SCMD_REQUEST_SENSE
2431  * SCMD_LOG_SENSE_G1
2432  * SCMD_LOG_SELECT_G1
2433  * SCMD_MODE_SENSE	(specific pages)
2434  * SCMD_MODE_SENSE_G1	(specific pages)
2435  * SCMD_MODE_SELECT	(specific pages)
2436  * SCMD_MODE_SELECT_G1	(specific pages)
2437  * SCMD_SYNCHRONIZE_CACHE
2438  * SCMD_SYNCHRONIZE_CACHE_G1
2439  * SCMD_READ
2440  * SCMD_READ_G1
2441  * SCMD_READ_G4
2442  * SCMD_READ_G5
2443  * SCMD_WRITE
2444  * SCMD_WRITE_BUFFER
2445  * SCMD_WRITE_G1
2446  * SCMD_WRITE_G4
2447  * SCMD_WRITE_G5
2448  * SCMD_SEEK		(noop)
2449  * SCMD_SDIAG
2450  *
2451  * All other commands are rejected as unsupported.
2452  *
2453  * Returns:
2454  * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver
2455  * for execution. TRAN_ACCEPT may be returned also if device was removed but
2456  * a callback could be scheduled.
2457  * TRAN_BADPKT if cmd was directed to invalid address.
2458  * TRAN_FATAL_ERROR is command was rejected due to hardware error, including
2459  * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device
2460  * was removed and there was no callback specified in scsi pkt.
2461  * TRAN_BUSY if command could not be executed becasue HBA driver or SATA
2462  * framework was busy performing some other operation(s).
2463  *
2464  */
2465 static int
2466 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
2467 {
2468 	sata_hba_inst_t *sata_hba_inst =
2469 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2470 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2471 	sata_device_t *sdevice = &spx->txlt_sata_pkt->satapkt_device;
2472 	sata_drive_info_t *sdinfo;
2473 	struct buf *bp;
2474 	uint8_t cport, pmport;
2475 	boolean_t dev_gone = B_FALSE;
2476 	int rval;
2477 
2478 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2479 	    "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]);
2480 
2481 	ASSERT(spx != NULL &&
2482 	    spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL);
2483 
2484 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
2485 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2486 
2487 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2488 
2489 	if (sdevice->satadev_addr.qual == SATA_ADDR_DCPORT) {
2490 		sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2491 		if (sdinfo == NULL ||
2492 		    SATA_CPORT_INFO(sata_hba_inst, cport)->
2493 		    cport_tgtnode_clean == B_FALSE ||
2494 		    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2495 			dev_gone = B_TRUE;
2496 		}
2497 	} else if (sdevice->satadev_addr.qual == SATA_ADDR_DPMPORT) {
2498 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
2499 		    SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst,
2500 		    cport) == NULL) {
2501 			dev_gone = B_TRUE;
2502 		} else if (SATA_PMPORT_INFO(sata_hba_inst, cport,
2503 		    pmport) == NULL) {
2504 			dev_gone = B_TRUE;
2505 		} else {
2506 			mutex_enter(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2507 			    cport, pmport)));
2508 			sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2509 			if (sdinfo == NULL ||
2510 			    SATA_PMPORT_INFO(sata_hba_inst, cport, pmport)->
2511 			    pmport_tgtnode_clean == B_FALSE ||
2512 			    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2513 				dev_gone = B_TRUE;
2514 			}
2515 			mutex_exit(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2516 			    cport, pmport)));
2517 		}
2518 	}
2519 
2520 	if (dev_gone == B_TRUE) {
2521 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2522 		pkt->pkt_reason = CMD_DEV_GONE;
2523 		/*
2524 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2525 		 * only in callback function (for normal requests) and
2526 		 * in the dump code path.
2527 		 * So, if the callback is available, we need to do
2528 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2529 		 */
2530 		if (pkt->pkt_comp != NULL) {
2531 			/* scsi callback required */
2532 			if (servicing_interrupt()) {
2533 				if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2534 				    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2535 				    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
2536 				    TASKQID_INVALID) {
2537 					return (TRAN_BUSY);
2538 				}
2539 			} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2540 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2541 			    spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
2542 				/* Scheduling the callback failed */
2543 				return (TRAN_BUSY);
2544 			}
2545 			return (TRAN_ACCEPT);
2546 		}
2547 		/* No callback available */
2548 		return (TRAN_FATAL_ERROR);
2549 	}
2550 
2551 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
2552 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2553 		rval = sata_txlt_atapi(spx);
2554 		SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2555 		    "sata_scsi_start atapi: rval %d\n", rval);
2556 		return (rval);
2557 	}
2558 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2559 
2560 	/*
2561 	 * Checking for power state, if it was on
2562 	 * STOPPED state, then the drive is not capable
2563 	 * of processing media access command.  And
2564 	 * TEST_UNIT_READY, REQUEST_SENSE has special handling
2565 	 * in the function for different power state.
2566 	 */
2567 	if (((sdinfo->satadrv_power_level == SATA_POWER_STANDBY) ||
2568 	    (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)) &&
2569 	    (SATA_IS_MEDIUM_ACCESS_CMD(pkt->pkt_cdbp[0]))) {
2570 		return (sata_txlt_check_condition(spx, KEY_NOT_READY,
2571 		    SD_SCSI_ASC_LU_NOT_READY));
2572 	}
2573 
2574 	/* ATA Disk commands processing starts here */
2575 
2576 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2577 
2578 	switch (pkt->pkt_cdbp[0]) {
2579 
2580 	case SCMD_INQUIRY:
2581 		/* Mapped to identify device */
2582 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2583 			bp_mapin(bp);
2584 		rval = sata_txlt_inquiry(spx);
2585 		break;
2586 
2587 	case SCMD_TEST_UNIT_READY:
2588 		/*
2589 		 * SAT "SATA to ATA Translation" doc specifies translation
2590 		 * to ATA CHECK POWER MODE.
2591 		 */
2592 		rval = sata_txlt_test_unit_ready(spx);
2593 		break;
2594 
2595 	case SCMD_START_STOP:
2596 		/* Mapping depends on the command */
2597 		rval = sata_txlt_start_stop_unit(spx);
2598 		break;
2599 
2600 	case SCMD_READ_CAPACITY:
2601 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2602 			bp_mapin(bp);
2603 		rval = sata_txlt_read_capacity(spx);
2604 		break;
2605 
2606 	case SCMD_SVC_ACTION_IN_G4:		/* READ CAPACITY (16) */
2607 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2608 			bp_mapin(bp);
2609 		rval = sata_txlt_read_capacity16(spx);
2610 		break;
2611 
2612 	case SCMD_REQUEST_SENSE:
2613 		/*
2614 		 * Always No Sense, since we force ARQ
2615 		 */
2616 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2617 			bp_mapin(bp);
2618 		rval = sata_txlt_request_sense(spx);
2619 		break;
2620 
2621 	case SCMD_LOG_SENSE_G1:
2622 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2623 			bp_mapin(bp);
2624 		rval = sata_txlt_log_sense(spx);
2625 		break;
2626 
2627 	case SCMD_LOG_SELECT_G1:
2628 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2629 			bp_mapin(bp);
2630 		rval = sata_txlt_log_select(spx);
2631 		break;
2632 
2633 	case SCMD_MODE_SENSE:
2634 	case SCMD_MODE_SENSE_G1:
2635 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2636 			bp_mapin(bp);
2637 		rval = sata_txlt_mode_sense(spx);
2638 		break;
2639 
2640 
2641 	case SCMD_MODE_SELECT:
2642 	case SCMD_MODE_SELECT_G1:
2643 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2644 			bp_mapin(bp);
2645 		rval = sata_txlt_mode_select(spx);
2646 		break;
2647 
2648 	case SCMD_SYNCHRONIZE_CACHE:
2649 	case SCMD_SYNCHRONIZE_CACHE_G1:
2650 		rval = sata_txlt_synchronize_cache(spx);
2651 		break;
2652 
2653 	case SCMD_READ:
2654 	case SCMD_READ_G1:
2655 	case SCMD_READ_G4:
2656 	case SCMD_READ_G5:
2657 		rval = sata_txlt_read(spx);
2658 		break;
2659 	case SCMD_WRITE_BUFFER:
2660 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2661 			bp_mapin(bp);
2662 		rval = sata_txlt_write_buffer(spx);
2663 		break;
2664 
2665 	case SCMD_WRITE:
2666 	case SCMD_WRITE_G1:
2667 	case SCMD_WRITE_G4:
2668 	case SCMD_WRITE_G5:
2669 		rval = sata_txlt_write(spx);
2670 		break;
2671 
2672 	case SCMD_SEEK:
2673 		rval = sata_txlt_nodata_cmd_immediate(spx);
2674 		break;
2675 
2676 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12:
2677 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16:
2678 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2679 			bp_mapin(bp);
2680 		rval = sata_txlt_ata_pass_thru(spx);
2681 		break;
2682 
2683 		/* Other cases will be filed later */
2684 		/* postponed until phase 2 of the development */
2685 	case SPC3_CMD_UNMAP:
2686 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2687 			bp_mapin(bp);
2688 		rval = sata_txlt_unmap(spx);
2689 		break;
2690 	default:
2691 		rval = sata_txlt_invalid_command(spx);
2692 		break;
2693 	}
2694 
2695 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2696 	    "sata_scsi_start: rval %d\n", rval);
2697 
2698 	return (rval);
2699 }
2700 
2701 /*
2702  * Implementation of scsi tran_abort.
2703  * Abort specific pkt or all packets.
2704  *
2705  * Returns 1 if one or more packets were aborted, returns 0 otherwise
2706  *
2707  * May be called from an interrupt level.
2708  */
2709 static int
2710 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt)
2711 {
2712 	sata_hba_inst_t *sata_hba_inst =
2713 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2714 	sata_device_t	sata_device;
2715 	sata_pkt_t	*sata_pkt;
2716 
2717 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2718 	    "sata_scsi_abort: %s at target: 0x%x\n",
2719 	    scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target);
2720 
2721 	/* Validate address */
2722 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0)
2723 		/* Invalid address */
2724 		return (0);
2725 
2726 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2727 	    sata_device.satadev_addr.cport)));
2728 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2729 		/* invalid address */
2730 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2731 		    sata_device.satadev_addr.cport)));
2732 		return (0);
2733 	}
2734 	if (scsi_pkt == NULL) {
2735 		/*
2736 		 * Abort all packets.
2737 		 * Although we do not have specific packet, we still need
2738 		 * dummy packet structure to pass device address to HBA.
2739 		 * Allocate one, without sleeping. Fail if pkt cannot be
2740 		 * allocated.
2741 		 */
2742 		sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP);
2743 		if (sata_pkt == NULL) {
2744 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2745 			    sata_device.satadev_addr.cport)));
2746 			SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: "
2747 			    "could not allocate sata_pkt"));
2748 			return (0);
2749 		}
2750 		sata_pkt->satapkt_rev = SATA_PKT_REV;
2751 		sata_pkt->satapkt_device = sata_device;
2752 		sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
2753 	} else {
2754 		if (scsi_pkt->pkt_ha_private == NULL) {
2755 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2756 			    sata_device.satadev_addr.cport)));
2757 			return (0); /* Bad scsi pkt */
2758 		}
2759 		/* extract pointer to sata pkt */
2760 		sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)->
2761 		    txlt_sata_pkt;
2762 	}
2763 
2764 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2765 	    sata_device.satadev_addr.cport)));
2766 	/* Send abort request to HBA */
2767 	if ((*SATA_ABORT_FUNC(sata_hba_inst))
2768 	    (SATA_DIP(sata_hba_inst), sata_pkt,
2769 	    scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) ==
2770 	    SATA_SUCCESS) {
2771 		if (scsi_pkt == NULL)
2772 			kmem_free(sata_pkt, sizeof (sata_pkt_t));
2773 		/* Success */
2774 		return (1);
2775 	}
2776 	/* Else, something did not go right */
2777 	if (scsi_pkt == NULL)
2778 		kmem_free(sata_pkt, sizeof (sata_pkt_t));
2779 	/* Failure */
2780 	return (0);
2781 }
2782 
2783 
2784 /*
2785  * Implementation of scsi tran_reset.
2786  * RESET_ALL request is translated into port reset.
2787  * RESET_TARGET requests is translated into a device reset,
2788  * RESET_LUN request is accepted only for LUN 0 and translated into
2789  * device reset.
2790  * The target reset should cause all HBA active and queued packets to
2791  * be terminated and returned with pkt reason SATA_PKT_RESET prior to
2792  * the return. HBA should report reset event for the device.
2793  *
2794  * Returns 1 upon success, 0 upon failure.
2795  */
2796 static int
2797 sata_scsi_reset(struct scsi_address *ap, int level)
2798 {
2799 	sata_hba_inst_t	*sata_hba_inst =
2800 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2801 	sata_device_t	sata_device;
2802 	int		val;
2803 
2804 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2805 	    "sata_scsi_reset: level %d target: 0x%x\n",
2806 	    level, ap->a_target);
2807 
2808 	/* Validate address */
2809 	val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device);
2810 	if (val == -1)
2811 		/* Invalid address */
2812 		return (0);
2813 
2814 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2815 	    sata_device.satadev_addr.cport)));
2816 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2817 		/* invalid address */
2818 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2819 		    sata_device.satadev_addr.cport)));
2820 		return (0);
2821 	}
2822 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2823 	    sata_device.satadev_addr.cport)));
2824 	if (level == RESET_ALL) {
2825 		/* port reset */
2826 		if (sata_device.satadev_addr.qual == SATA_ADDR_DCPORT)
2827 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2828 		else
2829 			sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
2830 
2831 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2832 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2833 			return (1);
2834 		else
2835 			return (0);
2836 
2837 	} else if (val == 0 &&
2838 	    (level == RESET_TARGET || level == RESET_LUN)) {
2839 		/* reset device (device attached) */
2840 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2841 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2842 			return (1);
2843 		else
2844 			return (0);
2845 	}
2846 	return (0);
2847 }
2848 
2849 
2850 /*
2851  * Implementation of scsi tran_getcap (get transport/device capabilities).
2852  * Supported capabilities for SATA hard disks:
2853  * auto-rqsense		(always supported)
2854  * tagged-qing		(supported if HBA supports it)
2855  * untagged-qing	(could be supported if disk supports it, but because
2856  *			 caching behavior allowing untagged queuing actually
2857  *			 results in reduced performance.  sd tries to throttle
2858  *			 back to only 3 outstanding commands, which may
2859  *			 work for real SCSI disks, but with read ahead
2860  *			 caching, having more than 1 outstanding command
2861  *			 results in cache thrashing.)
2862  * sector_size
2863  * dma_max
2864  * interconnect-type	(INTERCONNECT_SATA)
2865  *
2866  * Supported capabilities for ATAPI CD/DVD devices:
2867  * auto-rqsense		(always supported)
2868  * sector_size
2869  * dma_max
2870  * max-cdb-length
2871  * interconnect-type	(INTERCONNECT_SATA)
2872  *
2873  * Supported capabilities for ATAPI TAPE devices:
2874  * auto-rqsense		(always supported)
2875  * dma_max
2876  * max-cdb-length
2877  *
2878  * Supported capabilities for SATA ATAPI hard disks:
2879  * auto-rqsense		(always supported)
2880  * interconnect-type	(INTERCONNECT_SATA)
2881  * max-cdb-length
2882  *
2883  * Request for other capabilities is rejected as unsupported.
2884  *
2885  * Returns supported capability value, or -1 if capability is unsuppported or
2886  * the address is invalid - no device.
2887  */
2888 
2889 static int
2890 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
2891 {
2892 
2893 	sata_hba_inst_t		*sata_hba_inst =
2894 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2895 	sata_device_t		sata_device;
2896 	sata_drive_info_t	*sdinfo;
2897 	ddi_dma_attr_t		adj_dma_attr;
2898 	int			rval;
2899 
2900 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2901 	    "sata_scsi_getcap: target: 0x%x, cap: %s\n",
2902 	    ap->a_target, cap);
2903 
2904 	/*
2905 	 * We want to process the capabilities on per port granularity.
2906 	 * So, we are specifically restricting ourselves to whom != 0
2907 	 * to exclude the controller wide handling.
2908 	 */
2909 	if (cap == NULL || whom == 0)
2910 		return (-1);
2911 
2912 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2913 		/* Invalid address */
2914 		return (-1);
2915 	}
2916 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2917 	    sata_device.satadev_addr.cport)));
2918 	if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) ==
2919 	    NULL) {
2920 		/* invalid address */
2921 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2922 		    sata_device.satadev_addr.cport)));
2923 		return (-1);
2924 	}
2925 
2926 	switch (scsi_hba_lookup_capstr(cap)) {
2927 	case SCSI_CAP_ARQ:
2928 		rval = 1;		/* ARQ supported, turned on */
2929 		break;
2930 
2931 	case SCSI_CAP_SECTOR_SIZE:
2932 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK)
2933 			rval = SATA_DISK_SECTOR_SIZE;	/* fixed size */
2934 		else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD)
2935 			rval = SATA_ATAPI_SECTOR_SIZE;
2936 		else rval = -1;
2937 		break;
2938 
2939 	/*
2940 	 * untagged queuing cause a performance inversion because of
2941 	 * the way sd operates.  Because of this reason we do not
2942 	 * use it when available.
2943 	 */
2944 	case SCSI_CAP_UNTAGGED_QING:
2945 		if (sdinfo->satadrv_features_enabled &
2946 		    SATA_DEV_F_E_UNTAGGED_QING)
2947 			rval = 1;	/* Untagged queuing available */
2948 		else
2949 			rval = -1;	/* Untagged queuing not available */
2950 		break;
2951 
2952 	case SCSI_CAP_TAGGED_QING:
2953 		if ((sdinfo->satadrv_features_enabled &
2954 		    SATA_DEV_F_E_TAGGED_QING) &&
2955 		    (sdinfo->satadrv_max_queue_depth > 1))
2956 			rval = 1;	/* Tagged queuing available */
2957 		else
2958 			rval = -1;	/* Tagged queuing not available */
2959 		break;
2960 
2961 	case SCSI_CAP_DMA_MAX:
2962 		sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst),
2963 		    &adj_dma_attr);
2964 		rval = (int)adj_dma_attr.dma_attr_maxxfer;
2965 		/* We rely on the fact that dma_attr_maxxfer < 0x80000000 */
2966 		break;
2967 
2968 	case SCSI_CAP_INTERCONNECT_TYPE:
2969 		rval = INTERCONNECT_SATA;	/* SATA interconnect type */
2970 		break;
2971 
2972 	case SCSI_CAP_CDB_LEN:
2973 		if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI)
2974 			rval = sdinfo->satadrv_atapi_cdb_len;
2975 		else
2976 			rval = -1;
2977 		break;
2978 
2979 	default:
2980 		rval = -1;
2981 		break;
2982 	}
2983 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2984 	    sata_device.satadev_addr.cport)));
2985 	return (rval);
2986 }
2987 
2988 /*
2989  * Implementation of scsi tran_setcap
2990  *
2991  * Only SCSI_CAP_UNTAGGED_QING and  SCSI_CAP_TAGGED_QING are changeable.
2992  *
2993  */
2994 static int
2995 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
2996 {
2997 	sata_hba_inst_t	*sata_hba_inst =
2998 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2999 	sata_device_t	sata_device;
3000 	sata_drive_info_t	*sdinfo;
3001 	int		rval;
3002 
3003 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
3004 	    "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap);
3005 
3006 	/*
3007 	 * We want to process the capabilities on per port granularity.
3008 	 * So, we are specifically restricting ourselves to whom != 0
3009 	 * to exclude the controller wide handling.
3010 	 */
3011 	if (cap == NULL || whom == 0) {
3012 		return (-1);
3013 	}
3014 
3015 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
3016 		/* Invalid address */
3017 		return (-1);
3018 	}
3019 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
3020 	    sata_device.satadev_addr.cport)));
3021 	if ((sdinfo = sata_get_device_info(sata_hba_inst,
3022 	    &sata_device)) == NULL) {
3023 		/* invalid address */
3024 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3025 		    sata_device.satadev_addr.cport)));
3026 		return (-1);
3027 	}
3028 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3029 	    sata_device.satadev_addr.cport)));
3030 
3031 	switch (scsi_hba_lookup_capstr(cap)) {
3032 	case SCSI_CAP_ARQ:
3033 	case SCSI_CAP_SECTOR_SIZE:
3034 	case SCSI_CAP_DMA_MAX:
3035 	case SCSI_CAP_INTERCONNECT_TYPE:
3036 		rval = 0;
3037 		break;
3038 	case SCSI_CAP_UNTAGGED_QING:
3039 		if (SATA_QDEPTH(sata_hba_inst) > 1) {
3040 			rval = 1;
3041 			if (value == 1) {
3042 				sdinfo->satadrv_features_enabled |=
3043 				    SATA_DEV_F_E_UNTAGGED_QING;
3044 			} else if (value == 0) {
3045 				sdinfo->satadrv_features_enabled &=
3046 				    ~SATA_DEV_F_E_UNTAGGED_QING;
3047 			} else {
3048 				rval = -1;
3049 			}
3050 		} else {
3051 			rval = 0;
3052 		}
3053 		break;
3054 	case SCSI_CAP_TAGGED_QING:
3055 		/* This can TCQ or NCQ */
3056 		if (sata_func_enable & SATA_ENABLE_QUEUING &&
3057 		    ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ &&
3058 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) ||
3059 		    (sata_func_enable & SATA_ENABLE_NCQ &&
3060 		    sdinfo->satadrv_features_support & SATA_DEV_F_NCQ &&
3061 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) &&
3062 		    (sdinfo->satadrv_max_queue_depth > 1)) {
3063 			rval = 1;
3064 			if (value == 1) {
3065 				sdinfo->satadrv_features_enabled |=
3066 				    SATA_DEV_F_E_TAGGED_QING;
3067 			} else if (value == 0) {
3068 				sdinfo->satadrv_features_enabled &=
3069 				    ~SATA_DEV_F_E_TAGGED_QING;
3070 			} else {
3071 				rval = -1;
3072 			}
3073 		} else {
3074 			rval = 0;
3075 		}
3076 		break;
3077 	default:
3078 		rval = -1;
3079 		break;
3080 	}
3081 	return (rval);
3082 }
3083 
3084 /*
3085  * Implementations of scsi tran_destroy_pkt.
3086  * Free resources allocated by sata_scsi_init_pkt()
3087  */
3088 static void
3089 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3090 {
3091 	sata_pkt_txlate_t *spx;
3092 
3093 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3094 
3095 	sata_common_free_dma_rsrcs(spx);
3096 
3097 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
3098 	sata_pkt_free(spx);
3099 
3100 	scsi_hba_pkt_free(ap, pkt);
3101 }
3102 
3103 /*
3104  * Implementation of scsi tran_dmafree.
3105  * Free DMA resources allocated by sata_scsi_init_pkt()
3106  */
3107 
3108 static void
3109 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
3110 {
3111 #ifndef __lock_lint
3112 	_NOTE(ARGUNUSED(ap))
3113 #endif
3114 	sata_pkt_txlate_t *spx;
3115 
3116 	ASSERT(pkt != NULL);
3117 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3118 
3119 	sata_common_free_dma_rsrcs(spx);
3120 }
3121 
3122 /*
3123  * Implementation of scsi tran_sync_pkt.
3124  *
3125  * The assumption below is that pkt is unique - there is no need to check ap
3126  *
3127  * Synchronize DMA buffer and, if the intermediate buffer is used, copy data
3128  * into/from the real buffer.
3129  */
3130 static void
3131 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3132 {
3133 #ifndef __lock_lint
3134 	_NOTE(ARGUNUSED(ap))
3135 #endif
3136 	int rval;
3137 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3138 	struct buf *bp;
3139 	int direction;
3140 
3141 	ASSERT(spx != NULL);
3142 	if (spx->txlt_buf_dma_handle != NULL) {
3143 		direction = spx->txlt_sata_pkt->
3144 		    satapkt_cmd.satacmd_flags.sata_data_direction;
3145 		if (spx->txlt_sata_pkt != NULL &&
3146 		    direction != SATA_DIR_NODATA_XFER) {
3147 			if (spx->txlt_tmp_buf != NULL) {
3148 				/* Intermediate DMA buffer used */
3149 				bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3150 
3151 				if (direction & SATA_DIR_WRITE) {
3152 					bcopy(bp->b_un.b_addr,
3153 					    spx->txlt_tmp_buf, bp->b_bcount);
3154 				}
3155 			}
3156 			/* Sync the buffer for device or for CPU */
3157 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle,   0, 0,
3158 			    (direction & SATA_DIR_WRITE) ?
3159 			    DDI_DMA_SYNC_FORDEV :  DDI_DMA_SYNC_FORCPU);
3160 			ASSERT(rval == DDI_SUCCESS);
3161 			if (spx->txlt_tmp_buf != NULL &&
3162 			    !(direction & SATA_DIR_WRITE)) {
3163 				/* Intermediate DMA buffer used for read */
3164 				bcopy(spx->txlt_tmp_buf,
3165 				    bp->b_un.b_addr, bp->b_bcount);
3166 			}
3167 
3168 		}
3169 	}
3170 }
3171 
3172 
3173 
3174 /* *******************  SATA - SCSI Translation functions **************** */
3175 /*
3176  * SCSI to SATA pkt and command translation and SATA to SCSI status/error
3177  * translation.
3178  */
3179 
3180 /*
3181  * Checks if a device exists and can be access and translates common
3182  * scsi_pkt data to sata_pkt data.
3183  *
3184  * Flag argument indicates that a non-read/write ATA command may be sent
3185  * to HBA in arbitrary SYNC mode to execute this packet.
3186  *
3187  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and
3188  * sata_pkt was set-up.
3189  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not
3190  * exist and pkt_comp callback was scheduled.
3191  * Returns other TRAN_XXXXX values when error occured and command should be
3192  * rejected with the returned TRAN_XXXXX value.
3193  *
3194  * This function should be called with port mutex held.
3195  */
3196 static int
3197 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason, int flag)
3198 {
3199 	sata_drive_info_t *sdinfo;
3200 	sata_device_t sata_device;
3201 	const struct sata_cmd_flags sata_initial_cmd_flags = {
3202 		SATA_DIR_NODATA_XFER,
3203 		/* all other values to 0/FALSE */
3204 	};
3205 	/*
3206 	 * Pkt_reason has to be set if the pkt_comp callback is invoked,
3207 	 * and that implies TRAN_ACCEPT return value. Any other returned value
3208 	 * indicates that the scsi packet was not accepted (the reason will not
3209 	 * be checked by the scsi target driver).
3210 	 * To make debugging easier, we set pkt_reason to know value here.
3211 	 * It may be changed later when different completion reason is
3212 	 * determined.
3213 	 */
3214 	spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
3215 	*reason = CMD_TRAN_ERR;
3216 
3217 	/* Validate address */
3218 	switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst,
3219 	    &spx->txlt_scsi_pkt->pkt_address, &sata_device)) {
3220 
3221 	case -1:
3222 		/* Invalid address or invalid device type */
3223 		return (TRAN_BADPKT);
3224 	case 2:
3225 		/*
3226 		 * Valid address but device type is unknown - Chack if it is
3227 		 * in the reset state and therefore in an indeterminate state.
3228 		 */
3229 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3230 		    &spx->txlt_sata_pkt->satapkt_device);
3231 		if (sdinfo != NULL && (sdinfo->satadrv_event_flags &
3232 		    (SATA_EVNT_DEVICE_RESET |
3233 		    SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3234 			if (!ddi_in_panic()) {
3235 				spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3236 				*reason = CMD_INCOMPLETE;
3237 				SATADBG1(SATA_DBG_SCSI_IF,
3238 				    spx->txlt_sata_hba_inst,
3239 				    "sata_scsi_start: rejecting command "
3240 				    "because of device reset state\n", NULL);
3241 				return (TRAN_BUSY);
3242 			}
3243 		}
3244 		/* FALLTHROUGH */
3245 	case 1:
3246 		/* valid address but no valid device - it has disappeared */
3247 		spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE;
3248 		*reason = CMD_DEV_GONE;
3249 		/*
3250 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
3251 		 * only in callback function (for normal requests) and
3252 		 * in the dump code path.
3253 		 * So, if the callback is available, we need to do
3254 		 * the callback rather than returning TRAN_FATAL_ERROR here.
3255 		 */
3256 		if (spx->txlt_scsi_pkt->pkt_comp != NULL) {
3257 			/* scsi callback required */
3258 			if (servicing_interrupt()) {
3259 				if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3260 				    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3261 				    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3262 				    TASKQID_INVALID) {
3263 					return (TRAN_BUSY);
3264 				}
3265 			} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3266 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3267 			    spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
3268 				/* Scheduling the callback failed */
3269 				return (TRAN_BUSY);
3270 			}
3271 
3272 			return (TRAN_ACCEPT);
3273 		}
3274 		return (TRAN_FATAL_ERROR);
3275 	default:
3276 		/* all OK; pkt reason will be overwritten later */
3277 		break;
3278 	}
3279 	/*
3280 	 * If pkt is to be executed in polling mode and a command will not be
3281 	 * emulated in SATA module (requires sending a non-read/write ATA
3282 	 * command to HBA driver in arbitrary SYNC mode) and we are in the
3283 	 * interrupt context and not in the panic dump, then reject the packet
3284 	 * to avoid a possible interrupt stack overrun or hang caused by
3285 	 * a potentially blocked interrupt.
3286 	 */
3287 	if (((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0 || flag != 0) &&
3288 	    servicing_interrupt() && !ddi_in_panic()) {
3289 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
3290 		    "sata_scsi_start: rejecting synchronous command because "
3291 		    "of interrupt context\n", NULL);
3292 		return (TRAN_BUSY);
3293 	}
3294 
3295 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3296 	    &spx->txlt_sata_pkt->satapkt_device);
3297 
3298 	/*
3299 	 * If device is in reset condition, reject the packet with
3300 	 * TRAN_BUSY, unless:
3301 	 * 1. system is panicking (dumping)
3302 	 * In such case only one thread is running and there is no way to
3303 	 * process reset.
3304 	 * 2. cfgadm operation is is progress (internal APCTL lock is set)
3305 	 * Some cfgadm operations involve drive commands, so reset condition
3306 	 * needs to be ignored for IOCTL operations.
3307 	 */
3308 	if ((sdinfo->satadrv_event_flags &
3309 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3310 
3311 		if (!ddi_in_panic() &&
3312 		    ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst,
3313 		    sata_device.satadev_addr.cport) &
3314 		    SATA_APCTL_LOCK_PORT_BUSY) == 0)) {
3315 			spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3316 			*reason = CMD_INCOMPLETE;
3317 			SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3318 			    "sata_scsi_start: rejecting command because "
3319 			    "of device reset state\n", NULL);
3320 			return (TRAN_BUSY);
3321 		}
3322 	}
3323 
3324 	/*
3325 	 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by
3326 	 * sata_scsi_pkt_init() because pkt init had to work also with
3327 	 * non-existing devices.
3328 	 * Now we know that the packet was set-up for a real device, so its
3329 	 * type is known.
3330 	 */
3331 	spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type;
3332 
3333 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags;
3334 	if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst,
3335 	    sata_device.satadev_addr.cport)->cport_event_flags &
3336 	    SATA_APCTL_LOCK_PORT_BUSY) != 0) {
3337 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3338 		    sata_ignore_dev_reset = B_TRUE;
3339 	}
3340 	/*
3341 	 * At this point the generic translation routine determined that the
3342 	 * scsi packet should be accepted. Packet completion reason may be
3343 	 * changed later when a different completion reason is determined.
3344 	 */
3345 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3346 	*reason = CMD_CMPLT;
3347 
3348 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
3349 		/* Synchronous execution */
3350 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH |
3351 		    SATA_OPMODE_POLLING;
3352 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3353 		    sata_ignore_dev_reset = ddi_in_panic();
3354 	} else {
3355 		/* Asynchronous execution */
3356 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH |
3357 		    SATA_OPMODE_INTERRUPTS;
3358 	}
3359 	/* Convert queuing information */
3360 	if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG)
3361 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag =
3362 		    B_TRUE;
3363 	else if (spx->txlt_scsi_pkt->pkt_flags &
3364 	    (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD))
3365 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag =
3366 		    B_TRUE;
3367 
3368 	/* Always limit pkt time */
3369 	if (spx->txlt_scsi_pkt->pkt_time == 0)
3370 		spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time;
3371 	else
3372 		/* Pass on scsi_pkt time */
3373 		spx->txlt_sata_pkt->satapkt_time =
3374 		    spx->txlt_scsi_pkt->pkt_time;
3375 
3376 	return (TRAN_ACCEPT);
3377 }
3378 
3379 
3380 /*
3381  * Translate ATA Identify Device data to SCSI Inquiry data.
3382  * This function may be called only for ATA devices.
3383  * This function should not be called for ATAPI devices - they
3384  * respond directly to SCSI Inquiry command.
3385  *
3386  * SATA Identify Device data has to be valid in sata_drive_info.
3387  * Buffer has to accomodate the inquiry length (36 bytes).
3388  *
3389  * This function should be called with a port mutex held.
3390  */
3391 static	void
3392 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst,
3393     sata_drive_info_t *sdinfo, uint8_t *buf)
3394 {
3395 
3396 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
3397 	struct sata_id *sid = &sdinfo->satadrv_id;
3398 
3399 	/* Start with a nice clean slate */
3400 	bzero((void *)inq, sizeof (struct scsi_inquiry));
3401 
3402 	/*
3403 	 * Rely on the dev_type for setting paripheral qualifier.
3404 	 * Assume that  DTYPE_RODIRECT applies to CD/DVD R/W devices.
3405 	 * It could be that DTYPE_OPTICAL could also qualify in the future.
3406 	 * ATAPI Inquiry may provide more data to the target driver.
3407 	 */
3408 	inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3409 	    DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */
3410 
3411 	/* CFA type device is not a removable media device */
3412 	inq->inq_rmb = ((sid->ai_config != SATA_CFA_TYPE) &&
3413 	    (sid->ai_config & SATA_REM_MEDIA)) ? 1 : 0;
3414 	inq->inq_qual = 0;	/* Device type qualifier (obsolete in SCSI3? */
3415 	inq->inq_iso = 0;	/* ISO version */
3416 	inq->inq_ecma = 0;	/* ECMA version */
3417 	inq->inq_ansi = 3;	/* ANSI version - SCSI 3 */
3418 	inq->inq_aenc = 0;	/* Async event notification cap. */
3419 	inq->inq_trmiop = 0;	/* Supports TERMINATE I/O PROC msg - NO */
3420 	inq->inq_normaca = 0;	/* setting NACA bit supported - NO */
3421 	inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */
3422 	inq->inq_len = 31;	/* Additional length */
3423 	inq->inq_dualp = 0;	/* dual port device - NO */
3424 	inq->inq_reladdr = 0;	/* Supports relative addressing - NO */
3425 	inq->inq_sync = 0;	/* Supports synchronous data xfers - NO */
3426 	inq->inq_linked = 0;	/* Supports linked commands - NO */
3427 				/*
3428 				 * Queuing support - controller has to
3429 				 * support some sort of command queuing.
3430 				 */
3431 	if (SATA_QDEPTH(sata_hba_inst) > 1)
3432 		inq->inq_cmdque = 1; /* Supports command queueing - YES */
3433 	else
3434 		inq->inq_cmdque = 0; /* Supports command queueing - NO */
3435 	inq->inq_sftre = 0;	/* Supports Soft Reset option - NO ??? */
3436 	inq->inq_wbus32 = 0;	/* Supports 32 bit wide data xfers - NO */
3437 	inq->inq_wbus16 = 0;	/* Supports 16 bit wide data xfers - NO */
3438 
3439 #ifdef	_LITTLE_ENDIAN
3440 	/* Swap text fields to match SCSI format */
3441 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3442 	swab(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3443 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3444 		swab(sid->ai_fw, inq->inq_revision, 4);	/* Revision level */
3445 	else
3446 		swab(&sid->ai_fw[4], inq->inq_revision, 4);	/* Rev. level */
3447 #else	/* _LITTLE_ENDIAN */
3448 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3449 	bcopy(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3450 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3451 		bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
3452 	else
3453 		bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
3454 #endif	/* _LITTLE_ENDIAN */
3455 }
3456 
3457 
3458 /*
3459  * Scsi response set up for invalid command (command not supported)
3460  *
3461  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3462  */
3463 static int
3464 sata_txlt_invalid_command(sata_pkt_txlate_t *spx)
3465 {
3466 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3467 	struct scsi_extended_sense *sense;
3468 
3469 	scsipkt->pkt_reason = CMD_CMPLT;
3470 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3471 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3472 
3473 	*scsipkt->pkt_scbp = STATUS_CHECK;
3474 
3475 	sense = sata_arq_sense(spx);
3476 	sense->es_key = KEY_ILLEGAL_REQUEST;
3477 	sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE;
3478 
3479 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3480 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3481 
3482 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3483 	    scsipkt->pkt_comp != NULL) {
3484 		/* scsi callback required */
3485 		if (servicing_interrupt()) {
3486 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3487 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3488 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3489 			    TASKQID_INVALID) {
3490 				return (TRAN_BUSY);
3491 			}
3492 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3493 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3494 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
3495 			/* Scheduling the callback failed */
3496 			return (TRAN_BUSY);
3497 		}
3498 	}
3499 	return (TRAN_ACCEPT);
3500 }
3501 
3502 /*
3503  * Scsi response set up for check condition with special sense key
3504  * and additional sense code.
3505  *
3506  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3507  */
3508 static int
3509 sata_txlt_check_condition(sata_pkt_txlate_t *spx, uchar_t key, uchar_t code)
3510 {
3511 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3512 	int cport = SATA_TXLT_CPORT(spx);
3513 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3514 	struct scsi_extended_sense *sense;
3515 
3516 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3517 	scsipkt->pkt_reason = CMD_CMPLT;
3518 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3519 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3520 
3521 	*scsipkt->pkt_scbp = STATUS_CHECK;
3522 
3523 	sense = sata_arq_sense(spx);
3524 	sense->es_key = key;
3525 	sense->es_add_code = code;
3526 
3527 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3528 
3529 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3530 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3531 
3532 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3533 	    scsipkt->pkt_comp != NULL) {
3534 		/* scsi callback required */
3535 		if (servicing_interrupt()) {
3536 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3537 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3538 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3539 			    TASKQID_INVALID) {
3540 				return (TRAN_BUSY);
3541 			}
3542 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3543 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3544 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
3545 			/* Scheduling the callback failed */
3546 			return (TRAN_BUSY);
3547 		}
3548 	}
3549 	return (TRAN_ACCEPT);
3550 }
3551 
3552 /*
3553  * Scsi response setup for
3554  * emulated non-data command that requires no action/return data
3555  *
3556  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3557  */
3558 static	int
3559 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx)
3560 {
3561 	int rval;
3562 	int reason;
3563 	kmutex_t *cport_mutex =  &(SATA_TXLT_CPORT_MUTEX(spx));
3564 
3565 	mutex_enter(cport_mutex);
3566 
3567 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
3568 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3569 		mutex_exit(cport_mutex);
3570 		return (rval);
3571 	}
3572 	mutex_exit(cport_mutex);
3573 
3574 	spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3575 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3576 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3577 	*(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD;
3578 
3579 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3580 	    "Scsi_pkt completion reason %x\n",
3581 	    spx->txlt_scsi_pkt->pkt_reason);
3582 
3583 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 &&
3584 	    spx->txlt_scsi_pkt->pkt_comp != NULL) {
3585 		/* scsi callback required */
3586 		if (servicing_interrupt()) {
3587 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3588 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3589 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3590 			    TASKQID_INVALID) {
3591 				return (TRAN_BUSY);
3592 			}
3593 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3594 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3595 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
3596 			/* Scheduling the callback failed */
3597 			return (TRAN_BUSY);
3598 		}
3599 	}
3600 	return (TRAN_ACCEPT);
3601 }
3602 
3603 
3604 /*
3605  * SATA translate command: Inquiry / Identify Device
3606  * Use cached Identify Device data for now, rather than issuing actual
3607  * Device Identify cmd request. If device is detached and re-attached,
3608  * asynchronous event processing should fetch and refresh Identify Device
3609  * data.
3610  * VPD pages supported now:
3611  * Vital Product Data page
3612  * Unit Serial Number page
3613  * Block Device Characteristics Page
3614  * ATA Information Page
3615  *
3616  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3617  */
3618 
3619 #define	EVPD			1	/* Extended Vital Product Data flag */
3620 #define	CMDDT			2	/* Command Support Data - Obsolete */
3621 #define	INQUIRY_SUP_VPD_PAGE	0	/* Supported VPD Pages Page Code */
3622 #define	INQUIRY_USN_PAGE	0x80	/* Unit Serial Number Page Code */
3623 #define	INQUIRY_BDC_PAGE	0xB1	/* Block Device Characteristics Page */
3624 					/* Code */
3625 #define	INQUIRY_ATA_INFO_PAGE	0x89	/* ATA Information Page Code */
3626 #define	INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */
3627 
3628 static int
3629 sata_txlt_inquiry(sata_pkt_txlate_t *spx)
3630 {
3631 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3632 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3633 	sata_drive_info_t *sdinfo;
3634 	struct scsi_extended_sense *sense;
3635 	int count;
3636 	uint8_t *p;
3637 	int i, j;
3638 	uint8_t page_buf[1024]; /* Max length */
3639 	int rval, reason;
3640 	ushort_t rate;
3641 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
3642 
3643 	mutex_enter(cport_mutex);
3644 
3645 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
3646 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3647 		mutex_exit(cport_mutex);
3648 		return (rval);
3649 	}
3650 
3651 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3652 	    &spx->txlt_sata_pkt->satapkt_device);
3653 
3654 	ASSERT(sdinfo != NULL);
3655 
3656 	scsipkt->pkt_reason = CMD_CMPLT;
3657 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3658 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3659 
3660 	/* Reject not supported request */
3661 	if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */
3662 		*scsipkt->pkt_scbp = STATUS_CHECK;
3663 		sense = sata_arq_sense(spx);
3664 		sense->es_key = KEY_ILLEGAL_REQUEST;
3665 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3666 		goto done;
3667 	}
3668 
3669 	/* Valid Inquiry request */
3670 	*scsipkt->pkt_scbp = STATUS_GOOD;
3671 
3672 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3673 
3674 		/*
3675 		 * Because it is fully emulated command storing data
3676 		 * programatically in the specified buffer, release
3677 		 * preallocated DMA resources before storing data in the buffer,
3678 		 * so no unwanted DMA sync would take place.
3679 		 */
3680 		sata_scsi_dmafree(NULL, scsipkt);
3681 
3682 		if (!(scsipkt->pkt_cdbp[1] & EVPD)) {
3683 			/* Standard Inquiry Data request */
3684 			struct scsi_inquiry inq;
3685 			unsigned int bufsize;
3686 
3687 			sata_identdev_to_inquiry(spx->txlt_sata_hba_inst,
3688 			    sdinfo, (uint8_t *)&inq);
3689 			/* Copy no more than requested */
3690 			count = MIN(bp->b_bcount,
3691 			    sizeof (struct scsi_inquiry));
3692 			bufsize = scsipkt->pkt_cdbp[4];
3693 			bufsize |= scsipkt->pkt_cdbp[3] << 8;
3694 			count = MIN(count, bufsize);
3695 			bcopy(&inq, bp->b_un.b_addr, count);
3696 
3697 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
3698 			scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3699 			    bufsize - count : 0;
3700 		} else {
3701 			/*
3702 			 * peripheral_qualifier = 0;
3703 			 *
3704 			 * We are dealing only with HD and will be
3705 			 * dealing with CD/DVD devices soon
3706 			 */
3707 			uint8_t peripheral_device_type =
3708 			    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3709 			    DTYPE_DIRECT : DTYPE_RODIRECT;
3710 
3711 			bzero(page_buf, sizeof (page_buf));
3712 
3713 			switch ((uint_t)scsipkt->pkt_cdbp[2]) {
3714 			case INQUIRY_SUP_VPD_PAGE:
3715 				/*
3716 				 * Request for supported Vital Product Data
3717 				 * pages.
3718 				 */
3719 				page_buf[0] = peripheral_device_type;
3720 				page_buf[1] = INQUIRY_SUP_VPD_PAGE;
3721 				page_buf[2] = 0;
3722 				page_buf[3] = 4; /* page length */
3723 				page_buf[4] = INQUIRY_SUP_VPD_PAGE;
3724 				page_buf[5] = INQUIRY_USN_PAGE;
3725 				page_buf[6] = INQUIRY_BDC_PAGE;
3726 				page_buf[7] = INQUIRY_ATA_INFO_PAGE;
3727 				/* Copy no more than requested */
3728 				count = MIN(bp->b_bcount, 8);
3729 				bcopy(page_buf, bp->b_un.b_addr, count);
3730 				break;
3731 
3732 			case INQUIRY_USN_PAGE:
3733 				/*
3734 				 * Request for Unit Serial Number page.
3735 				 * Set-up the page.
3736 				 */
3737 				page_buf[0] = peripheral_device_type;
3738 				page_buf[1] = INQUIRY_USN_PAGE;
3739 				page_buf[2] = 0;
3740 				/* remaining page length */
3741 				page_buf[3] = SATA_ID_SERIAL_LEN;
3742 
3743 				/*
3744 				 * Copy serial number from Identify Device data
3745 				 * words into the inquiry page and swap bytes
3746 				 * when necessary.
3747 				 */
3748 				p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser);
3749 #ifdef	_LITTLE_ENDIAN
3750 				swab(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3751 #else
3752 				bcopy(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3753 #endif
3754 				/*
3755 				 * Least significant character of the serial
3756 				 * number shall appear as the last byte,
3757 				 * according to SBC-3 spec.
3758 				 * Count trailing spaces to determine the
3759 				 * necessary shift length.
3760 				 */
3761 				p = &page_buf[SATA_ID_SERIAL_LEN + 4 - 1];
3762 				for (j = 0; j < SATA_ID_SERIAL_LEN; j++) {
3763 					if (*(p - j) != '\0' &&
3764 					    *(p - j) != '\040')
3765 						break;
3766 				}
3767 
3768 				/*
3769 				 * Shift SN string right, so that the last
3770 				 * non-blank character would appear in last
3771 				 * byte of SN field in the page.
3772 				 * 'j' is the shift length.
3773 				 */
3774 				for (i = 0;
3775 				    i < (SATA_ID_SERIAL_LEN - j) && j != 0;
3776 				    i++, p--)
3777 					*p = *(p - j);
3778 
3779 				/*
3780 				 * Add leading spaces - same number as the
3781 				 * shift size
3782 				 */
3783 				for (; j > 0; j--)
3784 					page_buf[4 + j - 1] = '\040';
3785 
3786 				count = MIN(bp->b_bcount,
3787 				    SATA_ID_SERIAL_LEN + 4);
3788 				bcopy(page_buf, bp->b_un.b_addr, count);
3789 				break;
3790 
3791 			case INQUIRY_BDC_PAGE:
3792 				/*
3793 				 * Request for Block Device Characteristics
3794 				 * page.  Set-up the page.
3795 				 */
3796 				page_buf[0] = peripheral_device_type;
3797 				page_buf[1] = INQUIRY_BDC_PAGE;
3798 				page_buf[2] = 0;
3799 				/* remaining page length */
3800 				page_buf[3] = SATA_ID_BDC_LEN;
3801 
3802 				rate = sdinfo->satadrv_id.ai_medrotrate;
3803 				page_buf[4] = (rate >> 8) & 0xff;
3804 				page_buf[5] = rate & 0xff;
3805 				page_buf[6] = 0;
3806 				page_buf[7] = sdinfo->satadrv_id.
3807 				    ai_nomformfactor & 0xf;
3808 
3809 				count = MIN(bp->b_bcount,
3810 				    SATA_ID_BDC_LEN + 4);
3811 				bcopy(page_buf, bp->b_un.b_addr, count);
3812 				break;
3813 
3814 			case INQUIRY_ATA_INFO_PAGE:
3815 				/*
3816 				 * Request for ATA Information page.
3817 				 */
3818 				page_buf[0] = peripheral_device_type;
3819 				page_buf[1] = INQUIRY_ATA_INFO_PAGE;
3820 				page_buf[2] = (SATA_ID_ATA_INFO_LEN >> 8) &
3821 				    0xff;
3822 				page_buf[3] = SATA_ID_ATA_INFO_LEN & 0xff;
3823 				/* page_buf[4-7] reserved */
3824 #ifdef  _LITTLE_ENDIAN
3825 				bcopy("ATA     ", &page_buf[8], 8);
3826 				swab(sdinfo->satadrv_id.ai_model,
3827 				    &page_buf[16], 16);
3828 				if (strncmp(&sdinfo->satadrv_id.ai_fw[4],
3829 				    "    ", 4) == 0) {
3830 					swab(sdinfo->satadrv_id.ai_fw,
3831 					    &page_buf[32], 4);
3832 				} else {
3833 					swab(&sdinfo->satadrv_id.ai_fw[4],
3834 					    &page_buf[32], 4);
3835 				}
3836 #else   /* _LITTLE_ENDIAN */
3837 				bcopy("ATA     ", &page_buf[8], 8);
3838 				bcopy(sdinfo->satadrv_id.ai_model,
3839 				    &page_buf[16], 16);
3840 				if (strncmp(&sdinfo->satadrv_id.ai_fw[4],
3841 				    "    ", 4) == 0) {
3842 					bcopy(sdinfo->satadrv_id.ai_fw,
3843 					    &page_buf[32], 4);
3844 				} else {
3845 					bcopy(&sdinfo->satadrv_id.ai_fw[4],
3846 					    &page_buf[32], 4);
3847 				}
3848 #endif  /* _LITTLE_ENDIAN */
3849 				/*
3850 				 * page_buf[36-55] which defines the device
3851 				 * signature is not defined at this
3852 				 * time.
3853 				 */
3854 
3855 				/* Set the command code */
3856 				if (sdinfo->satadrv_type ==
3857 				    SATA_DTYPE_ATADISK) {
3858 					page_buf[56] = SATAC_ID_DEVICE;
3859 				} else if (sdinfo->satadrv_type ==
3860 				    SATA_DTYPE_ATAPI) {
3861 					page_buf[56] = SATAC_ID_PACKET_DEVICE;
3862 				}
3863 				/*
3864 				 * If the command code, page_buf[56], is not
3865 				 * zero and if one of the identify commands
3866 				 * succeeds, return the identify data.
3867 				 */
3868 				if ((page_buf[56] != 0) &&
3869 				    (sata_fetch_device_identify_data(
3870 				    spx->txlt_sata_hba_inst, sdinfo) ==
3871 				    SATA_SUCCESS)) {
3872 					bcopy(&sdinfo->satadrv_id,
3873 					    &page_buf[60], sizeof (sata_id_t));
3874 				}
3875 
3876 				/* Need to copy out the page_buf to bp */
3877 				count = MIN(bp->b_bcount,
3878 				    SATA_ID_ATA_INFO_LEN + 4);
3879 				bcopy(page_buf, bp->b_un.b_addr, count);
3880 				break;
3881 
3882 			case INQUIRY_DEV_IDENTIFICATION_PAGE:
3883 				/*
3884 				 * We may want to implement this page, when
3885 				 * identifiers are common for SATA devices
3886 				 * But not now.
3887 				 */
3888 				/*FALLTHROUGH*/
3889 
3890 			default:
3891 				/* Request for unsupported VPD page */
3892 				*scsipkt->pkt_scbp = STATUS_CHECK;
3893 				sense = sata_arq_sense(spx);
3894 				sense->es_key = KEY_ILLEGAL_REQUEST;
3895 				sense->es_add_code =
3896 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3897 				goto done;
3898 			}
3899 		}
3900 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3901 		scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3902 		    scsipkt->pkt_cdbp[4] - count : 0;
3903 	}
3904 done:
3905 	mutex_exit(cport_mutex);
3906 
3907 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3908 	    "Scsi_pkt completion reason %x\n",
3909 	    scsipkt->pkt_reason);
3910 
3911 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3912 	    scsipkt->pkt_comp != NULL) {
3913 		/* scsi callback required */
3914 		if (servicing_interrupt()) {
3915 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3916 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3917 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3918 			    TASKQID_INVALID) {
3919 				return (TRAN_BUSY);
3920 			}
3921 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3922 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3923 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
3924 			/* Scheduling the callback failed */
3925 			return (TRAN_BUSY);
3926 		}
3927 	}
3928 	return (TRAN_ACCEPT);
3929 }
3930 
3931 /*
3932  * SATA translate command: Request Sense.
3933  *
3934  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3935  * At the moment this is an emulated command (ATA version for SATA hard disks).
3936  * May be translated into Check Power Mode command in the future.
3937  *
3938  * Note: There is a mismatch between already implemented Informational
3939  * Exception Mode Select page 0x1C and this function.
3940  * When MRIE bit is set in page 0x1C, Request Sense is supposed to return
3941  * NO SENSE and set additional sense code to the exception code - this is not
3942  * implemented here.
3943  */
3944 static int
3945 sata_txlt_request_sense(sata_pkt_txlate_t *spx)
3946 {
3947 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3948 	struct scsi_extended_sense sense;
3949 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3950 	sata_drive_info_t *sdinfo;
3951 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3952 	int rval, reason, power_state = 0;
3953 	kmutex_t *cport_mutex;
3954 
3955 	cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
3956 	mutex_enter(cport_mutex);
3957 
3958 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
3959 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3960 		mutex_exit(cport_mutex);
3961 		return (rval);
3962 	}
3963 
3964 	scsipkt->pkt_reason = CMD_CMPLT;
3965 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3966 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3967 	*scsipkt->pkt_scbp = STATUS_GOOD;
3968 
3969 	/*
3970 	 * when CONTROL field's NACA bit == 1
3971 	 * return ILLEGAL_REQUEST
3972 	 */
3973 	if (scsipkt->pkt_cdbp[5] & CTL_BYTE_NACA_MASK) {
3974 		mutex_exit(cport_mutex);
3975 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
3976 		    SD_SCSI_ASC_CMD_SEQUENCE_ERR));
3977 	}
3978 
3979 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3980 	    &spx->txlt_sata_pkt->satapkt_device);
3981 	ASSERT(sdinfo != NULL);
3982 
3983 	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
3984 
3985 	sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
3986 	scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
3987 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3988 	if (sata_hba_start(spx, &rval) != 0) {
3989 		mutex_exit(cport_mutex);
3990 		return (rval);
3991 	}
3992 	if (scmd->satacmd_error_reg != 0) {
3993 		mutex_exit(cport_mutex);
3994 		return (sata_txlt_check_condition(spx, KEY_NO_SENSE,
3995 		    SD_SCSI_ASC_NO_ADD_SENSE));
3996 	}
3997 
3998 	switch (scmd->satacmd_sec_count_lsb) {
3999 	case SATA_PWRMODE_STANDBY: /* device in standby mode */
4000 		if (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)
4001 			power_state = SATA_POWER_STOPPED;
4002 		else {
4003 			power_state = SATA_POWER_STANDBY;
4004 			sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
4005 		}
4006 		break;
4007 	case SATA_PWRMODE_IDLE: /* device in idle mode */
4008 		power_state = SATA_POWER_IDLE;
4009 		sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4010 		break;
4011 	case SATA_PWRMODE_ACTIVE: /* device in active or idle mode */
4012 	default:		  /* 0x40, 0x41 active mode */
4013 		if (sdinfo->satadrv_power_level == SATA_POWER_IDLE)
4014 			power_state = SATA_POWER_IDLE;
4015 		else {
4016 			power_state = SATA_POWER_ACTIVE;
4017 			sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4018 		}
4019 		break;
4020 	}
4021 
4022 	mutex_exit(cport_mutex);
4023 
4024 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4025 		/*
4026 		 * Because it is fully emulated command storing data
4027 		 * programatically in the specified buffer, release
4028 		 * preallocated DMA resources before storing data in the buffer,
4029 		 * so no unwanted DMA sync would take place.
4030 		 */
4031 		int count = MIN(bp->b_bcount,
4032 		    sizeof (struct scsi_extended_sense));
4033 		sata_scsi_dmafree(NULL, scsipkt);
4034 		bzero(&sense, sizeof (struct scsi_extended_sense));
4035 		sense.es_valid = 0;	/* Valid LBA */
4036 		sense.es_class = 7;	/* Response code 0x70 - current err */
4037 		sense.es_key = KEY_NO_SENSE;
4038 		sense.es_add_len = 6;	/* Additional length */
4039 		/* Copy no more than requested */
4040 		bcopy(&sense, bp->b_un.b_addr, count);
4041 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4042 		scsipkt->pkt_resid = 0;
4043 		switch (power_state) {
4044 		case SATA_POWER_IDLE:
4045 		case SATA_POWER_STANDBY:
4046 			sense.es_add_code =
4047 			    SD_SCSI_ASC_LOW_POWER_CONDITION_ON;
4048 			break;
4049 		case SATA_POWER_STOPPED:
4050 			sense.es_add_code = SD_SCSI_ASC_NO_ADD_SENSE;
4051 			break;
4052 		case SATA_POWER_ACTIVE:
4053 		default:
4054 			break;
4055 		}
4056 	}
4057 
4058 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4059 	    "Scsi_pkt completion reason %x\n",
4060 	    scsipkt->pkt_reason);
4061 
4062 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4063 	    scsipkt->pkt_comp != NULL) {
4064 		/* scsi callback required */
4065 		if (servicing_interrupt()) {
4066 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4067 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4068 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
4069 			    TASKQID_INVALID) {
4070 				return (TRAN_BUSY);
4071 			}
4072 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4073 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4074 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
4075 			/* Scheduling the callback failed */
4076 			return (TRAN_BUSY);
4077 		}
4078 	}
4079 	return (TRAN_ACCEPT);
4080 }
4081 
4082 /*
4083  * SATA translate command: Test Unit Ready
4084  * (ATA version for SATA hard disks).
4085  * It is translated into the Check Power Mode command.
4086  *
4087  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4088  */
4089 static int
4090 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx)
4091 {
4092 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4093 	struct scsi_extended_sense *sense;
4094 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4095 	sata_drive_info_t *sdinfo;
4096 	int power_state;
4097 	int rval, reason;
4098 	kmutex_t *cport_mutex =  &(SATA_TXLT_CPORT_MUTEX(spx));
4099 
4100 	mutex_enter(cport_mutex);
4101 
4102 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
4103 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4104 		mutex_exit(cport_mutex);
4105 		return (rval);
4106 	}
4107 
4108 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4109 	    &spx->txlt_sata_pkt->satapkt_device);
4110 	ASSERT(sdinfo != NULL);
4111 
4112 	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
4113 
4114 	/* send CHECK POWER MODE command */
4115 	sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
4116 	scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
4117 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4118 	if (sata_hba_start(spx, &rval) != 0) {
4119 		mutex_exit(cport_mutex);
4120 		return (rval);
4121 	}
4122 
4123 	if (scmd->satacmd_error_reg != 0) {
4124 		mutex_exit(cport_mutex);
4125 		return (sata_txlt_check_condition(spx, KEY_NOT_READY,
4126 		    SD_SCSI_ASC_LU_NOT_RESPONSE));
4127 	}
4128 
4129 	power_state = scmd->satacmd_sec_count_lsb;
4130 
4131 	/*
4132 	 * return NOT READY when device in STOPPED mode
4133 	 */
4134 	if (power_state == SATA_PWRMODE_STANDBY &&
4135 	    sdinfo->satadrv_power_level == SATA_POWER_STOPPED) {
4136 		*scsipkt->pkt_scbp = STATUS_CHECK;
4137 		sense = sata_arq_sense(spx);
4138 		sense->es_key = KEY_NOT_READY;
4139 		sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY;
4140 	} else {
4141 		/*
4142 		 * For other power mode, return GOOD status
4143 		 */
4144 		*scsipkt->pkt_scbp = STATUS_GOOD;
4145 	}
4146 
4147 	scsipkt->pkt_reason = CMD_CMPLT;
4148 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4149 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4150 
4151 	mutex_exit(cport_mutex);
4152 
4153 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4154 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4155 
4156 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4157 	    scsipkt->pkt_comp != NULL) {
4158 		/* scsi callback required */
4159 		if (servicing_interrupt()) {
4160 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4161 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4162 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
4163 			    TASKQID_INVALID) {
4164 				return (TRAN_BUSY);
4165 			}
4166 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4167 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4168 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
4169 			/* Scheduling the callback failed */
4170 			return (TRAN_BUSY);
4171 		}
4172 	}
4173 
4174 	return (TRAN_ACCEPT);
4175 }
4176 
4177 /*
4178  * SATA translate command: Start Stop Unit
4179  * Translation depends on a command:
4180  *
4181  * Power condition bits will be supported
4182  * and the power level should be maintained by SATL,
4183  * When SATL received a command, it will check the
4184  * power level firstly, and return the status according
4185  * to SAT2 v2.6 and SAT-2 Standby Modifications
4186  *
4187  * SPC-4/SBC-3      SATL    ATA power condition  SATL      SPC/SBC
4188  * -----------------------------------------------------------------------
4189  * SSU_PC1 Active   <==>     ATA  Active         <==>     SSU:start_bit =1
4190  * SSU_PC2 Idle     <==>     ATA  Idle           <==>     N/A
4191  * SSU_PC3 Standby  <==>     ATA  Standby        <==>     N/A
4192  * SSU_PC4 Stopped  <==>     ATA  Standby        <==>     SSU:start_bit = 0
4193  *
4194  *	Unload Media / NOT SUPPORTED YET
4195  *	Load Media / NOT SUPPROTED YET
4196  *	Immediate bit / NOT SUPPORTED YET (deferred error)
4197  *
4198  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4199  * appropriate values in scsi_pkt fields.
4200  */
4201 static int
4202 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx)
4203 {
4204 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4205 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4206 	int rval, reason;
4207 	sata_drive_info_t *sdinfo;
4208 	sata_id_t *sata_id;
4209 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4210 
4211 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4212 	    "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1);
4213 
4214 	mutex_enter(cport_mutex);
4215 
4216 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
4217 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4218 		mutex_exit(cport_mutex);
4219 		return (rval);
4220 	}
4221 
4222 	if (scsipkt->pkt_cdbp[1] & START_STOP_IMMED_MASK) {
4223 		/* IMMED bit - not supported */
4224 		mutex_exit(cport_mutex);
4225 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4226 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4227 	}
4228 
4229 	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
4230 	spx->txlt_sata_pkt->satapkt_comp = NULL;
4231 
4232 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4233 	    &spx->txlt_sata_pkt->satapkt_device);
4234 	ASSERT(sdinfo != NULL);
4235 	sata_id = &sdinfo->satadrv_id;
4236 
4237 	switch ((scsipkt->pkt_cdbp[4] & START_STOP_POWER_COND_MASK) >> 4) {
4238 	case 0:
4239 		if (scsipkt->pkt_cdbp[4] & START_STOP_LOEJ_MASK) {
4240 			/* Load/Unload Media - invalid request */
4241 			goto err_out;
4242 		}
4243 		if (scsipkt->pkt_cdbp[4] & START_STOP_START_MASK) {
4244 			/* Start Unit */
4245 			sata_build_read_verify_cmd(scmd, 1, 5);
4246 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4247 			/* Transfer command to HBA */
4248 			if (sata_hba_start(spx, &rval) != 0) {
4249 				/* Pkt not accepted for execution */
4250 				mutex_exit(cport_mutex);
4251 				return (rval);
4252 			}
4253 			if (scmd->satacmd_error_reg != 0) {
4254 				goto err_out;
4255 			}
4256 			sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4257 		} else {
4258 			/* Stop Unit */
4259 			sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4260 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4261 			if (sata_hba_start(spx, &rval) != 0) {
4262 				mutex_exit(cport_mutex);
4263 				return (rval);
4264 			} else {
4265 				if (scmd->satacmd_error_reg != 0) {
4266 					goto err_out;
4267 				}
4268 			}
4269 			/* ata standby immediate command */
4270 			sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
4271 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4272 			if (sata_hba_start(spx, &rval) != 0) {
4273 				mutex_exit(cport_mutex);
4274 				return (rval);
4275 			}
4276 			if (scmd->satacmd_error_reg != 0) {
4277 				goto err_out;
4278 			}
4279 			sdinfo->satadrv_power_level = SATA_POWER_STOPPED;
4280 		}
4281 		break;
4282 	case 0x1:
4283 		sata_build_generic_cmd(scmd, SATAC_IDLE);
4284 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4285 		if (sata_hba_start(spx, &rval) != 0) {
4286 			mutex_exit(cport_mutex);
4287 			return (rval);
4288 		}
4289 		if (scmd->satacmd_error_reg != 0) {
4290 			goto err_out;
4291 		}
4292 		sata_build_read_verify_cmd(scmd, 1, 5);
4293 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4294 		/* Transfer command to HBA */
4295 		if (sata_hba_start(spx, &rval) != 0) {
4296 			/* Pkt not accepted for execution */
4297 			mutex_exit(cport_mutex);
4298 			return (rval);
4299 		} else {
4300 			if (scmd->satacmd_error_reg != 0) {
4301 				goto err_out;
4302 			}
4303 		}
4304 		sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4305 		break;
4306 	case 0x2:
4307 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4308 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4309 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4310 			if (sata_hba_start(spx, &rval) != 0) {
4311 				mutex_exit(cport_mutex);
4312 				return (rval);
4313 			}
4314 			if (scmd->satacmd_error_reg != 0) {
4315 				goto err_out;
4316 			}
4317 		}
4318 		sata_build_generic_cmd(scmd, SATAC_IDLE);
4319 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4320 		if (sata_hba_start(spx, &rval) != 0) {
4321 			mutex_exit(cport_mutex);
4322 			return (rval);
4323 		}
4324 		if (scmd->satacmd_error_reg != 0) {
4325 			goto err_out;
4326 		}
4327 		if ((scsipkt->pkt_cdbp[3] & START_STOP_MODIFIER_MASK)) {
4328 			/*
4329 			 *  POWER CONDITION MODIFIER bit set
4330 			 *  to 0x1 or larger it will be handled
4331 			 *  on the same way as bit = 0x1
4332 			 */
4333 			if (!(sata_id->ai_cmdset84 &
4334 			    SATA_IDLE_UNLOAD_SUPPORTED)) {
4335 				sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4336 				break;
4337 			}
4338 			sata_build_generic_cmd(scmd, SATAC_IDLE_IM);
4339 			scmd->satacmd_features_reg = 0x44;
4340 			scmd->satacmd_lba_low_lsb = 0x4c;
4341 			scmd->satacmd_lba_mid_lsb = 0x4e;
4342 			scmd->satacmd_lba_high_lsb = 0x55;
4343 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4344 			if (sata_hba_start(spx, &rval) != 0) {
4345 				mutex_exit(cport_mutex);
4346 				return (rval);
4347 			}
4348 			if (scmd->satacmd_error_reg != 0) {
4349 				goto err_out;
4350 			}
4351 		}
4352 		sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4353 		break;
4354 	case 0x3:
4355 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4356 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4357 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4358 			if (sata_hba_start(spx, &rval) != 0) {
4359 				mutex_exit(cport_mutex);
4360 				return (rval);
4361 			}
4362 			if (scmd->satacmd_error_reg != 0) {
4363 				goto err_out;
4364 			}
4365 		}
4366 		sata_build_generic_cmd(scmd, SATAC_STANDBY);
4367 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4368 		if (sata_hba_start(spx, &rval) != 0) {
4369 			mutex_exit(cport_mutex);
4370 			return (rval);
4371 		}
4372 		if (scmd->satacmd_error_reg != 0) {
4373 			goto err_out;
4374 		}
4375 		sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
4376 		break;
4377 	case 0x7:
4378 		sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
4379 		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
4380 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4381 		if (sata_hba_start(spx, &rval) != 0) {
4382 			mutex_exit(cport_mutex);
4383 			return (rval);
4384 		}
4385 		if (scmd->satacmd_error_reg != 0) {
4386 			goto err_out;
4387 		}
4388 		switch (scmd->satacmd_sec_count_lsb) {
4389 		case SATA_PWRMODE_STANDBY:
4390 			sata_build_generic_cmd(scmd, SATAC_STANDBY);
4391 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4392 			    sdinfo->satadrv_standby_timer);
4393 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4394 			if (sata_hba_start(spx, &rval) != 0) {
4395 				mutex_exit(cport_mutex);
4396 				return (rval);
4397 			} else {
4398 				if (scmd->satacmd_error_reg != 0) {
4399 					goto err_out;
4400 				}
4401 			}
4402 			break;
4403 		case SATA_PWRMODE_IDLE:
4404 			sata_build_generic_cmd(scmd, SATAC_IDLE);
4405 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4406 			    sdinfo->satadrv_standby_timer);
4407 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4408 			if (sata_hba_start(spx, &rval) != 0) {
4409 				mutex_exit(cport_mutex);
4410 				return (rval);
4411 			} else {
4412 				if (scmd->satacmd_error_reg != 0) {
4413 					goto err_out;
4414 				}
4415 			}
4416 			break;
4417 		case SATA_PWRMODE_ACTIVE_SPINDOWN:
4418 		case SATA_PWRMODE_ACTIVE_SPINUP:
4419 		case SATA_PWRMODE_ACTIVE:
4420 			sata_build_generic_cmd(scmd, SATAC_IDLE);
4421 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4422 			    sdinfo->satadrv_standby_timer);
4423 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4424 			if (sata_hba_start(spx, &rval) != 0) {
4425 				mutex_exit(cport_mutex);
4426 				return (rval);
4427 			}
4428 			if (scmd->satacmd_error_reg != 0) {
4429 				goto err_out;
4430 			}
4431 			sata_build_read_verify_cmd(scmd, 1, 5);
4432 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4433 			if (sata_hba_start(spx, &rval) != 0) {
4434 				mutex_exit(cport_mutex);
4435 				return (rval);
4436 			}
4437 			if (scmd->satacmd_error_reg != 0) {
4438 				goto err_out;
4439 			}
4440 			break;
4441 		default:
4442 			goto err_out;
4443 		}
4444 		break;
4445 	case 0xb:
4446 		if ((sata_get_standby_timer(sdinfo->satadrv_standby_timer) ==
4447 		    0) || (!(sata_id->ai_cap & SATA_STANDBYTIMER))) {
4448 			mutex_exit(cport_mutex);
4449 			return (sata_txlt_check_condition(spx,
4450 			    KEY_ILLEGAL_REQUEST,
4451 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4452 		}
4453 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4454 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4455 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4456 			if (sata_hba_start(spx, &rval) != 0) {
4457 				mutex_exit(cport_mutex);
4458 				return (rval);
4459 			}
4460 			if (scmd->satacmd_error_reg != 0) {
4461 				goto err_out;
4462 			}
4463 			sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
4464 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4465 			if (sata_hba_start(spx, &rval) != 0) {
4466 				mutex_exit(cport_mutex);
4467 				return (rval);
4468 			}
4469 			if (scmd->satacmd_error_reg != 0) {
4470 				goto err_out;
4471 			}
4472 		}
4473 		bzero(sdinfo->satadrv_standby_timer, sizeof (uchar_t) * 4);
4474 		break;
4475 	default:
4476 err_out:
4477 		mutex_exit(cport_mutex);
4478 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4479 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4480 	}
4481 
4482 	/*
4483 	 * Since it was a synchronous command,
4484 	 * a callback function will be called directly.
4485 	 */
4486 	mutex_exit(cport_mutex);
4487 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4488 	    "synchronous execution status %x\n",
4489 	    spx->txlt_sata_pkt->satapkt_reason);
4490 
4491 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4492 	    scsipkt->pkt_comp != NULL) {
4493 		sata_set_arq_data(spx->txlt_sata_pkt);
4494 		if (servicing_interrupt()) {
4495 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4496 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4497 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
4498 			    TASKQID_INVALID) {
4499 				return (TRAN_BUSY);
4500 			}
4501 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4502 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4503 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
4504 			/* Scheduling the callback failed */
4505 			return (TRAN_BUSY);
4506 		}
4507 	}
4508 	else
4509 
4510 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
4511 
4512 	return (TRAN_ACCEPT);
4513 
4514 }
4515 
4516 /*
4517  * SATA translate command:  Read Capacity.
4518  * Emulated command for SATA disks.
4519  * Capacity is retrieved from cached Idenifty Device data.
4520  * Identify Device data shows effective disk capacity, not the native
4521  * capacity, which may be limitted by Set Max Address command.
4522  * This is ATA version for SATA hard disks.
4523  *
4524  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4525  */
4526 static int
4527 sata_txlt_read_capacity(sata_pkt_txlate_t *spx)
4528 {
4529 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4530 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4531 	sata_drive_info_t *sdinfo;
4532 	uint64_t val;
4533 	uint32_t lbsize = DEV_BSIZE;
4534 	uchar_t *rbuf;
4535 	int rval, reason;
4536 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4537 
4538 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4539 	    "sata_txlt_read_capacity: ", NULL);
4540 
4541 	mutex_enter(cport_mutex);
4542 
4543 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4544 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4545 		mutex_exit(cport_mutex);
4546 		return (rval);
4547 	}
4548 
4549 	scsipkt->pkt_reason = CMD_CMPLT;
4550 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4551 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4552 	*scsipkt->pkt_scbp = STATUS_GOOD;
4553 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4554 		/*
4555 		 * Because it is fully emulated command storing data
4556 		 * programatically in the specified buffer, release
4557 		 * preallocated DMA resources before storing data in the buffer,
4558 		 * so no unwanted DMA sync would take place.
4559 		 */
4560 		sata_scsi_dmafree(NULL, scsipkt);
4561 
4562 		sdinfo = sata_get_device_info(
4563 		    spx->txlt_sata_hba_inst,
4564 		    &spx->txlt_sata_pkt->satapkt_device);
4565 
4566 		/*
4567 		 * As per SBC-3, the "returned LBA" is either the highest
4568 		 * addressable LBA or 0xffffffff, whichever is smaller.
4569 		 */
4570 		val = MIN(sdinfo->satadrv_capacity - 1, UINT32_MAX);
4571 
4572 		if (sdinfo->satadrv_id.ai_phys_sect_sz & SATA_L2PS_CHECK_BIT) {
4573 			/* physical/logical sector size word is valid */
4574 
4575 			if (sdinfo->satadrv_id.ai_phys_sect_sz &
4576 			    SATA_L2PS_BIG_SECTORS) {
4577 				/* if this set 117-118 words are valid */
4578 				lbsize = sdinfo->satadrv_id.ai_words_lsec[0] |
4579 				    (sdinfo->satadrv_id.ai_words_lsec[1] << 16);
4580 				lbsize <<= 1; /* convert from words to bytes */
4581 			}
4582 		}
4583 		rbuf = (uchar_t *)bp->b_un.b_addr;
4584 		/* Need to swap endians to match scsi format */
4585 		rbuf[0] = (val >> 24) & 0xff;
4586 		rbuf[1] = (val >> 16) & 0xff;
4587 		rbuf[2] = (val >> 8) & 0xff;
4588 		rbuf[3] = val & 0xff;
4589 		rbuf[4] = (lbsize >> 24) & 0xff;
4590 		rbuf[5] = (lbsize >> 16) & 0xff;
4591 		rbuf[6] = (lbsize >> 8) & 0xff;
4592 		rbuf[7] = lbsize & 0xff;
4593 
4594 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4595 		scsipkt->pkt_resid = 0;
4596 
4597 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n",
4598 		    sdinfo->satadrv_capacity -1);
4599 	}
4600 	mutex_exit(cport_mutex);
4601 	/*
4602 	 * If a callback was requested, do it now.
4603 	 */
4604 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4605 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4606 
4607 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4608 	    scsipkt->pkt_comp != NULL) {
4609 		/* scsi callback required */
4610 		if (servicing_interrupt()) {
4611 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4612 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4613 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
4614 			    TASKQID_INVALID) {
4615 				return (TRAN_BUSY);
4616 			}
4617 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4618 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4619 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
4620 			/* Scheduling the callback failed */
4621 			return (TRAN_BUSY);
4622 		}
4623 	}
4624 
4625 	return (TRAN_ACCEPT);
4626 }
4627 
4628 /*
4629  * SATA translate command:  Read Capacity (16).
4630  * Emulated command for SATA disks.
4631  * Info is retrieved from cached Identify Device data.
4632  * Implemented to SBC-3 (draft 21) and SAT-2 (final) specifications.
4633  *
4634  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4635  */
4636 static int
4637 sata_txlt_read_capacity16(sata_pkt_txlate_t *spx)
4638 {
4639 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4640 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4641 	sata_drive_info_t *sdinfo;
4642 	uint64_t val;
4643 	uint16_t l2p_exp;
4644 	uint32_t lbsize = DEV_BSIZE;
4645 	uchar_t *rbuf;
4646 	int rval, reason;
4647 #define	TPE	0x80
4648 #define	TPRZ	0x40
4649 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4650 
4651 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4652 	    "sata_txlt_read_capacity: ", NULL);
4653 
4654 	mutex_enter(cport_mutex);
4655 
4656 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4657 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4658 		mutex_exit(cport_mutex);
4659 		return (rval);
4660 	}
4661 
4662 	scsipkt->pkt_reason = CMD_CMPLT;
4663 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4664 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4665 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4666 		/*
4667 		 * Because it is fully emulated command storing data
4668 		 * programatically in the specified buffer, release
4669 		 * preallocated DMA resources before storing data in the buffer,
4670 		 * so no unwanted DMA sync would take place.
4671 		 */
4672 		sata_scsi_dmafree(NULL, scsipkt);
4673 
4674 		/* Check SERVICE ACTION field */
4675 		if ((scsipkt->pkt_cdbp[1] & 0x1f) !=
4676 		    SSVC_ACTION_READ_CAPACITY_G4) {
4677 			mutex_exit(cport_mutex);
4678 			return (sata_txlt_check_condition(spx,
4679 			    KEY_ILLEGAL_REQUEST,
4680 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4681 		}
4682 
4683 		/* Check LBA field */
4684 		if ((scsipkt->pkt_cdbp[2] != 0) ||
4685 		    (scsipkt->pkt_cdbp[3] != 0) ||
4686 		    (scsipkt->pkt_cdbp[4] != 0) ||
4687 		    (scsipkt->pkt_cdbp[5] != 0) ||
4688 		    (scsipkt->pkt_cdbp[6] != 0) ||
4689 		    (scsipkt->pkt_cdbp[7] != 0) ||
4690 		    (scsipkt->pkt_cdbp[8] != 0) ||
4691 		    (scsipkt->pkt_cdbp[9] != 0)) {
4692 			mutex_exit(cport_mutex);
4693 			return (sata_txlt_check_condition(spx,
4694 			    KEY_ILLEGAL_REQUEST,
4695 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4696 		}
4697 
4698 		/* Check PMI bit */
4699 		if (scsipkt->pkt_cdbp[14] & 0x1) {
4700 			mutex_exit(cport_mutex);
4701 			return (sata_txlt_check_condition(spx,
4702 			    KEY_ILLEGAL_REQUEST,
4703 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4704 		}
4705 
4706 		*scsipkt->pkt_scbp = STATUS_GOOD;
4707 
4708 		sdinfo = sata_get_device_info(
4709 		    spx->txlt_sata_hba_inst,
4710 		    &spx->txlt_sata_pkt->satapkt_device);
4711 
4712 		/* last logical block address */
4713 		val = MIN(sdinfo->satadrv_capacity - 1,
4714 		    SCSI_READ_CAPACITY16_MAX_LBA);
4715 
4716 		/* logical to physical block size exponent */
4717 		l2p_exp = 0;
4718 		if (sdinfo->satadrv_id.ai_phys_sect_sz & SATA_L2PS_CHECK_BIT) {
4719 			/* physical/logical sector size word is valid */
4720 
4721 			if (sdinfo->satadrv_id.ai_phys_sect_sz &
4722 			    SATA_L2PS_HAS_MULT) {
4723 				/* multiple logical sectors per phys sectors */
4724 				l2p_exp =
4725 				    sdinfo->satadrv_id.ai_phys_sect_sz &
4726 				    SATA_L2PS_EXP_MASK;
4727 			}
4728 
4729 			if (sdinfo->satadrv_id.ai_phys_sect_sz &
4730 			    SATA_L2PS_BIG_SECTORS) {
4731 				/* if this set 117-118 words are valid */
4732 				lbsize = sdinfo->satadrv_id.ai_words_lsec[0] |
4733 				    (sdinfo->satadrv_id.ai_words_lsec[1] << 16);
4734 				lbsize <<= 1; /* convert from words to bytes */
4735 			}
4736 		}
4737 
4738 		rbuf = (uchar_t *)bp->b_un.b_addr;
4739 		bzero(rbuf, bp->b_bcount);
4740 
4741 		/* returned logical block address */
4742 		rbuf[0] = (val >> 56) & 0xff;
4743 		rbuf[1] = (val >> 48) & 0xff;
4744 		rbuf[2] = (val >> 40) & 0xff;
4745 		rbuf[3] = (val >> 32) & 0xff;
4746 		rbuf[4] = (val >> 24) & 0xff;
4747 		rbuf[5] = (val >> 16) & 0xff;
4748 		rbuf[6] = (val >> 8) & 0xff;
4749 		rbuf[7] = val & 0xff;
4750 		rbuf[8] = (lbsize >> 24) & 0xff;
4751 		rbuf[9] = (lbsize >> 16) & 0xff;
4752 		rbuf[10] = (lbsize >> 8) & 0xff;
4753 		rbuf[11] = lbsize & 0xff;
4754 
4755 		/* p_type, prot_en, unspecified by SAT-2 */
4756 		/* rbuf[12] = 0; */
4757 
4758 		/* p_i_exponent, undefined by SAT-2 */
4759 		/* logical blocks per physical block exponent */
4760 		rbuf[13] = l2p_exp;
4761 
4762 		/*
4763 		 * tpe and tprz as defined in T10/10-079 r0.
4764 		 * TRIM support is indicated by the relevant bit in the data
4765 		 * set management word. Read-after-trim behavior is indicated
4766 		 * by the additional bits in the identify device word. Of the
4767 		 * three defined possibilities, we only flag read-zero.
4768 		 */
4769 		if (sdinfo->satadrv_id.ai_dsm & SATA_DSM_TRIM) {
4770 			rbuf[14] |= TPE;
4771 
4772 			if ((sdinfo->satadrv_id.ai_addsupported &
4773 			    SATA_DETERMINISTIC_READ) &&
4774 			    (sdinfo->satadrv_id.ai_addsupported &
4775 			    SATA_READ_ZERO)) {
4776 				rbuf[14] |= TPRZ;
4777 			}
4778 		}
4779 
4780 		/* lowest aligned logical block address = 0 (for now) */
4781 		/* rbuf[15] = 0; */
4782 
4783 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4784 		scsipkt->pkt_resid = 0;
4785 
4786 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%llu\n",
4787 		    sdinfo->satadrv_capacity -1);
4788 	}
4789 
4790 	mutex_exit(cport_mutex);
4791 
4792 	/*
4793 	 * If a callback was requested, do it now.
4794 	 */
4795 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4796 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4797 
4798 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4799 	    scsipkt->pkt_comp != NULL) {
4800 		/* scsi callback required */
4801 		if (servicing_interrupt()) {
4802 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4803 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4804 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
4805 			    TASKQID_INVALID) {
4806 				return (TRAN_BUSY);
4807 			}
4808 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4809 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4810 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
4811 			/* Scheduling the callback failed */
4812 			return (TRAN_BUSY);
4813 		}
4814 	}
4815 
4816 	return (TRAN_ACCEPT);
4817 }
4818 
4819 /*
4820  * Translate command: UNMAP
4821  *
4822  * The function cannot be called in interrupt context since it may sleep.
4823  */
4824 static int
4825 sata_txlt_unmap(sata_pkt_txlate_t *spx)
4826 {
4827 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4828 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4829 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4830 	uint16_t count = 0;
4831 	int synch;
4832 	int rval, reason;
4833 	int i, x;
4834 	int bdlen = 0;
4835 	int ranges = 0;
4836 	int paramlen = 8;
4837 	uint8_t *data, *tmpbd;
4838 	sata_drive_info_t *sdinfo;
4839 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4840 #define	TRIM	0x1
4841 
4842 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4843 	    "sata_txlt_unmap: ", NULL);
4844 
4845 	mutex_enter(cport_mutex);
4846 
4847 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4848 	    &spx->txlt_sata_pkt->satapkt_device);
4849 	if (sdinfo != NULL) {
4850 		SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4851 		    "DSM support 0x%x, max number of 512 byte blocks of LBA "
4852 		    "range entries 0x%x\n", sdinfo->satadrv_id.ai_dsm,
4853 		    sdinfo->satadrv_id.ai_maxcount);
4854 	}
4855 
4856 	rval = sata_txlt_generic_pkt_info(spx, &reason, 1);
4857 	if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4858 		mutex_exit(cport_mutex);
4859 		return (rval);
4860 	}
4861 
4862 	/*
4863 	 * Need to modify bp to have TRIM data instead of UNMAP data.
4864 	 * Start by getting the block descriptor data length by subtracting
4865 	 * the 8 byte parameter list header from the parameter list length.
4866 	 * The block descriptor size has to be a multiple of 16 bytes.
4867 	 */
4868 	bdlen = scsipkt->pkt_cdbp[7];
4869 	bdlen = (bdlen << 8) + scsipkt->pkt_cdbp[8] - paramlen;
4870 	if ((bdlen < 0) || ((bdlen % 16) != 0) ||
4871 	    ((bp != NULL) && (bdlen > (bp->b_bcount - paramlen)))) {
4872 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4873 		    "sata_txlt_unmap: invalid block descriptor length", NULL);
4874 		mutex_exit(cport_mutex);
4875 		return ((sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4876 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB)));
4877 	}
4878 	/*
4879 	 * If there are no parameter data or block descriptors, it is not
4880 	 * considered an error so just complete the command without sending
4881 	 * TRIM.
4882 	 */
4883 	if ((bdlen == 0) || (bp == NULL) || (bp->b_un.b_addr == NULL) ||
4884 	    (bp->b_bcount == 0)) {
4885 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4886 		    "sata_txlt_unmap: no parameter data or block descriptors",
4887 		    NULL);
4888 		mutex_exit(cport_mutex);
4889 		return (sata_txlt_unmap_nodata_cmd(spx));
4890 	}
4891 	tmpbd = (uint8_t *)bp->b_un.b_addr + paramlen;
4892 	data = kmem_zalloc(bdlen, KM_SLEEP);
4893 
4894 	/*
4895 	 * Loop through all the UNMAP block descriptors and convert the data
4896 	 * into TRIM format.
4897 	 */
4898 	for (i = 0, x = 0; i < bdlen; i += 16, x += 8) {
4899 		/* get range length */
4900 		data[x] = tmpbd[i+7];
4901 		data[x+1] = tmpbd[i+6];
4902 		/* get LBA */
4903 		data[x+2] = tmpbd[i+5];
4904 		data[x+3] = tmpbd[i+4];
4905 		data[x+4] = tmpbd[i+3];
4906 		data[x+5] = tmpbd[i+2];
4907 		data[x+6] = tmpbd[i+11];
4908 		data[x+7] = tmpbd[i+10];
4909 
4910 		ranges++;
4911 	}
4912 
4913 	/*
4914 	 * The TRIM command expects the data buffer to be a multiple of
4915 	 * 512-byte blocks of range entries.  This means that the UNMAP buffer
4916 	 * may be too small.  Free the original DMA resources and create a
4917 	 * local buffer.
4918 	 */
4919 	sata_common_free_dma_rsrcs(spx);
4920 
4921 	/*
4922 	 * Get count of 512-byte blocks of range entries.  The length
4923 	 * of a range entry is 8 bytes which means one count has 64 range
4924 	 * entries.
4925 	 */
4926 	count = (ranges + 63)/64;
4927 
4928 	/* Allocate a buffer that is a multiple of 512 bytes. */
4929 	mutex_exit(cport_mutex);
4930 	bp = sata_alloc_local_buffer(spx, count * 512);
4931 	if (bp == NULL) {
4932 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
4933 		    "sata_txlt_unmap: "
4934 		    "cannot allocate buffer for TRIM command", NULL);
4935 		kmem_free(data, bdlen);
4936 		return (TRAN_BUSY);
4937 	}
4938 	bp_mapin(bp); /* make data buffer accessible */
4939 	mutex_enter(cport_mutex);
4940 
4941 	bzero(bp->b_un.b_addr, bp->b_bcount);
4942 	bcopy(data, bp->b_un.b_addr, x);
4943 	kmem_free(data, bdlen);
4944 	rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
4945 	    DDI_DMA_SYNC_FORDEV);
4946 	ASSERT(rval == DDI_SUCCESS);
4947 
4948 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4949 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4950 	scmd->satacmd_cmd_reg = SATAC_DSM;
4951 	scmd->satacmd_sec_count_msb = (count >> 8) & 0xff;
4952 	scmd->satacmd_sec_count_lsb = count & 0xff;
4953 	scmd->satacmd_features_reg = TRIM;
4954 	scmd->satacmd_device_reg = SATA_ADH_LBA;
4955 	scmd->satacmd_status_reg = 0;
4956 	scmd->satacmd_error_reg = 0;
4957 
4958 	/* Start processing command */
4959 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4960 		spx->txlt_sata_pkt->satapkt_comp =
4961 		    sata_txlt_unmap_completion;
4962 		synch = FALSE;
4963 	} else {
4964 		synch = TRUE;
4965 	}
4966 
4967 	if (sata_hba_start(spx, &rval) != 0) {
4968 		mutex_exit(cport_mutex);
4969 		return (rval);
4970 	}
4971 
4972 	mutex_exit(cport_mutex);
4973 
4974 	if (synch) {
4975 		sata_txlt_unmap_completion(spx->txlt_sata_pkt);
4976 	}
4977 
4978 	return (TRAN_ACCEPT);
4979 }
4980 
4981 /*
4982  * SATA translate command: Mode Sense.
4983  * Translated into appropriate SATA command or emulated.
4984  * Saved Values Page Control (03) are not supported.
4985  *
4986  * NOTE: only caching mode sense page is currently implemented.
4987  *
4988  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4989  */
4990 
4991 #define	LLBAA	0x10	/* Long LBA Accepted */
4992 
4993 static int
4994 sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
4995 {
4996 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4997 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4998 	sata_drive_info_t *sdinfo;
4999 	sata_id_t *sata_id;
5000 	struct scsi_extended_sense *sense;
5001 	int		len, bdlen, count, alc_len;
5002 	int		pc;	/* Page Control code */
5003 	uint8_t		*buf;	/* mode sense buffer */
5004 	int		rval, reason;
5005 	kmutex_t	*cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5006 
5007 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5008 	    "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
5009 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5010 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5011 
5012 	if (servicing_interrupt()) {
5013 		buf = kmem_zalloc(1024, KM_NOSLEEP);
5014 		if (buf == NULL) {
5015 			return (TRAN_BUSY);
5016 		}
5017 	} else {
5018 		buf = kmem_zalloc(1024, KM_SLEEP);
5019 	}
5020 
5021 	mutex_enter(cport_mutex);
5022 
5023 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
5024 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5025 		mutex_exit(cport_mutex);
5026 		kmem_free(buf, 1024);
5027 		return (rval);
5028 	}
5029 
5030 	scsipkt->pkt_reason = CMD_CMPLT;
5031 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5032 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5033 
5034 	pc = scsipkt->pkt_cdbp[2] >> 6;
5035 
5036 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
5037 		/*
5038 		 * Because it is fully emulated command storing data
5039 		 * programatically in the specified buffer, release
5040 		 * preallocated DMA resources before storing data in the buffer,
5041 		 * so no unwanted DMA sync would take place.
5042 		 */
5043 		sata_scsi_dmafree(NULL, scsipkt);
5044 
5045 		len = 0;
5046 		bdlen = 0;
5047 		if (!(scsipkt->pkt_cdbp[1] & 8)) {
5048 			if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
5049 			    (scsipkt->pkt_cdbp[1] & LLBAA))
5050 				bdlen = 16;
5051 			else
5052 				bdlen = 8;
5053 		}
5054 		/* Build mode parameter header */
5055 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5056 			/* 4-byte mode parameter header */
5057 			buf[len++] = 0;		/* mode data length */
5058 			buf[len++] = 0;		/* medium type */
5059 			buf[len++] = 0;		/* dev-specific param */
5060 			buf[len++] = bdlen;	/* Block Descriptor length */
5061 		} else {
5062 			/* 8-byte mode parameter header */
5063 			buf[len++] = 0;		/* mode data length */
5064 			buf[len++] = 0;
5065 			buf[len++] = 0;		/* medium type */
5066 			buf[len++] = 0;		/* dev-specific param */
5067 			if (bdlen == 16)
5068 				buf[len++] = 1;	/* long lba descriptor */
5069 			else
5070 				buf[len++] = 0;
5071 			buf[len++] = 0;
5072 			buf[len++] = 0;		/* Block Descriptor length */
5073 			buf[len++] = bdlen;
5074 		}
5075 
5076 		sdinfo = sata_get_device_info(
5077 		    spx->txlt_sata_hba_inst,
5078 		    &spx->txlt_sata_pkt->satapkt_device);
5079 
5080 		/* Build block descriptor only if not disabled (DBD) */
5081 		if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
5082 			/* Block descriptor - direct-access device format */
5083 			if (bdlen == 8) {
5084 				/* build regular block descriptor */
5085 				buf[len++] =
5086 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
5087 				buf[len++] =
5088 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
5089 				buf[len++] =
5090 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
5091 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
5092 				buf[len++] = 0; /* density code */
5093 				buf[len++] = 0;
5094 				if (sdinfo->satadrv_type ==
5095 				    SATA_DTYPE_ATADISK)
5096 					buf[len++] = 2;
5097 				else
5098 					/* ATAPI */
5099 					buf[len++] = 8;
5100 				buf[len++] = 0;
5101 			} else if (bdlen == 16) {
5102 				/* Long LBA Accepted */
5103 				/* build long lba block descriptor */
5104 #ifndef __lock_lint
5105 				buf[len++] =
5106 				    (sdinfo->satadrv_capacity >> 56) & 0xff;
5107 				buf[len++] =
5108 				    (sdinfo->satadrv_capacity >> 48) & 0xff;
5109 				buf[len++] =
5110 				    (sdinfo->satadrv_capacity >> 40) & 0xff;
5111 				buf[len++] =
5112 				    (sdinfo->satadrv_capacity >> 32) & 0xff;
5113 #endif
5114 				buf[len++] =
5115 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
5116 				buf[len++] =
5117 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
5118 				buf[len++] =
5119 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
5120 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
5121 				buf[len++] = 0;
5122 				buf[len++] = 0; /* density code */
5123 				buf[len++] = 0;
5124 				buf[len++] = 0;
5125 				if (sdinfo->satadrv_type ==
5126 				    SATA_DTYPE_ATADISK)
5127 					buf[len++] = 2;
5128 				else
5129 					/* ATAPI */
5130 					buf[len++] = 8;
5131 				buf[len++] = 0;
5132 			}
5133 		}
5134 
5135 		sata_id = &sdinfo->satadrv_id;
5136 
5137 		/*
5138 		 * Add requested pages.
5139 		 * Page 3 and 4 are obsolete and we are not supporting them.
5140 		 * We deal now with:
5141 		 * caching (read/write cache control).
5142 		 * We should eventually deal with following mode pages:
5143 		 * error recovery  (0x01),
5144 		 * power condition (0x1a),
5145 		 * exception control page (enables SMART) (0x1c),
5146 		 * enclosure management (ses),
5147 		 * protocol-specific port mode (port control).
5148 		 */
5149 		switch (scsipkt->pkt_cdbp[2] & 0x3f) {
5150 		case MODEPAGE_RW_ERRRECOV:
5151 			/* DAD_MODE_ERR_RECOV */
5152 			/* R/W recovery */
5153 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
5154 			break;
5155 		case MODEPAGE_CACHING:
5156 			/* DAD_MODE_CACHE */
5157 			/* Reject not supported request for saved parameters */
5158 			if (pc == 3) {
5159 				*scsipkt->pkt_scbp = STATUS_CHECK;
5160 				sense = sata_arq_sense(spx);
5161 				sense->es_key = KEY_ILLEGAL_REQUEST;
5162 				sense->es_add_code =
5163 				    SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
5164 				goto done;
5165 			}
5166 
5167 			/* caching */
5168 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
5169 			break;
5170 		case MODEPAGE_INFO_EXCPT:
5171 			/* exception cntrl */
5172 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
5173 				len += sata_build_msense_page_1c(sdinfo, pc,
5174 				    buf+len);
5175 			}
5176 			else
5177 				goto err;
5178 			break;
5179 		case MODEPAGE_POWER_COND:
5180 			/* DAD_MODE_POWER_COND */
5181 			/* power condition */
5182 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
5183 			break;
5184 
5185 		case MODEPAGE_ACOUSTIC_MANAG:
5186 			/* acoustic management */
5187 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
5188 			break;
5189 		case MODEPAGE_ALLPAGES:
5190 			/* all pages */
5191 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
5192 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
5193 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
5194 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
5195 				len += sata_build_msense_page_1c(sdinfo, pc,
5196 				    buf+len);
5197 			}
5198 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
5199 			break;
5200 		default:
5201 		err:
5202 			/* Invalid request */
5203 			*scsipkt->pkt_scbp = STATUS_CHECK;
5204 			sense = sata_arq_sense(spx);
5205 			sense->es_key = KEY_ILLEGAL_REQUEST;
5206 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5207 			goto done;
5208 		}
5209 
5210 		/* fix total mode data length */
5211 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5212 			/* 4-byte mode parameter header */
5213 			buf[0] = len - 1;	/* mode data length */
5214 		} else {
5215 			buf[0] = (len -2) >> 8;
5216 			buf[1] = (len -2) & 0xff;
5217 		}
5218 
5219 
5220 		/* Check allocation length */
5221 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5222 			alc_len = scsipkt->pkt_cdbp[4];
5223 		} else {
5224 			alc_len = scsipkt->pkt_cdbp[7];
5225 			alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
5226 		}
5227 		/*
5228 		 * We do not check for possible parameters truncation
5229 		 * (alc_len < len) assuming that the target driver works
5230 		 * correctly. Just avoiding overrun.
5231 		 * Copy no more than requested and possible, buffer-wise.
5232 		 */
5233 		count = MIN(alc_len, len);
5234 		count = MIN(bp->b_bcount, count);
5235 		bcopy(buf, bp->b_un.b_addr, count);
5236 
5237 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
5238 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
5239 	}
5240 	*scsipkt->pkt_scbp = STATUS_GOOD;
5241 done:
5242 	mutex_exit(cport_mutex);
5243 	(void) kmem_free(buf, 1024);
5244 
5245 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5246 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5247 
5248 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5249 	    scsipkt->pkt_comp != NULL) {
5250 		/* scsi callback required */
5251 		if (servicing_interrupt()) {
5252 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5253 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5254 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
5255 			    TASKQID_INVALID) {
5256 				return (TRAN_BUSY);
5257 			}
5258 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5259 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5260 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
5261 			/* Scheduling the callback failed */
5262 			return (TRAN_BUSY);
5263 		}
5264 	}
5265 
5266 	return (TRAN_ACCEPT);
5267 }
5268 
5269 
5270 /*
5271  * SATA translate command: Mode Select.
5272  * Translated into appropriate SATA command or emulated.
5273  * Saving parameters is not supported.
5274  * Changing device capacity is not supported (although theoretically
5275  * possible by executing SET FEATURES/SET MAX ADDRESS)
5276  *
5277  * Assumption is that the target driver is working correctly.
5278  *
5279  * More than one SATA command may be executed to perform operations specified
5280  * by mode select pages. The first error terminates further execution.
5281  * Operations performed successully are not backed-up in such case.
5282  *
5283  * NOTE: Implemented pages:
5284  * - caching page
5285  * - informational exception page
5286  * - acoustic management page
5287  * - power condition page
5288  * Caching setup is remembered so it could be re-stored in case of
5289  * an unexpected device reset.
5290  *
5291  * Returns TRAN_XXXX.
5292  * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields.
5293  */
5294 
5295 static int
5296 sata_txlt_mode_select(sata_pkt_txlate_t *spx)
5297 {
5298 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5299 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5300 	struct scsi_extended_sense *sense;
5301 	int len, pagelen, count, pllen;
5302 	uint8_t *buf;	/* mode select buffer */
5303 	int rval, stat, reason;
5304 	uint_t nointr_flag;
5305 	int dmod = 0;
5306 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5307 
5308 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5309 	    "sata_txlt_mode_select, pc %x page code 0x%02x\n",
5310 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5311 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5312 
5313 	mutex_enter(cport_mutex);
5314 
5315 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
5316 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5317 		mutex_exit(cport_mutex);
5318 		return (rval);
5319 	}
5320 
5321 	rval = TRAN_ACCEPT;
5322 
5323 	scsipkt->pkt_reason = CMD_CMPLT;
5324 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5325 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5326 	nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
5327 
5328 	/* Reject not supported request */
5329 	if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
5330 		*scsipkt->pkt_scbp = STATUS_CHECK;
5331 		sense = sata_arq_sense(spx);
5332 		sense->es_key = KEY_ILLEGAL_REQUEST;
5333 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5334 		goto done;
5335 	}
5336 
5337 	if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
5338 		pllen = scsipkt->pkt_cdbp[4];
5339 	} else {
5340 		pllen = scsipkt->pkt_cdbp[7];
5341 		pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
5342 	}
5343 
5344 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
5345 
5346 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
5347 		buf = (uint8_t *)bp->b_un.b_addr;
5348 		count = MIN(bp->b_bcount, pllen);
5349 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
5350 		scsipkt->pkt_resid = 0;
5351 		pllen = count;
5352 
5353 		/*
5354 		 * Check the header to skip the block descriptor(s) - we
5355 		 * do not support setting device capacity.
5356 		 * Existing macros do not recognize long LBA dscriptor,
5357 		 * hence manual calculation.
5358 		 */
5359 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
5360 			/* 6-bytes CMD, 4 bytes header */
5361 			if (count <= 4)
5362 				goto done;		/* header only */
5363 			len = buf[3] + 4;
5364 		} else {
5365 			/* 10-bytes CMD, 8 bytes header */
5366 			if (count <= 8)
5367 				goto done;		/* header only */
5368 			len = buf[6];
5369 			len = (len << 8) + buf[7] + 8;
5370 		}
5371 		if (len >= count)
5372 			goto done;	/* header + descriptor(s) only */
5373 
5374 		pllen -= len;		/* remaining data length */
5375 
5376 		/*
5377 		 * We may be executing SATA command and want to execute it
5378 		 * in SYNCH mode, regardless of scsi_pkt setting.
5379 		 * Save scsi_pkt setting and indicate SYNCH mode
5380 		 */
5381 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5382 		    scsipkt->pkt_comp != NULL) {
5383 			scsipkt->pkt_flags |= FLAG_NOINTR;
5384 		}
5385 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
5386 
5387 		/*
5388 		 * len is now the offset to a first mode select page
5389 		 * Process all pages
5390 		 */
5391 		while (pllen > 0) {
5392 			switch ((int)buf[len]) {
5393 			case MODEPAGE_CACHING:
5394 				/* No support for SP (saving) */
5395 				if (scsipkt->pkt_cdbp[1] & 0x01) {
5396 					*scsipkt->pkt_scbp = STATUS_CHECK;
5397 					sense = sata_arq_sense(spx);
5398 					sense->es_key = KEY_ILLEGAL_REQUEST;
5399 					sense->es_add_code =
5400 					    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5401 					goto done;
5402 				}
5403 				stat = sata_mode_select_page_8(spx,
5404 				    (struct mode_cache_scsi3 *)&buf[len],
5405 				    pllen, &pagelen, &rval, &dmod);
5406 				/*
5407 				 * The pagelen value indicates the number of
5408 				 * parameter bytes already processed.
5409 				 * The rval is the return value from
5410 				 * sata_tran_start().
5411 				 * The stat indicates the overall status of
5412 				 * the operation(s).
5413 				 */
5414 				if (stat != SATA_SUCCESS)
5415 					/*
5416 					 * Page processing did not succeed -
5417 					 * all error info is already set-up,
5418 					 * just return
5419 					 */
5420 					pllen = 0; /* this breaks the loop */
5421 				else {
5422 					len += pagelen;
5423 					pllen -= pagelen;
5424 				}
5425 				break;
5426 
5427 			case MODEPAGE_INFO_EXCPT:
5428 				stat = sata_mode_select_page_1c(spx,
5429 				    (struct mode_info_excpt_page *)&buf[len],
5430 				    pllen, &pagelen, &rval, &dmod);
5431 				/*
5432 				 * The pagelen value indicates the number of
5433 				 * parameter bytes already processed.
5434 				 * The rval is the return value from
5435 				 * sata_tran_start().
5436 				 * The stat indicates the overall status of
5437 				 * the operation(s).
5438 				 */
5439 				if (stat != SATA_SUCCESS)
5440 					/*
5441 					 * Page processing did not succeed -
5442 					 * all error info is already set-up,
5443 					 * just return
5444 					 */
5445 					pllen = 0; /* this breaks the loop */
5446 				else {
5447 					len += pagelen;
5448 					pllen -= pagelen;
5449 				}
5450 				break;
5451 
5452 			case MODEPAGE_ACOUSTIC_MANAG:
5453 				stat = sata_mode_select_page_30(spx,
5454 				    (struct mode_acoustic_management *)
5455 				    &buf[len], pllen, &pagelen, &rval, &dmod);
5456 				/*
5457 				 * The pagelen value indicates the number of
5458 				 * parameter bytes already processed.
5459 				 * The rval is the return value from
5460 				 * sata_tran_start().
5461 				 * The stat indicates the overall status of
5462 				 * the operation(s).
5463 				 */
5464 				if (stat != SATA_SUCCESS)
5465 					/*
5466 					 * Page processing did not succeed -
5467 					 * all error info is already set-up,
5468 					 * just return
5469 					 */
5470 					pllen = 0; /* this breaks the loop */
5471 				else {
5472 					len += pagelen;
5473 					pllen -= pagelen;
5474 				}
5475 
5476 				break;
5477 			case MODEPAGE_POWER_COND:
5478 				stat = sata_mode_select_page_1a(spx,
5479 				    (struct mode_info_power_cond *)&buf[len],
5480 				    pllen, &pagelen, &rval, &dmod);
5481 				/*
5482 				 * The pagelen value indicates the number of
5483 				 * parameter bytes already processed.
5484 				 * The rval is the return value from
5485 				 * sata_tran_start().
5486 				 * The stat indicates the overall status of
5487 				 * the operation(s).
5488 				 */
5489 				if (stat != SATA_SUCCESS)
5490 					/*
5491 					 * Page processing did not succeed -
5492 					 * all error info is already set-up,
5493 					 * just return
5494 					 */
5495 					pllen = 0; /* this breaks the loop */
5496 				else {
5497 					len += pagelen;
5498 					pllen -= pagelen;
5499 				}
5500 				break;
5501 			default:
5502 				*scsipkt->pkt_scbp = STATUS_CHECK;
5503 				sense = sata_arq_sense(spx);
5504 				sense->es_key = KEY_ILLEGAL_REQUEST;
5505 				sense->es_add_code =
5506 				    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
5507 				goto done;
5508 			}
5509 		}
5510 	}
5511 done:
5512 	mutex_exit(cport_mutex);
5513 	/*
5514 	 * If device parameters were modified, fetch and store the new
5515 	 * Identify Device data. Since port mutex could have been released
5516 	 * for accessing HBA driver, we need to re-check device existence.
5517 	 */
5518 	if (dmod != 0) {
5519 		sata_drive_info_t new_sdinfo, *sdinfo;
5520 		int rv = 0;
5521 
5522 		/*
5523 		 * Following statement has to be changed if this function is
5524 		 * used for devices other than SATA hard disks.
5525 		 */
5526 		new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
5527 
5528 		new_sdinfo.satadrv_addr =
5529 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr;
5530 		rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
5531 		    &new_sdinfo);
5532 
5533 		mutex_enter(cport_mutex);
5534 		/*
5535 		 * Since port mutex could have been released when
5536 		 * accessing HBA driver, we need to re-check that the
5537 		 * framework still holds the device info structure.
5538 		 */
5539 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
5540 		    &spx->txlt_sata_pkt->satapkt_device);
5541 		if (sdinfo != NULL) {
5542 			/*
5543 			 * Device still has info structure in the
5544 			 * sata framework. Copy newly fetched info
5545 			 */
5546 			if (rv == 0) {
5547 				sdinfo->satadrv_id = new_sdinfo.satadrv_id;
5548 				sata_save_drive_settings(sdinfo);
5549 			} else {
5550 				/*
5551 				 * Could not fetch new data - invalidate
5552 				 * sata_drive_info. That makes device
5553 				 * unusable.
5554 				 */
5555 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
5556 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
5557 			}
5558 		}
5559 		if (rv != 0 || sdinfo == NULL) {
5560 			/*
5561 			 * This changes the overall mode select completion
5562 			 * reason to a failed one !!!!!
5563 			 */
5564 			*scsipkt->pkt_scbp = STATUS_CHECK;
5565 			sense = sata_arq_sense(spx);
5566 			scsipkt->pkt_reason = CMD_INCOMPLETE;
5567 			rval = TRAN_ACCEPT;
5568 		}
5569 		mutex_exit(cport_mutex);
5570 	}
5571 	/* Restore the scsi pkt flags */
5572 	scsipkt->pkt_flags &= ~FLAG_NOINTR;
5573 	scsipkt->pkt_flags |= nointr_flag;
5574 
5575 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5576 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5577 
5578 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5579 	    scsipkt->pkt_comp != NULL) {
5580 		/* scsi callback required */
5581 		if (servicing_interrupt()) {
5582 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5583 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5584 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
5585 			    TASKQID_INVALID) {
5586 				return (TRAN_BUSY);
5587 			}
5588 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5589 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5590 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
5591 			/* Scheduling the callback failed */
5592 			return (TRAN_BUSY);
5593 		}
5594 	}
5595 
5596 	return (rval);
5597 }
5598 
5599 /*
5600  * Translate command: ATA Pass Through
5601  * Incomplete implementation.  Only supports No-Data, PIO Data-In, and
5602  * PIO Data-Out protocols.  Also supports CK_COND bit.
5603  *
5604  * Mapping of the incoming CDB bytes to the outgoing satacmd bytes is
5605  * described in Table 111 of SAT-2 (Draft 9).
5606  */
5607 static  int
5608 sata_txlt_ata_pass_thru(sata_pkt_txlate_t *spx)
5609 {
5610 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5611 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5612 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5613 	int extend;
5614 	uint64_t lba;
5615 	uint16_t feature, sec_count;
5616 	int t_len, synch;
5617 	int rval, reason;
5618 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5619 
5620 	mutex_enter(cport_mutex);
5621 
5622 	rval = sata_txlt_generic_pkt_info(spx, &reason, 1);
5623 	if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5624 		mutex_exit(cport_mutex);
5625 		return (rval);
5626 	}
5627 
5628 	/* T_DIR bit */
5629 	if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_T_DIR)
5630 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
5631 	else
5632 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
5633 
5634 	/* MULTIPLE_COUNT field.  If non-zero, invalid command (for now). */
5635 	if (((scsipkt->pkt_cdbp[1] >> 5) & 0x7) != 0) {
5636 		mutex_exit(cport_mutex);
5637 		return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5638 	}
5639 
5640 	/* OFFLINE field. If non-zero, invalid command (for now). */
5641 	if (((scsipkt->pkt_cdbp[2] >> 6) & 0x3) != 0) {
5642 		mutex_exit(cport_mutex);
5643 		return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5644 	}
5645 
5646 	/* PROTOCOL field */
5647 	switch ((scsipkt->pkt_cdbp[1] >> 1) & 0xf) {
5648 	case SATL_APT_P_HW_RESET:
5649 	case SATL_APT_P_SRST:
5650 	case SATL_APT_P_DMA:
5651 	case SATL_APT_P_DMA_QUEUED:
5652 	case SATL_APT_P_DEV_DIAG:
5653 	case SATL_APT_P_DEV_RESET:
5654 	case SATL_APT_P_UDMA_IN:
5655 	case SATL_APT_P_UDMA_OUT:
5656 	case SATL_APT_P_FPDMA:
5657 	case SATL_APT_P_RET_RESP:
5658 		/* Not yet implemented */
5659 	default:
5660 		mutex_exit(cport_mutex);
5661 		return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5662 
5663 	case SATL_APT_P_NON_DATA:
5664 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
5665 		break;
5666 
5667 	case SATL_APT_P_PIO_DATA_IN:
5668 		/* If PROTOCOL disagrees with T_DIR, invalid command */
5669 		if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_WRITE) {
5670 			mutex_exit(cport_mutex);
5671 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5672 		}
5673 
5674 		/* if there is a buffer, release its DMA resources */
5675 		if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) {
5676 			sata_scsi_dmafree(NULL, scsipkt);
5677 		} else {
5678 			/* if there is no buffer, how do you PIO in? */
5679 			mutex_exit(cport_mutex);
5680 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5681 		}
5682 
5683 		break;
5684 
5685 	case SATL_APT_P_PIO_DATA_OUT:
5686 		/* If PROTOCOL disagrees with T_DIR, invalid command */
5687 		if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_READ) {
5688 			mutex_exit(cport_mutex);
5689 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5690 		}
5691 
5692 		/* if there is a buffer, release its DMA resources */
5693 		if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) {
5694 			sata_scsi_dmafree(NULL, scsipkt);
5695 		} else {
5696 			/* if there is no buffer, how do you PIO out? */
5697 			mutex_exit(cport_mutex);
5698 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5699 		}
5700 
5701 		break;
5702 	}
5703 
5704 	/* Parse the ATA cmd fields, transfer some straight to the satacmd */
5705 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
5706 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12:
5707 		feature = scsipkt->pkt_cdbp[3];
5708 
5709 		sec_count = scsipkt->pkt_cdbp[4];
5710 
5711 		lba = scsipkt->pkt_cdbp[8] & 0xf;
5712 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5713 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
5714 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5715 
5716 		scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] & 0xf0;
5717 		scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[9];
5718 
5719 		break;
5720 
5721 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16:
5722 		if (scsipkt->pkt_cdbp[1] & SATL_APT_BM_EXTEND) {
5723 			extend = 1;
5724 
5725 			feature = scsipkt->pkt_cdbp[3];
5726 			feature = (feature << 8) | scsipkt->pkt_cdbp[4];
5727 
5728 			sec_count = scsipkt->pkt_cdbp[5];
5729 			sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[6];
5730 
5731 			lba = scsipkt->pkt_cdbp[11];
5732 			lba = (lba << 8) | scsipkt->pkt_cdbp[12];
5733 			lba = (lba << 8) | scsipkt->pkt_cdbp[9];
5734 			lba = (lba << 8) | scsipkt->pkt_cdbp[10];
5735 			lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5736 			lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5737 
5738 			scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13];
5739 			scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14];
5740 		} else {
5741 			feature = scsipkt->pkt_cdbp[3];
5742 
5743 			sec_count = scsipkt->pkt_cdbp[5];
5744 
5745 			lba = scsipkt->pkt_cdbp[13] & 0xf;
5746 			lba = (lba << 8) | scsipkt->pkt_cdbp[12];
5747 			lba = (lba << 8) | scsipkt->pkt_cdbp[10];
5748 			lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5749 
5750 			scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] &
5751 			    0xf0;
5752 			scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14];
5753 		}
5754 
5755 		break;
5756 	}
5757 
5758 	/* CK_COND bit */
5759 	if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) {
5760 		if (extend) {
5761 			scmd->satacmd_flags.sata_copy_out_sec_count_msb = 1;
5762 			scmd->satacmd_flags.sata_copy_out_lba_low_msb = 1;
5763 			scmd->satacmd_flags.sata_copy_out_lba_mid_msb = 1;
5764 			scmd->satacmd_flags.sata_copy_out_lba_high_msb = 1;
5765 		}
5766 
5767 		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1;
5768 		scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1;
5769 		scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1;
5770 		scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1;
5771 		scmd->satacmd_flags.sata_copy_out_device_reg = 1;
5772 		scmd->satacmd_flags.sata_copy_out_error_reg = 1;
5773 	}
5774 
5775 	/* Transfer remaining parsed ATA cmd values to the satacmd */
5776 	if (extend) {
5777 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
5778 
5779 		scmd->satacmd_features_reg_ext = (feature >> 8) & 0xff;
5780 		scmd->satacmd_sec_count_msb = (sec_count >> 8) & 0xff;
5781 		scmd->satacmd_lba_low_msb = (lba >> 8) & 0xff;
5782 		scmd->satacmd_lba_mid_msb = (lba >> 8) & 0xff;
5783 		scmd->satacmd_lba_high_msb = lba >> 40;
5784 	} else {
5785 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
5786 
5787 		scmd->satacmd_features_reg_ext = 0;
5788 		scmd->satacmd_sec_count_msb = 0;
5789 		scmd->satacmd_lba_low_msb = 0;
5790 		scmd->satacmd_lba_mid_msb = 0;
5791 		scmd->satacmd_lba_high_msb = 0;
5792 	}
5793 
5794 	scmd->satacmd_features_reg = feature & 0xff;
5795 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
5796 	scmd->satacmd_lba_low_lsb = lba & 0xff;
5797 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
5798 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
5799 
5800 	/* Determine transfer length */
5801 	switch (scsipkt->pkt_cdbp[2] & 0x3) {		/* T_LENGTH field */
5802 	case 1:
5803 		t_len = feature;
5804 		break;
5805 	case 2:
5806 		t_len = sec_count;
5807 		break;
5808 	default:
5809 		t_len = 0;
5810 		break;
5811 	}
5812 
5813 	/* Adjust transfer length for the Byte Block bit */
5814 	if ((scsipkt->pkt_cdbp[2] >> 2) & 1)
5815 		t_len *= SATA_DISK_SECTOR_SIZE;
5816 
5817 	/* Start processing command */
5818 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5819 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_apt_completion;
5820 		synch = FALSE;
5821 	} else {
5822 		synch = TRUE;
5823 	}
5824 
5825 	if (sata_hba_start(spx, &rval) != 0) {
5826 		mutex_exit(cport_mutex);
5827 		return (rval);
5828 	}
5829 
5830 	mutex_exit(cport_mutex);
5831 
5832 	if (synch) {
5833 		sata_txlt_apt_completion(spx->txlt_sata_pkt);
5834 	}
5835 
5836 	return (TRAN_ACCEPT);
5837 }
5838 
5839 /*
5840  * Translate command: Log Sense
5841  */
5842 static int
5843 sata_txlt_log_sense(sata_pkt_txlate_t *spx)
5844 {
5845 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
5846 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5847 	sata_drive_info_t *sdinfo;
5848 	struct scsi_extended_sense *sense;
5849 	int		len, count, alc_len;
5850 	int		pc;	/* Page Control code */
5851 	int		page_code;	/* Page code */
5852 	uint8_t		*buf;	/* log sense buffer */
5853 	int		rval, reason;
5854 #define	MAX_LOG_SENSE_PAGE_SIZE	512
5855 	kmutex_t	*cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5856 
5857 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5858 	    "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
5859 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5860 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5861 
5862 	if (servicing_interrupt()) {
5863 		buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_NOSLEEP);
5864 		if (buf == NULL) {
5865 			return (TRAN_BUSY);
5866 		}
5867 	} else {
5868 		buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
5869 	}
5870 
5871 	mutex_enter(cport_mutex);
5872 
5873 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
5874 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5875 		mutex_exit(cport_mutex);
5876 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
5877 		return (rval);
5878 	}
5879 
5880 	scsipkt->pkt_reason = CMD_CMPLT;
5881 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5882 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5883 
5884 	pc = scsipkt->pkt_cdbp[2] >> 6;
5885 	page_code = scsipkt->pkt_cdbp[2] & 0x3f;
5886 
5887 	/* Reject not supported request for all but cumulative values */
5888 	switch (pc) {
5889 	case PC_CUMULATIVE_VALUES:
5890 		break;
5891 	default:
5892 		*scsipkt->pkt_scbp = STATUS_CHECK;
5893 		sense = sata_arq_sense(spx);
5894 		sense->es_key = KEY_ILLEGAL_REQUEST;
5895 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5896 		goto done;
5897 	}
5898 
5899 	switch (page_code) {
5900 	case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5901 	case PAGE_CODE_SELF_TEST_RESULTS:
5902 	case PAGE_CODE_INFORMATION_EXCEPTIONS:
5903 	case PAGE_CODE_SMART_READ_DATA:
5904 	case PAGE_CODE_START_STOP_CYCLE_COUNTER:
5905 		break;
5906 	default:
5907 		*scsipkt->pkt_scbp = STATUS_CHECK;
5908 		sense = sata_arq_sense(spx);
5909 		sense->es_key = KEY_ILLEGAL_REQUEST;
5910 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5911 		goto done;
5912 	}
5913 
5914 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
5915 		/*
5916 		 * Because log sense uses local buffers for data retrieval from
5917 		 * the devices and sets the data programatically in the
5918 		 * original specified buffer, release preallocated DMA
5919 		 * resources before storing data in the original buffer,
5920 		 * so no unwanted DMA sync would take place.
5921 		 */
5922 		sata_id_t *sata_id;
5923 
5924 		sata_scsi_dmafree(NULL, scsipkt);
5925 
5926 		len = 0;
5927 
5928 		/* Build log parameter header */
5929 		buf[len++] = page_code;	/* page code as in the CDB */
5930 		buf[len++] = 0;		/* reserved */
5931 		buf[len++] = 0;		/* Zero out page length for now (MSB) */
5932 		buf[len++] = 0;		/* (LSB) */
5933 
5934 		sdinfo = sata_get_device_info(
5935 		    spx->txlt_sata_hba_inst,
5936 		    &spx->txlt_sata_pkt->satapkt_device);
5937 
5938 		/*
5939 		 * Add requested pages.
5940 		 */
5941 		switch (page_code) {
5942 		case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5943 			len = sata_build_lsense_page_0(sdinfo, buf + len);
5944 			break;
5945 		case PAGE_CODE_SELF_TEST_RESULTS:
5946 			sata_id = &sdinfo->satadrv_id;
5947 			if ((! (sata_id->ai_cmdset84 &
5948 			    SATA_SMART_SELF_TEST_SUPPORTED)) ||
5949 			    (! (sata_id->ai_features87 &
5950 			    SATA_SMART_SELF_TEST_SUPPORTED))) {
5951 				*scsipkt->pkt_scbp = STATUS_CHECK;
5952 				sense = sata_arq_sense(spx);
5953 				sense->es_key = KEY_ILLEGAL_REQUEST;
5954 				sense->es_add_code =
5955 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5956 
5957 				goto done;
5958 			}
5959 			len = sata_build_lsense_page_10(sdinfo, buf + len,
5960 			    spx->txlt_sata_hba_inst);
5961 			break;
5962 		case PAGE_CODE_INFORMATION_EXCEPTIONS:
5963 			sata_id = &sdinfo->satadrv_id;
5964 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5965 				*scsipkt->pkt_scbp = STATUS_CHECK;
5966 				sense = sata_arq_sense(spx);
5967 				sense->es_key = KEY_ILLEGAL_REQUEST;
5968 				sense->es_add_code =
5969 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5970 
5971 				goto done;
5972 			}
5973 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5974 				*scsipkt->pkt_scbp = STATUS_CHECK;
5975 				sense = sata_arq_sense(spx);
5976 				sense->es_key = KEY_ABORTED_COMMAND;
5977 				sense->es_add_code =
5978 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5979 				sense->es_qual_code =
5980 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5981 
5982 				goto done;
5983 			}
5984 
5985 			len = sata_build_lsense_page_2f(sdinfo, buf + len,
5986 			    spx->txlt_sata_hba_inst);
5987 			break;
5988 		case PAGE_CODE_SMART_READ_DATA:
5989 			sata_id = &sdinfo->satadrv_id;
5990 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5991 				*scsipkt->pkt_scbp = STATUS_CHECK;
5992 				sense = sata_arq_sense(spx);
5993 				sense->es_key = KEY_ILLEGAL_REQUEST;
5994 				sense->es_add_code =
5995 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5996 
5997 				goto done;
5998 			}
5999 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
6000 				*scsipkt->pkt_scbp = STATUS_CHECK;
6001 				sense = sata_arq_sense(spx);
6002 				sense->es_key = KEY_ABORTED_COMMAND;
6003 				sense->es_add_code =
6004 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
6005 				sense->es_qual_code =
6006 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
6007 
6008 				goto done;
6009 			}
6010 
6011 			/* This page doesn't include a page header */
6012 			len = sata_build_lsense_page_30(sdinfo, buf,
6013 			    spx->txlt_sata_hba_inst);
6014 			goto no_header;
6015 		case PAGE_CODE_START_STOP_CYCLE_COUNTER:
6016 			sata_id = &sdinfo->satadrv_id;
6017 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
6018 				*scsipkt->pkt_scbp = STATUS_CHECK;
6019 				sense = sata_arq_sense(spx);
6020 				sense->es_key = KEY_ILLEGAL_REQUEST;
6021 				sense->es_add_code =
6022 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6023 
6024 				goto done;
6025 			}
6026 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
6027 				*scsipkt->pkt_scbp = STATUS_CHECK;
6028 				sense = sata_arq_sense(spx);
6029 				sense->es_key = KEY_ABORTED_COMMAND;
6030 				sense->es_add_code =
6031 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
6032 				sense->es_qual_code =
6033 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
6034 
6035 				goto done;
6036 			}
6037 			len = sata_build_lsense_page_0e(sdinfo, buf, spx);
6038 			goto no_header;
6039 		default:
6040 			/* Invalid request */
6041 			*scsipkt->pkt_scbp = STATUS_CHECK;
6042 			sense = sata_arq_sense(spx);
6043 			sense->es_key = KEY_ILLEGAL_REQUEST;
6044 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6045 			goto done;
6046 		}
6047 
6048 		/* set parameter log sense data length */
6049 		buf[2] = len >> 8;	/* log sense length (MSB) */
6050 		buf[3] = len & 0xff;	/* log sense length (LSB) */
6051 
6052 		len += SCSI_LOG_PAGE_HDR_LEN;
6053 		ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
6054 
6055 no_header:
6056 		/* Check allocation length */
6057 		alc_len = scsipkt->pkt_cdbp[7];
6058 		alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
6059 
6060 		/*
6061 		 * We do not check for possible parameters truncation
6062 		 * (alc_len < len) assuming that the target driver works
6063 		 * correctly. Just avoiding overrun.
6064 		 * Copy no more than requested and possible, buffer-wise.
6065 		 */
6066 		count = MIN(alc_len, len);
6067 		count = MIN(bp->b_bcount, count);
6068 		bcopy(buf, bp->b_un.b_addr, count);
6069 
6070 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
6071 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
6072 	}
6073 	*scsipkt->pkt_scbp = STATUS_GOOD;
6074 done:
6075 	mutex_exit(cport_mutex);
6076 	(void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
6077 
6078 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6079 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6080 
6081 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6082 	    scsipkt->pkt_comp != NULL) {
6083 		/* scsi callback required */
6084 		if (servicing_interrupt()) {
6085 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6086 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6087 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
6088 			    TASKQID_INVALID) {
6089 				return (TRAN_BUSY);
6090 			}
6091 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6092 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6093 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
6094 			/* Scheduling the callback failed */
6095 			return (TRAN_BUSY);
6096 		}
6097 	}
6098 
6099 	return (TRAN_ACCEPT);
6100 }
6101 
6102 /*
6103  * Translate command: Log Select
6104  * Not implemented at this time - returns invalid command response.
6105  */
6106 static	int
6107 sata_txlt_log_select(sata_pkt_txlate_t *spx)
6108 {
6109 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6110 	    "sata_txlt_log_select\n", NULL);
6111 
6112 	return (sata_txlt_invalid_command(spx));
6113 }
6114 
6115 
6116 /*
6117  * Translate command: Read (various types).
6118  * Translated into appropriate type of ATA READ command
6119  * for SATA hard disks.
6120  * Both the device capabilities and requested operation mode are
6121  * considered.
6122  *
6123  * Following scsi cdb fields are ignored:
6124  * rdprotect, dpo, fua, fua_nv, group_number.
6125  *
6126  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
6127  * enable variable sata_func_enable), the capability of the controller and
6128  * capability of a device are checked and if both support queueing, read
6129  * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
6130  * command rather than plain READ_XXX command.
6131  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
6132  * both the controller and device suport such functionality, the read
6133  * request will be translated to READ_FPDMA_QUEUED command.
6134  * In both cases the maximum queue depth is derived as minimum of:
6135  * HBA capability,device capability and sata_max_queue_depth variable setting.
6136  * The value passed to HBA driver is decremented by 1, because only 5 bits are
6137  * used to pass max queue depth value, and the maximum possible queue depth
6138  * is 32.
6139  *
6140  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6141  * appropriate values in scsi_pkt fields.
6142  */
6143 static int
6144 sata_txlt_read(sata_pkt_txlate_t *spx)
6145 {
6146 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6147 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6148 	sata_drive_info_t *sdinfo;
6149 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
6150 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6151 	uint16_t sec_count;
6152 	uint64_t lba;
6153 	int rval, reason;
6154 	int synch;
6155 
6156 	mutex_enter(cport_mutex);
6157 
6158 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
6159 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6160 		mutex_exit(cport_mutex);
6161 		return (rval);
6162 	}
6163 
6164 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6165 	    &spx->txlt_sata_pkt->satapkt_device);
6166 
6167 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
6168 	/*
6169 	 * Extract LBA and sector count from scsi CDB.
6170 	 */
6171 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6172 	case SCMD_READ:
6173 		/* 6-byte scsi read cmd : 0x08 */
6174 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
6175 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
6176 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6177 		sec_count = scsipkt->pkt_cdbp[4];
6178 		/* sec_count 0 will be interpreted as 256 by a device */
6179 		break;
6180 	case SCMD_READ_G1:
6181 		/* 10-bytes scsi read command : 0x28 */
6182 		lba = scsipkt->pkt_cdbp[2];
6183 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6184 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6185 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6186 		sec_count = scsipkt->pkt_cdbp[7];
6187 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6188 		break;
6189 	case SCMD_READ_G5:
6190 		/* 12-bytes scsi read command : 0xA8 */
6191 		lba = scsipkt->pkt_cdbp[2];
6192 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6193 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6194 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6195 		sec_count = scsipkt->pkt_cdbp[6];
6196 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
6197 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6198 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
6199 		break;
6200 	case SCMD_READ_G4:
6201 		/* 16-bytes scsi read command : 0x88 */
6202 		lba = scsipkt->pkt_cdbp[2];
6203 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6204 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6205 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6206 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
6207 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
6208 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
6209 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
6210 		sec_count = scsipkt->pkt_cdbp[10];
6211 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
6212 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
6213 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
6214 		break;
6215 	default:
6216 		/* Unsupported command */
6217 		mutex_exit(cport_mutex);
6218 		return (sata_txlt_invalid_command(spx));
6219 	}
6220 
6221 	/*
6222 	 * Check if specified address exceeds device capacity
6223 	 */
6224 	if ((lba >= sdinfo->satadrv_capacity) ||
6225 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
6226 		/* LBA out of range */
6227 		mutex_exit(cport_mutex);
6228 		return (sata_txlt_lba_out_of_range(spx));
6229 	}
6230 
6231 	/*
6232 	 * For zero-length transfer, emulate good completion of the command
6233 	 * (reasons for rejecting the command were already checked).
6234 	 * No DMA resources were allocated.
6235 	 */
6236 	if (spx->txlt_dma_cookie_list == NULL) {
6237 		mutex_exit(cport_mutex);
6238 		return (sata_emul_rw_completion(spx));
6239 	}
6240 
6241 	/*
6242 	 * Build cmd block depending on the device capability and
6243 	 * requested operation mode.
6244 	 * Do not bother with non-dma mode - we are working only with
6245 	 * devices supporting DMA.
6246 	 */
6247 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
6248 	scmd->satacmd_device_reg = SATA_ADH_LBA;
6249 	scmd->satacmd_cmd_reg = SATAC_READ_DMA;
6250 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6251 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
6252 		scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT;
6253 		scmd->satacmd_sec_count_msb = sec_count >> 8;
6254 #ifndef __lock_lint
6255 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
6256 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
6257 		scmd->satacmd_lba_high_msb = lba >> 40;
6258 #endif
6259 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
6260 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
6261 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
6262 	}
6263 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
6264 	scmd->satacmd_lba_low_lsb = lba & 0xff;
6265 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
6266 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
6267 	scmd->satacmd_features_reg = 0;
6268 	scmd->satacmd_status_reg = 0;
6269 	scmd->satacmd_error_reg = 0;
6270 
6271 	/*
6272 	 * Check if queueing commands should be used and switch
6273 	 * to appropriate command if possible
6274 	 */
6275 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
6276 		boolean_t using_queuing;
6277 
6278 		/* Queuing supported by controller and device? */
6279 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
6280 		    (sdinfo->satadrv_features_support &
6281 		    SATA_DEV_F_NCQ) &&
6282 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6283 		    SATA_CTLF_NCQ)) {
6284 			using_queuing = B_TRUE;
6285 
6286 			/* NCQ supported - use FPDMA READ */
6287 			scmd->satacmd_cmd_reg =
6288 			    SATAC_READ_FPDMA_QUEUED;
6289 			scmd->satacmd_features_reg_ext =
6290 			    scmd->satacmd_sec_count_msb;
6291 			scmd->satacmd_sec_count_msb = 0;
6292 		} else if ((sdinfo->satadrv_features_support &
6293 		    SATA_DEV_F_TCQ) &&
6294 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6295 		    SATA_CTLF_QCMD)) {
6296 			using_queuing = B_TRUE;
6297 
6298 			/* Legacy queueing */
6299 			if (sdinfo->satadrv_features_support &
6300 			    SATA_DEV_F_LBA48) {
6301 				scmd->satacmd_cmd_reg =
6302 				    SATAC_READ_DMA_QUEUED_EXT;
6303 				scmd->satacmd_features_reg_ext =
6304 				    scmd->satacmd_sec_count_msb;
6305 				scmd->satacmd_sec_count_msb = 0;
6306 			} else {
6307 				scmd->satacmd_cmd_reg =
6308 				    SATAC_READ_DMA_QUEUED;
6309 			}
6310 		} else	/* NCQ nor legacy queuing not supported */
6311 			using_queuing = B_FALSE;
6312 
6313 		/*
6314 		 * If queuing, the sector count goes in the features register
6315 		 * and the secount count will contain the tag.
6316 		 */
6317 		if (using_queuing) {
6318 			scmd->satacmd_features_reg =
6319 			    scmd->satacmd_sec_count_lsb;
6320 			scmd->satacmd_sec_count_lsb = 0;
6321 			scmd->satacmd_flags.sata_queued = B_TRUE;
6322 
6323 			/* Set-up maximum queue depth */
6324 			scmd->satacmd_flags.sata_max_queue_depth =
6325 			    sdinfo->satadrv_max_queue_depth - 1;
6326 		} else if (sdinfo->satadrv_features_enabled &
6327 		    SATA_DEV_F_E_UNTAGGED_QING) {
6328 			/*
6329 			 * Although NCQ/TCQ is not enabled, untagged queuing
6330 			 * may be still used.
6331 			 * Set-up the maximum untagged queue depth.
6332 			 * Use controller's queue depth from sata_hba_tran.
6333 			 * SATA HBA drivers may ignore this value and rely on
6334 			 * the internal limits.For drivers that do not
6335 			 * ignore untaged queue depth, limit the value to
6336 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
6337 			 * largest value that can be passed via
6338 			 * satacmd_flags.sata_max_queue_depth.
6339 			 */
6340 			scmd->satacmd_flags.sata_max_queue_depth =
6341 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
6342 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
6343 
6344 		} else {
6345 			scmd->satacmd_flags.sata_max_queue_depth = 0;
6346 		}
6347 	} else
6348 		scmd->satacmd_flags.sata_max_queue_depth = 0;
6349 
6350 	SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst,
6351 	    "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n",
6352 	    scmd->satacmd_cmd_reg, lba, sec_count);
6353 
6354 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6355 		/* Need callback function */
6356 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
6357 		synch = FALSE;
6358 	} else
6359 		synch = TRUE;
6360 
6361 	/* Transfer command to HBA */
6362 	if (sata_hba_start(spx, &rval) != 0) {
6363 		/* Pkt not accepted for execution */
6364 		mutex_exit(cport_mutex);
6365 		return (rval);
6366 	}
6367 	mutex_exit(cport_mutex);
6368 	/*
6369 	 * If execution is non-synchronous,
6370 	 * a callback function will handle potential errors, translate
6371 	 * the response and will do a callback to a target driver.
6372 	 * If it was synchronous, check execution status using the same
6373 	 * framework callback.
6374 	 */
6375 	if (synch) {
6376 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6377 		    "synchronous execution status %x\n",
6378 		    spx->txlt_sata_pkt->satapkt_reason);
6379 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
6380 	}
6381 	return (TRAN_ACCEPT);
6382 }
6383 
6384 
6385 /*
6386  * SATA translate command: Write (various types)
6387  * Translated into appropriate type of ATA WRITE command
6388  * for SATA hard disks.
6389  * Both the device capabilities and requested operation mode are
6390  * considered.
6391  *
6392  * Following scsi cdb fields are ignored:
6393  * rwprotect, dpo, fua, fua_nv, group_number.
6394  *
6395  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
6396  * enable variable sata_func_enable), the capability of the controller and
6397  * capability of a device are checked and if both support queueing, write
6398  * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT
6399  * command rather than plain WRITE_XXX command.
6400  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
6401  * both the controller and device suport such functionality, the write
6402  * request will be translated to WRITE_FPDMA_QUEUED command.
6403  * In both cases the maximum queue depth is derived as minimum of:
6404  * HBA capability,device capability and sata_max_queue_depth variable setting.
6405  * The value passed to HBA driver is decremented by 1, because only 5 bits are
6406  * used to pass max queue depth value, and the maximum possible queue depth
6407  * is 32.
6408  *
6409  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6410  * appropriate values in scsi_pkt fields.
6411  */
6412 static int
6413 sata_txlt_write(sata_pkt_txlate_t *spx)
6414 {
6415 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6416 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6417 	sata_drive_info_t *sdinfo;
6418 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
6419 	uint16_t sec_count;
6420 	uint64_t lba;
6421 	int rval, reason;
6422 	int synch;
6423 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6424 
6425 	mutex_enter(cport_mutex);
6426 
6427 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
6428 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6429 		mutex_exit(cport_mutex);
6430 		return (rval);
6431 	}
6432 
6433 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6434 	    &spx->txlt_sata_pkt->satapkt_device);
6435 
6436 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
6437 	/*
6438 	 * Extract LBA and sector count from scsi CDB
6439 	 */
6440 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6441 	case SCMD_WRITE:
6442 		/* 6-byte scsi read cmd : 0x0A */
6443 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
6444 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
6445 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6446 		sec_count = scsipkt->pkt_cdbp[4];
6447 		/* sec_count 0 will be interpreted as 256 by a device */
6448 		break;
6449 	case SCMD_WRITE_G1:
6450 		/* 10-bytes scsi write command : 0x2A */
6451 		lba = scsipkt->pkt_cdbp[2];
6452 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6453 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6454 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6455 		sec_count = scsipkt->pkt_cdbp[7];
6456 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6457 		break;
6458 	case SCMD_WRITE_G5:
6459 		/* 12-bytes scsi read command : 0xAA */
6460 		lba = scsipkt->pkt_cdbp[2];
6461 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6462 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6463 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6464 		sec_count = scsipkt->pkt_cdbp[6];
6465 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
6466 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6467 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
6468 		break;
6469 	case SCMD_WRITE_G4:
6470 		/* 16-bytes scsi write command : 0x8A */
6471 		lba = scsipkt->pkt_cdbp[2];
6472 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6473 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6474 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6475 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
6476 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
6477 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
6478 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
6479 		sec_count = scsipkt->pkt_cdbp[10];
6480 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
6481 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
6482 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
6483 		break;
6484 	default:
6485 		/* Unsupported command */
6486 		mutex_exit(cport_mutex);
6487 		return (sata_txlt_invalid_command(spx));
6488 	}
6489 
6490 	/*
6491 	 * Check if specified address and length exceeds device capacity
6492 	 */
6493 	if ((lba >= sdinfo->satadrv_capacity) ||
6494 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
6495 		/* LBA out of range */
6496 		mutex_exit(cport_mutex);
6497 		return (sata_txlt_lba_out_of_range(spx));
6498 	}
6499 
6500 	/*
6501 	 * For zero-length transfer, emulate good completion of the command
6502 	 * (reasons for rejecting the command were already checked).
6503 	 * No DMA resources were allocated.
6504 	 */
6505 	if (spx->txlt_dma_cookie_list == NULL) {
6506 		mutex_exit(cport_mutex);
6507 		return (sata_emul_rw_completion(spx));
6508 	}
6509 
6510 	/*
6511 	 * Build cmd block depending on the device capability and
6512 	 * requested operation mode.
6513 	 * Do not bother with non-dma mode- we are working only with
6514 	 * devices supporting DMA.
6515 	 */
6516 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
6517 	scmd->satacmd_device_reg = SATA_ADH_LBA;
6518 	scmd->satacmd_cmd_reg = SATAC_WRITE_DMA;
6519 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6520 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
6521 		scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT;
6522 		scmd->satacmd_sec_count_msb = sec_count >> 8;
6523 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
6524 #ifndef __lock_lint
6525 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
6526 		scmd->satacmd_lba_high_msb = lba >> 40;
6527 #endif
6528 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
6529 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
6530 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
6531 	}
6532 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
6533 	scmd->satacmd_lba_low_lsb = lba & 0xff;
6534 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
6535 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
6536 	scmd->satacmd_features_reg = 0;
6537 	scmd->satacmd_status_reg = 0;
6538 	scmd->satacmd_error_reg = 0;
6539 
6540 	/*
6541 	 * Check if queueing commands should be used and switch
6542 	 * to appropriate command if possible
6543 	 */
6544 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
6545 		boolean_t using_queuing;
6546 
6547 		/* Queuing supported by controller and device? */
6548 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
6549 		    (sdinfo->satadrv_features_support &
6550 		    SATA_DEV_F_NCQ) &&
6551 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6552 		    SATA_CTLF_NCQ)) {
6553 			using_queuing = B_TRUE;
6554 
6555 			/* NCQ supported - use FPDMA WRITE */
6556 			scmd->satacmd_cmd_reg =
6557 			    SATAC_WRITE_FPDMA_QUEUED;
6558 			scmd->satacmd_features_reg_ext =
6559 			    scmd->satacmd_sec_count_msb;
6560 			scmd->satacmd_sec_count_msb = 0;
6561 		} else if ((sdinfo->satadrv_features_support &
6562 		    SATA_DEV_F_TCQ) &&
6563 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6564 		    SATA_CTLF_QCMD)) {
6565 			using_queuing = B_TRUE;
6566 
6567 			/* Legacy queueing */
6568 			if (sdinfo->satadrv_features_support &
6569 			    SATA_DEV_F_LBA48) {
6570 				scmd->satacmd_cmd_reg =
6571 				    SATAC_WRITE_DMA_QUEUED_EXT;
6572 				scmd->satacmd_features_reg_ext =
6573 				    scmd->satacmd_sec_count_msb;
6574 				scmd->satacmd_sec_count_msb = 0;
6575 			} else {
6576 				scmd->satacmd_cmd_reg =
6577 				    SATAC_WRITE_DMA_QUEUED;
6578 			}
6579 		} else	/*  NCQ nor legacy queuing not supported */
6580 			using_queuing = B_FALSE;
6581 
6582 		if (using_queuing) {
6583 			scmd->satacmd_features_reg =
6584 			    scmd->satacmd_sec_count_lsb;
6585 			scmd->satacmd_sec_count_lsb = 0;
6586 			scmd->satacmd_flags.sata_queued = B_TRUE;
6587 			/* Set-up maximum queue depth */
6588 			scmd->satacmd_flags.sata_max_queue_depth =
6589 			    sdinfo->satadrv_max_queue_depth - 1;
6590 		} else if (sdinfo->satadrv_features_enabled &
6591 		    SATA_DEV_F_E_UNTAGGED_QING) {
6592 			/*
6593 			 * Although NCQ/TCQ is not enabled, untagged queuing
6594 			 * may be still used.
6595 			 * Set-up the maximum untagged queue depth.
6596 			 * Use controller's queue depth from sata_hba_tran.
6597 			 * SATA HBA drivers may ignore this value and rely on
6598 			 * the internal limits. For drivera that do not
6599 			 * ignore untaged queue depth, limit the value to
6600 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
6601 			 * largest value that can be passed via
6602 			 * satacmd_flags.sata_max_queue_depth.
6603 			 */
6604 			scmd->satacmd_flags.sata_max_queue_depth =
6605 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
6606 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
6607 
6608 		} else {
6609 			scmd->satacmd_flags.sata_max_queue_depth = 0;
6610 		}
6611 	} else
6612 		scmd->satacmd_flags.sata_max_queue_depth = 0;
6613 
6614 	SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6615 	    "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n",
6616 	    scmd->satacmd_cmd_reg, lba, sec_count);
6617 
6618 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6619 		/* Need callback function */
6620 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
6621 		synch = FALSE;
6622 	} else
6623 		synch = TRUE;
6624 
6625 	/* Transfer command to HBA */
6626 	if (sata_hba_start(spx, &rval) != 0) {
6627 		/* Pkt not accepted for execution */
6628 		mutex_exit(cport_mutex);
6629 		return (rval);
6630 	}
6631 	mutex_exit(cport_mutex);
6632 
6633 	/*
6634 	 * If execution is non-synchronous,
6635 	 * a callback function will handle potential errors, translate
6636 	 * the response and will do a callback to a target driver.
6637 	 * If it was synchronous, check execution status using the same
6638 	 * framework callback.
6639 	 */
6640 	if (synch) {
6641 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6642 		    "synchronous execution status %x\n",
6643 		    spx->txlt_sata_pkt->satapkt_reason);
6644 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
6645 	}
6646 	return (TRAN_ACCEPT);
6647 }
6648 
6649 
6650 /*
6651  * Implements SCSI SBC WRITE BUFFER command download microcode option
6652  */
6653 static int
6654 sata_txlt_write_buffer(sata_pkt_txlate_t *spx)
6655 {
6656 #define	WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE			4
6657 #define	WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE			5
6658 
6659 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6660 	struct sata_pkt *sata_pkt = spx->txlt_sata_pkt;
6661 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6662 
6663 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
6664 	struct scsi_extended_sense *sense;
6665 	int rval, mode, sector_count, reason;
6666 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6667 
6668 	mode = scsipkt->pkt_cdbp[1] & 0x1f;
6669 
6670 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6671 	    "sata_txlt_write_buffer, mode 0x%x\n", mode);
6672 
6673 	mutex_enter(cport_mutex);
6674 
6675 	if ((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
6676 	    TRAN_ACCEPT) {
6677 		mutex_exit(cport_mutex);
6678 		return (rval);
6679 	}
6680 
6681 	/* Use synchronous mode */
6682 	spx->txlt_sata_pkt->satapkt_op_mode
6683 	    |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
6684 
6685 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
6686 
6687 	scsipkt->pkt_reason = CMD_CMPLT;
6688 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6689 	    STATE_SENT_CMD | STATE_GOT_STATUS;
6690 
6691 	/*
6692 	 * The SCSI to ATA translation specification only calls
6693 	 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE.
6694 	 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but
6695 	 * ATA 8 (draft) got rid of download microcode for temp
6696 	 * and it is even optional for ATA 7, so it may be aborted.
6697 	 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as
6698 	 * it is not specified and the buffer offset for SCSI is a 16-bit
6699 	 * value in bytes, but for ATA it is a 16-bit offset in 512 byte
6700 	 * sectors.  Thus the offset really doesn't buy us anything.
6701 	 * If and when ATA 8 is stabilized and the SCSI to ATA specification
6702 	 * is revised, this can be revisisted.
6703 	 */
6704 	/* Reject not supported request */
6705 	switch (mode) {
6706 	case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE:
6707 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP;
6708 		break;
6709 	case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE:
6710 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE;
6711 		break;
6712 	default:
6713 		goto bad_param;
6714 	}
6715 
6716 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
6717 
6718 	scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE;
6719 	if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0)
6720 		goto bad_param;
6721 	sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE;
6722 	scmd->satacmd_sec_count_lsb = (uint8_t)sector_count;
6723 	scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8;
6724 	scmd->satacmd_lba_mid_lsb = 0;
6725 	scmd->satacmd_lba_high_lsb = 0;
6726 	scmd->satacmd_device_reg = 0;
6727 	spx->txlt_sata_pkt->satapkt_comp = NULL;
6728 	scmd->satacmd_addr_type = 0;
6729 
6730 	/* Transfer command to HBA */
6731 	if (sata_hba_start(spx, &rval) != 0) {
6732 		/* Pkt not accepted for execution */
6733 		mutex_exit(cport_mutex);
6734 		return (rval);
6735 	}
6736 
6737 	mutex_exit(cport_mutex);
6738 
6739 	/* Then we need synchronous check the status of the disk */
6740 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6741 	    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
6742 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
6743 		scsipkt->pkt_reason = CMD_CMPLT;
6744 
6745 		/* Download commmand succeed, so probe and identify device */
6746 		sata_reidentify_device(spx);
6747 	} else {
6748 		/* Something went wrong, microcode download command failed */
6749 		scsipkt->pkt_reason = CMD_INCOMPLETE;
6750 		*scsipkt->pkt_scbp = STATUS_CHECK;
6751 		sense = sata_arq_sense(spx);
6752 		switch (sata_pkt->satapkt_reason) {
6753 		case SATA_PKT_PORT_ERROR:
6754 			/*
6755 			 * We have no device data. Assume no data transfered.
6756 			 */
6757 			sense->es_key = KEY_HARDWARE_ERROR;
6758 			break;
6759 
6760 		case SATA_PKT_DEV_ERROR:
6761 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
6762 			    SATA_STATUS_ERR) {
6763 				/*
6764 				 * determine dev error reason from error
6765 				 * reg content
6766 				 */
6767 				sata_decode_device_error(spx, sense);
6768 				break;
6769 			}
6770 			/* No extended sense key - no info available */
6771 			break;
6772 
6773 		case SATA_PKT_TIMEOUT:
6774 			scsipkt->pkt_reason = CMD_TIMEOUT;
6775 			scsipkt->pkt_statistics |=
6776 			    STAT_TIMEOUT | STAT_DEV_RESET;
6777 			/* No extended sense key ? */
6778 			break;
6779 
6780 		case SATA_PKT_ABORTED:
6781 			scsipkt->pkt_reason = CMD_ABORTED;
6782 			scsipkt->pkt_statistics |= STAT_ABORTED;
6783 			/* No extended sense key ? */
6784 			break;
6785 
6786 		case SATA_PKT_RESET:
6787 			/* pkt aborted by an explicit reset from a host */
6788 			scsipkt->pkt_reason = CMD_RESET;
6789 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
6790 			break;
6791 
6792 		default:
6793 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
6794 			    "sata_txlt_nodata_cmd_completion: "
6795 			    "invalid packet completion reason %d",
6796 			    sata_pkt->satapkt_reason));
6797 			scsipkt->pkt_reason = CMD_TRAN_ERR;
6798 			break;
6799 		}
6800 
6801 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6802 		    "scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6803 
6804 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6805 			/* scsi callback required */
6806 			scsi_hba_pkt_comp(scsipkt);
6807 	}
6808 	return (TRAN_ACCEPT);
6809 
6810 bad_param:
6811 	mutex_exit(cport_mutex);
6812 	*scsipkt->pkt_scbp = STATUS_CHECK;
6813 	sense = sata_arq_sense(spx);
6814 	sense->es_key = KEY_ILLEGAL_REQUEST;
6815 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6816 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6817 	    scsipkt->pkt_comp != NULL) {
6818 		/* scsi callback required */
6819 		if (servicing_interrupt()) {
6820 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6821 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6822 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
6823 			    TASKQID_INVALID) {
6824 				return (TRAN_BUSY);
6825 			}
6826 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6827 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6828 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
6829 			/* Scheduling the callback failed */
6830 			return (TRAN_BUSY);
6831 		}
6832 	}
6833 	return (rval);
6834 }
6835 
6836 /*
6837  * Re-identify device after doing a firmware download.
6838  */
6839 static void
6840 sata_reidentify_device(sata_pkt_txlate_t *spx)
6841 {
6842 #define	DOWNLOAD_WAIT_TIME_SECS	60
6843 #define	DOWNLOAD_WAIT_INTERVAL_SECS	1
6844 	int rval;
6845 	int retry_cnt;
6846 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6847 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
6848 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
6849 	sata_drive_info_t *sdinfo;
6850 
6851 	/*
6852 	 * Before returning good status, probe device.
6853 	 * Device probing will get IDENTIFY DEVICE data, if possible.
6854 	 * The assumption is that the new microcode is applied by the
6855 	 * device. It is a caller responsibility to verify this.
6856 	 */
6857 	for (retry_cnt = 0;
6858 	    retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS;
6859 	    retry_cnt++) {
6860 		rval = sata_probe_device(sata_hba_inst, &sata_device);
6861 
6862 		if (rval == SATA_SUCCESS) { /* Set default features */
6863 			sdinfo = sata_get_device_info(sata_hba_inst,
6864 			    &sata_device);
6865 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
6866 			    SATA_SUCCESS) {
6867 				/* retry */
6868 				rval = sata_initialize_device(sata_hba_inst,
6869 				    sdinfo);
6870 				if (rval == SATA_RETRY)
6871 					sata_log(sata_hba_inst, CE_WARN,
6872 					    "SATA device at port %d pmport %d -"
6873 					    " default device features could not"
6874 					    " be set. Device may not operate "
6875 					    "as expected.",
6876 					    sata_device.satadev_addr.cport,
6877 					    sata_device.satadev_addr.pmport);
6878 			}
6879 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6880 				scsi_hba_pkt_comp(scsipkt);
6881 			return;
6882 		} else if (rval == SATA_RETRY) {
6883 			delay(drv_usectohz(1000000 *
6884 			    DOWNLOAD_WAIT_INTERVAL_SECS));
6885 			continue;
6886 		} else	/* failed - no reason to retry */
6887 			break;
6888 	}
6889 
6890 	/*
6891 	 * Something went wrong, device probing failed.
6892 	 */
6893 	SATA_LOG_D((sata_hba_inst, CE_WARN,
6894 	    "Cannot probe device after downloading microcode\n"));
6895 
6896 	/* Reset device to force retrying the probe. */
6897 	(void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst))
6898 	    (SATA_DIP(sata_hba_inst), &sata_device);
6899 
6900 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6901 		scsi_hba_pkt_comp(scsipkt);
6902 }
6903 
6904 
6905 /*
6906  * Translate command: Synchronize Cache.
6907  * Translates into Flush Cache command for SATA hard disks.
6908  *
6909  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6910  * appropriate values in scsi_pkt fields.
6911  */
6912 static int
6913 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx)
6914 {
6915 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6916 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6917 	int rval, reason;
6918 	int synch;
6919 
6920 	mutex_enter(cport_mutex);
6921 
6922 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
6923 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6924 		mutex_exit(cport_mutex);
6925 		return (rval);
6926 	}
6927 
6928 	scmd->satacmd_addr_type = 0;
6929 	scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE;
6930 	scmd->satacmd_device_reg = 0;
6931 	scmd->satacmd_sec_count_lsb = 0;
6932 	scmd->satacmd_lba_low_lsb = 0;
6933 	scmd->satacmd_lba_mid_lsb = 0;
6934 	scmd->satacmd_lba_high_lsb = 0;
6935 	scmd->satacmd_features_reg = 0;
6936 	scmd->satacmd_status_reg = 0;
6937 	scmd->satacmd_error_reg = 0;
6938 
6939 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6940 	    "sata_txlt_synchronize_cache\n", NULL);
6941 
6942 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6943 		/* Need to set-up a callback function */
6944 		spx->txlt_sata_pkt->satapkt_comp =
6945 		    sata_txlt_nodata_cmd_completion;
6946 		synch = FALSE;
6947 	} else
6948 		synch = TRUE;
6949 
6950 	/* Transfer command to HBA */
6951 	if (sata_hba_start(spx, &rval) != 0) {
6952 		/* Pkt not accepted for execution */
6953 		mutex_exit(cport_mutex);
6954 		return (rval);
6955 	}
6956 	mutex_exit(cport_mutex);
6957 
6958 	/*
6959 	 * If execution non-synchronous, it had to be completed
6960 	 * a callback function will handle potential errors, translate
6961 	 * the response and will do a callback to a target driver.
6962 	 * If it was synchronous, check status, using the same
6963 	 * framework callback.
6964 	 */
6965 	if (synch) {
6966 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6967 		    "synchronous execution status %x\n",
6968 		    spx->txlt_sata_pkt->satapkt_reason);
6969 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
6970 	}
6971 	return (TRAN_ACCEPT);
6972 }
6973 
6974 
6975 /*
6976  * Send pkt to SATA HBA driver
6977  *
6978  * This function may be called only if the operation is requested by scsi_pkt,
6979  * i.e. scsi_pkt is not NULL.
6980  *
6981  * This function has to be called with cport mutex held. It does release
6982  * the mutex when it calls HBA driver sata_tran_start function and
6983  * re-acquires it afterwards.
6984  *
6985  * If return value is 0, pkt was accepted, -1 otherwise
6986  * rval is set to appropriate sata_scsi_start return value.
6987  *
6988  * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not
6989  * have called the sata_pkt callback function for this packet.
6990  *
6991  * The scsi callback has to be performed by the caller of this routine.
6992  */
6993 static int
6994 sata_hba_start(sata_pkt_txlate_t *spx, int *rval)
6995 {
6996 	int stat;
6997 	uint8_t cport = SATA_TXLT_CPORT(spx);
6998 	uint8_t pmport = SATA_TXLT_PMPORT(spx);
6999 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
7000 	sata_drive_info_t *sdinfo;
7001 	sata_pmult_info_t *pminfo;
7002 	sata_pmport_info_t *pmportinfo = NULL;
7003 	sata_device_t *sata_device = NULL;
7004 	uint8_t cmd;
7005 	struct sata_cmd_flags cmd_flags;
7006 
7007 	ASSERT(spx->txlt_sata_pkt != NULL);
7008 
7009 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7010 
7011 	sdinfo = sata_get_device_info(sata_hba_inst,
7012 	    &spx->txlt_sata_pkt->satapkt_device);
7013 	ASSERT(sdinfo != NULL);
7014 
7015 	/* Clear device reset state? */
7016 	/* qual should be XXX_DPMPORT, but add XXX_PMPORT in case */
7017 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT ||
7018 	    sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT) {
7019 
7020 		/*
7021 		 * Get the pmult_info of the its parent port multiplier, all
7022 		 * sub-devices share a common device reset flags on in
7023 		 * pmult_info.
7024 		 */
7025 		pminfo = SATA_PMULT_INFO(sata_hba_inst, cport);
7026 		pmportinfo = pminfo->pmult_dev_port[pmport];
7027 		ASSERT(pminfo != NULL);
7028 		if (pminfo->pmult_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) {
7029 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
7030 			    sata_clear_dev_reset = B_TRUE;
7031 			pminfo->pmult_event_flags &=
7032 			    ~SATA_EVNT_CLEAR_DEVICE_RESET;
7033 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7034 			    "sata_hba_start: clearing device reset state"
7035 			    "on pmult.\n", NULL);
7036 		}
7037 	} else {
7038 		if (sdinfo->satadrv_event_flags &
7039 		    SATA_EVNT_CLEAR_DEVICE_RESET) {
7040 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
7041 			    sata_clear_dev_reset = B_TRUE;
7042 			sdinfo->satadrv_event_flags &=
7043 			    ~SATA_EVNT_CLEAR_DEVICE_RESET;
7044 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7045 			    "sata_hba_start: clearing device reset state\n",
7046 			    NULL);
7047 		}
7048 	}
7049 
7050 	cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg;
7051 	cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags;
7052 	sata_device = &spx->txlt_sata_pkt->satapkt_device;
7053 
7054 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7055 
7056 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7057 	    "Sata cmd 0x%2x\n", cmd);
7058 
7059 	stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
7060 	    spx->txlt_sata_pkt);
7061 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7062 	/*
7063 	 * If sata pkt was accepted and executed in asynchronous mode, i.e.
7064 	 * with the sata callback, the sata_pkt could be already destroyed
7065 	 * by the time we check ther return status from the hba_start()
7066 	 * function, because sata_scsi_destroy_pkt() could have been already
7067 	 * called (perhaps in the interrupt context). So, in such case, there
7068 	 * should be no references to it. In other cases, sata_pkt still
7069 	 * exists.
7070 	 */
7071 	if (stat == SATA_TRAN_ACCEPTED) {
7072 		/*
7073 		 * pkt accepted for execution.
7074 		 * If it was executed synchronously, it is already completed
7075 		 * and pkt completion_reason indicates completion status.
7076 		 */
7077 		*rval = TRAN_ACCEPT;
7078 		return (0);
7079 	}
7080 
7081 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
7082 	switch (stat) {
7083 	case SATA_TRAN_QUEUE_FULL:
7084 		/*
7085 		 * Controller detected queue full condition.
7086 		 */
7087 		SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst,
7088 		    "sata_hba_start: queue full\n", NULL);
7089 
7090 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
7091 		*spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL;
7092 
7093 		*rval = TRAN_BUSY;
7094 		break;
7095 
7096 	case SATA_TRAN_PORT_ERROR:
7097 		/*
7098 		 * Communication/link with device or general port error
7099 		 * detected before pkt execution begun.
7100 		 */
7101 		if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
7102 		    SATA_ADDR_CPORT ||
7103 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
7104 		    SATA_ADDR_DCPORT)
7105 			sata_log(sata_hba_inst, CE_CONT,
7106 			    "SATA port %d error",
7107 			    sata_device->satadev_addr.cport);
7108 		else
7109 			sata_log(sata_hba_inst, CE_CONT,
7110 			    "SATA port %d:%d error\n",
7111 			    sata_device->satadev_addr.cport,
7112 			    sata_device->satadev_addr.pmport);
7113 
7114 		/*
7115 		 * Update the port/device structure.
7116 		 * sata_pkt should be still valid. Since port error is
7117 		 * returned, sata_device content should reflect port
7118 		 * state - it means, that sata address have been changed,
7119 		 * because original packet's sata address refered to a device
7120 		 * attached to some port.
7121 		 */
7122 		if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT ||
7123 		    sata_device->satadev_addr.qual == SATA_ADDR_PMPORT) {
7124 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7125 			mutex_enter(&pmportinfo->pmport_mutex);
7126 			sata_update_pmport_info(sata_hba_inst, sata_device);
7127 			mutex_exit(&pmportinfo->pmport_mutex);
7128 			mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7129 		} else {
7130 			sata_update_port_info(sata_hba_inst, sata_device);
7131 		}
7132 
7133 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
7134 		*rval = TRAN_FATAL_ERROR;
7135 		break;
7136 
7137 	case SATA_TRAN_CMD_UNSUPPORTED:
7138 		/*
7139 		 * Command rejected by HBA as unsupported. It was HBA driver
7140 		 * that rejected the command, command was not sent to
7141 		 * an attached device.
7142 		 */
7143 		if ((sdinfo != NULL) &&
7144 		    (sdinfo->satadrv_state & SATA_DSTATE_RESET))
7145 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7146 			    "sat_hba_start: cmd 0x%2x rejected "
7147 			    "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd);
7148 
7149 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7150 		(void) sata_txlt_invalid_command(spx);
7151 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7152 
7153 		*rval = TRAN_ACCEPT;
7154 		break;
7155 
7156 	case SATA_TRAN_BUSY:
7157 		/*
7158 		 * Command rejected by HBA because other operation prevents
7159 		 * accepting the packet, or device is in RESET condition.
7160 		 */
7161 		if (sdinfo != NULL) {
7162 			sdinfo->satadrv_state =
7163 			    spx->txlt_sata_pkt->satapkt_device.satadev_state;
7164 
7165 			if (sdinfo->satadrv_state & SATA_DSTATE_RESET) {
7166 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7167 				    "sata_hba_start: cmd 0x%2x rejected "
7168 				    "because of device reset condition\n",
7169 				    cmd);
7170 			} else {
7171 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7172 				    "sata_hba_start: cmd 0x%2x rejected "
7173 				    "with SATA_TRAN_BUSY status\n",
7174 				    cmd);
7175 			}
7176 		}
7177 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
7178 		*rval = TRAN_BUSY;
7179 		break;
7180 
7181 	default:
7182 		/* Unrecognized HBA response */
7183 		SATA_LOG_D((sata_hba_inst, CE_WARN,
7184 		    "sata_hba_start: unrecognized HBA response "
7185 		    "to cmd : 0x%2x resp 0x%x", cmd, rval));
7186 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
7187 		*rval = TRAN_FATAL_ERROR;
7188 		break;
7189 	}
7190 
7191 	/*
7192 	 * If we got here, the packet was rejected.
7193 	 * Check if we need to remember reset state clearing request
7194 	 */
7195 	if (cmd_flags.sata_clear_dev_reset) {
7196 		/*
7197 		 * Check if device is still configured - it may have
7198 		 * disapeared from the configuration
7199 		 */
7200 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
7201 		if (sdinfo != NULL) {
7202 			/*
7203 			 * Restore the flag that requests clearing of
7204 			 * the device reset state,
7205 			 * so the next sata packet may carry it to HBA.
7206 			 */
7207 			if (sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT ||
7208 			    sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT) {
7209 				pminfo->pmult_event_flags |=
7210 				    SATA_EVNT_CLEAR_DEVICE_RESET;
7211 			} else {
7212 				sdinfo->satadrv_event_flags |=
7213 				    SATA_EVNT_CLEAR_DEVICE_RESET;
7214 			}
7215 		}
7216 	}
7217 	return (-1);
7218 }
7219 
7220 /*
7221  * Scsi response setup for invalid LBA
7222  *
7223  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
7224  */
7225 static int
7226 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx)
7227 {
7228 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7229 	struct scsi_extended_sense *sense;
7230 
7231 	scsipkt->pkt_reason = CMD_CMPLT;
7232 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7233 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7234 	*scsipkt->pkt_scbp = STATUS_CHECK;
7235 
7236 	*scsipkt->pkt_scbp = STATUS_CHECK;
7237 	sense = sata_arq_sense(spx);
7238 	sense->es_key = KEY_ILLEGAL_REQUEST;
7239 	sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7240 
7241 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7242 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7243 
7244 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7245 	    scsipkt->pkt_comp != NULL) {
7246 		/* scsi callback required */
7247 		if (servicing_interrupt()) {
7248 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7249 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7250 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
7251 			    TASKQID_INVALID) {
7252 				return (TRAN_BUSY);
7253 			}
7254 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7255 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7256 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
7257 			/* Scheduling the callback failed */
7258 			return (TRAN_BUSY);
7259 		}
7260 	}
7261 	return (TRAN_ACCEPT);
7262 }
7263 
7264 
7265 /*
7266  * Analyze device status and error registers and translate them into
7267  * appropriate scsi sense codes.
7268  * NOTE: non-packet commands only for now
7269  */
7270 static void
7271 sata_decode_device_error(sata_pkt_txlate_t *spx,
7272     struct scsi_extended_sense *sense)
7273 {
7274 	uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg;
7275 
7276 	ASSERT(sense != NULL);
7277 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
7278 	    SATA_STATUS_ERR);
7279 
7280 
7281 	if (err_reg & SATA_ERROR_ICRC) {
7282 		sense->es_key = KEY_ABORTED_COMMAND;
7283 		sense->es_add_code = 0x08; /* Communication failure */
7284 		return;
7285 	}
7286 
7287 	if (err_reg & SATA_ERROR_UNC) {
7288 		sense->es_key = KEY_MEDIUM_ERROR;
7289 		/* Information bytes (LBA) need to be set by a caller */
7290 		return;
7291 	}
7292 
7293 	/* ADD HERE: MC error bit handling for ATAPI CD/DVD */
7294 	if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) {
7295 		sense->es_key = KEY_UNIT_ATTENTION;
7296 		sense->es_add_code = 0x3a; /* No media present */
7297 		return;
7298 	}
7299 
7300 	if (err_reg & SATA_ERROR_IDNF) {
7301 		if (err_reg & SATA_ERROR_ABORT) {
7302 			sense->es_key = KEY_ABORTED_COMMAND;
7303 		} else {
7304 			sense->es_key = KEY_ILLEGAL_REQUEST;
7305 			sense->es_add_code = 0x21; /* LBA out of range */
7306 		}
7307 		return;
7308 	}
7309 
7310 	if (err_reg & SATA_ERROR_ABORT) {
7311 		ASSERT(spx->txlt_sata_pkt != NULL);
7312 		sense->es_key = KEY_ABORTED_COMMAND;
7313 		return;
7314 	}
7315 }
7316 
7317 /*
7318  * Extract error LBA from sata_pkt.satapkt_cmd register fields
7319  */
7320 static void
7321 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba)
7322 {
7323 	sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd;
7324 
7325 	*lba = 0;
7326 	if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) {
7327 		*lba = sata_cmd->satacmd_lba_high_msb;
7328 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb;
7329 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb;
7330 	} else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) {
7331 		*lba = sata_cmd->satacmd_device_reg & 0xf;
7332 	}
7333 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb;
7334 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb;
7335 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb;
7336 }
7337 
7338 /*
7339  * This is fixed sense format - if LBA exceeds the info field size,
7340  * no valid info will be returned (valid bit in extended sense will
7341  * be set to 0).
7342  */
7343 static struct scsi_extended_sense *
7344 sata_arq_sense(sata_pkt_txlate_t *spx)
7345 {
7346 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7347 	struct scsi_arq_status *arqs;
7348 	struct scsi_extended_sense *sense;
7349 
7350 	/* Fill ARQ sense data */
7351 	scsipkt->pkt_state |= STATE_ARQ_DONE;
7352 	arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp;
7353 	*(uchar_t *)&arqs->sts_status = STATUS_CHECK;
7354 	*(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD;
7355 	arqs->sts_rqpkt_reason = CMD_CMPLT;
7356 	arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7357 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
7358 	arqs->sts_rqpkt_resid = 0;
7359 	sense = &arqs->sts_sensedata;
7360 	bzero(sense, sizeof (struct scsi_extended_sense));
7361 	sata_fixed_sense_data_preset(sense);
7362 	return (sense);
7363 }
7364 
7365 /*
7366  * ATA Pass Through support
7367  * Sets flags indicating that an invalid value was found in some
7368  * field in the command.  It could be something illegal according to
7369  * the SAT-2 spec or it could be a feature that is not (yet?)
7370  * supported.
7371  */
7372 static int
7373 sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *spx)
7374 {
7375 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7376 	struct scsi_extended_sense *sense = sata_arq_sense(spx);
7377 
7378 	scsipkt->pkt_reason = CMD_CMPLT;
7379 	*scsipkt->pkt_scbp = STATUS_CHECK;
7380 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7381 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7382 
7383 	sense = sata_arq_sense(spx);
7384 	sense->es_key = KEY_ILLEGAL_REQUEST;
7385 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
7386 
7387 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7388 	    scsipkt->pkt_comp != NULL) {
7389 		/* scsi callback required */
7390 		if (servicing_interrupt()) {
7391 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7392 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7393 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
7394 			    TASKQID_INVALID) {
7395 				return (TRAN_BUSY);
7396 			}
7397 		} else 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) == TASKQID_INVALID) {
7400 			/* Scheduling the callback failed */
7401 			return (TRAN_BUSY);
7402 		}
7403 	}
7404 
7405 	return (TRAN_ACCEPT);
7406 }
7407 
7408 /*
7409  * The UNMAP command considers it not to be an error if the parameter length
7410  * or block descriptor length is 0.  For this case, there is nothing for TRIM
7411  * to do so just complete the command.
7412  */
7413 static int
7414 sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *spx)
7415 {
7416 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7417 
7418 	scsipkt->pkt_reason = CMD_CMPLT;
7419 	*scsipkt->pkt_scbp = STATUS_GOOD;
7420 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7421 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7422 
7423 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7424 	    scsipkt->pkt_comp != NULL) {
7425 		/* scsi callback required */
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_SLEEP) == TASKQID_INVALID) {
7429 			/* Scheduling the callback failed */
7430 			return (TRAN_BUSY);
7431 		}
7432 	}
7433 
7434 	return (TRAN_ACCEPT);
7435 }
7436 
7437 /*
7438  * Emulated SATA Read/Write command completion for zero-length requests.
7439  * This request always succedes, so in synchronous mode it always returns
7440  * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the
7441  * callback cannot be scheduled.
7442  */
7443 static int
7444 sata_emul_rw_completion(sata_pkt_txlate_t *spx)
7445 {
7446 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7447 
7448 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7449 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7450 	scsipkt->pkt_reason = CMD_CMPLT;
7451 	*scsipkt->pkt_scbp = STATUS_GOOD;
7452 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
7453 		/* scsi callback required - have to schedule it */
7454 		if (servicing_interrupt()) {
7455 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7456 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7457 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
7458 			    TASKQID_INVALID) {
7459 				return (TRAN_BUSY);
7460 			}
7461 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7462 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7463 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
7464 			/* Scheduling the callback failed */
7465 			return (TRAN_BUSY);
7466 		}
7467 	}
7468 	return (TRAN_ACCEPT);
7469 }
7470 
7471 
7472 /*
7473  * Translate completion status of SATA read/write commands into scsi response.
7474  * pkt completion_reason is checked to determine the completion status.
7475  * Do scsi callback if necessary.
7476  *
7477  * Note: this function may be called also for synchronously executed
7478  * commands.
7479  * This function may be used only if scsi_pkt is non-NULL.
7480  */
7481 static void
7482 sata_txlt_rw_completion(sata_pkt_t *sata_pkt)
7483 {
7484 	sata_pkt_txlate_t *spx =
7485 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7486 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7487 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7488 	struct scsi_extended_sense *sense;
7489 	uint64_t lba;
7490 	struct buf *bp;
7491 	int rval;
7492 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7493 		/* Normal completion */
7494 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7495 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7496 		scsipkt->pkt_reason = CMD_CMPLT;
7497 		*scsipkt->pkt_scbp = STATUS_GOOD;
7498 		if (spx->txlt_tmp_buf != NULL) {
7499 			/* Temporary buffer was used */
7500 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7501 			if (bp->b_flags & B_READ) {
7502 				rval = ddi_dma_sync(
7503 				    spx->txlt_buf_dma_handle, 0, 0,
7504 				    DDI_DMA_SYNC_FORCPU);
7505 				ASSERT(rval == DDI_SUCCESS);
7506 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7507 				    bp->b_bcount);
7508 			}
7509 		}
7510 	} else {
7511 		/*
7512 		 * Something went wrong - analyze return
7513 		 */
7514 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7515 		    STATE_SENT_CMD | STATE_GOT_STATUS;
7516 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7517 		*scsipkt->pkt_scbp = STATUS_CHECK;
7518 		sense = sata_arq_sense(spx);
7519 		ASSERT(sense != NULL);
7520 
7521 		/*
7522 		 * SATA_PKT_DEV_ERROR is the only case where we may be able to
7523 		 * extract from device registers the failing LBA.
7524 		 */
7525 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7526 			if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) &&
7527 			    (scmd->satacmd_lba_mid_msb != 0 ||
7528 			    scmd->satacmd_lba_high_msb != 0)) {
7529 				/*
7530 				 * We have problem reporting this cmd LBA
7531 				 * in fixed sense data format, because of
7532 				 * the size of the scsi LBA fields.
7533 				 */
7534 				sense->es_valid = 0;
7535 			} else {
7536 				sata_extract_error_lba(spx, &lba);
7537 				sense->es_info_1 = (lba & 0xFF000000) >> 24;
7538 				sense->es_info_2 = (lba & 0xFF0000) >> 16;
7539 				sense->es_info_3 = (lba & 0xFF00) >> 8;
7540 				sense->es_info_4 = lba & 0xFF;
7541 			}
7542 		} else {
7543 			/* Invalid extended sense info */
7544 			sense->es_valid = 0;
7545 		}
7546 
7547 		switch (sata_pkt->satapkt_reason) {
7548 		case SATA_PKT_PORT_ERROR:
7549 			/* We may want to handle DEV GONE state as well */
7550 			/*
7551 			 * We have no device data. Assume no data transfered.
7552 			 */
7553 			sense->es_key = KEY_HARDWARE_ERROR;
7554 			break;
7555 
7556 		case SATA_PKT_DEV_ERROR:
7557 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
7558 			    SATA_STATUS_ERR) {
7559 				/*
7560 				 * determine dev error reason from error
7561 				 * reg content
7562 				 */
7563 				sata_decode_device_error(spx, sense);
7564 				if (sense->es_key == KEY_MEDIUM_ERROR) {
7565 					switch (scmd->satacmd_cmd_reg) {
7566 					case SATAC_READ_DMA:
7567 					case SATAC_READ_DMA_EXT:
7568 					case SATAC_READ_DMA_QUEUED:
7569 					case SATAC_READ_DMA_QUEUED_EXT:
7570 					case SATAC_READ_FPDMA_QUEUED:
7571 						/* Unrecovered read error */
7572 						sense->es_add_code =
7573 						    SD_SCSI_ASC_UNREC_READ_ERR;
7574 						break;
7575 					case SATAC_WRITE_DMA:
7576 					case SATAC_WRITE_DMA_EXT:
7577 					case SATAC_WRITE_DMA_QUEUED:
7578 					case SATAC_WRITE_DMA_QUEUED_EXT:
7579 					case SATAC_WRITE_FPDMA_QUEUED:
7580 						/* Write error */
7581 						sense->es_add_code =
7582 						    SD_SCSI_ASC_WRITE_ERR;
7583 						break;
7584 					default:
7585 						/* Internal error */
7586 						SATA_LOG_D((
7587 						    spx->txlt_sata_hba_inst,
7588 						    CE_WARN,
7589 						    "sata_txlt_rw_completion :"
7590 						    "internal error - invalid "
7591 						    "command 0x%2x",
7592 						    scmd->satacmd_cmd_reg));
7593 						break;
7594 					}
7595 				}
7596 				break;
7597 			}
7598 			/* No extended sense key - no info available */
7599 			scsipkt->pkt_reason = CMD_INCOMPLETE;
7600 			break;
7601 
7602 		case SATA_PKT_TIMEOUT:
7603 			scsipkt->pkt_reason = CMD_TIMEOUT;
7604 			scsipkt->pkt_statistics |=
7605 			    STAT_TIMEOUT | STAT_DEV_RESET;
7606 			sense->es_key = KEY_ABORTED_COMMAND;
7607 			break;
7608 
7609 		case SATA_PKT_ABORTED:
7610 			scsipkt->pkt_reason = CMD_ABORTED;
7611 			scsipkt->pkt_statistics |= STAT_ABORTED;
7612 			sense->es_key = KEY_ABORTED_COMMAND;
7613 			break;
7614 
7615 		case SATA_PKT_RESET:
7616 			scsipkt->pkt_reason = CMD_RESET;
7617 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
7618 			sense->es_key = KEY_ABORTED_COMMAND;
7619 			break;
7620 
7621 		default:
7622 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7623 			    "sata_txlt_rw_completion: "
7624 			    "invalid packet completion reason"));
7625 			scsipkt->pkt_reason = CMD_TRAN_ERR;
7626 			break;
7627 		}
7628 	}
7629 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7630 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7631 
7632 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7633 		/* scsi callback required */
7634 		scsi_hba_pkt_comp(scsipkt);
7635 }
7636 
7637 
7638 /*
7639  * Translate completion status of non-data commands (i.e. commands returning
7640  * no data).
7641  * pkt completion_reason is checked to determine the completion status.
7642  * Do scsi callback if necessary (FLAG_NOINTR == 0)
7643  *
7644  * Note: this function may be called also for synchronously executed
7645  * commands.
7646  * This function may be used only if scsi_pkt is non-NULL.
7647  */
7648 
7649 static	void
7650 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt)
7651 {
7652 	sata_pkt_txlate_t *spx =
7653 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7654 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7655 
7656 	sata_set_arq_data(sata_pkt);
7657 
7658 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7659 		/* scsi callback required */
7660 		scsi_hba_pkt_comp(scsipkt);
7661 }
7662 
7663 /*
7664  * Completion handler for ATA Pass Through command
7665  */
7666 static void
7667 sata_txlt_apt_completion(sata_pkt_t *sata_pkt)
7668 {
7669 	sata_pkt_txlate_t *spx =
7670 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7671 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7672 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7673 	struct buf *bp;
7674 	uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0;
7675 
7676 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7677 		/* Normal completion */
7678 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7679 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7680 		scsipkt->pkt_reason = CMD_CMPLT;
7681 		*scsipkt->pkt_scbp = STATUS_GOOD;
7682 
7683 		/*
7684 		 * If the command has CK_COND set
7685 		 */
7686 		if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) {
7687 			*scsipkt->pkt_scbp = STATUS_CHECK;
7688 			sata_fill_ata_return_desc(sata_pkt,
7689 			    KEY_RECOVERABLE_ERROR,
7690 			    SD_SCSI_ASC_APT_INFO_AVAIL, 0x1d);
7691 		}
7692 
7693 		if (spx->txlt_tmp_buf != NULL) {
7694 			/* Temporary buffer was used */
7695 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7696 			if (bp->b_flags & B_READ) {
7697 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7698 				    bp->b_bcount);
7699 			}
7700 		}
7701 	} else {
7702 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7703 		    STATE_SENT_CMD | STATE_GOT_STATUS;
7704 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7705 		*scsipkt->pkt_scbp = STATUS_CHECK;
7706 
7707 		/*
7708 		 * If DF or ERR was set, the HBA should have copied out the
7709 		 * status and error registers to the satacmd structure.
7710 		 */
7711 		if (scmd->satacmd_status_reg & SATA_STATUS_DF) {
7712 			sense_key = KEY_HARDWARE_ERROR;
7713 			addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE;
7714 			addl_sense_qual = 0;
7715 		} else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
7716 			if (scmd->satacmd_error_reg & SATA_ERROR_NM) {
7717 				sense_key = KEY_NOT_READY;
7718 				addl_sense_code =
7719 				    SD_SCSI_ASC_MEDIUM_NOT_PRESENT;
7720 				addl_sense_qual = 0;
7721 			} else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) {
7722 				sense_key = KEY_MEDIUM_ERROR;
7723 				addl_sense_code = SD_SCSI_ASC_UNREC_READ_ERR;
7724 				addl_sense_qual = 0;
7725 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) {
7726 				sense_key = KEY_DATA_PROTECT;
7727 				addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED;
7728 				addl_sense_qual = 0;
7729 			} else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) {
7730 				sense_key = KEY_ILLEGAL_REQUEST;
7731 				addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7732 				addl_sense_qual = 0;
7733 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
7734 				sense_key = KEY_ABORTED_COMMAND;
7735 				addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE;
7736 				addl_sense_qual = 0;
7737 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MC) {
7738 				sense_key = KEY_UNIT_ATTENTION;
7739 				addl_sense_code =
7740 				    SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED;
7741 				addl_sense_qual = 0;
7742 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) {
7743 				sense_key = KEY_UNIT_ATTENTION;
7744 				addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ;
7745 				addl_sense_qual = 0;
7746 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) {
7747 				sense_key = KEY_ABORTED_COMMAND;
7748 				addl_sense_code =
7749 				    SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR;
7750 				addl_sense_qual = 0;
7751 			}
7752 		}
7753 
7754 		sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code,
7755 		    addl_sense_qual);
7756 	}
7757 
7758 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7759 		/* scsi callback required */
7760 		scsi_hba_pkt_comp(scsipkt);
7761 }
7762 
7763 /*
7764  * Completion handler for unmap translation command
7765  */
7766 static void
7767 sata_txlt_unmap_completion(sata_pkt_t *sata_pkt)
7768 {
7769 	sata_pkt_txlate_t *spx =
7770 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7771 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7772 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7773 	struct buf *bp;
7774 	uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0;
7775 
7776 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7777 		/* Normal completion */
7778 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7779 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7780 		scsipkt->pkt_reason = CMD_CMPLT;
7781 		*scsipkt->pkt_scbp = STATUS_GOOD;
7782 
7783 		if (spx->txlt_tmp_buf != NULL) {
7784 			/* Temporary buffer was used */
7785 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7786 			if (bp->b_flags & B_READ) {
7787 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7788 				    bp->b_bcount);
7789 			}
7790 		}
7791 	} else {
7792 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7793 		    STATE_SENT_CMD | STATE_GOT_STATUS;
7794 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7795 		*scsipkt->pkt_scbp = STATUS_CHECK;
7796 
7797 		/*
7798 		 * If DF or ERR was set, the HBA should have copied out the
7799 		 * status and error registers to the satacmd structure.
7800 		 */
7801 		if (scmd->satacmd_status_reg & SATA_STATUS_DF) {
7802 			sense_key = KEY_HARDWARE_ERROR;
7803 			addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE;
7804 			addl_sense_qual = 0;
7805 		} else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
7806 			if (scmd->satacmd_error_reg & SATA_ERROR_NM) {
7807 				sense_key = KEY_NOT_READY;
7808 				addl_sense_code =
7809 				    SD_SCSI_ASC_MEDIUM_NOT_PRESENT;
7810 				addl_sense_qual = 0;
7811 			} else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) {
7812 				sense_key = KEY_MEDIUM_ERROR;
7813 				addl_sense_code = SD_SCSI_ASC_WRITE_ERR;
7814 				addl_sense_qual = 0;
7815 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) {
7816 				sense_key = KEY_DATA_PROTECT;
7817 				addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED;
7818 				addl_sense_qual = 0;
7819 			} else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) {
7820 				sense_key = KEY_ILLEGAL_REQUEST;
7821 				addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7822 				addl_sense_qual = 0;
7823 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
7824 				sense_key = KEY_ABORTED_COMMAND;
7825 				addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE;
7826 				addl_sense_qual = 0;
7827 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MC) {
7828 				sense_key = KEY_UNIT_ATTENTION;
7829 				addl_sense_code =
7830 				    SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED;
7831 				addl_sense_qual = 0;
7832 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) {
7833 				sense_key = KEY_UNIT_ATTENTION;
7834 				addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ;
7835 				addl_sense_qual = 0;
7836 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) {
7837 				sense_key = KEY_ABORTED_COMMAND;
7838 				addl_sense_code =
7839 				    SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR;
7840 				addl_sense_qual = 0;
7841 			}
7842 		}
7843 
7844 		sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code,
7845 		    addl_sense_qual);
7846 	}
7847 
7848 	sata_free_local_buffer(spx);
7849 
7850 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7851 		/* scsi callback required */
7852 		scsi_hba_pkt_comp(scsipkt);
7853 }
7854 
7855 /*
7856  *
7857  */
7858 static void
7859 sata_fill_ata_return_desc(sata_pkt_t *sata_pkt, uint8_t sense_key,
7860     uint8_t addl_sense_code, uint8_t addl_sense_qual)
7861 {
7862 	sata_pkt_txlate_t *spx =
7863 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7864 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7865 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7866 	struct sata_apt_sense_data *apt_sd =
7867 	    (struct sata_apt_sense_data *)scsipkt->pkt_scbp;
7868 	struct scsi_descr_sense_hdr *sds = &(apt_sd->apt_sd_hdr);
7869 	struct scsi_ata_status_ret_sense_descr *ata_ret_desc =
7870 	    &(apt_sd->apt_sd_sense);
7871 	int extend = 0;
7872 
7873 	if ((scsipkt->pkt_cdbp[0] == SPC3_CMD_ATA_COMMAND_PASS_THROUGH16) &&
7874 	    (scsipkt->pkt_cdbp[2] & SATL_APT_BM_EXTEND))
7875 		extend = 1;
7876 
7877 	scsipkt->pkt_state |= STATE_ARQ_DONE;
7878 
7879 	/* update the residual count */
7880 	*(uchar_t *)&apt_sd->apt_status = STATUS_CHECK;
7881 	*(uchar_t *)&apt_sd->apt_rqpkt_status = STATUS_GOOD;
7882 	apt_sd->apt_rqpkt_reason = CMD_CMPLT;
7883 	apt_sd->apt_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7884 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
7885 	apt_sd->apt_rqpkt_resid = scsipkt->pkt_scblen -
7886 	    sizeof (struct sata_apt_sense_data);
7887 
7888 	/*
7889 	 * Fill in the Descriptor sense header
7890 	 */
7891 	bzero(sds, sizeof (struct scsi_descr_sense_hdr));
7892 	sds->ds_code = CODE_FMT_DESCR_CURRENT;
7893 	sds->ds_class = CLASS_EXTENDED_SENSE;
7894 	sds->ds_key = sense_key & 0xf;
7895 	sds->ds_add_code = addl_sense_code;
7896 	sds->ds_qual_code = addl_sense_qual;
7897 	sds->ds_addl_sense_length =
7898 	    sizeof (struct scsi_ata_status_ret_sense_descr);
7899 
7900 	/*
7901 	 * Fill in the ATA Return descriptor sense data
7902 	 */
7903 	bzero(ata_ret_desc, sizeof (struct scsi_ata_status_ret_sense_descr));
7904 	ata_ret_desc->ars_descr_type = DESCR_ATA_STATUS_RETURN;
7905 	ata_ret_desc->ars_addl_length = 0xc;
7906 	ata_ret_desc->ars_error = scmd->satacmd_error_reg;
7907 	ata_ret_desc->ars_sec_count_lsb = scmd->satacmd_sec_count_lsb;
7908 	ata_ret_desc->ars_lba_low_lsb = scmd->satacmd_lba_low_lsb;
7909 	ata_ret_desc->ars_lba_mid_lsb = scmd->satacmd_lba_mid_lsb;
7910 	ata_ret_desc->ars_lba_high_lsb = scmd->satacmd_lba_high_lsb;
7911 	ata_ret_desc->ars_device = scmd->satacmd_device_reg;
7912 	ata_ret_desc->ars_status = scmd->satacmd_status_reg;
7913 
7914 	if (extend == 1) {
7915 		ata_ret_desc->ars_extend = 1;
7916 		ata_ret_desc->ars_sec_count_msb = scmd->satacmd_sec_count_msb;
7917 		ata_ret_desc->ars_lba_low_msb = scmd->satacmd_lba_low_msb;
7918 		ata_ret_desc->ars_lba_mid_msb = scmd->satacmd_lba_mid_msb;
7919 		ata_ret_desc->ars_lba_high_msb = scmd->satacmd_lba_high_msb;
7920 	} else {
7921 		ata_ret_desc->ars_extend = 0;
7922 		ata_ret_desc->ars_sec_count_msb = 0;
7923 		ata_ret_desc->ars_lba_low_msb = 0;
7924 		ata_ret_desc->ars_lba_mid_msb = 0;
7925 		ata_ret_desc->ars_lba_high_msb = 0;
7926 	}
7927 }
7928 
7929 static	void
7930 sata_set_arq_data(sata_pkt_t *sata_pkt)
7931 {
7932 	sata_pkt_txlate_t *spx =
7933 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7934 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7935 	struct scsi_extended_sense *sense;
7936 
7937 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7938 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7939 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7940 		/* Normal completion */
7941 		scsipkt->pkt_reason = CMD_CMPLT;
7942 		*scsipkt->pkt_scbp = STATUS_GOOD;
7943 	} else {
7944 		/* Something went wrong */
7945 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7946 		*scsipkt->pkt_scbp = STATUS_CHECK;
7947 		sense = sata_arq_sense(spx);
7948 		switch (sata_pkt->satapkt_reason) {
7949 		case SATA_PKT_PORT_ERROR:
7950 			/*
7951 			 * We have no device data. Assume no data transfered.
7952 			 */
7953 			sense->es_key = KEY_HARDWARE_ERROR;
7954 			break;
7955 
7956 		case SATA_PKT_DEV_ERROR:
7957 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
7958 			    SATA_STATUS_ERR) {
7959 				/*
7960 				 * determine dev error reason from error
7961 				 * reg content
7962 				 */
7963 				sata_decode_device_error(spx, sense);
7964 				break;
7965 			}
7966 			/* No extended sense key - no info available */
7967 			break;
7968 
7969 		case SATA_PKT_TIMEOUT:
7970 			scsipkt->pkt_reason = CMD_TIMEOUT;
7971 			scsipkt->pkt_statistics |=
7972 			    STAT_TIMEOUT | STAT_DEV_RESET;
7973 			/* No extended sense key ? */
7974 			break;
7975 
7976 		case SATA_PKT_ABORTED:
7977 			scsipkt->pkt_reason = CMD_ABORTED;
7978 			scsipkt->pkt_statistics |= STAT_ABORTED;
7979 			/* No extended sense key ? */
7980 			break;
7981 
7982 		case SATA_PKT_RESET:
7983 			/* pkt aborted by an explicit reset from a host */
7984 			scsipkt->pkt_reason = CMD_RESET;
7985 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
7986 			break;
7987 
7988 		default:
7989 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7990 			    "sata_txlt_nodata_cmd_completion: "
7991 			    "invalid packet completion reason %d",
7992 			    sata_pkt->satapkt_reason));
7993 			scsipkt->pkt_reason = CMD_TRAN_ERR;
7994 			break;
7995 		}
7996 
7997 	}
7998 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7999 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
8000 }
8001 
8002 
8003 /*
8004  * Build Mode sense R/W recovery page
8005  * NOT IMPLEMENTED
8006  */
8007 
8008 static int
8009 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8010 {
8011 #ifndef __lock_lint
8012 	_NOTE(ARGUNUSED(sdinfo))
8013 	_NOTE(ARGUNUSED(pcntrl))
8014 	_NOTE(ARGUNUSED(buf))
8015 #endif
8016 	return (0);
8017 }
8018 
8019 /*
8020  * Build Mode sense caching page  -  scsi-3 implementation.
8021  * Page length distinguishes previous format from scsi-3 format.
8022  * buf must have space for 0x12 bytes.
8023  * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable.
8024  *
8025  */
8026 static int
8027 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8028 {
8029 	struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf;
8030 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8031 
8032 	/*
8033 	 * Most of the fields are set to 0, being not supported and/or disabled
8034 	 */
8035 	bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3);
8036 
8037 	/* Saved paramters not supported */
8038 	if (pcntrl == 3)
8039 		return (0);
8040 	if (pcntrl == 0 || pcntrl == 2) {
8041 		/*
8042 		 * For now treat current and default parameters as same
8043 		 * That may have to change, if target driver will complain
8044 		 */
8045 		page->mode_page.code = MODEPAGE_CACHING;	/* PS = 0 */
8046 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
8047 
8048 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id) &&
8049 		    !SATA_READ_AHEAD_ENABLED(*sata_id)) {
8050 			page->dra = 1;		/* Read Ahead disabled */
8051 			page->rcd = 1;		/* Read Cache disabled */
8052 		}
8053 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id) &&
8054 		    SATA_WRITE_CACHE_ENABLED(*sata_id))
8055 			page->wce = 1;		/* Write Cache enabled */
8056 	} else {
8057 		/* Changeable parameters */
8058 		page->mode_page.code = MODEPAGE_CACHING;
8059 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
8060 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
8061 			page->dra = 1;
8062 			page->rcd = 1;
8063 		}
8064 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id))
8065 			page->wce = 1;
8066 	}
8067 	return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
8068 	    sizeof (struct mode_page));
8069 }
8070 
8071 /*
8072  * Build Mode sense exception cntrl page
8073  */
8074 static int
8075 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8076 {
8077 	struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf;
8078 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8079 
8080 	/*
8081 	 * Most of the fields are set to 0, being not supported and/or disabled
8082 	 */
8083 	bzero(buf, PAGELENGTH_INFO_EXCPT);
8084 
8085 	page->mode_page.code = MODEPAGE_INFO_EXCPT;
8086 	page->mode_page.length = PAGELENGTH_INFO_EXCPT;
8087 
8088 	/* Indicate that this is page is saveable */
8089 	page->mode_page.ps = 1;
8090 
8091 	/*
8092 	 * We will return the same data for default, current and saved page.
8093 	 * The only changeable bit is dexcpt and that bit is required
8094 	 * by the ATA specification to be preserved across power cycles.
8095 	 */
8096 	if (pcntrl != 1) {
8097 		page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED);
8098 		page->mrie = MRIE_ONLY_ON_REQUEST;
8099 	}
8100 	else
8101 		page->dexcpt = 1;	/* Only changeable parameter */
8102 
8103 	return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page));
8104 }
8105 
8106 
8107 static int
8108 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8109 {
8110 	struct mode_acoustic_management *page =
8111 	    (struct mode_acoustic_management *)buf;
8112 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8113 
8114 	/*
8115 	 * Most of the fields are set to 0, being not supported and/or disabled
8116 	 */
8117 	bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT);
8118 
8119 	switch (pcntrl) {
8120 	case P_CNTRL_DEFAULT:
8121 		/*  default paramters not supported */
8122 		return (0);
8123 
8124 	case P_CNTRL_CURRENT:
8125 	case P_CNTRL_SAVED:
8126 		/* Saved and current are supported and are identical */
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 feature is supported */
8133 		/* If feature is not supported */
8134 		if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) {
8135 			page->acoustic_manag_enable =
8136 			    ACOUSTIC_DISABLED;
8137 		} else {
8138 			page->acoustic_manag_enable =
8139 			    ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT)
8140 			    != 0);
8141 			/* Word 94 inidicates the value */
8142 #ifdef	_LITTLE_ENDIAN
8143 			page->acoustic_manag_level =
8144 			    (uchar_t)sata_id->ai_acoustic;
8145 			page->vendor_recommended_value =
8146 			    sata_id->ai_acoustic >> 8;
8147 #else
8148 			page->acoustic_manag_level =
8149 			    sata_id->ai_acoustic >> 8;
8150 			page->vendor_recommended_value =
8151 			    (uchar_t)sata_id->ai_acoustic;
8152 #endif
8153 		}
8154 		break;
8155 
8156 	case P_CNTRL_CHANGEABLE:
8157 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
8158 		page->mode_page.length =
8159 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
8160 		page->mode_page.ps = 1;
8161 
8162 		/* Word 83 indicates if the feature is supported */
8163 		if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) {
8164 			page->acoustic_manag_enable =
8165 			    ACOUSTIC_ENABLED;
8166 			page->acoustic_manag_level = 0xff;
8167 		}
8168 		break;
8169 	}
8170 	return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8171 	    sizeof (struct mode_page));
8172 }
8173 
8174 
8175 /*
8176  * Build Mode sense power condition page.
8177  */
8178 static int
8179 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8180 {
8181 	struct mode_info_power_cond *page = (struct mode_info_power_cond *)buf;
8182 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8183 
8184 	/*
8185 	 * Most of the fields are set to 0, being not supported and/or disabled
8186 	 * power condition page length was 0x0a
8187 	 */
8188 	bzero(buf, sizeof (struct mode_info_power_cond));
8189 
8190 	if (pcntrl == P_CNTRL_DEFAULT) {
8191 		/*  default paramters not supported */
8192 		return (0);
8193 	}
8194 
8195 	page->mode_page.code = MODEPAGE_POWER_COND;
8196 	page->mode_page.length = sizeof (struct mode_info_power_cond);
8197 
8198 	if (sata_id->ai_cap & SATA_STANDBYTIMER) {
8199 		page->standby = 1;
8200 		bcopy(sdinfo->satadrv_standby_timer, page->standby_cond_timer,
8201 		    sizeof (uchar_t) * 4);
8202 	}
8203 
8204 	return (sizeof (struct mode_info_power_cond));
8205 }
8206 
8207 /*
8208  * Process mode select caching page 8 (scsi3 format only).
8209  * Read Ahead (same as read cache) and Write Cache may be turned on and off
8210  * if these features are supported by the device. If these features are not
8211  * supported, the command will be terminated with STATUS_CHECK.
8212  * This function fails only if the SET FEATURE command sent to
8213  * the device fails. The page format is not verified, assuming that the
8214  * target driver operates correctly - if parameters length is too short,
8215  * we just drop the page.
8216  * Two command may be sent if both Read Cache/Read Ahead and Write Cache
8217  * setting have to be changed.
8218  * SET FEATURE command is executed synchronously, i.e. we wait here until
8219  * it is completed, regardless of the scsi pkt directives.
8220  *
8221  * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e.
8222  * changing DRA will change RCD.
8223  *
8224  * More than one SATA command may be executed to perform operations specified
8225  * by mode select pages. The first error terminates further execution.
8226  * Operations performed successully are not backed-up in such case.
8227  *
8228  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
8229  * If operation resulted in changing device setup, dmod flag should be set to
8230  * one (1). If parameters were not changed, dmod flag should be set to 0.
8231  * Upon return, if operation required sending command to the device, the rval
8232  * should be set to the value returned by sata_hba_start. If operation
8233  * did not require device access, rval should be set to TRAN_ACCEPT.
8234  * The pagelen should be set to the length of the page.
8235  *
8236  * This function has to be called with a port mutex held.
8237  *
8238  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8239  */
8240 int
8241 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page,
8242     int parmlen, int *pagelen, int *rval, int *dmod)
8243 {
8244 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8245 	sata_drive_info_t *sdinfo;
8246 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8247 	sata_id_t *sata_id;
8248 	struct scsi_extended_sense *sense;
8249 	int wce, dra;	/* Current settings */
8250 
8251 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8252 	    &spx->txlt_sata_pkt->satapkt_device);
8253 	sata_id = &sdinfo->satadrv_id;
8254 	*dmod = 0;
8255 
8256 	/* Verify parameters length. If too short, drop it */
8257 	if ((PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
8258 	    sizeof (struct mode_page)) > parmlen) {
8259 		*scsipkt->pkt_scbp = STATUS_CHECK;
8260 		sense = sata_arq_sense(spx);
8261 		sense->es_key = KEY_ILLEGAL_REQUEST;
8262 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8263 		*pagelen = parmlen;
8264 		*rval = TRAN_ACCEPT;
8265 		return (SATA_FAILURE);
8266 	}
8267 
8268 	*pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page);
8269 
8270 	/* Current setting of Read Ahead (and Read Cache) */
8271 	if (SATA_READ_AHEAD_ENABLED(*sata_id))
8272 		dra = 0;	/* 0 == not disabled */
8273 	else
8274 		dra = 1;
8275 	/* Current setting of Write Cache */
8276 	if (SATA_WRITE_CACHE_ENABLED(*sata_id))
8277 		wce = 1;
8278 	else
8279 		wce = 0;
8280 
8281 	if (page->dra == dra && page->wce == wce && page->rcd == dra) {
8282 		/* nothing to do */
8283 		*rval = TRAN_ACCEPT;
8284 		return (SATA_SUCCESS);
8285 	}
8286 
8287 	/*
8288 	 * Need to flip some setting
8289 	 * Set-up Internal SET FEATURES command(s)
8290 	 */
8291 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8292 	scmd->satacmd_addr_type = 0;
8293 	scmd->satacmd_device_reg = 0;
8294 	scmd->satacmd_status_reg = 0;
8295 	scmd->satacmd_error_reg = 0;
8296 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
8297 	if (page->dra != dra || page->rcd != dra) {
8298 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
8299 			/* Need to flip read ahead setting */
8300 			if (dra == 0)
8301 				/* Disable read ahead / read cache */
8302 				scmd->satacmd_features_reg =
8303 				    SATAC_SF_DISABLE_READ_AHEAD;
8304 			else
8305 				/* Enable read ahead  / read cache */
8306 				scmd->satacmd_features_reg =
8307 				    SATAC_SF_ENABLE_READ_AHEAD;
8308 
8309 			/* Transfer command to HBA */
8310 			if (sata_hba_start(spx, rval) != 0)
8311 				/*
8312 				 * Pkt not accepted for execution.
8313 				 */
8314 				return (SATA_FAILURE);
8315 
8316 			*dmod = 1;
8317 
8318 			/* Now process return */
8319 			if (spx->txlt_sata_pkt->satapkt_reason !=
8320 			    SATA_PKT_COMPLETED) {
8321 				goto failure;	/* Terminate */
8322 			}
8323 		} else {
8324 			*scsipkt->pkt_scbp = STATUS_CHECK;
8325 			sense = sata_arq_sense(spx);
8326 			sense->es_key = KEY_ILLEGAL_REQUEST;
8327 			sense->es_add_code =
8328 			    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8329 			*pagelen = parmlen;
8330 			*rval = TRAN_ACCEPT;
8331 			return (SATA_FAILURE);
8332 		}
8333 	}
8334 
8335 	/* Note that the packet is not removed, so it could be re-used */
8336 	if (page->wce != wce) {
8337 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id)) {
8338 			/* Need to flip Write Cache setting */
8339 			if (page->wce == 1)
8340 				/* Enable write cache */
8341 				scmd->satacmd_features_reg =
8342 				    SATAC_SF_ENABLE_WRITE_CACHE;
8343 			else
8344 				/* Disable write cache */
8345 				scmd->satacmd_features_reg =
8346 				    SATAC_SF_DISABLE_WRITE_CACHE;
8347 
8348 			/* Transfer command to HBA */
8349 			if (sata_hba_start(spx, rval) != 0)
8350 				/*
8351 				 * Pkt not accepted for execution.
8352 				 */
8353 				return (SATA_FAILURE);
8354 
8355 			*dmod = 1;
8356 
8357 			/* Now process return */
8358 			if (spx->txlt_sata_pkt->satapkt_reason !=
8359 			    SATA_PKT_COMPLETED) {
8360 				goto failure;
8361 			}
8362 		} else {
8363 			*scsipkt->pkt_scbp = STATUS_CHECK;
8364 			sense = sata_arq_sense(spx);
8365 			sense->es_key = KEY_ILLEGAL_REQUEST;
8366 			sense->es_add_code =
8367 			    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8368 			*pagelen = parmlen;
8369 			*rval = TRAN_ACCEPT;
8370 			return (SATA_FAILURE);
8371 		}
8372 	}
8373 	return (SATA_SUCCESS);
8374 
8375 failure:
8376 	sata_xlate_errors(spx);
8377 
8378 	return (SATA_FAILURE);
8379 }
8380 
8381 /*
8382  * Process mode select informational exceptions control page 0x1c
8383  *
8384  * The only changeable bit is dexcpt (disable exceptions).
8385  * MRIE (method of reporting informational exceptions) must be
8386  * "only on request".
8387  * This page applies to informational exceptions that report
8388  * additional sense codes with the ADDITIONAL SENSE CODE field set to 5Dh
8389  * (e.g.,FAILURE PREDICTION THRESHOLD EXCEEDED) or 0Bh (e.g., WARNING_).
8390  * Informational exception conditions occur as the result of background scan
8391  * errors, background self-test errors, or vendor specific events within a
8392  * logical unit. An informational exception condition may occur asynchronous
8393  * to any commands.
8394  *
8395  * Returns: SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
8396  * If operation resulted in changing device setup, dmod flag should be set to
8397  * one (1). If parameters were not changed, dmod flag should be set to 0.
8398  * Upon return, if operation required sending command to the device, the rval
8399  * should be set to the value returned by sata_hba_start. If operation
8400  * did not require device access, rval should be set to TRAN_ACCEPT.
8401  * The pagelen should be set to the length of the page.
8402  *
8403  * This function has to be called with a port mutex held.
8404  *
8405  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8406  *
8407  * Cannot be called in the interrupt context.
8408  */
8409 static	int
8410 sata_mode_select_page_1c(
8411 	sata_pkt_txlate_t *spx,
8412 	struct mode_info_excpt_page *page,
8413 	int parmlen,
8414 	int *pagelen,
8415 	int *rval,
8416 	int *dmod)
8417 {
8418 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8419 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8420 	sata_drive_info_t *sdinfo;
8421 	sata_id_t *sata_id;
8422 	struct scsi_extended_sense *sense;
8423 
8424 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8425 	    &spx->txlt_sata_pkt->satapkt_device);
8426 	sata_id = &sdinfo->satadrv_id;
8427 
8428 	*dmod = 0;
8429 
8430 	/* Verify parameters length. If too short, drop it */
8431 	if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) > parmlen) ||
8432 	    page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) {
8433 		*scsipkt->pkt_scbp = STATUS_CHECK;
8434 		sense = sata_arq_sense(spx);
8435 		sense->es_key = KEY_ILLEGAL_REQUEST;
8436 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8437 		*pagelen = parmlen;
8438 		*rval = TRAN_ACCEPT;
8439 		return (SATA_FAILURE);
8440 	}
8441 
8442 	*pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page);
8443 
8444 	if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
8445 		*scsipkt->pkt_scbp = STATUS_CHECK;
8446 		sense = sata_arq_sense(spx);
8447 		sense->es_key = KEY_ILLEGAL_REQUEST;
8448 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
8449 		*pagelen = parmlen;
8450 		*rval = TRAN_ACCEPT;
8451 		return (SATA_FAILURE);
8452 	}
8453 
8454 	/* If already in the state requested, we are done */
8455 	if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
8456 		/* nothing to do */
8457 		*rval = TRAN_ACCEPT;
8458 		return (SATA_SUCCESS);
8459 	}
8460 
8461 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8462 
8463 	/* Build SMART_ENABLE or SMART_DISABLE command */
8464 	scmd->satacmd_addr_type = 0;		/* N/A */
8465 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
8466 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
8467 	scmd->satacmd_features_reg = page->dexcpt ?
8468 	    SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS;
8469 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
8470 	scmd->satacmd_cmd_reg = SATAC_SMART;
8471 
8472 	/* Transfer command to HBA */
8473 	if (sata_hba_start(spx, rval) != 0)
8474 		/*
8475 		 * Pkt not accepted for execution.
8476 		 */
8477 		return (SATA_FAILURE);
8478 
8479 	*dmod = 1;	/* At least may have been modified */
8480 
8481 	/* Now process return */
8482 	if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED)
8483 		return (SATA_SUCCESS);
8484 
8485 	/* Packet did not complete successfully */
8486 	sata_xlate_errors(spx);
8487 
8488 	return (SATA_FAILURE);
8489 }
8490 
8491 /*
8492  * Process mode select acoustic management control page 0x30
8493  *
8494  *
8495  * This function has to be called with a port mutex held.
8496  *
8497  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8498  *
8499  * Cannot be called in the interrupt context.
8500  */
8501 int
8502 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct
8503     mode_acoustic_management *page, int parmlen, int *pagelen,
8504     int *rval, int *dmod)
8505 {
8506 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8507 	sata_drive_info_t *sdinfo;
8508 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8509 	sata_id_t *sata_id;
8510 	struct scsi_extended_sense *sense;
8511 
8512 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8513 	    &spx->txlt_sata_pkt->satapkt_device);
8514 	sata_id = &sdinfo->satadrv_id;
8515 	*dmod = 0;
8516 
8517 	/* If parmlen is too short or the feature is not supported, drop it */
8518 	if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8519 	    sizeof (struct mode_page)) > parmlen) ||
8520 	    (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) {
8521 		*scsipkt->pkt_scbp = STATUS_CHECK;
8522 		sense = sata_arq_sense(spx);
8523 		sense->es_key = KEY_ILLEGAL_REQUEST;
8524 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8525 		*pagelen = parmlen;
8526 		*rval = TRAN_ACCEPT;
8527 		return (SATA_FAILURE);
8528 	}
8529 
8530 	*pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8531 	    sizeof (struct mode_page);
8532 
8533 	/*
8534 	 * We can enable and disable acoustice management and
8535 	 * set the acoustic management level.
8536 	 */
8537 
8538 	/*
8539 	 * Set-up Internal SET FEATURES command(s)
8540 	 */
8541 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8542 	scmd->satacmd_addr_type = 0;
8543 	scmd->satacmd_device_reg = 0;
8544 	scmd->satacmd_status_reg = 0;
8545 	scmd->satacmd_error_reg = 0;
8546 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
8547 	if (page->acoustic_manag_enable) {
8548 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC;
8549 		scmd->satacmd_sec_count_lsb = page->acoustic_manag_level;
8550 	} else {	/* disabling acoustic management */
8551 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC;
8552 	}
8553 
8554 	/* Transfer command to HBA */
8555 	if (sata_hba_start(spx, rval) != 0)
8556 		/*
8557 		 * Pkt not accepted for execution.
8558 		 */
8559 		return (SATA_FAILURE);
8560 
8561 	/* Now process return */
8562 	if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) {
8563 		sata_xlate_errors(spx);
8564 		return (SATA_FAILURE);
8565 	}
8566 
8567 	*dmod = 1;
8568 
8569 	return (SATA_SUCCESS);
8570 }
8571 
8572 /*
8573  * Process mode select power condition page 0x1a
8574  *
8575  * This function has to be called with a port mutex held.
8576  *
8577  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8578  *
8579  * Cannot be called in the interrupt context.
8580  */
8581 int
8582 sata_mode_select_page_1a(sata_pkt_txlate_t *spx, struct
8583     mode_info_power_cond *page, int parmlen, int *pagelen,
8584     int *rval, int *dmod)
8585 {
8586 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8587 	sata_drive_info_t *sdinfo;
8588 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8589 	sata_id_t *sata_id;
8590 	struct scsi_extended_sense *sense;
8591 	uint8_t ata_count;
8592 	int i, len;
8593 
8594 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8595 	    &spx->txlt_sata_pkt->satapkt_device);
8596 	sata_id = &sdinfo->satadrv_id;
8597 	*dmod = 0;
8598 
8599 	len = sizeof (struct mode_info_power_cond);
8600 	len += sizeof (struct mode_page);
8601 
8602 	/* If parmlen is too short or the feature is not supported, drop it */
8603 	if ((len < parmlen) || (page->idle == 1) ||
8604 	    (!(sata_id->ai_cap & SATA_STANDBYTIMER) && page->standby == 1)) {
8605 		*scsipkt->pkt_scbp = STATUS_CHECK;
8606 		sense = sata_arq_sense(spx);
8607 		sense->es_key = KEY_ILLEGAL_REQUEST;
8608 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8609 		*pagelen = parmlen;
8610 		*rval = TRAN_ACCEPT;
8611 		return (SATA_FAILURE);
8612 	}
8613 
8614 	*pagelen = len;
8615 
8616 	/*
8617 	 * Set-up Internal STANDBY command(s)
8618 	 */
8619 	if (page->standby == 0)
8620 		goto out;
8621 
8622 	ata_count = sata_get_standby_timer(page->standby_cond_timer);
8623 
8624 	scmd->satacmd_addr_type = 0;
8625 	scmd->satacmd_sec_count_lsb = ata_count;
8626 	scmd->satacmd_lba_low_lsb = 0;
8627 	scmd->satacmd_lba_mid_lsb = 0;
8628 	scmd->satacmd_lba_high_lsb = 0;
8629 	scmd->satacmd_features_reg = 0;
8630 	scmd->satacmd_device_reg = 0;
8631 	scmd->satacmd_status_reg = 0;
8632 	scmd->satacmd_cmd_reg = SATAC_STANDBY;
8633 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
8634 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
8635 
8636 	/* Transfer command to HBA */
8637 	if (sata_hba_start(spx, rval) != 0) {
8638 		return (SATA_FAILURE);
8639 	} else {
8640 		if ((scmd->satacmd_error_reg != 0) ||
8641 		    (spx->txlt_sata_pkt->satapkt_reason !=
8642 		    SATA_PKT_COMPLETED)) {
8643 			sata_xlate_errors(spx);
8644 			return (SATA_FAILURE);
8645 		}
8646 	}
8647 
8648 	for (i = 0; i < 4; i++) {
8649 		sdinfo->satadrv_standby_timer[i] = page->standby_cond_timer[i];
8650 	}
8651 out:
8652 	*dmod = 1;
8653 	return (SATA_SUCCESS);
8654 }
8655 
8656 /*
8657  * sata_build_lsense_page0() is used to create the
8658  * SCSI LOG SENSE page 0 (supported log pages)
8659  *
8660  * Currently supported pages are 0, 0x10, 0x2f, 0x30 and 0x0e
8661  * (supported log pages, self-test results, informational exceptions
8662  * Sun vendor specific ATA SMART data, and start stop cycle counter).
8663  *
8664  * Takes a sata_drive_info t * and the address of a buffer
8665  * in which to create the page information.
8666  *
8667  * Returns the number of bytes valid in the buffer.
8668  */
8669 static	int
8670 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf)
8671 {
8672 	struct log_parameter *lpp = (struct log_parameter *)buf;
8673 	uint8_t *page_ptr = (uint8_t *)lpp->param_values;
8674 	int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */
8675 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8676 
8677 	lpp->param_code[0] = 0;
8678 	lpp->param_code[1] = 0;
8679 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
8680 	*page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES;
8681 
8682 	if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
8683 		if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) {
8684 			*page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS;
8685 			++num_pages_supported;
8686 		}
8687 		*page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS;
8688 		++num_pages_supported;
8689 		*page_ptr++ = PAGE_CODE_SMART_READ_DATA;
8690 		++num_pages_supported;
8691 		*page_ptr++ = PAGE_CODE_START_STOP_CYCLE_COUNTER;
8692 		++num_pages_supported;
8693 	}
8694 
8695 	lpp->param_len = num_pages_supported;
8696 
8697 	return ((&lpp->param_values[0] - (uint8_t *)lpp) +
8698 	    num_pages_supported);
8699 }
8700 
8701 /*
8702  * sata_build_lsense_page_10() is used to create the
8703  * SCSI LOG SENSE page 0x10 (self-test results)
8704  *
8705  * Takes a sata_drive_info t * and the address of a buffer
8706  * in which to create the page information as well as a sata_hba_inst_t *.
8707  *
8708  * Returns the number of bytes valid in the buffer.
8709  *
8710  * Note: Self test and SMART data is accessible in device log pages.
8711  * The log pages can be accessed by SMART READ/WRITE LOG (up to 255 sectors
8712  * of data can be transferred by a single command), or by the General Purpose
8713  * Logging commands (GPL) READ LOG EXT and WRITE LOG EXT (up to 65,535 sectors
8714  * - approximately 33MB - can be transferred by a single command.
8715  * The SCT Command response (either error or command) is the same for both
8716  * the SMART and GPL methods of issuing commands.
8717  * This function uses READ LOG EXT command when drive supports LBA48, and
8718  * SMART READ command otherwise.
8719  *
8720  * Since above commands are executed in a synchronous mode, this function
8721  * should not be called in an interrupt context.
8722  */
8723 static	int
8724 sata_build_lsense_page_10(
8725 	sata_drive_info_t *sdinfo,
8726 	uint8_t *buf,
8727 	sata_hba_inst_t *sata_hba_inst)
8728 {
8729 	struct log_parameter *lpp = (struct log_parameter *)buf;
8730 	int rval;
8731 
8732 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
8733 		struct smart_ext_selftest_log *ext_selftest_log;
8734 
8735 		ext_selftest_log = kmem_zalloc(
8736 		    sizeof (struct smart_ext_selftest_log), KM_SLEEP);
8737 
8738 		rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo,
8739 		    ext_selftest_log, 0);
8740 		if (rval == 0) {
8741 			int index, start_index;
8742 			struct smart_ext_selftest_log_entry *entry;
8743 			static const struct smart_ext_selftest_log_entry empty =
8744 			    {0};
8745 			uint16_t block_num;
8746 			int count;
8747 			boolean_t only_one_block = B_FALSE;
8748 
8749 			index = ext_selftest_log->
8750 			    smart_ext_selftest_log_index[0];
8751 			index |= ext_selftest_log->
8752 			    smart_ext_selftest_log_index[1] << 8;
8753 			if (index == 0)
8754 				goto out;
8755 
8756 			--index;	/* Correct for 0 origin */
8757 			start_index = index;	/* remember where we started */
8758 			block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8759 			if (block_num != 0) {
8760 				rval = sata_ext_smart_selftest_read_log(
8761 				    sata_hba_inst, sdinfo, ext_selftest_log,
8762 				    block_num);
8763 				if (rval != 0)
8764 					goto out;
8765 			}
8766 			index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8767 			entry =
8768 			    &ext_selftest_log->
8769 			    smart_ext_selftest_log_entries[index];
8770 
8771 			for (count = 1;
8772 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
8773 			    ++count) {
8774 				uint8_t status;
8775 				uint8_t code;
8776 				uint8_t sense_key;
8777 				uint8_t add_sense_code;
8778 				uint8_t add_sense_code_qual;
8779 
8780 				/* If this is an unused entry, we are done */
8781 				if (bcmp(entry, &empty, sizeof (empty)) == 0) {
8782 					/* Broken firmware on some disks */
8783 					if (index + 1 ==
8784 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK) {
8785 						--entry;
8786 						--index;
8787 						if (bcmp(entry, &empty,
8788 						    sizeof (empty)) == 0)
8789 							goto out;
8790 					} else
8791 						goto out;
8792 				}
8793 
8794 				if (only_one_block &&
8795 				    start_index == index)
8796 					goto out;
8797 
8798 				lpp->param_code[0] = 0;
8799 				lpp->param_code[1] = count;
8800 				lpp->param_ctrl_flags =
8801 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
8802 				lpp->param_len =
8803 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
8804 
8805 				status = entry->smart_ext_selftest_log_status;
8806 				status >>= 4;
8807 				switch (status) {
8808 				case 0:
8809 				default:
8810 					sense_key = KEY_NO_SENSE;
8811 					add_sense_code =
8812 					    SD_SCSI_ASC_NO_ADD_SENSE;
8813 					add_sense_code_qual = 0;
8814 					break;
8815 				case 1:
8816 					sense_key = KEY_ABORTED_COMMAND;
8817 					add_sense_code =
8818 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8819 					add_sense_code_qual = SCSI_COMPONENT_81;
8820 					break;
8821 				case 2:
8822 					sense_key = KEY_ABORTED_COMMAND;
8823 					add_sense_code =
8824 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8825 					add_sense_code_qual = SCSI_COMPONENT_82;
8826 					break;
8827 				case 3:
8828 					sense_key = KEY_ABORTED_COMMAND;
8829 					add_sense_code =
8830 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8831 					add_sense_code_qual = SCSI_COMPONENT_83;
8832 					break;
8833 				case 4:
8834 					sense_key = KEY_HARDWARE_ERROR;
8835 					add_sense_code =
8836 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8837 					add_sense_code_qual = SCSI_COMPONENT_84;
8838 					break;
8839 				case 5:
8840 					sense_key = KEY_HARDWARE_ERROR;
8841 					add_sense_code =
8842 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8843 					add_sense_code_qual = SCSI_COMPONENT_85;
8844 					break;
8845 				case 6:
8846 					sense_key = KEY_HARDWARE_ERROR;
8847 					add_sense_code =
8848 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8849 					add_sense_code_qual = SCSI_COMPONENT_86;
8850 					break;
8851 				case 7:
8852 					sense_key = KEY_MEDIUM_ERROR;
8853 					add_sense_code =
8854 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8855 					add_sense_code_qual = SCSI_COMPONENT_87;
8856 					break;
8857 				case 8:
8858 					sense_key = KEY_HARDWARE_ERROR;
8859 					add_sense_code =
8860 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8861 					add_sense_code_qual = SCSI_COMPONENT_88;
8862 					break;
8863 				}
8864 				code = 0;	/* unspecified */
8865 				status |= (code << 4);
8866 				lpp->param_values[0] = status;
8867 				lpp->param_values[1] = 0; /* unspecified */
8868 				lpp->param_values[2] = entry->
8869 				    smart_ext_selftest_log_timestamp[1];
8870 				lpp->param_values[3] = entry->
8871 				    smart_ext_selftest_log_timestamp[0];
8872 				if (status != 0) {
8873 					lpp->param_values[4] = 0;
8874 					lpp->param_values[5] = 0;
8875 					lpp->param_values[6] = entry->
8876 					    smart_ext_selftest_log_failing_lba
8877 					    [5];
8878 					lpp->param_values[7] = entry->
8879 					    smart_ext_selftest_log_failing_lba
8880 					    [4];
8881 					lpp->param_values[8] = entry->
8882 					    smart_ext_selftest_log_failing_lba
8883 					    [3];
8884 					lpp->param_values[9] = entry->
8885 					    smart_ext_selftest_log_failing_lba
8886 					    [2];
8887 					lpp->param_values[10] = entry->
8888 					    smart_ext_selftest_log_failing_lba
8889 					    [1];
8890 					lpp->param_values[11] = entry->
8891 					    smart_ext_selftest_log_failing_lba
8892 					    [0];
8893 				} else {	/* No bad block address */
8894 					lpp->param_values[4] = 0xff;
8895 					lpp->param_values[5] = 0xff;
8896 					lpp->param_values[6] = 0xff;
8897 					lpp->param_values[7] = 0xff;
8898 					lpp->param_values[8] = 0xff;
8899 					lpp->param_values[9] = 0xff;
8900 					lpp->param_values[10] = 0xff;
8901 					lpp->param_values[11] = 0xff;
8902 				}
8903 
8904 				lpp->param_values[12] = sense_key;
8905 				lpp->param_values[13] = add_sense_code;
8906 				lpp->param_values[14] = add_sense_code_qual;
8907 				lpp->param_values[15] = 0; /* undefined */
8908 
8909 				lpp = (struct log_parameter *)
8910 				    (((uint8_t *)lpp) +
8911 				    SCSI_LOG_PARAM_HDR_LEN +
8912 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
8913 
8914 				--index;	/* Back up to previous entry */
8915 				if (index < 0) {
8916 					if (block_num > 0) {
8917 						--block_num;
8918 					} else {
8919 						struct read_log_ext_directory
8920 						    logdir;
8921 
8922 						rval =
8923 						    sata_read_log_ext_directory(
8924 						    sata_hba_inst, sdinfo,
8925 						    &logdir);
8926 						if (rval == -1)
8927 							goto out;
8928 						if ((logdir.read_log_ext_vers
8929 						    [0] == 0) &&
8930 						    (logdir.read_log_ext_vers
8931 						    [1] == 0))
8932 							goto out;
8933 						block_num =
8934 						    logdir.read_log_ext_nblks
8935 						    [EXT_SMART_SELFTEST_LOG_PAGE
8936 						    - 1][0];
8937 						block_num |= logdir.
8938 						    read_log_ext_nblks
8939 						    [EXT_SMART_SELFTEST_LOG_PAGE
8940 						    - 1][1] << 8;
8941 						--block_num;
8942 						only_one_block =
8943 						    (block_num == 0);
8944 					}
8945 					rval = sata_ext_smart_selftest_read_log(
8946 					    sata_hba_inst, sdinfo,
8947 					    ext_selftest_log, block_num);
8948 					if (rval != 0)
8949 						goto out;
8950 
8951 					index =
8952 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK -
8953 					    1;
8954 				}
8955 				index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8956 				entry = &ext_selftest_log->
8957 				    smart_ext_selftest_log_entries[index];
8958 			}
8959 		}
8960 out:
8961 		kmem_free(ext_selftest_log,
8962 		    sizeof (struct smart_ext_selftest_log));
8963 	} else {
8964 		struct smart_selftest_log *selftest_log;
8965 
8966 		selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log),
8967 		    KM_SLEEP);
8968 
8969 		rval = sata_smart_selftest_log(sata_hba_inst, sdinfo,
8970 		    selftest_log);
8971 
8972 		if (rval == 0) {
8973 			int index;
8974 			int count;
8975 			struct smart_selftest_log_entry *entry;
8976 			static const struct smart_selftest_log_entry empty =
8977 			    { 0 };
8978 
8979 			index = selftest_log->smart_selftest_log_index;
8980 			if (index == 0)
8981 				goto done;
8982 			--index;	/* Correct for 0 origin */
8983 			entry = &selftest_log->
8984 			    smart_selftest_log_entries[index];
8985 			for (count = 1;
8986 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
8987 			    ++count) {
8988 				uint8_t status;
8989 				uint8_t code;
8990 				uint8_t sense_key;
8991 				uint8_t add_sense_code;
8992 				uint8_t add_sense_code_qual;
8993 
8994 				if (bcmp(entry, &empty, sizeof (empty)) == 0)
8995 					goto done;
8996 
8997 				lpp->param_code[0] = 0;
8998 				lpp->param_code[1] = count;
8999 				lpp->param_ctrl_flags =
9000 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
9001 				lpp->param_len =
9002 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
9003 
9004 				status = entry->smart_selftest_log_status;
9005 				status >>= 4;
9006 				switch (status) {
9007 				case 0:
9008 				default:
9009 					sense_key = KEY_NO_SENSE;
9010 					add_sense_code =
9011 					    SD_SCSI_ASC_NO_ADD_SENSE;
9012 					break;
9013 				case 1:
9014 					sense_key = KEY_ABORTED_COMMAND;
9015 					add_sense_code =
9016 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
9017 					add_sense_code_qual = SCSI_COMPONENT_81;
9018 					break;
9019 				case 2:
9020 					sense_key = KEY_ABORTED_COMMAND;
9021 					add_sense_code =
9022 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
9023 					add_sense_code_qual = SCSI_COMPONENT_82;
9024 					break;
9025 				case 3:
9026 					sense_key = KEY_ABORTED_COMMAND;
9027 					add_sense_code =
9028 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
9029 					add_sense_code_qual = SCSI_COMPONENT_83;
9030 					break;
9031 				case 4:
9032 					sense_key = KEY_HARDWARE_ERROR;
9033 					add_sense_code =
9034 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
9035 					add_sense_code_qual = SCSI_COMPONENT_84;
9036 					break;
9037 				case 5:
9038 					sense_key = KEY_HARDWARE_ERROR;
9039 					add_sense_code =
9040 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
9041 					add_sense_code_qual = SCSI_COMPONENT_85;
9042 					break;
9043 				case 6:
9044 					sense_key = KEY_HARDWARE_ERROR;
9045 					add_sense_code =
9046 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
9047 					add_sense_code_qual = SCSI_COMPONENT_86;
9048 					break;
9049 				case 7:
9050 					sense_key = KEY_MEDIUM_ERROR;
9051 					add_sense_code =
9052 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
9053 					add_sense_code_qual = SCSI_COMPONENT_87;
9054 					break;
9055 				case 8:
9056 					sense_key = KEY_HARDWARE_ERROR;
9057 					add_sense_code =
9058 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
9059 					add_sense_code_qual = SCSI_COMPONENT_88;
9060 					break;
9061 				}
9062 				code = 0;	/* unspecified */
9063 				status |= (code << 4);
9064 				lpp->param_values[0] = status;
9065 				lpp->param_values[1] = 0; /* unspecified */
9066 				lpp->param_values[2] = entry->
9067 				    smart_selftest_log_timestamp[1];
9068 				lpp->param_values[3] = entry->
9069 				    smart_selftest_log_timestamp[0];
9070 				if (status != 0) {
9071 					lpp->param_values[4] = 0;
9072 					lpp->param_values[5] = 0;
9073 					lpp->param_values[6] = 0;
9074 					lpp->param_values[7] = 0;
9075 					lpp->param_values[8] = entry->
9076 					    smart_selftest_log_failing_lba[3];
9077 					lpp->param_values[9] = entry->
9078 					    smart_selftest_log_failing_lba[2];
9079 					lpp->param_values[10] = entry->
9080 					    smart_selftest_log_failing_lba[1];
9081 					lpp->param_values[11] = entry->
9082 					    smart_selftest_log_failing_lba[0];
9083 				} else {	/* No block address */
9084 					lpp->param_values[4] = 0xff;
9085 					lpp->param_values[5] = 0xff;
9086 					lpp->param_values[6] = 0xff;
9087 					lpp->param_values[7] = 0xff;
9088 					lpp->param_values[8] = 0xff;
9089 					lpp->param_values[9] = 0xff;
9090 					lpp->param_values[10] = 0xff;
9091 					lpp->param_values[11] = 0xff;
9092 				}
9093 				lpp->param_values[12] = sense_key;
9094 				lpp->param_values[13] = add_sense_code;
9095 				lpp->param_values[14] = add_sense_code_qual;
9096 				lpp->param_values[15] = 0; /* undefined */
9097 
9098 				lpp = (struct log_parameter *)
9099 				    (((uint8_t *)lpp) +
9100 				    SCSI_LOG_PARAM_HDR_LEN +
9101 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
9102 				--index;	/* back up to previous entry */
9103 				if (index < 0) {
9104 					index =
9105 					    NUM_SMART_SELFTEST_LOG_ENTRIES - 1;
9106 				}
9107 				entry = &selftest_log->
9108 				    smart_selftest_log_entries[index];
9109 			}
9110 		}
9111 done:
9112 		kmem_free(selftest_log, sizeof (struct smart_selftest_log));
9113 	}
9114 
9115 	return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) *
9116 	    SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS);
9117 }
9118 
9119 /*
9120  * sata_build_lsense_page_2f() is used to create the
9121  * SCSI LOG SENSE page 0x2f (informational exceptions)
9122  *
9123  * Takes a sata_drive_info t * and the address of a buffer
9124  * in which to create the page information as well as a sata_hba_inst_t *.
9125  *
9126  * Returns the number of bytes valid in the buffer.
9127  *
9128  * Because it invokes function(s) that send synchronously executed command
9129  * to the HBA, it cannot be called in the interrupt context.
9130  */
9131 static	int
9132 sata_build_lsense_page_2f(
9133 	sata_drive_info_t *sdinfo,
9134 	uint8_t *buf,
9135 	sata_hba_inst_t *sata_hba_inst)
9136 {
9137 	struct log_parameter *lpp = (struct log_parameter *)buf;
9138 	int rval;
9139 	uint8_t *smart_data;
9140 	uint8_t temp;
9141 	sata_id_t *sata_id;
9142 #define	SMART_NO_TEMP	0xff
9143 
9144 	lpp->param_code[0] = 0;
9145 	lpp->param_code[1] = 0;
9146 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
9147 
9148 	/* Now get the SMART status w.r.t. threshold exceeded */
9149 	rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo);
9150 	switch (rval) {
9151 	case 1:
9152 		lpp->param_values[0] = SCSI_PREDICTED_FAILURE;
9153 		lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE;
9154 		break;
9155 	case 0:
9156 	case -1:	/* failed to get data */
9157 		lpp->param_values[0] = 0;	/* No failure predicted */
9158 		lpp->param_values[1] = 0;
9159 		break;
9160 #if defined(SATA_DEBUG)
9161 	default:
9162 		cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value");
9163 		/* NOTREACHED */
9164 #endif
9165 	}
9166 
9167 	sata_id = &sdinfo->satadrv_id;
9168 	if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP))
9169 		temp = SMART_NO_TEMP;
9170 	else {
9171 		/* Now get the temperature */
9172 		smart_data = kmem_zalloc(512, KM_SLEEP);
9173 		rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data,
9174 		    SCT_STATUS_LOG_PAGE, 1);
9175 		if (rval == -1)
9176 			temp = SMART_NO_TEMP;
9177 		else {
9178 			temp = smart_data[200];
9179 			if (temp & 0x80) {
9180 				if (temp & 0x7f)
9181 					temp = 0;
9182 				else
9183 					temp = SMART_NO_TEMP;
9184 			}
9185 		}
9186 		kmem_free(smart_data, 512);
9187 	}
9188 
9189 	lpp->param_values[2] = temp;	/* most recent temperature */
9190 	lpp->param_values[3] = 0;	/* required vendor specific byte */
9191 
9192 	lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN;
9193 
9194 
9195 	return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN);
9196 }
9197 
9198 /*
9199  * sata_build_lsense_page_30() is used to create the
9200  * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data).
9201  *
9202  * Takes a sata_drive_info t * and the address of a buffer
9203  * in which to create the page information as well as a sata_hba_inst_t *.
9204  *
9205  * Returns the number of bytes valid in the buffer.
9206  */
9207 static int
9208 sata_build_lsense_page_30(
9209 	sata_drive_info_t *sdinfo,
9210 	uint8_t *buf,
9211 	sata_hba_inst_t *sata_hba_inst)
9212 {
9213 	struct smart_data *smart_data = (struct smart_data *)buf;
9214 	int rval;
9215 
9216 	/* Now do the SMART READ DATA */
9217 	rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data);
9218 	if (rval == -1)
9219 		return (0);
9220 
9221 	return (sizeof (struct smart_data));
9222 }
9223 
9224 /*
9225  * sata_build_lsense_page_0e() is used to create the
9226  * SCSI LOG SENSE page 0e (start-stop cycle counter page)
9227  *
9228  * Date of Manufacture (0x0001)
9229  *	YEAR = "0000"
9230  *	WEEK = "00"
9231  * Accounting Date (0x0002)
9232  *	6 ASCII space character(20h)
9233  * Specified cycle count over device lifetime
9234  *	VALUE - THRESH - the delta between max and min;
9235  * Accumulated start-stop cycles
9236  *	VALUE - WORST - the accumulated cycles;
9237  *
9238  * ID FLAG THRESH VALUE WORST RAW on start/stop counter attribute
9239  *
9240  * Takes a sata_drive_info t * and the address of a buffer
9241  * in which to create the page information as well as a sata_hba_inst_t *.
9242  *
9243  * Returns the number of bytes valid in the buffer.
9244  */
9245 static	int
9246 sata_build_lsense_page_0e(sata_drive_info_t *sdinfo, uint8_t *buf,
9247     sata_pkt_txlate_t *spx)
9248 {
9249 	struct start_stop_cycle_counter_log *log_page;
9250 	int i, rval, index;
9251 	uint8_t smart_data[512], id, value, worst, thresh;
9252 	uint32_t max_count, cycles;
9253 
9254 	/* Now do the SMART READ DATA */
9255 	rval = sata_fetch_smart_data(spx->txlt_sata_hba_inst, sdinfo,
9256 	    (struct smart_data *)smart_data);
9257 	if (rval == -1)
9258 		return (0);
9259 	for (i = 0, id = 0; i < SMART_START_STOP_COUNT_ID * 2; i++) {
9260 		index = (i * 12) + 2;
9261 		id = smart_data[index];
9262 		if (id != SMART_START_STOP_COUNT_ID)
9263 			continue;
9264 		else {
9265 			thresh = smart_data[index + 2];
9266 			value = smart_data[index + 3];
9267 			worst = smart_data[index + 4];
9268 			break;
9269 		}
9270 	}
9271 	if (id != SMART_START_STOP_COUNT_ID)
9272 		return (0);
9273 	max_count = value - thresh;
9274 	cycles = value - worst;
9275 
9276 	log_page = (struct start_stop_cycle_counter_log *)buf;
9277 	bzero(log_page, sizeof (struct start_stop_cycle_counter_log));
9278 	log_page->code = 0x0e;
9279 	log_page->page_len_low = 0x24;
9280 
9281 	log_page->manufactor_date_low = 0x1;
9282 	log_page->param_1.fmt_link = 0x1; /* 01b */
9283 	log_page->param_len_1 = 0x06;
9284 	for (i = 0; i < 4; i++) {
9285 		log_page->year_manu[i] = 0x30;
9286 		if (i < 2)
9287 			log_page->week_manu[i] = 0x30;
9288 	}
9289 
9290 	log_page->account_date_low = 0x02;
9291 	log_page->param_2.fmt_link = 0x01; /* 01b */
9292 	log_page->param_len_2 = 0x06;
9293 	for (i = 0; i < 4; i++) {
9294 		log_page->year_account[i] = 0x20;
9295 		if (i < 2)
9296 			log_page->week_account[i] = 0x20;
9297 	}
9298 
9299 	log_page->lifetime_code_low = 0x03;
9300 	log_page->param_3.fmt_link = 0x03; /* 11b */
9301 	log_page->param_len_3 = 0x04;
9302 	/* VALUE - THRESH - the delta between max and min */
9303 	log_page->cycle_code_low = 0x04;
9304 	log_page->param_4.fmt_link = 0x03; /* 11b */
9305 	log_page->param_len_4 = 0x04;
9306 	/* WORST - THRESH - the distance from 'now' to min */
9307 
9308 	for (i = 0; i < 4; i++) {
9309 		log_page->cycle_lifetime[i] =
9310 		    (max_count >> (8 * (3 - i))) & 0xff;
9311 		log_page->cycle_accumulated[i] =
9312 		    (cycles >> (8 * (3 - i))) & 0xff;
9313 	}
9314 
9315 	return (sizeof (struct start_stop_cycle_counter_log));
9316 }
9317 
9318 /*
9319  * This function was used for build a ATA read verify sector command
9320  */
9321 static void
9322 sata_build_read_verify_cmd(sata_cmd_t *scmd, uint16_t sec, uint64_t lba)
9323 {
9324 	scmd->satacmd_cmd_reg = SATAC_RDVER;
9325 	scmd->satacmd_addr_type = ATA_ADDR_LBA28;
9326 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
9327 
9328 	scmd->satacmd_sec_count_lsb = sec & 0xff;
9329 	scmd->satacmd_lba_low_lsb = lba & 0xff;
9330 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
9331 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
9332 	scmd->satacmd_device_reg = (SATA_ADH_LBA | (lba >> 24) & 0xf);
9333 	scmd->satacmd_features_reg = 0;
9334 	scmd->satacmd_status_reg = 0;
9335 	scmd->satacmd_error_reg = 0;
9336 }
9337 
9338 /*
9339  * This function was used for building an ATA
9340  * command, and only command register need to
9341  * be defined, other register will be zero or na.
9342  */
9343 static void
9344 sata_build_generic_cmd(sata_cmd_t *scmd, uint8_t cmd)
9345 {
9346 	scmd->satacmd_addr_type = 0;
9347 	scmd->satacmd_cmd_reg = cmd;
9348 	scmd->satacmd_device_reg = 0;
9349 	scmd->satacmd_sec_count_lsb = 0;
9350 	scmd->satacmd_lba_low_lsb = 0;
9351 	scmd->satacmd_lba_mid_lsb = 0;
9352 	scmd->satacmd_lba_high_lsb = 0;
9353 	scmd->satacmd_features_reg = 0;
9354 	scmd->satacmd_status_reg = 0;
9355 	scmd->satacmd_error_reg = 0;
9356 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
9357 }
9358 
9359 /*
9360  * This function was used for changing the standby
9361  * timer format from SCSI to ATA.
9362  */
9363 static uint8_t
9364 sata_get_standby_timer(uint8_t *timer)
9365 {
9366 	uint32_t i = 0, count = 0;
9367 	uint8_t ata_count;
9368 
9369 	for (i = 0; i < 4; i++) {
9370 		count = count << 8 | timer[i];
9371 	}
9372 
9373 	if (count == 0)
9374 		return (0);
9375 
9376 	if (count >= 1 && count <= 12000)
9377 		ata_count = (count -1) / 50 + 1;
9378 	else if (count > 12000 && count <= 12600)
9379 		ata_count = 0xfc;
9380 	else if (count > 12601 && count <= 12750)
9381 		ata_count = 0xff;
9382 	else if (count > 12750 && count <= 17999)
9383 		ata_count = 0xf1;
9384 	else if (count > 18000 && count <= 198000)
9385 		ata_count = count / 18000 + 240;
9386 	else
9387 		ata_count = 0xfd;
9388 	return (ata_count);
9389 }
9390 
9391 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */
9392 
9393 /*
9394  * Start command for ATAPI device.
9395  * This function processes scsi_pkt requests.
9396  * Now CD/DVD, tape and ATAPI disk devices are supported.
9397  * Most commands are packet without any translation into Packet Command.
9398  * Some may be trapped and executed as SATA commands (not clear which one).
9399  *
9400  * Returns TRAN_ACCEPT if command is accepted for execution (or completed
9401  * execution).
9402  * Returns other TRAN_XXXX codes if command is not accepted or completed
9403  * (see return values for sata_hba_start()).
9404  *
9405  * Note:
9406  * Inquiry cdb format differs between transport version 2 and 3.
9407  * However, the transport version 3 devices that were checked did not adhere
9408  * to the specification (ignored MSB of the allocation length). Therefore,
9409  * the transport version is not checked, but Inquiry allocation length is
9410  * truncated to 255 bytes if the original allocation length set-up by the
9411  * target driver is greater than 255 bytes.
9412  */
9413 static int
9414 sata_txlt_atapi(sata_pkt_txlate_t *spx)
9415 {
9416 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
9417 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
9418 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9419 	sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx);
9420 	sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba,
9421 	    &spx->txlt_sata_pkt->satapkt_device);
9422 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
9423 	int cdblen;
9424 	int rval, reason;
9425 	int synch;
9426 	union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp;
9427 
9428 	mutex_enter(cport_mutex);
9429 
9430 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
9431 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
9432 		mutex_exit(cport_mutex);
9433 		return (rval);
9434 	}
9435 
9436 	/*
9437 	 * ATAPI device executes some ATA commands in addition to those
9438 	 * commands sent via PACKET command. These ATA commands may be
9439 	 * executed by the regular SATA translation functions. None needs
9440 	 * to be captured now.
9441 	 *
9442 	 * Commands sent via PACKET command include:
9443 	 *	MMC command set for ATAPI CD/DVD device
9444 	 *	SSC command set for ATAPI TAPE device
9445 	 *	SBC command set for ATAPI disk device
9446 	 *
9447 	 */
9448 
9449 	/* Check the size of cdb */
9450 
9451 	switch (GETGROUP(cdbp)) {
9452 	case CDB_GROUPID_3:   /* Reserved, per SPC-4 */
9453 		/*
9454 		 * opcodes 0x7e and 0x7f identify variable-length CDBs and
9455 		 * therefore require special handling.  Return failure, for now.
9456 		 */
9457 		mutex_exit(cport_mutex);
9458 		return (TRAN_BADPKT);
9459 
9460 	case CDB_GROUPID_6:   /* Vendor-specific, per SPC-4 */
9461 	case CDB_GROUPID_7:   /* Vendor-specific, per SPC-4 */
9462 		/* obtain length from the scsi_pkt */
9463 		cdblen = scsipkt->pkt_cdblen;
9464 		break;
9465 
9466 	default:
9467 		/* CDB's length is statically known, per SPC-4 */
9468 		cdblen = scsi_cdb_size[GETGROUP(cdbp)];
9469 		break;
9470 	}
9471 
9472 	if (cdblen <= 0 || cdblen > sdinfo->satadrv_atapi_cdb_len) {
9473 		sata_log(NULL, CE_WARN,
9474 		    "sata: invalid ATAPI cdb length %d",
9475 		    cdblen);
9476 		mutex_exit(cport_mutex);
9477 		return (TRAN_BADPKT);
9478 	}
9479 
9480 	SATAATAPITRACE(spx, cdblen);
9481 
9482 	/*
9483 	 * For non-read/write commands we need to
9484 	 * map buffer
9485 	 */
9486 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
9487 	case SCMD_READ:
9488 	case SCMD_READ_G1:
9489 	case SCMD_READ_G5:
9490 	case SCMD_READ_G4:
9491 	case SCMD_WRITE:
9492 	case SCMD_WRITE_G1:
9493 	case SCMD_WRITE_G5:
9494 	case SCMD_WRITE_G4:
9495 		break;
9496 	default:
9497 		if (bp != NULL) {
9498 			if (bp->b_flags & (B_PHYS | B_PAGEIO))
9499 				bp_mapin(bp);
9500 		}
9501 		break;
9502 	}
9503 	/*
9504 	 * scmd->satacmd_flags.sata_data_direction default -
9505 	 * SATA_DIR_NODATA_XFER - is set by
9506 	 * sata_txlt_generic_pkt_info().
9507 	 */
9508 	if (scmd->satacmd_bp) {
9509 		if (scmd->satacmd_bp->b_flags & B_READ) {
9510 			scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9511 		} else {
9512 			scmd->satacmd_flags.sata_data_direction =
9513 			    SATA_DIR_WRITE;
9514 		}
9515 	}
9516 
9517 	/*
9518 	 * Set up ATAPI packet command.
9519 	 */
9520 
9521 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
9522 
9523 	/* Copy cdb into sata_cmd */
9524 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9525 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
9526 	bcopy(cdbp, scmd->satacmd_acdb, cdblen);
9527 
9528 	/* See note in the command header */
9529 	if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) {
9530 		if (scmd->satacmd_acdb[3] != 0)
9531 			scmd->satacmd_acdb[4] = 255;
9532 	}
9533 
9534 #ifdef SATA_DEBUG
9535 	if (sata_debug_flags & SATA_DBG_ATAPI) {
9536 		uint8_t *p = scmd->satacmd_acdb;
9537 		char buf[3 * SATA_ATAPI_MAX_CDB_LEN];
9538 
9539 		(void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN,
9540 		    "%02x %02x %02x %02x %02x %02x %02x %02x "
9541 		    "%2x %02x %02x %02x %02x %02x %02x %02x",
9542 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9543 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9544 		buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0';
9545 		cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf);
9546 	}
9547 #endif
9548 
9549 	/*
9550 	 * Preset request sense data to NO SENSE.
9551 	 * If there is no way to get error information via Request Sense,
9552 	 * the packet request sense data would not have to be modified by HBA,
9553 	 * but it could be returned as is.
9554 	 */
9555 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
9556 	sata_fixed_sense_data_preset(
9557 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
9558 
9559 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
9560 		/* Need callback function */
9561 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion;
9562 		synch = FALSE;
9563 	} else
9564 		synch = TRUE;
9565 
9566 	/* Transfer command to HBA */
9567 	if (sata_hba_start(spx, &rval) != 0) {
9568 		/* Pkt not accepted for execution */
9569 		mutex_exit(cport_mutex);
9570 		return (rval);
9571 	}
9572 	mutex_exit(cport_mutex);
9573 	/*
9574 	 * If execution is non-synchronous,
9575 	 * a callback function will handle potential errors, translate
9576 	 * the response and will do a callback to a target driver.
9577 	 * If it was synchronous, use the same framework callback to check
9578 	 * an execution status.
9579 	 */
9580 	if (synch) {
9581 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
9582 		    "synchronous execution status %x\n",
9583 		    spx->txlt_sata_pkt->satapkt_reason);
9584 		sata_txlt_atapi_completion(spx->txlt_sata_pkt);
9585 	}
9586 	return (TRAN_ACCEPT);
9587 }
9588 
9589 
9590 /*
9591  * ATAPI Packet command completion.
9592  *
9593  * Failure of the command passed via Packet command are considered device
9594  * error. SATA HBA driver would have to retrieve error data (via Request
9595  * Sense command delivered via error retrieval sata packet) and copy it
9596  * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data.
9597  */
9598 static void
9599 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt)
9600 {
9601 	sata_pkt_txlate_t *spx =
9602 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
9603 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
9604 	struct scsi_extended_sense *sense;
9605 	struct buf *bp;
9606 	int rval;
9607 
9608 #ifdef SATA_DEBUG
9609 	uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense;
9610 #endif
9611 
9612 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
9613 	    STATE_SENT_CMD | STATE_GOT_STATUS;
9614 
9615 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
9616 		/* Normal completion */
9617 		if (sata_pkt->satapkt_cmd.satacmd_bp != NULL)
9618 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
9619 		scsipkt->pkt_reason = CMD_CMPLT;
9620 		*scsipkt->pkt_scbp = STATUS_GOOD;
9621 		if (spx->txlt_tmp_buf != NULL) {
9622 			/* Temporary buffer was used */
9623 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9624 			if (bp->b_flags & B_READ) {
9625 				rval = ddi_dma_sync(
9626 				    spx->txlt_buf_dma_handle, 0, 0,
9627 				    DDI_DMA_SYNC_FORCPU);
9628 				ASSERT(rval == DDI_SUCCESS);
9629 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
9630 				    bp->b_bcount);
9631 			}
9632 		}
9633 	} else {
9634 		/*
9635 		 * Something went wrong - analyze return
9636 		 */
9637 		*scsipkt->pkt_scbp = STATUS_CHECK;
9638 		sense = sata_arq_sense(spx);
9639 
9640 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
9641 			/*
9642 			 * pkt_reason should be CMD_CMPLT for DEVICE ERROR.
9643 			 * Under this condition ERR bit is set for ATA command,
9644 			 * and CHK bit set for ATAPI command.
9645 			 *
9646 			 * Please check st_intr & sdintr about how pkt_reason
9647 			 * is used.
9648 			 */
9649 			scsipkt->pkt_reason = CMD_CMPLT;
9650 
9651 			/*
9652 			 * We may not have ARQ data if there was a double
9653 			 * error. But sense data in sata packet was pre-set
9654 			 * with NO SENSE so it is valid even if HBA could
9655 			 * not retrieve a real sense data.
9656 			 * Just copy this sense data into scsi pkt sense area.
9657 			 */
9658 			bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense,
9659 			    SATA_ATAPI_MIN_RQSENSE_LEN);
9660 #ifdef SATA_DEBUG
9661 			if (sata_debug_flags & SATA_DBG_SCSI_IF) {
9662 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
9663 				    "sata_txlt_atapi_completion: %02x\n"
9664 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
9665 				    "          %02x %02x %02x %02x %02x %02x "
9666 				    "          %02x %02x %02x %02x %02x %02x\n",
9667 				    scsipkt->pkt_reason,
9668 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
9669 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
9670 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
9671 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
9672 				    rqsp[16], rqsp[17]);
9673 			}
9674 #endif
9675 		} else {
9676 			switch (sata_pkt->satapkt_reason) {
9677 			case SATA_PKT_PORT_ERROR:
9678 				/*
9679 				 * We have no device data.
9680 				 */
9681 				scsipkt->pkt_reason = CMD_INCOMPLETE;
9682 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
9683 				    STATE_GOT_TARGET | STATE_SENT_CMD |
9684 				    STATE_GOT_STATUS);
9685 				sense->es_key = KEY_HARDWARE_ERROR;
9686 				break;
9687 
9688 			case SATA_PKT_TIMEOUT:
9689 				scsipkt->pkt_reason = CMD_TIMEOUT;
9690 				scsipkt->pkt_statistics |=
9691 				    STAT_TIMEOUT | STAT_DEV_RESET;
9692 				/*
9693 				 * Need to check if HARDWARE_ERROR/
9694 				 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more
9695 				 * appropriate.
9696 				 */
9697 				break;
9698 
9699 			case SATA_PKT_ABORTED:
9700 				scsipkt->pkt_reason = CMD_ABORTED;
9701 				scsipkt->pkt_statistics |= STAT_ABORTED;
9702 				/* Should we set key COMMAND_ABPRTED? */
9703 				break;
9704 
9705 			case SATA_PKT_RESET:
9706 				scsipkt->pkt_reason = CMD_RESET;
9707 				scsipkt->pkt_statistics |= STAT_DEV_RESET;
9708 				/*
9709 				 * May be we should set Unit Attention /
9710 				 * Reset. Perhaps the same should be
9711 				 * returned for disks....
9712 				 */
9713 				sense->es_key = KEY_UNIT_ATTENTION;
9714 				sense->es_add_code = SD_SCSI_ASC_RESET;
9715 				break;
9716 
9717 			default:
9718 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9719 				    "sata_txlt_atapi_completion: "
9720 				    "invalid packet completion reason"));
9721 				scsipkt->pkt_reason = CMD_TRAN_ERR;
9722 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
9723 				    STATE_GOT_TARGET | STATE_SENT_CMD |
9724 				    STATE_GOT_STATUS);
9725 				break;
9726 			}
9727 		}
9728 	}
9729 
9730 	SATAATAPITRACE(spx, 0);
9731 
9732 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
9733 	    scsipkt->pkt_comp != NULL) {
9734 		/* scsi callback required */
9735 		(*scsipkt->pkt_comp)(scsipkt);
9736 	}
9737 }
9738 
9739 /*
9740  * Set up error retrieval sata command for ATAPI Packet Command error data
9741  * recovery.
9742  *
9743  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
9744  * returns SATA_FAILURE otherwise.
9745  */
9746 
9747 static int
9748 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
9749 {
9750 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
9751 	sata_cmd_t *scmd;
9752 	struct buf *bp;
9753 
9754 	/*
9755 	 * Allocate dma-able buffer error data.
9756 	 * Buffer allocation will take care of buffer alignment and other DMA
9757 	 * attributes.
9758 	 */
9759 	bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN);
9760 	if (bp == NULL) {
9761 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
9762 		    "sata_get_err_retrieval_pkt: "
9763 		    "cannot allocate buffer for error data", NULL);
9764 		return (SATA_FAILURE);
9765 	}
9766 	bp_mapin(bp); /* make data buffer accessible */
9767 
9768 	/* Operation modes are up to the caller */
9769 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
9770 
9771 	/* Synchronous mode, no callback - may be changed by the caller */
9772 	spkt->satapkt_comp = NULL;
9773 	spkt->satapkt_time = sata_default_pkt_time;
9774 
9775 	scmd = &spkt->satapkt_cmd;
9776 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9777 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
9778 
9779 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
9780 
9781 	/*
9782 	 * Set-up acdb. Request Sense CDB (packet command content) is
9783 	 * not in DMA-able buffer. Its handling is HBA-specific (how
9784 	 * it is transfered into packet FIS).
9785 	 */
9786 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9787 	bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN);
9788 	/* Following zeroing of pad bytes may not be necessary */
9789 	bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN],
9790 	    sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN);
9791 
9792 	/*
9793 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
9794 	 * before accessing it. Handle is in usual place in translate struct.
9795 	 */
9796 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
9797 
9798 	/*
9799 	 * Preset request sense data to NO SENSE.
9800 	 * Here it is redundant, only for a symetry with scsi-originated
9801 	 * packets. It should not be used for anything but debugging.
9802 	 */
9803 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
9804 	sata_fixed_sense_data_preset(
9805 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
9806 
9807 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
9808 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
9809 
9810 	return (SATA_SUCCESS);
9811 }
9812 
9813 /*
9814  * Set-up ATAPI packet command.
9815  * Data transfer direction has to be set-up in sata_cmd structure prior to
9816  * calling this function.
9817  *
9818  * Returns void
9819  */
9820 
9821 static void
9822 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo)
9823 {
9824 	scmd->satacmd_addr_type = 0;		/* N/A */
9825 	scmd->satacmd_sec_count_lsb = 0;	/* no tag */
9826 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
9827 	scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ;
9828 	scmd->satacmd_lba_high_lsb =
9829 	    (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8);
9830 	scmd->satacmd_cmd_reg = SATAC_PACKET;	/* Command */
9831 
9832 	/*
9833 	 * We want all data to be transfered via DMA.
9834 	 * But specify it only if drive supports DMA and DMA mode is
9835 	 * selected - some drives are sensitive about it.
9836 	 * Hopefully it wil work for all drives....
9837 	 */
9838 	if (sdinfo->satadrv_settings & SATA_DEV_DMA)
9839 		scmd->satacmd_features_reg = SATA_ATAPI_F_DMA;
9840 
9841 	/*
9842 	 * Features register requires special care for devices that use
9843 	 * Serial ATA bridge - they need an explicit specification of
9844 	 * the data transfer direction for Packet DMA commands.
9845 	 * Setting this bit is harmless if DMA is not used.
9846 	 *
9847 	 * Many drives do not implement word 80, specifying what ATA/ATAPI
9848 	 * spec they follow.
9849 	 * We are arbitrarily following the latest SerialATA 2.6 spec,
9850 	 * which uses ATA/ATAPI 6 specification for Identify Data, unless
9851 	 * ATA/ATAPI-7 support is explicitly indicated.
9852 	 */
9853 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
9854 	    sdinfo->satadrv_id.ai_majorversion != 0xffff &&
9855 	    (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) {
9856 		/*
9857 		 * Specification of major version is valid and version 7
9858 		 * is supported. It does automatically imply that all
9859 		 * spec features are supported. For now, we assume that
9860 		 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete.
9861 		 */
9862 		if ((sdinfo->satadrv_id.ai_dirdma &
9863 		    SATA_ATAPI_ID_DMADIR_REQ) != 0) {
9864 			if (scmd->satacmd_flags.sata_data_direction ==
9865 			    SATA_DIR_READ)
9866 			scmd->satacmd_features_reg |=
9867 			    SATA_ATAPI_F_DATA_DIR_READ;
9868 		}
9869 	}
9870 }
9871 
9872 
9873 #ifdef SATA_DEBUG
9874 
9875 /* Display 18 bytes of Inquiry data */
9876 static void
9877 sata_show_inqry_data(uint8_t *buf)
9878 {
9879 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
9880 	uint8_t *p;
9881 
9882 	cmn_err(CE_NOTE, "Inquiry data:");
9883 	cmn_err(CE_NOTE, "device type %x", inq->inq_dtype);
9884 	cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb);
9885 	cmn_err(CE_NOTE, "version %x", inq->inq_ansi);
9886 	cmn_err(CE_NOTE, "ATAPI transport version %d",
9887 	    SATA_ATAPI_TRANS_VERSION(inq));
9888 	cmn_err(CE_NOTE, "response data format %d, aenc %d",
9889 	    inq->inq_rdf, inq->inq_aenc);
9890 	cmn_err(CE_NOTE, " additional length %d", inq->inq_len);
9891 	cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs);
9892 	p = (uint8_t *)inq->inq_vid;
9893 	cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x "
9894 	    "%02x %02x %02x %02x",
9895 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
9896 	p = (uint8_t *)inq->inq_vid;
9897 	cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c",
9898 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
9899 
9900 	p = (uint8_t *)inq->inq_pid;
9901 	cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x "
9902 	    "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
9903 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9904 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9905 	p = (uint8_t *)inq->inq_pid;
9906 	cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c "
9907 	    "%c %c %c %c %c %c %c %c",
9908 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9909 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9910 
9911 	p = (uint8_t *)inq->inq_revision;
9912 	cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x",
9913 	    p[0], p[1], p[2], p[3]);
9914 	p = (uint8_t *)inq->inq_revision;
9915 	cmn_err(CE_NOTE, "revision: %c %c %c %c",
9916 	    p[0], p[1], p[2], p[3]);
9917 
9918 }
9919 
9920 
9921 static void
9922 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count)
9923 {
9924 	struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt;
9925 
9926 	if (scsi_pkt == NULL)
9927 		return;
9928 	if (count != 0) {
9929 		/* saving cdb */
9930 		bzero(sata_atapi_trace[sata_atapi_trace_index].acdb,
9931 		    SATA_ATAPI_MAX_CDB_LEN);
9932 		bcopy(scsi_pkt->pkt_cdbp,
9933 		    sata_atapi_trace[sata_atapi_trace_index].acdb, count);
9934 	} else {
9935 		bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)->
9936 		    sts_sensedata,
9937 		    sata_atapi_trace[sata_atapi_trace_index].arqs,
9938 		    SATA_ATAPI_MIN_RQSENSE_LEN);
9939 		sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason =
9940 		    scsi_pkt->pkt_reason;
9941 		sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason =
9942 		    spx->txlt_sata_pkt->satapkt_reason;
9943 
9944 		if (++sata_atapi_trace_index >= 64)
9945 			sata_atapi_trace_index = 0;
9946 	}
9947 }
9948 
9949 #endif
9950 
9951 /*
9952  * Fetch inquiry data from ATAPI device
9953  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
9954  *
9955  * Note:
9956  * inqb pointer does not point to a DMA-able buffer. It is a local buffer
9957  * where the caller expects to see the inquiry data.
9958  *
9959  */
9960 
9961 static int
9962 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba,
9963     sata_address_t *saddr, struct scsi_inquiry *inq)
9964 {
9965 	sata_pkt_txlate_t *spx;
9966 	sata_pkt_t *spkt;
9967 	struct buf *bp;
9968 	sata_drive_info_t *sdinfo;
9969 	sata_cmd_t *scmd;
9970 	int rval;
9971 	uint8_t *rqsp;
9972 	dev_info_t *dip = SATA_DIP(sata_hba);
9973 #ifdef SATA_DEBUG
9974 	char msg_buf[MAXPATHLEN];
9975 #endif
9976 	kmutex_t *cport_mutex;
9977 
9978 	ASSERT(sata_hba != NULL);
9979 
9980 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
9981 	spx->txlt_sata_hba_inst = sata_hba;
9982 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
9983 	spkt = sata_pkt_alloc(spx, NULL);
9984 	if (spkt == NULL) {
9985 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
9986 		return (SATA_FAILURE);
9987 	}
9988 	/* address is needed now */
9989 	spkt->satapkt_device.satadev_addr = *saddr;
9990 
9991 	/* scsi_inquiry size buffer */
9992 	bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry));
9993 	if (bp == NULL) {
9994 		sata_pkt_free(spx);
9995 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
9996 		SATA_LOG_D((sata_hba, CE_WARN,
9997 		    "sata_get_atapi_inquiry_data: "
9998 		    "cannot allocate data buffer"));
9999 		return (SATA_FAILURE);
10000 	}
10001 	bp_mapin(bp); /* make data buffer accessible */
10002 
10003 	scmd = &spkt->satapkt_cmd;
10004 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
10005 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
10006 
10007 	/* Use synchronous mode */
10008 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10009 	spkt->satapkt_comp = NULL;
10010 	spkt->satapkt_time = sata_default_pkt_time;
10011 
10012 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
10013 
10014 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
10015 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10016 
10017 	cport_mutex = &(SATA_CPORT_MUTEX(sata_hba, saddr->cport));
10018 	mutex_enter(cport_mutex);
10019 	sdinfo = sata_get_device_info(sata_hba,
10020 	    &spx->txlt_sata_pkt->satapkt_device);
10021 	if (sdinfo == NULL) {
10022 		/* we have to be carefull about the disapearing device */
10023 		mutex_exit(cport_mutex);
10024 		rval = SATA_FAILURE;
10025 		goto cleanup;
10026 	}
10027 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
10028 
10029 	/*
10030 	 * Set-up acdb. This works for atapi transport version 2 and later.
10031 	 */
10032 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
10033 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
10034 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
10035 	scmd->satacmd_acdb[1] = 0x00;
10036 	scmd->satacmd_acdb[2] = 0x00;
10037 	scmd->satacmd_acdb[3] = 0x00;
10038 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
10039 	scmd->satacmd_acdb[5] = 0x00;
10040 
10041 	sata_fixed_sense_data_preset(
10042 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
10043 
10044 	/* Transfer command to HBA */
10045 	if (sata_hba_start(spx, &rval) != 0) {
10046 		/* Pkt not accepted for execution */
10047 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
10048 		    "sata_get_atapi_inquiry_data: "
10049 		    "Packet not accepted for execution - ret: %02x", rval);
10050 		mutex_exit(cport_mutex);
10051 		rval = SATA_FAILURE;
10052 		goto cleanup;
10053 	}
10054 	mutex_exit(cport_mutex);
10055 
10056 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10057 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
10058 		    "sata_get_atapi_inquiry_data: "
10059 		    "Packet completed successfully - ret: %02x", rval);
10060 		if (spx->txlt_buf_dma_handle != NULL) {
10061 			/*
10062 			 * Sync buffer. Handle is in usual place in translate
10063 			 * struct.
10064 			 */
10065 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10066 			    DDI_DMA_SYNC_FORCPU);
10067 			ASSERT(rval == DDI_SUCCESS);
10068 		}
10069 
10070 		if (sata_check_for_dma_error(dip, spx)) {
10071 			ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED);
10072 			rval = SATA_FAILURE;
10073 		} else {
10074 			/*
10075 			 * Normal completion - copy data into caller's buffer
10076 			 */
10077 			bcopy(bp->b_un.b_addr, (uint8_t *)inq,
10078 			    sizeof (struct scsi_inquiry));
10079 #ifdef SATA_DEBUG
10080 			if (sata_debug_flags & SATA_DBG_ATAPI) {
10081 				sata_show_inqry_data((uint8_t *)inq);
10082 			}
10083 #endif
10084 			rval = SATA_SUCCESS;
10085 		}
10086 	} else {
10087 		/*
10088 		 * Something went wrong - analyze return - check rqsense data
10089 		 */
10090 		rval = SATA_FAILURE;
10091 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
10092 			/*
10093 			 * ARQ data hopefull show something other than NO SENSE
10094 			 */
10095 			rqsp = scmd->satacmd_rqsense;
10096 #ifdef SATA_DEBUG
10097 			if (sata_debug_flags & SATA_DBG_ATAPI) {
10098 				msg_buf[0] = '\0';
10099 				(void) snprintf(msg_buf, MAXPATHLEN,
10100 				    "ATAPI packet completion reason: %02x\n"
10101 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x\n"
10102 				    "          %02x %02x %02x %02x %02x %02x\n"
10103 				    "          %02x %02x %02x %02x %02x %02x",
10104 				    spkt->satapkt_reason,
10105 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
10106 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
10107 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
10108 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
10109 				    rqsp[16], rqsp[17]);
10110 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
10111 				    "%s", msg_buf);
10112 			}
10113 #endif
10114 		} else {
10115 			switch (spkt->satapkt_reason) {
10116 			case SATA_PKT_PORT_ERROR:
10117 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10118 				    "sata_get_atapi_inquiry_data: "
10119 				    "packet reason: port error", NULL);
10120 				break;
10121 
10122 			case SATA_PKT_TIMEOUT:
10123 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10124 				    "sata_get_atapi_inquiry_data: "
10125 				    "packet reason: timeout", NULL);
10126 				break;
10127 
10128 			case SATA_PKT_ABORTED:
10129 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10130 				    "sata_get_atapi_inquiry_data: "
10131 				    "packet reason: aborted", NULL);
10132 				break;
10133 
10134 			case SATA_PKT_RESET:
10135 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10136 				    "sata_get_atapi_inquiry_data: "
10137 				    "packet reason: reset\n", NULL);
10138 				break;
10139 			default:
10140 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10141 				    "sata_get_atapi_inquiry_data: "
10142 				    "invalid packet reason: %02x\n",
10143 				    spkt->satapkt_reason);
10144 				break;
10145 			}
10146 		}
10147 	}
10148 cleanup:
10149 	sata_free_local_buffer(spx);
10150 	sata_pkt_free(spx);
10151 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
10152 	return (rval);
10153 }
10154 
10155 
10156 
10157 
10158 
10159 #if 0
10160 #ifdef SATA_DEBUG
10161 
10162 /*
10163  * Test ATAPI packet command.
10164  * Single threaded test: send packet command in synch mode, process completion
10165  *
10166  */
10167 static void
10168 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport)
10169 {
10170 	sata_pkt_txlate_t *spx;
10171 	sata_pkt_t *spkt;
10172 	struct buf *bp;
10173 	sata_device_t sata_device;
10174 	sata_drive_info_t *sdinfo;
10175 	sata_cmd_t *scmd;
10176 	int rval;
10177 	uint8_t *rqsp;
10178 
10179 	ASSERT(sata_hba_inst != NULL);
10180 	sata_device.satadev_addr.cport = cport;
10181 	sata_device.satadev_addr.pmport = 0;
10182 	sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
10183 	sata_device.satadev_rev = SATA_DEVICE_REV;
10184 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10185 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
10186 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10187 	if (sdinfo == NULL) {
10188 		sata_log(sata_hba_inst, CE_WARN,
10189 		    "sata_test_atapi_packet_command: "
10190 		    "no device info for cport %d",
10191 		    sata_device.satadev_addr.cport);
10192 		return;
10193 	}
10194 
10195 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10196 	spx->txlt_sata_hba_inst = sata_hba_inst;
10197 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
10198 	spkt = sata_pkt_alloc(spx, NULL);
10199 	if (spkt == NULL) {
10200 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10201 		return;
10202 	}
10203 	/* address is needed now */
10204 	spkt->satapkt_device.satadev_addr = sata_device.satadev_addr;
10205 
10206 	/* 1024k buffer */
10207 	bp = sata_alloc_local_buffer(spx, 1024);
10208 	if (bp == NULL) {
10209 		sata_pkt_free(spx);
10210 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10211 		sata_log(sata_hba_inst, CE_WARN,
10212 		    "sata_test_atapi_packet_command: "
10213 		    "cannot allocate data buffer");
10214 		return;
10215 	}
10216 	bp_mapin(bp); /* make data buffer accessible */
10217 
10218 	scmd = &spkt->satapkt_cmd;
10219 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
10220 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
10221 
10222 	/* Use synchronous mode */
10223 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10224 
10225 	/* Synchronous mode, no callback - may be changed by the caller */
10226 	spkt->satapkt_comp = NULL;
10227 	spkt->satapkt_time = sata_default_pkt_time;
10228 
10229 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
10230 
10231 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
10232 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10233 
10234 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
10235 
10236 	/* Set-up acdb. */
10237 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
10238 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
10239 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
10240 	scmd->satacmd_acdb[1] = 0x00;
10241 	scmd->satacmd_acdb[2] = 0x00;
10242 	scmd->satacmd_acdb[3] = 0x00;
10243 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
10244 	scmd->satacmd_acdb[5] = 0x00;
10245 
10246 	sata_fixed_sense_data_preset(
10247 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
10248 
10249 	/* Transfer command to HBA */
10250 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10251 	if (sata_hba_start(spx, &rval) != 0) {
10252 		/* Pkt not accepted for execution */
10253 		sata_log(sata_hba_inst, CE_WARN,
10254 		    "sata_test_atapi_packet_command: "
10255 		    "Packet not accepted for execution - ret: %02x", rval);
10256 		mutex_exit(
10257 		    &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10258 		goto cleanup;
10259 	}
10260 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10261 
10262 	if (spx->txlt_buf_dma_handle != NULL) {
10263 		/*
10264 		 * Sync buffer. Handle is in usual place in translate struct.
10265 		 */
10266 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10267 		    DDI_DMA_SYNC_FORCPU);
10268 		ASSERT(rval == DDI_SUCCESS);
10269 	}
10270 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10271 		sata_log(sata_hba_inst, CE_WARN,
10272 		    "sata_test_atapi_packet_command: "
10273 		    "Packet completed successfully");
10274 		/*
10275 		 * Normal completion - show inquiry data
10276 		 */
10277 		sata_show_inqry_data((uint8_t *)bp->b_un.b_addr);
10278 	} else {
10279 		/*
10280 		 * Something went wrong - analyze return - check rqsense data
10281 		 */
10282 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
10283 			/*
10284 			 * ARQ data hopefull show something other than NO SENSE
10285 			 */
10286 			rqsp = scmd->satacmd_rqsense;
10287 			sata_log(spx->txlt_sata_hba_inst, CE_WARN,
10288 			    "ATAPI packet completion reason: %02x\n"
10289 			    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
10290 			    "          %02x %02x %02x %02x %02x %02x "
10291 			    "          %02x %02x %02x %02x %02x %02x\n",
10292 			    spkt->satapkt_reason,
10293 			    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
10294 			    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
10295 			    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
10296 			    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
10297 			    rqsp[16], rqsp[17]);
10298 		} else {
10299 			switch (spkt->satapkt_reason) {
10300 			case SATA_PKT_PORT_ERROR:
10301 				sata_log(sata_hba_inst, CE_WARN,
10302 				    "sata_test_atapi_packet_command: "
10303 				    "packet reason: port error\n");
10304 				break;
10305 
10306 			case SATA_PKT_TIMEOUT:
10307 				sata_log(sata_hba_inst, CE_WARN,
10308 				    "sata_test_atapi_packet_command: "
10309 				    "packet reason: timeout\n");
10310 				break;
10311 
10312 			case SATA_PKT_ABORTED:
10313 				sata_log(sata_hba_inst, CE_WARN,
10314 				    "sata_test_atapi_packet_command: "
10315 				    "packet reason: aborted\n");
10316 				break;
10317 
10318 			case SATA_PKT_RESET:
10319 				sata_log(sata_hba_inst, CE_WARN,
10320 				    "sata_test_atapi_packet_command: "
10321 				    "packet reason: reset\n");
10322 				break;
10323 			default:
10324 				sata_log(sata_hba_inst, CE_WARN,
10325 				    "sata_test_atapi_packet_command: "
10326 				    "invalid packet reason: %02x\n",
10327 				    spkt->satapkt_reason);
10328 				break;
10329 			}
10330 		}
10331 	}
10332 cleanup:
10333 	sata_free_local_buffer(spx);
10334 	sata_pkt_free(spx);
10335 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
10336 }
10337 
10338 #endif /* SATA_DEBUG */
10339 #endif /* 1 */
10340 
10341 
10342 /* ************************** LOCAL HELPER FUNCTIONS *********************** */
10343 
10344 /*
10345  * Validate sata_tran info
10346  * SATA_FAILURE returns if structure is inconsistent or structure revision
10347  * does not match one used by the framework.
10348  *
10349  * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains
10350  * required function pointers.
10351  * Returns SATA_FAILURE otherwise.
10352  */
10353 static int
10354 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran)
10355 {
10356 	/*
10357 	 * SATA_TRAN_HBA_REV is the current (highest) revision number
10358 	 * of the SATA interface.
10359 	 */
10360 	if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) {
10361 		sata_log(NULL, CE_WARN,
10362 		    "sata: invalid sata_hba_tran version %d for driver %s",
10363 		    sata_tran->sata_tran_hba_rev, ddi_driver_name(dip));
10364 		return (SATA_FAILURE);
10365 	}
10366 
10367 	if (dip != sata_tran->sata_tran_hba_dip) {
10368 		SATA_LOG_D((NULL, CE_WARN,
10369 		    "sata: inconsistent sata_tran_hba_dip "
10370 		    "%p / %p", sata_tran->sata_tran_hba_dip, dip));
10371 		return (SATA_FAILURE);
10372 	}
10373 
10374 	if (sata_tran->sata_tran_probe_port == NULL ||
10375 	    sata_tran->sata_tran_start == NULL ||
10376 	    sata_tran->sata_tran_abort == NULL ||
10377 	    sata_tran->sata_tran_reset_dport == NULL ||
10378 	    sata_tran->sata_tran_hotplug_ops == NULL ||
10379 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL ||
10380 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate ==
10381 	    NULL) {
10382 		SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing "
10383 		    "required functions"));
10384 	}
10385 	return (SATA_SUCCESS);
10386 }
10387 
10388 /*
10389  * Remove HBA instance from sata_hba_list.
10390  */
10391 static void
10392 sata_remove_hba_instance(dev_info_t *dip)
10393 {
10394 	sata_hba_inst_t	*sata_hba_inst;
10395 
10396 	mutex_enter(&sata_mutex);
10397 	for (sata_hba_inst = sata_hba_list;
10398 	    sata_hba_inst != (struct sata_hba_inst *)NULL;
10399 	    sata_hba_inst = sata_hba_inst->satahba_next) {
10400 		if (sata_hba_inst->satahba_dip == dip)
10401 			break;
10402 	}
10403 
10404 	if (sata_hba_inst == (struct sata_hba_inst *)NULL) {
10405 #ifdef SATA_DEBUG
10406 		cmn_err(CE_WARN, "sata_remove_hba_instance: "
10407 		    "unknown HBA instance\n");
10408 #endif
10409 		ASSERT(FALSE);
10410 	}
10411 	if (sata_hba_inst == sata_hba_list) {
10412 		sata_hba_list = sata_hba_inst->satahba_next;
10413 		if (sata_hba_list) {
10414 			sata_hba_list->satahba_prev =
10415 			    (struct sata_hba_inst *)NULL;
10416 		}
10417 		if (sata_hba_inst == sata_hba_list_tail) {
10418 			sata_hba_list_tail = NULL;
10419 		}
10420 	} else if (sata_hba_inst == sata_hba_list_tail) {
10421 		sata_hba_list_tail = sata_hba_inst->satahba_prev;
10422 		if (sata_hba_list_tail) {
10423 			sata_hba_list_tail->satahba_next =
10424 			    (struct sata_hba_inst *)NULL;
10425 		}
10426 	} else {
10427 		sata_hba_inst->satahba_prev->satahba_next =
10428 		    sata_hba_inst->satahba_next;
10429 		sata_hba_inst->satahba_next->satahba_prev =
10430 		    sata_hba_inst->satahba_prev;
10431 	}
10432 	mutex_exit(&sata_mutex);
10433 }
10434 
10435 /*
10436  * Probe all SATA ports of the specified HBA instance.
10437  * The assumption is that there are no target and attachment point minor nodes
10438  * created by the boot subsystems, so we do not need to prune device tree.
10439  *
10440  * This function is called only from sata_hba_attach(). It does not have to
10441  * be protected by controller mutex, because the hba_attached flag is not set
10442  * yet and no one would be touching this HBA instance other than this thread.
10443  * Determines if port is active and what type of the device is attached
10444  * (if any). Allocates necessary structures for each port.
10445  *
10446  * An AP (Attachement Point) node is created for each SATA device port even
10447  * when there is no device attached.
10448  */
10449 
10450 static void
10451 sata_probe_ports(sata_hba_inst_t *sata_hba_inst)
10452 {
10453 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
10454 	int			ncport;
10455 	sata_cport_info_t	*cportinfo;
10456 	sata_drive_info_t	*drive;
10457 	sata_device_t		sata_device;
10458 	int			rval;
10459 	dev_t			minor_number;
10460 	char			name[16];
10461 	clock_t			start_time, cur_time;
10462 
10463 	/*
10464 	 * Probe controller ports first, to find port status and
10465 	 * any port multiplier attached.
10466 	 */
10467 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
10468 		/* allocate cport structure */
10469 		cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP);
10470 		ASSERT(cportinfo != NULL);
10471 		mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL);
10472 
10473 		mutex_enter(&cportinfo->cport_mutex);
10474 
10475 		cportinfo->cport_addr.cport = ncport;
10476 		cportinfo->cport_addr.pmport = 0;
10477 		cportinfo->cport_addr.qual = SATA_ADDR_CPORT;
10478 		cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
10479 		cportinfo->cport_state |= SATA_STATE_PROBING;
10480 		SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo;
10481 
10482 		/*
10483 		 * Regardless if a port is usable or not, create
10484 		 * an attachment point
10485 		 */
10486 		mutex_exit(&cportinfo->cport_mutex);
10487 		minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip),
10488 		    ncport, 0, SATA_ADDR_CPORT);
10489 		(void) sprintf(name, "%d", ncport);
10490 		if (ddi_create_minor_node(dip, name, S_IFCHR,
10491 		    minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) !=
10492 		    DDI_SUCCESS) {
10493 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
10494 			    "cannot create SATA attachment point for port %d",
10495 			    ncport);
10496 		}
10497 
10498 		/* Probe port */
10499 		start_time = ddi_get_lbolt();
10500 	reprobe_cport:
10501 		sata_device.satadev_addr.cport = ncport;
10502 		sata_device.satadev_addr.pmport = 0;
10503 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
10504 		sata_device.satadev_rev = SATA_DEVICE_REV;
10505 
10506 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10507 		    (dip, &sata_device);
10508 
10509 		mutex_enter(&cportinfo->cport_mutex);
10510 		cportinfo->cport_scr = sata_device.satadev_scr;
10511 		if (rval != SATA_SUCCESS) {
10512 			/* Something went wrong? Fail the port */
10513 			cportinfo->cport_state = SATA_PSTATE_FAILED;
10514 			mutex_exit(&cportinfo->cport_mutex);
10515 			continue;
10516 		}
10517 		cportinfo->cport_state &= ~SATA_STATE_PROBING;
10518 		cportinfo->cport_state |= SATA_STATE_PROBED;
10519 		cportinfo->cport_dev_type = sata_device.satadev_type;
10520 
10521 		cportinfo->cport_state |= SATA_STATE_READY;
10522 		if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) {
10523 			mutex_exit(&cportinfo->cport_mutex);
10524 			continue;
10525 		}
10526 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
10527 			/*
10528 			 * There is some device attached.
10529 			 * Allocate device info structure
10530 			 */
10531 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) {
10532 				mutex_exit(&cportinfo->cport_mutex);
10533 				SATA_CPORTINFO_DRV_INFO(cportinfo) =
10534 				    kmem_zalloc(sizeof (sata_drive_info_t),
10535 				    KM_SLEEP);
10536 				mutex_enter(&cportinfo->cport_mutex);
10537 			}
10538 			drive = SATA_CPORTINFO_DRV_INFO(cportinfo);
10539 			drive->satadrv_addr = cportinfo->cport_addr;
10540 			drive->satadrv_addr.qual = SATA_ADDR_DCPORT;
10541 			drive->satadrv_type = cportinfo->cport_dev_type;
10542 			drive->satadrv_state = SATA_STATE_UNKNOWN;
10543 
10544 			mutex_exit(&cportinfo->cport_mutex);
10545 			if (sata_add_device(dip, sata_hba_inst, &sata_device) !=
10546 			    SATA_SUCCESS) {
10547 				/*
10548 				 * Plugged device was not correctly identified.
10549 				 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT
10550 				 */
10551 				cur_time = ddi_get_lbolt();
10552 				if ((cur_time - start_time) <
10553 				    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
10554 					/* sleep for a while */
10555 					delay(drv_usectohz(
10556 					    SATA_DEV_RETRY_DLY));
10557 					goto reprobe_cport;
10558 				}
10559 			}
10560 		} else { /* SATA_DTYPE_PMULT */
10561 			mutex_exit(&cportinfo->cport_mutex);
10562 
10563 			/* Allocate sata_pmult_info and sata_pmport_info */
10564 			if (sata_alloc_pmult(sata_hba_inst, &sata_device) !=
10565 			    SATA_SUCCESS)
10566 				continue;
10567 
10568 			/* Log the information of the port multiplier */
10569 			sata_show_pmult_info(sata_hba_inst, &sata_device);
10570 
10571 			/* Probe its pmports */
10572 			sata_probe_pmports(sata_hba_inst, ncport);
10573 		}
10574 	}
10575 }
10576 
10577 /*
10578  * Probe all device ports behind a port multiplier.
10579  *
10580  * PMult-related structure should be allocated before by sata_alloc_pmult().
10581  *
10582  * NOTE1: Only called from sata_probe_ports()
10583  * NOTE2: No mutex should be hold.
10584  */
10585 static void
10586 sata_probe_pmports(sata_hba_inst_t *sata_hba_inst, uint8_t ncport)
10587 {
10588 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
10589 	sata_pmult_info_t	*pmultinfo = NULL;
10590 	sata_pmport_info_t	*pmportinfo = NULL;
10591 	sata_drive_info_t	*drive = NULL;
10592 	sata_device_t		sata_device;
10593 
10594 	clock_t			start_time, cur_time;
10595 	int			npmport;
10596 	int			rval;
10597 
10598 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, ncport);
10599 
10600 	/* Probe Port Multiplier ports */
10601 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; npmport++) {
10602 		pmportinfo = pmultinfo->pmult_dev_port[npmport];
10603 		start_time = ddi_get_lbolt();
10604 reprobe_pmport:
10605 		sata_device.satadev_addr.cport = ncport;
10606 		sata_device.satadev_addr.pmport = npmport;
10607 		sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
10608 		sata_device.satadev_rev = SATA_DEVICE_REV;
10609 
10610 		/* Let HBA driver probe it. */
10611 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10612 		    (dip, &sata_device);
10613 		mutex_enter(&pmportinfo->pmport_mutex);
10614 
10615 		pmportinfo->pmport_scr = sata_device.satadev_scr;
10616 
10617 		if (rval != SATA_SUCCESS) {
10618 			pmportinfo->pmport_state =
10619 			    SATA_PSTATE_FAILED;
10620 			mutex_exit(&pmportinfo->pmport_mutex);
10621 			continue;
10622 		}
10623 		pmportinfo->pmport_state &= ~SATA_STATE_PROBING;
10624 		pmportinfo->pmport_state |= SATA_STATE_PROBED;
10625 		pmportinfo->pmport_dev_type = sata_device.satadev_type;
10626 
10627 		pmportinfo->pmport_state |= SATA_STATE_READY;
10628 		if (pmportinfo->pmport_dev_type ==
10629 		    SATA_DTYPE_NONE) {
10630 			SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
10631 			    "no device found at port %d:%d", ncport, npmport);
10632 			mutex_exit(&pmportinfo->pmport_mutex);
10633 			continue;
10634 		}
10635 		/* Port multipliers cannot be chained */
10636 		ASSERT(pmportinfo->pmport_dev_type != SATA_DTYPE_PMULT);
10637 		/*
10638 		 * There is something attached to Port
10639 		 * Multiplier device port
10640 		 * Allocate device info structure
10641 		 */
10642 		if (pmportinfo->pmport_sata_drive == NULL) {
10643 			mutex_exit(&pmportinfo->pmport_mutex);
10644 			pmportinfo->pmport_sata_drive =
10645 			    kmem_zalloc(sizeof (sata_drive_info_t), KM_SLEEP);
10646 			mutex_enter(&pmportinfo->pmport_mutex);
10647 		}
10648 		drive = pmportinfo->pmport_sata_drive;
10649 		drive->satadrv_addr.cport = pmportinfo->pmport_addr.cport;
10650 		drive->satadrv_addr.pmport = npmport;
10651 		drive->satadrv_addr.qual = SATA_ADDR_DPMPORT;
10652 		drive->satadrv_type = pmportinfo-> pmport_dev_type;
10653 		drive->satadrv_state = SATA_STATE_UNKNOWN;
10654 
10655 		mutex_exit(&pmportinfo->pmport_mutex);
10656 		rval = sata_add_device(dip, sata_hba_inst, &sata_device);
10657 
10658 		if (rval != SATA_SUCCESS) {
10659 			/*
10660 			 * Plugged device was not correctly identified.
10661 			 * Retry, within the SATA_DEV_IDENTIFY_TIMEOUT
10662 			 */
10663 			cur_time = ddi_get_lbolt();
10664 			if ((cur_time - start_time) < drv_usectohz(
10665 			    SATA_DEV_IDENTIFY_TIMEOUT)) {
10666 				/* sleep for a while */
10667 				delay(drv_usectohz(SATA_DEV_RETRY_DLY));
10668 				goto reprobe_pmport;
10669 			}
10670 		}
10671 	}
10672 }
10673 
10674 /*
10675  * Add SATA device for specified HBA instance & port (SCSI target
10676  * device nodes).
10677  * This function is called (indirectly) only from sata_hba_attach().
10678  * A target node is created when there is a supported type device attached,
10679  * but may be removed if it cannot be put online.
10680  *
10681  * This function cannot be called from an interrupt context.
10682  *
10683  * Create target nodes for disk, CD/DVD, Tape and ATAPI disk devices
10684  *
10685  * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when
10686  * device identification failed - adding a device could be retried.
10687  *
10688  */
10689 static int
10690 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst,
10691     sata_device_t *sata_device)
10692 {
10693 	sata_cport_info_t	*cportinfo;
10694 	sata_pmult_info_t	*pminfo;
10695 	sata_pmport_info_t	*pmportinfo;
10696 	dev_info_t		*cdip;		/* child dip */
10697 	sata_address_t		*saddr = &sata_device->satadev_addr;
10698 	uint8_t			cport, pmport;
10699 	int			rval;
10700 
10701 	cport = saddr->cport;
10702 	pmport = saddr->pmport;
10703 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
10704 	ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE);
10705 
10706 	/*
10707 	 * Some device is attached to a controller port.
10708 	 * We rely on controllers distinquishing between no-device,
10709 	 * attached port multiplier and other kind of attached device.
10710 	 * We need to get Identify Device data and determine
10711 	 * positively the dev type before trying to attach
10712 	 * the target driver.
10713 	 */
10714 	sata_device->satadev_rev = SATA_DEVICE_REV;
10715 	switch (saddr->qual) {
10716 	case SATA_ADDR_CPORT:
10717 		/*
10718 		 * Add a non-port-multiplier device at controller port.
10719 		 */
10720 		saddr->qual = SATA_ADDR_DCPORT;
10721 
10722 		rval = sata_probe_device(sata_hba_inst, sata_device);
10723 		if (rval != SATA_SUCCESS ||
10724 		    sata_device->satadev_type == SATA_DTYPE_UNKNOWN)
10725 			return (SATA_FAILURE);
10726 
10727 		mutex_enter(&cportinfo->cport_mutex);
10728 		sata_show_drive_info(sata_hba_inst,
10729 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
10730 
10731 		if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) {
10732 			/*
10733 			 * Could not determine device type or
10734 			 * a device is not supported.
10735 			 * Degrade this device to unknown.
10736 			 */
10737 			cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
10738 			mutex_exit(&cportinfo->cport_mutex);
10739 			return (SATA_SUCCESS);
10740 		}
10741 		cportinfo->cport_dev_type = sata_device->satadev_type;
10742 		cportinfo->cport_tgtnode_clean = B_TRUE;
10743 		mutex_exit(&cportinfo->cport_mutex);
10744 
10745 		/*
10746 		 * Initialize device to the desired state. Even if it
10747 		 * fails, the device will still attach but syslog
10748 		 * will show the warning.
10749 		 */
10750 		if (sata_initialize_device(sata_hba_inst,
10751 		    SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) {
10752 			/* Retry */
10753 			rval = sata_initialize_device(sata_hba_inst,
10754 			    SATA_CPORTINFO_DRV_INFO(cportinfo));
10755 
10756 			if (rval == SATA_RETRY)
10757 				sata_log(sata_hba_inst, CE_WARN,
10758 				    "SATA device at port %d - "
10759 				    "default device features could not be set."
10760 				    " Device may not operate as expected.",
10761 				    cport);
10762 		}
10763 
10764 		cdip = sata_create_target_node(pdip, sata_hba_inst, saddr);
10765 		if (cdip == NULL) {
10766 			/*
10767 			 * Attaching target node failed.
10768 			 * We retain sata_drive_info structure...
10769 			 */
10770 			return (SATA_SUCCESS);
10771 		}
10772 
10773 		mutex_enter(&cportinfo->cport_mutex);
10774 		(SATA_CPORTINFO_DRV_INFO(cportinfo))->
10775 		    satadrv_state = SATA_STATE_READY;
10776 		mutex_exit(&cportinfo->cport_mutex);
10777 
10778 		break;
10779 
10780 	case SATA_ADDR_PMPORT:
10781 		saddr->qual = SATA_ADDR_DPMPORT;
10782 
10783 		mutex_enter(&cportinfo->cport_mutex);
10784 		/* It must be a Port Multiplier at the controller port */
10785 		ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT);
10786 
10787 		pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
10788 		pmportinfo = pminfo->pmult_dev_port[saddr->pmport];
10789 		mutex_exit(&cportinfo->cport_mutex);
10790 
10791 		rval = sata_probe_device(sata_hba_inst, sata_device);
10792 		if (rval != SATA_SUCCESS ||
10793 		    sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
10794 			return (SATA_FAILURE);
10795 		}
10796 
10797 		mutex_enter(&pmportinfo->pmport_mutex);
10798 		sata_show_drive_info(sata_hba_inst,
10799 		    SATA_PMPORTINFO_DRV_INFO(pmportinfo));
10800 
10801 		if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) {
10802 			/*
10803 			 * Could not determine device type.
10804 			 * Degrade this device to unknown.
10805 			 */
10806 			pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
10807 			mutex_exit(&pmportinfo->pmport_mutex);
10808 			return (SATA_SUCCESS);
10809 		}
10810 		pmportinfo->pmport_dev_type = sata_device->satadev_type;
10811 		pmportinfo->pmport_tgtnode_clean = B_TRUE;
10812 		mutex_exit(&pmportinfo->pmport_mutex);
10813 
10814 		/*
10815 		 * Initialize device to the desired state.
10816 		 * Even if it fails, the device will still
10817 		 * attach but syslog will show the warning.
10818 		 */
10819 		if (sata_initialize_device(sata_hba_inst,
10820 		    pmportinfo->pmport_sata_drive) != SATA_SUCCESS) {
10821 			/* Retry */
10822 			rval = sata_initialize_device(sata_hba_inst,
10823 			    pmportinfo->pmport_sata_drive);
10824 
10825 			if (rval == SATA_RETRY)
10826 				sata_log(sata_hba_inst, CE_WARN,
10827 				    "SATA device at port %d:%d - "
10828 				    "default device features could not be set."
10829 				    " Device may not operate as expected.",
10830 				    cport, pmport);
10831 		}
10832 
10833 		cdip = sata_create_target_node(pdip, sata_hba_inst, saddr);
10834 		if (cdip == NULL) {
10835 			/*
10836 			 * Attaching target node failed.
10837 			 * We retain sata_drive_info structure...
10838 			 */
10839 			return (SATA_SUCCESS);
10840 		}
10841 		mutex_enter(&pmportinfo->pmport_mutex);
10842 		pmportinfo->pmport_sata_drive->satadrv_state |=
10843 		    SATA_STATE_READY;
10844 		mutex_exit(&pmportinfo->pmport_mutex);
10845 
10846 		break;
10847 
10848 	default:
10849 		return (SATA_FAILURE);
10850 	}
10851 
10852 	return (SATA_SUCCESS);
10853 }
10854 
10855 /*
10856  * Clean up target node at specific address.
10857  *
10858  * NOTE: No Mutex should be hold.
10859  */
10860 static int
10861 sata_offline_device(sata_hba_inst_t *sata_hba_inst,
10862     sata_device_t *sata_device, sata_drive_info_t *sdinfo)
10863 {
10864 	uint8_t cport, pmport, qual;
10865 	dev_info_t *tdip;
10866 
10867 	cport = sata_device->satadev_addr.cport;
10868 	pmport = sata_device->satadev_addr.pmport;
10869 	qual = sata_device->satadev_addr.qual;
10870 
10871 	if (qual == SATA_ADDR_DCPORT) {
10872 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10873 		    "sata_hba_ioctl: disconnect device at port %d", cport));
10874 	} else {
10875 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10876 		    "sata_hba_ioctl: disconnect device at port %d:%d",
10877 		    cport, pmport));
10878 	}
10879 
10880 	/* We are addressing attached device, not a port */
10881 	sata_device->satadev_addr.qual =
10882 	    sdinfo->satadrv_addr.qual;
10883 	tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
10884 	    &sata_device->satadev_addr);
10885 	if (tdip != NULL && ndi_devi_offline(tdip,
10886 	    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
10887 		/*
10888 		 * Problem :
10889 		 * The target node remained attached.
10890 		 * This happens when the device file was open
10891 		 * or a node was waiting for resources.
10892 		 * Cannot do anything about it.
10893 		 */
10894 		if (qual == SATA_ADDR_DCPORT) {
10895 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10896 			    "sata_hba_ioctl: disconnect: could "
10897 			    "not unconfigure device before "
10898 			    "disconnecting the SATA port %d",
10899 			    cport));
10900 		} else {
10901 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10902 			    "sata_hba_ioctl: disconnect: could "
10903 			    "not unconfigure device before "
10904 			    "disconnecting the SATA port %d:%d",
10905 			    cport, pmport));
10906 		}
10907 		/*
10908 		 * Set DEVICE REMOVED state in the target
10909 		 * node. It will prevent access to the device
10910 		 * even when a new device is attached, until
10911 		 * the old target node is released, removed and
10912 		 * recreated for a new  device.
10913 		 */
10914 		sata_set_device_removed(tdip);
10915 
10916 		/*
10917 		 * Instruct event daemon to try the target
10918 		 * node cleanup later.
10919 		 */
10920 		sata_set_target_node_cleanup(
10921 		    sata_hba_inst, &sata_device->satadev_addr);
10922 	}
10923 
10924 
10925 	return (SATA_SUCCESS);
10926 }
10927 
10928 
10929 /*
10930  * Create scsi target node for attached device, create node properties and
10931  * attach the node.
10932  * The node could be removed if the device onlining fails.
10933  *
10934  * A dev_info_t pointer is returned if operation is successful, NULL is
10935  * returned otherwise.
10936  */
10937 
10938 static dev_info_t *
10939 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst,
10940     sata_address_t *sata_addr)
10941 {
10942 	dev_info_t *cdip = NULL;
10943 	int rval;
10944 	char *nname = NULL;
10945 	char **compatible = NULL;
10946 	int ncompatible;
10947 	struct scsi_inquiry inq;
10948 	sata_device_t sata_device;
10949 	sata_drive_info_t *sdinfo;
10950 	int target;
10951 	int i;
10952 
10953 	sata_device.satadev_rev = SATA_DEVICE_REV;
10954 	sata_device.satadev_addr = *sata_addr;
10955 
10956 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport)));
10957 
10958 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
10959 
10960 	target = SATA_TO_SCSI_TARGET(sata_addr->cport,
10961 	    sata_addr->pmport, sata_addr->qual);
10962 
10963 	if (sdinfo == NULL) {
10964 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10965 		    sata_addr->cport)));
10966 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10967 		    "sata_create_target_node: no sdinfo for target %x",
10968 		    target));
10969 		return (NULL);
10970 	}
10971 
10972 	/*
10973 	 * create or get scsi inquiry data, expected by
10974 	 * scsi_hba_nodename_compatible_get()
10975 	 * SATA hard disks get Identify Data translated into Inguiry Data.
10976 	 * ATAPI devices respond directly to Inquiry request.
10977 	 */
10978 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10979 		sata_identdev_to_inquiry(sata_hba_inst, sdinfo,
10980 		    (uint8_t *)&inq);
10981 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10982 		    sata_addr->cport)));
10983 	} else { /* Assume supported ATAPI device */
10984 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10985 		    sata_addr->cport)));
10986 		if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr,
10987 		    &inq) == SATA_FAILURE)
10988 			return (NULL);
10989 		/*
10990 		 * Save supported ATAPI transport version
10991 		 */
10992 		sdinfo->satadrv_atapi_trans_ver =
10993 		    SATA_ATAPI_TRANS_VERSION(&inq);
10994 	}
10995 
10996 	/* determine the node name and compatible */
10997 	scsi_hba_nodename_compatible_get(&inq, NULL,
10998 	    inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
10999 
11000 #ifdef SATA_DEBUG
11001 	if (sata_debug_flags & SATA_DBG_NODES) {
11002 		if (nname == NULL) {
11003 			cmn_err(CE_NOTE, "sata_create_target_node: "
11004 			    "cannot determine nodename for target %d\n",
11005 			    target);
11006 		} else {
11007 			cmn_err(CE_WARN, "sata_create_target_node: "
11008 			    "target %d nodename: %s\n", target, nname);
11009 		}
11010 		if (compatible == NULL) {
11011 			cmn_err(CE_WARN,
11012 			    "sata_create_target_node: no compatible name\n");
11013 		} else {
11014 			for (i = 0; i < ncompatible; i++) {
11015 				cmn_err(CE_WARN, "sata_create_target_node: "
11016 				    "compatible name: %s\n", compatible[i]);
11017 			}
11018 		}
11019 	}
11020 #endif
11021 
11022 	/* if nodename can't be determined, log error and exit */
11023 	if (nname == NULL) {
11024 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11025 		    "sata_create_target_node: cannot determine nodename "
11026 		    "for target %d\n", target));
11027 		scsi_hba_nodename_compatible_free(nname, compatible);
11028 		return (NULL);
11029 	}
11030 	/*
11031 	 * Create scsi target node
11032 	 */
11033 	ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip);
11034 	rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
11035 	    "device-type", "scsi");
11036 
11037 	if (rval != DDI_PROP_SUCCESS) {
11038 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
11039 		    "updating device_type prop failed %d", rval));
11040 		goto fail;
11041 	}
11042 
11043 	/*
11044 	 * Create target node properties: target & lun
11045 	 */
11046 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target);
11047 	if (rval != DDI_PROP_SUCCESS) {
11048 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
11049 		    "updating target prop failed %d", rval));
11050 		goto fail;
11051 	}
11052 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0);
11053 	if (rval != DDI_PROP_SUCCESS) {
11054 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
11055 		    "updating target prop failed %d", rval));
11056 		goto fail;
11057 	}
11058 
11059 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
11060 		/*
11061 		 * Add "variant" property
11062 		 */
11063 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
11064 		    "variant", "atapi");
11065 		if (rval != DDI_PROP_SUCCESS) {
11066 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11067 			    "sata_create_target_node: variant atapi "
11068 			    "property could not be created: %d", rval));
11069 			goto fail;
11070 		}
11071 	}
11072 	/* decorate the node with compatible */
11073 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible",
11074 	    compatible, ncompatible) != DDI_PROP_SUCCESS) {
11075 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11076 		    "sata_create_target_node: FAIL compatible props cdip 0x%p",
11077 		    (void *)cdip));
11078 		goto fail;
11079 	}
11080 
11081 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
11082 		/*
11083 		 * Add "sata-phy" property
11084 		 */
11085 		if (ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "sata-phy",
11086 		    (int)sata_addr->cport) != DDI_PROP_SUCCESS) {
11087 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11088 			    "sata_create_target_node: failed to create "
11089 			    "\"sata-phy\" property: port %d",
11090 			    sata_addr->cport));
11091 		}
11092 	}
11093 
11094 
11095 	/*
11096 	 * Now, try to attach the driver. If probing of the device fails,
11097 	 * the target node may be removed
11098 	 */
11099 	rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH);
11100 
11101 	scsi_hba_nodename_compatible_free(nname, compatible);
11102 
11103 	if (rval == NDI_SUCCESS)
11104 		return (cdip);
11105 
11106 	/* target node was removed - are we sure? */
11107 	return (NULL);
11108 
11109 fail:
11110 	scsi_hba_nodename_compatible_free(nname, compatible);
11111 	ddi_prop_remove_all(cdip);
11112 	rval = ndi_devi_free(cdip);
11113 	if (rval != NDI_SUCCESS) {
11114 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
11115 		    "node removal failed %d", rval));
11116 	}
11117 	sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: "
11118 	    "cannot create target node for SATA device at port %d",
11119 	    sata_addr->cport);
11120 	return (NULL);
11121 }
11122 
11123 /*
11124  * Remove a target node.
11125  */
11126 static void
11127 sata_remove_target_node(sata_hba_inst_t *sata_hba_inst,
11128     sata_address_t *sata_addr)
11129 {
11130 	dev_info_t *tdip;
11131 	uint8_t cport = sata_addr->cport;
11132 	uint8_t pmport = sata_addr->pmport;
11133 	uint8_t qual = sata_addr->qual;
11134 
11135 	/* Note the sata daemon uses the address of the port/pmport */
11136 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
11137 
11138 	/* Remove target node */
11139 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), cport, pmport);
11140 	if (tdip != NULL) {
11141 		/*
11142 		 * Target node exists.  Unconfigure device
11143 		 * then remove the target node (one ndi
11144 		 * operation).
11145 		 */
11146 		if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
11147 			/*
11148 			 * PROBLEM - no device, but target node remained. This
11149 			 * happens when the file was open or node was waiting
11150 			 * for resources.
11151 			 */
11152 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11153 			    "sata_remove_target_node: "
11154 			    "Failed to remove target node for "
11155 			    "detached SATA device."));
11156 			/*
11157 			 * Set target node state to DEVI_DEVICE_REMOVED. But
11158 			 * re-check first that the node still exists.
11159 			 */
11160 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
11161 			    cport, pmport);
11162 			if (tdip != NULL) {
11163 				sata_set_device_removed(tdip);
11164 				/*
11165 				 * Instruct event daemon to retry the cleanup
11166 				 * later.
11167 				 */
11168 				sata_set_target_node_cleanup(sata_hba_inst,
11169 				    sata_addr);
11170 			}
11171 		}
11172 
11173 		if (qual == SATA_ADDR_CPORT)
11174 			sata_log(sata_hba_inst, CE_WARN,
11175 			    "SATA device detached at port %d", cport);
11176 		else
11177 			sata_log(sata_hba_inst, CE_WARN,
11178 			    "SATA device detached at port %d:%d",
11179 			    cport, pmport);
11180 	}
11181 #ifdef SATA_DEBUG
11182 	else {
11183 		if (qual == SATA_ADDR_CPORT)
11184 			sata_log(sata_hba_inst, CE_WARN,
11185 			    "target node not found at port %d", cport);
11186 		else
11187 			sata_log(sata_hba_inst, CE_WARN,
11188 			    "target node not found at port %d:%d",
11189 			    cport, pmport);
11190 	}
11191 #endif
11192 }
11193 
11194 
11195 /*
11196  * Re-probe sata port, check for a device and attach info
11197  * structures when necessary. Identify Device data is fetched, if possible.
11198  * Assumption: sata address is already validated.
11199  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
11200  * the presence of a device and its type.
11201  *
11202  * flag arg specifies that the function should try multiple times to identify
11203  * device type and to initialize it, or it should return immediately on failure.
11204  * SATA_DEV_IDENTIFY_RETRY - retry
11205  * SATA_DEV_IDENTIFY_NORETRY - no retry
11206  *
11207  * SATA_FAILURE is returned if one of the operations failed.
11208  *
11209  * This function cannot be called in interrupt context - it may sleep.
11210  *
11211  * Note: Port multiplier is supported.
11212  */
11213 static int
11214 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11215     int flag)
11216 {
11217 	sata_cport_info_t *cportinfo;
11218 	sata_pmult_info_t *pmultinfo;
11219 	sata_drive_info_t *sdinfo, *osdinfo;
11220 	boolean_t init_device = B_FALSE;
11221 	int prev_device_type = SATA_DTYPE_NONE;
11222 	int prev_device_settings = 0;
11223 	int prev_device_state = 0;
11224 	clock_t start_time;
11225 	int retry = B_FALSE;
11226 	uint8_t cport = sata_device->satadev_addr.cport;
11227 	int rval_probe, rval_init;
11228 
11229 	/*
11230 	 * If target is pmport, sata_reprobe_pmport() will handle it.
11231 	 */
11232 	if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT ||
11233 	    sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT)
11234 		return (sata_reprobe_pmport(sata_hba_inst, sata_device, flag));
11235 
11236 	/* We only care about host sata cport for now */
11237 	cportinfo = SATA_CPORT_INFO(sata_hba_inst,
11238 	    sata_device->satadev_addr.cport);
11239 
11240 	/*
11241 	 * If a port multiplier was previously attached (we have no idea it
11242 	 * still there or not), sata_reprobe_pmult() will handle it.
11243 	 */
11244 	if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT)
11245 		return (sata_reprobe_pmult(sata_hba_inst, sata_device, flag));
11246 
11247 	/* Store sata_drive_info when a non-pmult device was attached. */
11248 	osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11249 	if (osdinfo != NULL) {
11250 		/*
11251 		 * We are re-probing port with a previously attached device.
11252 		 * Save previous device type and settings.
11253 		 */
11254 		prev_device_type = cportinfo->cport_dev_type;
11255 		prev_device_settings = osdinfo->satadrv_settings;
11256 		prev_device_state = osdinfo->satadrv_state;
11257 	}
11258 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
11259 		start_time = ddi_get_lbolt();
11260 		retry = B_TRUE;
11261 	}
11262 retry_probe:
11263 
11264 	/* probe port */
11265 	mutex_enter(&cportinfo->cport_mutex);
11266 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11267 	cportinfo->cport_state |= SATA_STATE_PROBING;
11268 	mutex_exit(&cportinfo->cport_mutex);
11269 
11270 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11271 	    (SATA_DIP(sata_hba_inst), sata_device);
11272 
11273 	mutex_enter(&cportinfo->cport_mutex);
11274 	if (rval_probe != SATA_SUCCESS) {
11275 		cportinfo->cport_state = SATA_PSTATE_FAILED;
11276 		mutex_exit(&cportinfo->cport_mutex);
11277 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: "
11278 		    "SATA port %d probing failed",
11279 		    cportinfo->cport_addr.cport));
11280 		return (SATA_FAILURE);
11281 	}
11282 
11283 	/*
11284 	 * update sata port state and set device type
11285 	 */
11286 	sata_update_port_info(sata_hba_inst, sata_device);
11287 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
11288 
11289 	/*
11290 	 * Sanity check - Port is active? Is the link active?
11291 	 * Is there any device attached?
11292 	 */
11293 	if ((cportinfo->cport_state &
11294 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11295 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11296 	    SATA_PORT_DEVLINK_UP) {
11297 		/*
11298 		 * Port in non-usable state or no link active/no device.
11299 		 * Free info structure if necessary (direct attached drive
11300 		 * only, for now!
11301 		 */
11302 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11303 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11304 		/* Add here differentiation for device attached or not */
11305 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11306 		mutex_exit(&cportinfo->cport_mutex);
11307 		if (sdinfo != NULL)
11308 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11309 		return (SATA_SUCCESS);
11310 	}
11311 
11312 	cportinfo->cport_state |= SATA_STATE_READY;
11313 	cportinfo->cport_state |= SATA_STATE_PROBED;
11314 
11315 	cportinfo->cport_dev_type = sata_device->satadev_type;
11316 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11317 
11318 	/*
11319 	 * If we are re-probing the port, there may be
11320 	 * sata_drive_info structure attached
11321 	 */
11322 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
11323 
11324 		/*
11325 		 * There is no device, so remove device info structure,
11326 		 * if necessary.
11327 		 */
11328 		/* Device change: Drive -> None */
11329 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11330 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11331 		if (sdinfo != NULL) {
11332 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11333 			sata_log(sata_hba_inst, CE_WARN,
11334 			    "SATA device detached "
11335 			    "from port %d", cportinfo->cport_addr.cport);
11336 		}
11337 		mutex_exit(&cportinfo->cport_mutex);
11338 		return (SATA_SUCCESS);
11339 
11340 	}
11341 
11342 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
11343 
11344 		/* Device (may) change: Drive -> Drive */
11345 		if (sdinfo == NULL) {
11346 			/*
11347 			 * There is some device attached, but there is
11348 			 * no sata_drive_info structure - allocate one
11349 			 */
11350 			mutex_exit(&cportinfo->cport_mutex);
11351 			sdinfo = kmem_zalloc(
11352 			    sizeof (sata_drive_info_t), KM_SLEEP);
11353 			mutex_enter(&cportinfo->cport_mutex);
11354 			/*
11355 			 * Recheck, that the port state did not change when we
11356 			 * released mutex.
11357 			 */
11358 			if (cportinfo->cport_state & SATA_STATE_READY) {
11359 				SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo;
11360 				sdinfo->satadrv_addr = cportinfo->cport_addr;
11361 				sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT;
11362 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11363 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
11364 			} else {
11365 				/*
11366 				 * Port is not in ready state, we
11367 				 * cannot attach a device.
11368 				 */
11369 				mutex_exit(&cportinfo->cport_mutex);
11370 				kmem_free(sdinfo, sizeof (sata_drive_info_t));
11371 				return (SATA_SUCCESS);
11372 			}
11373 			/*
11374 			 * Since we are adding device, presumably new one,
11375 			 * indicate that it  should be initalized,
11376 			 * as well as some internal framework states).
11377 			 */
11378 			init_device = B_TRUE;
11379 		}
11380 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11381 		sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
11382 	} else {
11383 		/* Device change: Drive -> PMult */
11384 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11385 		if (sdinfo != NULL) {
11386 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11387 			sata_log(sata_hba_inst, CE_WARN,
11388 			    "SATA device detached "
11389 			    "from port %d", cportinfo->cport_addr.cport);
11390 		}
11391 
11392 		sata_log(sata_hba_inst, CE_WARN,
11393 		    "SATA port multiplier detected at port %d",
11394 		    cportinfo->cport_addr.cport);
11395 
11396 		mutex_exit(&cportinfo->cport_mutex);
11397 		if (sata_alloc_pmult(sata_hba_inst, sata_device) !=
11398 		    SATA_SUCCESS)
11399 			return (SATA_FAILURE);
11400 		sata_show_pmult_info(sata_hba_inst, sata_device);
11401 		mutex_enter(&cportinfo->cport_mutex);
11402 
11403 		/*
11404 		 * Mark all the port multiplier port behind the port
11405 		 * multiplier behind with link events, so that the sata daemon
11406 		 * will update their status.
11407 		 */
11408 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
11409 		pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET;
11410 		mutex_exit(&cportinfo->cport_mutex);
11411 		return (SATA_SUCCESS);
11412 	}
11413 	mutex_exit(&cportinfo->cport_mutex);
11414 
11415 	/*
11416 	 * Figure out what kind of device we are really
11417 	 * dealing with. Failure of identifying device does not fail this
11418 	 * function.
11419 	 */
11420 	rval_probe = sata_probe_device(sata_hba_inst, sata_device);
11421 	rval_init = SATA_FAILURE;
11422 	mutex_enter(&cportinfo->cport_mutex);
11423 	if (rval_probe == SATA_SUCCESS) {
11424 		/*
11425 		 * If we are dealing with the same type of a device as before,
11426 		 * restore its settings flags.
11427 		 */
11428 		if (osdinfo != NULL &&
11429 		    sata_device->satadev_type == prev_device_type)
11430 			sdinfo->satadrv_settings = prev_device_settings;
11431 
11432 		mutex_exit(&cportinfo->cport_mutex);
11433 		rval_init = SATA_SUCCESS;
11434 		/* Set initial device features, if necessary */
11435 		if (init_device == B_TRUE) {
11436 			rval_init = sata_initialize_device(sata_hba_inst,
11437 			    sdinfo);
11438 		}
11439 		if (rval_init == SATA_SUCCESS)
11440 			return (rval_init);
11441 		/* else we will retry if retry was asked for */
11442 
11443 	} else {
11444 		/*
11445 		 * If there was some device info before we probe the device,
11446 		 * restore previous device setting, so we can retry from scratch
11447 		 * later. Providing, of course, that device has not disapear
11448 		 * during probing process.
11449 		 */
11450 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11451 			if (osdinfo != NULL) {
11452 				cportinfo->cport_dev_type = prev_device_type;
11453 				sdinfo->satadrv_type = prev_device_type;
11454 				sdinfo->satadrv_state = prev_device_state;
11455 			}
11456 		} else {
11457 			/* device is gone */
11458 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11459 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11460 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11461 			mutex_exit(&cportinfo->cport_mutex);
11462 			return (SATA_SUCCESS);
11463 		}
11464 		mutex_exit(&cportinfo->cport_mutex);
11465 	}
11466 
11467 	if (retry) {
11468 		clock_t cur_time = ddi_get_lbolt();
11469 		/*
11470 		 * A device was not successfully identified or initialized.
11471 		 * Track retry time for device identification.
11472 		 */
11473 		if ((cur_time - start_time) <
11474 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11475 			/* sleep for a while */
11476 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11477 			goto retry_probe;
11478 		}
11479 		/* else no more retries */
11480 		mutex_enter(&cportinfo->cport_mutex);
11481 		if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
11482 			if (rval_init == SATA_RETRY) {
11483 				/*
11484 				 * Setting drive features have failed, but
11485 				 * because the drive is still accessible,
11486 				 * keep it and emit a warning message.
11487 				 */
11488 				sata_log(sata_hba_inst, CE_WARN,
11489 				    "SATA device at port %d - desired "
11490 				    "drive features could not be set. "
11491 				    "Device may not operate as expected.",
11492 				    cportinfo->cport_addr.cport);
11493 			} else {
11494 				SATA_CPORTINFO_DRV_INFO(cportinfo)->
11495 				    satadrv_state = SATA_DSTATE_FAILED;
11496 			}
11497 		}
11498 		mutex_exit(&cportinfo->cport_mutex);
11499 	}
11500 	return (SATA_SUCCESS);
11501 }
11502 
11503 /*
11504  * Reprobe a controller port that connected to a port multiplier.
11505  *
11506  * NOTE: No Mutex should be hold.
11507  */
11508 static int
11509 sata_reprobe_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11510     int flag)
11511 {
11512 	_NOTE(ARGUNUSED(flag))
11513 	sata_cport_info_t *cportinfo;
11514 	sata_pmult_info_t *pmultinfo;
11515 	uint8_t cport = sata_device->satadev_addr.cport;
11516 	int rval_probe;
11517 
11518 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11519 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
11520 
11521 	/* probe port */
11522 	mutex_enter(&cportinfo->cport_mutex);
11523 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11524 	cportinfo->cport_state |= SATA_STATE_PROBING;
11525 	mutex_exit(&cportinfo->cport_mutex);
11526 
11527 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11528 	    (SATA_DIP(sata_hba_inst), sata_device);
11529 
11530 	mutex_enter(&cportinfo->cport_mutex);
11531 	if (rval_probe != SATA_SUCCESS) {
11532 		cportinfo->cport_state = SATA_PSTATE_FAILED;
11533 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmult: "
11534 		    "SATA port %d probing failed", cport));
11535 		sata_log(sata_hba_inst, CE_WARN,
11536 		    "SATA port multiplier detached at port %d", cport);
11537 		mutex_exit(&cportinfo->cport_mutex);
11538 		sata_free_pmult(sata_hba_inst, sata_device);
11539 		return (SATA_FAILURE);
11540 	}
11541 
11542 	/*
11543 	 * update sata port state and set device type
11544 	 */
11545 	sata_update_port_info(sata_hba_inst, sata_device);
11546 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
11547 	cportinfo->cport_state |= SATA_STATE_PROBED;
11548 
11549 	/*
11550 	 * Sanity check - Port is active? Is the link active?
11551 	 * Is there any device attached?
11552 	 */
11553 	if ((cportinfo->cport_state &
11554 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11555 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11556 	    SATA_PORT_DEVLINK_UP ||
11557 	    (sata_device->satadev_type == SATA_DTYPE_NONE)) {
11558 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11559 		mutex_exit(&cportinfo->cport_mutex);
11560 		sata_free_pmult(sata_hba_inst, sata_device);
11561 		sata_log(sata_hba_inst, CE_WARN,
11562 		    "SATA port multiplier detached at port %d", cport);
11563 		return (SATA_SUCCESS);
11564 	}
11565 
11566 	/*
11567 	 * Device changed: PMult -> Non-PMult
11568 	 *
11569 	 * This situation is uncommon, most possibly being caused by errors
11570 	 * after which the port multiplier is not correct initialized and
11571 	 * recognized. In that case the new device will be marked as unknown
11572 	 * and will not be automatically probed in this routine. Instead
11573 	 * system administrator could manually restart it via cfgadm(1M).
11574 	 */
11575 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
11576 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11577 		mutex_exit(&cportinfo->cport_mutex);
11578 		sata_free_pmult(sata_hba_inst, sata_device);
11579 		sata_log(sata_hba_inst, CE_WARN,
11580 		    "SATA port multiplier detached at port %d", cport);
11581 		return (SATA_FAILURE);
11582 	}
11583 
11584 	/*
11585 	 * Now we know it is a port multiplier. However, if this is not the
11586 	 * previously attached port multiplier - they may have different
11587 	 * pmport numbers - we need to re-allocate data structures for every
11588 	 * pmport and drive.
11589 	 *
11590 	 * Port multipliers of the same model have identical values in these
11591 	 * registers, so it is still necessary to update the information of
11592 	 * all drives attached to the previous port multiplier afterwards.
11593 	 */
11594 	/* Device changed: PMult -> another PMult */
11595 	mutex_exit(&cportinfo->cport_mutex);
11596 	sata_free_pmult(sata_hba_inst, sata_device);
11597 	if (sata_alloc_pmult(sata_hba_inst, sata_device) != SATA_SUCCESS)
11598 		return (SATA_FAILURE);
11599 	mutex_enter(&cportinfo->cport_mutex);
11600 
11601 	SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
11602 	    "SATA port multiplier [changed] at port %d", cport);
11603 	sata_log(sata_hba_inst, CE_WARN,
11604 	    "SATA port multiplier detected at port %d", cport);
11605 
11606 	/*
11607 	 * Mark all the port multiplier port behind the port
11608 	 * multiplier behind with link events, so that the sata daemon
11609 	 * will update their status.
11610 	 */
11611 	pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET;
11612 	mutex_exit(&cportinfo->cport_mutex);
11613 
11614 	return (SATA_SUCCESS);
11615 }
11616 
11617 /*
11618  * Re-probe a port multiplier port, check for a device and attach info
11619  * structures when necessary. Identify Device data is fetched, if possible.
11620  * Assumption: sata address is already validated as port multiplier port.
11621  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
11622  * the presence of a device and its type.
11623  *
11624  * flag arg specifies that the function should try multiple times to identify
11625  * device type and to initialize it, or it should return immediately on failure.
11626  * SATA_DEV_IDENTIFY_RETRY - retry
11627  * SATA_DEV_IDENTIFY_NORETRY - no retry
11628  *
11629  * SATA_FAILURE is returned if one of the operations failed.
11630  *
11631  * This function cannot be called in interrupt context - it may sleep.
11632  *
11633  * NOTE: Should be only called by sata_probe_port() in case target port is a
11634  *       port multiplier port.
11635  * NOTE: No Mutex should be hold.
11636  */
11637 static int
11638 sata_reprobe_pmport(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11639     int flag)
11640 {
11641 	sata_cport_info_t *cportinfo = NULL;
11642 	sata_pmport_info_t *pmportinfo = NULL;
11643 	sata_drive_info_t *sdinfo, *osdinfo;
11644 	sata_device_t sdevice;
11645 	boolean_t init_device = B_FALSE;
11646 	int prev_device_type = SATA_DTYPE_NONE;
11647 	int prev_device_settings = 0;
11648 	int prev_device_state = 0;
11649 	clock_t start_time;
11650 	uint8_t cport = sata_device->satadev_addr.cport;
11651 	uint8_t pmport = sata_device->satadev_addr.pmport;
11652 	int rval;
11653 
11654 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11655 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11656 	osdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11657 
11658 	if (osdinfo != NULL) {
11659 		/*
11660 		 * We are re-probing port with a previously attached device.
11661 		 * Save previous device type and settings.
11662 		 */
11663 		prev_device_type = pmportinfo->pmport_dev_type;
11664 		prev_device_settings = osdinfo->satadrv_settings;
11665 		prev_device_state = osdinfo->satadrv_state;
11666 	}
11667 
11668 	start_time = ddi_get_lbolt();
11669 
11670 	/* check parent status */
11671 	mutex_enter(&cportinfo->cport_mutex);
11672 	if ((cportinfo->cport_state &
11673 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11674 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11675 	    SATA_PORT_DEVLINK_UP) {
11676 		mutex_exit(&cportinfo->cport_mutex);
11677 		return (SATA_FAILURE);
11678 	}
11679 	mutex_exit(&cportinfo->cport_mutex);
11680 
11681 retry_probe_pmport:
11682 
11683 	/* probe port */
11684 	mutex_enter(&pmportinfo->pmport_mutex);
11685 	pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11686 	pmportinfo->pmport_state |= SATA_STATE_PROBING;
11687 	mutex_exit(&pmportinfo->pmport_mutex);
11688 
11689 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11690 	    (SATA_DIP(sata_hba_inst), sata_device);
11691 
11692 	/* might need retry because we cannot touch registers. */
11693 	if (rval == SATA_FAILURE) {
11694 		mutex_enter(&pmportinfo->pmport_mutex);
11695 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
11696 		mutex_exit(&pmportinfo->pmport_mutex);
11697 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: "
11698 		    "SATA port %d:%d probing failed",
11699 		    cport, pmport));
11700 		return (SATA_FAILURE);
11701 	} else if (rval == SATA_RETRY) {
11702 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: "
11703 		    "SATA port %d:%d probing failed, retrying...",
11704 		    cport, pmport));
11705 		clock_t cur_time = ddi_get_lbolt();
11706 		/*
11707 		 * A device was not successfully identified or initialized.
11708 		 * Track retry time for device identification.
11709 		 */
11710 		if ((cur_time - start_time) <
11711 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11712 			/* sleep for a while */
11713 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11714 			goto retry_probe_pmport;
11715 		} else {
11716 			mutex_enter(&pmportinfo->pmport_mutex);
11717 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL)
11718 				SATA_PMPORTINFO_DRV_INFO(pmportinfo)->
11719 				    satadrv_state = SATA_DSTATE_FAILED;
11720 			mutex_exit(&pmportinfo->pmport_mutex);
11721 			return (SATA_SUCCESS);
11722 		}
11723 	}
11724 
11725 	/*
11726 	 * Sanity check - Controller port is active? Is the link active?
11727 	 * Is it still a port multiplier?
11728 	 */
11729 	if ((cportinfo->cport_state &
11730 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11731 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11732 	    SATA_PORT_DEVLINK_UP ||
11733 	    (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) {
11734 		/*
11735 		 * Port in non-usable state or no link active/no
11736 		 * device. Free info structure.
11737 		 */
11738 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11739 
11740 		sdevice.satadev_addr.cport = cport;
11741 		sdevice.satadev_addr.pmport = pmport;
11742 		sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
11743 		mutex_exit(&cportinfo->cport_mutex);
11744 
11745 		sata_free_pmult(sata_hba_inst, &sdevice);
11746 		return (SATA_FAILURE);
11747 	}
11748 
11749 	/* SATA_SUCCESS NOW */
11750 	/*
11751 	 * update sata port state and set device type
11752 	 */
11753 	mutex_enter(&pmportinfo->pmport_mutex);
11754 	sata_update_pmport_info(sata_hba_inst, sata_device);
11755 	pmportinfo->pmport_state &= ~SATA_STATE_PROBING;
11756 
11757 	/*
11758 	 * Sanity check - Port is active? Is the link active?
11759 	 * Is there any device attached?
11760 	 */
11761 	if ((pmportinfo->pmport_state &
11762 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11763 	    (pmportinfo->pmport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11764 	    SATA_PORT_DEVLINK_UP) {
11765 		/*
11766 		 * Port in non-usable state or no link active/no device.
11767 		 * Free info structure if necessary (direct attached drive
11768 		 * only, for now!
11769 		 */
11770 		sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11771 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11772 		/* Add here differentiation for device attached or not */
11773 		pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11774 		mutex_exit(&pmportinfo->pmport_mutex);
11775 		if (sdinfo != NULL)
11776 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11777 		return (SATA_SUCCESS);
11778 	}
11779 
11780 	pmportinfo->pmport_state |= SATA_STATE_READY;
11781 	pmportinfo->pmport_dev_type = sata_device->satadev_type;
11782 	sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11783 
11784 	/*
11785 	 * If we are re-probing the port, there may be
11786 	 * sata_drive_info structure attached
11787 	 * (or sata_pm_info, if PMult is supported).
11788 	 */
11789 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
11790 		/*
11791 		 * There is no device, so remove device info structure,
11792 		 * if necessary.
11793 		 */
11794 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11795 		pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11796 		if (sdinfo != NULL) {
11797 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11798 			sata_log(sata_hba_inst, CE_WARN,
11799 			    "SATA device detached from port %d:%d",
11800 			    cport, pmport);
11801 		}
11802 		mutex_exit(&pmportinfo->pmport_mutex);
11803 		return (SATA_SUCCESS);
11804 	}
11805 
11806 	/* this should not be a pmult */
11807 	ASSERT(sata_device->satadev_type != SATA_DTYPE_PMULT);
11808 	if (sdinfo == NULL) {
11809 		/*
11810 		 * There is some device attached, but there is
11811 		 * no sata_drive_info structure - allocate one
11812 		 */
11813 		mutex_exit(&pmportinfo->pmport_mutex);
11814 		sdinfo = kmem_zalloc(sizeof (sata_drive_info_t),
11815 		    KM_SLEEP);
11816 		mutex_enter(&pmportinfo->pmport_mutex);
11817 		/*
11818 		 * Recheck, that the port state did not change when we
11819 		 * released mutex.
11820 		 */
11821 		if (pmportinfo->pmport_state & SATA_STATE_READY) {
11822 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = sdinfo;
11823 			sdinfo->satadrv_addr = pmportinfo->pmport_addr;
11824 			sdinfo->satadrv_addr.qual = SATA_ADDR_DPMPORT;
11825 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11826 			sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
11827 		} else {
11828 			/*
11829 			 * Port is not in ready state, we
11830 			 * cannot attach a device.
11831 			 */
11832 			mutex_exit(&pmportinfo->pmport_mutex);
11833 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11834 			return (SATA_SUCCESS);
11835 		}
11836 		/*
11837 		 * Since we are adding device, presumably new one,
11838 		 * indicate that it  should be initalized,
11839 		 * as well as some internal framework states).
11840 		 */
11841 		init_device = B_TRUE;
11842 	}
11843 
11844 	pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
11845 	sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
11846 
11847 	mutex_exit(&pmportinfo->pmport_mutex);
11848 	/*
11849 	 * Figure out what kind of device we are really
11850 	 * dealing with.
11851 	 */
11852 	rval = sata_probe_device(sata_hba_inst, sata_device);
11853 
11854 	mutex_enter(&pmportinfo->pmport_mutex);
11855 	if (rval == SATA_SUCCESS) {
11856 		/*
11857 		 * If we are dealing with the same type of a device as before,
11858 		 * restore its settings flags.
11859 		 */
11860 		if (osdinfo != NULL &&
11861 		    sata_device->satadev_type == prev_device_type)
11862 			sdinfo->satadrv_settings = prev_device_settings;
11863 
11864 		mutex_exit(&pmportinfo->pmport_mutex);
11865 		/* Set initial device features, if necessary */
11866 		if (init_device == B_TRUE) {
11867 			rval = sata_initialize_device(sata_hba_inst, sdinfo);
11868 		}
11869 		if (rval == SATA_SUCCESS)
11870 			return (rval);
11871 	} else {
11872 		/*
11873 		 * If there was some device info before we probe the device,
11874 		 * restore previous device setting, so we can retry from scratch
11875 		 * later. Providing, of course, that device has not disappeared
11876 		 * during probing process.
11877 		 */
11878 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11879 			if (osdinfo != NULL) {
11880 				pmportinfo->pmport_dev_type = prev_device_type;
11881 				sdinfo->satadrv_type = prev_device_type;
11882 				sdinfo->satadrv_state = prev_device_state;
11883 			}
11884 		} else {
11885 			/* device is gone */
11886 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11887 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11888 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11889 			mutex_exit(&pmportinfo->pmport_mutex);
11890 			return (SATA_SUCCESS);
11891 		}
11892 		mutex_exit(&pmportinfo->pmport_mutex);
11893 	}
11894 
11895 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
11896 		clock_t cur_time = ddi_get_lbolt();
11897 		/*
11898 		 * A device was not successfully identified or initialized.
11899 		 * Track retry time for device identification.
11900 		 */
11901 		if ((cur_time - start_time) <
11902 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11903 			/* sleep for a while */
11904 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11905 			goto retry_probe_pmport;
11906 		} else {
11907 			mutex_enter(&pmportinfo->pmport_mutex);
11908 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL)
11909 				SATA_PMPORTINFO_DRV_INFO(pmportinfo)->
11910 				    satadrv_state = SATA_DSTATE_FAILED;
11911 			mutex_exit(&pmportinfo->pmport_mutex);
11912 		}
11913 	}
11914 	return (SATA_SUCCESS);
11915 }
11916 
11917 /*
11918  * Allocated related structure for a port multiplier and its device ports
11919  *
11920  * Port multiplier should be ready and probed, and related information like
11921  * the number of the device ports should be store in sata_device_t.
11922  *
11923  * NOTE: No Mutex should be hold.
11924  */
11925 static int
11926 sata_alloc_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
11927 {
11928 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
11929 	sata_cport_info_t *cportinfo = NULL;
11930 	sata_pmult_info_t *pmultinfo = NULL;
11931 	sata_pmport_info_t *pmportinfo = NULL;
11932 	sata_device_t sd;
11933 	dev_t minor_number;
11934 	char name[16];
11935 	uint8_t cport = sata_device->satadev_addr.cport;
11936 	int rval;
11937 	int npmport;
11938 
11939 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11940 
11941 	/* This function might be called while a port-mult is hot-plugged. */
11942 	mutex_enter(&cportinfo->cport_mutex);
11943 
11944 	/* dev_type's not updated when get called from sata_reprobe_port() */
11945 	if (SATA_CPORTINFO_PMULT_INFO(cportinfo) == NULL) {
11946 		/* Create a pmult_info structure */
11947 		SATA_CPORTINFO_PMULT_INFO(cportinfo) =
11948 		    kmem_zalloc(sizeof (sata_pmult_info_t), KM_SLEEP);
11949 	}
11950 	pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
11951 
11952 	pmultinfo->pmult_addr = sata_device->satadev_addr;
11953 	pmultinfo->pmult_addr.qual = SATA_ADDR_PMULT;
11954 	pmultinfo->pmult_state = SATA_STATE_PROBING;
11955 
11956 	/*
11957 	 * Probe the port multiplier with qualifier SATA_ADDR_PMULT_SPEC,
11958 	 * The HBA driver should initialize and register the port multiplier,
11959 	 * sata_register_pmult() will fill following fields,
11960 	 *   + sata_pmult_info.pmult_gscr
11961 	 *   + sata_pmult_info.pmult_num_dev_ports
11962 	 */
11963 	sd.satadev_addr = sata_device->satadev_addr;
11964 	sd.satadev_addr.qual = SATA_ADDR_PMULT_SPEC;
11965 	mutex_exit(&cportinfo->cport_mutex);
11966 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11967 	    (SATA_DIP(sata_hba_inst), &sd);
11968 	mutex_enter(&cportinfo->cport_mutex);
11969 
11970 	if (rval != SATA_SUCCESS ||
11971 	    (sd.satadev_type != SATA_DTYPE_PMULT) ||
11972 	    !(sd.satadev_state & SATA_DSTATE_PMULT_INIT)) {
11973 		SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL;
11974 		kmem_free(pmultinfo, sizeof (sata_pmult_info_t));
11975 		cportinfo->cport_state = SATA_PSTATE_FAILED;
11976 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11977 		mutex_exit(&cportinfo->cport_mutex);
11978 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
11979 		    "sata_alloc_pmult: failed to initialize pmult "
11980 		    "at port %d.", cport)
11981 		return (SATA_FAILURE);
11982 	}
11983 
11984 	/* Initialize pmport_info structure */
11985 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports;
11986 	    npmport++) {
11987 
11988 		/* if everything is allocated, skip */
11989 		if (SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) != NULL)
11990 			continue;
11991 
11992 		pmportinfo = kmem_zalloc(sizeof (sata_pmport_info_t), KM_SLEEP);
11993 		mutex_init(&pmportinfo->pmport_mutex, NULL, MUTEX_DRIVER, NULL);
11994 		mutex_exit(&cportinfo->cport_mutex);
11995 
11996 		mutex_enter(&pmportinfo->pmport_mutex);
11997 		pmportinfo->pmport_addr.cport = cport;
11998 		pmportinfo->pmport_addr.pmport = (uint8_t)npmport;
11999 		pmportinfo->pmport_addr.qual = SATA_ADDR_PMPORT;
12000 		pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
12001 		mutex_exit(&pmportinfo->pmport_mutex);
12002 
12003 		mutex_enter(&cportinfo->cport_mutex);
12004 		SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) = pmportinfo;
12005 
12006 		/* Create an attachment point */
12007 		minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip),
12008 		    cport, (uint8_t)npmport, SATA_ADDR_PMPORT);
12009 		(void) sprintf(name, "%d.%d", cport, npmport);
12010 
12011 		if (ddi_create_minor_node(dip, name, S_IFCHR, minor_number,
12012 		    DDI_NT_SATA_ATTACHMENT_POINT, 0) != DDI_SUCCESS) {
12013 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
12014 			    "cannot create SATA attachment point for "
12015 			    "port %d:%d", cport, npmport);
12016 		}
12017 	}
12018 
12019 	pmultinfo->pmult_state &= ~SATA_STATE_PROBING;
12020 	pmultinfo->pmult_state |= (SATA_STATE_PROBED|SATA_STATE_READY);
12021 	cportinfo->cport_dev_type = SATA_DTYPE_PMULT;
12022 
12023 	mutex_exit(&cportinfo->cport_mutex);
12024 	return (SATA_SUCCESS);
12025 }
12026 
12027 /*
12028  * Free data structures when a port multiplier is removed.
12029  *
12030  * NOTE: No Mutex should be hold.
12031  */
12032 static void
12033 sata_free_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
12034 {
12035 	sata_cport_info_t *cportinfo;
12036 	sata_pmult_info_t *pmultinfo;
12037 	sata_pmport_info_t *pmportinfo;
12038 	sata_device_t pmport_device;
12039 	sata_drive_info_t *sdinfo;
12040 	dev_info_t *tdip;
12041 	char name[16];
12042 	uint8_t cport = sata_device->satadev_addr.cport;
12043 	int npmport;
12044 
12045 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
12046 
12047 	/* This function might be called while port-mult is hot plugged. */
12048 	mutex_enter(&cportinfo->cport_mutex);
12049 
12050 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
12051 	pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
12052 	ASSERT(pmultinfo != NULL);
12053 
12054 	/* Free pmport_info structure */
12055 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports;
12056 	    npmport++) {
12057 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport);
12058 		if (pmportinfo == NULL)
12059 			continue;
12060 		mutex_exit(&cportinfo->cport_mutex);
12061 
12062 		mutex_enter(&pmportinfo->pmport_mutex);
12063 		sdinfo = pmportinfo->pmport_sata_drive;
12064 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
12065 		mutex_exit(&pmportinfo->pmport_mutex);
12066 
12067 		/* Remove attachment point. */
12068 		name[0] = '\0';
12069 		(void) sprintf(name, "%d.%d", cport, npmport);
12070 		ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name);
12071 		sata_log(sata_hba_inst, CE_NOTE,
12072 		    "Remove attachment point of port %d:%d",
12073 		    cport, npmport);
12074 
12075 		/*
12076 		 * Rumove target node
12077 		 */
12078 		bzero(&pmport_device, sizeof (sata_device_t));
12079 		pmport_device.satadev_rev = SATA_DEVICE_REV;
12080 		pmport_device.satadev_addr.cport = cport;
12081 		pmport_device.satadev_addr.pmport = (uint8_t)npmport;
12082 		pmport_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
12083 
12084 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
12085 		    &(pmport_device.satadev_addr));
12086 		if (tdip != NULL && ndi_devi_offline(tdip,
12087 		    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
12088 			/*
12089 			 * Problem :
12090 			 * The target node remained attached.
12091 			 * This happens when the device file was open
12092 			 * or a node was waiting for resources.
12093 			 * Cannot do anything about it.
12094 			 */
12095 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12096 			    "sata_free_pmult: could not unconfigure device "
12097 			    "before disconnecting the SATA port %d:%d",
12098 			    cport, npmport));
12099 
12100 			/*
12101 			 * Set DEVICE REMOVED state in the target
12102 			 * node. It will prevent access to the device
12103 			 * even when a new device is attached, until
12104 			 * the old target node is released, removed and
12105 			 * recreated for a new  device.
12106 			 */
12107 			sata_set_device_removed(tdip);
12108 
12109 			/*
12110 			 * Instruct event daemon to try the target
12111 			 * node cleanup later.
12112 			 */
12113 			sata_set_target_node_cleanup(
12114 			    sata_hba_inst, &(pmport_device.satadev_addr));
12115 
12116 		}
12117 		mutex_enter(&cportinfo->cport_mutex);
12118 
12119 		/*
12120 		 * Add here differentiation for device attached or not
12121 		 */
12122 		if (sdinfo != NULL)  {
12123 			sata_log(sata_hba_inst, CE_WARN,
12124 			    "SATA device detached from port %d:%d",
12125 			    cport, npmport);
12126 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
12127 		}
12128 
12129 		mutex_destroy(&pmportinfo->pmport_mutex);
12130 		kmem_free(pmportinfo, sizeof (sata_pmport_info_t));
12131 	}
12132 
12133 	kmem_free(pmultinfo, sizeof (sata_pmult_info_t));
12134 
12135 	cportinfo->cport_devp.cport_sata_pmult = NULL;
12136 
12137 	sata_log(sata_hba_inst, CE_WARN,
12138 	    "SATA port multiplier detached at port %d", cport);
12139 
12140 	mutex_exit(&cportinfo->cport_mutex);
12141 }
12142 
12143 /*
12144  * Initialize device
12145  * Specified device is initialized to a default state.
12146  *
12147  * Returns SATA_SUCCESS if all device features are set successfully,
12148  * SATA_RETRY if device is accessible but device features were not set
12149  * successfully, and SATA_FAILURE otherwise.
12150  */
12151 static int
12152 sata_initialize_device(sata_hba_inst_t *sata_hba_inst,
12153     sata_drive_info_t *sdinfo)
12154 {
12155 	int rval;
12156 
12157 	sata_save_drive_settings(sdinfo);
12158 
12159 	sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
12160 
12161 	sata_init_write_cache_mode(sdinfo);
12162 
12163 	rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0);
12164 
12165 	/* Determine current data transfer mode */
12166 	if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) {
12167 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
12168 	} else if ((sdinfo->satadrv_id.ai_validinfo &
12169 	    SATA_VALIDINFO_88) != 0 &&
12170 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) {
12171 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
12172 	} else if ((sdinfo->satadrv_id.ai_dworddma &
12173 	    SATA_MDMA_SEL_MASK) != 0) {
12174 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
12175 	} else
12176 		/* DMA supported, not no DMA transfer mode is selected !? */
12177 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
12178 
12179 	if ((sdinfo->satadrv_id.ai_cmdset83 & 0x20) &&
12180 	    (sdinfo->satadrv_id.ai_features86 & 0x20))
12181 		sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
12182 	else
12183 		sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
12184 
12185 	return (rval);
12186 }
12187 
12188 
12189 /*
12190  * Initialize write cache mode.
12191  *
12192  * The default write cache setting for SATA HDD is provided by sata_write_cache
12193  * static variable. ATAPI CD/DVDs devices have write cache default is
12194  * determined by sata_atapicdvd_write_cache static variable.
12195  * ATAPI tape devices have write cache default is determined by
12196  * sata_atapitape_write_cache static variable.
12197  * ATAPI disk devices have write cache default is determined by
12198  * sata_atapidisk_write_cache static variable.
12199  * 1 - enable
12200  * 0 - disable
12201  * any other value - current drive setting
12202  *
12203  * Although there is not reason to disable write cache on CD/DVD devices,
12204  * tape devices and ATAPI disk devices, the default setting control is provided
12205  * for the maximun flexibility.
12206  *
12207  * In the future, it may be overridden by the
12208  * disk-write-cache-enable property setting, if it is defined.
12209  * Returns SATA_SUCCESS if all device features are set successfully,
12210  * SATA_FAILURE otherwise.
12211  */
12212 static void
12213 sata_init_write_cache_mode(sata_drive_info_t *sdinfo)
12214 {
12215 	switch (sdinfo->satadrv_type) {
12216 	case SATA_DTYPE_ATADISK:
12217 		if (sata_write_cache == 1)
12218 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12219 		else if (sata_write_cache == 0)
12220 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12221 		/*
12222 		 * When sata_write_cache value is not 0 or 1,
12223 		 * a current setting of the drive's write cache is used.
12224 		 */
12225 		break;
12226 	case SATA_DTYPE_ATAPICD:
12227 		if (sata_atapicdvd_write_cache == 1)
12228 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12229 		else if (sata_atapicdvd_write_cache == 0)
12230 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12231 		/*
12232 		 * When sata_atapicdvd_write_cache value is not 0 or 1,
12233 		 * a current setting of the drive's write cache is used.
12234 		 */
12235 		break;
12236 	case SATA_DTYPE_ATAPITAPE:
12237 		if (sata_atapitape_write_cache == 1)
12238 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12239 		else if (sata_atapitape_write_cache == 0)
12240 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12241 		/*
12242 		 * When sata_atapitape_write_cache value is not 0 or 1,
12243 		 * a current setting of the drive's write cache is used.
12244 		 */
12245 		break;
12246 	case SATA_DTYPE_ATAPIDISK:
12247 		if (sata_atapidisk_write_cache == 1)
12248 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12249 		else if (sata_atapidisk_write_cache == 0)
12250 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12251 		/*
12252 		 * When sata_atapidisk_write_cache value is not 0 or 1,
12253 		 * a current setting of the drive's write cache is used.
12254 		 */
12255 		break;
12256 	}
12257 }
12258 
12259 
12260 /*
12261  * Validate sata address.
12262  * Specified cport, pmport and qualifier has to match
12263  * passed sata_scsi configuration info.
12264  * The presence of an attached device is not verified.
12265  *
12266  * Returns 0 when address is valid, -1 otherwise.
12267  */
12268 static int
12269 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport,
12270     int pmport, int qual)
12271 {
12272 	if (qual == SATA_ADDR_DCPORT && pmport != 0)
12273 		goto invalid_address;
12274 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
12275 		goto invalid_address;
12276 	if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) &&
12277 	    ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) ||
12278 	    (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) ||
12279 	    (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport))))
12280 		goto invalid_address;
12281 
12282 	return (0);
12283 
12284 invalid_address:
12285 	return (-1);
12286 
12287 }
12288 
12289 /*
12290  * Validate scsi address
12291  * SCSI target address is translated into SATA cport/pmport and compared
12292  * with a controller port/device configuration. LUN has to be 0.
12293  * Returns 0 if a scsi target refers to an attached device,
12294  * returns 1 if address is valid but no valid device is attached,
12295  * returns 2 if address is valid but device type is unknown (not valid device),
12296  * returns -1 if bad address or device is of an unsupported type.
12297  * Upon return sata_device argument is set.
12298  *
12299  * Port multiplier is supported now.
12300  */
12301 static int
12302 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst,
12303     struct scsi_address *ap, sata_device_t *sata_device)
12304 {
12305 	int cport, pmport, qual, rval;
12306 
12307 	rval = -1;	/* Invalid address */
12308 	if (ap->a_lun != 0)
12309 		goto out;
12310 
12311 	qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
12312 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
12313 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
12314 
12315 	if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT)
12316 		goto out;
12317 
12318 	if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) ==
12319 	    0) {
12320 
12321 		sata_cport_info_t *cportinfo;
12322 		sata_pmult_info_t *pmultinfo;
12323 		sata_drive_info_t *sdinfo = NULL;
12324 
12325 		sata_device->satadev_addr.qual = qual;
12326 		sata_device->satadev_addr.cport = cport;
12327 		sata_device->satadev_addr.pmport = pmport;
12328 		sata_device->satadev_rev = SATA_DEVICE_REV_1;
12329 
12330 		rval = 1;	/* Valid sata address */
12331 
12332 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
12333 		if (qual == SATA_ADDR_DCPORT) {
12334 			if (cportinfo == NULL ||
12335 			    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
12336 				goto out;
12337 
12338 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
12339 			if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN &&
12340 			    sdinfo != NULL) {
12341 				rval = 2;
12342 				goto out;
12343 			}
12344 
12345 			if ((cportinfo->cport_dev_type &
12346 			    SATA_VALID_DEV_TYPE) == 0) {
12347 				rval = -1;
12348 				goto out;
12349 			}
12350 
12351 		} else if (qual == SATA_ADDR_DPMPORT) {
12352 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
12353 			if (pmultinfo == NULL) {
12354 				rval = -1;
12355 				goto out;
12356 			}
12357 			if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) ==
12358 			    NULL ||
12359 			    SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12360 			    pmport) == SATA_DTYPE_NONE)
12361 				goto out;
12362 
12363 			sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport,
12364 			    pmport);
12365 			if (SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12366 			    pmport) == SATA_DTYPE_UNKNOWN && sdinfo != NULL) {
12367 				rval = 2;
12368 				goto out;
12369 			}
12370 
12371 			if ((SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12372 			    pmport) & SATA_VALID_DEV_TYPE) == 0) {
12373 				rval = -1;
12374 				goto out;
12375 			}
12376 
12377 		} else {
12378 			rval = -1;
12379 			goto out;
12380 		}
12381 		if ((sdinfo == NULL) ||
12382 		    (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0)
12383 			goto out;
12384 
12385 		sata_device->satadev_type = sdinfo->satadrv_type;
12386 
12387 		return (0);
12388 	}
12389 out:
12390 	if (rval > 0) {
12391 		SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
12392 		    "sata_validate_scsi_address: no valid target %x lun %x",
12393 		    ap->a_target, ap->a_lun);
12394 	}
12395 	return (rval);
12396 }
12397 
12398 /*
12399  * Find dip corresponding to passed device number
12400  *
12401  * Returns NULL if invalid device number is passed or device cannot be found,
12402  * Returns dip is device is found.
12403  */
12404 static dev_info_t *
12405 sata_devt_to_devinfo(dev_t dev)
12406 {
12407 	dev_info_t *dip;
12408 #ifndef __lock_lint
12409 	struct devnames *dnp;
12410 	major_t major = getmajor(dev);
12411 	int instance = SATA_MINOR2INSTANCE(getminor(dev));
12412 
12413 	if (major >= devcnt)
12414 		return (NULL);
12415 
12416 	dnp = &devnamesp[major];
12417 	LOCK_DEV_OPS(&(dnp->dn_lock));
12418 	dip = dnp->dn_head;
12419 	while (dip && (ddi_get_instance(dip) != instance)) {
12420 		dip = ddi_get_next(dip);
12421 	}
12422 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
12423 #endif
12424 
12425 	return (dip);
12426 }
12427 
12428 
12429 /*
12430  * Probe device.
12431  * This function issues Identify Device command and initializes local
12432  * sata_drive_info structure if the device can be identified.
12433  * The device type is determined by examining Identify Device
12434  * command response.
12435  * If the sata_hba_inst has linked drive info structure for this
12436  * device address, the Identify Device data is stored into sata_drive_info
12437  * structure linked to the port info structure.
12438  *
12439  * sata_device has to refer to the valid sata port(s) for HBA described
12440  * by sata_hba_inst structure.
12441  *
12442  * Returns:
12443  *	SATA_SUCCESS if device type was successfully probed and port-linked
12444  *		drive info structure was updated;
12445  *	SATA_FAILURE if there is no device, or device was not probed
12446  *		successully;
12447  *	SATA_RETRY if device probe can be retried later.
12448  * If a device cannot be identified, sata_device's dev_state and dev_type
12449  * fields are set to unknown.
12450  * There are no retries in this function. Any retries should be managed by
12451  * the caller.
12452  */
12453 
12454 
12455 static int
12456 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
12457 {
12458 	sata_pmport_info_t *pmportinfo;
12459 	sata_drive_info_t *sdinfo;
12460 	sata_drive_info_t new_sdinfo;	/* local drive info struct */
12461 	int rval;
12462 
12463 	ASSERT((SATA_CPORT_STATE(sata_hba_inst,
12464 	    sata_device->satadev_addr.cport) &
12465 	    (SATA_STATE_PROBED | SATA_STATE_READY)) != 0);
12466 
12467 	sata_device->satadev_type = SATA_DTYPE_NONE;
12468 
12469 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12470 	    sata_device->satadev_addr.cport)));
12471 
12472 	if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT) {
12473 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
12474 		    sata_device->satadev_addr.cport,
12475 		    sata_device->satadev_addr.pmport);
12476 		ASSERT(pmportinfo != NULL);
12477 	}
12478 
12479 	/* Get pointer to port-linked sata device info structure */
12480 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12481 	if (sdinfo != NULL) {
12482 		sdinfo->satadrv_state &=
12483 		    ~(SATA_STATE_PROBED | SATA_STATE_READY);
12484 		sdinfo->satadrv_state |= SATA_STATE_PROBING;
12485 	} else {
12486 		/* No device to probe */
12487 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12488 		    sata_device->satadev_addr.cport)));
12489 		sata_device->satadev_type = SATA_DTYPE_NONE;
12490 		sata_device->satadev_state = SATA_STATE_UNKNOWN;
12491 		return (SATA_FAILURE);
12492 	}
12493 	/*
12494 	 * Need to issue both types of identify device command and
12495 	 * determine device type by examining retreived data/status.
12496 	 * First, ATA Identify Device.
12497 	 */
12498 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
12499 	new_sdinfo.satadrv_addr = sata_device->satadev_addr;
12500 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12501 	    sata_device->satadev_addr.cport)));
12502 	new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
12503 	rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
12504 	if (rval == SATA_RETRY) {
12505 		/* We may try to check for ATAPI device */
12506 		if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) {
12507 			/*
12508 			 * HBA supports ATAPI - try to issue Identify Packet
12509 			 * Device command.
12510 			 */
12511 			new_sdinfo.satadrv_type = SATA_DTYPE_ATAPI;
12512 			rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
12513 		}
12514 	}
12515 	if (rval == SATA_SUCCESS) {
12516 		/*
12517 		 * Got something responding positively to ATA Identify Device
12518 		 * or to Identify Packet Device cmd.
12519 		 * Save last used device type.
12520 		 */
12521 		sata_device->satadev_type = new_sdinfo.satadrv_type;
12522 
12523 		/* save device info, if possible */
12524 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12525 		    sata_device->satadev_addr.cport)));
12526 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12527 		if (sdinfo == NULL) {
12528 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12529 			    sata_device->satadev_addr.cport)));
12530 			return (SATA_FAILURE);
12531 		}
12532 		/*
12533 		 * Copy drive info into the port-linked drive info structure.
12534 		 */
12535 		*sdinfo = new_sdinfo;
12536 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
12537 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
12538 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
12539 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
12540 			    sata_device->satadev_addr.cport) =
12541 			    sdinfo->satadrv_type;
12542 		else { /* SATA_ADDR_DPMPORT */
12543 			mutex_enter(&pmportinfo->pmport_mutex);
12544 			SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12545 			    sata_device->satadev_addr.cport,
12546 			    sata_device->satadev_addr.pmport) =
12547 			    sdinfo->satadrv_type;
12548 			mutex_exit(&pmportinfo->pmport_mutex);
12549 		}
12550 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12551 		    sata_device->satadev_addr.cport)));
12552 		return (SATA_SUCCESS);
12553 	}
12554 
12555 	/*
12556 	 * It may be SATA_RETRY or SATA_FAILURE return.
12557 	 * Looks like we cannot determine the device type at this time.
12558 	 */
12559 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12560 	    sata_device->satadev_addr.cport)));
12561 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12562 	if (sdinfo != NULL) {
12563 		sata_device->satadev_type = SATA_DTYPE_UNKNOWN;
12564 		sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12565 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
12566 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
12567 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
12568 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
12569 			    sata_device->satadev_addr.cport) =
12570 			    SATA_DTYPE_UNKNOWN;
12571 		else {
12572 			/* SATA_ADDR_DPMPORT */
12573 			mutex_enter(&pmportinfo->pmport_mutex);
12574 			if ((SATA_PMULT_INFO(sata_hba_inst,
12575 			    sata_device->satadev_addr.cport) != NULL) &&
12576 			    (SATA_PMPORT_INFO(sata_hba_inst,
12577 			    sata_device->satadev_addr.cport,
12578 			    sata_device->satadev_addr.pmport) != NULL))
12579 				SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12580 				    sata_device->satadev_addr.cport,
12581 				    sata_device->satadev_addr.pmport) =
12582 				    SATA_DTYPE_UNKNOWN;
12583 			mutex_exit(&pmportinfo->pmport_mutex);
12584 		}
12585 	}
12586 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12587 	    sata_device->satadev_addr.cport)));
12588 	return (rval);
12589 }
12590 
12591 
12592 /*
12593  * Get pointer to sata_drive_info structure.
12594  *
12595  * The sata_device has to contain address (cport, pmport and qualifier) for
12596  * specified sata_scsi structure.
12597  *
12598  * Returns NULL if device address is not valid for this HBA configuration.
12599  * Otherwise, returns a pointer to sata_drive_info structure.
12600  *
12601  * This function should be called with a port mutex held.
12602  */
12603 static sata_drive_info_t *
12604 sata_get_device_info(sata_hba_inst_t *sata_hba_inst,
12605     sata_device_t *sata_device)
12606 {
12607 	uint8_t cport = sata_device->satadev_addr.cport;
12608 	uint8_t pmport = sata_device->satadev_addr.pmport;
12609 	uint8_t qual = sata_device->satadev_addr.qual;
12610 
12611 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
12612 		return (NULL);
12613 
12614 	if (!(SATA_CPORT_STATE(sata_hba_inst, cport) &
12615 	    (SATA_STATE_PROBED | SATA_STATE_READY)))
12616 		/* Port not probed yet */
12617 		return (NULL);
12618 
12619 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE)
12620 		return (NULL);
12621 
12622 	if (qual == SATA_ADDR_DCPORT) {
12623 		/* Request for a device on a controller port */
12624 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
12625 		    SATA_DTYPE_PMULT)
12626 			/* Port multiplier attached */
12627 			return (NULL);
12628 		return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport));
12629 	}
12630 	if (qual == SATA_ADDR_DPMPORT) {
12631 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
12632 		    SATA_DTYPE_PMULT)
12633 			return (NULL);
12634 
12635 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport))
12636 			return (NULL);
12637 
12638 		if (!(SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) &
12639 		    (SATA_STATE_PROBED | SATA_STATE_READY)))
12640 			/* Port multiplier port not probed yet */
12641 			return (NULL);
12642 
12643 		return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport));
12644 	}
12645 
12646 	/* we should not get here */
12647 	return (NULL);
12648 }
12649 
12650 
12651 /*
12652  * sata_identify_device.
12653  * Send Identify Device command to SATA HBA driver.
12654  * If command executes successfully, update sata_drive_info structure pointed
12655  * to by sdinfo argument, including Identify Device data.
12656  * If command fails, invalidate data in sata_drive_info.
12657  *
12658  * Cannot be called from interrupt level.
12659  *
12660  * Returns:
12661  * SATA_SUCCESS if the device was identified as a supported device,
12662  * SATA_RETRY if the device was not identified but could be retried,
12663  * SATA_FAILURE if the device was not identified and identify attempt
12664  *	should not be retried.
12665  */
12666 static int
12667 sata_identify_device(sata_hba_inst_t *sata_hba_inst,
12668     sata_drive_info_t *sdinfo)
12669 {
12670 	uint16_t cfg_word;
12671 	int rval;
12672 
12673 	/* fetch device identify data */
12674 	if ((rval = sata_fetch_device_identify_data(sata_hba_inst,
12675 	    sdinfo)) != SATA_SUCCESS)
12676 		goto fail_unknown;
12677 
12678 	cfg_word = sdinfo->satadrv_id.ai_config;
12679 
12680 	/* Set the correct device type */
12681 	if ((cfg_word & SATA_ATA_TYPE_MASK) == SATA_ATA_TYPE) {
12682 		sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
12683 	} else if (cfg_word == SATA_CFA_TYPE) {
12684 		/* It's a Compact Flash media via CF-to-SATA HDD adapter */
12685 		sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
12686 	} else if ((cfg_word & SATA_ATAPI_TYPE_MASK) == SATA_ATAPI_TYPE) {
12687 		switch (cfg_word & SATA_ATAPI_ID_DEV_TYPE) {
12688 		case SATA_ATAPI_CDROM_DEV:
12689 			sdinfo->satadrv_type = SATA_DTYPE_ATAPICD;
12690 			break;
12691 		case SATA_ATAPI_SQACC_DEV:
12692 			sdinfo->satadrv_type = SATA_DTYPE_ATAPITAPE;
12693 			break;
12694 		case SATA_ATAPI_DIRACC_DEV:
12695 			sdinfo->satadrv_type = SATA_DTYPE_ATAPIDISK;
12696 			break;
12697 		case SATA_ATAPI_PROC_DEV:
12698 			sdinfo->satadrv_type = SATA_DTYPE_ATAPIPROC;
12699 			break;
12700 		default:
12701 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12702 		}
12703 	} else {
12704 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12705 	}
12706 
12707 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12708 		if (sdinfo->satadrv_capacity == 0) {
12709 			/* Non-LBA disk. Too bad... */
12710 			sata_log(sata_hba_inst, CE_WARN,
12711 			    "SATA disk device at port %d does not support LBA",
12712 			    sdinfo->satadrv_addr.cport);
12713 			rval = SATA_FAILURE;
12714 			goto fail_unknown;
12715 		}
12716 	}
12717 #if 0
12718 	/* Left for historical reason */
12719 	/*
12720 	 * Some initial version of SATA spec indicated that at least
12721 	 * UDMA mode 4 has to be supported. It is not metioned in
12722 	 * SerialATA 2.6, so this restriction is removed.
12723 	 */
12724 	/* Check for Ultra DMA modes 6 through 0 being supported */
12725 	for (i = 6; i >= 0; --i) {
12726 		if (sdinfo->satadrv_id.ai_ultradma & (1 << i))
12727 			break;
12728 	}
12729 
12730 	/*
12731 	 * At least UDMA 4 mode has to be supported. If mode 4 or
12732 	 * higher are not supported by the device, fail this
12733 	 * device.
12734 	 */
12735 	if (i < 4) {
12736 		/* No required Ultra DMA mode supported */
12737 		sata_log(sata_hba_inst, CE_WARN,
12738 		    "SATA disk device at port %d does not support UDMA "
12739 		    "mode 4 or higher", sdinfo->satadrv_addr.cport);
12740 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12741 		    "mode 4 or higher required, %d supported", i));
12742 		rval = SATA_FAILURE;
12743 		goto fail_unknown;
12744 	}
12745 #endif
12746 
12747 	/*
12748 	 * For Disk devices, if it doesn't support UDMA mode, we would
12749 	 * like to return failure directly.
12750 	 */
12751 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
12752 	    !((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
12753 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0)) {
12754 		sata_log(sata_hba_inst, CE_WARN,
12755 		    "SATA disk device at port %d does not support UDMA",
12756 		    sdinfo->satadrv_addr.cport);
12757 		rval = SATA_FAILURE;
12758 		goto fail_unknown;
12759 	}
12760 
12761 	return (SATA_SUCCESS);
12762 
12763 fail_unknown:
12764 	/* Invalidate sata_drive_info ? */
12765 	sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12766 	sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
12767 	return (rval);
12768 }
12769 
12770 /*
12771  * Log/display device information
12772  */
12773 static void
12774 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst,
12775     sata_drive_info_t *sdinfo)
12776 {
12777 	int valid_version;
12778 	char msg_buf[MAXPATHLEN];
12779 	int i;
12780 
12781 	/* Show HBA path */
12782 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf);
12783 
12784 	cmn_err(CE_CONT, "?%s :\n", msg_buf);
12785 
12786 	switch (sdinfo->satadrv_type) {
12787 	case SATA_DTYPE_ATADISK:
12788 		(void) sprintf(msg_buf, "SATA disk device at");
12789 		break;
12790 
12791 	case SATA_DTYPE_ATAPICD:
12792 		(void) sprintf(msg_buf, "SATA CD/DVD (ATAPI) device at");
12793 		break;
12794 
12795 	case SATA_DTYPE_ATAPITAPE:
12796 		(void) sprintf(msg_buf, "SATA tape (ATAPI) device at");
12797 		break;
12798 
12799 	case SATA_DTYPE_ATAPIDISK:
12800 		(void) sprintf(msg_buf, "SATA disk (ATAPI) device at");
12801 		break;
12802 
12803 	case SATA_DTYPE_ATAPIPROC:
12804 		(void) sprintf(msg_buf, "SATA processor (ATAPI) device at");
12805 		break;
12806 
12807 	case SATA_DTYPE_UNKNOWN:
12808 		(void) sprintf(msg_buf,
12809 		    "Unsupported SATA device type (cfg 0x%x) at ",
12810 		    sdinfo->satadrv_id.ai_config);
12811 		break;
12812 	}
12813 
12814 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT)
12815 		cmn_err(CE_CONT, "?\t%s port %d\n",
12816 		    msg_buf, sdinfo->satadrv_addr.cport);
12817 	else
12818 		cmn_err(CE_CONT, "?\t%s port %d:%d\n",
12819 		    msg_buf, sdinfo->satadrv_addr.cport,
12820 		    sdinfo->satadrv_addr.pmport);
12821 
12822 	bcopy(&sdinfo->satadrv_id.ai_model, msg_buf,
12823 	    sizeof (sdinfo->satadrv_id.ai_model));
12824 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model));
12825 	msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
12826 	cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf);
12827 
12828 	bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf,
12829 	    sizeof (sdinfo->satadrv_id.ai_fw));
12830 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw));
12831 	msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
12832 	cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf);
12833 
12834 	bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf,
12835 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12836 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser));
12837 	msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
12838 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12839 		cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
12840 	} else {
12841 		/*
12842 		 * Some drives do not implement serial number and may
12843 		 * violate the spec by providing spaces rather than zeros
12844 		 * in serial number field. Scan the buffer to detect it.
12845 		 */
12846 		for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) {
12847 			if (msg_buf[i] != '\0' && msg_buf[i] != ' ')
12848 				break;
12849 		}
12850 		if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) {
12851 			cmn_err(CE_CONT, "?\tserial number - none\n");
12852 		} else {
12853 			cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
12854 		}
12855 	}
12856 
12857 #ifdef SATA_DEBUG
12858 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
12859 	    sdinfo->satadrv_id.ai_majorversion != 0xffff) {
12860 		int i;
12861 		for (i = 14; i >= 2; i--) {
12862 			if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) {
12863 				valid_version = i;
12864 				break;
12865 			}
12866 		}
12867 		cmn_err(CE_CONT,
12868 		    "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
12869 		    valid_version,
12870 		    sdinfo->satadrv_id.ai_majorversion,
12871 		    sdinfo->satadrv_id.ai_minorversion);
12872 	}
12873 #endif
12874 	/* Log some info */
12875 	cmn_err(CE_CONT, "?\tsupported features:\n");
12876 	msg_buf[0] = '\0';
12877 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12878 		if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48)
12879 			(void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN);
12880 		else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28)
12881 			(void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN);
12882 	}
12883 	if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA)
12884 		(void) strlcat(msg_buf, "DMA", MAXPATHLEN);
12885 	if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ)
12886 		(void) strlcat(msg_buf, ", Native Command Queueing",
12887 		    MAXPATHLEN);
12888 	if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)
12889 		(void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN);
12890 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) &&
12891 	    (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED))
12892 		(void) strlcat(msg_buf, ", SMART", MAXPATHLEN);
12893 	if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) &&
12894 	    (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED))
12895 		(void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN);
12896 	cmn_err(CE_CONT, "?\t %s\n", msg_buf);
12897 	if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA3)
12898 		cmn_err(CE_CONT, "?\tSATA Gen3 signaling speed (6.0Gbps)\n");
12899 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2)
12900 		cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n");
12901 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1)
12902 		cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n");
12903 	if (sdinfo->satadrv_features_support &
12904 	    (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) {
12905 		msg_buf[0] = '\0';
12906 		(void) snprintf(msg_buf, MAXPATHLEN,
12907 		    "Supported queue depth %d",
12908 		    sdinfo->satadrv_queue_depth);
12909 		if (!(sata_func_enable &
12910 		    (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ)))
12911 			(void) strlcat(msg_buf,
12912 			    " - queueing disabled globally", MAXPATHLEN);
12913 		else if (sdinfo->satadrv_queue_depth >
12914 		    sdinfo->satadrv_max_queue_depth) {
12915 			(void) snprintf(&msg_buf[strlen(msg_buf)],
12916 			    MAXPATHLEN - strlen(msg_buf), ", limited to %d",
12917 			    (int)sdinfo->satadrv_max_queue_depth);
12918 		}
12919 		cmn_err(CE_CONT, "?\t%s\n", msg_buf);
12920 	}
12921 
12922 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12923 #ifdef __i386
12924 		(void) sprintf(msg_buf, "\tcapacity = %llu sectors\n",
12925 		    sdinfo->satadrv_capacity);
12926 #else
12927 		(void) sprintf(msg_buf, "\tcapacity = %lu sectors\n",
12928 		    sdinfo->satadrv_capacity);
12929 #endif
12930 		cmn_err(CE_CONT, "?%s", msg_buf);
12931 	}
12932 }
12933 
12934 /*
12935  * Log/display port multiplier information
12936  * No Mutex should be hold.
12937  */
12938 static void
12939 sata_show_pmult_info(sata_hba_inst_t *sata_hba_inst,
12940     sata_device_t *sata_device)
12941 {
12942 	_NOTE(ARGUNUSED(sata_hba_inst))
12943 
12944 	int cport = sata_device->satadev_addr.cport;
12945 	sata_pmult_info_t *pmultinfo;
12946 	char msg_buf[MAXPATHLEN];
12947 	uint32_t gscr0, gscr1, gscr2, gscr64;
12948 
12949 	mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12950 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
12951 	if (pmultinfo == NULL) {
12952 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12953 		return;
12954 	}
12955 
12956 	gscr0 = pmultinfo->pmult_gscr.gscr0;
12957 	gscr1 = pmultinfo->pmult_gscr.gscr1;
12958 	gscr2 = pmultinfo->pmult_gscr.gscr2;
12959 	gscr64 = pmultinfo->pmult_gscr.gscr64;
12960 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12961 
12962 	cmn_err(CE_CONT, "?Port Multiplier %d device-ports found at port %d",
12963 	    sata_device->satadev_add_info, sata_device->satadev_addr.cport);
12964 
12965 	(void) sprintf(msg_buf, "\tVendor_ID 0x%04x, Module_ID 0x%04x",
12966 	    gscr0 & 0xffff, (gscr0 >> 16) & 0xffff);
12967 	cmn_err(CE_CONT, "?%s", msg_buf);
12968 
12969 	(void) strcpy(msg_buf, "\tSupport SATA PMP Spec ");
12970 	if (gscr1 & (1 << 3))
12971 		(void) strlcat(msg_buf, "1.2", MAXPATHLEN);
12972 	else if (gscr1 & (1 << 2))
12973 		(void) strlcat(msg_buf, "1.1", MAXPATHLEN);
12974 	else if (gscr1 & (1 << 1))
12975 		(void) strlcat(msg_buf, "1.0", MAXPATHLEN);
12976 	else
12977 		(void) strlcat(msg_buf, "unknown", MAXPATHLEN);
12978 	cmn_err(CE_CONT, "?%s", msg_buf);
12979 
12980 	(void) strcpy(msg_buf, "\tSupport ");
12981 	if (gscr64 & (1 << 3))
12982 		(void) strlcat(msg_buf, "Asy-Notif, ",
12983 		    MAXPATHLEN);
12984 	if (gscr64 & (1 << 2))
12985 		(void) strlcat(msg_buf, "Dyn-SSC, ", MAXPATHLEN);
12986 	if (gscr64 & (1 << 1))
12987 		(void) strlcat(msg_buf, "Iss-PMREQ, ", MAXPATHLEN);
12988 	if (gscr64 & (1 << 0))
12989 		(void) strlcat(msg_buf, "BIST", MAXPATHLEN);
12990 	if ((gscr64 & 0xf) == 0)
12991 		(void) strlcat(msg_buf, "nothing", MAXPATHLEN);
12992 	cmn_err(CE_CONT, "?%s", msg_buf);
12993 
12994 	(void) sprintf(msg_buf, "\tNumber of exposed device fan-out ports: %d",
12995 	    gscr2 & SATA_PMULT_PORTNUM_MASK);
12996 	cmn_err(CE_CONT, "?%s", msg_buf);
12997 }
12998 
12999 /*
13000  * sata_save_drive_settings extracts current setting of the device and stores
13001  * it for future reference, in case the device setup would need to be restored
13002  * after the device reset.
13003  *
13004  * For all devices read ahead and write cache settings are saved, if the
13005  * device supports these features at all.
13006  * For ATAPI devices the Removable Media Status Notification setting is saved.
13007  */
13008 static void
13009 sata_save_drive_settings(sata_drive_info_t *sdinfo)
13010 {
13011 	if (SATA_READ_AHEAD_SUPPORTED(sdinfo->satadrv_id) ||
13012 	    SATA_WRITE_CACHE_SUPPORTED(sdinfo->satadrv_id)) {
13013 
13014 		/* Current setting of Read Ahead (and Read Cache) */
13015 		if (SATA_READ_AHEAD_ENABLED(sdinfo->satadrv_id))
13016 			sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
13017 		else
13018 			sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD;
13019 
13020 		/* Current setting of Write Cache */
13021 		if (SATA_WRITE_CACHE_ENABLED(sdinfo->satadrv_id))
13022 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
13023 		else
13024 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
13025 	}
13026 
13027 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
13028 		if (SATA_RM_NOTIFIC_SUPPORTED(sdinfo->satadrv_id))
13029 			sdinfo->satadrv_settings |= SATA_DEV_RMSN;
13030 		else
13031 			sdinfo->satadrv_settings &= ~SATA_DEV_RMSN;
13032 	}
13033 }
13034 
13035 
13036 /*
13037  * sata_check_capacity function determines a disk capacity
13038  * and addressing mode (LBA28/LBA48) by examining a disk identify device data.
13039  *
13040  * NOTE: CHS mode is not supported! If a device does not support LBA,
13041  * this function is not called.
13042  *
13043  * Returns device capacity in number of blocks, i.e. largest addressable LBA+1
13044  */
13045 static uint64_t
13046 sata_check_capacity(sata_drive_info_t *sdinfo)
13047 {
13048 	uint64_t capacity = 0;
13049 	int i;
13050 
13051 	if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK ||
13052 	    !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT)
13053 		/* Capacity valid only for LBA-addressable disk devices */
13054 		return (0);
13055 
13056 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) &&
13057 	    (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) &&
13058 	    (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) {
13059 		/* LBA48 mode supported and enabled */
13060 		sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 |
13061 		    SATA_DEV_F_LBA28;
13062 		for (i = 3;  i >= 0;  --i) {
13063 			capacity <<= 16;
13064 			capacity += sdinfo->satadrv_id.ai_addrsecxt[i];
13065 		}
13066 	} else {
13067 		capacity = sdinfo->satadrv_id.ai_addrsec[1];
13068 		capacity <<= 16;
13069 		capacity += sdinfo->satadrv_id.ai_addrsec[0];
13070 		if (capacity >= 0x1000000)
13071 			/* LBA28 mode */
13072 			sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28;
13073 	}
13074 	return (capacity);
13075 }
13076 
13077 
13078 /*
13079  * Allocate consistent buffer for DMA transfer
13080  *
13081  * Cannot be called from interrupt level or with mutex held - it may sleep.
13082  *
13083  * Returns pointer to allocated buffer structure, or NULL if allocation failed.
13084  */
13085 static struct buf *
13086 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len)
13087 {
13088 	struct scsi_address ap;
13089 	struct buf *bp;
13090 	ddi_dma_attr_t	cur_dma_attr;
13091 
13092 	ASSERT(spx->txlt_sata_pkt != NULL);
13093 	ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran;
13094 	ap.a_target = SATA_TO_SCSI_TARGET(
13095 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport,
13096 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport,
13097 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual);
13098 	ap.a_lun = 0;
13099 
13100 	bp = scsi_alloc_consistent_buf(&ap, NULL, len,
13101 	    B_READ, SLEEP_FUNC, NULL);
13102 
13103 	if (bp != NULL) {
13104 		/* Allocate DMA resources for this buffer */
13105 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
13106 		/*
13107 		 * We use a local version of the dma_attr, to account
13108 		 * for a device addressing limitations.
13109 		 * sata_adjust_dma_attr() will handle sdinfo == NULL which
13110 		 * will cause dma attributes to be adjusted to a lowest
13111 		 * acceptable level.
13112 		 */
13113 		sata_adjust_dma_attr(NULL,
13114 		    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
13115 
13116 		if (sata_dma_buf_setup(spx, PKT_CONSISTENT,
13117 		    SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) {
13118 			scsi_free_consistent_buf(bp);
13119 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
13120 			bp = NULL;
13121 		}
13122 	}
13123 	return (bp);
13124 }
13125 
13126 /*
13127  * Release local buffer (consistent buffer for DMA transfer) allocated
13128  * via sata_alloc_local_buffer().
13129  */
13130 static void
13131 sata_free_local_buffer(sata_pkt_txlate_t *spx)
13132 {
13133 	ASSERT(spx->txlt_sata_pkt != NULL);
13134 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL);
13135 
13136 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0;
13137 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL;
13138 
13139 	sata_common_free_dma_rsrcs(spx);
13140 
13141 	/* Free buffer */
13142 	scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp);
13143 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
13144 }
13145 
13146 /*
13147  * Allocate sata_pkt
13148  * Pkt structure version and embedded strcutures version are initialized.
13149  * sata_pkt and sata_pkt_txlate structures are cross-linked.
13150  *
13151  * Since this may be called in interrupt context by sata_scsi_init_pkt,
13152  * callback argument determines if it can sleep or not.
13153  * Hence, it should not be called from interrupt context.
13154  *
13155  * If successful, non-NULL pointer to a sata pkt is returned.
13156  * Upon failure, NULL pointer is returned.
13157  */
13158 static sata_pkt_t *
13159 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t))
13160 {
13161 	sata_pkt_t *spkt;
13162 	int kmsflag;
13163 
13164 	kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
13165 	spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag);
13166 	if (spkt == NULL) {
13167 		SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13168 		    "sata_pkt_alloc: failed"));
13169 		return (NULL);
13170 	}
13171 	spkt->satapkt_rev = SATA_PKT_REV;
13172 	spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV;
13173 	spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
13174 	spkt->satapkt_framework_private = spx;
13175 	spx->txlt_sata_pkt = spkt;
13176 	return (spkt);
13177 }
13178 
13179 /*
13180  * Free sata pkt allocated via sata_pkt_alloc()
13181  */
13182 static void
13183 sata_pkt_free(sata_pkt_txlate_t *spx)
13184 {
13185 	ASSERT(spx->txlt_sata_pkt != NULL);
13186 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL);
13187 	kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t));
13188 	spx->txlt_sata_pkt = NULL;
13189 }
13190 
13191 
13192 /*
13193  * Adjust DMA attributes.
13194  * SCSI cmds block count is up to 24 bits, SATA cmd block count vary
13195  * from 8 bits to 16 bits, depending on a command being used.
13196  * Limiting max block count arbitrarily to 256 for all read/write
13197  * commands may affects performance, so check both the device and
13198  * controller capability before adjusting dma attributes.
13199  */
13200 void
13201 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr,
13202     ddi_dma_attr_t *adj_dma_attr)
13203 {
13204 	uint32_t count_max;
13205 
13206 	/* Copy original attributes */
13207 	*adj_dma_attr = *dma_attr;
13208 	/*
13209 	 * Things to consider: device addressing capability,
13210 	 * "excessive" controller DMA capabilities.
13211 	 * If a device is being probed/initialized, there are
13212 	 * no device info - use default limits then.
13213 	 */
13214 	if (sdinfo == NULL) {
13215 		count_max = dma_attr->dma_attr_granular * 0x100;
13216 		if (dma_attr->dma_attr_count_max > count_max)
13217 			adj_dma_attr->dma_attr_count_max = count_max;
13218 		if (dma_attr->dma_attr_maxxfer > count_max)
13219 			adj_dma_attr->dma_attr_maxxfer = count_max;
13220 		return;
13221 	}
13222 
13223 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
13224 		if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) {
13225 			/*
13226 			 * 16-bit sector count may be used - we rely on
13227 			 * the assumption that only read and write cmds
13228 			 * will request more than 256 sectors worth of data
13229 			 */
13230 			count_max = adj_dma_attr->dma_attr_granular * 0x10000;
13231 		} else {
13232 			/*
13233 			 * 8-bit sector count will be used - default limits
13234 			 * for dma attributes
13235 			 */
13236 			count_max = adj_dma_attr->dma_attr_granular * 0x100;
13237 		}
13238 		/*
13239 		 * Adjust controler dma attributes, if necessary
13240 		 */
13241 		if (dma_attr->dma_attr_count_max > count_max)
13242 			adj_dma_attr->dma_attr_count_max = count_max;
13243 		if (dma_attr->dma_attr_maxxfer > count_max)
13244 			adj_dma_attr->dma_attr_maxxfer = count_max;
13245 	}
13246 }
13247 
13248 
13249 /*
13250  * Allocate DMA resources for the buffer
13251  * This function handles initial DMA resource allocation as well as
13252  * DMA window shift and may be called repeatedly for the same DMA window
13253  * until all DMA cookies in the DMA window are processed.
13254  * To guarantee that there is always a coherent set of cookies to process
13255  * by SATA HBA driver (observing alignment, device granularity, etc.),
13256  * the number of slots for DMA cookies is equal to lesser of  a number of
13257  * cookies in a DMA window and a max number of scatter/gather entries.
13258  *
13259  * Returns DDI_SUCCESS upon successful operation.
13260  * Return failure code of a failing command or DDI_FAILURE when
13261  * internal cleanup failed.
13262  */
13263 static int
13264 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags,
13265     int (*callback)(caddr_t), caddr_t arg,
13266     ddi_dma_attr_t *cur_dma_attr)
13267 {
13268 	int	rval;
13269 	off_t	offset;
13270 	size_t	size;
13271 	int	max_sg_len, req_len, i;
13272 	uint_t	dma_flags;
13273 	struct buf	*bp;
13274 	uint64_t	cur_txfer_len;
13275 
13276 
13277 	ASSERT(spx->txlt_sata_pkt != NULL);
13278 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
13279 	ASSERT(bp != NULL);
13280 
13281 
13282 	if (spx->txlt_buf_dma_handle == NULL) {
13283 		/*
13284 		 * No DMA resources allocated so far - this is a first call
13285 		 * for this sata pkt.
13286 		 */
13287 		rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst),
13288 		    cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle);
13289 
13290 		if (rval != DDI_SUCCESS) {
13291 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13292 			    "sata_dma_buf_setup: no buf DMA resources %x",
13293 			    rval));
13294 			return (rval);
13295 		}
13296 
13297 		if (bp->b_flags & B_READ)
13298 			dma_flags = DDI_DMA_READ;
13299 		else
13300 			dma_flags = DDI_DMA_WRITE;
13301 
13302 		if (flags & PKT_CONSISTENT)
13303 			dma_flags |= DDI_DMA_CONSISTENT;
13304 
13305 		if (flags & PKT_DMA_PARTIAL)
13306 			dma_flags |= DDI_DMA_PARTIAL;
13307 
13308 		/*
13309 		 * Check buffer alignment and size against dma attributes
13310 		 * Consider dma_attr_align only. There may be requests
13311 		 * with the size lower than device granularity, but they
13312 		 * will not read/write from/to the device, so no adjustment
13313 		 * is necessary. The dma_attr_minxfer theoretically should
13314 		 * be considered, but no HBA driver is checking it.
13315 		 */
13316 		if (IS_P2ALIGNED(bp->b_un.b_addr,
13317 		    cur_dma_attr->dma_attr_align)) {
13318 			rval = ddi_dma_buf_bind_handle(
13319 			    spx->txlt_buf_dma_handle,
13320 			    bp, dma_flags, callback, arg,
13321 			    &spx->txlt_dma_cookie,
13322 			    &spx->txlt_curwin_num_dma_cookies);
13323 		} else { /* Buffer is not aligned */
13324 
13325 			int	(*ddicallback)(caddr_t);
13326 			size_t	bufsz;
13327 
13328 			/* Check id sleeping is allowed */
13329 			ddicallback = (callback == NULL_FUNC) ?
13330 			    DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
13331 
13332 			SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13333 			    "mis-aligned buffer: addr=0x%p, cnt=%lu",
13334 			    (void *)bp->b_un.b_addr, bp->b_bcount);
13335 
13336 			if (bp->b_flags & (B_PAGEIO|B_PHYS))
13337 				/*
13338 				 * CPU will need to access data in the buffer
13339 				 * (for copying) so map it.
13340 				 */
13341 				bp_mapin(bp);
13342 
13343 			ASSERT(spx->txlt_tmp_buf == NULL);
13344 
13345 			/* Buffer may be padded by ddi_dma_mem_alloc()! */
13346 			rval = ddi_dma_mem_alloc(
13347 			    spx->txlt_buf_dma_handle,
13348 			    bp->b_bcount,
13349 			    &sata_acc_attr,
13350 			    DDI_DMA_STREAMING,
13351 			    ddicallback, NULL,
13352 			    &spx->txlt_tmp_buf,
13353 			    &bufsz,
13354 			    &spx->txlt_tmp_buf_handle);
13355 
13356 			if (rval != DDI_SUCCESS) {
13357 				/* DMA mapping failed */
13358 				(void) ddi_dma_free_handle(
13359 				    &spx->txlt_buf_dma_handle);
13360 				spx->txlt_buf_dma_handle = NULL;
13361 #ifdef SATA_DEBUG
13362 				mbuffail_count++;
13363 #endif
13364 				SATADBG1(SATA_DBG_DMA_SETUP,
13365 				    spx->txlt_sata_hba_inst,
13366 				    "sata_dma_buf_setup: "
13367 				    "buf dma mem alloc failed %x\n", rval);
13368 				return (rval);
13369 			}
13370 			ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf,
13371 			    cur_dma_attr->dma_attr_align));
13372 
13373 #ifdef SATA_DEBUG
13374 			mbuf_count++;
13375 
13376 			if (bp->b_bcount != bufsz)
13377 				/*
13378 				 * This will require special handling, because
13379 				 * DMA cookies will be based on the temporary
13380 				 * buffer size, not the original buffer
13381 				 * b_bcount, so the residue may have to
13382 				 * be counted differently.
13383 				 */
13384 				SATADBG2(SATA_DBG_DMA_SETUP,
13385 				    spx->txlt_sata_hba_inst,
13386 				    "sata_dma_buf_setup: bp size %x != "
13387 				    "bufsz %x\n", bp->b_bcount, bufsz);
13388 #endif
13389 			if (dma_flags & DDI_DMA_WRITE) {
13390 				/*
13391 				 * Write operation - copy data into
13392 				 * an aligned temporary buffer. Buffer will be
13393 				 * synced for device by ddi_dma_addr_bind_handle
13394 				 */
13395 				bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf,
13396 				    bp->b_bcount);
13397 			}
13398 
13399 			rval = ddi_dma_addr_bind_handle(
13400 			    spx->txlt_buf_dma_handle,
13401 			    NULL,
13402 			    spx->txlt_tmp_buf,
13403 			    bufsz, dma_flags, ddicallback, 0,
13404 			    &spx->txlt_dma_cookie,
13405 			    &spx->txlt_curwin_num_dma_cookies);
13406 		}
13407 
13408 		switch (rval) {
13409 		case DDI_DMA_PARTIAL_MAP:
13410 			SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13411 			    "sata_dma_buf_setup: DMA Partial Map\n", NULL);
13412 			/*
13413 			 * Partial DMA mapping.
13414 			 * Retrieve number of DMA windows for this request.
13415 			 */
13416 			if (ddi_dma_numwin(spx->txlt_buf_dma_handle,
13417 			    &spx->txlt_num_dma_win) != DDI_SUCCESS) {
13418 				if (spx->txlt_tmp_buf != NULL) {
13419 					ddi_dma_mem_free(
13420 					    &spx->txlt_tmp_buf_handle);
13421 					spx->txlt_tmp_buf = NULL;
13422 				}
13423 				(void) ddi_dma_unbind_handle(
13424 				    spx->txlt_buf_dma_handle);
13425 				(void) ddi_dma_free_handle(
13426 				    &spx->txlt_buf_dma_handle);
13427 				spx->txlt_buf_dma_handle = NULL;
13428 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13429 				    "sata_dma_buf_setup: numwin failed\n"));
13430 				return (DDI_FAILURE);
13431 			}
13432 			SATADBG2(SATA_DBG_DMA_SETUP,
13433 			    spx->txlt_sata_hba_inst,
13434 			    "sata_dma_buf_setup: windows: %d, cookies: %d\n",
13435 			    spx->txlt_num_dma_win,
13436 			    spx->txlt_curwin_num_dma_cookies);
13437 			spx->txlt_cur_dma_win = 0;
13438 			break;
13439 
13440 		case DDI_DMA_MAPPED:
13441 			/* DMA fully mapped */
13442 			spx->txlt_num_dma_win = 1;
13443 			spx->txlt_cur_dma_win = 0;
13444 			SATADBG1(SATA_DBG_DMA_SETUP,
13445 			    spx->txlt_sata_hba_inst,
13446 			    "sata_dma_buf_setup: windows: 1 "
13447 			    "cookies: %d\n", spx->txlt_curwin_num_dma_cookies);
13448 			break;
13449 
13450 		default:
13451 			/* DMA mapping failed */
13452 			if (spx->txlt_tmp_buf != NULL) {
13453 				ddi_dma_mem_free(
13454 				    &spx->txlt_tmp_buf_handle);
13455 				spx->txlt_tmp_buf = NULL;
13456 			}
13457 			(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
13458 			spx->txlt_buf_dma_handle = NULL;
13459 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13460 			    "sata_dma_buf_setup: buf dma handle binding "
13461 			    "failed %x\n", rval));
13462 			return (rval);
13463 		}
13464 		spx->txlt_curwin_processed_dma_cookies = 0;
13465 		spx->txlt_dma_cookie_list = NULL;
13466 	} else {
13467 		/*
13468 		 * DMA setup is reused. Check if we need to process more
13469 		 * cookies in current window, or to get next window, if any.
13470 		 */
13471 
13472 		ASSERT(spx->txlt_curwin_processed_dma_cookies <=
13473 		    spx->txlt_curwin_num_dma_cookies);
13474 
13475 		if (spx->txlt_curwin_processed_dma_cookies ==
13476 		    spx->txlt_curwin_num_dma_cookies) {
13477 			/*
13478 			 * All cookies from current DMA window were processed.
13479 			 * Get next DMA window.
13480 			 */
13481 			spx->txlt_cur_dma_win++;
13482 			if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) {
13483 				(void) ddi_dma_getwin(spx->txlt_buf_dma_handle,
13484 				    spx->txlt_cur_dma_win, &offset, &size,
13485 				    &spx->txlt_dma_cookie,
13486 				    &spx->txlt_curwin_num_dma_cookies);
13487 				spx->txlt_curwin_processed_dma_cookies = 0;
13488 			} else {
13489 				/* No more windows! End of request! */
13490 				/* What to do? - panic for now */
13491 				ASSERT(spx->txlt_cur_dma_win >=
13492 				    spx->txlt_num_dma_win);
13493 
13494 				spx->txlt_curwin_num_dma_cookies = 0;
13495 				spx->txlt_curwin_processed_dma_cookies = 0;
13496 				spx->txlt_sata_pkt->
13497 				    satapkt_cmd.satacmd_num_dma_cookies = 0;
13498 				return (DDI_SUCCESS);
13499 			}
13500 		}
13501 	}
13502 	/* There better be at least one DMA cookie outstanding */
13503 	ASSERT((spx->txlt_curwin_num_dma_cookies -
13504 	    spx->txlt_curwin_processed_dma_cookies) > 0);
13505 
13506 	if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) {
13507 		/* The default cookie slot was used in previous run */
13508 		ASSERT(spx->txlt_curwin_processed_dma_cookies == 0);
13509 		spx->txlt_dma_cookie_list = NULL;
13510 		spx->txlt_dma_cookie_list_len = 0;
13511 	}
13512 	if (spx->txlt_curwin_processed_dma_cookies == 0) {
13513 		/*
13514 		 * Processing a new DMA window - set-up dma cookies list.
13515 		 * We may reuse previously allocated cookie array if it is
13516 		 * possible.
13517 		 */
13518 		if (spx->txlt_dma_cookie_list != NULL &&
13519 		    spx->txlt_dma_cookie_list_len <
13520 		    spx->txlt_curwin_num_dma_cookies) {
13521 			/*
13522 			 * New DMA window contains more cookies than
13523 			 * the previous one. We need larger cookie list - free
13524 			 * the old one.
13525 			 */
13526 			(void) kmem_free(spx->txlt_dma_cookie_list,
13527 			    spx->txlt_dma_cookie_list_len *
13528 			    sizeof (ddi_dma_cookie_t));
13529 			spx->txlt_dma_cookie_list = NULL;
13530 			spx->txlt_dma_cookie_list_len = 0;
13531 		}
13532 		if (spx->txlt_dma_cookie_list == NULL) {
13533 			/*
13534 			 * Calculate lesser of number of cookies in this
13535 			 * DMA window and number of s/g entries.
13536 			 */
13537 			max_sg_len = cur_dma_attr->dma_attr_sgllen;
13538 			req_len = MIN(max_sg_len,
13539 			    spx->txlt_curwin_num_dma_cookies);
13540 
13541 			/* Allocate new dma cookie array if necessary */
13542 			if (req_len == 1) {
13543 				/* Only one cookie - no need for a list */
13544 				spx->txlt_dma_cookie_list =
13545 				    &spx->txlt_dma_cookie;
13546 				spx->txlt_dma_cookie_list_len = 1;
13547 			} else {
13548 				/*
13549 				 * More than one cookie - try to allocate space.
13550 				 */
13551 				spx->txlt_dma_cookie_list = kmem_zalloc(
13552 				    sizeof (ddi_dma_cookie_t) * req_len,
13553 				    callback == NULL_FUNC ? KM_NOSLEEP :
13554 				    KM_SLEEP);
13555 				if (spx->txlt_dma_cookie_list == NULL) {
13556 					SATADBG1(SATA_DBG_DMA_SETUP,
13557 					    spx->txlt_sata_hba_inst,
13558 					    "sata_dma_buf_setup: cookie list "
13559 					    "allocation failed\n", NULL);
13560 					/*
13561 					 * We could not allocate space for
13562 					 * neccessary number of dma cookies in
13563 					 * this window, so we fail this request.
13564 					 * Next invocation would try again to
13565 					 * allocate space for cookie list.
13566 					 * Note:Packet residue was not modified.
13567 					 */
13568 					return (DDI_DMA_NORESOURCES);
13569 				} else {
13570 					spx->txlt_dma_cookie_list_len = req_len;
13571 				}
13572 			}
13573 		}
13574 		/*
13575 		 * Fetch DMA cookies into cookie list in sata_pkt_txlate.
13576 		 * First cookie was already fetched.
13577 		 */
13578 		*(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie;
13579 		cur_txfer_len =
13580 		    (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size;
13581 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1;
13582 		spx->txlt_curwin_processed_dma_cookies++;
13583 		for (i = 1; (i < spx->txlt_dma_cookie_list_len) &&
13584 		    (i < spx->txlt_curwin_num_dma_cookies); i++) {
13585 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
13586 			    &spx->txlt_dma_cookie_list[i]);
13587 			cur_txfer_len +=
13588 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
13589 			spx->txlt_curwin_processed_dma_cookies++;
13590 			spx->txlt_sata_pkt->
13591 			    satapkt_cmd.satacmd_num_dma_cookies += 1;
13592 		}
13593 	} else {
13594 		SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13595 		    "sata_dma_buf_setup: sliding within DMA window, "
13596 		    "cur cookie %d, total cookies %d\n",
13597 		    spx->txlt_curwin_processed_dma_cookies,
13598 		    spx->txlt_curwin_num_dma_cookies);
13599 
13600 		/*
13601 		 * Not all cookies from the current dma window were used because
13602 		 * of s/g limitation.
13603 		 * There is no need to re-size the list - it was set at
13604 		 * optimal size, or only default entry is used (s/g = 1).
13605 		 */
13606 		if (spx->txlt_dma_cookie_list == NULL) {
13607 			spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie;
13608 			spx->txlt_dma_cookie_list_len = 1;
13609 		}
13610 		/*
13611 		 * Since we are processing remaining cookies in a DMA window,
13612 		 * there may be less of them than the number of entries in the
13613 		 * current dma cookie list.
13614 		 */
13615 		req_len = MIN(spx->txlt_dma_cookie_list_len,
13616 		    (spx->txlt_curwin_num_dma_cookies -
13617 		    spx->txlt_curwin_processed_dma_cookies));
13618 
13619 		/* Fetch the next batch of cookies */
13620 		for (i = 0, cur_txfer_len = 0; i < req_len; i++) {
13621 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
13622 			    &spx->txlt_dma_cookie_list[i]);
13623 			cur_txfer_len +=
13624 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
13625 			spx->txlt_sata_pkt->
13626 			    satapkt_cmd.satacmd_num_dma_cookies++;
13627 			spx->txlt_curwin_processed_dma_cookies++;
13628 		}
13629 	}
13630 
13631 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0);
13632 
13633 	/* Point sata_cmd to the cookie list */
13634 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list =
13635 	    &spx->txlt_dma_cookie_list[0];
13636 
13637 	/* Remember number of DMA cookies passed in sata packet */
13638 	spx->txlt_num_dma_cookies =
13639 	    spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies;
13640 
13641 	ASSERT(cur_txfer_len != 0);
13642 	if (cur_txfer_len <= bp->b_bcount)
13643 		spx->txlt_total_residue -= cur_txfer_len;
13644 	else {
13645 		/*
13646 		 * Temporary DMA buffer has been padded by
13647 		 * ddi_dma_mem_alloc()!
13648 		 * This requires special handling, because DMA cookies are
13649 		 * based on the temporary buffer size, not the b_bcount,
13650 		 * and we have extra bytes to transfer - but the packet
13651 		 * residue has to stay correct because we will copy only
13652 		 * the requested number of bytes.
13653 		 */
13654 		spx->txlt_total_residue -= bp->b_bcount;
13655 	}
13656 
13657 	return (DDI_SUCCESS);
13658 }
13659 
13660 /*
13661  * Common routine for releasing DMA resources
13662  */
13663 static void
13664 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx)
13665 {
13666 	if (spx->txlt_buf_dma_handle != NULL) {
13667 		if (spx->txlt_tmp_buf != NULL)  {
13668 			/*
13669 			 * Intermediate DMA buffer was allocated.
13670 			 * Free allocated buffer and associated access handle.
13671 			 */
13672 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
13673 			spx->txlt_tmp_buf = NULL;
13674 		}
13675 		/*
13676 		 * Free DMA resources - cookies and handles
13677 		 */
13678 		/* ASSERT(spx->txlt_dma_cookie_list != NULL); */
13679 		if (spx->txlt_dma_cookie_list != NULL) {
13680 			if (spx->txlt_dma_cookie_list !=
13681 			    &spx->txlt_dma_cookie) {
13682 				(void) kmem_free(spx->txlt_dma_cookie_list,
13683 				    spx->txlt_dma_cookie_list_len *
13684 				    sizeof (ddi_dma_cookie_t));
13685 				spx->txlt_dma_cookie_list = NULL;
13686 			}
13687 		}
13688 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
13689 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
13690 		spx->txlt_buf_dma_handle = NULL;
13691 	}
13692 }
13693 
13694 /*
13695  * Free DMA resources
13696  * Used by the HBA driver to release DMA resources that it does not use.
13697  *
13698  * Returns Void
13699  */
13700 void
13701 sata_free_dma_resources(sata_pkt_t *sata_pkt)
13702 {
13703 	sata_pkt_txlate_t *spx;
13704 
13705 	if (sata_pkt == NULL)
13706 		return;
13707 
13708 	spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
13709 
13710 	sata_common_free_dma_rsrcs(spx);
13711 }
13712 
13713 /*
13714  * Fetch Device Identify data.
13715  * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type)
13716  * command to a device and get the device identify data.
13717  * The device_info structure has to be set to device type (for selecting proper
13718  * device identify command).
13719  *
13720  * Returns:
13721  * SATA_SUCCESS if cmd succeeded
13722  * SATA_RETRY if cmd was rejected and could be retried,
13723  * SATA_FAILURE if cmd failed and should not be retried (port error)
13724  *
13725  * Cannot be called in an interrupt context.
13726  */
13727 
13728 static int
13729 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst,
13730     sata_drive_info_t *sdinfo)
13731 {
13732 	struct buf *bp;
13733 	sata_pkt_t *spkt;
13734 	sata_cmd_t *scmd;
13735 	sata_pkt_txlate_t *spx;
13736 	int rval;
13737 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
13738 
13739 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13740 	spx->txlt_sata_hba_inst = sata_hba_inst;
13741 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13742 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13743 	if (spkt == NULL) {
13744 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13745 		return (SATA_RETRY); /* may retry later */
13746 	}
13747 	/* address is needed now */
13748 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13749 
13750 	/*
13751 	 * Allocate buffer for Identify Data return data
13752 	 */
13753 	bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t));
13754 	if (bp == NULL) {
13755 		sata_pkt_free(spx);
13756 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13757 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13758 		    "sata_fetch_device_identify_data: "
13759 		    "cannot allocate buffer for ID"));
13760 		return (SATA_RETRY); /* may retry later */
13761 	}
13762 
13763 	/* Fill sata_pkt */
13764 	sdinfo->satadrv_state = SATA_STATE_PROBING;
13765 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13766 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13767 	/* Synchronous mode, no callback */
13768 	spkt->satapkt_comp = NULL;
13769 	/* Timeout 30s */
13770 	spkt->satapkt_time = sata_default_pkt_time;
13771 
13772 	scmd = &spkt->satapkt_cmd;
13773 	scmd->satacmd_bp = bp;
13774 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13775 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
13776 
13777 	/* Build Identify Device cmd in the sata_pkt */
13778 	scmd->satacmd_addr_type = 0;		/* N/A */
13779 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
13780 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
13781 	scmd->satacmd_lba_mid_lsb = 0;		/* N/A */
13782 	scmd->satacmd_lba_high_lsb = 0;		/* N/A */
13783 	scmd->satacmd_features_reg = 0;		/* N/A */
13784 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13785 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
13786 		/* Identify Packet Device cmd */
13787 		scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE;
13788 	} else {
13789 		/* Identify Device cmd - mandatory for all other devices */
13790 		scmd->satacmd_cmd_reg = SATAC_ID_DEVICE;
13791 	}
13792 
13793 	/* Send pkt to SATA HBA driver */
13794 	rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt);
13795 
13796 #ifdef SATA_INJECT_FAULTS
13797 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
13798 #endif
13799 
13800 	if (rval == SATA_TRAN_ACCEPTED &&
13801 	    spkt->satapkt_reason == SATA_PKT_COMPLETED) {
13802 		if (spx->txlt_buf_dma_handle != NULL) {
13803 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13804 			    DDI_DMA_SYNC_FORKERNEL);
13805 			ASSERT(rval == DDI_SUCCESS);
13806 			if (sata_check_for_dma_error(dip, spx)) {
13807 				ddi_fm_service_impact(dip,
13808 				    DDI_SERVICE_UNAFFECTED);
13809 				rval = SATA_RETRY;
13810 				goto fail;
13811 			}
13812 
13813 		}
13814 		if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config &
13815 		    SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) {
13816 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13817 			    "SATA disk device at port %d - "
13818 			    "partial Identify Data",
13819 			    sdinfo->satadrv_addr.cport));
13820 			rval = SATA_RETRY; /* may retry later */
13821 			goto fail;
13822 		}
13823 		/* Update sata_drive_info */
13824 		bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id,
13825 		    sizeof (sata_id_t));
13826 
13827 		sdinfo->satadrv_features_support = 0;
13828 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
13829 			/*
13830 			 * Retrieve capacity (disks only) and addressing mode
13831 			 */
13832 			sdinfo->satadrv_capacity = sata_check_capacity(sdinfo);
13833 		} else {
13834 			/*
13835 			 * For ATAPI devices one would have to issue
13836 			 * Get Capacity cmd for media capacity. Not here.
13837 			 */
13838 			sdinfo->satadrv_capacity = 0;
13839 			/*
13840 			 * Check what cdb length is supported
13841 			 */
13842 			if ((sdinfo->satadrv_id.ai_config &
13843 			    SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B)
13844 				sdinfo->satadrv_atapi_cdb_len = 16;
13845 			else
13846 				sdinfo->satadrv_atapi_cdb_len = 12;
13847 		}
13848 		/* Setup supported features flags */
13849 		if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT)
13850 			sdinfo->satadrv_features_support |= SATA_DEV_F_DMA;
13851 
13852 		/* Check for SATA GEN and NCQ support */
13853 		if (sdinfo->satadrv_id.ai_satacap != 0 &&
13854 		    sdinfo->satadrv_id.ai_satacap != 0xffff) {
13855 			/* SATA compliance */
13856 			if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ)
13857 				sdinfo->satadrv_features_support |=
13858 				    SATA_DEV_F_NCQ;
13859 			if (sdinfo->satadrv_id.ai_satacap &
13860 			    (SATA_1_SPEED | SATA_2_SPEED | SATA_3_SPEED)) {
13861 				if (sdinfo->satadrv_id.ai_satacap &
13862 				    SATA_3_SPEED)
13863 					sdinfo->satadrv_features_support |=
13864 					    SATA_DEV_F_SATA3;
13865 				if (sdinfo->satadrv_id.ai_satacap &
13866 				    SATA_2_SPEED)
13867 					sdinfo->satadrv_features_support |=
13868 					    SATA_DEV_F_SATA2;
13869 				if (sdinfo->satadrv_id.ai_satacap &
13870 				    SATA_1_SPEED)
13871 					sdinfo->satadrv_features_support |=
13872 					    SATA_DEV_F_SATA1;
13873 			} else {
13874 				sdinfo->satadrv_features_support |=
13875 				    SATA_DEV_F_SATA1;
13876 			}
13877 		}
13878 		if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) &&
13879 		    (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD))
13880 			sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ;
13881 
13882 		sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth;
13883 		if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) ||
13884 		    (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) {
13885 			++sdinfo->satadrv_queue_depth;
13886 			/* Adjust according to controller capabilities */
13887 			sdinfo->satadrv_max_queue_depth = MIN(
13888 			    sdinfo->satadrv_queue_depth,
13889 			    SATA_QDEPTH(sata_hba_inst));
13890 			/* Adjust according to global queue depth limit */
13891 			sdinfo->satadrv_max_queue_depth = MIN(
13892 			    sdinfo->satadrv_max_queue_depth,
13893 			    sata_current_max_qdepth);
13894 			if (sdinfo->satadrv_max_queue_depth == 0)
13895 				sdinfo->satadrv_max_queue_depth = 1;
13896 		} else
13897 			sdinfo->satadrv_max_queue_depth = 1;
13898 
13899 		rval = SATA_SUCCESS;
13900 	} else {
13901 		/*
13902 		 * Woops, no Identify Data.
13903 		 */
13904 		if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) {
13905 			rval = SATA_RETRY; /* may retry later */
13906 		} else if (rval == SATA_TRAN_ACCEPTED) {
13907 			if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR ||
13908 			    spkt->satapkt_reason == SATA_PKT_ABORTED ||
13909 			    spkt->satapkt_reason == SATA_PKT_TIMEOUT ||
13910 			    spkt->satapkt_reason == SATA_PKT_RESET)
13911 				rval = SATA_RETRY; /* may retry later */
13912 			else
13913 				rval = SATA_FAILURE;
13914 		} else {
13915 			rval = SATA_FAILURE;
13916 		}
13917 	}
13918 fail:
13919 	/* Free allocated resources */
13920 	sata_free_local_buffer(spx);
13921 	sata_pkt_free(spx);
13922 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13923 
13924 	return (rval);
13925 }
13926 
13927 
13928 /*
13929  * Some devices may not come-up with default DMA mode (UDMA or MWDMA).
13930  * UDMA mode is checked first, followed by MWDMA mode.
13931  * set correctly, so this function is setting it to the highest supported level.
13932  * Older SATA spec required that the device supports at least DMA 4 mode and
13933  * UDMA mode is selected.  It is not mentioned in SerialATA 2.6, so this
13934  * restriction has been removed.
13935  *
13936  * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported.
13937  * Returns SATA_FAILURE if proper DMA mode could not be selected.
13938  *
13939  * NOTE: This function should be called only if DMA mode is supported.
13940  */
13941 static int
13942 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo)
13943 {
13944 	sata_pkt_t *spkt;
13945 	sata_cmd_t *scmd;
13946 	sata_pkt_txlate_t *spx;
13947 	int i, mode;
13948 	uint8_t subcmd;
13949 	int rval = SATA_SUCCESS;
13950 
13951 	ASSERT(sdinfo != NULL);
13952 	ASSERT(sata_hba_inst != NULL);
13953 
13954 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
13955 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) {
13956 		/* Find highest Ultra DMA mode supported */
13957 		for (mode = 6; mode >= 0; --mode) {
13958 			if (sdinfo->satadrv_id.ai_ultradma & (1 << mode))
13959 				break;
13960 		}
13961 #if 0
13962 		/* Left for historical reasons */
13963 		/*
13964 		 * Some initial version of SATA spec indicated that at least
13965 		 * UDMA mode 4 has to be supported. It is not mentioned in
13966 		 * SerialATA 2.6, so this restriction is removed.
13967 		 */
13968 		if (mode < 4)
13969 			return (SATA_FAILURE);
13970 #endif
13971 
13972 		/*
13973 		 * For disk, we're still going to set DMA mode whatever is
13974 		 * selected by default
13975 		 *
13976 		 * We saw an old maxtor sata drive will select Ultra DMA and
13977 		 * Multi-Word DMA simultaneouly by default, which is going
13978 		 * to cause DMA command timed out, so we need to select DMA
13979 		 * mode even when it's already done by default
13980 		 */
13981 		if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) {
13982 
13983 			/* Find UDMA mode currently selected */
13984 			for (i = 6; i >= 0; --i) {
13985 				if (sdinfo->satadrv_id.ai_ultradma &
13986 				    (1 << (i + 8)))
13987 					break;
13988 			}
13989 			if (i >= mode)
13990 				/* Nothing to do */
13991 				return (SATA_SUCCESS);
13992 		}
13993 
13994 		subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA;
13995 
13996 	} else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) {
13997 		/* Find highest MultiWord DMA mode supported */
13998 		for (mode = 2; mode >= 0; --mode) {
13999 			if (sdinfo->satadrv_id.ai_dworddma & (1 << mode))
14000 				break;
14001 		}
14002 
14003 		/*
14004 		 * For disk, We're still going to set DMA mode whatever is
14005 		 * selected by default
14006 		 *
14007 		 * We saw an old maxtor sata drive will select Ultra DMA and
14008 		 * Multi-Word DMA simultaneouly by default, which is going
14009 		 * to cause DMA command timed out, so we need to select DMA
14010 		 * mode even when it's already done by default
14011 		 */
14012 		if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) {
14013 
14014 			/* Find highest MultiWord DMA mode selected */
14015 			for (i = 2; i >= 0; --i) {
14016 				if (sdinfo->satadrv_id.ai_dworddma &
14017 				    (1 << (i + 8)))
14018 					break;
14019 			}
14020 			if (i >= mode)
14021 				/* Nothing to do */
14022 				return (SATA_SUCCESS);
14023 		}
14024 
14025 		subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA;
14026 	} else
14027 		return (SATA_SUCCESS);
14028 
14029 	/*
14030 	 * Set DMA mode via SET FEATURES COMMAND.
14031 	 * Prepare packet for SET FEATURES COMMAND.
14032 	 */
14033 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14034 	spx->txlt_sata_hba_inst = sata_hba_inst;
14035 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
14036 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14037 	if (spkt == NULL) {
14038 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14039 		    "sata_set_dma_mode: could not set DMA mode %d", mode));
14040 		rval = SATA_FAILURE;
14041 		goto done;
14042 	}
14043 	/* Fill sata_pkt */
14044 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14045 	/* Timeout 30s */
14046 	spkt->satapkt_time = sata_default_pkt_time;
14047 	/* Synchronous mode, no callback, interrupts */
14048 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14049 	spkt->satapkt_comp = NULL;
14050 	scmd = &spkt->satapkt_cmd;
14051 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14052 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14053 	scmd->satacmd_addr_type = 0;
14054 	scmd->satacmd_device_reg = 0;
14055 	scmd->satacmd_status_reg = 0;
14056 	scmd->satacmd_error_reg = 0;
14057 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14058 	scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE;
14059 	scmd->satacmd_sec_count_lsb = subcmd | mode;
14060 
14061 	/* Transfer command to HBA */
14062 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
14063 	    spkt) != SATA_TRAN_ACCEPTED ||
14064 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
14065 		/* Pkt execution failed */
14066 		rval = SATA_FAILURE;
14067 	}
14068 done:
14069 
14070 	/* Free allocated resources */
14071 	if (spkt != NULL)
14072 		sata_pkt_free(spx);
14073 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14074 
14075 	return (rval);
14076 }
14077 
14078 
14079 /*
14080  * Set device caching mode.
14081  * One of the following operations should be specified:
14082  * SATAC_SF_ENABLE_READ_AHEAD
14083  * SATAC_SF_DISABLE_READ_AHEAD
14084  * SATAC_SF_ENABLE_WRITE_CACHE
14085  * SATAC_SF_DISABLE_WRITE_CACHE
14086  *
14087  * If operation fails, system log messgage is emitted.
14088  * Returns SATA_SUCCESS when the operation succeeds, SATA_RETRY if
14089  * command was sent but did not succeed, and SATA_FAILURE otherwise.
14090  */
14091 
14092 static int
14093 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
14094     int cache_op)
14095 {
14096 	sata_pkt_t *spkt;
14097 	sata_cmd_t *scmd;
14098 	sata_pkt_txlate_t *spx;
14099 	int rval = SATA_SUCCESS;
14100 	int hba_rval;
14101 	char *infop;
14102 
14103 	ASSERT(sdinfo != NULL);
14104 	ASSERT(sata_hba_inst != NULL);
14105 	ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD ||
14106 	    cache_op == SATAC_SF_DISABLE_READ_AHEAD ||
14107 	    cache_op == SATAC_SF_ENABLE_WRITE_CACHE ||
14108 	    cache_op == SATAC_SF_DISABLE_WRITE_CACHE);
14109 
14110 
14111 	/* Prepare packet for SET FEATURES COMMAND */
14112 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14113 	spx->txlt_sata_hba_inst = sata_hba_inst;
14114 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
14115 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14116 	if (spkt == NULL) {
14117 		rval = SATA_FAILURE;
14118 		goto failure;
14119 	}
14120 	/* Fill sata_pkt */
14121 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14122 	/* Timeout 30s */
14123 	spkt->satapkt_time = sata_default_pkt_time;
14124 	/* Synchronous mode, no callback, interrupts */
14125 	spkt->satapkt_op_mode =
14126 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14127 	spkt->satapkt_comp = NULL;
14128 	scmd = &spkt->satapkt_cmd;
14129 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14130 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14131 	scmd->satacmd_addr_type = 0;
14132 	scmd->satacmd_device_reg = 0;
14133 	scmd->satacmd_status_reg = 0;
14134 	scmd->satacmd_error_reg = 0;
14135 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14136 	scmd->satacmd_features_reg = cache_op;
14137 
14138 	/* Transfer command to HBA */
14139 	hba_rval = (*SATA_START_FUNC(sata_hba_inst))(
14140 	    SATA_DIP(sata_hba_inst), spkt);
14141 
14142 #ifdef SATA_INJECT_FAULTS
14143 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
14144 #endif
14145 
14146 	if ((hba_rval != SATA_TRAN_ACCEPTED) ||
14147 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
14148 		/* Pkt execution failed */
14149 		switch (cache_op) {
14150 		case SATAC_SF_ENABLE_READ_AHEAD:
14151 			infop = "enabling read ahead failed";
14152 			break;
14153 		case SATAC_SF_DISABLE_READ_AHEAD:
14154 			infop = "disabling read ahead failed";
14155 			break;
14156 		case SATAC_SF_ENABLE_WRITE_CACHE:
14157 			infop = "enabling write cache failed";
14158 			break;
14159 		case SATAC_SF_DISABLE_WRITE_CACHE:
14160 			infop = "disabling write cache failed";
14161 			break;
14162 		}
14163 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
14164 		rval = SATA_RETRY;
14165 	}
14166 failure:
14167 	/* Free allocated resources */
14168 	if (spkt != NULL)
14169 		sata_pkt_free(spx);
14170 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14171 	return (rval);
14172 }
14173 
14174 /*
14175  * Set Removable Media Status Notification (enable/disable)
14176  * state == 0 , disable
14177  * state != 0 , enable
14178  *
14179  * If operation fails, system log messgage is emitted.
14180  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
14181  */
14182 
14183 static int
14184 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
14185     int state)
14186 {
14187 	sata_pkt_t *spkt;
14188 	sata_cmd_t *scmd;
14189 	sata_pkt_txlate_t *spx;
14190 	int rval = SATA_SUCCESS;
14191 	char *infop;
14192 
14193 	ASSERT(sdinfo != NULL);
14194 	ASSERT(sata_hba_inst != NULL);
14195 
14196 	/* Prepare packet for SET FEATURES COMMAND */
14197 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14198 	spx->txlt_sata_hba_inst = sata_hba_inst;
14199 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
14200 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14201 	if (spkt == NULL) {
14202 		rval = SATA_FAILURE;
14203 		goto failure;
14204 	}
14205 	/* Fill sata_pkt */
14206 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14207 	/* Timeout 30s */
14208 	spkt->satapkt_time = sata_default_pkt_time;
14209 	/* Synchronous mode, no callback, interrupts */
14210 	spkt->satapkt_op_mode =
14211 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14212 	spkt->satapkt_comp = NULL;
14213 	scmd = &spkt->satapkt_cmd;
14214 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14215 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14216 	scmd->satacmd_addr_type = 0;
14217 	scmd->satacmd_device_reg = 0;
14218 	scmd->satacmd_status_reg = 0;
14219 	scmd->satacmd_error_reg = 0;
14220 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14221 	if (state == 0)
14222 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN;
14223 	else
14224 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN;
14225 
14226 	/* Transfer command to HBA */
14227 	if (((*SATA_START_FUNC(sata_hba_inst))(
14228 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
14229 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
14230 		/* Pkt execution failed */
14231 		if (state == 0)
14232 			infop = "disabling Removable Media Status "
14233 			    "Notification failed";
14234 		else
14235 			infop = "enabling Removable Media Status "
14236 			    "Notification failed";
14237 
14238 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
14239 		rval = SATA_FAILURE;
14240 	}
14241 failure:
14242 	/* Free allocated resources */
14243 	if (spkt != NULL)
14244 		sata_pkt_free(spx);
14245 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14246 	return (rval);
14247 }
14248 
14249 
14250 /*
14251  * Update state and copy port ss* values from passed sata_device structure.
14252  * sata_address is validated - if not valid, nothing is changed in sata_scsi
14253  * configuration struct.
14254  *
14255  * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function
14256  * regardless of the state in device argument.
14257  *
14258  * Port mutex should be held while calling this function.
14259  */
14260 static void
14261 sata_update_port_info(sata_hba_inst_t *sata_hba_inst,
14262     sata_device_t *sata_device)
14263 {
14264 	sata_cport_info_t *cportinfo;
14265 
14266 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT ||
14267 	    sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
14268 		if (SATA_NUM_CPORTS(sata_hba_inst) <=
14269 		    sata_device->satadev_addr.cport)
14270 			return;
14271 
14272 		cportinfo = SATA_CPORT_INFO(sata_hba_inst,
14273 		    sata_device->satadev_addr.cport);
14274 
14275 		ASSERT(mutex_owned(&cportinfo->cport_mutex));
14276 		cportinfo->cport_scr = sata_device->satadev_scr;
14277 
14278 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
14279 		cportinfo->cport_state &= ~(SATA_PSTATE_PWRON |
14280 		    SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
14281 		cportinfo->cport_state |=
14282 		    sata_device->satadev_state & SATA_PSTATE_VALID;
14283 	}
14284 }
14285 
14286 void
14287 sata_update_pmport_info(sata_hba_inst_t *sata_hba_inst,
14288     sata_device_t *sata_device)
14289 {
14290 	sata_pmport_info_t *pmportinfo;
14291 
14292 	if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT &&
14293 	    sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) ||
14294 	    SATA_NUM_PMPORTS(sata_hba_inst,
14295 	    sata_device->satadev_addr.cport) <
14296 	    sata_device->satadev_addr.pmport) {
14297 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
14298 		    "sata_update_port_info: error address %p.",
14299 		    &sata_device->satadev_addr);
14300 		return;
14301 	}
14302 
14303 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
14304 	    sata_device->satadev_addr.cport,
14305 	    sata_device->satadev_addr.pmport);
14306 
14307 	ASSERT(mutex_owned(&pmportinfo->pmport_mutex));
14308 	pmportinfo->pmport_scr = sata_device->satadev_scr;
14309 
14310 	/* Preserve SATA_PSTATE_SHUTDOWN flag */
14311 	pmportinfo->pmport_state &=
14312 	    ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
14313 	pmportinfo->pmport_state |=
14314 	    sata_device->satadev_state & SATA_PSTATE_VALID;
14315 }
14316 
14317 /*
14318  * Extract SATA port specification from an IOCTL argument.
14319  *
14320  * This function return the port the user land send us as is, unless it
14321  * cannot retrieve port spec, then -1 is returned.
14322  *
14323  * Support port multiplier.
14324  */
14325 static int32_t
14326 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp)
14327 {
14328 	int32_t port;
14329 
14330 	/* Extract port number from nvpair in dca structure  */
14331 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
14332 		SATA_LOG_D((sata_hba_inst, CE_NOTE,
14333 		    "sata_get_port_num: invalid port spec 0x%x in ioctl",
14334 		    port));
14335 		port = -1;
14336 	}
14337 
14338 	return (port);
14339 }
14340 
14341 /*
14342  * Get dev_info_t pointer to the device node pointed to by port argument.
14343  * NOTE: target argument is a value used in ioctls to identify
14344  * the AP - it is not a sata_address.
14345  * It is a combination of cport, pmport and address qualifier, encodded same
14346  * way as a scsi target number.
14347  * At this moment it carries only cport number.
14348  *
14349  * PMult hotplug is supported now.
14350  *
14351  * Returns dev_info_t pointer if target device was found, NULL otherwise.
14352  */
14353 
14354 static dev_info_t *
14355 sata_get_target_dip(dev_info_t *dip, uint8_t cport, uint8_t pmport)
14356 {
14357 	dev_info_t	*cdip = NULL;
14358 	int		target, tgt;
14359 	int		circ;
14360 	uint8_t		qual;
14361 
14362 	sata_hba_inst_t	*sata_hba_inst;
14363 	scsi_hba_tran_t *scsi_hba_tran;
14364 
14365 	/* Get target id */
14366 	scsi_hba_tran = ddi_get_driver_private(dip);
14367 	if (scsi_hba_tran == NULL)
14368 		return (NULL);
14369 
14370 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
14371 
14372 	if (sata_hba_inst == NULL)
14373 		return (NULL);
14374 
14375 	/* Identify a port-mult by cport_info.cport_dev_type */
14376 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT)
14377 		qual = SATA_ADDR_DPMPORT;
14378 	else
14379 		qual = SATA_ADDR_DCPORT;
14380 
14381 	target = SATA_TO_SCSI_TARGET(cport, pmport, qual);
14382 
14383 	/* Retrieve target dip */
14384 	ndi_devi_enter(dip, &circ);
14385 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
14386 		dev_info_t *next = ddi_get_next_sibling(cdip);
14387 
14388 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
14389 		    DDI_PROP_DONTPASS, "target", -1);
14390 		if (tgt == -1) {
14391 			/*
14392 			 * This is actually an error condition, but not
14393 			 * a fatal one. Just continue the search.
14394 			 */
14395 			cdip = next;
14396 			continue;
14397 		}
14398 
14399 		if (tgt == target)
14400 			break;
14401 
14402 		cdip = next;
14403 	}
14404 	ndi_devi_exit(dip, circ);
14405 
14406 	return (cdip);
14407 }
14408 
14409 /*
14410  * Get dev_info_t pointer to the device node pointed to by port argument.
14411  * NOTE: target argument is a value used in ioctls to identify
14412  * the AP - it is not a sata_address.
14413  * It is a combination of cport, pmport and address qualifier, encoded same
14414  * way as a scsi target number.
14415  *
14416  * Returns dev_info_t pointer if target device was found, NULL otherwise.
14417  */
14418 
14419 static dev_info_t *
14420 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr)
14421 {
14422 	dev_info_t	*cdip = NULL;
14423 	int		target, tgt;
14424 	int		circ;
14425 
14426 	target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual);
14427 
14428 	ndi_devi_enter(dip, &circ);
14429 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
14430 		dev_info_t *next = ddi_get_next_sibling(cdip);
14431 
14432 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
14433 		    DDI_PROP_DONTPASS, "target", -1);
14434 		if (tgt == -1) {
14435 			/*
14436 			 * This is actually an error condition, but not
14437 			 * a fatal one. Just continue the search.
14438 			 */
14439 			cdip = next;
14440 			continue;
14441 		}
14442 
14443 		if (tgt == target)
14444 			break;
14445 
14446 		cdip = next;
14447 	}
14448 	ndi_devi_exit(dip, circ);
14449 
14450 	return (cdip);
14451 }
14452 
14453 /*
14454  * Process sata port disconnect request.
14455  * Normally, cfgadm sata plugin will try to offline (unconfigure) the device
14456  * before this request. Nevertheless, if a device is still configured,
14457  * we need to attempt to offline and unconfigure device.
14458  * Regardless of the unconfigure operation results the port is marked as
14459  * deactivated and no access to the attached device is possible.
14460  * If the target node remains because unconfigure operation failed, its state
14461  * will be set to DEVICE_REMOVED, preventing it to be used again when a device
14462  * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure
14463  * the device and remove old target node.
14464  *
14465  * This function invokes sata_hba_inst->satahba_tran->
14466  * sata_tran_hotplug_ops->sata_tran_port_deactivate().
14467  * If successful, the device structure (if any) attached to the specified port
14468  * is removed and state of the port marked appropriately.
14469  * Failure of the port_deactivate may keep port in the physically active state,
14470  * or may fail the port.
14471  *
14472  * NOTE: Port multiplier is supported.
14473  */
14474 
14475 static int
14476 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst,
14477     sata_device_t *sata_device)
14478 {
14479 	sata_drive_info_t *sdinfo = NULL, *subsdinfo = NULL;
14480 	sata_cport_info_t *cportinfo = NULL;
14481 	sata_pmport_info_t *pmportinfo = NULL;
14482 	sata_pmult_info_t *pmultinfo = NULL;
14483 	sata_device_t subsdevice;
14484 	int cport, pmport, qual;
14485 	int rval = SATA_SUCCESS;
14486 	int npmport = 0;
14487 	int rv = 0;
14488 
14489 	cport = sata_device->satadev_addr.cport;
14490 	pmport = sata_device->satadev_addr.pmport;
14491 	qual = sata_device->satadev_addr.qual;
14492 
14493 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
14494 	if (qual == SATA_ADDR_DCPORT)
14495 		qual = SATA_ADDR_CPORT;
14496 	else
14497 		qual = SATA_ADDR_PMPORT;
14498 
14499 	/*
14500 	 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran->
14501 	 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
14502 	 * Do the sanity check.
14503 	 */
14504 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
14505 		/* No physical port deactivation supported. */
14506 		return (EINVAL);
14507 	}
14508 
14509 	/* Check the current state of the port */
14510 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14511 	    (SATA_DIP(sata_hba_inst), sata_device);
14512 
14513 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
14514 
14515 	/*
14516 	 * Processing port mulitiplier
14517 	 */
14518 	if (qual == SATA_ADDR_CPORT &&
14519 	    SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) {
14520 		mutex_enter(&cportinfo->cport_mutex);
14521 
14522 		/* Check controller port status */
14523 		sata_update_port_info(sata_hba_inst, sata_device);
14524 		if (rval != SATA_SUCCESS ||
14525 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14526 			/*
14527 			 * Device port status is unknown or it is in failed
14528 			 * state
14529 			 */
14530 			SATA_CPORT_STATE(sata_hba_inst, cport) =
14531 			    SATA_PSTATE_FAILED;
14532 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14533 			    "sata_hba_ioctl: connect: failed to deactivate "
14534 			    "SATA port %d", cport);
14535 			mutex_exit(&cportinfo->cport_mutex);
14536 			return (EIO);
14537 		}
14538 
14539 		/* Disconnect all sub-devices. */
14540 		pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
14541 		if (pmultinfo != NULL) {
14542 
14543 			for (npmport = 0; npmport < SATA_NUM_PMPORTS(
14544 			    sata_hba_inst, cport); npmport ++) {
14545 				subsdinfo = SATA_PMPORT_DRV_INFO(
14546 				    sata_hba_inst, cport, npmport);
14547 				if (subsdinfo == NULL)
14548 					continue;
14549 
14550 				subsdevice.satadev_addr = subsdinfo->
14551 				    satadrv_addr;
14552 
14553 				mutex_exit(&cportinfo->cport_mutex);
14554 				if (sata_ioctl_disconnect(sata_hba_inst,
14555 				    &subsdevice) == SATA_SUCCESS) {
14556 					SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
14557 					"[Remove] device at port %d:%d "
14558 					"successfully.", cport, npmport);
14559 				}
14560 				mutex_enter(&cportinfo->cport_mutex);
14561 			}
14562 		}
14563 
14564 		/* Disconnect the port multiplier */
14565 		cportinfo->cport_state &= ~SATA_STATE_READY;
14566 		mutex_exit(&cportinfo->cport_mutex);
14567 
14568 		sata_device->satadev_addr.qual = qual;
14569 		rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
14570 		    (SATA_DIP(sata_hba_inst), sata_device);
14571 
14572 		sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14573 		    SE_NO_HINT);
14574 
14575 		mutex_enter(&cportinfo->cport_mutex);
14576 		sata_update_port_info(sata_hba_inst, sata_device);
14577 		if (rval != SATA_SUCCESS &&
14578 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
14579 			cportinfo->cport_state = SATA_PSTATE_FAILED;
14580 			rv = EIO;
14581 		} else {
14582 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
14583 		}
14584 		mutex_exit(&cportinfo->cport_mutex);
14585 
14586 		return (rv);
14587 	}
14588 
14589 	/*
14590 	 * Process non-port-multiplier device - it could be a drive connected
14591 	 * to a port multiplier port or a controller port.
14592 	 */
14593 	if (qual == SATA_ADDR_PMPORT) {
14594 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14595 		mutex_enter(&pmportinfo->pmport_mutex);
14596 		sata_update_pmport_info(sata_hba_inst, sata_device);
14597 		if (rval != SATA_SUCCESS ||
14598 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14599 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
14600 			    SATA_PSTATE_FAILED;
14601 			SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
14602 			    "sata_hba_ioctl: connect: failed to deactivate "
14603 			    "SATA port %d:%d", cport, pmport);
14604 			mutex_exit(&pmportinfo->pmport_mutex);
14605 			return (EIO);
14606 		}
14607 
14608 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
14609 			sdinfo = pmportinfo->pmport_sata_drive;
14610 			ASSERT(sdinfo != NULL);
14611 		}
14612 
14613 		/*
14614 		 * Set port's dev_state to not ready - this will disable
14615 		 * an access to a potentially attached device.
14616 		 */
14617 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
14618 
14619 		/* Remove and release sata_drive info structure. */
14620 		if (sdinfo != NULL) {
14621 			if ((sdinfo->satadrv_type &
14622 			    SATA_VALID_DEV_TYPE) != 0) {
14623 				/*
14624 				 * If a target node exists, try to offline
14625 				 * a device and remove target node.
14626 				 */
14627 				mutex_exit(&pmportinfo->pmport_mutex);
14628 				(void) sata_offline_device(sata_hba_inst,
14629 				    sata_device, sdinfo);
14630 				mutex_enter(&pmportinfo->pmport_mutex);
14631 			}
14632 
14633 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
14634 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
14635 			(void) kmem_free((void *)sdinfo,
14636 			    sizeof (sata_drive_info_t));
14637 		}
14638 		mutex_exit(&pmportinfo->pmport_mutex);
14639 
14640 	} else if (qual == SATA_ADDR_CPORT) {
14641 		mutex_enter(&cportinfo->cport_mutex);
14642 		sata_update_port_info(sata_hba_inst, sata_device);
14643 		if (rval != SATA_SUCCESS ||
14644 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14645 			/*
14646 			 * Device port status is unknown or it is in failed
14647 			 * state
14648 			 */
14649 			SATA_CPORT_STATE(sata_hba_inst, cport) =
14650 			    SATA_PSTATE_FAILED;
14651 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14652 			    "sata_hba_ioctl: connect: failed to deactivate "
14653 			    "SATA port %d", cport);
14654 			mutex_exit(&cportinfo->cport_mutex);
14655 			return (EIO);
14656 		}
14657 
14658 		if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
14659 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
14660 			ASSERT(pmultinfo != NULL);
14661 		} else if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
14662 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14663 			ASSERT(sdinfo != NULL);
14664 		}
14665 		cportinfo->cport_state &= ~SATA_STATE_READY;
14666 
14667 		if (sdinfo != NULL) {
14668 			if ((sdinfo->satadrv_type &
14669 			    SATA_VALID_DEV_TYPE) != 0) {
14670 				/*
14671 				 * If a target node exists, try to offline
14672 				 * a device and remove target node.
14673 				 */
14674 				mutex_exit(&cportinfo->cport_mutex);
14675 				(void) sata_offline_device(sata_hba_inst,
14676 				    sata_device, sdinfo);
14677 				mutex_enter(&cportinfo->cport_mutex);
14678 			}
14679 
14680 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14681 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14682 			(void) kmem_free((void *)sdinfo,
14683 			    sizeof (sata_drive_info_t));
14684 		}
14685 		mutex_exit(&cportinfo->cport_mutex);
14686 	}
14687 
14688 	/* Just ask HBA driver to deactivate port */
14689 	sata_device->satadev_addr.qual = qual;
14690 
14691 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
14692 	    (SATA_DIP(sata_hba_inst), sata_device);
14693 
14694 	/*
14695 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14696 	 * without the hint (to force listener to investivate the state).
14697 	 */
14698 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14699 	    SE_NO_HINT);
14700 
14701 	if (qual == SATA_ADDR_PMPORT) {
14702 		mutex_enter(&pmportinfo->pmport_mutex);
14703 		sata_update_pmport_info(sata_hba_inst, sata_device);
14704 
14705 		if (rval != SATA_SUCCESS &&
14706 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
14707 			/*
14708 			 * Port deactivation failure - do not change port
14709 			 * state unless the state returned by HBA indicates a
14710 			 * port failure.
14711 			 *
14712 			 * NOTE: device structures were released, so devices
14713 			 * now are invisible! Port reset is needed to
14714 			 * re-enumerate devices.
14715 			 */
14716 			pmportinfo->pmport_state = SATA_PSTATE_FAILED;
14717 			rv = EIO;
14718 		} else {
14719 			/*
14720 			 * Deactivation succeded. From now on the sata framework
14721 			 * will not care what is happening to the device, until
14722 			 * the port is activated again.
14723 			 */
14724 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
14725 		}
14726 		mutex_exit(&pmportinfo->pmport_mutex);
14727 	} else if (qual == SATA_ADDR_CPORT) {
14728 		mutex_enter(&cportinfo->cport_mutex);
14729 		sata_update_port_info(sata_hba_inst, sata_device);
14730 
14731 		if (rval != SATA_SUCCESS &&
14732 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
14733 			cportinfo->cport_state = SATA_PSTATE_FAILED;
14734 			rv = EIO;
14735 		} else {
14736 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
14737 		}
14738 		mutex_exit(&cportinfo->cport_mutex);
14739 	}
14740 
14741 	return (rv);
14742 }
14743 
14744 
14745 
14746 /*
14747  * Process sata port connect request
14748  * The sata cfgadm pluging will invoke this operation only if port was found
14749  * in the disconnect state (failed state is also treated as the disconnected
14750  * state).
14751  * DEVCTL_AP_CONNECT would invoke  sata_hba_inst->satahba_tran->
14752  * sata_tran_hotplug_ops->sata_tran_port_activate().
14753  * If successful and a device is found attached to the port,
14754  * the initialization sequence is executed to attach a device structure to
14755  * a port structure. The state of the port and a device would be set
14756  * appropriately.
14757  * The device is not set in configured state (system-wise) by this operation.
14758  *
14759  * Note, that activating the port may generate link events,
14760  * so it is important that following processing and the
14761  * event processing does not interfere with each other!
14762  *
14763  * This operation may remove port failed state and will
14764  * try to make port active and in good standing.
14765  *
14766  * NOTE: Port multiplier is supported.
14767  */
14768 
14769 static int
14770 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst,
14771     sata_device_t *sata_device)
14772 {
14773 	sata_pmport_info_t	*pmportinfo = NULL;
14774 	uint8_t cport, pmport, qual;
14775 	int rv = 0;
14776 
14777 	cport = sata_device->satadev_addr.cport;
14778 	pmport = sata_device->satadev_addr.pmport;
14779 	qual = sata_device->satadev_addr.qual;
14780 
14781 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
14782 	if (qual == SATA_ADDR_DCPORT)
14783 		qual = SATA_ADDR_CPORT;
14784 	else
14785 		qual = SATA_ADDR_PMPORT;
14786 
14787 	if (qual == SATA_ADDR_PMPORT)
14788 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14789 
14790 	/*
14791 	 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->
14792 	 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate().
14793 	 * Perform sanity check now.
14794 	 */
14795 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) {
14796 		/* No physical port activation supported. */
14797 		return (EINVAL);
14798 	}
14799 
14800 	/* Just ask HBA driver to activate port */
14801 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
14802 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
14803 		/*
14804 		 * Port activation failure.
14805 		 */
14806 		if (qual == SATA_ADDR_CPORT) {
14807 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14808 			    cport)->cport_mutex);
14809 			sata_update_port_info(sata_hba_inst, sata_device);
14810 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
14811 				SATA_CPORT_STATE(sata_hba_inst, cport) =
14812 				    SATA_PSTATE_FAILED;
14813 				SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14814 				    "sata_hba_ioctl: connect: failed to "
14815 				    "activate SATA port %d", cport);
14816 			}
14817 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14818 			    cport)->cport_mutex);
14819 		} else { /* port multiplier device port */
14820 			mutex_enter(&pmportinfo->pmport_mutex);
14821 			sata_update_pmport_info(sata_hba_inst, sata_device);
14822 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
14823 				SATA_PMPORT_STATE(sata_hba_inst, cport,
14824 				    pmport) = SATA_PSTATE_FAILED;
14825 				SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
14826 				    "sata_hba_ioctl: connect: failed to "
14827 				    "activate SATA port %d:%d", cport, pmport);
14828 			}
14829 			mutex_exit(&pmportinfo->pmport_mutex);
14830 		}
14831 		return (EIO);
14832 	}
14833 
14834 	/* Virgin port state - will be updated by the port re-probe. */
14835 	if (qual == SATA_ADDR_CPORT) {
14836 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14837 		    cport)->cport_mutex);
14838 		SATA_CPORT_STATE(sata_hba_inst, cport) = 0;
14839 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14840 		    cport)->cport_mutex);
14841 	} else { /* port multiplier device port */
14842 		mutex_enter(&pmportinfo->pmport_mutex);
14843 		SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0;
14844 		mutex_exit(&pmportinfo->pmport_mutex);
14845 	}
14846 
14847 	/*
14848 	 * Probe the port to find its state and attached device.
14849 	 */
14850 	if (sata_reprobe_port(sata_hba_inst, sata_device,
14851 	    SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE)
14852 		rv = EIO;
14853 
14854 	/*
14855 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14856 	 * without the hint
14857 	 */
14858 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14859 	    SE_NO_HINT);
14860 
14861 	/*
14862 	 * If there is a device attached to the port, emit
14863 	 * a message.
14864 	 */
14865 	if (sata_device->satadev_type != SATA_DTYPE_NONE) {
14866 
14867 		if (qual == SATA_ADDR_CPORT) {
14868 			if (sata_device->satadev_type == SATA_DTYPE_PMULT) {
14869 				sata_log(sata_hba_inst, CE_WARN,
14870 				    "SATA port multiplier detected "
14871 				    "at port %d", cport);
14872 			} else {
14873 				sata_log(sata_hba_inst, CE_WARN,
14874 				    "SATA device detected at port %d", cport);
14875 				if (sata_device->satadev_type ==
14876 				    SATA_DTYPE_UNKNOWN) {
14877 				/*
14878 				 * A device was not successfully identified
14879 				 */
14880 				sata_log(sata_hba_inst, CE_WARN,
14881 				    "Could not identify SATA "
14882 				    "device at port %d", cport);
14883 				}
14884 			}
14885 		} else { /* port multiplier device port */
14886 			sata_log(sata_hba_inst, CE_WARN,
14887 			    "SATA device detected at port %d:%d",
14888 			    cport, pmport);
14889 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
14890 				/*
14891 				 * A device was not successfully identified
14892 				 */
14893 				sata_log(sata_hba_inst, CE_WARN,
14894 				    "Could not identify SATA "
14895 				    "device at port %d:%d", cport, pmport);
14896 			}
14897 		}
14898 	}
14899 
14900 	return (rv);
14901 }
14902 
14903 
14904 /*
14905  * Process sata device unconfigure request.
14906  * The unconfigure operation uses generic nexus operation to
14907  * offline a device. It leaves a target device node attached.
14908  * and obviously sata_drive_info attached as well, because
14909  * from the hardware point of view nothing has changed.
14910  */
14911 static int
14912 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst,
14913     sata_device_t *sata_device)
14914 {
14915 	int rv = 0;
14916 	dev_info_t *tdip;
14917 
14918 	/* We are addressing attached device, not a port */
14919 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
14920 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
14921 	else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT)
14922 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
14923 
14924 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
14925 	    &sata_device->satadev_addr)) != NULL) {
14926 
14927 		if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) {
14928 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14929 			    "sata_hba_ioctl: unconfigure: "
14930 			    "failed to unconfigure device at SATA port %d:%d",
14931 			    sata_device->satadev_addr.cport,
14932 			    sata_device->satadev_addr.pmport));
14933 			rv = EIO;
14934 		}
14935 		/*
14936 		 * The target node devi_state should be marked with
14937 		 * DEVI_DEVICE_OFFLINE by ndi_devi_offline().
14938 		 * This would be the indication for cfgadm that
14939 		 * the AP node occupant state is 'unconfigured'.
14940 		 */
14941 
14942 	} else {
14943 		/*
14944 		 * This would indicate a failure on the part of cfgadm
14945 		 * to detect correct state of the node prior to this
14946 		 * call - one cannot unconfigure non-existing device.
14947 		 */
14948 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14949 		    "sata_hba_ioctl: unconfigure: "
14950 		    "attempt to unconfigure non-existing device "
14951 		    "at SATA port %d:%d",
14952 		    sata_device->satadev_addr.cport,
14953 		    sata_device->satadev_addr.pmport));
14954 		rv = ENXIO;
14955 	}
14956 	return (rv);
14957 }
14958 
14959 /*
14960  * Process sata device configure request
14961  * If port is in a failed state, operation is aborted - one has to use
14962  * an explicit connect or port activate request to try to get a port into
14963  * non-failed mode. Port reset wil also work in such situation.
14964  * If the port is in disconnected (shutdown) state, the connect operation is
14965  * attempted prior to any other action.
14966  * When port is in the active state, there is a device attached and the target
14967  * node exists, a device was most likely offlined.
14968  * If target node does not exist, a new target node is created. In both cases
14969  * an attempt is made to online (configure) the device.
14970  *
14971  * NOTE: Port multiplier is supported.
14972  */
14973 static int
14974 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst,
14975     sata_device_t *sata_device)
14976 {
14977 	int cport, pmport, qual;
14978 	int rval;
14979 	boolean_t target = B_TRUE;
14980 	sata_cport_info_t *cportinfo;
14981 	sata_pmport_info_t *pmportinfo = NULL;
14982 	dev_info_t *tdip;
14983 	sata_drive_info_t *sdinfo;
14984 
14985 	cport = sata_device->satadev_addr.cport;
14986 	pmport = sata_device->satadev_addr.pmport;
14987 	qual = sata_device->satadev_addr.qual;
14988 
14989 	/* Get current port state */
14990 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14991 	    (SATA_DIP(sata_hba_inst), sata_device);
14992 
14993 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
14994 	if (qual == SATA_ADDR_DPMPORT) {
14995 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14996 		mutex_enter(&pmportinfo->pmport_mutex);
14997 		sata_update_pmport_info(sata_hba_inst, sata_device);
14998 		if (rval != SATA_SUCCESS ||
14999 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
15000 			/*
15001 			 * Obviously, device on a failed port is not visible
15002 			 */
15003 			mutex_exit(&pmportinfo->pmport_mutex);
15004 			return (ENXIO);
15005 		}
15006 		mutex_exit(&pmportinfo->pmport_mutex);
15007 	} else {
15008 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15009 		    cport)->cport_mutex);
15010 		sata_update_port_info(sata_hba_inst, sata_device);
15011 		if (rval != SATA_SUCCESS ||
15012 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
15013 			/*
15014 			 * Obviously, device on a failed port is not visible
15015 			 */
15016 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15017 			    cport)->cport_mutex);
15018 			return (ENXIO);
15019 		}
15020 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15021 		    cport)->cport_mutex);
15022 	}
15023 
15024 	if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) {
15025 		/* need to activate port */
15026 		target = B_FALSE;
15027 
15028 		/* Sanity check */
15029 		if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
15030 			return (ENXIO);
15031 
15032 		/* Just let HBA driver to activate port */
15033 		if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
15034 		    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15035 			/*
15036 			 * Port activation failure - do not change port state
15037 			 * unless the state returned by HBA indicates a port
15038 			 * failure.
15039 			 */
15040 			if (qual == SATA_ADDR_DPMPORT) {
15041 				mutex_enter(&pmportinfo->pmport_mutex);
15042 				sata_update_pmport_info(sata_hba_inst,
15043 				    sata_device);
15044 				if (sata_device->satadev_state &
15045 				    SATA_PSTATE_FAILED)
15046 					pmportinfo->pmport_state =
15047 					    SATA_PSTATE_FAILED;
15048 				mutex_exit(&pmportinfo->pmport_mutex);
15049 			} else {
15050 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15051 				    cport)->cport_mutex);
15052 				sata_update_port_info(sata_hba_inst,
15053 				    sata_device);
15054 				if (sata_device->satadev_state &
15055 				    SATA_PSTATE_FAILED)
15056 					cportinfo->cport_state =
15057 					    SATA_PSTATE_FAILED;
15058 				mutex_exit(&SATA_CPORT_INFO(
15059 				    sata_hba_inst, cport)->cport_mutex);
15060 			}
15061 		}
15062 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15063 		    "sata_hba_ioctl: configure: "
15064 		    "failed to activate SATA port %d:%d",
15065 		    cport, pmport));
15066 		return (EIO);
15067 	}
15068 	/*
15069 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15070 	 * without the hint.
15071 	 */
15072 	sata_gen_sysevent(sata_hba_inst,
15073 	    &sata_device->satadev_addr, SE_NO_HINT);
15074 
15075 	/* Virgin port state */
15076 	if (qual == SATA_ADDR_DPMPORT) {
15077 		mutex_enter(&pmportinfo->pmport_mutex);
15078 		pmportinfo->pmport_state = 0;
15079 		mutex_exit(&pmportinfo->pmport_mutex);
15080 	} else {
15081 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15082 		    cport)-> cport_mutex);
15083 		cportinfo->cport_state = 0;
15084 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15085 		    cport)->cport_mutex);
15086 	}
15087 	/*
15088 	 * Always reprobe port, to get current device info.
15089 	 */
15090 	if (sata_reprobe_port(sata_hba_inst, sata_device,
15091 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
15092 		return (EIO);
15093 
15094 	if (sata_device->satadev_type != SATA_DTYPE_NONE && target == B_FALSE) {
15095 		if (qual == SATA_ADDR_DPMPORT) {
15096 			/*
15097 			 * That's the transition from "inactive" port
15098 			 * to active one with device attached.
15099 			 */
15100 			sata_log(sata_hba_inst, CE_WARN,
15101 			    "SATA device detected at port %d:%d",
15102 			    cport, pmport);
15103 		} else {
15104 			/*
15105 			 * When PM is attached to the cport and cport is
15106 			 * activated, every PM device port needs to be reprobed.
15107 			 * We need to emit message for all devices detected
15108 			 * at port multiplier's device ports.
15109 			 * Add such code here.
15110 			 * For now, just inform about device attached to
15111 			 * cport.
15112 			 */
15113 			sata_log(sata_hba_inst, CE_WARN,
15114 			    "SATA device detected at port %d", cport);
15115 		}
15116 	}
15117 
15118 	/*
15119 	 * This is where real configuration operation starts.
15120 	 *
15121 	 * When PM is attached to the cport and cport is activated,
15122 	 * devices attached PM device ports may have to be configured
15123 	 * explicitly. This may change when port multiplier is supported.
15124 	 * For now, configure only disks and other valid target devices.
15125 	 */
15126 	if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) {
15127 		if (qual == SATA_ADDR_DCPORT) {
15128 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
15129 				/*
15130 				 * A device was not successfully identified
15131 				 */
15132 				sata_log(sata_hba_inst, CE_WARN,
15133 				    "Could not identify SATA "
15134 				    "device at port %d", cport);
15135 			}
15136 		} else { /* port multiplier device port */
15137 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
15138 				/*
15139 				 * A device was not successfully identified
15140 				 */
15141 				sata_log(sata_hba_inst, CE_WARN,
15142 				    "Could not identify SATA "
15143 				    "device at port %d:%d", cport, pmport);
15144 			}
15145 		}
15146 		return (ENXIO);		/* No device to configure */
15147 	}
15148 
15149 	/*
15150 	 * Here we may have a device in reset condition,
15151 	 * but because we are just configuring it, there is
15152 	 * no need to process the reset other than just
15153 	 * to clear device reset condition in the HBA driver.
15154 	 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will
15155 	 * cause a first command sent the HBA driver with the request
15156 	 * to clear device reset condition.
15157 	 */
15158 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15159 	if (qual == SATA_ADDR_DPMPORT)
15160 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15161 	else
15162 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
15163 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
15164 	if (sdinfo == NULL) {
15165 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15166 		return (ENXIO);
15167 	}
15168 	if (sdinfo->satadrv_event_flags &
15169 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
15170 		sdinfo->satadrv_event_flags = 0;
15171 	}
15172 	sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
15173 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15174 
15175 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
15176 	    &sata_device->satadev_addr)) != NULL) {
15177 		/*
15178 		 * Target node exists. Verify, that it belongs
15179 		 * to existing, attached device and not to
15180 		 * a removed device.
15181 		 */
15182 		if (sata_check_device_removed(tdip) == B_TRUE) {
15183 			if (qual == SATA_ADDR_DPMPORT)
15184 				sata_log(sata_hba_inst, CE_WARN,
15185 				    "SATA device at port %d cannot be "
15186 				    "configured. "
15187 				    "Application(s) accessing "
15188 				    "previously attached device "
15189 				    "have to release it before newly "
15190 				    "inserted device can be made accessible.",
15191 				    cport);
15192 			else
15193 				sata_log(sata_hba_inst, CE_WARN,
15194 				    "SATA device at port %d:%d cannot be"
15195 				    "configured. "
15196 				    "Application(s) accessing "
15197 				    "previously attached device "
15198 				    "have to release it before newly "
15199 				    "inserted device can be made accessible.",
15200 				    cport, pmport);
15201 			return (EIO);
15202 		}
15203 		/*
15204 		 * Device was not removed and re-inserted.
15205 		 * Try to online it.
15206 		 */
15207 		if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) {
15208 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15209 			    "sata_hba_ioctl: configure: "
15210 			    "onlining device at SATA port "
15211 			    "%d:%d failed", cport, pmport));
15212 			return (EIO);
15213 		}
15214 
15215 		if (qual == SATA_ADDR_DPMPORT) {
15216 			mutex_enter(&pmportinfo->pmport_mutex);
15217 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
15218 			mutex_exit(&pmportinfo->pmport_mutex);
15219 		} else {
15220 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15221 			    cport)->cport_mutex);
15222 			cportinfo-> cport_tgtnode_clean = B_TRUE;
15223 			mutex_exit(&SATA_CPORT_INFO(
15224 			    sata_hba_inst, cport)->cport_mutex);
15225 		}
15226 	} else {
15227 		/*
15228 		 * No target node - need to create a new target node.
15229 		 */
15230 		if (qual == SATA_ADDR_DPMPORT) {
15231 			mutex_enter(&pmportinfo->pmport_mutex);
15232 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
15233 			mutex_exit(&pmportinfo->pmport_mutex);
15234 		} else {
15235 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15236 			    cport_mutex);
15237 			cportinfo-> cport_tgtnode_clean = B_TRUE;
15238 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15239 			    cport_mutex);
15240 		}
15241 
15242 		tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
15243 		    sata_hba_inst, &sata_device->satadev_addr);
15244 		if (tdip == NULL) {
15245 			/* Configure operation failed */
15246 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15247 			    "sata_hba_ioctl: configure: "
15248 			    "configuring SATA device at port %d:%d "
15249 			    "failed", cport, pmport));
15250 			return (EIO);
15251 		}
15252 	}
15253 	return (0);
15254 }
15255 
15256 
15257 /*
15258  * Process ioctl deactivate port request.
15259  * Arbitrarily unconfigure attached device, if any.
15260  * Even if the unconfigure fails, proceed with the
15261  * port deactivation.
15262  *
15263  * NOTE: Port Multiplier is supported now.
15264  */
15265 
15266 static int
15267 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst,
15268     sata_device_t *sata_device)
15269 {
15270 	int cport, pmport, qual;
15271 	int rval, rv = 0;
15272 	int npmport;
15273 	sata_cport_info_t *cportinfo;
15274 	sata_pmport_info_t *pmportinfo;
15275 	sata_pmult_info_t *pmultinfo;
15276 	dev_info_t *tdip;
15277 	sata_drive_info_t *sdinfo = NULL;
15278 	sata_device_t subsdevice;
15279 
15280 	/* Sanity check */
15281 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL)
15282 		return (ENOTSUP);
15283 
15284 	cport = sata_device->satadev_addr.cport;
15285 	pmport = sata_device->satadev_addr.pmport;
15286 	qual = sata_device->satadev_addr.qual;
15287 
15288 	/* SCSI_TO_SATA_ADDR_QUAL() translate ap_id into a device qualifier */
15289 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
15290 	if (qual == SATA_ADDR_DCPORT)
15291 		qual = SATA_ADDR_CPORT;
15292 	else
15293 		qual = SATA_ADDR_PMPORT;
15294 
15295 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
15296 	if (qual == SATA_ADDR_PMPORT)
15297 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
15298 
15299 	/*
15300 	 * Processing port multiplier
15301 	 */
15302 	if (qual == SATA_ADDR_CPORT &&
15303 	    SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) {
15304 		mutex_enter(&cportinfo->cport_mutex);
15305 
15306 		/* Deactivate all sub-deices */
15307 		pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
15308 		if (pmultinfo != NULL) {
15309 			for (npmport = 0; npmport < SATA_NUM_PMPORTS(
15310 			    sata_hba_inst, cport); npmport++) {
15311 
15312 				subsdevice.satadev_addr.cport = cport;
15313 				subsdevice.satadev_addr.pmport =
15314 				    (uint8_t)npmport;
15315 				subsdevice.satadev_addr.qual =
15316 				    SATA_ADDR_DPMPORT;
15317 
15318 				SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
15319 				    "sata_hba_ioctl: deactivate: trying to "
15320 				    "deactivate SATA port %d:%d",
15321 				    cport, npmport);
15322 
15323 				mutex_exit(&cportinfo->cport_mutex);
15324 				if (sata_ioctl_deactivate(sata_hba_inst,
15325 				    &subsdevice) == SATA_SUCCESS) {
15326 					SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
15327 					    "[Deactivate] device at port %d:%d "
15328 					    "successfully.", cport, npmport);
15329 				}
15330 				mutex_enter(&cportinfo->cport_mutex);
15331 			}
15332 		}
15333 
15334 		/* Deactivate the port multiplier now. */
15335 		cportinfo->cport_state &= ~SATA_STATE_READY;
15336 		mutex_exit(&cportinfo->cport_mutex);
15337 
15338 		sata_device->satadev_addr.qual = qual;
15339 		rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
15340 		    (SATA_DIP(sata_hba_inst), sata_device);
15341 
15342 		sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15343 		    SE_NO_HINT);
15344 
15345 		mutex_enter(&cportinfo->cport_mutex);
15346 		sata_update_port_info(sata_hba_inst, sata_device);
15347 		if (rval != SATA_SUCCESS) {
15348 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15349 				cportinfo->cport_state = SATA_PSTATE_FAILED;
15350 			}
15351 			rv = EIO;
15352 		} else {
15353 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
15354 		}
15355 		mutex_exit(&cportinfo->cport_mutex);
15356 
15357 		return (rv);
15358 	}
15359 
15360 	/*
15361 	 * Process non-port-multiplier device - it could be a drive connected
15362 	 * to a port multiplier port or a controller port.
15363 	 */
15364 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15365 	if (qual == SATA_ADDR_CPORT) {
15366 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
15367 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
15368 			/* deal only with valid devices */
15369 			if ((cportinfo->cport_dev_type &
15370 			    SATA_VALID_DEV_TYPE) != 0)
15371 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
15372 		}
15373 		cportinfo->cport_state &= ~SATA_STATE_READY;
15374 	} else {
15375 		/* Port multiplier device port */
15376 		mutex_enter(&pmportinfo->pmport_mutex);
15377 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15378 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
15379 		    (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0)
15380 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
15381 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
15382 		mutex_exit(&pmportinfo->pmport_mutex);
15383 	}
15384 
15385 	if (sdinfo != NULL) {
15386 		/*
15387 		 * If a target node exists, try to offline a device and
15388 		 * to remove a target node.
15389 		 */
15390 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15391 		    cport_mutex);
15392 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
15393 		    &sata_device->satadev_addr);
15394 		if (tdip != NULL) {
15395 			/* target node exist */
15396 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
15397 			    "sata_hba_ioctl: port deactivate: "
15398 			    "target node exists.", NULL);
15399 
15400 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) !=
15401 			    NDI_SUCCESS) {
15402 				SATA_LOG_D((sata_hba_inst, CE_WARN,
15403 				    "sata_hba_ioctl: port deactivate: "
15404 				    "failed to unconfigure device at port "
15405 				    "%d:%d before deactivating the port",
15406 				    cport, pmport));
15407 				/*
15408 				 * Set DEVICE REMOVED state in the target
15409 				 * node. It will prevent an access to
15410 				 * the device even when a new device is
15411 				 * attached, until the old target node is
15412 				 * released, removed and recreated for a new
15413 				 * device.
15414 				 */
15415 				sata_set_device_removed(tdip);
15416 
15417 				/*
15418 				 * Instruct the event daemon to try the
15419 				 * target node cleanup later.
15420 				 */
15421 				sata_set_target_node_cleanup(sata_hba_inst,
15422 				    &sata_device->satadev_addr);
15423 			}
15424 		}
15425 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15426 		    cport_mutex);
15427 		/*
15428 		 * In any case, remove and release sata_drive_info
15429 		 * structure.
15430 		 */
15431 		if (qual == SATA_ADDR_CPORT) {
15432 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
15433 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
15434 		} else { /* port multiplier device port */
15435 			mutex_enter(&pmportinfo->pmport_mutex);
15436 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
15437 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
15438 			mutex_exit(&pmportinfo->pmport_mutex);
15439 		}
15440 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
15441 	}
15442 
15443 	if (qual == SATA_ADDR_CPORT) {
15444 		cportinfo->cport_state &= ~(SATA_STATE_PROBED |
15445 		    SATA_STATE_PROBING);
15446 	} else if (qual == SATA_ADDR_PMPORT) {
15447 		mutex_enter(&pmportinfo->pmport_mutex);
15448 		pmportinfo->pmport_state &= ~(SATA_STATE_PROBED |
15449 		    SATA_STATE_PROBING);
15450 		mutex_exit(&pmportinfo->pmport_mutex);
15451 	}
15452 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15453 
15454 	/* Just let HBA driver to deactivate port */
15455 	sata_device->satadev_addr.qual = qual;
15456 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
15457 	    (SATA_DIP(sata_hba_inst), sata_device);
15458 
15459 	/*
15460 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15461 	 * without the hint
15462 	 */
15463 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15464 	    SE_NO_HINT);
15465 
15466 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15467 	sata_update_port_info(sata_hba_inst, sata_device);
15468 	if (qual == SATA_ADDR_CPORT) {
15469 		if (rval != SATA_SUCCESS) {
15470 			/*
15471 			 * Port deactivation failure - do not change port state
15472 			 * unless the state returned by HBA indicates a port
15473 			 * failure.
15474 			 */
15475 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15476 				SATA_CPORT_STATE(sata_hba_inst, cport) =
15477 				    SATA_PSTATE_FAILED;
15478 			}
15479 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15480 			    "sata_hba_ioctl: port deactivate: "
15481 			    "cannot deactivate SATA port %d", cport));
15482 			rv = EIO;
15483 		} else {
15484 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
15485 		}
15486 	} else {
15487 		mutex_enter(&pmportinfo->pmport_mutex);
15488 		if (rval != SATA_SUCCESS) {
15489 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15490 				SATA_PMPORT_STATE(sata_hba_inst, cport,
15491 				    pmport) = SATA_PSTATE_FAILED;
15492 			}
15493 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15494 			    "sata_hba_ioctl: port deactivate: "
15495 			    "cannot deactivate SATA port %d:%d",
15496 			    cport, pmport));
15497 			rv = EIO;
15498 		} else {
15499 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
15500 		}
15501 		mutex_exit(&pmportinfo->pmport_mutex);
15502 	}
15503 
15504 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15505 
15506 	return (rv);
15507 }
15508 
15509 /*
15510  * Process ioctl port activate request.
15511  *
15512  * NOTE: Port multiplier is supported now.
15513  */
15514 static int
15515 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst,
15516     sata_device_t *sata_device)
15517 {
15518 	int cport, pmport, qual;
15519 	sata_cport_info_t *cportinfo;
15520 	sata_pmport_info_t *pmportinfo = NULL;
15521 	boolean_t dev_existed = B_TRUE;
15522 
15523 	/* Sanity check */
15524 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
15525 		return (ENOTSUP);
15526 
15527 	cport = sata_device->satadev_addr.cport;
15528 	pmport = sata_device->satadev_addr.pmport;
15529 	qual = sata_device->satadev_addr.qual;
15530 
15531 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
15532 
15533 	/*
15534 	 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL())
15535 	 * is a device. But what we are dealing with is port/pmport.
15536 	 */
15537 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
15538 	if (qual == SATA_ADDR_DCPORT)
15539 		sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT;
15540 	else
15541 		sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT;
15542 
15543 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15544 	if (qual == SATA_ADDR_PMPORT) {
15545 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
15546 		if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN ||
15547 		    pmportinfo->pmport_dev_type == SATA_DTYPE_NONE)
15548 			dev_existed = B_FALSE;
15549 	} else { /* cport */
15550 		if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN ||
15551 		    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
15552 			dev_existed = B_FALSE;
15553 	}
15554 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15555 
15556 	/* Just let HBA driver to activate port, if necessary */
15557 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
15558 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15559 		/*
15560 		 * Port activation failure - do not change port state unless
15561 		 * the state returned by HBA indicates a port failure.
15562 		 */
15563 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15564 		    cport)->cport_mutex);
15565 		sata_update_port_info(sata_hba_inst, sata_device);
15566 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15567 			if (qual == SATA_ADDR_PMPORT) {
15568 				mutex_enter(&pmportinfo->pmport_mutex);
15569 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
15570 				mutex_exit(&pmportinfo->pmport_mutex);
15571 			} else
15572 				cportinfo->cport_state = SATA_PSTATE_FAILED;
15573 
15574 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15575 			    cport)->cport_mutex);
15576 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15577 			    "sata_hba_ioctl: port activate: cannot activate "
15578 			    "SATA port %d:%d", cport, pmport));
15579 			return (EIO);
15580 		}
15581 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15582 	}
15583 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15584 	if (qual == SATA_ADDR_PMPORT) {
15585 		mutex_enter(&pmportinfo->pmport_mutex);
15586 		pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN;
15587 		mutex_exit(&pmportinfo->pmport_mutex);
15588 	} else
15589 		cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN;
15590 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15591 
15592 	/*
15593 	 * Re-probe port to find its current state and possibly attached device.
15594 	 * Port re-probing may change the cportinfo device type if device is
15595 	 * found attached.
15596 	 * If port probing failed, the device type would be set to
15597 	 * SATA_DTYPE_NONE.
15598 	 */
15599 	(void) sata_reprobe_port(sata_hba_inst, sata_device,
15600 	    SATA_DEV_IDENTIFY_RETRY);
15601 
15602 	/*
15603 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15604 	 * without the hint.
15605 	 */
15606 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15607 	    SE_NO_HINT);
15608 
15609 	if (dev_existed == B_FALSE) {
15610 		if (qual == SATA_ADDR_PMPORT &&
15611 		    pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
15612 			/*
15613 			 * That's the transition from the "inactive" port state
15614 			 * or the active port without a device attached to the
15615 			 * active port state with a device attached.
15616 			 */
15617 			sata_log(sata_hba_inst, CE_WARN,
15618 			    "SATA device detected at port %d:%d",
15619 			    cport, pmport);
15620 		} else if (qual == SATA_ADDR_CPORT &&
15621 		    cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
15622 			/*
15623 			 * That's the transition from the "inactive" port state
15624 			 * or the active port without a device attached to the
15625 			 * active port state with a device attached.
15626 			 */
15627 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
15628 				sata_log(sata_hba_inst, CE_WARN,
15629 				    "SATA device detected at port %d", cport);
15630 			} else {
15631 				sata_log(sata_hba_inst, CE_WARN,
15632 				    "SATA port multiplier detected at port %d",
15633 				    cport);
15634 			}
15635 		}
15636 	}
15637 	return (0);
15638 }
15639 
15640 
15641 
15642 /*
15643  * Process ioctl reset port request.
15644  *
15645  * NOTE: Port-Multiplier is supported.
15646  */
15647 static int
15648 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst,
15649     sata_device_t *sata_device)
15650 {
15651 	int cport, pmport, qual;
15652 	int rv = 0;
15653 
15654 	cport = sata_device->satadev_addr.cport;
15655 	pmport = sata_device->satadev_addr.pmport;
15656 	qual = sata_device->satadev_addr.qual;
15657 
15658 	/*
15659 	 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL())
15660 	 * is a device. But what we are dealing with is port/pmport.
15661 	 */
15662 	if (qual == SATA_ADDR_DCPORT)
15663 		sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT;
15664 	else
15665 		sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT;
15666 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
15667 
15668 	/* Sanity check */
15669 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15670 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15671 		    "sata_hba_ioctl: sata_hba_tran missing required "
15672 		    "function sata_tran_reset_dport"));
15673 		return (ENOTSUP);
15674 	}
15675 
15676 	/* Ask HBA to reset port */
15677 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
15678 	    sata_device) != SATA_SUCCESS) {
15679 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15680 		    "sata_hba_ioctl: reset port: failed %d:%d",
15681 		    cport, pmport));
15682 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15683 		    cport_mutex);
15684 		sata_update_port_info(sata_hba_inst, sata_device);
15685 		if (qual == SATA_ADDR_CPORT)
15686 			SATA_CPORT_STATE(sata_hba_inst, cport) =
15687 			    SATA_PSTATE_FAILED;
15688 		else {
15689 			mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport,
15690 			    pmport));
15691 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
15692 			    SATA_PSTATE_FAILED;
15693 			mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport,
15694 			    pmport));
15695 		}
15696 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15697 		    cport_mutex);
15698 		rv = EIO;
15699 	}
15700 
15701 	return (rv);
15702 }
15703 
15704 /*
15705  * Process ioctl reset device request.
15706  *
15707  * NOTE: Port multiplier is supported.
15708  */
15709 static int
15710 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst,
15711     sata_device_t *sata_device)
15712 {
15713 	sata_drive_info_t *sdinfo = NULL;
15714 	sata_pmult_info_t *pmultinfo = NULL;
15715 	int cport, pmport;
15716 	int rv = 0;
15717 
15718 	/* Sanity check */
15719 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15720 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15721 		    "sata_hba_ioctl: sata_hba_tran missing required "
15722 		    "function sata_tran_reset_dport"));
15723 		return (ENOTSUP);
15724 	}
15725 
15726 	cport = sata_device->satadev_addr.cport;
15727 	pmport = sata_device->satadev_addr.pmport;
15728 
15729 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15730 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
15731 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
15732 		    SATA_DTYPE_PMULT)
15733 			pmultinfo = SATA_CPORT_INFO(sata_hba_inst, cport)->
15734 			    cport_devp.cport_sata_pmult;
15735 		else
15736 			sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
15737 			    sata_device->satadev_addr.cport);
15738 	} else { /* port multiplier */
15739 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15740 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
15741 		    sata_device->satadev_addr.cport,
15742 		    sata_device->satadev_addr.pmport);
15743 	}
15744 	if (sdinfo == NULL && pmultinfo == NULL) {
15745 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15746 		return (EINVAL);
15747 	}
15748 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15749 
15750 	/* Ask HBA to reset device */
15751 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
15752 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15753 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15754 		    "sata_hba_ioctl: reset device: failed at port %d:%d",
15755 		    cport, pmport));
15756 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15757 		    cport_mutex);
15758 		sata_update_port_info(sata_hba_inst, sata_device);
15759 		/*
15760 		 * Device info structure remains attached. Another device reset
15761 		 * or port disconnect/connect and re-probing is
15762 		 * needed to change it's state
15763 		 */
15764 		if (sdinfo != NULL) {
15765 			sdinfo->satadrv_state &= ~SATA_STATE_READY;
15766 			sdinfo->satadrv_state |= SATA_DSTATE_FAILED;
15767 		} else if (pmultinfo != NULL) {
15768 			pmultinfo->pmult_state &= ~SATA_STATE_READY;
15769 			pmultinfo->pmult_state |= SATA_DSTATE_FAILED;
15770 		}
15771 
15772 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15773 		rv = EIO;
15774 	}
15775 	/*
15776 	 * If attached device was a port multiplier, some extra processing
15777 	 * may be needed to bring it back. SATA specification requies a
15778 	 * mandatory software reset on host port to reliably enumerate a port
15779 	 * multiplier, the HBA driver should handle that after reset
15780 	 * operation.
15781 	 */
15782 	return (rv);
15783 }
15784 
15785 
15786 /*
15787  * Process ioctl reset all request.
15788  */
15789 static int
15790 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst)
15791 {
15792 	sata_device_t sata_device;
15793 	int rv = 0;
15794 	int tcport;
15795 
15796 	sata_device.satadev_rev = SATA_DEVICE_REV;
15797 
15798 	/*
15799 	 * There is no protection here for configured devices.
15800 	 */
15801 	/* Sanity check */
15802 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15803 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15804 		    "sata_hba_ioctl: sata_hba_tran missing required "
15805 		    "function sata_tran_reset_dport"));
15806 		return (ENOTSUP);
15807 	}
15808 
15809 	/*
15810 	 * Need to lock all ports, not just one.
15811 	 * If any port is locked by event processing, fail the whole operation.
15812 	 * One port is already locked, but for simplicity lock it again.
15813 	 */
15814 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
15815 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15816 		    cport_mutex);
15817 		if (((SATA_CPORT_INFO(sata_hba_inst, tcport)->
15818 		    cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) {
15819 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15820 			    cport_mutex);
15821 			rv = EBUSY;
15822 			break;
15823 		} else {
15824 			/*
15825 			 * It is enough to lock cport in command-based
15826 			 * switching mode.
15827 			 */
15828 			SATA_CPORT_INFO(sata_hba_inst, tcport)->
15829 			    cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
15830 		}
15831 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15832 		    cport_mutex);
15833 	}
15834 
15835 	if (rv == 0) {
15836 		/*
15837 		 * All cports were successfully locked.
15838 		 * Reset main SATA controller.
15839 		 * Set the device address to port 0, to have a valid device
15840 		 * address.
15841 		 */
15842 		sata_device.satadev_addr.qual = SATA_ADDR_CNTRL;
15843 		sata_device.satadev_addr.cport = 0;
15844 		sata_device.satadev_addr.pmport = 0;
15845 
15846 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
15847 		    (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) {
15848 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15849 			    "sata_hba_ioctl: reset controller failed"));
15850 			return (EIO);
15851 		}
15852 	}
15853 	/*
15854 	 * Unlock all ports
15855 	 */
15856 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
15857 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15858 		    cport_mutex);
15859 		SATA_CPORT_INFO(sata_hba_inst, tcport)->
15860 		    cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
15861 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15862 		    cport_mutex);
15863 	}
15864 
15865 	/*
15866 	 * This operation returns EFAULT if either reset
15867 	 * controller failed or a re-probing of any port failed.
15868 	 */
15869 	return (rv);
15870 }
15871 
15872 
15873 /*
15874  * Process ioctl port self test request.
15875  *
15876  * NOTE: Port multiplier code is not completed nor tested.
15877  */
15878 static int
15879 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst,
15880     sata_device_t *sata_device)
15881 {
15882 	int cport, pmport, qual;
15883 	int rv = 0;
15884 
15885 	/* Sanity check */
15886 	if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL)
15887 		return (ENOTSUP);
15888 
15889 	cport = sata_device->satadev_addr.cport;
15890 	pmport = sata_device->satadev_addr.pmport;
15891 	qual = sata_device->satadev_addr.qual;
15892 
15893 	/*
15894 	 * There is no protection here for a configured
15895 	 * device attached to this port.
15896 	 */
15897 
15898 	if ((*SATA_SELFTEST_FUNC(sata_hba_inst))
15899 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15900 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15901 		    "sata_hba_ioctl: port selftest: "
15902 		    "failed port %d:%d", cport, pmport));
15903 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15904 		    cport_mutex);
15905 		sata_update_port_info(sata_hba_inst, sata_device);
15906 		if (qual == SATA_ADDR_CPORT)
15907 			SATA_CPORT_STATE(sata_hba_inst, cport) =
15908 			    SATA_PSTATE_FAILED;
15909 		else { /* port multiplier device port */
15910 			mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst,
15911 			    cport, pmport));
15912 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
15913 			    SATA_PSTATE_FAILED;
15914 			mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst,
15915 			    cport, pmport));
15916 		}
15917 
15918 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15919 		    cport_mutex);
15920 		return (EIO);
15921 	}
15922 	/*
15923 	 * Beacuse the port was reset in the course of testing, it should be
15924 	 * re-probed and attached device state should be restored. At this
15925 	 * point the port state is unknown - it's state is HBA-specific.
15926 	 * Force port re-probing to get it into a known state.
15927 	 */
15928 	if (sata_reprobe_port(sata_hba_inst, sata_device,
15929 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
15930 		rv = EIO;
15931 	return (rv);
15932 }
15933 
15934 
15935 /*
15936  * sata_cfgadm_state:
15937  * Use the sata port state and state of the target node to figure out
15938  * the cfgadm_state.
15939  *
15940  * The port argument is a value with encoded cport,
15941  * pmport and address qualifier, in the same manner as a scsi target number.
15942  * SCSI_TO_SATA_CPORT macro extracts cport number,
15943  * SCSI_TO_SATA_PMPORT extracts pmport number and
15944  * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag.
15945  *
15946  * Port multiplier is supported.
15947  */
15948 
15949 static void
15950 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port,
15951     devctl_ap_state_t *ap_state)
15952 {
15953 	uint8_t		cport, pmport, qual;
15954 	uint32_t	port_state, pmult_state;
15955 	uint32_t	dev_type;
15956 	sata_drive_info_t *sdinfo;
15957 
15958 	cport = SCSI_TO_SATA_CPORT(port);
15959 	pmport = SCSI_TO_SATA_PMPORT(port);
15960 	qual = SCSI_TO_SATA_ADDR_QUAL(port);
15961 
15962 	/* Check cport state */
15963 	port_state = SATA_CPORT_STATE(sata_hba_inst, cport);
15964 	if (port_state & SATA_PSTATE_SHUTDOWN ||
15965 	    port_state & SATA_PSTATE_FAILED) {
15966 		ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
15967 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15968 		if (port_state & SATA_PSTATE_FAILED)
15969 			ap_state->ap_condition = AP_COND_FAILED;
15970 		else
15971 			ap_state->ap_condition = AP_COND_UNKNOWN;
15972 
15973 		return;
15974 	}
15975 
15976 	/* cport state is okay. Now check pmport state */
15977 	if (qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) {
15978 		/* Sanity check */
15979 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
15980 		    SATA_DTYPE_PMULT || SATA_PMPORT_INFO(sata_hba_inst,
15981 		    cport, pmport) == NULL)
15982 			return;
15983 		port_state = SATA_PMPORT_STATE(sata_hba_inst, cport, pmport);
15984 		if (port_state & SATA_PSTATE_SHUTDOWN ||
15985 		    port_state & SATA_PSTATE_FAILED) {
15986 			ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
15987 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
15988 			if (port_state & SATA_PSTATE_FAILED)
15989 				ap_state->ap_condition = AP_COND_FAILED;
15990 			else
15991 				ap_state->ap_condition = AP_COND_UNKNOWN;
15992 
15993 			return;
15994 		}
15995 	}
15996 
15997 	/* Port is enabled and ready */
15998 	if (qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_CPORT)
15999 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, cport);
16000 	else
16001 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, pmport);
16002 
16003 	switch (dev_type) {
16004 	case SATA_DTYPE_NONE:
16005 	{
16006 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16007 		ap_state->ap_condition = AP_COND_OK;
16008 		/* No device attached */
16009 		ap_state->ap_rstate = AP_RSTATE_EMPTY;
16010 		break;
16011 	}
16012 	case SATA_DTYPE_PMULT:
16013 	{
16014 		/* Need to check port multiplier state */
16015 		ASSERT(qual == SATA_ADDR_DCPORT);
16016 		pmult_state = SATA_PMULT_INFO(sata_hba_inst, cport)->
16017 		    pmult_state;
16018 		if (pmult_state & (SATA_PSTATE_SHUTDOWN|SATA_PSTATE_FAILED)) {
16019 			ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
16020 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16021 			if (pmult_state & SATA_PSTATE_FAILED)
16022 				ap_state->ap_condition = AP_COND_FAILED;
16023 			else
16024 				ap_state->ap_condition = AP_COND_UNKNOWN;
16025 
16026 			return;
16027 		}
16028 
16029 		/* Port multiplier is not configurable */
16030 		ap_state->ap_ostate = AP_OSTATE_CONFIGURED;
16031 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
16032 		ap_state->ap_condition = AP_COND_OK;
16033 		break;
16034 	}
16035 
16036 	case SATA_DTYPE_ATADISK:
16037 	case SATA_DTYPE_ATAPICD:
16038 	case SATA_DTYPE_ATAPITAPE:
16039 	case SATA_DTYPE_ATAPIDISK:
16040 	{
16041 		dev_info_t *tdip = NULL;
16042 		dev_info_t *dip = NULL;
16043 		int circ;
16044 
16045 		dip = SATA_DIP(sata_hba_inst);
16046 		tdip = sata_get_target_dip(dip, cport, pmport);
16047 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
16048 		if (tdip != NULL) {
16049 			ndi_devi_enter(dip, &circ);
16050 			mutex_enter(&(DEVI(tdip)->devi_lock));
16051 			if (DEVI_IS_DEVICE_REMOVED(tdip)) {
16052 				/*
16053 				 * There could be the case where previously
16054 				 * configured and opened device was removed
16055 				 * and unknown device was plugged.
16056 				 * In such case we want to show a device, and
16057 				 * its configured or unconfigured state but
16058 				 * indicate unusable condition untill the
16059 				 * old target node is released and removed.
16060 				 */
16061 				ap_state->ap_condition = AP_COND_UNUSABLE;
16062 			} else {
16063 				mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst,
16064 				    cport));
16065 				sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16066 				    cport);
16067 				if (sdinfo != NULL) {
16068 					if ((sdinfo->satadrv_state &
16069 					    SATA_DSTATE_FAILED) != 0)
16070 						ap_state->ap_condition =
16071 						    AP_COND_FAILED;
16072 					else
16073 						ap_state->ap_condition =
16074 						    AP_COND_OK;
16075 				} else {
16076 					ap_state->ap_condition =
16077 					    AP_COND_UNKNOWN;
16078 				}
16079 				mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst,
16080 				    cport));
16081 			}
16082 			if ((DEVI_IS_DEVICE_OFFLINE(tdip)) ||
16083 			    (DEVI_IS_DEVICE_DOWN(tdip))) {
16084 				ap_state->ap_ostate =
16085 				    AP_OSTATE_UNCONFIGURED;
16086 			} else {
16087 				ap_state->ap_ostate =
16088 				    AP_OSTATE_CONFIGURED;
16089 			}
16090 			mutex_exit(&(DEVI(tdip)->devi_lock));
16091 			ndi_devi_exit(dip, circ);
16092 		} else {
16093 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16094 			ap_state->ap_condition = AP_COND_UNKNOWN;
16095 		}
16096 		break;
16097 	}
16098 	case SATA_DTYPE_ATAPIPROC:
16099 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
16100 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16101 		ap_state->ap_condition = AP_COND_OK;
16102 		break;
16103 	default:
16104 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
16105 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16106 		ap_state->ap_condition = AP_COND_UNKNOWN;
16107 		/*
16108 		 * This is actually internal error condition (non fatal),
16109 		 * because we have already checked all defined device types.
16110 		 */
16111 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16112 		    "sata_cfgadm_state: Internal error: "
16113 		    "unknown device type"));
16114 		break;
16115 	}
16116 }
16117 
16118 
16119 /*
16120  * Process ioctl get device path request.
16121  *
16122  * NOTE: Port multiplier has no target dip. Devices connected to port
16123  * multiplier have target node attached to the HBA node. The only difference
16124  * between them and the directly-attached device node is a target address.
16125  */
16126 static int
16127 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst,
16128     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16129 {
16130 	char path[MAXPATHLEN];
16131 	uint32_t size;
16132 	dev_info_t *tdip;
16133 
16134 	(void) strcpy(path, "/devices");
16135 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
16136 	    &sata_device->satadev_addr)) == NULL) {
16137 		/*
16138 		 * No such device. If this is a request for a size, do not
16139 		 * return EINVAL for non-existing target, because cfgadm
16140 		 * will then indicate a meaningless ioctl failure.
16141 		 * If this is a request for a path, indicate invalid
16142 		 * argument.
16143 		 */
16144 		if (ioc->get_size == 0)
16145 			return (EINVAL);
16146 	} else {
16147 		(void) ddi_pathname(tdip, path + strlen(path));
16148 	}
16149 	size = strlen(path) + 1;
16150 
16151 	if (ioc->get_size != 0) {
16152 		if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz,
16153 		    mode) != 0)
16154 			return (EFAULT);
16155 	} else {
16156 		if (ioc->bufsiz != size)
16157 			return (EINVAL);
16158 
16159 		else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz,
16160 		    mode) != 0)
16161 			return (EFAULT);
16162 	}
16163 	return (0);
16164 }
16165 
16166 /*
16167  * Process ioctl get attachment point type request.
16168  *
16169  * NOTE: Port multiplier is supported.
16170  */
16171 static	int
16172 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst,
16173     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16174 {
16175 	uint32_t	type_len;
16176 	const char	*ap_type;
16177 	int		dev_type;
16178 
16179 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16180 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst,
16181 		    sata_device->satadev_addr.cport);
16182 	else /* pmport */
16183 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst,
16184 		    sata_device->satadev_addr.cport,
16185 		    sata_device->satadev_addr.pmport);
16186 
16187 	switch (dev_type) {
16188 	case SATA_DTYPE_NONE:
16189 		ap_type = "port";
16190 		break;
16191 
16192 	case SATA_DTYPE_ATADISK:
16193 	case SATA_DTYPE_ATAPIDISK:
16194 		ap_type = "disk";
16195 		break;
16196 
16197 	case SATA_DTYPE_ATAPICD:
16198 		ap_type = "cd/dvd";
16199 		break;
16200 
16201 	case SATA_DTYPE_ATAPITAPE:
16202 		ap_type = "tape";
16203 		break;
16204 
16205 	case SATA_DTYPE_ATAPIPROC:
16206 		ap_type = "processor";
16207 		break;
16208 
16209 	case SATA_DTYPE_PMULT:
16210 		ap_type = "sata-pmult";
16211 		break;
16212 
16213 	case SATA_DTYPE_UNKNOWN:
16214 		ap_type = "unknown";
16215 		break;
16216 
16217 	default:
16218 		ap_type = "unsupported";
16219 		break;
16220 
16221 	} /* end of dev_type switch */
16222 
16223 	type_len = strlen(ap_type) + 1;
16224 
16225 	if (ioc->get_size) {
16226 		if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz,
16227 		    mode) != 0)
16228 			return (EFAULT);
16229 	} else {
16230 		if (ioc->bufsiz != type_len)
16231 			return (EINVAL);
16232 
16233 		if (ddi_copyout((void *)ap_type, ioc->buf,
16234 		    ioc->bufsiz, mode) != 0)
16235 			return (EFAULT);
16236 	}
16237 	return (0);
16238 
16239 }
16240 
16241 /*
16242  * Process ioctl get device model info request.
16243  * This operation should return to cfgadm the device model
16244  * information string
16245  *
16246  * NOTE: Port multiplier is supported.
16247  */
16248 static	int
16249 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst,
16250     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16251 {
16252 	sata_drive_info_t *sdinfo;
16253 	uint32_t info_len;
16254 	char ap_info[SATA_ID_MODEL_LEN + 1];
16255 
16256 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16257 	    sata_device->satadev_addr.cport)->cport_mutex);
16258 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16259 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16260 		    sata_device->satadev_addr.cport);
16261 	else /* port multiplier */
16262 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16263 		    sata_device->satadev_addr.cport,
16264 		    sata_device->satadev_addr.pmport);
16265 	if (sdinfo == NULL) {
16266 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16267 		    sata_device->satadev_addr.cport)->cport_mutex);
16268 		return (EINVAL);
16269 	}
16270 
16271 #ifdef	_LITTLE_ENDIAN
16272 	swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
16273 #else	/* _LITTLE_ENDIAN */
16274 	bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
16275 #endif	/* _LITTLE_ENDIAN */
16276 
16277 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16278 	    sata_device->satadev_addr.cport)->cport_mutex);
16279 
16280 	ap_info[SATA_ID_MODEL_LEN] = '\0';
16281 
16282 	info_len = strlen(ap_info) + 1;
16283 
16284 	if (ioc->get_size) {
16285 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16286 		    mode) != 0)
16287 			return (EFAULT);
16288 	} else {
16289 		if (ioc->bufsiz < info_len)
16290 			return (EINVAL);
16291 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16292 		    mode) != 0)
16293 			return (EFAULT);
16294 	}
16295 	return (0);
16296 }
16297 
16298 
16299 /*
16300  * Process ioctl get device firmware revision info request.
16301  * This operation should return to cfgadm the device firmware revision
16302  * information string
16303  *
16304  * Port multiplier is supported.
16305  */
16306 static	int
16307 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst,
16308     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16309 {
16310 	sata_drive_info_t *sdinfo;
16311 	uint32_t info_len;
16312 	char ap_info[SATA_ID_FW_LEN + 1];
16313 
16314 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16315 	    sata_device->satadev_addr.cport)->cport_mutex);
16316 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16317 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16318 		    sata_device->satadev_addr.cport);
16319 	else /* port multiplier */
16320 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16321 		    sata_device->satadev_addr.cport,
16322 		    sata_device->satadev_addr.pmport);
16323 	if (sdinfo == NULL) {
16324 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16325 		    sata_device->satadev_addr.cport)->cport_mutex);
16326 		return (EINVAL);
16327 	}
16328 
16329 #ifdef	_LITTLE_ENDIAN
16330 	swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
16331 #else	/* _LITTLE_ENDIAN */
16332 	bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
16333 #endif	/* _LITTLE_ENDIAN */
16334 
16335 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16336 	    sata_device->satadev_addr.cport)->cport_mutex);
16337 
16338 	ap_info[SATA_ID_FW_LEN] = '\0';
16339 
16340 	info_len = strlen(ap_info) + 1;
16341 
16342 	if (ioc->get_size) {
16343 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16344 		    mode) != 0)
16345 			return (EFAULT);
16346 	} else {
16347 		if (ioc->bufsiz < info_len)
16348 			return (EINVAL);
16349 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16350 		    mode) != 0)
16351 			return (EFAULT);
16352 	}
16353 	return (0);
16354 }
16355 
16356 
16357 /*
16358  * Process ioctl get device serial number info request.
16359  * This operation should return to cfgadm the device serial number string.
16360  *
16361  * NOTE: Port multiplier is supported.
16362  */
16363 static	int
16364 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst,
16365     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16366 {
16367 	sata_drive_info_t *sdinfo;
16368 	uint32_t info_len;
16369 	char ap_info[SATA_ID_SERIAL_LEN + 1];
16370 
16371 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16372 	    sata_device->satadev_addr.cport)->cport_mutex);
16373 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16374 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16375 		    sata_device->satadev_addr.cport);
16376 	else /* port multiplier */
16377 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16378 		    sata_device->satadev_addr.cport,
16379 		    sata_device->satadev_addr.pmport);
16380 	if (sdinfo == NULL) {
16381 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16382 		    sata_device->satadev_addr.cport)->cport_mutex);
16383 		return (EINVAL);
16384 	}
16385 
16386 #ifdef	_LITTLE_ENDIAN
16387 	swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
16388 #else	/* _LITTLE_ENDIAN */
16389 	bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
16390 #endif	/* _LITTLE_ENDIAN */
16391 
16392 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16393 	    sata_device->satadev_addr.cport)->cport_mutex);
16394 
16395 	ap_info[SATA_ID_SERIAL_LEN] = '\0';
16396 
16397 	info_len = strlen(ap_info) + 1;
16398 
16399 	if (ioc->get_size) {
16400 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16401 		    mode) != 0)
16402 			return (EFAULT);
16403 	} else {
16404 		if (ioc->bufsiz < info_len)
16405 			return (EINVAL);
16406 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16407 		    mode) != 0)
16408 			return (EFAULT);
16409 	}
16410 	return (0);
16411 }
16412 
16413 
16414 /*
16415  * Preset scsi extended sense data (to NO SENSE)
16416  * First 18 bytes of the sense data are preset to current valid sense
16417  * with a key NO SENSE data.
16418  *
16419  * Returns void
16420  */
16421 static void
16422 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense)
16423 {
16424 	sense->es_valid = 1;		/* Valid sense */
16425 	sense->es_class = CLASS_EXTENDED_SENSE;	/* 0x70 - current err */
16426 	sense->es_key = KEY_NO_SENSE;
16427 	sense->es_info_1 = 0;
16428 	sense->es_info_2 = 0;
16429 	sense->es_info_3 = 0;
16430 	sense->es_info_4 = 0;
16431 	sense->es_add_len = 10;	/* Additional length - replace with a def */
16432 	sense->es_cmd_info[0] = 0;
16433 	sense->es_cmd_info[1] = 0;
16434 	sense->es_cmd_info[2] = 0;
16435 	sense->es_cmd_info[3] = 0;
16436 	sense->es_add_code = 0;
16437 	sense->es_qual_code = 0;
16438 }
16439 
16440 /*
16441  * Register a legacy cmdk-style devid for the target (disk) device.
16442  *
16443  * Note: This function is called only when the HBA devinfo node has the
16444  * property "use-cmdk-devid-format" set. This property indicates that
16445  * devid compatible with old cmdk (target) driver is to be generated
16446  * for any target device attached to this controller. This will take
16447  * precedence over the devid generated by sd (target) driver.
16448  * This function is derived from cmdk_devid_setup() function in cmdk.c.
16449  */
16450 static void
16451 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo)
16452 {
16453 	char	*hwid;
16454 	int	modlen;
16455 	int	serlen;
16456 	int	rval;
16457 	ddi_devid_t	devid;
16458 
16459 	/*
16460 	 * device ID is a concatanation of model number, "=", serial number.
16461 	 */
16462 	hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP);
16463 	bcopy(&sdinfo->satadrv_id.ai_model, hwid,
16464 	    sizeof (sdinfo->satadrv_id.ai_model));
16465 	swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model));
16466 	modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model));
16467 	if (modlen == 0)
16468 		goto err;
16469 	hwid[modlen++] = '=';
16470 	bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen],
16471 	    sizeof (sdinfo->satadrv_id.ai_drvser));
16472 	swab(&hwid[modlen], &hwid[modlen],
16473 	    sizeof (sdinfo->satadrv_id.ai_drvser));
16474 	serlen = sata_check_modser(&hwid[modlen],
16475 	    sizeof (sdinfo->satadrv_id.ai_drvser));
16476 	if (serlen == 0)
16477 		goto err;
16478 	hwid[modlen + serlen] = 0; /* terminate the hwid string */
16479 
16480 	/* initialize/register devid */
16481 	if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL,
16482 	    (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS) {
16483 		rval = ddi_devid_register(dip, devid);
16484 		/*
16485 		 * Free up the allocated devid buffer.
16486 		 * NOTE: This doesn't mean unregistering devid.
16487 		 */
16488 		ddi_devid_free(devid);
16489 	}
16490 
16491 	if (rval != DDI_SUCCESS)
16492 		cmn_err(CE_WARN, "sata: failed to create devid for the disk"
16493 		    " on port %d", sdinfo->satadrv_addr.cport);
16494 err:
16495 	kmem_free(hwid, LEGACY_HWID_LEN);
16496 }
16497 
16498 /*
16499  * valid model/serial string must contain a non-zero non-space characters.
16500  * trim trailing spaces/NULLs.
16501  */
16502 static int
16503 sata_check_modser(char *buf, int buf_len)
16504 {
16505 	boolean_t ret;
16506 	char *s;
16507 	int i;
16508 	int tb;
16509 	char ch;
16510 
16511 	ret = B_FALSE;
16512 	s = buf;
16513 	for (i = 0; i < buf_len; i++) {
16514 		ch = *s++;
16515 		if (ch != ' ' && ch != '\0')
16516 			tb = i + 1;
16517 		if (ch != ' ' && ch != '\0' && ch != '0')
16518 			ret = B_TRUE;
16519 	}
16520 
16521 	if (ret == B_FALSE)
16522 		return (0); /* invalid string */
16523 
16524 	return (tb); /* return length */
16525 }
16526 
16527 /*
16528  * sata_set_drive_features function compares current device features setting
16529  * with the saved device features settings and, if there is a difference,
16530  * it restores device features setting to the previously saved state.
16531  * It also arbitrarily tries to select the highest supported DMA mode.
16532  * Device Identify or Identify Packet Device data has to be current.
16533  * At the moment read ahead and write cache are considered for all devices.
16534  * For atapi devices, Removable Media Status Notification is set in addition
16535  * to common features.
16536  *
16537  * This function cannot be called in the interrupt context (it may sleep).
16538  *
16539  * The input argument sdinfo should point to the drive info structure
16540  * to be updated after features are set. Note, that only
16541  * device (packet) identify data is updated, not the flags indicating the
16542  * supported features.
16543  *
16544  * Returns SATA_SUCCESS if successful or there was nothing to do.
16545  * Device Identify data in the drive info structure pointed to by the sdinfo
16546  * arguments is updated even when no features were set or changed.
16547  *
16548  * Returns SATA_FAILURE if device features could not be set or DMA mode
16549  * for a disk cannot be set and device identify data cannot be fetched.
16550  *
16551  * Returns SATA_RETRY if device features could not be set (other than disk
16552  * DMA mode) but the device identify data was fetched successfully.
16553  *
16554  * Note: This function may fail the port, making it inaccessible.
16555  * In such case the explicit port disconnect/connect or physical device
16556  * detach/attach is required to re-evaluate port state again.
16557  */
16558 
16559 static int
16560 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst,
16561     sata_drive_info_t *sdinfo, int restore)
16562 {
16563 	int rval = SATA_SUCCESS;
16564 	int rval_set;
16565 	sata_drive_info_t new_sdinfo;
16566 	char *finfo = "sata_set_drive_features: cannot";
16567 	char *finfox;
16568 	int cache_op;
16569 
16570 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
16571 	new_sdinfo.satadrv_addr = sdinfo->satadrv_addr;
16572 	new_sdinfo.satadrv_type = sdinfo->satadrv_type;
16573 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
16574 		/*
16575 		 * Cannot get device identification - caller may retry later
16576 		 */
16577 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16578 		    "%s fetch device identify data\n", finfo);
16579 		return (SATA_FAILURE);
16580 	}
16581 	finfox = (restore != 0) ? " restore device features" :
16582 	    " initialize device features\n";
16583 
16584 	switch (sdinfo->satadrv_type) {
16585 	case SATA_DTYPE_ATADISK:
16586 		/* Arbitrarily set UDMA mode */
16587 		if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
16588 		    SATA_SUCCESS) {
16589 			SATA_LOG_D((sata_hba_inst, CE_WARN,
16590 			    "%s set UDMA mode\n", finfo));
16591 			return (SATA_FAILURE);
16592 		}
16593 		break;
16594 	case SATA_DTYPE_ATAPICD:
16595 	case SATA_DTYPE_ATAPITAPE:
16596 	case SATA_DTYPE_ATAPIDISK:
16597 		/*  Set Removable Media Status Notification, if necessary */
16598 		if (SATA_RM_NOTIFIC_SUPPORTED(new_sdinfo.satadrv_id) &&
16599 		    restore != 0) {
16600 			if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) &&
16601 			    (!SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id)))||
16602 			    ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) &&
16603 			    SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id))) {
16604 				/* Current setting does not match saved one */
16605 				if (sata_set_rmsn(sata_hba_inst, sdinfo,
16606 				    sdinfo->satadrv_settings &
16607 				    SATA_DEV_RMSN) != SATA_SUCCESS)
16608 					rval = SATA_FAILURE;
16609 			}
16610 		}
16611 		/*
16612 		 * We have to set Multiword DMA or UDMA, if it is supported, as
16613 		 * we want to use DMA transfer mode whenever possible.
16614 		 * Some devices require explicit setting of the DMA mode.
16615 		 */
16616 		if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) {
16617 			/* Set highest supported DMA mode */
16618 			if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
16619 			    SATA_SUCCESS) {
16620 				SATA_LOG_D((sata_hba_inst, CE_WARN,
16621 				    "%s set UDMA mode\n", finfo));
16622 				rval = SATA_FAILURE;
16623 			}
16624 		}
16625 		break;
16626 	}
16627 
16628 	if (!SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id) &&
16629 	    !SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
16630 		/*
16631 		 * neither READ AHEAD nor WRITE CACHE is supported
16632 		 * - do nothing
16633 		 */
16634 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16635 		    "settable features not supported\n", NULL);
16636 		goto update_sdinfo;
16637 	}
16638 
16639 	if ((SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id) &&
16640 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) &&
16641 	    (SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id) &&
16642 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
16643 		/*
16644 		 * both READ AHEAD and WRITE CACHE are enabled
16645 		 * - Nothing to do
16646 		 */
16647 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16648 		    "no device features to set\n", NULL);
16649 		goto update_sdinfo;
16650 	}
16651 
16652 	cache_op = 0;
16653 
16654 	if (SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id)) {
16655 		if ((sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
16656 		    !SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
16657 			/* Enable read ahead / read cache */
16658 			cache_op = SATAC_SF_ENABLE_READ_AHEAD;
16659 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16660 			    "enabling read cache\n", NULL);
16661 		} else if (!(sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
16662 		    SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
16663 			/* Disable read ahead  / read cache */
16664 			cache_op = SATAC_SF_DISABLE_READ_AHEAD;
16665 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16666 			    "disabling read cache\n", NULL);
16667 		}
16668 
16669 		if (cache_op != 0) {
16670 			/* Try to set read cache mode */
16671 			rval_set = sata_set_cache_mode(sata_hba_inst,
16672 			    &new_sdinfo, cache_op);
16673 			if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
16674 				rval = rval_set;
16675 		}
16676 	}
16677 
16678 	cache_op = 0;
16679 
16680 	if (SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
16681 		if ((sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
16682 		    !SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
16683 			/* Enable write cache */
16684 			cache_op = SATAC_SF_ENABLE_WRITE_CACHE;
16685 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16686 			    "enabling write cache\n", NULL);
16687 		} else if (!(sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
16688 		    SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
16689 			/* Disable write cache */
16690 			cache_op = SATAC_SF_DISABLE_WRITE_CACHE;
16691 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16692 			    "disabling write cache\n", NULL);
16693 		}
16694 
16695 		if (cache_op != 0) {
16696 			/* Try to set write cache mode */
16697 			rval_set = sata_set_cache_mode(sata_hba_inst,
16698 			    &new_sdinfo, cache_op);
16699 			if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
16700 				rval = rval_set;
16701 		}
16702 	}
16703 	if (rval != SATA_SUCCESS)
16704 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16705 		    "%s %s", finfo, finfox));
16706 
16707 update_sdinfo:
16708 	/*
16709 	 * We need to fetch Device Identify data again
16710 	 */
16711 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
16712 		/*
16713 		 * Cannot get device identification - retry later
16714 		 */
16715 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16716 		    "%s re-fetch device identify data\n", finfo));
16717 		rval = SATA_FAILURE;
16718 	}
16719 	/* Copy device sata info. */
16720 	sdinfo->satadrv_id = new_sdinfo.satadrv_id;
16721 
16722 	return (rval);
16723 }
16724 
16725 
16726 /*
16727  *
16728  * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if
16729  * unable to determine.
16730  *
16731  * Cannot be called in an interrupt context.
16732  *
16733  * Called by sata_build_lsense_page_2f()
16734  */
16735 
16736 static int
16737 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst,
16738     sata_drive_info_t *sdinfo)
16739 {
16740 	sata_pkt_t *spkt;
16741 	sata_cmd_t *scmd;
16742 	sata_pkt_txlate_t *spx;
16743 	int rval;
16744 
16745 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16746 	spx->txlt_sata_hba_inst = sata_hba_inst;
16747 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
16748 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16749 	if (spkt == NULL) {
16750 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16751 		return (-1);
16752 	}
16753 	/* address is needed now */
16754 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16755 
16756 
16757 	/* Fill sata_pkt */
16758 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16759 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16760 	/* Synchronous mode, no callback */
16761 	spkt->satapkt_comp = NULL;
16762 	/* Timeout 30s */
16763 	spkt->satapkt_time = sata_default_pkt_time;
16764 
16765 	scmd = &spkt->satapkt_cmd;
16766 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
16767 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
16768 
16769 	/* Set up which registers need to be returned */
16770 	scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE;
16771 	scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE;
16772 
16773 	/* Build SMART_RETURN_STATUS cmd in the sata_pkt */
16774 	scmd->satacmd_addr_type = 0;		/* N/A */
16775 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
16776 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
16777 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
16778 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
16779 	scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS;
16780 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
16781 	scmd->satacmd_cmd_reg = SATAC_SMART;
16782 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16783 	    sdinfo->satadrv_addr.cport)));
16784 
16785 
16786 	/* Send pkt to SATA HBA driver */
16787 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16788 	    SATA_TRAN_ACCEPTED ||
16789 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16790 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16791 		    sdinfo->satadrv_addr.cport)));
16792 		/*
16793 		 * Whoops, no SMART RETURN STATUS
16794 		 */
16795 		rval = -1;
16796 	} else {
16797 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16798 		    sdinfo->satadrv_addr.cport)));
16799 		if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
16800 			rval = -1;
16801 			goto fail;
16802 		}
16803 		if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
16804 			rval = -1;
16805 			goto fail;
16806 		}
16807 		if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) &&
16808 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2))
16809 			rval = 0;
16810 		else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) &&
16811 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4))
16812 			rval = 1;
16813 		else {
16814 			rval = -1;
16815 			goto fail;
16816 		}
16817 	}
16818 fail:
16819 	/* Free allocated resources */
16820 	sata_pkt_free(spx);
16821 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
16822 
16823 	return (rval);
16824 }
16825 
16826 /*
16827  *
16828  * Returns 0 if succeeded, -1 otherwise
16829  *
16830  * Cannot be called in an interrupt context.
16831  *
16832  */
16833 static int
16834 sata_fetch_smart_data(
16835 	sata_hba_inst_t *sata_hba_inst,
16836 	sata_drive_info_t *sdinfo,
16837 	struct smart_data *smart_data)
16838 {
16839 	sata_pkt_t *spkt;
16840 	sata_cmd_t *scmd;
16841 	sata_pkt_txlate_t *spx;
16842 	int rval;
16843 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
16844 
16845 #if ! defined(lint)
16846 	ASSERT(sizeof (struct smart_data) == 512);
16847 #endif
16848 
16849 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16850 	spx->txlt_sata_hba_inst = sata_hba_inst;
16851 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
16852 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16853 	if (spkt == NULL) {
16854 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16855 		return (-1);
16856 	}
16857 	/* address is needed now */
16858 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16859 
16860 
16861 	/* Fill sata_pkt */
16862 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16863 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16864 	/* Synchronous mode, no callback */
16865 	spkt->satapkt_comp = NULL;
16866 	/* Timeout 30s */
16867 	spkt->satapkt_time = sata_default_pkt_time;
16868 
16869 	scmd = &spkt->satapkt_cmd;
16870 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
16871 
16872 	/*
16873 	 * Allocate buffer for SMART data
16874 	 */
16875 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
16876 	    sizeof (struct smart_data));
16877 	if (scmd->satacmd_bp == NULL) {
16878 		sata_pkt_free(spx);
16879 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16880 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16881 		    "sata_fetch_smart_data: "
16882 		    "cannot allocate buffer"));
16883 		return (-1);
16884 	}
16885 
16886 
16887 	/* Build SMART_READ_DATA cmd in the sata_pkt */
16888 	scmd->satacmd_addr_type = 0;		/* N/A */
16889 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
16890 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
16891 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
16892 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
16893 	scmd->satacmd_features_reg = SATA_SMART_READ_DATA;
16894 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
16895 	scmd->satacmd_cmd_reg = SATAC_SMART;
16896 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16897 	    sdinfo->satadrv_addr.cport)));
16898 
16899 	/* Send pkt to SATA HBA driver */
16900 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16901 	    SATA_TRAN_ACCEPTED ||
16902 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16903 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16904 		    sdinfo->satadrv_addr.cport)));
16905 		/*
16906 		 * Whoops, no SMART DATA available
16907 		 */
16908 		rval = -1;
16909 		goto fail;
16910 	} else {
16911 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16912 		    sdinfo->satadrv_addr.cport)));
16913 		if (spx->txlt_buf_dma_handle != NULL) {
16914 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
16915 			    DDI_DMA_SYNC_FORKERNEL);
16916 			ASSERT(rval == DDI_SUCCESS);
16917 			if (sata_check_for_dma_error(dip, spx)) {
16918 				ddi_fm_service_impact(dip,
16919 				    DDI_SERVICE_UNAFFECTED);
16920 				rval = -1;
16921 				goto fail;
16922 			}
16923 		}
16924 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data,
16925 		    sizeof (struct smart_data));
16926 	}
16927 
16928 fail:
16929 	/* Free allocated resources */
16930 	sata_free_local_buffer(spx);
16931 	sata_pkt_free(spx);
16932 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
16933 
16934 	return (rval);
16935 }
16936 
16937 /*
16938  * Used by LOG SENSE page 0x10
16939  * Reads (in synchronous mode) the self test log data using Read Log Ext cmd.
16940  * Note: cannot be called in the interrupt context.
16941  *
16942  * return 0 for success, -1 otherwise
16943  *
16944  */
16945 static int
16946 sata_ext_smart_selftest_read_log(
16947 	sata_hba_inst_t *sata_hba_inst,
16948 	sata_drive_info_t *sdinfo,
16949 	struct smart_ext_selftest_log *ext_selftest_log,
16950 	uint16_t block_num)
16951 {
16952 	sata_pkt_txlate_t *spx;
16953 	sata_pkt_t *spkt;
16954 	sata_cmd_t *scmd;
16955 	int rval;
16956 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
16957 
16958 #if ! defined(lint)
16959 	ASSERT(sizeof (struct smart_ext_selftest_log) == 512);
16960 #endif
16961 
16962 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16963 	spx->txlt_sata_hba_inst = sata_hba_inst;
16964 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
16965 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16966 	if (spkt == NULL) {
16967 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16968 		return (-1);
16969 	}
16970 	/* address is needed now */
16971 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16972 
16973 
16974 	/* Fill sata_pkt */
16975 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16976 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16977 	/* Synchronous mode, no callback */
16978 	spkt->satapkt_comp = NULL;
16979 	/* Timeout 30s */
16980 	spkt->satapkt_time = sata_default_pkt_time;
16981 
16982 	scmd = &spkt->satapkt_cmd;
16983 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
16984 
16985 	/*
16986 	 * Allocate buffer for SMART extended self-test log
16987 	 */
16988 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
16989 	    sizeof (struct smart_ext_selftest_log));
16990 	if (scmd->satacmd_bp == NULL) {
16991 		sata_pkt_free(spx);
16992 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16993 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16994 		    "sata_ext_smart_selftest_log: "
16995 		    "cannot allocate buffer"));
16996 		return (-1);
16997 	}
16998 
16999 	/* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */
17000 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
17001 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of selftest log */
17002 	scmd->satacmd_sec_count_msb = 0;	/* One sector of selftest log */
17003 	scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE;
17004 	scmd->satacmd_lba_low_msb = 0;
17005 	scmd->satacmd_lba_mid_lsb = block_num & 0xff;
17006 	scmd->satacmd_lba_mid_msb = block_num >> 8;
17007 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
17008 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
17009 
17010 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17011 	    sdinfo->satadrv_addr.cport)));
17012 
17013 	/* Send pkt to SATA HBA driver */
17014 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17015 	    SATA_TRAN_ACCEPTED ||
17016 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17017 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17018 		    sdinfo->satadrv_addr.cport)));
17019 
17020 		/*
17021 		 * Whoops, no SMART selftest log info available
17022 		 */
17023 		rval = -1;
17024 		goto fail;
17025 	} else {
17026 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17027 		    sdinfo->satadrv_addr.cport)));
17028 
17029 		if (spx->txlt_buf_dma_handle != NULL) {
17030 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17031 			    DDI_DMA_SYNC_FORKERNEL);
17032 			ASSERT(rval == DDI_SUCCESS);
17033 			if (sata_check_for_dma_error(dip, spx)) {
17034 				ddi_fm_service_impact(dip,
17035 				    DDI_SERVICE_UNAFFECTED);
17036 				rval = -1;
17037 				goto fail;
17038 			}
17039 		}
17040 		bcopy(scmd->satacmd_bp->b_un.b_addr,
17041 		    (uint8_t *)ext_selftest_log,
17042 		    sizeof (struct smart_ext_selftest_log));
17043 		rval = 0;
17044 	}
17045 
17046 fail:
17047 	/* Free allocated resources */
17048 	sata_free_local_buffer(spx);
17049 	sata_pkt_free(spx);
17050 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17051 
17052 	return (rval);
17053 }
17054 
17055 /*
17056  * Returns 0 for success, -1 otherwise
17057  *
17058  * SMART self-test log data is returned in buffer pointed to by selftest_log
17059  */
17060 static int
17061 sata_smart_selftest_log(
17062 	sata_hba_inst_t *sata_hba_inst,
17063 	sata_drive_info_t *sdinfo,
17064 	struct smart_selftest_log *selftest_log)
17065 {
17066 	sata_pkt_t *spkt;
17067 	sata_cmd_t *scmd;
17068 	sata_pkt_txlate_t *spx;
17069 	int rval;
17070 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
17071 
17072 #if ! defined(lint)
17073 	ASSERT(sizeof (struct smart_selftest_log) == 512);
17074 #endif
17075 
17076 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17077 	spx->txlt_sata_hba_inst = sata_hba_inst;
17078 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
17079 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17080 	if (spkt == NULL) {
17081 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17082 		return (-1);
17083 	}
17084 	/* address is needed now */
17085 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17086 
17087 
17088 	/* Fill sata_pkt */
17089 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17090 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17091 	/* Synchronous mode, no callback */
17092 	spkt->satapkt_comp = NULL;
17093 	/* Timeout 30s */
17094 	spkt->satapkt_time = sata_default_pkt_time;
17095 
17096 	scmd = &spkt->satapkt_cmd;
17097 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17098 
17099 	/*
17100 	 * Allocate buffer for SMART SELFTEST LOG
17101 	 */
17102 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
17103 	    sizeof (struct smart_selftest_log));
17104 	if (scmd->satacmd_bp == NULL) {
17105 		sata_pkt_free(spx);
17106 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17107 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17108 		    "sata_smart_selftest_log: "
17109 		    "cannot allocate buffer"));
17110 		return (-1);
17111 	}
17112 
17113 	/* Build SMART_READ_LOG cmd in the sata_pkt */
17114 	scmd->satacmd_addr_type = 0;		/* N/A */
17115 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of SMART log */
17116 	scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE;
17117 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
17118 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
17119 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
17120 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
17121 	scmd->satacmd_cmd_reg = SATAC_SMART;
17122 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17123 	    sdinfo->satadrv_addr.cport)));
17124 
17125 	/* Send pkt to SATA HBA driver */
17126 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17127 	    SATA_TRAN_ACCEPTED ||
17128 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17129 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17130 		    sdinfo->satadrv_addr.cport)));
17131 		/*
17132 		 * Whoops, no SMART DATA available
17133 		 */
17134 		rval = -1;
17135 		goto fail;
17136 	} else {
17137 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17138 		    sdinfo->satadrv_addr.cport)));
17139 		if (spx->txlt_buf_dma_handle != NULL) {
17140 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17141 			    DDI_DMA_SYNC_FORKERNEL);
17142 			ASSERT(rval == DDI_SUCCESS);
17143 			if (sata_check_for_dma_error(dip, spx)) {
17144 				ddi_fm_service_impact(dip,
17145 				    DDI_SERVICE_UNAFFECTED);
17146 				rval = -1;
17147 				goto fail;
17148 			}
17149 		}
17150 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log,
17151 		    sizeof (struct smart_selftest_log));
17152 		rval = 0;
17153 	}
17154 
17155 fail:
17156 	/* Free allocated resources */
17157 	sata_free_local_buffer(spx);
17158 	sata_pkt_free(spx);
17159 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17160 
17161 	return (rval);
17162 }
17163 
17164 
17165 /*
17166  * Returns 0 for success, -1 otherwise
17167  *
17168  * SMART READ LOG data is returned in buffer pointed to by smart_log
17169  */
17170 static int
17171 sata_smart_read_log(
17172 	sata_hba_inst_t *sata_hba_inst,
17173 	sata_drive_info_t *sdinfo,
17174 	uint8_t *smart_log,		/* where the data should be returned */
17175 	uint8_t which_log,		/* which log should be returned */
17176 	uint8_t log_size)		/* # of 512 bytes in log */
17177 {
17178 	sata_pkt_t *spkt;
17179 	sata_cmd_t *scmd;
17180 	sata_pkt_txlate_t *spx;
17181 	int rval;
17182 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
17183 
17184 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17185 	spx->txlt_sata_hba_inst = sata_hba_inst;
17186 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
17187 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17188 	if (spkt == NULL) {
17189 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17190 		return (-1);
17191 	}
17192 	/* address is needed now */
17193 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17194 
17195 
17196 	/* Fill sata_pkt */
17197 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17198 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17199 	/* Synchronous mode, no callback */
17200 	spkt->satapkt_comp = NULL;
17201 	/* Timeout 30s */
17202 	spkt->satapkt_time = sata_default_pkt_time;
17203 
17204 	scmd = &spkt->satapkt_cmd;
17205 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17206 
17207 	/*
17208 	 * Allocate buffer for SMART READ LOG
17209 	 */
17210 	scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512);
17211 	if (scmd->satacmd_bp == NULL) {
17212 		sata_pkt_free(spx);
17213 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17214 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17215 		    "sata_smart_read_log: " "cannot allocate buffer"));
17216 		return (-1);
17217 	}
17218 
17219 	/* Build SMART_READ_LOG cmd in the sata_pkt */
17220 	scmd->satacmd_addr_type = 0;		/* N/A */
17221 	scmd->satacmd_sec_count_lsb = log_size;	/* what the caller asked for */
17222 	scmd->satacmd_lba_low_lsb = which_log;	/* which log page */
17223 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
17224 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
17225 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
17226 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
17227 	scmd->satacmd_cmd_reg = SATAC_SMART;
17228 
17229 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17230 	    sdinfo->satadrv_addr.cport)));
17231 
17232 	/* Send pkt to SATA HBA driver */
17233 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17234 	    SATA_TRAN_ACCEPTED ||
17235 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17236 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17237 		    sdinfo->satadrv_addr.cport)));
17238 
17239 		/*
17240 		 * Whoops, no SMART DATA available
17241 		 */
17242 		rval = -1;
17243 		goto fail;
17244 	} else {
17245 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17246 		    sdinfo->satadrv_addr.cport)));
17247 
17248 		if (spx->txlt_buf_dma_handle != NULL) {
17249 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17250 			    DDI_DMA_SYNC_FORKERNEL);
17251 			ASSERT(rval == DDI_SUCCESS);
17252 			if (sata_check_for_dma_error(dip, spx)) {
17253 				ddi_fm_service_impact(dip,
17254 				    DDI_SERVICE_UNAFFECTED);
17255 				rval = -1;
17256 				goto fail;
17257 			}
17258 		}
17259 		bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512);
17260 		rval = 0;
17261 	}
17262 
17263 fail:
17264 	/* Free allocated resources */
17265 	sata_free_local_buffer(spx);
17266 	sata_pkt_free(spx);
17267 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17268 
17269 	return (rval);
17270 }
17271 
17272 /*
17273  * Used by LOG SENSE page 0x10
17274  *
17275  * return 0 for success, -1 otherwise
17276  *
17277  */
17278 static int
17279 sata_read_log_ext_directory(
17280 	sata_hba_inst_t *sata_hba_inst,
17281 	sata_drive_info_t *sdinfo,
17282 	struct read_log_ext_directory *logdir)
17283 {
17284 	sata_pkt_txlate_t *spx;
17285 	sata_pkt_t *spkt;
17286 	sata_cmd_t *scmd;
17287 	int rval;
17288 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
17289 
17290 #if ! defined(lint)
17291 	ASSERT(sizeof (struct read_log_ext_directory) == 512);
17292 #endif
17293 
17294 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17295 	spx->txlt_sata_hba_inst = sata_hba_inst;
17296 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
17297 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17298 	if (spkt == NULL) {
17299 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17300 		return (-1);
17301 	}
17302 
17303 	/* Fill sata_pkt */
17304 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17305 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17306 	/* Synchronous mode, no callback */
17307 	spkt->satapkt_comp = NULL;
17308 	/* Timeout 30s */
17309 	spkt->satapkt_time = sata_default_pkt_time;
17310 
17311 	scmd = &spkt->satapkt_cmd;
17312 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17313 
17314 	/*
17315 	 * Allocate buffer for SMART READ LOG EXTENDED command
17316 	 */
17317 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
17318 	    sizeof (struct read_log_ext_directory));
17319 	if (scmd->satacmd_bp == NULL) {
17320 		sata_pkt_free(spx);
17321 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17322 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17323 		    "sata_read_log_ext_directory: "
17324 		    "cannot allocate buffer"));
17325 		return (-1);
17326 	}
17327 
17328 	/* Build READ LOG EXT w/ log directory cmd in the  sata_pkt */
17329 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
17330 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of directory */
17331 	scmd->satacmd_sec_count_msb = 0;	/* One sector of directory */
17332 	scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY;
17333 	scmd->satacmd_lba_low_msb = 0;
17334 	scmd->satacmd_lba_mid_lsb = 0;
17335 	scmd->satacmd_lba_mid_msb = 0;
17336 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
17337 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
17338 
17339 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17340 	    sdinfo->satadrv_addr.cport)));
17341 
17342 	/* Send pkt to SATA HBA driver */
17343 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17344 	    SATA_TRAN_ACCEPTED ||
17345 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17346 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17347 		    sdinfo->satadrv_addr.cport)));
17348 		/*
17349 		 * Whoops, no SMART selftest log info available
17350 		 */
17351 		rval = -1;
17352 		goto fail;
17353 	} else {
17354 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17355 		    sdinfo->satadrv_addr.cport)));
17356 		if (spx->txlt_buf_dma_handle != NULL) {
17357 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17358 			    DDI_DMA_SYNC_FORKERNEL);
17359 			ASSERT(rval == DDI_SUCCESS);
17360 			if (sata_check_for_dma_error(dip, spx)) {
17361 				ddi_fm_service_impact(dip,
17362 				    DDI_SERVICE_UNAFFECTED);
17363 				rval = -1;
17364 				goto fail;
17365 			}
17366 		}
17367 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir,
17368 		    sizeof (struct read_log_ext_directory));
17369 		rval = 0;
17370 	}
17371 
17372 fail:
17373 	/* Free allocated resources */
17374 	sata_free_local_buffer(spx);
17375 	sata_pkt_free(spx);
17376 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17377 
17378 	return (rval);
17379 }
17380 
17381 /*
17382  * Set up error retrieval sata command for NCQ command error data
17383  * recovery.
17384  *
17385  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
17386  * returns SATA_FAILURE otherwise.
17387  */
17388 static int
17389 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
17390 {
17391 #ifndef __lock_lint
17392 	_NOTE(ARGUNUSED(sdinfo))
17393 #endif
17394 
17395 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
17396 	sata_cmd_t *scmd;
17397 	struct buf *bp;
17398 
17399 	/* Operation modes are up to the caller */
17400 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17401 
17402 	/* Synchronous mode, no callback - may be changed by the caller */
17403 	spkt->satapkt_comp = NULL;
17404 	spkt->satapkt_time = sata_default_pkt_time;
17405 
17406 	scmd = &spkt->satapkt_cmd;
17407 	bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t));
17408 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
17409 
17410 	/*
17411 	 * Allocate dma_able buffer error data.
17412 	 * Buffer allocation will take care of buffer alignment and other DMA
17413 	 * attributes.
17414 	 */
17415 	bp = sata_alloc_local_buffer(spx,
17416 	    sizeof (struct sata_ncq_error_recovery_page));
17417 	if (bp == NULL)
17418 		return (SATA_FAILURE);
17419 
17420 	bp_mapin(bp); /* make data buffer accessible */
17421 	scmd->satacmd_bp = bp;
17422 
17423 	/*
17424 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
17425 	 * before accessing it. Handle is in usual place in translate struct.
17426 	 */
17427 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
17428 
17429 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
17430 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
17431 
17432 	return (SATA_SUCCESS);
17433 }
17434 
17435 /*
17436  * sata_xlate_errors() is used to translate (S)ATA error
17437  * information to SCSI information returned in the SCSI
17438  * packet.
17439  */
17440 static void
17441 sata_xlate_errors(sata_pkt_txlate_t *spx)
17442 {
17443 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
17444 	struct scsi_extended_sense *sense;
17445 
17446 	scsipkt->pkt_reason = CMD_INCOMPLETE;
17447 	*scsipkt->pkt_scbp = STATUS_CHECK;
17448 	sense = sata_arq_sense(spx);
17449 
17450 	switch (spx->txlt_sata_pkt->satapkt_reason) {
17451 	case SATA_PKT_PORT_ERROR:
17452 		/*
17453 		 * We have no device data. Assume no data transfered.
17454 		 */
17455 		sense->es_key = KEY_HARDWARE_ERROR;
17456 		break;
17457 
17458 	case SATA_PKT_DEV_ERROR:
17459 		if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
17460 		    SATA_STATUS_ERR) {
17461 			/*
17462 			 * determine dev error reason from error
17463 			 * reg content
17464 			 */
17465 			sata_decode_device_error(spx, sense);
17466 			break;
17467 		}
17468 		/* No extended sense key - no info available */
17469 		break;
17470 
17471 	case SATA_PKT_TIMEOUT:
17472 		scsipkt->pkt_reason = CMD_TIMEOUT;
17473 		scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET;
17474 		/* No extended sense key */
17475 		break;
17476 
17477 	case SATA_PKT_ABORTED:
17478 		scsipkt->pkt_reason = CMD_ABORTED;
17479 		scsipkt->pkt_statistics |= STAT_ABORTED;
17480 		/* No extended sense key */
17481 		break;
17482 
17483 	case SATA_PKT_RESET:
17484 		/*
17485 		 * pkt aborted either by an explicit reset request from
17486 		 * a host, or due to error recovery
17487 		 */
17488 		scsipkt->pkt_reason = CMD_RESET;
17489 		scsipkt->pkt_statistics |= STAT_DEV_RESET;
17490 		break;
17491 
17492 	default:
17493 		scsipkt->pkt_reason = CMD_TRAN_ERR;
17494 		break;
17495 	}
17496 }
17497 
17498 
17499 
17500 
17501 /*
17502  * Log sata message
17503  * dev pathname msg line preceeds the logged message.
17504  */
17505 
17506 static	void
17507 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...)
17508 {
17509 	char pathname[128];
17510 	dev_info_t *dip = NULL;
17511 	va_list ap;
17512 
17513 	mutex_enter(&sata_log_mutex);
17514 
17515 	va_start(ap, fmt);
17516 	(void) vsprintf(sata_log_buf, fmt, ap);
17517 	va_end(ap);
17518 
17519 	if (sata_hba_inst != NULL) {
17520 		dip = SATA_DIP(sata_hba_inst);
17521 		(void) ddi_pathname(dip, pathname);
17522 	} else {
17523 		pathname[0] = 0;
17524 	}
17525 	if (level == CE_CONT) {
17526 		if (sata_debug_flags == 0)
17527 			cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf);
17528 		else
17529 			cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf);
17530 	} else {
17531 		if (level != CE_NOTE) {
17532 			cmn_err(level, "%s:\n %s", pathname, sata_log_buf);
17533 		} else if (sata_msg) {
17534 			cmn_err(level, "%s:\n %s", pathname,
17535 			    sata_log_buf);
17536 		}
17537 	}
17538 
17539 	/* sata trace debug */
17540 	sata_trace_debug(dip, sata_log_buf);
17541 
17542 	mutex_exit(&sata_log_mutex);
17543 }
17544 
17545 
17546 /* ******** Asynchronous HBA events handling & hotplugging support ******** */
17547 
17548 /*
17549  * Start or terminate the thread, depending on flag arg and current state
17550  */
17551 static void
17552 sata_event_thread_control(int startstop)
17553 {
17554 	static int sata_event_thread_terminating = 0;
17555 	static int sata_event_thread_starting = 0;
17556 	int i;
17557 
17558 	mutex_enter(&sata_event_mutex);
17559 
17560 	if (startstop == 0 && (sata_event_thread_starting == 1 ||
17561 	    sata_event_thread_terminating == 1)) {
17562 		mutex_exit(&sata_event_mutex);
17563 		return;
17564 	}
17565 	if (startstop == 1 && sata_event_thread_starting == 1) {
17566 		mutex_exit(&sata_event_mutex);
17567 		return;
17568 	}
17569 	if (startstop == 1 && sata_event_thread_terminating == 1) {
17570 		sata_event_thread_starting = 1;
17571 		/* wait til terminate operation completes */
17572 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
17573 		while (sata_event_thread_terminating == 1) {
17574 			if (i-- <= 0) {
17575 				sata_event_thread_starting = 0;
17576 				mutex_exit(&sata_event_mutex);
17577 #ifdef SATA_DEBUG
17578 				cmn_err(CE_WARN, "sata_event_thread_control: "
17579 				    "timeout waiting for thread to terminate");
17580 #endif
17581 				return;
17582 			}
17583 			mutex_exit(&sata_event_mutex);
17584 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
17585 			mutex_enter(&sata_event_mutex);
17586 		}
17587 	}
17588 	if (startstop == 1) {
17589 		if (sata_event_thread == NULL) {
17590 			sata_event_thread = thread_create(NULL, 0,
17591 			    (void (*)())sata_event_daemon,
17592 			    &sata_hba_list, 0, &p0, TS_RUN, minclsyspri);
17593 		}
17594 		sata_event_thread_starting = 0;
17595 		mutex_exit(&sata_event_mutex);
17596 		return;
17597 	}
17598 
17599 	/*
17600 	 * If we got here, thread may need to be terminated
17601 	 */
17602 	if (sata_event_thread != NULL) {
17603 		int i;
17604 		/* Signal event thread to go away */
17605 		sata_event_thread_terminating = 1;
17606 		sata_event_thread_terminate = 1;
17607 		cv_signal(&sata_event_cv);
17608 		/*
17609 		 * Wait til daemon terminates.
17610 		 */
17611 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
17612 		while (sata_event_thread_terminate == 1) {
17613 			mutex_exit(&sata_event_mutex);
17614 			if (i-- <= 0) {
17615 				/* Daemon did not go away !!! */
17616 #ifdef SATA_DEBUG
17617 				cmn_err(CE_WARN, "sata_event_thread_control: "
17618 				    "cannot terminate event daemon thread");
17619 #endif
17620 				mutex_enter(&sata_event_mutex);
17621 				break;
17622 			}
17623 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
17624 			mutex_enter(&sata_event_mutex);
17625 		}
17626 		sata_event_thread_terminating = 0;
17627 	}
17628 	ASSERT(sata_event_thread_terminating == 0);
17629 	ASSERT(sata_event_thread_starting == 0);
17630 	mutex_exit(&sata_event_mutex);
17631 }
17632 
17633 
17634 /*
17635  * SATA HBA event notification function.
17636  * Events reported by SATA HBA drivers per HBA instance relate to a change in
17637  * a port and/or device state or a controller itself.
17638  * Events for different addresses/addr types cannot be combined.
17639  * A warning message is generated for each event type.
17640  * Events are not processed by this function, so only the
17641  * event flag(s)is set for an affected entity and the event thread is
17642  * waken up. Event daemon thread processes all events.
17643  *
17644  * NOTE: Since more than one event may be reported at the same time, one
17645  * cannot determine a sequence of events when opposite event are reported, eg.
17646  * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing
17647  * is taking precedence over reported events, i.e. may cause ignoring some
17648  * events.
17649  */
17650 #define	SATA_EVENT_MAX_MSG_LENGTH	79
17651 
17652 void
17653 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event)
17654 {
17655 	sata_hba_inst_t *sata_hba_inst = NULL;
17656 	sata_address_t *saddr;
17657 	sata_pmult_info_t *pmultinfo;
17658 	sata_drive_info_t *sdinfo;
17659 	sata_port_stats_t *pstats;
17660 	sata_cport_info_t *cportinfo;
17661 	sata_pmport_info_t *pmportinfo;
17662 	int cport, pmport;
17663 	char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1];
17664 	char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1];
17665 	char *lcp;
17666 	static char *err_msg_evnt_1 =
17667 	    "sata_hba_event_notify: invalid port event 0x%x ";
17668 	static char *err_msg_evnt_2 =
17669 	    "sata_hba_event_notify: invalid device event 0x%x ";
17670 	int linkevent;
17671 
17672 	/*
17673 	 * There is a possibility that an event will be generated on HBA
17674 	 * that has not completed attachment or is detaching. We still want
17675 	 * to process events until HBA is detached.
17676 	 */
17677 	mutex_enter(&sata_mutex);
17678 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
17679 	    sata_hba_inst = sata_hba_inst->satahba_next) {
17680 		if (SATA_DIP(sata_hba_inst) == dip)
17681 			if (sata_hba_inst->satahba_attached == 1)
17682 				break;
17683 	}
17684 	mutex_exit(&sata_mutex);
17685 	if (sata_hba_inst == NULL)
17686 		/* HBA not attached */
17687 		return;
17688 
17689 	ASSERT(sata_device != NULL);
17690 
17691 	/*
17692 	 * Validate address before - do not proceed with invalid address.
17693 	 */
17694 	saddr = &sata_device->satadev_addr;
17695 	if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst))
17696 		return;
17697 
17698 	cport = saddr->cport;
17699 	pmport = saddr->pmport;
17700 
17701 	buf1[0] = buf2[0] = '\0';
17702 
17703 	/*
17704 	 * If event relates to port or device, check port state.
17705 	 * Port has to be initialized, or we cannot accept an event.
17706 	 */
17707 	if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT |
17708 	    SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT | SATA_ADDR_PMULT)) != 0) {
17709 		mutex_enter(&sata_hba_inst->satahba_mutex);
17710 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
17711 		mutex_exit(&sata_hba_inst->satahba_mutex);
17712 		if (cportinfo == NULL || cportinfo->cport_state == 0)
17713 			return;
17714 	}
17715 
17716 	if ((saddr->qual & (SATA_ADDR_PMULT | SATA_ADDR_PMPORT |
17717 	    SATA_ADDR_DPMPORT)) != 0) {
17718 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
17719 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17720 			    "sata_hba_event_notify: Non-pmult device (0x%x)"
17721 			    "is attached to port %d, ignore pmult/pmport "
17722 			    "event 0x%x", cportinfo->cport_dev_type,
17723 			    cport, event));
17724 			return;
17725 		}
17726 
17727 		mutex_enter(&cportinfo->cport_mutex);
17728 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
17729 		mutex_exit(&cportinfo->cport_mutex);
17730 
17731 		/*
17732 		 * The daemon might be processing attachment of port
17733 		 * multiplier, in that case we should ignore events on its
17734 		 * sub-devices.
17735 		 *
17736 		 * NOTE: Only pmult_state is checked in sata_hba_event_notify.
17737 		 * The pmport_state is checked by sata daemon.
17738 		 */
17739 		if (pmultinfo == NULL ||
17740 		    pmultinfo->pmult_state == SATA_STATE_UNKNOWN) {
17741 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17742 			    "sata_hba_event_notify: pmult is not"
17743 			    "available at port %d:%d, ignore event 0x%x",
17744 			    cport, pmport, event));
17745 			return;
17746 		}
17747 	}
17748 
17749 	if ((saddr->qual &
17750 	    (SATA_ADDR_PMPORT | SATA_ADDR_DPMPORT)) != 0) {
17751 
17752 		mutex_enter(&cportinfo->cport_mutex);
17753 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) {
17754 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17755 			    "sata_hba_event_notify: invalid/"
17756 			    "un-implemented port %d:%d (%d ports), "
17757 			    "ignore event 0x%x", cport, pmport,
17758 			    SATA_NUM_PMPORTS(sata_hba_inst, cport), event));
17759 			mutex_exit(&cportinfo->cport_mutex);
17760 			return;
17761 		}
17762 		mutex_exit(&cportinfo->cport_mutex);
17763 
17764 		mutex_enter(&sata_hba_inst->satahba_mutex);
17765 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
17766 		    cport, pmport);
17767 		mutex_exit(&sata_hba_inst->satahba_mutex);
17768 
17769 		/* pmport is implemented/valid? */
17770 		if (pmportinfo == NULL) {
17771 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17772 			    "sata_hba_event_notify: invalid/"
17773 			    "un-implemented port %d:%d, ignore "
17774 			    "event 0x%x", cport, pmport, event));
17775 			return;
17776 		}
17777 	}
17778 
17779 	/*
17780 	 * Events refer to devices, ports and controllers - each has
17781 	 * unique address. Events for different addresses cannot be combined.
17782 	 */
17783 	if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) {
17784 
17785 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17786 
17787 		/* qualify this event(s) */
17788 		if ((event & SATA_EVNT_PORT_EVENTS) == 0) {
17789 			/* Invalid event for the device port */
17790 			(void) sprintf(buf2, err_msg_evnt_1,
17791 			    event & SATA_EVNT_PORT_EVENTS);
17792 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17793 			goto event_info;
17794 		}
17795 		if (saddr->qual == SATA_ADDR_CPORT) {
17796 			/* Controller's device port event */
17797 
17798 			(SATA_CPORT_INFO(sata_hba_inst, cport))->
17799 			    cport_event_flags |=
17800 			    event & SATA_EVNT_PORT_EVENTS;
17801 			pstats =
17802 			    &(SATA_CPORT_INFO(sata_hba_inst, cport))->
17803 			    cport_stats;
17804 		} else {
17805 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17806 			mutex_enter(&pmportinfo->pmport_mutex);
17807 			/* Port multiplier's device port event */
17808 			(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
17809 			    pmport_event_flags |=
17810 			    event & SATA_EVNT_PORT_EVENTS;
17811 			pstats =
17812 			    &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
17813 			    pmport_stats;
17814 			mutex_exit(&pmportinfo->pmport_mutex);
17815 			mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17816 		}
17817 
17818 		/*
17819 		 * Add to statistics and log the message. We have to do it
17820 		 * here rather than in the event daemon, because there may be
17821 		 * multiple events occuring before they are processed.
17822 		 */
17823 		linkevent = event &
17824 		    (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED);
17825 		if (linkevent) {
17826 			if (linkevent == (SATA_EVNT_LINK_LOST |
17827 			    SATA_EVNT_LINK_ESTABLISHED)) {
17828 				/* This is likely event combination */
17829 				(void) strlcat(buf1, "link lost/established, ",
17830 				    SATA_EVENT_MAX_MSG_LENGTH);
17831 
17832 				if (pstats->link_lost < 0xffffffffffffffffULL)
17833 					pstats->link_lost++;
17834 				if (pstats->link_established <
17835 				    0xffffffffffffffffULL)
17836 					pstats->link_established++;
17837 				linkevent = 0;
17838 			} else if (linkevent & SATA_EVNT_LINK_LOST) {
17839 				(void) strlcat(buf1, "link lost, ",
17840 				    SATA_EVENT_MAX_MSG_LENGTH);
17841 
17842 				if (pstats->link_lost < 0xffffffffffffffffULL)
17843 					pstats->link_lost++;
17844 			} else {
17845 				(void) strlcat(buf1, "link established, ",
17846 				    SATA_EVENT_MAX_MSG_LENGTH);
17847 				if (pstats->link_established <
17848 				    0xffffffffffffffffULL)
17849 					pstats->link_established++;
17850 			}
17851 		}
17852 		if (event & SATA_EVNT_DEVICE_ATTACHED) {
17853 			(void) strlcat(buf1, "device attached, ",
17854 			    SATA_EVENT_MAX_MSG_LENGTH);
17855 			if (pstats->device_attached < 0xffffffffffffffffULL)
17856 				pstats->device_attached++;
17857 		}
17858 		if (event & SATA_EVNT_DEVICE_DETACHED) {
17859 			(void) strlcat(buf1, "device detached, ",
17860 			    SATA_EVENT_MAX_MSG_LENGTH);
17861 			if (pstats->device_detached < 0xffffffffffffffffULL)
17862 				pstats->device_detached++;
17863 		}
17864 		if (event & SATA_EVNT_PWR_LEVEL_CHANGED) {
17865 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
17866 			    "port %d power level changed", cport);
17867 			if (pstats->port_pwr_changed < 0xffffffffffffffffULL)
17868 				pstats->port_pwr_changed++;
17869 		}
17870 
17871 		if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) {
17872 			/* There should be no other events for this address */
17873 			(void) sprintf(buf2, err_msg_evnt_1,
17874 			    event & ~SATA_EVNT_PORT_EVENTS);
17875 		}
17876 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17877 
17878 	} else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) {
17879 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17880 
17881 		/* qualify this event */
17882 		if ((event & SATA_EVNT_DEVICE_RESET) == 0) {
17883 			/* Invalid event for a device */
17884 			(void) sprintf(buf2, err_msg_evnt_2,
17885 			    event & SATA_EVNT_DEVICE_RESET);
17886 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17887 			goto event_info;
17888 		}
17889 		/* drive event */
17890 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
17891 		if (sdinfo != NULL) {
17892 			if (event & SATA_EVNT_DEVICE_RESET) {
17893 				(void) strlcat(buf1, "device reset, ",
17894 				    SATA_EVENT_MAX_MSG_LENGTH);
17895 				if (sdinfo->satadrv_stats.drive_reset <
17896 				    0xffffffffffffffffULL)
17897 					sdinfo->satadrv_stats.drive_reset++;
17898 				sdinfo->satadrv_event_flags |=
17899 				    SATA_EVNT_DEVICE_RESET;
17900 			}
17901 		}
17902 		if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) {
17903 			/* Invalid event for a device */
17904 			(void) sprintf(buf2, err_msg_evnt_2,
17905 			    event & ~SATA_EVNT_DRIVE_EVENTS);
17906 		}
17907 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17908 	} else if (saddr->qual == SATA_ADDR_PMULT) {
17909 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17910 
17911 		/* qualify this event */
17912 		if ((event & (SATA_EVNT_DEVICE_RESET |
17913 		    SATA_EVNT_PMULT_LINK_CHANGED)) == 0) {
17914 			/* Invalid event for a port multiplier */
17915 			(void) sprintf(buf2, err_msg_evnt_2,
17916 			    event & SATA_EVNT_DEVICE_RESET);
17917 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17918 			goto event_info;
17919 		}
17920 
17921 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
17922 
17923 		if (event & SATA_EVNT_DEVICE_RESET) {
17924 
17925 			SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
17926 			    "[Reset] port-mult on cport %d", cport);
17927 			pmultinfo->pmult_event_flags |=
17928 			    SATA_EVNT_DEVICE_RESET;
17929 			(void) strlcat(buf1, "pmult reset, ",
17930 			    SATA_EVENT_MAX_MSG_LENGTH);
17931 		}
17932 
17933 		if (event & SATA_EVNT_PMULT_LINK_CHANGED) {
17934 
17935 			SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
17936 			    "pmult link changed on cport %d", cport);
17937 			pmultinfo->pmult_event_flags |=
17938 			    SATA_EVNT_PMULT_LINK_CHANGED;
17939 			(void) strlcat(buf1, "pmult link changed, ",
17940 			    SATA_EVENT_MAX_MSG_LENGTH);
17941 		}
17942 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17943 
17944 	} else {
17945 		if (saddr->qual != SATA_ADDR_NULL) {
17946 			/* Wrong address qualifier */
17947 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17948 			    "sata_hba_event_notify: invalid address 0x%x",
17949 			    *(uint32_t *)saddr));
17950 			return;
17951 		}
17952 		if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 ||
17953 		    (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) {
17954 			/* Invalid event for the controller */
17955 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17956 			    "sata_hba_event_notify: invalid event 0x%x for "
17957 			    "controller",
17958 			    event & SATA_EVNT_CONTROLLER_EVENTS));
17959 			return;
17960 		}
17961 		buf1[0] = '\0';
17962 		/* This may be a frequent and not interesting event */
17963 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
17964 		    "controller power level changed\n", NULL);
17965 
17966 		mutex_enter(&sata_hba_inst->satahba_mutex);
17967 		if (sata_hba_inst->satahba_stats.ctrl_pwr_change <
17968 		    0xffffffffffffffffULL)
17969 			sata_hba_inst->satahba_stats.ctrl_pwr_change++;
17970 
17971 		sata_hba_inst->satahba_event_flags |=
17972 		    SATA_EVNT_PWR_LEVEL_CHANGED;
17973 		mutex_exit(&sata_hba_inst->satahba_mutex);
17974 	}
17975 	/*
17976 	 * If we got here, there is something to do with this HBA
17977 	 * instance.
17978 	 */
17979 	mutex_enter(&sata_hba_inst->satahba_mutex);
17980 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
17981 	mutex_exit(&sata_hba_inst->satahba_mutex);
17982 	mutex_enter(&sata_mutex);
17983 	sata_event_pending |= SATA_EVNT_MAIN;	/* global event indicator */
17984 	mutex_exit(&sata_mutex);
17985 
17986 	/* Tickle event thread */
17987 	mutex_enter(&sata_event_mutex);
17988 	if (sata_event_thread_active == 0)
17989 		cv_signal(&sata_event_cv);
17990 	mutex_exit(&sata_event_mutex);
17991 
17992 event_info:
17993 	if (buf1[0] != '\0') {
17994 		lcp = strrchr(buf1, ',');
17995 		if (lcp != NULL)
17996 			*lcp = '\0';
17997 	}
17998 	if (saddr->qual == SATA_ADDR_CPORT ||
17999 	    saddr->qual == SATA_ADDR_DCPORT) {
18000 		if (buf1[0] != '\0') {
18001 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
18002 			    cport, buf1);
18003 		}
18004 		if (buf2[0] != '\0') {
18005 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
18006 			    cport, buf2);
18007 		}
18008 	} else if (saddr->qual == SATA_ADDR_PMPORT ||
18009 	    saddr->qual == SATA_ADDR_DPMPORT) {
18010 		if (buf1[0] != '\0') {
18011 			sata_log(sata_hba_inst, CE_NOTE,
18012 			    "port %d pmport %d: %s\n", cport, pmport, buf1);
18013 		}
18014 		if (buf2[0] != '\0') {
18015 			sata_log(sata_hba_inst, CE_NOTE,
18016 			    "port %d pmport %d: %s\n", cport, pmport, buf2);
18017 		}
18018 	}
18019 }
18020 
18021 
18022 /*
18023  * Event processing thread.
18024  * Arg is a pointer to the sata_hba_list pointer.
18025  * It is not really needed, because sata_hba_list is global and static
18026  */
18027 static void
18028 sata_event_daemon(void *arg)
18029 {
18030 #ifndef __lock_lint
18031 	_NOTE(ARGUNUSED(arg))
18032 #endif
18033 	sata_hba_inst_t *sata_hba_inst;
18034 	clock_t delta;
18035 
18036 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18037 	    "SATA event daemon started\n", NULL);
18038 loop:
18039 	/*
18040 	 * Process events here. Walk through all registered HBAs
18041 	 */
18042 	mutex_enter(&sata_mutex);
18043 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
18044 	    sata_hba_inst = sata_hba_inst->satahba_next) {
18045 		ASSERT(sata_hba_inst != NULL);
18046 		mutex_enter(&sata_hba_inst->satahba_mutex);
18047 		if (sata_hba_inst->satahba_attached == 0 ||
18048 		    (sata_hba_inst->satahba_event_flags &
18049 		    SATA_EVNT_SKIP) != 0) {
18050 			mutex_exit(&sata_hba_inst->satahba_mutex);
18051 			continue;
18052 		}
18053 		if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) {
18054 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP;
18055 			mutex_exit(&sata_hba_inst->satahba_mutex);
18056 			mutex_exit(&sata_mutex);
18057 			/* Got the controller with pending event */
18058 			sata_process_controller_events(sata_hba_inst);
18059 			/*
18060 			 * Since global mutex was released, there is a
18061 			 * possibility that HBA list has changed, so start
18062 			 * over from the top. Just processed controller
18063 			 * will be passed-over because of the SKIP flag.
18064 			 */
18065 			goto loop;
18066 		}
18067 		mutex_exit(&sata_hba_inst->satahba_mutex);
18068 	}
18069 	/* Clear SKIP flag in all controllers */
18070 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
18071 	    sata_hba_inst = sata_hba_inst->satahba_next) {
18072 		mutex_enter(&sata_hba_inst->satahba_mutex);
18073 		sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP;
18074 		mutex_exit(&sata_hba_inst->satahba_mutex);
18075 	}
18076 	mutex_exit(&sata_mutex);
18077 
18078 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18079 	    "SATA EVENT DAEMON suspending itself", NULL);
18080 
18081 #ifdef SATA_DEBUG
18082 	if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) {
18083 		sata_log(sata_hba_inst, CE_WARN,
18084 		    "SATA EVENTS PROCESSING DISABLED\n");
18085 		thread_exit(); /* Daemon will not run again */
18086 	}
18087 #endif
18088 	mutex_enter(&sata_event_mutex);
18089 	sata_event_thread_active = 0;
18090 	mutex_exit(&sata_event_mutex);
18091 	/*
18092 	 * Go to sleep/suspend itself and wake up either because new event or
18093 	 * wait timeout. Exit if there is a termination request (driver
18094 	 * unload).
18095 	 */
18096 	delta = drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME);
18097 	do {
18098 		mutex_enter(&sata_event_mutex);
18099 		(void) cv_reltimedwait(&sata_event_cv, &sata_event_mutex,
18100 		    delta, TR_CLOCK_TICK);
18101 
18102 		if (sata_event_thread_active != 0) {
18103 			mutex_exit(&sata_event_mutex);
18104 			continue;
18105 		}
18106 
18107 		/* Check if it is time to go away */
18108 		if (sata_event_thread_terminate == 1) {
18109 			/*
18110 			 * It is up to the thread setting above flag to make
18111 			 * sure that this thread is not killed prematurely.
18112 			 */
18113 			sata_event_thread_terminate = 0;
18114 			sata_event_thread = NULL;
18115 			mutex_exit(&sata_event_mutex);
18116 			SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18117 			    "SATA_EVENT_DAEMON_TERMINATING", NULL);
18118 			thread_exit();  { _NOTE(NOT_REACHED) }
18119 		}
18120 		mutex_exit(&sata_event_mutex);
18121 	} while (!(sata_event_pending & SATA_EVNT_MAIN));
18122 
18123 	mutex_enter(&sata_event_mutex);
18124 	sata_event_thread_active = 1;
18125 	mutex_exit(&sata_event_mutex);
18126 
18127 	mutex_enter(&sata_mutex);
18128 	sata_event_pending &= ~SATA_EVNT_MAIN;
18129 	mutex_exit(&sata_mutex);
18130 
18131 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18132 	    "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL);
18133 
18134 	goto loop;
18135 }
18136 
18137 /*
18138  * Specific HBA instance event processing.
18139  *
18140  * NOTE: At the moment, device event processing is limited to hard disks
18141  * only.
18142  * Port multiplier is supported now.
18143  */
18144 static void
18145 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst)
18146 {
18147 	int ncport;
18148 	uint32_t event_flags;
18149 	sata_address_t *saddr;
18150 	sata_cport_info_t *cportinfo;
18151 	sata_pmult_info_t *pmultinfo;
18152 
18153 	SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst,
18154 	    "Processing controller %d event(s)",
18155 	    ddi_get_instance(SATA_DIP(sata_hba_inst)));
18156 
18157 	mutex_enter(&sata_hba_inst->satahba_mutex);
18158 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN;
18159 	event_flags = sata_hba_inst->satahba_event_flags;
18160 	mutex_exit(&sata_hba_inst->satahba_mutex);
18161 	/*
18162 	 * Process controller power change first
18163 	 * HERE
18164 	 */
18165 	if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED)
18166 		sata_process_cntrl_pwr_level_change(sata_hba_inst);
18167 
18168 	/*
18169 	 * Search through ports/devices to identify affected port/device.
18170 	 * We may have to process events for more than one port/device.
18171 	 */
18172 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
18173 		/*
18174 		 * Not all ports may be processed in attach by the time we
18175 		 * get an event. Check if port info is initialized.
18176 		 */
18177 		mutex_enter(&sata_hba_inst->satahba_mutex);
18178 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
18179 		mutex_exit(&sata_hba_inst->satahba_mutex);
18180 		if (cportinfo == NULL || cportinfo->cport_state == 0)
18181 			continue;
18182 
18183 		/* We have initialized controller port info */
18184 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18185 		event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))->
18186 		    cport_event_flags;
18187 		/* Check if port was locked by IOCTL processing */
18188 		if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) {
18189 			/*
18190 			 * We ignore port events because port is busy
18191 			 * with AP control processing. Set again
18192 			 * controller and main event flag, so that
18193 			 * events may be processed by the next daemon
18194 			 * run.
18195 			 */
18196 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18197 			mutex_enter(&sata_hba_inst->satahba_mutex);
18198 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
18199 			mutex_exit(&sata_hba_inst->satahba_mutex);
18200 			mutex_enter(&sata_mutex);
18201 			sata_event_pending |= SATA_EVNT_MAIN;
18202 			mutex_exit(&sata_mutex);
18203 			SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst,
18204 			    "Event processing postponed until "
18205 			    "AP control processing completes",
18206 			    NULL);
18207 			/* Check other ports */
18208 			continue;
18209 		} else {
18210 			/*
18211 			 * Set BSY flag so that AP control would not
18212 			 * interfere with events processing for
18213 			 * this port.
18214 			 */
18215 			(SATA_CPORT_INFO(sata_hba_inst, ncport))->
18216 			    cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY;
18217 		}
18218 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18219 
18220 		saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr;
18221 
18222 		if ((event_flags &
18223 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
18224 			/*
18225 			 * Got port event.
18226 			 * We need some hierarchy of event processing as they
18227 			 * are affecting each other:
18228 			 * 1. port failed
18229 			 * 2. device detached/attached
18230 			 * 3. link events - link events may trigger device
18231 			 *    detached or device attached events in some
18232 			 *    circumstances.
18233 			 * 4. port power level changed
18234 			 */
18235 			if (event_flags & SATA_EVNT_PORT_FAILED) {
18236 				sata_process_port_failed_event(sata_hba_inst,
18237 				    saddr);
18238 			}
18239 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
18240 				sata_process_device_detached(sata_hba_inst,
18241 				    saddr);
18242 			}
18243 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
18244 				sata_process_device_attached(sata_hba_inst,
18245 				    saddr);
18246 			}
18247 			if (event_flags &
18248 			    (SATA_EVNT_LINK_ESTABLISHED |
18249 			    SATA_EVNT_LINK_LOST)) {
18250 				sata_process_port_link_events(sata_hba_inst,
18251 				    saddr);
18252 			}
18253 			if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) {
18254 				sata_process_port_pwr_change(sata_hba_inst,
18255 				    saddr);
18256 			}
18257 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
18258 				sata_process_target_node_cleanup(
18259 				    sata_hba_inst, saddr);
18260 			}
18261 			if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) {
18262 				sata_process_device_autoonline(
18263 				    sata_hba_inst, saddr);
18264 			}
18265 		}
18266 
18267 
18268 		/*
18269 		 * Scan port multiplier and all its sub-ports event flags.
18270 		 * The events are marked by
18271 		 * (1) sata_pmult_info.pmult_event_flags
18272 		 * (2) sata_pmport_info.pmport_event_flags
18273 		 */
18274 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18275 		if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
18276 			/*
18277 			 * There should be another extra check: this
18278 			 * port multiplier still exists?
18279 			 */
18280 			pmultinfo = SATA_PMULT_INFO(sata_hba_inst,
18281 			    ncport);
18282 
18283 			if (pmultinfo != NULL) {
18284 				mutex_exit(&(SATA_CPORT_MUTEX(
18285 				    sata_hba_inst, ncport)));
18286 				sata_process_pmult_events(
18287 				    sata_hba_inst, ncport);
18288 				mutex_enter(&(SATA_CPORT_MUTEX(
18289 				    sata_hba_inst, ncport)));
18290 			} else {
18291 				SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
18292 				    "Port-multiplier is gone. "
18293 				    "Ignore all sub-device events "
18294 				    "at port %d.", ncport);
18295 			}
18296 		}
18297 
18298 		if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) !=
18299 		    SATA_DTYPE_NONE) &&
18300 		    (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) {
18301 			if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)->
18302 			    satadrv_event_flags &
18303 			    (SATA_EVNT_DEVICE_RESET |
18304 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
18305 				/* Have device event */
18306 				sata_process_device_reset(sata_hba_inst,
18307 				    saddr);
18308 			}
18309 		}
18310 		/* Release PORT_BUSY flag */
18311 		(SATA_CPORT_INFO(sata_hba_inst, ncport))->
18312 		    cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
18313 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18314 
18315 	} /* End of loop through the controller SATA ports */
18316 }
18317 
18318 /*
18319  * Specific port multiplier instance event processing. At the moment, device
18320  * event processing is limited to link/attach event only.
18321  *
18322  * NOTE: power management event is not supported yet.
18323  */
18324 static void
18325 sata_process_pmult_events(sata_hba_inst_t *sata_hba_inst, uint8_t cport)
18326 {
18327 	sata_cport_info_t *cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
18328 	sata_pmult_info_t *pmultinfo;
18329 	sata_pmport_info_t *pmportinfo;
18330 	sata_address_t *saddr;
18331 	sata_device_t sata_device;
18332 	uint32_t event_flags;
18333 	int npmport;
18334 	int rval;
18335 
18336 	SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst,
18337 	    "Processing pmult event(s) on cport %d of controller %d",
18338 	    cport, ddi_get_instance(SATA_DIP(sata_hba_inst)));
18339 
18340 	/* First process events on port multiplier */
18341 	mutex_enter(&cportinfo->cport_mutex);
18342 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
18343 	event_flags = pmultinfo->pmult_event_flags;
18344 
18345 	/*
18346 	 * Reset event (of port multiplier) has higher priority because the
18347 	 * port multiplier itself might be failed or removed after reset.
18348 	 */
18349 	if (event_flags & SATA_EVNT_DEVICE_RESET) {
18350 		/*
18351 		 * The status of the sub-links are uncertain,
18352 		 * so mark all sub-ports as RESET
18353 		 */
18354 		for (npmport = 0; npmport < SATA_NUM_PMPORTS(
18355 		    sata_hba_inst, cport); npmport ++) {
18356 			pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
18357 			    cport, npmport);
18358 			if (pmportinfo == NULL) {
18359 				/* That's weird. */
18360 				SATA_LOG_D((sata_hba_inst, CE_WARN,
18361 				    "sata_hba_event_notify: "
18362 				    "invalid/un-implemented "
18363 				    "port %d:%d (%d ports), ",
18364 				    cport, npmport, SATA_NUM_PMPORTS(
18365 				    sata_hba_inst, cport)));
18366 				continue;
18367 			}
18368 
18369 			mutex_enter(&pmportinfo->pmport_mutex);
18370 
18371 			/* Mark all pmport to unknow state. */
18372 			pmportinfo->pmport_state = SATA_STATE_UNKNOWN;
18373 			/* Mark all pmports with link events. */
18374 			pmportinfo->pmport_event_flags =
18375 			    (SATA_EVNT_LINK_ESTABLISHED|SATA_EVNT_LINK_LOST);
18376 			mutex_exit(&pmportinfo->pmport_mutex);
18377 		}
18378 
18379 	} else if (event_flags & SATA_EVNT_PMULT_LINK_CHANGED) {
18380 		/*
18381 		 * We need probe the port multiplier to know what has
18382 		 * happened.
18383 		 */
18384 		bzero(&sata_device, sizeof (sata_device_t));
18385 		sata_device.satadev_rev = SATA_DEVICE_REV;
18386 		sata_device.satadev_addr.cport = cport;
18387 		sata_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
18388 		sata_device.satadev_addr.qual = SATA_ADDR_PMULT;
18389 
18390 		mutex_exit(&cportinfo->cport_mutex);
18391 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18392 		    (SATA_DIP(sata_hba_inst), &sata_device);
18393 		mutex_enter(&cportinfo->cport_mutex);
18394 		if (rval != SATA_SUCCESS) {
18395 			/* Something went wrong? Fail the port */
18396 			cportinfo->cport_state = SATA_PSTATE_FAILED;
18397 			mutex_exit(&cportinfo->cport_mutex);
18398 			SATA_LOG_D((sata_hba_inst, CE_WARN,
18399 			    "SATA port %d probing failed", cport));
18400 
18401 			/* PMult structure must be released.  */
18402 			sata_free_pmult(sata_hba_inst, &sata_device);
18403 			return;
18404 		}
18405 
18406 		sata_update_port_info(sata_hba_inst, &sata_device);
18407 
18408 		/*
18409 		 * Sanity check - Port is active? Is the link active?
18410 		 * The device is still a port multiplier?
18411 		 */
18412 		if ((cportinfo->cport_state &
18413 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
18414 		    ((cportinfo->cport_scr.sstatus &
18415 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) ||
18416 		    (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) {
18417 			mutex_exit(&cportinfo->cport_mutex);
18418 
18419 			/* PMult structure must be released.  */
18420 			sata_free_pmult(sata_hba_inst, &sata_device);
18421 			return;
18422 		}
18423 
18424 		/* Probed succeed, set port ready. */
18425 		cportinfo->cport_state |=
18426 		    SATA_STATE_PROBED | SATA_STATE_READY;
18427 	}
18428 
18429 	/* Release port multiplier event flags. */
18430 	pmultinfo->pmult_event_flags &=
18431 	    ~(SATA_EVNT_DEVICE_RESET|SATA_EVNT_PMULT_LINK_CHANGED);
18432 	mutex_exit(&cportinfo->cport_mutex);
18433 
18434 	/*
18435 	 * Check all sub-links.
18436 	 */
18437 	for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst, cport);
18438 	    npmport ++) {
18439 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport);
18440 		mutex_enter(&pmportinfo->pmport_mutex);
18441 		event_flags = pmportinfo->pmport_event_flags;
18442 		mutex_exit(&pmportinfo->pmport_mutex);
18443 		saddr = &pmportinfo->pmport_addr;
18444 
18445 		if ((event_flags &
18446 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
18447 			/*
18448 			 * Got port multiplier port event.
18449 			 * We need some hierarchy of event processing as they
18450 			 * are affecting each other:
18451 			 * 1. device detached/attached
18452 			 * 2. link events - link events may trigger device
18453 			 *    detached or device attached events in some
18454 			 *    circumstances.
18455 			 */
18456 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
18457 				sata_process_pmdevice_detached(sata_hba_inst,
18458 				    saddr);
18459 			}
18460 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
18461 				sata_process_pmdevice_attached(sata_hba_inst,
18462 				    saddr);
18463 			}
18464 			if (event_flags & SATA_EVNT_LINK_ESTABLISHED ||
18465 			    event_flags & SATA_EVNT_LINK_LOST) {
18466 				sata_process_pmport_link_events(sata_hba_inst,
18467 				    saddr);
18468 			}
18469 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
18470 				sata_process_target_node_cleanup(
18471 				    sata_hba_inst, saddr);
18472 			}
18473 		}
18474 
18475 		/* Checking drive event(s). */
18476 		mutex_enter(&pmportinfo->pmport_mutex);
18477 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
18478 		    pmportinfo->pmport_sata_drive != NULL) {
18479 			event_flags = pmportinfo->pmport_sata_drive->
18480 			    satadrv_event_flags;
18481 			if (event_flags & (SATA_EVNT_DEVICE_RESET |
18482 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
18483 
18484 				/* Have device event */
18485 				sata_process_pmdevice_reset(sata_hba_inst,
18486 				    saddr);
18487 			}
18488 		}
18489 		mutex_exit(&pmportinfo->pmport_mutex);
18490 
18491 		/* Release PORT_BUSY flag */
18492 		mutex_enter(&cportinfo->cport_mutex);
18493 		cportinfo->cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
18494 		mutex_exit(&cportinfo->cport_mutex);
18495 	}
18496 
18497 	SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst,
18498 	    "[DONE] pmult event(s) on cport %d of controller %d",
18499 	    cport, ddi_get_instance(SATA_DIP(sata_hba_inst)));
18500 }
18501 
18502 /*
18503  * Process HBA power level change reported by HBA driver.
18504  * Not implemented at this time - event is ignored.
18505  */
18506 static void
18507 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst)
18508 {
18509 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18510 	    "Processing controller power level change", NULL);
18511 
18512 	/* Ignoring it for now */
18513 	mutex_enter(&sata_hba_inst->satahba_mutex);
18514 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
18515 	mutex_exit(&sata_hba_inst->satahba_mutex);
18516 }
18517 
18518 /*
18519  * Process port power level change reported by HBA driver.
18520  * Not implemented at this time - event is ignored.
18521  */
18522 static void
18523 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst,
18524     sata_address_t *saddr)
18525 {
18526 	sata_cport_info_t *cportinfo;
18527 
18528 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18529 	    "Processing port power level change", NULL);
18530 
18531 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18532 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18533 	/* Reset event flag */
18534 	cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
18535 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18536 }
18537 
18538 /*
18539  * Process port failure reported by HBA driver.
18540  * cports support only - no pmports.
18541  */
18542 static void
18543 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst,
18544     sata_address_t *saddr)
18545 {
18546 	sata_cport_info_t *cportinfo;
18547 
18548 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18549 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18550 	/* Reset event flag first */
18551 	cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED;
18552 	/* If the port is in SHUTDOWN or FAILED state, ignore this event. */
18553 	if ((cportinfo->cport_state &
18554 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) {
18555 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18556 		    cport_mutex);
18557 		return;
18558 	}
18559 	/* Fail the port */
18560 	cportinfo->cport_state = SATA_PSTATE_FAILED;
18561 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18562 	sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport);
18563 }
18564 
18565 /*
18566  * Device Reset Event processing.
18567  * The sequence is managed by 3 stage flags:
18568  * - reset event reported,
18569  * - reset event being processed,
18570  * - request to clear device reset state.
18571  *
18572  * NOTE: This function has to be entered with cport mutex held. It exits with
18573  * mutex held as well, but can release mutex during the processing.
18574  */
18575 static void
18576 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst,
18577     sata_address_t *saddr)
18578 {
18579 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
18580 	sata_drive_info_t *sdinfo;
18581 	sata_cport_info_t *cportinfo;
18582 	sata_device_t sata_device;
18583 	int rval_probe, rval_set;
18584 
18585 	/* We only care about host sata cport for now */
18586 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18587 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18588 	/*
18589 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
18590 	 * state, ignore reset event.
18591 	 */
18592 	if (((cportinfo->cport_state &
18593 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
18594 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
18595 		sdinfo->satadrv_event_flags &=
18596 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
18597 		return;
18598 	}
18599 
18600 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) ==
18601 	    SATA_DTYPE_PMULT)) {
18602 		/*
18603 		 * Should not happened: this is already handled in
18604 		 * sata_hba_event_notify()
18605 		 */
18606 		mutex_exit(&cportinfo->cport_mutex);
18607 		goto done;
18608 	}
18609 
18610 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) &
18611 	    SATA_VALID_DEV_TYPE) == 0) {
18612 		/*
18613 		 * This should not happen - coding error.
18614 		 * But we can recover, so do not panic, just clean up
18615 		 * and if in debug mode, log the message.
18616 		 */
18617 #ifdef SATA_DEBUG
18618 		sata_log(sata_hba_inst, CE_WARN,
18619 		    "sata_process_device_reset: "
18620 		    "Invalid device type with sdinfo!", NULL);
18621 #endif
18622 		sdinfo->satadrv_event_flags = 0;
18623 		return;
18624 	}
18625 
18626 #ifdef SATA_DEBUG
18627 	if ((sdinfo->satadrv_event_flags &
18628 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
18629 		/* Nothing to do */
18630 		/* Something is weird - why we are processing dev reset? */
18631 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18632 		    "No device reset event!!!!", NULL);
18633 
18634 		return;
18635 	}
18636 	if ((sdinfo->satadrv_event_flags &
18637 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
18638 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
18639 		/* Something is weird - new device reset event */
18640 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18641 		    "Overlapping device reset events!", NULL);
18642 	}
18643 #endif
18644 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18645 	    "Processing port %d device reset", saddr->cport);
18646 
18647 	/* Clear event flag */
18648 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
18649 
18650 	/* It seems that we always need to check the port state first */
18651 	sata_device.satadev_rev = SATA_DEVICE_REV;
18652 	sata_device.satadev_addr = *saddr;
18653 	/*
18654 	 * We have to exit mutex, because the HBA probe port function may
18655 	 * block on its own mutex.
18656 	 */
18657 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18658 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18659 	    (SATA_DIP(sata_hba_inst), &sata_device);
18660 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18661 	sata_update_port_info(sata_hba_inst, &sata_device);
18662 	if (rval_probe != SATA_SUCCESS) {
18663 		/* Something went wrong? Fail the port */
18664 		cportinfo->cport_state = SATA_PSTATE_FAILED;
18665 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18666 		if (sdinfo != NULL)
18667 			sdinfo->satadrv_event_flags = 0;
18668 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18669 		    cport_mutex);
18670 		SATA_LOG_D((sata_hba_inst, CE_WARN,
18671 		    "SATA port %d probing failed",
18672 		    saddr->cport));
18673 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
18674 		    saddr->cport)->cport_mutex);
18675 		return;
18676 	}
18677 	if ((sata_device.satadev_scr.sstatus  &
18678 	    SATA_PORT_DEVLINK_UP_MASK) !=
18679 	    SATA_PORT_DEVLINK_UP ||
18680 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
18681 		/*
18682 		 * No device to process, anymore. Some other event processing
18683 		 * would or have already performed port info cleanup.
18684 		 * To be safe (HBA may need it), request clearing device
18685 		 * reset condition.
18686 		 */
18687 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18688 		if (sdinfo != NULL) {
18689 			sdinfo->satadrv_event_flags &=
18690 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18691 			sdinfo->satadrv_event_flags |=
18692 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18693 		}
18694 		return;
18695 	}
18696 
18697 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18698 	if (sdinfo == NULL) {
18699 		return;
18700 	}
18701 	if ((sdinfo->satadrv_event_flags &
18702 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
18703 		/*
18704 		 * Start tracking time for device feature restoration and
18705 		 * identification. Save current time (lbolt value).
18706 		 */
18707 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
18708 	}
18709 	/* Mark device reset processing as active */
18710 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
18711 
18712 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
18713 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18714 
18715 	rval_set = sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1);
18716 
18717 	if (rval_set  != SATA_SUCCESS) {
18718 		/*
18719 		 * Restoring drive setting failed.
18720 		 * Probe the port first, to check if the port state has changed
18721 		 */
18722 		sata_device.satadev_rev = SATA_DEVICE_REV;
18723 		sata_device.satadev_addr = *saddr;
18724 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
18725 		/* probe port */
18726 		rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18727 		    (SATA_DIP(sata_hba_inst), &sata_device);
18728 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18729 		    cport_mutex);
18730 		if (rval_probe == SATA_SUCCESS &&
18731 		    (sata_device.satadev_state &
18732 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
18733 		    (sata_device.satadev_scr.sstatus  &
18734 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
18735 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
18736 			/*
18737 			 * We may retry this a bit later - in-process reset
18738 			 * condition should be already set.
18739 			 * Track retry time for device identification.
18740 			 */
18741 			if ((cportinfo->cport_dev_type &
18742 			    SATA_VALID_DEV_TYPE) != 0 &&
18743 			    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL &&
18744 			    sdinfo->satadrv_reset_time != 0) {
18745 				clock_t cur_time = ddi_get_lbolt();
18746 				/*
18747 				 * If the retry time limit was not
18748 				 * exceeded, retry.
18749 				 */
18750 				if ((cur_time - sdinfo->satadrv_reset_time) <
18751 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
18752 					mutex_enter(
18753 					    &sata_hba_inst->satahba_mutex);
18754 					sata_hba_inst->satahba_event_flags |=
18755 					    SATA_EVNT_MAIN;
18756 					mutex_exit(
18757 					    &sata_hba_inst->satahba_mutex);
18758 					mutex_enter(&sata_mutex);
18759 					sata_event_pending |= SATA_EVNT_MAIN;
18760 					mutex_exit(&sata_mutex);
18761 					return;
18762 				}
18763 				if (rval_set == SATA_RETRY) {
18764 					/*
18765 					 * Setting drive features failed, but
18766 					 * the drive is still accessible,
18767 					 * so emit a warning message before
18768 					 * return.
18769 					 */
18770 					mutex_exit(&SATA_CPORT_INFO(
18771 					    sata_hba_inst,
18772 					    saddr->cport)->cport_mutex);
18773 					goto done;
18774 				}
18775 			}
18776 			/* Fail the drive */
18777 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
18778 
18779 			sata_log(sata_hba_inst, CE_WARN,
18780 			    "SATA device at port %d - device failed",
18781 			    saddr->cport);
18782 
18783 			DTRACE_PROBE(port_failed_f);
18784 		}
18785 		/*
18786 		 * No point of retrying - device failed or some other event
18787 		 * processing or already did or will do port info cleanup.
18788 		 * To be safe (HBA may need it),
18789 		 * request clearing device reset condition.
18790 		 */
18791 		sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
18792 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
18793 		sdinfo->satadrv_reset_time = 0;
18794 		return;
18795 	}
18796 done:
18797 	/*
18798 	 * If setting of drive features failed, but the drive is still
18799 	 * accessible, emit a warning message.
18800 	 */
18801 	if (rval_set == SATA_RETRY) {
18802 		sata_log(sata_hba_inst, CE_WARN,
18803 		    "SATA device at port %d - desired setting could not be "
18804 		    "restored after reset. Device may not operate as expected.",
18805 		    saddr->cport);
18806 	}
18807 	/*
18808 	 * Raise the flag indicating that the next sata command could
18809 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
18810 	 * reset is reported.
18811 	 */
18812 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18813 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
18814 		sdinfo->satadrv_reset_time = 0;
18815 		if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) {
18816 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
18817 			sdinfo->satadrv_event_flags &=
18818 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18819 			sdinfo->satadrv_event_flags |=
18820 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18821 		}
18822 	}
18823 }
18824 
18825 
18826 /*
18827  * Port Multiplier Port Device Reset Event processing.
18828  *
18829  * NOTE: This function has to be entered with pmport mutex held. It exits with
18830  * mutex held as well, but can release mutex during the processing.
18831  */
18832 static void
18833 sata_process_pmdevice_reset(sata_hba_inst_t *sata_hba_inst,
18834     sata_address_t *saddr)
18835 {
18836 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
18837 	sata_drive_info_t *sdinfo = NULL;
18838 	sata_cport_info_t *cportinfo = NULL;
18839 	sata_pmport_info_t *pmportinfo = NULL;
18840 	sata_pmult_info_t *pminfo = NULL;
18841 	sata_device_t sata_device;
18842 	uint8_t cport = saddr->cport;
18843 	uint8_t pmport = saddr->pmport;
18844 	int rval;
18845 
18846 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18847 	    "Processing drive reset at port %d:%d", cport, pmport);
18848 
18849 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
18850 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
18851 	sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport);
18852 
18853 	/*
18854 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
18855 	 * state, ignore reset event.
18856 	 */
18857 	if (((cportinfo->cport_state &
18858 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
18859 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
18860 		sdinfo->satadrv_event_flags &=
18861 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
18862 		return;
18863 	}
18864 
18865 	if ((pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
18866 		/*
18867 		 * This should not happen - coding error.
18868 		 * But we can recover, so do not panic, just clean up
18869 		 * and if in debug mode, log the message.
18870 		 */
18871 #ifdef SATA_DEBUG
18872 		sata_log(sata_hba_inst, CE_WARN,
18873 		    "sata_process_pmdevice_reset: "
18874 		    "Invalid device type with sdinfo!", NULL);
18875 #endif
18876 		sdinfo->satadrv_event_flags = 0;
18877 		return;
18878 	}
18879 
18880 #ifdef SATA_DEBUG
18881 	if ((sdinfo->satadrv_event_flags &
18882 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
18883 		/* Nothing to do */
18884 		/* Something is weird - why we are processing dev reset? */
18885 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18886 		    "No device reset event!!!!", NULL);
18887 
18888 		return;
18889 	}
18890 	if ((sdinfo->satadrv_event_flags &
18891 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
18892 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
18893 		/* Something is weird - new device reset event */
18894 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18895 		    "Overlapping device reset events!", NULL);
18896 	}
18897 #endif
18898 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18899 	    "Processing port %d:%d device reset", cport, pmport);
18900 
18901 	/* Clear event flag */
18902 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
18903 
18904 	/* It seems that we always need to check the port state first */
18905 	sata_device.satadev_rev = SATA_DEVICE_REV;
18906 	sata_device.satadev_addr = *saddr;
18907 	/*
18908 	 * We have to exit mutex, because the HBA probe port function may
18909 	 * block on its own mutex.
18910 	 */
18911 	mutex_exit(&pmportinfo->pmport_mutex);
18912 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18913 	    (SATA_DIP(sata_hba_inst), &sata_device);
18914 	mutex_enter(&pmportinfo->pmport_mutex);
18915 
18916 	sata_update_pmport_info(sata_hba_inst, &sata_device);
18917 	if (rval != SATA_SUCCESS) {
18918 		/* Something went wrong? Fail the port */
18919 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
18920 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18921 		    saddr->pmport);
18922 		if (sdinfo != NULL)
18923 			sdinfo->satadrv_event_flags = 0;
18924 		mutex_exit(&pmportinfo->pmport_mutex);
18925 		SATA_LOG_D((sata_hba_inst, CE_WARN,
18926 		    "SATA port %d:%d probing failed",
18927 		    saddr->cport, saddr->pmport));
18928 		mutex_enter(&pmportinfo->pmport_mutex);
18929 		return;
18930 	}
18931 	if ((sata_device.satadev_scr.sstatus  &
18932 	    SATA_PORT_DEVLINK_UP_MASK) !=
18933 	    SATA_PORT_DEVLINK_UP ||
18934 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
18935 		/*
18936 		 * No device to process, anymore. Some other event processing
18937 		 * would or have already performed port info cleanup.
18938 		 * To be safe (HBA may need it), request clearing device
18939 		 * reset condition.
18940 		 */
18941 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18942 		    saddr->pmport);
18943 		if (sdinfo != NULL) {
18944 			sdinfo->satadrv_event_flags &=
18945 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18946 			/* must clear flags on cport */
18947 			pminfo = SATA_PMULT_INFO(sata_hba_inst,
18948 			    saddr->cport);
18949 			pminfo->pmult_event_flags |=
18950 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18951 		}
18952 		return;
18953 	}
18954 
18955 	sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18956 	    saddr->pmport);
18957 	if (sdinfo == NULL) {
18958 		return;
18959 	}
18960 	if ((sdinfo->satadrv_event_flags &
18961 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
18962 		/*
18963 		 * Start tracking time for device feature restoration and
18964 		 * identification. Save current time (lbolt value).
18965 		 */
18966 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
18967 	}
18968 	/* Mark device reset processing as active */
18969 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
18970 
18971 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
18972 	mutex_exit(&pmportinfo->pmport_mutex);
18973 
18974 	if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) ==
18975 	    SATA_FAILURE) {
18976 		/*
18977 		 * Restoring drive setting failed.
18978 		 * Probe the port first, to check if the port state has changed
18979 		 */
18980 		sata_device.satadev_rev = SATA_DEVICE_REV;
18981 		sata_device.satadev_addr = *saddr;
18982 		sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
18983 
18984 		/* probe port */
18985 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18986 		    (SATA_DIP(sata_hba_inst), &sata_device);
18987 		mutex_enter(&pmportinfo->pmport_mutex);
18988 		if (rval == SATA_SUCCESS &&
18989 		    (sata_device.satadev_state &
18990 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
18991 		    (sata_device.satadev_scr.sstatus  &
18992 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
18993 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
18994 			/*
18995 			 * We may retry this a bit later - in-process reset
18996 			 * condition should be already set.
18997 			 * Track retry time for device identification.
18998 			 */
18999 			if ((pmportinfo->pmport_dev_type &
19000 			    SATA_VALID_DEV_TYPE) != 0 &&
19001 			    SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL &&
19002 			    sdinfo->satadrv_reset_time != 0) {
19003 				clock_t cur_time = ddi_get_lbolt();
19004 				/*
19005 				 * If the retry time limit was not
19006 				 * exceeded, retry.
19007 				 */
19008 				if ((cur_time - sdinfo->satadrv_reset_time) <
19009 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
19010 					mutex_enter(
19011 					    &sata_hba_inst->satahba_mutex);
19012 					sata_hba_inst->satahba_event_flags |=
19013 					    SATA_EVNT_MAIN;
19014 					mutex_exit(
19015 					    &sata_hba_inst->satahba_mutex);
19016 					mutex_enter(&sata_mutex);
19017 					sata_event_pending |= SATA_EVNT_MAIN;
19018 					mutex_exit(&sata_mutex);
19019 					return;
19020 				}
19021 			}
19022 			/* Fail the drive */
19023 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
19024 
19025 			sata_log(sata_hba_inst, CE_WARN,
19026 			    "SATA device at port %d:%d - device failed",
19027 			    saddr->cport, saddr->pmport);
19028 		} else {
19029 			/*
19030 			 * No point of retrying - some other event processing
19031 			 * would or already did port info cleanup.
19032 			 * To be safe (HBA may need it),
19033 			 * request clearing device reset condition.
19034 			 */
19035 			sdinfo->satadrv_event_flags |=
19036 			    SATA_EVNT_CLEAR_DEVICE_RESET;
19037 		}
19038 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
19039 		sdinfo->satadrv_reset_time = 0;
19040 		return;
19041 	}
19042 	/*
19043 	 * Raise the flag indicating that the next sata command could
19044 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
19045 	 * reset is reported.
19046 	 */
19047 	mutex_enter(&pmportinfo->pmport_mutex);
19048 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
19049 		sdinfo->satadrv_reset_time = 0;
19050 		if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) {
19051 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19052 			sdinfo->satadrv_event_flags &=
19053 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
19054 			/* must clear flags on cport */
19055 			pminfo = SATA_PMULT_INFO(sata_hba_inst,
19056 			    saddr->cport);
19057 			pminfo->pmult_event_flags |=
19058 			    SATA_EVNT_CLEAR_DEVICE_RESET;
19059 		}
19060 	}
19061 }
19062 
19063 /*
19064  * Port Link Events processing.
19065  * Every link established event may involve device reset (due to
19066  * COMRESET signal, equivalent of the hard reset) so arbitrarily
19067  * set device reset event for an attached device (if any).
19068  * If the port is in SHUTDOWN or FAILED state, ignore link events.
19069  *
19070  * The link established event processing varies, depending on the state
19071  * of the target node, HBA hotplugging capabilities, state of the port.
19072  * If the link is not active, the link established event is ignored.
19073  * If HBA cannot detect device attachment and there is no target node,
19074  * the link established event triggers device attach event processing.
19075  * Else, link established event triggers device reset event processing.
19076  *
19077  * The link lost event processing varies, depending on a HBA hotplugging
19078  * capability and the state of the port (link active or not active).
19079  * If the link is active, the lost link event is ignored.
19080  * If HBA cannot detect device removal, the lost link event triggers
19081  * device detached event processing after link lost timeout.
19082  * Else, the event is ignored.
19083  *
19084  * NOTE: Port multiplier ports events are handled by
19085  * sata_process_pmport_link_events();
19086  */
19087 static void
19088 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst,
19089     sata_address_t *saddr)
19090 {
19091 	sata_device_t sata_device;
19092 	sata_cport_info_t *cportinfo;
19093 	sata_drive_info_t *sdinfo;
19094 	uint32_t event_flags;
19095 	int rval;
19096 
19097 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19098 	    "Processing port %d link event(s)", saddr->cport);
19099 
19100 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19101 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19102 	event_flags = cportinfo->cport_event_flags;
19103 
19104 	/* Reset event flags first */
19105 	cportinfo->cport_event_flags &=
19106 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
19107 
19108 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
19109 	if ((cportinfo->cport_state &
19110 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19111 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19112 		    cport_mutex);
19113 		return;
19114 	}
19115 
19116 	/*
19117 	 * For the sanity sake get current port state.
19118 	 * Set device address only. Other sata_device fields should be
19119 	 * set by HBA driver.
19120 	 */
19121 	sata_device.satadev_rev = SATA_DEVICE_REV;
19122 	sata_device.satadev_addr = *saddr;
19123 	/*
19124 	 * We have to exit mutex, because the HBA probe port function may
19125 	 * block on its own mutex.
19126 	 */
19127 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19128 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19129 	    (SATA_DIP(sata_hba_inst), &sata_device);
19130 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19131 	sata_update_port_info(sata_hba_inst, &sata_device);
19132 	if (rval != SATA_SUCCESS) {
19133 		/* Something went wrong? Fail the port */
19134 		cportinfo->cport_state = SATA_PSTATE_FAILED;
19135 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19136 		    cport_mutex);
19137 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19138 		    "SATA port %d probing failed",
19139 		    saddr->cport));
19140 		/*
19141 		 * We may want to release device info structure, but
19142 		 * it is not necessary.
19143 		 */
19144 		return;
19145 	} else {
19146 		/* port probed successfully */
19147 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19148 	}
19149 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
19150 
19151 		if ((sata_device.satadev_scr.sstatus &
19152 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
19153 			/* Ignore event */
19154 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19155 			    "Ignoring port %d link established event - "
19156 			    "link down",
19157 			    saddr->cport);
19158 			goto linklost;
19159 		}
19160 
19161 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19162 		    "Processing port %d link established event",
19163 		    saddr->cport);
19164 
19165 		/*
19166 		 * For the sanity sake check if a device is attached - check
19167 		 * return state of a port probing.
19168 		 */
19169 		if (sata_device.satadev_type != SATA_DTYPE_NONE) {
19170 			/*
19171 			 * HBA port probe indicated that there is a device
19172 			 * attached. Check if the framework had device info
19173 			 * structure attached for this device.
19174 			 */
19175 			if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
19176 				ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) !=
19177 				    NULL);
19178 
19179 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19180 				if ((sdinfo->satadrv_type &
19181 				    SATA_VALID_DEV_TYPE) != 0) {
19182 					/*
19183 					 * Dev info structure is present.
19184 					 * If dev_type is set to known type in
19185 					 * the framework's drive info struct
19186 					 * then the device existed before and
19187 					 * the link was probably lost
19188 					 * momentarily - in such case
19189 					 * we may want to check device
19190 					 * identity.
19191 					 * Identity check is not supported now.
19192 					 *
19193 					 * Link established event
19194 					 * triggers device reset event.
19195 					 */
19196 					(SATA_CPORTINFO_DRV_INFO(cportinfo))->
19197 					    satadrv_event_flags |=
19198 					    SATA_EVNT_DEVICE_RESET;
19199 				}
19200 			} else if (cportinfo->cport_dev_type ==
19201 			    SATA_DTYPE_NONE) {
19202 				/*
19203 				 * We got new device attached! If HBA does not
19204 				 * generate device attached events, trigger it
19205 				 * here.
19206 				 */
19207 				if (!(SATA_FEATURES(sata_hba_inst) &
19208 				    SATA_CTLF_HOTPLUG)) {
19209 					cportinfo->cport_event_flags |=
19210 					    SATA_EVNT_DEVICE_ATTACHED;
19211 				}
19212 			}
19213 			/* Reset link lost timeout */
19214 			cportinfo->cport_link_lost_time = 0;
19215 		}
19216 	}
19217 linklost:
19218 	if (event_flags & SATA_EVNT_LINK_LOST) {
19219 		if ((sata_device.satadev_scr.sstatus &
19220 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
19221 			/* Ignore event */
19222 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19223 			    "Ignoring port %d link lost event - link is up",
19224 			    saddr->cport);
19225 			goto done;
19226 		}
19227 #ifdef SATA_DEBUG
19228 		if (cportinfo->cport_link_lost_time == 0) {
19229 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19230 			    "Processing port %d link lost event",
19231 			    saddr->cport);
19232 		}
19233 #endif
19234 		/*
19235 		 * When HBA cannot generate device attached/detached events,
19236 		 * we need to track link lost time and eventually generate
19237 		 * device detach event.
19238 		 */
19239 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
19240 			/* We are tracking link lost time */
19241 			if (cportinfo->cport_link_lost_time == 0) {
19242 				/* save current time (lbolt value) */
19243 				cportinfo->cport_link_lost_time =
19244 				    ddi_get_lbolt();
19245 				/* just keep link lost event */
19246 				cportinfo->cport_event_flags |=
19247 				    SATA_EVNT_LINK_LOST;
19248 			} else {
19249 				clock_t cur_time = ddi_get_lbolt();
19250 				if ((cur_time -
19251 				    cportinfo->cport_link_lost_time) >=
19252 				    drv_usectohz(
19253 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
19254 					/* trigger device detach event */
19255 					cportinfo->cport_event_flags |=
19256 					    SATA_EVNT_DEVICE_DETACHED;
19257 					cportinfo->cport_link_lost_time = 0;
19258 					SATADBG1(SATA_DBG_EVENTS,
19259 					    sata_hba_inst,
19260 					    "Triggering port %d "
19261 					    "device detached event",
19262 					    saddr->cport);
19263 				} else {
19264 					/* keep link lost event */
19265 					cportinfo->cport_event_flags |=
19266 					    SATA_EVNT_LINK_LOST;
19267 				}
19268 			}
19269 		}
19270 		/*
19271 		 * We could change port state to disable/delay access to
19272 		 * the attached device until the link is recovered.
19273 		 */
19274 	}
19275 done:
19276 	event_flags = cportinfo->cport_event_flags;
19277 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19278 	if (event_flags != 0) {
19279 		mutex_enter(&sata_hba_inst->satahba_mutex);
19280 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19281 		mutex_exit(&sata_hba_inst->satahba_mutex);
19282 		mutex_enter(&sata_mutex);
19283 		sata_event_pending |= SATA_EVNT_MAIN;
19284 		mutex_exit(&sata_mutex);
19285 	}
19286 }
19287 
19288 /*
19289  * Port Multiplier Port Link Events processing.
19290  */
19291 static void
19292 sata_process_pmport_link_events(sata_hba_inst_t *sata_hba_inst,
19293     sata_address_t *saddr)
19294 {
19295 	sata_device_t sata_device;
19296 	sata_pmport_info_t *pmportinfo = NULL;
19297 	sata_drive_info_t *sdinfo = NULL;
19298 	uint32_t event_flags;
19299 	uint8_t cport = saddr->cport;
19300 	uint8_t pmport = saddr->pmport;
19301 	int rval;
19302 
19303 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19304 	    "Processing port %d:%d link event(s)",
19305 	    cport, pmport);
19306 
19307 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
19308 	mutex_enter(&pmportinfo->pmport_mutex);
19309 	event_flags = pmportinfo->pmport_event_flags;
19310 
19311 	/* Reset event flags first */
19312 	pmportinfo->pmport_event_flags &=
19313 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
19314 
19315 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
19316 	if ((pmportinfo->pmport_state &
19317 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19318 		mutex_exit(&pmportinfo->pmport_mutex);
19319 		return;
19320 	}
19321 
19322 	/*
19323 	 * For the sanity sake get current port state.
19324 	 * Set device address only. Other sata_device fields should be
19325 	 * set by HBA driver.
19326 	 */
19327 	sata_device.satadev_rev = SATA_DEVICE_REV;
19328 	sata_device.satadev_addr = *saddr;
19329 	/*
19330 	 * We have to exit mutex, because the HBA probe port function may
19331 	 * block on its own mutex.
19332 	 */
19333 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19334 	    saddr->pmport));
19335 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19336 	    (SATA_DIP(sata_hba_inst), &sata_device);
19337 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19338 	    saddr->pmport));
19339 	sata_update_pmport_info(sata_hba_inst, &sata_device);
19340 	if (rval != SATA_SUCCESS) {
19341 		/* Something went wrong? Fail the port */
19342 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
19343 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19344 		    saddr->pmport));
19345 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19346 		    "SATA port %d:%d probing failed",
19347 		    saddr->cport, saddr->pmport));
19348 		/*
19349 		 * We may want to release device info structure, but
19350 		 * it is not necessary.
19351 		 */
19352 		return;
19353 	} else {
19354 		/* port probed successfully */
19355 		pmportinfo->pmport_state |=
19356 		    SATA_STATE_PROBED | SATA_STATE_READY;
19357 	}
19358 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst,
19359 	    saddr->cport, saddr->pmport));
19360 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst,
19361 	    saddr->cport, saddr->pmport));
19362 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
19363 
19364 		if ((sata_device.satadev_scr.sstatus &
19365 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
19366 			/* Ignore event */
19367 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19368 			    "Ignoring port %d:%d link established event - "
19369 			    "link down",
19370 			    saddr->cport, saddr->pmport);
19371 			goto linklost;
19372 		}
19373 
19374 		SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19375 		    "Processing port %d:%d link established event",
19376 		    cport, pmport);
19377 
19378 		/*
19379 		 * For the sanity sake check if a device is attached - check
19380 		 * return state of a port probing.
19381 		 */
19382 		if (sata_device.satadev_type != SATA_DTYPE_NONE &&
19383 		    sata_device.satadev_type != SATA_DTYPE_PMULT) {
19384 			/*
19385 			 * HBA port probe indicated that there is a device
19386 			 * attached. Check if the framework had device info
19387 			 * structure attached for this device.
19388 			 */
19389 			if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
19390 				ASSERT(SATA_PMPORTINFO_DRV_INFO(pmportinfo) !=
19391 				    NULL);
19392 
19393 				sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19394 				if ((sdinfo->satadrv_type &
19395 				    SATA_VALID_DEV_TYPE) != 0) {
19396 					/*
19397 					 * Dev info structure is present.
19398 					 * If dev_type is set to known type in
19399 					 * the framework's drive info struct
19400 					 * then the device existed before and
19401 					 * the link was probably lost
19402 					 * momentarily - in such case
19403 					 * we may want to check device
19404 					 * identity.
19405 					 * Identity check is not supported now.
19406 					 *
19407 					 * Link established event
19408 					 * triggers device reset event.
19409 					 */
19410 					(SATA_PMPORTINFO_DRV_INFO(pmportinfo))->
19411 					    satadrv_event_flags |=
19412 					    SATA_EVNT_DEVICE_RESET;
19413 				}
19414 			} else if (pmportinfo->pmport_dev_type ==
19415 			    SATA_DTYPE_NONE) {
19416 				/*
19417 				 * We got new device attached! If HBA does not
19418 				 * generate device attached events, trigger it
19419 				 * here.
19420 				 */
19421 				if (!(SATA_FEATURES(sata_hba_inst) &
19422 				    SATA_CTLF_HOTPLUG)) {
19423 					pmportinfo->pmport_event_flags |=
19424 					    SATA_EVNT_DEVICE_ATTACHED;
19425 				}
19426 			}
19427 			/* Reset link lost timeout */
19428 			pmportinfo->pmport_link_lost_time = 0;
19429 		}
19430 	}
19431 linklost:
19432 	if (event_flags & SATA_EVNT_LINK_LOST) {
19433 #ifdef SATA_DEBUG
19434 		if (pmportinfo->pmport_link_lost_time == 0) {
19435 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19436 			    "Processing port %d:%d link lost event",
19437 			    saddr->cport, saddr->pmport);
19438 		}
19439 #endif
19440 		if ((sata_device.satadev_scr.sstatus &
19441 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
19442 			/* Ignore event */
19443 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19444 			    "Ignoring port %d:%d link lost event - link is up",
19445 			    saddr->cport, saddr->pmport);
19446 			goto done;
19447 		}
19448 		/*
19449 		 * When HBA cannot generate device attached/detached events,
19450 		 * we need to track link lost time and eventually generate
19451 		 * device detach event.
19452 		 */
19453 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
19454 			/* We are tracking link lost time */
19455 			if (pmportinfo->pmport_link_lost_time == 0) {
19456 				/* save current time (lbolt value) */
19457 				pmportinfo->pmport_link_lost_time =
19458 				    ddi_get_lbolt();
19459 				/* just keep link lost event */
19460 				pmportinfo->pmport_event_flags |=
19461 				    SATA_EVNT_LINK_LOST;
19462 			} else {
19463 				clock_t cur_time = ddi_get_lbolt();
19464 				if ((cur_time -
19465 				    pmportinfo->pmport_link_lost_time) >=
19466 				    drv_usectohz(
19467 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
19468 					/* trigger device detach event */
19469 					pmportinfo->pmport_event_flags |=
19470 					    SATA_EVNT_DEVICE_DETACHED;
19471 					pmportinfo->pmport_link_lost_time = 0;
19472 					SATADBG2(SATA_DBG_EVENTS,
19473 					    sata_hba_inst,
19474 					    "Triggering port %d:%d "
19475 					    "device detached event",
19476 					    saddr->cport, saddr->pmport);
19477 				} else {
19478 					/* keep link lost event */
19479 					pmportinfo->pmport_event_flags |=
19480 					    SATA_EVNT_LINK_LOST;
19481 				}
19482 			}
19483 		}
19484 		/*
19485 		 * We could change port state to disable/delay access to
19486 		 * the attached device until the link is recovered.
19487 		 */
19488 	}
19489 done:
19490 	event_flags = pmportinfo->pmport_event_flags;
19491 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19492 	    saddr->pmport));
19493 	if (event_flags != 0) {
19494 		mutex_enter(&sata_hba_inst->satahba_mutex);
19495 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19496 		mutex_exit(&sata_hba_inst->satahba_mutex);
19497 		mutex_enter(&sata_mutex);
19498 		sata_event_pending |= SATA_EVNT_MAIN;
19499 		mutex_exit(&sata_mutex);
19500 	}
19501 }
19502 
19503 /*
19504  * Device Detached Event processing.
19505  * Port is probed to find if a device is really gone. If so,
19506  * the device info structure is detached from the SATA port info structure
19507  * and released.
19508  * Port status is updated.
19509  *
19510  * NOTE: Port multiplier ports events are handled by
19511  * sata_process_pmdevice_detached()
19512  */
19513 static void
19514 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst,
19515     sata_address_t *saddr)
19516 {
19517 	sata_cport_info_t *cportinfo;
19518 	sata_pmport_info_t *pmportinfo;
19519 	sata_drive_info_t *sdevinfo;
19520 	sata_device_t sata_device;
19521 	sata_address_t pmport_addr;
19522 	char name[16];
19523 	uint8_t cport = saddr->cport;
19524 	int npmport;
19525 	int rval;
19526 
19527 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19528 	    "Processing port %d device detached", saddr->cport);
19529 
19530 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19531 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19532 	/* Clear event flag */
19533 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
19534 
19535 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
19536 	if ((cportinfo->cport_state &
19537 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19538 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19539 		    cport_mutex);
19540 		return;
19541 	}
19542 	/* For sanity, re-probe the port */
19543 	sata_device.satadev_rev = SATA_DEVICE_REV;
19544 	sata_device.satadev_addr = *saddr;
19545 
19546 	/*
19547 	 * We have to exit mutex, because the HBA probe port function may
19548 	 * block on its own mutex.
19549 	 */
19550 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19551 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19552 	    (SATA_DIP(sata_hba_inst), &sata_device);
19553 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19554 	sata_update_port_info(sata_hba_inst, &sata_device);
19555 	if (rval != SATA_SUCCESS) {
19556 		/* Something went wrong? Fail the port */
19557 		cportinfo->cport_state = SATA_PSTATE_FAILED;
19558 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19559 		    cport_mutex);
19560 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19561 		    "SATA port %d probing failed",
19562 		    saddr->cport));
19563 		/*
19564 		 * We may want to release device info structure, but
19565 		 * it is not necessary.
19566 		 */
19567 		return;
19568 	} else {
19569 		/* port probed successfully */
19570 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19571 	}
19572 	/*
19573 	 * Check if a device is still attached. For sanity, check also
19574 	 * link status - if no link, there is no device.
19575 	 */
19576 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
19577 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
19578 	    SATA_DTYPE_NONE) {
19579 		/*
19580 		 * Device is still attached - ignore detach event.
19581 		 */
19582 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19583 		    cport_mutex);
19584 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19585 		    "Ignoring detach - device still attached to port %d",
19586 		    sata_device.satadev_addr.cport);
19587 		return;
19588 	}
19589 	/*
19590 	 * We need to detach and release device info structure here
19591 	 */
19592 	if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
19593 		/*
19594 		 * A port-multiplier is removed.
19595 		 *
19596 		 * Calling sata_process_pmdevice_detached() does not work
19597 		 * here. The port multiplier is gone, so we cannot probe
19598 		 * sub-port any more and all pmult-related data structure must
19599 		 * be de-allocated immediately. Following structure of every
19600 		 * implemented sub-port behind the pmult are required to
19601 		 * released.
19602 		 *
19603 		 *   - attachment point
19604 		 *   - target node
19605 		 *   - sata_drive_info
19606 		 *   - sata_pmport_info
19607 		 */
19608 		for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst,
19609 		    cport); npmport ++) {
19610 			SATADBG2(SATA_DBG_PMULT|SATA_DBG_EVENTS_PROC,
19611 			    sata_hba_inst,
19612 			    "Detaching target node at port %d:%d",
19613 			    cport, npmport);
19614 
19615 			mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
19616 
19617 			/* Remove attachment point. */
19618 			name[0] = '\0';
19619 			(void) sprintf(name, "%d.%d", cport, npmport);
19620 			ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name);
19621 			sata_log(sata_hba_inst, CE_NOTE,
19622 			    "Remove attachment point of port %d:%d",
19623 			    cport, npmport);
19624 
19625 			/* Remove target node */
19626 			pmport_addr.cport = cport;
19627 			pmport_addr.pmport = (uint8_t)npmport;
19628 			pmport_addr.qual = SATA_ADDR_PMPORT;
19629 			sata_remove_target_node(sata_hba_inst, &pmport_addr);
19630 
19631 			mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
19632 
19633 			/* Release sata_pmport_info & sata_drive_info. */
19634 			pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
19635 			    cport, npmport);
19636 			ASSERT(pmportinfo != NULL);
19637 
19638 			sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19639 			if (sdevinfo != NULL) {
19640 				(void) kmem_free((void *) sdevinfo,
19641 				    sizeof (sata_drive_info_t));
19642 			}
19643 
19644 			/* Release sata_pmport_info at last */
19645 			(void) kmem_free((void *) pmportinfo,
19646 			    sizeof (sata_pmport_info_t));
19647 		}
19648 
19649 		/* Finally, release sata_pmult_info */
19650 		(void) kmem_free((void *)
19651 		    SATA_CPORTINFO_PMULT_INFO(cportinfo),
19652 		    sizeof (sata_pmult_info_t));
19653 		SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL;
19654 
19655 		sata_log(sata_hba_inst, CE_WARN,
19656 		    "SATA port-multiplier detached at port %d", cport);
19657 
19658 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19659 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19660 		    saddr->cport)->cport_mutex);
19661 	} else {
19662 		if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19663 			sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19664 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
19665 			(void) kmem_free((void *)sdevinfo,
19666 			    sizeof (sata_drive_info_t));
19667 		}
19668 		sata_log(sata_hba_inst, CE_WARN,
19669 		    "SATA device detached at port %d", cport);
19670 
19671 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19672 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19673 		    saddr->cport)->cport_mutex);
19674 
19675 		/*
19676 		 * Try to offline a device and remove target node
19677 		 * if it still exists
19678 		 */
19679 		sata_remove_target_node(sata_hba_inst, saddr);
19680 	}
19681 
19682 
19683 	/*
19684 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19685 	 * with the hint: SE_HINT_REMOVE
19686 	 */
19687 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
19688 }
19689 
19690 /*
19691  * Port Multiplier Port Device Deattached Event processing.
19692  *
19693  * NOTE: No Mutex should be hold.
19694  */
19695 static void
19696 sata_process_pmdevice_detached(sata_hba_inst_t *sata_hba_inst,
19697     sata_address_t *saddr)
19698 {
19699 	sata_pmport_info_t *pmportinfo;
19700 	sata_drive_info_t *sdevinfo;
19701 	sata_device_t sata_device;
19702 	int rval;
19703 	uint8_t cport, pmport;
19704 
19705 	cport = saddr->cport;
19706 	pmport = saddr->pmport;
19707 
19708 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19709 	    "Processing port %d:%d device detached",
19710 	    cport, pmport);
19711 
19712 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
19713 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19714 
19715 	/* Clear event flag */
19716 	pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
19717 
19718 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
19719 	if ((pmportinfo->pmport_state &
19720 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19721 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19722 		return;
19723 	}
19724 	/* For sanity, re-probe the port */
19725 	sata_device.satadev_rev = SATA_DEVICE_REV;
19726 	sata_device.satadev_addr = *saddr;
19727 
19728 	/*
19729 	 * We have to exit mutex, because the HBA probe port function may
19730 	 * block on its own mutex.
19731 	 */
19732 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19733 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19734 	    (SATA_DIP(sata_hba_inst), &sata_device);
19735 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19736 	sata_update_pmport_info(sata_hba_inst, &sata_device);
19737 	if (rval != SATA_SUCCESS) {
19738 		/* Something went wrong? Fail the port */
19739 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
19740 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19741 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19742 		    "SATA port %d:%d probing failed",
19743 		    saddr->pmport));
19744 		/*
19745 		 * We may want to release device info structure, but
19746 		 * it is not necessary.
19747 		 */
19748 		return;
19749 	} else {
19750 		/* port probed successfully */
19751 		pmportinfo->pmport_state |=
19752 		    SATA_STATE_PROBED | SATA_STATE_READY;
19753 	}
19754 	/*
19755 	 * Check if a device is still attached. For sanity, check also
19756 	 * link status - if no link, there is no device.
19757 	 */
19758 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
19759 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
19760 	    SATA_DTYPE_NONE) {
19761 		/*
19762 		 * Device is still attached - ignore detach event.
19763 		 */
19764 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19765 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19766 		    "Ignoring detach - device still attached to port %d",
19767 		    sata_device.satadev_addr.pmport);
19768 		return;
19769 	}
19770 	/*
19771 	 * We need to detach and release device info structure here
19772 	 */
19773 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
19774 		sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19775 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
19776 		(void) kmem_free((void *)sdevinfo,
19777 		    sizeof (sata_drive_info_t));
19778 	}
19779 	pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
19780 	/*
19781 	 * Device cannot be reached anymore, even if the target node may be
19782 	 * still present.
19783 	 */
19784 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19785 
19786 	/*
19787 	 * Try to offline a device and remove target node if it still exists
19788 	 */
19789 	sata_remove_target_node(sata_hba_inst, saddr);
19790 
19791 	/*
19792 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19793 	 * with the hint: SE_HINT_REMOVE
19794 	 */
19795 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
19796 }
19797 
19798 
19799 /*
19800  * Device Attached Event processing.
19801  * Port state is checked to verify that a device is really attached. If so,
19802  * the device info structure is created and attached to the SATA port info
19803  * structure.
19804  *
19805  * If attached device cannot be identified or set-up, the retry for the
19806  * attach processing is set-up. Subsequent daemon run would try again to
19807  * identify the device, until the time limit is reached
19808  * (SATA_DEV_IDENTIFY_TIMEOUT).
19809  *
19810  * This function cannot be called in interrupt context (it may sleep).
19811  *
19812  * NOTE: Port multiplier ports events are handled by
19813  * sata_process_pmdevice_attached()
19814  */
19815 static void
19816 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst,
19817     sata_address_t *saddr)
19818 {
19819 	sata_cport_info_t *cportinfo = NULL;
19820 	sata_drive_info_t *sdevinfo = NULL;
19821 	sata_pmult_info_t *pmultinfo = NULL;
19822 	sata_pmport_info_t *pmportinfo = NULL;
19823 	sata_device_t sata_device;
19824 	dev_info_t *tdip;
19825 	uint32_t event_flags = 0, pmult_event_flags = 0;
19826 	int rval;
19827 	int npmport;
19828 
19829 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19830 	    "Processing port %d device attached", saddr->cport);
19831 
19832 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19833 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19834 
19835 	/* Clear attach event flag first */
19836 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
19837 
19838 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
19839 	if ((cportinfo->cport_state &
19840 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19841 		cportinfo->cport_dev_attach_time = 0;
19842 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19843 		    cport_mutex);
19844 		return;
19845 	}
19846 
19847 	/*
19848 	 * If the sata_drive_info structure is found attached to the port info,
19849 	 * despite the fact the device was removed and now it is re-attached,
19850 	 * the old drive info structure was not removed.
19851 	 * Arbitrarily release device info structure.
19852 	 */
19853 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19854 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19855 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
19856 		(void) kmem_free((void *)sdevinfo,
19857 		    sizeof (sata_drive_info_t));
19858 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19859 		    "Arbitrarily detaching old device info.", NULL);
19860 	}
19861 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19862 
19863 	/* For sanity, re-probe the port */
19864 	sata_device.satadev_rev = SATA_DEVICE_REV;
19865 	sata_device.satadev_addr = *saddr;
19866 
19867 	/*
19868 	 * We have to exit mutex, because the HBA probe port function may
19869 	 * block on its own mutex.
19870 	 */
19871 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19872 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19873 	    (SATA_DIP(sata_hba_inst), &sata_device);
19874 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19875 	sata_update_port_info(sata_hba_inst, &sata_device);
19876 	if (rval != SATA_SUCCESS) {
19877 		/* Something went wrong? Fail the port */
19878 		cportinfo->cport_state = SATA_PSTATE_FAILED;
19879 		cportinfo->cport_dev_attach_time = 0;
19880 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19881 		    cport_mutex);
19882 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19883 		    "SATA port %d probing failed",
19884 		    saddr->cport));
19885 		return;
19886 	} else {
19887 		/* port probed successfully */
19888 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19889 	}
19890 	/*
19891 	 * Check if a device is still attached. For sanity, check also
19892 	 * link status - if no link, there is no device.
19893 	 */
19894 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
19895 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
19896 	    SATA_DTYPE_NONE) {
19897 		/*
19898 		 * No device - ignore attach event.
19899 		 */
19900 		cportinfo->cport_dev_attach_time = 0;
19901 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19902 		    cport_mutex);
19903 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19904 		    "Ignoring attach - no device connected to port %d",
19905 		    sata_device.satadev_addr.cport);
19906 		return;
19907 	}
19908 
19909 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19910 	/*
19911 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19912 	 * with the hint: SE_HINT_INSERT
19913 	 */
19914 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
19915 
19916 	/*
19917 	 * Port reprobing will take care of the creation of the device
19918 	 * info structure and determination of the device type.
19919 	 */
19920 	sata_device.satadev_addr = *saddr;
19921 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
19922 	    SATA_DEV_IDENTIFY_NORETRY);
19923 
19924 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19925 	    cport_mutex);
19926 	if ((cportinfo->cport_state & SATA_STATE_READY) &&
19927 	    (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) {
19928 		/* Some device is attached to the port */
19929 		if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) {
19930 			/*
19931 			 * A device was not successfully attached.
19932 			 * Track retry time for device identification.
19933 			 */
19934 			if (cportinfo->cport_dev_attach_time != 0) {
19935 				clock_t cur_time = ddi_get_lbolt();
19936 				/*
19937 				 * If the retry time limit was not exceeded,
19938 				 * reinstate attach event.
19939 				 */
19940 				if ((cur_time -
19941 				    cportinfo->cport_dev_attach_time) <
19942 				    drv_usectohz(
19943 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
19944 					/* OK, restore attach event */
19945 					cportinfo->cport_event_flags |=
19946 					    SATA_EVNT_DEVICE_ATTACHED;
19947 				} else {
19948 					/* Timeout - cannot identify device */
19949 					cportinfo->cport_dev_attach_time = 0;
19950 					sata_log(sata_hba_inst,
19951 					    CE_WARN,
19952 					    "Could not identify SATA device "
19953 					    "at port %d",
19954 					    saddr->cport);
19955 				}
19956 			} else {
19957 				/*
19958 				 * Start tracking time for device
19959 				 * identification.
19960 				 * Save current time (lbolt value).
19961 				 */
19962 				cportinfo->cport_dev_attach_time =
19963 				    ddi_get_lbolt();
19964 				/* Restore attach event */
19965 				cportinfo->cport_event_flags |=
19966 				    SATA_EVNT_DEVICE_ATTACHED;
19967 			}
19968 		} else if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
19969 			cportinfo->cport_dev_attach_time = 0;
19970 			sata_log(sata_hba_inst, CE_NOTE,
19971 			    "SATA port-multiplier detected at port %d",
19972 			    saddr->cport);
19973 
19974 			if (SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL) {
19975 				/* Log the info of new port multiplier */
19976 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19977 				    saddr->cport)->cport_mutex);
19978 				sata_show_pmult_info(sata_hba_inst,
19979 				    &sata_device);
19980 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19981 				    saddr->cport)->cport_mutex);
19982 			}
19983 
19984 			ASSERT(SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL);
19985 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
19986 			for (npmport = 0; npmport <
19987 			    pmultinfo->pmult_num_dev_ports; npmport++) {
19988 				pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
19989 				    saddr->cport, npmport);
19990 				ASSERT(pmportinfo != NULL);
19991 
19992 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19993 				    saddr->cport)->cport_mutex);
19994 				mutex_enter(&pmportinfo->pmport_mutex);
19995 				/* Marked all pmports with link events. */
19996 				pmportinfo->pmport_event_flags =
19997 				    SATA_EVNT_LINK_ESTABLISHED;
19998 				pmult_event_flags |=
19999 				    pmportinfo->pmport_event_flags;
20000 				mutex_exit(&pmportinfo->pmport_mutex);
20001 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20002 				    saddr->cport)->cport_mutex);
20003 			}
20004 			/* Auto-online is not available for PMult now. */
20005 
20006 		} else {
20007 			/*
20008 			 * If device was successfully attached, the subsequent
20009 			 * action depends on a state of the
20010 			 * sata_auto_online variable. If it is set to zero.
20011 			 * an explicit 'configure' command will be needed to
20012 			 * configure it. If its value is non-zero, we will
20013 			 * attempt to online (configure) the device.
20014 			 * First, log the message indicating that a device
20015 			 * was attached.
20016 			 */
20017 			cportinfo->cport_dev_attach_time = 0;
20018 			sata_log(sata_hba_inst, CE_WARN,
20019 			    "SATA device detected at port %d", saddr->cport);
20020 
20021 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
20022 				sata_drive_info_t new_sdinfo;
20023 
20024 				/* Log device info data */
20025 				new_sdinfo = *(SATA_CPORTINFO_DRV_INFO(
20026 				    cportinfo));
20027 				sata_show_drive_info(sata_hba_inst,
20028 				    &new_sdinfo);
20029 			}
20030 
20031 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20032 			    saddr->cport)->cport_mutex);
20033 
20034 			/*
20035 			 * Make sure that there is no target node for that
20036 			 * device. If so, release it. It should not happen,
20037 			 * unless we had problem removing the node when
20038 			 * device was detached.
20039 			 */
20040 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
20041 			    saddr->cport, saddr->pmport);
20042 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20043 			    saddr->cport)->cport_mutex);
20044 			if (tdip != NULL) {
20045 
20046 #ifdef SATA_DEBUG
20047 				if ((cportinfo->cport_event_flags &
20048 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
20049 					sata_log(sata_hba_inst, CE_WARN,
20050 					    "sata_process_device_attached: "
20051 					    "old device target node exists!");
20052 #endif
20053 				/*
20054 				 * target node exists - try to unconfigure
20055 				 * device and remove the node.
20056 				 */
20057 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20058 				    saddr->cport)->cport_mutex);
20059 				rval = ndi_devi_offline(tdip,
20060 				    NDI_DEVI_REMOVE);
20061 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20062 				    saddr->cport)->cport_mutex);
20063 
20064 				if (rval == NDI_SUCCESS) {
20065 					cportinfo->cport_event_flags &=
20066 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20067 					cportinfo->cport_tgtnode_clean = B_TRUE;
20068 				} else {
20069 					/*
20070 					 * PROBLEM - the target node remained
20071 					 * and it belongs to a previously
20072 					 * attached device.
20073 					 * This happens when the file was open
20074 					 * or the node was waiting for
20075 					 * resources at the time the
20076 					 * associated device was removed.
20077 					 * Instruct event daemon to retry the
20078 					 * cleanup later.
20079 					 */
20080 					sata_log(sata_hba_inst,
20081 					    CE_WARN,
20082 					    "Application(s) accessing "
20083 					    "previously attached SATA "
20084 					    "device have to release "
20085 					    "it before newly inserted "
20086 					    "device can be made accessible.",
20087 					    saddr->cport);
20088 					cportinfo->cport_event_flags |=
20089 					    SATA_EVNT_TARGET_NODE_CLEANUP;
20090 					cportinfo->cport_tgtnode_clean =
20091 					    B_FALSE;
20092 				}
20093 			}
20094 			if (sata_auto_online != 0) {
20095 				cportinfo->cport_event_flags |=
20096 				    SATA_EVNT_AUTOONLINE_DEVICE;
20097 			}
20098 
20099 		}
20100 	} else {
20101 		cportinfo->cport_dev_attach_time = 0;
20102 	}
20103 
20104 	event_flags = cportinfo->cport_event_flags;
20105 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20106 	if (event_flags != 0 || pmult_event_flags != 0) {
20107 		mutex_enter(&sata_hba_inst->satahba_mutex);
20108 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20109 		mutex_exit(&sata_hba_inst->satahba_mutex);
20110 		mutex_enter(&sata_mutex);
20111 		sata_event_pending |= SATA_EVNT_MAIN;
20112 		mutex_exit(&sata_mutex);
20113 	}
20114 }
20115 
20116 /*
20117  * Port Multiplier Port Device Attached Event processing.
20118  *
20119  * NOTE: No Mutex should be hold.
20120  */
20121 static void
20122 sata_process_pmdevice_attached(sata_hba_inst_t *sata_hba_inst,
20123     sata_address_t *saddr)
20124 {
20125 	sata_pmport_info_t *pmportinfo;
20126 	sata_drive_info_t *sdinfo;
20127 	sata_device_t sata_device;
20128 	dev_info_t *tdip;
20129 	uint32_t event_flags;
20130 	uint8_t cport = saddr->cport;
20131 	uint8_t pmport = saddr->pmport;
20132 	int rval;
20133 
20134 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20135 	    "Processing port %d:%d device attached", cport, pmport);
20136 
20137 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
20138 
20139 	mutex_enter(&pmportinfo->pmport_mutex);
20140 
20141 	/* Clear attach event flag first */
20142 	pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
20143 
20144 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
20145 	if ((pmportinfo->pmport_state &
20146 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
20147 		pmportinfo->pmport_dev_attach_time = 0;
20148 		mutex_exit(&pmportinfo->pmport_mutex);
20149 		return;
20150 	}
20151 
20152 	/*
20153 	 * If the sata_drive_info structure is found attached to the port info,
20154 	 * despite the fact the device was removed and now it is re-attached,
20155 	 * the old drive info structure was not removed.
20156 	 * Arbitrarily release device info structure.
20157 	 */
20158 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20159 		sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
20160 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
20161 		(void) kmem_free((void *)sdinfo,
20162 		    sizeof (sata_drive_info_t));
20163 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20164 		    "Arbitrarily detaching old device info.", NULL);
20165 	}
20166 	pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
20167 
20168 	/* For sanity, re-probe the port */
20169 	sata_device.satadev_rev = SATA_DEVICE_REV;
20170 	sata_device.satadev_addr = *saddr;
20171 
20172 	/*
20173 	 * We have to exit mutex, because the HBA probe port function may
20174 	 * block on its own mutex.
20175 	 */
20176 	mutex_exit(&pmportinfo->pmport_mutex);
20177 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
20178 	    (SATA_DIP(sata_hba_inst), &sata_device);
20179 	mutex_enter(&pmportinfo->pmport_mutex);
20180 
20181 	sata_update_pmport_info(sata_hba_inst, &sata_device);
20182 	if (rval != SATA_SUCCESS) {
20183 		/* Something went wrong? Fail the port */
20184 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
20185 		pmportinfo->pmport_dev_attach_time = 0;
20186 		mutex_exit(&pmportinfo->pmport_mutex);
20187 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20188 		    "SATA port %d:%d probing failed", cport, pmport));
20189 		return;
20190 	} else {
20191 		/* pmport probed successfully */
20192 		pmportinfo->pmport_state |=
20193 		    SATA_STATE_PROBED | SATA_STATE_READY;
20194 	}
20195 	/*
20196 	 * Check if a device is still attached. For sanity, check also
20197 	 * link status - if no link, there is no device.
20198 	 */
20199 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
20200 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
20201 	    SATA_DTYPE_NONE) {
20202 		/*
20203 		 * No device - ignore attach event.
20204 		 */
20205 		pmportinfo->pmport_dev_attach_time = 0;
20206 		mutex_exit(&pmportinfo->pmport_mutex);
20207 		SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20208 		    "Ignoring attach - no device connected to port %d:%d",
20209 		    cport, pmport);
20210 		return;
20211 	}
20212 
20213 	mutex_exit(&pmportinfo->pmport_mutex);
20214 	/*
20215 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
20216 	 * with the hint: SE_HINT_INSERT
20217 	 */
20218 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
20219 
20220 	/*
20221 	 * Port reprobing will take care of the creation of the device
20222 	 * info structure and determination of the device type.
20223 	 */
20224 	sata_device.satadev_addr = *saddr;
20225 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
20226 	    SATA_DEV_IDENTIFY_NORETRY);
20227 
20228 	mutex_enter(&pmportinfo->pmport_mutex);
20229 	if ((pmportinfo->pmport_state & SATA_STATE_READY) &&
20230 	    (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE)) {
20231 		/* Some device is attached to the port */
20232 		if (pmportinfo->pmport_dev_type == SATA_DTYPE_UNKNOWN) {
20233 			/*
20234 			 * A device was not successfully attached.
20235 			 * Track retry time for device identification.
20236 			 */
20237 			if (pmportinfo->pmport_dev_attach_time != 0) {
20238 				clock_t cur_time = ddi_get_lbolt();
20239 				/*
20240 				 * If the retry time limit was not exceeded,
20241 				 * reinstate attach event.
20242 				 */
20243 				if ((cur_time -
20244 				    pmportinfo->pmport_dev_attach_time) <
20245 				    drv_usectohz(
20246 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
20247 					/* OK, restore attach event */
20248 					pmportinfo->pmport_event_flags |=
20249 					    SATA_EVNT_DEVICE_ATTACHED;
20250 				} else {
20251 					/* Timeout - cannot identify device */
20252 					pmportinfo->pmport_dev_attach_time = 0;
20253 					sata_log(sata_hba_inst, CE_WARN,
20254 					    "Could not identify SATA device "
20255 					    "at port %d:%d",
20256 					    cport, pmport);
20257 				}
20258 			} else {
20259 				/*
20260 				 * Start tracking time for device
20261 				 * identification.
20262 				 * Save current time (lbolt value).
20263 				 */
20264 				pmportinfo->pmport_dev_attach_time =
20265 				    ddi_get_lbolt();
20266 				/* Restore attach event */
20267 				pmportinfo->pmport_event_flags |=
20268 				    SATA_EVNT_DEVICE_ATTACHED;
20269 			}
20270 		} else {
20271 			/*
20272 			 * If device was successfully attached, the subsequent
20273 			 * action depends on a state of the
20274 			 * sata_auto_online variable. If it is set to zero.
20275 			 * an explicit 'configure' command will be needed to
20276 			 * configure it. If its value is non-zero, we will
20277 			 * attempt to online (configure) the device.
20278 			 * First, log the message indicating that a device
20279 			 * was attached.
20280 			 */
20281 			pmportinfo->pmport_dev_attach_time = 0;
20282 			sata_log(sata_hba_inst, CE_WARN,
20283 			    "SATA device detected at port %d:%d",
20284 			    cport, pmport);
20285 
20286 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20287 				sata_drive_info_t new_sdinfo;
20288 
20289 				/* Log device info data */
20290 				new_sdinfo = *(SATA_PMPORTINFO_DRV_INFO(
20291 				    pmportinfo));
20292 				sata_show_drive_info(sata_hba_inst,
20293 				    &new_sdinfo);
20294 			}
20295 
20296 			mutex_exit(&pmportinfo->pmport_mutex);
20297 
20298 			/*
20299 			 * Make sure that there is no target node for that
20300 			 * device. If so, release it. It should not happen,
20301 			 * unless we had problem removing the node when
20302 			 * device was detached.
20303 			 */
20304 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
20305 			    saddr->cport, saddr->pmport);
20306 			mutex_enter(&pmportinfo->pmport_mutex);
20307 			if (tdip != NULL) {
20308 
20309 #ifdef SATA_DEBUG
20310 				if ((pmportinfo->pmport_event_flags &
20311 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
20312 					sata_log(sata_hba_inst, CE_WARN,
20313 					    "sata_process_device_attached: "
20314 					    "old device target node exists!");
20315 #endif
20316 				/*
20317 				 * target node exists - try to unconfigure
20318 				 * device and remove the node.
20319 				 */
20320 				mutex_exit(&pmportinfo->pmport_mutex);
20321 				rval = ndi_devi_offline(tdip,
20322 				    NDI_DEVI_REMOVE);
20323 				mutex_enter(&pmportinfo->pmport_mutex);
20324 
20325 				if (rval == NDI_SUCCESS) {
20326 					pmportinfo->pmport_event_flags &=
20327 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20328 					pmportinfo->pmport_tgtnode_clean =
20329 					    B_TRUE;
20330 				} else {
20331 					/*
20332 					 * PROBLEM - the target node remained
20333 					 * and it belongs to a previously
20334 					 * attached device.
20335 					 * This happens when the file was open
20336 					 * or the node was waiting for
20337 					 * resources at the time the
20338 					 * associated device was removed.
20339 					 * Instruct event daemon to retry the
20340 					 * cleanup later.
20341 					 */
20342 					sata_log(sata_hba_inst,
20343 					    CE_WARN,
20344 					    "Application(s) accessing "
20345 					    "previously attached SATA "
20346 					    "device have to release "
20347 					    "it before newly inserted "
20348 					    "device can be made accessible."
20349 					    "at port %d:%d",
20350 					    cport, pmport);
20351 					pmportinfo->pmport_event_flags |=
20352 					    SATA_EVNT_TARGET_NODE_CLEANUP;
20353 					pmportinfo->pmport_tgtnode_clean =
20354 					    B_FALSE;
20355 				}
20356 			}
20357 			if (sata_auto_online != 0) {
20358 				pmportinfo->pmport_event_flags |=
20359 				    SATA_EVNT_AUTOONLINE_DEVICE;
20360 			}
20361 
20362 		}
20363 	} else {
20364 		pmportinfo->pmport_dev_attach_time = 0;
20365 	}
20366 
20367 	event_flags = pmportinfo->pmport_event_flags;
20368 	mutex_exit(&pmportinfo->pmport_mutex);
20369 	if (event_flags != 0) {
20370 		mutex_enter(&sata_hba_inst->satahba_mutex);
20371 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20372 		mutex_exit(&sata_hba_inst->satahba_mutex);
20373 		mutex_enter(&sata_mutex);
20374 		sata_event_pending |= SATA_EVNT_MAIN;
20375 		mutex_exit(&sata_mutex);
20376 	}
20377 
20378 	/* clear the reset_in_progress events */
20379 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20380 		if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) {
20381 			/* must clear flags on cport */
20382 			sata_pmult_info_t *pminfo =
20383 			    SATA_PMULT_INFO(sata_hba_inst,
20384 			    saddr->cport);
20385 			pminfo->pmult_event_flags |=
20386 			    SATA_EVNT_CLEAR_DEVICE_RESET;
20387 		}
20388 	}
20389 }
20390 
20391 /*
20392  * Device Target Node Cleanup Event processing.
20393  * If the target node associated with a sata port device is in
20394  * DEVI_DEVICE_REMOVED state, an attempt is made to remove it.
20395  * If the target node cannot be removed, the event flag is left intact,
20396  * so that event daemon may re-run this function later.
20397  *
20398  * This function cannot be called in interrupt context (it may sleep).
20399  *
20400  * NOTE: Processes cport events only, not port multiplier ports.
20401  */
20402 static void
20403 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
20404     sata_address_t *saddr)
20405 {
20406 	sata_cport_info_t *cportinfo;
20407 	dev_info_t *tdip;
20408 
20409 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20410 	    "Processing port %d device target node cleanup", saddr->cport);
20411 
20412 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
20413 
20414 	/*
20415 	 * Check if there is target node for that device and it is in the
20416 	 * DEVI_DEVICE_REMOVED state. If so, release it.
20417 	 */
20418 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport,
20419 	    saddr->pmport);
20420 	if (tdip != NULL) {
20421 		/*
20422 		 * target node exists - check if it is target node of
20423 		 * a removed device.
20424 		 */
20425 		if (sata_check_device_removed(tdip) == B_TRUE) {
20426 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20427 			    "sata_process_target_node_cleanup: "
20428 			    "old device target node exists!", NULL);
20429 			/*
20430 			 * Unconfigure and remove the target node
20431 			 */
20432 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) ==
20433 			    NDI_SUCCESS) {
20434 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20435 				    saddr->cport)->cport_mutex);
20436 				cportinfo->cport_event_flags &=
20437 				    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20438 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20439 				    saddr->cport)->cport_mutex);
20440 				return;
20441 			}
20442 			/*
20443 			 * Event daemon will retry the cleanup later.
20444 			 */
20445 			mutex_enter(&sata_hba_inst->satahba_mutex);
20446 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20447 			mutex_exit(&sata_hba_inst->satahba_mutex);
20448 			mutex_enter(&sata_mutex);
20449 			sata_event_pending |= SATA_EVNT_MAIN;
20450 			mutex_exit(&sata_mutex);
20451 		}
20452 	} else {
20453 		if (saddr->qual == SATA_ADDR_CPORT ||
20454 		    saddr->qual == SATA_ADDR_DCPORT) {
20455 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20456 			    saddr->cport)->cport_mutex);
20457 			cportinfo->cport_event_flags &=
20458 			    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20459 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20460 			    saddr->cport)->cport_mutex);
20461 		} else {
20462 			/* sanity check */
20463 			if (SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) !=
20464 			    SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst,
20465 			    saddr->cport) == NULL)
20466 				return;
20467 			if (SATA_PMPORT_INFO(sata_hba_inst, saddr->cport,
20468 			    saddr->pmport) == NULL)
20469 				return;
20470 
20471 			mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst,
20472 			    saddr->cport, saddr->pmport)->pmport_mutex);
20473 			SATA_PMPORT_INFO(sata_hba_inst, saddr->cport,
20474 			    saddr->pmport)->pmport_event_flags &=
20475 			    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20476 			mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst,
20477 			    saddr->cport, saddr->pmport)->pmport_mutex);
20478 		}
20479 	}
20480 }
20481 
20482 /*
20483  * Device AutoOnline Event processing.
20484  * If attached device is to be onlined, an attempt is made to online this
20485  * device, but only if there is no lingering (old) target node present.
20486  * If the device cannot be onlined, the event flag is left intact,
20487  * so that event daemon may re-run this function later.
20488  *
20489  * This function cannot be called in interrupt context (it may sleep).
20490  *
20491  * NOTE: Processes cport events only, not port multiplier ports.
20492  */
20493 static void
20494 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst,
20495     sata_address_t *saddr)
20496 {
20497 	sata_cport_info_t *cportinfo;
20498 	sata_drive_info_t *sdinfo;
20499 	sata_device_t sata_device;
20500 	dev_info_t *tdip;
20501 
20502 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20503 	    "Processing port %d attached device auto-onlining", saddr->cport);
20504 
20505 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
20506 
20507 	/*
20508 	 * Check if device is present and recognized. If not, reset event.
20509 	 */
20510 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20511 	if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
20512 		/* Nothing to online */
20513 		cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
20514 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20515 		    saddr->cport)->cport_mutex);
20516 		return;
20517 	}
20518 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20519 
20520 	/*
20521 	 * Check if there is target node for this device and if it is in the
20522 	 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep
20523 	 * the event for later processing.
20524 	 */
20525 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport,
20526 	    saddr->pmport);
20527 	if (tdip != NULL) {
20528 		/*
20529 		 * target node exists - check if it is target node of
20530 		 * a removed device.
20531 		 */
20532 		if (sata_check_device_removed(tdip) == B_TRUE) {
20533 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20534 			    "sata_process_device_autoonline: "
20535 			    "old device target node exists!", NULL);
20536 			/*
20537 			 * Event daemon will retry device onlining later.
20538 			 */
20539 			mutex_enter(&sata_hba_inst->satahba_mutex);
20540 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20541 			mutex_exit(&sata_hba_inst->satahba_mutex);
20542 			mutex_enter(&sata_mutex);
20543 			sata_event_pending |= SATA_EVNT_MAIN;
20544 			mutex_exit(&sata_mutex);
20545 			return;
20546 		}
20547 		/*
20548 		 * If the target node is not in the 'removed" state, assume
20549 		 * that it belongs to this device. There is nothing more to do,
20550 		 * but reset the event.
20551 		 */
20552 	} else {
20553 
20554 		/*
20555 		 * Try to online the device
20556 		 * If there is any reset-related event, remove it. We are
20557 		 * configuring the device and no state restoring is needed.
20558 		 */
20559 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20560 		    saddr->cport)->cport_mutex);
20561 		sata_device.satadev_addr = *saddr;
20562 		if (saddr->qual == SATA_ADDR_CPORT)
20563 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
20564 		else
20565 			sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
20566 		sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
20567 		if (sdinfo != NULL) {
20568 			if (sdinfo->satadrv_event_flags &
20569 			    (SATA_EVNT_DEVICE_RESET |
20570 			    SATA_EVNT_INPROC_DEVICE_RESET))
20571 				sdinfo->satadrv_event_flags = 0;
20572 			sdinfo->satadrv_event_flags |=
20573 			    SATA_EVNT_CLEAR_DEVICE_RESET;
20574 
20575 			/* Need to create a new target node. */
20576 			cportinfo->cport_tgtnode_clean = B_TRUE;
20577 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20578 			    saddr->cport)->cport_mutex);
20579 			tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
20580 			    sata_hba_inst, &sata_device.satadev_addr);
20581 			if (tdip == NULL) {
20582 				/*
20583 				 * Configure (onlining) failed.
20584 				 * We will NOT retry
20585 				 */
20586 				SATA_LOG_D((sata_hba_inst, CE_WARN,
20587 				    "sata_process_device_autoonline: "
20588 				    "configuring SATA device at port %d failed",
20589 				    saddr->cport));
20590 			}
20591 		} else {
20592 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20593 			    saddr->cport)->cport_mutex);
20594 		}
20595 
20596 	}
20597 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20598 	cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
20599 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20600 	    saddr->cport)->cport_mutex);
20601 }
20602 
20603 
20604 static void
20605 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr,
20606     int hint)
20607 {
20608 	char ap[MAXPATHLEN];
20609 	nvlist_t *ev_attr_list = NULL;
20610 	int err;
20611 
20612 	/* Allocate and build sysevent attribute list */
20613 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP);
20614 	if (err != 0) {
20615 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20616 		    "sata_gen_sysevent: "
20617 		    "cannot allocate memory for sysevent attributes\n"));
20618 		return;
20619 	}
20620 	/* Add hint attribute */
20621 	err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint));
20622 	if (err != 0) {
20623 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20624 		    "sata_gen_sysevent: "
20625 		    "failed to add DR_HINT attr for sysevent"));
20626 		nvlist_free(ev_attr_list);
20627 		return;
20628 	}
20629 	/*
20630 	 * Add AP attribute.
20631 	 * Get controller pathname and convert it into AP pathname by adding
20632 	 * a target number.
20633 	 */
20634 	(void) snprintf(ap, MAXPATHLEN, "/devices");
20635 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap));
20636 	(void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d",
20637 	    SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual));
20638 
20639 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap);
20640 	if (err != 0) {
20641 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20642 		    "sata_gen_sysevent: "
20643 		    "failed to add DR_AP_ID attr for sysevent"));
20644 		nvlist_free(ev_attr_list);
20645 		return;
20646 	}
20647 
20648 	/* Generate/log sysevent */
20649 	err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR,
20650 	    ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP);
20651 	if (err != DDI_SUCCESS) {
20652 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20653 		    "sata_gen_sysevent: "
20654 		    "cannot log sysevent, err code %x\n", err));
20655 	}
20656 
20657 	nvlist_free(ev_attr_list);
20658 }
20659 
20660 
20661 
20662 
20663 /*
20664  * Set DEVI_DEVICE_REMOVED state in the SATA device target node.
20665  */
20666 static void
20667 sata_set_device_removed(dev_info_t *tdip)
20668 {
20669 	int circ;
20670 
20671 	ASSERT(tdip != NULL);
20672 
20673 	ndi_devi_enter(tdip, &circ);
20674 	mutex_enter(&DEVI(tdip)->devi_lock);
20675 	DEVI_SET_DEVICE_REMOVED(tdip);
20676 	mutex_exit(&DEVI(tdip)->devi_lock);
20677 	ndi_devi_exit(tdip, circ);
20678 }
20679 
20680 
20681 /*
20682  * Set internal event instructing event daemon to try
20683  * to perform the target node cleanup.
20684  */
20685 static void
20686 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
20687     sata_address_t *saddr)
20688 {
20689 	if (saddr->qual == SATA_ADDR_CPORT ||
20690 	    saddr->qual == SATA_ADDR_DCPORT) {
20691 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20692 		    saddr->cport)->cport_mutex);
20693 		SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |=
20694 		    SATA_EVNT_TARGET_NODE_CLEANUP;
20695 		SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
20696 		    cport_tgtnode_clean = B_FALSE;
20697 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20698 		    saddr->cport)->cport_mutex);
20699 	} else {
20700 		mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst,
20701 		    saddr->cport, saddr->pmport)->pmport_mutex);
20702 		SATA_PMPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport,
20703 		    saddr->pmport) |= SATA_EVNT_TARGET_NODE_CLEANUP;
20704 		SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, saddr->pmport)->
20705 		    pmport_tgtnode_clean = B_FALSE;
20706 		mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst,
20707 		    saddr->cport, saddr->pmport)->pmport_mutex);
20708 	}
20709 	mutex_enter(&sata_hba_inst->satahba_mutex);
20710 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20711 	mutex_exit(&sata_hba_inst->satahba_mutex);
20712 	mutex_enter(&sata_mutex);
20713 	sata_event_pending |= SATA_EVNT_MAIN;
20714 	mutex_exit(&sata_mutex);
20715 }
20716 
20717 
20718 /*
20719  * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state,
20720  * i.e. check if the target node state indicates that it belongs to a removed
20721  * device.
20722  *
20723  * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state,
20724  * B_FALSE otherwise.
20725  */
20726 static boolean_t
20727 sata_check_device_removed(dev_info_t *tdip)
20728 {
20729 	ASSERT(tdip != NULL);
20730 
20731 	if (DEVI_IS_DEVICE_REMOVED(tdip))
20732 		return (B_TRUE);
20733 	else
20734 		return (B_FALSE);
20735 }
20736 
20737 
20738 /*
20739  * Check for DMA error. Return B_TRUE if error, B_FALSE otherwise.
20740  */
20741 static boolean_t
20742 sata_check_for_dma_error(dev_info_t *dip, sata_pkt_txlate_t *spx)
20743 {
20744 	int fm_capability = ddi_fm_capable(dip);
20745 	ddi_fm_error_t de;
20746 
20747 	if (fm_capability & DDI_FM_DMACHK_CAPABLE) {
20748 		if (spx->txlt_buf_dma_handle != NULL) {
20749 			ddi_fm_dma_err_get(spx->txlt_buf_dma_handle, &de,
20750 			    DDI_FME_VERSION);
20751 			if (de.fme_status != DDI_SUCCESS)
20752 				return (B_TRUE);
20753 		}
20754 	}
20755 	return (B_FALSE);
20756 }
20757 
20758 
20759 /* ************************ FAULT INJECTTION **************************** */
20760 
20761 #ifdef SATA_INJECT_FAULTS
20762 
20763 static	uint32_t sata_fault_count = 0;
20764 static	uint32_t sata_fault_suspend_count = 0;
20765 
20766 /*
20767  * Inject sata pkt fault
20768  * It modifies returned values of the sata packet.
20769  * It returns immediately if:
20770  * pkt fault injection is not enabled (via sata_inject_fault,
20771  * sata_inject_fault_count), or invalid fault is specified (sata_fault_type),
20772  * or pkt does not contain command to be faulted (set in sata_fault_cmd), or
20773  * pkt is not directed to specified fault controller/device
20774  * (sata_fault_ctrl_dev and sata_fault_device).
20775  * If fault controller is not specified, fault injection applies to all
20776  * controllers and devices.
20777  *
20778  * First argument is the pointer to the executed sata packet.
20779  * Second argument is a pointer to a value returned by the HBA tran_start
20780  * function.
20781  * Third argument specifies injected error. Injected sata packet faults
20782  * are the satapkt_reason values.
20783  * SATA_PKT_BUSY		-1	Not completed, busy
20784  * SATA_PKT_DEV_ERROR		1	Device reported error
20785  * SATA_PKT_QUEUE_FULL		2	Not accepted, queue full
20786  * SATA_PKT_PORT_ERROR		3	Not completed, port error
20787  * SATA_PKT_CMD_UNSUPPORTED	4	Cmd unsupported
20788  * SATA_PKT_ABORTED		5	Aborted by request
20789  * SATA_PKT_TIMEOUT		6	Operation timeut
20790  * SATA_PKT_RESET		7	Aborted by reset request
20791  *
20792  * Additional global variables affecting the execution:
20793  *
20794  * sata_inject_fault_count variable specifies number of times in row the
20795  * error is injected. Value of -1 specifies permanent fault, ie. every time
20796  * the fault injection point is reached, the fault is injected and a pause
20797  * between fault injection specified by sata_inject_fault_pause_count is
20798  * ignored). Fault injection routine decrements sata_inject_fault_count
20799  * (if greater than zero) until it reaches 0. No fault is injected when
20800  * sata_inject_fault_count is 0 (zero).
20801  *
20802  * sata_inject_fault_pause_count variable specifies number of times a fault
20803  * injection is bypassed (pause between fault injections).
20804  * If set to 0, a fault is injected only a number of times specified by
20805  * sata_inject_fault_count.
20806  *
20807  * The fault counts are static, so for periodic errors they have to be manually
20808  * reset to start repetition sequence from scratch.
20809  * If the original value returned by the HBA tran_start function is not
20810  * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error
20811  * is injected (to avoid masking real problems);
20812  *
20813  * NOTE: In its current incarnation, this function should be invoked only for
20814  * commands executed in SYNCHRONOUS mode.
20815  */
20816 
20817 
20818 static void
20819 sata_inject_pkt_fault(sata_pkt_t *spkt, int *rval, int fault)
20820 {
20821 
20822 	if (sata_inject_fault != SATA_INJECT_PKT_FAULT)
20823 		return;
20824 
20825 	if (sata_inject_fault_count == 0)
20826 		return;
20827 
20828 	if (fault == 0)
20829 		return;
20830 
20831 	if (sata_fault_cmd != spkt->satapkt_cmd.satacmd_cmd_reg)
20832 		return;
20833 
20834 	if (sata_fault_ctrl != NULL) {
20835 		sata_pkt_txlate_t *spx =
20836 		    (sata_pkt_txlate_t *)spkt->satapkt_framework_private;
20837 
20838 		if (sata_fault_ctrl != NULL && sata_fault_ctrl !=
20839 		    spx->txlt_sata_hba_inst->satahba_dip)
20840 			return;
20841 
20842 		if (sata_fault_device.satadev_addr.cport !=
20843 		    spkt->satapkt_device.satadev_addr.cport ||
20844 		    sata_fault_device.satadev_addr.pmport !=
20845 		    spkt->satapkt_device.satadev_addr.pmport ||
20846 		    sata_fault_device.satadev_addr.qual !=
20847 		    spkt->satapkt_device.satadev_addr.qual)
20848 			return;
20849 	}
20850 
20851 	/* Modify pkt return parameters */
20852 	if (*rval != SATA_TRAN_ACCEPTED ||
20853 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
20854 		sata_fault_count = 0;
20855 		sata_fault_suspend_count = 0;
20856 		return;
20857 	}
20858 	if (sata_fault_count == 0 && sata_fault_suspend_count != 0) {
20859 		/* Pause in the injection */
20860 		sata_fault_suspend_count -= 1;
20861 		return;
20862 	}
20863 
20864 	if (sata_fault_count == 0 && sata_fault_suspend_count == 0) {
20865 		/*
20866 		 * Init inject fault cycle. If fault count is set to -1,
20867 		 * it is a permanent fault.
20868 		 */
20869 		if (sata_inject_fault_count != -1) {
20870 			sata_fault_count = sata_inject_fault_count;
20871 			sata_fault_suspend_count =
20872 			    sata_inject_fault_pause_count;
20873 			if (sata_fault_suspend_count == 0)
20874 				sata_inject_fault_count = 0;
20875 		}
20876 	}
20877 
20878 	if (sata_fault_count != 0)
20879 		sata_fault_count -= 1;
20880 
20881 	switch (fault) {
20882 	case SATA_PKT_BUSY:
20883 		*rval = SATA_TRAN_BUSY;
20884 		spkt->satapkt_reason = SATA_PKT_BUSY;
20885 		break;
20886 
20887 	case SATA_PKT_QUEUE_FULL:
20888 		*rval = SATA_TRAN_QUEUE_FULL;
20889 		spkt->satapkt_reason = SATA_PKT_QUEUE_FULL;
20890 		break;
20891 
20892 	case SATA_PKT_CMD_UNSUPPORTED:
20893 		*rval = SATA_TRAN_CMD_UNSUPPORTED;
20894 		spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED;
20895 		break;
20896 
20897 	case SATA_PKT_PORT_ERROR:
20898 		/* This is "rejected" command */
20899 		*rval = SATA_TRAN_PORT_ERROR;
20900 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
20901 		/* Additional error setup could be done here - port state */
20902 		break;
20903 
20904 	case SATA_PKT_DEV_ERROR:
20905 		spkt->satapkt_reason = SATA_PKT_DEV_ERROR;
20906 		/*
20907 		 * Additional error setup could be done here
20908 		 */
20909 		break;
20910 
20911 	case SATA_PKT_ABORTED:
20912 		spkt->satapkt_reason = SATA_PKT_ABORTED;
20913 		break;
20914 
20915 	case SATA_PKT_TIMEOUT:
20916 		spkt->satapkt_reason = SATA_PKT_TIMEOUT;
20917 		/* Additional error setup could be done here */
20918 		break;
20919 
20920 	case SATA_PKT_RESET:
20921 		spkt->satapkt_reason = SATA_PKT_RESET;
20922 		/*
20923 		 * Additional error setup could be done here - device reset
20924 		 */
20925 		break;
20926 
20927 	default:
20928 		break;
20929 	}
20930 }
20931 
20932 #endif
20933 
20934 /*
20935  * SATA Trace Ring Buffer
20936  * ----------------------
20937  *
20938  * Overview
20939  *
20940  * The SATA trace ring buffer is a ring buffer created and managed by
20941  * the SATA framework module that can be used by any module or driver
20942  * within the SATA framework to store debug messages.
20943  *
20944  * Ring Buffer Interfaces:
20945  *
20946  *	sata_vtrace_debug()	<-- Adds debug message to ring buffer
20947  *	sata_trace_debug()	<-- Wraps varargs into sata_vtrace_debug()
20948  *
20949  *	Note that the sata_trace_debug() interface was created to give
20950  *	consumers the flexibilty of sending debug messages to ring buffer
20951  *	as variable arguments.  Consumers can send type va_list debug
20952  *	messages directly to sata_vtrace_debug(). The sata_trace_debug()
20953  *	and sata_vtrace_debug() relationship is similar to that of
20954  *	cmn_err(9F) and vcmn_err(9F).
20955  *
20956  * Below is a diagram of the SATA trace ring buffer interfaces and
20957  * sample consumers:
20958  *
20959  * +---------------------------------+
20960  * |    o  o  SATA Framework Module  |
20961  * | o  SATA  o     +------------------+      +------------------+
20962  * |o   Trace  o <--|sata_vtrace_debug/|<-----|SATA HBA Driver #1|
20963  * |o   R-Buf  o    |sata_trace_debug  |<--+  +------------------+
20964  * | o        o     +------------------+   |  +------------------+
20965  * |    o  o                ^        |     +--|SATA HBA Driver #2|
20966  * |                        |        |        +------------------+
20967  * |           +------------------+  |
20968  * |           |SATA Debug Message|  |
20969  * |           +------------------+  |
20970  * +---------------------------------+
20971  *
20972  * Supporting Routines:
20973  *
20974  *	sata_trace_rbuf_alloc()	<-- Initializes ring buffer
20975  *	sata_trace_rbuf_free()	<-- Destroys ring buffer
20976  *	sata_trace_dmsg_alloc() <-- Creates or reuses buffer in ring buffer
20977  *	sata_trace_dmsg_free()	<-- Destroys content of ring buffer
20978  *
20979  * The default SATA trace ring buffer size is defined by DMSG_RING_SIZE.
20980  * The ring buffer size can be adjusted by setting dmsg_ring_size in
20981  * /etc/system to desired size in unit of bytes.
20982  *
20983  * The individual debug message size in the ring buffer is restricted
20984  * to DMSG_BUF_SIZE.
20985  */
20986 void
20987 sata_vtrace_debug(dev_info_t *dip, const char *fmt, va_list ap)
20988 {
20989 	sata_trace_dmsg_t *dmsg;
20990 
20991 	if (sata_debug_rbuf == NULL) {
20992 		return;
20993 	}
20994 
20995 	/*
20996 	 * If max size of ring buffer is smaller than size
20997 	 * required for one debug message then just return
20998 	 * since we have no room for the debug message.
20999 	 */
21000 	if (sata_debug_rbuf->maxsize < (sizeof (sata_trace_dmsg_t))) {
21001 		return;
21002 	}
21003 
21004 	mutex_enter(&sata_debug_rbuf->lock);
21005 
21006 	/* alloc or reuse on ring buffer */
21007 	dmsg = sata_trace_dmsg_alloc();
21008 
21009 	if (dmsg == NULL) {
21010 		/* resource allocation failed */
21011 		mutex_exit(&sata_debug_rbuf->lock);
21012 		return;
21013 	}
21014 
21015 	dmsg->dip = dip;
21016 	gethrestime(&dmsg->timestamp);
21017 
21018 	(void) vsnprintf(dmsg->buf, sizeof (dmsg->buf), fmt, ap);
21019 
21020 	mutex_exit(&sata_debug_rbuf->lock);
21021 }
21022 
21023 void
21024 sata_trace_debug(dev_info_t *dip, const char *fmt, ...)
21025 {
21026 	va_list ap;
21027 
21028 	va_start(ap, fmt);
21029 	sata_vtrace_debug(dip, fmt, ap);
21030 	va_end(ap);
21031 }
21032 
21033 /*
21034  * This routine is used to manage debug messages
21035  * on ring buffer.
21036  */
21037 static sata_trace_dmsg_t *
21038 sata_trace_dmsg_alloc(void)
21039 {
21040 	sata_trace_dmsg_t *dmsg_alloc, *dmsg = sata_debug_rbuf->dmsgp;
21041 
21042 	if (sata_debug_rbuf->looped == TRUE) {
21043 		sata_debug_rbuf->dmsgp = dmsg->next;
21044 		return (sata_debug_rbuf->dmsgp);
21045 	}
21046 
21047 	/*
21048 	 * If we're looping for the first time,
21049 	 * connect the ring.
21050 	 */
21051 	if (((sata_debug_rbuf->size + (sizeof (sata_trace_dmsg_t))) >
21052 	    sata_debug_rbuf->maxsize) && (sata_debug_rbuf->dmsgh != NULL)) {
21053 		dmsg->next = sata_debug_rbuf->dmsgh;
21054 		sata_debug_rbuf->dmsgp = sata_debug_rbuf->dmsgh;
21055 		sata_debug_rbuf->looped = TRUE;
21056 		return (sata_debug_rbuf->dmsgp);
21057 	}
21058 
21059 	/* If we've gotten this far then memory allocation is needed */
21060 	dmsg_alloc = kmem_zalloc(sizeof (sata_trace_dmsg_t), KM_NOSLEEP);
21061 	if (dmsg_alloc == NULL) {
21062 		sata_debug_rbuf->allocfailed++;
21063 		return (dmsg_alloc);
21064 	} else {
21065 		sata_debug_rbuf->size += sizeof (sata_trace_dmsg_t);
21066 	}
21067 
21068 	if (sata_debug_rbuf->dmsgp != NULL) {
21069 		dmsg->next = dmsg_alloc;
21070 		sata_debug_rbuf->dmsgp = dmsg->next;
21071 		return (sata_debug_rbuf->dmsgp);
21072 	} else {
21073 		/*
21074 		 * We should only be here if we're initializing
21075 		 * the ring buffer.
21076 		 */
21077 		if (sata_debug_rbuf->dmsgh == NULL) {
21078 			sata_debug_rbuf->dmsgh = dmsg_alloc;
21079 		} else {
21080 			/* Something is wrong */
21081 			kmem_free(dmsg_alloc, sizeof (sata_trace_dmsg_t));
21082 			return (NULL);
21083 		}
21084 
21085 		sata_debug_rbuf->dmsgp = dmsg_alloc;
21086 		return (sata_debug_rbuf->dmsgp);
21087 	}
21088 }
21089 
21090 
21091 /*
21092  * Free all messages on debug ring buffer.
21093  */
21094 static void
21095 sata_trace_dmsg_free(void)
21096 {
21097 	sata_trace_dmsg_t *dmsg_next, *dmsg = sata_debug_rbuf->dmsgh;
21098 
21099 	while (dmsg != NULL) {
21100 		dmsg_next = dmsg->next;
21101 		kmem_free(dmsg, sizeof (sata_trace_dmsg_t));
21102 
21103 		/*
21104 		 * If we've looped around the ring than we're done.
21105 		 */
21106 		if (dmsg_next == sata_debug_rbuf->dmsgh) {
21107 			break;
21108 		} else {
21109 			dmsg = dmsg_next;
21110 		}
21111 	}
21112 }
21113 
21114 
21115 /*
21116  * This function can block
21117  */
21118 static void
21119 sata_trace_rbuf_alloc(void)
21120 {
21121 	sata_debug_rbuf = kmem_zalloc(sizeof (sata_trace_rbuf_t), KM_SLEEP);
21122 
21123 	mutex_init(&sata_debug_rbuf->lock, NULL, MUTEX_DRIVER, NULL);
21124 
21125 	if (dmsg_ring_size > 0) {
21126 		sata_debug_rbuf->maxsize = (size_t)dmsg_ring_size;
21127 	}
21128 }
21129 
21130 
21131 static void
21132 sata_trace_rbuf_free(void)
21133 {
21134 	sata_trace_dmsg_free();
21135 	mutex_destroy(&sata_debug_rbuf->lock);
21136 	kmem_free(sata_debug_rbuf, sizeof (sata_trace_rbuf_t));
21137 }
21138 
21139 /*
21140  * If SATA_DEBUG is not defined then this routine is called instead
21141  * of sata_log() via the SATA_LOG_D macro.
21142  */
21143 static void
21144 sata_trace_log(sata_hba_inst_t *sata_hba_inst, uint_t level,
21145     const char *fmt, ...)
21146 {
21147 #ifndef __lock_lint
21148 	_NOTE(ARGUNUSED(level))
21149 #endif
21150 
21151 	dev_info_t *dip = NULL;
21152 	va_list ap;
21153 
21154 	if (sata_hba_inst != NULL) {
21155 		dip = SATA_DIP(sata_hba_inst);
21156 	}
21157 
21158 	va_start(ap, fmt);
21159 	sata_vtrace_debug(dip, fmt, ap);
21160 	va_end(ap);
21161 }
21162