xref: /titanic_41/usr/src/uts/common/io/sata/impl/sata.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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.45"};
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	void 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  * Search a port multiplier in the blacklist and update the flags if a match
1860  * is found.
1861  *
1862  * Returns:
1863  * SATA_SUCCESS if any matched entry is found.
1864  * SATA_FAILURE if no matched entry is found.
1865  */
1866 int
1867 sata_check_pmult_blacklist(sata_device_t *sd)
1868 {
1869 	sata_pmult_bl_t *blp;
1870 	for (blp = sata_pmult_blacklist; blp->bl_gscr0; blp++) {
1871 		if (sd->satadev_gscr.gscr0 != blp->bl_gscr0 && blp->bl_gscr0)
1872 			continue;
1873 		if (sd->satadev_gscr.gscr1 != blp->bl_gscr1 && blp->bl_gscr1)
1874 			continue;
1875 		if (sd->satadev_gscr.gscr2 != blp->bl_gscr2 && blp->bl_gscr2)
1876 			continue;
1877 
1878 		cmn_err(CE_WARN, "!Port multiplier is on the blacklist.");
1879 		sd->satadev_add_info = blp->bl_flags;
1880 		return (SATA_SUCCESS);
1881 	}
1882 	return (SATA_FAILURE);
1883 }
1884 
1885 /*
1886  * sata_name_child is for composing the name of the node
1887  * the format of the name is "target,0".
1888  */
1889 static int
1890 sata_name_child(dev_info_t *dip, char *name, int namelen)
1891 {
1892 	int target;
1893 
1894 	target = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1895 	    DDI_PROP_DONTPASS, "target", -1);
1896 	if (target == -1)
1897 		return (DDI_FAILURE);
1898 	(void) snprintf(name, namelen, "%x,0", target);
1899 	return (DDI_SUCCESS);
1900 }
1901 
1902 
1903 
1904 /* ****************** SCSA required entry points *********************** */
1905 
1906 /*
1907  * Implementation of scsi tran_tgt_init.
1908  * sata_scsi_tgt_init() initializes scsi_device structure
1909  *
1910  * If successful, DDI_SUCCESS is returned.
1911  * DDI_FAILURE is returned if addressed device does not exist
1912  */
1913 
1914 static int
1915 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1916     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1917 {
1918 #ifndef __lock_lint
1919 	_NOTE(ARGUNUSED(hba_dip))
1920 	_NOTE(ARGUNUSED(tgt_dip))
1921 #endif
1922 	sata_device_t		sata_device;
1923 	sata_drive_info_t	*sdinfo;
1924 	struct sata_id		*sid;
1925 	sata_hba_inst_t		*sata_hba_inst;
1926 	char			model[SATA_ID_MODEL_LEN + 1];
1927 	char			fw[SATA_ID_FW_LEN + 1];
1928 	char			*vid, *pid;
1929 	int			i;
1930 
1931 	/*
1932 	 * Fail tran_tgt_init for .conf stub node
1933 	 */
1934 	if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
1935 		(void) ndi_merge_node(tgt_dip, sata_name_child);
1936 		ddi_set_name_addr(tgt_dip, NULL);
1937 		return (DDI_FAILURE);
1938 	}
1939 
1940 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
1941 
1942 	/* Validate scsi device address */
1943 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
1944 	    &sata_device) != 0)
1945 		return (DDI_FAILURE);
1946 
1947 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1948 	    sata_device.satadev_addr.cport)));
1949 
1950 	/* sata_device now contains a valid sata address */
1951 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
1952 	if (sdinfo == NULL) {
1953 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1954 		    sata_device.satadev_addr.cport)));
1955 		return (DDI_FAILURE);
1956 	}
1957 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1958 	    sata_device.satadev_addr.cport)));
1959 
1960 	/*
1961 	 * Check if we need to create a legacy devid (i.e cmdk style) for
1962 	 * the target disks.
1963 	 *
1964 	 * HBA devinfo node will have the property "use-cmdk-devid-format"
1965 	 * if we need to create cmdk-style devid for all the disk devices
1966 	 * attached to this controller. This property may have been set
1967 	 * from HBA driver's .conf file or by the HBA driver in its
1968 	 * attach(9F) function.
1969 	 */
1970 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
1971 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
1972 	    "use-cmdk-devid-format", 0) == 1)) {
1973 		/* register a legacy devid for this target node */
1974 		sata_target_devid_register(tgt_dip, sdinfo);
1975 	}
1976 
1977 
1978 	/*
1979 	 * 'Identify Device Data' does not always fit in standard SCSI
1980 	 * INQUIRY data, so establish INQUIRY_* properties with full-form
1981 	 * of information.
1982 	 */
1983 	sid = &sdinfo->satadrv_id;
1984 #ifdef	_LITTLE_ENDIAN
1985 	swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
1986 	swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
1987 #else	/* _LITTLE_ENDIAN */
1988 	bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN);
1989 	bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN);
1990 #endif	/* _LITTLE_ENDIAN */
1991 	model[SATA_ID_MODEL_LEN] = 0;
1992 	fw[SATA_ID_FW_LEN] = 0;
1993 
1994 	/* split model into into vid/pid */
1995 	for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++)
1996 		if ((*pid == ' ') || (*pid == '\t'))
1997 			break;
1998 	if (i < SATA_ID_MODEL_LEN) {
1999 		vid = model;
2000 		*pid++ = 0;		/* terminate vid, establish pid */
2001 	} else {
2002 		vid = NULL;		/* vid will stay "ATA     " */
2003 		pid = model;		/* model is all pid */
2004 	}
2005 
2006 	if (vid)
2007 		(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_VENDOR_ID,
2008 		    vid, strlen(vid));
2009 	if (pid)
2010 		(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID,
2011 		    pid, strlen(pid));
2012 	(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_REVISION_ID,
2013 	    fw, strlen(fw));
2014 
2015 	return (DDI_SUCCESS);
2016 }
2017 
2018 /*
2019  * Implementation of scsi tran_tgt_probe.
2020  * Probe target, by calling default scsi routine scsi_hba_probe()
2021  */
2022 static int
2023 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void))
2024 {
2025 	sata_hba_inst_t *sata_hba_inst =
2026 	    (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private);
2027 	int rval;
2028 	uint32_t pm_cap;
2029 
2030 	rval = scsi_hba_probe(sd, callback);
2031 	pm_cap = SATA_CAP_POWER_CONDITON | SATA_CAP_SMART_PAGE |
2032 	    SATA_CAP_LOG_SENSE;
2033 
2034 	if (rval == SCSIPROBE_EXISTS) {
2035 		/*
2036 		 * Set property "pm-capable" on the target device node, so that
2037 		 * the target driver will not try to fetch scsi cycle counters
2038 		 * before enabling device power-management.
2039 		 */
2040 		if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
2041 		    "pm-capable", pm_cap)) != DDI_PROP_SUCCESS) {
2042 			sata_log(sata_hba_inst, CE_WARN,
2043 			    "SATA device at port %d: "
2044 			    "will not be power-managed ",
2045 			    SCSI_TO_SATA_CPORT(sd->sd_address.a_target));
2046 			SATA_LOG_D((sata_hba_inst, CE_WARN,
2047 			    "failure updating pm-capable property"));
2048 		}
2049 	}
2050 	return (rval);
2051 }
2052 
2053 /*
2054  * Implementation of scsi tran_tgt_free.
2055  * Release all resources allocated for scsi_device
2056  */
2057 static void
2058 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2059     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2060 {
2061 #ifndef __lock_lint
2062 	_NOTE(ARGUNUSED(hba_dip))
2063 #endif
2064 	sata_device_t		sata_device;
2065 	sata_drive_info_t	*sdinfo;
2066 	sata_hba_inst_t		*sata_hba_inst;
2067 	ddi_devid_t		devid;
2068 
2069 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2070 
2071 	/* Validate scsi device address */
2072 	/*
2073 	 * Note: tgt_free relates to the SCSA view of a device. If called, there
2074 	 * was a device at this address, so even if the sata framework internal
2075 	 * resources were alredy released because a device was detached,
2076 	 * this function should be executed as long as its actions do
2077 	 * not require the internal sata view of a device and the address
2078 	 * refers to a valid sata address.
2079 	 * Validating the address here means that we do not trust SCSA...
2080 	 */
2081 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2082 	    &sata_device) == -1)
2083 		return;
2084 
2085 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2086 	    sata_device.satadev_addr.cport)));
2087 
2088 	/* sata_device now should contain a valid sata address */
2089 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2090 	if (sdinfo == NULL) {
2091 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2092 		    sata_device.satadev_addr.cport)));
2093 		return;
2094 	}
2095 	/*
2096 	 * We did not allocate any resources in sata_scsi_tgt_init()
2097 	 * other than few properties.
2098 	 * Free them.
2099 	 */
2100 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2101 	    sata_device.satadev_addr.cport)));
2102 	(void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable");
2103 
2104 	/*
2105 	 * If devid was previously created but not freed up from
2106 	 * sd(7D) driver (i.e during detach(9F)) then do it here.
2107 	 */
2108 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2109 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2110 	    "use-cmdk-devid-format", 0) == 1) &&
2111 	    (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) {
2112 		ddi_devid_unregister(tgt_dip);
2113 		ddi_devid_free(devid);
2114 	}
2115 }
2116 
2117 /*
2118  * Implementation of scsi tran_init_pkt
2119  * Upon successful return, scsi pkt buffer has DMA resources allocated.
2120  *
2121  * It seems that we should always allocate pkt, even if the address is
2122  * for non-existing device - just use some default for dma_attr.
2123  * The reason is that there is no way to communicate this to a caller here.
2124  * Subsequent call to sata_scsi_start may fail appropriately.
2125  * Simply returning NULL does not seem to discourage a target driver...
2126  *
2127  * Returns a pointer to initialized scsi_pkt, or NULL otherwise.
2128  */
2129 static struct scsi_pkt *
2130 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
2131     struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
2132     int (*callback)(caddr_t), caddr_t arg)
2133 {
2134 	sata_hba_inst_t *sata_hba_inst =
2135 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2136 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
2137 	sata_device_t sata_device;
2138 	sata_drive_info_t *sdinfo;
2139 	sata_pkt_txlate_t *spx;
2140 	ddi_dma_attr_t cur_dma_attr;
2141 	int rval;
2142 	boolean_t new_pkt = TRUE;
2143 
2144 	ASSERT(ap->a_hba_tran->tran_hba_dip == dip);
2145 
2146 	/*
2147 	 * We need to translate the address, even if it could be
2148 	 * a bogus one, for a non-existing device
2149 	 */
2150 	sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
2151 	sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target);
2152 	sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2153 	sata_device.satadev_rev = SATA_DEVICE_REV;
2154 
2155 	if (pkt == NULL) {
2156 		/*
2157 		 * Have to allocate a brand new scsi packet.
2158 		 * We need to operate with auto request sense enabled.
2159 		 */
2160 		pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen,
2161 		    MAX(statuslen, sizeof (struct scsi_arq_status)),
2162 		    tgtlen, sizeof (sata_pkt_txlate_t), callback, arg);
2163 
2164 		if (pkt == NULL)
2165 			return (NULL);
2166 
2167 		/* Fill scsi packet structure */
2168 		pkt->pkt_comp		= (void (*)())NULL;
2169 		pkt->pkt_time		= 0;
2170 		pkt->pkt_resid		= 0;
2171 		pkt->pkt_statistics	= 0;
2172 		pkt->pkt_reason		= 0;
2173 
2174 		/*
2175 		 * pkt_hba_private will point to sata pkt txlate structure
2176 		 */
2177 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2178 		bzero(spx, sizeof (sata_pkt_txlate_t));
2179 
2180 		spx->txlt_scsi_pkt = pkt;
2181 		spx->txlt_sata_hba_inst = sata_hba_inst;
2182 
2183 		/* Allocate sata_pkt */
2184 		spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback);
2185 		if (spx->txlt_sata_pkt == NULL) {
2186 			/* Could not allocate sata pkt */
2187 			scsi_hba_pkt_free(ap, pkt);
2188 			return (NULL);
2189 		}
2190 		/* Set sata address */
2191 		spx->txlt_sata_pkt->satapkt_device.satadev_addr =
2192 		    sata_device.satadev_addr;
2193 		spx->txlt_sata_pkt->satapkt_device.satadev_rev =
2194 		    sata_device.satadev_rev;
2195 
2196 		if ((bp == NULL) || (bp->b_bcount == 0))
2197 			return (pkt);
2198 
2199 		spx->txlt_total_residue = bp->b_bcount;
2200 	} else {
2201 		new_pkt = FALSE;
2202 		/*
2203 		 * Packet was preallocated/initialized by previous call
2204 		 */
2205 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2206 
2207 		if ((bp == NULL) || (bp->b_bcount == 0)) {
2208 			return (pkt);
2209 		}
2210 
2211 		/* Pkt is available already: spx->txlt_scsi_pkt == pkt; */
2212 	}
2213 
2214 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
2215 
2216 	/*
2217 	 * We use an adjusted version of the dma_attr, to account
2218 	 * for device addressing limitations.
2219 	 * sata_adjust_dma_attr() will handle sdinfo == NULL which may
2220 	 * happen when a device is not yet configured.
2221 	 */
2222 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2223 	    sata_device.satadev_addr.cport)));
2224 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2225 	    &spx->txlt_sata_pkt->satapkt_device);
2226 	/* NULL sdinfo may be passsed to sata_adjust_dma_attr() */
2227 	sata_adjust_dma_attr(sdinfo,
2228 	    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
2229 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2230 	    sata_device.satadev_addr.cport)));
2231 	/*
2232 	 * Allocate necessary DMA resources for the packet's data buffer
2233 	 * NOTE:
2234 	 * In case of read/write commands, DMA resource allocation here is
2235 	 * based on the premise that the transfer length specified in
2236 	 * the read/write scsi cdb will match exactly DMA resources -
2237 	 * returning correct packet residue is crucial.
2238 	 */
2239 	if ((rval = sata_dma_buf_setup(spx, flags, callback, arg,
2240 	    &cur_dma_attr)) != DDI_SUCCESS) {
2241 		/*
2242 		 * If a DMA allocation request fails with
2243 		 * DDI_DMA_NOMAPPING, indicate the error by calling
2244 		 * bioerror(9F) with bp and an error code of EFAULT.
2245 		 * If a DMA allocation request fails with
2246 		 * DDI_DMA_TOOBIG, indicate the error by calling
2247 		 * bioerror(9F) with bp and an error code of EINVAL.
2248 		 * For DDI_DMA_NORESOURCES, we may have some of them allocated.
2249 		 * Request may be repeated later - there is no real error.
2250 		 */
2251 		switch (rval) {
2252 		case DDI_DMA_NORESOURCES:
2253 			bioerror(bp, 0);
2254 			break;
2255 		case DDI_DMA_NOMAPPING:
2256 		case DDI_DMA_BADATTR:
2257 			bioerror(bp, EFAULT);
2258 			break;
2259 		case DDI_DMA_TOOBIG:
2260 		default:
2261 			bioerror(bp, EINVAL);
2262 			break;
2263 		}
2264 		if (new_pkt == TRUE) {
2265 			/*
2266 			 * Since this is a new packet, we can clean-up
2267 			 * everything
2268 			 */
2269 			sata_scsi_destroy_pkt(ap, pkt);
2270 		} else {
2271 			/*
2272 			 * This is a re-used packet. It will be target driver's
2273 			 * responsibility to eventually destroy it (which
2274 			 * will free allocated resources).
2275 			 * Here, we just "complete" the request, leaving
2276 			 * allocated resources intact, so the request may
2277 			 * be retried.
2278 			 */
2279 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2280 			sata_pkt_free(spx);
2281 		}
2282 		return (NULL);
2283 	}
2284 	/* Set number of bytes that are not yet accounted for */
2285 	pkt->pkt_resid = spx->txlt_total_residue;
2286 	ASSERT(pkt->pkt_resid >= 0);
2287 
2288 	return (pkt);
2289 }
2290 
2291 /*
2292  * Implementation of scsi tran_start.
2293  * Translate scsi cmd into sata operation and return status.
2294  * ATAPI CDBs are passed to ATAPI devices - the device determines what commands
2295  * are supported.
2296  * For SATA hard disks, supported scsi commands:
2297  * SCMD_INQUIRY
2298  * SCMD_TEST_UNIT_READY
2299  * SCMD_START_STOP
2300  * SCMD_READ_CAPACITY
2301  * SCMD_REQUEST_SENSE
2302  * SCMD_LOG_SENSE_G1
2303  * SCMD_LOG_SELECT_G1
2304  * SCMD_MODE_SENSE	(specific pages)
2305  * SCMD_MODE_SENSE_G1	(specific pages)
2306  * SCMD_MODE_SELECT	(specific pages)
2307  * SCMD_MODE_SELECT_G1	(specific pages)
2308  * SCMD_SYNCHRONIZE_CACHE
2309  * SCMD_SYNCHRONIZE_CACHE_G1
2310  * SCMD_READ
2311  * SCMD_READ_G1
2312  * SCMD_READ_G4
2313  * SCMD_READ_G5
2314  * SCMD_WRITE
2315  * SCMD_WRITE_BUFFER
2316  * SCMD_WRITE_G1
2317  * SCMD_WRITE_G4
2318  * SCMD_WRITE_G5
2319  * SCMD_SEEK		(noop)
2320  * SCMD_SDIAG
2321  *
2322  * All other commands are rejected as unsupported.
2323  *
2324  * Returns:
2325  * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver
2326  * for execution. TRAN_ACCEPT may be returned also if device was removed but
2327  * a callback could be scheduled.
2328  * TRAN_BADPKT if cmd was directed to invalid address.
2329  * TRAN_FATAL_ERROR is command was rejected due to hardware error, including
2330  * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device
2331  * was removed and there was no callback specified in scsi pkt.
2332  * TRAN_BUSY if command could not be executed becasue HBA driver or SATA
2333  * framework was busy performing some other operation(s).
2334  *
2335  */
2336 static int
2337 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
2338 {
2339 	sata_hba_inst_t *sata_hba_inst =
2340 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2341 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2342 	sata_device_t *sdevice = &spx->txlt_sata_pkt->satapkt_device;
2343 	sata_drive_info_t *sdinfo;
2344 	struct buf *bp;
2345 	uint8_t cport, pmport;
2346 	boolean_t dev_gone = B_FALSE;
2347 	int rval;
2348 
2349 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2350 	    "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]);
2351 
2352 	ASSERT(spx != NULL &&
2353 	    spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL);
2354 
2355 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
2356 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2357 
2358 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2359 
2360 	if (sdevice->satadev_addr.qual == SATA_ADDR_DCPORT) {
2361 		sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2362 		if (sdinfo == NULL ||
2363 		    SATA_CPORT_INFO(sata_hba_inst, cport)->
2364 		    cport_tgtnode_clean == B_FALSE ||
2365 		    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2366 			dev_gone = B_TRUE;
2367 		}
2368 	} else if (sdevice->satadev_addr.qual == SATA_ADDR_DPMPORT) {
2369 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
2370 		    SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst,
2371 		    cport) == NULL) {
2372 			dev_gone = B_TRUE;
2373 		} else if (SATA_PMPORT_INFO(sata_hba_inst, cport,
2374 		    pmport) == NULL) {
2375 			dev_gone = B_TRUE;
2376 		} else {
2377 			mutex_enter(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2378 			    cport, pmport)));
2379 			sdinfo = sata_get_device_info(sata_hba_inst, sdevice);
2380 			if (sdinfo == NULL ||
2381 			    SATA_PMPORT_INFO(sata_hba_inst, cport, pmport)->
2382 			    pmport_tgtnode_clean == B_FALSE ||
2383 			    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2384 				dev_gone = B_TRUE;
2385 			}
2386 			mutex_exit(&(SATA_PMPORT_MUTEX(sata_hba_inst,
2387 			    cport, pmport)));
2388 		}
2389 	}
2390 
2391 	if (dev_gone == B_TRUE) {
2392 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2393 		pkt->pkt_reason = CMD_DEV_GONE;
2394 		/*
2395 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2396 		 * only in callback function (for normal requests) and
2397 		 * in the dump code path.
2398 		 * So, if the callback is available, we need to do
2399 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2400 		 */
2401 		if (pkt->pkt_comp != NULL) {
2402 			/* scsi callback required */
2403 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2404 			    (task_func_t *)pkt->pkt_comp,
2405 			    (void *)pkt, TQ_SLEEP) == NULL)
2406 				/* Scheduling the callback failed */
2407 				return (TRAN_BUSY);
2408 			return (TRAN_ACCEPT);
2409 		}
2410 		/* No callback available */
2411 		return (TRAN_FATAL_ERROR);
2412 	}
2413 
2414 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
2415 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2416 		rval = sata_txlt_atapi(spx);
2417 		SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2418 		    "sata_scsi_start atapi: rval %d\n", rval);
2419 		return (rval);
2420 	}
2421 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2422 
2423 	/*
2424 	 * Checking for power state, if it was on
2425 	 * STOPPED state, then the drive is not capable
2426 	 * of processing media access command.  And
2427 	 * TEST_UNIT_READY, REQUEST_SENSE has special handling
2428 	 * in the function for different power state.
2429 	 */
2430 	if (((sdinfo->satadrv_power_level == SATA_POWER_STANDBY) ||
2431 	    (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)) &&
2432 	    (SATA_IS_MEDIUM_ACCESS_CMD(pkt->pkt_cdbp[0]))) {
2433 		return (sata_txlt_check_condition(spx, KEY_NOT_READY,
2434 		    SD_SCSI_ASC_LU_NOT_READY));
2435 	}
2436 
2437 	/* ATA Disk commands processing starts here */
2438 
2439 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2440 
2441 	switch (pkt->pkt_cdbp[0]) {
2442 
2443 	case SCMD_INQUIRY:
2444 		/* Mapped to identify device */
2445 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2446 			bp_mapin(bp);
2447 		rval = sata_txlt_inquiry(spx);
2448 		break;
2449 
2450 	case SCMD_TEST_UNIT_READY:
2451 		/*
2452 		 * SAT "SATA to ATA Translation" doc specifies translation
2453 		 * to ATA CHECK POWER MODE.
2454 		 */
2455 		rval = sata_txlt_test_unit_ready(spx);
2456 		break;
2457 
2458 	case SCMD_START_STOP:
2459 		/* Mapping depends on the command */
2460 		rval = sata_txlt_start_stop_unit(spx);
2461 		break;
2462 
2463 	case SCMD_READ_CAPACITY:
2464 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2465 			bp_mapin(bp);
2466 		rval = sata_txlt_read_capacity(spx);
2467 		break;
2468 
2469 	case SCMD_REQUEST_SENSE:
2470 		/*
2471 		 * Always No Sense, since we force ARQ
2472 		 */
2473 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2474 			bp_mapin(bp);
2475 		rval = sata_txlt_request_sense(spx);
2476 		break;
2477 
2478 	case SCMD_LOG_SENSE_G1:
2479 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2480 			bp_mapin(bp);
2481 		rval = sata_txlt_log_sense(spx);
2482 		break;
2483 
2484 	case SCMD_LOG_SELECT_G1:
2485 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2486 			bp_mapin(bp);
2487 		rval = sata_txlt_log_select(spx);
2488 		break;
2489 
2490 	case SCMD_MODE_SENSE:
2491 	case SCMD_MODE_SENSE_G1:
2492 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2493 			bp_mapin(bp);
2494 		rval = sata_txlt_mode_sense(spx);
2495 		break;
2496 
2497 
2498 	case SCMD_MODE_SELECT:
2499 	case SCMD_MODE_SELECT_G1:
2500 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2501 			bp_mapin(bp);
2502 		rval = sata_txlt_mode_select(spx);
2503 		break;
2504 
2505 	case SCMD_SYNCHRONIZE_CACHE:
2506 	case SCMD_SYNCHRONIZE_CACHE_G1:
2507 		rval = sata_txlt_synchronize_cache(spx);
2508 		break;
2509 
2510 	case SCMD_READ:
2511 	case SCMD_READ_G1:
2512 	case SCMD_READ_G4:
2513 	case SCMD_READ_G5:
2514 		rval = sata_txlt_read(spx);
2515 		break;
2516 	case SCMD_WRITE_BUFFER:
2517 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2518 			bp_mapin(bp);
2519 		rval = sata_txlt_write_buffer(spx);
2520 		break;
2521 
2522 	case SCMD_WRITE:
2523 	case SCMD_WRITE_G1:
2524 	case SCMD_WRITE_G4:
2525 	case SCMD_WRITE_G5:
2526 		rval = sata_txlt_write(spx);
2527 		break;
2528 
2529 	case SCMD_SEEK:
2530 		rval = sata_txlt_nodata_cmd_immediate(spx);
2531 		break;
2532 
2533 		/* Other cases will be filed later */
2534 		/* postponed until phase 2 of the development */
2535 	default:
2536 		rval = sata_txlt_invalid_command(spx);
2537 		break;
2538 	}
2539 
2540 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2541 	    "sata_scsi_start: rval %d\n", rval);
2542 
2543 	return (rval);
2544 }
2545 
2546 /*
2547  * Implementation of scsi tran_abort.
2548  * Abort specific pkt or all packets.
2549  *
2550  * Returns 1 if one or more packets were aborted, returns 0 otherwise
2551  *
2552  * May be called from an interrupt level.
2553  */
2554 static int
2555 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt)
2556 {
2557 	sata_hba_inst_t *sata_hba_inst =
2558 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2559 	sata_device_t	sata_device;
2560 	sata_pkt_t	*sata_pkt;
2561 
2562 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2563 	    "sata_scsi_abort: %s at target: 0x%x\n",
2564 	    scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target);
2565 
2566 	/* Validate address */
2567 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0)
2568 		/* Invalid address */
2569 		return (0);
2570 
2571 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2572 	    sata_device.satadev_addr.cport)));
2573 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2574 		/* invalid address */
2575 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2576 		    sata_device.satadev_addr.cport)));
2577 		return (0);
2578 	}
2579 	if (scsi_pkt == NULL) {
2580 		/*
2581 		 * Abort all packets.
2582 		 * Although we do not have specific packet, we still need
2583 		 * dummy packet structure to pass device address to HBA.
2584 		 * Allocate one, without sleeping. Fail if pkt cannot be
2585 		 * allocated.
2586 		 */
2587 		sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP);
2588 		if (sata_pkt == NULL) {
2589 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2590 			    sata_device.satadev_addr.cport)));
2591 			SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: "
2592 			    "could not allocate sata_pkt"));
2593 			return (0);
2594 		}
2595 		sata_pkt->satapkt_rev = SATA_PKT_REV;
2596 		sata_pkt->satapkt_device = sata_device;
2597 		sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
2598 	} else {
2599 		if (scsi_pkt->pkt_ha_private == NULL) {
2600 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2601 			    sata_device.satadev_addr.cport)));
2602 			return (0); /* Bad scsi pkt */
2603 		}
2604 		/* extract pointer to sata pkt */
2605 		sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)->
2606 		    txlt_sata_pkt;
2607 	}
2608 
2609 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2610 	    sata_device.satadev_addr.cport)));
2611 	/* Send abort request to HBA */
2612 	if ((*SATA_ABORT_FUNC(sata_hba_inst))
2613 	    (SATA_DIP(sata_hba_inst), sata_pkt,
2614 	    scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) ==
2615 	    SATA_SUCCESS) {
2616 		if (scsi_pkt == NULL)
2617 			kmem_free(sata_pkt, sizeof (sata_pkt_t));
2618 		/* Success */
2619 		return (1);
2620 	}
2621 	/* Else, something did not go right */
2622 	if (scsi_pkt == NULL)
2623 		kmem_free(sata_pkt, sizeof (sata_pkt_t));
2624 	/* Failure */
2625 	return (0);
2626 }
2627 
2628 
2629 /*
2630  * Implementation of scsi tran_reset.
2631  * RESET_ALL request is translated into port reset.
2632  * RESET_TARGET requests is translated into a device reset,
2633  * RESET_LUN request is accepted only for LUN 0 and translated into
2634  * device reset.
2635  * The target reset should cause all HBA active and queued packets to
2636  * be terminated and returned with pkt reason SATA_PKT_RESET prior to
2637  * the return. HBA should report reset event for the device.
2638  *
2639  * Returns 1 upon success, 0 upon failure.
2640  */
2641 static int
2642 sata_scsi_reset(struct scsi_address *ap, int level)
2643 {
2644 	sata_hba_inst_t	*sata_hba_inst =
2645 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2646 	sata_device_t	sata_device;
2647 	int		val;
2648 
2649 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2650 	    "sata_scsi_reset: level %d target: 0x%x\n",
2651 	    level, ap->a_target);
2652 
2653 	/* Validate address */
2654 	val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device);
2655 	if (val == -1)
2656 		/* Invalid address */
2657 		return (0);
2658 
2659 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2660 	    sata_device.satadev_addr.cport)));
2661 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2662 		/* invalid address */
2663 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2664 		    sata_device.satadev_addr.cport)));
2665 		return (0);
2666 	}
2667 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2668 	    sata_device.satadev_addr.cport)));
2669 	if (level == RESET_ALL) {
2670 		/* port reset */
2671 		if (sata_device.satadev_addr.qual == SATA_ADDR_DCPORT)
2672 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2673 		else
2674 			sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
2675 
2676 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2677 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2678 			return (1);
2679 		else
2680 			return (0);
2681 
2682 	} else if (val == 0 &&
2683 	    (level == RESET_TARGET || level == RESET_LUN)) {
2684 		/* reset device (device attached) */
2685 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2686 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2687 			return (1);
2688 		else
2689 			return (0);
2690 	}
2691 	return (0);
2692 }
2693 
2694 
2695 /*
2696  * Implementation of scsi tran_getcap (get transport/device capabilities).
2697  * Supported capabilities for SATA hard disks:
2698  * auto-rqsense		(always supported)
2699  * tagged-qing		(supported if HBA supports it)
2700  * untagged-qing	(could be supported if disk supports it, but because
2701  *			 caching behavior allowing untagged queuing actually
2702  *			 results in reduced performance.  sd tries to throttle
2703  *			 back to only 3 outstanding commands, which may
2704  *			 work for real SCSI disks, but with read ahead
2705  *			 caching, having more than 1 outstanding command
2706  *			 results in cache thrashing.)
2707  * sector_size
2708  * dma_max
2709  * interconnect-type	(INTERCONNECT_SATA)
2710  *
2711  * Supported capabilities for ATAPI CD/DVD devices:
2712  * auto-rqsense		(always supported)
2713  * sector_size
2714  * dma_max
2715  * max-cdb-length
2716  * interconnect-type	(INTERCONNECT_SATA)
2717  *
2718  * Supported capabilities for ATAPI TAPE devices:
2719  * auto-rqsense		(always supported)
2720  * dma_max
2721  * max-cdb-length
2722  *
2723  * Supported capabilities for SATA ATAPI hard disks:
2724  * auto-rqsense		(always supported)
2725  * interconnect-type	(INTERCONNECT_SATA)
2726  * max-cdb-length
2727  *
2728  * Request for other capabilities is rejected as unsupported.
2729  *
2730  * Returns supported capability value, or -1 if capability is unsuppported or
2731  * the address is invalid - no device.
2732  */
2733 
2734 static int
2735 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
2736 {
2737 
2738 	sata_hba_inst_t 	*sata_hba_inst =
2739 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2740 	sata_device_t		sata_device;
2741 	sata_drive_info_t	*sdinfo;
2742 	ddi_dma_attr_t		adj_dma_attr;
2743 	int 			rval;
2744 
2745 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2746 	    "sata_scsi_getcap: target: 0x%x, cap: %s\n",
2747 	    ap->a_target, cap);
2748 
2749 	/*
2750 	 * We want to process the capabilities on per port granularity.
2751 	 * So, we are specifically restricting ourselves to whom != 0
2752 	 * to exclude the controller wide handling.
2753 	 */
2754 	if (cap == NULL || whom == 0)
2755 		return (-1);
2756 
2757 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2758 		/* Invalid address */
2759 		return (-1);
2760 	}
2761 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2762 	    sata_device.satadev_addr.cport)));
2763 	if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) ==
2764 	    NULL) {
2765 		/* invalid address */
2766 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2767 		    sata_device.satadev_addr.cport)));
2768 		return (-1);
2769 	}
2770 
2771 	switch (scsi_hba_lookup_capstr(cap)) {
2772 	case SCSI_CAP_ARQ:
2773 		rval = 1;		/* ARQ supported, turned on */
2774 		break;
2775 
2776 	case SCSI_CAP_SECTOR_SIZE:
2777 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK)
2778 			rval = SATA_DISK_SECTOR_SIZE;	/* fixed size */
2779 		else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD)
2780 			rval = SATA_ATAPI_SECTOR_SIZE;
2781 		else rval = -1;
2782 		break;
2783 
2784 	/*
2785 	 * untagged queuing cause a performance inversion because of
2786 	 * the way sd operates.  Because of this reason we do not
2787 	 * use it when available.
2788 	 */
2789 	case SCSI_CAP_UNTAGGED_QING:
2790 		if (sdinfo->satadrv_features_enabled &
2791 		    SATA_DEV_F_E_UNTAGGED_QING)
2792 			rval = 1;	/* Untagged queuing available */
2793 		else
2794 			rval = -1;	/* Untagged queuing not available */
2795 		break;
2796 
2797 	case SCSI_CAP_TAGGED_QING:
2798 		if ((sdinfo->satadrv_features_enabled &
2799 		    SATA_DEV_F_E_TAGGED_QING) &&
2800 		    (sdinfo->satadrv_max_queue_depth > 1))
2801 			rval = 1;	/* Tagged queuing available */
2802 		else
2803 			rval = -1;	/* Tagged queuing not available */
2804 		break;
2805 
2806 	case SCSI_CAP_DMA_MAX:
2807 		sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst),
2808 		    &adj_dma_attr);
2809 		rval = (int)adj_dma_attr.dma_attr_maxxfer;
2810 		/* We rely on the fact that dma_attr_maxxfer < 0x80000000 */
2811 		break;
2812 
2813 	case SCSI_CAP_INTERCONNECT_TYPE:
2814 		rval = INTERCONNECT_SATA;	/* SATA interconnect type */
2815 		break;
2816 
2817 	case SCSI_CAP_CDB_LEN:
2818 		if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI)
2819 			rval = sdinfo->satadrv_atapi_cdb_len;
2820 		else
2821 			rval = -1;
2822 		break;
2823 
2824 	default:
2825 		rval = -1;
2826 		break;
2827 	}
2828 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2829 	    sata_device.satadev_addr.cport)));
2830 	return (rval);
2831 }
2832 
2833 /*
2834  * Implementation of scsi tran_setcap
2835  *
2836  * Only SCSI_CAP_UNTAGGED_QING and  SCSI_CAP_TAGGED_QING are changeable.
2837  *
2838  */
2839 static int
2840 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
2841 {
2842 	sata_hba_inst_t	*sata_hba_inst =
2843 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2844 	sata_device_t	sata_device;
2845 	sata_drive_info_t	*sdinfo;
2846 	int		rval;
2847 
2848 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2849 	    "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap);
2850 
2851 	/*
2852 	 * We want to process the capabilities on per port granularity.
2853 	 * So, we are specifically restricting ourselves to whom != 0
2854 	 * to exclude the controller wide handling.
2855 	 */
2856 	if (cap == NULL || whom == 0) {
2857 		return (-1);
2858 	}
2859 
2860 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2861 		/* Invalid address */
2862 		return (-1);
2863 	}
2864 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2865 	    sata_device.satadev_addr.cport)));
2866 	if ((sdinfo = sata_get_device_info(sata_hba_inst,
2867 	    &sata_device)) == NULL) {
2868 		/* invalid address */
2869 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2870 		    sata_device.satadev_addr.cport)));
2871 		return (-1);
2872 	}
2873 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2874 	    sata_device.satadev_addr.cport)));
2875 
2876 	switch (scsi_hba_lookup_capstr(cap)) {
2877 	case SCSI_CAP_ARQ:
2878 	case SCSI_CAP_SECTOR_SIZE:
2879 	case SCSI_CAP_DMA_MAX:
2880 	case SCSI_CAP_INTERCONNECT_TYPE:
2881 		rval = 0;
2882 		break;
2883 	case SCSI_CAP_UNTAGGED_QING:
2884 		if (SATA_QDEPTH(sata_hba_inst) > 1) {
2885 			rval = 1;
2886 			if (value == 1) {
2887 				sdinfo->satadrv_features_enabled |=
2888 				    SATA_DEV_F_E_UNTAGGED_QING;
2889 			} else if (value == 0) {
2890 				sdinfo->satadrv_features_enabled &=
2891 				    ~SATA_DEV_F_E_UNTAGGED_QING;
2892 			} else {
2893 				rval = -1;
2894 			}
2895 		} else {
2896 			rval = 0;
2897 		}
2898 		break;
2899 	case SCSI_CAP_TAGGED_QING:
2900 		/* This can TCQ or NCQ */
2901 		if (sata_func_enable & SATA_ENABLE_QUEUING &&
2902 		    ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ &&
2903 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) ||
2904 		    (sata_func_enable & SATA_ENABLE_NCQ &&
2905 		    sdinfo->satadrv_features_support & SATA_DEV_F_NCQ &&
2906 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) &&
2907 		    (sdinfo->satadrv_max_queue_depth > 1)) {
2908 			rval = 1;
2909 			if (value == 1) {
2910 				sdinfo->satadrv_features_enabled |=
2911 				    SATA_DEV_F_E_TAGGED_QING;
2912 			} else if (value == 0) {
2913 				sdinfo->satadrv_features_enabled &=
2914 				    ~SATA_DEV_F_E_TAGGED_QING;
2915 			} else {
2916 				rval = -1;
2917 			}
2918 		} else {
2919 			rval = 0;
2920 		}
2921 		break;
2922 	default:
2923 		rval = -1;
2924 		break;
2925 	}
2926 	return (rval);
2927 }
2928 
2929 /*
2930  * Implementations of scsi tran_destroy_pkt.
2931  * Free resources allocated by sata_scsi_init_pkt()
2932  */
2933 static void
2934 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2935 {
2936 	sata_pkt_txlate_t *spx;
2937 
2938 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2939 
2940 	sata_common_free_dma_rsrcs(spx);
2941 
2942 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2943 	sata_pkt_free(spx);
2944 
2945 	scsi_hba_pkt_free(ap, pkt);
2946 }
2947 
2948 /*
2949  * Implementation of scsi tran_dmafree.
2950  * Free DMA resources allocated by sata_scsi_init_pkt()
2951  */
2952 
2953 static void
2954 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
2955 {
2956 #ifndef __lock_lint
2957 	_NOTE(ARGUNUSED(ap))
2958 #endif
2959 	sata_pkt_txlate_t *spx;
2960 
2961 	ASSERT(pkt != NULL);
2962 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2963 
2964 	sata_common_free_dma_rsrcs(spx);
2965 }
2966 
2967 /*
2968  * Implementation of scsi tran_sync_pkt.
2969  *
2970  * The assumption below is that pkt is unique - there is no need to check ap
2971  *
2972  * Synchronize DMA buffer and, if the intermediate buffer is used, copy data
2973  * into/from the real buffer.
2974  */
2975 static void
2976 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2977 {
2978 #ifndef __lock_lint
2979 	_NOTE(ARGUNUSED(ap))
2980 #endif
2981 	int rval;
2982 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2983 	struct buf *bp;
2984 	int direction;
2985 
2986 	ASSERT(spx != NULL);
2987 	if (spx->txlt_buf_dma_handle != NULL) {
2988 		direction = spx->txlt_sata_pkt->
2989 		    satapkt_cmd.satacmd_flags.sata_data_direction;
2990 		if (spx->txlt_sata_pkt != NULL &&
2991 		    direction != SATA_DIR_NODATA_XFER) {
2992 			if (spx->txlt_tmp_buf != NULL) {
2993 				/* Intermediate DMA buffer used */
2994 				bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2995 
2996 				if (direction & SATA_DIR_WRITE) {
2997 					bcopy(bp->b_un.b_addr,
2998 					    spx->txlt_tmp_buf, bp->b_bcount);
2999 				}
3000 			}
3001 			/* Sync the buffer for device or for CPU */
3002 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle,   0, 0,
3003 			    (direction & SATA_DIR_WRITE) ?
3004 			    DDI_DMA_SYNC_FORDEV :  DDI_DMA_SYNC_FORCPU);
3005 			ASSERT(rval == DDI_SUCCESS);
3006 			if (spx->txlt_tmp_buf != NULL &&
3007 			    !(direction & SATA_DIR_WRITE)) {
3008 				/* Intermediate DMA buffer used for read */
3009 				bcopy(spx->txlt_tmp_buf,
3010 				    bp->b_un.b_addr, bp->b_bcount);
3011 			}
3012 
3013 		}
3014 	}
3015 }
3016 
3017 
3018 
3019 /* *******************  SATA - SCSI Translation functions **************** */
3020 /*
3021  * SCSI to SATA pkt and command translation and SATA to SCSI status/error
3022  * translation.
3023  */
3024 
3025 /*
3026  * Checks if a device exists and can be access and translates common
3027  * scsi_pkt data to sata_pkt data.
3028  *
3029  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and
3030  * sata_pkt was set-up.
3031  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not
3032  * exist and pkt_comp callback was scheduled.
3033  * Returns other TRAN_XXXXX values when error occured and command should be
3034  * rejected with the returned TRAN_XXXXX value.
3035  *
3036  * This function should be called with port mutex held.
3037  */
3038 static int
3039 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason)
3040 {
3041 	sata_drive_info_t *sdinfo;
3042 	sata_device_t sata_device;
3043 	const struct sata_cmd_flags sata_initial_cmd_flags = {
3044 		SATA_DIR_NODATA_XFER,
3045 		/* all other values to 0/FALSE */
3046 	};
3047 	/*
3048 	 * Pkt_reason has to be set if the pkt_comp callback is invoked,
3049 	 * and that implies TRAN_ACCEPT return value. Any other returned value
3050 	 * indicates that the scsi packet was not accepted (the reason will not
3051 	 * be checked by the scsi target driver).
3052 	 * To make debugging easier, we set pkt_reason to know value here.
3053 	 * It may be changed later when different completion reason is
3054 	 * determined.
3055 	 */
3056 	spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
3057 	*reason = CMD_TRAN_ERR;
3058 
3059 	/* Validate address */
3060 	switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst,
3061 	    &spx->txlt_scsi_pkt->pkt_address, &sata_device)) {
3062 
3063 	case -1:
3064 		/* Invalid address or invalid device type */
3065 		return (TRAN_BADPKT);
3066 	case 2:
3067 		/*
3068 		 * Valid address but device type is unknown - Chack if it is
3069 		 * in the reset state and therefore in an indeterminate state.
3070 		 */
3071 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3072 		    &spx->txlt_sata_pkt->satapkt_device);
3073 		if (sdinfo != NULL && (sdinfo->satadrv_event_flags &
3074 		    (SATA_EVNT_DEVICE_RESET |
3075 		    SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3076 			if (!ddi_in_panic()) {
3077 				spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3078 				*reason = CMD_INCOMPLETE;
3079 				SATADBG1(SATA_DBG_SCSI_IF,
3080 				    spx->txlt_sata_hba_inst,
3081 				    "sata_scsi_start: rejecting command "
3082 				    "because of device reset state\n", NULL);
3083 				return (TRAN_BUSY);
3084 			}
3085 		}
3086 		/* FALLTHROUGH */
3087 	case 1:
3088 		/* valid address but no valid device - it has disappeared */
3089 		spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE;
3090 		*reason = CMD_DEV_GONE;
3091 		/*
3092 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
3093 		 * only in callback function (for normal requests) and
3094 		 * in the dump code path.
3095 		 * So, if the callback is available, we need to do
3096 		 * the callback rather than returning TRAN_FATAL_ERROR here.
3097 		 */
3098 		if (spx->txlt_scsi_pkt->pkt_comp != NULL) {
3099 			/* scsi callback required */
3100 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3101 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3102 			    (void *)spx->txlt_scsi_pkt,
3103 			    TQ_SLEEP) == NULL)
3104 				/* Scheduling the callback failed */
3105 				return (TRAN_BUSY);
3106 
3107 			return (TRAN_ACCEPT);
3108 		}
3109 		return (TRAN_FATAL_ERROR);
3110 	default:
3111 		/* all OK; pkt reason will be overwritten later */
3112 		break;
3113 	}
3114 	/*
3115 	 * If in an interrupt context, reject packet if it is to be
3116 	 * executed in polling mode
3117 	 */
3118 	if (servicing_interrupt() &&
3119 	    (spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
3120 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
3121 		    "sata_scsi_start: rejecting synchronous command because "
3122 		    "of interrupt context\n", NULL);
3123 		return (TRAN_BUSY);
3124 	}
3125 
3126 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3127 	    &spx->txlt_sata_pkt->satapkt_device);
3128 
3129 	/*
3130 	 * If device is in reset condition, reject the packet with
3131 	 * TRAN_BUSY, unless:
3132 	 * 1. system is panicking (dumping)
3133 	 * In such case only one thread is running and there is no way to
3134 	 * process reset.
3135 	 * 2. cfgadm operation is is progress (internal APCTL lock is set)
3136 	 * Some cfgadm operations involve drive commands, so reset condition
3137 	 * needs to be ignored for IOCTL operations.
3138 	 */
3139 	if ((sdinfo->satadrv_event_flags &
3140 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3141 
3142 		if (!ddi_in_panic() &&
3143 		    ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst,
3144 		    sata_device.satadev_addr.cport) &
3145 		    SATA_APCTL_LOCK_PORT_BUSY) == 0)) {
3146 			spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3147 			*reason = CMD_INCOMPLETE;
3148 			SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3149 			    "sata_scsi_start: rejecting command because "
3150 			    "of device reset state\n", NULL);
3151 			return (TRAN_BUSY);
3152 		}
3153 	}
3154 
3155 	/*
3156 	 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by
3157 	 * sata_scsi_pkt_init() because pkt init had to work also with
3158 	 * non-existing devices.
3159 	 * Now we know that the packet was set-up for a real device, so its
3160 	 * type is known.
3161 	 */
3162 	spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type;
3163 
3164 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags;
3165 	if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst,
3166 	    sata_device.satadev_addr.cport)->cport_event_flags &
3167 	    SATA_APCTL_LOCK_PORT_BUSY) != 0) {
3168 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3169 		    sata_ignore_dev_reset = B_TRUE;
3170 	}
3171 	/*
3172 	 * At this point the generic translation routine determined that the
3173 	 * scsi packet should be accepted. Packet completion reason may be
3174 	 * changed later when a different completion reason is determined.
3175 	 */
3176 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3177 	*reason = CMD_CMPLT;
3178 
3179 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
3180 		/* Synchronous execution */
3181 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH |
3182 		    SATA_OPMODE_POLLING;
3183 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3184 		    sata_ignore_dev_reset = ddi_in_panic();
3185 	} else {
3186 		/* Asynchronous execution */
3187 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH |
3188 		    SATA_OPMODE_INTERRUPTS;
3189 	}
3190 	/* Convert queuing information */
3191 	if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG)
3192 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag =
3193 		    B_TRUE;
3194 	else if (spx->txlt_scsi_pkt->pkt_flags &
3195 	    (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD))
3196 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag =
3197 		    B_TRUE;
3198 
3199 	/* Always limit pkt time */
3200 	if (spx->txlt_scsi_pkt->pkt_time == 0)
3201 		spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time;
3202 	else
3203 		/* Pass on scsi_pkt time */
3204 		spx->txlt_sata_pkt->satapkt_time =
3205 		    spx->txlt_scsi_pkt->pkt_time;
3206 
3207 	return (TRAN_ACCEPT);
3208 }
3209 
3210 
3211 /*
3212  * Translate ATA Identify Device data to SCSI Inquiry data.
3213  * This function may be called only for ATA devices.
3214  * This function should not be called for ATAPI devices - they
3215  * respond directly to SCSI Inquiry command.
3216  *
3217  * SATA Identify Device data has to be valid in sata_drive_info.
3218  * Buffer has to accomodate the inquiry length (36 bytes).
3219  *
3220  * This function should be called with a port mutex held.
3221  */
3222 static	void
3223 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst,
3224     sata_drive_info_t *sdinfo, uint8_t *buf)
3225 {
3226 
3227 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
3228 	struct sata_id *sid = &sdinfo->satadrv_id;
3229 
3230 	/* Start with a nice clean slate */
3231 	bzero((void *)inq, sizeof (struct scsi_inquiry));
3232 
3233 	/*
3234 	 * Rely on the dev_type for setting paripheral qualifier.
3235 	 * Assume that  DTYPE_RODIRECT applies to CD/DVD R/W devices.
3236 	 * It could be that DTYPE_OPTICAL could also qualify in the future.
3237 	 * ATAPI Inquiry may provide more data to the target driver.
3238 	 */
3239 	inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3240 	    DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */
3241 
3242 	/* CFA type device is not a removable media device */
3243 	inq->inq_rmb = ((sid->ai_config != SATA_CFA_TYPE) &&
3244 	    (sid->ai_config & SATA_REM_MEDIA)) ? 1 : 0;
3245 	inq->inq_qual = 0;	/* Device type qualifier (obsolete in SCSI3? */
3246 	inq->inq_iso = 0;	/* ISO version */
3247 	inq->inq_ecma = 0;	/* ECMA version */
3248 	inq->inq_ansi = 3;	/* ANSI version - SCSI 3 */
3249 	inq->inq_aenc = 0;	/* Async event notification cap. */
3250 	inq->inq_trmiop = 0;	/* Supports TERMINATE I/O PROC msg - NO */
3251 	inq->inq_normaca = 0;	/* setting NACA bit supported - NO */
3252 	inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */
3253 	inq->inq_len = 31;	/* Additional length */
3254 	inq->inq_dualp = 0;	/* dual port device - NO */
3255 	inq->inq_reladdr = 0;	/* Supports relative addressing - NO */
3256 	inq->inq_sync = 0;	/* Supports synchronous data xfers - NO */
3257 	inq->inq_linked = 0;	/* Supports linked commands - NO */
3258 				/*
3259 				 * Queuing support - controller has to
3260 				 * support some sort of command queuing.
3261 				 */
3262 	if (SATA_QDEPTH(sata_hba_inst) > 1)
3263 		inq->inq_cmdque = 1; /* Supports command queueing - YES */
3264 	else
3265 		inq->inq_cmdque = 0; /* Supports command queueing - NO */
3266 	inq->inq_sftre = 0;	/* Supports Soft Reset option - NO ??? */
3267 	inq->inq_wbus32 = 0;	/* Supports 32 bit wide data xfers - NO */
3268 	inq->inq_wbus16 = 0;	/* Supports 16 bit wide data xfers - NO */
3269 
3270 #ifdef	_LITTLE_ENDIAN
3271 	/* Swap text fields to match SCSI format */
3272 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3273 	swab(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3274 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3275 		swab(sid->ai_fw, inq->inq_revision, 4);	/* Revision level */
3276 	else
3277 		swab(&sid->ai_fw[4], inq->inq_revision, 4);	/* Rev. level */
3278 #else	/* _LITTLE_ENDIAN */
3279 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3280 	bcopy(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3281 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3282 		bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
3283 	else
3284 		bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
3285 #endif	/* _LITTLE_ENDIAN */
3286 }
3287 
3288 
3289 /*
3290  * Scsi response set up for invalid command (command not supported)
3291  *
3292  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3293  */
3294 static int
3295 sata_txlt_invalid_command(sata_pkt_txlate_t *spx)
3296 {
3297 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3298 	struct scsi_extended_sense *sense;
3299 
3300 	scsipkt->pkt_reason = CMD_CMPLT;
3301 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3302 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3303 
3304 	*scsipkt->pkt_scbp = STATUS_CHECK;
3305 
3306 	sense = sata_arq_sense(spx);
3307 	sense->es_key = KEY_ILLEGAL_REQUEST;
3308 	sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE;
3309 
3310 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3311 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3312 
3313 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3314 	    scsipkt->pkt_comp != NULL)
3315 		/* scsi callback required */
3316 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3317 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3318 		    (void *)spx->txlt_scsi_pkt,
3319 		    TQ_SLEEP) == NULL)
3320 			/* Scheduling the callback failed */
3321 			return (TRAN_BUSY);
3322 	return (TRAN_ACCEPT);
3323 }
3324 
3325 /*
3326  * Scsi response set up for check condition with special sense key
3327  * and additional sense code.
3328  *
3329  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3330  */
3331 static int
3332 sata_txlt_check_condition(sata_pkt_txlate_t *spx, uchar_t key, uchar_t code)
3333 {
3334 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3335 	int cport = SATA_TXLT_CPORT(spx);
3336 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3337 	struct scsi_extended_sense *sense;
3338 
3339 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3340 	scsipkt->pkt_reason = CMD_CMPLT;
3341 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3342 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3343 
3344 	*scsipkt->pkt_scbp = STATUS_CHECK;
3345 
3346 	sense = sata_arq_sense(spx);
3347 	sense->es_key = key;
3348 	sense->es_add_code = code;
3349 
3350 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3351 
3352 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3353 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3354 
3355 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
3356 		/* scsi callback required */
3357 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3358 		    (task_func_t *)scsi_hba_pkt_comp,
3359 		    (void *)spx->txlt_scsi_pkt,
3360 		    TQ_SLEEP) == NULL)
3361 			/* Scheduling the callback failed */
3362 			return (TRAN_BUSY);
3363 	return (TRAN_ACCEPT);
3364 }
3365 
3366 /*
3367  * Scsi response setup for
3368  * emulated non-data command that requires no action/return data
3369  *
3370  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3371  */
3372 static	int
3373 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx)
3374 {
3375 	int rval;
3376 	int reason;
3377 
3378 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3379 
3380 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3381 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3382 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3383 		return (rval);
3384 	}
3385 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3386 
3387 	spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3388 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3389 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3390 	*(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD;
3391 
3392 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3393 	    "Scsi_pkt completion reason %x\n",
3394 	    spx->txlt_scsi_pkt->pkt_reason);
3395 
3396 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 &&
3397 	    spx->txlt_scsi_pkt->pkt_comp != NULL)
3398 		/* scsi callback required */
3399 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3400 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3401 		    (void *)spx->txlt_scsi_pkt,
3402 		    TQ_SLEEP) == NULL)
3403 			/* Scheduling the callback failed */
3404 			return (TRAN_BUSY);
3405 	return (TRAN_ACCEPT);
3406 }
3407 
3408 
3409 /*
3410  * SATA translate command: Inquiry / Identify Device
3411  * Use cached Identify Device data for now, rather than issuing actual
3412  * Device Identify cmd request. If device is detached and re-attached,
3413  * asynchronous event processing should fetch and refresh Identify Device
3414  * data.
3415  * Two VPD pages are supported now:
3416  * Vital Product Data page
3417  * Unit Serial Number page
3418  *
3419  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3420  */
3421 
3422 #define	EVPD			1	/* Extended Vital Product Data flag */
3423 #define	CMDDT			2	/* Command Support Data - Obsolete */
3424 #define	INQUIRY_SUP_VPD_PAGE	0	/* Supported VDP Pages Page COde */
3425 #define	INQUIRY_USN_PAGE	0x80	/* Unit Serial Number Page Code */
3426 #define	INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */
3427 
3428 static int
3429 sata_txlt_inquiry(sata_pkt_txlate_t *spx)
3430 {
3431 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3432 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3433 	sata_drive_info_t *sdinfo;
3434 	struct scsi_extended_sense *sense;
3435 	int count;
3436 	uint8_t *p;
3437 	int i, j;
3438 	uint8_t page_buf[0xff]; /* Max length */
3439 	int rval, reason;
3440 
3441 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3442 
3443 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3444 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3445 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3446 		return (rval);
3447 	}
3448 
3449 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3450 	    &spx->txlt_sata_pkt->satapkt_device);
3451 
3452 	ASSERT(sdinfo != NULL);
3453 
3454 	scsipkt->pkt_reason = CMD_CMPLT;
3455 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3456 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3457 
3458 	/* Reject not supported request */
3459 	if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */
3460 		*scsipkt->pkt_scbp = STATUS_CHECK;
3461 		sense = sata_arq_sense(spx);
3462 		sense->es_key = KEY_ILLEGAL_REQUEST;
3463 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3464 		goto done;
3465 	}
3466 
3467 	/* Valid Inquiry request */
3468 	*scsipkt->pkt_scbp = STATUS_GOOD;
3469 
3470 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3471 
3472 		/*
3473 		 * Because it is fully emulated command storing data
3474 		 * programatically in the specified buffer, release
3475 		 * preallocated DMA resources before storing data in the buffer,
3476 		 * so no unwanted DMA sync would take place.
3477 		 */
3478 		sata_scsi_dmafree(NULL, scsipkt);
3479 
3480 		if (!(scsipkt->pkt_cdbp[1] & EVPD)) {
3481 			/* Standard Inquiry Data request */
3482 			struct scsi_inquiry inq;
3483 			unsigned int bufsize;
3484 
3485 			sata_identdev_to_inquiry(spx->txlt_sata_hba_inst,
3486 			    sdinfo, (uint8_t *)&inq);
3487 			/* Copy no more than requested */
3488 			count = MIN(bp->b_bcount,
3489 			    sizeof (struct scsi_inquiry));
3490 			bufsize = scsipkt->pkt_cdbp[4];
3491 			bufsize |= scsipkt->pkt_cdbp[3] << 8;
3492 			count = MIN(count, bufsize);
3493 			bcopy(&inq, bp->b_un.b_addr, count);
3494 
3495 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
3496 			scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3497 			    bufsize - count : 0;
3498 		} else {
3499 			/*
3500 			 * peripheral_qualifier = 0;
3501 			 *
3502 			 * We are dealing only with HD and will be
3503 			 * dealing with CD/DVD devices soon
3504 			 */
3505 			uint8_t peripheral_device_type =
3506 			    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3507 			    DTYPE_DIRECT : DTYPE_RODIRECT;
3508 
3509 			switch ((uint_t)scsipkt->pkt_cdbp[2]) {
3510 			case INQUIRY_SUP_VPD_PAGE:
3511 				/*
3512 				 * Request for suported Vital Product Data
3513 				 * pages - assuming only 2 page codes
3514 				 * supported.
3515 				 */
3516 				page_buf[0] = peripheral_device_type;
3517 				page_buf[1] = INQUIRY_SUP_VPD_PAGE;
3518 				page_buf[2] = 0;
3519 				page_buf[3] = 2; /* page length */
3520 				page_buf[4] = INQUIRY_SUP_VPD_PAGE;
3521 				page_buf[5] = INQUIRY_USN_PAGE;
3522 				/* Copy no more than requested */
3523 				count = MIN(bp->b_bcount, 6);
3524 				bcopy(page_buf, bp->b_un.b_addr, count);
3525 				break;
3526 
3527 			case INQUIRY_USN_PAGE:
3528 				/*
3529 				 * Request for Unit Serial Number page.
3530 				 * Set-up the page.
3531 				 */
3532 				page_buf[0] = peripheral_device_type;
3533 				page_buf[1] = INQUIRY_USN_PAGE;
3534 				page_buf[2] = 0;
3535 				/* remaining page length */
3536 				page_buf[3] = SATA_ID_SERIAL_LEN;
3537 
3538 				/*
3539 				 * Copy serial number from Identify Device data
3540 				 * words into the inquiry page and swap bytes
3541 				 * when necessary.
3542 				 */
3543 				p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser);
3544 #ifdef	_LITTLE_ENDIAN
3545 				swab(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3546 #else
3547 				bcopy(p, &page_buf[4], SATA_ID_SERIAL_LEN);
3548 #endif
3549 				/*
3550 				 * Least significant character of the serial
3551 				 * number shall appear as the last byte,
3552 				 * according to SBC-3 spec.
3553 				 * Count trailing spaces to determine the
3554 				 * necessary shift length.
3555 				 */
3556 				p = &page_buf[SATA_ID_SERIAL_LEN + 4 - 1];
3557 				for (j = 0; j < SATA_ID_SERIAL_LEN; j++) {
3558 					if (*(p - j) != '\0' &&
3559 					    *(p - j) != '\040')
3560 						break;
3561 				}
3562 
3563 				/*
3564 				 * Shift SN string right, so that the last
3565 				 * non-blank character would appear in last
3566 				 * byte of SN field in the page.
3567 				 * 'j' is the shift length.
3568 				 */
3569 				for (i = 0;
3570 				    i < (SATA_ID_SERIAL_LEN - j) && j != 0;
3571 				    i++, p--)
3572 					*p = *(p - j);
3573 
3574 				/*
3575 				 * Add leading spaces - same number as the
3576 				 * shift size
3577 				 */
3578 				for (; j > 0; j--)
3579 					page_buf[4 + j - 1] = '\040';
3580 
3581 				count = MIN(bp->b_bcount,
3582 				    SATA_ID_SERIAL_LEN + 4);
3583 				bcopy(page_buf, bp->b_un.b_addr, count);
3584 				break;
3585 
3586 			case INQUIRY_DEV_IDENTIFICATION_PAGE:
3587 				/*
3588 				 * We may want to implement this page, when
3589 				 * identifiers are common for SATA devices
3590 				 * But not now.
3591 				 */
3592 				/*FALLTHROUGH*/
3593 
3594 			default:
3595 				/* Request for unsupported VPD page */
3596 				*scsipkt->pkt_scbp = STATUS_CHECK;
3597 				sense = sata_arq_sense(spx);
3598 				sense->es_key = KEY_ILLEGAL_REQUEST;
3599 				sense->es_add_code =
3600 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3601 				goto done;
3602 			}
3603 		}
3604 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3605 		scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3606 		    scsipkt->pkt_cdbp[4] - count : 0;
3607 	}
3608 done:
3609 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3610 
3611 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3612 	    "Scsi_pkt completion reason %x\n",
3613 	    scsipkt->pkt_reason);
3614 
3615 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3616 	    scsipkt->pkt_comp != NULL) {
3617 		/* scsi callback required */
3618 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3619 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3620 		    TQ_SLEEP) == NULL)
3621 			/* Scheduling the callback failed */
3622 			return (TRAN_BUSY);
3623 	}
3624 	return (TRAN_ACCEPT);
3625 }
3626 
3627 /*
3628  * SATA translate command: Request Sense.
3629  *
3630  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3631  * At the moment this is an emulated command (ATA version for SATA hard disks).
3632  * May be translated into Check Power Mode command in the future.
3633  *
3634  * Note: There is a mismatch between already implemented Informational
3635  * Exception Mode Select page 0x1C and this function.
3636  * When MRIE bit is set in page 0x1C, Request Sense is supposed to return
3637  * NO SENSE and set additional sense code to the exception code - this is not
3638  * implemented here.
3639  */
3640 static int
3641 sata_txlt_request_sense(sata_pkt_txlate_t *spx)
3642 {
3643 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3644 	struct scsi_extended_sense sense;
3645 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3646 	sata_drive_info_t *sdinfo;
3647 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3648 	int rval, reason, power_state = 0;
3649 
3650 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3651 
3652 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3653 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3654 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3655 		return (rval);
3656 	}
3657 
3658 	scsipkt->pkt_reason = CMD_CMPLT;
3659 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3660 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3661 	*scsipkt->pkt_scbp = STATUS_GOOD;
3662 
3663 	/*
3664 	 * when CONTROL field's NACA bit == 1
3665 	 * return ILLEGAL_REQUEST
3666 	 */
3667 	if (scsipkt->pkt_cdbp[5] & CTL_BYTE_NACA_MASK) {
3668 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3669 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
3670 		    SD_SCSI_ASC_CMD_SEQUENCE_ERR));
3671 	}
3672 
3673 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3674 	    &spx->txlt_sata_pkt->satapkt_device);
3675 	ASSERT(sdinfo != NULL);
3676 
3677 	spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
3678 
3679 	sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
3680 	scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
3681 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3682 	if (sata_hba_start(spx, &rval) != 0) {
3683 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3684 		return (rval);
3685 	} else {
3686 		if (scmd->satacmd_error_reg != 0) {
3687 			mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3688 			return (sata_txlt_check_condition(spx, KEY_NO_SENSE,
3689 			    SD_SCSI_ASC_NO_ADD_SENSE));
3690 		}
3691 	}
3692 
3693 	switch (scmd->satacmd_sec_count_lsb) {
3694 	case SATA_PWRMODE_STANDBY: /* device in standby mode */
3695 		if (sdinfo->satadrv_power_level == SATA_POWER_STOPPED)
3696 			power_state = SATA_POWER_STOPPED;
3697 		else {
3698 			power_state = SATA_POWER_STANDBY;
3699 			sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
3700 		}
3701 		break;
3702 	case SATA_PWRMODE_IDLE: /* device in idle mode */
3703 		power_state = SATA_POWER_IDLE;
3704 		sdinfo->satadrv_power_level = SATA_POWER_IDLE;
3705 		break;
3706 	case SATA_PWRMODE_ACTIVE: /* device in active or idle mode */
3707 	default:		  /* 0x40, 0x41 active mode */
3708 		if (sdinfo->satadrv_power_level == SATA_POWER_IDLE)
3709 			power_state = SATA_POWER_IDLE;
3710 		else {
3711 			power_state = SATA_POWER_ACTIVE;
3712 			sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
3713 		}
3714 		break;
3715 	}
3716 
3717 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3718 
3719 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3720 		/*
3721 		 * Because it is fully emulated command storing data
3722 		 * programatically in the specified buffer, release
3723 		 * preallocated DMA resources before storing data in the buffer,
3724 		 * so no unwanted DMA sync would take place.
3725 		 */
3726 		int count = MIN(bp->b_bcount,
3727 		    sizeof (struct scsi_extended_sense));
3728 		sata_scsi_dmafree(NULL, scsipkt);
3729 		bzero(&sense, sizeof (struct scsi_extended_sense));
3730 		sense.es_valid = 0;	/* Valid LBA */
3731 		sense.es_class = 7;	/* Response code 0x70 - current err */
3732 		sense.es_key = KEY_NO_SENSE;
3733 		sense.es_add_len = 6;	/* Additional length */
3734 		/* Copy no more than requested */
3735 		bcopy(&sense, bp->b_un.b_addr, count);
3736 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3737 		scsipkt->pkt_resid = 0;
3738 		switch (power_state) {
3739 		case SATA_POWER_IDLE:
3740 		case SATA_POWER_STANDBY:
3741 			sense.es_add_code =
3742 			    SD_SCSI_ASC_LOW_POWER_CONDITION_ON;
3743 			break;
3744 		case SATA_POWER_STOPPED:
3745 			sense.es_add_code = SD_SCSI_ASC_NO_ADD_SENSE;
3746 			break;
3747 		case SATA_POWER_ACTIVE:
3748 		default:
3749 			break;
3750 		}
3751 	}
3752 
3753 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3754 	    "Scsi_pkt completion reason %x\n",
3755 	    scsipkt->pkt_reason);
3756 
3757 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
3758 		/* scsi callback required */
3759 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3760 		    (task_func_t *)scsi_hba_pkt_comp, (void *) scsipkt,
3761 		    TQ_SLEEP) == NULL)
3762 			/* Scheduling the callback failed */
3763 			return (TRAN_BUSY);
3764 	return (TRAN_ACCEPT);
3765 }
3766 
3767 /*
3768  * SATA translate command: Test Unit Ready
3769  * (ATA version for SATA hard disks).
3770  * It is translated into the Check Power Mode command.
3771  *
3772  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3773  */
3774 static int
3775 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx)
3776 {
3777 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3778 	struct scsi_extended_sense *sense;
3779 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3780 	sata_drive_info_t *sdinfo;
3781 	int power_state;
3782 	int rval, reason;
3783 
3784 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3785 
3786 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3787 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3788 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3789 		return (rval);
3790 	}
3791 
3792 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3793 	    &spx->txlt_sata_pkt->satapkt_device);
3794 	ASSERT(sdinfo != NULL);
3795 
3796 	spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
3797 
3798 	/* send CHECK POWER MODE command */
3799 	sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
3800 	scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
3801 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3802 	if (sata_hba_start(spx, &rval) != 0) {
3803 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3804 		return (rval);
3805 	} else {
3806 		if (scmd->satacmd_error_reg != 0) {
3807 			mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3808 			return (sata_txlt_check_condition(spx, KEY_NOT_READY,
3809 			    SD_SCSI_ASC_LU_NOT_RESPONSE));
3810 		}
3811 	}
3812 
3813 	power_state = scmd->satacmd_sec_count_lsb;
3814 
3815 	/*
3816 	 * return NOT READY when device in STOPPED mode
3817 	 */
3818 	if (power_state == SATA_PWRMODE_STANDBY &&
3819 	    sdinfo->satadrv_power_level == SATA_POWER_STOPPED) {
3820 		*scsipkt->pkt_scbp = STATUS_CHECK;
3821 		sense = sata_arq_sense(spx);
3822 		sense->es_key = KEY_NOT_READY;
3823 		sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY;
3824 	} else {
3825 		/*
3826 		 * For other power mode, return GOOD status
3827 		 */
3828 		*scsipkt->pkt_scbp = STATUS_GOOD;
3829 	}
3830 
3831 	scsipkt->pkt_reason = CMD_CMPLT;
3832 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3833 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3834 
3835 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3836 
3837 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3838 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3839 
3840 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
3841 		/* scsi callback required */
3842 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3843 		    (task_func_t *)scsi_hba_pkt_comp, (void *) scsipkt,
3844 		    TQ_SLEEP) == NULL)
3845 			/* Scheduling the callback failed */
3846 			return (TRAN_BUSY);
3847 
3848 	return (TRAN_ACCEPT);
3849 }
3850 
3851 /*
3852  * SATA translate command: Start Stop Unit
3853  * Translation depends on a command:
3854  *
3855  * Power condition bits will be supported
3856  * and the power level should be maintained by SATL,
3857  * When SATL received a command, it will check the
3858  * power level firstly, and return the status according
3859  * to SAT2 v2.6 and SAT-2 Standby Modifications
3860  *
3861  * SPC-4/SBC-3      SATL    ATA power condition  SATL      SPC/SBC
3862  * -----------------------------------------------------------------------
3863  * SSU_PC1 Active   <==>     ATA  Active         <==>     SSU:start_bit =1
3864  * SSU_PC2 Idle     <==>     ATA  Idle           <==>     N/A
3865  * SSU_PC3 Standby  <==>     ATA  Standby        <==>     N/A
3866  * SSU_PC4 Stopped  <==>     ATA  Standby        <==>     SSU:start_bit = 0
3867  *
3868  *	Unload Media / NOT SUPPORTED YET
3869  *	Load Media / NOT SUPPROTED YET
3870  *	Immediate bit / NOT SUPPORTED YET (deferred error)
3871  *
3872  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
3873  * appropriate values in scsi_pkt fields.
3874  */
3875 static int
3876 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx)
3877 {
3878 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3879 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3880 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3881 	int cport = SATA_TXLT_CPORT(spx);
3882 	int rval, reason;
3883 	sata_drive_info_t *sdinfo;
3884 	sata_id_t *sata_id;
3885 
3886 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3887 	    "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1);
3888 
3889 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3890 
3891 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3892 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3893 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3894 		return (rval);
3895 	}
3896 
3897 	if (scsipkt->pkt_cdbp[1] & START_STOP_IMMED_MASK) {
3898 		/* IMMED bit - not supported */
3899 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3900 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
3901 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
3902 	}
3903 
3904 	spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
3905 	spx->txlt_sata_pkt->satapkt_comp = NULL;
3906 
3907 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3908 	    &spx->txlt_sata_pkt->satapkt_device);
3909 	ASSERT(sdinfo != NULL);
3910 	sata_id = &sdinfo->satadrv_id;
3911 
3912 	switch ((scsipkt->pkt_cdbp[4] & START_STOP_POWER_COND_MASK) >> 4) {
3913 	case 0:
3914 		if (scsipkt->pkt_cdbp[4] & START_STOP_LOEJ_MASK) {
3915 			/* Load/Unload Media - invalid request */
3916 			goto err_out;
3917 		}
3918 		if (scsipkt->pkt_cdbp[4] & START_STOP_START_MASK) {
3919 			/* Start Unit */
3920 			sata_build_read_verify_cmd(scmd, 1, 5);
3921 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3922 			/* Transfer command to HBA */
3923 			if (sata_hba_start(spx, &rval) != 0) {
3924 				/* Pkt not accepted for execution */
3925 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3926 				return (rval);
3927 			} else {
3928 				if (scmd->satacmd_error_reg != 0) {
3929 					goto err_out;
3930 				}
3931 			}
3932 			sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
3933 		} else {
3934 			/* Stop Unit */
3935 			sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
3936 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3937 			if (sata_hba_start(spx, &rval) != 0) {
3938 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3939 				return (rval);
3940 			} else {
3941 				if (scmd->satacmd_error_reg != 0) {
3942 					goto err_out;
3943 				}
3944 			}
3945 			/* ata standby immediate command */
3946 			sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
3947 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3948 			if (sata_hba_start(spx, &rval) != 0) {
3949 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3950 				return (rval);
3951 			} else {
3952 				if (scmd->satacmd_error_reg != 0) {
3953 					goto err_out;
3954 				}
3955 			}
3956 			sdinfo->satadrv_power_level = SATA_POWER_STOPPED;
3957 		}
3958 		break;
3959 	case 0x1:
3960 		sata_build_generic_cmd(scmd, SATAC_IDLE);
3961 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3962 		if (sata_hba_start(spx, &rval) != 0) {
3963 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3964 			return (rval);
3965 		} else {
3966 			if (scmd->satacmd_error_reg != 0) {
3967 				goto err_out;
3968 			}
3969 		}
3970 		sata_build_read_verify_cmd(scmd, 1, 5);
3971 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3972 		/* Transfer command to HBA */
3973 		if (sata_hba_start(spx, &rval) != 0) {
3974 			/* Pkt not accepted for execution */
3975 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3976 			return (rval);
3977 		} else {
3978 			if (scmd->satacmd_error_reg != 0) {
3979 				goto err_out;
3980 			}
3981 		}
3982 		sdinfo->satadrv_power_level = SATA_POWER_ACTIVE;
3983 		break;
3984 	case 0x2:
3985 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
3986 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3987 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
3988 			if (sata_hba_start(spx, &rval) != 0) {
3989 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3990 				return (rval);
3991 			} else {
3992 				if (scmd->satacmd_error_reg != 0) {
3993 					goto err_out;
3994 				}
3995 			}
3996 		}
3997 		sata_build_generic_cmd(scmd, SATAC_IDLE);
3998 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
3999 		if (sata_hba_start(spx, &rval) != 0) {
4000 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4001 			return (rval);
4002 		} else {
4003 			if (scmd->satacmd_error_reg != 0) {
4004 				goto err_out;
4005 			}
4006 		}
4007 		if ((scsipkt->pkt_cdbp[3] & START_STOP_MODIFIER_MASK)) {
4008 			/*
4009 			 *  POWER CONDITION MODIFIER bit set
4010 			 *  to 0x1 or larger it will be handled
4011 			 *  on the same way as bit = 0x1
4012 			 */
4013 			if (!(sata_id->ai_cmdset84 &
4014 			    SATA_IDLE_UNLOAD_SUPPORTED)) {
4015 				sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4016 				break;
4017 			}
4018 			sata_build_generic_cmd(scmd, SATAC_IDLE_IM);
4019 			scmd->satacmd_features_reg = 0x44;
4020 			scmd->satacmd_lba_low_lsb = 0x4c;
4021 			scmd->satacmd_lba_mid_lsb = 0x4e;
4022 			scmd->satacmd_lba_high_lsb = 0x55;
4023 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4024 			if (sata_hba_start(spx, &rval) != 0) {
4025 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4026 				return (rval);
4027 			} else {
4028 				if (scmd->satacmd_error_reg != 0) {
4029 					goto err_out;
4030 				}
4031 			}
4032 		}
4033 		sdinfo->satadrv_power_level = SATA_POWER_IDLE;
4034 		break;
4035 	case 0x3:
4036 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4037 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4038 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4039 			if (sata_hba_start(spx, &rval) != 0) {
4040 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4041 				return (rval);
4042 			} else {
4043 				if (scmd->satacmd_error_reg != 0) {
4044 					goto err_out;
4045 				}
4046 			}
4047 		}
4048 		sata_build_generic_cmd(scmd, SATAC_STANDBY);
4049 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4050 		if (sata_hba_start(spx, &rval) != 0) {
4051 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4052 			return (rval);
4053 		} else {
4054 			if (scmd->satacmd_error_reg != 0) {
4055 				goto err_out;
4056 			}
4057 		}
4058 		sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
4059 		break;
4060 	case 0x7:
4061 		sata_build_generic_cmd(scmd, SATAC_CHECK_POWER_MODE);
4062 		scmd->satacmd_flags.sata_copy_out_sec_count_lsb = B_TRUE;
4063 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4064 		if (sata_hba_start(spx, &rval) != 0) {
4065 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4066 			return (rval);
4067 		} else {
4068 			if (scmd->satacmd_error_reg != 0) {
4069 				goto err_out;
4070 			}
4071 		}
4072 		switch (scmd->satacmd_sec_count_lsb) {
4073 		case SATA_PWRMODE_STANDBY:
4074 			sata_build_generic_cmd(scmd, SATAC_STANDBY);
4075 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4076 			    sdinfo->satadrv_standby_timer);
4077 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4078 			if (sata_hba_start(spx, &rval) != 0) {
4079 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4080 				return (rval);
4081 			} else {
4082 				if (scmd->satacmd_error_reg != 0) {
4083 					goto err_out;
4084 				}
4085 			}
4086 			break;
4087 		case SATA_PWRMODE_IDLE:
4088 			sata_build_generic_cmd(scmd, SATAC_IDLE);
4089 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4090 			    sdinfo->satadrv_standby_timer);
4091 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4092 			if (sata_hba_start(spx, &rval) != 0) {
4093 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4094 				return (rval);
4095 			} else {
4096 				if (scmd->satacmd_error_reg != 0) {
4097 					goto err_out;
4098 				}
4099 			}
4100 			break;
4101 		case SATA_PWRMODE_ACTIVE_SPINDOWN:
4102 		case SATA_PWRMODE_ACTIVE_SPINUP:
4103 		case SATA_PWRMODE_ACTIVE:
4104 			sata_build_generic_cmd(scmd, SATAC_IDLE);
4105 			scmd->satacmd_sec_count_msb = sata_get_standby_timer(
4106 			    sdinfo->satadrv_standby_timer);
4107 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4108 			if (sata_hba_start(spx, &rval) != 0) {
4109 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4110 				return (rval);
4111 			} else {
4112 				if (scmd->satacmd_error_reg != 0) {
4113 					goto err_out;
4114 				}
4115 			}
4116 			sata_build_read_verify_cmd(scmd, 1, 5);
4117 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4118 			if (sata_hba_start(spx, &rval) != 0) {
4119 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4120 				return (rval);
4121 			} else {
4122 				if (scmd->satacmd_error_reg != 0) {
4123 					goto err_out;
4124 				}
4125 			}
4126 			break;
4127 		default:
4128 			goto err_out;
4129 		}
4130 		break;
4131 	case 0xb:
4132 		if ((sata_get_standby_timer(sdinfo->satadrv_standby_timer) ==
4133 		    0) || (!(sata_id->ai_cap & SATA_STANDBYTIMER))) {
4134 			mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4135 			return (sata_txlt_check_condition(spx,
4136 			    KEY_ILLEGAL_REQUEST,
4137 			    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4138 		}
4139 		sata_build_generic_cmd(scmd, SATAC_FLUSH_CACHE);
4140 		scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4141 		if (!(scsipkt->pkt_cdbp[4] & START_STOP_NOFLUSH_MASK)) {
4142 			if (sata_hba_start(spx, &rval) != 0) {
4143 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4144 				return (rval);
4145 			} else {
4146 				if (scmd->satacmd_error_reg != 0) {
4147 					goto err_out;
4148 				}
4149 			}
4150 			sata_build_generic_cmd(scmd, SATAC_STANDBY_IM);
4151 			scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
4152 			if (sata_hba_start(spx, &rval) != 0) {
4153 				mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4154 				return (rval);
4155 			} else {
4156 				if (scmd->satacmd_error_reg != 0) {
4157 					goto err_out;
4158 				}
4159 			}
4160 		}
4161 		bzero(sdinfo->satadrv_standby_timer, sizeof (uchar_t) * 4);
4162 		break;
4163 	default:
4164 err_out:
4165 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4166 		return (sata_txlt_check_condition(spx, KEY_ILLEGAL_REQUEST,
4167 		    SD_SCSI_ASC_INVALID_FIELD_IN_CDB));
4168 	}
4169 
4170 	/*
4171 	 * Since it was a synchronous command,
4172 	 * a callback function will be called directly.
4173 	 */
4174 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4175 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4176 	    "synchronous execution status %x\n",
4177 	    spx->txlt_sata_pkt->satapkt_reason);
4178 
4179 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0) {
4180 		sata_set_arq_data(spx->txlt_sata_pkt);
4181 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4182 		    (task_func_t *)scsi_hba_pkt_comp, (void *) scsipkt,
4183 		    TQ_SLEEP) == 0) {
4184 			return (TRAN_BUSY);
4185 		}
4186 	}
4187 	else
4188 
4189 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
4190 
4191 	return (TRAN_ACCEPT);
4192 
4193 }
4194 
4195 /*
4196  * SATA translate command:  Read Capacity.
4197  * Emulated command for SATA disks.
4198  * Capacity is retrieved from cached Idenifty Device data.
4199  * Identify Device data shows effective disk capacity, not the native
4200  * capacity, which may be limitted by Set Max Address command.
4201  * This is ATA version for SATA hard disks.
4202  *
4203  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4204  */
4205 static int
4206 sata_txlt_read_capacity(sata_pkt_txlate_t *spx)
4207 {
4208 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4209 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4210 	sata_drive_info_t *sdinfo;
4211 	uint64_t val;
4212 	uchar_t *rbuf;
4213 	int rval, reason;
4214 
4215 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4216 	    "sata_txlt_read_capacity: ", NULL);
4217 
4218 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4219 
4220 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4221 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4222 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4223 		return (rval);
4224 	}
4225 
4226 	scsipkt->pkt_reason = CMD_CMPLT;
4227 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4228 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4229 	*scsipkt->pkt_scbp = STATUS_GOOD;
4230 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4231 		/*
4232 		 * Because it is fully emulated command storing data
4233 		 * programatically in the specified buffer, release
4234 		 * preallocated DMA resources before storing data in the buffer,
4235 		 * so no unwanted DMA sync would take place.
4236 		 */
4237 		sata_scsi_dmafree(NULL, scsipkt);
4238 
4239 		sdinfo = sata_get_device_info(
4240 		    spx->txlt_sata_hba_inst,
4241 		    &spx->txlt_sata_pkt->satapkt_device);
4242 		/* Last logical block address */
4243 		val = sdinfo->satadrv_capacity - 1;
4244 		rbuf = (uchar_t *)bp->b_un.b_addr;
4245 		/* Need to swap endians to match scsi format */
4246 		rbuf[0] = (val >> 24) & 0xff;
4247 		rbuf[1] = (val >> 16) & 0xff;
4248 		rbuf[2] = (val >> 8) & 0xff;
4249 		rbuf[3] = val & 0xff;
4250 		/* block size - always 512 bytes, for now */
4251 		rbuf[4] = 0;
4252 		rbuf[5] = 0;
4253 		rbuf[6] = 0x02;
4254 		rbuf[7] = 0;
4255 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4256 		scsipkt->pkt_resid = 0;
4257 
4258 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n",
4259 		    sdinfo->satadrv_capacity -1);
4260 	}
4261 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4262 	/*
4263 	 * If a callback was requested, do it now.
4264 	 */
4265 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4266 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4267 
4268 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4269 	    scsipkt->pkt_comp != NULL)
4270 		/* scsi callback required */
4271 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4272 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4273 		    TQ_SLEEP) == NULL)
4274 			/* Scheduling the callback failed */
4275 			return (TRAN_BUSY);
4276 
4277 	return (TRAN_ACCEPT);
4278 }
4279 
4280 /*
4281  * SATA translate command: Mode Sense.
4282  * Translated into appropriate SATA command or emulated.
4283  * Saved Values Page Control (03) are not supported.
4284  *
4285  * NOTE: only caching mode sense page is currently implemented.
4286  *
4287  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4288  */
4289 
4290 #define	LLBAA	0x10	/* Long LBA Accepted */
4291 
4292 static int
4293 sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
4294 {
4295 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4296 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4297 	sata_drive_info_t *sdinfo;
4298 	sata_id_t *sata_id;
4299 	struct scsi_extended_sense *sense;
4300 	int 		len, bdlen, count, alc_len;
4301 	int		pc;	/* Page Control code */
4302 	uint8_t		*buf;	/* mode sense buffer */
4303 	int		rval, reason;
4304 
4305 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4306 	    "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
4307 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4308 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4309 
4310 	buf = kmem_zalloc(1024, KM_SLEEP);
4311 
4312 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4313 
4314 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4315 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4316 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4317 		kmem_free(buf, 1024);
4318 		return (rval);
4319 	}
4320 
4321 	scsipkt->pkt_reason = CMD_CMPLT;
4322 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4323 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4324 
4325 	pc = scsipkt->pkt_cdbp[2] >> 6;
4326 
4327 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4328 		/*
4329 		 * Because it is fully emulated command storing data
4330 		 * programatically in the specified buffer, release
4331 		 * preallocated DMA resources before storing data in the buffer,
4332 		 * so no unwanted DMA sync would take place.
4333 		 */
4334 		sata_scsi_dmafree(NULL, scsipkt);
4335 
4336 		len = 0;
4337 		bdlen = 0;
4338 		if (!(scsipkt->pkt_cdbp[1] & 8)) {
4339 			if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
4340 			    (scsipkt->pkt_cdbp[1] & LLBAA))
4341 				bdlen = 16;
4342 			else
4343 				bdlen = 8;
4344 		}
4345 		/* Build mode parameter header */
4346 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
4347 			/* 4-byte mode parameter header */
4348 			buf[len++] = 0;		/* mode data length */
4349 			buf[len++] = 0;		/* medium type */
4350 			buf[len++] = 0;		/* dev-specific param */
4351 			buf[len++] = bdlen;	/* Block Descriptor length */
4352 		} else {
4353 			/* 8-byte mode parameter header */
4354 			buf[len++] = 0;		/* mode data length */
4355 			buf[len++] = 0;
4356 			buf[len++] = 0;		/* medium type */
4357 			buf[len++] = 0;		/* dev-specific param */
4358 			if (bdlen == 16)
4359 				buf[len++] = 1;	/* long lba descriptor */
4360 			else
4361 				buf[len++] = 0;
4362 			buf[len++] = 0;
4363 			buf[len++] = 0;		/* Block Descriptor length */
4364 			buf[len++] = bdlen;
4365 		}
4366 
4367 		sdinfo = sata_get_device_info(
4368 		    spx->txlt_sata_hba_inst,
4369 		    &spx->txlt_sata_pkt->satapkt_device);
4370 
4371 		/* Build block descriptor only if not disabled (DBD) */
4372 		if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
4373 			/* Block descriptor - direct-access device format */
4374 			if (bdlen == 8) {
4375 				/* build regular block descriptor */
4376 				buf[len++] =
4377 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
4378 				buf[len++] =
4379 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
4380 				buf[len++] =
4381 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
4382 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
4383 				buf[len++] = 0; /* density code */
4384 				buf[len++] = 0;
4385 				if (sdinfo->satadrv_type ==
4386 				    SATA_DTYPE_ATADISK)
4387 					buf[len++] = 2;
4388 				else
4389 					/* ATAPI */
4390 					buf[len++] = 8;
4391 				buf[len++] = 0;
4392 			} else if (bdlen == 16) {
4393 				/* Long LBA Accepted */
4394 				/* build long lba block descriptor */
4395 #ifndef __lock_lint
4396 				buf[len++] =
4397 				    (sdinfo->satadrv_capacity >> 56) & 0xff;
4398 				buf[len++] =
4399 				    (sdinfo->satadrv_capacity >> 48) & 0xff;
4400 				buf[len++] =
4401 				    (sdinfo->satadrv_capacity >> 40) & 0xff;
4402 				buf[len++] =
4403 				    (sdinfo->satadrv_capacity >> 32) & 0xff;
4404 #endif
4405 				buf[len++] =
4406 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
4407 				buf[len++] =
4408 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
4409 				buf[len++] =
4410 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
4411 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
4412 				buf[len++] = 0;
4413 				buf[len++] = 0; /* density code */
4414 				buf[len++] = 0;
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 			}
4424 		}
4425 
4426 		sata_id = &sdinfo->satadrv_id;
4427 
4428 		/*
4429 		 * Add requested pages.
4430 		 * Page 3 and 4 are obsolete and we are not supporting them.
4431 		 * We deal now with:
4432 		 * caching (read/write cache control).
4433 		 * We should eventually deal with following mode pages:
4434 		 * error recovery  (0x01),
4435 		 * power condition (0x1a),
4436 		 * exception control page (enables SMART) (0x1c),
4437 		 * enclosure management (ses),
4438 		 * protocol-specific port mode (port control).
4439 		 */
4440 		switch (scsipkt->pkt_cdbp[2] & 0x3f) {
4441 		case MODEPAGE_RW_ERRRECOV:
4442 			/* DAD_MODE_ERR_RECOV */
4443 			/* R/W recovery */
4444 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
4445 			break;
4446 		case MODEPAGE_CACHING:
4447 			/* DAD_MODE_CACHE */
4448 			/* Reject not supported request for saved parameters */
4449 			if (pc == 3) {
4450 				*scsipkt->pkt_scbp = STATUS_CHECK;
4451 				sense = sata_arq_sense(spx);
4452 				sense->es_key = KEY_ILLEGAL_REQUEST;
4453 				sense->es_add_code =
4454 				    SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
4455 				goto done;
4456 			}
4457 
4458 			/* caching */
4459 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
4460 			break;
4461 		case MODEPAGE_INFO_EXCPT:
4462 			/* exception cntrl */
4463 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
4464 				len += sata_build_msense_page_1c(sdinfo, pc,
4465 				    buf+len);
4466 			}
4467 			else
4468 				goto err;
4469 			break;
4470 		case MODEPAGE_POWER_COND:
4471 			/* DAD_MODE_POWER_COND */
4472 			/* power condition */
4473 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
4474 			break;
4475 
4476 		case MODEPAGE_ACOUSTIC_MANAG:
4477 			/* acoustic management */
4478 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
4479 			break;
4480 		case MODEPAGE_ALLPAGES:
4481 			/* all pages */
4482 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
4483 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
4484 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
4485 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
4486 				len += sata_build_msense_page_1c(sdinfo, pc,
4487 				    buf+len);
4488 			}
4489 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
4490 			break;
4491 		default:
4492 		err:
4493 			/* Invalid request */
4494 			*scsipkt->pkt_scbp = STATUS_CHECK;
4495 			sense = sata_arq_sense(spx);
4496 			sense->es_key = KEY_ILLEGAL_REQUEST;
4497 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4498 			goto done;
4499 		}
4500 
4501 		/* fix total mode data length */
4502 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
4503 			/* 4-byte mode parameter header */
4504 			buf[0] = len - 1;	/* mode data length */
4505 		} else {
4506 			buf[0] = (len -2) >> 8;
4507 			buf[1] = (len -2) & 0xff;
4508 		}
4509 
4510 
4511 		/* Check allocation length */
4512 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
4513 			alc_len = scsipkt->pkt_cdbp[4];
4514 		} else {
4515 			alc_len = scsipkt->pkt_cdbp[7];
4516 			alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
4517 		}
4518 		/*
4519 		 * We do not check for possible parameters truncation
4520 		 * (alc_len < len) assuming that the target driver works
4521 		 * correctly. Just avoiding overrun.
4522 		 * Copy no more than requested and possible, buffer-wise.
4523 		 */
4524 		count = MIN(alc_len, len);
4525 		count = MIN(bp->b_bcount, count);
4526 		bcopy(buf, bp->b_un.b_addr, count);
4527 
4528 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4529 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
4530 	}
4531 	*scsipkt->pkt_scbp = STATUS_GOOD;
4532 done:
4533 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4534 	(void) kmem_free(buf, 1024);
4535 
4536 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4537 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4538 
4539 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4540 	    scsipkt->pkt_comp != NULL)
4541 		/* scsi callback required */
4542 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4543 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4544 		    TQ_SLEEP) == NULL)
4545 			/* Scheduling the callback failed */
4546 			return (TRAN_BUSY);
4547 
4548 	return (TRAN_ACCEPT);
4549 }
4550 
4551 
4552 /*
4553  * SATA translate command: Mode Select.
4554  * Translated into appropriate SATA command or emulated.
4555  * Saving parameters is not supported.
4556  * Changing device capacity is not supported (although theoretically
4557  * possible by executing SET FEATURES/SET MAX ADDRESS)
4558  *
4559  * Assumption is that the target driver is working correctly.
4560  *
4561  * More than one SATA command may be executed to perform operations specified
4562  * by mode select pages. The first error terminates further execution.
4563  * Operations performed successully are not backed-up in such case.
4564  *
4565  * NOTE: Implemented pages:
4566  * - caching page
4567  * - informational exception page
4568  * - acoustic management page
4569  * - power condition page
4570  * Caching setup is remembered so it could be re-stored in case of
4571  * an unexpected device reset.
4572  *
4573  * Returns TRAN_XXXX.
4574  * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields.
4575  */
4576 
4577 static int
4578 sata_txlt_mode_select(sata_pkt_txlate_t *spx)
4579 {
4580 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4581 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4582 	struct scsi_extended_sense *sense;
4583 	int len, pagelen, count, pllen;
4584 	uint8_t *buf;	/* mode select buffer */
4585 	int rval, stat, reason;
4586 	uint_t nointr_flag;
4587 	int dmod = 0;
4588 
4589 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4590 	    "sata_txlt_mode_select, pc %x page code 0x%02x\n",
4591 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4592 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4593 
4594 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4595 
4596 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4597 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4598 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4599 		return (rval);
4600 	}
4601 	/*
4602 	 * If in interrupt context, reject this packet because it may result
4603 	 * in issuing a synchronous command to HBA.
4604 	 */
4605 	if (servicing_interrupt()) {
4606 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
4607 		    "sata_txlt_mode_select: rejecting command because "
4608 		    "of interrupt context\n", NULL);
4609 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4610 		return (TRAN_BUSY);
4611 	}
4612 
4613 	rval = TRAN_ACCEPT;
4614 
4615 	scsipkt->pkt_reason = CMD_CMPLT;
4616 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4617 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4618 	nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
4619 
4620 	/* Reject not supported request */
4621 	if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
4622 		*scsipkt->pkt_scbp = STATUS_CHECK;
4623 		sense = sata_arq_sense(spx);
4624 		sense->es_key = KEY_ILLEGAL_REQUEST;
4625 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4626 		goto done;
4627 	}
4628 
4629 	if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
4630 		pllen = scsipkt->pkt_cdbp[4];
4631 	} else {
4632 		pllen = scsipkt->pkt_cdbp[7];
4633 		pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
4634 	}
4635 
4636 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
4637 
4638 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
4639 		buf = (uint8_t *)bp->b_un.b_addr;
4640 		count = MIN(bp->b_bcount, pllen);
4641 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4642 		scsipkt->pkt_resid = 0;
4643 		pllen = count;
4644 
4645 		/*
4646 		 * Check the header to skip the block descriptor(s) - we
4647 		 * do not support setting device capacity.
4648 		 * Existing macros do not recognize long LBA dscriptor,
4649 		 * hence manual calculation.
4650 		 */
4651 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
4652 			/* 6-bytes CMD, 4 bytes header */
4653 			if (count <= 4)
4654 				goto done;		/* header only */
4655 			len = buf[3] + 4;
4656 		} else {
4657 			/* 10-bytes CMD, 8 bytes header */
4658 			if (count <= 8)
4659 				goto done;		/* header only */
4660 			len = buf[6];
4661 			len = (len << 8) + buf[7] + 8;
4662 		}
4663 		if (len >= count)
4664 			goto done;	/* header + descriptor(s) only */
4665 
4666 		pllen -= len;		/* remaining data length */
4667 
4668 		/*
4669 		 * We may be executing SATA command and want to execute it
4670 		 * in SYNCH mode, regardless of scsi_pkt setting.
4671 		 * Save scsi_pkt setting and indicate SYNCH mode
4672 		 */
4673 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4674 		    scsipkt->pkt_comp != NULL) {
4675 			scsipkt->pkt_flags |= FLAG_NOINTR;
4676 		}
4677 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
4678 
4679 		/*
4680 		 * len is now the offset to a first mode select page
4681 		 * Process all pages
4682 		 */
4683 		while (pllen > 0) {
4684 			switch ((int)buf[len]) {
4685 			case MODEPAGE_CACHING:
4686 				/* No support for SP (saving) */
4687 				if (scsipkt->pkt_cdbp[1] & 0x01) {
4688 					*scsipkt->pkt_scbp = STATUS_CHECK;
4689 					sense = sata_arq_sense(spx);
4690 					sense->es_key = KEY_ILLEGAL_REQUEST;
4691 					sense->es_add_code =
4692 					    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4693 					goto done;
4694 				}
4695 				stat = sata_mode_select_page_8(spx,
4696 				    (struct mode_cache_scsi3 *)&buf[len],
4697 				    pllen, &pagelen, &rval, &dmod);
4698 				/*
4699 				 * The pagelen value indicates the number of
4700 				 * parameter bytes already processed.
4701 				 * The rval is the return value from
4702 				 * sata_tran_start().
4703 				 * The stat indicates the overall status of
4704 				 * the operation(s).
4705 				 */
4706 				if (stat != SATA_SUCCESS)
4707 					/*
4708 					 * Page processing did not succeed -
4709 					 * all error info is already set-up,
4710 					 * just return
4711 					 */
4712 					pllen = 0; /* this breaks the loop */
4713 				else {
4714 					len += pagelen;
4715 					pllen -= pagelen;
4716 				}
4717 				break;
4718 
4719 			case MODEPAGE_INFO_EXCPT:
4720 				stat = sata_mode_select_page_1c(spx,
4721 				    (struct mode_info_excpt_page *)&buf[len],
4722 				    pllen, &pagelen, &rval, &dmod);
4723 				/*
4724 				 * The pagelen value indicates the number of
4725 				 * parameter bytes already processed.
4726 				 * The rval is the return value from
4727 				 * sata_tran_start().
4728 				 * The stat indicates the overall status of
4729 				 * the operation(s).
4730 				 */
4731 				if (stat != SATA_SUCCESS)
4732 					/*
4733 					 * Page processing did not succeed -
4734 					 * all error info is already set-up,
4735 					 * just return
4736 					 */
4737 					pllen = 0; /* this breaks the loop */
4738 				else {
4739 					len += pagelen;
4740 					pllen -= pagelen;
4741 				}
4742 				break;
4743 
4744 			case MODEPAGE_ACOUSTIC_MANAG:
4745 				stat = sata_mode_select_page_30(spx,
4746 				    (struct mode_acoustic_management *)
4747 				    &buf[len], pllen, &pagelen, &rval, &dmod);
4748 				/*
4749 				 * The pagelen value indicates the number of
4750 				 * parameter bytes already processed.
4751 				 * The rval is the return value from
4752 				 * sata_tran_start().
4753 				 * The stat indicates the overall status of
4754 				 * the operation(s).
4755 				 */
4756 				if (stat != SATA_SUCCESS)
4757 					/*
4758 					 * Page processing did not succeed -
4759 					 * all error info is already set-up,
4760 					 * just return
4761 					 */
4762 					pllen = 0; /* this breaks the loop */
4763 				else {
4764 					len += pagelen;
4765 					pllen -= pagelen;
4766 				}
4767 
4768 				break;
4769 			case MODEPAGE_POWER_COND:
4770 				stat = sata_mode_select_page_1a(spx,
4771 				    (struct mode_info_power_cond *)&buf[len],
4772 				    pllen, &pagelen, &rval, &dmod);
4773 				/*
4774 				 * The pagelen value indicates the number of
4775 				 * parameter bytes already processed.
4776 				 * The rval is the return value from
4777 				 * sata_tran_start().
4778 				 * The stat indicates the overall status of
4779 				 * the operation(s).
4780 				 */
4781 				if (stat != SATA_SUCCESS)
4782 					/*
4783 					 * Page processing did not succeed -
4784 					 * all error info is already set-up,
4785 					 * just return
4786 					 */
4787 					pllen = 0; /* this breaks the loop */
4788 				else {
4789 					len += pagelen;
4790 					pllen -= pagelen;
4791 				}
4792 				break;
4793 			default:
4794 				*scsipkt->pkt_scbp = STATUS_CHECK;
4795 				sense = sata_arq_sense(spx);
4796 				sense->es_key = KEY_ILLEGAL_REQUEST;
4797 				sense->es_add_code =
4798 				    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
4799 				goto done;
4800 			}
4801 		}
4802 	}
4803 done:
4804 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4805 	/*
4806 	 * If device parameters were modified, fetch and store the new
4807 	 * Identify Device data. Since port mutex could have been released
4808 	 * for accessing HBA driver, we need to re-check device existence.
4809 	 */
4810 	if (dmod != 0) {
4811 		sata_drive_info_t new_sdinfo, *sdinfo;
4812 		int rv = 0;
4813 
4814 		/*
4815 		 * Following statement has to be changed if this function is
4816 		 * used for devices other than SATA hard disks.
4817 		 */
4818 		new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
4819 
4820 		new_sdinfo.satadrv_addr =
4821 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr;
4822 		rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
4823 		    &new_sdinfo);
4824 
4825 		mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4826 		/*
4827 		 * Since port mutex could have been released when
4828 		 * accessing HBA driver, we need to re-check that the
4829 		 * framework still holds the device info structure.
4830 		 */
4831 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4832 		    &spx->txlt_sata_pkt->satapkt_device);
4833 		if (sdinfo != NULL) {
4834 			/*
4835 			 * Device still has info structure in the
4836 			 * sata framework. Copy newly fetched info
4837 			 */
4838 			if (rv == 0) {
4839 				sdinfo->satadrv_id = new_sdinfo.satadrv_id;
4840 				sata_save_drive_settings(sdinfo);
4841 			} else {
4842 				/*
4843 				 * Could not fetch new data - invalidate
4844 				 * sata_drive_info. That makes device
4845 				 * unusable.
4846 				 */
4847 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
4848 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
4849 			}
4850 		}
4851 		if (rv != 0 || sdinfo == NULL) {
4852 			/*
4853 			 * This changes the overall mode select completion
4854 			 * reason to a failed one !!!!!
4855 			 */
4856 			*scsipkt->pkt_scbp = STATUS_CHECK;
4857 			sense = sata_arq_sense(spx);
4858 			scsipkt->pkt_reason = CMD_INCOMPLETE;
4859 			rval = TRAN_ACCEPT;
4860 		}
4861 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4862 	}
4863 	/* Restore the scsi pkt flags */
4864 	scsipkt->pkt_flags &= ~FLAG_NOINTR;
4865 	scsipkt->pkt_flags |= nointr_flag;
4866 
4867 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4868 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4869 
4870 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4871 	    scsipkt->pkt_comp != NULL)
4872 		/* scsi callback required */
4873 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4874 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4875 		    TQ_SLEEP) == NULL)
4876 			/* Scheduling the callback failed */
4877 			return (TRAN_BUSY);
4878 
4879 	return (rval);
4880 }
4881 
4882 
4883 
4884 /*
4885  * Translate command: Log Sense
4886  */
4887 static 	int
4888 sata_txlt_log_sense(sata_pkt_txlate_t *spx)
4889 {
4890 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4891 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4892 	sata_drive_info_t *sdinfo;
4893 	struct scsi_extended_sense *sense;
4894 	int 		len, count, alc_len;
4895 	int		pc;	/* Page Control code */
4896 	int		page_code;	/* Page code */
4897 	uint8_t		*buf;	/* log sense buffer */
4898 	int		rval, reason;
4899 #define	MAX_LOG_SENSE_PAGE_SIZE	512
4900 
4901 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4902 	    "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
4903 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4904 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4905 
4906 	buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
4907 
4908 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4909 
4910 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4911 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4912 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4913 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4914 		return (rval);
4915 	}
4916 	/*
4917 	 * If in interrupt context, reject this packet because it may result
4918 	 * in issuing a synchronous command to HBA.
4919 	 */
4920 	if (servicing_interrupt()) {
4921 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
4922 		    "sata_log_sense: rejecting command because "
4923 		    "of interrupt context\n", NULL);
4924 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4925 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4926 		return (TRAN_BUSY);
4927 	}
4928 
4929 	scsipkt->pkt_reason = CMD_CMPLT;
4930 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4931 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4932 
4933 	pc = scsipkt->pkt_cdbp[2] >> 6;
4934 	page_code = scsipkt->pkt_cdbp[2] & 0x3f;
4935 
4936 	/* Reject not supported request for all but cumulative values */
4937 	switch (pc) {
4938 	case PC_CUMULATIVE_VALUES:
4939 		break;
4940 	default:
4941 		*scsipkt->pkt_scbp = STATUS_CHECK;
4942 		sense = sata_arq_sense(spx);
4943 		sense->es_key = KEY_ILLEGAL_REQUEST;
4944 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4945 		goto done;
4946 	}
4947 
4948 	switch (page_code) {
4949 	case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
4950 	case PAGE_CODE_SELF_TEST_RESULTS:
4951 	case PAGE_CODE_INFORMATION_EXCEPTIONS:
4952 	case PAGE_CODE_SMART_READ_DATA:
4953 	case PAGE_CODE_START_STOP_CYCLE_COUNTER:
4954 		break;
4955 	default:
4956 		*scsipkt->pkt_scbp = STATUS_CHECK;
4957 		sense = sata_arq_sense(spx);
4958 		sense->es_key = KEY_ILLEGAL_REQUEST;
4959 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4960 		goto done;
4961 	}
4962 
4963 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4964 		/*
4965 		 * Because log sense uses local buffers for data retrieval from
4966 		 * the devices and sets the data programatically in the
4967 		 * original specified buffer, release preallocated DMA
4968 		 * resources before storing data in the original buffer,
4969 		 * so no unwanted DMA sync would take place.
4970 		 */
4971 		sata_id_t *sata_id;
4972 
4973 		sata_scsi_dmafree(NULL, scsipkt);
4974 
4975 		len = 0;
4976 
4977 		/* Build log parameter header */
4978 		buf[len++] = page_code;	/* page code as in the CDB */
4979 		buf[len++] = 0;		/* reserved */
4980 		buf[len++] = 0;		/* Zero out page length for now (MSB) */
4981 		buf[len++] = 0;		/* (LSB) */
4982 
4983 		sdinfo = sata_get_device_info(
4984 		    spx->txlt_sata_hba_inst,
4985 		    &spx->txlt_sata_pkt->satapkt_device);
4986 
4987 		/*
4988 		 * Add requested pages.
4989 		 */
4990 		switch (page_code) {
4991 		case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
4992 			len = sata_build_lsense_page_0(sdinfo, buf + len);
4993 			break;
4994 		case PAGE_CODE_SELF_TEST_RESULTS:
4995 			sata_id = &sdinfo->satadrv_id;
4996 			if ((! (sata_id->ai_cmdset84 &
4997 			    SATA_SMART_SELF_TEST_SUPPORTED)) ||
4998 			    (! (sata_id->ai_features87 &
4999 			    SATA_SMART_SELF_TEST_SUPPORTED))) {
5000 				*scsipkt->pkt_scbp = STATUS_CHECK;
5001 				sense = sata_arq_sense(spx);
5002 				sense->es_key = KEY_ILLEGAL_REQUEST;
5003 				sense->es_add_code =
5004 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5005 
5006 				goto done;
5007 			}
5008 			len = sata_build_lsense_page_10(sdinfo, buf + len,
5009 			    spx->txlt_sata_hba_inst);
5010 			break;
5011 		case PAGE_CODE_INFORMATION_EXCEPTIONS:
5012 			sata_id = &sdinfo->satadrv_id;
5013 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5014 				*scsipkt->pkt_scbp = STATUS_CHECK;
5015 				sense = sata_arq_sense(spx);
5016 				sense->es_key = KEY_ILLEGAL_REQUEST;
5017 				sense->es_add_code =
5018 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5019 
5020 				goto done;
5021 			}
5022 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5023 				*scsipkt->pkt_scbp = STATUS_CHECK;
5024 				sense = sata_arq_sense(spx);
5025 				sense->es_key = KEY_ABORTED_COMMAND;
5026 				sense->es_add_code =
5027 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5028 				sense->es_qual_code =
5029 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5030 
5031 				goto done;
5032 			}
5033 
5034 			len = sata_build_lsense_page_2f(sdinfo, buf + len,
5035 			    spx->txlt_sata_hba_inst);
5036 			break;
5037 		case PAGE_CODE_SMART_READ_DATA:
5038 			sata_id = &sdinfo->satadrv_id;
5039 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5040 				*scsipkt->pkt_scbp = STATUS_CHECK;
5041 				sense = sata_arq_sense(spx);
5042 				sense->es_key = KEY_ILLEGAL_REQUEST;
5043 				sense->es_add_code =
5044 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5045 
5046 				goto done;
5047 			}
5048 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5049 				*scsipkt->pkt_scbp = STATUS_CHECK;
5050 				sense = sata_arq_sense(spx);
5051 				sense->es_key = KEY_ABORTED_COMMAND;
5052 				sense->es_add_code =
5053 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5054 				sense->es_qual_code =
5055 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5056 
5057 				goto done;
5058 			}
5059 
5060 			/* This page doesn't include a page header */
5061 			len = sata_build_lsense_page_30(sdinfo, buf,
5062 			    spx->txlt_sata_hba_inst);
5063 			goto no_header;
5064 		case PAGE_CODE_START_STOP_CYCLE_COUNTER:
5065 			sata_id = &sdinfo->satadrv_id;
5066 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5067 				*scsipkt->pkt_scbp = STATUS_CHECK;
5068 				sense = sata_arq_sense(spx);
5069 				sense->es_key = KEY_ILLEGAL_REQUEST;
5070 				sense->es_add_code =
5071 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5072 
5073 				goto done;
5074 			}
5075 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5076 				*scsipkt->pkt_scbp = STATUS_CHECK;
5077 				sense = sata_arq_sense(spx);
5078 				sense->es_key = KEY_ABORTED_COMMAND;
5079 				sense->es_add_code =
5080 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5081 				sense->es_qual_code =
5082 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5083 
5084 				goto done;
5085 			}
5086 			len = sata_build_lsense_page_0e(sdinfo, buf, spx);
5087 			goto no_header;
5088 		default:
5089 			/* Invalid request */
5090 			*scsipkt->pkt_scbp = STATUS_CHECK;
5091 			sense = sata_arq_sense(spx);
5092 			sense->es_key = KEY_ILLEGAL_REQUEST;
5093 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5094 			goto done;
5095 		}
5096 
5097 		/* set parameter log sense data length */
5098 		buf[2] = len >> 8;	/* log sense length (MSB) */
5099 		buf[3] = len & 0xff;	/* log sense length (LSB) */
5100 
5101 		len += SCSI_LOG_PAGE_HDR_LEN;
5102 		ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
5103 
5104 no_header:
5105 		/* Check allocation length */
5106 		alc_len = scsipkt->pkt_cdbp[7];
5107 		alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
5108 
5109 		/*
5110 		 * We do not check for possible parameters truncation
5111 		 * (alc_len < len) assuming that the target driver works
5112 		 * correctly. Just avoiding overrun.
5113 		 * Copy no more than requested and possible, buffer-wise.
5114 		 */
5115 		count = MIN(alc_len, len);
5116 		count = MIN(bp->b_bcount, count);
5117 		bcopy(buf, bp->b_un.b_addr, count);
5118 
5119 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
5120 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
5121 	}
5122 	*scsipkt->pkt_scbp = STATUS_GOOD;
5123 done:
5124 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5125 	(void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
5126 
5127 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5128 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5129 
5130 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5131 	    scsipkt->pkt_comp != NULL)
5132 		/* scsi callback required */
5133 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5134 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5135 		    TQ_SLEEP) == NULL)
5136 			/* Scheduling the callback failed */
5137 			return (TRAN_BUSY);
5138 
5139 	return (TRAN_ACCEPT);
5140 }
5141 
5142 /*
5143  * Translate command: Log Select
5144  * Not implemented at this time - returns invalid command response.
5145  */
5146 static	int
5147 sata_txlt_log_select(sata_pkt_txlate_t *spx)
5148 {
5149 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5150 	    "sata_txlt_log_select\n", NULL);
5151 
5152 	return (sata_txlt_invalid_command(spx));
5153 }
5154 
5155 
5156 /*
5157  * Translate command: Read (various types).
5158  * Translated into appropriate type of ATA READ command
5159  * for SATA hard disks.
5160  * Both the device capabilities and requested operation mode are
5161  * considered.
5162  *
5163  * Following scsi cdb fields are ignored:
5164  * rdprotect, dpo, fua, fua_nv, group_number.
5165  *
5166  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
5167  * enable variable sata_func_enable), the capability of the controller and
5168  * capability of a device are checked and if both support queueing, read
5169  * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
5170  * command rather than plain READ_XXX command.
5171  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
5172  * both the controller and device suport such functionality, the read
5173  * request will be translated to READ_FPDMA_QUEUED command.
5174  * In both cases the maximum queue depth is derived as minimum of:
5175  * HBA capability,device capability and sata_max_queue_depth variable setting.
5176  * The value passed to HBA driver is decremented by 1, because only 5 bits are
5177  * used to pass max queue depth value, and the maximum possible queue depth
5178  * is 32.
5179  *
5180  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
5181  * appropriate values in scsi_pkt fields.
5182  */
5183 static int
5184 sata_txlt_read(sata_pkt_txlate_t *spx)
5185 {
5186 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5187 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5188 	sata_drive_info_t *sdinfo;
5189 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
5190 	int cport = SATA_TXLT_CPORT(spx);
5191 	uint16_t sec_count;
5192 	uint64_t lba;
5193 	int rval, reason;
5194 	int synch;
5195 
5196 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5197 
5198 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
5199 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5200 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5201 		return (rval);
5202 	}
5203 
5204 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
5205 	    &spx->txlt_sata_pkt->satapkt_device);
5206 
5207 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
5208 	/*
5209 	 * Extract LBA and sector count from scsi CDB.
5210 	 */
5211 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
5212 	case SCMD_READ:
5213 		/* 6-byte scsi read cmd : 0x08 */
5214 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
5215 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
5216 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5217 		sec_count = scsipkt->pkt_cdbp[4];
5218 		/* sec_count 0 will be interpreted as 256 by a device */
5219 		break;
5220 	case SCMD_READ_G1:
5221 		/* 10-bytes scsi read command : 0x28 */
5222 		lba = scsipkt->pkt_cdbp[2];
5223 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5224 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5225 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5226 		sec_count = scsipkt->pkt_cdbp[7];
5227 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
5228 		break;
5229 	case SCMD_READ_G5:
5230 		/* 12-bytes scsi read command : 0xA8 */
5231 		lba = scsipkt->pkt_cdbp[2];
5232 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5233 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5234 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5235 		sec_count = scsipkt->pkt_cdbp[6];
5236 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
5237 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
5238 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
5239 		break;
5240 	case SCMD_READ_G4:
5241 		/* 16-bytes scsi read command : 0x88 */
5242 		lba = scsipkt->pkt_cdbp[2];
5243 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5244 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5245 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5246 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
5247 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5248 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5249 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
5250 		sec_count = scsipkt->pkt_cdbp[10];
5251 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
5252 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
5253 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
5254 		break;
5255 	default:
5256 		/* Unsupported command */
5257 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5258 		return (sata_txlt_invalid_command(spx));
5259 	}
5260 
5261 	/*
5262 	 * Check if specified address exceeds device capacity
5263 	 */
5264 	if ((lba >= sdinfo->satadrv_capacity) ||
5265 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
5266 		/* LBA out of range */
5267 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5268 		return (sata_txlt_lba_out_of_range(spx));
5269 	}
5270 
5271 	/*
5272 	 * For zero-length transfer, emulate good completion of the command
5273 	 * (reasons for rejecting the command were already checked).
5274 	 * No DMA resources were allocated.
5275 	 */
5276 	if (spx->txlt_dma_cookie_list == NULL) {
5277 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5278 		return (sata_emul_rw_completion(spx));
5279 	}
5280 
5281 	/*
5282 	 * Build cmd block depending on the device capability and
5283 	 * requested operation mode.
5284 	 * Do not bother with non-dma mode - we are working only with
5285 	 * devices supporting DMA.
5286 	 */
5287 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
5288 	scmd->satacmd_device_reg = SATA_ADH_LBA;
5289 	scmd->satacmd_cmd_reg = SATAC_READ_DMA;
5290 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
5291 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
5292 		scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT;
5293 		scmd->satacmd_sec_count_msb = sec_count >> 8;
5294 #ifndef __lock_lint
5295 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
5296 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
5297 		scmd->satacmd_lba_high_msb = lba >> 40;
5298 #endif
5299 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
5300 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
5301 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
5302 	}
5303 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
5304 	scmd->satacmd_lba_low_lsb = lba & 0xff;
5305 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
5306 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
5307 	scmd->satacmd_features_reg = 0;
5308 	scmd->satacmd_status_reg = 0;
5309 	scmd->satacmd_error_reg = 0;
5310 
5311 	/*
5312 	 * Check if queueing commands should be used and switch
5313 	 * to appropriate command if possible
5314 	 */
5315 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
5316 		boolean_t using_queuing;
5317 
5318 		/* Queuing supported by controller and device? */
5319 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
5320 		    (sdinfo->satadrv_features_support &
5321 		    SATA_DEV_F_NCQ) &&
5322 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
5323 		    SATA_CTLF_NCQ)) {
5324 			using_queuing = B_TRUE;
5325 
5326 			/* NCQ supported - use FPDMA READ */
5327 			scmd->satacmd_cmd_reg =
5328 			    SATAC_READ_FPDMA_QUEUED;
5329 			scmd->satacmd_features_reg_ext =
5330 			    scmd->satacmd_sec_count_msb;
5331 			scmd->satacmd_sec_count_msb = 0;
5332 		} else if ((sdinfo->satadrv_features_support &
5333 		    SATA_DEV_F_TCQ) &&
5334 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
5335 		    SATA_CTLF_QCMD)) {
5336 			using_queuing = B_TRUE;
5337 
5338 			/* Legacy queueing */
5339 			if (sdinfo->satadrv_features_support &
5340 			    SATA_DEV_F_LBA48) {
5341 				scmd->satacmd_cmd_reg =
5342 				    SATAC_READ_DMA_QUEUED_EXT;
5343 				scmd->satacmd_features_reg_ext =
5344 				    scmd->satacmd_sec_count_msb;
5345 				scmd->satacmd_sec_count_msb = 0;
5346 			} else {
5347 				scmd->satacmd_cmd_reg =
5348 				    SATAC_READ_DMA_QUEUED;
5349 			}
5350 		} else	/* NCQ nor legacy queuing not supported */
5351 			using_queuing = B_FALSE;
5352 
5353 		/*
5354 		 * If queuing, the sector count goes in the features register
5355 		 * and the secount count will contain the tag.
5356 		 */
5357 		if (using_queuing) {
5358 			scmd->satacmd_features_reg =
5359 			    scmd->satacmd_sec_count_lsb;
5360 			scmd->satacmd_sec_count_lsb = 0;
5361 			scmd->satacmd_flags.sata_queued = B_TRUE;
5362 
5363 			/* Set-up maximum queue depth */
5364 			scmd->satacmd_flags.sata_max_queue_depth =
5365 			    sdinfo->satadrv_max_queue_depth - 1;
5366 		} else if (sdinfo->satadrv_features_enabled &
5367 		    SATA_DEV_F_E_UNTAGGED_QING) {
5368 			/*
5369 			 * Although NCQ/TCQ is not enabled, untagged queuing
5370 			 * may be still used.
5371 			 * Set-up the maximum untagged queue depth.
5372 			 * Use controller's queue depth from sata_hba_tran.
5373 			 * SATA HBA drivers may ignore this value and rely on
5374 			 * the internal limits.For drivers that do not
5375 			 * ignore untaged queue depth, limit the value to
5376 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
5377 			 * largest value that can be passed via
5378 			 * satacmd_flags.sata_max_queue_depth.
5379 			 */
5380 			scmd->satacmd_flags.sata_max_queue_depth =
5381 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
5382 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
5383 
5384 		} else {
5385 			scmd->satacmd_flags.sata_max_queue_depth = 0;
5386 		}
5387 	} else
5388 		scmd->satacmd_flags.sata_max_queue_depth = 0;
5389 
5390 	SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst,
5391 	    "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n",
5392 	    scmd->satacmd_cmd_reg, lba, sec_count);
5393 
5394 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5395 		/* Need callback function */
5396 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
5397 		synch = FALSE;
5398 	} else
5399 		synch = TRUE;
5400 
5401 	/* Transfer command to HBA */
5402 	if (sata_hba_start(spx, &rval) != 0) {
5403 		/* Pkt not accepted for execution */
5404 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5405 		return (rval);
5406 	}
5407 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5408 	/*
5409 	 * If execution is non-synchronous,
5410 	 * a callback function will handle potential errors, translate
5411 	 * the response and will do a callback to a target driver.
5412 	 * If it was synchronous, check execution status using the same
5413 	 * framework callback.
5414 	 */
5415 	if (synch) {
5416 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5417 		    "synchronous execution status %x\n",
5418 		    spx->txlt_sata_pkt->satapkt_reason);
5419 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
5420 	}
5421 	return (TRAN_ACCEPT);
5422 }
5423 
5424 
5425 /*
5426  * SATA translate command: Write (various types)
5427  * Translated into appropriate type of ATA WRITE command
5428  * for SATA hard disks.
5429  * Both the device capabilities and requested operation mode are
5430  * considered.
5431  *
5432  * Following scsi cdb fields are ignored:
5433  * rwprotect, dpo, fua, fua_nv, group_number.
5434  *
5435  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
5436  * enable variable sata_func_enable), the capability of the controller and
5437  * capability of a device are checked and if both support queueing, write
5438  * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT
5439  * command rather than plain WRITE_XXX command.
5440  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
5441  * both the controller and device suport such functionality, the write
5442  * request will be translated to WRITE_FPDMA_QUEUED command.
5443  * In both cases the maximum queue depth is derived as minimum of:
5444  * HBA capability,device capability and sata_max_queue_depth variable setting.
5445  * The value passed to HBA driver is decremented by 1, because only 5 bits are
5446  * used to pass max queue depth value, and the maximum possible queue depth
5447  * is 32.
5448  *
5449  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
5450  * appropriate values in scsi_pkt fields.
5451  */
5452 static int
5453 sata_txlt_write(sata_pkt_txlate_t *spx)
5454 {
5455 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5456 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5457 	sata_drive_info_t *sdinfo;
5458 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
5459 	int cport = SATA_TXLT_CPORT(spx);
5460 	uint16_t sec_count;
5461 	uint64_t lba;
5462 	int rval, reason;
5463 	int synch;
5464 
5465 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5466 
5467 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
5468 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5469 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5470 		return (rval);
5471 	}
5472 
5473 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
5474 	    &spx->txlt_sata_pkt->satapkt_device);
5475 
5476 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
5477 	/*
5478 	 * Extract LBA and sector count from scsi CDB
5479 	 */
5480 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
5481 	case SCMD_WRITE:
5482 		/* 6-byte scsi read cmd : 0x0A */
5483 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
5484 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
5485 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5486 		sec_count = scsipkt->pkt_cdbp[4];
5487 		/* sec_count 0 will be interpreted as 256 by a device */
5488 		break;
5489 	case SCMD_WRITE_G1:
5490 		/* 10-bytes scsi write command : 0x2A */
5491 		lba = scsipkt->pkt_cdbp[2];
5492 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5493 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5494 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5495 		sec_count = scsipkt->pkt_cdbp[7];
5496 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
5497 		break;
5498 	case SCMD_WRITE_G5:
5499 		/* 12-bytes scsi read command : 0xAA */
5500 		lba = scsipkt->pkt_cdbp[2];
5501 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5502 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5503 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5504 		sec_count = scsipkt->pkt_cdbp[6];
5505 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
5506 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
5507 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
5508 		break;
5509 	case SCMD_WRITE_G4:
5510 		/* 16-bytes scsi write command : 0x8A */
5511 		lba = scsipkt->pkt_cdbp[2];
5512 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5513 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5514 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5515 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
5516 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5517 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5518 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
5519 		sec_count = scsipkt->pkt_cdbp[10];
5520 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
5521 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
5522 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
5523 		break;
5524 	default:
5525 		/* Unsupported command */
5526 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5527 		return (sata_txlt_invalid_command(spx));
5528 	}
5529 
5530 	/*
5531 	 * Check if specified address and length exceeds device capacity
5532 	 */
5533 	if ((lba >= sdinfo->satadrv_capacity) ||
5534 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
5535 		/* LBA out of range */
5536 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5537 		return (sata_txlt_lba_out_of_range(spx));
5538 	}
5539 
5540 	/*
5541 	 * For zero-length transfer, emulate good completion of the command
5542 	 * (reasons for rejecting the command were already checked).
5543 	 * No DMA resources were allocated.
5544 	 */
5545 	if (spx->txlt_dma_cookie_list == NULL) {
5546 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5547 		return (sata_emul_rw_completion(spx));
5548 	}
5549 
5550 	/*
5551 	 * Build cmd block depending on the device capability and
5552 	 * requested operation mode.
5553 	 * Do not bother with non-dma mode- we are working only with
5554 	 * devices supporting DMA.
5555 	 */
5556 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
5557 	scmd->satacmd_device_reg = SATA_ADH_LBA;
5558 	scmd->satacmd_cmd_reg = SATAC_WRITE_DMA;
5559 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
5560 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
5561 		scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT;
5562 		scmd->satacmd_sec_count_msb = sec_count >> 8;
5563 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
5564 #ifndef __lock_lint
5565 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
5566 		scmd->satacmd_lba_high_msb = lba >> 40;
5567 #endif
5568 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
5569 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
5570 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
5571 	}
5572 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
5573 	scmd->satacmd_lba_low_lsb = lba & 0xff;
5574 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
5575 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
5576 	scmd->satacmd_features_reg = 0;
5577 	scmd->satacmd_status_reg = 0;
5578 	scmd->satacmd_error_reg = 0;
5579 
5580 	/*
5581 	 * Check if queueing commands should be used and switch
5582 	 * to appropriate command if possible
5583 	 */
5584 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
5585 		boolean_t using_queuing;
5586 
5587 		/* Queuing supported by controller and device? */
5588 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
5589 		    (sdinfo->satadrv_features_support &
5590 		    SATA_DEV_F_NCQ) &&
5591 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
5592 		    SATA_CTLF_NCQ)) {
5593 			using_queuing = B_TRUE;
5594 
5595 			/* NCQ supported - use FPDMA WRITE */
5596 			scmd->satacmd_cmd_reg =
5597 			    SATAC_WRITE_FPDMA_QUEUED;
5598 			scmd->satacmd_features_reg_ext =
5599 			    scmd->satacmd_sec_count_msb;
5600 			scmd->satacmd_sec_count_msb = 0;
5601 		} else if ((sdinfo->satadrv_features_support &
5602 		    SATA_DEV_F_TCQ) &&
5603 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
5604 		    SATA_CTLF_QCMD)) {
5605 			using_queuing = B_TRUE;
5606 
5607 			/* Legacy queueing */
5608 			if (sdinfo->satadrv_features_support &
5609 			    SATA_DEV_F_LBA48) {
5610 				scmd->satacmd_cmd_reg =
5611 				    SATAC_WRITE_DMA_QUEUED_EXT;
5612 				scmd->satacmd_features_reg_ext =
5613 				    scmd->satacmd_sec_count_msb;
5614 				scmd->satacmd_sec_count_msb = 0;
5615 			} else {
5616 				scmd->satacmd_cmd_reg =
5617 				    SATAC_WRITE_DMA_QUEUED;
5618 			}
5619 		} else	/*  NCQ nor legacy queuing not supported */
5620 			using_queuing = B_FALSE;
5621 
5622 		if (using_queuing) {
5623 			scmd->satacmd_features_reg =
5624 			    scmd->satacmd_sec_count_lsb;
5625 			scmd->satacmd_sec_count_lsb = 0;
5626 			scmd->satacmd_flags.sata_queued = B_TRUE;
5627 			/* Set-up maximum queue depth */
5628 			scmd->satacmd_flags.sata_max_queue_depth =
5629 			    sdinfo->satadrv_max_queue_depth - 1;
5630 		} else if (sdinfo->satadrv_features_enabled &
5631 		    SATA_DEV_F_E_UNTAGGED_QING) {
5632 			/*
5633 			 * Although NCQ/TCQ is not enabled, untagged queuing
5634 			 * may be still used.
5635 			 * Set-up the maximum untagged queue depth.
5636 			 * Use controller's queue depth from sata_hba_tran.
5637 			 * SATA HBA drivers may ignore this value and rely on
5638 			 * the internal limits. For drivera that do not
5639 			 * ignore untaged queue depth, limit the value to
5640 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
5641 			 * largest value that can be passed via
5642 			 * satacmd_flags.sata_max_queue_depth.
5643 			 */
5644 			scmd->satacmd_flags.sata_max_queue_depth =
5645 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
5646 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
5647 
5648 		} else {
5649 			scmd->satacmd_flags.sata_max_queue_depth = 0;
5650 		}
5651 	} else
5652 		scmd->satacmd_flags.sata_max_queue_depth = 0;
5653 
5654 	SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5655 	    "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n",
5656 	    scmd->satacmd_cmd_reg, lba, sec_count);
5657 
5658 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5659 		/* Need callback function */
5660 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
5661 		synch = FALSE;
5662 	} else
5663 		synch = TRUE;
5664 
5665 	/* Transfer command to HBA */
5666 	if (sata_hba_start(spx, &rval) != 0) {
5667 		/* Pkt not accepted for execution */
5668 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5669 		return (rval);
5670 	}
5671 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5672 
5673 	/*
5674 	 * If execution is non-synchronous,
5675 	 * a callback function will handle potential errors, translate
5676 	 * the response and will do a callback to a target driver.
5677 	 * If it was synchronous, check execution status using the same
5678 	 * framework callback.
5679 	 */
5680 	if (synch) {
5681 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5682 		    "synchronous execution status %x\n",
5683 		    spx->txlt_sata_pkt->satapkt_reason);
5684 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
5685 	}
5686 	return (TRAN_ACCEPT);
5687 }
5688 
5689 
5690 /*
5691  * Implements SCSI SBC WRITE BUFFER command download microcode option
5692  */
5693 static int
5694 sata_txlt_write_buffer(sata_pkt_txlate_t *spx)
5695 {
5696 #define	WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE			4
5697 #define	WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE			5
5698 
5699 	sata_hba_inst_t *sata_hba_inst = SATA_TXLT_HBA_INST(spx);
5700 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5701 	struct sata_pkt *sata_pkt = spx->txlt_sata_pkt;
5702 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5703 
5704 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5705 	struct scsi_extended_sense *sense;
5706 	int rval, mode, sector_count, reason;
5707 	int cport = SATA_TXLT_CPORT(spx);
5708 
5709 	mode = scsipkt->pkt_cdbp[1] & 0x1f;
5710 
5711 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5712 	    "sata_txlt_write_buffer, mode 0x%x\n", mode);
5713 
5714 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5715 
5716 	if ((rval = sata_txlt_generic_pkt_info(spx, &reason)) != TRAN_ACCEPT) {
5717 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5718 		return (rval);
5719 	}
5720 	/*
5721 	 * If in interrupt context, reject this packet because it would issue
5722 	 * a synchronous command to HBA.
5723 	 */
5724 	if (servicing_interrupt()) {
5725 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
5726 		    "sata_txlt_write_buffer: rejecting command because "
5727 		    "of interrupt context\n", NULL);
5728 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5729 		return (TRAN_BUSY);
5730 	}
5731 
5732 	/* Use synchronous mode */
5733 	spx->txlt_sata_pkt->satapkt_op_mode
5734 	    |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
5735 
5736 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
5737 
5738 	scsipkt->pkt_reason = CMD_CMPLT;
5739 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5740 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5741 
5742 	/*
5743 	 * The SCSI to ATA translation specification only calls
5744 	 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE.
5745 	 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but
5746 	 * ATA 8 (draft) got rid of download microcode for temp
5747 	 * and it is even optional for ATA 7, so it may be aborted.
5748 	 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as
5749 	 * it is not specified and the buffer offset for SCSI is a 16-bit
5750 	 * value in bytes, but for ATA it is a 16-bit offset in 512 byte
5751 	 * sectors.  Thus the offset really doesn't buy us anything.
5752 	 * If and when ATA 8 is stabilized and the SCSI to ATA specification
5753 	 * is revised, this can be revisisted.
5754 	 */
5755 	/* Reject not supported request */
5756 	switch (mode) {
5757 	case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE:
5758 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP;
5759 		break;
5760 	case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE:
5761 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE;
5762 		break;
5763 	default:
5764 		goto bad_param;
5765 	}
5766 
5767 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
5768 
5769 	scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE;
5770 	if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0)
5771 		goto bad_param;
5772 	sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE;
5773 	scmd->satacmd_sec_count_lsb = (uint8_t)sector_count;
5774 	scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8;
5775 	scmd->satacmd_lba_mid_lsb = 0;
5776 	scmd->satacmd_lba_high_lsb = 0;
5777 	scmd->satacmd_device_reg = 0;
5778 	spx->txlt_sata_pkt->satapkt_comp = NULL;
5779 	scmd->satacmd_addr_type = 0;
5780 
5781 	/* Transfer command to HBA */
5782 	if (sata_hba_start(spx, &rval) != 0) {
5783 		/* Pkt not accepted for execution */
5784 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
5785 		return (rval);
5786 	}
5787 
5788 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
5789 
5790 	/* Then we need synchronous check the status of the disk */
5791 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5792 	    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
5793 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5794 		scsipkt->pkt_reason = CMD_CMPLT;
5795 
5796 		/* Download commmand succeed, so probe and identify device */
5797 		sata_reidentify_device(spx);
5798 	} else {
5799 		/* Something went wrong, microcode download command failed */
5800 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5801 		*scsipkt->pkt_scbp = STATUS_CHECK;
5802 		sense = sata_arq_sense(spx);
5803 		switch (sata_pkt->satapkt_reason) {
5804 		case SATA_PKT_PORT_ERROR:
5805 			/*
5806 			 * We have no device data. Assume no data transfered.
5807 			 */
5808 			sense->es_key = KEY_HARDWARE_ERROR;
5809 			break;
5810 
5811 		case SATA_PKT_DEV_ERROR:
5812 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5813 			    SATA_STATUS_ERR) {
5814 				/*
5815 				 * determine dev error reason from error
5816 				 * reg content
5817 				 */
5818 				sata_decode_device_error(spx, sense);
5819 				break;
5820 			}
5821 			/* No extended sense key - no info available */
5822 			break;
5823 
5824 		case SATA_PKT_TIMEOUT:
5825 			scsipkt->pkt_reason = CMD_TIMEOUT;
5826 			scsipkt->pkt_statistics |=
5827 			    STAT_TIMEOUT | STAT_DEV_RESET;
5828 			/* No extended sense key ? */
5829 			break;
5830 
5831 		case SATA_PKT_ABORTED:
5832 			scsipkt->pkt_reason = CMD_ABORTED;
5833 			scsipkt->pkt_statistics |= STAT_ABORTED;
5834 			/* No extended sense key ? */
5835 			break;
5836 
5837 		case SATA_PKT_RESET:
5838 			/* pkt aborted by an explicit reset from a host */
5839 			scsipkt->pkt_reason = CMD_RESET;
5840 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5841 			break;
5842 
5843 		default:
5844 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5845 			    "sata_txlt_nodata_cmd_completion: "
5846 			    "invalid packet completion reason %d",
5847 			    sata_pkt->satapkt_reason));
5848 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5849 			break;
5850 		}
5851 
5852 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5853 		    "scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5854 
5855 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
5856 			/* scsi callback required */
5857 			scsi_hba_pkt_comp(scsipkt);
5858 	}
5859 	return (TRAN_ACCEPT);
5860 
5861 bad_param:
5862 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5863 	*scsipkt->pkt_scbp = STATUS_CHECK;
5864 	sense = sata_arq_sense(spx);
5865 	sense->es_key = KEY_ILLEGAL_REQUEST;
5866 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5867 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5868 	    scsipkt->pkt_comp != NULL) {
5869 		/* scsi callback required */
5870 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5871 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5872 		    TQ_SLEEP) == 0) {
5873 			/* Scheduling the callback failed */
5874 			rval = TRAN_BUSY;
5875 		}
5876 	}
5877 	return (rval);
5878 }
5879 
5880 /*
5881  * Re-identify device after doing a firmware download.
5882  */
5883 static void
5884 sata_reidentify_device(sata_pkt_txlate_t *spx)
5885 {
5886 #define	DOWNLOAD_WAIT_TIME_SECS	60
5887 #define	DOWNLOAD_WAIT_INTERVAL_SECS	1
5888 	int rval;
5889 	int retry_cnt;
5890 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5891 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
5892 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
5893 	sata_drive_info_t *sdinfo;
5894 
5895 	/*
5896 	 * Before returning good status, probe device.
5897 	 * Device probing will get IDENTIFY DEVICE data, if possible.
5898 	 * The assumption is that the new microcode is applied by the
5899 	 * device. It is a caller responsibility to verify this.
5900 	 */
5901 	for (retry_cnt = 0;
5902 	    retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS;
5903 	    retry_cnt++) {
5904 		rval = sata_probe_device(sata_hba_inst, &sata_device);
5905 
5906 		if (rval == SATA_SUCCESS) { /* Set default features */
5907 			sdinfo = sata_get_device_info(sata_hba_inst,
5908 			    &sata_device);
5909 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
5910 			    SATA_SUCCESS) {
5911 				/* retry */
5912 				rval = sata_initialize_device(sata_hba_inst,
5913 				    sdinfo);
5914 				if (rval == SATA_RETRY)
5915 					sata_log(sata_hba_inst, CE_WARN,
5916 					    "SATA device at port %d pmport %d -"
5917 					    " default device features could not"
5918 					    " be set. Device may not operate "
5919 					    "as expected.",
5920 					    sata_device.satadev_addr.cport,
5921 					    sata_device.satadev_addr.pmport);
5922 			}
5923 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
5924 				scsi_hba_pkt_comp(scsipkt);
5925 			return;
5926 		} else if (rval == SATA_RETRY) {
5927 			delay(drv_usectohz(1000000 *
5928 			    DOWNLOAD_WAIT_INTERVAL_SECS));
5929 			continue;
5930 		} else	/* failed - no reason to retry */
5931 			break;
5932 	}
5933 
5934 	/*
5935 	 * Something went wrong, device probing failed.
5936 	 */
5937 	SATA_LOG_D((sata_hba_inst, CE_WARN,
5938 	    "Cannot probe device after downloading microcode\n"));
5939 
5940 	/* Reset device to force retrying the probe. */
5941 	(void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst))
5942 	    (SATA_DIP(sata_hba_inst), &sata_device);
5943 
5944 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
5945 		scsi_hba_pkt_comp(scsipkt);
5946 }
5947 
5948 
5949 /*
5950  * Translate command: Synchronize Cache.
5951  * Translates into Flush Cache command for SATA hard disks.
5952  *
5953  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
5954  * appropriate values in scsi_pkt fields.
5955  */
5956 static 	int
5957 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx)
5958 {
5959 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5960 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
5961 	int cport = SATA_TXLT_CPORT(spx);
5962 	int rval, reason;
5963 	int synch;
5964 
5965 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5966 
5967 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
5968 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5969 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5970 		return (rval);
5971 	}
5972 
5973 	scmd->satacmd_addr_type = 0;
5974 	scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE;
5975 	scmd->satacmd_device_reg = 0;
5976 	scmd->satacmd_sec_count_lsb = 0;
5977 	scmd->satacmd_lba_low_lsb = 0;
5978 	scmd->satacmd_lba_mid_lsb = 0;
5979 	scmd->satacmd_lba_high_lsb = 0;
5980 	scmd->satacmd_features_reg = 0;
5981 	scmd->satacmd_status_reg = 0;
5982 	scmd->satacmd_error_reg = 0;
5983 
5984 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5985 	    "sata_txlt_synchronize_cache\n", NULL);
5986 
5987 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5988 		/* Need to set-up a callback function */
5989 		spx->txlt_sata_pkt->satapkt_comp =
5990 		    sata_txlt_nodata_cmd_completion;
5991 		synch = FALSE;
5992 	} else
5993 		synch = TRUE;
5994 
5995 	/* Transfer command to HBA */
5996 	if (sata_hba_start(spx, &rval) != 0) {
5997 		/* Pkt not accepted for execution */
5998 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5999 		return (rval);
6000 	}
6001 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
6002 
6003 	/*
6004 	 * If execution non-synchronous, it had to be completed
6005 	 * a callback function will handle potential errors, translate
6006 	 * the response and will do a callback to a target driver.
6007 	 * If it was synchronous, check status, using the same
6008 	 * framework callback.
6009 	 */
6010 	if (synch) {
6011 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6012 		    "synchronous execution status %x\n",
6013 		    spx->txlt_sata_pkt->satapkt_reason);
6014 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
6015 	}
6016 	return (TRAN_ACCEPT);
6017 }
6018 
6019 
6020 /*
6021  * Send pkt to SATA HBA driver
6022  *
6023  * This function may be called only if the operation is requested by scsi_pkt,
6024  * i.e. scsi_pkt is not NULL.
6025  *
6026  * This function has to be called with cport mutex held. It does release
6027  * the mutex when it calls HBA driver sata_tran_start function and
6028  * re-acquires it afterwards.
6029  *
6030  * If return value is 0, pkt was accepted, -1 otherwise
6031  * rval is set to appropriate sata_scsi_start return value.
6032  *
6033  * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not
6034  * have called the sata_pkt callback function for this packet.
6035  *
6036  * The scsi callback has to be performed by the caller of this routine.
6037  */
6038 static int
6039 sata_hba_start(sata_pkt_txlate_t *spx, int *rval)
6040 {
6041 	int stat;
6042 	uint8_t cport = SATA_TXLT_CPORT(spx);
6043 	uint8_t pmport = SATA_TXLT_PMPORT(spx);
6044 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
6045 	sata_drive_info_t *sdinfo;
6046 	sata_pmult_info_t *pminfo;
6047 	sata_pmport_info_t *pmportinfo = NULL;
6048 	sata_device_t *sata_device = NULL;
6049 	uint8_t cmd;
6050 	struct sata_cmd_flags cmd_flags;
6051 
6052 	ASSERT(spx->txlt_sata_pkt != NULL);
6053 
6054 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6055 
6056 	sdinfo = sata_get_device_info(sata_hba_inst,
6057 	    &spx->txlt_sata_pkt->satapkt_device);
6058 	ASSERT(sdinfo != NULL);
6059 
6060 	/* Clear device reset state? */
6061 	/* qual should be XXX_DPMPORT, but add XXX_PMPORT in case */
6062 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT ||
6063 	    sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT) {
6064 
6065 		/*
6066 		 * Get the pmult_info of the its parent port multiplier, all
6067 		 * sub-devices share a common device reset flags on in
6068 		 * pmult_info.
6069 		 */
6070 		pminfo = SATA_PMULT_INFO(sata_hba_inst, cport);
6071 		pmportinfo = pminfo->pmult_dev_port[pmport];
6072 		ASSERT(pminfo != NULL);
6073 		if (pminfo->pmult_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) {
6074 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
6075 			    sata_clear_dev_reset = B_TRUE;
6076 			pminfo->pmult_event_flags &=
6077 			    ~SATA_EVNT_CLEAR_DEVICE_RESET;
6078 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6079 			    "sata_hba_start: clearing device reset state"
6080 			    "on pmult.\n", NULL);
6081 		}
6082 	} else {
6083 		if (sdinfo->satadrv_event_flags &
6084 		    SATA_EVNT_CLEAR_DEVICE_RESET) {
6085 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
6086 			    sata_clear_dev_reset = B_TRUE;
6087 			sdinfo->satadrv_event_flags &=
6088 			    ~SATA_EVNT_CLEAR_DEVICE_RESET;
6089 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6090 			    "sata_hba_start: clearing device reset state\n",
6091 			    NULL);
6092 		}
6093 	}
6094 
6095 	cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg;
6096 	cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags;
6097 	sata_device = &spx->txlt_sata_pkt->satapkt_device;
6098 
6099 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6100 
6101 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6102 	    "Sata cmd 0x%2x\n", cmd);
6103 
6104 	stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
6105 	    spx->txlt_sata_pkt);
6106 
6107 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6108 	/*
6109 	 * If sata pkt was accepted and executed in asynchronous mode, i.e.
6110 	 * with the sata callback, the sata_pkt could be already destroyed
6111 	 * by the time we check ther return status from the hba_start()
6112 	 * function, because sata_scsi_destroy_pkt() could have been already
6113 	 * called (perhaps in the interrupt context). So, in such case, there
6114 	 * should be no references to it. In other cases, sata_pkt still
6115 	 * exists.
6116 	 */
6117 	if (stat == SATA_TRAN_ACCEPTED) {
6118 		/*
6119 		 * pkt accepted for execution.
6120 		 * If it was executed synchronously, it is already completed
6121 		 * and pkt completion_reason indicates completion status.
6122 		 */
6123 		*rval = TRAN_ACCEPT;
6124 		return (0);
6125 	}
6126 
6127 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
6128 	switch (stat) {
6129 	case SATA_TRAN_QUEUE_FULL:
6130 		/*
6131 		 * Controller detected queue full condition.
6132 		 */
6133 		SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst,
6134 		    "sata_hba_start: queue full\n", NULL);
6135 
6136 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
6137 		*spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL;
6138 
6139 		*rval = TRAN_BUSY;
6140 		break;
6141 
6142 	case SATA_TRAN_PORT_ERROR:
6143 		/*
6144 		 * Communication/link with device or general port error
6145 		 * detected before pkt execution begun.
6146 		 */
6147 		if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
6148 		    SATA_ADDR_CPORT ||
6149 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
6150 		    SATA_ADDR_DCPORT)
6151 			sata_log(sata_hba_inst, CE_CONT,
6152 			    "SATA port %d error",
6153 			    sata_device->satadev_addr.cport);
6154 		else
6155 			sata_log(sata_hba_inst, CE_CONT,
6156 			    "SATA port %d:%d error\n",
6157 			    sata_device->satadev_addr.cport,
6158 			    sata_device->satadev_addr.pmport);
6159 
6160 		/*
6161 		 * Update the port/device structure.
6162 		 * sata_pkt should be still valid. Since port error is
6163 		 * returned, sata_device content should reflect port
6164 		 * state - it means, that sata address have been changed,
6165 		 * because original packet's sata address refered to a device
6166 		 * attached to some port.
6167 		 */
6168 		if (sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT ||
6169 		    sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT) {
6170 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6171 			mutex_enter(&pmportinfo->pmport_mutex);
6172 			sata_update_pmport_info(sata_hba_inst, sata_device);
6173 			mutex_exit(&pmportinfo->pmport_mutex);
6174 			mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6175 		} else {
6176 			sata_update_port_info(sata_hba_inst, sata_device);
6177 		}
6178 
6179 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
6180 		*rval = TRAN_FATAL_ERROR;
6181 		break;
6182 
6183 	case SATA_TRAN_CMD_UNSUPPORTED:
6184 		/*
6185 		 * Command rejected by HBA as unsupported. It was HBA driver
6186 		 * that rejected the command, command was not sent to
6187 		 * an attached device.
6188 		 */
6189 		if ((sdinfo != NULL) &&
6190 		    (sdinfo->satadrv_state & SATA_DSTATE_RESET))
6191 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6192 			    "sat_hba_start: cmd 0x%2x rejected "
6193 			    "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd);
6194 
6195 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6196 		(void) sata_txlt_invalid_command(spx);
6197 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6198 
6199 		*rval = TRAN_ACCEPT;
6200 		break;
6201 
6202 	case SATA_TRAN_BUSY:
6203 		/*
6204 		 * Command rejected by HBA because other operation prevents
6205 		 * accepting the packet, or device is in RESET condition.
6206 		 */
6207 		if (sdinfo != NULL) {
6208 			sdinfo->satadrv_state =
6209 			    spx->txlt_sata_pkt->satapkt_device.satadev_state;
6210 
6211 			if (sdinfo->satadrv_state & SATA_DSTATE_RESET) {
6212 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6213 				    "sata_hba_start: cmd 0x%2x rejected "
6214 				    "because of device reset condition\n",
6215 				    cmd);
6216 			} else {
6217 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6218 				    "sata_hba_start: cmd 0x%2x rejected "
6219 				    "with SATA_TRAN_BUSY status\n",
6220 				    cmd);
6221 			}
6222 		}
6223 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
6224 		*rval = TRAN_BUSY;
6225 		break;
6226 
6227 	default:
6228 		/* Unrecognized HBA response */
6229 		SATA_LOG_D((sata_hba_inst, CE_WARN,
6230 		    "sata_hba_start: unrecognized HBA response "
6231 		    "to cmd : 0x%2x resp 0x%x", cmd, rval));
6232 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
6233 		*rval = TRAN_FATAL_ERROR;
6234 		break;
6235 	}
6236 
6237 	/*
6238 	 * If we got here, the packet was rejected.
6239 	 * Check if we need to remember reset state clearing request
6240 	 */
6241 	if (cmd_flags.sata_clear_dev_reset) {
6242 		/*
6243 		 * Check if device is still configured - it may have
6244 		 * disapeared from the configuration
6245 		 */
6246 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
6247 		if (sdinfo != NULL) {
6248 			/*
6249 			 * Restore the flag that requests clearing of
6250 			 * the device reset state,
6251 			 * so the next sata packet may carry it to HBA.
6252 			 */
6253 			if (sdinfo->satadrv_addr.qual == SATA_ADDR_PMPORT ||
6254 			    sdinfo->satadrv_addr.qual == SATA_ADDR_DPMPORT) {
6255 				pminfo->pmult_event_flags |=
6256 				    SATA_EVNT_CLEAR_DEVICE_RESET;
6257 			} else {
6258 				sdinfo->satadrv_event_flags |=
6259 				    SATA_EVNT_CLEAR_DEVICE_RESET;
6260 			}
6261 		}
6262 	}
6263 	return (-1);
6264 }
6265 
6266 /*
6267  * Scsi response setup for invalid LBA
6268  *
6269  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
6270  */
6271 static int
6272 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx)
6273 {
6274 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6275 	struct scsi_extended_sense *sense;
6276 
6277 	scsipkt->pkt_reason = CMD_CMPLT;
6278 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6279 	    STATE_SENT_CMD | STATE_GOT_STATUS;
6280 	*scsipkt->pkt_scbp = STATUS_CHECK;
6281 
6282 	*scsipkt->pkt_scbp = STATUS_CHECK;
6283 	sense = sata_arq_sense(spx);
6284 	sense->es_key = KEY_ILLEGAL_REQUEST;
6285 	sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
6286 
6287 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6288 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6289 
6290 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6291 	    scsipkt->pkt_comp != NULL)
6292 		/* scsi callback required */
6293 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6294 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
6295 		    TQ_SLEEP) == NULL)
6296 			/* Scheduling the callback failed */
6297 			return (TRAN_BUSY);
6298 	return (TRAN_ACCEPT);
6299 }
6300 
6301 
6302 /*
6303  * Analyze device status and error registers and translate them into
6304  * appropriate scsi sense codes.
6305  * NOTE: non-packet commands only for now
6306  */
6307 static void
6308 sata_decode_device_error(sata_pkt_txlate_t *spx,
6309     struct scsi_extended_sense *sense)
6310 {
6311 	uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg;
6312 
6313 	ASSERT(sense != NULL);
6314 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
6315 	    SATA_STATUS_ERR);
6316 
6317 
6318 	if (err_reg & SATA_ERROR_ICRC) {
6319 		sense->es_key = KEY_ABORTED_COMMAND;
6320 		sense->es_add_code = 0x08; /* Communication failure */
6321 		return;
6322 	}
6323 
6324 	if (err_reg & SATA_ERROR_UNC) {
6325 		sense->es_key = KEY_MEDIUM_ERROR;
6326 		/* Information bytes (LBA) need to be set by a caller */
6327 		return;
6328 	}
6329 
6330 	/* ADD HERE: MC error bit handling for ATAPI CD/DVD */
6331 	if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) {
6332 		sense->es_key = KEY_UNIT_ATTENTION;
6333 		sense->es_add_code = 0x3a; /* No media present */
6334 		return;
6335 	}
6336 
6337 	if (err_reg & SATA_ERROR_IDNF) {
6338 		if (err_reg & SATA_ERROR_ABORT) {
6339 			sense->es_key = KEY_ABORTED_COMMAND;
6340 		} else {
6341 			sense->es_key = KEY_ILLEGAL_REQUEST;
6342 			sense->es_add_code = 0x21; /* LBA out of range */
6343 		}
6344 		return;
6345 	}
6346 
6347 	if (err_reg & SATA_ERROR_ABORT) {
6348 		ASSERT(spx->txlt_sata_pkt != NULL);
6349 		sense->es_key = KEY_ABORTED_COMMAND;
6350 		return;
6351 	}
6352 }
6353 
6354 /*
6355  * Extract error LBA from sata_pkt.satapkt_cmd register fields
6356  */
6357 static void
6358 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba)
6359 {
6360 	sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd;
6361 
6362 	*lba = 0;
6363 	if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) {
6364 		*lba = sata_cmd->satacmd_lba_high_msb;
6365 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb;
6366 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb;
6367 	} else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) {
6368 		*lba = sata_cmd->satacmd_device_reg & 0xf;
6369 	}
6370 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb;
6371 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb;
6372 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb;
6373 }
6374 
6375 /*
6376  * This is fixed sense format - if LBA exceeds the info field size,
6377  * no valid info will be returned (valid bit in extended sense will
6378  * be set to 0).
6379  */
6380 static struct scsi_extended_sense *
6381 sata_arq_sense(sata_pkt_txlate_t *spx)
6382 {
6383 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6384 	struct scsi_arq_status *arqs;
6385 	struct scsi_extended_sense *sense;
6386 
6387 	/* Fill ARQ sense data */
6388 	scsipkt->pkt_state |= STATE_ARQ_DONE;
6389 	arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp;
6390 	*(uchar_t *)&arqs->sts_status = STATUS_CHECK;
6391 	*(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD;
6392 	arqs->sts_rqpkt_reason = CMD_CMPLT;
6393 	arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6394 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
6395 	arqs->sts_rqpkt_resid = 0;
6396 	sense = &arqs->sts_sensedata;
6397 	bzero(sense, sizeof (struct scsi_extended_sense));
6398 	sata_fixed_sense_data_preset(sense);
6399 	return (sense);
6400 }
6401 
6402 
6403 /*
6404  * Emulated SATA Read/Write command completion for zero-length requests.
6405  * This request always succedes, so in synchronous mode it always returns
6406  * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the
6407  * callback cannot be scheduled.
6408  */
6409 static int
6410 sata_emul_rw_completion(sata_pkt_txlate_t *spx)
6411 {
6412 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6413 
6414 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6415 	    STATE_SENT_CMD | STATE_GOT_STATUS;
6416 	scsipkt->pkt_reason = CMD_CMPLT;
6417 	*scsipkt->pkt_scbp = STATUS_GOOD;
6418 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6419 		/* scsi callback required - have to schedule it */
6420 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6421 		    (task_func_t *)scsipkt->pkt_comp,
6422 		    (void *)scsipkt, TQ_SLEEP) == NULL)
6423 			/* Scheduling the callback failed */
6424 			return (TRAN_BUSY);
6425 	}
6426 	return (TRAN_ACCEPT);
6427 }
6428 
6429 
6430 /*
6431  * Translate completion status of SATA read/write commands into scsi response.
6432  * pkt completion_reason is checked to determine the completion status.
6433  * Do scsi callback if necessary.
6434  *
6435  * Note: this function may be called also for synchronously executed
6436  * commands.
6437  * This function may be used only if scsi_pkt is non-NULL.
6438  */
6439 static void
6440 sata_txlt_rw_completion(sata_pkt_t *sata_pkt)
6441 {
6442 	sata_pkt_txlate_t *spx =
6443 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
6444 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
6445 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6446 	struct scsi_extended_sense *sense;
6447 	uint64_t lba;
6448 	struct buf *bp;
6449 	int rval;
6450 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
6451 		/* Normal completion */
6452 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6453 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
6454 		scsipkt->pkt_reason = CMD_CMPLT;
6455 		*scsipkt->pkt_scbp = STATUS_GOOD;
6456 		if (spx->txlt_tmp_buf != NULL) {
6457 			/* Temporary buffer was used */
6458 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
6459 			if (bp->b_flags & B_READ) {
6460 				rval = ddi_dma_sync(
6461 				    spx->txlt_buf_dma_handle, 0, 0,
6462 				    DDI_DMA_SYNC_FORCPU);
6463 				ASSERT(rval == DDI_SUCCESS);
6464 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
6465 				    bp->b_bcount);
6466 			}
6467 		}
6468 	} else {
6469 		/*
6470 		 * Something went wrong - analyze return
6471 		 */
6472 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6473 		    STATE_SENT_CMD | STATE_GOT_STATUS;
6474 		scsipkt->pkt_reason = CMD_INCOMPLETE;
6475 		*scsipkt->pkt_scbp = STATUS_CHECK;
6476 		sense = sata_arq_sense(spx);
6477 		ASSERT(sense != NULL);
6478 
6479 		/*
6480 		 * SATA_PKT_DEV_ERROR is the only case where we may be able to
6481 		 * extract from device registers the failing LBA.
6482 		 */
6483 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
6484 			if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) &&
6485 			    (scmd->satacmd_lba_mid_msb != 0 ||
6486 			    scmd->satacmd_lba_high_msb != 0)) {
6487 				/*
6488 				 * We have problem reporting this cmd LBA
6489 				 * in fixed sense data format, because of
6490 				 * the size of the scsi LBA fields.
6491 				 */
6492 				sense->es_valid = 0;
6493 			} else {
6494 				sata_extract_error_lba(spx, &lba);
6495 				sense->es_info_1 = (lba & 0xFF000000) >> 24;
6496 				sense->es_info_2 = (lba & 0xFF0000) >> 16;
6497 				sense->es_info_3 = (lba & 0xFF00) >> 8;
6498 				sense->es_info_4 = lba & 0xFF;
6499 			}
6500 		} else {
6501 			/* Invalid extended sense info */
6502 			sense->es_valid = 0;
6503 		}
6504 
6505 		switch (sata_pkt->satapkt_reason) {
6506 		case SATA_PKT_PORT_ERROR:
6507 			/* We may want to handle DEV GONE state as well */
6508 			/*
6509 			 * We have no device data. Assume no data transfered.
6510 			 */
6511 			sense->es_key = KEY_HARDWARE_ERROR;
6512 			break;
6513 
6514 		case SATA_PKT_DEV_ERROR:
6515 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
6516 			    SATA_STATUS_ERR) {
6517 				/*
6518 				 * determine dev error reason from error
6519 				 * reg content
6520 				 */
6521 				sata_decode_device_error(spx, sense);
6522 				if (sense->es_key == KEY_MEDIUM_ERROR) {
6523 					switch (scmd->satacmd_cmd_reg) {
6524 					case SATAC_READ_DMA:
6525 					case SATAC_READ_DMA_EXT:
6526 					case SATAC_READ_DMA_QUEUED:
6527 					case SATAC_READ_DMA_QUEUED_EXT:
6528 					case SATAC_READ_FPDMA_QUEUED:
6529 						/* Unrecovered read error */
6530 						sense->es_add_code =
6531 						    SD_SCSI_ASC_UNREC_READ_ERR;
6532 						break;
6533 					case SATAC_WRITE_DMA:
6534 					case SATAC_WRITE_DMA_EXT:
6535 					case SATAC_WRITE_DMA_QUEUED:
6536 					case SATAC_WRITE_DMA_QUEUED_EXT:
6537 					case SATAC_WRITE_FPDMA_QUEUED:
6538 						/* Write error */
6539 						sense->es_add_code =
6540 						    SD_SCSI_ASC_WRITE_ERR;
6541 						break;
6542 					default:
6543 						/* Internal error */
6544 						SATA_LOG_D((
6545 						    spx->txlt_sata_hba_inst,
6546 						    CE_WARN,
6547 						    "sata_txlt_rw_completion :"
6548 						    "internal error - invalid "
6549 						    "command 0x%2x",
6550 						    scmd->satacmd_cmd_reg));
6551 						break;
6552 					}
6553 				}
6554 				break;
6555 			}
6556 			/* No extended sense key - no info available */
6557 			scsipkt->pkt_reason = CMD_INCOMPLETE;
6558 			break;
6559 
6560 		case SATA_PKT_TIMEOUT:
6561 			scsipkt->pkt_reason = CMD_TIMEOUT;
6562 			scsipkt->pkt_statistics |=
6563 			    STAT_TIMEOUT | STAT_DEV_RESET;
6564 			sense->es_key = KEY_ABORTED_COMMAND;
6565 			break;
6566 
6567 		case SATA_PKT_ABORTED:
6568 			scsipkt->pkt_reason = CMD_ABORTED;
6569 			scsipkt->pkt_statistics |= STAT_ABORTED;
6570 			sense->es_key = KEY_ABORTED_COMMAND;
6571 			break;
6572 
6573 		case SATA_PKT_RESET:
6574 			scsipkt->pkt_reason = CMD_RESET;
6575 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
6576 			sense->es_key = KEY_ABORTED_COMMAND;
6577 			break;
6578 
6579 		default:
6580 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
6581 			    "sata_txlt_rw_completion: "
6582 			    "invalid packet completion reason"));
6583 			scsipkt->pkt_reason = CMD_TRAN_ERR;
6584 			break;
6585 		}
6586 	}
6587 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6588 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6589 
6590 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6591 		/* scsi callback required */
6592 		scsi_hba_pkt_comp(scsipkt);
6593 }
6594 
6595 
6596 /*
6597  * Translate completion status of non-data commands (i.e. commands returning
6598  * no data).
6599  * pkt completion_reason is checked to determine the completion status.
6600  * Do scsi callback if necessary (FLAG_NOINTR == 0)
6601  *
6602  * Note: this function may be called also for synchronously executed
6603  * commands.
6604  * This function may be used only if scsi_pkt is non-NULL.
6605  */
6606 
6607 static	void
6608 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt)
6609 {
6610 	sata_pkt_txlate_t *spx =
6611 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
6612 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6613 
6614 	sata_set_arq_data(sata_pkt);
6615 
6616 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0)
6617 		/* scsi callback required */
6618 		scsi_hba_pkt_comp(scsipkt);
6619 }
6620 
6621 static	void
6622 sata_set_arq_data(sata_pkt_t *sata_pkt)
6623 {
6624 	sata_pkt_txlate_t *spx =
6625 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
6626 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6627 	struct scsi_extended_sense *sense;
6628 
6629 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6630 	    STATE_SENT_CMD | STATE_GOT_STATUS;
6631 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
6632 		/* Normal completion */
6633 		scsipkt->pkt_reason = CMD_CMPLT;
6634 		*scsipkt->pkt_scbp = STATUS_GOOD;
6635 	} else {
6636 		/* Something went wrong */
6637 		scsipkt->pkt_reason = CMD_INCOMPLETE;
6638 		*scsipkt->pkt_scbp = STATUS_CHECK;
6639 		sense = sata_arq_sense(spx);
6640 		switch (sata_pkt->satapkt_reason) {
6641 		case SATA_PKT_PORT_ERROR:
6642 			/*
6643 			 * We have no device data. Assume no data transfered.
6644 			 */
6645 			sense->es_key = KEY_HARDWARE_ERROR;
6646 			break;
6647 
6648 		case SATA_PKT_DEV_ERROR:
6649 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
6650 			    SATA_STATUS_ERR) {
6651 				/*
6652 				 * determine dev error reason from error
6653 				 * reg content
6654 				 */
6655 				sata_decode_device_error(spx, sense);
6656 				break;
6657 			}
6658 			/* No extended sense key - no info available */
6659 			break;
6660 
6661 		case SATA_PKT_TIMEOUT:
6662 			scsipkt->pkt_reason = CMD_TIMEOUT;
6663 			scsipkt->pkt_statistics |=
6664 			    STAT_TIMEOUT | STAT_DEV_RESET;
6665 			/* No extended sense key ? */
6666 			break;
6667 
6668 		case SATA_PKT_ABORTED:
6669 			scsipkt->pkt_reason = CMD_ABORTED;
6670 			scsipkt->pkt_statistics |= STAT_ABORTED;
6671 			/* No extended sense key ? */
6672 			break;
6673 
6674 		case SATA_PKT_RESET:
6675 			/* pkt aborted by an explicit reset from a host */
6676 			scsipkt->pkt_reason = CMD_RESET;
6677 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
6678 			break;
6679 
6680 		default:
6681 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
6682 			    "sata_txlt_nodata_cmd_completion: "
6683 			    "invalid packet completion reason %d",
6684 			    sata_pkt->satapkt_reason));
6685 			scsipkt->pkt_reason = CMD_TRAN_ERR;
6686 			break;
6687 		}
6688 
6689 	}
6690 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6691 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6692 }
6693 
6694 
6695 /*
6696  * Build Mode sense R/W recovery page
6697  * NOT IMPLEMENTED
6698  */
6699 
6700 static int
6701 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6702 {
6703 #ifndef __lock_lint
6704 	_NOTE(ARGUNUSED(sdinfo))
6705 	_NOTE(ARGUNUSED(pcntrl))
6706 	_NOTE(ARGUNUSED(buf))
6707 #endif
6708 	return (0);
6709 }
6710 
6711 /*
6712  * Build Mode sense caching page  -  scsi-3 implementation.
6713  * Page length distinguishes previous format from scsi-3 format.
6714  * buf must have space for 0x12 bytes.
6715  * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable.
6716  *
6717  */
6718 static int
6719 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6720 {
6721 	struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf;
6722 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6723 
6724 	/*
6725 	 * Most of the fields are set to 0, being not supported and/or disabled
6726 	 */
6727 	bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3);
6728 
6729 	/* Saved paramters not supported */
6730 	if (pcntrl == 3)
6731 		return (0);
6732 	if (pcntrl == 0 || pcntrl == 2) {
6733 		/*
6734 		 * For now treat current and default parameters as same
6735 		 * That may have to change, if target driver will complain
6736 		 */
6737 		page->mode_page.code = MODEPAGE_CACHING;	/* PS = 0 */
6738 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
6739 
6740 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id) &&
6741 		    !SATA_READ_AHEAD_ENABLED(*sata_id)) {
6742 			page->dra = 1;		/* Read Ahead disabled */
6743 			page->rcd = 1;		/* Read Cache disabled */
6744 		}
6745 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id) &&
6746 		    SATA_WRITE_CACHE_ENABLED(*sata_id))
6747 			page->wce = 1;		/* Write Cache enabled */
6748 	} else {
6749 		/* Changeable parameters */
6750 		page->mode_page.code = MODEPAGE_CACHING;
6751 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
6752 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
6753 			page->dra = 1;
6754 			page->rcd = 1;
6755 		}
6756 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id))
6757 			page->wce = 1;
6758 	}
6759 	return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
6760 	    sizeof (struct mode_page));
6761 }
6762 
6763 /*
6764  * Build Mode sense exception cntrl page
6765  */
6766 static int
6767 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6768 {
6769 	struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf;
6770 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6771 
6772 	/*
6773 	 * Most of the fields are set to 0, being not supported and/or disabled
6774 	 */
6775 	bzero(buf, PAGELENGTH_INFO_EXCPT);
6776 
6777 	page->mode_page.code = MODEPAGE_INFO_EXCPT;
6778 	page->mode_page.length = PAGELENGTH_INFO_EXCPT;
6779 
6780 	/* Indicate that this is page is saveable */
6781 	page->mode_page.ps = 1;
6782 
6783 	/*
6784 	 * We will return the same data for default, current and saved page.
6785 	 * The only changeable bit is dexcpt and that bit is required
6786 	 * by the ATA specification to be preserved across power cycles.
6787 	 */
6788 	if (pcntrl != 1) {
6789 		page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED);
6790 		page->mrie = MRIE_ONLY_ON_REQUEST;
6791 	}
6792 	else
6793 		page->dexcpt = 1;	/* Only changeable parameter */
6794 
6795 	return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page));
6796 }
6797 
6798 
6799 static int
6800 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6801 {
6802 	struct mode_acoustic_management *page =
6803 	    (struct mode_acoustic_management *)buf;
6804 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6805 
6806 	/*
6807 	 * Most of the fields are set to 0, being not supported and/or disabled
6808 	 */
6809 	bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT);
6810 
6811 	switch (pcntrl) {
6812 	case P_CNTRL_DEFAULT:
6813 		/*  default paramters not supported */
6814 		return (0);
6815 
6816 	case P_CNTRL_CURRENT:
6817 	case P_CNTRL_SAVED:
6818 		/* Saved and current are supported and are identical */
6819 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
6820 		page->mode_page.length =
6821 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
6822 		page->mode_page.ps = 1;
6823 
6824 		/* Word 83 indicates if feature is supported */
6825 		/* If feature is not supported */
6826 		if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) {
6827 			page->acoustic_manag_enable =
6828 			    ACOUSTIC_DISABLED;
6829 		} else {
6830 			page->acoustic_manag_enable =
6831 			    ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT)
6832 			    != 0);
6833 			/* Word 94 inidicates the value */
6834 #ifdef	_LITTLE_ENDIAN
6835 			page->acoustic_manag_level =
6836 			    (uchar_t)sata_id->ai_acoustic;
6837 			page->vendor_recommended_value =
6838 			    sata_id->ai_acoustic >> 8;
6839 #else
6840 			page->acoustic_manag_level =
6841 			    sata_id->ai_acoustic >> 8;
6842 			page->vendor_recommended_value =
6843 			    (uchar_t)sata_id->ai_acoustic;
6844 #endif
6845 		}
6846 		break;
6847 
6848 	case P_CNTRL_CHANGEABLE:
6849 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
6850 		page->mode_page.length =
6851 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
6852 		page->mode_page.ps = 1;
6853 
6854 		/* Word 83 indicates if the feature is supported */
6855 		if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) {
6856 			page->acoustic_manag_enable =
6857 			    ACOUSTIC_ENABLED;
6858 			page->acoustic_manag_level = 0xff;
6859 		}
6860 		break;
6861 	}
6862 	return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6863 	    sizeof (struct mode_page));
6864 }
6865 
6866 
6867 /*
6868  * Build Mode sense power condition page.
6869  */
6870 static int
6871 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6872 {
6873 	struct mode_info_power_cond *page = (struct mode_info_power_cond *)buf;
6874 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6875 
6876 	/*
6877 	 * Most of the fields are set to 0, being not supported and/or disabled
6878 	 * power condition page length was 0x0a
6879 	 */
6880 	bzero(buf, sizeof (struct mode_info_power_cond));
6881 
6882 	if (pcntrl == P_CNTRL_DEFAULT) {
6883 		/*  default paramters not supported */
6884 		return (0);
6885 	}
6886 
6887 	page->mode_page.code = MODEPAGE_POWER_COND;
6888 	page->mode_page.length = sizeof (struct mode_info_power_cond);
6889 
6890 	if (sata_id->ai_cap && SATA_STANDBYTIMER) {
6891 		page->standby = 1;
6892 		bcopy(sdinfo->satadrv_standby_timer, page->standby_cond_timer,
6893 		    sizeof (uchar_t) * 4);
6894 	}
6895 
6896 	return (sizeof (struct mode_info_power_cond));
6897 }
6898 
6899 /*
6900  * Process mode select caching page 8 (scsi3 format only).
6901  * Read Ahead (same as read cache) and Write Cache may be turned on and off
6902  * if these features are supported by the device. If these features are not
6903  * supported, the command will be terminated with STATUS_CHECK.
6904  * This function fails only if the SET FEATURE command sent to
6905  * the device fails. The page format is not varified, assuming that the
6906  * target driver operates correctly - if parameters length is too short,
6907  * we just drop the page.
6908  * Two command may be sent if both Read Cache/Read Ahead and Write Cache
6909  * setting have to be changed.
6910  * SET FEATURE command is executed synchronously, i.e. we wait here until
6911  * it is completed, regardless of the scsi pkt directives.
6912  *
6913  * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e.
6914  * changing DRA will change RCD.
6915  *
6916  * More than one SATA command may be executed to perform operations specified
6917  * by mode select pages. The first error terminates further execution.
6918  * Operations performed successully are not backed-up in such case.
6919  *
6920  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
6921  * If operation resulted in changing device setup, dmod flag should be set to
6922  * one (1). If parameters were not changed, dmod flag should be set to 0.
6923  * Upon return, if operation required sending command to the device, the rval
6924  * should be set to the value returned by sata_hba_start. If operation
6925  * did not require device access, rval should be set to TRAN_ACCEPT.
6926  * The pagelen should be set to the length of the page.
6927  *
6928  * This function has to be called with a port mutex held.
6929  *
6930  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6931  */
6932 int
6933 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page,
6934     int parmlen, int *pagelen, int *rval, int *dmod)
6935 {
6936 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6937 	sata_drive_info_t *sdinfo;
6938 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6939 	sata_id_t *sata_id;
6940 	struct scsi_extended_sense *sense;
6941 	int wce, dra;	/* Current settings */
6942 
6943 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6944 	    &spx->txlt_sata_pkt->satapkt_device);
6945 	sata_id = &sdinfo->satadrv_id;
6946 	*dmod = 0;
6947 
6948 	/* Verify parameters length. If too short, drop it */
6949 	if ((PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
6950 	    sizeof (struct mode_page)) > parmlen) {
6951 		*scsipkt->pkt_scbp = STATUS_CHECK;
6952 		sense = sata_arq_sense(spx);
6953 		sense->es_key = KEY_ILLEGAL_REQUEST;
6954 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6955 		*pagelen = parmlen;
6956 		*rval = TRAN_ACCEPT;
6957 		return (SATA_FAILURE);
6958 	}
6959 
6960 	*pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page);
6961 
6962 	/* Current setting of Read Ahead (and Read Cache) */
6963 	if (SATA_READ_AHEAD_ENABLED(*sata_id))
6964 		dra = 0;	/* 0 == not disabled */
6965 	else
6966 		dra = 1;
6967 	/* Current setting of Write Cache */
6968 	if (SATA_WRITE_CACHE_ENABLED(*sata_id))
6969 		wce = 1;
6970 	else
6971 		wce = 0;
6972 
6973 	if (page->dra == dra && page->wce == wce && page->rcd == dra) {
6974 		/* nothing to do */
6975 		*rval = TRAN_ACCEPT;
6976 		return (SATA_SUCCESS);
6977 	}
6978 
6979 	/*
6980 	 * Need to flip some setting
6981 	 * Set-up Internal SET FEATURES command(s)
6982 	 */
6983 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6984 	scmd->satacmd_addr_type = 0;
6985 	scmd->satacmd_device_reg = 0;
6986 	scmd->satacmd_status_reg = 0;
6987 	scmd->satacmd_error_reg = 0;
6988 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
6989 	if (page->dra != dra || page->rcd != dra) {
6990 		if (SATA_READ_AHEAD_SUPPORTED(*sata_id)) {
6991 			/* Need to flip read ahead setting */
6992 			if (dra == 0)
6993 				/* Disable read ahead / read cache */
6994 				scmd->satacmd_features_reg =
6995 				    SATAC_SF_DISABLE_READ_AHEAD;
6996 			else
6997 				/* Enable read ahead  / read cache */
6998 				scmd->satacmd_features_reg =
6999 				    SATAC_SF_ENABLE_READ_AHEAD;
7000 
7001 			/* Transfer command to HBA */
7002 			if (sata_hba_start(spx, rval) != 0)
7003 				/*
7004 				 * Pkt not accepted for execution.
7005 				 */
7006 				return (SATA_FAILURE);
7007 
7008 			*dmod = 1;
7009 
7010 			/* Now process return */
7011 			if (spx->txlt_sata_pkt->satapkt_reason !=
7012 			    SATA_PKT_COMPLETED) {
7013 				goto failure;	/* Terminate */
7014 			}
7015 		} else {
7016 			*scsipkt->pkt_scbp = STATUS_CHECK;
7017 			sense = sata_arq_sense(spx);
7018 			sense->es_key = KEY_ILLEGAL_REQUEST;
7019 			sense->es_add_code =
7020 			    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
7021 			*pagelen = parmlen;
7022 			*rval = TRAN_ACCEPT;
7023 			return (SATA_FAILURE);
7024 		}
7025 	}
7026 
7027 	/* Note that the packet is not removed, so it could be re-used */
7028 	if (page->wce != wce) {
7029 		if (SATA_WRITE_CACHE_SUPPORTED(*sata_id)) {
7030 			/* Need to flip Write Cache setting */
7031 			if (page->wce == 1)
7032 				/* Enable write cache */
7033 				scmd->satacmd_features_reg =
7034 				    SATAC_SF_ENABLE_WRITE_CACHE;
7035 			else
7036 				/* Disable write cache */
7037 				scmd->satacmd_features_reg =
7038 				    SATAC_SF_DISABLE_WRITE_CACHE;
7039 
7040 			/* Transfer command to HBA */
7041 			if (sata_hba_start(spx, rval) != 0)
7042 				/*
7043 				 * Pkt not accepted for execution.
7044 				 */
7045 				return (SATA_FAILURE);
7046 
7047 			*dmod = 1;
7048 
7049 			/* Now process return */
7050 			if (spx->txlt_sata_pkt->satapkt_reason !=
7051 			    SATA_PKT_COMPLETED) {
7052 				goto failure;
7053 			}
7054 		} else {
7055 			*scsipkt->pkt_scbp = STATUS_CHECK;
7056 			sense = sata_arq_sense(spx);
7057 			sense->es_key = KEY_ILLEGAL_REQUEST;
7058 			sense->es_add_code =
7059 			    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
7060 			*pagelen = parmlen;
7061 			*rval = TRAN_ACCEPT;
7062 			return (SATA_FAILURE);
7063 		}
7064 	}
7065 	return (SATA_SUCCESS);
7066 
7067 failure:
7068 	sata_xlate_errors(spx);
7069 
7070 	return (SATA_FAILURE);
7071 }
7072 
7073 /*
7074  * Process mode select informational exceptions control page 0x1c
7075  *
7076  * The only changeable bit is dexcpt (disable exceptions).
7077  * MRIE (method of reporting informational exceptions) must be
7078  * "only on request".
7079  * This page applies to informational exceptions that report
7080  * additional sense codes with the ADDITIONAL SENSE CODE field set to 5Dh
7081  * (e.g.,FAILURE PREDICTION THRESHOLD EXCEEDED) or 0Bh (e.g., WARNING_).
7082  * Informational exception conditions occur as the result of background scan
7083  * errors, background self-test errors, or vendor specific events within a
7084  * logical unit. An informational exception condition may occur asynchronous
7085  * to any commands.
7086  *
7087  * Returns: SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
7088  * If operation resulted in changing device setup, dmod flag should be set to
7089  * one (1). If parameters were not changed, dmod flag should be set to 0.
7090  * Upon return, if operation required sending command to the device, the rval
7091  * should be set to the value returned by sata_hba_start. If operation
7092  * did not require device access, rval should be set to TRAN_ACCEPT.
7093  * The pagelen should be set to the length of the page.
7094  *
7095  * This function has to be called with a port mutex held.
7096  *
7097  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
7098  *
7099  * Cannot be called in the interrupt context.
7100  */
7101 static	int
7102 sata_mode_select_page_1c(
7103 	sata_pkt_txlate_t *spx,
7104 	struct mode_info_excpt_page *page,
7105 	int parmlen,
7106 	int *pagelen,
7107 	int *rval,
7108 	int *dmod)
7109 {
7110 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7111 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
7112 	sata_drive_info_t *sdinfo;
7113 	sata_id_t *sata_id;
7114 	struct scsi_extended_sense *sense;
7115 
7116 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
7117 	    &spx->txlt_sata_pkt->satapkt_device);
7118 	sata_id = &sdinfo->satadrv_id;
7119 
7120 	*dmod = 0;
7121 
7122 	/* Verify parameters length. If too short, drop it */
7123 	if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) > parmlen) ||
7124 	    page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) {
7125 		*scsipkt->pkt_scbp = STATUS_CHECK;
7126 		sense = sata_arq_sense(spx);
7127 		sense->es_key = KEY_ILLEGAL_REQUEST;
7128 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
7129 		*pagelen = parmlen;
7130 		*rval = TRAN_ACCEPT;
7131 		return (SATA_FAILURE);
7132 	}
7133 
7134 	*pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page);
7135 
7136 	if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
7137 		*scsipkt->pkt_scbp = STATUS_CHECK;
7138 		sense = sata_arq_sense(spx);
7139 		sense->es_key = KEY_ILLEGAL_REQUEST;
7140 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
7141 		*pagelen = parmlen;
7142 		*rval = TRAN_ACCEPT;
7143 		return (SATA_FAILURE);
7144 	}
7145 
7146 	/* If already in the state requested, we are done */
7147 	if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
7148 		/* nothing to do */
7149 		*rval = TRAN_ACCEPT;
7150 		return (SATA_SUCCESS);
7151 	}
7152 
7153 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
7154 
7155 	/* Build SMART_ENABLE or SMART_DISABLE command */
7156 	scmd->satacmd_addr_type = 0;		/* N/A */
7157 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
7158 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
7159 	scmd->satacmd_features_reg = page->dexcpt ?
7160 	    SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS;
7161 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
7162 	scmd->satacmd_cmd_reg = SATAC_SMART;
7163 
7164 	/* Transfer command to HBA */
7165 	if (sata_hba_start(spx, rval) != 0)
7166 		/*
7167 		 * Pkt not accepted for execution.
7168 		 */
7169 		return (SATA_FAILURE);
7170 
7171 	*dmod = 1;	/* At least may have been modified */
7172 
7173 	/* Now process return */
7174 	if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED)
7175 		return (SATA_SUCCESS);
7176 
7177 	/* Packet did not complete successfully */
7178 	sata_xlate_errors(spx);
7179 
7180 	return (SATA_FAILURE);
7181 }
7182 
7183 /*
7184  * Process mode select acoustic management control page 0x30
7185  *
7186  *
7187  * This function has to be called with a port mutex held.
7188  *
7189  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
7190  *
7191  * Cannot be called in the interrupt context.
7192  */
7193 int
7194 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct
7195     mode_acoustic_management *page, int parmlen, int *pagelen,
7196     int *rval, int *dmod)
7197 {
7198 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7199 	sata_drive_info_t *sdinfo;
7200 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
7201 	sata_id_t *sata_id;
7202 	struct scsi_extended_sense *sense;
7203 
7204 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
7205 	    &spx->txlt_sata_pkt->satapkt_device);
7206 	sata_id = &sdinfo->satadrv_id;
7207 	*dmod = 0;
7208 
7209 	/* If parmlen is too short or the feature is not supported, drop it */
7210 	if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
7211 	    sizeof (struct mode_page)) > parmlen) ||
7212 	    (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) {
7213 		*scsipkt->pkt_scbp = STATUS_CHECK;
7214 		sense = sata_arq_sense(spx);
7215 		sense->es_key = KEY_ILLEGAL_REQUEST;
7216 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
7217 		*pagelen = parmlen;
7218 		*rval = TRAN_ACCEPT;
7219 		return (SATA_FAILURE);
7220 	}
7221 
7222 	*pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
7223 	    sizeof (struct mode_page);
7224 
7225 	/*
7226 	 * We can enable and disable acoustice management and
7227 	 * set the acoustic management level.
7228 	 */
7229 
7230 	/*
7231 	 * Set-up Internal SET FEATURES command(s)
7232 	 */
7233 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
7234 	scmd->satacmd_addr_type = 0;
7235 	scmd->satacmd_device_reg = 0;
7236 	scmd->satacmd_status_reg = 0;
7237 	scmd->satacmd_error_reg = 0;
7238 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
7239 	if (page->acoustic_manag_enable) {
7240 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC;
7241 		scmd->satacmd_sec_count_lsb = page->acoustic_manag_level;
7242 	} else {	/* disabling acoustic management */
7243 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC;
7244 	}
7245 
7246 	/* Transfer command to HBA */
7247 	if (sata_hba_start(spx, rval) != 0)
7248 		/*
7249 		 * Pkt not accepted for execution.
7250 		 */
7251 		return (SATA_FAILURE);
7252 
7253 	/* Now process return */
7254 	if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) {
7255 		sata_xlate_errors(spx);
7256 		return (SATA_FAILURE);
7257 	}
7258 
7259 	*dmod = 1;
7260 
7261 	return (SATA_SUCCESS);
7262 }
7263 
7264 /*
7265  * Process mode select power condition page 0x1a
7266  *
7267  * This function has to be called with a port mutex held.
7268  *
7269  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
7270  *
7271  * Cannot be called in the interrupt context.
7272  */
7273 int
7274 sata_mode_select_page_1a(sata_pkt_txlate_t *spx, struct
7275     mode_info_power_cond *page, int parmlen, int *pagelen,
7276     int *rval, int *dmod)
7277 {
7278 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7279 	sata_drive_info_t *sdinfo;
7280 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
7281 	sata_id_t *sata_id;
7282 	struct scsi_extended_sense *sense;
7283 	uint8_t ata_count;
7284 	int i, len;
7285 
7286 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
7287 	    &spx->txlt_sata_pkt->satapkt_device);
7288 	sata_id = &sdinfo->satadrv_id;
7289 	*dmod = 0;
7290 
7291 	len = sizeof (struct mode_info_power_cond);
7292 	len += sizeof (struct mode_page);
7293 
7294 	/* If parmlen is too short or the feature is not supported, drop it */
7295 	if ((len < parmlen) || (page->idle == 1) ||
7296 	    (!(sata_id->ai_cap && SATA_STANDBYTIMER) && page->standby == 1)) {
7297 		*scsipkt->pkt_scbp = STATUS_CHECK;
7298 		sense = sata_arq_sense(spx);
7299 		sense->es_key = KEY_ILLEGAL_REQUEST;
7300 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
7301 		*pagelen = parmlen;
7302 		*rval = TRAN_ACCEPT;
7303 		return (SATA_FAILURE);
7304 	}
7305 
7306 	*pagelen = len;
7307 
7308 	/*
7309 	 * Set-up Internal STANDBY command(s)
7310 	 */
7311 	if (page->standby == 0)
7312 		goto out;
7313 
7314 	ata_count = sata_get_standby_timer(page->standby_cond_timer);
7315 
7316 	scmd->satacmd_addr_type = 0;
7317 	scmd->satacmd_sec_count_lsb = ata_count;
7318 	scmd->satacmd_lba_low_lsb = 0;
7319 	scmd->satacmd_lba_mid_lsb = 0;
7320 	scmd->satacmd_lba_high_lsb = 0;
7321 	scmd->satacmd_features_reg = 0;
7322 	scmd->satacmd_device_reg = 0;
7323 	scmd->satacmd_status_reg = 0;
7324 	scmd->satacmd_cmd_reg = SATAC_STANDBY;
7325 	scmd->satacmd_flags.sata_copy_out_error_reg = B_TRUE;
7326 
7327 	/* Transfer command to HBA */
7328 	if (sata_hba_start(spx, rval) != 0) {
7329 		return (SATA_FAILURE);
7330 	} else {
7331 		if ((scmd->satacmd_error_reg != 0) ||
7332 		    (spx->txlt_sata_pkt->satapkt_reason !=
7333 		    SATA_PKT_COMPLETED)) {
7334 			sata_xlate_errors(spx);
7335 			return (SATA_FAILURE);
7336 		}
7337 	}
7338 
7339 	for (i = 0; i < 4; i++) {
7340 		sdinfo->satadrv_standby_timer[i] = page->standby_cond_timer[i];
7341 	}
7342 out:
7343 	*dmod = 1;
7344 	return (SATA_SUCCESS);
7345 }
7346 
7347 /*
7348  * sata_build_lsense_page0() is used to create the
7349  * SCSI LOG SENSE page 0 (supported log pages)
7350  *
7351  * Currently supported pages are 0, 0x10, 0x2f, 0x30 and 0x0e
7352  * (supported log pages, self-test results, informational exceptions
7353  * Sun vendor specific ATA SMART data, and start stop cycle counter).
7354  *
7355  * Takes a sata_drive_info t * and the address of a buffer
7356  * in which to create the page information.
7357  *
7358  * Returns the number of bytes valid in the buffer.
7359  */
7360 static	int
7361 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf)
7362 {
7363 	struct log_parameter *lpp = (struct log_parameter *)buf;
7364 	uint8_t *page_ptr = (uint8_t *)lpp->param_values;
7365 	int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */
7366 	sata_id_t *sata_id = &sdinfo->satadrv_id;
7367 
7368 	lpp->param_code[0] = 0;
7369 	lpp->param_code[1] = 0;
7370 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
7371 	*page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES;
7372 
7373 	if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
7374 		if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) {
7375 			*page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS;
7376 			++num_pages_supported;
7377 		}
7378 		*page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS;
7379 		++num_pages_supported;
7380 		*page_ptr++ = PAGE_CODE_SMART_READ_DATA;
7381 		++num_pages_supported;
7382 		*page_ptr++ = PAGE_CODE_START_STOP_CYCLE_COUNTER;
7383 		++num_pages_supported;
7384 	}
7385 
7386 	lpp->param_len = num_pages_supported;
7387 
7388 	return ((&lpp->param_values[0] - (uint8_t *)lpp) +
7389 	    num_pages_supported);
7390 }
7391 
7392 /*
7393  * sata_build_lsense_page_10() is used to create the
7394  * SCSI LOG SENSE page 0x10 (self-test results)
7395  *
7396  * Takes a sata_drive_info t * and the address of a buffer
7397  * in which to create the page information as well as a sata_hba_inst_t *.
7398  *
7399  * Returns the number of bytes valid in the buffer.
7400  *
7401  * Note: Self test and SMART data is accessible in device log pages.
7402  * The log pages can be accessed by SMART READ/WRITE LOG (up to 255 sectors
7403  * of data can be transferred by a single command), or by the General Purpose
7404  * Logging commands (GPL) READ LOG EXT and WRITE LOG EXT (up to 65,535 sectors
7405  * - approximately 33MB - can be transferred by a single command.
7406  * The SCT Command response (either error or command) is the same for both
7407  * the SMART and GPL methods of issuing commands.
7408  * This function uses READ LOG EXT command when drive supports LBA48, and
7409  * SMART READ command otherwise.
7410  *
7411  * Since above commands are executed in a synchronous mode, this function
7412  * should not be called in an interrupt context.
7413  */
7414 static	int
7415 sata_build_lsense_page_10(
7416 	sata_drive_info_t *sdinfo,
7417 	uint8_t *buf,
7418 	sata_hba_inst_t *sata_hba_inst)
7419 {
7420 	struct log_parameter *lpp = (struct log_parameter *)buf;
7421 	int rval;
7422 
7423 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
7424 		struct smart_ext_selftest_log *ext_selftest_log;
7425 
7426 		ext_selftest_log = kmem_zalloc(
7427 		    sizeof (struct smart_ext_selftest_log), KM_SLEEP);
7428 
7429 		rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo,
7430 		    ext_selftest_log, 0);
7431 		if (rval == 0) {
7432 			int index, start_index;
7433 			struct smart_ext_selftest_log_entry *entry;
7434 			static const struct smart_ext_selftest_log_entry empty =
7435 			    {0};
7436 			uint16_t block_num;
7437 			int count;
7438 			boolean_t only_one_block = B_FALSE;
7439 
7440 			index = ext_selftest_log->
7441 			    smart_ext_selftest_log_index[0];
7442 			index |= ext_selftest_log->
7443 			    smart_ext_selftest_log_index[1] << 8;
7444 			if (index == 0)
7445 				goto out;
7446 
7447 			--index;	/* Correct for 0 origin */
7448 			start_index = index;	/* remember where we started */
7449 			block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
7450 			if (block_num != 0) {
7451 				rval = sata_ext_smart_selftest_read_log(
7452 				    sata_hba_inst, sdinfo, ext_selftest_log,
7453 				    block_num);
7454 				if (rval != 0)
7455 					goto out;
7456 			}
7457 			index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
7458 			entry =
7459 			    &ext_selftest_log->
7460 			    smart_ext_selftest_log_entries[index];
7461 
7462 			for (count = 1;
7463 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
7464 			    ++count) {
7465 				uint8_t status;
7466 				uint8_t code;
7467 				uint8_t sense_key;
7468 				uint8_t add_sense_code;
7469 				uint8_t add_sense_code_qual;
7470 
7471 				/* If this is an unused entry, we are done */
7472 				if (bcmp(entry, &empty, sizeof (empty)) == 0) {
7473 					/* Broken firmware on some disks */
7474 					if (index + 1 ==
7475 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK) {
7476 						--entry;
7477 						--index;
7478 						if (bcmp(entry, &empty,
7479 						    sizeof (empty)) == 0)
7480 							goto out;
7481 					} else
7482 						goto out;
7483 				}
7484 
7485 				if (only_one_block &&
7486 				    start_index == index)
7487 					goto out;
7488 
7489 				lpp->param_code[0] = 0;
7490 				lpp->param_code[1] = count;
7491 				lpp->param_ctrl_flags =
7492 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
7493 				lpp->param_len =
7494 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
7495 
7496 				status = entry->smart_ext_selftest_log_status;
7497 				status >>= 4;
7498 				switch (status) {
7499 				case 0:
7500 				default:
7501 					sense_key = KEY_NO_SENSE;
7502 					add_sense_code =
7503 					    SD_SCSI_ASC_NO_ADD_SENSE;
7504 					add_sense_code_qual = 0;
7505 					break;
7506 				case 1:
7507 					sense_key = KEY_ABORTED_COMMAND;
7508 					add_sense_code =
7509 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7510 					add_sense_code_qual = SCSI_COMPONENT_81;
7511 					break;
7512 				case 2:
7513 					sense_key = KEY_ABORTED_COMMAND;
7514 					add_sense_code =
7515 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7516 					add_sense_code_qual = SCSI_COMPONENT_82;
7517 					break;
7518 				case 3:
7519 					sense_key = KEY_ABORTED_COMMAND;
7520 					add_sense_code =
7521 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7522 					add_sense_code_qual = SCSI_COMPONENT_83;
7523 					break;
7524 				case 4:
7525 					sense_key = KEY_HARDWARE_ERROR;
7526 					add_sense_code =
7527 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7528 					add_sense_code_qual = SCSI_COMPONENT_84;
7529 					break;
7530 				case 5:
7531 					sense_key = KEY_HARDWARE_ERROR;
7532 					add_sense_code =
7533 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7534 					add_sense_code_qual = SCSI_COMPONENT_85;
7535 					break;
7536 				case 6:
7537 					sense_key = KEY_HARDWARE_ERROR;
7538 					add_sense_code =
7539 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7540 					add_sense_code_qual = SCSI_COMPONENT_86;
7541 					break;
7542 				case 7:
7543 					sense_key = KEY_MEDIUM_ERROR;
7544 					add_sense_code =
7545 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7546 					add_sense_code_qual = SCSI_COMPONENT_87;
7547 					break;
7548 				case 8:
7549 					sense_key = KEY_HARDWARE_ERROR;
7550 					add_sense_code =
7551 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7552 					add_sense_code_qual = SCSI_COMPONENT_88;
7553 					break;
7554 				}
7555 				code = 0;	/* unspecified */
7556 				status |= (code << 4);
7557 				lpp->param_values[0] = status;
7558 				lpp->param_values[1] = 0; /* unspecified */
7559 				lpp->param_values[2] = entry->
7560 				    smart_ext_selftest_log_timestamp[1];
7561 				lpp->param_values[3] = entry->
7562 				    smart_ext_selftest_log_timestamp[0];
7563 				if (status != 0) {
7564 					lpp->param_values[4] = 0;
7565 					lpp->param_values[5] = 0;
7566 					lpp->param_values[6] = entry->
7567 					    smart_ext_selftest_log_failing_lba
7568 					    [5];
7569 					lpp->param_values[7] = entry->
7570 					    smart_ext_selftest_log_failing_lba
7571 					    [4];
7572 					lpp->param_values[8] = entry->
7573 					    smart_ext_selftest_log_failing_lba
7574 					    [3];
7575 					lpp->param_values[9] = entry->
7576 					    smart_ext_selftest_log_failing_lba
7577 					    [2];
7578 					lpp->param_values[10] = entry->
7579 					    smart_ext_selftest_log_failing_lba
7580 					    [1];
7581 					lpp->param_values[11] = entry->
7582 					    smart_ext_selftest_log_failing_lba
7583 					    [0];
7584 				} else {	/* No bad block address */
7585 					lpp->param_values[4] = 0xff;
7586 					lpp->param_values[5] = 0xff;
7587 					lpp->param_values[6] = 0xff;
7588 					lpp->param_values[7] = 0xff;
7589 					lpp->param_values[8] = 0xff;
7590 					lpp->param_values[9] = 0xff;
7591 					lpp->param_values[10] = 0xff;
7592 					lpp->param_values[11] = 0xff;
7593 				}
7594 
7595 				lpp->param_values[12] = sense_key;
7596 				lpp->param_values[13] = add_sense_code;
7597 				lpp->param_values[14] = add_sense_code_qual;
7598 				lpp->param_values[15] = 0; /* undefined */
7599 
7600 				lpp = (struct log_parameter *)
7601 				    (((uint8_t *)lpp) +
7602 				    SCSI_LOG_PARAM_HDR_LEN +
7603 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
7604 
7605 				--index;	/* Back up to previous entry */
7606 				if (index < 0) {
7607 					if (block_num > 0) {
7608 						--block_num;
7609 					} else {
7610 						struct read_log_ext_directory
7611 						    logdir;
7612 
7613 						rval =
7614 						    sata_read_log_ext_directory(
7615 						    sata_hba_inst, sdinfo,
7616 						    &logdir);
7617 						if (rval == -1)
7618 							goto out;
7619 						if ((logdir.read_log_ext_vers
7620 						    [0] == 0) &&
7621 						    (logdir.read_log_ext_vers
7622 						    [1] == 0))
7623 							goto out;
7624 						block_num =
7625 						    logdir.read_log_ext_nblks
7626 						    [EXT_SMART_SELFTEST_LOG_PAGE
7627 						    - 1][0];
7628 						block_num |= logdir.
7629 						    read_log_ext_nblks
7630 						    [EXT_SMART_SELFTEST_LOG_PAGE
7631 						    - 1][1] << 8;
7632 						--block_num;
7633 						only_one_block =
7634 						    (block_num == 0);
7635 					}
7636 					rval = sata_ext_smart_selftest_read_log(
7637 					    sata_hba_inst, sdinfo,
7638 					    ext_selftest_log, block_num);
7639 					if (rval != 0)
7640 						goto out;
7641 
7642 					index =
7643 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK -
7644 					    1;
7645 				}
7646 				index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
7647 				entry = &ext_selftest_log->
7648 				    smart_ext_selftest_log_entries[index];
7649 			}
7650 		}
7651 out:
7652 		kmem_free(ext_selftest_log,
7653 		    sizeof (struct smart_ext_selftest_log));
7654 	} else {
7655 		struct smart_selftest_log *selftest_log;
7656 
7657 		selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log),
7658 		    KM_SLEEP);
7659 
7660 		rval = sata_smart_selftest_log(sata_hba_inst, sdinfo,
7661 		    selftest_log);
7662 
7663 		if (rval == 0) {
7664 			int index;
7665 			int count;
7666 			struct smart_selftest_log_entry *entry;
7667 			static const struct smart_selftest_log_entry empty =
7668 			    { 0 };
7669 
7670 			index = selftest_log->smart_selftest_log_index;
7671 			if (index == 0)
7672 				goto done;
7673 			--index;	/* Correct for 0 origin */
7674 			entry = &selftest_log->
7675 			    smart_selftest_log_entries[index];
7676 			for (count = 1;
7677 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
7678 			    ++count) {
7679 				uint8_t status;
7680 				uint8_t code;
7681 				uint8_t sense_key;
7682 				uint8_t add_sense_code;
7683 				uint8_t add_sense_code_qual;
7684 
7685 				if (bcmp(entry, &empty, sizeof (empty)) == 0)
7686 					goto done;
7687 
7688 				lpp->param_code[0] = 0;
7689 				lpp->param_code[1] = count;
7690 				lpp->param_ctrl_flags =
7691 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
7692 				lpp->param_len =
7693 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
7694 
7695 				status = entry->smart_selftest_log_status;
7696 				status >>= 4;
7697 				switch (status) {
7698 				case 0:
7699 				default:
7700 					sense_key = KEY_NO_SENSE;
7701 					add_sense_code =
7702 					    SD_SCSI_ASC_NO_ADD_SENSE;
7703 					break;
7704 				case 1:
7705 					sense_key = KEY_ABORTED_COMMAND;
7706 					add_sense_code =
7707 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7708 					add_sense_code_qual = SCSI_COMPONENT_81;
7709 					break;
7710 				case 2:
7711 					sense_key = KEY_ABORTED_COMMAND;
7712 					add_sense_code =
7713 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7714 					add_sense_code_qual = SCSI_COMPONENT_82;
7715 					break;
7716 				case 3:
7717 					sense_key = KEY_ABORTED_COMMAND;
7718 					add_sense_code =
7719 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7720 					add_sense_code_qual = SCSI_COMPONENT_83;
7721 					break;
7722 				case 4:
7723 					sense_key = KEY_HARDWARE_ERROR;
7724 					add_sense_code =
7725 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7726 					add_sense_code_qual = SCSI_COMPONENT_84;
7727 					break;
7728 				case 5:
7729 					sense_key = KEY_HARDWARE_ERROR;
7730 					add_sense_code =
7731 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7732 					add_sense_code_qual = SCSI_COMPONENT_85;
7733 					break;
7734 				case 6:
7735 					sense_key = KEY_HARDWARE_ERROR;
7736 					add_sense_code =
7737 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7738 					add_sense_code_qual = SCSI_COMPONENT_86;
7739 					break;
7740 				case 7:
7741 					sense_key = KEY_MEDIUM_ERROR;
7742 					add_sense_code =
7743 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7744 					add_sense_code_qual = SCSI_COMPONENT_87;
7745 					break;
7746 				case 8:
7747 					sense_key = KEY_HARDWARE_ERROR;
7748 					add_sense_code =
7749 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7750 					add_sense_code_qual = SCSI_COMPONENT_88;
7751 					break;
7752 				}
7753 				code = 0;	/* unspecified */
7754 				status |= (code << 4);
7755 				lpp->param_values[0] = status;
7756 				lpp->param_values[1] = 0; /* unspecified */
7757 				lpp->param_values[2] = entry->
7758 				    smart_selftest_log_timestamp[1];
7759 				lpp->param_values[3] = entry->
7760 				    smart_selftest_log_timestamp[0];
7761 				if (status != 0) {
7762 					lpp->param_values[4] = 0;
7763 					lpp->param_values[5] = 0;
7764 					lpp->param_values[6] = 0;
7765 					lpp->param_values[7] = 0;
7766 					lpp->param_values[8] = entry->
7767 					    smart_selftest_log_failing_lba[3];
7768 					lpp->param_values[9] = entry->
7769 					    smart_selftest_log_failing_lba[2];
7770 					lpp->param_values[10] = entry->
7771 					    smart_selftest_log_failing_lba[1];
7772 					lpp->param_values[11] = entry->
7773 					    smart_selftest_log_failing_lba[0];
7774 				} else {	/* No block address */
7775 					lpp->param_values[4] = 0xff;
7776 					lpp->param_values[5] = 0xff;
7777 					lpp->param_values[6] = 0xff;
7778 					lpp->param_values[7] = 0xff;
7779 					lpp->param_values[8] = 0xff;
7780 					lpp->param_values[9] = 0xff;
7781 					lpp->param_values[10] = 0xff;
7782 					lpp->param_values[11] = 0xff;
7783 				}
7784 				lpp->param_values[12] = sense_key;
7785 				lpp->param_values[13] = add_sense_code;
7786 				lpp->param_values[14] = add_sense_code_qual;
7787 				lpp->param_values[15] = 0; /* undefined */
7788 
7789 				lpp = (struct log_parameter *)
7790 				    (((uint8_t *)lpp) +
7791 				    SCSI_LOG_PARAM_HDR_LEN +
7792 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
7793 				--index;	/* back up to previous entry */
7794 				if (index < 0) {
7795 					index =
7796 					    NUM_SMART_SELFTEST_LOG_ENTRIES - 1;
7797 				}
7798 				entry = &selftest_log->
7799 				    smart_selftest_log_entries[index];
7800 			}
7801 		}
7802 done:
7803 		kmem_free(selftest_log, sizeof (struct smart_selftest_log));
7804 	}
7805 
7806 	return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) *
7807 	    SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS);
7808 }
7809 
7810 /*
7811  * sata_build_lsense_page_2f() is used to create the
7812  * SCSI LOG SENSE page 0x2f (informational exceptions)
7813  *
7814  * Takes a sata_drive_info t * and the address of a buffer
7815  * in which to create the page information as well as a sata_hba_inst_t *.
7816  *
7817  * Returns the number of bytes valid in the buffer.
7818  *
7819  * Because it invokes function(s) that send synchronously executed command
7820  * to the HBA, it cannot be called in the interrupt context.
7821  */
7822 static	int
7823 sata_build_lsense_page_2f(
7824 	sata_drive_info_t *sdinfo,
7825 	uint8_t *buf,
7826 	sata_hba_inst_t *sata_hba_inst)
7827 {
7828 	struct log_parameter *lpp = (struct log_parameter *)buf;
7829 	int rval;
7830 	uint8_t *smart_data;
7831 	uint8_t temp;
7832 	sata_id_t *sata_id;
7833 #define	SMART_NO_TEMP	0xff
7834 
7835 	lpp->param_code[0] = 0;
7836 	lpp->param_code[1] = 0;
7837 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
7838 
7839 	/* Now get the SMART status w.r.t. threshold exceeded */
7840 	rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo);
7841 	switch (rval) {
7842 	case 1:
7843 		lpp->param_values[0] = SCSI_PREDICTED_FAILURE;
7844 		lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE;
7845 		break;
7846 	case 0:
7847 	case -1:	/* failed to get data */
7848 		lpp->param_values[0] = 0;	/* No failure predicted */
7849 		lpp->param_values[1] = 0;
7850 		break;
7851 #if defined(SATA_DEBUG)
7852 	default:
7853 		cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value");
7854 		/* NOTREACHED */
7855 #endif
7856 	}
7857 
7858 	sata_id = &sdinfo->satadrv_id;
7859 	if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP))
7860 		temp = SMART_NO_TEMP;
7861 	else {
7862 		/* Now get the temperature */
7863 		smart_data = kmem_zalloc(512, KM_SLEEP);
7864 		rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data,
7865 		    SCT_STATUS_LOG_PAGE, 1);
7866 		if (rval == -1)
7867 			temp = SMART_NO_TEMP;
7868 		else {
7869 			temp = smart_data[200];
7870 			if (temp & 0x80) {
7871 				if (temp & 0x7f)
7872 					temp = 0;
7873 				else
7874 					temp = SMART_NO_TEMP;
7875 			}
7876 		}
7877 		kmem_free(smart_data, 512);
7878 	}
7879 
7880 	lpp->param_values[2] = temp;	/* most recent temperature */
7881 	lpp->param_values[3] = 0;	/* required vendor specific byte */
7882 
7883 	lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN;
7884 
7885 
7886 	return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN);
7887 }
7888 
7889 /*
7890  * sata_build_lsense_page_30() is used to create the
7891  * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data).
7892  *
7893  * Takes a sata_drive_info t * and the address of a buffer
7894  * in which to create the page information as well as a sata_hba_inst_t *.
7895  *
7896  * Returns the number of bytes valid in the buffer.
7897  */
7898 static int
7899 sata_build_lsense_page_30(
7900 	sata_drive_info_t *sdinfo,
7901 	uint8_t *buf,
7902 	sata_hba_inst_t *sata_hba_inst)
7903 {
7904 	struct smart_data *smart_data = (struct smart_data *)buf;
7905 	int rval;
7906 
7907 	/* Now do the SMART READ DATA */
7908 	rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data);
7909 	if (rval == -1)
7910 		return (0);
7911 
7912 	return (sizeof (struct smart_data));
7913 }
7914 
7915 /*
7916  * sata_build_lsense_page_0e() is used to create the
7917  * SCSI LOG SENSE page 0e (start-stop cycle counter page)
7918  *
7919  * Date of Manufacture (0x0001)
7920  *	YEAR = "0000"
7921  *	WEEK = "00"
7922  * Accounting Date (0x0002)
7923  *	6 ASCII space character(20h)
7924  * Specified cycle count over device lifetime
7925  *	VALUE - THRESH - the delta between max and min;
7926  * Accumulated start-stop cycles
7927  *	VALUE - WORST - the accumulated cycles;
7928  *
7929  * ID FLAG THRESH VALUE WORST RAW on start/stop counter attribute
7930  *
7931  * Takes a sata_drive_info t * and the address of a buffer
7932  * in which to create the page information as well as a sata_hba_inst_t *.
7933  *
7934  * Returns the number of bytes valid in the buffer.
7935  */
7936 static	int
7937 sata_build_lsense_page_0e(sata_drive_info_t *sdinfo, uint8_t *buf,
7938 	sata_pkt_txlate_t *spx)
7939 {
7940 	struct start_stop_cycle_counter_log *log_page;
7941 	int i, rval, index;
7942 	uint8_t smart_data[512], id, value, worst, thresh;
7943 	uint32_t max_count, cycles;
7944 
7945 	/* Now do the SMART READ DATA */
7946 	rval = sata_fetch_smart_data(spx->txlt_sata_hba_inst, sdinfo,
7947 	    (struct smart_data *)smart_data);
7948 	if (rval == -1)
7949 		return (0);
7950 	for (i = 0, id = 0; i < SMART_START_STOP_COUNT_ID * 2; i++) {
7951 		index = (i * 12) + 2;
7952 		id = smart_data[index];
7953 		if (id != SMART_START_STOP_COUNT_ID)
7954 			continue;
7955 		else {
7956 			thresh = smart_data[index + 2];
7957 			value = smart_data[index + 3];
7958 			worst = smart_data[index + 4];
7959 			break;
7960 		}
7961 	}
7962 	if (id != SMART_START_STOP_COUNT_ID)
7963 		return (0);
7964 	max_count = value - thresh;
7965 	cycles = value - worst;
7966 
7967 	log_page = (struct start_stop_cycle_counter_log *)buf;
7968 	bzero(log_page, sizeof (struct start_stop_cycle_counter_log));
7969 	log_page->code = 0x0e;
7970 	log_page->page_len_low = 0x24;
7971 
7972 	log_page->manufactor_date_low = 0x1;
7973 	log_page->param_1.fmt_link = 0x1; /* 01b */
7974 	log_page->param_len_1 = 0x06;
7975 	for (i = 0; i < 4; i++) {
7976 		log_page->year_manu[i] = 0x30;
7977 		if (i < 2)
7978 			log_page->week_manu[i] = 0x30;
7979 	}
7980 
7981 	log_page->account_date_low = 0x02;
7982 	log_page->param_2.fmt_link = 0x01; /* 01b */
7983 	log_page->param_len_2 = 0x06;
7984 	for (i = 0; i < 4; i++) {
7985 		log_page->year_account[i] = 0x20;
7986 		if (i < 2)
7987 			log_page->week_account[i] = 0x20;
7988 	}
7989 
7990 	log_page->lifetime_code_low = 0x03;
7991 	log_page->param_3.fmt_link = 0x03; /* 11b */
7992 	log_page->param_len_3 = 0x04;
7993 	/* VALUE - THRESH - the delta between max and min */
7994 	log_page->cycle_code_low = 0x04;
7995 	log_page->param_4.fmt_link = 0x03; /* 11b */
7996 	log_page->param_len_4 = 0x04;
7997 	/* WORST - THRESH - the distance from 'now' to min */
7998 
7999 	for (i = 0; i < 4; i++) {
8000 		log_page->cycle_lifetime[i] =
8001 		    (max_count >> (8 * (3 - i))) & 0xff;
8002 		log_page->cycle_accumulated[i] =
8003 		    (cycles >> (8 * (3 - i))) & 0xff;
8004 	}
8005 
8006 	return (sizeof (struct start_stop_cycle_counter_log));
8007 }
8008 
8009 /*
8010  * This function was used for build a ATA read verify sector command
8011  */
8012 static void
8013 sata_build_read_verify_cmd(sata_cmd_t *scmd, uint16_t sec, uint64_t lba)
8014 {
8015 	scmd->satacmd_cmd_reg = SATAC_RDVER;
8016 	scmd->satacmd_addr_type = ATA_ADDR_LBA28;
8017 
8018 	scmd->satacmd_sec_count_lsb = sec & 0xff;
8019 	scmd->satacmd_lba_low_lsb = lba & 0xff;
8020 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
8021 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
8022 	scmd->satacmd_device_reg = (SATA_ADH_LBA | (lba >> 24) & 0xf);
8023 	scmd->satacmd_features_reg = 0;
8024 	scmd->satacmd_status_reg = 0;
8025 	scmd->satacmd_error_reg = 0;
8026 }
8027 
8028 /*
8029  * This function was used for building an ATA
8030  * command, and only command register need to
8031  * be defined, other register will be zero or na.
8032  */
8033 static void
8034 sata_build_generic_cmd(sata_cmd_t *scmd, uint8_t cmd)
8035 {
8036 	scmd->satacmd_addr_type = 0;
8037 	scmd->satacmd_cmd_reg = cmd;
8038 	scmd->satacmd_device_reg = 0;
8039 	scmd->satacmd_sec_count_lsb = 0;
8040 	scmd->satacmd_lba_low_lsb = 0;
8041 	scmd->satacmd_lba_mid_lsb = 0;
8042 	scmd->satacmd_lba_high_lsb = 0;
8043 	scmd->satacmd_features_reg = 0;
8044 	scmd->satacmd_status_reg = 0;
8045 	scmd->satacmd_error_reg = 0;
8046 }
8047 
8048 /*
8049  * This function was used for changing the standby
8050  * timer format from SCSI to ATA.
8051  */
8052 static uint8_t
8053 sata_get_standby_timer(uint8_t *timer)
8054 {
8055 	uint32_t i = 0, count = 0;
8056 	uint8_t ata_count;
8057 
8058 	for (i = 0; i < 4; i++) {
8059 		count = count << 8 | timer[i];
8060 	}
8061 
8062 	if (count == 0)
8063 		return (0);
8064 
8065 	if (count >= 1 && count <= 12000)
8066 		ata_count = (count -1) / 50 + 1;
8067 	else if (count > 12000 && count <= 12600)
8068 		ata_count = 0xfc;
8069 	else if (count > 12601 && count <= 12750)
8070 		ata_count = 0xff;
8071 	else if (count > 12750 && count <= 17999)
8072 		ata_count = 0xf1;
8073 	else if (count > 18000 && count <= 198000)
8074 		ata_count = count / 18000 + 240;
8075 	else
8076 		ata_count = 0xfd;
8077 	return (ata_count);
8078 }
8079 
8080 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */
8081 
8082 /*
8083  * Start command for ATAPI device.
8084  * This function processes scsi_pkt requests.
8085  * Now CD/DVD, tape and ATAPI disk devices are supported.
8086  * Most commands are packet without any translation into Packet Command.
8087  * Some may be trapped and executed as SATA commands (not clear which one).
8088  *
8089  * Returns TRAN_ACCEPT if command is accepted for execution (or completed
8090  * execution).
8091  * Returns other TRAN_XXXX codes if command is not accepted or completed
8092  * (see return values for sata_hba_start()).
8093  *
8094  * Note:
8095  * Inquiry cdb format differs between transport version 2 and 3.
8096  * However, the transport version 3 devices that were checked did not adhere
8097  * to the specification (ignored MSB of the allocation length). Therefore,
8098  * the transport version is not checked, but Inquiry allocation length is
8099  * truncated to 255 bytes if the original allocation length set-up by the
8100  * target driver is greater than 255 bytes.
8101  */
8102 static int
8103 sata_txlt_atapi(sata_pkt_txlate_t *spx)
8104 {
8105 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8106 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
8107 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
8108 	sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx);
8109 	sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba,
8110 	    &spx->txlt_sata_pkt->satapkt_device);
8111 	int cport = SATA_TXLT_CPORT(spx);
8112 	int cdblen;
8113 	int rval, reason;
8114 	int synch;
8115 	union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp;
8116 
8117 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
8118 
8119 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
8120 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
8121 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
8122 		return (rval);
8123 	}
8124 
8125 	/*
8126 	 * ATAPI device executes some ATA commands in addition to those
8127 	 * commands sent via PACKET command. These ATA commands may be
8128 	 * executed by the regular SATA translation functions. None needs
8129 	 * to be captured now.
8130 	 *
8131 	 * Commands sent via PACKET command include:
8132 	 *	MMC command set for ATAPI CD/DVD device
8133 	 *	SSC command set for ATAPI TAPE device
8134 	 *	SBC command set for ATAPI disk device
8135 	 *
8136 	 */
8137 
8138 	/* Check the size of cdb */
8139 	cdblen = scsi_cdb_size[GETGROUP(cdbp)];
8140 	if (cdblen > sdinfo->satadrv_atapi_cdb_len) {
8141 		sata_log(NULL, CE_WARN,
8142 		    "sata: invalid ATAPI cdb length %d",
8143 		    scsipkt->pkt_cdblen);
8144 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
8145 		return (TRAN_BADPKT);
8146 	}
8147 
8148 	SATAATAPITRACE(spx, cdblen);
8149 
8150 	/*
8151 	 * For non-read/write commands we need to
8152 	 * map buffer
8153 	 */
8154 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
8155 	case SCMD_READ:
8156 	case SCMD_READ_G1:
8157 	case SCMD_READ_G5:
8158 	case SCMD_READ_G4:
8159 	case SCMD_WRITE:
8160 	case SCMD_WRITE_G1:
8161 	case SCMD_WRITE_G5:
8162 	case SCMD_WRITE_G4:
8163 		break;
8164 	default:
8165 		if (bp != NULL) {
8166 			if (bp->b_flags & (B_PHYS | B_PAGEIO))
8167 				bp_mapin(bp);
8168 		}
8169 		break;
8170 	}
8171 	/*
8172 	 * scmd->satacmd_flags.sata_data_direction default -
8173 	 * SATA_DIR_NODATA_XFER - is set by
8174 	 * sata_txlt_generic_pkt_info().
8175 	 */
8176 	if (scmd->satacmd_bp) {
8177 		if (scmd->satacmd_bp->b_flags & B_READ) {
8178 			scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
8179 		} else {
8180 			scmd->satacmd_flags.sata_data_direction =
8181 			    SATA_DIR_WRITE;
8182 		}
8183 	}
8184 
8185 	/*
8186 	 * Set up ATAPI packet command.
8187 	 */
8188 
8189 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
8190 
8191 	/* Copy cdb into sata_cmd */
8192 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
8193 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
8194 	bcopy(cdbp, scmd->satacmd_acdb, cdblen);
8195 
8196 	/* See note in the command header */
8197 	if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) {
8198 		if (scmd->satacmd_acdb[3] != 0)
8199 			scmd->satacmd_acdb[4] = 255;
8200 	}
8201 
8202 #ifdef SATA_DEBUG
8203 	if (sata_debug_flags & SATA_DBG_ATAPI) {
8204 		uint8_t *p = scmd->satacmd_acdb;
8205 		char buf[3 * SATA_ATAPI_MAX_CDB_LEN];
8206 
8207 		(void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN,
8208 		    "%02x %02x %02x %02x %02x %02x %02x %02x "
8209 		    "%2x %02x %02x %02x %02x %02x %02x %02x",
8210 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
8211 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
8212 		buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0';
8213 		cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf);
8214 	}
8215 #endif
8216 
8217 	/*
8218 	 * Preset request sense data to NO SENSE.
8219 	 * If there is no way to get error information via Request Sense,
8220 	 * the packet request sense data would not have to be modified by HBA,
8221 	 * but it could be returned as is.
8222 	 */
8223 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
8224 	sata_fixed_sense_data_preset(
8225 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
8226 
8227 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
8228 		/* Need callback function */
8229 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion;
8230 		synch = FALSE;
8231 	} else
8232 		synch = TRUE;
8233 
8234 	/* Transfer command to HBA */
8235 	if (sata_hba_start(spx, &rval) != 0) {
8236 		/* Pkt not accepted for execution */
8237 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
8238 		return (rval);
8239 	}
8240 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
8241 	/*
8242 	 * If execution is non-synchronous,
8243 	 * a callback function will handle potential errors, translate
8244 	 * the response and will do a callback to a target driver.
8245 	 * If it was synchronous, use the same framework callback to check
8246 	 * an execution status.
8247 	 */
8248 	if (synch) {
8249 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
8250 		    "synchronous execution status %x\n",
8251 		    spx->txlt_sata_pkt->satapkt_reason);
8252 		sata_txlt_atapi_completion(spx->txlt_sata_pkt);
8253 	}
8254 	return (TRAN_ACCEPT);
8255 }
8256 
8257 
8258 /*
8259  * ATAPI Packet command completion.
8260  *
8261  * Failure of the command passed via Packet command are considered device
8262  * error. SATA HBA driver would have to retrieve error data (via Request
8263  * Sense command delivered via error retrieval sata packet) and copy it
8264  * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data.
8265  */
8266 static void
8267 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt)
8268 {
8269 	sata_pkt_txlate_t *spx =
8270 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
8271 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8272 	struct scsi_extended_sense *sense;
8273 	struct buf *bp;
8274 	int rval;
8275 
8276 #ifdef SATA_DEBUG
8277 	uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense;
8278 #endif
8279 
8280 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
8281 	    STATE_SENT_CMD | STATE_GOT_STATUS;
8282 
8283 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
8284 		/* Normal completion */
8285 		if (sata_pkt->satapkt_cmd.satacmd_bp != NULL)
8286 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
8287 		scsipkt->pkt_reason = CMD_CMPLT;
8288 		*scsipkt->pkt_scbp = STATUS_GOOD;
8289 		if (spx->txlt_tmp_buf != NULL) {
8290 			/* Temporary buffer was used */
8291 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
8292 			if (bp->b_flags & B_READ) {
8293 				rval = ddi_dma_sync(
8294 				    spx->txlt_buf_dma_handle, 0, 0,
8295 				    DDI_DMA_SYNC_FORCPU);
8296 				ASSERT(rval == DDI_SUCCESS);
8297 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
8298 				    bp->b_bcount);
8299 			}
8300 		}
8301 	} else {
8302 		/*
8303 		 * Something went wrong - analyze return
8304 		 */
8305 		*scsipkt->pkt_scbp = STATUS_CHECK;
8306 		sense = sata_arq_sense(spx);
8307 
8308 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
8309 			/*
8310 			 * pkt_reason should be CMD_CMPLT for DEVICE ERROR.
8311 			 * Under this condition ERR bit is set for ATA command,
8312 			 * and CHK bit set for ATAPI command.
8313 			 *
8314 			 * Please check st_intr & sdintr about how pkt_reason
8315 			 * is used.
8316 			 */
8317 			scsipkt->pkt_reason = CMD_CMPLT;
8318 
8319 			/*
8320 			 * We may not have ARQ data if there was a double
8321 			 * error. But sense data in sata packet was pre-set
8322 			 * with NO SENSE so it is valid even if HBA could
8323 			 * not retrieve a real sense data.
8324 			 * Just copy this sense data into scsi pkt sense area.
8325 			 */
8326 			bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense,
8327 			    SATA_ATAPI_MIN_RQSENSE_LEN);
8328 #ifdef SATA_DEBUG
8329 			if (sata_debug_flags & SATA_DBG_SCSI_IF) {
8330 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
8331 				    "sata_txlt_atapi_completion: %02x\n"
8332 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
8333 				    "          %02x %02x %02x %02x %02x %02x "
8334 				    "          %02x %02x %02x %02x %02x %02x\n",
8335 				    scsipkt->pkt_reason,
8336 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
8337 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
8338 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
8339 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
8340 				    rqsp[16], rqsp[17]);
8341 			}
8342 #endif
8343 		} else {
8344 			switch (sata_pkt->satapkt_reason) {
8345 			case SATA_PKT_PORT_ERROR:
8346 				/*
8347 				 * We have no device data.
8348 				 */
8349 				scsipkt->pkt_reason = CMD_INCOMPLETE;
8350 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
8351 				    STATE_GOT_TARGET | STATE_SENT_CMD |
8352 				    STATE_GOT_STATUS);
8353 				sense->es_key = KEY_HARDWARE_ERROR;
8354 				break;
8355 
8356 			case SATA_PKT_TIMEOUT:
8357 				scsipkt->pkt_reason = CMD_TIMEOUT;
8358 				scsipkt->pkt_statistics |=
8359 				    STAT_TIMEOUT | STAT_DEV_RESET;
8360 				/*
8361 				 * Need to check if HARDWARE_ERROR/
8362 				 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more
8363 				 * appropriate.
8364 				 */
8365 				break;
8366 
8367 			case SATA_PKT_ABORTED:
8368 				scsipkt->pkt_reason = CMD_ABORTED;
8369 				scsipkt->pkt_statistics |= STAT_ABORTED;
8370 				/* Should we set key COMMAND_ABPRTED? */
8371 				break;
8372 
8373 			case SATA_PKT_RESET:
8374 				scsipkt->pkt_reason = CMD_RESET;
8375 				scsipkt->pkt_statistics |= STAT_DEV_RESET;
8376 				/*
8377 				 * May be we should set Unit Attention /
8378 				 * Reset. Perhaps the same should be
8379 				 * returned for disks....
8380 				 */
8381 				sense->es_key = KEY_UNIT_ATTENTION;
8382 				sense->es_add_code = SD_SCSI_ASC_RESET;
8383 				break;
8384 
8385 			default:
8386 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
8387 				    "sata_txlt_atapi_completion: "
8388 				    "invalid packet completion reason"));
8389 				scsipkt->pkt_reason = CMD_TRAN_ERR;
8390 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
8391 				    STATE_GOT_TARGET | STATE_SENT_CMD |
8392 				    STATE_GOT_STATUS);
8393 				break;
8394 			}
8395 		}
8396 	}
8397 
8398 	SATAATAPITRACE(spx, 0);
8399 
8400 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
8401 	    scsipkt->pkt_comp != NULL) {
8402 		/* scsi callback required */
8403 		(*scsipkt->pkt_comp)(scsipkt);
8404 	}
8405 }
8406 
8407 /*
8408  * Set up error retrieval sata command for ATAPI Packet Command error data
8409  * recovery.
8410  *
8411  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
8412  * returns SATA_FAILURE otherwise.
8413  */
8414 
8415 static int
8416 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
8417 {
8418 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
8419 	sata_cmd_t *scmd;
8420 	struct buf *bp;
8421 
8422 	/*
8423 	 * Allocate dma-able buffer error data.
8424 	 * Buffer allocation will take care of buffer alignment and other DMA
8425 	 * attributes.
8426 	 */
8427 	bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN);
8428 	if (bp == NULL) {
8429 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
8430 		    "sata_get_err_retrieval_pkt: "
8431 		    "cannot allocate buffer for error data", NULL);
8432 		return (SATA_FAILURE);
8433 	}
8434 	bp_mapin(bp); /* make data buffer accessible */
8435 
8436 	/* Operation modes are up to the caller */
8437 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
8438 
8439 	/* Synchronous mode, no callback - may be changed by the caller */
8440 	spkt->satapkt_comp = NULL;
8441 	spkt->satapkt_time = sata_default_pkt_time;
8442 
8443 	scmd = &spkt->satapkt_cmd;
8444 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
8445 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
8446 
8447 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
8448 
8449 	/*
8450 	 * Set-up acdb. Request Sense CDB (packet command content) is
8451 	 * not in DMA-able buffer. Its handling is HBA-specific (how
8452 	 * it is transfered into packet FIS).
8453 	 */
8454 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
8455 	bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN);
8456 	/* Following zeroing of pad bytes may not be necessary */
8457 	bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN],
8458 	    sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN);
8459 
8460 	/*
8461 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
8462 	 * before accessing it. Handle is in usual place in translate struct.
8463 	 */
8464 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
8465 
8466 	/*
8467 	 * Preset request sense data to NO SENSE.
8468 	 * Here it is redundant, only for a symetry with scsi-originated
8469 	 * packets. It should not be used for anything but debugging.
8470 	 */
8471 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
8472 	sata_fixed_sense_data_preset(
8473 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
8474 
8475 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
8476 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
8477 
8478 	return (SATA_SUCCESS);
8479 }
8480 
8481 /*
8482  * Set-up ATAPI packet command.
8483  * Data transfer direction has to be set-up in sata_cmd structure prior to
8484  * calling this function.
8485  *
8486  * Returns void
8487  */
8488 
8489 static void
8490 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo)
8491 {
8492 	scmd->satacmd_addr_type = 0;		/* N/A */
8493 	scmd->satacmd_sec_count_lsb = 0;	/* no tag */
8494 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
8495 	scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ;
8496 	scmd->satacmd_lba_high_lsb =
8497 	    (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8);
8498 	scmd->satacmd_cmd_reg = SATAC_PACKET;	/* Command */
8499 
8500 	/*
8501 	 * We want all data to be transfered via DMA.
8502 	 * But specify it only if drive supports DMA and DMA mode is
8503 	 * selected - some drives are sensitive about it.
8504 	 * Hopefully it wil work for all drives....
8505 	 */
8506 	if (sdinfo->satadrv_settings & SATA_DEV_DMA)
8507 		scmd->satacmd_features_reg = SATA_ATAPI_F_DMA;
8508 
8509 	/*
8510 	 * Features register requires special care for devices that use
8511 	 * Serial ATA bridge - they need an explicit specification of
8512 	 * the data transfer direction for Packet DMA commands.
8513 	 * Setting this bit is harmless if DMA is not used.
8514 	 *
8515 	 * Many drives do not implement word 80, specifying what ATA/ATAPI
8516 	 * spec they follow.
8517 	 * We are arbitrarily following the latest SerialATA 2.6 spec,
8518 	 * which uses ATA/ATAPI 6 specification for Identify Data, unless
8519 	 * ATA/ATAPI-7 support is explicitly indicated.
8520 	 */
8521 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
8522 	    sdinfo->satadrv_id.ai_majorversion != 0xffff &&
8523 	    (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) {
8524 		/*
8525 		 * Specification of major version is valid and version 7
8526 		 * is supported. It does automatically imply that all
8527 		 * spec features are supported. For now, we assume that
8528 		 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete.
8529 		 */
8530 		if ((sdinfo->satadrv_id.ai_dirdma &
8531 		    SATA_ATAPI_ID_DMADIR_REQ) != 0) {
8532 			if (scmd->satacmd_flags.sata_data_direction ==
8533 			    SATA_DIR_READ)
8534 			scmd->satacmd_features_reg |=
8535 			    SATA_ATAPI_F_DATA_DIR_READ;
8536 		}
8537 	}
8538 }
8539 
8540 
8541 #ifdef SATA_DEBUG
8542 
8543 /* Display 18 bytes of Inquiry data */
8544 static void
8545 sata_show_inqry_data(uint8_t *buf)
8546 {
8547 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
8548 	uint8_t *p;
8549 
8550 	cmn_err(CE_NOTE, "Inquiry data:");
8551 	cmn_err(CE_NOTE, "device type %x", inq->inq_dtype);
8552 	cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb);
8553 	cmn_err(CE_NOTE, "version %x", inq->inq_ansi);
8554 	cmn_err(CE_NOTE, "ATAPI transport version %d",
8555 	    SATA_ATAPI_TRANS_VERSION(inq));
8556 	cmn_err(CE_NOTE, "response data format %d, aenc %d",
8557 	    inq->inq_rdf, inq->inq_aenc);
8558 	cmn_err(CE_NOTE, " additional length %d", inq->inq_len);
8559 	cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs);
8560 	p = (uint8_t *)inq->inq_vid;
8561 	cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x "
8562 	    "%02x %02x %02x %02x",
8563 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
8564 	p = (uint8_t *)inq->inq_vid;
8565 	cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c",
8566 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
8567 
8568 	p = (uint8_t *)inq->inq_pid;
8569 	cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x "
8570 	    "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
8571 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
8572 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
8573 	p = (uint8_t *)inq->inq_pid;
8574 	cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c "
8575 	    "%c %c %c %c %c %c %c %c",
8576 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
8577 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
8578 
8579 	p = (uint8_t *)inq->inq_revision;
8580 	cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x",
8581 	    p[0], p[1], p[2], p[3]);
8582 	p = (uint8_t *)inq->inq_revision;
8583 	cmn_err(CE_NOTE, "revision: %c %c %c %c",
8584 	    p[0], p[1], p[2], p[3]);
8585 
8586 }
8587 
8588 
8589 static void
8590 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count)
8591 {
8592 	struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt;
8593 
8594 	if (scsi_pkt == NULL)
8595 		return;
8596 	if (count != 0) {
8597 		/* saving cdb */
8598 		bzero(sata_atapi_trace[sata_atapi_trace_index].acdb,
8599 		    SATA_ATAPI_MAX_CDB_LEN);
8600 		bcopy(scsi_pkt->pkt_cdbp,
8601 		    sata_atapi_trace[sata_atapi_trace_index].acdb, count);
8602 	} else {
8603 		bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)->
8604 		    sts_sensedata,
8605 		    sata_atapi_trace[sata_atapi_trace_index].arqs,
8606 		    SATA_ATAPI_MIN_RQSENSE_LEN);
8607 		sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason =
8608 		    scsi_pkt->pkt_reason;
8609 		sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason =
8610 		    spx->txlt_sata_pkt->satapkt_reason;
8611 
8612 		if (++sata_atapi_trace_index >= 64)
8613 			sata_atapi_trace_index = 0;
8614 	}
8615 }
8616 
8617 #endif
8618 
8619 /*
8620  * Fetch inquiry data from ATAPI device
8621  * Returns SATA_SUCCESS if operation was successfull, SATA_FAILURE otherwise.
8622  *
8623  * Note:
8624  * inqb pointer does not point to a DMA-able buffer. It is a local buffer
8625  * where the caller expects to see the inquiry data.
8626  *
8627  */
8628 
8629 static int
8630 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba,
8631     sata_address_t *saddr, struct scsi_inquiry *inq)
8632 {
8633 	sata_pkt_txlate_t *spx;
8634 	sata_pkt_t *spkt;
8635 	struct buf *bp;
8636 	sata_drive_info_t *sdinfo;
8637 	sata_cmd_t *scmd;
8638 	int rval;
8639 	uint8_t *rqsp;
8640 #ifdef SATA_DEBUG
8641 	char msg_buf[MAXPATHLEN];
8642 #endif
8643 
8644 	ASSERT(sata_hba != NULL);
8645 
8646 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
8647 	spx->txlt_sata_hba_inst = sata_hba;
8648 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
8649 	spkt = sata_pkt_alloc(spx, NULL);
8650 	if (spkt == NULL) {
8651 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
8652 		return (SATA_FAILURE);
8653 	}
8654 	/* address is needed now */
8655 	spkt->satapkt_device.satadev_addr = *saddr;
8656 
8657 	/* scsi_inquiry size buffer */
8658 	bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry));
8659 	if (bp == NULL) {
8660 		sata_pkt_free(spx);
8661 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
8662 		SATA_LOG_D((sata_hba, CE_WARN,
8663 		    "sata_get_atapi_inquiry_data: "
8664 		    "cannot allocate data buffer"));
8665 		return (SATA_FAILURE);
8666 	}
8667 	bp_mapin(bp); /* make data buffer accessible */
8668 
8669 	scmd = &spkt->satapkt_cmd;
8670 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
8671 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
8672 
8673 	/* Use synchronous mode */
8674 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
8675 	spkt->satapkt_comp = NULL;
8676 	spkt->satapkt_time = sata_default_pkt_time;
8677 
8678 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
8679 
8680 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
8681 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
8682 
8683 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
8684 	sdinfo = sata_get_device_info(sata_hba,
8685 	    &spx->txlt_sata_pkt->satapkt_device);
8686 	if (sdinfo == NULL) {
8687 		/* we have to be carefull about the disapearing device */
8688 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
8689 		rval = SATA_FAILURE;
8690 		goto cleanup;
8691 	}
8692 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
8693 
8694 	/*
8695 	 * Set-up acdb. This works for atapi transport version 2 and later.
8696 	 */
8697 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
8698 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
8699 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
8700 	scmd->satacmd_acdb[1] = 0x00;
8701 	scmd->satacmd_acdb[2] = 0x00;
8702 	scmd->satacmd_acdb[3] = 0x00;
8703 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
8704 	scmd->satacmd_acdb[5] = 0x00;
8705 
8706 	sata_fixed_sense_data_preset(
8707 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
8708 
8709 	/* Transfer command to HBA */
8710 	if (sata_hba_start(spx, &rval) != 0) {
8711 		/* Pkt not accepted for execution */
8712 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
8713 		    "sata_get_atapi_inquiry_data: "
8714 		    "Packet not accepted for execution - ret: %02x", rval);
8715 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
8716 		rval = SATA_FAILURE;
8717 		goto cleanup;
8718 	}
8719 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
8720 
8721 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
8722 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
8723 		    "sata_get_atapi_inquiry_data: "
8724 		    "Packet completed successfully - ret: %02x", rval);
8725 		if (spx->txlt_buf_dma_handle != NULL) {
8726 			/*
8727 			 * Sync buffer. Handle is in usual place in translate
8728 			 * struct.
8729 			 */
8730 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
8731 			    DDI_DMA_SYNC_FORCPU);
8732 			ASSERT(rval == DDI_SUCCESS);
8733 		}
8734 		/*
8735 		 * Normal completion - copy data into caller's buffer
8736 		 */
8737 		bcopy(bp->b_un.b_addr, (uint8_t *)inq,
8738 		    sizeof (struct scsi_inquiry));
8739 #ifdef SATA_DEBUG
8740 		if (sata_debug_flags & SATA_DBG_ATAPI) {
8741 			sata_show_inqry_data((uint8_t *)inq);
8742 		}
8743 #endif
8744 		rval = SATA_SUCCESS;
8745 	} else {
8746 		/*
8747 		 * Something went wrong - analyze return - check rqsense data
8748 		 */
8749 		rval = SATA_FAILURE;
8750 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
8751 			/*
8752 			 * ARQ data hopefull show something other than NO SENSE
8753 			 */
8754 			rqsp = scmd->satacmd_rqsense;
8755 #ifdef SATA_DEBUG
8756 			if (sata_debug_flags & SATA_DBG_ATAPI) {
8757 				msg_buf[0] = '\0';
8758 				(void) snprintf(msg_buf, MAXPATHLEN,
8759 				    "ATAPI packet completion reason: %02x\n"
8760 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x\n"
8761 				    "          %02x %02x %02x %02x %02x %02x\n"
8762 				    "          %02x %02x %02x %02x %02x %02x",
8763 				    spkt->satapkt_reason,
8764 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
8765 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
8766 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
8767 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
8768 				    rqsp[16], rqsp[17]);
8769 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
8770 				    "%s", msg_buf);
8771 			}
8772 #endif
8773 		} else {
8774 			switch (spkt->satapkt_reason) {
8775 			case SATA_PKT_PORT_ERROR:
8776 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
8777 				    "sata_get_atapi_inquiry_data: "
8778 				    "packet reason: port error", NULL);
8779 				break;
8780 
8781 			case SATA_PKT_TIMEOUT:
8782 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
8783 				    "sata_get_atapi_inquiry_data: "
8784 				    "packet reason: timeout", NULL);
8785 				break;
8786 
8787 			case SATA_PKT_ABORTED:
8788 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
8789 				    "sata_get_atapi_inquiry_data: "
8790 				    "packet reason: aborted", NULL);
8791 				break;
8792 
8793 			case SATA_PKT_RESET:
8794 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
8795 				    "sata_get_atapi_inquiry_data: "
8796 				    "packet reason: reset\n", NULL);
8797 				break;
8798 			default:
8799 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
8800 				    "sata_get_atapi_inquiry_data: "
8801 				    "invalid packet reason: %02x\n",
8802 				    spkt->satapkt_reason);
8803 				break;
8804 			}
8805 		}
8806 	}
8807 cleanup:
8808 	sata_free_local_buffer(spx);
8809 	sata_pkt_free(spx);
8810 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
8811 	return (rval);
8812 }
8813 
8814 
8815 
8816 
8817 
8818 #if 0
8819 #ifdef SATA_DEBUG
8820 
8821 /*
8822  * Test ATAPI packet command.
8823  * Single threaded test: send packet command in synch mode, process completion
8824  *
8825  */
8826 static void
8827 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport)
8828 {
8829 	sata_pkt_txlate_t *spx;
8830 	sata_pkt_t *spkt;
8831 	struct buf *bp;
8832 	sata_device_t sata_device;
8833 	sata_drive_info_t *sdinfo;
8834 	sata_cmd_t *scmd;
8835 	int rval;
8836 	uint8_t *rqsp;
8837 
8838 	ASSERT(sata_hba_inst != NULL);
8839 	sata_device.satadev_addr.cport = cport;
8840 	sata_device.satadev_addr.pmport = 0;
8841 	sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
8842 	sata_device.satadev_rev = SATA_DEVICE_REV;
8843 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8844 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
8845 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8846 	if (sdinfo == NULL) {
8847 		sata_log(sata_hba_inst, CE_WARN,
8848 		    "sata_test_atapi_packet_command: "
8849 		    "no device info for cport %d",
8850 		    sata_device.satadev_addr.cport);
8851 		return;
8852 	}
8853 
8854 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
8855 	spx->txlt_sata_hba_inst = sata_hba_inst;
8856 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
8857 	spkt = sata_pkt_alloc(spx, NULL);
8858 	if (spkt == NULL) {
8859 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
8860 		return;
8861 	}
8862 	/* address is needed now */
8863 	spkt->satapkt_device.satadev_addr = sata_device.satadev_addr;
8864 
8865 	/* 1024k buffer */
8866 	bp = sata_alloc_local_buffer(spx, 1024);
8867 	if (bp == NULL) {
8868 		sata_pkt_free(spx);
8869 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
8870 		sata_log(sata_hba_inst, CE_WARN,
8871 		    "sata_test_atapi_packet_command: "
8872 		    "cannot allocate data buffer");
8873 		return;
8874 	}
8875 	bp_mapin(bp); /* make data buffer accessible */
8876 
8877 	scmd = &spkt->satapkt_cmd;
8878 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
8879 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
8880 
8881 	/* Use synchronous mode */
8882 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
8883 
8884 	/* Synchronous mode, no callback - may be changed by the caller */
8885 	spkt->satapkt_comp = NULL;
8886 	spkt->satapkt_time = sata_default_pkt_time;
8887 
8888 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
8889 
8890 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
8891 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
8892 
8893 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
8894 
8895 	/* Set-up acdb. */
8896 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
8897 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
8898 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
8899 	scmd->satacmd_acdb[1] = 0x00;
8900 	scmd->satacmd_acdb[2] = 0x00;
8901 	scmd->satacmd_acdb[3] = 0x00;
8902 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
8903 	scmd->satacmd_acdb[5] = 0x00;
8904 
8905 	sata_fixed_sense_data_preset(
8906 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
8907 
8908 	/* Transfer command to HBA */
8909 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8910 	if (sata_hba_start(spx, &rval) != 0) {
8911 		/* Pkt not accepted for execution */
8912 		sata_log(sata_hba_inst, CE_WARN,
8913 		    "sata_test_atapi_packet_command: "
8914 		    "Packet not accepted for execution - ret: %02x", rval);
8915 		mutex_exit(
8916 		    &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8917 		goto cleanup;
8918 	}
8919 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8920 
8921 	if (spx->txlt_buf_dma_handle != NULL) {
8922 		/*
8923 		 * Sync buffer. Handle is in usual place in translate struct.
8924 		 */
8925 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
8926 		    DDI_DMA_SYNC_FORCPU);
8927 		ASSERT(rval == DDI_SUCCESS);
8928 	}
8929 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
8930 		sata_log(sata_hba_inst, CE_WARN,
8931 		    "sata_test_atapi_packet_command: "
8932 		    "Packet completed successfully");
8933 		/*
8934 		 * Normal completion - show inquiry data
8935 		 */
8936 		sata_show_inqry_data((uint8_t *)bp->b_un.b_addr);
8937 	} else {
8938 		/*
8939 		 * Something went wrong - analyze return - check rqsense data
8940 		 */
8941 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
8942 			/*
8943 			 * ARQ data hopefull show something other than NO SENSE
8944 			 */
8945 			rqsp = scmd->satacmd_rqsense;
8946 			sata_log(spx->txlt_sata_hba_inst, CE_WARN,
8947 			    "ATAPI packet completion reason: %02x\n"
8948 			    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
8949 			    "          %02x %02x %02x %02x %02x %02x "
8950 			    "          %02x %02x %02x %02x %02x %02x\n",
8951 			    spkt->satapkt_reason,
8952 			    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
8953 			    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
8954 			    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
8955 			    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
8956 			    rqsp[16], rqsp[17]);
8957 		} else {
8958 			switch (spkt->satapkt_reason) {
8959 			case SATA_PKT_PORT_ERROR:
8960 				sata_log(sata_hba_inst, CE_WARN,
8961 				    "sata_test_atapi_packet_command: "
8962 				    "packet reason: port error\n");
8963 				break;
8964 
8965 			case SATA_PKT_TIMEOUT:
8966 				sata_log(sata_hba_inst, CE_WARN,
8967 				    "sata_test_atapi_packet_command: "
8968 				    "packet reason: timeout\n");
8969 				break;
8970 
8971 			case SATA_PKT_ABORTED:
8972 				sata_log(sata_hba_inst, CE_WARN,
8973 				    "sata_test_atapi_packet_command: "
8974 				    "packet reason: aborted\n");
8975 				break;
8976 
8977 			case SATA_PKT_RESET:
8978 				sata_log(sata_hba_inst, CE_WARN,
8979 				    "sata_test_atapi_packet_command: "
8980 				    "packet reason: reset\n");
8981 				break;
8982 			default:
8983 				sata_log(sata_hba_inst, CE_WARN,
8984 				    "sata_test_atapi_packet_command: "
8985 				    "invalid packet reason: %02x\n",
8986 				    spkt->satapkt_reason);
8987 				break;
8988 			}
8989 		}
8990 	}
8991 cleanup:
8992 	sata_free_local_buffer(spx);
8993 	sata_pkt_free(spx);
8994 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
8995 }
8996 
8997 #endif /* SATA_DEBUG */
8998 #endif /* 1 */
8999 
9000 
9001 /* ************************** LOCAL HELPER FUNCTIONS *********************** */
9002 
9003 /*
9004  * Validate sata_tran info
9005  * SATA_FAILURE returns if structure is inconsistent or structure revision
9006  * does not match one used by the framework.
9007  *
9008  * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains
9009  * required function pointers.
9010  * Returns SATA_FAILURE otherwise.
9011  */
9012 static int
9013 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran)
9014 {
9015 	/*
9016 	 * SATA_TRAN_HBA_REV is the current (highest) revision number
9017 	 * of the SATA interface.
9018 	 */
9019 	if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) {
9020 		sata_log(NULL, CE_WARN,
9021 		    "sata: invalid sata_hba_tran version %d for driver %s",
9022 		    sata_tran->sata_tran_hba_rev, ddi_driver_name(dip));
9023 		return (SATA_FAILURE);
9024 	}
9025 
9026 	if (dip != sata_tran->sata_tran_hba_dip) {
9027 		SATA_LOG_D((NULL, CE_WARN,
9028 		    "sata: inconsistent sata_tran_hba_dip "
9029 		    "%p / %p", sata_tran->sata_tran_hba_dip, dip));
9030 		return (SATA_FAILURE);
9031 	}
9032 
9033 	if (sata_tran->sata_tran_probe_port == NULL ||
9034 	    sata_tran->sata_tran_start == NULL ||
9035 	    sata_tran->sata_tran_abort == NULL ||
9036 	    sata_tran->sata_tran_reset_dport == NULL ||
9037 	    sata_tran->sata_tran_hotplug_ops == NULL ||
9038 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL ||
9039 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate ==
9040 	    NULL) {
9041 		SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing "
9042 		    "required functions"));
9043 	}
9044 	return (SATA_SUCCESS);
9045 }
9046 
9047 /*
9048  * Remove HBA instance from sata_hba_list.
9049  */
9050 static void
9051 sata_remove_hba_instance(dev_info_t *dip)
9052 {
9053 	sata_hba_inst_t	*sata_hba_inst;
9054 
9055 	mutex_enter(&sata_mutex);
9056 	for (sata_hba_inst = sata_hba_list;
9057 	    sata_hba_inst != (struct sata_hba_inst *)NULL;
9058 	    sata_hba_inst = sata_hba_inst->satahba_next) {
9059 		if (sata_hba_inst->satahba_dip == dip)
9060 			break;
9061 	}
9062 
9063 	if (sata_hba_inst == (struct sata_hba_inst *)NULL) {
9064 #ifdef SATA_DEBUG
9065 		cmn_err(CE_WARN, "sata_remove_hba_instance: "
9066 		    "unknown HBA instance\n");
9067 #endif
9068 		ASSERT(FALSE);
9069 	}
9070 	if (sata_hba_inst == sata_hba_list) {
9071 		sata_hba_list = sata_hba_inst->satahba_next;
9072 		if (sata_hba_list) {
9073 			sata_hba_list->satahba_prev =
9074 			    (struct sata_hba_inst *)NULL;
9075 		}
9076 		if (sata_hba_inst == sata_hba_list_tail) {
9077 			sata_hba_list_tail = NULL;
9078 		}
9079 	} else if (sata_hba_inst == sata_hba_list_tail) {
9080 		sata_hba_list_tail = sata_hba_inst->satahba_prev;
9081 		if (sata_hba_list_tail) {
9082 			sata_hba_list_tail->satahba_next =
9083 			    (struct sata_hba_inst *)NULL;
9084 		}
9085 	} else {
9086 		sata_hba_inst->satahba_prev->satahba_next =
9087 		    sata_hba_inst->satahba_next;
9088 		sata_hba_inst->satahba_next->satahba_prev =
9089 		    sata_hba_inst->satahba_prev;
9090 	}
9091 	mutex_exit(&sata_mutex);
9092 }
9093 
9094 /*
9095  * Probe all SATA ports of the specified HBA instance.
9096  * The assumption is that there are no target and attachment point minor nodes
9097  * created by the boot subsystems, so we do not need to prune device tree.
9098  *
9099  * This function is called only from sata_hba_attach(). It does not have to
9100  * be protected by controller mutex, because the hba_attached flag is not set
9101  * yet and no one would be touching this HBA instance other than this thread.
9102  * Determines if port is active and what type of the device is attached
9103  * (if any). Allocates necessary structures for each port.
9104  *
9105  * An AP (Attachement Point) node is created for each SATA device port even
9106  * when there is no device attached.
9107  */
9108 
9109 static 	void
9110 sata_probe_ports(sata_hba_inst_t *sata_hba_inst)
9111 {
9112 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
9113 	int			ncport;
9114 	sata_cport_info_t 	*cportinfo;
9115 	sata_drive_info_t	*drive;
9116 	sata_device_t		sata_device;
9117 	int			rval;
9118 	dev_t			minor_number;
9119 	char			name[16];
9120 	clock_t			start_time, cur_time;
9121 
9122 	/*
9123 	 * Probe controller ports first, to find port status and
9124 	 * any port multiplier attached.
9125 	 */
9126 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
9127 		/* allocate cport structure */
9128 		cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP);
9129 		ASSERT(cportinfo != NULL);
9130 		mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL);
9131 
9132 		mutex_enter(&cportinfo->cport_mutex);
9133 
9134 		cportinfo->cport_addr.cport = ncport;
9135 		cportinfo->cport_addr.pmport = 0;
9136 		cportinfo->cport_addr.qual = SATA_ADDR_CPORT;
9137 		cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
9138 		cportinfo->cport_state |= SATA_STATE_PROBING;
9139 		SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo;
9140 
9141 		/*
9142 		 * Regardless if a port is usable or not, create
9143 		 * an attachment point
9144 		 */
9145 		mutex_exit(&cportinfo->cport_mutex);
9146 		minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip),
9147 		    ncport, 0, SATA_ADDR_CPORT);
9148 		(void) sprintf(name, "%d", ncport);
9149 		if (ddi_create_minor_node(dip, name, S_IFCHR,
9150 		    minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) !=
9151 		    DDI_SUCCESS) {
9152 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
9153 			    "cannot create SATA attachment point for port %d",
9154 			    ncport);
9155 		}
9156 
9157 		/* Probe port */
9158 		start_time = ddi_get_lbolt();
9159 	reprobe_cport:
9160 		sata_device.satadev_addr.cport = ncport;
9161 		sata_device.satadev_addr.pmport = 0;
9162 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
9163 		sata_device.satadev_rev = SATA_DEVICE_REV;
9164 
9165 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
9166 		    (dip, &sata_device);
9167 
9168 		mutex_enter(&cportinfo->cport_mutex);
9169 		cportinfo->cport_scr = sata_device.satadev_scr;
9170 		if (rval != SATA_SUCCESS) {
9171 			/* Something went wrong? Fail the port */
9172 			cportinfo->cport_state = SATA_PSTATE_FAILED;
9173 			mutex_exit(&cportinfo->cport_mutex);
9174 			continue;
9175 		}
9176 		cportinfo->cport_state &= ~SATA_STATE_PROBING;
9177 		cportinfo->cport_state |= SATA_STATE_PROBED;
9178 		cportinfo->cport_dev_type = sata_device.satadev_type;
9179 
9180 		cportinfo->cport_state |= SATA_STATE_READY;
9181 		if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) {
9182 			mutex_exit(&cportinfo->cport_mutex);
9183 			continue;
9184 		}
9185 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
9186 			/*
9187 			 * There is some device attached.
9188 			 * Allocate device info structure
9189 			 */
9190 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) {
9191 				mutex_exit(&cportinfo->cport_mutex);
9192 				SATA_CPORTINFO_DRV_INFO(cportinfo) =
9193 				    kmem_zalloc(sizeof (sata_drive_info_t),
9194 				    KM_SLEEP);
9195 				mutex_enter(&cportinfo->cport_mutex);
9196 			}
9197 			drive = SATA_CPORTINFO_DRV_INFO(cportinfo);
9198 			drive->satadrv_addr = cportinfo->cport_addr;
9199 			drive->satadrv_addr.qual = SATA_ADDR_DCPORT;
9200 			drive->satadrv_type = cportinfo->cport_dev_type;
9201 			drive->satadrv_state = SATA_STATE_UNKNOWN;
9202 
9203 			mutex_exit(&cportinfo->cport_mutex);
9204 			if (sata_add_device(dip, sata_hba_inst, &sata_device) !=
9205 			    SATA_SUCCESS) {
9206 				/*
9207 				 * Plugged device was not correctly identified.
9208 				 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT
9209 				 */
9210 				cur_time = ddi_get_lbolt();
9211 				if ((cur_time - start_time) <
9212 				    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
9213 					/* sleep for a while */
9214 					delay(drv_usectohz(
9215 					    SATA_DEV_RETRY_DLY));
9216 					goto reprobe_cport;
9217 				}
9218 			}
9219 		} else { /* SATA_DTYPE_PMULT */
9220 			mutex_exit(&cportinfo->cport_mutex);
9221 
9222 			/* Allocate sata_pmult_info and sata_pmport_info */
9223 			sata_alloc_pmult(sata_hba_inst, &sata_device);
9224 
9225 			/* Log the information of the port multiplier */
9226 			sata_show_pmult_info(sata_hba_inst, &sata_device);
9227 
9228 			/* Probe its pmports */
9229 			sata_probe_pmports(sata_hba_inst, ncport);
9230 		}
9231 	}
9232 }
9233 
9234 /*
9235  * Probe all device ports behind a port multiplier.
9236  *
9237  * PMult-related structure should be allocated before by sata_alloc_pmult().
9238  *
9239  * NOTE1: Only called from sata_probe_ports()
9240  * NOTE2: No mutex should be hold.
9241  */
9242 static void
9243 sata_probe_pmports(sata_hba_inst_t *sata_hba_inst, uint8_t ncport)
9244 {
9245 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
9246 	sata_pmult_info_t	*pmultinfo = NULL;
9247 	sata_pmport_info_t 	*pmportinfo = NULL;
9248 	sata_drive_info_t	*drive = NULL;
9249 	sata_device_t		sata_device;
9250 
9251 	clock_t			start_time, cur_time;
9252 	int			npmport;
9253 	int			rval;
9254 
9255 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, ncport);
9256 
9257 	/* Probe Port Multiplier ports */
9258 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports; npmport++) {
9259 		pmportinfo = pmultinfo->pmult_dev_port[npmport];
9260 		start_time = ddi_get_lbolt();
9261 reprobe_pmport:
9262 		sata_device.satadev_addr.cport = ncport;
9263 		sata_device.satadev_addr.pmport = npmport;
9264 		sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
9265 		sata_device.satadev_rev = SATA_DEVICE_REV;
9266 
9267 		/* Let HBA driver probe it. */
9268 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
9269 		    (dip, &sata_device);
9270 		mutex_enter(&pmportinfo->pmport_mutex);
9271 
9272 		pmportinfo->pmport_scr = sata_device.satadev_scr;
9273 
9274 		if (rval != SATA_SUCCESS) {
9275 			pmportinfo->pmport_state =
9276 			    SATA_PSTATE_FAILED;
9277 			mutex_exit(&pmportinfo->pmport_mutex);
9278 			continue;
9279 		}
9280 		pmportinfo->pmport_state &= ~SATA_STATE_PROBING;
9281 		pmportinfo->pmport_state |= SATA_STATE_PROBED;
9282 		pmportinfo->pmport_dev_type = sata_device.satadev_type;
9283 
9284 		pmportinfo->pmport_state |= SATA_STATE_READY;
9285 		if (pmportinfo->pmport_dev_type ==
9286 		    SATA_DTYPE_NONE) {
9287 			SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
9288 			    "no device found at port %d:%d", ncport, npmport);
9289 			mutex_exit(&pmportinfo->pmport_mutex);
9290 			continue;
9291 		}
9292 		/* Port multipliers cannot be chained */
9293 		ASSERT(pmportinfo->pmport_dev_type != SATA_DTYPE_PMULT);
9294 		/*
9295 		 * There is something attached to Port
9296 		 * Multiplier device port
9297 		 * Allocate device info structure
9298 		 */
9299 		if (pmportinfo->pmport_sata_drive == NULL) {
9300 			mutex_exit(&pmportinfo->pmport_mutex);
9301 			pmportinfo->pmport_sata_drive =
9302 			    kmem_zalloc(sizeof (sata_drive_info_t), KM_SLEEP);
9303 			mutex_enter(&pmportinfo->pmport_mutex);
9304 		}
9305 		drive = pmportinfo->pmport_sata_drive;
9306 		drive->satadrv_addr.cport = pmportinfo->pmport_addr.cport;
9307 		drive->satadrv_addr.pmport = npmport;
9308 		drive->satadrv_addr.qual = SATA_ADDR_DPMPORT;
9309 		drive->satadrv_type = pmportinfo-> pmport_dev_type;
9310 		drive->satadrv_state = SATA_STATE_UNKNOWN;
9311 
9312 		mutex_exit(&pmportinfo->pmport_mutex);
9313 		rval = sata_add_device(dip, sata_hba_inst, &sata_device);
9314 
9315 		if (rval != SATA_SUCCESS) {
9316 			/*
9317 			 * Plugged device was not correctly identified.
9318 			 * Retry, within the SATA_DEV_IDENTIFY_TIMEOUT
9319 			 */
9320 			cur_time = ddi_get_lbolt();
9321 			if ((cur_time - start_time) < drv_usectohz(
9322 			    SATA_DEV_IDENTIFY_TIMEOUT)) {
9323 				/* sleep for a while */
9324 				delay(drv_usectohz(SATA_DEV_RETRY_DLY));
9325 				goto reprobe_pmport;
9326 			}
9327 		}
9328 	}
9329 }
9330 
9331 /*
9332  * Add SATA device for specified HBA instance & port (SCSI target
9333  * device nodes).
9334  * This function is called (indirectly) only from sata_hba_attach().
9335  * A target node is created when there is a supported type device attached,
9336  * but may be removed if it cannot be put online.
9337  *
9338  * This function cannot be called from an interrupt context.
9339  *
9340  * Create target nodes for disk, CD/DVD, Tape and ATAPI disk devices
9341  *
9342  * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when
9343  * device identification failed - adding a device could be retried.
9344  *
9345  */
9346 static 	int
9347 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst,
9348     sata_device_t *sata_device)
9349 {
9350 	sata_cport_info_t 	*cportinfo;
9351 	sata_pmult_info_t	*pminfo;
9352 	sata_pmport_info_t	*pmportinfo;
9353 	dev_info_t		*cdip;		/* child dip */
9354 	sata_address_t		*saddr = &sata_device->satadev_addr;
9355 	uint8_t			cport, pmport;
9356 	int			rval;
9357 
9358 	cport = saddr->cport;
9359 	pmport = saddr->pmport;
9360 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
9361 	ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE);
9362 
9363 	/*
9364 	 * Some device is attached to a controller port.
9365 	 * We rely on controllers distinquishing between no-device,
9366 	 * attached port multiplier and other kind of attached device.
9367 	 * We need to get Identify Device data and determine
9368 	 * positively the dev type before trying to attach
9369 	 * the target driver.
9370 	 */
9371 	sata_device->satadev_rev = SATA_DEVICE_REV;
9372 	switch (saddr->qual) {
9373 	case SATA_ADDR_CPORT:
9374 		/*
9375 		 * Add a non-port-multiplier device at controller port.
9376 		 */
9377 		saddr->qual = SATA_ADDR_DCPORT;
9378 
9379 		rval = sata_probe_device(sata_hba_inst, sata_device);
9380 		if (rval != SATA_SUCCESS ||
9381 		    sata_device->satadev_type == SATA_DTYPE_UNKNOWN)
9382 			return (SATA_FAILURE);
9383 
9384 		mutex_enter(&cportinfo->cport_mutex);
9385 		sata_show_drive_info(sata_hba_inst,
9386 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
9387 
9388 		if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) {
9389 			/*
9390 			 * Could not determine device type or
9391 			 * a device is not supported.
9392 			 * Degrade this device to unknown.
9393 			 */
9394 			cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
9395 			mutex_exit(&cportinfo->cport_mutex);
9396 			return (SATA_SUCCESS);
9397 		}
9398 		cportinfo->cport_dev_type = sata_device->satadev_type;
9399 		cportinfo->cport_tgtnode_clean = B_TRUE;
9400 		mutex_exit(&cportinfo->cport_mutex);
9401 
9402 		/*
9403 		 * Initialize device to the desired state. Even if it
9404 		 * fails, the device will still attach but syslog
9405 		 * will show the warning.
9406 		 */
9407 		if (sata_initialize_device(sata_hba_inst,
9408 		    SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) {
9409 			/* Retry */
9410 			rval = sata_initialize_device(sata_hba_inst,
9411 			    SATA_CPORTINFO_DRV_INFO(cportinfo));
9412 
9413 			if (rval == SATA_RETRY)
9414 				sata_log(sata_hba_inst, CE_WARN,
9415 				    "SATA device at port %d - "
9416 				    "default device features could not be set."
9417 				    " Device may not operate as expected.",
9418 				    cport);
9419 		}
9420 
9421 		cdip = sata_create_target_node(pdip, sata_hba_inst, saddr);
9422 		if (cdip == NULL) {
9423 			/*
9424 			 * Attaching target node failed.
9425 			 * We retain sata_drive_info structure...
9426 			 */
9427 			return (SATA_SUCCESS);
9428 		}
9429 
9430 		mutex_enter(&cportinfo->cport_mutex);
9431 		(SATA_CPORTINFO_DRV_INFO(cportinfo))->
9432 		    satadrv_state = SATA_STATE_READY;
9433 		mutex_exit(&cportinfo->cport_mutex);
9434 
9435 		break;
9436 
9437 	case SATA_ADDR_PMPORT:
9438 		saddr->qual = SATA_ADDR_DPMPORT;
9439 
9440 		mutex_enter(&cportinfo->cport_mutex);
9441 		/* It must be a Port Multiplier at the controller port */
9442 		ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT);
9443 
9444 		pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
9445 		pmportinfo = pminfo->pmult_dev_port[saddr->pmport];
9446 		mutex_exit(&cportinfo->cport_mutex);
9447 
9448 		rval = sata_probe_device(sata_hba_inst, sata_device);
9449 		if (rval != SATA_SUCCESS ||
9450 		    sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
9451 			return (SATA_FAILURE);
9452 		}
9453 
9454 		mutex_enter(&pmportinfo->pmport_mutex);
9455 		sata_show_drive_info(sata_hba_inst,
9456 		    SATA_PMPORTINFO_DRV_INFO(pmportinfo));
9457 
9458 		if ((sata_device->satadev_type & SATA_VALID_DEV_TYPE) == 0) {
9459 			/*
9460 			 * Could not determine device type.
9461 			 * Degrade this device to unknown.
9462 			 */
9463 			pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
9464 			mutex_exit(&pmportinfo->pmport_mutex);
9465 			return (SATA_SUCCESS);
9466 		}
9467 		pmportinfo->pmport_dev_type = sata_device->satadev_type;
9468 		pmportinfo->pmport_tgtnode_clean = B_TRUE;
9469 		mutex_exit(&pmportinfo->pmport_mutex);
9470 
9471 		/*
9472 		 * Initialize device to the desired state.
9473 		 * Even if it fails, the device will still
9474 		 * attach but syslog will show the warning.
9475 		 */
9476 		if (sata_initialize_device(sata_hba_inst,
9477 		    pmportinfo->pmport_sata_drive) != SATA_SUCCESS) {
9478 			/* Retry */
9479 			rval = sata_initialize_device(sata_hba_inst,
9480 			    pmportinfo->pmport_sata_drive);
9481 
9482 			if (rval == SATA_RETRY)
9483 				sata_log(sata_hba_inst, CE_WARN,
9484 				    "SATA device at port %d:%d - "
9485 				    "default device features could not be set."
9486 				    " Device may not operate as expected.",
9487 				    cport, pmport);
9488 		}
9489 
9490 		cdip = sata_create_target_node(pdip, sata_hba_inst, saddr);
9491 		if (cdip == NULL) {
9492 			/*
9493 			 * Attaching target node failed.
9494 			 * We retain sata_drive_info structure...
9495 			 */
9496 			return (SATA_SUCCESS);
9497 		}
9498 		mutex_enter(&pmportinfo->pmport_mutex);
9499 		pmportinfo->pmport_sata_drive->satadrv_state |=
9500 		    SATA_STATE_READY;
9501 		mutex_exit(&pmportinfo->pmport_mutex);
9502 
9503 		break;
9504 
9505 	default:
9506 		return (SATA_FAILURE);
9507 	}
9508 
9509 	return (SATA_SUCCESS);
9510 }
9511 
9512 /*
9513  * Clean up target node at specific address.
9514  *
9515  * NOTE: No Mutex should be hold.
9516  */
9517 static int
9518 sata_offline_device(sata_hba_inst_t *sata_hba_inst,
9519     sata_device_t *sata_device, sata_drive_info_t *sdinfo)
9520 {
9521 	uint8_t cport, pmport, qual;
9522 	dev_info_t *tdip;
9523 
9524 	cport = sata_device->satadev_addr.cport;
9525 	pmport = sata_device->satadev_addr.pmport;
9526 	qual = sata_device->satadev_addr.qual;
9527 
9528 	if (qual == SATA_ADDR_DCPORT) {
9529 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9530 		    "sata_hba_ioctl: disconnect device at port %d", cport));
9531 	} else {
9532 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9533 		    "sata_hba_ioctl: disconnect device at port %d:%d",
9534 		    cport, pmport));
9535 	}
9536 
9537 	/* We are addressing attached device, not a port */
9538 	sata_device->satadev_addr.qual =
9539 	    sdinfo->satadrv_addr.qual;
9540 	tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
9541 	    &sata_device->satadev_addr);
9542 	if (tdip != NULL && ndi_devi_offline(tdip,
9543 	    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
9544 		/*
9545 		 * Problem :
9546 		 * The target node remained attached.
9547 		 * This happens when the device file was open
9548 		 * or a node was waiting for resources.
9549 		 * Cannot do anything about it.
9550 		 */
9551 		if (qual == SATA_ADDR_DCPORT) {
9552 			SATA_LOG_D((sata_hba_inst, CE_WARN,
9553 			    "sata_hba_ioctl: disconnect: could "
9554 			    "not unconfigure device before "
9555 			    "disconnecting the SATA port %d",
9556 			    cport));
9557 		} else {
9558 			SATA_LOG_D((sata_hba_inst, CE_WARN,
9559 			    "sata_hba_ioctl: disconnect: could "
9560 			    "not unconfigure device before "
9561 			    "disconnecting the SATA port %d:%d",
9562 			    cport, pmport));
9563 		}
9564 		/*
9565 		 * Set DEVICE REMOVED state in the target
9566 		 * node. It will prevent access to the device
9567 		 * even when a new device is attached, until
9568 		 * the old target node is released, removed and
9569 		 * recreated for a new  device.
9570 		 */
9571 		sata_set_device_removed(tdip);
9572 
9573 		/*
9574 		 * Instruct event daemon to try the target
9575 		 * node cleanup later.
9576 		 */
9577 		sata_set_target_node_cleanup(
9578 		    sata_hba_inst, &sata_device->satadev_addr);
9579 	}
9580 
9581 
9582 	return (SATA_SUCCESS);
9583 }
9584 
9585 
9586 /*
9587  * Create scsi target node for attached device, create node properties and
9588  * attach the node.
9589  * The node could be removed if the device onlining fails.
9590  *
9591  * A dev_info_t pointer is returned if operation is successful, NULL is
9592  * returned otherwise.
9593  */
9594 
9595 static dev_info_t *
9596 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst,
9597 			sata_address_t *sata_addr)
9598 {
9599 	dev_info_t *cdip = NULL;
9600 	int rval;
9601 	char *nname = NULL;
9602 	char **compatible = NULL;
9603 	int ncompatible;
9604 	struct scsi_inquiry inq;
9605 	sata_device_t sata_device;
9606 	sata_drive_info_t *sdinfo;
9607 	int target;
9608 	int i;
9609 
9610 	sata_device.satadev_rev = SATA_DEVICE_REV;
9611 	sata_device.satadev_addr = *sata_addr;
9612 
9613 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport)));
9614 
9615 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
9616 
9617 	target = SATA_TO_SCSI_TARGET(sata_addr->cport,
9618 	    sata_addr->pmport, sata_addr->qual);
9619 
9620 	if (sdinfo == NULL) {
9621 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9622 		    sata_addr->cport)));
9623 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9624 		    "sata_create_target_node: no sdinfo for target %x",
9625 		    target));
9626 		return (NULL);
9627 	}
9628 
9629 	/*
9630 	 * create or get scsi inquiry data, expected by
9631 	 * scsi_hba_nodename_compatible_get()
9632 	 * SATA hard disks get Identify Data translated into Inguiry Data.
9633 	 * ATAPI devices respond directly to Inquiry request.
9634 	 */
9635 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9636 		sata_identdev_to_inquiry(sata_hba_inst, sdinfo,
9637 		    (uint8_t *)&inq);
9638 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9639 		    sata_addr->cport)));
9640 	} else { /* Assume supported ATAPI device */
9641 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9642 		    sata_addr->cport)));
9643 		if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr,
9644 		    &inq) == SATA_FAILURE)
9645 			return (NULL);
9646 		/*
9647 		 * Save supported ATAPI transport version
9648 		 */
9649 		sdinfo->satadrv_atapi_trans_ver =
9650 		    SATA_ATAPI_TRANS_VERSION(&inq);
9651 	}
9652 
9653 	/* determine the node name and compatible */
9654 	scsi_hba_nodename_compatible_get(&inq, NULL,
9655 	    inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
9656 
9657 #ifdef SATA_DEBUG
9658 	if (sata_debug_flags & SATA_DBG_NODES) {
9659 		if (nname == NULL) {
9660 			cmn_err(CE_NOTE, "sata_create_target_node: "
9661 			    "cannot determine nodename for target %d\n",
9662 			    target);
9663 		} else {
9664 			cmn_err(CE_WARN, "sata_create_target_node: "
9665 			    "target %d nodename: %s\n", target, nname);
9666 		}
9667 		if (compatible == NULL) {
9668 			cmn_err(CE_WARN,
9669 			    "sata_create_target_node: no compatible name\n");
9670 		} else {
9671 			for (i = 0; i < ncompatible; i++) {
9672 				cmn_err(CE_WARN, "sata_create_target_node: "
9673 				    "compatible name: %s\n", compatible[i]);
9674 			}
9675 		}
9676 	}
9677 #endif
9678 
9679 	/* if nodename can't be determined, log error and exit */
9680 	if (nname == NULL) {
9681 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9682 		    "sata_create_target_node: cannot determine nodename "
9683 		    "for target %d\n", target));
9684 		scsi_hba_nodename_compatible_free(nname, compatible);
9685 		return (NULL);
9686 	}
9687 	/*
9688 	 * Create scsi target node
9689 	 */
9690 	ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip);
9691 	rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
9692 	    "device-type", "scsi");
9693 
9694 	if (rval != DDI_PROP_SUCCESS) {
9695 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
9696 		    "updating device_type prop failed %d", rval));
9697 		goto fail;
9698 	}
9699 
9700 	/*
9701 	 * Create target node properties: target & lun
9702 	 */
9703 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target);
9704 	if (rval != DDI_PROP_SUCCESS) {
9705 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
9706 		    "updating target prop failed %d", rval));
9707 		goto fail;
9708 	}
9709 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0);
9710 	if (rval != DDI_PROP_SUCCESS) {
9711 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
9712 		    "updating target prop failed %d", rval));
9713 		goto fail;
9714 	}
9715 
9716 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
9717 		/*
9718 		 * Add "variant" property
9719 		 */
9720 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
9721 		    "variant", "atapi");
9722 		if (rval != DDI_PROP_SUCCESS) {
9723 			SATA_LOG_D((sata_hba_inst, CE_WARN,
9724 			    "sata_create_target_node: variant atapi "
9725 			    "property could not be created: %d", rval));
9726 			goto fail;
9727 		}
9728 	}
9729 	/* decorate the node with compatible */
9730 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible",
9731 	    compatible, ncompatible) != DDI_PROP_SUCCESS) {
9732 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9733 		    "sata_create_target_node: FAIL compatible props cdip 0x%p",
9734 		    (void *)cdip));
9735 		goto fail;
9736 	}
9737 
9738 
9739 	/*
9740 	 * Now, try to attach the driver. If probing of the device fails,
9741 	 * the target node may be removed
9742 	 */
9743 	rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH);
9744 
9745 	scsi_hba_nodename_compatible_free(nname, compatible);
9746 
9747 	if (rval == NDI_SUCCESS)
9748 		return (cdip);
9749 
9750 	/* target node was removed - are we sure? */
9751 	return (NULL);
9752 
9753 fail:
9754 	scsi_hba_nodename_compatible_free(nname, compatible);
9755 	ddi_prop_remove_all(cdip);
9756 	rval = ndi_devi_free(cdip);
9757 	if (rval != NDI_SUCCESS) {
9758 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
9759 		    "node removal failed %d", rval));
9760 	}
9761 	sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: "
9762 	    "cannot create target node for SATA device at port %d",
9763 	    sata_addr->cport);
9764 	return (NULL);
9765 }
9766 
9767 /*
9768  * Remove a target node.
9769  */
9770 static void
9771 sata_remove_target_node(sata_hba_inst_t *sata_hba_inst,
9772 			sata_address_t *sata_addr)
9773 {
9774 	dev_info_t *tdip;
9775 	uint8_t cport = sata_addr->cport;
9776 	uint8_t pmport = sata_addr->pmport;
9777 	uint8_t qual = sata_addr->qual;
9778 
9779 	/* Note the sata daemon uses the address of the port/pmport */
9780 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
9781 
9782 	/* Remove target node */
9783 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), cport, pmport);
9784 	if (tdip != NULL) {
9785 		/*
9786 		 * Target node exists.  Unconfigure device
9787 		 * then remove the target node (one ndi
9788 		 * operation).
9789 		 */
9790 		if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
9791 			/*
9792 			 * PROBLEM - no device, but target node remained. This
9793 			 * happens when the file was open or node was waiting
9794 			 * for resources.
9795 			 */
9796 			SATA_LOG_D((sata_hba_inst, CE_WARN,
9797 			    "sata_remove_target_node: "
9798 			    "Failed to remove target node for "
9799 			    "detached SATA device."));
9800 			/*
9801 			 * Set target node state to DEVI_DEVICE_REMOVED. But
9802 			 * re-check first that the node still exists.
9803 			 */
9804 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
9805 			    cport, pmport);
9806 			if (tdip != NULL) {
9807 				sata_set_device_removed(tdip);
9808 				/*
9809 				 * Instruct event daemon to retry the cleanup
9810 				 * later.
9811 				 */
9812 				sata_set_target_node_cleanup(sata_hba_inst,
9813 				    sata_addr);
9814 			}
9815 		}
9816 
9817 		if (qual == SATA_ADDR_CPORT)
9818 			sata_log(sata_hba_inst, CE_WARN,
9819 			    "SATA device detached at port %d", cport);
9820 		else
9821 			sata_log(sata_hba_inst, CE_WARN,
9822 			    "SATA device detached at port %d:%d",
9823 			    cport, pmport);
9824 	}
9825 #ifdef SATA_DEBUG
9826 	else {
9827 		if (qual == SATA_ADDR_CPORT)
9828 			sata_log(sata_hba_inst, CE_WARN,
9829 			    "target node not found at port %d", cport);
9830 		else
9831 			sata_log(sata_hba_inst, CE_WARN,
9832 			    "target node not found at port %d:%d",
9833 			    cport, pmport);
9834 	}
9835 #endif
9836 }
9837 
9838 
9839 /*
9840  * Re-probe sata port, check for a device and attach info
9841  * structures when necessary. Identify Device data is fetched, if possible.
9842  * Assumption: sata address is already validated.
9843  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
9844  * the presence of a device and its type.
9845  *
9846  * flag arg specifies that the function should try multiple times to identify
9847  * device type and to initialize it, or it should return immediately on failure.
9848  * SATA_DEV_IDENTIFY_RETRY - retry
9849  * SATA_DEV_IDENTIFY_NORETRY - no retry
9850  *
9851  * SATA_FAILURE is returned if one of the operations failed.
9852  *
9853  * This function cannot be called in interrupt context - it may sleep.
9854  *
9855  * Note: Port multiplier is supported.
9856  */
9857 static int
9858 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
9859     int flag)
9860 {
9861 	sata_cport_info_t *cportinfo;
9862 	sata_pmult_info_t *pmultinfo;
9863 	sata_drive_info_t *sdinfo, *osdinfo;
9864 	boolean_t init_device = B_FALSE;
9865 	int prev_device_type = SATA_DTYPE_NONE;
9866 	int prev_device_settings = 0;
9867 	int prev_device_state = 0;
9868 	clock_t start_time;
9869 	int retry = B_FALSE;
9870 	uint8_t cport = sata_device->satadev_addr.cport;
9871 	int rval_probe, rval_init;
9872 
9873 	/*
9874 	 * If target is pmport, sata_reprobe_pmport() will handle it.
9875 	 */
9876 	if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT ||
9877 	    sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT)
9878 		return (sata_reprobe_pmport(sata_hba_inst, sata_device, flag));
9879 
9880 	/* We only care about host sata cport for now */
9881 	cportinfo = SATA_CPORT_INFO(sata_hba_inst,
9882 	    sata_device->satadev_addr.cport);
9883 
9884 	/*
9885 	 * If a port multiplier was previously attached (we have no idea it
9886 	 * still there or not), sata_reprobe_pmult() will handle it.
9887 	 */
9888 	if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT)
9889 		return (sata_reprobe_pmult(sata_hba_inst, sata_device, flag));
9890 
9891 	/* Store sata_drive_info when a non-pmult device was attached. */
9892 	osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
9893 	if (osdinfo != NULL) {
9894 		/*
9895 		 * We are re-probing port with a previously attached device.
9896 		 * Save previous device type and settings.
9897 		 */
9898 		prev_device_type = cportinfo->cport_dev_type;
9899 		prev_device_settings = osdinfo->satadrv_settings;
9900 		prev_device_state = osdinfo->satadrv_state;
9901 	}
9902 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
9903 		start_time = ddi_get_lbolt();
9904 		retry = B_TRUE;
9905 	}
9906 retry_probe:
9907 
9908 	/* probe port */
9909 	mutex_enter(&cportinfo->cport_mutex);
9910 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
9911 	cportinfo->cport_state |= SATA_STATE_PROBING;
9912 	mutex_exit(&cportinfo->cport_mutex);
9913 
9914 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
9915 	    (SATA_DIP(sata_hba_inst), sata_device);
9916 
9917 	mutex_enter(&cportinfo->cport_mutex);
9918 	if (rval_probe != SATA_SUCCESS) {
9919 		cportinfo->cport_state = SATA_PSTATE_FAILED;
9920 		mutex_exit(&cportinfo->cport_mutex);
9921 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: "
9922 		    "SATA port %d probing failed",
9923 		    cportinfo->cport_addr.cport));
9924 		return (SATA_FAILURE);
9925 	}
9926 
9927 	/*
9928 	 * update sata port state and set device type
9929 	 */
9930 	sata_update_port_info(sata_hba_inst, sata_device);
9931 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
9932 
9933 	/*
9934 	 * Sanity check - Port is active? Is the link active?
9935 	 * Is there any device attached?
9936 	 */
9937 	if ((cportinfo->cport_state &
9938 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
9939 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
9940 	    SATA_PORT_DEVLINK_UP) {
9941 		/*
9942 		 * Port in non-usable state or no link active/no device.
9943 		 * Free info structure if necessary (direct attached drive
9944 		 * only, for now!
9945 		 */
9946 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
9947 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
9948 		/* Add here differentiation for device attached or not */
9949 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
9950 		mutex_exit(&cportinfo->cport_mutex);
9951 		if (sdinfo != NULL)
9952 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
9953 		return (SATA_SUCCESS);
9954 	}
9955 
9956 	cportinfo->cport_state |= SATA_STATE_READY;
9957 	cportinfo->cport_state |= SATA_STATE_PROBED;
9958 
9959 	cportinfo->cport_dev_type = sata_device->satadev_type;
9960 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
9961 
9962 	/*
9963 	 * If we are re-probing the port, there may be
9964 	 * sata_drive_info structure attached
9965 	 */
9966 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
9967 
9968 		/*
9969 		 * There is no device, so remove device info structure,
9970 		 * if necessary.
9971 		 */
9972 		/* Device change: Drive -> None */
9973 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
9974 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
9975 		if (sdinfo != NULL) {
9976 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
9977 			sata_log(sata_hba_inst, CE_WARN,
9978 			    "SATA device detached "
9979 			    "from port %d", cportinfo->cport_addr.cport);
9980 		}
9981 		mutex_exit(&cportinfo->cport_mutex);
9982 		return (SATA_SUCCESS);
9983 
9984 	}
9985 
9986 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
9987 
9988 		/* Device (may) change: Drive -> Drive */
9989 		if (sdinfo == NULL) {
9990 			/*
9991 			 * There is some device attached, but there is
9992 			 * no sata_drive_info structure - allocate one
9993 			 */
9994 			mutex_exit(&cportinfo->cport_mutex);
9995 			sdinfo = kmem_zalloc(
9996 			    sizeof (sata_drive_info_t), KM_SLEEP);
9997 			mutex_enter(&cportinfo->cport_mutex);
9998 			/*
9999 			 * Recheck, that the port state did not change when we
10000 			 * released mutex.
10001 			 */
10002 			if (cportinfo->cport_state & SATA_STATE_READY) {
10003 				SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo;
10004 				sdinfo->satadrv_addr = cportinfo->cport_addr;
10005 				sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT;
10006 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
10007 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
10008 			} else {
10009 				/*
10010 				 * Port is not in ready state, we
10011 				 * cannot attach a device.
10012 				 */
10013 				mutex_exit(&cportinfo->cport_mutex);
10014 				kmem_free(sdinfo, sizeof (sata_drive_info_t));
10015 				return (SATA_SUCCESS);
10016 			}
10017 			/*
10018 			 * Since we are adding device, presumably new one,
10019 			 * indicate that it  should be initalized,
10020 			 * as well as some internal framework states).
10021 			 */
10022 			init_device = B_TRUE;
10023 		}
10024 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
10025 		sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
10026 	} else {
10027 		/* Device change: Drive -> PMult */
10028 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
10029 		if (sdinfo != NULL) {
10030 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
10031 			sata_log(sata_hba_inst, CE_WARN,
10032 			    "SATA device detached "
10033 			    "from port %d", cportinfo->cport_addr.cport);
10034 		}
10035 
10036 		sata_log(sata_hba_inst, CE_WARN,
10037 		    "SATA port multiplier detected at port %d",
10038 		    cportinfo->cport_addr.cport);
10039 
10040 		mutex_exit(&cportinfo->cport_mutex);
10041 		sata_alloc_pmult(sata_hba_inst, sata_device);
10042 		sata_show_pmult_info(sata_hba_inst, sata_device);
10043 		mutex_enter(&cportinfo->cport_mutex);
10044 
10045 		/*
10046 		 * Mark all the port multiplier port behind the port
10047 		 * multiplier behind with link events, so that the sata daemon
10048 		 * will update their status.
10049 		 */
10050 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
10051 		pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET;
10052 		mutex_exit(&cportinfo->cport_mutex);
10053 		return (SATA_SUCCESS);
10054 	}
10055 	mutex_exit(&cportinfo->cport_mutex);
10056 
10057 	/*
10058 	 * Figure out what kind of device we are really
10059 	 * dealing with. Failure of identifying device does not fail this
10060 	 * function.
10061 	 */
10062 	rval_probe = sata_probe_device(sata_hba_inst, sata_device);
10063 	rval_init = SATA_FAILURE;
10064 	mutex_enter(&cportinfo->cport_mutex);
10065 	if (rval_probe == SATA_SUCCESS) {
10066 		/*
10067 		 * If we are dealing with the same type of a device as before,
10068 		 * restore its settings flags.
10069 		 */
10070 		if (osdinfo != NULL &&
10071 		    sata_device->satadev_type == prev_device_type)
10072 			sdinfo->satadrv_settings = prev_device_settings;
10073 
10074 		mutex_exit(&cportinfo->cport_mutex);
10075 		rval_init = SATA_SUCCESS;
10076 		/* Set initial device features, if necessary */
10077 		if (init_device == B_TRUE) {
10078 			rval_init = sata_initialize_device(sata_hba_inst,
10079 			    sdinfo);
10080 		}
10081 		if (rval_init == SATA_SUCCESS)
10082 			return (rval_init);
10083 		/* else we will retry if retry was asked for */
10084 
10085 	} else {
10086 		/*
10087 		 * If there was some device info before we probe the device,
10088 		 * restore previous device setting, so we can retry from scratch
10089 		 * later. Providing, of course, that device has not disapear
10090 		 * during probing process.
10091 		 */
10092 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
10093 			if (osdinfo != NULL) {
10094 				cportinfo->cport_dev_type = prev_device_type;
10095 				sdinfo->satadrv_type = prev_device_type;
10096 				sdinfo->satadrv_state = prev_device_state;
10097 			}
10098 		} else {
10099 			/* device is gone */
10100 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
10101 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
10102 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
10103 			mutex_exit(&cportinfo->cport_mutex);
10104 			return (SATA_SUCCESS);
10105 		}
10106 		mutex_exit(&cportinfo->cport_mutex);
10107 	}
10108 
10109 	if (retry) {
10110 		clock_t cur_time = ddi_get_lbolt();
10111 		/*
10112 		 * A device was not successfully identified or initialized.
10113 		 * Track retry time for device identification.
10114 		 */
10115 		if ((cur_time - start_time) <
10116 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
10117 			/* sleep for a while */
10118 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
10119 			goto retry_probe;
10120 		}
10121 		/* else no more retries */
10122 		mutex_enter(&cportinfo->cport_mutex);
10123 		if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
10124 			if (rval_init == SATA_RETRY) {
10125 				/*
10126 				 * Setting drive features have failed, but
10127 				 * because the drive is still accessible,
10128 				 * keep it and emit a warning message.
10129 				 */
10130 				sata_log(sata_hba_inst, CE_WARN,
10131 				    "SATA device at port %d - desired "
10132 				    "drive features could not be set. "
10133 				    "Device may not operate as expected.",
10134 				    cportinfo->cport_addr.cport);
10135 			} else {
10136 				SATA_CPORTINFO_DRV_INFO(cportinfo)->
10137 				    satadrv_state = SATA_DSTATE_FAILED;
10138 			}
10139 		}
10140 		mutex_exit(&cportinfo->cport_mutex);
10141 	}
10142 	return (SATA_SUCCESS);
10143 }
10144 
10145 /*
10146  * Reprobe a controller port that connected to a port multiplier.
10147  *
10148  * NOTE: No Mutex should be hold.
10149  */
10150 static int
10151 sata_reprobe_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
10152     int flag)
10153 {
10154 	_NOTE(ARGUNUSED(flag))
10155 	sata_cport_info_t *cportinfo;
10156 	sata_pmult_info_t *pmultinfo;
10157 	uint8_t cport = sata_device->satadev_addr.cport;
10158 	int rval_probe;
10159 
10160 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
10161 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
10162 
10163 	/* probe port */
10164 	mutex_enter(&cportinfo->cport_mutex);
10165 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
10166 	cportinfo->cport_state |= SATA_STATE_PROBING;
10167 	mutex_exit(&cportinfo->cport_mutex);
10168 
10169 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10170 	    (SATA_DIP(sata_hba_inst), sata_device);
10171 
10172 	mutex_enter(&cportinfo->cport_mutex);
10173 	if (rval_probe != SATA_SUCCESS) {
10174 		cportinfo->cport_state = SATA_PSTATE_FAILED;
10175 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmult: "
10176 		    "SATA port %d probing failed", cport));
10177 		sata_log(sata_hba_inst, CE_WARN,
10178 		    "SATA port multiplier detached at port %d", cport);
10179 		mutex_exit(&cportinfo->cport_mutex);
10180 		sata_free_pmult(sata_hba_inst, sata_device);
10181 		return (SATA_FAILURE);
10182 	}
10183 
10184 	/*
10185 	 * update sata port state and set device type
10186 	 */
10187 	sata_update_port_info(sata_hba_inst, sata_device);
10188 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
10189 	cportinfo->cport_state |= SATA_STATE_PROBED;
10190 
10191 	/*
10192 	 * Sanity check - Port is active? Is the link active?
10193 	 * Is there any device attached?
10194 	 */
10195 	if ((cportinfo->cport_state &
10196 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
10197 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
10198 	    SATA_PORT_DEVLINK_UP ||
10199 	    (sata_device->satadev_type == SATA_DTYPE_NONE)) {
10200 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
10201 		mutex_exit(&cportinfo->cport_mutex);
10202 		sata_free_pmult(sata_hba_inst, sata_device);
10203 		sata_log(sata_hba_inst, CE_WARN,
10204 		    "SATA port multiplier detached at port %d", cport);
10205 		return (SATA_SUCCESS);
10206 	}
10207 
10208 	/*
10209 	 * Device changed: PMult -> Non-PMult
10210 	 *
10211 	 * This situation is uncommon, most possibly being caused by errors
10212 	 * after which the port multiplier is not correct initialized and
10213 	 * recognized. In that case the new device will be marked as unknown
10214 	 * and will not be automatically probed in this routine. Instead
10215 	 * system administrator could manually restart it via cfgadm(1M).
10216 	 */
10217 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
10218 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
10219 		mutex_exit(&cportinfo->cport_mutex);
10220 		sata_free_pmult(sata_hba_inst, sata_device);
10221 		sata_log(sata_hba_inst, CE_WARN,
10222 		    "SATA port multiplier detached at port %d", cport);
10223 		return (SATA_FAILURE);
10224 	}
10225 
10226 	/*
10227 	 * Now we know it is a port multiplier. However, if this is not the
10228 	 * previously attached port multiplier - they may have different
10229 	 * pmport numbers - we need to re-allocate data structures for every
10230 	 * pmport and drive.
10231 	 *
10232 	 * Port multipliers of the same model have identical values in these
10233 	 * registers, so it is still necessary to update the information of
10234 	 * all drives attached to the previous port multiplier afterwards.
10235 	 */
10236 	if ((sata_device->satadev_gscr.gscr0 != pmultinfo->pmult_gscr.gscr0) ||
10237 	    (sata_device->satadev_gscr.gscr1 != pmultinfo->pmult_gscr.gscr1) ||
10238 	    (sata_device->satadev_gscr.gscr2 != pmultinfo->pmult_gscr.gscr2)) {
10239 
10240 		/* Device changed: PMult -> another PMult */
10241 		mutex_exit(&cportinfo->cport_mutex);
10242 		sata_free_pmult(sata_hba_inst, sata_device);
10243 		sata_alloc_pmult(sata_hba_inst, sata_device);
10244 		mutex_enter(&cportinfo->cport_mutex);
10245 
10246 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
10247 		    "SATA port multiplier [changed] at port %d", cport);
10248 		sata_log(sata_hba_inst, CE_WARN,
10249 		    "SATA port multiplier detected at port %d", cport);
10250 	}
10251 
10252 	/*
10253 	 * Mark all the port multiplier port behind the port
10254 	 * multiplier behind with link events, so that the sata daemon
10255 	 * will update their status.
10256 	 */
10257 	pmultinfo->pmult_event_flags |= SATA_EVNT_DEVICE_RESET;
10258 	mutex_exit(&cportinfo->cport_mutex);
10259 
10260 	return (SATA_SUCCESS);
10261 }
10262 
10263 /*
10264  * Re-probe a port multiplier port, check for a device and attach info
10265  * structures when necessary. Identify Device data is fetched, if possible.
10266  * Assumption: sata address is already validated as port multiplier port.
10267  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
10268  * the presence of a device and its type.
10269  *
10270  * flag arg specifies that the function should try multiple times to identify
10271  * device type and to initialize it, or it should return immediately on failure.
10272  * SATA_DEV_IDENTIFY_RETRY - retry
10273  * SATA_DEV_IDENTIFY_NORETRY - no retry
10274  *
10275  * SATA_FAILURE is returned if one of the operations failed.
10276  *
10277  * This function cannot be called in interrupt context - it may sleep.
10278  *
10279  * NOTE: Should be only called by sata_probe_port() in case target port is a
10280  *       port multiplier port.
10281  * NOTE: No Mutex should be hold.
10282  */
10283 static int
10284 sata_reprobe_pmport(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
10285     int flag)
10286 {
10287 	sata_cport_info_t *cportinfo = NULL;
10288 	sata_pmport_info_t *pmportinfo = NULL;
10289 	sata_drive_info_t *sdinfo, *osdinfo;
10290 	sata_device_t sdevice;
10291 	boolean_t init_device = B_FALSE;
10292 	int prev_device_type = SATA_DTYPE_NONE;
10293 	int prev_device_settings = 0;
10294 	int prev_device_state = 0;
10295 	clock_t start_time;
10296 	uint8_t cport = sata_device->satadev_addr.cport;
10297 	uint8_t pmport = sata_device->satadev_addr.pmport;
10298 	int rval;
10299 
10300 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
10301 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
10302 	osdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
10303 
10304 	if (osdinfo != NULL) {
10305 		/*
10306 		 * We are re-probing port with a previously attached device.
10307 		 * Save previous device type and settings.
10308 		 */
10309 		prev_device_type = pmportinfo->pmport_dev_type;
10310 		prev_device_settings = osdinfo->satadrv_settings;
10311 		prev_device_state = osdinfo->satadrv_state;
10312 	}
10313 
10314 	start_time = ddi_get_lbolt();
10315 
10316 	/* check parent status */
10317 	mutex_enter(&cportinfo->cport_mutex);
10318 	if ((cportinfo->cport_state &
10319 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
10320 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
10321 	    SATA_PORT_DEVLINK_UP) {
10322 		mutex_exit(&cportinfo->cport_mutex);
10323 		return (SATA_FAILURE);
10324 	}
10325 	mutex_exit(&cportinfo->cport_mutex);
10326 
10327 retry_probe_pmport:
10328 
10329 	/* probe port */
10330 	mutex_enter(&pmportinfo->pmport_mutex);
10331 	pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
10332 	pmportinfo->pmport_state |= SATA_STATE_PROBING;
10333 	mutex_exit(&pmportinfo->pmport_mutex);
10334 
10335 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10336 	    (SATA_DIP(sata_hba_inst), sata_device);
10337 
10338 	/* might need retry because we cannot touch registers. */
10339 	if (rval == SATA_FAILURE) {
10340 		mutex_enter(&pmportinfo->pmport_mutex);
10341 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
10342 		mutex_exit(&pmportinfo->pmport_mutex);
10343 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: "
10344 		    "SATA port %d:%d probing failed",
10345 		    cport, pmport));
10346 		return (SATA_FAILURE);
10347 	} else if (rval == SATA_RETRY) {
10348 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_pmport: "
10349 		    "SATA port %d:%d probing failed, retrying...",
10350 		    cport, pmport));
10351 		clock_t cur_time = ddi_get_lbolt();
10352 		/*
10353 		 * A device was not successfully identified or initialized.
10354 		 * Track retry time for device identification.
10355 		 */
10356 		if ((cur_time - start_time) <
10357 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
10358 			/* sleep for a while */
10359 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
10360 			goto retry_probe_pmport;
10361 		} else {
10362 			mutex_enter(&pmportinfo->pmport_mutex);
10363 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL)
10364 				SATA_PMPORTINFO_DRV_INFO(pmportinfo)->
10365 				    satadrv_state = SATA_DSTATE_FAILED;
10366 			mutex_exit(&pmportinfo->pmport_mutex);
10367 			return (SATA_SUCCESS);
10368 		}
10369 	}
10370 
10371 	/*
10372 	 * Sanity check - Controller port is active? Is the link active?
10373 	 * Is it still a port multiplier?
10374 	 */
10375 	if ((cportinfo->cport_state &
10376 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
10377 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
10378 	    SATA_PORT_DEVLINK_UP ||
10379 	    (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) {
10380 		/*
10381 		 * Port in non-usable state or no link active/no
10382 		 * device. Free info structure.
10383 		 */
10384 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
10385 
10386 		sdevice.satadev_addr.cport = cport;
10387 		sdevice.satadev_addr.pmport = pmport;
10388 		sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
10389 		mutex_exit(&cportinfo->cport_mutex);
10390 
10391 		sata_free_pmult(sata_hba_inst, &sdevice);
10392 		return (SATA_FAILURE);
10393 	}
10394 
10395 	/* SATA_SUCCESS NOW */
10396 	/*
10397 	 * update sata port state and set device type
10398 	 */
10399 	mutex_enter(&pmportinfo->pmport_mutex);
10400 	sata_update_pmport_info(sata_hba_inst, sata_device);
10401 	pmportinfo->pmport_state &= ~SATA_STATE_PROBING;
10402 
10403 	/*
10404 	 * Sanity check - Port is active? Is the link active?
10405 	 * Is there any device attached?
10406 	 */
10407 	if ((pmportinfo->pmport_state &
10408 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
10409 	    (pmportinfo->pmport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
10410 	    SATA_PORT_DEVLINK_UP) {
10411 		/*
10412 		 * Port in non-usable state or no link active/no device.
10413 		 * Free info structure if necessary (direct attached drive
10414 		 * only, for now!
10415 		 */
10416 		sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
10417 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
10418 		/* Add here differentiation for device attached or not */
10419 		pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
10420 		mutex_exit(&pmportinfo->pmport_mutex);
10421 		if (sdinfo != NULL)
10422 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
10423 		return (SATA_SUCCESS);
10424 	}
10425 
10426 	pmportinfo->pmport_state |= SATA_STATE_READY;
10427 	pmportinfo->pmport_dev_type = sata_device->satadev_type;
10428 	sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
10429 
10430 	/*
10431 	 * If we are re-probing the port, there may be
10432 	 * sata_drive_info structure attached
10433 	 * (or sata_pm_info, if PMult is supported).
10434 	 */
10435 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
10436 		/*
10437 		 * There is no device, so remove device info structure,
10438 		 * if necessary.
10439 		 */
10440 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
10441 		pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
10442 		if (sdinfo != NULL) {
10443 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
10444 			sata_log(sata_hba_inst, CE_WARN,
10445 			    "SATA device detached from port %d:%d",
10446 			    cport, pmport);
10447 		}
10448 		mutex_exit(&pmportinfo->pmport_mutex);
10449 		return (SATA_SUCCESS);
10450 	}
10451 
10452 	/* this should not be a pmult */
10453 	ASSERT(sata_device->satadev_type != SATA_DTYPE_PMULT);
10454 	if (sdinfo == NULL) {
10455 		/*
10456 		 * There is some device attached, but there is
10457 		 * no sata_drive_info structure - allocate one
10458 		 */
10459 		mutex_exit(&pmportinfo->pmport_mutex);
10460 		sdinfo = kmem_zalloc(sizeof (sata_drive_info_t),
10461 		    KM_SLEEP);
10462 		mutex_enter(&pmportinfo->pmport_mutex);
10463 		/*
10464 		 * Recheck, that the port state did not change when we
10465 		 * released mutex.
10466 		 */
10467 		if (pmportinfo->pmport_state & SATA_STATE_READY) {
10468 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = sdinfo;
10469 			sdinfo->satadrv_addr = pmportinfo->pmport_addr;
10470 			sdinfo->satadrv_addr.qual = SATA_ADDR_DPMPORT;
10471 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
10472 			sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
10473 		} else {
10474 			/*
10475 			 * Port is not in ready state, we
10476 			 * cannot attach a device.
10477 			 */
10478 			mutex_exit(&pmportinfo->pmport_mutex);
10479 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
10480 			return (SATA_SUCCESS);
10481 		}
10482 		/*
10483 		 * Since we are adding device, presumably new one,
10484 		 * indicate that it  should be initalized,
10485 		 * as well as some internal framework states).
10486 		 */
10487 		init_device = B_TRUE;
10488 	}
10489 
10490 	pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
10491 	sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
10492 
10493 	mutex_exit(&pmportinfo->pmport_mutex);
10494 	/*
10495 	 * Figure out what kind of device we are really
10496 	 * dealing with.
10497 	 */
10498 	rval = sata_probe_device(sata_hba_inst, sata_device);
10499 
10500 	mutex_enter(&pmportinfo->pmport_mutex);
10501 	if (rval == SATA_SUCCESS) {
10502 		/*
10503 		 * If we are dealing with the same type of a device as before,
10504 		 * restore its settings flags.
10505 		 */
10506 		if (osdinfo != NULL &&
10507 		    sata_device->satadev_type == prev_device_type)
10508 			sdinfo->satadrv_settings = prev_device_settings;
10509 
10510 		mutex_exit(&pmportinfo->pmport_mutex);
10511 		/* Set initial device features, if necessary */
10512 		if (init_device == B_TRUE) {
10513 			rval = sata_initialize_device(sata_hba_inst, sdinfo);
10514 		}
10515 		if (rval == SATA_SUCCESS)
10516 			return (rval);
10517 	} else {
10518 		/*
10519 		 * If there was some device info before we probe the device,
10520 		 * restore previous device setting, so we can retry from scratch
10521 		 * later. Providing, of course, that device has not disappeared
10522 		 * during probing process.
10523 		 */
10524 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
10525 			if (osdinfo != NULL) {
10526 				pmportinfo->pmport_dev_type = prev_device_type;
10527 				sdinfo->satadrv_type = prev_device_type;
10528 				sdinfo->satadrv_state = prev_device_state;
10529 			}
10530 		} else {
10531 			/* device is gone */
10532 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
10533 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
10534 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
10535 			mutex_exit(&pmportinfo->pmport_mutex);
10536 			return (SATA_SUCCESS);
10537 		}
10538 		mutex_exit(&pmportinfo->pmport_mutex);
10539 	}
10540 
10541 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
10542 		clock_t cur_time = ddi_get_lbolt();
10543 		/*
10544 		 * A device was not successfully identified or initialized.
10545 		 * Track retry time for device identification.
10546 		 */
10547 		if ((cur_time - start_time) <
10548 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
10549 			/* sleep for a while */
10550 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
10551 			goto retry_probe_pmport;
10552 		} else {
10553 			mutex_enter(&pmportinfo->pmport_mutex);
10554 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL)
10555 				SATA_PMPORTINFO_DRV_INFO(pmportinfo)->
10556 				    satadrv_state = SATA_DSTATE_FAILED;
10557 			mutex_exit(&pmportinfo->pmport_mutex);
10558 		}
10559 	}
10560 	return (SATA_SUCCESS);
10561 }
10562 
10563 /*
10564  * Allocated related structure for a port multiplier and its device ports
10565  *
10566  * Port multiplier should be ready and probed, and related information like
10567  * the number of the device ports should be store in sata_device_t.
10568  *
10569  * NOTE: No Mutex should be hold.
10570  */
10571 static void
10572 sata_alloc_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
10573 {
10574 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
10575 	sata_cport_info_t *cportinfo = NULL;
10576 	sata_pmult_info_t *pmultinfo = NULL;
10577 	sata_pmport_info_t *pmportinfo = NULL;
10578 	dev_t minor_number;
10579 	char name[16];
10580 	uint8_t cport = sata_device->satadev_addr.cport;
10581 	int npmport;
10582 
10583 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
10584 
10585 	/* This function might be called while a port-mult is hot-plugged. */
10586 	mutex_enter(&cportinfo->cport_mutex);
10587 
10588 	/* dev_type's not updated when get called from sata_reprobe_port() */
10589 	cportinfo->cport_dev_type = SATA_DTYPE_PMULT;
10590 	if (SATA_CPORTINFO_PMULT_INFO(cportinfo) == NULL) {
10591 		/* Create a pmult_info structure */
10592 		SATA_CPORTINFO_PMULT_INFO(cportinfo) =
10593 		    kmem_zalloc(sizeof (sata_pmult_info_t), KM_SLEEP);
10594 	}
10595 	pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
10596 
10597 	pmultinfo->pmult_addr = sata_device->satadev_addr;
10598 	pmultinfo->pmult_addr.qual = SATA_ADDR_PMULT;
10599 	pmultinfo->pmult_state = SATA_STATE_PROBING;
10600 	pmultinfo->pmult_gscr = sata_device->satadev_gscr;
10601 	pmultinfo->pmult_num_dev_ports = sata_device->satadev_add_info;
10602 
10603 	/* Initialize pmport_info structure */
10604 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports;
10605 	    npmport++) {
10606 
10607 		/* if everything is allocated, skip */
10608 		if (SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) != NULL)
10609 			continue;
10610 
10611 		pmportinfo = kmem_zalloc(sizeof (sata_pmport_info_t), KM_SLEEP);
10612 		mutex_init(&pmportinfo->pmport_mutex, NULL, MUTEX_DRIVER, NULL);
10613 		mutex_exit(&cportinfo->cport_mutex);
10614 
10615 		mutex_enter(&pmportinfo->pmport_mutex);
10616 		pmportinfo->pmport_addr.cport = cport;
10617 		pmportinfo->pmport_addr.pmport = (uint8_t)npmport;
10618 		pmportinfo->pmport_addr.qual = SATA_ADDR_PMPORT;
10619 		pmportinfo->pmport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
10620 		mutex_exit(&pmportinfo->pmport_mutex);
10621 
10622 		mutex_enter(&cportinfo->cport_mutex);
10623 		SATA_PMPORT_INFO(sata_hba_inst, cport, npmport) = pmportinfo;
10624 
10625 		/* Create an attachment point */
10626 		minor_number = SATA_MAKE_AP_MINOR(ddi_get_instance(dip),
10627 		    cport, (uint8_t)npmport, SATA_ADDR_PMPORT);
10628 		(void) sprintf(name, "%d.%d", cport, npmport);
10629 
10630 		if (ddi_create_minor_node(dip, name, S_IFCHR, minor_number,
10631 		    DDI_NT_SATA_ATTACHMENT_POINT, 0) != DDI_SUCCESS) {
10632 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
10633 			    "cannot create SATA attachment point for "
10634 			    "port %d:%d", cport, npmport);
10635 		}
10636 	}
10637 
10638 	pmultinfo->pmult_state &= ~SATA_STATE_PROBING;
10639 	pmultinfo->pmult_state |= (SATA_STATE_PROBED|SATA_STATE_READY);
10640 
10641 	mutex_exit(&cportinfo->cport_mutex);
10642 }
10643 
10644 /*
10645  * Free data structures when a port multiplier is removed.
10646  *
10647  * NOTE: No Mutex should be hold.
10648  */
10649 static void
10650 sata_free_pmult(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
10651 {
10652 	sata_cport_info_t *cportinfo;
10653 	sata_pmult_info_t *pmultinfo;
10654 	sata_pmport_info_t *pmportinfo;
10655 	sata_device_t pmport_device;
10656 	sata_drive_info_t *sdinfo;
10657 	dev_info_t *tdip;
10658 	char name[16];
10659 	uint8_t cport = sata_device->satadev_addr.cport;
10660 	int npmport;
10661 
10662 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
10663 
10664 	/* This function might be called while port-mult is hot plugged. */
10665 	mutex_enter(&cportinfo->cport_mutex);
10666 
10667 	pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
10668 	ASSERT(pmultinfo != NULL);
10669 
10670 	/* Free pmport_info structure */
10671 	for (npmport = 0; npmport < pmultinfo->pmult_num_dev_ports;
10672 	    npmport++) {
10673 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport);
10674 		if (pmportinfo == NULL)
10675 			continue;
10676 		mutex_exit(&cportinfo->cport_mutex);
10677 
10678 		mutex_enter(&pmportinfo->pmport_mutex);
10679 		sdinfo = pmportinfo->pmport_sata_drive;
10680 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
10681 		mutex_exit(&pmportinfo->pmport_mutex);
10682 
10683 		/* Remove attachment point. */
10684 		name[0] = '\0';
10685 		(void) sprintf(name, "%d.%d", cport, npmport);
10686 		ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name);
10687 		sata_log(sata_hba_inst, CE_NOTE,
10688 		    "Remove attachment point of port %d:%d",
10689 		    cport, npmport);
10690 
10691 		/*
10692 		 * Rumove target node
10693 		 */
10694 		bzero(&pmport_device, sizeof (sata_device_t));
10695 		pmport_device.satadev_rev = SATA_DEVICE_REV;
10696 		pmport_device.satadev_addr.cport = cport;
10697 		pmport_device.satadev_addr.pmport = (uint8_t)npmport;
10698 		pmport_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
10699 
10700 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
10701 		    &(pmport_device.satadev_addr));
10702 		if (tdip != NULL && ndi_devi_offline(tdip,
10703 		    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
10704 			/*
10705 			 * Problem :
10706 			 * The target node remained attached.
10707 			 * This happens when the device file was open
10708 			 * or a node was waiting for resources.
10709 			 * Cannot do anything about it.
10710 			 */
10711 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10712 			    "sata_free_pmult: could not unconfigure device "
10713 			    "before disconnecting the SATA port %d:%d",
10714 			    cport, npmport));
10715 
10716 			/*
10717 			 * Set DEVICE REMOVED state in the target
10718 			 * node. It will prevent access to the device
10719 			 * even when a new device is attached, until
10720 			 * the old target node is released, removed and
10721 			 * recreated for a new  device.
10722 			 */
10723 			sata_set_device_removed(tdip);
10724 
10725 			/*
10726 			 * Instruct event daemon to try the target
10727 			 * node cleanup later.
10728 			 */
10729 			sata_set_target_node_cleanup(
10730 			    sata_hba_inst, &(pmport_device.satadev_addr));
10731 
10732 		}
10733 		mutex_enter(&cportinfo->cport_mutex);
10734 
10735 		/*
10736 		 * Add here differentiation for device attached or not
10737 		 */
10738 		if (sdinfo != NULL)  {
10739 			sata_log(sata_hba_inst, CE_WARN,
10740 			    "SATA device detached from port %d:%d",
10741 			    cport, npmport);
10742 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
10743 		}
10744 
10745 		mutex_destroy(&pmportinfo->pmport_mutex);
10746 		kmem_free(pmportinfo, sizeof (sata_pmport_info_t));
10747 	}
10748 
10749 	kmem_free(pmultinfo, sizeof (sata_pmult_info_t));
10750 
10751 	cportinfo->cport_devp.cport_sata_pmult = NULL;
10752 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
10753 
10754 	sata_log(sata_hba_inst, CE_WARN,
10755 	    "SATA port multiplier detached at port %d", cport);
10756 
10757 	mutex_exit(&cportinfo->cport_mutex);
10758 }
10759 
10760 /*
10761  * Initialize device
10762  * Specified device is initialized to a default state.
10763  *
10764  * Returns SATA_SUCCESS if all device features are set successfully,
10765  * SATA_RETRY if device is accessible but device features were not set
10766  * successfully, and SATA_FAILURE otherwise.
10767  */
10768 static int
10769 sata_initialize_device(sata_hba_inst_t *sata_hba_inst,
10770     sata_drive_info_t *sdinfo)
10771 {
10772 	int rval;
10773 
10774 	sata_save_drive_settings(sdinfo);
10775 
10776 	sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
10777 
10778 	sata_init_write_cache_mode(sdinfo);
10779 
10780 	rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0);
10781 
10782 	/* Determine current data transfer mode */
10783 	if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) {
10784 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
10785 	} else if ((sdinfo->satadrv_id.ai_validinfo &
10786 	    SATA_VALIDINFO_88) != 0 &&
10787 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) {
10788 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
10789 	} else if ((sdinfo->satadrv_id.ai_dworddma &
10790 	    SATA_MDMA_SEL_MASK) != 0) {
10791 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
10792 	} else
10793 		/* DMA supported, not no DMA transfer mode is selected !? */
10794 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
10795 
10796 	if ((sdinfo->satadrv_id.ai_cmdset83 & 0x20) &&
10797 	    (sdinfo->satadrv_id.ai_features86 & 0x20))
10798 		sdinfo->satadrv_power_level = SATA_POWER_STANDBY;
10799 
10800 	return (rval);
10801 }
10802 
10803 
10804 /*
10805  * Initialize write cache mode.
10806  *
10807  * The default write cache setting for SATA HDD is provided by sata_write_cache
10808  * static variable. ATAPI CD/DVDs devices have write cache default is
10809  * determined by sata_atapicdvd_write_cache static variable.
10810  * ATAPI tape devices have write cache default is determined by
10811  * sata_atapitape_write_cache static variable.
10812  * ATAPI disk devices have write cache default is determined by
10813  * sata_atapidisk_write_cache static variable.
10814  * 1 - enable
10815  * 0 - disable
10816  * any other value - current drive setting
10817  *
10818  * Although there is not reason to disable write cache on CD/DVD devices,
10819  * tape devices and ATAPI disk devices, the default setting control is provided
10820  * for the maximun flexibility.
10821  *
10822  * In the future, it may be overridden by the
10823  * disk-write-cache-enable property setting, if it is defined.
10824  * Returns SATA_SUCCESS if all device features are set successfully,
10825  * SATA_FAILURE otherwise.
10826  */
10827 static void
10828 sata_init_write_cache_mode(sata_drive_info_t *sdinfo)
10829 {
10830 	switch (sdinfo->satadrv_type) {
10831 	case SATA_DTYPE_ATADISK:
10832 		if (sata_write_cache == 1)
10833 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
10834 		else if (sata_write_cache == 0)
10835 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
10836 		/*
10837 		 * When sata_write_cache value is not 0 or 1,
10838 		 * a current setting of the drive's write cache is used.
10839 		 */
10840 		break;
10841 	case SATA_DTYPE_ATAPICD:
10842 		if (sata_atapicdvd_write_cache == 1)
10843 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
10844 		else if (sata_atapicdvd_write_cache == 0)
10845 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
10846 		/*
10847 		 * When sata_atapicdvd_write_cache value is not 0 or 1,
10848 		 * a current setting of the drive's write cache is used.
10849 		 */
10850 		break;
10851 	case SATA_DTYPE_ATAPITAPE:
10852 		if (sata_atapitape_write_cache == 1)
10853 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
10854 		else if (sata_atapitape_write_cache == 0)
10855 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
10856 		/*
10857 		 * When sata_atapitape_write_cache value is not 0 or 1,
10858 		 * a current setting of the drive's write cache is used.
10859 		 */
10860 		break;
10861 	case SATA_DTYPE_ATAPIDISK:
10862 		if (sata_atapidisk_write_cache == 1)
10863 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
10864 		else if (sata_atapidisk_write_cache == 0)
10865 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
10866 		/*
10867 		 * When sata_atapidisk_write_cache value is not 0 or 1,
10868 		 * a current setting of the drive's write cache is used.
10869 		 */
10870 		break;
10871 	}
10872 }
10873 
10874 
10875 /*
10876  * Validate sata address.
10877  * Specified cport, pmport and qualifier has to match
10878  * passed sata_scsi configuration info.
10879  * The presence of an attached device is not verified.
10880  *
10881  * Returns 0 when address is valid, -1 otherwise.
10882  */
10883 static int
10884 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport,
10885 	int pmport, int qual)
10886 {
10887 	if (qual == SATA_ADDR_DCPORT && pmport != 0)
10888 		goto invalid_address;
10889 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
10890 		goto invalid_address;
10891 	if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) &&
10892 	    ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) ||
10893 	    (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) ||
10894 	    (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport))))
10895 		goto invalid_address;
10896 
10897 	return (0);
10898 
10899 invalid_address:
10900 	return (-1);
10901 
10902 }
10903 
10904 /*
10905  * Validate scsi address
10906  * SCSI target address is translated into SATA cport/pmport and compared
10907  * with a controller port/device configuration. LUN has to be 0.
10908  * Returns 0 if a scsi target refers to an attached device,
10909  * returns 1 if address is valid but no valid device is attached,
10910  * returns 2 if address is valid but device type is unknown (not valid device),
10911  * returns -1 if bad address or device is of an unsupported type.
10912  * Upon return sata_device argument is set.
10913  *
10914  * Port multiplier is supported now.
10915  */
10916 static int
10917 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst,
10918 	struct scsi_address *ap, sata_device_t *sata_device)
10919 {
10920 	int cport, pmport, qual, rval;
10921 
10922 	rval = -1;	/* Invalid address */
10923 	if (ap->a_lun != 0)
10924 		goto out;
10925 
10926 	qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
10927 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
10928 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
10929 
10930 	if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT)
10931 		goto out;
10932 
10933 	if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) ==
10934 	    0) {
10935 
10936 		sata_cport_info_t *cportinfo;
10937 		sata_pmult_info_t *pmultinfo;
10938 		sata_drive_info_t *sdinfo = NULL;
10939 
10940 		sata_device->satadev_addr.qual = qual;
10941 		sata_device->satadev_addr.cport = cport;
10942 		sata_device->satadev_addr.pmport = pmport;
10943 		sata_device->satadev_rev = SATA_DEVICE_REV_1;
10944 
10945 		rval = 1;	/* Valid sata address */
10946 
10947 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
10948 		if (qual == SATA_ADDR_DCPORT) {
10949 			if (cportinfo == NULL ||
10950 			    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
10951 				goto out;
10952 
10953 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
10954 			if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN &&
10955 			    sdinfo != NULL) {
10956 				rval = 2;
10957 				goto out;
10958 			}
10959 
10960 			if ((cportinfo->cport_dev_type &
10961 			    SATA_VALID_DEV_TYPE) == 0) {
10962 				rval = -1;
10963 				goto out;
10964 			}
10965 
10966 		} else if (qual == SATA_ADDR_DPMPORT) {
10967 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
10968 			if (pmultinfo == NULL) {
10969 				rval = -1;
10970 				goto out;
10971 			}
10972 			if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) ==
10973 			    NULL ||
10974 			    SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
10975 			    pmport) == SATA_DTYPE_NONE)
10976 				goto out;
10977 
10978 			sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport,
10979 			    pmport);
10980 			if (SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
10981 			    pmport) == SATA_DTYPE_UNKNOWN && sdinfo != NULL) {
10982 				rval = 2;
10983 				goto out;
10984 			}
10985 
10986 			if ((SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
10987 			    pmport) && SATA_VALID_DEV_TYPE) == 0) {
10988 				rval = -1;
10989 				goto out;
10990 			}
10991 
10992 		} else {
10993 			rval = -1;
10994 			goto out;
10995 		}
10996 		if ((sdinfo == NULL) ||
10997 		    (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0)
10998 			goto out;
10999 
11000 		sata_device->satadev_type = sdinfo->satadrv_type;
11001 
11002 		return (0);
11003 	}
11004 out:
11005 	if (rval > 0) {
11006 		SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
11007 		    "sata_validate_scsi_address: no valid target %x lun %x",
11008 		    ap->a_target, ap->a_lun);
11009 	}
11010 	return (rval);
11011 }
11012 
11013 /*
11014  * Find dip corresponding to passed device number
11015  *
11016  * Returns NULL if invalid device number is passed or device cannot be found,
11017  * Returns dip is device is found.
11018  */
11019 static dev_info_t *
11020 sata_devt_to_devinfo(dev_t dev)
11021 {
11022 	dev_info_t *dip;
11023 #ifndef __lock_lint
11024 	struct devnames *dnp;
11025 	major_t major = getmajor(dev);
11026 	int instance = SATA_MINOR2INSTANCE(getminor(dev));
11027 
11028 	if (major >= devcnt)
11029 		return (NULL);
11030 
11031 	dnp = &devnamesp[major];
11032 	LOCK_DEV_OPS(&(dnp->dn_lock));
11033 	dip = dnp->dn_head;
11034 	while (dip && (ddi_get_instance(dip) != instance)) {
11035 		dip = ddi_get_next(dip);
11036 	}
11037 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
11038 #endif
11039 
11040 	return (dip);
11041 }
11042 
11043 
11044 /*
11045  * Probe device.
11046  * This function issues Identify Device command and initializes local
11047  * sata_drive_info structure if the device can be identified.
11048  * The device type is determined by examining Identify Device
11049  * command response.
11050  * If the sata_hba_inst has linked drive info structure for this
11051  * device address, the Identify Device data is stored into sata_drive_info
11052  * structure linked to the port info structure.
11053  *
11054  * sata_device has to refer to the valid sata port(s) for HBA described
11055  * by sata_hba_inst structure.
11056  *
11057  * Returns:
11058  *	SATA_SUCCESS if device type was successfully probed and port-linked
11059  *		drive info structure was updated;
11060  * 	SATA_FAILURE if there is no device, or device was not probed
11061  *		successully;
11062  *	SATA_RETRY if device probe can be retried later.
11063  * If a device cannot be identified, sata_device's dev_state and dev_type
11064  * fields are set to unknown.
11065  * There are no retries in this function. Any retries should be managed by
11066  * the caller.
11067  */
11068 
11069 
11070 static int
11071 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
11072 {
11073 	sata_pmport_info_t *pmportinfo;
11074 	sata_drive_info_t *sdinfo;
11075 	sata_drive_info_t new_sdinfo;	/* local drive info struct */
11076 	int rval;
11077 
11078 	ASSERT((SATA_CPORT_STATE(sata_hba_inst,
11079 	    sata_device->satadev_addr.cport) &
11080 	    (SATA_STATE_PROBED | SATA_STATE_READY)) != 0);
11081 
11082 	sata_device->satadev_type = SATA_DTYPE_NONE;
11083 
11084 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
11085 	    sata_device->satadev_addr.cport)));
11086 
11087 	if (sata_device->satadev_addr.qual == SATA_ADDR_DPMPORT) {
11088 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
11089 		    sata_device->satadev_addr.cport,
11090 		    sata_device->satadev_addr.pmport);
11091 		ASSERT(pmportinfo != NULL);
11092 	}
11093 
11094 	/* Get pointer to port-linked sata device info structure */
11095 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
11096 	if (sdinfo != NULL) {
11097 		sdinfo->satadrv_state &=
11098 		    ~(SATA_STATE_PROBED | SATA_STATE_READY);
11099 		sdinfo->satadrv_state |= SATA_STATE_PROBING;
11100 	} else {
11101 		/* No device to probe */
11102 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
11103 		    sata_device->satadev_addr.cport)));
11104 		sata_device->satadev_type = SATA_DTYPE_NONE;
11105 		sata_device->satadev_state = SATA_STATE_UNKNOWN;
11106 		return (SATA_FAILURE);
11107 	}
11108 	/*
11109 	 * Need to issue both types of identify device command and
11110 	 * determine device type by examining retreived data/status.
11111 	 * First, ATA Identify Device.
11112 	 */
11113 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
11114 	new_sdinfo.satadrv_addr = sata_device->satadev_addr;
11115 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
11116 	    sata_device->satadev_addr.cport)));
11117 	new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
11118 	rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
11119 	if (rval == SATA_RETRY) {
11120 		/* We may try to check for ATAPI device */
11121 		if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) {
11122 			/*
11123 			 * HBA supports ATAPI - try to issue Identify Packet
11124 			 * Device command.
11125 			 */
11126 			new_sdinfo.satadrv_type = SATA_DTYPE_ATAPI;
11127 			rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
11128 		}
11129 	}
11130 	if (rval == SATA_SUCCESS) {
11131 		/*
11132 		 * Got something responding positively to ATA Identify Device
11133 		 * or to Identify Packet Device cmd.
11134 		 * Save last used device type.
11135 		 */
11136 		sata_device->satadev_type = new_sdinfo.satadrv_type;
11137 
11138 		/* save device info, if possible */
11139 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
11140 		    sata_device->satadev_addr.cport)));
11141 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
11142 		if (sdinfo == NULL) {
11143 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
11144 			    sata_device->satadev_addr.cport)));
11145 			return (SATA_FAILURE);
11146 		}
11147 		/*
11148 		 * Copy drive info into the port-linked drive info structure.
11149 		 */
11150 		*sdinfo = new_sdinfo;
11151 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
11152 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
11153 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
11154 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
11155 			    sata_device->satadev_addr.cport) =
11156 			    sdinfo->satadrv_type;
11157 		else { /* SATA_ADDR_DPMPORT */
11158 			mutex_enter(&pmportinfo->pmport_mutex);
11159 			SATA_PMPORT_DEV_TYPE(sata_hba_inst,
11160 			    sata_device->satadev_addr.cport,
11161 			    sata_device->satadev_addr.pmport) =
11162 			    sdinfo->satadrv_type;
11163 			mutex_exit(&pmportinfo->pmport_mutex);
11164 		}
11165 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
11166 		    sata_device->satadev_addr.cport)));
11167 		return (SATA_SUCCESS);
11168 	}
11169 
11170 	/*
11171 	 * It may be SATA_RETRY or SATA_FAILURE return.
11172 	 * Looks like we cannot determine the device type at this time.
11173 	 */
11174 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
11175 	    sata_device->satadev_addr.cport)));
11176 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
11177 	if (sdinfo != NULL) {
11178 		sata_device->satadev_type = SATA_DTYPE_UNKNOWN;
11179 		sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11180 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
11181 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
11182 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
11183 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
11184 			    sata_device->satadev_addr.cport) =
11185 			    SATA_DTYPE_UNKNOWN;
11186 		else {
11187 			/* SATA_ADDR_DPMPORT */
11188 			mutex_enter(&pmportinfo->pmport_mutex);
11189 			if ((SATA_PMULT_INFO(sata_hba_inst,
11190 			    sata_device->satadev_addr.cport) != NULL) &&
11191 			    (SATA_PMPORT_INFO(sata_hba_inst,
11192 			    sata_device->satadev_addr.cport,
11193 			    sata_device->satadev_addr.pmport) != NULL))
11194 				SATA_PMPORT_DEV_TYPE(sata_hba_inst,
11195 				    sata_device->satadev_addr.cport,
11196 				    sata_device->satadev_addr.pmport) =
11197 				    SATA_DTYPE_UNKNOWN;
11198 			mutex_exit(&pmportinfo->pmport_mutex);
11199 		}
11200 	}
11201 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
11202 	    sata_device->satadev_addr.cport)));
11203 	return (rval);
11204 }
11205 
11206 
11207 /*
11208  * Get pointer to sata_drive_info structure.
11209  *
11210  * The sata_device has to contain address (cport, pmport and qualifier) for
11211  * specified sata_scsi structure.
11212  *
11213  * Returns NULL if device address is not valid for this HBA configuration.
11214  * Otherwise, returns a pointer to sata_drive_info structure.
11215  *
11216  * This function should be called with a port mutex held.
11217  */
11218 static sata_drive_info_t *
11219 sata_get_device_info(sata_hba_inst_t *sata_hba_inst,
11220     sata_device_t *sata_device)
11221 {
11222 	uint8_t cport = sata_device->satadev_addr.cport;
11223 	uint8_t pmport = sata_device->satadev_addr.pmport;
11224 	uint8_t qual = sata_device->satadev_addr.qual;
11225 
11226 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
11227 		return (NULL);
11228 
11229 	if (!(SATA_CPORT_STATE(sata_hba_inst, cport) &
11230 	    (SATA_STATE_PROBED | SATA_STATE_READY)))
11231 		/* Port not probed yet */
11232 		return (NULL);
11233 
11234 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE)
11235 		return (NULL);
11236 
11237 	if (qual == SATA_ADDR_DCPORT) {
11238 		/* Request for a device on a controller port */
11239 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
11240 		    SATA_DTYPE_PMULT)
11241 			/* Port multiplier attached */
11242 			return (NULL);
11243 		return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport));
11244 	}
11245 	if (qual == SATA_ADDR_DPMPORT) {
11246 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
11247 		    SATA_DTYPE_PMULT)
11248 			return (NULL);
11249 
11250 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport))
11251 			return (NULL);
11252 
11253 		if (!(SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) &
11254 		    (SATA_STATE_PROBED | SATA_STATE_READY)))
11255 			/* Port multiplier port not probed yet */
11256 			return (NULL);
11257 
11258 		return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport));
11259 	}
11260 
11261 	/* we should not get here */
11262 	return (NULL);
11263 }
11264 
11265 
11266 /*
11267  * sata_identify_device.
11268  * Send Identify Device command to SATA HBA driver.
11269  * If command executes successfully, update sata_drive_info structure pointed
11270  * to by sdinfo argument, including Identify Device data.
11271  * If command fails, invalidate data in sata_drive_info.
11272  *
11273  * Cannot be called from interrupt level.
11274  *
11275  * Returns:
11276  * SATA_SUCCESS if the device was identified as a supported device,
11277  * SATA_RETRY if the device was not identified but could be retried,
11278  * SATA_FAILURE if the device was not identified and identify attempt
11279  *	should not be retried.
11280  */
11281 static int
11282 sata_identify_device(sata_hba_inst_t *sata_hba_inst,
11283     sata_drive_info_t *sdinfo)
11284 {
11285 	uint16_t cfg_word;
11286 	int rval;
11287 
11288 	/* fetch device identify data */
11289 	if ((rval = sata_fetch_device_identify_data(sata_hba_inst,
11290 	    sdinfo)) != SATA_SUCCESS)
11291 		goto fail_unknown;
11292 
11293 	cfg_word = sdinfo->satadrv_id.ai_config;
11294 
11295 	/* Set the correct device type */
11296 	if ((cfg_word & SATA_ATA_TYPE_MASK) == SATA_ATA_TYPE) {
11297 		sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
11298 	} else if (cfg_word == SATA_CFA_TYPE) {
11299 		/* It's a Compact Flash media via CF-to-SATA HDD adapter */
11300 		sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
11301 	} else if ((cfg_word & SATA_ATAPI_TYPE_MASK) == SATA_ATAPI_TYPE) {
11302 		switch (cfg_word & SATA_ATAPI_ID_DEV_TYPE) {
11303 		case SATA_ATAPI_CDROM_DEV:
11304 			sdinfo->satadrv_type = SATA_DTYPE_ATAPICD;
11305 			break;
11306 		case SATA_ATAPI_SQACC_DEV:
11307 			sdinfo->satadrv_type = SATA_DTYPE_ATAPITAPE;
11308 			break;
11309 		case SATA_ATAPI_DIRACC_DEV:
11310 			sdinfo->satadrv_type = SATA_DTYPE_ATAPIDISK;
11311 			break;
11312 		default:
11313 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11314 		}
11315 	} else {
11316 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11317 	}
11318 
11319 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
11320 		if (sdinfo->satadrv_capacity == 0) {
11321 			/* Non-LBA disk. Too bad... */
11322 			sata_log(sata_hba_inst, CE_WARN,
11323 			    "SATA disk device at port %d does not support LBA",
11324 			    sdinfo->satadrv_addr.cport);
11325 			rval = SATA_FAILURE;
11326 			goto fail_unknown;
11327 		}
11328 	}
11329 #if 0
11330 	/* Left for historical reason */
11331 	/*
11332 	 * Some initial version of SATA spec indicated that at least
11333 	 * UDMA mode 4 has to be supported. It is not metioned in
11334 	 * SerialATA 2.6, so this restriction is removed.
11335 	 */
11336 	/* Check for Ultra DMA modes 6 through 0 being supported */
11337 	for (i = 6; i >= 0; --i) {
11338 		if (sdinfo->satadrv_id.ai_ultradma & (1 << i))
11339 			break;
11340 	}
11341 
11342 	/*
11343 	 * At least UDMA 4 mode has to be supported. If mode 4 or
11344 	 * higher are not supported by the device, fail this
11345 	 * device.
11346 	 */
11347 	if (i < 4) {
11348 		/* No required Ultra DMA mode supported */
11349 		sata_log(sata_hba_inst, CE_WARN,
11350 		    "SATA disk device at port %d does not support UDMA "
11351 		    "mode 4 or higher", sdinfo->satadrv_addr.cport);
11352 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11353 		    "mode 4 or higher required, %d supported", i));
11354 		rval = SATA_FAILURE;
11355 		goto fail_unknown;
11356 	}
11357 #endif
11358 
11359 	/*
11360 	 * For Disk devices, if it doesn't support UDMA mode, we would
11361 	 * like to return failure directly.
11362 	 */
11363 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
11364 	    !((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
11365 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0)) {
11366 		sata_log(sata_hba_inst, CE_WARN,
11367 		    "SATA disk device at port %d does not support UDMA",
11368 		    sdinfo->satadrv_addr.cport);
11369 		rval = SATA_FAILURE;
11370 		goto fail_unknown;
11371 	}
11372 
11373 	return (SATA_SUCCESS);
11374 
11375 fail_unknown:
11376 	/* Invalidate sata_drive_info ? */
11377 	sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
11378 	sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
11379 	return (rval);
11380 }
11381 
11382 /*
11383  * Log/display device information
11384  */
11385 static void
11386 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst,
11387     sata_drive_info_t *sdinfo)
11388 {
11389 	int valid_version;
11390 	char msg_buf[MAXPATHLEN];
11391 	int i;
11392 
11393 	/* Show HBA path */
11394 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf);
11395 
11396 	cmn_err(CE_CONT, "?%s :\n", msg_buf);
11397 
11398 	switch (sdinfo->satadrv_type) {
11399 	case SATA_DTYPE_ATADISK:
11400 		(void) sprintf(msg_buf, "SATA disk device at");
11401 		break;
11402 
11403 	case SATA_DTYPE_ATAPICD:
11404 		(void) sprintf(msg_buf, "SATA CD/DVD (ATAPI) device at");
11405 		break;
11406 
11407 	case SATA_DTYPE_ATAPITAPE:
11408 		(void) sprintf(msg_buf, "SATA tape (ATAPI) device at");
11409 		break;
11410 
11411 	case SATA_DTYPE_ATAPIDISK:
11412 		(void) sprintf(msg_buf, "SATA disk (ATAPI) device at");
11413 		break;
11414 
11415 	case SATA_DTYPE_UNKNOWN:
11416 		(void) sprintf(msg_buf,
11417 		    "Unsupported SATA device type (cfg 0x%x) at ",
11418 		    sdinfo->satadrv_id.ai_config);
11419 		break;
11420 	}
11421 
11422 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT)
11423 		cmn_err(CE_CONT, "?\t%s port %d\n",
11424 		    msg_buf, sdinfo->satadrv_addr.cport);
11425 	else
11426 		cmn_err(CE_CONT, "?\t%s port %d:%d\n",
11427 		    msg_buf, sdinfo->satadrv_addr.cport,
11428 		    sdinfo->satadrv_addr.pmport);
11429 
11430 	bcopy(&sdinfo->satadrv_id.ai_model, msg_buf,
11431 	    sizeof (sdinfo->satadrv_id.ai_model));
11432 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model));
11433 	msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
11434 	cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf);
11435 
11436 	bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf,
11437 	    sizeof (sdinfo->satadrv_id.ai_fw));
11438 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw));
11439 	msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
11440 	cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf);
11441 
11442 	bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf,
11443 	    sizeof (sdinfo->satadrv_id.ai_drvser));
11444 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser));
11445 	msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
11446 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
11447 		cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
11448 	} else {
11449 		/*
11450 		 * Some drives do not implement serial number and may
11451 		 * violate the spec by providing spaces rather than zeros
11452 		 * in serial number field. Scan the buffer to detect it.
11453 		 */
11454 		for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) {
11455 			if (msg_buf[i] != '\0' && msg_buf[i] != ' ')
11456 				break;
11457 		}
11458 		if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) {
11459 			cmn_err(CE_CONT, "?\tserial number - none\n");
11460 		} else {
11461 			cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
11462 		}
11463 	}
11464 
11465 #ifdef SATA_DEBUG
11466 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
11467 	    sdinfo->satadrv_id.ai_majorversion != 0xffff) {
11468 		int i;
11469 		for (i = 14; i >= 2; i--) {
11470 			if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) {
11471 				valid_version = i;
11472 				break;
11473 			}
11474 		}
11475 		cmn_err(CE_CONT,
11476 		    "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
11477 		    valid_version,
11478 		    sdinfo->satadrv_id.ai_majorversion,
11479 		    sdinfo->satadrv_id.ai_minorversion);
11480 	}
11481 #endif
11482 	/* Log some info */
11483 	cmn_err(CE_CONT, "?\tsupported features:\n");
11484 	msg_buf[0] = '\0';
11485 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
11486 		if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48)
11487 			(void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN);
11488 		else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28)
11489 			(void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN);
11490 	}
11491 	if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA)
11492 		(void) strlcat(msg_buf, "DMA", MAXPATHLEN);
11493 	if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ)
11494 		(void) strlcat(msg_buf, ", Native Command Queueing",
11495 		    MAXPATHLEN);
11496 	if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)
11497 		(void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN);
11498 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) &&
11499 	    (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED))
11500 		(void) strlcat(msg_buf, ", SMART", MAXPATHLEN);
11501 	if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) &&
11502 	    (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED))
11503 		(void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN);
11504 	cmn_err(CE_CONT, "?\t %s\n", msg_buf);
11505 	if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2)
11506 		cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n");
11507 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1)
11508 		cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n");
11509 	if (sdinfo->satadrv_features_support &
11510 	    (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) {
11511 		msg_buf[0] = '\0';
11512 		(void) snprintf(msg_buf, MAXPATHLEN,
11513 		    "Supported queue depth %d",
11514 		    sdinfo->satadrv_queue_depth);
11515 		if (!(sata_func_enable &
11516 		    (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ)))
11517 			(void) strlcat(msg_buf,
11518 			    " - queueing disabled globally", MAXPATHLEN);
11519 		else if (sdinfo->satadrv_queue_depth >
11520 		    sdinfo->satadrv_max_queue_depth) {
11521 			(void) snprintf(&msg_buf[strlen(msg_buf)],
11522 			    MAXPATHLEN - strlen(msg_buf), ", limited to %d",
11523 			    (int)sdinfo->satadrv_max_queue_depth);
11524 		}
11525 		cmn_err(CE_CONT, "?\t%s\n", msg_buf);
11526 	}
11527 
11528 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
11529 #ifdef __i386
11530 		(void) sprintf(msg_buf, "\tcapacity = %llu sectors\n",
11531 		    sdinfo->satadrv_capacity);
11532 #else
11533 		(void) sprintf(msg_buf, "\tcapacity = %lu sectors\n",
11534 		    sdinfo->satadrv_capacity);
11535 #endif
11536 		cmn_err(CE_CONT, "?%s", msg_buf);
11537 	}
11538 }
11539 
11540 /*
11541  * Log/display port multiplier information
11542  */
11543 static void
11544 sata_show_pmult_info(sata_hba_inst_t *sata_hba_inst,
11545     sata_device_t *sata_device)
11546 {
11547 	_NOTE(ARGUNUSED(sata_hba_inst))
11548 
11549 	char msg_buf[MAXPATHLEN];
11550 	uint32_t gscr0, gscr1, gscr2, gscr64;
11551 
11552 	gscr0 = sata_device->satadev_gscr.gscr0;
11553 	gscr1 = sata_device->satadev_gscr.gscr1;
11554 	gscr2 = sata_device->satadev_gscr.gscr2;
11555 	gscr64 = sata_device->satadev_gscr.gscr64;
11556 
11557 	cmn_err(CE_CONT, "?Port Multiplier %d device-ports found at port %d",
11558 	    sata_device->satadev_add_info, sata_device->satadev_addr.cport);
11559 
11560 	(void) sprintf(msg_buf, "\tVendor_ID 0x%04x, Module_ID 0x%04x",
11561 	    gscr0 & 0xffff, (gscr0 >> 16) & 0xffff);
11562 	cmn_err(CE_CONT, "?%s", msg_buf);
11563 
11564 	(void) strcpy(msg_buf, "\tSupport SATA PMP Spec ");
11565 	if (gscr1 & (1 << 3))
11566 		(void) strlcat(msg_buf, "1.2", MAXPATHLEN);
11567 	else if (gscr1 & (1 << 2))
11568 		(void) strlcat(msg_buf, "1.1", MAXPATHLEN);
11569 	else if (gscr1 & (1 << 1))
11570 		(void) strlcat(msg_buf, "1.0", MAXPATHLEN);
11571 	else
11572 		(void) strlcat(msg_buf, "unknown", MAXPATHLEN);
11573 	cmn_err(CE_CONT, "?%s", msg_buf);
11574 
11575 	(void) strcpy(msg_buf, "\tSupport ");
11576 	if (gscr64 & (1 << 3))
11577 		(void) strlcat(msg_buf, "Asy-Notif, ",
11578 		    MAXPATHLEN);
11579 	if (gscr64 & (1 << 2))
11580 		(void) strlcat(msg_buf, "Dyn-SSC, ", MAXPATHLEN);
11581 	if (gscr64 & (1 << 1))
11582 		(void) strlcat(msg_buf, "Iss-PMREQ, ", MAXPATHLEN);
11583 	if (gscr64 & (1 << 0))
11584 		(void) strlcat(msg_buf, "BIST", MAXPATHLEN);
11585 	if ((gscr64 & 0xf) == 0)
11586 		(void) strlcat(msg_buf, "nothing", MAXPATHLEN);
11587 	cmn_err(CE_CONT, "?%s", msg_buf);
11588 
11589 	(void) sprintf(msg_buf, "\tNumber of exposed device fan-out ports: %d",
11590 	    gscr2 & SATA_PMULT_PORTNUM_MASK);
11591 	cmn_err(CE_CONT, "?%s", msg_buf);
11592 }
11593 
11594 /*
11595  * sata_save_drive_settings extracts current setting of the device and stores
11596  * it for future reference, in case the device setup would need to be restored
11597  * after the device reset.
11598  *
11599  * For all devices read ahead and write cache settings are saved, if the
11600  * device supports these features at all.
11601  * For ATAPI devices the Removable Media Status Notification setting is saved.
11602  */
11603 static void
11604 sata_save_drive_settings(sata_drive_info_t *sdinfo)
11605 {
11606 	if (SATA_READ_AHEAD_SUPPORTED(sdinfo->satadrv_id) ||
11607 	    SATA_WRITE_CACHE_SUPPORTED(sdinfo->satadrv_id)) {
11608 
11609 		/* Current setting of Read Ahead (and Read Cache) */
11610 		if (SATA_READ_AHEAD_ENABLED(sdinfo->satadrv_id))
11611 			sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
11612 		else
11613 			sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD;
11614 
11615 		/* Current setting of Write Cache */
11616 		if (SATA_WRITE_CACHE_ENABLED(sdinfo->satadrv_id))
11617 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
11618 		else
11619 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
11620 	}
11621 
11622 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
11623 		if (SATA_RM_NOTIFIC_SUPPORTED(sdinfo->satadrv_id))
11624 			sdinfo->satadrv_settings |= SATA_DEV_RMSN;
11625 		else
11626 			sdinfo->satadrv_settings &= ~SATA_DEV_RMSN;
11627 	}
11628 }
11629 
11630 
11631 /*
11632  * sata_check_capacity function determines a disk capacity
11633  * and addressing mode (LBA28/LBA48) by examining a disk identify device data.
11634  *
11635  * NOTE: CHS mode is not supported! If a device does not support LBA,
11636  * this function is not called.
11637  *
11638  * Returns device capacity in number of blocks, i.e. largest addressable LBA+1
11639  */
11640 static uint64_t
11641 sata_check_capacity(sata_drive_info_t *sdinfo)
11642 {
11643 	uint64_t capacity = 0;
11644 	int i;
11645 
11646 	if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK ||
11647 	    !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT)
11648 		/* Capacity valid only for LBA-addressable disk devices */
11649 		return (0);
11650 
11651 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) &&
11652 	    (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) &&
11653 	    (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) {
11654 		/* LBA48 mode supported and enabled */
11655 		sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 |
11656 		    SATA_DEV_F_LBA28;
11657 		for (i = 3;  i >= 0;  --i) {
11658 			capacity <<= 16;
11659 			capacity += sdinfo->satadrv_id.ai_addrsecxt[i];
11660 		}
11661 	} else {
11662 		capacity = sdinfo->satadrv_id.ai_addrsec[1];
11663 		capacity <<= 16;
11664 		capacity += sdinfo->satadrv_id.ai_addrsec[0];
11665 		if (capacity >= 0x1000000)
11666 			/* LBA28 mode */
11667 			sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28;
11668 	}
11669 	return (capacity);
11670 }
11671 
11672 
11673 /*
11674  * Allocate consistent buffer for DMA transfer
11675  *
11676  * Cannot be called from interrupt level or with mutex held - it may sleep.
11677  *
11678  * Returns pointer to allocated buffer structure, or NULL if allocation failed.
11679  */
11680 static struct buf *
11681 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len)
11682 {
11683 	struct scsi_address ap;
11684 	struct buf *bp;
11685 	ddi_dma_attr_t	cur_dma_attr;
11686 
11687 	ASSERT(spx->txlt_sata_pkt != NULL);
11688 	ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran;
11689 	ap.a_target = SATA_TO_SCSI_TARGET(
11690 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport,
11691 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport,
11692 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual);
11693 	ap.a_lun = 0;
11694 
11695 	bp = scsi_alloc_consistent_buf(&ap, NULL, len,
11696 	    B_READ, SLEEP_FUNC, NULL);
11697 
11698 	if (bp != NULL) {
11699 		/* Allocate DMA resources for this buffer */
11700 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
11701 		/*
11702 		 * We use a local version of the dma_attr, to account
11703 		 * for a device addressing limitations.
11704 		 * sata_adjust_dma_attr() will handle sdinfo == NULL which
11705 		 * will cause dma attributes to be adjusted to a lowest
11706 		 * acceptable level.
11707 		 */
11708 		sata_adjust_dma_attr(NULL,
11709 		    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
11710 
11711 		if (sata_dma_buf_setup(spx, PKT_CONSISTENT,
11712 		    SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) {
11713 			scsi_free_consistent_buf(bp);
11714 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
11715 			bp = NULL;
11716 		}
11717 	}
11718 	return (bp);
11719 }
11720 
11721 /*
11722  * Release local buffer (consistent buffer for DMA transfer) allocated
11723  * via sata_alloc_local_buffer().
11724  */
11725 static void
11726 sata_free_local_buffer(sata_pkt_txlate_t *spx)
11727 {
11728 	ASSERT(spx->txlt_sata_pkt != NULL);
11729 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL);
11730 
11731 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0;
11732 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL;
11733 
11734 	sata_common_free_dma_rsrcs(spx);
11735 
11736 	/* Free buffer */
11737 	scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp);
11738 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
11739 }
11740 
11741 /*
11742  * Allocate sata_pkt
11743  * Pkt structure version and embedded strcutures version are initialized.
11744  * sata_pkt and sata_pkt_txlate structures are cross-linked.
11745  *
11746  * Since this may be called in interrupt context by sata_scsi_init_pkt,
11747  * callback argument determines if it can sleep or not.
11748  * Hence, it should not be called from interrupt context.
11749  *
11750  * If successful, non-NULL pointer to a sata pkt is returned.
11751  * Upon failure, NULL pointer is returned.
11752  */
11753 static sata_pkt_t *
11754 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t))
11755 {
11756 	sata_pkt_t *spkt;
11757 	int kmsflag;
11758 
11759 	kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
11760 	spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag);
11761 	if (spkt == NULL) {
11762 		SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
11763 		    "sata_pkt_alloc: failed"));
11764 		return (NULL);
11765 	}
11766 	spkt->satapkt_rev = SATA_PKT_REV;
11767 	spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV;
11768 	spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
11769 	spkt->satapkt_framework_private = spx;
11770 	spx->txlt_sata_pkt = spkt;
11771 	return (spkt);
11772 }
11773 
11774 /*
11775  * Free sata pkt allocated via sata_pkt_alloc()
11776  */
11777 static void
11778 sata_pkt_free(sata_pkt_txlate_t *spx)
11779 {
11780 	ASSERT(spx->txlt_sata_pkt != NULL);
11781 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL);
11782 	kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t));
11783 	spx->txlt_sata_pkt = NULL;
11784 }
11785 
11786 
11787 /*
11788  * Adjust DMA attributes.
11789  * SCSI cmds block count is up to 24 bits, SATA cmd block count vary
11790  * from 8 bits to 16 bits, depending on a command being used.
11791  * Limiting max block count arbitrarily to 256 for all read/write
11792  * commands may affects performance, so check both the device and
11793  * controller capability before adjusting dma attributes.
11794  */
11795 void
11796 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr,
11797     ddi_dma_attr_t *adj_dma_attr)
11798 {
11799 	uint32_t count_max;
11800 
11801 	/* Copy original attributes */
11802 	*adj_dma_attr = *dma_attr;
11803 	/*
11804 	 * Things to consider: device addressing capability,
11805 	 * "excessive" controller DMA capabilities.
11806 	 * If a device is being probed/initialized, there are
11807 	 * no device info - use default limits then.
11808 	 */
11809 	if (sdinfo == NULL) {
11810 		count_max = dma_attr->dma_attr_granular * 0x100;
11811 		if (dma_attr->dma_attr_count_max > count_max)
11812 			adj_dma_attr->dma_attr_count_max = count_max;
11813 		if (dma_attr->dma_attr_maxxfer > count_max)
11814 			adj_dma_attr->dma_attr_maxxfer = count_max;
11815 		return;
11816 	}
11817 
11818 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
11819 		if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) {
11820 			/*
11821 			 * 16-bit sector count may be used - we rely on
11822 			 * the assumption that only read and write cmds
11823 			 * will request more than 256 sectors worth of data
11824 			 */
11825 			count_max = adj_dma_attr->dma_attr_granular * 0x10000;
11826 		} else {
11827 			/*
11828 			 * 8-bit sector count will be used - default limits
11829 			 * for dma attributes
11830 			 */
11831 			count_max = adj_dma_attr->dma_attr_granular * 0x100;
11832 		}
11833 		/*
11834 		 * Adjust controler dma attributes, if necessary
11835 		 */
11836 		if (dma_attr->dma_attr_count_max > count_max)
11837 			adj_dma_attr->dma_attr_count_max = count_max;
11838 		if (dma_attr->dma_attr_maxxfer > count_max)
11839 			adj_dma_attr->dma_attr_maxxfer = count_max;
11840 	}
11841 }
11842 
11843 
11844 /*
11845  * Allocate DMA resources for the buffer
11846  * This function handles initial DMA resource allocation as well as
11847  * DMA window shift and may be called repeatedly for the same DMA window
11848  * until all DMA cookies in the DMA window are processed.
11849  * To guarantee that there is always a coherent set of cookies to process
11850  * by SATA HBA driver (observing alignment, device granularity, etc.),
11851  * the number of slots for DMA cookies is equal to lesser of  a number of
11852  * cookies in a DMA window and a max number of scatter/gather entries.
11853  *
11854  * Returns DDI_SUCCESS upon successful operation.
11855  * Return failure code of a failing command or DDI_FAILURE when
11856  * internal cleanup failed.
11857  */
11858 static int
11859 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags,
11860     int (*callback)(caddr_t), caddr_t arg,
11861     ddi_dma_attr_t *cur_dma_attr)
11862 {
11863 	int	rval;
11864 	off_t	offset;
11865 	size_t	size;
11866 	int	max_sg_len, req_len, i;
11867 	uint_t	dma_flags;
11868 	struct buf	*bp;
11869 	uint64_t	cur_txfer_len;
11870 
11871 
11872 	ASSERT(spx->txlt_sata_pkt != NULL);
11873 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
11874 	ASSERT(bp != NULL);
11875 
11876 
11877 	if (spx->txlt_buf_dma_handle == NULL) {
11878 		/*
11879 		 * No DMA resources allocated so far - this is a first call
11880 		 * for this sata pkt.
11881 		 */
11882 		rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst),
11883 		    cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle);
11884 
11885 		if (rval != DDI_SUCCESS) {
11886 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
11887 			    "sata_dma_buf_setup: no buf DMA resources %x",
11888 			    rval));
11889 			return (rval);
11890 		}
11891 
11892 		if (bp->b_flags & B_READ)
11893 			dma_flags = DDI_DMA_READ;
11894 		else
11895 			dma_flags = DDI_DMA_WRITE;
11896 
11897 		if (flags & PKT_CONSISTENT)
11898 			dma_flags |= DDI_DMA_CONSISTENT;
11899 
11900 		if (flags & PKT_DMA_PARTIAL)
11901 			dma_flags |= DDI_DMA_PARTIAL;
11902 
11903 		/*
11904 		 * Check buffer alignment and size against dma attributes
11905 		 * Consider dma_attr_align only. There may be requests
11906 		 * with the size lower than device granularity, but they
11907 		 * will not read/write from/to the device, so no adjustment
11908 		 * is necessary. The dma_attr_minxfer theoretically should
11909 		 * be considered, but no HBA driver is checking it.
11910 		 */
11911 		if (IS_P2ALIGNED(bp->b_un.b_addr,
11912 		    cur_dma_attr->dma_attr_align)) {
11913 			rval = ddi_dma_buf_bind_handle(
11914 			    spx->txlt_buf_dma_handle,
11915 			    bp, dma_flags, callback, arg,
11916 			    &spx->txlt_dma_cookie,
11917 			    &spx->txlt_curwin_num_dma_cookies);
11918 		} else { /* Buffer is not aligned */
11919 
11920 			int	(*ddicallback)(caddr_t);
11921 			size_t	bufsz;
11922 
11923 			/* Check id sleeping is allowed */
11924 			ddicallback = (callback == NULL_FUNC) ?
11925 			    DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
11926 
11927 			SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
11928 			    "mis-aligned buffer: addr=0x%p, cnt=%lu",
11929 			    (void *)bp->b_un.b_addr, bp->b_bcount);
11930 
11931 			if (bp->b_flags & (B_PAGEIO|B_PHYS))
11932 				/*
11933 				 * CPU will need to access data in the buffer
11934 				 * (for copying) so map it.
11935 				 */
11936 				bp_mapin(bp);
11937 
11938 			ASSERT(spx->txlt_tmp_buf == NULL);
11939 
11940 			/* Buffer may be padded by ddi_dma_mem_alloc()! */
11941 			rval = ddi_dma_mem_alloc(
11942 			    spx->txlt_buf_dma_handle,
11943 			    bp->b_bcount,
11944 			    &sata_acc_attr,
11945 			    DDI_DMA_STREAMING,
11946 			    ddicallback, NULL,
11947 			    &spx->txlt_tmp_buf,
11948 			    &bufsz,
11949 			    &spx->txlt_tmp_buf_handle);
11950 
11951 			if (rval != DDI_SUCCESS) {
11952 				/* DMA mapping failed */
11953 				(void) ddi_dma_free_handle(
11954 				    &spx->txlt_buf_dma_handle);
11955 				spx->txlt_buf_dma_handle = NULL;
11956 #ifdef SATA_DEBUG
11957 				mbuffail_count++;
11958 #endif
11959 				SATADBG1(SATA_DBG_DMA_SETUP,
11960 				    spx->txlt_sata_hba_inst,
11961 				    "sata_dma_buf_setup: "
11962 				    "buf dma mem alloc failed %x\n", rval);
11963 				return (rval);
11964 			}
11965 			ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf,
11966 			    cur_dma_attr->dma_attr_align));
11967 
11968 #ifdef SATA_DEBUG
11969 			mbuf_count++;
11970 
11971 			if (bp->b_bcount != bufsz)
11972 				/*
11973 				 * This will require special handling, because
11974 				 * DMA cookies will be based on the temporary
11975 				 * buffer size, not the original buffer
11976 				 * b_bcount, so the residue may have to
11977 				 * be counted differently.
11978 				 */
11979 				SATADBG2(SATA_DBG_DMA_SETUP,
11980 				    spx->txlt_sata_hba_inst,
11981 				    "sata_dma_buf_setup: bp size %x != "
11982 				    "bufsz %x\n", bp->b_bcount, bufsz);
11983 #endif
11984 			if (dma_flags & DDI_DMA_WRITE) {
11985 				/*
11986 				 * Write operation - copy data into
11987 				 * an aligned temporary buffer. Buffer will be
11988 				 * synced for device by ddi_dma_addr_bind_handle
11989 				 */
11990 				bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf,
11991 				    bp->b_bcount);
11992 			}
11993 
11994 			rval = ddi_dma_addr_bind_handle(
11995 			    spx->txlt_buf_dma_handle,
11996 			    NULL,
11997 			    spx->txlt_tmp_buf,
11998 			    bufsz, dma_flags, ddicallback, 0,
11999 			    &spx->txlt_dma_cookie,
12000 			    &spx->txlt_curwin_num_dma_cookies);
12001 		}
12002 
12003 		switch (rval) {
12004 		case DDI_DMA_PARTIAL_MAP:
12005 			SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
12006 			    "sata_dma_buf_setup: DMA Partial Map\n", NULL);
12007 			/*
12008 			 * Partial DMA mapping.
12009 			 * Retrieve number of DMA windows for this request.
12010 			 */
12011 			if (ddi_dma_numwin(spx->txlt_buf_dma_handle,
12012 			    &spx->txlt_num_dma_win) != DDI_SUCCESS) {
12013 				if (spx->txlt_tmp_buf != NULL) {
12014 					ddi_dma_mem_free(
12015 					    &spx->txlt_tmp_buf_handle);
12016 					spx->txlt_tmp_buf = NULL;
12017 				}
12018 				(void) ddi_dma_unbind_handle(
12019 				    spx->txlt_buf_dma_handle);
12020 				(void) ddi_dma_free_handle(
12021 				    &spx->txlt_buf_dma_handle);
12022 				spx->txlt_buf_dma_handle = NULL;
12023 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
12024 				    "sata_dma_buf_setup: numwin failed\n"));
12025 				return (DDI_FAILURE);
12026 			}
12027 			SATADBG2(SATA_DBG_DMA_SETUP,
12028 			    spx->txlt_sata_hba_inst,
12029 			    "sata_dma_buf_setup: windows: %d, cookies: %d\n",
12030 			    spx->txlt_num_dma_win,
12031 			    spx->txlt_curwin_num_dma_cookies);
12032 			spx->txlt_cur_dma_win = 0;
12033 			break;
12034 
12035 		case DDI_DMA_MAPPED:
12036 			/* DMA fully mapped */
12037 			spx->txlt_num_dma_win = 1;
12038 			spx->txlt_cur_dma_win = 0;
12039 			SATADBG1(SATA_DBG_DMA_SETUP,
12040 			    spx->txlt_sata_hba_inst,
12041 			    "sata_dma_buf_setup: windows: 1 "
12042 			    "cookies: %d\n", spx->txlt_curwin_num_dma_cookies);
12043 			break;
12044 
12045 		default:
12046 			/* DMA mapping failed */
12047 			if (spx->txlt_tmp_buf != NULL) {
12048 				ddi_dma_mem_free(
12049 				    &spx->txlt_tmp_buf_handle);
12050 				spx->txlt_tmp_buf = NULL;
12051 			}
12052 			(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
12053 			spx->txlt_buf_dma_handle = NULL;
12054 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
12055 			    "sata_dma_buf_setup: buf dma handle binding "
12056 			    "failed %x\n", rval));
12057 			return (rval);
12058 		}
12059 		spx->txlt_curwin_processed_dma_cookies = 0;
12060 		spx->txlt_dma_cookie_list = NULL;
12061 	} else {
12062 		/*
12063 		 * DMA setup is reused. Check if we need to process more
12064 		 * cookies in current window, or to get next window, if any.
12065 		 */
12066 
12067 		ASSERT(spx->txlt_curwin_processed_dma_cookies <=
12068 		    spx->txlt_curwin_num_dma_cookies);
12069 
12070 		if (spx->txlt_curwin_processed_dma_cookies ==
12071 		    spx->txlt_curwin_num_dma_cookies) {
12072 			/*
12073 			 * All cookies from current DMA window were processed.
12074 			 * Get next DMA window.
12075 			 */
12076 			spx->txlt_cur_dma_win++;
12077 			if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) {
12078 				(void) ddi_dma_getwin(spx->txlt_buf_dma_handle,
12079 				    spx->txlt_cur_dma_win, &offset, &size,
12080 				    &spx->txlt_dma_cookie,
12081 				    &spx->txlt_curwin_num_dma_cookies);
12082 				spx->txlt_curwin_processed_dma_cookies = 0;
12083 			} else {
12084 				/* No more windows! End of request! */
12085 				/* What to do? - panic for now */
12086 				ASSERT(spx->txlt_cur_dma_win >=
12087 				    spx->txlt_num_dma_win);
12088 
12089 				spx->txlt_curwin_num_dma_cookies = 0;
12090 				spx->txlt_curwin_processed_dma_cookies = 0;
12091 				spx->txlt_sata_pkt->
12092 				    satapkt_cmd.satacmd_num_dma_cookies = 0;
12093 				return (DDI_SUCCESS);
12094 			}
12095 		}
12096 	}
12097 	/* There better be at least one DMA cookie outstanding */
12098 	ASSERT((spx->txlt_curwin_num_dma_cookies -
12099 	    spx->txlt_curwin_processed_dma_cookies) > 0);
12100 
12101 	if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) {
12102 		/* The default cookie slot was used in previous run */
12103 		ASSERT(spx->txlt_curwin_processed_dma_cookies == 0);
12104 		spx->txlt_dma_cookie_list = NULL;
12105 		spx->txlt_dma_cookie_list_len = 0;
12106 	}
12107 	if (spx->txlt_curwin_processed_dma_cookies == 0) {
12108 		/*
12109 		 * Processing a new DMA window - set-up dma cookies list.
12110 		 * We may reuse previously allocated cookie array if it is
12111 		 * possible.
12112 		 */
12113 		if (spx->txlt_dma_cookie_list != NULL &&
12114 		    spx->txlt_dma_cookie_list_len <
12115 		    spx->txlt_curwin_num_dma_cookies) {
12116 			/*
12117 			 * New DMA window contains more cookies than
12118 			 * the previous one. We need larger cookie list - free
12119 			 * the old one.
12120 			 */
12121 			(void) kmem_free(spx->txlt_dma_cookie_list,
12122 			    spx->txlt_dma_cookie_list_len *
12123 			    sizeof (ddi_dma_cookie_t));
12124 			spx->txlt_dma_cookie_list = NULL;
12125 			spx->txlt_dma_cookie_list_len = 0;
12126 		}
12127 		if (spx->txlt_dma_cookie_list == NULL) {
12128 			/*
12129 			 * Calculate lesser of number of cookies in this
12130 			 * DMA window and number of s/g entries.
12131 			 */
12132 			max_sg_len = cur_dma_attr->dma_attr_sgllen;
12133 			req_len = MIN(max_sg_len,
12134 			    spx->txlt_curwin_num_dma_cookies);
12135 
12136 			/* Allocate new dma cookie array if necessary */
12137 			if (req_len == 1) {
12138 				/* Only one cookie - no need for a list */
12139 				spx->txlt_dma_cookie_list =
12140 				    &spx->txlt_dma_cookie;
12141 				spx->txlt_dma_cookie_list_len = 1;
12142 			} else {
12143 				/*
12144 				 * More than one cookie - try to allocate space.
12145 				 */
12146 				spx->txlt_dma_cookie_list = kmem_zalloc(
12147 				    sizeof (ddi_dma_cookie_t) * req_len,
12148 				    callback == NULL_FUNC ? KM_NOSLEEP :
12149 				    KM_SLEEP);
12150 				if (spx->txlt_dma_cookie_list == NULL) {
12151 					SATADBG1(SATA_DBG_DMA_SETUP,
12152 					    spx->txlt_sata_hba_inst,
12153 					    "sata_dma_buf_setup: cookie list "
12154 					    "allocation failed\n", NULL);
12155 					/*
12156 					 * We could not allocate space for
12157 					 * neccessary number of dma cookies in
12158 					 * this window, so we fail this request.
12159 					 * Next invocation would try again to
12160 					 * allocate space for cookie list.
12161 					 * Note:Packet residue was not modified.
12162 					 */
12163 					return (DDI_DMA_NORESOURCES);
12164 				} else {
12165 					spx->txlt_dma_cookie_list_len = req_len;
12166 				}
12167 			}
12168 		}
12169 		/*
12170 		 * Fetch DMA cookies into cookie list in sata_pkt_txlate.
12171 		 * First cookie was already fetched.
12172 		 */
12173 		*(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie;
12174 		cur_txfer_len =
12175 		    (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size;
12176 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1;
12177 		spx->txlt_curwin_processed_dma_cookies++;
12178 		for (i = 1; (i < spx->txlt_dma_cookie_list_len) &&
12179 		    (i < spx->txlt_curwin_num_dma_cookies); i++) {
12180 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
12181 			    &spx->txlt_dma_cookie_list[i]);
12182 			cur_txfer_len +=
12183 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
12184 			spx->txlt_curwin_processed_dma_cookies++;
12185 			spx->txlt_sata_pkt->
12186 			    satapkt_cmd.satacmd_num_dma_cookies += 1;
12187 		}
12188 	} else {
12189 		SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
12190 		    "sata_dma_buf_setup: sliding within DMA window, "
12191 		    "cur cookie %d, total cookies %d\n",
12192 		    spx->txlt_curwin_processed_dma_cookies,
12193 		    spx->txlt_curwin_num_dma_cookies);
12194 
12195 		/*
12196 		 * Not all cookies from the current dma window were used because
12197 		 * of s/g limitation.
12198 		 * There is no need to re-size the list - it was set at
12199 		 * optimal size, or only default entry is used (s/g = 1).
12200 		 */
12201 		if (spx->txlt_dma_cookie_list == NULL) {
12202 			spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie;
12203 			spx->txlt_dma_cookie_list_len = 1;
12204 		}
12205 		/*
12206 		 * Since we are processing remaining cookies in a DMA window,
12207 		 * there may be less of them than the number of entries in the
12208 		 * current dma cookie list.
12209 		 */
12210 		req_len = MIN(spx->txlt_dma_cookie_list_len,
12211 		    (spx->txlt_curwin_num_dma_cookies -
12212 		    spx->txlt_curwin_processed_dma_cookies));
12213 
12214 		/* Fetch the next batch of cookies */
12215 		for (i = 0, cur_txfer_len = 0; i < req_len; i++) {
12216 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
12217 			    &spx->txlt_dma_cookie_list[i]);
12218 			cur_txfer_len +=
12219 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
12220 			spx->txlt_sata_pkt->
12221 			    satapkt_cmd.satacmd_num_dma_cookies++;
12222 			spx->txlt_curwin_processed_dma_cookies++;
12223 		}
12224 	}
12225 
12226 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0);
12227 
12228 	/* Point sata_cmd to the cookie list */
12229 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list =
12230 	    &spx->txlt_dma_cookie_list[0];
12231 
12232 	/* Remember number of DMA cookies passed in sata packet */
12233 	spx->txlt_num_dma_cookies =
12234 	    spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies;
12235 
12236 	ASSERT(cur_txfer_len != 0);
12237 	if (cur_txfer_len <= bp->b_bcount)
12238 		spx->txlt_total_residue -= cur_txfer_len;
12239 	else {
12240 		/*
12241 		 * Temporary DMA buffer has been padded by
12242 		 * ddi_dma_mem_alloc()!
12243 		 * This requires special handling, because DMA cookies are
12244 		 * based on the temporary buffer size, not the b_bcount,
12245 		 * and we have extra bytes to transfer - but the packet
12246 		 * residue has to stay correct because we will copy only
12247 		 * the requested number of bytes.
12248 		 */
12249 		spx->txlt_total_residue -= bp->b_bcount;
12250 	}
12251 
12252 	return (DDI_SUCCESS);
12253 }
12254 
12255 /*
12256  * Common routine for releasing DMA resources
12257  */
12258 static void
12259 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx)
12260 {
12261 	if (spx->txlt_buf_dma_handle != NULL) {
12262 		if (spx->txlt_tmp_buf != NULL)  {
12263 			/*
12264 			 * Intermediate DMA buffer was allocated.
12265 			 * Free allocated buffer and associated access handle.
12266 			 */
12267 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
12268 			spx->txlt_tmp_buf = NULL;
12269 		}
12270 		/*
12271 		 * Free DMA resources - cookies and handles
12272 		 */
12273 		/* ASSERT(spx->txlt_dma_cookie_list != NULL); */
12274 		if (spx->txlt_dma_cookie_list != NULL) {
12275 			if (spx->txlt_dma_cookie_list !=
12276 			    &spx->txlt_dma_cookie) {
12277 				(void) kmem_free(spx->txlt_dma_cookie_list,
12278 				    spx->txlt_dma_cookie_list_len *
12279 				    sizeof (ddi_dma_cookie_t));
12280 				spx->txlt_dma_cookie_list = NULL;
12281 			}
12282 		}
12283 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
12284 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
12285 		spx->txlt_buf_dma_handle = NULL;
12286 	}
12287 }
12288 
12289 /*
12290  * Free DMA resources
12291  * Used by the HBA driver to release DMA resources that it does not use.
12292  *
12293  * Returns Void
12294  */
12295 void
12296 sata_free_dma_resources(sata_pkt_t *sata_pkt)
12297 {
12298 	sata_pkt_txlate_t *spx;
12299 
12300 	if (sata_pkt == NULL)
12301 		return;
12302 
12303 	spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
12304 
12305 	sata_common_free_dma_rsrcs(spx);
12306 }
12307 
12308 /*
12309  * Fetch Device Identify data.
12310  * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type)
12311  * command to a device and get the device identify data.
12312  * The device_info structure has to be set to device type (for selecting proper
12313  * device identify command).
12314  *
12315  * Returns:
12316  * SATA_SUCCESS if cmd succeeded
12317  * SATA_RETRY if cmd was rejected and could be retried,
12318  * SATA_FAILURE if cmd failed and should not be retried (port error)
12319  *
12320  * Cannot be called in an interrupt context.
12321  */
12322 
12323 static int
12324 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst,
12325     sata_drive_info_t *sdinfo)
12326 {
12327 	struct buf *bp;
12328 	sata_pkt_t *spkt;
12329 	sata_cmd_t *scmd;
12330 	sata_pkt_txlate_t *spx;
12331 	int rval;
12332 
12333 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12334 	spx->txlt_sata_hba_inst = sata_hba_inst;
12335 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12336 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12337 	if (spkt == NULL) {
12338 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12339 		return (SATA_RETRY); /* may retry later */
12340 	}
12341 	/* address is needed now */
12342 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12343 
12344 	/*
12345 	 * Allocate buffer for Identify Data return data
12346 	 */
12347 	bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t));
12348 	if (bp == NULL) {
12349 		sata_pkt_free(spx);
12350 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12351 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12352 		    "sata_fetch_device_identify_data: "
12353 		    "cannot allocate buffer for ID"));
12354 		return (SATA_RETRY); /* may retry later */
12355 	}
12356 
12357 	/* Fill sata_pkt */
12358 	sdinfo->satadrv_state = SATA_STATE_PROBING;
12359 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12360 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12361 	/* Synchronous mode, no callback */
12362 	spkt->satapkt_comp = NULL;
12363 	/* Timeout 30s */
12364 	spkt->satapkt_time = sata_default_pkt_time;
12365 
12366 	scmd = &spkt->satapkt_cmd;
12367 	scmd->satacmd_bp = bp;
12368 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
12369 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
12370 
12371 	/* Build Identify Device cmd in the sata_pkt */
12372 	scmd->satacmd_addr_type = 0;		/* N/A */
12373 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
12374 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
12375 	scmd->satacmd_lba_mid_lsb = 0;		/* N/A */
12376 	scmd->satacmd_lba_high_lsb = 0;		/* N/A */
12377 	scmd->satacmd_features_reg = 0;		/* N/A */
12378 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12379 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
12380 		/* Identify Packet Device cmd */
12381 		scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE;
12382 	} else {
12383 		/* Identify Device cmd - mandatory for all other devices */
12384 		scmd->satacmd_cmd_reg = SATAC_ID_DEVICE;
12385 	}
12386 
12387 	/* Send pkt to SATA HBA driver */
12388 	rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt);
12389 
12390 #ifdef SATA_INJECT_FAULTS
12391 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
12392 #endif
12393 
12394 	if (rval == SATA_TRAN_ACCEPTED &&
12395 	    spkt->satapkt_reason == SATA_PKT_COMPLETED) {
12396 		if (spx->txlt_buf_dma_handle != NULL) {
12397 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
12398 			    DDI_DMA_SYNC_FORKERNEL);
12399 			ASSERT(rval == DDI_SUCCESS);
12400 		}
12401 		if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config &
12402 		    SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) {
12403 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12404 			    "SATA disk device at port %d - "
12405 			    "partial Identify Data",
12406 			    sdinfo->satadrv_addr.cport));
12407 			rval = SATA_RETRY; /* may retry later */
12408 			goto fail;
12409 		}
12410 		/* Update sata_drive_info */
12411 		bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id,
12412 		    sizeof (sata_id_t));
12413 
12414 		sdinfo->satadrv_features_support = 0;
12415 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12416 			/*
12417 			 * Retrieve capacity (disks only) and addressing mode
12418 			 */
12419 			sdinfo->satadrv_capacity = sata_check_capacity(sdinfo);
12420 		} else {
12421 			/*
12422 			 * For ATAPI devices one would have to issue
12423 			 * Get Capacity cmd for media capacity. Not here.
12424 			 */
12425 			sdinfo->satadrv_capacity = 0;
12426 			/*
12427 			 * Check what cdb length is supported
12428 			 */
12429 			if ((sdinfo->satadrv_id.ai_config &
12430 			    SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B)
12431 				sdinfo->satadrv_atapi_cdb_len = 16;
12432 			else
12433 				sdinfo->satadrv_atapi_cdb_len = 12;
12434 		}
12435 		/* Setup supported features flags */
12436 		if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT)
12437 			sdinfo->satadrv_features_support |= SATA_DEV_F_DMA;
12438 
12439 		/* Check for SATA GEN and NCQ support */
12440 		if (sdinfo->satadrv_id.ai_satacap != 0 &&
12441 		    sdinfo->satadrv_id.ai_satacap != 0xffff) {
12442 			/* SATA compliance */
12443 			if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ)
12444 				sdinfo->satadrv_features_support |=
12445 				    SATA_DEV_F_NCQ;
12446 			if (sdinfo->satadrv_id.ai_satacap &
12447 			    (SATA_1_SPEED | SATA_2_SPEED)) {
12448 				if (sdinfo->satadrv_id.ai_satacap &
12449 				    SATA_2_SPEED)
12450 					sdinfo->satadrv_features_support |=
12451 					    SATA_DEV_F_SATA2;
12452 				if (sdinfo->satadrv_id.ai_satacap &
12453 				    SATA_1_SPEED)
12454 					sdinfo->satadrv_features_support |=
12455 					    SATA_DEV_F_SATA1;
12456 			} else {
12457 				sdinfo->satadrv_features_support |=
12458 				    SATA_DEV_F_SATA1;
12459 			}
12460 		}
12461 		if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) &&
12462 		    (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD))
12463 			sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ;
12464 
12465 		sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth;
12466 		if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) ||
12467 		    (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) {
12468 			++sdinfo->satadrv_queue_depth;
12469 			/* Adjust according to controller capabilities */
12470 			sdinfo->satadrv_max_queue_depth = MIN(
12471 			    sdinfo->satadrv_queue_depth,
12472 			    SATA_QDEPTH(sata_hba_inst));
12473 			/* Adjust according to global queue depth limit */
12474 			sdinfo->satadrv_max_queue_depth = MIN(
12475 			    sdinfo->satadrv_max_queue_depth,
12476 			    sata_current_max_qdepth);
12477 			if (sdinfo->satadrv_max_queue_depth == 0)
12478 				sdinfo->satadrv_max_queue_depth = 1;
12479 		} else
12480 			sdinfo->satadrv_max_queue_depth = 1;
12481 
12482 		rval = SATA_SUCCESS;
12483 	} else {
12484 		/*
12485 		 * Woops, no Identify Data.
12486 		 */
12487 		if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) {
12488 			rval = SATA_RETRY; /* may retry later */
12489 		} else if (rval == SATA_TRAN_ACCEPTED) {
12490 			if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR ||
12491 			    spkt->satapkt_reason == SATA_PKT_ABORTED ||
12492 			    spkt->satapkt_reason == SATA_PKT_TIMEOUT ||
12493 			    spkt->satapkt_reason == SATA_PKT_RESET)
12494 				rval = SATA_RETRY; /* may retry later */
12495 			else
12496 				rval = SATA_FAILURE;
12497 		} else {
12498 			rval = SATA_FAILURE;
12499 		}
12500 	}
12501 fail:
12502 	/* Free allocated resources */
12503 	sata_free_local_buffer(spx);
12504 	sata_pkt_free(spx);
12505 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12506 
12507 	return (rval);
12508 }
12509 
12510 
12511 /*
12512  * Some devices may not come-up with default DMA mode (UDMA or MWDMA).
12513  * UDMA mode is checked first, followed by MWDMA mode.
12514  * set correctly, so this function is setting it to the highest supported level.
12515  * Older SATA spec required that the device supports at least DMA 4 mode and
12516  * UDMA mode is selected.  It is not mentioned in SerialATA 2.6, so this
12517  * restriction has been removed.
12518  *
12519  * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported.
12520  * Returns SATA_FAILURE if proper DMA mode could not be selected.
12521  *
12522  * NOTE: This function should be called only if DMA mode is supported.
12523  */
12524 static int
12525 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo)
12526 {
12527 	sata_pkt_t *spkt;
12528 	sata_cmd_t *scmd;
12529 	sata_pkt_txlate_t *spx;
12530 	int mode;
12531 	uint8_t subcmd;
12532 	int rval = SATA_SUCCESS;
12533 
12534 	ASSERT(sdinfo != NULL);
12535 	ASSERT(sata_hba_inst != NULL);
12536 
12537 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
12538 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) {
12539 		/* Find highest Ultra DMA mode supported */
12540 		for (mode = 6; mode >= 0; --mode) {
12541 			if (sdinfo->satadrv_id.ai_ultradma & (1 << mode))
12542 				break;
12543 		}
12544 #if 0
12545 		/* Left for historical reasons */
12546 		/*
12547 		 * Some initial version of SATA spec indicated that at least
12548 		 * UDMA mode 4 has to be supported. It is not mentioned in
12549 		 * SerialATA 2.6, so this restriction is removed.
12550 		 */
12551 		if (mode < 4)
12552 			return (SATA_FAILURE);
12553 #endif
12554 
12555 		/*
12556 		 * We're still going to set DMA mode whatever is selected
12557 		 * by default
12558 		 *
12559 		 * We saw an old maxtor sata drive will select Ultra DMA and
12560 		 * Multi-Word DMA simultaneouly by default, which is going
12561 		 * to cause DMA command timed out, so we need to select DMA
12562 		 * mode even when it's already done by default
12563 		 */
12564 
12565 		subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA;
12566 
12567 	} else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) {
12568 		/* Find highest MultiWord DMA mode supported */
12569 		for (mode = 2; mode >= 0; --mode) {
12570 			if (sdinfo->satadrv_id.ai_dworddma & (1 << mode))
12571 				break;
12572 		}
12573 
12574 		/*
12575 		 * We're still going to set DMA mode whatever is selected
12576 		 * by default
12577 		 *
12578 		 * We saw an old maxtor sata drive will select Ultra DMA and
12579 		 * Multi-Word DMA simultaneouly by default, which is going
12580 		 * to cause DMA command timed out, so we need to select DMA
12581 		 * mode even when it's already done by default
12582 		 */
12583 
12584 		subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA;
12585 	} else
12586 		return (SATA_SUCCESS);
12587 
12588 	/*
12589 	 * Set DMA mode via SET FEATURES COMMAND.
12590 	 * Prepare packet for SET FEATURES COMMAND.
12591 	 */
12592 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12593 	spx->txlt_sata_hba_inst = sata_hba_inst;
12594 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
12595 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12596 	if (spkt == NULL) {
12597 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12598 		    "sata_set_dma_mode: could not set DMA mode %", mode));
12599 		rval = SATA_FAILURE;
12600 		goto done;
12601 	}
12602 	/* Fill sata_pkt */
12603 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12604 	/* Timeout 30s */
12605 	spkt->satapkt_time = sata_default_pkt_time;
12606 	/* Synchronous mode, no callback, interrupts */
12607 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12608 	spkt->satapkt_comp = NULL;
12609 	scmd = &spkt->satapkt_cmd;
12610 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
12611 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
12612 	scmd->satacmd_addr_type = 0;
12613 	scmd->satacmd_device_reg = 0;
12614 	scmd->satacmd_status_reg = 0;
12615 	scmd->satacmd_error_reg = 0;
12616 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
12617 	scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE;
12618 	scmd->satacmd_sec_count_lsb = subcmd | mode;
12619 
12620 	/* Transfer command to HBA */
12621 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
12622 	    spkt) != SATA_TRAN_ACCEPTED ||
12623 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12624 		/* Pkt execution failed */
12625 		rval = SATA_FAILURE;
12626 	}
12627 done:
12628 
12629 	/* Free allocated resources */
12630 	if (spkt != NULL)
12631 		sata_pkt_free(spx);
12632 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
12633 
12634 	return (rval);
12635 }
12636 
12637 
12638 /*
12639  * Set device caching mode.
12640  * One of the following operations should be specified:
12641  * SATAC_SF_ENABLE_READ_AHEAD
12642  * SATAC_SF_DISABLE_READ_AHEAD
12643  * SATAC_SF_ENABLE_WRITE_CACHE
12644  * SATAC_SF_DISABLE_WRITE_CACHE
12645  *
12646  * If operation fails, system log messgage is emitted.
12647  * Returns SATA_SUCCESS when the operation succeeds, SATA_RETRY if
12648  * command was sent but did not succeed, and SATA_FAILURE otherwise.
12649  */
12650 
12651 static int
12652 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
12653     int cache_op)
12654 {
12655 	sata_pkt_t *spkt;
12656 	sata_cmd_t *scmd;
12657 	sata_pkt_txlate_t *spx;
12658 	int rval = SATA_SUCCESS;
12659 	int hba_rval;
12660 	char *infop;
12661 
12662 	ASSERT(sdinfo != NULL);
12663 	ASSERT(sata_hba_inst != NULL);
12664 	ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD ||
12665 	    cache_op == SATAC_SF_DISABLE_READ_AHEAD ||
12666 	    cache_op == SATAC_SF_ENABLE_WRITE_CACHE ||
12667 	    cache_op == SATAC_SF_DISABLE_WRITE_CACHE);
12668 
12669 
12670 	/* Prepare packet for SET FEATURES COMMAND */
12671 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12672 	spx->txlt_sata_hba_inst = sata_hba_inst;
12673 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
12674 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12675 	if (spkt == NULL) {
12676 		rval = SATA_FAILURE;
12677 		goto failure;
12678 	}
12679 	/* Fill sata_pkt */
12680 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12681 	/* Timeout 30s */
12682 	spkt->satapkt_time = sata_default_pkt_time;
12683 	/* Synchronous mode, no callback, interrupts */
12684 	spkt->satapkt_op_mode =
12685 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12686 	spkt->satapkt_comp = NULL;
12687 	scmd = &spkt->satapkt_cmd;
12688 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
12689 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
12690 	scmd->satacmd_addr_type = 0;
12691 	scmd->satacmd_device_reg = 0;
12692 	scmd->satacmd_status_reg = 0;
12693 	scmd->satacmd_error_reg = 0;
12694 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
12695 	scmd->satacmd_features_reg = cache_op;
12696 
12697 	/* Transfer command to HBA */
12698 	hba_rval = (*SATA_START_FUNC(sata_hba_inst))(
12699 	    SATA_DIP(sata_hba_inst), spkt);
12700 
12701 #ifdef SATA_INJECT_FAULTS
12702 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
12703 #endif
12704 
12705 	if ((hba_rval != SATA_TRAN_ACCEPTED) ||
12706 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
12707 		/* Pkt execution failed */
12708 		switch (cache_op) {
12709 		case SATAC_SF_ENABLE_READ_AHEAD:
12710 			infop = "enabling read ahead failed";
12711 			break;
12712 		case SATAC_SF_DISABLE_READ_AHEAD:
12713 			infop = "disabling read ahead failed";
12714 			break;
12715 		case SATAC_SF_ENABLE_WRITE_CACHE:
12716 			infop = "enabling write cache failed";
12717 			break;
12718 		case SATAC_SF_DISABLE_WRITE_CACHE:
12719 			infop = "disabling write cache failed";
12720 			break;
12721 		}
12722 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
12723 		rval = SATA_RETRY;
12724 	}
12725 failure:
12726 	/* Free allocated resources */
12727 	if (spkt != NULL)
12728 		sata_pkt_free(spx);
12729 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
12730 	return (rval);
12731 }
12732 
12733 /*
12734  * Set Removable Media Status Notification (enable/disable)
12735  * state == 0 , disable
12736  * state != 0 , enable
12737  *
12738  * If operation fails, system log messgage is emitted.
12739  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
12740  */
12741 
12742 static int
12743 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
12744     int state)
12745 {
12746 	sata_pkt_t *spkt;
12747 	sata_cmd_t *scmd;
12748 	sata_pkt_txlate_t *spx;
12749 	int rval = SATA_SUCCESS;
12750 	char *infop;
12751 
12752 	ASSERT(sdinfo != NULL);
12753 	ASSERT(sata_hba_inst != NULL);
12754 
12755 	/* Prepare packet for SET FEATURES COMMAND */
12756 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12757 	spx->txlt_sata_hba_inst = sata_hba_inst;
12758 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
12759 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12760 	if (spkt == NULL) {
12761 		rval = SATA_FAILURE;
12762 		goto failure;
12763 	}
12764 	/* Fill sata_pkt */
12765 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12766 	/* Timeout 30s */
12767 	spkt->satapkt_time = sata_default_pkt_time;
12768 	/* Synchronous mode, no callback, interrupts */
12769 	spkt->satapkt_op_mode =
12770 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12771 	spkt->satapkt_comp = NULL;
12772 	scmd = &spkt->satapkt_cmd;
12773 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
12774 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
12775 	scmd->satacmd_addr_type = 0;
12776 	scmd->satacmd_device_reg = 0;
12777 	scmd->satacmd_status_reg = 0;
12778 	scmd->satacmd_error_reg = 0;
12779 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
12780 	if (state == 0)
12781 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN;
12782 	else
12783 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN;
12784 
12785 	/* Transfer command to HBA */
12786 	if (((*SATA_START_FUNC(sata_hba_inst))(
12787 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
12788 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
12789 		/* Pkt execution failed */
12790 		if (state == 0)
12791 			infop = "disabling Removable Media Status "
12792 			    "Notification failed";
12793 		else
12794 			infop = "enabling Removable Media Status "
12795 			    "Notification failed";
12796 
12797 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
12798 		rval = SATA_FAILURE;
12799 	}
12800 failure:
12801 	/* Free allocated resources */
12802 	if (spkt != NULL)
12803 		sata_pkt_free(spx);
12804 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
12805 	return (rval);
12806 }
12807 
12808 
12809 /*
12810  * Update state and copy port ss* values from passed sata_device structure.
12811  * sata_address is validated - if not valid, nothing is changed in sata_scsi
12812  * configuration struct.
12813  *
12814  * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function
12815  * regardless of the state in device argument.
12816  *
12817  * Port mutex should be held while calling this function.
12818  */
12819 static void
12820 sata_update_port_info(sata_hba_inst_t *sata_hba_inst,
12821     sata_device_t *sata_device)
12822 {
12823 	sata_cport_info_t *cportinfo;
12824 
12825 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT ||
12826 	    sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
12827 		if (SATA_NUM_CPORTS(sata_hba_inst) <=
12828 		    sata_device->satadev_addr.cport)
12829 			return;
12830 
12831 		cportinfo = SATA_CPORT_INFO(sata_hba_inst,
12832 		    sata_device->satadev_addr.cport);
12833 
12834 		ASSERT(mutex_owned(&cportinfo->cport_mutex));
12835 		cportinfo->cport_scr = sata_device->satadev_scr;
12836 
12837 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
12838 		cportinfo->cport_state &= ~(SATA_PSTATE_PWRON |
12839 		    SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
12840 		cportinfo->cport_state |=
12841 		    sata_device->satadev_state & SATA_PSTATE_VALID;
12842 	}
12843 }
12844 
12845 void
12846 sata_update_pmport_info(sata_hba_inst_t *sata_hba_inst,
12847     sata_device_t *sata_device)
12848 {
12849 	sata_pmport_info_t *pmportinfo;
12850 
12851 	if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT &&
12852 	    sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) ||
12853 	    SATA_NUM_PMPORTS(sata_hba_inst,
12854 	    sata_device->satadev_addr.cport) <
12855 	    sata_device->satadev_addr.pmport) {
12856 		SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
12857 		    "sata_update_port_info: error address %p.",
12858 		    &sata_device->satadev_addr);
12859 		return;
12860 	}
12861 
12862 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
12863 	    sata_device->satadev_addr.cport,
12864 	    sata_device->satadev_addr.pmport);
12865 
12866 	ASSERT(mutex_owned(&pmportinfo->pmport_mutex));
12867 	pmportinfo->pmport_scr = sata_device->satadev_scr;
12868 
12869 	/* Preserve SATA_PSTATE_SHUTDOWN flag */
12870 	pmportinfo->pmport_state &=
12871 	    ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
12872 	pmportinfo->pmport_state |=
12873 	    sata_device->satadev_state & SATA_PSTATE_VALID;
12874 }
12875 
12876 /*
12877  * Extract SATA port specification from an IOCTL argument.
12878  *
12879  * This function return the port the user land send us as is, unless it
12880  * cannot retrieve port spec, then -1 is returned.
12881  *
12882  * Support port multiplier.
12883  */
12884 static int32_t
12885 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp)
12886 {
12887 	int32_t port;
12888 
12889 	/* Extract port number from nvpair in dca structure  */
12890 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
12891 		SATA_LOG_D((sata_hba_inst, CE_NOTE,
12892 		    "sata_get_port_num: invalid port spec 0x%x in ioctl",
12893 		    port));
12894 		port = -1;
12895 	}
12896 
12897 	return (port);
12898 }
12899 
12900 /*
12901  * Get dev_info_t pointer to the device node pointed to by port argument.
12902  * NOTE: target argument is a value used in ioctls to identify
12903  * the AP - it is not a sata_address.
12904  * It is a combination of cport, pmport and address qualifier, encodded same
12905  * way as a scsi target number.
12906  * At this moment it carries only cport number.
12907  *
12908  * PMult hotplug is supported now.
12909  *
12910  * Returns dev_info_t pointer if target device was found, NULL otherwise.
12911  */
12912 
12913 static dev_info_t *
12914 sata_get_target_dip(dev_info_t *dip, uint8_t cport, uint8_t pmport)
12915 {
12916 	dev_info_t	*cdip = NULL;
12917 	int		target, tgt;
12918 	int 		circ;
12919 	uint8_t		qual;
12920 
12921 	sata_hba_inst_t	*sata_hba_inst;
12922 	scsi_hba_tran_t *scsi_hba_tran;
12923 
12924 	/* Get target id */
12925 	scsi_hba_tran = ddi_get_driver_private(dip);
12926 	if (scsi_hba_tran == NULL)
12927 		return (NULL);
12928 
12929 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
12930 
12931 	if (sata_hba_inst == NULL)
12932 		return (NULL);
12933 
12934 	/* Identify a port-mult by cport_info.cport_dev_type */
12935 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT)
12936 		qual = SATA_ADDR_DPMPORT;
12937 	else
12938 		qual = SATA_ADDR_DCPORT;
12939 
12940 	target = SATA_TO_SCSI_TARGET(cport, pmport, qual);
12941 
12942 	/* Retrieve target dip */
12943 	ndi_devi_enter(dip, &circ);
12944 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
12945 		dev_info_t *next = ddi_get_next_sibling(cdip);
12946 
12947 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
12948 		    DDI_PROP_DONTPASS, "target", -1);
12949 		if (tgt == -1) {
12950 			/*
12951 			 * This is actually an error condition, but not
12952 			 * a fatal one. Just continue the search.
12953 			 */
12954 			cdip = next;
12955 			continue;
12956 		}
12957 
12958 		if (tgt == target)
12959 			break;
12960 
12961 		cdip = next;
12962 	}
12963 	ndi_devi_exit(dip, circ);
12964 
12965 	return (cdip);
12966 }
12967 
12968 /*
12969  * Get dev_info_t pointer to the device node pointed to by port argument.
12970  * NOTE: target argument is a value used in ioctls to identify
12971  * the AP - it is not a sata_address.
12972  * It is a combination of cport, pmport and address qualifier, encoded same
12973  * way as a scsi target number.
12974  *
12975  * Returns dev_info_t pointer if target device was found, NULL otherwise.
12976  */
12977 
12978 static dev_info_t *
12979 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr)
12980 {
12981 	dev_info_t	*cdip = NULL;
12982 	int		target, tgt;
12983 	int 		circ;
12984 
12985 	target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual);
12986 
12987 	ndi_devi_enter(dip, &circ);
12988 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
12989 		dev_info_t *next = ddi_get_next_sibling(cdip);
12990 
12991 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
12992 		    DDI_PROP_DONTPASS, "target", -1);
12993 		if (tgt == -1) {
12994 			/*
12995 			 * This is actually an error condition, but not
12996 			 * a fatal one. Just continue the search.
12997 			 */
12998 			cdip = next;
12999 			continue;
13000 		}
13001 
13002 		if (tgt == target)
13003 			break;
13004 
13005 		cdip = next;
13006 	}
13007 	ndi_devi_exit(dip, circ);
13008 
13009 	return (cdip);
13010 }
13011 
13012 /*
13013  * Process sata port disconnect request.
13014  * Normally, cfgadm sata plugin will try to offline (unconfigure) the device
13015  * before this request. Nevertheless, if a device is still configured,
13016  * we need to attempt to offline and unconfigure device.
13017  * Regardless of the unconfigure operation results the port is marked as
13018  * deactivated and no access to the attached device is possible.
13019  * If the target node remains because unconfigure operation failed, its state
13020  * will be set to DEVICE_REMOVED, preventing it to be used again when a device
13021  * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure
13022  * the device and remove old target node.
13023  *
13024  * This function invokes sata_hba_inst->satahba_tran->
13025  * sata_tran_hotplug_ops->sata_tran_port_deactivate().
13026  * If successful, the device structure (if any) attached to the specified port
13027  * is removed and state of the port marked appropriately.
13028  * Failure of the port_deactivate may keep port in the physically active state,
13029  * or may fail the port.
13030  *
13031  * NOTE: Port multiplier is supported.
13032  */
13033 
13034 static int
13035 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst,
13036     sata_device_t *sata_device)
13037 {
13038 	sata_drive_info_t *sdinfo = NULL, *subsdinfo = NULL;
13039 	sata_cport_info_t *cportinfo = NULL;
13040 	sata_pmport_info_t *pmportinfo = NULL;
13041 	sata_pmult_info_t *pmultinfo = NULL;
13042 	sata_device_t subsdevice;
13043 	int cport, pmport, qual;
13044 	int rval = SATA_SUCCESS;
13045 	int npmport = 0;
13046 	int rv = 0;
13047 
13048 	cport = sata_device->satadev_addr.cport;
13049 	pmport = sata_device->satadev_addr.pmport;
13050 	qual = sata_device->satadev_addr.qual;
13051 
13052 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
13053 	if (qual == SATA_ADDR_DCPORT)
13054 		qual = SATA_ADDR_CPORT;
13055 	else
13056 		qual = SATA_ADDR_PMPORT;
13057 
13058 	/*
13059 	 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran->
13060 	 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
13061 	 * Do the sanity check.
13062 	 */
13063 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
13064 		/* No physical port deactivation supported. */
13065 		return (EINVAL);
13066 	}
13067 
13068 	/* Check the current state of the port */
13069 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
13070 	    (SATA_DIP(sata_hba_inst), sata_device);
13071 
13072 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
13073 
13074 	/*
13075 	 * Processing port mulitiplier
13076 	 */
13077 	if (qual == SATA_ADDR_CPORT &&
13078 	    SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) {
13079 		mutex_enter(&cportinfo->cport_mutex);
13080 
13081 		/* Check controller port status */
13082 		sata_update_port_info(sata_hba_inst, sata_device);
13083 		if (rval != SATA_SUCCESS ||
13084 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
13085 			/*
13086 			 * Device port status is unknown or it is in failed
13087 			 * state
13088 			 */
13089 			SATA_CPORT_STATE(sata_hba_inst, cport) =
13090 			    SATA_PSTATE_FAILED;
13091 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
13092 			    "sata_hba_ioctl: connect: failed to deactivate "
13093 			    "SATA port %d", cport);
13094 			mutex_exit(&cportinfo->cport_mutex);
13095 			return (EIO);
13096 		}
13097 
13098 		/* Disconnect all sub-devices. */
13099 		pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
13100 		if (pmultinfo != NULL) {
13101 
13102 			for (npmport = 0; npmport < SATA_NUM_PMPORTS(
13103 			    sata_hba_inst, cport); npmport ++) {
13104 				subsdinfo = SATA_PMPORT_DRV_INFO(
13105 				    sata_hba_inst, cport, npmport);
13106 				if (subsdinfo == NULL)
13107 					continue;
13108 
13109 				subsdevice.satadev_addr = subsdinfo->
13110 				    satadrv_addr;
13111 
13112 				mutex_exit(&cportinfo->cport_mutex);
13113 				if (sata_ioctl_disconnect(sata_hba_inst,
13114 				    &subsdevice) == SATA_SUCCESS) {
13115 					SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
13116 					"[Remove] device at port %d:%d "
13117 					"successfully.", cport, npmport);
13118 				}
13119 				mutex_enter(&cportinfo->cport_mutex);
13120 			}
13121 		}
13122 
13123 		/* Disconnect the port multiplier */
13124 		cportinfo->cport_state &= ~SATA_STATE_READY;
13125 		mutex_exit(&cportinfo->cport_mutex);
13126 
13127 		sata_device->satadev_addr.qual = qual;
13128 		rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
13129 		    (SATA_DIP(sata_hba_inst), sata_device);
13130 
13131 		sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
13132 		    SE_NO_HINT);
13133 
13134 		mutex_enter(&cportinfo->cport_mutex);
13135 		sata_update_port_info(sata_hba_inst, sata_device);
13136 		if (rval != SATA_SUCCESS &&
13137 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
13138 			cportinfo->cport_state = SATA_PSTATE_FAILED;
13139 			rv = EIO;
13140 		} else {
13141 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
13142 		}
13143 		mutex_exit(&cportinfo->cport_mutex);
13144 
13145 		return (rv);
13146 	}
13147 
13148 	/*
13149 	 * Process non-port-multiplier device - it could be a drive connected
13150 	 * to a port multiplier port or a controller port.
13151 	 */
13152 	if (qual == SATA_ADDR_PMPORT) {
13153 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
13154 		mutex_enter(&pmportinfo->pmport_mutex);
13155 		sata_update_pmport_info(sata_hba_inst, sata_device);
13156 		if (rval != SATA_SUCCESS ||
13157 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
13158 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
13159 			    SATA_PSTATE_FAILED;
13160 			SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
13161 			    "sata_hba_ioctl: connect: failed to deactivate "
13162 			    "SATA port %d:%d", cport, pmport);
13163 			mutex_exit(&pmportinfo->pmport_mutex);
13164 			return (EIO);
13165 		}
13166 
13167 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
13168 			sdinfo = pmportinfo->pmport_sata_drive;
13169 			ASSERT(sdinfo != NULL);
13170 		}
13171 
13172 		/*
13173 		 * Set port's dev_state to not ready - this will disable
13174 		 * an access to a potentially attached device.
13175 		 */
13176 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
13177 
13178 		/* Remove and release sata_drive info structure. */
13179 		if (sdinfo != NULL) {
13180 			if ((sdinfo->satadrv_type &
13181 			    SATA_VALID_DEV_TYPE) != 0) {
13182 				/*
13183 				 * If a target node exists, try to offline
13184 				 * a device and remove target node.
13185 				 */
13186 				mutex_exit(&pmportinfo->pmport_mutex);
13187 				(void) sata_offline_device(sata_hba_inst,
13188 				    sata_device, sdinfo);
13189 				mutex_enter(&pmportinfo->pmport_mutex);
13190 			}
13191 
13192 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
13193 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
13194 			(void) kmem_free((void *)sdinfo,
13195 			    sizeof (sata_drive_info_t));
13196 		}
13197 		mutex_exit(&pmportinfo->pmport_mutex);
13198 
13199 	} else if (qual == SATA_ADDR_CPORT) {
13200 		mutex_enter(&cportinfo->cport_mutex);
13201 		sata_update_port_info(sata_hba_inst, sata_device);
13202 		if (rval != SATA_SUCCESS ||
13203 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
13204 			/*
13205 			 * Device port status is unknown or it is in failed
13206 			 * state
13207 			 */
13208 			SATA_CPORT_STATE(sata_hba_inst, cport) =
13209 			    SATA_PSTATE_FAILED;
13210 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
13211 			    "sata_hba_ioctl: connect: failed to deactivate "
13212 			    "SATA port %d", cport);
13213 			mutex_exit(&cportinfo->cport_mutex);
13214 			return (EIO);
13215 		}
13216 
13217 		if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
13218 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
13219 			ASSERT(pmultinfo != NULL);
13220 		} else if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
13221 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
13222 			ASSERT(sdinfo != NULL);
13223 		}
13224 		cportinfo->cport_state &= ~SATA_STATE_READY;
13225 
13226 		if (sdinfo != NULL) {
13227 			if ((sdinfo->satadrv_type &
13228 			    SATA_VALID_DEV_TYPE) != 0) {
13229 				/*
13230 				 * If a target node exists, try to offline
13231 				 * a device and remove target node.
13232 				 */
13233 				mutex_exit(&cportinfo->cport_mutex);
13234 				(void) sata_offline_device(sata_hba_inst,
13235 				    sata_device, sdinfo);
13236 				mutex_enter(&cportinfo->cport_mutex);
13237 			}
13238 
13239 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
13240 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
13241 			(void) kmem_free((void *)sdinfo,
13242 			    sizeof (sata_drive_info_t));
13243 		}
13244 		mutex_exit(&cportinfo->cport_mutex);
13245 	}
13246 
13247 	/* Just ask HBA driver to deactivate port */
13248 	sata_device->satadev_addr.qual = qual;
13249 
13250 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
13251 	    (SATA_DIP(sata_hba_inst), sata_device);
13252 
13253 	/*
13254 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
13255 	 * without the hint (to force listener to investivate the state).
13256 	 */
13257 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
13258 	    SE_NO_HINT);
13259 
13260 	if (qual == SATA_ADDR_PMPORT) {
13261 		mutex_enter(&pmportinfo->pmport_mutex);
13262 		sata_update_pmport_info(sata_hba_inst, sata_device);
13263 
13264 		if (rval != SATA_SUCCESS &&
13265 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
13266 			/*
13267 			 * Port deactivation failure - do not change port
13268 			 * state unless the state returned by HBA indicates a
13269 			 * port failure.
13270 			 *
13271 			 * NOTE: device structures were released, so devices
13272 			 * now are invisible! Port reset is needed to
13273 			 * re-enumerate devices.
13274 			 */
13275 			pmportinfo->pmport_state = SATA_PSTATE_FAILED;
13276 			rv = EIO;
13277 		} else {
13278 			/*
13279 			 * Deactivation succeded. From now on the sata framework
13280 			 * will not care what is happening to the device, until
13281 			 * the port is activated again.
13282 			 */
13283 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
13284 		}
13285 		mutex_exit(&pmportinfo->pmport_mutex);
13286 	} else if (qual == SATA_ADDR_CPORT) {
13287 		mutex_enter(&cportinfo->cport_mutex);
13288 		sata_update_port_info(sata_hba_inst, sata_device);
13289 
13290 		if (rval != SATA_SUCCESS &&
13291 		    sata_device->satadev_state & SATA_PSTATE_FAILED) {
13292 			cportinfo->cport_state = SATA_PSTATE_FAILED;
13293 			rv = EIO;
13294 		} else {
13295 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
13296 		}
13297 		mutex_exit(&cportinfo->cport_mutex);
13298 	}
13299 
13300 	return (rv);
13301 }
13302 
13303 
13304 
13305 /*
13306  * Process sata port connect request
13307  * The sata cfgadm pluging will invoke this operation only if port was found
13308  * in the disconnect state (failed state is also treated as the disconnected
13309  * state).
13310  * DEVCTL_AP_CONNECT would invoke  sata_hba_inst->satahba_tran->
13311  * sata_tran_hotplug_ops->sata_tran_port_activate().
13312  * If successful and a device is found attached to the port,
13313  * the initialization sequence is executed to attach a device structure to
13314  * a port structure. The state of the port and a device would be set
13315  * appropriately.
13316  * The device is not set in configured state (system-wise) by this operation.
13317  *
13318  * Note, that activating the port may generate link events,
13319  * so it is important that following processing and the
13320  * event processing does not interfere with each other!
13321  *
13322  * This operation may remove port failed state and will
13323  * try to make port active and in good standing.
13324  *
13325  * NOTE: Port multiplier is supported.
13326  */
13327 
13328 static int
13329 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst,
13330     sata_device_t *sata_device)
13331 {
13332 	sata_pmport_info_t	*pmportinfo = NULL;
13333 	uint8_t cport, pmport, qual;
13334 	int rv = 0;
13335 
13336 	cport = sata_device->satadev_addr.cport;
13337 	pmport = sata_device->satadev_addr.pmport;
13338 	qual = sata_device->satadev_addr.qual;
13339 
13340 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
13341 	if (qual == SATA_ADDR_DCPORT)
13342 		qual = SATA_ADDR_CPORT;
13343 	else
13344 		qual = SATA_ADDR_PMPORT;
13345 
13346 	if (qual == SATA_ADDR_PMPORT)
13347 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
13348 
13349 	/*
13350 	 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->
13351 	 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate().
13352 	 * Perform sanity check now.
13353 	 */
13354 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) {
13355 		/* No physical port activation supported. */
13356 		return (EINVAL);
13357 	}
13358 
13359 	/* Just ask HBA driver to activate port */
13360 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
13361 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
13362 		/*
13363 		 * Port activation failure.
13364 		 */
13365 		if (qual == SATA_ADDR_CPORT) {
13366 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
13367 			    cport)->cport_mutex);
13368 			sata_update_port_info(sata_hba_inst, sata_device);
13369 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
13370 				SATA_CPORT_STATE(sata_hba_inst, cport) =
13371 				    SATA_PSTATE_FAILED;
13372 				SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
13373 				    "sata_hba_ioctl: connect: failed to "
13374 				    "activate SATA port %d", cport);
13375 			}
13376 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
13377 			    cport)->cport_mutex);
13378 		} else { /* port multiplier device port */
13379 			mutex_enter(&pmportinfo->pmport_mutex);
13380 			sata_update_pmport_info(sata_hba_inst, sata_device);
13381 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
13382 				SATA_PMPORT_STATE(sata_hba_inst, cport,
13383 				    pmport) = SATA_PSTATE_FAILED;
13384 				SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
13385 				    "sata_hba_ioctl: connect: failed to "
13386 				    "activate SATA port %d:%d", cport, pmport);
13387 			}
13388 			mutex_exit(&pmportinfo->pmport_mutex);
13389 		}
13390 		return (EIO);
13391 	}
13392 
13393 	/* Virgin port state - will be updated by the port re-probe. */
13394 	if (qual == SATA_ADDR_CPORT) {
13395 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
13396 		    cport)->cport_mutex);
13397 		SATA_CPORT_STATE(sata_hba_inst, cport) = 0;
13398 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
13399 		    cport)->cport_mutex);
13400 	} else { /* port multiplier device port */
13401 		mutex_enter(&pmportinfo->pmport_mutex);
13402 		SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0;
13403 		mutex_exit(&pmportinfo->pmport_mutex);
13404 	}
13405 
13406 	/*
13407 	 * Probe the port to find its state and attached device.
13408 	 */
13409 	if (sata_reprobe_port(sata_hba_inst, sata_device,
13410 	    SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE)
13411 		rv = EIO;
13412 
13413 	/*
13414 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
13415 	 * without the hint
13416 	 */
13417 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
13418 	    SE_NO_HINT);
13419 
13420 	/*
13421 	 * If there is a device attached to the port, emit
13422 	 * a message.
13423 	 */
13424 	if (sata_device->satadev_type != SATA_DTYPE_NONE) {
13425 
13426 		if (qual == SATA_ADDR_CPORT) {
13427 			if (sata_device->satadev_type == SATA_DTYPE_PMULT) {
13428 				sata_log(sata_hba_inst, CE_WARN,
13429 				    "SATA port multiplier detected "
13430 				    "at port %d", cport);
13431 			} else {
13432 				sata_log(sata_hba_inst, CE_WARN,
13433 				    "SATA device detected at port %d", cport);
13434 				if (sata_device->satadev_type ==
13435 				    SATA_DTYPE_UNKNOWN) {
13436 				/*
13437 				 * A device was not successfully identified
13438 				 */
13439 				sata_log(sata_hba_inst, CE_WARN,
13440 				    "Could not identify SATA "
13441 				    "device at port %d", cport);
13442 				}
13443 			}
13444 		} else { /* port multiplier device port */
13445 			sata_log(sata_hba_inst, CE_WARN,
13446 			    "SATA device detected at port %d:%d",
13447 			    cport, pmport);
13448 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
13449 				/*
13450 				 * A device was not successfully identified
13451 				 */
13452 				sata_log(sata_hba_inst, CE_WARN,
13453 				    "Could not identify SATA "
13454 				    "device at port %d:%d", cport, pmport);
13455 			}
13456 		}
13457 	}
13458 
13459 	return (rv);
13460 }
13461 
13462 
13463 /*
13464  * Process sata device unconfigure request.
13465  * The unconfigure operation uses generic nexus operation to
13466  * offline a device. It leaves a target device node attached.
13467  * and obviously sata_drive_info attached as well, because
13468  * from the hardware point of view nothing has changed.
13469  */
13470 static int
13471 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst,
13472     sata_device_t *sata_device)
13473 {
13474 	int rv = 0;
13475 	dev_info_t *tdip;
13476 
13477 	/* We are addressing attached device, not a port */
13478 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
13479 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
13480 	else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT)
13481 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
13482 
13483 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
13484 	    &sata_device->satadev_addr)) != NULL) {
13485 
13486 		if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) {
13487 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13488 			    "sata_hba_ioctl: unconfigure: "
13489 			    "failed to unconfigure device at SATA port %d:%d",
13490 			    sata_device->satadev_addr.cport,
13491 			    sata_device->satadev_addr.pmport));
13492 			rv = EIO;
13493 		}
13494 		/*
13495 		 * The target node devi_state should be marked with
13496 		 * DEVI_DEVICE_OFFLINE by ndi_devi_offline().
13497 		 * This would be the indication for cfgadm that
13498 		 * the AP node occupant state is 'unconfigured'.
13499 		 */
13500 
13501 	} else {
13502 		/*
13503 		 * This would indicate a failure on the part of cfgadm
13504 		 * to detect correct state of the node prior to this
13505 		 * call - one cannot unconfigure non-existing device.
13506 		 */
13507 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13508 		    "sata_hba_ioctl: unconfigure: "
13509 		    "attempt to unconfigure non-existing device "
13510 		    "at SATA port %d:%d",
13511 		    sata_device->satadev_addr.cport,
13512 		    sata_device->satadev_addr.pmport));
13513 		rv = ENXIO;
13514 	}
13515 	return (rv);
13516 }
13517 
13518 /*
13519  * Process sata device configure request
13520  * If port is in a failed state, operation is aborted - one has to use
13521  * an explicit connect or port activate request to try to get a port into
13522  * non-failed mode. Port reset wil also work in such situation.
13523  * If the port is in disconnected (shutdown) state, the connect operation is
13524  * attempted prior to any other action.
13525  * When port is in the active state, there is a device attached and the target
13526  * node exists, a device was most likely offlined.
13527  * If target node does not exist, a new target node is created. In both cases
13528  * an attempt is made to online (configure) the device.
13529  *
13530  * NOTE: Port multiplier is supported.
13531  */
13532 static int
13533 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst,
13534     sata_device_t *sata_device)
13535 {
13536 	int cport, pmport, qual;
13537 	int rval;
13538 	boolean_t target = TRUE;
13539 	sata_cport_info_t *cportinfo;
13540 	sata_pmport_info_t *pmportinfo = NULL;
13541 	dev_info_t *tdip;
13542 	sata_drive_info_t *sdinfo;
13543 
13544 	cport = sata_device->satadev_addr.cport;
13545 	pmport = sata_device->satadev_addr.pmport;
13546 	qual = sata_device->satadev_addr.qual;
13547 
13548 	/* Get current port state */
13549 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
13550 	    (SATA_DIP(sata_hba_inst), sata_device);
13551 
13552 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
13553 	if (qual == SATA_ADDR_DPMPORT) {
13554 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
13555 		mutex_enter(&pmportinfo->pmport_mutex);
13556 		sata_update_pmport_info(sata_hba_inst, sata_device);
13557 		if (rval != SATA_SUCCESS ||
13558 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
13559 			/*
13560 			 * Obviously, device on a failed port is not visible
13561 			 */
13562 			mutex_exit(&pmportinfo->pmport_mutex);
13563 			return (ENXIO);
13564 		}
13565 		mutex_exit(&pmportinfo->pmport_mutex);
13566 	} else {
13567 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
13568 		    cport)->cport_mutex);
13569 		sata_update_port_info(sata_hba_inst, sata_device);
13570 		if (rval != SATA_SUCCESS ||
13571 		    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
13572 			/*
13573 			 * Obviously, device on a failed port is not visible
13574 			 */
13575 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
13576 			    cport)->cport_mutex);
13577 			return (ENXIO);
13578 		}
13579 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
13580 		    cport)->cport_mutex);
13581 	}
13582 
13583 	if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) {
13584 		/* need to activate port */
13585 		target = FALSE;
13586 
13587 		/* Sanity check */
13588 		if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
13589 			return (ENXIO);
13590 
13591 		/* Just let HBA driver to activate port */
13592 		if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
13593 		    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
13594 			/*
13595 			 * Port activation failure - do not change port state
13596 			 * unless the state returned by HBA indicates a port
13597 			 * failure.
13598 			 */
13599 			if (qual == SATA_ADDR_DPMPORT) {
13600 				mutex_enter(&pmportinfo->pmport_mutex);
13601 				sata_update_pmport_info(sata_hba_inst,
13602 				    sata_device);
13603 				if (sata_device->satadev_state &
13604 				    SATA_PSTATE_FAILED)
13605 					pmportinfo->pmport_state =
13606 					    SATA_PSTATE_FAILED;
13607 				mutex_exit(&pmportinfo->pmport_mutex);
13608 			} else {
13609 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
13610 				    cport)->cport_mutex);
13611 				sata_update_port_info(sata_hba_inst,
13612 				    sata_device);
13613 				if (sata_device->satadev_state &
13614 				    SATA_PSTATE_FAILED)
13615 					cportinfo->cport_state =
13616 					    SATA_PSTATE_FAILED;
13617 				mutex_exit(&SATA_CPORT_INFO(
13618 				    sata_hba_inst, cport)->cport_mutex);
13619 			}
13620 		}
13621 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13622 		    "sata_hba_ioctl: configure: "
13623 		    "failed to activate SATA port %d:%d",
13624 		    cport, pmport));
13625 		return (EIO);
13626 	}
13627 	/*
13628 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
13629 	 * without the hint.
13630 	 */
13631 	sata_gen_sysevent(sata_hba_inst,
13632 	    &sata_device->satadev_addr, SE_NO_HINT);
13633 
13634 	/* Virgin port state */
13635 	if (qual == SATA_ADDR_DPMPORT) {
13636 		mutex_enter(&pmportinfo->pmport_mutex);
13637 		pmportinfo->pmport_state = 0;
13638 		mutex_exit(&pmportinfo->pmport_mutex);
13639 	} else {
13640 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
13641 		    cport)-> cport_mutex);
13642 		cportinfo->cport_state = 0;
13643 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
13644 		    cport)->cport_mutex);
13645 	}
13646 	/*
13647 	 * Always reprobe port, to get current device info.
13648 	 */
13649 	if (sata_reprobe_port(sata_hba_inst, sata_device,
13650 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
13651 		return (EIO);
13652 
13653 	if (sata_device->satadev_type != SATA_DTYPE_NONE && target == FALSE) {
13654 		if (qual == SATA_ADDR_DPMPORT) {
13655 			/*
13656 			 * That's the transition from "inactive" port
13657 			 * to active one with device attached.
13658 			 */
13659 			sata_log(sata_hba_inst, CE_WARN,
13660 			    "SATA device detected at port %d:%d",
13661 			    cport, pmport);
13662 		} else {
13663 			/*
13664 			 * When PM is attached to the cport and cport is
13665 			 * activated, every PM device port needs to be reprobed.
13666 			 * We need to emit message for all devices detected
13667 			 * at port multiplier's device ports.
13668 			 * Add such code here.
13669 			 * For now, just inform about device attached to
13670 			 * cport.
13671 			 */
13672 			sata_log(sata_hba_inst, CE_WARN,
13673 			    "SATA device detected at port %d", cport);
13674 		}
13675 	}
13676 
13677 	/*
13678 	 * This is where real configuration operation starts.
13679 	 *
13680 	 * When PM is attached to the cport and cport is activated,
13681 	 * devices attached PM device ports may have to be configured
13682 	 * explicitly. This may change when port multiplier is supported.
13683 	 * For now, configure only disks and other valid target devices.
13684 	 */
13685 	if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) {
13686 		if (qual == SATA_ADDR_DCPORT) {
13687 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
13688 				/*
13689 				 * A device was not successfully identified
13690 				 */
13691 				sata_log(sata_hba_inst, CE_WARN,
13692 				    "Could not identify SATA "
13693 				    "device at port %d", cport);
13694 			}
13695 		} else { /* port multiplier device port */
13696 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
13697 				/*
13698 				 * A device was not successfully identified
13699 				 */
13700 				sata_log(sata_hba_inst, CE_WARN,
13701 				    "Could not identify SATA "
13702 				    "device at port %d:%d", cport, pmport);
13703 			}
13704 		}
13705 		return (ENXIO);		/* No device to configure */
13706 	}
13707 
13708 	/*
13709 	 * Here we may have a device in reset condition,
13710 	 * but because we are just configuring it, there is
13711 	 * no need to process the reset other than just
13712 	 * to clear device reset condition in the HBA driver.
13713 	 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will
13714 	 * cause a first command sent the HBA driver with the request
13715 	 * to clear device reset condition.
13716 	 */
13717 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
13718 	if (qual == SATA_ADDR_DPMPORT)
13719 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
13720 	else
13721 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
13722 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
13723 	if (sdinfo == NULL) {
13724 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
13725 		return (ENXIO);
13726 	}
13727 	if (sdinfo->satadrv_event_flags &
13728 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
13729 		sdinfo->satadrv_event_flags = 0;
13730 	}
13731 	sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
13732 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
13733 
13734 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
13735 	    &sata_device->satadev_addr)) != NULL) {
13736 		/*
13737 		 * Target node exists. Verify, that it belongs
13738 		 * to existing, attached device and not to
13739 		 * a removed device.
13740 		 */
13741 		if (sata_check_device_removed(tdip) == B_TRUE) {
13742 			if (qual == SATA_ADDR_DPMPORT)
13743 				sata_log(sata_hba_inst, CE_WARN,
13744 				    "SATA device at port %d cannot be "
13745 				    "configured. "
13746 				    "Application(s) accessing "
13747 				    "previously attached device "
13748 				    "have to release it before newly "
13749 				    "inserted device can be made accessible.",
13750 				    cport);
13751 			else
13752 				sata_log(sata_hba_inst, CE_WARN,
13753 				    "SATA device at port %d:%d cannot be"
13754 				    "configured. "
13755 				    "Application(s) accessing "
13756 				    "previously attached device "
13757 				    "have to release it before newly "
13758 				    "inserted device can be made accessible.",
13759 				    cport, pmport);
13760 			return (EIO);
13761 		}
13762 		/*
13763 		 * Device was not removed and re-inserted.
13764 		 * Try to online it.
13765 		 */
13766 		if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) {
13767 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13768 			    "sata_hba_ioctl: configure: "
13769 			    "onlining device at SATA port "
13770 			    "%d:%d failed", cport, pmport));
13771 			return (EIO);
13772 		}
13773 
13774 		if (qual == SATA_ADDR_DPMPORT) {
13775 			mutex_enter(&pmportinfo->pmport_mutex);
13776 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
13777 			mutex_exit(&pmportinfo->pmport_mutex);
13778 		} else {
13779 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
13780 			    cport)->cport_mutex);
13781 			cportinfo-> cport_tgtnode_clean = B_TRUE;
13782 			mutex_exit(&SATA_CPORT_INFO(
13783 			    sata_hba_inst, cport)->cport_mutex);
13784 		}
13785 	} else {
13786 		/*
13787 		 * No target node - need to create a new target node.
13788 		 */
13789 		if (qual == SATA_ADDR_DPMPORT) {
13790 			mutex_enter(&pmportinfo->pmport_mutex);
13791 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
13792 			mutex_exit(&pmportinfo->pmport_mutex);
13793 		} else {
13794 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
13795 			    cport_mutex);
13796 			cportinfo-> cport_tgtnode_clean = B_TRUE;
13797 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
13798 			    cport_mutex);
13799 		}
13800 
13801 		tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
13802 		    sata_hba_inst, &sata_device->satadev_addr);
13803 		if (tdip == NULL) {
13804 			/* Configure operation failed */
13805 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13806 			    "sata_hba_ioctl: configure: "
13807 			    "configuring SATA device at port %d:%d "
13808 			    "failed", cport, pmport));
13809 			return (EIO);
13810 		}
13811 	}
13812 	return (0);
13813 }
13814 
13815 
13816 /*
13817  * Process ioctl deactivate port request.
13818  * Arbitrarily unconfigure attached device, if any.
13819  * Even if the unconfigure fails, proceed with the
13820  * port deactivation.
13821  *
13822  * NOTE: Port Multiplier is supported now.
13823  */
13824 
13825 static int
13826 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst,
13827     sata_device_t *sata_device)
13828 {
13829 	int cport, pmport, qual;
13830 	int rval, rv = 0;
13831 	int npmport;
13832 	sata_cport_info_t *cportinfo;
13833 	sata_pmport_info_t *pmportinfo;
13834 	sata_pmult_info_t *pmultinfo;
13835 	dev_info_t *tdip;
13836 	sata_drive_info_t *sdinfo = NULL;
13837 	sata_device_t subsdevice;
13838 
13839 	/* Sanity check */
13840 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL)
13841 		return (ENOTSUP);
13842 
13843 	cport = sata_device->satadev_addr.cport;
13844 	pmport = sata_device->satadev_addr.pmport;
13845 	qual = sata_device->satadev_addr.qual;
13846 
13847 	/* SCSI_TO_SATA_ADDR_QUAL() translate ap_id into a device qualifier */
13848 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
13849 	if (qual == SATA_ADDR_DCPORT)
13850 		qual = SATA_ADDR_CPORT;
13851 	else
13852 		qual = SATA_ADDR_PMPORT;
13853 
13854 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
13855 	if (qual == SATA_ADDR_PMPORT)
13856 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
13857 
13858 	/*
13859 	 * Processing port multiplier
13860 	 */
13861 	if (qual == SATA_ADDR_CPORT &&
13862 	    SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_PMULT) {
13863 		mutex_enter(&cportinfo->cport_mutex);
13864 
13865 		/* Deactivate all sub-deices */
13866 		pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
13867 		if (pmultinfo != NULL) {
13868 			for (npmport = 0; npmport < SATA_NUM_PMPORTS(
13869 			    sata_hba_inst, cport); npmport++) {
13870 
13871 				subsdevice.satadev_addr.cport = cport;
13872 				subsdevice.satadev_addr.pmport =
13873 				    (uint8_t)npmport;
13874 				subsdevice.satadev_addr.qual =
13875 				    SATA_ADDR_DPMPORT;
13876 
13877 				SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
13878 				    "sata_hba_ioctl: deactivate: trying to "
13879 				    "deactivate SATA port %d:%d",
13880 				    cport, npmport);
13881 
13882 				mutex_exit(&cportinfo->cport_mutex);
13883 				if (sata_ioctl_deactivate(sata_hba_inst,
13884 				    &subsdevice) == SATA_SUCCESS) {
13885 					SATADBG2(SATA_DBG_PMULT, sata_hba_inst,
13886 					    "[Deactivate] device at port %d:%d "
13887 					    "successfully.", cport, npmport);
13888 				}
13889 				mutex_enter(&cportinfo->cport_mutex);
13890 			}
13891 		}
13892 
13893 		/* Deactivate the port multiplier now. */
13894 		cportinfo->cport_state &= ~SATA_STATE_READY;
13895 		mutex_exit(&cportinfo->cport_mutex);
13896 
13897 		sata_device->satadev_addr.qual = qual;
13898 		rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
13899 		    (SATA_DIP(sata_hba_inst), sata_device);
13900 
13901 		sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
13902 		    SE_NO_HINT);
13903 
13904 		mutex_enter(&cportinfo->cport_mutex);
13905 		sata_update_port_info(sata_hba_inst, sata_device);
13906 		if (rval != SATA_SUCCESS) {
13907 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
13908 				cportinfo->cport_state = SATA_PSTATE_FAILED;
13909 			}
13910 			rv = EIO;
13911 		} else {
13912 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
13913 		}
13914 		mutex_exit(&cportinfo->cport_mutex);
13915 
13916 		return (rv);
13917 	}
13918 
13919 	/*
13920 	 * Process non-port-multiplier device - it could be a drive connected
13921 	 * to a port multiplier port or a controller port.
13922 	 */
13923 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
13924 	if (qual == SATA_ADDR_CPORT) {
13925 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
13926 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
13927 			/* deal only with valid devices */
13928 			if ((cportinfo->cport_dev_type &
13929 			    SATA_VALID_DEV_TYPE) != 0)
13930 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
13931 		}
13932 		cportinfo->cport_state &= ~SATA_STATE_READY;
13933 	} else {
13934 		/* Port multiplier device port */
13935 		mutex_enter(&pmportinfo->pmport_mutex);
13936 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
13937 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
13938 		    (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0)
13939 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
13940 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
13941 		mutex_exit(&pmportinfo->pmport_mutex);
13942 	}
13943 
13944 	if (sdinfo != NULL) {
13945 		/*
13946 		 * If a target node exists, try to offline a device and
13947 		 * to remove a target node.
13948 		 */
13949 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
13950 		    cport_mutex);
13951 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
13952 		    &sata_device->satadev_addr);
13953 		if (tdip != NULL) {
13954 			/* target node exist */
13955 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
13956 			    "sata_hba_ioctl: port deactivate: "
13957 			    "target node exists.", NULL);
13958 
13959 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) !=
13960 			    NDI_SUCCESS) {
13961 				SATA_LOG_D((sata_hba_inst, CE_WARN,
13962 				    "sata_hba_ioctl: port deactivate: "
13963 				    "failed to unconfigure device at port "
13964 				    "%d:%d before deactivating the port",
13965 				    cport, pmport));
13966 				/*
13967 				 * Set DEVICE REMOVED state in the target
13968 				 * node. It will prevent an access to
13969 				 * the device even when a new device is
13970 				 * attached, until the old target node is
13971 				 * released, removed and recreated for a new
13972 				 * device.
13973 				 */
13974 				sata_set_device_removed(tdip);
13975 
13976 				/*
13977 				 * Instruct the event daemon to try the
13978 				 * target node cleanup later.
13979 				 */
13980 				sata_set_target_node_cleanup(sata_hba_inst,
13981 				    &sata_device->satadev_addr);
13982 			}
13983 		}
13984 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
13985 		    cport_mutex);
13986 		/*
13987 		 * In any case, remove and release sata_drive_info
13988 		 * structure.
13989 		 */
13990 		if (qual == SATA_ADDR_CPORT) {
13991 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
13992 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
13993 		} else { /* port multiplier device port */
13994 			mutex_enter(&pmportinfo->pmport_mutex);
13995 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
13996 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
13997 			mutex_exit(&pmportinfo->pmport_mutex);
13998 		}
13999 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
14000 	}
14001 
14002 	if (qual == SATA_ADDR_CPORT) {
14003 		cportinfo->cport_state &= ~(SATA_STATE_PROBED |
14004 		    SATA_STATE_PROBING);
14005 	} else if (qual == SATA_ADDR_PMPORT) {
14006 		mutex_enter(&pmportinfo->pmport_mutex);
14007 		pmportinfo->pmport_state &= ~(SATA_STATE_PROBED |
14008 		    SATA_STATE_PROBING);
14009 		mutex_exit(&pmportinfo->pmport_mutex);
14010 	}
14011 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14012 
14013 	/* Just let HBA driver to deactivate port */
14014 	sata_device->satadev_addr.qual = qual;
14015 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
14016 	    (SATA_DIP(sata_hba_inst), sata_device);
14017 
14018 	/*
14019 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14020 	 * without the hint
14021 	 */
14022 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14023 	    SE_NO_HINT);
14024 
14025 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14026 	sata_update_port_info(sata_hba_inst, sata_device);
14027 	if (qual == SATA_ADDR_CPORT) {
14028 		if (rval != SATA_SUCCESS) {
14029 			/*
14030 			 * Port deactivation failure - do not change port state
14031 			 * unless the state returned by HBA indicates a port
14032 			 * failure.
14033 			 */
14034 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
14035 				SATA_CPORT_STATE(sata_hba_inst, cport) =
14036 				    SATA_PSTATE_FAILED;
14037 			}
14038 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14039 			    "sata_hba_ioctl: port deactivate: "
14040 			    "cannot deactivate SATA port %d", cport));
14041 			rv = EIO;
14042 		} else {
14043 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
14044 		}
14045 	} else {
14046 		mutex_enter(&pmportinfo->pmport_mutex);
14047 		if (rval != SATA_SUCCESS) {
14048 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
14049 				SATA_PMPORT_STATE(sata_hba_inst, cport,
14050 				    pmport) = SATA_PSTATE_FAILED;
14051 			}
14052 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14053 			    "sata_hba_ioctl: port deactivate: "
14054 			    "cannot deactivate SATA port %d:%d",
14055 			    cport, pmport));
14056 			rv = EIO;
14057 		} else {
14058 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
14059 		}
14060 		mutex_exit(&pmportinfo->pmport_mutex);
14061 	}
14062 
14063 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14064 
14065 	return (rv);
14066 }
14067 
14068 /*
14069  * Process ioctl port activate request.
14070  *
14071  * NOTE: Port multiplier is supported now.
14072  */
14073 static int
14074 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst,
14075     sata_device_t *sata_device)
14076 {
14077 	int cport, pmport, qual;
14078 	sata_cport_info_t *cportinfo;
14079 	sata_pmport_info_t *pmportinfo = NULL;
14080 	boolean_t dev_existed = TRUE;
14081 
14082 	/* Sanity check */
14083 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
14084 		return (ENOTSUP);
14085 
14086 	cport = sata_device->satadev_addr.cport;
14087 	pmport = sata_device->satadev_addr.pmport;
14088 	qual = sata_device->satadev_addr.qual;
14089 
14090 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
14091 
14092 	/*
14093 	 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL())
14094 	 * is a device. But what we are dealing with is port/pmport.
14095 	 */
14096 	ASSERT(qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_DPMPORT);
14097 	if (qual == SATA_ADDR_DCPORT)
14098 		sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT;
14099 	else
14100 		sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT;
14101 
14102 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14103 	if (qual == SATA_ADDR_PMPORT) {
14104 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
14105 		if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN ||
14106 		    pmportinfo->pmport_dev_type == SATA_DTYPE_NONE)
14107 			dev_existed = FALSE;
14108 	} else { /* cport */
14109 		if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN ||
14110 		    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
14111 			dev_existed = FALSE;
14112 	}
14113 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14114 
14115 	/* Just let HBA driver to activate port, if necessary */
14116 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
14117 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
14118 		/*
14119 		 * Port activation failure - do not change port state unless
14120 		 * the state returned by HBA indicates a port failure.
14121 		 */
14122 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14123 		    cport)->cport_mutex);
14124 		sata_update_port_info(sata_hba_inst, sata_device);
14125 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
14126 			if (qual == SATA_ADDR_PMPORT) {
14127 				mutex_enter(&pmportinfo->pmport_mutex);
14128 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
14129 				mutex_exit(&pmportinfo->pmport_mutex);
14130 			} else
14131 				cportinfo->cport_state = SATA_PSTATE_FAILED;
14132 
14133 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14134 			    cport)->cport_mutex);
14135 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14136 			    "sata_hba_ioctl: port activate: cannot activate "
14137 			    "SATA port %d:%d", cport, pmport));
14138 			return (EIO);
14139 		}
14140 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14141 	}
14142 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14143 	if (qual == SATA_ADDR_PMPORT) {
14144 		mutex_enter(&pmportinfo->pmport_mutex);
14145 		pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN;
14146 		mutex_exit(&pmportinfo->pmport_mutex);
14147 	} else
14148 		cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN;
14149 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14150 
14151 	/*
14152 	 * Re-probe port to find its current state and possibly attached device.
14153 	 * Port re-probing may change the cportinfo device type if device is
14154 	 * found attached.
14155 	 * If port probing failed, the device type would be set to
14156 	 * SATA_DTYPE_NONE.
14157 	 */
14158 	(void) sata_reprobe_port(sata_hba_inst, sata_device,
14159 	    SATA_DEV_IDENTIFY_RETRY);
14160 
14161 	/*
14162 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14163 	 * without the hint.
14164 	 */
14165 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
14166 	    SE_NO_HINT);
14167 
14168 	if (dev_existed == FALSE) {
14169 		if (qual == SATA_ADDR_PMPORT &&
14170 		    pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
14171 			/*
14172 			 * That's the transition from the "inactive" port state
14173 			 * or the active port without a device attached to the
14174 			 * active port state with a device attached.
14175 			 */
14176 			sata_log(sata_hba_inst, CE_WARN,
14177 			    "SATA device detected at port %d:%d",
14178 			    cport, pmport);
14179 		} else if (qual == SATA_ADDR_CPORT &&
14180 		    cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
14181 			/*
14182 			 * That's the transition from the "inactive" port state
14183 			 * or the active port without a device attached to the
14184 			 * active port state with a device attached.
14185 			 */
14186 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
14187 				sata_log(sata_hba_inst, CE_WARN,
14188 				    "SATA device detected at port %d", cport);
14189 			} else {
14190 				sata_log(sata_hba_inst, CE_WARN,
14191 				    "SATA port multiplier detected at port %d",
14192 				    cport);
14193 			}
14194 		}
14195 	}
14196 	return (0);
14197 }
14198 
14199 
14200 
14201 /*
14202  * Process ioctl reset port request.
14203  *
14204  * NOTE: Port-Multiplier is supported.
14205  */
14206 static int
14207 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst,
14208     sata_device_t *sata_device)
14209 {
14210 	int cport, pmport, qual;
14211 	int rv = 0;
14212 
14213 	cport = sata_device->satadev_addr.cport;
14214 	pmport = sata_device->satadev_addr.pmport;
14215 	qual = sata_device->satadev_addr.qual;
14216 
14217 	/*
14218 	 * The qual translate from ap_id (by SCSI_TO_SATA_ADDR_QUAL())
14219 	 * is a device. But what we are dealing with is port/pmport.
14220 	 */
14221 	if (qual == SATA_ADDR_DCPORT)
14222 		sata_device->satadev_addr.qual = qual = SATA_ADDR_CPORT;
14223 	else
14224 		sata_device->satadev_addr.qual = qual = SATA_ADDR_PMPORT;
14225 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
14226 
14227 	/* Sanity check */
14228 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
14229 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14230 		    "sata_hba_ioctl: sata_hba_tran missing required "
14231 		    "function sata_tran_reset_dport"));
14232 		return (ENOTSUP);
14233 	}
14234 
14235 	/* Ask HBA to reset port */
14236 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
14237 	    sata_device) != SATA_SUCCESS) {
14238 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14239 		    "sata_hba_ioctl: reset port: failed %d:%d",
14240 		    cport, pmport));
14241 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
14242 		    cport_mutex);
14243 		sata_update_port_info(sata_hba_inst, sata_device);
14244 		if (qual == SATA_ADDR_CPORT)
14245 			SATA_CPORT_STATE(sata_hba_inst, cport) =
14246 			    SATA_PSTATE_FAILED;
14247 		else {
14248 			mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport,
14249 			    pmport));
14250 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
14251 			    SATA_PSTATE_FAILED;
14252 			mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport,
14253 			    pmport));
14254 		}
14255 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
14256 		    cport_mutex);
14257 		rv = EIO;
14258 	}
14259 	/*
14260 	 * Beacuse the port was reset, it should be probed and
14261 	 * attached device reinitialized. At this point the
14262 	 * port state is unknown - it's state is HBA-specific.
14263 	 * Re-probe port to get its state.
14264 	 */
14265 	if (sata_reprobe_port(sata_hba_inst, sata_device,
14266 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) {
14267 		rv = EIO;
14268 	}
14269 	return (rv);
14270 }
14271 
14272 /*
14273  * Process ioctl reset device request.
14274  *
14275  * NOTE: Port multiplier is supported.
14276  */
14277 static int
14278 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst,
14279     sata_device_t *sata_device)
14280 {
14281 	sata_drive_info_t *sdinfo = NULL;
14282 	sata_pmult_info_t *pmultinfo = NULL;
14283 	int cport, pmport;
14284 	int rv = 0;
14285 
14286 	/* Sanity check */
14287 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
14288 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14289 		    "sata_hba_ioctl: sata_hba_tran missing required "
14290 		    "function sata_tran_reset_dport"));
14291 		return (ENOTSUP);
14292 	}
14293 
14294 	cport = sata_device->satadev_addr.cport;
14295 	pmport = sata_device->satadev_addr.pmport;
14296 
14297 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14298 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
14299 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
14300 		    SATA_DTYPE_PMULT)
14301 			pmultinfo = SATA_CPORT_INFO(sata_hba_inst, cport)->
14302 			    cport_devp.cport_sata_pmult;
14303 		else
14304 			sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
14305 			    sata_device->satadev_addr.cport);
14306 	} else { /* port multiplier */
14307 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
14308 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
14309 		    sata_device->satadev_addr.cport,
14310 		    sata_device->satadev_addr.pmport);
14311 	}
14312 	if (sdinfo == NULL && pmultinfo == NULL) {
14313 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14314 		return (EINVAL);
14315 	}
14316 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14317 
14318 	/* Ask HBA to reset device */
14319 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
14320 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
14321 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14322 		    "sata_hba_ioctl: reset device: failed at port %d:%d",
14323 		    cport, pmport));
14324 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
14325 		    cport_mutex);
14326 		sata_update_port_info(sata_hba_inst, sata_device);
14327 		/*
14328 		 * Device info structure remains attached. Another device reset
14329 		 * or port disconnect/connect and re-probing is
14330 		 * needed to change it's state
14331 		 */
14332 		if (sdinfo != NULL) {
14333 			sdinfo->satadrv_state &= ~SATA_STATE_READY;
14334 			sdinfo->satadrv_state |= SATA_DSTATE_FAILED;
14335 		} else if (pmultinfo != NULL) {
14336 			pmultinfo->pmult_state &= ~SATA_STATE_READY;
14337 			pmultinfo->pmult_state |= SATA_DSTATE_FAILED;
14338 		}
14339 
14340 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14341 		rv = EIO;
14342 	}
14343 	/*
14344 	 * If attached device was a port multiplier, some extra processing
14345 	 * may be needed to bring it back. SATA specification requies a
14346 	 * mandatory software reset on host port to reliably enumerate a port
14347 	 * multiplier, the HBA driver should handle that after reset
14348 	 * operation.
14349 	 */
14350 	return (rv);
14351 }
14352 
14353 
14354 /*
14355  * Process ioctl reset all request.
14356  */
14357 static int
14358 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst)
14359 {
14360 	sata_device_t sata_device;
14361 	int rv = 0;
14362 	int tcport;
14363 	int tpmport = 0;
14364 
14365 	sata_device.satadev_rev = SATA_DEVICE_REV;
14366 
14367 	/*
14368 	 * There is no protection here for configured devices.
14369 	 */
14370 	/* Sanity check */
14371 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
14372 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14373 		    "sata_hba_ioctl: sata_hba_tran missing required "
14374 		    "function sata_tran_reset_dport"));
14375 		return (ENOTSUP);
14376 	}
14377 
14378 	/*
14379 	 * Need to lock all ports, not just one.
14380 	 * If any port is locked by event processing, fail the whole operation.
14381 	 * One port is already locked, but for simplicity lock it again.
14382 	 */
14383 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
14384 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
14385 		    cport_mutex);
14386 		if (((SATA_CPORT_INFO(sata_hba_inst, tcport)->
14387 		    cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) {
14388 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
14389 			    cport_mutex);
14390 			rv = EBUSY;
14391 			break;
14392 		} else {
14393 			/*
14394 			 * It is enough to lock cport in command-based
14395 			 * switching mode.
14396 			 */
14397 			SATA_CPORT_INFO(sata_hba_inst, tcport)->
14398 			    cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
14399 		}
14400 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
14401 		    cport_mutex);
14402 	}
14403 
14404 	if (rv == 0) {
14405 		/*
14406 		 * All cports were successfully locked.
14407 		 * Reset main SATA controller.
14408 		 * Set the device address to port 0, to have a valid device
14409 		 * address.
14410 		 */
14411 		sata_device.satadev_addr.qual = SATA_ADDR_CNTRL;
14412 		sata_device.satadev_addr.cport = 0;
14413 		sata_device.satadev_addr.pmport = 0;
14414 
14415 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
14416 		    (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) {
14417 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14418 			    "sata_hba_ioctl: reset controller failed"));
14419 			return (EIO);
14420 		}
14421 		/*
14422 		 * Because ports were reset, port states are unknown.
14423 		 * They should be re-probed to get their state and
14424 		 * attached devices should be reinitialized.
14425 		 */
14426 		for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst);
14427 		    tcport++) {
14428 			sata_device.satadev_addr.cport = tcport;
14429 			sata_device.satadev_addr.pmport = tpmport;
14430 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
14431 
14432 			/*
14433 			 * The sata_reprobe_port() will mark a
14434 			 * SATA_EVNT_DEVICE_RESET event on the port
14435 			 * multiplier, all its sub-ports will be probed by
14436 			 * sata daemon afterwards.
14437 			 */
14438 			if (sata_reprobe_port(sata_hba_inst, &sata_device,
14439 			    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
14440 				rv = EIO;
14441 		}
14442 	}
14443 	/*
14444 	 * Unlock all ports
14445 	 */
14446 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
14447 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
14448 		    cport_mutex);
14449 		SATA_CPORT_INFO(sata_hba_inst, tcport)->
14450 		    cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
14451 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
14452 		    cport_mutex);
14453 	}
14454 
14455 	/*
14456 	 * This operation returns EFAULT if either reset
14457 	 * controller failed or a re-probing of any port failed.
14458 	 */
14459 	return (rv);
14460 }
14461 
14462 
14463 /*
14464  * Process ioctl port self test request.
14465  *
14466  * NOTE: Port multiplier code is not completed nor tested.
14467  */
14468 static int
14469 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst,
14470     sata_device_t *sata_device)
14471 {
14472 	int cport, pmport, qual;
14473 	int rv = 0;
14474 
14475 	/* Sanity check */
14476 	if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL)
14477 		return (ENOTSUP);
14478 
14479 	cport = sata_device->satadev_addr.cport;
14480 	pmport = sata_device->satadev_addr.pmport;
14481 	qual = sata_device->satadev_addr.qual;
14482 
14483 	/*
14484 	 * There is no protection here for a configured
14485 	 * device attached to this port.
14486 	 */
14487 
14488 	if ((*SATA_SELFTEST_FUNC(sata_hba_inst))
14489 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
14490 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14491 		    "sata_hba_ioctl: port selftest: "
14492 		    "failed port %d:%d", cport, pmport));
14493 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
14494 		    cport_mutex);
14495 		sata_update_port_info(sata_hba_inst, sata_device);
14496 		if (qual == SATA_ADDR_CPORT)
14497 			SATA_CPORT_STATE(sata_hba_inst, cport) =
14498 			    SATA_PSTATE_FAILED;
14499 		else { /* port multiplier device port */
14500 			mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst,
14501 			    cport, pmport));
14502 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
14503 			    SATA_PSTATE_FAILED;
14504 			mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst,
14505 			    cport, pmport));
14506 		}
14507 
14508 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
14509 		    cport_mutex);
14510 		return (EIO);
14511 	}
14512 	/*
14513 	 * Beacuse the port was reset in the course of testing, it should be
14514 	 * re-probed and attached device state should be restored. At this
14515 	 * point the port state is unknown - it's state is HBA-specific.
14516 	 * Force port re-probing to get it into a known state.
14517 	 */
14518 	if (sata_reprobe_port(sata_hba_inst, sata_device,
14519 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
14520 		rv = EIO;
14521 	return (rv);
14522 }
14523 
14524 
14525 /*
14526  * sata_cfgadm_state:
14527  * Use the sata port state and state of the target node to figure out
14528  * the cfgadm_state.
14529  *
14530  * The port argument is a value with encoded cport,
14531  * pmport and address qualifier, in the same manner as a scsi target number.
14532  * SCSI_TO_SATA_CPORT macro extracts cport number,
14533  * SCSI_TO_SATA_PMPORT extracts pmport number and
14534  * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag.
14535  *
14536  * Port multiplier is supported.
14537  */
14538 
14539 static void
14540 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port,
14541     devctl_ap_state_t *ap_state)
14542 {
14543 	uint8_t		cport, pmport, qual;
14544 	uint32_t	port_state, pmult_state;
14545 	uint32_t	dev_type;
14546 	sata_drive_info_t *sdinfo;
14547 
14548 	cport = SCSI_TO_SATA_CPORT(port);
14549 	pmport = SCSI_TO_SATA_PMPORT(port);
14550 	qual = SCSI_TO_SATA_ADDR_QUAL(port);
14551 
14552 	/* Check cport state */
14553 	port_state = SATA_CPORT_STATE(sata_hba_inst, cport);
14554 	if (port_state & SATA_PSTATE_SHUTDOWN ||
14555 	    port_state & SATA_PSTATE_FAILED) {
14556 		ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
14557 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
14558 		if (port_state & SATA_PSTATE_FAILED)
14559 			ap_state->ap_condition = AP_COND_FAILED;
14560 		else
14561 			ap_state->ap_condition = AP_COND_UNKNOWN;
14562 
14563 		return;
14564 	}
14565 
14566 	/* cport state is okay. Now check pmport state */
14567 	if (qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) {
14568 		/* Sanity check */
14569 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
14570 		    SATA_DTYPE_PMULT || SATA_PMPORT_INFO(sata_hba_inst,
14571 		    cport, pmport) == NULL)
14572 			return;
14573 		port_state = SATA_PMPORT_STATE(sata_hba_inst, cport, pmport);
14574 		if (port_state & SATA_PSTATE_SHUTDOWN ||
14575 		    port_state & SATA_PSTATE_FAILED) {
14576 			ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
14577 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
14578 			if (port_state & SATA_PSTATE_FAILED)
14579 				ap_state->ap_condition = AP_COND_FAILED;
14580 			else
14581 				ap_state->ap_condition = AP_COND_UNKNOWN;
14582 
14583 			return;
14584 		}
14585 	}
14586 
14587 	/* Port is enabled and ready */
14588 	if (qual == SATA_ADDR_DCPORT || qual == SATA_ADDR_CPORT)
14589 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst, cport);
14590 	else
14591 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport, pmport);
14592 
14593 	switch (dev_type) {
14594 	case SATA_DTYPE_NONE:
14595 	{
14596 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
14597 		ap_state->ap_condition = AP_COND_OK;
14598 		/* No device attached */
14599 		ap_state->ap_rstate = AP_RSTATE_EMPTY;
14600 		break;
14601 	}
14602 	case SATA_DTYPE_PMULT:
14603 	{
14604 		/* Need to check port multiplier state */
14605 		ASSERT(qual == SATA_ADDR_DCPORT);
14606 		pmult_state = SATA_PMULT_INFO(sata_hba_inst, cport)->
14607 		    pmult_state;
14608 		if (pmult_state & (SATA_PSTATE_SHUTDOWN|SATA_PSTATE_FAILED)) {
14609 			ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
14610 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
14611 			if (pmult_state & SATA_PSTATE_FAILED)
14612 				ap_state->ap_condition = AP_COND_FAILED;
14613 			else
14614 				ap_state->ap_condition = AP_COND_UNKNOWN;
14615 
14616 			return;
14617 		}
14618 
14619 		/* Port multiplier is not configurable */
14620 		ap_state->ap_ostate = AP_OSTATE_CONFIGURED;
14621 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
14622 		ap_state->ap_condition = AP_COND_OK;
14623 		break;
14624 	}
14625 
14626 	case SATA_DTYPE_ATADISK:
14627 	case SATA_DTYPE_ATAPICD:
14628 	case SATA_DTYPE_ATAPITAPE:
14629 	case SATA_DTYPE_ATAPIDISK:
14630 	{
14631 		dev_info_t *tdip = NULL;
14632 		dev_info_t *dip = NULL;
14633 		int circ;
14634 
14635 		dip = SATA_DIP(sata_hba_inst);
14636 		tdip = sata_get_target_dip(dip, cport, pmport);
14637 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
14638 		if (tdip != NULL) {
14639 			ndi_devi_enter(dip, &circ);
14640 			mutex_enter(&(DEVI(tdip)->devi_lock));
14641 			if (DEVI_IS_DEVICE_REMOVED(tdip)) {
14642 				/*
14643 				 * There could be the case where previously
14644 				 * configured and opened device was removed
14645 				 * and unknown device was plugged.
14646 				 * In such case we want to show a device, and
14647 				 * its configured or unconfigured state but
14648 				 * indicate unusable condition untill the
14649 				 * old target node is released and removed.
14650 				 */
14651 				ap_state->ap_condition = AP_COND_UNUSABLE;
14652 			} else {
14653 				mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst,
14654 				    cport));
14655 				sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
14656 				    cport);
14657 				if (sdinfo != NULL) {
14658 					if ((sdinfo->satadrv_state &
14659 					    SATA_DSTATE_FAILED) != 0)
14660 						ap_state->ap_condition =
14661 						    AP_COND_FAILED;
14662 					else
14663 						ap_state->ap_condition =
14664 						    AP_COND_OK;
14665 				} else {
14666 					ap_state->ap_condition =
14667 					    AP_COND_UNKNOWN;
14668 				}
14669 				mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst,
14670 				    cport));
14671 			}
14672 			if ((DEVI_IS_DEVICE_OFFLINE(tdip)) ||
14673 			    (DEVI_IS_DEVICE_DOWN(tdip))) {
14674 				ap_state->ap_ostate =
14675 				    AP_OSTATE_UNCONFIGURED;
14676 			} else {
14677 				ap_state->ap_ostate =
14678 				    AP_OSTATE_CONFIGURED;
14679 			}
14680 			mutex_exit(&(DEVI(tdip)->devi_lock));
14681 			ndi_devi_exit(dip, circ);
14682 		} else {
14683 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
14684 			ap_state->ap_condition = AP_COND_UNKNOWN;
14685 		}
14686 		break;
14687 	}
14688 	default:
14689 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
14690 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
14691 		ap_state->ap_condition = AP_COND_UNKNOWN;
14692 		/*
14693 		 * This is actually internal error condition (non fatal),
14694 		 * because we have already checked all defined device types.
14695 		 */
14696 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14697 		    "sata_cfgadm_state: Internal error: "
14698 		    "unknown device type"));
14699 		break;
14700 	}
14701 }
14702 
14703 
14704 /*
14705  * Process ioctl get device path request.
14706  *
14707  * NOTE: Port multiplier has no target dip. Devices connected to port
14708  * multiplier have target node attached to the HBA node. The only difference
14709  * between them and the directly-attached device node is a target address.
14710  */
14711 static int
14712 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst,
14713     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
14714 {
14715 	char path[MAXPATHLEN];
14716 	uint32_t size;
14717 	dev_info_t *tdip;
14718 
14719 	(void) strcpy(path, "/devices");
14720 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
14721 	    &sata_device->satadev_addr)) == NULL) {
14722 		/*
14723 		 * No such device. If this is a request for a size, do not
14724 		 * return EINVAL for non-existing target, because cfgadm
14725 		 * will then indicate a meaningless ioctl failure.
14726 		 * If this is a request for a path, indicate invalid
14727 		 * argument.
14728 		 */
14729 		if (ioc->get_size == 0)
14730 			return (EINVAL);
14731 	} else {
14732 		(void) ddi_pathname(tdip, path + strlen(path));
14733 	}
14734 	size = strlen(path) + 1;
14735 
14736 	if (ioc->get_size != 0) {
14737 		if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz,
14738 		    mode) != 0)
14739 			return (EFAULT);
14740 	} else {
14741 		if (ioc->bufsiz != size)
14742 			return (EINVAL);
14743 
14744 		else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz,
14745 		    mode) != 0)
14746 			return (EFAULT);
14747 	}
14748 	return (0);
14749 }
14750 
14751 /*
14752  * Process ioctl get attachment point type request.
14753  *
14754  * NOTE: Port multiplier is supported.
14755  */
14756 static	int
14757 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst,
14758     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
14759 {
14760 	uint32_t	type_len;
14761 	const char	*ap_type;
14762 	int		dev_type;
14763 
14764 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
14765 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst,
14766 		    sata_device->satadev_addr.cport);
14767 	else /* pmport */
14768 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst,
14769 		    sata_device->satadev_addr.cport,
14770 		    sata_device->satadev_addr.pmport);
14771 
14772 	switch (dev_type) {
14773 	case SATA_DTYPE_NONE:
14774 		ap_type = "port";
14775 		break;
14776 
14777 	case SATA_DTYPE_ATADISK:
14778 	case SATA_DTYPE_ATAPIDISK:
14779 		ap_type = "disk";
14780 		break;
14781 
14782 	case SATA_DTYPE_ATAPICD:
14783 		ap_type = "cd/dvd";
14784 		break;
14785 
14786 	case SATA_DTYPE_ATAPITAPE:
14787 		ap_type = "tape";
14788 		break;
14789 
14790 	case SATA_DTYPE_PMULT:
14791 		ap_type = "sata-pmult";
14792 		break;
14793 
14794 	case SATA_DTYPE_UNKNOWN:
14795 		ap_type = "unknown";
14796 		break;
14797 
14798 	default:
14799 		ap_type = "unsupported";
14800 		break;
14801 
14802 	} /* end of dev_type switch */
14803 
14804 	type_len = strlen(ap_type) + 1;
14805 
14806 	if (ioc->get_size) {
14807 		if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz,
14808 		    mode) != 0)
14809 			return (EFAULT);
14810 	} else {
14811 		if (ioc->bufsiz != type_len)
14812 			return (EINVAL);
14813 
14814 		if (ddi_copyout((void *)ap_type, ioc->buf,
14815 		    ioc->bufsiz, mode) != 0)
14816 			return (EFAULT);
14817 	}
14818 	return (0);
14819 
14820 }
14821 
14822 /*
14823  * Process ioctl get device model info request.
14824  * This operation should return to cfgadm the device model
14825  * information string
14826  *
14827  * NOTE: Port multiplier is supported.
14828  */
14829 static	int
14830 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst,
14831     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
14832 {
14833 	sata_drive_info_t *sdinfo;
14834 	uint32_t info_len;
14835 	char ap_info[SATA_ID_MODEL_LEN + 1];
14836 
14837 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14838 	    sata_device->satadev_addr.cport)->cport_mutex);
14839 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
14840 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
14841 		    sata_device->satadev_addr.cport);
14842 	else /* port multiplier */
14843 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
14844 		    sata_device->satadev_addr.cport,
14845 		    sata_device->satadev_addr.pmport);
14846 	if (sdinfo == NULL) {
14847 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14848 		    sata_device->satadev_addr.cport)->cport_mutex);
14849 		return (EINVAL);
14850 	}
14851 
14852 #ifdef	_LITTLE_ENDIAN
14853 	swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
14854 #else	/* _LITTLE_ENDIAN */
14855 	bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
14856 #endif	/* _LITTLE_ENDIAN */
14857 
14858 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14859 	    sata_device->satadev_addr.cport)->cport_mutex);
14860 
14861 	ap_info[SATA_ID_MODEL_LEN] = '\0';
14862 
14863 	info_len = strlen(ap_info) + 1;
14864 
14865 	if (ioc->get_size) {
14866 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
14867 		    mode) != 0)
14868 			return (EFAULT);
14869 	} else {
14870 		if (ioc->bufsiz < info_len)
14871 			return (EINVAL);
14872 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
14873 		    mode) != 0)
14874 			return (EFAULT);
14875 	}
14876 	return (0);
14877 }
14878 
14879 
14880 /*
14881  * Process ioctl get device firmware revision info request.
14882  * This operation should return to cfgadm the device firmware revision
14883  * information string
14884  *
14885  * Port multiplier is supported.
14886  */
14887 static	int
14888 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst,
14889     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
14890 {
14891 	sata_drive_info_t *sdinfo;
14892 	uint32_t info_len;
14893 	char ap_info[SATA_ID_FW_LEN + 1];
14894 
14895 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14896 	    sata_device->satadev_addr.cport)->cport_mutex);
14897 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
14898 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
14899 		    sata_device->satadev_addr.cport);
14900 	else /* port multiplier */
14901 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
14902 		    sata_device->satadev_addr.cport,
14903 		    sata_device->satadev_addr.pmport);
14904 	if (sdinfo == NULL) {
14905 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14906 		    sata_device->satadev_addr.cport)->cport_mutex);
14907 		return (EINVAL);
14908 	}
14909 
14910 #ifdef	_LITTLE_ENDIAN
14911 	swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
14912 #else	/* _LITTLE_ENDIAN */
14913 	bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
14914 #endif	/* _LITTLE_ENDIAN */
14915 
14916 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14917 	    sata_device->satadev_addr.cport)->cport_mutex);
14918 
14919 	ap_info[SATA_ID_FW_LEN] = '\0';
14920 
14921 	info_len = strlen(ap_info) + 1;
14922 
14923 	if (ioc->get_size) {
14924 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
14925 		    mode) != 0)
14926 			return (EFAULT);
14927 	} else {
14928 		if (ioc->bufsiz < info_len)
14929 			return (EINVAL);
14930 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
14931 		    mode) != 0)
14932 			return (EFAULT);
14933 	}
14934 	return (0);
14935 }
14936 
14937 
14938 /*
14939  * Process ioctl get device serial number info request.
14940  * This operation should return to cfgadm the device serial number string.
14941  *
14942  * NOTE: Port multiplier is supported.
14943  */
14944 static	int
14945 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst,
14946     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
14947 {
14948 	sata_drive_info_t *sdinfo;
14949 	uint32_t info_len;
14950 	char ap_info[SATA_ID_SERIAL_LEN + 1];
14951 
14952 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14953 	    sata_device->satadev_addr.cport)->cport_mutex);
14954 	if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
14955 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
14956 		    sata_device->satadev_addr.cport);
14957 	else /* port multiplier */
14958 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
14959 		    sata_device->satadev_addr.cport,
14960 		    sata_device->satadev_addr.pmport);
14961 	if (sdinfo == NULL) {
14962 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14963 		    sata_device->satadev_addr.cport)->cport_mutex);
14964 		return (EINVAL);
14965 	}
14966 
14967 #ifdef	_LITTLE_ENDIAN
14968 	swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
14969 #else	/* _LITTLE_ENDIAN */
14970 	bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
14971 #endif	/* _LITTLE_ENDIAN */
14972 
14973 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14974 	    sata_device->satadev_addr.cport)->cport_mutex);
14975 
14976 	ap_info[SATA_ID_SERIAL_LEN] = '\0';
14977 
14978 	info_len = strlen(ap_info) + 1;
14979 
14980 	if (ioc->get_size) {
14981 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
14982 		    mode) != 0)
14983 			return (EFAULT);
14984 	} else {
14985 		if (ioc->bufsiz < info_len)
14986 			return (EINVAL);
14987 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
14988 		    mode) != 0)
14989 			return (EFAULT);
14990 	}
14991 	return (0);
14992 }
14993 
14994 
14995 /*
14996  * Preset scsi extended sense data (to NO SENSE)
14997  * First 18 bytes of the sense data are preset to current valid sense
14998  * with a key NO SENSE data.
14999  *
15000  * Returns void
15001  */
15002 static void
15003 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense)
15004 {
15005 	sense->es_valid = 1;		/* Valid sense */
15006 	sense->es_class = CLASS_EXTENDED_SENSE;	/* 0x70 - current err */
15007 	sense->es_key = KEY_NO_SENSE;
15008 	sense->es_info_1 = 0;
15009 	sense->es_info_2 = 0;
15010 	sense->es_info_3 = 0;
15011 	sense->es_info_4 = 0;
15012 	sense->es_add_len = 10;	/* Additional length - replace with a def */
15013 	sense->es_cmd_info[0] = 0;
15014 	sense->es_cmd_info[1] = 0;
15015 	sense->es_cmd_info[2] = 0;
15016 	sense->es_cmd_info[3] = 0;
15017 	sense->es_add_code = 0;
15018 	sense->es_qual_code = 0;
15019 }
15020 
15021 /*
15022  * Register a legacy cmdk-style devid for the target (disk) device.
15023  *
15024  * Note: This function is called only when the HBA devinfo node has the
15025  * property "use-cmdk-devid-format" set. This property indicates that
15026  * devid compatible with old cmdk (target) driver is to be generated
15027  * for any target device attached to this controller. This will take
15028  * precedence over the devid generated by sd (target) driver.
15029  * This function is derived from cmdk_devid_setup() function in cmdk.c.
15030  */
15031 static void
15032 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo)
15033 {
15034 	char	*hwid;
15035 	int	modlen;
15036 	int	serlen;
15037 	int	rval;
15038 	ddi_devid_t	devid;
15039 
15040 	/*
15041 	 * device ID is a concatanation of model number, "=", serial number.
15042 	 */
15043 	hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP);
15044 	bcopy(&sdinfo->satadrv_id.ai_model, hwid,
15045 	    sizeof (sdinfo->satadrv_id.ai_model));
15046 	swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model));
15047 	modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model));
15048 	if (modlen == 0)
15049 		goto err;
15050 	hwid[modlen++] = '=';
15051 	bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen],
15052 	    sizeof (sdinfo->satadrv_id.ai_drvser));
15053 	swab(&hwid[modlen], &hwid[modlen],
15054 	    sizeof (sdinfo->satadrv_id.ai_drvser));
15055 	serlen = sata_check_modser(&hwid[modlen],
15056 	    sizeof (sdinfo->satadrv_id.ai_drvser));
15057 	if (serlen == 0)
15058 		goto err;
15059 	hwid[modlen + serlen] = 0; /* terminate the hwid string */
15060 
15061 	/* initialize/register devid */
15062 	if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL,
15063 	    (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS) {
15064 		rval = ddi_devid_register(dip, devid);
15065 		/*
15066 		 * Free up the allocated devid buffer.
15067 		 * NOTE: This doesn't mean unregistering devid.
15068 		 */
15069 		ddi_devid_free(devid);
15070 	}
15071 
15072 	if (rval != DDI_SUCCESS)
15073 		cmn_err(CE_WARN, "sata: failed to create devid for the disk"
15074 		    " on port %d", sdinfo->satadrv_addr.cport);
15075 err:
15076 	kmem_free(hwid, LEGACY_HWID_LEN);
15077 }
15078 
15079 /*
15080  * valid model/serial string must contain a non-zero non-space characters.
15081  * trim trailing spaces/NULLs.
15082  */
15083 static int
15084 sata_check_modser(char *buf, int buf_len)
15085 {
15086 	boolean_t ret;
15087 	char *s;
15088 	int i;
15089 	int tb;
15090 	char ch;
15091 
15092 	ret = B_FALSE;
15093 	s = buf;
15094 	for (i = 0; i < buf_len; i++) {
15095 		ch = *s++;
15096 		if (ch != ' ' && ch != '\0')
15097 			tb = i + 1;
15098 		if (ch != ' ' && ch != '\0' && ch != '0')
15099 			ret = B_TRUE;
15100 	}
15101 
15102 	if (ret == B_FALSE)
15103 		return (0); /* invalid string */
15104 
15105 	return (tb); /* return length */
15106 }
15107 
15108 /*
15109  * sata_set_drive_features function compares current device features setting
15110  * with the saved device features settings and, if there is a difference,
15111  * it restores device features setting to the previously saved state.
15112  * It also arbitrarily tries to select the highest supported DMA mode.
15113  * Device Identify or Identify Packet Device data has to be current.
15114  * At the moment read ahead and write cache are considered for all devices.
15115  * For atapi devices, Removable Media Status Notification is set in addition
15116  * to common features.
15117  *
15118  * This function cannot be called in the interrupt context (it may sleep).
15119  *
15120  * The input argument sdinfo should point to the drive info structure
15121  * to be updated after features are set. Note, that only
15122  * device (packet) identify data is updated, not the flags indicating the
15123  * supported features.
15124  *
15125  * Returns SATA_SUCCESS if successful or there was nothing to do.
15126  * Device Identify data in the drive info structure pointed to by the sdinfo
15127  * arguments is updated even when no features were set or changed.
15128  *
15129  * Returns SATA_FAILURE if device features could not be set or DMA mode
15130  * for a disk cannot be set and device identify data cannot be fetched.
15131  *
15132  * Returns SATA_RETRY if device features could not be set (other than disk
15133  * DMA mode) but the device identify data was fetched successfully.
15134  *
15135  * Note: This function may fail the port, making it inaccessible.
15136  * In such case the explicit port disconnect/connect or physical device
15137  * detach/attach is required to re-evaluate port state again.
15138  */
15139 
15140 static int
15141 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst,
15142     sata_drive_info_t *sdinfo, int restore)
15143 {
15144 	int rval = SATA_SUCCESS;
15145 	int rval_set;
15146 	sata_drive_info_t new_sdinfo;
15147 	char *finfo = "sata_set_drive_features: cannot";
15148 	char *finfox;
15149 	int cache_op;
15150 
15151 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
15152 	new_sdinfo.satadrv_addr = sdinfo->satadrv_addr;
15153 	new_sdinfo.satadrv_type = sdinfo->satadrv_type;
15154 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
15155 		/*
15156 		 * Cannot get device identification - caller may retry later
15157 		 */
15158 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
15159 		    "%s fetch device identify data\n", finfo);
15160 		return (SATA_FAILURE);
15161 	}
15162 	finfox = (restore != 0) ? " restore device features" :
15163 	    " initialize device features\n";
15164 
15165 	switch (sdinfo->satadrv_type) {
15166 	case SATA_DTYPE_ATADISK:
15167 		/* Arbitrarily set UDMA mode */
15168 		if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
15169 		    SATA_SUCCESS) {
15170 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15171 			    "%s set UDMA mode\n", finfo));
15172 			return (SATA_FAILURE);
15173 		}
15174 		break;
15175 	case SATA_DTYPE_ATAPICD:
15176 	case SATA_DTYPE_ATAPITAPE:
15177 	case SATA_DTYPE_ATAPIDISK:
15178 		/*  Set Removable Media Status Notification, if necessary */
15179 		if (SATA_RM_NOTIFIC_SUPPORTED(new_sdinfo.satadrv_id) &&
15180 		    restore != 0) {
15181 			if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) &&
15182 			    (!SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id)))||
15183 			    ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) &&
15184 			    SATA_RM_NOTIFIC_ENABLED(new_sdinfo.satadrv_id))) {
15185 				/* Current setting does not match saved one */
15186 				if (sata_set_rmsn(sata_hba_inst, sdinfo,
15187 				    sdinfo->satadrv_settings &
15188 				    SATA_DEV_RMSN) != SATA_SUCCESS)
15189 					rval = SATA_FAILURE;
15190 			}
15191 		}
15192 		/*
15193 		 * We have to set Multiword DMA or UDMA, if it is supported, as
15194 		 * we want to use DMA transfer mode whenever possible.
15195 		 * Some devices require explicit setting of the DMA mode.
15196 		 */
15197 		if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) {
15198 			/* Set highest supported DMA mode */
15199 			if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
15200 			    SATA_SUCCESS) {
15201 				SATA_LOG_D((sata_hba_inst, CE_WARN,
15202 				    "%s set UDMA mode\n", finfo));
15203 				rval = SATA_FAILURE;
15204 			}
15205 		}
15206 		break;
15207 	}
15208 
15209 	if (!SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id) &&
15210 	    !SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
15211 		/*
15212 		 * neither READ AHEAD nor WRITE CACHE is supported
15213 		 * - do nothing
15214 		 */
15215 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
15216 		    "settable features not supported\n", NULL);
15217 		goto update_sdinfo;
15218 	}
15219 
15220 	if ((SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id) &&
15221 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) &&
15222 	    (SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id) &&
15223 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
15224 		/*
15225 		 * both READ AHEAD and WRITE CACHE are enabled
15226 		 * - Nothing to do
15227 		 */
15228 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
15229 		    "no device features to set\n", NULL);
15230 		goto update_sdinfo;
15231 	}
15232 
15233 	cache_op = 0;
15234 
15235 	if (SATA_READ_AHEAD_SUPPORTED(new_sdinfo.satadrv_id)) {
15236 		if ((sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
15237 		    !SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
15238 			/* Enable read ahead / read cache */
15239 			cache_op = SATAC_SF_ENABLE_READ_AHEAD;
15240 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
15241 			    "enabling read cache\n", NULL);
15242 		} else if (!(sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) &&
15243 		    SATA_READ_AHEAD_ENABLED(new_sdinfo.satadrv_id)) {
15244 			/* Disable read ahead  / read cache */
15245 			cache_op = SATAC_SF_DISABLE_READ_AHEAD;
15246 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
15247 			    "disabling read cache\n", NULL);
15248 		}
15249 
15250 		if (cache_op != 0) {
15251 			/* Try to set read cache mode */
15252 			rval_set = sata_set_cache_mode(sata_hba_inst,
15253 			    &new_sdinfo, cache_op);
15254 			if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
15255 				rval = rval_set;
15256 		}
15257 	}
15258 
15259 	cache_op = 0;
15260 
15261 	if (SATA_WRITE_CACHE_SUPPORTED(new_sdinfo.satadrv_id)) {
15262 		if ((sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
15263 		    !SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
15264 			/* Enable write cache */
15265 			cache_op = SATAC_SF_ENABLE_WRITE_CACHE;
15266 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
15267 			    "enabling write cache\n", NULL);
15268 		} else if (!(sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) &&
15269 		    SATA_WRITE_CACHE_ENABLED(new_sdinfo.satadrv_id)) {
15270 			/* Disable write cache */
15271 			cache_op = SATAC_SF_DISABLE_WRITE_CACHE;
15272 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
15273 			    "disabling write cache\n", NULL);
15274 		}
15275 
15276 		if (cache_op != 0) {
15277 			/* Try to set write cache mode */
15278 			rval_set = sata_set_cache_mode(sata_hba_inst,
15279 			    &new_sdinfo, cache_op);
15280 			if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
15281 				rval = rval_set;
15282 		}
15283 	}
15284 	if (rval != SATA_SUCCESS)
15285 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15286 		    "%s %s", finfo, finfox));
15287 
15288 update_sdinfo:
15289 	/*
15290 	 * We need to fetch Device Identify data again
15291 	 */
15292 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
15293 		/*
15294 		 * Cannot get device identification - retry later
15295 		 */
15296 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15297 		    "%s re-fetch device identify data\n", finfo));
15298 		rval = SATA_FAILURE;
15299 	}
15300 	/* Copy device sata info. */
15301 	sdinfo->satadrv_id = new_sdinfo.satadrv_id;
15302 
15303 	return (rval);
15304 }
15305 
15306 
15307 /*
15308  *
15309  * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if
15310  * unable to determine.
15311  *
15312  * Cannot be called in an interrupt context.
15313  *
15314  * Called by sata_build_lsense_page_2f()
15315  */
15316 
15317 static int
15318 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst,
15319     sata_drive_info_t *sdinfo)
15320 {
15321 	sata_pkt_t *spkt;
15322 	sata_cmd_t *scmd;
15323 	sata_pkt_txlate_t *spx;
15324 	int rval;
15325 
15326 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
15327 	spx->txlt_sata_hba_inst = sata_hba_inst;
15328 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
15329 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
15330 	if (spkt == NULL) {
15331 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
15332 		return (-1);
15333 	}
15334 	/* address is needed now */
15335 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
15336 
15337 
15338 	/* Fill sata_pkt */
15339 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
15340 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
15341 	/* Synchronous mode, no callback */
15342 	spkt->satapkt_comp = NULL;
15343 	/* Timeout 30s */
15344 	spkt->satapkt_time = sata_default_pkt_time;
15345 
15346 	scmd = &spkt->satapkt_cmd;
15347 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
15348 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
15349 
15350 	/* Set up which registers need to be returned */
15351 	scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE;
15352 	scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE;
15353 
15354 	/* Build SMART_RETURN_STATUS cmd in the sata_pkt */
15355 	scmd->satacmd_addr_type = 0;		/* N/A */
15356 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
15357 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
15358 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
15359 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
15360 	scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS;
15361 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
15362 	scmd->satacmd_cmd_reg = SATAC_SMART;
15363 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
15364 	    sdinfo->satadrv_addr.cport)));
15365 
15366 
15367 	/* Send pkt to SATA HBA driver */
15368 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
15369 	    SATA_TRAN_ACCEPTED ||
15370 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
15371 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
15372 		    sdinfo->satadrv_addr.cport)));
15373 		/*
15374 		 * Whoops, no SMART RETURN STATUS
15375 		 */
15376 		rval = -1;
15377 	} else {
15378 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
15379 		    sdinfo->satadrv_addr.cport)));
15380 		if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
15381 			rval = -1;
15382 			goto fail;
15383 		}
15384 		if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
15385 			rval = -1;
15386 			goto fail;
15387 		}
15388 		if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) &&
15389 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2))
15390 			rval = 0;
15391 		else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) &&
15392 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4))
15393 			rval = 1;
15394 		else {
15395 			rval = -1;
15396 			goto fail;
15397 		}
15398 	}
15399 fail:
15400 	/* Free allocated resources */
15401 	sata_pkt_free(spx);
15402 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
15403 
15404 	return (rval);
15405 }
15406 
15407 /*
15408  *
15409  * Returns 0 if succeeded, -1 otherwise
15410  *
15411  * Cannot be called in an interrupt context.
15412  *
15413  */
15414 static int
15415 sata_fetch_smart_data(
15416 	sata_hba_inst_t *sata_hba_inst,
15417 	sata_drive_info_t *sdinfo,
15418 	struct smart_data *smart_data)
15419 {
15420 	sata_pkt_t *spkt;
15421 	sata_cmd_t *scmd;
15422 	sata_pkt_txlate_t *spx;
15423 	int rval;
15424 
15425 #if ! defined(lint)
15426 	ASSERT(sizeof (struct smart_data) == 512);
15427 #endif
15428 
15429 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
15430 	spx->txlt_sata_hba_inst = sata_hba_inst;
15431 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
15432 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
15433 	if (spkt == NULL) {
15434 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
15435 		return (-1);
15436 	}
15437 	/* address is needed now */
15438 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
15439 
15440 
15441 	/* Fill sata_pkt */
15442 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
15443 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
15444 	/* Synchronous mode, no callback */
15445 	spkt->satapkt_comp = NULL;
15446 	/* Timeout 30s */
15447 	spkt->satapkt_time = sata_default_pkt_time;
15448 
15449 	scmd = &spkt->satapkt_cmd;
15450 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
15451 
15452 	/*
15453 	 * Allocate buffer for SMART data
15454 	 */
15455 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
15456 	    sizeof (struct smart_data));
15457 	if (scmd->satacmd_bp == NULL) {
15458 		sata_pkt_free(spx);
15459 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
15460 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15461 		    "sata_fetch_smart_data: "
15462 		    "cannot allocate buffer"));
15463 		return (-1);
15464 	}
15465 
15466 
15467 	/* Build SMART_READ_DATA cmd in the sata_pkt */
15468 	scmd->satacmd_addr_type = 0;		/* N/A */
15469 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
15470 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
15471 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
15472 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
15473 	scmd->satacmd_features_reg = SATA_SMART_READ_DATA;
15474 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
15475 	scmd->satacmd_cmd_reg = SATAC_SMART;
15476 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
15477 	    sdinfo->satadrv_addr.cport)));
15478 
15479 	/* Send pkt to SATA HBA driver */
15480 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
15481 	    SATA_TRAN_ACCEPTED ||
15482 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
15483 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
15484 		    sdinfo->satadrv_addr.cport)));
15485 		/*
15486 		 * Whoops, no SMART DATA available
15487 		 */
15488 		rval = -1;
15489 		goto fail;
15490 	} else {
15491 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
15492 		    sdinfo->satadrv_addr.cport)));
15493 		if (spx->txlt_buf_dma_handle != NULL) {
15494 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
15495 			    DDI_DMA_SYNC_FORKERNEL);
15496 			ASSERT(rval == DDI_SUCCESS);
15497 		}
15498 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data,
15499 		    sizeof (struct smart_data));
15500 	}
15501 
15502 fail:
15503 	/* Free allocated resources */
15504 	sata_free_local_buffer(spx);
15505 	sata_pkt_free(spx);
15506 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
15507 
15508 	return (rval);
15509 }
15510 
15511 /*
15512  * Used by LOG SENSE page 0x10
15513  * Reads (in synchronous mode) the self test log data using Read Log Ext cmd.
15514  * Note: cannot be called in the interrupt context.
15515  *
15516  * return 0 for success, -1 otherwise
15517  *
15518  */
15519 static int
15520 sata_ext_smart_selftest_read_log(
15521 	sata_hba_inst_t *sata_hba_inst,
15522 	sata_drive_info_t *sdinfo,
15523 	struct smart_ext_selftest_log *ext_selftest_log,
15524 	uint16_t block_num)
15525 {
15526 	sata_pkt_txlate_t *spx;
15527 	sata_pkt_t *spkt;
15528 	sata_cmd_t *scmd;
15529 	int rval;
15530 
15531 #if ! defined(lint)
15532 	ASSERT(sizeof (struct smart_ext_selftest_log) == 512);
15533 #endif
15534 
15535 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
15536 	spx->txlt_sata_hba_inst = sata_hba_inst;
15537 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
15538 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
15539 	if (spkt == NULL) {
15540 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
15541 		return (-1);
15542 	}
15543 	/* address is needed now */
15544 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
15545 
15546 
15547 	/* Fill sata_pkt */
15548 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
15549 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
15550 	/* Synchronous mode, no callback */
15551 	spkt->satapkt_comp = NULL;
15552 	/* Timeout 30s */
15553 	spkt->satapkt_time = sata_default_pkt_time;
15554 
15555 	scmd = &spkt->satapkt_cmd;
15556 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
15557 
15558 	/*
15559 	 * Allocate buffer for SMART extended self-test log
15560 	 */
15561 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
15562 	    sizeof (struct smart_ext_selftest_log));
15563 	if (scmd->satacmd_bp == NULL) {
15564 		sata_pkt_free(spx);
15565 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
15566 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15567 		    "sata_ext_smart_selftest_log: "
15568 		    "cannot allocate buffer"));
15569 		return (-1);
15570 	}
15571 
15572 	/* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */
15573 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
15574 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of selftest log */
15575 	scmd->satacmd_sec_count_msb = 0;	/* One sector of selftest log */
15576 	scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE;
15577 	scmd->satacmd_lba_low_msb = 0;
15578 	scmd->satacmd_lba_mid_lsb = block_num & 0xff;
15579 	scmd->satacmd_lba_mid_msb = block_num >> 8;
15580 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
15581 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
15582 
15583 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
15584 	    sdinfo->satadrv_addr.cport)));
15585 
15586 	/* Send pkt to SATA HBA driver */
15587 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
15588 	    SATA_TRAN_ACCEPTED ||
15589 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
15590 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
15591 		    sdinfo->satadrv_addr.cport)));
15592 
15593 		/*
15594 		 * Whoops, no SMART selftest log info available
15595 		 */
15596 		rval = -1;
15597 		goto fail;
15598 	} else {
15599 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
15600 		    sdinfo->satadrv_addr.cport)));
15601 
15602 		if (spx->txlt_buf_dma_handle != NULL) {
15603 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
15604 			    DDI_DMA_SYNC_FORKERNEL);
15605 			ASSERT(rval == DDI_SUCCESS);
15606 		}
15607 		bcopy(scmd->satacmd_bp->b_un.b_addr,
15608 		    (uint8_t *)ext_selftest_log,
15609 		    sizeof (struct smart_ext_selftest_log));
15610 		rval = 0;
15611 	}
15612 
15613 fail:
15614 	/* Free allocated resources */
15615 	sata_free_local_buffer(spx);
15616 	sata_pkt_free(spx);
15617 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
15618 
15619 	return (rval);
15620 }
15621 
15622 /*
15623  * Returns 0 for success, -1 otherwise
15624  *
15625  * SMART self-test log data is returned in buffer pointed to by selftest_log
15626  */
15627 static int
15628 sata_smart_selftest_log(
15629 	sata_hba_inst_t *sata_hba_inst,
15630 	sata_drive_info_t *sdinfo,
15631 	struct smart_selftest_log *selftest_log)
15632 {
15633 	sata_pkt_t *spkt;
15634 	sata_cmd_t *scmd;
15635 	sata_pkt_txlate_t *spx;
15636 	int rval;
15637 
15638 #if ! defined(lint)
15639 	ASSERT(sizeof (struct smart_selftest_log) == 512);
15640 #endif
15641 
15642 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
15643 	spx->txlt_sata_hba_inst = sata_hba_inst;
15644 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
15645 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
15646 	if (spkt == NULL) {
15647 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
15648 		return (-1);
15649 	}
15650 	/* address is needed now */
15651 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
15652 
15653 
15654 	/* Fill sata_pkt */
15655 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
15656 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
15657 	/* Synchronous mode, no callback */
15658 	spkt->satapkt_comp = NULL;
15659 	/* Timeout 30s */
15660 	spkt->satapkt_time = sata_default_pkt_time;
15661 
15662 	scmd = &spkt->satapkt_cmd;
15663 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
15664 
15665 	/*
15666 	 * Allocate buffer for SMART SELFTEST LOG
15667 	 */
15668 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
15669 	    sizeof (struct smart_selftest_log));
15670 	if (scmd->satacmd_bp == NULL) {
15671 		sata_pkt_free(spx);
15672 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
15673 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15674 		    "sata_smart_selftest_log: "
15675 		    "cannot allocate buffer"));
15676 		return (-1);
15677 	}
15678 
15679 	/* Build SMART_READ_LOG cmd in the sata_pkt */
15680 	scmd->satacmd_addr_type = 0;		/* N/A */
15681 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of SMART log */
15682 	scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE;
15683 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
15684 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
15685 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
15686 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
15687 	scmd->satacmd_cmd_reg = SATAC_SMART;
15688 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
15689 	    sdinfo->satadrv_addr.cport)));
15690 
15691 	/* Send pkt to SATA HBA driver */
15692 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
15693 	    SATA_TRAN_ACCEPTED ||
15694 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
15695 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
15696 		    sdinfo->satadrv_addr.cport)));
15697 		/*
15698 		 * Whoops, no SMART DATA available
15699 		 */
15700 		rval = -1;
15701 		goto fail;
15702 	} else {
15703 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
15704 		    sdinfo->satadrv_addr.cport)));
15705 		if (spx->txlt_buf_dma_handle != NULL) {
15706 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
15707 			    DDI_DMA_SYNC_FORKERNEL);
15708 			ASSERT(rval == DDI_SUCCESS);
15709 		}
15710 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log,
15711 		    sizeof (struct smart_selftest_log));
15712 		rval = 0;
15713 	}
15714 
15715 fail:
15716 	/* Free allocated resources */
15717 	sata_free_local_buffer(spx);
15718 	sata_pkt_free(spx);
15719 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
15720 
15721 	return (rval);
15722 }
15723 
15724 
15725 /*
15726  * Returns 0 for success, -1 otherwise
15727  *
15728  * SMART READ LOG data is returned in buffer pointed to by smart_log
15729  */
15730 static int
15731 sata_smart_read_log(
15732 	sata_hba_inst_t *sata_hba_inst,
15733 	sata_drive_info_t *sdinfo,
15734 	uint8_t *smart_log,		/* where the data should be returned */
15735 	uint8_t which_log,		/* which log should be returned */
15736 	uint8_t log_size)		/* # of 512 bytes in log */
15737 {
15738 	sata_pkt_t *spkt;
15739 	sata_cmd_t *scmd;
15740 	sata_pkt_txlate_t *spx;
15741 	int rval;
15742 
15743 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
15744 	spx->txlt_sata_hba_inst = sata_hba_inst;
15745 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
15746 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
15747 	if (spkt == NULL) {
15748 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
15749 		return (-1);
15750 	}
15751 	/* address is needed now */
15752 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
15753 
15754 
15755 	/* Fill sata_pkt */
15756 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
15757 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
15758 	/* Synchronous mode, no callback */
15759 	spkt->satapkt_comp = NULL;
15760 	/* Timeout 30s */
15761 	spkt->satapkt_time = sata_default_pkt_time;
15762 
15763 	scmd = &spkt->satapkt_cmd;
15764 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
15765 
15766 	/*
15767 	 * Allocate buffer for SMART READ LOG
15768 	 */
15769 	scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512);
15770 	if (scmd->satacmd_bp == NULL) {
15771 		sata_pkt_free(spx);
15772 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
15773 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15774 		    "sata_smart_read_log: " "cannot allocate buffer"));
15775 		return (-1);
15776 	}
15777 
15778 	/* Build SMART_READ_LOG cmd in the sata_pkt */
15779 	scmd->satacmd_addr_type = 0;		/* N/A */
15780 	scmd->satacmd_sec_count_lsb = log_size;	/* what the caller asked for */
15781 	scmd->satacmd_lba_low_lsb = which_log;	/* which log page */
15782 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
15783 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
15784 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
15785 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
15786 	scmd->satacmd_cmd_reg = SATAC_SMART;
15787 
15788 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
15789 	    sdinfo->satadrv_addr.cport)));
15790 
15791 	/* Send pkt to SATA HBA driver */
15792 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
15793 	    SATA_TRAN_ACCEPTED ||
15794 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
15795 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
15796 		    sdinfo->satadrv_addr.cport)));
15797 
15798 		/*
15799 		 * Whoops, no SMART DATA available
15800 		 */
15801 		rval = -1;
15802 		goto fail;
15803 	} else {
15804 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
15805 		    sdinfo->satadrv_addr.cport)));
15806 
15807 		if (spx->txlt_buf_dma_handle != NULL) {
15808 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
15809 			    DDI_DMA_SYNC_FORKERNEL);
15810 			ASSERT(rval == DDI_SUCCESS);
15811 		}
15812 		bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512);
15813 		rval = 0;
15814 	}
15815 
15816 fail:
15817 	/* Free allocated resources */
15818 	sata_free_local_buffer(spx);
15819 	sata_pkt_free(spx);
15820 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
15821 
15822 	return (rval);
15823 }
15824 
15825 /*
15826  * Used by LOG SENSE page 0x10
15827  *
15828  * return 0 for success, -1 otherwise
15829  *
15830  */
15831 static int
15832 sata_read_log_ext_directory(
15833 	sata_hba_inst_t *sata_hba_inst,
15834 	sata_drive_info_t *sdinfo,
15835 	struct read_log_ext_directory *logdir)
15836 {
15837 	sata_pkt_txlate_t *spx;
15838 	sata_pkt_t *spkt;
15839 	sata_cmd_t *scmd;
15840 	int rval;
15841 
15842 #if ! defined(lint)
15843 	ASSERT(sizeof (struct read_log_ext_directory) == 512);
15844 #endif
15845 
15846 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
15847 	spx->txlt_sata_hba_inst = sata_hba_inst;
15848 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
15849 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
15850 	if (spkt == NULL) {
15851 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
15852 		return (-1);
15853 	}
15854 
15855 	/* Fill sata_pkt */
15856 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
15857 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
15858 	/* Synchronous mode, no callback */
15859 	spkt->satapkt_comp = NULL;
15860 	/* Timeout 30s */
15861 	spkt->satapkt_time = sata_default_pkt_time;
15862 
15863 	scmd = &spkt->satapkt_cmd;
15864 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
15865 
15866 	/*
15867 	 * Allocate buffer for SMART READ LOG EXTENDED command
15868 	 */
15869 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
15870 	    sizeof (struct read_log_ext_directory));
15871 	if (scmd->satacmd_bp == NULL) {
15872 		sata_pkt_free(spx);
15873 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
15874 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15875 		    "sata_read_log_ext_directory: "
15876 		    "cannot allocate buffer"));
15877 		return (-1);
15878 	}
15879 
15880 	/* Build READ LOG EXT w/ log directory cmd in the  sata_pkt */
15881 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
15882 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of directory */
15883 	scmd->satacmd_sec_count_msb = 0;	/* One sector of directory */
15884 	scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY;
15885 	scmd->satacmd_lba_low_msb = 0;
15886 	scmd->satacmd_lba_mid_lsb = 0;
15887 	scmd->satacmd_lba_mid_msb = 0;
15888 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
15889 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
15890 
15891 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
15892 	    sdinfo->satadrv_addr.cport)));
15893 
15894 	/* Send pkt to SATA HBA driver */
15895 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
15896 	    SATA_TRAN_ACCEPTED ||
15897 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
15898 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
15899 		    sdinfo->satadrv_addr.cport)));
15900 		/*
15901 		 * Whoops, no SMART selftest log info available
15902 		 */
15903 		rval = -1;
15904 		goto fail;
15905 	} else {
15906 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
15907 		    sdinfo->satadrv_addr.cport)));
15908 		if (spx->txlt_buf_dma_handle != NULL) {
15909 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
15910 			    DDI_DMA_SYNC_FORKERNEL);
15911 			ASSERT(rval == DDI_SUCCESS);
15912 		}
15913 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir,
15914 		    sizeof (struct read_log_ext_directory));
15915 		rval = 0;
15916 	}
15917 
15918 fail:
15919 	/* Free allocated resources */
15920 	sata_free_local_buffer(spx);
15921 	sata_pkt_free(spx);
15922 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
15923 
15924 	return (rval);
15925 }
15926 
15927 /*
15928  * Set up error retrieval sata command for NCQ command error data
15929  * recovery.
15930  *
15931  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
15932  * returns SATA_FAILURE otherwise.
15933  */
15934 static int
15935 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
15936 {
15937 #ifndef __lock_lint
15938 	_NOTE(ARGUNUSED(sdinfo))
15939 #endif
15940 
15941 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
15942 	sata_cmd_t *scmd;
15943 	struct buf *bp;
15944 
15945 	/* Operation modes are up to the caller */
15946 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
15947 
15948 	/* Synchronous mode, no callback - may be changed by the caller */
15949 	spkt->satapkt_comp = NULL;
15950 	spkt->satapkt_time = sata_default_pkt_time;
15951 
15952 	scmd = &spkt->satapkt_cmd;
15953 	bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t));
15954 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
15955 
15956 	/*
15957 	 * Allocate dma_able buffer error data.
15958 	 * Buffer allocation will take care of buffer alignment and other DMA
15959 	 * attributes.
15960 	 */
15961 	bp = sata_alloc_local_buffer(spx,
15962 	    sizeof (struct sata_ncq_error_recovery_page));
15963 	if (bp == NULL)
15964 		return (SATA_FAILURE);
15965 
15966 	bp_mapin(bp); /* make data buffer accessible */
15967 	scmd->satacmd_bp = bp;
15968 
15969 	/*
15970 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
15971 	 * before accessing it. Handle is in usual place in translate struct.
15972 	 */
15973 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
15974 
15975 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
15976 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
15977 
15978 	return (SATA_SUCCESS);
15979 }
15980 
15981 /*
15982  * sata_xlate_errors() is used to translate (S)ATA error
15983  * information to SCSI information returned in the SCSI
15984  * packet.
15985  */
15986 static void
15987 sata_xlate_errors(sata_pkt_txlate_t *spx)
15988 {
15989 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
15990 	struct scsi_extended_sense *sense;
15991 
15992 	scsipkt->pkt_reason = CMD_INCOMPLETE;
15993 	*scsipkt->pkt_scbp = STATUS_CHECK;
15994 	sense = sata_arq_sense(spx);
15995 
15996 	switch (spx->txlt_sata_pkt->satapkt_reason) {
15997 	case SATA_PKT_PORT_ERROR:
15998 		/*
15999 		 * We have no device data. Assume no data transfered.
16000 		 */
16001 		sense->es_key = KEY_HARDWARE_ERROR;
16002 		break;
16003 
16004 	case SATA_PKT_DEV_ERROR:
16005 		if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
16006 		    SATA_STATUS_ERR) {
16007 			/*
16008 			 * determine dev error reason from error
16009 			 * reg content
16010 			 */
16011 			sata_decode_device_error(spx, sense);
16012 			break;
16013 		}
16014 		/* No extended sense key - no info available */
16015 		break;
16016 
16017 	case SATA_PKT_TIMEOUT:
16018 		scsipkt->pkt_reason = CMD_TIMEOUT;
16019 		scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET;
16020 		/* No extended sense key */
16021 		break;
16022 
16023 	case SATA_PKT_ABORTED:
16024 		scsipkt->pkt_reason = CMD_ABORTED;
16025 		scsipkt->pkt_statistics |= STAT_ABORTED;
16026 		/* No extended sense key */
16027 		break;
16028 
16029 	case SATA_PKT_RESET:
16030 		/*
16031 		 * pkt aborted either by an explicit reset request from
16032 		 * a host, or due to error recovery
16033 		 */
16034 		scsipkt->pkt_reason = CMD_RESET;
16035 		scsipkt->pkt_statistics |= STAT_DEV_RESET;
16036 		break;
16037 
16038 	default:
16039 		scsipkt->pkt_reason = CMD_TRAN_ERR;
16040 		break;
16041 	}
16042 }
16043 
16044 
16045 
16046 
16047 /*
16048  * Log sata message
16049  * dev pathname msg line preceeds the logged message.
16050  */
16051 
16052 static	void
16053 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...)
16054 {
16055 	char pathname[128];
16056 	dev_info_t *dip = NULL;
16057 	va_list ap;
16058 
16059 	mutex_enter(&sata_log_mutex);
16060 
16061 	va_start(ap, fmt);
16062 	(void) vsprintf(sata_log_buf, fmt, ap);
16063 	va_end(ap);
16064 
16065 	if (sata_hba_inst != NULL) {
16066 		dip = SATA_DIP(sata_hba_inst);
16067 		(void) ddi_pathname(dip, pathname);
16068 	} else {
16069 		pathname[0] = 0;
16070 	}
16071 	if (level == CE_CONT) {
16072 		if (sata_debug_flags == 0)
16073 			cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf);
16074 		else
16075 			cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf);
16076 	} else {
16077 		if (level != CE_NOTE) {
16078 			cmn_err(level, "%s:\n %s", pathname, sata_log_buf);
16079 		} else if (sata_msg) {
16080 			cmn_err(level, "%s:\n %s", pathname,
16081 			    sata_log_buf);
16082 		}
16083 	}
16084 
16085 	/* sata trace debug */
16086 	sata_trace_debug(dip, sata_log_buf);
16087 
16088 	mutex_exit(&sata_log_mutex);
16089 }
16090 
16091 
16092 /* ******** Asynchronous HBA events handling & hotplugging support ******** */
16093 
16094 /*
16095  * Start or terminate the thread, depending on flag arg and current state
16096  */
16097 static void
16098 sata_event_thread_control(int startstop)
16099 {
16100 	static 	int sata_event_thread_terminating = 0;
16101 	static 	int sata_event_thread_starting = 0;
16102 	int i;
16103 
16104 	mutex_enter(&sata_event_mutex);
16105 
16106 	if (startstop == 0 && (sata_event_thread_starting == 1 ||
16107 	    sata_event_thread_terminating == 1)) {
16108 		mutex_exit(&sata_event_mutex);
16109 		return;
16110 	}
16111 	if (startstop == 1 && sata_event_thread_starting == 1) {
16112 		mutex_exit(&sata_event_mutex);
16113 		return;
16114 	}
16115 	if (startstop == 1 && sata_event_thread_terminating == 1) {
16116 		sata_event_thread_starting = 1;
16117 		/* wait til terminate operation completes */
16118 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
16119 		while (sata_event_thread_terminating == 1) {
16120 			if (i-- <= 0) {
16121 				sata_event_thread_starting = 0;
16122 				mutex_exit(&sata_event_mutex);
16123 #ifdef SATA_DEBUG
16124 				cmn_err(CE_WARN, "sata_event_thread_control: "
16125 				    "timeout waiting for thread to terminate");
16126 #endif
16127 				return;
16128 			}
16129 			mutex_exit(&sata_event_mutex);
16130 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
16131 			mutex_enter(&sata_event_mutex);
16132 		}
16133 	}
16134 	if (startstop == 1) {
16135 		if (sata_event_thread == NULL) {
16136 			sata_event_thread = thread_create(NULL, 0,
16137 			    (void (*)())sata_event_daemon,
16138 			    &sata_hba_list, 0, &p0, TS_RUN, minclsyspri);
16139 		}
16140 		sata_event_thread_starting = 0;
16141 		mutex_exit(&sata_event_mutex);
16142 		return;
16143 	}
16144 
16145 	/*
16146 	 * If we got here, thread may need to be terminated
16147 	 */
16148 	if (sata_event_thread != NULL) {
16149 		int i;
16150 		/* Signal event thread to go away */
16151 		sata_event_thread_terminating = 1;
16152 		sata_event_thread_terminate = 1;
16153 		cv_signal(&sata_event_cv);
16154 		/*
16155 		 * Wait til daemon terminates.
16156 		 */
16157 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
16158 		while (sata_event_thread_terminate == 1) {
16159 			mutex_exit(&sata_event_mutex);
16160 			if (i-- <= 0) {
16161 				/* Daemon did not go away !!! */
16162 #ifdef SATA_DEBUG
16163 				cmn_err(CE_WARN, "sata_event_thread_control: "
16164 				    "cannot terminate event daemon thread");
16165 #endif
16166 				mutex_enter(&sata_event_mutex);
16167 				break;
16168 			}
16169 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
16170 			mutex_enter(&sata_event_mutex);
16171 		}
16172 		sata_event_thread_terminating = 0;
16173 	}
16174 	ASSERT(sata_event_thread_terminating == 0);
16175 	ASSERT(sata_event_thread_starting == 0);
16176 	mutex_exit(&sata_event_mutex);
16177 }
16178 
16179 
16180 /*
16181  * SATA HBA event notification function.
16182  * Events reported by SATA HBA drivers per HBA instance relate to a change in
16183  * a port and/or device state or a controller itself.
16184  * Events for different addresses/addr types cannot be combined.
16185  * A warning message is generated for each event type.
16186  * Events are not processed by this function, so only the
16187  * event flag(s)is set for an affected entity and the event thread is
16188  * waken up. Event daemon thread processes all events.
16189  *
16190  * NOTE: Since more than one event may be reported at the same time, one
16191  * cannot determine a sequence of events when opposite event are reported, eg.
16192  * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing
16193  * is taking precedence over reported events, i.e. may cause ignoring some
16194  * events.
16195  */
16196 #define	SATA_EVENT_MAX_MSG_LENGTH	79
16197 
16198 void
16199 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event)
16200 {
16201 	sata_hba_inst_t *sata_hba_inst = NULL;
16202 	sata_address_t *saddr;
16203 	sata_pmult_info_t *pmultinfo;
16204 	sata_drive_info_t *sdinfo;
16205 	sata_port_stats_t *pstats;
16206 	sata_cport_info_t *cportinfo;
16207 	sata_pmport_info_t *pmportinfo;
16208 	int cport, pmport;
16209 	char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1];
16210 	char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1];
16211 	char *lcp;
16212 	static char *err_msg_evnt_1 =
16213 	    "sata_hba_event_notify: invalid port event 0x%x ";
16214 	static char *err_msg_evnt_2 =
16215 	    "sata_hba_event_notify: invalid device event 0x%x ";
16216 	int linkevent;
16217 
16218 	/*
16219 	 * There is a possibility that an event will be generated on HBA
16220 	 * that has not completed attachment or is detaching. We still want
16221 	 * to process events until HBA is detached.
16222 	 */
16223 	mutex_enter(&sata_mutex);
16224 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
16225 	    sata_hba_inst = sata_hba_inst->satahba_next) {
16226 		if (SATA_DIP(sata_hba_inst) == dip)
16227 			if (sata_hba_inst->satahba_attached == 1)
16228 				break;
16229 	}
16230 	mutex_exit(&sata_mutex);
16231 	if (sata_hba_inst == NULL)
16232 		/* HBA not attached */
16233 		return;
16234 
16235 	ASSERT(sata_device != NULL);
16236 
16237 	/*
16238 	 * Validate address before - do not proceed with invalid address.
16239 	 */
16240 	saddr = &sata_device->satadev_addr;
16241 	if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst))
16242 		return;
16243 
16244 	cport = saddr->cport;
16245 	pmport = saddr->pmport;
16246 
16247 	buf1[0] = buf2[0] = '\0';
16248 
16249 	/*
16250 	 * If event relates to port or device, check port state.
16251 	 * Port has to be initialized, or we cannot accept an event.
16252 	 */
16253 	if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT |
16254 	    SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT | SATA_ADDR_PMULT)) != 0) {
16255 		mutex_enter(&sata_hba_inst->satahba_mutex);
16256 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
16257 		mutex_exit(&sata_hba_inst->satahba_mutex);
16258 		if (cportinfo == NULL || cportinfo->cport_state == 0)
16259 			return;
16260 	}
16261 
16262 	if ((saddr->qual & (SATA_ADDR_PMULT | SATA_ADDR_PMPORT |
16263 	    SATA_ADDR_DPMPORT)) != 0) {
16264 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
16265 			SATA_LOG_D((sata_hba_inst, CE_WARN,
16266 			    "sata_hba_event_notify: Non-pmult device (0x%x)"
16267 			    "is attached to port %d, ignore pmult/pmport "
16268 			    "event 0x%x", cportinfo->cport_dev_type,
16269 			    cport, event));
16270 			return;
16271 		}
16272 
16273 		mutex_enter(&cportinfo->cport_mutex);
16274 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
16275 		mutex_exit(&cportinfo->cport_mutex);
16276 
16277 		/*
16278 		 * The daemon might be processing attachment of port
16279 		 * multiplier, in that case we should ignore events on its
16280 		 * sub-devices.
16281 		 *
16282 		 * NOTE: Only pmult_state is checked in sata_hba_event_notify.
16283 		 * The pmport_state is checked by sata daemon.
16284 		 */
16285 		if (pmultinfo == NULL ||
16286 		    pmultinfo->pmult_state == SATA_STATE_UNKNOWN) {
16287 			SATA_LOG_D((sata_hba_inst, CE_WARN,
16288 			    "sata_hba_event_notify: pmult is not"
16289 			    "available at port %d:%d, ignore event 0x%x",
16290 			    cport, pmport, event));
16291 			return;
16292 		}
16293 	}
16294 
16295 	if ((saddr->qual &
16296 	    (SATA_ADDR_PMPORT | SATA_ADDR_DPMPORT)) != 0) {
16297 
16298 		mutex_enter(&cportinfo->cport_mutex);
16299 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport)) {
16300 			SATA_LOG_D((sata_hba_inst, CE_WARN,
16301 			    "sata_hba_event_notify: invalid/"
16302 			    "un-implemented port %d:%d (%d ports), "
16303 			    "ignore event 0x%x", cport, pmport,
16304 			    SATA_NUM_PMPORTS(sata_hba_inst, cport), event));
16305 			mutex_exit(&cportinfo->cport_mutex);
16306 			return;
16307 		}
16308 		mutex_exit(&cportinfo->cport_mutex);
16309 
16310 		mutex_enter(&sata_hba_inst->satahba_mutex);
16311 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
16312 		    cport, pmport);
16313 		mutex_exit(&sata_hba_inst->satahba_mutex);
16314 
16315 		/* pmport is implemented/valid? */
16316 		if (pmportinfo == NULL) {
16317 			SATA_LOG_D((sata_hba_inst, CE_WARN,
16318 			    "sata_hba_event_notify: invalid/"
16319 			    "un-implemented port %d:%d, ignore "
16320 			    "event 0x%x", cport, pmport, event));
16321 			return;
16322 		}
16323 	}
16324 
16325 	/*
16326 	 * Events refer to devices, ports and controllers - each has
16327 	 * unique address. Events for different addresses cannot be combined.
16328 	 */
16329 	if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) {
16330 
16331 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
16332 
16333 		/* qualify this event(s) */
16334 		if ((event & SATA_EVNT_PORT_EVENTS) == 0) {
16335 			/* Invalid event for the device port */
16336 			(void) sprintf(buf2, err_msg_evnt_1,
16337 			    event & SATA_EVNT_PORT_EVENTS);
16338 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
16339 			goto event_info;
16340 		}
16341 		if (saddr->qual == SATA_ADDR_CPORT) {
16342 			/* Controller's device port event */
16343 
16344 			(SATA_CPORT_INFO(sata_hba_inst, cport))->
16345 			    cport_event_flags |=
16346 			    event & SATA_EVNT_PORT_EVENTS;
16347 			pstats =
16348 			    &(SATA_CPORT_INFO(sata_hba_inst, cport))->
16349 			    cport_stats;
16350 		} else {
16351 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
16352 			mutex_enter(&pmportinfo->pmport_mutex);
16353 			/* Port multiplier's device port event */
16354 			(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
16355 			    pmport_event_flags |=
16356 			    event & SATA_EVNT_PORT_EVENTS;
16357 			pstats =
16358 			    &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
16359 			    pmport_stats;
16360 			mutex_exit(&pmportinfo->pmport_mutex);
16361 			mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
16362 		}
16363 
16364 		/*
16365 		 * Add to statistics and log the message. We have to do it
16366 		 * here rather than in the event daemon, because there may be
16367 		 * multiple events occuring before they are processed.
16368 		 */
16369 		linkevent = event &
16370 		    (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED);
16371 		if (linkevent) {
16372 			if (linkevent == (SATA_EVNT_LINK_LOST |
16373 			    SATA_EVNT_LINK_ESTABLISHED)) {
16374 				/* This is likely event combination */
16375 				(void) strlcat(buf1, "link lost/established, ",
16376 				    SATA_EVENT_MAX_MSG_LENGTH);
16377 
16378 				if (pstats->link_lost < 0xffffffffffffffffULL)
16379 					pstats->link_lost++;
16380 				if (pstats->link_established <
16381 				    0xffffffffffffffffULL)
16382 					pstats->link_established++;
16383 				linkevent = 0;
16384 			} else if (linkevent & SATA_EVNT_LINK_LOST) {
16385 				(void) strlcat(buf1, "link lost, ",
16386 				    SATA_EVENT_MAX_MSG_LENGTH);
16387 
16388 				if (pstats->link_lost < 0xffffffffffffffffULL)
16389 					pstats->link_lost++;
16390 			} else {
16391 				(void) strlcat(buf1, "link established, ",
16392 				    SATA_EVENT_MAX_MSG_LENGTH);
16393 				if (pstats->link_established <
16394 				    0xffffffffffffffffULL)
16395 					pstats->link_established++;
16396 			}
16397 		}
16398 		if (event & SATA_EVNT_DEVICE_ATTACHED) {
16399 			(void) strlcat(buf1, "device attached, ",
16400 			    SATA_EVENT_MAX_MSG_LENGTH);
16401 			if (pstats->device_attached < 0xffffffffffffffffULL)
16402 				pstats->device_attached++;
16403 		}
16404 		if (event & SATA_EVNT_DEVICE_DETACHED) {
16405 			(void) strlcat(buf1, "device detached, ",
16406 			    SATA_EVENT_MAX_MSG_LENGTH);
16407 			if (pstats->device_detached < 0xffffffffffffffffULL)
16408 				pstats->device_detached++;
16409 		}
16410 		if (event & SATA_EVNT_PWR_LEVEL_CHANGED) {
16411 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
16412 			    "port %d power level changed", cport);
16413 			if (pstats->port_pwr_changed < 0xffffffffffffffffULL)
16414 				pstats->port_pwr_changed++;
16415 		}
16416 
16417 		if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) {
16418 			/* There should be no other events for this address */
16419 			(void) sprintf(buf2, err_msg_evnt_1,
16420 			    event & ~SATA_EVNT_PORT_EVENTS);
16421 		}
16422 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
16423 
16424 	} else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) {
16425 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
16426 
16427 		/* qualify this event */
16428 		if ((event & SATA_EVNT_DEVICE_RESET) == 0) {
16429 			/* Invalid event for a device */
16430 			(void) sprintf(buf2, err_msg_evnt_2,
16431 			    event & SATA_EVNT_DEVICE_RESET);
16432 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
16433 			goto event_info;
16434 		}
16435 		/* drive event */
16436 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
16437 		if (sdinfo != NULL) {
16438 			if (event & SATA_EVNT_DEVICE_RESET) {
16439 				(void) strlcat(buf1, "device reset, ",
16440 				    SATA_EVENT_MAX_MSG_LENGTH);
16441 				if (sdinfo->satadrv_stats.drive_reset <
16442 				    0xffffffffffffffffULL)
16443 					sdinfo->satadrv_stats.drive_reset++;
16444 				sdinfo->satadrv_event_flags |=
16445 				    SATA_EVNT_DEVICE_RESET;
16446 			}
16447 		}
16448 		if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) {
16449 			/* Invalid event for a device */
16450 			(void) sprintf(buf2, err_msg_evnt_2,
16451 			    event & ~SATA_EVNT_DRIVE_EVENTS);
16452 		}
16453 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
16454 	} else if (saddr->qual == SATA_ADDR_PMULT) {
16455 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
16456 
16457 		/* qualify this event */
16458 		if ((event & (SATA_EVNT_DEVICE_RESET |
16459 		    SATA_EVNT_PMULT_LINK_CHANGED)) == 0) {
16460 			/* Invalid event for a port multiplier */
16461 			(void) sprintf(buf2, err_msg_evnt_2,
16462 			    event & SATA_EVNT_DEVICE_RESET);
16463 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
16464 			goto event_info;
16465 		}
16466 
16467 		pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
16468 
16469 		if (event & SATA_EVNT_DEVICE_RESET) {
16470 
16471 			SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
16472 			    "[Reset] port-mult on cport %d", cport);
16473 			pmultinfo->pmult_event_flags |=
16474 			    SATA_EVNT_DEVICE_RESET;
16475 			(void) strlcat(buf1, "pmult reset, ",
16476 			    SATA_EVENT_MAX_MSG_LENGTH);
16477 		}
16478 
16479 		if (event & SATA_EVNT_PMULT_LINK_CHANGED) {
16480 
16481 			SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
16482 			    "pmult link changed on cport %d", cport);
16483 			pmultinfo->pmult_event_flags |=
16484 			    SATA_EVNT_PMULT_LINK_CHANGED;
16485 			(void) strlcat(buf1, "pmult link changed, ",
16486 			    SATA_EVENT_MAX_MSG_LENGTH);
16487 		}
16488 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
16489 
16490 	} else {
16491 		if (saddr->qual != SATA_ADDR_NULL) {
16492 			/* Wrong address qualifier */
16493 			SATA_LOG_D((sata_hba_inst, CE_WARN,
16494 			    "sata_hba_event_notify: invalid address 0x%x",
16495 			    *(uint32_t *)saddr));
16496 			return;
16497 		}
16498 		if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 ||
16499 		    (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) {
16500 			/* Invalid event for the controller */
16501 			SATA_LOG_D((sata_hba_inst, CE_WARN,
16502 			    "sata_hba_event_notify: invalid event 0x%x for "
16503 			    "controller",
16504 			    event & SATA_EVNT_CONTROLLER_EVENTS));
16505 			return;
16506 		}
16507 		buf1[0] = '\0';
16508 		/* This may be a frequent and not interesting event */
16509 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
16510 		    "controller power level changed\n", NULL);
16511 
16512 		mutex_enter(&sata_hba_inst->satahba_mutex);
16513 		if (sata_hba_inst->satahba_stats.ctrl_pwr_change <
16514 		    0xffffffffffffffffULL)
16515 			sata_hba_inst->satahba_stats.ctrl_pwr_change++;
16516 
16517 		sata_hba_inst->satahba_event_flags |=
16518 		    SATA_EVNT_PWR_LEVEL_CHANGED;
16519 		mutex_exit(&sata_hba_inst->satahba_mutex);
16520 	}
16521 	/*
16522 	 * If we got here, there is something to do with this HBA
16523 	 * instance.
16524 	 */
16525 	mutex_enter(&sata_hba_inst->satahba_mutex);
16526 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
16527 	mutex_exit(&sata_hba_inst->satahba_mutex);
16528 	mutex_enter(&sata_mutex);
16529 	sata_event_pending |= SATA_EVNT_MAIN;	/* global event indicator */
16530 	mutex_exit(&sata_mutex);
16531 
16532 	/* Tickle event thread */
16533 	mutex_enter(&sata_event_mutex);
16534 	if (sata_event_thread_active == 0)
16535 		cv_signal(&sata_event_cv);
16536 	mutex_exit(&sata_event_mutex);
16537 
16538 event_info:
16539 	if (buf1[0] != '\0') {
16540 		lcp = strrchr(buf1, ',');
16541 		if (lcp != NULL)
16542 			*lcp = '\0';
16543 	}
16544 	if (saddr->qual == SATA_ADDR_CPORT ||
16545 	    saddr->qual == SATA_ADDR_DCPORT) {
16546 		if (buf1[0] != '\0') {
16547 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
16548 			    cport, buf1);
16549 		}
16550 		if (buf2[0] != '\0') {
16551 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
16552 			    cport, buf2);
16553 		}
16554 	} else if (saddr->qual == SATA_ADDR_PMPORT ||
16555 	    saddr->qual == SATA_ADDR_DPMPORT) {
16556 		if (buf1[0] != '\0') {
16557 			sata_log(sata_hba_inst, CE_NOTE,
16558 			    "port %d pmport %d: %s\n", cport, pmport, buf1);
16559 		}
16560 		if (buf2[0] != '\0') {
16561 			sata_log(sata_hba_inst, CE_NOTE,
16562 			    "port %d pmport %d: %s\n", cport, pmport, buf2);
16563 		}
16564 	}
16565 }
16566 
16567 
16568 /*
16569  * Event processing thread.
16570  * Arg is a pointer to the sata_hba_list pointer.
16571  * It is not really needed, because sata_hba_list is global and static
16572  */
16573 static void
16574 sata_event_daemon(void *arg)
16575 {
16576 #ifndef __lock_lint
16577 	_NOTE(ARGUNUSED(arg))
16578 #endif
16579 	sata_hba_inst_t *sata_hba_inst;
16580 	clock_t lbolt;
16581 
16582 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
16583 	    "SATA event daemon started\n", NULL);
16584 loop:
16585 	/*
16586 	 * Process events here. Walk through all registered HBAs
16587 	 */
16588 	mutex_enter(&sata_mutex);
16589 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
16590 	    sata_hba_inst = sata_hba_inst->satahba_next) {
16591 		ASSERT(sata_hba_inst != NULL);
16592 		mutex_enter(&sata_hba_inst->satahba_mutex);
16593 		if (sata_hba_inst->satahba_attached == 0 ||
16594 		    (sata_hba_inst->satahba_event_flags &
16595 		    SATA_EVNT_SKIP) != 0) {
16596 			mutex_exit(&sata_hba_inst->satahba_mutex);
16597 			continue;
16598 		}
16599 		if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) {
16600 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP;
16601 			mutex_exit(&sata_hba_inst->satahba_mutex);
16602 			mutex_exit(&sata_mutex);
16603 			/* Got the controller with pending event */
16604 			sata_process_controller_events(sata_hba_inst);
16605 			/*
16606 			 * Since global mutex was released, there is a
16607 			 * possibility that HBA list has changed, so start
16608 			 * over from the top. Just processed controller
16609 			 * will be passed-over because of the SKIP flag.
16610 			 */
16611 			goto loop;
16612 		}
16613 		mutex_exit(&sata_hba_inst->satahba_mutex);
16614 	}
16615 	/* Clear SKIP flag in all controllers */
16616 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
16617 	    sata_hba_inst = sata_hba_inst->satahba_next) {
16618 		mutex_enter(&sata_hba_inst->satahba_mutex);
16619 		sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP;
16620 		mutex_exit(&sata_hba_inst->satahba_mutex);
16621 	}
16622 	mutex_exit(&sata_mutex);
16623 
16624 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
16625 	    "SATA EVENT DAEMON suspending itself", NULL);
16626 
16627 #ifdef SATA_DEBUG
16628 	if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) {
16629 		sata_log(sata_hba_inst, CE_WARN,
16630 		    "SATA EVENTS PROCESSING DISABLED\n");
16631 		thread_exit(); /* Daemon will not run again */
16632 	}
16633 #endif
16634 	mutex_enter(&sata_event_mutex);
16635 	sata_event_thread_active = 0;
16636 	mutex_exit(&sata_event_mutex);
16637 	/*
16638 	 * Go to sleep/suspend itself and wake up either because new event or
16639 	 * wait timeout. Exit if there is a termination request (driver
16640 	 * unload).
16641 	 */
16642 	do {
16643 		lbolt = ddi_get_lbolt();
16644 		lbolt += drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME);
16645 		mutex_enter(&sata_event_mutex);
16646 		(void) cv_timedwait(&sata_event_cv, &sata_event_mutex, lbolt);
16647 
16648 		if (sata_event_thread_active != 0) {
16649 			mutex_exit(&sata_event_mutex);
16650 			continue;
16651 		}
16652 
16653 		/* Check if it is time to go away */
16654 		if (sata_event_thread_terminate == 1) {
16655 			/*
16656 			 * It is up to the thread setting above flag to make
16657 			 * sure that this thread is not killed prematurely.
16658 			 */
16659 			sata_event_thread_terminate = 0;
16660 			sata_event_thread = NULL;
16661 			mutex_exit(&sata_event_mutex);
16662 			SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
16663 			    "SATA_EVENT_DAEMON_TERMINATING", NULL);
16664 			thread_exit();  { _NOTE(NOT_REACHED) }
16665 		}
16666 		mutex_exit(&sata_event_mutex);
16667 	} while (!(sata_event_pending & SATA_EVNT_MAIN));
16668 
16669 	mutex_enter(&sata_event_mutex);
16670 	sata_event_thread_active = 1;
16671 	mutex_exit(&sata_event_mutex);
16672 
16673 	mutex_enter(&sata_mutex);
16674 	sata_event_pending &= ~SATA_EVNT_MAIN;
16675 	mutex_exit(&sata_mutex);
16676 
16677 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
16678 	    "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL);
16679 
16680 	goto loop;
16681 }
16682 
16683 /*
16684  * Specific HBA instance event processing.
16685  *
16686  * NOTE: At the moment, device event processing is limited to hard disks
16687  * only.
16688  * Port multiplier is supported now.
16689  */
16690 static void
16691 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst)
16692 {
16693 	int ncport;
16694 	uint32_t event_flags;
16695 	sata_address_t *saddr;
16696 	sata_cport_info_t *cportinfo;
16697 	sata_pmult_info_t *pmultinfo;
16698 
16699 	SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst,
16700 	    "Processing controller %d event(s)",
16701 	    ddi_get_instance(SATA_DIP(sata_hba_inst)));
16702 
16703 	mutex_enter(&sata_hba_inst->satahba_mutex);
16704 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN;
16705 	event_flags = sata_hba_inst->satahba_event_flags;
16706 	mutex_exit(&sata_hba_inst->satahba_mutex);
16707 	/*
16708 	 * Process controller power change first
16709 	 * HERE
16710 	 */
16711 	if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED)
16712 		sata_process_cntrl_pwr_level_change(sata_hba_inst);
16713 
16714 	/*
16715 	 * Search through ports/devices to identify affected port/device.
16716 	 * We may have to process events for more than one port/device.
16717 	 */
16718 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
16719 		/*
16720 		 * Not all ports may be processed in attach by the time we
16721 		 * get an event. Check if port info is initialized.
16722 		 */
16723 		mutex_enter(&sata_hba_inst->satahba_mutex);
16724 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
16725 		mutex_exit(&sata_hba_inst->satahba_mutex);
16726 		if (cportinfo == NULL || cportinfo->cport_state == NULL)
16727 			continue;
16728 
16729 		/* We have initialized controller port info */
16730 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
16731 		event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))->
16732 		    cport_event_flags;
16733 		/* Check if port was locked by IOCTL processing */
16734 		if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) {
16735 			/*
16736 			 * We ignore port events because port is busy
16737 			 * with AP control processing. Set again
16738 			 * controller and main event flag, so that
16739 			 * events may be processed by the next daemon
16740 			 * run.
16741 			 */
16742 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
16743 			mutex_enter(&sata_hba_inst->satahba_mutex);
16744 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
16745 			mutex_exit(&sata_hba_inst->satahba_mutex);
16746 			mutex_enter(&sata_mutex);
16747 			sata_event_pending |= SATA_EVNT_MAIN;
16748 			mutex_exit(&sata_mutex);
16749 			SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst,
16750 			    "Event processing postponed until "
16751 			    "AP control processing completes",
16752 			    NULL);
16753 			/* Check other ports */
16754 			continue;
16755 		} else {
16756 			/*
16757 			 * Set BSY flag so that AP control would not
16758 			 * interfere with events processing for
16759 			 * this port.
16760 			 */
16761 			(SATA_CPORT_INFO(sata_hba_inst, ncport))->
16762 			    cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY;
16763 		}
16764 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
16765 
16766 		saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr;
16767 
16768 		if ((event_flags &
16769 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
16770 			/*
16771 			 * Got port event.
16772 			 * We need some hierarchy of event processing as they
16773 			 * are affecting each other:
16774 			 * 1. port failed
16775 			 * 2. device detached/attached
16776 			 * 3. link events - link events may trigger device
16777 			 *    detached or device attached events in some
16778 			 *    circumstances.
16779 			 * 4. port power level changed
16780 			 */
16781 			if (event_flags & SATA_EVNT_PORT_FAILED) {
16782 				sata_process_port_failed_event(sata_hba_inst,
16783 				    saddr);
16784 			}
16785 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
16786 				sata_process_device_detached(sata_hba_inst,
16787 				    saddr);
16788 			}
16789 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
16790 				sata_process_device_attached(sata_hba_inst,
16791 				    saddr);
16792 			}
16793 			if (event_flags &
16794 			    (SATA_EVNT_LINK_ESTABLISHED |
16795 			    SATA_EVNT_LINK_LOST)) {
16796 				sata_process_port_link_events(sata_hba_inst,
16797 				    saddr);
16798 			}
16799 			if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) {
16800 				sata_process_port_pwr_change(sata_hba_inst,
16801 				    saddr);
16802 			}
16803 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
16804 				sata_process_target_node_cleanup(
16805 				    sata_hba_inst, saddr);
16806 			}
16807 			if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) {
16808 				sata_process_device_autoonline(
16809 				    sata_hba_inst, saddr);
16810 			}
16811 		}
16812 
16813 
16814 		/*
16815 		 * Scan port multiplier and all its sub-ports event flags.
16816 		 * The events are marked by
16817 		 * (1) sata_pmult_info.pmult_event_flags
16818 		 * (2) sata_pmport_info.pmport_event_flags
16819 		 */
16820 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
16821 		if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
16822 			/*
16823 			 * There should be another extra check: this
16824 			 * port multiplier still exists?
16825 			 */
16826 			pmultinfo = SATA_PMULT_INFO(sata_hba_inst,
16827 			    ncport);
16828 
16829 			if (pmultinfo != NULL) {
16830 				mutex_exit(&(SATA_CPORT_MUTEX(
16831 				    sata_hba_inst, ncport)));
16832 				sata_process_pmult_events(
16833 				    sata_hba_inst, ncport);
16834 				mutex_enter(&(SATA_CPORT_MUTEX(
16835 				    sata_hba_inst, ncport)));
16836 			} else {
16837 				SATADBG1(SATA_DBG_PMULT, sata_hba_inst,
16838 				    "Port-multiplier is gone. "
16839 				    "Ignore all sub-device events "
16840 				    "at port %d.", ncport);
16841 			}
16842 		}
16843 
16844 		if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) !=
16845 		    SATA_DTYPE_NONE) &&
16846 		    (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) {
16847 			if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)->
16848 			    satadrv_event_flags &
16849 			    (SATA_EVNT_DEVICE_RESET |
16850 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
16851 				/* Have device event */
16852 				sata_process_device_reset(sata_hba_inst,
16853 				    saddr);
16854 			}
16855 		}
16856 		/* Release PORT_BUSY flag */
16857 		(SATA_CPORT_INFO(sata_hba_inst, ncport))->
16858 		    cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
16859 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
16860 
16861 	} /* End of loop through the controller SATA ports */
16862 }
16863 
16864 /*
16865  * Specific port multiplier instance event processing. At the moment, device
16866  * event processing is limited to link/attach event only.
16867  *
16868  * NOTE: power management event is not supported yet.
16869  */
16870 static void
16871 sata_process_pmult_events(sata_hba_inst_t *sata_hba_inst, uint8_t cport)
16872 {
16873 	sata_cport_info_t *cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
16874 	sata_pmult_info_t *pmultinfo;
16875 	sata_pmport_info_t *pmportinfo;
16876 	sata_address_t *saddr;
16877 	sata_device_t sata_device;
16878 	uint32_t event_flags;
16879 	int npmport;
16880 	int rval;
16881 
16882 	SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst,
16883 	    "Processing pmult event(s) on cport %d of controller %d",
16884 	    cport, ddi_get_instance(SATA_DIP(sata_hba_inst)));
16885 
16886 	/* First process events on port multiplier */
16887 	mutex_enter(&cportinfo->cport_mutex);
16888 	pmultinfo = SATA_PMULT_INFO(sata_hba_inst, cport);
16889 	event_flags = pmultinfo->pmult_event_flags;
16890 
16891 	/*
16892 	 * Reset event (of port multiplier) has higher priority because the
16893 	 * port multiplier itself might be failed or removed after reset.
16894 	 */
16895 	if (event_flags & SATA_EVNT_DEVICE_RESET) {
16896 		/*
16897 		 * The status of the sub-links are uncertain,
16898 		 * so mark all sub-ports as RESET
16899 		 */
16900 		for (npmport = 0; npmport < SATA_NUM_PMPORTS(
16901 		    sata_hba_inst, cport); npmport ++) {
16902 			pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
16903 			    cport, npmport);
16904 			if (pmportinfo == NULL) {
16905 				/* That's weird. */
16906 				SATA_LOG_D((sata_hba_inst, CE_WARN,
16907 				    "sata_hba_event_notify: "
16908 				    "invalid/un-implemented "
16909 				    "port %d:%d (%d ports), ",
16910 				    cport, npmport, SATA_NUM_PMPORTS(
16911 				    sata_hba_inst, cport)));
16912 				continue;
16913 			}
16914 
16915 			mutex_enter(&pmportinfo->pmport_mutex);
16916 
16917 			/* Mark all pmport to unknow state. */
16918 			pmportinfo->pmport_state = SATA_STATE_UNKNOWN;
16919 			/* Mark all pmports with link events. */
16920 			pmportinfo->pmport_event_flags =
16921 			    (SATA_EVNT_LINK_ESTABLISHED|SATA_EVNT_LINK_LOST);
16922 			mutex_exit(&pmportinfo->pmport_mutex);
16923 		}
16924 
16925 	} else if (event_flags & SATA_EVNT_PMULT_LINK_CHANGED) {
16926 		/*
16927 		 * We need probe the port multiplier to know what has
16928 		 * happened.
16929 		 */
16930 		bzero(&sata_device, sizeof (sata_device_t));
16931 		sata_device.satadev_rev = SATA_DEVICE_REV;
16932 		sata_device.satadev_addr.cport = cport;
16933 		sata_device.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
16934 		sata_device.satadev_addr.qual = SATA_ADDR_PMULT;
16935 
16936 		mutex_exit(&cportinfo->cport_mutex);
16937 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
16938 		    (SATA_DIP(sata_hba_inst), &sata_device);
16939 		mutex_enter(&cportinfo->cport_mutex);
16940 		if (rval != SATA_SUCCESS) {
16941 			/* Something went wrong? Fail the port */
16942 			cportinfo->cport_state = SATA_PSTATE_FAILED;
16943 			mutex_exit(&cportinfo->cport_mutex);
16944 			SATA_LOG_D((sata_hba_inst, CE_WARN,
16945 			    "SATA port %d probing failed", cport));
16946 
16947 			/* PMult structure must be released.  */
16948 			sata_free_pmult(sata_hba_inst, &sata_device);
16949 			return;
16950 		}
16951 
16952 		sata_update_port_info(sata_hba_inst, &sata_device);
16953 
16954 		/*
16955 		 * Sanity check - Port is active? Is the link active?
16956 		 * The device is still a port multiplier?
16957 		 */
16958 		if ((cportinfo->cport_state &
16959 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
16960 		    ((cportinfo->cport_scr.sstatus &
16961 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) ||
16962 		    (cportinfo->cport_dev_type != SATA_DTYPE_PMULT)) {
16963 			mutex_exit(&cportinfo->cport_mutex);
16964 
16965 			/* PMult structure must be released.  */
16966 			sata_free_pmult(sata_hba_inst, &sata_device);
16967 			return;
16968 		}
16969 
16970 		/* Probed succeed, set port ready. */
16971 		cportinfo->cport_state |=
16972 		    SATA_STATE_PROBED | SATA_STATE_READY;
16973 	}
16974 
16975 	/* Release port multiplier event flags. */
16976 	pmultinfo->pmult_event_flags &=
16977 	    ~(SATA_EVNT_DEVICE_RESET|SATA_EVNT_PMULT_LINK_CHANGED);
16978 	mutex_exit(&cportinfo->cport_mutex);
16979 
16980 	/*
16981 	 * Check all sub-links.
16982 	 */
16983 	for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst, cport);
16984 	    npmport ++) {
16985 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, npmport);
16986 		mutex_enter(&pmportinfo->pmport_mutex);
16987 		event_flags = pmportinfo->pmport_event_flags;
16988 		mutex_exit(&pmportinfo->pmport_mutex);
16989 		saddr = &pmportinfo->pmport_addr;
16990 
16991 		if ((event_flags &
16992 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
16993 			/*
16994 			 * Got port multiplier port event.
16995 			 * We need some hierarchy of event processing as they
16996 			 * are affecting each other:
16997 			 * 1. device detached/attached
16998 			 * 2. link events - link events may trigger device
16999 			 *    detached or device attached events in some
17000 			 *    circumstances.
17001 			 */
17002 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
17003 				sata_process_pmdevice_detached(sata_hba_inst,
17004 				    saddr);
17005 			}
17006 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
17007 				sata_process_pmdevice_attached(sata_hba_inst,
17008 				    saddr);
17009 			}
17010 			if (event_flags & SATA_EVNT_LINK_ESTABLISHED ||
17011 			    event_flags & SATA_EVNT_LINK_LOST) {
17012 				sata_process_pmport_link_events(sata_hba_inst,
17013 				    saddr);
17014 			}
17015 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
17016 				sata_process_target_node_cleanup(
17017 				    sata_hba_inst, saddr);
17018 			}
17019 		}
17020 
17021 		/* Checking drive event(s). */
17022 		mutex_enter(&pmportinfo->pmport_mutex);
17023 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
17024 		    pmportinfo->pmport_sata_drive != NULL) {
17025 			event_flags = pmportinfo->pmport_sata_drive->
17026 			    satadrv_event_flags;
17027 			if (event_flags & (SATA_EVNT_DEVICE_RESET |
17028 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
17029 
17030 				/* Have device event */
17031 				sata_process_pmdevice_reset(sata_hba_inst,
17032 				    saddr);
17033 			}
17034 		}
17035 		mutex_exit(&pmportinfo->pmport_mutex);
17036 
17037 		/* Release PORT_BUSY flag */
17038 		mutex_enter(&cportinfo->cport_mutex);
17039 		cportinfo->cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
17040 		mutex_exit(&cportinfo->cport_mutex);
17041 	}
17042 
17043 	SATADBG2(SATA_DBG_EVENTS_CNTRL|SATA_DBG_PMULT, sata_hba_inst,
17044 	    "[DONE] pmult event(s) on cport %d of controller %d",
17045 	    cport, ddi_get_instance(SATA_DIP(sata_hba_inst)));
17046 }
17047 
17048 /*
17049  * Process HBA power level change reported by HBA driver.
17050  * Not implemented at this time - event is ignored.
17051  */
17052 static void
17053 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst)
17054 {
17055 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17056 	    "Processing controller power level change", NULL);
17057 
17058 	/* Ignoring it for now */
17059 	mutex_enter(&sata_hba_inst->satahba_mutex);
17060 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
17061 	mutex_exit(&sata_hba_inst->satahba_mutex);
17062 }
17063 
17064 /*
17065  * Process port power level change reported by HBA driver.
17066  * Not implemented at this time - event is ignored.
17067  */
17068 static void
17069 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst,
17070     sata_address_t *saddr)
17071 {
17072 	sata_cport_info_t *cportinfo;
17073 
17074 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17075 	    "Processing port power level change", NULL);
17076 
17077 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
17078 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
17079 	/* Reset event flag */
17080 	cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
17081 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
17082 }
17083 
17084 /*
17085  * Process port failure reported by HBA driver.
17086  * cports support only - no pmports.
17087  */
17088 static void
17089 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst,
17090     sata_address_t *saddr)
17091 {
17092 	sata_cport_info_t *cportinfo;
17093 
17094 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
17095 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
17096 	/* Reset event flag first */
17097 	cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED;
17098 	/* If the port is in SHUTDOWN or FAILED state, ignore this event. */
17099 	if ((cportinfo->cport_state &
17100 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) {
17101 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
17102 		    cport_mutex);
17103 		return;
17104 	}
17105 	/* Fail the port */
17106 	cportinfo->cport_state = SATA_PSTATE_FAILED;
17107 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
17108 	sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport);
17109 }
17110 
17111 /*
17112  * Device Reset Event processing.
17113  * The seqeunce is managed by 3 stage flags:
17114  * - reset event reported,
17115  * - reset event being processed,
17116  * - request to clear device reset state.
17117  *
17118  * NOTE: This function has to be entered with cport mutex held. It exits with
17119  * mutex held as well, but can release mutex during the processing.
17120  */
17121 static void
17122 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst,
17123     sata_address_t *saddr)
17124 {
17125 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
17126 	sata_drive_info_t *sdinfo;
17127 	sata_cport_info_t *cportinfo;
17128 	sata_device_t sata_device;
17129 	int rval_probe, rval_set;
17130 
17131 	/* We only care about host sata cport for now */
17132 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
17133 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
17134 	/*
17135 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
17136 	 * state, ignore reset event.
17137 	 */
17138 	if (((cportinfo->cport_state &
17139 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
17140 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
17141 		sdinfo->satadrv_event_flags &=
17142 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
17143 		return;
17144 	}
17145 
17146 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) ==
17147 	    SATA_DTYPE_PMULT)) {
17148 		/*
17149 		 * Should not happened: this is already handled in
17150 		 * sata_hba_event_notify()
17151 		 */
17152 		mutex_exit(&cportinfo->cport_mutex);
17153 		goto done;
17154 	}
17155 
17156 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) &
17157 	    SATA_VALID_DEV_TYPE) == 0) {
17158 		/*
17159 		 * This should not happen - coding error.
17160 		 * But we can recover, so do not panic, just clean up
17161 		 * and if in debug mode, log the message.
17162 		 */
17163 #ifdef SATA_DEBUG
17164 		sata_log(sata_hba_inst, CE_WARN,
17165 		    "sata_process_device_reset: "
17166 		    "Invalid device type with sdinfo!", NULL);
17167 #endif
17168 		sdinfo->satadrv_event_flags = 0;
17169 		return;
17170 	}
17171 
17172 #ifdef SATA_DEBUG
17173 	if ((sdinfo->satadrv_event_flags &
17174 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
17175 		/* Nothing to do */
17176 		/* Something is weird - why we are processing dev reset? */
17177 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17178 		    "No device reset event!!!!", NULL);
17179 
17180 		return;
17181 	}
17182 	if ((sdinfo->satadrv_event_flags &
17183 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
17184 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
17185 		/* Something is weird - new device reset event */
17186 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17187 		    "Overlapping device reset events!", NULL);
17188 	}
17189 #endif
17190 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17191 	    "Processing port %d device reset", saddr->cport);
17192 
17193 	/* Clear event flag */
17194 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
17195 
17196 	/* It seems that we always need to check the port state first */
17197 	sata_device.satadev_rev = SATA_DEVICE_REV;
17198 	sata_device.satadev_addr = *saddr;
17199 	/*
17200 	 * We have to exit mutex, because the HBA probe port function may
17201 	 * block on its own mutex.
17202 	 */
17203 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
17204 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
17205 	    (SATA_DIP(sata_hba_inst), &sata_device);
17206 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
17207 	sata_update_port_info(sata_hba_inst, &sata_device);
17208 	if (rval_probe != SATA_SUCCESS) {
17209 		/* Something went wrong? Fail the port */
17210 		cportinfo->cport_state = SATA_PSTATE_FAILED;
17211 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
17212 		if (sdinfo != NULL)
17213 			sdinfo->satadrv_event_flags = 0;
17214 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
17215 		    cport_mutex);
17216 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17217 		    "SATA port %d probing failed",
17218 		    saddr->cport));
17219 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
17220 		    saddr->cport)->cport_mutex);
17221 		return;
17222 	}
17223 	if ((sata_device.satadev_scr.sstatus  &
17224 	    SATA_PORT_DEVLINK_UP_MASK) !=
17225 	    SATA_PORT_DEVLINK_UP ||
17226 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
17227 		/*
17228 		 * No device to process, anymore. Some other event processing
17229 		 * would or have already performed port info cleanup.
17230 		 * To be safe (HBA may need it), request clearing device
17231 		 * reset condition.
17232 		 */
17233 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
17234 		if (sdinfo != NULL) {
17235 			sdinfo->satadrv_event_flags &=
17236 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
17237 			sdinfo->satadrv_event_flags |=
17238 			    SATA_EVNT_CLEAR_DEVICE_RESET;
17239 		}
17240 		return;
17241 	}
17242 
17243 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
17244 	if (sdinfo == NULL) {
17245 		return;
17246 	}
17247 	if ((sdinfo->satadrv_event_flags &
17248 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
17249 		/*
17250 		 * Start tracking time for device feature restoration and
17251 		 * identification. Save current time (lbolt value).
17252 		 */
17253 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
17254 	}
17255 	/* Mark device reset processing as active */
17256 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
17257 
17258 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
17259 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
17260 
17261 	rval_set = sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1);
17262 
17263 	if (rval_set  != SATA_SUCCESS) {
17264 		/*
17265 		 * Restoring drive setting failed.
17266 		 * Probe the port first, to check if the port state has changed
17267 		 */
17268 		sata_device.satadev_rev = SATA_DEVICE_REV;
17269 		sata_device.satadev_addr = *saddr;
17270 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
17271 		/* probe port */
17272 		rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
17273 		    (SATA_DIP(sata_hba_inst), &sata_device);
17274 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
17275 		    cport_mutex);
17276 		if (rval_probe == SATA_SUCCESS &&
17277 		    (sata_device.satadev_state &
17278 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
17279 		    (sata_device.satadev_scr.sstatus  &
17280 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
17281 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
17282 			/*
17283 			 * We may retry this a bit later - in-process reset
17284 			 * condition should be already set.
17285 			 * Track retry time for device identification.
17286 			 */
17287 			if ((cportinfo->cport_dev_type &
17288 			    SATA_VALID_DEV_TYPE) != 0 &&
17289 			    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL &&
17290 			    sdinfo->satadrv_reset_time != 0) {
17291 				clock_t cur_time = ddi_get_lbolt();
17292 				/*
17293 				 * If the retry time limit was not
17294 				 * exceeded, retry.
17295 				 */
17296 				if ((cur_time - sdinfo->satadrv_reset_time) <
17297 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
17298 					mutex_enter(
17299 					    &sata_hba_inst->satahba_mutex);
17300 					sata_hba_inst->satahba_event_flags |=
17301 					    SATA_EVNT_MAIN;
17302 					mutex_exit(
17303 					    &sata_hba_inst->satahba_mutex);
17304 					mutex_enter(&sata_mutex);
17305 					sata_event_pending |= SATA_EVNT_MAIN;
17306 					mutex_exit(&sata_mutex);
17307 					return;
17308 				}
17309 				if (rval_set == SATA_RETRY) {
17310 					/*
17311 					 * Setting drive features failed, but
17312 					 * the drive is still accessible,
17313 					 * so emit a warning message before
17314 					 * return.
17315 					 */
17316 					mutex_exit(&SATA_CPORT_INFO(
17317 					    sata_hba_inst,
17318 					    saddr->cport)->cport_mutex);
17319 					goto done;
17320 				}
17321 			}
17322 			/* Fail the drive */
17323 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
17324 
17325 			sata_log(sata_hba_inst, CE_WARN,
17326 			    "SATA device at port %d - device failed",
17327 			    saddr->cport);
17328 		}
17329 		/*
17330 		 * No point of retrying - device failed or some other event
17331 		 * processing or already did or will do port info cleanup.
17332 		 * To be safe (HBA may need it),
17333 		 * request clearing device reset condition.
17334 		 */
17335 		sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
17336 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
17337 		sdinfo->satadrv_reset_time = 0;
17338 		return;
17339 	}
17340 done:
17341 	/*
17342 	 * If setting of drive features failed, but the drive is still
17343 	 * accessible, emit a warning message.
17344 	 */
17345 	if (rval_set == SATA_RETRY) {
17346 		sata_log(sata_hba_inst, CE_WARN,
17347 		    "SATA device at port %d - desired setting could not be "
17348 		    "restored after reset. Device may not operate as expected.",
17349 		    saddr->cport);
17350 	}
17351 	/*
17352 	 * Raise the flag indicating that the next sata command could
17353 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
17354 	 * reset is reported.
17355 	 */
17356 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
17357 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
17358 		sdinfo->satadrv_reset_time = 0;
17359 		if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) {
17360 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
17361 			sdinfo->satadrv_event_flags &=
17362 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
17363 			sdinfo->satadrv_event_flags |=
17364 			    SATA_EVNT_CLEAR_DEVICE_RESET;
17365 		}
17366 	}
17367 }
17368 
17369 
17370 /*
17371  * Port Multiplier Port Device Reset Event processing.
17372  *
17373  * NOTE: This function has to be entered with pmport mutex held. It exits with
17374  * mutex held as well, but can release mutex during the processing.
17375  */
17376 static void
17377 sata_process_pmdevice_reset(sata_hba_inst_t *sata_hba_inst,
17378     sata_address_t *saddr)
17379 {
17380 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
17381 	sata_drive_info_t *sdinfo = NULL;
17382 	sata_cport_info_t *cportinfo = NULL;
17383 	sata_pmport_info_t *pmportinfo = NULL;
17384 	sata_pmult_info_t *pminfo = NULL;
17385 	sata_device_t sata_device;
17386 	uint8_t cport = saddr->cport;
17387 	uint8_t pmport = saddr->pmport;
17388 	int rval;
17389 
17390 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17391 	    "Processing drive reset at port %d:%d", cport, pmport);
17392 
17393 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
17394 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
17395 	sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport);
17396 
17397 	/*
17398 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
17399 	 * state, ignore reset event.
17400 	 */
17401 	if (((cportinfo->cport_state &
17402 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
17403 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
17404 		sdinfo->satadrv_event_flags &=
17405 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
17406 		return;
17407 	}
17408 
17409 	if ((pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
17410 		/*
17411 		 * This should not happen - coding error.
17412 		 * But we can recover, so do not panic, just clean up
17413 		 * and if in debug mode, log the message.
17414 		 */
17415 #ifdef SATA_DEBUG
17416 		sata_log(sata_hba_inst, CE_WARN,
17417 		    "sata_process_pmdevice_reset: "
17418 		    "Invalid device type with sdinfo!", NULL);
17419 #endif
17420 		sdinfo->satadrv_event_flags = 0;
17421 		return;
17422 	}
17423 
17424 #ifdef SATA_DEBUG
17425 	if ((sdinfo->satadrv_event_flags &
17426 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
17427 		/* Nothing to do */
17428 		/* Something is weird - why we are processing dev reset? */
17429 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17430 		    "No device reset event!!!!", NULL);
17431 
17432 		return;
17433 	}
17434 	if ((sdinfo->satadrv_event_flags &
17435 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
17436 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
17437 		/* Something is weird - new device reset event */
17438 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17439 		    "Overlapping device reset events!", NULL);
17440 	}
17441 #endif
17442 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17443 	    "Processing port %d:%d device reset", cport, pmport);
17444 
17445 	/* Clear event flag */
17446 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
17447 
17448 	/* It seems that we always need to check the port state first */
17449 	sata_device.satadev_rev = SATA_DEVICE_REV;
17450 	sata_device.satadev_addr = *saddr;
17451 	/*
17452 	 * We have to exit mutex, because the HBA probe port function may
17453 	 * block on its own mutex.
17454 	 */
17455 	mutex_exit(&pmportinfo->pmport_mutex);
17456 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
17457 	    (SATA_DIP(sata_hba_inst), &sata_device);
17458 	mutex_enter(&pmportinfo->pmport_mutex);
17459 
17460 	sata_update_pmport_info(sata_hba_inst, &sata_device);
17461 	if (rval != SATA_SUCCESS) {
17462 		/* Something went wrong? Fail the port */
17463 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
17464 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
17465 		    saddr->pmport);
17466 		if (sdinfo != NULL)
17467 			sdinfo->satadrv_event_flags = 0;
17468 		mutex_exit(&pmportinfo->pmport_mutex);
17469 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17470 		    "SATA port %d:%d probing failed",
17471 		    saddr->cport, saddr->pmport));
17472 		mutex_enter(&pmportinfo->pmport_mutex);
17473 		return;
17474 	}
17475 	if ((sata_device.satadev_scr.sstatus  &
17476 	    SATA_PORT_DEVLINK_UP_MASK) !=
17477 	    SATA_PORT_DEVLINK_UP ||
17478 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
17479 		/*
17480 		 * No device to process, anymore. Some other event processing
17481 		 * would or have already performed port info cleanup.
17482 		 * To be safe (HBA may need it), request clearing device
17483 		 * reset condition.
17484 		 */
17485 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
17486 		    saddr->pmport);
17487 		if (sdinfo != NULL) {
17488 			sdinfo->satadrv_event_flags &=
17489 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
17490 			/* must clear flags on cport */
17491 			pminfo = SATA_PMULT_INFO(sata_hba_inst,
17492 			    saddr->cport);
17493 			pminfo->pmult_event_flags |=
17494 			    SATA_EVNT_CLEAR_DEVICE_RESET;
17495 		}
17496 		return;
17497 	}
17498 
17499 	sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, saddr->cport,
17500 	    saddr->pmport);
17501 	if (sdinfo == NULL) {
17502 		return;
17503 	}
17504 	if ((sdinfo->satadrv_event_flags &
17505 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
17506 		/*
17507 		 * Start tracking time for device feature restoration and
17508 		 * identification. Save current time (lbolt value).
17509 		 */
17510 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
17511 	}
17512 	/* Mark device reset processing as active */
17513 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
17514 
17515 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
17516 	mutex_exit(&pmportinfo->pmport_mutex);
17517 
17518 	if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) ==
17519 	    SATA_FAILURE) {
17520 		/*
17521 		 * Restoring drive setting failed.
17522 		 * Probe the port first, to check if the port state has changed
17523 		 */
17524 		sata_device.satadev_rev = SATA_DEVICE_REV;
17525 		sata_device.satadev_addr = *saddr;
17526 		sata_device.satadev_addr.qual = SATA_ADDR_PMPORT;
17527 
17528 		/* probe port */
17529 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
17530 		    (SATA_DIP(sata_hba_inst), &sata_device);
17531 		mutex_enter(&pmportinfo->pmport_mutex);
17532 		if (rval == SATA_SUCCESS &&
17533 		    (sata_device.satadev_state &
17534 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
17535 		    (sata_device.satadev_scr.sstatus  &
17536 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
17537 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
17538 			/*
17539 			 * We may retry this a bit later - in-process reset
17540 			 * condition should be already set.
17541 			 * Track retry time for device identification.
17542 			 */
17543 			if ((pmportinfo->pmport_dev_type &
17544 			    SATA_VALID_DEV_TYPE) != 0 &&
17545 			    SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL &&
17546 			    sdinfo->satadrv_reset_time != 0) {
17547 				clock_t cur_time = ddi_get_lbolt();
17548 				/*
17549 				 * If the retry time limit was not
17550 				 * exceeded, retry.
17551 				 */
17552 				if ((cur_time - sdinfo->satadrv_reset_time) <
17553 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
17554 					mutex_enter(
17555 					    &sata_hba_inst->satahba_mutex);
17556 					sata_hba_inst->satahba_event_flags |=
17557 					    SATA_EVNT_MAIN;
17558 					mutex_exit(
17559 					    &sata_hba_inst->satahba_mutex);
17560 					mutex_enter(&sata_mutex);
17561 					sata_event_pending |= SATA_EVNT_MAIN;
17562 					mutex_exit(&sata_mutex);
17563 					return;
17564 				}
17565 			}
17566 			/* Fail the drive */
17567 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
17568 
17569 			sata_log(sata_hba_inst, CE_WARN,
17570 			    "SATA device at port %d:%d - device failed",
17571 			    saddr->cport, saddr->pmport);
17572 		} else {
17573 			/*
17574 			 * No point of retrying - some other event processing
17575 			 * would or already did port info cleanup.
17576 			 * To be safe (HBA may need it),
17577 			 * request clearing device reset condition.
17578 			 */
17579 			sdinfo->satadrv_event_flags |=
17580 			    SATA_EVNT_CLEAR_DEVICE_RESET;
17581 		}
17582 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
17583 		sdinfo->satadrv_reset_time = 0;
17584 		return;
17585 	}
17586 	/*
17587 	 * Raise the flag indicating that the next sata command could
17588 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
17589 	 * reset is reported.
17590 	 */
17591 	mutex_enter(&pmportinfo->pmport_mutex);
17592 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
17593 		sdinfo->satadrv_reset_time = 0;
17594 		if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) {
17595 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
17596 			sdinfo->satadrv_event_flags &=
17597 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
17598 			/* must clear flags on cport */
17599 			pminfo = SATA_PMULT_INFO(sata_hba_inst,
17600 			    saddr->cport);
17601 			pminfo->pmult_event_flags |=
17602 			    SATA_EVNT_CLEAR_DEVICE_RESET;
17603 		}
17604 	}
17605 }
17606 
17607 /*
17608  * Port Link Events processing.
17609  * Every link established event may involve device reset (due to
17610  * COMRESET signal, equivalent of the hard reset) so arbitrarily
17611  * set device reset event for an attached device (if any).
17612  * If the port is in SHUTDOWN or FAILED state, ignore link events.
17613  *
17614  * The link established event processing varies, depending on the state
17615  * of the target node, HBA hotplugging capabilities, state of the port.
17616  * If the link is not active, the link established event is ignored.
17617  * If HBA cannot detect device attachment and there is no target node,
17618  * the link established event triggers device attach event processing.
17619  * Else, link established event triggers device reset event processing.
17620  *
17621  * The link lost event processing varies, depending on a HBA hotplugging
17622  * capability and the state of the port (link active or not active).
17623  * If the link is active, the lost link event is ignored.
17624  * If HBA cannot detect device removal, the lost link event triggers
17625  * device detached event processing after link lost timeout.
17626  * Else, the event is ignored.
17627  *
17628  * NOTE: Port multiplier ports events are handled by
17629  * sata_process_pmport_link_events();
17630  */
17631 static void
17632 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst,
17633     sata_address_t *saddr)
17634 {
17635 	sata_device_t sata_device;
17636 	sata_cport_info_t *cportinfo;
17637 	sata_drive_info_t *sdinfo;
17638 	uint32_t event_flags;
17639 	int rval;
17640 
17641 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17642 	    "Processing port %d link event(s)", saddr->cport);
17643 
17644 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
17645 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
17646 	event_flags = cportinfo->cport_event_flags;
17647 
17648 	/* Reset event flags first */
17649 	cportinfo->cport_event_flags &=
17650 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
17651 
17652 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
17653 	if ((cportinfo->cport_state &
17654 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
17655 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
17656 		    cport_mutex);
17657 		return;
17658 	}
17659 
17660 	/*
17661 	 * For the sanity sake get current port state.
17662 	 * Set device address only. Other sata_device fields should be
17663 	 * set by HBA driver.
17664 	 */
17665 	sata_device.satadev_rev = SATA_DEVICE_REV;
17666 	sata_device.satadev_addr = *saddr;
17667 	/*
17668 	 * We have to exit mutex, because the HBA probe port function may
17669 	 * block on its own mutex.
17670 	 */
17671 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
17672 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
17673 	    (SATA_DIP(sata_hba_inst), &sata_device);
17674 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
17675 	sata_update_port_info(sata_hba_inst, &sata_device);
17676 	if (rval != SATA_SUCCESS) {
17677 		/* Something went wrong? Fail the port */
17678 		cportinfo->cport_state = SATA_PSTATE_FAILED;
17679 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
17680 		    cport_mutex);
17681 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17682 		    "SATA port %d probing failed",
17683 		    saddr->cport));
17684 		/*
17685 		 * We may want to release device info structure, but
17686 		 * it is not necessary.
17687 		 */
17688 		return;
17689 	} else {
17690 		/* port probed successfully */
17691 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
17692 	}
17693 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
17694 
17695 		if ((sata_device.satadev_scr.sstatus &
17696 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
17697 			/* Ignore event */
17698 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17699 			    "Ignoring port %d link established event - "
17700 			    "link down",
17701 			    saddr->cport);
17702 			goto linklost;
17703 		}
17704 
17705 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17706 		    "Processing port %d link established event",
17707 		    saddr->cport);
17708 
17709 		/*
17710 		 * For the sanity sake check if a device is attached - check
17711 		 * return state of a port probing.
17712 		 */
17713 		if (sata_device.satadev_type != SATA_DTYPE_NONE) {
17714 			/*
17715 			 * HBA port probe indicated that there is a device
17716 			 * attached. Check if the framework had device info
17717 			 * structure attached for this device.
17718 			 */
17719 			if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
17720 				ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) !=
17721 				    NULL);
17722 
17723 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
17724 				if ((sdinfo->satadrv_type &
17725 				    SATA_VALID_DEV_TYPE) != 0) {
17726 					/*
17727 					 * Dev info structure is present.
17728 					 * If dev_type is set to known type in
17729 					 * the framework's drive info struct
17730 					 * then the device existed before and
17731 					 * the link was probably lost
17732 					 * momentarily - in such case
17733 					 * we may want to check device
17734 					 * identity.
17735 					 * Identity check is not supported now.
17736 					 *
17737 					 * Link established event
17738 					 * triggers device reset event.
17739 					 */
17740 					(SATA_CPORTINFO_DRV_INFO(cportinfo))->
17741 					    satadrv_event_flags |=
17742 					    SATA_EVNT_DEVICE_RESET;
17743 				}
17744 			} else if (cportinfo->cport_dev_type ==
17745 			    SATA_DTYPE_NONE) {
17746 				/*
17747 				 * We got new device attached! If HBA does not
17748 				 * generate device attached events, trigger it
17749 				 * here.
17750 				 */
17751 				if (!(SATA_FEATURES(sata_hba_inst) &
17752 				    SATA_CTLF_HOTPLUG)) {
17753 					cportinfo->cport_event_flags |=
17754 					    SATA_EVNT_DEVICE_ATTACHED;
17755 				}
17756 			}
17757 			/* Reset link lost timeout */
17758 			cportinfo->cport_link_lost_time = 0;
17759 		}
17760 	}
17761 linklost:
17762 	if (event_flags & SATA_EVNT_LINK_LOST) {
17763 		if ((sata_device.satadev_scr.sstatus &
17764 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
17765 			/* Ignore event */
17766 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17767 			    "Ignoring port %d link lost event - link is up",
17768 			    saddr->cport);
17769 			goto done;
17770 		}
17771 #ifdef SATA_DEBUG
17772 		if (cportinfo->cport_link_lost_time == 0) {
17773 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17774 			    "Processing port %d link lost event",
17775 			    saddr->cport);
17776 		}
17777 #endif
17778 		/*
17779 		 * When HBA cannot generate device attached/detached events,
17780 		 * we need to track link lost time and eventually generate
17781 		 * device detach event.
17782 		 */
17783 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
17784 			/* We are tracking link lost time */
17785 			if (cportinfo->cport_link_lost_time == 0) {
17786 				/* save current time (lbolt value) */
17787 				cportinfo->cport_link_lost_time =
17788 				    ddi_get_lbolt();
17789 				/* just keep link lost event */
17790 				cportinfo->cport_event_flags |=
17791 				    SATA_EVNT_LINK_LOST;
17792 			} else {
17793 				clock_t cur_time = ddi_get_lbolt();
17794 				if ((cur_time -
17795 				    cportinfo->cport_link_lost_time) >=
17796 				    drv_usectohz(
17797 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
17798 					/* trigger device detach event */
17799 					cportinfo->cport_event_flags |=
17800 					    SATA_EVNT_DEVICE_DETACHED;
17801 					cportinfo->cport_link_lost_time = 0;
17802 					SATADBG1(SATA_DBG_EVENTS,
17803 					    sata_hba_inst,
17804 					    "Triggering port %d "
17805 					    "device detached event",
17806 					    saddr->cport);
17807 				} else {
17808 					/* keep link lost event */
17809 					cportinfo->cport_event_flags |=
17810 					    SATA_EVNT_LINK_LOST;
17811 				}
17812 			}
17813 		}
17814 		/*
17815 		 * We could change port state to disable/delay access to
17816 		 * the attached device until the link is recovered.
17817 		 */
17818 	}
17819 done:
17820 	event_flags = cportinfo->cport_event_flags;
17821 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
17822 	if (event_flags != 0) {
17823 		mutex_enter(&sata_hba_inst->satahba_mutex);
17824 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
17825 		mutex_exit(&sata_hba_inst->satahba_mutex);
17826 		mutex_enter(&sata_mutex);
17827 		sata_event_pending |= SATA_EVNT_MAIN;
17828 		mutex_exit(&sata_mutex);
17829 	}
17830 }
17831 
17832 /*
17833  * Port Multiplier Port Link Events processing.
17834  */
17835 static void
17836 sata_process_pmport_link_events(sata_hba_inst_t *sata_hba_inst,
17837     sata_address_t *saddr)
17838 {
17839 	sata_device_t sata_device;
17840 	sata_pmport_info_t *pmportinfo = NULL;
17841 	sata_drive_info_t *sdinfo = NULL;
17842 	uint32_t event_flags;
17843 	uint8_t cport = saddr->cport;
17844 	uint8_t pmport = saddr->pmport;
17845 	int rval;
17846 
17847 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17848 	    "Processing port %d:%d link event(s)",
17849 	    cport, pmport);
17850 
17851 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
17852 	mutex_enter(&pmportinfo->pmport_mutex);
17853 	event_flags = pmportinfo->pmport_event_flags;
17854 
17855 	/* Reset event flags first */
17856 	pmportinfo->pmport_event_flags &=
17857 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
17858 
17859 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
17860 	if ((pmportinfo->pmport_state &
17861 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
17862 		mutex_exit(&pmportinfo->pmport_mutex);
17863 		return;
17864 	}
17865 
17866 	/*
17867 	 * For the sanity sake get current port state.
17868 	 * Set device address only. Other sata_device fields should be
17869 	 * set by HBA driver.
17870 	 */
17871 	sata_device.satadev_rev = SATA_DEVICE_REV;
17872 	sata_device.satadev_addr = *saddr;
17873 	/*
17874 	 * We have to exit mutex, because the HBA probe port function may
17875 	 * block on its own mutex.
17876 	 */
17877 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
17878 	    saddr->pmport));
17879 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
17880 	    (SATA_DIP(sata_hba_inst), &sata_device);
17881 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
17882 	    saddr->pmport));
17883 	sata_update_pmport_info(sata_hba_inst, &sata_device);
17884 	if (rval != SATA_SUCCESS) {
17885 		/* Something went wrong? Fail the port */
17886 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
17887 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
17888 		    saddr->pmport));
17889 		SATA_LOG_D((sata_hba_inst, CE_WARN,
17890 		    "SATA port %d:%d probing failed",
17891 		    saddr->cport, saddr->pmport));
17892 		/*
17893 		 * We may want to release device info structure, but
17894 		 * it is not necessary.
17895 		 */
17896 		return;
17897 	} else {
17898 		/* port probed successfully */
17899 		pmportinfo->pmport_state |=
17900 		    SATA_STATE_PROBED | SATA_STATE_READY;
17901 	}
17902 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst,
17903 	    saddr->cport, saddr->pmport));
17904 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst,
17905 	    saddr->cport, saddr->pmport));
17906 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
17907 
17908 		if ((sata_device.satadev_scr.sstatus &
17909 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
17910 			/* Ignore event */
17911 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17912 			    "Ignoring port %d:%d link established event - "
17913 			    "link down",
17914 			    saddr->cport, saddr->pmport);
17915 			goto linklost;
17916 		}
17917 
17918 		SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17919 		    "Processing port %d:%d link established event",
17920 		    cport, pmport);
17921 
17922 		/*
17923 		 * For the sanity sake check if a device is attached - check
17924 		 * return state of a port probing.
17925 		 */
17926 		if (sata_device.satadev_type != SATA_DTYPE_NONE &&
17927 		    sata_device.satadev_type != SATA_DTYPE_PMULT) {
17928 			/*
17929 			 * HBA port probe indicated that there is a device
17930 			 * attached. Check if the framework had device info
17931 			 * structure attached for this device.
17932 			 */
17933 			if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
17934 				ASSERT(SATA_PMPORTINFO_DRV_INFO(pmportinfo) !=
17935 				    NULL);
17936 
17937 				sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
17938 				if ((sdinfo->satadrv_type &
17939 				    SATA_VALID_DEV_TYPE) != 0) {
17940 					/*
17941 					 * Dev info structure is present.
17942 					 * If dev_type is set to known type in
17943 					 * the framework's drive info struct
17944 					 * then the device existed before and
17945 					 * the link was probably lost
17946 					 * momentarily - in such case
17947 					 * we may want to check device
17948 					 * identity.
17949 					 * Identity check is not supported now.
17950 					 *
17951 					 * Link established event
17952 					 * triggers device reset event.
17953 					 */
17954 					(SATA_PMPORTINFO_DRV_INFO(pmportinfo))->
17955 					    satadrv_event_flags |=
17956 					    SATA_EVNT_DEVICE_RESET;
17957 				}
17958 			} else if (pmportinfo->pmport_dev_type ==
17959 			    SATA_DTYPE_NONE) {
17960 				/*
17961 				 * We got new device attached! If HBA does not
17962 				 * generate device attached events, trigger it
17963 				 * here.
17964 				 */
17965 				if (!(SATA_FEATURES(sata_hba_inst) &
17966 				    SATA_CTLF_HOTPLUG)) {
17967 					pmportinfo->pmport_event_flags |=
17968 					    SATA_EVNT_DEVICE_ATTACHED;
17969 				}
17970 			}
17971 			/* Reset link lost timeout */
17972 			pmportinfo->pmport_link_lost_time = 0;
17973 		}
17974 	}
17975 linklost:
17976 	if (event_flags & SATA_EVNT_LINK_LOST) {
17977 #ifdef SATA_DEBUG
17978 		if (pmportinfo->pmport_link_lost_time == 0) {
17979 			SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
17980 			    "Processing port %d:%d link lost event",
17981 			    saddr->cport, saddr->pmport);
17982 		}
17983 #endif
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 lost event - link is up",
17989 			    saddr->cport, saddr->pmport);
17990 			goto done;
17991 		}
17992 		/*
17993 		 * When HBA cannot generate device attached/detached events,
17994 		 * we need to track link lost time and eventually generate
17995 		 * device detach event.
17996 		 */
17997 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
17998 			/* We are tracking link lost time */
17999 			if (pmportinfo->pmport_link_lost_time == 0) {
18000 				/* save current time (lbolt value) */
18001 				pmportinfo->pmport_link_lost_time =
18002 				    ddi_get_lbolt();
18003 				/* just keep link lost event */
18004 				pmportinfo->pmport_event_flags |=
18005 				    SATA_EVNT_LINK_LOST;
18006 			} else {
18007 				clock_t cur_time = ddi_get_lbolt();
18008 				if ((cur_time -
18009 				    pmportinfo->pmport_link_lost_time) >=
18010 				    drv_usectohz(
18011 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
18012 					/* trigger device detach event */
18013 					pmportinfo->pmport_event_flags |=
18014 					    SATA_EVNT_DEVICE_DETACHED;
18015 					pmportinfo->pmport_link_lost_time = 0;
18016 					SATADBG2(SATA_DBG_EVENTS,
18017 					    sata_hba_inst,
18018 					    "Triggering port %d:%d "
18019 					    "device detached event",
18020 					    saddr->cport, saddr->pmport);
18021 				} else {
18022 					/* keep link lost event */
18023 					pmportinfo->pmport_event_flags |=
18024 					    SATA_EVNT_LINK_LOST;
18025 				}
18026 			}
18027 		}
18028 		/*
18029 		 * We could change port state to disable/delay access to
18030 		 * the attached device until the link is recovered.
18031 		 */
18032 	}
18033 done:
18034 	event_flags = pmportinfo->pmport_event_flags;
18035 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, saddr->cport,
18036 	    saddr->pmport));
18037 	if (event_flags != 0) {
18038 		mutex_enter(&sata_hba_inst->satahba_mutex);
18039 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
18040 		mutex_exit(&sata_hba_inst->satahba_mutex);
18041 		mutex_enter(&sata_mutex);
18042 		sata_event_pending |= SATA_EVNT_MAIN;
18043 		mutex_exit(&sata_mutex);
18044 	}
18045 }
18046 
18047 /*
18048  * Device Detached Event processing.
18049  * Port is probed to find if a device is really gone. If so,
18050  * the device info structure is detached from the SATA port info structure
18051  * and released.
18052  * Port status is updated.
18053  *
18054  * NOTE: Port multiplier ports events are handled by
18055  * sata_process_pmdevice_detached()
18056  */
18057 static void
18058 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst,
18059     sata_address_t *saddr)
18060 {
18061 	sata_cport_info_t *cportinfo;
18062 	sata_pmport_info_t *pmportinfo;
18063 	sata_drive_info_t *sdevinfo;
18064 	sata_device_t sata_device;
18065 	sata_address_t pmport_addr;
18066 	char name[16];
18067 	uint8_t cport = saddr->cport;
18068 	int npmport;
18069 	int rval;
18070 
18071 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18072 	    "Processing port %d device detached", saddr->cport);
18073 
18074 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18075 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18076 	/* Clear event flag */
18077 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
18078 
18079 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
18080 	if ((cportinfo->cport_state &
18081 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
18082 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18083 		    cport_mutex);
18084 		return;
18085 	}
18086 	/* For sanity, re-probe the port */
18087 	sata_device.satadev_rev = SATA_DEVICE_REV;
18088 	sata_device.satadev_addr = *saddr;
18089 
18090 	/*
18091 	 * We have to exit mutex, because the HBA probe port function may
18092 	 * block on its own mutex.
18093 	 */
18094 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18095 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18096 	    (SATA_DIP(sata_hba_inst), &sata_device);
18097 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18098 	sata_update_port_info(sata_hba_inst, &sata_device);
18099 	if (rval != SATA_SUCCESS) {
18100 		/* Something went wrong? Fail the port */
18101 		cportinfo->cport_state = SATA_PSTATE_FAILED;
18102 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18103 		    cport_mutex);
18104 		SATA_LOG_D((sata_hba_inst, CE_WARN,
18105 		    "SATA port %d probing failed",
18106 		    saddr->cport));
18107 		/*
18108 		 * We may want to release device info structure, but
18109 		 * it is not necessary.
18110 		 */
18111 		return;
18112 	} else {
18113 		/* port probed successfully */
18114 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
18115 	}
18116 	/*
18117 	 * Check if a device is still attached. For sanity, check also
18118 	 * link status - if no link, there is no device.
18119 	 */
18120 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
18121 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
18122 	    SATA_DTYPE_NONE) {
18123 		/*
18124 		 * Device is still attached - ignore detach event.
18125 		 */
18126 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18127 		    cport_mutex);
18128 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18129 		    "Ignoring detach - device still attached to port %d",
18130 		    sata_device.satadev_addr.cport);
18131 		return;
18132 	}
18133 	/*
18134 	 * We need to detach and release device info structure here
18135 	 */
18136 	if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
18137 		/*
18138 		 * A port-multiplier is removed.
18139 		 *
18140 		 * Calling sata_process_pmdevice_detached() does not work
18141 		 * here. The port multiplier is gone, so we cannot probe
18142 		 * sub-port any more and all pmult-related data structure must
18143 		 * be de-allocated immediately. Following structure of every
18144 		 * implemented sub-port behind the pmult are required to
18145 		 * released.
18146 		 *
18147 		 *   - attachment point
18148 		 *   - target node
18149 		 *   - sata_drive_info
18150 		 *   - sata_pmport_info
18151 		 */
18152 		for (npmport = 0; npmport < SATA_NUM_PMPORTS(sata_hba_inst,
18153 		    cport); npmport ++) {
18154 			SATADBG2(SATA_DBG_PMULT|SATA_DBG_EVENTS_PROC,
18155 			    sata_hba_inst,
18156 			    "Detaching target node at port %d:%d",
18157 			    cport, npmport);
18158 
18159 			mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
18160 
18161 			/* Remove attachment point. */
18162 			name[0] = '\0';
18163 			(void) sprintf(name, "%d.%d", cport, npmport);
18164 			ddi_remove_minor_node(SATA_DIP(sata_hba_inst), name);
18165 			sata_log(sata_hba_inst, CE_NOTE,
18166 			    "Remove attachment point of port %d:%d",
18167 			    cport, npmport);
18168 
18169 			/* Remove target node */
18170 			pmport_addr.cport = cport;
18171 			pmport_addr.pmport = (uint8_t)npmport;
18172 			pmport_addr.qual = SATA_ADDR_PMPORT;
18173 			sata_remove_target_node(sata_hba_inst, &pmport_addr);
18174 
18175 			mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
18176 
18177 			/* Release sata_pmport_info & sata_drive_info. */
18178 			pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
18179 			    cport, npmport);
18180 			ASSERT(pmportinfo != NULL);
18181 
18182 			sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
18183 			if (sdevinfo != NULL) {
18184 				(void) kmem_free((void *) sdevinfo,
18185 				    sizeof (sata_drive_info_t));
18186 			}
18187 
18188 			/* Release sata_pmport_info at last */
18189 			(void) kmem_free((void *) pmportinfo,
18190 			    sizeof (sata_pmport_info_t));
18191 		}
18192 
18193 		/* Finally, release sata_pmult_info */
18194 		(void) kmem_free((void *)
18195 		    SATA_CPORTINFO_PMULT_INFO(cportinfo),
18196 		    sizeof (sata_pmult_info_t));
18197 		SATA_CPORTINFO_PMULT_INFO(cportinfo) = NULL;
18198 
18199 		sata_log(sata_hba_inst, CE_WARN,
18200 		    "SATA port-multiplier detached at port %d", cport);
18201 
18202 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
18203 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
18204 		    saddr->cport)->cport_mutex);
18205 	} else {
18206 		if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
18207 			sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
18208 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
18209 			(void) kmem_free((void *)sdevinfo,
18210 			    sizeof (sata_drive_info_t));
18211 		}
18212 		sata_log(sata_hba_inst, CE_WARN,
18213 		    "SATA device detached at port %d", cport);
18214 
18215 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
18216 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
18217 		    saddr->cport)->cport_mutex);
18218 
18219 		/*
18220 		 * Try to offline a device and remove target node
18221 		 * if it still exists
18222 		 */
18223 		sata_remove_target_node(sata_hba_inst, saddr);
18224 	}
18225 
18226 
18227 	/*
18228 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
18229 	 * with the hint: SE_HINT_REMOVE
18230 	 */
18231 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
18232 }
18233 
18234 /*
18235  * Port Multiplier Port Device Deattached Event processing.
18236  *
18237  * NOTE: No Mutex should be hold.
18238  */
18239 static void
18240 sata_process_pmdevice_detached(sata_hba_inst_t *sata_hba_inst,
18241     sata_address_t *saddr)
18242 {
18243 	sata_pmport_info_t *pmportinfo;
18244 	sata_drive_info_t *sdevinfo;
18245 	sata_device_t sata_device;
18246 	int rval;
18247 	uint8_t cport, pmport;
18248 
18249 	cport = saddr->cport;
18250 	pmport = saddr->pmport;
18251 
18252 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18253 	    "Processing port %d:%d device detached",
18254 	    cport, pmport);
18255 
18256 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
18257 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
18258 
18259 	/* Clear event flag */
18260 	pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
18261 
18262 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
18263 	if ((pmportinfo->pmport_state &
18264 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
18265 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
18266 		return;
18267 	}
18268 	/* For sanity, re-probe the port */
18269 	sata_device.satadev_rev = SATA_DEVICE_REV;
18270 	sata_device.satadev_addr = *saddr;
18271 
18272 	/*
18273 	 * We have to exit mutex, because the HBA probe port function may
18274 	 * block on its own mutex.
18275 	 */
18276 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
18277 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18278 	    (SATA_DIP(sata_hba_inst), &sata_device);
18279 	mutex_enter(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
18280 	sata_update_pmport_info(sata_hba_inst, &sata_device);
18281 	if (rval != SATA_SUCCESS) {
18282 		/* Something went wrong? Fail the port */
18283 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
18284 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
18285 		SATA_LOG_D((sata_hba_inst, CE_WARN,
18286 		    "SATA port %d:%d probing failed",
18287 		    saddr->pmport));
18288 		/*
18289 		 * We may want to release device info structure, but
18290 		 * it is not necessary.
18291 		 */
18292 		return;
18293 	} else {
18294 		/* port probed successfully */
18295 		pmportinfo->pmport_state |=
18296 		    SATA_STATE_PROBED | SATA_STATE_READY;
18297 	}
18298 	/*
18299 	 * Check if a device is still attached. For sanity, check also
18300 	 * link status - if no link, there is no device.
18301 	 */
18302 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
18303 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
18304 	    SATA_DTYPE_NONE) {
18305 		/*
18306 		 * Device is still attached - ignore detach event.
18307 		 */
18308 		mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
18309 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18310 		    "Ignoring detach - device still attached to port %d",
18311 		    sata_device.satadev_addr.pmport);
18312 		return;
18313 	}
18314 	/*
18315 	 * We need to detach and release device info structure here
18316 	 */
18317 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
18318 		sdevinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
18319 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
18320 		(void) kmem_free((void *)sdevinfo,
18321 		    sizeof (sata_drive_info_t));
18322 	}
18323 	pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
18324 	/*
18325 	 * Device cannot be reached anymore, even if the target node may be
18326 	 * still present.
18327 	 */
18328 	mutex_exit(&SATA_PMPORT_MUTEX(sata_hba_inst, cport, pmport));
18329 
18330 	/*
18331 	 * Try to offline a device and remove target node if it still exists
18332 	 */
18333 	sata_remove_target_node(sata_hba_inst, saddr);
18334 
18335 	/*
18336 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
18337 	 * with the hint: SE_HINT_REMOVE
18338 	 */
18339 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
18340 }
18341 
18342 
18343 /*
18344  * Device Attached Event processing.
18345  * Port state is checked to verify that a device is really attached. If so,
18346  * the device info structure is created and attached to the SATA port info
18347  * structure.
18348  *
18349  * If attached device cannot be identified or set-up, the retry for the
18350  * attach processing is set-up. Subsequent daemon run would try again to
18351  * identify the device, until the time limit is reached
18352  * (SATA_DEV_IDENTIFY_TIMEOUT).
18353  *
18354  * This function cannot be called in interrupt context (it may sleep).
18355  *
18356  * NOTE: Port multiplier ports events are handled by
18357  * sata_process_pmdevice_attached()
18358  */
18359 static void
18360 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst,
18361     sata_address_t *saddr)
18362 {
18363 	sata_cport_info_t *cportinfo = NULL;
18364 	sata_drive_info_t *sdevinfo = NULL;
18365 	sata_pmult_info_t *pmultinfo = NULL;
18366 	sata_pmport_info_t *pmportinfo = NULL;
18367 	sata_device_t sata_device;
18368 	dev_info_t *tdip;
18369 	uint32_t event_flags = 0, pmult_event_flags = 0;
18370 	int rval;
18371 	int npmport;
18372 
18373 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18374 	    "Processing port %d device attached", saddr->cport);
18375 
18376 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18377 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18378 
18379 	/* Clear attach event flag first */
18380 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
18381 
18382 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
18383 	if ((cportinfo->cport_state &
18384 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
18385 		cportinfo->cport_dev_attach_time = 0;
18386 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18387 		    cport_mutex);
18388 		return;
18389 	}
18390 
18391 	/*
18392 	 * If the sata_drive_info structure is found attached to the port info,
18393 	 * despite the fact the device was removed and now it is re-attached,
18394 	 * the old drive info structure was not removed.
18395 	 * Arbitrarily release device info structure.
18396 	 */
18397 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
18398 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
18399 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
18400 		(void) kmem_free((void *)sdevinfo,
18401 		    sizeof (sata_drive_info_t));
18402 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18403 		    "Arbitrarily detaching old device info.", NULL);
18404 	}
18405 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
18406 
18407 	/* For sanity, re-probe the port */
18408 	sata_device.satadev_rev = SATA_DEVICE_REV;
18409 	sata_device.satadev_addr = *saddr;
18410 
18411 	/*
18412 	 * We have to exit mutex, because the HBA probe port function may
18413 	 * block on its own mutex.
18414 	 */
18415 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18416 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18417 	    (SATA_DIP(sata_hba_inst), &sata_device);
18418 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18419 	sata_update_port_info(sata_hba_inst, &sata_device);
18420 	if (rval != SATA_SUCCESS) {
18421 		/* Something went wrong? Fail the port */
18422 		cportinfo->cport_state = SATA_PSTATE_FAILED;
18423 		cportinfo->cport_dev_attach_time = 0;
18424 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18425 		    cport_mutex);
18426 		SATA_LOG_D((sata_hba_inst, CE_WARN,
18427 		    "SATA port %d probing failed",
18428 		    saddr->cport));
18429 		return;
18430 	} else {
18431 		/* port probed successfully */
18432 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
18433 	}
18434 	/*
18435 	 * Check if a device is still attached. For sanity, check also
18436 	 * link status - if no link, there is no device.
18437 	 */
18438 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
18439 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
18440 	    SATA_DTYPE_NONE) {
18441 		/*
18442 		 * No device - ignore attach event.
18443 		 */
18444 		cportinfo->cport_dev_attach_time = 0;
18445 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18446 		    cport_mutex);
18447 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18448 		    "Ignoring attach - no device connected to port %d",
18449 		    sata_device.satadev_addr.cport);
18450 		return;
18451 	}
18452 
18453 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18454 	/*
18455 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
18456 	 * with the hint: SE_HINT_INSERT
18457 	 */
18458 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
18459 
18460 	/*
18461 	 * Port reprobing will take care of the creation of the device
18462 	 * info structure and determination of the device type.
18463 	 */
18464 	sata_device.satadev_addr = *saddr;
18465 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
18466 	    SATA_DEV_IDENTIFY_NORETRY);
18467 
18468 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
18469 	    cport_mutex);
18470 	if ((cportinfo->cport_state & SATA_STATE_READY) &&
18471 	    (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) {
18472 		/* Some device is attached to the port */
18473 		if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) {
18474 			/*
18475 			 * A device was not successfully attached.
18476 			 * Track retry time for device identification.
18477 			 */
18478 			if (cportinfo->cport_dev_attach_time != 0) {
18479 				clock_t cur_time = ddi_get_lbolt();
18480 				/*
18481 				 * If the retry time limit was not exceeded,
18482 				 * reinstate attach event.
18483 				 */
18484 				if ((cur_time -
18485 				    cportinfo->cport_dev_attach_time) <
18486 				    drv_usectohz(
18487 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
18488 					/* OK, restore attach event */
18489 					cportinfo->cport_event_flags |=
18490 					    SATA_EVNT_DEVICE_ATTACHED;
18491 				} else {
18492 					/* Timeout - cannot identify device */
18493 					cportinfo->cport_dev_attach_time = 0;
18494 					sata_log(sata_hba_inst,
18495 					    CE_WARN,
18496 					    "Could not identify SATA device "
18497 					    "at port %d",
18498 					    saddr->cport);
18499 				}
18500 			} else {
18501 				/*
18502 				 * Start tracking time for device
18503 				 * identification.
18504 				 * Save current time (lbolt value).
18505 				 */
18506 				cportinfo->cport_dev_attach_time =
18507 				    ddi_get_lbolt();
18508 				/* Restore attach event */
18509 				cportinfo->cport_event_flags |=
18510 				    SATA_EVNT_DEVICE_ATTACHED;
18511 			}
18512 		} else if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
18513 			cportinfo->cport_dev_attach_time = 0;
18514 			sata_log(sata_hba_inst, CE_NOTE,
18515 			    "SATA port-multiplier detected at port %d",
18516 			    saddr->cport);
18517 
18518 			if (SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL) {
18519 				/* Log the info of new port multiplier */
18520 				sata_show_pmult_info(sata_hba_inst,
18521 				    &sata_device);
18522 			}
18523 
18524 			ASSERT(SATA_CPORTINFO_PMULT_INFO(cportinfo) != NULL);
18525 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
18526 			for (npmport = 0; npmport <
18527 			    pmultinfo->pmult_num_dev_ports; npmport++) {
18528 				pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
18529 				    saddr->cport, npmport);
18530 				ASSERT(pmportinfo != NULL);
18531 
18532 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
18533 				    saddr->cport)->cport_mutex);
18534 				mutex_enter(&pmportinfo->pmport_mutex);
18535 				/* Marked all pmports with link events. */
18536 				pmportinfo->pmport_event_flags =
18537 				    SATA_EVNT_LINK_ESTABLISHED;
18538 				pmult_event_flags |=
18539 				    pmportinfo->pmport_event_flags;
18540 				mutex_exit(&pmportinfo->pmport_mutex);
18541 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
18542 				    saddr->cport)->cport_mutex);
18543 			}
18544 			/* Auto-online is not available for PMult now. */
18545 
18546 		} else {
18547 			/*
18548 			 * If device was successfully attached, the subsequent
18549 			 * action depends on a state of the
18550 			 * sata_auto_online variable. If it is set to zero.
18551 			 * an explicit 'configure' command will be needed to
18552 			 * configure it. If its value is non-zero, we will
18553 			 * attempt to online (configure) the device.
18554 			 * First, log the message indicating that a device
18555 			 * was attached.
18556 			 */
18557 			cportinfo->cport_dev_attach_time = 0;
18558 			sata_log(sata_hba_inst, CE_WARN,
18559 			    "SATA device detected at port %d", saddr->cport);
18560 
18561 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
18562 				sata_drive_info_t new_sdinfo;
18563 
18564 				/* Log device info data */
18565 				new_sdinfo = *(SATA_CPORTINFO_DRV_INFO(
18566 				    cportinfo));
18567 				sata_show_drive_info(sata_hba_inst,
18568 				    &new_sdinfo);
18569 			}
18570 
18571 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
18572 			    saddr->cport)->cport_mutex);
18573 
18574 			/*
18575 			 * Make sure that there is no target node for that
18576 			 * device. If so, release it. It should not happen,
18577 			 * unless we had problem removing the node when
18578 			 * device was detached.
18579 			 */
18580 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
18581 			    saddr->cport, saddr->pmport);
18582 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
18583 			    saddr->cport)->cport_mutex);
18584 			if (tdip != NULL) {
18585 
18586 #ifdef SATA_DEBUG
18587 				if ((cportinfo->cport_event_flags &
18588 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
18589 					sata_log(sata_hba_inst, CE_WARN,
18590 					    "sata_process_device_attached: "
18591 					    "old device target node exists!");
18592 #endif
18593 				/*
18594 				 * target node exists - try to unconfigure
18595 				 * device and remove the node.
18596 				 */
18597 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
18598 				    saddr->cport)->cport_mutex);
18599 				rval = ndi_devi_offline(tdip,
18600 				    NDI_DEVI_REMOVE);
18601 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
18602 				    saddr->cport)->cport_mutex);
18603 
18604 				if (rval == NDI_SUCCESS) {
18605 					cportinfo->cport_event_flags &=
18606 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
18607 					cportinfo->cport_tgtnode_clean = B_TRUE;
18608 				} else {
18609 					/*
18610 					 * PROBLEM - the target node remained
18611 					 * and it belongs to a previously
18612 					 * attached device.
18613 					 * This happens when the file was open
18614 					 * or the node was waiting for
18615 					 * resources at the time the
18616 					 * associated device was removed.
18617 					 * Instruct event daemon to retry the
18618 					 * cleanup later.
18619 					 */
18620 					sata_log(sata_hba_inst,
18621 					    CE_WARN,
18622 					    "Application(s) accessing "
18623 					    "previously attached SATA "
18624 					    "device have to release "
18625 					    "it before newly inserted "
18626 					    "device can be made accessible.",
18627 					    saddr->cport);
18628 					cportinfo->cport_event_flags |=
18629 					    SATA_EVNT_TARGET_NODE_CLEANUP;
18630 					cportinfo->cport_tgtnode_clean =
18631 					    B_FALSE;
18632 				}
18633 			}
18634 			if (sata_auto_online != 0) {
18635 				cportinfo->cport_event_flags |=
18636 				    SATA_EVNT_AUTOONLINE_DEVICE;
18637 			}
18638 
18639 		}
18640 	} else {
18641 		cportinfo->cport_dev_attach_time = 0;
18642 	}
18643 
18644 	event_flags = cportinfo->cport_event_flags;
18645 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
18646 	if (event_flags != 0 || pmult_event_flags != 0) {
18647 		mutex_enter(&sata_hba_inst->satahba_mutex);
18648 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
18649 		mutex_exit(&sata_hba_inst->satahba_mutex);
18650 		mutex_enter(&sata_mutex);
18651 		sata_event_pending |= SATA_EVNT_MAIN;
18652 		mutex_exit(&sata_mutex);
18653 	}
18654 }
18655 
18656 /*
18657  * Port Multiplier Port Device Attached Event processing.
18658  *
18659  * NOTE: No Mutex should be hold.
18660  */
18661 static void
18662 sata_process_pmdevice_attached(sata_hba_inst_t *sata_hba_inst,
18663     sata_address_t *saddr)
18664 {
18665 	sata_pmport_info_t *pmportinfo;
18666 	sata_drive_info_t *sdinfo;
18667 	sata_device_t sata_device;
18668 	dev_info_t *tdip;
18669 	uint32_t event_flags;
18670 	uint8_t cport = saddr->cport;
18671 	uint8_t pmport = saddr->pmport;
18672 	int rval;
18673 
18674 	SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18675 	    "Processing port %d:%d device attached", cport, pmport);
18676 
18677 	pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
18678 
18679 	mutex_enter(&pmportinfo->pmport_mutex);
18680 
18681 	/* Clear attach event flag first */
18682 	pmportinfo->pmport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
18683 
18684 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
18685 	if ((pmportinfo->pmport_state &
18686 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
18687 		pmportinfo->pmport_dev_attach_time = 0;
18688 		mutex_exit(&pmportinfo->pmport_mutex);
18689 		return;
18690 	}
18691 
18692 	/*
18693 	 * If the sata_drive_info structure is found attached to the port info,
18694 	 * despite the fact the device was removed and now it is re-attached,
18695 	 * the old drive info structure was not removed.
18696 	 * Arbitrarily release device info structure.
18697 	 */
18698 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
18699 		sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
18700 		SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
18701 		(void) kmem_free((void *)sdinfo,
18702 		    sizeof (sata_drive_info_t));
18703 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18704 		    "Arbitrarily detaching old device info.", NULL);
18705 	}
18706 	pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
18707 
18708 	/* For sanity, re-probe the port */
18709 	sata_device.satadev_rev = SATA_DEVICE_REV;
18710 	sata_device.satadev_addr = *saddr;
18711 
18712 	/*
18713 	 * We have to exit mutex, because the HBA probe port function may
18714 	 * block on its own mutex.
18715 	 */
18716 	mutex_exit(&pmportinfo->pmport_mutex);
18717 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
18718 	    (SATA_DIP(sata_hba_inst), &sata_device);
18719 	mutex_enter(&pmportinfo->pmport_mutex);
18720 
18721 	sata_update_pmport_info(sata_hba_inst, &sata_device);
18722 	if (rval != SATA_SUCCESS) {
18723 		/* Something went wrong? Fail the port */
18724 		pmportinfo->pmport_state = SATA_PSTATE_FAILED;
18725 		pmportinfo->pmport_dev_attach_time = 0;
18726 		mutex_exit(&pmportinfo->pmport_mutex);
18727 		SATA_LOG_D((sata_hba_inst, CE_WARN,
18728 		    "SATA port %d:%d probing failed", cport, pmport));
18729 		return;
18730 	} else {
18731 		/* pmport probed successfully */
18732 		pmportinfo->pmport_state |=
18733 		    SATA_STATE_PROBED | SATA_STATE_READY;
18734 	}
18735 	/*
18736 	 * Check if a device is still attached. For sanity, check also
18737 	 * link status - if no link, there is no device.
18738 	 */
18739 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
18740 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
18741 	    SATA_DTYPE_NONE) {
18742 		/*
18743 		 * No device - ignore attach event.
18744 		 */
18745 		pmportinfo->pmport_dev_attach_time = 0;
18746 		mutex_exit(&pmportinfo->pmport_mutex);
18747 		SATADBG2(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18748 		    "Ignoring attach - no device connected to port %d:%d",
18749 		    cport, pmport);
18750 		return;
18751 	}
18752 
18753 	mutex_exit(&pmportinfo->pmport_mutex);
18754 	/*
18755 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
18756 	 * with the hint: SE_HINT_INSERT
18757 	 */
18758 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
18759 
18760 	/*
18761 	 * Port reprobing will take care of the creation of the device
18762 	 * info structure and determination of the device type.
18763 	 */
18764 	sata_device.satadev_addr = *saddr;
18765 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
18766 	    SATA_DEV_IDENTIFY_NORETRY);
18767 
18768 	mutex_enter(&pmportinfo->pmport_mutex);
18769 	if ((pmportinfo->pmport_state & SATA_STATE_READY) &&
18770 	    (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE)) {
18771 		/* Some device is attached to the port */
18772 		if (pmportinfo->pmport_dev_type == SATA_DTYPE_UNKNOWN) {
18773 			/*
18774 			 * A device was not successfully attached.
18775 			 * Track retry time for device identification.
18776 			 */
18777 			if (pmportinfo->pmport_dev_attach_time != 0) {
18778 				clock_t cur_time = ddi_get_lbolt();
18779 				/*
18780 				 * If the retry time limit was not exceeded,
18781 				 * reinstate attach event.
18782 				 */
18783 				if ((cur_time -
18784 				    pmportinfo->pmport_dev_attach_time) <
18785 				    drv_usectohz(
18786 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
18787 					/* OK, restore attach event */
18788 					pmportinfo->pmport_event_flags |=
18789 					    SATA_EVNT_DEVICE_ATTACHED;
18790 				} else {
18791 					/* Timeout - cannot identify device */
18792 					pmportinfo->pmport_dev_attach_time = 0;
18793 					sata_log(sata_hba_inst, CE_WARN,
18794 					    "Could not identify SATA device "
18795 					    "at port %d:%d",
18796 					    cport, pmport);
18797 				}
18798 			} else {
18799 				/*
18800 				 * Start tracking time for device
18801 				 * identification.
18802 				 * Save current time (lbolt value).
18803 				 */
18804 				pmportinfo->pmport_dev_attach_time =
18805 				    ddi_get_lbolt();
18806 				/* Restore attach event */
18807 				pmportinfo->pmport_event_flags |=
18808 				    SATA_EVNT_DEVICE_ATTACHED;
18809 			}
18810 		} else {
18811 			/*
18812 			 * If device was successfully attached, the subsequent
18813 			 * action depends on a state of the
18814 			 * sata_auto_online variable. If it is set to zero.
18815 			 * an explicit 'configure' command will be needed to
18816 			 * configure it. If its value is non-zero, we will
18817 			 * attempt to online (configure) the device.
18818 			 * First, log the message indicating that a device
18819 			 * was attached.
18820 			 */
18821 			pmportinfo->pmport_dev_attach_time = 0;
18822 			sata_log(sata_hba_inst, CE_WARN,
18823 			    "SATA device detected at port %d:%d",
18824 			    cport, pmport);
18825 
18826 			if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
18827 				sata_drive_info_t new_sdinfo;
18828 
18829 				/* Log device info data */
18830 				new_sdinfo = *(SATA_PMPORTINFO_DRV_INFO(
18831 				    pmportinfo));
18832 				sata_show_drive_info(sata_hba_inst,
18833 				    &new_sdinfo);
18834 			}
18835 
18836 			mutex_exit(&pmportinfo->pmport_mutex);
18837 
18838 			/*
18839 			 * Make sure that there is no target node for that
18840 			 * device. If so, release it. It should not happen,
18841 			 * unless we had problem removing the node when
18842 			 * device was detached.
18843 			 */
18844 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
18845 			    saddr->cport, saddr->pmport);
18846 			mutex_enter(&pmportinfo->pmport_mutex);
18847 			if (tdip != NULL) {
18848 
18849 #ifdef SATA_DEBUG
18850 				if ((pmportinfo->pmport_event_flags &
18851 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
18852 					sata_log(sata_hba_inst, CE_WARN,
18853 					    "sata_process_device_attached: "
18854 					    "old device target node exists!");
18855 #endif
18856 				/*
18857 				 * target node exists - try to unconfigure
18858 				 * device and remove the node.
18859 				 */
18860 				mutex_exit(&pmportinfo->pmport_mutex);
18861 				rval = ndi_devi_offline(tdip,
18862 				    NDI_DEVI_REMOVE);
18863 				mutex_enter(&pmportinfo->pmport_mutex);
18864 
18865 				if (rval == NDI_SUCCESS) {
18866 					pmportinfo->pmport_event_flags &=
18867 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
18868 					pmportinfo->pmport_tgtnode_clean =
18869 					    B_TRUE;
18870 				} else {
18871 					/*
18872 					 * PROBLEM - the target node remained
18873 					 * and it belongs to a previously
18874 					 * attached device.
18875 					 * This happens when the file was open
18876 					 * or the node was waiting for
18877 					 * resources at the time the
18878 					 * associated device was removed.
18879 					 * Instruct event daemon to retry the
18880 					 * cleanup later.
18881 					 */
18882 					sata_log(sata_hba_inst,
18883 					    CE_WARN,
18884 					    "Application(s) accessing "
18885 					    "previously attached SATA "
18886 					    "device have to release "
18887 					    "it before newly inserted "
18888 					    "device can be made accessible."
18889 					    "at port %d:%d",
18890 					    cport, pmport);
18891 					pmportinfo->pmport_event_flags |=
18892 					    SATA_EVNT_TARGET_NODE_CLEANUP;
18893 					pmportinfo->pmport_tgtnode_clean =
18894 					    B_FALSE;
18895 				}
18896 			}
18897 			if (sata_auto_online != 0) {
18898 				pmportinfo->pmport_event_flags |=
18899 				    SATA_EVNT_AUTOONLINE_DEVICE;
18900 			}
18901 
18902 		}
18903 	} else {
18904 		pmportinfo->pmport_dev_attach_time = 0;
18905 	}
18906 
18907 	event_flags = pmportinfo->pmport_event_flags;
18908 	mutex_exit(&pmportinfo->pmport_mutex);
18909 	if (event_flags != 0) {
18910 		mutex_enter(&sata_hba_inst->satahba_mutex);
18911 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
18912 		mutex_exit(&sata_hba_inst->satahba_mutex);
18913 		mutex_enter(&sata_mutex);
18914 		sata_event_pending |= SATA_EVNT_MAIN;
18915 		mutex_exit(&sata_mutex);
18916 	}
18917 
18918 	/* clear the reset_in_progress events */
18919 	if (SATA_PMPORTINFO_DRV_INFO(pmportinfo) != NULL) {
18920 		if (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) {
18921 			/* must clear flags on cport */
18922 			sata_pmult_info_t *pminfo =
18923 			    SATA_PMULT_INFO(sata_hba_inst,
18924 			    saddr->cport);
18925 			pminfo->pmult_event_flags |=
18926 			    SATA_EVNT_CLEAR_DEVICE_RESET;
18927 		}
18928 	}
18929 }
18930 
18931 /*
18932  * Device Target Node Cleanup Event processing.
18933  * If the target node associated with a sata port device is in
18934  * DEVI_DEVICE_REMOVED state, an attempt is made to remove it.
18935  * If the target node cannot be removed, the event flag is left intact,
18936  * so that event daemon may re-run this function later.
18937  *
18938  * This function cannot be called in interrupt context (it may sleep).
18939  *
18940  * NOTE: Processes cport events only, not port multiplier ports.
18941  */
18942 static void
18943 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
18944     sata_address_t *saddr)
18945 {
18946 	sata_cport_info_t *cportinfo;
18947 	dev_info_t *tdip;
18948 
18949 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18950 	    "Processing port %d device target node cleanup", saddr->cport);
18951 
18952 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
18953 
18954 	/*
18955 	 * Check if there is target node for that device and it is in the
18956 	 * DEVI_DEVICE_REMOVED state. If so, release it.
18957 	 */
18958 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport,
18959 	    saddr->pmport);
18960 	if (tdip != NULL) {
18961 		/*
18962 		 * target node exists - check if it is target node of
18963 		 * a removed device.
18964 		 */
18965 		if (sata_check_device_removed(tdip) == B_TRUE) {
18966 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
18967 			    "sata_process_target_node_cleanup: "
18968 			    "old device target node exists!", NULL);
18969 			/*
18970 			 * Unconfigure and remove the target node
18971 			 */
18972 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) ==
18973 			    NDI_SUCCESS) {
18974 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
18975 				    saddr->cport)->cport_mutex);
18976 				cportinfo->cport_event_flags &=
18977 				    ~SATA_EVNT_TARGET_NODE_CLEANUP;
18978 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
18979 				    saddr->cport)->cport_mutex);
18980 				return;
18981 			}
18982 			/*
18983 			 * Event daemon will retry the cleanup later.
18984 			 */
18985 			mutex_enter(&sata_hba_inst->satahba_mutex);
18986 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
18987 			mutex_exit(&sata_hba_inst->satahba_mutex);
18988 			mutex_enter(&sata_mutex);
18989 			sata_event_pending |= SATA_EVNT_MAIN;
18990 			mutex_exit(&sata_mutex);
18991 		}
18992 	} else {
18993 		if (saddr->qual == SATA_ADDR_CPORT ||
18994 		    saddr->qual == SATA_ADDR_DCPORT) {
18995 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
18996 			    saddr->cport)->cport_mutex);
18997 			cportinfo->cport_event_flags &=
18998 			    ~SATA_EVNT_TARGET_NODE_CLEANUP;
18999 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19000 			    saddr->cport)->cport_mutex);
19001 		} else {
19002 			/* sanity check */
19003 			if (SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) !=
19004 			    SATA_DTYPE_PMULT || SATA_PMULT_INFO(sata_hba_inst,
19005 			    saddr->cport) == NULL)
19006 				return;
19007 			if (SATA_PMPORT_INFO(sata_hba_inst, saddr->cport,
19008 			    saddr->pmport) == NULL)
19009 				return;
19010 
19011 			mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst,
19012 			    saddr->cport, saddr->pmport)->pmport_mutex);
19013 			SATA_PMPORT_INFO(sata_hba_inst, saddr->cport,
19014 			    saddr->pmport)->pmport_event_flags &=
19015 			    ~SATA_EVNT_TARGET_NODE_CLEANUP;
19016 			mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst,
19017 			    saddr->cport, saddr->pmport)->pmport_mutex);
19018 		}
19019 	}
19020 }
19021 
19022 /*
19023  * Device AutoOnline Event processing.
19024  * If attached device is to be onlined, an attempt is made to online this
19025  * device, but only if there is no lingering (old) target node present.
19026  * If the device cannot be onlined, the event flag is left intact,
19027  * so that event daemon may re-run this function later.
19028  *
19029  * This function cannot be called in interrupt context (it may sleep).
19030  *
19031  * NOTE: Processes cport events only, not port multiplier ports.
19032  */
19033 static void
19034 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst,
19035     sata_address_t *saddr)
19036 {
19037 	sata_cport_info_t *cportinfo;
19038 	sata_drive_info_t *sdinfo;
19039 	sata_device_t sata_device;
19040 	dev_info_t *tdip;
19041 
19042 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19043 	    "Processing port %d attached device auto-onlining", saddr->cport);
19044 
19045 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
19046 
19047 	/*
19048 	 * Check if device is present and recognized. If not, reset event.
19049 	 */
19050 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19051 	if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
19052 		/* Nothing to online */
19053 		cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
19054 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19055 		    saddr->cport)->cport_mutex);
19056 		return;
19057 	}
19058 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19059 
19060 	/*
19061 	 * Check if there is target node for this device and if it is in the
19062 	 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep
19063 	 * the event for later processing.
19064 	 */
19065 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport,
19066 	    saddr->pmport);
19067 	if (tdip != NULL) {
19068 		/*
19069 		 * target node exists - check if it is target node of
19070 		 * a removed device.
19071 		 */
19072 		if (sata_check_device_removed(tdip) == B_TRUE) {
19073 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
19074 			    "sata_process_device_autoonline: "
19075 			    "old device target node exists!", NULL);
19076 			/*
19077 			 * Event daemon will retry device onlining later.
19078 			 */
19079 			mutex_enter(&sata_hba_inst->satahba_mutex);
19080 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19081 			mutex_exit(&sata_hba_inst->satahba_mutex);
19082 			mutex_enter(&sata_mutex);
19083 			sata_event_pending |= SATA_EVNT_MAIN;
19084 			mutex_exit(&sata_mutex);
19085 			return;
19086 		}
19087 		/*
19088 		 * If the target node is not in the 'removed" state, assume
19089 		 * that it belongs to this device. There is nothing more to do,
19090 		 * but reset the event.
19091 		 */
19092 	} else {
19093 
19094 		/*
19095 		 * Try to online the device
19096 		 * If there is any reset-related event, remove it. We are
19097 		 * configuring the device and no state restoring is needed.
19098 		 */
19099 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19100 		    saddr->cport)->cport_mutex);
19101 		sata_device.satadev_addr = *saddr;
19102 		if (saddr->qual == SATA_ADDR_CPORT)
19103 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
19104 		else
19105 			sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
19106 		sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
19107 		if (sdinfo != NULL) {
19108 			if (sdinfo->satadrv_event_flags &
19109 			    (SATA_EVNT_DEVICE_RESET |
19110 			    SATA_EVNT_INPROC_DEVICE_RESET))
19111 				sdinfo->satadrv_event_flags = 0;
19112 			sdinfo->satadrv_event_flags |=
19113 			    SATA_EVNT_CLEAR_DEVICE_RESET;
19114 
19115 			/* Need to create a new target node. */
19116 			cportinfo->cport_tgtnode_clean = B_TRUE;
19117 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19118 			    saddr->cport)->cport_mutex);
19119 			tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
19120 			    sata_hba_inst, &sata_device.satadev_addr);
19121 			if (tdip == NULL) {
19122 				/*
19123 				 * Configure (onlining) failed.
19124 				 * We will NOT retry
19125 				 */
19126 				SATA_LOG_D((sata_hba_inst, CE_WARN,
19127 				    "sata_process_device_autoonline: "
19128 				    "configuring SATA device at port %d failed",
19129 				    saddr->cport));
19130 			}
19131 		} else {
19132 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19133 			    saddr->cport)->cport_mutex);
19134 		}
19135 
19136 	}
19137 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
19138 	cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
19139 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19140 	    saddr->cport)->cport_mutex);
19141 }
19142 
19143 
19144 static void
19145 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr,
19146     int hint)
19147 {
19148 	char ap[MAXPATHLEN];
19149 	nvlist_t *ev_attr_list = NULL;
19150 	int err;
19151 
19152 	/* Allocate and build sysevent attribute list */
19153 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP);
19154 	if (err != 0) {
19155 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19156 		    "sata_gen_sysevent: "
19157 		    "cannot allocate memory for sysevent attributes\n"));
19158 		return;
19159 	}
19160 	/* Add hint attribute */
19161 	err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint));
19162 	if (err != 0) {
19163 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19164 		    "sata_gen_sysevent: "
19165 		    "failed to add DR_HINT attr for sysevent"));
19166 		nvlist_free(ev_attr_list);
19167 		return;
19168 	}
19169 	/*
19170 	 * Add AP attribute.
19171 	 * Get controller pathname and convert it into AP pathname by adding
19172 	 * a target number.
19173 	 */
19174 	(void) snprintf(ap, MAXPATHLEN, "/devices");
19175 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap));
19176 	(void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d",
19177 	    SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual));
19178 
19179 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap);
19180 	if (err != 0) {
19181 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19182 		    "sata_gen_sysevent: "
19183 		    "failed to add DR_AP_ID attr for sysevent"));
19184 		nvlist_free(ev_attr_list);
19185 		return;
19186 	}
19187 
19188 	/* Generate/log sysevent */
19189 	err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR,
19190 	    ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP);
19191 	if (err != DDI_SUCCESS) {
19192 		SATA_LOG_D((sata_hba_inst, CE_WARN,
19193 		    "sata_gen_sysevent: "
19194 		    "cannot log sysevent, err code %x\n", err));
19195 	}
19196 
19197 	nvlist_free(ev_attr_list);
19198 }
19199 
19200 
19201 
19202 
19203 /*
19204  * Set DEVI_DEVICE_REMOVED state in the SATA device target node.
19205  */
19206 static void
19207 sata_set_device_removed(dev_info_t *tdip)
19208 {
19209 	int circ;
19210 
19211 	ASSERT(tdip != NULL);
19212 
19213 	ndi_devi_enter(tdip, &circ);
19214 	mutex_enter(&DEVI(tdip)->devi_lock);
19215 	DEVI_SET_DEVICE_REMOVED(tdip);
19216 	mutex_exit(&DEVI(tdip)->devi_lock);
19217 	ndi_devi_exit(tdip, circ);
19218 }
19219 
19220 
19221 /*
19222  * Set internal event instructing event daemon to try
19223  * to perform the target node cleanup.
19224  */
19225 static void
19226 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
19227     sata_address_t *saddr)
19228 {
19229 	if (saddr->qual == SATA_ADDR_CPORT ||
19230 	    saddr->qual == SATA_ADDR_DCPORT) {
19231 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
19232 		    saddr->cport)->cport_mutex);
19233 		SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |=
19234 		    SATA_EVNT_TARGET_NODE_CLEANUP;
19235 		SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
19236 		    cport_tgtnode_clean = B_FALSE;
19237 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
19238 		    saddr->cport)->cport_mutex);
19239 	} else {
19240 		mutex_enter(&SATA_PMPORT_INFO(sata_hba_inst,
19241 		    saddr->cport, saddr->pmport)->pmport_mutex);
19242 		SATA_PMPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport,
19243 		    saddr->pmport) |= SATA_EVNT_TARGET_NODE_CLEANUP;
19244 		SATA_PMPORT_INFO(sata_hba_inst, saddr->cport, saddr->pmport)->
19245 		    pmport_tgtnode_clean = B_FALSE;
19246 		mutex_exit(&SATA_PMPORT_INFO(sata_hba_inst,
19247 		    saddr->cport, saddr->pmport)->pmport_mutex);
19248 	}
19249 	mutex_enter(&sata_hba_inst->satahba_mutex);
19250 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
19251 	mutex_exit(&sata_hba_inst->satahba_mutex);
19252 	mutex_enter(&sata_mutex);
19253 	sata_event_pending |= SATA_EVNT_MAIN;
19254 	mutex_exit(&sata_mutex);
19255 }
19256 
19257 
19258 /*
19259  * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state,
19260  * i.e. check if the target node state indicates that it belongs to a removed
19261  * device.
19262  *
19263  * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state,
19264  * B_FALSE otherwise.
19265  */
19266 static boolean_t
19267 sata_check_device_removed(dev_info_t *tdip)
19268 {
19269 	ASSERT(tdip != NULL);
19270 
19271 	if (DEVI_IS_DEVICE_REMOVED(tdip))
19272 		return (B_TRUE);
19273 	else
19274 		return (B_FALSE);
19275 }
19276 
19277 /* ************************ FAULT INJECTTION **************************** */
19278 
19279 #ifdef SATA_INJECT_FAULTS
19280 
19281 static	uint32_t sata_fault_count = 0;
19282 static	uint32_t sata_fault_suspend_count = 0;
19283 
19284 /*
19285  * Inject sata pkt fault
19286  * It modifies returned values of the sata packet.
19287  * It returns immediately if:
19288  * pkt fault injection is not enabled (via sata_inject_fault,
19289  * sata_inject_fault_count), or invalid fault is specified (sata_fault_type),
19290  * or pkt does not contain command to be faulted (set in sata_fault_cmd), or
19291  * pkt is not directed to specified fault controller/device
19292  * (sata_fault_ctrl_dev and sata_fault_device).
19293  * If fault controller is not specified, fault injection applies to all
19294  * controllers and devices.
19295  *
19296  * First argument is the pointer to the executed sata packet.
19297  * Second argument is a pointer to a value returned by the HBA tran_start
19298  * function.
19299  * Third argument specifies injected error. Injected sata packet faults
19300  * are the satapkt_reason values.
19301  * SATA_PKT_BUSY		-1	Not completed, busy
19302  * SATA_PKT_DEV_ERROR		1	Device reported error
19303  * SATA_PKT_QUEUE_FULL		2	Not accepted, queue full
19304  * SATA_PKT_PORT_ERROR		3	Not completed, port error
19305  * SATA_PKT_CMD_UNSUPPORTED	4	Cmd unsupported
19306  * SATA_PKT_ABORTED		5	Aborted by request
19307  * SATA_PKT_TIMEOUT		6	Operation timeut
19308  * SATA_PKT_RESET		7	Aborted by reset request
19309  *
19310  * Additional global variables affecting the execution:
19311  *
19312  * sata_inject_fault_count variable specifies number of times in row the
19313  * error is injected. Value of -1 specifies permanent fault, ie. every time
19314  * the fault injection point is reached, the fault is injected and a pause
19315  * between fault injection specified by sata_inject_fault_pause_count is
19316  * ignored). Fault injection routine decrements sata_inject_fault_count
19317  * (if greater than zero) until it reaches 0. No fault is injected when
19318  * sata_inject_fault_count is 0 (zero).
19319  *
19320  * sata_inject_fault_pause_count variable specifies number of times a fault
19321  * injection is bypassed (pause between fault injections).
19322  * If set to 0, a fault is injected only a number of times specified by
19323  * sata_inject_fault_count.
19324  *
19325  * The fault counts are static, so for periodic errors they have to be manually
19326  * reset to start repetition sequence from scratch.
19327  * If the original value returned by the HBA tran_start function is not
19328  * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error
19329  * is injected (to avoid masking real problems);
19330  *
19331  * NOTE: In its current incarnation, this function should be invoked only for
19332  * commands executed in SYNCHRONOUS mode.
19333  */
19334 
19335 
19336 static void
19337 sata_inject_pkt_fault(sata_pkt_t *spkt, int *rval, int fault)
19338 {
19339 
19340 	if (sata_inject_fault != SATA_INJECT_PKT_FAULT)
19341 		return;
19342 
19343 	if (sata_inject_fault_count == 0)
19344 		return;
19345 
19346 	if (fault == 0)
19347 		return;
19348 
19349 	if (sata_fault_cmd != spkt->satapkt_cmd.satacmd_cmd_reg)
19350 		return;
19351 
19352 	if (sata_fault_ctrl != NULL) {
19353 		sata_pkt_txlate_t *spx =
19354 		    (sata_pkt_txlate_t *)spkt->satapkt_framework_private;
19355 
19356 		if (sata_fault_ctrl != NULL && sata_fault_ctrl !=
19357 		    spx->txlt_sata_hba_inst->satahba_dip)
19358 			return;
19359 
19360 		if (sata_fault_device.satadev_addr.cport !=
19361 		    spkt->satapkt_device.satadev_addr.cport ||
19362 		    sata_fault_device.satadev_addr.pmport !=
19363 		    spkt->satapkt_device.satadev_addr.pmport ||
19364 		    sata_fault_device.satadev_addr.qual !=
19365 		    spkt->satapkt_device.satadev_addr.qual)
19366 			return;
19367 	}
19368 
19369 	/* Modify pkt return parameters */
19370 	if (*rval != SATA_TRAN_ACCEPTED ||
19371 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
19372 		sata_fault_count = 0;
19373 		sata_fault_suspend_count = 0;
19374 		return;
19375 	}
19376 	if (sata_fault_count == 0 && sata_fault_suspend_count != 0) {
19377 		/* Pause in the injection */
19378 		sata_fault_suspend_count -= 1;
19379 		return;
19380 	}
19381 
19382 	if (sata_fault_count == 0 && sata_fault_suspend_count == 0) {
19383 		/*
19384 		 * Init inject fault cycle. If fault count is set to -1,
19385 		 * it is a permanent fault.
19386 		 */
19387 		if (sata_inject_fault_count != -1) {
19388 			sata_fault_count = sata_inject_fault_count;
19389 			sata_fault_suspend_count =
19390 			    sata_inject_fault_pause_count;
19391 			if (sata_fault_suspend_count == 0)
19392 				sata_inject_fault_count = 0;
19393 		}
19394 	}
19395 
19396 	if (sata_fault_count != 0)
19397 		sata_fault_count -= 1;
19398 
19399 	switch (fault) {
19400 	case SATA_PKT_BUSY:
19401 		*rval = SATA_TRAN_BUSY;
19402 		spkt->satapkt_reason = SATA_PKT_BUSY;
19403 		break;
19404 
19405 	case SATA_PKT_QUEUE_FULL:
19406 		*rval = SATA_TRAN_QUEUE_FULL;
19407 		spkt->satapkt_reason = SATA_PKT_QUEUE_FULL;
19408 		break;
19409 
19410 	case SATA_PKT_CMD_UNSUPPORTED:
19411 		*rval = SATA_TRAN_CMD_UNSUPPORTED;
19412 		spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED;
19413 		break;
19414 
19415 	case SATA_PKT_PORT_ERROR:
19416 		/* This is "rejected" command */
19417 		*rval = SATA_TRAN_PORT_ERROR;
19418 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
19419 		/* Additional error setup could be done here - port state */
19420 		break;
19421 
19422 	case SATA_PKT_DEV_ERROR:
19423 		spkt->satapkt_reason = SATA_PKT_DEV_ERROR;
19424 		/*
19425 		 * Additional error setup could be done here
19426 		 */
19427 		break;
19428 
19429 	case SATA_PKT_ABORTED:
19430 		spkt->satapkt_reason = SATA_PKT_ABORTED;
19431 		break;
19432 
19433 	case SATA_PKT_TIMEOUT:
19434 		spkt->satapkt_reason = SATA_PKT_TIMEOUT;
19435 		/* Additional error setup could be done here */
19436 		break;
19437 
19438 	case SATA_PKT_RESET:
19439 		spkt->satapkt_reason = SATA_PKT_RESET;
19440 		/*
19441 		 * Additional error setup could be done here - device reset
19442 		 */
19443 		break;
19444 
19445 	default:
19446 		break;
19447 	}
19448 }
19449 
19450 #endif
19451 
19452 /*
19453  * SATA Trace Ring Buffer
19454  * ----------------------
19455  *
19456  * Overview
19457  *
19458  * The SATA trace ring buffer is a ring buffer created and managed by
19459  * the SATA framework module that can be used by any module or driver
19460  * within the SATA framework to store debug messages.
19461  *
19462  * Ring Buffer Interfaces:
19463  *
19464  *	sata_vtrace_debug()	<-- Adds debug message to ring buffer
19465  *	sata_trace_debug()	<-- Wraps varargs into sata_vtrace_debug()
19466  *
19467  *	Note that the sata_trace_debug() interface was created to give
19468  *	consumers the flexibilty of sending debug messages to ring buffer
19469  *	as variable arguments.  Consumers can send type va_list debug
19470  *	messages directly to sata_vtrace_debug(). The sata_trace_debug()
19471  *	and sata_vtrace_debug() relationship is similar to that of
19472  *	cmn_err(9F) and vcmn_err(9F).
19473  *
19474  * Below is a diagram of the SATA trace ring buffer interfaces and
19475  * sample consumers:
19476  *
19477  * +---------------------------------+
19478  * |    o  o  SATA Framework Module  |
19479  * | o  SATA  o     +------------------+      +------------------+
19480  * |o   Trace  o <--|sata_vtrace_debug/|<-----|SATA HBA Driver #1|
19481  * |o   R-Buf  o    |sata_trace_debug  |<--+  +------------------+
19482  * | o        o     +------------------+   |  +------------------+
19483  * |    o  o                ^        |     +--|SATA HBA Driver #2|
19484  * |                        |        |        +------------------+
19485  * |           +------------------+  |
19486  * |           |SATA Debug Message|  |
19487  * |           +------------------+  |
19488  * +---------------------------------+
19489  *
19490  * Supporting Routines:
19491  *
19492  *	sata_trace_rbuf_alloc()	<-- Initializes ring buffer
19493  *	sata_trace_rbuf_free()	<-- Destroys ring buffer
19494  *	sata_trace_dmsg_alloc() <-- Creates or reuses buffer in ring buffer
19495  *	sata_trace_dmsg_free()	<-- Destroys content of ring buffer
19496  *
19497  * The default SATA trace ring buffer size is defined by DMSG_RING_SIZE.
19498  * The ring buffer size can be adjusted by setting dmsg_ring_size in
19499  * /etc/system to desired size in unit of bytes.
19500  *
19501  * The individual debug message size in the ring buffer is restricted
19502  * to DMSG_BUF_SIZE.
19503  */
19504 void
19505 sata_vtrace_debug(dev_info_t *dip, const char *fmt, va_list ap)
19506 {
19507 	sata_trace_dmsg_t *dmsg;
19508 
19509 	if (sata_debug_rbuf == NULL) {
19510 		return;
19511 	}
19512 
19513 	/*
19514 	 * If max size of ring buffer is smaller than size
19515 	 * required for one debug message then just return
19516 	 * since we have no room for the debug message.
19517 	 */
19518 	if (sata_debug_rbuf->maxsize < (sizeof (sata_trace_dmsg_t))) {
19519 		return;
19520 	}
19521 
19522 	mutex_enter(&sata_debug_rbuf->lock);
19523 
19524 	/* alloc or reuse on ring buffer */
19525 	dmsg = sata_trace_dmsg_alloc();
19526 
19527 	if (dmsg == NULL) {
19528 		/* resource allocation failed */
19529 		mutex_exit(&sata_debug_rbuf->lock);
19530 		return;
19531 	}
19532 
19533 	dmsg->dip = dip;
19534 	gethrestime(&dmsg->timestamp);
19535 
19536 	(void) vsnprintf(dmsg->buf, sizeof (dmsg->buf), fmt, ap);
19537 
19538 	mutex_exit(&sata_debug_rbuf->lock);
19539 }
19540 
19541 void
19542 sata_trace_debug(dev_info_t *dip, const char *fmt, ...)
19543 {
19544 	va_list ap;
19545 
19546 	va_start(ap, fmt);
19547 	sata_vtrace_debug(dip, fmt, ap);
19548 	va_end(ap);
19549 }
19550 
19551 /*
19552  * This routine is used to manage debug messages
19553  * on ring buffer.
19554  */
19555 static sata_trace_dmsg_t *
19556 sata_trace_dmsg_alloc(void)
19557 {
19558 	sata_trace_dmsg_t *dmsg_alloc, *dmsg = sata_debug_rbuf->dmsgp;
19559 
19560 	if (sata_debug_rbuf->looped == TRUE) {
19561 		sata_debug_rbuf->dmsgp = dmsg->next;
19562 		return (sata_debug_rbuf->dmsgp);
19563 	}
19564 
19565 	/*
19566 	 * If we're looping for the first time,
19567 	 * connect the ring.
19568 	 */
19569 	if (((sata_debug_rbuf->size + (sizeof (sata_trace_dmsg_t))) >
19570 	    sata_debug_rbuf->maxsize) && (sata_debug_rbuf->dmsgh != NULL)) {
19571 		dmsg->next = sata_debug_rbuf->dmsgh;
19572 		sata_debug_rbuf->dmsgp = sata_debug_rbuf->dmsgh;
19573 		sata_debug_rbuf->looped = TRUE;
19574 		return (sata_debug_rbuf->dmsgp);
19575 	}
19576 
19577 	/* If we've gotten this far then memory allocation is needed */
19578 	dmsg_alloc = kmem_zalloc(sizeof (sata_trace_dmsg_t), KM_NOSLEEP);
19579 	if (dmsg_alloc == NULL) {
19580 		sata_debug_rbuf->allocfailed++;
19581 		return (dmsg_alloc);
19582 	} else {
19583 		sata_debug_rbuf->size += sizeof (sata_trace_dmsg_t);
19584 	}
19585 
19586 	if (sata_debug_rbuf->dmsgp != NULL) {
19587 		dmsg->next = dmsg_alloc;
19588 		sata_debug_rbuf->dmsgp = dmsg->next;
19589 		return (sata_debug_rbuf->dmsgp);
19590 	} else {
19591 		/*
19592 		 * We should only be here if we're initializing
19593 		 * the ring buffer.
19594 		 */
19595 		if (sata_debug_rbuf->dmsgh == NULL) {
19596 			sata_debug_rbuf->dmsgh = dmsg_alloc;
19597 		} else {
19598 			/* Something is wrong */
19599 			kmem_free(dmsg_alloc, sizeof (sata_trace_dmsg_t));
19600 			return (NULL);
19601 		}
19602 
19603 		sata_debug_rbuf->dmsgp = dmsg_alloc;
19604 		return (sata_debug_rbuf->dmsgp);
19605 	}
19606 }
19607 
19608 
19609 /*
19610  * Free all messages on debug ring buffer.
19611  */
19612 static void
19613 sata_trace_dmsg_free(void)
19614 {
19615 	sata_trace_dmsg_t *dmsg_next, *dmsg = sata_debug_rbuf->dmsgh;
19616 
19617 	while (dmsg != NULL) {
19618 		dmsg_next = dmsg->next;
19619 		kmem_free(dmsg, sizeof (sata_trace_dmsg_t));
19620 
19621 		/*
19622 		 * If we've looped around the ring than we're done.
19623 		 */
19624 		if (dmsg_next == sata_debug_rbuf->dmsgh) {
19625 			break;
19626 		} else {
19627 			dmsg = dmsg_next;
19628 		}
19629 	}
19630 }
19631 
19632 
19633 /*
19634  * This function can block
19635  */
19636 static void
19637 sata_trace_rbuf_alloc(void)
19638 {
19639 	sata_debug_rbuf = kmem_zalloc(sizeof (sata_trace_rbuf_t), KM_SLEEP);
19640 
19641 	mutex_init(&sata_debug_rbuf->lock, NULL, MUTEX_DRIVER, NULL);
19642 
19643 	if (dmsg_ring_size > 0) {
19644 		sata_debug_rbuf->maxsize = (size_t)dmsg_ring_size;
19645 	}
19646 }
19647 
19648 
19649 static void
19650 sata_trace_rbuf_free(void)
19651 {
19652 	sata_trace_dmsg_free();
19653 	mutex_destroy(&sata_debug_rbuf->lock);
19654 	kmem_free(sata_debug_rbuf, sizeof (sata_trace_rbuf_t));
19655 }
19656 
19657 /*
19658  * If SATA_DEBUG is not defined then this routine is called instead
19659  * of sata_log() via the SATA_LOG_D macro.
19660  */
19661 static void
19662 sata_trace_log(sata_hba_inst_t *sata_hba_inst, uint_t level,
19663     const char *fmt, ...)
19664 {
19665 #ifndef __lock_lint
19666 	_NOTE(ARGUNUSED(level))
19667 #endif
19668 
19669 	dev_info_t *dip = NULL;
19670 	va_list ap;
19671 
19672 	if (sata_hba_inst != NULL) {
19673 		dip = SATA_DIP(sata_hba_inst);
19674 	}
19675 
19676 	va_start(ap, fmt);
19677 	sata_vtrace_debug(dip, fmt, ap);
19678 	va_end(ap);
19679 }
19680