xref: /titanic_41/usr/src/uts/common/io/sata/impl/sata.c (revision 4c06356b0f0fffb4fc1b6eccc8e5d8e2254a84d6)
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 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 
28 /*
29  * SATA Framework
30  * Generic SATA Host Adapter Implementation
31  */
32 
33 #include <sys/conf.h>
34 #include <sys/file.h>
35 #include <sys/ddi.h>
36 #include <sys/sunddi.h>
37 #include <sys/modctl.h>
38 #include <sys/cmn_err.h>
39 #include <sys/errno.h>
40 #include <sys/thread.h>
41 #include <sys/kstat.h>
42 #include <sys/note.h>
43 #include <sys/sysevent.h>
44 #include <sys/sysevent/eventdefs.h>
45 #include <sys/sysevent/dr.h>
46 #include <sys/taskq.h>
47 #include <sys/disp.h>
48 
49 #include <sys/sata/impl/sata.h>
50 #include <sys/sata/sata_hba.h>
51 #include <sys/sata/sata_defs.h>
52 #include <sys/sata/sata_cfgadm.h>
53 #include <sys/sata/sata_blacklist.h>
54 
55 /* Debug flags - defined in sata.h */
56 int	sata_debug_flags = 0;
57 int	sata_msg = 0;
58 
59 /*
60  * Flags enabling selected SATA HBA framework functionality
61  */
62 #define	SATA_ENABLE_QUEUING		1
63 #define	SATA_ENABLE_NCQ			2
64 #define	SATA_ENABLE_PROCESS_EVENTS	4
65 #define	SATA_ENABLE_PMULT_FBS		8 /* FIS-Based Switching */
66 int sata_func_enable =
67 	SATA_ENABLE_PROCESS_EVENTS | SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ;
68 
69 /*
70  * Global variable setting default maximum queue depth (NCQ or TCQ)
71  * Note:minimum queue depth is 1
72  */
73 int sata_max_queue_depth = SATA_MAX_QUEUE_DEPTH; /* max NCQ/TCQ queue depth */
74 
75 /*
76  * Currently used default NCQ/TCQ queue depth. It is set-up during the driver
77  * initialization, using value from sata_max_queue_depth
78  * It is adjusted to minimum supported by the controller and by the device,
79  * if queueing is enabled.
80  */
81 static	int sata_current_max_qdepth;
82 
83 /*
84  * Global variable determining the default behavior after device hotpluggin.
85  * If non-zero, the hotplugged device is onlined (if possible) without explicit
86  * IOCTL request (AP_CONFIGURE).
87  * If zero, hotplugged device is identified, but not onlined.
88  * Enabling (AP_CONNECT) device port with an attached device does not result
89  * in device onlining regardless of the flag setting
90  */
91 int sata_auto_online = 0;
92 
93 #ifdef SATA_DEBUG
94 
95 #define	SATA_LOG_D(args)	sata_log args
96 uint64_t mbuf_count = 0;
97 uint64_t mbuffail_count = 0;
98 
99 sata_atapi_cmd_t sata_atapi_trace[64];
100 uint32_t sata_atapi_trace_index = 0;
101 int sata_atapi_trace_save = 1;
102 static	void sata_save_atapi_trace(sata_pkt_txlate_t *, int);
103 #define	SATAATAPITRACE(spx, count)	if (sata_atapi_trace_save) \
104     sata_save_atapi_trace(spx, count);
105 
106 #else
107 #define	SATA_LOG_D(args)	sata_trace_log args
108 #define	SATAATAPITRACE(spx, count)
109 #endif
110 
111 #if 0
112 static void
113 sata_test_atapi_packet_command(sata_hba_inst_t *, int);
114 #endif
115 
116 #ifdef SATA_INJECT_FAULTS
117 
118 #define		SATA_INJECT_PKT_FAULT	1
119 uint32_t	sata_inject_fault = 0;
120 
121 uint32_t	sata_inject_fault_count = 0;
122 uint32_t	sata_inject_fault_pause_count = 0;
123 uint32_t	sata_fault_type = 0;
124 uint32_t	sata_fault_cmd = 0;
125 dev_info_t	*sata_fault_ctrl = NULL;
126 sata_device_t	sata_fault_device;
127 
128 static	void sata_inject_pkt_fault(sata_pkt_t *, int *, int);
129 
130 #endif
131 
132 #define	LEGACY_HWID_LEN	64	/* Model (40) + Serial (20) + pad */
133 
134 static char sata_rev_tag[] = {"1.46"};
135 
136 /*
137  * SATA cb_ops functions
138  */
139 static 	int sata_hba_open(dev_t *, int, int, cred_t *);
140 static 	int sata_hba_close(dev_t, int, int, cred_t *);
141 static 	int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *,	int *);
142 
143 /*
144  * SCSA required entry points
145  */
146 static	int sata_scsi_tgt_init(dev_info_t *, dev_info_t *,
147     scsi_hba_tran_t *, struct scsi_device *);
148 static	int sata_scsi_tgt_probe(struct scsi_device *,
149     int (*callback)(void));
150 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *,
151     scsi_hba_tran_t *, struct scsi_device *);
152 static 	int sata_scsi_start(struct scsi_address *, struct scsi_pkt *);
153 static 	int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *);
154 static 	int sata_scsi_reset(struct scsi_address *, int);
155 static 	int sata_scsi_getcap(struct scsi_address *, char *, int);
156 static 	int sata_scsi_setcap(struct scsi_address *, char *, int, int);
157 static 	struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *,
158     struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t),
159     caddr_t);
160 static 	void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *);
161 static 	void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *);
162 static 	void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *);
163 
164 /*
165  * SATA HBA interface functions are defined in sata_hba.h header file
166  */
167 
168 /* Event processing functions */
169 static	void sata_event_daemon(void *);
170 static	void sata_event_thread_control(int);
171 static	void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst);
172 static	void sata_process_pmult_events(sata_hba_inst_t *, uint8_t);
173 static	void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *);
174 static	void sata_process_pmdevice_reset(sata_hba_inst_t *, sata_address_t *);
175 static	void sata_process_port_failed_event(sata_hba_inst_t *,
176     sata_address_t *);
177 static	void sata_process_port_link_events(sata_hba_inst_t *,
178     sata_address_t *);
179 static	void sata_process_pmport_link_events(sata_hba_inst_t *,
180     sata_address_t *);
181 static	void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *);
182 static	void sata_process_pmdevice_detached(sata_hba_inst_t *,
183     sata_address_t *);
184 static	void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *);
185 static	void sata_process_pmdevice_attached(sata_hba_inst_t *,
186     sata_address_t *);
187 static	void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *);
188 static	void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *);
189 static	void sata_process_target_node_cleanup(sata_hba_inst_t *,
190     sata_address_t *);
191 static	void sata_process_device_autoonline(sata_hba_inst_t *,
192     sata_address_t *saddr);
193 
194 /*
195  * Local translation functions
196  */
197 static	int sata_txlt_inquiry(sata_pkt_txlate_t *);
198 static	int sata_txlt_test_unit_ready(sata_pkt_txlate_t *);
199 static	int sata_txlt_start_stop_unit(sata_pkt_txlate_t *);
200 static	int sata_txlt_read_capacity(sata_pkt_txlate_t *);
201 static	int sata_txlt_request_sense(sata_pkt_txlate_t *);
202 static	int sata_txlt_read(sata_pkt_txlate_t *);
203 static	int sata_txlt_write(sata_pkt_txlate_t *);
204 static	int sata_txlt_log_sense(sata_pkt_txlate_t *);
205 static	int sata_txlt_log_select(sata_pkt_txlate_t *);
206 static	int sata_txlt_mode_sense(sata_pkt_txlate_t *);
207 static	int sata_txlt_mode_select(sata_pkt_txlate_t *);
208 static	int sata_txlt_synchronize_cache(sata_pkt_txlate_t *);
209 static	int sata_txlt_write_buffer(sata_pkt_txlate_t *);
210 static	int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *);
211 
212 static	int sata_hba_start(sata_pkt_txlate_t *, int *);
213 static	int sata_txlt_invalid_command(sata_pkt_txlate_t *);
214 static	int sata_txlt_check_condition(sata_pkt_txlate_t *, uchar_t, uchar_t);
215 static	int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *);
216 static	void sata_txlt_rw_completion(sata_pkt_t *);
217 static	void sata_txlt_nodata_cmd_completion(sata_pkt_t *);
218 static	void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *);
219 static	int sata_emul_rw_completion(sata_pkt_txlate_t *);
220 static	struct scsi_extended_sense *sata_immediate_error_response(
221     sata_pkt_txlate_t *, int);
222 static	struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *);
223 
224 static	int sata_txlt_atapi(sata_pkt_txlate_t *);
225 static	void sata_txlt_atapi_completion(sata_pkt_t *);
226 
227 /*
228  * Local functions for ioctl
229  */
230 static	int32_t sata_get_port_num(sata_hba_inst_t *,  struct devctl_iocdata *);
231 static	void sata_cfgadm_state(sata_hba_inst_t *, int32_t,
232     devctl_ap_state_t *);
233 static	dev_info_t *sata_get_target_dip(dev_info_t *, uint8_t, uint8_t);
234 static	dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *);
235 static	dev_info_t *sata_devt_to_devinfo(dev_t);
236 static	int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *);
237 static	int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *);
238 static	int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *);
239 static	int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *);
240 static	int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *);
241 static	int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *);
242 static	int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *);
243 static	int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *);
244 static	int sata_ioctl_reset_all(sata_hba_inst_t *);
245 static	int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *);
246 static	int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *,
247     sata_ioctl_data_t *, int mode);
248 static	int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *,
249     sata_ioctl_data_t *, int mode);
250 static	int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *,
251     sata_ioctl_data_t *, int mode);
252 static	int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *,
253     sata_ioctl_data_t *, int mode);
254 static	int sata_ioctl_get_serialnumber_info(sata_hba_inst_t *,
255     sata_device_t *, sata_ioctl_data_t *, int mode);
256 
257 /*
258  * Local functions
259  */
260 static 	void sata_remove_hba_instance(dev_info_t *);
261 static 	int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *);
262 static 	void sata_probe_ports(sata_hba_inst_t *);
263 static	void sata_probe_pmports(sata_hba_inst_t *, uint8_t);
264 static 	int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int);
265 static 	int sata_reprobe_pmult(sata_hba_inst_t *, sata_device_t *, int);
266 static 	int sata_reprobe_pmport(sata_hba_inst_t *, sata_device_t *, int);
267 static	int sata_alloc_pmult(sata_hba_inst_t *, sata_device_t *);
268 static	void sata_free_pmult(sata_hba_inst_t *, sata_device_t *);
269 static 	int sata_add_device(dev_info_t *, sata_hba_inst_t *, sata_device_t *);
270 static	int sata_offline_device(sata_hba_inst_t *, sata_device_t *,
271     sata_drive_info_t *);
272 static 	dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *,
273     sata_address_t *);
274 static 	void sata_remove_target_node(sata_hba_inst_t *,
275     sata_address_t *);
276 static 	int sata_validate_scsi_address(sata_hba_inst_t *,
277     struct scsi_address *, sata_device_t *);
278 static 	int sata_validate_sata_address(sata_hba_inst_t *, int, int, int);
279 static	sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t));
280 static	void sata_pkt_free(sata_pkt_txlate_t *);
281 static	int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t),
282     caddr_t, ddi_dma_attr_t *);
283 static	void sata_common_free_dma_rsrcs(sata_pkt_txlate_t *);
284 static	int sata_probe_device(sata_hba_inst_t *, sata_device_t *);
285 static	sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *,
286     sata_device_t *);
287 static 	int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *);
288 static	void sata_reidentify_device(sata_pkt_txlate_t *);
289 static	struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int);
290 static 	void sata_free_local_buffer(sata_pkt_txlate_t *);
291 static 	uint64_t sata_check_capacity(sata_drive_info_t *);
292 void 	sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *,
293     ddi_dma_attr_t *);
294 static 	int sata_fetch_device_identify_data(sata_hba_inst_t *,
295     sata_drive_info_t *);
296 static	void sata_update_port_info(sata_hba_inst_t *, sata_device_t *);
297 static	void sata_update_pmport_info(sata_hba_inst_t *, sata_device_t *);
298 static	void sata_update_port_scr(sata_port_scr_t *, sata_device_t *);
299 static	int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *);
300 static	int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int);
301 static	int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int);
302 static	int sata_set_drive_features(sata_hba_inst_t *,
303     sata_drive_info_t *, int flag);
304 static	void sata_init_write_cache_mode(sata_drive_info_t *sdinfo);
305 static	int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *);
306 static	void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *,
307     uint8_t *);
308 static	int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *,
309     struct scsi_inquiry *);
310 static	int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *);
311 static	int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *);
312 static	int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *);
313 static	int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *);
314 static	int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *);
315 static	int sata_mode_select_page_8(sata_pkt_txlate_t *,
316     struct mode_cache_scsi3 *, int, int *, int *, int *);
317 static	int sata_mode_select_page_1a(sata_pkt_txlate_t *,
318     struct mode_info_power_cond *, int, int *, int *, int *);
319 static	int sata_mode_select_page_1c(sata_pkt_txlate_t *,
320     struct mode_info_excpt_page *, int, int *, int *, int *);
321 static	int sata_mode_select_page_30(sata_pkt_txlate_t *,
322     struct mode_acoustic_management *, int, int *, int *, int *);
323 
324 static	int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *);
325 static	int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *,
326     sata_hba_inst_t *);
327 static	int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *,
328     sata_hba_inst_t *);
329 static	int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *,
330     sata_hba_inst_t *);
331 static	int sata_build_lsense_page_0e(sata_drive_info_t *, uint8_t *,
332     sata_pkt_txlate_t *);
333 
334 static	void sata_set_arq_data(sata_pkt_t *);
335 static	void sata_build_read_verify_cmd(sata_cmd_t *, uint16_t, uint64_t);
336 static	void sata_build_generic_cmd(sata_cmd_t *, uint8_t);
337 static	uint8_t sata_get_standby_timer(uint8_t *timer);
338 
339 static	void sata_save_drive_settings(sata_drive_info_t *);
340 static	void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *);
341 static	void sata_show_pmult_info(sata_hba_inst_t *, sata_device_t *);
342 static	void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...);
343 static	void sata_trace_log(sata_hba_inst_t *, uint_t, const char *fmt, ...);
344 static	int sata_fetch_smart_return_status(sata_hba_inst_t *,
345     sata_drive_info_t *);
346 static	int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *,
347     struct smart_data *);
348 static	int sata_smart_selftest_log(sata_hba_inst_t *,
349     sata_drive_info_t *,
350     struct smart_selftest_log *);
351 static	int sata_ext_smart_selftest_read_log(sata_hba_inst_t *,
352     sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t);
353 static	int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *,
354     uint8_t *, uint8_t, uint8_t);
355 static	int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *,
356     struct read_log_ext_directory *);
357 static	void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int);
358 static	void sata_xlate_errors(sata_pkt_txlate_t *);
359 static	void sata_decode_device_error(sata_pkt_txlate_t *,
360     struct scsi_extended_sense *);
361 static	void sata_set_device_removed(dev_info_t *);
362 static	boolean_t sata_check_device_removed(dev_info_t *);
363 static	void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *);
364 static	int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *,
365     sata_drive_info_t *);
366 static	int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *,
367     sata_drive_info_t *);
368 static	void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *);
369 static	void sata_fixed_sense_data_preset(struct scsi_extended_sense *);
370 static  void sata_target_devid_register(dev_info_t *, sata_drive_info_t *);
371 static  int sata_check_modser(char *, int);
372 
373 
374 
375 /*
376  * SATA Framework will ignore SATA HBA driver cb_ops structure and
377  * register following one with SCSA framework.
378  * Open & close are provided, so scsi framework will not use its own
379  */
380 static struct cb_ops sata_cb_ops = {
381 	sata_hba_open,			/* open */
382 	sata_hba_close,			/* close */
383 	nodev,				/* strategy */
384 	nodev,				/* print */
385 	nodev,				/* dump */
386 	nodev,				/* read */
387 	nodev,				/* write */
388 	sata_hba_ioctl,			/* ioctl */
389 	nodev,				/* devmap */
390 	nodev,				/* mmap */
391 	nodev,				/* segmap */
392 	nochpoll,			/* chpoll */
393 	ddi_prop_op,			/* cb_prop_op */
394 	0,				/* streamtab */
395 	D_NEW | D_MP,			/* cb_flag */
396 	CB_REV,				/* rev */
397 	nodev,				/* aread */
398 	nodev				/* awrite */
399 };
400 
401 
402 extern struct mod_ops mod_miscops;
403 extern uchar_t	scsi_cdb_size[];
404 
405 static struct modlmisc modlmisc = {
406 	&mod_miscops,			/* Type of module */
407 	"SATA Module"			/* module name */
408 };
409 
410 
411 static struct modlinkage modlinkage = {
412 	MODREV_1,
413 	(void *)&modlmisc,
414 	NULL
415 };
416 
417 /*
418  * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero,
419  * i.e. when scsi_pkt has not timeout specified.
420  */
421 static int sata_default_pkt_time = 60;	/* 60 seconds */
422 
423 /*
424  * Intermediate buffer device access attributes - they are required,
425  * but not necessarily used.
426  */
427 static ddi_device_acc_attr_t sata_acc_attr = {
428 	DDI_DEVICE_ATTR_V0,
429 	DDI_STRUCTURE_LE_ACC,
430 	DDI_STRICTORDER_ACC
431 };
432 
433 
434 /*
435  * Mutexes protecting structures in multithreaded operations.
436  * Because events are relatively rare, a single global mutex protecting
437  * data structures should be sufficient. To increase performance, add
438  * separate mutex per each sata port and use global mutex only to protect
439  * common data structures.
440  */
441 static	kmutex_t sata_mutex;		/* protects sata_hba_list */
442 static	kmutex_t sata_log_mutex;	/* protects log */
443 
444 static 	char sata_log_buf[256];
445 
446 /*
447  * sata trace debug
448  */
449 static	sata_trace_rbuf_t *sata_debug_rbuf;
450 static	sata_trace_dmsg_t *sata_trace_dmsg_alloc(void);
451 static	void sata_trace_dmsg_free(void);
452 static	void sata_trace_rbuf_alloc(void);
453 static	void sata_trace_rbuf_free(void);
454 
455 int	dmsg_ring_size = DMSG_RING_SIZE;
456 
457 /* Default write cache setting for SATA hard disks */
458 int	sata_write_cache = 1;		/* enabled */
459 
460 /* Default write cache setting for SATA ATAPI CD/DVD */
461 int	sata_atapicdvd_write_cache = 1; /* enabled */
462 
463 /* Default write cache setting for SATA ATAPI tape */
464 int	sata_atapitape_write_cache = 1; /* enabled */
465 
466 /* Default write cache setting for SATA ATAPI disk */
467 int	sata_atapidisk_write_cache = 1;	/* enabled */
468 
469 /*
470  * Linked list of HBA instances
471  */
472 static 	sata_hba_inst_t *sata_hba_list = NULL;
473 static 	sata_hba_inst_t *sata_hba_list_tail = NULL;
474 /*
475  * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran
476  * structure and in sata soft state.
477  */
478 
479 /*
480  * Event daemon related variables
481  */
482 static 	kmutex_t sata_event_mutex;
483 static 	kcondvar_t sata_event_cv;
484 static 	kthread_t *sata_event_thread = NULL;
485 static 	int sata_event_thread_terminate = 0;
486 static 	int sata_event_pending = 0;
487 static 	int sata_event_thread_active = 0;
488 extern 	pri_t minclsyspri;
489 
490 /*
491  * NCQ error recovery command
492  */
493 static const sata_cmd_t sata_rle_cmd = {
494 	SATA_CMD_REV,
495 	NULL,
496 	{
497 		SATA_DIR_READ
498 	},
499 	ATA_ADDR_LBA48,
500 	0,
501 	0,
502 	0,
503 	0,
504 	0,
505 	1,
506 	READ_LOG_EXT_NCQ_ERROR_RECOVERY,
507 	0,
508 	0,
509 	0,
510 	SATAC_READ_LOG_EXT,
511 	0,
512 	0,
513 	0,
514 };
515 
516 /*
517  * ATAPI error recovery CDB
518  */
519 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = {
520 	SCMD_REQUEST_SENSE,
521 	0,			/* Only fixed RQ format is supported */
522 	0,
523 	0,
524 	SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */
525 	0
526 };
527 
528 
529 /* Warlock directives */
530 
531 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran))
532 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device))
533 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops))
534 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense))
535 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status))
536 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr))
537 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t))
538 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state))
539 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state))
540 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list))
541 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list))
542 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next))
543 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev))
544 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \
545     sata_hba_inst::satahba_scsi_tran))
546 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran))
547 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip))
548 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached))
549 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port))
550 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex,
551     sata_hba_inst::satahba_event_flags))
552 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
553     sata_cport_info::cport_devp))
554 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp))
555 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr))
556 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
557     sata_cport_info::cport_dev_type))
558 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type))
559 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
560     sata_cport_info::cport_state))
561 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state))
562 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
563     sata_pmport_info::pmport_state))
564 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state))
565 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
566     sata_pmport_info::pmport_dev_type))
567 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type))
568 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
569     sata_pmport_info::pmport_sata_drive))
570 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
571     sata_pmport_info::pmport_tgtnode_clean))
572 _NOTE(MUTEX_PROTECTS_DATA(sata_pmport_info::pmport_mutex, \
573     sata_pmport_info::pmport_event_flags))
574 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive))
575 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port))
576 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports))
577 #ifdef SATA_DEBUG
578 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count))
579 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count))
580 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace))
581 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index))
582 #endif
583 
584 /* End of warlock directives */
585 
586 /* ************** loadable module configuration functions ************** */
587 
588 int
589 _init()
590 {
591 	int rval;
592 
593 	mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL);
594 	mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL);
595 	mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL);
596 	cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL);
597 	sata_trace_rbuf_alloc();
598 	if ((rval = mod_install(&modlinkage)) != 0) {
599 #ifdef SATA_DEBUG
600 		cmn_err(CE_WARN, "sata: _init: mod_install failed\n");
601 #endif
602 		sata_trace_rbuf_free();
603 		mutex_destroy(&sata_log_mutex);
604 		cv_destroy(&sata_event_cv);
605 		mutex_destroy(&sata_event_mutex);
606 		mutex_destroy(&sata_mutex);
607 	}
608 	return (rval);
609 }
610 
611 int
612 _fini()
613 {
614 	int rval;
615 
616 	if ((rval = mod_remove(&modlinkage)) != 0)
617 		return (rval);
618 
619 	sata_trace_rbuf_free();
620 	mutex_destroy(&sata_log_mutex);
621 	cv_destroy(&sata_event_cv);
622 	mutex_destroy(&sata_event_mutex);
623 	mutex_destroy(&sata_mutex);
624 	return (rval);
625 }
626 
627 int
628 _info(struct modinfo *modinfop)
629 {
630 	return (mod_info(&modlinkage, modinfop));
631 }
632 
633 
634 
635 /* ********************* SATA HBA entry points ********************* */
636 
637 
638 /*
639  * Called by SATA HBA from _init().
640  * Registers HBA driver instance/sata framework pair with scsi framework, by
641  * calling scsi_hba_init().
642  *
643  * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used
644  * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver
645  * cb_ops pointer in SATA HBA driver dev_ops structure.
646  * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors.
647  *
648  * Return status of the scsi_hba_init() is returned to a calling SATA HBA
649  * driver.
650  */
651 int
652 sata_hba_init(struct modlinkage *modlp)
653 {
654 	int rval;
655 	struct dev_ops *hba_ops;
656 
657 	SATADBG1(SATA_DBG_HBA_IF, NULL,
658 	    "sata_hba_init: name %s \n",
659 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
660 	/*
661 	 * Fill-up cb_ops and dev_ops when necessary
662 	 */
663 	hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
664 	/*
665 	 * Provide pointer to SATA dev_ops
666 	 */
667 	hba_ops->devo_cb_ops = &sata_cb_ops;
668 
669 	/*
670 	 * Register SATA HBA with SCSI framework
671 	 */
672 	if ((rval = scsi_hba_init(modlp)) != 0) {
673 		SATADBG1(SATA_DBG_HBA_IF, NULL,
674 		    "sata_hba_init: scsi hba init failed\n", NULL);
675 		return (rval);
676 	}
677 
678 	return (0);
679 }
680 
681 
682 /* HBA attach stages */
683 #define	HBA_ATTACH_STAGE_SATA_HBA_INST	1
684 #define	HBA_ATTACH_STAGE_SCSI_ATTACHED	2
685 #define	HBA_ATTACH_STAGE_SETUP		4
686 #define	HBA_ATTACH_STAGE_LINKED		8
687 
688 
689 /*
690  *
691  * Called from SATA HBA driver's attach routine to attach an instance of
692  * the HBA.
693  *
694  * For DDI_ATTACH command:
695  * sata_hba_inst structure is allocated here and initialized with pointers to
696  * SATA framework implementation of required scsi tran functions.
697  * The scsi_tran's tran_hba_private field is used by SATA Framework to point
698  * to the soft structure (sata_hba_inst) allocated by SATA framework for
699  * SATA HBA instance related data.
700  * The scsi_tran's tran_hba_private field is used by SATA framework to
701  * store a pointer to per-HBA-instance of sata_hba_inst structure.
702  * The sata_hba_inst structure is cross-linked to scsi tran structure.
703  * Among other info, a pointer to sata_hba_tran structure is stored in
704  * sata_hba_inst. The sata_hba_inst structures for different HBA instances are
705  * linked together into the list, pointed to by sata_hba_list.
706  * On the first HBA instance attach the sata event thread is initialized.
707  * Attachment points are created for all SATA ports of the HBA being attached.
708  * All HBA instance's SATA ports are probed and type of plugged devices is
709  * determined. For each device of a supported type, a target node is created.
710  *
711  * DDI_SUCCESS is returned when attachment process is successful,
712  * DDI_FAILURE is returned otherwise.
713  *
714  * For DDI_RESUME command:
715  * Not implemented at this time (postponed until phase 2 of the development).
716  */
717 int
718 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran,
719     ddi_attach_cmd_t cmd)
720 {
721 	sata_hba_inst_t	*sata_hba_inst;
722 	scsi_hba_tran_t *scsi_tran = NULL;
723 	int hba_attach_state = 0;
724 	char taskq_name[MAXPATHLEN];
725 
726 	SATADBG3(SATA_DBG_HBA_IF, NULL,
727 	    "sata_hba_attach: node %s (%s%d)\n",
728 	    ddi_node_name(dip), ddi_driver_name(dip),
729 	    ddi_get_instance(dip));
730 
731 	if (cmd == DDI_RESUME) {
732 		/*
733 		 * Postponed until phase 2 of the development
734 		 */
735 		return (DDI_FAILURE);
736 	}
737 
738 	if (cmd != DDI_ATTACH) {
739 		return (DDI_FAILURE);
740 	}
741 
742 	/* cmd == DDI_ATTACH */
743 
744 	if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) {
745 		SATA_LOG_D((NULL, CE_WARN,
746 		    "sata_hba_attach: invalid sata_hba_tran"));
747 		return (DDI_FAILURE);
748 	}
749 	/*
750 	 * Allocate and initialize SCSI tran structure.
751 	 * SATA copy of tran_bus_config is provided to create port nodes.
752 	 */
753 	scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
754 	if (scsi_tran == NULL)
755 		return (DDI_FAILURE);
756 	/*
757 	 * Allocate soft structure for SATA HBA instance.
758 	 * There is a separate softstate for each HBA instance.
759 	 */
760 	sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP);
761 	ASSERT(sata_hba_inst != NULL); /* this should not fail */
762 	mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL);
763 	hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST;
764 
765 	/*
766 	 * scsi_trans's tran_hba_private is used by SATA Framework to point to
767 	 * soft structure allocated by SATA framework for
768 	 * SATA HBA instance related data.
769 	 */
770 	scsi_tran->tran_hba_private	= sata_hba_inst;
771 	scsi_tran->tran_tgt_private	= NULL;
772 
773 	scsi_tran->tran_tgt_init	= sata_scsi_tgt_init;
774 	scsi_tran->tran_tgt_probe	= sata_scsi_tgt_probe;
775 	scsi_tran->tran_tgt_free	= sata_scsi_tgt_free;
776 
777 	scsi_tran->tran_start		= sata_scsi_start;
778 	scsi_tran->tran_reset		= sata_scsi_reset;
779 	scsi_tran->tran_abort		= sata_scsi_abort;
780 	scsi_tran->tran_getcap		= sata_scsi_getcap;
781 	scsi_tran->tran_setcap		= sata_scsi_setcap;
782 	scsi_tran->tran_init_pkt	= sata_scsi_init_pkt;
783 	scsi_tran->tran_destroy_pkt	= sata_scsi_destroy_pkt;
784 
785 	scsi_tran->tran_dmafree		= sata_scsi_dmafree;
786 	scsi_tran->tran_sync_pkt	= sata_scsi_sync_pkt;
787 
788 	scsi_tran->tran_reset_notify	= NULL;
789 	scsi_tran->tran_get_bus_addr	= NULL;
790 	scsi_tran->tran_quiesce		= NULL;
791 	scsi_tran->tran_unquiesce	= NULL;
792 	scsi_tran->tran_bus_reset	= NULL;
793 
794 	if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr,
795 	    scsi_tran, 0) != DDI_SUCCESS) {
796 #ifdef SATA_DEBUG
797 		cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed",
798 		    ddi_driver_name(dip), ddi_get_instance(dip));
799 #endif
800 		goto fail;
801 	}
802 	hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED;
803 
804 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) {
805 		if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
806 		    "sata", 1) != DDI_PROP_SUCCESS) {
807 			SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: "
808 			    "failed to create hba sata prop"));
809 			goto fail;
810 		}
811 	}
812 
813 	/*
814 	 * Save pointers in hba instance soft state.
815 	 */
816 	sata_hba_inst->satahba_scsi_tran = scsi_tran;
817 	sata_hba_inst->satahba_tran = sata_tran;
818 	sata_hba_inst->satahba_dip = dip;
819 
820 	/*
821 	 * Create a task queue to handle emulated commands completion
822 	 * Use node name, dash, instance number as the queue name.
823 	 */
824 	taskq_name[0] = '\0';
825 	(void) strlcat(taskq_name, DEVI(dip)->devi_node_name,
826 	    sizeof (taskq_name));
827 	(void) snprintf(taskq_name + strlen(taskq_name),
828 	    sizeof (taskq_name) - strlen(taskq_name),
829 	    "-%d", DEVI(dip)->devi_instance);
830 	sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1,
831 	    minclsyspri, 1, sata_tran->sata_tran_hba_num_cports * 4,
832 	    TASKQ_DYNAMIC);
833 
834 	hba_attach_state |= HBA_ATTACH_STAGE_SETUP;
835 
836 	/*
837 	 * Create events thread if not created yet.
838 	 */
839 	sata_event_thread_control(1);
840 
841 	/*
842 	 * Link this hba instance into the list.
843 	 */
844 	mutex_enter(&sata_mutex);
845 
846 	if (sata_hba_list == NULL) {
847 		/*
848 		 * The first instance of HBA is attached.
849 		 * Set current/active default maximum NCQ/TCQ queue depth for
850 		 * all SATA devices. It is done here and now, to eliminate the
851 		 * possibility of the dynamic, programatic modification of the
852 		 * queue depth via global (and public) sata_max_queue_depth
853 		 * variable (this would require special handling in HBA drivers)
854 		 */
855 		sata_current_max_qdepth = sata_max_queue_depth;
856 		if (sata_current_max_qdepth > 32)
857 			sata_current_max_qdepth = 32;
858 		else if (sata_current_max_qdepth < 1)
859 			sata_current_max_qdepth = 1;
860 	}
861 
862 	sata_hba_inst->satahba_next = NULL;
863 	sata_hba_inst->satahba_prev = sata_hba_list_tail;
864 	if (sata_hba_list == NULL) {
865 		sata_hba_list = sata_hba_inst;
866 	}
867 	if (sata_hba_list_tail != NULL) {
868 		sata_hba_list_tail->satahba_next = sata_hba_inst;
869 	}
870 	sata_hba_list_tail = sata_hba_inst;
871 	mutex_exit(&sata_mutex);
872 	hba_attach_state |= HBA_ATTACH_STAGE_LINKED;
873 
874 	/*
875 	 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl
876 	 * SATA HBA driver should not use its own open/close entry points.
877 	 *
878 	 * Make sure that instance number doesn't overflow
879 	 * when forming minor numbers.
880 	 */
881 	ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT));
882 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
883 	    INST2DEVCTL(ddi_get_instance(dip)),
884 	    DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) {
885 #ifdef SATA_DEBUG
886 		cmn_err(CE_WARN, "sata_hba_attach: "
887 		    "cannot create devctl minor node");
888 #endif
889 		goto fail;
890 	}
891 
892 
893 	/*
894 	 * Set-up kstats here, if necessary.
895 	 * (postponed until future phase of the development).
896 	 */
897 
898 	/*
899 	 * Indicate that HBA is attached. This will enable events processing
900 	 * for this HBA.
901 	 */
902 	sata_hba_inst->satahba_attached = 1;
903 	/*
904 	 * Probe controller ports. This operation will describe a current
905 	 * controller/port/multipliers/device configuration and will create
906 	 * attachment points.
907 	 * We may end-up with just a controller with no devices attached.
908 	 * For the ports with a supported device attached, device target nodes
909 	 * are created and devices are initialized.
910 	 */
911 	sata_probe_ports(sata_hba_inst);
912 
913 	return (DDI_SUCCESS);
914 
915 fail:
916 	if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) {
917 		(void) sata_remove_hba_instance(dip);
918 		if (sata_hba_list == NULL)
919 			sata_event_thread_control(0);
920 	}
921 
922 	if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) {
923 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
924 		taskq_destroy(sata_hba_inst->satahba_taskq);
925 	}
926 
927 	if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED)
928 		(void) scsi_hba_detach(dip);
929 
930 	if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) {
931 		mutex_destroy(&sata_hba_inst->satahba_mutex);
932 		kmem_free((void *)sata_hba_inst,
933 		    sizeof (struct sata_hba_inst));
934 		scsi_hba_tran_free(scsi_tran);
935 	}
936 
937 	sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed",
938 	    ddi_driver_name(dip), ddi_get_instance(dip));
939 
940 	return (DDI_FAILURE);
941 }
942 
943 
944 /*
945  * Called by SATA HBA from to detach an instance of the driver.
946  *
947  * For DDI_DETACH command:
948  * Free local structures allocated for SATA HBA instance during
949  * sata_hba_attach processing.
950  *
951  * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise.
952  *
953  * For DDI_SUSPEND command:
954  * Not implemented at this time (postponed until phase 2 of the development)
955  * Returnd DDI_SUCCESS.
956  *
957  * When the last HBA instance is detached, the event daemon is terminated.
958  *
959  * NOTE: Port multiplier is supported.
960  */
961 int
962 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
963 {
964 	dev_info_t	*tdip;
965 	sata_hba_inst_t	*sata_hba_inst;
966 	scsi_hba_tran_t *scsi_hba_tran;
967 	sata_cport_info_t *cportinfo;
968 	sata_pmult_info_t *pminfo;
969 	sata_drive_info_t *sdinfo;
970 	sata_device_t	sdevice;
971 	int ncport, npmport;
972 
973 	SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n",
974 	    ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip));
975 
976 	switch (cmd) {
977 	case DDI_DETACH:
978 
979 		if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
980 			return (DDI_FAILURE);
981 
982 		sata_hba_inst = scsi_hba_tran->tran_hba_private;
983 		if (sata_hba_inst == NULL)
984 			return (DDI_FAILURE);
985 
986 		if (scsi_hba_detach(dip) == DDI_FAILURE) {
987 			sata_hba_inst->satahba_attached = 1;
988 			return (DDI_FAILURE);
989 		}
990 
991 		/*
992 		 * Free all target nodes - at this point
993 		 * devices should be at least offlined
994 		 * otherwise scsi_hba_detach() should not be called.
995 		 */
996 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
997 		    ncport++) {
998 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
999 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
1000 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
1001 				if (sdinfo != NULL) {
1002 					tdip = sata_get_target_dip(dip,
1003 					    ncport, 0);
1004 					if (tdip != NULL) {
1005 						if (ndi_devi_offline(tdip,
1006 						    NDI_DEVI_REMOVE) !=
1007 						    NDI_SUCCESS) {
1008 							SATA_LOG_D((
1009 							    sata_hba_inst,
1010 							    CE_WARN,
1011 							    "sata_hba_detach: "
1012 							    "Target node not "
1013 							    "removed !"));
1014 							return (DDI_FAILURE);
1015 						}
1016 					}
1017 				}
1018 			} else { /* SATA_DTYPE_PMULT */
1019 				mutex_enter(&cportinfo->cport_mutex);
1020 				pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
1021 
1022 				if (pminfo == NULL) {
1023 					SATA_LOG_D((sata_hba_inst, CE_WARN,
1024 					    "sata_hba_detach: Port multiplier "
1025 					    "not ready yet!"));
1026 					mutex_exit(&cportinfo->cport_mutex);
1027 					return (DDI_FAILURE);
1028 				}
1029 
1030 				/*
1031 				 * Detach would fail if removal of any of the
1032 				 * target nodes is failed - albeit in that
1033 				 * case some of them may have been removed.
1034 				 */
1035 				for (npmport = 0; npmport < SATA_NUM_PMPORTS(
1036 				    sata_hba_inst, ncport); npmport++) {
1037 					tdip = sata_get_target_dip(dip, ncport,
1038 					    npmport);
1039 					if (tdip != NULL) {
1040 						if (ndi_devi_offline(tdip,
1041 						    NDI_DEVI_REMOVE) !=
1042 						    NDI_SUCCESS) {
1043 							SATA_LOG_D((
1044 							    sata_hba_inst,
1045 							    CE_WARN,
1046 							    "sata_hba_detach: "
1047 							    "Target node not "
1048 							    "removed !"));
1049 							mutex_exit(&cportinfo->
1050 							    cport_mutex);
1051 							return (DDI_FAILURE);
1052 						}
1053 					}
1054 				}
1055 				mutex_exit(&cportinfo->cport_mutex);
1056 			}
1057 		}
1058 		/*
1059 		 * Disable sata event daemon processing for this HBA
1060 		 */
1061 		sata_hba_inst->satahba_attached = 0;
1062 
1063 		/*
1064 		 * Remove event daemon thread, if it is last HBA instance.
1065 		 */
1066 
1067 		mutex_enter(&sata_mutex);
1068 		if (sata_hba_list->satahba_next == NULL) {
1069 			mutex_exit(&sata_mutex);
1070 			sata_event_thread_control(0);
1071 			mutex_enter(&sata_mutex);
1072 		}
1073 		mutex_exit(&sata_mutex);
1074 
1075 		/* Remove this HBA instance from the HBA list */
1076 		sata_remove_hba_instance(dip);
1077 
1078 		/*
1079 		 * At this point there should be no target nodes attached.
1080 		 * Detach and destroy device and port info structures.
1081 		 */
1082 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
1083 		    ncport++) {
1084 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
1085 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
1086 				sdinfo =
1087 				    cportinfo->cport_devp.cport_sata_drive;
1088 				if (sdinfo != NULL) {
1089 					/* Release device structure */
1090 					kmem_free(sdinfo,
1091 					    sizeof (sata_drive_info_t));
1092 				}
1093 				/* Release cport info */
1094 				mutex_destroy(&cportinfo->cport_mutex);
1095 				kmem_free(cportinfo,
1096 				    sizeof (sata_cport_info_t));
1097 			} else { /* SATA_DTYPE_PMULT */
1098 				sdevice.satadev_addr.cport = (uint8_t)ncport;
1099 				sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
1100 				sata_free_pmult(sata_hba_inst, &sdevice);
1101 			}
1102 		}
1103 
1104 		scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran);
1105 
1106 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
1107 
1108 		taskq_destroy(sata_hba_inst->satahba_taskq);
1109 
1110 		mutex_destroy(&sata_hba_inst->satahba_mutex);
1111 		kmem_free((void *)sata_hba_inst,
1112 		    sizeof (struct sata_hba_inst));
1113 
1114 		return (DDI_SUCCESS);
1115 
1116 	case DDI_SUSPEND:
1117 		/*
1118 		 * Postponed until phase 2
1119 		 */
1120 		return (DDI_FAILURE);
1121 
1122 	default:
1123 		return (DDI_FAILURE);
1124 	}
1125 }
1126 
1127 
1128 /*
1129  * Called by an HBA drive from _fini() routine.
1130  * Unregisters SATA HBA instance/SATA framework pair from the scsi framework.
1131  */
1132 void
1133 sata_hba_fini(struct modlinkage *modlp)
1134 {
1135 	SATADBG1(SATA_DBG_HBA_IF, NULL,
1136 	    "sata_hba_fini: name %s\n",
1137 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
1138 
1139 	scsi_hba_fini(modlp);
1140 }
1141 
1142 
1143 /*
1144  * Default open and close routine for sata_hba framework.
1145  *
1146  */
1147 /*
1148  * Open devctl node.
1149  *
1150  * Returns:
1151  * 0 if node was open successfully, error code otherwise.
1152  *
1153  *
1154  */
1155 
1156 static int
1157 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1158 {
1159 #ifndef __lock_lint
1160 	_NOTE(ARGUNUSED(credp))
1161 #endif
1162 	int rv = 0;
1163 	dev_info_t *dip;
1164 	scsi_hba_tran_t *scsi_hba_tran;
1165 	sata_hba_inst_t	*sata_hba_inst;
1166 
1167 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL);
1168 
1169 	if (otyp != OTYP_CHR)
1170 		return (EINVAL);
1171 
1172 	dip = sata_devt_to_devinfo(*devp);
1173 	if (dip == NULL)
1174 		return (ENXIO);
1175 
1176 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1177 		return (ENXIO);
1178 
1179 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1180 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1181 		return (ENXIO);
1182 
1183 	mutex_enter(&sata_mutex);
1184 	if (flags & FEXCL) {
1185 		if (sata_hba_inst->satahba_open_flag != 0) {
1186 			rv = EBUSY;
1187 		} else {
1188 			sata_hba_inst->satahba_open_flag =
1189 			    SATA_DEVCTL_EXOPENED;
1190 		}
1191 	} else {
1192 		if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) {
1193 			rv = EBUSY;
1194 		} else {
1195 			sata_hba_inst->satahba_open_flag =
1196 			    SATA_DEVCTL_SOPENED;
1197 		}
1198 	}
1199 	mutex_exit(&sata_mutex);
1200 
1201 	return (rv);
1202 }
1203 
1204 
1205 /*
1206  * Close devctl node.
1207  * Returns:
1208  * 0 if node was closed successfully, error code otherwise.
1209  *
1210  */
1211 
1212 static int
1213 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
1214 {
1215 #ifndef __lock_lint
1216 	_NOTE(ARGUNUSED(credp))
1217 	_NOTE(ARGUNUSED(flag))
1218 #endif
1219 	dev_info_t *dip;
1220 	scsi_hba_tran_t *scsi_hba_tran;
1221 	sata_hba_inst_t	*sata_hba_inst;
1222 
1223 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL);
1224 
1225 	if (otyp != OTYP_CHR)
1226 		return (EINVAL);
1227 
1228 	dip = sata_devt_to_devinfo(dev);
1229 	if (dip == NULL)
1230 		return (ENXIO);
1231 
1232 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1233 		return (ENXIO);
1234 
1235 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1236 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1237 		return (ENXIO);
1238 
1239 	mutex_enter(&sata_mutex);
1240 	sata_hba_inst->satahba_open_flag = 0;
1241 	mutex_exit(&sata_mutex);
1242 	return (0);
1243 }
1244 
1245 
1246 
1247 /*
1248  * Standard IOCTL commands for SATA hotplugging.
1249  * Implemented DEVCTL_AP commands:
1250  * DEVCTL_AP_CONNECT
1251  * DEVCTL_AP_DISCONNECT
1252  * DEVCTL_AP_CONFIGURE
1253  * DEVCTL_UNCONFIGURE
1254  * DEVCTL_AP_CONTROL
1255  *
1256  * Commands passed to default ndi ioctl handler:
1257  * DEVCTL_DEVICE_GETSTATE
1258  * DEVCTL_DEVICE_ONLINE
1259  * DEVCTL_DEVICE_OFFLINE
1260  * DEVCTL_DEVICE_REMOVE
1261  * DEVCTL_DEVICE_INSERT
1262  * DEVCTL_BUS_GETSTATE
1263  *
1264  * All other cmds are passed to HBA if it provide ioctl handler, or failed
1265  * if not.
1266  *
1267  * Returns:
1268  * 0 if successful,
1269  * error code if operation failed.
1270  *
1271  * Port Multiplier support is supported now.
1272  *
1273  * NOTE: qual should be SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT
1274  */
1275 
1276 static int
1277 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1278     int *rvalp)
1279 {
1280 #ifndef __lock_lint
1281 	_NOTE(ARGUNUSED(credp))
1282 	_NOTE(ARGUNUSED(rvalp))
1283 #endif
1284 	int rv = 0;
1285 	int32_t	comp_port = -1;
1286 	dev_info_t *dip;
1287 	devctl_ap_state_t ap_state;
1288 	struct devctl_iocdata *dcp = NULL;
1289 	scsi_hba_tran_t *scsi_hba_tran;
1290 	sata_hba_inst_t *sata_hba_inst;
1291 	sata_device_t sata_device;
1292 	sata_cport_info_t *cportinfo;
1293 	int cport, pmport, qual;
1294 	int rval = SATA_SUCCESS;
1295 
1296 	dip = sata_devt_to_devinfo(dev);
1297 	if (dip == NULL)
1298 		return (ENXIO);
1299 
1300 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1301 		return (ENXIO);
1302 
1303 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1304 	if (sata_hba_inst == NULL)
1305 		return (ENXIO);
1306 
1307 	if (sata_hba_inst->satahba_tran == NULL)
1308 		return (ENXIO);
1309 
1310 	switch (cmd) {
1311 
1312 	case DEVCTL_DEVICE_GETSTATE:
1313 	case DEVCTL_DEVICE_ONLINE:
1314 	case DEVCTL_DEVICE_OFFLINE:
1315 	case DEVCTL_DEVICE_REMOVE:
1316 	case DEVCTL_BUS_GETSTATE:
1317 		/*
1318 		 * There may be more cases that we want to pass to default
1319 		 * handler rather than fail them.
1320 		 */
1321 		return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
1322 	}
1323 
1324 	/* read devctl ioctl data */
1325 	if (cmd != DEVCTL_AP_CONTROL) {
1326 		if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1327 			return (EFAULT);
1328 
1329 		if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) ==
1330 		    -1) {
1331 			if (dcp)
1332 				ndi_dc_freehdl(dcp);
1333 			return (EINVAL);
1334 		}
1335 
1336 		/*
1337 		 * According to SCSI_TO_SATA_ADDR_QUAL, qual should be either
1338 		 * SATA_ADDR_DCPORT or SATA_ADDR_DPMPORT.
1339 		 */
1340 		cport = SCSI_TO_SATA_CPORT(comp_port);
1341 		pmport = SCSI_TO_SATA_PMPORT(comp_port);
1342 		qual = SCSI_TO_SATA_ADDR_QUAL(comp_port);
1343 
1344 		if (sata_validate_sata_address(sata_hba_inst, cport, pmport,
1345 		    qual) != 0) {
1346 			ndi_dc_freehdl(dcp);
1347 			return (EINVAL);
1348 		}
1349 
1350 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1351 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1352 		    cport_mutex);
1353 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1354 			/*
1355 			 * Cannot process ioctl request now. Come back later.
1356 			 */
1357 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1358 			    cport_mutex);
1359 			ndi_dc_freehdl(dcp);
1360 			return (EBUSY);
1361 		}
1362 		/* Block event processing for this port */
1363 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1364 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1365 
1366 		sata_device.satadev_addr.cport = cport;
1367 		sata_device.satadev_addr.pmport = pmport;
1368 		sata_device.satadev_addr.qual = qual;
1369 		sata_device.satadev_rev = SATA_DEVICE_REV;
1370 	}
1371 
1372 	switch (cmd) {
1373 
1374 	case DEVCTL_AP_DISCONNECT:
1375 
1376 		/*
1377 		 * Normally, cfgadm sata plugin will try to offline
1378 		 * (unconfigure) device before this request. Nevertheless,
1379 		 * if a device is still configured, we need to
1380 		 * attempt to offline and unconfigure device first, and we will
1381 		 * deactivate the port regardless of the unconfigure
1382 		 * operation results.
1383 		 *
1384 		 */
1385 		rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device);
1386 
1387 		break;
1388 
1389 	case DEVCTL_AP_UNCONFIGURE:
1390 
1391 		/*
1392 		 * The unconfigure operation uses generic nexus operation to
1393 		 * offline a device. It leaves a target device node attached.
1394 		 * and obviously sata_drive_info attached as well, because
1395 		 * from the hardware point of view nothing has changed.
1396 		 */
1397 		rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device);
1398 		break;
1399 
1400 	case DEVCTL_AP_CONNECT:
1401 	{
1402 		/*
1403 		 * The sata cfgadm pluging will invoke this operation only if
1404 		 * port was found in the disconnect state (failed state
1405 		 * is also treated as the disconnected state).
1406 		 * If port activation is successful and a device is found
1407 		 * attached to the port, the initialization sequence is
1408 		 * executed to probe the port and attach
1409 		 * a device structure to a port structure. The device is not
1410 		 * set in configured state (system-wise) by this operation.
1411 		 */
1412 
1413 		rv = sata_ioctl_connect(sata_hba_inst, &sata_device);
1414 
1415 		break;
1416 	}
1417 
1418 	case DEVCTL_AP_CONFIGURE:
1419 	{
1420 		/*
1421 		 * A port may be in an active or shutdown state.
1422 		 * If port is in a failed state, operation is aborted.
1423 		 * If a port is in a shutdown state, sata_tran_port_activate()
1424 		 * is invoked prior to any other operation.
1425 		 *
1426 		 * Onlining the device involves creating a new target node.
1427 		 * If there is an old target node present (belonging to
1428 		 * previously removed device), the operation is aborted - the
1429 		 * old node has to be released and removed before configure
1430 		 * operation is attempted.
1431 		 */
1432 
1433 		rv = sata_ioctl_configure(sata_hba_inst, &sata_device);
1434 
1435 		break;
1436 	}
1437 
1438 	case DEVCTL_AP_GETSTATE:
1439 
1440 		sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state);
1441 
1442 		ap_state.ap_last_change = (time_t)-1;
1443 		ap_state.ap_error_code = 0;
1444 		ap_state.ap_in_transition = 0;
1445 
1446 		/* Copy the return AP-state information to the user space */
1447 		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
1448 			rv = EFAULT;
1449 		}
1450 		break;
1451 
1452 	case DEVCTL_AP_CONTROL:
1453 	{
1454 		/*
1455 		 * Generic devctl for hardware specific functionality
1456 		 */
1457 		sata_ioctl_data_t	ioc;
1458 
1459 		ASSERT(dcp == NULL);
1460 
1461 		/* Copy in user ioctl data first */
1462 #ifdef _MULTI_DATAMODEL
1463 		if (ddi_model_convert_from(mode & FMODELS) ==
1464 		    DDI_MODEL_ILP32) {
1465 
1466 			sata_ioctl_data_32_t	ioc32;
1467 
1468 			if (ddi_copyin((void *)arg, (void *)&ioc32,
1469 			    sizeof (ioc32), mode) != 0) {
1470 				rv = EFAULT;
1471 				break;
1472 			}
1473 			ioc.cmd 	= (uint_t)ioc32.cmd;
1474 			ioc.port	= (uint_t)ioc32.port;
1475 			ioc.get_size	= (uint_t)ioc32.get_size;
1476 			ioc.buf		= (caddr_t)(uintptr_t)ioc32.buf;
1477 			ioc.bufsiz	= (uint_t)ioc32.bufsiz;
1478 			ioc.misc_arg	= (uint_t)ioc32.misc_arg;
1479 		} else
1480 #endif /* _MULTI_DATAMODEL */
1481 		if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
1482 		    mode) != 0) {
1483 			return (EFAULT);
1484 		}
1485 
1486 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
1487 		    "sata_hba_ioctl: DEVCTL_AP_CONTROL "
1488 		    "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port);
1489 
1490 		/*
1491 		 * To avoid BE/LE and 32/64 issues, a get_size always returns
1492 		 * a 32-bit number.
1493 		 */
1494 		if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
1495 			return (EINVAL);
1496 		}
1497 		/* validate address */
1498 		cport = SCSI_TO_SATA_CPORT(ioc.port);
1499 		pmport = SCSI_TO_SATA_PMPORT(ioc.port);
1500 		qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port);
1501 
1502 		SATADBG3(SATA_DBG_IOCTL_IF, sata_hba_inst,
1503 		    "sata_hba_ioctl: target port is %d:%d (%d)",
1504 		    cport, pmport, qual);
1505 
1506 		if (sata_validate_sata_address(sata_hba_inst, cport,
1507 		    pmport, qual) != 0)
1508 			return (EINVAL);
1509 
1510 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1511 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1512 		    cport_mutex);
1513 		/* Is the port locked by event processing daemon ? */
1514 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1515 			/*
1516 			 * Cannot process ioctl request now. Come back later
1517 			 */
1518 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1519 			    cport_mutex);
1520 			return (EBUSY);
1521 		}
1522 		/* Block event processing for this port */
1523 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1524 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1525 
1526 
1527 		sata_device.satadev_addr.cport = cport;
1528 		sata_device.satadev_addr.pmport = pmport;
1529 		sata_device.satadev_addr.qual = qual;
1530 		sata_device.satadev_rev = SATA_DEVICE_REV;
1531 
1532 		switch (ioc.cmd) {
1533 
1534 		case SATA_CFGA_RESET_PORT:
1535 			/*
1536 			 * There is no protection for configured device.
1537 			 */
1538 			rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device);
1539 			break;
1540 
1541 		case SATA_CFGA_RESET_DEVICE:
1542 			/*
1543 			 * There is no protection for configured device.
1544 			 */
1545 			rv = sata_ioctl_reset_device(sata_hba_inst,
1546 			    &sata_device);
1547 			break;
1548 
1549 		case SATA_CFGA_RESET_ALL:
1550 			/*
1551 			 * There is no protection for configured devices.
1552 			 */
1553 			rv = sata_ioctl_reset_all(sata_hba_inst);
1554 			/*
1555 			 * We return here, because common return is for
1556 			 * a single port operation - we have already unlocked
1557 			 * all ports and no dc handle was allocated.
1558 			 */
1559 			return (rv);
1560 
1561 		case SATA_CFGA_PORT_DEACTIVATE:
1562 			/*
1563 			 * Arbitrarily unconfigure attached device, if any.
1564 			 * Even if the unconfigure fails, proceed with the
1565 			 * port deactivation.
1566 			 */
1567 			rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device);
1568 
1569 			break;
1570 
1571 		case SATA_CFGA_PORT_ACTIVATE:
1572 
1573 			rv = sata_ioctl_activate(sata_hba_inst, &sata_device);
1574 			break;
1575 
1576 		case SATA_CFGA_PORT_SELF_TEST:
1577 
1578 			rv = sata_ioctl_port_self_test(sata_hba_inst,
1579 			    &sata_device);
1580 			break;
1581 
1582 		case SATA_CFGA_GET_DEVICE_PATH:
1583 
1584 			rv = sata_ioctl_get_device_path(sata_hba_inst,
1585 			    &sata_device, &ioc, mode);
1586 			break;
1587 
1588 		case SATA_CFGA_GET_AP_TYPE:
1589 
1590 			rv = sata_ioctl_get_ap_type(sata_hba_inst,
1591 			    &sata_device, &ioc, mode);
1592 			break;
1593 
1594 		case SATA_CFGA_GET_MODEL_INFO:
1595 
1596 			rv = sata_ioctl_get_model_info(sata_hba_inst,
1597 			    &sata_device, &ioc, mode);
1598 			break;
1599 
1600 		case SATA_CFGA_GET_REVFIRMWARE_INFO:
1601 
1602 			rv = sata_ioctl_get_revfirmware_info(sata_hba_inst,
1603 			    &sata_device, &ioc, mode);
1604 			break;
1605 
1606 		case SATA_CFGA_GET_SERIALNUMBER_INFO:
1607 
1608 			rv = sata_ioctl_get_serialnumber_info(sata_hba_inst,
1609 			    &sata_device, &ioc, mode);
1610 			break;
1611 
1612 		default:
1613 			rv = EINVAL;
1614 			break;
1615 
1616 		} /* End of DEVCTL_AP_CONTROL cmd switch */
1617 
1618 		break;
1619 	}
1620 
1621 	default:
1622 	{
1623 		/*
1624 		 * If we got here, we got an IOCTL that SATA HBA Framework
1625 		 * does not recognize. Pass ioctl to HBA driver, in case
1626 		 * it could process it.
1627 		 */
1628 		sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran;
1629 		dev_info_t	*mydip = SATA_DIP(sata_hba_inst);
1630 
1631 		SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1632 		    "IOCTL 0x%2x not supported in SATA framework, "
1633 		    "passthrough to HBA", cmd);
1634 
1635 		if (sata_tran->sata_tran_ioctl == NULL) {
1636 			rv = EINVAL;
1637 			break;
1638 		}
1639 		rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg);
1640 		if (rval != 0) {
1641 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1642 			    "IOCTL 0x%2x failed in HBA", cmd);
1643 			rv = rval;
1644 		}
1645 		break;
1646 	}
1647 
1648 	} /* End of main IOCTL switch */
1649 
1650 	if (dcp) {
1651 		ndi_dc_freehdl(dcp);
1652 	}
1653 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1654 	cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
1655 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1656 
1657 	return (rv);
1658 }
1659 
1660 
1661 /*
1662  * Create error retrieval sata packet
1663  *
1664  * A sata packet is allocated and set-up to contain specified error retrieval
1665  * command and appropriate dma-able data buffer.
1666  * No association with any scsi packet is made and no callback routine is
1667  * specified.
1668  *
1669  * Returns a pointer to sata packet upon successfull packet creation.
1670  * Returns NULL, if packet cannot be created.
1671  */
1672 sata_pkt_t *
1673 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device,
1674     int pkt_type)
1675 {
1676 	sata_hba_inst_t	*sata_hba_inst;
1677 	sata_pkt_txlate_t *spx;
1678 	sata_pkt_t *spkt;
1679 	sata_drive_info_t *sdinfo;
1680 
1681 	mutex_enter(&sata_mutex);
1682 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1683 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1684 		if (SATA_DIP(sata_hba_inst) == dip)
1685 			break;
1686 	}
1687 	mutex_exit(&sata_mutex);
1688 	ASSERT(sata_hba_inst != NULL);
1689 
1690 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
1691 	if (sdinfo == NULL) {
1692 		sata_log(sata_hba_inst, CE_WARN,
1693 		    "sata: error recovery request for non-attached device at "
1694 		    "cport %d", sata_device->satadev_addr.cport);
1695 		return (NULL);
1696 	}
1697 
1698 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1699 	spx->txlt_sata_hba_inst = sata_hba_inst;
1700 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
1701 	spkt = sata_pkt_alloc(spx, NULL);
1702 	if (spkt == NULL) {
1703 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1704 		return (NULL);
1705 	}
1706 	/* address is needed now */
1707 	spkt->satapkt_device.satadev_addr = sata_device->satadev_addr;
1708 
1709 	switch (pkt_type) {
1710 	case SATA_ERR_RETR_PKT_TYPE_NCQ:
1711 		if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS)
1712 			return (spkt);
1713 		break;
1714 
1715 	case SATA_ERR_RETR_PKT_TYPE_ATAPI:
1716 		if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS)
1717 			return (spkt);
1718 		break;
1719 
1720 	default:
1721 		break;
1722 	}
1723 
1724 	sata_pkt_free(spx);
1725 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1726 	return (NULL);
1727 
1728 }
1729 
1730 
1731 /*
1732  * Free error retrieval sata packet
1733  *
1734  * Free sata packet and any associated resources allocated previously by
1735  * sata_get_error_retrieval_pkt().
1736  *
1737  * Void return.
1738  */
1739 void
1740 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt)
1741 {
1742 	sata_pkt_txlate_t *spx =
1743 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1744 
1745 	ASSERT(sata_pkt != NULL);
1746 
1747 	sata_free_local_buffer(spx);
1748 	sata_pkt_free(spx);
1749 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1750 
1751 }
1752 
1753 /*
1754  * Create READ PORT MULTIPLIER and WRITE PORT MULTIPLIER sata packet
1755  *
1756  * No association with any scsi packet is made and no callback routine is
1757  * specified.
1758  *
1759  * Returns a pointer to sata packet upon successfull packet creation.
1760  * Returns NULL, if packet cannot be created.
1761  *
1762  * NOTE: Input/Output value includes 64 bits accoring to SATA Spec 2.6,
1763  * only lower 32 bits are available currently.
1764  */
1765 sata_pkt_t *
1766 sata_get_rdwr_pmult_pkt(dev_info_t *dip, sata_device_t *sd,
1767     uint8_t regn, uint32_t regv, uint32_t type)
1768 {
1769 	sata_hba_inst_t	*sata_hba_inst;
1770 	sata_pkt_txlate_t *spx;
1771 	sata_pkt_t *spkt;
1772 	sata_cmd_t *scmd;
1773 
1774 	/* Only READ/WRITE commands are accepted. */
1775 	ASSERT(type == SATA_RDWR_PMULT_PKT_TYPE_READ ||
1776 	    type == SATA_RDWR_PMULT_PKT_TYPE_WRITE);
1777 
1778 	mutex_enter(&sata_mutex);
1779 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1780 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1781 		if (SATA_DIP(sata_hba_inst) == dip)
1782 			break;
1783 	}
1784 	mutex_exit(&sata_mutex);
1785 	ASSERT(sata_hba_inst != NULL);
1786 
1787 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1788 	spx->txlt_sata_hba_inst = sata_hba_inst;
1789 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
1790 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
1791 	if (spkt == NULL) {
1792 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1793 		return (NULL);
1794 	}
1795 
1796 	/*
1797 	 * NOTE: We need to send this command to the port multiplier,
1798 	 * that means send to SATA_PMULT_HOSTPORT(0xf) pmport
1799 	 *
1800 	 * sata_device contains the address of actual target device, and the
1801 	 * pmport number in the command comes from the sata_device structure.
1802 	 */
1803 	spkt->satapkt_device.satadev_addr = sd->satadev_addr;
1804 	spkt->satapkt_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
1805 	spkt->satapkt_device.satadev_addr.qual = SATA_ADDR_PMULT;
1806 
1807 	/* Fill sata_pkt */
1808 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_POLLING;
1809 	spkt->satapkt_comp = NULL; /* Synchronous mode, no callback */
1810 	spkt->satapkt_time = 10; /* Timeout 10s */
1811 
1812 	/* Build READ PORT MULTIPLIER cmd in the sata_pkt */
1813 	scmd = &spkt->satapkt_cmd;
1814 	scmd->satacmd_features_reg = regn & 0xff;
1815 	scmd->satacmd_features_reg_ext = (regn >> 8) & 0xff;
1816 	scmd->satacmd_device_reg = sd->satadev_addr.pmport;
1817 	scmd->satacmd_addr_type = 0;		/* N/A */
1818 
1819 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
1820 
1821 	if (type == SATA_RDWR_PMULT_PKT_TYPE_READ) {
1822 		scmd->satacmd_cmd_reg = SATAC_READ_PORTMULT;
1823 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
1824 		scmd->satacmd_flags.sata_special_regs = 1;
1825 		scmd->satacmd_flags.sata_copy_out_lba_high_lsb = 1;
1826 		scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = 1;
1827 		scmd->satacmd_flags.sata_copy_out_lba_low_lsb = 1;
1828 		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = 1;
1829 	} else if (type == SATA_RDWR_PMULT_PKT_TYPE_WRITE) {
1830 		scmd->satacmd_cmd_reg = SATAC_WRITE_PORTMULT;
1831 		scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
1832 		scmd->satacmd_sec_count_lsb = regv & 0xff;
1833 		scmd->satacmd_lba_low_lsb = regv >> 8 & 0xff;
1834 		scmd->satacmd_lba_mid_lsb = regv >> 16 & 0xff;
1835 		scmd->satacmd_lba_high_lsb = regv >> 24 & 0xff;
1836 	}
1837 
1838 	return (spkt);
1839 }
1840 
1841 /*
1842  * Free sata packet and any associated resources allocated previously by
1843  * sata_get_rdwr_pmult_pkt().
1844  *
1845  * Void return.
1846  */
1847 void
1848 sata_free_rdwr_pmult_pkt(sata_pkt_t *sata_pkt)
1849 {
1850 	sata_pkt_txlate_t *spx =
1851 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1852 
1853 	/* Free allocated resources */
1854 	sata_pkt_free(spx);
1855 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1856 }
1857 
1858 /*
1859  * Register a port multiplier to framework.
1860  * 1) Store the GSCR values in the previous allocated pmult_info strctures.
1861  * 2) Search in the blacklist and update the number of the device ports of the
1862  * port multiplier.
1863  *
1864  * Void return.
1865  */
1866 void
1867 sata_register_pmult(dev_info_t *dip, sata_device_t *sd, sata_pmult_gscr_t *sg)
1868 {
1869 	sata_hba_inst_t *sata_hba_inst = NULL;
1870 	sata_pmult_info_t *pmultinfo;
1871 	sata_pmult_bl_t *blp;
1872 	int cport = sd->satadev_addr.cport;
1873 
1874 	mutex_enter(&sata_mutex);
1875 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1876 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1877 		if (SATA_DIP(sata_hba_inst) == dip)
1878 			if (sata_hba_inst->satahba_attached == 1)
1879 				break;
1880 	}
1881 	mutex_exit(&sata_mutex);
1882 	/* HBA not attached? */
1883 	if (sata_hba_inst == NULL)
1884 		return;
1885 
1886 	/* Number of pmports */
1887 	sd->satadev_add_info = sg->gscr2 & SATA_PMULT_PORTNUM_MASK;
1888 
1889 	/* Check the blacklist */
1890 	for (blp = sata_pmult_blacklist; blp->bl_gscr0; blp++) {
1891 		if (sg->gscr0 != blp->bl_gscr0 && blp->bl_gscr0)
1892 			continue;
1893 		if (sg->gscr1 != blp->bl_gscr1 && blp->bl_gscr1)
1894 			continue;
1895 		if (sg->gscr2 != blp->bl_gscr2 && blp->bl_gscr2)
1896 			continue;
1897 
1898 		cmn_err(CE_WARN, "!Port multiplier is on the blacklist.");
1899 		sd->satadev_add_info = blp->bl_flags;
1900 		break;
1901 	}
1902 
1903 	/* Register the port multiplier GSCR */
1904 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
1905 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
1906 	if (pmultinfo != NULL) {
1907 		pmultinfo->pmult_gscr = *sg;
1908 		pmultinfo->pmult_num_dev_ports =
1909 		    sd->satadev_add_info & SATA_PMULT_PORTNUM_MASK;
1910 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
1911 		    "Port multiplier registered at port %d", cport);
1912 	}
1913 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
1914 }
1915 
1916 /*
1917  * sata_name_child is for composing the name of the node
1918  * the format of the name is "target,0".
1919  */
1920 static int
1921 sata_name_child(dev_info_t *dip, char *name, int namelen)
1922 {
1923 	int target;
1924 
1925 	target = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1926 	    DDI_PROP_DONTPASS, "target", -1);
1927 	if (target == -1)
1928 		return (DDI_FAILURE);
1929 	(void) snprintf(name, namelen, "%x,0", target);
1930 	return (DDI_SUCCESS);
1931 }
1932 
1933 
1934 
1935 /* ****************** SCSA required entry points *********************** */
1936 
1937 /*
1938  * Implementation of scsi tran_tgt_init.
1939  * sata_scsi_tgt_init() initializes scsi_device structure
1940  *
1941  * If successful, DDI_SUCCESS is returned.
1942  * DDI_FAILURE is returned if addressed device does not exist
1943  */
1944 
1945 static int
1946 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1947     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1948 {
1949 #ifndef __lock_lint
1950 	_NOTE(ARGUNUSED(hba_dip))
1951 	_NOTE(ARGUNUSED(tgt_dip))
1952 #endif
1953 	sata_device_t		sata_device;
1954 	sata_drive_info_t	*sdinfo;
1955 	struct sata_id		*sid;
1956 	sata_hba_inst_t		*sata_hba_inst;
1957 	char			model[SATA_ID_MODEL_LEN + 1];
1958 	char			fw[SATA_ID_FW_LEN + 1];
1959 	char			*vid, *pid;
1960 	int			i;
1961 
1962 	/*
1963 	 * Fail tran_tgt_init for .conf stub node
1964 	 */
1965 	if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
1966 		(void) ndi_merge_node(tgt_dip, sata_name_child);
1967 		ddi_set_name_addr(tgt_dip, NULL);
1968 		return (DDI_FAILURE);
1969 	}
1970 
1971 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
1972 
1973 	/* Validate scsi device address */
1974 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
1975 	    &sata_device) != 0)
1976 		return (DDI_FAILURE);
1977 
1978 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1979 	    sata_device.satadev_addr.cport)));
1980 
1981 	/* sata_device now contains a valid sata address */
1982 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
1983 	if (sdinfo == NULL) {
1984 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1985 		    sata_device.satadev_addr.cport)));
1986 		return (DDI_FAILURE);
1987 	}
1988 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1989 	    sata_device.satadev_addr.cport)));
1990 
1991 	/*
1992 	 * Check if we need to create a legacy devid (i.e cmdk style) for
1993 	 * the target disks.
1994 	 *
1995 	 * HBA devinfo node will have the property "use-cmdk-devid-format"
1996 	 * if we need to create cmdk-style devid for all the disk devices
1997 	 * attached to this controller. This property may have been set
1998 	 * from HBA driver's .conf file or by the HBA driver in its
1999 	 * attach(9F) function.
2000 	 */
2001 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2002 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2003 	    "use-cmdk-devid-format", 0) == 1)) {
2004 		/* register a legacy devid for this target node */
2005 		sata_target_devid_register(tgt_dip, sdinfo);
2006 	}
2007 
2008 
2009 	/*
2010 	 * 'Identify Device Data' does not always fit in standard SCSI
2011 	 * INQUIRY data, so establish INQUIRY_* properties with full-form
2012 	 * of information.
2013 	 */
2014 	sid = &sdinfo->satadrv_id;
2015 #ifdef	_LITTLE_ENDIAN
2016 	swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
2017 	swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
2018 #else	/* _LITTLE_ENDIAN */
2019 	bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN);
2020 	bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN);
2021 #endif	/* _LITTLE_ENDIAN */
2022 	model[SATA_ID_MODEL_LEN] = 0;
2023 	fw[SATA_ID_FW_LEN] = 0;
2024 
2025 	/* split model into into vid/pid */
2026 	for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++)
2027 		if ((*pid == ' ') || (*pid == '\t'))
2028 			break;
2029 	if (i < SATA_ID_MODEL_LEN) {
2030 		vid = model;
2031 		*pid++ = 0;		/* terminate vid, establish pid */
2032 	} else {
2033 		vid = NULL;		/* vid will stay "ATA     " */
2034 		pid = model;		/* model is all pid */
2035 	}
2036 
2037 	if (vid)
2038 		(void) scsi_device_prop_update_inqstring(sd, INQUIRY_VENDOR_ID,
2039 		    vid, strlen(vid));
2040 	if (pid)
2041 		(void) scsi_device_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID,
2042 		    pid, strlen(pid));
2043 	(void) scsi_device_prop_update_inqstring(sd, INQUIRY_REVISION_ID,
2044 	    fw, strlen(fw));
2045 
2046 	return (DDI_SUCCESS);
2047 }
2048 
2049 /*
2050  * Implementation of scsi tran_tgt_probe.
2051  * Probe target, by calling default scsi routine scsi_hba_probe()
2052  */
2053 static int
2054 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void))
2055 {
2056 	sata_hba_inst_t *sata_hba_inst =
2057 	    (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private);
2058 	int rval;
2059 	uint32_t pm_cap;
2060 
2061 	rval = scsi_hba_probe(sd, callback);
2062 	pm_cap = SATA_CAP_POWER_CONDITON | SATA_CAP_SMART_PAGE |
2063 	    SATA_CAP_LOG_SENSE;
2064 
2065 	if (rval == SCSIPROBE_EXISTS) {
2066 		/*
2067 		 * Set property "pm-capable" on the target device node, so that
2068 		 * the target driver will not try to fetch scsi cycle counters
2069 		 * before enabling device power-management.
2070 		 */
2071 		if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
2072 		    "pm-capable", pm_cap)) != DDI_PROP_SUCCESS) {
2073 			sata_log(sata_hba_inst, CE_WARN,
2074 			    "SATA device at port %d: "
2075 			    "will not be power-managed ",
2076 			    SCSI_TO_SATA_CPORT(sd->sd_address.a_target));
2077 			SATA_LOG_D((sata_hba_inst, CE_WARN,
2078 			    "failure updating pm-capable property"));
2079 		}
2080 	}
2081 	return (rval);
2082 }
2083 
2084 /*
2085  * Implementation of scsi tran_tgt_free.
2086  * Release all resources allocated for scsi_device
2087  */
2088 static void
2089 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2090     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2091 {
2092 #ifndef __lock_lint
2093 	_NOTE(ARGUNUSED(hba_dip))
2094 #endif
2095 	sata_device_t		sata_device;
2096 	sata_drive_info_t	*sdinfo;
2097 	sata_hba_inst_t		*sata_hba_inst;
2098 	ddi_devid_t		devid;
2099 
2100 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2101 
2102 	/* Validate scsi device address */
2103 	/*
2104 	 * Note: tgt_free relates to the SCSA view of a device. If called, there
2105 	 * was a device at this address, so even if the sata framework internal
2106 	 * resources were alredy released because a device was detached,
2107 	 * this function should be executed as long as its actions do
2108 	 * not require the internal sata view of a device and the address
2109 	 * refers to a valid sata address.
2110 	 * Validating the address here means that we do not trust SCSA...
2111 	 */
2112 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2113 	    &sata_device) == -1)
2114 		return;
2115 
2116 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2117 	    sata_device.satadev_addr.cport)));
2118 
2119 	/* sata_device now should contain a valid sata address */
2120 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2121 	if (sdinfo == NULL) {
2122 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2123 		    sata_device.satadev_addr.cport)));
2124 		return;
2125 	}
2126 	/*
2127 	 * We did not allocate any resources in sata_scsi_tgt_init()
2128 	 * other than few properties.
2129 	 * Free them.
2130 	 */
2131 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2132 	    sata_device.satadev_addr.cport)));
2133 	(void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable");
2134 
2135 	/*
2136 	 * If devid was previously created but not freed up from
2137 	 * sd(7D) driver (i.e during detach(9F)) then do it here.
2138 	 */
2139 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2140 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2141 	    "use-cmdk-devid-format", 0) == 1) &&
2142 	    (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) {
2143 		ddi_devid_unregister(tgt_dip);
2144 		ddi_devid_free(devid);
2145 	}
2146 }
2147 
2148 /*
2149  * Implementation of scsi tran_init_pkt
2150  * Upon successful return, scsi pkt buffer has DMA resources allocated.
2151  *
2152  * It seems that we should always allocate pkt, even if the address is
2153  * for non-existing device - just use some default for dma_attr.
2154  * The reason is that there is no way to communicate this to a caller here.
2155  * Subsequent call to sata_scsi_start may fail appropriately.
2156  * Simply returning NULL does not seem to discourage a target driver...
2157  *
2158  * Returns a pointer to initialized scsi_pkt, or NULL otherwise.
2159  */
2160 static struct scsi_pkt *
2161 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
2162     struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
2163     int (*callback)(caddr_t), caddr_t arg)
2164 {
2165 	sata_hba_inst_t *sata_hba_inst =
2166 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2167 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
2168 	sata_device_t sata_device;
2169 	sata_drive_info_t *sdinfo;
2170 	sata_pkt_txlate_t *spx;
2171 	ddi_dma_attr_t cur_dma_attr;
2172 	int rval;
2173 	boolean_t new_pkt = TRUE;
2174 
2175 	ASSERT(ap->a_hba_tran->tran_hba_dip == dip);
2176 
2177 	/*
2178 	 * We need to translate the address, even if it could be
2179 	 * a bogus one, for a non-existing device
2180 	 */
2181 	sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
2182 	sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target);
2183 	sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2184 	sata_device.satadev_rev = SATA_DEVICE_REV;
2185 
2186 	if (pkt == NULL) {
2187 		/*
2188 		 * Have to allocate a brand new scsi packet.
2189 		 * We need to operate with auto request sense enabled.
2190 		 */
2191 		pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen,
2192 		    MAX(statuslen, sizeof (struct scsi_arq_status)),
2193 		    tgtlen, sizeof (sata_pkt_txlate_t), callback, arg);
2194 
2195 		if (pkt == NULL)
2196 			return (NULL);
2197 
2198 		/* Fill scsi packet structure */
2199 		pkt->pkt_comp		= (void (*)())NULL;
2200 		pkt->pkt_time		= 0;
2201 		pkt->pkt_resid		= 0;
2202 		pkt->pkt_statistics	= 0;
2203 		pkt->pkt_reason		= 0;
2204 
2205 		/*
2206 		 * pkt_hba_private will point to sata pkt txlate structure
2207 		 */
2208 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2209 		bzero(spx, sizeof (sata_pkt_txlate_t));
2210 
2211 		spx->txlt_scsi_pkt = pkt;
2212 		spx->txlt_sata_hba_inst = sata_hba_inst;
2213 
2214 		/* Allocate sata_pkt */
2215 		spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback);
2216 		if (spx->txlt_sata_pkt == NULL) {
2217 			/* Could not allocate sata pkt */
2218 			scsi_hba_pkt_free(ap, pkt);
2219 			return (NULL);
2220 		}
2221 		/* Set sata address */
2222 		spx->txlt_sata_pkt->satapkt_device.satadev_addr =
2223 		    sata_device.satadev_addr;
2224 		spx->txlt_sata_pkt->satapkt_device.satadev_rev =
2225 		    sata_device.satadev_rev;
2226 
2227 		if ((bp == NULL) || (bp->b_bcount == 0))
2228 			return (pkt);
2229 
2230 		spx->txlt_total_residue = bp->b_bcount;
2231 	} else {
2232 		new_pkt = FALSE;
2233 		/*
2234 		 * Packet was preallocated/initialized by previous call
2235 		 */
2236 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2237 
2238 		if ((bp == NULL) || (bp->b_bcount == 0)) {
2239 			return (pkt);
2240 		}
2241 
2242 		/* Pkt is available already: spx->txlt_scsi_pkt == pkt; */
2243 	}
2244 
2245 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
2246 
2247 	/*
2248 	 * We use an adjusted version of the dma_attr, to account
2249 	 * for device addressing limitations.
2250 	 * sata_adjust_dma_attr() will handle sdinfo == NULL which may
2251 	 * happen when a device is not yet configured.
2252 	 */
2253 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2254 	    sata_device.satadev_addr.cport)));
2255 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2256 	    &spx->txlt_sata_pkt->satapkt_device);
2257 	/* NULL sdinfo may be passsed to sata_adjust_dma_attr() */
2258 	sata_adjust_dma_attr(sdinfo,
2259 	    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
2260 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2261 	    sata_device.satadev_addr.cport)));
2262 	/*
2263 	 * Allocate necessary DMA resources for the packet's data buffer
2264 	 * NOTE:
2265 	 * In case of read/write commands, DMA resource allocation here is
2266 	 * based on the premise that the transfer length specified in
2267 	 * the read/write scsi cdb will match exactly DMA resources -
2268 	 * returning correct packet residue is crucial.
2269 	 */
2270 	if ((rval = sata_dma_buf_setup(spx, flags, callback, arg,
2271 	    &cur_dma_attr)) != DDI_SUCCESS) {
2272 		/*
2273 		 * If a DMA allocation request fails with
2274 		 * DDI_DMA_NOMAPPING, indicate the error by calling
2275 		 * bioerror(9F) with bp and an error code of EFAULT.
2276 		 * If a DMA allocation request fails with
2277 		 * DDI_DMA_TOOBIG, indicate the error by calling
2278 		 * bioerror(9F) with bp and an error code of EINVAL.
2279 		 * For DDI_DMA_NORESOURCES, we may have some of them allocated.
2280 		 * Request may be repeated later - there is no real error.
2281 		 */
2282 		switch (rval) {
2283 		case DDI_DMA_NORESOURCES:
2284 			bioerror(bp, 0);
2285 			break;
2286 		case DDI_DMA_NOMAPPING:
2287 		case DDI_DMA_BADATTR:
2288 			bioerror(bp, EFAULT);
2289 			break;
2290 		case DDI_DMA_TOOBIG:
2291 		default:
2292 			bioerror(bp, EINVAL);
2293 			break;
2294 		}
2295 		if (new_pkt == TRUE) {
2296 			/*
2297 			 * Since this is a new packet, we can clean-up
2298 			 * everything
2299 			 */
2300 			sata_scsi_destroy_pkt(ap, pkt);
2301 		} else {
2302 			/*
2303 			 * This is a re-used packet. It will be target driver's
2304 			 * responsibility to eventually destroy it (which
2305 			 * will free allocated resources).
2306 			 * Here, we just "complete" the request, leaving
2307 			 * allocated resources intact, so the request may
2308 			 * be retried.
2309 			 */
2310 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2311 			sata_pkt_free(spx);
2312 		}
2313 		return (NULL);
2314 	}
2315 	/* Set number of bytes that are not yet accounted for */
2316 	pkt->pkt_resid = spx->txlt_total_residue;
2317 	ASSERT(pkt->pkt_resid >= 0);
2318 
2319 	return (pkt);
2320 }
2321 
2322 /*
2323  * Implementation of scsi tran_start.
2324  * Translate scsi cmd into sata operation and return status.
2325  * ATAPI CDBs are passed to ATAPI devices - the device determines what commands
2326  * are supported.
2327  * For SATA hard disks, supported scsi commands:
2328  * SCMD_INQUIRY
2329  * SCMD_TEST_UNIT_READY
2330  * SCMD_START_STOP
2331  * SCMD_READ_CAPACITY
2332  * SCMD_REQUEST_SENSE
2333  * SCMD_LOG_SENSE_G1
2334  * SCMD_LOG_SELECT_G1
2335  * SCMD_MODE_SENSE	(specific pages)
2336  * SCMD_MODE_SENSE_G1	(specific pages)
2337  * SCMD_MODE_SELECT	(specific pages)
2338  * SCMD_MODE_SELECT_G1	(specific pages)
2339  * SCMD_SYNCHRONIZE_CACHE
2340  * SCMD_SYNCHRONIZE_CACHE_G1
2341  * SCMD_READ
2342  * SCMD_READ_G1
2343  * SCMD_READ_G4
2344  * SCMD_READ_G5
2345  * SCMD_WRITE
2346  * SCMD_WRITE_BUFFER
2347  * SCMD_WRITE_G1
2348  * SCMD_WRITE_G4
2349  * SCMD_WRITE_G5
2350  * SCMD_SEEK		(noop)
2351  * SCMD_SDIAG
2352  *
2353  * All other commands are rejected as unsupported.
2354  *
2355  * Returns:
2356  * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver
2357  * for execution. TRAN_ACCEPT may be returned also if device was removed but
2358  * a callback could be scheduled.
2359  * TRAN_BADPKT if cmd was directed to invalid address.
2360  * TRAN_FATAL_ERROR is command was rejected due to hardware error, including
2361  * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device
2362  * was removed and there was no callback specified in scsi pkt.
2363  * TRAN_BUSY if command could not be executed becasue HBA driver or SATA
2364  * framework was busy performing some other operation(s).
2365  *
2366  */
2367 static int
2368 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
2369 {
2370 	sata_hba_inst_t *sata_hba_inst =
2371 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2372 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2373 	sata_device_t *sdevice = &spx->txlt_sata_pkt->satapkt_device;
2374 	sata_drive_info_t *sdinfo;
2375 	struct buf *bp;
2376 	uint8_t cport, pmport;
2377 	boolean_t dev_gone = B_FALSE;
2378 	int rval;
2379 
2380 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2381 	    "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]);
2382 
2383 	ASSERT(spx != NULL &&
2384 	    spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL);
2385 
2386 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
2387 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2388 
2389 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2390 
2391 	if (sdevice->satadev_addr.qual == SATA_ADDR_DCPORT) {
2392 		sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2393 		if (sdinfo == NULL ||
2394 		    SATA_CPORT_INFO(sata_hba_inst, cport)->
2395 		    cport_tgtnode_clean == B_FALSE ||
2396 		    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2397 			dev_gone = B_TRUE;
2398 		}
2399 	} else if (sdevice->satadev_addr.qual == SATA_ADDR_DPMPORT) {
2400 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
2401 		    SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst,
2402 		    cport) == NULL) {
2403 			dev_gone = B_TRUE;
2404 		} else if (SATA_PMPORT_INFO(sata_hba_inst, cport,
2405 		    pmport) == NULL) {
2406 			dev_gone = B_TRUE;
2407 		} else {
2408 			mutex_enter(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2409 			    cport, pmport)));
2410 			sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2411 			if (sdinfo == NULL ||
2412 			    SATA_PMPORT_INFO(sata_hba_inst, cport, pmport)->
2413 			    pmport_tgtnode_clean == B_FALSE ||
2414 			    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2415 				dev_gone = B_TRUE;
2416 			}
2417 			mutex_exit(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2418 			    cport, pmport)));
2419 		}
2420 	}
2421 
2422 	if (dev_gone == B_TRUE) {
2423 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2424 		pkt->pkt_reason = CMD_DEV_GONE;
2425 		/*
2426 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2427 		 * only in callback function (for normal requests) and
2428 		 * in the dump code path.
2429 		 * So, if the callback is available, we need to do
2430 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2431 		 */
2432 		if (pkt->pkt_comp != NULL) {
2433 			/* scsi callback required */
2434 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2435 			    (task_func_t *)pkt->pkt_comp,
2436 			    (void *)pkt, TQ_SLEEP) == NULL)
2437 				/* Scheduling the callback failed */
2438 				return (TRAN_BUSY);
2439 			return (TRAN_ACCEPT);
2440 		}
2441 		/* No callback available */
2442 		return (TRAN_FATAL_ERROR);
2443 	}
2444 
2445 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
2446 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2447 		rval = sata_txlt_atapi(spx);
2448 		SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2449 		    "sata_scsi_start atapi: rval %d\n", rval);
2450 		return (rval);
2451 	}
2452 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2453 
2454 	/*
2455 	 * Checking for power state, if it was on
2456 	 * STOPPED state, then the drive is not capable
2457 	 * of processing media access command.  And
2458 	 * TEST_UNIT_READY, REQUEST_SENSE has special handling
2459 	 * in the function for different power state.
2460 	 */
2461 	if (((sdinfo->satadrv_power_level == SATA_POWER_STANDBY) ||
2462 	    (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)) &&
2463 	    (SATA_IS_MEDIUM_ACCESS_CMD(pkt->pkt_cdbp[0]))) {
2464 		return (sata_txlt_check_condition(spx, KEY_NOT_READY,
2465 		    SD_SCSI_ASC_LU_NOT_READY));
2466 	}
2467 
2468 	/* ATA Disk commands processing starts here */
2469 
2470 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2471 
2472 	switch (pkt->pkt_cdbp[0]) {
2473 
2474 	case SCMD_INQUIRY:
2475 		/* Mapped to identify device */
2476 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2477 			bp_mapin(bp);
2478 		rval = sata_txlt_inquiry(spx);
2479 		break;
2480 
2481 	case SCMD_TEST_UNIT_READY:
2482 		/*
2483 		 * SAT "SATA to ATA Translation" doc specifies translation
2484 		 * to ATA CHECK POWER MODE.
2485 		 */
2486 		rval = sata_txlt_test_unit_ready(spx);
2487 		break;
2488 
2489 	case SCMD_START_STOP:
2490 		/* Mapping depends on the command */
2491 		rval = sata_txlt_start_stop_unit(spx);
2492 		break;
2493 
2494 	case SCMD_READ_CAPACITY:
2495 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2496 			bp_mapin(bp);
2497 		rval = sata_txlt_read_capacity(spx);
2498 		break;
2499 
2500 	case SCMD_REQUEST_SENSE:
2501 		/*
2502 		 * Always No Sense, since we force ARQ
2503 		 */
2504 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2505 			bp_mapin(bp);
2506 		rval = sata_txlt_request_sense(spx);
2507 		break;
2508 
2509 	case SCMD_LOG_SENSE_G1:
2510 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2511 			bp_mapin(bp);
2512 		rval = sata_txlt_log_sense(spx);
2513 		break;
2514 
2515 	case SCMD_LOG_SELECT_G1:
2516 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2517 			bp_mapin(bp);
2518 		rval = sata_txlt_log_select(spx);
2519 		break;
2520 
2521 	case SCMD_MODE_SENSE:
2522 	case SCMD_MODE_SENSE_G1:
2523 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2524 			bp_mapin(bp);
2525 		rval = sata_txlt_mode_sense(spx);
2526 		break;
2527 
2528 
2529 	case SCMD_MODE_SELECT:
2530 	case SCMD_MODE_SELECT_G1:
2531 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2532 			bp_mapin(bp);
2533 		rval = sata_txlt_mode_select(spx);
2534 		break;
2535 
2536 	case SCMD_SYNCHRONIZE_CACHE:
2537 	case SCMD_SYNCHRONIZE_CACHE_G1:
2538 		rval = sata_txlt_synchronize_cache(spx);
2539 		break;
2540 
2541 	case SCMD_READ:
2542 	case SCMD_READ_G1:
2543 	case SCMD_READ_G4:
2544 	case SCMD_READ_G5:
2545 		rval = sata_txlt_read(spx);
2546 		break;
2547 	case SCMD_WRITE_BUFFER:
2548 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2549 			bp_mapin(bp);
2550 		rval = sata_txlt_write_buffer(spx);
2551 		break;
2552 
2553 	case SCMD_WRITE:
2554 	case SCMD_WRITE_G1:
2555 	case SCMD_WRITE_G4:
2556 	case SCMD_WRITE_G5:
2557 		rval = sata_txlt_write(spx);
2558 		break;
2559 
2560 	case SCMD_SEEK:
2561 		rval = sata_txlt_nodata_cmd_immediate(spx);
2562 		break;
2563 
2564 		/* Other cases will be filed later */
2565 		/* postponed until phase 2 of the development */
2566 	default:
2567 		rval = sata_txlt_invalid_command(spx);
2568 		break;
2569 	}
2570 
2571 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2572 	    "sata_scsi_start: rval %d\n", rval);
2573 
2574 	return (rval);
2575 }
2576 
2577 /*
2578  * Implementation of scsi tran_abort.
2579  * Abort specific pkt or all packets.
2580  *
2581  * Returns 1 if one or more packets were aborted, returns 0 otherwise
2582  *
2583  * May be called from an interrupt level.
2584  */
2585 static int
2586 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt)
2587 {
2588 	sata_hba_inst_t *sata_hba_inst =
2589 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2590 	sata_device_t	sata_device;
2591 	sata_pkt_t	*sata_pkt;
2592 
2593 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2594 	    "sata_scsi_abort: %s at target: 0x%x\n",
2595 	    scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target);
2596 
2597 	/* Validate address */
2598 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0)
2599 		/* Invalid address */
2600 		return (0);
2601 
2602 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2603 	    sata_device.satadev_addr.cport)));
2604 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2605 		/* invalid address */
2606 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2607 		    sata_device.satadev_addr.cport)));
2608 		return (0);
2609 	}
2610 	if (scsi_pkt == NULL) {
2611 		/*
2612 		 * Abort all packets.
2613 		 * Although we do not have specific packet, we still need
2614 		 * dummy packet structure to pass device address to HBA.
2615 		 * Allocate one, without sleeping. Fail if pkt cannot be
2616 		 * allocated.
2617 		 */
2618 		sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP);
2619 		if (sata_pkt == NULL) {
2620 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2621 			    sata_device.satadev_addr.cport)));
2622 			SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: "
2623 			    "could not allocate sata_pkt"));
2624 			return (0);
2625 		}
2626 		sata_pkt->satapkt_rev = SATA_PKT_REV;
2627 		sata_pkt->satapkt_device = sata_device;
2628 		sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
2629 	} else {
2630 		if (scsi_pkt->pkt_ha_private == NULL) {
2631 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2632 			    sata_device.satadev_addr.cport)));
2633 			return (0); /* Bad scsi pkt */
2634 		}
2635 		/* extract pointer to sata pkt */
2636 		sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)->
2637 		    txlt_sata_pkt;
2638 	}
2639 
2640 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2641 	    sata_device.satadev_addr.cport)));
2642 	/* Send abort request to HBA */
2643 	if ((*SATA_ABORT_FUNC(sata_hba_inst))
2644 	    (SATA_DIP(sata_hba_inst), sata_pkt,
2645 	    scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) ==
2646 	    SATA_SUCCESS) {
2647 		if (scsi_pkt == NULL)
2648 			kmem_free(sata_pkt, sizeof (sata_pkt_t));
2649 		/* Success */
2650 		return (1);
2651 	}
2652 	/* Else, something did not go right */
2653 	if (scsi_pkt == NULL)
2654 		kmem_free(sata_pkt, sizeof (sata_pkt_t));
2655 	/* Failure */
2656 	return (0);
2657 }
2658 
2659 
2660 /*
2661  * Implementation of scsi tran_reset.
2662  * RESET_ALL request is translated into port reset.
2663  * RESET_TARGET requests is translated into a device reset,
2664  * RESET_LUN request is accepted only for LUN 0 and translated into
2665  * device reset.
2666  * The target reset should cause all HBA active and queued packets to
2667  * be terminated and returned with pkt reason SATA_PKT_RESET prior to
2668  * the return. HBA should report reset event for the device.
2669  *
2670  * Returns 1 upon success, 0 upon failure.
2671  */
2672 static int
2673 sata_scsi_reset(struct scsi_address *ap, int level)
2674 {
2675 	sata_hba_inst_t	*sata_hba_inst =
2676 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2677 	sata_device_t	sata_device;
2678 	int		val;
2679 
2680 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2681 	    "sata_scsi_reset: level %d target: 0x%x\n",
2682 	    level, ap->a_target);
2683 
2684 	/* Validate address */
2685 	val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device);
2686 	if (val == -1)
2687 		/* Invalid address */
2688 		return (0);
2689 
2690 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2691 	    sata_device.satadev_addr.cport)));
2692 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2693 		/* invalid address */
2694 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2695 		    sata_device.satadev_addr.cport)));
2696 		return (0);
2697 	}
2698 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2699 	    sata_device.satadev_addr.cport)));
2700 	if (level == RESET_ALL) {
2701 		/* port reset */
2702 		if (sata_device.satadev_addr.qual == SATA_ADDR_DCPORT)
2703 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2704 		else
2705 			sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
2706 
2707 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2708 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2709 			return (1);
2710 		else
2711 			return (0);
2712 
2713 	} else if (val == 0 &&
2714 	    (level == RESET_TARGET || level == RESET_LUN)) {
2715 		/* reset device (device attached) */
2716 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2717 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2718 			return (1);
2719 		else
2720 			return (0);
2721 	}
2722 	return (0);
2723 }
2724 
2725 
2726 /*
2727  * Implementation of scsi tran_getcap (get transport/device capabilities).
2728  * Supported capabilities for SATA hard disks:
2729  * auto-rqsense		(always supported)
2730  * tagged-qing		(supported if HBA supports it)
2731  * untagged-qing	(could be supported if disk supports it, but because
2732  *			 caching behavior allowing untagged queuing actually
2733  *			 results in reduced performance.  sd tries to throttle
2734  *			 back to only 3 outstanding commands, which may
2735  *			 work for real SCSI disks, but with read ahead
2736  *			 caching, having more than 1 outstanding command
2737  *			 results in cache thrashing.)
2738  * sector_size
2739  * dma_max
2740  * interconnect-type	(INTERCONNECT_SATA)
2741  *
2742  * Supported capabilities for ATAPI CD/DVD devices:
2743  * auto-rqsense		(always supported)
2744  * sector_size
2745  * dma_max
2746  * max-cdb-length
2747  * interconnect-type	(INTERCONNECT_SATA)
2748  *
2749  * Supported capabilities for ATAPI TAPE devices:
2750  * auto-rqsense		(always supported)
2751  * dma_max
2752  * max-cdb-length
2753  *
2754  * Supported capabilities for SATA ATAPI hard disks:
2755  * auto-rqsense		(always supported)
2756  * interconnect-type	(INTERCONNECT_SATA)
2757  * max-cdb-length
2758  *
2759  * Request for other capabilities is rejected as unsupported.
2760  *
2761  * Returns supported capability value, or -1 if capability is unsuppported or
2762  * the address is invalid - no device.
2763  */
2764 
2765 static int
2766 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
2767 {
2768 
2769 	sata_hba_inst_t 	*sata_hba_inst =
2770 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2771 	sata_device_t		sata_device;
2772 	sata_drive_info_t	*sdinfo;
2773 	ddi_dma_attr_t		adj_dma_attr;
2774 	int 			rval;
2775 
2776 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2777 	    "sata_scsi_getcap: target: 0x%x, cap: %s\n",
2778 	    ap->a_target, cap);
2779 
2780 	/*
2781 	 * We want to process the capabilities on per port granularity.
2782 	 * So, we are specifically restricting ourselves to whom != 0
2783 	 * to exclude the controller wide handling.
2784 	 */
2785 	if (cap == NULL || whom == 0)
2786 		return (-1);
2787 
2788 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2789 		/* Invalid address */
2790 		return (-1);
2791 	}
2792 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2793 	    sata_device.satadev_addr.cport)));
2794 	if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) ==
2795 	    NULL) {
2796 		/* invalid address */
2797 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2798 		    sata_device.satadev_addr.cport)));
2799 		return (-1);
2800 	}
2801 
2802 	switch (scsi_hba_lookup_capstr(cap)) {
2803 	case SCSI_CAP_ARQ:
2804 		rval = 1;		/* ARQ supported, turned on */
2805 		break;
2806 
2807 	case SCSI_CAP_SECTOR_SIZE:
2808 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK)
2809 			rval = SATA_DISK_SECTOR_SIZE;	/* fixed size */
2810 		else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD)
2811 			rval = SATA_ATAPI_SECTOR_SIZE;
2812 		else rval = -1;
2813 		break;
2814 
2815 	/*
2816 	 * untagged queuing cause a performance inversion because of
2817 	 * the way sd operates.  Because of this reason we do not
2818 	 * use it when available.
2819 	 */
2820 	case SCSI_CAP_UNTAGGED_QING:
2821 		if (sdinfo->satadrv_features_enabled &
2822 		    SATA_DEV_F_E_UNTAGGED_QING)
2823 			rval = 1;	/* Untagged queuing available */
2824 		else
2825 			rval = -1;	/* Untagged queuing not available */
2826 		break;
2827 
2828 	case SCSI_CAP_TAGGED_QING:
2829 		if ((sdinfo->satadrv_features_enabled &
2830 		    SATA_DEV_F_E_TAGGED_QING) &&
2831 		    (sdinfo->satadrv_max_queue_depth > 1))
2832 			rval = 1;	/* Tagged queuing available */
2833 		else
2834 			rval = -1;	/* Tagged queuing not available */
2835 		break;
2836 
2837 	case SCSI_CAP_DMA_MAX:
2838 		sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst),
2839 		    &adj_dma_attr);
2840 		rval = (int)adj_dma_attr.dma_attr_maxxfer;
2841 		/* We rely on the fact that dma_attr_maxxfer < 0x80000000 */
2842 		break;
2843 
2844 	case SCSI_CAP_INTERCONNECT_TYPE:
2845 		rval = INTERCONNECT_SATA;	/* SATA interconnect type */
2846 		break;
2847 
2848 	case SCSI_CAP_CDB_LEN:
2849 		if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI)
2850 			rval = sdinfo->satadrv_atapi_cdb_len;
2851 		else
2852 			rval = -1;
2853 		break;
2854 
2855 	default:
2856 		rval = -1;
2857 		break;
2858 	}
2859 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2860 	    sata_device.satadev_addr.cport)));
2861 	return (rval);
2862 }
2863 
2864 /*
2865  * Implementation of scsi tran_setcap
2866  *
2867  * Only SCSI_CAP_UNTAGGED_QING and  SCSI_CAP_TAGGED_QING are changeable.
2868  *
2869  */
2870 static int
2871 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
2872 {
2873 	sata_hba_inst_t	*sata_hba_inst =
2874 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2875 	sata_device_t	sata_device;
2876 	sata_drive_info_t	*sdinfo;
2877 	int		rval;
2878 
2879 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2880 	    "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap);
2881 
2882 	/*
2883 	 * We want to process the capabilities on per port granularity.
2884 	 * So, we are specifically restricting ourselves to whom != 0
2885 	 * to exclude the controller wide handling.
2886 	 */
2887 	if (cap == NULL || whom == 0) {
2888 		return (-1);
2889 	}
2890 
2891 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2892 		/* Invalid address */
2893 		return (-1);
2894 	}
2895 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2896 	    sata_device.satadev_addr.cport)));
2897 	if ((sdinfo = sata_get_device_info(sata_hba_inst,
2898 	    &sata_device)) == NULL) {
2899 		/* invalid address */
2900 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2901 		    sata_device.satadev_addr.cport)));
2902 		return (-1);
2903 	}
2904 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2905 	    sata_device.satadev_addr.cport)));
2906 
2907 	switch (scsi_hba_lookup_capstr(cap)) {
2908 	case SCSI_CAP_ARQ:
2909 	case SCSI_CAP_SECTOR_SIZE:
2910 	case SCSI_CAP_DMA_MAX:
2911 	case SCSI_CAP_INTERCONNECT_TYPE:
2912 		rval = 0;
2913 		break;
2914 	case SCSI_CAP_UNTAGGED_QING:
2915 		if (SATA_QDEPTH(sata_hba_inst) > 1) {
2916 			rval = 1;
2917 			if (value == 1) {
2918 				sdinfo->satadrv_features_enabled |=
2919 				    SATA_DEV_F_E_UNTAGGED_QING;
2920 			} else if (value == 0) {
2921 				sdinfo->satadrv_features_enabled &=
2922 				    ~SATA_DEV_F_E_UNTAGGED_QING;
2923 			} else {
2924 				rval = -1;
2925 			}
2926 		} else {
2927 			rval = 0;
2928 		}
2929 		break;
2930 	case SCSI_CAP_TAGGED_QING:
2931 		/* This can TCQ or NCQ */
2932 		if (sata_func_enable & SATA_ENABLE_QUEUING &&
2933 		    ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ &&
2934 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) ||
2935 		    (sata_func_enable & SATA_ENABLE_NCQ &&
2936 		    sdinfo->satadrv_features_support & SATA_DEV_F_NCQ &&
2937 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) &&
2938 		    (sdinfo->satadrv_max_queue_depth > 1)) {
2939 			rval = 1;
2940 			if (value == 1) {
2941 				sdinfo->satadrv_features_enabled |=
2942 				    SATA_DEV_F_E_TAGGED_QING;
2943 			} else if (value == 0) {
2944 				sdinfo->satadrv_features_enabled &=
2945 				    ~SATA_DEV_F_E_TAGGED_QING;
2946 			} else {
2947 				rval = -1;
2948 			}
2949 		} else {
2950 			rval = 0;
2951 		}
2952 		break;
2953 	default:
2954 		rval = -1;
2955 		break;
2956 	}
2957 	return (rval);
2958 }
2959 
2960 /*
2961  * Implementations of scsi tran_destroy_pkt.
2962  * Free resources allocated by sata_scsi_init_pkt()
2963  */
2964 static void
2965 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2966 {
2967 	sata_pkt_txlate_t *spx;
2968 
2969 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2970 
2971 	sata_common_free_dma_rsrcs(spx);
2972 
2973 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2974 	sata_pkt_free(spx);
2975 
2976 	scsi_hba_pkt_free(ap, pkt);
2977 }
2978 
2979 /*
2980  * Implementation of scsi tran_dmafree.
2981  * Free DMA resources allocated by sata_scsi_init_pkt()
2982  */
2983 
2984 static void
2985 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
2986 {
2987 #ifndef __lock_lint
2988 	_NOTE(ARGUNUSED(ap))
2989 #endif
2990 	sata_pkt_txlate_t *spx;
2991 
2992 	ASSERT(pkt != NULL);
2993 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2994 
2995 	sata_common_free_dma_rsrcs(spx);
2996 }
2997 
2998 /*
2999  * Implementation of scsi tran_sync_pkt.
3000  *
3001  * The assumption below is that pkt is unique - there is no need to check ap
3002  *
3003  * Synchronize DMA buffer and, if the intermediate buffer is used, copy data
3004  * into/from the real buffer.
3005  */
3006 static void
3007 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3008 {
3009 #ifndef __lock_lint
3010 	_NOTE(ARGUNUSED(ap))
3011 #endif
3012 	int rval;
3013 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3014 	struct buf *bp;
3015 	int direction;
3016 
3017 	ASSERT(spx != NULL);
3018 	if (spx->txlt_buf_dma_handle != NULL) {
3019 		direction = spx->txlt_sata_pkt->
3020 		    satapkt_cmd.satacmd_flags.sata_data_direction;
3021 		if (spx->txlt_sata_pkt != NULL &&
3022 		    direction != SATA_DIR_NODATA_XFER) {
3023 			if (spx->txlt_tmp_buf != NULL) {
3024 				/* Intermediate DMA buffer used */
3025 				bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3026 
3027 				if (direction & SATA_DIR_WRITE) {
3028 					bcopy(bp->b_un.b_addr,
3029 					    spx->txlt_tmp_buf, bp->b_bcount);
3030 				}
3031 			}
3032 			/* Sync the buffer for device or for CPU */
3033 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle,   0, 0,
3034 			    (direction & SATA_DIR_WRITE) ?
3035 			    DDI_DMA_SYNC_FORDEV :  DDI_DMA_SYNC_FORCPU);
3036 			ASSERT(rval == DDI_SUCCESS);
3037 			if (spx->txlt_tmp_buf != NULL &&
3038 			    !(direction & SATA_DIR_WRITE)) {
3039 				/* Intermediate DMA buffer used for read */
3040 				bcopy(spx->txlt_tmp_buf,
3041 				    bp->b_un.b_addr, bp->b_bcount);
3042 			}
3043 
3044 		}
3045 	}
3046 }
3047 
3048 
3049 
3050 /* *******************  SATA - SCSI Translation functions **************** */
3051 /*
3052  * SCSI to SATA pkt and command translation and SATA to SCSI status/error
3053  * translation.
3054  */
3055 
3056 /*
3057  * Checks if a device exists and can be access and translates common
3058  * scsi_pkt data to sata_pkt data.
3059  *
3060  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and
3061  * sata_pkt was set-up.
3062  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not
3063  * exist and pkt_comp callback was scheduled.
3064  * Returns other TRAN_XXXXX values when error occured and command should be
3065  * rejected with the returned TRAN_XXXXX value.
3066  *
3067  * This function should be called with port mutex held.
3068  */
3069 static int
3070 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason)
3071 {
3072 	sata_drive_info_t *sdinfo;
3073 	sata_device_t sata_device;
3074 	const struct sata_cmd_flags sata_initial_cmd_flags = {
3075 		SATA_DIR_NODATA_XFER,
3076 		/* all other values to 0/FALSE */
3077 	};
3078 	/*
3079 	 * Pkt_reason has to be set if the pkt_comp callback is invoked,
3080 	 * and that implies TRAN_ACCEPT return value. Any other returned value
3081 	 * indicates that the scsi packet was not accepted (the reason will not
3082 	 * be checked by the scsi target driver).
3083 	 * To make debugging easier, we set pkt_reason to know value here.
3084 	 * It may be changed later when different completion reason is
3085 	 * determined.
3086 	 */
3087 	spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
3088 	*reason = CMD_TRAN_ERR;
3089 
3090 	/* Validate address */
3091 	switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst,
3092 	    &spx->txlt_scsi_pkt->pkt_address, &sata_device)) {
3093 
3094 	case -1:
3095 		/* Invalid address or invalid device type */
3096 		return (TRAN_BADPKT);
3097 	case 2:
3098 		/*
3099 		 * Valid address but device type is unknown - Chack if it is
3100 		 * in the reset state and therefore in an indeterminate state.
3101 		 */
3102 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3103 		    &spx->txlt_sata_pkt->satapkt_device);
3104 		if (sdinfo != NULL && (sdinfo->satadrv_event_flags &
3105 		    (SATA_EVNT_DEVICE_RESET |
3106 		    SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3107 			if (!ddi_in_panic()) {
3108 				spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3109 				*reason = CMD_INCOMPLETE;
3110 				SATADBG1(SATA_DBG_SCSI_IF,
3111 				    spx->txlt_sata_hba_inst,
3112 				    "sata_scsi_start: rejecting command "
3113 				    "because of device reset state\n", NULL);
3114 				return (TRAN_BUSY);
3115 			}
3116 		}
3117 		/* FALLTHROUGH */
3118 	case 1:
3119 		/* valid address but no valid device - it has disappeared */
3120 		spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE;
3121 		*reason = CMD_DEV_GONE;
3122 		/*
3123 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
3124 		 * only in callback function (for normal requests) and
3125 		 * in the dump code path.
3126 		 * So, if the callback is available, we need to do
3127 		 * the callback rather than returning TRAN_FATAL_ERROR here.
3128 		 */
3129 		if (spx->txlt_scsi_pkt->pkt_comp != NULL) {
3130 			/* scsi callback required */
3131 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3132 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3133 			    (void *)spx->txlt_scsi_pkt,
3134 			    TQ_SLEEP) == NULL)
3135 				/* Scheduling the callback failed */
3136 				return (TRAN_BUSY);
3137 
3138 			return (TRAN_ACCEPT);
3139 		}
3140 		return (TRAN_FATAL_ERROR);
3141 	default:
3142 		/* all OK; pkt reason will be overwritten later */
3143 		break;
3144 	}
3145 	/*
3146 	 * If in an interrupt context, reject packet if it is to be
3147 	 * executed in polling mode
3148 	 */
3149 	if (servicing_interrupt() &&
3150 	    (spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
3151 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
3152 		    "sata_scsi_start: rejecting synchronous command because "
3153 		    "of interrupt context\n", NULL);
3154 		return (TRAN_BUSY);
3155 	}
3156 
3157 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3158 	    &spx->txlt_sata_pkt->satapkt_device);
3159 
3160 	/*
3161 	 * If device is in reset condition, reject the packet with
3162 	 * TRAN_BUSY, unless:
3163 	 * 1. system is panicking (dumping)
3164 	 * In such case only one thread is running and there is no way to
3165 	 * process reset.
3166 	 * 2. cfgadm operation is is progress (internal APCTL lock is set)
3167 	 * Some cfgadm operations involve drive commands, so reset condition
3168 	 * needs to be ignored for IOCTL operations.
3169 	 */
3170 	if ((sdinfo->satadrv_event_flags &
3171 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3172 
3173 		if (!ddi_in_panic() &&
3174 		    ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst,
3175 		    sata_device.satadev_addr.cport) &
3176 		    SATA_APCTL_LOCK_PORT_BUSY) == 0)) {
3177 			spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3178 			*reason = CMD_INCOMPLETE;
3179 			SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3180 			    "sata_scsi_start: rejecting command because "
3181 			    "of device reset state\n", NULL);
3182 			return (TRAN_BUSY);
3183 		}
3184 	}
3185 
3186 	/*
3187 	 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by
3188 	 * sata_scsi_pkt_init() because pkt init had to work also with
3189 	 * non-existing devices.
3190 	 * Now we know that the packet was set-up for a real device, so its
3191 	 * type is known.
3192 	 */
3193 	spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type;
3194 
3195 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags;
3196 	if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst,
3197 	    sata_device.satadev_addr.cport)->cport_event_flags &
3198 	    SATA_APCTL_LOCK_PORT_BUSY) != 0) {
3199 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3200 		    sata_ignore_dev_reset = B_TRUE;
3201 	}
3202 	/*
3203 	 * At this point the generic translation routine determined that the
3204 	 * scsi packet should be accepted. Packet completion reason may be
3205 	 * changed later when a different completion reason is determined.
3206 	 */
3207 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3208 	*reason = CMD_CMPLT;
3209 
3210 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
3211 		/* Synchronous execution */
3212 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH |
3213 		    SATA_OPMODE_POLLING;
3214 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3215 		    sata_ignore_dev_reset = ddi_in_panic();
3216 	} else {
3217 		/* Asynchronous execution */
3218 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH |
3219 		    SATA_OPMODE_INTERRUPTS;
3220 	}
3221 	/* Convert queuing information */
3222 	if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG)
3223 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag =
3224 		    B_TRUE;
3225 	else if (spx->txlt_scsi_pkt->pkt_flags &
3226 	    (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD))
3227 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag =
3228 		    B_TRUE;
3229 
3230 	/* Always limit pkt time */
3231 	if (spx->txlt_scsi_pkt->pkt_time == 0)
3232 		spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time;
3233 	else
3234 		/* Pass on scsi_pkt time */
3235 		spx->txlt_sata_pkt->satapkt_time =
3236 		    spx->txlt_scsi_pkt->pkt_time;
3237 
3238 	return (TRAN_ACCEPT);
3239 }
3240 
3241 
3242 /*
3243  * Translate ATA Identify Device data to SCSI Inquiry data.
3244  * This function may be called only for ATA devices.
3245  * This function should not be called for ATAPI devices - they
3246  * respond directly to SCSI Inquiry command.
3247  *
3248  * SATA Identify Device data has to be valid in sata_drive_info.
3249  * Buffer has to accomodate the inquiry length (36 bytes).
3250  *
3251  * This function should be called with a port mutex held.
3252  */
3253 static	void
3254 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst,
3255     sata_drive_info_t *sdinfo, uint8_t *buf)
3256 {
3257 
3258 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
3259 	struct sata_id *sid = &sdinfo->satadrv_id;
3260 
3261 	/* Start with a nice clean slate */
3262 	bzero((void *)inq, sizeof (struct scsi_inquiry));
3263 
3264 	/*
3265 	 * Rely on the dev_type for setting paripheral qualifier.
3266 	 * Assume that  DTYPE_RODIRECT applies to CD/DVD R/W devices.
3267 	 * It could be that DTYPE_OPTICAL could also qualify in the future.
3268 	 * ATAPI Inquiry may provide more data to the target driver.
3269 	 */
3270 	inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3271 	    DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */
3272 
3273 	/* CFA type device is not a removable media device */
3274 	inq->inq_rmb = ((sid->ai_config != SATA_CFA_TYPE) &&
3275 	    (sid->ai_config & SATA_REM_MEDIA)) ? 1 : 0;
3276 	inq->inq_qual = 0;	/* Device type qualifier (obsolete in SCSI3? */
3277 	inq->inq_iso = 0;	/* ISO version */
3278 	inq->inq_ecma = 0;	/* ECMA version */
3279 	inq->inq_ansi = 3;	/* ANSI version - SCSI 3 */
3280 	inq->inq_aenc = 0;	/* Async event notification cap. */
3281 	inq->inq_trmiop = 0;	/* Supports TERMINATE I/O PROC msg - NO */
3282 	inq->inq_normaca = 0;	/* setting NACA bit supported - NO */
3283 	inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */
3284 	inq->inq_len = 31;	/* Additional length */
3285 	inq->inq_dualp = 0;	/* dual port device - NO */
3286 	inq->inq_reladdr = 0;	/* Supports relative addressing - NO */
3287 	inq->inq_sync = 0;	/* Supports synchronous data xfers - NO */
3288 	inq->inq_linked = 0;	/* Supports linked commands - NO */
3289 				/*
3290 				 * Queuing support - controller has to
3291 				 * support some sort of command queuing.
3292 				 */
3293 	if (SATA_QDEPTH(sata_hba_inst) > 1)
3294 		inq->inq_cmdque = 1; /* Supports command queueing - YES */
3295 	else
3296 		inq->inq_cmdque = 0; /* Supports command queueing - NO */
3297 	inq->inq_sftre = 0;	/* Supports Soft Reset option - NO ??? */
3298 	inq->inq_wbus32 = 0;	/* Supports 32 bit wide data xfers - NO */
3299 	inq->inq_wbus16 = 0;	/* Supports 16 bit wide data xfers - NO */
3300 
3301 #ifdef	_LITTLE_ENDIAN
3302 	/* Swap text fields to match SCSI format */
3303 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3304 	swab(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3305 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3306 		swab(sid->ai_fw, inq->inq_revision, 4);	/* Revision level */
3307 	else
3308 		swab(&sid->ai_fw[4], inq->inq_revision, 4);	/* Rev. level */
3309 #else	/* _LITTLE_ENDIAN */
3310 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3311 	bcopy(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3312 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3313 		bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
3314 	else
3315 		bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
3316 #endif	/* _LITTLE_ENDIAN */
3317 }
3318 
3319 
3320 /*
3321  * Scsi response set up for invalid command (command not supported)
3322  *
3323  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3324  */
3325 static int
3326 sata_txlt_invalid_command(sata_pkt_txlate_t *spx)
3327 {
3328 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3329 	struct scsi_extended_sense *sense;
3330 
3331 	scsipkt->pkt_reason = CMD_CMPLT;
3332 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3333 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3334 
3335 	*scsipkt->pkt_scbp = STATUS_CHECK;
3336 
3337 	sense = sata_arq_sense(spx);
3338 	sense->es_key = KEY_ILLEGAL_REQUEST;
3339 	sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE;
3340 
3341 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3342 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3343 
3344 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3345 	    scsipkt->pkt_comp != NULL)
3346 		/* scsi callback required */
3347 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3348 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3349 		    (void *)spx->txlt_scsi_pkt,
3350 		    TQ_SLEEP) == NULL)
3351 			/* Scheduling the callback failed */
3352 			return (TRAN_BUSY);
3353 	return (TRAN_ACCEPT);
3354 }
3355 
3356 /*
3357  * Scsi response set up for check condition with special sense key
3358  * and additional sense code.
3359  *
3360  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3361  */
3362 static int
3363 sata_txlt_check_condition(sata_pkt_txlate_t *spx, uchar_t key, uchar_t code)
3364 {
3365 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3366 	int cport = SATA_TXLT_CPORT(spx);
3367 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3368 	struct scsi_extended_sense *sense;
3369 
3370 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3371 	scsipkt->pkt_reason = CMD_CMPLT;
3372 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3373 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3374 
3375 	*scsipkt->pkt_scbp = STATUS_CHECK;
3376 
3377 	sense = sata_arq_sense(spx);
3378 	sense->es_key = key;
3379 	sense->es_add_code = code;
3380 
3381 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3382 
3383 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3384 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3385 
3386 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
3387 		/* scsi callback required */
3388 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3389 		    (task_func_t *)scsi_hba_pkt_comp,
3390 		    (void *)spx->txlt_scsi_pkt,
3391 		    TQ_SLEEP) == NULL)
3392 			/* Scheduling the callback failed */
3393 			return (TRAN_BUSY);
3394 	return (TRAN_ACCEPT);
3395 }
3396 
3397 /*
3398  * Scsi response setup for
3399  * emulated non-data command that requires no action/return data
3400  *
3401  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3402  */
3403 static	int
3404 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx)
3405 {
3406 	int rval;
3407 	int reason;
3408 
3409 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3410 
3411 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3412 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3413 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3414 		return (rval);
3415 	}
3416 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3417 
3418 	spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3419 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3420 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3421 	*(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD;
3422 
3423 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3424 	    "Scsi_pkt completion reason %x\n",
3425 	    spx->txlt_scsi_pkt->pkt_reason);
3426 
3427 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 &&
3428 	    spx->txlt_scsi_pkt->pkt_comp != NULL)
3429 		/* scsi callback required */
3430 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3431 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3432 		    (void *)spx->txlt_scsi_pkt,
3433 		    TQ_SLEEP) == NULL)
3434 			/* Scheduling the callback failed */
3435 			return (TRAN_BUSY);
3436 	return (TRAN_ACCEPT);
3437 }
3438 
3439 
3440 /*
3441  * SATA translate command: Inquiry / Identify Device
3442  * Use cached Identify Device data for now, rather than issuing actual
3443  * Device Identify cmd request. If device is detached and re-attached,
3444  * asynchronous event processing should fetch and refresh Identify Device
3445  * data.
3446  * Two VPD pages are supported now:
3447  * Vital Product Data page
3448  * Unit Serial Number page
3449  *
3450  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3451  */
3452 
3453 #define	EVPD			1	/* Extended Vital Product Data flag */
3454 #define	CMDDT			2	/* Command Support Data - Obsolete */
3455 #define	INQUIRY_SUP_VPD_PAGE	0	/* Supported VDP Pages Page COde */
3456 #define	INQUIRY_USN_PAGE	0x80	/* Unit Serial Number Page Code */
3457 #define	INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */
3458 
3459 static int
3460 sata_txlt_inquiry(sata_pkt_txlate_t *spx)
3461 {
3462 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3463 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3464 	sata_drive_info_t *sdinfo;
3465 	struct scsi_extended_sense *sense;
3466 	int count;
3467 	uint8_t *p;
3468 	int i, j;
3469 	uint8_t page_buf[0xff]; /* Max length */
3470 	int rval, reason;
3471 
3472 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3473 
3474 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3475 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3476 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3477 		return (rval);
3478 	}
3479 
3480 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3481 	    &spx->txlt_sata_pkt->satapkt_device);
3482 
3483 	ASSERT(sdinfo != NULL);
3484 
3485 	scsipkt->pkt_reason = CMD_CMPLT;
3486 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3487 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3488 
3489 	/* Reject not supported request */
3490 	if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */
3491 		*scsipkt->pkt_scbp = STATUS_CHECK;
3492 		sense = sata_arq_sense(spx);
3493 		sense->es_key = KEY_ILLEGAL_REQUEST;
3494 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3495 		goto done;
3496 	}
3497 
3498 	/* Valid Inquiry request */
3499 	*scsipkt->pkt_scbp = STATUS_GOOD;
3500 
3501 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3502 
3503 		/*
3504 		 * Because it is fully emulated command storing data
3505 		 * programatically in the specified buffer, release
3506 		 * preallocated DMA resources before storing data in the buffer,
3507 		 * so no unwanted DMA sync would take place.
3508 		 */
3509 		sata_scsi_dmafree(NULL, scsipkt);
3510 
3511 		if (!(scsipkt->pkt_cdbp[1] & EVPD)) {
3512 			/* Standard Inquiry Data request */
3513 			struct scsi_inquiry inq;
3514 			unsigned int bufsize;
3515 
3516 			sata_identdev_to_inquiry(spx->txlt_sata_hba_inst,
3517 			    sdinfo, (uint8_t *)&inq);
3518 			/* Copy no more than requested */
3519 			count = MIN(bp->b_bcount,
3520 			    sizeof (struct scsi_inquiry));
3521 			bufsize = scsipkt->pkt_cdbp[4];
3522 			bufsize |= scsipkt->pkt_cdbp[3] << 8;
3523 			count = MIN(count, bufsize);
3524 			bcopy(&inq, bp->b_un.b_addr, count);
3525 
3526 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
3527 			scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3528 			    bufsize - count : 0;
3529 		} else {
3530 			/*
3531 			 * peripheral_qualifier = 0;
3532 			 *
3533 			 * We are dealing only with HD and will be
3534 			 * dealing with CD/DVD devices soon
3535 			 */
3536 			uint8_t peripheral_device_type =
3537 			    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3538 			    DTYPE_DIRECT : DTYPE_RODIRECT;
3539 
3540 			switch ((uint_t)scsipkt->pkt_cdbp[2]) {
3541 			case INQUIRY_SUP_VPD_PAGE:
3542 				/*
3543 				 * Request for suported Vital Product Data
3544 				 * pages - assuming only 2 page codes
3545 				 * supported.
3546 				 */
3547 				page_buf[0] = peripheral_device_type;
3548 				page_buf[1] = INQUIRY_SUP_VPD_PAGE;
3549 				page_buf[2] = 0;
3550 				page_buf[3] = 2; /* page length */
3551 				page_buf[4] = INQUIRY_SUP_VPD_PAGE;
3552 				page_buf[5] = INQUIRY_USN_PAGE;
3553 				/* Copy no more than requested */
3554 				count = MIN(bp->b_bcount, 6);
3555 				bcopy(page_buf, bp->b_un.b_addr, count);
3556 				break;
3557 
3558 			case INQUIRY_USN_PAGE:
3559 				/*
3560 				 * Request for Unit Serial Number page.
3561 				 * Set-up the page.
3562 				 */
3563 				page_buf[0] = peripheral_device_type;
3564 				page_buf[1] = INQUIRY_USN_PAGE;
3565 				page_buf[2] = 0;
3566 				/* remaining page length */
3567 				page_buf[3] = SATA_ID_SERIAL_LEN;
3568 
3569 				/*
3570 				 * Copy serial number from Identify Device data
3571 				 * words into the inquiry page and swap bytes
3572 				 * when necessary.
3573 				 */
3574 				p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser);
3575 #ifdef	_LITTLE_ENDIAN
3576 				swab(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3577 #else
3578 				bcopy(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3579 #endif
3580 				/*
3581 				 * Least significant character of the serial
3582 				 * number shall appear as the last byte,
3583 				 * according to SBC-3 spec.
3584 				 * Count trailing spaces to determine the
3585 				 * necessary shift length.
3586 				 */
3587 				p = &page_buf[SATA_ID_SERIAL_LEN + 4 - 1];
3588 				for (j = 0; j < SATA_ID_SERIAL_LEN; j++) {
3589 					if (*(p - j) != '\0' &&
3590 					    *(p - j) != '\040')
3591 						break;
3592 				}
3593 
3594 				/*
3595 				 * Shift SN string right, so that the last
3596 				 * non-blank character would appear in last
3597 				 * byte of SN field in the page.
3598 				 * 'j' is the shift length.
3599 				 */
3600 				for (i = 0;
3601 				    i < (SATA_ID_SERIAL_LEN - j) && j != 0;
3602 				    i++, p--)
3603 					*p = *(p - j);
3604 
3605 				/*
3606 				 * Add leading spaces - same number as the
3607 				 * shift size
3608 				 */
3609 				for (; j > 0; j--)
3610 					page_buf[4 + j - 1] = '\040';
3611 
3612 				count = MIN(bp->b_bcount,
3613 				    SATA_ID_SERIAL_LEN + 4);
3614 				bcopy(page_buf, bp->b_un.b_addr, count);
3615 				break;
3616 
3617 			case INQUIRY_DEV_IDENTIFICATION_PAGE:
3618 				/*
3619 				 * We may want to implement this page, when
3620 				 * identifiers are common for SATA devices
3621 				 * But not now.
3622 				 */
3623 				/*FALLTHROUGH*/
3624 
3625 			default:
3626 				/* Request for unsupported VPD page */
3627 				*scsipkt->pkt_scbp = STATUS_CHECK;
3628 				sense = sata_arq_sense(spx);
3629 				sense->es_key = KEY_ILLEGAL_REQUEST;
3630 				sense->es_add_code =
3631 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3632 				goto done;
3633 			}
3634 		}
3635 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3636 		scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3637 		    scsipkt->pkt_cdbp[4] - count : 0;
3638 	}
3639 done:
3640 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3641 
3642 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3643 	    "Scsi_pkt completion reason %x\n",
3644 	    scsipkt->pkt_reason);
3645 
3646 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3647 	    scsipkt->pkt_comp != NULL) {
3648 		/* scsi callback required */
3649 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3650 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3651 		    TQ_SLEEP) == NULL)
3652 			/* Scheduling the callback failed */
3653 			return (TRAN_BUSY);
3654 	}
3655 	return (TRAN_ACCEPT);
3656 }
3657 
3658 /*
3659  * SATA translate command: Request Sense.
3660  *
3661  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3662  * At the moment this is an emulated command (ATA version for SATA hard disks).
3663  * May be translated into Check Power Mode command in the future.
3664  *
3665  * Note: There is a mismatch between already implemented Informational
3666  * Exception Mode Select page 0x1C and this function.
3667  * When MRIE bit is set in page 0x1C, Request Sense is supposed to return
3668  * NO SENSE and set additional sense code to the exception code - this is not
3669  * implemented here.
3670  */
3671 static int
3672 sata_txlt_request_sense(sata_pkt_txlate_t *spx)
3673 {
3674 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3675 	struct scsi_extended_sense sense;
3676 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3677 	sata_drive_info_t *sdinfo;
3678 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3679 	int rval, reason, power_state = 0;
3680 
3681 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3682 
3683 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3684 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3685 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3686 		return (rval);
3687 	}
3688 
3689 	scsipkt->pkt_reason = CMD_CMPLT;
3690 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3691 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3692 	*scsipkt->pkt_scbp = STATUS_GOOD;
3693 
3694 	/*
3695 	 * when CONTROL field's NACA bit == 1
3696 	 * return ILLEGAL_REQUEST
3697 	 */
3698 	if (scsipkt->pkt_cdbp[5] & CTL_BYTE_NACA_MASK) {
3699 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3700 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
3701 		    SD_SCSI_ASC_CMD_SEQUENCE_ERR));
3702 	}
3703 
3704 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3705 	    &spx->txlt_sata_pkt->satapkt_device);
3706 	ASSERT(sdinfo != NULL);
3707 
3708 	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
3709 
3710 	sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
3711 	scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
3712 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3713 	if (sata_hba_start(spx, &rval) != 0) {
3714 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3715 		return (rval);
3716 	} else {
3717 		if (scmd->satacmd_error_reg != 0) {
3718 			mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3719 			return (sata_txlt_check_condition(spx, KEY_NO_SENSE,
3720 			    SD_SCSI_ASC_NO_ADD_SENSE));
3721 		}
3722 	}
3723 
3724 	switch (scmd->satacmd_sec_count_lsb) {
3725 	case SATA_PWRMODE_STANDBY: /* device in standby mode */
3726 		if (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)
3727 			power_state = SATA_POWER_STOPPED;
3728 		else {
3729 			power_state = SATA_POWER_STANDBY;
3730 			sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
3731 		}
3732 		break;
3733 	case SATA_PWRMODE_IDLE: /* device in idle mode */
3734 		power_state = SATA_POWER_IDLE;
3735 		sdinfo->satadrv_power_level = SATA_POWER_IDLE;
3736 		break;
3737 	case SATA_PWRMODE_ACTIVE: /* device in active or idle mode */
3738 	default:		  /* 0x40, 0x41 active mode */
3739 		if (sdinfo->satadrv_power_level == SATA_POWER_IDLE)
3740 			power_state = SATA_POWER_IDLE;
3741 		else {
3742 			power_state = SATA_POWER_ACTIVE;
3743 			sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
3744 		}
3745 		break;
3746 	}
3747 
3748 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3749 
3750 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3751 		/*
3752 		 * Because it is fully emulated command storing data
3753 		 * programatically in the specified buffer, release
3754 		 * preallocated DMA resources before storing data in the buffer,
3755 		 * so no unwanted DMA sync would take place.
3756 		 */
3757 		int count = MIN(bp->b_bcount,
3758 		    sizeof (struct scsi_extended_sense));
3759 		sata_scsi_dmafree(NULL, scsipkt);
3760 		bzero(&sense, sizeof (struct scsi_extended_sense));
3761 		sense.es_valid = 0;	/* Valid LBA */
3762 		sense.es_class = 7;	/* Response code 0x70 - current err */
3763 		sense.es_key = KEY_NO_SENSE;
3764 		sense.es_add_len = 6;	/* Additional length */
3765 		/* Copy no more than requested */
3766 		bcopy(&sense, bp->b_un.b_addr, count);
3767 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3768 		scsipkt->pkt_resid = 0;
3769 		switch (power_state) {
3770 		case SATA_POWER_IDLE:
3771 		case SATA_POWER_STANDBY:
3772 			sense.es_add_code =
3773 			    SD_SCSI_ASC_LOW_POWER_CONDITION_ON;
3774 			break;
3775 		case SATA_POWER_STOPPED:
3776 			sense.es_add_code = SD_SCSI_ASC_NO_ADD_SENSE;
3777 			break;
3778 		case SATA_POWER_ACTIVE:
3779 		default:
3780 			break;
3781 		}
3782 	}
3783 
3784 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3785 	    "Scsi_pkt completion reason %x\n",
3786 	    scsipkt->pkt_reason);
3787 
3788 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
3789 		/* scsi callback required */
3790 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3791 		    (task_func_t *)scsi_hba_pkt_comp, (void *) scsipkt,
3792 		    TQ_SLEEP) == NULL)
3793 			/* Scheduling the callback failed */
3794 			return (TRAN_BUSY);
3795 	return (TRAN_ACCEPT);
3796 }
3797 
3798 /*
3799  * SATA translate command: Test Unit Ready
3800  * (ATA version for SATA hard disks).
3801  * It is translated into the Check Power Mode command.
3802  *
3803  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3804  */
3805 static int
3806 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx)
3807 {
3808 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3809 	struct scsi_extended_sense *sense;
3810 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3811 	sata_drive_info_t *sdinfo;
3812 	int power_state;
3813 	int rval, reason;
3814 
3815 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3816 
3817 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3818 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3819 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3820 		return (rval);
3821 	}
3822 
3823 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3824 	    &spx->txlt_sata_pkt->satapkt_device);
3825 	ASSERT(sdinfo != NULL);
3826 
3827 	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
3828 
3829 	/* send CHECK POWER MODE command */
3830 	sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
3831 	scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
3832 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3833 	if (sata_hba_start(spx, &rval) != 0) {
3834 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3835 		return (rval);
3836 	} else {
3837 		if (scmd->satacmd_error_reg != 0) {
3838 			mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3839 			return (sata_txlt_check_condition(spx, KEY_NOT_READY,
3840 			    SD_SCSI_ASC_LU_NOT_RESPONSE));
3841 		}
3842 	}
3843 
3844 	power_state = scmd->satacmd_sec_count_lsb;
3845 
3846 	/*
3847 	 * return NOT READY when device in STOPPED mode
3848 	 */
3849 	if (power_state == SATA_PWRMODE_STANDBY &&
3850 	    sdinfo->satadrv_power_level == SATA_POWER_STOPPED) {
3851 		*scsipkt->pkt_scbp = STATUS_CHECK;
3852 		sense = sata_arq_sense(spx);
3853 		sense->es_key = KEY_NOT_READY;
3854 		sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY;
3855 	} else {
3856 		/*
3857 		 * For other power mode, return GOOD status
3858 		 */
3859 		*scsipkt->pkt_scbp = STATUS_GOOD;
3860 	}
3861 
3862 	scsipkt->pkt_reason = CMD_CMPLT;
3863 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3864 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3865 
3866 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3867 
3868 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3869 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3870 
3871 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
3872 		/* scsi callback required */
3873 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3874 		    (task_func_t *)scsi_hba_pkt_comp, (void *) scsipkt,
3875 		    TQ_SLEEP) == NULL)
3876 			/* Scheduling the callback failed */
3877 			return (TRAN_BUSY);
3878 
3879 	return (TRAN_ACCEPT);
3880 }
3881 
3882 /*
3883  * SATA translate command: Start Stop Unit
3884  * Translation depends on a command:
3885  *
3886  * Power condition bits will be supported
3887  * and the power level should be maintained by SATL,
3888  * When SATL received a command, it will check the
3889  * power level firstly, and return the status according
3890  * to SAT2 v2.6 and SAT-2 Standby Modifications
3891  *
3892  * SPC-4/SBC-3      SATL    ATA power condition  SATL      SPC/SBC
3893  * -----------------------------------------------------------------------
3894  * SSU_PC1 Active   <==>     ATA  Active         <==>     SSU:start_bit =1
3895  * SSU_PC2 Idle     <==>     ATA  Idle           <==>     N/A
3896  * SSU_PC3 Standby  <==>     ATA  Standby        <==>     N/A
3897  * SSU_PC4 Stopped  <==>     ATA  Standby        <==>     SSU:start_bit = 0
3898  *
3899  *	Unload Media / NOT SUPPORTED YET
3900  *	Load Media / NOT SUPPROTED YET
3901  *	Immediate bit / NOT SUPPORTED YET (deferred error)
3902  *
3903  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
3904  * appropriate values in scsi_pkt fields.
3905  */
3906 static int
3907 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx)
3908 {
3909 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3910 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3911 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3912 	int cport = SATA_TXLT_CPORT(spx);
3913 	int rval, reason;
3914 	sata_drive_info_t *sdinfo;
3915 	sata_id_t *sata_id;
3916 
3917 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3918 	    "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1);
3919 
3920 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3921 
3922 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3923 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3924 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3925 		return (rval);
3926 	}
3927 
3928 	if (scsipkt->pkt_cdbp[1] & START_STOP_IMMED_MASK) {
3929 		/* IMMED bit - not supported */
3930 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3931 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
3932 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
3933 	}
3934 
3935 	spx->txlt_sata_pkt->satapkt_op_mode |= SATA_OPMODE_SYNCH;
3936 	spx->txlt_sata_pkt->satapkt_comp = NULL;
3937 
3938 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3939 	    &spx->txlt_sata_pkt->satapkt_device);
3940 	ASSERT(sdinfo != NULL);
3941 	sata_id = &sdinfo->satadrv_id;
3942 
3943 	switch ((scsipkt->pkt_cdbp[4] & START_STOP_POWER_COND_MASK) >> 4) {
3944 	case 0:
3945 		if (scsipkt->pkt_cdbp[4] & START_STOP_LOEJ_MASK) {
3946 			/* Load/Unload Media - invalid request */
3947 			goto err_out;
3948 		}
3949 		if (scsipkt->pkt_cdbp[4] & START_STOP_START_MASK) {
3950 			/* Start Unit */
3951 			sata_build_read_verify_cmd(scmd, 1, 5);
3952 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3953 			/* Transfer command to HBA */
3954 			if (sata_hba_start(spx, &rval) != 0) {
3955 				/* Pkt not accepted for execution */
3956 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3957 				return (rval);
3958 			} else {
3959 				if (scmd->satacmd_error_reg != 0) {
3960 					goto err_out;
3961 				}
3962 			}
3963 			sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
3964 		} else {
3965 			/* Stop Unit */
3966 			sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
3967 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3968 			if (sata_hba_start(spx, &rval) != 0) {
3969 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3970 				return (rval);
3971 			} else {
3972 				if (scmd->satacmd_error_reg != 0) {
3973 					goto err_out;
3974 				}
3975 			}
3976 			/* ata standby immediate command */
3977 			sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
3978 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3979 			if (sata_hba_start(spx, &rval) != 0) {
3980 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3981 				return (rval);
3982 			} else {
3983 				if (scmd->satacmd_error_reg != 0) {
3984 					goto err_out;
3985 				}
3986 			}
3987 			sdinfo->satadrv_power_level = SATA_POWER_STOPPED;
3988 		}
3989 		break;
3990 	case 0x1:
3991 		sata_build_generic_cmd(scmd, SATAC_IDLE);
3992 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3993 		if (sata_hba_start(spx, &rval) != 0) {
3994 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3995 			return (rval);
3996 		} else {
3997 			if (scmd->satacmd_error_reg != 0) {
3998 				goto err_out;
3999 			}
4000 		}
4001 		sata_build_read_verify_cmd(scmd, 1, 5);
4002 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4003 		/* Transfer command to HBA */
4004 		if (sata_hba_start(spx, &rval) != 0) {
4005 			/* Pkt not accepted for execution */
4006 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4007 			return (rval);
4008 		} else {
4009 			if (scmd->satacmd_error_reg != 0) {
4010 				goto err_out;
4011 			}
4012 		}
4013 		sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
4014 		break;
4015 	case 0x2:
4016 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4017 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4018 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4019 			if (sata_hba_start(spx, &rval) != 0) {
4020 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4021 				return (rval);
4022 			} else {
4023 				if (scmd->satacmd_error_reg != 0) {
4024 					goto err_out;
4025 				}
4026 			}
4027 		}
4028 		sata_build_generic_cmd(scmd, SATAC_IDLE);
4029 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4030 		if (sata_hba_start(spx, &rval) != 0) {
4031 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4032 			return (rval);
4033 		} else {
4034 			if (scmd->satacmd_error_reg != 0) {
4035 				goto err_out;
4036 			}
4037 		}
4038 		if ((scsipkt->pkt_cdbp[3] & START_STOP_MODIFIER_MASK)) {
4039 			/*
4040 			 *  POWER CONDITION MODIFIER bit set
4041 			 *  to 0x1 or larger it will be handled
4042 			 *  on the same way as bit = 0x1
4043 			 */
4044 			if (!(sata_id->ai_cmdset84 &
4045 			    SATA_IDLE_UNLOAD_SUPPORTED)) {
4046 				sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4047 				break;
4048 			}
4049 			sata_build_generic_cmd(scmd, SATAC_IDLE_IM);
4050 			scmd->satacmd_features_reg = 0x44;
4051 			scmd->satacmd_lba_low_lsb = 0x4c;
4052 			scmd->satacmd_lba_mid_lsb = 0x4e;
4053 			scmd->satacmd_lba_high_lsb = 0x55;
4054 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4055 			if (sata_hba_start(spx, &rval) != 0) {
4056 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4057 				return (rval);
4058 			} else {
4059 				if (scmd->satacmd_error_reg != 0) {
4060 					goto err_out;
4061 				}
4062 			}
4063 		}
4064 		sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4065 		break;
4066 	case 0x3:
4067 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4068 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4069 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4070 			if (sata_hba_start(spx, &rval) != 0) {
4071 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4072 				return (rval);
4073 			} else {
4074 				if (scmd->satacmd_error_reg != 0) {
4075 					goto err_out;
4076 				}
4077 			}
4078 		}
4079 		sata_build_generic_cmd(scmd, SATAC_STANDBY);
4080 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4081 		if (sata_hba_start(spx, &rval) != 0) {
4082 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4083 			return (rval);
4084 		} else {
4085 			if (scmd->satacmd_error_reg != 0) {
4086 				goto err_out;
4087 			}
4088 		}
4089 		sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
4090 		break;
4091 	case 0x7:
4092 		sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
4093 		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
4094 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4095 		if (sata_hba_start(spx, &rval) != 0) {
4096 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4097 			return (rval);
4098 		} else {
4099 			if (scmd->satacmd_error_reg != 0) {
4100 				goto err_out;
4101 			}
4102 		}
4103 		switch (scmd->satacmd_sec_count_lsb) {
4104 		case SATA_PWRMODE_STANDBY:
4105 			sata_build_generic_cmd(scmd, SATAC_STANDBY);
4106 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4107 			    sdinfo->satadrv_standby_timer);
4108 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4109 			if (sata_hba_start(spx, &rval) != 0) {
4110 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4111 				return (rval);
4112 			} else {
4113 				if (scmd->satacmd_error_reg != 0) {
4114 					goto err_out;
4115 				}
4116 			}
4117 			break;
4118 		case SATA_PWRMODE_IDLE:
4119 			sata_build_generic_cmd(scmd, SATAC_IDLE);
4120 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4121 			    sdinfo->satadrv_standby_timer);
4122 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4123 			if (sata_hba_start(spx, &rval) != 0) {
4124 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4125 				return (rval);
4126 			} else {
4127 				if (scmd->satacmd_error_reg != 0) {
4128 					goto err_out;
4129 				}
4130 			}
4131 			break;
4132 		case SATA_PWRMODE_ACTIVE_SPINDOWN:
4133 		case SATA_PWRMODE_ACTIVE_SPINUP:
4134 		case SATA_PWRMODE_ACTIVE:
4135 			sata_build_generic_cmd(scmd, SATAC_IDLE);
4136 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4137 			    sdinfo->satadrv_standby_timer);
4138 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4139 			if (sata_hba_start(spx, &rval) != 0) {
4140 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4141 				return (rval);
4142 			} else {
4143 				if (scmd->satacmd_error_reg != 0) {
4144 					goto err_out;
4145 				}
4146 			}
4147 			sata_build_read_verify_cmd(scmd, 1, 5);
4148 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4149 			if (sata_hba_start(spx, &rval) != 0) {
4150 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4151 				return (rval);
4152 			} else {
4153 				if (scmd->satacmd_error_reg != 0) {
4154 					goto err_out;
4155 				}
4156 			}
4157 			break;
4158 		default:
4159 			goto err_out;
4160 		}
4161 		break;
4162 	case 0xb:
4163 		if ((sata_get_standby_timer(sdinfo->satadrv_standby_timer) ==
4164 		    0) || (!(sata_id->ai_cap & SATA_STANDBYTIMER))) {
4165 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4166 			return (sata_txlt_check_condition(spx,
4167 			    KEY_ILLEGAL_REQUEST,
4168 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4169 		}
4170 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4171 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4172 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4173 			if (sata_hba_start(spx, &rval) != 0) {
4174 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4175 				return (rval);
4176 			} else {
4177 				if (scmd->satacmd_error_reg != 0) {
4178 					goto err_out;
4179 				}
4180 			}
4181 			sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
4182 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4183 			if (sata_hba_start(spx, &rval) != 0) {
4184 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4185 				return (rval);
4186 			} else {
4187 				if (scmd->satacmd_error_reg != 0) {
4188 					goto err_out;
4189 				}
4190 			}
4191 		}
4192 		bzero(sdinfo->satadrv_standby_timer, sizeof (uchar_t) * 4);
4193 		break;
4194 	default:
4195 err_out:
4196 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4197 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4198 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4199 	}
4200 
4201 	/*
4202 	 * Since it was a synchronous command,
4203 	 * a callback function will be called directly.
4204 	 */
4205 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4206 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4207 	    "synchronous execution status %x\n",
4208 	    spx->txlt_sata_pkt->satapkt_reason);
4209 
4210 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) {
4211 		sata_set_arq_data(spx->txlt_sata_pkt);
4212 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4213 		    (task_func_t *)scsi_hba_pkt_comp, (void *) scsipkt,
4214 		    TQ_SLEEP) == 0) {
4215 			return (TRAN_BUSY);
4216 		}
4217 	}
4218 	else
4219 
4220 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
4221 
4222 	return (TRAN_ACCEPT);
4223 
4224 }
4225 
4226 /*
4227  * SATA translate command:  Read Capacity.
4228  * Emulated command for SATA disks.
4229  * Capacity is retrieved from cached Idenifty Device data.
4230  * Identify Device data shows effective disk capacity, not the native
4231  * capacity, which may be limitted by Set Max Address command.
4232  * This is ATA version for SATA hard disks.
4233  *
4234  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4235  */
4236 static int
4237 sata_txlt_read_capacity(sata_pkt_txlate_t *spx)
4238 {
4239 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4240 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4241 	sata_drive_info_t *sdinfo;
4242 	uint64_t val;
4243 	uchar_t *rbuf;
4244 	int rval, reason;
4245 
4246 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4247 	    "sata_txlt_read_capacity: ", NULL);
4248 
4249 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4250 
4251 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4252 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4253 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4254 		return (rval);
4255 	}
4256 
4257 	scsipkt->pkt_reason = CMD_CMPLT;
4258 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4259 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4260 	*scsipkt->pkt_scbp = STATUS_GOOD;
4261 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4262 		/*
4263 		 * Because it is fully emulated command storing data
4264 		 * programatically in the specified buffer, release
4265 		 * preallocated DMA resources before storing data in the buffer,
4266 		 * so no unwanted DMA sync would take place.
4267 		 */
4268 		sata_scsi_dmafree(NULL, scsipkt);
4269 
4270 		sdinfo = sata_get_device_info(
4271 		    spx->txlt_sata_hba_inst,
4272 		    &spx->txlt_sata_pkt->satapkt_device);
4273 		/* Last logical block address */
4274 		val = sdinfo->satadrv_capacity - 1;
4275 		rbuf = (uchar_t *)bp->b_un.b_addr;
4276 		/* Need to swap endians to match scsi format */
4277 		rbuf[0] = (val >> 24) & 0xff;
4278 		rbuf[1] = (val >> 16) & 0xff;
4279 		rbuf[2] = (val >> 8) & 0xff;
4280 		rbuf[3] = val & 0xff;
4281 		/* block size - always 512 bytes, for now */
4282 		rbuf[4] = 0;
4283 		rbuf[5] = 0;
4284 		rbuf[6] = 0x02;
4285 		rbuf[7] = 0;
4286 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4287 		scsipkt->pkt_resid = 0;
4288 
4289 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n",
4290 		    sdinfo->satadrv_capacity -1);
4291 	}
4292 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4293 	/*
4294 	 * If a callback was requested, do it now.
4295 	 */
4296 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4297 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4298 
4299 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4300 	    scsipkt->pkt_comp != NULL)
4301 		/* scsi callback required */
4302 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4303 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4304 		    TQ_SLEEP) == NULL)
4305 			/* Scheduling the callback failed */
4306 			return (TRAN_BUSY);
4307 
4308 	return (TRAN_ACCEPT);
4309 }
4310 
4311 /*
4312  * SATA translate command: Mode Sense.
4313  * Translated into appropriate SATA command or emulated.
4314  * Saved Values Page Control (03) are not supported.
4315  *
4316  * NOTE: only caching mode sense page is currently implemented.
4317  *
4318  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4319  */
4320 
4321 #define	LLBAA	0x10	/* Long LBA Accepted */
4322 
4323 static int
4324 sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
4325 {
4326 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4327 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4328 	sata_drive_info_t *sdinfo;
4329 	sata_id_t *sata_id;
4330 	struct scsi_extended_sense *sense;
4331 	int 		len, bdlen, count, alc_len;
4332 	int		pc;	/* Page Control code */
4333 	uint8_t		*buf;	/* mode sense buffer */
4334 	int		rval, reason;
4335 
4336 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4337 	    "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
4338 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4339 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4340 
4341 	buf = kmem_zalloc(1024, KM_SLEEP);
4342 
4343 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4344 
4345 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4346 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4347 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4348 		kmem_free(buf, 1024);
4349 		return (rval);
4350 	}
4351 
4352 	scsipkt->pkt_reason = CMD_CMPLT;
4353 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4354 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4355 
4356 	pc = scsipkt->pkt_cdbp[2] >> 6;
4357 
4358 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4359 		/*
4360 		 * Because it is fully emulated command storing data
4361 		 * programatically in the specified buffer, release
4362 		 * preallocated DMA resources before storing data in the buffer,
4363 		 * so no unwanted DMA sync would take place.
4364 		 */
4365 		sata_scsi_dmafree(NULL, scsipkt);
4366 
4367 		len = 0;
4368 		bdlen = 0;
4369 		if (!(scsipkt->pkt_cdbp[1] & 8)) {
4370 			if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
4371 			    (scsipkt->pkt_cdbp[1] & LLBAA))
4372 				bdlen = 16;
4373 			else
4374 				bdlen = 8;
4375 		}
4376 		/* Build mode parameter header */
4377 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
4378 			/* 4-byte mode parameter header */
4379 			buf[len++] = 0;		/* mode data length */
4380 			buf[len++] = 0;		/* medium type */
4381 			buf[len++] = 0;		/* dev-specific param */
4382 			buf[len++] = bdlen;	/* Block Descriptor length */
4383 		} else {
4384 			/* 8-byte mode parameter header */
4385 			buf[len++] = 0;		/* mode data length */
4386 			buf[len++] = 0;
4387 			buf[len++] = 0;		/* medium type */
4388 			buf[len++] = 0;		/* dev-specific param */
4389 			if (bdlen == 16)
4390 				buf[len++] = 1;	/* long lba descriptor */
4391 			else
4392 				buf[len++] = 0;
4393 			buf[len++] = 0;
4394 			buf[len++] = 0;		/* Block Descriptor length */
4395 			buf[len++] = bdlen;
4396 		}
4397 
4398 		sdinfo = sata_get_device_info(
4399 		    spx->txlt_sata_hba_inst,
4400 		    &spx->txlt_sata_pkt->satapkt_device);
4401 
4402 		/* Build block descriptor only if not disabled (DBD) */
4403 		if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
4404 			/* Block descriptor - direct-access device format */
4405 			if (bdlen == 8) {
4406 				/* build regular block descriptor */
4407 				buf[len++] =
4408 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
4409 				buf[len++] =
4410 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
4411 				buf[len++] =
4412 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
4413 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
4414 				buf[len++] = 0; /* density code */
4415 				buf[len++] = 0;
4416 				if (sdinfo->satadrv_type ==
4417 				    SATA_DTYPE_ATADISK)
4418 					buf[len++] = 2;
4419 				else
4420 					/* ATAPI */
4421 					buf[len++] = 8;
4422 				buf[len++] = 0;
4423 			} else if (bdlen == 16) {
4424 				/* Long LBA Accepted */
4425 				/* build long lba block descriptor */
4426 #ifndef __lock_lint
4427 				buf[len++] =
4428 				    (sdinfo->satadrv_capacity >> 56) & 0xff;
4429 				buf[len++] =
4430 				    (sdinfo->satadrv_capacity >> 48) & 0xff;
4431 				buf[len++] =
4432 				    (sdinfo->satadrv_capacity >> 40) & 0xff;
4433 				buf[len++] =
4434 				    (sdinfo->satadrv_capacity >> 32) & 0xff;
4435 #endif
4436 				buf[len++] =
4437 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
4438 				buf[len++] =
4439 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
4440 				buf[len++] =
4441 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
4442 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
4443 				buf[len++] = 0;
4444 				buf[len++] = 0; /* density code */
4445 				buf[len++] = 0;
4446 				buf[len++] = 0;
4447 				if (sdinfo->satadrv_type ==
4448 				    SATA_DTYPE_ATADISK)
4449 					buf[len++] = 2;
4450 				else
4451 					/* ATAPI */
4452 					buf[len++] = 8;
4453 				buf[len++] = 0;
4454 			}
4455 		}
4456 
4457 		sata_id = &sdinfo->satadrv_id;
4458 
4459 		/*
4460 		 * Add requested pages.
4461 		 * Page 3 and 4 are obsolete and we are not supporting them.
4462 		 * We deal now with:
4463 		 * caching (read/write cache control).
4464 		 * We should eventually deal with following mode pages:
4465 		 * error recovery  (0x01),
4466 		 * power condition (0x1a),
4467 		 * exception control page (enables SMART) (0x1c),
4468 		 * enclosure management (ses),
4469 		 * protocol-specific port mode (port control).
4470 		 */
4471 		switch (scsipkt->pkt_cdbp[2] & 0x3f) {
4472 		case MODEPAGE_RW_ERRRECOV:
4473 			/* DAD_MODE_ERR_RECOV */
4474 			/* R/W recovery */
4475 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
4476 			break;
4477 		case MODEPAGE_CACHING:
4478 			/* DAD_MODE_CACHE */
4479 			/* Reject not supported request for saved parameters */
4480 			if (pc == 3) {
4481 				*scsipkt->pkt_scbp = STATUS_CHECK;
4482 				sense = sata_arq_sense(spx);
4483 				sense->es_key = KEY_ILLEGAL_REQUEST;
4484 				sense->es_add_code =
4485 				    SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
4486 				goto done;
4487 			}
4488 
4489 			/* caching */
4490 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
4491 			break;
4492 		case MODEPAGE_INFO_EXCPT:
4493 			/* exception cntrl */
4494 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
4495 				len += sata_build_msense_page_1c(sdinfo, pc,
4496 				    buf+len);
4497 			}
4498 			else
4499 				goto err;
4500 			break;
4501 		case MODEPAGE_POWER_COND:
4502 			/* DAD_MODE_POWER_COND */
4503 			/* power condition */
4504 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
4505 			break;
4506 
4507 		case MODEPAGE_ACOUSTIC_MANAG:
4508 			/* acoustic management */
4509 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
4510 			break;
4511 		case MODEPAGE_ALLPAGES:
4512 			/* all pages */
4513 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
4514 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
4515 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
4516 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
4517 				len += sata_build_msense_page_1c(sdinfo, pc,
4518 				    buf+len);
4519 			}
4520 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
4521 			break;
4522 		default:
4523 		err:
4524 			/* Invalid request */
4525 			*scsipkt->pkt_scbp = STATUS_CHECK;
4526 			sense = sata_arq_sense(spx);
4527 			sense->es_key = KEY_ILLEGAL_REQUEST;
4528 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4529 			goto done;
4530 		}
4531 
4532 		/* fix total mode data length */
4533 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
4534 			/* 4-byte mode parameter header */
4535 			buf[0] = len - 1;	/* mode data length */
4536 		} else {
4537 			buf[0] = (len -2) >> 8;
4538 			buf[1] = (len -2) & 0xff;
4539 		}
4540 
4541 
4542 		/* Check allocation length */
4543 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
4544 			alc_len = scsipkt->pkt_cdbp[4];
4545 		} else {
4546 			alc_len = scsipkt->pkt_cdbp[7];
4547 			alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
4548 		}
4549 		/*
4550 		 * We do not check for possible parameters truncation
4551 		 * (alc_len < len) assuming that the target driver works
4552 		 * correctly. Just avoiding overrun.
4553 		 * Copy no more than requested and possible, buffer-wise.
4554 		 */
4555 		count = MIN(alc_len, len);
4556 		count = MIN(bp->b_bcount, count);
4557 		bcopy(buf, bp->b_un.b_addr, count);
4558 
4559 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4560 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
4561 	}
4562 	*scsipkt->pkt_scbp = STATUS_GOOD;
4563 done:
4564 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4565 	(void) kmem_free(buf, 1024);
4566 
4567 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4568 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4569 
4570 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4571 	    scsipkt->pkt_comp != NULL)
4572 		/* scsi callback required */
4573 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4574 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4575 		    TQ_SLEEP) == NULL)
4576 			/* Scheduling the callback failed */
4577 			return (TRAN_BUSY);
4578 
4579 	return (TRAN_ACCEPT);
4580 }
4581 
4582 
4583 /*
4584  * SATA translate command: Mode Select.
4585  * Translated into appropriate SATA command or emulated.
4586  * Saving parameters is not supported.
4587  * Changing device capacity is not supported (although theoretically
4588  * possible by executing SET FEATURES/SET MAX ADDRESS)
4589  *
4590  * Assumption is that the target driver is working correctly.
4591  *
4592  * More than one SATA command may be executed to perform operations specified
4593  * by mode select pages. The first error terminates further execution.
4594  * Operations performed successully are not backed-up in such case.
4595  *
4596  * NOTE: Implemented pages:
4597  * - caching page
4598  * - informational exception page
4599  * - acoustic management page
4600  * - power condition page
4601  * Caching setup is remembered so it could be re-stored in case of
4602  * an unexpected device reset.
4603  *
4604  * Returns TRAN_XXXX.
4605  * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields.
4606  */
4607 
4608 static int
4609 sata_txlt_mode_select(sata_pkt_txlate_t *spx)
4610 {
4611 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4612 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4613 	struct scsi_extended_sense *sense;
4614 	int len, pagelen, count, pllen;
4615 	uint8_t *buf;	/* mode select buffer */
4616 	int rval, stat, reason;
4617 	uint_t nointr_flag;
4618 	int dmod = 0;
4619 
4620 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4621 	    "sata_txlt_mode_select, pc %x page code 0x%02x\n",
4622 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4623 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4624 
4625 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4626 
4627 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4628 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4629 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4630 		return (rval);
4631 	}
4632 	/*
4633 	 * If in interrupt context, reject this packet because it may result
4634 	 * in issuing a synchronous command to HBA.
4635 	 */
4636 	if (servicing_interrupt()) {
4637 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
4638 		    "sata_txlt_mode_select: rejecting command because "
4639 		    "of interrupt context\n", NULL);
4640 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4641 		return (TRAN_BUSY);
4642 	}
4643 
4644 	rval = TRAN_ACCEPT;
4645 
4646 	scsipkt->pkt_reason = CMD_CMPLT;
4647 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4648 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4649 	nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
4650 
4651 	/* Reject not supported request */
4652 	if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
4653 		*scsipkt->pkt_scbp = STATUS_CHECK;
4654 		sense = sata_arq_sense(spx);
4655 		sense->es_key = KEY_ILLEGAL_REQUEST;
4656 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4657 		goto done;
4658 	}
4659 
4660 	if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
4661 		pllen = scsipkt->pkt_cdbp[4];
4662 	} else {
4663 		pllen = scsipkt->pkt_cdbp[7];
4664 		pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
4665 	}
4666 
4667 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
4668 
4669 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
4670 		buf = (uint8_t *)bp->b_un.b_addr;
4671 		count = MIN(bp->b_bcount, pllen);
4672 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4673 		scsipkt->pkt_resid = 0;
4674 		pllen = count;
4675 
4676 		/*
4677 		 * Check the header to skip the block descriptor(s) - we
4678 		 * do not support setting device capacity.
4679 		 * Existing macros do not recognize long LBA dscriptor,
4680 		 * hence manual calculation.
4681 		 */
4682 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
4683 			/* 6-bytes CMD, 4 bytes header */
4684 			if (count <= 4)
4685 				goto done;		/* header only */
4686 			len = buf[3] + 4;
4687 		} else {
4688 			/* 10-bytes CMD, 8 bytes header */
4689 			if (count <= 8)
4690 				goto done;		/* header only */
4691 			len = buf[6];
4692 			len = (len << 8) + buf[7] + 8;
4693 		}
4694 		if (len >= count)
4695 			goto done;	/* header + descriptor(s) only */
4696 
4697 		pllen -= len;		/* remaining data length */
4698 
4699 		/*
4700 		 * We may be executing SATA command and want to execute it
4701 		 * in SYNCH mode, regardless of scsi_pkt setting.
4702 		 * Save scsi_pkt setting and indicate SYNCH mode
4703 		 */
4704 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4705 		    scsipkt->pkt_comp != NULL) {
4706 			scsipkt->pkt_flags |= FLAG_NOINTR;
4707 		}
4708 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
4709 
4710 		/*
4711 		 * len is now the offset to a first mode select page
4712 		 * Process all pages
4713 		 */
4714 		while (pllen > 0) {
4715 			switch ((int)buf[len]) {
4716 			case MODEPAGE_CACHING:
4717 				/* No support for SP (saving) */
4718 				if (scsipkt->pkt_cdbp[1] & 0x01) {
4719 					*scsipkt->pkt_scbp = STATUS_CHECK;
4720 					sense = sata_arq_sense(spx);
4721 					sense->es_key = KEY_ILLEGAL_REQUEST;
4722 					sense->es_add_code =
4723 					    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4724 					goto done;
4725 				}
4726 				stat = sata_mode_select_page_8(spx,
4727 				    (struct mode_cache_scsi3 *)&buf[len],
4728 				    pllen, &pagelen, &rval, &dmod);
4729 				/*
4730 				 * The pagelen value indicates the number of
4731 				 * parameter bytes already processed.
4732 				 * The rval is the return value from
4733 				 * sata_tran_start().
4734 				 * The stat indicates the overall status of
4735 				 * the operation(s).
4736 				 */
4737 				if (stat != SATA_SUCCESS)
4738 					/*
4739 					 * Page processing did not succeed -
4740 					 * all error info is already set-up,
4741 					 * just return
4742 					 */
4743 					pllen = 0; /* this breaks the loop */
4744 				else {
4745 					len += pagelen;
4746 					pllen -= pagelen;
4747 				}
4748 				break;
4749 
4750 			case MODEPAGE_INFO_EXCPT:
4751 				stat = sata_mode_select_page_1c(spx,
4752 				    (struct mode_info_excpt_page *)&buf[len],
4753 				    pllen, &pagelen, &rval, &dmod);
4754 				/*
4755 				 * The pagelen value indicates the number of
4756 				 * parameter bytes already processed.
4757 				 * The rval is the return value from
4758 				 * sata_tran_start().
4759 				 * The stat indicates the overall status of
4760 				 * the operation(s).
4761 				 */
4762 				if (stat != SATA_SUCCESS)
4763 					/*
4764 					 * Page processing did not succeed -
4765 					 * all error info is already set-up,
4766 					 * just return
4767 					 */
4768 					pllen = 0; /* this breaks the loop */
4769 				else {
4770 					len += pagelen;
4771 					pllen -= pagelen;
4772 				}
4773 				break;
4774 
4775 			case MODEPAGE_ACOUSTIC_MANAG:
4776 				stat = sata_mode_select_page_30(spx,
4777 				    (struct mode_acoustic_management *)
4778 				    &buf[len], pllen, &pagelen, &rval, &dmod);
4779 				/*
4780 				 * The pagelen value indicates the number of
4781 				 * parameter bytes already processed.
4782 				 * The rval is the return value from
4783 				 * sata_tran_start().
4784 				 * The stat indicates the overall status of
4785 				 * the operation(s).
4786 				 */
4787 				if (stat != SATA_SUCCESS)
4788 					/*
4789 					 * Page processing did not succeed -
4790 					 * all error info is already set-up,
4791 					 * just return
4792 					 */
4793 					pllen = 0; /* this breaks the loop */
4794 				else {
4795 					len += pagelen;
4796 					pllen -= pagelen;
4797 				}
4798 
4799 				break;
4800 			case MODEPAGE_POWER_COND:
4801 				stat = sata_mode_select_page_1a(spx,
4802 				    (struct mode_info_power_cond *)&buf[len],
4803 				    pllen, &pagelen, &rval, &dmod);
4804 				/*
4805 				 * The pagelen value indicates the number of
4806 				 * parameter bytes already processed.
4807 				 * The rval is the return value from
4808 				 * sata_tran_start().
4809 				 * The stat indicates the overall status of
4810 				 * the operation(s).
4811 				 */
4812 				if (stat != SATA_SUCCESS)
4813 					/*
4814 					 * Page processing did not succeed -
4815 					 * all error info is already set-up,
4816 					 * just return
4817 					 */
4818 					pllen = 0; /* this breaks the loop */
4819 				else {
4820 					len += pagelen;
4821 					pllen -= pagelen;
4822 				}
4823 				break;
4824 			default:
4825 				*scsipkt->pkt_scbp = STATUS_CHECK;
4826 				sense = sata_arq_sense(spx);
4827 				sense->es_key = KEY_ILLEGAL_REQUEST;
4828 				sense->es_add_code =
4829 				    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
4830 				goto done;
4831 			}
4832 		}
4833 	}
4834 done:
4835 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4836 	/*
4837 	 * If device parameters were modified, fetch and store the new
4838 	 * Identify Device data. Since port mutex could have been released
4839 	 * for accessing HBA driver, we need to re-check device existence.
4840 	 */
4841 	if (dmod != 0) {
4842 		sata_drive_info_t new_sdinfo, *sdinfo;
4843 		int rv = 0;
4844 
4845 		/*
4846 		 * Following statement has to be changed if this function is
4847 		 * used for devices other than SATA hard disks.
4848 		 */
4849 		new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
4850 
4851 		new_sdinfo.satadrv_addr =
4852 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr;
4853 		rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
4854 		    &new_sdinfo);
4855 
4856 		mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4857 		/*
4858 		 * Since port mutex could have been released when
4859 		 * accessing HBA driver, we need to re-check that the
4860 		 * framework still holds the device info structure.
4861 		 */
4862 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4863 		    &spx->txlt_sata_pkt->satapkt_device);
4864 		if (sdinfo != NULL) {
4865 			/*
4866 			 * Device still has info structure in the
4867 			 * sata framework. Copy newly fetched info
4868 			 */
4869 			if (rv == 0) {
4870 				sdinfo->satadrv_id = new_sdinfo.satadrv_id;
4871 				sata_save_drive_settings(sdinfo);
4872 			} else {
4873 				/*
4874 				 * Could not fetch new data - invalidate
4875 				 * sata_drive_info. That makes device
4876 				 * unusable.
4877 				 */
4878 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
4879 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
4880 			}
4881 		}
4882 		if (rv != 0 || sdinfo == NULL) {
4883 			/*
4884 			 * This changes the overall mode select completion
4885 			 * reason to a failed one !!!!!
4886 			 */
4887 			*scsipkt->pkt_scbp = STATUS_CHECK;
4888 			sense = sata_arq_sense(spx);
4889 			scsipkt->pkt_reason = CMD_INCOMPLETE;
4890 			rval = TRAN_ACCEPT;
4891 		}
4892 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4893 	}
4894 	/* Restore the scsi pkt flags */
4895 	scsipkt->pkt_flags &= ~FLAG_NOINTR;
4896 	scsipkt->pkt_flags |= nointr_flag;
4897 
4898 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4899 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4900 
4901 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4902 	    scsipkt->pkt_comp != NULL)
4903 		/* scsi callback required */
4904 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4905 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4906 		    TQ_SLEEP) == NULL)
4907 			/* Scheduling the callback failed */
4908 			return (TRAN_BUSY);
4909 
4910 	return (rval);
4911 }
4912 
4913 
4914 
4915 /*
4916  * Translate command: Log Sense
4917  */
4918 static 	int
4919 sata_txlt_log_sense(sata_pkt_txlate_t *spx)
4920 {
4921 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4922 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4923 	sata_drive_info_t *sdinfo;
4924 	struct scsi_extended_sense *sense;
4925 	int 		len, count, alc_len;
4926 	int		pc;	/* Page Control code */
4927 	int		page_code;	/* Page code */
4928 	uint8_t		*buf;	/* log sense buffer */
4929 	int		rval, reason;
4930 #define	MAX_LOG_SENSE_PAGE_SIZE	512
4931 
4932 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4933 	    "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
4934 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4935 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4936 
4937 	buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
4938 
4939 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4940 
4941 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4942 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4943 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4944 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4945 		return (rval);
4946 	}
4947 	/*
4948 	 * If in interrupt context, reject this packet because it may result
4949 	 * in issuing a synchronous command to HBA.
4950 	 */
4951 	if (servicing_interrupt()) {
4952 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
4953 		    "sata_log_sense: rejecting command because "
4954 		    "of interrupt context\n", NULL);
4955 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4956 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4957 		return (TRAN_BUSY);
4958 	}
4959 
4960 	scsipkt->pkt_reason = CMD_CMPLT;
4961 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4962 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4963 
4964 	pc = scsipkt->pkt_cdbp[2] >> 6;
4965 	page_code = scsipkt->pkt_cdbp[2] & 0x3f;
4966 
4967 	/* Reject not supported request for all but cumulative values */
4968 	switch (pc) {
4969 	case PC_CUMULATIVE_VALUES:
4970 		break;
4971 	default:
4972 		*scsipkt->pkt_scbp = STATUS_CHECK;
4973 		sense = sata_arq_sense(spx);
4974 		sense->es_key = KEY_ILLEGAL_REQUEST;
4975 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4976 		goto done;
4977 	}
4978 
4979 	switch (page_code) {
4980 	case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
4981 	case PAGE_CODE_SELF_TEST_RESULTS:
4982 	case PAGE_CODE_INFORMATION_EXCEPTIONS:
4983 	case PAGE_CODE_SMART_READ_DATA:
4984 	case PAGE_CODE_START_STOP_CYCLE_COUNTER:
4985 		break;
4986 	default:
4987 		*scsipkt->pkt_scbp = STATUS_CHECK;
4988 		sense = sata_arq_sense(spx);
4989 		sense->es_key = KEY_ILLEGAL_REQUEST;
4990 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4991 		goto done;
4992 	}
4993 
4994 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4995 		/*
4996 		 * Because log sense uses local buffers for data retrieval from
4997 		 * the devices and sets the data programatically in the
4998 		 * original specified buffer, release preallocated DMA
4999 		 * resources before storing data in the original buffer,
5000 		 * so no unwanted DMA sync would take place.
5001 		 */
5002 		sata_id_t *sata_id;
5003 
5004 		sata_scsi_dmafree(NULL, scsipkt);
5005 
5006 		len = 0;
5007 
5008 		/* Build log parameter header */
5009 		buf[len++] = page_code;	/* page code as in the CDB */
5010 		buf[len++] = 0;		/* reserved */
5011 		buf[len++] = 0;		/* Zero out page length for now (MSB) */
5012 		buf[len++] = 0;		/* (LSB) */
5013 
5014 		sdinfo = sata_get_device_info(
5015 		    spx->txlt_sata_hba_inst,
5016 		    &spx->txlt_sata_pkt->satapkt_device);
5017 
5018 		/*
5019 		 * Add requested pages.
5020 		 */
5021 		switch (page_code) {
5022 		case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5023 			len = sata_build_lsense_page_0(sdinfo, buf + len);
5024 			break;
5025 		case PAGE_CODE_SELF_TEST_RESULTS:
5026 			sata_id = &sdinfo->satadrv_id;
5027 			if ((! (sata_id->ai_cmdset84 &
5028 			    SATA_SMART_SELF_TEST_SUPPORTED)) ||
5029 			    (! (sata_id->ai_features87 &
5030 			    SATA_SMART_SELF_TEST_SUPPORTED))) {
5031 				*scsipkt->pkt_scbp = STATUS_CHECK;
5032 				sense = sata_arq_sense(spx);
5033 				sense->es_key = KEY_ILLEGAL_REQUEST;
5034 				sense->es_add_code =
5035 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5036 
5037 				goto done;
5038 			}
5039 			len = sata_build_lsense_page_10(sdinfo, buf + len,
5040 			    spx->txlt_sata_hba_inst);
5041 			break;
5042 		case PAGE_CODE_INFORMATION_EXCEPTIONS:
5043 			sata_id = &sdinfo->satadrv_id;
5044 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5045 				*scsipkt->pkt_scbp = STATUS_CHECK;
5046 				sense = sata_arq_sense(spx);
5047 				sense->es_key = KEY_ILLEGAL_REQUEST;
5048 				sense->es_add_code =
5049 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5050 
5051 				goto done;
5052 			}
5053 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5054 				*scsipkt->pkt_scbp = STATUS_CHECK;
5055 				sense = sata_arq_sense(spx);
5056 				sense->es_key = KEY_ABORTED_COMMAND;
5057 				sense->es_add_code =
5058 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5059 				sense->es_qual_code =
5060 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5061 
5062 				goto done;
5063 			}
5064 
5065 			len = sata_build_lsense_page_2f(sdinfo, buf + len,
5066 			    spx->txlt_sata_hba_inst);
5067 			break;
5068 		case PAGE_CODE_SMART_READ_DATA:
5069 			sata_id = &sdinfo->satadrv_id;
5070 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5071 				*scsipkt->pkt_scbp = STATUS_CHECK;
5072 				sense = sata_arq_sense(spx);
5073 				sense->es_key = KEY_ILLEGAL_REQUEST;
5074 				sense->es_add_code =
5075 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5076 
5077 				goto done;
5078 			}
5079 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5080 				*scsipkt->pkt_scbp = STATUS_CHECK;
5081 				sense = sata_arq_sense(spx);
5082 				sense->es_key = KEY_ABORTED_COMMAND;
5083 				sense->es_add_code =
5084 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5085 				sense->es_qual_code =
5086 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5087 
5088 				goto done;
5089 			}
5090 
5091 			/* This page doesn't include a page header */
5092 			len = sata_build_lsense_page_30(sdinfo, buf,
5093 			    spx->txlt_sata_hba_inst);
5094 			goto no_header;
5095 		case PAGE_CODE_START_STOP_CYCLE_COUNTER:
5096 			sata_id = &sdinfo->satadrv_id;
5097 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5098 				*scsipkt->pkt_scbp = STATUS_CHECK;
5099 				sense = sata_arq_sense(spx);
5100 				sense->es_key = KEY_ILLEGAL_REQUEST;
5101 				sense->es_add_code =
5102 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5103 
5104 				goto done;
5105 			}
5106 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5107 				*scsipkt->pkt_scbp = STATUS_CHECK;
5108 				sense = sata_arq_sense(spx);
5109 				sense->es_key = KEY_ABORTED_COMMAND;
5110 				sense->es_add_code =
5111 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5112 				sense->es_qual_code =
5113 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5114 
5115 				goto done;
5116 			}
5117 			len = sata_build_lsense_page_0e(sdinfo, buf, spx);
5118 			goto no_header;
5119 		default:
5120 			/* Invalid request */
5121 			*scsipkt->pkt_scbp = STATUS_CHECK;
5122 			sense = sata_arq_sense(spx);
5123 			sense->es_key = KEY_ILLEGAL_REQUEST;
5124 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5125 			goto done;
5126 		}
5127 
5128 		/* set parameter log sense data length */
5129 		buf[2] = len >> 8;	/* log sense length (MSB) */
5130 		buf[3] = len & 0xff;	/* log sense length (LSB) */
5131 
5132 		len += SCSI_LOG_PAGE_HDR_LEN;
5133 		ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
5134 
5135 no_header:
5136 		/* Check allocation length */
5137 		alc_len = scsipkt->pkt_cdbp[7];
5138 		alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
5139 
5140 		/*
5141 		 * We do not check for possible parameters truncation
5142 		 * (alc_len < len) assuming that the target driver works
5143 		 * correctly. Just avoiding overrun.
5144 		 * Copy no more than requested and possible, buffer-wise.
5145 		 */
5146 		count = MIN(alc_len, len);
5147 		count = MIN(bp->b_bcount, count);
5148 		bcopy(buf, bp->b_un.b_addr, count);
5149 
5150 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
5151 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
5152 	}
5153 	*scsipkt->pkt_scbp = STATUS_GOOD;
5154 done:
5155 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5156 	(void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
5157 
5158 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5159 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5160 
5161 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5162 	    scsipkt->pkt_comp != NULL)
5163 		/* scsi callback required */
5164 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5165 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5166 		    TQ_SLEEP) == NULL)
5167 			/* Scheduling the callback failed */
5168 			return (TRAN_BUSY);
5169 
5170 	return (TRAN_ACCEPT);
5171 }
5172 
5173 /*
5174  * Translate command: Log Select
5175  * Not implemented at this time - returns invalid command response.
5176  */
5177 static	int
5178 sata_txlt_log_select(sata_pkt_txlate_t *spx)
5179 {
5180 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5181 	    "sata_txlt_log_select\n", NULL);
5182 
5183 	return (sata_txlt_invalid_command(spx));
5184 }
5185 
5186 
5187 /*
5188  * Translate command: Read (various types).
5189  * Translated into appropriate type of ATA READ command
5190  * for SATA hard disks.
5191  * Both the device capabilities and requested operation mode are
5192  * considered.
5193  *
5194  * Following scsi cdb fields are ignored:
5195  * rdprotect, dpo, fua, fua_nv, group_number.
5196  *
5197  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
5198  * enable variable sata_func_enable), the capability of the controller and
5199  * capability of a device are checked and if both support queueing, read
5200  * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
5201  * command rather than plain READ_XXX command.
5202  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
5203  * both the controller and device suport such functionality, the read
5204  * request will be translated to READ_FPDMA_QUEUED command.
5205  * In both cases the maximum queue depth is derived as minimum of:
5206  * HBA capability,device capability and sata_max_queue_depth variable setting.
5207  * The value passed to HBA driver is decremented by 1, because only 5 bits are
5208  * used to pass max queue depth value, and the maximum possible queue depth
5209  * is 32.
5210  *
5211  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
5212  * appropriate values in scsi_pkt fields.
5213  */
5214 static int
5215 sata_txlt_read(sata_pkt_txlate_t *spx)
5216 {
5217 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5218 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5219 	sata_drive_info_t *sdinfo;
5220 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
5221 	int cport = SATA_TXLT_CPORT(spx);
5222 	uint16_t sec_count;
5223 	uint64_t lba;
5224 	int rval, reason;
5225 	int synch;
5226 
5227 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5228 
5229 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
5230 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5231 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5232 		return (rval);
5233 	}
5234 
5235 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
5236 	    &spx->txlt_sata_pkt->satapkt_device);
5237 
5238 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
5239 	/*
5240 	 * Extract LBA and sector count from scsi CDB.
5241 	 */
5242 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
5243 	case SCMD_READ:
5244 		/* 6-byte scsi read cmd : 0x08 */
5245 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
5246 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
5247 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5248 		sec_count = scsipkt->pkt_cdbp[4];
5249 		/* sec_count 0 will be interpreted as 256 by a device */
5250 		break;
5251 	case SCMD_READ_G1:
5252 		/* 10-bytes scsi read command : 0x28 */
5253 		lba = scsipkt->pkt_cdbp[2];
5254 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5255 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5256 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5257 		sec_count = scsipkt->pkt_cdbp[7];
5258 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
5259 		break;
5260 	case SCMD_READ_G5:
5261 		/* 12-bytes scsi read command : 0xA8 */
5262 		lba = scsipkt->pkt_cdbp[2];
5263 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5264 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5265 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5266 		sec_count = scsipkt->pkt_cdbp[6];
5267 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
5268 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
5269 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
5270 		break;
5271 	case SCMD_READ_G4:
5272 		/* 16-bytes scsi read command : 0x88 */
5273 		lba = scsipkt->pkt_cdbp[2];
5274 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5275 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5276 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5277 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
5278 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5279 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5280 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
5281 		sec_count = scsipkt->pkt_cdbp[10];
5282 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
5283 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
5284 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
5285 		break;
5286 	default:
5287 		/* Unsupported command */
5288 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5289 		return (sata_txlt_invalid_command(spx));
5290 	}
5291 
5292 	/*
5293 	 * Check if specified address exceeds device capacity
5294 	 */
5295 	if ((lba >= sdinfo->satadrv_capacity) ||
5296 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
5297 		/* LBA out of range */
5298 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5299 		return (sata_txlt_lba_out_of_range(spx));
5300 	}
5301 
5302 	/*
5303 	 * For zero-length transfer, emulate good completion of the command
5304 	 * (reasons for rejecting the command were already checked).
5305 	 * No DMA resources were allocated.
5306 	 */
5307 	if (spx->txlt_dma_cookie_list == NULL) {
5308 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5309 		return (sata_emul_rw_completion(spx));
5310 	}
5311 
5312 	/*
5313 	 * Build cmd block depending on the device capability and
5314 	 * requested operation mode.
5315 	 * Do not bother with non-dma mode - we are working only with
5316 	 * devices supporting DMA.
5317 	 */
5318 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
5319 	scmd->satacmd_device_reg = SATA_ADH_LBA;
5320 	scmd->satacmd_cmd_reg = SATAC_READ_DMA;
5321 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
5322 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
5323 		scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT;
5324 		scmd->satacmd_sec_count_msb = sec_count >> 8;
5325 #ifndef __lock_lint
5326 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
5327 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
5328 		scmd->satacmd_lba_high_msb = lba >> 40;
5329 #endif
5330 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
5331 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
5332 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
5333 	}
5334 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
5335 	scmd->satacmd_lba_low_lsb = lba & 0xff;
5336 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
5337 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
5338 	scmd->satacmd_features_reg = 0;
5339 	scmd->satacmd_status_reg = 0;
5340 	scmd->satacmd_error_reg = 0;
5341 
5342 	/*
5343 	 * Check if queueing commands should be used and switch
5344 	 * to appropriate command if possible
5345 	 */
5346 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
5347 		boolean_t using_queuing;
5348 
5349 		/* Queuing supported by controller and device? */
5350 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
5351 		    (sdinfo->satadrv_features_support &
5352 		    SATA_DEV_F_NCQ) &&
5353 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
5354 		    SATA_CTLF_NCQ)) {
5355 			using_queuing = B_TRUE;
5356 
5357 			/* NCQ supported - use FPDMA READ */
5358 			scmd->satacmd_cmd_reg =
5359 			    SATAC_READ_FPDMA_QUEUED;
5360 			scmd->satacmd_features_reg_ext =
5361 			    scmd->satacmd_sec_count_msb;
5362 			scmd->satacmd_sec_count_msb = 0;
5363 		} else if ((sdinfo->satadrv_features_support &
5364 		    SATA_DEV_F_TCQ) &&
5365 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
5366 		    SATA_CTLF_QCMD)) {
5367 			using_queuing = B_TRUE;
5368 
5369 			/* Legacy queueing */
5370 			if (sdinfo->satadrv_features_support &
5371 			    SATA_DEV_F_LBA48) {
5372 				scmd->satacmd_cmd_reg =
5373 				    SATAC_READ_DMA_QUEUED_EXT;
5374 				scmd->satacmd_features_reg_ext =
5375 				    scmd->satacmd_sec_count_msb;
5376 				scmd->satacmd_sec_count_msb = 0;
5377 			} else {
5378 				scmd->satacmd_cmd_reg =
5379 				    SATAC_READ_DMA_QUEUED;
5380 			}
5381 		} else	/* NCQ nor legacy queuing not supported */
5382 			using_queuing = B_FALSE;
5383 
5384 		/*
5385 		 * If queuing, the sector count goes in the features register
5386 		 * and the secount count will contain the tag.
5387 		 */
5388 		if (using_queuing) {
5389 			scmd->satacmd_features_reg =
5390 			    scmd->satacmd_sec_count_lsb;
5391 			scmd->satacmd_sec_count_lsb = 0;
5392 			scmd->satacmd_flags.sata_queued = B_TRUE;
5393 
5394 			/* Set-up maximum queue depth */
5395 			scmd->satacmd_flags.sata_max_queue_depth =
5396 			    sdinfo->satadrv_max_queue_depth - 1;
5397 		} else if (sdinfo->satadrv_features_enabled &
5398 		    SATA_DEV_F_E_UNTAGGED_QING) {
5399 			/*
5400 			 * Although NCQ/TCQ is not enabled, untagged queuing
5401 			 * may be still used.
5402 			 * Set-up the maximum untagged queue depth.
5403 			 * Use controller's queue depth from sata_hba_tran.
5404 			 * SATA HBA drivers may ignore this value and rely on
5405 			 * the internal limits.For drivers that do not
5406 			 * ignore untaged queue depth, limit the value to
5407 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
5408 			 * largest value that can be passed via
5409 			 * satacmd_flags.sata_max_queue_depth.
5410 			 */
5411 			scmd->satacmd_flags.sata_max_queue_depth =
5412 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
5413 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
5414 
5415 		} else {
5416 			scmd->satacmd_flags.sata_max_queue_depth = 0;
5417 		}
5418 	} else
5419 		scmd->satacmd_flags.sata_max_queue_depth = 0;
5420 
5421 	SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst,
5422 	    "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n",
5423 	    scmd->satacmd_cmd_reg, lba, sec_count);
5424 
5425 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5426 		/* Need callback function */
5427 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
5428 		synch = FALSE;
5429 	} else
5430 		synch = TRUE;
5431 
5432 	/* Transfer command to HBA */
5433 	if (sata_hba_start(spx, &rval) != 0) {
5434 		/* Pkt not accepted for execution */
5435 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5436 		return (rval);
5437 	}
5438 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5439 	/*
5440 	 * If execution is non-synchronous,
5441 	 * a callback function will handle potential errors, translate
5442 	 * the response and will do a callback to a target driver.
5443 	 * If it was synchronous, check execution status using the same
5444 	 * framework callback.
5445 	 */
5446 	if (synch) {
5447 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5448 		    "synchronous execution status %x\n",
5449 		    spx->txlt_sata_pkt->satapkt_reason);
5450 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
5451 	}
5452 	return (TRAN_ACCEPT);
5453 }
5454 
5455 
5456 /*
5457  * SATA translate command: Write (various types)
5458  * Translated into appropriate type of ATA WRITE command
5459  * for SATA hard disks.
5460  * Both the device capabilities and requested operation mode are
5461  * considered.
5462  *
5463  * Following scsi cdb fields are ignored:
5464  * rwprotect, dpo, fua, fua_nv, group_number.
5465  *
5466  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
5467  * enable variable sata_func_enable), the capability of the controller and
5468  * capability of a device are checked and if both support queueing, write
5469  * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT
5470  * command rather than plain WRITE_XXX command.
5471  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
5472  * both the controller and device suport such functionality, the write
5473  * request will be translated to WRITE_FPDMA_QUEUED command.
5474  * In both cases the maximum queue depth is derived as minimum of:
5475  * HBA capability,device capability and sata_max_queue_depth variable setting.
5476  * The value passed to HBA driver is decremented by 1, because only 5 bits are
5477  * used to pass max queue depth value, and the maximum possible queue depth
5478  * is 32.
5479  *
5480  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
5481  * appropriate values in scsi_pkt fields.
5482  */
5483 static int
5484 sata_txlt_write(sata_pkt_txlate_t *spx)
5485 {
5486 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5487 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5488 	sata_drive_info_t *sdinfo;
5489 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
5490 	int cport = SATA_TXLT_CPORT(spx);
5491 	uint16_t sec_count;
5492 	uint64_t lba;
5493 	int rval, reason;
5494 	int synch;
5495 
5496 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5497 
5498 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
5499 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5500 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5501 		return (rval);
5502 	}
5503 
5504 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
5505 	    &spx->txlt_sata_pkt->satapkt_device);
5506 
5507 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
5508 	/*
5509 	 * Extract LBA and sector count from scsi CDB
5510 	 */
5511 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
5512 	case SCMD_WRITE:
5513 		/* 6-byte scsi read cmd : 0x0A */
5514 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
5515 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
5516 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5517 		sec_count = scsipkt->pkt_cdbp[4];
5518 		/* sec_count 0 will be interpreted as 256 by a device */
5519 		break;
5520 	case SCMD_WRITE_G1:
5521 		/* 10-bytes scsi write command : 0x2A */
5522 		lba = scsipkt->pkt_cdbp[2];
5523 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5524 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5525 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5526 		sec_count = scsipkt->pkt_cdbp[7];
5527 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
5528 		break;
5529 	case SCMD_WRITE_G5:
5530 		/* 12-bytes scsi read command : 0xAA */
5531 		lba = scsipkt->pkt_cdbp[2];
5532 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5533 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5534 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5535 		sec_count = scsipkt->pkt_cdbp[6];
5536 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
5537 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
5538 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
5539 		break;
5540 	case SCMD_WRITE_G4:
5541 		/* 16-bytes scsi write command : 0x8A */
5542 		lba = scsipkt->pkt_cdbp[2];
5543 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5544 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5545 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5546 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
5547 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5548 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5549 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
5550 		sec_count = scsipkt->pkt_cdbp[10];
5551 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
5552 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
5553 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
5554 		break;
5555 	default:
5556 		/* Unsupported command */
5557 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5558 		return (sata_txlt_invalid_command(spx));
5559 	}
5560 
5561 	/*
5562 	 * Check if specified address and length exceeds device capacity
5563 	 */
5564 	if ((lba >= sdinfo->satadrv_capacity) ||
5565 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
5566 		/* LBA out of range */
5567 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5568 		return (sata_txlt_lba_out_of_range(spx));
5569 	}
5570 
5571 	/*
5572 	 * For zero-length transfer, emulate good completion of the command
5573 	 * (reasons for rejecting the command were already checked).
5574 	 * No DMA resources were allocated.
5575 	 */
5576 	if (spx->txlt_dma_cookie_list == NULL) {
5577 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5578 		return (sata_emul_rw_completion(spx));
5579 	}
5580 
5581 	/*
5582 	 * Build cmd block depending on the device capability and
5583 	 * requested operation mode.
5584 	 * Do not bother with non-dma mode- we are working only with
5585 	 * devices supporting DMA.
5586 	 */
5587 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
5588 	scmd->satacmd_device_reg = SATA_ADH_LBA;
5589 	scmd->satacmd_cmd_reg = SATAC_WRITE_DMA;
5590 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
5591 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
5592 		scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT;
5593 		scmd->satacmd_sec_count_msb = sec_count >> 8;
5594 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
5595 #ifndef __lock_lint
5596 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
5597 		scmd->satacmd_lba_high_msb = lba >> 40;
5598 #endif
5599 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
5600 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
5601 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
5602 	}
5603 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
5604 	scmd->satacmd_lba_low_lsb = lba & 0xff;
5605 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
5606 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
5607 	scmd->satacmd_features_reg = 0;
5608 	scmd->satacmd_status_reg = 0;
5609 	scmd->satacmd_error_reg = 0;
5610 
5611 	/*
5612 	 * Check if queueing commands should be used and switch
5613 	 * to appropriate command if possible
5614 	 */
5615 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
5616 		boolean_t using_queuing;
5617 
5618 		/* Queuing supported by controller and device? */
5619 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
5620 		    (sdinfo->satadrv_features_support &
5621 		    SATA_DEV_F_NCQ) &&
5622 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
5623 		    SATA_CTLF_NCQ)) {
5624 			using_queuing = B_TRUE;
5625 
5626 			/* NCQ supported - use FPDMA WRITE */
5627 			scmd->satacmd_cmd_reg =
5628 			    SATAC_WRITE_FPDMA_QUEUED;
5629 			scmd->satacmd_features_reg_ext =
5630 			    scmd->satacmd_sec_count_msb;
5631 			scmd->satacmd_sec_count_msb = 0;
5632 		} else if ((sdinfo->satadrv_features_support &
5633 		    SATA_DEV_F_TCQ) &&
5634 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
5635 		    SATA_CTLF_QCMD)) {
5636 			using_queuing = B_TRUE;
5637 
5638 			/* Legacy queueing */
5639 			if (sdinfo->satadrv_features_support &
5640 			    SATA_DEV_F_LBA48) {
5641 				scmd->satacmd_cmd_reg =
5642 				    SATAC_WRITE_DMA_QUEUED_EXT;
5643 				scmd->satacmd_features_reg_ext =
5644 				    scmd->satacmd_sec_count_msb;
5645 				scmd->satacmd_sec_count_msb = 0;
5646 			} else {
5647 				scmd->satacmd_cmd_reg =
5648 				    SATAC_WRITE_DMA_QUEUED;
5649 			}
5650 		} else	/*  NCQ nor legacy queuing not supported */
5651 			using_queuing = B_FALSE;
5652 
5653 		if (using_queuing) {
5654 			scmd->satacmd_features_reg =
5655 			    scmd->satacmd_sec_count_lsb;
5656 			scmd->satacmd_sec_count_lsb = 0;
5657 			scmd->satacmd_flags.sata_queued = B_TRUE;
5658 			/* Set-up maximum queue depth */
5659 			scmd->satacmd_flags.sata_max_queue_depth =
5660 			    sdinfo->satadrv_max_queue_depth - 1;
5661 		} else if (sdinfo->satadrv_features_enabled &
5662 		    SATA_DEV_F_E_UNTAGGED_QING) {
5663 			/*
5664 			 * Although NCQ/TCQ is not enabled, untagged queuing
5665 			 * may be still used.
5666 			 * Set-up the maximum untagged queue depth.
5667 			 * Use controller's queue depth from sata_hba_tran.
5668 			 * SATA HBA drivers may ignore this value and rely on
5669 			 * the internal limits. For drivera that do not
5670 			 * ignore untaged queue depth, limit the value to
5671 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
5672 			 * largest value that can be passed via
5673 			 * satacmd_flags.sata_max_queue_depth.
5674 			 */
5675 			scmd->satacmd_flags.sata_max_queue_depth =
5676 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
5677 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
5678 
5679 		} else {
5680 			scmd->satacmd_flags.sata_max_queue_depth = 0;
5681 		}
5682 	} else
5683 		scmd->satacmd_flags.sata_max_queue_depth = 0;
5684 
5685 	SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5686 	    "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n",
5687 	    scmd->satacmd_cmd_reg, lba, sec_count);
5688 
5689 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5690 		/* Need callback function */
5691 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
5692 		synch = FALSE;
5693 	} else
5694 		synch = TRUE;
5695 
5696 	/* Transfer command to HBA */
5697 	if (sata_hba_start(spx, &rval) != 0) {
5698 		/* Pkt not accepted for execution */
5699 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5700 		return (rval);
5701 	}
5702 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5703 
5704 	/*
5705 	 * If execution is non-synchronous,
5706 	 * a callback function will handle potential errors, translate
5707 	 * the response and will do a callback to a target driver.
5708 	 * If it was synchronous, check execution status using the same
5709 	 * framework callback.
5710 	 */
5711 	if (synch) {
5712 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5713 		    "synchronous execution status %x\n",
5714 		    spx->txlt_sata_pkt->satapkt_reason);
5715 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
5716 	}
5717 	return (TRAN_ACCEPT);
5718 }
5719 
5720 
5721 /*
5722  * Implements SCSI SBC WRITE BUFFER command download microcode option
5723  */
5724 static int
5725 sata_txlt_write_buffer(sata_pkt_txlate_t *spx)
5726 {
5727 #define	WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE			4
5728 #define	WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE			5
5729 
5730 	sata_hba_inst_t *sata_hba_inst = SATA_TXLT_HBA_INST(spx);
5731 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5732 	struct sata_pkt *sata_pkt = spx->txlt_sata_pkt;
5733 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5734 
5735 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5736 	struct scsi_extended_sense *sense;
5737 	int rval, mode, sector_count, reason;
5738 	int cport = SATA_TXLT_CPORT(spx);
5739 
5740 	mode = scsipkt->pkt_cdbp[1] & 0x1f;
5741 
5742 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5743 	    "sata_txlt_write_buffer, mode 0x%x\n", mode);
5744 
5745 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5746 
5747 	if ((rval = sata_txlt_generic_pkt_info(spx, &reason)) != TRAN_ACCEPT) {
5748 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5749 		return (rval);
5750 	}
5751 	/*
5752 	 * If in interrupt context, reject this packet because it would issue
5753 	 * a synchronous command to HBA.
5754 	 */
5755 	if (servicing_interrupt()) {
5756 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
5757 		    "sata_txlt_write_buffer: rejecting command because "
5758 		    "of interrupt context\n", NULL);
5759 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5760 		return (TRAN_BUSY);
5761 	}
5762 
5763 	/* Use synchronous mode */
5764 	spx->txlt_sata_pkt->satapkt_op_mode
5765 	    |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
5766 
5767 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
5768 
5769 	scsipkt->pkt_reason = CMD_CMPLT;
5770 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5771 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5772 
5773 	/*
5774 	 * The SCSI to ATA translation specification only calls
5775 	 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE.
5776 	 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but
5777 	 * ATA 8 (draft) got rid of download microcode for temp
5778 	 * and it is even optional for ATA 7, so it may be aborted.
5779 	 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as
5780 	 * it is not specified and the buffer offset for SCSI is a 16-bit
5781 	 * value in bytes, but for ATA it is a 16-bit offset in 512 byte
5782 	 * sectors.  Thus the offset really doesn't buy us anything.
5783 	 * If and when ATA 8 is stabilized and the SCSI to ATA specification
5784 	 * is revised, this can be revisisted.
5785 	 */
5786 	/* Reject not supported request */
5787 	switch (mode) {
5788 	case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE:
5789 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP;
5790 		break;
5791 	case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE:
5792 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE;
5793 		break;
5794 	default:
5795 		goto bad_param;
5796 	}
5797 
5798 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
5799 
5800 	scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE;
5801 	if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0)
5802 		goto bad_param;
5803 	sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE;
5804 	scmd->satacmd_sec_count_lsb = (uint8_t)sector_count;
5805 	scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8;
5806 	scmd->satacmd_lba_mid_lsb = 0;
5807 	scmd->satacmd_lba_high_lsb = 0;
5808 	scmd->satacmd_device_reg = 0;
5809 	spx->txlt_sata_pkt->satapkt_comp = NULL;
5810 	scmd->satacmd_addr_type = 0;
5811 
5812 	/* Transfer command to HBA */
5813 	if (sata_hba_start(spx, &rval) != 0) {
5814 		/* Pkt not accepted for execution */
5815 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
5816 		return (rval);
5817 	}
5818 
5819 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
5820 
5821 	/* Then we need synchronous check the status of the disk */
5822 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5823 	    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
5824 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5825 		scsipkt->pkt_reason = CMD_CMPLT;
5826 
5827 		/* Download commmand succeed, so probe and identify device */
5828 		sata_reidentify_device(spx);
5829 	} else {
5830 		/* Something went wrong, microcode download command failed */
5831 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5832 		*scsipkt->pkt_scbp = STATUS_CHECK;
5833 		sense = sata_arq_sense(spx);
5834 		switch (sata_pkt->satapkt_reason) {
5835 		case SATA_PKT_PORT_ERROR:
5836 			/*
5837 			 * We have no device data. Assume no data transfered.
5838 			 */
5839 			sense->es_key = KEY_HARDWARE_ERROR;
5840 			break;
5841 
5842 		case SATA_PKT_DEV_ERROR:
5843 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5844 			    SATA_STATUS_ERR) {
5845 				/*
5846 				 * determine dev error reason from error
5847 				 * reg content
5848 				 */
5849 				sata_decode_device_error(spx, sense);
5850 				break;
5851 			}
5852 			/* No extended sense key - no info available */
5853 			break;
5854 
5855 		case SATA_PKT_TIMEOUT:
5856 			scsipkt->pkt_reason = CMD_TIMEOUT;
5857 			scsipkt->pkt_statistics |=
5858 			    STAT_TIMEOUT | STAT_DEV_RESET;
5859 			/* No extended sense key ? */
5860 			break;
5861 
5862 		case SATA_PKT_ABORTED:
5863 			scsipkt->pkt_reason = CMD_ABORTED;
5864 			scsipkt->pkt_statistics |= STAT_ABORTED;
5865 			/* No extended sense key ? */
5866 			break;
5867 
5868 		case SATA_PKT_RESET:
5869 			/* pkt aborted by an explicit reset from a host */
5870 			scsipkt->pkt_reason = CMD_RESET;
5871 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5872 			break;
5873 
5874 		default:
5875 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5876 			    "sata_txlt_nodata_cmd_completion: "
5877 			    "invalid packet completion reason %d",
5878 			    sata_pkt->satapkt_reason));
5879 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5880 			break;
5881 		}
5882 
5883 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5884 		    "scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5885 
5886 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
5887 			/* scsi callback required */
5888 			scsi_hba_pkt_comp(scsipkt);
5889 	}
5890 	return (TRAN_ACCEPT);
5891 
5892 bad_param:
5893 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5894 	*scsipkt->pkt_scbp = STATUS_CHECK;
5895 	sense = sata_arq_sense(spx);
5896 	sense->es_key = KEY_ILLEGAL_REQUEST;
5897 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5898 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5899 	    scsipkt->pkt_comp != NULL) {
5900 		/* scsi callback required */
5901 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5902 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5903 		    TQ_SLEEP) == 0) {
5904 			/* Scheduling the callback failed */
5905 			rval = TRAN_BUSY;
5906 		}
5907 	}
5908 	return (rval);
5909 }
5910 
5911 /*
5912  * Re-identify device after doing a firmware download.
5913  */
5914 static void
5915 sata_reidentify_device(sata_pkt_txlate_t *spx)
5916 {
5917 #define	DOWNLOAD_WAIT_TIME_SECS	60
5918 #define	DOWNLOAD_WAIT_INTERVAL_SECS	1
5919 	int rval;
5920 	int retry_cnt;
5921 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5922 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
5923 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
5924 	sata_drive_info_t *sdinfo;
5925 
5926 	/*
5927 	 * Before returning good status, probe device.
5928 	 * Device probing will get IDENTIFY DEVICE data, if possible.
5929 	 * The assumption is that the new microcode is applied by the
5930 	 * device. It is a caller responsibility to verify this.
5931 	 */
5932 	for (retry_cnt = 0;
5933 	    retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS;
5934 	    retry_cnt++) {
5935 		rval = sata_probe_device(sata_hba_inst, &sata_device);
5936 
5937 		if (rval == SATA_SUCCESS) { /* Set default features */
5938 			sdinfo = sata_get_device_info(sata_hba_inst,
5939 			    &sata_device);
5940 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
5941 			    SATA_SUCCESS) {
5942 				/* retry */
5943 				rval = sata_initialize_device(sata_hba_inst,
5944 				    sdinfo);
5945 				if (rval == SATA_RETRY)
5946 					sata_log(sata_hba_inst, CE_WARN,
5947 					    "SATA device at port %d pmport %d -"
5948 					    " default device features could not"
5949 					    " be set. Device may not operate "
5950 					    "as expected.",
5951 					    sata_device.satadev_addr.cport,
5952 					    sata_device.satadev_addr.pmport);
5953 			}
5954 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
5955 				scsi_hba_pkt_comp(scsipkt);
5956 			return;
5957 		} else if (rval == SATA_RETRY) {
5958 			delay(drv_usectohz(1000000 *
5959 			    DOWNLOAD_WAIT_INTERVAL_SECS));
5960 			continue;
5961 		} else	/* failed - no reason to retry */
5962 			break;
5963 	}
5964 
5965 	/*
5966 	 * Something went wrong, device probing failed.
5967 	 */
5968 	SATA_LOG_D((sata_hba_inst, CE_WARN,
5969 	    "Cannot probe device after downloading microcode\n"));
5970 
5971 	/* Reset device to force retrying the probe. */
5972 	(void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst))
5973 	    (SATA_DIP(sata_hba_inst), &sata_device);
5974 
5975 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
5976 		scsi_hba_pkt_comp(scsipkt);
5977 }
5978 
5979 
5980 /*
5981  * Translate command: Synchronize Cache.
5982  * Translates into Flush Cache command for SATA hard disks.
5983  *
5984  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
5985  * appropriate values in scsi_pkt fields.
5986  */
5987 static 	int
5988 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx)
5989 {
5990 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5991 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
5992 	int cport = SATA_TXLT_CPORT(spx);
5993 	int rval, reason;
5994 	int synch;
5995 
5996 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5997 
5998 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
5999 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6000 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6001 		return (rval);
6002 	}
6003 
6004 	scmd->satacmd_addr_type = 0;
6005 	scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE;
6006 	scmd->satacmd_device_reg = 0;
6007 	scmd->satacmd_sec_count_lsb = 0;
6008 	scmd->satacmd_lba_low_lsb = 0;
6009 	scmd->satacmd_lba_mid_lsb = 0;
6010 	scmd->satacmd_lba_high_lsb = 0;
6011 	scmd->satacmd_features_reg = 0;
6012 	scmd->satacmd_status_reg = 0;
6013 	scmd->satacmd_error_reg = 0;
6014 
6015 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6016 	    "sata_txlt_synchronize_cache\n", NULL);
6017 
6018 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6019 		/* Need to set-up a callback function */
6020 		spx->txlt_sata_pkt->satapkt_comp =
6021 		    sata_txlt_nodata_cmd_completion;
6022 		synch = FALSE;
6023 	} else
6024 		synch = TRUE;
6025 
6026 	/* Transfer command to HBA */
6027 	if (sata_hba_start(spx, &rval) != 0) {
6028 		/* Pkt not accepted for execution */
6029 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
6030 		return (rval);
6031 	}
6032 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
6033 
6034 	/*
6035 	 * If execution non-synchronous, it had to be completed
6036 	 * a callback function will handle potential errors, translate
6037 	 * the response and will do a callback to a target driver.
6038 	 * If it was synchronous, check status, using the same
6039 	 * framework callback.
6040 	 */
6041 	if (synch) {
6042 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6043 		    "synchronous execution status %x\n",
6044 		    spx->txlt_sata_pkt->satapkt_reason);
6045 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
6046 	}
6047 	return (TRAN_ACCEPT);
6048 }
6049 
6050 
6051 /*
6052  * Send pkt to SATA HBA driver
6053  *
6054  * This function may be called only if the operation is requested by scsi_pkt,
6055  * i.e. scsi_pkt is not NULL.
6056  *
6057  * This function has to be called with cport mutex held. It does release
6058  * the mutex when it calls HBA driver sata_tran_start function and
6059  * re-acquires it afterwards.
6060  *
6061  * If return value is 0, pkt was accepted, -1 otherwise
6062  * rval is set to appropriate sata_scsi_start return value.
6063  *
6064  * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not
6065  * have called the sata_pkt callback function for this packet.
6066  *
6067  * The scsi callback has to be performed by the caller of this routine.
6068  */
6069 static int
6070 sata_hba_start(sata_pkt_txlate_t *spx, int *rval)
6071 {
6072 	int stat;
6073 	uint8_t cport = SATA_TXLT_CPORT(spx);
6074 	uint8_t pmport = SATA_TXLT_PMPORT(spx);
6075 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
6076 	sata_drive_info_t *sdinfo;
6077 	sata_pmult_info_t *pminfo;
6078 	sata_pmport_info_t *pmportinfo = NULL;
6079 	sata_device_t *sata_device = NULL;
6080 	uint8_t cmd;
6081 	struct sata_cmd_flags cmd_flags;
6082 
6083 	ASSERT(spx->txlt_sata_pkt != NULL);
6084 
6085 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6086 
6087 	sdinfo = sata_get_device_info(sata_hba_inst,
6088 	    &spx->txlt_sata_pkt->satapkt_device);
6089 	ASSERT(sdinfo != NULL);
6090 
6091 	/* Clear device reset state? */
6092 	/* qual should be XXX_DPMPORT, but add XXX_PMPORT in case */
6093 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT ||
6094 	    sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT) {
6095 
6096 		/*
6097 		 * Get the pmult_info of the its parent port multiplier, all
6098 		 * sub-devices share a common device reset flags on in
6099 		 * pmult_info.
6100 		 */
6101 		pminfo = SATA_PMULT_INFO(sata_hba_inst, cport);
6102 		pmportinfo = pminfo->pmult_dev_port[pmport];
6103 		ASSERT(pminfo != NULL);
6104 		if (pminfo->pmult_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) {
6105 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
6106 			    sata_clear_dev_reset = B_TRUE;
6107 			pminfo->pmult_event_flags &=
6108 			    ~SATA_EVNT_CLEAR_DEVICE_RESET;
6109 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6110 			    "sata_hba_start: clearing device reset state"
6111 			    "on pmult.\n", NULL);
6112 		}
6113 	} else {
6114 		if (sdinfo->satadrv_event_flags &
6115 		    SATA_EVNT_CLEAR_DEVICE_RESET) {
6116 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
6117 			    sata_clear_dev_reset = B_TRUE;
6118 			sdinfo->satadrv_event_flags &=
6119 			    ~SATA_EVNT_CLEAR_DEVICE_RESET;
6120 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6121 			    "sata_hba_start: clearing device reset state\n",
6122 			    NULL);
6123 		}
6124 	}
6125 
6126 	cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg;
6127 	cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags;
6128 	sata_device = &spx->txlt_sata_pkt->satapkt_device;
6129 
6130 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6131 
6132 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6133 	    "Sata cmd 0x%2x\n", cmd);
6134 
6135 	stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
6136 	    spx->txlt_sata_pkt);
6137 
6138 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6139 	/*
6140 	 * If sata pkt was accepted and executed in asynchronous mode, i.e.
6141 	 * with the sata callback, the sata_pkt could be already destroyed
6142 	 * by the time we check ther return status from the hba_start()
6143 	 * function, because sata_scsi_destroy_pkt() could have been already
6144 	 * called (perhaps in the interrupt context). So, in such case, there
6145 	 * should be no references to it. In other cases, sata_pkt still
6146 	 * exists.
6147 	 */
6148 	if (stat == SATA_TRAN_ACCEPTED) {
6149 		/*
6150 		 * pkt accepted for execution.
6151 		 * If it was executed synchronously, it is already completed
6152 		 * and pkt completion_reason indicates completion status.
6153 		 */
6154 		*rval = TRAN_ACCEPT;
6155 		return (0);
6156 	}
6157 
6158 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
6159 	switch (stat) {
6160 	case SATA_TRAN_QUEUE_FULL:
6161 		/*
6162 		 * Controller detected queue full condition.
6163 		 */
6164 		SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst,
6165 		    "sata_hba_start: queue full\n", NULL);
6166 
6167 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
6168 		*spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL;
6169 
6170 		*rval = TRAN_BUSY;
6171 		break;
6172 
6173 	case SATA_TRAN_PORT_ERROR:
6174 		/*
6175 		 * Communication/link with device or general port error
6176 		 * detected before pkt execution begun.
6177 		 */
6178 		if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
6179 		    SATA_ADDR_CPORT ||
6180 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
6181 		    SATA_ADDR_DCPORT)
6182 			sata_log(sata_hba_inst, CE_CONT,
6183 			    "SATA port %d error",
6184 			    sata_device->satadev_addr.cport);
6185 		else
6186 			sata_log(sata_hba_inst, CE_CONT,
6187 			    "SATA port %d:%d error\n",
6188 			    sata_device->satadev_addr.cport,
6189 			    sata_device->satadev_addr.pmport);
6190 
6191 		/*
6192 		 * Update the port/device structure.
6193 		 * sata_pkt should be still valid. Since port error is
6194 		 * returned, sata_device content should reflect port
6195 		 * state - it means, that sata address have been changed,
6196 		 * because original packet's sata address refered to a device
6197 		 * attached to some port.
6198 		 */
6199 		if (sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT ||
6200 		    sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT) {
6201 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6202 			mutex_enter(&pmportinfo->pmport_mutex);
6203 			sata_update_pmport_info(sata_hba_inst, sata_device);
6204 			mutex_exit(&pmportinfo->pmport_mutex);
6205 			mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6206 		} else {
6207 			sata_update_port_info(sata_hba_inst, sata_device);
6208 		}
6209 
6210 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
6211 		*rval = TRAN_FATAL_ERROR;
6212 		break;
6213 
6214 	case SATA_TRAN_CMD_UNSUPPORTED:
6215 		/*
6216 		 * Command rejected by HBA as unsupported. It was HBA driver
6217 		 * that rejected the command, command was not sent to
6218 		 * an attached device.
6219 		 */
6220 		if ((sdinfo != NULL) &&
6221 		    (sdinfo->satadrv_state & SATA_DSTATE_RESET))
6222 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6223 			    "sat_hba_start: cmd 0x%2x rejected "
6224 			    "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd);
6225 
6226 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6227 		(void) sata_txlt_invalid_command(spx);
6228 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6229 
6230 		*rval = TRAN_ACCEPT;
6231 		break;
6232 
6233 	case SATA_TRAN_BUSY:
6234 		/*
6235 		 * Command rejected by HBA because other operation prevents
6236 		 * accepting the packet, or device is in RESET condition.
6237 		 */
6238 		if (sdinfo != NULL) {
6239 			sdinfo->satadrv_state =
6240 			    spx->txlt_sata_pkt->satapkt_device.satadev_state;
6241 
6242 			if (sdinfo->satadrv_state & SATA_DSTATE_RESET) {
6243 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6244 				    "sata_hba_start: cmd 0x%2x rejected "
6245 				    "because of device reset condition\n",
6246 				    cmd);
6247 			} else {
6248 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6249 				    "sata_hba_start: cmd 0x%2x rejected "
6250 				    "with SATA_TRAN_BUSY status\n",
6251 				    cmd);
6252 			}
6253 		}
6254 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
6255 		*rval = TRAN_BUSY;
6256 		break;
6257 
6258 	default:
6259 		/* Unrecognized HBA response */
6260 		SATA_LOG_D((sata_hba_inst, CE_WARN,
6261 		    "sata_hba_start: unrecognized HBA response "
6262 		    "to cmd : 0x%2x resp 0x%x", cmd, rval));
6263 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
6264 		*rval = TRAN_FATAL_ERROR;
6265 		break;
6266 	}
6267 
6268 	/*
6269 	 * If we got here, the packet was rejected.
6270 	 * Check if we need to remember reset state clearing request
6271 	 */
6272 	if (cmd_flags.sata_clear_dev_reset) {
6273 		/*
6274 		 * Check if device is still configured - it may have
6275 		 * disapeared from the configuration
6276 		 */
6277 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
6278 		if (sdinfo != NULL) {
6279 			/*
6280 			 * Restore the flag that requests clearing of
6281 			 * the device reset state,
6282 			 * so the next sata packet may carry it to HBA.
6283 			 */
6284 			if (sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT ||
6285 			    sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT) {
6286 				pminfo->pmult_event_flags |=
6287 				    SATA_EVNT_CLEAR_DEVICE_RESET;
6288 			} else {
6289 				sdinfo->satadrv_event_flags |=
6290 				    SATA_EVNT_CLEAR_DEVICE_RESET;
6291 			}
6292 		}
6293 	}
6294 	return (-1);
6295 }
6296 
6297 /*
6298  * Scsi response setup for invalid LBA
6299  *
6300  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
6301  */
6302 static int
6303 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx)
6304 {
6305 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6306 	struct scsi_extended_sense *sense;
6307 
6308 	scsipkt->pkt_reason = CMD_CMPLT;
6309 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6310 	    STATE_SENT_CMD | STATE_GOT_STATUS;
6311 	*scsipkt->pkt_scbp = STATUS_CHECK;
6312 
6313 	*scsipkt->pkt_scbp = STATUS_CHECK;
6314 	sense = sata_arq_sense(spx);
6315 	sense->es_key = KEY_ILLEGAL_REQUEST;
6316 	sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
6317 
6318 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6319 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6320 
6321 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6322 	    scsipkt->pkt_comp != NULL)
6323 		/* scsi callback required */
6324 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6325 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
6326 		    TQ_SLEEP) == NULL)
6327 			/* Scheduling the callback failed */
6328 			return (TRAN_BUSY);
6329 	return (TRAN_ACCEPT);
6330 }
6331 
6332 
6333 /*
6334  * Analyze device status and error registers and translate them into
6335  * appropriate scsi sense codes.
6336  * NOTE: non-packet commands only for now
6337  */
6338 static void
6339 sata_decode_device_error(sata_pkt_txlate_t *spx,
6340     struct scsi_extended_sense *sense)
6341 {
6342 	uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg;
6343 
6344 	ASSERT(sense != NULL);
6345 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
6346 	    SATA_STATUS_ERR);
6347 
6348 
6349 	if (err_reg & SATA_ERROR_ICRC) {
6350 		sense->es_key = KEY_ABORTED_COMMAND;
6351 		sense->es_add_code = 0x08; /* Communication failure */
6352 		return;
6353 	}
6354 
6355 	if (err_reg & SATA_ERROR_UNC) {
6356 		sense->es_key = KEY_MEDIUM_ERROR;
6357 		/* Information bytes (LBA) need to be set by a caller */
6358 		return;
6359 	}
6360 
6361 	/* ADD HERE: MC error bit handling for ATAPI CD/DVD */
6362 	if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) {
6363 		sense->es_key = KEY_UNIT_ATTENTION;
6364 		sense->es_add_code = 0x3a; /* No media present */
6365 		return;
6366 	}
6367 
6368 	if (err_reg & SATA_ERROR_IDNF) {
6369 		if (err_reg & SATA_ERROR_ABORT) {
6370 			sense->es_key = KEY_ABORTED_COMMAND;
6371 		} else {
6372 			sense->es_key = KEY_ILLEGAL_REQUEST;
6373 			sense->es_add_code = 0x21; /* LBA out of range */
6374 		}
6375 		return;
6376 	}
6377 
6378 	if (err_reg & SATA_ERROR_ABORT) {
6379 		ASSERT(spx->txlt_sata_pkt != NULL);
6380 		sense->es_key = KEY_ABORTED_COMMAND;
6381 		return;
6382 	}
6383 }
6384 
6385 /*
6386  * Extract error LBA from sata_pkt.satapkt_cmd register fields
6387  */
6388 static void
6389 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba)
6390 {
6391 	sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd;
6392 
6393 	*lba = 0;
6394 	if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) {
6395 		*lba = sata_cmd->satacmd_lba_high_msb;
6396 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb;
6397 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb;
6398 	} else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) {
6399 		*lba = sata_cmd->satacmd_device_reg & 0xf;
6400 	}
6401 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb;
6402 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb;
6403 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb;
6404 }
6405 
6406 /*
6407  * This is fixed sense format - if LBA exceeds the info field size,
6408  * no valid info will be returned (valid bit in extended sense will
6409  * be set to 0).
6410  */
6411 static struct scsi_extended_sense *
6412 sata_arq_sense(sata_pkt_txlate_t *spx)
6413 {
6414 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6415 	struct scsi_arq_status *arqs;
6416 	struct scsi_extended_sense *sense;
6417 
6418 	/* Fill ARQ sense data */
6419 	scsipkt->pkt_state |= STATE_ARQ_DONE;
6420 	arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp;
6421 	*(uchar_t *)&arqs->sts_status = STATUS_CHECK;
6422 	*(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD;
6423 	arqs->sts_rqpkt_reason = CMD_CMPLT;
6424 	arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6425 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
6426 	arqs->sts_rqpkt_resid = 0;
6427 	sense = &arqs->sts_sensedata;
6428 	bzero(sense, sizeof (struct scsi_extended_sense));
6429 	sata_fixed_sense_data_preset(sense);
6430 	return (sense);
6431 }
6432 
6433 
6434 /*
6435  * Emulated SATA Read/Write command completion for zero-length requests.
6436  * This request always succedes, so in synchronous mode it always returns
6437  * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the
6438  * callback cannot be scheduled.
6439  */
6440 static int
6441 sata_emul_rw_completion(sata_pkt_txlate_t *spx)
6442 {
6443 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6444 
6445 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6446 	    STATE_SENT_CMD | STATE_GOT_STATUS;
6447 	scsipkt->pkt_reason = CMD_CMPLT;
6448 	*scsipkt->pkt_scbp = STATUS_GOOD;
6449 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6450 		/* scsi callback required - have to schedule it */
6451 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6452 		    (task_func_t *)scsipkt->pkt_comp,
6453 		    (void *)scsipkt, TQ_SLEEP) == NULL)
6454 			/* Scheduling the callback failed */
6455 			return (TRAN_BUSY);
6456 	}
6457 	return (TRAN_ACCEPT);
6458 }
6459 
6460 
6461 /*
6462  * Translate completion status of SATA read/write commands into scsi response.
6463  * pkt completion_reason is checked to determine the completion status.
6464  * Do scsi callback if necessary.
6465  *
6466  * Note: this function may be called also for synchronously executed
6467  * commands.
6468  * This function may be used only if scsi_pkt is non-NULL.
6469  */
6470 static void
6471 sata_txlt_rw_completion(sata_pkt_t *sata_pkt)
6472 {
6473 	sata_pkt_txlate_t *spx =
6474 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
6475 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
6476 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6477 	struct scsi_extended_sense *sense;
6478 	uint64_t lba;
6479 	struct buf *bp;
6480 	int rval;
6481 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
6482 		/* Normal completion */
6483 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6484 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
6485 		scsipkt->pkt_reason = CMD_CMPLT;
6486 		*scsipkt->pkt_scbp = STATUS_GOOD;
6487 		if (spx->txlt_tmp_buf != NULL) {
6488 			/* Temporary buffer was used */
6489 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
6490 			if (bp->b_flags & B_READ) {
6491 				rval = ddi_dma_sync(
6492 				    spx->txlt_buf_dma_handle, 0, 0,
6493 				    DDI_DMA_SYNC_FORCPU);
6494 				ASSERT(rval == DDI_SUCCESS);
6495 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
6496 				    bp->b_bcount);
6497 			}
6498 		}
6499 	} else {
6500 		/*
6501 		 * Something went wrong - analyze return
6502 		 */
6503 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6504 		    STATE_SENT_CMD | STATE_GOT_STATUS;
6505 		scsipkt->pkt_reason = CMD_INCOMPLETE;
6506 		*scsipkt->pkt_scbp = STATUS_CHECK;
6507 		sense = sata_arq_sense(spx);
6508 		ASSERT(sense != NULL);
6509 
6510 		/*
6511 		 * SATA_PKT_DEV_ERROR is the only case where we may be able to
6512 		 * extract from device registers the failing LBA.
6513 		 */
6514 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
6515 			if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) &&
6516 			    (scmd->satacmd_lba_mid_msb != 0 ||
6517 			    scmd->satacmd_lba_high_msb != 0)) {
6518 				/*
6519 				 * We have problem reporting this cmd LBA
6520 				 * in fixed sense data format, because of
6521 				 * the size of the scsi LBA fields.
6522 				 */
6523 				sense->es_valid = 0;
6524 			} else {
6525 				sata_extract_error_lba(spx, &lba);
6526 				sense->es_info_1 = (lba & 0xFF000000) >> 24;
6527 				sense->es_info_2 = (lba & 0xFF0000) >> 16;
6528 				sense->es_info_3 = (lba & 0xFF00) >> 8;
6529 				sense->es_info_4 = lba & 0xFF;
6530 			}
6531 		} else {
6532 			/* Invalid extended sense info */
6533 			sense->es_valid = 0;
6534 		}
6535 
6536 		switch (sata_pkt->satapkt_reason) {
6537 		case SATA_PKT_PORT_ERROR:
6538 			/* We may want to handle DEV GONE state as well */
6539 			/*
6540 			 * We have no device data. Assume no data transfered.
6541 			 */
6542 			sense->es_key = KEY_HARDWARE_ERROR;
6543 			break;
6544 
6545 		case SATA_PKT_DEV_ERROR:
6546 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
6547 			    SATA_STATUS_ERR) {
6548 				/*
6549 				 * determine dev error reason from error
6550 				 * reg content
6551 				 */
6552 				sata_decode_device_error(spx, sense);
6553 				if (sense->es_key == KEY_MEDIUM_ERROR) {
6554 					switch (scmd->satacmd_cmd_reg) {
6555 					case SATAC_READ_DMA:
6556 					case SATAC_READ_DMA_EXT:
6557 					case SATAC_READ_DMA_QUEUED:
6558 					case SATAC_READ_DMA_QUEUED_EXT:
6559 					case SATAC_READ_FPDMA_QUEUED:
6560 						/* Unrecovered read error */
6561 						sense->es_add_code =
6562 						    SD_SCSI_ASC_UNREC_READ_ERR;
6563 						break;
6564 					case SATAC_WRITE_DMA:
6565 					case SATAC_WRITE_DMA_EXT:
6566 					case SATAC_WRITE_DMA_QUEUED:
6567 					case SATAC_WRITE_DMA_QUEUED_EXT:
6568 					case SATAC_WRITE_FPDMA_QUEUED:
6569 						/* Write error */
6570 						sense->es_add_code =
6571 						    SD_SCSI_ASC_WRITE_ERR;
6572 						break;
6573 					default:
6574 						/* Internal error */
6575 						SATA_LOG_D((
6576 						    spx->txlt_sata_hba_inst,
6577 						    CE_WARN,
6578 						    "sata_txlt_rw_completion :"
6579 						    "internal error - invalid "
6580 						    "command 0x%2x",
6581 						    scmd->satacmd_cmd_reg));
6582 						break;
6583 					}
6584 				}
6585 				break;
6586 			}
6587 			/* No extended sense key - no info available */
6588 			scsipkt->pkt_reason = CMD_INCOMPLETE;
6589 			break;
6590 
6591 		case SATA_PKT_TIMEOUT:
6592 			scsipkt->pkt_reason = CMD_TIMEOUT;
6593 			scsipkt->pkt_statistics |=
6594 			    STAT_TIMEOUT | STAT_DEV_RESET;
6595 			sense->es_key = KEY_ABORTED_COMMAND;
6596 			break;
6597 
6598 		case SATA_PKT_ABORTED:
6599 			scsipkt->pkt_reason = CMD_ABORTED;
6600 			scsipkt->pkt_statistics |= STAT_ABORTED;
6601 			sense->es_key = KEY_ABORTED_COMMAND;
6602 			break;
6603 
6604 		case SATA_PKT_RESET:
6605 			scsipkt->pkt_reason = CMD_RESET;
6606 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
6607 			sense->es_key = KEY_ABORTED_COMMAND;
6608 			break;
6609 
6610 		default:
6611 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
6612 			    "sata_txlt_rw_completion: "
6613 			    "invalid packet completion reason"));
6614 			scsipkt->pkt_reason = CMD_TRAN_ERR;
6615 			break;
6616 		}
6617 	}
6618 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6619 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6620 
6621 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6622 		/* scsi callback required */
6623 		scsi_hba_pkt_comp(scsipkt);
6624 }
6625 
6626 
6627 /*
6628  * Translate completion status of non-data commands (i.e. commands returning
6629  * no data).
6630  * pkt completion_reason is checked to determine the completion status.
6631  * Do scsi callback if necessary (FLAG_NOINTR == 0)
6632  *
6633  * Note: this function may be called also for synchronously executed
6634  * commands.
6635  * This function may be used only if scsi_pkt is non-NULL.
6636  */
6637 
6638 static	void
6639 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt)
6640 {
6641 	sata_pkt_txlate_t *spx =
6642 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
6643 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6644 
6645 	sata_set_arq_data(sata_pkt);
6646 
6647 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6648 		/* scsi callback required */
6649 		scsi_hba_pkt_comp(scsipkt);
6650 }
6651 
6652 static	void
6653 sata_set_arq_data(sata_pkt_t *sata_pkt)
6654 {
6655 	sata_pkt_txlate_t *spx =
6656 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
6657 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6658 	struct scsi_extended_sense *sense;
6659 
6660 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6661 	    STATE_SENT_CMD | STATE_GOT_STATUS;
6662 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
6663 		/* Normal completion */
6664 		scsipkt->pkt_reason = CMD_CMPLT;
6665 		*scsipkt->pkt_scbp = STATUS_GOOD;
6666 	} else {
6667 		/* Something went wrong */
6668 		scsipkt->pkt_reason = CMD_INCOMPLETE;
6669 		*scsipkt->pkt_scbp = STATUS_CHECK;
6670 		sense = sata_arq_sense(spx);
6671 		switch (sata_pkt->satapkt_reason) {
6672 		case SATA_PKT_PORT_ERROR:
6673 			/*
6674 			 * We have no device data. Assume no data transfered.
6675 			 */
6676 			sense->es_key = KEY_HARDWARE_ERROR;
6677 			break;
6678 
6679 		case SATA_PKT_DEV_ERROR:
6680 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
6681 			    SATA_STATUS_ERR) {
6682 				/*
6683 				 * determine dev error reason from error
6684 				 * reg content
6685 				 */
6686 				sata_decode_device_error(spx, sense);
6687 				break;
6688 			}
6689 			/* No extended sense key - no info available */
6690 			break;
6691 
6692 		case SATA_PKT_TIMEOUT:
6693 			scsipkt->pkt_reason = CMD_TIMEOUT;
6694 			scsipkt->pkt_statistics |=
6695 			    STAT_TIMEOUT | STAT_DEV_RESET;
6696 			/* No extended sense key ? */
6697 			break;
6698 
6699 		case SATA_PKT_ABORTED:
6700 			scsipkt->pkt_reason = CMD_ABORTED;
6701 			scsipkt->pkt_statistics |= STAT_ABORTED;
6702 			/* No extended sense key ? */
6703 			break;
6704 
6705 		case SATA_PKT_RESET:
6706 			/* pkt aborted by an explicit reset from a host */
6707 			scsipkt->pkt_reason = CMD_RESET;
6708 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
6709 			break;
6710 
6711 		default:
6712 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
6713 			    "sata_txlt_nodata_cmd_completion: "
6714 			    "invalid packet completion reason %d",
6715 			    sata_pkt->satapkt_reason));
6716 			scsipkt->pkt_reason = CMD_TRAN_ERR;
6717 			break;
6718 		}
6719 
6720 	}
6721 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6722 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6723 }
6724 
6725 
6726 /*
6727  * Build Mode sense R/W recovery page
6728  * NOT IMPLEMENTED
6729  */
6730 
6731 static int
6732 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6733 {
6734 #ifndef __lock_lint
6735 	_NOTE(ARGUNUSED(sdinfo))
6736 	_NOTE(ARGUNUSED(pcntrl))
6737 	_NOTE(ARGUNUSED(buf))
6738 #endif
6739 	return (0);
6740 }
6741 
6742 /*
6743  * Build Mode sense caching page  -  scsi-3 implementation.
6744  * Page length distinguishes previous format from scsi-3 format.
6745  * buf must have space for 0x12 bytes.
6746  * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable.
6747  *
6748  */
6749 static int
6750 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6751 {
6752 	struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf;
6753 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6754 
6755 	/*
6756 	 * Most of the fields are set to 0, being not supported and/or disabled
6757 	 */
6758 	bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3);
6759 
6760 	/* Saved paramters not supported */
6761 	if (pcntrl == 3)
6762 		return (0);
6763 	if (pcntrl == 0 || pcntrl == 2) {
6764 		/*
6765 		 * For now treat current and default parameters as same
6766 		 * That may have to change, if target driver will complain
6767 		 */
6768 		page->mode_page.code = MODEPAGE_CACHING;	/* PS = 0 */
6769 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
6770 
6771 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id) &&
6772 		    !SATA_READ_AHEAD_ENABLED(*sata_id)) {
6773 			page->dra = 1;		/* Read Ahead disabled */
6774 			page->rcd = 1;		/* Read Cache disabled */
6775 		}
6776 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id) &&
6777 		    SATA_WRITE_CACHE_ENABLED(*sata_id))
6778 			page->wce = 1;		/* Write Cache enabled */
6779 	} else {
6780 		/* Changeable parameters */
6781 		page->mode_page.code = MODEPAGE_CACHING;
6782 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
6783 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
6784 			page->dra = 1;
6785 			page->rcd = 1;
6786 		}
6787 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id))
6788 			page->wce = 1;
6789 	}
6790 	return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
6791 	    sizeof (struct mode_page));
6792 }
6793 
6794 /*
6795  * Build Mode sense exception cntrl page
6796  */
6797 static int
6798 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6799 {
6800 	struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf;
6801 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6802 
6803 	/*
6804 	 * Most of the fields are set to 0, being not supported and/or disabled
6805 	 */
6806 	bzero(buf, PAGELENGTH_INFO_EXCPT);
6807 
6808 	page->mode_page.code = MODEPAGE_INFO_EXCPT;
6809 	page->mode_page.length = PAGELENGTH_INFO_EXCPT;
6810 
6811 	/* Indicate that this is page is saveable */
6812 	page->mode_page.ps = 1;
6813 
6814 	/*
6815 	 * We will return the same data for default, current and saved page.
6816 	 * The only changeable bit is dexcpt and that bit is required
6817 	 * by the ATA specification to be preserved across power cycles.
6818 	 */
6819 	if (pcntrl != 1) {
6820 		page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED);
6821 		page->mrie = MRIE_ONLY_ON_REQUEST;
6822 	}
6823 	else
6824 		page->dexcpt = 1;	/* Only changeable parameter */
6825 
6826 	return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page));
6827 }
6828 
6829 
6830 static int
6831 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6832 {
6833 	struct mode_acoustic_management *page =
6834 	    (struct mode_acoustic_management *)buf;
6835 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6836 
6837 	/*
6838 	 * Most of the fields are set to 0, being not supported and/or disabled
6839 	 */
6840 	bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT);
6841 
6842 	switch (pcntrl) {
6843 	case P_CNTRL_DEFAULT:
6844 		/*  default paramters not supported */
6845 		return (0);
6846 
6847 	case P_CNTRL_CURRENT:
6848 	case P_CNTRL_SAVED:
6849 		/* Saved and current are supported and are identical */
6850 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
6851 		page->mode_page.length =
6852 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
6853 		page->mode_page.ps = 1;
6854 
6855 		/* Word 83 indicates if feature is supported */
6856 		/* If feature is not supported */
6857 		if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) {
6858 			page->acoustic_manag_enable =
6859 			    ACOUSTIC_DISABLED;
6860 		} else {
6861 			page->acoustic_manag_enable =
6862 			    ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT)
6863 			    != 0);
6864 			/* Word 94 inidicates the value */
6865 #ifdef	_LITTLE_ENDIAN
6866 			page->acoustic_manag_level =
6867 			    (uchar_t)sata_id->ai_acoustic;
6868 			page->vendor_recommended_value =
6869 			    sata_id->ai_acoustic >> 8;
6870 #else
6871 			page->acoustic_manag_level =
6872 			    sata_id->ai_acoustic >> 8;
6873 			page->vendor_recommended_value =
6874 			    (uchar_t)sata_id->ai_acoustic;
6875 #endif
6876 		}
6877 		break;
6878 
6879 	case P_CNTRL_CHANGEABLE:
6880 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
6881 		page->mode_page.length =
6882 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
6883 		page->mode_page.ps = 1;
6884 
6885 		/* Word 83 indicates if the feature is supported */
6886 		if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) {
6887 			page->acoustic_manag_enable =
6888 			    ACOUSTIC_ENABLED;
6889 			page->acoustic_manag_level = 0xff;
6890 		}
6891 		break;
6892 	}
6893 	return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6894 	    sizeof (struct mode_page));
6895 }
6896 
6897 
6898 /*
6899  * Build Mode sense power condition page.
6900  */
6901 static int
6902 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6903 {
6904 	struct mode_info_power_cond *page = (struct mode_info_power_cond *)buf;
6905 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6906 
6907 	/*
6908 	 * Most of the fields are set to 0, being not supported and/or disabled
6909 	 * power condition page length was 0x0a
6910 	 */
6911 	bzero(buf, sizeof (struct mode_info_power_cond));
6912 
6913 	if (pcntrl == P_CNTRL_DEFAULT) {
6914 		/*  default paramters not supported */
6915 		return (0);
6916 	}
6917 
6918 	page->mode_page.code = MODEPAGE_POWER_COND;
6919 	page->mode_page.length = sizeof (struct mode_info_power_cond);
6920 
6921 	if (sata_id->ai_cap && SATA_STANDBYTIMER) {
6922 		page->standby = 1;
6923 		bcopy(sdinfo->satadrv_standby_timer, page->standby_cond_timer,
6924 		    sizeof (uchar_t) * 4);
6925 	}
6926 
6927 	return (sizeof (struct mode_info_power_cond));
6928 }
6929 
6930 /*
6931  * Process mode select caching page 8 (scsi3 format only).
6932  * Read Ahead (same as read cache) and Write Cache may be turned on and off
6933  * if these features are supported by the device. If these features are not
6934  * supported, the command will be terminated with STATUS_CHECK.
6935  * This function fails only if the SET FEATURE command sent to
6936  * the device fails. The page format is not varified, assuming that the
6937  * target driver operates correctly - if parameters length is too short,
6938  * we just drop the page.
6939  * Two command may be sent if both Read Cache/Read Ahead and Write Cache
6940  * setting have to be changed.
6941  * SET FEATURE command is executed synchronously, i.e. we wait here until
6942  * it is completed, regardless of the scsi pkt directives.
6943  *
6944  * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e.
6945  * changing DRA will change RCD.
6946  *
6947  * More than one SATA command may be executed to perform operations specified
6948  * by mode select pages. The first error terminates further execution.
6949  * Operations performed successully are not backed-up in such case.
6950  *
6951  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
6952  * If operation resulted in changing device setup, dmod flag should be set to
6953  * one (1). If parameters were not changed, dmod flag should be set to 0.
6954  * Upon return, if operation required sending command to the device, the rval
6955  * should be set to the value returned by sata_hba_start. If operation
6956  * did not require device access, rval should be set to TRAN_ACCEPT.
6957  * The pagelen should be set to the length of the page.
6958  *
6959  * This function has to be called with a port mutex held.
6960  *
6961  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6962  */
6963 int
6964 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page,
6965     int parmlen, int *pagelen, int *rval, int *dmod)
6966 {
6967 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6968 	sata_drive_info_t *sdinfo;
6969 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6970 	sata_id_t *sata_id;
6971 	struct scsi_extended_sense *sense;
6972 	int wce, dra;	/* Current settings */
6973 
6974 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6975 	    &spx->txlt_sata_pkt->satapkt_device);
6976 	sata_id = &sdinfo->satadrv_id;
6977 	*dmod = 0;
6978 
6979 	/* Verify parameters length. If too short, drop it */
6980 	if ((PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
6981 	    sizeof (struct mode_page)) > parmlen) {
6982 		*scsipkt->pkt_scbp = STATUS_CHECK;
6983 		sense = sata_arq_sense(spx);
6984 		sense->es_key = KEY_ILLEGAL_REQUEST;
6985 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6986 		*pagelen = parmlen;
6987 		*rval = TRAN_ACCEPT;
6988 		return (SATA_FAILURE);
6989 	}
6990 
6991 	*pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page);
6992 
6993 	/* Current setting of Read Ahead (and Read Cache) */
6994 	if (SATA_READ_AHEAD_ENABLED(*sata_id))
6995 		dra = 0;	/* 0 == not disabled */
6996 	else
6997 		dra = 1;
6998 	/* Current setting of Write Cache */
6999 	if (SATA_WRITE_CACHE_ENABLED(*sata_id))
7000 		wce = 1;
7001 	else
7002 		wce = 0;
7003 
7004 	if (page->dra == dra && page->wce == wce && page->rcd == dra) {
7005 		/* nothing to do */
7006 		*rval = TRAN_ACCEPT;
7007 		return (SATA_SUCCESS);
7008 	}
7009 
7010 	/*
7011 	 * Need to flip some setting
7012 	 * Set-up Internal SET FEATURES command(s)
7013 	 */
7014 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
7015 	scmd->satacmd_addr_type = 0;
7016 	scmd->satacmd_device_reg = 0;
7017 	scmd->satacmd_status_reg = 0;
7018 	scmd->satacmd_error_reg = 0;
7019 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
7020 	if (page->dra != dra || page->rcd != dra) {
7021 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
7022 			/* Need to flip read ahead setting */
7023 			if (dra == 0)
7024 				/* Disable read ahead / read cache */
7025 				scmd->satacmd_features_reg =
7026 				    SATAC_SF_DISABLE_READ_AHEAD;
7027 			else
7028 				/* Enable read ahead  / read cache */
7029 				scmd->satacmd_features_reg =
7030 				    SATAC_SF_ENABLE_READ_AHEAD;
7031 
7032 			/* Transfer command to HBA */
7033 			if (sata_hba_start(spx, rval) != 0)
7034 				/*
7035 				 * Pkt not accepted for execution.
7036 				 */
7037 				return (SATA_FAILURE);
7038 
7039 			*dmod = 1;
7040 
7041 			/* Now process return */
7042 			if (spx->txlt_sata_pkt->satapkt_reason !=
7043 			    SATA_PKT_COMPLETED) {
7044 				goto failure;	/* Terminate */
7045 			}
7046 		} else {
7047 			*scsipkt->pkt_scbp = STATUS_CHECK;
7048 			sense = sata_arq_sense(spx);
7049 			sense->es_key = KEY_ILLEGAL_REQUEST;
7050 			sense->es_add_code =
7051 			    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
7052 			*pagelen = parmlen;
7053 			*rval = TRAN_ACCEPT;
7054 			return (SATA_FAILURE);
7055 		}
7056 	}
7057 
7058 	/* Note that the packet is not removed, so it could be re-used */
7059 	if (page->wce != wce) {
7060 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id)) {
7061 			/* Need to flip Write Cache setting */
7062 			if (page->wce == 1)
7063 				/* Enable write cache */
7064 				scmd->satacmd_features_reg =
7065 				    SATAC_SF_ENABLE_WRITE_CACHE;
7066 			else
7067 				/* Disable write cache */
7068 				scmd->satacmd_features_reg =
7069 				    SATAC_SF_DISABLE_WRITE_CACHE;
7070 
7071 			/* Transfer command to HBA */
7072 			if (sata_hba_start(spx, rval) != 0)
7073 				/*
7074 				 * Pkt not accepted for execution.
7075 				 */
7076 				return (SATA_FAILURE);
7077 
7078 			*dmod = 1;
7079 
7080 			/* Now process return */
7081 			if (spx->txlt_sata_pkt->satapkt_reason !=
7082 			    SATA_PKT_COMPLETED) {
7083 				goto failure;
7084 			}
7085 		} else {
7086 			*scsipkt->pkt_scbp = STATUS_CHECK;
7087 			sense = sata_arq_sense(spx);
7088 			sense->es_key = KEY_ILLEGAL_REQUEST;
7089 			sense->es_add_code =
7090 			    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
7091 			*pagelen = parmlen;
7092 			*rval = TRAN_ACCEPT;
7093 			return (SATA_FAILURE);
7094 		}
7095 	}
7096 	return (SATA_SUCCESS);
7097 
7098 failure:
7099 	sata_xlate_errors(spx);
7100 
7101 	return (SATA_FAILURE);
7102 }
7103 
7104 /*
7105  * Process mode select informational exceptions control page 0x1c
7106  *
7107  * The only changeable bit is dexcpt (disable exceptions).
7108  * MRIE (method of reporting informational exceptions) must be
7109  * "only on request".
7110  * This page applies to informational exceptions that report
7111  * additional sense codes with the ADDITIONAL SENSE CODE field set to 5Dh
7112  * (e.g.,FAILURE PREDICTION THRESHOLD EXCEEDED) or 0Bh (e.g., WARNING_).
7113  * Informational exception conditions occur as the result of background scan
7114  * errors, background self-test errors, or vendor specific events within a
7115  * logical unit. An informational exception condition may occur asynchronous
7116  * to any commands.
7117  *
7118  * Returns: SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
7119  * If operation resulted in changing device setup, dmod flag should be set to
7120  * one (1). If parameters were not changed, dmod flag should be set to 0.
7121  * Upon return, if operation required sending command to the device, the rval
7122  * should be set to the value returned by sata_hba_start. If operation
7123  * did not require device access, rval should be set to TRAN_ACCEPT.
7124  * The pagelen should be set to the length of the page.
7125  *
7126  * This function has to be called with a port mutex held.
7127  *
7128  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
7129  *
7130  * Cannot be called in the interrupt context.
7131  */
7132 static	int
7133 sata_mode_select_page_1c(
7134 	sata_pkt_txlate_t *spx,
7135 	struct mode_info_excpt_page *page,
7136 	int parmlen,
7137 	int *pagelen,
7138 	int *rval,
7139 	int *dmod)
7140 {
7141 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7142 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
7143 	sata_drive_info_t *sdinfo;
7144 	sata_id_t *sata_id;
7145 	struct scsi_extended_sense *sense;
7146 
7147 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
7148 	    &spx->txlt_sata_pkt->satapkt_device);
7149 	sata_id = &sdinfo->satadrv_id;
7150 
7151 	*dmod = 0;
7152 
7153 	/* Verify parameters length. If too short, drop it */
7154 	if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) > parmlen) ||
7155 	    page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) {
7156 		*scsipkt->pkt_scbp = STATUS_CHECK;
7157 		sense = sata_arq_sense(spx);
7158 		sense->es_key = KEY_ILLEGAL_REQUEST;
7159 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
7160 		*pagelen = parmlen;
7161 		*rval = TRAN_ACCEPT;
7162 		return (SATA_FAILURE);
7163 	}
7164 
7165 	*pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page);
7166 
7167 	if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
7168 		*scsipkt->pkt_scbp = STATUS_CHECK;
7169 		sense = sata_arq_sense(spx);
7170 		sense->es_key = KEY_ILLEGAL_REQUEST;
7171 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
7172 		*pagelen = parmlen;
7173 		*rval = TRAN_ACCEPT;
7174 		return (SATA_FAILURE);
7175 	}
7176 
7177 	/* If already in the state requested, we are done */
7178 	if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
7179 		/* nothing to do */
7180 		*rval = TRAN_ACCEPT;
7181 		return (SATA_SUCCESS);
7182 	}
7183 
7184 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
7185 
7186 	/* Build SMART_ENABLE or SMART_DISABLE command */
7187 	scmd->satacmd_addr_type = 0;		/* N/A */
7188 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
7189 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
7190 	scmd->satacmd_features_reg = page->dexcpt ?
7191 	    SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS;
7192 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
7193 	scmd->satacmd_cmd_reg = SATAC_SMART;
7194 
7195 	/* Transfer command to HBA */
7196 	if (sata_hba_start(spx, rval) != 0)
7197 		/*
7198 		 * Pkt not accepted for execution.
7199 		 */
7200 		return (SATA_FAILURE);
7201 
7202 	*dmod = 1;	/* At least may have been modified */
7203 
7204 	/* Now process return */
7205 	if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED)
7206 		return (SATA_SUCCESS);
7207 
7208 	/* Packet did not complete successfully */
7209 	sata_xlate_errors(spx);
7210 
7211 	return (SATA_FAILURE);
7212 }
7213 
7214 /*
7215  * Process mode select acoustic management control page 0x30
7216  *
7217  *
7218  * This function has to be called with a port mutex held.
7219  *
7220  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
7221  *
7222  * Cannot be called in the interrupt context.
7223  */
7224 int
7225 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct
7226     mode_acoustic_management *page, int parmlen, int *pagelen,
7227     int *rval, int *dmod)
7228 {
7229 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7230 	sata_drive_info_t *sdinfo;
7231 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
7232 	sata_id_t *sata_id;
7233 	struct scsi_extended_sense *sense;
7234 
7235 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
7236 	    &spx->txlt_sata_pkt->satapkt_device);
7237 	sata_id = &sdinfo->satadrv_id;
7238 	*dmod = 0;
7239 
7240 	/* If parmlen is too short or the feature is not supported, drop it */
7241 	if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
7242 	    sizeof (struct mode_page)) > parmlen) ||
7243 	    (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) {
7244 		*scsipkt->pkt_scbp = STATUS_CHECK;
7245 		sense = sata_arq_sense(spx);
7246 		sense->es_key = KEY_ILLEGAL_REQUEST;
7247 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
7248 		*pagelen = parmlen;
7249 		*rval = TRAN_ACCEPT;
7250 		return (SATA_FAILURE);
7251 	}
7252 
7253 	*pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
7254 	    sizeof (struct mode_page);
7255 
7256 	/*
7257 	 * We can enable and disable acoustice management and
7258 	 * set the acoustic management level.
7259 	 */
7260 
7261 	/*
7262 	 * Set-up Internal SET FEATURES command(s)
7263 	 */
7264 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
7265 	scmd->satacmd_addr_type = 0;
7266 	scmd->satacmd_device_reg = 0;
7267 	scmd->satacmd_status_reg = 0;
7268 	scmd->satacmd_error_reg = 0;
7269 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
7270 	if (page->acoustic_manag_enable) {
7271 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC;
7272 		scmd->satacmd_sec_count_lsb = page->acoustic_manag_level;
7273 	} else {	/* disabling acoustic management */
7274 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC;
7275 	}
7276 
7277 	/* Transfer command to HBA */
7278 	if (sata_hba_start(spx, rval) != 0)
7279 		/*
7280 		 * Pkt not accepted for execution.
7281 		 */
7282 		return (SATA_FAILURE);
7283 
7284 	/* Now process return */
7285 	if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) {
7286 		sata_xlate_errors(spx);
7287 		return (SATA_FAILURE);
7288 	}
7289 
7290 	*dmod = 1;
7291 
7292 	return (SATA_SUCCESS);
7293 }
7294 
7295 /*
7296  * Process mode select power condition page 0x1a
7297  *
7298  * This function has to be called with a port mutex held.
7299  *
7300  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
7301  *
7302  * Cannot be called in the interrupt context.
7303  */
7304 int
7305 sata_mode_select_page_1a(sata_pkt_txlate_t *spx, struct
7306     mode_info_power_cond *page, int parmlen, int *pagelen,
7307     int *rval, int *dmod)
7308 {
7309 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7310 	sata_drive_info_t *sdinfo;
7311 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
7312 	sata_id_t *sata_id;
7313 	struct scsi_extended_sense *sense;
7314 	uint8_t ata_count;
7315 	int i, len;
7316 
7317 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
7318 	    &spx->txlt_sata_pkt->satapkt_device);
7319 	sata_id = &sdinfo->satadrv_id;
7320 	*dmod = 0;
7321 
7322 	len = sizeof (struct mode_info_power_cond);
7323 	len += sizeof (struct mode_page);
7324 
7325 	/* If parmlen is too short or the feature is not supported, drop it */
7326 	if ((len < parmlen) || (page->idle == 1) ||
7327 	    (!(sata_id->ai_cap && SATA_STANDBYTIMER) && page->standby == 1)) {
7328 		*scsipkt->pkt_scbp = STATUS_CHECK;
7329 		sense = sata_arq_sense(spx);
7330 		sense->es_key = KEY_ILLEGAL_REQUEST;
7331 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
7332 		*pagelen = parmlen;
7333 		*rval = TRAN_ACCEPT;
7334 		return (SATA_FAILURE);
7335 	}
7336 
7337 	*pagelen = len;
7338 
7339 	/*
7340 	 * Set-up Internal STANDBY command(s)
7341 	 */
7342 	if (page->standby == 0)
7343 		goto out;
7344 
7345 	ata_count = sata_get_standby_timer(page->standby_cond_timer);
7346 
7347 	scmd->satacmd_addr_type = 0;
7348 	scmd->satacmd_sec_count_lsb = ata_count;
7349 	scmd->satacmd_lba_low_lsb = 0;
7350 	scmd->satacmd_lba_mid_lsb = 0;
7351 	scmd->satacmd_lba_high_lsb = 0;
7352 	scmd->satacmd_features_reg = 0;
7353 	scmd->satacmd_device_reg = 0;
7354 	scmd->satacmd_status_reg = 0;
7355 	scmd->satacmd_cmd_reg = SATAC_STANDBY;
7356 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
7357 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
7358 
7359 	/* Transfer command to HBA */
7360 	if (sata_hba_start(spx, rval) != 0) {
7361 		return (SATA_FAILURE);
7362 	} else {
7363 		if ((scmd->satacmd_error_reg != 0) ||
7364 		    (spx->txlt_sata_pkt->satapkt_reason !=
7365 		    SATA_PKT_COMPLETED)) {
7366 			sata_xlate_errors(spx);
7367 			return (SATA_FAILURE);
7368 		}
7369 	}
7370 
7371 	for (i = 0; i < 4; i++) {
7372 		sdinfo->satadrv_standby_timer[i] = page->standby_cond_timer[i];
7373 	}
7374 out:
7375 	*dmod = 1;
7376 	return (SATA_SUCCESS);
7377 }
7378 
7379 /*
7380  * sata_build_lsense_page0() is used to create the
7381  * SCSI LOG SENSE page 0 (supported log pages)
7382  *
7383  * Currently supported pages are 0, 0x10, 0x2f, 0x30 and 0x0e
7384  * (supported log pages, self-test results, informational exceptions
7385  * Sun vendor specific ATA SMART data, and start stop cycle counter).
7386  *
7387  * Takes a sata_drive_info t * and the address of a buffer
7388  * in which to create the page information.
7389  *
7390  * Returns the number of bytes valid in the buffer.
7391  */
7392 static	int
7393 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf)
7394 {
7395 	struct log_parameter *lpp = (struct log_parameter *)buf;
7396 	uint8_t *page_ptr = (uint8_t *)lpp->param_values;
7397 	int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */
7398 	sata_id_t *sata_id = &sdinfo->satadrv_id;
7399 
7400 	lpp->param_code[0] = 0;
7401 	lpp->param_code[1] = 0;
7402 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
7403 	*page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES;
7404 
7405 	if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
7406 		if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) {
7407 			*page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS;
7408 			++num_pages_supported;
7409 		}
7410 		*page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS;
7411 		++num_pages_supported;
7412 		*page_ptr++ = PAGE_CODE_SMART_READ_DATA;
7413 		++num_pages_supported;
7414 		*page_ptr++ = PAGE_CODE_START_STOP_CYCLE_COUNTER;
7415 		++num_pages_supported;
7416 	}
7417 
7418 	lpp->param_len = num_pages_supported;
7419 
7420 	return ((&lpp->param_values[0] - (uint8_t *)lpp) +
7421 	    num_pages_supported);
7422 }
7423 
7424 /*
7425  * sata_build_lsense_page_10() is used to create the
7426  * SCSI LOG SENSE page 0x10 (self-test results)
7427  *
7428  * Takes a sata_drive_info t * and the address of a buffer
7429  * in which to create the page information as well as a sata_hba_inst_t *.
7430  *
7431  * Returns the number of bytes valid in the buffer.
7432  *
7433  * Note: Self test and SMART data is accessible in device log pages.
7434  * The log pages can be accessed by SMART READ/WRITE LOG (up to 255 sectors
7435  * of data can be transferred by a single command), or by the General Purpose
7436  * Logging commands (GPL) READ LOG EXT and WRITE LOG EXT (up to 65,535 sectors
7437  * - approximately 33MB - can be transferred by a single command.
7438  * The SCT Command response (either error or command) is the same for both
7439  * the SMART and GPL methods of issuing commands.
7440  * This function uses READ LOG EXT command when drive supports LBA48, and
7441  * SMART READ command otherwise.
7442  *
7443  * Since above commands are executed in a synchronous mode, this function
7444  * should not be called in an interrupt context.
7445  */
7446 static	int
7447 sata_build_lsense_page_10(
7448 	sata_drive_info_t *sdinfo,
7449 	uint8_t *buf,
7450 	sata_hba_inst_t *sata_hba_inst)
7451 {
7452 	struct log_parameter *lpp = (struct log_parameter *)buf;
7453 	int rval;
7454 
7455 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
7456 		struct smart_ext_selftest_log *ext_selftest_log;
7457 
7458 		ext_selftest_log = kmem_zalloc(
7459 		    sizeof (struct smart_ext_selftest_log), KM_SLEEP);
7460 
7461 		rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo,
7462 		    ext_selftest_log, 0);
7463 		if (rval == 0) {
7464 			int index, start_index;
7465 			struct smart_ext_selftest_log_entry *entry;
7466 			static const struct smart_ext_selftest_log_entry empty =
7467 			    {0};
7468 			uint16_t block_num;
7469 			int count;
7470 			boolean_t only_one_block = B_FALSE;
7471 
7472 			index = ext_selftest_log->
7473 			    smart_ext_selftest_log_index[0];
7474 			index |= ext_selftest_log->
7475 			    smart_ext_selftest_log_index[1] << 8;
7476 			if (index == 0)
7477 				goto out;
7478 
7479 			--index;	/* Correct for 0 origin */
7480 			start_index = index;	/* remember where we started */
7481 			block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
7482 			if (block_num != 0) {
7483 				rval = sata_ext_smart_selftest_read_log(
7484 				    sata_hba_inst, sdinfo, ext_selftest_log,
7485 				    block_num);
7486 				if (rval != 0)
7487 					goto out;
7488 			}
7489 			index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
7490 			entry =
7491 			    &ext_selftest_log->
7492 			    smart_ext_selftest_log_entries[index];
7493 
7494 			for (count = 1;
7495 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
7496 			    ++count) {
7497 				uint8_t status;
7498 				uint8_t code;
7499 				uint8_t sense_key;
7500 				uint8_t add_sense_code;
7501 				uint8_t add_sense_code_qual;
7502 
7503 				/* If this is an unused entry, we are done */
7504 				if (bcmp(entry, &empty, sizeof (empty)) == 0) {
7505 					/* Broken firmware on some disks */
7506 					if (index + 1 ==
7507 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK) {
7508 						--entry;
7509 						--index;
7510 						if (bcmp(entry, &empty,
7511 						    sizeof (empty)) == 0)
7512 							goto out;
7513 					} else
7514 						goto out;
7515 				}
7516 
7517 				if (only_one_block &&
7518 				    start_index == index)
7519 					goto out;
7520 
7521 				lpp->param_code[0] = 0;
7522 				lpp->param_code[1] = count;
7523 				lpp->param_ctrl_flags =
7524 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
7525 				lpp->param_len =
7526 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
7527 
7528 				status = entry->smart_ext_selftest_log_status;
7529 				status >>= 4;
7530 				switch (status) {
7531 				case 0:
7532 				default:
7533 					sense_key = KEY_NO_SENSE;
7534 					add_sense_code =
7535 					    SD_SCSI_ASC_NO_ADD_SENSE;
7536 					add_sense_code_qual = 0;
7537 					break;
7538 				case 1:
7539 					sense_key = KEY_ABORTED_COMMAND;
7540 					add_sense_code =
7541 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7542 					add_sense_code_qual = SCSI_COMPONENT_81;
7543 					break;
7544 				case 2:
7545 					sense_key = KEY_ABORTED_COMMAND;
7546 					add_sense_code =
7547 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7548 					add_sense_code_qual = SCSI_COMPONENT_82;
7549 					break;
7550 				case 3:
7551 					sense_key = KEY_ABORTED_COMMAND;
7552 					add_sense_code =
7553 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7554 					add_sense_code_qual = SCSI_COMPONENT_83;
7555 					break;
7556 				case 4:
7557 					sense_key = KEY_HARDWARE_ERROR;
7558 					add_sense_code =
7559 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7560 					add_sense_code_qual = SCSI_COMPONENT_84;
7561 					break;
7562 				case 5:
7563 					sense_key = KEY_HARDWARE_ERROR;
7564 					add_sense_code =
7565 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7566 					add_sense_code_qual = SCSI_COMPONENT_85;
7567 					break;
7568 				case 6:
7569 					sense_key = KEY_HARDWARE_ERROR;
7570 					add_sense_code =
7571 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7572 					add_sense_code_qual = SCSI_COMPONENT_86;
7573 					break;
7574 				case 7:
7575 					sense_key = KEY_MEDIUM_ERROR;
7576 					add_sense_code =
7577 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7578 					add_sense_code_qual = SCSI_COMPONENT_87;
7579 					break;
7580 				case 8:
7581 					sense_key = KEY_HARDWARE_ERROR;
7582 					add_sense_code =
7583 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7584 					add_sense_code_qual = SCSI_COMPONENT_88;
7585 					break;
7586 				}
7587 				code = 0;	/* unspecified */
7588 				status |= (code << 4);
7589 				lpp->param_values[0] = status;
7590 				lpp->param_values[1] = 0; /* unspecified */
7591 				lpp->param_values[2] = entry->
7592 				    smart_ext_selftest_log_timestamp[1];
7593 				lpp->param_values[3] = entry->
7594 				    smart_ext_selftest_log_timestamp[0];
7595 				if (status != 0) {
7596 					lpp->param_values[4] = 0;
7597 					lpp->param_values[5] = 0;
7598 					lpp->param_values[6] = entry->
7599 					    smart_ext_selftest_log_failing_lba
7600 					    [5];
7601 					lpp->param_values[7] = entry->
7602 					    smart_ext_selftest_log_failing_lba
7603 					    [4];
7604 					lpp->param_values[8] = entry->
7605 					    smart_ext_selftest_log_failing_lba
7606 					    [3];
7607 					lpp->param_values[9] = entry->
7608 					    smart_ext_selftest_log_failing_lba
7609 					    [2];
7610 					lpp->param_values[10] = entry->
7611 					    smart_ext_selftest_log_failing_lba
7612 					    [1];
7613 					lpp->param_values[11] = entry->
7614 					    smart_ext_selftest_log_failing_lba
7615 					    [0];
7616 				} else {	/* No bad block address */
7617 					lpp->param_values[4] = 0xff;
7618 					lpp->param_values[5] = 0xff;
7619 					lpp->param_values[6] = 0xff;
7620 					lpp->param_values[7] = 0xff;
7621 					lpp->param_values[8] = 0xff;
7622 					lpp->param_values[9] = 0xff;
7623 					lpp->param_values[10] = 0xff;
7624 					lpp->param_values[11] = 0xff;
7625 				}
7626 
7627 				lpp->param_values[12] = sense_key;
7628 				lpp->param_values[13] = add_sense_code;
7629 				lpp->param_values[14] = add_sense_code_qual;
7630 				lpp->param_values[15] = 0; /* undefined */
7631 
7632 				lpp = (struct log_parameter *)
7633 				    (((uint8_t *)lpp) +
7634 				    SCSI_LOG_PARAM_HDR_LEN +
7635 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
7636 
7637 				--index;	/* Back up to previous entry */
7638 				if (index < 0) {
7639 					if (block_num > 0) {
7640 						--block_num;
7641 					} else {
7642 						struct read_log_ext_directory
7643 						    logdir;
7644 
7645 						rval =
7646 						    sata_read_log_ext_directory(
7647 						    sata_hba_inst, sdinfo,
7648 						    &logdir);
7649 						if (rval == -1)
7650 							goto out;
7651 						if ((logdir.read_log_ext_vers
7652 						    [0] == 0) &&
7653 						    (logdir.read_log_ext_vers
7654 						    [1] == 0))
7655 							goto out;
7656 						block_num =
7657 						    logdir.read_log_ext_nblks
7658 						    [EXT_SMART_SELFTEST_LOG_PAGE
7659 						    - 1][0];
7660 						block_num |= logdir.
7661 						    read_log_ext_nblks
7662 						    [EXT_SMART_SELFTEST_LOG_PAGE
7663 						    - 1][1] << 8;
7664 						--block_num;
7665 						only_one_block =
7666 						    (block_num == 0);
7667 					}
7668 					rval = sata_ext_smart_selftest_read_log(
7669 					    sata_hba_inst, sdinfo,
7670 					    ext_selftest_log, block_num);
7671 					if (rval != 0)
7672 						goto out;
7673 
7674 					index =
7675 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK -
7676 					    1;
7677 				}
7678 				index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
7679 				entry = &ext_selftest_log->
7680 				    smart_ext_selftest_log_entries[index];
7681 			}
7682 		}
7683 out:
7684 		kmem_free(ext_selftest_log,
7685 		    sizeof (struct smart_ext_selftest_log));
7686 	} else {
7687 		struct smart_selftest_log *selftest_log;
7688 
7689 		selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log),
7690 		    KM_SLEEP);
7691 
7692 		rval = sata_smart_selftest_log(sata_hba_inst, sdinfo,
7693 		    selftest_log);
7694 
7695 		if (rval == 0) {
7696 			int index;
7697 			int count;
7698 			struct smart_selftest_log_entry *entry;
7699 			static const struct smart_selftest_log_entry empty =
7700 			    { 0 };
7701 
7702 			index = selftest_log->smart_selftest_log_index;
7703 			if (index == 0)
7704 				goto done;
7705 			--index;	/* Correct for 0 origin */
7706 			entry = &selftest_log->
7707 			    smart_selftest_log_entries[index];
7708 			for (count = 1;
7709 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
7710 			    ++count) {
7711 				uint8_t status;
7712 				uint8_t code;
7713 				uint8_t sense_key;
7714 				uint8_t add_sense_code;
7715 				uint8_t add_sense_code_qual;
7716 
7717 				if (bcmp(entry, &empty, sizeof (empty)) == 0)
7718 					goto done;
7719 
7720 				lpp->param_code[0] = 0;
7721 				lpp->param_code[1] = count;
7722 				lpp->param_ctrl_flags =
7723 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
7724 				lpp->param_len =
7725 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
7726 
7727 				status = entry->smart_selftest_log_status;
7728 				status >>= 4;
7729 				switch (status) {
7730 				case 0:
7731 				default:
7732 					sense_key = KEY_NO_SENSE;
7733 					add_sense_code =
7734 					    SD_SCSI_ASC_NO_ADD_SENSE;
7735 					break;
7736 				case 1:
7737 					sense_key = KEY_ABORTED_COMMAND;
7738 					add_sense_code =
7739 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7740 					add_sense_code_qual = SCSI_COMPONENT_81;
7741 					break;
7742 				case 2:
7743 					sense_key = KEY_ABORTED_COMMAND;
7744 					add_sense_code =
7745 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7746 					add_sense_code_qual = SCSI_COMPONENT_82;
7747 					break;
7748 				case 3:
7749 					sense_key = KEY_ABORTED_COMMAND;
7750 					add_sense_code =
7751 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7752 					add_sense_code_qual = SCSI_COMPONENT_83;
7753 					break;
7754 				case 4:
7755 					sense_key = KEY_HARDWARE_ERROR;
7756 					add_sense_code =
7757 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7758 					add_sense_code_qual = SCSI_COMPONENT_84;
7759 					break;
7760 				case 5:
7761 					sense_key = KEY_HARDWARE_ERROR;
7762 					add_sense_code =
7763 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7764 					add_sense_code_qual = SCSI_COMPONENT_85;
7765 					break;
7766 				case 6:
7767 					sense_key = KEY_HARDWARE_ERROR;
7768 					add_sense_code =
7769 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7770 					add_sense_code_qual = SCSI_COMPONENT_86;
7771 					break;
7772 				case 7:
7773 					sense_key = KEY_MEDIUM_ERROR;
7774 					add_sense_code =
7775 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7776 					add_sense_code_qual = SCSI_COMPONENT_87;
7777 					break;
7778 				case 8:
7779 					sense_key = KEY_HARDWARE_ERROR;
7780 					add_sense_code =
7781 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7782 					add_sense_code_qual = SCSI_COMPONENT_88;
7783 					break;
7784 				}
7785 				code = 0;	/* unspecified */
7786 				status |= (code << 4);
7787 				lpp->param_values[0] = status;
7788 				lpp->param_values[1] = 0; /* unspecified */
7789 				lpp->param_values[2] = entry->
7790 				    smart_selftest_log_timestamp[1];
7791 				lpp->param_values[3] = entry->
7792 				    smart_selftest_log_timestamp[0];
7793 				if (status != 0) {
7794 					lpp->param_values[4] = 0;
7795 					lpp->param_values[5] = 0;
7796 					lpp->param_values[6] = 0;
7797 					lpp->param_values[7] = 0;
7798 					lpp->param_values[8] = entry->
7799 					    smart_selftest_log_failing_lba[3];
7800 					lpp->param_values[9] = entry->
7801 					    smart_selftest_log_failing_lba[2];
7802 					lpp->param_values[10] = entry->
7803 					    smart_selftest_log_failing_lba[1];
7804 					lpp->param_values[11] = entry->
7805 					    smart_selftest_log_failing_lba[0];
7806 				} else {	/* No block address */
7807 					lpp->param_values[4] = 0xff;
7808 					lpp->param_values[5] = 0xff;
7809 					lpp->param_values[6] = 0xff;
7810 					lpp->param_values[7] = 0xff;
7811 					lpp->param_values[8] = 0xff;
7812 					lpp->param_values[9] = 0xff;
7813 					lpp->param_values[10] = 0xff;
7814 					lpp->param_values[11] = 0xff;
7815 				}
7816 				lpp->param_values[12] = sense_key;
7817 				lpp->param_values[13] = add_sense_code;
7818 				lpp->param_values[14] = add_sense_code_qual;
7819 				lpp->param_values[15] = 0; /* undefined */
7820 
7821 				lpp = (struct log_parameter *)
7822 				    (((uint8_t *)lpp) +
7823 				    SCSI_LOG_PARAM_HDR_LEN +
7824 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
7825 				--index;	/* back up to previous entry */
7826 				if (index < 0) {
7827 					index =
7828 					    NUM_SMART_SELFTEST_LOG_ENTRIES - 1;
7829 				}
7830 				entry = &selftest_log->
7831 				    smart_selftest_log_entries[index];
7832 			}
7833 		}
7834 done:
7835 		kmem_free(selftest_log, sizeof (struct smart_selftest_log));
7836 	}
7837 
7838 	return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) *
7839 	    SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS);
7840 }
7841 
7842 /*
7843  * sata_build_lsense_page_2f() is used to create the
7844  * SCSI LOG SENSE page 0x2f (informational exceptions)
7845  *
7846  * Takes a sata_drive_info t * and the address of a buffer
7847  * in which to create the page information as well as a sata_hba_inst_t *.
7848  *
7849  * Returns the number of bytes valid in the buffer.
7850  *
7851  * Because it invokes function(s) that send synchronously executed command
7852  * to the HBA, it cannot be called in the interrupt context.
7853  */
7854 static	int
7855 sata_build_lsense_page_2f(
7856 	sata_drive_info_t *sdinfo,
7857 	uint8_t *buf,
7858 	sata_hba_inst_t *sata_hba_inst)
7859 {
7860 	struct log_parameter *lpp = (struct log_parameter *)buf;
7861 	int rval;
7862 	uint8_t *smart_data;
7863 	uint8_t temp;
7864 	sata_id_t *sata_id;
7865 #define	SMART_NO_TEMP	0xff
7866 
7867 	lpp->param_code[0] = 0;
7868 	lpp->param_code[1] = 0;
7869 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
7870 
7871 	/* Now get the SMART status w.r.t. threshold exceeded */
7872 	rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo);
7873 	switch (rval) {
7874 	case 1:
7875 		lpp->param_values[0] = SCSI_PREDICTED_FAILURE;
7876 		lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE;
7877 		break;
7878 	case 0:
7879 	case -1:	/* failed to get data */
7880 		lpp->param_values[0] = 0;	/* No failure predicted */
7881 		lpp->param_values[1] = 0;
7882 		break;
7883 #if defined(SATA_DEBUG)
7884 	default:
7885 		cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value");
7886 		/* NOTREACHED */
7887 #endif
7888 	}
7889 
7890 	sata_id = &sdinfo->satadrv_id;
7891 	if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP))
7892 		temp = SMART_NO_TEMP;
7893 	else {
7894 		/* Now get the temperature */
7895 		smart_data = kmem_zalloc(512, KM_SLEEP);
7896 		rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data,
7897 		    SCT_STATUS_LOG_PAGE, 1);
7898 		if (rval == -1)
7899 			temp = SMART_NO_TEMP;
7900 		else {
7901 			temp = smart_data[200];
7902 			if (temp & 0x80) {
7903 				if (temp & 0x7f)
7904 					temp = 0;
7905 				else
7906 					temp = SMART_NO_TEMP;
7907 			}
7908 		}
7909 		kmem_free(smart_data, 512);
7910 	}
7911 
7912 	lpp->param_values[2] = temp;	/* most recent temperature */
7913 	lpp->param_values[3] = 0;	/* required vendor specific byte */
7914 
7915 	lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN;
7916 
7917 
7918 	return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN);
7919 }
7920 
7921 /*
7922  * sata_build_lsense_page_30() is used to create the
7923  * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data).
7924  *
7925  * Takes a sata_drive_info t * and the address of a buffer
7926  * in which to create the page information as well as a sata_hba_inst_t *.
7927  *
7928  * Returns the number of bytes valid in the buffer.
7929  */
7930 static int
7931 sata_build_lsense_page_30(
7932 	sata_drive_info_t *sdinfo,
7933 	uint8_t *buf,
7934 	sata_hba_inst_t *sata_hba_inst)
7935 {
7936 	struct smart_data *smart_data = (struct smart_data *)buf;
7937 	int rval;
7938 
7939 	/* Now do the SMART READ DATA */
7940 	rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data);
7941 	if (rval == -1)
7942 		return (0);
7943 
7944 	return (sizeof (struct smart_data));
7945 }
7946 
7947 /*
7948  * sata_build_lsense_page_0e() is used to create the
7949  * SCSI LOG SENSE page 0e (start-stop cycle counter page)
7950  *
7951  * Date of Manufacture (0x0001)
7952  *	YEAR = "0000"
7953  *	WEEK = "00"
7954  * Accounting Date (0x0002)
7955  *	6 ASCII space character(20h)
7956  * Specified cycle count over device lifetime
7957  *	VALUE - THRESH - the delta between max and min;
7958  * Accumulated start-stop cycles
7959  *	VALUE - WORST - the accumulated cycles;
7960  *
7961  * ID FLAG THRESH VALUE WORST RAW on start/stop counter attribute
7962  *
7963  * Takes a sata_drive_info t * and the address of a buffer
7964  * in which to create the page information as well as a sata_hba_inst_t *.
7965  *
7966  * Returns the number of bytes valid in the buffer.
7967  */
7968 static	int
7969 sata_build_lsense_page_0e(sata_drive_info_t *sdinfo, uint8_t *buf,
7970 	sata_pkt_txlate_t *spx)
7971 {
7972 	struct start_stop_cycle_counter_log *log_page;
7973 	int i, rval, index;
7974 	uint8_t smart_data[512], id, value, worst, thresh;
7975 	uint32_t max_count, cycles;
7976 
7977 	/* Now do the SMART READ DATA */
7978 	rval = sata_fetch_smart_data(spx->txlt_sata_hba_inst, sdinfo,
7979 	    (struct smart_data *)smart_data);
7980 	if (rval == -1)
7981 		return (0);
7982 	for (i = 0, id = 0; i < SMART_START_STOP_COUNT_ID * 2; i++) {
7983 		index = (i * 12) + 2;
7984 		id = smart_data[index];
7985 		if (id != SMART_START_STOP_COUNT_ID)
7986 			continue;
7987 		else {
7988 			thresh = smart_data[index + 2];
7989 			value = smart_data[index + 3];
7990 			worst = smart_data[index + 4];
7991 			break;
7992 		}
7993 	}
7994 	if (id != SMART_START_STOP_COUNT_ID)
7995 		return (0);
7996 	max_count = value - thresh;
7997 	cycles = value - worst;
7998 
7999 	log_page = (struct start_stop_cycle_counter_log *)buf;
8000 	bzero(log_page, sizeof (struct start_stop_cycle_counter_log));
8001 	log_page->code = 0x0e;
8002 	log_page->page_len_low = 0x24;
8003 
8004 	log_page->manufactor_date_low = 0x1;
8005 	log_page->param_1.fmt_link = 0x1; /* 01b */
8006 	log_page->param_len_1 = 0x06;
8007 	for (i = 0; i < 4; i++) {
8008 		log_page->year_manu[i] = 0x30;
8009 		if (i < 2)
8010 			log_page->week_manu[i] = 0x30;
8011 	}
8012 
8013 	log_page->account_date_low = 0x02;
8014 	log_page->param_2.fmt_link = 0x01; /* 01b */
8015 	log_page->param_len_2 = 0x06;
8016 	for (i = 0; i < 4; i++) {
8017 		log_page->year_account[i] = 0x20;
8018 		if (i < 2)
8019 			log_page->week_account[i] = 0x20;
8020 	}
8021 
8022 	log_page->lifetime_code_low = 0x03;
8023 	log_page->param_3.fmt_link = 0x03; /* 11b */
8024 	log_page->param_len_3 = 0x04;
8025 	/* VALUE - THRESH - the delta between max and min */
8026 	log_page->cycle_code_low = 0x04;
8027 	log_page->param_4.fmt_link = 0x03; /* 11b */
8028 	log_page->param_len_4 = 0x04;
8029 	/* WORST - THRESH - the distance from 'now' to min */
8030 
8031 	for (i = 0; i < 4; i++) {
8032 		log_page->cycle_lifetime[i] =
8033 		    (max_count >> (8 * (3 - i))) & 0xff;
8034 		log_page->cycle_accumulated[i] =
8035 		    (cycles >> (8 * (3 - i))) & 0xff;
8036 	}
8037 
8038 	return (sizeof (struct start_stop_cycle_counter_log));
8039 }
8040 
8041 /*
8042  * This function was used for build a ATA read verify sector command
8043  */
8044 static void
8045 sata_build_read_verify_cmd(sata_cmd_t *scmd, uint16_t sec, uint64_t lba)
8046 {
8047 	scmd->satacmd_cmd_reg = SATAC_RDVER;
8048 	scmd->satacmd_addr_type = ATA_ADDR_LBA28;
8049 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
8050 
8051 	scmd->satacmd_sec_count_lsb = sec & 0xff;
8052 	scmd->satacmd_lba_low_lsb = lba & 0xff;
8053 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
8054 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
8055 	scmd->satacmd_device_reg = (SATA_ADH_LBA | (lba >> 24) & 0xf);
8056 	scmd->satacmd_features_reg = 0;
8057 	scmd->satacmd_status_reg = 0;
8058 	scmd->satacmd_error_reg = 0;
8059 }
8060 
8061 /*
8062  * This function was used for building an ATA
8063  * command, and only command register need to
8064  * be defined, other register will be zero or na.
8065  */
8066 static void
8067 sata_build_generic_cmd(sata_cmd_t *scmd, uint8_t cmd)
8068 {
8069 	scmd->satacmd_addr_type = 0;
8070 	scmd->satacmd_cmd_reg = cmd;
8071 	scmd->satacmd_device_reg = 0;
8072 	scmd->satacmd_sec_count_lsb = 0;
8073 	scmd->satacmd_lba_low_lsb = 0;
8074 	scmd->satacmd_lba_mid_lsb = 0;
8075 	scmd->satacmd_lba_high_lsb = 0;
8076 	scmd->satacmd_features_reg = 0;
8077 	scmd->satacmd_status_reg = 0;
8078 	scmd->satacmd_error_reg = 0;
8079 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
8080 }
8081 
8082 /*
8083  * This function was used for changing the standby
8084  * timer format from SCSI to ATA.
8085  */
8086 static uint8_t
8087 sata_get_standby_timer(uint8_t *timer)
8088 {
8089 	uint32_t i = 0, count = 0;
8090 	uint8_t ata_count;
8091 
8092 	for (i = 0; i < 4; i++) {
8093 		count = count << 8 | timer[i];
8094 	}
8095 
8096 	if (count == 0)
8097 		return (0);
8098 
8099 	if (count >= 1 && count <= 12000)
8100 		ata_count = (count -1) / 50 + 1;
8101 	else if (count > 12000 && count <= 12600)
8102 		ata_count = 0xfc;
8103 	else if (count > 12601 && count <= 12750)
8104 		ata_count = 0xff;
8105 	else if (count > 12750 && count <= 17999)
8106 		ata_count = 0xf1;
8107 	else if (count > 18000 && count <= 198000)
8108 		ata_count = count / 18000 + 240;
8109 	else
8110 		ata_count = 0xfd;
8111 	return (ata_count);
8112 }
8113 
8114 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */
8115 
8116 /*
8117  * Start command for ATAPI device.
8118  * This function processes scsi_pkt requests.
8119  * Now CD/DVD, tape and ATAPI disk devices are supported.
8120  * Most commands are packet without any translation into Packet Command.
8121  * Some may be trapped and executed as SATA commands (not clear which one).
8122  *
8123  * Returns TRAN_ACCEPT if command is accepted for execution (or completed
8124  * execution).
8125  * Returns other TRAN_XXXX codes if command is not accepted or completed
8126  * (see return values for sata_hba_start()).
8127  *
8128  * Note:
8129  * Inquiry cdb format differs between transport version 2 and 3.
8130  * However, the transport version 3 devices that were checked did not adhere
8131  * to the specification (ignored MSB of the allocation length). Therefore,
8132  * the transport version is not checked, but Inquiry allocation length is
8133  * truncated to 255 bytes if the original allocation length set-up by the
8134  * target driver is greater than 255 bytes.
8135  */
8136 static int
8137 sata_txlt_atapi(sata_pkt_txlate_t *spx)
8138 {
8139 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8140 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8141 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
8142 	sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx);
8143 	sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba,
8144 	    &spx->txlt_sata_pkt->satapkt_device);
8145 	int cport = SATA_TXLT_CPORT(spx);
8146 	int cdblen;
8147 	int rval, reason;
8148 	int synch;
8149 	union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp;
8150 
8151 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
8152 
8153 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
8154 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
8155 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
8156 		return (rval);
8157 	}
8158 
8159 	/*
8160 	 * ATAPI device executes some ATA commands in addition to those
8161 	 * commands sent via PACKET command. These ATA commands may be
8162 	 * executed by the regular SATA translation functions. None needs
8163 	 * to be captured now.
8164 	 *
8165 	 * Commands sent via PACKET command include:
8166 	 *	MMC command set for ATAPI CD/DVD device
8167 	 *	SSC command set for ATAPI TAPE device
8168 	 *	SBC command set for ATAPI disk device
8169 	 *
8170 	 */
8171 
8172 	/* Check the size of cdb */
8173 	cdblen = scsi_cdb_size[GETGROUP(cdbp)];
8174 	if (cdblen > sdinfo->satadrv_atapi_cdb_len) {
8175 		sata_log(NULL, CE_WARN,
8176 		    "sata: invalid ATAPI cdb length %d",
8177 		    scsipkt->pkt_cdblen);
8178 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
8179 		return (TRAN_BADPKT);
8180 	}
8181 
8182 	SATAATAPITRACE(spx, cdblen);
8183 
8184 	/*
8185 	 * For non-read/write commands we need to
8186 	 * map buffer
8187 	 */
8188 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
8189 	case SCMD_READ:
8190 	case SCMD_READ_G1:
8191 	case SCMD_READ_G5:
8192 	case SCMD_READ_G4:
8193 	case SCMD_WRITE:
8194 	case SCMD_WRITE_G1:
8195 	case SCMD_WRITE_G5:
8196 	case SCMD_WRITE_G4:
8197 		break;
8198 	default:
8199 		if (bp != NULL) {
8200 			if (bp->b_flags & (B_PHYS | B_PAGEIO))
8201 				bp_mapin(bp);
8202 		}
8203 		break;
8204 	}
8205 	/*
8206 	 * scmd->satacmd_flags.sata_data_direction default -
8207 	 * SATA_DIR_NODATA_XFER - is set by
8208 	 * sata_txlt_generic_pkt_info().
8209 	 */
8210 	if (scmd->satacmd_bp) {
8211 		if (scmd->satacmd_bp->b_flags & B_READ) {
8212 			scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
8213 		} else {
8214 			scmd->satacmd_flags.sata_data_direction =
8215 			    SATA_DIR_WRITE;
8216 		}
8217 	}
8218 
8219 	/*
8220 	 * Set up ATAPI packet command.
8221 	 */
8222 
8223 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
8224 
8225 	/* Copy cdb into sata_cmd */
8226 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
8227 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
8228 	bcopy(cdbp, scmd->satacmd_acdb, cdblen);
8229 
8230 	/* See note in the command header */
8231 	if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) {
8232 		if (scmd->satacmd_acdb[3] != 0)
8233 			scmd->satacmd_acdb[4] = 255;
8234 	}
8235 
8236 #ifdef SATA_DEBUG
8237 	if (sata_debug_flags & SATA_DBG_ATAPI) {
8238 		uint8_t *p = scmd->satacmd_acdb;
8239 		char buf[3 * SATA_ATAPI_MAX_CDB_LEN];
8240 
8241 		(void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN,
8242 		    "%02x %02x %02x %02x %02x %02x %02x %02x "
8243 		    "%2x %02x %02x %02x %02x %02x %02x %02x",
8244 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
8245 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
8246 		buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0';
8247 		cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf);
8248 	}
8249 #endif
8250 
8251 	/*
8252 	 * Preset request sense data to NO SENSE.
8253 	 * If there is no way to get error information via Request Sense,
8254 	 * the packet request sense data would not have to be modified by HBA,
8255 	 * but it could be returned as is.
8256 	 */
8257 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
8258 	sata_fixed_sense_data_preset(
8259 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
8260 
8261 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
8262 		/* Need callback function */
8263 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion;
8264 		synch = FALSE;
8265 	} else
8266 		synch = TRUE;
8267 
8268 	/* Transfer command to HBA */
8269 	if (sata_hba_start(spx, &rval) != 0) {
8270 		/* Pkt not accepted for execution */
8271 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
8272 		return (rval);
8273 	}
8274 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
8275 	/*
8276 	 * If execution is non-synchronous,
8277 	 * a callback function will handle potential errors, translate
8278 	 * the response and will do a callback to a target driver.
8279 	 * If it was synchronous, use the same framework callback to check
8280 	 * an execution status.
8281 	 */
8282 	if (synch) {
8283 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
8284 		    "synchronous execution status %x\n",
8285 		    spx->txlt_sata_pkt->satapkt_reason);
8286 		sata_txlt_atapi_completion(spx->txlt_sata_pkt);
8287 	}
8288 	return (TRAN_ACCEPT);
8289 }
8290 
8291 
8292 /*
8293  * ATAPI Packet command completion.
8294  *
8295  * Failure of the command passed via Packet command are considered device
8296  * error. SATA HBA driver would have to retrieve error data (via Request
8297  * Sense command delivered via error retrieval sata packet) and copy it
8298  * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data.
8299  */
8300 static void
8301 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt)
8302 {
8303 	sata_pkt_txlate_t *spx =
8304 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
8305 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8306 	struct scsi_extended_sense *sense;
8307 	struct buf *bp;
8308 	int rval;
8309 
8310 #ifdef SATA_DEBUG
8311 	uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense;
8312 #endif
8313 
8314 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
8315 	    STATE_SENT_CMD | STATE_GOT_STATUS;
8316 
8317 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
8318 		/* Normal completion */
8319 		if (sata_pkt->satapkt_cmd.satacmd_bp != NULL)
8320 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
8321 		scsipkt->pkt_reason = CMD_CMPLT;
8322 		*scsipkt->pkt_scbp = STATUS_GOOD;
8323 		if (spx->txlt_tmp_buf != NULL) {
8324 			/* Temporary buffer was used */
8325 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
8326 			if (bp->b_flags & B_READ) {
8327 				rval = ddi_dma_sync(
8328 				    spx->txlt_buf_dma_handle, 0, 0,
8329 				    DDI_DMA_SYNC_FORCPU);
8330 				ASSERT(rval == DDI_SUCCESS);
8331 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
8332 				    bp->b_bcount);
8333 			}
8334 		}
8335 	} else {
8336 		/*
8337 		 * Something went wrong - analyze return
8338 		 */
8339 		*scsipkt->pkt_scbp = STATUS_CHECK;
8340 		sense = sata_arq_sense(spx);
8341 
8342 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
8343 			/*
8344 			 * pkt_reason should be CMD_CMPLT for DEVICE ERROR.
8345 			 * Under this condition ERR bit is set for ATA command,
8346 			 * and CHK bit set for ATAPI command.
8347 			 *
8348 			 * Please check st_intr & sdintr about how pkt_reason
8349 			 * is used.
8350 			 */
8351 			scsipkt->pkt_reason = CMD_CMPLT;
8352 
8353 			/*
8354 			 * We may not have ARQ data if there was a double
8355 			 * error. But sense data in sata packet was pre-set
8356 			 * with NO SENSE so it is valid even if HBA could
8357 			 * not retrieve a real sense data.
8358 			 * Just copy this sense data into scsi pkt sense area.
8359 			 */
8360 			bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense,
8361 			    SATA_ATAPI_MIN_RQSENSE_LEN);
8362 #ifdef SATA_DEBUG
8363 			if (sata_debug_flags & SATA_DBG_SCSI_IF) {
8364 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
8365 				    "sata_txlt_atapi_completion: %02x\n"
8366 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
8367 				    "          %02x %02x %02x %02x %02x %02x "
8368 				    "          %02x %02x %02x %02x %02x %02x\n",
8369 				    scsipkt->pkt_reason,
8370 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
8371 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
8372 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
8373 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
8374 				    rqsp[16], rqsp[17]);
8375 			}
8376 #endif
8377 		} else {
8378 			switch (sata_pkt->satapkt_reason) {
8379 			case SATA_PKT_PORT_ERROR:
8380 				/*
8381 				 * We have no device data.
8382 				 */
8383 				scsipkt->pkt_reason = CMD_INCOMPLETE;
8384 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
8385 				    STATE_GOT_TARGET | STATE_SENT_CMD |
8386 				    STATE_GOT_STATUS);
8387 				sense->es_key = KEY_HARDWARE_ERROR;
8388 				break;
8389 
8390 			case SATA_PKT_TIMEOUT:
8391 				scsipkt->pkt_reason = CMD_TIMEOUT;
8392 				scsipkt->pkt_statistics |=
8393 				    STAT_TIMEOUT | STAT_DEV_RESET;
8394 				/*
8395 				 * Need to check if HARDWARE_ERROR/
8396 				 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more
8397 				 * appropriate.
8398 				 */
8399 				break;
8400 
8401 			case SATA_PKT_ABORTED:
8402 				scsipkt->pkt_reason = CMD_ABORTED;
8403 				scsipkt->pkt_statistics |= STAT_ABORTED;
8404 				/* Should we set key COMMAND_ABPRTED? */
8405 				break;
8406 
8407 			case SATA_PKT_RESET:
8408 				scsipkt->pkt_reason = CMD_RESET;
8409 				scsipkt->pkt_statistics |= STAT_DEV_RESET;
8410 				/*
8411 				 * May be we should set Unit Attention /
8412 				 * Reset. Perhaps the same should be
8413 				 * returned for disks....
8414 				 */
8415 				sense->es_key = KEY_UNIT_ATTENTION;
8416 				sense->es_add_code = SD_SCSI_ASC_RESET;
8417 				break;
8418 
8419 			default:
8420 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
8421 				    "sata_txlt_atapi_completion: "
8422 				    "invalid packet completion reason"));
8423 				scsipkt->pkt_reason = CMD_TRAN_ERR;
8424 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
8425 				    STATE_GOT_TARGET | STATE_SENT_CMD |
8426 				    STATE_GOT_STATUS);
8427 				break;
8428 			}
8429 		}
8430 	}
8431 
8432 	SATAATAPITRACE(spx, 0);
8433 
8434 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
8435 	    scsipkt->pkt_comp != NULL) {
8436 		/* scsi callback required */
8437 		(*scsipkt->pkt_comp)(scsipkt);
8438 	}
8439 }
8440 
8441 /*
8442  * Set up error retrieval sata command for ATAPI Packet Command error data
8443  * recovery.
8444  *
8445  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
8446  * returns SATA_FAILURE otherwise.
8447  */
8448 
8449 static int
8450 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
8451 {
8452 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
8453 	sata_cmd_t *scmd;
8454 	struct buf *bp;
8455 
8456 	/*
8457 	 * Allocate dma-able buffer error data.
8458 	 * Buffer allocation will take care of buffer alignment and other DMA
8459 	 * attributes.
8460 	 */
8461 	bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN);
8462 	if (bp == NULL) {
8463 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
8464 		    "sata_get_err_retrieval_pkt: "
8465 		    "cannot allocate buffer for error data", NULL);
8466 		return (SATA_FAILURE);
8467 	}
8468 	bp_mapin(bp); /* make data buffer accessible */
8469 
8470 	/* Operation modes are up to the caller */
8471 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
8472 
8473 	/* Synchronous mode, no callback - may be changed by the caller */
8474 	spkt->satapkt_comp = NULL;
8475 	spkt->satapkt_time = sata_default_pkt_time;
8476 
8477 	scmd = &spkt->satapkt_cmd;
8478 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
8479 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
8480 
8481 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
8482 
8483 	/*
8484 	 * Set-up acdb. Request Sense CDB (packet command content) is
8485 	 * not in DMA-able buffer. Its handling is HBA-specific (how
8486 	 * it is transfered into packet FIS).
8487 	 */
8488 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
8489 	bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN);
8490 	/* Following zeroing of pad bytes may not be necessary */
8491 	bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN],
8492 	    sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN);
8493 
8494 	/*
8495 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
8496 	 * before accessing it. Handle is in usual place in translate struct.
8497 	 */
8498 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
8499 
8500 	/*
8501 	 * Preset request sense data to NO SENSE.
8502 	 * Here it is redundant, only for a symetry with scsi-originated
8503 	 * packets. It should not be used for anything but debugging.
8504 	 */
8505 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
8506 	sata_fixed_sense_data_preset(
8507 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
8508 
8509 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
8510 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
8511 
8512 	return (SATA_SUCCESS);
8513 }
8514 
8515 /*
8516  * Set-up ATAPI packet command.
8517  * Data transfer direction has to be set-up in sata_cmd structure prior to
8518  * calling this function.
8519  *
8520  * Returns void
8521  */
8522 
8523 static void
8524 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo)
8525 {
8526 	scmd->satacmd_addr_type = 0;		/* N/A */
8527 	scmd->satacmd_sec_count_lsb = 0;	/* no tag */
8528 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
8529 	scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ;
8530 	scmd->satacmd_lba_high_lsb =
8531 	    (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8);
8532 	scmd->satacmd_cmd_reg = SATAC_PACKET;	/* Command */
8533 
8534 	/*
8535 	 * We want all data to be transfered via DMA.
8536 	 * But specify it only if drive supports DMA and DMA mode is
8537 	 * selected - some drives are sensitive about it.
8538 	 * Hopefully it wil work for all drives....
8539 	 */
8540 	if (sdinfo->satadrv_settings & SATA_DEV_DMA)
8541 		scmd->satacmd_features_reg = SATA_ATAPI_F_DMA;
8542 
8543 	/*
8544 	 * Features register requires special care for devices that use
8545 	 * Serial ATA bridge - they need an explicit specification of
8546 	 * the data transfer direction for Packet DMA commands.
8547 	 * Setting this bit is harmless if DMA is not used.
8548 	 *
8549 	 * Many drives do not implement word 80, specifying what ATA/ATAPI
8550 	 * spec they follow.
8551 	 * We are arbitrarily following the latest SerialATA 2.6 spec,
8552 	 * which uses ATA/ATAPI 6 specification for Identify Data, unless
8553 	 * ATA/ATAPI-7 support is explicitly indicated.
8554 	 */
8555 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
8556 	    sdinfo->satadrv_id.ai_majorversion != 0xffff &&
8557 	    (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) {
8558 		/*
8559 		 * Specification of major version is valid and version 7
8560 		 * is supported. It does automatically imply that all
8561 		 * spec features are supported. For now, we assume that
8562 		 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete.
8563 		 */
8564 		if ((sdinfo->satadrv_id.ai_dirdma &
8565 		    SATA_ATAPI_ID_DMADIR_REQ) != 0) {
8566 			if (scmd->satacmd_flags.sata_data_direction ==
8567 			    SATA_DIR_READ)
8568 			scmd->satacmd_features_reg |=
8569 			    SATA_ATAPI_F_DATA_DIR_READ;
8570 		}
8571 	}
8572 }
8573 
8574 
8575 #ifdef SATA_DEBUG
8576 
8577 /* Display 18 bytes of Inquiry data */
8578 static void
8579 sata_show_inqry_data(uint8_t *buf)
8580 {
8581 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
8582 	uint8_t *p;
8583 
8584 	cmn_err(CE_NOTE, "Inquiry data:");
8585 	cmn_err(CE_NOTE, "device type %x", inq->inq_dtype);
8586 	cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb);
8587 	cmn_err(CE_NOTE, "version %x", inq->inq_ansi);
8588 	cmn_err(CE_NOTE, "ATAPI transport version %d",
8589 	    SATA_ATAPI_TRANS_VERSION(inq));
8590 	cmn_err(CE_NOTE, "response data format %d, aenc %d",
8591 	    inq->inq_rdf, inq->inq_aenc);
8592 	cmn_err(CE_NOTE, " additional length %d", inq->inq_len);
8593 	cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs);
8594 	p = (uint8_t *)inq->inq_vid;
8595 	cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x "
8596 	    "%02x %02x %02x %02x",
8597 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
8598 	p = (uint8_t *)inq->inq_vid;
8599 	cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c",
8600 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
8601 
8602 	p = (uint8_t *)inq->inq_pid;
8603 	cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x "
8604 	    "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
8605 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
8606 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
8607 	p = (uint8_t *)inq->inq_pid;
8608 	cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c "
8609 	    "%c %c %c %c %c %c %c %c",
8610 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
8611 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
8612 
8613 	p = (uint8_t *)inq->inq_revision;
8614 	cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x",
8615 	    p[0], p[1], p[2], p[3]);
8616 	p = (uint8_t *)inq->inq_revision;
8617 	cmn_err(CE_NOTE, "revision: %c %c %c %c",
8618 	    p[0], p[1], p[2], p[3]);
8619 
8620 }
8621 
8622 
8623 static void
8624 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count)
8625 {
8626 	struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt;
8627 
8628 	if (scsi_pkt == NULL)
8629 		return;
8630 	if (count != 0) {
8631 		/* saving cdb */
8632 		bzero(sata_atapi_trace[sata_atapi_trace_index].acdb,
8633 		    SATA_ATAPI_MAX_CDB_LEN);
8634 		bcopy(scsi_pkt->pkt_cdbp,
8635 		    sata_atapi_trace[sata_atapi_trace_index].acdb, count);
8636 	} else {
8637 		bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)->
8638 		    sts_sensedata,
8639 		    sata_atapi_trace[sata_atapi_trace_index].arqs,
8640 		    SATA_ATAPI_MIN_RQSENSE_LEN);
8641 		sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason =
8642 		    scsi_pkt->pkt_reason;
8643 		sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason =
8644 		    spx->txlt_sata_pkt->satapkt_reason;
8645 
8646 		if (++sata_atapi_trace_index >= 64)
8647 			sata_atapi_trace_index = 0;
8648 	}
8649 }
8650 
8651 #endif
8652 
8653 /*
8654  * Fetch inquiry data from ATAPI device
8655  * Returns SATA_SUCCESS if operation was successfull, SATA_FAILURE otherwise.
8656  *
8657  * Note:
8658  * inqb pointer does not point to a DMA-able buffer. It is a local buffer
8659  * where the caller expects to see the inquiry data.
8660  *
8661  */
8662 
8663 static int
8664 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba,
8665     sata_address_t *saddr, struct scsi_inquiry *inq)
8666 {
8667 	sata_pkt_txlate_t *spx;
8668 	sata_pkt_t *spkt;
8669 	struct buf *bp;
8670 	sata_drive_info_t *sdinfo;
8671 	sata_cmd_t *scmd;
8672 	int rval;
8673 	uint8_t *rqsp;
8674 #ifdef SATA_DEBUG
8675 	char msg_buf[MAXPATHLEN];
8676 #endif
8677 
8678 	ASSERT(sata_hba != NULL);
8679 
8680 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
8681 	spx->txlt_sata_hba_inst = sata_hba;
8682 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
8683 	spkt = sata_pkt_alloc(spx, NULL);
8684 	if (spkt == NULL) {
8685 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
8686 		return (SATA_FAILURE);
8687 	}
8688 	/* address is needed now */
8689 	spkt->satapkt_device.satadev_addr = *saddr;
8690 
8691 	/* scsi_inquiry size buffer */
8692 	bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry));
8693 	if (bp == NULL) {
8694 		sata_pkt_free(spx);
8695 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
8696 		SATA_LOG_D((sata_hba, CE_WARN,
8697 		    "sata_get_atapi_inquiry_data: "
8698 		    "cannot allocate data buffer"));
8699 		return (SATA_FAILURE);
8700 	}
8701 	bp_mapin(bp); /* make data buffer accessible */
8702 
8703 	scmd = &spkt->satapkt_cmd;
8704 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
8705 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
8706 
8707 	/* Use synchronous mode */
8708 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
8709 	spkt->satapkt_comp = NULL;
8710 	spkt->satapkt_time = sata_default_pkt_time;
8711 
8712 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
8713 
8714 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
8715 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
8716 
8717 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
8718 	sdinfo = sata_get_device_info(sata_hba,
8719 	    &spx->txlt_sata_pkt->satapkt_device);
8720 	if (sdinfo == NULL) {
8721 		/* we have to be carefull about the disapearing device */
8722 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
8723 		rval = SATA_FAILURE;
8724 		goto cleanup;
8725 	}
8726 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
8727 
8728 	/*
8729 	 * Set-up acdb. This works for atapi transport version 2 and later.
8730 	 */
8731 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
8732 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
8733 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
8734 	scmd->satacmd_acdb[1] = 0x00;
8735 	scmd->satacmd_acdb[2] = 0x00;
8736 	scmd->satacmd_acdb[3] = 0x00;
8737 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
8738 	scmd->satacmd_acdb[5] = 0x00;
8739 
8740 	sata_fixed_sense_data_preset(
8741 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
8742 
8743 	/* Transfer command to HBA */
8744 	if (sata_hba_start(spx, &rval) != 0) {
8745 		/* Pkt not accepted for execution */
8746 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
8747 		    "sata_get_atapi_inquiry_data: "
8748 		    "Packet not accepted for execution - ret: %02x", rval);
8749 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
8750 		rval = SATA_FAILURE;
8751 		goto cleanup;
8752 	}
8753 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
8754 
8755 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
8756 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
8757 		    "sata_get_atapi_inquiry_data: "
8758 		    "Packet completed successfully - ret: %02x", rval);
8759 		if (spx->txlt_buf_dma_handle != NULL) {
8760 			/*
8761 			 * Sync buffer. Handle is in usual place in translate
8762 			 * struct.
8763 			 */
8764 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
8765 			    DDI_DMA_SYNC_FORCPU);
8766 			ASSERT(rval == DDI_SUCCESS);
8767 		}
8768 		/*
8769 		 * Normal completion - copy data into caller's buffer
8770 		 */
8771 		bcopy(bp->b_un.b_addr, (uint8_t *)inq,
8772 		    sizeof (struct scsi_inquiry));
8773 #ifdef SATA_DEBUG
8774 		if (sata_debug_flags & SATA_DBG_ATAPI) {
8775 			sata_show_inqry_data((uint8_t *)inq);
8776 		}
8777 #endif
8778 		rval = SATA_SUCCESS;
8779 	} else {
8780 		/*
8781 		 * Something went wrong - analyze return - check rqsense data
8782 		 */
8783 		rval = SATA_FAILURE;
8784 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
8785 			/*
8786 			 * ARQ data hopefull show something other than NO SENSE
8787 			 */
8788 			rqsp = scmd->satacmd_rqsense;
8789 #ifdef SATA_DEBUG
8790 			if (sata_debug_flags & SATA_DBG_ATAPI) {
8791 				msg_buf[0] = '\0';
8792 				(void) snprintf(msg_buf, MAXPATHLEN,
8793 				    "ATAPI packet completion reason: %02x\n"
8794 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x\n"
8795 				    "          %02x %02x %02x %02x %02x %02x\n"
8796 				    "          %02x %02x %02x %02x %02x %02x",
8797 				    spkt->satapkt_reason,
8798 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
8799 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
8800 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
8801 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
8802 				    rqsp[16], rqsp[17]);
8803 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
8804 				    "%s", msg_buf);
8805 			}
8806 #endif
8807 		} else {
8808 			switch (spkt->satapkt_reason) {
8809 			case SATA_PKT_PORT_ERROR:
8810 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
8811 				    "sata_get_atapi_inquiry_data: "
8812 				    "packet reason: port error", NULL);
8813 				break;
8814 
8815 			case SATA_PKT_TIMEOUT:
8816 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
8817 				    "sata_get_atapi_inquiry_data: "
8818 				    "packet reason: timeout", NULL);
8819 				break;
8820 
8821 			case SATA_PKT_ABORTED:
8822 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
8823 				    "sata_get_atapi_inquiry_data: "
8824 				    "packet reason: aborted", NULL);
8825 				break;
8826 
8827 			case SATA_PKT_RESET:
8828 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
8829 				    "sata_get_atapi_inquiry_data: "
8830 				    "packet reason: reset\n", NULL);
8831 				break;
8832 			default:
8833 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
8834 				    "sata_get_atapi_inquiry_data: "
8835 				    "invalid packet reason: %02x\n",
8836 				    spkt->satapkt_reason);
8837 				break;
8838 			}
8839 		}
8840 	}
8841 cleanup:
8842 	sata_free_local_buffer(spx);
8843 	sata_pkt_free(spx);
8844 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
8845 	return (rval);
8846 }
8847 
8848 
8849 
8850 
8851 
8852 #if 0
8853 #ifdef SATA_DEBUG
8854 
8855 /*
8856  * Test ATAPI packet command.
8857  * Single threaded test: send packet command in synch mode, process completion
8858  *
8859  */
8860 static void
8861 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport)
8862 {
8863 	sata_pkt_txlate_t *spx;
8864 	sata_pkt_t *spkt;
8865 	struct buf *bp;
8866 	sata_device_t sata_device;
8867 	sata_drive_info_t *sdinfo;
8868 	sata_cmd_t *scmd;
8869 	int rval;
8870 	uint8_t *rqsp;
8871 
8872 	ASSERT(sata_hba_inst != NULL);
8873 	sata_device.satadev_addr.cport = cport;
8874 	sata_device.satadev_addr.pmport = 0;
8875 	sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
8876 	sata_device.satadev_rev = SATA_DEVICE_REV;
8877 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8878 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
8879 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8880 	if (sdinfo == NULL) {
8881 		sata_log(sata_hba_inst, CE_WARN,
8882 		    "sata_test_atapi_packet_command: "
8883 		    "no device info for cport %d",
8884 		    sata_device.satadev_addr.cport);
8885 		return;
8886 	}
8887 
8888 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
8889 	spx->txlt_sata_hba_inst = sata_hba_inst;
8890 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
8891 	spkt = sata_pkt_alloc(spx, NULL);
8892 	if (spkt == NULL) {
8893 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
8894 		return;
8895 	}
8896 	/* address is needed now */
8897 	spkt->satapkt_device.satadev_addr = sata_device.satadev_addr;
8898 
8899 	/* 1024k buffer */
8900 	bp = sata_alloc_local_buffer(spx, 1024);
8901 	if (bp == NULL) {
8902 		sata_pkt_free(spx);
8903 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
8904 		sata_log(sata_hba_inst, CE_WARN,
8905 		    "sata_test_atapi_packet_command: "
8906 		    "cannot allocate data buffer");
8907 		return;
8908 	}
8909 	bp_mapin(bp); /* make data buffer accessible */
8910 
8911 	scmd = &spkt->satapkt_cmd;
8912 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
8913 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
8914 
8915 	/* Use synchronous mode */
8916 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
8917 
8918 	/* Synchronous mode, no callback - may be changed by the caller */
8919 	spkt->satapkt_comp = NULL;
8920 	spkt->satapkt_time = sata_default_pkt_time;
8921 
8922 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
8923 
8924 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
8925 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
8926 
8927 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
8928 
8929 	/* Set-up acdb. */
8930 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
8931 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
8932 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
8933 	scmd->satacmd_acdb[1] = 0x00;
8934 	scmd->satacmd_acdb[2] = 0x00;
8935 	scmd->satacmd_acdb[3] = 0x00;
8936 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
8937 	scmd->satacmd_acdb[5] = 0x00;
8938 
8939 	sata_fixed_sense_data_preset(
8940 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
8941 
8942 	/* Transfer command to HBA */
8943 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8944 	if (sata_hba_start(spx, &rval) != 0) {
8945 		/* Pkt not accepted for execution */
8946 		sata_log(sata_hba_inst, CE_WARN,
8947 		    "sata_test_atapi_packet_command: "
8948 		    "Packet not accepted for execution - ret: %02x", rval);
8949 		mutex_exit(
8950 		    &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8951 		goto cleanup;
8952 	}
8953 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8954 
8955 	if (spx->txlt_buf_dma_handle != NULL) {
8956 		/*
8957 		 * Sync buffer. Handle is in usual place in translate struct.
8958 		 */
8959 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
8960 		    DDI_DMA_SYNC_FORCPU);
8961 		ASSERT(rval == DDI_SUCCESS);
8962 	}
8963 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
8964 		sata_log(sata_hba_inst, CE_WARN,
8965 		    "sata_test_atapi_packet_command: "
8966 		    "Packet completed successfully");
8967 		/*
8968 		 * Normal completion - show inquiry data
8969 		 */
8970 		sata_show_inqry_data((uint8_t *)bp->b_un.b_addr);
8971 	} else {
8972 		/*
8973 		 * Something went wrong - analyze return - check rqsense data
8974 		 */
8975 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
8976 			/*
8977 			 * ARQ data hopefull show something other than NO SENSE
8978 			 */
8979 			rqsp = scmd->satacmd_rqsense;
8980 			sata_log(spx->txlt_sata_hba_inst, CE_WARN,
8981 			    "ATAPI packet completion reason: %02x\n"
8982 			    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
8983 			    "          %02x %02x %02x %02x %02x %02x "
8984 			    "          %02x %02x %02x %02x %02x %02x\n",
8985 			    spkt->satapkt_reason,
8986 			    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
8987 			    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
8988 			    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
8989 			    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
8990 			    rqsp[16], rqsp[17]);
8991 		} else {
8992 			switch (spkt->satapkt_reason) {
8993 			case SATA_PKT_PORT_ERROR:
8994 				sata_log(sata_hba_inst, CE_WARN,
8995 				    "sata_test_atapi_packet_command: "
8996 				    "packet reason: port error\n");
8997 				break;
8998 
8999 			case SATA_PKT_TIMEOUT:
9000 				sata_log(sata_hba_inst, CE_WARN,
9001 				    "sata_test_atapi_packet_command: "
9002 				    "packet reason: timeout\n");
9003 				break;
9004 
9005 			case SATA_PKT_ABORTED:
9006 				sata_log(sata_hba_inst, CE_WARN,
9007 				    "sata_test_atapi_packet_command: "
9008 				    "packet reason: aborted\n");
9009 				break;
9010 
9011 			case SATA_PKT_RESET:
9012 				sata_log(sata_hba_inst, CE_WARN,
9013 				    "sata_test_atapi_packet_command: "
9014 				    "packet reason: reset\n");
9015 				break;
9016 			default:
9017 				sata_log(sata_hba_inst, CE_WARN,
9018 				    "sata_test_atapi_packet_command: "
9019 				    "invalid packet reason: %02x\n",
9020 				    spkt->satapkt_reason);
9021 				break;
9022 			}
9023 		}
9024 	}
9025 cleanup:
9026 	sata_free_local_buffer(spx);
9027 	sata_pkt_free(spx);
9028 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
9029 }
9030 
9031 #endif /* SATA_DEBUG */
9032 #endif /* 1 */
9033 
9034 
9035 /* ************************** LOCAL HELPER FUNCTIONS *********************** */
9036 
9037 /*
9038  * Validate sata_tran info
9039  * SATA_FAILURE returns if structure is inconsistent or structure revision
9040  * does not match one used by the framework.
9041  *
9042  * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains
9043  * required function pointers.
9044  * Returns SATA_FAILURE otherwise.
9045  */
9046 static int
9047 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran)
9048 {
9049 	/*
9050 	 * SATA_TRAN_HBA_REV is the current (highest) revision number
9051 	 * of the SATA interface.
9052 	 */
9053 	if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) {
9054 		sata_log(NULL, CE_WARN,
9055 		    "sata: invalid sata_hba_tran version %d for driver %s",
9056 		    sata_tran->sata_tran_hba_rev, ddi_driver_name(dip));
9057 		return (SATA_FAILURE);
9058 	}
9059 
9060 	if (dip != sata_tran->sata_tran_hba_dip) {
9061 		SATA_LOG_D((NULL, CE_WARN,
9062 		    "sata: inconsistent sata_tran_hba_dip "
9063 		    "%p / %p", sata_tran->sata_tran_hba_dip, dip));
9064 		return (SATA_FAILURE);
9065 	}
9066 
9067 	if (sata_tran->sata_tran_probe_port == NULL ||
9068 	    sata_tran->sata_tran_start == NULL ||
9069 	    sata_tran->sata_tran_abort == NULL ||
9070 	    sata_tran->sata_tran_reset_dport == NULL ||
9071 	    sata_tran->sata_tran_hotplug_ops == NULL ||
9072 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL ||
9073 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate ==
9074 	    NULL) {
9075 		SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing "
9076 		    "required functions"));
9077 	}
9078 	return (SATA_SUCCESS);
9079 }
9080 
9081 /*
9082  * Remove HBA instance from sata_hba_list.
9083  */
9084 static void
9085 sata_remove_hba_instance(dev_info_t *dip)
9086 {
9087 	sata_hba_inst_t	*sata_hba_inst;
9088 
9089 	mutex_enter(&sata_mutex);
9090 	for (sata_hba_inst = sata_hba_list;
9091 	    sata_hba_inst != (struct sata_hba_inst *)NULL;
9092 	    sata_hba_inst = sata_hba_inst->satahba_next) {
9093 		if (sata_hba_inst->satahba_dip == dip)
9094 			break;
9095 	}
9096 
9097 	if (sata_hba_inst == (struct sata_hba_inst *)NULL) {
9098 #ifdef SATA_DEBUG
9099 		cmn_err(CE_WARN, "sata_remove_hba_instance: "
9100 		    "unknown HBA instance\n");
9101 #endif
9102 		ASSERT(FALSE);
9103 	}
9104 	if (sata_hba_inst == sata_hba_list) {
9105 		sata_hba_list = sata_hba_inst->satahba_next;
9106 		if (sata_hba_list) {
9107 			sata_hba_list->satahba_prev =
9108 			    (struct sata_hba_inst *)NULL;
9109 		}
9110 		if (sata_hba_inst == sata_hba_list_tail) {
9111 			sata_hba_list_tail = NULL;
9112 		}
9113 	} else if (sata_hba_inst == sata_hba_list_tail) {
9114 		sata_hba_list_tail = sata_hba_inst->satahba_prev;
9115 		if (sata_hba_list_tail) {
9116 			sata_hba_list_tail->satahba_next =
9117 			    (struct sata_hba_inst *)NULL;
9118 		}
9119 	} else {
9120 		sata_hba_inst->satahba_prev->satahba_next =
9121 		    sata_hba_inst->satahba_next;
9122 		sata_hba_inst->satahba_next->satahba_prev =
9123 		    sata_hba_inst->satahba_prev;
9124 	}
9125 	mutex_exit(&sata_mutex);
9126 }
9127 
9128 /*
9129  * Probe all SATA ports of the specified HBA instance.
9130  * The assumption is that there are no target and attachment point minor nodes
9131  * created by the boot subsystems, so we do not need to prune device tree.
9132  *
9133  * This function is called only from sata_hba_attach(). It does not have to
9134  * be protected by controller mutex, because the hba_attached flag is not set
9135  * yet and no one would be touching this HBA instance other than this thread.
9136  * Determines if port is active and what type of the device is attached
9137  * (if any). Allocates necessary structures for each port.
9138  *
9139  * An AP (Attachement Point) node is created for each SATA device port even
9140  * when there is no device attached.
9141  */
9142 
9143 static 	void
9144 sata_probe_ports(sata_hba_inst_t *sata_hba_inst)
9145 {
9146 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
9147 	int			ncport;
9148 	sata_cport_info_t 	*cportinfo;
9149 	sata_drive_info_t	*drive;
9150 	sata_device_t		sata_device;
9151 	int			rval;
9152 	dev_t			minor_number;
9153 	char			name[16];
9154 	clock_t			start_time, cur_time;
9155 
9156 	/*
9157 	 * Probe controller ports first, to find port status and
9158 	 * any port multiplier attached.
9159 	 */
9160 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
9161 		/* allocate cport structure */
9162 		cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP);
9163 		ASSERT(cportinfo != NULL);
9164 		mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL);
9165 
9166 		mutex_enter(&cportinfo->cport_mutex);
9167 
9168 		cportinfo->cport_addr.cport = ncport;
9169 		cportinfo->cport_addr.pmport = 0;
9170 		cportinfo->cport_addr.qual = SATA_ADDR_CPORT;
9171 		cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
9172 		cportinfo->cport_state |= SATA_STATE_PROBING;
9173 		SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo;
9174 
9175 		/*
9176 		 * Regardless if a port is usable or not, create
9177 		 * an attachment point
9178 		 */
9179 		mutex_exit(&cportinfo->cport_mutex);
9180 		minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip),
9181 		    ncport, 0, SATA_ADDR_CPORT);
9182 		(void) sprintf(name, "%d", ncport);
9183 		if (ddi_create_minor_node(dip, name, S_IFCHR,
9184 		    minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) !=
9185 		    DDI_SUCCESS) {
9186 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
9187 			    "cannot create SATA attachment point for port %d",
9188 			    ncport);
9189 		}
9190 
9191 		/* Probe port */
9192 		start_time = ddi_get_lbolt();
9193 	reprobe_cport:
9194 		sata_device.satadev_addr.cport = ncport;
9195 		sata_device.satadev_addr.pmport = 0;
9196 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
9197 		sata_device.satadev_rev = SATA_DEVICE_REV;
9198 
9199 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
9200 		    (dip, &sata_device);
9201 
9202 		mutex_enter(&cportinfo->cport_mutex);
9203 		cportinfo->cport_scr = sata_device.satadev_scr;
9204 		if (rval != SATA_SUCCESS) {
9205 			/* Something went wrong? Fail the port */
9206 			cportinfo->cport_state = SATA_PSTATE_FAILED;
9207 			mutex_exit(&cportinfo->cport_mutex);
9208 			continue;
9209 		}
9210 		cportinfo->cport_state &= ~SATA_STATE_PROBING;
9211 		cportinfo->cport_state |= SATA_STATE_PROBED;
9212 		cportinfo->cport_dev_type = sata_device.satadev_type;
9213 
9214 		cportinfo->cport_state |= SATA_STATE_READY;
9215 		if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) {
9216 			mutex_exit(&cportinfo->cport_mutex);
9217 			continue;
9218 		}
9219 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
9220 			/*
9221 			 * There is some device attached.
9222 			 * Allocate device info structure
9223 			 */
9224 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) {
9225 				mutex_exit(&cportinfo->cport_mutex);
9226 				SATA_CPORTINFO_DRV_INFO(cportinfo) =
9227 				    kmem_zalloc(sizeof (sata_drive_info_t),
9228 				    KM_SLEEP);
9229 				mutex_enter(&cportinfo->cport_mutex);
9230 			}
9231 			drive = SATA_CPORTINFO_DRV_INFO(cportinfo);
9232 			drive->satadrv_addr = cportinfo->cport_addr;
9233 			drive->satadrv_addr.qual = SATA_ADDR_DCPORT;
9234 			drive->satadrv_type = cportinfo->cport_dev_type;
9235 			drive->satadrv_state = SATA_STATE_UNKNOWN;
9236 
9237 			mutex_exit(&cportinfo->cport_mutex);
9238 			if (sata_add_device(dip, sata_hba_inst, &sata_device) !=
9239 			    SATA_SUCCESS) {
9240 				/*
9241 				 * Plugged device was not correctly identified.
9242 				 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT
9243 				 */
9244 				cur_time = ddi_get_lbolt();
9245 				if ((cur_time - start_time) <
9246 				    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
9247 					/* sleep for a while */
9248 					delay(drv_usectohz(
9249 					    SATA_DEV_RETRY_DLY));
9250 					goto reprobe_cport;
9251 				}
9252 			}
9253 		} else { /* SATA_DTYPE_PMULT */
9254 			mutex_exit(&cportinfo->cport_mutex);
9255 
9256 			/* Allocate sata_pmult_info and sata_pmport_info */
9257 			if (sata_alloc_pmult(sata_hba_inst, &sata_device) !=
9258 			    SATA_SUCCESS)
9259 				continue;
9260 
9261 			/* Log the information of the port multiplier */
9262 			sata_show_pmult_info(sata_hba_inst, &sata_device);
9263 
9264 			/* Probe its pmports */
9265 			sata_probe_pmports(sata_hba_inst, ncport);
9266 		}
9267 	}
9268 }
9269 
9270 /*
9271  * Probe all device ports behind a port multiplier.
9272  *
9273  * PMult-related structure should be allocated before by sata_alloc_pmult().
9274  *
9275  * NOTE1: Only called from sata_probe_ports()
9276  * NOTE2: No mutex should be hold.
9277  */
9278 static void
9279 sata_probe_pmports(sata_hba_inst_t *sata_hba_inst, uint8_t ncport)
9280 {
9281 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
9282 	sata_pmult_info_t	*pmultinfo = NULL;
9283 	sata_pmport_info_t 	*pmportinfo = NULL;
9284 	sata_drive_info_t	*drive = NULL;
9285 	sata_device_t		sata_device;
9286 
9287 	clock_t			start_time, cur_time;
9288 	int			npmport;
9289 	int			rval;
9290 
9291 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, ncport);
9292 
9293 	/* Probe Port Multiplier ports */
9294 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; npmport++) {
9295 		pmportinfo = pmultinfo->pmult_dev_port[npmport];
9296 		start_time = ddi_get_lbolt();
9297 reprobe_pmport:
9298 		sata_device.satadev_addr.cport = ncport;
9299 		sata_device.satadev_addr.pmport = npmport;
9300 		sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
9301 		sata_device.satadev_rev = SATA_DEVICE_REV;
9302 
9303 		/* Let HBA driver probe it. */
9304 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
9305 		    (dip, &sata_device);
9306 		mutex_enter(&pmportinfo->pmport_mutex);
9307 
9308 		pmportinfo->pmport_scr = sata_device.satadev_scr;
9309 
9310 		if (rval != SATA_SUCCESS) {
9311 			pmportinfo->pmport_state =
9312 			    SATA_PSTATE_FAILED;
9313 			mutex_exit(&pmportinfo->pmport_mutex);
9314 			continue;
9315 		}
9316 		pmportinfo->pmport_state &= ~SATA_STATE_PROBING;
9317 		pmportinfo->pmport_state |= SATA_STATE_PROBED;
9318 		pmportinfo->pmport_dev_type = sata_device.satadev_type;
9319 
9320 		pmportinfo->pmport_state |= SATA_STATE_READY;
9321 		if (pmportinfo->pmport_dev_type ==
9322 		    SATA_DTYPE_NONE) {
9323 			SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
9324 			    "no device found at port %d:%d", ncport, npmport);
9325 			mutex_exit(&pmportinfo->pmport_mutex);
9326 			continue;
9327 		}
9328 		/* Port multipliers cannot be chained */
9329 		ASSERT(pmportinfo->pmport_dev_type != SATA_DTYPE_PMULT);
9330 		/*
9331 		 * There is something attached to Port
9332 		 * Multiplier device port
9333 		 * Allocate device info structure
9334 		 */
9335 		if (pmportinfo->pmport_sata_drive == NULL) {
9336 			mutex_exit(&pmportinfo->pmport_mutex);
9337 			pmportinfo->pmport_sata_drive =
9338 			    kmem_zalloc(sizeof (sata_drive_info_t), KM_SLEEP);
9339 			mutex_enter(&pmportinfo->pmport_mutex);
9340 		}
9341 		drive = pmportinfo->pmport_sata_drive;
9342 		drive->satadrv_addr.cport = pmportinfo->pmport_addr.cport;
9343 		drive->satadrv_addr.pmport = npmport;
9344 		drive->satadrv_addr.qual = SATA_ADDR_DPMPORT;
9345 		drive->satadrv_type = pmportinfo-> pmport_dev_type;
9346 		drive->satadrv_state = SATA_STATE_UNKNOWN;
9347 
9348 		mutex_exit(&pmportinfo->pmport_mutex);
9349 		rval = sata_add_device(dip, sata_hba_inst, &sata_device);
9350 
9351 		if (rval != SATA_SUCCESS) {
9352 			/*
9353 			 * Plugged device was not correctly identified.
9354 			 * Retry, within the SATA_DEV_IDENTIFY_TIMEOUT
9355 			 */
9356 			cur_time = ddi_get_lbolt();
9357 			if ((cur_time - start_time) < drv_usectohz(
9358 			    SATA_DEV_IDENTIFY_TIMEOUT)) {
9359 				/* sleep for a while */
9360 				delay(drv_usectohz(SATA_DEV_RETRY_DLY));
9361 				goto reprobe_pmport;
9362 			}
9363 		}
9364 	}
9365 }
9366 
9367 /*
9368  * Add SATA device for specified HBA instance & port (SCSI target
9369  * device nodes).
9370  * This function is called (indirectly) only from sata_hba_attach().
9371  * A target node is created when there is a supported type device attached,
9372  * but may be removed if it cannot be put online.
9373  *
9374  * This function cannot be called from an interrupt context.
9375  *
9376  * Create target nodes for disk, CD/DVD, Tape and ATAPI disk devices
9377  *
9378  * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when
9379  * device identification failed - adding a device could be retried.
9380  *
9381  */
9382 static 	int
9383 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst,
9384     sata_device_t *sata_device)
9385 {
9386 	sata_cport_info_t 	*cportinfo;
9387 	sata_pmult_info_t	*pminfo;
9388 	sata_pmport_info_t	*pmportinfo;
9389 	dev_info_t		*cdip;		/* child dip */
9390 	sata_address_t		*saddr = &sata_device->satadev_addr;
9391 	uint8_t			cport, pmport;
9392 	int			rval;
9393 
9394 	cport = saddr->cport;
9395 	pmport = saddr->pmport;
9396 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
9397 	ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE);
9398 
9399 	/*
9400 	 * Some device is attached to a controller port.
9401 	 * We rely on controllers distinquishing between no-device,
9402 	 * attached port multiplier and other kind of attached device.
9403 	 * We need to get Identify Device data and determine
9404 	 * positively the dev type before trying to attach
9405 	 * the target driver.
9406 	 */
9407 	sata_device->satadev_rev = SATA_DEVICE_REV;
9408 	switch (saddr->qual) {
9409 	case SATA_ADDR_CPORT:
9410 		/*
9411 		 * Add a non-port-multiplier device at controller port.
9412 		 */
9413 		saddr->qual = SATA_ADDR_DCPORT;
9414 
9415 		rval = sata_probe_device(sata_hba_inst, sata_device);
9416 		if (rval != SATA_SUCCESS ||
9417 		    sata_device->satadev_type == SATA_DTYPE_UNKNOWN)
9418 			return (SATA_FAILURE);
9419 
9420 		mutex_enter(&cportinfo->cport_mutex);
9421 		sata_show_drive_info(sata_hba_inst,
9422 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
9423 
9424 		if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) {
9425 			/*
9426 			 * Could not determine device type or
9427 			 * a device is not supported.
9428 			 * Degrade this device to unknown.
9429 			 */
9430 			cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
9431 			mutex_exit(&cportinfo->cport_mutex);
9432 			return (SATA_SUCCESS);
9433 		}
9434 		cportinfo->cport_dev_type = sata_device->satadev_type;
9435 		cportinfo->cport_tgtnode_clean = B_TRUE;
9436 		mutex_exit(&cportinfo->cport_mutex);
9437 
9438 		/*
9439 		 * Initialize device to the desired state. Even if it
9440 		 * fails, the device will still attach but syslog
9441 		 * will show the warning.
9442 		 */
9443 		if (sata_initialize_device(sata_hba_inst,
9444 		    SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) {
9445 			/* Retry */
9446 			rval = sata_initialize_device(sata_hba_inst,
9447 			    SATA_CPORTINFO_DRV_INFO(cportinfo));
9448 
9449 			if (rval == SATA_RETRY)
9450 				sata_log(sata_hba_inst, CE_WARN,
9451 				    "SATA device at port %d - "
9452 				    "default device features could not be set."
9453 				    " Device may not operate as expected.",
9454 				    cport);
9455 		}
9456 
9457 		cdip = sata_create_target_node(pdip, sata_hba_inst, saddr);
9458 		if (cdip == NULL) {
9459 			/*
9460 			 * Attaching target node failed.
9461 			 * We retain sata_drive_info structure...
9462 			 */
9463 			return (SATA_SUCCESS);
9464 		}
9465 
9466 		mutex_enter(&cportinfo->cport_mutex);
9467 		(SATA_CPORTINFO_DRV_INFO(cportinfo))->
9468 		    satadrv_state = SATA_STATE_READY;
9469 		mutex_exit(&cportinfo->cport_mutex);
9470 
9471 		break;
9472 
9473 	case SATA_ADDR_PMPORT:
9474 		saddr->qual = SATA_ADDR_DPMPORT;
9475 
9476 		mutex_enter(&cportinfo->cport_mutex);
9477 		/* It must be a Port Multiplier at the controller port */
9478 		ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT);
9479 
9480 		pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
9481 		pmportinfo = pminfo->pmult_dev_port[saddr->pmport];
9482 		mutex_exit(&cportinfo->cport_mutex);
9483 
9484 		rval = sata_probe_device(sata_hba_inst, sata_device);
9485 		if (rval != SATA_SUCCESS ||
9486 		    sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
9487 			return (SATA_FAILURE);
9488 		}
9489 
9490 		mutex_enter(&pmportinfo->pmport_mutex);
9491 		sata_show_drive_info(sata_hba_inst,
9492 		    SATA_PMPORTINFO_DRV_INFO(pmportinfo));
9493 
9494 		if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) {
9495 			/*
9496 			 * Could not determine device type.
9497 			 * Degrade this device to unknown.
9498 			 */
9499 			pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
9500 			mutex_exit(&pmportinfo->pmport_mutex);
9501 			return (SATA_SUCCESS);
9502 		}
9503 		pmportinfo->pmport_dev_type = sata_device->satadev_type;
9504 		pmportinfo->pmport_tgtnode_clean = B_TRUE;
9505 		mutex_exit(&pmportinfo->pmport_mutex);
9506 
9507 		/*
9508 		 * Initialize device to the desired state.
9509 		 * Even if it fails, the device will still
9510 		 * attach but syslog will show the warning.
9511 		 */
9512 		if (sata_initialize_device(sata_hba_inst,
9513 		    pmportinfo->pmport_sata_drive) != SATA_SUCCESS) {
9514 			/* Retry */
9515 			rval = sata_initialize_device(sata_hba_inst,
9516 			    pmportinfo->pmport_sata_drive);
9517 
9518 			if (rval == SATA_RETRY)
9519 				sata_log(sata_hba_inst, CE_WARN,
9520 				    "SATA device at port %d:%d - "
9521 				    "default device features could not be set."
9522 				    " Device may not operate as expected.",
9523 				    cport, pmport);
9524 		}
9525 
9526 		cdip = sata_create_target_node(pdip, sata_hba_inst, saddr);
9527 		if (cdip == NULL) {
9528 			/*
9529 			 * Attaching target node failed.
9530 			 * We retain sata_drive_info structure...
9531 			 */
9532 			return (SATA_SUCCESS);
9533 		}
9534 		mutex_enter(&pmportinfo->pmport_mutex);
9535 		pmportinfo->pmport_sata_drive->satadrv_state |=
9536 		    SATA_STATE_READY;
9537 		mutex_exit(&pmportinfo->pmport_mutex);
9538 
9539 		break;
9540 
9541 	default:
9542 		return (SATA_FAILURE);
9543 	}
9544 
9545 	return (SATA_SUCCESS);
9546 }
9547 
9548 /*
9549  * Clean up target node at specific address.
9550  *
9551  * NOTE: No Mutex should be hold.
9552  */
9553 static int
9554 sata_offline_device(sata_hba_inst_t *sata_hba_inst,
9555     sata_device_t *sata_device, sata_drive_info_t *sdinfo)
9556 {
9557 	uint8_t cport, pmport, qual;
9558 	dev_info_t *tdip;
9559 
9560 	cport = sata_device->satadev_addr.cport;
9561 	pmport = sata_device->satadev_addr.pmport;
9562 	qual = sata_device->satadev_addr.qual;
9563 
9564 	if (qual == SATA_ADDR_DCPORT) {
9565 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9566 		    "sata_hba_ioctl: disconnect device at port %d", cport));
9567 	} else {
9568 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9569 		    "sata_hba_ioctl: disconnect device at port %d:%d",
9570 		    cport, pmport));
9571 	}
9572 
9573 	/* We are addressing attached device, not a port */
9574 	sata_device->satadev_addr.qual =
9575 	    sdinfo->satadrv_addr.qual;
9576 	tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
9577 	    &sata_device->satadev_addr);
9578 	if (tdip != NULL && ndi_devi_offline(tdip,
9579 	    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
9580 		/*
9581 		 * Problem :
9582 		 * The target node remained attached.
9583 		 * This happens when the device file was open
9584 		 * or a node was waiting for resources.
9585 		 * Cannot do anything about it.
9586 		 */
9587 		if (qual == SATA_ADDR_DCPORT) {
9588 			SATA_LOG_D((sata_hba_inst, CE_WARN,
9589 			    "sata_hba_ioctl: disconnect: could "
9590 			    "not unconfigure device before "
9591 			    "disconnecting the SATA port %d",
9592 			    cport));
9593 		} else {
9594 			SATA_LOG_D((sata_hba_inst, CE_WARN,
9595 			    "sata_hba_ioctl: disconnect: could "
9596 			    "not unconfigure device before "
9597 			    "disconnecting the SATA port %d:%d",
9598 			    cport, pmport));
9599 		}
9600 		/*
9601 		 * Set DEVICE REMOVED state in the target
9602 		 * node. It will prevent access to the device
9603 		 * even when a new device is attached, until
9604 		 * the old target node is released, removed and
9605 		 * recreated for a new  device.
9606 		 */
9607 		sata_set_device_removed(tdip);
9608 
9609 		/*
9610 		 * Instruct event daemon to try the target
9611 		 * node cleanup later.
9612 		 */
9613 		sata_set_target_node_cleanup(
9614 		    sata_hba_inst, &sata_device->satadev_addr);
9615 	}
9616 
9617 
9618 	return (SATA_SUCCESS);
9619 }
9620 
9621 
9622 /*
9623  * Create scsi target node for attached device, create node properties and
9624  * attach the node.
9625  * The node could be removed if the device onlining fails.
9626  *
9627  * A dev_info_t pointer is returned if operation is successful, NULL is
9628  * returned otherwise.
9629  */
9630 
9631 static dev_info_t *
9632 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst,
9633 			sata_address_t *sata_addr)
9634 {
9635 	dev_info_t *cdip = NULL;
9636 	int rval;
9637 	char *nname = NULL;
9638 	char **compatible = NULL;
9639 	int ncompatible;
9640 	struct scsi_inquiry inq;
9641 	sata_device_t sata_device;
9642 	sata_drive_info_t *sdinfo;
9643 	int target;
9644 	int i;
9645 
9646 	sata_device.satadev_rev = SATA_DEVICE_REV;
9647 	sata_device.satadev_addr = *sata_addr;
9648 
9649 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport)));
9650 
9651 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
9652 
9653 	target = SATA_TO_SCSI_TARGET(sata_addr->cport,
9654 	    sata_addr->pmport, sata_addr->qual);
9655 
9656 	if (sdinfo == NULL) {
9657 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9658 		    sata_addr->cport)));
9659 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9660 		    "sata_create_target_node: no sdinfo for target %x",
9661 		    target));
9662 		return (NULL);
9663 	}
9664 
9665 	/*
9666 	 * create or get scsi inquiry data, expected by
9667 	 * scsi_hba_nodename_compatible_get()
9668 	 * SATA hard disks get Identify Data translated into Inguiry Data.
9669 	 * ATAPI devices respond directly to Inquiry request.
9670 	 */
9671 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9672 		sata_identdev_to_inquiry(sata_hba_inst, sdinfo,
9673 		    (uint8_t *)&inq);
9674 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9675 		    sata_addr->cport)));
9676 	} else { /* Assume supported ATAPI device */
9677 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9678 		    sata_addr->cport)));
9679 		if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr,
9680 		    &inq) == SATA_FAILURE)
9681 			return (NULL);
9682 		/*
9683 		 * Save supported ATAPI transport version
9684 		 */
9685 		sdinfo->satadrv_atapi_trans_ver =
9686 		    SATA_ATAPI_TRANS_VERSION(&inq);
9687 	}
9688 
9689 	/* determine the node name and compatible */
9690 	scsi_hba_nodename_compatible_get(&inq, NULL,
9691 	    inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
9692 
9693 #ifdef SATA_DEBUG
9694 	if (sata_debug_flags & SATA_DBG_NODES) {
9695 		if (nname == NULL) {
9696 			cmn_err(CE_NOTE, "sata_create_target_node: "
9697 			    "cannot determine nodename for target %d\n",
9698 			    target);
9699 		} else {
9700 			cmn_err(CE_WARN, "sata_create_target_node: "
9701 			    "target %d nodename: %s\n", target, nname);
9702 		}
9703 		if (compatible == NULL) {
9704 			cmn_err(CE_WARN,
9705 			    "sata_create_target_node: no compatible name\n");
9706 		} else {
9707 			for (i = 0; i < ncompatible; i++) {
9708 				cmn_err(CE_WARN, "sata_create_target_node: "
9709 				    "compatible name: %s\n", compatible[i]);
9710 			}
9711 		}
9712 	}
9713 #endif
9714 
9715 	/* if nodename can't be determined, log error and exit */
9716 	if (nname == NULL) {
9717 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9718 		    "sata_create_target_node: cannot determine nodename "
9719 		    "for target %d\n", target));
9720 		scsi_hba_nodename_compatible_free(nname, compatible);
9721 		return (NULL);
9722 	}
9723 	/*
9724 	 * Create scsi target node
9725 	 */
9726 	ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip);
9727 	rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
9728 	    "device-type", "scsi");
9729 
9730 	if (rval != DDI_PROP_SUCCESS) {
9731 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
9732 		    "updating device_type prop failed %d", rval));
9733 		goto fail;
9734 	}
9735 
9736 	/*
9737 	 * Create target node properties: target & lun
9738 	 */
9739 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target);
9740 	if (rval != DDI_PROP_SUCCESS) {
9741 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
9742 		    "updating target prop failed %d", rval));
9743 		goto fail;
9744 	}
9745 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0);
9746 	if (rval != DDI_PROP_SUCCESS) {
9747 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
9748 		    "updating target prop failed %d", rval));
9749 		goto fail;
9750 	}
9751 
9752 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
9753 		/*
9754 		 * Add "variant" property
9755 		 */
9756 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
9757 		    "variant", "atapi");
9758 		if (rval != DDI_PROP_SUCCESS) {
9759 			SATA_LOG_D((sata_hba_inst, CE_WARN,
9760 			    "sata_create_target_node: variant atapi "
9761 			    "property could not be created: %d", rval));
9762 			goto fail;
9763 		}
9764 	}
9765 	/* decorate the node with compatible */
9766 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible",
9767 	    compatible, ncompatible) != DDI_PROP_SUCCESS) {
9768 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9769 		    "sata_create_target_node: FAIL compatible props cdip 0x%p",
9770 		    (void *)cdip));
9771 		goto fail;
9772 	}
9773 
9774 
9775 	/*
9776 	 * Now, try to attach the driver. If probing of the device fails,
9777 	 * the target node may be removed
9778 	 */
9779 	rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH);
9780 
9781 	scsi_hba_nodename_compatible_free(nname, compatible);
9782 
9783 	if (rval == NDI_SUCCESS)
9784 		return (cdip);
9785 
9786 	/* target node was removed - are we sure? */
9787 	return (NULL);
9788 
9789 fail:
9790 	scsi_hba_nodename_compatible_free(nname, compatible);
9791 	ddi_prop_remove_all(cdip);
9792 	rval = ndi_devi_free(cdip);
9793 	if (rval != NDI_SUCCESS) {
9794 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
9795 		    "node removal failed %d", rval));
9796 	}
9797 	sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: "
9798 	    "cannot create target node for SATA device at port %d",
9799 	    sata_addr->cport);
9800 	return (NULL);
9801 }
9802 
9803 /*
9804  * Remove a target node.
9805  */
9806 static void
9807 sata_remove_target_node(sata_hba_inst_t *sata_hba_inst,
9808 			sata_address_t *sata_addr)
9809 {
9810 	dev_info_t *tdip;
9811 	uint8_t cport = sata_addr->cport;
9812 	uint8_t pmport = sata_addr->pmport;
9813 	uint8_t qual = sata_addr->qual;
9814 
9815 	/* Note the sata daemon uses the address of the port/pmport */
9816 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
9817 
9818 	/* Remove target node */
9819 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), cport, pmport);
9820 	if (tdip != NULL) {
9821 		/*
9822 		 * Target node exists.  Unconfigure device
9823 		 * then remove the target node (one ndi
9824 		 * operation).
9825 		 */
9826 		if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
9827 			/*
9828 			 * PROBLEM - no device, but target node remained. This
9829 			 * happens when the file was open or node was waiting
9830 			 * for resources.
9831 			 */
9832 			SATA_LOG_D((sata_hba_inst, CE_WARN,
9833 			    "sata_remove_target_node: "
9834 			    "Failed to remove target node for "
9835 			    "detached SATA device."));
9836 			/*
9837 			 * Set target node state to DEVI_DEVICE_REMOVED. But
9838 			 * re-check first that the node still exists.
9839 			 */
9840 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
9841 			    cport, pmport);
9842 			if (tdip != NULL) {
9843 				sata_set_device_removed(tdip);
9844 				/*
9845 				 * Instruct event daemon to retry the cleanup
9846 				 * later.
9847 				 */
9848 				sata_set_target_node_cleanup(sata_hba_inst,
9849 				    sata_addr);
9850 			}
9851 		}
9852 
9853 		if (qual == SATA_ADDR_CPORT)
9854 			sata_log(sata_hba_inst, CE_WARN,
9855 			    "SATA device detached at port %d", cport);
9856 		else
9857 			sata_log(sata_hba_inst, CE_WARN,
9858 			    "SATA device detached at port %d:%d",
9859 			    cport, pmport);
9860 	}
9861 #ifdef SATA_DEBUG
9862 	else {
9863 		if (qual == SATA_ADDR_CPORT)
9864 			sata_log(sata_hba_inst, CE_WARN,
9865 			    "target node not found at port %d", cport);
9866 		else
9867 			sata_log(sata_hba_inst, CE_WARN,
9868 			    "target node not found at port %d:%d",
9869 			    cport, pmport);
9870 	}
9871 #endif
9872 }
9873 
9874 
9875 /*
9876  * Re-probe sata port, check for a device and attach info
9877  * structures when necessary. Identify Device data is fetched, if possible.
9878  * Assumption: sata address is already validated.
9879  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
9880  * the presence of a device and its type.
9881  *
9882  * flag arg specifies that the function should try multiple times to identify
9883  * device type and to initialize it, or it should return immediately on failure.
9884  * SATA_DEV_IDENTIFY_RETRY - retry
9885  * SATA_DEV_IDENTIFY_NORETRY - no retry
9886  *
9887  * SATA_FAILURE is returned if one of the operations failed.
9888  *
9889  * This function cannot be called in interrupt context - it may sleep.
9890  *
9891  * Note: Port multiplier is supported.
9892  */
9893 static int
9894 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
9895     int flag)
9896 {
9897 	sata_cport_info_t *cportinfo;
9898 	sata_pmult_info_t *pmultinfo;
9899 	sata_drive_info_t *sdinfo, *osdinfo;
9900 	boolean_t init_device = B_FALSE;
9901 	int prev_device_type = SATA_DTYPE_NONE;
9902 	int prev_device_settings = 0;
9903 	int prev_device_state = 0;
9904 	clock_t start_time;
9905 	int retry = B_FALSE;
9906 	uint8_t cport = sata_device->satadev_addr.cport;
9907 	int rval_probe, rval_init;
9908 
9909 	/*
9910 	 * If target is pmport, sata_reprobe_pmport() will handle it.
9911 	 */
9912 	if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT ||
9913 	    sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT)
9914 		return (sata_reprobe_pmport(sata_hba_inst, sata_device, flag));
9915 
9916 	/* We only care about host sata cport for now */
9917 	cportinfo = SATA_CPORT_INFO(sata_hba_inst,
9918 	    sata_device->satadev_addr.cport);
9919 
9920 	/*
9921 	 * If a port multiplier was previously attached (we have no idea it
9922 	 * still there or not), sata_reprobe_pmult() will handle it.
9923 	 */
9924 	if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT)
9925 		return (sata_reprobe_pmult(sata_hba_inst, sata_device, flag));
9926 
9927 	/* Store sata_drive_info when a non-pmult device was attached. */
9928 	osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
9929 	if (osdinfo != NULL) {
9930 		/*
9931 		 * We are re-probing port with a previously attached device.
9932 		 * Save previous device type and settings.
9933 		 */
9934 		prev_device_type = cportinfo->cport_dev_type;
9935 		prev_device_settings = osdinfo->satadrv_settings;
9936 		prev_device_state = osdinfo->satadrv_state;
9937 	}
9938 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
9939 		start_time = ddi_get_lbolt();
9940 		retry = B_TRUE;
9941 	}
9942 retry_probe:
9943 
9944 	/* probe port */
9945 	mutex_enter(&cportinfo->cport_mutex);
9946 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
9947 	cportinfo->cport_state |= SATA_STATE_PROBING;
9948 	mutex_exit(&cportinfo->cport_mutex);
9949 
9950 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
9951 	    (SATA_DIP(sata_hba_inst), sata_device);
9952 
9953 	mutex_enter(&cportinfo->cport_mutex);
9954 	if (rval_probe != SATA_SUCCESS) {
9955 		cportinfo->cport_state = SATA_PSTATE_FAILED;
9956 		mutex_exit(&cportinfo->cport_mutex);
9957 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: "
9958 		    "SATA port %d probing failed",
9959 		    cportinfo->cport_addr.cport));
9960 		return (SATA_FAILURE);
9961 	}
9962 
9963 	/*
9964 	 * update sata port state and set device type
9965 	 */
9966 	sata_update_port_info(sata_hba_inst, sata_device);
9967 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
9968 
9969 	/*
9970 	 * Sanity check - Port is active? Is the link active?
9971 	 * Is there any device attached?
9972 	 */
9973 	if ((cportinfo->cport_state &
9974 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
9975 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
9976 	    SATA_PORT_DEVLINK_UP) {
9977 		/*
9978 		 * Port in non-usable state or no link active/no device.
9979 		 * Free info structure if necessary (direct attached drive
9980 		 * only, for now!
9981 		 */
9982 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
9983 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
9984 		/* Add here differentiation for device attached or not */
9985 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
9986 		mutex_exit(&cportinfo->cport_mutex);
9987 		if (sdinfo != NULL)
9988 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
9989 		return (SATA_SUCCESS);
9990 	}
9991 
9992 	cportinfo->cport_state |= SATA_STATE_READY;
9993 	cportinfo->cport_state |= SATA_STATE_PROBED;
9994 
9995 	cportinfo->cport_dev_type = sata_device->satadev_type;
9996 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
9997 
9998 	/*
9999 	 * If we are re-probing the port, there may be
10000 	 * sata_drive_info structure attached
10001 	 */
10002 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
10003 
10004 		/*
10005 		 * There is no device, so remove device info structure,
10006 		 * if necessary.
10007 		 */
10008 		/* Device change: Drive -> None */
10009 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
10010 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
10011 		if (sdinfo != NULL) {
10012 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
10013 			sata_log(sata_hba_inst, CE_WARN,
10014 			    "SATA device detached "
10015 			    "from port %d", cportinfo->cport_addr.cport);
10016 		}
10017 		mutex_exit(&cportinfo->cport_mutex);
10018 		return (SATA_SUCCESS);
10019 
10020 	}
10021 
10022 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
10023 
10024 		/* Device (may) change: Drive -> Drive */
10025 		if (sdinfo == NULL) {
10026 			/*
10027 			 * There is some device attached, but there is
10028 			 * no sata_drive_info structure - allocate one
10029 			 */
10030 			mutex_exit(&cportinfo->cport_mutex);
10031 			sdinfo = kmem_zalloc(
10032 			    sizeof (sata_drive_info_t), KM_SLEEP);
10033 			mutex_enter(&cportinfo->cport_mutex);
10034 			/*
10035 			 * Recheck, that the port state did not change when we
10036 			 * released mutex.
10037 			 */
10038 			if (cportinfo->cport_state & SATA_STATE_READY) {
10039 				SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo;
10040 				sdinfo->satadrv_addr = cportinfo->cport_addr;
10041 				sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT;
10042 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
10043 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
10044 			} else {
10045 				/*
10046 				 * Port is not in ready state, we
10047 				 * cannot attach a device.
10048 				 */
10049 				mutex_exit(&cportinfo->cport_mutex);
10050 				kmem_free(sdinfo, sizeof (sata_drive_info_t));
10051 				return (SATA_SUCCESS);
10052 			}
10053 			/*
10054 			 * Since we are adding device, presumably new one,
10055 			 * indicate that it  should be initalized,
10056 			 * as well as some internal framework states).
10057 			 */
10058 			init_device = B_TRUE;
10059 		}
10060 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
10061 		sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
10062 	} else {
10063 		/* Device change: Drive -> PMult */
10064 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
10065 		if (sdinfo != NULL) {
10066 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
10067 			sata_log(sata_hba_inst, CE_WARN,
10068 			    "SATA device detached "
10069 			    "from port %d", cportinfo->cport_addr.cport);
10070 		}
10071 
10072 		sata_log(sata_hba_inst, CE_WARN,
10073 		    "SATA port multiplier detected at port %d",
10074 		    cportinfo->cport_addr.cport);
10075 
10076 		mutex_exit(&cportinfo->cport_mutex);
10077 		if (sata_alloc_pmult(sata_hba_inst, sata_device) !=
10078 		    SATA_SUCCESS)
10079 			return (SATA_FAILURE);
10080 		sata_show_pmult_info(sata_hba_inst, sata_device);
10081 		mutex_enter(&cportinfo->cport_mutex);
10082 
10083 		/*
10084 		 * Mark all the port multiplier port behind the port
10085 		 * multiplier behind with link events, so that the sata daemon
10086 		 * will update their status.
10087 		 */
10088 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
10089 		pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET;
10090 		mutex_exit(&cportinfo->cport_mutex);
10091 		return (SATA_SUCCESS);
10092 	}
10093 	mutex_exit(&cportinfo->cport_mutex);
10094 
10095 	/*
10096 	 * Figure out what kind of device we are really
10097 	 * dealing with. Failure of identifying device does not fail this
10098 	 * function.
10099 	 */
10100 	rval_probe = sata_probe_device(sata_hba_inst, sata_device);
10101 	rval_init = SATA_FAILURE;
10102 	mutex_enter(&cportinfo->cport_mutex);
10103 	if (rval_probe == SATA_SUCCESS) {
10104 		/*
10105 		 * If we are dealing with the same type of a device as before,
10106 		 * restore its settings flags.
10107 		 */
10108 		if (osdinfo != NULL &&
10109 		    sata_device->satadev_type == prev_device_type)
10110 			sdinfo->satadrv_settings = prev_device_settings;
10111 
10112 		mutex_exit(&cportinfo->cport_mutex);
10113 		rval_init = SATA_SUCCESS;
10114 		/* Set initial device features, if necessary */
10115 		if (init_device == B_TRUE) {
10116 			rval_init = sata_initialize_device(sata_hba_inst,
10117 			    sdinfo);
10118 		}
10119 		if (rval_init == SATA_SUCCESS)
10120 			return (rval_init);
10121 		/* else we will retry if retry was asked for */
10122 
10123 	} else {
10124 		/*
10125 		 * If there was some device info before we probe the device,
10126 		 * restore previous device setting, so we can retry from scratch
10127 		 * later. Providing, of course, that device has not disapear
10128 		 * during probing process.
10129 		 */
10130 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
10131 			if (osdinfo != NULL) {
10132 				cportinfo->cport_dev_type = prev_device_type;
10133 				sdinfo->satadrv_type = prev_device_type;
10134 				sdinfo->satadrv_state = prev_device_state;
10135 			}
10136 		} else {
10137 			/* device is gone */
10138 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
10139 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
10140 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
10141 			mutex_exit(&cportinfo->cport_mutex);
10142 			return (SATA_SUCCESS);
10143 		}
10144 		mutex_exit(&cportinfo->cport_mutex);
10145 	}
10146 
10147 	if (retry) {
10148 		clock_t cur_time = ddi_get_lbolt();
10149 		/*
10150 		 * A device was not successfully identified or initialized.
10151 		 * Track retry time for device identification.
10152 		 */
10153 		if ((cur_time - start_time) <
10154 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
10155 			/* sleep for a while */
10156 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
10157 			goto retry_probe;
10158 		}
10159 		/* else no more retries */
10160 		mutex_enter(&cportinfo->cport_mutex);
10161 		if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
10162 			if (rval_init == SATA_RETRY) {
10163 				/*
10164 				 * Setting drive features have failed, but
10165 				 * because the drive is still accessible,
10166 				 * keep it and emit a warning message.
10167 				 */
10168 				sata_log(sata_hba_inst, CE_WARN,
10169 				    "SATA device at port %d - desired "
10170 				    "drive features could not be set. "
10171 				    "Device may not operate as expected.",
10172 				    cportinfo->cport_addr.cport);
10173 			} else {
10174 				SATA_CPORTINFO_DRV_INFO(cportinfo)->
10175 				    satadrv_state = SATA_DSTATE_FAILED;
10176 			}
10177 		}
10178 		mutex_exit(&cportinfo->cport_mutex);
10179 	}
10180 	return (SATA_SUCCESS);
10181 }
10182 
10183 /*
10184  * Reprobe a controller port that connected to a port multiplier.
10185  *
10186  * NOTE: No Mutex should be hold.
10187  */
10188 static int
10189 sata_reprobe_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
10190     int flag)
10191 {
10192 	_NOTE(ARGUNUSED(flag))
10193 	sata_cport_info_t *cportinfo;
10194 	sata_pmult_info_t *pmultinfo;
10195 	uint8_t cport = sata_device->satadev_addr.cport;
10196 	int rval_probe;
10197 
10198 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
10199 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
10200 
10201 	/* probe port */
10202 	mutex_enter(&cportinfo->cport_mutex);
10203 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
10204 	cportinfo->cport_state |= SATA_STATE_PROBING;
10205 	mutex_exit(&cportinfo->cport_mutex);
10206 
10207 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10208 	    (SATA_DIP(sata_hba_inst), sata_device);
10209 
10210 	mutex_enter(&cportinfo->cport_mutex);
10211 	if (rval_probe != SATA_SUCCESS) {
10212 		cportinfo->cport_state = SATA_PSTATE_FAILED;
10213 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmult: "
10214 		    "SATA port %d probing failed", cport));
10215 		sata_log(sata_hba_inst, CE_WARN,
10216 		    "SATA port multiplier detached at port %d", cport);
10217 		mutex_exit(&cportinfo->cport_mutex);
10218 		sata_free_pmult(sata_hba_inst, sata_device);
10219 		return (SATA_FAILURE);
10220 	}
10221 
10222 	/*
10223 	 * update sata port state and set device type
10224 	 */
10225 	sata_update_port_info(sata_hba_inst, sata_device);
10226 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
10227 	cportinfo->cport_state |= SATA_STATE_PROBED;
10228 
10229 	/*
10230 	 * Sanity check - Port is active? Is the link active?
10231 	 * Is there any device attached?
10232 	 */
10233 	if ((cportinfo->cport_state &
10234 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
10235 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
10236 	    SATA_PORT_DEVLINK_UP ||
10237 	    (sata_device->satadev_type == SATA_DTYPE_NONE)) {
10238 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
10239 		mutex_exit(&cportinfo->cport_mutex);
10240 		sata_free_pmult(sata_hba_inst, sata_device);
10241 		sata_log(sata_hba_inst, CE_WARN,
10242 		    "SATA port multiplier detached at port %d", cport);
10243 		return (SATA_SUCCESS);
10244 	}
10245 
10246 	/*
10247 	 * Device changed: PMult -> Non-PMult
10248 	 *
10249 	 * This situation is uncommon, most possibly being caused by errors
10250 	 * after which the port multiplier is not correct initialized and
10251 	 * recognized. In that case the new device will be marked as unknown
10252 	 * and will not be automatically probed in this routine. Instead
10253 	 * system administrator could manually restart it via cfgadm(1M).
10254 	 */
10255 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
10256 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
10257 		mutex_exit(&cportinfo->cport_mutex);
10258 		sata_free_pmult(sata_hba_inst, sata_device);
10259 		sata_log(sata_hba_inst, CE_WARN,
10260 		    "SATA port multiplier detached at port %d", cport);
10261 		return (SATA_FAILURE);
10262 	}
10263 
10264 	/*
10265 	 * Now we know it is a port multiplier. However, if this is not the
10266 	 * previously attached port multiplier - they may have different
10267 	 * pmport numbers - we need to re-allocate data structures for every
10268 	 * pmport and drive.
10269 	 *
10270 	 * Port multipliers of the same model have identical values in these
10271 	 * registers, so it is still necessary to update the information of
10272 	 * all drives attached to the previous port multiplier afterwards.
10273 	 */
10274 	/* Device changed: PMult -> another PMult */
10275 	mutex_exit(&cportinfo->cport_mutex);
10276 	sata_free_pmult(sata_hba_inst, sata_device);
10277 	if (sata_alloc_pmult(sata_hba_inst, sata_device) != SATA_SUCCESS)
10278 		return (SATA_FAILURE);
10279 	mutex_enter(&cportinfo->cport_mutex);
10280 
10281 	SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
10282 	    "SATA port multiplier [changed] at port %d", cport);
10283 	sata_log(sata_hba_inst, CE_WARN,
10284 	    "SATA port multiplier detected at port %d", cport);
10285 
10286 	/*
10287 	 * Mark all the port multiplier port behind the port
10288 	 * multiplier behind with link events, so that the sata daemon
10289 	 * will update their status.
10290 	 */
10291 	pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET;
10292 	mutex_exit(&cportinfo->cport_mutex);
10293 
10294 	return (SATA_SUCCESS);
10295 }
10296 
10297 /*
10298  * Re-probe a port multiplier port, check for a device and attach info
10299  * structures when necessary. Identify Device data is fetched, if possible.
10300  * Assumption: sata address is already validated as port multiplier port.
10301  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
10302  * the presence of a device and its type.
10303  *
10304  * flag arg specifies that the function should try multiple times to identify
10305  * device type and to initialize it, or it should return immediately on failure.
10306  * SATA_DEV_IDENTIFY_RETRY - retry
10307  * SATA_DEV_IDENTIFY_NORETRY - no retry
10308  *
10309  * SATA_FAILURE is returned if one of the operations failed.
10310  *
10311  * This function cannot be called in interrupt context - it may sleep.
10312  *
10313  * NOTE: Should be only called by sata_probe_port() in case target port is a
10314  *       port multiplier port.
10315  * NOTE: No Mutex should be hold.
10316  */
10317 static int
10318 sata_reprobe_pmport(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
10319     int flag)
10320 {
10321 	sata_cport_info_t *cportinfo = NULL;
10322 	sata_pmport_info_t *pmportinfo = NULL;
10323 	sata_drive_info_t *sdinfo, *osdinfo;
10324 	sata_device_t sdevice;
10325 	boolean_t init_device = B_FALSE;
10326 	int prev_device_type = SATA_DTYPE_NONE;
10327 	int prev_device_settings = 0;
10328 	int prev_device_state = 0;
10329 	clock_t start_time;
10330 	uint8_t cport = sata_device->satadev_addr.cport;
10331 	uint8_t pmport = sata_device->satadev_addr.pmport;
10332 	int rval;
10333 
10334 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
10335 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
10336 	osdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
10337 
10338 	if (osdinfo != NULL) {
10339 		/*
10340 		 * We are re-probing port with a previously attached device.
10341 		 * Save previous device type and settings.
10342 		 */
10343 		prev_device_type = pmportinfo->pmport_dev_type;
10344 		prev_device_settings = osdinfo->satadrv_settings;
10345 		prev_device_state = osdinfo->satadrv_state;
10346 	}
10347 
10348 	start_time = ddi_get_lbolt();
10349 
10350 	/* check parent status */
10351 	mutex_enter(&cportinfo->cport_mutex);
10352 	if ((cportinfo->cport_state &
10353 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
10354 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
10355 	    SATA_PORT_DEVLINK_UP) {
10356 		mutex_exit(&cportinfo->cport_mutex);
10357 		return (SATA_FAILURE);
10358 	}
10359 	mutex_exit(&cportinfo->cport_mutex);
10360 
10361 retry_probe_pmport:
10362 
10363 	/* probe port */
10364 	mutex_enter(&pmportinfo->pmport_mutex);
10365 	pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
10366 	pmportinfo->pmport_state |= SATA_STATE_PROBING;
10367 	mutex_exit(&pmportinfo->pmport_mutex);
10368 
10369 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10370 	    (SATA_DIP(sata_hba_inst), sata_device);
10371 
10372 	/* might need retry because we cannot touch registers. */
10373 	if (rval == SATA_FAILURE) {
10374 		mutex_enter(&pmportinfo->pmport_mutex);
10375 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
10376 		mutex_exit(&pmportinfo->pmport_mutex);
10377 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: "
10378 		    "SATA port %d:%d probing failed",
10379 		    cport, pmport));
10380 		return (SATA_FAILURE);
10381 	} else if (rval == SATA_RETRY) {
10382 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: "
10383 		    "SATA port %d:%d probing failed, retrying...",
10384 		    cport, pmport));
10385 		clock_t cur_time = ddi_get_lbolt();
10386 		/*
10387 		 * A device was not successfully identified or initialized.
10388 		 * Track retry time for device identification.
10389 		 */
10390 		if ((cur_time - start_time) <
10391 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
10392 			/* sleep for a while */
10393 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
10394 			goto retry_probe_pmport;
10395 		} else {
10396 			mutex_enter(&pmportinfo->pmport_mutex);
10397 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL)
10398 				SATA_PMPORTINFO_DRV_INFO(pmportinfo)->
10399 				    satadrv_state = SATA_DSTATE_FAILED;
10400 			mutex_exit(&pmportinfo->pmport_mutex);
10401 			return (SATA_SUCCESS);
10402 		}
10403 	}
10404 
10405 	/*
10406 	 * Sanity check - Controller port is active? Is the link active?
10407 	 * Is it still a port multiplier?
10408 	 */
10409 	if ((cportinfo->cport_state &
10410 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
10411 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
10412 	    SATA_PORT_DEVLINK_UP ||
10413 	    (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) {
10414 		/*
10415 		 * Port in non-usable state or no link active/no
10416 		 * device. Free info structure.
10417 		 */
10418 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
10419 
10420 		sdevice.satadev_addr.cport = cport;
10421 		sdevice.satadev_addr.pmport = pmport;
10422 		sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
10423 		mutex_exit(&cportinfo->cport_mutex);
10424 
10425 		sata_free_pmult(sata_hba_inst, &sdevice);
10426 		return (SATA_FAILURE);
10427 	}
10428 
10429 	/* SATA_SUCCESS NOW */
10430 	/*
10431 	 * update sata port state and set device type
10432 	 */
10433 	mutex_enter(&pmportinfo->pmport_mutex);
10434 	sata_update_pmport_info(sata_hba_inst, sata_device);
10435 	pmportinfo->pmport_state &= ~SATA_STATE_PROBING;
10436 
10437 	/*
10438 	 * Sanity check - Port is active? Is the link active?
10439 	 * Is there any device attached?
10440 	 */
10441 	if ((pmportinfo->pmport_state &
10442 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
10443 	    (pmportinfo->pmport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
10444 	    SATA_PORT_DEVLINK_UP) {
10445 		/*
10446 		 * Port in non-usable state or no link active/no device.
10447 		 * Free info structure if necessary (direct attached drive
10448 		 * only, for now!
10449 		 */
10450 		sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
10451 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
10452 		/* Add here differentiation for device attached or not */
10453 		pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
10454 		mutex_exit(&pmportinfo->pmport_mutex);
10455 		if (sdinfo != NULL)
10456 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
10457 		return (SATA_SUCCESS);
10458 	}
10459 
10460 	pmportinfo->pmport_state |= SATA_STATE_READY;
10461 	pmportinfo->pmport_dev_type = sata_device->satadev_type;
10462 	sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
10463 
10464 	/*
10465 	 * If we are re-probing the port, there may be
10466 	 * sata_drive_info structure attached
10467 	 * (or sata_pm_info, if PMult is supported).
10468 	 */
10469 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
10470 		/*
10471 		 * There is no device, so remove device info structure,
10472 		 * if necessary.
10473 		 */
10474 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
10475 		pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
10476 		if (sdinfo != NULL) {
10477 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
10478 			sata_log(sata_hba_inst, CE_WARN,
10479 			    "SATA device detached from port %d:%d",
10480 			    cport, pmport);
10481 		}
10482 		mutex_exit(&pmportinfo->pmport_mutex);
10483 		return (SATA_SUCCESS);
10484 	}
10485 
10486 	/* this should not be a pmult */
10487 	ASSERT(sata_device->satadev_type != SATA_DTYPE_PMULT);
10488 	if (sdinfo == NULL) {
10489 		/*
10490 		 * There is some device attached, but there is
10491 		 * no sata_drive_info structure - allocate one
10492 		 */
10493 		mutex_exit(&pmportinfo->pmport_mutex);
10494 		sdinfo = kmem_zalloc(sizeof (sata_drive_info_t),
10495 		    KM_SLEEP);
10496 		mutex_enter(&pmportinfo->pmport_mutex);
10497 		/*
10498 		 * Recheck, that the port state did not change when we
10499 		 * released mutex.
10500 		 */
10501 		if (pmportinfo->pmport_state & SATA_STATE_READY) {
10502 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = sdinfo;
10503 			sdinfo->satadrv_addr = pmportinfo->pmport_addr;
10504 			sdinfo->satadrv_addr.qual = SATA_ADDR_DPMPORT;
10505 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
10506 			sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
10507 		} else {
10508 			/*
10509 			 * Port is not in ready state, we
10510 			 * cannot attach a device.
10511 			 */
10512 			mutex_exit(&pmportinfo->pmport_mutex);
10513 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
10514 			return (SATA_SUCCESS);
10515 		}
10516 		/*
10517 		 * Since we are adding device, presumably new one,
10518 		 * indicate that it  should be initalized,
10519 		 * as well as some internal framework states).
10520 		 */
10521 		init_device = B_TRUE;
10522 	}
10523 
10524 	pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
10525 	sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
10526 
10527 	mutex_exit(&pmportinfo->pmport_mutex);
10528 	/*
10529 	 * Figure out what kind of device we are really
10530 	 * dealing with.
10531 	 */
10532 	rval = sata_probe_device(sata_hba_inst, sata_device);
10533 
10534 	mutex_enter(&pmportinfo->pmport_mutex);
10535 	if (rval == SATA_SUCCESS) {
10536 		/*
10537 		 * If we are dealing with the same type of a device as before,
10538 		 * restore its settings flags.
10539 		 */
10540 		if (osdinfo != NULL &&
10541 		    sata_device->satadev_type == prev_device_type)
10542 			sdinfo->satadrv_settings = prev_device_settings;
10543 
10544 		mutex_exit(&pmportinfo->pmport_mutex);
10545 		/* Set initial device features, if necessary */
10546 		if (init_device == B_TRUE) {
10547 			rval = sata_initialize_device(sata_hba_inst, sdinfo);
10548 		}
10549 		if (rval == SATA_SUCCESS)
10550 			return (rval);
10551 	} else {
10552 		/*
10553 		 * If there was some device info before we probe the device,
10554 		 * restore previous device setting, so we can retry from scratch
10555 		 * later. Providing, of course, that device has not disappeared
10556 		 * during probing process.
10557 		 */
10558 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
10559 			if (osdinfo != NULL) {
10560 				pmportinfo->pmport_dev_type = prev_device_type;
10561 				sdinfo->satadrv_type = prev_device_type;
10562 				sdinfo->satadrv_state = prev_device_state;
10563 			}
10564 		} else {
10565 			/* device is gone */
10566 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
10567 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
10568 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
10569 			mutex_exit(&pmportinfo->pmport_mutex);
10570 			return (SATA_SUCCESS);
10571 		}
10572 		mutex_exit(&pmportinfo->pmport_mutex);
10573 	}
10574 
10575 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
10576 		clock_t cur_time = ddi_get_lbolt();
10577 		/*
10578 		 * A device was not successfully identified or initialized.
10579 		 * Track retry time for device identification.
10580 		 */
10581 		if ((cur_time - start_time) <
10582 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
10583 			/* sleep for a while */
10584 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
10585 			goto retry_probe_pmport;
10586 		} else {
10587 			mutex_enter(&pmportinfo->pmport_mutex);
10588 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL)
10589 				SATA_PMPORTINFO_DRV_INFO(pmportinfo)->
10590 				    satadrv_state = SATA_DSTATE_FAILED;
10591 			mutex_exit(&pmportinfo->pmport_mutex);
10592 		}
10593 	}
10594 	return (SATA_SUCCESS);
10595 }
10596 
10597 /*
10598  * Allocated related structure for a port multiplier and its device ports
10599  *
10600  * Port multiplier should be ready and probed, and related information like
10601  * the number of the device ports should be store in sata_device_t.
10602  *
10603  * NOTE: No Mutex should be hold.
10604  */
10605 static int
10606 sata_alloc_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
10607 {
10608 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
10609 	sata_cport_info_t *cportinfo = NULL;
10610 	sata_pmult_info_t *pmultinfo = NULL;
10611 	sata_pmport_info_t *pmportinfo = NULL;
10612 	sata_device_t sd;
10613 	dev_t minor_number;
10614 	char name[16];
10615 	uint8_t cport = sata_device->satadev_addr.cport;
10616 	int rval;
10617 	int npmport;
10618 
10619 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
10620 
10621 	/* This function might be called while a port-mult is hot-plugged. */
10622 	mutex_enter(&cportinfo->cport_mutex);
10623 
10624 	/* dev_type's not updated when get called from sata_reprobe_port() */
10625 	cportinfo->cport_dev_type = SATA_DTYPE_PMULT;
10626 	if (SATA_CPORTINFO_PMULT_INFO(cportinfo) == NULL) {
10627 		/* Create a pmult_info structure */
10628 		SATA_CPORTINFO_PMULT_INFO(cportinfo) =
10629 		    kmem_zalloc(sizeof (sata_pmult_info_t), KM_SLEEP);
10630 	}
10631 	pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
10632 
10633 	pmultinfo->pmult_addr = sata_device->satadev_addr;
10634 	pmultinfo->pmult_addr.qual = SATA_ADDR_PMULT;
10635 	pmultinfo->pmult_state = SATA_STATE_PROBING;
10636 
10637 	/*
10638 	 * Probe the port multiplier with qualifier SATA_ADDR_PMULT_SPEC,
10639 	 * The HBA driver should initialize and register the port multiplier,
10640 	 * sata_register_pmult() will fill following fields,
10641 	 *   + sata_pmult_info.pmult_gscr
10642 	 *   + sata_pmult_info.pmult_num_dev_ports
10643 	 */
10644 	sd.satadev_addr = sata_device->satadev_addr;
10645 	sd.satadev_addr.qual = SATA_ADDR_PMULT_SPEC;
10646 	mutex_exit(&cportinfo->cport_mutex);
10647 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10648 	    (SATA_DIP(sata_hba_inst), &sd);
10649 	mutex_enter(&cportinfo->cport_mutex);
10650 
10651 	if (rval != SATA_SUCCESS ||
10652 	    (sd.satadev_type != SATA_DTYPE_PMULT) ||
10653 	    !(sd.satadev_state & SATA_DSTATE_PMULT_INIT)) {
10654 		SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL;
10655 		kmem_free(pmultinfo, sizeof (sata_pmult_info_t));
10656 		cportinfo->cport_state = SATA_PSTATE_FAILED;
10657 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
10658 		mutex_exit(&cportinfo->cport_mutex);
10659 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
10660 		    "sata_alloc_pmult: failed to initialize pmult "
10661 		    "at port %d.", cport)
10662 		return (SATA_FAILURE);
10663 	}
10664 
10665 	/* Initialize pmport_info structure */
10666 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports;
10667 	    npmport++) {
10668 
10669 		/* if everything is allocated, skip */
10670 		if (SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) != NULL)
10671 			continue;
10672 
10673 		pmportinfo = kmem_zalloc(sizeof (sata_pmport_info_t), KM_SLEEP);
10674 		mutex_init(&pmportinfo->pmport_mutex, NULL, MUTEX_DRIVER, NULL);
10675 		mutex_exit(&cportinfo->cport_mutex);
10676 
10677 		mutex_enter(&pmportinfo->pmport_mutex);
10678 		pmportinfo->pmport_addr.cport = cport;
10679 		pmportinfo->pmport_addr.pmport = (uint8_t)npmport;
10680 		pmportinfo->pmport_addr.qual = SATA_ADDR_PMPORT;
10681 		pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
10682 		mutex_exit(&pmportinfo->pmport_mutex);
10683 
10684 		mutex_enter(&cportinfo->cport_mutex);
10685 		SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) = pmportinfo;
10686 
10687 		/* Create an attachment point */
10688 		minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip),
10689 		    cport, (uint8_t)npmport, SATA_ADDR_PMPORT);
10690 		(void) sprintf(name, "%d.%d", cport, npmport);
10691 
10692 		if (ddi_create_minor_node(dip, name, S_IFCHR, minor_number,
10693 		    DDI_NT_SATA_ATTACHMENT_POINT, 0) != DDI_SUCCESS) {
10694 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
10695 			    "cannot create SATA attachment point for "
10696 			    "port %d:%d", cport, npmport);
10697 		}
10698 	}
10699 
10700 	pmultinfo->pmult_state &= ~SATA_STATE_PROBING;
10701 	pmultinfo->pmult_state |= (SATA_STATE_PROBED|SATA_STATE_READY);
10702 
10703 	mutex_exit(&cportinfo->cport_mutex);
10704 	return (SATA_SUCCESS);
10705 }
10706 
10707 /*
10708  * Free data structures when a port multiplier is removed.
10709  *
10710  * NOTE: No Mutex should be hold.
10711  */
10712 static void
10713 sata_free_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
10714 {
10715 	sata_cport_info_t *cportinfo;
10716 	sata_pmult_info_t *pmultinfo;
10717 	sata_pmport_info_t *pmportinfo;
10718 	sata_device_t pmport_device;
10719 	sata_drive_info_t *sdinfo;
10720 	dev_info_t *tdip;
10721 	char name[16];
10722 	uint8_t cport = sata_device->satadev_addr.cport;
10723 	int npmport;
10724 
10725 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
10726 
10727 	/* This function might be called while port-mult is hot plugged. */
10728 	mutex_enter(&cportinfo->cport_mutex);
10729 
10730 	pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
10731 	ASSERT(pmultinfo != NULL);
10732 
10733 	/* Free pmport_info structure */
10734 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports;
10735 	    npmport++) {
10736 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport);
10737 		if (pmportinfo == NULL)
10738 			continue;
10739 		mutex_exit(&cportinfo->cport_mutex);
10740 
10741 		mutex_enter(&pmportinfo->pmport_mutex);
10742 		sdinfo = pmportinfo->pmport_sata_drive;
10743 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
10744 		mutex_exit(&pmportinfo->pmport_mutex);
10745 
10746 		/* Remove attachment point. */
10747 		name[0] = '\0';
10748 		(void) sprintf(name, "%d.%d", cport, npmport);
10749 		ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name);
10750 		sata_log(sata_hba_inst, CE_NOTE,
10751 		    "Remove attachment point of port %d:%d",
10752 		    cport, npmport);
10753 
10754 		/*
10755 		 * Rumove target node
10756 		 */
10757 		bzero(&pmport_device, sizeof (sata_device_t));
10758 		pmport_device.satadev_rev = SATA_DEVICE_REV;
10759 		pmport_device.satadev_addr.cport = cport;
10760 		pmport_device.satadev_addr.pmport = (uint8_t)npmport;
10761 		pmport_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
10762 
10763 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
10764 		    &(pmport_device.satadev_addr));
10765 		if (tdip != NULL && ndi_devi_offline(tdip,
10766 		    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
10767 			/*
10768 			 * Problem :
10769 			 * The target node remained attached.
10770 			 * This happens when the device file was open
10771 			 * or a node was waiting for resources.
10772 			 * Cannot do anything about it.
10773 			 */
10774 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10775 			    "sata_free_pmult: could not unconfigure device "
10776 			    "before disconnecting the SATA port %d:%d",
10777 			    cport, npmport));
10778 
10779 			/*
10780 			 * Set DEVICE REMOVED state in the target
10781 			 * node. It will prevent access to the device
10782 			 * even when a new device is attached, until
10783 			 * the old target node is released, removed and
10784 			 * recreated for a new  device.
10785 			 */
10786 			sata_set_device_removed(tdip);
10787 
10788 			/*
10789 			 * Instruct event daemon to try the target
10790 			 * node cleanup later.
10791 			 */
10792 			sata_set_target_node_cleanup(
10793 			    sata_hba_inst, &(pmport_device.satadev_addr));
10794 
10795 		}
10796 		mutex_enter(&cportinfo->cport_mutex);
10797 
10798 		/*
10799 		 * Add here differentiation for device attached or not
10800 		 */
10801 		if (sdinfo != NULL)  {
10802 			sata_log(sata_hba_inst, CE_WARN,
10803 			    "SATA device detached from port %d:%d",
10804 			    cport, npmport);
10805 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
10806 		}
10807 
10808 		mutex_destroy(&pmportinfo->pmport_mutex);
10809 		kmem_free(pmportinfo, sizeof (sata_pmport_info_t));
10810 	}
10811 
10812 	kmem_free(pmultinfo, sizeof (sata_pmult_info_t));
10813 
10814 	cportinfo->cport_devp.cport_sata_pmult = NULL;
10815 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
10816 
10817 	sata_log(sata_hba_inst, CE_WARN,
10818 	    "SATA port multiplier detached at port %d", cport);
10819 
10820 	mutex_exit(&cportinfo->cport_mutex);
10821 }
10822 
10823 /*
10824  * Initialize device
10825  * Specified device is initialized to a default state.
10826  *
10827  * Returns SATA_SUCCESS if all device features are set successfully,
10828  * SATA_RETRY if device is accessible but device features were not set
10829  * successfully, and SATA_FAILURE otherwise.
10830  */
10831 static int
10832 sata_initialize_device(sata_hba_inst_t *sata_hba_inst,
10833     sata_drive_info_t *sdinfo)
10834 {
10835 	int rval;
10836 
10837 	sata_save_drive_settings(sdinfo);
10838 
10839 	sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
10840 
10841 	sata_init_write_cache_mode(sdinfo);
10842 
10843 	rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0);
10844 
10845 	/* Determine current data transfer mode */
10846 	if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) {
10847 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
10848 	} else if ((sdinfo->satadrv_id.ai_validinfo &
10849 	    SATA_VALIDINFO_88) != 0 &&
10850 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) {
10851 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
10852 	} else if ((sdinfo->satadrv_id.ai_dworddma &
10853 	    SATA_MDMA_SEL_MASK) != 0) {
10854 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
10855 	} else
10856 		/* DMA supported, not no DMA transfer mode is selected !? */
10857 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
10858 
10859 	if ((sdinfo->satadrv_id.ai_cmdset83 & 0x20) &&
10860 	    (sdinfo->satadrv_id.ai_features86 & 0x20))
10861 		sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
10862 	else
10863 		sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
10864 
10865 	return (rval);
10866 }
10867 
10868 
10869 /*
10870  * Initialize write cache mode.
10871  *
10872  * The default write cache setting for SATA HDD is provided by sata_write_cache
10873  * static variable. ATAPI CD/DVDs devices have write cache default is
10874  * determined by sata_atapicdvd_write_cache static variable.
10875  * ATAPI tape devices have write cache default is determined by
10876  * sata_atapitape_write_cache static variable.
10877  * ATAPI disk devices have write cache default is determined by
10878  * sata_atapidisk_write_cache static variable.
10879  * 1 - enable
10880  * 0 - disable
10881  * any other value - current drive setting
10882  *
10883  * Although there is not reason to disable write cache on CD/DVD devices,
10884  * tape devices and ATAPI disk devices, the default setting control is provided
10885  * for the maximun flexibility.
10886  *
10887  * In the future, it may be overridden by the
10888  * disk-write-cache-enable property setting, if it is defined.
10889  * Returns SATA_SUCCESS if all device features are set successfully,
10890  * SATA_FAILURE otherwise.
10891  */
10892 static void
10893 sata_init_write_cache_mode(sata_drive_info_t *sdinfo)
10894 {
10895 	switch (sdinfo->satadrv_type) {
10896 	case SATA_DTYPE_ATADISK:
10897 		if (sata_write_cache == 1)
10898 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
10899 		else if (sata_write_cache == 0)
10900 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
10901 		/*
10902 		 * When sata_write_cache value is not 0 or 1,
10903 		 * a current setting of the drive's write cache is used.
10904 		 */
10905 		break;
10906 	case SATA_DTYPE_ATAPICD:
10907 		if (sata_atapicdvd_write_cache == 1)
10908 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
10909 		else if (sata_atapicdvd_write_cache == 0)
10910 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
10911 		/*
10912 		 * When sata_atapicdvd_write_cache value is not 0 or 1,
10913 		 * a current setting of the drive's write cache is used.
10914 		 */
10915 		break;
10916 	case SATA_DTYPE_ATAPITAPE:
10917 		if (sata_atapitape_write_cache == 1)
10918 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
10919 		else if (sata_atapitape_write_cache == 0)
10920 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
10921 		/*
10922 		 * When sata_atapitape_write_cache value is not 0 or 1,
10923 		 * a current setting of the drive's write cache is used.
10924 		 */
10925 		break;
10926 	case SATA_DTYPE_ATAPIDISK:
10927 		if (sata_atapidisk_write_cache == 1)
10928 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
10929 		else if (sata_atapidisk_write_cache == 0)
10930 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
10931 		/*
10932 		 * When sata_atapidisk_write_cache value is not 0 or 1,
10933 		 * a current setting of the drive's write cache is used.
10934 		 */
10935 		break;
10936 	}
10937 }
10938 
10939 
10940 /*
10941  * Validate sata address.
10942  * Specified cport, pmport and qualifier has to match
10943  * passed sata_scsi configuration info.
10944  * The presence of an attached device is not verified.
10945  *
10946  * Returns 0 when address is valid, -1 otherwise.
10947  */
10948 static int
10949 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport,
10950 	int pmport, int qual)
10951 {
10952 	if (qual == SATA_ADDR_DCPORT && pmport != 0)
10953 		goto invalid_address;
10954 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
10955 		goto invalid_address;
10956 	if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) &&
10957 	    ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) ||
10958 	    (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) ||
10959 	    (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport))))
10960 		goto invalid_address;
10961 
10962 	return (0);
10963 
10964 invalid_address:
10965 	return (-1);
10966 
10967 }
10968 
10969 /*
10970  * Validate scsi address
10971  * SCSI target address is translated into SATA cport/pmport and compared
10972  * with a controller port/device configuration. LUN has to be 0.
10973  * Returns 0 if a scsi target refers to an attached device,
10974  * returns 1 if address is valid but no valid device is attached,
10975  * returns 2 if address is valid but device type is unknown (not valid device),
10976  * returns -1 if bad address or device is of an unsupported type.
10977  * Upon return sata_device argument is set.
10978  *
10979  * Port multiplier is supported now.
10980  */
10981 static int
10982 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst,
10983 	struct scsi_address *ap, sata_device_t *sata_device)
10984 {
10985 	int cport, pmport, qual, rval;
10986 
10987 	rval = -1;	/* Invalid address */
10988 	if (ap->a_lun != 0)
10989 		goto out;
10990 
10991 	qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
10992 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
10993 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
10994 
10995 	if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT)
10996 		goto out;
10997 
10998 	if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) ==
10999 	    0) {
11000 
11001 		sata_cport_info_t *cportinfo;
11002 		sata_pmult_info_t *pmultinfo;
11003 		sata_drive_info_t *sdinfo = NULL;
11004 
11005 		sata_device->satadev_addr.qual = qual;
11006 		sata_device->satadev_addr.cport = cport;
11007 		sata_device->satadev_addr.pmport = pmport;
11008 		sata_device->satadev_rev = SATA_DEVICE_REV_1;
11009 
11010 		rval = 1;	/* Valid sata address */
11011 
11012 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11013 		if (qual == SATA_ADDR_DCPORT) {
11014 			if (cportinfo == NULL ||
11015 			    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
11016 				goto out;
11017 
11018 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11019 			if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN &&
11020 			    sdinfo != NULL) {
11021 				rval = 2;
11022 				goto out;
11023 			}
11024 
11025 			if ((cportinfo->cport_dev_type &
11026 			    SATA_VALID_DEV_TYPE) == 0) {
11027 				rval = -1;
11028 				goto out;
11029 			}
11030 
11031 		} else if (qual == SATA_ADDR_DPMPORT) {
11032 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
11033 			if (pmultinfo == NULL) {
11034 				rval = -1;
11035 				goto out;
11036 			}
11037 			if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) ==
11038 			    NULL ||
11039 			    SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
11040 			    pmport) == SATA_DTYPE_NONE)
11041 				goto out;
11042 
11043 			sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport,
11044 			    pmport);
11045 			if (SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
11046 			    pmport) == SATA_DTYPE_UNKNOWN && sdinfo != NULL) {
11047 				rval = 2;
11048 				goto out;
11049 			}
11050 
11051 			if ((SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
11052 			    pmport) && SATA_VALID_DEV_TYPE) == 0) {
11053 				rval = -1;
11054 				goto out;
11055 			}
11056 
11057 		} else {
11058 			rval = -1;
11059 			goto out;
11060 		}
11061 		if ((sdinfo == NULL) ||
11062 		    (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0)
11063 			goto out;
11064 
11065 		sata_device->satadev_type = sdinfo->satadrv_type;
11066 
11067 		return (0);
11068 	}
11069 out:
11070 	if (rval > 0) {
11071 		SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
11072 		    "sata_validate_scsi_address: no valid target %x lun %x",
11073 		    ap->a_target, ap->a_lun);
11074 	}
11075 	return (rval);
11076 }
11077 
11078 /*
11079  * Find dip corresponding to passed device number
11080  *
11081  * Returns NULL if invalid device number is passed or device cannot be found,
11082  * Returns dip is device is found.
11083  */
11084 static dev_info_t *
11085 sata_devt_to_devinfo(dev_t dev)
11086 {
11087 	dev_info_t *dip;
11088 #ifndef __lock_lint
11089 	struct devnames *dnp;
11090 	major_t major = getmajor(dev);
11091 	int instance = SATA_MINOR2INSTANCE(getminor(dev));
11092 
11093 	if (major >= devcnt)
11094 		return (NULL);
11095 
11096 	dnp = &devnamesp[major];
11097 	LOCK_DEV_OPS(&(dnp->dn_lock));
11098 	dip = dnp->dn_head;
11099 	while (dip && (ddi_get_instance(dip) != instance)) {
11100 		dip = ddi_get_next(dip);
11101 	}
11102 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
11103 #endif
11104 
11105 	return (dip);
11106 }
11107 
11108 
11109 /*
11110  * Probe device.
11111  * This function issues Identify Device command and initializes local
11112  * sata_drive_info structure if the device can be identified.
11113  * The device type is determined by examining Identify Device
11114  * command response.
11115  * If the sata_hba_inst has linked drive info structure for this
11116  * device address, the Identify Device data is stored into sata_drive_info
11117  * structure linked to the port info structure.
11118  *
11119  * sata_device has to refer to the valid sata port(s) for HBA described
11120  * by sata_hba_inst structure.
11121  *
11122  * Returns:
11123  *	SATA_SUCCESS if device type was successfully probed and port-linked
11124  *		drive info structure was updated;
11125  * 	SATA_FAILURE if there is no device, or device was not probed
11126  *		successully;
11127  *	SATA_RETRY if device probe can be retried later.
11128  * If a device cannot be identified, sata_device's dev_state and dev_type
11129  * fields are set to unknown.
11130  * There are no retries in this function. Any retries should be managed by
11131  * the caller.
11132  */
11133 
11134 
11135 static int
11136 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
11137 {
11138 	sata_pmport_info_t *pmportinfo;
11139 	sata_drive_info_t *sdinfo;
11140 	sata_drive_info_t new_sdinfo;	/* local drive info struct */
11141 	int rval;
11142 
11143 	ASSERT((SATA_CPORT_STATE(sata_hba_inst,
11144 	    sata_device->satadev_addr.cport) &
11145 	    (SATA_STATE_PROBED | SATA_STATE_READY)) != 0);
11146 
11147 	sata_device->satadev_type = SATA_DTYPE_NONE;
11148 
11149 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
11150 	    sata_device->satadev_addr.cport)));
11151 
11152 	if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT) {
11153 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
11154 		    sata_device->satadev_addr.cport,
11155 		    sata_device->satadev_addr.pmport);
11156 		ASSERT(pmportinfo != NULL);
11157 	}
11158 
11159 	/* Get pointer to port-linked sata device info structure */
11160 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
11161 	if (sdinfo != NULL) {
11162 		sdinfo->satadrv_state &=
11163 		    ~(SATA_STATE_PROBED | SATA_STATE_READY);
11164 		sdinfo->satadrv_state |= SATA_STATE_PROBING;
11165 	} else {
11166 		/* No device to probe */
11167 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
11168 		    sata_device->satadev_addr.cport)));
11169 		sata_device->satadev_type = SATA_DTYPE_NONE;
11170 		sata_device->satadev_state = SATA_STATE_UNKNOWN;
11171 		return (SATA_FAILURE);
11172 	}
11173 	/*
11174 	 * Need to issue both types of identify device command and
11175 	 * determine device type by examining retreived data/status.
11176 	 * First, ATA Identify Device.
11177 	 */
11178 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
11179 	new_sdinfo.satadrv_addr = sata_device->satadev_addr;
11180 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
11181 	    sata_device->satadev_addr.cport)));
11182 	new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
11183 	rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
11184 	if (rval == SATA_RETRY) {
11185 		/* We may try to check for ATAPI device */
11186 		if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) {
11187 			/*
11188 			 * HBA supports ATAPI - try to issue Identify Packet
11189 			 * Device command.
11190 			 */
11191 			new_sdinfo.satadrv_type = SATA_DTYPE_ATAPI;
11192 			rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
11193 		}
11194 	}
11195 	if (rval == SATA_SUCCESS) {
11196 		/*
11197 		 * Got something responding positively to ATA Identify Device
11198 		 * or to Identify Packet Device cmd.
11199 		 * Save last used device type.
11200 		 */
11201 		sata_device->satadev_type = new_sdinfo.satadrv_type;
11202 
11203 		/* save device info, if possible */
11204 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
11205 		    sata_device->satadev_addr.cport)));
11206 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
11207 		if (sdinfo == NULL) {
11208 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
11209 			    sata_device->satadev_addr.cport)));
11210 			return (SATA_FAILURE);
11211 		}
11212 		/*
11213 		 * Copy drive info into the port-linked drive info structure.
11214 		 */
11215 		*sdinfo = new_sdinfo;
11216 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
11217 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
11218 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
11219 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
11220 			    sata_device->satadev_addr.cport) =
11221 			    sdinfo->satadrv_type;
11222 		else { /* SATA_ADDR_DPMPORT */
11223 			mutex_enter(&pmportinfo->pmport_mutex);
11224 			SATA_PMPORT_DEV_TYPE(sata_hba_inst,
11225 			    sata_device->satadev_addr.cport,
11226 			    sata_device->satadev_addr.pmport) =
11227 			    sdinfo->satadrv_type;
11228 			mutex_exit(&pmportinfo->pmport_mutex);
11229 		}
11230 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
11231 		    sata_device->satadev_addr.cport)));
11232 		return (SATA_SUCCESS);
11233 	}
11234 
11235 	/*
11236 	 * It may be SATA_RETRY or SATA_FAILURE return.
11237 	 * Looks like we cannot determine the device type at this time.
11238 	 */
11239 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
11240 	    sata_device->satadev_addr.cport)));
11241 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
11242 	if (sdinfo != NULL) {
11243 		sata_device->satadev_type = SATA_DTYPE_UNKNOWN;
11244 		sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11245 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
11246 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
11247 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
11248 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
11249 			    sata_device->satadev_addr.cport) =
11250 			    SATA_DTYPE_UNKNOWN;
11251 		else {
11252 			/* SATA_ADDR_DPMPORT */
11253 			mutex_enter(&pmportinfo->pmport_mutex);
11254 			if ((SATA_PMULT_INFO(sata_hba_inst,
11255 			    sata_device->satadev_addr.cport) != NULL) &&
11256 			    (SATA_PMPORT_INFO(sata_hba_inst,
11257 			    sata_device->satadev_addr.cport,
11258 			    sata_device->satadev_addr.pmport) != NULL))
11259 				SATA_PMPORT_DEV_TYPE(sata_hba_inst,
11260 				    sata_device->satadev_addr.cport,
11261 				    sata_device->satadev_addr.pmport) =
11262 				    SATA_DTYPE_UNKNOWN;
11263 			mutex_exit(&pmportinfo->pmport_mutex);
11264 		}
11265 	}
11266 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
11267 	    sata_device->satadev_addr.cport)));
11268 	return (rval);
11269 }
11270 
11271 
11272 /*
11273  * Get pointer to sata_drive_info structure.
11274  *
11275  * The sata_device has to contain address (cport, pmport and qualifier) for
11276  * specified sata_scsi structure.
11277  *
11278  * Returns NULL if device address is not valid for this HBA configuration.
11279  * Otherwise, returns a pointer to sata_drive_info structure.
11280  *
11281  * This function should be called with a port mutex held.
11282  */
11283 static sata_drive_info_t *
11284 sata_get_device_info(sata_hba_inst_t *sata_hba_inst,
11285     sata_device_t *sata_device)
11286 {
11287 	uint8_t cport = sata_device->satadev_addr.cport;
11288 	uint8_t pmport = sata_device->satadev_addr.pmport;
11289 	uint8_t qual = sata_device->satadev_addr.qual;
11290 
11291 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
11292 		return (NULL);
11293 
11294 	if (!(SATA_CPORT_STATE(sata_hba_inst, cport) &
11295 	    (SATA_STATE_PROBED | SATA_STATE_READY)))
11296 		/* Port not probed yet */
11297 		return (NULL);
11298 
11299 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE)
11300 		return (NULL);
11301 
11302 	if (qual == SATA_ADDR_DCPORT) {
11303 		/* Request for a device on a controller port */
11304 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
11305 		    SATA_DTYPE_PMULT)
11306 			/* Port multiplier attached */
11307 			return (NULL);
11308 		return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport));
11309 	}
11310 	if (qual == SATA_ADDR_DPMPORT) {
11311 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
11312 		    SATA_DTYPE_PMULT)
11313 			return (NULL);
11314 
11315 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport))
11316 			return (NULL);
11317 
11318 		if (!(SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) &
11319 		    (SATA_STATE_PROBED | SATA_STATE_READY)))
11320 			/* Port multiplier port not probed yet */
11321 			return (NULL);
11322 
11323 		return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport));
11324 	}
11325 
11326 	/* we should not get here */
11327 	return (NULL);
11328 }
11329 
11330 
11331 /*
11332  * sata_identify_device.
11333  * Send Identify Device command to SATA HBA driver.
11334  * If command executes successfully, update sata_drive_info structure pointed
11335  * to by sdinfo argument, including Identify Device data.
11336  * If command fails, invalidate data in sata_drive_info.
11337  *
11338  * Cannot be called from interrupt level.
11339  *
11340  * Returns:
11341  * SATA_SUCCESS if the device was identified as a supported device,
11342  * SATA_RETRY if the device was not identified but could be retried,
11343  * SATA_FAILURE if the device was not identified and identify attempt
11344  *	should not be retried.
11345  */
11346 static int
11347 sata_identify_device(sata_hba_inst_t *sata_hba_inst,
11348     sata_drive_info_t *sdinfo)
11349 {
11350 	uint16_t cfg_word;
11351 	int rval;
11352 
11353 	/* fetch device identify data */
11354 	if ((rval = sata_fetch_device_identify_data(sata_hba_inst,
11355 	    sdinfo)) != SATA_SUCCESS)
11356 		goto fail_unknown;
11357 
11358 	cfg_word = sdinfo->satadrv_id.ai_config;
11359 
11360 	/* Set the correct device type */
11361 	if ((cfg_word & SATA_ATA_TYPE_MASK) == SATA_ATA_TYPE) {
11362 		sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
11363 	} else if (cfg_word == SATA_CFA_TYPE) {
11364 		/* It's a Compact Flash media via CF-to-SATA HDD adapter */
11365 		sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
11366 	} else if ((cfg_word & SATA_ATAPI_TYPE_MASK) == SATA_ATAPI_TYPE) {
11367 		switch (cfg_word & SATA_ATAPI_ID_DEV_TYPE) {
11368 		case SATA_ATAPI_CDROM_DEV:
11369 			sdinfo->satadrv_type = SATA_DTYPE_ATAPICD;
11370 			break;
11371 		case SATA_ATAPI_SQACC_DEV:
11372 			sdinfo->satadrv_type = SATA_DTYPE_ATAPITAPE;
11373 			break;
11374 		case SATA_ATAPI_DIRACC_DEV:
11375 			sdinfo->satadrv_type = SATA_DTYPE_ATAPIDISK;
11376 			break;
11377 		default:
11378 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11379 		}
11380 	} else {
11381 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11382 	}
11383 
11384 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
11385 		if (sdinfo->satadrv_capacity == 0) {
11386 			/* Non-LBA disk. Too bad... */
11387 			sata_log(sata_hba_inst, CE_WARN,
11388 			    "SATA disk device at port %d does not support LBA",
11389 			    sdinfo->satadrv_addr.cport);
11390 			rval = SATA_FAILURE;
11391 			goto fail_unknown;
11392 		}
11393 	}
11394 #if 0
11395 	/* Left for historical reason */
11396 	/*
11397 	 * Some initial version of SATA spec indicated that at least
11398 	 * UDMA mode 4 has to be supported. It is not metioned in
11399 	 * SerialATA 2.6, so this restriction is removed.
11400 	 */
11401 	/* Check for Ultra DMA modes 6 through 0 being supported */
11402 	for (i = 6; i >= 0; --i) {
11403 		if (sdinfo->satadrv_id.ai_ultradma & (1 << i))
11404 			break;
11405 	}
11406 
11407 	/*
11408 	 * At least UDMA 4 mode has to be supported. If mode 4 or
11409 	 * higher are not supported by the device, fail this
11410 	 * device.
11411 	 */
11412 	if (i < 4) {
11413 		/* No required Ultra DMA mode supported */
11414 		sata_log(sata_hba_inst, CE_WARN,
11415 		    "SATA disk device at port %d does not support UDMA "
11416 		    "mode 4 or higher", sdinfo->satadrv_addr.cport);
11417 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11418 		    "mode 4 or higher required, %d supported", i));
11419 		rval = SATA_FAILURE;
11420 		goto fail_unknown;
11421 	}
11422 #endif
11423 
11424 	/*
11425 	 * For Disk devices, if it doesn't support UDMA mode, we would
11426 	 * like to return failure directly.
11427 	 */
11428 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
11429 	    !((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
11430 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0)) {
11431 		sata_log(sata_hba_inst, CE_WARN,
11432 		    "SATA disk device at port %d does not support UDMA",
11433 		    sdinfo->satadrv_addr.cport);
11434 		rval = SATA_FAILURE;
11435 		goto fail_unknown;
11436 	}
11437 
11438 	return (SATA_SUCCESS);
11439 
11440 fail_unknown:
11441 	/* Invalidate sata_drive_info ? */
11442 	sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11443 	sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
11444 	return (rval);
11445 }
11446 
11447 /*
11448  * Log/display device information
11449  */
11450 static void
11451 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst,
11452     sata_drive_info_t *sdinfo)
11453 {
11454 	int valid_version;
11455 	char msg_buf[MAXPATHLEN];
11456 	int i;
11457 
11458 	/* Show HBA path */
11459 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf);
11460 
11461 	cmn_err(CE_CONT, "?%s :\n", msg_buf);
11462 
11463 	switch (sdinfo->satadrv_type) {
11464 	case SATA_DTYPE_ATADISK:
11465 		(void) sprintf(msg_buf, "SATA disk device at");
11466 		break;
11467 
11468 	case SATA_DTYPE_ATAPICD:
11469 		(void) sprintf(msg_buf, "SATA CD/DVD (ATAPI) device at");
11470 		break;
11471 
11472 	case SATA_DTYPE_ATAPITAPE:
11473 		(void) sprintf(msg_buf, "SATA tape (ATAPI) device at");
11474 		break;
11475 
11476 	case SATA_DTYPE_ATAPIDISK:
11477 		(void) sprintf(msg_buf, "SATA disk (ATAPI) device at");
11478 		break;
11479 
11480 	case SATA_DTYPE_UNKNOWN:
11481 		(void) sprintf(msg_buf,
11482 		    "Unsupported SATA device type (cfg 0x%x) at ",
11483 		    sdinfo->satadrv_id.ai_config);
11484 		break;
11485 	}
11486 
11487 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT)
11488 		cmn_err(CE_CONT, "?\t%s port %d\n",
11489 		    msg_buf, sdinfo->satadrv_addr.cport);
11490 	else
11491 		cmn_err(CE_CONT, "?\t%s port %d:%d\n",
11492 		    msg_buf, sdinfo->satadrv_addr.cport,
11493 		    sdinfo->satadrv_addr.pmport);
11494 
11495 	bcopy(&sdinfo->satadrv_id.ai_model, msg_buf,
11496 	    sizeof (sdinfo->satadrv_id.ai_model));
11497 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model));
11498 	msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
11499 	cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf);
11500 
11501 	bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf,
11502 	    sizeof (sdinfo->satadrv_id.ai_fw));
11503 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw));
11504 	msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
11505 	cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf);
11506 
11507 	bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf,
11508 	    sizeof (sdinfo->satadrv_id.ai_drvser));
11509 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser));
11510 	msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
11511 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
11512 		cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
11513 	} else {
11514 		/*
11515 		 * Some drives do not implement serial number and may
11516 		 * violate the spec by providing spaces rather than zeros
11517 		 * in serial number field. Scan the buffer to detect it.
11518 		 */
11519 		for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) {
11520 			if (msg_buf[i] != '\0' && msg_buf[i] != ' ')
11521 				break;
11522 		}
11523 		if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) {
11524 			cmn_err(CE_CONT, "?\tserial number - none\n");
11525 		} else {
11526 			cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
11527 		}
11528 	}
11529 
11530 #ifdef SATA_DEBUG
11531 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
11532 	    sdinfo->satadrv_id.ai_majorversion != 0xffff) {
11533 		int i;
11534 		for (i = 14; i >= 2; i--) {
11535 			if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) {
11536 				valid_version = i;
11537 				break;
11538 			}
11539 		}
11540 		cmn_err(CE_CONT,
11541 		    "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
11542 		    valid_version,
11543 		    sdinfo->satadrv_id.ai_majorversion,
11544 		    sdinfo->satadrv_id.ai_minorversion);
11545 	}
11546 #endif
11547 	/* Log some info */
11548 	cmn_err(CE_CONT, "?\tsupported features:\n");
11549 	msg_buf[0] = '\0';
11550 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
11551 		if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48)
11552 			(void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN);
11553 		else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28)
11554 			(void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN);
11555 	}
11556 	if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA)
11557 		(void) strlcat(msg_buf, "DMA", MAXPATHLEN);
11558 	if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ)
11559 		(void) strlcat(msg_buf, ", Native Command Queueing",
11560 		    MAXPATHLEN);
11561 	if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)
11562 		(void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN);
11563 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) &&
11564 	    (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED))
11565 		(void) strlcat(msg_buf, ", SMART", MAXPATHLEN);
11566 	if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) &&
11567 	    (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED))
11568 		(void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN);
11569 	cmn_err(CE_CONT, "?\t %s\n", msg_buf);
11570 	if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2)
11571 		cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n");
11572 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1)
11573 		cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n");
11574 	if (sdinfo->satadrv_features_support &
11575 	    (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) {
11576 		msg_buf[0] = '\0';
11577 		(void) snprintf(msg_buf, MAXPATHLEN,
11578 		    "Supported queue depth %d",
11579 		    sdinfo->satadrv_queue_depth);
11580 		if (!(sata_func_enable &
11581 		    (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ)))
11582 			(void) strlcat(msg_buf,
11583 			    " - queueing disabled globally", MAXPATHLEN);
11584 		else if (sdinfo->satadrv_queue_depth >
11585 		    sdinfo->satadrv_max_queue_depth) {
11586 			(void) snprintf(&msg_buf[strlen(msg_buf)],
11587 			    MAXPATHLEN - strlen(msg_buf), ", limited to %d",
11588 			    (int)sdinfo->satadrv_max_queue_depth);
11589 		}
11590 		cmn_err(CE_CONT, "?\t%s\n", msg_buf);
11591 	}
11592 
11593 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
11594 #ifdef __i386
11595 		(void) sprintf(msg_buf, "\tcapacity = %llu sectors\n",
11596 		    sdinfo->satadrv_capacity);
11597 #else
11598 		(void) sprintf(msg_buf, "\tcapacity = %lu sectors\n",
11599 		    sdinfo->satadrv_capacity);
11600 #endif
11601 		cmn_err(CE_CONT, "?%s", msg_buf);
11602 	}
11603 }
11604 
11605 /*
11606  * Log/display port multiplier information
11607  * No Mutex should be hold.
11608  */
11609 static void
11610 sata_show_pmult_info(sata_hba_inst_t *sata_hba_inst,
11611     sata_device_t *sata_device)
11612 {
11613 	_NOTE(ARGUNUSED(sata_hba_inst))
11614 
11615 	int cport = sata_device->satadev_addr.cport;
11616 	sata_pmult_info_t *pmultinfo;
11617 	char msg_buf[MAXPATHLEN];
11618 	uint32_t gscr0, gscr1, gscr2, gscr64;
11619 
11620 	mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
11621 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
11622 	if (pmultinfo == NULL) {
11623 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
11624 		return;
11625 	}
11626 
11627 	gscr0 = pmultinfo->pmult_gscr.gscr0;
11628 	gscr1 = pmultinfo->pmult_gscr.gscr1;
11629 	gscr2 = pmultinfo->pmult_gscr.gscr2;
11630 	gscr64 = pmultinfo->pmult_gscr.gscr64;
11631 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
11632 
11633 	cmn_err(CE_CONT, "?Port Multiplier %d device-ports found at port %d",
11634 	    sata_device->satadev_add_info, sata_device->satadev_addr.cport);
11635 
11636 	(void) sprintf(msg_buf, "\tVendor_ID 0x%04x, Module_ID 0x%04x",
11637 	    gscr0 & 0xffff, (gscr0 >> 16) & 0xffff);
11638 	cmn_err(CE_CONT, "?%s", msg_buf);
11639 
11640 	(void) strcpy(msg_buf, "\tSupport SATA PMP Spec ");
11641 	if (gscr1 & (1 << 3))
11642 		(void) strlcat(msg_buf, "1.2", MAXPATHLEN);
11643 	else if (gscr1 & (1 << 2))
11644 		(void) strlcat(msg_buf, "1.1", MAXPATHLEN);
11645 	else if (gscr1 & (1 << 1))
11646 		(void) strlcat(msg_buf, "1.0", MAXPATHLEN);
11647 	else
11648 		(void) strlcat(msg_buf, "unknown", MAXPATHLEN);
11649 	cmn_err(CE_CONT, "?%s", msg_buf);
11650 
11651 	(void) strcpy(msg_buf, "\tSupport ");
11652 	if (gscr64 & (1 << 3))
11653 		(void) strlcat(msg_buf, "Asy-Notif, ",
11654 		    MAXPATHLEN);
11655 	if (gscr64 & (1 << 2))
11656 		(void) strlcat(msg_buf, "Dyn-SSC, ", MAXPATHLEN);
11657 	if (gscr64 & (1 << 1))
11658 		(void) strlcat(msg_buf, "Iss-PMREQ, ", MAXPATHLEN);
11659 	if (gscr64 & (1 << 0))
11660 		(void) strlcat(msg_buf, "BIST", MAXPATHLEN);
11661 	if ((gscr64 & 0xf) == 0)
11662 		(void) strlcat(msg_buf, "nothing", MAXPATHLEN);
11663 	cmn_err(CE_CONT, "?%s", msg_buf);
11664 
11665 	(void) sprintf(msg_buf, "\tNumber of exposed device fan-out ports: %d",
11666 	    gscr2 & SATA_PMULT_PORTNUM_MASK);
11667 	cmn_err(CE_CONT, "?%s", msg_buf);
11668 }
11669 
11670 /*
11671  * sata_save_drive_settings extracts current setting of the device and stores
11672  * it for future reference, in case the device setup would need to be restored
11673  * after the device reset.
11674  *
11675  * For all devices read ahead and write cache settings are saved, if the
11676  * device supports these features at all.
11677  * For ATAPI devices the Removable Media Status Notification setting is saved.
11678  */
11679 static void
11680 sata_save_drive_settings(sata_drive_info_t *sdinfo)
11681 {
11682 	if (SATA_READ_AHEAD_SUPPORTED(sdinfo->satadrv_id) ||
11683 	    SATA_WRITE_CACHE_SUPPORTED(sdinfo->satadrv_id)) {
11684 
11685 		/* Current setting of Read Ahead (and Read Cache) */
11686 		if (SATA_READ_AHEAD_ENABLED(sdinfo->satadrv_id))
11687 			sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
11688 		else
11689 			sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD;
11690 
11691 		/* Current setting of Write Cache */
11692 		if (SATA_WRITE_CACHE_ENABLED(sdinfo->satadrv_id))
11693 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
11694 		else
11695 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
11696 	}
11697 
11698 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
11699 		if (SATA_RM_NOTIFIC_SUPPORTED(sdinfo->satadrv_id))
11700 			sdinfo->satadrv_settings |= SATA_DEV_RMSN;
11701 		else
11702 			sdinfo->satadrv_settings &= ~SATA_DEV_RMSN;
11703 	}
11704 }
11705 
11706 
11707 /*
11708  * sata_check_capacity function determines a disk capacity
11709  * and addressing mode (LBA28/LBA48) by examining a disk identify device data.
11710  *
11711  * NOTE: CHS mode is not supported! If a device does not support LBA,
11712  * this function is not called.
11713  *
11714  * Returns device capacity in number of blocks, i.e. largest addressable LBA+1
11715  */
11716 static uint64_t
11717 sata_check_capacity(sata_drive_info_t *sdinfo)
11718 {
11719 	uint64_t capacity = 0;
11720 	int i;
11721 
11722 	if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK ||
11723 	    !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT)
11724 		/* Capacity valid only for LBA-addressable disk devices */
11725 		return (0);
11726 
11727 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) &&
11728 	    (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) &&
11729 	    (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) {
11730 		/* LBA48 mode supported and enabled */
11731 		sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 |
11732 		    SATA_DEV_F_LBA28;
11733 		for (i = 3;  i >= 0;  --i) {
11734 			capacity <<= 16;
11735 			capacity += sdinfo->satadrv_id.ai_addrsecxt[i];
11736 		}
11737 	} else {
11738 		capacity = sdinfo->satadrv_id.ai_addrsec[1];
11739 		capacity <<= 16;
11740 		capacity += sdinfo->satadrv_id.ai_addrsec[0];
11741 		if (capacity >= 0x1000000)
11742 			/* LBA28 mode */
11743 			sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28;
11744 	}
11745 	return (capacity);
11746 }
11747 
11748 
11749 /*
11750  * Allocate consistent buffer for DMA transfer
11751  *
11752  * Cannot be called from interrupt level or with mutex held - it may sleep.
11753  *
11754  * Returns pointer to allocated buffer structure, or NULL if allocation failed.
11755  */
11756 static struct buf *
11757 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len)
11758 {
11759 	struct scsi_address ap;
11760 	struct buf *bp;
11761 	ddi_dma_attr_t	cur_dma_attr;
11762 
11763 	ASSERT(spx->txlt_sata_pkt != NULL);
11764 	ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran;
11765 	ap.a_target = SATA_TO_SCSI_TARGET(
11766 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport,
11767 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport,
11768 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual);
11769 	ap.a_lun = 0;
11770 
11771 	bp = scsi_alloc_consistent_buf(&ap, NULL, len,
11772 	    B_READ, SLEEP_FUNC, NULL);
11773 
11774 	if (bp != NULL) {
11775 		/* Allocate DMA resources for this buffer */
11776 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
11777 		/*
11778 		 * We use a local version of the dma_attr, to account
11779 		 * for a device addressing limitations.
11780 		 * sata_adjust_dma_attr() will handle sdinfo == NULL which
11781 		 * will cause dma attributes to be adjusted to a lowest
11782 		 * acceptable level.
11783 		 */
11784 		sata_adjust_dma_attr(NULL,
11785 		    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
11786 
11787 		if (sata_dma_buf_setup(spx, PKT_CONSISTENT,
11788 		    SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) {
11789 			scsi_free_consistent_buf(bp);
11790 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
11791 			bp = NULL;
11792 		}
11793 	}
11794 	return (bp);
11795 }
11796 
11797 /*
11798  * Release local buffer (consistent buffer for DMA transfer) allocated
11799  * via sata_alloc_local_buffer().
11800  */
11801 static void
11802 sata_free_local_buffer(sata_pkt_txlate_t *spx)
11803 {
11804 	ASSERT(spx->txlt_sata_pkt != NULL);
11805 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL);
11806 
11807 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0;
11808 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL;
11809 
11810 	sata_common_free_dma_rsrcs(spx);
11811 
11812 	/* Free buffer */
11813 	scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp);
11814 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
11815 }
11816 
11817 /*
11818  * Allocate sata_pkt
11819  * Pkt structure version and embedded strcutures version are initialized.
11820  * sata_pkt and sata_pkt_txlate structures are cross-linked.
11821  *
11822  * Since this may be called in interrupt context by sata_scsi_init_pkt,
11823  * callback argument determines if it can sleep or not.
11824  * Hence, it should not be called from interrupt context.
11825  *
11826  * If successful, non-NULL pointer to a sata pkt is returned.
11827  * Upon failure, NULL pointer is returned.
11828  */
11829 static sata_pkt_t *
11830 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t))
11831 {
11832 	sata_pkt_t *spkt;
11833 	int kmsflag;
11834 
11835 	kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
11836 	spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag);
11837 	if (spkt == NULL) {
11838 		SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
11839 		    "sata_pkt_alloc: failed"));
11840 		return (NULL);
11841 	}
11842 	spkt->satapkt_rev = SATA_PKT_REV;
11843 	spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV;
11844 	spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
11845 	spkt->satapkt_framework_private = spx;
11846 	spx->txlt_sata_pkt = spkt;
11847 	return (spkt);
11848 }
11849 
11850 /*
11851  * Free sata pkt allocated via sata_pkt_alloc()
11852  */
11853 static void
11854 sata_pkt_free(sata_pkt_txlate_t *spx)
11855 {
11856 	ASSERT(spx->txlt_sata_pkt != NULL);
11857 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL);
11858 	kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t));
11859 	spx->txlt_sata_pkt = NULL;
11860 }
11861 
11862 
11863 /*
11864  * Adjust DMA attributes.
11865  * SCSI cmds block count is up to 24 bits, SATA cmd block count vary
11866  * from 8 bits to 16 bits, depending on a command being used.
11867  * Limiting max block count arbitrarily to 256 for all read/write
11868  * commands may affects performance, so check both the device and
11869  * controller capability before adjusting dma attributes.
11870  */
11871 void
11872 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr,
11873     ddi_dma_attr_t *adj_dma_attr)
11874 {
11875 	uint32_t count_max;
11876 
11877 	/* Copy original attributes */
11878 	*adj_dma_attr = *dma_attr;
11879 	/*
11880 	 * Things to consider: device addressing capability,
11881 	 * "excessive" controller DMA capabilities.
11882 	 * If a device is being probed/initialized, there are
11883 	 * no device info - use default limits then.
11884 	 */
11885 	if (sdinfo == NULL) {
11886 		count_max = dma_attr->dma_attr_granular * 0x100;
11887 		if (dma_attr->dma_attr_count_max > count_max)
11888 			adj_dma_attr->dma_attr_count_max = count_max;
11889 		if (dma_attr->dma_attr_maxxfer > count_max)
11890 			adj_dma_attr->dma_attr_maxxfer = count_max;
11891 		return;
11892 	}
11893 
11894 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
11895 		if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) {
11896 			/*
11897 			 * 16-bit sector count may be used - we rely on
11898 			 * the assumption that only read and write cmds
11899 			 * will request more than 256 sectors worth of data
11900 			 */
11901 			count_max = adj_dma_attr->dma_attr_granular * 0x10000;
11902 		} else {
11903 			/*
11904 			 * 8-bit sector count will be used - default limits
11905 			 * for dma attributes
11906 			 */
11907 			count_max = adj_dma_attr->dma_attr_granular * 0x100;
11908 		}
11909 		/*
11910 		 * Adjust controler dma attributes, if necessary
11911 		 */
11912 		if (dma_attr->dma_attr_count_max > count_max)
11913 			adj_dma_attr->dma_attr_count_max = count_max;
11914 		if (dma_attr->dma_attr_maxxfer > count_max)
11915 			adj_dma_attr->dma_attr_maxxfer = count_max;
11916 	}
11917 }
11918 
11919 
11920 /*
11921  * Allocate DMA resources for the buffer
11922  * This function handles initial DMA resource allocation as well as
11923  * DMA window shift and may be called repeatedly for the same DMA window
11924  * until all DMA cookies in the DMA window are processed.
11925  * To guarantee that there is always a coherent set of cookies to process
11926  * by SATA HBA driver (observing alignment, device granularity, etc.),
11927  * the number of slots for DMA cookies is equal to lesser of  a number of
11928  * cookies in a DMA window and a max number of scatter/gather entries.
11929  *
11930  * Returns DDI_SUCCESS upon successful operation.
11931  * Return failure code of a failing command or DDI_FAILURE when
11932  * internal cleanup failed.
11933  */
11934 static int
11935 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags,
11936     int (*callback)(caddr_t), caddr_t arg,
11937     ddi_dma_attr_t *cur_dma_attr)
11938 {
11939 	int	rval;
11940 	off_t	offset;
11941 	size_t	size;
11942 	int	max_sg_len, req_len, i;
11943 	uint_t	dma_flags;
11944 	struct buf	*bp;
11945 	uint64_t	cur_txfer_len;
11946 
11947 
11948 	ASSERT(spx->txlt_sata_pkt != NULL);
11949 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
11950 	ASSERT(bp != NULL);
11951 
11952 
11953 	if (spx->txlt_buf_dma_handle == NULL) {
11954 		/*
11955 		 * No DMA resources allocated so far - this is a first call
11956 		 * for this sata pkt.
11957 		 */
11958 		rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst),
11959 		    cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle);
11960 
11961 		if (rval != DDI_SUCCESS) {
11962 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
11963 			    "sata_dma_buf_setup: no buf DMA resources %x",
11964 			    rval));
11965 			return (rval);
11966 		}
11967 
11968 		if (bp->b_flags & B_READ)
11969 			dma_flags = DDI_DMA_READ;
11970 		else
11971 			dma_flags = DDI_DMA_WRITE;
11972 
11973 		if (flags & PKT_CONSISTENT)
11974 			dma_flags |= DDI_DMA_CONSISTENT;
11975 
11976 		if (flags & PKT_DMA_PARTIAL)
11977 			dma_flags |= DDI_DMA_PARTIAL;
11978 
11979 		/*
11980 		 * Check buffer alignment and size against dma attributes
11981 		 * Consider dma_attr_align only. There may be requests
11982 		 * with the size lower than device granularity, but they
11983 		 * will not read/write from/to the device, so no adjustment
11984 		 * is necessary. The dma_attr_minxfer theoretically should
11985 		 * be considered, but no HBA driver is checking it.
11986 		 */
11987 		if (IS_P2ALIGNED(bp->b_un.b_addr,
11988 		    cur_dma_attr->dma_attr_align)) {
11989 			rval = ddi_dma_buf_bind_handle(
11990 			    spx->txlt_buf_dma_handle,
11991 			    bp, dma_flags, callback, arg,
11992 			    &spx->txlt_dma_cookie,
11993 			    &spx->txlt_curwin_num_dma_cookies);
11994 		} else { /* Buffer is not aligned */
11995 
11996 			int	(*ddicallback)(caddr_t);
11997 			size_t	bufsz;
11998 
11999 			/* Check id sleeping is allowed */
12000 			ddicallback = (callback == NULL_FUNC) ?
12001 			    DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
12002 
12003 			SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
12004 			    "mis-aligned buffer: addr=0x%p, cnt=%lu",
12005 			    (void *)bp->b_un.b_addr, bp->b_bcount);
12006 
12007 			if (bp->b_flags & (B_PAGEIO|B_PHYS))
12008 				/*
12009 				 * CPU will need to access data in the buffer
12010 				 * (for copying) so map it.
12011 				 */
12012 				bp_mapin(bp);
12013 
12014 			ASSERT(spx->txlt_tmp_buf == NULL);
12015 
12016 			/* Buffer may be padded by ddi_dma_mem_alloc()! */
12017 			rval = ddi_dma_mem_alloc(
12018 			    spx->txlt_buf_dma_handle,
12019 			    bp->b_bcount,
12020 			    &sata_acc_attr,
12021 			    DDI_DMA_STREAMING,
12022 			    ddicallback, NULL,
12023 			    &spx->txlt_tmp_buf,
12024 			    &bufsz,
12025 			    &spx->txlt_tmp_buf_handle);
12026 
12027 			if (rval != DDI_SUCCESS) {
12028 				/* DMA mapping failed */
12029 				(void) ddi_dma_free_handle(
12030 				    &spx->txlt_buf_dma_handle);
12031 				spx->txlt_buf_dma_handle = NULL;
12032 #ifdef SATA_DEBUG
12033 				mbuffail_count++;
12034 #endif
12035 				SATADBG1(SATA_DBG_DMA_SETUP,
12036 				    spx->txlt_sata_hba_inst,
12037 				    "sata_dma_buf_setup: "
12038 				    "buf dma mem alloc failed %x\n", rval);
12039 				return (rval);
12040 			}
12041 			ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf,
12042 			    cur_dma_attr->dma_attr_align));
12043 
12044 #ifdef SATA_DEBUG
12045 			mbuf_count++;
12046 
12047 			if (bp->b_bcount != bufsz)
12048 				/*
12049 				 * This will require special handling, because
12050 				 * DMA cookies will be based on the temporary
12051 				 * buffer size, not the original buffer
12052 				 * b_bcount, so the residue may have to
12053 				 * be counted differently.
12054 				 */
12055 				SATADBG2(SATA_DBG_DMA_SETUP,
12056 				    spx->txlt_sata_hba_inst,
12057 				    "sata_dma_buf_setup: bp size %x != "
12058 				    "bufsz %x\n", bp->b_bcount, bufsz);
12059 #endif
12060 			if (dma_flags & DDI_DMA_WRITE) {
12061 				/*
12062 				 * Write operation - copy data into
12063 				 * an aligned temporary buffer. Buffer will be
12064 				 * synced for device by ddi_dma_addr_bind_handle
12065 				 */
12066 				bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf,
12067 				    bp->b_bcount);
12068 			}
12069 
12070 			rval = ddi_dma_addr_bind_handle(
12071 			    spx->txlt_buf_dma_handle,
12072 			    NULL,
12073 			    spx->txlt_tmp_buf,
12074 			    bufsz, dma_flags, ddicallback, 0,
12075 			    &spx->txlt_dma_cookie,
12076 			    &spx->txlt_curwin_num_dma_cookies);
12077 		}
12078 
12079 		switch (rval) {
12080 		case DDI_DMA_PARTIAL_MAP:
12081 			SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
12082 			    "sata_dma_buf_setup: DMA Partial Map\n", NULL);
12083 			/*
12084 			 * Partial DMA mapping.
12085 			 * Retrieve number of DMA windows for this request.
12086 			 */
12087 			if (ddi_dma_numwin(spx->txlt_buf_dma_handle,
12088 			    &spx->txlt_num_dma_win) != DDI_SUCCESS) {
12089 				if (spx->txlt_tmp_buf != NULL) {
12090 					ddi_dma_mem_free(
12091 					    &spx->txlt_tmp_buf_handle);
12092 					spx->txlt_tmp_buf = NULL;
12093 				}
12094 				(void) ddi_dma_unbind_handle(
12095 				    spx->txlt_buf_dma_handle);
12096 				(void) ddi_dma_free_handle(
12097 				    &spx->txlt_buf_dma_handle);
12098 				spx->txlt_buf_dma_handle = NULL;
12099 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
12100 				    "sata_dma_buf_setup: numwin failed\n"));
12101 				return (DDI_FAILURE);
12102 			}
12103 			SATADBG2(SATA_DBG_DMA_SETUP,
12104 			    spx->txlt_sata_hba_inst,
12105 			    "sata_dma_buf_setup: windows: %d, cookies: %d\n",
12106 			    spx->txlt_num_dma_win,
12107 			    spx->txlt_curwin_num_dma_cookies);
12108 			spx->txlt_cur_dma_win = 0;
12109 			break;
12110 
12111 		case DDI_DMA_MAPPED:
12112 			/* DMA fully mapped */
12113 			spx->txlt_num_dma_win = 1;
12114 			spx->txlt_cur_dma_win = 0;
12115 			SATADBG1(SATA_DBG_DMA_SETUP,
12116 			    spx->txlt_sata_hba_inst,
12117 			    "sata_dma_buf_setup: windows: 1 "
12118 			    "cookies: %d\n", spx->txlt_curwin_num_dma_cookies);
12119 			break;
12120 
12121 		default:
12122 			/* DMA mapping failed */
12123 			if (spx->txlt_tmp_buf != NULL) {
12124 				ddi_dma_mem_free(
12125 				    &spx->txlt_tmp_buf_handle);
12126 				spx->txlt_tmp_buf = NULL;
12127 			}
12128 			(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
12129 			spx->txlt_buf_dma_handle = NULL;
12130 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
12131 			    "sata_dma_buf_setup: buf dma handle binding "
12132 			    "failed %x\n", rval));
12133 			return (rval);
12134 		}
12135 		spx->txlt_curwin_processed_dma_cookies = 0;
12136 		spx->txlt_dma_cookie_list = NULL;
12137 	} else {
12138 		/*
12139 		 * DMA setup is reused. Check if we need to process more
12140 		 * cookies in current window, or to get next window, if any.
12141 		 */
12142 
12143 		ASSERT(spx->txlt_curwin_processed_dma_cookies <=
12144 		    spx->txlt_curwin_num_dma_cookies);
12145 
12146 		if (spx->txlt_curwin_processed_dma_cookies ==
12147 		    spx->txlt_curwin_num_dma_cookies) {
12148 			/*
12149 			 * All cookies from current DMA window were processed.
12150 			 * Get next DMA window.
12151 			 */
12152 			spx->txlt_cur_dma_win++;
12153 			if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) {
12154 				(void) ddi_dma_getwin(spx->txlt_buf_dma_handle,
12155 				    spx->txlt_cur_dma_win, &offset, &size,
12156 				    &spx->txlt_dma_cookie,
12157 				    &spx->txlt_curwin_num_dma_cookies);
12158 				spx->txlt_curwin_processed_dma_cookies = 0;
12159 			} else {
12160 				/* No more windows! End of request! */
12161 				/* What to do? - panic for now */
12162 				ASSERT(spx->txlt_cur_dma_win >=
12163 				    spx->txlt_num_dma_win);
12164 
12165 				spx->txlt_curwin_num_dma_cookies = 0;
12166 				spx->txlt_curwin_processed_dma_cookies = 0;
12167 				spx->txlt_sata_pkt->
12168 				    satapkt_cmd.satacmd_num_dma_cookies = 0;
12169 				return (DDI_SUCCESS);
12170 			}
12171 		}
12172 	}
12173 	/* There better be at least one DMA cookie outstanding */
12174 	ASSERT((spx->txlt_curwin_num_dma_cookies -
12175 	    spx->txlt_curwin_processed_dma_cookies) > 0);
12176 
12177 	if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) {
12178 		/* The default cookie slot was used in previous run */
12179 		ASSERT(spx->txlt_curwin_processed_dma_cookies == 0);
12180 		spx->txlt_dma_cookie_list = NULL;
12181 		spx->txlt_dma_cookie_list_len = 0;
12182 	}
12183 	if (spx->txlt_curwin_processed_dma_cookies == 0) {
12184 		/*
12185 		 * Processing a new DMA window - set-up dma cookies list.
12186 		 * We may reuse previously allocated cookie array if it is
12187 		 * possible.
12188 		 */
12189 		if (spx->txlt_dma_cookie_list != NULL &&
12190 		    spx->txlt_dma_cookie_list_len <
12191 		    spx->txlt_curwin_num_dma_cookies) {
12192 			/*
12193 			 * New DMA window contains more cookies than
12194 			 * the previous one. We need larger cookie list - free
12195 			 * the old one.
12196 			 */
12197 			(void) kmem_free(spx->txlt_dma_cookie_list,
12198 			    spx->txlt_dma_cookie_list_len *
12199 			    sizeof (ddi_dma_cookie_t));
12200 			spx->txlt_dma_cookie_list = NULL;
12201 			spx->txlt_dma_cookie_list_len = 0;
12202 		}
12203 		if (spx->txlt_dma_cookie_list == NULL) {
12204 			/*
12205 			 * Calculate lesser of number of cookies in this
12206 			 * DMA window and number of s/g entries.
12207 			 */
12208 			max_sg_len = cur_dma_attr->dma_attr_sgllen;
12209 			req_len = MIN(max_sg_len,
12210 			    spx->txlt_curwin_num_dma_cookies);
12211 
12212 			/* Allocate new dma cookie array if necessary */
12213 			if (req_len == 1) {
12214 				/* Only one cookie - no need for a list */
12215 				spx->txlt_dma_cookie_list =
12216 				    &spx->txlt_dma_cookie;
12217 				spx->txlt_dma_cookie_list_len = 1;
12218 			} else {
12219 				/*
12220 				 * More than one cookie - try to allocate space.
12221 				 */
12222 				spx->txlt_dma_cookie_list = kmem_zalloc(
12223 				    sizeof (ddi_dma_cookie_t) * req_len,
12224 				    callback == NULL_FUNC ? KM_NOSLEEP :
12225 				    KM_SLEEP);
12226 				if (spx->txlt_dma_cookie_list == NULL) {
12227 					SATADBG1(SATA_DBG_DMA_SETUP,
12228 					    spx->txlt_sata_hba_inst,
12229 					    "sata_dma_buf_setup: cookie list "
12230 					    "allocation failed\n", NULL);
12231 					/*
12232 					 * We could not allocate space for
12233 					 * neccessary number of dma cookies in
12234 					 * this window, so we fail this request.
12235 					 * Next invocation would try again to
12236 					 * allocate space for cookie list.
12237 					 * Note:Packet residue was not modified.
12238 					 */
12239 					return (DDI_DMA_NORESOURCES);
12240 				} else {
12241 					spx->txlt_dma_cookie_list_len = req_len;
12242 				}
12243 			}
12244 		}
12245 		/*
12246 		 * Fetch DMA cookies into cookie list in sata_pkt_txlate.
12247 		 * First cookie was already fetched.
12248 		 */
12249 		*(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie;
12250 		cur_txfer_len =
12251 		    (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size;
12252 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1;
12253 		spx->txlt_curwin_processed_dma_cookies++;
12254 		for (i = 1; (i < spx->txlt_dma_cookie_list_len) &&
12255 		    (i < spx->txlt_curwin_num_dma_cookies); i++) {
12256 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
12257 			    &spx->txlt_dma_cookie_list[i]);
12258 			cur_txfer_len +=
12259 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
12260 			spx->txlt_curwin_processed_dma_cookies++;
12261 			spx->txlt_sata_pkt->
12262 			    satapkt_cmd.satacmd_num_dma_cookies += 1;
12263 		}
12264 	} else {
12265 		SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
12266 		    "sata_dma_buf_setup: sliding within DMA window, "
12267 		    "cur cookie %d, total cookies %d\n",
12268 		    spx->txlt_curwin_processed_dma_cookies,
12269 		    spx->txlt_curwin_num_dma_cookies);
12270 
12271 		/*
12272 		 * Not all cookies from the current dma window were used because
12273 		 * of s/g limitation.
12274 		 * There is no need to re-size the list - it was set at
12275 		 * optimal size, or only default entry is used (s/g = 1).
12276 		 */
12277 		if (spx->txlt_dma_cookie_list == NULL) {
12278 			spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie;
12279 			spx->txlt_dma_cookie_list_len = 1;
12280 		}
12281 		/*
12282 		 * Since we are processing remaining cookies in a DMA window,
12283 		 * there may be less of them than the number of entries in the
12284 		 * current dma cookie list.
12285 		 */
12286 		req_len = MIN(spx->txlt_dma_cookie_list_len,
12287 		    (spx->txlt_curwin_num_dma_cookies -
12288 		    spx->txlt_curwin_processed_dma_cookies));
12289 
12290 		/* Fetch the next batch of cookies */
12291 		for (i = 0, cur_txfer_len = 0; i < req_len; i++) {
12292 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
12293 			    &spx->txlt_dma_cookie_list[i]);
12294 			cur_txfer_len +=
12295 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
12296 			spx->txlt_sata_pkt->
12297 			    satapkt_cmd.satacmd_num_dma_cookies++;
12298 			spx->txlt_curwin_processed_dma_cookies++;
12299 		}
12300 	}
12301 
12302 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0);
12303 
12304 	/* Point sata_cmd to the cookie list */
12305 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list =
12306 	    &spx->txlt_dma_cookie_list[0];
12307 
12308 	/* Remember number of DMA cookies passed in sata packet */
12309 	spx->txlt_num_dma_cookies =
12310 	    spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies;
12311 
12312 	ASSERT(cur_txfer_len != 0);
12313 	if (cur_txfer_len <= bp->b_bcount)
12314 		spx->txlt_total_residue -= cur_txfer_len;
12315 	else {
12316 		/*
12317 		 * Temporary DMA buffer has been padded by
12318 		 * ddi_dma_mem_alloc()!
12319 		 * This requires special handling, because DMA cookies are
12320 		 * based on the temporary buffer size, not the b_bcount,
12321 		 * and we have extra bytes to transfer - but the packet
12322 		 * residue has to stay correct because we will copy only
12323 		 * the requested number of bytes.
12324 		 */
12325 		spx->txlt_total_residue -= bp->b_bcount;
12326 	}
12327 
12328 	return (DDI_SUCCESS);
12329 }
12330 
12331 /*
12332  * Common routine for releasing DMA resources
12333  */
12334 static void
12335 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx)
12336 {
12337 	if (spx->txlt_buf_dma_handle != NULL) {
12338 		if (spx->txlt_tmp_buf != NULL)  {
12339 			/*
12340 			 * Intermediate DMA buffer was allocated.
12341 			 * Free allocated buffer and associated access handle.
12342 			 */
12343 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
12344 			spx->txlt_tmp_buf = NULL;
12345 		}
12346 		/*
12347 		 * Free DMA resources - cookies and handles
12348 		 */
12349 		/* ASSERT(spx->txlt_dma_cookie_list != NULL); */
12350 		if (spx->txlt_dma_cookie_list != NULL) {
12351 			if (spx->txlt_dma_cookie_list !=
12352 			    &spx->txlt_dma_cookie) {
12353 				(void) kmem_free(spx->txlt_dma_cookie_list,
12354 				    spx->txlt_dma_cookie_list_len *
12355 				    sizeof (ddi_dma_cookie_t));
12356 				spx->txlt_dma_cookie_list = NULL;
12357 			}
12358 		}
12359 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
12360 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
12361 		spx->txlt_buf_dma_handle = NULL;
12362 	}
12363 }
12364 
12365 /*
12366  * Free DMA resources
12367  * Used by the HBA driver to release DMA resources that it does not use.
12368  *
12369  * Returns Void
12370  */
12371 void
12372 sata_free_dma_resources(sata_pkt_t *sata_pkt)
12373 {
12374 	sata_pkt_txlate_t *spx;
12375 
12376 	if (sata_pkt == NULL)
12377 		return;
12378 
12379 	spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
12380 
12381 	sata_common_free_dma_rsrcs(spx);
12382 }
12383 
12384 /*
12385  * Fetch Device Identify data.
12386  * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type)
12387  * command to a device and get the device identify data.
12388  * The device_info structure has to be set to device type (for selecting proper
12389  * device identify command).
12390  *
12391  * Returns:
12392  * SATA_SUCCESS if cmd succeeded
12393  * SATA_RETRY if cmd was rejected and could be retried,
12394  * SATA_FAILURE if cmd failed and should not be retried (port error)
12395  *
12396  * Cannot be called in an interrupt context.
12397  */
12398 
12399 static int
12400 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst,
12401     sata_drive_info_t *sdinfo)
12402 {
12403 	struct buf *bp;
12404 	sata_pkt_t *spkt;
12405 	sata_cmd_t *scmd;
12406 	sata_pkt_txlate_t *spx;
12407 	int rval;
12408 
12409 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12410 	spx->txlt_sata_hba_inst = sata_hba_inst;
12411 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12412 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12413 	if (spkt == NULL) {
12414 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12415 		return (SATA_RETRY); /* may retry later */
12416 	}
12417 	/* address is needed now */
12418 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12419 
12420 	/*
12421 	 * Allocate buffer for Identify Data return data
12422 	 */
12423 	bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t));
12424 	if (bp == NULL) {
12425 		sata_pkt_free(spx);
12426 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12427 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12428 		    "sata_fetch_device_identify_data: "
12429 		    "cannot allocate buffer for ID"));
12430 		return (SATA_RETRY); /* may retry later */
12431 	}
12432 
12433 	/* Fill sata_pkt */
12434 	sdinfo->satadrv_state = SATA_STATE_PROBING;
12435 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12436 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12437 	/* Synchronous mode, no callback */
12438 	spkt->satapkt_comp = NULL;
12439 	/* Timeout 30s */
12440 	spkt->satapkt_time = sata_default_pkt_time;
12441 
12442 	scmd = &spkt->satapkt_cmd;
12443 	scmd->satacmd_bp = bp;
12444 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
12445 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
12446 
12447 	/* Build Identify Device cmd in the sata_pkt */
12448 	scmd->satacmd_addr_type = 0;		/* N/A */
12449 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
12450 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
12451 	scmd->satacmd_lba_mid_lsb = 0;		/* N/A */
12452 	scmd->satacmd_lba_high_lsb = 0;		/* N/A */
12453 	scmd->satacmd_features_reg = 0;		/* N/A */
12454 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12455 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
12456 		/* Identify Packet Device cmd */
12457 		scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE;
12458 	} else {
12459 		/* Identify Device cmd - mandatory for all other devices */
12460 		scmd->satacmd_cmd_reg = SATAC_ID_DEVICE;
12461 	}
12462 
12463 	/* Send pkt to SATA HBA driver */
12464 	rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt);
12465 
12466 #ifdef SATA_INJECT_FAULTS
12467 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
12468 #endif
12469 
12470 	if (rval == SATA_TRAN_ACCEPTED &&
12471 	    spkt->satapkt_reason == SATA_PKT_COMPLETED) {
12472 		if (spx->txlt_buf_dma_handle != NULL) {
12473 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
12474 			    DDI_DMA_SYNC_FORKERNEL);
12475 			ASSERT(rval == DDI_SUCCESS);
12476 		}
12477 		if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config &
12478 		    SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) {
12479 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12480 			    "SATA disk device at port %d - "
12481 			    "partial Identify Data",
12482 			    sdinfo->satadrv_addr.cport));
12483 			rval = SATA_RETRY; /* may retry later */
12484 			goto fail;
12485 		}
12486 		/* Update sata_drive_info */
12487 		bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id,
12488 		    sizeof (sata_id_t));
12489 
12490 		sdinfo->satadrv_features_support = 0;
12491 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12492 			/*
12493 			 * Retrieve capacity (disks only) and addressing mode
12494 			 */
12495 			sdinfo->satadrv_capacity = sata_check_capacity(sdinfo);
12496 		} else {
12497 			/*
12498 			 * For ATAPI devices one would have to issue
12499 			 * Get Capacity cmd for media capacity. Not here.
12500 			 */
12501 			sdinfo->satadrv_capacity = 0;
12502 			/*
12503 			 * Check what cdb length is supported
12504 			 */
12505 			if ((sdinfo->satadrv_id.ai_config &
12506 			    SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B)
12507 				sdinfo->satadrv_atapi_cdb_len = 16;
12508 			else
12509 				sdinfo->satadrv_atapi_cdb_len = 12;
12510 		}
12511 		/* Setup supported features flags */
12512 		if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT)
12513 			sdinfo->satadrv_features_support |= SATA_DEV_F_DMA;
12514 
12515 		/* Check for SATA GEN and NCQ support */
12516 		if (sdinfo->satadrv_id.ai_satacap != 0 &&
12517 		    sdinfo->satadrv_id.ai_satacap != 0xffff) {
12518 			/* SATA compliance */
12519 			if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ)
12520 				sdinfo->satadrv_features_support |=
12521 				    SATA_DEV_F_NCQ;
12522 			if (sdinfo->satadrv_id.ai_satacap &
12523 			    (SATA_1_SPEED | SATA_2_SPEED)) {
12524 				if (sdinfo->satadrv_id.ai_satacap &
12525 				    SATA_2_SPEED)
12526 					sdinfo->satadrv_features_support |=
12527 					    SATA_DEV_F_SATA2;
12528 				if (sdinfo->satadrv_id.ai_satacap &
12529 				    SATA_1_SPEED)
12530 					sdinfo->satadrv_features_support |=
12531 					    SATA_DEV_F_SATA1;
12532 			} else {
12533 				sdinfo->satadrv_features_support |=
12534 				    SATA_DEV_F_SATA1;
12535 			}
12536 		}
12537 		if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) &&
12538 		    (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD))
12539 			sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ;
12540 
12541 		sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth;
12542 		if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) ||
12543 		    (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) {
12544 			++sdinfo->satadrv_queue_depth;
12545 			/* Adjust according to controller capabilities */
12546 			sdinfo->satadrv_max_queue_depth = MIN(
12547 			    sdinfo->satadrv_queue_depth,
12548 			    SATA_QDEPTH(sata_hba_inst));
12549 			/* Adjust according to global queue depth limit */
12550 			sdinfo->satadrv_max_queue_depth = MIN(
12551 			    sdinfo->satadrv_max_queue_depth,
12552 			    sata_current_max_qdepth);
12553 			if (sdinfo->satadrv_max_queue_depth == 0)
12554 				sdinfo->satadrv_max_queue_depth = 1;
12555 		} else
12556 			sdinfo->satadrv_max_queue_depth = 1;
12557 
12558 		rval = SATA_SUCCESS;
12559 	} else {
12560 		/*
12561 		 * Woops, no Identify Data.
12562 		 */
12563 		if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) {
12564 			rval = SATA_RETRY; /* may retry later */
12565 		} else if (rval == SATA_TRAN_ACCEPTED) {
12566 			if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR ||
12567 			    spkt->satapkt_reason == SATA_PKT_ABORTED ||
12568 			    spkt->satapkt_reason == SATA_PKT_TIMEOUT ||
12569 			    spkt->satapkt_reason == SATA_PKT_RESET)
12570 				rval = SATA_RETRY; /* may retry later */
12571 			else
12572 				rval = SATA_FAILURE;
12573 		} else {
12574 			rval = SATA_FAILURE;
12575 		}
12576 	}
12577 fail:
12578 	/* Free allocated resources */
12579 	sata_free_local_buffer(spx);
12580 	sata_pkt_free(spx);
12581 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12582 
12583 	return (rval);
12584 }
12585 
12586 
12587 /*
12588  * Some devices may not come-up with default DMA mode (UDMA or MWDMA).
12589  * UDMA mode is checked first, followed by MWDMA mode.
12590  * set correctly, so this function is setting it to the highest supported level.
12591  * Older SATA spec required that the device supports at least DMA 4 mode and
12592  * UDMA mode is selected.  It is not mentioned in SerialATA 2.6, so this
12593  * restriction has been removed.
12594  *
12595  * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported.
12596  * Returns SATA_FAILURE if proper DMA mode could not be selected.
12597  *
12598  * NOTE: This function should be called only if DMA mode is supported.
12599  */
12600 static int
12601 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo)
12602 {
12603 	sata_pkt_t *spkt;
12604 	sata_cmd_t *scmd;
12605 	sata_pkt_txlate_t *spx;
12606 	int mode;
12607 	uint8_t subcmd;
12608 	int rval = SATA_SUCCESS;
12609 
12610 	ASSERT(sdinfo != NULL);
12611 	ASSERT(sata_hba_inst != NULL);
12612 
12613 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
12614 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) {
12615 		/* Find highest Ultra DMA mode supported */
12616 		for (mode = 6; mode >= 0; --mode) {
12617 			if (sdinfo->satadrv_id.ai_ultradma & (1 << mode))
12618 				break;
12619 		}
12620 #if 0
12621 		/* Left for historical reasons */
12622 		/*
12623 		 * Some initial version of SATA spec indicated that at least
12624 		 * UDMA mode 4 has to be supported. It is not mentioned in
12625 		 * SerialATA 2.6, so this restriction is removed.
12626 		 */
12627 		if (mode < 4)
12628 			return (SATA_FAILURE);
12629 #endif
12630 
12631 		/*
12632 		 * We're still going to set DMA mode whatever is selected
12633 		 * by default
12634 		 *
12635 		 * We saw an old maxtor sata drive will select Ultra DMA and
12636 		 * Multi-Word DMA simultaneouly by default, which is going
12637 		 * to cause DMA command timed out, so we need to select DMA
12638 		 * mode even when it's already done by default
12639 		 */
12640 
12641 		subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA;
12642 
12643 	} else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) {
12644 		/* Find highest MultiWord DMA mode supported */
12645 		for (mode = 2; mode >= 0; --mode) {
12646 			if (sdinfo->satadrv_id.ai_dworddma & (1 << mode))
12647 				break;
12648 		}
12649 
12650 		/*
12651 		 * We're still going to set DMA mode whatever is selected
12652 		 * by default
12653 		 *
12654 		 * We saw an old maxtor sata drive will select Ultra DMA and
12655 		 * Multi-Word DMA simultaneouly by default, which is going
12656 		 * to cause DMA command timed out, so we need to select DMA
12657 		 * mode even when it's already done by default
12658 		 */
12659 
12660 		subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA;
12661 	} else
12662 		return (SATA_SUCCESS);
12663 
12664 	/*
12665 	 * Set DMA mode via SET FEATURES COMMAND.
12666 	 * Prepare packet for SET FEATURES COMMAND.
12667 	 */
12668 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12669 	spx->txlt_sata_hba_inst = sata_hba_inst;
12670 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
12671 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12672 	if (spkt == NULL) {
12673 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12674 		    "sata_set_dma_mode: could not set DMA mode %", mode));
12675 		rval = SATA_FAILURE;
12676 		goto done;
12677 	}
12678 	/* Fill sata_pkt */
12679 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12680 	/* Timeout 30s */
12681 	spkt->satapkt_time = sata_default_pkt_time;
12682 	/* Synchronous mode, no callback, interrupts */
12683 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12684 	spkt->satapkt_comp = NULL;
12685 	scmd = &spkt->satapkt_cmd;
12686 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
12687 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
12688 	scmd->satacmd_addr_type = 0;
12689 	scmd->satacmd_device_reg = 0;
12690 	scmd->satacmd_status_reg = 0;
12691 	scmd->satacmd_error_reg = 0;
12692 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
12693 	scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE;
12694 	scmd->satacmd_sec_count_lsb = subcmd | mode;
12695 
12696 	/* Transfer command to HBA */
12697 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
12698 	    spkt) != SATA_TRAN_ACCEPTED ||
12699 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12700 		/* Pkt execution failed */
12701 		rval = SATA_FAILURE;
12702 	}
12703 done:
12704 
12705 	/* Free allocated resources */
12706 	if (spkt != NULL)
12707 		sata_pkt_free(spx);
12708 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
12709 
12710 	return (rval);
12711 }
12712 
12713 
12714 /*
12715  * Set device caching mode.
12716  * One of the following operations should be specified:
12717  * SATAC_SF_ENABLE_READ_AHEAD
12718  * SATAC_SF_DISABLE_READ_AHEAD
12719  * SATAC_SF_ENABLE_WRITE_CACHE
12720  * SATAC_SF_DISABLE_WRITE_CACHE
12721  *
12722  * If operation fails, system log messgage is emitted.
12723  * Returns SATA_SUCCESS when the operation succeeds, SATA_RETRY if
12724  * command was sent but did not succeed, and SATA_FAILURE otherwise.
12725  */
12726 
12727 static int
12728 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
12729     int cache_op)
12730 {
12731 	sata_pkt_t *spkt;
12732 	sata_cmd_t *scmd;
12733 	sata_pkt_txlate_t *spx;
12734 	int rval = SATA_SUCCESS;
12735 	int hba_rval;
12736 	char *infop;
12737 
12738 	ASSERT(sdinfo != NULL);
12739 	ASSERT(sata_hba_inst != NULL);
12740 	ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD ||
12741 	    cache_op == SATAC_SF_DISABLE_READ_AHEAD ||
12742 	    cache_op == SATAC_SF_ENABLE_WRITE_CACHE ||
12743 	    cache_op == SATAC_SF_DISABLE_WRITE_CACHE);
12744 
12745 
12746 	/* Prepare packet for SET FEATURES COMMAND */
12747 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12748 	spx->txlt_sata_hba_inst = sata_hba_inst;
12749 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
12750 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12751 	if (spkt == NULL) {
12752 		rval = SATA_FAILURE;
12753 		goto failure;
12754 	}
12755 	/* Fill sata_pkt */
12756 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12757 	/* Timeout 30s */
12758 	spkt->satapkt_time = sata_default_pkt_time;
12759 	/* Synchronous mode, no callback, interrupts */
12760 	spkt->satapkt_op_mode =
12761 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12762 	spkt->satapkt_comp = NULL;
12763 	scmd = &spkt->satapkt_cmd;
12764 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
12765 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
12766 	scmd->satacmd_addr_type = 0;
12767 	scmd->satacmd_device_reg = 0;
12768 	scmd->satacmd_status_reg = 0;
12769 	scmd->satacmd_error_reg = 0;
12770 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
12771 	scmd->satacmd_features_reg = cache_op;
12772 
12773 	/* Transfer command to HBA */
12774 	hba_rval = (*SATA_START_FUNC(sata_hba_inst))(
12775 	    SATA_DIP(sata_hba_inst), spkt);
12776 
12777 #ifdef SATA_INJECT_FAULTS
12778 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
12779 #endif
12780 
12781 	if ((hba_rval != SATA_TRAN_ACCEPTED) ||
12782 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
12783 		/* Pkt execution failed */
12784 		switch (cache_op) {
12785 		case SATAC_SF_ENABLE_READ_AHEAD:
12786 			infop = "enabling read ahead failed";
12787 			break;
12788 		case SATAC_SF_DISABLE_READ_AHEAD:
12789 			infop = "disabling read ahead failed";
12790 			break;
12791 		case SATAC_SF_ENABLE_WRITE_CACHE:
12792 			infop = "enabling write cache failed";
12793 			break;
12794 		case SATAC_SF_DISABLE_WRITE_CACHE:
12795 			infop = "disabling write cache failed";
12796 			break;
12797 		}
12798 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
12799 		rval = SATA_RETRY;
12800 	}
12801 failure:
12802 	/* Free allocated resources */
12803 	if (spkt != NULL)
12804 		sata_pkt_free(spx);
12805 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
12806 	return (rval);
12807 }
12808 
12809 /*
12810  * Set Removable Media Status Notification (enable/disable)
12811  * state == 0 , disable
12812  * state != 0 , enable
12813  *
12814  * If operation fails, system log messgage is emitted.
12815  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
12816  */
12817 
12818 static int
12819 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
12820     int state)
12821 {
12822 	sata_pkt_t *spkt;
12823 	sata_cmd_t *scmd;
12824 	sata_pkt_txlate_t *spx;
12825 	int rval = SATA_SUCCESS;
12826 	char *infop;
12827 
12828 	ASSERT(sdinfo != NULL);
12829 	ASSERT(sata_hba_inst != NULL);
12830 
12831 	/* Prepare packet for SET FEATURES COMMAND */
12832 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12833 	spx->txlt_sata_hba_inst = sata_hba_inst;
12834 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
12835 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12836 	if (spkt == NULL) {
12837 		rval = SATA_FAILURE;
12838 		goto failure;
12839 	}
12840 	/* Fill sata_pkt */
12841 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12842 	/* Timeout 30s */
12843 	spkt->satapkt_time = sata_default_pkt_time;
12844 	/* Synchronous mode, no callback, interrupts */
12845 	spkt->satapkt_op_mode =
12846 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12847 	spkt->satapkt_comp = NULL;
12848 	scmd = &spkt->satapkt_cmd;
12849 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
12850 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
12851 	scmd->satacmd_addr_type = 0;
12852 	scmd->satacmd_device_reg = 0;
12853 	scmd->satacmd_status_reg = 0;
12854 	scmd->satacmd_error_reg = 0;
12855 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
12856 	if (state == 0)
12857 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN;
12858 	else
12859 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN;
12860 
12861 	/* Transfer command to HBA */
12862 	if (((*SATA_START_FUNC(sata_hba_inst))(
12863 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
12864 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
12865 		/* Pkt execution failed */
12866 		if (state == 0)
12867 			infop = "disabling Removable Media Status "
12868 			    "Notification failed";
12869 		else
12870 			infop = "enabling Removable Media Status "
12871 			    "Notification failed";
12872 
12873 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
12874 		rval = SATA_FAILURE;
12875 	}
12876 failure:
12877 	/* Free allocated resources */
12878 	if (spkt != NULL)
12879 		sata_pkt_free(spx);
12880 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
12881 	return (rval);
12882 }
12883 
12884 
12885 /*
12886  * Update state and copy port ss* values from passed sata_device structure.
12887  * sata_address is validated - if not valid, nothing is changed in sata_scsi
12888  * configuration struct.
12889  *
12890  * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function
12891  * regardless of the state in device argument.
12892  *
12893  * Port mutex should be held while calling this function.
12894  */
12895 static void
12896 sata_update_port_info(sata_hba_inst_t *sata_hba_inst,
12897     sata_device_t *sata_device)
12898 {
12899 	sata_cport_info_t *cportinfo;
12900 
12901 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT ||
12902 	    sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
12903 		if (SATA_NUM_CPORTS(sata_hba_inst) <=
12904 		    sata_device->satadev_addr.cport)
12905 			return;
12906 
12907 		cportinfo = SATA_CPORT_INFO(sata_hba_inst,
12908 		    sata_device->satadev_addr.cport);
12909 
12910 		ASSERT(mutex_owned(&cportinfo->cport_mutex));
12911 		cportinfo->cport_scr = sata_device->satadev_scr;
12912 
12913 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
12914 		cportinfo->cport_state &= ~(SATA_PSTATE_PWRON |
12915 		    SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
12916 		cportinfo->cport_state |=
12917 		    sata_device->satadev_state & SATA_PSTATE_VALID;
12918 	}
12919 }
12920 
12921 void
12922 sata_update_pmport_info(sata_hba_inst_t *sata_hba_inst,
12923     sata_device_t *sata_device)
12924 {
12925 	sata_pmport_info_t *pmportinfo;
12926 
12927 	if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT &&
12928 	    sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) ||
12929 	    SATA_NUM_PMPORTS(sata_hba_inst,
12930 	    sata_device->satadev_addr.cport) <
12931 	    sata_device->satadev_addr.pmport) {
12932 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
12933 		    "sata_update_port_info: error address %p.",
12934 		    &sata_device->satadev_addr);
12935 		return;
12936 	}
12937 
12938 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
12939 	    sata_device->satadev_addr.cport,
12940 	    sata_device->satadev_addr.pmport);
12941 
12942 	ASSERT(mutex_owned(&pmportinfo->pmport_mutex));
12943 	pmportinfo->pmport_scr = sata_device->satadev_scr;
12944 
12945 	/* Preserve SATA_PSTATE_SHUTDOWN flag */
12946 	pmportinfo->pmport_state &=
12947 	    ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
12948 	pmportinfo->pmport_state |=
12949 	    sata_device->satadev_state & SATA_PSTATE_VALID;
12950 }
12951 
12952 /*
12953  * Extract SATA port specification from an IOCTL argument.
12954  *
12955  * This function return the port the user land send us as is, unless it
12956  * cannot retrieve port spec, then -1 is returned.
12957  *
12958  * Support port multiplier.
12959  */
12960 static int32_t
12961 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp)
12962 {
12963 	int32_t port;
12964 
12965 	/* Extract port number from nvpair in dca structure  */
12966 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
12967 		SATA_LOG_D((sata_hba_inst, CE_NOTE,
12968 		    "sata_get_port_num: invalid port spec 0x%x in ioctl",
12969 		    port));
12970 		port = -1;
12971 	}
12972 
12973 	return (port);
12974 }
12975 
12976 /*
12977  * Get dev_info_t pointer to the device node pointed to by port argument.
12978  * NOTE: target argument is a value used in ioctls to identify
12979  * the AP - it is not a sata_address.
12980  * It is a combination of cport, pmport and address qualifier, encodded same
12981  * way as a scsi target number.
12982  * At this moment it carries only cport number.
12983  *
12984  * PMult hotplug is supported now.
12985  *
12986  * Returns dev_info_t pointer if target device was found, NULL otherwise.
12987  */
12988 
12989 static dev_info_t *
12990 sata_get_target_dip(dev_info_t *dip, uint8_t cport, uint8_t pmport)
12991 {
12992 	dev_info_t	*cdip = NULL;
12993 	int		target, tgt;
12994 	int 		circ;
12995 	uint8_t		qual;
12996 
12997 	sata_hba_inst_t	*sata_hba_inst;
12998 	scsi_hba_tran_t *scsi_hba_tran;
12999 
13000 	/* Get target id */
13001 	scsi_hba_tran = ddi_get_driver_private(dip);
13002 	if (scsi_hba_tran == NULL)
13003 		return (NULL);
13004 
13005 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
13006 
13007 	if (sata_hba_inst == NULL)
13008 		return (NULL);
13009 
13010 	/* Identify a port-mult by cport_info.cport_dev_type */
13011 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT)
13012 		qual = SATA_ADDR_DPMPORT;
13013 	else
13014 		qual = SATA_ADDR_DCPORT;
13015 
13016 	target = SATA_TO_SCSI_TARGET(cport, pmport, qual);
13017 
13018 	/* Retrieve target dip */
13019 	ndi_devi_enter(dip, &circ);
13020 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
13021 		dev_info_t *next = ddi_get_next_sibling(cdip);
13022 
13023 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
13024 		    DDI_PROP_DONTPASS, "target", -1);
13025 		if (tgt == -1) {
13026 			/*
13027 			 * This is actually an error condition, but not
13028 			 * a fatal one. Just continue the search.
13029 			 */
13030 			cdip = next;
13031 			continue;
13032 		}
13033 
13034 		if (tgt == target)
13035 			break;
13036 
13037 		cdip = next;
13038 	}
13039 	ndi_devi_exit(dip, circ);
13040 
13041 	return (cdip);
13042 }
13043 
13044 /*
13045  * Get dev_info_t pointer to the device node pointed to by port argument.
13046  * NOTE: target argument is a value used in ioctls to identify
13047  * the AP - it is not a sata_address.
13048  * It is a combination of cport, pmport and address qualifier, encoded same
13049  * way as a scsi target number.
13050  *
13051  * Returns dev_info_t pointer if target device was found, NULL otherwise.
13052  */
13053 
13054 static dev_info_t *
13055 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr)
13056 {
13057 	dev_info_t	*cdip = NULL;
13058 	int		target, tgt;
13059 	int 		circ;
13060 
13061 	target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual);
13062 
13063 	ndi_devi_enter(dip, &circ);
13064 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
13065 		dev_info_t *next = ddi_get_next_sibling(cdip);
13066 
13067 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
13068 		    DDI_PROP_DONTPASS, "target", -1);
13069 		if (tgt == -1) {
13070 			/*
13071 			 * This is actually an error condition, but not
13072 			 * a fatal one. Just continue the search.
13073 			 */
13074 			cdip = next;
13075 			continue;
13076 		}
13077 
13078 		if (tgt == target)
13079 			break;
13080 
13081 		cdip = next;
13082 	}
13083 	ndi_devi_exit(dip, circ);
13084 
13085 	return (cdip);
13086 }
13087 
13088 /*
13089  * Process sata port disconnect request.
13090  * Normally, cfgadm sata plugin will try to offline (unconfigure) the device
13091  * before this request. Nevertheless, if a device is still configured,
13092  * we need to attempt to offline and unconfigure device.
13093  * Regardless of the unconfigure operation results the port is marked as
13094  * deactivated and no access to the attached device is possible.
13095  * If the target node remains because unconfigure operation failed, its state
13096  * will be set to DEVICE_REMOVED, preventing it to be used again when a device
13097  * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure
13098  * the device and remove old target node.
13099  *
13100  * This function invokes sata_hba_inst->satahba_tran->
13101  * sata_tran_hotplug_ops->sata_tran_port_deactivate().
13102  * If successful, the device structure (if any) attached to the specified port
13103  * is removed and state of the port marked appropriately.
13104  * Failure of the port_deactivate may keep port in the physically active state,
13105  * or may fail the port.
13106  *
13107  * NOTE: Port multiplier is supported.
13108  */
13109 
13110 static int
13111 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst,
13112     sata_device_t *sata_device)
13113 {
13114 	sata_drive_info_t *sdinfo = NULL, *subsdinfo = NULL;
13115 	sata_cport_info_t *cportinfo = NULL;
13116 	sata_pmport_info_t *pmportinfo = NULL;
13117 	sata_pmult_info_t *pmultinfo = NULL;
13118 	sata_device_t subsdevice;
13119 	int cport, pmport, qual;
13120 	int rval = SATA_SUCCESS;
13121 	int npmport = 0;
13122 	int rv = 0;
13123 
13124 	cport = sata_device->satadev_addr.cport;
13125 	pmport = sata_device->satadev_addr.pmport;
13126 	qual = sata_device->satadev_addr.qual;
13127 
13128 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
13129 	if (qual == SATA_ADDR_DCPORT)
13130 		qual = SATA_ADDR_CPORT;
13131 	else
13132 		qual = SATA_ADDR_PMPORT;
13133 
13134 	/*
13135 	 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran->
13136 	 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
13137 	 * Do the sanity check.
13138 	 */
13139 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
13140 		/* No physical port deactivation supported. */
13141 		return (EINVAL);
13142 	}
13143 
13144 	/* Check the current state of the port */
13145 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
13146 	    (SATA_DIP(sata_hba_inst), sata_device);
13147 
13148 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
13149 
13150 	/*
13151 	 * Processing port mulitiplier
13152 	 */
13153 	if (qual == SATA_ADDR_CPORT &&
13154 	    SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) {
13155 		mutex_enter(&cportinfo->cport_mutex);
13156 
13157 		/* Check controller port status */
13158 		sata_update_port_info(sata_hba_inst, sata_device);
13159 		if (rval != SATA_SUCCESS ||
13160 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
13161 			/*
13162 			 * Device port status is unknown or it is in failed
13163 			 * state
13164 			 */
13165 			SATA_CPORT_STATE(sata_hba_inst, cport) =
13166 			    SATA_PSTATE_FAILED;
13167 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
13168 			    "sata_hba_ioctl: connect: failed to deactivate "
13169 			    "SATA port %d", cport);
13170 			mutex_exit(&cportinfo->cport_mutex);
13171 			return (EIO);
13172 		}
13173 
13174 		/* Disconnect all sub-devices. */
13175 		pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
13176 		if (pmultinfo != NULL) {
13177 
13178 			for (npmport = 0; npmport < SATA_NUM_PMPORTS(
13179 			    sata_hba_inst, cport); npmport ++) {
13180 				subsdinfo = SATA_PMPORT_DRV_INFO(
13181 				    sata_hba_inst, cport, npmport);
13182 				if (subsdinfo == NULL)
13183 					continue;
13184 
13185 				subsdevice.satadev_addr = subsdinfo->
13186 				    satadrv_addr;
13187 
13188 				mutex_exit(&cportinfo->cport_mutex);
13189 				if (sata_ioctl_disconnect(sata_hba_inst,
13190 				    &subsdevice) == SATA_SUCCESS) {
13191 					SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
13192 					"[Remove] device at port %d:%d "
13193 					"successfully.", cport, npmport);
13194 				}
13195 				mutex_enter(&cportinfo->cport_mutex);
13196 			}
13197 		}
13198 
13199 		/* Disconnect the port multiplier */
13200 		cportinfo->cport_state &= ~SATA_STATE_READY;
13201 		mutex_exit(&cportinfo->cport_mutex);
13202 
13203 		sata_device->satadev_addr.qual = qual;
13204 		rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
13205 		    (SATA_DIP(sata_hba_inst), sata_device);
13206 
13207 		sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
13208 		    SE_NO_HINT);
13209 
13210 		mutex_enter(&cportinfo->cport_mutex);
13211 		sata_update_port_info(sata_hba_inst, sata_device);
13212 		if (rval != SATA_SUCCESS &&
13213 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
13214 			cportinfo->cport_state = SATA_PSTATE_FAILED;
13215 			rv = EIO;
13216 		} else {
13217 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
13218 		}
13219 		mutex_exit(&cportinfo->cport_mutex);
13220 
13221 		return (rv);
13222 	}
13223 
13224 	/*
13225 	 * Process non-port-multiplier device - it could be a drive connected
13226 	 * to a port multiplier port or a controller port.
13227 	 */
13228 	if (qual == SATA_ADDR_PMPORT) {
13229 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
13230 		mutex_enter(&pmportinfo->pmport_mutex);
13231 		sata_update_pmport_info(sata_hba_inst, sata_device);
13232 		if (rval != SATA_SUCCESS ||
13233 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
13234 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
13235 			    SATA_PSTATE_FAILED;
13236 			SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
13237 			    "sata_hba_ioctl: connect: failed to deactivate "
13238 			    "SATA port %d:%d", cport, pmport);
13239 			mutex_exit(&pmportinfo->pmport_mutex);
13240 			return (EIO);
13241 		}
13242 
13243 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
13244 			sdinfo = pmportinfo->pmport_sata_drive;
13245 			ASSERT(sdinfo != NULL);
13246 		}
13247 
13248 		/*
13249 		 * Set port's dev_state to not ready - this will disable
13250 		 * an access to a potentially attached device.
13251 		 */
13252 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
13253 
13254 		/* Remove and release sata_drive info structure. */
13255 		if (sdinfo != NULL) {
13256 			if ((sdinfo->satadrv_type &
13257 			    SATA_VALID_DEV_TYPE) != 0) {
13258 				/*
13259 				 * If a target node exists, try to offline
13260 				 * a device and remove target node.
13261 				 */
13262 				mutex_exit(&pmportinfo->pmport_mutex);
13263 				(void) sata_offline_device(sata_hba_inst,
13264 				    sata_device, sdinfo);
13265 				mutex_enter(&pmportinfo->pmport_mutex);
13266 			}
13267 
13268 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
13269 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
13270 			(void) kmem_free((void *)sdinfo,
13271 			    sizeof (sata_drive_info_t));
13272 		}
13273 		mutex_exit(&pmportinfo->pmport_mutex);
13274 
13275 	} else if (qual == SATA_ADDR_CPORT) {
13276 		mutex_enter(&cportinfo->cport_mutex);
13277 		sata_update_port_info(sata_hba_inst, sata_device);
13278 		if (rval != SATA_SUCCESS ||
13279 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
13280 			/*
13281 			 * Device port status is unknown or it is in failed
13282 			 * state
13283 			 */
13284 			SATA_CPORT_STATE(sata_hba_inst, cport) =
13285 			    SATA_PSTATE_FAILED;
13286 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
13287 			    "sata_hba_ioctl: connect: failed to deactivate "
13288 			    "SATA port %d", cport);
13289 			mutex_exit(&cportinfo->cport_mutex);
13290 			return (EIO);
13291 		}
13292 
13293 		if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
13294 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
13295 			ASSERT(pmultinfo != NULL);
13296 		} else if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
13297 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
13298 			ASSERT(sdinfo != NULL);
13299 		}
13300 		cportinfo->cport_state &= ~SATA_STATE_READY;
13301 
13302 		if (sdinfo != NULL) {
13303 			if ((sdinfo->satadrv_type &
13304 			    SATA_VALID_DEV_TYPE) != 0) {
13305 				/*
13306 				 * If a target node exists, try to offline
13307 				 * a device and remove target node.
13308 				 */
13309 				mutex_exit(&cportinfo->cport_mutex);
13310 				(void) sata_offline_device(sata_hba_inst,
13311 				    sata_device, sdinfo);
13312 				mutex_enter(&cportinfo->cport_mutex);
13313 			}
13314 
13315 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
13316 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
13317 			(void) kmem_free((void *)sdinfo,
13318 			    sizeof (sata_drive_info_t));
13319 		}
13320 		mutex_exit(&cportinfo->cport_mutex);
13321 	}
13322 
13323 	/* Just ask HBA driver to deactivate port */
13324 	sata_device->satadev_addr.qual = qual;
13325 
13326 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
13327 	    (SATA_DIP(sata_hba_inst), sata_device);
13328 
13329 	/*
13330 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
13331 	 * without the hint (to force listener to investivate the state).
13332 	 */
13333 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
13334 	    SE_NO_HINT);
13335 
13336 	if (qual == SATA_ADDR_PMPORT) {
13337 		mutex_enter(&pmportinfo->pmport_mutex);
13338 		sata_update_pmport_info(sata_hba_inst, sata_device);
13339 
13340 		if (rval != SATA_SUCCESS &&
13341 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
13342 			/*
13343 			 * Port deactivation failure - do not change port
13344 			 * state unless the state returned by HBA indicates a
13345 			 * port failure.
13346 			 *
13347 			 * NOTE: device structures were released, so devices
13348 			 * now are invisible! Port reset is needed to
13349 			 * re-enumerate devices.
13350 			 */
13351 			pmportinfo->pmport_state = SATA_PSTATE_FAILED;
13352 			rv = EIO;
13353 		} else {
13354 			/*
13355 			 * Deactivation succeded. From now on the sata framework
13356 			 * will not care what is happening to the device, until
13357 			 * the port is activated again.
13358 			 */
13359 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
13360 		}
13361 		mutex_exit(&pmportinfo->pmport_mutex);
13362 	} else if (qual == SATA_ADDR_CPORT) {
13363 		mutex_enter(&cportinfo->cport_mutex);
13364 		sata_update_port_info(sata_hba_inst, sata_device);
13365 
13366 		if (rval != SATA_SUCCESS &&
13367 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
13368 			cportinfo->cport_state = SATA_PSTATE_FAILED;
13369 			rv = EIO;
13370 		} else {
13371 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
13372 		}
13373 		mutex_exit(&cportinfo->cport_mutex);
13374 	}
13375 
13376 	return (rv);
13377 }
13378 
13379 
13380 
13381 /*
13382  * Process sata port connect request
13383  * The sata cfgadm pluging will invoke this operation only if port was found
13384  * in the disconnect state (failed state is also treated as the disconnected
13385  * state).
13386  * DEVCTL_AP_CONNECT would invoke  sata_hba_inst->satahba_tran->
13387  * sata_tran_hotplug_ops->sata_tran_port_activate().
13388  * If successful and a device is found attached to the port,
13389  * the initialization sequence is executed to attach a device structure to
13390  * a port structure. The state of the port and a device would be set
13391  * appropriately.
13392  * The device is not set in configured state (system-wise) by this operation.
13393  *
13394  * Note, that activating the port may generate link events,
13395  * so it is important that following processing and the
13396  * event processing does not interfere with each other!
13397  *
13398  * This operation may remove port failed state and will
13399  * try to make port active and in good standing.
13400  *
13401  * NOTE: Port multiplier is supported.
13402  */
13403 
13404 static int
13405 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst,
13406     sata_device_t *sata_device)
13407 {
13408 	sata_pmport_info_t	*pmportinfo = NULL;
13409 	uint8_t cport, pmport, qual;
13410 	int rv = 0;
13411 
13412 	cport = sata_device->satadev_addr.cport;
13413 	pmport = sata_device->satadev_addr.pmport;
13414 	qual = sata_device->satadev_addr.qual;
13415 
13416 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
13417 	if (qual == SATA_ADDR_DCPORT)
13418 		qual = SATA_ADDR_CPORT;
13419 	else
13420 		qual = SATA_ADDR_PMPORT;
13421 
13422 	if (qual == SATA_ADDR_PMPORT)
13423 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
13424 
13425 	/*
13426 	 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->
13427 	 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate().
13428 	 * Perform sanity check now.
13429 	 */
13430 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) {
13431 		/* No physical port activation supported. */
13432 		return (EINVAL);
13433 	}
13434 
13435 	/* Just ask HBA driver to activate port */
13436 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
13437 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
13438 		/*
13439 		 * Port activation failure.
13440 		 */
13441 		if (qual == SATA_ADDR_CPORT) {
13442 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
13443 			    cport)->cport_mutex);
13444 			sata_update_port_info(sata_hba_inst, sata_device);
13445 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
13446 				SATA_CPORT_STATE(sata_hba_inst, cport) =
13447 				    SATA_PSTATE_FAILED;
13448 				SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
13449 				    "sata_hba_ioctl: connect: failed to "
13450 				    "activate SATA port %d", cport);
13451 			}
13452 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
13453 			    cport)->cport_mutex);
13454 		} else { /* port multiplier device port */
13455 			mutex_enter(&pmportinfo->pmport_mutex);
13456 			sata_update_pmport_info(sata_hba_inst, sata_device);
13457 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
13458 				SATA_PMPORT_STATE(sata_hba_inst, cport,
13459 				    pmport) = SATA_PSTATE_FAILED;
13460 				SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
13461 				    "sata_hba_ioctl: connect: failed to "
13462 				    "activate SATA port %d:%d", cport, pmport);
13463 			}
13464 			mutex_exit(&pmportinfo->pmport_mutex);
13465 		}
13466 		return (EIO);
13467 	}
13468 
13469 	/* Virgin port state - will be updated by the port re-probe. */
13470 	if (qual == SATA_ADDR_CPORT) {
13471 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
13472 		    cport)->cport_mutex);
13473 		SATA_CPORT_STATE(sata_hba_inst, cport) = 0;
13474 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
13475 		    cport)->cport_mutex);
13476 	} else { /* port multiplier device port */
13477 		mutex_enter(&pmportinfo->pmport_mutex);
13478 		SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0;
13479 		mutex_exit(&pmportinfo->pmport_mutex);
13480 	}
13481 
13482 	/*
13483 	 * Probe the port to find its state and attached device.
13484 	 */
13485 	if (sata_reprobe_port(sata_hba_inst, sata_device,
13486 	    SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE)
13487 		rv = EIO;
13488 
13489 	/*
13490 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
13491 	 * without the hint
13492 	 */
13493 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
13494 	    SE_NO_HINT);
13495 
13496 	/*
13497 	 * If there is a device attached to the port, emit
13498 	 * a message.
13499 	 */
13500 	if (sata_device->satadev_type != SATA_DTYPE_NONE) {
13501 
13502 		if (qual == SATA_ADDR_CPORT) {
13503 			if (sata_device->satadev_type == SATA_DTYPE_PMULT) {
13504 				sata_log(sata_hba_inst, CE_WARN,
13505 				    "SATA port multiplier detected "
13506 				    "at port %d", cport);
13507 			} else {
13508 				sata_log(sata_hba_inst, CE_WARN,
13509 				    "SATA device detected at port %d", cport);
13510 				if (sata_device->satadev_type ==
13511 				    SATA_DTYPE_UNKNOWN) {
13512 				/*
13513 				 * A device was not successfully identified
13514 				 */
13515 				sata_log(sata_hba_inst, CE_WARN,
13516 				    "Could not identify SATA "
13517 				    "device at port %d", cport);
13518 				}
13519 			}
13520 		} else { /* port multiplier device port */
13521 			sata_log(sata_hba_inst, CE_WARN,
13522 			    "SATA device detected at port %d:%d",
13523 			    cport, pmport);
13524 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
13525 				/*
13526 				 * A device was not successfully identified
13527 				 */
13528 				sata_log(sata_hba_inst, CE_WARN,
13529 				    "Could not identify SATA "
13530 				    "device at port %d:%d", cport, pmport);
13531 			}
13532 		}
13533 	}
13534 
13535 	return (rv);
13536 }
13537 
13538 
13539 /*
13540  * Process sata device unconfigure request.
13541  * The unconfigure operation uses generic nexus operation to
13542  * offline a device. It leaves a target device node attached.
13543  * and obviously sata_drive_info attached as well, because
13544  * from the hardware point of view nothing has changed.
13545  */
13546 static int
13547 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst,
13548     sata_device_t *sata_device)
13549 {
13550 	int rv = 0;
13551 	dev_info_t *tdip;
13552 
13553 	/* We are addressing attached device, not a port */
13554 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
13555 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
13556 	else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT)
13557 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
13558 
13559 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
13560 	    &sata_device->satadev_addr)) != NULL) {
13561 
13562 		if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) {
13563 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13564 			    "sata_hba_ioctl: unconfigure: "
13565 			    "failed to unconfigure device at SATA port %d:%d",
13566 			    sata_device->satadev_addr.cport,
13567 			    sata_device->satadev_addr.pmport));
13568 			rv = EIO;
13569 		}
13570 		/*
13571 		 * The target node devi_state should be marked with
13572 		 * DEVI_DEVICE_OFFLINE by ndi_devi_offline().
13573 		 * This would be the indication for cfgadm that
13574 		 * the AP node occupant state is 'unconfigured'.
13575 		 */
13576 
13577 	} else {
13578 		/*
13579 		 * This would indicate a failure on the part of cfgadm
13580 		 * to detect correct state of the node prior to this
13581 		 * call - one cannot unconfigure non-existing device.
13582 		 */
13583 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13584 		    "sata_hba_ioctl: unconfigure: "
13585 		    "attempt to unconfigure non-existing device "
13586 		    "at SATA port %d:%d",
13587 		    sata_device->satadev_addr.cport,
13588 		    sata_device->satadev_addr.pmport));
13589 		rv = ENXIO;
13590 	}
13591 	return (rv);
13592 }
13593 
13594 /*
13595  * Process sata device configure request
13596  * If port is in a failed state, operation is aborted - one has to use
13597  * an explicit connect or port activate request to try to get a port into
13598  * non-failed mode. Port reset wil also work in such situation.
13599  * If the port is in disconnected (shutdown) state, the connect operation is
13600  * attempted prior to any other action.
13601  * When port is in the active state, there is a device attached and the target
13602  * node exists, a device was most likely offlined.
13603  * If target node does not exist, a new target node is created. In both cases
13604  * an attempt is made to online (configure) the device.
13605  *
13606  * NOTE: Port multiplier is supported.
13607  */
13608 static int
13609 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst,
13610     sata_device_t *sata_device)
13611 {
13612 	int cport, pmport, qual;
13613 	int rval;
13614 	boolean_t target = TRUE;
13615 	sata_cport_info_t *cportinfo;
13616 	sata_pmport_info_t *pmportinfo = NULL;
13617 	dev_info_t *tdip;
13618 	sata_drive_info_t *sdinfo;
13619 
13620 	cport = sata_device->satadev_addr.cport;
13621 	pmport = sata_device->satadev_addr.pmport;
13622 	qual = sata_device->satadev_addr.qual;
13623 
13624 	/* Get current port state */
13625 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
13626 	    (SATA_DIP(sata_hba_inst), sata_device);
13627 
13628 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
13629 	if (qual == SATA_ADDR_DPMPORT) {
13630 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
13631 		mutex_enter(&pmportinfo->pmport_mutex);
13632 		sata_update_pmport_info(sata_hba_inst, sata_device);
13633 		if (rval != SATA_SUCCESS ||
13634 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
13635 			/*
13636 			 * Obviously, device on a failed port is not visible
13637 			 */
13638 			mutex_exit(&pmportinfo->pmport_mutex);
13639 			return (ENXIO);
13640 		}
13641 		mutex_exit(&pmportinfo->pmport_mutex);
13642 	} else {
13643 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
13644 		    cport)->cport_mutex);
13645 		sata_update_port_info(sata_hba_inst, sata_device);
13646 		if (rval != SATA_SUCCESS ||
13647 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
13648 			/*
13649 			 * Obviously, device on a failed port is not visible
13650 			 */
13651 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
13652 			    cport)->cport_mutex);
13653 			return (ENXIO);
13654 		}
13655 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
13656 		    cport)->cport_mutex);
13657 	}
13658 
13659 	if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) {
13660 		/* need to activate port */
13661 		target = FALSE;
13662 
13663 		/* Sanity check */
13664 		if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
13665 			return (ENXIO);
13666 
13667 		/* Just let HBA driver to activate port */
13668 		if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
13669 		    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
13670 			/*
13671 			 * Port activation failure - do not change port state
13672 			 * unless the state returned by HBA indicates a port
13673 			 * failure.
13674 			 */
13675 			if (qual == SATA_ADDR_DPMPORT) {
13676 				mutex_enter(&pmportinfo->pmport_mutex);
13677 				sata_update_pmport_info(sata_hba_inst,
13678 				    sata_device);
13679 				if (sata_device->satadev_state &
13680 				    SATA_PSTATE_FAILED)
13681 					pmportinfo->pmport_state =
13682 					    SATA_PSTATE_FAILED;
13683 				mutex_exit(&pmportinfo->pmport_mutex);
13684 			} else {
13685 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
13686 				    cport)->cport_mutex);
13687 				sata_update_port_info(sata_hba_inst,
13688 				    sata_device);
13689 				if (sata_device->satadev_state &
13690 				    SATA_PSTATE_FAILED)
13691 					cportinfo->cport_state =
13692 					    SATA_PSTATE_FAILED;
13693 				mutex_exit(&SATA_CPORT_INFO(
13694 				    sata_hba_inst, cport)->cport_mutex);
13695 			}
13696 		}
13697 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13698 		    "sata_hba_ioctl: configure: "
13699 		    "failed to activate SATA port %d:%d",
13700 		    cport, pmport));
13701 		return (EIO);
13702 	}
13703 	/*
13704 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
13705 	 * without the hint.
13706 	 */
13707 	sata_gen_sysevent(sata_hba_inst,
13708 	    &sata_device->satadev_addr, SE_NO_HINT);
13709 
13710 	/* Virgin port state */
13711 	if (qual == SATA_ADDR_DPMPORT) {
13712 		mutex_enter(&pmportinfo->pmport_mutex);
13713 		pmportinfo->pmport_state = 0;
13714 		mutex_exit(&pmportinfo->pmport_mutex);
13715 	} else {
13716 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
13717 		    cport)-> cport_mutex);
13718 		cportinfo->cport_state = 0;
13719 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
13720 		    cport)->cport_mutex);
13721 	}
13722 	/*
13723 	 * Always reprobe port, to get current device info.
13724 	 */
13725 	if (sata_reprobe_port(sata_hba_inst, sata_device,
13726 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
13727 		return (EIO);
13728 
13729 	if (sata_device->satadev_type != SATA_DTYPE_NONE && target == FALSE) {
13730 		if (qual == SATA_ADDR_DPMPORT) {
13731 			/*
13732 			 * That's the transition from "inactive" port
13733 			 * to active one with device attached.
13734 			 */
13735 			sata_log(sata_hba_inst, CE_WARN,
13736 			    "SATA device detected at port %d:%d",
13737 			    cport, pmport);
13738 		} else {
13739 			/*
13740 			 * When PM is attached to the cport and cport is
13741 			 * activated, every PM device port needs to be reprobed.
13742 			 * We need to emit message for all devices detected
13743 			 * at port multiplier's device ports.
13744 			 * Add such code here.
13745 			 * For now, just inform about device attached to
13746 			 * cport.
13747 			 */
13748 			sata_log(sata_hba_inst, CE_WARN,
13749 			    "SATA device detected at port %d", cport);
13750 		}
13751 	}
13752 
13753 	/*
13754 	 * This is where real configuration operation starts.
13755 	 *
13756 	 * When PM is attached to the cport and cport is activated,
13757 	 * devices attached PM device ports may have to be configured
13758 	 * explicitly. This may change when port multiplier is supported.
13759 	 * For now, configure only disks and other valid target devices.
13760 	 */
13761 	if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) {
13762 		if (qual == SATA_ADDR_DCPORT) {
13763 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
13764 				/*
13765 				 * A device was not successfully identified
13766 				 */
13767 				sata_log(sata_hba_inst, CE_WARN,
13768 				    "Could not identify SATA "
13769 				    "device at port %d", cport);
13770 			}
13771 		} else { /* port multiplier device port */
13772 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
13773 				/*
13774 				 * A device was not successfully identified
13775 				 */
13776 				sata_log(sata_hba_inst, CE_WARN,
13777 				    "Could not identify SATA "
13778 				    "device at port %d:%d", cport, pmport);
13779 			}
13780 		}
13781 		return (ENXIO);		/* No device to configure */
13782 	}
13783 
13784 	/*
13785 	 * Here we may have a device in reset condition,
13786 	 * but because we are just configuring it, there is
13787 	 * no need to process the reset other than just
13788 	 * to clear device reset condition in the HBA driver.
13789 	 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will
13790 	 * cause a first command sent the HBA driver with the request
13791 	 * to clear device reset condition.
13792 	 */
13793 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
13794 	if (qual == SATA_ADDR_DPMPORT)
13795 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
13796 	else
13797 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
13798 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
13799 	if (sdinfo == NULL) {
13800 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
13801 		return (ENXIO);
13802 	}
13803 	if (sdinfo->satadrv_event_flags &
13804 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
13805 		sdinfo->satadrv_event_flags = 0;
13806 	}
13807 	sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
13808 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
13809 
13810 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
13811 	    &sata_device->satadev_addr)) != NULL) {
13812 		/*
13813 		 * Target node exists. Verify, that it belongs
13814 		 * to existing, attached device and not to
13815 		 * a removed device.
13816 		 */
13817 		if (sata_check_device_removed(tdip) == B_TRUE) {
13818 			if (qual == SATA_ADDR_DPMPORT)
13819 				sata_log(sata_hba_inst, CE_WARN,
13820 				    "SATA device at port %d cannot be "
13821 				    "configured. "
13822 				    "Application(s) accessing "
13823 				    "previously attached device "
13824 				    "have to release it before newly "
13825 				    "inserted device can be made accessible.",
13826 				    cport);
13827 			else
13828 				sata_log(sata_hba_inst, CE_WARN,
13829 				    "SATA device at port %d:%d cannot be"
13830 				    "configured. "
13831 				    "Application(s) accessing "
13832 				    "previously attached device "
13833 				    "have to release it before newly "
13834 				    "inserted device can be made accessible.",
13835 				    cport, pmport);
13836 			return (EIO);
13837 		}
13838 		/*
13839 		 * Device was not removed and re-inserted.
13840 		 * Try to online it.
13841 		 */
13842 		if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) {
13843 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13844 			    "sata_hba_ioctl: configure: "
13845 			    "onlining device at SATA port "
13846 			    "%d:%d failed", cport, pmport));
13847 			return (EIO);
13848 		}
13849 
13850 		if (qual == SATA_ADDR_DPMPORT) {
13851 			mutex_enter(&pmportinfo->pmport_mutex);
13852 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
13853 			mutex_exit(&pmportinfo->pmport_mutex);
13854 		} else {
13855 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
13856 			    cport)->cport_mutex);
13857 			cportinfo-> cport_tgtnode_clean = B_TRUE;
13858 			mutex_exit(&SATA_CPORT_INFO(
13859 			    sata_hba_inst, cport)->cport_mutex);
13860 		}
13861 	} else {
13862 		/*
13863 		 * No target node - need to create a new target node.
13864 		 */
13865 		if (qual == SATA_ADDR_DPMPORT) {
13866 			mutex_enter(&pmportinfo->pmport_mutex);
13867 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
13868 			mutex_exit(&pmportinfo->pmport_mutex);
13869 		} else {
13870 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
13871 			    cport_mutex);
13872 			cportinfo-> cport_tgtnode_clean = B_TRUE;
13873 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
13874 			    cport_mutex);
13875 		}
13876 
13877 		tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
13878 		    sata_hba_inst, &sata_device->satadev_addr);
13879 		if (tdip == NULL) {
13880 			/* Configure operation failed */
13881 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13882 			    "sata_hba_ioctl: configure: "
13883 			    "configuring SATA device at port %d:%d "
13884 			    "failed", cport, pmport));
13885 			return (EIO);
13886 		}
13887 	}
13888 	return (0);
13889 }
13890 
13891 
13892 /*
13893  * Process ioctl deactivate port request.
13894  * Arbitrarily unconfigure attached device, if any.
13895  * Even if the unconfigure fails, proceed with the
13896  * port deactivation.
13897  *
13898  * NOTE: Port Multiplier is supported now.
13899  */
13900 
13901 static int
13902 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst,
13903     sata_device_t *sata_device)
13904 {
13905 	int cport, pmport, qual;
13906 	int rval, rv = 0;
13907 	int npmport;
13908 	sata_cport_info_t *cportinfo;
13909 	sata_pmport_info_t *pmportinfo;
13910 	sata_pmult_info_t *pmultinfo;
13911 	dev_info_t *tdip;
13912 	sata_drive_info_t *sdinfo = NULL;
13913 	sata_device_t subsdevice;
13914 
13915 	/* Sanity check */
13916 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL)
13917 		return (ENOTSUP);
13918 
13919 	cport = sata_device->satadev_addr.cport;
13920 	pmport = sata_device->satadev_addr.pmport;
13921 	qual = sata_device->satadev_addr.qual;
13922 
13923 	/* SCSI_TO_SATA_ADDR_QUAL() translate ap_id into a device qualifier */
13924 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
13925 	if (qual == SATA_ADDR_DCPORT)
13926 		qual = SATA_ADDR_CPORT;
13927 	else
13928 		qual = SATA_ADDR_PMPORT;
13929 
13930 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
13931 	if (qual == SATA_ADDR_PMPORT)
13932 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
13933 
13934 	/*
13935 	 * Processing port multiplier
13936 	 */
13937 	if (qual == SATA_ADDR_CPORT &&
13938 	    SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) {
13939 		mutex_enter(&cportinfo->cport_mutex);
13940 
13941 		/* Deactivate all sub-deices */
13942 		pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
13943 		if (pmultinfo != NULL) {
13944 			for (npmport = 0; npmport < SATA_NUM_PMPORTS(
13945 			    sata_hba_inst, cport); npmport++) {
13946 
13947 				subsdevice.satadev_addr.cport = cport;
13948 				subsdevice.satadev_addr.pmport =
13949 				    (uint8_t)npmport;
13950 				subsdevice.satadev_addr.qual =
13951 				    SATA_ADDR_DPMPORT;
13952 
13953 				SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
13954 				    "sata_hba_ioctl: deactivate: trying to "
13955 				    "deactivate SATA port %d:%d",
13956 				    cport, npmport);
13957 
13958 				mutex_exit(&cportinfo->cport_mutex);
13959 				if (sata_ioctl_deactivate(sata_hba_inst,
13960 				    &subsdevice) == SATA_SUCCESS) {
13961 					SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
13962 					    "[Deactivate] device at port %d:%d "
13963 					    "successfully.", cport, npmport);
13964 				}
13965 				mutex_enter(&cportinfo->cport_mutex);
13966 			}
13967 		}
13968 
13969 		/* Deactivate the port multiplier now. */
13970 		cportinfo->cport_state &= ~SATA_STATE_READY;
13971 		mutex_exit(&cportinfo->cport_mutex);
13972 
13973 		sata_device->satadev_addr.qual = qual;
13974 		rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
13975 		    (SATA_DIP(sata_hba_inst), sata_device);
13976 
13977 		sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
13978 		    SE_NO_HINT);
13979 
13980 		mutex_enter(&cportinfo->cport_mutex);
13981 		sata_update_port_info(sata_hba_inst, sata_device);
13982 		if (rval != SATA_SUCCESS) {
13983 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
13984 				cportinfo->cport_state = SATA_PSTATE_FAILED;
13985 			}
13986 			rv = EIO;
13987 		} else {
13988 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
13989 		}
13990 		mutex_exit(&cportinfo->cport_mutex);
13991 
13992 		return (rv);
13993 	}
13994 
13995 	/*
13996 	 * Process non-port-multiplier device - it could be a drive connected
13997 	 * to a port multiplier port or a controller port.
13998 	 */
13999 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14000 	if (qual == SATA_ADDR_CPORT) {
14001 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
14002 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
14003 			/* deal only with valid devices */
14004 			if ((cportinfo->cport_dev_type &
14005 			    SATA_VALID_DEV_TYPE) != 0)
14006 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14007 		}
14008 		cportinfo->cport_state &= ~SATA_STATE_READY;
14009 	} else {
14010 		/* Port multiplier device port */
14011 		mutex_enter(&pmportinfo->pmport_mutex);
14012 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
14013 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
14014 		    (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0)
14015 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
14016 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
14017 		mutex_exit(&pmportinfo->pmport_mutex);
14018 	}
14019 
14020 	if (sdinfo != NULL) {
14021 		/*
14022 		 * If a target node exists, try to offline a device and
14023 		 * to remove a target node.
14024 		 */
14025 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
14026 		    cport_mutex);
14027 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
14028 		    &sata_device->satadev_addr);
14029 		if (tdip != NULL) {
14030 			/* target node exist */
14031 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
14032 			    "sata_hba_ioctl: port deactivate: "
14033 			    "target node exists.", NULL);
14034 
14035 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) !=
14036 			    NDI_SUCCESS) {
14037 				SATA_LOG_D((sata_hba_inst, CE_WARN,
14038 				    "sata_hba_ioctl: port deactivate: "
14039 				    "failed to unconfigure device at port "
14040 				    "%d:%d before deactivating the port",
14041 				    cport, pmport));
14042 				/*
14043 				 * Set DEVICE REMOVED state in the target
14044 				 * node. It will prevent an access to
14045 				 * the device even when a new device is
14046 				 * attached, until the old target node is
14047 				 * released, removed and recreated for a new
14048 				 * device.
14049 				 */
14050 				sata_set_device_removed(tdip);
14051 
14052 				/*
14053 				 * Instruct the event daemon to try the
14054 				 * target node cleanup later.
14055 				 */
14056 				sata_set_target_node_cleanup(sata_hba_inst,
14057 				    &sata_device->satadev_addr);
14058 			}
14059 		}
14060 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
14061 		    cport_mutex);
14062 		/*
14063 		 * In any case, remove and release sata_drive_info
14064 		 * structure.
14065 		 */
14066 		if (qual == SATA_ADDR_CPORT) {
14067 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14068 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14069 		} else { /* port multiplier device port */
14070 			mutex_enter(&pmportinfo->pmport_mutex);
14071 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
14072 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
14073 			mutex_exit(&pmportinfo->pmport_mutex);
14074 		}
14075 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
14076 	}
14077 
14078 	if (qual == SATA_ADDR_CPORT) {
14079 		cportinfo->cport_state &= ~(SATA_STATE_PROBED |
14080 		    SATA_STATE_PROBING);
14081 	} else if (qual == SATA_ADDR_PMPORT) {
14082 		mutex_enter(&pmportinfo->pmport_mutex);
14083 		pmportinfo->pmport_state &= ~(SATA_STATE_PROBED |
14084 		    SATA_STATE_PROBING);
14085 		mutex_exit(&pmportinfo->pmport_mutex);
14086 	}
14087 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14088 
14089 	/* Just let HBA driver to deactivate port */
14090 	sata_device->satadev_addr.qual = qual;
14091 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
14092 	    (SATA_DIP(sata_hba_inst), sata_device);
14093 
14094 	/*
14095 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14096 	 * without the hint
14097 	 */
14098 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14099 	    SE_NO_HINT);
14100 
14101 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14102 	sata_update_port_info(sata_hba_inst, sata_device);
14103 	if (qual == SATA_ADDR_CPORT) {
14104 		if (rval != SATA_SUCCESS) {
14105 			/*
14106 			 * Port deactivation failure - do not change port state
14107 			 * unless the state returned by HBA indicates a port
14108 			 * failure.
14109 			 */
14110 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
14111 				SATA_CPORT_STATE(sata_hba_inst, cport) =
14112 				    SATA_PSTATE_FAILED;
14113 			}
14114 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14115 			    "sata_hba_ioctl: port deactivate: "
14116 			    "cannot deactivate SATA port %d", cport));
14117 			rv = EIO;
14118 		} else {
14119 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
14120 		}
14121 	} else {
14122 		mutex_enter(&pmportinfo->pmport_mutex);
14123 		if (rval != SATA_SUCCESS) {
14124 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
14125 				SATA_PMPORT_STATE(sata_hba_inst, cport,
14126 				    pmport) = SATA_PSTATE_FAILED;
14127 			}
14128 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14129 			    "sata_hba_ioctl: port deactivate: "
14130 			    "cannot deactivate SATA port %d:%d",
14131 			    cport, pmport));
14132 			rv = EIO;
14133 		} else {
14134 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
14135 		}
14136 		mutex_exit(&pmportinfo->pmport_mutex);
14137 	}
14138 
14139 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14140 
14141 	return (rv);
14142 }
14143 
14144 /*
14145  * Process ioctl port activate request.
14146  *
14147  * NOTE: Port multiplier is supported now.
14148  */
14149 static int
14150 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst,
14151     sata_device_t *sata_device)
14152 {
14153 	int cport, pmport, qual;
14154 	sata_cport_info_t *cportinfo;
14155 	sata_pmport_info_t *pmportinfo = NULL;
14156 	boolean_t dev_existed = TRUE;
14157 
14158 	/* Sanity check */
14159 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
14160 		return (ENOTSUP);
14161 
14162 	cport = sata_device->satadev_addr.cport;
14163 	pmport = sata_device->satadev_addr.pmport;
14164 	qual = sata_device->satadev_addr.qual;
14165 
14166 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
14167 
14168 	/*
14169 	 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL())
14170 	 * is a device. But what we are dealing with is port/pmport.
14171 	 */
14172 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
14173 	if (qual == SATA_ADDR_DCPORT)
14174 		sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT;
14175 	else
14176 		sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT;
14177 
14178 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14179 	if (qual == SATA_ADDR_PMPORT) {
14180 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14181 		if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN ||
14182 		    pmportinfo->pmport_dev_type == SATA_DTYPE_NONE)
14183 			dev_existed = FALSE;
14184 	} else { /* cport */
14185 		if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN ||
14186 		    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
14187 			dev_existed = FALSE;
14188 	}
14189 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14190 
14191 	/* Just let HBA driver to activate port, if necessary */
14192 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
14193 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
14194 		/*
14195 		 * Port activation failure - do not change port state unless
14196 		 * the state returned by HBA indicates a port failure.
14197 		 */
14198 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14199 		    cport)->cport_mutex);
14200 		sata_update_port_info(sata_hba_inst, sata_device);
14201 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
14202 			if (qual == SATA_ADDR_PMPORT) {
14203 				mutex_enter(&pmportinfo->pmport_mutex);
14204 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
14205 				mutex_exit(&pmportinfo->pmport_mutex);
14206 			} else
14207 				cportinfo->cport_state = SATA_PSTATE_FAILED;
14208 
14209 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14210 			    cport)->cport_mutex);
14211 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14212 			    "sata_hba_ioctl: port activate: cannot activate "
14213 			    "SATA port %d:%d", cport, pmport));
14214 			return (EIO);
14215 		}
14216 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14217 	}
14218 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14219 	if (qual == SATA_ADDR_PMPORT) {
14220 		mutex_enter(&pmportinfo->pmport_mutex);
14221 		pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN;
14222 		mutex_exit(&pmportinfo->pmport_mutex);
14223 	} else
14224 		cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN;
14225 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14226 
14227 	/*
14228 	 * Re-probe port to find its current state and possibly attached device.
14229 	 * Port re-probing may change the cportinfo device type if device is
14230 	 * found attached.
14231 	 * If port probing failed, the device type would be set to
14232 	 * SATA_DTYPE_NONE.
14233 	 */
14234 	(void) sata_reprobe_port(sata_hba_inst, sata_device,
14235 	    SATA_DEV_IDENTIFY_RETRY);
14236 
14237 	/*
14238 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14239 	 * without the hint.
14240 	 */
14241 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14242 	    SE_NO_HINT);
14243 
14244 	if (dev_existed == FALSE) {
14245 		if (qual == SATA_ADDR_PMPORT &&
14246 		    pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
14247 			/*
14248 			 * That's the transition from the "inactive" port state
14249 			 * or the active port without a device attached to the
14250 			 * active port state with a device attached.
14251 			 */
14252 			sata_log(sata_hba_inst, CE_WARN,
14253 			    "SATA device detected at port %d:%d",
14254 			    cport, pmport);
14255 		} else if (qual == SATA_ADDR_CPORT &&
14256 		    cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
14257 			/*
14258 			 * That's the transition from the "inactive" port state
14259 			 * or the active port without a device attached to the
14260 			 * active port state with a device attached.
14261 			 */
14262 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
14263 				sata_log(sata_hba_inst, CE_WARN,
14264 				    "SATA device detected at port %d", cport);
14265 			} else {
14266 				sata_log(sata_hba_inst, CE_WARN,
14267 				    "SATA port multiplier detected at port %d",
14268 				    cport);
14269 			}
14270 		}
14271 	}
14272 	return (0);
14273 }
14274 
14275 
14276 
14277 /*
14278  * Process ioctl reset port request.
14279  *
14280  * NOTE: Port-Multiplier is supported.
14281  */
14282 static int
14283 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst,
14284     sata_device_t *sata_device)
14285 {
14286 	int cport, pmport, qual;
14287 	int rv = 0;
14288 
14289 	cport = sata_device->satadev_addr.cport;
14290 	pmport = sata_device->satadev_addr.pmport;
14291 	qual = sata_device->satadev_addr.qual;
14292 
14293 	/*
14294 	 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL())
14295 	 * is a device. But what we are dealing with is port/pmport.
14296 	 */
14297 	if (qual == SATA_ADDR_DCPORT)
14298 		sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT;
14299 	else
14300 		sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT;
14301 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
14302 
14303 	/* Sanity check */
14304 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
14305 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14306 		    "sata_hba_ioctl: sata_hba_tran missing required "
14307 		    "function sata_tran_reset_dport"));
14308 		return (ENOTSUP);
14309 	}
14310 
14311 	/* Ask HBA to reset port */
14312 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
14313 	    sata_device) != SATA_SUCCESS) {
14314 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14315 		    "sata_hba_ioctl: reset port: failed %d:%d",
14316 		    cport, pmport));
14317 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
14318 		    cport_mutex);
14319 		sata_update_port_info(sata_hba_inst, sata_device);
14320 		if (qual == SATA_ADDR_CPORT)
14321 			SATA_CPORT_STATE(sata_hba_inst, cport) =
14322 			    SATA_PSTATE_FAILED;
14323 		else {
14324 			mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport,
14325 			    pmport));
14326 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
14327 			    SATA_PSTATE_FAILED;
14328 			mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport,
14329 			    pmport));
14330 		}
14331 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
14332 		    cport_mutex);
14333 		rv = EIO;
14334 	}
14335 	/*
14336 	 * Beacuse the port was reset, it should be probed and
14337 	 * attached device reinitialized. At this point the
14338 	 * port state is unknown - it's state is HBA-specific.
14339 	 * Re-probe port to get its state.
14340 	 */
14341 	if (sata_reprobe_port(sata_hba_inst, sata_device,
14342 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) {
14343 		rv = EIO;
14344 	}
14345 	return (rv);
14346 }
14347 
14348 /*
14349  * Process ioctl reset device request.
14350  *
14351  * NOTE: Port multiplier is supported.
14352  */
14353 static int
14354 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst,
14355     sata_device_t *sata_device)
14356 {
14357 	sata_drive_info_t *sdinfo = NULL;
14358 	sata_pmult_info_t *pmultinfo = NULL;
14359 	int cport, pmport;
14360 	int rv = 0;
14361 
14362 	/* Sanity check */
14363 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
14364 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14365 		    "sata_hba_ioctl: sata_hba_tran missing required "
14366 		    "function sata_tran_reset_dport"));
14367 		return (ENOTSUP);
14368 	}
14369 
14370 	cport = sata_device->satadev_addr.cport;
14371 	pmport = sata_device->satadev_addr.pmport;
14372 
14373 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14374 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
14375 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
14376 		    SATA_DTYPE_PMULT)
14377 			pmultinfo = SATA_CPORT_INFO(sata_hba_inst, cport)->
14378 			    cport_devp.cport_sata_pmult;
14379 		else
14380 			sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
14381 			    sata_device->satadev_addr.cport);
14382 	} else { /* port multiplier */
14383 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
14384 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
14385 		    sata_device->satadev_addr.cport,
14386 		    sata_device->satadev_addr.pmport);
14387 	}
14388 	if (sdinfo == NULL && pmultinfo == NULL) {
14389 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14390 		return (EINVAL);
14391 	}
14392 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14393 
14394 	/* Ask HBA to reset device */
14395 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
14396 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
14397 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14398 		    "sata_hba_ioctl: reset device: failed at port %d:%d",
14399 		    cport, pmport));
14400 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
14401 		    cport_mutex);
14402 		sata_update_port_info(sata_hba_inst, sata_device);
14403 		/*
14404 		 * Device info structure remains attached. Another device reset
14405 		 * or port disconnect/connect and re-probing is
14406 		 * needed to change it's state
14407 		 */
14408 		if (sdinfo != NULL) {
14409 			sdinfo->satadrv_state &= ~SATA_STATE_READY;
14410 			sdinfo->satadrv_state |= SATA_DSTATE_FAILED;
14411 		} else if (pmultinfo != NULL) {
14412 			pmultinfo->pmult_state &= ~SATA_STATE_READY;
14413 			pmultinfo->pmult_state |= SATA_DSTATE_FAILED;
14414 		}
14415 
14416 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14417 		rv = EIO;
14418 	}
14419 	/*
14420 	 * If attached device was a port multiplier, some extra processing
14421 	 * may be needed to bring it back. SATA specification requies a
14422 	 * mandatory software reset on host port to reliably enumerate a port
14423 	 * multiplier, the HBA driver should handle that after reset
14424 	 * operation.
14425 	 */
14426 	return (rv);
14427 }
14428 
14429 
14430 /*
14431  * Process ioctl reset all request.
14432  */
14433 static int
14434 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst)
14435 {
14436 	sata_device_t sata_device;
14437 	int rv = 0;
14438 	int tcport;
14439 	int tpmport = 0;
14440 
14441 	sata_device.satadev_rev = SATA_DEVICE_REV;
14442 
14443 	/*
14444 	 * There is no protection here for configured devices.
14445 	 */
14446 	/* Sanity check */
14447 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
14448 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14449 		    "sata_hba_ioctl: sata_hba_tran missing required "
14450 		    "function sata_tran_reset_dport"));
14451 		return (ENOTSUP);
14452 	}
14453 
14454 	/*
14455 	 * Need to lock all ports, not just one.
14456 	 * If any port is locked by event processing, fail the whole operation.
14457 	 * One port is already locked, but for simplicity lock it again.
14458 	 */
14459 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
14460 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
14461 		    cport_mutex);
14462 		if (((SATA_CPORT_INFO(sata_hba_inst, tcport)->
14463 		    cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) {
14464 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
14465 			    cport_mutex);
14466 			rv = EBUSY;
14467 			break;
14468 		} else {
14469 			/*
14470 			 * It is enough to lock cport in command-based
14471 			 * switching mode.
14472 			 */
14473 			SATA_CPORT_INFO(sata_hba_inst, tcport)->
14474 			    cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
14475 		}
14476 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
14477 		    cport_mutex);
14478 	}
14479 
14480 	if (rv == 0) {
14481 		/*
14482 		 * All cports were successfully locked.
14483 		 * Reset main SATA controller.
14484 		 * Set the device address to port 0, to have a valid device
14485 		 * address.
14486 		 */
14487 		sata_device.satadev_addr.qual = SATA_ADDR_CNTRL;
14488 		sata_device.satadev_addr.cport = 0;
14489 		sata_device.satadev_addr.pmport = 0;
14490 
14491 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
14492 		    (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) {
14493 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14494 			    "sata_hba_ioctl: reset controller failed"));
14495 			return (EIO);
14496 		}
14497 		/*
14498 		 * Because ports were reset, port states are unknown.
14499 		 * They should be re-probed to get their state and
14500 		 * attached devices should be reinitialized.
14501 		 */
14502 		for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst);
14503 		    tcport++) {
14504 			sata_device.satadev_addr.cport = tcport;
14505 			sata_device.satadev_addr.pmport = tpmport;
14506 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
14507 
14508 			/*
14509 			 * The sata_reprobe_port() will mark a
14510 			 * SATA_EVNT_DEVICE_RESET event on the port
14511 			 * multiplier, all its sub-ports will be probed by
14512 			 * sata daemon afterwards.
14513 			 */
14514 			if (sata_reprobe_port(sata_hba_inst, &sata_device,
14515 			    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
14516 				rv = EIO;
14517 		}
14518 	}
14519 	/*
14520 	 * Unlock all ports
14521 	 */
14522 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
14523 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
14524 		    cport_mutex);
14525 		SATA_CPORT_INFO(sata_hba_inst, tcport)->
14526 		    cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
14527 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
14528 		    cport_mutex);
14529 	}
14530 
14531 	/*
14532 	 * This operation returns EFAULT if either reset
14533 	 * controller failed or a re-probing of any port failed.
14534 	 */
14535 	return (rv);
14536 }
14537 
14538 
14539 /*
14540  * Process ioctl port self test request.
14541  *
14542  * NOTE: Port multiplier code is not completed nor tested.
14543  */
14544 static int
14545 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst,
14546     sata_device_t *sata_device)
14547 {
14548 	int cport, pmport, qual;
14549 	int rv = 0;
14550 
14551 	/* Sanity check */
14552 	if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL)
14553 		return (ENOTSUP);
14554 
14555 	cport = sata_device->satadev_addr.cport;
14556 	pmport = sata_device->satadev_addr.pmport;
14557 	qual = sata_device->satadev_addr.qual;
14558 
14559 	/*
14560 	 * There is no protection here for a configured
14561 	 * device attached to this port.
14562 	 */
14563 
14564 	if ((*SATA_SELFTEST_FUNC(sata_hba_inst))
14565 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
14566 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14567 		    "sata_hba_ioctl: port selftest: "
14568 		    "failed port %d:%d", cport, pmport));
14569 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
14570 		    cport_mutex);
14571 		sata_update_port_info(sata_hba_inst, sata_device);
14572 		if (qual == SATA_ADDR_CPORT)
14573 			SATA_CPORT_STATE(sata_hba_inst, cport) =
14574 			    SATA_PSTATE_FAILED;
14575 		else { /* port multiplier device port */
14576 			mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst,
14577 			    cport, pmport));
14578 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
14579 			    SATA_PSTATE_FAILED;
14580 			mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst,
14581 			    cport, pmport));
14582 		}
14583 
14584 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
14585 		    cport_mutex);
14586 		return (EIO);
14587 	}
14588 	/*
14589 	 * Beacuse the port was reset in the course of testing, it should be
14590 	 * re-probed and attached device state should be restored. At this
14591 	 * point the port state is unknown - it's state is HBA-specific.
14592 	 * Force port re-probing to get it into a known state.
14593 	 */
14594 	if (sata_reprobe_port(sata_hba_inst, sata_device,
14595 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
14596 		rv = EIO;
14597 	return (rv);
14598 }
14599 
14600 
14601 /*
14602  * sata_cfgadm_state:
14603  * Use the sata port state and state of the target node to figure out
14604  * the cfgadm_state.
14605  *
14606  * The port argument is a value with encoded cport,
14607  * pmport and address qualifier, in the same manner as a scsi target number.
14608  * SCSI_TO_SATA_CPORT macro extracts cport number,
14609  * SCSI_TO_SATA_PMPORT extracts pmport number and
14610  * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag.
14611  *
14612  * Port multiplier is supported.
14613  */
14614 
14615 static void
14616 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port,
14617     devctl_ap_state_t *ap_state)
14618 {
14619 	uint8_t		cport, pmport, qual;
14620 	uint32_t	port_state, pmult_state;
14621 	uint32_t	dev_type;
14622 	sata_drive_info_t *sdinfo;
14623 
14624 	cport = SCSI_TO_SATA_CPORT(port);
14625 	pmport = SCSI_TO_SATA_PMPORT(port);
14626 	qual = SCSI_TO_SATA_ADDR_QUAL(port);
14627 
14628 	/* Check cport state */
14629 	port_state = SATA_CPORT_STATE(sata_hba_inst, cport);
14630 	if (port_state & SATA_PSTATE_SHUTDOWN ||
14631 	    port_state & SATA_PSTATE_FAILED) {
14632 		ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
14633 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
14634 		if (port_state & SATA_PSTATE_FAILED)
14635 			ap_state->ap_condition = AP_COND_FAILED;
14636 		else
14637 			ap_state->ap_condition = AP_COND_UNKNOWN;
14638 
14639 		return;
14640 	}
14641 
14642 	/* cport state is okay. Now check pmport state */
14643 	if (qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) {
14644 		/* Sanity check */
14645 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
14646 		    SATA_DTYPE_PMULT || SATA_PMPORT_INFO(sata_hba_inst,
14647 		    cport, pmport) == NULL)
14648 			return;
14649 		port_state = SATA_PMPORT_STATE(sata_hba_inst, cport, pmport);
14650 		if (port_state & SATA_PSTATE_SHUTDOWN ||
14651 		    port_state & SATA_PSTATE_FAILED) {
14652 			ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
14653 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
14654 			if (port_state & SATA_PSTATE_FAILED)
14655 				ap_state->ap_condition = AP_COND_FAILED;
14656 			else
14657 				ap_state->ap_condition = AP_COND_UNKNOWN;
14658 
14659 			return;
14660 		}
14661 	}
14662 
14663 	/* Port is enabled and ready */
14664 	if (qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_CPORT)
14665 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, cport);
14666 	else
14667 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, pmport);
14668 
14669 	switch (dev_type) {
14670 	case SATA_DTYPE_NONE:
14671 	{
14672 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
14673 		ap_state->ap_condition = AP_COND_OK;
14674 		/* No device attached */
14675 		ap_state->ap_rstate = AP_RSTATE_EMPTY;
14676 		break;
14677 	}
14678 	case SATA_DTYPE_PMULT:
14679 	{
14680 		/* Need to check port multiplier state */
14681 		ASSERT(qual == SATA_ADDR_DCPORT);
14682 		pmult_state = SATA_PMULT_INFO(sata_hba_inst, cport)->
14683 		    pmult_state;
14684 		if (pmult_state & (SATA_PSTATE_SHUTDOWN|SATA_PSTATE_FAILED)) {
14685 			ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
14686 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
14687 			if (pmult_state & SATA_PSTATE_FAILED)
14688 				ap_state->ap_condition = AP_COND_FAILED;
14689 			else
14690 				ap_state->ap_condition = AP_COND_UNKNOWN;
14691 
14692 			return;
14693 		}
14694 
14695 		/* Port multiplier is not configurable */
14696 		ap_state->ap_ostate = AP_OSTATE_CONFIGURED;
14697 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
14698 		ap_state->ap_condition = AP_COND_OK;
14699 		break;
14700 	}
14701 
14702 	case SATA_DTYPE_ATADISK:
14703 	case SATA_DTYPE_ATAPICD:
14704 	case SATA_DTYPE_ATAPITAPE:
14705 	case SATA_DTYPE_ATAPIDISK:
14706 	{
14707 		dev_info_t *tdip = NULL;
14708 		dev_info_t *dip = NULL;
14709 		int circ;
14710 
14711 		dip = SATA_DIP(sata_hba_inst);
14712 		tdip = sata_get_target_dip(dip, cport, pmport);
14713 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
14714 		if (tdip != NULL) {
14715 			ndi_devi_enter(dip, &circ);
14716 			mutex_enter(&(DEVI(tdip)->devi_lock));
14717 			if (DEVI_IS_DEVICE_REMOVED(tdip)) {
14718 				/*
14719 				 * There could be the case where previously
14720 				 * configured and opened device was removed
14721 				 * and unknown device was plugged.
14722 				 * In such case we want to show a device, and
14723 				 * its configured or unconfigured state but
14724 				 * indicate unusable condition untill the
14725 				 * old target node is released and removed.
14726 				 */
14727 				ap_state->ap_condition = AP_COND_UNUSABLE;
14728 			} else {
14729 				mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst,
14730 				    cport));
14731 				sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
14732 				    cport);
14733 				if (sdinfo != NULL) {
14734 					if ((sdinfo->satadrv_state &
14735 					    SATA_DSTATE_FAILED) != 0)
14736 						ap_state->ap_condition =
14737 						    AP_COND_FAILED;
14738 					else
14739 						ap_state->ap_condition =
14740 						    AP_COND_OK;
14741 				} else {
14742 					ap_state->ap_condition =
14743 					    AP_COND_UNKNOWN;
14744 				}
14745 				mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst,
14746 				    cport));
14747 			}
14748 			if ((DEVI_IS_DEVICE_OFFLINE(tdip)) ||
14749 			    (DEVI_IS_DEVICE_DOWN(tdip))) {
14750 				ap_state->ap_ostate =
14751 				    AP_OSTATE_UNCONFIGURED;
14752 			} else {
14753 				ap_state->ap_ostate =
14754 				    AP_OSTATE_CONFIGURED;
14755 			}
14756 			mutex_exit(&(DEVI(tdip)->devi_lock));
14757 			ndi_devi_exit(dip, circ);
14758 		} else {
14759 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
14760 			ap_state->ap_condition = AP_COND_UNKNOWN;
14761 		}
14762 		break;
14763 	}
14764 	default:
14765 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
14766 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
14767 		ap_state->ap_condition = AP_COND_UNKNOWN;
14768 		/*
14769 		 * This is actually internal error condition (non fatal),
14770 		 * because we have already checked all defined device types.
14771 		 */
14772 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14773 		    "sata_cfgadm_state: Internal error: "
14774 		    "unknown device type"));
14775 		break;
14776 	}
14777 }
14778 
14779 
14780 /*
14781  * Process ioctl get device path request.
14782  *
14783  * NOTE: Port multiplier has no target dip. Devices connected to port
14784  * multiplier have target node attached to the HBA node. The only difference
14785  * between them and the directly-attached device node is a target address.
14786  */
14787 static int
14788 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst,
14789     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
14790 {
14791 	char path[MAXPATHLEN];
14792 	uint32_t size;
14793 	dev_info_t *tdip;
14794 
14795 	(void) strcpy(path, "/devices");
14796 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
14797 	    &sata_device->satadev_addr)) == NULL) {
14798 		/*
14799 		 * No such device. If this is a request for a size, do not
14800 		 * return EINVAL for non-existing target, because cfgadm
14801 		 * will then indicate a meaningless ioctl failure.
14802 		 * If this is a request for a path, indicate invalid
14803 		 * argument.
14804 		 */
14805 		if (ioc->get_size == 0)
14806 			return (EINVAL);
14807 	} else {
14808 		(void) ddi_pathname(tdip, path + strlen(path));
14809 	}
14810 	size = strlen(path) + 1;
14811 
14812 	if (ioc->get_size != 0) {
14813 		if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz,
14814 		    mode) != 0)
14815 			return (EFAULT);
14816 	} else {
14817 		if (ioc->bufsiz != size)
14818 			return (EINVAL);
14819 
14820 		else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz,
14821 		    mode) != 0)
14822 			return (EFAULT);
14823 	}
14824 	return (0);
14825 }
14826 
14827 /*
14828  * Process ioctl get attachment point type request.
14829  *
14830  * NOTE: Port multiplier is supported.
14831  */
14832 static	int
14833 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst,
14834     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
14835 {
14836 	uint32_t	type_len;
14837 	const char	*ap_type;
14838 	int		dev_type;
14839 
14840 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
14841 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst,
14842 		    sata_device->satadev_addr.cport);
14843 	else /* pmport */
14844 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst,
14845 		    sata_device->satadev_addr.cport,
14846 		    sata_device->satadev_addr.pmport);
14847 
14848 	switch (dev_type) {
14849 	case SATA_DTYPE_NONE:
14850 		ap_type = "port";
14851 		break;
14852 
14853 	case SATA_DTYPE_ATADISK:
14854 	case SATA_DTYPE_ATAPIDISK:
14855 		ap_type = "disk";
14856 		break;
14857 
14858 	case SATA_DTYPE_ATAPICD:
14859 		ap_type = "cd/dvd";
14860 		break;
14861 
14862 	case SATA_DTYPE_ATAPITAPE:
14863 		ap_type = "tape";
14864 		break;
14865 
14866 	case SATA_DTYPE_PMULT:
14867 		ap_type = "sata-pmult";
14868 		break;
14869 
14870 	case SATA_DTYPE_UNKNOWN:
14871 		ap_type = "unknown";
14872 		break;
14873 
14874 	default:
14875 		ap_type = "unsupported";
14876 		break;
14877 
14878 	} /* end of dev_type switch */
14879 
14880 	type_len = strlen(ap_type) + 1;
14881 
14882 	if (ioc->get_size) {
14883 		if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz,
14884 		    mode) != 0)
14885 			return (EFAULT);
14886 	} else {
14887 		if (ioc->bufsiz != type_len)
14888 			return (EINVAL);
14889 
14890 		if (ddi_copyout((void *)ap_type, ioc->buf,
14891 		    ioc->bufsiz, mode) != 0)
14892 			return (EFAULT);
14893 	}
14894 	return (0);
14895 
14896 }
14897 
14898 /*
14899  * Process ioctl get device model info request.
14900  * This operation should return to cfgadm the device model
14901  * information string
14902  *
14903  * NOTE: Port multiplier is supported.
14904  */
14905 static	int
14906 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst,
14907     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
14908 {
14909 	sata_drive_info_t *sdinfo;
14910 	uint32_t info_len;
14911 	char ap_info[SATA_ID_MODEL_LEN + 1];
14912 
14913 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14914 	    sata_device->satadev_addr.cport)->cport_mutex);
14915 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
14916 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
14917 		    sata_device->satadev_addr.cport);
14918 	else /* port multiplier */
14919 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
14920 		    sata_device->satadev_addr.cport,
14921 		    sata_device->satadev_addr.pmport);
14922 	if (sdinfo == NULL) {
14923 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14924 		    sata_device->satadev_addr.cport)->cport_mutex);
14925 		return (EINVAL);
14926 	}
14927 
14928 #ifdef	_LITTLE_ENDIAN
14929 	swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
14930 #else	/* _LITTLE_ENDIAN */
14931 	bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
14932 #endif	/* _LITTLE_ENDIAN */
14933 
14934 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14935 	    sata_device->satadev_addr.cport)->cport_mutex);
14936 
14937 	ap_info[SATA_ID_MODEL_LEN] = '\0';
14938 
14939 	info_len = strlen(ap_info) + 1;
14940 
14941 	if (ioc->get_size) {
14942 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
14943 		    mode) != 0)
14944 			return (EFAULT);
14945 	} else {
14946 		if (ioc->bufsiz < info_len)
14947 			return (EINVAL);
14948 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
14949 		    mode) != 0)
14950 			return (EFAULT);
14951 	}
14952 	return (0);
14953 }
14954 
14955 
14956 /*
14957  * Process ioctl get device firmware revision info request.
14958  * This operation should return to cfgadm the device firmware revision
14959  * information string
14960  *
14961  * Port multiplier is supported.
14962  */
14963 static	int
14964 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst,
14965     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
14966 {
14967 	sata_drive_info_t *sdinfo;
14968 	uint32_t info_len;
14969 	char ap_info[SATA_ID_FW_LEN + 1];
14970 
14971 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14972 	    sata_device->satadev_addr.cport)->cport_mutex);
14973 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
14974 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
14975 		    sata_device->satadev_addr.cport);
14976 	else /* port multiplier */
14977 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
14978 		    sata_device->satadev_addr.cport,
14979 		    sata_device->satadev_addr.pmport);
14980 	if (sdinfo == NULL) {
14981 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14982 		    sata_device->satadev_addr.cport)->cport_mutex);
14983 		return (EINVAL);
14984 	}
14985 
14986 #ifdef	_LITTLE_ENDIAN
14987 	swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
14988 #else	/* _LITTLE_ENDIAN */
14989 	bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
14990 #endif	/* _LITTLE_ENDIAN */
14991 
14992 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14993 	    sata_device->satadev_addr.cport)->cport_mutex);
14994 
14995 	ap_info[SATA_ID_FW_LEN] = '\0';
14996 
14997 	info_len = strlen(ap_info) + 1;
14998 
14999 	if (ioc->get_size) {
15000 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
15001 		    mode) != 0)
15002 			return (EFAULT);
15003 	} else {
15004 		if (ioc->bufsiz < info_len)
15005 			return (EINVAL);
15006 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
15007 		    mode) != 0)
15008 			return (EFAULT);
15009 	}
15010 	return (0);
15011 }
15012 
15013 
15014 /*
15015  * Process ioctl get device serial number info request.
15016  * This operation should return to cfgadm the device serial number string.
15017  *
15018  * NOTE: Port multiplier is supported.
15019  */
15020 static	int
15021 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst,
15022     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
15023 {
15024 	sata_drive_info_t *sdinfo;
15025 	uint32_t info_len;
15026 	char ap_info[SATA_ID_SERIAL_LEN + 1];
15027 
15028 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15029 	    sata_device->satadev_addr.cport)->cport_mutex);
15030 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
15031 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
15032 		    sata_device->satadev_addr.cport);
15033 	else /* port multiplier */
15034 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
15035 		    sata_device->satadev_addr.cport,
15036 		    sata_device->satadev_addr.pmport);
15037 	if (sdinfo == NULL) {
15038 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15039 		    sata_device->satadev_addr.cport)->cport_mutex);
15040 		return (EINVAL);
15041 	}
15042 
15043 #ifdef	_LITTLE_ENDIAN
15044 	swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
15045 #else	/* _LITTLE_ENDIAN */
15046 	bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
15047 #endif	/* _LITTLE_ENDIAN */
15048 
15049 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15050 	    sata_device->satadev_addr.cport)->cport_mutex);
15051 
15052 	ap_info[SATA_ID_SERIAL_LEN] = '\0';
15053 
15054 	info_len = strlen(ap_info) + 1;
15055 
15056 	if (ioc->get_size) {
15057 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
15058 		    mode) != 0)
15059 			return (EFAULT);
15060 	} else {
15061 		if (ioc->bufsiz < info_len)
15062 			return (EINVAL);
15063 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
15064 		    mode) != 0)
15065 			return (EFAULT);
15066 	}
15067 	return (0);
15068 }
15069 
15070 
15071 /*
15072  * Preset scsi extended sense data (to NO SENSE)
15073  * First 18 bytes of the sense data are preset to current valid sense
15074  * with a key NO SENSE data.
15075  *
15076  * Returns void
15077  */
15078 static void
15079 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense)
15080 {
15081 	sense->es_valid = 1;		/* Valid sense */
15082 	sense->es_class = CLASS_EXTENDED_SENSE;	/* 0x70 - current err */
15083 	sense->es_key = KEY_NO_SENSE;
15084 	sense->es_info_1 = 0;
15085 	sense->es_info_2 = 0;
15086 	sense->es_info_3 = 0;
15087 	sense->es_info_4 = 0;
15088 	sense->es_add_len = 10;	/* Additional length - replace with a def */
15089 	sense->es_cmd_info[0] = 0;
15090 	sense->es_cmd_info[1] = 0;
15091 	sense->es_cmd_info[2] = 0;
15092 	sense->es_cmd_info[3] = 0;
15093 	sense->es_add_code = 0;
15094 	sense->es_qual_code = 0;
15095 }
15096 
15097 /*
15098  * Register a legacy cmdk-style devid for the target (disk) device.
15099  *
15100  * Note: This function is called only when the HBA devinfo node has the
15101  * property "use-cmdk-devid-format" set. This property indicates that
15102  * devid compatible with old cmdk (target) driver is to be generated
15103  * for any target device attached to this controller. This will take
15104  * precedence over the devid generated by sd (target) driver.
15105  * This function is derived from cmdk_devid_setup() function in cmdk.c.
15106  */
15107 static void
15108 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo)
15109 {
15110 	char	*hwid;
15111 	int	modlen;
15112 	int	serlen;
15113 	int	rval;
15114 	ddi_devid_t	devid;
15115 
15116 	/*
15117 	 * device ID is a concatanation of model number, "=", serial number.
15118 	 */
15119 	hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP);
15120 	bcopy(&sdinfo->satadrv_id.ai_model, hwid,
15121 	    sizeof (sdinfo->satadrv_id.ai_model));
15122 	swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model));
15123 	modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model));
15124 	if (modlen == 0)
15125 		goto err;
15126 	hwid[modlen++] = '=';
15127 	bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen],
15128 	    sizeof (sdinfo->satadrv_id.ai_drvser));
15129 	swab(&hwid[modlen], &hwid[modlen],
15130 	    sizeof (sdinfo->satadrv_id.ai_drvser));
15131 	serlen = sata_check_modser(&hwid[modlen],
15132 	    sizeof (sdinfo->satadrv_id.ai_drvser));
15133 	if (serlen == 0)
15134 		goto err;
15135 	hwid[modlen + serlen] = 0; /* terminate the hwid string */
15136 
15137 	/* initialize/register devid */
15138 	if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL,
15139 	    (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS) {
15140 		rval = ddi_devid_register(dip, devid);
15141 		/*
15142 		 * Free up the allocated devid buffer.
15143 		 * NOTE: This doesn't mean unregistering devid.
15144 		 */
15145 		ddi_devid_free(devid);
15146 	}
15147 
15148 	if (rval != DDI_SUCCESS)
15149 		cmn_err(CE_WARN, "sata: failed to create devid for the disk"
15150 		    " on port %d", sdinfo->satadrv_addr.cport);
15151 err:
15152 	kmem_free(hwid, LEGACY_HWID_LEN);
15153 }
15154 
15155 /*
15156  * valid model/serial string must contain a non-zero non-space characters.
15157  * trim trailing spaces/NULLs.
15158  */
15159 static int
15160 sata_check_modser(char *buf, int buf_len)
15161 {
15162 	boolean_t ret;
15163 	char *s;
15164 	int i;
15165 	int tb;
15166 	char ch;
15167 
15168 	ret = B_FALSE;
15169 	s = buf;
15170 	for (i = 0; i < buf_len; i++) {
15171 		ch = *s++;
15172 		if (ch != ' ' && ch != '\0')
15173 			tb = i + 1;
15174 		if (ch != ' ' && ch != '\0' && ch != '0')
15175 			ret = B_TRUE;
15176 	}
15177 
15178 	if (ret == B_FALSE)
15179 		return (0); /* invalid string */
15180 
15181 	return (tb); /* return length */
15182 }
15183 
15184 /*
15185  * sata_set_drive_features function compares current device features setting
15186  * with the saved device features settings and, if there is a difference,
15187  * it restores device features setting to the previously saved state.
15188  * It also arbitrarily tries to select the highest supported DMA mode.
15189  * Device Identify or Identify Packet Device data has to be current.
15190  * At the moment read ahead and write cache are considered for all devices.
15191  * For atapi devices, Removable Media Status Notification is set in addition
15192  * to common features.
15193  *
15194  * This function cannot be called in the interrupt context (it may sleep).
15195  *
15196  * The input argument sdinfo should point to the drive info structure
15197  * to be updated after features are set. Note, that only
15198  * device (packet) identify data is updated, not the flags indicating the
15199  * supported features.
15200  *
15201  * Returns SATA_SUCCESS if successful or there was nothing to do.
15202  * Device Identify data in the drive info structure pointed to by the sdinfo
15203  * arguments is updated even when no features were set or changed.
15204  *
15205  * Returns SATA_FAILURE if device features could not be set or DMA mode
15206  * for a disk cannot be set and device identify data cannot be fetched.
15207  *
15208  * Returns SATA_RETRY if device features could not be set (other than disk
15209  * DMA mode) but the device identify data was fetched successfully.
15210  *
15211  * Note: This function may fail the port, making it inaccessible.
15212  * In such case the explicit port disconnect/connect or physical device
15213  * detach/attach is required to re-evaluate port state again.
15214  */
15215 
15216 static int
15217 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst,
15218     sata_drive_info_t *sdinfo, int restore)
15219 {
15220 	int rval = SATA_SUCCESS;
15221 	int rval_set;
15222 	sata_drive_info_t new_sdinfo;
15223 	char *finfo = "sata_set_drive_features: cannot";
15224 	char *finfox;
15225 	int cache_op;
15226 
15227 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
15228 	new_sdinfo.satadrv_addr = sdinfo->satadrv_addr;
15229 	new_sdinfo.satadrv_type = sdinfo->satadrv_type;
15230 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
15231 		/*
15232 		 * Cannot get device identification - caller may retry later
15233 		 */
15234 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
15235 		    "%s fetch device identify data\n", finfo);
15236 		return (SATA_FAILURE);
15237 	}
15238 	finfox = (restore != 0) ? " restore device features" :
15239 	    " initialize device features\n";
15240 
15241 	switch (sdinfo->satadrv_type) {
15242 	case SATA_DTYPE_ATADISK:
15243 		/* Arbitrarily set UDMA mode */
15244 		if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
15245 		    SATA_SUCCESS) {
15246 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15247 			    "%s set UDMA mode\n", finfo));
15248 			return (SATA_FAILURE);
15249 		}
15250 		break;
15251 	case SATA_DTYPE_ATAPICD:
15252 	case SATA_DTYPE_ATAPITAPE:
15253 	case SATA_DTYPE_ATAPIDISK:
15254 		/*  Set Removable Media Status Notification, if necessary */
15255 		if (SATA_RM_NOTIFIC_SUPPORTED(new_sdinfo.satadrv_id) &&
15256 		    restore != 0) {
15257 			if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) &&
15258 			    (!SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id)))||
15259 			    ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) &&
15260 			    SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id))) {
15261 				/* Current setting does not match saved one */
15262 				if (sata_set_rmsn(sata_hba_inst, sdinfo,
15263 				    sdinfo->satadrv_settings &
15264 				    SATA_DEV_RMSN) != SATA_SUCCESS)
15265 					rval = SATA_FAILURE;
15266 			}
15267 		}
15268 		/*
15269 		 * We have to set Multiword DMA or UDMA, if it is supported, as
15270 		 * we want to use DMA transfer mode whenever possible.
15271 		 * Some devices require explicit setting of the DMA mode.
15272 		 */
15273 		if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) {
15274 			/* Set highest supported DMA mode */
15275 			if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
15276 			    SATA_SUCCESS) {
15277 				SATA_LOG_D((sata_hba_inst, CE_WARN,
15278 				    "%s set UDMA mode\n", finfo));
15279 				rval = SATA_FAILURE;
15280 			}
15281 		}
15282 		break;
15283 	}
15284 
15285 	if (!SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id) &&
15286 	    !SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
15287 		/*
15288 		 * neither READ AHEAD nor WRITE CACHE is supported
15289 		 * - do nothing
15290 		 */
15291 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
15292 		    "settable features not supported\n", NULL);
15293 		goto update_sdinfo;
15294 	}
15295 
15296 	if ((SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id) &&
15297 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) &&
15298 	    (SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id) &&
15299 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
15300 		/*
15301 		 * both READ AHEAD and WRITE CACHE are enabled
15302 		 * - Nothing to do
15303 		 */
15304 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
15305 		    "no device features to set\n", NULL);
15306 		goto update_sdinfo;
15307 	}
15308 
15309 	cache_op = 0;
15310 
15311 	if (SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id)) {
15312 		if ((sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
15313 		    !SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
15314 			/* Enable read ahead / read cache */
15315 			cache_op = SATAC_SF_ENABLE_READ_AHEAD;
15316 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
15317 			    "enabling read cache\n", NULL);
15318 		} else if (!(sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
15319 		    SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
15320 			/* Disable read ahead  / read cache */
15321 			cache_op = SATAC_SF_DISABLE_READ_AHEAD;
15322 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
15323 			    "disabling read cache\n", NULL);
15324 		}
15325 
15326 		if (cache_op != 0) {
15327 			/* Try to set read cache mode */
15328 			rval_set = sata_set_cache_mode(sata_hba_inst,
15329 			    &new_sdinfo, cache_op);
15330 			if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
15331 				rval = rval_set;
15332 		}
15333 	}
15334 
15335 	cache_op = 0;
15336 
15337 	if (SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
15338 		if ((sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
15339 		    !SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
15340 			/* Enable write cache */
15341 			cache_op = SATAC_SF_ENABLE_WRITE_CACHE;
15342 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
15343 			    "enabling write cache\n", NULL);
15344 		} else if (!(sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
15345 		    SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
15346 			/* Disable write cache */
15347 			cache_op = SATAC_SF_DISABLE_WRITE_CACHE;
15348 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
15349 			    "disabling write cache\n", NULL);
15350 		}
15351 
15352 		if (cache_op != 0) {
15353 			/* Try to set write cache mode */
15354 			rval_set = sata_set_cache_mode(sata_hba_inst,
15355 			    &new_sdinfo, cache_op);
15356 			if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
15357 				rval = rval_set;
15358 		}
15359 	}
15360 	if (rval != SATA_SUCCESS)
15361 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15362 		    "%s %s", finfo, finfox));
15363 
15364 update_sdinfo:
15365 	/*
15366 	 * We need to fetch Device Identify data again
15367 	 */
15368 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
15369 		/*
15370 		 * Cannot get device identification - retry later
15371 		 */
15372 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15373 		    "%s re-fetch device identify data\n", finfo));
15374 		rval = SATA_FAILURE;
15375 	}
15376 	/* Copy device sata info. */
15377 	sdinfo->satadrv_id = new_sdinfo.satadrv_id;
15378 
15379 	return (rval);
15380 }
15381 
15382 
15383 /*
15384  *
15385  * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if
15386  * unable to determine.
15387  *
15388  * Cannot be called in an interrupt context.
15389  *
15390  * Called by sata_build_lsense_page_2f()
15391  */
15392 
15393 static int
15394 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst,
15395     sata_drive_info_t *sdinfo)
15396 {
15397 	sata_pkt_t *spkt;
15398 	sata_cmd_t *scmd;
15399 	sata_pkt_txlate_t *spx;
15400 	int rval;
15401 
15402 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
15403 	spx->txlt_sata_hba_inst = sata_hba_inst;
15404 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
15405 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
15406 	if (spkt == NULL) {
15407 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
15408 		return (-1);
15409 	}
15410 	/* address is needed now */
15411 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
15412 
15413 
15414 	/* Fill sata_pkt */
15415 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
15416 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
15417 	/* Synchronous mode, no callback */
15418 	spkt->satapkt_comp = NULL;
15419 	/* Timeout 30s */
15420 	spkt->satapkt_time = sata_default_pkt_time;
15421 
15422 	scmd = &spkt->satapkt_cmd;
15423 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
15424 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
15425 
15426 	/* Set up which registers need to be returned */
15427 	scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE;
15428 	scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE;
15429 
15430 	/* Build SMART_RETURN_STATUS cmd in the sata_pkt */
15431 	scmd->satacmd_addr_type = 0;		/* N/A */
15432 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
15433 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
15434 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
15435 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
15436 	scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS;
15437 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
15438 	scmd->satacmd_cmd_reg = SATAC_SMART;
15439 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
15440 	    sdinfo->satadrv_addr.cport)));
15441 
15442 
15443 	/* Send pkt to SATA HBA driver */
15444 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
15445 	    SATA_TRAN_ACCEPTED ||
15446 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
15447 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
15448 		    sdinfo->satadrv_addr.cport)));
15449 		/*
15450 		 * Whoops, no SMART RETURN STATUS
15451 		 */
15452 		rval = -1;
15453 	} else {
15454 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
15455 		    sdinfo->satadrv_addr.cport)));
15456 		if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
15457 			rval = -1;
15458 			goto fail;
15459 		}
15460 		if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
15461 			rval = -1;
15462 			goto fail;
15463 		}
15464 		if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) &&
15465 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2))
15466 			rval = 0;
15467 		else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) &&
15468 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4))
15469 			rval = 1;
15470 		else {
15471 			rval = -1;
15472 			goto fail;
15473 		}
15474 	}
15475 fail:
15476 	/* Free allocated resources */
15477 	sata_pkt_free(spx);
15478 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
15479 
15480 	return (rval);
15481 }
15482 
15483 /*
15484  *
15485  * Returns 0 if succeeded, -1 otherwise
15486  *
15487  * Cannot be called in an interrupt context.
15488  *
15489  */
15490 static int
15491 sata_fetch_smart_data(
15492 	sata_hba_inst_t *sata_hba_inst,
15493 	sata_drive_info_t *sdinfo,
15494 	struct smart_data *smart_data)
15495 {
15496 	sata_pkt_t *spkt;
15497 	sata_cmd_t *scmd;
15498 	sata_pkt_txlate_t *spx;
15499 	int rval;
15500 
15501 #if ! defined(lint)
15502 	ASSERT(sizeof (struct smart_data) == 512);
15503 #endif
15504 
15505 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
15506 	spx->txlt_sata_hba_inst = sata_hba_inst;
15507 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
15508 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
15509 	if (spkt == NULL) {
15510 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
15511 		return (-1);
15512 	}
15513 	/* address is needed now */
15514 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
15515 
15516 
15517 	/* Fill sata_pkt */
15518 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
15519 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
15520 	/* Synchronous mode, no callback */
15521 	spkt->satapkt_comp = NULL;
15522 	/* Timeout 30s */
15523 	spkt->satapkt_time = sata_default_pkt_time;
15524 
15525 	scmd = &spkt->satapkt_cmd;
15526 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
15527 
15528 	/*
15529 	 * Allocate buffer for SMART data
15530 	 */
15531 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
15532 	    sizeof (struct smart_data));
15533 	if (scmd->satacmd_bp == NULL) {
15534 		sata_pkt_free(spx);
15535 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
15536 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15537 		    "sata_fetch_smart_data: "
15538 		    "cannot allocate buffer"));
15539 		return (-1);
15540 	}
15541 
15542 
15543 	/* Build SMART_READ_DATA cmd in the sata_pkt */
15544 	scmd->satacmd_addr_type = 0;		/* N/A */
15545 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
15546 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
15547 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
15548 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
15549 	scmd->satacmd_features_reg = SATA_SMART_READ_DATA;
15550 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
15551 	scmd->satacmd_cmd_reg = SATAC_SMART;
15552 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
15553 	    sdinfo->satadrv_addr.cport)));
15554 
15555 	/* Send pkt to SATA HBA driver */
15556 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
15557 	    SATA_TRAN_ACCEPTED ||
15558 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
15559 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
15560 		    sdinfo->satadrv_addr.cport)));
15561 		/*
15562 		 * Whoops, no SMART DATA available
15563 		 */
15564 		rval = -1;
15565 		goto fail;
15566 	} else {
15567 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
15568 		    sdinfo->satadrv_addr.cport)));
15569 		if (spx->txlt_buf_dma_handle != NULL) {
15570 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
15571 			    DDI_DMA_SYNC_FORKERNEL);
15572 			ASSERT(rval == DDI_SUCCESS);
15573 		}
15574 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data,
15575 		    sizeof (struct smart_data));
15576 	}
15577 
15578 fail:
15579 	/* Free allocated resources */
15580 	sata_free_local_buffer(spx);
15581 	sata_pkt_free(spx);
15582 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
15583 
15584 	return (rval);
15585 }
15586 
15587 /*
15588  * Used by LOG SENSE page 0x10
15589  * Reads (in synchronous mode) the self test log data using Read Log Ext cmd.
15590  * Note: cannot be called in the interrupt context.
15591  *
15592  * return 0 for success, -1 otherwise
15593  *
15594  */
15595 static int
15596 sata_ext_smart_selftest_read_log(
15597 	sata_hba_inst_t *sata_hba_inst,
15598 	sata_drive_info_t *sdinfo,
15599 	struct smart_ext_selftest_log *ext_selftest_log,
15600 	uint16_t block_num)
15601 {
15602 	sata_pkt_txlate_t *spx;
15603 	sata_pkt_t *spkt;
15604 	sata_cmd_t *scmd;
15605 	int rval;
15606 
15607 #if ! defined(lint)
15608 	ASSERT(sizeof (struct smart_ext_selftest_log) == 512);
15609 #endif
15610 
15611 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
15612 	spx->txlt_sata_hba_inst = sata_hba_inst;
15613 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
15614 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
15615 	if (spkt == NULL) {
15616 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
15617 		return (-1);
15618 	}
15619 	/* address is needed now */
15620 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
15621 
15622 
15623 	/* Fill sata_pkt */
15624 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
15625 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
15626 	/* Synchronous mode, no callback */
15627 	spkt->satapkt_comp = NULL;
15628 	/* Timeout 30s */
15629 	spkt->satapkt_time = sata_default_pkt_time;
15630 
15631 	scmd = &spkt->satapkt_cmd;
15632 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
15633 
15634 	/*
15635 	 * Allocate buffer for SMART extended self-test log
15636 	 */
15637 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
15638 	    sizeof (struct smart_ext_selftest_log));
15639 	if (scmd->satacmd_bp == NULL) {
15640 		sata_pkt_free(spx);
15641 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
15642 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15643 		    "sata_ext_smart_selftest_log: "
15644 		    "cannot allocate buffer"));
15645 		return (-1);
15646 	}
15647 
15648 	/* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */
15649 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
15650 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of selftest log */
15651 	scmd->satacmd_sec_count_msb = 0;	/* One sector of selftest log */
15652 	scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE;
15653 	scmd->satacmd_lba_low_msb = 0;
15654 	scmd->satacmd_lba_mid_lsb = block_num & 0xff;
15655 	scmd->satacmd_lba_mid_msb = block_num >> 8;
15656 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
15657 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
15658 
15659 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
15660 	    sdinfo->satadrv_addr.cport)));
15661 
15662 	/* Send pkt to SATA HBA driver */
15663 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
15664 	    SATA_TRAN_ACCEPTED ||
15665 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
15666 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
15667 		    sdinfo->satadrv_addr.cport)));
15668 
15669 		/*
15670 		 * Whoops, no SMART selftest log info available
15671 		 */
15672 		rval = -1;
15673 		goto fail;
15674 	} else {
15675 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
15676 		    sdinfo->satadrv_addr.cport)));
15677 
15678 		if (spx->txlt_buf_dma_handle != NULL) {
15679 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
15680 			    DDI_DMA_SYNC_FORKERNEL);
15681 			ASSERT(rval == DDI_SUCCESS);
15682 		}
15683 		bcopy(scmd->satacmd_bp->b_un.b_addr,
15684 		    (uint8_t *)ext_selftest_log,
15685 		    sizeof (struct smart_ext_selftest_log));
15686 		rval = 0;
15687 	}
15688 
15689 fail:
15690 	/* Free allocated resources */
15691 	sata_free_local_buffer(spx);
15692 	sata_pkt_free(spx);
15693 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
15694 
15695 	return (rval);
15696 }
15697 
15698 /*
15699  * Returns 0 for success, -1 otherwise
15700  *
15701  * SMART self-test log data is returned in buffer pointed to by selftest_log
15702  */
15703 static int
15704 sata_smart_selftest_log(
15705 	sata_hba_inst_t *sata_hba_inst,
15706 	sata_drive_info_t *sdinfo,
15707 	struct smart_selftest_log *selftest_log)
15708 {
15709 	sata_pkt_t *spkt;
15710 	sata_cmd_t *scmd;
15711 	sata_pkt_txlate_t *spx;
15712 	int rval;
15713 
15714 #if ! defined(lint)
15715 	ASSERT(sizeof (struct smart_selftest_log) == 512);
15716 #endif
15717 
15718 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
15719 	spx->txlt_sata_hba_inst = sata_hba_inst;
15720 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
15721 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
15722 	if (spkt == NULL) {
15723 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
15724 		return (-1);
15725 	}
15726 	/* address is needed now */
15727 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
15728 
15729 
15730 	/* Fill sata_pkt */
15731 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
15732 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
15733 	/* Synchronous mode, no callback */
15734 	spkt->satapkt_comp = NULL;
15735 	/* Timeout 30s */
15736 	spkt->satapkt_time = sata_default_pkt_time;
15737 
15738 	scmd = &spkt->satapkt_cmd;
15739 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
15740 
15741 	/*
15742 	 * Allocate buffer for SMART SELFTEST LOG
15743 	 */
15744 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
15745 	    sizeof (struct smart_selftest_log));
15746 	if (scmd->satacmd_bp == NULL) {
15747 		sata_pkt_free(spx);
15748 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
15749 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15750 		    "sata_smart_selftest_log: "
15751 		    "cannot allocate buffer"));
15752 		return (-1);
15753 	}
15754 
15755 	/* Build SMART_READ_LOG cmd in the sata_pkt */
15756 	scmd->satacmd_addr_type = 0;		/* N/A */
15757 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of SMART log */
15758 	scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE;
15759 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
15760 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
15761 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
15762 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
15763 	scmd->satacmd_cmd_reg = SATAC_SMART;
15764 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
15765 	    sdinfo->satadrv_addr.cport)));
15766 
15767 	/* Send pkt to SATA HBA driver */
15768 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
15769 	    SATA_TRAN_ACCEPTED ||
15770 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
15771 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
15772 		    sdinfo->satadrv_addr.cport)));
15773 		/*
15774 		 * Whoops, no SMART DATA available
15775 		 */
15776 		rval = -1;
15777 		goto fail;
15778 	} else {
15779 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
15780 		    sdinfo->satadrv_addr.cport)));
15781 		if (spx->txlt_buf_dma_handle != NULL) {
15782 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
15783 			    DDI_DMA_SYNC_FORKERNEL);
15784 			ASSERT(rval == DDI_SUCCESS);
15785 		}
15786 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log,
15787 		    sizeof (struct smart_selftest_log));
15788 		rval = 0;
15789 	}
15790 
15791 fail:
15792 	/* Free allocated resources */
15793 	sata_free_local_buffer(spx);
15794 	sata_pkt_free(spx);
15795 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
15796 
15797 	return (rval);
15798 }
15799 
15800 
15801 /*
15802  * Returns 0 for success, -1 otherwise
15803  *
15804  * SMART READ LOG data is returned in buffer pointed to by smart_log
15805  */
15806 static int
15807 sata_smart_read_log(
15808 	sata_hba_inst_t *sata_hba_inst,
15809 	sata_drive_info_t *sdinfo,
15810 	uint8_t *smart_log,		/* where the data should be returned */
15811 	uint8_t which_log,		/* which log should be returned */
15812 	uint8_t log_size)		/* # of 512 bytes in log */
15813 {
15814 	sata_pkt_t *spkt;
15815 	sata_cmd_t *scmd;
15816 	sata_pkt_txlate_t *spx;
15817 	int rval;
15818 
15819 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
15820 	spx->txlt_sata_hba_inst = sata_hba_inst;
15821 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
15822 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
15823 	if (spkt == NULL) {
15824 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
15825 		return (-1);
15826 	}
15827 	/* address is needed now */
15828 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
15829 
15830 
15831 	/* Fill sata_pkt */
15832 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
15833 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
15834 	/* Synchronous mode, no callback */
15835 	spkt->satapkt_comp = NULL;
15836 	/* Timeout 30s */
15837 	spkt->satapkt_time = sata_default_pkt_time;
15838 
15839 	scmd = &spkt->satapkt_cmd;
15840 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
15841 
15842 	/*
15843 	 * Allocate buffer for SMART READ LOG
15844 	 */
15845 	scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512);
15846 	if (scmd->satacmd_bp == NULL) {
15847 		sata_pkt_free(spx);
15848 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
15849 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15850 		    "sata_smart_read_log: " "cannot allocate buffer"));
15851 		return (-1);
15852 	}
15853 
15854 	/* Build SMART_READ_LOG cmd in the sata_pkt */
15855 	scmd->satacmd_addr_type = 0;		/* N/A */
15856 	scmd->satacmd_sec_count_lsb = log_size;	/* what the caller asked for */
15857 	scmd->satacmd_lba_low_lsb = which_log;	/* which log page */
15858 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
15859 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
15860 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
15861 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
15862 	scmd->satacmd_cmd_reg = SATAC_SMART;
15863 
15864 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
15865 	    sdinfo->satadrv_addr.cport)));
15866 
15867 	/* Send pkt to SATA HBA driver */
15868 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
15869 	    SATA_TRAN_ACCEPTED ||
15870 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
15871 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
15872 		    sdinfo->satadrv_addr.cport)));
15873 
15874 		/*
15875 		 * Whoops, no SMART DATA available
15876 		 */
15877 		rval = -1;
15878 		goto fail;
15879 	} else {
15880 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
15881 		    sdinfo->satadrv_addr.cport)));
15882 
15883 		if (spx->txlt_buf_dma_handle != NULL) {
15884 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
15885 			    DDI_DMA_SYNC_FORKERNEL);
15886 			ASSERT(rval == DDI_SUCCESS);
15887 		}
15888 		bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512);
15889 		rval = 0;
15890 	}
15891 
15892 fail:
15893 	/* Free allocated resources */
15894 	sata_free_local_buffer(spx);
15895 	sata_pkt_free(spx);
15896 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
15897 
15898 	return (rval);
15899 }
15900 
15901 /*
15902  * Used by LOG SENSE page 0x10
15903  *
15904  * return 0 for success, -1 otherwise
15905  *
15906  */
15907 static int
15908 sata_read_log_ext_directory(
15909 	sata_hba_inst_t *sata_hba_inst,
15910 	sata_drive_info_t *sdinfo,
15911 	struct read_log_ext_directory *logdir)
15912 {
15913 	sata_pkt_txlate_t *spx;
15914 	sata_pkt_t *spkt;
15915 	sata_cmd_t *scmd;
15916 	int rval;
15917 
15918 #if ! defined(lint)
15919 	ASSERT(sizeof (struct read_log_ext_directory) == 512);
15920 #endif
15921 
15922 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
15923 	spx->txlt_sata_hba_inst = sata_hba_inst;
15924 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
15925 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
15926 	if (spkt == NULL) {
15927 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
15928 		return (-1);
15929 	}
15930 
15931 	/* Fill sata_pkt */
15932 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
15933 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
15934 	/* Synchronous mode, no callback */
15935 	spkt->satapkt_comp = NULL;
15936 	/* Timeout 30s */
15937 	spkt->satapkt_time = sata_default_pkt_time;
15938 
15939 	scmd = &spkt->satapkt_cmd;
15940 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
15941 
15942 	/*
15943 	 * Allocate buffer for SMART READ LOG EXTENDED command
15944 	 */
15945 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
15946 	    sizeof (struct read_log_ext_directory));
15947 	if (scmd->satacmd_bp == NULL) {
15948 		sata_pkt_free(spx);
15949 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
15950 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15951 		    "sata_read_log_ext_directory: "
15952 		    "cannot allocate buffer"));
15953 		return (-1);
15954 	}
15955 
15956 	/* Build READ LOG EXT w/ log directory cmd in the  sata_pkt */
15957 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
15958 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of directory */
15959 	scmd->satacmd_sec_count_msb = 0;	/* One sector of directory */
15960 	scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY;
15961 	scmd->satacmd_lba_low_msb = 0;
15962 	scmd->satacmd_lba_mid_lsb = 0;
15963 	scmd->satacmd_lba_mid_msb = 0;
15964 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
15965 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
15966 
15967 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
15968 	    sdinfo->satadrv_addr.cport)));
15969 
15970 	/* Send pkt to SATA HBA driver */
15971 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
15972 	    SATA_TRAN_ACCEPTED ||
15973 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
15974 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
15975 		    sdinfo->satadrv_addr.cport)));
15976 		/*
15977 		 * Whoops, no SMART selftest log info available
15978 		 */
15979 		rval = -1;
15980 		goto fail;
15981 	} else {
15982 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
15983 		    sdinfo->satadrv_addr.cport)));
15984 		if (spx->txlt_buf_dma_handle != NULL) {
15985 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
15986 			    DDI_DMA_SYNC_FORKERNEL);
15987 			ASSERT(rval == DDI_SUCCESS);
15988 		}
15989 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir,
15990 		    sizeof (struct read_log_ext_directory));
15991 		rval = 0;
15992 	}
15993 
15994 fail:
15995 	/* Free allocated resources */
15996 	sata_free_local_buffer(spx);
15997 	sata_pkt_free(spx);
15998 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
15999 
16000 	return (rval);
16001 }
16002 
16003 /*
16004  * Set up error retrieval sata command for NCQ command error data
16005  * recovery.
16006  *
16007  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
16008  * returns SATA_FAILURE otherwise.
16009  */
16010 static int
16011 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
16012 {
16013 #ifndef __lock_lint
16014 	_NOTE(ARGUNUSED(sdinfo))
16015 #endif
16016 
16017 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
16018 	sata_cmd_t *scmd;
16019 	struct buf *bp;
16020 
16021 	/* Operation modes are up to the caller */
16022 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
16023 
16024 	/* Synchronous mode, no callback - may be changed by the caller */
16025 	spkt->satapkt_comp = NULL;
16026 	spkt->satapkt_time = sata_default_pkt_time;
16027 
16028 	scmd = &spkt->satapkt_cmd;
16029 	bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t));
16030 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
16031 
16032 	/*
16033 	 * Allocate dma_able buffer error data.
16034 	 * Buffer allocation will take care of buffer alignment and other DMA
16035 	 * attributes.
16036 	 */
16037 	bp = sata_alloc_local_buffer(spx,
16038 	    sizeof (struct sata_ncq_error_recovery_page));
16039 	if (bp == NULL)
16040 		return (SATA_FAILURE);
16041 
16042 	bp_mapin(bp); /* make data buffer accessible */
16043 	scmd->satacmd_bp = bp;
16044 
16045 	/*
16046 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
16047 	 * before accessing it. Handle is in usual place in translate struct.
16048 	 */
16049 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
16050 
16051 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
16052 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
16053 
16054 	return (SATA_SUCCESS);
16055 }
16056 
16057 /*
16058  * sata_xlate_errors() is used to translate (S)ATA error
16059  * information to SCSI information returned in the SCSI
16060  * packet.
16061  */
16062 static void
16063 sata_xlate_errors(sata_pkt_txlate_t *spx)
16064 {
16065 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
16066 	struct scsi_extended_sense *sense;
16067 
16068 	scsipkt->pkt_reason = CMD_INCOMPLETE;
16069 	*scsipkt->pkt_scbp = STATUS_CHECK;
16070 	sense = sata_arq_sense(spx);
16071 
16072 	switch (spx->txlt_sata_pkt->satapkt_reason) {
16073 	case SATA_PKT_PORT_ERROR:
16074 		/*
16075 		 * We have no device data. Assume no data transfered.
16076 		 */
16077 		sense->es_key = KEY_HARDWARE_ERROR;
16078 		break;
16079 
16080 	case SATA_PKT_DEV_ERROR:
16081 		if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
16082 		    SATA_STATUS_ERR) {
16083 			/*
16084 			 * determine dev error reason from error
16085 			 * reg content
16086 			 */
16087 			sata_decode_device_error(spx, sense);
16088 			break;
16089 		}
16090 		/* No extended sense key - no info available */
16091 		break;
16092 
16093 	case SATA_PKT_TIMEOUT:
16094 		scsipkt->pkt_reason = CMD_TIMEOUT;
16095 		scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET;
16096 		/* No extended sense key */
16097 		break;
16098 
16099 	case SATA_PKT_ABORTED:
16100 		scsipkt->pkt_reason = CMD_ABORTED;
16101 		scsipkt->pkt_statistics |= STAT_ABORTED;
16102 		/* No extended sense key */
16103 		break;
16104 
16105 	case SATA_PKT_RESET:
16106 		/*
16107 		 * pkt aborted either by an explicit reset request from
16108 		 * a host, or due to error recovery
16109 		 */
16110 		scsipkt->pkt_reason = CMD_RESET;
16111 		scsipkt->pkt_statistics |= STAT_DEV_RESET;
16112 		break;
16113 
16114 	default:
16115 		scsipkt->pkt_reason = CMD_TRAN_ERR;
16116 		break;
16117 	}
16118 }
16119 
16120 
16121 
16122 
16123 /*
16124  * Log sata message
16125  * dev pathname msg line preceeds the logged message.
16126  */
16127 
16128 static	void
16129 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...)
16130 {
16131 	char pathname[128];
16132 	dev_info_t *dip = NULL;
16133 	va_list ap;
16134 
16135 	mutex_enter(&sata_log_mutex);
16136 
16137 	va_start(ap, fmt);
16138 	(void) vsprintf(sata_log_buf, fmt, ap);
16139 	va_end(ap);
16140 
16141 	if (sata_hba_inst != NULL) {
16142 		dip = SATA_DIP(sata_hba_inst);
16143 		(void) ddi_pathname(dip, pathname);
16144 	} else {
16145 		pathname[0] = 0;
16146 	}
16147 	if (level == CE_CONT) {
16148 		if (sata_debug_flags == 0)
16149 			cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf);
16150 		else
16151 			cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf);
16152 	} else {
16153 		if (level != CE_NOTE) {
16154 			cmn_err(level, "%s:\n %s", pathname, sata_log_buf);
16155 		} else if (sata_msg) {
16156 			cmn_err(level, "%s:\n %s", pathname,
16157 			    sata_log_buf);
16158 		}
16159 	}
16160 
16161 	/* sata trace debug */
16162 	sata_trace_debug(dip, sata_log_buf);
16163 
16164 	mutex_exit(&sata_log_mutex);
16165 }
16166 
16167 
16168 /* ******** Asynchronous HBA events handling & hotplugging support ******** */
16169 
16170 /*
16171  * Start or terminate the thread, depending on flag arg and current state
16172  */
16173 static void
16174 sata_event_thread_control(int startstop)
16175 {
16176 	static 	int sata_event_thread_terminating = 0;
16177 	static 	int sata_event_thread_starting = 0;
16178 	int i;
16179 
16180 	mutex_enter(&sata_event_mutex);
16181 
16182 	if (startstop == 0 && (sata_event_thread_starting == 1 ||
16183 	    sata_event_thread_terminating == 1)) {
16184 		mutex_exit(&sata_event_mutex);
16185 		return;
16186 	}
16187 	if (startstop == 1 && sata_event_thread_starting == 1) {
16188 		mutex_exit(&sata_event_mutex);
16189 		return;
16190 	}
16191 	if (startstop == 1 && sata_event_thread_terminating == 1) {
16192 		sata_event_thread_starting = 1;
16193 		/* wait til terminate operation completes */
16194 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
16195 		while (sata_event_thread_terminating == 1) {
16196 			if (i-- <= 0) {
16197 				sata_event_thread_starting = 0;
16198 				mutex_exit(&sata_event_mutex);
16199 #ifdef SATA_DEBUG
16200 				cmn_err(CE_WARN, "sata_event_thread_control: "
16201 				    "timeout waiting for thread to terminate");
16202 #endif
16203 				return;
16204 			}
16205 			mutex_exit(&sata_event_mutex);
16206 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
16207 			mutex_enter(&sata_event_mutex);
16208 		}
16209 	}
16210 	if (startstop == 1) {
16211 		if (sata_event_thread == NULL) {
16212 			sata_event_thread = thread_create(NULL, 0,
16213 			    (void (*)())sata_event_daemon,
16214 			    &sata_hba_list, 0, &p0, TS_RUN, minclsyspri);
16215 		}
16216 		sata_event_thread_starting = 0;
16217 		mutex_exit(&sata_event_mutex);
16218 		return;
16219 	}
16220 
16221 	/*
16222 	 * If we got here, thread may need to be terminated
16223 	 */
16224 	if (sata_event_thread != NULL) {
16225 		int i;
16226 		/* Signal event thread to go away */
16227 		sata_event_thread_terminating = 1;
16228 		sata_event_thread_terminate = 1;
16229 		cv_signal(&sata_event_cv);
16230 		/*
16231 		 * Wait til daemon terminates.
16232 		 */
16233 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
16234 		while (sata_event_thread_terminate == 1) {
16235 			mutex_exit(&sata_event_mutex);
16236 			if (i-- <= 0) {
16237 				/* Daemon did not go away !!! */
16238 #ifdef SATA_DEBUG
16239 				cmn_err(CE_WARN, "sata_event_thread_control: "
16240 				    "cannot terminate event daemon thread");
16241 #endif
16242 				mutex_enter(&sata_event_mutex);
16243 				break;
16244 			}
16245 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
16246 			mutex_enter(&sata_event_mutex);
16247 		}
16248 		sata_event_thread_terminating = 0;
16249 	}
16250 	ASSERT(sata_event_thread_terminating == 0);
16251 	ASSERT(sata_event_thread_starting == 0);
16252 	mutex_exit(&sata_event_mutex);
16253 }
16254 
16255 
16256 /*
16257  * SATA HBA event notification function.
16258  * Events reported by SATA HBA drivers per HBA instance relate to a change in
16259  * a port and/or device state or a controller itself.
16260  * Events for different addresses/addr types cannot be combined.
16261  * A warning message is generated for each event type.
16262  * Events are not processed by this function, so only the
16263  * event flag(s)is set for an affected entity and the event thread is
16264  * waken up. Event daemon thread processes all events.
16265  *
16266  * NOTE: Since more than one event may be reported at the same time, one
16267  * cannot determine a sequence of events when opposite event are reported, eg.
16268  * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing
16269  * is taking precedence over reported events, i.e. may cause ignoring some
16270  * events.
16271  */
16272 #define	SATA_EVENT_MAX_MSG_LENGTH	79
16273 
16274 void
16275 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event)
16276 {
16277 	sata_hba_inst_t *sata_hba_inst = NULL;
16278 	sata_address_t *saddr;
16279 	sata_pmult_info_t *pmultinfo;
16280 	sata_drive_info_t *sdinfo;
16281 	sata_port_stats_t *pstats;
16282 	sata_cport_info_t *cportinfo;
16283 	sata_pmport_info_t *pmportinfo;
16284 	int cport, pmport;
16285 	char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1];
16286 	char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1];
16287 	char *lcp;
16288 	static char *err_msg_evnt_1 =
16289 	    "sata_hba_event_notify: invalid port event 0x%x ";
16290 	static char *err_msg_evnt_2 =
16291 	    "sata_hba_event_notify: invalid device event 0x%x ";
16292 	int linkevent;
16293 
16294 	/*
16295 	 * There is a possibility that an event will be generated on HBA
16296 	 * that has not completed attachment or is detaching. We still want
16297 	 * to process events until HBA is detached.
16298 	 */
16299 	mutex_enter(&sata_mutex);
16300 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
16301 	    sata_hba_inst = sata_hba_inst->satahba_next) {
16302 		if (SATA_DIP(sata_hba_inst) == dip)
16303 			if (sata_hba_inst->satahba_attached == 1)
16304 				break;
16305 	}
16306 	mutex_exit(&sata_mutex);
16307 	if (sata_hba_inst == NULL)
16308 		/* HBA not attached */
16309 		return;
16310 
16311 	ASSERT(sata_device != NULL);
16312 
16313 	/*
16314 	 * Validate address before - do not proceed with invalid address.
16315 	 */
16316 	saddr = &sata_device->satadev_addr;
16317 	if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst))
16318 		return;
16319 
16320 	cport = saddr->cport;
16321 	pmport = saddr->pmport;
16322 
16323 	buf1[0] = buf2[0] = '\0';
16324 
16325 	/*
16326 	 * If event relates to port or device, check port state.
16327 	 * Port has to be initialized, or we cannot accept an event.
16328 	 */
16329 	if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT |
16330 	    SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT | SATA_ADDR_PMULT)) != 0) {
16331 		mutex_enter(&sata_hba_inst->satahba_mutex);
16332 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
16333 		mutex_exit(&sata_hba_inst->satahba_mutex);
16334 		if (cportinfo == NULL || cportinfo->cport_state == 0)
16335 			return;
16336 	}
16337 
16338 	if ((saddr->qual & (SATA_ADDR_PMULT | SATA_ADDR_PMPORT |
16339 	    SATA_ADDR_DPMPORT)) != 0) {
16340 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
16341 			SATA_LOG_D((sata_hba_inst, CE_WARN,
16342 			    "sata_hba_event_notify: Non-pmult device (0x%x)"
16343 			    "is attached to port %d, ignore pmult/pmport "
16344 			    "event 0x%x", cportinfo->cport_dev_type,
16345 			    cport, event));
16346 			return;
16347 		}
16348 
16349 		mutex_enter(&cportinfo->cport_mutex);
16350 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
16351 		mutex_exit(&cportinfo->cport_mutex);
16352 
16353 		/*
16354 		 * The daemon might be processing attachment of port
16355 		 * multiplier, in that case we should ignore events on its
16356 		 * sub-devices.
16357 		 *
16358 		 * NOTE: Only pmult_state is checked in sata_hba_event_notify.
16359 		 * The pmport_state is checked by sata daemon.
16360 		 */
16361 		if (pmultinfo == NULL ||
16362 		    pmultinfo->pmult_state == SATA_STATE_UNKNOWN) {
16363 			SATA_LOG_D((sata_hba_inst, CE_WARN,
16364 			    "sata_hba_event_notify: pmult is not"
16365 			    "available at port %d:%d, ignore event 0x%x",
16366 			    cport, pmport, event));
16367 			return;
16368 		}
16369 	}
16370 
16371 	if ((saddr->qual &
16372 	    (SATA_ADDR_PMPORT | SATA_ADDR_DPMPORT)) != 0) {
16373 
16374 		mutex_enter(&cportinfo->cport_mutex);
16375 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) {
16376 			SATA_LOG_D((sata_hba_inst, CE_WARN,
16377 			    "sata_hba_event_notify: invalid/"
16378 			    "un-implemented port %d:%d (%d ports), "
16379 			    "ignore event 0x%x", cport, pmport,
16380 			    SATA_NUM_PMPORTS(sata_hba_inst, cport), event));
16381 			mutex_exit(&cportinfo->cport_mutex);
16382 			return;
16383 		}
16384 		mutex_exit(&cportinfo->cport_mutex);
16385 
16386 		mutex_enter(&sata_hba_inst->satahba_mutex);
16387 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
16388 		    cport, pmport);
16389 		mutex_exit(&sata_hba_inst->satahba_mutex);
16390 
16391 		/* pmport is implemented/valid? */
16392 		if (pmportinfo == NULL) {
16393 			SATA_LOG_D((sata_hba_inst, CE_WARN,
16394 			    "sata_hba_event_notify: invalid/"
16395 			    "un-implemented port %d:%d, ignore "
16396 			    "event 0x%x", cport, pmport, event));
16397 			return;
16398 		}
16399 	}
16400 
16401 	/*
16402 	 * Events refer to devices, ports and controllers - each has
16403 	 * unique address. Events for different addresses cannot be combined.
16404 	 */
16405 	if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) {
16406 
16407 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
16408 
16409 		/* qualify this event(s) */
16410 		if ((event & SATA_EVNT_PORT_EVENTS) == 0) {
16411 			/* Invalid event for the device port */
16412 			(void) sprintf(buf2, err_msg_evnt_1,
16413 			    event & SATA_EVNT_PORT_EVENTS);
16414 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
16415 			goto event_info;
16416 		}
16417 		if (saddr->qual == SATA_ADDR_CPORT) {
16418 			/* Controller's device port event */
16419 
16420 			(SATA_CPORT_INFO(sata_hba_inst, cport))->
16421 			    cport_event_flags |=
16422 			    event & SATA_EVNT_PORT_EVENTS;
16423 			pstats =
16424 			    &(SATA_CPORT_INFO(sata_hba_inst, cport))->
16425 			    cport_stats;
16426 		} else {
16427 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
16428 			mutex_enter(&pmportinfo->pmport_mutex);
16429 			/* Port multiplier's device port event */
16430 			(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
16431 			    pmport_event_flags |=
16432 			    event & SATA_EVNT_PORT_EVENTS;
16433 			pstats =
16434 			    &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
16435 			    pmport_stats;
16436 			mutex_exit(&pmportinfo->pmport_mutex);
16437 			mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
16438 		}
16439 
16440 		/*
16441 		 * Add to statistics and log the message. We have to do it
16442 		 * here rather than in the event daemon, because there may be
16443 		 * multiple events occuring before they are processed.
16444 		 */
16445 		linkevent = event &
16446 		    (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED);
16447 		if (linkevent) {
16448 			if (linkevent == (SATA_EVNT_LINK_LOST |
16449 			    SATA_EVNT_LINK_ESTABLISHED)) {
16450 				/* This is likely event combination */
16451 				(void) strlcat(buf1, "link lost/established, ",
16452 				    SATA_EVENT_MAX_MSG_LENGTH);
16453 
16454 				if (pstats->link_lost < 0xffffffffffffffffULL)
16455 					pstats->link_lost++;
16456 				if (pstats->link_established <
16457 				    0xffffffffffffffffULL)
16458 					pstats->link_established++;
16459 				linkevent = 0;
16460 			} else if (linkevent & SATA_EVNT_LINK_LOST) {
16461 				(void) strlcat(buf1, "link lost, ",
16462 				    SATA_EVENT_MAX_MSG_LENGTH);
16463 
16464 				if (pstats->link_lost < 0xffffffffffffffffULL)
16465 					pstats->link_lost++;
16466 			} else {
16467 				(void) strlcat(buf1, "link established, ",
16468 				    SATA_EVENT_MAX_MSG_LENGTH);
16469 				if (pstats->link_established <
16470 				    0xffffffffffffffffULL)
16471 					pstats->link_established++;
16472 			}
16473 		}
16474 		if (event & SATA_EVNT_DEVICE_ATTACHED) {
16475 			(void) strlcat(buf1, "device attached, ",
16476 			    SATA_EVENT_MAX_MSG_LENGTH);
16477 			if (pstats->device_attached < 0xffffffffffffffffULL)
16478 				pstats->device_attached++;
16479 		}
16480 		if (event & SATA_EVNT_DEVICE_DETACHED) {
16481 			(void) strlcat(buf1, "device detached, ",
16482 			    SATA_EVENT_MAX_MSG_LENGTH);
16483 			if (pstats->device_detached < 0xffffffffffffffffULL)
16484 				pstats->device_detached++;
16485 		}
16486 		if (event & SATA_EVNT_PWR_LEVEL_CHANGED) {
16487 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
16488 			    "port %d power level changed", cport);
16489 			if (pstats->port_pwr_changed < 0xffffffffffffffffULL)
16490 				pstats->port_pwr_changed++;
16491 		}
16492 
16493 		if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) {
16494 			/* There should be no other events for this address */
16495 			(void) sprintf(buf2, err_msg_evnt_1,
16496 			    event & ~SATA_EVNT_PORT_EVENTS);
16497 		}
16498 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
16499 
16500 	} else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) {
16501 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
16502 
16503 		/* qualify this event */
16504 		if ((event & SATA_EVNT_DEVICE_RESET) == 0) {
16505 			/* Invalid event for a device */
16506 			(void) sprintf(buf2, err_msg_evnt_2,
16507 			    event & SATA_EVNT_DEVICE_RESET);
16508 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
16509 			goto event_info;
16510 		}
16511 		/* drive event */
16512 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
16513 		if (sdinfo != NULL) {
16514 			if (event & SATA_EVNT_DEVICE_RESET) {
16515 				(void) strlcat(buf1, "device reset, ",
16516 				    SATA_EVENT_MAX_MSG_LENGTH);
16517 				if (sdinfo->satadrv_stats.drive_reset <
16518 				    0xffffffffffffffffULL)
16519 					sdinfo->satadrv_stats.drive_reset++;
16520 				sdinfo->satadrv_event_flags |=
16521 				    SATA_EVNT_DEVICE_RESET;
16522 			}
16523 		}
16524 		if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) {
16525 			/* Invalid event for a device */
16526 			(void) sprintf(buf2, err_msg_evnt_2,
16527 			    event & ~SATA_EVNT_DRIVE_EVENTS);
16528 		}
16529 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
16530 	} else if (saddr->qual == SATA_ADDR_PMULT) {
16531 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
16532 
16533 		/* qualify this event */
16534 		if ((event & (SATA_EVNT_DEVICE_RESET |
16535 		    SATA_EVNT_PMULT_LINK_CHANGED)) == 0) {
16536 			/* Invalid event for a port multiplier */
16537 			(void) sprintf(buf2, err_msg_evnt_2,
16538 			    event & SATA_EVNT_DEVICE_RESET);
16539 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
16540 			goto event_info;
16541 		}
16542 
16543 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
16544 
16545 		if (event & SATA_EVNT_DEVICE_RESET) {
16546 
16547 			SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
16548 			    "[Reset] port-mult on cport %d", cport);
16549 			pmultinfo->pmult_event_flags |=
16550 			    SATA_EVNT_DEVICE_RESET;
16551 			(void) strlcat(buf1, "pmult reset, ",
16552 			    SATA_EVENT_MAX_MSG_LENGTH);
16553 		}
16554 
16555 		if (event & SATA_EVNT_PMULT_LINK_CHANGED) {
16556 
16557 			SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
16558 			    "pmult link changed on cport %d", cport);
16559 			pmultinfo->pmult_event_flags |=
16560 			    SATA_EVNT_PMULT_LINK_CHANGED;
16561 			(void) strlcat(buf1, "pmult link changed, ",
16562 			    SATA_EVENT_MAX_MSG_LENGTH);
16563 		}
16564 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
16565 
16566 	} else {
16567 		if (saddr->qual != SATA_ADDR_NULL) {
16568 			/* Wrong address qualifier */
16569 			SATA_LOG_D((sata_hba_inst, CE_WARN,
16570 			    "sata_hba_event_notify: invalid address 0x%x",
16571 			    *(uint32_t *)saddr));
16572 			return;
16573 		}
16574 		if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 ||
16575 		    (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) {
16576 			/* Invalid event for the controller */
16577 			SATA_LOG_D((sata_hba_inst, CE_WARN,
16578 			    "sata_hba_event_notify: invalid event 0x%x for "
16579 			    "controller",
16580 			    event & SATA_EVNT_CONTROLLER_EVENTS));
16581 			return;
16582 		}
16583 		buf1[0] = '\0';
16584 		/* This may be a frequent and not interesting event */
16585 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
16586 		    "controller power level changed\n", NULL);
16587 
16588 		mutex_enter(&sata_hba_inst->satahba_mutex);
16589 		if (sata_hba_inst->satahba_stats.ctrl_pwr_change <
16590 		    0xffffffffffffffffULL)
16591 			sata_hba_inst->satahba_stats.ctrl_pwr_change++;
16592 
16593 		sata_hba_inst->satahba_event_flags |=
16594 		    SATA_EVNT_PWR_LEVEL_CHANGED;
16595 		mutex_exit(&sata_hba_inst->satahba_mutex);
16596 	}
16597 	/*
16598 	 * If we got here, there is something to do with this HBA
16599 	 * instance.
16600 	 */
16601 	mutex_enter(&sata_hba_inst->satahba_mutex);
16602 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
16603 	mutex_exit(&sata_hba_inst->satahba_mutex);
16604 	mutex_enter(&sata_mutex);
16605 	sata_event_pending |= SATA_EVNT_MAIN;	/* global event indicator */
16606 	mutex_exit(&sata_mutex);
16607 
16608 	/* Tickle event thread */
16609 	mutex_enter(&sata_event_mutex);
16610 	if (sata_event_thread_active == 0)
16611 		cv_signal(&sata_event_cv);
16612 	mutex_exit(&sata_event_mutex);
16613 
16614 event_info:
16615 	if (buf1[0] != '\0') {
16616 		lcp = strrchr(buf1, ',');
16617 		if (lcp != NULL)
16618 			*lcp = '\0';
16619 	}
16620 	if (saddr->qual == SATA_ADDR_CPORT ||
16621 	    saddr->qual == SATA_ADDR_DCPORT) {
16622 		if (buf1[0] != '\0') {
16623 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
16624 			    cport, buf1);
16625 		}
16626 		if (buf2[0] != '\0') {
16627 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
16628 			    cport, buf2);
16629 		}
16630 	} else if (saddr->qual == SATA_ADDR_PMPORT ||
16631 	    saddr->qual == SATA_ADDR_DPMPORT) {
16632 		if (buf1[0] != '\0') {
16633 			sata_log(sata_hba_inst, CE_NOTE,
16634 			    "port %d pmport %d: %s\n", cport, pmport, buf1);
16635 		}
16636 		if (buf2[0] != '\0') {
16637 			sata_log(sata_hba_inst, CE_NOTE,
16638 			    "port %d pmport %d: %s\n", cport, pmport, buf2);
16639 		}
16640 	}
16641 }
16642 
16643 
16644 /*
16645  * Event processing thread.
16646  * Arg is a pointer to the sata_hba_list pointer.
16647  * It is not really needed, because sata_hba_list is global and static
16648  */
16649 static void
16650 sata_event_daemon(void *arg)
16651 {
16652 #ifndef __lock_lint
16653 	_NOTE(ARGUNUSED(arg))
16654 #endif
16655 	sata_hba_inst_t *sata_hba_inst;
16656 	clock_t lbolt;
16657 
16658 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
16659 	    "SATA event daemon started\n", NULL);
16660 loop:
16661 	/*
16662 	 * Process events here. Walk through all registered HBAs
16663 	 */
16664 	mutex_enter(&sata_mutex);
16665 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
16666 	    sata_hba_inst = sata_hba_inst->satahba_next) {
16667 		ASSERT(sata_hba_inst != NULL);
16668 		mutex_enter(&sata_hba_inst->satahba_mutex);
16669 		if (sata_hba_inst->satahba_attached == 0 ||
16670 		    (sata_hba_inst->satahba_event_flags &
16671 		    SATA_EVNT_SKIP) != 0) {
16672 			mutex_exit(&sata_hba_inst->satahba_mutex);
16673 			continue;
16674 		}
16675 		if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) {
16676 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP;
16677 			mutex_exit(&sata_hba_inst->satahba_mutex);
16678 			mutex_exit(&sata_mutex);
16679 			/* Got the controller with pending event */
16680 			sata_process_controller_events(sata_hba_inst);
16681 			/*
16682 			 * Since global mutex was released, there is a
16683 			 * possibility that HBA list has changed, so start
16684 			 * over from the top. Just processed controller
16685 			 * will be passed-over because of the SKIP flag.
16686 			 */
16687 			goto loop;
16688 		}
16689 		mutex_exit(&sata_hba_inst->satahba_mutex);
16690 	}
16691 	/* Clear SKIP flag in all controllers */
16692 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
16693 	    sata_hba_inst = sata_hba_inst->satahba_next) {
16694 		mutex_enter(&sata_hba_inst->satahba_mutex);
16695 		sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP;
16696 		mutex_exit(&sata_hba_inst->satahba_mutex);
16697 	}
16698 	mutex_exit(&sata_mutex);
16699 
16700 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
16701 	    "SATA EVENT DAEMON suspending itself", NULL);
16702 
16703 #ifdef SATA_DEBUG
16704 	if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) {
16705 		sata_log(sata_hba_inst, CE_WARN,
16706 		    "SATA EVENTS PROCESSING DISABLED\n");
16707 		thread_exit(); /* Daemon will not run again */
16708 	}
16709 #endif
16710 	mutex_enter(&sata_event_mutex);
16711 	sata_event_thread_active = 0;
16712 	mutex_exit(&sata_event_mutex);
16713 	/*
16714 	 * Go to sleep/suspend itself and wake up either because new event or
16715 	 * wait timeout. Exit if there is a termination request (driver
16716 	 * unload).
16717 	 */
16718 	do {
16719 		lbolt = ddi_get_lbolt();
16720 		lbolt += drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME);
16721 		mutex_enter(&sata_event_mutex);
16722 		(void) cv_timedwait(&sata_event_cv, &sata_event_mutex, lbolt);
16723 
16724 		if (sata_event_thread_active != 0) {
16725 			mutex_exit(&sata_event_mutex);
16726 			continue;
16727 		}
16728 
16729 		/* Check if it is time to go away */
16730 		if (sata_event_thread_terminate == 1) {
16731 			/*
16732 			 * It is up to the thread setting above flag to make
16733 			 * sure that this thread is not killed prematurely.
16734 			 */
16735 			sata_event_thread_terminate = 0;
16736 			sata_event_thread = NULL;
16737 			mutex_exit(&sata_event_mutex);
16738 			SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
16739 			    "SATA_EVENT_DAEMON_TERMINATING", NULL);
16740 			thread_exit();  { _NOTE(NOT_REACHED) }
16741 		}
16742 		mutex_exit(&sata_event_mutex);
16743 	} while (!(sata_event_pending & SATA_EVNT_MAIN));
16744 
16745 	mutex_enter(&sata_event_mutex);
16746 	sata_event_thread_active = 1;
16747 	mutex_exit(&sata_event_mutex);
16748 
16749 	mutex_enter(&sata_mutex);
16750 	sata_event_pending &= ~SATA_EVNT_MAIN;
16751 	mutex_exit(&sata_mutex);
16752 
16753 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
16754 	    "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL);
16755 
16756 	goto loop;
16757 }
16758 
16759 /*
16760  * Specific HBA instance event processing.
16761  *
16762  * NOTE: At the moment, device event processing is limited to hard disks
16763  * only.
16764  * Port multiplier is supported now.
16765  */
16766 static void
16767 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst)
16768 {
16769 	int ncport;
16770 	uint32_t event_flags;
16771 	sata_address_t *saddr;
16772 	sata_cport_info_t *cportinfo;
16773 	sata_pmult_info_t *pmultinfo;
16774 
16775 	SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst,
16776 	    "Processing controller %d event(s)",
16777 	    ddi_get_instance(SATA_DIP(sata_hba_inst)));
16778 
16779 	mutex_enter(&sata_hba_inst->satahba_mutex);
16780 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN;
16781 	event_flags = sata_hba_inst->satahba_event_flags;
16782 	mutex_exit(&sata_hba_inst->satahba_mutex);
16783 	/*
16784 	 * Process controller power change first
16785 	 * HERE
16786 	 */
16787 	if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED)
16788 		sata_process_cntrl_pwr_level_change(sata_hba_inst);
16789 
16790 	/*
16791 	 * Search through ports/devices to identify affected port/device.
16792 	 * We may have to process events for more than one port/device.
16793 	 */
16794 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
16795 		/*
16796 		 * Not all ports may be processed in attach by the time we
16797 		 * get an event. Check if port info is initialized.
16798 		 */
16799 		mutex_enter(&sata_hba_inst->satahba_mutex);
16800 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
16801 		mutex_exit(&sata_hba_inst->satahba_mutex);
16802 		if (cportinfo == NULL || cportinfo->cport_state == NULL)
16803 			continue;
16804 
16805 		/* We have initialized controller port info */
16806 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
16807 		event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))->
16808 		    cport_event_flags;
16809 		/* Check if port was locked by IOCTL processing */
16810 		if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) {
16811 			/*
16812 			 * We ignore port events because port is busy
16813 			 * with AP control processing. Set again
16814 			 * controller and main event flag, so that
16815 			 * events may be processed by the next daemon
16816 			 * run.
16817 			 */
16818 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
16819 			mutex_enter(&sata_hba_inst->satahba_mutex);
16820 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
16821 			mutex_exit(&sata_hba_inst->satahba_mutex);
16822 			mutex_enter(&sata_mutex);
16823 			sata_event_pending |= SATA_EVNT_MAIN;
16824 			mutex_exit(&sata_mutex);
16825 			SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst,
16826 			    "Event processing postponed until "
16827 			    "AP control processing completes",
16828 			    NULL);
16829 			/* Check other ports */
16830 			continue;
16831 		} else {
16832 			/*
16833 			 * Set BSY flag so that AP control would not
16834 			 * interfere with events processing for
16835 			 * this port.
16836 			 */
16837 			(SATA_CPORT_INFO(sata_hba_inst, ncport))->
16838 			    cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY;
16839 		}
16840 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
16841 
16842 		saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr;
16843 
16844 		if ((event_flags &
16845 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
16846 			/*
16847 			 * Got port event.
16848 			 * We need some hierarchy of event processing as they
16849 			 * are affecting each other:
16850 			 * 1. port failed
16851 			 * 2. device detached/attached
16852 			 * 3. link events - link events may trigger device
16853 			 *    detached or device attached events in some
16854 			 *    circumstances.
16855 			 * 4. port power level changed
16856 			 */
16857 			if (event_flags & SATA_EVNT_PORT_FAILED) {
16858 				sata_process_port_failed_event(sata_hba_inst,
16859 				    saddr);
16860 			}
16861 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
16862 				sata_process_device_detached(sata_hba_inst,
16863 				    saddr);
16864 			}
16865 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
16866 				sata_process_device_attached(sata_hba_inst,
16867 				    saddr);
16868 			}
16869 			if (event_flags &
16870 			    (SATA_EVNT_LINK_ESTABLISHED |
16871 			    SATA_EVNT_LINK_LOST)) {
16872 				sata_process_port_link_events(sata_hba_inst,
16873 				    saddr);
16874 			}
16875 			if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) {
16876 				sata_process_port_pwr_change(sata_hba_inst,
16877 				    saddr);
16878 			}
16879 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
16880 				sata_process_target_node_cleanup(
16881 				    sata_hba_inst, saddr);
16882 			}
16883 			if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) {
16884 				sata_process_device_autoonline(
16885 				    sata_hba_inst, saddr);
16886 			}
16887 		}
16888 
16889 
16890 		/*
16891 		 * Scan port multiplier and all its sub-ports event flags.
16892 		 * The events are marked by
16893 		 * (1) sata_pmult_info.pmult_event_flags
16894 		 * (2) sata_pmport_info.pmport_event_flags
16895 		 */
16896 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
16897 		if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
16898 			/*
16899 			 * There should be another extra check: this
16900 			 * port multiplier still exists?
16901 			 */
16902 			pmultinfo = SATA_PMULT_INFO(sata_hba_inst,
16903 			    ncport);
16904 
16905 			if (pmultinfo != NULL) {
16906 				mutex_exit(&(SATA_CPORT_MUTEX(
16907 				    sata_hba_inst, ncport)));
16908 				sata_process_pmult_events(
16909 				    sata_hba_inst, ncport);
16910 				mutex_enter(&(SATA_CPORT_MUTEX(
16911 				    sata_hba_inst, ncport)));
16912 			} else {
16913 				SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
16914 				    "Port-multiplier is gone. "
16915 				    "Ignore all sub-device events "
16916 				    "at port %d.", ncport);
16917 			}
16918 		}
16919 
16920 		if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) !=
16921 		    SATA_DTYPE_NONE) &&
16922 		    (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) {
16923 			if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)->
16924 			    satadrv_event_flags &
16925 			    (SATA_EVNT_DEVICE_RESET |
16926 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
16927 				/* Have device event */
16928 				sata_process_device_reset(sata_hba_inst,
16929 				    saddr);
16930 			}
16931 		}
16932 		/* Release PORT_BUSY flag */
16933 		(SATA_CPORT_INFO(sata_hba_inst, ncport))->
16934 		    cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
16935 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
16936 
16937 	} /* End of loop through the controller SATA ports */
16938 }
16939 
16940 /*
16941  * Specific port multiplier instance event processing. At the moment, device
16942  * event processing is limited to link/attach event only.
16943  *
16944  * NOTE: power management event is not supported yet.
16945  */
16946 static void
16947 sata_process_pmult_events(sata_hba_inst_t *sata_hba_inst, uint8_t cport)
16948 {
16949 	sata_cport_info_t *cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
16950 	sata_pmult_info_t *pmultinfo;
16951 	sata_pmport_info_t *pmportinfo;
16952 	sata_address_t *saddr;
16953 	sata_device_t sata_device;
16954 	uint32_t event_flags;
16955 	int npmport;
16956 	int rval;
16957 
16958 	SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst,
16959 	    "Processing pmult event(s) on cport %d of controller %d",
16960 	    cport, ddi_get_instance(SATA_DIP(sata_hba_inst)));
16961 
16962 	/* First process events on port multiplier */
16963 	mutex_enter(&cportinfo->cport_mutex);
16964 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
16965 	event_flags = pmultinfo->pmult_event_flags;
16966 
16967 	/*
16968 	 * Reset event (of port multiplier) has higher priority because the
16969 	 * port multiplier itself might be failed or removed after reset.
16970 	 */
16971 	if (event_flags & SATA_EVNT_DEVICE_RESET) {
16972 		/*
16973 		 * The status of the sub-links are uncertain,
16974 		 * so mark all sub-ports as RESET
16975 		 */
16976 		for (npmport = 0; npmport < SATA_NUM_PMPORTS(
16977 		    sata_hba_inst, cport); npmport ++) {
16978 			pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
16979 			    cport, npmport);
16980 			if (pmportinfo == NULL) {
16981 				/* That's weird. */
16982 				SATA_LOG_D((sata_hba_inst, CE_WARN,
16983 				    "sata_hba_event_notify: "
16984 				    "invalid/un-implemented "
16985 				    "port %d:%d (%d ports), ",
16986 				    cport, npmport, SATA_NUM_PMPORTS(
16987 				    sata_hba_inst, cport)));
16988 				continue;
16989 			}
16990 
16991 			mutex_enter(&pmportinfo->pmport_mutex);
16992 
16993 			/* Mark all pmport to unknow state. */
16994 			pmportinfo->pmport_state = SATA_STATE_UNKNOWN;
16995 			/* Mark all pmports with link events. */
16996 			pmportinfo->pmport_event_flags =
16997 			    (SATA_EVNT_LINK_ESTABLISHED|SATA_EVNT_LINK_LOST);
16998 			mutex_exit(&pmportinfo->pmport_mutex);
16999 		}
17000 
17001 	} else if (event_flags & SATA_EVNT_PMULT_LINK_CHANGED) {
17002 		/*
17003 		 * We need probe the port multiplier to know what has
17004 		 * happened.
17005 		 */
17006 		bzero(&sata_device, sizeof (sata_device_t));
17007 		sata_device.satadev_rev = SATA_DEVICE_REV;
17008 		sata_device.satadev_addr.cport = cport;
17009 		sata_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
17010 		sata_device.satadev_addr.qual = SATA_ADDR_PMULT;
17011 
17012 		mutex_exit(&cportinfo->cport_mutex);
17013 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
17014 		    (SATA_DIP(sata_hba_inst), &sata_device);
17015 		mutex_enter(&cportinfo->cport_mutex);
17016 		if (rval != SATA_SUCCESS) {
17017 			/* Something went wrong? Fail the port */
17018 			cportinfo->cport_state = SATA_PSTATE_FAILED;
17019 			mutex_exit(&cportinfo->cport_mutex);
17020 			SATA_LOG_D((sata_hba_inst, CE_WARN,
17021 			    "SATA port %d probing failed", cport));
17022 
17023 			/* PMult structure must be released.  */
17024 			sata_free_pmult(sata_hba_inst, &sata_device);
17025 			return;
17026 		}
17027 
17028 		sata_update_port_info(sata_hba_inst, &sata_device);
17029 
17030 		/*
17031 		 * Sanity check - Port is active? Is the link active?
17032 		 * The device is still a port multiplier?
17033 		 */
17034 		if ((cportinfo->cport_state &
17035 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
17036 		    ((cportinfo->cport_scr.sstatus &
17037 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) ||
17038 		    (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) {
17039 			mutex_exit(&cportinfo->cport_mutex);
17040 
17041 			/* PMult structure must be released.  */
17042 			sata_free_pmult(sata_hba_inst, &sata_device);
17043 			return;
17044 		}
17045 
17046 		/* Probed succeed, set port ready. */
17047 		cportinfo->cport_state |=
17048 		    SATA_STATE_PROBED | SATA_STATE_READY;
17049 	}
17050 
17051 	/* Release port multiplier event flags. */
17052 	pmultinfo->pmult_event_flags &=
17053 	    ~(SATA_EVNT_DEVICE_RESET|SATA_EVNT_PMULT_LINK_CHANGED);
17054 	mutex_exit(&cportinfo->cport_mutex);
17055 
17056 	/*
17057 	 * Check all sub-links.
17058 	 */
17059 	for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst, cport);
17060 	    npmport ++) {
17061 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport);
17062 		mutex_enter(&pmportinfo->pmport_mutex);
17063 		event_flags = pmportinfo->pmport_event_flags;
17064 		mutex_exit(&pmportinfo->pmport_mutex);
17065 		saddr = &pmportinfo->pmport_addr;
17066 
17067 		if ((event_flags &
17068 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
17069 			/*
17070 			 * Got port multiplier port event.
17071 			 * We need some hierarchy of event processing as they
17072 			 * are affecting each other:
17073 			 * 1. device detached/attached
17074 			 * 2. link events - link events may trigger device
17075 			 *    detached or device attached events in some
17076 			 *    circumstances.
17077 			 */
17078 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
17079 				sata_process_pmdevice_detached(sata_hba_inst,
17080 				    saddr);
17081 			}
17082 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
17083 				sata_process_pmdevice_attached(sata_hba_inst,
17084 				    saddr);
17085 			}
17086 			if (event_flags & SATA_EVNT_LINK_ESTABLISHED ||
17087 			    event_flags & SATA_EVNT_LINK_LOST) {
17088 				sata_process_pmport_link_events(sata_hba_inst,
17089 				    saddr);
17090 			}
17091 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
17092 				sata_process_target_node_cleanup(
17093 				    sata_hba_inst, saddr);
17094 			}
17095 		}
17096 
17097 		/* Checking drive event(s). */
17098 		mutex_enter(&pmportinfo->pmport_mutex);
17099 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
17100 		    pmportinfo->pmport_sata_drive != NULL) {
17101 			event_flags = pmportinfo->pmport_sata_drive->
17102 			    satadrv_event_flags;
17103 			if (event_flags & (SATA_EVNT_DEVICE_RESET |
17104 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
17105 
17106 				/* Have device event */
17107 				sata_process_pmdevice_reset(sata_hba_inst,
17108 				    saddr);
17109 			}
17110 		}
17111 		mutex_exit(&pmportinfo->pmport_mutex);
17112 
17113 		/* Release PORT_BUSY flag */
17114 		mutex_enter(&cportinfo->cport_mutex);
17115 		cportinfo->cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
17116 		mutex_exit(&cportinfo->cport_mutex);
17117 	}
17118 
17119 	SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst,
17120 	    "[DONE] pmult event(s) on cport %d of controller %d",
17121 	    cport, ddi_get_instance(SATA_DIP(sata_hba_inst)));
17122 }
17123 
17124 /*
17125  * Process HBA power level change reported by HBA driver.
17126  * Not implemented at this time - event is ignored.
17127  */
17128 static void
17129 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst)
17130 {
17131 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17132 	    "Processing controller power level change", NULL);
17133 
17134 	/* Ignoring it for now */
17135 	mutex_enter(&sata_hba_inst->satahba_mutex);
17136 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
17137 	mutex_exit(&sata_hba_inst->satahba_mutex);
17138 }
17139 
17140 /*
17141  * Process port power level change reported by HBA driver.
17142  * Not implemented at this time - event is ignored.
17143  */
17144 static void
17145 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst,
17146     sata_address_t *saddr)
17147 {
17148 	sata_cport_info_t *cportinfo;
17149 
17150 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17151 	    "Processing port power level change", NULL);
17152 
17153 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
17154 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
17155 	/* Reset event flag */
17156 	cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
17157 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
17158 }
17159 
17160 /*
17161  * Process port failure reported by HBA driver.
17162  * cports support only - no pmports.
17163  */
17164 static void
17165 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst,
17166     sata_address_t *saddr)
17167 {
17168 	sata_cport_info_t *cportinfo;
17169 
17170 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
17171 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
17172 	/* Reset event flag first */
17173 	cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED;
17174 	/* If the port is in SHUTDOWN or FAILED state, ignore this event. */
17175 	if ((cportinfo->cport_state &
17176 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) {
17177 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
17178 		    cport_mutex);
17179 		return;
17180 	}
17181 	/* Fail the port */
17182 	cportinfo->cport_state = SATA_PSTATE_FAILED;
17183 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
17184 	sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport);
17185 }
17186 
17187 /*
17188  * Device Reset Event processing.
17189  * The seqeunce is managed by 3 stage flags:
17190  * - reset event reported,
17191  * - reset event being processed,
17192  * - request to clear device reset state.
17193  *
17194  * NOTE: This function has to be entered with cport mutex held. It exits with
17195  * mutex held as well, but can release mutex during the processing.
17196  */
17197 static void
17198 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst,
17199     sata_address_t *saddr)
17200 {
17201 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
17202 	sata_drive_info_t *sdinfo;
17203 	sata_cport_info_t *cportinfo;
17204 	sata_device_t sata_device;
17205 	int rval_probe, rval_set;
17206 
17207 	/* We only care about host sata cport for now */
17208 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
17209 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
17210 	/*
17211 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
17212 	 * state, ignore reset event.
17213 	 */
17214 	if (((cportinfo->cport_state &
17215 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
17216 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
17217 		sdinfo->satadrv_event_flags &=
17218 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
17219 		return;
17220 	}
17221 
17222 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) ==
17223 	    SATA_DTYPE_PMULT)) {
17224 		/*
17225 		 * Should not happened: this is already handled in
17226 		 * sata_hba_event_notify()
17227 		 */
17228 		mutex_exit(&cportinfo->cport_mutex);
17229 		goto done;
17230 	}
17231 
17232 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) &
17233 	    SATA_VALID_DEV_TYPE) == 0) {
17234 		/*
17235 		 * This should not happen - coding error.
17236 		 * But we can recover, so do not panic, just clean up
17237 		 * and if in debug mode, log the message.
17238 		 */
17239 #ifdef SATA_DEBUG
17240 		sata_log(sata_hba_inst, CE_WARN,
17241 		    "sata_process_device_reset: "
17242 		    "Invalid device type with sdinfo!", NULL);
17243 #endif
17244 		sdinfo->satadrv_event_flags = 0;
17245 		return;
17246 	}
17247 
17248 #ifdef SATA_DEBUG
17249 	if ((sdinfo->satadrv_event_flags &
17250 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
17251 		/* Nothing to do */
17252 		/* Something is weird - why we are processing dev reset? */
17253 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17254 		    "No device reset event!!!!", NULL);
17255 
17256 		return;
17257 	}
17258 	if ((sdinfo->satadrv_event_flags &
17259 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
17260 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
17261 		/* Something is weird - new device reset event */
17262 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17263 		    "Overlapping device reset events!", NULL);
17264 	}
17265 #endif
17266 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17267 	    "Processing port %d device reset", saddr->cport);
17268 
17269 	/* Clear event flag */
17270 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
17271 
17272 	/* It seems that we always need to check the port state first */
17273 	sata_device.satadev_rev = SATA_DEVICE_REV;
17274 	sata_device.satadev_addr = *saddr;
17275 	/*
17276 	 * We have to exit mutex, because the HBA probe port function may
17277 	 * block on its own mutex.
17278 	 */
17279 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
17280 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
17281 	    (SATA_DIP(sata_hba_inst), &sata_device);
17282 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
17283 	sata_update_port_info(sata_hba_inst, &sata_device);
17284 	if (rval_probe != SATA_SUCCESS) {
17285 		/* Something went wrong? Fail the port */
17286 		cportinfo->cport_state = SATA_PSTATE_FAILED;
17287 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
17288 		if (sdinfo != NULL)
17289 			sdinfo->satadrv_event_flags = 0;
17290 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
17291 		    cport_mutex);
17292 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17293 		    "SATA port %d probing failed",
17294 		    saddr->cport));
17295 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
17296 		    saddr->cport)->cport_mutex);
17297 		return;
17298 	}
17299 	if ((sata_device.satadev_scr.sstatus  &
17300 	    SATA_PORT_DEVLINK_UP_MASK) !=
17301 	    SATA_PORT_DEVLINK_UP ||
17302 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
17303 		/*
17304 		 * No device to process, anymore. Some other event processing
17305 		 * would or have already performed port info cleanup.
17306 		 * To be safe (HBA may need it), request clearing device
17307 		 * reset condition.
17308 		 */
17309 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
17310 		if (sdinfo != NULL) {
17311 			sdinfo->satadrv_event_flags &=
17312 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
17313 			sdinfo->satadrv_event_flags |=
17314 			    SATA_EVNT_CLEAR_DEVICE_RESET;
17315 		}
17316 		return;
17317 	}
17318 
17319 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
17320 	if (sdinfo == NULL) {
17321 		return;
17322 	}
17323 	if ((sdinfo->satadrv_event_flags &
17324 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
17325 		/*
17326 		 * Start tracking time for device feature restoration and
17327 		 * identification. Save current time (lbolt value).
17328 		 */
17329 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
17330 	}
17331 	/* Mark device reset processing as active */
17332 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
17333 
17334 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
17335 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
17336 
17337 	rval_set = sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1);
17338 
17339 	if (rval_set  != SATA_SUCCESS) {
17340 		/*
17341 		 * Restoring drive setting failed.
17342 		 * Probe the port first, to check if the port state has changed
17343 		 */
17344 		sata_device.satadev_rev = SATA_DEVICE_REV;
17345 		sata_device.satadev_addr = *saddr;
17346 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
17347 		/* probe port */
17348 		rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
17349 		    (SATA_DIP(sata_hba_inst), &sata_device);
17350 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
17351 		    cport_mutex);
17352 		if (rval_probe == SATA_SUCCESS &&
17353 		    (sata_device.satadev_state &
17354 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
17355 		    (sata_device.satadev_scr.sstatus  &
17356 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
17357 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
17358 			/*
17359 			 * We may retry this a bit later - in-process reset
17360 			 * condition should be already set.
17361 			 * Track retry time for device identification.
17362 			 */
17363 			if ((cportinfo->cport_dev_type &
17364 			    SATA_VALID_DEV_TYPE) != 0 &&
17365 			    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL &&
17366 			    sdinfo->satadrv_reset_time != 0) {
17367 				clock_t cur_time = ddi_get_lbolt();
17368 				/*
17369 				 * If the retry time limit was not
17370 				 * exceeded, retry.
17371 				 */
17372 				if ((cur_time - sdinfo->satadrv_reset_time) <
17373 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
17374 					mutex_enter(
17375 					    &sata_hba_inst->satahba_mutex);
17376 					sata_hba_inst->satahba_event_flags |=
17377 					    SATA_EVNT_MAIN;
17378 					mutex_exit(
17379 					    &sata_hba_inst->satahba_mutex);
17380 					mutex_enter(&sata_mutex);
17381 					sata_event_pending |= SATA_EVNT_MAIN;
17382 					mutex_exit(&sata_mutex);
17383 					return;
17384 				}
17385 				if (rval_set == SATA_RETRY) {
17386 					/*
17387 					 * Setting drive features failed, but
17388 					 * the drive is still accessible,
17389 					 * so emit a warning message before
17390 					 * return.
17391 					 */
17392 					mutex_exit(&SATA_CPORT_INFO(
17393 					    sata_hba_inst,
17394 					    saddr->cport)->cport_mutex);
17395 					goto done;
17396 				}
17397 			}
17398 			/* Fail the drive */
17399 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
17400 
17401 			sata_log(sata_hba_inst, CE_WARN,
17402 			    "SATA device at port %d - device failed",
17403 			    saddr->cport);
17404 		}
17405 		/*
17406 		 * No point of retrying - device failed or some other event
17407 		 * processing or already did or will do port info cleanup.
17408 		 * To be safe (HBA may need it),
17409 		 * request clearing device reset condition.
17410 		 */
17411 		sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
17412 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
17413 		sdinfo->satadrv_reset_time = 0;
17414 		return;
17415 	}
17416 done:
17417 	/*
17418 	 * If setting of drive features failed, but the drive is still
17419 	 * accessible, emit a warning message.
17420 	 */
17421 	if (rval_set == SATA_RETRY) {
17422 		sata_log(sata_hba_inst, CE_WARN,
17423 		    "SATA device at port %d - desired setting could not be "
17424 		    "restored after reset. Device may not operate as expected.",
17425 		    saddr->cport);
17426 	}
17427 	/*
17428 	 * Raise the flag indicating that the next sata command could
17429 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
17430 	 * reset is reported.
17431 	 */
17432 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
17433 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
17434 		sdinfo->satadrv_reset_time = 0;
17435 		if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) {
17436 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
17437 			sdinfo->satadrv_event_flags &=
17438 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
17439 			sdinfo->satadrv_event_flags |=
17440 			    SATA_EVNT_CLEAR_DEVICE_RESET;
17441 		}
17442 	}
17443 }
17444 
17445 
17446 /*
17447  * Port Multiplier Port Device Reset Event processing.
17448  *
17449  * NOTE: This function has to be entered with pmport mutex held. It exits with
17450  * mutex held as well, but can release mutex during the processing.
17451  */
17452 static void
17453 sata_process_pmdevice_reset(sata_hba_inst_t *sata_hba_inst,
17454     sata_address_t *saddr)
17455 {
17456 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
17457 	sata_drive_info_t *sdinfo = NULL;
17458 	sata_cport_info_t *cportinfo = NULL;
17459 	sata_pmport_info_t *pmportinfo = NULL;
17460 	sata_pmult_info_t *pminfo = NULL;
17461 	sata_device_t sata_device;
17462 	uint8_t cport = saddr->cport;
17463 	uint8_t pmport = saddr->pmport;
17464 	int rval;
17465 
17466 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17467 	    "Processing drive reset at port %d:%d", cport, pmport);
17468 
17469 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
17470 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
17471 	sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport);
17472 
17473 	/*
17474 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
17475 	 * state, ignore reset event.
17476 	 */
17477 	if (((cportinfo->cport_state &
17478 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
17479 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
17480 		sdinfo->satadrv_event_flags &=
17481 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
17482 		return;
17483 	}
17484 
17485 	if ((pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
17486 		/*
17487 		 * This should not happen - coding error.
17488 		 * But we can recover, so do not panic, just clean up
17489 		 * and if in debug mode, log the message.
17490 		 */
17491 #ifdef SATA_DEBUG
17492 		sata_log(sata_hba_inst, CE_WARN,
17493 		    "sata_process_pmdevice_reset: "
17494 		    "Invalid device type with sdinfo!", NULL);
17495 #endif
17496 		sdinfo->satadrv_event_flags = 0;
17497 		return;
17498 	}
17499 
17500 #ifdef SATA_DEBUG
17501 	if ((sdinfo->satadrv_event_flags &
17502 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
17503 		/* Nothing to do */
17504 		/* Something is weird - why we are processing dev reset? */
17505 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17506 		    "No device reset event!!!!", NULL);
17507 
17508 		return;
17509 	}
17510 	if ((sdinfo->satadrv_event_flags &
17511 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
17512 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
17513 		/* Something is weird - new device reset event */
17514 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17515 		    "Overlapping device reset events!", NULL);
17516 	}
17517 #endif
17518 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17519 	    "Processing port %d:%d device reset", cport, pmport);
17520 
17521 	/* Clear event flag */
17522 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
17523 
17524 	/* It seems that we always need to check the port state first */
17525 	sata_device.satadev_rev = SATA_DEVICE_REV;
17526 	sata_device.satadev_addr = *saddr;
17527 	/*
17528 	 * We have to exit mutex, because the HBA probe port function may
17529 	 * block on its own mutex.
17530 	 */
17531 	mutex_exit(&pmportinfo->pmport_mutex);
17532 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
17533 	    (SATA_DIP(sata_hba_inst), &sata_device);
17534 	mutex_enter(&pmportinfo->pmport_mutex);
17535 
17536 	sata_update_pmport_info(sata_hba_inst, &sata_device);
17537 	if (rval != SATA_SUCCESS) {
17538 		/* Something went wrong? Fail the port */
17539 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
17540 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
17541 		    saddr->pmport);
17542 		if (sdinfo != NULL)
17543 			sdinfo->satadrv_event_flags = 0;
17544 		mutex_exit(&pmportinfo->pmport_mutex);
17545 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17546 		    "SATA port %d:%d probing failed",
17547 		    saddr->cport, saddr->pmport));
17548 		mutex_enter(&pmportinfo->pmport_mutex);
17549 		return;
17550 	}
17551 	if ((sata_device.satadev_scr.sstatus  &
17552 	    SATA_PORT_DEVLINK_UP_MASK) !=
17553 	    SATA_PORT_DEVLINK_UP ||
17554 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
17555 		/*
17556 		 * No device to process, anymore. Some other event processing
17557 		 * would or have already performed port info cleanup.
17558 		 * To be safe (HBA may need it), request clearing device
17559 		 * reset condition.
17560 		 */
17561 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
17562 		    saddr->pmport);
17563 		if (sdinfo != NULL) {
17564 			sdinfo->satadrv_event_flags &=
17565 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
17566 			/* must clear flags on cport */
17567 			pminfo = SATA_PMULT_INFO(sata_hba_inst,
17568 			    saddr->cport);
17569 			pminfo->pmult_event_flags |=
17570 			    SATA_EVNT_CLEAR_DEVICE_RESET;
17571 		}
17572 		return;
17573 	}
17574 
17575 	sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
17576 	    saddr->pmport);
17577 	if (sdinfo == NULL) {
17578 		return;
17579 	}
17580 	if ((sdinfo->satadrv_event_flags &
17581 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
17582 		/*
17583 		 * Start tracking time for device feature restoration and
17584 		 * identification. Save current time (lbolt value).
17585 		 */
17586 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
17587 	}
17588 	/* Mark device reset processing as active */
17589 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
17590 
17591 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
17592 	mutex_exit(&pmportinfo->pmport_mutex);
17593 
17594 	if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) ==
17595 	    SATA_FAILURE) {
17596 		/*
17597 		 * Restoring drive setting failed.
17598 		 * Probe the port first, to check if the port state has changed
17599 		 */
17600 		sata_device.satadev_rev = SATA_DEVICE_REV;
17601 		sata_device.satadev_addr = *saddr;
17602 		sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
17603 
17604 		/* probe port */
17605 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
17606 		    (SATA_DIP(sata_hba_inst), &sata_device);
17607 		mutex_enter(&pmportinfo->pmport_mutex);
17608 		if (rval == SATA_SUCCESS &&
17609 		    (sata_device.satadev_state &
17610 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
17611 		    (sata_device.satadev_scr.sstatus  &
17612 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
17613 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
17614 			/*
17615 			 * We may retry this a bit later - in-process reset
17616 			 * condition should be already set.
17617 			 * Track retry time for device identification.
17618 			 */
17619 			if ((pmportinfo->pmport_dev_type &
17620 			    SATA_VALID_DEV_TYPE) != 0 &&
17621 			    SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL &&
17622 			    sdinfo->satadrv_reset_time != 0) {
17623 				clock_t cur_time = ddi_get_lbolt();
17624 				/*
17625 				 * If the retry time limit was not
17626 				 * exceeded, retry.
17627 				 */
17628 				if ((cur_time - sdinfo->satadrv_reset_time) <
17629 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
17630 					mutex_enter(
17631 					    &sata_hba_inst->satahba_mutex);
17632 					sata_hba_inst->satahba_event_flags |=
17633 					    SATA_EVNT_MAIN;
17634 					mutex_exit(
17635 					    &sata_hba_inst->satahba_mutex);
17636 					mutex_enter(&sata_mutex);
17637 					sata_event_pending |= SATA_EVNT_MAIN;
17638 					mutex_exit(&sata_mutex);
17639 					return;
17640 				}
17641 			}
17642 			/* Fail the drive */
17643 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
17644 
17645 			sata_log(sata_hba_inst, CE_WARN,
17646 			    "SATA device at port %d:%d - device failed",
17647 			    saddr->cport, saddr->pmport);
17648 		} else {
17649 			/*
17650 			 * No point of retrying - some other event processing
17651 			 * would or already did port info cleanup.
17652 			 * To be safe (HBA may need it),
17653 			 * request clearing device reset condition.
17654 			 */
17655 			sdinfo->satadrv_event_flags |=
17656 			    SATA_EVNT_CLEAR_DEVICE_RESET;
17657 		}
17658 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
17659 		sdinfo->satadrv_reset_time = 0;
17660 		return;
17661 	}
17662 	/*
17663 	 * Raise the flag indicating that the next sata command could
17664 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
17665 	 * reset is reported.
17666 	 */
17667 	mutex_enter(&pmportinfo->pmport_mutex);
17668 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
17669 		sdinfo->satadrv_reset_time = 0;
17670 		if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) {
17671 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
17672 			sdinfo->satadrv_event_flags &=
17673 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
17674 			/* must clear flags on cport */
17675 			pminfo = SATA_PMULT_INFO(sata_hba_inst,
17676 			    saddr->cport);
17677 			pminfo->pmult_event_flags |=
17678 			    SATA_EVNT_CLEAR_DEVICE_RESET;
17679 		}
17680 	}
17681 }
17682 
17683 /*
17684  * Port Link Events processing.
17685  * Every link established event may involve device reset (due to
17686  * COMRESET signal, equivalent of the hard reset) so arbitrarily
17687  * set device reset event for an attached device (if any).
17688  * If the port is in SHUTDOWN or FAILED state, ignore link events.
17689  *
17690  * The link established event processing varies, depending on the state
17691  * of the target node, HBA hotplugging capabilities, state of the port.
17692  * If the link is not active, the link established event is ignored.
17693  * If HBA cannot detect device attachment and there is no target node,
17694  * the link established event triggers device attach event processing.
17695  * Else, link established event triggers device reset event processing.
17696  *
17697  * The link lost event processing varies, depending on a HBA hotplugging
17698  * capability and the state of the port (link active or not active).
17699  * If the link is active, the lost link event is ignored.
17700  * If HBA cannot detect device removal, the lost link event triggers
17701  * device detached event processing after link lost timeout.
17702  * Else, the event is ignored.
17703  *
17704  * NOTE: Port multiplier ports events are handled by
17705  * sata_process_pmport_link_events();
17706  */
17707 static void
17708 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst,
17709     sata_address_t *saddr)
17710 {
17711 	sata_device_t sata_device;
17712 	sata_cport_info_t *cportinfo;
17713 	sata_drive_info_t *sdinfo;
17714 	uint32_t event_flags;
17715 	int rval;
17716 
17717 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17718 	    "Processing port %d link event(s)", saddr->cport);
17719 
17720 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
17721 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
17722 	event_flags = cportinfo->cport_event_flags;
17723 
17724 	/* Reset event flags first */
17725 	cportinfo->cport_event_flags &=
17726 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
17727 
17728 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
17729 	if ((cportinfo->cport_state &
17730 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
17731 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
17732 		    cport_mutex);
17733 		return;
17734 	}
17735 
17736 	/*
17737 	 * For the sanity sake get current port state.
17738 	 * Set device address only. Other sata_device fields should be
17739 	 * set by HBA driver.
17740 	 */
17741 	sata_device.satadev_rev = SATA_DEVICE_REV;
17742 	sata_device.satadev_addr = *saddr;
17743 	/*
17744 	 * We have to exit mutex, because the HBA probe port function may
17745 	 * block on its own mutex.
17746 	 */
17747 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
17748 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
17749 	    (SATA_DIP(sata_hba_inst), &sata_device);
17750 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
17751 	sata_update_port_info(sata_hba_inst, &sata_device);
17752 	if (rval != SATA_SUCCESS) {
17753 		/* Something went wrong? Fail the port */
17754 		cportinfo->cport_state = SATA_PSTATE_FAILED;
17755 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
17756 		    cport_mutex);
17757 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17758 		    "SATA port %d probing failed",
17759 		    saddr->cport));
17760 		/*
17761 		 * We may want to release device info structure, but
17762 		 * it is not necessary.
17763 		 */
17764 		return;
17765 	} else {
17766 		/* port probed successfully */
17767 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
17768 	}
17769 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
17770 
17771 		if ((sata_device.satadev_scr.sstatus &
17772 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
17773 			/* Ignore event */
17774 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17775 			    "Ignoring port %d link established event - "
17776 			    "link down",
17777 			    saddr->cport);
17778 			goto linklost;
17779 		}
17780 
17781 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17782 		    "Processing port %d link established event",
17783 		    saddr->cport);
17784 
17785 		/*
17786 		 * For the sanity sake check if a device is attached - check
17787 		 * return state of a port probing.
17788 		 */
17789 		if (sata_device.satadev_type != SATA_DTYPE_NONE) {
17790 			/*
17791 			 * HBA port probe indicated that there is a device
17792 			 * attached. Check if the framework had device info
17793 			 * structure attached for this device.
17794 			 */
17795 			if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
17796 				ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) !=
17797 				    NULL);
17798 
17799 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
17800 				if ((sdinfo->satadrv_type &
17801 				    SATA_VALID_DEV_TYPE) != 0) {
17802 					/*
17803 					 * Dev info structure is present.
17804 					 * If dev_type is set to known type in
17805 					 * the framework's drive info struct
17806 					 * then the device existed before and
17807 					 * the link was probably lost
17808 					 * momentarily - in such case
17809 					 * we may want to check device
17810 					 * identity.
17811 					 * Identity check is not supported now.
17812 					 *
17813 					 * Link established event
17814 					 * triggers device reset event.
17815 					 */
17816 					(SATA_CPORTINFO_DRV_INFO(cportinfo))->
17817 					    satadrv_event_flags |=
17818 					    SATA_EVNT_DEVICE_RESET;
17819 				}
17820 			} else if (cportinfo->cport_dev_type ==
17821 			    SATA_DTYPE_NONE) {
17822 				/*
17823 				 * We got new device attached! If HBA does not
17824 				 * generate device attached events, trigger it
17825 				 * here.
17826 				 */
17827 				if (!(SATA_FEATURES(sata_hba_inst) &
17828 				    SATA_CTLF_HOTPLUG)) {
17829 					cportinfo->cport_event_flags |=
17830 					    SATA_EVNT_DEVICE_ATTACHED;
17831 				}
17832 			}
17833 			/* Reset link lost timeout */
17834 			cportinfo->cport_link_lost_time = 0;
17835 		}
17836 	}
17837 linklost:
17838 	if (event_flags & SATA_EVNT_LINK_LOST) {
17839 		if ((sata_device.satadev_scr.sstatus &
17840 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
17841 			/* Ignore event */
17842 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17843 			    "Ignoring port %d link lost event - link is up",
17844 			    saddr->cport);
17845 			goto done;
17846 		}
17847 #ifdef SATA_DEBUG
17848 		if (cportinfo->cport_link_lost_time == 0) {
17849 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17850 			    "Processing port %d link lost event",
17851 			    saddr->cport);
17852 		}
17853 #endif
17854 		/*
17855 		 * When HBA cannot generate device attached/detached events,
17856 		 * we need to track link lost time and eventually generate
17857 		 * device detach event.
17858 		 */
17859 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
17860 			/* We are tracking link lost time */
17861 			if (cportinfo->cport_link_lost_time == 0) {
17862 				/* save current time (lbolt value) */
17863 				cportinfo->cport_link_lost_time =
17864 				    ddi_get_lbolt();
17865 				/* just keep link lost event */
17866 				cportinfo->cport_event_flags |=
17867 				    SATA_EVNT_LINK_LOST;
17868 			} else {
17869 				clock_t cur_time = ddi_get_lbolt();
17870 				if ((cur_time -
17871 				    cportinfo->cport_link_lost_time) >=
17872 				    drv_usectohz(
17873 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
17874 					/* trigger device detach event */
17875 					cportinfo->cport_event_flags |=
17876 					    SATA_EVNT_DEVICE_DETACHED;
17877 					cportinfo->cport_link_lost_time = 0;
17878 					SATADBG1(SATA_DBG_EVENTS,
17879 					    sata_hba_inst,
17880 					    "Triggering port %d "
17881 					    "device detached event",
17882 					    saddr->cport);
17883 				} else {
17884 					/* keep link lost event */
17885 					cportinfo->cport_event_flags |=
17886 					    SATA_EVNT_LINK_LOST;
17887 				}
17888 			}
17889 		}
17890 		/*
17891 		 * We could change port state to disable/delay access to
17892 		 * the attached device until the link is recovered.
17893 		 */
17894 	}
17895 done:
17896 	event_flags = cportinfo->cport_event_flags;
17897 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
17898 	if (event_flags != 0) {
17899 		mutex_enter(&sata_hba_inst->satahba_mutex);
17900 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
17901 		mutex_exit(&sata_hba_inst->satahba_mutex);
17902 		mutex_enter(&sata_mutex);
17903 		sata_event_pending |= SATA_EVNT_MAIN;
17904 		mutex_exit(&sata_mutex);
17905 	}
17906 }
17907 
17908 /*
17909  * Port Multiplier Port Link Events processing.
17910  */
17911 static void
17912 sata_process_pmport_link_events(sata_hba_inst_t *sata_hba_inst,
17913     sata_address_t *saddr)
17914 {
17915 	sata_device_t sata_device;
17916 	sata_pmport_info_t *pmportinfo = NULL;
17917 	sata_drive_info_t *sdinfo = NULL;
17918 	uint32_t event_flags;
17919 	uint8_t cport = saddr->cport;
17920 	uint8_t pmport = saddr->pmport;
17921 	int rval;
17922 
17923 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17924 	    "Processing port %d:%d link event(s)",
17925 	    cport, pmport);
17926 
17927 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
17928 	mutex_enter(&pmportinfo->pmport_mutex);
17929 	event_flags = pmportinfo->pmport_event_flags;
17930 
17931 	/* Reset event flags first */
17932 	pmportinfo->pmport_event_flags &=
17933 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
17934 
17935 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
17936 	if ((pmportinfo->pmport_state &
17937 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
17938 		mutex_exit(&pmportinfo->pmport_mutex);
17939 		return;
17940 	}
17941 
17942 	/*
17943 	 * For the sanity sake get current port state.
17944 	 * Set device address only. Other sata_device fields should be
17945 	 * set by HBA driver.
17946 	 */
17947 	sata_device.satadev_rev = SATA_DEVICE_REV;
17948 	sata_device.satadev_addr = *saddr;
17949 	/*
17950 	 * We have to exit mutex, because the HBA probe port function may
17951 	 * block on its own mutex.
17952 	 */
17953 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
17954 	    saddr->pmport));
17955 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
17956 	    (SATA_DIP(sata_hba_inst), &sata_device);
17957 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
17958 	    saddr->pmport));
17959 	sata_update_pmport_info(sata_hba_inst, &sata_device);
17960 	if (rval != SATA_SUCCESS) {
17961 		/* Something went wrong? Fail the port */
17962 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
17963 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
17964 		    saddr->pmport));
17965 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17966 		    "SATA port %d:%d probing failed",
17967 		    saddr->cport, saddr->pmport));
17968 		/*
17969 		 * We may want to release device info structure, but
17970 		 * it is not necessary.
17971 		 */
17972 		return;
17973 	} else {
17974 		/* port probed successfully */
17975 		pmportinfo->pmport_state |=
17976 		    SATA_STATE_PROBED | SATA_STATE_READY;
17977 	}
17978 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst,
17979 	    saddr->cport, saddr->pmport));
17980 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst,
17981 	    saddr->cport, saddr->pmport));
17982 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
17983 
17984 		if ((sata_device.satadev_scr.sstatus &
17985 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
17986 			/* Ignore event */
17987 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17988 			    "Ignoring port %d:%d link established event - "
17989 			    "link down",
17990 			    saddr->cport, saddr->pmport);
17991 			goto linklost;
17992 		}
17993 
17994 		SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17995 		    "Processing port %d:%d link established event",
17996 		    cport, pmport);
17997 
17998 		/*
17999 		 * For the sanity sake check if a device is attached - check
18000 		 * return state of a port probing.
18001 		 */
18002 		if (sata_device.satadev_type != SATA_DTYPE_NONE &&
18003 		    sata_device.satadev_type != SATA_DTYPE_PMULT) {
18004 			/*
18005 			 * HBA port probe indicated that there is a device
18006 			 * attached. Check if the framework had device info
18007 			 * structure attached for this device.
18008 			 */
18009 			if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
18010 				ASSERT(SATA_PMPORTINFO_DRV_INFO(pmportinfo) !=
18011 				    NULL);
18012 
18013 				sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
18014 				if ((sdinfo->satadrv_type &
18015 				    SATA_VALID_DEV_TYPE) != 0) {
18016 					/*
18017 					 * Dev info structure is present.
18018 					 * If dev_type is set to known type in
18019 					 * the framework's drive info struct
18020 					 * then the device existed before and
18021 					 * the link was probably lost
18022 					 * momentarily - in such case
18023 					 * we may want to check device
18024 					 * identity.
18025 					 * Identity check is not supported now.
18026 					 *
18027 					 * Link established event
18028 					 * triggers device reset event.
18029 					 */
18030 					(SATA_PMPORTINFO_DRV_INFO(pmportinfo))->
18031 					    satadrv_event_flags |=
18032 					    SATA_EVNT_DEVICE_RESET;
18033 				}
18034 			} else if (pmportinfo->pmport_dev_type ==
18035 			    SATA_DTYPE_NONE) {
18036 				/*
18037 				 * We got new device attached! If HBA does not
18038 				 * generate device attached events, trigger it
18039 				 * here.
18040 				 */
18041 				if (!(SATA_FEATURES(sata_hba_inst) &
18042 				    SATA_CTLF_HOTPLUG)) {
18043 					pmportinfo->pmport_event_flags |=
18044 					    SATA_EVNT_DEVICE_ATTACHED;
18045 				}
18046 			}
18047 			/* Reset link lost timeout */
18048 			pmportinfo->pmport_link_lost_time = 0;
18049 		}
18050 	}
18051 linklost:
18052 	if (event_flags & SATA_EVNT_LINK_LOST) {
18053 #ifdef SATA_DEBUG
18054 		if (pmportinfo->pmport_link_lost_time == 0) {
18055 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18056 			    "Processing port %d:%d link lost event",
18057 			    saddr->cport, saddr->pmport);
18058 		}
18059 #endif
18060 		if ((sata_device.satadev_scr.sstatus &
18061 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
18062 			/* Ignore event */
18063 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18064 			    "Ignoring port %d:%d link lost event - link is up",
18065 			    saddr->cport, saddr->pmport);
18066 			goto done;
18067 		}
18068 		/*
18069 		 * When HBA cannot generate device attached/detached events,
18070 		 * we need to track link lost time and eventually generate
18071 		 * device detach event.
18072 		 */
18073 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
18074 			/* We are tracking link lost time */
18075 			if (pmportinfo->pmport_link_lost_time == 0) {
18076 				/* save current time (lbolt value) */
18077 				pmportinfo->pmport_link_lost_time =
18078 				    ddi_get_lbolt();
18079 				/* just keep link lost event */
18080 				pmportinfo->pmport_event_flags |=
18081 				    SATA_EVNT_LINK_LOST;
18082 			} else {
18083 				clock_t cur_time = ddi_get_lbolt();
18084 				if ((cur_time -
18085 				    pmportinfo->pmport_link_lost_time) >=
18086 				    drv_usectohz(
18087 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
18088 					/* trigger device detach event */
18089 					pmportinfo->pmport_event_flags |=
18090 					    SATA_EVNT_DEVICE_DETACHED;
18091 					pmportinfo->pmport_link_lost_time = 0;
18092 					SATADBG2(SATA_DBG_EVENTS,
18093 					    sata_hba_inst,
18094 					    "Triggering port %d:%d "
18095 					    "device detached event",
18096 					    saddr->cport, saddr->pmport);
18097 				} else {
18098 					/* keep link lost event */
18099 					pmportinfo->pmport_event_flags |=
18100 					    SATA_EVNT_LINK_LOST;
18101 				}
18102 			}
18103 		}
18104 		/*
18105 		 * We could change port state to disable/delay access to
18106 		 * the attached device until the link is recovered.
18107 		 */
18108 	}
18109 done:
18110 	event_flags = pmportinfo->pmport_event_flags;
18111 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
18112 	    saddr->pmport));
18113 	if (event_flags != 0) {
18114 		mutex_enter(&sata_hba_inst->satahba_mutex);
18115 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
18116 		mutex_exit(&sata_hba_inst->satahba_mutex);
18117 		mutex_enter(&sata_mutex);
18118 		sata_event_pending |= SATA_EVNT_MAIN;
18119 		mutex_exit(&sata_mutex);
18120 	}
18121 }
18122 
18123 /*
18124  * Device Detached Event processing.
18125  * Port is probed to find if a device is really gone. If so,
18126  * the device info structure is detached from the SATA port info structure
18127  * and released.
18128  * Port status is updated.
18129  *
18130  * NOTE: Port multiplier ports events are handled by
18131  * sata_process_pmdevice_detached()
18132  */
18133 static void
18134 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst,
18135     sata_address_t *saddr)
18136 {
18137 	sata_cport_info_t *cportinfo;
18138 	sata_pmport_info_t *pmportinfo;
18139 	sata_drive_info_t *sdevinfo;
18140 	sata_device_t sata_device;
18141 	sata_address_t pmport_addr;
18142 	char name[16];
18143 	uint8_t cport = saddr->cport;
18144 	int npmport;
18145 	int rval;
18146 
18147 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18148 	    "Processing port %d device detached", saddr->cport);
18149 
18150 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18151 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18152 	/* Clear event flag */
18153 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
18154 
18155 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
18156 	if ((cportinfo->cport_state &
18157 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
18158 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18159 		    cport_mutex);
18160 		return;
18161 	}
18162 	/* For sanity, re-probe the port */
18163 	sata_device.satadev_rev = SATA_DEVICE_REV;
18164 	sata_device.satadev_addr = *saddr;
18165 
18166 	/*
18167 	 * We have to exit mutex, because the HBA probe port function may
18168 	 * block on its own mutex.
18169 	 */
18170 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18171 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18172 	    (SATA_DIP(sata_hba_inst), &sata_device);
18173 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18174 	sata_update_port_info(sata_hba_inst, &sata_device);
18175 	if (rval != SATA_SUCCESS) {
18176 		/* Something went wrong? Fail the port */
18177 		cportinfo->cport_state = SATA_PSTATE_FAILED;
18178 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18179 		    cport_mutex);
18180 		SATA_LOG_D((sata_hba_inst, CE_WARN,
18181 		    "SATA port %d probing failed",
18182 		    saddr->cport));
18183 		/*
18184 		 * We may want to release device info structure, but
18185 		 * it is not necessary.
18186 		 */
18187 		return;
18188 	} else {
18189 		/* port probed successfully */
18190 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
18191 	}
18192 	/*
18193 	 * Check if a device is still attached. For sanity, check also
18194 	 * link status - if no link, there is no device.
18195 	 */
18196 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
18197 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
18198 	    SATA_DTYPE_NONE) {
18199 		/*
18200 		 * Device is still attached - ignore detach event.
18201 		 */
18202 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18203 		    cport_mutex);
18204 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18205 		    "Ignoring detach - device still attached to port %d",
18206 		    sata_device.satadev_addr.cport);
18207 		return;
18208 	}
18209 	/*
18210 	 * We need to detach and release device info structure here
18211 	 */
18212 	if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
18213 		/*
18214 		 * A port-multiplier is removed.
18215 		 *
18216 		 * Calling sata_process_pmdevice_detached() does not work
18217 		 * here. The port multiplier is gone, so we cannot probe
18218 		 * sub-port any more and all pmult-related data structure must
18219 		 * be de-allocated immediately. Following structure of every
18220 		 * implemented sub-port behind the pmult are required to
18221 		 * released.
18222 		 *
18223 		 *   - attachment point
18224 		 *   - target node
18225 		 *   - sata_drive_info
18226 		 *   - sata_pmport_info
18227 		 */
18228 		for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst,
18229 		    cport); npmport ++) {
18230 			SATADBG2(SATA_DBG_PMULT|SATA_DBG_EVENTS_PROC,
18231 			    sata_hba_inst,
18232 			    "Detaching target node at port %d:%d",
18233 			    cport, npmport);
18234 
18235 			mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
18236 
18237 			/* Remove attachment point. */
18238 			name[0] = '\0';
18239 			(void) sprintf(name, "%d.%d", cport, npmport);
18240 			ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name);
18241 			sata_log(sata_hba_inst, CE_NOTE,
18242 			    "Remove attachment point of port %d:%d",
18243 			    cport, npmport);
18244 
18245 			/* Remove target node */
18246 			pmport_addr.cport = cport;
18247 			pmport_addr.pmport = (uint8_t)npmport;
18248 			pmport_addr.qual = SATA_ADDR_PMPORT;
18249 			sata_remove_target_node(sata_hba_inst, &pmport_addr);
18250 
18251 			mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
18252 
18253 			/* Release sata_pmport_info & sata_drive_info. */
18254 			pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
18255 			    cport, npmport);
18256 			ASSERT(pmportinfo != NULL);
18257 
18258 			sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
18259 			if (sdevinfo != NULL) {
18260 				(void) kmem_free((void *) sdevinfo,
18261 				    sizeof (sata_drive_info_t));
18262 			}
18263 
18264 			/* Release sata_pmport_info at last */
18265 			(void) kmem_free((void *) pmportinfo,
18266 			    sizeof (sata_pmport_info_t));
18267 		}
18268 
18269 		/* Finally, release sata_pmult_info */
18270 		(void) kmem_free((void *)
18271 		    SATA_CPORTINFO_PMULT_INFO(cportinfo),
18272 		    sizeof (sata_pmult_info_t));
18273 		SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL;
18274 
18275 		sata_log(sata_hba_inst, CE_WARN,
18276 		    "SATA port-multiplier detached at port %d", cport);
18277 
18278 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
18279 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
18280 		    saddr->cport)->cport_mutex);
18281 	} else {
18282 		if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
18283 			sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
18284 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
18285 			(void) kmem_free((void *)sdevinfo,
18286 			    sizeof (sata_drive_info_t));
18287 		}
18288 		sata_log(sata_hba_inst, CE_WARN,
18289 		    "SATA device detached at port %d", cport);
18290 
18291 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
18292 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
18293 		    saddr->cport)->cport_mutex);
18294 
18295 		/*
18296 		 * Try to offline a device and remove target node
18297 		 * if it still exists
18298 		 */
18299 		sata_remove_target_node(sata_hba_inst, saddr);
18300 	}
18301 
18302 
18303 	/*
18304 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
18305 	 * with the hint: SE_HINT_REMOVE
18306 	 */
18307 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
18308 }
18309 
18310 /*
18311  * Port Multiplier Port Device Deattached Event processing.
18312  *
18313  * NOTE: No Mutex should be hold.
18314  */
18315 static void
18316 sata_process_pmdevice_detached(sata_hba_inst_t *sata_hba_inst,
18317     sata_address_t *saddr)
18318 {
18319 	sata_pmport_info_t *pmportinfo;
18320 	sata_drive_info_t *sdevinfo;
18321 	sata_device_t sata_device;
18322 	int rval;
18323 	uint8_t cport, pmport;
18324 
18325 	cport = saddr->cport;
18326 	pmport = saddr->pmport;
18327 
18328 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18329 	    "Processing port %d:%d device detached",
18330 	    cport, pmport);
18331 
18332 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
18333 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
18334 
18335 	/* Clear event flag */
18336 	pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
18337 
18338 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
18339 	if ((pmportinfo->pmport_state &
18340 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
18341 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
18342 		return;
18343 	}
18344 	/* For sanity, re-probe the port */
18345 	sata_device.satadev_rev = SATA_DEVICE_REV;
18346 	sata_device.satadev_addr = *saddr;
18347 
18348 	/*
18349 	 * We have to exit mutex, because the HBA probe port function may
18350 	 * block on its own mutex.
18351 	 */
18352 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
18353 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18354 	    (SATA_DIP(sata_hba_inst), &sata_device);
18355 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
18356 	sata_update_pmport_info(sata_hba_inst, &sata_device);
18357 	if (rval != SATA_SUCCESS) {
18358 		/* Something went wrong? Fail the port */
18359 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
18360 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
18361 		SATA_LOG_D((sata_hba_inst, CE_WARN,
18362 		    "SATA port %d:%d probing failed",
18363 		    saddr->pmport));
18364 		/*
18365 		 * We may want to release device info structure, but
18366 		 * it is not necessary.
18367 		 */
18368 		return;
18369 	} else {
18370 		/* port probed successfully */
18371 		pmportinfo->pmport_state |=
18372 		    SATA_STATE_PROBED | SATA_STATE_READY;
18373 	}
18374 	/*
18375 	 * Check if a device is still attached. For sanity, check also
18376 	 * link status - if no link, there is no device.
18377 	 */
18378 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
18379 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
18380 	    SATA_DTYPE_NONE) {
18381 		/*
18382 		 * Device is still attached - ignore detach event.
18383 		 */
18384 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
18385 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18386 		    "Ignoring detach - device still attached to port %d",
18387 		    sata_device.satadev_addr.pmport);
18388 		return;
18389 	}
18390 	/*
18391 	 * We need to detach and release device info structure here
18392 	 */
18393 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
18394 		sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
18395 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
18396 		(void) kmem_free((void *)sdevinfo,
18397 		    sizeof (sata_drive_info_t));
18398 	}
18399 	pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
18400 	/*
18401 	 * Device cannot be reached anymore, even if the target node may be
18402 	 * still present.
18403 	 */
18404 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
18405 
18406 	/*
18407 	 * Try to offline a device and remove target node if it still exists
18408 	 */
18409 	sata_remove_target_node(sata_hba_inst, saddr);
18410 
18411 	/*
18412 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
18413 	 * with the hint: SE_HINT_REMOVE
18414 	 */
18415 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
18416 }
18417 
18418 
18419 /*
18420  * Device Attached Event processing.
18421  * Port state is checked to verify that a device is really attached. If so,
18422  * the device info structure is created and attached to the SATA port info
18423  * structure.
18424  *
18425  * If attached device cannot be identified or set-up, the retry for the
18426  * attach processing is set-up. Subsequent daemon run would try again to
18427  * identify the device, until the time limit is reached
18428  * (SATA_DEV_IDENTIFY_TIMEOUT).
18429  *
18430  * This function cannot be called in interrupt context (it may sleep).
18431  *
18432  * NOTE: Port multiplier ports events are handled by
18433  * sata_process_pmdevice_attached()
18434  */
18435 static void
18436 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst,
18437     sata_address_t *saddr)
18438 {
18439 	sata_cport_info_t *cportinfo = NULL;
18440 	sata_drive_info_t *sdevinfo = NULL;
18441 	sata_pmult_info_t *pmultinfo = NULL;
18442 	sata_pmport_info_t *pmportinfo = NULL;
18443 	sata_device_t sata_device;
18444 	dev_info_t *tdip;
18445 	uint32_t event_flags = 0, pmult_event_flags = 0;
18446 	int rval;
18447 	int npmport;
18448 
18449 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18450 	    "Processing port %d device attached", saddr->cport);
18451 
18452 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18453 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18454 
18455 	/* Clear attach event flag first */
18456 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
18457 
18458 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
18459 	if ((cportinfo->cport_state &
18460 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
18461 		cportinfo->cport_dev_attach_time = 0;
18462 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18463 		    cport_mutex);
18464 		return;
18465 	}
18466 
18467 	/*
18468 	 * If the sata_drive_info structure is found attached to the port info,
18469 	 * despite the fact the device was removed and now it is re-attached,
18470 	 * the old drive info structure was not removed.
18471 	 * Arbitrarily release device info structure.
18472 	 */
18473 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
18474 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
18475 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
18476 		(void) kmem_free((void *)sdevinfo,
18477 		    sizeof (sata_drive_info_t));
18478 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18479 		    "Arbitrarily detaching old device info.", NULL);
18480 	}
18481 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
18482 
18483 	/* For sanity, re-probe the port */
18484 	sata_device.satadev_rev = SATA_DEVICE_REV;
18485 	sata_device.satadev_addr = *saddr;
18486 
18487 	/*
18488 	 * We have to exit mutex, because the HBA probe port function may
18489 	 * block on its own mutex.
18490 	 */
18491 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18492 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18493 	    (SATA_DIP(sata_hba_inst), &sata_device);
18494 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18495 	sata_update_port_info(sata_hba_inst, &sata_device);
18496 	if (rval != SATA_SUCCESS) {
18497 		/* Something went wrong? Fail the port */
18498 		cportinfo->cport_state = SATA_PSTATE_FAILED;
18499 		cportinfo->cport_dev_attach_time = 0;
18500 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18501 		    cport_mutex);
18502 		SATA_LOG_D((sata_hba_inst, CE_WARN,
18503 		    "SATA port %d probing failed",
18504 		    saddr->cport));
18505 		return;
18506 	} else {
18507 		/* port probed successfully */
18508 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
18509 	}
18510 	/*
18511 	 * Check if a device is still attached. For sanity, check also
18512 	 * link status - if no link, there is no device.
18513 	 */
18514 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
18515 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
18516 	    SATA_DTYPE_NONE) {
18517 		/*
18518 		 * No device - ignore attach event.
18519 		 */
18520 		cportinfo->cport_dev_attach_time = 0;
18521 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18522 		    cport_mutex);
18523 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18524 		    "Ignoring attach - no device connected to port %d",
18525 		    sata_device.satadev_addr.cport);
18526 		return;
18527 	}
18528 
18529 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18530 	/*
18531 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
18532 	 * with the hint: SE_HINT_INSERT
18533 	 */
18534 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
18535 
18536 	/*
18537 	 * Port reprobing will take care of the creation of the device
18538 	 * info structure and determination of the device type.
18539 	 */
18540 	sata_device.satadev_addr = *saddr;
18541 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
18542 	    SATA_DEV_IDENTIFY_NORETRY);
18543 
18544 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18545 	    cport_mutex);
18546 	if ((cportinfo->cport_state & SATA_STATE_READY) &&
18547 	    (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) {
18548 		/* Some device is attached to the port */
18549 		if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) {
18550 			/*
18551 			 * A device was not successfully attached.
18552 			 * Track retry time for device identification.
18553 			 */
18554 			if (cportinfo->cport_dev_attach_time != 0) {
18555 				clock_t cur_time = ddi_get_lbolt();
18556 				/*
18557 				 * If the retry time limit was not exceeded,
18558 				 * reinstate attach event.
18559 				 */
18560 				if ((cur_time -
18561 				    cportinfo->cport_dev_attach_time) <
18562 				    drv_usectohz(
18563 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
18564 					/* OK, restore attach event */
18565 					cportinfo->cport_event_flags |=
18566 					    SATA_EVNT_DEVICE_ATTACHED;
18567 				} else {
18568 					/* Timeout - cannot identify device */
18569 					cportinfo->cport_dev_attach_time = 0;
18570 					sata_log(sata_hba_inst,
18571 					    CE_WARN,
18572 					    "Could not identify SATA device "
18573 					    "at port %d",
18574 					    saddr->cport);
18575 				}
18576 			} else {
18577 				/*
18578 				 * Start tracking time for device
18579 				 * identification.
18580 				 * Save current time (lbolt value).
18581 				 */
18582 				cportinfo->cport_dev_attach_time =
18583 				    ddi_get_lbolt();
18584 				/* Restore attach event */
18585 				cportinfo->cport_event_flags |=
18586 				    SATA_EVNT_DEVICE_ATTACHED;
18587 			}
18588 		} else if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
18589 			cportinfo->cport_dev_attach_time = 0;
18590 			sata_log(sata_hba_inst, CE_NOTE,
18591 			    "SATA port-multiplier detected at port %d",
18592 			    saddr->cport);
18593 
18594 			if (SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL) {
18595 				/* Log the info of new port multiplier */
18596 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
18597 				    saddr->cport)->cport_mutex);
18598 				sata_show_pmult_info(sata_hba_inst,
18599 				    &sata_device);
18600 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
18601 				    saddr->cport)->cport_mutex);
18602 			}
18603 
18604 			ASSERT(SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL);
18605 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
18606 			for (npmport = 0; npmport <
18607 			    pmultinfo->pmult_num_dev_ports; npmport++) {
18608 				pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
18609 				    saddr->cport, npmport);
18610 				ASSERT(pmportinfo != NULL);
18611 
18612 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
18613 				    saddr->cport)->cport_mutex);
18614 				mutex_enter(&pmportinfo->pmport_mutex);
18615 				/* Marked all pmports with link events. */
18616 				pmportinfo->pmport_event_flags =
18617 				    SATA_EVNT_LINK_ESTABLISHED;
18618 				pmult_event_flags |=
18619 				    pmportinfo->pmport_event_flags;
18620 				mutex_exit(&pmportinfo->pmport_mutex);
18621 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
18622 				    saddr->cport)->cport_mutex);
18623 			}
18624 			/* Auto-online is not available for PMult now. */
18625 
18626 		} else {
18627 			/*
18628 			 * If device was successfully attached, the subsequent
18629 			 * action depends on a state of the
18630 			 * sata_auto_online variable. If it is set to zero.
18631 			 * an explicit 'configure' command will be needed to
18632 			 * configure it. If its value is non-zero, we will
18633 			 * attempt to online (configure) the device.
18634 			 * First, log the message indicating that a device
18635 			 * was attached.
18636 			 */
18637 			cportinfo->cport_dev_attach_time = 0;
18638 			sata_log(sata_hba_inst, CE_WARN,
18639 			    "SATA device detected at port %d", saddr->cport);
18640 
18641 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
18642 				sata_drive_info_t new_sdinfo;
18643 
18644 				/* Log device info data */
18645 				new_sdinfo = *(SATA_CPORTINFO_DRV_INFO(
18646 				    cportinfo));
18647 				sata_show_drive_info(sata_hba_inst,
18648 				    &new_sdinfo);
18649 			}
18650 
18651 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
18652 			    saddr->cport)->cport_mutex);
18653 
18654 			/*
18655 			 * Make sure that there is no target node for that
18656 			 * device. If so, release it. It should not happen,
18657 			 * unless we had problem removing the node when
18658 			 * device was detached.
18659 			 */
18660 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
18661 			    saddr->cport, saddr->pmport);
18662 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
18663 			    saddr->cport)->cport_mutex);
18664 			if (tdip != NULL) {
18665 
18666 #ifdef SATA_DEBUG
18667 				if ((cportinfo->cport_event_flags &
18668 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
18669 					sata_log(sata_hba_inst, CE_WARN,
18670 					    "sata_process_device_attached: "
18671 					    "old device target node exists!");
18672 #endif
18673 				/*
18674 				 * target node exists - try to unconfigure
18675 				 * device and remove the node.
18676 				 */
18677 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
18678 				    saddr->cport)->cport_mutex);
18679 				rval = ndi_devi_offline(tdip,
18680 				    NDI_DEVI_REMOVE);
18681 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
18682 				    saddr->cport)->cport_mutex);
18683 
18684 				if (rval == NDI_SUCCESS) {
18685 					cportinfo->cport_event_flags &=
18686 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
18687 					cportinfo->cport_tgtnode_clean = B_TRUE;
18688 				} else {
18689 					/*
18690 					 * PROBLEM - the target node remained
18691 					 * and it belongs to a previously
18692 					 * attached device.
18693 					 * This happens when the file was open
18694 					 * or the node was waiting for
18695 					 * resources at the time the
18696 					 * associated device was removed.
18697 					 * Instruct event daemon to retry the
18698 					 * cleanup later.
18699 					 */
18700 					sata_log(sata_hba_inst,
18701 					    CE_WARN,
18702 					    "Application(s) accessing "
18703 					    "previously attached SATA "
18704 					    "device have to release "
18705 					    "it before newly inserted "
18706 					    "device can be made accessible.",
18707 					    saddr->cport);
18708 					cportinfo->cport_event_flags |=
18709 					    SATA_EVNT_TARGET_NODE_CLEANUP;
18710 					cportinfo->cport_tgtnode_clean =
18711 					    B_FALSE;
18712 				}
18713 			}
18714 			if (sata_auto_online != 0) {
18715 				cportinfo->cport_event_flags |=
18716 				    SATA_EVNT_AUTOONLINE_DEVICE;
18717 			}
18718 
18719 		}
18720 	} else {
18721 		cportinfo->cport_dev_attach_time = 0;
18722 	}
18723 
18724 	event_flags = cportinfo->cport_event_flags;
18725 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18726 	if (event_flags != 0 || pmult_event_flags != 0) {
18727 		mutex_enter(&sata_hba_inst->satahba_mutex);
18728 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
18729 		mutex_exit(&sata_hba_inst->satahba_mutex);
18730 		mutex_enter(&sata_mutex);
18731 		sata_event_pending |= SATA_EVNT_MAIN;
18732 		mutex_exit(&sata_mutex);
18733 	}
18734 }
18735 
18736 /*
18737  * Port Multiplier Port Device Attached Event processing.
18738  *
18739  * NOTE: No Mutex should be hold.
18740  */
18741 static void
18742 sata_process_pmdevice_attached(sata_hba_inst_t *sata_hba_inst,
18743     sata_address_t *saddr)
18744 {
18745 	sata_pmport_info_t *pmportinfo;
18746 	sata_drive_info_t *sdinfo;
18747 	sata_device_t sata_device;
18748 	dev_info_t *tdip;
18749 	uint32_t event_flags;
18750 	uint8_t cport = saddr->cport;
18751 	uint8_t pmport = saddr->pmport;
18752 	int rval;
18753 
18754 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18755 	    "Processing port %d:%d device attached", cport, pmport);
18756 
18757 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
18758 
18759 	mutex_enter(&pmportinfo->pmport_mutex);
18760 
18761 	/* Clear attach event flag first */
18762 	pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
18763 
18764 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
18765 	if ((pmportinfo->pmport_state &
18766 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
18767 		pmportinfo->pmport_dev_attach_time = 0;
18768 		mutex_exit(&pmportinfo->pmport_mutex);
18769 		return;
18770 	}
18771 
18772 	/*
18773 	 * If the sata_drive_info structure is found attached to the port info,
18774 	 * despite the fact the device was removed and now it is re-attached,
18775 	 * the old drive info structure was not removed.
18776 	 * Arbitrarily release device info structure.
18777 	 */
18778 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
18779 		sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
18780 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
18781 		(void) kmem_free((void *)sdinfo,
18782 		    sizeof (sata_drive_info_t));
18783 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18784 		    "Arbitrarily detaching old device info.", NULL);
18785 	}
18786 	pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
18787 
18788 	/* For sanity, re-probe the port */
18789 	sata_device.satadev_rev = SATA_DEVICE_REV;
18790 	sata_device.satadev_addr = *saddr;
18791 
18792 	/*
18793 	 * We have to exit mutex, because the HBA probe port function may
18794 	 * block on its own mutex.
18795 	 */
18796 	mutex_exit(&pmportinfo->pmport_mutex);
18797 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18798 	    (SATA_DIP(sata_hba_inst), &sata_device);
18799 	mutex_enter(&pmportinfo->pmport_mutex);
18800 
18801 	sata_update_pmport_info(sata_hba_inst, &sata_device);
18802 	if (rval != SATA_SUCCESS) {
18803 		/* Something went wrong? Fail the port */
18804 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
18805 		pmportinfo->pmport_dev_attach_time = 0;
18806 		mutex_exit(&pmportinfo->pmport_mutex);
18807 		SATA_LOG_D((sata_hba_inst, CE_WARN,
18808 		    "SATA port %d:%d probing failed", cport, pmport));
18809 		return;
18810 	} else {
18811 		/* pmport probed successfully */
18812 		pmportinfo->pmport_state |=
18813 		    SATA_STATE_PROBED | SATA_STATE_READY;
18814 	}
18815 	/*
18816 	 * Check if a device is still attached. For sanity, check also
18817 	 * link status - if no link, there is no device.
18818 	 */
18819 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
18820 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
18821 	    SATA_DTYPE_NONE) {
18822 		/*
18823 		 * No device - ignore attach event.
18824 		 */
18825 		pmportinfo->pmport_dev_attach_time = 0;
18826 		mutex_exit(&pmportinfo->pmport_mutex);
18827 		SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18828 		    "Ignoring attach - no device connected to port %d:%d",
18829 		    cport, pmport);
18830 		return;
18831 	}
18832 
18833 	mutex_exit(&pmportinfo->pmport_mutex);
18834 	/*
18835 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
18836 	 * with the hint: SE_HINT_INSERT
18837 	 */
18838 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
18839 
18840 	/*
18841 	 * Port reprobing will take care of the creation of the device
18842 	 * info structure and determination of the device type.
18843 	 */
18844 	sata_device.satadev_addr = *saddr;
18845 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
18846 	    SATA_DEV_IDENTIFY_NORETRY);
18847 
18848 	mutex_enter(&pmportinfo->pmport_mutex);
18849 	if ((pmportinfo->pmport_state & SATA_STATE_READY) &&
18850 	    (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE)) {
18851 		/* Some device is attached to the port */
18852 		if (pmportinfo->pmport_dev_type == SATA_DTYPE_UNKNOWN) {
18853 			/*
18854 			 * A device was not successfully attached.
18855 			 * Track retry time for device identification.
18856 			 */
18857 			if (pmportinfo->pmport_dev_attach_time != 0) {
18858 				clock_t cur_time = ddi_get_lbolt();
18859 				/*
18860 				 * If the retry time limit was not exceeded,
18861 				 * reinstate attach event.
18862 				 */
18863 				if ((cur_time -
18864 				    pmportinfo->pmport_dev_attach_time) <
18865 				    drv_usectohz(
18866 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
18867 					/* OK, restore attach event */
18868 					pmportinfo->pmport_event_flags |=
18869 					    SATA_EVNT_DEVICE_ATTACHED;
18870 				} else {
18871 					/* Timeout - cannot identify device */
18872 					pmportinfo->pmport_dev_attach_time = 0;
18873 					sata_log(sata_hba_inst, CE_WARN,
18874 					    "Could not identify SATA device "
18875 					    "at port %d:%d",
18876 					    cport, pmport);
18877 				}
18878 			} else {
18879 				/*
18880 				 * Start tracking time for device
18881 				 * identification.
18882 				 * Save current time (lbolt value).
18883 				 */
18884 				pmportinfo->pmport_dev_attach_time =
18885 				    ddi_get_lbolt();
18886 				/* Restore attach event */
18887 				pmportinfo->pmport_event_flags |=
18888 				    SATA_EVNT_DEVICE_ATTACHED;
18889 			}
18890 		} else {
18891 			/*
18892 			 * If device was successfully attached, the subsequent
18893 			 * action depends on a state of the
18894 			 * sata_auto_online variable. If it is set to zero.
18895 			 * an explicit 'configure' command will be needed to
18896 			 * configure it. If its value is non-zero, we will
18897 			 * attempt to online (configure) the device.
18898 			 * First, log the message indicating that a device
18899 			 * was attached.
18900 			 */
18901 			pmportinfo->pmport_dev_attach_time = 0;
18902 			sata_log(sata_hba_inst, CE_WARN,
18903 			    "SATA device detected at port %d:%d",
18904 			    cport, pmport);
18905 
18906 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
18907 				sata_drive_info_t new_sdinfo;
18908 
18909 				/* Log device info data */
18910 				new_sdinfo = *(SATA_PMPORTINFO_DRV_INFO(
18911 				    pmportinfo));
18912 				sata_show_drive_info(sata_hba_inst,
18913 				    &new_sdinfo);
18914 			}
18915 
18916 			mutex_exit(&pmportinfo->pmport_mutex);
18917 
18918 			/*
18919 			 * Make sure that there is no target node for that
18920 			 * device. If so, release it. It should not happen,
18921 			 * unless we had problem removing the node when
18922 			 * device was detached.
18923 			 */
18924 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
18925 			    saddr->cport, saddr->pmport);
18926 			mutex_enter(&pmportinfo->pmport_mutex);
18927 			if (tdip != NULL) {
18928 
18929 #ifdef SATA_DEBUG
18930 				if ((pmportinfo->pmport_event_flags &
18931 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
18932 					sata_log(sata_hba_inst, CE_WARN,
18933 					    "sata_process_device_attached: "
18934 					    "old device target node exists!");
18935 #endif
18936 				/*
18937 				 * target node exists - try to unconfigure
18938 				 * device and remove the node.
18939 				 */
18940 				mutex_exit(&pmportinfo->pmport_mutex);
18941 				rval = ndi_devi_offline(tdip,
18942 				    NDI_DEVI_REMOVE);
18943 				mutex_enter(&pmportinfo->pmport_mutex);
18944 
18945 				if (rval == NDI_SUCCESS) {
18946 					pmportinfo->pmport_event_flags &=
18947 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
18948 					pmportinfo->pmport_tgtnode_clean =
18949 					    B_TRUE;
18950 				} else {
18951 					/*
18952 					 * PROBLEM - the target node remained
18953 					 * and it belongs to a previously
18954 					 * attached device.
18955 					 * This happens when the file was open
18956 					 * or the node was waiting for
18957 					 * resources at the time the
18958 					 * associated device was removed.
18959 					 * Instruct event daemon to retry the
18960 					 * cleanup later.
18961 					 */
18962 					sata_log(sata_hba_inst,
18963 					    CE_WARN,
18964 					    "Application(s) accessing "
18965 					    "previously attached SATA "
18966 					    "device have to release "
18967 					    "it before newly inserted "
18968 					    "device can be made accessible."
18969 					    "at port %d:%d",
18970 					    cport, pmport);
18971 					pmportinfo->pmport_event_flags |=
18972 					    SATA_EVNT_TARGET_NODE_CLEANUP;
18973 					pmportinfo->pmport_tgtnode_clean =
18974 					    B_FALSE;
18975 				}
18976 			}
18977 			if (sata_auto_online != 0) {
18978 				pmportinfo->pmport_event_flags |=
18979 				    SATA_EVNT_AUTOONLINE_DEVICE;
18980 			}
18981 
18982 		}
18983 	} else {
18984 		pmportinfo->pmport_dev_attach_time = 0;
18985 	}
18986 
18987 	event_flags = pmportinfo->pmport_event_flags;
18988 	mutex_exit(&pmportinfo->pmport_mutex);
18989 	if (event_flags != 0) {
18990 		mutex_enter(&sata_hba_inst->satahba_mutex);
18991 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
18992 		mutex_exit(&sata_hba_inst->satahba_mutex);
18993 		mutex_enter(&sata_mutex);
18994 		sata_event_pending |= SATA_EVNT_MAIN;
18995 		mutex_exit(&sata_mutex);
18996 	}
18997 
18998 	/* clear the reset_in_progress events */
18999 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
19000 		if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) {
19001 			/* must clear flags on cport */
19002 			sata_pmult_info_t *pminfo =
19003 			    SATA_PMULT_INFO(sata_hba_inst,
19004 			    saddr->cport);
19005 			pminfo->pmult_event_flags |=
19006 			    SATA_EVNT_CLEAR_DEVICE_RESET;
19007 		}
19008 	}
19009 }
19010 
19011 /*
19012  * Device Target Node Cleanup Event processing.
19013  * If the target node associated with a sata port device is in
19014  * DEVI_DEVICE_REMOVED state, an attempt is made to remove it.
19015  * If the target node cannot be removed, the event flag is left intact,
19016  * so that event daemon may re-run this function later.
19017  *
19018  * This function cannot be called in interrupt context (it may sleep).
19019  *
19020  * NOTE: Processes cport events only, not port multiplier ports.
19021  */
19022 static void
19023 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
19024     sata_address_t *saddr)
19025 {
19026 	sata_cport_info_t *cportinfo;
19027 	dev_info_t *tdip;
19028 
19029 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19030 	    "Processing port %d device target node cleanup", saddr->cport);
19031 
19032 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19033 
19034 	/*
19035 	 * Check if there is target node for that device and it is in the
19036 	 * DEVI_DEVICE_REMOVED state. If so, release it.
19037 	 */
19038 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport,
19039 	    saddr->pmport);
19040 	if (tdip != NULL) {
19041 		/*
19042 		 * target node exists - check if it is target node of
19043 		 * a removed device.
19044 		 */
19045 		if (sata_check_device_removed(tdip) == B_TRUE) {
19046 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19047 			    "sata_process_target_node_cleanup: "
19048 			    "old device target node exists!", NULL);
19049 			/*
19050 			 * Unconfigure and remove the target node
19051 			 */
19052 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) ==
19053 			    NDI_SUCCESS) {
19054 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19055 				    saddr->cport)->cport_mutex);
19056 				cportinfo->cport_event_flags &=
19057 				    ~SATA_EVNT_TARGET_NODE_CLEANUP;
19058 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19059 				    saddr->cport)->cport_mutex);
19060 				return;
19061 			}
19062 			/*
19063 			 * Event daemon will retry the cleanup later.
19064 			 */
19065 			mutex_enter(&sata_hba_inst->satahba_mutex);
19066 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19067 			mutex_exit(&sata_hba_inst->satahba_mutex);
19068 			mutex_enter(&sata_mutex);
19069 			sata_event_pending |= SATA_EVNT_MAIN;
19070 			mutex_exit(&sata_mutex);
19071 		}
19072 	} else {
19073 		if (saddr->qual == SATA_ADDR_CPORT ||
19074 		    saddr->qual == SATA_ADDR_DCPORT) {
19075 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19076 			    saddr->cport)->cport_mutex);
19077 			cportinfo->cport_event_flags &=
19078 			    ~SATA_EVNT_TARGET_NODE_CLEANUP;
19079 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19080 			    saddr->cport)->cport_mutex);
19081 		} else {
19082 			/* sanity check */
19083 			if (SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) !=
19084 			    SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst,
19085 			    saddr->cport) == NULL)
19086 				return;
19087 			if (SATA_PMPORT_INFO(sata_hba_inst, saddr->cport,
19088 			    saddr->pmport) == NULL)
19089 				return;
19090 
19091 			mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst,
19092 			    saddr->cport, saddr->pmport)->pmport_mutex);
19093 			SATA_PMPORT_INFO(sata_hba_inst, saddr->cport,
19094 			    saddr->pmport)->pmport_event_flags &=
19095 			    ~SATA_EVNT_TARGET_NODE_CLEANUP;
19096 			mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst,
19097 			    saddr->cport, saddr->pmport)->pmport_mutex);
19098 		}
19099 	}
19100 }
19101 
19102 /*
19103  * Device AutoOnline Event processing.
19104  * If attached device is to be onlined, an attempt is made to online this
19105  * device, but only if there is no lingering (old) target node present.
19106  * If the device cannot be onlined, the event flag is left intact,
19107  * so that event daemon may re-run this function later.
19108  *
19109  * This function cannot be called in interrupt context (it may sleep).
19110  *
19111  * NOTE: Processes cport events only, not port multiplier ports.
19112  */
19113 static void
19114 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst,
19115     sata_address_t *saddr)
19116 {
19117 	sata_cport_info_t *cportinfo;
19118 	sata_drive_info_t *sdinfo;
19119 	sata_device_t sata_device;
19120 	dev_info_t *tdip;
19121 
19122 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19123 	    "Processing port %d attached device auto-onlining", saddr->cport);
19124 
19125 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19126 
19127 	/*
19128 	 * Check if device is present and recognized. If not, reset event.
19129 	 */
19130 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19131 	if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
19132 		/* Nothing to online */
19133 		cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
19134 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19135 		    saddr->cport)->cport_mutex);
19136 		return;
19137 	}
19138 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19139 
19140 	/*
19141 	 * Check if there is target node for this device and if it is in the
19142 	 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep
19143 	 * the event for later processing.
19144 	 */
19145 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport,
19146 	    saddr->pmport);
19147 	if (tdip != NULL) {
19148 		/*
19149 		 * target node exists - check if it is target node of
19150 		 * a removed device.
19151 		 */
19152 		if (sata_check_device_removed(tdip) == B_TRUE) {
19153 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19154 			    "sata_process_device_autoonline: "
19155 			    "old device target node exists!", NULL);
19156 			/*
19157 			 * Event daemon will retry device onlining later.
19158 			 */
19159 			mutex_enter(&sata_hba_inst->satahba_mutex);
19160 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19161 			mutex_exit(&sata_hba_inst->satahba_mutex);
19162 			mutex_enter(&sata_mutex);
19163 			sata_event_pending |= SATA_EVNT_MAIN;
19164 			mutex_exit(&sata_mutex);
19165 			return;
19166 		}
19167 		/*
19168 		 * If the target node is not in the 'removed" state, assume
19169 		 * that it belongs to this device. There is nothing more to do,
19170 		 * but reset the event.
19171 		 */
19172 	} else {
19173 
19174 		/*
19175 		 * Try to online the device
19176 		 * If there is any reset-related event, remove it. We are
19177 		 * configuring the device and no state restoring is needed.
19178 		 */
19179 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19180 		    saddr->cport)->cport_mutex);
19181 		sata_device.satadev_addr = *saddr;
19182 		if (saddr->qual == SATA_ADDR_CPORT)
19183 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
19184 		else
19185 			sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
19186 		sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
19187 		if (sdinfo != NULL) {
19188 			if (sdinfo->satadrv_event_flags &
19189 			    (SATA_EVNT_DEVICE_RESET |
19190 			    SATA_EVNT_INPROC_DEVICE_RESET))
19191 				sdinfo->satadrv_event_flags = 0;
19192 			sdinfo->satadrv_event_flags |=
19193 			    SATA_EVNT_CLEAR_DEVICE_RESET;
19194 
19195 			/* Need to create a new target node. */
19196 			cportinfo->cport_tgtnode_clean = B_TRUE;
19197 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19198 			    saddr->cport)->cport_mutex);
19199 			tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
19200 			    sata_hba_inst, &sata_device.satadev_addr);
19201 			if (tdip == NULL) {
19202 				/*
19203 				 * Configure (onlining) failed.
19204 				 * We will NOT retry
19205 				 */
19206 				SATA_LOG_D((sata_hba_inst, CE_WARN,
19207 				    "sata_process_device_autoonline: "
19208 				    "configuring SATA device at port %d failed",
19209 				    saddr->cport));
19210 			}
19211 		} else {
19212 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19213 			    saddr->cport)->cport_mutex);
19214 		}
19215 
19216 	}
19217 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19218 	cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
19219 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19220 	    saddr->cport)->cport_mutex);
19221 }
19222 
19223 
19224 static void
19225 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr,
19226     int hint)
19227 {
19228 	char ap[MAXPATHLEN];
19229 	nvlist_t *ev_attr_list = NULL;
19230 	int err;
19231 
19232 	/* Allocate and build sysevent attribute list */
19233 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP);
19234 	if (err != 0) {
19235 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19236 		    "sata_gen_sysevent: "
19237 		    "cannot allocate memory for sysevent attributes\n"));
19238 		return;
19239 	}
19240 	/* Add hint attribute */
19241 	err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint));
19242 	if (err != 0) {
19243 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19244 		    "sata_gen_sysevent: "
19245 		    "failed to add DR_HINT attr for sysevent"));
19246 		nvlist_free(ev_attr_list);
19247 		return;
19248 	}
19249 	/*
19250 	 * Add AP attribute.
19251 	 * Get controller pathname and convert it into AP pathname by adding
19252 	 * a target number.
19253 	 */
19254 	(void) snprintf(ap, MAXPATHLEN, "/devices");
19255 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap));
19256 	(void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d",
19257 	    SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual));
19258 
19259 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap);
19260 	if (err != 0) {
19261 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19262 		    "sata_gen_sysevent: "
19263 		    "failed to add DR_AP_ID attr for sysevent"));
19264 		nvlist_free(ev_attr_list);
19265 		return;
19266 	}
19267 
19268 	/* Generate/log sysevent */
19269 	err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR,
19270 	    ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP);
19271 	if (err != DDI_SUCCESS) {
19272 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19273 		    "sata_gen_sysevent: "
19274 		    "cannot log sysevent, err code %x\n", err));
19275 	}
19276 
19277 	nvlist_free(ev_attr_list);
19278 }
19279 
19280 
19281 
19282 
19283 /*
19284  * Set DEVI_DEVICE_REMOVED state in the SATA device target node.
19285  */
19286 static void
19287 sata_set_device_removed(dev_info_t *tdip)
19288 {
19289 	int circ;
19290 
19291 	ASSERT(tdip != NULL);
19292 
19293 	ndi_devi_enter(tdip, &circ);
19294 	mutex_enter(&DEVI(tdip)->devi_lock);
19295 	DEVI_SET_DEVICE_REMOVED(tdip);
19296 	mutex_exit(&DEVI(tdip)->devi_lock);
19297 	ndi_devi_exit(tdip, circ);
19298 }
19299 
19300 
19301 /*
19302  * Set internal event instructing event daemon to try
19303  * to perform the target node cleanup.
19304  */
19305 static void
19306 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
19307     sata_address_t *saddr)
19308 {
19309 	if (saddr->qual == SATA_ADDR_CPORT ||
19310 	    saddr->qual == SATA_ADDR_DCPORT) {
19311 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19312 		    saddr->cport)->cport_mutex);
19313 		SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |=
19314 		    SATA_EVNT_TARGET_NODE_CLEANUP;
19315 		SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19316 		    cport_tgtnode_clean = B_FALSE;
19317 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19318 		    saddr->cport)->cport_mutex);
19319 	} else {
19320 		mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst,
19321 		    saddr->cport, saddr->pmport)->pmport_mutex);
19322 		SATA_PMPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport,
19323 		    saddr->pmport) |= SATA_EVNT_TARGET_NODE_CLEANUP;
19324 		SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, saddr->pmport)->
19325 		    pmport_tgtnode_clean = B_FALSE;
19326 		mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst,
19327 		    saddr->cport, saddr->pmport)->pmport_mutex);
19328 	}
19329 	mutex_enter(&sata_hba_inst->satahba_mutex);
19330 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19331 	mutex_exit(&sata_hba_inst->satahba_mutex);
19332 	mutex_enter(&sata_mutex);
19333 	sata_event_pending |= SATA_EVNT_MAIN;
19334 	mutex_exit(&sata_mutex);
19335 }
19336 
19337 
19338 /*
19339  * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state,
19340  * i.e. check if the target node state indicates that it belongs to a removed
19341  * device.
19342  *
19343  * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state,
19344  * B_FALSE otherwise.
19345  */
19346 static boolean_t
19347 sata_check_device_removed(dev_info_t *tdip)
19348 {
19349 	ASSERT(tdip != NULL);
19350 
19351 	if (DEVI_IS_DEVICE_REMOVED(tdip))
19352 		return (B_TRUE);
19353 	else
19354 		return (B_FALSE);
19355 }
19356 
19357 /* ************************ FAULT INJECTTION **************************** */
19358 
19359 #ifdef SATA_INJECT_FAULTS
19360 
19361 static	uint32_t sata_fault_count = 0;
19362 static	uint32_t sata_fault_suspend_count = 0;
19363 
19364 /*
19365  * Inject sata pkt fault
19366  * It modifies returned values of the sata packet.
19367  * It returns immediately if:
19368  * pkt fault injection is not enabled (via sata_inject_fault,
19369  * sata_inject_fault_count), or invalid fault is specified (sata_fault_type),
19370  * or pkt does not contain command to be faulted (set in sata_fault_cmd), or
19371  * pkt is not directed to specified fault controller/device
19372  * (sata_fault_ctrl_dev and sata_fault_device).
19373  * If fault controller is not specified, fault injection applies to all
19374  * controllers and devices.
19375  *
19376  * First argument is the pointer to the executed sata packet.
19377  * Second argument is a pointer to a value returned by the HBA tran_start
19378  * function.
19379  * Third argument specifies injected error. Injected sata packet faults
19380  * are the satapkt_reason values.
19381  * SATA_PKT_BUSY		-1	Not completed, busy
19382  * SATA_PKT_DEV_ERROR		1	Device reported error
19383  * SATA_PKT_QUEUE_FULL		2	Not accepted, queue full
19384  * SATA_PKT_PORT_ERROR		3	Not completed, port error
19385  * SATA_PKT_CMD_UNSUPPORTED	4	Cmd unsupported
19386  * SATA_PKT_ABORTED		5	Aborted by request
19387  * SATA_PKT_TIMEOUT		6	Operation timeut
19388  * SATA_PKT_RESET		7	Aborted by reset request
19389  *
19390  * Additional global variables affecting the execution:
19391  *
19392  * sata_inject_fault_count variable specifies number of times in row the
19393  * error is injected. Value of -1 specifies permanent fault, ie. every time
19394  * the fault injection point is reached, the fault is injected and a pause
19395  * between fault injection specified by sata_inject_fault_pause_count is
19396  * ignored). Fault injection routine decrements sata_inject_fault_count
19397  * (if greater than zero) until it reaches 0. No fault is injected when
19398  * sata_inject_fault_count is 0 (zero).
19399  *
19400  * sata_inject_fault_pause_count variable specifies number of times a fault
19401  * injection is bypassed (pause between fault injections).
19402  * If set to 0, a fault is injected only a number of times specified by
19403  * sata_inject_fault_count.
19404  *
19405  * The fault counts are static, so for periodic errors they have to be manually
19406  * reset to start repetition sequence from scratch.
19407  * If the original value returned by the HBA tran_start function is not
19408  * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error
19409  * is injected (to avoid masking real problems);
19410  *
19411  * NOTE: In its current incarnation, this function should be invoked only for
19412  * commands executed in SYNCHRONOUS mode.
19413  */
19414 
19415 
19416 static void
19417 sata_inject_pkt_fault(sata_pkt_t *spkt, int *rval, int fault)
19418 {
19419 
19420 	if (sata_inject_fault != SATA_INJECT_PKT_FAULT)
19421 		return;
19422 
19423 	if (sata_inject_fault_count == 0)
19424 		return;
19425 
19426 	if (fault == 0)
19427 		return;
19428 
19429 	if (sata_fault_cmd != spkt->satapkt_cmd.satacmd_cmd_reg)
19430 		return;
19431 
19432 	if (sata_fault_ctrl != NULL) {
19433 		sata_pkt_txlate_t *spx =
19434 		    (sata_pkt_txlate_t *)spkt->satapkt_framework_private;
19435 
19436 		if (sata_fault_ctrl != NULL && sata_fault_ctrl !=
19437 		    spx->txlt_sata_hba_inst->satahba_dip)
19438 			return;
19439 
19440 		if (sata_fault_device.satadev_addr.cport !=
19441 		    spkt->satapkt_device.satadev_addr.cport ||
19442 		    sata_fault_device.satadev_addr.pmport !=
19443 		    spkt->satapkt_device.satadev_addr.pmport ||
19444 		    sata_fault_device.satadev_addr.qual !=
19445 		    spkt->satapkt_device.satadev_addr.qual)
19446 			return;
19447 	}
19448 
19449 	/* Modify pkt return parameters */
19450 	if (*rval != SATA_TRAN_ACCEPTED ||
19451 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
19452 		sata_fault_count = 0;
19453 		sata_fault_suspend_count = 0;
19454 		return;
19455 	}
19456 	if (sata_fault_count == 0 && sata_fault_suspend_count != 0) {
19457 		/* Pause in the injection */
19458 		sata_fault_suspend_count -= 1;
19459 		return;
19460 	}
19461 
19462 	if (sata_fault_count == 0 && sata_fault_suspend_count == 0) {
19463 		/*
19464 		 * Init inject fault cycle. If fault count is set to -1,
19465 		 * it is a permanent fault.
19466 		 */
19467 		if (sata_inject_fault_count != -1) {
19468 			sata_fault_count = sata_inject_fault_count;
19469 			sata_fault_suspend_count =
19470 			    sata_inject_fault_pause_count;
19471 			if (sata_fault_suspend_count == 0)
19472 				sata_inject_fault_count = 0;
19473 		}
19474 	}
19475 
19476 	if (sata_fault_count != 0)
19477 		sata_fault_count -= 1;
19478 
19479 	switch (fault) {
19480 	case SATA_PKT_BUSY:
19481 		*rval = SATA_TRAN_BUSY;
19482 		spkt->satapkt_reason = SATA_PKT_BUSY;
19483 		break;
19484 
19485 	case SATA_PKT_QUEUE_FULL:
19486 		*rval = SATA_TRAN_QUEUE_FULL;
19487 		spkt->satapkt_reason = SATA_PKT_QUEUE_FULL;
19488 		break;
19489 
19490 	case SATA_PKT_CMD_UNSUPPORTED:
19491 		*rval = SATA_TRAN_CMD_UNSUPPORTED;
19492 		spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED;
19493 		break;
19494 
19495 	case SATA_PKT_PORT_ERROR:
19496 		/* This is "rejected" command */
19497 		*rval = SATA_TRAN_PORT_ERROR;
19498 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
19499 		/* Additional error setup could be done here - port state */
19500 		break;
19501 
19502 	case SATA_PKT_DEV_ERROR:
19503 		spkt->satapkt_reason = SATA_PKT_DEV_ERROR;
19504 		/*
19505 		 * Additional error setup could be done here
19506 		 */
19507 		break;
19508 
19509 	case SATA_PKT_ABORTED:
19510 		spkt->satapkt_reason = SATA_PKT_ABORTED;
19511 		break;
19512 
19513 	case SATA_PKT_TIMEOUT:
19514 		spkt->satapkt_reason = SATA_PKT_TIMEOUT;
19515 		/* Additional error setup could be done here */
19516 		break;
19517 
19518 	case SATA_PKT_RESET:
19519 		spkt->satapkt_reason = SATA_PKT_RESET;
19520 		/*
19521 		 * Additional error setup could be done here - device reset
19522 		 */
19523 		break;
19524 
19525 	default:
19526 		break;
19527 	}
19528 }
19529 
19530 #endif
19531 
19532 /*
19533  * SATA Trace Ring Buffer
19534  * ----------------------
19535  *
19536  * Overview
19537  *
19538  * The SATA trace ring buffer is a ring buffer created and managed by
19539  * the SATA framework module that can be used by any module or driver
19540  * within the SATA framework to store debug messages.
19541  *
19542  * Ring Buffer Interfaces:
19543  *
19544  *	sata_vtrace_debug()	<-- Adds debug message to ring buffer
19545  *	sata_trace_debug()	<-- Wraps varargs into sata_vtrace_debug()
19546  *
19547  *	Note that the sata_trace_debug() interface was created to give
19548  *	consumers the flexibilty of sending debug messages to ring buffer
19549  *	as variable arguments.  Consumers can send type va_list debug
19550  *	messages directly to sata_vtrace_debug(). The sata_trace_debug()
19551  *	and sata_vtrace_debug() relationship is similar to that of
19552  *	cmn_err(9F) and vcmn_err(9F).
19553  *
19554  * Below is a diagram of the SATA trace ring buffer interfaces and
19555  * sample consumers:
19556  *
19557  * +---------------------------------+
19558  * |    o  o  SATA Framework Module  |
19559  * | o  SATA  o     +------------------+      +------------------+
19560  * |o   Trace  o <--|sata_vtrace_debug/|<-----|SATA HBA Driver #1|
19561  * |o   R-Buf  o    |sata_trace_debug  |<--+  +------------------+
19562  * | o        o     +------------------+   |  +------------------+
19563  * |    o  o                ^        |     +--|SATA HBA Driver #2|
19564  * |                        |        |        +------------------+
19565  * |           +------------------+  |
19566  * |           |SATA Debug Message|  |
19567  * |           +------------------+  |
19568  * +---------------------------------+
19569  *
19570  * Supporting Routines:
19571  *
19572  *	sata_trace_rbuf_alloc()	<-- Initializes ring buffer
19573  *	sata_trace_rbuf_free()	<-- Destroys ring buffer
19574  *	sata_trace_dmsg_alloc() <-- Creates or reuses buffer in ring buffer
19575  *	sata_trace_dmsg_free()	<-- Destroys content of ring buffer
19576  *
19577  * The default SATA trace ring buffer size is defined by DMSG_RING_SIZE.
19578  * The ring buffer size can be adjusted by setting dmsg_ring_size in
19579  * /etc/system to desired size in unit of bytes.
19580  *
19581  * The individual debug message size in the ring buffer is restricted
19582  * to DMSG_BUF_SIZE.
19583  */
19584 void
19585 sata_vtrace_debug(dev_info_t *dip, const char *fmt, va_list ap)
19586 {
19587 	sata_trace_dmsg_t *dmsg;
19588 
19589 	if (sata_debug_rbuf == NULL) {
19590 		return;
19591 	}
19592 
19593 	/*
19594 	 * If max size of ring buffer is smaller than size
19595 	 * required for one debug message then just return
19596 	 * since we have no room for the debug message.
19597 	 */
19598 	if (sata_debug_rbuf->maxsize < (sizeof (sata_trace_dmsg_t))) {
19599 		return;
19600 	}
19601 
19602 	mutex_enter(&sata_debug_rbuf->lock);
19603 
19604 	/* alloc or reuse on ring buffer */
19605 	dmsg = sata_trace_dmsg_alloc();
19606 
19607 	if (dmsg == NULL) {
19608 		/* resource allocation failed */
19609 		mutex_exit(&sata_debug_rbuf->lock);
19610 		return;
19611 	}
19612 
19613 	dmsg->dip = dip;
19614 	gethrestime(&dmsg->timestamp);
19615 
19616 	(void) vsnprintf(dmsg->buf, sizeof (dmsg->buf), fmt, ap);
19617 
19618 	mutex_exit(&sata_debug_rbuf->lock);
19619 }
19620 
19621 void
19622 sata_trace_debug(dev_info_t *dip, const char *fmt, ...)
19623 {
19624 	va_list ap;
19625 
19626 	va_start(ap, fmt);
19627 	sata_vtrace_debug(dip, fmt, ap);
19628 	va_end(ap);
19629 }
19630 
19631 /*
19632  * This routine is used to manage debug messages
19633  * on ring buffer.
19634  */
19635 static sata_trace_dmsg_t *
19636 sata_trace_dmsg_alloc(void)
19637 {
19638 	sata_trace_dmsg_t *dmsg_alloc, *dmsg = sata_debug_rbuf->dmsgp;
19639 
19640 	if (sata_debug_rbuf->looped == TRUE) {
19641 		sata_debug_rbuf->dmsgp = dmsg->next;
19642 		return (sata_debug_rbuf->dmsgp);
19643 	}
19644 
19645 	/*
19646 	 * If we're looping for the first time,
19647 	 * connect the ring.
19648 	 */
19649 	if (((sata_debug_rbuf->size + (sizeof (sata_trace_dmsg_t))) >
19650 	    sata_debug_rbuf->maxsize) && (sata_debug_rbuf->dmsgh != NULL)) {
19651 		dmsg->next = sata_debug_rbuf->dmsgh;
19652 		sata_debug_rbuf->dmsgp = sata_debug_rbuf->dmsgh;
19653 		sata_debug_rbuf->looped = TRUE;
19654 		return (sata_debug_rbuf->dmsgp);
19655 	}
19656 
19657 	/* If we've gotten this far then memory allocation is needed */
19658 	dmsg_alloc = kmem_zalloc(sizeof (sata_trace_dmsg_t), KM_NOSLEEP);
19659 	if (dmsg_alloc == NULL) {
19660 		sata_debug_rbuf->allocfailed++;
19661 		return (dmsg_alloc);
19662 	} else {
19663 		sata_debug_rbuf->size += sizeof (sata_trace_dmsg_t);
19664 	}
19665 
19666 	if (sata_debug_rbuf->dmsgp != NULL) {
19667 		dmsg->next = dmsg_alloc;
19668 		sata_debug_rbuf->dmsgp = dmsg->next;
19669 		return (sata_debug_rbuf->dmsgp);
19670 	} else {
19671 		/*
19672 		 * We should only be here if we're initializing
19673 		 * the ring buffer.
19674 		 */
19675 		if (sata_debug_rbuf->dmsgh == NULL) {
19676 			sata_debug_rbuf->dmsgh = dmsg_alloc;
19677 		} else {
19678 			/* Something is wrong */
19679 			kmem_free(dmsg_alloc, sizeof (sata_trace_dmsg_t));
19680 			return (NULL);
19681 		}
19682 
19683 		sata_debug_rbuf->dmsgp = dmsg_alloc;
19684 		return (sata_debug_rbuf->dmsgp);
19685 	}
19686 }
19687 
19688 
19689 /*
19690  * Free all messages on debug ring buffer.
19691  */
19692 static void
19693 sata_trace_dmsg_free(void)
19694 {
19695 	sata_trace_dmsg_t *dmsg_next, *dmsg = sata_debug_rbuf->dmsgh;
19696 
19697 	while (dmsg != NULL) {
19698 		dmsg_next = dmsg->next;
19699 		kmem_free(dmsg, sizeof (sata_trace_dmsg_t));
19700 
19701 		/*
19702 		 * If we've looped around the ring than we're done.
19703 		 */
19704 		if (dmsg_next == sata_debug_rbuf->dmsgh) {
19705 			break;
19706 		} else {
19707 			dmsg = dmsg_next;
19708 		}
19709 	}
19710 }
19711 
19712 
19713 /*
19714  * This function can block
19715  */
19716 static void
19717 sata_trace_rbuf_alloc(void)
19718 {
19719 	sata_debug_rbuf = kmem_zalloc(sizeof (sata_trace_rbuf_t), KM_SLEEP);
19720 
19721 	mutex_init(&sata_debug_rbuf->lock, NULL, MUTEX_DRIVER, NULL);
19722 
19723 	if (dmsg_ring_size > 0) {
19724 		sata_debug_rbuf->maxsize = (size_t)dmsg_ring_size;
19725 	}
19726 }
19727 
19728 
19729 static void
19730 sata_trace_rbuf_free(void)
19731 {
19732 	sata_trace_dmsg_free();
19733 	mutex_destroy(&sata_debug_rbuf->lock);
19734 	kmem_free(sata_debug_rbuf, sizeof (sata_trace_rbuf_t));
19735 }
19736 
19737 /*
19738  * If SATA_DEBUG is not defined then this routine is called instead
19739  * of sata_log() via the SATA_LOG_D macro.
19740  */
19741 static void
19742 sata_trace_log(sata_hba_inst_t *sata_hba_inst, uint_t level,
19743     const char *fmt, ...)
19744 {
19745 #ifndef __lock_lint
19746 	_NOTE(ARGUNUSED(level))
19747 #endif
19748 
19749 	dev_info_t *dip = NULL;
19750 	va_list ap;
19751 
19752 	if (sata_hba_inst != NULL) {
19753 		dip = SATA_DIP(sata_hba_inst);
19754 	}
19755 
19756 	va_start(ap, fmt);
19757 	sata_vtrace_debug(dip, fmt, ap);
19758 	va_end(ap);
19759 }
19760