xref: /illumos-gate/usr/src/uts/common/io/sata/impl/sata.c (revision 012e6ce759c490003aed29439cc47d3d73a99ad3)
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  * Copyright 2022 Racktop Systems, Inc.
30  */
31 
32 /*
33  * SATA Framework
34  * Generic SATA Host Adapter Implementation
35  */
36 
37 #include <sys/conf.h>
38 #include <sys/file.h>
39 #include <sys/ddi.h>
40 #include <sys/sunddi.h>
41 #include <sys/modctl.h>
42 #include <sys/cmn_err.h>
43 #include <sys/errno.h>
44 #include <sys/thread.h>
45 #include <sys/kstat.h>
46 #include <sys/note.h>
47 #include <sys/sysevent.h>
48 #include <sys/sysevent/eventdefs.h>
49 #include <sys/sysevent/dr.h>
50 #include <sys/taskq.h>
51 #include <sys/disp.h>
52 #include <sys/sdt.h>
53 
54 #include <sys/sata/impl/sata.h>
55 #include <sys/sata/sata_hba.h>
56 #include <sys/sata/sata_defs.h>
57 #include <sys/sata/sata_cfgadm.h>
58 #include <sys/sata/sata_blacklist.h>
59 #include <sys/sata/sata_satl.h>
60 
61 #include <sys/scsi/impl/spc3_types.h>
62 
63 /*
64  * FMA header files
65  */
66 #include <sys/ddifm.h>
67 #include <sys/fm/protocol.h>
68 #include <sys/fm/util.h>
69 #include <sys/fm/io/ddi.h>
70 
71 /* Debug flags - defined in sata.h */
72 int	sata_debug_flags = 0;
73 int	sata_msg = 0;
74 
75 /*
76  * Flags enabling selected SATA HBA framework functionality
77  */
78 #define	SATA_ENABLE_QUEUING		1
79 #define	SATA_ENABLE_NCQ			2
80 #define	SATA_ENABLE_PROCESS_EVENTS	4
81 #define	SATA_ENABLE_PMULT_FBS		8 /* FIS-Based Switching */
82 int sata_func_enable =
83 	SATA_ENABLE_PROCESS_EVENTS | SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ;
84 
85 /*
86  * Global variable setting default maximum queue depth (NCQ or TCQ)
87  * Note:minimum queue depth is 1
88  */
89 int sata_max_queue_depth = SATA_MAX_QUEUE_DEPTH; /* max NCQ/TCQ queue depth */
90 
91 /*
92  * Currently used default NCQ/TCQ queue depth. It is set-up during the driver
93  * initialization, using value from sata_max_queue_depth
94  * It is adjusted to minimum supported by the controller and by the device,
95  * if queueing is enabled.
96  */
97 static	int sata_current_max_qdepth;
98 
99 /*
100  * Global variable determining the default behavior after device hotpluggin.
101  * If non-zero, the hotplugged device is onlined (if possible) without explicit
102  * IOCTL request (AP_CONFIGURE).
103  * If zero, hotplugged device is identified, but not onlined.
104  * Enabling (AP_CONNECT) device port with an attached device does not result
105  * in device onlining regardless of the flag setting
106  */
107 int sata_auto_online = 0;
108 
109 #ifdef SATA_DEBUG
110 
111 #define	SATA_LOG_D(args)	sata_log args
112 uint64_t mbuf_count = 0;
113 uint64_t mbuffail_count = 0;
114 
115 sata_atapi_cmd_t sata_atapi_trace[64];
116 uint32_t sata_atapi_trace_index = 0;
117 int sata_atapi_trace_save = 1;
118 static	void sata_save_atapi_trace(sata_pkt_txlate_t *, int);
119 #define	SATAATAPITRACE(spx, count)	if (sata_atapi_trace_save) \
120     sata_save_atapi_trace(spx, count);
121 
122 #else
123 #define	SATA_LOG_D(args)	sata_trace_log args
124 #define	SATAATAPITRACE(spx, count)
125 #endif
126 
127 #if 0
128 static void
129 sata_test_atapi_packet_command(sata_hba_inst_t *, int);
130 #endif
131 
132 #ifdef SATA_INJECT_FAULTS
133 
134 #define		SATA_INJECT_PKT_FAULT	1
135 uint32_t	sata_inject_fault = 0;
136 
137 uint32_t	sata_inject_fault_count = 0;
138 uint32_t	sata_inject_fault_pause_count = 0;
139 uint32_t	sata_fault_type = 0;
140 uint32_t	sata_fault_cmd = 0;
141 dev_info_t	*sata_fault_ctrl = NULL;
142 sata_device_t	sata_fault_device;
143 
144 static	void sata_inject_pkt_fault(sata_pkt_t *, int *, int);
145 
146 #endif
147 
148 #define	LEGACY_HWID_LEN	64	/* Model (40) + Serial (20) + pad */
149 
150 static char sata_rev_tag[] = {"1.46"};
151 
152 /*
153  * SATA cb_ops functions
154  */
155 static	int sata_hba_open(dev_t *, int, int, cred_t *);
156 static	int sata_hba_close(dev_t, int, int, cred_t *);
157 static	int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *,	int *);
158 
159 /*
160  * SCSA required entry points
161  */
162 static	int sata_scsi_tgt_init(dev_info_t *, dev_info_t *,
163     scsi_hba_tran_t *, struct scsi_device *);
164 static	int sata_scsi_tgt_probe(struct scsi_device *,
165     int (*callback)(void));
166 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *,
167     scsi_hba_tran_t *, struct scsi_device *);
168 static	int sata_scsi_start(struct scsi_address *, struct scsi_pkt *);
169 static	int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *);
170 static	int sata_scsi_reset(struct scsi_address *, int);
171 static	int sata_scsi_getcap(struct scsi_address *, char *, int);
172 static	int sata_scsi_setcap(struct scsi_address *, char *, int, int);
173 static	struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *,
174     struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t),
175     caddr_t);
176 static	void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *);
177 static	void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *);
178 static	void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *);
179 
180 /*
181  * SATA HBA interface functions are defined in sata_hba.h header file
182  */
183 
184 /* Event processing functions */
185 static	void sata_event_daemon(void *);
186 static	void sata_event_thread_control(int);
187 static	void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst);
188 static	void sata_process_pmult_events(sata_hba_inst_t *, uint8_t);
189 static	void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *);
190 static	void sata_process_pmdevice_reset(sata_hba_inst_t *, sata_address_t *);
191 static	void sata_process_port_failed_event(sata_hba_inst_t *,
192     sata_address_t *);
193 static	void sata_process_port_link_events(sata_hba_inst_t *,
194     sata_address_t *);
195 static	void sata_process_pmport_link_events(sata_hba_inst_t *,
196     sata_address_t *);
197 static	void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *);
198 static	void sata_process_pmdevice_detached(sata_hba_inst_t *,
199     sata_address_t *);
200 static	void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *);
201 static	void sata_process_pmdevice_attached(sata_hba_inst_t *,
202     sata_address_t *);
203 static	void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *);
204 static	void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *);
205 static	void sata_process_target_node_cleanup(sata_hba_inst_t *,
206     sata_address_t *);
207 static	void sata_process_device_autoonline(sata_hba_inst_t *,
208     sata_address_t *saddr);
209 
210 /*
211  * Local translation functions
212  */
213 static	int sata_txlt_inquiry(sata_pkt_txlate_t *);
214 static	int sata_txlt_test_unit_ready(sata_pkt_txlate_t *);
215 static	int sata_txlt_start_stop_unit(sata_pkt_txlate_t *);
216 static	int sata_txlt_read_capacity(sata_pkt_txlate_t *);
217 static	int sata_txlt_read_capacity16(sata_pkt_txlate_t *);
218 static  int sata_txlt_unmap(sata_pkt_txlate_t *);
219 static	int sata_txlt_request_sense(sata_pkt_txlate_t *);
220 static	int sata_txlt_read(sata_pkt_txlate_t *);
221 static	int sata_txlt_write(sata_pkt_txlate_t *);
222 static	int sata_txlt_log_sense(sata_pkt_txlate_t *);
223 static	int sata_txlt_log_select(sata_pkt_txlate_t *);
224 static	int sata_txlt_mode_sense(sata_pkt_txlate_t *);
225 static	int sata_txlt_mode_select(sata_pkt_txlate_t *);
226 static	int sata_txlt_ata_pass_thru(sata_pkt_txlate_t *);
227 static	int sata_txlt_synchronize_cache(sata_pkt_txlate_t *);
228 static	int sata_txlt_write_buffer(sata_pkt_txlate_t *);
229 static	int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *);
230 
231 static	int sata_hba_start(sata_pkt_txlate_t *, int *);
232 static	int sata_txlt_invalid_command(sata_pkt_txlate_t *);
233 static	int sata_txlt_check_condition(sata_pkt_txlate_t *, uchar_t, uchar_t);
234 static	int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *);
235 static	int sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *);
236 static  int sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *);
237 static	void sata_txlt_rw_completion(sata_pkt_t *);
238 static	void sata_txlt_nodata_cmd_completion(sata_pkt_t *);
239 static	void sata_txlt_apt_completion(sata_pkt_t *sata_pkt);
240 static	void sata_txlt_unmap_completion(sata_pkt_t *sata_pkt);
241 static	void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *);
242 static	int sata_emul_rw_completion(sata_pkt_txlate_t *);
243 static	void sata_fill_ata_return_desc(sata_pkt_t *, uint8_t, uint8_t,
244     uint8_t);
245 static	struct scsi_extended_sense *sata_immediate_error_response(
246     sata_pkt_txlate_t *, int);
247 static	struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *);
248 
249 static	int sata_txlt_atapi(sata_pkt_txlate_t *);
250 static	void sata_txlt_atapi_completion(sata_pkt_t *);
251 
252 /*
253  * Local functions for ioctl
254  */
255 static	int32_t sata_get_port_num(sata_hba_inst_t *,  struct devctl_iocdata *);
256 static	void sata_cfgadm_state(sata_hba_inst_t *, int32_t,
257     devctl_ap_state_t *);
258 static	dev_info_t *sata_get_target_dip(dev_info_t *, uint8_t, uint8_t);
259 static	dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *);
260 static	dev_info_t *sata_devt_to_devinfo(dev_t);
261 static	int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *);
262 static	int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *);
263 static	int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *);
264 static	int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *);
265 static	int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *);
266 static	int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *);
267 static	int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *);
268 static	int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *);
269 static	int sata_ioctl_reset_all(sata_hba_inst_t *);
270 static	int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *);
271 static	int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *,
272     sata_ioctl_data_t *, int mode);
273 static	int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *,
274     sata_ioctl_data_t *, int mode);
275 static	int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *,
276     sata_ioctl_data_t *, int mode);
277 static	int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *,
278     sata_ioctl_data_t *, int mode);
279 static	int sata_ioctl_get_serialnumber_info(sata_hba_inst_t *,
280     sata_device_t *, sata_ioctl_data_t *, int mode);
281 
282 /*
283  * Local functions
284  */
285 static	void sata_remove_hba_instance(dev_info_t *);
286 static	int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *);
287 static	void sata_probe_ports(sata_hba_inst_t *);
288 static	void sata_probe_pmports(sata_hba_inst_t *, uint8_t);
289 static	int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int);
290 static	int sata_reprobe_pmult(sata_hba_inst_t *, sata_device_t *, int);
291 static	int sata_reprobe_pmport(sata_hba_inst_t *, sata_device_t *, int);
292 static	int sata_alloc_pmult(sata_hba_inst_t *, sata_device_t *);
293 static	void sata_free_pmult(sata_hba_inst_t *, sata_device_t *);
294 static	int sata_add_device(dev_info_t *, sata_hba_inst_t *, sata_device_t *);
295 static	int sata_offline_device(sata_hba_inst_t *, sata_device_t *,
296     sata_drive_info_t *);
297 static	dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *,
298     sata_address_t *);
299 static	void sata_remove_target_node(sata_hba_inst_t *,
300     sata_address_t *);
301 static	int sata_validate_scsi_address(sata_hba_inst_t *,
302     struct scsi_address *, sata_device_t *);
303 static	int sata_validate_sata_address(sata_hba_inst_t *, int, int, int);
304 static	sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t));
305 static	void sata_pkt_free(sata_pkt_txlate_t *);
306 static	int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t),
307     caddr_t, ddi_dma_attr_t *);
308 static	void sata_common_free_dma_rsrcs(sata_pkt_txlate_t *);
309 static	int sata_probe_device(sata_hba_inst_t *, sata_device_t *);
310 static	sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *,
311     sata_device_t *);
312 static	int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *);
313 static	void sata_reidentify_device(sata_pkt_txlate_t *);
314 static	struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int);
315 static	void sata_free_local_buffer(sata_pkt_txlate_t *);
316 static	uint64_t sata_check_capacity(sata_drive_info_t *);
317 void	sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *,
318     ddi_dma_attr_t *);
319 static	int sata_fetch_device_identify_data(sata_hba_inst_t *,
320     sata_drive_info_t *);
321 static	void sata_update_port_info(sata_hba_inst_t *, sata_device_t *);
322 static	void sata_update_pmport_info(sata_hba_inst_t *, sata_device_t *);
323 static	void sata_update_port_scr(sata_port_scr_t *, sata_device_t *);
324 static	int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *);
325 static	int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int);
326 static	int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int);
327 static	int sata_set_drive_features(sata_hba_inst_t *,
328     sata_drive_info_t *, int flag);
329 static	void sata_init_write_cache_mode(sata_drive_info_t *sdinfo);
330 static	int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *);
331 static	void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *,
332     uint8_t *);
333 static	int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *,
334     struct scsi_inquiry *);
335 static	int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *);
336 static	int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *);
337 static	int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *);
338 static	int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *);
339 static	int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *);
340 static	int sata_mode_select_page_8(sata_pkt_txlate_t *,
341     struct mode_cache_scsi3 *, int, int *, int *, int *);
342 static	int sata_mode_select_page_1a(sata_pkt_txlate_t *,
343     struct mode_info_power_cond *, int, int *, int *, int *);
344 static	int sata_mode_select_page_1c(sata_pkt_txlate_t *,
345     struct mode_info_excpt_page *, int, int *, int *, int *);
346 static	int sata_mode_select_page_30(sata_pkt_txlate_t *,
347     struct mode_acoustic_management *, int, int *, int *, int *);
348 
349 static	int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *);
350 static	int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *,
351     sata_hba_inst_t *);
352 static	int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *,
353     sata_hba_inst_t *);
354 static	int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *,
355     sata_hba_inst_t *);
356 static	int sata_build_lsense_page_0e(sata_drive_info_t *, uint8_t *,
357     sata_pkt_txlate_t *);
358 
359 static	void sata_set_arq_data(sata_pkt_t *);
360 static	void sata_build_read_verify_cmd(sata_cmd_t *, uint16_t, uint64_t);
361 static	void sata_build_generic_cmd(sata_cmd_t *, uint8_t);
362 static	uint8_t sata_get_standby_timer(uint8_t *timer);
363 
364 static	void sata_save_drive_settings(sata_drive_info_t *);
365 static	void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *);
366 static	void sata_show_pmult_info(sata_hba_inst_t *, sata_device_t *);
367 static	void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...);
368 static	void sata_trace_log(sata_hba_inst_t *, uint_t, const char *fmt, ...);
369 static	int sata_fetch_smart_return_status(sata_hba_inst_t *,
370     sata_drive_info_t *);
371 static	int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *,
372     struct smart_data *);
373 static	int sata_smart_selftest_log(sata_hba_inst_t *,
374     sata_drive_info_t *,
375     struct smart_selftest_log *);
376 static	int sata_ext_smart_selftest_read_log(sata_hba_inst_t *,
377     sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t);
378 static	int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *,
379     uint8_t *, uint8_t, uint8_t);
380 static	int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *,
381     struct read_log_ext_directory *);
382 static	void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int);
383 static	void sata_xlate_errors(sata_pkt_txlate_t *);
384 static	void sata_decode_device_error(sata_pkt_txlate_t *,
385     struct scsi_extended_sense *);
386 static	void sata_set_device_removed(dev_info_t *);
387 static	boolean_t sata_check_device_removed(dev_info_t *);
388 static	void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *);
389 static	int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *,
390     sata_drive_info_t *);
391 static	int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *,
392     sata_drive_info_t *);
393 static	void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *);
394 static	void sata_fixed_sense_data_preset(struct scsi_extended_sense *);
395 static  void sata_target_devid_register(dev_info_t *, sata_drive_info_t *);
396 static  int sata_check_modser(char *, int);
397 
398 /*
399  * FMA
400  */
401 static boolean_t sata_check_for_dma_error(dev_info_t *, sata_pkt_txlate_t *);
402 
403 
404 /*
405  * SATA Framework will ignore SATA HBA driver cb_ops structure and
406  * register following one with SCSA framework.
407  * Open & close are provided, so scsi framework will not use its own
408  */
409 static struct cb_ops sata_cb_ops = {
410 	sata_hba_open,			/* open */
411 	sata_hba_close,			/* close */
412 	nodev,				/* strategy */
413 	nodev,				/* print */
414 	nodev,				/* dump */
415 	nodev,				/* read */
416 	nodev,				/* write */
417 	sata_hba_ioctl,			/* ioctl */
418 	nodev,				/* devmap */
419 	nodev,				/* mmap */
420 	nodev,				/* segmap */
421 	nochpoll,			/* chpoll */
422 	ddi_prop_op,			/* cb_prop_op */
423 	0,				/* streamtab */
424 	D_NEW | D_MP,			/* cb_flag */
425 	CB_REV,				/* rev */
426 	nodev,				/* aread */
427 	nodev				/* awrite */
428 };
429 
430 
431 extern struct mod_ops mod_miscops;
432 extern uchar_t	scsi_cdb_size[];
433 
434 static struct modlmisc modlmisc = {
435 	&mod_miscops,			/* Type of module */
436 	"SATA Module"			/* module name */
437 };
438 
439 
440 static struct modlinkage modlinkage = {
441 	MODREV_1,
442 	(void *)&modlmisc,
443 	NULL
444 };
445 
446 /*
447  * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero,
448  * i.e. when scsi_pkt has not timeout specified.
449  */
450 static int sata_default_pkt_time = 60;	/* 60 seconds */
451 
452 /*
453  * Intermediate buffer device access attributes - they are required,
454  * but not necessarily used.
455  */
456 static ddi_device_acc_attr_t sata_acc_attr = {
457 	DDI_DEVICE_ATTR_V0,
458 	DDI_STRUCTURE_LE_ACC,
459 	DDI_STRICTORDER_ACC
460 };
461 
462 
463 /*
464  * Mutexes protecting structures in multithreaded operations.
465  * Because events are relatively rare, a single global mutex protecting
466  * data structures should be sufficient. To increase performance, add
467  * separate mutex per each sata port and use global mutex only to protect
468  * common data structures.
469  */
470 static	kmutex_t sata_mutex;		/* protects sata_hba_list */
471 static	kmutex_t sata_log_mutex;	/* protects log */
472 
473 static	char sata_log_buf[256];
474 
475 /*
476  * sata trace debug
477  */
478 static	sata_trace_rbuf_t *sata_debug_rbuf;
479 static	sata_trace_dmsg_t *sata_trace_dmsg_alloc(void);
480 static	void sata_trace_dmsg_free(void);
481 static	void sata_trace_rbuf_alloc(void);
482 static	void sata_trace_rbuf_free(void);
483 
484 int	dmsg_ring_size = DMSG_RING_SIZE;
485 
486 /* Default write cache setting for SATA hard disks */
487 int	sata_write_cache = 1;		/* enabled */
488 
489 /* Default write cache setting for SATA ATAPI CD/DVD */
490 int	sata_atapicdvd_write_cache = 1; /* enabled */
491 
492 /* Default write cache setting for SATA ATAPI tape */
493 int	sata_atapitape_write_cache = 1; /* enabled */
494 
495 /* Default write cache setting for SATA ATAPI disk */
496 int	sata_atapidisk_write_cache = 1;	/* enabled */
497 
498 /*
499  * Linked list of HBA instances
500  */
501 static	sata_hba_inst_t *sata_hba_list = NULL;
502 static	sata_hba_inst_t *sata_hba_list_tail = NULL;
503 /*
504  * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran
505  * structure and in sata soft state.
506  */
507 
508 /*
509  * Event daemon related variables
510  */
511 static	kmutex_t sata_event_mutex;
512 static	kcondvar_t sata_event_cv;
513 static	kthread_t *sata_event_thread = NULL;
514 static	int sata_event_thread_terminate = 0;
515 static	int sata_event_pending = 0;
516 static	int sata_event_thread_active = 0;
517 extern	pri_t minclsyspri;
518 
519 /*
520  * NCQ error recovery command
521  */
522 static const sata_cmd_t sata_rle_cmd = {
523 	SATA_CMD_REV,
524 	NULL,
525 	{
526 		SATA_DIR_READ
527 	},
528 	ATA_ADDR_LBA48,
529 	0,
530 	0,
531 	0,
532 	0,
533 	0,
534 	1,
535 	READ_LOG_EXT_NCQ_ERROR_RECOVERY,
536 	0,
537 	0,
538 	0,
539 	SATAC_READ_LOG_EXT,
540 	0,
541 	0,
542 	0,
543 };
544 
545 /*
546  * ATAPI error recovery CDB
547  */
548 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = {
549 	SCMD_REQUEST_SENSE,
550 	0,			/* Only fixed RQ format is supported */
551 	0,
552 	0,
553 	SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */
554 	0
555 };
556 
557 
558 /* Warlock directives */
559 
560 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran))
561 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device))
562 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops))
563 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense))
564 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status))
565 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr))
566 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t))
567 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state))
568 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state))
569 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list))
570 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list))
571 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next))
572 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev))
573 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \
574     sata_hba_inst::satahba_scsi_tran))
575 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran))
576 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip))
577 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached))
578 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port))
579 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex,
580     sata_hba_inst::satahba_event_flags))
581 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
582     sata_cport_info::cport_devp))
583 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp))
584 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr))
585 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
586     sata_cport_info::cport_dev_type))
587 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type))
588 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
589     sata_cport_info::cport_state))
590 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state))
591 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
592     sata_pmport_info::pmport_state))
593 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state))
594 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
595     sata_pmport_info::pmport_dev_type))
596 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type))
597 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
598     sata_pmport_info::pmport_sata_drive))
599 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
600     sata_pmport_info::pmport_tgtnode_clean))
601 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
602     sata_pmport_info::pmport_event_flags))
603 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive))
604 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port))
605 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports))
606 #ifdef SATA_DEBUG
607 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count))
608 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count))
609 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace))
610 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index))
611 #endif
612 
613 /* End of warlock directives */
614 
615 /* ************** loadable module configuration functions ************** */
616 
617 int
618 _init()
619 {
620 	int rval;
621 
622 	mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL);
623 	mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL);
624 	mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL);
625 	cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL);
626 	sata_trace_rbuf_alloc();
627 	if ((rval = mod_install(&modlinkage)) != 0) {
628 #ifdef SATA_DEBUG
629 		cmn_err(CE_WARN, "sata: _init: mod_install failed\n");
630 #endif
631 		sata_trace_rbuf_free();
632 		mutex_destroy(&sata_log_mutex);
633 		cv_destroy(&sata_event_cv);
634 		mutex_destroy(&sata_event_mutex);
635 		mutex_destroy(&sata_mutex);
636 	}
637 	return (rval);
638 }
639 
640 int
641 _fini()
642 {
643 	int rval;
644 
645 	if ((rval = mod_remove(&modlinkage)) != 0)
646 		return (rval);
647 
648 	sata_trace_rbuf_free();
649 	mutex_destroy(&sata_log_mutex);
650 	cv_destroy(&sata_event_cv);
651 	mutex_destroy(&sata_event_mutex);
652 	mutex_destroy(&sata_mutex);
653 	return (rval);
654 }
655 
656 int
657 _info(struct modinfo *modinfop)
658 {
659 	return (mod_info(&modlinkage, modinfop));
660 }
661 
662 
663 
664 /* ********************* SATA HBA entry points ********************* */
665 
666 
667 /*
668  * Called by SATA HBA from _init().
669  * Registers HBA driver instance/sata framework pair with scsi framework, by
670  * calling scsi_hba_init().
671  *
672  * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used
673  * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver
674  * cb_ops pointer in SATA HBA driver dev_ops structure.
675  * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors.
676  *
677  * Return status of the scsi_hba_init() is returned to a calling SATA HBA
678  * driver.
679  */
680 int
681 sata_hba_init(struct modlinkage *modlp)
682 {
683 	int rval;
684 	struct dev_ops *hba_ops;
685 
686 	SATADBG1(SATA_DBG_HBA_IF, NULL,
687 	    "sata_hba_init: name %s \n",
688 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
689 	/*
690 	 * Fill-up cb_ops and dev_ops when necessary
691 	 */
692 	hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
693 	/*
694 	 * Provide pointer to SATA dev_ops
695 	 */
696 	hba_ops->devo_cb_ops = &sata_cb_ops;
697 
698 	/*
699 	 * Register SATA HBA with SCSI framework
700 	 */
701 	if ((rval = scsi_hba_init(modlp)) != 0) {
702 		SATADBG1(SATA_DBG_HBA_IF, NULL,
703 		    "sata_hba_init: scsi hba init failed\n", NULL);
704 		return (rval);
705 	}
706 
707 	return (0);
708 }
709 
710 
711 /* HBA attach stages */
712 #define	HBA_ATTACH_STAGE_SATA_HBA_INST	1
713 #define	HBA_ATTACH_STAGE_SCSI_ATTACHED	2
714 #define	HBA_ATTACH_STAGE_SETUP		4
715 #define	HBA_ATTACH_STAGE_LINKED		8
716 
717 
718 /*
719  *
720  * Called from SATA HBA driver's attach routine to attach an instance of
721  * the HBA.
722  *
723  * For DDI_ATTACH command:
724  * sata_hba_inst structure is allocated here and initialized with pointers to
725  * SATA framework implementation of required scsi tran functions.
726  * The scsi_tran's tran_hba_private field is used by SATA Framework to point
727  * to the soft structure (sata_hba_inst) allocated by SATA framework for
728  * SATA HBA instance related data.
729  * The scsi_tran's tran_hba_private field is used by SATA framework to
730  * store a pointer to per-HBA-instance of sata_hba_inst structure.
731  * The sata_hba_inst structure is cross-linked to scsi tran structure.
732  * Among other info, a pointer to sata_hba_tran structure is stored in
733  * sata_hba_inst. The sata_hba_inst structures for different HBA instances are
734  * linked together into the list, pointed to by sata_hba_list.
735  * On the first HBA instance attach the sata event thread is initialized.
736  * Attachment points are created for all SATA ports of the HBA being attached.
737  * All HBA instance's SATA ports are probed and type of plugged devices is
738  * determined. For each device of a supported type, a target node is created.
739  *
740  * DDI_SUCCESS is returned when attachment process is successful,
741  * DDI_FAILURE is returned otherwise.
742  *
743  * For DDI_RESUME command:
744  * Not implemented at this time (postponed until phase 2 of the development).
745  */
746 int
747 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran,
748     ddi_attach_cmd_t cmd)
749 {
750 	sata_hba_inst_t	*sata_hba_inst;
751 	scsi_hba_tran_t *scsi_tran = NULL;
752 	int hba_attach_state = 0;
753 	char taskq_name[MAXPATHLEN];
754 
755 	SATADBG3(SATA_DBG_HBA_IF, NULL,
756 	    "sata_hba_attach: node %s (%s%d)\n",
757 	    ddi_node_name(dip), ddi_driver_name(dip),
758 	    ddi_get_instance(dip));
759 
760 	if (cmd == DDI_RESUME) {
761 		/*
762 		 * Postponed until phase 2 of the development
763 		 */
764 		return (DDI_FAILURE);
765 	}
766 
767 	if (cmd != DDI_ATTACH) {
768 		return (DDI_FAILURE);
769 	}
770 
771 	/* cmd == DDI_ATTACH */
772 
773 	if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) {
774 		SATA_LOG_D((NULL, CE_WARN,
775 		    "sata_hba_attach: invalid sata_hba_tran"));
776 		return (DDI_FAILURE);
777 	}
778 	/*
779 	 * Allocate and initialize SCSI tran structure.
780 	 * SATA copy of tran_bus_config is provided to create port nodes.
781 	 */
782 	scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
783 	if (scsi_tran == NULL)
784 		return (DDI_FAILURE);
785 	/*
786 	 * Allocate soft structure for SATA HBA instance.
787 	 * There is a separate softstate for each HBA instance.
788 	 */
789 	sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP);
790 	ASSERT(sata_hba_inst != NULL); /* this should not fail */
791 	mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL);
792 	hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST;
793 
794 	/*
795 	 * scsi_trans's tran_hba_private is used by SATA Framework to point to
796 	 * soft structure allocated by SATA framework for
797 	 * SATA HBA instance related data.
798 	 */
799 	scsi_tran->tran_hba_private	= sata_hba_inst;
800 	scsi_tran->tran_tgt_private	= NULL;
801 
802 	scsi_tran->tran_tgt_init	= sata_scsi_tgt_init;
803 	scsi_tran->tran_tgt_probe	= sata_scsi_tgt_probe;
804 	scsi_tran->tran_tgt_free	= sata_scsi_tgt_free;
805 
806 	scsi_tran->tran_start		= sata_scsi_start;
807 	scsi_tran->tran_reset		= sata_scsi_reset;
808 	scsi_tran->tran_abort		= sata_scsi_abort;
809 	scsi_tran->tran_getcap		= sata_scsi_getcap;
810 	scsi_tran->tran_setcap		= sata_scsi_setcap;
811 	scsi_tran->tran_init_pkt	= sata_scsi_init_pkt;
812 	scsi_tran->tran_destroy_pkt	= sata_scsi_destroy_pkt;
813 
814 	scsi_tran->tran_dmafree		= sata_scsi_dmafree;
815 	scsi_tran->tran_sync_pkt	= sata_scsi_sync_pkt;
816 
817 	scsi_tran->tran_reset_notify	= NULL;
818 	scsi_tran->tran_get_bus_addr	= NULL;
819 	scsi_tran->tran_quiesce		= NULL;
820 	scsi_tran->tran_unquiesce	= NULL;
821 	scsi_tran->tran_bus_reset	= NULL;
822 
823 	if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr,
824 	    scsi_tran, 0) != DDI_SUCCESS) {
825 #ifdef SATA_DEBUG
826 		cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed",
827 		    ddi_driver_name(dip), ddi_get_instance(dip));
828 #endif
829 		goto fail;
830 	}
831 	hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED;
832 
833 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) {
834 		if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
835 		    "sata", 1) != DDI_PROP_SUCCESS) {
836 			SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: "
837 			    "failed to create hba sata prop"));
838 			goto fail;
839 		}
840 	}
841 
842 	/*
843 	 * Save pointers in hba instance soft state.
844 	 */
845 	sata_hba_inst->satahba_scsi_tran = scsi_tran;
846 	sata_hba_inst->satahba_tran = sata_tran;
847 	sata_hba_inst->satahba_dip = dip;
848 
849 	/*
850 	 * Create a task queue to handle emulated commands completion
851 	 * Use node name, dash, instance number as the queue name.
852 	 */
853 	taskq_name[0] = '\0';
854 	(void) strlcat(taskq_name, DEVI(dip)->devi_node_name,
855 	    sizeof (taskq_name));
856 	(void) snprintf(taskq_name + strlen(taskq_name),
857 	    sizeof (taskq_name) - strlen(taskq_name),
858 	    "-%d", DEVI(dip)->devi_instance);
859 	sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1,
860 	    minclsyspri, 1, sata_tran->sata_tran_hba_num_cports * 4,
861 	    TASKQ_DYNAMIC);
862 
863 	hba_attach_state |= HBA_ATTACH_STAGE_SETUP;
864 
865 	/*
866 	 * Create events thread if not created yet.
867 	 */
868 	sata_event_thread_control(1);
869 
870 	/*
871 	 * Link this hba instance into the list.
872 	 */
873 	mutex_enter(&sata_mutex);
874 
875 	if (sata_hba_list == NULL) {
876 		/*
877 		 * The first instance of HBA is attached.
878 		 * Set current/active default maximum NCQ/TCQ queue depth for
879 		 * all SATA devices. It is done here and now, to eliminate the
880 		 * possibility of the dynamic, programatic modification of the
881 		 * queue depth via global (and public) sata_max_queue_depth
882 		 * variable (this would require special handling in HBA drivers)
883 		 */
884 		sata_current_max_qdepth = sata_max_queue_depth;
885 		if (sata_current_max_qdepth > 32)
886 			sata_current_max_qdepth = 32;
887 		else if (sata_current_max_qdepth < 1)
888 			sata_current_max_qdepth = 1;
889 	}
890 
891 	sata_hba_inst->satahba_next = NULL;
892 	sata_hba_inst->satahba_prev = sata_hba_list_tail;
893 	if (sata_hba_list == NULL) {
894 		sata_hba_list = sata_hba_inst;
895 	}
896 	if (sata_hba_list_tail != NULL) {
897 		sata_hba_list_tail->satahba_next = sata_hba_inst;
898 	}
899 	sata_hba_list_tail = sata_hba_inst;
900 	mutex_exit(&sata_mutex);
901 	hba_attach_state |= HBA_ATTACH_STAGE_LINKED;
902 
903 	/*
904 	 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl
905 	 * SATA HBA driver should not use its own open/close entry points.
906 	 *
907 	 * Make sure that instance number doesn't overflow
908 	 * when forming minor numbers.
909 	 */
910 	ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT));
911 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
912 	    INST2DEVCTL(ddi_get_instance(dip)),
913 	    DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) {
914 #ifdef SATA_DEBUG
915 		cmn_err(CE_WARN, "sata_hba_attach: "
916 		    "cannot create devctl minor node");
917 #endif
918 		goto fail;
919 	}
920 
921 
922 	/*
923 	 * Set-up kstats here, if necessary.
924 	 * (postponed until future phase of the development).
925 	 */
926 
927 	/*
928 	 * Indicate that HBA is attached. This will enable events processing
929 	 * for this HBA.
930 	 */
931 	sata_hba_inst->satahba_attached = 1;
932 	/*
933 	 * Probe controller ports. This operation will describe a current
934 	 * controller/port/multipliers/device configuration and will create
935 	 * attachment points.
936 	 * We may end-up with just a controller with no devices attached.
937 	 * For the ports with a supported device attached, device target nodes
938 	 * are created and devices are initialized.
939 	 */
940 	sata_probe_ports(sata_hba_inst);
941 
942 	return (DDI_SUCCESS);
943 
944 fail:
945 	if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) {
946 		(void) sata_remove_hba_instance(dip);
947 		if (sata_hba_list == NULL)
948 			sata_event_thread_control(0);
949 	}
950 
951 	if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) {
952 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
953 		taskq_destroy(sata_hba_inst->satahba_taskq);
954 	}
955 
956 	if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED)
957 		(void) scsi_hba_detach(dip);
958 
959 	if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) {
960 		mutex_destroy(&sata_hba_inst->satahba_mutex);
961 		kmem_free((void *)sata_hba_inst,
962 		    sizeof (struct sata_hba_inst));
963 		scsi_hba_tran_free(scsi_tran);
964 	}
965 
966 	sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed",
967 	    ddi_driver_name(dip), ddi_get_instance(dip));
968 
969 	return (DDI_FAILURE);
970 }
971 
972 
973 /*
974  * Called by SATA HBA from to detach an instance of the driver.
975  *
976  * For DDI_DETACH command:
977  * Free local structures allocated for SATA HBA instance during
978  * sata_hba_attach processing.
979  *
980  * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise.
981  *
982  * For DDI_SUSPEND command:
983  * Not implemented at this time (postponed until phase 2 of the development)
984  * Returnd DDI_SUCCESS.
985  *
986  * When the last HBA instance is detached, the event daemon is terminated.
987  *
988  * NOTE: Port multiplier is supported.
989  */
990 int
991 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
992 {
993 	dev_info_t	*tdip;
994 	sata_hba_inst_t	*sata_hba_inst;
995 	scsi_hba_tran_t *scsi_hba_tran;
996 	sata_cport_info_t *cportinfo;
997 	sata_pmult_info_t *pminfo;
998 	sata_drive_info_t *sdinfo;
999 	sata_device_t	sdevice;
1000 	int ncport, npmport;
1001 
1002 	SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n",
1003 	    ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip));
1004 
1005 	switch (cmd) {
1006 	case DDI_DETACH:
1007 
1008 		if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1009 			return (DDI_FAILURE);
1010 
1011 		sata_hba_inst = scsi_hba_tran->tran_hba_private;
1012 		if (sata_hba_inst == NULL)
1013 			return (DDI_FAILURE);
1014 
1015 		if (scsi_hba_detach(dip) == DDI_FAILURE) {
1016 			sata_hba_inst->satahba_attached = 1;
1017 			return (DDI_FAILURE);
1018 		}
1019 
1020 		/*
1021 		 * Free all target nodes - at this point
1022 		 * devices should be at least offlined
1023 		 * otherwise scsi_hba_detach() should not be called.
1024 		 */
1025 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
1026 		    ncport++) {
1027 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
1028 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
1029 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
1030 				if (sdinfo != NULL) {
1031 					tdip = sata_get_target_dip(dip,
1032 					    ncport, 0);
1033 					if (tdip != NULL) {
1034 						if (ndi_devi_offline(tdip,
1035 						    NDI_DEVI_REMOVE) !=
1036 						    NDI_SUCCESS) {
1037 							SATA_LOG_D((
1038 							    sata_hba_inst,
1039 							    CE_WARN,
1040 							    "sata_hba_detach: "
1041 							    "Target node not "
1042 							    "removed !"));
1043 							return (DDI_FAILURE);
1044 						}
1045 					}
1046 				}
1047 			} else { /* SATA_DTYPE_PMULT */
1048 				mutex_enter(&cportinfo->cport_mutex);
1049 				pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
1050 
1051 				if (pminfo == NULL) {
1052 					SATA_LOG_D((sata_hba_inst, CE_WARN,
1053 					    "sata_hba_detach: Port multiplier "
1054 					    "not ready yet!"));
1055 					mutex_exit(&cportinfo->cport_mutex);
1056 					return (DDI_FAILURE);
1057 				}
1058 
1059 				/*
1060 				 * Detach would fail if removal of any of the
1061 				 * target nodes is failed - albeit in that
1062 				 * case some of them may have been removed.
1063 				 */
1064 				for (npmport = 0; npmport < SATA_NUM_PMPORTS(
1065 				    sata_hba_inst, ncport); npmport++) {
1066 					tdip = sata_get_target_dip(dip, ncport,
1067 					    npmport);
1068 					if (tdip != NULL) {
1069 						if (ndi_devi_offline(tdip,
1070 						    NDI_DEVI_REMOVE) !=
1071 						    NDI_SUCCESS) {
1072 							SATA_LOG_D((
1073 							    sata_hba_inst,
1074 							    CE_WARN,
1075 							    "sata_hba_detach: "
1076 							    "Target node not "
1077 							    "removed !"));
1078 							mutex_exit(&cportinfo->
1079 							    cport_mutex);
1080 							return (DDI_FAILURE);
1081 						}
1082 					}
1083 				}
1084 				mutex_exit(&cportinfo->cport_mutex);
1085 			}
1086 		}
1087 		/*
1088 		 * Disable sata event daemon processing for this HBA
1089 		 */
1090 		sata_hba_inst->satahba_attached = 0;
1091 
1092 		/*
1093 		 * Remove event daemon thread, if it is last HBA instance.
1094 		 */
1095 
1096 		mutex_enter(&sata_mutex);
1097 		if (sata_hba_list->satahba_next == NULL) {
1098 			mutex_exit(&sata_mutex);
1099 			sata_event_thread_control(0);
1100 			mutex_enter(&sata_mutex);
1101 		}
1102 		mutex_exit(&sata_mutex);
1103 
1104 		/* Remove this HBA instance from the HBA list */
1105 		sata_remove_hba_instance(dip);
1106 
1107 		/*
1108 		 * At this point there should be no target nodes attached.
1109 		 * Detach and destroy device and port info structures.
1110 		 */
1111 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
1112 		    ncport++) {
1113 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
1114 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
1115 				sdinfo =
1116 				    cportinfo->cport_devp.cport_sata_drive;
1117 				if (sdinfo != NULL) {
1118 					/* Release device structure */
1119 					kmem_free(sdinfo,
1120 					    sizeof (sata_drive_info_t));
1121 				}
1122 				/* Release cport info */
1123 				mutex_destroy(&cportinfo->cport_mutex);
1124 				kmem_free(cportinfo,
1125 				    sizeof (sata_cport_info_t));
1126 			} else { /* SATA_DTYPE_PMULT */
1127 				sdevice.satadev_addr.cport = (uint8_t)ncport;
1128 				sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
1129 				sata_free_pmult(sata_hba_inst, &sdevice);
1130 			}
1131 		}
1132 
1133 		scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran);
1134 
1135 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
1136 
1137 		taskq_destroy(sata_hba_inst->satahba_taskq);
1138 
1139 		mutex_destroy(&sata_hba_inst->satahba_mutex);
1140 		kmem_free((void *)sata_hba_inst,
1141 		    sizeof (struct sata_hba_inst));
1142 
1143 		return (DDI_SUCCESS);
1144 
1145 	case DDI_SUSPEND:
1146 		/*
1147 		 * Postponed until phase 2
1148 		 */
1149 		return (DDI_FAILURE);
1150 
1151 	default:
1152 		return (DDI_FAILURE);
1153 	}
1154 }
1155 
1156 
1157 /*
1158  * Called by an HBA drive from _fini() routine.
1159  * Unregisters SATA HBA instance/SATA framework pair from the scsi framework.
1160  */
1161 void
1162 sata_hba_fini(struct modlinkage *modlp)
1163 {
1164 	SATADBG1(SATA_DBG_HBA_IF, NULL,
1165 	    "sata_hba_fini: name %s\n",
1166 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
1167 
1168 	scsi_hba_fini(modlp);
1169 }
1170 
1171 
1172 /*
1173  * Default open and close routine for sata_hba framework.
1174  *
1175  */
1176 /*
1177  * Open devctl node.
1178  *
1179  * Returns:
1180  * 0 if node was open successfully, error code otherwise.
1181  *
1182  *
1183  */
1184 
1185 static int
1186 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1187 {
1188 #ifndef __lock_lint
1189 	_NOTE(ARGUNUSED(credp))
1190 #endif
1191 	int rv = 0;
1192 	dev_info_t *dip;
1193 	scsi_hba_tran_t *scsi_hba_tran;
1194 	sata_hba_inst_t	*sata_hba_inst;
1195 
1196 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL);
1197 
1198 	if (otyp != OTYP_CHR)
1199 		return (EINVAL);
1200 
1201 	dip = sata_devt_to_devinfo(*devp);
1202 	if (dip == NULL)
1203 		return (ENXIO);
1204 
1205 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1206 		return (ENXIO);
1207 
1208 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1209 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1210 		return (ENXIO);
1211 
1212 	mutex_enter(&sata_mutex);
1213 	if (flags & FEXCL) {
1214 		if (sata_hba_inst->satahba_open_flag != 0) {
1215 			rv = EBUSY;
1216 		} else {
1217 			sata_hba_inst->satahba_open_flag =
1218 			    SATA_DEVCTL_EXOPENED;
1219 		}
1220 	} else {
1221 		if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) {
1222 			rv = EBUSY;
1223 		} else {
1224 			sata_hba_inst->satahba_open_flag =
1225 			    SATA_DEVCTL_SOPENED;
1226 		}
1227 	}
1228 	mutex_exit(&sata_mutex);
1229 
1230 	return (rv);
1231 }
1232 
1233 
1234 /*
1235  * Close devctl node.
1236  * Returns:
1237  * 0 if node was closed successfully, error code otherwise.
1238  *
1239  */
1240 
1241 static int
1242 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
1243 {
1244 #ifndef __lock_lint
1245 	_NOTE(ARGUNUSED(credp))
1246 	_NOTE(ARGUNUSED(flag))
1247 #endif
1248 	dev_info_t *dip;
1249 	scsi_hba_tran_t *scsi_hba_tran;
1250 	sata_hba_inst_t	*sata_hba_inst;
1251 
1252 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL);
1253 
1254 	if (otyp != OTYP_CHR)
1255 		return (EINVAL);
1256 
1257 	dip = sata_devt_to_devinfo(dev);
1258 	if (dip == NULL)
1259 		return (ENXIO);
1260 
1261 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1262 		return (ENXIO);
1263 
1264 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1265 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1266 		return (ENXIO);
1267 
1268 	mutex_enter(&sata_mutex);
1269 	sata_hba_inst->satahba_open_flag = 0;
1270 	mutex_exit(&sata_mutex);
1271 	return (0);
1272 }
1273 
1274 
1275 
1276 /*
1277  * Standard IOCTL commands for SATA hotplugging.
1278  * Implemented DEVCTL_AP commands:
1279  * DEVCTL_AP_CONNECT
1280  * DEVCTL_AP_DISCONNECT
1281  * DEVCTL_AP_CONFIGURE
1282  * DEVCTL_UNCONFIGURE
1283  * DEVCTL_AP_CONTROL
1284  *
1285  * Commands passed to default ndi ioctl handler:
1286  * DEVCTL_DEVICE_GETSTATE
1287  * DEVCTL_DEVICE_ONLINE
1288  * DEVCTL_DEVICE_OFFLINE
1289  * DEVCTL_DEVICE_REMOVE
1290  * DEVCTL_DEVICE_INSERT
1291  * DEVCTL_BUS_GETSTATE
1292  *
1293  * All other cmds are passed to HBA if it provide ioctl handler, or failed
1294  * if not.
1295  *
1296  * Returns:
1297  * 0 if successful,
1298  * error code if operation failed.
1299  *
1300  * Port Multiplier support is supported now.
1301  *
1302  * NOTE: qual should be SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT
1303  */
1304 
1305 static int
1306 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1307     int *rvalp)
1308 {
1309 #ifndef __lock_lint
1310 	_NOTE(ARGUNUSED(credp))
1311 	_NOTE(ARGUNUSED(rvalp))
1312 #endif
1313 	int rv = 0;
1314 	int32_t	comp_port = -1;
1315 	dev_info_t *dip;
1316 	devctl_ap_state_t ap_state;
1317 	struct devctl_iocdata *dcp = NULL;
1318 	scsi_hba_tran_t *scsi_hba_tran;
1319 	sata_hba_inst_t *sata_hba_inst;
1320 	sata_device_t sata_device;
1321 	sata_cport_info_t *cportinfo;
1322 	int cport, pmport, qual;
1323 	int rval = SATA_SUCCESS;
1324 
1325 	dip = sata_devt_to_devinfo(dev);
1326 	if (dip == NULL)
1327 		return (ENXIO);
1328 
1329 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1330 		return (ENXIO);
1331 
1332 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1333 	if (sata_hba_inst == NULL)
1334 		return (ENXIO);
1335 
1336 	if (sata_hba_inst->satahba_tran == NULL)
1337 		return (ENXIO);
1338 
1339 	switch (cmd) {
1340 
1341 	case DEVCTL_DEVICE_GETSTATE:
1342 	case DEVCTL_DEVICE_ONLINE:
1343 	case DEVCTL_DEVICE_OFFLINE:
1344 	case DEVCTL_DEVICE_REMOVE:
1345 	case DEVCTL_BUS_GETSTATE:
1346 		/*
1347 		 * There may be more cases that we want to pass to default
1348 		 * handler rather than fail them.
1349 		 */
1350 		return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
1351 	}
1352 
1353 	/* read devctl ioctl data */
1354 	if (cmd != DEVCTL_AP_CONTROL && cmd >= DEVCTL_IOC &&
1355 	    cmd <= DEVCTL_IOC_MAX) {
1356 		if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1357 			return (EFAULT);
1358 
1359 		if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) ==
1360 		    -1) {
1361 			if (dcp)
1362 				ndi_dc_freehdl(dcp);
1363 			return (EINVAL);
1364 		}
1365 
1366 		/*
1367 		 * According to SCSI_TO_SATA_ADDR_QUAL, qual should be either
1368 		 * SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT.
1369 		 */
1370 		cport = SCSI_TO_SATA_CPORT(comp_port);
1371 		pmport = SCSI_TO_SATA_PMPORT(comp_port);
1372 		qual = SCSI_TO_SATA_ADDR_QUAL(comp_port);
1373 
1374 		if (sata_validate_sata_address(sata_hba_inst, cport, pmport,
1375 		    qual) != 0) {
1376 			ndi_dc_freehdl(dcp);
1377 			return (EINVAL);
1378 		}
1379 
1380 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1381 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1382 		    cport_mutex);
1383 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1384 			/*
1385 			 * Cannot process ioctl request now. Come back later.
1386 			 */
1387 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1388 			    cport_mutex);
1389 			ndi_dc_freehdl(dcp);
1390 			return (EBUSY);
1391 		}
1392 		/* Block event processing for this port */
1393 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1394 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1395 
1396 		sata_device.satadev_addr.cport = cport;
1397 		sata_device.satadev_addr.pmport = pmport;
1398 		sata_device.satadev_addr.qual = qual;
1399 		sata_device.satadev_rev = SATA_DEVICE_REV;
1400 	}
1401 
1402 	switch (cmd) {
1403 
1404 	case DEVCTL_AP_DISCONNECT:
1405 
1406 		/*
1407 		 * Normally, cfgadm sata plugin will try to offline
1408 		 * (unconfigure) device before this request. Nevertheless,
1409 		 * if a device is still configured, we need to
1410 		 * attempt to offline and unconfigure device first, and we will
1411 		 * deactivate the port regardless of the unconfigure
1412 		 * operation results.
1413 		 *
1414 		 */
1415 		rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device);
1416 
1417 		break;
1418 
1419 	case DEVCTL_AP_UNCONFIGURE:
1420 
1421 		/*
1422 		 * The unconfigure operation uses generic nexus operation to
1423 		 * offline a device. It leaves a target device node attached.
1424 		 * and obviously sata_drive_info attached as well, because
1425 		 * from the hardware point of view nothing has changed.
1426 		 */
1427 		rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device);
1428 		break;
1429 
1430 	case DEVCTL_AP_CONNECT:
1431 	{
1432 		/*
1433 		 * The sata cfgadm pluging will invoke this operation only if
1434 		 * port was found in the disconnect state (failed state
1435 		 * is also treated as the disconnected state).
1436 		 * If port activation is successful and a device is found
1437 		 * attached to the port, the initialization sequence is
1438 		 * executed to probe the port and attach
1439 		 * a device structure to a port structure. The device is not
1440 		 * set in configured state (system-wise) by this operation.
1441 		 */
1442 
1443 		rv = sata_ioctl_connect(sata_hba_inst, &sata_device);
1444 
1445 		break;
1446 	}
1447 
1448 	case DEVCTL_AP_CONFIGURE:
1449 	{
1450 		/*
1451 		 * A port may be in an active or shutdown state.
1452 		 * If port is in a failed state, operation is aborted.
1453 		 * If a port is in a shutdown state, sata_tran_port_activate()
1454 		 * is invoked prior to any other operation.
1455 		 *
1456 		 * Onlining the device involves creating a new target node.
1457 		 * If there is an old target node present (belonging to
1458 		 * previously removed device), the operation is aborted - the
1459 		 * old node has to be released and removed before configure
1460 		 * operation is attempted.
1461 		 */
1462 
1463 		rv = sata_ioctl_configure(sata_hba_inst, &sata_device);
1464 
1465 		break;
1466 	}
1467 
1468 	case DEVCTL_AP_GETSTATE:
1469 
1470 		sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state);
1471 
1472 		ap_state.ap_last_change = (time_t)-1;
1473 		ap_state.ap_error_code = 0;
1474 		ap_state.ap_in_transition = 0;
1475 
1476 		/* Copy the return AP-state information to the user space */
1477 		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
1478 			rv = EFAULT;
1479 		}
1480 		break;
1481 
1482 	case DEVCTL_AP_CONTROL:
1483 	{
1484 		/*
1485 		 * Generic devctl for hardware specific functionality
1486 		 */
1487 		sata_ioctl_data_t	ioc;
1488 
1489 		ASSERT(dcp == NULL);
1490 
1491 		/* Copy in user ioctl data first */
1492 #ifdef _MULTI_DATAMODEL
1493 		if (ddi_model_convert_from(mode & FMODELS) ==
1494 		    DDI_MODEL_ILP32) {
1495 
1496 			sata_ioctl_data_32_t	ioc32;
1497 
1498 			if (ddi_copyin((void *)arg, (void *)&ioc32,
1499 			    sizeof (ioc32), mode) != 0) {
1500 				rv = EFAULT;
1501 				break;
1502 			}
1503 			ioc.cmd		= (uint_t)ioc32.cmd;
1504 			ioc.port	= (uint_t)ioc32.port;
1505 			ioc.get_size	= (uint_t)ioc32.get_size;
1506 			ioc.buf		= (caddr_t)(uintptr_t)ioc32.buf;
1507 			ioc.bufsiz	= (uint_t)ioc32.bufsiz;
1508 			ioc.misc_arg	= (uint_t)ioc32.misc_arg;
1509 		} else
1510 #endif /* _MULTI_DATAMODEL */
1511 		if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
1512 		    mode) != 0) {
1513 			return (EFAULT);
1514 		}
1515 
1516 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
1517 		    "sata_hba_ioctl: DEVCTL_AP_CONTROL "
1518 		    "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port);
1519 
1520 		/*
1521 		 * To avoid BE/LE and 32/64 issues, a get_size always returns
1522 		 * a 32-bit number.
1523 		 */
1524 		if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
1525 			return (EINVAL);
1526 		}
1527 		/* validate address */
1528 		cport = SCSI_TO_SATA_CPORT(ioc.port);
1529 		pmport = SCSI_TO_SATA_PMPORT(ioc.port);
1530 		qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port);
1531 
1532 		SATADBG3(SATA_DBG_IOCTL_IF, sata_hba_inst,
1533 		    "sata_hba_ioctl: target port is %d:%d (%d)",
1534 		    cport, pmport, qual);
1535 
1536 		if (sata_validate_sata_address(sata_hba_inst, cport,
1537 		    pmport, qual) != 0)
1538 			return (EINVAL);
1539 
1540 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1541 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1542 		    cport_mutex);
1543 		/* Is the port locked by event processing daemon ? */
1544 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1545 			/*
1546 			 * Cannot process ioctl request now. Come back later
1547 			 */
1548 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1549 			    cport_mutex);
1550 			return (EBUSY);
1551 		}
1552 		/* Block event processing for this port */
1553 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1554 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1555 
1556 
1557 		sata_device.satadev_addr.cport = cport;
1558 		sata_device.satadev_addr.pmport = pmport;
1559 		sata_device.satadev_addr.qual = qual;
1560 		sata_device.satadev_rev = SATA_DEVICE_REV;
1561 
1562 		switch (ioc.cmd) {
1563 
1564 		case SATA_CFGA_RESET_PORT:
1565 			/*
1566 			 * There is no protection for configured device.
1567 			 */
1568 			rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device);
1569 			break;
1570 
1571 		case SATA_CFGA_RESET_DEVICE:
1572 			/*
1573 			 * There is no protection for configured device.
1574 			 */
1575 			rv = sata_ioctl_reset_device(sata_hba_inst,
1576 			    &sata_device);
1577 			break;
1578 
1579 		case SATA_CFGA_RESET_ALL:
1580 			/*
1581 			 * There is no protection for configured devices.
1582 			 */
1583 			rv = sata_ioctl_reset_all(sata_hba_inst);
1584 			/*
1585 			 * We return here, because common return is for
1586 			 * a single port operation - we have already unlocked
1587 			 * all ports and no dc handle was allocated.
1588 			 */
1589 			return (rv);
1590 
1591 		case SATA_CFGA_PORT_DEACTIVATE:
1592 			/*
1593 			 * Arbitrarily unconfigure attached device, if any.
1594 			 * Even if the unconfigure fails, proceed with the
1595 			 * port deactivation.
1596 			 */
1597 			rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device);
1598 
1599 			break;
1600 
1601 		case SATA_CFGA_PORT_ACTIVATE:
1602 
1603 			rv = sata_ioctl_activate(sata_hba_inst, &sata_device);
1604 			break;
1605 
1606 		case SATA_CFGA_PORT_SELF_TEST:
1607 
1608 			rv = sata_ioctl_port_self_test(sata_hba_inst,
1609 			    &sata_device);
1610 			break;
1611 
1612 		case SATA_CFGA_GET_DEVICE_PATH:
1613 
1614 			rv = sata_ioctl_get_device_path(sata_hba_inst,
1615 			    &sata_device, &ioc, mode);
1616 			break;
1617 
1618 		case SATA_CFGA_GET_AP_TYPE:
1619 
1620 			rv = sata_ioctl_get_ap_type(sata_hba_inst,
1621 			    &sata_device, &ioc, mode);
1622 			break;
1623 
1624 		case SATA_CFGA_GET_MODEL_INFO:
1625 
1626 			rv = sata_ioctl_get_model_info(sata_hba_inst,
1627 			    &sata_device, &ioc, mode);
1628 			break;
1629 
1630 		case SATA_CFGA_GET_REVFIRMWARE_INFO:
1631 
1632 			rv = sata_ioctl_get_revfirmware_info(sata_hba_inst,
1633 			    &sata_device, &ioc, mode);
1634 			break;
1635 
1636 		case SATA_CFGA_GET_SERIALNUMBER_INFO:
1637 
1638 			rv = sata_ioctl_get_serialnumber_info(sata_hba_inst,
1639 			    &sata_device, &ioc, mode);
1640 			break;
1641 
1642 		default:
1643 			rv = EINVAL;
1644 			break;
1645 
1646 		} /* End of DEVCTL_AP_CONTROL cmd switch */
1647 
1648 		break;
1649 	}
1650 
1651 	default:
1652 	{
1653 		/*
1654 		 * If we got here, we got an IOCTL that SATA HBA Framework
1655 		 * does not recognize. Pass ioctl to HBA driver, in case
1656 		 * it could process it.
1657 		 */
1658 		sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran;
1659 		dev_info_t	*mydip = SATA_DIP(sata_hba_inst);
1660 
1661 		SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1662 		    "IOCTL 0x%2x not supported in SATA framework, "
1663 		    "passthrough to HBA", cmd);
1664 
1665 		if (sata_tran->sata_tran_ioctl == NULL) {
1666 			rv = EINVAL;
1667 			break;
1668 		}
1669 		rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg);
1670 		if (rval != 0) {
1671 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1672 			    "IOCTL 0x%2x failed in HBA", cmd);
1673 			rv = rval;
1674 		}
1675 		break;
1676 	}
1677 
1678 	} /* End of main IOCTL switch */
1679 
1680 	if (dcp) {
1681 		ndi_dc_freehdl(dcp);
1682 	}
1683 
1684 	if (cmd >= DEVCTL_IOC && cmd <= DEVCTL_IOC_MAX) {
1685 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
1686 		    cport)->cport_mutex);
1687 		cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
1688 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1689 	}
1690 
1691 	return (rv);
1692 }
1693 
1694 
1695 /*
1696  * Create error retrieval sata packet
1697  *
1698  * A sata packet is allocated and set-up to contain specified error retrieval
1699  * command and appropriate dma-able data buffer.
1700  * No association with any scsi packet is made and no callback routine is
1701  * specified.
1702  *
1703  * Returns a pointer to sata packet upon successful packet creation.
1704  * Returns NULL, if packet cannot be created.
1705  */
1706 sata_pkt_t *
1707 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device,
1708     int pkt_type)
1709 {
1710 	sata_hba_inst_t	*sata_hba_inst;
1711 	sata_pkt_txlate_t *spx;
1712 	sata_pkt_t *spkt;
1713 	sata_drive_info_t *sdinfo;
1714 
1715 	mutex_enter(&sata_mutex);
1716 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1717 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1718 		if (SATA_DIP(sata_hba_inst) == dip)
1719 			break;
1720 	}
1721 	mutex_exit(&sata_mutex);
1722 	ASSERT(sata_hba_inst != NULL);
1723 
1724 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
1725 	if (sdinfo == NULL) {
1726 		sata_log(sata_hba_inst, CE_WARN,
1727 		    "sata: error recovery request for non-attached device at "
1728 		    "cport %d", sata_device->satadev_addr.cport);
1729 		return (NULL);
1730 	}
1731 
1732 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1733 	spx->txlt_sata_hba_inst = sata_hba_inst;
1734 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
1735 	spkt = sata_pkt_alloc(spx, NULL);
1736 	if (spkt == NULL) {
1737 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1738 		return (NULL);
1739 	}
1740 	/* address is needed now */
1741 	spkt->satapkt_device.satadev_addr = sata_device->satadev_addr;
1742 
1743 	switch (pkt_type) {
1744 	case SATA_ERR_RETR_PKT_TYPE_NCQ:
1745 		if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) {
1746 			if (sata_check_for_dma_error(dip, spx)) {
1747 				ddi_fm_service_impact(dip,
1748 				    DDI_SERVICE_UNAFFECTED);
1749 				break;
1750 			}
1751 			return (spkt);
1752 		}
1753 		break;
1754 
1755 	case SATA_ERR_RETR_PKT_TYPE_ATAPI:
1756 		if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS) {
1757 			if (sata_check_for_dma_error(dip, spx)) {
1758 				ddi_fm_service_impact(dip,
1759 				    DDI_SERVICE_UNAFFECTED);
1760 				break;
1761 			}
1762 			return (spkt);
1763 		}
1764 		break;
1765 
1766 	default:
1767 		break;
1768 	}
1769 
1770 	sata_pkt_free(spx);
1771 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1772 	return (NULL);
1773 
1774 }
1775 
1776 
1777 /*
1778  * Free error retrieval sata packet
1779  *
1780  * Free sata packet and any associated resources allocated previously by
1781  * sata_get_error_retrieval_pkt().
1782  *
1783  * Void return.
1784  */
1785 void
1786 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt)
1787 {
1788 	sata_pkt_txlate_t *spx =
1789 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1790 
1791 	ASSERT(sata_pkt != NULL);
1792 
1793 	sata_free_local_buffer(spx);
1794 	sata_pkt_free(spx);
1795 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1796 
1797 }
1798 
1799 /*
1800  * Create READ PORT MULTIPLIER and WRITE PORT MULTIPLIER sata packet
1801  *
1802  * No association with any scsi packet is made and no callback routine is
1803  * specified.
1804  *
1805  * Returns a pointer to sata packet upon successful packet creation.
1806  * Returns NULL, if packet cannot be created.
1807  *
1808  * NOTE: Input/Output value includes 64 bits accoring to SATA Spec 2.6,
1809  * only lower 32 bits are available currently.
1810  */
1811 sata_pkt_t *
1812 sata_get_rdwr_pmult_pkt(dev_info_t *dip, sata_device_t *sd,
1813     uint8_t regn, uint32_t regv, uint32_t type)
1814 {
1815 	sata_hba_inst_t	*sata_hba_inst;
1816 	sata_pkt_txlate_t *spx;
1817 	sata_pkt_t *spkt;
1818 	sata_cmd_t *scmd;
1819 
1820 	/* Only READ/WRITE commands are accepted. */
1821 	ASSERT(type == SATA_RDWR_PMULT_PKT_TYPE_READ ||
1822 	    type == SATA_RDWR_PMULT_PKT_TYPE_WRITE);
1823 
1824 	mutex_enter(&sata_mutex);
1825 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1826 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1827 		if (SATA_DIP(sata_hba_inst) == dip)
1828 			break;
1829 	}
1830 	mutex_exit(&sata_mutex);
1831 	ASSERT(sata_hba_inst != NULL);
1832 
1833 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1834 	spx->txlt_sata_hba_inst = sata_hba_inst;
1835 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
1836 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
1837 	if (spkt == NULL) {
1838 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1839 		return (NULL);
1840 	}
1841 
1842 	/*
1843 	 * NOTE: We need to send this command to the port multiplier,
1844 	 * that means send to SATA_PMULT_HOSTPORT(0xf) pmport
1845 	 *
1846 	 * sata_device contains the address of actual target device, and the
1847 	 * pmport number in the command comes from the sata_device structure.
1848 	 */
1849 	spkt->satapkt_device.satadev_addr = sd->satadev_addr;
1850 	spkt->satapkt_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
1851 	spkt->satapkt_device.satadev_addr.qual = SATA_ADDR_PMULT;
1852 
1853 	/* Fill sata_pkt */
1854 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_POLLING;
1855 	spkt->satapkt_comp = NULL; /* Synchronous mode, no callback */
1856 	spkt->satapkt_time = 10; /* Timeout 10s */
1857 
1858 	/* Build READ PORT MULTIPLIER cmd in the sata_pkt */
1859 	scmd = &spkt->satapkt_cmd;
1860 	scmd->satacmd_features_reg = regn & 0xff;
1861 	scmd->satacmd_features_reg_ext = (regn >> 8) & 0xff;
1862 	scmd->satacmd_device_reg = sd->satadev_addr.pmport;
1863 	scmd->satacmd_addr_type = 0;		/* N/A */
1864 
1865 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
1866 
1867 	if (type == SATA_RDWR_PMULT_PKT_TYPE_READ) {
1868 		scmd->satacmd_cmd_reg = SATAC_READ_PORTMULT;
1869 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
1870 		scmd->satacmd_flags.sata_special_regs = 1;
1871 		scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1;
1872 		scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1;
1873 		scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1;
1874 		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1;
1875 	} else if (type == SATA_RDWR_PMULT_PKT_TYPE_WRITE) {
1876 		scmd->satacmd_cmd_reg = SATAC_WRITE_PORTMULT;
1877 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
1878 		scmd->satacmd_sec_count_lsb = regv & 0xff;
1879 		scmd->satacmd_lba_low_lsb = regv >> 8 & 0xff;
1880 		scmd->satacmd_lba_mid_lsb = regv >> 16 & 0xff;
1881 		scmd->satacmd_lba_high_lsb = regv >> 24 & 0xff;
1882 	}
1883 
1884 	return (spkt);
1885 }
1886 
1887 /*
1888  * Free sata packet and any associated resources allocated previously by
1889  * sata_get_rdwr_pmult_pkt().
1890  *
1891  * Void return.
1892  */
1893 void
1894 sata_free_rdwr_pmult_pkt(sata_pkt_t *sata_pkt)
1895 {
1896 	sata_pkt_txlate_t *spx =
1897 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1898 
1899 	/* Free allocated resources */
1900 	sata_pkt_free(spx);
1901 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1902 }
1903 
1904 /*
1905  * Register a port multiplier to framework.
1906  * 1) Store the GSCR values in the previous allocated pmult_info strctures.
1907  * 2) Search in the blacklist and update the number of the device ports of the
1908  * port multiplier.
1909  *
1910  * Void return.
1911  */
1912 void
1913 sata_register_pmult(dev_info_t *dip, sata_device_t *sd, sata_pmult_gscr_t *sg)
1914 {
1915 	sata_hba_inst_t *sata_hba_inst = NULL;
1916 	sata_pmult_info_t *pmultinfo;
1917 	sata_pmult_bl_t *blp;
1918 	int cport = sd->satadev_addr.cport;
1919 
1920 	mutex_enter(&sata_mutex);
1921 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1922 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1923 		if (SATA_DIP(sata_hba_inst) == dip)
1924 			if (sata_hba_inst->satahba_attached == 1)
1925 				break;
1926 	}
1927 	mutex_exit(&sata_mutex);
1928 	/* HBA not attached? */
1929 	if (sata_hba_inst == NULL)
1930 		return;
1931 
1932 	/* Number of pmports */
1933 	sd->satadev_add_info = sg->gscr2 & SATA_PMULT_PORTNUM_MASK;
1934 
1935 	/* Check the blacklist */
1936 	for (blp = sata_pmult_blacklist; blp->bl_gscr0; blp++) {
1937 		if (sg->gscr0 != blp->bl_gscr0 && blp->bl_gscr0)
1938 			continue;
1939 		if (sg->gscr1 != blp->bl_gscr1 && blp->bl_gscr1)
1940 			continue;
1941 		if (sg->gscr2 != blp->bl_gscr2 && blp->bl_gscr2)
1942 			continue;
1943 
1944 		cmn_err(CE_WARN, "!Port multiplier is on the blacklist.");
1945 		sd->satadev_add_info = blp->bl_flags;
1946 		break;
1947 	}
1948 
1949 	/* Register the port multiplier GSCR */
1950 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
1951 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
1952 	if (pmultinfo != NULL) {
1953 		pmultinfo->pmult_gscr = *sg;
1954 		pmultinfo->pmult_num_dev_ports =
1955 		    sd->satadev_add_info & SATA_PMULT_PORTNUM_MASK;
1956 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
1957 		    "Port multiplier registered at port %d", cport);
1958 	}
1959 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
1960 }
1961 
1962 /*
1963  * sata_split_model splits the model ID into vendor and product IDs.
1964  * It assumes that a vendor ID cannot be longer than 8 characters, and
1965  * that vendor and product ID are separated by a whitespace.
1966  */
1967 void
1968 sata_split_model(char *model, char **vendor, char **product)
1969 {
1970 	int i, modlen;
1971 	char *vid, *pid;
1972 
1973 	/*
1974 	 * remove whitespace at the end of model
1975 	 */
1976 	for (i = SATA_ID_MODEL_LEN; i > 0; i--)
1977 		if (model[i] == ' ' || model[i] == '\t' || model[i] == '\0')
1978 			model[i] = '\0';
1979 		else
1980 			break;
1981 
1982 	/*
1983 	 * try to split model into into vid/pid
1984 	 */
1985 	modlen = strlen(model);
1986 	for (i = 0, pid = model; i < modlen; i++, pid++)
1987 		if ((*pid == ' ') || (*pid == '\t'))
1988 			break;
1989 
1990 	/*
1991 	 * only use vid if it is less than 8 chars (as in SCSI)
1992 	 */
1993 	if (i < modlen && i <= 8) {
1994 		vid = model;
1995 		/*
1996 		 * terminate vid, establish pid
1997 		 */
1998 		*pid++ = '\0';
1999 	} else {
2000 		/*
2001 		 * vid will stay "ATA     "
2002 		 */
2003 		vid = NULL;
2004 		/*
2005 		 * model is all pid
2006 		 */
2007 		pid = model;
2008 	}
2009 
2010 	*vendor = vid;
2011 	*product = pid;
2012 }
2013 
2014 /*
2015  * sata_name_child is for composing the name of the node
2016  * the format of the name is "target,0".
2017  */
2018 static int
2019 sata_name_child(dev_info_t *dip, char *name, int namelen)
2020 {
2021 	int target;
2022 
2023 	target = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
2024 	    DDI_PROP_DONTPASS, "target", -1);
2025 	if (target == -1)
2026 		return (DDI_FAILURE);
2027 	(void) snprintf(name, namelen, "%x,0", target);
2028 	return (DDI_SUCCESS);
2029 }
2030 
2031 
2032 
2033 /* ****************** SCSA required entry points *********************** */
2034 
2035 /*
2036  * Implementation of scsi tran_tgt_init.
2037  * sata_scsi_tgt_init() initializes scsi_device structure
2038  *
2039  * If successful, DDI_SUCCESS is returned.
2040  * DDI_FAILURE is returned if addressed device does not exist
2041  */
2042 
2043 static int
2044 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2045     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2046 {
2047 #ifndef __lock_lint
2048 	_NOTE(ARGUNUSED(hba_dip))
2049 	_NOTE(ARGUNUSED(tgt_dip))
2050 #endif
2051 	sata_device_t		sata_device;
2052 	sata_drive_info_t	*sdinfo;
2053 	struct sata_id		*sid;
2054 	sata_hba_inst_t		*sata_hba_inst;
2055 	char			model[SATA_ID_MODEL_LEN + 1];
2056 	char			fw[SATA_ID_FW_LEN + 1];
2057 	char			*vid, *pid;
2058 
2059 	/*
2060 	 * Fail tran_tgt_init for .conf stub node
2061 	 */
2062 	if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
2063 		(void) ndi_merge_node(tgt_dip, sata_name_child);
2064 		ddi_set_name_addr(tgt_dip, NULL);
2065 		return (DDI_FAILURE);
2066 	}
2067 
2068 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2069 
2070 	/* Validate scsi device address */
2071 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2072 	    &sata_device) != 0)
2073 		return (DDI_FAILURE);
2074 
2075 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2076 	    sata_device.satadev_addr.cport)));
2077 
2078 	/* sata_device now contains a valid sata address */
2079 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2080 	if (sdinfo == NULL) {
2081 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2082 		    sata_device.satadev_addr.cport)));
2083 		return (DDI_FAILURE);
2084 	}
2085 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2086 	    sata_device.satadev_addr.cport)));
2087 
2088 	/*
2089 	 * Check if we need to create a legacy devid (i.e cmdk style) for
2090 	 * the target disks.
2091 	 *
2092 	 * HBA devinfo node will have the property "use-cmdk-devid-format"
2093 	 * if we need to create cmdk-style devid for all the disk devices
2094 	 * attached to this controller. This property may have been set
2095 	 * from HBA driver's .conf file or by the HBA driver in its
2096 	 * attach(9F) function.
2097 	 */
2098 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2099 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2100 	    "use-cmdk-devid-format", 0) == 1)) {
2101 		/* register a legacy devid for this target node */
2102 		sata_target_devid_register(tgt_dip, sdinfo);
2103 	}
2104 
2105 
2106 	/*
2107 	 * 'Identify Device Data' does not always fit in standard SCSI
2108 	 * INQUIRY data, so establish INQUIRY_* properties with full-form
2109 	 * of information.
2110 	 */
2111 	sid = &sdinfo->satadrv_id;
2112 #ifdef	_LITTLE_ENDIAN
2113 	swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
2114 	swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
2115 #else	/* _LITTLE_ENDIAN */
2116 	bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN);
2117 	bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN);
2118 #endif	/* _LITTLE_ENDIAN */
2119 	model[SATA_ID_MODEL_LEN] = 0;
2120 	fw[SATA_ID_FW_LEN] = 0;
2121 
2122 	sata_split_model(model, &vid, &pid);
2123 
2124 	if (vid)
2125 		(void) scsi_device_prop_update_inqstring(sd, INQUIRY_VENDOR_ID,
2126 		    vid, strlen(vid));
2127 	if (pid)
2128 		(void) scsi_device_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID,
2129 		    pid, strlen(pid));
2130 	(void) scsi_device_prop_update_inqstring(sd, INQUIRY_REVISION_ID,
2131 	    fw, strlen(fw));
2132 
2133 	return (DDI_SUCCESS);
2134 }
2135 
2136 /*
2137  * Implementation of scsi tran_tgt_probe.
2138  * Probe target, by calling default scsi routine scsi_hba_probe()
2139  */
2140 static int
2141 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void))
2142 {
2143 	sata_hba_inst_t *sata_hba_inst =
2144 	    (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private);
2145 	int rval;
2146 	uint32_t pm_cap;
2147 
2148 	rval = scsi_hba_probe(sd, callback);
2149 	pm_cap = SATA_CAP_POWER_CONDITON | SATA_CAP_SMART_PAGE |
2150 	    SATA_CAP_LOG_SENSE;
2151 
2152 	if (rval == SCSIPROBE_EXISTS) {
2153 		/*
2154 		 * Set property "pm-capable" on the target device node, so that
2155 		 * the target driver will not try to fetch scsi cycle counters
2156 		 * before enabling device power-management.
2157 		 */
2158 		if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
2159 		    "pm-capable", pm_cap)) != DDI_PROP_SUCCESS) {
2160 			sata_log(sata_hba_inst, CE_WARN,
2161 			    "SATA device at port %d: "
2162 			    "will not be power-managed ",
2163 			    SCSI_TO_SATA_CPORT(sd->sd_address.a_target));
2164 			SATA_LOG_D((sata_hba_inst, CE_WARN,
2165 			    "failure updating pm-capable property"));
2166 		}
2167 	}
2168 	return (rval);
2169 }
2170 
2171 /*
2172  * Implementation of scsi tran_tgt_free.
2173  * Release all resources allocated for scsi_device
2174  */
2175 static void
2176 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2177     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2178 {
2179 #ifndef __lock_lint
2180 	_NOTE(ARGUNUSED(hba_dip))
2181 #endif
2182 	sata_device_t		sata_device;
2183 	sata_drive_info_t	*sdinfo;
2184 	sata_hba_inst_t		*sata_hba_inst;
2185 	ddi_devid_t		devid;
2186 
2187 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2188 
2189 	/* Validate scsi device address */
2190 	/*
2191 	 * Note: tgt_free relates to the SCSA view of a device. If called, there
2192 	 * was a device at this address, so even if the sata framework internal
2193 	 * resources were alredy released because a device was detached,
2194 	 * this function should be executed as long as its actions do
2195 	 * not require the internal sata view of a device and the address
2196 	 * refers to a valid sata address.
2197 	 * Validating the address here means that we do not trust SCSA...
2198 	 */
2199 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2200 	    &sata_device) == -1)
2201 		return;
2202 
2203 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2204 	    sata_device.satadev_addr.cport)));
2205 
2206 	/* sata_device now should contain a valid sata address */
2207 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2208 	if (sdinfo == NULL) {
2209 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2210 		    sata_device.satadev_addr.cport)));
2211 		return;
2212 	}
2213 	/*
2214 	 * We did not allocate any resources in sata_scsi_tgt_init()
2215 	 * other than few properties.
2216 	 * Free them.
2217 	 */
2218 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2219 	    sata_device.satadev_addr.cport)));
2220 	(void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable");
2221 
2222 	/*
2223 	 * If devid was previously created but not freed up from
2224 	 * sd(4D) driver (i.e during detach(9F)) then do it here.
2225 	 */
2226 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2227 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2228 	    "use-cmdk-devid-format", 0) == 1) &&
2229 	    (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) {
2230 		ddi_devid_unregister(tgt_dip);
2231 		ddi_devid_free(devid);
2232 	}
2233 }
2234 
2235 /*
2236  * Implementation of scsi tran_init_pkt
2237  * Upon successful return, scsi pkt buffer has DMA resources allocated.
2238  *
2239  * It seems that we should always allocate pkt, even if the address is
2240  * for non-existing device - just use some default for dma_attr.
2241  * The reason is that there is no way to communicate this to a caller here.
2242  * Subsequent call to sata_scsi_start may fail appropriately.
2243  * Simply returning NULL does not seem to discourage a target driver...
2244  *
2245  * Returns a pointer to initialized scsi_pkt, or NULL otherwise.
2246  */
2247 static struct scsi_pkt *
2248 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
2249     struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
2250     int (*callback)(caddr_t), caddr_t arg)
2251 {
2252 	sata_hba_inst_t *sata_hba_inst =
2253 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2254 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
2255 	sata_device_t sata_device;
2256 	sata_drive_info_t *sdinfo;
2257 	sata_pkt_txlate_t *spx;
2258 	ddi_dma_attr_t cur_dma_attr;
2259 	int rval;
2260 	boolean_t new_pkt = B_TRUE;
2261 
2262 	ASSERT(ap->a_hba_tran->tran_hba_dip == dip);
2263 
2264 	/*
2265 	 * We need to translate the address, even if it could be
2266 	 * a bogus one, for a non-existing device
2267 	 */
2268 	sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
2269 	sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target);
2270 	sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2271 	sata_device.satadev_rev = SATA_DEVICE_REV;
2272 
2273 	if (pkt == NULL) {
2274 		/*
2275 		 * Have to allocate a brand new scsi packet.
2276 		 * We need to operate with auto request sense enabled.
2277 		 */
2278 		pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen,
2279 		    MAX(statuslen, SATA_MAX_SENSE_LEN),
2280 		    tgtlen, sizeof (sata_pkt_txlate_t), callback, arg);
2281 
2282 		if (pkt == NULL)
2283 			return (NULL);
2284 
2285 		/* Fill scsi packet structure */
2286 		pkt->pkt_comp		= (void (*)())NULL;
2287 		pkt->pkt_time		= 0;
2288 		pkt->pkt_resid		= 0;
2289 		pkt->pkt_statistics	= 0;
2290 		pkt->pkt_reason		= 0;
2291 
2292 		/*
2293 		 * pkt_hba_private will point to sata pkt txlate structure
2294 		 */
2295 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2296 		bzero(spx, sizeof (sata_pkt_txlate_t));
2297 
2298 		spx->txlt_scsi_pkt = pkt;
2299 		spx->txlt_sata_hba_inst = sata_hba_inst;
2300 
2301 		/* Allocate sata_pkt */
2302 		spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback);
2303 		if (spx->txlt_sata_pkt == NULL) {
2304 			/* Could not allocate sata pkt */
2305 			scsi_hba_pkt_free(ap, pkt);
2306 			return (NULL);
2307 		}
2308 		/* Set sata address */
2309 		spx->txlt_sata_pkt->satapkt_device.satadev_addr =
2310 		    sata_device.satadev_addr;
2311 		spx->txlt_sata_pkt->satapkt_device.satadev_rev =
2312 		    sata_device.satadev_rev;
2313 
2314 		if ((bp == NULL) || (bp->b_bcount == 0))
2315 			return (pkt);
2316 
2317 		spx->txlt_total_residue = bp->b_bcount;
2318 	} else {
2319 		new_pkt = B_FALSE;
2320 		/*
2321 		 * Packet was preallocated/initialized by previous call
2322 		 */
2323 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2324 
2325 		if ((bp == NULL) || (bp->b_bcount == 0)) {
2326 			return (pkt);
2327 		}
2328 
2329 		/* Pkt is available already: spx->txlt_scsi_pkt == pkt; */
2330 	}
2331 
2332 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
2333 
2334 	/*
2335 	 * We use an adjusted version of the dma_attr, to account
2336 	 * for device addressing limitations.
2337 	 * sata_adjust_dma_attr() will handle sdinfo == NULL which may
2338 	 * happen when a device is not yet configured.
2339 	 */
2340 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2341 	    sata_device.satadev_addr.cport)));
2342 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2343 	    &spx->txlt_sata_pkt->satapkt_device);
2344 	/* NULL sdinfo may be passsed to sata_adjust_dma_attr() */
2345 	sata_adjust_dma_attr(sdinfo,
2346 	    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
2347 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2348 	    sata_device.satadev_addr.cport)));
2349 	/*
2350 	 * Allocate necessary DMA resources for the packet's data buffer
2351 	 * NOTE:
2352 	 * In case of read/write commands, DMA resource allocation here is
2353 	 * based on the premise that the transfer length specified in
2354 	 * the read/write scsi cdb will match exactly DMA resources -
2355 	 * returning correct packet residue is crucial.
2356 	 */
2357 	if ((rval = sata_dma_buf_setup(spx, flags, callback, arg,
2358 	    &cur_dma_attr)) != DDI_SUCCESS) {
2359 		/*
2360 		 * If a DMA allocation request fails with
2361 		 * DDI_DMA_NOMAPPING, indicate the error by calling
2362 		 * bioerror(9F) with bp and an error code of EFAULT.
2363 		 * If a DMA allocation request fails with
2364 		 * DDI_DMA_TOOBIG, indicate the error by calling
2365 		 * bioerror(9F) with bp and an error code of EINVAL.
2366 		 * For DDI_DMA_NORESOURCES, we may have some of them allocated.
2367 		 * Request may be repeated later - there is no real error.
2368 		 */
2369 		switch (rval) {
2370 		case DDI_DMA_NORESOURCES:
2371 			bioerror(bp, 0);
2372 			break;
2373 		case DDI_DMA_NOMAPPING:
2374 		case DDI_DMA_BADATTR:
2375 			bioerror(bp, EFAULT);
2376 			break;
2377 		case DDI_DMA_TOOBIG:
2378 		default:
2379 			bioerror(bp, EINVAL);
2380 			break;
2381 		}
2382 		goto fail;
2383 	}
2384 
2385 	if (sata_check_for_dma_error(dip, spx)) {
2386 		ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED);
2387 		bioerror(bp, EFAULT);
2388 		goto fail;
2389 	}
2390 
2391 success:
2392 	/* Set number of bytes that are not yet accounted for */
2393 	pkt->pkt_resid = spx->txlt_total_residue;
2394 	ASSERT(pkt->pkt_resid >= 0);
2395 
2396 	return (pkt);
2397 
2398 fail:
2399 	if (new_pkt == B_TRUE) {
2400 		/*
2401 		 * Since this is a new packet, we can clean-up
2402 		 * everything
2403 		 */
2404 		sata_scsi_destroy_pkt(ap, pkt);
2405 	} else {
2406 		/*
2407 		 * This is a re-used packet. It will be target driver's
2408 		 * responsibility to eventually destroy it (which
2409 		 * will free allocated resources).
2410 		 * Here, we just "complete" the request, leaving
2411 		 * allocated resources intact, so the request may
2412 		 * be retried.
2413 		 */
2414 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2415 		sata_pkt_free(spx);
2416 	}
2417 	return (NULL);
2418 }
2419 
2420 /*
2421  * Implementation of scsi tran_start.
2422  * Translate scsi cmd into sata operation and return status.
2423  * ATAPI CDBs are passed to ATAPI devices - the device determines what commands
2424  * are supported.
2425  * For SATA hard disks, supported scsi commands:
2426  * SCMD_INQUIRY
2427  * SCMD_TEST_UNIT_READY
2428  * SCMD_START_STOP
2429  * SCMD_READ_CAPACITY
2430  * SCMD_SVC_ACTION_IN_G4 (READ CAPACITY (16))
2431  * SCMD_REQUEST_SENSE
2432  * SCMD_LOG_SENSE_G1
2433  * SCMD_LOG_SELECT_G1
2434  * SCMD_MODE_SENSE	(specific pages)
2435  * SCMD_MODE_SENSE_G1	(specific pages)
2436  * SCMD_MODE_SELECT	(specific pages)
2437  * SCMD_MODE_SELECT_G1	(specific pages)
2438  * SCMD_SYNCHRONIZE_CACHE
2439  * SCMD_SYNCHRONIZE_CACHE_G1
2440  * SCMD_READ
2441  * SCMD_READ_G1
2442  * SCMD_READ_G4
2443  * SCMD_READ_G5
2444  * SCMD_WRITE
2445  * SCMD_WRITE_BUFFER
2446  * SCMD_WRITE_G1
2447  * SCMD_WRITE_G4
2448  * SCMD_WRITE_G5
2449  * SCMD_SEEK		(noop)
2450  * SCMD_SDIAG
2451  *
2452  * All other commands are rejected as unsupported.
2453  *
2454  * Returns:
2455  * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver
2456  * for execution. TRAN_ACCEPT may be returned also if device was removed but
2457  * a callback could be scheduled.
2458  * TRAN_BADPKT if cmd was directed to invalid address.
2459  * TRAN_FATAL_ERROR is command was rejected due to hardware error, including
2460  * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device
2461  * was removed and there was no callback specified in scsi pkt.
2462  * TRAN_BUSY if command could not be executed becasue HBA driver or SATA
2463  * framework was busy performing some other operation(s).
2464  *
2465  */
2466 static int
2467 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
2468 {
2469 	sata_hba_inst_t *sata_hba_inst =
2470 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2471 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2472 	sata_device_t *sdevice = &spx->txlt_sata_pkt->satapkt_device;
2473 	sata_drive_info_t *sdinfo;
2474 	struct buf *bp;
2475 	uint8_t cport, pmport;
2476 	boolean_t dev_gone = B_FALSE;
2477 	int rval;
2478 
2479 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2480 	    "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]);
2481 
2482 	ASSERT(spx != NULL &&
2483 	    spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL);
2484 
2485 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
2486 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2487 
2488 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2489 
2490 	if (sdevice->satadev_addr.qual == SATA_ADDR_DCPORT) {
2491 		sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2492 		if (sdinfo == NULL ||
2493 		    SATA_CPORT_INFO(sata_hba_inst, cport)->
2494 		    cport_tgtnode_clean == B_FALSE ||
2495 		    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2496 			dev_gone = B_TRUE;
2497 		}
2498 	} else if (sdevice->satadev_addr.qual == SATA_ADDR_DPMPORT) {
2499 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
2500 		    SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst,
2501 		    cport) == NULL) {
2502 			dev_gone = B_TRUE;
2503 		} else if (SATA_PMPORT_INFO(sata_hba_inst, cport,
2504 		    pmport) == NULL) {
2505 			dev_gone = B_TRUE;
2506 		} else {
2507 			mutex_enter(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2508 			    cport, pmport)));
2509 			sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2510 			if (sdinfo == NULL ||
2511 			    SATA_PMPORT_INFO(sata_hba_inst, cport, pmport)->
2512 			    pmport_tgtnode_clean == B_FALSE ||
2513 			    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2514 				dev_gone = B_TRUE;
2515 			}
2516 			mutex_exit(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2517 			    cport, pmport)));
2518 		}
2519 	}
2520 
2521 	if (dev_gone == B_TRUE) {
2522 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2523 		pkt->pkt_reason = CMD_DEV_GONE;
2524 		/*
2525 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2526 		 * only in callback function (for normal requests) and
2527 		 * in the dump code path.
2528 		 * So, if the callback is available, we need to do
2529 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2530 		 */
2531 		if (pkt->pkt_comp != NULL) {
2532 			/* scsi callback required */
2533 			if (servicing_interrupt()) {
2534 				if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2535 				    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2536 				    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
2537 				    TASKQID_INVALID) {
2538 					return (TRAN_BUSY);
2539 				}
2540 			} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2541 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2542 			    spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
2543 				/* Scheduling the callback failed */
2544 				return (TRAN_BUSY);
2545 			}
2546 			return (TRAN_ACCEPT);
2547 		}
2548 		/* No callback available */
2549 		return (TRAN_FATAL_ERROR);
2550 	}
2551 
2552 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
2553 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2554 		rval = sata_txlt_atapi(spx);
2555 		SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2556 		    "sata_scsi_start atapi: rval %d\n", rval);
2557 		return (rval);
2558 	}
2559 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2560 
2561 	/*
2562 	 * Checking for power state, if it was on
2563 	 * STOPPED state, then the drive is not capable
2564 	 * of processing media access command.  And
2565 	 * TEST_UNIT_READY, REQUEST_SENSE has special handling
2566 	 * in the function for different power state.
2567 	 */
2568 	if (((sdinfo->satadrv_power_level == SATA_POWER_STANDBY) ||
2569 	    (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)) &&
2570 	    (SATA_IS_MEDIUM_ACCESS_CMD(pkt->pkt_cdbp[0]))) {
2571 		return (sata_txlt_check_condition(spx, KEY_NOT_READY,
2572 		    SD_SCSI_ASC_LU_NOT_READY));
2573 	}
2574 
2575 	/* ATA Disk commands processing starts here */
2576 
2577 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2578 
2579 	switch (pkt->pkt_cdbp[0]) {
2580 
2581 	case SCMD_INQUIRY:
2582 		/* Mapped to identify device */
2583 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2584 			bp_mapin(bp);
2585 		rval = sata_txlt_inquiry(spx);
2586 		break;
2587 
2588 	case SCMD_TEST_UNIT_READY:
2589 		/*
2590 		 * SAT "SATA to ATA Translation" doc specifies translation
2591 		 * to ATA CHECK POWER MODE.
2592 		 */
2593 		rval = sata_txlt_test_unit_ready(spx);
2594 		break;
2595 
2596 	case SCMD_START_STOP:
2597 		/* Mapping depends on the command */
2598 		rval = sata_txlt_start_stop_unit(spx);
2599 		break;
2600 
2601 	case SCMD_READ_CAPACITY:
2602 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2603 			bp_mapin(bp);
2604 		rval = sata_txlt_read_capacity(spx);
2605 		break;
2606 
2607 	case SCMD_SVC_ACTION_IN_G4:		/* READ CAPACITY (16) */
2608 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2609 			bp_mapin(bp);
2610 		rval = sata_txlt_read_capacity16(spx);
2611 		break;
2612 
2613 	case SCMD_REQUEST_SENSE:
2614 		/*
2615 		 * Always No Sense, since we force ARQ
2616 		 */
2617 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2618 			bp_mapin(bp);
2619 		rval = sata_txlt_request_sense(spx);
2620 		break;
2621 
2622 	case SCMD_LOG_SENSE_G1:
2623 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2624 			bp_mapin(bp);
2625 		rval = sata_txlt_log_sense(spx);
2626 		break;
2627 
2628 	case SCMD_LOG_SELECT_G1:
2629 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2630 			bp_mapin(bp);
2631 		rval = sata_txlt_log_select(spx);
2632 		break;
2633 
2634 	case SCMD_MODE_SENSE:
2635 	case SCMD_MODE_SENSE_G1:
2636 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2637 			bp_mapin(bp);
2638 		rval = sata_txlt_mode_sense(spx);
2639 		break;
2640 
2641 
2642 	case SCMD_MODE_SELECT:
2643 	case SCMD_MODE_SELECT_G1:
2644 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2645 			bp_mapin(bp);
2646 		rval = sata_txlt_mode_select(spx);
2647 		break;
2648 
2649 	case SCMD_SYNCHRONIZE_CACHE:
2650 	case SCMD_SYNCHRONIZE_CACHE_G1:
2651 		rval = sata_txlt_synchronize_cache(spx);
2652 		break;
2653 
2654 	case SCMD_READ:
2655 	case SCMD_READ_G1:
2656 	case SCMD_READ_G4:
2657 	case SCMD_READ_G5:
2658 		rval = sata_txlt_read(spx);
2659 		break;
2660 	case SCMD_WRITE_BUFFER:
2661 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2662 			bp_mapin(bp);
2663 		rval = sata_txlt_write_buffer(spx);
2664 		break;
2665 
2666 	case SCMD_WRITE:
2667 	case SCMD_WRITE_G1:
2668 	case SCMD_WRITE_G4:
2669 	case SCMD_WRITE_G5:
2670 		rval = sata_txlt_write(spx);
2671 		break;
2672 
2673 	case SCMD_SEEK:
2674 		rval = sata_txlt_nodata_cmd_immediate(spx);
2675 		break;
2676 
2677 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12:
2678 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16:
2679 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2680 			bp_mapin(bp);
2681 		rval = sata_txlt_ata_pass_thru(spx);
2682 		break;
2683 
2684 		/* Other cases will be filed later */
2685 		/* postponed until phase 2 of the development */
2686 	case SPC3_CMD_UNMAP:
2687 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2688 			bp_mapin(bp);
2689 		rval = sata_txlt_unmap(spx);
2690 		break;
2691 	default:
2692 		rval = sata_txlt_invalid_command(spx);
2693 		break;
2694 	}
2695 
2696 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2697 	    "sata_scsi_start: rval %d\n", rval);
2698 
2699 	return (rval);
2700 }
2701 
2702 /*
2703  * Implementation of scsi tran_abort.
2704  * Abort specific pkt or all packets.
2705  *
2706  * Returns 1 if one or more packets were aborted, returns 0 otherwise
2707  *
2708  * May be called from an interrupt level.
2709  */
2710 static int
2711 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt)
2712 {
2713 	sata_hba_inst_t *sata_hba_inst =
2714 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2715 	sata_device_t	sata_device;
2716 	sata_pkt_t	*sata_pkt;
2717 
2718 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2719 	    "sata_scsi_abort: %s at target: 0x%x\n",
2720 	    scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target);
2721 
2722 	/* Validate address */
2723 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0)
2724 		/* Invalid address */
2725 		return (0);
2726 
2727 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2728 	    sata_device.satadev_addr.cport)));
2729 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2730 		/* invalid address */
2731 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2732 		    sata_device.satadev_addr.cport)));
2733 		return (0);
2734 	}
2735 	if (scsi_pkt == NULL) {
2736 		/*
2737 		 * Abort all packets.
2738 		 * Although we do not have specific packet, we still need
2739 		 * dummy packet structure to pass device address to HBA.
2740 		 * Allocate one, without sleeping. Fail if pkt cannot be
2741 		 * allocated.
2742 		 */
2743 		sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP);
2744 		if (sata_pkt == NULL) {
2745 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2746 			    sata_device.satadev_addr.cport)));
2747 			SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: "
2748 			    "could not allocate sata_pkt"));
2749 			return (0);
2750 		}
2751 		sata_pkt->satapkt_rev = SATA_PKT_REV;
2752 		sata_pkt->satapkt_device = sata_device;
2753 		sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
2754 	} else {
2755 		if (scsi_pkt->pkt_ha_private == NULL) {
2756 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2757 			    sata_device.satadev_addr.cport)));
2758 			return (0); /* Bad scsi pkt */
2759 		}
2760 		/* extract pointer to sata pkt */
2761 		sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)->
2762 		    txlt_sata_pkt;
2763 	}
2764 
2765 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2766 	    sata_device.satadev_addr.cport)));
2767 	/* Send abort request to HBA */
2768 	if ((*SATA_ABORT_FUNC(sata_hba_inst))
2769 	    (SATA_DIP(sata_hba_inst), sata_pkt,
2770 	    scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) ==
2771 	    SATA_SUCCESS) {
2772 		if (scsi_pkt == NULL)
2773 			kmem_free(sata_pkt, sizeof (sata_pkt_t));
2774 		/* Success */
2775 		return (1);
2776 	}
2777 	/* Else, something did not go right */
2778 	if (scsi_pkt == NULL)
2779 		kmem_free(sata_pkt, sizeof (sata_pkt_t));
2780 	/* Failure */
2781 	return (0);
2782 }
2783 
2784 
2785 /*
2786  * Implementation of scsi tran_reset.
2787  * RESET_ALL request is translated into port reset.
2788  * RESET_TARGET requests is translated into a device reset,
2789  * RESET_LUN request is accepted only for LUN 0 and translated into
2790  * device reset.
2791  * The target reset should cause all HBA active and queued packets to
2792  * be terminated and returned with pkt reason SATA_PKT_RESET prior to
2793  * the return. HBA should report reset event for the device.
2794  *
2795  * Returns 1 upon success, 0 upon failure.
2796  */
2797 static int
2798 sata_scsi_reset(struct scsi_address *ap, int level)
2799 {
2800 	sata_hba_inst_t	*sata_hba_inst =
2801 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2802 	sata_device_t	sata_device;
2803 	int		val;
2804 
2805 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2806 	    "sata_scsi_reset: level %d target: 0x%x\n",
2807 	    level, ap->a_target);
2808 
2809 	/* Validate address */
2810 	val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device);
2811 	if (val == -1)
2812 		/* Invalid address */
2813 		return (0);
2814 
2815 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2816 	    sata_device.satadev_addr.cport)));
2817 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2818 		/* invalid address */
2819 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2820 		    sata_device.satadev_addr.cport)));
2821 		return (0);
2822 	}
2823 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2824 	    sata_device.satadev_addr.cport)));
2825 	if (level == RESET_ALL) {
2826 		/* port reset */
2827 		if (sata_device.satadev_addr.qual == SATA_ADDR_DCPORT)
2828 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2829 		else
2830 			sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
2831 
2832 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2833 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2834 			return (1);
2835 		else
2836 			return (0);
2837 
2838 	} else if (val == 0 &&
2839 	    (level == RESET_TARGET || level == RESET_LUN)) {
2840 		/* reset device (device attached) */
2841 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2842 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2843 			return (1);
2844 		else
2845 			return (0);
2846 	}
2847 	return (0);
2848 }
2849 
2850 
2851 /*
2852  * Implementation of scsi tran_getcap (get transport/device capabilities).
2853  * Supported capabilities for SATA hard disks:
2854  * auto-rqsense		(always supported)
2855  * tagged-qing		(supported if HBA supports it)
2856  * untagged-qing	(could be supported if disk supports it, but because
2857  *			 caching behavior allowing untagged queuing actually
2858  *			 results in reduced performance.  sd tries to throttle
2859  *			 back to only 3 outstanding commands, which may
2860  *			 work for real SCSI disks, but with read ahead
2861  *			 caching, having more than 1 outstanding command
2862  *			 results in cache thrashing.)
2863  * sector_size
2864  * dma_max
2865  * interconnect-type	(INTERCONNECT_SATA)
2866  *
2867  * Supported capabilities for ATAPI CD/DVD devices:
2868  * auto-rqsense		(always supported)
2869  * sector_size
2870  * dma_max
2871  * max-cdb-length
2872  * interconnect-type	(INTERCONNECT_SATA)
2873  *
2874  * Supported capabilities for ATAPI TAPE devices:
2875  * auto-rqsense		(always supported)
2876  * dma_max
2877  * max-cdb-length
2878  *
2879  * Supported capabilities for SATA ATAPI hard disks:
2880  * auto-rqsense		(always supported)
2881  * interconnect-type	(INTERCONNECT_SATA)
2882  * max-cdb-length
2883  *
2884  * Request for other capabilities is rejected as unsupported.
2885  *
2886  * Returns supported capability value, or -1 if capability is unsuppported or
2887  * the address is invalid - no device.
2888  */
2889 
2890 static int
2891 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
2892 {
2893 
2894 	sata_hba_inst_t		*sata_hba_inst =
2895 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2896 	sata_device_t		sata_device;
2897 	sata_drive_info_t	*sdinfo;
2898 	ddi_dma_attr_t		adj_dma_attr;
2899 	int			rval;
2900 
2901 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2902 	    "sata_scsi_getcap: target: 0x%x, cap: %s\n",
2903 	    ap->a_target, cap);
2904 
2905 	/*
2906 	 * We want to process the capabilities on per port granularity.
2907 	 * So, we are specifically restricting ourselves to whom != 0
2908 	 * to exclude the controller wide handling.
2909 	 */
2910 	if (cap == NULL || whom == 0)
2911 		return (-1);
2912 
2913 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2914 		/* Invalid address */
2915 		return (-1);
2916 	}
2917 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2918 	    sata_device.satadev_addr.cport)));
2919 	if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) ==
2920 	    NULL) {
2921 		/* invalid address */
2922 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2923 		    sata_device.satadev_addr.cport)));
2924 		return (-1);
2925 	}
2926 
2927 	switch (scsi_hba_lookup_capstr(cap)) {
2928 	case SCSI_CAP_ARQ:
2929 		rval = 1;		/* ARQ supported, turned on */
2930 		break;
2931 
2932 	case SCSI_CAP_SECTOR_SIZE:
2933 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK)
2934 			rval = SATA_DISK_SECTOR_SIZE;	/* fixed size */
2935 		else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD)
2936 			rval = SATA_ATAPI_SECTOR_SIZE;
2937 		else rval = -1;
2938 		break;
2939 
2940 	/*
2941 	 * untagged queuing cause a performance inversion because of
2942 	 * the way sd operates.  Because of this reason we do not
2943 	 * use it when available.
2944 	 */
2945 	case SCSI_CAP_UNTAGGED_QING:
2946 		if (sdinfo->satadrv_features_enabled &
2947 		    SATA_DEV_F_E_UNTAGGED_QING)
2948 			rval = 1;	/* Untagged queuing available */
2949 		else
2950 			rval = -1;	/* Untagged queuing not available */
2951 		break;
2952 
2953 	case SCSI_CAP_TAGGED_QING:
2954 		if ((sdinfo->satadrv_features_enabled &
2955 		    SATA_DEV_F_E_TAGGED_QING) &&
2956 		    (sdinfo->satadrv_max_queue_depth > 1))
2957 			rval = 1;	/* Tagged queuing available */
2958 		else
2959 			rval = -1;	/* Tagged queuing not available */
2960 		break;
2961 
2962 	case SCSI_CAP_DMA_MAX:
2963 		sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst),
2964 		    &adj_dma_attr);
2965 		rval = (int)adj_dma_attr.dma_attr_maxxfer;
2966 		/* We rely on the fact that dma_attr_maxxfer < 0x80000000 */
2967 		break;
2968 
2969 	case SCSI_CAP_INTERCONNECT_TYPE:
2970 		rval = INTERCONNECT_SATA;	/* SATA interconnect type */
2971 		break;
2972 
2973 	case SCSI_CAP_CDB_LEN:
2974 		if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI)
2975 			rval = sdinfo->satadrv_atapi_cdb_len;
2976 		else
2977 			rval = -1;
2978 		break;
2979 
2980 	default:
2981 		rval = -1;
2982 		break;
2983 	}
2984 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2985 	    sata_device.satadev_addr.cport)));
2986 	return (rval);
2987 }
2988 
2989 /*
2990  * Implementation of scsi tran_setcap
2991  *
2992  * Only SCSI_CAP_UNTAGGED_QING and  SCSI_CAP_TAGGED_QING are changeable.
2993  *
2994  */
2995 static int
2996 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
2997 {
2998 	sata_hba_inst_t	*sata_hba_inst =
2999 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
3000 	sata_device_t	sata_device;
3001 	sata_drive_info_t	*sdinfo;
3002 	int		rval;
3003 
3004 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
3005 	    "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap);
3006 
3007 	/*
3008 	 * We want to process the capabilities on per port granularity.
3009 	 * So, we are specifically restricting ourselves to whom != 0
3010 	 * to exclude the controller wide handling.
3011 	 */
3012 	if (cap == NULL || whom == 0) {
3013 		return (-1);
3014 	}
3015 
3016 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
3017 		/* Invalid address */
3018 		return (-1);
3019 	}
3020 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
3021 	    sata_device.satadev_addr.cport)));
3022 	if ((sdinfo = sata_get_device_info(sata_hba_inst,
3023 	    &sata_device)) == NULL) {
3024 		/* invalid address */
3025 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3026 		    sata_device.satadev_addr.cport)));
3027 		return (-1);
3028 	}
3029 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3030 	    sata_device.satadev_addr.cport)));
3031 
3032 	switch (scsi_hba_lookup_capstr(cap)) {
3033 	case SCSI_CAP_ARQ:
3034 	case SCSI_CAP_SECTOR_SIZE:
3035 	case SCSI_CAP_DMA_MAX:
3036 	case SCSI_CAP_INTERCONNECT_TYPE:
3037 		rval = 0;
3038 		break;
3039 	case SCSI_CAP_UNTAGGED_QING:
3040 		if (SATA_QDEPTH(sata_hba_inst) > 1) {
3041 			rval = 1;
3042 			if (value == 1) {
3043 				sdinfo->satadrv_features_enabled |=
3044 				    SATA_DEV_F_E_UNTAGGED_QING;
3045 			} else if (value == 0) {
3046 				sdinfo->satadrv_features_enabled &=
3047 				    ~SATA_DEV_F_E_UNTAGGED_QING;
3048 			} else {
3049 				rval = -1;
3050 			}
3051 		} else {
3052 			rval = 0;
3053 		}
3054 		break;
3055 	case SCSI_CAP_TAGGED_QING:
3056 		/* This can TCQ or NCQ */
3057 		if (sata_func_enable & SATA_ENABLE_QUEUING &&
3058 		    ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ &&
3059 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) ||
3060 		    (sata_func_enable & SATA_ENABLE_NCQ &&
3061 		    sdinfo->satadrv_features_support & SATA_DEV_F_NCQ &&
3062 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) &&
3063 		    (sdinfo->satadrv_max_queue_depth > 1)) {
3064 			rval = 1;
3065 			if (value == 1) {
3066 				sdinfo->satadrv_features_enabled |=
3067 				    SATA_DEV_F_E_TAGGED_QING;
3068 			} else if (value == 0) {
3069 				sdinfo->satadrv_features_enabled &=
3070 				    ~SATA_DEV_F_E_TAGGED_QING;
3071 			} else {
3072 				rval = -1;
3073 			}
3074 		} else {
3075 			rval = 0;
3076 		}
3077 		break;
3078 	default:
3079 		rval = -1;
3080 		break;
3081 	}
3082 	return (rval);
3083 }
3084 
3085 /*
3086  * Implementations of scsi tran_destroy_pkt.
3087  * Free resources allocated by sata_scsi_init_pkt()
3088  */
3089 static void
3090 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3091 {
3092 	sata_pkt_txlate_t *spx;
3093 
3094 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3095 
3096 	sata_common_free_dma_rsrcs(spx);
3097 
3098 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
3099 	sata_pkt_free(spx);
3100 
3101 	scsi_hba_pkt_free(ap, pkt);
3102 }
3103 
3104 /*
3105  * Implementation of scsi tran_dmafree.
3106  * Free DMA resources allocated by sata_scsi_init_pkt()
3107  */
3108 
3109 static void
3110 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
3111 {
3112 #ifndef __lock_lint
3113 	_NOTE(ARGUNUSED(ap))
3114 #endif
3115 	sata_pkt_txlate_t *spx;
3116 
3117 	ASSERT(pkt != NULL);
3118 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3119 
3120 	sata_common_free_dma_rsrcs(spx);
3121 }
3122 
3123 /*
3124  * Implementation of scsi tran_sync_pkt.
3125  *
3126  * The assumption below is that pkt is unique - there is no need to check ap
3127  *
3128  * Synchronize DMA buffer and, if the intermediate buffer is used, copy data
3129  * into/from the real buffer.
3130  */
3131 static void
3132 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3133 {
3134 #ifndef __lock_lint
3135 	_NOTE(ARGUNUSED(ap))
3136 #endif
3137 	int rval;
3138 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3139 	struct buf *bp;
3140 	int direction;
3141 
3142 	ASSERT(spx != NULL);
3143 	if (spx->txlt_buf_dma_handle != NULL) {
3144 		direction = spx->txlt_sata_pkt->
3145 		    satapkt_cmd.satacmd_flags.sata_data_direction;
3146 		if (spx->txlt_sata_pkt != NULL &&
3147 		    direction != SATA_DIR_NODATA_XFER) {
3148 			if (spx->txlt_tmp_buf != NULL) {
3149 				/* Intermediate DMA buffer used */
3150 				bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3151 
3152 				if (direction & SATA_DIR_WRITE) {
3153 					bcopy(bp->b_un.b_addr,
3154 					    spx->txlt_tmp_buf, bp->b_bcount);
3155 				}
3156 			}
3157 			/* Sync the buffer for device or for CPU */
3158 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle,   0, 0,
3159 			    (direction & SATA_DIR_WRITE) ?
3160 			    DDI_DMA_SYNC_FORDEV :  DDI_DMA_SYNC_FORCPU);
3161 			ASSERT(rval == DDI_SUCCESS);
3162 			if (spx->txlt_tmp_buf != NULL &&
3163 			    !(direction & SATA_DIR_WRITE)) {
3164 				/* Intermediate DMA buffer used for read */
3165 				bcopy(spx->txlt_tmp_buf,
3166 				    bp->b_un.b_addr, bp->b_bcount);
3167 			}
3168 
3169 		}
3170 	}
3171 }
3172 
3173 
3174 
3175 /* *******************  SATA - SCSI Translation functions **************** */
3176 /*
3177  * SCSI to SATA pkt and command translation and SATA to SCSI status/error
3178  * translation.
3179  */
3180 
3181 /*
3182  * Checks if a device exists and can be access and translates common
3183  * scsi_pkt data to sata_pkt data.
3184  *
3185  * Flag argument indicates that a non-read/write ATA command may be sent
3186  * to HBA in arbitrary SYNC mode to execute this packet.
3187  *
3188  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and
3189  * sata_pkt was set-up.
3190  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not
3191  * exist and pkt_comp callback was scheduled.
3192  * Returns other TRAN_XXXXX values when error occured and command should be
3193  * rejected with the returned TRAN_XXXXX value.
3194  *
3195  * This function should be called with port mutex held.
3196  */
3197 static int
3198 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason, int flag)
3199 {
3200 	sata_drive_info_t *sdinfo;
3201 	sata_device_t sata_device;
3202 	const struct sata_cmd_flags sata_initial_cmd_flags = {
3203 		SATA_DIR_NODATA_XFER,
3204 		/* all other values to 0/FALSE */
3205 	};
3206 	/*
3207 	 * Pkt_reason has to be set if the pkt_comp callback is invoked,
3208 	 * and that implies TRAN_ACCEPT return value. Any other returned value
3209 	 * indicates that the scsi packet was not accepted (the reason will not
3210 	 * be checked by the scsi target driver).
3211 	 * To make debugging easier, we set pkt_reason to know value here.
3212 	 * It may be changed later when different completion reason is
3213 	 * determined.
3214 	 */
3215 	spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
3216 	*reason = CMD_TRAN_ERR;
3217 
3218 	/* Validate address */
3219 	switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst,
3220 	    &spx->txlt_scsi_pkt->pkt_address, &sata_device)) {
3221 
3222 	case -1:
3223 		/* Invalid address or invalid device type */
3224 		return (TRAN_BADPKT);
3225 	case 2:
3226 		/*
3227 		 * Valid address but device type is unknown - Chack if it is
3228 		 * in the reset state and therefore in an indeterminate state.
3229 		 */
3230 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3231 		    &spx->txlt_sata_pkt->satapkt_device);
3232 		if (sdinfo != NULL && (sdinfo->satadrv_event_flags &
3233 		    (SATA_EVNT_DEVICE_RESET |
3234 		    SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3235 			if (!ddi_in_panic()) {
3236 				spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3237 				*reason = CMD_INCOMPLETE;
3238 				SATADBG1(SATA_DBG_SCSI_IF,
3239 				    spx->txlt_sata_hba_inst,
3240 				    "sata_scsi_start: rejecting command "
3241 				    "because of device reset state\n", NULL);
3242 				return (TRAN_BUSY);
3243 			}
3244 		}
3245 		/* FALLTHROUGH */
3246 	case 1:
3247 		/* valid address but no valid device - it has disappeared */
3248 		spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE;
3249 		*reason = CMD_DEV_GONE;
3250 		/*
3251 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
3252 		 * only in callback function (for normal requests) and
3253 		 * in the dump code path.
3254 		 * So, if the callback is available, we need to do
3255 		 * the callback rather than returning TRAN_FATAL_ERROR here.
3256 		 */
3257 		if (spx->txlt_scsi_pkt->pkt_comp != NULL) {
3258 			/* scsi callback required */
3259 			if (servicing_interrupt()) {
3260 				if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3261 				    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3262 				    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3263 				    TASKQID_INVALID) {
3264 					return (TRAN_BUSY);
3265 				}
3266 			} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3267 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3268 			    spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
3269 				/* Scheduling the callback failed */
3270 				return (TRAN_BUSY);
3271 			}
3272 
3273 			return (TRAN_ACCEPT);
3274 		}
3275 		return (TRAN_FATAL_ERROR);
3276 	default:
3277 		/* all OK; pkt reason will be overwritten later */
3278 		break;
3279 	}
3280 	/*
3281 	 * If pkt is to be executed in polling mode and a command will not be
3282 	 * emulated in SATA module (requires sending a non-read/write ATA
3283 	 * command to HBA driver in arbitrary SYNC mode) and we are in the
3284 	 * interrupt context and not in the panic dump, then reject the packet
3285 	 * to avoid a possible interrupt stack overrun or hang caused by
3286 	 * a potentially blocked interrupt.
3287 	 */
3288 	if (((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0 || flag != 0) &&
3289 	    servicing_interrupt() && !ddi_in_panic()) {
3290 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
3291 		    "sata_scsi_start: rejecting synchronous command because "
3292 		    "of interrupt context\n", NULL);
3293 		return (TRAN_BUSY);
3294 	}
3295 
3296 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3297 	    &spx->txlt_sata_pkt->satapkt_device);
3298 
3299 	/*
3300 	 * If device is in reset condition, reject the packet with
3301 	 * TRAN_BUSY, unless:
3302 	 * 1. system is panicking (dumping)
3303 	 * In such case only one thread is running and there is no way to
3304 	 * process reset.
3305 	 * 2. cfgadm operation is is progress (internal APCTL lock is set)
3306 	 * Some cfgadm operations involve drive commands, so reset condition
3307 	 * needs to be ignored for IOCTL operations.
3308 	 */
3309 	if ((sdinfo->satadrv_event_flags &
3310 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3311 
3312 		if (!ddi_in_panic() &&
3313 		    ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst,
3314 		    sata_device.satadev_addr.cport) &
3315 		    SATA_APCTL_LOCK_PORT_BUSY) == 0)) {
3316 			spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3317 			*reason = CMD_INCOMPLETE;
3318 			SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3319 			    "sata_scsi_start: rejecting command because "
3320 			    "of device reset state\n", NULL);
3321 			return (TRAN_BUSY);
3322 		}
3323 	}
3324 
3325 	/*
3326 	 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by
3327 	 * sata_scsi_pkt_init() because pkt init had to work also with
3328 	 * non-existing devices.
3329 	 * Now we know that the packet was set-up for a real device, so its
3330 	 * type is known.
3331 	 */
3332 	spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type;
3333 
3334 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags;
3335 	if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst,
3336 	    sata_device.satadev_addr.cport)->cport_event_flags &
3337 	    SATA_APCTL_LOCK_PORT_BUSY) != 0) {
3338 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3339 		    sata_ignore_dev_reset = B_TRUE;
3340 	}
3341 	/*
3342 	 * At this point the generic translation routine determined that the
3343 	 * scsi packet should be accepted. Packet completion reason may be
3344 	 * changed later when a different completion reason is determined.
3345 	 */
3346 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3347 	*reason = CMD_CMPLT;
3348 
3349 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
3350 		/* Synchronous execution */
3351 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH |
3352 		    SATA_OPMODE_POLLING;
3353 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3354 		    sata_ignore_dev_reset = ddi_in_panic();
3355 	} else {
3356 		/* Asynchronous execution */
3357 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH |
3358 		    SATA_OPMODE_INTERRUPTS;
3359 	}
3360 	/* Convert queuing information */
3361 	if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG)
3362 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag =
3363 		    B_TRUE;
3364 	else if (spx->txlt_scsi_pkt->pkt_flags &
3365 	    (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD))
3366 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag =
3367 		    B_TRUE;
3368 
3369 	/* Always limit pkt time */
3370 	if (spx->txlt_scsi_pkt->pkt_time == 0)
3371 		spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time;
3372 	else
3373 		/* Pass on scsi_pkt time */
3374 		spx->txlt_sata_pkt->satapkt_time =
3375 		    spx->txlt_scsi_pkt->pkt_time;
3376 
3377 	return (TRAN_ACCEPT);
3378 }
3379 
3380 
3381 /*
3382  * Translate ATA Identify Device data to SCSI Inquiry data.
3383  * This function may be called only for ATA devices.
3384  * This function should not be called for ATAPI devices - they
3385  * respond directly to SCSI Inquiry command.
3386  *
3387  * SATA Identify Device data has to be valid in sata_drive_info.
3388  * Buffer has to accomodate the inquiry length (36 bytes).
3389  *
3390  * This function should be called with a port mutex held.
3391  */
3392 static	void
3393 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst,
3394     sata_drive_info_t *sdinfo, uint8_t *buf)
3395 {
3396 
3397 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
3398 	struct sata_id *sid = &sdinfo->satadrv_id;
3399 
3400 	/* Start with a nice clean slate */
3401 	bzero((void *)inq, sizeof (struct scsi_inquiry));
3402 
3403 	/*
3404 	 * Rely on the dev_type for setting paripheral qualifier.
3405 	 * Assume that  DTYPE_RODIRECT applies to CD/DVD R/W devices.
3406 	 * It could be that DTYPE_OPTICAL could also qualify in the future.
3407 	 * ATAPI Inquiry may provide more data to the target driver.
3408 	 */
3409 	inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3410 	    DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */
3411 
3412 	/* CFA type device is not a removable media device */
3413 	inq->inq_rmb = ((sid->ai_config != SATA_CFA_TYPE) &&
3414 	    (sid->ai_config & SATA_REM_MEDIA)) ? 1 : 0;
3415 	inq->inq_qual = 0;	/* Device type qualifier (obsolete in SCSI3? */
3416 	inq->inq_iso = 0;	/* ISO version */
3417 	inq->inq_ecma = 0;	/* ECMA version */
3418 	inq->inq_ansi = 3;	/* ANSI version - SCSI 3 */
3419 	inq->inq_aenc = 0;	/* Async event notification cap. */
3420 	inq->inq_trmiop = 0;	/* Supports TERMINATE I/O PROC msg - NO */
3421 	inq->inq_normaca = 0;	/* setting NACA bit supported - NO */
3422 	inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */
3423 	inq->inq_len = 31;	/* Additional length */
3424 	inq->inq_dualp = 0;	/* dual port device - NO */
3425 	inq->inq_reladdr = 0;	/* Supports relative addressing - NO */
3426 	inq->inq_sync = 0;	/* Supports synchronous data xfers - NO */
3427 	inq->inq_linked = 0;	/* Supports linked commands - NO */
3428 				/*
3429 				 * Queuing support - controller has to
3430 				 * support some sort of command queuing.
3431 				 */
3432 	if (SATA_QDEPTH(sata_hba_inst) > 1)
3433 		inq->inq_cmdque = 1; /* Supports command queueing - YES */
3434 	else
3435 		inq->inq_cmdque = 0; /* Supports command queueing - NO */
3436 	inq->inq_sftre = 0;	/* Supports Soft Reset option - NO ??? */
3437 	inq->inq_wbus32 = 0;	/* Supports 32 bit wide data xfers - NO */
3438 	inq->inq_wbus16 = 0;	/* Supports 16 bit wide data xfers - NO */
3439 
3440 #ifdef	_LITTLE_ENDIAN
3441 	/* Swap text fields to match SCSI format */
3442 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3443 	swab(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3444 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3445 		swab(sid->ai_fw, inq->inq_revision, 4);	/* Revision level */
3446 	else
3447 		swab(&sid->ai_fw[4], inq->inq_revision, 4);	/* Rev. level */
3448 #else	/* _LITTLE_ENDIAN */
3449 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3450 	bcopy(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3451 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3452 		bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
3453 	else
3454 		bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
3455 #endif	/* _LITTLE_ENDIAN */
3456 }
3457 
3458 
3459 /*
3460  * Scsi response set up for invalid command (command not supported)
3461  *
3462  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3463  */
3464 static int
3465 sata_txlt_invalid_command(sata_pkt_txlate_t *spx)
3466 {
3467 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3468 	struct scsi_extended_sense *sense;
3469 
3470 	scsipkt->pkt_reason = CMD_CMPLT;
3471 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3472 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3473 
3474 	*scsipkt->pkt_scbp = STATUS_CHECK;
3475 
3476 	sense = sata_arq_sense(spx);
3477 	sense->es_key = KEY_ILLEGAL_REQUEST;
3478 	sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE;
3479 
3480 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3481 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3482 
3483 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3484 	    scsipkt->pkt_comp != NULL) {
3485 		/* scsi callback required */
3486 		if (servicing_interrupt()) {
3487 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3488 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3489 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3490 			    TASKQID_INVALID) {
3491 				return (TRAN_BUSY);
3492 			}
3493 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3494 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3495 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
3496 			/* Scheduling the callback failed */
3497 			return (TRAN_BUSY);
3498 		}
3499 	}
3500 	return (TRAN_ACCEPT);
3501 }
3502 
3503 /*
3504  * Scsi response set up for check condition with special sense key
3505  * and additional sense code.
3506  *
3507  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3508  */
3509 static int
3510 sata_txlt_check_condition(sata_pkt_txlate_t *spx, uchar_t key, uchar_t code)
3511 {
3512 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3513 	int cport = SATA_TXLT_CPORT(spx);
3514 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3515 	struct scsi_extended_sense *sense;
3516 
3517 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3518 	scsipkt->pkt_reason = CMD_CMPLT;
3519 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3520 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3521 
3522 	*scsipkt->pkt_scbp = STATUS_CHECK;
3523 
3524 	sense = sata_arq_sense(spx);
3525 	sense->es_key = key;
3526 	sense->es_add_code = code;
3527 
3528 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3529 
3530 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3531 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3532 
3533 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3534 	    scsipkt->pkt_comp != NULL) {
3535 		/* scsi callback required */
3536 		if (servicing_interrupt()) {
3537 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3538 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3539 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3540 			    TASKQID_INVALID) {
3541 				return (TRAN_BUSY);
3542 			}
3543 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3544 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3545 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
3546 			/* Scheduling the callback failed */
3547 			return (TRAN_BUSY);
3548 		}
3549 	}
3550 	return (TRAN_ACCEPT);
3551 }
3552 
3553 /*
3554  * Scsi response setup for
3555  * emulated non-data command that requires no action/return data
3556  *
3557  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3558  */
3559 static	int
3560 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx)
3561 {
3562 	int rval;
3563 	int reason;
3564 	kmutex_t *cport_mutex =  &(SATA_TXLT_CPORT_MUTEX(spx));
3565 
3566 	mutex_enter(cport_mutex);
3567 
3568 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
3569 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3570 		mutex_exit(cport_mutex);
3571 		return (rval);
3572 	}
3573 	mutex_exit(cport_mutex);
3574 
3575 	spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3576 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3577 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3578 	*(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD;
3579 
3580 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3581 	    "Scsi_pkt completion reason %x\n",
3582 	    spx->txlt_scsi_pkt->pkt_reason);
3583 
3584 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 &&
3585 	    spx->txlt_scsi_pkt->pkt_comp != NULL) {
3586 		/* scsi callback required */
3587 		if (servicing_interrupt()) {
3588 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3589 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3590 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3591 			    TASKQID_INVALID) {
3592 				return (TRAN_BUSY);
3593 			}
3594 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3595 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3596 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
3597 			/* Scheduling the callback failed */
3598 			return (TRAN_BUSY);
3599 		}
3600 	}
3601 	return (TRAN_ACCEPT);
3602 }
3603 
3604 
3605 /*
3606  * SATA translate command: Inquiry / Identify Device
3607  * Use cached Identify Device data for now, rather than issuing actual
3608  * Device Identify cmd request. If device is detached and re-attached,
3609  * asynchronous event processing should fetch and refresh Identify Device
3610  * data.
3611  * VPD pages supported now:
3612  * Vital Product Data page
3613  * Unit Serial Number page
3614  * Block Device Characteristics Page
3615  * ATA Information Page
3616  *
3617  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3618  */
3619 
3620 #define	EVPD			1	/* Extended Vital Product Data flag */
3621 #define	CMDDT			2	/* Command Support Data - Obsolete */
3622 #define	INQUIRY_SUP_VPD_PAGE	0	/* Supported VPD Pages Page Code */
3623 #define	INQUIRY_USN_PAGE	0x80	/* Unit Serial Number Page Code */
3624 #define	INQUIRY_BDC_PAGE	0xB1	/* Block Device Characteristics Page */
3625 					/* Code */
3626 #define	INQUIRY_ATA_INFO_PAGE	0x89	/* ATA Information Page Code */
3627 #define	INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Device identifiers */
3628 
3629 static int
3630 sata_txlt_inquiry(sata_pkt_txlate_t *spx)
3631 {
3632 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3633 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3634 	sata_drive_info_t *sdinfo;
3635 	struct scsi_extended_sense *sense;
3636 	int count;
3637 	uint8_t *p;
3638 	int i, j;
3639 	uint8_t page_buf[1024]; /* Max length */
3640 	int rval, reason;
3641 	ushort_t rate;
3642 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
3643 
3644 	/*
3645 	 * sata_txlt_generic_pkt_info() and sata_get_device_info() require
3646 	 * cport_mutex to be held while they are called. sdinfo is also
3647 	 * protected by cport_mutex, so we hold cport_mutex until after we've
3648 	 * finished using sdinfo.
3649 	 */
3650 	mutex_enter(cport_mutex);
3651 
3652 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
3653 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3654 		mutex_exit(cport_mutex);
3655 		return (rval);
3656 	}
3657 
3658 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3659 	    &spx->txlt_sata_pkt->satapkt_device);
3660 
3661 	ASSERT(sdinfo != NULL);
3662 
3663 	scsipkt->pkt_reason = CMD_CMPLT;
3664 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3665 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3666 
3667 	/* Reject not supported request */
3668 	if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */
3669 		*scsipkt->pkt_scbp = STATUS_CHECK;
3670 		sense = sata_arq_sense(spx);
3671 		sense->es_key = KEY_ILLEGAL_REQUEST;
3672 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3673 		goto done;
3674 	}
3675 
3676 	/* Valid Inquiry request */
3677 	*scsipkt->pkt_scbp = STATUS_GOOD;
3678 
3679 	if (bp == NULL || bp->b_un.b_addr == NULL || bp->b_bcount == 0)
3680 		goto done;
3681 
3682 	/*
3683 	 * Because it is fully emulated command storing data
3684 	 * programatically in the specified buffer, release
3685 	 * preallocated DMA resources before storing data in the buffer,
3686 	 * so no unwanted DMA sync would take place.
3687 	 */
3688 	sata_scsi_dmafree(NULL, scsipkt);
3689 
3690 	if (!(scsipkt->pkt_cdbp[1] & EVPD)) {
3691 		/* Standard Inquiry Data request */
3692 		struct scsi_inquiry inq;
3693 		unsigned int bufsize;
3694 
3695 		sata_identdev_to_inquiry(spx->txlt_sata_hba_inst,
3696 		    sdinfo, (uint8_t *)&inq);
3697 		/* Copy no more than requested */
3698 		count = MIN(bp->b_bcount, sizeof (struct scsi_inquiry));
3699 		bufsize = scsipkt->pkt_cdbp[4];
3700 		bufsize |= scsipkt->pkt_cdbp[3] << 8;
3701 		count = MIN(count, bufsize);
3702 		bcopy(&inq, bp->b_un.b_addr, count);
3703 
3704 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3705 		scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3706 		    bufsize - count : 0;
3707 		goto done;
3708 	}
3709 
3710 	/*
3711 	 * peripheral_qualifier = 0;
3712 	 *
3713 	 * We are dealing only with HD and will be
3714 	 * dealing with CD/DVD devices soon
3715 	 */
3716 	uint8_t peripheral_device_type =
3717 	    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3718 	    DTYPE_DIRECT : DTYPE_RODIRECT;
3719 
3720 	bzero(page_buf, sizeof (page_buf));
3721 
3722 	switch ((uint_t)scsipkt->pkt_cdbp[2]) {
3723 	case INQUIRY_SUP_VPD_PAGE:
3724 		/*
3725 		 * Request for supported Vital Product Data pages.
3726 		 */
3727 		page_buf[0] = peripheral_device_type;
3728 		page_buf[1] = INQUIRY_SUP_VPD_PAGE;
3729 		page_buf[2] = 0;
3730 		page_buf[4] = INQUIRY_SUP_VPD_PAGE;
3731 		page_buf[5] = INQUIRY_USN_PAGE;
3732 		page_buf[6] = INQUIRY_BDC_PAGE;
3733 		/*
3734 		 * If WWN info is present, provide a page for it.
3735 		 * Modern drives always have, but some legacy ones do not.
3736 		 */
3737 		if (sdinfo->satadrv_id.ai_naa_ieee_oui != 0) {
3738 			page_buf[3] = 5; /* page length */
3739 			page_buf[7] = INQUIRY_DEV_IDENTIFICATION_PAGE;
3740 			page_buf[8] = INQUIRY_ATA_INFO_PAGE;
3741 			count = 9;
3742 		} else {
3743 			page_buf[3] = 4; /* page length */
3744 			page_buf[7] = INQUIRY_ATA_INFO_PAGE;
3745 			count = 8;
3746 		}
3747 		/* Copy no more than requested */
3748 		count = MIN(bp->b_bcount, count);
3749 		bcopy(page_buf, bp->b_un.b_addr, count);
3750 		break;
3751 
3752 	case INQUIRY_USN_PAGE:
3753 		/*
3754 		 * Request for Unit Serial Number page.
3755 		 * Set-up the page.
3756 		 */
3757 		page_buf[0] = peripheral_device_type;
3758 		page_buf[1] = INQUIRY_USN_PAGE;
3759 		page_buf[2] = 0;
3760 		/* remaining page length */
3761 		page_buf[3] = SATA_ID_SERIAL_LEN;
3762 
3763 		/*
3764 		 * Copy serial number from Identify Device data
3765 		 * words into the inquiry page and swap bytes
3766 		 * when necessary.
3767 		 */
3768 		p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser);
3769 #ifdef	_LITTLE_ENDIAN
3770 		swab(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3771 #else
3772 		bcopy(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3773 #endif
3774 		/*
3775 		 * Least significant character of the serial
3776 		 * number shall appear as the last byte,
3777 		 * according to SBC-3 spec.
3778 		 * Count trailing spaces to determine the
3779 		 * necessary shift length.
3780 		 */
3781 		p = &page_buf[SATA_ID_SERIAL_LEN + 4 - 1];
3782 		for (j = 0; j < SATA_ID_SERIAL_LEN; j++) {
3783 			if (*(p - j) != '\0' && *(p - j) != '\040')
3784 				break;
3785 		}
3786 
3787 		/*
3788 		 * Shift SN string right, so that the last
3789 		 * non-blank character would appear in last
3790 		 * byte of SN field in the page.
3791 		 * 'j' is the shift length.
3792 		 */
3793 		for (i = 0; i < (SATA_ID_SERIAL_LEN - j) && j != 0; i++, p--)
3794 			*p = *(p - j);
3795 
3796 		/*
3797 		 * Add leading spaces - same number as the
3798 		 * shift size
3799 		 */
3800 		for (; j > 0; j--)
3801 			page_buf[4 + j - 1] = '\040';
3802 
3803 		count = MIN(bp->b_bcount, SATA_ID_SERIAL_LEN + 4);
3804 		bcopy(page_buf, bp->b_un.b_addr, count);
3805 		break;
3806 
3807 	case INQUIRY_BDC_PAGE:
3808 		/*
3809 		 * Request for Block Device Characteristics
3810 		 * page.  Set-up the page.
3811 		 */
3812 		page_buf[0] = peripheral_device_type;
3813 		page_buf[1] = INQUIRY_BDC_PAGE;
3814 		page_buf[2] = 0;
3815 		/* remaining page length */
3816 		page_buf[3] = SATA_ID_BDC_LEN;
3817 
3818 		rate = sdinfo->satadrv_id.ai_medrotrate;
3819 		page_buf[4] = (rate >> 8) & 0xff;
3820 		page_buf[5] = rate & 0xff;
3821 		page_buf[6] = 0;
3822 		page_buf[7] = sdinfo->satadrv_id.ai_nomformfactor & 0xf;
3823 
3824 		count = MIN(bp->b_bcount, SATA_ID_BDC_LEN + 4);
3825 		bcopy(page_buf, bp->b_un.b_addr, count);
3826 		break;
3827 
3828 	case INQUIRY_ATA_INFO_PAGE:
3829 		/*
3830 		 * Request for ATA Information page.
3831 		 */
3832 		page_buf[0] = peripheral_device_type;
3833 		page_buf[1] = INQUIRY_ATA_INFO_PAGE;
3834 		page_buf[2] = (SATA_ID_ATA_INFO_LEN >> 8) & 0xff;
3835 		page_buf[3] = SATA_ID_ATA_INFO_LEN & 0xff;
3836 		/* page_buf[4-7] reserved */
3837 #ifdef  _LITTLE_ENDIAN
3838 		bcopy("ATA     ", &page_buf[8], 8);
3839 		swab(sdinfo->satadrv_id.ai_model, &page_buf[16], 16);
3840 		if (strncmp(&sdinfo->satadrv_id.ai_fw[4], "    ", 4) == 0) {
3841 			swab(sdinfo->satadrv_id.ai_fw, &page_buf[32], 4);
3842 		} else {
3843 			swab(&sdinfo->satadrv_id.ai_fw[4], &page_buf[32], 4);
3844 		}
3845 #else   /* _LITTLE_ENDIAN */
3846 		bcopy("ATA     ", &page_buf[8], 8);
3847 		bcopy(sdinfo->satadrv_id.ai_model, &page_buf[16], 16);
3848 		if (strncmp(&sdinfo->satadrv_id.ai_fw[4], "    ", 4) == 0) {
3849 			bcopy(sdinfo->satadrv_id.ai_fw, &page_buf[32], 4);
3850 		} else {
3851 			bcopy(&sdinfo->satadrv_id.ai_fw[4], &page_buf[32], 4);
3852 		}
3853 #endif  /* _LITTLE_ENDIAN */
3854 		/*
3855 		 * page_buf[36-55] which defines the device
3856 		 * signature is not defined at this
3857 		 * time.
3858 		 */
3859 
3860 		/* Set the command code */
3861 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
3862 			page_buf[56] = SATAC_ID_DEVICE;
3863 		} else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPI) {
3864 			page_buf[56] = SATAC_ID_PACKET_DEVICE;
3865 		}
3866 		/*
3867 		 * If the command code, page_buf[56], is not
3868 		 * zero and if one of the identify commands
3869 		 * succeeds, return the identify data.
3870 		 */
3871 		if (page_buf[56] != 0) {
3872 			sata_drive_info_t temp_info = {
3873 				.satadrv_addr = sdinfo->satadrv_addr,
3874 				.satadrv_type = sdinfo->satadrv_type,
3875 			};
3876 
3877 			/*
3878 			 * It appears calls to an HBA's start (sata_hba_start)
3879 			 * method (which sata_fetch_device_identify_data_retry()
3880 			 * calls) must not be done while holding cport_mutex.
3881 			 *
3882 			 * A packet's completion routine may call back into
3883 			 * the sata framework and deadlock (and all extant
3884 			 * calls to the HBA's start method either drop and
3885 			 * re-acquire cport_mutex, or never held cport_mutex).
3886 			 *
3887 			 * sdinfo is protected by cport_mutex, so we need to
3888 			 * obtain the SATA address and type from sdinfo
3889 			 * before releasing cport_mutex and submitting the
3890 			 * request. We reacquire cport_mutex to simplfy
3891 			 * cleanup after the done label.
3892 			 */
3893 			mutex_exit(cport_mutex);
3894 			(void) sata_fetch_device_identify_data(
3895 			    spx->txlt_sata_hba_inst, &temp_info);
3896 			mutex_enter(cport_mutex);
3897 
3898 			/*
3899 			 * If sata_fetch_device_identify_data()
3900 			 * fails, the bcopy() is harmless since we're copying
3901 			 * zeros back over zeros. If it succeeds, we're
3902 			 * copying over the portion of the response we need.
3903 			 */
3904 			bcopy(&temp_info.satadrv_id, &page_buf[60],
3905 			    sizeof (sata_id_t));
3906 		}
3907 
3908 		/* Need to copy out the page_buf to bp */
3909 		count = MIN(bp->b_bcount, SATA_ID_ATA_INFO_LEN + 4);
3910 		bcopy(page_buf, bp->b_un.b_addr, count);
3911 		break;
3912 
3913 	case INQUIRY_DEV_IDENTIFICATION_PAGE:
3914 		if (sdinfo->satadrv_id.ai_naa_ieee_oui != 0) {
3915 			/*
3916 			 * Page 83; SAT-5 requires this, and modern
3917 			 * SATA devices all support a WWN.
3918 			 */
3919 			page_buf[0] = peripheral_device_type;
3920 			page_buf[1] = INQUIRY_DEV_IDENTIFICATION_PAGE;
3921 			page_buf[2] = 0;
3922 			page_buf[3] = 12; /* remaining length */
3923 			page_buf[4] = 0x01; /* protocol 0, code set 1 */
3924 			page_buf[5] = 0x03; /* LUN, NAA type */
3925 			page_buf[6] = 0;
3926 			page_buf[7] = 0x08; /* length (64-bit WWN) */
3927 #ifdef	_LITTLE_ENDIAN
3928 			swab(&sdinfo->satadrv_id.ai_naa_ieee_oui, &page_buf[8],
3929 			    8);
3930 #else
3931 			bcopy(&sdinfo->statadrv_id.ai_naa_ieee_oui,
3932 			    &page_buf[8], 8);
3933 #endif
3934 			/* header + designator */
3935 			count = MIN(bp->b_bcount, 12 + 4);
3936 			bcopy(page_buf, bp->b_un.b_addr, count);
3937 			break;
3938 		}
3939 		/* FALLTHROUGH */
3940 
3941 	default:
3942 		/* Request for unsupported VPD page */
3943 		*scsipkt->pkt_scbp = STATUS_CHECK;
3944 		sense = sata_arq_sense(spx);
3945 		sense->es_key = KEY_ILLEGAL_REQUEST;
3946 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3947 		goto done;
3948 	}
3949 
3950 	scsipkt->pkt_state |= STATE_XFERRED_DATA;
3951 	scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3952 	    scsipkt->pkt_cdbp[4] - count : 0;
3953 
3954 done:
3955 	mutex_exit(cport_mutex);
3956 
3957 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3958 	    "Scsi_pkt completion reason %x\n",
3959 	    scsipkt->pkt_reason);
3960 
3961 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3962 	    scsipkt->pkt_comp != NULL) {
3963 		/* scsi callback required */
3964 		if (servicing_interrupt()) {
3965 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3966 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3967 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
3968 			    TASKQID_INVALID) {
3969 				return (TRAN_BUSY);
3970 			}
3971 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3972 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3973 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
3974 			/* Scheduling the callback failed */
3975 			return (TRAN_BUSY);
3976 		}
3977 	}
3978 	return (TRAN_ACCEPT);
3979 }
3980 
3981 /*
3982  * SATA translate command: Request Sense.
3983  *
3984  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3985  * At the moment this is an emulated command (ATA version for SATA hard disks).
3986  * May be translated into Check Power Mode command in the future.
3987  *
3988  * Note: There is a mismatch between already implemented Informational
3989  * Exception Mode Select page 0x1C and this function.
3990  * When MRIE bit is set in page 0x1C, Request Sense is supposed to return
3991  * NO SENSE and set additional sense code to the exception code - this is not
3992  * implemented here.
3993  */
3994 static int
3995 sata_txlt_request_sense(sata_pkt_txlate_t *spx)
3996 {
3997 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3998 	struct scsi_extended_sense sense;
3999 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4000 	sata_drive_info_t *sdinfo;
4001 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4002 	int rval, reason, power_state = 0;
4003 	kmutex_t *cport_mutex;
4004 
4005 	cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4006 	mutex_enter(cport_mutex);
4007 
4008 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
4009 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4010 		mutex_exit(cport_mutex);
4011 		return (rval);
4012 	}
4013 
4014 	scsipkt->pkt_reason = CMD_CMPLT;
4015 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4016 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4017 	*scsipkt->pkt_scbp = STATUS_GOOD;
4018 
4019 	/*
4020 	 * when CONTROL field's NACA bit == 1
4021 	 * return ILLEGAL_REQUEST
4022 	 */
4023 	if (scsipkt->pkt_cdbp[5] & CTL_BYTE_NACA_MASK) {
4024 		mutex_exit(cport_mutex);
4025 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4026 		    SD_SCSI_ASC_CMD_SEQUENCE_ERR));
4027 	}
4028 
4029 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4030 	    &spx->txlt_sata_pkt->satapkt_device);
4031 	ASSERT(sdinfo != NULL);
4032 
4033 	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
4034 
4035 	sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
4036 	scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
4037 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4038 	if (sata_hba_start(spx, &rval) != 0) {
4039 		mutex_exit(cport_mutex);
4040 		return (rval);
4041 	}
4042 	if (scmd->satacmd_error_reg != 0) {
4043 		mutex_exit(cport_mutex);
4044 		return (sata_txlt_check_condition(spx, KEY_NO_SENSE,
4045 		    SD_SCSI_ASC_NO_ADD_SENSE));
4046 	}
4047 
4048 	switch (scmd->satacmd_sec_count_lsb) {
4049 	case SATA_PWRMODE_STANDBY: /* device in standby mode */
4050 		if (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)
4051 			power_state = SATA_POWER_STOPPED;
4052 		else {
4053 			power_state = SATA_POWER_STANDBY;
4054 			sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
4055 		}
4056 		break;
4057 	case SATA_PWRMODE_IDLE: /* device in idle mode */
4058 		power_state = SATA_POWER_IDLE;
4059 		sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4060 		break;
4061 	case SATA_PWRMODE_ACTIVE: /* device in active or idle mode */
4062 	default:		  /* 0x40, 0x41 active mode */
4063 		if (sdinfo->satadrv_power_level == SATA_POWER_IDLE)
4064 			power_state = SATA_POWER_IDLE;
4065 		else {
4066 			power_state = SATA_POWER_ACTIVE;
4067 			sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4068 		}
4069 		break;
4070 	}
4071 
4072 	mutex_exit(cport_mutex);
4073 
4074 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4075 		/*
4076 		 * Because it is fully emulated command storing data
4077 		 * programatically in the specified buffer, release
4078 		 * preallocated DMA resources before storing data in the buffer,
4079 		 * so no unwanted DMA sync would take place.
4080 		 */
4081 		int count = MIN(bp->b_bcount,
4082 		    sizeof (struct scsi_extended_sense));
4083 		sata_scsi_dmafree(NULL, scsipkt);
4084 		bzero(&sense, sizeof (struct scsi_extended_sense));
4085 		sense.es_valid = 0;	/* Valid LBA */
4086 		sense.es_class = 7;	/* Response code 0x70 - current err */
4087 		sense.es_key = KEY_NO_SENSE;
4088 		sense.es_add_len = 6;	/* Additional length */
4089 		/* Copy no more than requested */
4090 		bcopy(&sense, bp->b_un.b_addr, count);
4091 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4092 		scsipkt->pkt_resid = 0;
4093 		switch (power_state) {
4094 		case SATA_POWER_IDLE:
4095 		case SATA_POWER_STANDBY:
4096 			sense.es_add_code =
4097 			    SD_SCSI_ASC_LOW_POWER_CONDITION_ON;
4098 			break;
4099 		case SATA_POWER_STOPPED:
4100 			sense.es_add_code = SD_SCSI_ASC_NO_ADD_SENSE;
4101 			break;
4102 		case SATA_POWER_ACTIVE:
4103 		default:
4104 			break;
4105 		}
4106 	}
4107 
4108 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4109 	    "Scsi_pkt completion reason %x\n",
4110 	    scsipkt->pkt_reason);
4111 
4112 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4113 	    scsipkt->pkt_comp != NULL) {
4114 		/* scsi callback required */
4115 		if (servicing_interrupt()) {
4116 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4117 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4118 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
4119 			    TASKQID_INVALID) {
4120 				return (TRAN_BUSY);
4121 			}
4122 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4123 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4124 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
4125 			/* Scheduling the callback failed */
4126 			return (TRAN_BUSY);
4127 		}
4128 	}
4129 	return (TRAN_ACCEPT);
4130 }
4131 
4132 /*
4133  * SATA translate command: Test Unit Ready
4134  * (ATA version for SATA hard disks).
4135  * It is translated into the Check Power Mode command.
4136  *
4137  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4138  */
4139 static int
4140 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx)
4141 {
4142 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4143 	struct scsi_extended_sense *sense;
4144 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4145 	sata_drive_info_t *sdinfo;
4146 	int power_state;
4147 	int rval, reason;
4148 	kmutex_t *cport_mutex =  &(SATA_TXLT_CPORT_MUTEX(spx));
4149 
4150 	mutex_enter(cport_mutex);
4151 
4152 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
4153 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4154 		mutex_exit(cport_mutex);
4155 		return (rval);
4156 	}
4157 
4158 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4159 	    &spx->txlt_sata_pkt->satapkt_device);
4160 	ASSERT(sdinfo != NULL);
4161 
4162 	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
4163 
4164 	/* send CHECK POWER MODE command */
4165 	sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
4166 	scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
4167 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4168 	if (sata_hba_start(spx, &rval) != 0) {
4169 		mutex_exit(cport_mutex);
4170 		return (rval);
4171 	}
4172 
4173 	if (scmd->satacmd_error_reg != 0) {
4174 		mutex_exit(cport_mutex);
4175 		return (sata_txlt_check_condition(spx, KEY_NOT_READY,
4176 		    SD_SCSI_ASC_LU_NOT_RESPONSE));
4177 	}
4178 
4179 	power_state = scmd->satacmd_sec_count_lsb;
4180 
4181 	/*
4182 	 * return NOT READY when device in STOPPED mode
4183 	 */
4184 	if (power_state == SATA_PWRMODE_STANDBY &&
4185 	    sdinfo->satadrv_power_level == SATA_POWER_STOPPED) {
4186 		*scsipkt->pkt_scbp = STATUS_CHECK;
4187 		sense = sata_arq_sense(spx);
4188 		sense->es_key = KEY_NOT_READY;
4189 		sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY;
4190 	} else {
4191 		/*
4192 		 * For other power mode, return GOOD status
4193 		 */
4194 		*scsipkt->pkt_scbp = STATUS_GOOD;
4195 	}
4196 
4197 	scsipkt->pkt_reason = CMD_CMPLT;
4198 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4199 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4200 
4201 	mutex_exit(cport_mutex);
4202 
4203 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4204 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4205 
4206 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4207 	    scsipkt->pkt_comp != NULL) {
4208 		/* scsi callback required */
4209 		if (servicing_interrupt()) {
4210 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4211 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4212 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
4213 			    TASKQID_INVALID) {
4214 				return (TRAN_BUSY);
4215 			}
4216 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4217 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4218 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
4219 			/* Scheduling the callback failed */
4220 			return (TRAN_BUSY);
4221 		}
4222 	}
4223 
4224 	return (TRAN_ACCEPT);
4225 }
4226 
4227 /*
4228  * SATA translate command: Start Stop Unit
4229  * Translation depends on a command:
4230  *
4231  * Power condition bits will be supported
4232  * and the power level should be maintained by SATL,
4233  * When SATL received a command, it will check the
4234  * power level firstly, and return the status according
4235  * to SAT2 v2.6 and SAT-2 Standby Modifications
4236  *
4237  * SPC-4/SBC-3      SATL    ATA power condition  SATL      SPC/SBC
4238  * -----------------------------------------------------------------------
4239  * SSU_PC1 Active   <==>     ATA  Active         <==>     SSU:start_bit =1
4240  * SSU_PC2 Idle     <==>     ATA  Idle           <==>     N/A
4241  * SSU_PC3 Standby  <==>     ATA  Standby        <==>     N/A
4242  * SSU_PC4 Stopped  <==>     ATA  Standby        <==>     SSU:start_bit = 0
4243  *
4244  *	Unload Media / NOT SUPPORTED YET
4245  *	Load Media / NOT SUPPROTED YET
4246  *	Immediate bit / NOT SUPPORTED YET (deferred error)
4247  *
4248  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4249  * appropriate values in scsi_pkt fields.
4250  */
4251 static int
4252 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx)
4253 {
4254 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4255 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4256 	int rval, reason;
4257 	sata_drive_info_t *sdinfo;
4258 	sata_id_t *sata_id;
4259 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4260 
4261 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4262 	    "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1);
4263 
4264 	mutex_enter(cport_mutex);
4265 
4266 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
4267 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4268 		mutex_exit(cport_mutex);
4269 		return (rval);
4270 	}
4271 
4272 	if (scsipkt->pkt_cdbp[1] & START_STOP_IMMED_MASK) {
4273 		/* IMMED bit - not supported */
4274 		mutex_exit(cport_mutex);
4275 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4276 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4277 	}
4278 
4279 	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
4280 	spx->txlt_sata_pkt->satapkt_comp = NULL;
4281 
4282 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4283 	    &spx->txlt_sata_pkt->satapkt_device);
4284 	ASSERT(sdinfo != NULL);
4285 	sata_id = &sdinfo->satadrv_id;
4286 
4287 	switch ((scsipkt->pkt_cdbp[4] & START_STOP_POWER_COND_MASK) >> 4) {
4288 	case 0:
4289 		if (scsipkt->pkt_cdbp[4] & START_STOP_LOEJ_MASK) {
4290 			/* Load/Unload Media - invalid request */
4291 			goto err_out;
4292 		}
4293 		if (scsipkt->pkt_cdbp[4] & START_STOP_START_MASK) {
4294 			/* Start Unit */
4295 			sata_build_read_verify_cmd(scmd, 1, 5);
4296 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4297 			/* Transfer command to HBA */
4298 			if (sata_hba_start(spx, &rval) != 0) {
4299 				/* Pkt not accepted for execution */
4300 				mutex_exit(cport_mutex);
4301 				return (rval);
4302 			}
4303 			if (scmd->satacmd_error_reg != 0) {
4304 				goto err_out;
4305 			}
4306 			sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4307 		} else {
4308 			/* Stop Unit */
4309 			sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4310 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4311 			if (sata_hba_start(spx, &rval) != 0) {
4312 				mutex_exit(cport_mutex);
4313 				return (rval);
4314 			} else {
4315 				if (scmd->satacmd_error_reg != 0) {
4316 					goto err_out;
4317 				}
4318 			}
4319 			/* ata standby immediate command */
4320 			sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
4321 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4322 			if (sata_hba_start(spx, &rval) != 0) {
4323 				mutex_exit(cport_mutex);
4324 				return (rval);
4325 			}
4326 			if (scmd->satacmd_error_reg != 0) {
4327 				goto err_out;
4328 			}
4329 			sdinfo->satadrv_power_level = SATA_POWER_STOPPED;
4330 		}
4331 		break;
4332 	case 0x1:
4333 		sata_build_generic_cmd(scmd, SATAC_IDLE);
4334 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4335 		if (sata_hba_start(spx, &rval) != 0) {
4336 			mutex_exit(cport_mutex);
4337 			return (rval);
4338 		}
4339 		if (scmd->satacmd_error_reg != 0) {
4340 			goto err_out;
4341 		}
4342 		sata_build_read_verify_cmd(scmd, 1, 5);
4343 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4344 		/* Transfer command to HBA */
4345 		if (sata_hba_start(spx, &rval) != 0) {
4346 			/* Pkt not accepted for execution */
4347 			mutex_exit(cport_mutex);
4348 			return (rval);
4349 		} else {
4350 			if (scmd->satacmd_error_reg != 0) {
4351 				goto err_out;
4352 			}
4353 		}
4354 		sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4355 		break;
4356 	case 0x2:
4357 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4358 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4359 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4360 			if (sata_hba_start(spx, &rval) != 0) {
4361 				mutex_exit(cport_mutex);
4362 				return (rval);
4363 			}
4364 			if (scmd->satacmd_error_reg != 0) {
4365 				goto err_out;
4366 			}
4367 		}
4368 		sata_build_generic_cmd(scmd, SATAC_IDLE);
4369 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4370 		if (sata_hba_start(spx, &rval) != 0) {
4371 			mutex_exit(cport_mutex);
4372 			return (rval);
4373 		}
4374 		if (scmd->satacmd_error_reg != 0) {
4375 			goto err_out;
4376 		}
4377 		if ((scsipkt->pkt_cdbp[3] & START_STOP_MODIFIER_MASK)) {
4378 			/*
4379 			 *  POWER CONDITION MODIFIER bit set
4380 			 *  to 0x1 or larger it will be handled
4381 			 *  on the same way as bit = 0x1
4382 			 */
4383 			if (!(sata_id->ai_cmdset84 &
4384 			    SATA_IDLE_UNLOAD_SUPPORTED)) {
4385 				sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4386 				break;
4387 			}
4388 			sata_build_generic_cmd(scmd, SATAC_IDLE_IM);
4389 			scmd->satacmd_features_reg = 0x44;
4390 			scmd->satacmd_lba_low_lsb = 0x4c;
4391 			scmd->satacmd_lba_mid_lsb = 0x4e;
4392 			scmd->satacmd_lba_high_lsb = 0x55;
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 			}
4398 			if (scmd->satacmd_error_reg != 0) {
4399 				goto err_out;
4400 			}
4401 		}
4402 		sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4403 		break;
4404 	case 0x3:
4405 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4406 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4407 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4408 			if (sata_hba_start(spx, &rval) != 0) {
4409 				mutex_exit(cport_mutex);
4410 				return (rval);
4411 			}
4412 			if (scmd->satacmd_error_reg != 0) {
4413 				goto err_out;
4414 			}
4415 		}
4416 		sata_build_generic_cmd(scmd, SATAC_STANDBY);
4417 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4418 		if (sata_hba_start(spx, &rval) != 0) {
4419 			mutex_exit(cport_mutex);
4420 			return (rval);
4421 		}
4422 		if (scmd->satacmd_error_reg != 0) {
4423 			goto err_out;
4424 		}
4425 		sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
4426 		break;
4427 	case 0x7:
4428 		sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
4429 		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
4430 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4431 		if (sata_hba_start(spx, &rval) != 0) {
4432 			mutex_exit(cport_mutex);
4433 			return (rval);
4434 		}
4435 		if (scmd->satacmd_error_reg != 0) {
4436 			goto err_out;
4437 		}
4438 		switch (scmd->satacmd_sec_count_lsb) {
4439 		case SATA_PWRMODE_STANDBY:
4440 			sata_build_generic_cmd(scmd, SATAC_STANDBY);
4441 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4442 			    sdinfo->satadrv_standby_timer);
4443 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4444 			if (sata_hba_start(spx, &rval) != 0) {
4445 				mutex_exit(cport_mutex);
4446 				return (rval);
4447 			} else {
4448 				if (scmd->satacmd_error_reg != 0) {
4449 					goto err_out;
4450 				}
4451 			}
4452 			break;
4453 		case SATA_PWRMODE_IDLE:
4454 			sata_build_generic_cmd(scmd, SATAC_IDLE);
4455 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4456 			    sdinfo->satadrv_standby_timer);
4457 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4458 			if (sata_hba_start(spx, &rval) != 0) {
4459 				mutex_exit(cport_mutex);
4460 				return (rval);
4461 			} else {
4462 				if (scmd->satacmd_error_reg != 0) {
4463 					goto err_out;
4464 				}
4465 			}
4466 			break;
4467 		case SATA_PWRMODE_ACTIVE_SPINDOWN:
4468 		case SATA_PWRMODE_ACTIVE_SPINUP:
4469 		case SATA_PWRMODE_ACTIVE:
4470 			sata_build_generic_cmd(scmd, SATAC_IDLE);
4471 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4472 			    sdinfo->satadrv_standby_timer);
4473 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4474 			if (sata_hba_start(spx, &rval) != 0) {
4475 				mutex_exit(cport_mutex);
4476 				return (rval);
4477 			}
4478 			if (scmd->satacmd_error_reg != 0) {
4479 				goto err_out;
4480 			}
4481 			sata_build_read_verify_cmd(scmd, 1, 5);
4482 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4483 			if (sata_hba_start(spx, &rval) != 0) {
4484 				mutex_exit(cport_mutex);
4485 				return (rval);
4486 			}
4487 			if (scmd->satacmd_error_reg != 0) {
4488 				goto err_out;
4489 			}
4490 			break;
4491 		default:
4492 			goto err_out;
4493 		}
4494 		break;
4495 	case 0xb:
4496 		if ((sata_get_standby_timer(sdinfo->satadrv_standby_timer) ==
4497 		    0) || (!(sata_id->ai_cap & SATA_STANDBYTIMER))) {
4498 			mutex_exit(cport_mutex);
4499 			return (sata_txlt_check_condition(spx,
4500 			    KEY_ILLEGAL_REQUEST,
4501 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4502 		}
4503 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4504 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4505 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4506 			if (sata_hba_start(spx, &rval) != 0) {
4507 				mutex_exit(cport_mutex);
4508 				return (rval);
4509 			}
4510 			if (scmd->satacmd_error_reg != 0) {
4511 				goto err_out;
4512 			}
4513 			sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
4514 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4515 			if (sata_hba_start(spx, &rval) != 0) {
4516 				mutex_exit(cport_mutex);
4517 				return (rval);
4518 			}
4519 			if (scmd->satacmd_error_reg != 0) {
4520 				goto err_out;
4521 			}
4522 		}
4523 		bzero(sdinfo->satadrv_standby_timer, sizeof (uchar_t) * 4);
4524 		break;
4525 	default:
4526 err_out:
4527 		mutex_exit(cport_mutex);
4528 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4529 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4530 	}
4531 
4532 	/*
4533 	 * Since it was a synchronous command,
4534 	 * a callback function will be called directly.
4535 	 */
4536 	mutex_exit(cport_mutex);
4537 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4538 	    "synchronous execution status %x\n",
4539 	    spx->txlt_sata_pkt->satapkt_reason);
4540 
4541 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4542 	    scsipkt->pkt_comp != NULL) {
4543 		sata_set_arq_data(spx->txlt_sata_pkt);
4544 		if (servicing_interrupt()) {
4545 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4546 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4547 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
4548 			    TASKQID_INVALID) {
4549 				return (TRAN_BUSY);
4550 			}
4551 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4552 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4553 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
4554 			/* Scheduling the callback failed */
4555 			return (TRAN_BUSY);
4556 		}
4557 	}
4558 	else
4559 
4560 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
4561 
4562 	return (TRAN_ACCEPT);
4563 
4564 }
4565 
4566 /*
4567  * SATA translate command:  Read Capacity.
4568  * Emulated command for SATA disks.
4569  * Capacity is retrieved from cached Idenifty Device data.
4570  * Identify Device data shows effective disk capacity, not the native
4571  * capacity, which may be limitted by Set Max Address command.
4572  * This is ATA version for SATA hard disks.
4573  *
4574  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4575  */
4576 static int
4577 sata_txlt_read_capacity(sata_pkt_txlate_t *spx)
4578 {
4579 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4580 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4581 	sata_drive_info_t *sdinfo;
4582 	uint64_t val;
4583 	uint32_t lbsize = DEV_BSIZE;
4584 	uchar_t *rbuf;
4585 	int rval, reason;
4586 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4587 
4588 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4589 	    "sata_txlt_read_capacity: ", NULL);
4590 
4591 	mutex_enter(cport_mutex);
4592 
4593 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4594 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4595 		mutex_exit(cport_mutex);
4596 		return (rval);
4597 	}
4598 
4599 	scsipkt->pkt_reason = CMD_CMPLT;
4600 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4601 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4602 	*scsipkt->pkt_scbp = STATUS_GOOD;
4603 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4604 		/*
4605 		 * Because it is fully emulated command storing data
4606 		 * programatically in the specified buffer, release
4607 		 * preallocated DMA resources before storing data in the buffer,
4608 		 * so no unwanted DMA sync would take place.
4609 		 */
4610 		sata_scsi_dmafree(NULL, scsipkt);
4611 
4612 		sdinfo = sata_get_device_info(
4613 		    spx->txlt_sata_hba_inst,
4614 		    &spx->txlt_sata_pkt->satapkt_device);
4615 
4616 		/*
4617 		 * As per SBC-3, the "returned LBA" is either the highest
4618 		 * addressable LBA or 0xffffffff, whichever is smaller.
4619 		 */
4620 		val = MIN(sdinfo->satadrv_capacity - 1, UINT32_MAX);
4621 
4622 		if (sdinfo->satadrv_id.ai_phys_sect_sz & SATA_L2PS_CHECK_BIT) {
4623 			/* physical/logical sector size word is valid */
4624 
4625 			if (sdinfo->satadrv_id.ai_phys_sect_sz &
4626 			    SATA_L2PS_BIG_SECTORS) {
4627 				/* if this set 117-118 words are valid */
4628 				lbsize = sdinfo->satadrv_id.ai_words_lsec[0] |
4629 				    (sdinfo->satadrv_id.ai_words_lsec[1] << 16);
4630 				lbsize <<= 1; /* convert from words to bytes */
4631 			}
4632 		}
4633 		rbuf = (uchar_t *)bp->b_un.b_addr;
4634 		/* Need to swap endians to match scsi format */
4635 		rbuf[0] = (val >> 24) & 0xff;
4636 		rbuf[1] = (val >> 16) & 0xff;
4637 		rbuf[2] = (val >> 8) & 0xff;
4638 		rbuf[3] = val & 0xff;
4639 		rbuf[4] = (lbsize >> 24) & 0xff;
4640 		rbuf[5] = (lbsize >> 16) & 0xff;
4641 		rbuf[6] = (lbsize >> 8) & 0xff;
4642 		rbuf[7] = lbsize & 0xff;
4643 
4644 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4645 		scsipkt->pkt_resid = 0;
4646 
4647 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n",
4648 		    sdinfo->satadrv_capacity -1);
4649 	}
4650 	mutex_exit(cport_mutex);
4651 	/*
4652 	 * If a callback was requested, do it now.
4653 	 */
4654 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4655 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4656 
4657 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4658 	    scsipkt->pkt_comp != NULL) {
4659 		/* scsi callback required */
4660 		if (servicing_interrupt()) {
4661 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4662 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4663 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
4664 			    TASKQID_INVALID) {
4665 				return (TRAN_BUSY);
4666 			}
4667 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4668 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4669 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
4670 			/* Scheduling the callback failed */
4671 			return (TRAN_BUSY);
4672 		}
4673 	}
4674 
4675 	return (TRAN_ACCEPT);
4676 }
4677 
4678 /*
4679  * SATA translate command:  Read Capacity (16).
4680  * Emulated command for SATA disks.
4681  * Info is retrieved from cached Identify Device data.
4682  * Implemented to SBC-3 (draft 21) and SAT-2 (final) specifications.
4683  *
4684  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4685  */
4686 static int
4687 sata_txlt_read_capacity16(sata_pkt_txlate_t *spx)
4688 {
4689 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4690 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4691 	sata_drive_info_t *sdinfo;
4692 	uint64_t val;
4693 	uint16_t l2p_exp;
4694 	uint32_t lbsize = DEV_BSIZE;
4695 	uchar_t *rbuf;
4696 	int rval, reason;
4697 #define	TPE	0x80
4698 #define	TPRZ	0x40
4699 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4700 
4701 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4702 	    "sata_txlt_read_capacity: ", NULL);
4703 
4704 	mutex_enter(cport_mutex);
4705 
4706 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
4707 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4708 		mutex_exit(cport_mutex);
4709 		return (rval);
4710 	}
4711 
4712 	scsipkt->pkt_reason = CMD_CMPLT;
4713 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4714 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4715 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4716 		/*
4717 		 * Because it is fully emulated command storing data
4718 		 * programatically in the specified buffer, release
4719 		 * preallocated DMA resources before storing data in the buffer,
4720 		 * so no unwanted DMA sync would take place.
4721 		 */
4722 		sata_scsi_dmafree(NULL, scsipkt);
4723 
4724 		/* Check SERVICE ACTION field */
4725 		if ((scsipkt->pkt_cdbp[1] & 0x1f) !=
4726 		    SSVC_ACTION_READ_CAPACITY_G4) {
4727 			mutex_exit(cport_mutex);
4728 			return (sata_txlt_check_condition(spx,
4729 			    KEY_ILLEGAL_REQUEST,
4730 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4731 		}
4732 
4733 		/* Check LBA field */
4734 		if ((scsipkt->pkt_cdbp[2] != 0) ||
4735 		    (scsipkt->pkt_cdbp[3] != 0) ||
4736 		    (scsipkt->pkt_cdbp[4] != 0) ||
4737 		    (scsipkt->pkt_cdbp[5] != 0) ||
4738 		    (scsipkt->pkt_cdbp[6] != 0) ||
4739 		    (scsipkt->pkt_cdbp[7] != 0) ||
4740 		    (scsipkt->pkt_cdbp[8] != 0) ||
4741 		    (scsipkt->pkt_cdbp[9] != 0)) {
4742 			mutex_exit(cport_mutex);
4743 			return (sata_txlt_check_condition(spx,
4744 			    KEY_ILLEGAL_REQUEST,
4745 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4746 		}
4747 
4748 		/* Check PMI bit */
4749 		if (scsipkt->pkt_cdbp[14] & 0x1) {
4750 			mutex_exit(cport_mutex);
4751 			return (sata_txlt_check_condition(spx,
4752 			    KEY_ILLEGAL_REQUEST,
4753 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4754 		}
4755 
4756 		*scsipkt->pkt_scbp = STATUS_GOOD;
4757 
4758 		sdinfo = sata_get_device_info(
4759 		    spx->txlt_sata_hba_inst,
4760 		    &spx->txlt_sata_pkt->satapkt_device);
4761 
4762 		/* last logical block address */
4763 		val = MIN(sdinfo->satadrv_capacity - 1,
4764 		    SCSI_READ_CAPACITY16_MAX_LBA);
4765 
4766 		/* logical to physical block size exponent */
4767 		l2p_exp = 0;
4768 		if (sdinfo->satadrv_id.ai_phys_sect_sz & SATA_L2PS_CHECK_BIT) {
4769 			/* physical/logical sector size word is valid */
4770 
4771 			if (sdinfo->satadrv_id.ai_phys_sect_sz &
4772 			    SATA_L2PS_HAS_MULT) {
4773 				/* multiple logical sectors per phys sectors */
4774 				l2p_exp =
4775 				    sdinfo->satadrv_id.ai_phys_sect_sz &
4776 				    SATA_L2PS_EXP_MASK;
4777 			}
4778 
4779 			if (sdinfo->satadrv_id.ai_phys_sect_sz &
4780 			    SATA_L2PS_BIG_SECTORS) {
4781 				/* if this set 117-118 words are valid */
4782 				lbsize = sdinfo->satadrv_id.ai_words_lsec[0] |
4783 				    (sdinfo->satadrv_id.ai_words_lsec[1] << 16);
4784 				lbsize <<= 1; /* convert from words to bytes */
4785 			}
4786 		}
4787 
4788 		rbuf = (uchar_t *)bp->b_un.b_addr;
4789 		bzero(rbuf, bp->b_bcount);
4790 
4791 		/* returned logical block address */
4792 		rbuf[0] = (val >> 56) & 0xff;
4793 		rbuf[1] = (val >> 48) & 0xff;
4794 		rbuf[2] = (val >> 40) & 0xff;
4795 		rbuf[3] = (val >> 32) & 0xff;
4796 		rbuf[4] = (val >> 24) & 0xff;
4797 		rbuf[5] = (val >> 16) & 0xff;
4798 		rbuf[6] = (val >> 8) & 0xff;
4799 		rbuf[7] = val & 0xff;
4800 		rbuf[8] = (lbsize >> 24) & 0xff;
4801 		rbuf[9] = (lbsize >> 16) & 0xff;
4802 		rbuf[10] = (lbsize >> 8) & 0xff;
4803 		rbuf[11] = lbsize & 0xff;
4804 
4805 		/* p_type, prot_en, unspecified by SAT-2 */
4806 		/* rbuf[12] = 0; */
4807 
4808 		/* p_i_exponent, undefined by SAT-2 */
4809 		/* logical blocks per physical block exponent */
4810 		rbuf[13] = l2p_exp;
4811 
4812 		/*
4813 		 * tpe and tprz as defined in T10/10-079 r0.
4814 		 * TRIM support is indicated by the relevant bit in the data
4815 		 * set management word. Read-after-trim behavior is indicated
4816 		 * by the additional bits in the identify device word. Of the
4817 		 * three defined possibilities, we only flag read-zero.
4818 		 */
4819 		if (sdinfo->satadrv_id.ai_dsm & SATA_DSM_TRIM) {
4820 			rbuf[14] |= TPE;
4821 
4822 			if ((sdinfo->satadrv_id.ai_addsupported &
4823 			    SATA_DETERMINISTIC_READ) &&
4824 			    (sdinfo->satadrv_id.ai_addsupported &
4825 			    SATA_READ_ZERO)) {
4826 				rbuf[14] |= TPRZ;
4827 			}
4828 		}
4829 
4830 		/* lowest aligned logical block address = 0 (for now) */
4831 		/* rbuf[15] = 0; */
4832 
4833 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4834 		scsipkt->pkt_resid = 0;
4835 
4836 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%llu\n",
4837 		    sdinfo->satadrv_capacity -1);
4838 	}
4839 
4840 	mutex_exit(cport_mutex);
4841 
4842 	/*
4843 	 * If a callback was requested, do it now.
4844 	 */
4845 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4846 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4847 
4848 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4849 	    scsipkt->pkt_comp != NULL) {
4850 		/* scsi callback required */
4851 		if (servicing_interrupt()) {
4852 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4853 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4854 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
4855 			    TASKQID_INVALID) {
4856 				return (TRAN_BUSY);
4857 			}
4858 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4859 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4860 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
4861 			/* Scheduling the callback failed */
4862 			return (TRAN_BUSY);
4863 		}
4864 	}
4865 
4866 	return (TRAN_ACCEPT);
4867 }
4868 
4869 /*
4870  * Translate command: UNMAP
4871  *
4872  * The function cannot be called in interrupt context since it may sleep.
4873  */
4874 static int
4875 sata_txlt_unmap(sata_pkt_txlate_t *spx)
4876 {
4877 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4878 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4879 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4880 	uint16_t count = 0;
4881 	int synch;
4882 	int rval, reason;
4883 	int i, x;
4884 	int bdlen = 0;
4885 	int ranges = 0;
4886 	int paramlen = 8;
4887 	uint8_t *data, *tmpbd;
4888 	sata_drive_info_t *sdinfo;
4889 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
4890 #define	TRIM	0x1
4891 
4892 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4893 	    "sata_txlt_unmap: ", NULL);
4894 
4895 	mutex_enter(cport_mutex);
4896 
4897 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4898 	    &spx->txlt_sata_pkt->satapkt_device);
4899 	if (sdinfo != NULL) {
4900 		SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4901 		    "DSM support 0x%x, max number of 512 byte blocks of LBA "
4902 		    "range entries 0x%x\n", sdinfo->satadrv_id.ai_dsm,
4903 		    sdinfo->satadrv_id.ai_maxcount);
4904 	}
4905 
4906 	rval = sata_txlt_generic_pkt_info(spx, &reason, 1);
4907 	if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4908 		mutex_exit(cport_mutex);
4909 		return (rval);
4910 	}
4911 
4912 	/*
4913 	 * Need to modify bp to have TRIM data instead of UNMAP data.
4914 	 * Start by getting the block descriptor data length by subtracting
4915 	 * the 8 byte parameter list header from the parameter list length.
4916 	 * The block descriptor size has to be a multiple of 16 bytes.
4917 	 */
4918 	bdlen = scsipkt->pkt_cdbp[7];
4919 	bdlen = (bdlen << 8) + scsipkt->pkt_cdbp[8] - paramlen;
4920 	if ((bdlen < 0) || ((bdlen % 16) != 0) ||
4921 	    ((bp != NULL) && (bdlen > (bp->b_bcount - paramlen)))) {
4922 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4923 		    "sata_txlt_unmap: invalid block descriptor length", NULL);
4924 		mutex_exit(cport_mutex);
4925 		return ((sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4926 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB)));
4927 	}
4928 	/*
4929 	 * If there are no parameter data or block descriptors, it is not
4930 	 * considered an error so just complete the command without sending
4931 	 * TRIM.
4932 	 */
4933 	if ((bdlen == 0) || (bp == NULL) || (bp->b_un.b_addr == NULL) ||
4934 	    (bp->b_bcount == 0)) {
4935 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4936 		    "sata_txlt_unmap: no parameter data or block descriptors",
4937 		    NULL);
4938 		mutex_exit(cport_mutex);
4939 		return (sata_txlt_unmap_nodata_cmd(spx));
4940 	}
4941 	tmpbd = (uint8_t *)bp->b_un.b_addr + paramlen;
4942 	data = kmem_zalloc(bdlen, KM_SLEEP);
4943 
4944 	/*
4945 	 * Loop through all the UNMAP block descriptors and convert the data
4946 	 * into TRIM format.
4947 	 */
4948 	for (i = 0, x = 0; i < bdlen; i += 16, x += 8) {
4949 		/* get range length */
4950 		data[x] = tmpbd[i+7];
4951 		data[x+1] = tmpbd[i+6];
4952 		/* get LBA */
4953 		data[x+2] = tmpbd[i+5];
4954 		data[x+3] = tmpbd[i+4];
4955 		data[x+4] = tmpbd[i+3];
4956 		data[x+5] = tmpbd[i+2];
4957 		data[x+6] = tmpbd[i+11];
4958 		data[x+7] = tmpbd[i+10];
4959 
4960 		ranges++;
4961 	}
4962 
4963 	/*
4964 	 * The TRIM command expects the data buffer to be a multiple of
4965 	 * 512-byte blocks of range entries.  This means that the UNMAP buffer
4966 	 * may be too small.  Free the original DMA resources and create a
4967 	 * local buffer.
4968 	 */
4969 	sata_common_free_dma_rsrcs(spx);
4970 
4971 	/*
4972 	 * Get count of 512-byte blocks of range entries.  The length
4973 	 * of a range entry is 8 bytes which means one count has 64 range
4974 	 * entries.
4975 	 */
4976 	count = (ranges + 63)/64;
4977 
4978 	/* Allocate a buffer that is a multiple of 512 bytes. */
4979 	mutex_exit(cport_mutex);
4980 	bp = sata_alloc_local_buffer(spx, count * 512);
4981 	if (bp == NULL) {
4982 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
4983 		    "sata_txlt_unmap: "
4984 		    "cannot allocate buffer for TRIM command", NULL);
4985 		kmem_free(data, bdlen);
4986 		return (TRAN_BUSY);
4987 	}
4988 	bp_mapin(bp); /* make data buffer accessible */
4989 	mutex_enter(cport_mutex);
4990 
4991 	bzero(bp->b_un.b_addr, bp->b_bcount);
4992 	bcopy(data, bp->b_un.b_addr, x);
4993 	kmem_free(data, bdlen);
4994 	rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
4995 	    DDI_DMA_SYNC_FORDEV);
4996 	ASSERT(rval == DDI_SUCCESS);
4997 
4998 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4999 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
5000 	scmd->satacmd_cmd_reg = SATAC_DSM;
5001 	scmd->satacmd_sec_count_msb = (count >> 8) & 0xff;
5002 	scmd->satacmd_sec_count_lsb = count & 0xff;
5003 	scmd->satacmd_features_reg = TRIM;
5004 	scmd->satacmd_device_reg = SATA_ADH_LBA;
5005 	scmd->satacmd_status_reg = 0;
5006 	scmd->satacmd_error_reg = 0;
5007 
5008 	/* Start processing command */
5009 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5010 		spx->txlt_sata_pkt->satapkt_comp =
5011 		    sata_txlt_unmap_completion;
5012 		synch = FALSE;
5013 	} else {
5014 		synch = TRUE;
5015 	}
5016 
5017 	if (sata_hba_start(spx, &rval) != 0) {
5018 		mutex_exit(cport_mutex);
5019 		return (rval);
5020 	}
5021 
5022 	mutex_exit(cport_mutex);
5023 
5024 	if (synch) {
5025 		sata_txlt_unmap_completion(spx->txlt_sata_pkt);
5026 	}
5027 
5028 	return (TRAN_ACCEPT);
5029 }
5030 
5031 /*
5032  * SATA translate command: Mode Sense.
5033  * Translated into appropriate SATA command or emulated.
5034  * Saved Values Page Control (03) are not supported.
5035  *
5036  * NOTE: only caching mode sense page is currently implemented.
5037  *
5038  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
5039  */
5040 
5041 #define	LLBAA	0x10	/* Long LBA Accepted */
5042 
5043 static int
5044 sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
5045 {
5046 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
5047 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5048 	sata_drive_info_t *sdinfo;
5049 	sata_id_t *sata_id;
5050 	struct scsi_extended_sense *sense;
5051 	int		len, bdlen, count, alc_len;
5052 	int		pc;	/* Page Control code */
5053 	uint8_t		*buf;	/* mode sense buffer */
5054 	int		rval, reason;
5055 	kmutex_t	*cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5056 
5057 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5058 	    "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
5059 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5060 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5061 
5062 	if (servicing_interrupt()) {
5063 		buf = kmem_zalloc(1024, KM_NOSLEEP);
5064 		if (buf == NULL) {
5065 			return (TRAN_BUSY);
5066 		}
5067 	} else {
5068 		buf = kmem_zalloc(1024, KM_SLEEP);
5069 	}
5070 
5071 	mutex_enter(cport_mutex);
5072 
5073 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
5074 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5075 		mutex_exit(cport_mutex);
5076 		kmem_free(buf, 1024);
5077 		return (rval);
5078 	}
5079 
5080 	scsipkt->pkt_reason = CMD_CMPLT;
5081 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5082 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5083 
5084 	pc = scsipkt->pkt_cdbp[2] >> 6;
5085 
5086 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
5087 		/*
5088 		 * Because it is fully emulated command storing data
5089 		 * programatically in the specified buffer, release
5090 		 * preallocated DMA resources before storing data in the buffer,
5091 		 * so no unwanted DMA sync would take place.
5092 		 */
5093 		sata_scsi_dmafree(NULL, scsipkt);
5094 
5095 		len = 0;
5096 		bdlen = 0;
5097 		if (!(scsipkt->pkt_cdbp[1] & 8)) {
5098 			if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
5099 			    (scsipkt->pkt_cdbp[1] & LLBAA))
5100 				bdlen = 16;
5101 			else
5102 				bdlen = 8;
5103 		}
5104 		/* Build mode parameter header */
5105 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5106 			/* 4-byte mode parameter header */
5107 			buf[len++] = 0;		/* mode data length */
5108 			buf[len++] = 0;		/* medium type */
5109 			buf[len++] = 0;		/* dev-specific param */
5110 			buf[len++] = bdlen;	/* Block Descriptor length */
5111 		} else {
5112 			/* 8-byte mode parameter header */
5113 			buf[len++] = 0;		/* mode data length */
5114 			buf[len++] = 0;
5115 			buf[len++] = 0;		/* medium type */
5116 			buf[len++] = 0;		/* dev-specific param */
5117 			if (bdlen == 16)
5118 				buf[len++] = 1;	/* long lba descriptor */
5119 			else
5120 				buf[len++] = 0;
5121 			buf[len++] = 0;
5122 			buf[len++] = 0;		/* Block Descriptor length */
5123 			buf[len++] = bdlen;
5124 		}
5125 
5126 		sdinfo = sata_get_device_info(
5127 		    spx->txlt_sata_hba_inst,
5128 		    &spx->txlt_sata_pkt->satapkt_device);
5129 
5130 		/* Build block descriptor only if not disabled (DBD) */
5131 		if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
5132 			/* Block descriptor - direct-access device format */
5133 			if (bdlen == 8) {
5134 				/* build regular block descriptor */
5135 				buf[len++] =
5136 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
5137 				buf[len++] =
5138 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
5139 				buf[len++] =
5140 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
5141 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
5142 				buf[len++] = 0; /* density code */
5143 				buf[len++] = 0;
5144 				if (sdinfo->satadrv_type ==
5145 				    SATA_DTYPE_ATADISK)
5146 					buf[len++] = 2;
5147 				else
5148 					/* ATAPI */
5149 					buf[len++] = 8;
5150 				buf[len++] = 0;
5151 			} else if (bdlen == 16) {
5152 				/* Long LBA Accepted */
5153 				/* build long lba block descriptor */
5154 #ifndef __lock_lint
5155 				buf[len++] =
5156 				    (sdinfo->satadrv_capacity >> 56) & 0xff;
5157 				buf[len++] =
5158 				    (sdinfo->satadrv_capacity >> 48) & 0xff;
5159 				buf[len++] =
5160 				    (sdinfo->satadrv_capacity >> 40) & 0xff;
5161 				buf[len++] =
5162 				    (sdinfo->satadrv_capacity >> 32) & 0xff;
5163 #endif
5164 				buf[len++] =
5165 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
5166 				buf[len++] =
5167 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
5168 				buf[len++] =
5169 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
5170 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
5171 				buf[len++] = 0;
5172 				buf[len++] = 0; /* density code */
5173 				buf[len++] = 0;
5174 				buf[len++] = 0;
5175 				if (sdinfo->satadrv_type ==
5176 				    SATA_DTYPE_ATADISK)
5177 					buf[len++] = 2;
5178 				else
5179 					/* ATAPI */
5180 					buf[len++] = 8;
5181 				buf[len++] = 0;
5182 			}
5183 		}
5184 
5185 		sata_id = &sdinfo->satadrv_id;
5186 
5187 		/*
5188 		 * Add requested pages.
5189 		 * Page 3 and 4 are obsolete and we are not supporting them.
5190 		 * We deal now with:
5191 		 * caching (read/write cache control).
5192 		 * We should eventually deal with following mode pages:
5193 		 * error recovery  (0x01),
5194 		 * power condition (0x1a),
5195 		 * exception control page (enables SMART) (0x1c),
5196 		 * enclosure management (ses),
5197 		 * protocol-specific port mode (port control).
5198 		 */
5199 		switch (scsipkt->pkt_cdbp[2] & 0x3f) {
5200 		case MODEPAGE_RW_ERRRECOV:
5201 			/* DAD_MODE_ERR_RECOV */
5202 			/* R/W recovery */
5203 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
5204 			break;
5205 		case MODEPAGE_CACHING:
5206 			/* DAD_MODE_CACHE */
5207 			/* Reject not supported request for saved parameters */
5208 			if (pc == 3) {
5209 				*scsipkt->pkt_scbp = STATUS_CHECK;
5210 				sense = sata_arq_sense(spx);
5211 				sense->es_key = KEY_ILLEGAL_REQUEST;
5212 				sense->es_add_code =
5213 				    SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
5214 				goto done;
5215 			}
5216 
5217 			/* caching */
5218 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
5219 			break;
5220 		case MODEPAGE_INFO_EXCPT:
5221 			/* exception cntrl */
5222 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
5223 				len += sata_build_msense_page_1c(sdinfo, pc,
5224 				    buf+len);
5225 			}
5226 			else
5227 				goto err;
5228 			break;
5229 		case MODEPAGE_POWER_COND:
5230 			/* DAD_MODE_POWER_COND */
5231 			/* power condition */
5232 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
5233 			break;
5234 
5235 		case MODEPAGE_ACOUSTIC_MANAG:
5236 			/* acoustic management */
5237 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
5238 			break;
5239 		case MODEPAGE_ALLPAGES:
5240 			/* all pages */
5241 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
5242 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
5243 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
5244 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
5245 				len += sata_build_msense_page_1c(sdinfo, pc,
5246 				    buf+len);
5247 			}
5248 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
5249 			break;
5250 		default:
5251 		err:
5252 			/* Invalid request */
5253 			*scsipkt->pkt_scbp = STATUS_CHECK;
5254 			sense = sata_arq_sense(spx);
5255 			sense->es_key = KEY_ILLEGAL_REQUEST;
5256 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5257 			goto done;
5258 		}
5259 
5260 		/* fix total mode data length */
5261 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5262 			/* 4-byte mode parameter header */
5263 			buf[0] = len - 1;	/* mode data length */
5264 		} else {
5265 			buf[0] = (len -2) >> 8;
5266 			buf[1] = (len -2) & 0xff;
5267 		}
5268 
5269 
5270 		/* Check allocation length */
5271 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
5272 			alc_len = scsipkt->pkt_cdbp[4];
5273 		} else {
5274 			alc_len = scsipkt->pkt_cdbp[7];
5275 			alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
5276 		}
5277 		/*
5278 		 * We do not check for possible parameters truncation
5279 		 * (alc_len < len) assuming that the target driver works
5280 		 * correctly. Just avoiding overrun.
5281 		 * Copy no more than requested and possible, buffer-wise.
5282 		 */
5283 		count = MIN(alc_len, len);
5284 		count = MIN(bp->b_bcount, count);
5285 		bcopy(buf, bp->b_un.b_addr, count);
5286 
5287 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
5288 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
5289 	}
5290 	*scsipkt->pkt_scbp = STATUS_GOOD;
5291 done:
5292 	mutex_exit(cport_mutex);
5293 	(void) kmem_free(buf, 1024);
5294 
5295 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5296 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5297 
5298 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5299 	    scsipkt->pkt_comp != NULL) {
5300 		/* scsi callback required */
5301 		if (servicing_interrupt()) {
5302 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5303 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5304 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
5305 			    TASKQID_INVALID) {
5306 				return (TRAN_BUSY);
5307 			}
5308 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5309 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5310 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
5311 			/* Scheduling the callback failed */
5312 			return (TRAN_BUSY);
5313 		}
5314 	}
5315 
5316 	return (TRAN_ACCEPT);
5317 }
5318 
5319 
5320 /*
5321  * SATA translate command: Mode Select.
5322  * Translated into appropriate SATA command or emulated.
5323  * Saving parameters is not supported.
5324  * Changing device capacity is not supported (although theoretically
5325  * possible by executing SET FEATURES/SET MAX ADDRESS)
5326  *
5327  * Assumption is that the target driver is working correctly.
5328  *
5329  * More than one SATA command may be executed to perform operations specified
5330  * by mode select pages. The first error terminates further execution.
5331  * Operations performed successully are not backed-up in such case.
5332  *
5333  * NOTE: Implemented pages:
5334  * - caching page
5335  * - informational exception page
5336  * - acoustic management page
5337  * - power condition page
5338  * Caching setup is remembered so it could be re-stored in case of
5339  * an unexpected device reset.
5340  *
5341  * Returns TRAN_XXXX.
5342  * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields.
5343  */
5344 
5345 static int
5346 sata_txlt_mode_select(sata_pkt_txlate_t *spx)
5347 {
5348 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5349 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5350 	struct scsi_extended_sense *sense;
5351 	int len, pagelen, count, pllen;
5352 	uint8_t *buf;	/* mode select buffer */
5353 	int rval, stat, reason;
5354 	uint_t nointr_flag;
5355 	int dmod = 0;
5356 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5357 
5358 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5359 	    "sata_txlt_mode_select, pc %x page code 0x%02x\n",
5360 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5361 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5362 
5363 	mutex_enter(cport_mutex);
5364 
5365 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
5366 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5367 		mutex_exit(cport_mutex);
5368 		return (rval);
5369 	}
5370 
5371 	rval = TRAN_ACCEPT;
5372 
5373 	scsipkt->pkt_reason = CMD_CMPLT;
5374 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5375 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5376 	nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
5377 
5378 	/* Reject not supported request */
5379 	if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
5380 		*scsipkt->pkt_scbp = STATUS_CHECK;
5381 		sense = sata_arq_sense(spx);
5382 		sense->es_key = KEY_ILLEGAL_REQUEST;
5383 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5384 		goto done;
5385 	}
5386 
5387 	if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
5388 		pllen = scsipkt->pkt_cdbp[4];
5389 	} else {
5390 		pllen = scsipkt->pkt_cdbp[7];
5391 		pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
5392 	}
5393 
5394 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
5395 
5396 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
5397 		buf = (uint8_t *)bp->b_un.b_addr;
5398 		count = MIN(bp->b_bcount, pllen);
5399 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
5400 		scsipkt->pkt_resid = 0;
5401 		pllen = count;
5402 
5403 		/*
5404 		 * Check the header to skip the block descriptor(s) - we
5405 		 * do not support setting device capacity.
5406 		 * Existing macros do not recognize long LBA dscriptor,
5407 		 * hence manual calculation.
5408 		 */
5409 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
5410 			/* 6-bytes CMD, 4 bytes header */
5411 			if (count <= 4)
5412 				goto done;		/* header only */
5413 			len = buf[3] + 4;
5414 		} else {
5415 			/* 10-bytes CMD, 8 bytes header */
5416 			if (count <= 8)
5417 				goto done;		/* header only */
5418 			len = buf[6];
5419 			len = (len << 8) + buf[7] + 8;
5420 		}
5421 		if (len >= count)
5422 			goto done;	/* header + descriptor(s) only */
5423 
5424 		pllen -= len;		/* remaining data length */
5425 
5426 		/*
5427 		 * We may be executing SATA command and want to execute it
5428 		 * in SYNCH mode, regardless of scsi_pkt setting.
5429 		 * Save scsi_pkt setting and indicate SYNCH mode
5430 		 */
5431 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5432 		    scsipkt->pkt_comp != NULL) {
5433 			scsipkt->pkt_flags |= FLAG_NOINTR;
5434 		}
5435 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
5436 
5437 		/*
5438 		 * len is now the offset to a first mode select page
5439 		 * Process all pages
5440 		 */
5441 		while (pllen > 0) {
5442 			switch ((int)buf[len]) {
5443 			case MODEPAGE_CACHING:
5444 				/* No support for SP (saving) */
5445 				if (scsipkt->pkt_cdbp[1] & 0x01) {
5446 					*scsipkt->pkt_scbp = STATUS_CHECK;
5447 					sense = sata_arq_sense(spx);
5448 					sense->es_key = KEY_ILLEGAL_REQUEST;
5449 					sense->es_add_code =
5450 					    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5451 					goto done;
5452 				}
5453 				stat = sata_mode_select_page_8(spx,
5454 				    (struct mode_cache_scsi3 *)&buf[len],
5455 				    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 				break;
5476 
5477 			case MODEPAGE_INFO_EXCPT:
5478 				stat = sata_mode_select_page_1c(spx,
5479 				    (struct mode_info_excpt_page *)&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 
5502 			case MODEPAGE_ACOUSTIC_MANAG:
5503 				stat = sata_mode_select_page_30(spx,
5504 				    (struct mode_acoustic_management *)
5505 				    &buf[len], pllen, &pagelen, &rval, &dmod);
5506 				/*
5507 				 * The pagelen value indicates the number of
5508 				 * parameter bytes already processed.
5509 				 * The rval is the return value from
5510 				 * sata_tran_start().
5511 				 * The stat indicates the overall status of
5512 				 * the operation(s).
5513 				 */
5514 				if (stat != SATA_SUCCESS)
5515 					/*
5516 					 * Page processing did not succeed -
5517 					 * all error info is already set-up,
5518 					 * just return
5519 					 */
5520 					pllen = 0; /* this breaks the loop */
5521 				else {
5522 					len += pagelen;
5523 					pllen -= pagelen;
5524 				}
5525 
5526 				break;
5527 			case MODEPAGE_POWER_COND:
5528 				stat = sata_mode_select_page_1a(spx,
5529 				    (struct mode_info_power_cond *)&buf[len],
5530 				    pllen, &pagelen, &rval, &dmod);
5531 				/*
5532 				 * The pagelen value indicates the number of
5533 				 * parameter bytes already processed.
5534 				 * The rval is the return value from
5535 				 * sata_tran_start().
5536 				 * The stat indicates the overall status of
5537 				 * the operation(s).
5538 				 */
5539 				if (stat != SATA_SUCCESS)
5540 					/*
5541 					 * Page processing did not succeed -
5542 					 * all error info is already set-up,
5543 					 * just return
5544 					 */
5545 					pllen = 0; /* this breaks the loop */
5546 				else {
5547 					len += pagelen;
5548 					pllen -= pagelen;
5549 				}
5550 				break;
5551 			default:
5552 				*scsipkt->pkt_scbp = STATUS_CHECK;
5553 				sense = sata_arq_sense(spx);
5554 				sense->es_key = KEY_ILLEGAL_REQUEST;
5555 				sense->es_add_code =
5556 				    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
5557 				goto done;
5558 			}
5559 		}
5560 	}
5561 done:
5562 	mutex_exit(cport_mutex);
5563 	/*
5564 	 * If device parameters were modified, fetch and store the new
5565 	 * Identify Device data. Since port mutex could have been released
5566 	 * for accessing HBA driver, we need to re-check device existence.
5567 	 */
5568 	if (dmod != 0) {
5569 		sata_drive_info_t new_sdinfo, *sdinfo;
5570 		int rv = 0;
5571 
5572 		/*
5573 		 * Following statement has to be changed if this function is
5574 		 * used for devices other than SATA hard disks.
5575 		 */
5576 		new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
5577 
5578 		new_sdinfo.satadrv_addr =
5579 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr;
5580 		rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
5581 		    &new_sdinfo);
5582 
5583 		mutex_enter(cport_mutex);
5584 		/*
5585 		 * Since port mutex could have been released when
5586 		 * accessing HBA driver, we need to re-check that the
5587 		 * framework still holds the device info structure.
5588 		 */
5589 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
5590 		    &spx->txlt_sata_pkt->satapkt_device);
5591 		if (sdinfo != NULL) {
5592 			/*
5593 			 * Device still has info structure in the
5594 			 * sata framework. Copy newly fetched info
5595 			 */
5596 			if (rv == 0) {
5597 				sdinfo->satadrv_id = new_sdinfo.satadrv_id;
5598 				sata_save_drive_settings(sdinfo);
5599 			} else {
5600 				/*
5601 				 * Could not fetch new data - invalidate
5602 				 * sata_drive_info. That makes device
5603 				 * unusable.
5604 				 */
5605 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
5606 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
5607 			}
5608 		}
5609 		if (rv != 0 || sdinfo == NULL) {
5610 			/*
5611 			 * This changes the overall mode select completion
5612 			 * reason to a failed one !!!!!
5613 			 */
5614 			*scsipkt->pkt_scbp = STATUS_CHECK;
5615 			sense = sata_arq_sense(spx);
5616 			scsipkt->pkt_reason = CMD_INCOMPLETE;
5617 			rval = TRAN_ACCEPT;
5618 		}
5619 		mutex_exit(cport_mutex);
5620 	}
5621 	/* Restore the scsi pkt flags */
5622 	scsipkt->pkt_flags &= ~FLAG_NOINTR;
5623 	scsipkt->pkt_flags |= nointr_flag;
5624 
5625 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5626 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5627 
5628 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5629 	    scsipkt->pkt_comp != NULL) {
5630 		/* scsi callback required */
5631 		if (servicing_interrupt()) {
5632 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5633 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5634 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
5635 			    TASKQID_INVALID) {
5636 				return (TRAN_BUSY);
5637 			}
5638 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5639 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
5640 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
5641 			/* Scheduling the callback failed */
5642 			return (TRAN_BUSY);
5643 		}
5644 	}
5645 
5646 	return (rval);
5647 }
5648 
5649 /*
5650  * Translate command: ATA Pass Through
5651  * Incomplete implementation.  Only supports No-Data, PIO Data-In, and
5652  * PIO Data-Out protocols.  Also supports CK_COND bit.
5653  *
5654  * Mapping of the incoming CDB bytes to the outgoing satacmd bytes is
5655  * described in Table 111 of SAT-2 (Draft 9).
5656  */
5657 static  int
5658 sata_txlt_ata_pass_thru(sata_pkt_txlate_t *spx)
5659 {
5660 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5661 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5662 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5663 	int extend;
5664 	uint64_t lba;
5665 	uint16_t feature, sec_count;
5666 	int t_len, synch;
5667 	int rval, reason;
5668 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5669 
5670 	mutex_enter(cport_mutex);
5671 
5672 	rval = sata_txlt_generic_pkt_info(spx, &reason, 1);
5673 	if ((rval != TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5674 		mutex_exit(cport_mutex);
5675 		return (rval);
5676 	}
5677 
5678 	/* T_DIR bit */
5679 	if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_T_DIR)
5680 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
5681 	else
5682 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
5683 
5684 	/* MULTIPLE_COUNT field.  If non-zero, invalid command (for now). */
5685 	if (((scsipkt->pkt_cdbp[1] >> 5) & 0x7) != 0) {
5686 		mutex_exit(cport_mutex);
5687 		return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5688 	}
5689 
5690 	/* OFFLINE field. If non-zero, invalid command (for now). */
5691 	if (((scsipkt->pkt_cdbp[2] >> 6) & 0x3) != 0) {
5692 		mutex_exit(cport_mutex);
5693 		return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5694 	}
5695 
5696 	/* PROTOCOL field */
5697 	switch ((scsipkt->pkt_cdbp[1] >> 1) & 0xf) {
5698 	case SATL_APT_P_HW_RESET:
5699 	case SATL_APT_P_SRST:
5700 	case SATL_APT_P_DMA:
5701 	case SATL_APT_P_DMA_QUEUED:
5702 	case SATL_APT_P_DEV_DIAG:
5703 	case SATL_APT_P_DEV_RESET:
5704 	case SATL_APT_P_UDMA_IN:
5705 	case SATL_APT_P_UDMA_OUT:
5706 	case SATL_APT_P_FPDMA:
5707 	case SATL_APT_P_RET_RESP:
5708 		/* Not yet implemented */
5709 	default:
5710 		mutex_exit(cport_mutex);
5711 		return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5712 
5713 	case SATL_APT_P_NON_DATA:
5714 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
5715 		break;
5716 
5717 	case SATL_APT_P_PIO_DATA_IN:
5718 		/* If PROTOCOL disagrees with T_DIR, invalid command */
5719 		if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_WRITE) {
5720 			mutex_exit(cport_mutex);
5721 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5722 		}
5723 
5724 		/* if there is a buffer, release its DMA resources */
5725 		if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) {
5726 			sata_scsi_dmafree(NULL, scsipkt);
5727 		} else {
5728 			/* if there is no buffer, how do you PIO in? */
5729 			mutex_exit(cport_mutex);
5730 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5731 		}
5732 
5733 		break;
5734 
5735 	case SATL_APT_P_PIO_DATA_OUT:
5736 		/* If PROTOCOL disagrees with T_DIR, invalid command */
5737 		if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_READ) {
5738 			mutex_exit(cport_mutex);
5739 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5740 		}
5741 
5742 		/* if there is a buffer, release its DMA resources */
5743 		if ((bp != NULL) && bp->b_un.b_addr && bp->b_bcount) {
5744 			sata_scsi_dmafree(NULL, scsipkt);
5745 		} else {
5746 			/* if there is no buffer, how do you PIO out? */
5747 			mutex_exit(cport_mutex);
5748 			return (sata_txlt_ata_pass_thru_illegal_cmd(spx));
5749 		}
5750 
5751 		break;
5752 	}
5753 
5754 	/* Parse the ATA cmd fields, transfer some straight to the satacmd */
5755 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
5756 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH12:
5757 		feature = scsipkt->pkt_cdbp[3];
5758 
5759 		sec_count = scsipkt->pkt_cdbp[4];
5760 
5761 		lba = scsipkt->pkt_cdbp[8] & 0xf;
5762 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5763 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
5764 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5765 
5766 		scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] & 0xf0;
5767 		scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[9];
5768 
5769 		break;
5770 
5771 	case SPC3_CMD_ATA_COMMAND_PASS_THROUGH16:
5772 		if (scsipkt->pkt_cdbp[1] & SATL_APT_BM_EXTEND) {
5773 			extend = 1;
5774 
5775 			feature = scsipkt->pkt_cdbp[3];
5776 			feature = (feature << 8) | scsipkt->pkt_cdbp[4];
5777 
5778 			sec_count = scsipkt->pkt_cdbp[5];
5779 			sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[6];
5780 
5781 			lba = scsipkt->pkt_cdbp[11];
5782 			lba = (lba << 8) | scsipkt->pkt_cdbp[12];
5783 			lba = (lba << 8) | scsipkt->pkt_cdbp[9];
5784 			lba = (lba << 8) | scsipkt->pkt_cdbp[10];
5785 			lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5786 			lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5787 
5788 			scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13];
5789 			scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14];
5790 		} else {
5791 			feature = scsipkt->pkt_cdbp[3];
5792 
5793 			sec_count = scsipkt->pkt_cdbp[5];
5794 
5795 			lba = scsipkt->pkt_cdbp[13] & 0xf;
5796 			lba = (lba << 8) | scsipkt->pkt_cdbp[12];
5797 			lba = (lba << 8) | scsipkt->pkt_cdbp[10];
5798 			lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5799 
5800 			scmd->satacmd_device_reg = scsipkt->pkt_cdbp[13] &
5801 			    0xf0;
5802 			scmd->satacmd_cmd_reg = scsipkt->pkt_cdbp[14];
5803 		}
5804 
5805 		break;
5806 	}
5807 
5808 	/* CK_COND bit */
5809 	if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) {
5810 		if (extend) {
5811 			scmd->satacmd_flags.sata_copy_out_sec_count_msb = 1;
5812 			scmd->satacmd_flags.sata_copy_out_lba_low_msb = 1;
5813 			scmd->satacmd_flags.sata_copy_out_lba_mid_msb = 1;
5814 			scmd->satacmd_flags.sata_copy_out_lba_high_msb = 1;
5815 		}
5816 
5817 		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1;
5818 		scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1;
5819 		scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1;
5820 		scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1;
5821 		scmd->satacmd_flags.sata_copy_out_device_reg = 1;
5822 		scmd->satacmd_flags.sata_copy_out_error_reg = 1;
5823 	}
5824 
5825 	/* Transfer remaining parsed ATA cmd values to the satacmd */
5826 	if (extend) {
5827 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
5828 
5829 		scmd->satacmd_features_reg_ext = (feature >> 8) & 0xff;
5830 		scmd->satacmd_sec_count_msb = (sec_count >> 8) & 0xff;
5831 		scmd->satacmd_lba_low_msb = (lba >> 8) & 0xff;
5832 		scmd->satacmd_lba_mid_msb = (lba >> 8) & 0xff;
5833 		scmd->satacmd_lba_high_msb = lba >> 40;
5834 	} else {
5835 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
5836 
5837 		scmd->satacmd_features_reg_ext = 0;
5838 		scmd->satacmd_sec_count_msb = 0;
5839 		scmd->satacmd_lba_low_msb = 0;
5840 		scmd->satacmd_lba_mid_msb = 0;
5841 		scmd->satacmd_lba_high_msb = 0;
5842 	}
5843 
5844 	scmd->satacmd_features_reg = feature & 0xff;
5845 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
5846 	scmd->satacmd_lba_low_lsb = lba & 0xff;
5847 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
5848 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
5849 
5850 	/* Determine transfer length */
5851 	switch (scsipkt->pkt_cdbp[2] & 0x3) {		/* T_LENGTH field */
5852 	case 1:
5853 		t_len = feature;
5854 		break;
5855 	case 2:
5856 		t_len = sec_count;
5857 		break;
5858 	default:
5859 		t_len = 0;
5860 		break;
5861 	}
5862 
5863 	/* Adjust transfer length for the Byte Block bit */
5864 	if ((scsipkt->pkt_cdbp[2] >> 2) & 1)
5865 		t_len *= SATA_DISK_SECTOR_SIZE;
5866 
5867 	/* Start processing command */
5868 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5869 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_apt_completion;
5870 		synch = FALSE;
5871 	} else {
5872 		synch = TRUE;
5873 	}
5874 
5875 	if (sata_hba_start(spx, &rval) != 0) {
5876 		mutex_exit(cport_mutex);
5877 		return (rval);
5878 	}
5879 
5880 	mutex_exit(cport_mutex);
5881 
5882 	if (synch) {
5883 		sata_txlt_apt_completion(spx->txlt_sata_pkt);
5884 	}
5885 
5886 	return (TRAN_ACCEPT);
5887 }
5888 
5889 /*
5890  * Translate command: Log Sense
5891  */
5892 static int
5893 sata_txlt_log_sense(sata_pkt_txlate_t *spx)
5894 {
5895 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
5896 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5897 	sata_drive_info_t *sdinfo;
5898 	struct scsi_extended_sense *sense;
5899 	int		len, count, alc_len;
5900 	int		pc;	/* Page Control code */
5901 	int		page_code;	/* Page code */
5902 	uint8_t		*buf;	/* log sense buffer */
5903 	int		rval, reason;
5904 #define	MAX_LOG_SENSE_PAGE_SIZE	512
5905 	kmutex_t	*cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
5906 
5907 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5908 	    "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
5909 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5910 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5911 
5912 	if (servicing_interrupt()) {
5913 		buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_NOSLEEP);
5914 		if (buf == NULL) {
5915 			return (TRAN_BUSY);
5916 		}
5917 	} else {
5918 		buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
5919 	}
5920 
5921 	mutex_enter(cport_mutex);
5922 
5923 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
5924 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5925 		mutex_exit(cport_mutex);
5926 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
5927 		return (rval);
5928 	}
5929 
5930 	scsipkt->pkt_reason = CMD_CMPLT;
5931 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5932 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5933 
5934 	pc = scsipkt->pkt_cdbp[2] >> 6;
5935 	page_code = scsipkt->pkt_cdbp[2] & 0x3f;
5936 
5937 	/* Reject not supported request for all but cumulative values */
5938 	switch (pc) {
5939 	case PC_CUMULATIVE_VALUES:
5940 		break;
5941 	default:
5942 		*scsipkt->pkt_scbp = STATUS_CHECK;
5943 		sense = sata_arq_sense(spx);
5944 		sense->es_key = KEY_ILLEGAL_REQUEST;
5945 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5946 		goto done;
5947 	}
5948 
5949 	switch (page_code) {
5950 	case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5951 	case PAGE_CODE_SELF_TEST_RESULTS:
5952 	case PAGE_CODE_INFORMATION_EXCEPTIONS:
5953 	case PAGE_CODE_SMART_READ_DATA:
5954 	case PAGE_CODE_START_STOP_CYCLE_COUNTER:
5955 		break;
5956 	default:
5957 		*scsipkt->pkt_scbp = STATUS_CHECK;
5958 		sense = sata_arq_sense(spx);
5959 		sense->es_key = KEY_ILLEGAL_REQUEST;
5960 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5961 		goto done;
5962 	}
5963 
5964 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
5965 		/*
5966 		 * Because log sense uses local buffers for data retrieval from
5967 		 * the devices and sets the data programatically in the
5968 		 * original specified buffer, release preallocated DMA
5969 		 * resources before storing data in the original buffer,
5970 		 * so no unwanted DMA sync would take place.
5971 		 */
5972 		sata_id_t *sata_id;
5973 
5974 		sata_scsi_dmafree(NULL, scsipkt);
5975 
5976 		len = 0;
5977 
5978 		/* Build log parameter header */
5979 		buf[len++] = page_code;	/* page code as in the CDB */
5980 		buf[len++] = 0;		/* reserved */
5981 		buf[len++] = 0;		/* Zero out page length for now (MSB) */
5982 		buf[len++] = 0;		/* (LSB) */
5983 
5984 		sdinfo = sata_get_device_info(
5985 		    spx->txlt_sata_hba_inst,
5986 		    &spx->txlt_sata_pkt->satapkt_device);
5987 
5988 		/*
5989 		 * Add requested pages.
5990 		 */
5991 		switch (page_code) {
5992 		case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5993 			len = sata_build_lsense_page_0(sdinfo, buf + len);
5994 			break;
5995 		case PAGE_CODE_SELF_TEST_RESULTS:
5996 			sata_id = &sdinfo->satadrv_id;
5997 			if ((! (sata_id->ai_cmdset84 &
5998 			    SATA_SMART_SELF_TEST_SUPPORTED)) ||
5999 			    (! (sata_id->ai_features87 &
6000 			    SATA_SMART_SELF_TEST_SUPPORTED))) {
6001 				*scsipkt->pkt_scbp = STATUS_CHECK;
6002 				sense = sata_arq_sense(spx);
6003 				sense->es_key = KEY_ILLEGAL_REQUEST;
6004 				sense->es_add_code =
6005 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6006 
6007 				goto done;
6008 			}
6009 			len = sata_build_lsense_page_10(sdinfo, buf + len,
6010 			    spx->txlt_sata_hba_inst);
6011 			break;
6012 		case PAGE_CODE_INFORMATION_EXCEPTIONS:
6013 			sata_id = &sdinfo->satadrv_id;
6014 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
6015 				*scsipkt->pkt_scbp = STATUS_CHECK;
6016 				sense = sata_arq_sense(spx);
6017 				sense->es_key = KEY_ILLEGAL_REQUEST;
6018 				sense->es_add_code =
6019 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6020 
6021 				goto done;
6022 			}
6023 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
6024 				*scsipkt->pkt_scbp = STATUS_CHECK;
6025 				sense = sata_arq_sense(spx);
6026 				sense->es_key = KEY_ABORTED_COMMAND;
6027 				sense->es_add_code =
6028 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
6029 				sense->es_qual_code =
6030 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
6031 
6032 				goto done;
6033 			}
6034 
6035 			len = sata_build_lsense_page_2f(sdinfo, buf + len,
6036 			    spx->txlt_sata_hba_inst);
6037 			break;
6038 		case PAGE_CODE_SMART_READ_DATA:
6039 			sata_id = &sdinfo->satadrv_id;
6040 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
6041 				*scsipkt->pkt_scbp = STATUS_CHECK;
6042 				sense = sata_arq_sense(spx);
6043 				sense->es_key = KEY_ILLEGAL_REQUEST;
6044 				sense->es_add_code =
6045 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6046 
6047 				goto done;
6048 			}
6049 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
6050 				*scsipkt->pkt_scbp = STATUS_CHECK;
6051 				sense = sata_arq_sense(spx);
6052 				sense->es_key = KEY_ABORTED_COMMAND;
6053 				sense->es_add_code =
6054 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
6055 				sense->es_qual_code =
6056 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
6057 
6058 				goto done;
6059 			}
6060 
6061 			/* This page doesn't include a page header */
6062 			len = sata_build_lsense_page_30(sdinfo, buf,
6063 			    spx->txlt_sata_hba_inst);
6064 			goto no_header;
6065 		case PAGE_CODE_START_STOP_CYCLE_COUNTER:
6066 			sata_id = &sdinfo->satadrv_id;
6067 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
6068 				*scsipkt->pkt_scbp = STATUS_CHECK;
6069 				sense = sata_arq_sense(spx);
6070 				sense->es_key = KEY_ILLEGAL_REQUEST;
6071 				sense->es_add_code =
6072 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6073 
6074 				goto done;
6075 			}
6076 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
6077 				*scsipkt->pkt_scbp = STATUS_CHECK;
6078 				sense = sata_arq_sense(spx);
6079 				sense->es_key = KEY_ABORTED_COMMAND;
6080 				sense->es_add_code =
6081 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
6082 				sense->es_qual_code =
6083 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
6084 
6085 				goto done;
6086 			}
6087 			len = sata_build_lsense_page_0e(sdinfo, buf, spx);
6088 			goto no_header;
6089 		default:
6090 			/* Invalid request */
6091 			*scsipkt->pkt_scbp = STATUS_CHECK;
6092 			sense = sata_arq_sense(spx);
6093 			sense->es_key = KEY_ILLEGAL_REQUEST;
6094 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6095 			goto done;
6096 		}
6097 
6098 		/* set parameter log sense data length */
6099 		buf[2] = len >> 8;	/* log sense length (MSB) */
6100 		buf[3] = len & 0xff;	/* log sense length (LSB) */
6101 
6102 		len += SCSI_LOG_PAGE_HDR_LEN;
6103 		ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
6104 
6105 no_header:
6106 		/* Check allocation length */
6107 		alc_len = scsipkt->pkt_cdbp[7];
6108 		alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
6109 
6110 		/*
6111 		 * We do not check for possible parameters truncation
6112 		 * (alc_len < len) assuming that the target driver works
6113 		 * correctly. Just avoiding overrun.
6114 		 * Copy no more than requested and possible, buffer-wise.
6115 		 */
6116 		count = MIN(alc_len, len);
6117 		count = MIN(bp->b_bcount, count);
6118 		bcopy(buf, bp->b_un.b_addr, count);
6119 
6120 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
6121 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
6122 	}
6123 	*scsipkt->pkt_scbp = STATUS_GOOD;
6124 done:
6125 	mutex_exit(cport_mutex);
6126 	(void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
6127 
6128 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6129 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6130 
6131 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6132 	    scsipkt->pkt_comp != NULL) {
6133 		/* scsi callback required */
6134 		if (servicing_interrupt()) {
6135 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6136 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6137 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
6138 			    TASKQID_INVALID) {
6139 				return (TRAN_BUSY);
6140 			}
6141 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6142 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6143 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
6144 			/* Scheduling the callback failed */
6145 			return (TRAN_BUSY);
6146 		}
6147 	}
6148 
6149 	return (TRAN_ACCEPT);
6150 }
6151 
6152 /*
6153  * Translate command: Log Select
6154  * Not implemented at this time - returns invalid command response.
6155  */
6156 static	int
6157 sata_txlt_log_select(sata_pkt_txlate_t *spx)
6158 {
6159 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6160 	    "sata_txlt_log_select\n", NULL);
6161 
6162 	return (sata_txlt_invalid_command(spx));
6163 }
6164 
6165 
6166 /*
6167  * Translate command: Read (various types).
6168  * Translated into appropriate type of ATA READ command
6169  * for SATA hard disks.
6170  * Both the device capabilities and requested operation mode are
6171  * considered.
6172  *
6173  * Following scsi cdb fields are ignored:
6174  * rdprotect, dpo, fua, fua_nv, group_number.
6175  *
6176  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
6177  * enable variable sata_func_enable), the capability of the controller and
6178  * capability of a device are checked and if both support queueing, read
6179  * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
6180  * command rather than plain READ_XXX command.
6181  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
6182  * both the controller and device suport such functionality, the read
6183  * request will be translated to READ_FPDMA_QUEUED command.
6184  * In both cases the maximum queue depth is derived as minimum of:
6185  * HBA capability,device capability and sata_max_queue_depth variable setting.
6186  * The value passed to HBA driver is decremented by 1, because only 5 bits are
6187  * used to pass max queue depth value, and the maximum possible queue depth
6188  * is 32.
6189  *
6190  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6191  * appropriate values in scsi_pkt fields.
6192  */
6193 static int
6194 sata_txlt_read(sata_pkt_txlate_t *spx)
6195 {
6196 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6197 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6198 	sata_drive_info_t *sdinfo;
6199 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
6200 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6201 	uint16_t sec_count;
6202 	uint64_t lba;
6203 	int rval, reason;
6204 	int synch;
6205 
6206 	mutex_enter(cport_mutex);
6207 
6208 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
6209 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6210 		mutex_exit(cport_mutex);
6211 		return (rval);
6212 	}
6213 
6214 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6215 	    &spx->txlt_sata_pkt->satapkt_device);
6216 
6217 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
6218 	/*
6219 	 * Extract LBA and sector count from scsi CDB.
6220 	 */
6221 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6222 	case SCMD_READ:
6223 		/* 6-byte scsi read cmd : 0x08 */
6224 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
6225 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
6226 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6227 		sec_count = scsipkt->pkt_cdbp[4];
6228 		/* sec_count 0 will be interpreted as 256 by a device */
6229 		break;
6230 	case SCMD_READ_G1:
6231 		/* 10-bytes scsi read command : 0x28 */
6232 		lba = scsipkt->pkt_cdbp[2];
6233 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6234 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6235 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6236 		sec_count = scsipkt->pkt_cdbp[7];
6237 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6238 		break;
6239 	case SCMD_READ_G5:
6240 		/* 12-bytes scsi read command : 0xA8 */
6241 		lba = scsipkt->pkt_cdbp[2];
6242 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6243 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6244 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6245 		sec_count = scsipkt->pkt_cdbp[6];
6246 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
6247 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6248 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
6249 		break;
6250 	case SCMD_READ_G4:
6251 		/* 16-bytes scsi read command : 0x88 */
6252 		lba = scsipkt->pkt_cdbp[2];
6253 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6254 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6255 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6256 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
6257 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
6258 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
6259 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
6260 		sec_count = scsipkt->pkt_cdbp[10];
6261 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
6262 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
6263 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
6264 		break;
6265 	default:
6266 		/* Unsupported command */
6267 		mutex_exit(cport_mutex);
6268 		return (sata_txlt_invalid_command(spx));
6269 	}
6270 
6271 	/*
6272 	 * Check if specified address exceeds device capacity
6273 	 */
6274 	if ((lba >= sdinfo->satadrv_capacity) ||
6275 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
6276 		/* LBA out of range */
6277 		mutex_exit(cport_mutex);
6278 		return (sata_txlt_lba_out_of_range(spx));
6279 	}
6280 
6281 	/*
6282 	 * For zero-length transfer, emulate good completion of the command
6283 	 * (reasons for rejecting the command were already checked).
6284 	 * No DMA resources were allocated.
6285 	 */
6286 	if (spx->txlt_dma_cookie_list == NULL) {
6287 		mutex_exit(cport_mutex);
6288 		return (sata_emul_rw_completion(spx));
6289 	}
6290 
6291 	/*
6292 	 * Build cmd block depending on the device capability and
6293 	 * requested operation mode.
6294 	 * Do not bother with non-dma mode - we are working only with
6295 	 * devices supporting DMA.
6296 	 */
6297 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
6298 	scmd->satacmd_device_reg = SATA_ADH_LBA;
6299 	scmd->satacmd_cmd_reg = SATAC_READ_DMA;
6300 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6301 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
6302 		scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT;
6303 		scmd->satacmd_sec_count_msb = sec_count >> 8;
6304 #ifndef __lock_lint
6305 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
6306 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
6307 		scmd->satacmd_lba_high_msb = lba >> 40;
6308 #endif
6309 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
6310 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
6311 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
6312 	}
6313 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
6314 	scmd->satacmd_lba_low_lsb = lba & 0xff;
6315 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
6316 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
6317 	scmd->satacmd_features_reg = 0;
6318 	scmd->satacmd_status_reg = 0;
6319 	scmd->satacmd_error_reg = 0;
6320 
6321 	/*
6322 	 * Check if queueing commands should be used and switch
6323 	 * to appropriate command if possible
6324 	 */
6325 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
6326 		boolean_t using_queuing;
6327 
6328 		/* Queuing supported by controller and device? */
6329 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
6330 		    (sdinfo->satadrv_features_support &
6331 		    SATA_DEV_F_NCQ) &&
6332 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6333 		    SATA_CTLF_NCQ)) {
6334 			using_queuing = B_TRUE;
6335 
6336 			/* NCQ supported - use FPDMA READ */
6337 			scmd->satacmd_cmd_reg =
6338 			    SATAC_READ_FPDMA_QUEUED;
6339 			scmd->satacmd_features_reg_ext =
6340 			    scmd->satacmd_sec_count_msb;
6341 			scmd->satacmd_sec_count_msb = 0;
6342 		} else if ((sdinfo->satadrv_features_support &
6343 		    SATA_DEV_F_TCQ) &&
6344 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6345 		    SATA_CTLF_QCMD)) {
6346 			using_queuing = B_TRUE;
6347 
6348 			/* Legacy queueing */
6349 			if (sdinfo->satadrv_features_support &
6350 			    SATA_DEV_F_LBA48) {
6351 				scmd->satacmd_cmd_reg =
6352 				    SATAC_READ_DMA_QUEUED_EXT;
6353 				scmd->satacmd_features_reg_ext =
6354 				    scmd->satacmd_sec_count_msb;
6355 				scmd->satacmd_sec_count_msb = 0;
6356 			} else {
6357 				scmd->satacmd_cmd_reg =
6358 				    SATAC_READ_DMA_QUEUED;
6359 			}
6360 		} else	/* NCQ nor legacy queuing not supported */
6361 			using_queuing = B_FALSE;
6362 
6363 		/*
6364 		 * If queuing, the sector count goes in the features register
6365 		 * and the secount count will contain the tag.
6366 		 */
6367 		if (using_queuing) {
6368 			scmd->satacmd_features_reg =
6369 			    scmd->satacmd_sec_count_lsb;
6370 			scmd->satacmd_sec_count_lsb = 0;
6371 			scmd->satacmd_flags.sata_queued = B_TRUE;
6372 
6373 			/* Set-up maximum queue depth */
6374 			scmd->satacmd_flags.sata_max_queue_depth =
6375 			    sdinfo->satadrv_max_queue_depth - 1;
6376 		} else if (sdinfo->satadrv_features_enabled &
6377 		    SATA_DEV_F_E_UNTAGGED_QING) {
6378 			/*
6379 			 * Although NCQ/TCQ is not enabled, untagged queuing
6380 			 * may be still used.
6381 			 * Set-up the maximum untagged queue depth.
6382 			 * Use controller's queue depth from sata_hba_tran.
6383 			 * SATA HBA drivers may ignore this value and rely on
6384 			 * the internal limits.For drivers that do not
6385 			 * ignore untaged queue depth, limit the value to
6386 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
6387 			 * largest value that can be passed via
6388 			 * satacmd_flags.sata_max_queue_depth.
6389 			 */
6390 			scmd->satacmd_flags.sata_max_queue_depth =
6391 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
6392 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
6393 
6394 		} else {
6395 			scmd->satacmd_flags.sata_max_queue_depth = 0;
6396 		}
6397 	} else
6398 		scmd->satacmd_flags.sata_max_queue_depth = 0;
6399 
6400 	SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst,
6401 	    "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n",
6402 	    scmd->satacmd_cmd_reg, lba, sec_count);
6403 
6404 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6405 		/* Need callback function */
6406 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
6407 		synch = FALSE;
6408 	} else
6409 		synch = TRUE;
6410 
6411 	/* Transfer command to HBA */
6412 	if (sata_hba_start(spx, &rval) != 0) {
6413 		/* Pkt not accepted for execution */
6414 		mutex_exit(cport_mutex);
6415 		return (rval);
6416 	}
6417 	mutex_exit(cport_mutex);
6418 	/*
6419 	 * If execution is non-synchronous,
6420 	 * a callback function will handle potential errors, translate
6421 	 * the response and will do a callback to a target driver.
6422 	 * If it was synchronous, check execution status using the same
6423 	 * framework callback.
6424 	 */
6425 	if (synch) {
6426 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6427 		    "synchronous execution status %x\n",
6428 		    spx->txlt_sata_pkt->satapkt_reason);
6429 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
6430 	}
6431 	return (TRAN_ACCEPT);
6432 }
6433 
6434 
6435 /*
6436  * SATA translate command: Write (various types)
6437  * Translated into appropriate type of ATA WRITE command
6438  * for SATA hard disks.
6439  * Both the device capabilities and requested operation mode are
6440  * considered.
6441  *
6442  * Following scsi cdb fields are ignored:
6443  * rwprotect, dpo, fua, fua_nv, group_number.
6444  *
6445  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
6446  * enable variable sata_func_enable), the capability of the controller and
6447  * capability of a device are checked and if both support queueing, write
6448  * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT
6449  * command rather than plain WRITE_XXX command.
6450  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
6451  * both the controller and device suport such functionality, the write
6452  * request will be translated to WRITE_FPDMA_QUEUED command.
6453  * In both cases the maximum queue depth is derived as minimum of:
6454  * HBA capability,device capability and sata_max_queue_depth variable setting.
6455  * The value passed to HBA driver is decremented by 1, because only 5 bits are
6456  * used to pass max queue depth value, and the maximum possible queue depth
6457  * is 32.
6458  *
6459  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6460  * appropriate values in scsi_pkt fields.
6461  */
6462 static int
6463 sata_txlt_write(sata_pkt_txlate_t *spx)
6464 {
6465 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6466 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6467 	sata_drive_info_t *sdinfo;
6468 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
6469 	uint16_t sec_count;
6470 	uint64_t lba;
6471 	int rval, reason;
6472 	int synch;
6473 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6474 
6475 	mutex_enter(cport_mutex);
6476 
6477 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
6478 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6479 		mutex_exit(cport_mutex);
6480 		return (rval);
6481 	}
6482 
6483 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6484 	    &spx->txlt_sata_pkt->satapkt_device);
6485 
6486 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
6487 	/*
6488 	 * Extract LBA and sector count from scsi CDB
6489 	 */
6490 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6491 	case SCMD_WRITE:
6492 		/* 6-byte scsi read cmd : 0x0A */
6493 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
6494 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
6495 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6496 		sec_count = scsipkt->pkt_cdbp[4];
6497 		/* sec_count 0 will be interpreted as 256 by a device */
6498 		break;
6499 	case SCMD_WRITE_G1:
6500 		/* 10-bytes scsi write command : 0x2A */
6501 		lba = scsipkt->pkt_cdbp[2];
6502 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6503 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6504 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6505 		sec_count = scsipkt->pkt_cdbp[7];
6506 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6507 		break;
6508 	case SCMD_WRITE_G5:
6509 		/* 12-bytes scsi read command : 0xAA */
6510 		lba = scsipkt->pkt_cdbp[2];
6511 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6512 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6513 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6514 		sec_count = scsipkt->pkt_cdbp[6];
6515 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
6516 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
6517 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
6518 		break;
6519 	case SCMD_WRITE_G4:
6520 		/* 16-bytes scsi write command : 0x8A */
6521 		lba = scsipkt->pkt_cdbp[2];
6522 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
6523 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
6524 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
6525 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
6526 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
6527 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
6528 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
6529 		sec_count = scsipkt->pkt_cdbp[10];
6530 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
6531 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
6532 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
6533 		break;
6534 	default:
6535 		/* Unsupported command */
6536 		mutex_exit(cport_mutex);
6537 		return (sata_txlt_invalid_command(spx));
6538 	}
6539 
6540 	/*
6541 	 * Check if specified address and length exceeds device capacity
6542 	 */
6543 	if ((lba >= sdinfo->satadrv_capacity) ||
6544 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
6545 		/* LBA out of range */
6546 		mutex_exit(cport_mutex);
6547 		return (sata_txlt_lba_out_of_range(spx));
6548 	}
6549 
6550 	/*
6551 	 * For zero-length transfer, emulate good completion of the command
6552 	 * (reasons for rejecting the command were already checked).
6553 	 * No DMA resources were allocated.
6554 	 */
6555 	if (spx->txlt_dma_cookie_list == NULL) {
6556 		mutex_exit(cport_mutex);
6557 		return (sata_emul_rw_completion(spx));
6558 	}
6559 
6560 	/*
6561 	 * Build cmd block depending on the device capability and
6562 	 * requested operation mode.
6563 	 * Do not bother with non-dma mode- we are working only with
6564 	 * devices supporting DMA.
6565 	 */
6566 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
6567 	scmd->satacmd_device_reg = SATA_ADH_LBA;
6568 	scmd->satacmd_cmd_reg = SATAC_WRITE_DMA;
6569 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6570 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
6571 		scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT;
6572 		scmd->satacmd_sec_count_msb = sec_count >> 8;
6573 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
6574 #ifndef __lock_lint
6575 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
6576 		scmd->satacmd_lba_high_msb = lba >> 40;
6577 #endif
6578 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
6579 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
6580 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
6581 	}
6582 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
6583 	scmd->satacmd_lba_low_lsb = lba & 0xff;
6584 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
6585 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
6586 	scmd->satacmd_features_reg = 0;
6587 	scmd->satacmd_status_reg = 0;
6588 	scmd->satacmd_error_reg = 0;
6589 
6590 	/*
6591 	 * Check if queueing commands should be used and switch
6592 	 * to appropriate command if possible
6593 	 */
6594 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
6595 		boolean_t using_queuing;
6596 
6597 		/* Queuing supported by controller and device? */
6598 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
6599 		    (sdinfo->satadrv_features_support &
6600 		    SATA_DEV_F_NCQ) &&
6601 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6602 		    SATA_CTLF_NCQ)) {
6603 			using_queuing = B_TRUE;
6604 
6605 			/* NCQ supported - use FPDMA WRITE */
6606 			scmd->satacmd_cmd_reg =
6607 			    SATAC_WRITE_FPDMA_QUEUED;
6608 			scmd->satacmd_features_reg_ext =
6609 			    scmd->satacmd_sec_count_msb;
6610 			scmd->satacmd_sec_count_msb = 0;
6611 		} else if ((sdinfo->satadrv_features_support &
6612 		    SATA_DEV_F_TCQ) &&
6613 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
6614 		    SATA_CTLF_QCMD)) {
6615 			using_queuing = B_TRUE;
6616 
6617 			/* Legacy queueing */
6618 			if (sdinfo->satadrv_features_support &
6619 			    SATA_DEV_F_LBA48) {
6620 				scmd->satacmd_cmd_reg =
6621 				    SATAC_WRITE_DMA_QUEUED_EXT;
6622 				scmd->satacmd_features_reg_ext =
6623 				    scmd->satacmd_sec_count_msb;
6624 				scmd->satacmd_sec_count_msb = 0;
6625 			} else {
6626 				scmd->satacmd_cmd_reg =
6627 				    SATAC_WRITE_DMA_QUEUED;
6628 			}
6629 		} else	/*  NCQ nor legacy queuing not supported */
6630 			using_queuing = B_FALSE;
6631 
6632 		if (using_queuing) {
6633 			scmd->satacmd_features_reg =
6634 			    scmd->satacmd_sec_count_lsb;
6635 			scmd->satacmd_sec_count_lsb = 0;
6636 			scmd->satacmd_flags.sata_queued = B_TRUE;
6637 			/* Set-up maximum queue depth */
6638 			scmd->satacmd_flags.sata_max_queue_depth =
6639 			    sdinfo->satadrv_max_queue_depth - 1;
6640 		} else if (sdinfo->satadrv_features_enabled &
6641 		    SATA_DEV_F_E_UNTAGGED_QING) {
6642 			/*
6643 			 * Although NCQ/TCQ is not enabled, untagged queuing
6644 			 * may be still used.
6645 			 * Set-up the maximum untagged queue depth.
6646 			 * Use controller's queue depth from sata_hba_tran.
6647 			 * SATA HBA drivers may ignore this value and rely on
6648 			 * the internal limits. For drivera that do not
6649 			 * ignore untaged queue depth, limit the value to
6650 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
6651 			 * largest value that can be passed via
6652 			 * satacmd_flags.sata_max_queue_depth.
6653 			 */
6654 			scmd->satacmd_flags.sata_max_queue_depth =
6655 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
6656 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
6657 
6658 		} else {
6659 			scmd->satacmd_flags.sata_max_queue_depth = 0;
6660 		}
6661 	} else
6662 		scmd->satacmd_flags.sata_max_queue_depth = 0;
6663 
6664 	SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6665 	    "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n",
6666 	    scmd->satacmd_cmd_reg, lba, sec_count);
6667 
6668 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6669 		/* Need callback function */
6670 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
6671 		synch = FALSE;
6672 	} else
6673 		synch = TRUE;
6674 
6675 	/* Transfer command to HBA */
6676 	if (sata_hba_start(spx, &rval) != 0) {
6677 		/* Pkt not accepted for execution */
6678 		mutex_exit(cport_mutex);
6679 		return (rval);
6680 	}
6681 	mutex_exit(cport_mutex);
6682 
6683 	/*
6684 	 * If execution is non-synchronous,
6685 	 * a callback function will handle potential errors, translate
6686 	 * the response and will do a callback to a target driver.
6687 	 * If it was synchronous, check execution status using the same
6688 	 * framework callback.
6689 	 */
6690 	if (synch) {
6691 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6692 		    "synchronous execution status %x\n",
6693 		    spx->txlt_sata_pkt->satapkt_reason);
6694 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
6695 	}
6696 	return (TRAN_ACCEPT);
6697 }
6698 
6699 
6700 /*
6701  * Implements SCSI SBC WRITE BUFFER command download microcode option
6702  */
6703 static int
6704 sata_txlt_write_buffer(sata_pkt_txlate_t *spx)
6705 {
6706 #define	WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE			4
6707 #define	WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE			5
6708 
6709 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6710 	struct sata_pkt *sata_pkt = spx->txlt_sata_pkt;
6711 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6712 
6713 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
6714 	struct scsi_extended_sense *sense;
6715 	int rval, mode, sector_count, reason;
6716 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6717 
6718 	mode = scsipkt->pkt_cdbp[1] & 0x1f;
6719 
6720 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6721 	    "sata_txlt_write_buffer, mode 0x%x\n", mode);
6722 
6723 	mutex_enter(cport_mutex);
6724 
6725 	if ((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
6726 	    TRAN_ACCEPT) {
6727 		mutex_exit(cport_mutex);
6728 		return (rval);
6729 	}
6730 
6731 	/* Use synchronous mode */
6732 	spx->txlt_sata_pkt->satapkt_op_mode
6733 	    |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
6734 
6735 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
6736 
6737 	scsipkt->pkt_reason = CMD_CMPLT;
6738 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6739 	    STATE_SENT_CMD | STATE_GOT_STATUS;
6740 
6741 	/*
6742 	 * The SCSI to ATA translation specification only calls
6743 	 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE.
6744 	 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but
6745 	 * ATA 8 (draft) got rid of download microcode for temp
6746 	 * and it is even optional for ATA 7, so it may be aborted.
6747 	 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as
6748 	 * it is not specified and the buffer offset for SCSI is a 16-bit
6749 	 * value in bytes, but for ATA it is a 16-bit offset in 512 byte
6750 	 * sectors.  Thus the offset really doesn't buy us anything.
6751 	 * If and when ATA 8 is stabilized and the SCSI to ATA specification
6752 	 * is revised, this can be revisisted.
6753 	 */
6754 	/* Reject not supported request */
6755 	switch (mode) {
6756 	case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE:
6757 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP;
6758 		break;
6759 	case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE:
6760 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE;
6761 		break;
6762 	default:
6763 		goto bad_param;
6764 	}
6765 
6766 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
6767 
6768 	scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE;
6769 	if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0)
6770 		goto bad_param;
6771 	sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE;
6772 	scmd->satacmd_sec_count_lsb = (uint8_t)sector_count;
6773 	scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8;
6774 	scmd->satacmd_lba_mid_lsb = 0;
6775 	scmd->satacmd_lba_high_lsb = 0;
6776 	scmd->satacmd_device_reg = 0;
6777 	spx->txlt_sata_pkt->satapkt_comp = NULL;
6778 	scmd->satacmd_addr_type = 0;
6779 
6780 	/* Transfer command to HBA */
6781 	if (sata_hba_start(spx, &rval) != 0) {
6782 		/* Pkt not accepted for execution */
6783 		mutex_exit(cport_mutex);
6784 		return (rval);
6785 	}
6786 
6787 	mutex_exit(cport_mutex);
6788 
6789 	/* Then we need synchronous check the status of the disk */
6790 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6791 	    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
6792 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
6793 		scsipkt->pkt_reason = CMD_CMPLT;
6794 
6795 		/* Download commmand succeed, so probe and identify device */
6796 		sata_reidentify_device(spx);
6797 	} else {
6798 		/* Something went wrong, microcode download command failed */
6799 		scsipkt->pkt_reason = CMD_INCOMPLETE;
6800 		*scsipkt->pkt_scbp = STATUS_CHECK;
6801 		sense = sata_arq_sense(spx);
6802 		switch (sata_pkt->satapkt_reason) {
6803 		case SATA_PKT_PORT_ERROR:
6804 			/*
6805 			 * We have no device data. Assume no data transfered.
6806 			 */
6807 			sense->es_key = KEY_HARDWARE_ERROR;
6808 			break;
6809 
6810 		case SATA_PKT_DEV_ERROR:
6811 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
6812 			    SATA_STATUS_ERR) {
6813 				/*
6814 				 * determine dev error reason from error
6815 				 * reg content
6816 				 */
6817 				sata_decode_device_error(spx, sense);
6818 				break;
6819 			}
6820 			/* No extended sense key - no info available */
6821 			break;
6822 
6823 		case SATA_PKT_TIMEOUT:
6824 			scsipkt->pkt_reason = CMD_TIMEOUT;
6825 			scsipkt->pkt_statistics |=
6826 			    STAT_TIMEOUT | STAT_DEV_RESET;
6827 			/* No extended sense key ? */
6828 			break;
6829 
6830 		case SATA_PKT_ABORTED:
6831 			scsipkt->pkt_reason = CMD_ABORTED;
6832 			scsipkt->pkt_statistics |= STAT_ABORTED;
6833 			/* No extended sense key ? */
6834 			break;
6835 
6836 		case SATA_PKT_RESET:
6837 			/* pkt aborted by an explicit reset from a host */
6838 			scsipkt->pkt_reason = CMD_RESET;
6839 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
6840 			break;
6841 
6842 		default:
6843 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
6844 			    "sata_txlt_nodata_cmd_completion: "
6845 			    "invalid packet completion reason %d",
6846 			    sata_pkt->satapkt_reason));
6847 			scsipkt->pkt_reason = CMD_TRAN_ERR;
6848 			break;
6849 		}
6850 
6851 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6852 		    "scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6853 
6854 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6855 			/* scsi callback required */
6856 			scsi_hba_pkt_comp(scsipkt);
6857 	}
6858 	return (TRAN_ACCEPT);
6859 
6860 bad_param:
6861 	mutex_exit(cport_mutex);
6862 	*scsipkt->pkt_scbp = STATUS_CHECK;
6863 	sense = sata_arq_sense(spx);
6864 	sense->es_key = KEY_ILLEGAL_REQUEST;
6865 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6866 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6867 	    scsipkt->pkt_comp != NULL) {
6868 		/* scsi callback required */
6869 		if (servicing_interrupt()) {
6870 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6871 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6872 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
6873 			    TASKQID_INVALID) {
6874 				return (TRAN_BUSY);
6875 			}
6876 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6877 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
6878 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
6879 			/* Scheduling the callback failed */
6880 			return (TRAN_BUSY);
6881 		}
6882 	}
6883 	return (rval);
6884 }
6885 
6886 /*
6887  * Re-identify device after doing a firmware download.
6888  */
6889 static void
6890 sata_reidentify_device(sata_pkt_txlate_t *spx)
6891 {
6892 #define	DOWNLOAD_WAIT_TIME_SECS	60
6893 #define	DOWNLOAD_WAIT_INTERVAL_SECS	1
6894 	int rval;
6895 	int retry_cnt;
6896 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6897 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
6898 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
6899 	sata_drive_info_t *sdinfo;
6900 
6901 	/*
6902 	 * Before returning good status, probe device.
6903 	 * Device probing will get IDENTIFY DEVICE data, if possible.
6904 	 * The assumption is that the new microcode is applied by the
6905 	 * device. It is a caller responsibility to verify this.
6906 	 */
6907 	for (retry_cnt = 0;
6908 	    retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS;
6909 	    retry_cnt++) {
6910 		rval = sata_probe_device(sata_hba_inst, &sata_device);
6911 
6912 		if (rval == SATA_SUCCESS) { /* Set default features */
6913 			sdinfo = sata_get_device_info(sata_hba_inst,
6914 			    &sata_device);
6915 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
6916 			    SATA_SUCCESS) {
6917 				/* retry */
6918 				rval = sata_initialize_device(sata_hba_inst,
6919 				    sdinfo);
6920 				if (rval == SATA_RETRY)
6921 					sata_log(sata_hba_inst, CE_WARN,
6922 					    "SATA device at port %d pmport %d -"
6923 					    " default device features could not"
6924 					    " be set. Device may not operate "
6925 					    "as expected.",
6926 					    sata_device.satadev_addr.cport,
6927 					    sata_device.satadev_addr.pmport);
6928 			}
6929 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6930 				scsi_hba_pkt_comp(scsipkt);
6931 			return;
6932 		} else if (rval == SATA_RETRY) {
6933 			delay(drv_usectohz(1000000 *
6934 			    DOWNLOAD_WAIT_INTERVAL_SECS));
6935 			continue;
6936 		} else	/* failed - no reason to retry */
6937 			break;
6938 	}
6939 
6940 	/*
6941 	 * Something went wrong, device probing failed.
6942 	 */
6943 	SATA_LOG_D((sata_hba_inst, CE_WARN,
6944 	    "Cannot probe device after downloading microcode\n"));
6945 
6946 	/* Reset device to force retrying the probe. */
6947 	(void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst))
6948 	    (SATA_DIP(sata_hba_inst), &sata_device);
6949 
6950 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6951 		scsi_hba_pkt_comp(scsipkt);
6952 }
6953 
6954 
6955 /*
6956  * Translate command: Synchronize Cache.
6957  * Translates into Flush Cache command for SATA hard disks.
6958  *
6959  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6960  * appropriate values in scsi_pkt fields.
6961  */
6962 static int
6963 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx)
6964 {
6965 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6966 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
6967 	int rval, reason;
6968 	int synch;
6969 
6970 	mutex_enter(cport_mutex);
6971 
6972 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 1)) !=
6973 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6974 		mutex_exit(cport_mutex);
6975 		return (rval);
6976 	}
6977 
6978 	scmd->satacmd_addr_type = 0;
6979 	scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE;
6980 	scmd->satacmd_device_reg = 0;
6981 	scmd->satacmd_sec_count_lsb = 0;
6982 	scmd->satacmd_lba_low_lsb = 0;
6983 	scmd->satacmd_lba_mid_lsb = 0;
6984 	scmd->satacmd_lba_high_lsb = 0;
6985 	scmd->satacmd_features_reg = 0;
6986 	scmd->satacmd_status_reg = 0;
6987 	scmd->satacmd_error_reg = 0;
6988 
6989 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6990 	    "sata_txlt_synchronize_cache\n", NULL);
6991 
6992 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6993 		/* Need to set-up a callback function */
6994 		spx->txlt_sata_pkt->satapkt_comp =
6995 		    sata_txlt_nodata_cmd_completion;
6996 		synch = FALSE;
6997 	} else
6998 		synch = TRUE;
6999 
7000 	/* Transfer command to HBA */
7001 	if (sata_hba_start(spx, &rval) != 0) {
7002 		/* Pkt not accepted for execution */
7003 		mutex_exit(cport_mutex);
7004 		return (rval);
7005 	}
7006 	mutex_exit(cport_mutex);
7007 
7008 	/*
7009 	 * If execution non-synchronous, it had to be completed
7010 	 * a callback function will handle potential errors, translate
7011 	 * the response and will do a callback to a target driver.
7012 	 * If it was synchronous, check status, using the same
7013 	 * framework callback.
7014 	 */
7015 	if (synch) {
7016 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7017 		    "synchronous execution status %x\n",
7018 		    spx->txlt_sata_pkt->satapkt_reason);
7019 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
7020 	}
7021 	return (TRAN_ACCEPT);
7022 }
7023 
7024 
7025 /*
7026  * Send pkt to SATA HBA driver
7027  *
7028  * This function may be called only if the operation is requested by scsi_pkt,
7029  * i.e. scsi_pkt is not NULL.
7030  *
7031  * This function has to be called with cport mutex held. It does release
7032  * the mutex when it calls HBA driver sata_tran_start function and
7033  * re-acquires it afterwards.
7034  *
7035  * If return value is 0, pkt was accepted, -1 otherwise
7036  * rval is set to appropriate sata_scsi_start return value.
7037  *
7038  * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not
7039  * have called the sata_pkt callback function for this packet.
7040  *
7041  * The scsi callback has to be performed by the caller of this routine.
7042  */
7043 static int
7044 sata_hba_start(sata_pkt_txlate_t *spx, int *rval)
7045 {
7046 	int stat;
7047 	uint8_t cport = SATA_TXLT_CPORT(spx);
7048 	uint8_t pmport = SATA_TXLT_PMPORT(spx);
7049 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
7050 	sata_drive_info_t *sdinfo;
7051 	sata_pmult_info_t *pminfo;
7052 	sata_pmport_info_t *pmportinfo = NULL;
7053 	sata_device_t *sata_device = NULL;
7054 	uint8_t cmd;
7055 	struct sata_cmd_flags cmd_flags;
7056 
7057 	ASSERT(spx->txlt_sata_pkt != NULL);
7058 
7059 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7060 
7061 	sdinfo = sata_get_device_info(sata_hba_inst,
7062 	    &spx->txlt_sata_pkt->satapkt_device);
7063 	ASSERT(sdinfo != NULL);
7064 
7065 	/* Clear device reset state? */
7066 	/* qual should be XXX_DPMPORT, but add XXX_PMPORT in case */
7067 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT ||
7068 	    sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT) {
7069 
7070 		/*
7071 		 * Get the pmult_info of the its parent port multiplier, all
7072 		 * sub-devices share a common device reset flags on in
7073 		 * pmult_info.
7074 		 */
7075 		pminfo = SATA_PMULT_INFO(sata_hba_inst, cport);
7076 		pmportinfo = pminfo->pmult_dev_port[pmport];
7077 		ASSERT(pminfo != NULL);
7078 		if (pminfo->pmult_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) {
7079 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
7080 			    sata_clear_dev_reset = B_TRUE;
7081 			pminfo->pmult_event_flags &=
7082 			    ~SATA_EVNT_CLEAR_DEVICE_RESET;
7083 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7084 			    "sata_hba_start: clearing device reset state"
7085 			    "on pmult.\n", NULL);
7086 		}
7087 	} else {
7088 		if (sdinfo->satadrv_event_flags &
7089 		    SATA_EVNT_CLEAR_DEVICE_RESET) {
7090 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
7091 			    sata_clear_dev_reset = B_TRUE;
7092 			sdinfo->satadrv_event_flags &=
7093 			    ~SATA_EVNT_CLEAR_DEVICE_RESET;
7094 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7095 			    "sata_hba_start: clearing device reset state\n",
7096 			    NULL);
7097 		}
7098 	}
7099 
7100 	cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg;
7101 	cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags;
7102 	sata_device = &spx->txlt_sata_pkt->satapkt_device;
7103 
7104 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7105 
7106 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7107 	    "Sata cmd 0x%2x\n", cmd);
7108 
7109 	stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
7110 	    spx->txlt_sata_pkt);
7111 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7112 	/*
7113 	 * If sata pkt was accepted and executed in asynchronous mode, i.e.
7114 	 * with the sata callback, the sata_pkt could be already destroyed
7115 	 * by the time we check ther return status from the hba_start()
7116 	 * function, because sata_scsi_destroy_pkt() could have been already
7117 	 * called (perhaps in the interrupt context). So, in such case, there
7118 	 * should be no references to it. In other cases, sata_pkt still
7119 	 * exists.
7120 	 */
7121 	if (stat == SATA_TRAN_ACCEPTED) {
7122 		/*
7123 		 * pkt accepted for execution.
7124 		 * If it was executed synchronously, it is already completed
7125 		 * and pkt completion_reason indicates completion status.
7126 		 */
7127 		*rval = TRAN_ACCEPT;
7128 		return (0);
7129 	}
7130 
7131 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
7132 	switch (stat) {
7133 	case SATA_TRAN_QUEUE_FULL:
7134 		/*
7135 		 * Controller detected queue full condition.
7136 		 */
7137 		SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst,
7138 		    "sata_hba_start: queue full\n", NULL);
7139 
7140 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
7141 		*spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL;
7142 
7143 		*rval = TRAN_BUSY;
7144 		break;
7145 
7146 	case SATA_TRAN_PORT_ERROR:
7147 		/*
7148 		 * Communication/link with device or general port error
7149 		 * detected before pkt execution begun.
7150 		 */
7151 		if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
7152 		    SATA_ADDR_CPORT ||
7153 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
7154 		    SATA_ADDR_DCPORT)
7155 			sata_log(sata_hba_inst, CE_CONT,
7156 			    "SATA port %d error",
7157 			    sata_device->satadev_addr.cport);
7158 		else
7159 			sata_log(sata_hba_inst, CE_CONT,
7160 			    "SATA port %d:%d error\n",
7161 			    sata_device->satadev_addr.cport,
7162 			    sata_device->satadev_addr.pmport);
7163 
7164 		/*
7165 		 * Update the port/device structure.
7166 		 * sata_pkt should be still valid. Since port error is
7167 		 * returned, sata_device content should reflect port
7168 		 * state - it means, that sata address have been changed,
7169 		 * because original packet's sata address refered to a device
7170 		 * attached to some port.
7171 		 */
7172 		if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT ||
7173 		    sata_device->satadev_addr.qual == SATA_ADDR_PMPORT) {
7174 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7175 			mutex_enter(&pmportinfo->pmport_mutex);
7176 			sata_update_pmport_info(sata_hba_inst, sata_device);
7177 			mutex_exit(&pmportinfo->pmport_mutex);
7178 			mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7179 		} else {
7180 			sata_update_port_info(sata_hba_inst, sata_device);
7181 		}
7182 
7183 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
7184 		*rval = TRAN_FATAL_ERROR;
7185 		break;
7186 
7187 	case SATA_TRAN_CMD_UNSUPPORTED:
7188 		/*
7189 		 * Command rejected by HBA as unsupported. It was HBA driver
7190 		 * that rejected the command, command was not sent to
7191 		 * an attached device.
7192 		 */
7193 		if ((sdinfo != NULL) &&
7194 		    (sdinfo->satadrv_state & SATA_DSTATE_RESET))
7195 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7196 			    "sat_hba_start: cmd 0x%2x rejected "
7197 			    "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd);
7198 
7199 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7200 		(void) sata_txlt_invalid_command(spx);
7201 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
7202 
7203 		*rval = TRAN_ACCEPT;
7204 		break;
7205 
7206 	case SATA_TRAN_BUSY:
7207 		/*
7208 		 * Command rejected by HBA because other operation prevents
7209 		 * accepting the packet, or device is in RESET condition.
7210 		 */
7211 		if (sdinfo != NULL) {
7212 			sdinfo->satadrv_state =
7213 			    spx->txlt_sata_pkt->satapkt_device.satadev_state;
7214 
7215 			if (sdinfo->satadrv_state & SATA_DSTATE_RESET) {
7216 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7217 				    "sata_hba_start: cmd 0x%2x rejected "
7218 				    "because of device reset condition\n",
7219 				    cmd);
7220 			} else {
7221 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
7222 				    "sata_hba_start: cmd 0x%2x rejected "
7223 				    "with SATA_TRAN_BUSY status\n",
7224 				    cmd);
7225 			}
7226 		}
7227 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
7228 		*rval = TRAN_BUSY;
7229 		break;
7230 
7231 	default:
7232 		/* Unrecognized HBA response */
7233 		SATA_LOG_D((sata_hba_inst, CE_WARN,
7234 		    "sata_hba_start: unrecognized HBA response "
7235 		    "to cmd : 0x%2x resp 0x%x", cmd, rval));
7236 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
7237 		*rval = TRAN_FATAL_ERROR;
7238 		break;
7239 	}
7240 
7241 	/*
7242 	 * If we got here, the packet was rejected.
7243 	 * Check if we need to remember reset state clearing request
7244 	 */
7245 	if (cmd_flags.sata_clear_dev_reset) {
7246 		/*
7247 		 * Check if device is still configured - it may have
7248 		 * disapeared from the configuration
7249 		 */
7250 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
7251 		if (sdinfo != NULL) {
7252 			/*
7253 			 * Restore the flag that requests clearing of
7254 			 * the device reset state,
7255 			 * so the next sata packet may carry it to HBA.
7256 			 */
7257 			if (sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT ||
7258 			    sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT) {
7259 				pminfo->pmult_event_flags |=
7260 				    SATA_EVNT_CLEAR_DEVICE_RESET;
7261 			} else {
7262 				sdinfo->satadrv_event_flags |=
7263 				    SATA_EVNT_CLEAR_DEVICE_RESET;
7264 			}
7265 		}
7266 	}
7267 	return (-1);
7268 }
7269 
7270 /*
7271  * Scsi response setup for invalid LBA
7272  *
7273  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
7274  */
7275 static int
7276 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx)
7277 {
7278 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7279 	struct scsi_extended_sense *sense;
7280 
7281 	scsipkt->pkt_reason = CMD_CMPLT;
7282 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7283 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7284 	*scsipkt->pkt_scbp = STATUS_CHECK;
7285 
7286 	*scsipkt->pkt_scbp = STATUS_CHECK;
7287 	sense = sata_arq_sense(spx);
7288 	sense->es_key = KEY_ILLEGAL_REQUEST;
7289 	sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7290 
7291 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7292 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7293 
7294 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7295 	    scsipkt->pkt_comp != NULL) {
7296 		/* scsi callback required */
7297 		if (servicing_interrupt()) {
7298 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7299 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7300 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
7301 			    TASKQID_INVALID) {
7302 				return (TRAN_BUSY);
7303 			}
7304 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7305 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7306 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
7307 			/* Scheduling the callback failed */
7308 			return (TRAN_BUSY);
7309 		}
7310 	}
7311 	return (TRAN_ACCEPT);
7312 }
7313 
7314 
7315 /*
7316  * Analyze device status and error registers and translate them into
7317  * appropriate scsi sense codes.
7318  * NOTE: non-packet commands only for now
7319  */
7320 static void
7321 sata_decode_device_error(sata_pkt_txlate_t *spx,
7322     struct scsi_extended_sense *sense)
7323 {
7324 	uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg;
7325 
7326 	ASSERT(sense != NULL);
7327 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
7328 	    SATA_STATUS_ERR);
7329 
7330 
7331 	if (err_reg & SATA_ERROR_ICRC) {
7332 		sense->es_key = KEY_ABORTED_COMMAND;
7333 		sense->es_add_code = 0x08; /* Communication failure */
7334 		return;
7335 	}
7336 
7337 	if (err_reg & SATA_ERROR_UNC) {
7338 		sense->es_key = KEY_MEDIUM_ERROR;
7339 		/* Information bytes (LBA) need to be set by a caller */
7340 		return;
7341 	}
7342 
7343 	/* ADD HERE: MC error bit handling for ATAPI CD/DVD */
7344 	if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) {
7345 		sense->es_key = KEY_UNIT_ATTENTION;
7346 		sense->es_add_code = 0x3a; /* No media present */
7347 		return;
7348 	}
7349 
7350 	if (err_reg & SATA_ERROR_IDNF) {
7351 		if (err_reg & SATA_ERROR_ABORT) {
7352 			sense->es_key = KEY_ABORTED_COMMAND;
7353 		} else {
7354 			sense->es_key = KEY_ILLEGAL_REQUEST;
7355 			sense->es_add_code = 0x21; /* LBA out of range */
7356 		}
7357 		return;
7358 	}
7359 
7360 	if (err_reg & SATA_ERROR_ABORT) {
7361 		ASSERT(spx->txlt_sata_pkt != NULL);
7362 		sense->es_key = KEY_ABORTED_COMMAND;
7363 		return;
7364 	}
7365 }
7366 
7367 /*
7368  * Extract error LBA from sata_pkt.satapkt_cmd register fields
7369  */
7370 static void
7371 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba)
7372 {
7373 	sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd;
7374 
7375 	*lba = 0;
7376 	if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) {
7377 		*lba = sata_cmd->satacmd_lba_high_msb;
7378 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb;
7379 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb;
7380 	} else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) {
7381 		*lba = sata_cmd->satacmd_device_reg & 0xf;
7382 	}
7383 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb;
7384 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb;
7385 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb;
7386 }
7387 
7388 /*
7389  * This is fixed sense format - if LBA exceeds the info field size,
7390  * no valid info will be returned (valid bit in extended sense will
7391  * be set to 0).
7392  */
7393 static struct scsi_extended_sense *
7394 sata_arq_sense(sata_pkt_txlate_t *spx)
7395 {
7396 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7397 	struct scsi_arq_status *arqs;
7398 	struct scsi_extended_sense *sense;
7399 
7400 	/* Fill ARQ sense data */
7401 	scsipkt->pkt_state |= STATE_ARQ_DONE;
7402 	arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp;
7403 	*(uchar_t *)&arqs->sts_status = STATUS_CHECK;
7404 	*(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD;
7405 	arqs->sts_rqpkt_reason = CMD_CMPLT;
7406 	arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7407 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
7408 	arqs->sts_rqpkt_resid = 0;
7409 	sense = &arqs->sts_sensedata;
7410 	bzero(sense, sizeof (struct scsi_extended_sense));
7411 	sata_fixed_sense_data_preset(sense);
7412 	return (sense);
7413 }
7414 
7415 /*
7416  * ATA Pass Through support
7417  * Sets flags indicating that an invalid value was found in some
7418  * field in the command.  It could be something illegal according to
7419  * the SAT-2 spec or it could be a feature that is not (yet?)
7420  * supported.
7421  */
7422 static int
7423 sata_txlt_ata_pass_thru_illegal_cmd(sata_pkt_txlate_t *spx)
7424 {
7425 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7426 	struct scsi_extended_sense *sense = sata_arq_sense(spx);
7427 
7428 	scsipkt->pkt_reason = CMD_CMPLT;
7429 	*scsipkt->pkt_scbp = STATUS_CHECK;
7430 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7431 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7432 
7433 	sense = sata_arq_sense(spx);
7434 	sense->es_key = KEY_ILLEGAL_REQUEST;
7435 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
7436 
7437 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7438 	    scsipkt->pkt_comp != NULL) {
7439 		/* scsi callback required */
7440 		if (servicing_interrupt()) {
7441 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7442 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7443 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
7444 			    TASKQID_INVALID) {
7445 				return (TRAN_BUSY);
7446 			}
7447 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7448 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7449 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
7450 			/* Scheduling the callback failed */
7451 			return (TRAN_BUSY);
7452 		}
7453 	}
7454 
7455 	return (TRAN_ACCEPT);
7456 }
7457 
7458 /*
7459  * The UNMAP command considers it not to be an error if the parameter length
7460  * or block descriptor length is 0.  For this case, there is nothing for TRIM
7461  * to do so just complete the command.
7462  */
7463 static int
7464 sata_txlt_unmap_nodata_cmd(sata_pkt_txlate_t *spx)
7465 {
7466 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7467 
7468 	scsipkt->pkt_reason = CMD_CMPLT;
7469 	*scsipkt->pkt_scbp = STATUS_GOOD;
7470 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7471 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7472 
7473 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7474 	    scsipkt->pkt_comp != NULL) {
7475 		/* scsi callback required */
7476 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7477 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7478 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
7479 			/* Scheduling the callback failed */
7480 			return (TRAN_BUSY);
7481 		}
7482 	}
7483 
7484 	return (TRAN_ACCEPT);
7485 }
7486 
7487 /*
7488  * Emulated SATA Read/Write command completion for zero-length requests.
7489  * This request always succedes, so in synchronous mode it always returns
7490  * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the
7491  * callback cannot be scheduled.
7492  */
7493 static int
7494 sata_emul_rw_completion(sata_pkt_txlate_t *spx)
7495 {
7496 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7497 
7498 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7499 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7500 	scsipkt->pkt_reason = CMD_CMPLT;
7501 	*scsipkt->pkt_scbp = STATUS_GOOD;
7502 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
7503 		/* scsi callback required - have to schedule it */
7504 		if (servicing_interrupt()) {
7505 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7506 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7507 			    (void *)spx->txlt_scsi_pkt, TQ_NOSLEEP) ==
7508 			    TASKQID_INVALID) {
7509 				return (TRAN_BUSY);
7510 			}
7511 		} else if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
7512 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
7513 		    (void *)spx->txlt_scsi_pkt, TQ_SLEEP) == TASKQID_INVALID) {
7514 			/* Scheduling the callback failed */
7515 			return (TRAN_BUSY);
7516 		}
7517 	}
7518 	return (TRAN_ACCEPT);
7519 }
7520 
7521 
7522 /*
7523  * Translate completion status of SATA read/write commands into scsi response.
7524  * pkt completion_reason is checked to determine the completion status.
7525  * Do scsi callback if necessary.
7526  *
7527  * Note: this function may be called also for synchronously executed
7528  * commands.
7529  * This function may be used only if scsi_pkt is non-NULL.
7530  */
7531 static void
7532 sata_txlt_rw_completion(sata_pkt_t *sata_pkt)
7533 {
7534 	sata_pkt_txlate_t *spx =
7535 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7536 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7537 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7538 	struct scsi_extended_sense *sense;
7539 	uint64_t lba;
7540 	struct buf *bp;
7541 	int rval;
7542 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7543 		/* Normal completion */
7544 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7545 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7546 		scsipkt->pkt_reason = CMD_CMPLT;
7547 		*scsipkt->pkt_scbp = STATUS_GOOD;
7548 		if (spx->txlt_tmp_buf != NULL) {
7549 			/* Temporary buffer was used */
7550 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7551 			if (bp->b_flags & B_READ) {
7552 				rval = ddi_dma_sync(
7553 				    spx->txlt_buf_dma_handle, 0, 0,
7554 				    DDI_DMA_SYNC_FORCPU);
7555 				ASSERT(rval == DDI_SUCCESS);
7556 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7557 				    bp->b_bcount);
7558 			}
7559 		}
7560 	} else {
7561 		/*
7562 		 * Something went wrong - analyze return
7563 		 */
7564 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7565 		    STATE_SENT_CMD | STATE_GOT_STATUS;
7566 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7567 		*scsipkt->pkt_scbp = STATUS_CHECK;
7568 		sense = sata_arq_sense(spx);
7569 		ASSERT(sense != NULL);
7570 
7571 		/*
7572 		 * SATA_PKT_DEV_ERROR is the only case where we may be able to
7573 		 * extract from device registers the failing LBA.
7574 		 */
7575 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7576 			if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) &&
7577 			    (scmd->satacmd_lba_mid_msb != 0 ||
7578 			    scmd->satacmd_lba_high_msb != 0)) {
7579 				/*
7580 				 * We have problem reporting this cmd LBA
7581 				 * in fixed sense data format, because of
7582 				 * the size of the scsi LBA fields.
7583 				 */
7584 				sense->es_valid = 0;
7585 			} else {
7586 				sata_extract_error_lba(spx, &lba);
7587 				sense->es_info_1 = (lba & 0xFF000000) >> 24;
7588 				sense->es_info_2 = (lba & 0xFF0000) >> 16;
7589 				sense->es_info_3 = (lba & 0xFF00) >> 8;
7590 				sense->es_info_4 = lba & 0xFF;
7591 			}
7592 		} else {
7593 			/* Invalid extended sense info */
7594 			sense->es_valid = 0;
7595 		}
7596 
7597 		switch (sata_pkt->satapkt_reason) {
7598 		case SATA_PKT_PORT_ERROR:
7599 			/* We may want to handle DEV GONE state as well */
7600 			/*
7601 			 * We have no device data. Assume no data transfered.
7602 			 */
7603 			sense->es_key = KEY_HARDWARE_ERROR;
7604 			break;
7605 
7606 		case SATA_PKT_DEV_ERROR:
7607 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
7608 			    SATA_STATUS_ERR) {
7609 				/*
7610 				 * determine dev error reason from error
7611 				 * reg content
7612 				 */
7613 				sata_decode_device_error(spx, sense);
7614 				if (sense->es_key == KEY_MEDIUM_ERROR) {
7615 					switch (scmd->satacmd_cmd_reg) {
7616 					case SATAC_READ_DMA:
7617 					case SATAC_READ_DMA_EXT:
7618 					case SATAC_READ_DMA_QUEUED:
7619 					case SATAC_READ_DMA_QUEUED_EXT:
7620 					case SATAC_READ_FPDMA_QUEUED:
7621 						/* Unrecovered read error */
7622 						sense->es_add_code =
7623 						    SD_SCSI_ASC_UNREC_READ_ERR;
7624 						break;
7625 					case SATAC_WRITE_DMA:
7626 					case SATAC_WRITE_DMA_EXT:
7627 					case SATAC_WRITE_DMA_QUEUED:
7628 					case SATAC_WRITE_DMA_QUEUED_EXT:
7629 					case SATAC_WRITE_FPDMA_QUEUED:
7630 						/* Write error */
7631 						sense->es_add_code =
7632 						    SD_SCSI_ASC_WRITE_ERR;
7633 						break;
7634 					default:
7635 						/* Internal error */
7636 						SATA_LOG_D((
7637 						    spx->txlt_sata_hba_inst,
7638 						    CE_WARN,
7639 						    "sata_txlt_rw_completion :"
7640 						    "internal error - invalid "
7641 						    "command 0x%2x",
7642 						    scmd->satacmd_cmd_reg));
7643 						break;
7644 					}
7645 				}
7646 				break;
7647 			}
7648 			/* No extended sense key - no info available */
7649 			scsipkt->pkt_reason = CMD_INCOMPLETE;
7650 			break;
7651 
7652 		case SATA_PKT_TIMEOUT:
7653 			scsipkt->pkt_reason = CMD_TIMEOUT;
7654 			scsipkt->pkt_statistics |=
7655 			    STAT_TIMEOUT | STAT_DEV_RESET;
7656 			sense->es_key = KEY_ABORTED_COMMAND;
7657 			break;
7658 
7659 		case SATA_PKT_ABORTED:
7660 			scsipkt->pkt_reason = CMD_ABORTED;
7661 			scsipkt->pkt_statistics |= STAT_ABORTED;
7662 			sense->es_key = KEY_ABORTED_COMMAND;
7663 			break;
7664 
7665 		case SATA_PKT_RESET:
7666 			scsipkt->pkt_reason = CMD_RESET;
7667 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
7668 			sense->es_key = KEY_ABORTED_COMMAND;
7669 			break;
7670 
7671 		default:
7672 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7673 			    "sata_txlt_rw_completion: "
7674 			    "invalid packet completion reason"));
7675 			scsipkt->pkt_reason = CMD_TRAN_ERR;
7676 			break;
7677 		}
7678 	}
7679 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7680 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
7681 
7682 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7683 		/* scsi callback required */
7684 		scsi_hba_pkt_comp(scsipkt);
7685 }
7686 
7687 
7688 /*
7689  * Translate completion status of non-data commands (i.e. commands returning
7690  * no data).
7691  * pkt completion_reason is checked to determine the completion status.
7692  * Do scsi callback if necessary (FLAG_NOINTR == 0)
7693  *
7694  * Note: this function may be called also for synchronously executed
7695  * commands.
7696  * This function may be used only if scsi_pkt is non-NULL.
7697  */
7698 
7699 static	void
7700 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt)
7701 {
7702 	sata_pkt_txlate_t *spx =
7703 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7704 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7705 
7706 	sata_set_arq_data(sata_pkt);
7707 
7708 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7709 		/* scsi callback required */
7710 		scsi_hba_pkt_comp(scsipkt);
7711 }
7712 
7713 /*
7714  * Completion handler for ATA Pass Through command
7715  */
7716 static void
7717 sata_txlt_apt_completion(sata_pkt_t *sata_pkt)
7718 {
7719 	sata_pkt_txlate_t *spx =
7720 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7721 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7722 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7723 	struct buf *bp;
7724 	uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0;
7725 
7726 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7727 		/* Normal completion */
7728 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7729 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7730 		scsipkt->pkt_reason = CMD_CMPLT;
7731 		*scsipkt->pkt_scbp = STATUS_GOOD;
7732 
7733 		/*
7734 		 * If the command has CK_COND set
7735 		 */
7736 		if (scsipkt->pkt_cdbp[2] & SATL_APT_BM_CK_COND) {
7737 			*scsipkt->pkt_scbp = STATUS_CHECK;
7738 			sata_fill_ata_return_desc(sata_pkt,
7739 			    KEY_RECOVERABLE_ERROR,
7740 			    SD_SCSI_ASC_APT_INFO_AVAIL, 0x1d);
7741 		}
7742 
7743 		if (spx->txlt_tmp_buf != NULL) {
7744 			/* Temporary buffer was used */
7745 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7746 			if (bp->b_flags & B_READ) {
7747 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7748 				    bp->b_bcount);
7749 			}
7750 		}
7751 	} else {
7752 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7753 		    STATE_SENT_CMD | STATE_GOT_STATUS;
7754 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7755 		*scsipkt->pkt_scbp = STATUS_CHECK;
7756 
7757 		/*
7758 		 * If DF or ERR was set, the HBA should have copied out the
7759 		 * status and error registers to the satacmd structure.
7760 		 */
7761 		if (scmd->satacmd_status_reg & SATA_STATUS_DF) {
7762 			sense_key = KEY_HARDWARE_ERROR;
7763 			addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE;
7764 			addl_sense_qual = 0;
7765 		} else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
7766 			if (scmd->satacmd_error_reg & SATA_ERROR_NM) {
7767 				sense_key = KEY_NOT_READY;
7768 				addl_sense_code =
7769 				    SD_SCSI_ASC_MEDIUM_NOT_PRESENT;
7770 				addl_sense_qual = 0;
7771 			} else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) {
7772 				sense_key = KEY_MEDIUM_ERROR;
7773 				addl_sense_code = SD_SCSI_ASC_UNREC_READ_ERR;
7774 				addl_sense_qual = 0;
7775 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) {
7776 				sense_key = KEY_DATA_PROTECT;
7777 				addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED;
7778 				addl_sense_qual = 0;
7779 			} else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) {
7780 				sense_key = KEY_ILLEGAL_REQUEST;
7781 				addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7782 				addl_sense_qual = 0;
7783 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
7784 				sense_key = KEY_ABORTED_COMMAND;
7785 				addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE;
7786 				addl_sense_qual = 0;
7787 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MC) {
7788 				sense_key = KEY_UNIT_ATTENTION;
7789 				addl_sense_code =
7790 				    SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED;
7791 				addl_sense_qual = 0;
7792 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) {
7793 				sense_key = KEY_UNIT_ATTENTION;
7794 				addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ;
7795 				addl_sense_qual = 0;
7796 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) {
7797 				sense_key = KEY_ABORTED_COMMAND;
7798 				addl_sense_code =
7799 				    SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR;
7800 				addl_sense_qual = 0;
7801 			}
7802 		}
7803 
7804 		sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code,
7805 		    addl_sense_qual);
7806 	}
7807 
7808 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7809 		/* scsi callback required */
7810 		scsi_hba_pkt_comp(scsipkt);
7811 }
7812 
7813 /*
7814  * Completion handler for unmap translation command
7815  */
7816 static void
7817 sata_txlt_unmap_completion(sata_pkt_t *sata_pkt)
7818 {
7819 	sata_pkt_txlate_t *spx =
7820 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7821 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7822 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7823 	struct buf *bp;
7824 	uint8_t sense_key = 0, addl_sense_code = 0, addl_sense_qual = 0;
7825 
7826 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7827 		/* Normal completion */
7828 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7829 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
7830 		scsipkt->pkt_reason = CMD_CMPLT;
7831 		*scsipkt->pkt_scbp = STATUS_GOOD;
7832 
7833 		if (spx->txlt_tmp_buf != NULL) {
7834 			/* Temporary buffer was used */
7835 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7836 			if (bp->b_flags & B_READ) {
7837 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7838 				    bp->b_bcount);
7839 			}
7840 		}
7841 	} else {
7842 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7843 		    STATE_SENT_CMD | STATE_GOT_STATUS;
7844 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7845 		*scsipkt->pkt_scbp = STATUS_CHECK;
7846 
7847 		/*
7848 		 * If DF or ERR was set, the HBA should have copied out the
7849 		 * status and error registers to the satacmd structure.
7850 		 */
7851 		if (scmd->satacmd_status_reg & SATA_STATUS_DF) {
7852 			sense_key = KEY_HARDWARE_ERROR;
7853 			addl_sense_code = SD_SCSI_ASC_INTERNAL_TARGET_FAILURE;
7854 			addl_sense_qual = 0;
7855 		} else if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
7856 			if (scmd->satacmd_error_reg & SATA_ERROR_NM) {
7857 				sense_key = KEY_NOT_READY;
7858 				addl_sense_code =
7859 				    SD_SCSI_ASC_MEDIUM_NOT_PRESENT;
7860 				addl_sense_qual = 0;
7861 			} else if (scmd->satacmd_error_reg & SATA_ERROR_UNC) {
7862 				sense_key = KEY_MEDIUM_ERROR;
7863 				addl_sense_code = SD_SCSI_ASC_WRITE_ERR;
7864 				addl_sense_qual = 0;
7865 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ILI) {
7866 				sense_key = KEY_DATA_PROTECT;
7867 				addl_sense_code = SD_SCSI_ASC_WRITE_PROTECTED;
7868 				addl_sense_qual = 0;
7869 			} else if (scmd->satacmd_error_reg & SATA_ERROR_IDNF) {
7870 				sense_key = KEY_ILLEGAL_REQUEST;
7871 				addl_sense_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
7872 				addl_sense_qual = 0;
7873 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
7874 				sense_key = KEY_ABORTED_COMMAND;
7875 				addl_sense_code = SD_SCSI_ASC_NO_ADD_SENSE;
7876 				addl_sense_qual = 0;
7877 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MC) {
7878 				sense_key = KEY_UNIT_ATTENTION;
7879 				addl_sense_code =
7880 				    SD_SCSI_ASC_MEDIUM_MAY_HAVE_CHANGED;
7881 				addl_sense_qual = 0;
7882 			} else if (scmd->satacmd_error_reg & SATA_ERROR_MCR) {
7883 				sense_key = KEY_UNIT_ATTENTION;
7884 				addl_sense_code = SD_SCSI_ASC_OP_MEDIUM_REM_REQ;
7885 				addl_sense_qual = 0;
7886 			} else if (scmd->satacmd_error_reg & SATA_ERROR_ICRC) {
7887 				sense_key = KEY_ABORTED_COMMAND;
7888 				addl_sense_code =
7889 				    SD_SCSI_ASC_INFO_UNIT_IUCRC_ERR;
7890 				addl_sense_qual = 0;
7891 			}
7892 		}
7893 
7894 		sata_fill_ata_return_desc(sata_pkt, sense_key, addl_sense_code,
7895 		    addl_sense_qual);
7896 	}
7897 
7898 	sata_free_local_buffer(spx);
7899 
7900 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
7901 		/* scsi callback required */
7902 		scsi_hba_pkt_comp(scsipkt);
7903 }
7904 
7905 /*
7906  *
7907  */
7908 static void
7909 sata_fill_ata_return_desc(sata_pkt_t *sata_pkt, uint8_t sense_key,
7910     uint8_t addl_sense_code, uint8_t addl_sense_qual)
7911 {
7912 	sata_pkt_txlate_t *spx =
7913 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7914 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
7915 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7916 	struct sata_apt_sense_data *apt_sd =
7917 	    (struct sata_apt_sense_data *)scsipkt->pkt_scbp;
7918 	struct scsi_descr_sense_hdr *sds = &(apt_sd->apt_sd_hdr);
7919 	struct scsi_ata_status_ret_sense_descr *ata_ret_desc =
7920 	    &(apt_sd->apt_sd_sense);
7921 	int extend = 0;
7922 
7923 	if ((scsipkt->pkt_cdbp[0] == SPC3_CMD_ATA_COMMAND_PASS_THROUGH16) &&
7924 	    (scsipkt->pkt_cdbp[2] & SATL_APT_BM_EXTEND))
7925 		extend = 1;
7926 
7927 	scsipkt->pkt_state |= STATE_ARQ_DONE;
7928 
7929 	/* update the residual count */
7930 	*(uchar_t *)&apt_sd->apt_status = STATUS_CHECK;
7931 	*(uchar_t *)&apt_sd->apt_rqpkt_status = STATUS_GOOD;
7932 	apt_sd->apt_rqpkt_reason = CMD_CMPLT;
7933 	apt_sd->apt_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7934 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
7935 	apt_sd->apt_rqpkt_resid = scsipkt->pkt_scblen -
7936 	    sizeof (struct sata_apt_sense_data);
7937 
7938 	/*
7939 	 * Fill in the Descriptor sense header
7940 	 */
7941 	bzero(sds, sizeof (struct scsi_descr_sense_hdr));
7942 	sds->ds_code = CODE_FMT_DESCR_CURRENT;
7943 	sds->ds_class = CLASS_EXTENDED_SENSE;
7944 	sds->ds_key = sense_key & 0xf;
7945 	sds->ds_add_code = addl_sense_code;
7946 	sds->ds_qual_code = addl_sense_qual;
7947 	sds->ds_addl_sense_length =
7948 	    sizeof (struct scsi_ata_status_ret_sense_descr);
7949 
7950 	/*
7951 	 * Fill in the ATA Return descriptor sense data
7952 	 */
7953 	bzero(ata_ret_desc, sizeof (struct scsi_ata_status_ret_sense_descr));
7954 	ata_ret_desc->ars_descr_type = DESCR_ATA_STATUS_RETURN;
7955 	ata_ret_desc->ars_addl_length = 0xc;
7956 	ata_ret_desc->ars_error = scmd->satacmd_error_reg;
7957 	ata_ret_desc->ars_sec_count_lsb = scmd->satacmd_sec_count_lsb;
7958 	ata_ret_desc->ars_lba_low_lsb = scmd->satacmd_lba_low_lsb;
7959 	ata_ret_desc->ars_lba_mid_lsb = scmd->satacmd_lba_mid_lsb;
7960 	ata_ret_desc->ars_lba_high_lsb = scmd->satacmd_lba_high_lsb;
7961 	ata_ret_desc->ars_device = scmd->satacmd_device_reg;
7962 	ata_ret_desc->ars_status = scmd->satacmd_status_reg;
7963 
7964 	if (extend == 1) {
7965 		ata_ret_desc->ars_extend = 1;
7966 		ata_ret_desc->ars_sec_count_msb = scmd->satacmd_sec_count_msb;
7967 		ata_ret_desc->ars_lba_low_msb = scmd->satacmd_lba_low_msb;
7968 		ata_ret_desc->ars_lba_mid_msb = scmd->satacmd_lba_mid_msb;
7969 		ata_ret_desc->ars_lba_high_msb = scmd->satacmd_lba_high_msb;
7970 	} else {
7971 		ata_ret_desc->ars_extend = 0;
7972 		ata_ret_desc->ars_sec_count_msb = 0;
7973 		ata_ret_desc->ars_lba_low_msb = 0;
7974 		ata_ret_desc->ars_lba_mid_msb = 0;
7975 		ata_ret_desc->ars_lba_high_msb = 0;
7976 	}
7977 }
7978 
7979 static	void
7980 sata_set_arq_data(sata_pkt_t *sata_pkt)
7981 {
7982 	sata_pkt_txlate_t *spx =
7983 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7984 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7985 	struct scsi_extended_sense *sense;
7986 
7987 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7988 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7989 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7990 		/* Normal completion */
7991 		scsipkt->pkt_reason = CMD_CMPLT;
7992 		*scsipkt->pkt_scbp = STATUS_GOOD;
7993 	} else {
7994 		/* Something went wrong */
7995 		scsipkt->pkt_reason = CMD_INCOMPLETE;
7996 		*scsipkt->pkt_scbp = STATUS_CHECK;
7997 		sense = sata_arq_sense(spx);
7998 		switch (sata_pkt->satapkt_reason) {
7999 		case SATA_PKT_PORT_ERROR:
8000 			/*
8001 			 * We have no device data. Assume no data transfered.
8002 			 */
8003 			sense->es_key = KEY_HARDWARE_ERROR;
8004 			break;
8005 
8006 		case SATA_PKT_DEV_ERROR:
8007 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
8008 			    SATA_STATUS_ERR) {
8009 				/*
8010 				 * determine dev error reason from error
8011 				 * reg content
8012 				 */
8013 				sata_decode_device_error(spx, sense);
8014 				break;
8015 			}
8016 			/* No extended sense key - no info available */
8017 			break;
8018 
8019 		case SATA_PKT_TIMEOUT:
8020 			scsipkt->pkt_reason = CMD_TIMEOUT;
8021 			scsipkt->pkt_statistics |=
8022 			    STAT_TIMEOUT | STAT_DEV_RESET;
8023 			/* No extended sense key ? */
8024 			break;
8025 
8026 		case SATA_PKT_ABORTED:
8027 			scsipkt->pkt_reason = CMD_ABORTED;
8028 			scsipkt->pkt_statistics |= STAT_ABORTED;
8029 			/* No extended sense key ? */
8030 			break;
8031 
8032 		case SATA_PKT_RESET:
8033 			/* pkt aborted by an explicit reset from a host */
8034 			scsipkt->pkt_reason = CMD_RESET;
8035 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
8036 			break;
8037 
8038 		default:
8039 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
8040 			    "sata_txlt_nodata_cmd_completion: "
8041 			    "invalid packet completion reason %d",
8042 			    sata_pkt->satapkt_reason));
8043 			scsipkt->pkt_reason = CMD_TRAN_ERR;
8044 			break;
8045 		}
8046 
8047 	}
8048 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
8049 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
8050 }
8051 
8052 
8053 /*
8054  * Build Mode sense R/W recovery page
8055  * NOT IMPLEMENTED
8056  */
8057 
8058 static int
8059 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8060 {
8061 #ifndef __lock_lint
8062 	_NOTE(ARGUNUSED(sdinfo))
8063 	_NOTE(ARGUNUSED(pcntrl))
8064 	_NOTE(ARGUNUSED(buf))
8065 #endif
8066 	return (0);
8067 }
8068 
8069 /*
8070  * Build Mode sense caching page  -  scsi-3 implementation.
8071  * Page length distinguishes previous format from scsi-3 format.
8072  * buf must have space for 0x12 bytes.
8073  * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable.
8074  *
8075  */
8076 static int
8077 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8078 {
8079 	struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf;
8080 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8081 
8082 	/*
8083 	 * Most of the fields are set to 0, being not supported and/or disabled
8084 	 */
8085 	bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3);
8086 
8087 	/* Saved paramters not supported */
8088 	if (pcntrl == 3)
8089 		return (0);
8090 	if (pcntrl == 0 || pcntrl == 2) {
8091 		/*
8092 		 * For now treat current and default parameters as same
8093 		 * That may have to change, if target driver will complain
8094 		 */
8095 		page->mode_page.code = MODEPAGE_CACHING;	/* PS = 0 */
8096 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
8097 
8098 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id) &&
8099 		    !SATA_READ_AHEAD_ENABLED(*sata_id)) {
8100 			page->dra = 1;		/* Read Ahead disabled */
8101 			page->rcd = 1;		/* Read Cache disabled */
8102 		}
8103 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id) &&
8104 		    SATA_WRITE_CACHE_ENABLED(*sata_id))
8105 			page->wce = 1;		/* Write Cache enabled */
8106 	} else {
8107 		/* Changeable parameters */
8108 		page->mode_page.code = MODEPAGE_CACHING;
8109 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
8110 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
8111 			page->dra = 1;
8112 			page->rcd = 1;
8113 		}
8114 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id))
8115 			page->wce = 1;
8116 	}
8117 	return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
8118 	    sizeof (struct mode_page));
8119 }
8120 
8121 /*
8122  * Build Mode sense exception cntrl page
8123  */
8124 static int
8125 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8126 {
8127 	struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf;
8128 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8129 
8130 	/*
8131 	 * Most of the fields are set to 0, being not supported and/or disabled
8132 	 */
8133 	bzero(buf, PAGELENGTH_INFO_EXCPT);
8134 
8135 	page->mode_page.code = MODEPAGE_INFO_EXCPT;
8136 	page->mode_page.length = PAGELENGTH_INFO_EXCPT;
8137 
8138 	/* Indicate that this is page is saveable */
8139 	page->mode_page.ps = 1;
8140 
8141 	/*
8142 	 * We will return the same data for default, current and saved page.
8143 	 * The only changeable bit is dexcpt and that bit is required
8144 	 * by the ATA specification to be preserved across power cycles.
8145 	 */
8146 	if (pcntrl != 1) {
8147 		page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED);
8148 		page->mrie = MRIE_ONLY_ON_REQUEST;
8149 	}
8150 	else
8151 		page->dexcpt = 1;	/* Only changeable parameter */
8152 
8153 	return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page));
8154 }
8155 
8156 
8157 static int
8158 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8159 {
8160 	struct mode_acoustic_management *page =
8161 	    (struct mode_acoustic_management *)buf;
8162 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8163 
8164 	/*
8165 	 * Most of the fields are set to 0, being not supported and/or disabled
8166 	 */
8167 	bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT);
8168 
8169 	switch (pcntrl) {
8170 	case P_CNTRL_DEFAULT:
8171 		/*  default paramters not supported */
8172 		return (0);
8173 
8174 	case P_CNTRL_CURRENT:
8175 	case P_CNTRL_SAVED:
8176 		/* Saved and current are supported and are identical */
8177 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
8178 		page->mode_page.length =
8179 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
8180 		page->mode_page.ps = 1;
8181 
8182 		/* Word 83 indicates if feature is supported */
8183 		/* If feature is not supported */
8184 		if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) {
8185 			page->acoustic_manag_enable =
8186 			    ACOUSTIC_DISABLED;
8187 		} else {
8188 			page->acoustic_manag_enable =
8189 			    ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT)
8190 			    != 0);
8191 			/* Word 94 inidicates the value */
8192 #ifdef	_LITTLE_ENDIAN
8193 			page->acoustic_manag_level =
8194 			    (uchar_t)sata_id->ai_acoustic;
8195 			page->vendor_recommended_value =
8196 			    sata_id->ai_acoustic >> 8;
8197 #else
8198 			page->acoustic_manag_level =
8199 			    sata_id->ai_acoustic >> 8;
8200 			page->vendor_recommended_value =
8201 			    (uchar_t)sata_id->ai_acoustic;
8202 #endif
8203 		}
8204 		break;
8205 
8206 	case P_CNTRL_CHANGEABLE:
8207 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
8208 		page->mode_page.length =
8209 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
8210 		page->mode_page.ps = 1;
8211 
8212 		/* Word 83 indicates if the feature is supported */
8213 		if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) {
8214 			page->acoustic_manag_enable =
8215 			    ACOUSTIC_ENABLED;
8216 			page->acoustic_manag_level = 0xff;
8217 		}
8218 		break;
8219 	}
8220 	return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8221 	    sizeof (struct mode_page));
8222 }
8223 
8224 
8225 /*
8226  * Build Mode sense power condition page.
8227  */
8228 static int
8229 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
8230 {
8231 	struct mode_info_power_cond *page = (struct mode_info_power_cond *)buf;
8232 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8233 
8234 	/*
8235 	 * Most of the fields are set to 0, being not supported and/or disabled
8236 	 * power condition page length was 0x0a
8237 	 */
8238 	bzero(buf, sizeof (struct mode_info_power_cond));
8239 
8240 	if (pcntrl == P_CNTRL_DEFAULT) {
8241 		/*  default paramters not supported */
8242 		return (0);
8243 	}
8244 
8245 	page->mode_page.code = MODEPAGE_POWER_COND;
8246 	page->mode_page.length = sizeof (struct mode_info_power_cond);
8247 
8248 	if (sata_id->ai_cap & SATA_STANDBYTIMER) {
8249 		page->standby = 1;
8250 		bcopy(sdinfo->satadrv_standby_timer, page->standby_cond_timer,
8251 		    sizeof (uchar_t) * 4);
8252 	}
8253 
8254 	return (sizeof (struct mode_info_power_cond));
8255 }
8256 
8257 /*
8258  * Process mode select caching page 8 (scsi3 format only).
8259  * Read Ahead (same as read cache) and Write Cache may be turned on and off
8260  * if these features are supported by the device. If these features are not
8261  * supported, the command will be terminated with STATUS_CHECK.
8262  * This function fails only if the SET FEATURE command sent to
8263  * the device fails. The page format is not verified, assuming that the
8264  * target driver operates correctly - if parameters length is too short,
8265  * we just drop the page.
8266  * Two command may be sent if both Read Cache/Read Ahead and Write Cache
8267  * setting have to be changed.
8268  * SET FEATURE command is executed synchronously, i.e. we wait here until
8269  * it is completed, regardless of the scsi pkt directives.
8270  *
8271  * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e.
8272  * changing DRA will change RCD.
8273  *
8274  * More than one SATA command may be executed to perform operations specified
8275  * by mode select pages. The first error terminates further execution.
8276  * Operations performed successully are not backed-up in such case.
8277  *
8278  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
8279  * If operation resulted in changing device setup, dmod flag should be set to
8280  * one (1). If parameters were not changed, dmod flag should be set to 0.
8281  * Upon return, if operation required sending command to the device, the rval
8282  * should be set to the value returned by sata_hba_start. If operation
8283  * did not require device access, rval should be set to TRAN_ACCEPT.
8284  * The pagelen should be set to the length of the page.
8285  *
8286  * This function has to be called with a port mutex held.
8287  *
8288  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8289  */
8290 int
8291 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page,
8292     int parmlen, int *pagelen, int *rval, int *dmod)
8293 {
8294 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8295 	sata_drive_info_t *sdinfo;
8296 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8297 	sata_id_t *sata_id;
8298 	struct scsi_extended_sense *sense;
8299 	int wce, dra;	/* Current settings */
8300 
8301 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8302 	    &spx->txlt_sata_pkt->satapkt_device);
8303 	sata_id = &sdinfo->satadrv_id;
8304 	*dmod = 0;
8305 
8306 	/* Verify parameters length. If too short, drop it */
8307 	if ((PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
8308 	    sizeof (struct mode_page)) > parmlen) {
8309 		*scsipkt->pkt_scbp = STATUS_CHECK;
8310 		sense = sata_arq_sense(spx);
8311 		sense->es_key = KEY_ILLEGAL_REQUEST;
8312 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8313 		*pagelen = parmlen;
8314 		*rval = TRAN_ACCEPT;
8315 		return (SATA_FAILURE);
8316 	}
8317 
8318 	*pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page);
8319 
8320 	/* Current setting of Read Ahead (and Read Cache) */
8321 	if (SATA_READ_AHEAD_ENABLED(*sata_id))
8322 		dra = 0;	/* 0 == not disabled */
8323 	else
8324 		dra = 1;
8325 	/* Current setting of Write Cache */
8326 	if (SATA_WRITE_CACHE_ENABLED(*sata_id))
8327 		wce = 1;
8328 	else
8329 		wce = 0;
8330 
8331 	if (page->dra == dra && page->wce == wce && page->rcd == dra) {
8332 		/* nothing to do */
8333 		*rval = TRAN_ACCEPT;
8334 		return (SATA_SUCCESS);
8335 	}
8336 
8337 	/*
8338 	 * Need to flip some setting
8339 	 * Set-up Internal SET FEATURES command(s)
8340 	 */
8341 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8342 	scmd->satacmd_addr_type = 0;
8343 	scmd->satacmd_device_reg = 0;
8344 	scmd->satacmd_status_reg = 0;
8345 	scmd->satacmd_error_reg = 0;
8346 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
8347 	if (page->dra != dra || page->rcd != dra) {
8348 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
8349 			/* Need to flip read ahead setting */
8350 			if (dra == 0)
8351 				/* Disable read ahead / read cache */
8352 				scmd->satacmd_features_reg =
8353 				    SATAC_SF_DISABLE_READ_AHEAD;
8354 			else
8355 				/* Enable read ahead  / read cache */
8356 				scmd->satacmd_features_reg =
8357 				    SATAC_SF_ENABLE_READ_AHEAD;
8358 
8359 			/* Transfer command to HBA */
8360 			if (sata_hba_start(spx, rval) != 0)
8361 				/*
8362 				 * Pkt not accepted for execution.
8363 				 */
8364 				return (SATA_FAILURE);
8365 
8366 			*dmod = 1;
8367 
8368 			/* Now process return */
8369 			if (spx->txlt_sata_pkt->satapkt_reason !=
8370 			    SATA_PKT_COMPLETED) {
8371 				goto failure;	/* Terminate */
8372 			}
8373 		} else {
8374 			*scsipkt->pkt_scbp = STATUS_CHECK;
8375 			sense = sata_arq_sense(spx);
8376 			sense->es_key = KEY_ILLEGAL_REQUEST;
8377 			sense->es_add_code =
8378 			    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8379 			*pagelen = parmlen;
8380 			*rval = TRAN_ACCEPT;
8381 			return (SATA_FAILURE);
8382 		}
8383 	}
8384 
8385 	/* Note that the packet is not removed, so it could be re-used */
8386 	if (page->wce != wce) {
8387 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id)) {
8388 			/* Need to flip Write Cache setting */
8389 			if (page->wce == 1)
8390 				/* Enable write cache */
8391 				scmd->satacmd_features_reg =
8392 				    SATAC_SF_ENABLE_WRITE_CACHE;
8393 			else
8394 				/* Disable write cache */
8395 				scmd->satacmd_features_reg =
8396 				    SATAC_SF_DISABLE_WRITE_CACHE;
8397 
8398 			/* Transfer command to HBA */
8399 			if (sata_hba_start(spx, rval) != 0)
8400 				/*
8401 				 * Pkt not accepted for execution.
8402 				 */
8403 				return (SATA_FAILURE);
8404 
8405 			*dmod = 1;
8406 
8407 			/* Now process return */
8408 			if (spx->txlt_sata_pkt->satapkt_reason !=
8409 			    SATA_PKT_COMPLETED) {
8410 				goto failure;
8411 			}
8412 		} else {
8413 			*scsipkt->pkt_scbp = STATUS_CHECK;
8414 			sense = sata_arq_sense(spx);
8415 			sense->es_key = KEY_ILLEGAL_REQUEST;
8416 			sense->es_add_code =
8417 			    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8418 			*pagelen = parmlen;
8419 			*rval = TRAN_ACCEPT;
8420 			return (SATA_FAILURE);
8421 		}
8422 	}
8423 	return (SATA_SUCCESS);
8424 
8425 failure:
8426 	sata_xlate_errors(spx);
8427 
8428 	return (SATA_FAILURE);
8429 }
8430 
8431 /*
8432  * Process mode select informational exceptions control page 0x1c
8433  *
8434  * The only changeable bit is dexcpt (disable exceptions).
8435  * MRIE (method of reporting informational exceptions) must be
8436  * "only on request".
8437  * This page applies to informational exceptions that report
8438  * additional sense codes with the ADDITIONAL SENSE CODE field set to 5Dh
8439  * (e.g.,FAILURE PREDICTION THRESHOLD EXCEEDED) or 0Bh (e.g., WARNING_).
8440  * Informational exception conditions occur as the result of background scan
8441  * errors, background self-test errors, or vendor specific events within a
8442  * logical unit. An informational exception condition may occur asynchronous
8443  * to any commands.
8444  *
8445  * Returns: SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
8446  * If operation resulted in changing device setup, dmod flag should be set to
8447  * one (1). If parameters were not changed, dmod flag should be set to 0.
8448  * Upon return, if operation required sending command to the device, the rval
8449  * should be set to the value returned by sata_hba_start. If operation
8450  * did not require device access, rval should be set to TRAN_ACCEPT.
8451  * The pagelen should be set to the length of the page.
8452  *
8453  * This function has to be called with a port mutex held.
8454  *
8455  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8456  *
8457  * Cannot be called in the interrupt context.
8458  */
8459 static	int
8460 sata_mode_select_page_1c(
8461 	sata_pkt_txlate_t *spx,
8462 	struct mode_info_excpt_page *page,
8463 	int parmlen,
8464 	int *pagelen,
8465 	int *rval,
8466 	int *dmod)
8467 {
8468 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8469 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8470 	sata_drive_info_t *sdinfo;
8471 	sata_id_t *sata_id;
8472 	struct scsi_extended_sense *sense;
8473 
8474 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8475 	    &spx->txlt_sata_pkt->satapkt_device);
8476 	sata_id = &sdinfo->satadrv_id;
8477 
8478 	*dmod = 0;
8479 
8480 	/* Verify parameters length. If too short, drop it */
8481 	if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) > parmlen) ||
8482 	    page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) {
8483 		*scsipkt->pkt_scbp = STATUS_CHECK;
8484 		sense = sata_arq_sense(spx);
8485 		sense->es_key = KEY_ILLEGAL_REQUEST;
8486 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8487 		*pagelen = parmlen;
8488 		*rval = TRAN_ACCEPT;
8489 		return (SATA_FAILURE);
8490 	}
8491 
8492 	*pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page);
8493 
8494 	if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
8495 		*scsipkt->pkt_scbp = STATUS_CHECK;
8496 		sense = sata_arq_sense(spx);
8497 		sense->es_key = KEY_ILLEGAL_REQUEST;
8498 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
8499 		*pagelen = parmlen;
8500 		*rval = TRAN_ACCEPT;
8501 		return (SATA_FAILURE);
8502 	}
8503 
8504 	/* If already in the state requested, we are done */
8505 	if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
8506 		/* nothing to do */
8507 		*rval = TRAN_ACCEPT;
8508 		return (SATA_SUCCESS);
8509 	}
8510 
8511 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8512 
8513 	/* Build SMART_ENABLE or SMART_DISABLE command */
8514 	scmd->satacmd_addr_type = 0;		/* N/A */
8515 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
8516 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
8517 	scmd->satacmd_features_reg = page->dexcpt ?
8518 	    SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS;
8519 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
8520 	scmd->satacmd_cmd_reg = SATAC_SMART;
8521 
8522 	/* Transfer command to HBA */
8523 	if (sata_hba_start(spx, rval) != 0)
8524 		/*
8525 		 * Pkt not accepted for execution.
8526 		 */
8527 		return (SATA_FAILURE);
8528 
8529 	*dmod = 1;	/* At least may have been modified */
8530 
8531 	/* Now process return */
8532 	if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED)
8533 		return (SATA_SUCCESS);
8534 
8535 	/* Packet did not complete successfully */
8536 	sata_xlate_errors(spx);
8537 
8538 	return (SATA_FAILURE);
8539 }
8540 
8541 /*
8542  * Process mode select acoustic management control page 0x30
8543  *
8544  *
8545  * This function has to be called with a port mutex held.
8546  *
8547  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8548  *
8549  * Cannot be called in the interrupt context.
8550  */
8551 int
8552 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct
8553     mode_acoustic_management *page, int parmlen, int *pagelen,
8554     int *rval, int *dmod)
8555 {
8556 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8557 	sata_drive_info_t *sdinfo;
8558 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8559 	sata_id_t *sata_id;
8560 	struct scsi_extended_sense *sense;
8561 
8562 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8563 	    &spx->txlt_sata_pkt->satapkt_device);
8564 	sata_id = &sdinfo->satadrv_id;
8565 	*dmod = 0;
8566 
8567 	/* If parmlen is too short or the feature is not supported, drop it */
8568 	if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8569 	    sizeof (struct mode_page)) > parmlen) ||
8570 	    (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) {
8571 		*scsipkt->pkt_scbp = STATUS_CHECK;
8572 		sense = sata_arq_sense(spx);
8573 		sense->es_key = KEY_ILLEGAL_REQUEST;
8574 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8575 		*pagelen = parmlen;
8576 		*rval = TRAN_ACCEPT;
8577 		return (SATA_FAILURE);
8578 	}
8579 
8580 	*pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
8581 	    sizeof (struct mode_page);
8582 
8583 	/*
8584 	 * We can enable and disable acoustice management and
8585 	 * set the acoustic management level.
8586 	 */
8587 
8588 	/*
8589 	 * Set-up Internal SET FEATURES command(s)
8590 	 */
8591 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
8592 	scmd->satacmd_addr_type = 0;
8593 	scmd->satacmd_device_reg = 0;
8594 	scmd->satacmd_status_reg = 0;
8595 	scmd->satacmd_error_reg = 0;
8596 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
8597 	if (page->acoustic_manag_enable) {
8598 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC;
8599 		scmd->satacmd_sec_count_lsb = page->acoustic_manag_level;
8600 	} else {	/* disabling acoustic management */
8601 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC;
8602 	}
8603 
8604 	/* Transfer command to HBA */
8605 	if (sata_hba_start(spx, rval) != 0)
8606 		/*
8607 		 * Pkt not accepted for execution.
8608 		 */
8609 		return (SATA_FAILURE);
8610 
8611 	/* Now process return */
8612 	if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) {
8613 		sata_xlate_errors(spx);
8614 		return (SATA_FAILURE);
8615 	}
8616 
8617 	*dmod = 1;
8618 
8619 	return (SATA_SUCCESS);
8620 }
8621 
8622 /*
8623  * Process mode select power condition page 0x1a
8624  *
8625  * This function has to be called with a port mutex held.
8626  *
8627  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
8628  *
8629  * Cannot be called in the interrupt context.
8630  */
8631 int
8632 sata_mode_select_page_1a(sata_pkt_txlate_t *spx, struct
8633     mode_info_power_cond *page, int parmlen, int *pagelen,
8634     int *rval, int *dmod)
8635 {
8636 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8637 	sata_drive_info_t *sdinfo;
8638 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8639 	sata_id_t *sata_id;
8640 	struct scsi_extended_sense *sense;
8641 	uint8_t ata_count;
8642 	int i, len;
8643 
8644 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
8645 	    &spx->txlt_sata_pkt->satapkt_device);
8646 	sata_id = &sdinfo->satadrv_id;
8647 	*dmod = 0;
8648 
8649 	len = sizeof (struct mode_info_power_cond);
8650 	len += sizeof (struct mode_page);
8651 
8652 	/* If parmlen is too short or the feature is not supported, drop it */
8653 	if ((len < parmlen) || (page->idle == 1) ||
8654 	    (!(sata_id->ai_cap & SATA_STANDBYTIMER) && page->standby == 1)) {
8655 		*scsipkt->pkt_scbp = STATUS_CHECK;
8656 		sense = sata_arq_sense(spx);
8657 		sense->es_key = KEY_ILLEGAL_REQUEST;
8658 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
8659 		*pagelen = parmlen;
8660 		*rval = TRAN_ACCEPT;
8661 		return (SATA_FAILURE);
8662 	}
8663 
8664 	*pagelen = len;
8665 
8666 	/*
8667 	 * Set-up Internal STANDBY command(s)
8668 	 */
8669 	if (page->standby == 0)
8670 		goto out;
8671 
8672 	ata_count = sata_get_standby_timer(page->standby_cond_timer);
8673 
8674 	scmd->satacmd_addr_type = 0;
8675 	scmd->satacmd_sec_count_lsb = ata_count;
8676 	scmd->satacmd_lba_low_lsb = 0;
8677 	scmd->satacmd_lba_mid_lsb = 0;
8678 	scmd->satacmd_lba_high_lsb = 0;
8679 	scmd->satacmd_features_reg = 0;
8680 	scmd->satacmd_device_reg = 0;
8681 	scmd->satacmd_status_reg = 0;
8682 	scmd->satacmd_cmd_reg = SATAC_STANDBY;
8683 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
8684 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
8685 
8686 	/* Transfer command to HBA */
8687 	if (sata_hba_start(spx, rval) != 0) {
8688 		return (SATA_FAILURE);
8689 	} else {
8690 		if ((scmd->satacmd_error_reg != 0) ||
8691 		    (spx->txlt_sata_pkt->satapkt_reason !=
8692 		    SATA_PKT_COMPLETED)) {
8693 			sata_xlate_errors(spx);
8694 			return (SATA_FAILURE);
8695 		}
8696 	}
8697 
8698 	for (i = 0; i < 4; i++) {
8699 		sdinfo->satadrv_standby_timer[i] = page->standby_cond_timer[i];
8700 	}
8701 out:
8702 	*dmod = 1;
8703 	return (SATA_SUCCESS);
8704 }
8705 
8706 /*
8707  * sata_build_lsense_page0() is used to create the
8708  * SCSI LOG SENSE page 0 (supported log pages)
8709  *
8710  * Currently supported pages are 0, 0x10, 0x2f, 0x30 and 0x0e
8711  * (supported log pages, self-test results, informational exceptions
8712  * Sun vendor specific ATA SMART data, and start stop cycle counter).
8713  *
8714  * Takes a sata_drive_info t * and the address of a buffer
8715  * in which to create the page information.
8716  *
8717  * Returns the number of bytes valid in the buffer.
8718  */
8719 static	int
8720 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf)
8721 {
8722 	struct log_parameter *lpp = (struct log_parameter *)buf;
8723 	uint8_t *page_ptr = (uint8_t *)lpp->param_values;
8724 	int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */
8725 	sata_id_t *sata_id = &sdinfo->satadrv_id;
8726 
8727 	lpp->param_code[0] = 0;
8728 	lpp->param_code[1] = 0;
8729 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
8730 	*page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES;
8731 
8732 	if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
8733 		if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) {
8734 			*page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS;
8735 			++num_pages_supported;
8736 		}
8737 		*page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS;
8738 		++num_pages_supported;
8739 		*page_ptr++ = PAGE_CODE_SMART_READ_DATA;
8740 		++num_pages_supported;
8741 		*page_ptr++ = PAGE_CODE_START_STOP_CYCLE_COUNTER;
8742 		++num_pages_supported;
8743 	}
8744 
8745 	lpp->param_len = num_pages_supported;
8746 
8747 	return ((&lpp->param_values[0] - (uint8_t *)lpp) +
8748 	    num_pages_supported);
8749 }
8750 
8751 /*
8752  * sata_build_lsense_page_10() is used to create the
8753  * SCSI LOG SENSE page 0x10 (self-test results)
8754  *
8755  * Takes a sata_drive_info t * and the address of a buffer
8756  * in which to create the page information as well as a sata_hba_inst_t *.
8757  *
8758  * Returns the number of bytes valid in the buffer.
8759  *
8760  * Note: Self test and SMART data is accessible in device log pages.
8761  * The log pages can be accessed by SMART READ/WRITE LOG (up to 255 sectors
8762  * of data can be transferred by a single command), or by the General Purpose
8763  * Logging commands (GPL) READ LOG EXT and WRITE LOG EXT (up to 65,535 sectors
8764  * - approximately 33MB - can be transferred by a single command.
8765  * The SCT Command response (either error or command) is the same for both
8766  * the SMART and GPL methods of issuing commands.
8767  * This function uses READ LOG EXT command when drive supports LBA48, and
8768  * SMART READ command otherwise.
8769  *
8770  * Since above commands are executed in a synchronous mode, this function
8771  * should not be called in an interrupt context.
8772  */
8773 static	int
8774 sata_build_lsense_page_10(
8775 	sata_drive_info_t *sdinfo,
8776 	uint8_t *buf,
8777 	sata_hba_inst_t *sata_hba_inst)
8778 {
8779 	struct log_parameter *lpp = (struct log_parameter *)buf;
8780 	int rval;
8781 
8782 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
8783 		struct smart_ext_selftest_log *ext_selftest_log;
8784 
8785 		ext_selftest_log = kmem_zalloc(
8786 		    sizeof (struct smart_ext_selftest_log), KM_SLEEP);
8787 
8788 		rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo,
8789 		    ext_selftest_log, 0);
8790 		if (rval == 0) {
8791 			int index, start_index;
8792 			struct smart_ext_selftest_log_entry *entry;
8793 			static const struct smart_ext_selftest_log_entry empty =
8794 			    {0};
8795 			uint16_t block_num;
8796 			int count;
8797 			boolean_t only_one_block = B_FALSE;
8798 
8799 			index = ext_selftest_log->
8800 			    smart_ext_selftest_log_index[0];
8801 			index |= ext_selftest_log->
8802 			    smart_ext_selftest_log_index[1] << 8;
8803 			if (index == 0)
8804 				goto out;
8805 
8806 			--index;	/* Correct for 0 origin */
8807 			start_index = index;	/* remember where we started */
8808 			block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8809 			if (block_num != 0) {
8810 				rval = sata_ext_smart_selftest_read_log(
8811 				    sata_hba_inst, sdinfo, ext_selftest_log,
8812 				    block_num);
8813 				if (rval != 0)
8814 					goto out;
8815 			}
8816 			index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
8817 			entry =
8818 			    &ext_selftest_log->
8819 			    smart_ext_selftest_log_entries[index];
8820 
8821 			for (count = 1;
8822 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
8823 			    ++count) {
8824 				uint8_t status;
8825 				uint8_t code;
8826 				uint8_t sense_key;
8827 				uint8_t add_sense_code;
8828 				uint8_t add_sense_code_qual;
8829 
8830 				/* If this is an unused entry, we are done */
8831 				if (bcmp(entry, &empty, sizeof (empty)) == 0) {
8832 					/* Broken firmware on some disks */
8833 					if (index + 1 ==
8834 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK) {
8835 						--entry;
8836 						--index;
8837 						if (bcmp(entry, &empty,
8838 						    sizeof (empty)) == 0)
8839 							goto out;
8840 					} else
8841 						goto out;
8842 				}
8843 
8844 				if (only_one_block &&
8845 				    start_index == index)
8846 					goto out;
8847 
8848 				lpp->param_code[0] = 0;
8849 				lpp->param_code[1] = count;
8850 				lpp->param_ctrl_flags =
8851 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
8852 				lpp->param_len =
8853 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
8854 
8855 				status = entry->smart_ext_selftest_log_status;
8856 				status >>= 4;
8857 				switch (status) {
8858 				case 0:
8859 				default:
8860 					sense_key = KEY_NO_SENSE;
8861 					add_sense_code =
8862 					    SD_SCSI_ASC_NO_ADD_SENSE;
8863 					add_sense_code_qual = 0;
8864 					break;
8865 				case 1:
8866 					sense_key = KEY_ABORTED_COMMAND;
8867 					add_sense_code =
8868 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8869 					add_sense_code_qual = SCSI_COMPONENT_81;
8870 					break;
8871 				case 2:
8872 					sense_key = KEY_ABORTED_COMMAND;
8873 					add_sense_code =
8874 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8875 					add_sense_code_qual = SCSI_COMPONENT_82;
8876 					break;
8877 				case 3:
8878 					sense_key = KEY_ABORTED_COMMAND;
8879 					add_sense_code =
8880 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8881 					add_sense_code_qual = SCSI_COMPONENT_83;
8882 					break;
8883 				case 4:
8884 					sense_key = KEY_HARDWARE_ERROR;
8885 					add_sense_code =
8886 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8887 					add_sense_code_qual = SCSI_COMPONENT_84;
8888 					break;
8889 				case 5:
8890 					sense_key = KEY_HARDWARE_ERROR;
8891 					add_sense_code =
8892 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8893 					add_sense_code_qual = SCSI_COMPONENT_85;
8894 					break;
8895 				case 6:
8896 					sense_key = KEY_HARDWARE_ERROR;
8897 					add_sense_code =
8898 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8899 					add_sense_code_qual = SCSI_COMPONENT_86;
8900 					break;
8901 				case 7:
8902 					sense_key = KEY_MEDIUM_ERROR;
8903 					add_sense_code =
8904 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8905 					add_sense_code_qual = SCSI_COMPONENT_87;
8906 					break;
8907 				case 8:
8908 					sense_key = KEY_HARDWARE_ERROR;
8909 					add_sense_code =
8910 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
8911 					add_sense_code_qual = SCSI_COMPONENT_88;
8912 					break;
8913 				}
8914 				code = 0;	/* unspecified */
8915 				status |= (code << 4);
8916 				lpp->param_values[0] = status;
8917 				lpp->param_values[1] = 0; /* unspecified */
8918 				lpp->param_values[2] = entry->
8919 				    smart_ext_selftest_log_timestamp[1];
8920 				lpp->param_values[3] = entry->
8921 				    smart_ext_selftest_log_timestamp[0];
8922 				if (status != 0) {
8923 					lpp->param_values[4] = 0;
8924 					lpp->param_values[5] = 0;
8925 					lpp->param_values[6] = entry->
8926 					    smart_ext_selftest_log_failing_lba
8927 					    [5];
8928 					lpp->param_values[7] = entry->
8929 					    smart_ext_selftest_log_failing_lba
8930 					    [4];
8931 					lpp->param_values[8] = entry->
8932 					    smart_ext_selftest_log_failing_lba
8933 					    [3];
8934 					lpp->param_values[9] = entry->
8935 					    smart_ext_selftest_log_failing_lba
8936 					    [2];
8937 					lpp->param_values[10] = entry->
8938 					    smart_ext_selftest_log_failing_lba
8939 					    [1];
8940 					lpp->param_values[11] = entry->
8941 					    smart_ext_selftest_log_failing_lba
8942 					    [0];
8943 				} else {	/* No bad block address */
8944 					lpp->param_values[4] = 0xff;
8945 					lpp->param_values[5] = 0xff;
8946 					lpp->param_values[6] = 0xff;
8947 					lpp->param_values[7] = 0xff;
8948 					lpp->param_values[8] = 0xff;
8949 					lpp->param_values[9] = 0xff;
8950 					lpp->param_values[10] = 0xff;
8951 					lpp->param_values[11] = 0xff;
8952 				}
8953 
8954 				lpp->param_values[12] = sense_key;
8955 				lpp->param_values[13] = add_sense_code;
8956 				lpp->param_values[14] = add_sense_code_qual;
8957 				lpp->param_values[15] = 0; /* undefined */
8958 
8959 				lpp = (struct log_parameter *)
8960 				    (((uint8_t *)lpp) +
8961 				    SCSI_LOG_PARAM_HDR_LEN +
8962 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
8963 
8964 				--index;	/* Back up to previous entry */
8965 				if (index < 0) {
8966 					if (block_num > 0) {
8967 						--block_num;
8968 					} else {
8969 						struct read_log_ext_directory
8970 						    logdir;
8971 
8972 						rval =
8973 						    sata_read_log_ext_directory(
8974 						    sata_hba_inst, sdinfo,
8975 						    &logdir);
8976 						if (rval == -1)
8977 							goto out;
8978 						if ((logdir.read_log_ext_vers
8979 						    [0] == 0) &&
8980 						    (logdir.read_log_ext_vers
8981 						    [1] == 0))
8982 							goto out;
8983 						block_num =
8984 						    logdir.read_log_ext_nblks
8985 						    [EXT_SMART_SELFTEST_LOG_PAGE
8986 						    - 1][0];
8987 						block_num |= logdir.
8988 						    read_log_ext_nblks
8989 						    [EXT_SMART_SELFTEST_LOG_PAGE
8990 						    - 1][1] << 8;
8991 						--block_num;
8992 						only_one_block =
8993 						    (block_num == 0);
8994 					}
8995 					rval = sata_ext_smart_selftest_read_log(
8996 					    sata_hba_inst, sdinfo,
8997 					    ext_selftest_log, block_num);
8998 					if (rval != 0)
8999 						goto out;
9000 
9001 					index =
9002 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK -
9003 					    1;
9004 				}
9005 				index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
9006 				entry = &ext_selftest_log->
9007 				    smart_ext_selftest_log_entries[index];
9008 			}
9009 		}
9010 out:
9011 		kmem_free(ext_selftest_log,
9012 		    sizeof (struct smart_ext_selftest_log));
9013 	} else {
9014 		struct smart_selftest_log *selftest_log;
9015 
9016 		selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log),
9017 		    KM_SLEEP);
9018 
9019 		rval = sata_smart_selftest_log(sata_hba_inst, sdinfo,
9020 		    selftest_log);
9021 
9022 		if (rval == 0) {
9023 			int index;
9024 			int count;
9025 			struct smart_selftest_log_entry *entry;
9026 			static const struct smart_selftest_log_entry empty =
9027 			    { 0 };
9028 
9029 			index = selftest_log->smart_selftest_log_index;
9030 			if (index == 0)
9031 				goto done;
9032 			--index;	/* Correct for 0 origin */
9033 			entry = &selftest_log->
9034 			    smart_selftest_log_entries[index];
9035 			for (count = 1;
9036 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
9037 			    ++count) {
9038 				uint8_t status;
9039 				uint8_t code;
9040 				uint8_t sense_key;
9041 				uint8_t add_sense_code;
9042 				uint8_t add_sense_code_qual;
9043 
9044 				if (bcmp(entry, &empty, sizeof (empty)) == 0)
9045 					goto done;
9046 
9047 				lpp->param_code[0] = 0;
9048 				lpp->param_code[1] = count;
9049 				lpp->param_ctrl_flags =
9050 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
9051 				lpp->param_len =
9052 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
9053 
9054 				status = entry->smart_selftest_log_status;
9055 				status >>= 4;
9056 				switch (status) {
9057 				case 0:
9058 				default:
9059 					sense_key = KEY_NO_SENSE;
9060 					add_sense_code =
9061 					    SD_SCSI_ASC_NO_ADD_SENSE;
9062 					break;
9063 				case 1:
9064 					sense_key = KEY_ABORTED_COMMAND;
9065 					add_sense_code =
9066 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
9067 					add_sense_code_qual = SCSI_COMPONENT_81;
9068 					break;
9069 				case 2:
9070 					sense_key = KEY_ABORTED_COMMAND;
9071 					add_sense_code =
9072 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
9073 					add_sense_code_qual = SCSI_COMPONENT_82;
9074 					break;
9075 				case 3:
9076 					sense_key = KEY_ABORTED_COMMAND;
9077 					add_sense_code =
9078 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
9079 					add_sense_code_qual = SCSI_COMPONENT_83;
9080 					break;
9081 				case 4:
9082 					sense_key = KEY_HARDWARE_ERROR;
9083 					add_sense_code =
9084 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
9085 					add_sense_code_qual = SCSI_COMPONENT_84;
9086 					break;
9087 				case 5:
9088 					sense_key = KEY_HARDWARE_ERROR;
9089 					add_sense_code =
9090 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
9091 					add_sense_code_qual = SCSI_COMPONENT_85;
9092 					break;
9093 				case 6:
9094 					sense_key = KEY_HARDWARE_ERROR;
9095 					add_sense_code =
9096 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
9097 					add_sense_code_qual = SCSI_COMPONENT_86;
9098 					break;
9099 				case 7:
9100 					sense_key = KEY_MEDIUM_ERROR;
9101 					add_sense_code =
9102 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
9103 					add_sense_code_qual = SCSI_COMPONENT_87;
9104 					break;
9105 				case 8:
9106 					sense_key = KEY_HARDWARE_ERROR;
9107 					add_sense_code =
9108 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
9109 					add_sense_code_qual = SCSI_COMPONENT_88;
9110 					break;
9111 				}
9112 				code = 0;	/* unspecified */
9113 				status |= (code << 4);
9114 				lpp->param_values[0] = status;
9115 				lpp->param_values[1] = 0; /* unspecified */
9116 				lpp->param_values[2] = entry->
9117 				    smart_selftest_log_timestamp[1];
9118 				lpp->param_values[3] = entry->
9119 				    smart_selftest_log_timestamp[0];
9120 				if (status != 0) {
9121 					lpp->param_values[4] = 0;
9122 					lpp->param_values[5] = 0;
9123 					lpp->param_values[6] = 0;
9124 					lpp->param_values[7] = 0;
9125 					lpp->param_values[8] = entry->
9126 					    smart_selftest_log_failing_lba[3];
9127 					lpp->param_values[9] = entry->
9128 					    smart_selftest_log_failing_lba[2];
9129 					lpp->param_values[10] = entry->
9130 					    smart_selftest_log_failing_lba[1];
9131 					lpp->param_values[11] = entry->
9132 					    smart_selftest_log_failing_lba[0];
9133 				} else {	/* No block address */
9134 					lpp->param_values[4] = 0xff;
9135 					lpp->param_values[5] = 0xff;
9136 					lpp->param_values[6] = 0xff;
9137 					lpp->param_values[7] = 0xff;
9138 					lpp->param_values[8] = 0xff;
9139 					lpp->param_values[9] = 0xff;
9140 					lpp->param_values[10] = 0xff;
9141 					lpp->param_values[11] = 0xff;
9142 				}
9143 				lpp->param_values[12] = sense_key;
9144 				lpp->param_values[13] = add_sense_code;
9145 				lpp->param_values[14] = add_sense_code_qual;
9146 				lpp->param_values[15] = 0; /* undefined */
9147 
9148 				lpp = (struct log_parameter *)
9149 				    (((uint8_t *)lpp) +
9150 				    SCSI_LOG_PARAM_HDR_LEN +
9151 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
9152 				--index;	/* back up to previous entry */
9153 				if (index < 0) {
9154 					index =
9155 					    NUM_SMART_SELFTEST_LOG_ENTRIES - 1;
9156 				}
9157 				entry = &selftest_log->
9158 				    smart_selftest_log_entries[index];
9159 			}
9160 		}
9161 done:
9162 		kmem_free(selftest_log, sizeof (struct smart_selftest_log));
9163 	}
9164 
9165 	return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) *
9166 	    SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS);
9167 }
9168 
9169 /*
9170  * sata_build_lsense_page_2f() is used to create the
9171  * SCSI LOG SENSE page 0x2f (informational exceptions)
9172  *
9173  * Takes a sata_drive_info t * and the address of a buffer
9174  * in which to create the page information as well as a sata_hba_inst_t *.
9175  *
9176  * Returns the number of bytes valid in the buffer.
9177  *
9178  * Because it invokes function(s) that send synchronously executed command
9179  * to the HBA, it cannot be called in the interrupt context.
9180  */
9181 static	int
9182 sata_build_lsense_page_2f(
9183 	sata_drive_info_t *sdinfo,
9184 	uint8_t *buf,
9185 	sata_hba_inst_t *sata_hba_inst)
9186 {
9187 	struct log_parameter *lpp = (struct log_parameter *)buf;
9188 	int rval;
9189 	uint8_t *smart_data;
9190 	uint8_t temp;
9191 	sata_id_t *sata_id;
9192 #define	SMART_NO_TEMP	0xff
9193 
9194 	lpp->param_code[0] = 0;
9195 	lpp->param_code[1] = 0;
9196 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
9197 
9198 	/* Now get the SMART status w.r.t. threshold exceeded */
9199 	rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo);
9200 	switch (rval) {
9201 	case 1:
9202 		lpp->param_values[0] = SCSI_PREDICTED_FAILURE;
9203 		lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE;
9204 		break;
9205 	case 0:
9206 	case -1:	/* failed to get data */
9207 		lpp->param_values[0] = 0;	/* No failure predicted */
9208 		lpp->param_values[1] = 0;
9209 		break;
9210 #if defined(SATA_DEBUG)
9211 	default:
9212 		cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value");
9213 		/* NOTREACHED */
9214 #endif
9215 	}
9216 
9217 	sata_id = &sdinfo->satadrv_id;
9218 	if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP))
9219 		temp = SMART_NO_TEMP;
9220 	else {
9221 		/* Now get the temperature */
9222 		smart_data = kmem_zalloc(512, KM_SLEEP);
9223 		rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data,
9224 		    SCT_STATUS_LOG_PAGE, 1);
9225 		if (rval == -1)
9226 			temp = SMART_NO_TEMP;
9227 		else {
9228 			temp = smart_data[200];
9229 			if (temp & 0x80) {
9230 				if (temp & 0x7f)
9231 					temp = 0;
9232 				else
9233 					temp = SMART_NO_TEMP;
9234 			}
9235 		}
9236 		kmem_free(smart_data, 512);
9237 	}
9238 
9239 	lpp->param_values[2] = temp;	/* most recent temperature */
9240 	lpp->param_values[3] = 0;	/* required vendor specific byte */
9241 
9242 	lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN;
9243 
9244 
9245 	return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN);
9246 }
9247 
9248 /*
9249  * sata_build_lsense_page_30() is used to create the
9250  * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data).
9251  *
9252  * Takes a sata_drive_info t * and the address of a buffer
9253  * in which to create the page information as well as a sata_hba_inst_t *.
9254  *
9255  * Returns the number of bytes valid in the buffer.
9256  */
9257 static int
9258 sata_build_lsense_page_30(
9259 	sata_drive_info_t *sdinfo,
9260 	uint8_t *buf,
9261 	sata_hba_inst_t *sata_hba_inst)
9262 {
9263 	struct smart_data *smart_data = (struct smart_data *)buf;
9264 	int rval;
9265 
9266 	/* Now do the SMART READ DATA */
9267 	rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data);
9268 	if (rval == -1)
9269 		return (0);
9270 
9271 	return (sizeof (struct smart_data));
9272 }
9273 
9274 /*
9275  * sata_build_lsense_page_0e() is used to create the
9276  * SCSI LOG SENSE page 0e (start-stop cycle counter page)
9277  *
9278  * Date of Manufacture (0x0001)
9279  *	YEAR = "0000"
9280  *	WEEK = "00"
9281  * Accounting Date (0x0002)
9282  *	6 ASCII space character(20h)
9283  * Specified cycle count over device lifetime
9284  *	VALUE - THRESH - the delta between max and min;
9285  * Accumulated start-stop cycles
9286  *	VALUE - WORST - the accumulated cycles;
9287  *
9288  * ID FLAG THRESH VALUE WORST RAW on start/stop counter attribute
9289  *
9290  * Takes a sata_drive_info t * and the address of a buffer
9291  * in which to create the page information as well as a sata_hba_inst_t *.
9292  *
9293  * Returns the number of bytes valid in the buffer.
9294  */
9295 static	int
9296 sata_build_lsense_page_0e(sata_drive_info_t *sdinfo, uint8_t *buf,
9297     sata_pkt_txlate_t *spx)
9298 {
9299 	struct start_stop_cycle_counter_log *log_page;
9300 	int i, rval, index;
9301 	uint8_t smart_data[512], id, value, worst, thresh;
9302 	uint32_t max_count, cycles;
9303 
9304 	/* Now do the SMART READ DATA */
9305 	rval = sata_fetch_smart_data(spx->txlt_sata_hba_inst, sdinfo,
9306 	    (struct smart_data *)smart_data);
9307 	if (rval == -1)
9308 		return (0);
9309 	for (i = 0, id = 0; i < SMART_START_STOP_COUNT_ID * 2; i++) {
9310 		index = (i * 12) + 2;
9311 		id = smart_data[index];
9312 		if (id != SMART_START_STOP_COUNT_ID)
9313 			continue;
9314 		else {
9315 			thresh = smart_data[index + 2];
9316 			value = smart_data[index + 3];
9317 			worst = smart_data[index + 4];
9318 			break;
9319 		}
9320 	}
9321 	if (id != SMART_START_STOP_COUNT_ID)
9322 		return (0);
9323 	max_count = value - thresh;
9324 	cycles = value - worst;
9325 
9326 	log_page = (struct start_stop_cycle_counter_log *)buf;
9327 	bzero(log_page, sizeof (struct start_stop_cycle_counter_log));
9328 	log_page->code = 0x0e;
9329 	log_page->page_len_low = 0x24;
9330 
9331 	log_page->manufactor_date_low = 0x1;
9332 	log_page->param_1.fmt_link = 0x1; /* 01b */
9333 	log_page->param_len_1 = 0x06;
9334 	for (i = 0; i < 4; i++) {
9335 		log_page->year_manu[i] = 0x30;
9336 		if (i < 2)
9337 			log_page->week_manu[i] = 0x30;
9338 	}
9339 
9340 	log_page->account_date_low = 0x02;
9341 	log_page->param_2.fmt_link = 0x01; /* 01b */
9342 	log_page->param_len_2 = 0x06;
9343 	for (i = 0; i < 4; i++) {
9344 		log_page->year_account[i] = 0x20;
9345 		if (i < 2)
9346 			log_page->week_account[i] = 0x20;
9347 	}
9348 
9349 	log_page->lifetime_code_low = 0x03;
9350 	log_page->param_3.fmt_link = 0x03; /* 11b */
9351 	log_page->param_len_3 = 0x04;
9352 	/* VALUE - THRESH - the delta between max and min */
9353 	log_page->cycle_code_low = 0x04;
9354 	log_page->param_4.fmt_link = 0x03; /* 11b */
9355 	log_page->param_len_4 = 0x04;
9356 	/* WORST - THRESH - the distance from 'now' to min */
9357 
9358 	for (i = 0; i < 4; i++) {
9359 		log_page->cycle_lifetime[i] =
9360 		    (max_count >> (8 * (3 - i))) & 0xff;
9361 		log_page->cycle_accumulated[i] =
9362 		    (cycles >> (8 * (3 - i))) & 0xff;
9363 	}
9364 
9365 	return (sizeof (struct start_stop_cycle_counter_log));
9366 }
9367 
9368 /*
9369  * This function was used for build a ATA read verify sector command
9370  */
9371 static void
9372 sata_build_read_verify_cmd(sata_cmd_t *scmd, uint16_t sec, uint64_t lba)
9373 {
9374 	scmd->satacmd_cmd_reg = SATAC_RDVER;
9375 	scmd->satacmd_addr_type = ATA_ADDR_LBA28;
9376 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
9377 
9378 	scmd->satacmd_sec_count_lsb = sec & 0xff;
9379 	scmd->satacmd_lba_low_lsb = lba & 0xff;
9380 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
9381 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
9382 	scmd->satacmd_device_reg = (SATA_ADH_LBA | (lba >> 24) & 0xf);
9383 	scmd->satacmd_features_reg = 0;
9384 	scmd->satacmd_status_reg = 0;
9385 	scmd->satacmd_error_reg = 0;
9386 }
9387 
9388 /*
9389  * This function was used for building an ATA
9390  * command, and only command register need to
9391  * be defined, other register will be zero or na.
9392  */
9393 static void
9394 sata_build_generic_cmd(sata_cmd_t *scmd, uint8_t cmd)
9395 {
9396 	scmd->satacmd_addr_type = 0;
9397 	scmd->satacmd_cmd_reg = cmd;
9398 	scmd->satacmd_device_reg = 0;
9399 	scmd->satacmd_sec_count_lsb = 0;
9400 	scmd->satacmd_lba_low_lsb = 0;
9401 	scmd->satacmd_lba_mid_lsb = 0;
9402 	scmd->satacmd_lba_high_lsb = 0;
9403 	scmd->satacmd_features_reg = 0;
9404 	scmd->satacmd_status_reg = 0;
9405 	scmd->satacmd_error_reg = 0;
9406 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
9407 }
9408 
9409 /*
9410  * This function was used for changing the standby
9411  * timer format from SCSI to ATA.
9412  */
9413 static uint8_t
9414 sata_get_standby_timer(uint8_t *timer)
9415 {
9416 	uint32_t i = 0, count = 0;
9417 	uint8_t ata_count;
9418 
9419 	for (i = 0; i < 4; i++) {
9420 		count = count << 8 | timer[i];
9421 	}
9422 
9423 	if (count == 0)
9424 		return (0);
9425 
9426 	if (count >= 1 && count <= 12000)
9427 		ata_count = (count -1) / 50 + 1;
9428 	else if (count > 12000 && count <= 12600)
9429 		ata_count = 0xfc;
9430 	else if (count > 12601 && count <= 12750)
9431 		ata_count = 0xff;
9432 	else if (count > 12750 && count <= 17999)
9433 		ata_count = 0xf1;
9434 	else if (count > 18000 && count <= 198000)
9435 		ata_count = count / 18000 + 240;
9436 	else
9437 		ata_count = 0xfd;
9438 	return (ata_count);
9439 }
9440 
9441 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */
9442 
9443 /*
9444  * Start command for ATAPI device.
9445  * This function processes scsi_pkt requests.
9446  * Now CD/DVD, tape and ATAPI disk devices are supported.
9447  * Most commands are packet without any translation into Packet Command.
9448  * Some may be trapped and executed as SATA commands (not clear which one).
9449  *
9450  * Returns TRAN_ACCEPT if command is accepted for execution (or completed
9451  * execution).
9452  * Returns other TRAN_XXXX codes if command is not accepted or completed
9453  * (see return values for sata_hba_start()).
9454  *
9455  * Note:
9456  * Inquiry cdb format differs between transport version 2 and 3.
9457  * However, the transport version 3 devices that were checked did not adhere
9458  * to the specification (ignored MSB of the allocation length). Therefore,
9459  * the transport version is not checked, but Inquiry allocation length is
9460  * truncated to 255 bytes if the original allocation length set-up by the
9461  * target driver is greater than 255 bytes.
9462  */
9463 static int
9464 sata_txlt_atapi(sata_pkt_txlate_t *spx)
9465 {
9466 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
9467 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
9468 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9469 	sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx);
9470 	sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba,
9471 	    &spx->txlt_sata_pkt->satapkt_device);
9472 	kmutex_t *cport_mutex = &(SATA_TXLT_CPORT_MUTEX(spx));
9473 	int cdblen;
9474 	int rval, reason;
9475 	int synch;
9476 	union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp;
9477 
9478 	mutex_enter(cport_mutex);
9479 
9480 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason, 0)) !=
9481 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
9482 		mutex_exit(cport_mutex);
9483 		return (rval);
9484 	}
9485 
9486 	/*
9487 	 * ATAPI device executes some ATA commands in addition to those
9488 	 * commands sent via PACKET command. These ATA commands may be
9489 	 * executed by the regular SATA translation functions. None needs
9490 	 * to be captured now.
9491 	 *
9492 	 * Commands sent via PACKET command include:
9493 	 *	MMC command set for ATAPI CD/DVD device
9494 	 *	SSC command set for ATAPI TAPE device
9495 	 *	SBC command set for ATAPI disk device
9496 	 *
9497 	 */
9498 
9499 	/* Check the size of cdb */
9500 
9501 	switch (GETGROUP(cdbp)) {
9502 	case CDB_GROUPID_3:   /* Reserved, per SPC-4 */
9503 		/*
9504 		 * opcodes 0x7e and 0x7f identify variable-length CDBs and
9505 		 * therefore require special handling.  Return failure, for now.
9506 		 */
9507 		mutex_exit(cport_mutex);
9508 		return (TRAN_BADPKT);
9509 
9510 	case CDB_GROUPID_6:   /* Vendor-specific, per SPC-4 */
9511 	case CDB_GROUPID_7:   /* Vendor-specific, per SPC-4 */
9512 		/* obtain length from the scsi_pkt */
9513 		cdblen = scsipkt->pkt_cdblen;
9514 		break;
9515 
9516 	default:
9517 		/* CDB's length is statically known, per SPC-4 */
9518 		cdblen = scsi_cdb_size[GETGROUP(cdbp)];
9519 		break;
9520 	}
9521 
9522 	if (cdblen <= 0 || cdblen > sdinfo->satadrv_atapi_cdb_len) {
9523 		sata_log(NULL, CE_WARN,
9524 		    "sata: invalid ATAPI cdb length %d",
9525 		    cdblen);
9526 		mutex_exit(cport_mutex);
9527 		return (TRAN_BADPKT);
9528 	}
9529 
9530 	SATAATAPITRACE(spx, cdblen);
9531 
9532 	/*
9533 	 * For non-read/write commands we need to
9534 	 * map buffer
9535 	 */
9536 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
9537 	case SCMD_READ:
9538 	case SCMD_READ_G1:
9539 	case SCMD_READ_G5:
9540 	case SCMD_READ_G4:
9541 	case SCMD_WRITE:
9542 	case SCMD_WRITE_G1:
9543 	case SCMD_WRITE_G5:
9544 	case SCMD_WRITE_G4:
9545 		break;
9546 	default:
9547 		if (bp != NULL) {
9548 			if (bp->b_flags & (B_PHYS | B_PAGEIO))
9549 				bp_mapin(bp);
9550 		}
9551 		break;
9552 	}
9553 	/*
9554 	 * scmd->satacmd_flags.sata_data_direction default -
9555 	 * SATA_DIR_NODATA_XFER - is set by
9556 	 * sata_txlt_generic_pkt_info().
9557 	 */
9558 	if (scmd->satacmd_bp) {
9559 		if (scmd->satacmd_bp->b_flags & B_READ) {
9560 			scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9561 		} else {
9562 			scmd->satacmd_flags.sata_data_direction =
9563 			    SATA_DIR_WRITE;
9564 		}
9565 	}
9566 
9567 	/*
9568 	 * Set up ATAPI packet command.
9569 	 */
9570 
9571 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
9572 
9573 	/* Copy cdb into sata_cmd */
9574 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9575 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
9576 	bcopy(cdbp, scmd->satacmd_acdb, cdblen);
9577 
9578 	/* See note in the command header */
9579 	if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) {
9580 		if (scmd->satacmd_acdb[3] != 0)
9581 			scmd->satacmd_acdb[4] = 255;
9582 	}
9583 
9584 #ifdef SATA_DEBUG
9585 	if (sata_debug_flags & SATA_DBG_ATAPI) {
9586 		uint8_t *p = scmd->satacmd_acdb;
9587 		char buf[3 * SATA_ATAPI_MAX_CDB_LEN];
9588 
9589 		(void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN,
9590 		    "%02x %02x %02x %02x %02x %02x %02x %02x "
9591 		    "%2x %02x %02x %02x %02x %02x %02x %02x",
9592 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9593 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9594 		buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0';
9595 		cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf);
9596 	}
9597 #endif
9598 
9599 	/*
9600 	 * Preset request sense data to NO SENSE.
9601 	 * If there is no way to get error information via Request Sense,
9602 	 * the packet request sense data would not have to be modified by HBA,
9603 	 * but it could be returned as is.
9604 	 */
9605 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
9606 	sata_fixed_sense_data_preset(
9607 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
9608 
9609 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
9610 		/* Need callback function */
9611 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion;
9612 		synch = FALSE;
9613 	} else
9614 		synch = TRUE;
9615 
9616 	/* Transfer command to HBA */
9617 	if (sata_hba_start(spx, &rval) != 0) {
9618 		/* Pkt not accepted for execution */
9619 		mutex_exit(cport_mutex);
9620 		return (rval);
9621 	}
9622 	mutex_exit(cport_mutex);
9623 	/*
9624 	 * If execution is non-synchronous,
9625 	 * a callback function will handle potential errors, translate
9626 	 * the response and will do a callback to a target driver.
9627 	 * If it was synchronous, use the same framework callback to check
9628 	 * an execution status.
9629 	 */
9630 	if (synch) {
9631 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
9632 		    "synchronous execution status %x\n",
9633 		    spx->txlt_sata_pkt->satapkt_reason);
9634 		sata_txlt_atapi_completion(spx->txlt_sata_pkt);
9635 	}
9636 	return (TRAN_ACCEPT);
9637 }
9638 
9639 
9640 /*
9641  * ATAPI Packet command completion.
9642  *
9643  * Failure of the command passed via Packet command are considered device
9644  * error. SATA HBA driver would have to retrieve error data (via Request
9645  * Sense command delivered via error retrieval sata packet) and copy it
9646  * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data.
9647  */
9648 static void
9649 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt)
9650 {
9651 	sata_pkt_txlate_t *spx =
9652 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
9653 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
9654 	struct scsi_extended_sense *sense;
9655 	struct buf *bp;
9656 	int rval;
9657 
9658 #ifdef SATA_DEBUG
9659 	uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense;
9660 #endif
9661 
9662 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
9663 	    STATE_SENT_CMD | STATE_GOT_STATUS;
9664 
9665 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
9666 		/* Normal completion */
9667 		if (sata_pkt->satapkt_cmd.satacmd_bp != NULL)
9668 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
9669 		scsipkt->pkt_reason = CMD_CMPLT;
9670 		*scsipkt->pkt_scbp = STATUS_GOOD;
9671 		if (spx->txlt_tmp_buf != NULL) {
9672 			/* Temporary buffer was used */
9673 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9674 			if (bp->b_flags & B_READ) {
9675 				rval = ddi_dma_sync(
9676 				    spx->txlt_buf_dma_handle, 0, 0,
9677 				    DDI_DMA_SYNC_FORCPU);
9678 				ASSERT(rval == DDI_SUCCESS);
9679 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
9680 				    bp->b_bcount);
9681 			}
9682 		}
9683 	} else {
9684 		/*
9685 		 * Something went wrong - analyze return
9686 		 */
9687 		*scsipkt->pkt_scbp = STATUS_CHECK;
9688 		sense = sata_arq_sense(spx);
9689 
9690 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
9691 			/*
9692 			 * pkt_reason should be CMD_CMPLT for DEVICE ERROR.
9693 			 * Under this condition ERR bit is set for ATA command,
9694 			 * and CHK bit set for ATAPI command.
9695 			 *
9696 			 * Please check st_intr & sdintr about how pkt_reason
9697 			 * is used.
9698 			 */
9699 			scsipkt->pkt_reason = CMD_CMPLT;
9700 
9701 			/*
9702 			 * We may not have ARQ data if there was a double
9703 			 * error. But sense data in sata packet was pre-set
9704 			 * with NO SENSE so it is valid even if HBA could
9705 			 * not retrieve a real sense data.
9706 			 * Just copy this sense data into scsi pkt sense area.
9707 			 */
9708 			bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense,
9709 			    SATA_ATAPI_MIN_RQSENSE_LEN);
9710 #ifdef SATA_DEBUG
9711 			if (sata_debug_flags & SATA_DBG_SCSI_IF) {
9712 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
9713 				    "sata_txlt_atapi_completion: %02x\n"
9714 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
9715 				    "          %02x %02x %02x %02x %02x %02x "
9716 				    "          %02x %02x %02x %02x %02x %02x\n",
9717 				    scsipkt->pkt_reason,
9718 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
9719 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
9720 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
9721 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
9722 				    rqsp[16], rqsp[17]);
9723 			}
9724 #endif
9725 		} else {
9726 			switch (sata_pkt->satapkt_reason) {
9727 			case SATA_PKT_PORT_ERROR:
9728 				/*
9729 				 * We have no device data.
9730 				 */
9731 				scsipkt->pkt_reason = CMD_INCOMPLETE;
9732 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
9733 				    STATE_GOT_TARGET | STATE_SENT_CMD |
9734 				    STATE_GOT_STATUS);
9735 				sense->es_key = KEY_HARDWARE_ERROR;
9736 				break;
9737 
9738 			case SATA_PKT_TIMEOUT:
9739 				scsipkt->pkt_reason = CMD_TIMEOUT;
9740 				scsipkt->pkt_statistics |=
9741 				    STAT_TIMEOUT | STAT_DEV_RESET;
9742 				/*
9743 				 * Need to check if HARDWARE_ERROR/
9744 				 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more
9745 				 * appropriate.
9746 				 */
9747 				break;
9748 
9749 			case SATA_PKT_ABORTED:
9750 				scsipkt->pkt_reason = CMD_ABORTED;
9751 				scsipkt->pkt_statistics |= STAT_ABORTED;
9752 				/* Should we set key COMMAND_ABPRTED? */
9753 				break;
9754 
9755 			case SATA_PKT_RESET:
9756 				scsipkt->pkt_reason = CMD_RESET;
9757 				scsipkt->pkt_statistics |= STAT_DEV_RESET;
9758 				/*
9759 				 * May be we should set Unit Attention /
9760 				 * Reset. Perhaps the same should be
9761 				 * returned for disks....
9762 				 */
9763 				sense->es_key = KEY_UNIT_ATTENTION;
9764 				sense->es_add_code = SD_SCSI_ASC_RESET;
9765 				break;
9766 
9767 			default:
9768 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9769 				    "sata_txlt_atapi_completion: "
9770 				    "invalid packet completion reason"));
9771 				scsipkt->pkt_reason = CMD_TRAN_ERR;
9772 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
9773 				    STATE_GOT_TARGET | STATE_SENT_CMD |
9774 				    STATE_GOT_STATUS);
9775 				break;
9776 			}
9777 		}
9778 	}
9779 
9780 	SATAATAPITRACE(spx, 0);
9781 
9782 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
9783 	    scsipkt->pkt_comp != NULL) {
9784 		/* scsi callback required */
9785 		(*scsipkt->pkt_comp)(scsipkt);
9786 	}
9787 }
9788 
9789 /*
9790  * Set up error retrieval sata command for ATAPI Packet Command error data
9791  * recovery.
9792  *
9793  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
9794  * returns SATA_FAILURE otherwise.
9795  */
9796 
9797 static int
9798 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
9799 {
9800 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
9801 	sata_cmd_t *scmd;
9802 	struct buf *bp;
9803 
9804 	/*
9805 	 * Allocate dma-able buffer error data.
9806 	 * Buffer allocation will take care of buffer alignment and other DMA
9807 	 * attributes.
9808 	 */
9809 	bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN);
9810 	if (bp == NULL) {
9811 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
9812 		    "sata_get_err_retrieval_pkt: "
9813 		    "cannot allocate buffer for error data", NULL);
9814 		return (SATA_FAILURE);
9815 	}
9816 	bp_mapin(bp); /* make data buffer accessible */
9817 
9818 	/* Operation modes are up to the caller */
9819 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
9820 
9821 	/* Synchronous mode, no callback - may be changed by the caller */
9822 	spkt->satapkt_comp = NULL;
9823 	spkt->satapkt_time = sata_default_pkt_time;
9824 
9825 	scmd = &spkt->satapkt_cmd;
9826 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
9827 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
9828 
9829 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
9830 
9831 	/*
9832 	 * Set-up acdb. Request Sense CDB (packet command content) is
9833 	 * not in DMA-able buffer. Its handling is HBA-specific (how
9834 	 * it is transfered into packet FIS).
9835 	 */
9836 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
9837 	bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN);
9838 	/* Following zeroing of pad bytes may not be necessary */
9839 	bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN],
9840 	    sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN);
9841 
9842 	/*
9843 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
9844 	 * before accessing it. Handle is in usual place in translate struct.
9845 	 */
9846 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
9847 
9848 	/*
9849 	 * Preset request sense data to NO SENSE.
9850 	 * Here it is redundant, only for a symetry with scsi-originated
9851 	 * packets. It should not be used for anything but debugging.
9852 	 */
9853 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
9854 	sata_fixed_sense_data_preset(
9855 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
9856 
9857 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
9858 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
9859 
9860 	return (SATA_SUCCESS);
9861 }
9862 
9863 /*
9864  * Set-up ATAPI packet command.
9865  * Data transfer direction has to be set-up in sata_cmd structure prior to
9866  * calling this function.
9867  *
9868  * Returns void
9869  */
9870 
9871 static void
9872 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo)
9873 {
9874 	scmd->satacmd_addr_type = 0;		/* N/A */
9875 	scmd->satacmd_sec_count_lsb = 0;	/* no tag */
9876 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
9877 	scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ;
9878 	scmd->satacmd_lba_high_lsb =
9879 	    (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8);
9880 	scmd->satacmd_cmd_reg = SATAC_PACKET;	/* Command */
9881 
9882 	/*
9883 	 * We want all data to be transfered via DMA.
9884 	 * But specify it only if drive supports DMA and DMA mode is
9885 	 * selected - some drives are sensitive about it.
9886 	 * Hopefully it wil work for all drives....
9887 	 */
9888 	if (sdinfo->satadrv_settings & SATA_DEV_DMA)
9889 		scmd->satacmd_features_reg = SATA_ATAPI_F_DMA;
9890 
9891 	/*
9892 	 * Features register requires special care for devices that use
9893 	 * Serial ATA bridge - they need an explicit specification of
9894 	 * the data transfer direction for Packet DMA commands.
9895 	 * Setting this bit is harmless if DMA is not used.
9896 	 *
9897 	 * Many drives do not implement word 80, specifying what ATA/ATAPI
9898 	 * spec they follow.
9899 	 * We are arbitrarily following the latest SerialATA 2.6 spec,
9900 	 * which uses ATA/ATAPI 6 specification for Identify Data, unless
9901 	 * ATA/ATAPI-7 support is explicitly indicated.
9902 	 */
9903 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
9904 	    sdinfo->satadrv_id.ai_majorversion != 0xffff &&
9905 	    (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) {
9906 		/*
9907 		 * Specification of major version is valid and version 7
9908 		 * is supported. It does automatically imply that all
9909 		 * spec features are supported. For now, we assume that
9910 		 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete.
9911 		 */
9912 		if ((sdinfo->satadrv_id.ai_dirdma &
9913 		    SATA_ATAPI_ID_DMADIR_REQ) != 0) {
9914 			if (scmd->satacmd_flags.sata_data_direction ==
9915 			    SATA_DIR_READ)
9916 			scmd->satacmd_features_reg |=
9917 			    SATA_ATAPI_F_DATA_DIR_READ;
9918 		}
9919 	}
9920 }
9921 
9922 
9923 #ifdef SATA_DEBUG
9924 
9925 /* Display 18 bytes of Inquiry data */
9926 static void
9927 sata_show_inqry_data(uint8_t *buf)
9928 {
9929 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
9930 	uint8_t *p;
9931 
9932 	cmn_err(CE_NOTE, "Inquiry data:");
9933 	cmn_err(CE_NOTE, "device type %x", inq->inq_dtype);
9934 	cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb);
9935 	cmn_err(CE_NOTE, "version %x", inq->inq_ansi);
9936 	cmn_err(CE_NOTE, "ATAPI transport version %d",
9937 	    SATA_ATAPI_TRANS_VERSION(inq));
9938 	cmn_err(CE_NOTE, "response data format %d, aenc %d",
9939 	    inq->inq_rdf, inq->inq_aenc);
9940 	cmn_err(CE_NOTE, " additional length %d", inq->inq_len);
9941 	cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs);
9942 	p = (uint8_t *)inq->inq_vid;
9943 	cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x "
9944 	    "%02x %02x %02x %02x",
9945 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
9946 	p = (uint8_t *)inq->inq_vid;
9947 	cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c",
9948 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
9949 
9950 	p = (uint8_t *)inq->inq_pid;
9951 	cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x "
9952 	    "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
9953 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9954 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9955 	p = (uint8_t *)inq->inq_pid;
9956 	cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c "
9957 	    "%c %c %c %c %c %c %c %c",
9958 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
9959 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
9960 
9961 	p = (uint8_t *)inq->inq_revision;
9962 	cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x",
9963 	    p[0], p[1], p[2], p[3]);
9964 	p = (uint8_t *)inq->inq_revision;
9965 	cmn_err(CE_NOTE, "revision: %c %c %c %c",
9966 	    p[0], p[1], p[2], p[3]);
9967 
9968 }
9969 
9970 
9971 static void
9972 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count)
9973 {
9974 	struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt;
9975 
9976 	if (scsi_pkt == NULL)
9977 		return;
9978 	if (count != 0) {
9979 		/* saving cdb */
9980 		bzero(sata_atapi_trace[sata_atapi_trace_index].acdb,
9981 		    SATA_ATAPI_MAX_CDB_LEN);
9982 		bcopy(scsi_pkt->pkt_cdbp,
9983 		    sata_atapi_trace[sata_atapi_trace_index].acdb, count);
9984 	} else {
9985 		bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)->
9986 		    sts_sensedata,
9987 		    sata_atapi_trace[sata_atapi_trace_index].arqs,
9988 		    SATA_ATAPI_MIN_RQSENSE_LEN);
9989 		sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason =
9990 		    scsi_pkt->pkt_reason;
9991 		sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason =
9992 		    spx->txlt_sata_pkt->satapkt_reason;
9993 
9994 		if (++sata_atapi_trace_index >= 64)
9995 			sata_atapi_trace_index = 0;
9996 	}
9997 }
9998 
9999 #endif
10000 
10001 /*
10002  * Fetch inquiry data from ATAPI device
10003  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
10004  *
10005  * Note:
10006  * inqb pointer does not point to a DMA-able buffer. It is a local buffer
10007  * where the caller expects to see the inquiry data.
10008  *
10009  */
10010 
10011 static int
10012 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba,
10013     sata_address_t *saddr, struct scsi_inquiry *inq)
10014 {
10015 	sata_pkt_txlate_t *spx;
10016 	sata_pkt_t *spkt;
10017 	struct buf *bp;
10018 	sata_drive_info_t *sdinfo;
10019 	sata_cmd_t *scmd;
10020 	int rval;
10021 	uint8_t *rqsp;
10022 	dev_info_t *dip = SATA_DIP(sata_hba);
10023 #ifdef SATA_DEBUG
10024 	char msg_buf[MAXPATHLEN];
10025 #endif
10026 	kmutex_t *cport_mutex;
10027 
10028 	ASSERT(sata_hba != NULL);
10029 
10030 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10031 	spx->txlt_sata_hba_inst = sata_hba;
10032 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
10033 	spkt = sata_pkt_alloc(spx, NULL);
10034 	if (spkt == NULL) {
10035 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10036 		return (SATA_FAILURE);
10037 	}
10038 	/* address is needed now */
10039 	spkt->satapkt_device.satadev_addr = *saddr;
10040 
10041 	/* scsi_inquiry size buffer */
10042 	bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry));
10043 	if (bp == NULL) {
10044 		sata_pkt_free(spx);
10045 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10046 		SATA_LOG_D((sata_hba, CE_WARN,
10047 		    "sata_get_atapi_inquiry_data: "
10048 		    "cannot allocate data buffer"));
10049 		return (SATA_FAILURE);
10050 	}
10051 	bp_mapin(bp); /* make data buffer accessible */
10052 
10053 	scmd = &spkt->satapkt_cmd;
10054 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
10055 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
10056 
10057 	/* Use synchronous mode */
10058 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10059 	spkt->satapkt_comp = NULL;
10060 	spkt->satapkt_time = sata_default_pkt_time;
10061 
10062 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
10063 
10064 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
10065 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10066 
10067 	cport_mutex = &(SATA_CPORT_MUTEX(sata_hba, saddr->cport));
10068 	mutex_enter(cport_mutex);
10069 	sdinfo = sata_get_device_info(sata_hba,
10070 	    &spx->txlt_sata_pkt->satapkt_device);
10071 	if (sdinfo == NULL) {
10072 		/* we have to be carefull about the disapearing device */
10073 		mutex_exit(cport_mutex);
10074 		rval = SATA_FAILURE;
10075 		goto cleanup;
10076 	}
10077 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
10078 
10079 	/*
10080 	 * Set-up acdb. This works for atapi transport version 2 and later.
10081 	 */
10082 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
10083 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
10084 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
10085 	scmd->satacmd_acdb[1] = 0x00;
10086 	scmd->satacmd_acdb[2] = 0x00;
10087 	scmd->satacmd_acdb[3] = 0x00;
10088 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
10089 	scmd->satacmd_acdb[5] = 0x00;
10090 
10091 	sata_fixed_sense_data_preset(
10092 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
10093 
10094 	/* Transfer command to HBA */
10095 	if (sata_hba_start(spx, &rval) != 0) {
10096 		/* Pkt not accepted for execution */
10097 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
10098 		    "sata_get_atapi_inquiry_data: "
10099 		    "Packet not accepted for execution - ret: %02x", rval);
10100 		mutex_exit(cport_mutex);
10101 		rval = SATA_FAILURE;
10102 		goto cleanup;
10103 	}
10104 	mutex_exit(cport_mutex);
10105 
10106 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10107 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
10108 		    "sata_get_atapi_inquiry_data: "
10109 		    "Packet completed successfully - ret: %02x", rval);
10110 		if (spx->txlt_buf_dma_handle != NULL) {
10111 			/*
10112 			 * Sync buffer. Handle is in usual place in translate
10113 			 * struct.
10114 			 */
10115 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10116 			    DDI_DMA_SYNC_FORCPU);
10117 			ASSERT(rval == DDI_SUCCESS);
10118 		}
10119 
10120 		if (sata_check_for_dma_error(dip, spx)) {
10121 			ddi_fm_service_impact(dip, DDI_SERVICE_UNAFFECTED);
10122 			rval = SATA_FAILURE;
10123 		} else {
10124 			/*
10125 			 * Normal completion - copy data into caller's buffer
10126 			 */
10127 			bcopy(bp->b_un.b_addr, (uint8_t *)inq,
10128 			    sizeof (struct scsi_inquiry));
10129 #ifdef SATA_DEBUG
10130 			if (sata_debug_flags & SATA_DBG_ATAPI) {
10131 				sata_show_inqry_data((uint8_t *)inq);
10132 			}
10133 #endif
10134 			rval = SATA_SUCCESS;
10135 		}
10136 	} else {
10137 		/*
10138 		 * Something went wrong - analyze return - check rqsense data
10139 		 */
10140 		rval = SATA_FAILURE;
10141 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
10142 			/*
10143 			 * ARQ data hopefull show something other than NO SENSE
10144 			 */
10145 			rqsp = scmd->satacmd_rqsense;
10146 #ifdef SATA_DEBUG
10147 			if (sata_debug_flags & SATA_DBG_ATAPI) {
10148 				msg_buf[0] = '\0';
10149 				(void) snprintf(msg_buf, MAXPATHLEN,
10150 				    "ATAPI packet completion reason: %02x\n"
10151 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x\n"
10152 				    "          %02x %02x %02x %02x %02x %02x\n"
10153 				    "          %02x %02x %02x %02x %02x %02x",
10154 				    spkt->satapkt_reason,
10155 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
10156 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
10157 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
10158 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
10159 				    rqsp[16], rqsp[17]);
10160 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
10161 				    "%s", msg_buf);
10162 			}
10163 #endif
10164 		} else {
10165 			switch (spkt->satapkt_reason) {
10166 			case SATA_PKT_PORT_ERROR:
10167 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10168 				    "sata_get_atapi_inquiry_data: "
10169 				    "packet reason: port error", NULL);
10170 				break;
10171 
10172 			case SATA_PKT_TIMEOUT:
10173 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10174 				    "sata_get_atapi_inquiry_data: "
10175 				    "packet reason: timeout", NULL);
10176 				break;
10177 
10178 			case SATA_PKT_ABORTED:
10179 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10180 				    "sata_get_atapi_inquiry_data: "
10181 				    "packet reason: aborted", NULL);
10182 				break;
10183 
10184 			case SATA_PKT_RESET:
10185 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10186 				    "sata_get_atapi_inquiry_data: "
10187 				    "packet reason: reset\n", NULL);
10188 				break;
10189 			default:
10190 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
10191 				    "sata_get_atapi_inquiry_data: "
10192 				    "invalid packet reason: %02x\n",
10193 				    spkt->satapkt_reason);
10194 				break;
10195 			}
10196 		}
10197 	}
10198 cleanup:
10199 	sata_free_local_buffer(spx);
10200 	sata_pkt_free(spx);
10201 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
10202 	return (rval);
10203 }
10204 
10205 
10206 
10207 
10208 
10209 #if 0
10210 #ifdef SATA_DEBUG
10211 
10212 /*
10213  * Test ATAPI packet command.
10214  * Single threaded test: send packet command in synch mode, process completion
10215  *
10216  */
10217 static void
10218 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport)
10219 {
10220 	sata_pkt_txlate_t *spx;
10221 	sata_pkt_t *spkt;
10222 	struct buf *bp;
10223 	sata_device_t sata_device;
10224 	sata_drive_info_t *sdinfo;
10225 	sata_cmd_t *scmd;
10226 	int rval;
10227 	uint8_t *rqsp;
10228 
10229 	ASSERT(sata_hba_inst != NULL);
10230 	sata_device.satadev_addr.cport = cport;
10231 	sata_device.satadev_addr.pmport = 0;
10232 	sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
10233 	sata_device.satadev_rev = SATA_DEVICE_REV;
10234 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10235 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
10236 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10237 	if (sdinfo == NULL) {
10238 		sata_log(sata_hba_inst, CE_WARN,
10239 		    "sata_test_atapi_packet_command: "
10240 		    "no device info for cport %d",
10241 		    sata_device.satadev_addr.cport);
10242 		return;
10243 	}
10244 
10245 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10246 	spx->txlt_sata_hba_inst = sata_hba_inst;
10247 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
10248 	spkt = sata_pkt_alloc(spx, NULL);
10249 	if (spkt == NULL) {
10250 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10251 		return;
10252 	}
10253 	/* address is needed now */
10254 	spkt->satapkt_device.satadev_addr = sata_device.satadev_addr;
10255 
10256 	/* 1024k buffer */
10257 	bp = sata_alloc_local_buffer(spx, 1024);
10258 	if (bp == NULL) {
10259 		sata_pkt_free(spx);
10260 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10261 		sata_log(sata_hba_inst, CE_WARN,
10262 		    "sata_test_atapi_packet_command: "
10263 		    "cannot allocate data buffer");
10264 		return;
10265 	}
10266 	bp_mapin(bp); /* make data buffer accessible */
10267 
10268 	scmd = &spkt->satapkt_cmd;
10269 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
10270 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
10271 
10272 	/* Use synchronous mode */
10273 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10274 
10275 	/* Synchronous mode, no callback - may be changed by the caller */
10276 	spkt->satapkt_comp = NULL;
10277 	spkt->satapkt_time = sata_default_pkt_time;
10278 
10279 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
10280 
10281 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
10282 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10283 
10284 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
10285 
10286 	/* Set-up acdb. */
10287 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
10288 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
10289 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
10290 	scmd->satacmd_acdb[1] = 0x00;
10291 	scmd->satacmd_acdb[2] = 0x00;
10292 	scmd->satacmd_acdb[3] = 0x00;
10293 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
10294 	scmd->satacmd_acdb[5] = 0x00;
10295 
10296 	sata_fixed_sense_data_preset(
10297 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
10298 
10299 	/* Transfer command to HBA */
10300 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10301 	if (sata_hba_start(spx, &rval) != 0) {
10302 		/* Pkt not accepted for execution */
10303 		sata_log(sata_hba_inst, CE_WARN,
10304 		    "sata_test_atapi_packet_command: "
10305 		    "Packet not accepted for execution - ret: %02x", rval);
10306 		mutex_exit(
10307 		    &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10308 		goto cleanup;
10309 	}
10310 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10311 
10312 	if (spx->txlt_buf_dma_handle != NULL) {
10313 		/*
10314 		 * Sync buffer. Handle is in usual place in translate struct.
10315 		 */
10316 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10317 		    DDI_DMA_SYNC_FORCPU);
10318 		ASSERT(rval == DDI_SUCCESS);
10319 	}
10320 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10321 		sata_log(sata_hba_inst, CE_WARN,
10322 		    "sata_test_atapi_packet_command: "
10323 		    "Packet completed successfully");
10324 		/*
10325 		 * Normal completion - show inquiry data
10326 		 */
10327 		sata_show_inqry_data((uint8_t *)bp->b_un.b_addr);
10328 	} else {
10329 		/*
10330 		 * Something went wrong - analyze return - check rqsense data
10331 		 */
10332 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
10333 			/*
10334 			 * ARQ data hopefull show something other than NO SENSE
10335 			 */
10336 			rqsp = scmd->satacmd_rqsense;
10337 			sata_log(spx->txlt_sata_hba_inst, CE_WARN,
10338 			    "ATAPI packet completion reason: %02x\n"
10339 			    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
10340 			    "          %02x %02x %02x %02x %02x %02x "
10341 			    "          %02x %02x %02x %02x %02x %02x\n",
10342 			    spkt->satapkt_reason,
10343 			    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
10344 			    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
10345 			    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
10346 			    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
10347 			    rqsp[16], rqsp[17]);
10348 		} else {
10349 			switch (spkt->satapkt_reason) {
10350 			case SATA_PKT_PORT_ERROR:
10351 				sata_log(sata_hba_inst, CE_WARN,
10352 				    "sata_test_atapi_packet_command: "
10353 				    "packet reason: port error\n");
10354 				break;
10355 
10356 			case SATA_PKT_TIMEOUT:
10357 				sata_log(sata_hba_inst, CE_WARN,
10358 				    "sata_test_atapi_packet_command: "
10359 				    "packet reason: timeout\n");
10360 				break;
10361 
10362 			case SATA_PKT_ABORTED:
10363 				sata_log(sata_hba_inst, CE_WARN,
10364 				    "sata_test_atapi_packet_command: "
10365 				    "packet reason: aborted\n");
10366 				break;
10367 
10368 			case SATA_PKT_RESET:
10369 				sata_log(sata_hba_inst, CE_WARN,
10370 				    "sata_test_atapi_packet_command: "
10371 				    "packet reason: reset\n");
10372 				break;
10373 			default:
10374 				sata_log(sata_hba_inst, CE_WARN,
10375 				    "sata_test_atapi_packet_command: "
10376 				    "invalid packet reason: %02x\n",
10377 				    spkt->satapkt_reason);
10378 				break;
10379 			}
10380 		}
10381 	}
10382 cleanup:
10383 	sata_free_local_buffer(spx);
10384 	sata_pkt_free(spx);
10385 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
10386 }
10387 
10388 #endif /* SATA_DEBUG */
10389 #endif /* 1 */
10390 
10391 
10392 /* ************************** LOCAL HELPER FUNCTIONS *********************** */
10393 
10394 /*
10395  * Validate sata_tran info
10396  * SATA_FAILURE returns if structure is inconsistent or structure revision
10397  * does not match one used by the framework.
10398  *
10399  * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains
10400  * required function pointers.
10401  * Returns SATA_FAILURE otherwise.
10402  */
10403 static int
10404 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran)
10405 {
10406 	/*
10407 	 * SATA_TRAN_HBA_REV is the current (highest) revision number
10408 	 * of the SATA interface.
10409 	 */
10410 	if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) {
10411 		sata_log(NULL, CE_WARN,
10412 		    "sata: invalid sata_hba_tran version %d for driver %s",
10413 		    sata_tran->sata_tran_hba_rev, ddi_driver_name(dip));
10414 		return (SATA_FAILURE);
10415 	}
10416 
10417 	if (dip != sata_tran->sata_tran_hba_dip) {
10418 		SATA_LOG_D((NULL, CE_WARN,
10419 		    "sata: inconsistent sata_tran_hba_dip "
10420 		    "%p / %p", sata_tran->sata_tran_hba_dip, dip));
10421 		return (SATA_FAILURE);
10422 	}
10423 
10424 	if (sata_tran->sata_tran_probe_port == NULL ||
10425 	    sata_tran->sata_tran_start == NULL ||
10426 	    sata_tran->sata_tran_abort == NULL ||
10427 	    sata_tran->sata_tran_reset_dport == NULL ||
10428 	    sata_tran->sata_tran_hotplug_ops == NULL ||
10429 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL ||
10430 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate ==
10431 	    NULL) {
10432 		SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing "
10433 		    "required functions"));
10434 	}
10435 	return (SATA_SUCCESS);
10436 }
10437 
10438 /*
10439  * Remove HBA instance from sata_hba_list.
10440  */
10441 static void
10442 sata_remove_hba_instance(dev_info_t *dip)
10443 {
10444 	sata_hba_inst_t	*sata_hba_inst;
10445 
10446 	mutex_enter(&sata_mutex);
10447 	for (sata_hba_inst = sata_hba_list;
10448 	    sata_hba_inst != (struct sata_hba_inst *)NULL;
10449 	    sata_hba_inst = sata_hba_inst->satahba_next) {
10450 		if (sata_hba_inst->satahba_dip == dip)
10451 			break;
10452 	}
10453 
10454 	if (sata_hba_inst == (struct sata_hba_inst *)NULL) {
10455 #ifdef SATA_DEBUG
10456 		cmn_err(CE_WARN, "sata_remove_hba_instance: "
10457 		    "unknown HBA instance\n");
10458 #endif
10459 		ASSERT(FALSE);
10460 	}
10461 	if (sata_hba_inst == sata_hba_list) {
10462 		sata_hba_list = sata_hba_inst->satahba_next;
10463 		if (sata_hba_list) {
10464 			sata_hba_list->satahba_prev =
10465 			    (struct sata_hba_inst *)NULL;
10466 		}
10467 		if (sata_hba_inst == sata_hba_list_tail) {
10468 			sata_hba_list_tail = NULL;
10469 		}
10470 	} else if (sata_hba_inst == sata_hba_list_tail) {
10471 		sata_hba_list_tail = sata_hba_inst->satahba_prev;
10472 		if (sata_hba_list_tail) {
10473 			sata_hba_list_tail->satahba_next =
10474 			    (struct sata_hba_inst *)NULL;
10475 		}
10476 	} else {
10477 		sata_hba_inst->satahba_prev->satahba_next =
10478 		    sata_hba_inst->satahba_next;
10479 		sata_hba_inst->satahba_next->satahba_prev =
10480 		    sata_hba_inst->satahba_prev;
10481 	}
10482 	mutex_exit(&sata_mutex);
10483 }
10484 
10485 /*
10486  * Probe all SATA ports of the specified HBA instance.
10487  * The assumption is that there are no target and attachment point minor nodes
10488  * created by the boot subsystems, so we do not need to prune device tree.
10489  *
10490  * This function is called only from sata_hba_attach(). It does not have to
10491  * be protected by controller mutex, because the hba_attached flag is not set
10492  * yet and no one would be touching this HBA instance other than this thread.
10493  * Determines if port is active and what type of the device is attached
10494  * (if any). Allocates necessary structures for each port.
10495  *
10496  * An AP (Attachement Point) node is created for each SATA device port even
10497  * when there is no device attached.
10498  */
10499 
10500 static void
10501 sata_probe_ports(sata_hba_inst_t *sata_hba_inst)
10502 {
10503 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
10504 	int			ncport;
10505 	sata_cport_info_t	*cportinfo;
10506 	sata_drive_info_t	*drive;
10507 	sata_device_t		sata_device;
10508 	int			rval;
10509 	dev_t			minor_number;
10510 	char			name[16];
10511 	clock_t			start_time, cur_time;
10512 
10513 	/*
10514 	 * Probe controller ports first, to find port status and
10515 	 * any port multiplier attached.
10516 	 */
10517 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
10518 		/* allocate cport structure */
10519 		cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP);
10520 		ASSERT(cportinfo != NULL);
10521 		mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL);
10522 
10523 		mutex_enter(&cportinfo->cport_mutex);
10524 
10525 		cportinfo->cport_addr.cport = ncport;
10526 		cportinfo->cport_addr.pmport = 0;
10527 		cportinfo->cport_addr.qual = SATA_ADDR_CPORT;
10528 		cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
10529 		cportinfo->cport_state |= SATA_STATE_PROBING;
10530 		SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo;
10531 
10532 		/*
10533 		 * Regardless if a port is usable or not, create
10534 		 * an attachment point
10535 		 */
10536 		mutex_exit(&cportinfo->cport_mutex);
10537 		minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip),
10538 		    ncport, 0, SATA_ADDR_CPORT);
10539 		(void) sprintf(name, "%d", ncport);
10540 		if (ddi_create_minor_node(dip, name, S_IFCHR,
10541 		    minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) !=
10542 		    DDI_SUCCESS) {
10543 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
10544 			    "cannot create SATA attachment point for port %d",
10545 			    ncport);
10546 		}
10547 
10548 		/* Probe port */
10549 		start_time = ddi_get_lbolt();
10550 	reprobe_cport:
10551 		sata_device.satadev_addr.cport = ncport;
10552 		sata_device.satadev_addr.pmport = 0;
10553 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
10554 		sata_device.satadev_rev = SATA_DEVICE_REV;
10555 
10556 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10557 		    (dip, &sata_device);
10558 
10559 		mutex_enter(&cportinfo->cport_mutex);
10560 		cportinfo->cport_scr = sata_device.satadev_scr;
10561 		if (rval != SATA_SUCCESS) {
10562 			/* Something went wrong? Fail the port */
10563 			cportinfo->cport_state = SATA_PSTATE_FAILED;
10564 			mutex_exit(&cportinfo->cport_mutex);
10565 			continue;
10566 		}
10567 		cportinfo->cport_state &= ~SATA_STATE_PROBING;
10568 		cportinfo->cport_state |= SATA_STATE_PROBED;
10569 		cportinfo->cport_dev_type = sata_device.satadev_type;
10570 
10571 		cportinfo->cport_state |= SATA_STATE_READY;
10572 		if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) {
10573 			mutex_exit(&cportinfo->cport_mutex);
10574 			continue;
10575 		}
10576 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
10577 			/*
10578 			 * There is some device attached.
10579 			 * Allocate device info structure
10580 			 */
10581 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) {
10582 				mutex_exit(&cportinfo->cport_mutex);
10583 				SATA_CPORTINFO_DRV_INFO(cportinfo) =
10584 				    kmem_zalloc(sizeof (sata_drive_info_t),
10585 				    KM_SLEEP);
10586 				mutex_enter(&cportinfo->cport_mutex);
10587 			}
10588 			drive = SATA_CPORTINFO_DRV_INFO(cportinfo);
10589 			drive->satadrv_addr = cportinfo->cport_addr;
10590 			drive->satadrv_addr.qual = SATA_ADDR_DCPORT;
10591 			drive->satadrv_type = cportinfo->cport_dev_type;
10592 			drive->satadrv_state = SATA_STATE_UNKNOWN;
10593 
10594 			mutex_exit(&cportinfo->cport_mutex);
10595 			if (sata_add_device(dip, sata_hba_inst, &sata_device) !=
10596 			    SATA_SUCCESS) {
10597 				/*
10598 				 * Plugged device was not correctly identified.
10599 				 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT
10600 				 */
10601 				cur_time = ddi_get_lbolt();
10602 				if ((cur_time - start_time) <
10603 				    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
10604 					/* sleep for a while */
10605 					delay(drv_usectohz(
10606 					    SATA_DEV_RETRY_DLY));
10607 					goto reprobe_cport;
10608 				}
10609 			}
10610 		} else { /* SATA_DTYPE_PMULT */
10611 			mutex_exit(&cportinfo->cport_mutex);
10612 
10613 			/* Allocate sata_pmult_info and sata_pmport_info */
10614 			if (sata_alloc_pmult(sata_hba_inst, &sata_device) !=
10615 			    SATA_SUCCESS)
10616 				continue;
10617 
10618 			/* Log the information of the port multiplier */
10619 			sata_show_pmult_info(sata_hba_inst, &sata_device);
10620 
10621 			/* Probe its pmports */
10622 			sata_probe_pmports(sata_hba_inst, ncport);
10623 		}
10624 	}
10625 }
10626 
10627 /*
10628  * Probe all device ports behind a port multiplier.
10629  *
10630  * PMult-related structure should be allocated before by sata_alloc_pmult().
10631  *
10632  * NOTE1: Only called from sata_probe_ports()
10633  * NOTE2: No mutex should be hold.
10634  */
10635 static void
10636 sata_probe_pmports(sata_hba_inst_t *sata_hba_inst, uint8_t ncport)
10637 {
10638 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
10639 	sata_pmult_info_t	*pmultinfo = NULL;
10640 	sata_pmport_info_t	*pmportinfo = NULL;
10641 	sata_drive_info_t	*drive = NULL;
10642 	sata_device_t		sata_device;
10643 
10644 	clock_t			start_time, cur_time;
10645 	int			npmport;
10646 	int			rval;
10647 
10648 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, ncport);
10649 
10650 	/* Probe Port Multiplier ports */
10651 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; npmport++) {
10652 		pmportinfo = pmultinfo->pmult_dev_port[npmport];
10653 		start_time = ddi_get_lbolt();
10654 reprobe_pmport:
10655 		sata_device.satadev_addr.cport = ncport;
10656 		sata_device.satadev_addr.pmport = npmport;
10657 		sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
10658 		sata_device.satadev_rev = SATA_DEVICE_REV;
10659 
10660 		/* Let HBA driver probe it. */
10661 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10662 		    (dip, &sata_device);
10663 		mutex_enter(&pmportinfo->pmport_mutex);
10664 
10665 		pmportinfo->pmport_scr = sata_device.satadev_scr;
10666 
10667 		if (rval != SATA_SUCCESS) {
10668 			pmportinfo->pmport_state =
10669 			    SATA_PSTATE_FAILED;
10670 			mutex_exit(&pmportinfo->pmport_mutex);
10671 			continue;
10672 		}
10673 		pmportinfo->pmport_state &= ~SATA_STATE_PROBING;
10674 		pmportinfo->pmport_state |= SATA_STATE_PROBED;
10675 		pmportinfo->pmport_dev_type = sata_device.satadev_type;
10676 
10677 		pmportinfo->pmport_state |= SATA_STATE_READY;
10678 		if (pmportinfo->pmport_dev_type ==
10679 		    SATA_DTYPE_NONE) {
10680 			SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
10681 			    "no device found at port %d:%d", ncport, npmport);
10682 			mutex_exit(&pmportinfo->pmport_mutex);
10683 			continue;
10684 		}
10685 		/* Port multipliers cannot be chained */
10686 		ASSERT(pmportinfo->pmport_dev_type != SATA_DTYPE_PMULT);
10687 		/*
10688 		 * There is something attached to Port
10689 		 * Multiplier device port
10690 		 * Allocate device info structure
10691 		 */
10692 		if (pmportinfo->pmport_sata_drive == NULL) {
10693 			mutex_exit(&pmportinfo->pmport_mutex);
10694 			pmportinfo->pmport_sata_drive =
10695 			    kmem_zalloc(sizeof (sata_drive_info_t), KM_SLEEP);
10696 			mutex_enter(&pmportinfo->pmport_mutex);
10697 		}
10698 		drive = pmportinfo->pmport_sata_drive;
10699 		drive->satadrv_addr.cport = pmportinfo->pmport_addr.cport;
10700 		drive->satadrv_addr.pmport = npmport;
10701 		drive->satadrv_addr.qual = SATA_ADDR_DPMPORT;
10702 		drive->satadrv_type = pmportinfo-> pmport_dev_type;
10703 		drive->satadrv_state = SATA_STATE_UNKNOWN;
10704 
10705 		mutex_exit(&pmportinfo->pmport_mutex);
10706 		rval = sata_add_device(dip, sata_hba_inst, &sata_device);
10707 
10708 		if (rval != SATA_SUCCESS) {
10709 			/*
10710 			 * Plugged device was not correctly identified.
10711 			 * Retry, within the SATA_DEV_IDENTIFY_TIMEOUT
10712 			 */
10713 			cur_time = ddi_get_lbolt();
10714 			if ((cur_time - start_time) < drv_usectohz(
10715 			    SATA_DEV_IDENTIFY_TIMEOUT)) {
10716 				/* sleep for a while */
10717 				delay(drv_usectohz(SATA_DEV_RETRY_DLY));
10718 				goto reprobe_pmport;
10719 			}
10720 		}
10721 	}
10722 }
10723 
10724 /*
10725  * Add SATA device for specified HBA instance & port (SCSI target
10726  * device nodes).
10727  * This function is called (indirectly) only from sata_hba_attach().
10728  * A target node is created when there is a supported type device attached,
10729  * but may be removed if it cannot be put online.
10730  *
10731  * This function cannot be called from an interrupt context.
10732  *
10733  * Create target nodes for disk, CD/DVD, Tape and ATAPI disk devices
10734  *
10735  * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when
10736  * device identification failed - adding a device could be retried.
10737  *
10738  */
10739 static int
10740 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst,
10741     sata_device_t *sata_device)
10742 {
10743 	sata_cport_info_t	*cportinfo;
10744 	sata_pmult_info_t	*pminfo;
10745 	sata_pmport_info_t	*pmportinfo;
10746 	dev_info_t		*cdip;		/* child dip */
10747 	sata_address_t		*saddr = &sata_device->satadev_addr;
10748 	uint8_t			cport, pmport;
10749 	int			rval;
10750 
10751 	cport = saddr->cport;
10752 	pmport = saddr->pmport;
10753 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
10754 	ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE);
10755 
10756 	/*
10757 	 * Some device is attached to a controller port.
10758 	 * We rely on controllers distinquishing between no-device,
10759 	 * attached port multiplier and other kind of attached device.
10760 	 * We need to get Identify Device data and determine
10761 	 * positively the dev type before trying to attach
10762 	 * the target driver.
10763 	 */
10764 	sata_device->satadev_rev = SATA_DEVICE_REV;
10765 	switch (saddr->qual) {
10766 	case SATA_ADDR_CPORT:
10767 		/*
10768 		 * Add a non-port-multiplier device at controller port.
10769 		 */
10770 		saddr->qual = SATA_ADDR_DCPORT;
10771 
10772 		rval = sata_probe_device(sata_hba_inst, sata_device);
10773 		if (rval != SATA_SUCCESS ||
10774 		    sata_device->satadev_type == SATA_DTYPE_UNKNOWN)
10775 			return (SATA_FAILURE);
10776 
10777 		mutex_enter(&cportinfo->cport_mutex);
10778 		sata_show_drive_info(sata_hba_inst,
10779 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
10780 
10781 		if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) {
10782 			/*
10783 			 * Could not determine device type or
10784 			 * a device is not supported.
10785 			 * Degrade this device to unknown.
10786 			 */
10787 			cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
10788 			mutex_exit(&cportinfo->cport_mutex);
10789 			return (SATA_SUCCESS);
10790 		}
10791 		cportinfo->cport_dev_type = sata_device->satadev_type;
10792 		cportinfo->cport_tgtnode_clean = B_TRUE;
10793 		mutex_exit(&cportinfo->cport_mutex);
10794 
10795 		/*
10796 		 * Initialize device to the desired state. Even if it
10797 		 * fails, the device will still attach but syslog
10798 		 * will show the warning.
10799 		 */
10800 		if (sata_initialize_device(sata_hba_inst,
10801 		    SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) {
10802 			/* Retry */
10803 			rval = sata_initialize_device(sata_hba_inst,
10804 			    SATA_CPORTINFO_DRV_INFO(cportinfo));
10805 
10806 			if (rval == SATA_RETRY)
10807 				sata_log(sata_hba_inst, CE_WARN,
10808 				    "SATA device at port %d - "
10809 				    "default device features could not be set."
10810 				    " Device may not operate as expected.",
10811 				    cport);
10812 		}
10813 
10814 		cdip = sata_create_target_node(pdip, sata_hba_inst, saddr);
10815 		if (cdip == NULL) {
10816 			/*
10817 			 * Attaching target node failed.
10818 			 * We retain sata_drive_info structure...
10819 			 */
10820 			return (SATA_SUCCESS);
10821 		}
10822 
10823 		mutex_enter(&cportinfo->cport_mutex);
10824 		(SATA_CPORTINFO_DRV_INFO(cportinfo))->
10825 		    satadrv_state = SATA_STATE_READY;
10826 		mutex_exit(&cportinfo->cport_mutex);
10827 
10828 		break;
10829 
10830 	case SATA_ADDR_PMPORT:
10831 		saddr->qual = SATA_ADDR_DPMPORT;
10832 
10833 		mutex_enter(&cportinfo->cport_mutex);
10834 		/* It must be a Port Multiplier at the controller port */
10835 		ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT);
10836 
10837 		pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
10838 		pmportinfo = pminfo->pmult_dev_port[saddr->pmport];
10839 		mutex_exit(&cportinfo->cport_mutex);
10840 
10841 		rval = sata_probe_device(sata_hba_inst, sata_device);
10842 		if (rval != SATA_SUCCESS ||
10843 		    sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
10844 			return (SATA_FAILURE);
10845 		}
10846 
10847 		mutex_enter(&pmportinfo->pmport_mutex);
10848 		sata_show_drive_info(sata_hba_inst,
10849 		    SATA_PMPORTINFO_DRV_INFO(pmportinfo));
10850 
10851 		if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) {
10852 			/*
10853 			 * Could not determine device type.
10854 			 * Degrade this device to unknown.
10855 			 */
10856 			pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
10857 			mutex_exit(&pmportinfo->pmport_mutex);
10858 			return (SATA_SUCCESS);
10859 		}
10860 		pmportinfo->pmport_dev_type = sata_device->satadev_type;
10861 		pmportinfo->pmport_tgtnode_clean = B_TRUE;
10862 		mutex_exit(&pmportinfo->pmport_mutex);
10863 
10864 		/*
10865 		 * Initialize device to the desired state.
10866 		 * Even if it fails, the device will still
10867 		 * attach but syslog will show the warning.
10868 		 */
10869 		if (sata_initialize_device(sata_hba_inst,
10870 		    pmportinfo->pmport_sata_drive) != SATA_SUCCESS) {
10871 			/* Retry */
10872 			rval = sata_initialize_device(sata_hba_inst,
10873 			    pmportinfo->pmport_sata_drive);
10874 
10875 			if (rval == SATA_RETRY)
10876 				sata_log(sata_hba_inst, CE_WARN,
10877 				    "SATA device at port %d:%d - "
10878 				    "default device features could not be set."
10879 				    " Device may not operate as expected.",
10880 				    cport, pmport);
10881 		}
10882 
10883 		cdip = sata_create_target_node(pdip, sata_hba_inst, saddr);
10884 		if (cdip == NULL) {
10885 			/*
10886 			 * Attaching target node failed.
10887 			 * We retain sata_drive_info structure...
10888 			 */
10889 			return (SATA_SUCCESS);
10890 		}
10891 		mutex_enter(&pmportinfo->pmport_mutex);
10892 		pmportinfo->pmport_sata_drive->satadrv_state |=
10893 		    SATA_STATE_READY;
10894 		mutex_exit(&pmportinfo->pmport_mutex);
10895 
10896 		break;
10897 
10898 	default:
10899 		return (SATA_FAILURE);
10900 	}
10901 
10902 	return (SATA_SUCCESS);
10903 }
10904 
10905 /*
10906  * Clean up target node at specific address.
10907  *
10908  * NOTE: No Mutex should be hold.
10909  */
10910 static int
10911 sata_offline_device(sata_hba_inst_t *sata_hba_inst,
10912     sata_device_t *sata_device, sata_drive_info_t *sdinfo)
10913 {
10914 	uint8_t cport, pmport, qual;
10915 	dev_info_t *tdip;
10916 
10917 	cport = sata_device->satadev_addr.cport;
10918 	pmport = sata_device->satadev_addr.pmport;
10919 	qual = sata_device->satadev_addr.qual;
10920 
10921 	if (qual == SATA_ADDR_DCPORT) {
10922 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10923 		    "sata_hba_ioctl: disconnect device at port %d", cport));
10924 	} else {
10925 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10926 		    "sata_hba_ioctl: disconnect device at port %d:%d",
10927 		    cport, pmport));
10928 	}
10929 
10930 	/* We are addressing attached device, not a port */
10931 	sata_device->satadev_addr.qual =
10932 	    sdinfo->satadrv_addr.qual;
10933 	tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
10934 	    &sata_device->satadev_addr);
10935 	if (tdip != NULL && ndi_devi_offline(tdip,
10936 	    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
10937 		/*
10938 		 * Problem :
10939 		 * The target node remained attached.
10940 		 * This happens when the device file was open
10941 		 * or a node was waiting for resources.
10942 		 * Cannot do anything about it.
10943 		 */
10944 		if (qual == SATA_ADDR_DCPORT) {
10945 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10946 			    "sata_hba_ioctl: disconnect: could "
10947 			    "not unconfigure device before "
10948 			    "disconnecting the SATA port %d",
10949 			    cport));
10950 		} else {
10951 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10952 			    "sata_hba_ioctl: disconnect: could "
10953 			    "not unconfigure device before "
10954 			    "disconnecting the SATA port %d:%d",
10955 			    cport, pmport));
10956 		}
10957 		/*
10958 		 * Set DEVICE REMOVED state in the target
10959 		 * node. It will prevent access to the device
10960 		 * even when a new device is attached, until
10961 		 * the old target node is released, removed and
10962 		 * recreated for a new  device.
10963 		 */
10964 		sata_set_device_removed(tdip);
10965 
10966 		/*
10967 		 * Instruct event daemon to try the target
10968 		 * node cleanup later.
10969 		 */
10970 		sata_set_target_node_cleanup(
10971 		    sata_hba_inst, &sata_device->satadev_addr);
10972 	}
10973 
10974 
10975 	return (SATA_SUCCESS);
10976 }
10977 
10978 
10979 /*
10980  * Create scsi target node for attached device, create node properties and
10981  * attach the node.
10982  * The node could be removed if the device onlining fails.
10983  *
10984  * A dev_info_t pointer is returned if operation is successful, NULL is
10985  * returned otherwise.
10986  */
10987 
10988 static dev_info_t *
10989 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst,
10990     sata_address_t *sata_addr)
10991 {
10992 	dev_info_t *cdip = NULL;
10993 	int rval;
10994 	char *nname = NULL;
10995 	char **compatible = NULL;
10996 	int ncompatible;
10997 	struct scsi_inquiry inq;
10998 	sata_device_t sata_device;
10999 	sata_drive_info_t *sdinfo;
11000 	int target;
11001 	int i;
11002 
11003 	sata_device.satadev_rev = SATA_DEVICE_REV;
11004 	sata_device.satadev_addr = *sata_addr;
11005 
11006 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport)));
11007 
11008 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
11009 
11010 	target = SATA_TO_SCSI_TARGET(sata_addr->cport,
11011 	    sata_addr->pmport, sata_addr->qual);
11012 
11013 	if (sdinfo == NULL) {
11014 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
11015 		    sata_addr->cport)));
11016 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11017 		    "sata_create_target_node: no sdinfo for target %x",
11018 		    target));
11019 		return (NULL);
11020 	}
11021 
11022 	/*
11023 	 * create or get scsi inquiry data, expected by
11024 	 * scsi_hba_nodename_compatible_get()
11025 	 * SATA hard disks get Identify Data translated into Inguiry Data.
11026 	 * ATAPI devices respond directly to Inquiry request.
11027 	 */
11028 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
11029 		sata_identdev_to_inquiry(sata_hba_inst, sdinfo,
11030 		    (uint8_t *)&inq);
11031 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
11032 		    sata_addr->cport)));
11033 	} else { /* Assume supported ATAPI device */
11034 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
11035 		    sata_addr->cport)));
11036 		if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr,
11037 		    &inq) == SATA_FAILURE)
11038 			return (NULL);
11039 		/*
11040 		 * Save supported ATAPI transport version
11041 		 */
11042 		sdinfo->satadrv_atapi_trans_ver =
11043 		    SATA_ATAPI_TRANS_VERSION(&inq);
11044 	}
11045 
11046 	/* determine the node name and compatible */
11047 	scsi_hba_nodename_compatible_get(&inq, NULL,
11048 	    inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
11049 
11050 #ifdef SATA_DEBUG
11051 	if (sata_debug_flags & SATA_DBG_NODES) {
11052 		if (nname == NULL) {
11053 			cmn_err(CE_NOTE, "sata_create_target_node: "
11054 			    "cannot determine nodename for target %d\n",
11055 			    target);
11056 		} else {
11057 			cmn_err(CE_WARN, "sata_create_target_node: "
11058 			    "target %d nodename: %s\n", target, nname);
11059 		}
11060 		if (compatible == NULL) {
11061 			cmn_err(CE_WARN,
11062 			    "sata_create_target_node: no compatible name\n");
11063 		} else {
11064 			for (i = 0; i < ncompatible; i++) {
11065 				cmn_err(CE_WARN, "sata_create_target_node: "
11066 				    "compatible name: %s\n", compatible[i]);
11067 			}
11068 		}
11069 	}
11070 #endif
11071 
11072 	/* if nodename can't be determined, log error and exit */
11073 	if (nname == NULL) {
11074 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11075 		    "sata_create_target_node: cannot determine nodename "
11076 		    "for target %d\n", target));
11077 		scsi_hba_nodename_compatible_free(nname, compatible);
11078 		return (NULL);
11079 	}
11080 	/*
11081 	 * Create scsi target node
11082 	 */
11083 	ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip);
11084 	rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
11085 	    "device-type", "scsi");
11086 
11087 	if (rval != DDI_PROP_SUCCESS) {
11088 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
11089 		    "updating device_type prop failed %d", rval));
11090 		goto fail;
11091 	}
11092 
11093 	/*
11094 	 * Create target node properties: target & lun
11095 	 */
11096 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target);
11097 	if (rval != DDI_PROP_SUCCESS) {
11098 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
11099 		    "updating target prop failed %d", rval));
11100 		goto fail;
11101 	}
11102 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0);
11103 	if (rval != DDI_PROP_SUCCESS) {
11104 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
11105 		    "updating target prop failed %d", rval));
11106 		goto fail;
11107 	}
11108 
11109 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
11110 		/*
11111 		 * Add "variant" property
11112 		 */
11113 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
11114 		    "variant", "atapi");
11115 		if (rval != DDI_PROP_SUCCESS) {
11116 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11117 			    "sata_create_target_node: variant atapi "
11118 			    "property could not be created: %d", rval));
11119 			goto fail;
11120 		}
11121 	}
11122 	/* decorate the node with compatible */
11123 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible",
11124 	    compatible, ncompatible) != DDI_PROP_SUCCESS) {
11125 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11126 		    "sata_create_target_node: FAIL compatible props cdip 0x%p",
11127 		    (void *)cdip));
11128 		goto fail;
11129 	}
11130 
11131 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
11132 		/*
11133 		 * Add "sata-phy" property
11134 		 */
11135 		if (ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "sata-phy",
11136 		    (int)sata_addr->cport) != DDI_PROP_SUCCESS) {
11137 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11138 			    "sata_create_target_node: failed to create "
11139 			    "\"sata-phy\" property: port %d",
11140 			    sata_addr->cport));
11141 		}
11142 	}
11143 
11144 
11145 	/*
11146 	 * Now, try to attach the driver. If probing of the device fails,
11147 	 * the target node may be removed
11148 	 */
11149 	rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH);
11150 
11151 	scsi_hba_nodename_compatible_free(nname, compatible);
11152 
11153 	if (rval == NDI_SUCCESS)
11154 		return (cdip);
11155 
11156 	/* target node was removed - are we sure? */
11157 	return (NULL);
11158 
11159 fail:
11160 	scsi_hba_nodename_compatible_free(nname, compatible);
11161 	ddi_prop_remove_all(cdip);
11162 	rval = ndi_devi_free(cdip);
11163 	if (rval != NDI_SUCCESS) {
11164 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
11165 		    "node removal failed %d", rval));
11166 	}
11167 	sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: "
11168 	    "cannot create target node for SATA device at port %d",
11169 	    sata_addr->cport);
11170 	return (NULL);
11171 }
11172 
11173 /*
11174  * Remove a target node.
11175  */
11176 static void
11177 sata_remove_target_node(sata_hba_inst_t *sata_hba_inst,
11178     sata_address_t *sata_addr)
11179 {
11180 	dev_info_t *tdip;
11181 	uint8_t cport = sata_addr->cport;
11182 	uint8_t pmport = sata_addr->pmport;
11183 	uint8_t qual = sata_addr->qual;
11184 
11185 	/* Note the sata daemon uses the address of the port/pmport */
11186 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
11187 
11188 	/* Remove target node */
11189 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), cport, pmport);
11190 	if (tdip != NULL) {
11191 		/*
11192 		 * Target node exists.  Unconfigure device
11193 		 * then remove the target node (one ndi
11194 		 * operation).
11195 		 */
11196 		if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
11197 			/*
11198 			 * PROBLEM - no device, but target node remained. This
11199 			 * happens when the file was open or node was waiting
11200 			 * for resources.
11201 			 */
11202 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11203 			    "sata_remove_target_node: "
11204 			    "Failed to remove target node for "
11205 			    "detached SATA device."));
11206 			/*
11207 			 * Set target node state to DEVI_DEVICE_REMOVED. But
11208 			 * re-check first that the node still exists.
11209 			 */
11210 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
11211 			    cport, pmport);
11212 			if (tdip != NULL) {
11213 				sata_set_device_removed(tdip);
11214 				/*
11215 				 * Instruct event daemon to retry the cleanup
11216 				 * later.
11217 				 */
11218 				sata_set_target_node_cleanup(sata_hba_inst,
11219 				    sata_addr);
11220 			}
11221 		}
11222 
11223 		if (qual == SATA_ADDR_CPORT)
11224 			sata_log(sata_hba_inst, CE_WARN,
11225 			    "SATA device detached at port %d", cport);
11226 		else
11227 			sata_log(sata_hba_inst, CE_WARN,
11228 			    "SATA device detached at port %d:%d",
11229 			    cport, pmport);
11230 	}
11231 #ifdef SATA_DEBUG
11232 	else {
11233 		if (qual == SATA_ADDR_CPORT)
11234 			sata_log(sata_hba_inst, CE_WARN,
11235 			    "target node not found at port %d", cport);
11236 		else
11237 			sata_log(sata_hba_inst, CE_WARN,
11238 			    "target node not found at port %d:%d",
11239 			    cport, pmport);
11240 	}
11241 #endif
11242 }
11243 
11244 
11245 /*
11246  * Re-probe sata port, check for a device and attach info
11247  * structures when necessary. Identify Device data is fetched, if possible.
11248  * Assumption: sata address is already validated.
11249  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
11250  * the presence of a device and its type.
11251  *
11252  * flag arg specifies that the function should try multiple times to identify
11253  * device type and to initialize it, or it should return immediately on failure.
11254  * SATA_DEV_IDENTIFY_RETRY - retry
11255  * SATA_DEV_IDENTIFY_NORETRY - no retry
11256  *
11257  * SATA_FAILURE is returned if one of the operations failed.
11258  *
11259  * This function cannot be called in interrupt context - it may sleep.
11260  *
11261  * Note: Port multiplier is supported.
11262  */
11263 static int
11264 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11265     int flag)
11266 {
11267 	sata_cport_info_t *cportinfo;
11268 	sata_pmult_info_t *pmultinfo;
11269 	sata_drive_info_t *sdinfo, *osdinfo;
11270 	boolean_t init_device = B_FALSE;
11271 	int prev_device_type = SATA_DTYPE_NONE;
11272 	int prev_device_settings = 0;
11273 	int prev_device_state = 0;
11274 	clock_t start_time;
11275 	int retry = B_FALSE;
11276 	uint8_t cport = sata_device->satadev_addr.cport;
11277 	int rval_probe, rval_init;
11278 
11279 	/*
11280 	 * If target is pmport, sata_reprobe_pmport() will handle it.
11281 	 */
11282 	if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT ||
11283 	    sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT)
11284 		return (sata_reprobe_pmport(sata_hba_inst, sata_device, flag));
11285 
11286 	/* We only care about host sata cport for now */
11287 	cportinfo = SATA_CPORT_INFO(sata_hba_inst,
11288 	    sata_device->satadev_addr.cport);
11289 
11290 	/*
11291 	 * If a port multiplier was previously attached (we have no idea it
11292 	 * still there or not), sata_reprobe_pmult() will handle it.
11293 	 */
11294 	if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT)
11295 		return (sata_reprobe_pmult(sata_hba_inst, sata_device, flag));
11296 
11297 	/* Store sata_drive_info when a non-pmult device was attached. */
11298 	osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11299 	if (osdinfo != NULL) {
11300 		/*
11301 		 * We are re-probing port with a previously attached device.
11302 		 * Save previous device type and settings.
11303 		 */
11304 		prev_device_type = cportinfo->cport_dev_type;
11305 		prev_device_settings = osdinfo->satadrv_settings;
11306 		prev_device_state = osdinfo->satadrv_state;
11307 	}
11308 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
11309 		start_time = ddi_get_lbolt();
11310 		retry = B_TRUE;
11311 	}
11312 retry_probe:
11313 
11314 	/* probe port */
11315 	mutex_enter(&cportinfo->cport_mutex);
11316 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11317 	cportinfo->cport_state |= SATA_STATE_PROBING;
11318 	mutex_exit(&cportinfo->cport_mutex);
11319 
11320 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11321 	    (SATA_DIP(sata_hba_inst), sata_device);
11322 
11323 	mutex_enter(&cportinfo->cport_mutex);
11324 	if (rval_probe != SATA_SUCCESS) {
11325 		cportinfo->cport_state = SATA_PSTATE_FAILED;
11326 		mutex_exit(&cportinfo->cport_mutex);
11327 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: "
11328 		    "SATA port %d probing failed",
11329 		    cportinfo->cport_addr.cport));
11330 		return (SATA_FAILURE);
11331 	}
11332 
11333 	/*
11334 	 * update sata port state and set device type
11335 	 */
11336 	sata_update_port_info(sata_hba_inst, sata_device);
11337 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
11338 
11339 	/*
11340 	 * Sanity check - Port is active? Is the link active?
11341 	 * Is there any device attached?
11342 	 */
11343 	if ((cportinfo->cport_state &
11344 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11345 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11346 	    SATA_PORT_DEVLINK_UP) {
11347 		/*
11348 		 * Port in non-usable state or no link active/no device.
11349 		 * Free info structure if necessary (direct attached drive
11350 		 * only, for now!
11351 		 */
11352 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11353 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11354 		/* Add here differentiation for device attached or not */
11355 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11356 		mutex_exit(&cportinfo->cport_mutex);
11357 		if (sdinfo != NULL)
11358 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11359 		return (SATA_SUCCESS);
11360 	}
11361 
11362 	cportinfo->cport_state |= SATA_STATE_READY;
11363 	cportinfo->cport_state |= SATA_STATE_PROBED;
11364 
11365 	cportinfo->cport_dev_type = sata_device->satadev_type;
11366 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11367 
11368 	/*
11369 	 * If we are re-probing the port, there may be
11370 	 * sata_drive_info structure attached
11371 	 */
11372 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
11373 
11374 		/*
11375 		 * There is no device, so remove device info structure,
11376 		 * if necessary.
11377 		 */
11378 		/* Device change: Drive -> None */
11379 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11380 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11381 		if (sdinfo != NULL) {
11382 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11383 			sata_log(sata_hba_inst, CE_WARN,
11384 			    "SATA device detached "
11385 			    "from port %d", cportinfo->cport_addr.cport);
11386 		}
11387 		mutex_exit(&cportinfo->cport_mutex);
11388 		return (SATA_SUCCESS);
11389 
11390 	}
11391 
11392 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
11393 
11394 		/* Device (may) change: Drive -> Drive */
11395 		if (sdinfo == NULL) {
11396 			/*
11397 			 * There is some device attached, but there is
11398 			 * no sata_drive_info structure - allocate one
11399 			 */
11400 			mutex_exit(&cportinfo->cport_mutex);
11401 			sdinfo = kmem_zalloc(
11402 			    sizeof (sata_drive_info_t), KM_SLEEP);
11403 			mutex_enter(&cportinfo->cport_mutex);
11404 			/*
11405 			 * Recheck, that the port state did not change when we
11406 			 * released mutex.
11407 			 */
11408 			if (cportinfo->cport_state & SATA_STATE_READY) {
11409 				SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo;
11410 				sdinfo->satadrv_addr = cportinfo->cport_addr;
11411 				sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT;
11412 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11413 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
11414 			} else {
11415 				/*
11416 				 * Port is not in ready state, we
11417 				 * cannot attach a device.
11418 				 */
11419 				mutex_exit(&cportinfo->cport_mutex);
11420 				kmem_free(sdinfo, sizeof (sata_drive_info_t));
11421 				return (SATA_SUCCESS);
11422 			}
11423 			/*
11424 			 * Since we are adding device, presumably new one,
11425 			 * indicate that it  should be initalized,
11426 			 * as well as some internal framework states).
11427 			 */
11428 			init_device = B_TRUE;
11429 		}
11430 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11431 		sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
11432 	} else {
11433 		/* Device change: Drive -> PMult */
11434 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11435 		if (sdinfo != NULL) {
11436 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11437 			sata_log(sata_hba_inst, CE_WARN,
11438 			    "SATA device detached "
11439 			    "from port %d", cportinfo->cport_addr.cport);
11440 		}
11441 
11442 		sata_log(sata_hba_inst, CE_WARN,
11443 		    "SATA port multiplier detected at port %d",
11444 		    cportinfo->cport_addr.cport);
11445 
11446 		mutex_exit(&cportinfo->cport_mutex);
11447 		if (sata_alloc_pmult(sata_hba_inst, sata_device) !=
11448 		    SATA_SUCCESS)
11449 			return (SATA_FAILURE);
11450 		sata_show_pmult_info(sata_hba_inst, sata_device);
11451 		mutex_enter(&cportinfo->cport_mutex);
11452 
11453 		/*
11454 		 * Mark all the port multiplier port behind the port
11455 		 * multiplier behind with link events, so that the sata daemon
11456 		 * will update their status.
11457 		 */
11458 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
11459 		pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET;
11460 		mutex_exit(&cportinfo->cport_mutex);
11461 		return (SATA_SUCCESS);
11462 	}
11463 	mutex_exit(&cportinfo->cport_mutex);
11464 
11465 	/*
11466 	 * Figure out what kind of device we are really
11467 	 * dealing with. Failure of identifying device does not fail this
11468 	 * function.
11469 	 */
11470 	rval_probe = sata_probe_device(sata_hba_inst, sata_device);
11471 	rval_init = SATA_FAILURE;
11472 	mutex_enter(&cportinfo->cport_mutex);
11473 	if (rval_probe == SATA_SUCCESS) {
11474 		/*
11475 		 * If we are dealing with the same type of a device as before,
11476 		 * restore its settings flags.
11477 		 */
11478 		if (osdinfo != NULL &&
11479 		    sata_device->satadev_type == prev_device_type)
11480 			sdinfo->satadrv_settings = prev_device_settings;
11481 
11482 		mutex_exit(&cportinfo->cport_mutex);
11483 		rval_init = SATA_SUCCESS;
11484 		/* Set initial device features, if necessary */
11485 		if (init_device == B_TRUE) {
11486 			rval_init = sata_initialize_device(sata_hba_inst,
11487 			    sdinfo);
11488 		}
11489 		if (rval_init == SATA_SUCCESS)
11490 			return (rval_init);
11491 		/* else we will retry if retry was asked for */
11492 
11493 	} else {
11494 		/*
11495 		 * If there was some device info before we probe the device,
11496 		 * restore previous device setting, so we can retry from scratch
11497 		 * later. Providing, of course, that device has not disapear
11498 		 * during probing process.
11499 		 */
11500 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11501 			if (osdinfo != NULL) {
11502 				cportinfo->cport_dev_type = prev_device_type;
11503 				sdinfo->satadrv_type = prev_device_type;
11504 				sdinfo->satadrv_state = prev_device_state;
11505 			}
11506 		} else {
11507 			/* device is gone */
11508 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11509 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11510 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11511 			mutex_exit(&cportinfo->cport_mutex);
11512 			return (SATA_SUCCESS);
11513 		}
11514 		mutex_exit(&cportinfo->cport_mutex);
11515 	}
11516 
11517 	if (retry) {
11518 		clock_t cur_time = ddi_get_lbolt();
11519 		/*
11520 		 * A device was not successfully identified or initialized.
11521 		 * Track retry time for device identification.
11522 		 */
11523 		if ((cur_time - start_time) <
11524 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11525 			/* sleep for a while */
11526 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11527 			goto retry_probe;
11528 		}
11529 		/* else no more retries */
11530 		mutex_enter(&cportinfo->cport_mutex);
11531 		if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
11532 			if (rval_init == SATA_RETRY) {
11533 				/*
11534 				 * Setting drive features have failed, but
11535 				 * because the drive is still accessible,
11536 				 * keep it and emit a warning message.
11537 				 */
11538 				sata_log(sata_hba_inst, CE_WARN,
11539 				    "SATA device at port %d - desired "
11540 				    "drive features could not be set. "
11541 				    "Device may not operate as expected.",
11542 				    cportinfo->cport_addr.cport);
11543 			} else {
11544 				SATA_CPORTINFO_DRV_INFO(cportinfo)->
11545 				    satadrv_state = SATA_DSTATE_FAILED;
11546 			}
11547 		}
11548 		mutex_exit(&cportinfo->cport_mutex);
11549 	}
11550 	return (SATA_SUCCESS);
11551 }
11552 
11553 /*
11554  * Reprobe a controller port that connected to a port multiplier.
11555  *
11556  * NOTE: No Mutex should be hold.
11557  */
11558 static int
11559 sata_reprobe_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11560     int flag)
11561 {
11562 	_NOTE(ARGUNUSED(flag))
11563 	sata_cport_info_t *cportinfo;
11564 	sata_pmult_info_t *pmultinfo;
11565 	uint8_t cport = sata_device->satadev_addr.cport;
11566 	int rval_probe;
11567 
11568 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11569 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
11570 
11571 	/* probe port */
11572 	mutex_enter(&cportinfo->cport_mutex);
11573 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11574 	cportinfo->cport_state |= SATA_STATE_PROBING;
11575 	mutex_exit(&cportinfo->cport_mutex);
11576 
11577 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11578 	    (SATA_DIP(sata_hba_inst), sata_device);
11579 
11580 	mutex_enter(&cportinfo->cport_mutex);
11581 	if (rval_probe != SATA_SUCCESS) {
11582 		cportinfo->cport_state = SATA_PSTATE_FAILED;
11583 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmult: "
11584 		    "SATA port %d probing failed", cport));
11585 		sata_log(sata_hba_inst, CE_WARN,
11586 		    "SATA port multiplier detached at port %d", cport);
11587 		mutex_exit(&cportinfo->cport_mutex);
11588 		sata_free_pmult(sata_hba_inst, sata_device);
11589 		return (SATA_FAILURE);
11590 	}
11591 
11592 	/*
11593 	 * update sata port state and set device type
11594 	 */
11595 	sata_update_port_info(sata_hba_inst, sata_device);
11596 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
11597 	cportinfo->cport_state |= SATA_STATE_PROBED;
11598 
11599 	/*
11600 	 * Sanity check - Port is active? Is the link active?
11601 	 * Is there any device attached?
11602 	 */
11603 	if ((cportinfo->cport_state &
11604 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11605 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11606 	    SATA_PORT_DEVLINK_UP ||
11607 	    (sata_device->satadev_type == SATA_DTYPE_NONE)) {
11608 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11609 		mutex_exit(&cportinfo->cport_mutex);
11610 		sata_free_pmult(sata_hba_inst, sata_device);
11611 		sata_log(sata_hba_inst, CE_WARN,
11612 		    "SATA port multiplier detached at port %d", cport);
11613 		return (SATA_SUCCESS);
11614 	}
11615 
11616 	/*
11617 	 * Device changed: PMult -> Non-PMult
11618 	 *
11619 	 * This situation is uncommon, most possibly being caused by errors
11620 	 * after which the port multiplier is not correct initialized and
11621 	 * recognized. In that case the new device will be marked as unknown
11622 	 * and will not be automatically probed in this routine. Instead
11623 	 * system administrator could manually restart it via cfgadm(8).
11624 	 */
11625 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
11626 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11627 		mutex_exit(&cportinfo->cport_mutex);
11628 		sata_free_pmult(sata_hba_inst, sata_device);
11629 		sata_log(sata_hba_inst, CE_WARN,
11630 		    "SATA port multiplier detached at port %d", cport);
11631 		return (SATA_FAILURE);
11632 	}
11633 
11634 	/*
11635 	 * Now we know it is a port multiplier. However, if this is not the
11636 	 * previously attached port multiplier - they may have different
11637 	 * pmport numbers - we need to re-allocate data structures for every
11638 	 * pmport and drive.
11639 	 *
11640 	 * Port multipliers of the same model have identical values in these
11641 	 * registers, so it is still necessary to update the information of
11642 	 * all drives attached to the previous port multiplier afterwards.
11643 	 */
11644 	/* Device changed: PMult -> another PMult */
11645 	mutex_exit(&cportinfo->cport_mutex);
11646 	sata_free_pmult(sata_hba_inst, sata_device);
11647 	if (sata_alloc_pmult(sata_hba_inst, sata_device) != SATA_SUCCESS)
11648 		return (SATA_FAILURE);
11649 	mutex_enter(&cportinfo->cport_mutex);
11650 
11651 	SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
11652 	    "SATA port multiplier [changed] at port %d", cport);
11653 	sata_log(sata_hba_inst, CE_WARN,
11654 	    "SATA port multiplier detected at port %d", cport);
11655 
11656 	/*
11657 	 * Mark all the port multiplier port behind the port
11658 	 * multiplier behind with link events, so that the sata daemon
11659 	 * will update their status.
11660 	 */
11661 	pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET;
11662 	mutex_exit(&cportinfo->cport_mutex);
11663 
11664 	return (SATA_SUCCESS);
11665 }
11666 
11667 /*
11668  * Re-probe a port multiplier port, check for a device and attach info
11669  * structures when necessary. Identify Device data is fetched, if possible.
11670  * Assumption: sata address is already validated as port multiplier port.
11671  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
11672  * the presence of a device and its type.
11673  *
11674  * flag arg specifies that the function should try multiple times to identify
11675  * device type and to initialize it, or it should return immediately on failure.
11676  * SATA_DEV_IDENTIFY_RETRY - retry
11677  * SATA_DEV_IDENTIFY_NORETRY - no retry
11678  *
11679  * SATA_FAILURE is returned if one of the operations failed.
11680  *
11681  * This function cannot be called in interrupt context - it may sleep.
11682  *
11683  * NOTE: Should be only called by sata_probe_port() in case target port is a
11684  *       port multiplier port.
11685  * NOTE: No Mutex should be hold.
11686  */
11687 static int
11688 sata_reprobe_pmport(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
11689     int flag)
11690 {
11691 	sata_cport_info_t *cportinfo = NULL;
11692 	sata_pmport_info_t *pmportinfo = NULL;
11693 	sata_drive_info_t *sdinfo, *osdinfo;
11694 	sata_device_t sdevice;
11695 	boolean_t init_device = B_FALSE;
11696 	int prev_device_type = SATA_DTYPE_NONE;
11697 	int prev_device_settings = 0;
11698 	int prev_device_state = 0;
11699 	clock_t start_time;
11700 	uint8_t cport = sata_device->satadev_addr.cport;
11701 	uint8_t pmport = sata_device->satadev_addr.pmport;
11702 	int rval;
11703 
11704 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11705 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11706 	osdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11707 
11708 	if (osdinfo != NULL) {
11709 		/*
11710 		 * We are re-probing port with a previously attached device.
11711 		 * Save previous device type and settings.
11712 		 */
11713 		prev_device_type = pmportinfo->pmport_dev_type;
11714 		prev_device_settings = osdinfo->satadrv_settings;
11715 		prev_device_state = osdinfo->satadrv_state;
11716 	}
11717 
11718 	start_time = ddi_get_lbolt();
11719 
11720 	/* check parent status */
11721 	mutex_enter(&cportinfo->cport_mutex);
11722 	if ((cportinfo->cport_state &
11723 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11724 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11725 	    SATA_PORT_DEVLINK_UP) {
11726 		mutex_exit(&cportinfo->cport_mutex);
11727 		return (SATA_FAILURE);
11728 	}
11729 	mutex_exit(&cportinfo->cport_mutex);
11730 
11731 retry_probe_pmport:
11732 
11733 	/* probe port */
11734 	mutex_enter(&pmportinfo->pmport_mutex);
11735 	pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
11736 	pmportinfo->pmport_state |= SATA_STATE_PROBING;
11737 	mutex_exit(&pmportinfo->pmport_mutex);
11738 
11739 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11740 	    (SATA_DIP(sata_hba_inst), sata_device);
11741 
11742 	/* might need retry because we cannot touch registers. */
11743 	if (rval == SATA_FAILURE) {
11744 		mutex_enter(&pmportinfo->pmport_mutex);
11745 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
11746 		mutex_exit(&pmportinfo->pmport_mutex);
11747 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: "
11748 		    "SATA port %d:%d probing failed",
11749 		    cport, pmport));
11750 		return (SATA_FAILURE);
11751 	} else if (rval == SATA_RETRY) {
11752 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: "
11753 		    "SATA port %d:%d probing failed, retrying...",
11754 		    cport, pmport));
11755 		clock_t cur_time = ddi_get_lbolt();
11756 		/*
11757 		 * A device was not successfully identified or initialized.
11758 		 * Track retry time for device identification.
11759 		 */
11760 		if ((cur_time - start_time) <
11761 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11762 			/* sleep for a while */
11763 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11764 			goto retry_probe_pmport;
11765 		} else {
11766 			mutex_enter(&pmportinfo->pmport_mutex);
11767 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL)
11768 				SATA_PMPORTINFO_DRV_INFO(pmportinfo)->
11769 				    satadrv_state = SATA_DSTATE_FAILED;
11770 			mutex_exit(&pmportinfo->pmport_mutex);
11771 			return (SATA_SUCCESS);
11772 		}
11773 	}
11774 
11775 	/*
11776 	 * Sanity check - Controller port is active? Is the link active?
11777 	 * Is it still a port multiplier?
11778 	 */
11779 	if ((cportinfo->cport_state &
11780 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11781 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11782 	    SATA_PORT_DEVLINK_UP ||
11783 	    (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) {
11784 		/*
11785 		 * Port in non-usable state or no link active/no
11786 		 * device. Free info structure.
11787 		 */
11788 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
11789 
11790 		sdevice.satadev_addr.cport = cport;
11791 		sdevice.satadev_addr.pmport = pmport;
11792 		sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
11793 		mutex_exit(&cportinfo->cport_mutex);
11794 
11795 		sata_free_pmult(sata_hba_inst, &sdevice);
11796 		return (SATA_FAILURE);
11797 	}
11798 
11799 	/* SATA_SUCCESS NOW */
11800 	/*
11801 	 * update sata port state and set device type
11802 	 */
11803 	mutex_enter(&pmportinfo->pmport_mutex);
11804 	sata_update_pmport_info(sata_hba_inst, sata_device);
11805 	pmportinfo->pmport_state &= ~SATA_STATE_PROBING;
11806 
11807 	/*
11808 	 * Sanity check - Port is active? Is the link active?
11809 	 * Is there any device attached?
11810 	 */
11811 	if ((pmportinfo->pmport_state &
11812 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
11813 	    (pmportinfo->pmport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
11814 	    SATA_PORT_DEVLINK_UP) {
11815 		/*
11816 		 * Port in non-usable state or no link active/no device.
11817 		 * Free info structure if necessary (direct attached drive
11818 		 * only, for now!
11819 		 */
11820 		sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11821 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11822 		/* Add here differentiation for device attached or not */
11823 		pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11824 		mutex_exit(&pmportinfo->pmport_mutex);
11825 		if (sdinfo != NULL)
11826 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11827 		return (SATA_SUCCESS);
11828 	}
11829 
11830 	pmportinfo->pmport_state |= SATA_STATE_READY;
11831 	pmportinfo->pmport_dev_type = sata_device->satadev_type;
11832 	sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11833 
11834 	/*
11835 	 * If we are re-probing the port, there may be
11836 	 * sata_drive_info structure attached
11837 	 * (or sata_pm_info, if PMult is supported).
11838 	 */
11839 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
11840 		/*
11841 		 * There is no device, so remove device info structure,
11842 		 * if necessary.
11843 		 */
11844 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11845 		pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11846 		if (sdinfo != NULL) {
11847 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11848 			sata_log(sata_hba_inst, CE_WARN,
11849 			    "SATA device detached from port %d:%d",
11850 			    cport, pmport);
11851 		}
11852 		mutex_exit(&pmportinfo->pmport_mutex);
11853 		return (SATA_SUCCESS);
11854 	}
11855 
11856 	/* this should not be a pmult */
11857 	ASSERT(sata_device->satadev_type != SATA_DTYPE_PMULT);
11858 	if (sdinfo == NULL) {
11859 		/*
11860 		 * There is some device attached, but there is
11861 		 * no sata_drive_info structure - allocate one
11862 		 */
11863 		mutex_exit(&pmportinfo->pmport_mutex);
11864 		sdinfo = kmem_zalloc(sizeof (sata_drive_info_t),
11865 		    KM_SLEEP);
11866 		mutex_enter(&pmportinfo->pmport_mutex);
11867 		/*
11868 		 * Recheck, that the port state did not change when we
11869 		 * released mutex.
11870 		 */
11871 		if (pmportinfo->pmport_state & SATA_STATE_READY) {
11872 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = sdinfo;
11873 			sdinfo->satadrv_addr = pmportinfo->pmport_addr;
11874 			sdinfo->satadrv_addr.qual = SATA_ADDR_DPMPORT;
11875 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11876 			sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
11877 		} else {
11878 			/*
11879 			 * Port is not in ready state, we
11880 			 * cannot attach a device.
11881 			 */
11882 			mutex_exit(&pmportinfo->pmport_mutex);
11883 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11884 			return (SATA_SUCCESS);
11885 		}
11886 		/*
11887 		 * Since we are adding device, presumably new one,
11888 		 * indicate that it  should be initalized,
11889 		 * as well as some internal framework states).
11890 		 */
11891 		init_device = B_TRUE;
11892 	}
11893 
11894 	pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
11895 	sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
11896 
11897 	mutex_exit(&pmportinfo->pmport_mutex);
11898 	/*
11899 	 * Figure out what kind of device we are really
11900 	 * dealing with.
11901 	 */
11902 	rval = sata_probe_device(sata_hba_inst, sata_device);
11903 
11904 	mutex_enter(&pmportinfo->pmport_mutex);
11905 	if (rval == SATA_SUCCESS) {
11906 		/*
11907 		 * If we are dealing with the same type of a device as before,
11908 		 * restore its settings flags.
11909 		 */
11910 		if (osdinfo != NULL &&
11911 		    sata_device->satadev_type == prev_device_type)
11912 			sdinfo->satadrv_settings = prev_device_settings;
11913 
11914 		mutex_exit(&pmportinfo->pmport_mutex);
11915 		/* Set initial device features, if necessary */
11916 		if (init_device == B_TRUE) {
11917 			rval = sata_initialize_device(sata_hba_inst, sdinfo);
11918 		}
11919 		if (rval == SATA_SUCCESS)
11920 			return (rval);
11921 	} else {
11922 		/*
11923 		 * If there was some device info before we probe the device,
11924 		 * restore previous device setting, so we can retry from scratch
11925 		 * later. Providing, of course, that device has not disappeared
11926 		 * during probing process.
11927 		 */
11928 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11929 			if (osdinfo != NULL) {
11930 				pmportinfo->pmport_dev_type = prev_device_type;
11931 				sdinfo->satadrv_type = prev_device_type;
11932 				sdinfo->satadrv_state = prev_device_state;
11933 			}
11934 		} else {
11935 			/* device is gone */
11936 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
11937 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11938 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11939 			mutex_exit(&pmportinfo->pmport_mutex);
11940 			return (SATA_SUCCESS);
11941 		}
11942 		mutex_exit(&pmportinfo->pmport_mutex);
11943 	}
11944 
11945 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
11946 		clock_t cur_time = ddi_get_lbolt();
11947 		/*
11948 		 * A device was not successfully identified or initialized.
11949 		 * Track retry time for device identification.
11950 		 */
11951 		if ((cur_time - start_time) <
11952 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
11953 			/* sleep for a while */
11954 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
11955 			goto retry_probe_pmport;
11956 		} else {
11957 			mutex_enter(&pmportinfo->pmport_mutex);
11958 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL)
11959 				SATA_PMPORTINFO_DRV_INFO(pmportinfo)->
11960 				    satadrv_state = SATA_DSTATE_FAILED;
11961 			mutex_exit(&pmportinfo->pmport_mutex);
11962 		}
11963 	}
11964 	return (SATA_SUCCESS);
11965 }
11966 
11967 /*
11968  * Allocated related structure for a port multiplier and its device ports
11969  *
11970  * Port multiplier should be ready and probed, and related information like
11971  * the number of the device ports should be store in sata_device_t.
11972  *
11973  * NOTE: No Mutex should be hold.
11974  */
11975 static int
11976 sata_alloc_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
11977 {
11978 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
11979 	sata_cport_info_t *cportinfo = NULL;
11980 	sata_pmult_info_t *pmultinfo = NULL;
11981 	sata_pmport_info_t *pmportinfo = NULL;
11982 	sata_device_t sd;
11983 	dev_t minor_number;
11984 	char name[16];
11985 	uint8_t cport = sata_device->satadev_addr.cport;
11986 	int rval;
11987 	int npmport;
11988 
11989 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11990 
11991 	/* This function might be called while a port-mult is hot-plugged. */
11992 	mutex_enter(&cportinfo->cport_mutex);
11993 
11994 	/* dev_type's not updated when get called from sata_reprobe_port() */
11995 	if (SATA_CPORTINFO_PMULT_INFO(cportinfo) == NULL) {
11996 		/* Create a pmult_info structure */
11997 		SATA_CPORTINFO_PMULT_INFO(cportinfo) =
11998 		    kmem_zalloc(sizeof (sata_pmult_info_t), KM_SLEEP);
11999 	}
12000 	pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
12001 
12002 	pmultinfo->pmult_addr = sata_device->satadev_addr;
12003 	pmultinfo->pmult_addr.qual = SATA_ADDR_PMULT;
12004 	pmultinfo->pmult_state = SATA_STATE_PROBING;
12005 
12006 	/*
12007 	 * Probe the port multiplier with qualifier SATA_ADDR_PMULT_SPEC,
12008 	 * The HBA driver should initialize and register the port multiplier,
12009 	 * sata_register_pmult() will fill following fields,
12010 	 *   + sata_pmult_info.pmult_gscr
12011 	 *   + sata_pmult_info.pmult_num_dev_ports
12012 	 */
12013 	sd.satadev_addr = sata_device->satadev_addr;
12014 	sd.satadev_addr.qual = SATA_ADDR_PMULT_SPEC;
12015 	mutex_exit(&cportinfo->cport_mutex);
12016 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
12017 	    (SATA_DIP(sata_hba_inst), &sd);
12018 	mutex_enter(&cportinfo->cport_mutex);
12019 
12020 	if (rval != SATA_SUCCESS ||
12021 	    (sd.satadev_type != SATA_DTYPE_PMULT) ||
12022 	    !(sd.satadev_state & SATA_DSTATE_PMULT_INIT)) {
12023 		SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL;
12024 		kmem_free(pmultinfo, sizeof (sata_pmult_info_t));
12025 		cportinfo->cport_state = SATA_PSTATE_FAILED;
12026 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
12027 		mutex_exit(&cportinfo->cport_mutex);
12028 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
12029 		    "sata_alloc_pmult: failed to initialize pmult "
12030 		    "at port %d.", cport)
12031 		return (SATA_FAILURE);
12032 	}
12033 
12034 	/* Initialize pmport_info structure */
12035 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports;
12036 	    npmport++) {
12037 
12038 		/* if everything is allocated, skip */
12039 		if (SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) != NULL)
12040 			continue;
12041 
12042 		pmportinfo = kmem_zalloc(sizeof (sata_pmport_info_t), KM_SLEEP);
12043 		mutex_init(&pmportinfo->pmport_mutex, NULL, MUTEX_DRIVER, NULL);
12044 		mutex_exit(&cportinfo->cport_mutex);
12045 
12046 		mutex_enter(&pmportinfo->pmport_mutex);
12047 		pmportinfo->pmport_addr.cport = cport;
12048 		pmportinfo->pmport_addr.pmport = (uint8_t)npmport;
12049 		pmportinfo->pmport_addr.qual = SATA_ADDR_PMPORT;
12050 		pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
12051 		mutex_exit(&pmportinfo->pmport_mutex);
12052 
12053 		mutex_enter(&cportinfo->cport_mutex);
12054 		SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) = pmportinfo;
12055 
12056 		/* Create an attachment point */
12057 		minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip),
12058 		    cport, (uint8_t)npmport, SATA_ADDR_PMPORT);
12059 		(void) sprintf(name, "%d.%d", cport, npmport);
12060 
12061 		if (ddi_create_minor_node(dip, name, S_IFCHR, minor_number,
12062 		    DDI_NT_SATA_ATTACHMENT_POINT, 0) != DDI_SUCCESS) {
12063 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
12064 			    "cannot create SATA attachment point for "
12065 			    "port %d:%d", cport, npmport);
12066 		}
12067 	}
12068 
12069 	pmultinfo->pmult_state &= ~SATA_STATE_PROBING;
12070 	pmultinfo->pmult_state |= (SATA_STATE_PROBED|SATA_STATE_READY);
12071 	cportinfo->cport_dev_type = SATA_DTYPE_PMULT;
12072 
12073 	mutex_exit(&cportinfo->cport_mutex);
12074 	return (SATA_SUCCESS);
12075 }
12076 
12077 /*
12078  * Free data structures when a port multiplier is removed.
12079  *
12080  * NOTE: No Mutex should be hold.
12081  */
12082 static void
12083 sata_free_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
12084 {
12085 	sata_cport_info_t *cportinfo;
12086 	sata_pmult_info_t *pmultinfo;
12087 	sata_pmport_info_t *pmportinfo;
12088 	sata_device_t pmport_device;
12089 	sata_drive_info_t *sdinfo;
12090 	dev_info_t *tdip;
12091 	char name[16];
12092 	uint8_t cport = sata_device->satadev_addr.cport;
12093 	int npmport;
12094 
12095 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
12096 
12097 	/* This function might be called while port-mult is hot plugged. */
12098 	mutex_enter(&cportinfo->cport_mutex);
12099 
12100 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
12101 	pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
12102 	ASSERT(pmultinfo != NULL);
12103 
12104 	/* Free pmport_info structure */
12105 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports;
12106 	    npmport++) {
12107 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport);
12108 		if (pmportinfo == NULL)
12109 			continue;
12110 		mutex_exit(&cportinfo->cport_mutex);
12111 
12112 		mutex_enter(&pmportinfo->pmport_mutex);
12113 		sdinfo = pmportinfo->pmport_sata_drive;
12114 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
12115 		mutex_exit(&pmportinfo->pmport_mutex);
12116 
12117 		/* Remove attachment point. */
12118 		name[0] = '\0';
12119 		(void) sprintf(name, "%d.%d", cport, npmport);
12120 		ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name);
12121 		sata_log(sata_hba_inst, CE_NOTE,
12122 		    "Remove attachment point of port %d:%d",
12123 		    cport, npmport);
12124 
12125 		/*
12126 		 * Rumove target node
12127 		 */
12128 		bzero(&pmport_device, sizeof (sata_device_t));
12129 		pmport_device.satadev_rev = SATA_DEVICE_REV;
12130 		pmport_device.satadev_addr.cport = cport;
12131 		pmport_device.satadev_addr.pmport = (uint8_t)npmport;
12132 		pmport_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
12133 
12134 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
12135 		    &(pmport_device.satadev_addr));
12136 		if (tdip != NULL && ndi_devi_offline(tdip,
12137 		    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
12138 			/*
12139 			 * Problem :
12140 			 * The target node remained attached.
12141 			 * This happens when the device file was open
12142 			 * or a node was waiting for resources.
12143 			 * Cannot do anything about it.
12144 			 */
12145 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12146 			    "sata_free_pmult: could not unconfigure device "
12147 			    "before disconnecting the SATA port %d:%d",
12148 			    cport, npmport));
12149 
12150 			/*
12151 			 * Set DEVICE REMOVED state in the target
12152 			 * node. It will prevent access to the device
12153 			 * even when a new device is attached, until
12154 			 * the old target node is released, removed and
12155 			 * recreated for a new  device.
12156 			 */
12157 			sata_set_device_removed(tdip);
12158 
12159 			/*
12160 			 * Instruct event daemon to try the target
12161 			 * node cleanup later.
12162 			 */
12163 			sata_set_target_node_cleanup(
12164 			    sata_hba_inst, &(pmport_device.satadev_addr));
12165 
12166 		}
12167 		mutex_enter(&cportinfo->cport_mutex);
12168 
12169 		/*
12170 		 * Add here differentiation for device attached or not
12171 		 */
12172 		if (sdinfo != NULL)  {
12173 			sata_log(sata_hba_inst, CE_WARN,
12174 			    "SATA device detached from port %d:%d",
12175 			    cport, npmport);
12176 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
12177 		}
12178 
12179 		mutex_destroy(&pmportinfo->pmport_mutex);
12180 		kmem_free(pmportinfo, sizeof (sata_pmport_info_t));
12181 	}
12182 
12183 	kmem_free(pmultinfo, sizeof (sata_pmult_info_t));
12184 
12185 	cportinfo->cport_devp.cport_sata_pmult = NULL;
12186 
12187 	sata_log(sata_hba_inst, CE_WARN,
12188 	    "SATA port multiplier detached at port %d", cport);
12189 
12190 	mutex_exit(&cportinfo->cport_mutex);
12191 }
12192 
12193 /*
12194  * Initialize device
12195  * Specified device is initialized to a default state.
12196  *
12197  * Returns SATA_SUCCESS if all device features are set successfully,
12198  * SATA_RETRY if device is accessible but device features were not set
12199  * successfully, and SATA_FAILURE otherwise.
12200  */
12201 static int
12202 sata_initialize_device(sata_hba_inst_t *sata_hba_inst,
12203     sata_drive_info_t *sdinfo)
12204 {
12205 	int rval;
12206 
12207 	sata_save_drive_settings(sdinfo);
12208 
12209 	sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
12210 
12211 	sata_init_write_cache_mode(sdinfo);
12212 
12213 	rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0);
12214 
12215 	/* Determine current data transfer mode */
12216 	if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) {
12217 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
12218 	} else if ((sdinfo->satadrv_id.ai_validinfo &
12219 	    SATA_VALIDINFO_88) != 0 &&
12220 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) {
12221 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
12222 	} else if ((sdinfo->satadrv_id.ai_dworddma &
12223 	    SATA_MDMA_SEL_MASK) != 0) {
12224 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
12225 	} else
12226 		/* DMA supported, not no DMA transfer mode is selected !? */
12227 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
12228 
12229 	if ((sdinfo->satadrv_id.ai_cmdset83 & 0x20) &&
12230 	    (sdinfo->satadrv_id.ai_features86 & 0x20))
12231 		sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
12232 	else
12233 		sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
12234 
12235 	return (rval);
12236 }
12237 
12238 
12239 /*
12240  * Initialize write cache mode.
12241  *
12242  * The default write cache setting for SATA HDD is provided by sata_write_cache
12243  * static variable. ATAPI CD/DVDs devices have write cache default is
12244  * determined by sata_atapicdvd_write_cache static variable.
12245  * ATAPI tape devices have write cache default is determined by
12246  * sata_atapitape_write_cache static variable.
12247  * ATAPI disk devices have write cache default is determined by
12248  * sata_atapidisk_write_cache static variable.
12249  * 1 - enable
12250  * 0 - disable
12251  * any other value - current drive setting
12252  *
12253  * Although there is not reason to disable write cache on CD/DVD devices,
12254  * tape devices and ATAPI disk devices, the default setting control is provided
12255  * for the maximun flexibility.
12256  *
12257  * In the future, it may be overridden by the
12258  * disk-write-cache-enable property setting, if it is defined.
12259  * Returns SATA_SUCCESS if all device features are set successfully,
12260  * SATA_FAILURE otherwise.
12261  */
12262 static void
12263 sata_init_write_cache_mode(sata_drive_info_t *sdinfo)
12264 {
12265 	switch (sdinfo->satadrv_type) {
12266 	case SATA_DTYPE_ATADISK:
12267 		if (sata_write_cache == 1)
12268 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12269 		else if (sata_write_cache == 0)
12270 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12271 		/*
12272 		 * When sata_write_cache value is not 0 or 1,
12273 		 * a current setting of the drive's write cache is used.
12274 		 */
12275 		break;
12276 	case SATA_DTYPE_ATAPICD:
12277 		if (sata_atapicdvd_write_cache == 1)
12278 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12279 		else if (sata_atapicdvd_write_cache == 0)
12280 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12281 		/*
12282 		 * When sata_atapicdvd_write_cache value is not 0 or 1,
12283 		 * a current setting of the drive's write cache is used.
12284 		 */
12285 		break;
12286 	case SATA_DTYPE_ATAPITAPE:
12287 		if (sata_atapitape_write_cache == 1)
12288 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12289 		else if (sata_atapitape_write_cache == 0)
12290 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12291 		/*
12292 		 * When sata_atapitape_write_cache value is not 0 or 1,
12293 		 * a current setting of the drive's write cache is used.
12294 		 */
12295 		break;
12296 	case SATA_DTYPE_ATAPIDISK:
12297 		if (sata_atapidisk_write_cache == 1)
12298 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
12299 		else if (sata_atapidisk_write_cache == 0)
12300 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
12301 		/*
12302 		 * When sata_atapidisk_write_cache value is not 0 or 1,
12303 		 * a current setting of the drive's write cache is used.
12304 		 */
12305 		break;
12306 	}
12307 }
12308 
12309 
12310 /*
12311  * Validate sata address.
12312  * Specified cport, pmport and qualifier has to match
12313  * passed sata_scsi configuration info.
12314  * The presence of an attached device is not verified.
12315  *
12316  * Returns 0 when address is valid, -1 otherwise.
12317  */
12318 static int
12319 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport,
12320     int pmport, int qual)
12321 {
12322 	if (qual == SATA_ADDR_DCPORT && pmport != 0)
12323 		goto invalid_address;
12324 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
12325 		goto invalid_address;
12326 	if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) &&
12327 	    ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) ||
12328 	    (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) ||
12329 	    (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport))))
12330 		goto invalid_address;
12331 
12332 	return (0);
12333 
12334 invalid_address:
12335 	return (-1);
12336 
12337 }
12338 
12339 /*
12340  * Validate scsi address
12341  * SCSI target address is translated into SATA cport/pmport and compared
12342  * with a controller port/device configuration. LUN has to be 0.
12343  * Returns 0 if a scsi target refers to an attached device,
12344  * returns 1 if address is valid but no valid device is attached,
12345  * returns 2 if address is valid but device type is unknown (not valid device),
12346  * returns -1 if bad address or device is of an unsupported type.
12347  * Upon return sata_device argument is set.
12348  *
12349  * Port multiplier is supported now.
12350  */
12351 static int
12352 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst,
12353     struct scsi_address *ap, sata_device_t *sata_device)
12354 {
12355 	int cport, pmport, qual, rval;
12356 
12357 	rval = -1;	/* Invalid address */
12358 	if (ap->a_lun != 0)
12359 		goto out;
12360 
12361 	qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
12362 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
12363 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
12364 
12365 	if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT)
12366 		goto out;
12367 
12368 	if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) ==
12369 	    0) {
12370 
12371 		sata_cport_info_t *cportinfo;
12372 		sata_pmult_info_t *pmultinfo;
12373 		sata_drive_info_t *sdinfo = NULL;
12374 
12375 		sata_device->satadev_addr.qual = qual;
12376 		sata_device->satadev_addr.cport = cport;
12377 		sata_device->satadev_addr.pmport = pmport;
12378 		sata_device->satadev_rev = SATA_DEVICE_REV_1;
12379 
12380 		rval = 1;	/* Valid sata address */
12381 
12382 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
12383 		if (qual == SATA_ADDR_DCPORT) {
12384 			if (cportinfo == NULL ||
12385 			    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
12386 				goto out;
12387 
12388 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
12389 			if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN &&
12390 			    sdinfo != NULL) {
12391 				rval = 2;
12392 				goto out;
12393 			}
12394 
12395 			if ((cportinfo->cport_dev_type &
12396 			    SATA_VALID_DEV_TYPE) == 0) {
12397 				rval = -1;
12398 				goto out;
12399 			}
12400 
12401 		} else if (qual == SATA_ADDR_DPMPORT) {
12402 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
12403 			if (pmultinfo == NULL) {
12404 				rval = -1;
12405 				goto out;
12406 			}
12407 			if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) ==
12408 			    NULL ||
12409 			    SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12410 			    pmport) == SATA_DTYPE_NONE)
12411 				goto out;
12412 
12413 			sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport,
12414 			    pmport);
12415 			if (SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12416 			    pmport) == SATA_DTYPE_UNKNOWN && sdinfo != NULL) {
12417 				rval = 2;
12418 				goto out;
12419 			}
12420 
12421 			if ((SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
12422 			    pmport) & SATA_VALID_DEV_TYPE) == 0) {
12423 				rval = -1;
12424 				goto out;
12425 			}
12426 
12427 		} else {
12428 			rval = -1;
12429 			goto out;
12430 		}
12431 		if ((sdinfo == NULL) ||
12432 		    (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0)
12433 			goto out;
12434 
12435 		sata_device->satadev_type = sdinfo->satadrv_type;
12436 
12437 		return (0);
12438 	}
12439 out:
12440 	if (rval > 0) {
12441 		SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
12442 		    "sata_validate_scsi_address: no valid target %x lun %x",
12443 		    ap->a_target, ap->a_lun);
12444 	}
12445 	return (rval);
12446 }
12447 
12448 /*
12449  * Find dip corresponding to passed device number
12450  *
12451  * Returns NULL if invalid device number is passed or device cannot be found,
12452  * Returns dip is device is found.
12453  */
12454 static dev_info_t *
12455 sata_devt_to_devinfo(dev_t dev)
12456 {
12457 	dev_info_t *dip;
12458 #ifndef __lock_lint
12459 	struct devnames *dnp;
12460 	major_t major = getmajor(dev);
12461 	int instance = SATA_MINOR2INSTANCE(getminor(dev));
12462 
12463 	if (major >= devcnt)
12464 		return (NULL);
12465 
12466 	dnp = &devnamesp[major];
12467 	LOCK_DEV_OPS(&(dnp->dn_lock));
12468 	dip = dnp->dn_head;
12469 	while (dip && (ddi_get_instance(dip) != instance)) {
12470 		dip = ddi_get_next(dip);
12471 	}
12472 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
12473 #endif
12474 
12475 	return (dip);
12476 }
12477 
12478 
12479 /*
12480  * Probe device.
12481  * This function issues Identify Device command and initializes local
12482  * sata_drive_info structure if the device can be identified.
12483  * The device type is determined by examining Identify Device
12484  * command response.
12485  * If the sata_hba_inst has linked drive info structure for this
12486  * device address, the Identify Device data is stored into sata_drive_info
12487  * structure linked to the port info structure.
12488  *
12489  * sata_device has to refer to the valid sata port(s) for HBA described
12490  * by sata_hba_inst structure.
12491  *
12492  * Returns:
12493  *	SATA_SUCCESS if device type was successfully probed and port-linked
12494  *		drive info structure was updated;
12495  *	SATA_FAILURE if there is no device, or device was not probed
12496  *		successully;
12497  *	SATA_RETRY if device probe can be retried later.
12498  * If a device cannot be identified, sata_device's dev_state and dev_type
12499  * fields are set to unknown.
12500  * There are no retries in this function. Any retries should be managed by
12501  * the caller.
12502  */
12503 
12504 
12505 static int
12506 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
12507 {
12508 	sata_pmport_info_t *pmportinfo;
12509 	sata_drive_info_t *sdinfo;
12510 	sata_drive_info_t new_sdinfo;	/* local drive info struct */
12511 	int rval;
12512 
12513 	ASSERT((SATA_CPORT_STATE(sata_hba_inst,
12514 	    sata_device->satadev_addr.cport) &
12515 	    (SATA_STATE_PROBED | SATA_STATE_READY)) != 0);
12516 
12517 	sata_device->satadev_type = SATA_DTYPE_NONE;
12518 
12519 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12520 	    sata_device->satadev_addr.cport)));
12521 
12522 	if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT) {
12523 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
12524 		    sata_device->satadev_addr.cport,
12525 		    sata_device->satadev_addr.pmport);
12526 		ASSERT(pmportinfo != NULL);
12527 	}
12528 
12529 	/* Get pointer to port-linked sata device info structure */
12530 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12531 	if (sdinfo != NULL) {
12532 		sdinfo->satadrv_state &=
12533 		    ~(SATA_STATE_PROBED | SATA_STATE_READY);
12534 		sdinfo->satadrv_state |= SATA_STATE_PROBING;
12535 	} else {
12536 		/* No device to probe */
12537 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12538 		    sata_device->satadev_addr.cport)));
12539 		sata_device->satadev_type = SATA_DTYPE_NONE;
12540 		sata_device->satadev_state = SATA_STATE_UNKNOWN;
12541 		return (SATA_FAILURE);
12542 	}
12543 	/*
12544 	 * Need to issue both types of identify device command and
12545 	 * determine device type by examining retreived data/status.
12546 	 * First, ATA Identify Device.
12547 	 */
12548 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
12549 	new_sdinfo.satadrv_addr = sata_device->satadev_addr;
12550 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12551 	    sata_device->satadev_addr.cport)));
12552 	new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
12553 	rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
12554 	if (rval == SATA_RETRY) {
12555 		/* We may try to check for ATAPI device */
12556 		if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) {
12557 			/*
12558 			 * HBA supports ATAPI - try to issue Identify Packet
12559 			 * Device command.
12560 			 */
12561 			new_sdinfo.satadrv_type = SATA_DTYPE_ATAPI;
12562 			rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
12563 		}
12564 	}
12565 	if (rval == SATA_SUCCESS) {
12566 		/*
12567 		 * Got something responding positively to ATA Identify Device
12568 		 * or to Identify Packet Device cmd.
12569 		 * Save last used device type.
12570 		 */
12571 		sata_device->satadev_type = new_sdinfo.satadrv_type;
12572 
12573 		/* save device info, if possible */
12574 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12575 		    sata_device->satadev_addr.cport)));
12576 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12577 		if (sdinfo == NULL) {
12578 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12579 			    sata_device->satadev_addr.cport)));
12580 			return (SATA_FAILURE);
12581 		}
12582 		/*
12583 		 * Copy drive info into the port-linked drive info structure.
12584 		 */
12585 		*sdinfo = new_sdinfo;
12586 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
12587 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
12588 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
12589 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
12590 			    sata_device->satadev_addr.cport) =
12591 			    sdinfo->satadrv_type;
12592 		else { /* SATA_ADDR_DPMPORT */
12593 			mutex_enter(&pmportinfo->pmport_mutex);
12594 			SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12595 			    sata_device->satadev_addr.cport,
12596 			    sata_device->satadev_addr.pmport) =
12597 			    sdinfo->satadrv_type;
12598 			mutex_exit(&pmportinfo->pmport_mutex);
12599 		}
12600 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12601 		    sata_device->satadev_addr.cport)));
12602 		return (SATA_SUCCESS);
12603 	}
12604 
12605 	/*
12606 	 * It may be SATA_RETRY or SATA_FAILURE return.
12607 	 * Looks like we cannot determine the device type at this time.
12608 	 */
12609 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12610 	    sata_device->satadev_addr.cport)));
12611 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12612 	if (sdinfo != NULL) {
12613 		sata_device->satadev_type = SATA_DTYPE_UNKNOWN;
12614 		sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12615 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
12616 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
12617 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
12618 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
12619 			    sata_device->satadev_addr.cport) =
12620 			    SATA_DTYPE_UNKNOWN;
12621 		else {
12622 			/* SATA_ADDR_DPMPORT */
12623 			mutex_enter(&pmportinfo->pmport_mutex);
12624 			if ((SATA_PMULT_INFO(sata_hba_inst,
12625 			    sata_device->satadev_addr.cport) != NULL) &&
12626 			    (SATA_PMPORT_INFO(sata_hba_inst,
12627 			    sata_device->satadev_addr.cport,
12628 			    sata_device->satadev_addr.pmport) != NULL))
12629 				SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12630 				    sata_device->satadev_addr.cport,
12631 				    sata_device->satadev_addr.pmport) =
12632 				    SATA_DTYPE_UNKNOWN;
12633 			mutex_exit(&pmportinfo->pmport_mutex);
12634 		}
12635 	}
12636 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12637 	    sata_device->satadev_addr.cport)));
12638 	return (rval);
12639 }
12640 
12641 
12642 /*
12643  * Get pointer to sata_drive_info structure.
12644  *
12645  * The sata_device has to contain address (cport, pmport and qualifier) for
12646  * specified sata_scsi structure.
12647  *
12648  * Returns NULL if device address is not valid for this HBA configuration.
12649  * Otherwise, returns a pointer to sata_drive_info structure.
12650  *
12651  * This function should be called with a port mutex held.
12652  */
12653 static sata_drive_info_t *
12654 sata_get_device_info(sata_hba_inst_t *sata_hba_inst,
12655     sata_device_t *sata_device)
12656 {
12657 	uint8_t cport = sata_device->satadev_addr.cport;
12658 	uint8_t pmport = sata_device->satadev_addr.pmport;
12659 	uint8_t qual = sata_device->satadev_addr.qual;
12660 
12661 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
12662 		return (NULL);
12663 
12664 	if (!(SATA_CPORT_STATE(sata_hba_inst, cport) &
12665 	    (SATA_STATE_PROBED | SATA_STATE_READY)))
12666 		/* Port not probed yet */
12667 		return (NULL);
12668 
12669 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE)
12670 		return (NULL);
12671 
12672 	if (qual == SATA_ADDR_DCPORT) {
12673 		/* Request for a device on a controller port */
12674 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
12675 		    SATA_DTYPE_PMULT)
12676 			/* Port multiplier attached */
12677 			return (NULL);
12678 		return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport));
12679 	}
12680 	if (qual == SATA_ADDR_DPMPORT) {
12681 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
12682 		    SATA_DTYPE_PMULT)
12683 			return (NULL);
12684 
12685 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport))
12686 			return (NULL);
12687 
12688 		if (!(SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) &
12689 		    (SATA_STATE_PROBED | SATA_STATE_READY)))
12690 			/* Port multiplier port not probed yet */
12691 			return (NULL);
12692 
12693 		return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport));
12694 	}
12695 
12696 	/* we should not get here */
12697 	return (NULL);
12698 }
12699 
12700 
12701 /*
12702  * sata_identify_device.
12703  * Send Identify Device command to SATA HBA driver.
12704  * If command executes successfully, update sata_drive_info structure pointed
12705  * to by sdinfo argument, including Identify Device data.
12706  * If command fails, invalidate data in sata_drive_info.
12707  *
12708  * Cannot be called from interrupt level.
12709  *
12710  * Returns:
12711  * SATA_SUCCESS if the device was identified as a supported device,
12712  * SATA_RETRY if the device was not identified but could be retried,
12713  * SATA_FAILURE if the device was not identified and identify attempt
12714  *	should not be retried.
12715  */
12716 static int
12717 sata_identify_device(sata_hba_inst_t *sata_hba_inst,
12718     sata_drive_info_t *sdinfo)
12719 {
12720 	uint16_t cfg_word;
12721 	int rval;
12722 
12723 	/* fetch device identify data */
12724 	if ((rval = sata_fetch_device_identify_data(sata_hba_inst,
12725 	    sdinfo)) != SATA_SUCCESS)
12726 		goto fail_unknown;
12727 
12728 	cfg_word = sdinfo->satadrv_id.ai_config;
12729 
12730 	/* Set the correct device type */
12731 	if ((cfg_word & SATA_ATA_TYPE_MASK) == SATA_ATA_TYPE) {
12732 		sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
12733 	} else if (cfg_word == SATA_CFA_TYPE) {
12734 		/* It's a Compact Flash media via CF-to-SATA HDD adapter */
12735 		sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
12736 	} else if ((cfg_word & SATA_ATAPI_TYPE_MASK) == SATA_ATAPI_TYPE) {
12737 		switch (cfg_word & SATA_ATAPI_ID_DEV_TYPE) {
12738 		case SATA_ATAPI_CDROM_DEV:
12739 			sdinfo->satadrv_type = SATA_DTYPE_ATAPICD;
12740 			break;
12741 		case SATA_ATAPI_SQACC_DEV:
12742 			sdinfo->satadrv_type = SATA_DTYPE_ATAPITAPE;
12743 			break;
12744 		case SATA_ATAPI_DIRACC_DEV:
12745 			sdinfo->satadrv_type = SATA_DTYPE_ATAPIDISK;
12746 			break;
12747 		case SATA_ATAPI_PROC_DEV:
12748 			sdinfo->satadrv_type = SATA_DTYPE_ATAPIPROC;
12749 			break;
12750 		default:
12751 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12752 		}
12753 	} else {
12754 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12755 	}
12756 
12757 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12758 		if (sdinfo->satadrv_capacity == 0) {
12759 			/* Non-LBA disk. Too bad... */
12760 			sata_log(sata_hba_inst, CE_WARN,
12761 			    "SATA disk device at port %d does not support LBA",
12762 			    sdinfo->satadrv_addr.cport);
12763 			rval = SATA_FAILURE;
12764 			goto fail_unknown;
12765 		}
12766 	}
12767 #if 0
12768 	/* Left for historical reason */
12769 	/*
12770 	 * Some initial version of SATA spec indicated that at least
12771 	 * UDMA mode 4 has to be supported. It is not metioned in
12772 	 * SerialATA 2.6, so this restriction is removed.
12773 	 */
12774 	/* Check for Ultra DMA modes 6 through 0 being supported */
12775 	for (i = 6; i >= 0; --i) {
12776 		if (sdinfo->satadrv_id.ai_ultradma & (1 << i))
12777 			break;
12778 	}
12779 
12780 	/*
12781 	 * At least UDMA 4 mode has to be supported. If mode 4 or
12782 	 * higher are not supported by the device, fail this
12783 	 * device.
12784 	 */
12785 	if (i < 4) {
12786 		/* No required Ultra DMA mode supported */
12787 		sata_log(sata_hba_inst, CE_WARN,
12788 		    "SATA disk device at port %d does not support UDMA "
12789 		    "mode 4 or higher", sdinfo->satadrv_addr.cport);
12790 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12791 		    "mode 4 or higher required, %d supported", i));
12792 		rval = SATA_FAILURE;
12793 		goto fail_unknown;
12794 	}
12795 #endif
12796 
12797 	/*
12798 	 * For Disk devices, if it doesn't support UDMA mode, we would
12799 	 * like to return failure directly.
12800 	 */
12801 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
12802 	    !((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
12803 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0)) {
12804 		sata_log(sata_hba_inst, CE_WARN,
12805 		    "SATA disk device at port %d does not support UDMA",
12806 		    sdinfo->satadrv_addr.cport);
12807 		rval = SATA_FAILURE;
12808 		goto fail_unknown;
12809 	}
12810 
12811 	return (SATA_SUCCESS);
12812 
12813 fail_unknown:
12814 	/* Invalidate sata_drive_info ? */
12815 	sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
12816 	sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
12817 	return (rval);
12818 }
12819 
12820 /*
12821  * Log/display device information
12822  */
12823 static void
12824 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst,
12825     sata_drive_info_t *sdinfo)
12826 {
12827 	int valid_version;
12828 	char msg_buf[MAXPATHLEN];
12829 	int i;
12830 
12831 	/* Show HBA path */
12832 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf);
12833 
12834 	cmn_err(CE_CONT, "?%s :\n", msg_buf);
12835 
12836 	switch (sdinfo->satadrv_type) {
12837 	case SATA_DTYPE_ATADISK:
12838 		(void) sprintf(msg_buf, "SATA disk device at");
12839 		break;
12840 
12841 	case SATA_DTYPE_ATAPICD:
12842 		(void) sprintf(msg_buf, "SATA CD/DVD (ATAPI) device at");
12843 		break;
12844 
12845 	case SATA_DTYPE_ATAPITAPE:
12846 		(void) sprintf(msg_buf, "SATA tape (ATAPI) device at");
12847 		break;
12848 
12849 	case SATA_DTYPE_ATAPIDISK:
12850 		(void) sprintf(msg_buf, "SATA disk (ATAPI) device at");
12851 		break;
12852 
12853 	case SATA_DTYPE_ATAPIPROC:
12854 		(void) sprintf(msg_buf, "SATA processor (ATAPI) device at");
12855 		break;
12856 
12857 	case SATA_DTYPE_UNKNOWN:
12858 		(void) sprintf(msg_buf,
12859 		    "Unsupported SATA device type (cfg 0x%x) at ",
12860 		    sdinfo->satadrv_id.ai_config);
12861 		break;
12862 	}
12863 
12864 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT)
12865 		cmn_err(CE_CONT, "?\t%s port %d\n",
12866 		    msg_buf, sdinfo->satadrv_addr.cport);
12867 	else
12868 		cmn_err(CE_CONT, "?\t%s port %d:%d\n",
12869 		    msg_buf, sdinfo->satadrv_addr.cport,
12870 		    sdinfo->satadrv_addr.pmport);
12871 
12872 	bcopy(&sdinfo->satadrv_id.ai_model, msg_buf,
12873 	    sizeof (sdinfo->satadrv_id.ai_model));
12874 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model));
12875 	msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
12876 	cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf);
12877 
12878 	bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf,
12879 	    sizeof (sdinfo->satadrv_id.ai_fw));
12880 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw));
12881 	msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
12882 	cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf);
12883 
12884 	bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf,
12885 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12886 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser));
12887 	msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
12888 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12889 		cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
12890 	} else {
12891 		/*
12892 		 * Some drives do not implement serial number and may
12893 		 * violate the spec by providing spaces rather than zeros
12894 		 * in serial number field. Scan the buffer to detect it.
12895 		 */
12896 		for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) {
12897 			if (msg_buf[i] != '\0' && msg_buf[i] != ' ')
12898 				break;
12899 		}
12900 		if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) {
12901 			cmn_err(CE_CONT, "?\tserial number - none\n");
12902 		} else {
12903 			cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
12904 		}
12905 	}
12906 
12907 #ifdef SATA_DEBUG
12908 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
12909 	    sdinfo->satadrv_id.ai_majorversion != 0xffff) {
12910 		int i;
12911 		for (i = 14; i >= 2; i--) {
12912 			if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) {
12913 				valid_version = i;
12914 				break;
12915 			}
12916 		}
12917 		cmn_err(CE_CONT,
12918 		    "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
12919 		    valid_version,
12920 		    sdinfo->satadrv_id.ai_majorversion,
12921 		    sdinfo->satadrv_id.ai_minorversion);
12922 	}
12923 #endif
12924 	/* Log some info */
12925 	cmn_err(CE_CONT, "?\tsupported features:\n");
12926 	msg_buf[0] = '\0';
12927 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12928 		if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48)
12929 			(void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN);
12930 		else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28)
12931 			(void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN);
12932 	}
12933 	if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA)
12934 		(void) strlcat(msg_buf, "DMA", MAXPATHLEN);
12935 	if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ)
12936 		(void) strlcat(msg_buf, ", Native Command Queueing",
12937 		    MAXPATHLEN);
12938 	if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)
12939 		(void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN);
12940 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) &&
12941 	    (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED))
12942 		(void) strlcat(msg_buf, ", SMART", MAXPATHLEN);
12943 	if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) &&
12944 	    (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED))
12945 		(void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN);
12946 	cmn_err(CE_CONT, "?\t %s\n", msg_buf);
12947 	if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA3)
12948 		cmn_err(CE_CONT, "?\tSATA Gen3 signaling speed (6.0Gbps)\n");
12949 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2)
12950 		cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n");
12951 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1)
12952 		cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n");
12953 	if (sdinfo->satadrv_features_support &
12954 	    (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) {
12955 		msg_buf[0] = '\0';
12956 		(void) snprintf(msg_buf, MAXPATHLEN,
12957 		    "Supported queue depth %d",
12958 		    sdinfo->satadrv_queue_depth);
12959 		if (!(sata_func_enable &
12960 		    (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ)))
12961 			(void) strlcat(msg_buf,
12962 			    " - queueing disabled globally", MAXPATHLEN);
12963 		else if (sdinfo->satadrv_queue_depth >
12964 		    sdinfo->satadrv_max_queue_depth) {
12965 			(void) snprintf(&msg_buf[strlen(msg_buf)],
12966 			    MAXPATHLEN - strlen(msg_buf), ", limited to %d",
12967 			    (int)sdinfo->satadrv_max_queue_depth);
12968 		}
12969 		cmn_err(CE_CONT, "?\t%s\n", msg_buf);
12970 	}
12971 
12972 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12973 		(void) sprintf(msg_buf, "\tcapacity = %lu sectors\n",
12974 		    sdinfo->satadrv_capacity);
12975 		cmn_err(CE_CONT, "?%s", msg_buf);
12976 	}
12977 }
12978 
12979 /*
12980  * Log/display port multiplier information
12981  * No Mutex should be hold.
12982  */
12983 static void
12984 sata_show_pmult_info(sata_hba_inst_t *sata_hba_inst,
12985     sata_device_t *sata_device)
12986 {
12987 	_NOTE(ARGUNUSED(sata_hba_inst))
12988 
12989 	int cport = sata_device->satadev_addr.cport;
12990 	sata_pmult_info_t *pmultinfo;
12991 	char msg_buf[MAXPATHLEN];
12992 	uint32_t gscr0, gscr1, gscr2, gscr64;
12993 
12994 	mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12995 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
12996 	if (pmultinfo == NULL) {
12997 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
12998 		return;
12999 	}
13000 
13001 	gscr0 = pmultinfo->pmult_gscr.gscr0;
13002 	gscr1 = pmultinfo->pmult_gscr.gscr1;
13003 	gscr2 = pmultinfo->pmult_gscr.gscr2;
13004 	gscr64 = pmultinfo->pmult_gscr.gscr64;
13005 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
13006 
13007 	cmn_err(CE_CONT, "?Port Multiplier %d device-ports found at port %d",
13008 	    sata_device->satadev_add_info, sata_device->satadev_addr.cport);
13009 
13010 	(void) sprintf(msg_buf, "\tVendor_ID 0x%04x, Module_ID 0x%04x",
13011 	    gscr0 & 0xffff, (gscr0 >> 16) & 0xffff);
13012 	cmn_err(CE_CONT, "?%s", msg_buf);
13013 
13014 	(void) strcpy(msg_buf, "\tSupport SATA PMP Spec ");
13015 	if (gscr1 & (1 << 3))
13016 		(void) strlcat(msg_buf, "1.2", MAXPATHLEN);
13017 	else if (gscr1 & (1 << 2))
13018 		(void) strlcat(msg_buf, "1.1", MAXPATHLEN);
13019 	else if (gscr1 & (1 << 1))
13020 		(void) strlcat(msg_buf, "1.0", MAXPATHLEN);
13021 	else
13022 		(void) strlcat(msg_buf, "unknown", MAXPATHLEN);
13023 	cmn_err(CE_CONT, "?%s", msg_buf);
13024 
13025 	(void) strcpy(msg_buf, "\tSupport ");
13026 	if (gscr64 & (1 << 3))
13027 		(void) strlcat(msg_buf, "Asy-Notif, ",
13028 		    MAXPATHLEN);
13029 	if (gscr64 & (1 << 2))
13030 		(void) strlcat(msg_buf, "Dyn-SSC, ", MAXPATHLEN);
13031 	if (gscr64 & (1 << 1))
13032 		(void) strlcat(msg_buf, "Iss-PMREQ, ", MAXPATHLEN);
13033 	if (gscr64 & (1 << 0))
13034 		(void) strlcat(msg_buf, "BIST", MAXPATHLEN);
13035 	if ((gscr64 & 0xf) == 0)
13036 		(void) strlcat(msg_buf, "nothing", MAXPATHLEN);
13037 	cmn_err(CE_CONT, "?%s", msg_buf);
13038 
13039 	(void) sprintf(msg_buf, "\tNumber of exposed device fan-out ports: %d",
13040 	    gscr2 & SATA_PMULT_PORTNUM_MASK);
13041 	cmn_err(CE_CONT, "?%s", msg_buf);
13042 }
13043 
13044 /*
13045  * sata_save_drive_settings extracts current setting of the device and stores
13046  * it for future reference, in case the device setup would need to be restored
13047  * after the device reset.
13048  *
13049  * For all devices read ahead and write cache settings are saved, if the
13050  * device supports these features at all.
13051  * For ATAPI devices the Removable Media Status Notification setting is saved.
13052  */
13053 static void
13054 sata_save_drive_settings(sata_drive_info_t *sdinfo)
13055 {
13056 	if (SATA_READ_AHEAD_SUPPORTED(sdinfo->satadrv_id) ||
13057 	    SATA_WRITE_CACHE_SUPPORTED(sdinfo->satadrv_id)) {
13058 
13059 		/* Current setting of Read Ahead (and Read Cache) */
13060 		if (SATA_READ_AHEAD_ENABLED(sdinfo->satadrv_id))
13061 			sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
13062 		else
13063 			sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD;
13064 
13065 		/* Current setting of Write Cache */
13066 		if (SATA_WRITE_CACHE_ENABLED(sdinfo->satadrv_id))
13067 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
13068 		else
13069 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
13070 	}
13071 
13072 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
13073 		if (SATA_RM_NOTIFIC_SUPPORTED(sdinfo->satadrv_id))
13074 			sdinfo->satadrv_settings |= SATA_DEV_RMSN;
13075 		else
13076 			sdinfo->satadrv_settings &= ~SATA_DEV_RMSN;
13077 	}
13078 }
13079 
13080 
13081 /*
13082  * sata_check_capacity function determines a disk capacity
13083  * and addressing mode (LBA28/LBA48) by examining a disk identify device data.
13084  *
13085  * NOTE: CHS mode is not supported! If a device does not support LBA,
13086  * this function is not called.
13087  *
13088  * Returns device capacity in number of blocks, i.e. largest addressable LBA+1
13089  */
13090 static uint64_t
13091 sata_check_capacity(sata_drive_info_t *sdinfo)
13092 {
13093 	uint64_t capacity = 0;
13094 	int i;
13095 
13096 	if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK ||
13097 	    !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT)
13098 		/* Capacity valid only for LBA-addressable disk devices */
13099 		return (0);
13100 
13101 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) &&
13102 	    (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) &&
13103 	    (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) {
13104 		/* LBA48 mode supported and enabled */
13105 		sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 |
13106 		    SATA_DEV_F_LBA28;
13107 		for (i = 3;  i >= 0;  --i) {
13108 			capacity <<= 16;
13109 			capacity += sdinfo->satadrv_id.ai_addrsecxt[i];
13110 		}
13111 	} else {
13112 		capacity = sdinfo->satadrv_id.ai_addrsec[1];
13113 		capacity <<= 16;
13114 		capacity += sdinfo->satadrv_id.ai_addrsec[0];
13115 		if (capacity >= 0x1000000)
13116 			/* LBA28 mode */
13117 			sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28;
13118 	}
13119 	return (capacity);
13120 }
13121 
13122 
13123 /*
13124  * Allocate consistent buffer for DMA transfer
13125  *
13126  * Cannot be called from interrupt level or with mutex held - it may sleep.
13127  *
13128  * Returns pointer to allocated buffer structure, or NULL if allocation failed.
13129  */
13130 static struct buf *
13131 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len)
13132 {
13133 	struct scsi_address ap;
13134 	struct buf *bp;
13135 	ddi_dma_attr_t	cur_dma_attr;
13136 
13137 	ASSERT(spx->txlt_sata_pkt != NULL);
13138 	ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran;
13139 	ap.a_target = SATA_TO_SCSI_TARGET(
13140 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport,
13141 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport,
13142 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual);
13143 	ap.a_lun = 0;
13144 
13145 	bp = scsi_alloc_consistent_buf(&ap, NULL, len,
13146 	    B_READ, SLEEP_FUNC, NULL);
13147 
13148 	if (bp != NULL) {
13149 		/* Allocate DMA resources for this buffer */
13150 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
13151 		/*
13152 		 * We use a local version of the dma_attr, to account
13153 		 * for a device addressing limitations.
13154 		 * sata_adjust_dma_attr() will handle sdinfo == NULL which
13155 		 * will cause dma attributes to be adjusted to a lowest
13156 		 * acceptable level.
13157 		 */
13158 		sata_adjust_dma_attr(NULL,
13159 		    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
13160 
13161 		if (sata_dma_buf_setup(spx, PKT_CONSISTENT,
13162 		    SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) {
13163 			scsi_free_consistent_buf(bp);
13164 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
13165 			bp = NULL;
13166 		}
13167 	}
13168 	return (bp);
13169 }
13170 
13171 /*
13172  * Release local buffer (consistent buffer for DMA transfer) allocated
13173  * via sata_alloc_local_buffer().
13174  */
13175 static void
13176 sata_free_local_buffer(sata_pkt_txlate_t *spx)
13177 {
13178 	ASSERT(spx->txlt_sata_pkt != NULL);
13179 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL);
13180 
13181 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0;
13182 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL;
13183 
13184 	sata_common_free_dma_rsrcs(spx);
13185 
13186 	/* Free buffer */
13187 	scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp);
13188 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
13189 }
13190 
13191 /*
13192  * Allocate sata_pkt
13193  * Pkt structure version and embedded strcutures version are initialized.
13194  * sata_pkt and sata_pkt_txlate structures are cross-linked.
13195  *
13196  * Since this may be called in interrupt context by sata_scsi_init_pkt,
13197  * callback argument determines if it can sleep or not.
13198  * Hence, it should not be called from interrupt context.
13199  *
13200  * If successful, non-NULL pointer to a sata pkt is returned.
13201  * Upon failure, NULL pointer is returned.
13202  */
13203 static sata_pkt_t *
13204 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t))
13205 {
13206 	sata_pkt_t *spkt;
13207 	int kmsflag;
13208 
13209 	kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
13210 	spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag);
13211 	if (spkt == NULL) {
13212 		SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13213 		    "sata_pkt_alloc: failed"));
13214 		return (NULL);
13215 	}
13216 	spkt->satapkt_rev = SATA_PKT_REV;
13217 	spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV;
13218 	spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
13219 	spkt->satapkt_framework_private = spx;
13220 	spx->txlt_sata_pkt = spkt;
13221 	return (spkt);
13222 }
13223 
13224 /*
13225  * Free sata pkt allocated via sata_pkt_alloc()
13226  */
13227 static void
13228 sata_pkt_free(sata_pkt_txlate_t *spx)
13229 {
13230 	ASSERT(spx->txlt_sata_pkt != NULL);
13231 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL);
13232 	kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t));
13233 	spx->txlt_sata_pkt = NULL;
13234 }
13235 
13236 
13237 /*
13238  * Adjust DMA attributes.
13239  * SCSI cmds block count is up to 24 bits, SATA cmd block count vary
13240  * from 8 bits to 16 bits, depending on a command being used.
13241  * Limiting max block count arbitrarily to 256 for all read/write
13242  * commands may affects performance, so check both the device and
13243  * controller capability before adjusting dma attributes.
13244  */
13245 void
13246 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr,
13247     ddi_dma_attr_t *adj_dma_attr)
13248 {
13249 	uint32_t count_max;
13250 
13251 	/* Copy original attributes */
13252 	*adj_dma_attr = *dma_attr;
13253 	/*
13254 	 * Things to consider: device addressing capability,
13255 	 * "excessive" controller DMA capabilities.
13256 	 * If a device is being probed/initialized, there are
13257 	 * no device info - use default limits then.
13258 	 */
13259 	if (sdinfo == NULL) {
13260 		count_max = dma_attr->dma_attr_granular * 0x100;
13261 		if (dma_attr->dma_attr_count_max > count_max)
13262 			adj_dma_attr->dma_attr_count_max = count_max;
13263 		if (dma_attr->dma_attr_maxxfer > count_max)
13264 			adj_dma_attr->dma_attr_maxxfer = count_max;
13265 		return;
13266 	}
13267 
13268 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
13269 		if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) {
13270 			/*
13271 			 * 16-bit sector count may be used - we rely on
13272 			 * the assumption that only read and write cmds
13273 			 * will request more than 256 sectors worth of data
13274 			 */
13275 			count_max = adj_dma_attr->dma_attr_granular * 0x10000;
13276 		} else {
13277 			/*
13278 			 * 8-bit sector count will be used - default limits
13279 			 * for dma attributes
13280 			 */
13281 			count_max = adj_dma_attr->dma_attr_granular * 0x100;
13282 		}
13283 		/*
13284 		 * Adjust controler dma attributes, if necessary
13285 		 */
13286 		if (dma_attr->dma_attr_count_max > count_max)
13287 			adj_dma_attr->dma_attr_count_max = count_max;
13288 		if (dma_attr->dma_attr_maxxfer > count_max)
13289 			adj_dma_attr->dma_attr_maxxfer = count_max;
13290 	}
13291 }
13292 
13293 
13294 /*
13295  * Allocate DMA resources for the buffer
13296  * This function handles initial DMA resource allocation as well as
13297  * DMA window shift and may be called repeatedly for the same DMA window
13298  * until all DMA cookies in the DMA window are processed.
13299  * To guarantee that there is always a coherent set of cookies to process
13300  * by SATA HBA driver (observing alignment, device granularity, etc.),
13301  * the number of slots for DMA cookies is equal to lesser of  a number of
13302  * cookies in a DMA window and a max number of scatter/gather entries.
13303  *
13304  * Returns DDI_SUCCESS upon successful operation.
13305  * Return failure code of a failing command or DDI_FAILURE when
13306  * internal cleanup failed.
13307  */
13308 static int
13309 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags,
13310     int (*callback)(caddr_t), caddr_t arg,
13311     ddi_dma_attr_t *cur_dma_attr)
13312 {
13313 	int	rval;
13314 	off_t	offset;
13315 	size_t	size;
13316 	int	max_sg_len, req_len, i;
13317 	uint_t	dma_flags;
13318 	struct buf	*bp;
13319 	uint64_t	cur_txfer_len;
13320 
13321 
13322 	ASSERT(spx->txlt_sata_pkt != NULL);
13323 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
13324 	ASSERT(bp != NULL);
13325 
13326 
13327 	if (spx->txlt_buf_dma_handle == NULL) {
13328 		/*
13329 		 * No DMA resources allocated so far - this is a first call
13330 		 * for this sata pkt.
13331 		 */
13332 		rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst),
13333 		    cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle);
13334 
13335 		if (rval != DDI_SUCCESS) {
13336 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13337 			    "sata_dma_buf_setup: no buf DMA resources %x",
13338 			    rval));
13339 			return (rval);
13340 		}
13341 
13342 		if (bp->b_flags & B_READ)
13343 			dma_flags = DDI_DMA_READ;
13344 		else
13345 			dma_flags = DDI_DMA_WRITE;
13346 
13347 		if (flags & PKT_CONSISTENT)
13348 			dma_flags |= DDI_DMA_CONSISTENT;
13349 
13350 		if (flags & PKT_DMA_PARTIAL)
13351 			dma_flags |= DDI_DMA_PARTIAL;
13352 
13353 		/*
13354 		 * Check buffer alignment and size against dma attributes
13355 		 * Consider dma_attr_align only. There may be requests
13356 		 * with the size lower than device granularity, but they
13357 		 * will not read/write from/to the device, so no adjustment
13358 		 * is necessary. The dma_attr_minxfer theoretically should
13359 		 * be considered, but no HBA driver is checking it.
13360 		 */
13361 		if (IS_P2ALIGNED(bp->b_un.b_addr,
13362 		    cur_dma_attr->dma_attr_align)) {
13363 			rval = ddi_dma_buf_bind_handle(
13364 			    spx->txlt_buf_dma_handle,
13365 			    bp, dma_flags, callback, arg,
13366 			    &spx->txlt_dma_cookie,
13367 			    &spx->txlt_curwin_num_dma_cookies);
13368 		} else { /* Buffer is not aligned */
13369 
13370 			int	(*ddicallback)(caddr_t);
13371 			size_t	bufsz;
13372 
13373 			/* Check id sleeping is allowed */
13374 			ddicallback = (callback == NULL_FUNC) ?
13375 			    DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
13376 
13377 			SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13378 			    "mis-aligned buffer: addr=0x%p, cnt=%lu",
13379 			    (void *)bp->b_un.b_addr, bp->b_bcount);
13380 
13381 			if (bp->b_flags & (B_PAGEIO|B_PHYS))
13382 				/*
13383 				 * CPU will need to access data in the buffer
13384 				 * (for copying) so map it.
13385 				 */
13386 				bp_mapin(bp);
13387 
13388 			ASSERT(spx->txlt_tmp_buf == NULL);
13389 
13390 			/* Buffer may be padded by ddi_dma_mem_alloc()! */
13391 			rval = ddi_dma_mem_alloc(
13392 			    spx->txlt_buf_dma_handle,
13393 			    bp->b_bcount,
13394 			    &sata_acc_attr,
13395 			    DDI_DMA_STREAMING,
13396 			    ddicallback, NULL,
13397 			    &spx->txlt_tmp_buf,
13398 			    &bufsz,
13399 			    &spx->txlt_tmp_buf_handle);
13400 
13401 			if (rval != DDI_SUCCESS) {
13402 				/* DMA mapping failed */
13403 				(void) ddi_dma_free_handle(
13404 				    &spx->txlt_buf_dma_handle);
13405 				spx->txlt_buf_dma_handle = NULL;
13406 #ifdef SATA_DEBUG
13407 				mbuffail_count++;
13408 #endif
13409 				SATADBG1(SATA_DBG_DMA_SETUP,
13410 				    spx->txlt_sata_hba_inst,
13411 				    "sata_dma_buf_setup: "
13412 				    "buf dma mem alloc failed %x\n", rval);
13413 				return (rval);
13414 			}
13415 			ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf,
13416 			    cur_dma_attr->dma_attr_align));
13417 
13418 #ifdef SATA_DEBUG
13419 			mbuf_count++;
13420 
13421 			if (bp->b_bcount != bufsz)
13422 				/*
13423 				 * This will require special handling, because
13424 				 * DMA cookies will be based on the temporary
13425 				 * buffer size, not the original buffer
13426 				 * b_bcount, so the residue may have to
13427 				 * be counted differently.
13428 				 */
13429 				SATADBG2(SATA_DBG_DMA_SETUP,
13430 				    spx->txlt_sata_hba_inst,
13431 				    "sata_dma_buf_setup: bp size %x != "
13432 				    "bufsz %x\n", bp->b_bcount, bufsz);
13433 #endif
13434 			if (dma_flags & DDI_DMA_WRITE) {
13435 				/*
13436 				 * Write operation - copy data into
13437 				 * an aligned temporary buffer. Buffer will be
13438 				 * synced for device by ddi_dma_addr_bind_handle
13439 				 */
13440 				bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf,
13441 				    bp->b_bcount);
13442 			}
13443 
13444 			rval = ddi_dma_addr_bind_handle(
13445 			    spx->txlt_buf_dma_handle,
13446 			    NULL,
13447 			    spx->txlt_tmp_buf,
13448 			    bufsz, dma_flags, ddicallback, 0,
13449 			    &spx->txlt_dma_cookie,
13450 			    &spx->txlt_curwin_num_dma_cookies);
13451 		}
13452 
13453 		switch (rval) {
13454 		case DDI_DMA_PARTIAL_MAP:
13455 			SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13456 			    "sata_dma_buf_setup: DMA Partial Map\n", NULL);
13457 			/*
13458 			 * Partial DMA mapping.
13459 			 * Retrieve number of DMA windows for this request.
13460 			 */
13461 			if (ddi_dma_numwin(spx->txlt_buf_dma_handle,
13462 			    &spx->txlt_num_dma_win) != DDI_SUCCESS) {
13463 				if (spx->txlt_tmp_buf != NULL) {
13464 					ddi_dma_mem_free(
13465 					    &spx->txlt_tmp_buf_handle);
13466 					spx->txlt_tmp_buf = NULL;
13467 				}
13468 				(void) ddi_dma_unbind_handle(
13469 				    spx->txlt_buf_dma_handle);
13470 				(void) ddi_dma_free_handle(
13471 				    &spx->txlt_buf_dma_handle);
13472 				spx->txlt_buf_dma_handle = NULL;
13473 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13474 				    "sata_dma_buf_setup: numwin failed\n"));
13475 				return (DDI_FAILURE);
13476 			}
13477 			SATADBG2(SATA_DBG_DMA_SETUP,
13478 			    spx->txlt_sata_hba_inst,
13479 			    "sata_dma_buf_setup: windows: %d, cookies: %d\n",
13480 			    spx->txlt_num_dma_win,
13481 			    spx->txlt_curwin_num_dma_cookies);
13482 			spx->txlt_cur_dma_win = 0;
13483 			break;
13484 
13485 		case DDI_DMA_MAPPED:
13486 			/* DMA fully mapped */
13487 			spx->txlt_num_dma_win = 1;
13488 			spx->txlt_cur_dma_win = 0;
13489 			SATADBG1(SATA_DBG_DMA_SETUP,
13490 			    spx->txlt_sata_hba_inst,
13491 			    "sata_dma_buf_setup: windows: 1 "
13492 			    "cookies: %d\n", spx->txlt_curwin_num_dma_cookies);
13493 			break;
13494 
13495 		default:
13496 			/* DMA mapping failed */
13497 			if (spx->txlt_tmp_buf != NULL) {
13498 				ddi_dma_mem_free(
13499 				    &spx->txlt_tmp_buf_handle);
13500 				spx->txlt_tmp_buf = NULL;
13501 			}
13502 			(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
13503 			spx->txlt_buf_dma_handle = NULL;
13504 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
13505 			    "sata_dma_buf_setup: buf dma handle binding "
13506 			    "failed %x\n", rval));
13507 			return (rval);
13508 		}
13509 		spx->txlt_curwin_processed_dma_cookies = 0;
13510 		spx->txlt_dma_cookie_list = NULL;
13511 	} else {
13512 		/*
13513 		 * DMA setup is reused. Check if we need to process more
13514 		 * cookies in current window, or to get next window, if any.
13515 		 */
13516 
13517 		ASSERT(spx->txlt_curwin_processed_dma_cookies <=
13518 		    spx->txlt_curwin_num_dma_cookies);
13519 
13520 		if (spx->txlt_curwin_processed_dma_cookies ==
13521 		    spx->txlt_curwin_num_dma_cookies) {
13522 			/*
13523 			 * All cookies from current DMA window were processed.
13524 			 * Get next DMA window.
13525 			 */
13526 			spx->txlt_cur_dma_win++;
13527 			if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) {
13528 				(void) ddi_dma_getwin(spx->txlt_buf_dma_handle,
13529 				    spx->txlt_cur_dma_win, &offset, &size,
13530 				    &spx->txlt_dma_cookie,
13531 				    &spx->txlt_curwin_num_dma_cookies);
13532 				spx->txlt_curwin_processed_dma_cookies = 0;
13533 			} else {
13534 				/* No more windows! End of request! */
13535 				/* What to do? - panic for now */
13536 				ASSERT(spx->txlt_cur_dma_win >=
13537 				    spx->txlt_num_dma_win);
13538 
13539 				spx->txlt_curwin_num_dma_cookies = 0;
13540 				spx->txlt_curwin_processed_dma_cookies = 0;
13541 				spx->txlt_sata_pkt->
13542 				    satapkt_cmd.satacmd_num_dma_cookies = 0;
13543 				return (DDI_SUCCESS);
13544 			}
13545 		}
13546 	}
13547 	/* There better be at least one DMA cookie outstanding */
13548 	ASSERT((spx->txlt_curwin_num_dma_cookies -
13549 	    spx->txlt_curwin_processed_dma_cookies) > 0);
13550 
13551 	if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) {
13552 		/* The default cookie slot was used in previous run */
13553 		ASSERT(spx->txlt_curwin_processed_dma_cookies == 0);
13554 		spx->txlt_dma_cookie_list = NULL;
13555 		spx->txlt_dma_cookie_list_len = 0;
13556 	}
13557 	if (spx->txlt_curwin_processed_dma_cookies == 0) {
13558 		/*
13559 		 * Processing a new DMA window - set-up dma cookies list.
13560 		 * We may reuse previously allocated cookie array if it is
13561 		 * possible.
13562 		 */
13563 		if (spx->txlt_dma_cookie_list != NULL &&
13564 		    spx->txlt_dma_cookie_list_len <
13565 		    spx->txlt_curwin_num_dma_cookies) {
13566 			/*
13567 			 * New DMA window contains more cookies than
13568 			 * the previous one. We need larger cookie list - free
13569 			 * the old one.
13570 			 */
13571 			(void) kmem_free(spx->txlt_dma_cookie_list,
13572 			    spx->txlt_dma_cookie_list_len *
13573 			    sizeof (ddi_dma_cookie_t));
13574 			spx->txlt_dma_cookie_list = NULL;
13575 			spx->txlt_dma_cookie_list_len = 0;
13576 		}
13577 		if (spx->txlt_dma_cookie_list == NULL) {
13578 			/*
13579 			 * Calculate lesser of number of cookies in this
13580 			 * DMA window and number of s/g entries.
13581 			 */
13582 			max_sg_len = cur_dma_attr->dma_attr_sgllen;
13583 			req_len = MIN(max_sg_len,
13584 			    spx->txlt_curwin_num_dma_cookies);
13585 
13586 			/* Allocate new dma cookie array if necessary */
13587 			if (req_len == 1) {
13588 				/* Only one cookie - no need for a list */
13589 				spx->txlt_dma_cookie_list =
13590 				    &spx->txlt_dma_cookie;
13591 				spx->txlt_dma_cookie_list_len = 1;
13592 			} else {
13593 				/*
13594 				 * More than one cookie - try to allocate space.
13595 				 */
13596 				spx->txlt_dma_cookie_list = kmem_zalloc(
13597 				    sizeof (ddi_dma_cookie_t) * req_len,
13598 				    callback == NULL_FUNC ? KM_NOSLEEP :
13599 				    KM_SLEEP);
13600 				if (spx->txlt_dma_cookie_list == NULL) {
13601 					SATADBG1(SATA_DBG_DMA_SETUP,
13602 					    spx->txlt_sata_hba_inst,
13603 					    "sata_dma_buf_setup: cookie list "
13604 					    "allocation failed\n", NULL);
13605 					/*
13606 					 * We could not allocate space for
13607 					 * neccessary number of dma cookies in
13608 					 * this window, so we fail this request.
13609 					 * Next invocation would try again to
13610 					 * allocate space for cookie list.
13611 					 * Note:Packet residue was not modified.
13612 					 */
13613 					return (DDI_DMA_NORESOURCES);
13614 				} else {
13615 					spx->txlt_dma_cookie_list_len = req_len;
13616 				}
13617 			}
13618 		}
13619 		/*
13620 		 * Fetch DMA cookies into cookie list in sata_pkt_txlate.
13621 		 * First cookie was already fetched.
13622 		 */
13623 		*(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie;
13624 		cur_txfer_len =
13625 		    (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size;
13626 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1;
13627 		spx->txlt_curwin_processed_dma_cookies++;
13628 		for (i = 1; (i < spx->txlt_dma_cookie_list_len) &&
13629 		    (i < spx->txlt_curwin_num_dma_cookies); i++) {
13630 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
13631 			    &spx->txlt_dma_cookie_list[i]);
13632 			cur_txfer_len +=
13633 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
13634 			spx->txlt_curwin_processed_dma_cookies++;
13635 			spx->txlt_sata_pkt->
13636 			    satapkt_cmd.satacmd_num_dma_cookies += 1;
13637 		}
13638 	} else {
13639 		SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
13640 		    "sata_dma_buf_setup: sliding within DMA window, "
13641 		    "cur cookie %d, total cookies %d\n",
13642 		    spx->txlt_curwin_processed_dma_cookies,
13643 		    spx->txlt_curwin_num_dma_cookies);
13644 
13645 		/*
13646 		 * Not all cookies from the current dma window were used because
13647 		 * of s/g limitation.
13648 		 * There is no need to re-size the list - it was set at
13649 		 * optimal size, or only default entry is used (s/g = 1).
13650 		 */
13651 		if (spx->txlt_dma_cookie_list == NULL) {
13652 			spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie;
13653 			spx->txlt_dma_cookie_list_len = 1;
13654 		}
13655 		/*
13656 		 * Since we are processing remaining cookies in a DMA window,
13657 		 * there may be less of them than the number of entries in the
13658 		 * current dma cookie list.
13659 		 */
13660 		req_len = MIN(spx->txlt_dma_cookie_list_len,
13661 		    (spx->txlt_curwin_num_dma_cookies -
13662 		    spx->txlt_curwin_processed_dma_cookies));
13663 
13664 		/* Fetch the next batch of cookies */
13665 		for (i = 0, cur_txfer_len = 0; i < req_len; i++) {
13666 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
13667 			    &spx->txlt_dma_cookie_list[i]);
13668 			cur_txfer_len +=
13669 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
13670 			spx->txlt_sata_pkt->
13671 			    satapkt_cmd.satacmd_num_dma_cookies++;
13672 			spx->txlt_curwin_processed_dma_cookies++;
13673 		}
13674 	}
13675 
13676 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0);
13677 
13678 	/* Point sata_cmd to the cookie list */
13679 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list =
13680 	    &spx->txlt_dma_cookie_list[0];
13681 
13682 	/* Remember number of DMA cookies passed in sata packet */
13683 	spx->txlt_num_dma_cookies =
13684 	    spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies;
13685 
13686 	ASSERT(cur_txfer_len != 0);
13687 	if (cur_txfer_len <= bp->b_bcount)
13688 		spx->txlt_total_residue -= cur_txfer_len;
13689 	else {
13690 		/*
13691 		 * Temporary DMA buffer has been padded by
13692 		 * ddi_dma_mem_alloc()!
13693 		 * This requires special handling, because DMA cookies are
13694 		 * based on the temporary buffer size, not the b_bcount,
13695 		 * and we have extra bytes to transfer - but the packet
13696 		 * residue has to stay correct because we will copy only
13697 		 * the requested number of bytes.
13698 		 */
13699 		spx->txlt_total_residue -= bp->b_bcount;
13700 	}
13701 
13702 	return (DDI_SUCCESS);
13703 }
13704 
13705 /*
13706  * Common routine for releasing DMA resources
13707  */
13708 static void
13709 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx)
13710 {
13711 	if (spx->txlt_buf_dma_handle != NULL) {
13712 		if (spx->txlt_tmp_buf != NULL)  {
13713 			/*
13714 			 * Intermediate DMA buffer was allocated.
13715 			 * Free allocated buffer and associated access handle.
13716 			 */
13717 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
13718 			spx->txlt_tmp_buf = NULL;
13719 		}
13720 		/*
13721 		 * Free DMA resources - cookies and handles
13722 		 */
13723 		/* ASSERT(spx->txlt_dma_cookie_list != NULL); */
13724 		if (spx->txlt_dma_cookie_list != NULL) {
13725 			if (spx->txlt_dma_cookie_list !=
13726 			    &spx->txlt_dma_cookie) {
13727 				(void) kmem_free(spx->txlt_dma_cookie_list,
13728 				    spx->txlt_dma_cookie_list_len *
13729 				    sizeof (ddi_dma_cookie_t));
13730 				spx->txlt_dma_cookie_list = NULL;
13731 			}
13732 		}
13733 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
13734 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
13735 		spx->txlt_buf_dma_handle = NULL;
13736 	}
13737 }
13738 
13739 /*
13740  * Free DMA resources
13741  * Used by the HBA driver to release DMA resources that it does not use.
13742  *
13743  * Returns Void
13744  */
13745 void
13746 sata_free_dma_resources(sata_pkt_t *sata_pkt)
13747 {
13748 	sata_pkt_txlate_t *spx;
13749 
13750 	if (sata_pkt == NULL)
13751 		return;
13752 
13753 	spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
13754 
13755 	sata_common_free_dma_rsrcs(spx);
13756 }
13757 
13758 /*
13759  * Fetch Device Identify data.
13760  * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type)
13761  * command to a device and get the device identify data.
13762  * The device_info structure has to be set to device type (for selecting proper
13763  * device identify command).
13764  *
13765  * Returns:
13766  * SATA_SUCCESS if cmd succeeded
13767  * SATA_RETRY if cmd was rejected and could be retried,
13768  * SATA_FAILURE if cmd failed and should not be retried (port error)
13769  *
13770  * Cannot be called in an interrupt context.
13771  */
13772 
13773 static int
13774 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst,
13775     sata_drive_info_t *sdinfo)
13776 {
13777 	struct buf *bp;
13778 	sata_pkt_t *spkt;
13779 	sata_cmd_t *scmd;
13780 	sata_pkt_txlate_t *spx;
13781 	int rval;
13782 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
13783 
13784 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13785 	spx->txlt_sata_hba_inst = sata_hba_inst;
13786 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13787 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13788 	if (spkt == NULL) {
13789 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13790 		return (SATA_RETRY); /* may retry later */
13791 	}
13792 	/* address is needed now */
13793 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13794 
13795 	/*
13796 	 * Allocate buffer for Identify Data return data
13797 	 */
13798 	bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t));
13799 	if (bp == NULL) {
13800 		sata_pkt_free(spx);
13801 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13802 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13803 		    "sata_fetch_device_identify_data: "
13804 		    "cannot allocate buffer for ID"));
13805 		return (SATA_RETRY); /* may retry later */
13806 	}
13807 
13808 	/* Fill sata_pkt */
13809 	sdinfo->satadrv_state = SATA_STATE_PROBING;
13810 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13811 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13812 	/* Synchronous mode, no callback */
13813 	spkt->satapkt_comp = NULL;
13814 	/* Timeout 30s */
13815 	spkt->satapkt_time = sata_default_pkt_time;
13816 
13817 	scmd = &spkt->satapkt_cmd;
13818 	scmd->satacmd_bp = bp;
13819 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13820 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
13821 
13822 	/* Build Identify Device cmd in the sata_pkt */
13823 	scmd->satacmd_addr_type = 0;		/* N/A */
13824 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
13825 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
13826 	scmd->satacmd_lba_mid_lsb = 0;		/* N/A */
13827 	scmd->satacmd_lba_high_lsb = 0;		/* N/A */
13828 	scmd->satacmd_features_reg = 0;		/* N/A */
13829 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13830 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
13831 		/* Identify Packet Device cmd */
13832 		scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE;
13833 	} else {
13834 		/* Identify Device cmd - mandatory for all other devices */
13835 		scmd->satacmd_cmd_reg = SATAC_ID_DEVICE;
13836 	}
13837 
13838 	/* Send pkt to SATA HBA driver */
13839 	rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt);
13840 
13841 #ifdef SATA_INJECT_FAULTS
13842 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
13843 #endif
13844 
13845 	if (rval == SATA_TRAN_ACCEPTED &&
13846 	    spkt->satapkt_reason == SATA_PKT_COMPLETED) {
13847 		if (spx->txlt_buf_dma_handle != NULL) {
13848 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13849 			    DDI_DMA_SYNC_FORKERNEL);
13850 			ASSERT(rval == DDI_SUCCESS);
13851 			if (sata_check_for_dma_error(dip, spx)) {
13852 				ddi_fm_service_impact(dip,
13853 				    DDI_SERVICE_UNAFFECTED);
13854 				rval = SATA_RETRY;
13855 				goto fail;
13856 			}
13857 
13858 		}
13859 		if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config &
13860 		    SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) {
13861 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13862 			    "SATA disk device at port %d - "
13863 			    "partial Identify Data",
13864 			    sdinfo->satadrv_addr.cport));
13865 			rval = SATA_RETRY; /* may retry later */
13866 			goto fail;
13867 		}
13868 		/* Update sata_drive_info */
13869 		bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id,
13870 		    sizeof (sata_id_t));
13871 
13872 		sdinfo->satadrv_features_support = 0;
13873 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
13874 			/*
13875 			 * Retrieve capacity (disks only) and addressing mode
13876 			 */
13877 			sdinfo->satadrv_capacity = sata_check_capacity(sdinfo);
13878 		} else {
13879 			/*
13880 			 * For ATAPI devices one would have to issue
13881 			 * Get Capacity cmd for media capacity. Not here.
13882 			 */
13883 			sdinfo->satadrv_capacity = 0;
13884 			/*
13885 			 * Check what cdb length is supported
13886 			 */
13887 			if ((sdinfo->satadrv_id.ai_config &
13888 			    SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B)
13889 				sdinfo->satadrv_atapi_cdb_len = 16;
13890 			else
13891 				sdinfo->satadrv_atapi_cdb_len = 12;
13892 		}
13893 		/* Setup supported features flags */
13894 		if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT)
13895 			sdinfo->satadrv_features_support |= SATA_DEV_F_DMA;
13896 
13897 		/* Check for SATA GEN and NCQ support */
13898 		if (sdinfo->satadrv_id.ai_satacap != 0 &&
13899 		    sdinfo->satadrv_id.ai_satacap != 0xffff) {
13900 			/* SATA compliance */
13901 			if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ)
13902 				sdinfo->satadrv_features_support |=
13903 				    SATA_DEV_F_NCQ;
13904 			if (sdinfo->satadrv_id.ai_satacap &
13905 			    (SATA_1_SPEED | SATA_2_SPEED | SATA_3_SPEED)) {
13906 				if (sdinfo->satadrv_id.ai_satacap &
13907 				    SATA_3_SPEED)
13908 					sdinfo->satadrv_features_support |=
13909 					    SATA_DEV_F_SATA3;
13910 				if (sdinfo->satadrv_id.ai_satacap &
13911 				    SATA_2_SPEED)
13912 					sdinfo->satadrv_features_support |=
13913 					    SATA_DEV_F_SATA2;
13914 				if (sdinfo->satadrv_id.ai_satacap &
13915 				    SATA_1_SPEED)
13916 					sdinfo->satadrv_features_support |=
13917 					    SATA_DEV_F_SATA1;
13918 			} else {
13919 				sdinfo->satadrv_features_support |=
13920 				    SATA_DEV_F_SATA1;
13921 			}
13922 		}
13923 		if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) &&
13924 		    (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD))
13925 			sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ;
13926 
13927 		sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth;
13928 		if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) ||
13929 		    (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) {
13930 			++sdinfo->satadrv_queue_depth;
13931 			/* Adjust according to controller capabilities */
13932 			sdinfo->satadrv_max_queue_depth = MIN(
13933 			    sdinfo->satadrv_queue_depth,
13934 			    SATA_QDEPTH(sata_hba_inst));
13935 			/* Adjust according to global queue depth limit */
13936 			sdinfo->satadrv_max_queue_depth = MIN(
13937 			    sdinfo->satadrv_max_queue_depth,
13938 			    sata_current_max_qdepth);
13939 			if (sdinfo->satadrv_max_queue_depth == 0)
13940 				sdinfo->satadrv_max_queue_depth = 1;
13941 		} else
13942 			sdinfo->satadrv_max_queue_depth = 1;
13943 
13944 		rval = SATA_SUCCESS;
13945 	} else {
13946 		/*
13947 		 * Woops, no Identify Data.
13948 		 */
13949 		if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) {
13950 			rval = SATA_RETRY; /* may retry later */
13951 		} else if (rval == SATA_TRAN_ACCEPTED) {
13952 			if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR ||
13953 			    spkt->satapkt_reason == SATA_PKT_ABORTED ||
13954 			    spkt->satapkt_reason == SATA_PKT_TIMEOUT ||
13955 			    spkt->satapkt_reason == SATA_PKT_RESET)
13956 				rval = SATA_RETRY; /* may retry later */
13957 			else
13958 				rval = SATA_FAILURE;
13959 		} else {
13960 			rval = SATA_FAILURE;
13961 		}
13962 	}
13963 fail:
13964 	/* Free allocated resources */
13965 	sata_free_local_buffer(spx);
13966 	sata_pkt_free(spx);
13967 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13968 
13969 	return (rval);
13970 }
13971 
13972 
13973 /*
13974  * Some devices may not come-up with default DMA mode (UDMA or MWDMA).
13975  * UDMA mode is checked first, followed by MWDMA mode.
13976  * set correctly, so this function is setting it to the highest supported level.
13977  * Older SATA spec required that the device supports at least DMA 4 mode and
13978  * UDMA mode is selected.  It is not mentioned in SerialATA 2.6, so this
13979  * restriction has been removed.
13980  *
13981  * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported.
13982  * Returns SATA_FAILURE if proper DMA mode could not be selected.
13983  *
13984  * NOTE: This function should be called only if DMA mode is supported.
13985  */
13986 static int
13987 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo)
13988 {
13989 	sata_pkt_t *spkt;
13990 	sata_cmd_t *scmd;
13991 	sata_pkt_txlate_t *spx;
13992 	int i, mode;
13993 	uint8_t subcmd;
13994 	int rval = SATA_SUCCESS;
13995 
13996 	ASSERT(sdinfo != NULL);
13997 	ASSERT(sata_hba_inst != NULL);
13998 
13999 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
14000 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) {
14001 		/* Find highest Ultra DMA mode supported */
14002 		for (mode = 6; mode >= 0; --mode) {
14003 			if (sdinfo->satadrv_id.ai_ultradma & (1 << mode))
14004 				break;
14005 		}
14006 #if 0
14007 		/* Left for historical reasons */
14008 		/*
14009 		 * Some initial version of SATA spec indicated that at least
14010 		 * UDMA mode 4 has to be supported. It is not mentioned in
14011 		 * SerialATA 2.6, so this restriction is removed.
14012 		 */
14013 		if (mode < 4)
14014 			return (SATA_FAILURE);
14015 #endif
14016 
14017 		/*
14018 		 * For disk, we're still going to set DMA mode whatever is
14019 		 * selected by default
14020 		 *
14021 		 * We saw an old maxtor sata drive will select Ultra DMA and
14022 		 * Multi-Word DMA simultaneouly by default, which is going
14023 		 * to cause DMA command timed out, so we need to select DMA
14024 		 * mode even when it's already done by default
14025 		 */
14026 		if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) {
14027 
14028 			/* Find UDMA mode currently selected */
14029 			for (i = 6; i >= 0; --i) {
14030 				if (sdinfo->satadrv_id.ai_ultradma &
14031 				    (1 << (i + 8)))
14032 					break;
14033 			}
14034 			if (i >= mode)
14035 				/* Nothing to do */
14036 				return (SATA_SUCCESS);
14037 		}
14038 
14039 		subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA;
14040 
14041 	} else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) {
14042 		/* Find highest MultiWord DMA mode supported */
14043 		for (mode = 2; mode >= 0; --mode) {
14044 			if (sdinfo->satadrv_id.ai_dworddma & (1 << mode))
14045 				break;
14046 		}
14047 
14048 		/*
14049 		 * For disk, We're still going to set DMA mode whatever is
14050 		 * selected by default
14051 		 *
14052 		 * We saw an old maxtor sata drive will select Ultra DMA and
14053 		 * Multi-Word DMA simultaneouly by default, which is going
14054 		 * to cause DMA command timed out, so we need to select DMA
14055 		 * mode even when it's already done by default
14056 		 */
14057 		if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK) {
14058 
14059 			/* Find highest MultiWord DMA mode selected */
14060 			for (i = 2; i >= 0; --i) {
14061 				if (sdinfo->satadrv_id.ai_dworddma &
14062 				    (1 << (i + 8)))
14063 					break;
14064 			}
14065 			if (i >= mode)
14066 				/* Nothing to do */
14067 				return (SATA_SUCCESS);
14068 		}
14069 
14070 		subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA;
14071 	} else
14072 		return (SATA_SUCCESS);
14073 
14074 	/*
14075 	 * Set DMA mode via SET FEATURES COMMAND.
14076 	 * Prepare packet for SET FEATURES COMMAND.
14077 	 */
14078 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14079 	spx->txlt_sata_hba_inst = sata_hba_inst;
14080 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
14081 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14082 	if (spkt == NULL) {
14083 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14084 		    "sata_set_dma_mode: could not set DMA mode %d", mode));
14085 		rval = SATA_FAILURE;
14086 		goto done;
14087 	}
14088 	/* Fill sata_pkt */
14089 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14090 	/* Timeout 30s */
14091 	spkt->satapkt_time = sata_default_pkt_time;
14092 	/* Synchronous mode, no callback, interrupts */
14093 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14094 	spkt->satapkt_comp = NULL;
14095 	scmd = &spkt->satapkt_cmd;
14096 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14097 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14098 	scmd->satacmd_addr_type = 0;
14099 	scmd->satacmd_device_reg = 0;
14100 	scmd->satacmd_status_reg = 0;
14101 	scmd->satacmd_error_reg = 0;
14102 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14103 	scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE;
14104 	scmd->satacmd_sec_count_lsb = subcmd | mode;
14105 
14106 	/* Transfer command to HBA */
14107 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
14108 	    spkt) != SATA_TRAN_ACCEPTED ||
14109 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
14110 		/* Pkt execution failed */
14111 		rval = SATA_FAILURE;
14112 	}
14113 done:
14114 
14115 	/* Free allocated resources */
14116 	if (spkt != NULL)
14117 		sata_pkt_free(spx);
14118 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14119 
14120 	return (rval);
14121 }
14122 
14123 
14124 /*
14125  * Set device caching mode.
14126  * One of the following operations should be specified:
14127  * SATAC_SF_ENABLE_READ_AHEAD
14128  * SATAC_SF_DISABLE_READ_AHEAD
14129  * SATAC_SF_ENABLE_WRITE_CACHE
14130  * SATAC_SF_DISABLE_WRITE_CACHE
14131  *
14132  * If operation fails, system log messgage is emitted.
14133  * Returns SATA_SUCCESS when the operation succeeds, SATA_RETRY if
14134  * command was sent but did not succeed, and SATA_FAILURE otherwise.
14135  */
14136 
14137 static int
14138 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
14139     int cache_op)
14140 {
14141 	sata_pkt_t *spkt;
14142 	sata_cmd_t *scmd;
14143 	sata_pkt_txlate_t *spx;
14144 	int rval = SATA_SUCCESS;
14145 	int hba_rval;
14146 	char *infop;
14147 
14148 	ASSERT(sdinfo != NULL);
14149 	ASSERT(sata_hba_inst != NULL);
14150 	ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD ||
14151 	    cache_op == SATAC_SF_DISABLE_READ_AHEAD ||
14152 	    cache_op == SATAC_SF_ENABLE_WRITE_CACHE ||
14153 	    cache_op == SATAC_SF_DISABLE_WRITE_CACHE);
14154 
14155 
14156 	/* Prepare packet for SET FEATURES COMMAND */
14157 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14158 	spx->txlt_sata_hba_inst = sata_hba_inst;
14159 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
14160 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14161 	if (spkt == NULL) {
14162 		rval = SATA_FAILURE;
14163 		goto failure;
14164 	}
14165 	/* Fill sata_pkt */
14166 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14167 	/* Timeout 30s */
14168 	spkt->satapkt_time = sata_default_pkt_time;
14169 	/* Synchronous mode, no callback, interrupts */
14170 	spkt->satapkt_op_mode =
14171 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14172 	spkt->satapkt_comp = NULL;
14173 	scmd = &spkt->satapkt_cmd;
14174 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14175 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14176 	scmd->satacmd_addr_type = 0;
14177 	scmd->satacmd_device_reg = 0;
14178 	scmd->satacmd_status_reg = 0;
14179 	scmd->satacmd_error_reg = 0;
14180 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14181 	scmd->satacmd_features_reg = cache_op;
14182 
14183 	/* Transfer command to HBA */
14184 	hba_rval = (*SATA_START_FUNC(sata_hba_inst))(
14185 	    SATA_DIP(sata_hba_inst), spkt);
14186 
14187 #ifdef SATA_INJECT_FAULTS
14188 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
14189 #endif
14190 
14191 	if ((hba_rval != SATA_TRAN_ACCEPTED) ||
14192 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
14193 		/* Pkt execution failed */
14194 		switch (cache_op) {
14195 		case SATAC_SF_ENABLE_READ_AHEAD:
14196 			infop = "enabling read ahead failed";
14197 			break;
14198 		case SATAC_SF_DISABLE_READ_AHEAD:
14199 			infop = "disabling read ahead failed";
14200 			break;
14201 		case SATAC_SF_ENABLE_WRITE_CACHE:
14202 			infop = "enabling write cache failed";
14203 			break;
14204 		case SATAC_SF_DISABLE_WRITE_CACHE:
14205 			infop = "disabling write cache failed";
14206 			break;
14207 		}
14208 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
14209 		rval = SATA_RETRY;
14210 	}
14211 failure:
14212 	/* Free allocated resources */
14213 	if (spkt != NULL)
14214 		sata_pkt_free(spx);
14215 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14216 	return (rval);
14217 }
14218 
14219 /*
14220  * Set Removable Media Status Notification (enable/disable)
14221  * state == 0 , disable
14222  * state != 0 , enable
14223  *
14224  * If operation fails, system log messgage is emitted.
14225  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
14226  */
14227 
14228 static int
14229 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
14230     int state)
14231 {
14232 	sata_pkt_t *spkt;
14233 	sata_cmd_t *scmd;
14234 	sata_pkt_txlate_t *spx;
14235 	int rval = SATA_SUCCESS;
14236 	char *infop;
14237 
14238 	ASSERT(sdinfo != NULL);
14239 	ASSERT(sata_hba_inst != NULL);
14240 
14241 	/* Prepare packet for SET FEATURES COMMAND */
14242 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
14243 	spx->txlt_sata_hba_inst = sata_hba_inst;
14244 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
14245 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
14246 	if (spkt == NULL) {
14247 		rval = SATA_FAILURE;
14248 		goto failure;
14249 	}
14250 	/* Fill sata_pkt */
14251 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
14252 	/* Timeout 30s */
14253 	spkt->satapkt_time = sata_default_pkt_time;
14254 	/* Synchronous mode, no callback, interrupts */
14255 	spkt->satapkt_op_mode =
14256 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
14257 	spkt->satapkt_comp = NULL;
14258 	scmd = &spkt->satapkt_cmd;
14259 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
14260 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
14261 	scmd->satacmd_addr_type = 0;
14262 	scmd->satacmd_device_reg = 0;
14263 	scmd->satacmd_status_reg = 0;
14264 	scmd->satacmd_error_reg = 0;
14265 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
14266 	if (state == 0)
14267 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN;
14268 	else
14269 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN;
14270 
14271 	/* Transfer command to HBA */
14272 	if (((*SATA_START_FUNC(sata_hba_inst))(
14273 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
14274 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
14275 		/* Pkt execution failed */
14276 		if (state == 0)
14277 			infop = "disabling Removable Media Status "
14278 			    "Notification failed";
14279 		else
14280 			infop = "enabling Removable Media Status "
14281 			    "Notification failed";
14282 
14283 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
14284 		rval = SATA_FAILURE;
14285 	}
14286 failure:
14287 	/* Free allocated resources */
14288 	if (spkt != NULL)
14289 		sata_pkt_free(spx);
14290 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
14291 	return (rval);
14292 }
14293 
14294 
14295 /*
14296  * Update state and copy port ss* values from passed sata_device structure.
14297  * sata_address is validated - if not valid, nothing is changed in sata_scsi
14298  * configuration struct.
14299  *
14300  * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function
14301  * regardless of the state in device argument.
14302  *
14303  * Port mutex should be held while calling this function.
14304  */
14305 static void
14306 sata_update_port_info(sata_hba_inst_t *sata_hba_inst,
14307     sata_device_t *sata_device)
14308 {
14309 	sata_cport_info_t *cportinfo;
14310 
14311 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT ||
14312 	    sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
14313 		if (SATA_NUM_CPORTS(sata_hba_inst) <=
14314 		    sata_device->satadev_addr.cport)
14315 			return;
14316 
14317 		cportinfo = SATA_CPORT_INFO(sata_hba_inst,
14318 		    sata_device->satadev_addr.cport);
14319 
14320 		ASSERT(mutex_owned(&cportinfo->cport_mutex));
14321 		cportinfo->cport_scr = sata_device->satadev_scr;
14322 
14323 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
14324 		cportinfo->cport_state &= ~(SATA_PSTATE_PWRON |
14325 		    SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
14326 		cportinfo->cport_state |=
14327 		    sata_device->satadev_state & SATA_PSTATE_VALID;
14328 	}
14329 }
14330 
14331 void
14332 sata_update_pmport_info(sata_hba_inst_t *sata_hba_inst,
14333     sata_device_t *sata_device)
14334 {
14335 	sata_pmport_info_t *pmportinfo;
14336 
14337 	if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT &&
14338 	    sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) ||
14339 	    SATA_NUM_PMPORTS(sata_hba_inst,
14340 	    sata_device->satadev_addr.cport) <
14341 	    sata_device->satadev_addr.pmport) {
14342 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
14343 		    "sata_update_port_info: error address %p.",
14344 		    &sata_device->satadev_addr);
14345 		return;
14346 	}
14347 
14348 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
14349 	    sata_device->satadev_addr.cport,
14350 	    sata_device->satadev_addr.pmport);
14351 
14352 	ASSERT(mutex_owned(&pmportinfo->pmport_mutex));
14353 	pmportinfo->pmport_scr = sata_device->satadev_scr;
14354 
14355 	/* Preserve SATA_PSTATE_SHUTDOWN flag */
14356 	pmportinfo->pmport_state &=
14357 	    ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
14358 	pmportinfo->pmport_state |=
14359 	    sata_device->satadev_state & SATA_PSTATE_VALID;
14360 }
14361 
14362 /*
14363  * Extract SATA port specification from an IOCTL argument.
14364  *
14365  * This function return the port the user land send us as is, unless it
14366  * cannot retrieve port spec, then -1 is returned.
14367  *
14368  * Support port multiplier.
14369  */
14370 static int32_t
14371 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp)
14372 {
14373 	int32_t port;
14374 
14375 	/* Extract port number from nvpair in dca structure  */
14376 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
14377 		SATA_LOG_D((sata_hba_inst, CE_NOTE,
14378 		    "sata_get_port_num: invalid port spec 0x%x in ioctl",
14379 		    port));
14380 		port = -1;
14381 	}
14382 
14383 	return (port);
14384 }
14385 
14386 /*
14387  * Get dev_info_t pointer to the device node pointed to by port argument.
14388  * NOTE: target argument is a value used in ioctls to identify
14389  * the AP - it is not a sata_address.
14390  * It is a combination of cport, pmport and address qualifier, encodded same
14391  * way as a scsi target number.
14392  * At this moment it carries only cport number.
14393  *
14394  * PMult hotplug is supported now.
14395  *
14396  * Returns dev_info_t pointer if target device was found, NULL otherwise.
14397  */
14398 
14399 static dev_info_t *
14400 sata_get_target_dip(dev_info_t *dip, uint8_t cport, uint8_t pmport)
14401 {
14402 	dev_info_t	*cdip = NULL;
14403 	int		target, tgt;
14404 	int		circ;
14405 	uint8_t		qual;
14406 
14407 	sata_hba_inst_t	*sata_hba_inst;
14408 	scsi_hba_tran_t *scsi_hba_tran;
14409 
14410 	/* Get target id */
14411 	scsi_hba_tran = ddi_get_driver_private(dip);
14412 	if (scsi_hba_tran == NULL)
14413 		return (NULL);
14414 
14415 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
14416 
14417 	if (sata_hba_inst == NULL)
14418 		return (NULL);
14419 
14420 	/* Identify a port-mult by cport_info.cport_dev_type */
14421 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT)
14422 		qual = SATA_ADDR_DPMPORT;
14423 	else
14424 		qual = SATA_ADDR_DCPORT;
14425 
14426 	target = SATA_TO_SCSI_TARGET(cport, pmport, qual);
14427 
14428 	/* Retrieve target dip */
14429 	ndi_devi_enter(dip, &circ);
14430 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
14431 		dev_info_t *next = ddi_get_next_sibling(cdip);
14432 
14433 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
14434 		    DDI_PROP_DONTPASS, "target", -1);
14435 		if (tgt == -1) {
14436 			/*
14437 			 * This is actually an error condition, but not
14438 			 * a fatal one. Just continue the search.
14439 			 */
14440 			cdip = next;
14441 			continue;
14442 		}
14443 
14444 		if (tgt == target)
14445 			break;
14446 
14447 		cdip = next;
14448 	}
14449 	ndi_devi_exit(dip, circ);
14450 
14451 	return (cdip);
14452 }
14453 
14454 /*
14455  * Get dev_info_t pointer to the device node pointed to by port argument.
14456  * NOTE: target argument is a value used in ioctls to identify
14457  * the AP - it is not a sata_address.
14458  * It is a combination of cport, pmport and address qualifier, encoded same
14459  * way as a scsi target number.
14460  *
14461  * Returns dev_info_t pointer if target device was found, NULL otherwise.
14462  */
14463 
14464 static dev_info_t *
14465 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr)
14466 {
14467 	dev_info_t	*cdip = NULL;
14468 	int		target, tgt;
14469 	int		circ;
14470 
14471 	target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual);
14472 
14473 	ndi_devi_enter(dip, &circ);
14474 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
14475 		dev_info_t *next = ddi_get_next_sibling(cdip);
14476 
14477 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
14478 		    DDI_PROP_DONTPASS, "target", -1);
14479 		if (tgt == -1) {
14480 			/*
14481 			 * This is actually an error condition, but not
14482 			 * a fatal one. Just continue the search.
14483 			 */
14484 			cdip = next;
14485 			continue;
14486 		}
14487 
14488 		if (tgt == target)
14489 			break;
14490 
14491 		cdip = next;
14492 	}
14493 	ndi_devi_exit(dip, circ);
14494 
14495 	return (cdip);
14496 }
14497 
14498 /*
14499  * Process sata port disconnect request.
14500  * Normally, cfgadm sata plugin will try to offline (unconfigure) the device
14501  * before this request. Nevertheless, if a device is still configured,
14502  * we need to attempt to offline and unconfigure device.
14503  * Regardless of the unconfigure operation results the port is marked as
14504  * deactivated and no access to the attached device is possible.
14505  * If the target node remains because unconfigure operation failed, its state
14506  * will be set to DEVICE_REMOVED, preventing it to be used again when a device
14507  * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure
14508  * the device and remove old target node.
14509  *
14510  * This function invokes sata_hba_inst->satahba_tran->
14511  * sata_tran_hotplug_ops->sata_tran_port_deactivate().
14512  * If successful, the device structure (if any) attached to the specified port
14513  * is removed and state of the port marked appropriately.
14514  * Failure of the port_deactivate may keep port in the physically active state,
14515  * or may fail the port.
14516  *
14517  * NOTE: Port multiplier is supported.
14518  */
14519 
14520 static int
14521 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst,
14522     sata_device_t *sata_device)
14523 {
14524 	sata_drive_info_t *sdinfo = NULL, *subsdinfo = NULL;
14525 	sata_cport_info_t *cportinfo = NULL;
14526 	sata_pmport_info_t *pmportinfo = NULL;
14527 	sata_pmult_info_t *pmultinfo = NULL;
14528 	sata_device_t subsdevice;
14529 	int cport, pmport, qual;
14530 	int rval = SATA_SUCCESS;
14531 	int npmport = 0;
14532 	int rv = 0;
14533 
14534 	cport = sata_device->satadev_addr.cport;
14535 	pmport = sata_device->satadev_addr.pmport;
14536 	qual = sata_device->satadev_addr.qual;
14537 
14538 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
14539 	if (qual == SATA_ADDR_DCPORT)
14540 		qual = SATA_ADDR_CPORT;
14541 	else
14542 		qual = SATA_ADDR_PMPORT;
14543 
14544 	/*
14545 	 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran->
14546 	 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
14547 	 * Do the sanity check.
14548 	 */
14549 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
14550 		/* No physical port deactivation supported. */
14551 		return (EINVAL);
14552 	}
14553 
14554 	/* Check the current state of the port */
14555 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14556 	    (SATA_DIP(sata_hba_inst), sata_device);
14557 
14558 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
14559 
14560 	/*
14561 	 * Processing port mulitiplier
14562 	 */
14563 	if (qual == SATA_ADDR_CPORT &&
14564 	    SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) {
14565 		mutex_enter(&cportinfo->cport_mutex);
14566 
14567 		/* Check controller port status */
14568 		sata_update_port_info(sata_hba_inst, sata_device);
14569 		if (rval != SATA_SUCCESS ||
14570 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14571 			/*
14572 			 * Device port status is unknown or it is in failed
14573 			 * state
14574 			 */
14575 			SATA_CPORT_STATE(sata_hba_inst, cport) =
14576 			    SATA_PSTATE_FAILED;
14577 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14578 			    "sata_hba_ioctl: connect: failed to deactivate "
14579 			    "SATA port %d", cport);
14580 			mutex_exit(&cportinfo->cport_mutex);
14581 			return (EIO);
14582 		}
14583 
14584 		/* Disconnect all sub-devices. */
14585 		pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
14586 		if (pmultinfo != NULL) {
14587 
14588 			for (npmport = 0; npmport < SATA_NUM_PMPORTS(
14589 			    sata_hba_inst, cport); npmport ++) {
14590 				subsdinfo = SATA_PMPORT_DRV_INFO(
14591 				    sata_hba_inst, cport, npmport);
14592 				if (subsdinfo == NULL)
14593 					continue;
14594 
14595 				subsdevice.satadev_addr = subsdinfo->
14596 				    satadrv_addr;
14597 
14598 				mutex_exit(&cportinfo->cport_mutex);
14599 				if (sata_ioctl_disconnect(sata_hba_inst,
14600 				    &subsdevice) == SATA_SUCCESS) {
14601 					SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
14602 					"[Remove] device at port %d:%d "
14603 					"successfully.", cport, npmport);
14604 				}
14605 				mutex_enter(&cportinfo->cport_mutex);
14606 			}
14607 		}
14608 
14609 		/* Disconnect the port multiplier */
14610 		cportinfo->cport_state &= ~SATA_STATE_READY;
14611 		mutex_exit(&cportinfo->cport_mutex);
14612 
14613 		sata_device->satadev_addr.qual = qual;
14614 		rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
14615 		    (SATA_DIP(sata_hba_inst), sata_device);
14616 
14617 		sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14618 		    SE_NO_HINT);
14619 
14620 		mutex_enter(&cportinfo->cport_mutex);
14621 		sata_update_port_info(sata_hba_inst, sata_device);
14622 		if (rval != SATA_SUCCESS &&
14623 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
14624 			cportinfo->cport_state = SATA_PSTATE_FAILED;
14625 			rv = EIO;
14626 		} else {
14627 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
14628 		}
14629 		mutex_exit(&cportinfo->cport_mutex);
14630 
14631 		return (rv);
14632 	}
14633 
14634 	/*
14635 	 * Process non-port-multiplier device - it could be a drive connected
14636 	 * to a port multiplier port or a controller port.
14637 	 */
14638 	if (qual == SATA_ADDR_PMPORT) {
14639 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14640 		mutex_enter(&pmportinfo->pmport_mutex);
14641 		sata_update_pmport_info(sata_hba_inst, sata_device);
14642 		if (rval != SATA_SUCCESS ||
14643 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14644 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
14645 			    SATA_PSTATE_FAILED;
14646 			SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
14647 			    "sata_hba_ioctl: connect: failed to deactivate "
14648 			    "SATA port %d:%d", cport, pmport);
14649 			mutex_exit(&pmportinfo->pmport_mutex);
14650 			return (EIO);
14651 		}
14652 
14653 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
14654 			sdinfo = pmportinfo->pmport_sata_drive;
14655 			ASSERT(sdinfo != NULL);
14656 		}
14657 
14658 		/*
14659 		 * Set port's dev_state to not ready - this will disable
14660 		 * an access to a potentially attached device.
14661 		 */
14662 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
14663 
14664 		/* Remove and release sata_drive info structure. */
14665 		if (sdinfo != NULL) {
14666 			if ((sdinfo->satadrv_type &
14667 			    SATA_VALID_DEV_TYPE) != 0) {
14668 				/*
14669 				 * If a target node exists, try to offline
14670 				 * a device and remove target node.
14671 				 */
14672 				mutex_exit(&pmportinfo->pmport_mutex);
14673 				(void) sata_offline_device(sata_hba_inst,
14674 				    sata_device, sdinfo);
14675 				mutex_enter(&pmportinfo->pmport_mutex);
14676 			}
14677 
14678 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
14679 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
14680 			(void) kmem_free((void *)sdinfo,
14681 			    sizeof (sata_drive_info_t));
14682 		}
14683 		mutex_exit(&pmportinfo->pmport_mutex);
14684 
14685 	} else if (qual == SATA_ADDR_CPORT) {
14686 		mutex_enter(&cportinfo->cport_mutex);
14687 		sata_update_port_info(sata_hba_inst, sata_device);
14688 		if (rval != SATA_SUCCESS ||
14689 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
14690 			/*
14691 			 * Device port status is unknown or it is in failed
14692 			 * state
14693 			 */
14694 			SATA_CPORT_STATE(sata_hba_inst, cport) =
14695 			    SATA_PSTATE_FAILED;
14696 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14697 			    "sata_hba_ioctl: connect: failed to deactivate "
14698 			    "SATA port %d", cport);
14699 			mutex_exit(&cportinfo->cport_mutex);
14700 			return (EIO);
14701 		}
14702 
14703 		if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
14704 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
14705 			ASSERT(pmultinfo != NULL);
14706 		} else if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
14707 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14708 			ASSERT(sdinfo != NULL);
14709 		}
14710 		cportinfo->cport_state &= ~SATA_STATE_READY;
14711 
14712 		if (sdinfo != NULL) {
14713 			if ((sdinfo->satadrv_type &
14714 			    SATA_VALID_DEV_TYPE) != 0) {
14715 				/*
14716 				 * If a target node exists, try to offline
14717 				 * a device and remove target node.
14718 				 */
14719 				mutex_exit(&cportinfo->cport_mutex);
14720 				(void) sata_offline_device(sata_hba_inst,
14721 				    sata_device, sdinfo);
14722 				mutex_enter(&cportinfo->cport_mutex);
14723 			}
14724 
14725 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14726 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14727 			(void) kmem_free((void *)sdinfo,
14728 			    sizeof (sata_drive_info_t));
14729 		}
14730 		mutex_exit(&cportinfo->cport_mutex);
14731 	}
14732 
14733 	/* Just ask HBA driver to deactivate port */
14734 	sata_device->satadev_addr.qual = qual;
14735 
14736 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
14737 	    (SATA_DIP(sata_hba_inst), sata_device);
14738 
14739 	/*
14740 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14741 	 * without the hint (to force listener to investivate the state).
14742 	 */
14743 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14744 	    SE_NO_HINT);
14745 
14746 	if (qual == SATA_ADDR_PMPORT) {
14747 		mutex_enter(&pmportinfo->pmport_mutex);
14748 		sata_update_pmport_info(sata_hba_inst, sata_device);
14749 
14750 		if (rval != SATA_SUCCESS &&
14751 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
14752 			/*
14753 			 * Port deactivation failure - do not change port
14754 			 * state unless the state returned by HBA indicates a
14755 			 * port failure.
14756 			 *
14757 			 * NOTE: device structures were released, so devices
14758 			 * now are invisible! Port reset is needed to
14759 			 * re-enumerate devices.
14760 			 */
14761 			pmportinfo->pmport_state = SATA_PSTATE_FAILED;
14762 			rv = EIO;
14763 		} else {
14764 			/*
14765 			 * Deactivation succeded. From now on the sata framework
14766 			 * will not care what is happening to the device, until
14767 			 * the port is activated again.
14768 			 */
14769 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
14770 		}
14771 		mutex_exit(&pmportinfo->pmport_mutex);
14772 	} else if (qual == SATA_ADDR_CPORT) {
14773 		mutex_enter(&cportinfo->cport_mutex);
14774 		sata_update_port_info(sata_hba_inst, sata_device);
14775 
14776 		if (rval != SATA_SUCCESS &&
14777 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
14778 			cportinfo->cport_state = SATA_PSTATE_FAILED;
14779 			rv = EIO;
14780 		} else {
14781 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
14782 		}
14783 		mutex_exit(&cportinfo->cport_mutex);
14784 	}
14785 
14786 	return (rv);
14787 }
14788 
14789 
14790 
14791 /*
14792  * Process sata port connect request
14793  * The sata cfgadm pluging will invoke this operation only if port was found
14794  * in the disconnect state (failed state is also treated as the disconnected
14795  * state).
14796  * DEVCTL_AP_CONNECT would invoke  sata_hba_inst->satahba_tran->
14797  * sata_tran_hotplug_ops->sata_tran_port_activate().
14798  * If successful and a device is found attached to the port,
14799  * the initialization sequence is executed to attach a device structure to
14800  * a port structure. The state of the port and a device would be set
14801  * appropriately.
14802  * The device is not set in configured state (system-wise) by this operation.
14803  *
14804  * Note, that activating the port may generate link events,
14805  * so it is important that following processing and the
14806  * event processing does not interfere with each other!
14807  *
14808  * This operation may remove port failed state and will
14809  * try to make port active and in good standing.
14810  *
14811  * NOTE: Port multiplier is supported.
14812  */
14813 
14814 static int
14815 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst,
14816     sata_device_t *sata_device)
14817 {
14818 	sata_pmport_info_t	*pmportinfo = NULL;
14819 	uint8_t cport, pmport, qual;
14820 	int rv = 0;
14821 
14822 	cport = sata_device->satadev_addr.cport;
14823 	pmport = sata_device->satadev_addr.pmport;
14824 	qual = sata_device->satadev_addr.qual;
14825 
14826 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
14827 	if (qual == SATA_ADDR_DCPORT)
14828 		qual = SATA_ADDR_CPORT;
14829 	else
14830 		qual = SATA_ADDR_PMPORT;
14831 
14832 	if (qual == SATA_ADDR_PMPORT)
14833 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14834 
14835 	/*
14836 	 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->
14837 	 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate().
14838 	 * Perform sanity check now.
14839 	 */
14840 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) {
14841 		/* No physical port activation supported. */
14842 		return (EINVAL);
14843 	}
14844 
14845 	/* Just ask HBA driver to activate port */
14846 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
14847 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
14848 		/*
14849 		 * Port activation failure.
14850 		 */
14851 		if (qual == SATA_ADDR_CPORT) {
14852 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14853 			    cport)->cport_mutex);
14854 			sata_update_port_info(sata_hba_inst, sata_device);
14855 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
14856 				SATA_CPORT_STATE(sata_hba_inst, cport) =
14857 				    SATA_PSTATE_FAILED;
14858 				SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14859 				    "sata_hba_ioctl: connect: failed to "
14860 				    "activate SATA port %d", cport);
14861 			}
14862 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14863 			    cport)->cport_mutex);
14864 		} else { /* port multiplier device port */
14865 			mutex_enter(&pmportinfo->pmport_mutex);
14866 			sata_update_pmport_info(sata_hba_inst, sata_device);
14867 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
14868 				SATA_PMPORT_STATE(sata_hba_inst, cport,
14869 				    pmport) = SATA_PSTATE_FAILED;
14870 				SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
14871 				    "sata_hba_ioctl: connect: failed to "
14872 				    "activate SATA port %d:%d", cport, pmport);
14873 			}
14874 			mutex_exit(&pmportinfo->pmport_mutex);
14875 		}
14876 		return (EIO);
14877 	}
14878 
14879 	/* Virgin port state - will be updated by the port re-probe. */
14880 	if (qual == SATA_ADDR_CPORT) {
14881 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14882 		    cport)->cport_mutex);
14883 		SATA_CPORT_STATE(sata_hba_inst, cport) = 0;
14884 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14885 		    cport)->cport_mutex);
14886 	} else { /* port multiplier device port */
14887 		mutex_enter(&pmportinfo->pmport_mutex);
14888 		SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0;
14889 		mutex_exit(&pmportinfo->pmport_mutex);
14890 	}
14891 
14892 	/*
14893 	 * Probe the port to find its state and attached device.
14894 	 */
14895 	if (sata_reprobe_port(sata_hba_inst, sata_device,
14896 	    SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE)
14897 		rv = EIO;
14898 
14899 	/*
14900 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14901 	 * without the hint
14902 	 */
14903 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14904 	    SE_NO_HINT);
14905 
14906 	/*
14907 	 * If there is a device attached to the port, emit
14908 	 * a message.
14909 	 */
14910 	if (sata_device->satadev_type != SATA_DTYPE_NONE) {
14911 
14912 		if (qual == SATA_ADDR_CPORT) {
14913 			if (sata_device->satadev_type == SATA_DTYPE_PMULT) {
14914 				sata_log(sata_hba_inst, CE_WARN,
14915 				    "SATA port multiplier detected "
14916 				    "at port %d", cport);
14917 			} else {
14918 				sata_log(sata_hba_inst, CE_WARN,
14919 				    "SATA device detected at port %d", cport);
14920 				if (sata_device->satadev_type ==
14921 				    SATA_DTYPE_UNKNOWN) {
14922 				/*
14923 				 * A device was not successfully identified
14924 				 */
14925 				sata_log(sata_hba_inst, CE_WARN,
14926 				    "Could not identify SATA "
14927 				    "device at port %d", cport);
14928 				}
14929 			}
14930 		} else { /* port multiplier device port */
14931 			sata_log(sata_hba_inst, CE_WARN,
14932 			    "SATA device detected at port %d:%d",
14933 			    cport, pmport);
14934 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
14935 				/*
14936 				 * A device was not successfully identified
14937 				 */
14938 				sata_log(sata_hba_inst, CE_WARN,
14939 				    "Could not identify SATA "
14940 				    "device at port %d:%d", cport, pmport);
14941 			}
14942 		}
14943 	}
14944 
14945 	return (rv);
14946 }
14947 
14948 
14949 /*
14950  * Process sata device unconfigure request.
14951  * The unconfigure operation uses generic nexus operation to
14952  * offline a device. It leaves a target device node attached.
14953  * and obviously sata_drive_info attached as well, because
14954  * from the hardware point of view nothing has changed.
14955  */
14956 static int
14957 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst,
14958     sata_device_t *sata_device)
14959 {
14960 	int rv = 0;
14961 	dev_info_t *tdip;
14962 
14963 	/* We are addressing attached device, not a port */
14964 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
14965 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
14966 	else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT)
14967 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
14968 
14969 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
14970 	    &sata_device->satadev_addr)) != NULL) {
14971 
14972 		if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) {
14973 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14974 			    "sata_hba_ioctl: unconfigure: "
14975 			    "failed to unconfigure device at SATA port %d:%d",
14976 			    sata_device->satadev_addr.cport,
14977 			    sata_device->satadev_addr.pmport));
14978 			rv = EIO;
14979 		}
14980 		/*
14981 		 * The target node devi_state should be marked with
14982 		 * DEVI_DEVICE_OFFLINE by ndi_devi_offline().
14983 		 * This would be the indication for cfgadm that
14984 		 * the AP node occupant state is 'unconfigured'.
14985 		 */
14986 
14987 	} else {
14988 		/*
14989 		 * This would indicate a failure on the part of cfgadm
14990 		 * to detect correct state of the node prior to this
14991 		 * call - one cannot unconfigure non-existing device.
14992 		 */
14993 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14994 		    "sata_hba_ioctl: unconfigure: "
14995 		    "attempt to unconfigure non-existing device "
14996 		    "at SATA port %d:%d",
14997 		    sata_device->satadev_addr.cport,
14998 		    sata_device->satadev_addr.pmport));
14999 		rv = ENXIO;
15000 	}
15001 	return (rv);
15002 }
15003 
15004 /*
15005  * Process sata device configure request
15006  * If port is in a failed state, operation is aborted - one has to use
15007  * an explicit connect or port activate request to try to get a port into
15008  * non-failed mode. Port reset wil also work in such situation.
15009  * If the port is in disconnected (shutdown) state, the connect operation is
15010  * attempted prior to any other action.
15011  * When port is in the active state, there is a device attached and the target
15012  * node exists, a device was most likely offlined.
15013  * If target node does not exist, a new target node is created. In both cases
15014  * an attempt is made to online (configure) the device.
15015  *
15016  * NOTE: Port multiplier is supported.
15017  */
15018 static int
15019 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst,
15020     sata_device_t *sata_device)
15021 {
15022 	int cport, pmport, qual;
15023 	int rval;
15024 	boolean_t target = B_TRUE;
15025 	sata_cport_info_t *cportinfo;
15026 	sata_pmport_info_t *pmportinfo = NULL;
15027 	dev_info_t *tdip;
15028 	sata_drive_info_t *sdinfo;
15029 
15030 	cport = sata_device->satadev_addr.cport;
15031 	pmport = sata_device->satadev_addr.pmport;
15032 	qual = sata_device->satadev_addr.qual;
15033 
15034 	/* Get current port state */
15035 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
15036 	    (SATA_DIP(sata_hba_inst), sata_device);
15037 
15038 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
15039 	if (qual == SATA_ADDR_DPMPORT) {
15040 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
15041 		mutex_enter(&pmportinfo->pmport_mutex);
15042 		sata_update_pmport_info(sata_hba_inst, sata_device);
15043 		if (rval != SATA_SUCCESS ||
15044 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
15045 			/*
15046 			 * Obviously, device on a failed port is not visible
15047 			 */
15048 			mutex_exit(&pmportinfo->pmport_mutex);
15049 			return (ENXIO);
15050 		}
15051 		mutex_exit(&pmportinfo->pmport_mutex);
15052 	} else {
15053 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15054 		    cport)->cport_mutex);
15055 		sata_update_port_info(sata_hba_inst, sata_device);
15056 		if (rval != SATA_SUCCESS ||
15057 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
15058 			/*
15059 			 * Obviously, device on a failed port is not visible
15060 			 */
15061 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15062 			    cport)->cport_mutex);
15063 			return (ENXIO);
15064 		}
15065 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15066 		    cport)->cport_mutex);
15067 	}
15068 
15069 	if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) {
15070 		/* need to activate port */
15071 		target = B_FALSE;
15072 
15073 		/* Sanity check */
15074 		if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
15075 			return (ENXIO);
15076 
15077 		/* Just let HBA driver to activate port */
15078 		if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
15079 		    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15080 			/*
15081 			 * Port activation failure - do not change port state
15082 			 * unless the state returned by HBA indicates a port
15083 			 * failure.
15084 			 */
15085 			if (qual == SATA_ADDR_DPMPORT) {
15086 				mutex_enter(&pmportinfo->pmport_mutex);
15087 				sata_update_pmport_info(sata_hba_inst,
15088 				    sata_device);
15089 				if (sata_device->satadev_state &
15090 				    SATA_PSTATE_FAILED)
15091 					pmportinfo->pmport_state =
15092 					    SATA_PSTATE_FAILED;
15093 				mutex_exit(&pmportinfo->pmport_mutex);
15094 			} else {
15095 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15096 				    cport)->cport_mutex);
15097 				sata_update_port_info(sata_hba_inst,
15098 				    sata_device);
15099 				if (sata_device->satadev_state &
15100 				    SATA_PSTATE_FAILED)
15101 					cportinfo->cport_state =
15102 					    SATA_PSTATE_FAILED;
15103 				mutex_exit(&SATA_CPORT_INFO(
15104 				    sata_hba_inst, cport)->cport_mutex);
15105 			}
15106 		}
15107 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15108 		    "sata_hba_ioctl: configure: "
15109 		    "failed to activate SATA port %d:%d",
15110 		    cport, pmport));
15111 		return (EIO);
15112 	}
15113 	/*
15114 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15115 	 * without the hint.
15116 	 */
15117 	sata_gen_sysevent(sata_hba_inst,
15118 	    &sata_device->satadev_addr, SE_NO_HINT);
15119 
15120 	/* Virgin port state */
15121 	if (qual == SATA_ADDR_DPMPORT) {
15122 		mutex_enter(&pmportinfo->pmport_mutex);
15123 		pmportinfo->pmport_state = 0;
15124 		mutex_exit(&pmportinfo->pmport_mutex);
15125 	} else {
15126 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15127 		    cport)-> cport_mutex);
15128 		cportinfo->cport_state = 0;
15129 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15130 		    cport)->cport_mutex);
15131 	}
15132 	/*
15133 	 * Always reprobe port, to get current device info.
15134 	 */
15135 	if (sata_reprobe_port(sata_hba_inst, sata_device,
15136 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
15137 		return (EIO);
15138 
15139 	if (sata_device->satadev_type != SATA_DTYPE_NONE && target == B_FALSE) {
15140 		if (qual == SATA_ADDR_DPMPORT) {
15141 			/*
15142 			 * That's the transition from "inactive" port
15143 			 * to active one with device attached.
15144 			 */
15145 			sata_log(sata_hba_inst, CE_WARN,
15146 			    "SATA device detected at port %d:%d",
15147 			    cport, pmport);
15148 		} else {
15149 			/*
15150 			 * When PM is attached to the cport and cport is
15151 			 * activated, every PM device port needs to be reprobed.
15152 			 * We need to emit message for all devices detected
15153 			 * at port multiplier's device ports.
15154 			 * Add such code here.
15155 			 * For now, just inform about device attached to
15156 			 * cport.
15157 			 */
15158 			sata_log(sata_hba_inst, CE_WARN,
15159 			    "SATA device detected at port %d", cport);
15160 		}
15161 	}
15162 
15163 	/*
15164 	 * This is where real configuration operation starts.
15165 	 *
15166 	 * When PM is attached to the cport and cport is activated,
15167 	 * devices attached PM device ports may have to be configured
15168 	 * explicitly. This may change when port multiplier is supported.
15169 	 * For now, configure only disks and other valid target devices.
15170 	 */
15171 	if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) {
15172 		if (qual == SATA_ADDR_DCPORT) {
15173 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
15174 				/*
15175 				 * A device was not successfully identified
15176 				 */
15177 				sata_log(sata_hba_inst, CE_WARN,
15178 				    "Could not identify SATA "
15179 				    "device at port %d", cport);
15180 			}
15181 		} else { /* port multiplier device port */
15182 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
15183 				/*
15184 				 * A device was not successfully identified
15185 				 */
15186 				sata_log(sata_hba_inst, CE_WARN,
15187 				    "Could not identify SATA "
15188 				    "device at port %d:%d", cport, pmport);
15189 			}
15190 		}
15191 		return (ENXIO);		/* No device to configure */
15192 	}
15193 
15194 	/*
15195 	 * Here we may have a device in reset condition,
15196 	 * but because we are just configuring it, there is
15197 	 * no need to process the reset other than just
15198 	 * to clear device reset condition in the HBA driver.
15199 	 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will
15200 	 * cause a first command sent the HBA driver with the request
15201 	 * to clear device reset condition.
15202 	 */
15203 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15204 	if (qual == SATA_ADDR_DPMPORT)
15205 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15206 	else
15207 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
15208 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
15209 	if (sdinfo == NULL) {
15210 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15211 		return (ENXIO);
15212 	}
15213 	if (sdinfo->satadrv_event_flags &
15214 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
15215 		sdinfo->satadrv_event_flags = 0;
15216 	}
15217 	sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
15218 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15219 
15220 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
15221 	    &sata_device->satadev_addr)) != NULL) {
15222 		/*
15223 		 * Target node exists. Verify, that it belongs
15224 		 * to existing, attached device and not to
15225 		 * a removed device.
15226 		 */
15227 		if (sata_check_device_removed(tdip) == B_TRUE) {
15228 			if (qual == SATA_ADDR_DPMPORT)
15229 				sata_log(sata_hba_inst, CE_WARN,
15230 				    "SATA device at port %d cannot be "
15231 				    "configured. "
15232 				    "Application(s) accessing "
15233 				    "previously attached device "
15234 				    "have to release it before newly "
15235 				    "inserted device can be made accessible.",
15236 				    cport);
15237 			else
15238 				sata_log(sata_hba_inst, CE_WARN,
15239 				    "SATA device at port %d:%d cannot be"
15240 				    "configured. "
15241 				    "Application(s) accessing "
15242 				    "previously attached device "
15243 				    "have to release it before newly "
15244 				    "inserted device can be made accessible.",
15245 				    cport, pmport);
15246 			return (EIO);
15247 		}
15248 		/*
15249 		 * Device was not removed and re-inserted.
15250 		 * Try to online it.
15251 		 */
15252 		if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) {
15253 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15254 			    "sata_hba_ioctl: configure: "
15255 			    "onlining device at SATA port "
15256 			    "%d:%d failed", cport, pmport));
15257 			return (EIO);
15258 		}
15259 
15260 		if (qual == SATA_ADDR_DPMPORT) {
15261 			mutex_enter(&pmportinfo->pmport_mutex);
15262 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
15263 			mutex_exit(&pmportinfo->pmport_mutex);
15264 		} else {
15265 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15266 			    cport)->cport_mutex);
15267 			cportinfo-> cport_tgtnode_clean = B_TRUE;
15268 			mutex_exit(&SATA_CPORT_INFO(
15269 			    sata_hba_inst, cport)->cport_mutex);
15270 		}
15271 	} else {
15272 		/*
15273 		 * No target node - need to create a new target node.
15274 		 */
15275 		if (qual == SATA_ADDR_DPMPORT) {
15276 			mutex_enter(&pmportinfo->pmport_mutex);
15277 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
15278 			mutex_exit(&pmportinfo->pmport_mutex);
15279 		} else {
15280 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15281 			    cport_mutex);
15282 			cportinfo-> cport_tgtnode_clean = B_TRUE;
15283 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15284 			    cport_mutex);
15285 		}
15286 
15287 		tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
15288 		    sata_hba_inst, &sata_device->satadev_addr);
15289 		if (tdip == NULL) {
15290 			/* Configure operation failed */
15291 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15292 			    "sata_hba_ioctl: configure: "
15293 			    "configuring SATA device at port %d:%d "
15294 			    "failed", cport, pmport));
15295 			return (EIO);
15296 		}
15297 	}
15298 	return (0);
15299 }
15300 
15301 
15302 /*
15303  * Process ioctl deactivate port request.
15304  * Arbitrarily unconfigure attached device, if any.
15305  * Even if the unconfigure fails, proceed with the
15306  * port deactivation.
15307  *
15308  * NOTE: Port Multiplier is supported now.
15309  */
15310 
15311 static int
15312 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst,
15313     sata_device_t *sata_device)
15314 {
15315 	int cport, pmport, qual;
15316 	int rval, rv = 0;
15317 	int npmport;
15318 	sata_cport_info_t *cportinfo;
15319 	sata_pmport_info_t *pmportinfo;
15320 	sata_pmult_info_t *pmultinfo;
15321 	dev_info_t *tdip;
15322 	sata_drive_info_t *sdinfo = NULL;
15323 	sata_device_t subsdevice;
15324 
15325 	/* Sanity check */
15326 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL)
15327 		return (ENOTSUP);
15328 
15329 	cport = sata_device->satadev_addr.cport;
15330 	pmport = sata_device->satadev_addr.pmport;
15331 	qual = sata_device->satadev_addr.qual;
15332 
15333 	/* SCSI_TO_SATA_ADDR_QUAL() translate ap_id into a device qualifier */
15334 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
15335 	if (qual == SATA_ADDR_DCPORT)
15336 		qual = SATA_ADDR_CPORT;
15337 	else
15338 		qual = SATA_ADDR_PMPORT;
15339 
15340 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
15341 	if (qual == SATA_ADDR_PMPORT)
15342 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
15343 
15344 	/*
15345 	 * Processing port multiplier
15346 	 */
15347 	if (qual == SATA_ADDR_CPORT &&
15348 	    SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) {
15349 		mutex_enter(&cportinfo->cport_mutex);
15350 
15351 		/* Deactivate all sub-deices */
15352 		pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
15353 		if (pmultinfo != NULL) {
15354 			for (npmport = 0; npmport < SATA_NUM_PMPORTS(
15355 			    sata_hba_inst, cport); npmport++) {
15356 
15357 				subsdevice.satadev_addr.cport = cport;
15358 				subsdevice.satadev_addr.pmport =
15359 				    (uint8_t)npmport;
15360 				subsdevice.satadev_addr.qual =
15361 				    SATA_ADDR_DPMPORT;
15362 
15363 				SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
15364 				    "sata_hba_ioctl: deactivate: trying to "
15365 				    "deactivate SATA port %d:%d",
15366 				    cport, npmport);
15367 
15368 				mutex_exit(&cportinfo->cport_mutex);
15369 				if (sata_ioctl_deactivate(sata_hba_inst,
15370 				    &subsdevice) == SATA_SUCCESS) {
15371 					SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
15372 					    "[Deactivate] device at port %d:%d "
15373 					    "successfully.", cport, npmport);
15374 				}
15375 				mutex_enter(&cportinfo->cport_mutex);
15376 			}
15377 		}
15378 
15379 		/* Deactivate the port multiplier now. */
15380 		cportinfo->cport_state &= ~SATA_STATE_READY;
15381 		mutex_exit(&cportinfo->cport_mutex);
15382 
15383 		sata_device->satadev_addr.qual = qual;
15384 		rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
15385 		    (SATA_DIP(sata_hba_inst), sata_device);
15386 
15387 		sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15388 		    SE_NO_HINT);
15389 
15390 		mutex_enter(&cportinfo->cport_mutex);
15391 		sata_update_port_info(sata_hba_inst, sata_device);
15392 		if (rval != SATA_SUCCESS) {
15393 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15394 				cportinfo->cport_state = SATA_PSTATE_FAILED;
15395 			}
15396 			rv = EIO;
15397 		} else {
15398 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
15399 		}
15400 		mutex_exit(&cportinfo->cport_mutex);
15401 
15402 		return (rv);
15403 	}
15404 
15405 	/*
15406 	 * Process non-port-multiplier device - it could be a drive connected
15407 	 * to a port multiplier port or a controller port.
15408 	 */
15409 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15410 	if (qual == SATA_ADDR_CPORT) {
15411 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
15412 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
15413 			/* deal only with valid devices */
15414 			if ((cportinfo->cport_dev_type &
15415 			    SATA_VALID_DEV_TYPE) != 0)
15416 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
15417 		}
15418 		cportinfo->cport_state &= ~SATA_STATE_READY;
15419 	} else {
15420 		/* Port multiplier device port */
15421 		mutex_enter(&pmportinfo->pmport_mutex);
15422 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15423 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
15424 		    (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0)
15425 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
15426 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
15427 		mutex_exit(&pmportinfo->pmport_mutex);
15428 	}
15429 
15430 	if (sdinfo != NULL) {
15431 		/*
15432 		 * If a target node exists, try to offline a device and
15433 		 * to remove a target node.
15434 		 */
15435 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15436 		    cport_mutex);
15437 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
15438 		    &sata_device->satadev_addr);
15439 		if (tdip != NULL) {
15440 			/* target node exist */
15441 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
15442 			    "sata_hba_ioctl: port deactivate: "
15443 			    "target node exists.", NULL);
15444 
15445 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) !=
15446 			    NDI_SUCCESS) {
15447 				SATA_LOG_D((sata_hba_inst, CE_WARN,
15448 				    "sata_hba_ioctl: port deactivate: "
15449 				    "failed to unconfigure device at port "
15450 				    "%d:%d before deactivating the port",
15451 				    cport, pmport));
15452 				/*
15453 				 * Set DEVICE REMOVED state in the target
15454 				 * node. It will prevent an access to
15455 				 * the device even when a new device is
15456 				 * attached, until the old target node is
15457 				 * released, removed and recreated for a new
15458 				 * device.
15459 				 */
15460 				sata_set_device_removed(tdip);
15461 
15462 				/*
15463 				 * Instruct the event daemon to try the
15464 				 * target node cleanup later.
15465 				 */
15466 				sata_set_target_node_cleanup(sata_hba_inst,
15467 				    &sata_device->satadev_addr);
15468 			}
15469 		}
15470 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15471 		    cport_mutex);
15472 		/*
15473 		 * In any case, remove and release sata_drive_info
15474 		 * structure.
15475 		 */
15476 		if (qual == SATA_ADDR_CPORT) {
15477 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
15478 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
15479 		} else { /* port multiplier device port */
15480 			mutex_enter(&pmportinfo->pmport_mutex);
15481 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
15482 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
15483 			mutex_exit(&pmportinfo->pmport_mutex);
15484 		}
15485 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
15486 	}
15487 
15488 	if (qual == SATA_ADDR_CPORT) {
15489 		cportinfo->cport_state &= ~(SATA_STATE_PROBED |
15490 		    SATA_STATE_PROBING);
15491 	} else if (qual == SATA_ADDR_PMPORT) {
15492 		mutex_enter(&pmportinfo->pmport_mutex);
15493 		pmportinfo->pmport_state &= ~(SATA_STATE_PROBED |
15494 		    SATA_STATE_PROBING);
15495 		mutex_exit(&pmportinfo->pmport_mutex);
15496 	}
15497 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15498 
15499 	/* Just let HBA driver to deactivate port */
15500 	sata_device->satadev_addr.qual = qual;
15501 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
15502 	    (SATA_DIP(sata_hba_inst), sata_device);
15503 
15504 	/*
15505 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15506 	 * without the hint
15507 	 */
15508 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15509 	    SE_NO_HINT);
15510 
15511 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15512 	sata_update_port_info(sata_hba_inst, sata_device);
15513 	if (qual == SATA_ADDR_CPORT) {
15514 		if (rval != SATA_SUCCESS) {
15515 			/*
15516 			 * Port deactivation failure - do not change port state
15517 			 * unless the state returned by HBA indicates a port
15518 			 * failure.
15519 			 */
15520 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15521 				SATA_CPORT_STATE(sata_hba_inst, cport) =
15522 				    SATA_PSTATE_FAILED;
15523 			}
15524 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15525 			    "sata_hba_ioctl: port deactivate: "
15526 			    "cannot deactivate SATA port %d", cport));
15527 			rv = EIO;
15528 		} else {
15529 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
15530 		}
15531 	} else {
15532 		mutex_enter(&pmportinfo->pmport_mutex);
15533 		if (rval != SATA_SUCCESS) {
15534 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15535 				SATA_PMPORT_STATE(sata_hba_inst, cport,
15536 				    pmport) = SATA_PSTATE_FAILED;
15537 			}
15538 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15539 			    "sata_hba_ioctl: port deactivate: "
15540 			    "cannot deactivate SATA port %d:%d",
15541 			    cport, pmport));
15542 			rv = EIO;
15543 		} else {
15544 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
15545 		}
15546 		mutex_exit(&pmportinfo->pmport_mutex);
15547 	}
15548 
15549 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15550 
15551 	return (rv);
15552 }
15553 
15554 /*
15555  * Process ioctl port activate request.
15556  *
15557  * NOTE: Port multiplier is supported now.
15558  */
15559 static int
15560 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst,
15561     sata_device_t *sata_device)
15562 {
15563 	int cport, pmport, qual;
15564 	sata_cport_info_t *cportinfo;
15565 	sata_pmport_info_t *pmportinfo = NULL;
15566 	boolean_t dev_existed = B_TRUE;
15567 
15568 	/* Sanity check */
15569 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
15570 		return (ENOTSUP);
15571 
15572 	cport = sata_device->satadev_addr.cport;
15573 	pmport = sata_device->satadev_addr.pmport;
15574 	qual = sata_device->satadev_addr.qual;
15575 
15576 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
15577 
15578 	/*
15579 	 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL())
15580 	 * is a device. But what we are dealing with is port/pmport.
15581 	 */
15582 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
15583 	if (qual == SATA_ADDR_DCPORT)
15584 		sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT;
15585 	else
15586 		sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT;
15587 
15588 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15589 	if (qual == SATA_ADDR_PMPORT) {
15590 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
15591 		if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN ||
15592 		    pmportinfo->pmport_dev_type == SATA_DTYPE_NONE)
15593 			dev_existed = B_FALSE;
15594 	} else { /* cport */
15595 		if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN ||
15596 		    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
15597 			dev_existed = B_FALSE;
15598 	}
15599 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15600 
15601 	/* Just let HBA driver to activate port, if necessary */
15602 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
15603 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15604 		/*
15605 		 * Port activation failure - do not change port state unless
15606 		 * the state returned by HBA indicates a port failure.
15607 		 */
15608 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15609 		    cport)->cport_mutex);
15610 		sata_update_port_info(sata_hba_inst, sata_device);
15611 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
15612 			if (qual == SATA_ADDR_PMPORT) {
15613 				mutex_enter(&pmportinfo->pmport_mutex);
15614 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
15615 				mutex_exit(&pmportinfo->pmport_mutex);
15616 			} else
15617 				cportinfo->cport_state = SATA_PSTATE_FAILED;
15618 
15619 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15620 			    cport)->cport_mutex);
15621 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15622 			    "sata_hba_ioctl: port activate: cannot activate "
15623 			    "SATA port %d:%d", cport, pmport));
15624 			return (EIO);
15625 		}
15626 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15627 	}
15628 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15629 	if (qual == SATA_ADDR_PMPORT) {
15630 		mutex_enter(&pmportinfo->pmport_mutex);
15631 		pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN;
15632 		mutex_exit(&pmportinfo->pmport_mutex);
15633 	} else
15634 		cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN;
15635 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15636 
15637 	/*
15638 	 * Re-probe port to find its current state and possibly attached device.
15639 	 * Port re-probing may change the cportinfo device type if device is
15640 	 * found attached.
15641 	 * If port probing failed, the device type would be set to
15642 	 * SATA_DTYPE_NONE.
15643 	 */
15644 	(void) sata_reprobe_port(sata_hba_inst, sata_device,
15645 	    SATA_DEV_IDENTIFY_RETRY);
15646 
15647 	/*
15648 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15649 	 * without the hint.
15650 	 */
15651 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
15652 	    SE_NO_HINT);
15653 
15654 	if (dev_existed == B_FALSE) {
15655 		if (qual == SATA_ADDR_PMPORT &&
15656 		    pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
15657 			/*
15658 			 * That's the transition from the "inactive" port state
15659 			 * or the active port without a device attached to the
15660 			 * active port state with a device attached.
15661 			 */
15662 			sata_log(sata_hba_inst, CE_WARN,
15663 			    "SATA device detected at port %d:%d",
15664 			    cport, pmport);
15665 		} else if (qual == SATA_ADDR_CPORT &&
15666 		    cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
15667 			/*
15668 			 * That's the transition from the "inactive" port state
15669 			 * or the active port without a device attached to the
15670 			 * active port state with a device attached.
15671 			 */
15672 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
15673 				sata_log(sata_hba_inst, CE_WARN,
15674 				    "SATA device detected at port %d", cport);
15675 			} else {
15676 				sata_log(sata_hba_inst, CE_WARN,
15677 				    "SATA port multiplier detected at port %d",
15678 				    cport);
15679 			}
15680 		}
15681 	}
15682 	return (0);
15683 }
15684 
15685 
15686 
15687 /*
15688  * Process ioctl reset port request.
15689  *
15690  * NOTE: Port-Multiplier is supported.
15691  */
15692 static int
15693 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst,
15694     sata_device_t *sata_device)
15695 {
15696 	int cport, pmport, qual;
15697 	int rv = 0;
15698 
15699 	cport = sata_device->satadev_addr.cport;
15700 	pmport = sata_device->satadev_addr.pmport;
15701 	qual = sata_device->satadev_addr.qual;
15702 
15703 	/*
15704 	 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL())
15705 	 * is a device. But what we are dealing with is port/pmport.
15706 	 */
15707 	if (qual == SATA_ADDR_DCPORT)
15708 		sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT;
15709 	else
15710 		sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT;
15711 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
15712 
15713 	/* Sanity check */
15714 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15715 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15716 		    "sata_hba_ioctl: sata_hba_tran missing required "
15717 		    "function sata_tran_reset_dport"));
15718 		return (ENOTSUP);
15719 	}
15720 
15721 	/* Ask HBA to reset port */
15722 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
15723 	    sata_device) != SATA_SUCCESS) {
15724 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15725 		    "sata_hba_ioctl: reset port: failed %d:%d",
15726 		    cport, pmport));
15727 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15728 		    cport_mutex);
15729 		sata_update_port_info(sata_hba_inst, sata_device);
15730 		if (qual == SATA_ADDR_CPORT)
15731 			SATA_CPORT_STATE(sata_hba_inst, cport) =
15732 			    SATA_PSTATE_FAILED;
15733 		else {
15734 			mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport,
15735 			    pmport));
15736 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
15737 			    SATA_PSTATE_FAILED;
15738 			mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport,
15739 			    pmport));
15740 		}
15741 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15742 		    cport_mutex);
15743 		rv = EIO;
15744 	}
15745 
15746 	return (rv);
15747 }
15748 
15749 /*
15750  * Process ioctl reset device request.
15751  *
15752  * NOTE: Port multiplier is supported.
15753  */
15754 static int
15755 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst,
15756     sata_device_t *sata_device)
15757 {
15758 	sata_drive_info_t *sdinfo = NULL;
15759 	sata_pmult_info_t *pmultinfo = NULL;
15760 	int cport, pmport;
15761 	int rv = 0;
15762 
15763 	/* Sanity check */
15764 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15765 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15766 		    "sata_hba_ioctl: sata_hba_tran missing required "
15767 		    "function sata_tran_reset_dport"));
15768 		return (ENOTSUP);
15769 	}
15770 
15771 	cport = sata_device->satadev_addr.cport;
15772 	pmport = sata_device->satadev_addr.pmport;
15773 
15774 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15775 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
15776 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
15777 		    SATA_DTYPE_PMULT)
15778 			pmultinfo = SATA_CPORT_INFO(sata_hba_inst, cport)->
15779 			    cport_devp.cport_sata_pmult;
15780 		else
15781 			sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
15782 			    sata_device->satadev_addr.cport);
15783 	} else { /* port multiplier */
15784 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
15785 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
15786 		    sata_device->satadev_addr.cport,
15787 		    sata_device->satadev_addr.pmport);
15788 	}
15789 	if (sdinfo == NULL && pmultinfo == NULL) {
15790 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15791 		return (EINVAL);
15792 	}
15793 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15794 
15795 	/* Ask HBA to reset device */
15796 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
15797 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15798 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15799 		    "sata_hba_ioctl: reset device: failed at port %d:%d",
15800 		    cport, pmport));
15801 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15802 		    cport_mutex);
15803 		sata_update_port_info(sata_hba_inst, sata_device);
15804 		/*
15805 		 * Device info structure remains attached. Another device reset
15806 		 * or port disconnect/connect and re-probing is
15807 		 * needed to change it's state
15808 		 */
15809 		if (sdinfo != NULL) {
15810 			sdinfo->satadrv_state &= ~SATA_STATE_READY;
15811 			sdinfo->satadrv_state |= SATA_DSTATE_FAILED;
15812 		} else if (pmultinfo != NULL) {
15813 			pmultinfo->pmult_state &= ~SATA_STATE_READY;
15814 			pmultinfo->pmult_state |= SATA_DSTATE_FAILED;
15815 		}
15816 
15817 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
15818 		rv = EIO;
15819 	}
15820 	/*
15821 	 * If attached device was a port multiplier, some extra processing
15822 	 * may be needed to bring it back. SATA specification requies a
15823 	 * mandatory software reset on host port to reliably enumerate a port
15824 	 * multiplier, the HBA driver should handle that after reset
15825 	 * operation.
15826 	 */
15827 	return (rv);
15828 }
15829 
15830 
15831 /*
15832  * Process ioctl reset all request.
15833  */
15834 static int
15835 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst)
15836 {
15837 	sata_device_t sata_device;
15838 	int rv = 0;
15839 	int tcport;
15840 
15841 	sata_device.satadev_rev = SATA_DEVICE_REV;
15842 
15843 	/*
15844 	 * There is no protection here for configured devices.
15845 	 */
15846 	/* Sanity check */
15847 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
15848 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15849 		    "sata_hba_ioctl: sata_hba_tran missing required "
15850 		    "function sata_tran_reset_dport"));
15851 		return (ENOTSUP);
15852 	}
15853 
15854 	/*
15855 	 * Need to lock all ports, not just one.
15856 	 * If any port is locked by event processing, fail the whole operation.
15857 	 * One port is already locked, but for simplicity lock it again.
15858 	 */
15859 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
15860 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15861 		    cport_mutex);
15862 		if (((SATA_CPORT_INFO(sata_hba_inst, tcport)->
15863 		    cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) {
15864 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15865 			    cport_mutex);
15866 			rv = EBUSY;
15867 			break;
15868 		} else {
15869 			/*
15870 			 * It is enough to lock cport in command-based
15871 			 * switching mode.
15872 			 */
15873 			SATA_CPORT_INFO(sata_hba_inst, tcport)->
15874 			    cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
15875 		}
15876 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15877 		    cport_mutex);
15878 	}
15879 
15880 	if (rv == 0) {
15881 		/*
15882 		 * All cports were successfully locked.
15883 		 * Reset main SATA controller.
15884 		 * Set the device address to port 0, to have a valid device
15885 		 * address.
15886 		 */
15887 		sata_device.satadev_addr.qual = SATA_ADDR_CNTRL;
15888 		sata_device.satadev_addr.cport = 0;
15889 		sata_device.satadev_addr.pmport = 0;
15890 
15891 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
15892 		    (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) {
15893 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15894 			    "sata_hba_ioctl: reset controller failed"));
15895 			return (EIO);
15896 		}
15897 	}
15898 	/*
15899 	 * Unlock all ports
15900 	 */
15901 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
15902 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15903 		    cport_mutex);
15904 		SATA_CPORT_INFO(sata_hba_inst, tcport)->
15905 		    cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
15906 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
15907 		    cport_mutex);
15908 	}
15909 
15910 	/*
15911 	 * This operation returns EFAULT if either reset
15912 	 * controller failed or a re-probing of any port failed.
15913 	 */
15914 	return (rv);
15915 }
15916 
15917 
15918 /*
15919  * Process ioctl port self test request.
15920  *
15921  * NOTE: Port multiplier code is not completed nor tested.
15922  */
15923 static int
15924 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst,
15925     sata_device_t *sata_device)
15926 {
15927 	int cport, pmport, qual;
15928 	int rv = 0;
15929 
15930 	/* Sanity check */
15931 	if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL)
15932 		return (ENOTSUP);
15933 
15934 	cport = sata_device->satadev_addr.cport;
15935 	pmport = sata_device->satadev_addr.pmport;
15936 	qual = sata_device->satadev_addr.qual;
15937 
15938 	/*
15939 	 * There is no protection here for a configured
15940 	 * device attached to this port.
15941 	 */
15942 
15943 	if ((*SATA_SELFTEST_FUNC(sata_hba_inst))
15944 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
15945 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15946 		    "sata_hba_ioctl: port selftest: "
15947 		    "failed port %d:%d", cport, pmport));
15948 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15949 		    cport_mutex);
15950 		sata_update_port_info(sata_hba_inst, sata_device);
15951 		if (qual == SATA_ADDR_CPORT)
15952 			SATA_CPORT_STATE(sata_hba_inst, cport) =
15953 			    SATA_PSTATE_FAILED;
15954 		else { /* port multiplier device port */
15955 			mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst,
15956 			    cport, pmport));
15957 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
15958 			    SATA_PSTATE_FAILED;
15959 			mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst,
15960 			    cport, pmport));
15961 		}
15962 
15963 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
15964 		    cport_mutex);
15965 		return (EIO);
15966 	}
15967 	/*
15968 	 * Beacuse the port was reset in the course of testing, it should be
15969 	 * re-probed and attached device state should be restored. At this
15970 	 * point the port state is unknown - it's state is HBA-specific.
15971 	 * Force port re-probing to get it into a known state.
15972 	 */
15973 	if (sata_reprobe_port(sata_hba_inst, sata_device,
15974 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
15975 		rv = EIO;
15976 	return (rv);
15977 }
15978 
15979 
15980 /*
15981  * sata_cfgadm_state:
15982  * Use the sata port state and state of the target node to figure out
15983  * the cfgadm_state.
15984  *
15985  * The port argument is a value with encoded cport,
15986  * pmport and address qualifier, in the same manner as a scsi target number.
15987  * SCSI_TO_SATA_CPORT macro extracts cport number,
15988  * SCSI_TO_SATA_PMPORT extracts pmport number and
15989  * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag.
15990  *
15991  * Port multiplier is supported.
15992  */
15993 
15994 static void
15995 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port,
15996     devctl_ap_state_t *ap_state)
15997 {
15998 	uint8_t		cport, pmport, qual;
15999 	uint32_t	port_state, pmult_state;
16000 	uint32_t	dev_type;
16001 	sata_drive_info_t *sdinfo;
16002 
16003 	cport = SCSI_TO_SATA_CPORT(port);
16004 	pmport = SCSI_TO_SATA_PMPORT(port);
16005 	qual = SCSI_TO_SATA_ADDR_QUAL(port);
16006 
16007 	/* Check cport state */
16008 	port_state = SATA_CPORT_STATE(sata_hba_inst, cport);
16009 	if (port_state & SATA_PSTATE_SHUTDOWN ||
16010 	    port_state & SATA_PSTATE_FAILED) {
16011 		ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
16012 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16013 		if (port_state & SATA_PSTATE_FAILED)
16014 			ap_state->ap_condition = AP_COND_FAILED;
16015 		else
16016 			ap_state->ap_condition = AP_COND_UNKNOWN;
16017 
16018 		return;
16019 	}
16020 
16021 	/* cport state is okay. Now check pmport state */
16022 	if (qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) {
16023 		/* Sanity check */
16024 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
16025 		    SATA_DTYPE_PMULT || SATA_PMPORT_INFO(sata_hba_inst,
16026 		    cport, pmport) == NULL)
16027 			return;
16028 		port_state = SATA_PMPORT_STATE(sata_hba_inst, cport, pmport);
16029 		if (port_state & SATA_PSTATE_SHUTDOWN ||
16030 		    port_state & SATA_PSTATE_FAILED) {
16031 			ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
16032 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16033 			if (port_state & SATA_PSTATE_FAILED)
16034 				ap_state->ap_condition = AP_COND_FAILED;
16035 			else
16036 				ap_state->ap_condition = AP_COND_UNKNOWN;
16037 
16038 			return;
16039 		}
16040 	}
16041 
16042 	/* Port is enabled and ready */
16043 	if (qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_CPORT)
16044 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, cport);
16045 	else
16046 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, pmport);
16047 
16048 	switch (dev_type) {
16049 	case SATA_DTYPE_NONE:
16050 	{
16051 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16052 		ap_state->ap_condition = AP_COND_OK;
16053 		/* No device attached */
16054 		ap_state->ap_rstate = AP_RSTATE_EMPTY;
16055 		break;
16056 	}
16057 	case SATA_DTYPE_PMULT:
16058 	{
16059 		/* Need to check port multiplier state */
16060 		ASSERT(qual == SATA_ADDR_DCPORT);
16061 		pmult_state = SATA_PMULT_INFO(sata_hba_inst, cport)->
16062 		    pmult_state;
16063 		if (pmult_state & (SATA_PSTATE_SHUTDOWN|SATA_PSTATE_FAILED)) {
16064 			ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
16065 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16066 			if (pmult_state & SATA_PSTATE_FAILED)
16067 				ap_state->ap_condition = AP_COND_FAILED;
16068 			else
16069 				ap_state->ap_condition = AP_COND_UNKNOWN;
16070 
16071 			return;
16072 		}
16073 
16074 		/* Port multiplier is not configurable */
16075 		ap_state->ap_ostate = AP_OSTATE_CONFIGURED;
16076 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
16077 		ap_state->ap_condition = AP_COND_OK;
16078 		break;
16079 	}
16080 
16081 	case SATA_DTYPE_ATADISK:
16082 	case SATA_DTYPE_ATAPICD:
16083 	case SATA_DTYPE_ATAPITAPE:
16084 	case SATA_DTYPE_ATAPIDISK:
16085 	{
16086 		dev_info_t *tdip = NULL;
16087 		dev_info_t *dip = NULL;
16088 		int circ;
16089 
16090 		dip = SATA_DIP(sata_hba_inst);
16091 		tdip = sata_get_target_dip(dip, cport, pmport);
16092 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
16093 		if (tdip != NULL) {
16094 			ndi_devi_enter(dip, &circ);
16095 			mutex_enter(&(DEVI(tdip)->devi_lock));
16096 			if (DEVI_IS_DEVICE_REMOVED(tdip)) {
16097 				/*
16098 				 * There could be the case where previously
16099 				 * configured and opened device was removed
16100 				 * and unknown device was plugged.
16101 				 * In such case we want to show a device, and
16102 				 * its configured or unconfigured state but
16103 				 * indicate unusable condition untill the
16104 				 * old target node is released and removed.
16105 				 */
16106 				ap_state->ap_condition = AP_COND_UNUSABLE;
16107 			} else {
16108 				mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst,
16109 				    cport));
16110 				sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16111 				    cport);
16112 				if (sdinfo != NULL) {
16113 					if ((sdinfo->satadrv_state &
16114 					    SATA_DSTATE_FAILED) != 0)
16115 						ap_state->ap_condition =
16116 						    AP_COND_FAILED;
16117 					else
16118 						ap_state->ap_condition =
16119 						    AP_COND_OK;
16120 				} else {
16121 					ap_state->ap_condition =
16122 					    AP_COND_UNKNOWN;
16123 				}
16124 				mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst,
16125 				    cport));
16126 			}
16127 			if ((DEVI_IS_DEVICE_OFFLINE(tdip)) ||
16128 			    (DEVI_IS_DEVICE_DOWN(tdip))) {
16129 				ap_state->ap_ostate =
16130 				    AP_OSTATE_UNCONFIGURED;
16131 			} else {
16132 				ap_state->ap_ostate =
16133 				    AP_OSTATE_CONFIGURED;
16134 			}
16135 			mutex_exit(&(DEVI(tdip)->devi_lock));
16136 			ndi_devi_exit(dip, circ);
16137 		} else {
16138 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16139 			ap_state->ap_condition = AP_COND_UNKNOWN;
16140 		}
16141 		break;
16142 	}
16143 	case SATA_DTYPE_ATAPIPROC:
16144 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
16145 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16146 		ap_state->ap_condition = AP_COND_OK;
16147 		break;
16148 	default:
16149 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
16150 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
16151 		ap_state->ap_condition = AP_COND_UNKNOWN;
16152 		/*
16153 		 * This is actually internal error condition (non fatal),
16154 		 * because we have already checked all defined device types.
16155 		 */
16156 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16157 		    "sata_cfgadm_state: Internal error: "
16158 		    "unknown device type"));
16159 		break;
16160 	}
16161 }
16162 
16163 
16164 /*
16165  * Process ioctl get device path request.
16166  *
16167  * NOTE: Port multiplier has no target dip. Devices connected to port
16168  * multiplier have target node attached to the HBA node. The only difference
16169  * between them and the directly-attached device node is a target address.
16170  */
16171 static int
16172 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst,
16173     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16174 {
16175 	char path[MAXPATHLEN];
16176 	uint32_t size;
16177 	dev_info_t *tdip;
16178 
16179 	(void) strcpy(path, "/devices");
16180 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
16181 	    &sata_device->satadev_addr)) == NULL) {
16182 		/*
16183 		 * No such device. If this is a request for a size, do not
16184 		 * return EINVAL for non-existing target, because cfgadm
16185 		 * will then indicate a meaningless ioctl failure.
16186 		 * If this is a request for a path, indicate invalid
16187 		 * argument.
16188 		 */
16189 		if (ioc->get_size == 0)
16190 			return (EINVAL);
16191 	} else {
16192 		(void) ddi_pathname(tdip, path + strlen(path));
16193 	}
16194 	size = strlen(path) + 1;
16195 
16196 	if (ioc->get_size != 0) {
16197 		if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz,
16198 		    mode) != 0)
16199 			return (EFAULT);
16200 	} else {
16201 		if (ioc->bufsiz != size)
16202 			return (EINVAL);
16203 
16204 		else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz,
16205 		    mode) != 0)
16206 			return (EFAULT);
16207 	}
16208 	return (0);
16209 }
16210 
16211 /*
16212  * Process ioctl get attachment point type request.
16213  *
16214  * NOTE: Port multiplier is supported.
16215  */
16216 static	int
16217 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst,
16218     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16219 {
16220 	uint32_t	type_len;
16221 	const char	*ap_type;
16222 	int		dev_type;
16223 
16224 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16225 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst,
16226 		    sata_device->satadev_addr.cport);
16227 	else /* pmport */
16228 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst,
16229 		    sata_device->satadev_addr.cport,
16230 		    sata_device->satadev_addr.pmport);
16231 
16232 	switch (dev_type) {
16233 	case SATA_DTYPE_NONE:
16234 		ap_type = "port";
16235 		break;
16236 
16237 	case SATA_DTYPE_ATADISK:
16238 	case SATA_DTYPE_ATAPIDISK:
16239 		ap_type = "disk";
16240 		break;
16241 
16242 	case SATA_DTYPE_ATAPICD:
16243 		ap_type = "cd/dvd";
16244 		break;
16245 
16246 	case SATA_DTYPE_ATAPITAPE:
16247 		ap_type = "tape";
16248 		break;
16249 
16250 	case SATA_DTYPE_ATAPIPROC:
16251 		ap_type = "processor";
16252 		break;
16253 
16254 	case SATA_DTYPE_PMULT:
16255 		ap_type = "sata-pmult";
16256 		break;
16257 
16258 	case SATA_DTYPE_UNKNOWN:
16259 		ap_type = "unknown";
16260 		break;
16261 
16262 	default:
16263 		ap_type = "unsupported";
16264 		break;
16265 
16266 	} /* end of dev_type switch */
16267 
16268 	type_len = strlen(ap_type) + 1;
16269 
16270 	if (ioc->get_size) {
16271 		if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz,
16272 		    mode) != 0)
16273 			return (EFAULT);
16274 	} else {
16275 		if (ioc->bufsiz != type_len)
16276 			return (EINVAL);
16277 
16278 		if (ddi_copyout((void *)ap_type, ioc->buf,
16279 		    ioc->bufsiz, mode) != 0)
16280 			return (EFAULT);
16281 	}
16282 	return (0);
16283 
16284 }
16285 
16286 /*
16287  * Process ioctl get device model info request.
16288  * This operation should return to cfgadm the device model
16289  * information string
16290  *
16291  * NOTE: Port multiplier is supported.
16292  */
16293 static	int
16294 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst,
16295     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16296 {
16297 	sata_drive_info_t *sdinfo;
16298 	uint32_t info_len;
16299 	char ap_info[SATA_ID_MODEL_LEN + 1];
16300 
16301 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16302 	    sata_device->satadev_addr.cport)->cport_mutex);
16303 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16304 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16305 		    sata_device->satadev_addr.cport);
16306 	else /* port multiplier */
16307 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16308 		    sata_device->satadev_addr.cport,
16309 		    sata_device->satadev_addr.pmport);
16310 	if (sdinfo == NULL) {
16311 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16312 		    sata_device->satadev_addr.cport)->cport_mutex);
16313 		return (EINVAL);
16314 	}
16315 
16316 #ifdef	_LITTLE_ENDIAN
16317 	swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
16318 #else	/* _LITTLE_ENDIAN */
16319 	bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
16320 #endif	/* _LITTLE_ENDIAN */
16321 
16322 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16323 	    sata_device->satadev_addr.cport)->cport_mutex);
16324 
16325 	ap_info[SATA_ID_MODEL_LEN] = '\0';
16326 
16327 	info_len = strlen(ap_info) + 1;
16328 
16329 	if (ioc->get_size) {
16330 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16331 		    mode) != 0)
16332 			return (EFAULT);
16333 	} else {
16334 		if (ioc->bufsiz < info_len)
16335 			return (EINVAL);
16336 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16337 		    mode) != 0)
16338 			return (EFAULT);
16339 	}
16340 	return (0);
16341 }
16342 
16343 
16344 /*
16345  * Process ioctl get device firmware revision info request.
16346  * This operation should return to cfgadm the device firmware revision
16347  * information string
16348  *
16349  * Port multiplier is supported.
16350  */
16351 static	int
16352 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst,
16353     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16354 {
16355 	sata_drive_info_t *sdinfo;
16356 	uint32_t info_len;
16357 	char ap_info[SATA_ID_FW_LEN + 1];
16358 
16359 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16360 	    sata_device->satadev_addr.cport)->cport_mutex);
16361 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16362 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16363 		    sata_device->satadev_addr.cport);
16364 	else /* port multiplier */
16365 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16366 		    sata_device->satadev_addr.cport,
16367 		    sata_device->satadev_addr.pmport);
16368 	if (sdinfo == NULL) {
16369 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16370 		    sata_device->satadev_addr.cport)->cport_mutex);
16371 		return (EINVAL);
16372 	}
16373 
16374 #ifdef	_LITTLE_ENDIAN
16375 	swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
16376 #else	/* _LITTLE_ENDIAN */
16377 	bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
16378 #endif	/* _LITTLE_ENDIAN */
16379 
16380 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16381 	    sata_device->satadev_addr.cport)->cport_mutex);
16382 
16383 	ap_info[SATA_ID_FW_LEN] = '\0';
16384 
16385 	info_len = strlen(ap_info) + 1;
16386 
16387 	if (ioc->get_size) {
16388 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16389 		    mode) != 0)
16390 			return (EFAULT);
16391 	} else {
16392 		if (ioc->bufsiz < info_len)
16393 			return (EINVAL);
16394 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16395 		    mode) != 0)
16396 			return (EFAULT);
16397 	}
16398 	return (0);
16399 }
16400 
16401 
16402 /*
16403  * Process ioctl get device serial number info request.
16404  * This operation should return to cfgadm the device serial number string.
16405  *
16406  * NOTE: Port multiplier is supported.
16407  */
16408 static	int
16409 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst,
16410     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
16411 {
16412 	sata_drive_info_t *sdinfo;
16413 	uint32_t info_len;
16414 	char ap_info[SATA_ID_SERIAL_LEN + 1];
16415 
16416 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
16417 	    sata_device->satadev_addr.cport)->cport_mutex);
16418 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
16419 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
16420 		    sata_device->satadev_addr.cport);
16421 	else /* port multiplier */
16422 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
16423 		    sata_device->satadev_addr.cport,
16424 		    sata_device->satadev_addr.pmport);
16425 	if (sdinfo == NULL) {
16426 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16427 		    sata_device->satadev_addr.cport)->cport_mutex);
16428 		return (EINVAL);
16429 	}
16430 
16431 #ifdef	_LITTLE_ENDIAN
16432 	swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
16433 #else	/* _LITTLE_ENDIAN */
16434 	bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
16435 #endif	/* _LITTLE_ENDIAN */
16436 
16437 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
16438 	    sata_device->satadev_addr.cport)->cport_mutex);
16439 
16440 	ap_info[SATA_ID_SERIAL_LEN] = '\0';
16441 
16442 	info_len = strlen(ap_info) + 1;
16443 
16444 	if (ioc->get_size) {
16445 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
16446 		    mode) != 0)
16447 			return (EFAULT);
16448 	} else {
16449 		if (ioc->bufsiz < info_len)
16450 			return (EINVAL);
16451 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
16452 		    mode) != 0)
16453 			return (EFAULT);
16454 	}
16455 	return (0);
16456 }
16457 
16458 
16459 /*
16460  * Preset scsi extended sense data (to NO SENSE)
16461  * First 18 bytes of the sense data are preset to current valid sense
16462  * with a key NO SENSE data.
16463  *
16464  * Returns void
16465  */
16466 static void
16467 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense)
16468 {
16469 	sense->es_valid = 1;		/* Valid sense */
16470 	sense->es_class = CLASS_EXTENDED_SENSE;	/* 0x70 - current err */
16471 	sense->es_key = KEY_NO_SENSE;
16472 	sense->es_info_1 = 0;
16473 	sense->es_info_2 = 0;
16474 	sense->es_info_3 = 0;
16475 	sense->es_info_4 = 0;
16476 	sense->es_add_len = 10;	/* Additional length - replace with a def */
16477 	sense->es_cmd_info[0] = 0;
16478 	sense->es_cmd_info[1] = 0;
16479 	sense->es_cmd_info[2] = 0;
16480 	sense->es_cmd_info[3] = 0;
16481 	sense->es_add_code = 0;
16482 	sense->es_qual_code = 0;
16483 }
16484 
16485 /*
16486  * Register a legacy cmdk-style devid for the target (disk) device.
16487  *
16488  * Note: This function is called only when the HBA devinfo node has the
16489  * property "use-cmdk-devid-format" set. This property indicates that
16490  * devid compatible with old cmdk (target) driver is to be generated
16491  * for any target device attached to this controller. This will take
16492  * precedence over the devid generated by sd (target) driver.
16493  * This function is derived from cmdk_devid_setup() function in cmdk.c.
16494  */
16495 static void
16496 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo)
16497 {
16498 	char	*hwid;
16499 	int	modlen;
16500 	int	serlen;
16501 	int	rval;
16502 	ddi_devid_t	devid;
16503 
16504 	/*
16505 	 * device ID is a concatanation of model number, "=", serial number.
16506 	 */
16507 	hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP);
16508 	bcopy(&sdinfo->satadrv_id.ai_model, hwid,
16509 	    sizeof (sdinfo->satadrv_id.ai_model));
16510 	swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model));
16511 	modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model));
16512 	if (modlen == 0)
16513 		goto err;
16514 	hwid[modlen++] = '=';
16515 	bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen],
16516 	    sizeof (sdinfo->satadrv_id.ai_drvser));
16517 	swab(&hwid[modlen], &hwid[modlen],
16518 	    sizeof (sdinfo->satadrv_id.ai_drvser));
16519 	serlen = sata_check_modser(&hwid[modlen],
16520 	    sizeof (sdinfo->satadrv_id.ai_drvser));
16521 	if (serlen == 0)
16522 		goto err;
16523 	hwid[modlen + serlen] = 0; /* terminate the hwid string */
16524 
16525 	/* initialize/register devid */
16526 	if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL,
16527 	    (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS) {
16528 		rval = ddi_devid_register(dip, devid);
16529 		/*
16530 		 * Free up the allocated devid buffer.
16531 		 * NOTE: This doesn't mean unregistering devid.
16532 		 */
16533 		ddi_devid_free(devid);
16534 	}
16535 
16536 	if (rval != DDI_SUCCESS)
16537 		cmn_err(CE_WARN, "sata: failed to create devid for the disk"
16538 		    " on port %d", sdinfo->satadrv_addr.cport);
16539 err:
16540 	kmem_free(hwid, LEGACY_HWID_LEN);
16541 }
16542 
16543 /*
16544  * valid model/serial string must contain a non-zero non-space characters.
16545  * trim trailing spaces/NULLs.
16546  */
16547 static int
16548 sata_check_modser(char *buf, int buf_len)
16549 {
16550 	boolean_t ret;
16551 	char *s;
16552 	int i;
16553 	int tb;
16554 	char ch;
16555 
16556 	ret = B_FALSE;
16557 	s = buf;
16558 	for (i = 0; i < buf_len; i++) {
16559 		ch = *s++;
16560 		if (ch != ' ' && ch != '\0')
16561 			tb = i + 1;
16562 		if (ch != ' ' && ch != '\0' && ch != '0')
16563 			ret = B_TRUE;
16564 	}
16565 
16566 	if (ret == B_FALSE)
16567 		return (0); /* invalid string */
16568 
16569 	return (tb); /* return length */
16570 }
16571 
16572 /*
16573  * sata_set_drive_features function compares current device features setting
16574  * with the saved device features settings and, if there is a difference,
16575  * it restores device features setting to the previously saved state.
16576  * It also arbitrarily tries to select the highest supported DMA mode.
16577  * Device Identify or Identify Packet Device data has to be current.
16578  * At the moment read ahead and write cache are considered for all devices.
16579  * For atapi devices, Removable Media Status Notification is set in addition
16580  * to common features.
16581  *
16582  * This function cannot be called in the interrupt context (it may sleep).
16583  *
16584  * The input argument sdinfo should point to the drive info structure
16585  * to be updated after features are set. Note, that only
16586  * device (packet) identify data is updated, not the flags indicating the
16587  * supported features.
16588  *
16589  * Returns SATA_SUCCESS if successful or there was nothing to do.
16590  * Device Identify data in the drive info structure pointed to by the sdinfo
16591  * arguments is updated even when no features were set or changed.
16592  *
16593  * Returns SATA_FAILURE if device features could not be set or DMA mode
16594  * for a disk cannot be set and device identify data cannot be fetched.
16595  *
16596  * Returns SATA_RETRY if device features could not be set (other than disk
16597  * DMA mode) but the device identify data was fetched successfully.
16598  *
16599  * Note: This function may fail the port, making it inaccessible.
16600  * In such case the explicit port disconnect/connect or physical device
16601  * detach/attach is required to re-evaluate port state again.
16602  */
16603 
16604 static int
16605 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst,
16606     sata_drive_info_t *sdinfo, int restore)
16607 {
16608 	int rval = SATA_SUCCESS;
16609 	int rval_set;
16610 	sata_drive_info_t new_sdinfo;
16611 	char *finfo = "sata_set_drive_features: cannot";
16612 	char *finfox;
16613 	int cache_op;
16614 
16615 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
16616 	new_sdinfo.satadrv_addr = sdinfo->satadrv_addr;
16617 	new_sdinfo.satadrv_type = sdinfo->satadrv_type;
16618 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
16619 		/*
16620 		 * Cannot get device identification - caller may retry later
16621 		 */
16622 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16623 		    "%s fetch device identify data\n", finfo);
16624 		return (SATA_FAILURE);
16625 	}
16626 	finfox = (restore != 0) ? " restore device features" :
16627 	    " initialize device features\n";
16628 
16629 	switch (sdinfo->satadrv_type) {
16630 	case SATA_DTYPE_ATADISK:
16631 		/* Arbitrarily set UDMA mode */
16632 		if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
16633 		    SATA_SUCCESS) {
16634 			SATA_LOG_D((sata_hba_inst, CE_WARN,
16635 			    "%s set UDMA mode\n", finfo));
16636 			return (SATA_FAILURE);
16637 		}
16638 		break;
16639 	case SATA_DTYPE_ATAPICD:
16640 	case SATA_DTYPE_ATAPITAPE:
16641 	case SATA_DTYPE_ATAPIDISK:
16642 		/*  Set Removable Media Status Notification, if necessary */
16643 		if (SATA_RM_NOTIFIC_SUPPORTED(new_sdinfo.satadrv_id) &&
16644 		    restore != 0) {
16645 			if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) &&
16646 			    (!SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id)))||
16647 			    ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) &&
16648 			    SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id))) {
16649 				/* Current setting does not match saved one */
16650 				if (sata_set_rmsn(sata_hba_inst, sdinfo,
16651 				    sdinfo->satadrv_settings &
16652 				    SATA_DEV_RMSN) != SATA_SUCCESS)
16653 					rval = SATA_FAILURE;
16654 			}
16655 		}
16656 		/*
16657 		 * We have to set Multiword DMA or UDMA, if it is supported, as
16658 		 * we want to use DMA transfer mode whenever possible.
16659 		 * Some devices require explicit setting of the DMA mode.
16660 		 */
16661 		if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) {
16662 			/* Set highest supported DMA mode */
16663 			if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
16664 			    SATA_SUCCESS) {
16665 				SATA_LOG_D((sata_hba_inst, CE_WARN,
16666 				    "%s set UDMA mode\n", finfo));
16667 				rval = SATA_FAILURE;
16668 			}
16669 		}
16670 		break;
16671 	}
16672 
16673 	if (!SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id) &&
16674 	    !SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
16675 		/*
16676 		 * neither READ AHEAD nor WRITE CACHE is supported
16677 		 * - do nothing
16678 		 */
16679 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16680 		    "settable features not supported\n", NULL);
16681 		goto update_sdinfo;
16682 	}
16683 
16684 	if ((SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id) &&
16685 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) &&
16686 	    (SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id) &&
16687 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
16688 		/*
16689 		 * both READ AHEAD and WRITE CACHE are enabled
16690 		 * - Nothing to do
16691 		 */
16692 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16693 		    "no device features to set\n", NULL);
16694 		goto update_sdinfo;
16695 	}
16696 
16697 	cache_op = 0;
16698 
16699 	if (SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id)) {
16700 		if ((sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
16701 		    !SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
16702 			/* Enable read ahead / read cache */
16703 			cache_op = SATAC_SF_ENABLE_READ_AHEAD;
16704 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16705 			    "enabling read cache\n", NULL);
16706 		} else if (!(sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
16707 		    SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
16708 			/* Disable read ahead  / read cache */
16709 			cache_op = SATAC_SF_DISABLE_READ_AHEAD;
16710 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16711 			    "disabling read cache\n", NULL);
16712 		}
16713 
16714 		if (cache_op != 0) {
16715 			/* Try to set read cache mode */
16716 			rval_set = sata_set_cache_mode(sata_hba_inst,
16717 			    &new_sdinfo, cache_op);
16718 			if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
16719 				rval = rval_set;
16720 		}
16721 	}
16722 
16723 	cache_op = 0;
16724 
16725 	if (SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
16726 		if ((sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
16727 		    !SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
16728 			/* Enable write cache */
16729 			cache_op = SATAC_SF_ENABLE_WRITE_CACHE;
16730 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16731 			    "enabling write cache\n", NULL);
16732 		} else if (!(sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
16733 		    SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
16734 			/* Disable write cache */
16735 			cache_op = SATAC_SF_DISABLE_WRITE_CACHE;
16736 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
16737 			    "disabling write cache\n", NULL);
16738 		}
16739 
16740 		if (cache_op != 0) {
16741 			/* Try to set write cache mode */
16742 			rval_set = sata_set_cache_mode(sata_hba_inst,
16743 			    &new_sdinfo, cache_op);
16744 			if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
16745 				rval = rval_set;
16746 		}
16747 	}
16748 	if (rval != SATA_SUCCESS)
16749 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16750 		    "%s %s", finfo, finfox));
16751 
16752 update_sdinfo:
16753 	/*
16754 	 * We need to fetch Device Identify data again
16755 	 */
16756 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
16757 		/*
16758 		 * Cannot get device identification - retry later
16759 		 */
16760 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16761 		    "%s re-fetch device identify data\n", finfo));
16762 		rval = SATA_FAILURE;
16763 	}
16764 	/* Copy device sata info. */
16765 	sdinfo->satadrv_id = new_sdinfo.satadrv_id;
16766 
16767 	return (rval);
16768 }
16769 
16770 
16771 /*
16772  *
16773  * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if
16774  * unable to determine.
16775  *
16776  * Cannot be called in an interrupt context.
16777  *
16778  * Called by sata_build_lsense_page_2f()
16779  */
16780 
16781 static int
16782 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst,
16783     sata_drive_info_t *sdinfo)
16784 {
16785 	sata_pkt_t *spkt;
16786 	sata_cmd_t *scmd;
16787 	sata_pkt_txlate_t *spx;
16788 	int rval;
16789 
16790 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16791 	spx->txlt_sata_hba_inst = sata_hba_inst;
16792 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
16793 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16794 	if (spkt == NULL) {
16795 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16796 		return (-1);
16797 	}
16798 	/* address is needed now */
16799 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16800 
16801 
16802 	/* Fill sata_pkt */
16803 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16804 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16805 	/* Synchronous mode, no callback */
16806 	spkt->satapkt_comp = NULL;
16807 	/* Timeout 30s */
16808 	spkt->satapkt_time = sata_default_pkt_time;
16809 
16810 	scmd = &spkt->satapkt_cmd;
16811 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
16812 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
16813 
16814 	/* Set up which registers need to be returned */
16815 	scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE;
16816 	scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE;
16817 
16818 	/* Build SMART_RETURN_STATUS cmd in the sata_pkt */
16819 	scmd->satacmd_addr_type = 0;		/* N/A */
16820 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
16821 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
16822 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
16823 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
16824 	scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS;
16825 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
16826 	scmd->satacmd_cmd_reg = SATAC_SMART;
16827 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16828 	    sdinfo->satadrv_addr.cport)));
16829 
16830 
16831 	/* Send pkt to SATA HBA driver */
16832 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16833 	    SATA_TRAN_ACCEPTED ||
16834 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16835 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16836 		    sdinfo->satadrv_addr.cport)));
16837 		/*
16838 		 * Whoops, no SMART RETURN STATUS
16839 		 */
16840 		rval = -1;
16841 	} else {
16842 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16843 		    sdinfo->satadrv_addr.cport)));
16844 		if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
16845 			rval = -1;
16846 			goto fail;
16847 		}
16848 		if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
16849 			rval = -1;
16850 			goto fail;
16851 		}
16852 		if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) &&
16853 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2))
16854 			rval = 0;
16855 		else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) &&
16856 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4))
16857 			rval = 1;
16858 		else {
16859 			rval = -1;
16860 			goto fail;
16861 		}
16862 	}
16863 fail:
16864 	/* Free allocated resources */
16865 	sata_pkt_free(spx);
16866 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
16867 
16868 	return (rval);
16869 }
16870 
16871 /*
16872  *
16873  * Returns 0 if succeeded, -1 otherwise
16874  *
16875  * Cannot be called in an interrupt context.
16876  *
16877  */
16878 static int
16879 sata_fetch_smart_data(
16880 	sata_hba_inst_t *sata_hba_inst,
16881 	sata_drive_info_t *sdinfo,
16882 	struct smart_data *smart_data)
16883 {
16884 	sata_pkt_t *spkt;
16885 	sata_cmd_t *scmd;
16886 	sata_pkt_txlate_t *spx;
16887 	int rval;
16888 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
16889 
16890 #if ! defined(lint)
16891 	ASSERT(sizeof (struct smart_data) == 512);
16892 #endif
16893 
16894 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
16895 	spx->txlt_sata_hba_inst = sata_hba_inst;
16896 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
16897 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
16898 	if (spkt == NULL) {
16899 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16900 		return (-1);
16901 	}
16902 	/* address is needed now */
16903 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16904 
16905 
16906 	/* Fill sata_pkt */
16907 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
16908 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16909 	/* Synchronous mode, no callback */
16910 	spkt->satapkt_comp = NULL;
16911 	/* Timeout 30s */
16912 	spkt->satapkt_time = sata_default_pkt_time;
16913 
16914 	scmd = &spkt->satapkt_cmd;
16915 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
16916 
16917 	/*
16918 	 * Allocate buffer for SMART data
16919 	 */
16920 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
16921 	    sizeof (struct smart_data));
16922 	if (scmd->satacmd_bp == NULL) {
16923 		sata_pkt_free(spx);
16924 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
16925 		SATA_LOG_D((sata_hba_inst, CE_WARN,
16926 		    "sata_fetch_smart_data: "
16927 		    "cannot allocate buffer"));
16928 		return (-1);
16929 	}
16930 
16931 
16932 	/* Build SMART_READ_DATA cmd in the sata_pkt */
16933 	scmd->satacmd_addr_type = 0;		/* N/A */
16934 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
16935 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
16936 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
16937 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
16938 	scmd->satacmd_features_reg = SATA_SMART_READ_DATA;
16939 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
16940 	scmd->satacmd_cmd_reg = SATAC_SMART;
16941 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
16942 	    sdinfo->satadrv_addr.cport)));
16943 
16944 	/* Send pkt to SATA HBA driver */
16945 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
16946 	    SATA_TRAN_ACCEPTED ||
16947 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
16948 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16949 		    sdinfo->satadrv_addr.cport)));
16950 		/*
16951 		 * Whoops, no SMART DATA available
16952 		 */
16953 		rval = -1;
16954 		goto fail;
16955 	} else {
16956 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
16957 		    sdinfo->satadrv_addr.cport)));
16958 		if (spx->txlt_buf_dma_handle != NULL) {
16959 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
16960 			    DDI_DMA_SYNC_FORKERNEL);
16961 			ASSERT(rval == DDI_SUCCESS);
16962 			if (sata_check_for_dma_error(dip, spx)) {
16963 				ddi_fm_service_impact(dip,
16964 				    DDI_SERVICE_UNAFFECTED);
16965 				rval = -1;
16966 				goto fail;
16967 			}
16968 		}
16969 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data,
16970 		    sizeof (struct smart_data));
16971 	}
16972 
16973 fail:
16974 	/* Free allocated resources */
16975 	sata_free_local_buffer(spx);
16976 	sata_pkt_free(spx);
16977 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
16978 
16979 	return (rval);
16980 }
16981 
16982 /*
16983  * Used by LOG SENSE page 0x10
16984  * Reads (in synchronous mode) the self test log data using Read Log Ext cmd.
16985  * Note: cannot be called in the interrupt context.
16986  *
16987  * return 0 for success, -1 otherwise
16988  *
16989  */
16990 static int
16991 sata_ext_smart_selftest_read_log(
16992 	sata_hba_inst_t *sata_hba_inst,
16993 	sata_drive_info_t *sdinfo,
16994 	struct smart_ext_selftest_log *ext_selftest_log,
16995 	uint16_t block_num)
16996 {
16997 	sata_pkt_txlate_t *spx;
16998 	sata_pkt_t *spkt;
16999 	sata_cmd_t *scmd;
17000 	int rval;
17001 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
17002 
17003 #if ! defined(lint)
17004 	ASSERT(sizeof (struct smart_ext_selftest_log) == 512);
17005 #endif
17006 
17007 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17008 	spx->txlt_sata_hba_inst = sata_hba_inst;
17009 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
17010 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17011 	if (spkt == NULL) {
17012 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17013 		return (-1);
17014 	}
17015 	/* address is needed now */
17016 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17017 
17018 
17019 	/* Fill sata_pkt */
17020 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17021 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17022 	/* Synchronous mode, no callback */
17023 	spkt->satapkt_comp = NULL;
17024 	/* Timeout 30s */
17025 	spkt->satapkt_time = sata_default_pkt_time;
17026 
17027 	scmd = &spkt->satapkt_cmd;
17028 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17029 
17030 	/*
17031 	 * Allocate buffer for SMART extended self-test log
17032 	 */
17033 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
17034 	    sizeof (struct smart_ext_selftest_log));
17035 	if (scmd->satacmd_bp == NULL) {
17036 		sata_pkt_free(spx);
17037 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17038 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17039 		    "sata_ext_smart_selftest_log: "
17040 		    "cannot allocate buffer"));
17041 		return (-1);
17042 	}
17043 
17044 	/* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */
17045 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
17046 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of selftest log */
17047 	scmd->satacmd_sec_count_msb = 0;	/* One sector of selftest log */
17048 	scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE;
17049 	scmd->satacmd_lba_low_msb = 0;
17050 	scmd->satacmd_lba_mid_lsb = block_num & 0xff;
17051 	scmd->satacmd_lba_mid_msb = block_num >> 8;
17052 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
17053 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
17054 
17055 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17056 	    sdinfo->satadrv_addr.cport)));
17057 
17058 	/* Send pkt to SATA HBA driver */
17059 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17060 	    SATA_TRAN_ACCEPTED ||
17061 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17062 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17063 		    sdinfo->satadrv_addr.cport)));
17064 
17065 		/*
17066 		 * Whoops, no SMART selftest log info available
17067 		 */
17068 		rval = -1;
17069 		goto fail;
17070 	} else {
17071 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17072 		    sdinfo->satadrv_addr.cport)));
17073 
17074 		if (spx->txlt_buf_dma_handle != NULL) {
17075 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17076 			    DDI_DMA_SYNC_FORKERNEL);
17077 			ASSERT(rval == DDI_SUCCESS);
17078 			if (sata_check_for_dma_error(dip, spx)) {
17079 				ddi_fm_service_impact(dip,
17080 				    DDI_SERVICE_UNAFFECTED);
17081 				rval = -1;
17082 				goto fail;
17083 			}
17084 		}
17085 		bcopy(scmd->satacmd_bp->b_un.b_addr,
17086 		    (uint8_t *)ext_selftest_log,
17087 		    sizeof (struct smart_ext_selftest_log));
17088 		rval = 0;
17089 	}
17090 
17091 fail:
17092 	/* Free allocated resources */
17093 	sata_free_local_buffer(spx);
17094 	sata_pkt_free(spx);
17095 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17096 
17097 	return (rval);
17098 }
17099 
17100 /*
17101  * Returns 0 for success, -1 otherwise
17102  *
17103  * SMART self-test log data is returned in buffer pointed to by selftest_log
17104  */
17105 static int
17106 sata_smart_selftest_log(
17107 	sata_hba_inst_t *sata_hba_inst,
17108 	sata_drive_info_t *sdinfo,
17109 	struct smart_selftest_log *selftest_log)
17110 {
17111 	sata_pkt_t *spkt;
17112 	sata_cmd_t *scmd;
17113 	sata_pkt_txlate_t *spx;
17114 	int rval;
17115 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
17116 
17117 #if ! defined(lint)
17118 	ASSERT(sizeof (struct smart_selftest_log) == 512);
17119 #endif
17120 
17121 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17122 	spx->txlt_sata_hba_inst = sata_hba_inst;
17123 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
17124 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17125 	if (spkt == NULL) {
17126 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17127 		return (-1);
17128 	}
17129 	/* address is needed now */
17130 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17131 
17132 
17133 	/* Fill sata_pkt */
17134 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17135 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17136 	/* Synchronous mode, no callback */
17137 	spkt->satapkt_comp = NULL;
17138 	/* Timeout 30s */
17139 	spkt->satapkt_time = sata_default_pkt_time;
17140 
17141 	scmd = &spkt->satapkt_cmd;
17142 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17143 
17144 	/*
17145 	 * Allocate buffer for SMART SELFTEST LOG
17146 	 */
17147 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
17148 	    sizeof (struct smart_selftest_log));
17149 	if (scmd->satacmd_bp == NULL) {
17150 		sata_pkt_free(spx);
17151 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17152 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17153 		    "sata_smart_selftest_log: "
17154 		    "cannot allocate buffer"));
17155 		return (-1);
17156 	}
17157 
17158 	/* Build SMART_READ_LOG cmd in the sata_pkt */
17159 	scmd->satacmd_addr_type = 0;		/* N/A */
17160 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of SMART log */
17161 	scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE;
17162 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
17163 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
17164 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
17165 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
17166 	scmd->satacmd_cmd_reg = SATAC_SMART;
17167 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17168 	    sdinfo->satadrv_addr.cport)));
17169 
17170 	/* Send pkt to SATA HBA driver */
17171 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17172 	    SATA_TRAN_ACCEPTED ||
17173 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17174 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17175 		    sdinfo->satadrv_addr.cport)));
17176 		/*
17177 		 * Whoops, no SMART DATA available
17178 		 */
17179 		rval = -1;
17180 		goto fail;
17181 	} else {
17182 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17183 		    sdinfo->satadrv_addr.cport)));
17184 		if (spx->txlt_buf_dma_handle != NULL) {
17185 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17186 			    DDI_DMA_SYNC_FORKERNEL);
17187 			ASSERT(rval == DDI_SUCCESS);
17188 			if (sata_check_for_dma_error(dip, spx)) {
17189 				ddi_fm_service_impact(dip,
17190 				    DDI_SERVICE_UNAFFECTED);
17191 				rval = -1;
17192 				goto fail;
17193 			}
17194 		}
17195 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log,
17196 		    sizeof (struct smart_selftest_log));
17197 		rval = 0;
17198 	}
17199 
17200 fail:
17201 	/* Free allocated resources */
17202 	sata_free_local_buffer(spx);
17203 	sata_pkt_free(spx);
17204 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17205 
17206 	return (rval);
17207 }
17208 
17209 
17210 /*
17211  * Returns 0 for success, -1 otherwise
17212  *
17213  * SMART READ LOG data is returned in buffer pointed to by smart_log
17214  */
17215 static int
17216 sata_smart_read_log(
17217 	sata_hba_inst_t *sata_hba_inst,
17218 	sata_drive_info_t *sdinfo,
17219 	uint8_t *smart_log,		/* where the data should be returned */
17220 	uint8_t which_log,		/* which log should be returned */
17221 	uint8_t log_size)		/* # of 512 bytes in log */
17222 {
17223 	sata_pkt_t *spkt;
17224 	sata_cmd_t *scmd;
17225 	sata_pkt_txlate_t *spx;
17226 	int rval;
17227 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
17228 
17229 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17230 	spx->txlt_sata_hba_inst = sata_hba_inst;
17231 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
17232 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17233 	if (spkt == NULL) {
17234 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17235 		return (-1);
17236 	}
17237 	/* address is needed now */
17238 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17239 
17240 
17241 	/* Fill sata_pkt */
17242 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17243 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17244 	/* Synchronous mode, no callback */
17245 	spkt->satapkt_comp = NULL;
17246 	/* Timeout 30s */
17247 	spkt->satapkt_time = sata_default_pkt_time;
17248 
17249 	scmd = &spkt->satapkt_cmd;
17250 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17251 
17252 	/*
17253 	 * Allocate buffer for SMART READ LOG
17254 	 */
17255 	scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512);
17256 	if (scmd->satacmd_bp == NULL) {
17257 		sata_pkt_free(spx);
17258 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17259 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17260 		    "sata_smart_read_log: " "cannot allocate buffer"));
17261 		return (-1);
17262 	}
17263 
17264 	/* Build SMART_READ_LOG cmd in the sata_pkt */
17265 	scmd->satacmd_addr_type = 0;		/* N/A */
17266 	scmd->satacmd_sec_count_lsb = log_size;	/* what the caller asked for */
17267 	scmd->satacmd_lba_low_lsb = which_log;	/* which log page */
17268 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
17269 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
17270 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
17271 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
17272 	scmd->satacmd_cmd_reg = SATAC_SMART;
17273 
17274 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17275 	    sdinfo->satadrv_addr.cport)));
17276 
17277 	/* Send pkt to SATA HBA driver */
17278 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17279 	    SATA_TRAN_ACCEPTED ||
17280 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17281 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17282 		    sdinfo->satadrv_addr.cport)));
17283 
17284 		/*
17285 		 * Whoops, no SMART DATA available
17286 		 */
17287 		rval = -1;
17288 		goto fail;
17289 	} else {
17290 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17291 		    sdinfo->satadrv_addr.cport)));
17292 
17293 		if (spx->txlt_buf_dma_handle != NULL) {
17294 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17295 			    DDI_DMA_SYNC_FORKERNEL);
17296 			ASSERT(rval == DDI_SUCCESS);
17297 			if (sata_check_for_dma_error(dip, spx)) {
17298 				ddi_fm_service_impact(dip,
17299 				    DDI_SERVICE_UNAFFECTED);
17300 				rval = -1;
17301 				goto fail;
17302 			}
17303 		}
17304 		bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512);
17305 		rval = 0;
17306 	}
17307 
17308 fail:
17309 	/* Free allocated resources */
17310 	sata_free_local_buffer(spx);
17311 	sata_pkt_free(spx);
17312 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17313 
17314 	return (rval);
17315 }
17316 
17317 /*
17318  * Used by LOG SENSE page 0x10
17319  *
17320  * return 0 for success, -1 otherwise
17321  *
17322  */
17323 static int
17324 sata_read_log_ext_directory(
17325 	sata_hba_inst_t *sata_hba_inst,
17326 	sata_drive_info_t *sdinfo,
17327 	struct read_log_ext_directory *logdir)
17328 {
17329 	sata_pkt_txlate_t *spx;
17330 	sata_pkt_t *spkt;
17331 	sata_cmd_t *scmd;
17332 	int rval;
17333 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
17334 
17335 #if ! defined(lint)
17336 	ASSERT(sizeof (struct read_log_ext_directory) == 512);
17337 #endif
17338 
17339 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
17340 	spx->txlt_sata_hba_inst = sata_hba_inst;
17341 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
17342 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
17343 	if (spkt == NULL) {
17344 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17345 		return (-1);
17346 	}
17347 
17348 	/* Fill sata_pkt */
17349 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
17350 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17351 	/* Synchronous mode, no callback */
17352 	spkt->satapkt_comp = NULL;
17353 	/* Timeout 30s */
17354 	spkt->satapkt_time = sata_default_pkt_time;
17355 
17356 	scmd = &spkt->satapkt_cmd;
17357 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
17358 
17359 	/*
17360 	 * Allocate buffer for SMART READ LOG EXTENDED command
17361 	 */
17362 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
17363 	    sizeof (struct read_log_ext_directory));
17364 	if (scmd->satacmd_bp == NULL) {
17365 		sata_pkt_free(spx);
17366 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
17367 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17368 		    "sata_read_log_ext_directory: "
17369 		    "cannot allocate buffer"));
17370 		return (-1);
17371 	}
17372 
17373 	/* Build READ LOG EXT w/ log directory cmd in the  sata_pkt */
17374 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
17375 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of directory */
17376 	scmd->satacmd_sec_count_msb = 0;	/* One sector of directory */
17377 	scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY;
17378 	scmd->satacmd_lba_low_msb = 0;
17379 	scmd->satacmd_lba_mid_lsb = 0;
17380 	scmd->satacmd_lba_mid_msb = 0;
17381 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
17382 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
17383 
17384 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
17385 	    sdinfo->satadrv_addr.cport)));
17386 
17387 	/* Send pkt to SATA HBA driver */
17388 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
17389 	    SATA_TRAN_ACCEPTED ||
17390 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
17391 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17392 		    sdinfo->satadrv_addr.cport)));
17393 		/*
17394 		 * Whoops, no SMART selftest log info available
17395 		 */
17396 		rval = -1;
17397 		goto fail;
17398 	} else {
17399 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
17400 		    sdinfo->satadrv_addr.cport)));
17401 		if (spx->txlt_buf_dma_handle != NULL) {
17402 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
17403 			    DDI_DMA_SYNC_FORKERNEL);
17404 			ASSERT(rval == DDI_SUCCESS);
17405 			if (sata_check_for_dma_error(dip, spx)) {
17406 				ddi_fm_service_impact(dip,
17407 				    DDI_SERVICE_UNAFFECTED);
17408 				rval = -1;
17409 				goto fail;
17410 			}
17411 		}
17412 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir,
17413 		    sizeof (struct read_log_ext_directory));
17414 		rval = 0;
17415 	}
17416 
17417 fail:
17418 	/* Free allocated resources */
17419 	sata_free_local_buffer(spx);
17420 	sata_pkt_free(spx);
17421 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
17422 
17423 	return (rval);
17424 }
17425 
17426 /*
17427  * Set up error retrieval sata command for NCQ command error data
17428  * recovery.
17429  *
17430  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
17431  * returns SATA_FAILURE otherwise.
17432  */
17433 static int
17434 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
17435 {
17436 #ifndef __lock_lint
17437 	_NOTE(ARGUNUSED(sdinfo))
17438 #endif
17439 
17440 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
17441 	sata_cmd_t *scmd;
17442 	struct buf *bp;
17443 
17444 	/* Operation modes are up to the caller */
17445 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
17446 
17447 	/* Synchronous mode, no callback - may be changed by the caller */
17448 	spkt->satapkt_comp = NULL;
17449 	spkt->satapkt_time = sata_default_pkt_time;
17450 
17451 	scmd = &spkt->satapkt_cmd;
17452 	bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t));
17453 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
17454 
17455 	/*
17456 	 * Allocate dma_able buffer error data.
17457 	 * Buffer allocation will take care of buffer alignment and other DMA
17458 	 * attributes.
17459 	 */
17460 	bp = sata_alloc_local_buffer(spx,
17461 	    sizeof (struct sata_ncq_error_recovery_page));
17462 	if (bp == NULL)
17463 		return (SATA_FAILURE);
17464 
17465 	bp_mapin(bp); /* make data buffer accessible */
17466 	scmd->satacmd_bp = bp;
17467 
17468 	/*
17469 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
17470 	 * before accessing it. Handle is in usual place in translate struct.
17471 	 */
17472 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
17473 
17474 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
17475 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
17476 
17477 	return (SATA_SUCCESS);
17478 }
17479 
17480 /*
17481  * sata_xlate_errors() is used to translate (S)ATA error
17482  * information to SCSI information returned in the SCSI
17483  * packet.
17484  */
17485 static void
17486 sata_xlate_errors(sata_pkt_txlate_t *spx)
17487 {
17488 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
17489 	struct scsi_extended_sense *sense;
17490 
17491 	scsipkt->pkt_reason = CMD_INCOMPLETE;
17492 	*scsipkt->pkt_scbp = STATUS_CHECK;
17493 	sense = sata_arq_sense(spx);
17494 
17495 	switch (spx->txlt_sata_pkt->satapkt_reason) {
17496 	case SATA_PKT_PORT_ERROR:
17497 		/*
17498 		 * We have no device data. Assume no data transfered.
17499 		 */
17500 		sense->es_key = KEY_HARDWARE_ERROR;
17501 		break;
17502 
17503 	case SATA_PKT_DEV_ERROR:
17504 		if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
17505 		    SATA_STATUS_ERR) {
17506 			/*
17507 			 * determine dev error reason from error
17508 			 * reg content
17509 			 */
17510 			sata_decode_device_error(spx, sense);
17511 			break;
17512 		}
17513 		/* No extended sense key - no info available */
17514 		break;
17515 
17516 	case SATA_PKT_TIMEOUT:
17517 		scsipkt->pkt_reason = CMD_TIMEOUT;
17518 		scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET;
17519 		/* No extended sense key */
17520 		break;
17521 
17522 	case SATA_PKT_ABORTED:
17523 		scsipkt->pkt_reason = CMD_ABORTED;
17524 		scsipkt->pkt_statistics |= STAT_ABORTED;
17525 		/* No extended sense key */
17526 		break;
17527 
17528 	case SATA_PKT_RESET:
17529 		/*
17530 		 * pkt aborted either by an explicit reset request from
17531 		 * a host, or due to error recovery
17532 		 */
17533 		scsipkt->pkt_reason = CMD_RESET;
17534 		scsipkt->pkt_statistics |= STAT_DEV_RESET;
17535 		break;
17536 
17537 	default:
17538 		scsipkt->pkt_reason = CMD_TRAN_ERR;
17539 		break;
17540 	}
17541 }
17542 
17543 
17544 
17545 
17546 /*
17547  * Log sata message
17548  * dev pathname msg line preceeds the logged message.
17549  */
17550 
17551 static	void
17552 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...)
17553 {
17554 	char pathname[128];
17555 	dev_info_t *dip = NULL;
17556 	va_list ap;
17557 
17558 	mutex_enter(&sata_log_mutex);
17559 
17560 	va_start(ap, fmt);
17561 	(void) vsprintf(sata_log_buf, fmt, ap);
17562 	va_end(ap);
17563 
17564 	if (sata_hba_inst != NULL) {
17565 		dip = SATA_DIP(sata_hba_inst);
17566 		(void) ddi_pathname(dip, pathname);
17567 	} else {
17568 		pathname[0] = 0;
17569 	}
17570 	if (level == CE_CONT) {
17571 		if (sata_debug_flags == 0)
17572 			cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf);
17573 		else
17574 			cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf);
17575 	} else {
17576 		if (level != CE_NOTE) {
17577 			cmn_err(level, "%s:\n %s", pathname, sata_log_buf);
17578 		} else if (sata_msg) {
17579 			cmn_err(level, "%s:\n %s", pathname,
17580 			    sata_log_buf);
17581 		}
17582 	}
17583 
17584 	/* sata trace debug */
17585 	sata_trace_debug(dip, sata_log_buf);
17586 
17587 	mutex_exit(&sata_log_mutex);
17588 }
17589 
17590 
17591 /* ******** Asynchronous HBA events handling & hotplugging support ******** */
17592 
17593 /*
17594  * Start or terminate the thread, depending on flag arg and current state
17595  */
17596 static void
17597 sata_event_thread_control(int startstop)
17598 {
17599 	static int sata_event_thread_terminating = 0;
17600 	static int sata_event_thread_starting = 0;
17601 	int i;
17602 
17603 	mutex_enter(&sata_event_mutex);
17604 
17605 	if (startstop == 0 && (sata_event_thread_starting == 1 ||
17606 	    sata_event_thread_terminating == 1)) {
17607 		mutex_exit(&sata_event_mutex);
17608 		return;
17609 	}
17610 	if (startstop == 1 && sata_event_thread_starting == 1) {
17611 		mutex_exit(&sata_event_mutex);
17612 		return;
17613 	}
17614 	if (startstop == 1 && sata_event_thread_terminating == 1) {
17615 		sata_event_thread_starting = 1;
17616 		/* wait til terminate operation completes */
17617 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
17618 		while (sata_event_thread_terminating == 1) {
17619 			if (i-- <= 0) {
17620 				sata_event_thread_starting = 0;
17621 				mutex_exit(&sata_event_mutex);
17622 #ifdef SATA_DEBUG
17623 				cmn_err(CE_WARN, "sata_event_thread_control: "
17624 				    "timeout waiting for thread to terminate");
17625 #endif
17626 				return;
17627 			}
17628 			mutex_exit(&sata_event_mutex);
17629 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
17630 			mutex_enter(&sata_event_mutex);
17631 		}
17632 	}
17633 	if (startstop == 1) {
17634 		if (sata_event_thread == NULL) {
17635 			sata_event_thread = thread_create(NULL, 0,
17636 			    (void (*)())sata_event_daemon,
17637 			    &sata_hba_list, 0, &p0, TS_RUN, minclsyspri);
17638 		}
17639 		sata_event_thread_starting = 0;
17640 		mutex_exit(&sata_event_mutex);
17641 		return;
17642 	}
17643 
17644 	/*
17645 	 * If we got here, thread may need to be terminated
17646 	 */
17647 	if (sata_event_thread != NULL) {
17648 		int i;
17649 		/* Signal event thread to go away */
17650 		sata_event_thread_terminating = 1;
17651 		sata_event_thread_terminate = 1;
17652 		cv_signal(&sata_event_cv);
17653 		/*
17654 		 * Wait til daemon terminates.
17655 		 */
17656 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
17657 		while (sata_event_thread_terminate == 1) {
17658 			mutex_exit(&sata_event_mutex);
17659 			if (i-- <= 0) {
17660 				/* Daemon did not go away !!! */
17661 #ifdef SATA_DEBUG
17662 				cmn_err(CE_WARN, "sata_event_thread_control: "
17663 				    "cannot terminate event daemon thread");
17664 #endif
17665 				mutex_enter(&sata_event_mutex);
17666 				break;
17667 			}
17668 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
17669 			mutex_enter(&sata_event_mutex);
17670 		}
17671 		sata_event_thread_terminating = 0;
17672 	}
17673 	ASSERT(sata_event_thread_terminating == 0);
17674 	ASSERT(sata_event_thread_starting == 0);
17675 	mutex_exit(&sata_event_mutex);
17676 }
17677 
17678 
17679 /*
17680  * SATA HBA event notification function.
17681  * Events reported by SATA HBA drivers per HBA instance relate to a change in
17682  * a port and/or device state or a controller itself.
17683  * Events for different addresses/addr types cannot be combined.
17684  * A warning message is generated for each event type.
17685  * Events are not processed by this function, so only the
17686  * event flag(s)is set for an affected entity and the event thread is
17687  * waken up. Event daemon thread processes all events.
17688  *
17689  * NOTE: Since more than one event may be reported at the same time, one
17690  * cannot determine a sequence of events when opposite event are reported, eg.
17691  * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing
17692  * is taking precedence over reported events, i.e. may cause ignoring some
17693  * events.
17694  */
17695 #define	SATA_EVENT_MAX_MSG_LENGTH	79
17696 
17697 void
17698 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event)
17699 {
17700 	sata_hba_inst_t *sata_hba_inst = NULL;
17701 	sata_address_t *saddr;
17702 	sata_pmult_info_t *pmultinfo;
17703 	sata_drive_info_t *sdinfo;
17704 	sata_port_stats_t *pstats;
17705 	sata_cport_info_t *cportinfo;
17706 	sata_pmport_info_t *pmportinfo;
17707 	int cport, pmport;
17708 	char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1];
17709 	char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1];
17710 	char *lcp;
17711 	static char *err_msg_evnt_1 =
17712 	    "sata_hba_event_notify: invalid port event 0x%x ";
17713 	static char *err_msg_evnt_2 =
17714 	    "sata_hba_event_notify: invalid device event 0x%x ";
17715 	int linkevent;
17716 
17717 	/*
17718 	 * There is a possibility that an event will be generated on HBA
17719 	 * that has not completed attachment or is detaching. We still want
17720 	 * to process events until HBA is detached.
17721 	 */
17722 	mutex_enter(&sata_mutex);
17723 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
17724 	    sata_hba_inst = sata_hba_inst->satahba_next) {
17725 		if (SATA_DIP(sata_hba_inst) == dip)
17726 			if (sata_hba_inst->satahba_attached == 1)
17727 				break;
17728 	}
17729 	mutex_exit(&sata_mutex);
17730 	if (sata_hba_inst == NULL)
17731 		/* HBA not attached */
17732 		return;
17733 
17734 	ASSERT(sata_device != NULL);
17735 
17736 	/*
17737 	 * Validate address before - do not proceed with invalid address.
17738 	 */
17739 	saddr = &sata_device->satadev_addr;
17740 	if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst))
17741 		return;
17742 
17743 	cport = saddr->cport;
17744 	pmport = saddr->pmport;
17745 
17746 	buf1[0] = buf2[0] = '\0';
17747 
17748 	/*
17749 	 * If event relates to port or device, check port state.
17750 	 * Port has to be initialized, or we cannot accept an event.
17751 	 */
17752 	if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT |
17753 	    SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT | SATA_ADDR_PMULT)) != 0) {
17754 		mutex_enter(&sata_hba_inst->satahba_mutex);
17755 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
17756 		mutex_exit(&sata_hba_inst->satahba_mutex);
17757 		if (cportinfo == NULL || cportinfo->cport_state == 0)
17758 			return;
17759 	}
17760 
17761 	if ((saddr->qual & (SATA_ADDR_PMULT | SATA_ADDR_PMPORT |
17762 	    SATA_ADDR_DPMPORT)) != 0) {
17763 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
17764 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17765 			    "sata_hba_event_notify: Non-pmult device (0x%x)"
17766 			    "is attached to port %d, ignore pmult/pmport "
17767 			    "event 0x%x", cportinfo->cport_dev_type,
17768 			    cport, event));
17769 			return;
17770 		}
17771 
17772 		mutex_enter(&cportinfo->cport_mutex);
17773 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
17774 		mutex_exit(&cportinfo->cport_mutex);
17775 
17776 		/*
17777 		 * The daemon might be processing attachment of port
17778 		 * multiplier, in that case we should ignore events on its
17779 		 * sub-devices.
17780 		 *
17781 		 * NOTE: Only pmult_state is checked in sata_hba_event_notify.
17782 		 * The pmport_state is checked by sata daemon.
17783 		 */
17784 		if (pmultinfo == NULL ||
17785 		    pmultinfo->pmult_state == SATA_STATE_UNKNOWN) {
17786 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17787 			    "sata_hba_event_notify: pmult is not"
17788 			    "available at port %d:%d, ignore event 0x%x",
17789 			    cport, pmport, event));
17790 			return;
17791 		}
17792 	}
17793 
17794 	if ((saddr->qual &
17795 	    (SATA_ADDR_PMPORT | SATA_ADDR_DPMPORT)) != 0) {
17796 
17797 		mutex_enter(&cportinfo->cport_mutex);
17798 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) {
17799 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17800 			    "sata_hba_event_notify: invalid/"
17801 			    "un-implemented port %d:%d (%d ports), "
17802 			    "ignore event 0x%x", cport, pmport,
17803 			    SATA_NUM_PMPORTS(sata_hba_inst, cport), event));
17804 			mutex_exit(&cportinfo->cport_mutex);
17805 			return;
17806 		}
17807 		mutex_exit(&cportinfo->cport_mutex);
17808 
17809 		mutex_enter(&sata_hba_inst->satahba_mutex);
17810 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
17811 		    cport, pmport);
17812 		mutex_exit(&sata_hba_inst->satahba_mutex);
17813 
17814 		/* pmport is implemented/valid? */
17815 		if (pmportinfo == NULL) {
17816 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17817 			    "sata_hba_event_notify: invalid/"
17818 			    "un-implemented port %d:%d, ignore "
17819 			    "event 0x%x", cport, pmport, event));
17820 			return;
17821 		}
17822 	}
17823 
17824 	/*
17825 	 * Events refer to devices, ports and controllers - each has
17826 	 * unique address. Events for different addresses cannot be combined.
17827 	 */
17828 	if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) {
17829 
17830 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17831 
17832 		/* qualify this event(s) */
17833 		if ((event & SATA_EVNT_PORT_EVENTS) == 0) {
17834 			/* Invalid event for the device port */
17835 			(void) sprintf(buf2, err_msg_evnt_1,
17836 			    event & SATA_EVNT_PORT_EVENTS);
17837 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17838 			goto event_info;
17839 		}
17840 		if (saddr->qual == SATA_ADDR_CPORT) {
17841 			/* Controller's device port event */
17842 
17843 			(SATA_CPORT_INFO(sata_hba_inst, cport))->
17844 			    cport_event_flags |=
17845 			    event & SATA_EVNT_PORT_EVENTS;
17846 			pstats =
17847 			    &(SATA_CPORT_INFO(sata_hba_inst, cport))->
17848 			    cport_stats;
17849 		} else {
17850 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17851 			mutex_enter(&pmportinfo->pmport_mutex);
17852 			/* Port multiplier's device port event */
17853 			(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
17854 			    pmport_event_flags |=
17855 			    event & SATA_EVNT_PORT_EVENTS;
17856 			pstats =
17857 			    &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
17858 			    pmport_stats;
17859 			mutex_exit(&pmportinfo->pmport_mutex);
17860 			mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17861 		}
17862 
17863 		/*
17864 		 * Add to statistics and log the message. We have to do it
17865 		 * here rather than in the event daemon, because there may be
17866 		 * multiple events occuring before they are processed.
17867 		 */
17868 		linkevent = event &
17869 		    (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED);
17870 		if (linkevent) {
17871 			if (linkevent == (SATA_EVNT_LINK_LOST |
17872 			    SATA_EVNT_LINK_ESTABLISHED)) {
17873 				/* This is likely event combination */
17874 				(void) strlcat(buf1, "link lost/established, ",
17875 				    SATA_EVENT_MAX_MSG_LENGTH);
17876 
17877 				if (pstats->link_lost < 0xffffffffffffffffULL)
17878 					pstats->link_lost++;
17879 				if (pstats->link_established <
17880 				    0xffffffffffffffffULL)
17881 					pstats->link_established++;
17882 				linkevent = 0;
17883 			} else if (linkevent & SATA_EVNT_LINK_LOST) {
17884 				(void) strlcat(buf1, "link lost, ",
17885 				    SATA_EVENT_MAX_MSG_LENGTH);
17886 
17887 				if (pstats->link_lost < 0xffffffffffffffffULL)
17888 					pstats->link_lost++;
17889 			} else {
17890 				(void) strlcat(buf1, "link established, ",
17891 				    SATA_EVENT_MAX_MSG_LENGTH);
17892 				if (pstats->link_established <
17893 				    0xffffffffffffffffULL)
17894 					pstats->link_established++;
17895 			}
17896 		}
17897 		if (event & SATA_EVNT_DEVICE_ATTACHED) {
17898 			(void) strlcat(buf1, "device attached, ",
17899 			    SATA_EVENT_MAX_MSG_LENGTH);
17900 			if (pstats->device_attached < 0xffffffffffffffffULL)
17901 				pstats->device_attached++;
17902 		}
17903 		if (event & SATA_EVNT_DEVICE_DETACHED) {
17904 			(void) strlcat(buf1, "device detached, ",
17905 			    SATA_EVENT_MAX_MSG_LENGTH);
17906 			if (pstats->device_detached < 0xffffffffffffffffULL)
17907 				pstats->device_detached++;
17908 		}
17909 		if (event & SATA_EVNT_PWR_LEVEL_CHANGED) {
17910 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
17911 			    "port %d power level changed", cport);
17912 			if (pstats->port_pwr_changed < 0xffffffffffffffffULL)
17913 				pstats->port_pwr_changed++;
17914 		}
17915 
17916 		if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) {
17917 			/* There should be no other events for this address */
17918 			(void) sprintf(buf2, err_msg_evnt_1,
17919 			    event & ~SATA_EVNT_PORT_EVENTS);
17920 		}
17921 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17922 
17923 	} else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) {
17924 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17925 
17926 		/* qualify this event */
17927 		if ((event & SATA_EVNT_DEVICE_RESET) == 0) {
17928 			/* Invalid event for a device */
17929 			(void) sprintf(buf2, err_msg_evnt_2,
17930 			    event & SATA_EVNT_DEVICE_RESET);
17931 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17932 			goto event_info;
17933 		}
17934 		/* drive event */
17935 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
17936 		if (sdinfo != NULL) {
17937 			if (event & SATA_EVNT_DEVICE_RESET) {
17938 				(void) strlcat(buf1, "device reset, ",
17939 				    SATA_EVENT_MAX_MSG_LENGTH);
17940 				if (sdinfo->satadrv_stats.drive_reset <
17941 				    0xffffffffffffffffULL)
17942 					sdinfo->satadrv_stats.drive_reset++;
17943 				sdinfo->satadrv_event_flags |=
17944 				    SATA_EVNT_DEVICE_RESET;
17945 			}
17946 		}
17947 		if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) {
17948 			/* Invalid event for a device */
17949 			(void) sprintf(buf2, err_msg_evnt_2,
17950 			    event & ~SATA_EVNT_DRIVE_EVENTS);
17951 		}
17952 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17953 	} else if (saddr->qual == SATA_ADDR_PMULT) {
17954 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17955 
17956 		/* qualify this event */
17957 		if ((event & (SATA_EVNT_DEVICE_RESET |
17958 		    SATA_EVNT_PMULT_LINK_CHANGED)) == 0) {
17959 			/* Invalid event for a port multiplier */
17960 			(void) sprintf(buf2, err_msg_evnt_2,
17961 			    event & SATA_EVNT_DEVICE_RESET);
17962 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17963 			goto event_info;
17964 		}
17965 
17966 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
17967 
17968 		if (event & SATA_EVNT_DEVICE_RESET) {
17969 
17970 			SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
17971 			    "[Reset] port-mult on cport %d", cport);
17972 			pmultinfo->pmult_event_flags |=
17973 			    SATA_EVNT_DEVICE_RESET;
17974 			(void) strlcat(buf1, "pmult reset, ",
17975 			    SATA_EVENT_MAX_MSG_LENGTH);
17976 		}
17977 
17978 		if (event & SATA_EVNT_PMULT_LINK_CHANGED) {
17979 
17980 			SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
17981 			    "pmult link changed on cport %d", cport);
17982 			pmultinfo->pmult_event_flags |=
17983 			    SATA_EVNT_PMULT_LINK_CHANGED;
17984 			(void) strlcat(buf1, "pmult link changed, ",
17985 			    SATA_EVENT_MAX_MSG_LENGTH);
17986 		}
17987 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
17988 
17989 	} else {
17990 		if (saddr->qual != SATA_ADDR_NULL) {
17991 			/* Wrong address qualifier */
17992 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17993 			    "sata_hba_event_notify: invalid address 0x%x",
17994 			    *(uint32_t *)saddr));
17995 			return;
17996 		}
17997 		if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 ||
17998 		    (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) {
17999 			/* Invalid event for the controller */
18000 			SATA_LOG_D((sata_hba_inst, CE_WARN,
18001 			    "sata_hba_event_notify: invalid event 0x%x for "
18002 			    "controller",
18003 			    event & SATA_EVNT_CONTROLLER_EVENTS));
18004 			return;
18005 		}
18006 		buf1[0] = '\0';
18007 		/* This may be a frequent and not interesting event */
18008 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
18009 		    "controller power level changed\n", NULL);
18010 
18011 		mutex_enter(&sata_hba_inst->satahba_mutex);
18012 		if (sata_hba_inst->satahba_stats.ctrl_pwr_change <
18013 		    0xffffffffffffffffULL)
18014 			sata_hba_inst->satahba_stats.ctrl_pwr_change++;
18015 
18016 		sata_hba_inst->satahba_event_flags |=
18017 		    SATA_EVNT_PWR_LEVEL_CHANGED;
18018 		mutex_exit(&sata_hba_inst->satahba_mutex);
18019 	}
18020 	/*
18021 	 * If we got here, there is something to do with this HBA
18022 	 * instance.
18023 	 */
18024 	mutex_enter(&sata_hba_inst->satahba_mutex);
18025 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
18026 	mutex_exit(&sata_hba_inst->satahba_mutex);
18027 	mutex_enter(&sata_mutex);
18028 	sata_event_pending |= SATA_EVNT_MAIN;	/* global event indicator */
18029 	mutex_exit(&sata_mutex);
18030 
18031 	/* Tickle event thread */
18032 	mutex_enter(&sata_event_mutex);
18033 	if (sata_event_thread_active == 0)
18034 		cv_signal(&sata_event_cv);
18035 	mutex_exit(&sata_event_mutex);
18036 
18037 event_info:
18038 	if (buf1[0] != '\0') {
18039 		lcp = strrchr(buf1, ',');
18040 		if (lcp != NULL)
18041 			*lcp = '\0';
18042 	}
18043 	if (saddr->qual == SATA_ADDR_CPORT ||
18044 	    saddr->qual == SATA_ADDR_DCPORT) {
18045 		if (buf1[0] != '\0') {
18046 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
18047 			    cport, buf1);
18048 		}
18049 		if (buf2[0] != '\0') {
18050 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
18051 			    cport, buf2);
18052 		}
18053 	} else if (saddr->qual == SATA_ADDR_PMPORT ||
18054 	    saddr->qual == SATA_ADDR_DPMPORT) {
18055 		if (buf1[0] != '\0') {
18056 			sata_log(sata_hba_inst, CE_NOTE,
18057 			    "port %d pmport %d: %s\n", cport, pmport, buf1);
18058 		}
18059 		if (buf2[0] != '\0') {
18060 			sata_log(sata_hba_inst, CE_NOTE,
18061 			    "port %d pmport %d: %s\n", cport, pmport, buf2);
18062 		}
18063 	}
18064 }
18065 
18066 
18067 /*
18068  * Event processing thread.
18069  * Arg is a pointer to the sata_hba_list pointer.
18070  * It is not really needed, because sata_hba_list is global and static
18071  */
18072 static void
18073 sata_event_daemon(void *arg)
18074 {
18075 #ifndef __lock_lint
18076 	_NOTE(ARGUNUSED(arg))
18077 #endif
18078 	sata_hba_inst_t *sata_hba_inst;
18079 	clock_t delta;
18080 
18081 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18082 	    "SATA event daemon started\n", NULL);
18083 loop:
18084 	/*
18085 	 * Process events here. Walk through all registered HBAs
18086 	 */
18087 	mutex_enter(&sata_mutex);
18088 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
18089 	    sata_hba_inst = sata_hba_inst->satahba_next) {
18090 		ASSERT(sata_hba_inst != NULL);
18091 		mutex_enter(&sata_hba_inst->satahba_mutex);
18092 		if (sata_hba_inst->satahba_attached == 0 ||
18093 		    (sata_hba_inst->satahba_event_flags &
18094 		    SATA_EVNT_SKIP) != 0) {
18095 			mutex_exit(&sata_hba_inst->satahba_mutex);
18096 			continue;
18097 		}
18098 		if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) {
18099 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP;
18100 			mutex_exit(&sata_hba_inst->satahba_mutex);
18101 			mutex_exit(&sata_mutex);
18102 			/* Got the controller with pending event */
18103 			sata_process_controller_events(sata_hba_inst);
18104 			/*
18105 			 * Since global mutex was released, there is a
18106 			 * possibility that HBA list has changed, so start
18107 			 * over from the top. Just processed controller
18108 			 * will be passed-over because of the SKIP flag.
18109 			 */
18110 			goto loop;
18111 		}
18112 		mutex_exit(&sata_hba_inst->satahba_mutex);
18113 	}
18114 	/* Clear SKIP flag in all controllers */
18115 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
18116 	    sata_hba_inst = sata_hba_inst->satahba_next) {
18117 		mutex_enter(&sata_hba_inst->satahba_mutex);
18118 		sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP;
18119 		mutex_exit(&sata_hba_inst->satahba_mutex);
18120 	}
18121 	mutex_exit(&sata_mutex);
18122 
18123 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18124 	    "SATA EVENT DAEMON suspending itself", NULL);
18125 
18126 #ifdef SATA_DEBUG
18127 	if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) {
18128 		sata_log(sata_hba_inst, CE_WARN,
18129 		    "SATA EVENTS PROCESSING DISABLED\n");
18130 		thread_exit(); /* Daemon will not run again */
18131 	}
18132 #endif
18133 	mutex_enter(&sata_event_mutex);
18134 	sata_event_thread_active = 0;
18135 	mutex_exit(&sata_event_mutex);
18136 	/*
18137 	 * Go to sleep/suspend itself and wake up either because new event or
18138 	 * wait timeout. Exit if there is a termination request (driver
18139 	 * unload).
18140 	 */
18141 	delta = drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME);
18142 	do {
18143 		mutex_enter(&sata_event_mutex);
18144 		(void) cv_reltimedwait(&sata_event_cv, &sata_event_mutex,
18145 		    delta, TR_CLOCK_TICK);
18146 
18147 		if (sata_event_thread_active != 0) {
18148 			mutex_exit(&sata_event_mutex);
18149 			continue;
18150 		}
18151 
18152 		/* Check if it is time to go away */
18153 		if (sata_event_thread_terminate == 1) {
18154 			/*
18155 			 * It is up to the thread setting above flag to make
18156 			 * sure that this thread is not killed prematurely.
18157 			 */
18158 			sata_event_thread_terminate = 0;
18159 			sata_event_thread = NULL;
18160 			mutex_exit(&sata_event_mutex);
18161 			SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18162 			    "SATA_EVENT_DAEMON_TERMINATING", NULL);
18163 			thread_exit();  { _NOTE(NOT_REACHED) }
18164 		}
18165 		mutex_exit(&sata_event_mutex);
18166 	} while (!(sata_event_pending & SATA_EVNT_MAIN));
18167 
18168 	mutex_enter(&sata_event_mutex);
18169 	sata_event_thread_active = 1;
18170 	mutex_exit(&sata_event_mutex);
18171 
18172 	mutex_enter(&sata_mutex);
18173 	sata_event_pending &= ~SATA_EVNT_MAIN;
18174 	mutex_exit(&sata_mutex);
18175 
18176 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
18177 	    "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL);
18178 
18179 	goto loop;
18180 }
18181 
18182 /*
18183  * Specific HBA instance event processing.
18184  *
18185  * NOTE: At the moment, device event processing is limited to hard disks
18186  * only.
18187  * Port multiplier is supported now.
18188  */
18189 static void
18190 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst)
18191 {
18192 	int ncport;
18193 	uint32_t event_flags;
18194 	sata_address_t *saddr;
18195 	sata_cport_info_t *cportinfo;
18196 	sata_pmult_info_t *pmultinfo;
18197 
18198 	SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst,
18199 	    "Processing controller %d event(s)",
18200 	    ddi_get_instance(SATA_DIP(sata_hba_inst)));
18201 
18202 	mutex_enter(&sata_hba_inst->satahba_mutex);
18203 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN;
18204 	event_flags = sata_hba_inst->satahba_event_flags;
18205 	mutex_exit(&sata_hba_inst->satahba_mutex);
18206 	/*
18207 	 * Process controller power change first
18208 	 * HERE
18209 	 */
18210 	if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED)
18211 		sata_process_cntrl_pwr_level_change(sata_hba_inst);
18212 
18213 	/*
18214 	 * Search through ports/devices to identify affected port/device.
18215 	 * We may have to process events for more than one port/device.
18216 	 */
18217 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
18218 		/*
18219 		 * Not all ports may be processed in attach by the time we
18220 		 * get an event. Check if port info is initialized.
18221 		 */
18222 		mutex_enter(&sata_hba_inst->satahba_mutex);
18223 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
18224 		mutex_exit(&sata_hba_inst->satahba_mutex);
18225 		if (cportinfo == NULL || cportinfo->cport_state == 0)
18226 			continue;
18227 
18228 		/* We have initialized controller port info */
18229 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18230 		event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))->
18231 		    cport_event_flags;
18232 		/* Check if port was locked by IOCTL processing */
18233 		if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) {
18234 			/*
18235 			 * We ignore port events because port is busy
18236 			 * with AP control processing. Set again
18237 			 * controller and main event flag, so that
18238 			 * events may be processed by the next daemon
18239 			 * run.
18240 			 */
18241 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18242 			mutex_enter(&sata_hba_inst->satahba_mutex);
18243 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
18244 			mutex_exit(&sata_hba_inst->satahba_mutex);
18245 			mutex_enter(&sata_mutex);
18246 			sata_event_pending |= SATA_EVNT_MAIN;
18247 			mutex_exit(&sata_mutex);
18248 			SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst,
18249 			    "Event processing postponed until "
18250 			    "AP control processing completes",
18251 			    NULL);
18252 			/* Check other ports */
18253 			continue;
18254 		} else {
18255 			/*
18256 			 * Set BSY flag so that AP control would not
18257 			 * interfere with events processing for
18258 			 * this port.
18259 			 */
18260 			(SATA_CPORT_INFO(sata_hba_inst, ncport))->
18261 			    cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY;
18262 		}
18263 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18264 
18265 		saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr;
18266 
18267 		if ((event_flags &
18268 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
18269 			/*
18270 			 * Got port event.
18271 			 * We need some hierarchy of event processing as they
18272 			 * are affecting each other:
18273 			 * 1. port failed
18274 			 * 2. device detached/attached
18275 			 * 3. link events - link events may trigger device
18276 			 *    detached or device attached events in some
18277 			 *    circumstances.
18278 			 * 4. port power level changed
18279 			 */
18280 			if (event_flags & SATA_EVNT_PORT_FAILED) {
18281 				sata_process_port_failed_event(sata_hba_inst,
18282 				    saddr);
18283 			}
18284 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
18285 				sata_process_device_detached(sata_hba_inst,
18286 				    saddr);
18287 			}
18288 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
18289 				sata_process_device_attached(sata_hba_inst,
18290 				    saddr);
18291 			}
18292 			if (event_flags &
18293 			    (SATA_EVNT_LINK_ESTABLISHED |
18294 			    SATA_EVNT_LINK_LOST)) {
18295 				sata_process_port_link_events(sata_hba_inst,
18296 				    saddr);
18297 			}
18298 			if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) {
18299 				sata_process_port_pwr_change(sata_hba_inst,
18300 				    saddr);
18301 			}
18302 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
18303 				sata_process_target_node_cleanup(
18304 				    sata_hba_inst, saddr);
18305 			}
18306 			if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) {
18307 				sata_process_device_autoonline(
18308 				    sata_hba_inst, saddr);
18309 			}
18310 		}
18311 
18312 
18313 		/*
18314 		 * Scan port multiplier and all its sub-ports event flags.
18315 		 * The events are marked by
18316 		 * (1) sata_pmult_info.pmult_event_flags
18317 		 * (2) sata_pmport_info.pmport_event_flags
18318 		 */
18319 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18320 		if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
18321 			/*
18322 			 * There should be another extra check: this
18323 			 * port multiplier still exists?
18324 			 */
18325 			pmultinfo = SATA_PMULT_INFO(sata_hba_inst,
18326 			    ncport);
18327 
18328 			if (pmultinfo != NULL) {
18329 				mutex_exit(&(SATA_CPORT_MUTEX(
18330 				    sata_hba_inst, ncport)));
18331 				sata_process_pmult_events(
18332 				    sata_hba_inst, ncport);
18333 				mutex_enter(&(SATA_CPORT_MUTEX(
18334 				    sata_hba_inst, ncport)));
18335 			} else {
18336 				SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
18337 				    "Port-multiplier is gone. "
18338 				    "Ignore all sub-device events "
18339 				    "at port %d.", ncport);
18340 			}
18341 		}
18342 
18343 		if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) !=
18344 		    SATA_DTYPE_NONE) &&
18345 		    (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) {
18346 			if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)->
18347 			    satadrv_event_flags &
18348 			    (SATA_EVNT_DEVICE_RESET |
18349 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
18350 				/* Have device event */
18351 				sata_process_device_reset(sata_hba_inst,
18352 				    saddr);
18353 			}
18354 		}
18355 		/* Release PORT_BUSY flag */
18356 		(SATA_CPORT_INFO(sata_hba_inst, ncport))->
18357 		    cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
18358 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
18359 
18360 	} /* End of loop through the controller SATA ports */
18361 }
18362 
18363 /*
18364  * Specific port multiplier instance event processing. At the moment, device
18365  * event processing is limited to link/attach event only.
18366  *
18367  * NOTE: power management event is not supported yet.
18368  */
18369 static void
18370 sata_process_pmult_events(sata_hba_inst_t *sata_hba_inst, uint8_t cport)
18371 {
18372 	sata_cport_info_t *cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
18373 	sata_pmult_info_t *pmultinfo;
18374 	sata_pmport_info_t *pmportinfo;
18375 	sata_address_t *saddr;
18376 	sata_device_t sata_device;
18377 	uint32_t event_flags;
18378 	int npmport;
18379 	int rval;
18380 
18381 	SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst,
18382 	    "Processing pmult event(s) on cport %d of controller %d",
18383 	    cport, ddi_get_instance(SATA_DIP(sata_hba_inst)));
18384 
18385 	/* First process events on port multiplier */
18386 	mutex_enter(&cportinfo->cport_mutex);
18387 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
18388 	event_flags = pmultinfo->pmult_event_flags;
18389 
18390 	/*
18391 	 * Reset event (of port multiplier) has higher priority because the
18392 	 * port multiplier itself might be failed or removed after reset.
18393 	 */
18394 	if (event_flags & SATA_EVNT_DEVICE_RESET) {
18395 		/*
18396 		 * The status of the sub-links are uncertain,
18397 		 * so mark all sub-ports as RESET
18398 		 */
18399 		for (npmport = 0; npmport < SATA_NUM_PMPORTS(
18400 		    sata_hba_inst, cport); npmport ++) {
18401 			pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
18402 			    cport, npmport);
18403 			if (pmportinfo == NULL) {
18404 				/* That's weird. */
18405 				SATA_LOG_D((sata_hba_inst, CE_WARN,
18406 				    "sata_hba_event_notify: "
18407 				    "invalid/un-implemented "
18408 				    "port %d:%d (%d ports), ",
18409 				    cport, npmport, SATA_NUM_PMPORTS(
18410 				    sata_hba_inst, cport)));
18411 				continue;
18412 			}
18413 
18414 			mutex_enter(&pmportinfo->pmport_mutex);
18415 
18416 			/* Mark all pmport to unknow state. */
18417 			pmportinfo->pmport_state = SATA_STATE_UNKNOWN;
18418 			/* Mark all pmports with link events. */
18419 			pmportinfo->pmport_event_flags =
18420 			    (SATA_EVNT_LINK_ESTABLISHED|SATA_EVNT_LINK_LOST);
18421 			mutex_exit(&pmportinfo->pmport_mutex);
18422 		}
18423 
18424 	} else if (event_flags & SATA_EVNT_PMULT_LINK_CHANGED) {
18425 		/*
18426 		 * We need probe the port multiplier to know what has
18427 		 * happened.
18428 		 */
18429 		bzero(&sata_device, sizeof (sata_device_t));
18430 		sata_device.satadev_rev = SATA_DEVICE_REV;
18431 		sata_device.satadev_addr.cport = cport;
18432 		sata_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
18433 		sata_device.satadev_addr.qual = SATA_ADDR_PMULT;
18434 
18435 		mutex_exit(&cportinfo->cport_mutex);
18436 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18437 		    (SATA_DIP(sata_hba_inst), &sata_device);
18438 		mutex_enter(&cportinfo->cport_mutex);
18439 		if (rval != SATA_SUCCESS) {
18440 			/* Something went wrong? Fail the port */
18441 			cportinfo->cport_state = SATA_PSTATE_FAILED;
18442 			mutex_exit(&cportinfo->cport_mutex);
18443 			SATA_LOG_D((sata_hba_inst, CE_WARN,
18444 			    "SATA port %d probing failed", cport));
18445 
18446 			/* PMult structure must be released.  */
18447 			sata_free_pmult(sata_hba_inst, &sata_device);
18448 			return;
18449 		}
18450 
18451 		sata_update_port_info(sata_hba_inst, &sata_device);
18452 
18453 		/*
18454 		 * Sanity check - Port is active? Is the link active?
18455 		 * The device is still a port multiplier?
18456 		 */
18457 		if ((cportinfo->cport_state &
18458 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
18459 		    ((cportinfo->cport_scr.sstatus &
18460 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) ||
18461 		    (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) {
18462 			mutex_exit(&cportinfo->cport_mutex);
18463 
18464 			/* PMult structure must be released.  */
18465 			sata_free_pmult(sata_hba_inst, &sata_device);
18466 			return;
18467 		}
18468 
18469 		/* Probed succeed, set port ready. */
18470 		cportinfo->cport_state |=
18471 		    SATA_STATE_PROBED | SATA_STATE_READY;
18472 	}
18473 
18474 	/* Release port multiplier event flags. */
18475 	pmultinfo->pmult_event_flags &=
18476 	    ~(SATA_EVNT_DEVICE_RESET|SATA_EVNT_PMULT_LINK_CHANGED);
18477 	mutex_exit(&cportinfo->cport_mutex);
18478 
18479 	/*
18480 	 * Check all sub-links.
18481 	 */
18482 	for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst, cport);
18483 	    npmport ++) {
18484 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport);
18485 		mutex_enter(&pmportinfo->pmport_mutex);
18486 		event_flags = pmportinfo->pmport_event_flags;
18487 		mutex_exit(&pmportinfo->pmport_mutex);
18488 		saddr = &pmportinfo->pmport_addr;
18489 
18490 		if ((event_flags &
18491 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
18492 			/*
18493 			 * Got port multiplier port event.
18494 			 * We need some hierarchy of event processing as they
18495 			 * are affecting each other:
18496 			 * 1. device detached/attached
18497 			 * 2. link events - link events may trigger device
18498 			 *    detached or device attached events in some
18499 			 *    circumstances.
18500 			 */
18501 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
18502 				sata_process_pmdevice_detached(sata_hba_inst,
18503 				    saddr);
18504 			}
18505 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
18506 				sata_process_pmdevice_attached(sata_hba_inst,
18507 				    saddr);
18508 			}
18509 			if (event_flags & SATA_EVNT_LINK_ESTABLISHED ||
18510 			    event_flags & SATA_EVNT_LINK_LOST) {
18511 				sata_process_pmport_link_events(sata_hba_inst,
18512 				    saddr);
18513 			}
18514 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
18515 				sata_process_target_node_cleanup(
18516 				    sata_hba_inst, saddr);
18517 			}
18518 		}
18519 
18520 		/* Checking drive event(s). */
18521 		mutex_enter(&pmportinfo->pmport_mutex);
18522 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
18523 		    pmportinfo->pmport_sata_drive != NULL) {
18524 			event_flags = pmportinfo->pmport_sata_drive->
18525 			    satadrv_event_flags;
18526 			if (event_flags & (SATA_EVNT_DEVICE_RESET |
18527 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
18528 
18529 				/* Have device event */
18530 				sata_process_pmdevice_reset(sata_hba_inst,
18531 				    saddr);
18532 			}
18533 		}
18534 		mutex_exit(&pmportinfo->pmport_mutex);
18535 
18536 		/* Release PORT_BUSY flag */
18537 		mutex_enter(&cportinfo->cport_mutex);
18538 		cportinfo->cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
18539 		mutex_exit(&cportinfo->cport_mutex);
18540 	}
18541 
18542 	SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst,
18543 	    "[DONE] pmult event(s) on cport %d of controller %d",
18544 	    cport, ddi_get_instance(SATA_DIP(sata_hba_inst)));
18545 }
18546 
18547 /*
18548  * Process HBA power level change reported by HBA driver.
18549  * Not implemented at this time - event is ignored.
18550  */
18551 static void
18552 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst)
18553 {
18554 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18555 	    "Processing controller power level change", NULL);
18556 
18557 	/* Ignoring it for now */
18558 	mutex_enter(&sata_hba_inst->satahba_mutex);
18559 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
18560 	mutex_exit(&sata_hba_inst->satahba_mutex);
18561 }
18562 
18563 /*
18564  * Process port power level change reported by HBA driver.
18565  * Not implemented at this time - event is ignored.
18566  */
18567 static void
18568 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst,
18569     sata_address_t *saddr)
18570 {
18571 	sata_cport_info_t *cportinfo;
18572 
18573 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18574 	    "Processing port power level change", NULL);
18575 
18576 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18577 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18578 	/* Reset event flag */
18579 	cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
18580 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18581 }
18582 
18583 /*
18584  * Process port failure reported by HBA driver.
18585  * cports support only - no pmports.
18586  */
18587 static void
18588 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst,
18589     sata_address_t *saddr)
18590 {
18591 	sata_cport_info_t *cportinfo;
18592 
18593 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18594 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18595 	/* Reset event flag first */
18596 	cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED;
18597 	/* If the port is in SHUTDOWN or FAILED state, ignore this event. */
18598 	if ((cportinfo->cport_state &
18599 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) {
18600 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18601 		    cport_mutex);
18602 		return;
18603 	}
18604 	/* Fail the port */
18605 	cportinfo->cport_state = SATA_PSTATE_FAILED;
18606 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18607 	sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport);
18608 }
18609 
18610 /*
18611  * Device Reset Event processing.
18612  * The sequence is managed by 3 stage flags:
18613  * - reset event reported,
18614  * - reset event being processed,
18615  * - request to clear device reset state.
18616  *
18617  * NOTE: This function has to be entered with cport mutex held. It exits with
18618  * mutex held as well, but can release mutex during the processing.
18619  */
18620 static void
18621 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst,
18622     sata_address_t *saddr)
18623 {
18624 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
18625 	sata_drive_info_t *sdinfo;
18626 	sata_cport_info_t *cportinfo;
18627 	sata_device_t sata_device;
18628 	int rval_probe, rval_set;
18629 
18630 	/* We only care about host sata cport for now */
18631 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18632 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18633 	/*
18634 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
18635 	 * state, ignore reset event.
18636 	 */
18637 	if (((cportinfo->cport_state &
18638 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
18639 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
18640 		sdinfo->satadrv_event_flags &=
18641 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
18642 		return;
18643 	}
18644 
18645 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) ==
18646 	    SATA_DTYPE_PMULT)) {
18647 		/*
18648 		 * Should not happened: this is already handled in
18649 		 * sata_hba_event_notify()
18650 		 */
18651 		mutex_exit(&cportinfo->cport_mutex);
18652 		goto done;
18653 	}
18654 
18655 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) &
18656 	    SATA_VALID_DEV_TYPE) == 0) {
18657 		/*
18658 		 * This should not happen - coding error.
18659 		 * But we can recover, so do not panic, just clean up
18660 		 * and if in debug mode, log the message.
18661 		 */
18662 #ifdef SATA_DEBUG
18663 		sata_log(sata_hba_inst, CE_WARN,
18664 		    "sata_process_device_reset: "
18665 		    "Invalid device type with sdinfo!", NULL);
18666 #endif
18667 		sdinfo->satadrv_event_flags = 0;
18668 		return;
18669 	}
18670 
18671 #ifdef SATA_DEBUG
18672 	if ((sdinfo->satadrv_event_flags &
18673 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
18674 		/* Nothing to do */
18675 		/* Something is weird - why we are processing dev reset? */
18676 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18677 		    "No device reset event!!!!", NULL);
18678 
18679 		return;
18680 	}
18681 	if ((sdinfo->satadrv_event_flags &
18682 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
18683 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
18684 		/* Something is weird - new device reset event */
18685 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18686 		    "Overlapping device reset events!", NULL);
18687 	}
18688 #endif
18689 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18690 	    "Processing port %d device reset", saddr->cport);
18691 
18692 	/* Clear event flag */
18693 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
18694 
18695 	/* It seems that we always need to check the port state first */
18696 	sata_device.satadev_rev = SATA_DEVICE_REV;
18697 	sata_device.satadev_addr = *saddr;
18698 	/*
18699 	 * We have to exit mutex, because the HBA probe port function may
18700 	 * block on its own mutex.
18701 	 */
18702 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18703 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18704 	    (SATA_DIP(sata_hba_inst), &sata_device);
18705 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18706 	sata_update_port_info(sata_hba_inst, &sata_device);
18707 	if (rval_probe != SATA_SUCCESS) {
18708 		/* Something went wrong? Fail the port */
18709 		cportinfo->cport_state = SATA_PSTATE_FAILED;
18710 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18711 		if (sdinfo != NULL)
18712 			sdinfo->satadrv_event_flags = 0;
18713 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18714 		    cport_mutex);
18715 		SATA_LOG_D((sata_hba_inst, CE_WARN,
18716 		    "SATA port %d probing failed",
18717 		    saddr->cport));
18718 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
18719 		    saddr->cport)->cport_mutex);
18720 		return;
18721 	}
18722 	if ((sata_device.satadev_scr.sstatus  &
18723 	    SATA_PORT_DEVLINK_UP_MASK) !=
18724 	    SATA_PORT_DEVLINK_UP ||
18725 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
18726 		/*
18727 		 * No device to process, anymore. Some other event processing
18728 		 * would or have already performed port info cleanup.
18729 		 * To be safe (HBA may need it), request clearing device
18730 		 * reset condition.
18731 		 */
18732 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18733 		if (sdinfo != NULL) {
18734 			sdinfo->satadrv_event_flags &=
18735 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18736 			sdinfo->satadrv_event_flags |=
18737 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18738 		}
18739 		return;
18740 	}
18741 
18742 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
18743 	if (sdinfo == NULL) {
18744 		return;
18745 	}
18746 	if ((sdinfo->satadrv_event_flags &
18747 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
18748 		/*
18749 		 * Start tracking time for device feature restoration and
18750 		 * identification. Save current time (lbolt value).
18751 		 */
18752 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
18753 	}
18754 	/* Mark device reset processing as active */
18755 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
18756 
18757 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
18758 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18759 
18760 	rval_set = sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1);
18761 
18762 	if (rval_set  != SATA_SUCCESS) {
18763 		/*
18764 		 * Restoring drive setting failed.
18765 		 * Probe the port first, to check if the port state has changed
18766 		 */
18767 		sata_device.satadev_rev = SATA_DEVICE_REV;
18768 		sata_device.satadev_addr = *saddr;
18769 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
18770 		/* probe port */
18771 		rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18772 		    (SATA_DIP(sata_hba_inst), &sata_device);
18773 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18774 		    cport_mutex);
18775 		if (rval_probe == SATA_SUCCESS &&
18776 		    (sata_device.satadev_state &
18777 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
18778 		    (sata_device.satadev_scr.sstatus  &
18779 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
18780 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
18781 			/*
18782 			 * We may retry this a bit later - in-process reset
18783 			 * condition should be already set.
18784 			 * Track retry time for device identification.
18785 			 */
18786 			if ((cportinfo->cport_dev_type &
18787 			    SATA_VALID_DEV_TYPE) != 0 &&
18788 			    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL &&
18789 			    sdinfo->satadrv_reset_time != 0) {
18790 				clock_t cur_time = ddi_get_lbolt();
18791 				/*
18792 				 * If the retry time limit was not
18793 				 * exceeded, retry.
18794 				 */
18795 				if ((cur_time - sdinfo->satadrv_reset_time) <
18796 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
18797 					mutex_enter(
18798 					    &sata_hba_inst->satahba_mutex);
18799 					sata_hba_inst->satahba_event_flags |=
18800 					    SATA_EVNT_MAIN;
18801 					mutex_exit(
18802 					    &sata_hba_inst->satahba_mutex);
18803 					mutex_enter(&sata_mutex);
18804 					sata_event_pending |= SATA_EVNT_MAIN;
18805 					mutex_exit(&sata_mutex);
18806 					return;
18807 				}
18808 				if (rval_set == SATA_RETRY) {
18809 					/*
18810 					 * Setting drive features failed, but
18811 					 * the drive is still accessible,
18812 					 * so emit a warning message before
18813 					 * return.
18814 					 */
18815 					mutex_exit(&SATA_CPORT_INFO(
18816 					    sata_hba_inst,
18817 					    saddr->cport)->cport_mutex);
18818 					goto done;
18819 				}
18820 			}
18821 			/* Fail the drive */
18822 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
18823 
18824 			sata_log(sata_hba_inst, CE_WARN,
18825 			    "SATA device at port %d - device failed",
18826 			    saddr->cport);
18827 
18828 			DTRACE_PROBE(port_failed_f);
18829 		}
18830 		/*
18831 		 * No point of retrying - device failed or some other event
18832 		 * processing or already did or will do port info cleanup.
18833 		 * To be safe (HBA may need it),
18834 		 * request clearing device reset condition.
18835 		 */
18836 		sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
18837 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
18838 		sdinfo->satadrv_reset_time = 0;
18839 		return;
18840 	}
18841 done:
18842 	/*
18843 	 * If setting of drive features failed, but the drive is still
18844 	 * accessible, emit a warning message.
18845 	 */
18846 	if (rval_set == SATA_RETRY) {
18847 		sata_log(sata_hba_inst, CE_WARN,
18848 		    "SATA device at port %d - desired setting could not be "
18849 		    "restored after reset. Device may not operate as expected.",
18850 		    saddr->cport);
18851 	}
18852 	/*
18853 	 * Raise the flag indicating that the next sata command could
18854 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
18855 	 * reset is reported.
18856 	 */
18857 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18858 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
18859 		sdinfo->satadrv_reset_time = 0;
18860 		if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) {
18861 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
18862 			sdinfo->satadrv_event_flags &=
18863 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18864 			sdinfo->satadrv_event_flags |=
18865 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18866 		}
18867 	}
18868 }
18869 
18870 
18871 /*
18872  * Port Multiplier Port Device Reset Event processing.
18873  *
18874  * NOTE: This function has to be entered with pmport mutex held. It exits with
18875  * mutex held as well, but can release mutex during the processing.
18876  */
18877 static void
18878 sata_process_pmdevice_reset(sata_hba_inst_t *sata_hba_inst,
18879     sata_address_t *saddr)
18880 {
18881 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
18882 	sata_drive_info_t *sdinfo = NULL;
18883 	sata_cport_info_t *cportinfo = NULL;
18884 	sata_pmport_info_t *pmportinfo = NULL;
18885 	sata_pmult_info_t *pminfo = NULL;
18886 	sata_device_t sata_device;
18887 	uint8_t cport = saddr->cport;
18888 	uint8_t pmport = saddr->pmport;
18889 	int rval;
18890 
18891 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18892 	    "Processing drive reset at port %d:%d", cport, pmport);
18893 
18894 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
18895 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
18896 	sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport);
18897 
18898 	/*
18899 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
18900 	 * state, ignore reset event.
18901 	 */
18902 	if (((cportinfo->cport_state &
18903 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
18904 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
18905 		sdinfo->satadrv_event_flags &=
18906 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
18907 		return;
18908 	}
18909 
18910 	if ((pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
18911 		/*
18912 		 * This should not happen - coding error.
18913 		 * But we can recover, so do not panic, just clean up
18914 		 * and if in debug mode, log the message.
18915 		 */
18916 #ifdef SATA_DEBUG
18917 		sata_log(sata_hba_inst, CE_WARN,
18918 		    "sata_process_pmdevice_reset: "
18919 		    "Invalid device type with sdinfo!", NULL);
18920 #endif
18921 		sdinfo->satadrv_event_flags = 0;
18922 		return;
18923 	}
18924 
18925 #ifdef SATA_DEBUG
18926 	if ((sdinfo->satadrv_event_flags &
18927 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
18928 		/* Nothing to do */
18929 		/* Something is weird - why we are processing dev reset? */
18930 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18931 		    "No device reset event!!!!", NULL);
18932 
18933 		return;
18934 	}
18935 	if ((sdinfo->satadrv_event_flags &
18936 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
18937 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
18938 		/* Something is weird - new device reset event */
18939 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18940 		    "Overlapping device reset events!", NULL);
18941 	}
18942 #endif
18943 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18944 	    "Processing port %d:%d device reset", cport, pmport);
18945 
18946 	/* Clear event flag */
18947 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
18948 
18949 	/* It seems that we always need to check the port state first */
18950 	sata_device.satadev_rev = SATA_DEVICE_REV;
18951 	sata_device.satadev_addr = *saddr;
18952 	/*
18953 	 * We have to exit mutex, because the HBA probe port function may
18954 	 * block on its own mutex.
18955 	 */
18956 	mutex_exit(&pmportinfo->pmport_mutex);
18957 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18958 	    (SATA_DIP(sata_hba_inst), &sata_device);
18959 	mutex_enter(&pmportinfo->pmport_mutex);
18960 
18961 	sata_update_pmport_info(sata_hba_inst, &sata_device);
18962 	if (rval != SATA_SUCCESS) {
18963 		/* Something went wrong? Fail the port */
18964 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
18965 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18966 		    saddr->pmport);
18967 		if (sdinfo != NULL)
18968 			sdinfo->satadrv_event_flags = 0;
18969 		mutex_exit(&pmportinfo->pmport_mutex);
18970 		SATA_LOG_D((sata_hba_inst, CE_WARN,
18971 		    "SATA port %d:%d probing failed",
18972 		    saddr->cport, saddr->pmport));
18973 		mutex_enter(&pmportinfo->pmport_mutex);
18974 		return;
18975 	}
18976 	if ((sata_device.satadev_scr.sstatus  &
18977 	    SATA_PORT_DEVLINK_UP_MASK) !=
18978 	    SATA_PORT_DEVLINK_UP ||
18979 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
18980 		/*
18981 		 * No device to process, anymore. Some other event processing
18982 		 * would or have already performed port info cleanup.
18983 		 * To be safe (HBA may need it), request clearing device
18984 		 * reset condition.
18985 		 */
18986 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
18987 		    saddr->pmport);
18988 		if (sdinfo != NULL) {
18989 			sdinfo->satadrv_event_flags &=
18990 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
18991 			/* must clear flags on cport */
18992 			pminfo = SATA_PMULT_INFO(sata_hba_inst,
18993 			    saddr->cport);
18994 			pminfo->pmult_event_flags |=
18995 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18996 		}
18997 		return;
18998 	}
18999 
19000 	sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
19001 	    saddr->pmport);
19002 	if (sdinfo == NULL) {
19003 		return;
19004 	}
19005 	if ((sdinfo->satadrv_event_flags &
19006 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
19007 		/*
19008 		 * Start tracking time for device feature restoration and
19009 		 * identification. Save current time (lbolt value).
19010 		 */
19011 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
19012 	}
19013 	/* Mark device reset processing as active */
19014 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
19015 
19016 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
19017 	mutex_exit(&pmportinfo->pmport_mutex);
19018 
19019 	if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) ==
19020 	    SATA_FAILURE) {
19021 		/*
19022 		 * Restoring drive setting failed.
19023 		 * Probe the port first, to check if the port state has changed
19024 		 */
19025 		sata_device.satadev_rev = SATA_DEVICE_REV;
19026 		sata_device.satadev_addr = *saddr;
19027 		sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
19028 
19029 		/* probe port */
19030 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19031 		    (SATA_DIP(sata_hba_inst), &sata_device);
19032 		mutex_enter(&pmportinfo->pmport_mutex);
19033 		if (rval == SATA_SUCCESS &&
19034 		    (sata_device.satadev_state &
19035 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
19036 		    (sata_device.satadev_scr.sstatus  &
19037 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
19038 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
19039 			/*
19040 			 * We may retry this a bit later - in-process reset
19041 			 * condition should be already set.
19042 			 * Track retry time for device identification.
19043 			 */
19044 			if ((pmportinfo->pmport_dev_type &
19045 			    SATA_VALID_DEV_TYPE) != 0 &&
19046 			    SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL &&
19047 			    sdinfo->satadrv_reset_time != 0) {
19048 				clock_t cur_time = ddi_get_lbolt();
19049 				/*
19050 				 * If the retry time limit was not
19051 				 * exceeded, retry.
19052 				 */
19053 				if ((cur_time - sdinfo->satadrv_reset_time) <
19054 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
19055 					mutex_enter(
19056 					    &sata_hba_inst->satahba_mutex);
19057 					sata_hba_inst->satahba_event_flags |=
19058 					    SATA_EVNT_MAIN;
19059 					mutex_exit(
19060 					    &sata_hba_inst->satahba_mutex);
19061 					mutex_enter(&sata_mutex);
19062 					sata_event_pending |= SATA_EVNT_MAIN;
19063 					mutex_exit(&sata_mutex);
19064 					return;
19065 				}
19066 			}
19067 			/* Fail the drive */
19068 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
19069 
19070 			sata_log(sata_hba_inst, CE_WARN,
19071 			    "SATA device at port %d:%d - device failed",
19072 			    saddr->cport, saddr->pmport);
19073 		} else {
19074 			/*
19075 			 * No point of retrying - some other event processing
19076 			 * would or already did port info cleanup.
19077 			 * To be safe (HBA may need it),
19078 			 * request clearing device reset condition.
19079 			 */
19080 			sdinfo->satadrv_event_flags |=
19081 			    SATA_EVNT_CLEAR_DEVICE_RESET;
19082 		}
19083 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
19084 		sdinfo->satadrv_reset_time = 0;
19085 		return;
19086 	}
19087 	/*
19088 	 * Raise the flag indicating that the next sata command could
19089 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
19090 	 * reset is reported.
19091 	 */
19092 	mutex_enter(&pmportinfo->pmport_mutex);
19093 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
19094 		sdinfo->satadrv_reset_time = 0;
19095 		if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) {
19096 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19097 			sdinfo->satadrv_event_flags &=
19098 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
19099 			/* must clear flags on cport */
19100 			pminfo = SATA_PMULT_INFO(sata_hba_inst,
19101 			    saddr->cport);
19102 			pminfo->pmult_event_flags |=
19103 			    SATA_EVNT_CLEAR_DEVICE_RESET;
19104 		}
19105 	}
19106 }
19107 
19108 /*
19109  * Port Link Events processing.
19110  * Every link established event may involve device reset (due to
19111  * COMRESET signal, equivalent of the hard reset) so arbitrarily
19112  * set device reset event for an attached device (if any).
19113  * If the port is in SHUTDOWN or FAILED state, ignore link events.
19114  *
19115  * The link established event processing varies, depending on the state
19116  * of the target node, HBA hotplugging capabilities, state of the port.
19117  * If the link is not active, the link established event is ignored.
19118  * If HBA cannot detect device attachment and there is no target node,
19119  * the link established event triggers device attach event processing.
19120  * Else, link established event triggers device reset event processing.
19121  *
19122  * The link lost event processing varies, depending on a HBA hotplugging
19123  * capability and the state of the port (link active or not active).
19124  * If the link is active, the lost link event is ignored.
19125  * If HBA cannot detect device removal, the lost link event triggers
19126  * device detached event processing after link lost timeout.
19127  * Else, the event is ignored.
19128  *
19129  * NOTE: Port multiplier ports events are handled by
19130  * sata_process_pmport_link_events();
19131  */
19132 static void
19133 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst,
19134     sata_address_t *saddr)
19135 {
19136 	sata_device_t sata_device;
19137 	sata_cport_info_t *cportinfo;
19138 	sata_drive_info_t *sdinfo;
19139 	uint32_t event_flags;
19140 	int rval;
19141 
19142 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19143 	    "Processing port %d link event(s)", saddr->cport);
19144 
19145 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19146 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19147 	event_flags = cportinfo->cport_event_flags;
19148 
19149 	/* Reset event flags first */
19150 	cportinfo->cport_event_flags &=
19151 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
19152 
19153 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
19154 	if ((cportinfo->cport_state &
19155 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19156 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19157 		    cport_mutex);
19158 		return;
19159 	}
19160 
19161 	/*
19162 	 * For the sanity sake get current port state.
19163 	 * Set device address only. Other sata_device fields should be
19164 	 * set by HBA driver.
19165 	 */
19166 	sata_device.satadev_rev = SATA_DEVICE_REV;
19167 	sata_device.satadev_addr = *saddr;
19168 	/*
19169 	 * We have to exit mutex, because the HBA probe port function may
19170 	 * block on its own mutex.
19171 	 */
19172 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19173 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19174 	    (SATA_DIP(sata_hba_inst), &sata_device);
19175 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19176 	sata_update_port_info(sata_hba_inst, &sata_device);
19177 	if (rval != SATA_SUCCESS) {
19178 		/* Something went wrong? Fail the port */
19179 		cportinfo->cport_state = SATA_PSTATE_FAILED;
19180 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19181 		    cport_mutex);
19182 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19183 		    "SATA port %d probing failed",
19184 		    saddr->cport));
19185 		/*
19186 		 * We may want to release device info structure, but
19187 		 * it is not necessary.
19188 		 */
19189 		return;
19190 	} else {
19191 		/* port probed successfully */
19192 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19193 	}
19194 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
19195 
19196 		if ((sata_device.satadev_scr.sstatus &
19197 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
19198 			/* Ignore event */
19199 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19200 			    "Ignoring port %d link established event - "
19201 			    "link down",
19202 			    saddr->cport);
19203 			goto linklost;
19204 		}
19205 
19206 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19207 		    "Processing port %d link established event",
19208 		    saddr->cport);
19209 
19210 		/*
19211 		 * For the sanity sake check if a device is attached - check
19212 		 * return state of a port probing.
19213 		 */
19214 		if (sata_device.satadev_type != SATA_DTYPE_NONE) {
19215 			/*
19216 			 * HBA port probe indicated that there is a device
19217 			 * attached. Check if the framework had device info
19218 			 * structure attached for this device.
19219 			 */
19220 			if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
19221 				ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) !=
19222 				    NULL);
19223 
19224 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19225 				if ((sdinfo->satadrv_type &
19226 				    SATA_VALID_DEV_TYPE) != 0) {
19227 					/*
19228 					 * Dev info structure is present.
19229 					 * If dev_type is set to known type in
19230 					 * the framework's drive info struct
19231 					 * then the device existed before and
19232 					 * the link was probably lost
19233 					 * momentarily - in such case
19234 					 * we may want to check device
19235 					 * identity.
19236 					 * Identity check is not supported now.
19237 					 *
19238 					 * Link established event
19239 					 * triggers device reset event.
19240 					 */
19241 					(SATA_CPORTINFO_DRV_INFO(cportinfo))->
19242 					    satadrv_event_flags |=
19243 					    SATA_EVNT_DEVICE_RESET;
19244 				}
19245 			} else if (cportinfo->cport_dev_type ==
19246 			    SATA_DTYPE_NONE) {
19247 				/*
19248 				 * We got new device attached! If HBA does not
19249 				 * generate device attached events, trigger it
19250 				 * here.
19251 				 */
19252 				if (!(SATA_FEATURES(sata_hba_inst) &
19253 				    SATA_CTLF_HOTPLUG)) {
19254 					cportinfo->cport_event_flags |=
19255 					    SATA_EVNT_DEVICE_ATTACHED;
19256 				}
19257 			}
19258 			/* Reset link lost timeout */
19259 			cportinfo->cport_link_lost_time = 0;
19260 		}
19261 	}
19262 linklost:
19263 	if (event_flags & SATA_EVNT_LINK_LOST) {
19264 		if ((sata_device.satadev_scr.sstatus &
19265 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
19266 			/* Ignore event */
19267 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19268 			    "Ignoring port %d link lost event - link is up",
19269 			    saddr->cport);
19270 			goto done;
19271 		}
19272 #ifdef SATA_DEBUG
19273 		if (cportinfo->cport_link_lost_time == 0) {
19274 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19275 			    "Processing port %d link lost event",
19276 			    saddr->cport);
19277 		}
19278 #endif
19279 		/*
19280 		 * When HBA cannot generate device attached/detached events,
19281 		 * we need to track link lost time and eventually generate
19282 		 * device detach event.
19283 		 */
19284 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
19285 			/* We are tracking link lost time */
19286 			if (cportinfo->cport_link_lost_time == 0) {
19287 				/* save current time (lbolt value) */
19288 				cportinfo->cport_link_lost_time =
19289 				    ddi_get_lbolt();
19290 				/* just keep link lost event */
19291 				cportinfo->cport_event_flags |=
19292 				    SATA_EVNT_LINK_LOST;
19293 			} else {
19294 				clock_t cur_time = ddi_get_lbolt();
19295 				if ((cur_time -
19296 				    cportinfo->cport_link_lost_time) >=
19297 				    drv_usectohz(
19298 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
19299 					/* trigger device detach event */
19300 					cportinfo->cport_event_flags |=
19301 					    SATA_EVNT_DEVICE_DETACHED;
19302 					cportinfo->cport_link_lost_time = 0;
19303 					SATADBG1(SATA_DBG_EVENTS,
19304 					    sata_hba_inst,
19305 					    "Triggering port %d "
19306 					    "device detached event",
19307 					    saddr->cport);
19308 				} else {
19309 					/* keep link lost event */
19310 					cportinfo->cport_event_flags |=
19311 					    SATA_EVNT_LINK_LOST;
19312 				}
19313 			}
19314 		}
19315 		/*
19316 		 * We could change port state to disable/delay access to
19317 		 * the attached device until the link is recovered.
19318 		 */
19319 	}
19320 done:
19321 	event_flags = cportinfo->cport_event_flags;
19322 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19323 	if (event_flags != 0) {
19324 		mutex_enter(&sata_hba_inst->satahba_mutex);
19325 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19326 		mutex_exit(&sata_hba_inst->satahba_mutex);
19327 		mutex_enter(&sata_mutex);
19328 		sata_event_pending |= SATA_EVNT_MAIN;
19329 		mutex_exit(&sata_mutex);
19330 	}
19331 }
19332 
19333 /*
19334  * Port Multiplier Port Link Events processing.
19335  */
19336 static void
19337 sata_process_pmport_link_events(sata_hba_inst_t *sata_hba_inst,
19338     sata_address_t *saddr)
19339 {
19340 	sata_device_t sata_device;
19341 	sata_pmport_info_t *pmportinfo = NULL;
19342 	sata_drive_info_t *sdinfo = NULL;
19343 	uint32_t event_flags;
19344 	uint8_t cport = saddr->cport;
19345 	uint8_t pmport = saddr->pmport;
19346 	int rval;
19347 
19348 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19349 	    "Processing port %d:%d link event(s)",
19350 	    cport, pmport);
19351 
19352 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
19353 	mutex_enter(&pmportinfo->pmport_mutex);
19354 	event_flags = pmportinfo->pmport_event_flags;
19355 
19356 	/* Reset event flags first */
19357 	pmportinfo->pmport_event_flags &=
19358 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
19359 
19360 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
19361 	if ((pmportinfo->pmport_state &
19362 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19363 		mutex_exit(&pmportinfo->pmport_mutex);
19364 		return;
19365 	}
19366 
19367 	/*
19368 	 * For the sanity sake get current port state.
19369 	 * Set device address only. Other sata_device fields should be
19370 	 * set by HBA driver.
19371 	 */
19372 	sata_device.satadev_rev = SATA_DEVICE_REV;
19373 	sata_device.satadev_addr = *saddr;
19374 	/*
19375 	 * We have to exit mutex, because the HBA probe port function may
19376 	 * block on its own mutex.
19377 	 */
19378 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19379 	    saddr->pmport));
19380 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19381 	    (SATA_DIP(sata_hba_inst), &sata_device);
19382 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19383 	    saddr->pmport));
19384 	sata_update_pmport_info(sata_hba_inst, &sata_device);
19385 	if (rval != SATA_SUCCESS) {
19386 		/* Something went wrong? Fail the port */
19387 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
19388 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19389 		    saddr->pmport));
19390 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19391 		    "SATA port %d:%d probing failed",
19392 		    saddr->cport, saddr->pmport));
19393 		/*
19394 		 * We may want to release device info structure, but
19395 		 * it is not necessary.
19396 		 */
19397 		return;
19398 	} else {
19399 		/* port probed successfully */
19400 		pmportinfo->pmport_state |=
19401 		    SATA_STATE_PROBED | SATA_STATE_READY;
19402 	}
19403 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst,
19404 	    saddr->cport, saddr->pmport));
19405 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst,
19406 	    saddr->cport, saddr->pmport));
19407 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
19408 
19409 		if ((sata_device.satadev_scr.sstatus &
19410 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
19411 			/* Ignore event */
19412 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19413 			    "Ignoring port %d:%d link established event - "
19414 			    "link down",
19415 			    saddr->cport, saddr->pmport);
19416 			goto linklost;
19417 		}
19418 
19419 		SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19420 		    "Processing port %d:%d link established event",
19421 		    cport, pmport);
19422 
19423 		/*
19424 		 * For the sanity sake check if a device is attached - check
19425 		 * return state of a port probing.
19426 		 */
19427 		if (sata_device.satadev_type != SATA_DTYPE_NONE &&
19428 		    sata_device.satadev_type != SATA_DTYPE_PMULT) {
19429 			/*
19430 			 * HBA port probe indicated that there is a device
19431 			 * attached. Check if the framework had device info
19432 			 * structure attached for this device.
19433 			 */
19434 			if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
19435 				ASSERT(SATA_PMPORTINFO_DRV_INFO(pmportinfo) !=
19436 				    NULL);
19437 
19438 				sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19439 				if ((sdinfo->satadrv_type &
19440 				    SATA_VALID_DEV_TYPE) != 0) {
19441 					/*
19442 					 * Dev info structure is present.
19443 					 * If dev_type is set to known type in
19444 					 * the framework's drive info struct
19445 					 * then the device existed before and
19446 					 * the link was probably lost
19447 					 * momentarily - in such case
19448 					 * we may want to check device
19449 					 * identity.
19450 					 * Identity check is not supported now.
19451 					 *
19452 					 * Link established event
19453 					 * triggers device reset event.
19454 					 */
19455 					(SATA_PMPORTINFO_DRV_INFO(pmportinfo))->
19456 					    satadrv_event_flags |=
19457 					    SATA_EVNT_DEVICE_RESET;
19458 				}
19459 			} else if (pmportinfo->pmport_dev_type ==
19460 			    SATA_DTYPE_NONE) {
19461 				/*
19462 				 * We got new device attached! If HBA does not
19463 				 * generate device attached events, trigger it
19464 				 * here.
19465 				 */
19466 				if (!(SATA_FEATURES(sata_hba_inst) &
19467 				    SATA_CTLF_HOTPLUG)) {
19468 					pmportinfo->pmport_event_flags |=
19469 					    SATA_EVNT_DEVICE_ATTACHED;
19470 				}
19471 			}
19472 			/* Reset link lost timeout */
19473 			pmportinfo->pmport_link_lost_time = 0;
19474 		}
19475 	}
19476 linklost:
19477 	if (event_flags & SATA_EVNT_LINK_LOST) {
19478 #ifdef SATA_DEBUG
19479 		if (pmportinfo->pmport_link_lost_time == 0) {
19480 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19481 			    "Processing port %d:%d link lost event",
19482 			    saddr->cport, saddr->pmport);
19483 		}
19484 #endif
19485 		if ((sata_device.satadev_scr.sstatus &
19486 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
19487 			/* Ignore event */
19488 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19489 			    "Ignoring port %d:%d link lost event - link is up",
19490 			    saddr->cport, saddr->pmport);
19491 			goto done;
19492 		}
19493 		/*
19494 		 * When HBA cannot generate device attached/detached events,
19495 		 * we need to track link lost time and eventually generate
19496 		 * device detach event.
19497 		 */
19498 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
19499 			/* We are tracking link lost time */
19500 			if (pmportinfo->pmport_link_lost_time == 0) {
19501 				/* save current time (lbolt value) */
19502 				pmportinfo->pmport_link_lost_time =
19503 				    ddi_get_lbolt();
19504 				/* just keep link lost event */
19505 				pmportinfo->pmport_event_flags |=
19506 				    SATA_EVNT_LINK_LOST;
19507 			} else {
19508 				clock_t cur_time = ddi_get_lbolt();
19509 				if ((cur_time -
19510 				    pmportinfo->pmport_link_lost_time) >=
19511 				    drv_usectohz(
19512 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
19513 					/* trigger device detach event */
19514 					pmportinfo->pmport_event_flags |=
19515 					    SATA_EVNT_DEVICE_DETACHED;
19516 					pmportinfo->pmport_link_lost_time = 0;
19517 					SATADBG2(SATA_DBG_EVENTS,
19518 					    sata_hba_inst,
19519 					    "Triggering port %d:%d "
19520 					    "device detached event",
19521 					    saddr->cport, saddr->pmport);
19522 				} else {
19523 					/* keep link lost event */
19524 					pmportinfo->pmport_event_flags |=
19525 					    SATA_EVNT_LINK_LOST;
19526 				}
19527 			}
19528 		}
19529 		/*
19530 		 * We could change port state to disable/delay access to
19531 		 * the attached device until the link is recovered.
19532 		 */
19533 	}
19534 done:
19535 	event_flags = pmportinfo->pmport_event_flags;
19536 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
19537 	    saddr->pmport));
19538 	if (event_flags != 0) {
19539 		mutex_enter(&sata_hba_inst->satahba_mutex);
19540 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19541 		mutex_exit(&sata_hba_inst->satahba_mutex);
19542 		mutex_enter(&sata_mutex);
19543 		sata_event_pending |= SATA_EVNT_MAIN;
19544 		mutex_exit(&sata_mutex);
19545 	}
19546 }
19547 
19548 /*
19549  * Device Detached Event processing.
19550  * Port is probed to find if a device is really gone. If so,
19551  * the device info structure is detached from the SATA port info structure
19552  * and released.
19553  * Port status is updated.
19554  *
19555  * NOTE: Port multiplier ports events are handled by
19556  * sata_process_pmdevice_detached()
19557  */
19558 static void
19559 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst,
19560     sata_address_t *saddr)
19561 {
19562 	sata_cport_info_t *cportinfo;
19563 	sata_pmport_info_t *pmportinfo;
19564 	sata_drive_info_t *sdevinfo;
19565 	sata_device_t sata_device;
19566 	sata_address_t pmport_addr;
19567 	char name[16];
19568 	uint8_t cport = saddr->cport;
19569 	int npmport;
19570 	int rval;
19571 
19572 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19573 	    "Processing port %d device detached", saddr->cport);
19574 
19575 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19576 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19577 	/* Clear event flag */
19578 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
19579 
19580 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
19581 	if ((cportinfo->cport_state &
19582 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19583 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19584 		    cport_mutex);
19585 		return;
19586 	}
19587 	/* For sanity, re-probe the port */
19588 	sata_device.satadev_rev = SATA_DEVICE_REV;
19589 	sata_device.satadev_addr = *saddr;
19590 
19591 	/*
19592 	 * We have to exit mutex, because the HBA probe port function may
19593 	 * block on its own mutex.
19594 	 */
19595 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19596 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19597 	    (SATA_DIP(sata_hba_inst), &sata_device);
19598 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19599 	sata_update_port_info(sata_hba_inst, &sata_device);
19600 	if (rval != SATA_SUCCESS) {
19601 		/* Something went wrong? Fail the port */
19602 		cportinfo->cport_state = SATA_PSTATE_FAILED;
19603 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19604 		    cport_mutex);
19605 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19606 		    "SATA port %d probing failed",
19607 		    saddr->cport));
19608 		/*
19609 		 * We may want to release device info structure, but
19610 		 * it is not necessary.
19611 		 */
19612 		return;
19613 	} else {
19614 		/* port probed successfully */
19615 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19616 	}
19617 	/*
19618 	 * Check if a device is still attached. For sanity, check also
19619 	 * link status - if no link, there is no device.
19620 	 */
19621 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
19622 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
19623 	    SATA_DTYPE_NONE) {
19624 		/*
19625 		 * Device is still attached - ignore detach event.
19626 		 */
19627 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19628 		    cport_mutex);
19629 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19630 		    "Ignoring detach - device still attached to port %d",
19631 		    sata_device.satadev_addr.cport);
19632 		return;
19633 	}
19634 	/*
19635 	 * We need to detach and release device info structure here
19636 	 */
19637 	if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
19638 		/*
19639 		 * A port-multiplier is removed.
19640 		 *
19641 		 * Calling sata_process_pmdevice_detached() does not work
19642 		 * here. The port multiplier is gone, so we cannot probe
19643 		 * sub-port any more and all pmult-related data structure must
19644 		 * be de-allocated immediately. Following structure of every
19645 		 * implemented sub-port behind the pmult are required to
19646 		 * released.
19647 		 *
19648 		 *   - attachment point
19649 		 *   - target node
19650 		 *   - sata_drive_info
19651 		 *   - sata_pmport_info
19652 		 */
19653 		for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst,
19654 		    cport); npmport ++) {
19655 			SATADBG2(SATA_DBG_PMULT|SATA_DBG_EVENTS_PROC,
19656 			    sata_hba_inst,
19657 			    "Detaching target node at port %d:%d",
19658 			    cport, npmport);
19659 
19660 			mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
19661 
19662 			/* Remove attachment point. */
19663 			name[0] = '\0';
19664 			(void) sprintf(name, "%d.%d", cport, npmport);
19665 			ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name);
19666 			sata_log(sata_hba_inst, CE_NOTE,
19667 			    "Remove attachment point of port %d:%d",
19668 			    cport, npmport);
19669 
19670 			/* Remove target node */
19671 			pmport_addr.cport = cport;
19672 			pmport_addr.pmport = (uint8_t)npmport;
19673 			pmport_addr.qual = SATA_ADDR_PMPORT;
19674 			sata_remove_target_node(sata_hba_inst, &pmport_addr);
19675 
19676 			mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
19677 
19678 			/* Release sata_pmport_info & sata_drive_info. */
19679 			pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
19680 			    cport, npmport);
19681 			ASSERT(pmportinfo != NULL);
19682 
19683 			sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19684 			if (sdevinfo != NULL) {
19685 				(void) kmem_free((void *) sdevinfo,
19686 				    sizeof (sata_drive_info_t));
19687 			}
19688 
19689 			/* Release sata_pmport_info at last */
19690 			(void) kmem_free((void *) pmportinfo,
19691 			    sizeof (sata_pmport_info_t));
19692 		}
19693 
19694 		/* Finally, release sata_pmult_info */
19695 		(void) kmem_free((void *)
19696 		    SATA_CPORTINFO_PMULT_INFO(cportinfo),
19697 		    sizeof (sata_pmult_info_t));
19698 		SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL;
19699 
19700 		sata_log(sata_hba_inst, CE_WARN,
19701 		    "SATA port-multiplier detached at port %d", cport);
19702 
19703 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19704 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19705 		    saddr->cport)->cport_mutex);
19706 	} else {
19707 		if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19708 			sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19709 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
19710 			(void) kmem_free((void *)sdevinfo,
19711 			    sizeof (sata_drive_info_t));
19712 		}
19713 		sata_log(sata_hba_inst, CE_WARN,
19714 		    "SATA device detached at port %d", cport);
19715 
19716 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19717 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19718 		    saddr->cport)->cport_mutex);
19719 
19720 		/*
19721 		 * Try to offline a device and remove target node
19722 		 * if it still exists
19723 		 */
19724 		sata_remove_target_node(sata_hba_inst, saddr);
19725 	}
19726 
19727 
19728 	/*
19729 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19730 	 * with the hint: SE_HINT_REMOVE
19731 	 */
19732 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
19733 }
19734 
19735 /*
19736  * Port Multiplier Port Device Deattached Event processing.
19737  *
19738  * NOTE: No Mutex should be hold.
19739  */
19740 static void
19741 sata_process_pmdevice_detached(sata_hba_inst_t *sata_hba_inst,
19742     sata_address_t *saddr)
19743 {
19744 	sata_pmport_info_t *pmportinfo;
19745 	sata_drive_info_t *sdevinfo;
19746 	sata_device_t sata_device;
19747 	int rval;
19748 	uint8_t cport, pmport;
19749 
19750 	cport = saddr->cport;
19751 	pmport = saddr->pmport;
19752 
19753 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19754 	    "Processing port %d:%d device detached",
19755 	    cport, pmport);
19756 
19757 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
19758 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19759 
19760 	/* Clear event flag */
19761 	pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
19762 
19763 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
19764 	if ((pmportinfo->pmport_state &
19765 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19766 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19767 		return;
19768 	}
19769 	/* For sanity, re-probe the port */
19770 	sata_device.satadev_rev = SATA_DEVICE_REV;
19771 	sata_device.satadev_addr = *saddr;
19772 
19773 	/*
19774 	 * We have to exit mutex, because the HBA probe port function may
19775 	 * block on its own mutex.
19776 	 */
19777 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19778 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19779 	    (SATA_DIP(sata_hba_inst), &sata_device);
19780 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19781 	sata_update_pmport_info(sata_hba_inst, &sata_device);
19782 	if (rval != SATA_SUCCESS) {
19783 		/* Something went wrong? Fail the port */
19784 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
19785 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19786 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19787 		    "SATA port %d:%d probing failed",
19788 		    saddr->pmport));
19789 		/*
19790 		 * We may want to release device info structure, but
19791 		 * it is not necessary.
19792 		 */
19793 		return;
19794 	} else {
19795 		/* port probed successfully */
19796 		pmportinfo->pmport_state |=
19797 		    SATA_STATE_PROBED | SATA_STATE_READY;
19798 	}
19799 	/*
19800 	 * Check if a device is still attached. For sanity, check also
19801 	 * link status - if no link, there is no device.
19802 	 */
19803 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
19804 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
19805 	    SATA_DTYPE_NONE) {
19806 		/*
19807 		 * Device is still attached - ignore detach event.
19808 		 */
19809 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19810 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19811 		    "Ignoring detach - device still attached to port %d",
19812 		    sata_device.satadev_addr.pmport);
19813 		return;
19814 	}
19815 	/*
19816 	 * We need to detach and release device info structure here
19817 	 */
19818 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
19819 		sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
19820 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
19821 		(void) kmem_free((void *)sdevinfo,
19822 		    sizeof (sata_drive_info_t));
19823 	}
19824 	pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
19825 	/*
19826 	 * Device cannot be reached anymore, even if the target node may be
19827 	 * still present.
19828 	 */
19829 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
19830 
19831 	/*
19832 	 * Try to offline a device and remove target node if it still exists
19833 	 */
19834 	sata_remove_target_node(sata_hba_inst, saddr);
19835 
19836 	/*
19837 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19838 	 * with the hint: SE_HINT_REMOVE
19839 	 */
19840 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
19841 }
19842 
19843 
19844 /*
19845  * Device Attached Event processing.
19846  * Port state is checked to verify that a device is really attached. If so,
19847  * the device info structure is created and attached to the SATA port info
19848  * structure.
19849  *
19850  * If attached device cannot be identified or set-up, the retry for the
19851  * attach processing is set-up. Subsequent daemon run would try again to
19852  * identify the device, until the time limit is reached
19853  * (SATA_DEV_IDENTIFY_TIMEOUT).
19854  *
19855  * This function cannot be called in interrupt context (it may sleep).
19856  *
19857  * NOTE: Port multiplier ports events are handled by
19858  * sata_process_pmdevice_attached()
19859  */
19860 static void
19861 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst,
19862     sata_address_t *saddr)
19863 {
19864 	sata_cport_info_t *cportinfo = NULL;
19865 	sata_drive_info_t *sdevinfo = NULL;
19866 	sata_pmult_info_t *pmultinfo = NULL;
19867 	sata_pmport_info_t *pmportinfo = NULL;
19868 	sata_device_t sata_device;
19869 	dev_info_t *tdip;
19870 	uint32_t event_flags = 0, pmult_event_flags = 0;
19871 	int rval;
19872 	int npmport;
19873 
19874 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19875 	    "Processing port %d device attached", saddr->cport);
19876 
19877 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19878 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19879 
19880 	/* Clear attach event flag first */
19881 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
19882 
19883 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
19884 	if ((cportinfo->cport_state &
19885 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
19886 		cportinfo->cport_dev_attach_time = 0;
19887 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19888 		    cport_mutex);
19889 		return;
19890 	}
19891 
19892 	/*
19893 	 * If the sata_drive_info structure is found attached to the port info,
19894 	 * despite the fact the device was removed and now it is re-attached,
19895 	 * the old drive info structure was not removed.
19896 	 * Arbitrarily release device info structure.
19897 	 */
19898 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
19899 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
19900 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
19901 		(void) kmem_free((void *)sdevinfo,
19902 		    sizeof (sata_drive_info_t));
19903 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19904 		    "Arbitrarily detaching old device info.", NULL);
19905 	}
19906 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
19907 
19908 	/* For sanity, re-probe the port */
19909 	sata_device.satadev_rev = SATA_DEVICE_REV;
19910 	sata_device.satadev_addr = *saddr;
19911 
19912 	/*
19913 	 * We have to exit mutex, because the HBA probe port function may
19914 	 * block on its own mutex.
19915 	 */
19916 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19917 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
19918 	    (SATA_DIP(sata_hba_inst), &sata_device);
19919 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19920 	sata_update_port_info(sata_hba_inst, &sata_device);
19921 	if (rval != SATA_SUCCESS) {
19922 		/* Something went wrong? Fail the port */
19923 		cportinfo->cport_state = SATA_PSTATE_FAILED;
19924 		cportinfo->cport_dev_attach_time = 0;
19925 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19926 		    cport_mutex);
19927 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19928 		    "SATA port %d probing failed",
19929 		    saddr->cport));
19930 		return;
19931 	} else {
19932 		/* port probed successfully */
19933 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
19934 	}
19935 	/*
19936 	 * Check if a device is still attached. For sanity, check also
19937 	 * link status - if no link, there is no device.
19938 	 */
19939 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
19940 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
19941 	    SATA_DTYPE_NONE) {
19942 		/*
19943 		 * No device - ignore attach event.
19944 		 */
19945 		cportinfo->cport_dev_attach_time = 0;
19946 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19947 		    cport_mutex);
19948 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19949 		    "Ignoring attach - no device connected to port %d",
19950 		    sata_device.satadev_addr.cport);
19951 		return;
19952 	}
19953 
19954 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19955 	/*
19956 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
19957 	 * with the hint: SE_HINT_INSERT
19958 	 */
19959 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
19960 
19961 	/*
19962 	 * Port reprobing will take care of the creation of the device
19963 	 * info structure and determination of the device type.
19964 	 */
19965 	sata_device.satadev_addr = *saddr;
19966 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
19967 	    SATA_DEV_IDENTIFY_NORETRY);
19968 
19969 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19970 	    cport_mutex);
19971 	if ((cportinfo->cport_state & SATA_STATE_READY) &&
19972 	    (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) {
19973 		/* Some device is attached to the port */
19974 		if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) {
19975 			/*
19976 			 * A device was not successfully attached.
19977 			 * Track retry time for device identification.
19978 			 */
19979 			if (cportinfo->cport_dev_attach_time != 0) {
19980 				clock_t cur_time = ddi_get_lbolt();
19981 				/*
19982 				 * If the retry time limit was not exceeded,
19983 				 * reinstate attach event.
19984 				 */
19985 				if ((cur_time -
19986 				    cportinfo->cport_dev_attach_time) <
19987 				    drv_usectohz(
19988 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
19989 					/* OK, restore attach event */
19990 					cportinfo->cport_event_flags |=
19991 					    SATA_EVNT_DEVICE_ATTACHED;
19992 				} else {
19993 					/* Timeout - cannot identify device */
19994 					cportinfo->cport_dev_attach_time = 0;
19995 					sata_log(sata_hba_inst,
19996 					    CE_WARN,
19997 					    "Could not identify SATA device "
19998 					    "at port %d",
19999 					    saddr->cport);
20000 				}
20001 			} else {
20002 				/*
20003 				 * Start tracking time for device
20004 				 * identification.
20005 				 * Save current time (lbolt value).
20006 				 */
20007 				cportinfo->cport_dev_attach_time =
20008 				    ddi_get_lbolt();
20009 				/* Restore attach event */
20010 				cportinfo->cport_event_flags |=
20011 				    SATA_EVNT_DEVICE_ATTACHED;
20012 			}
20013 		} else if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
20014 			cportinfo->cport_dev_attach_time = 0;
20015 			sata_log(sata_hba_inst, CE_NOTE,
20016 			    "SATA port-multiplier detected at port %d",
20017 			    saddr->cport);
20018 
20019 			if (SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL) {
20020 				/* Log the info of new port multiplier */
20021 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20022 				    saddr->cport)->cport_mutex);
20023 				sata_show_pmult_info(sata_hba_inst,
20024 				    &sata_device);
20025 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20026 				    saddr->cport)->cport_mutex);
20027 			}
20028 
20029 			ASSERT(SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL);
20030 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
20031 			for (npmport = 0; npmport <
20032 			    pmultinfo->pmult_num_dev_ports; npmport++) {
20033 				pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
20034 				    saddr->cport, npmport);
20035 				ASSERT(pmportinfo != NULL);
20036 
20037 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20038 				    saddr->cport)->cport_mutex);
20039 				mutex_enter(&pmportinfo->pmport_mutex);
20040 				/* Marked all pmports with link events. */
20041 				pmportinfo->pmport_event_flags =
20042 				    SATA_EVNT_LINK_ESTABLISHED;
20043 				pmult_event_flags |=
20044 				    pmportinfo->pmport_event_flags;
20045 				mutex_exit(&pmportinfo->pmport_mutex);
20046 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20047 				    saddr->cport)->cport_mutex);
20048 			}
20049 			/* Auto-online is not available for PMult now. */
20050 
20051 		} else {
20052 			/*
20053 			 * If device was successfully attached, the subsequent
20054 			 * action depends on a state of the
20055 			 * sata_auto_online variable. If it is set to zero.
20056 			 * an explicit 'configure' command will be needed to
20057 			 * configure it. If its value is non-zero, we will
20058 			 * attempt to online (configure) the device.
20059 			 * First, log the message indicating that a device
20060 			 * was attached.
20061 			 */
20062 			cportinfo->cport_dev_attach_time = 0;
20063 			sata_log(sata_hba_inst, CE_WARN,
20064 			    "SATA device detected at port %d", saddr->cport);
20065 
20066 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
20067 				sata_drive_info_t new_sdinfo;
20068 
20069 				/* Log device info data */
20070 				new_sdinfo = *(SATA_CPORTINFO_DRV_INFO(
20071 				    cportinfo));
20072 				sata_show_drive_info(sata_hba_inst,
20073 				    &new_sdinfo);
20074 			}
20075 
20076 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20077 			    saddr->cport)->cport_mutex);
20078 
20079 			/*
20080 			 * Make sure that there is no target node for that
20081 			 * device. If so, release it. It should not happen,
20082 			 * unless we had problem removing the node when
20083 			 * device was detached.
20084 			 */
20085 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
20086 			    saddr->cport, saddr->pmport);
20087 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20088 			    saddr->cport)->cport_mutex);
20089 			if (tdip != NULL) {
20090 
20091 #ifdef SATA_DEBUG
20092 				if ((cportinfo->cport_event_flags &
20093 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
20094 					sata_log(sata_hba_inst, CE_WARN,
20095 					    "sata_process_device_attached: "
20096 					    "old device target node exists!");
20097 #endif
20098 				/*
20099 				 * target node exists - try to unconfigure
20100 				 * device and remove the node.
20101 				 */
20102 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20103 				    saddr->cport)->cport_mutex);
20104 				rval = ndi_devi_offline(tdip,
20105 				    NDI_DEVI_REMOVE);
20106 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20107 				    saddr->cport)->cport_mutex);
20108 
20109 				if (rval == NDI_SUCCESS) {
20110 					cportinfo->cport_event_flags &=
20111 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20112 					cportinfo->cport_tgtnode_clean = B_TRUE;
20113 				} else {
20114 					/*
20115 					 * PROBLEM - the target node remained
20116 					 * and it belongs to a previously
20117 					 * attached device.
20118 					 * This happens when the file was open
20119 					 * or the node was waiting for
20120 					 * resources at the time the
20121 					 * associated device was removed.
20122 					 * Instruct event daemon to retry the
20123 					 * cleanup later.
20124 					 */
20125 					sata_log(sata_hba_inst,
20126 					    CE_WARN,
20127 					    "Application(s) accessing "
20128 					    "previously attached SATA "
20129 					    "device have to release "
20130 					    "it before newly inserted "
20131 					    "device can be made accessible.",
20132 					    saddr->cport);
20133 					cportinfo->cport_event_flags |=
20134 					    SATA_EVNT_TARGET_NODE_CLEANUP;
20135 					cportinfo->cport_tgtnode_clean =
20136 					    B_FALSE;
20137 				}
20138 			}
20139 			if (sata_auto_online != 0) {
20140 				cportinfo->cport_event_flags |=
20141 				    SATA_EVNT_AUTOONLINE_DEVICE;
20142 			}
20143 
20144 		}
20145 	} else {
20146 		cportinfo->cport_dev_attach_time = 0;
20147 	}
20148 
20149 	event_flags = cportinfo->cport_event_flags;
20150 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20151 	if (event_flags != 0 || pmult_event_flags != 0) {
20152 		mutex_enter(&sata_hba_inst->satahba_mutex);
20153 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20154 		mutex_exit(&sata_hba_inst->satahba_mutex);
20155 		mutex_enter(&sata_mutex);
20156 		sata_event_pending |= SATA_EVNT_MAIN;
20157 		mutex_exit(&sata_mutex);
20158 	}
20159 }
20160 
20161 /*
20162  * Port Multiplier Port Device Attached Event processing.
20163  *
20164  * NOTE: No Mutex should be hold.
20165  */
20166 static void
20167 sata_process_pmdevice_attached(sata_hba_inst_t *sata_hba_inst,
20168     sata_address_t *saddr)
20169 {
20170 	sata_pmport_info_t *pmportinfo;
20171 	sata_drive_info_t *sdinfo;
20172 	sata_device_t sata_device;
20173 	dev_info_t *tdip;
20174 	uint32_t event_flags;
20175 	uint8_t cport = saddr->cport;
20176 	uint8_t pmport = saddr->pmport;
20177 	int rval;
20178 
20179 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20180 	    "Processing port %d:%d device attached", cport, pmport);
20181 
20182 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
20183 
20184 	mutex_enter(&pmportinfo->pmport_mutex);
20185 
20186 	/* Clear attach event flag first */
20187 	pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
20188 
20189 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
20190 	if ((pmportinfo->pmport_state &
20191 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
20192 		pmportinfo->pmport_dev_attach_time = 0;
20193 		mutex_exit(&pmportinfo->pmport_mutex);
20194 		return;
20195 	}
20196 
20197 	/*
20198 	 * If the sata_drive_info structure is found attached to the port info,
20199 	 * despite the fact the device was removed and now it is re-attached,
20200 	 * the old drive info structure was not removed.
20201 	 * Arbitrarily release device info structure.
20202 	 */
20203 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20204 		sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
20205 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
20206 		(void) kmem_free((void *)sdinfo,
20207 		    sizeof (sata_drive_info_t));
20208 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20209 		    "Arbitrarily detaching old device info.", NULL);
20210 	}
20211 	pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
20212 
20213 	/* For sanity, re-probe the port */
20214 	sata_device.satadev_rev = SATA_DEVICE_REV;
20215 	sata_device.satadev_addr = *saddr;
20216 
20217 	/*
20218 	 * We have to exit mutex, because the HBA probe port function may
20219 	 * block on its own mutex.
20220 	 */
20221 	mutex_exit(&pmportinfo->pmport_mutex);
20222 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
20223 	    (SATA_DIP(sata_hba_inst), &sata_device);
20224 	mutex_enter(&pmportinfo->pmport_mutex);
20225 
20226 	sata_update_pmport_info(sata_hba_inst, &sata_device);
20227 	if (rval != SATA_SUCCESS) {
20228 		/* Something went wrong? Fail the port */
20229 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
20230 		pmportinfo->pmport_dev_attach_time = 0;
20231 		mutex_exit(&pmportinfo->pmport_mutex);
20232 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20233 		    "SATA port %d:%d probing failed", cport, pmport));
20234 		return;
20235 	} else {
20236 		/* pmport probed successfully */
20237 		pmportinfo->pmport_state |=
20238 		    SATA_STATE_PROBED | SATA_STATE_READY;
20239 	}
20240 	/*
20241 	 * Check if a device is still attached. For sanity, check also
20242 	 * link status - if no link, there is no device.
20243 	 */
20244 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
20245 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
20246 	    SATA_DTYPE_NONE) {
20247 		/*
20248 		 * No device - ignore attach event.
20249 		 */
20250 		pmportinfo->pmport_dev_attach_time = 0;
20251 		mutex_exit(&pmportinfo->pmport_mutex);
20252 		SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20253 		    "Ignoring attach - no device connected to port %d:%d",
20254 		    cport, pmport);
20255 		return;
20256 	}
20257 
20258 	mutex_exit(&pmportinfo->pmport_mutex);
20259 	/*
20260 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
20261 	 * with the hint: SE_HINT_INSERT
20262 	 */
20263 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
20264 
20265 	/*
20266 	 * Port reprobing will take care of the creation of the device
20267 	 * info structure and determination of the device type.
20268 	 */
20269 	sata_device.satadev_addr = *saddr;
20270 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
20271 	    SATA_DEV_IDENTIFY_NORETRY);
20272 
20273 	mutex_enter(&pmportinfo->pmport_mutex);
20274 	if ((pmportinfo->pmport_state & SATA_STATE_READY) &&
20275 	    (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE)) {
20276 		/* Some device is attached to the port */
20277 		if (pmportinfo->pmport_dev_type == SATA_DTYPE_UNKNOWN) {
20278 			/*
20279 			 * A device was not successfully attached.
20280 			 * Track retry time for device identification.
20281 			 */
20282 			if (pmportinfo->pmport_dev_attach_time != 0) {
20283 				clock_t cur_time = ddi_get_lbolt();
20284 				/*
20285 				 * If the retry time limit was not exceeded,
20286 				 * reinstate attach event.
20287 				 */
20288 				if ((cur_time -
20289 				    pmportinfo->pmport_dev_attach_time) <
20290 				    drv_usectohz(
20291 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
20292 					/* OK, restore attach event */
20293 					pmportinfo->pmport_event_flags |=
20294 					    SATA_EVNT_DEVICE_ATTACHED;
20295 				} else {
20296 					/* Timeout - cannot identify device */
20297 					pmportinfo->pmport_dev_attach_time = 0;
20298 					sata_log(sata_hba_inst, CE_WARN,
20299 					    "Could not identify SATA device "
20300 					    "at port %d:%d",
20301 					    cport, pmport);
20302 				}
20303 			} else {
20304 				/*
20305 				 * Start tracking time for device
20306 				 * identification.
20307 				 * Save current time (lbolt value).
20308 				 */
20309 				pmportinfo->pmport_dev_attach_time =
20310 				    ddi_get_lbolt();
20311 				/* Restore attach event */
20312 				pmportinfo->pmport_event_flags |=
20313 				    SATA_EVNT_DEVICE_ATTACHED;
20314 			}
20315 		} else {
20316 			/*
20317 			 * If device was successfully attached, the subsequent
20318 			 * action depends on a state of the
20319 			 * sata_auto_online variable. If it is set to zero.
20320 			 * an explicit 'configure' command will be needed to
20321 			 * configure it. If its value is non-zero, we will
20322 			 * attempt to online (configure) the device.
20323 			 * First, log the message indicating that a device
20324 			 * was attached.
20325 			 */
20326 			pmportinfo->pmport_dev_attach_time = 0;
20327 			sata_log(sata_hba_inst, CE_WARN,
20328 			    "SATA device detected at port %d:%d",
20329 			    cport, pmport);
20330 
20331 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20332 				sata_drive_info_t new_sdinfo;
20333 
20334 				/* Log device info data */
20335 				new_sdinfo = *(SATA_PMPORTINFO_DRV_INFO(
20336 				    pmportinfo));
20337 				sata_show_drive_info(sata_hba_inst,
20338 				    &new_sdinfo);
20339 			}
20340 
20341 			mutex_exit(&pmportinfo->pmport_mutex);
20342 
20343 			/*
20344 			 * Make sure that there is no target node for that
20345 			 * device. If so, release it. It should not happen,
20346 			 * unless we had problem removing the node when
20347 			 * device was detached.
20348 			 */
20349 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
20350 			    saddr->cport, saddr->pmport);
20351 			mutex_enter(&pmportinfo->pmport_mutex);
20352 			if (tdip != NULL) {
20353 
20354 #ifdef SATA_DEBUG
20355 				if ((pmportinfo->pmport_event_flags &
20356 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
20357 					sata_log(sata_hba_inst, CE_WARN,
20358 					    "sata_process_device_attached: "
20359 					    "old device target node exists!");
20360 #endif
20361 				/*
20362 				 * target node exists - try to unconfigure
20363 				 * device and remove the node.
20364 				 */
20365 				mutex_exit(&pmportinfo->pmport_mutex);
20366 				rval = ndi_devi_offline(tdip,
20367 				    NDI_DEVI_REMOVE);
20368 				mutex_enter(&pmportinfo->pmport_mutex);
20369 
20370 				if (rval == NDI_SUCCESS) {
20371 					pmportinfo->pmport_event_flags &=
20372 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20373 					pmportinfo->pmport_tgtnode_clean =
20374 					    B_TRUE;
20375 				} else {
20376 					/*
20377 					 * PROBLEM - the target node remained
20378 					 * and it belongs to a previously
20379 					 * attached device.
20380 					 * This happens when the file was open
20381 					 * or the node was waiting for
20382 					 * resources at the time the
20383 					 * associated device was removed.
20384 					 * Instruct event daemon to retry the
20385 					 * cleanup later.
20386 					 */
20387 					sata_log(sata_hba_inst,
20388 					    CE_WARN,
20389 					    "Application(s) accessing "
20390 					    "previously attached SATA "
20391 					    "device have to release "
20392 					    "it before newly inserted "
20393 					    "device can be made accessible."
20394 					    "at port %d:%d",
20395 					    cport, pmport);
20396 					pmportinfo->pmport_event_flags |=
20397 					    SATA_EVNT_TARGET_NODE_CLEANUP;
20398 					pmportinfo->pmport_tgtnode_clean =
20399 					    B_FALSE;
20400 				}
20401 			}
20402 			if (sata_auto_online != 0) {
20403 				pmportinfo->pmport_event_flags |=
20404 				    SATA_EVNT_AUTOONLINE_DEVICE;
20405 			}
20406 
20407 		}
20408 	} else {
20409 		pmportinfo->pmport_dev_attach_time = 0;
20410 	}
20411 
20412 	event_flags = pmportinfo->pmport_event_flags;
20413 	mutex_exit(&pmportinfo->pmport_mutex);
20414 	if (event_flags != 0) {
20415 		mutex_enter(&sata_hba_inst->satahba_mutex);
20416 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20417 		mutex_exit(&sata_hba_inst->satahba_mutex);
20418 		mutex_enter(&sata_mutex);
20419 		sata_event_pending |= SATA_EVNT_MAIN;
20420 		mutex_exit(&sata_mutex);
20421 	}
20422 
20423 	/* clear the reset_in_progress events */
20424 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
20425 		if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) {
20426 			/* must clear flags on cport */
20427 			sata_pmult_info_t *pminfo =
20428 			    SATA_PMULT_INFO(sata_hba_inst,
20429 			    saddr->cport);
20430 			pminfo->pmult_event_flags |=
20431 			    SATA_EVNT_CLEAR_DEVICE_RESET;
20432 		}
20433 	}
20434 }
20435 
20436 /*
20437  * Device Target Node Cleanup Event processing.
20438  * If the target node associated with a sata port device is in
20439  * DEVI_DEVICE_REMOVED state, an attempt is made to remove it.
20440  * If the target node cannot be removed, the event flag is left intact,
20441  * so that event daemon may re-run this function later.
20442  *
20443  * This function cannot be called in interrupt context (it may sleep).
20444  *
20445  * NOTE: Processes cport events only, not port multiplier ports.
20446  */
20447 static void
20448 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
20449     sata_address_t *saddr)
20450 {
20451 	sata_cport_info_t *cportinfo;
20452 	dev_info_t *tdip;
20453 
20454 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20455 	    "Processing port %d device target node cleanup", saddr->cport);
20456 
20457 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
20458 
20459 	/*
20460 	 * Check if there is target node for that device and it is in the
20461 	 * DEVI_DEVICE_REMOVED state. If so, release it.
20462 	 */
20463 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport,
20464 	    saddr->pmport);
20465 	if (tdip != NULL) {
20466 		/*
20467 		 * target node exists - check if it is target node of
20468 		 * a removed device.
20469 		 */
20470 		if (sata_check_device_removed(tdip) == B_TRUE) {
20471 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20472 			    "sata_process_target_node_cleanup: "
20473 			    "old device target node exists!", NULL);
20474 			/*
20475 			 * Unconfigure and remove the target node
20476 			 */
20477 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) ==
20478 			    NDI_SUCCESS) {
20479 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20480 				    saddr->cport)->cport_mutex);
20481 				cportinfo->cport_event_flags &=
20482 				    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20483 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20484 				    saddr->cport)->cport_mutex);
20485 				return;
20486 			}
20487 			/*
20488 			 * Event daemon will retry the cleanup later.
20489 			 */
20490 			mutex_enter(&sata_hba_inst->satahba_mutex);
20491 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20492 			mutex_exit(&sata_hba_inst->satahba_mutex);
20493 			mutex_enter(&sata_mutex);
20494 			sata_event_pending |= SATA_EVNT_MAIN;
20495 			mutex_exit(&sata_mutex);
20496 		}
20497 	} else {
20498 		if (saddr->qual == SATA_ADDR_CPORT ||
20499 		    saddr->qual == SATA_ADDR_DCPORT) {
20500 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20501 			    saddr->cport)->cport_mutex);
20502 			cportinfo->cport_event_flags &=
20503 			    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20504 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20505 			    saddr->cport)->cport_mutex);
20506 		} else {
20507 			/* sanity check */
20508 			if (SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) !=
20509 			    SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst,
20510 			    saddr->cport) == NULL)
20511 				return;
20512 			if (SATA_PMPORT_INFO(sata_hba_inst, saddr->cport,
20513 			    saddr->pmport) == NULL)
20514 				return;
20515 
20516 			mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst,
20517 			    saddr->cport, saddr->pmport)->pmport_mutex);
20518 			SATA_PMPORT_INFO(sata_hba_inst, saddr->cport,
20519 			    saddr->pmport)->pmport_event_flags &=
20520 			    ~SATA_EVNT_TARGET_NODE_CLEANUP;
20521 			mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst,
20522 			    saddr->cport, saddr->pmport)->pmport_mutex);
20523 		}
20524 	}
20525 }
20526 
20527 /*
20528  * Device AutoOnline Event processing.
20529  * If attached device is to be onlined, an attempt is made to online this
20530  * device, but only if there is no lingering (old) target node present.
20531  * If the device cannot be onlined, the event flag is left intact,
20532  * so that event daemon may re-run this function later.
20533  *
20534  * This function cannot be called in interrupt context (it may sleep).
20535  *
20536  * NOTE: Processes cport events only, not port multiplier ports.
20537  */
20538 static void
20539 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst,
20540     sata_address_t *saddr)
20541 {
20542 	sata_cport_info_t *cportinfo;
20543 	sata_drive_info_t *sdinfo;
20544 	sata_device_t sata_device;
20545 	dev_info_t *tdip;
20546 
20547 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20548 	    "Processing port %d attached device auto-onlining", saddr->cport);
20549 
20550 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
20551 
20552 	/*
20553 	 * Check if device is present and recognized. If not, reset event.
20554 	 */
20555 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20556 	if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
20557 		/* Nothing to online */
20558 		cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
20559 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20560 		    saddr->cport)->cport_mutex);
20561 		return;
20562 	}
20563 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20564 
20565 	/*
20566 	 * Check if there is target node for this device and if it is in the
20567 	 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep
20568 	 * the event for later processing.
20569 	 */
20570 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport,
20571 	    saddr->pmport);
20572 	if (tdip != NULL) {
20573 		/*
20574 		 * target node exists - check if it is target node of
20575 		 * a removed device.
20576 		 */
20577 		if (sata_check_device_removed(tdip) == B_TRUE) {
20578 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
20579 			    "sata_process_device_autoonline: "
20580 			    "old device target node exists!", NULL);
20581 			/*
20582 			 * Event daemon will retry device onlining later.
20583 			 */
20584 			mutex_enter(&sata_hba_inst->satahba_mutex);
20585 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20586 			mutex_exit(&sata_hba_inst->satahba_mutex);
20587 			mutex_enter(&sata_mutex);
20588 			sata_event_pending |= SATA_EVNT_MAIN;
20589 			mutex_exit(&sata_mutex);
20590 			return;
20591 		}
20592 		/*
20593 		 * If the target node is not in the 'removed" state, assume
20594 		 * that it belongs to this device. There is nothing more to do,
20595 		 * but reset the event.
20596 		 */
20597 	} else {
20598 
20599 		/*
20600 		 * Try to online the device
20601 		 * If there is any reset-related event, remove it. We are
20602 		 * configuring the device and no state restoring is needed.
20603 		 */
20604 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20605 		    saddr->cport)->cport_mutex);
20606 		sata_device.satadev_addr = *saddr;
20607 		if (saddr->qual == SATA_ADDR_CPORT)
20608 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
20609 		else
20610 			sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
20611 		sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
20612 		if (sdinfo != NULL) {
20613 			if (sdinfo->satadrv_event_flags &
20614 			    (SATA_EVNT_DEVICE_RESET |
20615 			    SATA_EVNT_INPROC_DEVICE_RESET))
20616 				sdinfo->satadrv_event_flags = 0;
20617 			sdinfo->satadrv_event_flags |=
20618 			    SATA_EVNT_CLEAR_DEVICE_RESET;
20619 
20620 			/* Need to create a new target node. */
20621 			cportinfo->cport_tgtnode_clean = B_TRUE;
20622 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20623 			    saddr->cport)->cport_mutex);
20624 			tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
20625 			    sata_hba_inst, &sata_device.satadev_addr);
20626 			if (tdip == NULL) {
20627 				/*
20628 				 * Configure (onlining) failed.
20629 				 * We will NOT retry
20630 				 */
20631 				SATA_LOG_D((sata_hba_inst, CE_WARN,
20632 				    "sata_process_device_autoonline: "
20633 				    "configuring SATA device at port %d failed",
20634 				    saddr->cport));
20635 			}
20636 		} else {
20637 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20638 			    saddr->cport)->cport_mutex);
20639 		}
20640 
20641 	}
20642 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
20643 	cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
20644 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20645 	    saddr->cport)->cport_mutex);
20646 }
20647 
20648 
20649 static void
20650 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr,
20651     int hint)
20652 {
20653 	char ap[MAXPATHLEN];
20654 	nvlist_t *ev_attr_list = NULL;
20655 	int err;
20656 
20657 	/* Allocate and build sysevent attribute list */
20658 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP);
20659 	if (err != 0) {
20660 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20661 		    "sata_gen_sysevent: "
20662 		    "cannot allocate memory for sysevent attributes\n"));
20663 		return;
20664 	}
20665 	/* Add hint attribute */
20666 	err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint));
20667 	if (err != 0) {
20668 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20669 		    "sata_gen_sysevent: "
20670 		    "failed to add DR_HINT attr for sysevent"));
20671 		nvlist_free(ev_attr_list);
20672 		return;
20673 	}
20674 	/*
20675 	 * Add AP attribute.
20676 	 * Get controller pathname and convert it into AP pathname by adding
20677 	 * a target number.
20678 	 */
20679 	(void) snprintf(ap, MAXPATHLEN, "/devices");
20680 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap));
20681 	(void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d",
20682 	    SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual));
20683 
20684 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap);
20685 	if (err != 0) {
20686 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20687 		    "sata_gen_sysevent: "
20688 		    "failed to add DR_AP_ID attr for sysevent"));
20689 		nvlist_free(ev_attr_list);
20690 		return;
20691 	}
20692 
20693 	/* Generate/log sysevent */
20694 	err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR,
20695 	    ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP);
20696 	if (err != DDI_SUCCESS) {
20697 		SATA_LOG_D((sata_hba_inst, CE_WARN,
20698 		    "sata_gen_sysevent: "
20699 		    "cannot log sysevent, err code %x\n", err));
20700 	}
20701 
20702 	nvlist_free(ev_attr_list);
20703 }
20704 
20705 
20706 
20707 
20708 /*
20709  * Set DEVI_DEVICE_REMOVED state in the SATA device target node.
20710  */
20711 static void
20712 sata_set_device_removed(dev_info_t *tdip)
20713 {
20714 	int circ;
20715 
20716 	ASSERT(tdip != NULL);
20717 
20718 	ndi_devi_enter(tdip, &circ);
20719 	mutex_enter(&DEVI(tdip)->devi_lock);
20720 	DEVI_SET_DEVICE_REMOVED(tdip);
20721 	mutex_exit(&DEVI(tdip)->devi_lock);
20722 	ndi_devi_exit(tdip, circ);
20723 }
20724 
20725 
20726 /*
20727  * Set internal event instructing event daemon to try
20728  * to perform the target node cleanup.
20729  */
20730 static void
20731 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
20732     sata_address_t *saddr)
20733 {
20734 	if (saddr->qual == SATA_ADDR_CPORT ||
20735 	    saddr->qual == SATA_ADDR_DCPORT) {
20736 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
20737 		    saddr->cport)->cport_mutex);
20738 		SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |=
20739 		    SATA_EVNT_TARGET_NODE_CLEANUP;
20740 		SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
20741 		    cport_tgtnode_clean = B_FALSE;
20742 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
20743 		    saddr->cport)->cport_mutex);
20744 	} else {
20745 		mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst,
20746 		    saddr->cport, saddr->pmport)->pmport_mutex);
20747 		SATA_PMPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport,
20748 		    saddr->pmport) |= SATA_EVNT_TARGET_NODE_CLEANUP;
20749 		SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, saddr->pmport)->
20750 		    pmport_tgtnode_clean = B_FALSE;
20751 		mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst,
20752 		    saddr->cport, saddr->pmport)->pmport_mutex);
20753 	}
20754 	mutex_enter(&sata_hba_inst->satahba_mutex);
20755 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
20756 	mutex_exit(&sata_hba_inst->satahba_mutex);
20757 	mutex_enter(&sata_mutex);
20758 	sata_event_pending |= SATA_EVNT_MAIN;
20759 	mutex_exit(&sata_mutex);
20760 }
20761 
20762 
20763 /*
20764  * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state,
20765  * i.e. check if the target node state indicates that it belongs to a removed
20766  * device.
20767  *
20768  * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state,
20769  * B_FALSE otherwise.
20770  */
20771 static boolean_t
20772 sata_check_device_removed(dev_info_t *tdip)
20773 {
20774 	ASSERT(tdip != NULL);
20775 
20776 	if (DEVI_IS_DEVICE_REMOVED(tdip))
20777 		return (B_TRUE);
20778 	else
20779 		return (B_FALSE);
20780 }
20781 
20782 
20783 /*
20784  * Check for DMA error. Return B_TRUE if error, B_FALSE otherwise.
20785  */
20786 static boolean_t
20787 sata_check_for_dma_error(dev_info_t *dip, sata_pkt_txlate_t *spx)
20788 {
20789 	int fm_capability = ddi_fm_capable(dip);
20790 	ddi_fm_error_t de;
20791 
20792 	if (fm_capability & DDI_FM_DMACHK_CAPABLE) {
20793 		if (spx->txlt_buf_dma_handle != NULL) {
20794 			ddi_fm_dma_err_get(spx->txlt_buf_dma_handle, &de,
20795 			    DDI_FME_VERSION);
20796 			if (de.fme_status != DDI_SUCCESS)
20797 				return (B_TRUE);
20798 		}
20799 	}
20800 	return (B_FALSE);
20801 }
20802 
20803 
20804 /* ************************ FAULT INJECTTION **************************** */
20805 
20806 #ifdef SATA_INJECT_FAULTS
20807 
20808 static	uint32_t sata_fault_count = 0;
20809 static	uint32_t sata_fault_suspend_count = 0;
20810 
20811 /*
20812  * Inject sata pkt fault
20813  * It modifies returned values of the sata packet.
20814  * It returns immediately if:
20815  * pkt fault injection is not enabled (via sata_inject_fault,
20816  * sata_inject_fault_count), or invalid fault is specified (sata_fault_type),
20817  * or pkt does not contain command to be faulted (set in sata_fault_cmd), or
20818  * pkt is not directed to specified fault controller/device
20819  * (sata_fault_ctrl_dev and sata_fault_device).
20820  * If fault controller is not specified, fault injection applies to all
20821  * controllers and devices.
20822  *
20823  * First argument is the pointer to the executed sata packet.
20824  * Second argument is a pointer to a value returned by the HBA tran_start
20825  * function.
20826  * Third argument specifies injected error. Injected sata packet faults
20827  * are the satapkt_reason values.
20828  * SATA_PKT_BUSY		-1	Not completed, busy
20829  * SATA_PKT_DEV_ERROR		1	Device reported error
20830  * SATA_PKT_QUEUE_FULL		2	Not accepted, queue full
20831  * SATA_PKT_PORT_ERROR		3	Not completed, port error
20832  * SATA_PKT_CMD_UNSUPPORTED	4	Cmd unsupported
20833  * SATA_PKT_ABORTED		5	Aborted by request
20834  * SATA_PKT_TIMEOUT		6	Operation timeut
20835  * SATA_PKT_RESET		7	Aborted by reset request
20836  *
20837  * Additional global variables affecting the execution:
20838  *
20839  * sata_inject_fault_count variable specifies number of times in row the
20840  * error is injected. Value of -1 specifies permanent fault, ie. every time
20841  * the fault injection point is reached, the fault is injected and a pause
20842  * between fault injection specified by sata_inject_fault_pause_count is
20843  * ignored). Fault injection routine decrements sata_inject_fault_count
20844  * (if greater than zero) until it reaches 0. No fault is injected when
20845  * sata_inject_fault_count is 0 (zero).
20846  *
20847  * sata_inject_fault_pause_count variable specifies number of times a fault
20848  * injection is bypassed (pause between fault injections).
20849  * If set to 0, a fault is injected only a number of times specified by
20850  * sata_inject_fault_count.
20851  *
20852  * The fault counts are static, so for periodic errors they have to be manually
20853  * reset to start repetition sequence from scratch.
20854  * If the original value returned by the HBA tran_start function is not
20855  * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error
20856  * is injected (to avoid masking real problems);
20857  *
20858  * NOTE: In its current incarnation, this function should be invoked only for
20859  * commands executed in SYNCHRONOUS mode.
20860  */
20861 
20862 
20863 static void
20864 sata_inject_pkt_fault(sata_pkt_t *spkt, int *rval, int fault)
20865 {
20866 
20867 	if (sata_inject_fault != SATA_INJECT_PKT_FAULT)
20868 		return;
20869 
20870 	if (sata_inject_fault_count == 0)
20871 		return;
20872 
20873 	if (fault == 0)
20874 		return;
20875 
20876 	if (sata_fault_cmd != spkt->satapkt_cmd.satacmd_cmd_reg)
20877 		return;
20878 
20879 	if (sata_fault_ctrl != NULL) {
20880 		sata_pkt_txlate_t *spx =
20881 		    (sata_pkt_txlate_t *)spkt->satapkt_framework_private;
20882 
20883 		if (sata_fault_ctrl != NULL && sata_fault_ctrl !=
20884 		    spx->txlt_sata_hba_inst->satahba_dip)
20885 			return;
20886 
20887 		if (sata_fault_device.satadev_addr.cport !=
20888 		    spkt->satapkt_device.satadev_addr.cport ||
20889 		    sata_fault_device.satadev_addr.pmport !=
20890 		    spkt->satapkt_device.satadev_addr.pmport ||
20891 		    sata_fault_device.satadev_addr.qual !=
20892 		    spkt->satapkt_device.satadev_addr.qual)
20893 			return;
20894 	}
20895 
20896 	/* Modify pkt return parameters */
20897 	if (*rval != SATA_TRAN_ACCEPTED ||
20898 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
20899 		sata_fault_count = 0;
20900 		sata_fault_suspend_count = 0;
20901 		return;
20902 	}
20903 	if (sata_fault_count == 0 && sata_fault_suspend_count != 0) {
20904 		/* Pause in the injection */
20905 		sata_fault_suspend_count -= 1;
20906 		return;
20907 	}
20908 
20909 	if (sata_fault_count == 0 && sata_fault_suspend_count == 0) {
20910 		/*
20911 		 * Init inject fault cycle. If fault count is set to -1,
20912 		 * it is a permanent fault.
20913 		 */
20914 		if (sata_inject_fault_count != -1) {
20915 			sata_fault_count = sata_inject_fault_count;
20916 			sata_fault_suspend_count =
20917 			    sata_inject_fault_pause_count;
20918 			if (sata_fault_suspend_count == 0)
20919 				sata_inject_fault_count = 0;
20920 		}
20921 	}
20922 
20923 	if (sata_fault_count != 0)
20924 		sata_fault_count -= 1;
20925 
20926 	switch (fault) {
20927 	case SATA_PKT_BUSY:
20928 		*rval = SATA_TRAN_BUSY;
20929 		spkt->satapkt_reason = SATA_PKT_BUSY;
20930 		break;
20931 
20932 	case SATA_PKT_QUEUE_FULL:
20933 		*rval = SATA_TRAN_QUEUE_FULL;
20934 		spkt->satapkt_reason = SATA_PKT_QUEUE_FULL;
20935 		break;
20936 
20937 	case SATA_PKT_CMD_UNSUPPORTED:
20938 		*rval = SATA_TRAN_CMD_UNSUPPORTED;
20939 		spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED;
20940 		break;
20941 
20942 	case SATA_PKT_PORT_ERROR:
20943 		/* This is "rejected" command */
20944 		*rval = SATA_TRAN_PORT_ERROR;
20945 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
20946 		/* Additional error setup could be done here - port state */
20947 		break;
20948 
20949 	case SATA_PKT_DEV_ERROR:
20950 		spkt->satapkt_reason = SATA_PKT_DEV_ERROR;
20951 		/*
20952 		 * Additional error setup could be done here
20953 		 */
20954 		break;
20955 
20956 	case SATA_PKT_ABORTED:
20957 		spkt->satapkt_reason = SATA_PKT_ABORTED;
20958 		break;
20959 
20960 	case SATA_PKT_TIMEOUT:
20961 		spkt->satapkt_reason = SATA_PKT_TIMEOUT;
20962 		/* Additional error setup could be done here */
20963 		break;
20964 
20965 	case SATA_PKT_RESET:
20966 		spkt->satapkt_reason = SATA_PKT_RESET;
20967 		/*
20968 		 * Additional error setup could be done here - device reset
20969 		 */
20970 		break;
20971 
20972 	default:
20973 		break;
20974 	}
20975 }
20976 
20977 #endif
20978 
20979 /*
20980  * SATA Trace Ring Buffer
20981  * ----------------------
20982  *
20983  * Overview
20984  *
20985  * The SATA trace ring buffer is a ring buffer created and managed by
20986  * the SATA framework module that can be used by any module or driver
20987  * within the SATA framework to store debug messages.
20988  *
20989  * Ring Buffer Interfaces:
20990  *
20991  *	sata_vtrace_debug()	<-- Adds debug message to ring buffer
20992  *	sata_trace_debug()	<-- Wraps varargs into sata_vtrace_debug()
20993  *
20994  *	Note that the sata_trace_debug() interface was created to give
20995  *	consumers the flexibilty of sending debug messages to ring buffer
20996  *	as variable arguments.  Consumers can send type va_list debug
20997  *	messages directly to sata_vtrace_debug(). The sata_trace_debug()
20998  *	and sata_vtrace_debug() relationship is similar to that of
20999  *	cmn_err(9F) and vcmn_err(9F).
21000  *
21001  * Below is a diagram of the SATA trace ring buffer interfaces and
21002  * sample consumers:
21003  *
21004  * +---------------------------------+
21005  * |    o  o  SATA Framework Module  |
21006  * | o  SATA  o     +------------------+      +------------------+
21007  * |o   Trace  o <--|sata_vtrace_debug/|<-----|SATA HBA Driver #1|
21008  * |o   R-Buf  o    |sata_trace_debug  |<--+  +------------------+
21009  * | o        o     +------------------+   |  +------------------+
21010  * |    o  o                ^        |     +--|SATA HBA Driver #2|
21011  * |                        |        |        +------------------+
21012  * |           +------------------+  |
21013  * |           |SATA Debug Message|  |
21014  * |           +------------------+  |
21015  * +---------------------------------+
21016  *
21017  * Supporting Routines:
21018  *
21019  *	sata_trace_rbuf_alloc()	<-- Initializes ring buffer
21020  *	sata_trace_rbuf_free()	<-- Destroys ring buffer
21021  *	sata_trace_dmsg_alloc() <-- Creates or reuses buffer in ring buffer
21022  *	sata_trace_dmsg_free()	<-- Destroys content of ring buffer
21023  *
21024  * The default SATA trace ring buffer size is defined by DMSG_RING_SIZE.
21025  * The ring buffer size can be adjusted by setting dmsg_ring_size in
21026  * /etc/system to desired size in unit of bytes.
21027  *
21028  * The individual debug message size in the ring buffer is restricted
21029  * to DMSG_BUF_SIZE.
21030  */
21031 void
21032 sata_vtrace_debug(dev_info_t *dip, const char *fmt, va_list ap)
21033 {
21034 	sata_trace_dmsg_t *dmsg;
21035 
21036 	if (sata_debug_rbuf == NULL) {
21037 		return;
21038 	}
21039 
21040 	/*
21041 	 * If max size of ring buffer is smaller than size
21042 	 * required for one debug message then just return
21043 	 * since we have no room for the debug message.
21044 	 */
21045 	if (sata_debug_rbuf->maxsize < (sizeof (sata_trace_dmsg_t))) {
21046 		return;
21047 	}
21048 
21049 	mutex_enter(&sata_debug_rbuf->lock);
21050 
21051 	/* alloc or reuse on ring buffer */
21052 	dmsg = sata_trace_dmsg_alloc();
21053 
21054 	if (dmsg == NULL) {
21055 		/* resource allocation failed */
21056 		mutex_exit(&sata_debug_rbuf->lock);
21057 		return;
21058 	}
21059 
21060 	dmsg->dip = dip;
21061 	gethrestime(&dmsg->timestamp);
21062 
21063 	(void) vsnprintf(dmsg->buf, sizeof (dmsg->buf), fmt, ap);
21064 
21065 	mutex_exit(&sata_debug_rbuf->lock);
21066 }
21067 
21068 void
21069 sata_trace_debug(dev_info_t *dip, const char *fmt, ...)
21070 {
21071 	va_list ap;
21072 
21073 	va_start(ap, fmt);
21074 	sata_vtrace_debug(dip, fmt, ap);
21075 	va_end(ap);
21076 }
21077 
21078 /*
21079  * This routine is used to manage debug messages
21080  * on ring buffer.
21081  */
21082 static sata_trace_dmsg_t *
21083 sata_trace_dmsg_alloc(void)
21084 {
21085 	sata_trace_dmsg_t *dmsg_alloc, *dmsg = sata_debug_rbuf->dmsgp;
21086 
21087 	if (sata_debug_rbuf->looped == TRUE) {
21088 		sata_debug_rbuf->dmsgp = dmsg->next;
21089 		return (sata_debug_rbuf->dmsgp);
21090 	}
21091 
21092 	/*
21093 	 * If we're looping for the first time,
21094 	 * connect the ring.
21095 	 */
21096 	if (((sata_debug_rbuf->size + (sizeof (sata_trace_dmsg_t))) >
21097 	    sata_debug_rbuf->maxsize) && (sata_debug_rbuf->dmsgh != NULL)) {
21098 		dmsg->next = sata_debug_rbuf->dmsgh;
21099 		sata_debug_rbuf->dmsgp = sata_debug_rbuf->dmsgh;
21100 		sata_debug_rbuf->looped = TRUE;
21101 		return (sata_debug_rbuf->dmsgp);
21102 	}
21103 
21104 	/* If we've gotten this far then memory allocation is needed */
21105 	dmsg_alloc = kmem_zalloc(sizeof (sata_trace_dmsg_t), KM_NOSLEEP);
21106 	if (dmsg_alloc == NULL) {
21107 		sata_debug_rbuf->allocfailed++;
21108 		return (dmsg_alloc);
21109 	} else {
21110 		sata_debug_rbuf->size += sizeof (sata_trace_dmsg_t);
21111 	}
21112 
21113 	if (sata_debug_rbuf->dmsgp != NULL) {
21114 		dmsg->next = dmsg_alloc;
21115 		sata_debug_rbuf->dmsgp = dmsg->next;
21116 		return (sata_debug_rbuf->dmsgp);
21117 	} else {
21118 		/*
21119 		 * We should only be here if we're initializing
21120 		 * the ring buffer.
21121 		 */
21122 		if (sata_debug_rbuf->dmsgh == NULL) {
21123 			sata_debug_rbuf->dmsgh = dmsg_alloc;
21124 		} else {
21125 			/* Something is wrong */
21126 			kmem_free(dmsg_alloc, sizeof (sata_trace_dmsg_t));
21127 			return (NULL);
21128 		}
21129 
21130 		sata_debug_rbuf->dmsgp = dmsg_alloc;
21131 		return (sata_debug_rbuf->dmsgp);
21132 	}
21133 }
21134 
21135 
21136 /*
21137  * Free all messages on debug ring buffer.
21138  */
21139 static void
21140 sata_trace_dmsg_free(void)
21141 {
21142 	sata_trace_dmsg_t *dmsg_next, *dmsg = sata_debug_rbuf->dmsgh;
21143 
21144 	while (dmsg != NULL) {
21145 		dmsg_next = dmsg->next;
21146 		kmem_free(dmsg, sizeof (sata_trace_dmsg_t));
21147 
21148 		/*
21149 		 * If we've looped around the ring than we're done.
21150 		 */
21151 		if (dmsg_next == sata_debug_rbuf->dmsgh) {
21152 			break;
21153 		} else {
21154 			dmsg = dmsg_next;
21155 		}
21156 	}
21157 }
21158 
21159 
21160 /*
21161  * This function can block
21162  */
21163 static void
21164 sata_trace_rbuf_alloc(void)
21165 {
21166 	sata_debug_rbuf = kmem_zalloc(sizeof (sata_trace_rbuf_t), KM_SLEEP);
21167 
21168 	mutex_init(&sata_debug_rbuf->lock, NULL, MUTEX_DRIVER, NULL);
21169 
21170 	if (dmsg_ring_size > 0) {
21171 		sata_debug_rbuf->maxsize = (size_t)dmsg_ring_size;
21172 	}
21173 }
21174 
21175 
21176 static void
21177 sata_trace_rbuf_free(void)
21178 {
21179 	sata_trace_dmsg_free();
21180 	mutex_destroy(&sata_debug_rbuf->lock);
21181 	kmem_free(sata_debug_rbuf, sizeof (sata_trace_rbuf_t));
21182 }
21183 
21184 /*
21185  * If SATA_DEBUG is not defined then this routine is called instead
21186  * of sata_log() via the SATA_LOG_D macro.
21187  */
21188 static void
21189 sata_trace_log(sata_hba_inst_t *sata_hba_inst, uint_t level,
21190     const char *fmt, ...)
21191 {
21192 #ifndef __lock_lint
21193 	_NOTE(ARGUNUSED(level))
21194 #endif
21195 
21196 	dev_info_t *dip = NULL;
21197 	va_list ap;
21198 
21199 	if (sata_hba_inst != NULL) {
21200 		dip = SATA_DIP(sata_hba_inst);
21201 	}
21202 
21203 	va_start(ap, fmt);
21204 	sata_vtrace_debug(dip, fmt, ap);
21205 	va_end(ap);
21206 }
21207