xref: /titanic_50/usr/src/uts/common/io/sata/impl/sata.c (revision ff3124eff995e6cd8ebd8c6543648e0670920034)
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 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * SATA Framework
31  * Generic SATA Host Adapter Implementation
32  */
33 
34 #include <sys/conf.h>
35 #include <sys/file.h>
36 #include <sys/ddi.h>
37 #include <sys/sunddi.h>
38 #include <sys/modctl.h>
39 #include <sys/cmn_err.h>
40 #include <sys/errno.h>
41 #include <sys/thread.h>
42 #include <sys/kstat.h>
43 #include <sys/note.h>
44 #include <sys/sysevent.h>
45 #include <sys/sysevent/eventdefs.h>
46 #include <sys/sysevent/dr.h>
47 #include <sys/taskq.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 
54 /* Debug flags - defined in sata.h */
55 int	sata_debug_flags = 0;
56 int	sata_msg = 0;
57 
58 /*
59  * Flags enabling selected SATA HBA framework functionality
60  */
61 #define	SATA_ENABLE_QUEUING		1
62 #define	SATA_ENABLE_NCQ			2
63 #define	SATA_ENABLE_PROCESS_EVENTS	4
64 int sata_func_enable =
65 	SATA_ENABLE_PROCESS_EVENTS | SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ;
66 
67 /*
68  * Global variable setting default maximum queue depth (NCQ or TCQ)
69  * Note:minimum queue depth is 1
70  */
71 int sata_max_queue_depth = SATA_MAX_QUEUE_DEPTH; /* max NCQ/TCQ queue depth */
72 
73 /*
74  * Currently used default NCQ/TCQ queue depth. It is set-up during the driver
75  * initialization, using value from sata_max_queue_depth
76  * It is adjusted to minimum supported by the controller and by the device,
77  * if queueing is enabled.
78  */
79 static	int sata_current_max_qdepth;
80 
81 /*
82  * Global variable determining the default behavior after device hotpluggin.
83  * If non-zero, the hotplugged device is onlined (if possible) without explicit
84  * IOCTL request (AP_CONFIGURE).
85  * If zero, hotplugged device is identified, but not onlined.
86  * Enabling (AP_CONNECT) device port with an attached device does not result
87  * in device onlining regardless of the flag setting
88  */
89 int sata_auto_online = 0;
90 
91 #ifdef SATA_DEBUG
92 
93 #define	SATA_LOG_D(args)	sata_log args
94 uint64_t mbuf_count = 0;
95 uint64_t mbuffail_count = 0;
96 
97 sata_atapi_cmd_t sata_atapi_trace[64];
98 uint32_t sata_atapi_trace_index = 0;
99 int sata_atapi_trace_save = 1;
100 static	void sata_save_atapi_trace(sata_pkt_txlate_t *, int);
101 #define	SATAATAPITRACE(spx, count)	if (sata_atapi_trace_save) \
102     sata_save_atapi_trace(spx, count);
103 
104 #else
105 #define	SATA_LOG_D(arg)
106 #define	SATAATAPITRACE(spx, count)
107 #endif
108 
109 #if 0
110 static void
111 sata_test_atapi_packet_command(sata_hba_inst_t *, int);
112 #endif
113 
114 #ifdef SATA_INJECT_FAULTS
115 
116 #define		SATA_INJECT_PKT_FAULT	1
117 uint32_t	sata_inject_fault = 0;
118 
119 uint32_t	sata_fault_cmd = 0;
120 uint32_t	sata_inject_fault_type = 0;
121 uint32_t	sata_inject_fault_count = 0;
122 uint32_t	sata_inject_fault_pause_count = 0;
123 
124 static	void sata_inject_pkt_fault(sata_pkt_t *, uint8_t, int *, int);
125 
126 #endif
127 
128 #define	LEGACY_HWID_LEN	64	/* Model (40) + Serial (20) + pad */
129 
130 /*
131  * SATA cb_ops functions
132  */
133 static 	int sata_hba_open(dev_t *, int, int, cred_t *);
134 static 	int sata_hba_close(dev_t, int, int, cred_t *);
135 static 	int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *,	int *);
136 
137 /*
138  * SCSA required entry points
139  */
140 static	int sata_scsi_tgt_init(dev_info_t *, dev_info_t *,
141     scsi_hba_tran_t *, struct scsi_device *);
142 static	int sata_scsi_tgt_probe(struct scsi_device *,
143     int (*callback)(void));
144 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *,
145     scsi_hba_tran_t *, struct scsi_device *);
146 static 	int sata_scsi_start(struct scsi_address *, struct scsi_pkt *);
147 static 	int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *);
148 static 	int sata_scsi_reset(struct scsi_address *, int);
149 static 	int sata_scsi_getcap(struct scsi_address *, char *, int);
150 static 	int sata_scsi_setcap(struct scsi_address *, char *, int, int);
151 static 	struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *,
152     struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t),
153     caddr_t);
154 static 	void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *);
155 static 	void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *);
156 static 	void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *);
157 
158 /*
159  * SATA HBA interface functions are defined in sata_hba.h header file
160  */
161 
162 /* Event processing functions */
163 static	void sata_event_daemon(void *);
164 static	void sata_event_thread_control(int);
165 static	void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst);
166 static	void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *);
167 static	void sata_process_port_failed_event(sata_hba_inst_t *,
168     sata_address_t *);
169 static	void sata_process_port_link_events(sata_hba_inst_t *,
170     sata_address_t *);
171 static	void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *);
172 static	void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *);
173 static	void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *);
174 static	void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *);
175 static	void sata_process_target_node_cleanup(sata_hba_inst_t *,
176     sata_address_t *);
177 static	void sata_process_device_autoonline(sata_hba_inst_t *,
178     sata_address_t *saddr);
179 
180 /*
181  * Local translation functions
182  */
183 static	int sata_txlt_inquiry(sata_pkt_txlate_t *);
184 static	int sata_txlt_test_unit_ready(sata_pkt_txlate_t *);
185 static	int sata_txlt_start_stop_unit(sata_pkt_txlate_t *);
186 static	int sata_txlt_read_capacity(sata_pkt_txlate_t *);
187 static	int sata_txlt_request_sense(sata_pkt_txlate_t *);
188 static 	int sata_txlt_read(sata_pkt_txlate_t *);
189 static 	int sata_txlt_write(sata_pkt_txlate_t *);
190 static 	int sata_txlt_log_sense(sata_pkt_txlate_t *);
191 static 	int sata_txlt_log_select(sata_pkt_txlate_t *);
192 static 	int sata_txlt_mode_sense(sata_pkt_txlate_t *);
193 static 	int sata_txlt_mode_select(sata_pkt_txlate_t *);
194 static 	int sata_txlt_synchronize_cache(sata_pkt_txlate_t *);
195 static 	int sata_txlt_write_buffer(sata_pkt_txlate_t *);
196 static 	int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *);
197 
198 static 	int sata_hba_start(sata_pkt_txlate_t *, int *);
199 static	int sata_txlt_invalid_command(sata_pkt_txlate_t *);
200 static	int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *);
201 static 	void sata_txlt_rw_completion(sata_pkt_t *);
202 static 	void sata_txlt_nodata_cmd_completion(sata_pkt_t *);
203 static 	void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *);
204 static 	int sata_emul_rw_completion(sata_pkt_txlate_t *);
205 static 	struct scsi_extended_sense *sata_immediate_error_response(
206     sata_pkt_txlate_t *, int);
207 static	struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *);
208 
209 static 	int sata_txlt_atapi(sata_pkt_txlate_t *);
210 static 	void sata_txlt_atapi_completion(sata_pkt_t *);
211 
212 /*
213  * Local functions for ioctl
214  */
215 static	int32_t sata_get_port_num(sata_hba_inst_t *,  struct devctl_iocdata *);
216 static	void sata_cfgadm_state(sata_hba_inst_t *, int32_t,
217     devctl_ap_state_t *);
218 static	dev_info_t *sata_get_target_dip(dev_info_t *, int32_t);
219 static	dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *);
220 static	dev_info_t *sata_devt_to_devinfo(dev_t);
221 static	int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *);
222 static	int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *);
223 static	int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *);
224 static	int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *);
225 static	int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *);
226 static	int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *);
227 static	int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *);
228 static	int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *);
229 static	int sata_ioctl_reset_all(sata_hba_inst_t *);
230 static	int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *);
231 static	int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *,
232     sata_ioctl_data_t *, int mode);
233 static	int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *,
234     sata_ioctl_data_t *, int mode);
235 static	int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *,
236     sata_ioctl_data_t *, int mode);
237 static	int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *,
238     sata_ioctl_data_t *, int mode);
239 static	int sata_ioctl_get_serialnumber_info(sata_hba_inst_t *,
240     sata_device_t *, sata_ioctl_data_t *, int mode);
241 
242 /*
243  * Local functions
244  */
245 static 	void sata_remove_hba_instance(dev_info_t *);
246 static 	int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *);
247 static 	void sata_probe_ports(sata_hba_inst_t *);
248 static 	int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int);
249 static 	int sata_add_device(dev_info_t *, sata_hba_inst_t *, int cport,
250     int pmport);
251 static 	dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *,
252     sata_address_t *);
253 static 	int sata_validate_scsi_address(sata_hba_inst_t *,
254     struct scsi_address *, sata_device_t *);
255 static 	int sata_validate_sata_address(sata_hba_inst_t *, int, int, int);
256 static	sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t));
257 static	void sata_pkt_free(sata_pkt_txlate_t *);
258 static	int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t),
259     caddr_t, ddi_dma_attr_t *);
260 static	void sata_common_free_dma_rsrcs(sata_pkt_txlate_t *);
261 static	int sata_probe_device(sata_hba_inst_t *, sata_device_t *);
262 static	sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *,
263     sata_device_t *);
264 static 	int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *);
265 static	void sata_reidentify_device(sata_pkt_txlate_t *);
266 static	struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int);
267 static 	void sata_free_local_buffer(sata_pkt_txlate_t *);
268 static 	uint64_t sata_check_capacity(sata_drive_info_t *);
269 void 	sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *,
270     ddi_dma_attr_t *);
271 static 	int sata_fetch_device_identify_data(sata_hba_inst_t *,
272     sata_drive_info_t *);
273 static	void sata_update_port_info(sata_hba_inst_t *, sata_device_t *);
274 static	void sata_update_port_scr(sata_port_scr_t *, sata_device_t *);
275 static	int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *);
276 static	int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int);
277 static	int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int);
278 static	int sata_set_drive_features(sata_hba_inst_t *,
279     sata_drive_info_t *, int flag);
280 static	void sata_init_write_cache_mode(sata_drive_info_t *sdinfo);
281 static	int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *);
282 static	void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *,
283     uint8_t *);
284 static	int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *,
285     struct scsi_inquiry *);
286 static	int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *);
287 static	int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *);
288 static	int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *);
289 static	int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *);
290 static	int sata_mode_select_page_8(sata_pkt_txlate_t *,
291     struct mode_cache_scsi3 *, int, int *, int *, int *);
292 static	int sata_mode_select_page_1c(sata_pkt_txlate_t *,
293     struct mode_info_excpt_page *, int, int *, int *, int *);
294 static	int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *);
295 static	int sata_mode_select_page_30(sata_pkt_txlate_t *,
296     struct mode_acoustic_management *, int, int *, int *, int *);
297 
298 static	int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *);
299 static	int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *,
300     sata_hba_inst_t *);
301 static	int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *,
302     sata_hba_inst_t *);
303 static	int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *,
304     sata_hba_inst_t *);
305 static	void sata_save_drive_settings(sata_drive_info_t *);
306 static	void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *);
307 static	void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...);
308 static	int sata_fetch_smart_return_status(sata_hba_inst_t *,
309     sata_drive_info_t *);
310 static	int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *,
311     struct smart_data *);
312 static	int sata_smart_selftest_log(sata_hba_inst_t *,
313     sata_drive_info_t *,
314     struct smart_selftest_log *);
315 static	int sata_ext_smart_selftest_read_log(sata_hba_inst_t *,
316     sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t);
317 static	int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *,
318     uint8_t *, uint8_t, uint8_t);
319 static	int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *,
320     struct read_log_ext_directory *);
321 static	void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int);
322 static	void sata_xlate_errors(sata_pkt_txlate_t *);
323 static	void sata_decode_device_error(sata_pkt_txlate_t *,
324     struct scsi_extended_sense *);
325 static	void sata_set_device_removed(dev_info_t *);
326 static	boolean_t sata_check_device_removed(dev_info_t *);
327 static	void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *);
328 static	int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *,
329     sata_drive_info_t *);
330 static	int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *,
331     sata_drive_info_t *);
332 static	void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *);
333 static	void sata_fixed_sense_data_preset(struct scsi_extended_sense *);
334 static  void sata_target_devid_register(dev_info_t *, sata_drive_info_t *);
335 static  int sata_check_modser(char *, int);
336 
337 
338 
339 /*
340  * SATA Framework will ignore SATA HBA driver cb_ops structure and
341  * register following one with SCSA framework.
342  * Open & close are provided, so scsi framework will not use its own
343  */
344 static struct cb_ops sata_cb_ops = {
345 	sata_hba_open,			/* open */
346 	sata_hba_close,			/* close */
347 	nodev,				/* strategy */
348 	nodev,				/* print */
349 	nodev,				/* dump */
350 	nodev,				/* read */
351 	nodev,				/* write */
352 	sata_hba_ioctl,			/* ioctl */
353 	nodev,				/* devmap */
354 	nodev,				/* mmap */
355 	nodev,				/* segmap */
356 	nochpoll,			/* chpoll */
357 	ddi_prop_op,			/* cb_prop_op */
358 	0,				/* streamtab */
359 	D_NEW | D_MP,			/* cb_flag */
360 	CB_REV,				/* rev */
361 	nodev,				/* aread */
362 	nodev				/* awrite */
363 };
364 
365 
366 extern struct mod_ops mod_miscops;
367 extern uchar_t	scsi_cdb_size[];
368 
369 static struct modlmisc modlmisc = {
370 	&mod_miscops,			/* Type of module */
371 	"SATA Module v%I%"		/* module name */
372 };
373 
374 
375 static struct modlinkage modlinkage = {
376 	MODREV_1,
377 	(void *)&modlmisc,
378 	NULL
379 };
380 
381 /*
382  * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero,
383  * i.e. when scsi_pkt has not timeout specified.
384  */
385 static int sata_default_pkt_time = 60;	/* 60 seconds */
386 
387 /*
388  * Intermediate buffer device access attributes - they are required,
389  * but not necessarily used.
390  */
391 static ddi_device_acc_attr_t sata_acc_attr = {
392 	DDI_DEVICE_ATTR_V0,
393 	DDI_STRUCTURE_LE_ACC,
394 	DDI_STRICTORDER_ACC
395 };
396 
397 
398 /*
399  * Mutexes protecting structures in multithreaded operations.
400  * Because events are relatively rare, a single global mutex protecting
401  * data structures should be sufficient. To increase performance, add
402  * separate mutex per each sata port and use global mutex only to protect
403  * common data structures.
404  */
405 static	kmutex_t sata_mutex;		/* protects sata_hba_list */
406 static	kmutex_t sata_log_mutex;	/* protects log */
407 
408 static 	char sata_log_buf[256];
409 
410 /* Default write cache setting for SATA hard disks */
411 int	sata_write_cache = 1;		/* enabled */
412 
413 /* Default write cache setting for SATA ATAPI CD/DVD */
414 int 	sata_atapicdvd_write_cache = 1; /* enabled */
415 
416 /*
417  * Linked list of HBA instances
418  */
419 static 	sata_hba_inst_t *sata_hba_list = NULL;
420 static 	sata_hba_inst_t *sata_hba_list_tail = NULL;
421 /*
422  * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran
423  * structure and in sata soft state.
424  */
425 
426 /*
427  * Event daemon related variables
428  */
429 static 	kmutex_t sata_event_mutex;
430 static 	kcondvar_t sata_event_cv;
431 static 	kthread_t *sata_event_thread = NULL;
432 static 	int sata_event_thread_terminate = 0;
433 static 	int sata_event_pending = 0;
434 static 	int sata_event_thread_active = 0;
435 extern 	pri_t minclsyspri;
436 
437 /*
438  * NCQ error recovery command
439  */
440 static const sata_cmd_t sata_rle_cmd = {
441 	SATA_CMD_REV,
442 	NULL,
443 	{
444 		SATA_DIR_READ
445 	},
446 	ATA_ADDR_LBA48,
447 	0,
448 	0,
449 	0,
450 	0,
451 	0,
452 	1,
453 	READ_LOG_EXT_NCQ_ERROR_RECOVERY,
454 	0,
455 	0,
456 	0,
457 	SATAC_READ_LOG_EXT,
458 	0,
459 	0,
460 	0,
461 };
462 
463 /*
464  * ATAPI error recovery CDB
465  */
466 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = {
467 	SCMD_REQUEST_SENSE,
468 	0,			/* Only fixed RQ format is supported */
469 	0,
470 	0,
471 	SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */
472 	0
473 };
474 
475 
476 /* Warlock directives */
477 
478 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran))
479 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device))
480 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops))
481 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense))
482 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status))
483 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr))
484 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t))
485 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state))
486 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state))
487 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list))
488 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list))
489 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next))
490 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev))
491 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \
492     sata_hba_inst::satahba_scsi_tran))
493 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran))
494 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip))
495 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached))
496 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port))
497 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex,
498     sata_hba_inst::satahba_event_flags))
499 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
500     sata_cport_info::cport_devp))
501 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp))
502 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr))
503 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
504     sata_cport_info::cport_dev_type))
505 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type))
506 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
507     sata_cport_info::cport_state))
508 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state))
509 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
510     sata_pmport_info::pmport_state))
511 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state))
512 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type))
513 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive))
514 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port))
515 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports))
516 #ifdef SATA_DEBUG
517 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count))
518 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count))
519 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace))
520 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index))
521 #endif
522 
523 /* End of warlock directives */
524 
525 /* ************** loadable module configuration functions ************** */
526 
527 int
528 _init()
529 {
530 	int rval;
531 
532 	mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL);
533 	mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL);
534 	mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL);
535 	cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL);
536 	if ((rval = mod_install(&modlinkage)) != 0) {
537 #ifdef SATA_DEBUG
538 		cmn_err(CE_WARN, "sata: _init: mod_install failed\n");
539 #endif
540 		mutex_destroy(&sata_log_mutex);
541 		cv_destroy(&sata_event_cv);
542 		mutex_destroy(&sata_event_mutex);
543 		mutex_destroy(&sata_mutex);
544 	}
545 	return (rval);
546 }
547 
548 int
549 _fini()
550 {
551 	int rval;
552 
553 	if ((rval = mod_remove(&modlinkage)) != 0)
554 		return (rval);
555 
556 	mutex_destroy(&sata_log_mutex);
557 	cv_destroy(&sata_event_cv);
558 	mutex_destroy(&sata_event_mutex);
559 	mutex_destroy(&sata_mutex);
560 	return (rval);
561 }
562 
563 int
564 _info(struct modinfo *modinfop)
565 {
566 	return (mod_info(&modlinkage, modinfop));
567 }
568 
569 
570 
571 /* ********************* SATA HBA entry points ********************* */
572 
573 
574 /*
575  * Called by SATA HBA from _init().
576  * Registers HBA driver instance/sata framework pair with scsi framework, by
577  * calling scsi_hba_init().
578  *
579  * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used
580  * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver
581  * cb_ops pointer in SATA HBA driver dev_ops structure.
582  * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors.
583  *
584  * Return status of the scsi_hba_init() is returned to a calling SATA HBA
585  * driver.
586  */
587 int
588 sata_hba_init(struct modlinkage *modlp)
589 {
590 	int rval;
591 	struct dev_ops *hba_ops;
592 
593 	SATADBG1(SATA_DBG_HBA_IF, NULL,
594 	    "sata_hba_init: name %s \n",
595 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
596 	/*
597 	 * Fill-up cb_ops and dev_ops when necessary
598 	 */
599 	hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
600 	/*
601 	 * Provide pointer to SATA dev_ops
602 	 */
603 	hba_ops->devo_cb_ops = &sata_cb_ops;
604 
605 	/*
606 	 * Register SATA HBA with SCSI framework
607 	 */
608 	if ((rval = scsi_hba_init(modlp)) != 0) {
609 		SATADBG1(SATA_DBG_HBA_IF, NULL,
610 		    "sata_hba_init: scsi hba init failed\n", NULL);
611 		return (rval);
612 	}
613 
614 	return (0);
615 }
616 
617 
618 /* HBA attach stages */
619 #define	HBA_ATTACH_STAGE_SATA_HBA_INST	1
620 #define	HBA_ATTACH_STAGE_SCSI_ATTACHED	2
621 #define	HBA_ATTACH_STAGE_SETUP		4
622 #define	HBA_ATTACH_STAGE_LINKED		8
623 
624 
625 /*
626  *
627  * Called from SATA HBA driver's attach routine to attach an instance of
628  * the HBA.
629  *
630  * For DDI_ATTACH command:
631  * sata_hba_inst structure is allocated here and initialized with pointers to
632  * SATA framework implementation of required scsi tran functions.
633  * The scsi_tran's tran_hba_private field is used by SATA Framework to point
634  * to the soft structure (sata_hba_inst) allocated by SATA framework for
635  * SATA HBA instance related data.
636  * The scsi_tran's tran_hba_private field is used by SATA framework to
637  * store a pointer to per-HBA-instance of sata_hba_inst structure.
638  * The sata_hba_inst structure is cross-linked to scsi tran structure.
639  * Among other info, a pointer to sata_hba_tran structure is stored in
640  * sata_hba_inst. The sata_hba_inst structures for different HBA instances are
641  * linked together into the list, pointed to by sata_hba_list.
642  * On the first HBA instance attach the sata event thread is initialized.
643  * Attachment points are created for all SATA ports of the HBA being attached.
644  * All HBA instance's SATA ports are probed and type of plugged devices is
645  * determined. For each device of a supported type, a target node is created.
646  *
647  * DDI_SUCCESS is returned when attachment process is successful,
648  * DDI_FAILURE is returned otherwise.
649  *
650  * For DDI_RESUME command:
651  * Not implemented at this time (postponed until phase 2 of the development).
652  */
653 int
654 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran,
655     ddi_attach_cmd_t cmd)
656 {
657 	sata_hba_inst_t	*sata_hba_inst;
658 	scsi_hba_tran_t *scsi_tran = NULL;
659 	int hba_attach_state = 0;
660 	char taskq_name[MAXPATHLEN];
661 
662 	SATADBG3(SATA_DBG_HBA_IF, NULL,
663 	    "sata_hba_attach: node %s (%s%d)\n",
664 	    ddi_node_name(dip), ddi_driver_name(dip),
665 	    ddi_get_instance(dip));
666 
667 	if (cmd == DDI_RESUME) {
668 		/*
669 		 * Postponed until phase 2 of the development
670 		 */
671 		return (DDI_FAILURE);
672 	}
673 
674 	if (cmd != DDI_ATTACH) {
675 		return (DDI_FAILURE);
676 	}
677 
678 	/* cmd == DDI_ATTACH */
679 
680 	if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) {
681 		SATA_LOG_D((NULL, CE_WARN,
682 		    "sata_hba_attach: invalid sata_hba_tran"));
683 		return (DDI_FAILURE);
684 	}
685 	/*
686 	 * Allocate and initialize SCSI tran structure.
687 	 * SATA copy of tran_bus_config is provided to create port nodes.
688 	 */
689 	scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
690 	if (scsi_tran == NULL)
691 		return (DDI_FAILURE);
692 	/*
693 	 * Allocate soft structure for SATA HBA instance.
694 	 * There is a separate softstate for each HBA instance.
695 	 */
696 	sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP);
697 	ASSERT(sata_hba_inst != NULL); /* this should not fail */
698 	mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL);
699 	hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST;
700 
701 	/*
702 	 * scsi_trans's tran_hba_private is used by SATA Framework to point to
703 	 * soft structure allocated by SATA framework for
704 	 * SATA HBA instance related data.
705 	 */
706 	scsi_tran->tran_hba_private	= sata_hba_inst;
707 	scsi_tran->tran_tgt_private	= NULL;
708 
709 	scsi_tran->tran_tgt_init	= sata_scsi_tgt_init;
710 	scsi_tran->tran_tgt_probe	= sata_scsi_tgt_probe;
711 	scsi_tran->tran_tgt_free	= sata_scsi_tgt_free;
712 
713 	scsi_tran->tran_start		= sata_scsi_start;
714 	scsi_tran->tran_reset		= sata_scsi_reset;
715 	scsi_tran->tran_abort		= sata_scsi_abort;
716 	scsi_tran->tran_getcap		= sata_scsi_getcap;
717 	scsi_tran->tran_setcap		= sata_scsi_setcap;
718 	scsi_tran->tran_init_pkt	= sata_scsi_init_pkt;
719 	scsi_tran->tran_destroy_pkt	= sata_scsi_destroy_pkt;
720 
721 	scsi_tran->tran_dmafree		= sata_scsi_dmafree;
722 	scsi_tran->tran_sync_pkt	= sata_scsi_sync_pkt;
723 
724 	scsi_tran->tran_reset_notify	= NULL;
725 	scsi_tran->tran_get_bus_addr	= NULL;
726 	scsi_tran->tran_quiesce		= NULL;
727 	scsi_tran->tran_unquiesce	= NULL;
728 	scsi_tran->tran_bus_reset	= NULL;
729 
730 	if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr,
731 	    scsi_tran, 0) != DDI_SUCCESS) {
732 #ifdef SATA_DEBUG
733 		cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed",
734 		    ddi_driver_name(dip), ddi_get_instance(dip));
735 #endif
736 		goto fail;
737 	}
738 	hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED;
739 
740 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) {
741 		if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
742 		    "sata", 1) != DDI_PROP_SUCCESS) {
743 			SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: "
744 			    "failed to create hba sata prop"));
745 			goto fail;
746 		}
747 	}
748 
749 	/*
750 	 * Save pointers in hba instance soft state.
751 	 */
752 	sata_hba_inst->satahba_scsi_tran = scsi_tran;
753 	sata_hba_inst->satahba_tran = sata_tran;
754 	sata_hba_inst->satahba_dip = dip;
755 
756 	/*
757 	 * Create a task queue to handle emulated commands completion
758 	 * Use node name, dash, instance number as the queue name.
759 	 */
760 	taskq_name[0] = '\0';
761 	(void) strlcat(taskq_name, DEVI(dip)->devi_node_name,
762 	    sizeof (taskq_name));
763 	(void) snprintf(taskq_name + strlen(taskq_name),
764 	    sizeof (taskq_name) - strlen(taskq_name),
765 	    "-%d", DEVI(dip)->devi_instance);
766 	sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1,
767 	    minclsyspri, 1, sata_tran->sata_tran_hba_num_cports,
768 	    TASKQ_DYNAMIC);
769 
770 	hba_attach_state |= HBA_ATTACH_STAGE_SETUP;
771 
772 	/*
773 	 * Create events thread if not created yet.
774 	 */
775 	sata_event_thread_control(1);
776 
777 	/*
778 	 * Link this hba instance into the list.
779 	 */
780 	mutex_enter(&sata_mutex);
781 
782 	if (sata_hba_list == NULL) {
783 		/*
784 		 * The first instance of HBA is attached.
785 		 * Set current/active default maximum NCQ/TCQ queue depth for
786 		 * all SATA devices. It is done here and now, to eliminate the
787 		 * possibility of the dynamic, programatic modification of the
788 		 * queue depth via global (and public) sata_max_queue_depth
789 		 * variable (this would require special handling in HBA drivers)
790 		 */
791 		sata_current_max_qdepth = sata_max_queue_depth;
792 		if (sata_current_max_qdepth > 32)
793 			sata_current_max_qdepth = 32;
794 		else if (sata_current_max_qdepth < 1)
795 			sata_current_max_qdepth = 1;
796 	}
797 
798 	sata_hba_inst->satahba_next = NULL;
799 	sata_hba_inst->satahba_prev = sata_hba_list_tail;
800 	if (sata_hba_list == NULL) {
801 		sata_hba_list = sata_hba_inst;
802 	}
803 	if (sata_hba_list_tail != NULL) {
804 		sata_hba_list_tail->satahba_next = sata_hba_inst;
805 	}
806 	sata_hba_list_tail = sata_hba_inst;
807 	mutex_exit(&sata_mutex);
808 	hba_attach_state |= HBA_ATTACH_STAGE_LINKED;
809 
810 	/*
811 	 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl
812 	 * SATA HBA driver should not use its own open/close entry points.
813 	 *
814 	 * Make sure that instance number doesn't overflow
815 	 * when forming minor numbers.
816 	 */
817 	ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT));
818 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
819 	    INST2DEVCTL(ddi_get_instance(dip)),
820 	    DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) {
821 #ifdef SATA_DEBUG
822 		cmn_err(CE_WARN, "sata_hba_attach: "
823 		    "cannot create devctl minor node");
824 #endif
825 		goto fail;
826 	}
827 
828 
829 	/*
830 	 * Set-up kstats here, if necessary.
831 	 * (postponed until phase 2 of the development).
832 	 */
833 
834 
835 	/*
836 	 * Probe controller ports. This operation will describe a current
837 	 * controller/port/multipliers/device configuration and will create
838 	 * attachment points.
839 	 * We may end-up with just a controller with no devices attached.
840 	 * For the ports with a supported device attached, device target nodes
841 	 * are created and devices are initialized.
842 	 */
843 	sata_probe_ports(sata_hba_inst);
844 
845 	sata_hba_inst->satahba_attached = 1;
846 	return (DDI_SUCCESS);
847 
848 fail:
849 	if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) {
850 		(void) sata_remove_hba_instance(dip);
851 		if (sata_hba_list == NULL)
852 			sata_event_thread_control(0);
853 	}
854 
855 	if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) {
856 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
857 		taskq_destroy(sata_hba_inst->satahba_taskq);
858 	}
859 
860 	if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED)
861 		(void) scsi_hba_detach(dip);
862 
863 	if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) {
864 		mutex_destroy(&sata_hba_inst->satahba_mutex);
865 		kmem_free((void *)sata_hba_inst,
866 		    sizeof (struct sata_hba_inst));
867 		scsi_hba_tran_free(scsi_tran);
868 	}
869 
870 	sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed",
871 	    ddi_driver_name(dip), ddi_get_instance(dip));
872 
873 	return (DDI_FAILURE);
874 }
875 
876 
877 /*
878  * Called by SATA HBA from to detach an instance of the driver.
879  *
880  * For DDI_DETACH command:
881  * Free local structures allocated for SATA HBA instance during
882  * sata_hba_attach processing.
883  *
884  * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise.
885  *
886  * For DDI_SUSPEND command:
887  * Not implemented at this time (postponed until phase 2 of the development)
888  * Returnd DDI_SUCCESS.
889  *
890  * When the last HBA instance is detached, the event daemon is terminated.
891  *
892  * NOTE: cport support only, no port multiplier support.
893  */
894 int
895 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
896 {
897 	dev_info_t	*tdip;
898 	sata_hba_inst_t	*sata_hba_inst;
899 	scsi_hba_tran_t *scsi_hba_tran;
900 	sata_cport_info_t *cportinfo;
901 	sata_drive_info_t *sdinfo;
902 	int ncport;
903 
904 	SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n",
905 	    ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip));
906 
907 	switch (cmd) {
908 	case DDI_DETACH:
909 
910 		if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
911 			return (DDI_FAILURE);
912 
913 		sata_hba_inst = scsi_hba_tran->tran_hba_private;
914 		if (sata_hba_inst == NULL)
915 			return (DDI_FAILURE);
916 
917 		if (scsi_hba_detach(dip) == DDI_FAILURE) {
918 			sata_hba_inst->satahba_attached = 1;
919 			return (DDI_FAILURE);
920 		}
921 
922 		/*
923 		 * Free all target nodes - at this point
924 		 * devices should be at least offlined
925 		 * otherwise scsi_hba_detach() should not be called.
926 		 */
927 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
928 		    ncport++) {
929 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
930 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
931 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
932 				if (sdinfo != NULL) {
933 					tdip = sata_get_target_dip(dip,
934 					    ncport);
935 					if (tdip != NULL) {
936 						if (ndi_devi_offline(tdip,
937 						    NDI_DEVI_REMOVE) !=
938 						    NDI_SUCCESS) {
939 							SATA_LOG_D((
940 							    sata_hba_inst,
941 							    CE_WARN,
942 							    "sata_hba_detach: "
943 							    "Target node not "
944 							    "removed !"));
945 							return (DDI_FAILURE);
946 						}
947 					}
948 				}
949 			}
950 		}
951 		/*
952 		 * Disable sata event daemon processing for this HBA
953 		 */
954 		sata_hba_inst->satahba_attached = 0;
955 
956 		/*
957 		 * Remove event daemon thread, if it is last HBA instance.
958 		 */
959 
960 		mutex_enter(&sata_mutex);
961 		if (sata_hba_list->satahba_next == NULL) {
962 			mutex_exit(&sata_mutex);
963 			sata_event_thread_control(0);
964 			mutex_enter(&sata_mutex);
965 		}
966 		mutex_exit(&sata_mutex);
967 
968 		/* Remove this HBA instance from the HBA list */
969 		sata_remove_hba_instance(dip);
970 
971 		/*
972 		 * At this point there should be no target nodes attached.
973 		 * Detach and destroy device and port info structures.
974 		 */
975 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
976 		    ncport++) {
977 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
978 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
979 				sdinfo =
980 				    cportinfo->cport_devp.cport_sata_drive;
981 				if (sdinfo != NULL) {
982 					/* Release device structure */
983 					kmem_free(sdinfo,
984 					    sizeof (sata_drive_info_t));
985 				}
986 				/* Release cport info */
987 				mutex_destroy(&cportinfo->cport_mutex);
988 				kmem_free(cportinfo,
989 				    sizeof (sata_cport_info_t));
990 			}
991 		}
992 
993 		scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran);
994 
995 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
996 
997 		taskq_destroy(sata_hba_inst->satahba_taskq);
998 
999 		mutex_destroy(&sata_hba_inst->satahba_mutex);
1000 		kmem_free((void *)sata_hba_inst,
1001 		    sizeof (struct sata_hba_inst));
1002 
1003 		return (DDI_SUCCESS);
1004 
1005 	case DDI_SUSPEND:
1006 		/*
1007 		 * Postponed until phase 2
1008 		 */
1009 		return (DDI_FAILURE);
1010 
1011 	default:
1012 		return (DDI_FAILURE);
1013 	}
1014 }
1015 
1016 
1017 /*
1018  * Called by an HBA drive from _fini() routine.
1019  * Unregisters SATA HBA instance/SATA framework pair from the scsi framework.
1020  */
1021 void
1022 sata_hba_fini(struct modlinkage *modlp)
1023 {
1024 	SATADBG1(SATA_DBG_HBA_IF, NULL,
1025 	    "sata_hba_fini: name %s\n",
1026 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
1027 
1028 	scsi_hba_fini(modlp);
1029 }
1030 
1031 
1032 /*
1033  * Default open and close routine for sata_hba framework.
1034  *
1035  */
1036 /*
1037  * Open devctl node.
1038  *
1039  * Returns:
1040  * 0 if node was open successfully, error code otherwise.
1041  *
1042  *
1043  */
1044 
1045 static int
1046 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1047 {
1048 #ifndef __lock_lint
1049 	_NOTE(ARGUNUSED(credp))
1050 #endif
1051 	int rv = 0;
1052 	dev_info_t *dip;
1053 	scsi_hba_tran_t *scsi_hba_tran;
1054 	sata_hba_inst_t	*sata_hba_inst;
1055 
1056 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL);
1057 
1058 	if (otyp != OTYP_CHR)
1059 		return (EINVAL);
1060 
1061 	dip = sata_devt_to_devinfo(*devp);
1062 	if (dip == NULL)
1063 		return (ENXIO);
1064 
1065 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1066 		return (ENXIO);
1067 
1068 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1069 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1070 		return (ENXIO);
1071 
1072 	mutex_enter(&sata_mutex);
1073 	if (flags & FEXCL) {
1074 		if (sata_hba_inst->satahba_open_flag != 0) {
1075 			rv = EBUSY;
1076 		} else {
1077 			sata_hba_inst->satahba_open_flag =
1078 			    SATA_DEVCTL_EXOPENED;
1079 		}
1080 	} else {
1081 		if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) {
1082 			rv = EBUSY;
1083 		} else {
1084 			sata_hba_inst->satahba_open_flag =
1085 			    SATA_DEVCTL_SOPENED;
1086 		}
1087 	}
1088 	mutex_exit(&sata_mutex);
1089 
1090 	return (rv);
1091 }
1092 
1093 
1094 /*
1095  * Close devctl node.
1096  * Returns:
1097  * 0 if node was closed successfully, error code otherwise.
1098  *
1099  */
1100 
1101 static int
1102 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
1103 {
1104 #ifndef __lock_lint
1105 	_NOTE(ARGUNUSED(credp))
1106 	_NOTE(ARGUNUSED(flag))
1107 #endif
1108 	dev_info_t *dip;
1109 	scsi_hba_tran_t *scsi_hba_tran;
1110 	sata_hba_inst_t	*sata_hba_inst;
1111 
1112 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL);
1113 
1114 	if (otyp != OTYP_CHR)
1115 		return (EINVAL);
1116 
1117 	dip = sata_devt_to_devinfo(dev);
1118 	if (dip == NULL)
1119 		return (ENXIO);
1120 
1121 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1122 		return (ENXIO);
1123 
1124 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1125 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1126 		return (ENXIO);
1127 
1128 	mutex_enter(&sata_mutex);
1129 	sata_hba_inst->satahba_open_flag = 0;
1130 	mutex_exit(&sata_mutex);
1131 	return (0);
1132 }
1133 
1134 
1135 
1136 /*
1137  * Standard IOCTL commands for SATA hotplugging.
1138  * Implemented DEVCTL_AP commands:
1139  * DEVCTL_AP_CONNECT
1140  * DEVCTL_AP_DISCONNECT
1141  * DEVCTL_AP_CONFIGURE
1142  * DEVCTL_UNCONFIGURE
1143  * DEVCTL_AP_CONTROL
1144  *
1145  * Commands passed to default ndi ioctl handler:
1146  * DEVCTL_DEVICE_GETSTATE
1147  * DEVCTL_DEVICE_ONLINE
1148  * DEVCTL_DEVICE_OFFLINE
1149  * DEVCTL_DEVICE_REMOVE
1150  * DEVCTL_DEVICE_INSERT
1151  * DEVCTL_BUS_GETSTATE
1152  *
1153  * All other cmds are passed to HBA if it provide ioctl handler, or failed
1154  * if not.
1155  *
1156  * Returns:
1157  * 0 if successful,
1158  * error code if operation failed.
1159  *
1160  * NOTE: Port Multiplier is not supported.
1161  *
1162  */
1163 
1164 static int
1165 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1166     int *rvalp)
1167 {
1168 #ifndef __lock_lint
1169 	_NOTE(ARGUNUSED(credp))
1170 	_NOTE(ARGUNUSED(rvalp))
1171 #endif
1172 	int rv = 0;
1173 	int32_t	comp_port = -1;
1174 	dev_info_t *dip;
1175 	devctl_ap_state_t ap_state;
1176 	struct devctl_iocdata *dcp = NULL;
1177 	scsi_hba_tran_t *scsi_hba_tran;
1178 	sata_hba_inst_t *sata_hba_inst;
1179 	sata_device_t sata_device;
1180 	sata_cport_info_t *cportinfo;
1181 	int cport, pmport, qual;
1182 	int rval = SATA_SUCCESS;
1183 
1184 	dip = sata_devt_to_devinfo(dev);
1185 	if (dip == NULL)
1186 		return (ENXIO);
1187 
1188 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1189 		return (ENXIO);
1190 
1191 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1192 	if (sata_hba_inst == NULL)
1193 		return (ENXIO);
1194 
1195 	if (sata_hba_inst->satahba_tran == NULL)
1196 		return (ENXIO);
1197 
1198 	switch (cmd) {
1199 
1200 	case DEVCTL_DEVICE_GETSTATE:
1201 	case DEVCTL_DEVICE_ONLINE:
1202 	case DEVCTL_DEVICE_OFFLINE:
1203 	case DEVCTL_DEVICE_REMOVE:
1204 	case DEVCTL_BUS_GETSTATE:
1205 		/*
1206 		 * There may be more cases that we want to pass to default
1207 		 * handler rather than fail them.
1208 		 */
1209 		return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
1210 	}
1211 
1212 	/* read devctl ioctl data */
1213 	if (cmd != DEVCTL_AP_CONTROL) {
1214 		if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1215 			return (EFAULT);
1216 
1217 		if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) ==
1218 		    -1) {
1219 			if (dcp)
1220 				ndi_dc_freehdl(dcp);
1221 			return (EINVAL);
1222 		}
1223 
1224 		cport = SCSI_TO_SATA_CPORT(comp_port);
1225 		pmport = SCSI_TO_SATA_PMPORT(comp_port);
1226 		/* Only cport is considered now, i.e. SATA_ADDR_CPORT */
1227 		qual = SATA_ADDR_CPORT;
1228 		if (sata_validate_sata_address(sata_hba_inst, cport, pmport,
1229 		    qual) != 0) {
1230 			ndi_dc_freehdl(dcp);
1231 			return (EINVAL);
1232 		}
1233 
1234 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1235 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1236 		    cport_mutex);
1237 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1238 			/*
1239 			 * Cannot process ioctl request now. Come back later.
1240 			 */
1241 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1242 			    cport_mutex);
1243 			ndi_dc_freehdl(dcp);
1244 			return (EBUSY);
1245 		}
1246 		/* Block event processing for this port */
1247 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1248 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1249 
1250 		sata_device.satadev_addr.cport = cport;
1251 		sata_device.satadev_addr.pmport = pmport;
1252 		sata_device.satadev_addr.qual = qual;
1253 		sata_device.satadev_rev = SATA_DEVICE_REV;
1254 	}
1255 
1256 	switch (cmd) {
1257 
1258 	case DEVCTL_AP_DISCONNECT:
1259 
1260 		/*
1261 		 * Normally, cfgadm sata plugin will try to offline
1262 		 * (unconfigure) device before this request. Nevertheless,
1263 		 * if a device is still configured, we need to
1264 		 * attempt to offline and unconfigure device first, and we will
1265 		 * deactivate the port regardless of the unconfigure
1266 		 * operation results.
1267 		 *
1268 		 */
1269 		rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device);
1270 
1271 		break;
1272 
1273 	case DEVCTL_AP_UNCONFIGURE:
1274 
1275 		/*
1276 		 * The unconfigure operation uses generic nexus operation to
1277 		 * offline a device. It leaves a target device node attached.
1278 		 * and obviously sata_drive_info attached as well, because
1279 		 * from the hardware point of view nothing has changed.
1280 		 */
1281 		rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device);
1282 		break;
1283 
1284 	case DEVCTL_AP_CONNECT:
1285 	{
1286 		/*
1287 		 * The sata cfgadm pluging will invoke this operation only if
1288 		 * port was found in the disconnect state (failed state
1289 		 * is also treated as the disconnected state).
1290 		 * If port activation is successful and a device is found
1291 		 * attached to the port, the initialization sequence is
1292 		 * executed to probe the port and attach
1293 		 * a device structure to a port structure. The device is not
1294 		 * set in configured state (system-wise) by this operation.
1295 		 */
1296 
1297 		rv = sata_ioctl_connect(sata_hba_inst, &sata_device);
1298 
1299 		break;
1300 	}
1301 
1302 	case DEVCTL_AP_CONFIGURE:
1303 	{
1304 		/*
1305 		 * A port may be in an active or shutdown state.
1306 		 * If port is in a failed state, operation is aborted.
1307 		 * If a port is in a shutdown state, sata_tran_port_activate()
1308 		 * is invoked prior to any other operation.
1309 		 *
1310 		 * Onlining the device involves creating a new target node.
1311 		 * If there is an old target node present (belonging to
1312 		 * previously removed device), the operation is aborted - the
1313 		 * old node has to be released and removed before configure
1314 		 * operation is attempted.
1315 		 */
1316 
1317 		rv = sata_ioctl_configure(sata_hba_inst, &sata_device);
1318 
1319 		break;
1320 	}
1321 
1322 	case DEVCTL_AP_GETSTATE:
1323 
1324 		sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state);
1325 
1326 		ap_state.ap_last_change = (time_t)-1;
1327 		ap_state.ap_error_code = 0;
1328 		ap_state.ap_in_transition = 0;
1329 
1330 		/* Copy the return AP-state information to the user space */
1331 		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
1332 			rv = EFAULT;
1333 		}
1334 		break;
1335 
1336 	case DEVCTL_AP_CONTROL:
1337 	{
1338 		/*
1339 		 * Generic devctl for hardware specific functionality
1340 		 */
1341 		sata_ioctl_data_t	ioc;
1342 
1343 		ASSERT(dcp == NULL);
1344 
1345 		/* Copy in user ioctl data first */
1346 #ifdef _MULTI_DATAMODEL
1347 		if (ddi_model_convert_from(mode & FMODELS) ==
1348 		    DDI_MODEL_ILP32) {
1349 
1350 			sata_ioctl_data_32_t	ioc32;
1351 
1352 			if (ddi_copyin((void *)arg, (void *)&ioc32,
1353 			    sizeof (ioc32), mode) != 0) {
1354 				rv = EFAULT;
1355 				break;
1356 			}
1357 			ioc.cmd 	= (uint_t)ioc32.cmd;
1358 			ioc.port	= (uint_t)ioc32.port;
1359 			ioc.get_size	= (uint_t)ioc32.get_size;
1360 			ioc.buf		= (caddr_t)(uintptr_t)ioc32.buf;
1361 			ioc.bufsiz	= (uint_t)ioc32.bufsiz;
1362 			ioc.misc_arg	= (uint_t)ioc32.misc_arg;
1363 		} else
1364 #endif /* _MULTI_DATAMODEL */
1365 		if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
1366 		    mode) != 0) {
1367 			return (EFAULT);
1368 		}
1369 
1370 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
1371 		    "sata_hba_ioctl: DEVCTL_AP_CONTROL "
1372 		    "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port);
1373 
1374 		/*
1375 		 * To avoid BE/LE and 32/64 issues, a get_size always returns
1376 		 * a 32-bit number.
1377 		 */
1378 		if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
1379 			return (EINVAL);
1380 		}
1381 		/* validate address */
1382 		cport = SCSI_TO_SATA_CPORT(ioc.port);
1383 		pmport = SCSI_TO_SATA_PMPORT(ioc.port);
1384 		qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port);
1385 
1386 		/* Override address qualifier - handle cport only for now */
1387 		qual = SATA_ADDR_CPORT;
1388 
1389 		if (sata_validate_sata_address(sata_hba_inst, cport,
1390 		    pmport, qual) != 0)
1391 			return (EINVAL);
1392 
1393 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1394 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1395 		    cport_mutex);
1396 		/* Is the port locked by event processing daemon ? */
1397 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1398 			/*
1399 			 * Cannot process ioctl request now. Come back later
1400 			 */
1401 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1402 			    cport_mutex);
1403 			return (EBUSY);
1404 		}
1405 		/* Block event processing for this port */
1406 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1407 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1408 
1409 
1410 		sata_device.satadev_addr.cport = cport;
1411 		sata_device.satadev_addr.pmport = pmport;
1412 		sata_device.satadev_addr.qual = qual;
1413 		sata_device.satadev_rev = SATA_DEVICE_REV;
1414 
1415 		switch (ioc.cmd) {
1416 
1417 		case SATA_CFGA_RESET_PORT:
1418 			/*
1419 			 * There is no protection for configured device.
1420 			 */
1421 			rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device);
1422 			break;
1423 
1424 		case SATA_CFGA_RESET_DEVICE:
1425 			/*
1426 			 * There is no protection for configured device.
1427 			 */
1428 			rv = sata_ioctl_reset_device(sata_hba_inst,
1429 			    &sata_device);
1430 			break;
1431 
1432 		case SATA_CFGA_RESET_ALL:
1433 			/*
1434 			 * There is no protection for configured devices.
1435 			 */
1436 			rv = sata_ioctl_reset_all(sata_hba_inst);
1437 			/*
1438 			 * We return here, because common return is for
1439 			 * a single port operation - we have already unlocked
1440 			 * all ports and no dc handle was allocated.
1441 			 */
1442 			return (rv);
1443 
1444 		case SATA_CFGA_PORT_DEACTIVATE:
1445 			/*
1446 			 * Arbitrarily unconfigure attached device, if any.
1447 			 * Even if the unconfigure fails, proceed with the
1448 			 * port deactivation.
1449 			 */
1450 			rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device);
1451 
1452 			break;
1453 
1454 		case SATA_CFGA_PORT_ACTIVATE:
1455 
1456 			rv = sata_ioctl_activate(sata_hba_inst, &sata_device);
1457 			break;
1458 
1459 		case SATA_CFGA_PORT_SELF_TEST:
1460 
1461 			rv = sata_ioctl_port_self_test(sata_hba_inst,
1462 			    &sata_device);
1463 			break;
1464 
1465 		case SATA_CFGA_GET_DEVICE_PATH:
1466 			if (qual == SATA_ADDR_CPORT)
1467 				sata_device.satadev_addr.qual =
1468 				    SATA_ADDR_DCPORT;
1469 			else
1470 				sata_device.satadev_addr.qual =
1471 				    SATA_ADDR_DPMPORT;
1472 			rv = sata_ioctl_get_device_path(sata_hba_inst,
1473 			    &sata_device, &ioc, mode);
1474 			break;
1475 
1476 		case SATA_CFGA_GET_AP_TYPE:
1477 
1478 			rv = sata_ioctl_get_ap_type(sata_hba_inst,
1479 			    &sata_device, &ioc, mode);
1480 			break;
1481 
1482 		case SATA_CFGA_GET_MODEL_INFO:
1483 
1484 			rv = sata_ioctl_get_model_info(sata_hba_inst,
1485 			    &sata_device, &ioc, mode);
1486 			break;
1487 
1488 		case SATA_CFGA_GET_REVFIRMWARE_INFO:
1489 
1490 			rv = sata_ioctl_get_revfirmware_info(sata_hba_inst,
1491 			    &sata_device, &ioc, mode);
1492 			break;
1493 
1494 		case SATA_CFGA_GET_SERIALNUMBER_INFO:
1495 
1496 			rv = sata_ioctl_get_serialnumber_info(sata_hba_inst,
1497 			    &sata_device, &ioc, mode);
1498 			break;
1499 
1500 		default:
1501 			rv = EINVAL;
1502 			break;
1503 
1504 		} /* End of DEVCTL_AP_CONTROL cmd switch */
1505 
1506 		break;
1507 	}
1508 
1509 	default:
1510 	{
1511 		/*
1512 		 * If we got here, we got an IOCTL that SATA HBA Framework
1513 		 * does not recognize. Pass ioctl to HBA driver, in case
1514 		 * it could process it.
1515 		 */
1516 		sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran;
1517 		dev_info_t	*mydip = SATA_DIP(sata_hba_inst);
1518 
1519 		SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1520 		    "IOCTL 0x%2x not supported in SATA framework, "
1521 		    "passthrough to HBA", cmd);
1522 
1523 		if (sata_tran->sata_tran_ioctl == NULL) {
1524 			rv = EINVAL;
1525 			break;
1526 		}
1527 		rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg);
1528 		if (rval != 0) {
1529 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1530 			    "IOCTL 0x%2x failed in HBA", cmd);
1531 			rv = rval;
1532 		}
1533 		break;
1534 	}
1535 
1536 	} /* End of main IOCTL switch */
1537 
1538 	if (dcp) {
1539 		ndi_dc_freehdl(dcp);
1540 	}
1541 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1542 	cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
1543 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1544 
1545 	return (rv);
1546 }
1547 
1548 
1549 /*
1550  * Create error retrieval sata packet
1551  *
1552  * A sata packet is allocated and set-up to contain specified error retrieval
1553  * command and appropriate dma-able data buffer.
1554  * No association with any scsi packet is made and no callback routine is
1555  * specified.
1556  *
1557  * Returns a pointer to sata packet upon successfull packet creation.
1558  * Returns NULL, if packet cannot be created.
1559  */
1560 sata_pkt_t *
1561 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device,
1562     int pkt_type)
1563 {
1564 	sata_hba_inst_t	*sata_hba_inst;
1565 	sata_pkt_txlate_t *spx;
1566 	sata_pkt_t *spkt;
1567 	sata_drive_info_t *sdinfo;
1568 
1569 	mutex_enter(&sata_mutex);
1570 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1571 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1572 		if (SATA_DIP(sata_hba_inst) == dip)
1573 			break;
1574 	}
1575 	mutex_exit(&sata_mutex);
1576 	ASSERT(sata_hba_inst != NULL);
1577 
1578 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
1579 	if (sdinfo == NULL) {
1580 		sata_log(sata_hba_inst, CE_WARN,
1581 		    "sata: error recovery request for non-attached device at "
1582 		    "cport %d", sata_device->satadev_addr.cport);
1583 		return (NULL);
1584 	}
1585 
1586 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1587 	spx->txlt_sata_hba_inst = sata_hba_inst;
1588 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
1589 	spkt = sata_pkt_alloc(spx, NULL);
1590 	if (spkt == NULL) {
1591 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1592 		return (NULL);
1593 	}
1594 	/* address is needed now */
1595 	spkt->satapkt_device.satadev_addr = sata_device->satadev_addr;
1596 
1597 	switch (pkt_type) {
1598 	case SATA_ERR_RETR_PKT_TYPE_NCQ:
1599 		if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS)
1600 			return (spkt);
1601 		break;
1602 
1603 	case SATA_ERR_RETR_PKT_TYPE_ATAPI:
1604 		if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS)
1605 			return (spkt);
1606 		break;
1607 
1608 	default:
1609 		break;
1610 	}
1611 
1612 	sata_pkt_free(spx);
1613 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1614 	return (NULL);
1615 
1616 }
1617 
1618 
1619 /*
1620  * Free error retrieval sata packet
1621  *
1622  * Free sata packet and any associated resources allocated previously by
1623  * sata_get_error_retrieval_pkt().
1624  *
1625  * Void return.
1626  */
1627 void
1628 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt)
1629 {
1630 	sata_pkt_txlate_t *spx =
1631 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1632 
1633 	ASSERT(sata_pkt != NULL);
1634 
1635 	sata_free_local_buffer(spx);
1636 	sata_pkt_free(spx);
1637 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1638 
1639 }
1640 
1641 /*
1642  * sata_name_child is for composing the name of the node
1643  * the format of the name is "target,0".
1644  */
1645 static int
1646 sata_name_child(dev_info_t *dip, char *name, int namelen)
1647 {
1648 	int target;
1649 
1650 	target = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1651 	    DDI_PROP_DONTPASS, "target", -1);
1652 	if (target == -1)
1653 		return (DDI_FAILURE);
1654 	(void) snprintf(name, namelen, "%x,0", target);
1655 	return (DDI_SUCCESS);
1656 }
1657 
1658 
1659 
1660 /* ****************** SCSA required entry points *********************** */
1661 
1662 /*
1663  * Implementation of scsi tran_tgt_init.
1664  * sata_scsi_tgt_init() initializes scsi_device structure
1665  *
1666  * If successful, DDI_SUCCESS is returned.
1667  * DDI_FAILURE is returned if addressed device does not exist
1668  */
1669 
1670 static int
1671 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1672     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1673 {
1674 #ifndef __lock_lint
1675 	_NOTE(ARGUNUSED(hba_dip))
1676 	_NOTE(ARGUNUSED(tgt_dip))
1677 #endif
1678 	sata_device_t		sata_device;
1679 	sata_drive_info_t	*sdinfo;
1680 	struct sata_id		*sid;
1681 	sata_hba_inst_t		*sata_hba_inst;
1682 	char			model[SATA_ID_MODEL_LEN + 1];
1683 	char			fw[SATA_ID_FW_LEN + 1];
1684 	char			*vid, *pid;
1685 	int			i;
1686 
1687 	/*
1688 	 * Fail tran_tgt_init for .conf stub node
1689 	 */
1690 	if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
1691 		(void) ndi_merge_node(tgt_dip, sata_name_child);
1692 		ddi_set_name_addr(tgt_dip, NULL);
1693 		return (DDI_FAILURE);
1694 	}
1695 
1696 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
1697 
1698 	/* Validate scsi device address */
1699 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
1700 	    &sata_device) != 0)
1701 		return (DDI_FAILURE);
1702 
1703 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1704 	    sata_device.satadev_addr.cport)));
1705 
1706 	/* sata_device now contains a valid sata address */
1707 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
1708 	if (sdinfo == NULL) {
1709 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1710 		    sata_device.satadev_addr.cport)));
1711 		return (DDI_FAILURE);
1712 	}
1713 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1714 	    sata_device.satadev_addr.cport)));
1715 
1716 	/*
1717 	 * Check if we need to create a legacy devid (i.e cmdk style) for
1718 	 * the target disks.
1719 	 *
1720 	 * HBA devinfo node will have the property "use-cmdk-devid-format"
1721 	 * if we need to create cmdk-style devid for all the disk devices
1722 	 * attached to this controller. This property may have been set
1723 	 * from HBA driver's .conf file or by the HBA driver in its
1724 	 * attach(9F) function.
1725 	 */
1726 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
1727 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
1728 	    "use-cmdk-devid-format", 0) == 1)) {
1729 		/* register a legacy devid for this target node */
1730 		sata_target_devid_register(tgt_dip, sdinfo);
1731 	}
1732 
1733 
1734 	/*
1735 	 * 'Identify Device Data' does not always fit in standard SCSI
1736 	 * INQUIRY data, so establish INQUIRY_* properties with full-form
1737 	 * of information.
1738 	 */
1739 	sid = &sdinfo->satadrv_id;
1740 #ifdef	_LITTLE_ENDIAN
1741 	swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
1742 	swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
1743 #else	/* _LITTLE_ENDIAN */
1744 	bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN);
1745 	bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN);
1746 #endif	/* _LITTLE_ENDIAN */
1747 	model[SATA_ID_MODEL_LEN] = 0;
1748 	fw[SATA_ID_FW_LEN] = 0;
1749 
1750 	/* split model into into vid/pid */
1751 	for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++)
1752 		if ((*pid == ' ') || (*pid == '\t'))
1753 			break;
1754 	if (i < SATA_ID_MODEL_LEN) {
1755 		vid = model;
1756 		*pid++ = 0;		/* terminate vid, establish pid */
1757 	} else {
1758 		vid = NULL;		/* vid will stay "ATA     " */
1759 		pid = model;		/* model is all pid */
1760 	}
1761 
1762 	if (vid)
1763 		(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_VENDOR_ID,
1764 		    vid, strlen(vid));
1765 	if (pid)
1766 		(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID,
1767 		    pid, strlen(pid));
1768 	(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_REVISION_ID,
1769 	    fw, strlen(fw));
1770 
1771 	return (DDI_SUCCESS);
1772 }
1773 
1774 /*
1775  * Implementation of scsi tran_tgt_probe.
1776  * Probe target, by calling default scsi routine scsi_hba_probe()
1777  */
1778 static int
1779 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void))
1780 {
1781 	sata_hba_inst_t *sata_hba_inst =
1782 	    (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private);
1783 	int rval;
1784 
1785 	rval = scsi_hba_probe(sd, callback);
1786 
1787 	if (rval == SCSIPROBE_EXISTS) {
1788 		/*
1789 		 * Set property "pm-capable" on the target device node, so that
1790 		 * the target driver will not try to fetch scsi cycle counters
1791 		 * before enabling device power-management.
1792 		 */
1793 		if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
1794 		    "pm-capable", 1)) != DDI_PROP_SUCCESS) {
1795 			sata_log(sata_hba_inst, CE_WARN,
1796 			    "SATA device at port %d: "
1797 			    "will not be power-managed ",
1798 			    SCSI_TO_SATA_CPORT(sd->sd_address.a_target));
1799 			SATA_LOG_D((sata_hba_inst, CE_WARN,
1800 			    "failure updating pm-capable property"));
1801 		}
1802 	}
1803 	return (rval);
1804 }
1805 
1806 /*
1807  * Implementation of scsi tran_tgt_free.
1808  * Release all resources allocated for scsi_device
1809  */
1810 static void
1811 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1812     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1813 {
1814 #ifndef __lock_lint
1815 	_NOTE(ARGUNUSED(hba_dip))
1816 #endif
1817 	sata_device_t		sata_device;
1818 	sata_drive_info_t	*sdinfo;
1819 	sata_hba_inst_t		*sata_hba_inst;
1820 	ddi_devid_t		devid;
1821 
1822 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
1823 
1824 	/* Validate scsi device address */
1825 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
1826 	    &sata_device) != 0)
1827 		return;
1828 
1829 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1830 	    sata_device.satadev_addr.cport)));
1831 
1832 	/* sata_device now should contain a valid sata address */
1833 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
1834 	if (sdinfo == NULL) {
1835 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1836 		    sata_device.satadev_addr.cport)));
1837 		return;
1838 	}
1839 	/*
1840 	 * We did not allocate any resources in sata_scsi_tgt_init()
1841 	 * other than few properties.
1842 	 * Free them.
1843 	 */
1844 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1845 	    sata_device.satadev_addr.cport)));
1846 	(void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable");
1847 
1848 	/*
1849 	 * If devid was previously created but not freed up from
1850 	 * sd(7D) driver (i.e during detach(9F)) then do it here.
1851 	 */
1852 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
1853 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
1854 	    "use-cmdk-devid-format", 0) == 1) &&
1855 	    (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) {
1856 		ddi_devid_unregister(tgt_dip);
1857 		ddi_devid_free(devid);
1858 	}
1859 }
1860 
1861 /*
1862  * Implementation of scsi tran_init_pkt
1863  * Upon successful return, scsi pkt buffer has DMA resources allocated.
1864  *
1865  * It seems that we should always allocate pkt, even if the address is
1866  * for non-existing device - just use some default for dma_attr.
1867  * The reason is that there is no way to communicate this to a caller here.
1868  * Subsequent call to sata_scsi_start may fail appropriately.
1869  * Simply returning NULL does not seem to discourage a target driver...
1870  *
1871  * Returns a pointer to initialized scsi_pkt, or NULL otherwise.
1872  */
1873 static struct scsi_pkt *
1874 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
1875     struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
1876     int (*callback)(caddr_t), caddr_t arg)
1877 {
1878 	sata_hba_inst_t *sata_hba_inst =
1879 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
1880 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
1881 	sata_device_t sata_device;
1882 	sata_drive_info_t *sdinfo;
1883 	sata_pkt_txlate_t *spx;
1884 	ddi_dma_attr_t cur_dma_attr;
1885 	int rval;
1886 	boolean_t new_pkt = TRUE;
1887 
1888 	ASSERT(ap->a_hba_tran->tran_hba_dip == dip);
1889 
1890 	/*
1891 	 * We need to translate the address, even if it could be
1892 	 * a bogus one, for a non-existing device
1893 	 */
1894 	sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
1895 	sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target);
1896 	sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
1897 	sata_device.satadev_rev = SATA_DEVICE_REV;
1898 
1899 	if (pkt == NULL) {
1900 		/*
1901 		 * Have to allocate a brand new scsi packet.
1902 		 * We need to operate with auto request sense enabled.
1903 		 */
1904 		pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen,
1905 		    MAX(statuslen, sizeof (struct scsi_arq_status)),
1906 		    tgtlen, sizeof (sata_pkt_txlate_t), callback, arg);
1907 
1908 		if (pkt == NULL)
1909 			return (NULL);
1910 
1911 		/* Fill scsi packet structure */
1912 		pkt->pkt_comp		= (void (*)())NULL;
1913 		pkt->pkt_time		= 0;
1914 		pkt->pkt_resid		= 0;
1915 		pkt->pkt_statistics	= 0;
1916 		pkt->pkt_reason		= 0;
1917 
1918 		/*
1919 		 * pkt_hba_private will point to sata pkt txlate structure
1920 		 */
1921 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
1922 		bzero(spx, sizeof (sata_pkt_txlate_t));
1923 
1924 		spx->txlt_scsi_pkt = pkt;
1925 		spx->txlt_sata_hba_inst = sata_hba_inst;
1926 
1927 		/* Allocate sata_pkt */
1928 		spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback);
1929 		if (spx->txlt_sata_pkt == NULL) {
1930 			/* Could not allocate sata pkt */
1931 			scsi_hba_pkt_free(ap, pkt);
1932 			return (NULL);
1933 		}
1934 		/* Set sata address */
1935 		spx->txlt_sata_pkt->satapkt_device.satadev_addr =
1936 		    sata_device.satadev_addr;
1937 		spx->txlt_sata_pkt->satapkt_device.satadev_rev =
1938 		    sata_device.satadev_rev;
1939 
1940 		if ((bp == NULL) || (bp->b_bcount == 0))
1941 			return (pkt);
1942 
1943 		spx->txlt_total_residue = bp->b_bcount;
1944 	} else {
1945 		new_pkt = FALSE;
1946 		/*
1947 		 * Packet was preallocated/initialized by previous call
1948 		 */
1949 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
1950 
1951 		if ((bp == NULL) || (bp->b_bcount == 0)) {
1952 			return (pkt);
1953 		}
1954 
1955 		/* Pkt is available already: spx->txlt_scsi_pkt == pkt; */
1956 	}
1957 
1958 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
1959 
1960 	/*
1961 	 * We use an adjusted version of the dma_attr, to account
1962 	 * for device addressing limitations.
1963 	 * sata_adjust_dma_attr() will handle sdinfo == NULL which may
1964 	 * happen when a device is not yet configured.
1965 	 */
1966 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1967 	    sata_device.satadev_addr.cport)));
1968 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
1969 	    &spx->txlt_sata_pkt->satapkt_device);
1970 	/* NULL sdinfo may be passsed to sata_adjust_dma_attr() */
1971 	sata_adjust_dma_attr(sdinfo,
1972 	    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
1973 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1974 	    sata_device.satadev_addr.cport)));
1975 	/*
1976 	 * Allocate necessary DMA resources for the packet's data buffer
1977 	 * NOTE:
1978 	 * In case of read/write commands, DMA resource allocation here is
1979 	 * based on the premise that the transfer length specified in
1980 	 * the read/write scsi cdb will match exactly DMA resources -
1981 	 * returning correct packet residue is crucial.
1982 	 */
1983 	if ((rval = sata_dma_buf_setup(spx, flags, callback, arg,
1984 	    &cur_dma_attr)) != DDI_SUCCESS) {
1985 		/*
1986 		 * If a DMA allocation request fails with
1987 		 * DDI_DMA_NOMAPPING, indicate the error by calling
1988 		 * bioerror(9F) with bp and an error code of EFAULT.
1989 		 * If a DMA allocation request fails with
1990 		 * DDI_DMA_TOOBIG, indicate the error by calling
1991 		 * bioerror(9F) with bp and an error code of EINVAL.
1992 		 * For DDI_DMA_NORESOURCES, we may have some of them allocated.
1993 		 * Request may be repeated later - there is no real error.
1994 		 */
1995 		switch (rval) {
1996 		case DDI_DMA_NORESOURCES:
1997 			bioerror(bp, 0);
1998 			break;
1999 		case DDI_DMA_NOMAPPING:
2000 		case DDI_DMA_BADATTR:
2001 			bioerror(bp, EFAULT);
2002 			break;
2003 		case DDI_DMA_TOOBIG:
2004 		default:
2005 			bioerror(bp, EINVAL);
2006 			break;
2007 		}
2008 		if (new_pkt == TRUE) {
2009 			/*
2010 			 * Since this is a new packet, we can clean-up
2011 			 * everything
2012 			 */
2013 			sata_scsi_destroy_pkt(ap, pkt);
2014 		} else {
2015 			/*
2016 			 * This is a re-used packet. It will be target driver's
2017 			 * responsibility to eventually destroy it (which
2018 			 * will free allocated resources).
2019 			 * Here, we just "complete" the request, leaving
2020 			 * allocated resources intact, so the request may
2021 			 * be retried.
2022 			 */
2023 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2024 			sata_pkt_free(spx);
2025 		}
2026 		return (NULL);
2027 	}
2028 	/* Set number of bytes that are not yet accounted for */
2029 	pkt->pkt_resid = spx->txlt_total_residue;
2030 	ASSERT(pkt->pkt_resid >= 0);
2031 
2032 	return (pkt);
2033 }
2034 
2035 /*
2036  * Implementation of scsi tran_start.
2037  * Translate scsi cmd into sata operation and return status.
2038  * ATAPI CDBs are passed to ATAPI devices - the device determines what commands
2039  * are supported.
2040  * For SATA hard disks, supported scsi commands:
2041  * SCMD_INQUIRY
2042  * SCMD_TEST_UNIT_READY
2043  * SCMD_START_STOP
2044  * SCMD_READ_CAPACITY
2045  * SCMD_REQUEST_SENSE
2046  * SCMD_LOG_SENSE_G1
2047  * SCMD_LOG_SELECT_G1
2048  * SCMD_MODE_SENSE	(specific pages)
2049  * SCMD_MODE_SENSE_G1	(specific pages)
2050  * SCMD_MODE_SELECT	(specific pages)
2051  * SCMD_MODE_SELECT_G1	(specific pages)
2052  * SCMD_SYNCHRONIZE_CACHE
2053  * SCMD_SYNCHRONIZE_CACHE_G1
2054  * SCMD_READ
2055  * SCMD_READ_G1
2056  * SCMD_READ_G4
2057  * SCMD_READ_G5
2058  * SCMD_WRITE
2059  * SCMD_WRITE_BUFFER
2060  * SCMD_WRITE_G1
2061  * SCMD_WRITE_G4
2062  * SCMD_WRITE_G5
2063  * SCMD_SEEK		(noop)
2064  * SCMD_SDIAG
2065  *
2066  * All other commands are rejected as unsupported.
2067  *
2068  * Returns:
2069  * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver
2070  * for execution. TRAN_ACCEPT may be returned also if device was removed but
2071  * a callback could be scheduled.
2072  * TRAN_BADPKT if cmd was directed to invalid address.
2073  * TRAN_FATAL_ERROR is command was rejected due to hardware error, including
2074  * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device
2075  * was removed and there was no callback specified in scsi pkt.
2076  * TRAN_BUSY if command could not be executed becasue HBA driver or SATA
2077  * framework was busy performing some other operation(s).
2078  *
2079  */
2080 static int
2081 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
2082 {
2083 	sata_hba_inst_t *sata_hba_inst =
2084 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2085 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2086 	sata_drive_info_t *sdinfo;
2087 	struct buf *bp;
2088 	int cport;
2089 	int rval;
2090 
2091 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2092 	    "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]);
2093 
2094 	ASSERT(spx != NULL &&
2095 	    spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL);
2096 
2097 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
2098 
2099 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2100 	sdinfo = sata_get_device_info(sata_hba_inst,
2101 	    &spx->txlt_sata_pkt->satapkt_device);
2102 	if (sdinfo == NULL ||
2103 	    SATA_CPORT_INFO(sata_hba_inst, cport)->cport_tgtnode_clean ==
2104 	    B_FALSE ||
2105 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2106 
2107 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2108 		pkt->pkt_reason = CMD_DEV_GONE;
2109 		/*
2110 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2111 		 * only in callback function (for normal requests) and
2112 		 * in the dump code path.
2113 		 * So, if the callback is available, we need to do
2114 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2115 		 */
2116 		if (pkt->pkt_comp != NULL) {
2117 			/* scsi callback required */
2118 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2119 			    (task_func_t *)pkt->pkt_comp,
2120 			    (void *)pkt, TQ_SLEEP) == NULL)
2121 				/* Scheduling the callback failed */
2122 				return (TRAN_BUSY);
2123 			return (TRAN_ACCEPT);
2124 		}
2125 		/* No callback available */
2126 		return (TRAN_FATAL_ERROR);
2127 	}
2128 
2129 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
2130 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2131 		rval = sata_txlt_atapi(spx);
2132 		SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2133 		    "sata_scsi_start atapi: rval %d\n", rval);
2134 		return (rval);
2135 	}
2136 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2137 
2138 	/* ATA Disk commands processing starts here */
2139 
2140 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2141 
2142 	switch (pkt->pkt_cdbp[0]) {
2143 
2144 	case SCMD_INQUIRY:
2145 		/* Mapped to identify device */
2146 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2147 			bp_mapin(bp);
2148 		rval = sata_txlt_inquiry(spx);
2149 		break;
2150 
2151 	case SCMD_TEST_UNIT_READY:
2152 		/*
2153 		 * SAT "SATA to ATA Translation" doc specifies translation
2154 		 * to ATA CHECK POWER MODE.
2155 		 */
2156 		rval = sata_txlt_test_unit_ready(spx);
2157 		break;
2158 
2159 	case SCMD_START_STOP:
2160 		/* Mapping depends on the command */
2161 		rval = sata_txlt_start_stop_unit(spx);
2162 		break;
2163 
2164 	case SCMD_READ_CAPACITY:
2165 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2166 			bp_mapin(bp);
2167 		rval = sata_txlt_read_capacity(spx);
2168 		break;
2169 
2170 	case SCMD_REQUEST_SENSE:
2171 		/*
2172 		 * Always No Sense, since we force ARQ
2173 		 */
2174 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2175 			bp_mapin(bp);
2176 		rval = sata_txlt_request_sense(spx);
2177 		break;
2178 
2179 	case SCMD_LOG_SENSE_G1:
2180 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2181 			bp_mapin(bp);
2182 		rval = sata_txlt_log_sense(spx);
2183 		break;
2184 
2185 	case SCMD_LOG_SELECT_G1:
2186 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2187 			bp_mapin(bp);
2188 		rval = sata_txlt_log_select(spx);
2189 		break;
2190 
2191 	case SCMD_MODE_SENSE:
2192 	case SCMD_MODE_SENSE_G1:
2193 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2194 			bp_mapin(bp);
2195 		rval = sata_txlt_mode_sense(spx);
2196 		break;
2197 
2198 
2199 	case SCMD_MODE_SELECT:
2200 	case SCMD_MODE_SELECT_G1:
2201 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2202 			bp_mapin(bp);
2203 		rval = sata_txlt_mode_select(spx);
2204 		break;
2205 
2206 	case SCMD_SYNCHRONIZE_CACHE:
2207 	case SCMD_SYNCHRONIZE_CACHE_G1:
2208 		rval = sata_txlt_synchronize_cache(spx);
2209 		break;
2210 
2211 	case SCMD_READ:
2212 	case SCMD_READ_G1:
2213 	case SCMD_READ_G4:
2214 	case SCMD_READ_G5:
2215 		rval = sata_txlt_read(spx);
2216 		break;
2217 	case SCMD_WRITE_BUFFER:
2218 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2219 			bp_mapin(bp);
2220 		rval = sata_txlt_write_buffer(spx);
2221 		break;
2222 
2223 	case SCMD_WRITE:
2224 	case SCMD_WRITE_G1:
2225 	case SCMD_WRITE_G4:
2226 	case SCMD_WRITE_G5:
2227 		rval = sata_txlt_write(spx);
2228 		break;
2229 
2230 	case SCMD_SEEK:
2231 		rval = sata_txlt_nodata_cmd_immediate(spx);
2232 		break;
2233 
2234 		/* Other cases will be filed later */
2235 		/* postponed until phase 2 of the development */
2236 	default:
2237 		rval = sata_txlt_invalid_command(spx);
2238 		break;
2239 	}
2240 
2241 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2242 	    "sata_scsi_start: rval %d\n", rval);
2243 
2244 	return (rval);
2245 }
2246 
2247 /*
2248  * Implementation of scsi tran_abort.
2249  * Abort specific pkt or all packets.
2250  *
2251  * Returns 1 if one or more packets were aborted, returns 0 otherwise
2252  *
2253  * May be called from an interrupt level.
2254  */
2255 static int
2256 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt)
2257 {
2258 	sata_hba_inst_t *sata_hba_inst =
2259 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2260 	sata_device_t	sata_device;
2261 	sata_pkt_t	*sata_pkt;
2262 
2263 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2264 	    "sata_scsi_abort: %s at target: 0x%x\n",
2265 	    scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target);
2266 
2267 	/* Validate address */
2268 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0)
2269 		/* Invalid address */
2270 		return (0);
2271 
2272 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2273 	    sata_device.satadev_addr.cport)));
2274 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2275 		/* invalid address */
2276 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2277 		    sata_device.satadev_addr.cport)));
2278 		return (0);
2279 	}
2280 	if (scsi_pkt == NULL) {
2281 		/*
2282 		 * Abort all packets.
2283 		 * Although we do not have specific packet, we still need
2284 		 * dummy packet structure to pass device address to HBA.
2285 		 * Allocate one, without sleeping. Fail if pkt cannot be
2286 		 * allocated.
2287 		 */
2288 		sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP);
2289 		if (sata_pkt == NULL) {
2290 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2291 			    sata_device.satadev_addr.cport)));
2292 			SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: "
2293 			    "could not allocate sata_pkt"));
2294 			return (0);
2295 		}
2296 		sata_pkt->satapkt_rev = SATA_PKT_REV;
2297 		sata_pkt->satapkt_device = sata_device;
2298 		sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
2299 	} else {
2300 		if (scsi_pkt->pkt_ha_private == NULL) {
2301 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2302 			    sata_device.satadev_addr.cport)));
2303 			return (0); /* Bad scsi pkt */
2304 		}
2305 		/* extract pointer to sata pkt */
2306 		sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)->
2307 		    txlt_sata_pkt;
2308 	}
2309 
2310 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2311 	    sata_device.satadev_addr.cport)));
2312 	/* Send abort request to HBA */
2313 	if ((*SATA_ABORT_FUNC(sata_hba_inst))
2314 	    (SATA_DIP(sata_hba_inst), sata_pkt,
2315 	    scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) ==
2316 	    SATA_SUCCESS) {
2317 		if (scsi_pkt == NULL)
2318 			kmem_free(sata_pkt, sizeof (sata_pkt_t));
2319 		/* Success */
2320 		return (1);
2321 	}
2322 	/* Else, something did not go right */
2323 	if (scsi_pkt == NULL)
2324 		kmem_free(sata_pkt, sizeof (sata_pkt_t));
2325 	/* Failure */
2326 	return (0);
2327 }
2328 
2329 
2330 /*
2331  * Implementation of scsi tran_reset.
2332  * RESET_ALL request is translated into port reset.
2333  * RESET_TARGET requests is translated into a device reset,
2334  * RESET_LUN request is accepted only for LUN 0 and translated into
2335  * device reset.
2336  * The target reset should cause all HBA active and queued packets to
2337  * be terminated and returned with pkt reason SATA_PKT_RESET prior to
2338  * the return. HBA should report reset event for the device.
2339  *
2340  * Returns 1 upon success, 0 upon failure.
2341  */
2342 static int
2343 sata_scsi_reset(struct scsi_address *ap, int level)
2344 {
2345 	sata_hba_inst_t	*sata_hba_inst =
2346 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2347 	sata_device_t	sata_device;
2348 	int		val;
2349 
2350 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2351 	    "sata_scsi_reset: level %d target: 0x%x\n",
2352 	    level, ap->a_target);
2353 
2354 	/* Validate address */
2355 	val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device);
2356 	if (val == -1)
2357 		/* Invalid address */
2358 		return (0);
2359 
2360 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2361 	    sata_device.satadev_addr.cport)));
2362 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2363 		/* invalid address */
2364 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2365 		    sata_device.satadev_addr.cport)));
2366 		return (0);
2367 	}
2368 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2369 	    sata_device.satadev_addr.cport)));
2370 	if (level == RESET_ALL) {
2371 		/* port reset - cport only */
2372 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2373 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2374 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2375 			return (1);
2376 		else
2377 			return (0);
2378 
2379 	} else if (val == 0 &&
2380 	    (level == RESET_TARGET || level == RESET_LUN)) {
2381 		/* reset device (device attached) */
2382 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2383 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2384 			return (1);
2385 		else
2386 			return (0);
2387 	}
2388 	return (0);
2389 }
2390 
2391 
2392 /*
2393  * Implementation of scsi tran_getcap (get transport/device capabilities).
2394  * Supported capabilities for SATA hard disks:
2395  * auto-rqsense		(always supported)
2396  * tagged-qing		(supported if HBA supports it)
2397  * untagged-qing	(could be supported if disk supports it, but because
2398  *			 caching behavior allowing untagged queuing actually
2399  *			 results in reduced performance.  sd tries to throttle
2400  *			 back to only 3 outstanding commands, which may
2401  *			 work for real SCSI disks, but with read ahead
2402  *			 caching, having more than 1 outstanding command
2403  *			 results in cache thrashing.)
2404  * sector_size
2405  * dma_max
2406  * interconnect-type	(INTERCONNECT_SATA)
2407  *
2408  * Supported capabilities for ATAPI devices (CD/DVD):
2409  * auto-rqsense		(always supported)
2410  * sector_size
2411  * dma_max
2412  * interconnect-type	(INTERCONNECT_SATA)
2413  *
2414  * Request for other capabilities is rejected as unsupported.
2415  *
2416  * Returns supported capability value, or -1 if capability is unsuppported or
2417  * the address is invalid - no device.
2418  */
2419 
2420 static int
2421 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
2422 {
2423 
2424 	sata_hba_inst_t 	*sata_hba_inst =
2425 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2426 	sata_device_t		sata_device;
2427 	sata_drive_info_t	*sdinfo;
2428 	ddi_dma_attr_t		adj_dma_attr;
2429 	int 			rval;
2430 
2431 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2432 	    "sata_scsi_getcap: target: 0x%x, cap: %s\n",
2433 	    ap->a_target, cap);
2434 
2435 	/*
2436 	 * We want to process the capabilities on per port granularity.
2437 	 * So, we are specifically restricting ourselves to whom != 0
2438 	 * to exclude the controller wide handling.
2439 	 */
2440 	if (cap == NULL || whom == 0)
2441 		return (-1);
2442 
2443 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2444 		/* Invalid address */
2445 		return (-1);
2446 	}
2447 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2448 	    sata_device.satadev_addr.cport)));
2449 	if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) ==
2450 	    NULL) {
2451 		/* invalid address */
2452 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2453 		    sata_device.satadev_addr.cport)));
2454 		return (-1);
2455 	}
2456 
2457 	switch (scsi_hba_lookup_capstr(cap)) {
2458 	case SCSI_CAP_ARQ:
2459 		rval = 1;		/* ARQ supported, turned on */
2460 		break;
2461 
2462 	case SCSI_CAP_SECTOR_SIZE:
2463 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK)
2464 			rval = SATA_DISK_SECTOR_SIZE;	/* fixed size */
2465 		else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD)
2466 			rval = SATA_ATAPI_SECTOR_SIZE;
2467 		else rval = -1;
2468 		break;
2469 
2470 	/*
2471 	 * untagged queuing cause a performance inversion because of
2472 	 * the way sd operates.  Because of this reason we do not
2473 	 * use it when available.
2474 	 */
2475 	case SCSI_CAP_UNTAGGED_QING:
2476 		if (sdinfo->satadrv_features_enabled &
2477 		    SATA_DEV_F_E_UNTAGGED_QING)
2478 			rval = 1;	/* Untagged queuing available */
2479 		else
2480 			rval = -1;	/* Untagged queuing not available */
2481 		break;
2482 
2483 	case SCSI_CAP_TAGGED_QING:
2484 		if ((sdinfo->satadrv_features_enabled &
2485 		    SATA_DEV_F_E_TAGGED_QING) &&
2486 		    (sdinfo->satadrv_max_queue_depth > 1))
2487 			rval = 1;	/* Tagged queuing available */
2488 		else
2489 			rval = -1;	/* Tagged queuing not available */
2490 		break;
2491 
2492 	case SCSI_CAP_DMA_MAX:
2493 		sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst),
2494 		    &adj_dma_attr);
2495 		rval = (int)adj_dma_attr.dma_attr_maxxfer;
2496 		/* We rely on the fact that dma_attr_maxxfer < 0x80000000 */
2497 		break;
2498 
2499 	case SCSI_CAP_INTERCONNECT_TYPE:
2500 		rval = INTERCONNECT_SATA;	/* SATA interconnect type */
2501 		break;
2502 
2503 	default:
2504 		rval = -1;
2505 		break;
2506 	}
2507 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2508 	    sata_device.satadev_addr.cport)));
2509 	return (rval);
2510 }
2511 
2512 /*
2513  * Implementation of scsi tran_setcap
2514  *
2515  * Only SCSI_CAP_UNTAGGED_QING and  SCSI_CAP_TAGGED_QING are changeable.
2516  *
2517  */
2518 static int
2519 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
2520 {
2521 	sata_hba_inst_t	*sata_hba_inst =
2522 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2523 	sata_device_t	sata_device;
2524 	sata_drive_info_t	*sdinfo;
2525 	int		rval;
2526 
2527 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2528 	    "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap);
2529 
2530 	/*
2531 	 * We want to process the capabilities on per port granularity.
2532 	 * So, we are specifically restricting ourselves to whom != 0
2533 	 * to exclude the controller wide handling.
2534 	 */
2535 	if (cap == NULL || whom == 0) {
2536 		return (-1);
2537 	}
2538 
2539 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2540 		/* Invalid address */
2541 		return (-1);
2542 	}
2543 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2544 	    sata_device.satadev_addr.cport)));
2545 	if ((sdinfo = sata_get_device_info(sata_hba_inst,
2546 	    &sata_device)) == NULL) {
2547 		/* invalid address */
2548 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2549 		    sata_device.satadev_addr.cport)));
2550 		return (-1);
2551 	}
2552 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2553 	    sata_device.satadev_addr.cport)));
2554 
2555 	switch (scsi_hba_lookup_capstr(cap)) {
2556 	case SCSI_CAP_ARQ:
2557 	case SCSI_CAP_SECTOR_SIZE:
2558 	case SCSI_CAP_DMA_MAX:
2559 	case SCSI_CAP_INTERCONNECT_TYPE:
2560 		rval = 0;
2561 		break;
2562 	case SCSI_CAP_UNTAGGED_QING:
2563 		if (SATA_QDEPTH(sata_hba_inst) > 1) {
2564 			rval = 1;
2565 			if (value == 1) {
2566 				sdinfo->satadrv_features_enabled |=
2567 				    SATA_DEV_F_E_UNTAGGED_QING;
2568 			} else if (value == 0) {
2569 				sdinfo->satadrv_features_enabled &=
2570 				    ~SATA_DEV_F_E_UNTAGGED_QING;
2571 			} else {
2572 				rval = -1;
2573 			}
2574 		} else {
2575 			rval = 0;
2576 		}
2577 		break;
2578 	case SCSI_CAP_TAGGED_QING:
2579 		/* This can TCQ or NCQ */
2580 		if (sata_func_enable & SATA_ENABLE_QUEUING &&
2581 		    ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ &&
2582 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) ||
2583 		    (sata_func_enable & SATA_ENABLE_NCQ &&
2584 		    sdinfo->satadrv_features_support & SATA_DEV_F_NCQ &&
2585 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) &&
2586 		    (sdinfo->satadrv_max_queue_depth > 1)) {
2587 			rval = 1;
2588 			if (value == 1) {
2589 				sdinfo->satadrv_features_enabled |=
2590 				    SATA_DEV_F_E_TAGGED_QING;
2591 			} else if (value == 0) {
2592 				sdinfo->satadrv_features_enabled &=
2593 				    ~SATA_DEV_F_E_TAGGED_QING;
2594 			} else {
2595 				rval = -1;
2596 			}
2597 		} else {
2598 			rval = 0;
2599 		}
2600 		break;
2601 	default:
2602 		rval = -1;
2603 		break;
2604 	}
2605 	return (rval);
2606 }
2607 
2608 /*
2609  * Implementations of scsi tran_destroy_pkt.
2610  * Free resources allocated by sata_scsi_init_pkt()
2611  */
2612 static void
2613 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2614 {
2615 	sata_pkt_txlate_t *spx;
2616 
2617 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2618 
2619 	sata_common_free_dma_rsrcs(spx);
2620 
2621 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2622 	sata_pkt_free(spx);
2623 
2624 	scsi_hba_pkt_free(ap, pkt);
2625 }
2626 
2627 /*
2628  * Implementation of scsi tran_dmafree.
2629  * Free DMA resources allocated by sata_scsi_init_pkt()
2630  */
2631 
2632 static void
2633 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
2634 {
2635 #ifndef __lock_lint
2636 	_NOTE(ARGUNUSED(ap))
2637 #endif
2638 	sata_pkt_txlate_t *spx;
2639 
2640 	ASSERT(pkt != NULL);
2641 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2642 
2643 	sata_common_free_dma_rsrcs(spx);
2644 }
2645 
2646 /*
2647  * Implementation of scsi tran_sync_pkt.
2648  *
2649  * The assumption below is that pkt is unique - there is no need to check ap
2650  *
2651  * Synchronize DMA buffer and, if the intermediate buffer is used, copy data
2652  * into/from the real buffer.
2653  */
2654 static void
2655 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2656 {
2657 #ifndef __lock_lint
2658 	_NOTE(ARGUNUSED(ap))
2659 #endif
2660 	int rval;
2661 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2662 	struct buf *bp;
2663 	int direction;
2664 
2665 	ASSERT(spx != NULL);
2666 	if (spx->txlt_buf_dma_handle != NULL) {
2667 		direction = spx->txlt_sata_pkt->
2668 		    satapkt_cmd.satacmd_flags.sata_data_direction;
2669 		if (spx->txlt_sata_pkt != NULL &&
2670 		    direction != SATA_DIR_NODATA_XFER) {
2671 			if (spx->txlt_tmp_buf != NULL) {
2672 				/* Intermediate DMA buffer used */
2673 				bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2674 
2675 				if (direction & SATA_DIR_WRITE) {
2676 					bcopy(bp->b_un.b_addr,
2677 					    spx->txlt_tmp_buf, bp->b_bcount);
2678 				}
2679 			}
2680 			/* Sync the buffer for device or for CPU */
2681 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle,   0, 0,
2682 			    (direction & SATA_DIR_WRITE) ?
2683 			    DDI_DMA_SYNC_FORDEV :  DDI_DMA_SYNC_FORCPU);
2684 			ASSERT(rval == DDI_SUCCESS);
2685 			if (spx->txlt_tmp_buf != NULL &&
2686 			    !(direction & SATA_DIR_WRITE)) {
2687 				/* Intermediate DMA buffer used for read */
2688 				bcopy(spx->txlt_tmp_buf,
2689 				    bp->b_un.b_addr, bp->b_bcount);
2690 			}
2691 
2692 		}
2693 	}
2694 }
2695 
2696 
2697 
2698 /* *******************  SATA - SCSI Translation functions **************** */
2699 /*
2700  * SCSI to SATA pkt and command translation and SATA to SCSI status/error
2701  * translation.
2702  */
2703 
2704 /*
2705  * Checks if a device exists and can be access and translates common
2706  * scsi_pkt data to sata_pkt data.
2707  *
2708  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and
2709  * sata_pkt was set-up.
2710  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not
2711  * exist and pkt_comp callback was scheduled.
2712  * Returns other TRAN_XXXXX values when error occured and command should be
2713  * rejected with the returned TRAN_XXXXX value.
2714  *
2715  * This function should be called with port mutex held.
2716  */
2717 static int
2718 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason)
2719 {
2720 	sata_drive_info_t *sdinfo;
2721 	sata_device_t sata_device;
2722 	const struct sata_cmd_flags sata_initial_cmd_flags = {
2723 		SATA_DIR_NODATA_XFER,
2724 		/* all other values to 0/FALSE */
2725 	};
2726 	/*
2727 	 * Pkt_reason has to be set if the pkt_comp callback is invoked,
2728 	 * and that implies TRAN_ACCEPT return value. Any other returned value
2729 	 * indicates that the scsi packet was not accepted (the reason will not
2730 	 * be checked by the scsi target driver).
2731 	 * To make debugging easier, we set pkt_reason to know value here.
2732 	 * It may be changed later when different completion reason is
2733 	 * determined.
2734 	 */
2735 	spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
2736 	*reason = CMD_TRAN_ERR;
2737 
2738 	/* Validate address */
2739 	switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst,
2740 	    &spx->txlt_scsi_pkt->pkt_address, &sata_device)) {
2741 
2742 	case -1:
2743 		/* Invalid address or invalid device type */
2744 		return (TRAN_BADPKT);
2745 	case 1:
2746 		/* valid address but no device - it has disappeared ? */
2747 		spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE;
2748 		*reason = CMD_DEV_GONE;
2749 		/*
2750 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2751 		 * only in callback function (for normal requests) and
2752 		 * in the dump code path.
2753 		 * So, if the callback is available, we need to do
2754 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2755 		 */
2756 		if (spx->txlt_scsi_pkt->pkt_comp != NULL) {
2757 			/* scsi callback required */
2758 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2759 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2760 			    (void *)spx->txlt_scsi_pkt,
2761 			    TQ_SLEEP) == NULL)
2762 				/* Scheduling the callback failed */
2763 				return (TRAN_BUSY);
2764 
2765 			return (TRAN_ACCEPT);
2766 		}
2767 		return (TRAN_FATAL_ERROR);
2768 	default:
2769 		/* all OK; pkt reason will be overwritten later */
2770 		break;
2771 	}
2772 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2773 	    &spx->txlt_sata_pkt->satapkt_device);
2774 
2775 	/*
2776 	 * If device is in reset condition, reject the packet with
2777 	 * TRAN_BUSY, unless:
2778 	 * 1. system is panicking (dumping)
2779 	 * In such case only one thread is running and there is no way to
2780 	 * process reset.
2781 	 * 2. cfgadm operation is is progress (internal APCTL lock is set)
2782 	 * Some cfgadm operations involve drive commands, so reset condition
2783 	 * needs to be ignored for IOCTL operations.
2784 	 */
2785 	if ((sdinfo->satadrv_event_flags &
2786 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
2787 
2788 		if (!ddi_in_panic() &&
2789 		    ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst,
2790 		    sata_device.satadev_addr.cport) &
2791 		    SATA_APCTL_LOCK_PORT_BUSY) == 0)) {
2792 			spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
2793 			*reason = CMD_INCOMPLETE;
2794 			SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
2795 			    "sata_scsi_start: rejecting command because "
2796 			    "of device reset state\n", NULL);
2797 			return (TRAN_BUSY);
2798 		}
2799 	}
2800 
2801 	/*
2802 	 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by
2803 	 * sata_scsi_pkt_init() because pkt init had to work also with
2804 	 * non-existing devices.
2805 	 * Now we know that the packet was set-up for a real device, so its
2806 	 * type is known.
2807 	 */
2808 	spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type;
2809 
2810 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags;
2811 	if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst,
2812 	    sata_device.satadev_addr.cport)->cport_event_flags &
2813 	    SATA_APCTL_LOCK_PORT_BUSY) != 0) {
2814 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
2815 		    sata_ignore_dev_reset = B_TRUE;
2816 	}
2817 	/*
2818 	 * At this point the generic translation routine determined that the
2819 	 * scsi packet should be accepted. Packet completion reason may be
2820 	 * changed later when a different completion reason is determined.
2821 	 */
2822 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
2823 	*reason = CMD_CMPLT;
2824 
2825 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
2826 		/* Synchronous execution */
2827 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH |
2828 		    SATA_OPMODE_POLLING;
2829 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
2830 		    sata_ignore_dev_reset = ddi_in_panic();
2831 	} else {
2832 		/* Asynchronous execution */
2833 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH |
2834 		    SATA_OPMODE_INTERRUPTS;
2835 	}
2836 	/* Convert queuing information */
2837 	if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG)
2838 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag =
2839 		    B_TRUE;
2840 	else if (spx->txlt_scsi_pkt->pkt_flags &
2841 	    (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD))
2842 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag =
2843 		    B_TRUE;
2844 
2845 	/* Always limit pkt time */
2846 	if (spx->txlt_scsi_pkt->pkt_time == 0)
2847 		spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time;
2848 	else
2849 		/* Pass on scsi_pkt time */
2850 		spx->txlt_sata_pkt->satapkt_time =
2851 		    spx->txlt_scsi_pkt->pkt_time;
2852 
2853 	return (TRAN_ACCEPT);
2854 }
2855 
2856 
2857 /*
2858  * Translate ATA Identify Device data to SCSI Inquiry data.
2859  * This function may be called only for ATA devices.
2860  * This function should not be called for ATAPI devices - they
2861  * respond directly to SCSI Inquiry command.
2862  *
2863  * SATA Identify Device data has to be valid in sata_rive_info.
2864  * Buffer has to accomodate the inquiry length (36 bytes).
2865  *
2866  * This function should be called with a port mutex held.
2867  */
2868 static	void
2869 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst,
2870     sata_drive_info_t *sdinfo, uint8_t *buf)
2871 {
2872 
2873 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
2874 	struct sata_id *sid = &sdinfo->satadrv_id;
2875 
2876 	/* Start with a nice clean slate */
2877 	bzero((void *)inq, sizeof (struct scsi_inquiry));
2878 
2879 	/*
2880 	 * Rely on the dev_type for setting paripheral qualifier.
2881 	 * Assume that  DTYPE_RODIRECT applies to CD/DVD R/W devices.
2882 	 * It could be that DTYPE_OPTICAL could also qualify in the future.
2883 	 * ATAPI Inquiry may provide more data to the target driver.
2884 	 */
2885 	inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
2886 	    DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */
2887 
2888 	inq->inq_rmb = sid->ai_config & SATA_REM_MEDIA ? 1 : 0;
2889 	inq->inq_qual = 0;	/* Device type qualifier (obsolete in SCSI3? */
2890 	inq->inq_iso = 0;	/* ISO version */
2891 	inq->inq_ecma = 0;	/* ECMA version */
2892 	inq->inq_ansi = 3;	/* ANSI version - SCSI 3 */
2893 	inq->inq_aenc = 0;	/* Async event notification cap. */
2894 	inq->inq_trmiop = 0;	/* Supports TERMINATE I/O PROC msg - NO */
2895 	inq->inq_normaca = 0;	/* setting NACA bit supported - NO */
2896 	inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */
2897 	inq->inq_len = 31;	/* Additional length */
2898 	inq->inq_dualp = 0;	/* dual port device - NO */
2899 	inq->inq_reladdr = 0;	/* Supports relative addressing - NO */
2900 	inq->inq_sync = 0;	/* Supports synchronous data xfers - NO */
2901 	inq->inq_linked = 0;	/* Supports linked commands - NO */
2902 				/*
2903 				 * Queuing support - controller has to
2904 				 * support some sort of command queuing.
2905 				 */
2906 	if (SATA_QDEPTH(sata_hba_inst) > 1)
2907 		inq->inq_cmdque = 1; /* Supports command queueing - YES */
2908 	else
2909 		inq->inq_cmdque = 0; /* Supports command queueing - NO */
2910 	inq->inq_sftre = 0;	/* Supports Soft Reset option - NO ??? */
2911 	inq->inq_wbus32 = 0;	/* Supports 32 bit wide data xfers - NO */
2912 	inq->inq_wbus16 = 0;	/* Supports 16 bit wide data xfers - NO */
2913 
2914 #ifdef	_LITTLE_ENDIAN
2915 	/* Swap text fields to match SCSI format */
2916 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
2917 	swab(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
2918 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
2919 		swab(sid->ai_fw, inq->inq_revision, 4);	/* Revision level */
2920 	else
2921 		swab(&sid->ai_fw[4], inq->inq_revision, 4);	/* Rev. level */
2922 #else	/* _LITTLE_ENDIAN */
2923 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
2924 	bcopy(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
2925 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
2926 		bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
2927 	else
2928 		bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
2929 #endif	/* _LITTLE_ENDIAN */
2930 }
2931 
2932 
2933 /*
2934  * Scsi response set up for invalid command (command not supported)
2935  *
2936  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
2937  */
2938 static int
2939 sata_txlt_invalid_command(sata_pkt_txlate_t *spx)
2940 {
2941 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
2942 	struct scsi_extended_sense *sense;
2943 
2944 	scsipkt->pkt_reason = CMD_CMPLT;
2945 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
2946 	    STATE_SENT_CMD | STATE_GOT_STATUS;
2947 
2948 	*scsipkt->pkt_scbp = STATUS_CHECK;
2949 
2950 	sense = sata_arq_sense(spx);
2951 	sense->es_key = KEY_ILLEGAL_REQUEST;
2952 	sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE;
2953 
2954 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
2955 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
2956 
2957 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
2958 	    scsipkt->pkt_comp != NULL)
2959 		/* scsi callback required */
2960 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2961 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2962 		    (void *)spx->txlt_scsi_pkt,
2963 		    TQ_SLEEP) == NULL)
2964 			/* Scheduling the callback failed */
2965 			return (TRAN_BUSY);
2966 	return (TRAN_ACCEPT);
2967 }
2968 
2969 /*
2970  * Scsi response setup for
2971  * emulated non-data command that requires no action/return data
2972  *
2973  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
2974  */
2975 static 	int
2976 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx)
2977 {
2978 	int rval;
2979 	int reason;
2980 
2981 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
2982 
2983 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
2984 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
2985 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
2986 		return (rval);
2987 	}
2988 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
2989 
2990 	spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
2991 	    STATE_SENT_CMD | STATE_GOT_STATUS;
2992 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
2993 	*(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD;
2994 
2995 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
2996 	    "Scsi_pkt completion reason %x\n",
2997 	    spx->txlt_scsi_pkt->pkt_reason);
2998 
2999 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 &&
3000 	    spx->txlt_scsi_pkt->pkt_comp != NULL)
3001 		/* scsi callback required */
3002 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3003 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3004 		    (void *)spx->txlt_scsi_pkt,
3005 		    TQ_SLEEP) == NULL)
3006 			/* Scheduling the callback failed */
3007 			return (TRAN_BUSY);
3008 	return (TRAN_ACCEPT);
3009 }
3010 
3011 
3012 /*
3013  * SATA translate command: Inquiry / Identify Device
3014  * Use cached Identify Device data for now, rather than issuing actual
3015  * Device Identify cmd request. If device is detached and re-attached,
3016  * asynchromous event processing should fetch and refresh Identify Device
3017  * data.
3018  * Two VPD pages are supported now:
3019  * Vital Product Data page
3020  * Unit Serial Number page
3021  *
3022  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3023  */
3024 
3025 #define	EVPD			1	/* Extended Vital Product Data flag */
3026 #define	CMDDT			2	/* Command Support Data - Obsolete */
3027 #define	INQUIRY_SUP_VPD_PAGE	0	/* Supported VDP Pages Page COde */
3028 #define	INQUIRY_USN_PAGE	0x80	/* Unit Serial Number Page Code */
3029 #define	INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */
3030 
3031 static int
3032 sata_txlt_inquiry(sata_pkt_txlate_t *spx)
3033 {
3034 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3035 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3036 	sata_drive_info_t *sdinfo;
3037 	struct scsi_extended_sense *sense;
3038 	int count;
3039 	uint8_t *p;
3040 	int i, j;
3041 	uint8_t page_buf[0xff]; /* Max length */
3042 	int rval, reason;
3043 
3044 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3045 
3046 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3047 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3048 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3049 		return (rval);
3050 	}
3051 
3052 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3053 	    &spx->txlt_sata_pkt->satapkt_device);
3054 
3055 	ASSERT(sdinfo != NULL);
3056 
3057 	scsipkt->pkt_reason = CMD_CMPLT;
3058 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3059 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3060 
3061 	/* Reject not supported request */
3062 	if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */
3063 		*scsipkt->pkt_scbp = STATUS_CHECK;
3064 		sense = sata_arq_sense(spx);
3065 		sense->es_key = KEY_ILLEGAL_REQUEST;
3066 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3067 		goto done;
3068 	}
3069 
3070 	/* Valid Inquiry request */
3071 	*scsipkt->pkt_scbp = STATUS_GOOD;
3072 
3073 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3074 
3075 		/*
3076 		 * Because it is fully emulated command storing data
3077 		 * programatically in the specified buffer, release
3078 		 * preallocated DMA resources before storing data in the buffer,
3079 		 * so no unwanted DMA sync would take place.
3080 		 */
3081 		sata_scsi_dmafree(NULL, scsipkt);
3082 
3083 		if (!(scsipkt->pkt_cdbp[1] & EVPD)) {
3084 			/* Standard Inquiry Data request */
3085 			struct scsi_inquiry inq;
3086 			unsigned int bufsize;
3087 
3088 			sata_identdev_to_inquiry(spx->txlt_sata_hba_inst,
3089 			    sdinfo, (uint8_t *)&inq);
3090 			/* Copy no more than requested */
3091 			count = MIN(bp->b_bcount,
3092 			    sizeof (struct scsi_inquiry));
3093 			bufsize = scsipkt->pkt_cdbp[4];
3094 			bufsize |= scsipkt->pkt_cdbp[3] << 8;
3095 			count = MIN(count, bufsize);
3096 			bcopy(&inq, bp->b_un.b_addr, count);
3097 
3098 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
3099 			scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3100 			    bufsize - count : 0;
3101 		} else {
3102 			/*
3103 			 * peripheral_qualifier = 0;
3104 			 *
3105 			 * We are dealing only with HD and will be
3106 			 * dealing with CD/DVD devices soon
3107 			 */
3108 			uint8_t peripheral_device_type =
3109 			    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3110 			    DTYPE_DIRECT : DTYPE_RODIRECT;
3111 
3112 			switch ((uint_t)scsipkt->pkt_cdbp[2]) {
3113 			case INQUIRY_SUP_VPD_PAGE:
3114 				/*
3115 				 * Request for suported Vital Product Data
3116 				 * pages - assuming only 2 page codes
3117 				 * supported
3118 				 */
3119 				page_buf[0] = peripheral_device_type;
3120 				page_buf[1] = INQUIRY_SUP_VPD_PAGE;
3121 				page_buf[2] = 0;
3122 				page_buf[3] = 2; /* page length */
3123 				page_buf[4] = INQUIRY_SUP_VPD_PAGE;
3124 				page_buf[5] = INQUIRY_USN_PAGE;
3125 				/* Copy no more than requested */
3126 				count = MIN(bp->b_bcount, 6);
3127 				bcopy(page_buf, bp->b_un.b_addr, count);
3128 				break;
3129 			case INQUIRY_USN_PAGE:
3130 				/*
3131 				 * Request for Unit Serial Number page
3132 				 */
3133 				page_buf[0] = peripheral_device_type;
3134 				page_buf[1] = INQUIRY_USN_PAGE;
3135 				page_buf[2] = 0;
3136 				page_buf[3] = 20; /* remaining page length */
3137 				p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser);
3138 #ifdef	_LITTLE_ENDIAN
3139 				swab(p, &page_buf[4], 20);
3140 #else
3141 				bcopy(p, &page_buf[4], 20);
3142 #endif
3143 				for (i = 0; i < 20; i++) {
3144 					if (page_buf[4 + i] == '\0' ||
3145 					    page_buf[4 + i] == '\040') {
3146 						break;
3147 					}
3148 				}
3149 				/*
3150 				 * 'i' contains string length.
3151 				 *
3152 				 * Least significant character of the serial
3153 				 * number shall appear as the last byte,
3154 				 * according to SBC-3 spec.
3155 				 */
3156 				p = &page_buf[20 + 4 - 1];
3157 				for (j = i; j > 0; j--, p--) {
3158 					*p = *(p - 20 + i);
3159 				}
3160 				p = &page_buf[4];
3161 				for (j = 20 - i; j > 0; j--) {
3162 					*p++ = '\040';
3163 				}
3164 				count = MIN(bp->b_bcount, 24);
3165 				bcopy(page_buf, bp->b_un.b_addr, count);
3166 				break;
3167 
3168 			case INQUIRY_DEV_IDENTIFICATION_PAGE:
3169 				/*
3170 				 * We may want to implement this page, when
3171 				 * identifiers are common for SATA devices
3172 				 * But not now.
3173 				 */
3174 				/*FALLTHROUGH*/
3175 
3176 			default:
3177 				/* Request for unsupported VPD page */
3178 				*scsipkt->pkt_scbp = STATUS_CHECK;
3179 				sense = sata_arq_sense(spx);
3180 				sense->es_key = KEY_ILLEGAL_REQUEST;
3181 				sense->es_add_code =
3182 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3183 				goto done;
3184 			}
3185 		}
3186 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3187 		scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3188 		    scsipkt->pkt_cdbp[4] - count : 0;
3189 	}
3190 done:
3191 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3192 
3193 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3194 	    "Scsi_pkt completion reason %x\n",
3195 	    scsipkt->pkt_reason);
3196 
3197 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3198 	    scsipkt->pkt_comp != NULL) {
3199 		/* scsi callback required */
3200 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3201 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3202 		    TQ_SLEEP) == NULL)
3203 			/* Scheduling the callback failed */
3204 			return (TRAN_BUSY);
3205 	}
3206 	return (TRAN_ACCEPT);
3207 }
3208 
3209 /*
3210  * SATA translate command: Request Sense.
3211  * Emulated command (ATA version for SATA hard disks)
3212  * Always NO SENSE, because any sense data should be reported by ARQ sense.
3213  *
3214  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3215  */
3216 static int
3217 sata_txlt_request_sense(sata_pkt_txlate_t *spx)
3218 {
3219 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3220 	struct scsi_extended_sense sense;
3221 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3222 	int rval, reason;
3223 
3224 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3225 
3226 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3227 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3228 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3229 		return (rval);
3230 	}
3231 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3232 
3233 
3234 	scsipkt->pkt_reason = CMD_CMPLT;
3235 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3236 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3237 	*scsipkt->pkt_scbp = STATUS_GOOD;
3238 
3239 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3240 		/*
3241 		 * Because it is fully emulated command storing data
3242 		 * programatically in the specified buffer, release
3243 		 * preallocated DMA resources before storing data in the buffer,
3244 		 * so no unwanted DMA sync would take place.
3245 		 */
3246 		int count = MIN(bp->b_bcount,
3247 		    sizeof (struct scsi_extended_sense));
3248 		sata_scsi_dmafree(NULL, scsipkt);
3249 		bzero(&sense, sizeof (struct scsi_extended_sense));
3250 		sense.es_valid = 0;	/* Valid LBA */
3251 		sense.es_class = 7;	/* Response code 0x70 - current err */
3252 		sense.es_key = KEY_NO_SENSE;
3253 		sense.es_add_len = 6;	/* Additional length */
3254 		/* Copy no more than requested */
3255 		bcopy(&sense, bp->b_un.b_addr, count);
3256 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3257 		scsipkt->pkt_resid = 0;
3258 	}
3259 
3260 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3261 	    "Scsi_pkt completion reason %x\n",
3262 	    scsipkt->pkt_reason);
3263 
3264 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3265 	    scsipkt->pkt_comp != NULL)
3266 		/* scsi callback required */
3267 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3268 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3269 		    TQ_SLEEP) == NULL)
3270 			/* Scheduling the callback failed */
3271 			return (TRAN_BUSY);
3272 	return (TRAN_ACCEPT);
3273 }
3274 
3275 /*
3276  * SATA translate command: Test Unit Ready
3277  * At the moment this is an emulated command (ATA version for SATA hard disks).
3278  * May be translated into Check Power Mode command in the future
3279  *
3280  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3281  */
3282 static int
3283 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx)
3284 {
3285 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3286 	struct scsi_extended_sense *sense;
3287 	int power_state;
3288 	int rval, reason;
3289 
3290 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3291 
3292 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3293 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3294 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3295 		return (rval);
3296 	}
3297 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3298 
3299 	/* At this moment, emulate it rather than execute anything */
3300 	power_state = SATA_PWRMODE_ACTIVE;
3301 
3302 	scsipkt->pkt_reason = CMD_CMPLT;
3303 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3304 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3305 
3306 	switch (power_state) {
3307 	case SATA_PWRMODE_ACTIVE:
3308 	case SATA_PWRMODE_IDLE:
3309 		*scsipkt->pkt_scbp = STATUS_GOOD;
3310 		break;
3311 	default:
3312 		/* PWR mode standby */
3313 		*scsipkt->pkt_scbp = STATUS_CHECK;
3314 		sense = sata_arq_sense(spx);
3315 		sense->es_key = KEY_NOT_READY;
3316 		sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY;
3317 		break;
3318 	}
3319 
3320 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3321 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3322 
3323 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3324 	    scsipkt->pkt_comp != NULL)
3325 		/* scsi callback required */
3326 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3327 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3328 		    TQ_SLEEP) == NULL)
3329 			/* Scheduling the callback failed */
3330 			return (TRAN_BUSY);
3331 
3332 	return (TRAN_ACCEPT);
3333 }
3334 
3335 
3336 /*
3337  * SATA translate command: Start Stop Unit
3338  * Translation depends on a command:
3339  *	Start Unit translated into Idle Immediate
3340  *	Stop Unit translated into Standby Immediate
3341  *	Unload Media / NOT SUPPORTED YET
3342  *	Load Media / NOT SUPPROTED YET
3343  * Power condition bits are ignored, so is Immediate bit
3344  * Requesting synchronous execution.
3345  *
3346  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
3347  * appropriate values in scsi_pkt fields.
3348  */
3349 static int
3350 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx)
3351 {
3352 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3353 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3354 	struct scsi_extended_sense *sense;
3355 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3356 	int cport = SATA_TXLT_CPORT(spx);
3357 	int rval, reason;
3358 	int synch;
3359 
3360 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3361 	    "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1);
3362 
3363 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3364 
3365 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3366 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3367 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3368 		return (rval);
3369 	}
3370 
3371 	if (scsipkt->pkt_cdbp[4] & 2) {
3372 		/* Load/Unload Media - invalid request */
3373 		*scsipkt->pkt_scbp = STATUS_CHECK;
3374 		sense = sata_arq_sense(spx);
3375 		sense->es_key = KEY_ILLEGAL_REQUEST;
3376 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3377 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3378 
3379 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3380 		    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3381 
3382 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3383 		    scsipkt->pkt_comp != NULL)
3384 			/* scsi callback required */
3385 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3386 			    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3387 			    TQ_SLEEP) == NULL)
3388 				/* Scheduling the callback failed */
3389 				return (TRAN_BUSY);
3390 
3391 		return (TRAN_ACCEPT);
3392 	}
3393 	scmd->satacmd_addr_type = 0;
3394 	scmd->satacmd_sec_count_lsb = 0;
3395 	scmd->satacmd_lba_low_lsb = 0;
3396 	scmd->satacmd_lba_mid_lsb = 0;
3397 	scmd->satacmd_lba_high_lsb = 0;
3398 	scmd->satacmd_features_reg = 0;
3399 	scmd->satacmd_device_reg = 0;
3400 	scmd->satacmd_status_reg = 0;
3401 	if (scsipkt->pkt_cdbp[4] & 1) {
3402 		/* Start Unit */
3403 		scmd->satacmd_cmd_reg = SATAC_IDLE_IM;
3404 	} else {
3405 		/* Stop Unit */
3406 		scmd->satacmd_cmd_reg = SATAC_STANDBY_IM;
3407 	}
3408 
3409 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
3410 		/* Need to set-up a callback function */
3411 		spx->txlt_sata_pkt->satapkt_comp =
3412 		    sata_txlt_nodata_cmd_completion;
3413 		synch = FALSE;
3414 	} else {
3415 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
3416 		synch = TRUE;
3417 	}
3418 
3419 	/* Transfer command to HBA */
3420 	if (sata_hba_start(spx, &rval) != 0) {
3421 		/* Pkt not accepted for execution */
3422 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3423 		return (rval);
3424 	}
3425 
3426 	/*
3427 	 * If execution is non-synchronous,
3428 	 * a callback function will handle potential errors, translate
3429 	 * the response and will do a callback to a target driver.
3430 	 * If it was synchronous, check execution status using the same
3431 	 * framework callback.
3432 	 */
3433 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3434 	if (synch) {
3435 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3436 		    "synchronous execution status %x\n",
3437 		    spx->txlt_sata_pkt->satapkt_reason);
3438 
3439 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
3440 	}
3441 	return (TRAN_ACCEPT);
3442 
3443 }
3444 
3445 
3446 /*
3447  * SATA translate command:  Read Capacity.
3448  * Emulated command for SATA disks.
3449  * Capacity is retrieved from cached Idenifty Device data.
3450  * Identify Device data shows effective disk capacity, not the native
3451  * capacity, which may be limitted by Set Max Address command.
3452  * This is ATA version for SATA hard disks.
3453  *
3454  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3455  */
3456 static int
3457 sata_txlt_read_capacity(sata_pkt_txlate_t *spx)
3458 {
3459 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3460 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3461 	sata_drive_info_t *sdinfo;
3462 	uint64_t val;
3463 	uchar_t *rbuf;
3464 	int rval, reason;
3465 
3466 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3467 	    "sata_txlt_read_capacity: ", NULL);
3468 
3469 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3470 
3471 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3472 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3473 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3474 		return (rval);
3475 	}
3476 
3477 	scsipkt->pkt_reason = CMD_CMPLT;
3478 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3479 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3480 	*scsipkt->pkt_scbp = STATUS_GOOD;
3481 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3482 		/*
3483 		 * Because it is fully emulated command storing data
3484 		 * programatically in the specified buffer, release
3485 		 * preallocated DMA resources before storing data in the buffer,
3486 		 * so no unwanted DMA sync would take place.
3487 		 */
3488 		sata_scsi_dmafree(NULL, scsipkt);
3489 
3490 		sdinfo = sata_get_device_info(
3491 		    spx->txlt_sata_hba_inst,
3492 		    &spx->txlt_sata_pkt->satapkt_device);
3493 		/* Last logical block address */
3494 		val = sdinfo->satadrv_capacity - 1;
3495 		rbuf = (uchar_t *)bp->b_un.b_addr;
3496 		/* Need to swap endians to match scsi format */
3497 		rbuf[0] = (val >> 24) & 0xff;
3498 		rbuf[1] = (val >> 16) & 0xff;
3499 		rbuf[2] = (val >> 8) & 0xff;
3500 		rbuf[3] = val & 0xff;
3501 		/* block size - always 512 bytes, for now */
3502 		rbuf[4] = 0;
3503 		rbuf[5] = 0;
3504 		rbuf[6] = 0x02;
3505 		rbuf[7] = 0;
3506 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3507 		scsipkt->pkt_resid = 0;
3508 
3509 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n",
3510 		    sdinfo->satadrv_capacity -1);
3511 	}
3512 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3513 	/*
3514 	 * If a callback was requested, do it now.
3515 	 */
3516 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3517 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3518 
3519 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3520 	    scsipkt->pkt_comp != NULL)
3521 		/* scsi callback required */
3522 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3523 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3524 		    TQ_SLEEP) == NULL)
3525 			/* Scheduling the callback failed */
3526 			return (TRAN_BUSY);
3527 
3528 	return (TRAN_ACCEPT);
3529 }
3530 
3531 /*
3532  * SATA translate command: Mode Sense.
3533  * Translated into appropriate SATA command or emulated.
3534  * Saved Values Page Control (03) are not supported.
3535  *
3536  * NOTE: only caching mode sense page is currently implemented.
3537  *
3538  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3539  */
3540 
3541 static int
3542 sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
3543 {
3544 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
3545 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3546 	sata_drive_info_t *sdinfo;
3547 	sata_id_t *sata_id;
3548 	struct scsi_extended_sense *sense;
3549 	int 		len, bdlen, count, alc_len;
3550 	int		pc;	/* Page Control code */
3551 	uint8_t		*buf;	/* mode sense buffer */
3552 	int		rval, reason;
3553 
3554 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3555 	    "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
3556 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
3557 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
3558 
3559 	buf = kmem_zalloc(1024, KM_SLEEP);
3560 
3561 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3562 
3563 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3564 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3565 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3566 		kmem_free(buf, 1024);
3567 		return (rval);
3568 	}
3569 
3570 	scsipkt->pkt_reason = CMD_CMPLT;
3571 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3572 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3573 
3574 	pc = scsipkt->pkt_cdbp[2] >> 6;
3575 
3576 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3577 		/*
3578 		 * Because it is fully emulated command storing data
3579 		 * programatically in the specified buffer, release
3580 		 * preallocated DMA resources before storing data in the buffer,
3581 		 * so no unwanted DMA sync would take place.
3582 		 */
3583 		sata_scsi_dmafree(NULL, scsipkt);
3584 
3585 		len = 0;
3586 		bdlen = 0;
3587 		if (!(scsipkt->pkt_cdbp[1] & 8)) {
3588 			if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
3589 			    (scsipkt->pkt_cdbp[0] & 0x10))
3590 				bdlen = 16;
3591 			else
3592 				bdlen = 8;
3593 		}
3594 		/* Build mode parameter header */
3595 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3596 			/* 4-byte mode parameter header */
3597 			buf[len++] = 0;   	/* mode data length */
3598 			buf[len++] = 0;		/* medium type */
3599 			buf[len++] = 0;		/* dev-specific param */
3600 			buf[len++] = bdlen;	/* Block Descriptor length */
3601 		} else {
3602 			/* 8-byte mode parameter header */
3603 			buf[len++] = 0;		/* mode data length */
3604 			buf[len++] = 0;
3605 			buf[len++] = 0;		/* medium type */
3606 			buf[len++] = 0;		/* dev-specific param */
3607 			if (bdlen == 16)
3608 				buf[len++] = 1;	/* long lba descriptor */
3609 			else
3610 				buf[len++] = 0;
3611 			buf[len++] = 0;
3612 			buf[len++] = 0;		/* Block Descriptor length */
3613 			buf[len++] = bdlen;
3614 		}
3615 
3616 		sdinfo = sata_get_device_info(
3617 		    spx->txlt_sata_hba_inst,
3618 		    &spx->txlt_sata_pkt->satapkt_device);
3619 
3620 		/* Build block descriptor only if not disabled (DBD) */
3621 		if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
3622 			/* Block descriptor - direct-access device format */
3623 			if (bdlen == 8) {
3624 				/* build regular block descriptor */
3625 				buf[len++] =
3626 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
3627 				buf[len++] =
3628 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
3629 				buf[len++] =
3630 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
3631 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
3632 				buf[len++] = 0; /* density code */
3633 				buf[len++] = 0;
3634 				if (sdinfo->satadrv_type ==
3635 				    SATA_DTYPE_ATADISK)
3636 					buf[len++] = 2;
3637 				else
3638 					/* ATAPI */
3639 					buf[len++] = 8;
3640 				buf[len++] = 0;
3641 			} else if (bdlen == 16) {
3642 				/* Long LBA Accepted */
3643 				/* build long lba block descriptor */
3644 #ifndef __lock_lint
3645 				buf[len++] =
3646 				    (sdinfo->satadrv_capacity >> 56) & 0xff;
3647 				buf[len++] =
3648 				    (sdinfo->satadrv_capacity >> 48) & 0xff;
3649 				buf[len++] =
3650 				    (sdinfo->satadrv_capacity >> 40) & 0xff;
3651 				buf[len++] =
3652 				    (sdinfo->satadrv_capacity >> 32) & 0xff;
3653 #endif
3654 				buf[len++] =
3655 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
3656 				buf[len++] =
3657 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
3658 				buf[len++] =
3659 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
3660 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
3661 				buf[len++] = 0;
3662 				buf[len++] = 0; /* density code */
3663 				buf[len++] = 0;
3664 				buf[len++] = 0;
3665 				if (sdinfo->satadrv_type ==
3666 				    SATA_DTYPE_ATADISK)
3667 					buf[len++] = 2;
3668 				else
3669 					/* ATAPI */
3670 					buf[len++] = 8;
3671 				buf[len++] = 0;
3672 			}
3673 		}
3674 
3675 		sata_id = &sdinfo->satadrv_id;
3676 
3677 		/*
3678 		 * Add requested pages.
3679 		 * Page 3 and 4 are obsolete and we are not supporting them.
3680 		 * We deal now with:
3681 		 * caching (read/write cache control).
3682 		 * We should eventually deal with following mode pages:
3683 		 * error recovery  (0x01),
3684 		 * power condition (0x1a),
3685 		 * exception control page (enables SMART) (0x1c),
3686 		 * enclosure management (ses),
3687 		 * protocol-specific port mode (port control).
3688 		 */
3689 		switch (scsipkt->pkt_cdbp[2] & 0x3f) {
3690 		case MODEPAGE_RW_ERRRECOV:
3691 			/* DAD_MODE_ERR_RECOV */
3692 			/* R/W recovery */
3693 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
3694 			break;
3695 		case MODEPAGE_CACHING:
3696 			/* DAD_MODE_CACHE */
3697 			/* Reject not supported request for saved parameters */
3698 			if (pc == 3) {
3699 				*scsipkt->pkt_scbp = STATUS_CHECK;
3700 				sense = sata_arq_sense(spx);
3701 				sense->es_key = KEY_ILLEGAL_REQUEST;
3702 				sense->es_add_code =
3703 				    SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
3704 				goto done;
3705 			}
3706 
3707 			/* caching */
3708 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
3709 			break;
3710 		case MODEPAGE_INFO_EXCPT:
3711 			/* exception cntrl */
3712 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
3713 				len += sata_build_msense_page_1c(sdinfo, pc,
3714 				    buf+len);
3715 			}
3716 			else
3717 				goto err;
3718 			break;
3719 		case MODEPAGE_POWER_COND:
3720 			/* DAD_MODE_POWER_COND */
3721 			/* power condition */
3722 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
3723 			break;
3724 
3725 		case MODEPAGE_ACOUSTIC_MANAG:
3726 			/* acoustic management */
3727 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
3728 			break;
3729 		case MODEPAGE_ALLPAGES:
3730 			/* all pages */
3731 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
3732 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
3733 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
3734 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
3735 				len += sata_build_msense_page_1c(sdinfo, pc,
3736 				    buf+len);
3737 			}
3738 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
3739 			break;
3740 		default:
3741 		err:
3742 			/* Invalid request */
3743 			*scsipkt->pkt_scbp = STATUS_CHECK;
3744 			sense = sata_arq_sense(spx);
3745 			sense->es_key = KEY_ILLEGAL_REQUEST;
3746 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3747 			goto done;
3748 		}
3749 
3750 		/* fix total mode data length */
3751 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3752 			/* 4-byte mode parameter header */
3753 			buf[0] = len - 1;   	/* mode data length */
3754 		} else {
3755 			buf[0] = (len -2) >> 8;
3756 			buf[1] = (len -2) & 0xff;
3757 		}
3758 
3759 
3760 		/* Check allocation length */
3761 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3762 			alc_len = scsipkt->pkt_cdbp[4];
3763 		} else {
3764 			alc_len = scsipkt->pkt_cdbp[7];
3765 			alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
3766 		}
3767 		/*
3768 		 * We do not check for possible parameters truncation
3769 		 * (alc_len < len) assuming that the target driver works
3770 		 * correctly. Just avoiding overrun.
3771 		 * Copy no more than requested and possible, buffer-wise.
3772 		 */
3773 		count = MIN(alc_len, len);
3774 		count = MIN(bp->b_bcount, count);
3775 		bcopy(buf, bp->b_un.b_addr, count);
3776 
3777 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3778 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
3779 	}
3780 	*scsipkt->pkt_scbp = STATUS_GOOD;
3781 done:
3782 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3783 	(void) kmem_free(buf, 1024);
3784 
3785 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3786 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3787 
3788 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3789 	    scsipkt->pkt_comp != NULL)
3790 		/* scsi callback required */
3791 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3792 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3793 		    TQ_SLEEP) == NULL)
3794 			/* Scheduling the callback failed */
3795 			return (TRAN_BUSY);
3796 
3797 	return (TRAN_ACCEPT);
3798 }
3799 
3800 
3801 /*
3802  * SATA translate command: Mode Select.
3803  * Translated into appropriate SATA command or emulated.
3804  * Saving parameters is not supported.
3805  * Changing device capacity is not supported (although theoretically
3806  * possible by executing SET FEATURES/SET MAX ADDRESS)
3807  *
3808  * Assumption is that the target driver is working correctly.
3809  *
3810  * More than one SATA command may be executed to perform operations specified
3811  * by mode select pages. The first error terminates further execution.
3812  * Operations performed successully are not backed-up in such case.
3813  *
3814  * NOTE: only caching mode select page is implemented.
3815  * Caching setup is remembered so it could be re-stored in case of
3816  * an unexpected device reset.
3817  *
3818  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3819  */
3820 
3821 static int
3822 sata_txlt_mode_select(sata_pkt_txlate_t *spx)
3823 {
3824 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3825 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3826 	struct scsi_extended_sense *sense;
3827 	int len, pagelen, count, pllen;
3828 	uint8_t *buf;	/* mode select buffer */
3829 	int rval, stat, reason;
3830 	uint_t nointr_flag;
3831 	int dmod = 0;
3832 
3833 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3834 	    "sata_txlt_mode_select, pc %x page code 0x%02x\n",
3835 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
3836 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
3837 
3838 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3839 
3840 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3841 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3842 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3843 		return (rval);
3844 	}
3845 
3846 	rval = TRAN_ACCEPT;
3847 
3848 	scsipkt->pkt_reason = CMD_CMPLT;
3849 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3850 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3851 
3852 	/* Reject not supported request */
3853 	if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
3854 		*scsipkt->pkt_scbp = STATUS_CHECK;
3855 		sense = sata_arq_sense(spx);
3856 		sense->es_key = KEY_ILLEGAL_REQUEST;
3857 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3858 		goto done;
3859 	}
3860 
3861 	if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
3862 		pllen = scsipkt->pkt_cdbp[4];
3863 	} else {
3864 		pllen = scsipkt->pkt_cdbp[7];
3865 		pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
3866 	}
3867 
3868 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
3869 
3870 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
3871 		buf = (uint8_t *)bp->b_un.b_addr;
3872 		count = MIN(bp->b_bcount, pllen);
3873 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3874 		scsipkt->pkt_resid = 0;
3875 		pllen = count;
3876 
3877 		/*
3878 		 * Check the header to skip the block descriptor(s) - we
3879 		 * do not support setting device capacity.
3880 		 * Existing macros do not recognize long LBA dscriptor,
3881 		 * hence manual calculation.
3882 		 */
3883 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
3884 			/* 6-bytes CMD, 4 bytes header */
3885 			if (count <= 4)
3886 				goto done;		/* header only */
3887 			len = buf[3] + 4;
3888 		} else {
3889 			/* 10-bytes CMD, 8 bytes header */
3890 			if (count <= 8)
3891 				goto done;		/* header only */
3892 			len = buf[6];
3893 			len = (len << 8) + buf[7] + 8;
3894 		}
3895 		if (len >= count)
3896 			goto done;	/* header + descriptor(s) only */
3897 
3898 		pllen -= len;		/* remaining data length */
3899 
3900 		/*
3901 		 * We may be executing SATA command and want to execute it
3902 		 * in SYNCH mode, regardless of scsi_pkt setting.
3903 		 * Save scsi_pkt setting and indicate SYNCH mode
3904 		 */
3905 		nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
3906 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3907 		    scsipkt->pkt_comp != NULL) {
3908 			scsipkt->pkt_flags |= FLAG_NOINTR;
3909 		}
3910 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
3911 
3912 		/*
3913 		 * len is now the offset to a first mode select page
3914 		 * Process all pages
3915 		 */
3916 		while (pllen > 0) {
3917 			switch ((int)buf[len]) {
3918 			case MODEPAGE_CACHING:
3919 				/* No support for SP (saving) */
3920 				if (scsipkt->pkt_cdbp[1] & 0x01) {
3921 					*scsipkt->pkt_scbp = STATUS_CHECK;
3922 					sense = sata_arq_sense(spx);
3923 					sense->es_key = KEY_ILLEGAL_REQUEST;
3924 					sense->es_add_code =
3925 					    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3926 					goto done;
3927 				}
3928 				stat = sata_mode_select_page_8(spx,
3929 				    (struct mode_cache_scsi3 *)&buf[len],
3930 				    pllen, &pagelen, &rval, &dmod);
3931 				/*
3932 				 * The pagelen value indicates the number of
3933 				 * parameter bytes already processed.
3934 				 * The rval is the return value from
3935 				 * sata_tran_start().
3936 				 * The stat indicates the overall status of
3937 				 * the operation(s).
3938 				 */
3939 				if (stat != SATA_SUCCESS)
3940 					/*
3941 					 * Page processing did not succeed -
3942 					 * all error info is already set-up,
3943 					 * just return
3944 					 */
3945 					pllen = 0; /* this breaks the loop */
3946 				else {
3947 					len += pagelen;
3948 					pllen -= pagelen;
3949 				}
3950 				break;
3951 
3952 			case MODEPAGE_INFO_EXCPT:
3953 				stat = sata_mode_select_page_1c(spx,
3954 				    (struct mode_info_excpt_page *)&buf[len],
3955 				    pllen, &pagelen, &rval, &dmod);
3956 				/*
3957 				 * The pagelen value indicates the number of
3958 				 * parameter bytes already processed.
3959 				 * The rval is the return value from
3960 				 * sata_tran_start().
3961 				 * The stat indicates the overall status of
3962 				 * the operation(s).
3963 				 */
3964 				if (stat != SATA_SUCCESS)
3965 					/*
3966 					 * Page processing did not succeed -
3967 					 * all error info is already set-up,
3968 					 * just return
3969 					 */
3970 					pllen = 0; /* this breaks the loop */
3971 				else {
3972 					len += pagelen;
3973 					pllen -= pagelen;
3974 				}
3975 				break;
3976 
3977 			case MODEPAGE_ACOUSTIC_MANAG:
3978 				stat = sata_mode_select_page_30(spx,
3979 				    (struct mode_acoustic_management *)
3980 				    &buf[len], pllen, &pagelen, &rval, &dmod);
3981 				/*
3982 				 * The pagelen value indicates the number of
3983 				 * parameter bytes already processed.
3984 				 * The rval is the return value from
3985 				 * sata_tran_start().
3986 				 * The stat indicates the overall status of
3987 				 * the operation(s).
3988 				 */
3989 				if (stat != SATA_SUCCESS)
3990 					/*
3991 					 * Page processing did not succeed -
3992 					 * all error info is already set-up,
3993 					 * just return
3994 					 */
3995 					pllen = 0; /* this breaks the loop */
3996 				else {
3997 					len += pagelen;
3998 					pllen -= pagelen;
3999 				}
4000 
4001 				break;
4002 			default:
4003 				*scsipkt->pkt_scbp = STATUS_CHECK;
4004 				sense = sata_arq_sense(spx);
4005 				sense->es_key = KEY_ILLEGAL_REQUEST;
4006 				sense->es_add_code =
4007 				    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
4008 				goto done;
4009 			}
4010 		}
4011 	}
4012 done:
4013 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4014 	/*
4015 	 * If device parameters were modified, fetch and store the new
4016 	 * Identify Device data. Since port mutex could have been released
4017 	 * for accessing HBA driver, we need to re-check device existence.
4018 	 */
4019 	if (dmod != 0) {
4020 		sata_drive_info_t new_sdinfo, *sdinfo;
4021 		int rv = 0;
4022 
4023 		/*
4024 		 * Following statement has to be changed if this function is
4025 		 * used for devices other than SATA hard disks.
4026 		 */
4027 		new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
4028 
4029 		new_sdinfo.satadrv_addr =
4030 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr;
4031 		rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
4032 		    &new_sdinfo);
4033 
4034 		mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4035 		/*
4036 		 * Since port mutex could have been released when
4037 		 * accessing HBA driver, we need to re-check that the
4038 		 * framework still holds the device info structure.
4039 		 */
4040 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4041 		    &spx->txlt_sata_pkt->satapkt_device);
4042 		if (sdinfo != NULL) {
4043 			/*
4044 			 * Device still has info structure in the
4045 			 * sata framework. Copy newly fetched info
4046 			 */
4047 			if (rv == 0) {
4048 				sdinfo->satadrv_id = new_sdinfo.satadrv_id;
4049 				sata_save_drive_settings(sdinfo);
4050 			} else {
4051 				/*
4052 				 * Could not fetch new data - invalidate
4053 				 * sata_drive_info. That makes device
4054 				 * unusable.
4055 				 */
4056 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
4057 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
4058 			}
4059 		}
4060 		if (rv != 0 || sdinfo == NULL) {
4061 			/*
4062 			 * This changes the overall mode select completion
4063 			 * reason to a failed one !!!!!
4064 			 */
4065 			*scsipkt->pkt_scbp = STATUS_CHECK;
4066 			sense = sata_arq_sense(spx);
4067 			scsipkt->pkt_reason = CMD_INCOMPLETE;
4068 			rval = TRAN_ACCEPT;
4069 		}
4070 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4071 	}
4072 	/* Restore the scsi pkt flags */
4073 	scsipkt->pkt_flags &= ~FLAG_NOINTR;
4074 	scsipkt->pkt_flags |= nointr_flag;
4075 
4076 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4077 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4078 
4079 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4080 	    scsipkt->pkt_comp != NULL)
4081 		/* scsi callback required */
4082 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4083 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4084 		    TQ_SLEEP) == NULL)
4085 			/* Scheduling the callback failed */
4086 			return (TRAN_BUSY);
4087 
4088 	return (rval);
4089 }
4090 
4091 
4092 
4093 /*
4094  * Translate command: Log Sense
4095  */
4096 static 	int
4097 sata_txlt_log_sense(sata_pkt_txlate_t *spx)
4098 {
4099 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4100 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4101 	sata_drive_info_t *sdinfo;
4102 	struct scsi_extended_sense *sense;
4103 	int 		len, count, alc_len;
4104 	int		pc;	/* Page Control code */
4105 	int		page_code;	/* Page code */
4106 	uint8_t		*buf;	/* log sense buffer */
4107 	int		rval, reason;
4108 #define	MAX_LOG_SENSE_PAGE_SIZE	512
4109 
4110 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4111 	    "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
4112 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4113 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4114 
4115 	buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
4116 
4117 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4118 
4119 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4120 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4121 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4122 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4123 		return (rval);
4124 	}
4125 
4126 	scsipkt->pkt_reason = CMD_CMPLT;
4127 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4128 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4129 
4130 	pc = scsipkt->pkt_cdbp[2] >> 6;
4131 	page_code = scsipkt->pkt_cdbp[2] & 0x3f;
4132 
4133 	/* Reject not supported request for all but cumulative values */
4134 	switch (pc) {
4135 	case PC_CUMULATIVE_VALUES:
4136 		break;
4137 	default:
4138 		*scsipkt->pkt_scbp = STATUS_CHECK;
4139 		sense = sata_arq_sense(spx);
4140 		sense->es_key = KEY_ILLEGAL_REQUEST;
4141 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4142 		goto done;
4143 	}
4144 
4145 	switch (page_code) {
4146 	case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
4147 	case PAGE_CODE_SELF_TEST_RESULTS:
4148 	case PAGE_CODE_INFORMATION_EXCEPTIONS:
4149 	case PAGE_CODE_SMART_READ_DATA:
4150 		break;
4151 	default:
4152 		*scsipkt->pkt_scbp = STATUS_CHECK;
4153 		sense = sata_arq_sense(spx);
4154 		sense->es_key = KEY_ILLEGAL_REQUEST;
4155 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4156 		goto done;
4157 	}
4158 
4159 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4160 		/*
4161 		 * Because log sense uses local buffers for data retrieval from
4162 		 * the devices and sets the data programatically in the
4163 		 * original specified buffer, release preallocated DMA
4164 		 * resources before storing data in the original buffer,
4165 		 * so no unwanted DMA sync would take place.
4166 		 */
4167 		sata_id_t *sata_id;
4168 
4169 		sata_scsi_dmafree(NULL, scsipkt);
4170 
4171 		len = 0;
4172 
4173 		/* Build log parameter header */
4174 		buf[len++] = page_code;	/* page code as in the CDB */
4175 		buf[len++] = 0;		/* reserved */
4176 		buf[len++] = 0;		/* Zero out page length for now (MSB) */
4177 		buf[len++] = 0;		/* (LSB) */
4178 
4179 		sdinfo = sata_get_device_info(
4180 		    spx->txlt_sata_hba_inst,
4181 		    &spx->txlt_sata_pkt->satapkt_device);
4182 
4183 
4184 		/*
4185 		 * Add requested pages.
4186 		 */
4187 		switch (page_code) {
4188 		case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
4189 			len = sata_build_lsense_page_0(sdinfo, buf + len);
4190 			break;
4191 		case PAGE_CODE_SELF_TEST_RESULTS:
4192 			sata_id = &sdinfo->satadrv_id;
4193 			if ((! (sata_id->ai_cmdset84 &
4194 			    SATA_SMART_SELF_TEST_SUPPORTED)) ||
4195 			    (! (sata_id->ai_features87 &
4196 			    SATA_SMART_SELF_TEST_SUPPORTED))) {
4197 				*scsipkt->pkt_scbp = STATUS_CHECK;
4198 				sense = sata_arq_sense(spx);
4199 				sense->es_key = KEY_ILLEGAL_REQUEST;
4200 				sense->es_add_code =
4201 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4202 
4203 				goto done;
4204 			}
4205 			len = sata_build_lsense_page_10(sdinfo, buf + len,
4206 			    spx->txlt_sata_hba_inst);
4207 			break;
4208 		case PAGE_CODE_INFORMATION_EXCEPTIONS:
4209 			sata_id = &sdinfo->satadrv_id;
4210 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
4211 				*scsipkt->pkt_scbp = STATUS_CHECK;
4212 				sense = sata_arq_sense(spx);
4213 				sense->es_key = KEY_ILLEGAL_REQUEST;
4214 				sense->es_add_code =
4215 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4216 
4217 				goto done;
4218 			}
4219 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
4220 				*scsipkt->pkt_scbp = STATUS_CHECK;
4221 				sense = sata_arq_sense(spx);
4222 				sense->es_key = KEY_ABORTED_COMMAND;
4223 				sense->es_add_code =
4224 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
4225 				sense->es_qual_code =
4226 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
4227 
4228 				goto done;
4229 			}
4230 
4231 			len = sata_build_lsense_page_2f(sdinfo, buf + len,
4232 			    spx->txlt_sata_hba_inst);
4233 			break;
4234 		case PAGE_CODE_SMART_READ_DATA:
4235 			sata_id = &sdinfo->satadrv_id;
4236 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
4237 				*scsipkt->pkt_scbp = STATUS_CHECK;
4238 				sense = sata_arq_sense(spx);
4239 				sense->es_key = KEY_ILLEGAL_REQUEST;
4240 				sense->es_add_code =
4241 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4242 
4243 				goto done;
4244 			}
4245 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
4246 				*scsipkt->pkt_scbp = STATUS_CHECK;
4247 				sense = sata_arq_sense(spx);
4248 				sense->es_key = KEY_ABORTED_COMMAND;
4249 				sense->es_add_code =
4250 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
4251 				sense->es_qual_code =
4252 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
4253 
4254 				goto done;
4255 			}
4256 
4257 			/* This page doesn't include a page header */
4258 			len = sata_build_lsense_page_30(sdinfo, buf,
4259 			    spx->txlt_sata_hba_inst);
4260 			goto no_header;
4261 		default:
4262 			/* Invalid request */
4263 			*scsipkt->pkt_scbp = STATUS_CHECK;
4264 			sense = sata_arq_sense(spx);
4265 			sense->es_key = KEY_ILLEGAL_REQUEST;
4266 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4267 			goto done;
4268 		}
4269 
4270 		/* set parameter log sense data length */
4271 		buf[2] = len >> 8;	/* log sense length (MSB) */
4272 		buf[3] = len & 0xff;	/* log sense length (LSB) */
4273 
4274 		len += SCSI_LOG_PAGE_HDR_LEN;
4275 		ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
4276 
4277 no_header:
4278 		/* Check allocation length */
4279 		alc_len = scsipkt->pkt_cdbp[7];
4280 		alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
4281 
4282 		/*
4283 		 * We do not check for possible parameters truncation
4284 		 * (alc_len < len) assuming that the target driver works
4285 		 * correctly. Just avoiding overrun.
4286 		 * Copy no more than requested and possible, buffer-wise.
4287 		 */
4288 		count = MIN(alc_len, len);
4289 		count = MIN(bp->b_bcount, count);
4290 		bcopy(buf, bp->b_un.b_addr, count);
4291 
4292 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4293 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
4294 	}
4295 	*scsipkt->pkt_scbp = STATUS_GOOD;
4296 done:
4297 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4298 	(void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4299 
4300 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4301 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4302 
4303 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4304 	    scsipkt->pkt_comp != NULL)
4305 		/* scsi callback required */
4306 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4307 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4308 		    TQ_SLEEP) == NULL)
4309 			/* Scheduling the callback failed */
4310 			return (TRAN_BUSY);
4311 
4312 	return (TRAN_ACCEPT);
4313 }
4314 
4315 /*
4316  * Translate command: Log Select
4317  * Not implemented at this time - returns invalid command response.
4318  */
4319 static 	int
4320 sata_txlt_log_select(sata_pkt_txlate_t *spx)
4321 {
4322 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4323 	    "sata_txlt_log_select\n", NULL);
4324 
4325 	return (sata_txlt_invalid_command(spx));
4326 }
4327 
4328 
4329 /*
4330  * Translate command: Read (various types).
4331  * Translated into appropriate type of ATA READ command
4332  * for SATA hard disks.
4333  * Both the device capabilities and requested operation mode are
4334  * considered.
4335  *
4336  * Following scsi cdb fields are ignored:
4337  * rdprotect, dpo, fua, fua_nv, group_number.
4338  *
4339  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
4340  * enable variable sata_func_enable), the capability of the controller and
4341  * capability of a device are checked and if both support queueing, read
4342  * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
4343  * command rather than plain READ_XXX command.
4344  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
4345  * both the controller and device suport such functionality, the read
4346  * request will be translated to READ_FPDMA_QUEUED command.
4347  * In both cases the maximum queue depth is derived as minimum of:
4348  * HBA capability,device capability and sata_max_queue_depth variable setting.
4349  * The value passed to HBA driver is decremented by 1, because only 5 bits are
4350  * used to pass max queue depth value, and the maximum possible queue depth
4351  * is 32.
4352  *
4353  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4354  * appropriate values in scsi_pkt fields.
4355  */
4356 static int
4357 sata_txlt_read(sata_pkt_txlate_t *spx)
4358 {
4359 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4360 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4361 	sata_drive_info_t *sdinfo;
4362 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4363 	int cport = SATA_TXLT_CPORT(spx);
4364 	uint16_t sec_count;
4365 	uint64_t lba;
4366 	int rval, reason;
4367 	int synch;
4368 
4369 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4370 
4371 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4372 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4373 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4374 		return (rval);
4375 	}
4376 
4377 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4378 	    &spx->txlt_sata_pkt->satapkt_device);
4379 
4380 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
4381 	/*
4382 	 * Extract LBA and sector count from scsi CDB.
4383 	 */
4384 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
4385 	case SCMD_READ:
4386 		/* 6-byte scsi read cmd : 0x08 */
4387 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
4388 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
4389 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4390 		sec_count = scsipkt->pkt_cdbp[4];
4391 		/* sec_count 0 will be interpreted as 256 by a device */
4392 		break;
4393 	case SCMD_READ_G1:
4394 		/* 10-bytes scsi read command : 0x28 */
4395 		lba = scsipkt->pkt_cdbp[2];
4396 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4397 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4398 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4399 		sec_count = scsipkt->pkt_cdbp[7];
4400 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4401 		break;
4402 	case SCMD_READ_G5:
4403 		/* 12-bytes scsi read command : 0xA8 */
4404 		lba = scsipkt->pkt_cdbp[2];
4405 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4406 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4407 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4408 		sec_count = scsipkt->pkt_cdbp[6];
4409 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
4410 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4411 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
4412 		break;
4413 	case SCMD_READ_G4:
4414 		/* 16-bytes scsi read command : 0x88 */
4415 		lba = scsipkt->pkt_cdbp[2];
4416 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4417 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4418 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4419 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
4420 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
4421 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
4422 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
4423 		sec_count = scsipkt->pkt_cdbp[10];
4424 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
4425 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
4426 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
4427 		break;
4428 	default:
4429 		/* Unsupported command */
4430 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4431 		return (sata_txlt_invalid_command(spx));
4432 	}
4433 
4434 	/*
4435 	 * Check if specified address exceeds device capacity
4436 	 */
4437 	if ((lba >= sdinfo->satadrv_capacity) ||
4438 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
4439 		/* LBA out of range */
4440 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4441 		return (sata_txlt_lba_out_of_range(spx));
4442 	}
4443 
4444 	/*
4445 	 * For zero-length transfer, emulate good completion of the command
4446 	 * (reasons for rejecting the command were already checked).
4447 	 * No DMA resources were allocated.
4448 	 */
4449 	if (spx->txlt_dma_cookie_list == NULL) {
4450 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4451 		return (sata_emul_rw_completion(spx));
4452 	}
4453 
4454 	/*
4455 	 * Build cmd block depending on the device capability and
4456 	 * requested operation mode.
4457 	 * Do not bother with non-dma mode - we are working only with
4458 	 * devices supporting DMA.
4459 	 */
4460 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
4461 	scmd->satacmd_device_reg = SATA_ADH_LBA;
4462 	scmd->satacmd_cmd_reg = SATAC_READ_DMA;
4463 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
4464 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4465 		scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT;
4466 		scmd->satacmd_sec_count_msb = sec_count >> 8;
4467 #ifndef __lock_lint
4468 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
4469 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
4470 		scmd->satacmd_lba_high_msb = lba >> 40;
4471 #endif
4472 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
4473 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
4474 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
4475 	}
4476 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
4477 	scmd->satacmd_lba_low_lsb = lba & 0xff;
4478 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
4479 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
4480 	scmd->satacmd_features_reg = 0;
4481 	scmd->satacmd_status_reg = 0;
4482 	scmd->satacmd_error_reg = 0;
4483 
4484 	/*
4485 	 * Check if queueing commands should be used and switch
4486 	 * to appropriate command if possible
4487 	 */
4488 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
4489 		boolean_t using_queuing;
4490 
4491 		/* Queuing supported by controller and device? */
4492 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
4493 		    (sdinfo->satadrv_features_support &
4494 		    SATA_DEV_F_NCQ) &&
4495 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4496 		    SATA_CTLF_NCQ)) {
4497 			using_queuing = B_TRUE;
4498 
4499 			/* NCQ supported - use FPDMA READ */
4500 			scmd->satacmd_cmd_reg =
4501 			    SATAC_READ_FPDMA_QUEUED;
4502 			scmd->satacmd_features_reg_ext =
4503 			    scmd->satacmd_sec_count_msb;
4504 			scmd->satacmd_sec_count_msb = 0;
4505 		} else if ((sdinfo->satadrv_features_support &
4506 		    SATA_DEV_F_TCQ) &&
4507 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4508 		    SATA_CTLF_QCMD)) {
4509 			using_queuing = B_TRUE;
4510 
4511 			/* Legacy queueing */
4512 			if (sdinfo->satadrv_features_support &
4513 			    SATA_DEV_F_LBA48) {
4514 				scmd->satacmd_cmd_reg =
4515 				    SATAC_READ_DMA_QUEUED_EXT;
4516 				scmd->satacmd_features_reg_ext =
4517 				    scmd->satacmd_sec_count_msb;
4518 				scmd->satacmd_sec_count_msb = 0;
4519 			} else {
4520 				scmd->satacmd_cmd_reg =
4521 				    SATAC_READ_DMA_QUEUED;
4522 			}
4523 		} else	/* NCQ nor legacy queuing not supported */
4524 			using_queuing = B_FALSE;
4525 
4526 		/*
4527 		 * If queuing, the sector count goes in the features register
4528 		 * and the secount count will contain the tag.
4529 		 */
4530 		if (using_queuing) {
4531 			scmd->satacmd_features_reg =
4532 			    scmd->satacmd_sec_count_lsb;
4533 			scmd->satacmd_sec_count_lsb = 0;
4534 			scmd->satacmd_flags.sata_queued = B_TRUE;
4535 
4536 			/* Set-up maximum queue depth */
4537 			scmd->satacmd_flags.sata_max_queue_depth =
4538 			    sdinfo->satadrv_max_queue_depth - 1;
4539 		} else if (sdinfo->satadrv_features_enabled &
4540 		    SATA_DEV_F_E_UNTAGGED_QING) {
4541 			/*
4542 			 * Although NCQ/TCQ is not enabled, untagged queuing
4543 			 * may be still used.
4544 			 * Set-up the maximum untagged queue depth.
4545 			 * Use controller's queue depth from sata_hba_tran.
4546 			 * SATA HBA drivers may ignore this value and rely on
4547 			 * the internal limits.For drivers that do not
4548 			 * ignore untaged queue depth, limit the value to
4549 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
4550 			 * largest value that can be passed via
4551 			 * satacmd_flags.sata_max_queue_depth.
4552 			 */
4553 			scmd->satacmd_flags.sata_max_queue_depth =
4554 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
4555 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
4556 
4557 		} else {
4558 			scmd->satacmd_flags.sata_max_queue_depth = 0;
4559 		}
4560 	} else
4561 		scmd->satacmd_flags.sata_max_queue_depth = 0;
4562 
4563 	SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst,
4564 	    "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n",
4565 	    scmd->satacmd_cmd_reg, lba, sec_count);
4566 
4567 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4568 		/* Need callback function */
4569 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
4570 		synch = FALSE;
4571 	} else
4572 		synch = TRUE;
4573 
4574 	/* Transfer command to HBA */
4575 	if (sata_hba_start(spx, &rval) != 0) {
4576 		/* Pkt not accepted for execution */
4577 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4578 		return (rval);
4579 	}
4580 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4581 	/*
4582 	 * If execution is non-synchronous,
4583 	 * a callback function will handle potential errors, translate
4584 	 * the response and will do a callback to a target driver.
4585 	 * If it was synchronous, check execution status using the same
4586 	 * framework callback.
4587 	 */
4588 	if (synch) {
4589 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4590 		    "synchronous execution status %x\n",
4591 		    spx->txlt_sata_pkt->satapkt_reason);
4592 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
4593 	}
4594 	return (TRAN_ACCEPT);
4595 }
4596 
4597 
4598 /*
4599  * SATA translate command: Write (various types)
4600  * Translated into appropriate type of ATA WRITE command
4601  * for SATA hard disks.
4602  * Both the device capabilities and requested operation mode are
4603  * considered.
4604  *
4605  * Following scsi cdb fields are ignored:
4606  * rwprotect, dpo, fua, fua_nv, group_number.
4607  *
4608  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
4609  * enable variable sata_func_enable), the capability of the controller and
4610  * capability of a device are checked and if both support queueing, write
4611  * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT
4612  * command rather than plain WRITE_XXX command.
4613  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
4614  * both the controller and device suport such functionality, the write
4615  * request will be translated to WRITE_FPDMA_QUEUED command.
4616  * In both cases the maximum queue depth is derived as minimum of:
4617  * HBA capability,device capability and sata_max_queue_depth variable setting.
4618  * The value passed to HBA driver is decremented by 1, because only 5 bits are
4619  * used to pass max queue depth value, and the maximum possible queue depth
4620  * is 32.
4621  *
4622  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4623  * appropriate values in scsi_pkt fields.
4624  */
4625 static int
4626 sata_txlt_write(sata_pkt_txlate_t *spx)
4627 {
4628 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4629 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4630 	sata_drive_info_t *sdinfo;
4631 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4632 	int cport = SATA_TXLT_CPORT(spx);
4633 	uint16_t sec_count;
4634 	uint64_t lba;
4635 	int rval, reason;
4636 	int synch;
4637 
4638 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4639 
4640 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4641 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4642 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4643 		return (rval);
4644 	}
4645 
4646 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4647 	    &spx->txlt_sata_pkt->satapkt_device);
4648 
4649 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4650 	/*
4651 	 * Extract LBA and sector count from scsi CDB
4652 	 */
4653 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
4654 	case SCMD_WRITE:
4655 		/* 6-byte scsi read cmd : 0x0A */
4656 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
4657 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
4658 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4659 		sec_count = scsipkt->pkt_cdbp[4];
4660 		/* sec_count 0 will be interpreted as 256 by a device */
4661 		break;
4662 	case SCMD_WRITE_G1:
4663 		/* 10-bytes scsi write command : 0x2A */
4664 		lba = scsipkt->pkt_cdbp[2];
4665 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4666 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4667 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4668 		sec_count = scsipkt->pkt_cdbp[7];
4669 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4670 		break;
4671 	case SCMD_WRITE_G5:
4672 		/* 12-bytes scsi read command : 0xAA */
4673 		lba = scsipkt->pkt_cdbp[2];
4674 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4675 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4676 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4677 		sec_count = scsipkt->pkt_cdbp[6];
4678 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
4679 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4680 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
4681 		break;
4682 	case SCMD_WRITE_G4:
4683 		/* 16-bytes scsi write command : 0x8A */
4684 		lba = scsipkt->pkt_cdbp[2];
4685 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4686 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4687 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4688 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
4689 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
4690 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
4691 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
4692 		sec_count = scsipkt->pkt_cdbp[10];
4693 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
4694 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
4695 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
4696 		break;
4697 	default:
4698 		/* Unsupported command */
4699 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4700 		return (sata_txlt_invalid_command(spx));
4701 	}
4702 
4703 	/*
4704 	 * Check if specified address and length exceeds device capacity
4705 	 */
4706 	if ((lba >= sdinfo->satadrv_capacity) ||
4707 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
4708 		/* LBA out of range */
4709 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4710 		return (sata_txlt_lba_out_of_range(spx));
4711 	}
4712 
4713 	/*
4714 	 * For zero-length transfer, emulate good completion of the command
4715 	 * (reasons for rejecting the command were already checked).
4716 	 * No DMA resources were allocated.
4717 	 */
4718 	if (spx->txlt_dma_cookie_list == NULL) {
4719 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4720 		return (sata_emul_rw_completion(spx));
4721 	}
4722 
4723 	/*
4724 	 * Build cmd block depending on the device capability and
4725 	 * requested operation mode.
4726 	 * Do not bother with non-dma mode- we are working only with
4727 	 * devices supporting DMA.
4728 	 */
4729 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
4730 	scmd->satacmd_device_reg = SATA_ADH_LBA;
4731 	scmd->satacmd_cmd_reg = SATAC_WRITE_DMA;
4732 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
4733 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4734 		scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT;
4735 		scmd->satacmd_sec_count_msb = sec_count >> 8;
4736 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
4737 #ifndef __lock_lint
4738 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
4739 		scmd->satacmd_lba_high_msb = lba >> 40;
4740 #endif
4741 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
4742 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
4743 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
4744 	}
4745 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
4746 	scmd->satacmd_lba_low_lsb = lba & 0xff;
4747 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
4748 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
4749 	scmd->satacmd_features_reg = 0;
4750 	scmd->satacmd_status_reg = 0;
4751 	scmd->satacmd_error_reg = 0;
4752 
4753 	/*
4754 	 * Check if queueing commands should be used and switch
4755 	 * to appropriate command if possible
4756 	 */
4757 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
4758 		boolean_t using_queuing;
4759 
4760 		/* Queuing supported by controller and device? */
4761 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
4762 		    (sdinfo->satadrv_features_support &
4763 		    SATA_DEV_F_NCQ) &&
4764 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4765 		    SATA_CTLF_NCQ)) {
4766 			using_queuing = B_TRUE;
4767 
4768 			/* NCQ supported - use FPDMA WRITE */
4769 			scmd->satacmd_cmd_reg =
4770 			    SATAC_WRITE_FPDMA_QUEUED;
4771 			scmd->satacmd_features_reg_ext =
4772 			    scmd->satacmd_sec_count_msb;
4773 			scmd->satacmd_sec_count_msb = 0;
4774 		} else if ((sdinfo->satadrv_features_support &
4775 		    SATA_DEV_F_TCQ) &&
4776 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4777 		    SATA_CTLF_QCMD)) {
4778 			using_queuing = B_TRUE;
4779 
4780 			/* Legacy queueing */
4781 			if (sdinfo->satadrv_features_support &
4782 			    SATA_DEV_F_LBA48) {
4783 				scmd->satacmd_cmd_reg =
4784 				    SATAC_WRITE_DMA_QUEUED_EXT;
4785 				scmd->satacmd_features_reg_ext =
4786 				    scmd->satacmd_sec_count_msb;
4787 				scmd->satacmd_sec_count_msb = 0;
4788 			} else {
4789 				scmd->satacmd_cmd_reg =
4790 				    SATAC_WRITE_DMA_QUEUED;
4791 			}
4792 		} else	/*  NCQ nor legacy queuing not supported */
4793 			using_queuing = B_FALSE;
4794 
4795 		if (using_queuing) {
4796 			scmd->satacmd_features_reg =
4797 			    scmd->satacmd_sec_count_lsb;
4798 			scmd->satacmd_sec_count_lsb = 0;
4799 			scmd->satacmd_flags.sata_queued = B_TRUE;
4800 			/* Set-up maximum queue depth */
4801 			scmd->satacmd_flags.sata_max_queue_depth =
4802 			    sdinfo->satadrv_max_queue_depth - 1;
4803 		} else if (sdinfo->satadrv_features_enabled &
4804 		    SATA_DEV_F_E_UNTAGGED_QING) {
4805 			/*
4806 			 * Although NCQ/TCQ is not enabled, untagged queuing
4807 			 * may be still used.
4808 			 * Set-up the maximum untagged queue depth.
4809 			 * Use controller's queue depth from sata_hba_tran.
4810 			 * SATA HBA drivers may ignore this value and rely on
4811 			 * the internal limits. For drivera that do not
4812 			 * ignore untaged queue depth, limit the value to
4813 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
4814 			 * largest value that can be passed via
4815 			 * satacmd_flags.sata_max_queue_depth.
4816 			 */
4817 			scmd->satacmd_flags.sata_max_queue_depth =
4818 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
4819 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
4820 
4821 		} else {
4822 			scmd->satacmd_flags.sata_max_queue_depth = 0;
4823 		}
4824 	} else
4825 		scmd->satacmd_flags.sata_max_queue_depth = 0;
4826 
4827 	SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4828 	    "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n",
4829 	    scmd->satacmd_cmd_reg, lba, sec_count);
4830 
4831 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4832 		/* Need callback function */
4833 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
4834 		synch = FALSE;
4835 	} else
4836 		synch = TRUE;
4837 
4838 	/* Transfer command to HBA */
4839 	if (sata_hba_start(spx, &rval) != 0) {
4840 		/* Pkt not accepted for execution */
4841 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4842 		return (rval);
4843 	}
4844 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4845 
4846 	/*
4847 	 * If execution is non-synchronous,
4848 	 * a callback function will handle potential errors, translate
4849 	 * the response and will do a callback to a target driver.
4850 	 * If it was synchronous, check execution status using the same
4851 	 * framework callback.
4852 	 */
4853 	if (synch) {
4854 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4855 		    "synchronous execution status %x\n",
4856 		    spx->txlt_sata_pkt->satapkt_reason);
4857 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
4858 	}
4859 	return (TRAN_ACCEPT);
4860 }
4861 
4862 
4863 /*
4864  * Implements SCSI SBC WRITE BUFFER command download microcode option
4865  */
4866 static int
4867 sata_txlt_write_buffer(sata_pkt_txlate_t *spx)
4868 {
4869 #define	WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE			4
4870 #define	WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE			5
4871 
4872 	sata_hba_inst_t *sata_hba_inst = SATA_TXLT_HBA_INST(spx);
4873 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4874 	struct sata_pkt *sata_pkt = spx->txlt_sata_pkt;
4875 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4876 
4877 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4878 	struct scsi_extended_sense *sense;
4879 	int rval, mode, sector_count, reason;
4880 	int cport = SATA_TXLT_CPORT(spx);
4881 
4882 	mode = scsipkt->pkt_cdbp[1] & 0x1f;
4883 
4884 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4885 	    "sata_txlt_write_buffer, mode 0x%x\n", mode);
4886 
4887 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4888 
4889 	if ((rval = sata_txlt_generic_pkt_info(spx, &reason)) != TRAN_ACCEPT) {
4890 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4891 		return (rval);
4892 	}
4893 
4894 	/* Use synchronous mode */
4895 	spx->txlt_sata_pkt->satapkt_op_mode
4896 	    |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
4897 
4898 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4899 
4900 	scsipkt->pkt_reason = CMD_CMPLT;
4901 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4902 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4903 
4904 	/*
4905 	 * The SCSI to ATA translation specification only calls
4906 	 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE.
4907 	 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but
4908 	 * ATA 8 (draft) got rid of download microcode for temp
4909 	 * and it is even optional for ATA 7, so it may be aborted.
4910 	 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as
4911 	 * it is not specified and the buffer offset for SCSI is a 16-bit
4912 	 * value in bytes, but for ATA it is a 16-bit offset in 512 byte
4913 	 * sectors.  Thus the offset really doesn't buy us anything.
4914 	 * If and when ATA 8 is stabilized and the SCSI to ATA specification
4915 	 * is revised, this can be revisisted.
4916 	 */
4917 	/* Reject not supported request */
4918 	switch (mode) {
4919 	case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE:
4920 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP;
4921 		break;
4922 	case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE:
4923 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE;
4924 		break;
4925 	default:
4926 		goto bad_param;
4927 	}
4928 
4929 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
4930 
4931 	scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE;
4932 	if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0)
4933 		goto bad_param;
4934 	sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE;
4935 	scmd->satacmd_sec_count_lsb = (uint8_t)sector_count;
4936 	scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8;
4937 	scmd->satacmd_lba_mid_lsb = 0;
4938 	scmd->satacmd_lba_high_lsb = 0;
4939 	scmd->satacmd_device_reg = 0;
4940 	spx->txlt_sata_pkt->satapkt_comp = NULL;
4941 	scmd->satacmd_addr_type = 0;
4942 
4943 	/* Transfer command to HBA */
4944 	if (sata_hba_start(spx, &rval) != 0) {
4945 		/* Pkt not accepted for execution */
4946 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
4947 		return (rval);
4948 	}
4949 
4950 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
4951 
4952 	/* Then we need synchronous check the status of the disk */
4953 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4954 	    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
4955 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
4956 		scsipkt->pkt_reason = CMD_CMPLT;
4957 
4958 		/* Download commmand succeed, so probe and identify device */
4959 		sata_reidentify_device(spx);
4960 	} else {
4961 		/* Something went wrong, microcode download command failed */
4962 		scsipkt->pkt_reason = CMD_INCOMPLETE;
4963 		*scsipkt->pkt_scbp = STATUS_CHECK;
4964 		sense = sata_arq_sense(spx);
4965 		switch (sata_pkt->satapkt_reason) {
4966 		case SATA_PKT_PORT_ERROR:
4967 			/*
4968 			 * We have no device data. Assume no data transfered.
4969 			 */
4970 			sense->es_key = KEY_HARDWARE_ERROR;
4971 			break;
4972 
4973 		case SATA_PKT_DEV_ERROR:
4974 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
4975 			    SATA_STATUS_ERR) {
4976 				/*
4977 				 * determine dev error reason from error
4978 				 * reg content
4979 				 */
4980 				sata_decode_device_error(spx, sense);
4981 				break;
4982 			}
4983 			/* No extended sense key - no info available */
4984 			break;
4985 
4986 		case SATA_PKT_TIMEOUT:
4987 			scsipkt->pkt_reason = CMD_TIMEOUT;
4988 			scsipkt->pkt_statistics |=
4989 			    STAT_TIMEOUT | STAT_DEV_RESET;
4990 			/* No extended sense key ? */
4991 			break;
4992 
4993 		case SATA_PKT_ABORTED:
4994 			scsipkt->pkt_reason = CMD_ABORTED;
4995 			scsipkt->pkt_statistics |= STAT_ABORTED;
4996 			/* No extended sense key ? */
4997 			break;
4998 
4999 		case SATA_PKT_RESET:
5000 			/* pkt aborted by an explicit reset from a host */
5001 			scsipkt->pkt_reason = CMD_RESET;
5002 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5003 			break;
5004 
5005 		default:
5006 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5007 			    "sata_txlt_nodata_cmd_completion: "
5008 			    "invalid packet completion reason %d",
5009 			    sata_pkt->satapkt_reason));
5010 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5011 			break;
5012 		}
5013 
5014 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5015 		    "scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5016 
5017 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5018 		    scsipkt->pkt_comp != NULL)
5019 			/* scsi callback required */
5020 			(*scsipkt->pkt_comp)(scsipkt);
5021 	}
5022 	return (TRAN_ACCEPT);
5023 
5024 bad_param:
5025 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5026 	*scsipkt->pkt_scbp = STATUS_CHECK;
5027 	sense = sata_arq_sense(spx);
5028 	sense->es_key = KEY_ILLEGAL_REQUEST;
5029 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5030 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5031 	    scsipkt->pkt_comp != NULL) {
5032 		/* scsi callback required */
5033 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5034 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5035 		    TQ_SLEEP) == 0) {
5036 			/* Scheduling the callback failed */
5037 			rval = TRAN_BUSY;
5038 		}
5039 	}
5040 	return (rval);
5041 }
5042 
5043 /*
5044  * Re-identify device after doing a firmware download.
5045  */
5046 static void
5047 sata_reidentify_device(sata_pkt_txlate_t *spx)
5048 {
5049 #define	DOWNLOAD_WAIT_TIME_SECS	60
5050 #define	DOWNLOAD_WAIT_INTERVAL_SECS	1
5051 	int rval;
5052 	int retry_cnt;
5053 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5054 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
5055 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
5056 	sata_drive_info_t *sdinfo;
5057 
5058 	/*
5059 	 * Before returning good status, probe device.
5060 	 * Device probing will get IDENTIFY DEVICE data, if possible.
5061 	 * The assumption is that the new microcode is applied by the
5062 	 * device. It is a caller responsibility to verify this.
5063 	 */
5064 	for (retry_cnt = 0;
5065 	    retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS;
5066 	    retry_cnt++) {
5067 		rval = sata_probe_device(sata_hba_inst, &sata_device);
5068 
5069 		if (rval == SATA_SUCCESS) { /* Set default features */
5070 			sdinfo = sata_get_device_info(sata_hba_inst,
5071 			    &sata_device);
5072 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
5073 			    SATA_SUCCESS) {
5074 				/* retry */
5075 				(void) sata_initialize_device(sata_hba_inst,
5076 				    sdinfo);
5077 			}
5078 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5079 			    scsipkt->pkt_comp != NULL)
5080 				(*scsipkt->pkt_comp)(scsipkt);
5081 			return;
5082 		} else if (rval == SATA_RETRY) {
5083 			delay(drv_usectohz(1000000 *
5084 			    DOWNLOAD_WAIT_INTERVAL_SECS));
5085 			continue;
5086 		} else	/* failed - no reason to retry */
5087 			break;
5088 	}
5089 
5090 	/*
5091 	 * Something went wrong, device probing failed.
5092 	 */
5093 	SATA_LOG_D((sata_hba_inst, CE_WARN,
5094 	    "Cannot probe device after downloading microcode\n"));
5095 
5096 	/* Reset device to force retrying the probe. */
5097 	(void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst))
5098 	    (SATA_DIP(sata_hba_inst), &sata_device);
5099 
5100 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5101 	    scsipkt->pkt_comp != NULL)
5102 		(*scsipkt->pkt_comp)(scsipkt);
5103 }
5104 
5105 
5106 /*
5107  * Translate command: Synchronize Cache.
5108  * Translates into Flush Cache command for SATA hard disks.
5109  *
5110  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
5111  * appropriate values in scsi_pkt fields.
5112  */
5113 static 	int
5114 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx)
5115 {
5116 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5117 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
5118 	int cport = SATA_TXLT_CPORT(spx);
5119 	int rval, reason;
5120 	int synch;
5121 
5122 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5123 
5124 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
5125 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5126 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5127 		return (rval);
5128 	}
5129 
5130 	scmd->satacmd_addr_type = 0;
5131 	scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE;
5132 	scmd->satacmd_device_reg = 0;
5133 	scmd->satacmd_sec_count_lsb = 0;
5134 	scmd->satacmd_lba_low_lsb = 0;
5135 	scmd->satacmd_lba_mid_lsb = 0;
5136 	scmd->satacmd_lba_high_lsb = 0;
5137 	scmd->satacmd_features_reg = 0;
5138 	scmd->satacmd_status_reg = 0;
5139 	scmd->satacmd_error_reg = 0;
5140 
5141 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5142 	    "sata_txlt_synchronize_cache\n", NULL);
5143 
5144 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5145 		/* Need to set-up a callback function */
5146 		spx->txlt_sata_pkt->satapkt_comp =
5147 		    sata_txlt_nodata_cmd_completion;
5148 		synch = FALSE;
5149 	} else
5150 		synch = TRUE;
5151 
5152 	/* Transfer command to HBA */
5153 	if (sata_hba_start(spx, &rval) != 0) {
5154 		/* Pkt not accepted for execution */
5155 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5156 		return (rval);
5157 	}
5158 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5159 
5160 	/*
5161 	 * If execution non-synchronous, it had to be completed
5162 	 * a callback function will handle potential errors, translate
5163 	 * the response and will do a callback to a target driver.
5164 	 * If it was synchronous, check status, using the same
5165 	 * framework callback.
5166 	 */
5167 	if (synch) {
5168 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5169 		    "synchronous execution status %x\n",
5170 		    spx->txlt_sata_pkt->satapkt_reason);
5171 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
5172 	}
5173 	return (TRAN_ACCEPT);
5174 }
5175 
5176 
5177 /*
5178  * Send pkt to SATA HBA driver
5179  *
5180  * This function may be called only if the operation is requested by scsi_pkt,
5181  * i.e. scsi_pkt is not NULL.
5182  *
5183  * This function has to be called with cport mutex held. It does release
5184  * the mutex when it calls HBA driver sata_tran_start function and
5185  * re-acquires it afterwards.
5186  *
5187  * If return value is 0, pkt was accepted, -1 otherwise
5188  * rval is set to appropriate sata_scsi_start return value.
5189  *
5190  * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not
5191  * have called the sata_pkt callback function for this packet.
5192  *
5193  * The scsi callback has to be performed by the caller of this routine.
5194  *
5195  * Note 2: No port multiplier support for now.
5196  */
5197 static int
5198 sata_hba_start(sata_pkt_txlate_t *spx, int *rval)
5199 {
5200 	int stat, cport;
5201 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
5202 	sata_drive_info_t *sdinfo;
5203 	sata_device_t *sata_device;
5204 	uint8_t cmd;
5205 	struct sata_cmd_flags cmd_flags;
5206 
5207 	ASSERT(spx->txlt_sata_pkt != NULL);
5208 
5209 	cport = SATA_TXLT_CPORT(spx);
5210 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5211 
5212 	sdinfo = sata_get_device_info(sata_hba_inst,
5213 	    &spx->txlt_sata_pkt->satapkt_device);
5214 	ASSERT(sdinfo != NULL);
5215 
5216 	/* Clear device reset state? */
5217 	if (sdinfo->satadrv_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) {
5218 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
5219 		    sata_clear_dev_reset = B_TRUE;
5220 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_CLEAR_DEVICE_RESET;
5221 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5222 		    "sata_hba_start: clearing device reset state\n", NULL);
5223 	}
5224 	cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg;
5225 	cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags;
5226 	sata_device = &spx->txlt_sata_pkt->satapkt_device;
5227 
5228 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5229 
5230 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5231 	    "Sata cmd 0x%2x\n", cmd);
5232 
5233 	stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
5234 	    spx->txlt_sata_pkt);
5235 
5236 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5237 	/*
5238 	 * If sata pkt was accepted and executed in asynchronous mode, i.e.
5239 	 * with the sata callback, the sata_pkt could be already destroyed
5240 	 * by the time we check ther return status from the hba_start()
5241 	 * function, because sata_scsi_destroy_pkt() could have been already
5242 	 * called (perhaps in the interrupt context). So, in such case, there
5243 	 * should be no references to it. In other cases, sata_pkt still
5244 	 * exists.
5245 	 */
5246 	if (stat == SATA_TRAN_ACCEPTED) {
5247 		/*
5248 		 * pkt accepted for execution.
5249 		 * If it was executed synchronously, it is already completed
5250 		 * and pkt completion_reason indicates completion status.
5251 		 */
5252 		*rval = TRAN_ACCEPT;
5253 		return (0);
5254 	}
5255 
5256 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
5257 	switch (stat) {
5258 	case SATA_TRAN_QUEUE_FULL:
5259 		/*
5260 		 * Controller detected queue full condition.
5261 		 */
5262 		SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst,
5263 		    "sata_hba_start: queue full\n", NULL);
5264 
5265 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
5266 		*spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL;
5267 
5268 		*rval = TRAN_BUSY;
5269 		break;
5270 
5271 	case SATA_TRAN_PORT_ERROR:
5272 		/*
5273 		 * Communication/link with device or general port error
5274 		 * detected before pkt execution begun.
5275 		 */
5276 		if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
5277 		    SATA_ADDR_CPORT ||
5278 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
5279 		    SATA_ADDR_DCPORT)
5280 			sata_log(sata_hba_inst, CE_CONT,
5281 			    "SATA port %d error",
5282 			    sata_device->satadev_addr.cport);
5283 		else
5284 			sata_log(sata_hba_inst, CE_CONT,
5285 			    "SATA port %d pmport %d error\n",
5286 			    sata_device->satadev_addr.cport,
5287 			    sata_device->satadev_addr.pmport);
5288 
5289 		/*
5290 		 * Update the port/device structure.
5291 		 * sata_pkt should be still valid. Since port error is
5292 		 * returned, sata_device content should reflect port
5293 		 * state - it means, that sata address have been changed,
5294 		 * because original packet's sata address refered to a device
5295 		 * attached to some port.
5296 		 */
5297 		sata_update_port_info(sata_hba_inst, sata_device);
5298 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
5299 		*rval = TRAN_FATAL_ERROR;
5300 		break;
5301 
5302 	case SATA_TRAN_CMD_UNSUPPORTED:
5303 		/*
5304 		 * Command rejected by HBA as unsupported. It was HBA driver
5305 		 * that rejected the command, command was not sent to
5306 		 * an attached device.
5307 		 */
5308 		if ((sdinfo != NULL) &&
5309 		    (sdinfo->satadrv_state & SATA_DSTATE_RESET))
5310 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5311 			    "sat_hba_start: cmd 0x%2x rejected "
5312 			    "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd);
5313 
5314 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5315 		(void) sata_txlt_invalid_command(spx);
5316 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5317 
5318 		*rval = TRAN_ACCEPT;
5319 		break;
5320 
5321 	case SATA_TRAN_BUSY:
5322 		/*
5323 		 * Command rejected by HBA because other operation prevents
5324 		 * accepting the packet, or device is in RESET condition.
5325 		 */
5326 		if (sdinfo != NULL) {
5327 			sdinfo->satadrv_state =
5328 			    spx->txlt_sata_pkt->satapkt_device.satadev_state;
5329 
5330 			if (sdinfo->satadrv_state & SATA_DSTATE_RESET) {
5331 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5332 				    "sata_hba_start: cmd 0x%2x rejected "
5333 				    "because of device reset condition\n",
5334 				    cmd);
5335 			} else {
5336 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5337 				    "sata_hba_start: cmd 0x%2x rejected "
5338 				    "with SATA_TRAN_BUSY status\n",
5339 				    cmd);
5340 			}
5341 		}
5342 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
5343 		*rval = TRAN_BUSY;
5344 		break;
5345 
5346 	default:
5347 		/* Unrecognized HBA response */
5348 		SATA_LOG_D((sata_hba_inst, CE_WARN,
5349 		    "sata_hba_start: unrecognized HBA response "
5350 		    "to cmd : 0x%2x resp 0x%x", cmd, rval));
5351 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
5352 		*rval = TRAN_FATAL_ERROR;
5353 		break;
5354 	}
5355 
5356 	/*
5357 	 * If we got here, the packet was rejected.
5358 	 * Check if we need to remember reset state clearing request
5359 	 */
5360 	if (cmd_flags.sata_clear_dev_reset) {
5361 		/*
5362 		 * Check if device is still configured - it may have
5363 		 * disapeared from the configuration
5364 		 */
5365 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
5366 		if (sdinfo != NULL) {
5367 			/*
5368 			 * Restore the flag that requests clearing of
5369 			 * the device reset state,
5370 			 * so the next sata packet may carry it to HBA.
5371 			 */
5372 			sdinfo->satadrv_event_flags |=
5373 			    SATA_EVNT_CLEAR_DEVICE_RESET;
5374 		}
5375 	}
5376 	return (-1);
5377 }
5378 
5379 /*
5380  * Scsi response setup for invalid LBA
5381  *
5382  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
5383  */
5384 static int
5385 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx)
5386 {
5387 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5388 	struct scsi_extended_sense *sense;
5389 
5390 	scsipkt->pkt_reason = CMD_CMPLT;
5391 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5392 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5393 	*scsipkt->pkt_scbp = STATUS_CHECK;
5394 
5395 	*scsipkt->pkt_scbp = STATUS_CHECK;
5396 	sense = sata_arq_sense(spx);
5397 	sense->es_key = KEY_ILLEGAL_REQUEST;
5398 	sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
5399 
5400 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5401 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5402 
5403 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5404 	    scsipkt->pkt_comp != NULL)
5405 		/* scsi callback required */
5406 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5407 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5408 		    TQ_SLEEP) == NULL)
5409 			/* Scheduling the callback failed */
5410 			return (TRAN_BUSY);
5411 	return (TRAN_ACCEPT);
5412 }
5413 
5414 
5415 /*
5416  * Analyze device status and error registers and translate them into
5417  * appropriate scsi sense codes.
5418  * NOTE: non-packet commands only for now
5419  */
5420 static void
5421 sata_decode_device_error(sata_pkt_txlate_t *spx,
5422     struct scsi_extended_sense *sense)
5423 {
5424 	uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg;
5425 
5426 	ASSERT(sense != NULL);
5427 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
5428 	    SATA_STATUS_ERR);
5429 
5430 
5431 	if (err_reg & SATA_ERROR_ICRC) {
5432 		sense->es_key = KEY_ABORTED_COMMAND;
5433 		sense->es_add_code = 0x08; /* Communication failure */
5434 		return;
5435 	}
5436 
5437 	if (err_reg & SATA_ERROR_UNC) {
5438 		sense->es_key = KEY_MEDIUM_ERROR;
5439 		/* Information bytes (LBA) need to be set by a caller */
5440 		return;
5441 	}
5442 
5443 	/* ADD HERE: MC error bit handling for ATAPI CD/DVD */
5444 	if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) {
5445 		sense->es_key = KEY_UNIT_ATTENTION;
5446 		sense->es_add_code = 0x3a; /* No media present */
5447 		return;
5448 	}
5449 
5450 	if (err_reg & SATA_ERROR_IDNF) {
5451 		if (err_reg & SATA_ERROR_ABORT) {
5452 			sense->es_key = KEY_ABORTED_COMMAND;
5453 		} else {
5454 			sense->es_key = KEY_ILLEGAL_REQUEST;
5455 			sense->es_add_code = 0x21; /* LBA out of range */
5456 		}
5457 		return;
5458 	}
5459 
5460 	if (err_reg & SATA_ERROR_ABORT) {
5461 		ASSERT(spx->txlt_sata_pkt != NULL);
5462 		sense->es_key = KEY_ABORTED_COMMAND;
5463 		return;
5464 	}
5465 }
5466 
5467 /*
5468  * Extract error LBA from sata_pkt.satapkt_cmd register fields
5469  */
5470 static void
5471 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba)
5472 {
5473 	sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd;
5474 
5475 	*lba = 0;
5476 	if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) {
5477 		*lba = sata_cmd->satacmd_lba_high_msb;
5478 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb;
5479 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb;
5480 	} else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) {
5481 		*lba = sata_cmd->satacmd_device_reg & 0xf;
5482 	}
5483 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb;
5484 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb;
5485 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb;
5486 }
5487 
5488 /*
5489  * This is fixed sense format - if LBA exceeds the info field size,
5490  * no valid info will be returned (valid bit in extended sense will
5491  * be set to 0).
5492  */
5493 static struct scsi_extended_sense *
5494 sata_arq_sense(sata_pkt_txlate_t *spx)
5495 {
5496 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5497 	struct scsi_arq_status *arqs;
5498 	struct scsi_extended_sense *sense;
5499 
5500 	/* Fill ARQ sense data */
5501 	scsipkt->pkt_state |= STATE_ARQ_DONE;
5502 	arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp;
5503 	*(uchar_t *)&arqs->sts_status = STATUS_CHECK;
5504 	*(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD;
5505 	arqs->sts_rqpkt_reason = CMD_CMPLT;
5506 	arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5507 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
5508 	arqs->sts_rqpkt_resid = 0;
5509 	sense = &arqs->sts_sensedata;
5510 	bzero(sense, sizeof (struct scsi_extended_sense));
5511 	sata_fixed_sense_data_preset(sense);
5512 	return (sense);
5513 }
5514 
5515 
5516 /*
5517  * Emulated SATA Read/Write command completion for zero-length requests.
5518  * This request always succedes, so in synchronous mode it always returns
5519  * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the
5520  * callback cannot be scheduled.
5521  */
5522 static int
5523 sata_emul_rw_completion(sata_pkt_txlate_t *spx)
5524 {
5525 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5526 
5527 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5528 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5529 	scsipkt->pkt_reason = CMD_CMPLT;
5530 	*scsipkt->pkt_scbp = STATUS_GOOD;
5531 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5532 		/* scsi callback required - have to schedule it */
5533 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5534 		    (task_func_t *)scsipkt->pkt_comp,
5535 		    (void *)scsipkt, TQ_SLEEP) == NULL)
5536 			/* Scheduling the callback failed */
5537 			return (TRAN_BUSY);
5538 	}
5539 	return (TRAN_ACCEPT);
5540 }
5541 
5542 
5543 /*
5544  * Translate completion status of SATA read/write commands into scsi response.
5545  * pkt completion_reason is checked to determine the completion status.
5546  * Do scsi callback if necessary.
5547  *
5548  * Note: this function may be called also for synchronously executed
5549  * commands.
5550  * This function may be used only if scsi_pkt is non-NULL.
5551  */
5552 static void
5553 sata_txlt_rw_completion(sata_pkt_t *sata_pkt)
5554 {
5555 	sata_pkt_txlate_t *spx =
5556 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
5557 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
5558 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5559 	struct scsi_extended_sense *sense;
5560 	uint64_t lba;
5561 	struct buf *bp;
5562 	int rval;
5563 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5564 		/* Normal completion */
5565 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5566 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
5567 		scsipkt->pkt_reason = CMD_CMPLT;
5568 		*scsipkt->pkt_scbp = STATUS_GOOD;
5569 		if (spx->txlt_tmp_buf != NULL) {
5570 			/* Temporary buffer was used */
5571 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5572 			if (bp->b_flags & B_READ) {
5573 				rval = ddi_dma_sync(
5574 				    spx->txlt_buf_dma_handle, 0, 0,
5575 				    DDI_DMA_SYNC_FORCPU);
5576 				ASSERT(rval == DDI_SUCCESS);
5577 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
5578 				    bp->b_bcount);
5579 			}
5580 		}
5581 	} else {
5582 		/*
5583 		 * Something went wrong - analyze return
5584 		 */
5585 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5586 		    STATE_SENT_CMD | STATE_GOT_STATUS;
5587 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5588 		*scsipkt->pkt_scbp = STATUS_CHECK;
5589 		sense = sata_arq_sense(spx);
5590 		ASSERT(sense != NULL);
5591 
5592 		/*
5593 		 * SATA_PKT_DEV_ERROR is the only case where we may be able to
5594 		 * extract from device registers the failing LBA.
5595 		 */
5596 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
5597 			if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) &&
5598 			    (scmd->satacmd_lba_mid_msb != 0 ||
5599 			    scmd->satacmd_lba_high_msb != 0)) {
5600 				/*
5601 				 * We have problem reporting this cmd LBA
5602 				 * in fixed sense data format, because of
5603 				 * the size of the scsi LBA fields.
5604 				 */
5605 				sense->es_valid = 0;
5606 			} else {
5607 				sata_extract_error_lba(spx, &lba);
5608 				sense->es_info_1 = (lba & 0xFF000000) >> 24;
5609 				sense->es_info_2 = (lba & 0xFF0000) >> 16;
5610 				sense->es_info_3 = (lba & 0xFF00) >> 8;
5611 				sense->es_info_4 = lba & 0xFF;
5612 			}
5613 		} else {
5614 			/* Invalid extended sense info */
5615 			sense->es_valid = 0;
5616 		}
5617 
5618 		switch (sata_pkt->satapkt_reason) {
5619 		case SATA_PKT_PORT_ERROR:
5620 			/* We may want to handle DEV GONE state as well */
5621 			/*
5622 			 * We have no device data. Assume no data transfered.
5623 			 */
5624 			sense->es_key = KEY_HARDWARE_ERROR;
5625 			break;
5626 
5627 		case SATA_PKT_DEV_ERROR:
5628 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5629 			    SATA_STATUS_ERR) {
5630 				/*
5631 				 * determine dev error reason from error
5632 				 * reg content
5633 				 */
5634 				sata_decode_device_error(spx, sense);
5635 				if (sense->es_key == KEY_MEDIUM_ERROR) {
5636 					switch (scmd->satacmd_cmd_reg) {
5637 					case SATAC_READ_DMA:
5638 					case SATAC_READ_DMA_EXT:
5639 					case SATAC_READ_DMA_QUEUED:
5640 					case SATAC_READ_DMA_QUEUED_EXT:
5641 					case SATAC_READ_FPDMA_QUEUED:
5642 						/* Unrecovered read error */
5643 						sense->es_add_code =
5644 						    SD_SCSI_ASC_UNREC_READ_ERR;
5645 						break;
5646 					case SATAC_WRITE_DMA:
5647 					case SATAC_WRITE_DMA_EXT:
5648 					case SATAC_WRITE_DMA_QUEUED:
5649 					case SATAC_WRITE_DMA_QUEUED_EXT:
5650 					case SATAC_WRITE_FPDMA_QUEUED:
5651 						/* Write error */
5652 						sense->es_add_code =
5653 						    SD_SCSI_ASC_WRITE_ERR;
5654 						break;
5655 					default:
5656 						/* Internal error */
5657 						SATA_LOG_D((
5658 						    spx->txlt_sata_hba_inst,
5659 						    CE_WARN,
5660 						    "sata_txlt_rw_completion :"
5661 						    "internal error - invalid "
5662 						    "command 0x%2x",
5663 						    scmd->satacmd_cmd_reg));
5664 						break;
5665 					}
5666 				}
5667 				break;
5668 			}
5669 			/* No extended sense key - no info available */
5670 			scsipkt->pkt_reason = CMD_INCOMPLETE;
5671 			break;
5672 
5673 		case SATA_PKT_TIMEOUT:
5674 			scsipkt->pkt_reason = CMD_TIMEOUT;
5675 			scsipkt->pkt_statistics |=
5676 			    STAT_TIMEOUT | STAT_DEV_RESET;
5677 			sense->es_key = KEY_ABORTED_COMMAND;
5678 			break;
5679 
5680 		case SATA_PKT_ABORTED:
5681 			scsipkt->pkt_reason = CMD_ABORTED;
5682 			scsipkt->pkt_statistics |= STAT_ABORTED;
5683 			sense->es_key = KEY_ABORTED_COMMAND;
5684 			break;
5685 
5686 		case SATA_PKT_RESET:
5687 			scsipkt->pkt_reason = CMD_RESET;
5688 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5689 			sense->es_key = KEY_ABORTED_COMMAND;
5690 			break;
5691 
5692 		default:
5693 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5694 			    "sata_txlt_rw_completion: "
5695 			    "invalid packet completion reason"));
5696 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5697 			break;
5698 		}
5699 	}
5700 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5701 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5702 
5703 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5704 	    scsipkt->pkt_comp != NULL)
5705 		/* scsi callback required */
5706 		(*scsipkt->pkt_comp)(scsipkt);
5707 }
5708 
5709 
5710 /*
5711  * Translate completion status of non-data commands (i.e. commands returning
5712  * no data).
5713  * pkt completion_reason is checked to determine the completion status.
5714  * Do scsi callback if necessary (FLAG_NOINTR == 0)
5715  *
5716  * Note: this function may be called also for synchronously executed
5717  * commands.
5718  * This function may be used only if scsi_pkt is non-NULL.
5719  */
5720 
5721 static 	void
5722 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt)
5723 {
5724 	sata_pkt_txlate_t *spx =
5725 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
5726 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5727 	struct scsi_extended_sense *sense;
5728 
5729 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5730 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5731 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5732 		/* Normal completion */
5733 		scsipkt->pkt_reason = CMD_CMPLT;
5734 		*scsipkt->pkt_scbp = STATUS_GOOD;
5735 	} else {
5736 		/* Something went wrong */
5737 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5738 		*scsipkt->pkt_scbp = STATUS_CHECK;
5739 		sense = sata_arq_sense(spx);
5740 		switch (sata_pkt->satapkt_reason) {
5741 		case SATA_PKT_PORT_ERROR:
5742 			/*
5743 			 * We have no device data. Assume no data transfered.
5744 			 */
5745 			sense->es_key = KEY_HARDWARE_ERROR;
5746 			break;
5747 
5748 		case SATA_PKT_DEV_ERROR:
5749 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5750 			    SATA_STATUS_ERR) {
5751 				/*
5752 				 * determine dev error reason from error
5753 				 * reg content
5754 				 */
5755 				sata_decode_device_error(spx, sense);
5756 				break;
5757 			}
5758 			/* No extended sense key - no info available */
5759 			break;
5760 
5761 		case SATA_PKT_TIMEOUT:
5762 			scsipkt->pkt_reason = CMD_TIMEOUT;
5763 			scsipkt->pkt_statistics |=
5764 			    STAT_TIMEOUT | STAT_DEV_RESET;
5765 			/* No extended sense key ? */
5766 			break;
5767 
5768 		case SATA_PKT_ABORTED:
5769 			scsipkt->pkt_reason = CMD_ABORTED;
5770 			scsipkt->pkt_statistics |= STAT_ABORTED;
5771 			/* No extended sense key ? */
5772 			break;
5773 
5774 		case SATA_PKT_RESET:
5775 			/* pkt aborted by an explicit reset from a host */
5776 			scsipkt->pkt_reason = CMD_RESET;
5777 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5778 			break;
5779 
5780 		default:
5781 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5782 			    "sata_txlt_nodata_cmd_completion: "
5783 			    "invalid packet completion reason %d",
5784 			    sata_pkt->satapkt_reason));
5785 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5786 			break;
5787 		}
5788 
5789 	}
5790 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5791 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5792 
5793 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5794 	    scsipkt->pkt_comp != NULL)
5795 		/* scsi callback required */
5796 		(*scsipkt->pkt_comp)(scsipkt);
5797 }
5798 
5799 
5800 /*
5801  * Build Mode sense R/W recovery page
5802  * NOT IMPLEMENTED
5803  */
5804 
5805 static int
5806 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5807 {
5808 #ifndef __lock_lint
5809 	_NOTE(ARGUNUSED(sdinfo))
5810 	_NOTE(ARGUNUSED(pcntrl))
5811 	_NOTE(ARGUNUSED(buf))
5812 #endif
5813 	return (0);
5814 }
5815 
5816 /*
5817  * Build Mode sense caching page  -  scsi-3 implementation.
5818  * Page length distinguishes previous format from scsi-3 format.
5819  * buf must have space for 0x12 bytes.
5820  * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable.
5821  *
5822  */
5823 static int
5824 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5825 {
5826 	struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf;
5827 	sata_id_t *sata_id = &sdinfo->satadrv_id;
5828 
5829 	/*
5830 	 * Most of the fields are set to 0, being not supported and/or disabled
5831 	 */
5832 	bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3);
5833 
5834 	/* Saved paramters not supported */
5835 	if (pcntrl == 3)
5836 		return (0);
5837 	if (pcntrl == 0 || pcntrl == 2) {
5838 		/*
5839 		 * For now treat current and default parameters as same
5840 		 * That may have to change, if target driver will complain
5841 		 */
5842 		page->mode_page.code = MODEPAGE_CACHING;	/* PS = 0 */
5843 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
5844 
5845 		if ((sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) &&
5846 		    !(sata_id->ai_features85 & SATA_LOOK_AHEAD)) {
5847 			page->dra = 1;		/* Read Ahead disabled */
5848 			page->rcd = 1;		/* Read Cache disabled */
5849 		}
5850 		if ((sata_id->ai_cmdset82 & SATA_WRITE_CACHE) &&
5851 		    (sata_id->ai_features85 & SATA_WRITE_CACHE))
5852 			page->wce = 1;		/* Write Cache enabled */
5853 	} else {
5854 		/* Changeable parameters */
5855 		page->mode_page.code = MODEPAGE_CACHING;
5856 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
5857 		if (sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) {
5858 			page->dra = 1;
5859 			page->rcd = 1;
5860 		}
5861 		if (sata_id->ai_cmdset82 & SATA_WRITE_CACHE)
5862 			page->wce = 1;
5863 	}
5864 	return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
5865 	    sizeof (struct mode_page));
5866 }
5867 
5868 /*
5869  * Build Mode sense exception cntrl page
5870  */
5871 static int
5872 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5873 {
5874 	struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf;
5875 	sata_id_t *sata_id = &sdinfo->satadrv_id;
5876 
5877 	/*
5878 	 * Most of the fields are set to 0, being not supported and/or disabled
5879 	 */
5880 	bzero(buf, PAGELENGTH_INFO_EXCPT);
5881 
5882 	page->mode_page.code = MODEPAGE_INFO_EXCPT;
5883 	page->mode_page.length = PAGELENGTH_INFO_EXCPT;
5884 
5885 	/* Indicate that this is page is saveable */
5886 	page->mode_page.ps = 1;
5887 
5888 	/*
5889 	 * We will return the same data for default, current and saved page.
5890 	 * The only changeable bit is dexcpt and that bit is required
5891 	 * by the ATA specification to be preserved across power cycles.
5892 	 */
5893 	if (pcntrl != 1) {
5894 		page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED);
5895 		page->mrie = MRIE_ONLY_ON_REQUEST;
5896 	}
5897 	else
5898 		page->dexcpt = 1;	/* Only changeable parameter */
5899 
5900 	return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_info_excpt_page));
5901 }
5902 
5903 
5904 static int
5905 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5906 {
5907 	struct mode_acoustic_management *page =
5908 	    (struct mode_acoustic_management *)buf;
5909 	sata_id_t *sata_id = &sdinfo->satadrv_id;
5910 
5911 	/*
5912 	 * Most of the fields are set to 0, being not supported and/or disabled
5913 	 */
5914 	bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT);
5915 
5916 	switch (pcntrl) {
5917 	case P_CNTRL_DEFAULT:
5918 		/*  default paramters not supported */
5919 		return (0);
5920 
5921 	case P_CNTRL_CURRENT:
5922 	case P_CNTRL_SAVED:
5923 		/* Saved and current are supported and are identical */
5924 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
5925 		page->mode_page.length =
5926 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
5927 		page->mode_page.ps = 1;
5928 
5929 		/* Word 83 indicates if feature is supported */
5930 		/* If feature is not supported */
5931 		if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) {
5932 			page->acoustic_manag_enable =
5933 			    ACOUSTIC_DISABLED;
5934 		} else {
5935 			page->acoustic_manag_enable =
5936 			    ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT)
5937 			    != 0);
5938 			/* Word 94 inidicates the value */
5939 #ifdef	_LITTLE_ENDIAN
5940 			page->acoustic_manag_level =
5941 			    (uchar_t)sata_id->ai_acoustic;
5942 			page->vendor_recommended_value =
5943 			    sata_id->ai_acoustic >> 8;
5944 #else
5945 			page->acoustic_manag_level =
5946 			    sata_id->ai_acoustic >> 8;
5947 			page->vendor_recommended_value =
5948 			    (uchar_t)sata_id->ai_acoustic;
5949 #endif
5950 		}
5951 		break;
5952 
5953 	case P_CNTRL_CHANGEABLE:
5954 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
5955 		page->mode_page.length =
5956 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
5957 		page->mode_page.ps = 1;
5958 
5959 		/* Word 83 indicates if the feature is supported */
5960 		if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) {
5961 			page->acoustic_manag_enable =
5962 			    ACOUSTIC_ENABLED;
5963 			page->acoustic_manag_level = 0xff;
5964 		}
5965 		break;
5966 	}
5967 	return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
5968 	    sizeof (struct mode_page));
5969 }
5970 
5971 
5972 /*
5973  * Build Mode sense power condition page
5974  * NOT IMPLEMENTED.
5975  */
5976 static int
5977 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5978 {
5979 #ifndef __lock_lint
5980 	_NOTE(ARGUNUSED(sdinfo))
5981 	_NOTE(ARGUNUSED(pcntrl))
5982 	_NOTE(ARGUNUSED(buf))
5983 #endif
5984 	return (0);
5985 }
5986 
5987 
5988 /*
5989  * Process mode select caching page 8 (scsi3 format only).
5990  * Read Ahead (same as read cache) and Write Cache may be turned on and off
5991  * if these features are supported by the device. If these features are not
5992  * supported, quietly ignore them.
5993  * This function fails only if the SET FEATURE command sent to
5994  * the device fails. The page format is not varified, assuming that the
5995  * target driver operates correctly - if parameters length is too short,
5996  * we just drop the page.
5997  * Two command may be sent if both Read Cache/Read Ahead and Write Cache
5998  * setting have to be changed.
5999  * SET FEATURE command is executed synchronously, i.e. we wait here until
6000  * it is completed, regardless of the scsi pkt directives.
6001  *
6002  * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e.
6003  * changing DRA will change RCD.
6004  *
6005  * More than one SATA command may be executed to perform operations specified
6006  * by mode select pages. The first error terminates further execution.
6007  * Operations performed successully are not backed-up in such case.
6008  *
6009  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
6010  * If operation resulted in changing device setup, dmod flag should be set to
6011  * one (1). If parameters were not changed, dmod flag should be set to 0.
6012  * Upon return, if operation required sending command to the device, the rval
6013  * should be set to the value returned by sata_hba_start. If operation
6014  * did not require device access, rval should be set to TRAN_ACCEPT.
6015  * The pagelen should be set to the length of the page.
6016  *
6017  * This function has to be called with a port mutex held.
6018  *
6019  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6020  */
6021 int
6022 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page,
6023     int parmlen, int *pagelen, int *rval, int *dmod)
6024 {
6025 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6026 	sata_drive_info_t *sdinfo;
6027 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6028 	sata_id_t *sata_id;
6029 	struct scsi_extended_sense *sense;
6030 	int wce, dra;	/* Current settings */
6031 
6032 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6033 	    &spx->txlt_sata_pkt->satapkt_device);
6034 	sata_id = &sdinfo->satadrv_id;
6035 	*dmod = 0;
6036 
6037 	/* Verify parameters length. If too short, drop it */
6038 	if (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
6039 	    sizeof (struct mode_page) < parmlen) {
6040 		*scsipkt->pkt_scbp = STATUS_CHECK;
6041 		sense = sata_arq_sense(spx);
6042 		sense->es_key = KEY_ILLEGAL_REQUEST;
6043 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6044 		*pagelen = parmlen;
6045 		*rval = TRAN_ACCEPT;
6046 		return (SATA_FAILURE);
6047 	}
6048 
6049 	*pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page);
6050 
6051 	/*
6052 	 * We can manipulate only write cache and read ahead
6053 	 * (read cache) setting.
6054 	 */
6055 	if (!(sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) &&
6056 	    !(sata_id->ai_cmdset82 & SATA_WRITE_CACHE)) {
6057 		/*
6058 		 * None of the features is supported - ignore
6059 		 */
6060 		*rval = TRAN_ACCEPT;
6061 		return (SATA_SUCCESS);
6062 	}
6063 
6064 	/* Current setting of Read Ahead (and Read Cache) */
6065 	if (sata_id->ai_features85 & SATA_LOOK_AHEAD)
6066 		dra = 0;	/* 0 == not disabled */
6067 	else
6068 		dra = 1;
6069 	/* Current setting of Write Cache */
6070 	if (sata_id->ai_features85 & SATA_WRITE_CACHE)
6071 		wce = 1;
6072 	else
6073 		wce = 0;
6074 
6075 	if (page->dra == dra && page->wce == wce && page->rcd == dra) {
6076 		/* nothing to do */
6077 		*rval = TRAN_ACCEPT;
6078 		return (SATA_SUCCESS);
6079 	}
6080 	/*
6081 	 * Need to flip some setting
6082 	 * Set-up Internal SET FEATURES command(s)
6083 	 */
6084 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6085 	scmd->satacmd_addr_type = 0;
6086 	scmd->satacmd_device_reg = 0;
6087 	scmd->satacmd_status_reg = 0;
6088 	scmd->satacmd_error_reg = 0;
6089 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
6090 	if (page->dra != dra || page->rcd != dra) {
6091 		/* Need to flip read ahead setting */
6092 		if (dra == 0)
6093 			/* Disable read ahead / read cache */
6094 			scmd->satacmd_features_reg =
6095 			    SATAC_SF_DISABLE_READ_AHEAD;
6096 		else
6097 			/* Enable read ahead  / read cache */
6098 			scmd->satacmd_features_reg =
6099 			    SATAC_SF_ENABLE_READ_AHEAD;
6100 
6101 		/* Transfer command to HBA */
6102 		if (sata_hba_start(spx, rval) != 0)
6103 			/*
6104 			 * Pkt not accepted for execution.
6105 			 */
6106 			return (SATA_FAILURE);
6107 
6108 		*dmod = 1;
6109 
6110 		/* Now process return */
6111 		if (spx->txlt_sata_pkt->satapkt_reason !=
6112 		    SATA_PKT_COMPLETED) {
6113 			goto failure;	/* Terminate */
6114 		}
6115 	}
6116 
6117 	/* Note that the packet is not removed, so it could be re-used */
6118 	if (page->wce != wce) {
6119 		/* Need to flip Write Cache setting */
6120 		if (page->wce == 1)
6121 			/* Enable write cache */
6122 			scmd->satacmd_features_reg =
6123 			    SATAC_SF_ENABLE_WRITE_CACHE;
6124 		else
6125 			/* Disable write cache */
6126 			scmd->satacmd_features_reg =
6127 			    SATAC_SF_DISABLE_WRITE_CACHE;
6128 
6129 		/* Transfer command to HBA */
6130 		if (sata_hba_start(spx, rval) != 0)
6131 			/*
6132 			 * Pkt not accepted for execution.
6133 			 */
6134 			return (SATA_FAILURE);
6135 
6136 		*dmod = 1;
6137 
6138 		/* Now process return */
6139 		if (spx->txlt_sata_pkt->satapkt_reason !=
6140 		    SATA_PKT_COMPLETED) {
6141 			goto failure;
6142 		}
6143 	}
6144 	return (SATA_SUCCESS);
6145 
6146 failure:
6147 	sata_xlate_errors(spx);
6148 
6149 	return (SATA_FAILURE);
6150 }
6151 
6152 /*
6153  * Process mode select informational exceptions control page 0x1c
6154  *
6155  * The only changeable bit is dexcpt (disable exceptions).
6156  * MRIE (method of reporting informational exceptions) must be
6157  * "only on request".
6158  *
6159  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
6160  * If operation resulted in changing device setup, dmod flag should be set to
6161  * one (1). If parameters were not changed, dmod flag should be set to 0.
6162  * Upon return, if operation required sending command to the device, the rval
6163  * should be set to the value returned by sata_hba_start. If operation
6164  * did not require device access, rval should be set to TRAN_ACCEPT.
6165  * The pagelen should be set to the length of the page.
6166  *
6167  * This function has to be called with a port mutex held.
6168  *
6169  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6170  */
6171 static	int
6172 sata_mode_select_page_1c(
6173 	sata_pkt_txlate_t *spx,
6174 	struct mode_info_excpt_page *page,
6175 	int parmlen,
6176 	int *pagelen,
6177 	int *rval,
6178 	int *dmod)
6179 {
6180 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6181 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6182 	sata_drive_info_t *sdinfo;
6183 	sata_id_t *sata_id;
6184 	struct scsi_extended_sense *sense;
6185 
6186 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6187 	    &spx->txlt_sata_pkt->satapkt_device);
6188 	sata_id = &sdinfo->satadrv_id;
6189 
6190 	*dmod = 0;
6191 
6192 	/* Verify parameters length. If too short, drop it */
6193 	if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) < parmlen) ||
6194 	    page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) {
6195 		*scsipkt->pkt_scbp = STATUS_CHECK;
6196 		sense = sata_arq_sense(spx);
6197 		sense->es_key = KEY_ILLEGAL_REQUEST;
6198 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6199 		*pagelen = parmlen;
6200 		*rval = TRAN_ACCEPT;
6201 		return (SATA_FAILURE);
6202 	}
6203 
6204 	*pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page);
6205 
6206 	if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
6207 		*scsipkt->pkt_scbp = STATUS_CHECK;
6208 		sense = sata_arq_sense(spx);
6209 		sense->es_key = KEY_ILLEGAL_REQUEST;
6210 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6211 		*pagelen = parmlen;
6212 		*rval = TRAN_ACCEPT;
6213 		return (SATA_FAILURE);
6214 	}
6215 
6216 	/* If already in the state requested, we are done */
6217 	if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
6218 		/* nothing to do */
6219 		*rval = TRAN_ACCEPT;
6220 		return (SATA_SUCCESS);
6221 	}
6222 
6223 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6224 
6225 	/* Build SMART_ENABLE or SMART_DISABLE command */
6226 	scmd->satacmd_addr_type = 0;		/* N/A */
6227 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
6228 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
6229 	scmd->satacmd_features_reg = page->dexcpt ?
6230 	    SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS;
6231 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
6232 	scmd->satacmd_cmd_reg = SATAC_SMART;
6233 
6234 	/* Transfer command to HBA */
6235 	if (sata_hba_start(spx, rval) != 0)
6236 		/*
6237 		 * Pkt not accepted for execution.
6238 		 */
6239 		return (SATA_FAILURE);
6240 
6241 	*dmod = 1;	/* At least may have been modified */
6242 
6243 	/* Now process return */
6244 	if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED)
6245 		return (SATA_SUCCESS);
6246 
6247 	/* Packet did not complete successfully */
6248 	sata_xlate_errors(spx);
6249 
6250 	return (SATA_FAILURE);
6251 }
6252 
6253 int
6254 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct
6255     mode_acoustic_management *page, int parmlen, int *pagelen,
6256     int *rval, int *dmod)
6257 {
6258 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6259 	sata_drive_info_t *sdinfo;
6260 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6261 	sata_id_t *sata_id;
6262 	struct scsi_extended_sense *sense;
6263 
6264 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6265 	    &spx->txlt_sata_pkt->satapkt_device);
6266 	sata_id = &sdinfo->satadrv_id;
6267 	*dmod = 0;
6268 
6269 	/* If parmlen is too short or the feature is not supported, drop it */
6270 	if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6271 	    sizeof (struct mode_page)) < parmlen) ||
6272 	    (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) {
6273 		*scsipkt->pkt_scbp = STATUS_CHECK;
6274 		sense = sata_arq_sense(spx);
6275 		sense->es_key = KEY_ILLEGAL_REQUEST;
6276 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6277 		*pagelen = parmlen;
6278 		*rval = TRAN_ACCEPT;
6279 		return (SATA_FAILURE);
6280 	}
6281 
6282 	*pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6283 	    sizeof (struct mode_page);
6284 
6285 	/*
6286 	 * We can enable and disable acoustice management and
6287 	 * set the acoustic management level.
6288 	 */
6289 
6290 	/*
6291 	 * Set-up Internal SET FEATURES command(s)
6292 	 */
6293 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6294 	scmd->satacmd_addr_type = 0;
6295 	scmd->satacmd_device_reg = 0;
6296 	scmd->satacmd_status_reg = 0;
6297 	scmd->satacmd_error_reg = 0;
6298 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
6299 	if (page->acoustic_manag_enable) {
6300 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC;
6301 		scmd->satacmd_sec_count_lsb = page->acoustic_manag_level;
6302 	} else {	/* disabling acoustic management */
6303 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC;
6304 	}
6305 
6306 	/* Transfer command to HBA */
6307 	if (sata_hba_start(spx, rval) != 0)
6308 		/*
6309 		 * Pkt not accepted for execution.
6310 		 */
6311 		return (SATA_FAILURE);
6312 
6313 	/* Now process return */
6314 	if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) {
6315 		sata_xlate_errors(spx);
6316 		return (SATA_FAILURE);
6317 	}
6318 
6319 	*dmod = 1;
6320 
6321 	return (SATA_SUCCESS);
6322 }
6323 
6324 
6325 
6326 
6327 /*
6328  * sata_build_lsense_page0() is used to create the
6329  * SCSI LOG SENSE page 0 (supported log pages)
6330  *
6331  * Currently supported pages are 0, 0x10, 0x2f and 0x30
6332  * (supported log pages, self-test results, informational exceptions
6333  *  and Sun vendor specific ATA SMART data).
6334  *
6335  * Takes a sata_drive_info t * and the address of a buffer
6336  * in which to create the page information.
6337  *
6338  * Returns the number of bytes valid in the buffer.
6339  */
6340 static	int
6341 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf)
6342 {
6343 	struct log_parameter *lpp = (struct log_parameter *)buf;
6344 	uint8_t *page_ptr = (uint8_t *)lpp->param_values;
6345 	int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */
6346 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6347 
6348 	lpp->param_code[0] = 0;
6349 	lpp->param_code[1] = 0;
6350 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
6351 	*page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES;
6352 
6353 	if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
6354 		if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) {
6355 			*page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS;
6356 			++num_pages_supported;
6357 		}
6358 		*page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS;
6359 		++num_pages_supported;
6360 		*page_ptr++ = PAGE_CODE_SMART_READ_DATA;
6361 		++num_pages_supported;
6362 	}
6363 
6364 	lpp->param_len = num_pages_supported;
6365 
6366 	return ((&lpp->param_values[0] - (uint8_t *)lpp) +
6367 	    num_pages_supported);
6368 }
6369 
6370 /*
6371  * sata_build_lsense_page_10() is used to create the
6372  * SCSI LOG SENSE page 0x10 (self-test results)
6373  *
6374  * Takes a sata_drive_info t * and the address of a buffer
6375  * in which to create the page information as well as a sata_hba_inst_t *.
6376  *
6377  * Returns the number of bytes valid in the buffer.
6378  */
6379 static	int
6380 sata_build_lsense_page_10(
6381 	sata_drive_info_t *sdinfo,
6382 	uint8_t *buf,
6383 	sata_hba_inst_t *sata_hba_inst)
6384 {
6385 	struct log_parameter *lpp = (struct log_parameter *)buf;
6386 	int rval;
6387 
6388 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6389 		struct smart_ext_selftest_log *ext_selftest_log;
6390 
6391 		ext_selftest_log = kmem_zalloc(
6392 		    sizeof (struct smart_ext_selftest_log), KM_SLEEP);
6393 
6394 		rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo,
6395 		    ext_selftest_log, 0);
6396 		if (rval == 0) {
6397 			int index, start_index;
6398 			struct smart_ext_selftest_log_entry *entry;
6399 			static const struct smart_ext_selftest_log_entry empty =
6400 			    {0};
6401 			uint16_t block_num;
6402 			int count;
6403 			boolean_t only_one_block = B_FALSE;
6404 
6405 			index = ext_selftest_log->
6406 			    smart_ext_selftest_log_index[0];
6407 			index |= ext_selftest_log->
6408 			    smart_ext_selftest_log_index[1] << 8;
6409 			if (index == 0)
6410 				goto out;
6411 
6412 			--index;	/* Correct for 0 origin */
6413 			start_index = index;	/* remember where we started */
6414 			block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6415 			if (block_num != 0) {
6416 				rval = sata_ext_smart_selftest_read_log(
6417 				    sata_hba_inst, sdinfo, ext_selftest_log,
6418 				    block_num);
6419 				if (rval != 0)
6420 					goto out;
6421 			}
6422 			index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6423 			entry =
6424 			    &ext_selftest_log->
6425 			    smart_ext_selftest_log_entries[index];
6426 
6427 			for (count = 1;
6428 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
6429 			    ++count) {
6430 				uint8_t status;
6431 				uint8_t code;
6432 				uint8_t sense_key;
6433 				uint8_t add_sense_code;
6434 				uint8_t add_sense_code_qual;
6435 
6436 				/* If this is an unused entry, we are done */
6437 				if (bcmp(entry, &empty, sizeof (empty)) == 0) {
6438 					/* Broken firmware on some disks */
6439 					if (index + 1 ==
6440 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK) {
6441 						--entry;
6442 						--index;
6443 						if (bcmp(entry, &empty,
6444 						    sizeof (empty)) == 0)
6445 							goto out;
6446 					} else
6447 						goto out;
6448 				}
6449 
6450 				if (only_one_block &&
6451 				    start_index == index)
6452 					goto out;
6453 
6454 				lpp->param_code[0] = 0;
6455 				lpp->param_code[1] = count;
6456 				lpp->param_ctrl_flags =
6457 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
6458 				lpp->param_len =
6459 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
6460 
6461 				status = entry->smart_ext_selftest_log_status;
6462 				status >>= 4;
6463 				switch (status) {
6464 				case 0:
6465 				default:
6466 					sense_key = KEY_NO_SENSE;
6467 					add_sense_code =
6468 					    SD_SCSI_ASC_NO_ADD_SENSE;
6469 					add_sense_code_qual = 0;
6470 					break;
6471 				case 1:
6472 					sense_key = KEY_ABORTED_COMMAND;
6473 					add_sense_code =
6474 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6475 					add_sense_code_qual = SCSI_COMPONENT_81;
6476 					break;
6477 				case 2:
6478 					sense_key = KEY_ABORTED_COMMAND;
6479 					add_sense_code =
6480 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6481 					add_sense_code_qual = SCSI_COMPONENT_82;
6482 					break;
6483 				case 3:
6484 					sense_key = KEY_ABORTED_COMMAND;
6485 					add_sense_code =
6486 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6487 					add_sense_code_qual = SCSI_COMPONENT_83;
6488 					break;
6489 				case 4:
6490 					sense_key = KEY_HARDWARE_ERROR;
6491 					add_sense_code =
6492 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6493 					add_sense_code_qual = SCSI_COMPONENT_84;
6494 					break;
6495 				case 5:
6496 					sense_key = KEY_HARDWARE_ERROR;
6497 					add_sense_code =
6498 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6499 					add_sense_code_qual = SCSI_COMPONENT_85;
6500 					break;
6501 				case 6:
6502 					sense_key = KEY_HARDWARE_ERROR;
6503 					add_sense_code =
6504 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6505 					add_sense_code_qual = SCSI_COMPONENT_86;
6506 					break;
6507 				case 7:
6508 					sense_key = KEY_MEDIUM_ERROR;
6509 					add_sense_code =
6510 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6511 					add_sense_code_qual = SCSI_COMPONENT_87;
6512 					break;
6513 				case 8:
6514 					sense_key = KEY_HARDWARE_ERROR;
6515 					add_sense_code =
6516 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6517 					add_sense_code_qual = SCSI_COMPONENT_88;
6518 					break;
6519 				}
6520 				code = 0;	/* unspecified */
6521 				status |= (code << 4);
6522 				lpp->param_values[0] = status;
6523 				lpp->param_values[1] = 0; /* unspecified */
6524 				lpp->param_values[2] = entry->
6525 				    smart_ext_selftest_log_timestamp[1];
6526 				lpp->param_values[3] = entry->
6527 				    smart_ext_selftest_log_timestamp[0];
6528 				if (status != 0) {
6529 					lpp->param_values[4] = 0;
6530 					lpp->param_values[5] = 0;
6531 					lpp->param_values[6] = entry->
6532 					    smart_ext_selftest_log_failing_lba
6533 					    [5];
6534 					lpp->param_values[7] = entry->
6535 					    smart_ext_selftest_log_failing_lba
6536 					    [4];
6537 					lpp->param_values[8] = entry->
6538 					    smart_ext_selftest_log_failing_lba
6539 					    [3];
6540 					lpp->param_values[9] = entry->
6541 					    smart_ext_selftest_log_failing_lba
6542 					    [2];
6543 					lpp->param_values[10] = entry->
6544 					    smart_ext_selftest_log_failing_lba
6545 					    [1];
6546 					lpp->param_values[11] = entry->
6547 					    smart_ext_selftest_log_failing_lba
6548 					    [0];
6549 				} else {	/* No bad block address */
6550 					lpp->param_values[4] = 0xff;
6551 					lpp->param_values[5] = 0xff;
6552 					lpp->param_values[6] = 0xff;
6553 					lpp->param_values[7] = 0xff;
6554 					lpp->param_values[8] = 0xff;
6555 					lpp->param_values[9] = 0xff;
6556 					lpp->param_values[10] = 0xff;
6557 					lpp->param_values[11] = 0xff;
6558 				}
6559 
6560 				lpp->param_values[12] = sense_key;
6561 				lpp->param_values[13] = add_sense_code;
6562 				lpp->param_values[14] = add_sense_code_qual;
6563 				lpp->param_values[15] = 0; /* undefined */
6564 
6565 				lpp = (struct log_parameter *)
6566 				    (((uint8_t *)lpp) +
6567 				    SCSI_LOG_PARAM_HDR_LEN +
6568 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
6569 
6570 				--index;	/* Back up to previous entry */
6571 				if (index < 0) {
6572 					if (block_num > 0) {
6573 						--block_num;
6574 					} else {
6575 						struct read_log_ext_directory
6576 						    logdir;
6577 
6578 						rval =
6579 						    sata_read_log_ext_directory(
6580 						    sata_hba_inst, sdinfo,
6581 						    &logdir);
6582 						if (rval == -1)
6583 							goto out;
6584 						if ((logdir.read_log_ext_vers
6585 						    [0] == 0) &&
6586 						    (logdir.read_log_ext_vers
6587 						    [1] == 0))
6588 							goto out;
6589 						block_num =
6590 						    logdir.read_log_ext_nblks
6591 						    [EXT_SMART_SELFTEST_LOG_PAGE
6592 						    - 1][0];
6593 						block_num |= logdir.
6594 						    read_log_ext_nblks
6595 						    [EXT_SMART_SELFTEST_LOG_PAGE
6596 						    - 1][1] << 8;
6597 						--block_num;
6598 						only_one_block =
6599 						    (block_num == 0);
6600 					}
6601 					rval = sata_ext_smart_selftest_read_log(
6602 					    sata_hba_inst, sdinfo,
6603 					    ext_selftest_log, block_num);
6604 					if (rval != 0)
6605 						goto out;
6606 
6607 					index =
6608 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK -
6609 					    1;
6610 				}
6611 				index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6612 				entry = &ext_selftest_log->
6613 				    smart_ext_selftest_log_entries[index];
6614 			}
6615 		}
6616 out:
6617 		kmem_free(ext_selftest_log,
6618 		    sizeof (struct smart_ext_selftest_log));
6619 	} else {
6620 		struct smart_selftest_log *selftest_log;
6621 
6622 		selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log),
6623 		    KM_SLEEP);
6624 
6625 		rval = sata_smart_selftest_log(sata_hba_inst, sdinfo,
6626 		    selftest_log);
6627 
6628 		if (rval == 0) {
6629 			int index;
6630 			int count;
6631 			struct smart_selftest_log_entry *entry;
6632 			static const struct smart_selftest_log_entry empty =
6633 			    { 0 };
6634 
6635 			index = selftest_log->smart_selftest_log_index;
6636 			if (index == 0)
6637 				goto done;
6638 			--index;	/* Correct for 0 origin */
6639 			entry = &selftest_log->
6640 			    smart_selftest_log_entries[index];
6641 			for (count = 1;
6642 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
6643 			    ++count) {
6644 				uint8_t status;
6645 				uint8_t code;
6646 				uint8_t sense_key;
6647 				uint8_t add_sense_code;
6648 				uint8_t add_sense_code_qual;
6649 
6650 				if (bcmp(entry, &empty, sizeof (empty)) == 0)
6651 					goto done;
6652 
6653 				lpp->param_code[0] = 0;
6654 				lpp->param_code[1] = count;
6655 				lpp->param_ctrl_flags =
6656 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
6657 				lpp->param_len =
6658 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
6659 
6660 				status = entry->smart_selftest_log_status;
6661 				status >>= 4;
6662 				switch (status) {
6663 				case 0:
6664 				default:
6665 					sense_key = KEY_NO_SENSE;
6666 					add_sense_code =
6667 					    SD_SCSI_ASC_NO_ADD_SENSE;
6668 					break;
6669 				case 1:
6670 					sense_key = KEY_ABORTED_COMMAND;
6671 					add_sense_code =
6672 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6673 					add_sense_code_qual = SCSI_COMPONENT_81;
6674 					break;
6675 				case 2:
6676 					sense_key = KEY_ABORTED_COMMAND;
6677 					add_sense_code =
6678 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6679 					add_sense_code_qual = SCSI_COMPONENT_82;
6680 					break;
6681 				case 3:
6682 					sense_key = KEY_ABORTED_COMMAND;
6683 					add_sense_code =
6684 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6685 					add_sense_code_qual = SCSI_COMPONENT_83;
6686 					break;
6687 				case 4:
6688 					sense_key = KEY_HARDWARE_ERROR;
6689 					add_sense_code =
6690 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6691 					add_sense_code_qual = SCSI_COMPONENT_84;
6692 					break;
6693 				case 5:
6694 					sense_key = KEY_HARDWARE_ERROR;
6695 					add_sense_code =
6696 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6697 					add_sense_code_qual = SCSI_COMPONENT_85;
6698 					break;
6699 				case 6:
6700 					sense_key = KEY_HARDWARE_ERROR;
6701 					add_sense_code =
6702 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6703 					add_sense_code_qual = SCSI_COMPONENT_86;
6704 					break;
6705 				case 7:
6706 					sense_key = KEY_MEDIUM_ERROR;
6707 					add_sense_code =
6708 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6709 					add_sense_code_qual = SCSI_COMPONENT_87;
6710 					break;
6711 				case 8:
6712 					sense_key = KEY_HARDWARE_ERROR;
6713 					add_sense_code =
6714 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6715 					add_sense_code_qual = SCSI_COMPONENT_88;
6716 					break;
6717 				}
6718 				code = 0;	/* unspecified */
6719 				status |= (code << 4);
6720 				lpp->param_values[0] = status;
6721 				lpp->param_values[1] = 0; /* unspecified */
6722 				lpp->param_values[2] = entry->
6723 				    smart_selftest_log_timestamp[1];
6724 				lpp->param_values[3] = entry->
6725 				    smart_selftest_log_timestamp[0];
6726 				if (status != 0) {
6727 					lpp->param_values[4] = 0;
6728 					lpp->param_values[5] = 0;
6729 					lpp->param_values[6] = 0;
6730 					lpp->param_values[7] = 0;
6731 					lpp->param_values[8] = entry->
6732 					    smart_selftest_log_failing_lba[3];
6733 					lpp->param_values[9] = entry->
6734 					    smart_selftest_log_failing_lba[2];
6735 					lpp->param_values[10] = entry->
6736 					    smart_selftest_log_failing_lba[1];
6737 					lpp->param_values[11] = entry->
6738 					    smart_selftest_log_failing_lba[0];
6739 				} else {	/* No block address */
6740 					lpp->param_values[4] = 0xff;
6741 					lpp->param_values[5] = 0xff;
6742 					lpp->param_values[6] = 0xff;
6743 					lpp->param_values[7] = 0xff;
6744 					lpp->param_values[8] = 0xff;
6745 					lpp->param_values[9] = 0xff;
6746 					lpp->param_values[10] = 0xff;
6747 					lpp->param_values[11] = 0xff;
6748 				}
6749 				lpp->param_values[12] = sense_key;
6750 				lpp->param_values[13] = add_sense_code;
6751 				lpp->param_values[14] = add_sense_code_qual;
6752 				lpp->param_values[15] = 0; /* undefined */
6753 
6754 				lpp = (struct log_parameter *)
6755 				    (((uint8_t *)lpp) +
6756 				    SCSI_LOG_PARAM_HDR_LEN +
6757 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
6758 				--index;	/* back up to previous entry */
6759 				if (index < 0) {
6760 					index =
6761 					    NUM_SMART_SELFTEST_LOG_ENTRIES - 1;
6762 				}
6763 				entry = &selftest_log->
6764 				    smart_selftest_log_entries[index];
6765 			}
6766 		}
6767 done:
6768 		kmem_free(selftest_log, sizeof (struct smart_selftest_log));
6769 	}
6770 
6771 	return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) *
6772 	    SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS);
6773 }
6774 
6775 /*
6776  * sata_build_lsense_page_2f() is used to create the
6777  * SCSI LOG SENSE page 0x10 (informational exceptions)
6778  *
6779  * Takes a sata_drive_info t * and the address of a buffer
6780  * in which to create the page information as well as a sata_hba_inst_t *.
6781  *
6782  * Returns the number of bytes valid in the buffer.
6783  */
6784 static	int
6785 sata_build_lsense_page_2f(
6786 	sata_drive_info_t *sdinfo,
6787 	uint8_t *buf,
6788 	sata_hba_inst_t *sata_hba_inst)
6789 {
6790 	struct log_parameter *lpp = (struct log_parameter *)buf;
6791 	int rval;
6792 	uint8_t *smart_data;
6793 	uint8_t temp;
6794 	sata_id_t *sata_id;
6795 #define	SMART_NO_TEMP	0xff
6796 
6797 	lpp->param_code[0] = 0;
6798 	lpp->param_code[1] = 0;
6799 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
6800 
6801 	/* Now get the SMART status w.r.t. threshold exceeded */
6802 	rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo);
6803 	switch (rval) {
6804 	case 1:
6805 		lpp->param_values[0] = SCSI_PREDICTED_FAILURE;
6806 		lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE;
6807 		break;
6808 	case 0:
6809 	case -1:	/* failed to get data */
6810 		lpp->param_values[0] = 0;	/* No failure predicted */
6811 		lpp->param_values[1] = 0;
6812 		break;
6813 #if defined(SATA_DEBUG)
6814 	default:
6815 		cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value");
6816 		/* NOTREACHED */
6817 #endif
6818 	}
6819 
6820 	sata_id = &sdinfo->satadrv_id;
6821 	if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP))
6822 		temp = SMART_NO_TEMP;
6823 	else {
6824 		/* Now get the temperature */
6825 		smart_data = kmem_zalloc(512, KM_SLEEP);
6826 		rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data,
6827 		    SCT_STATUS_LOG_PAGE, 1);
6828 		if (rval == -1)
6829 			temp = SMART_NO_TEMP;
6830 		else {
6831 			temp = smart_data[200];
6832 			if (temp & 0x80) {
6833 				if (temp & 0x7f)
6834 					temp = 0;
6835 				else
6836 					temp = SMART_NO_TEMP;
6837 			}
6838 		}
6839 		kmem_free(smart_data, 512);
6840 	}
6841 
6842 	lpp->param_values[2] = temp;	/* most recent temperature */
6843 	lpp->param_values[3] = 0;	/* required vendor specific byte */
6844 
6845 	lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN;
6846 
6847 
6848 	return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN);
6849 }
6850 
6851 /*
6852  * sata_build_lsense_page_30() is used to create the
6853  * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data).
6854  *
6855  * Takes a sata_drive_info t * and the address of a buffer
6856  * in which to create the page information as well as a sata_hba_inst_t *.
6857  *
6858  * Returns the number of bytes valid in the buffer.
6859  */
6860 static int
6861 sata_build_lsense_page_30(
6862 	sata_drive_info_t *sdinfo,
6863 	uint8_t *buf,
6864 	sata_hba_inst_t *sata_hba_inst)
6865 {
6866 	struct smart_data *smart_data = (struct smart_data *)buf;
6867 	int rval;
6868 
6869 	/* Now do the SMART READ DATA */
6870 	rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data);
6871 	if (rval == -1)
6872 		return (0);
6873 
6874 	return (sizeof (struct smart_data));
6875 }
6876 
6877 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */
6878 
6879 /*
6880  * Start command for ATAPI device.
6881  * This function processes scsi_pkt requests.
6882  * Only CD/DVD devices are supported.
6883  * Most commands are packet without any translation into Packet Command.
6884  * Some may be trapped and executed as SATA commands (not clear which one).
6885  *
6886  * Returns TRAN_ACCEPT if command is accepted for execution (or completed
6887  * execution).
6888  * Returns other TRAN_XXXX codes if command is not accepted or completed
6889  * (see return values for sata_hba_start()).
6890  *
6891  * Note:
6892  * Inquiry cdb format differs between transport version 2 and 3.
6893  * However, the transport version 3 devices that were checked did not adhere
6894  * to the specification (ignored MSB of the allocation length). Therefore,
6895  * the transport version is not checked, but Inquiry allocation length is
6896  * truncated to 255 bytes if the original allocation length set-up by the
6897  * target driver is greater than 255 bytes.
6898  */
6899 static int
6900 sata_txlt_atapi(sata_pkt_txlate_t *spx)
6901 {
6902 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6903 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6904 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
6905 	sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx);
6906 	sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba,
6907 	    &spx->txlt_sata_pkt->satapkt_device);
6908 	int cport = SATA_TXLT_CPORT(spx);
6909 	int cdblen;
6910 	int rval, reason;
6911 	int synch;
6912 	union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp;
6913 
6914 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
6915 
6916 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
6917 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6918 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6919 		return (rval);
6920 	}
6921 
6922 	/*
6923 	 * ATAPI device executes some ATA commands in addition to MMC command
6924 	 * set. These ATA commands may be executed by the regular SATA
6925 	 * translation functions. None needs to be captured now.
6926 	 * Other commands belong to MMC command set and are delivered
6927 	 * to ATAPI device via Packet Command.
6928 	 */
6929 
6930 	/* Check the size of cdb */
6931 	cdblen = scsi_cdb_size[GETGROUP(cdbp)];
6932 	if (cdblen > sdinfo->satadrv_atapi_cdb_len) {
6933 		sata_log(NULL, CE_WARN,
6934 		    "sata: invalid ATAPI cdb length %d",
6935 		    scsipkt->pkt_cdblen);
6936 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6937 		return (TRAN_BADPKT);
6938 	}
6939 
6940 	SATAATAPITRACE(spx, cdblen);
6941 
6942 	/*
6943 	 * For non-read/write commands we need to
6944 	 * map buffer
6945 	 */
6946 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6947 	case SCMD_READ:
6948 	case SCMD_READ_G1:
6949 	case SCMD_READ_G5:
6950 	case SCMD_READ_G4:
6951 	case SCMD_WRITE:
6952 	case SCMD_WRITE_G1:
6953 	case SCMD_WRITE_G5:
6954 	case SCMD_WRITE_G4:
6955 		break;
6956 	default:
6957 		if (bp != NULL) {
6958 			if (bp->b_flags & (B_PHYS | B_PAGEIO))
6959 				bp_mapin(bp);
6960 		}
6961 		break;
6962 	}
6963 	/*
6964 	 * scmd->satacmd_flags.sata_data_direction default -
6965 	 * SATA_DIR_NODATA_XFER - is set by
6966 	 * sata_txlt_generic_pkt_info().
6967 	 */
6968 	if (scmd->satacmd_bp) {
6969 		if (scmd->satacmd_bp->b_flags & B_READ) {
6970 			scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
6971 		} else {
6972 			scmd->satacmd_flags.sata_data_direction =
6973 			    SATA_DIR_WRITE;
6974 		}
6975 	}
6976 
6977 	/*
6978 	 * Set up ATAPI packet command.
6979 	 */
6980 
6981 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
6982 
6983 	/* Copy cdb into sata_cmd */
6984 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
6985 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
6986 	bcopy(cdbp, scmd->satacmd_acdb, cdblen);
6987 
6988 	/* See note in the command header */
6989 	if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) {
6990 		if (scmd->satacmd_acdb[3] != 0)
6991 			scmd->satacmd_acdb[4] = 255;
6992 	}
6993 
6994 #ifdef SATA_DEBUG
6995 	if (sata_debug_flags & SATA_DBG_ATAPI) {
6996 		uint8_t *p = scmd->satacmd_acdb;
6997 		char buf[3 * SATA_ATAPI_MAX_CDB_LEN];
6998 
6999 		(void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN,
7000 		    "%02x %02x %02x %02x %02x %02x %02x %02x "
7001 		    "%2x %02x %02x %02x %02x %02x %02x %02x",
7002 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7003 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7004 		buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0';
7005 		cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf);
7006 	}
7007 #endif
7008 
7009 	/*
7010 	 * Preset request sense data to NO SENSE.
7011 	 * If there is no way to get error information via Request Sense,
7012 	 * the packet request sense data would not have to be modified by HBA,
7013 	 * but it could be returned as is.
7014 	 */
7015 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
7016 	sata_fixed_sense_data_preset(
7017 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7018 
7019 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
7020 		/* Need callback function */
7021 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion;
7022 		synch = FALSE;
7023 	} else
7024 		synch = TRUE;
7025 
7026 	/* Transfer command to HBA */
7027 	if (sata_hba_start(spx, &rval) != 0) {
7028 		/* Pkt not accepted for execution */
7029 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
7030 		return (rval);
7031 	}
7032 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
7033 	/*
7034 	 * If execution is non-synchronous,
7035 	 * a callback function will handle potential errors, translate
7036 	 * the response and will do a callback to a target driver.
7037 	 * If it was synchronous, use the same framework callback to check
7038 	 * an execution status.
7039 	 */
7040 	if (synch) {
7041 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7042 		    "synchronous execution status %x\n",
7043 		    spx->txlt_sata_pkt->satapkt_reason);
7044 		sata_txlt_atapi_completion(spx->txlt_sata_pkt);
7045 	}
7046 	return (TRAN_ACCEPT);
7047 }
7048 
7049 
7050 /*
7051  * ATAPI Packet command completion.
7052  *
7053  * Failure of the command passed via Packet command are considered device
7054  * error. SATA HBA driver would have to retrieve error data (via Request
7055  * Sense command delivered via error retrieval sata packet) and copy it
7056  * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data.
7057  */
7058 static void
7059 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt)
7060 {
7061 	sata_pkt_txlate_t *spx =
7062 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7063 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7064 	struct scsi_extended_sense *sense;
7065 	struct buf *bp;
7066 	int rval;
7067 
7068 #ifdef SATA_DEBUG
7069 	uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense;
7070 #endif
7071 
7072 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7073 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7074 
7075 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7076 		/* Normal completion */
7077 		if (sata_pkt->satapkt_cmd.satacmd_bp != NULL)
7078 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
7079 		scsipkt->pkt_reason = CMD_CMPLT;
7080 		*scsipkt->pkt_scbp = STATUS_GOOD;
7081 		if (spx->txlt_tmp_buf != NULL) {
7082 			/* Temporary buffer was used */
7083 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7084 			if (bp->b_flags & B_READ) {
7085 				rval = ddi_dma_sync(
7086 				    spx->txlt_buf_dma_handle, 0, 0,
7087 				    DDI_DMA_SYNC_FORCPU);
7088 				ASSERT(rval == DDI_SUCCESS);
7089 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7090 				    bp->b_bcount);
7091 			}
7092 		}
7093 	} else {
7094 		/*
7095 		 * Something went wrong - analyze return
7096 		 */
7097 		*scsipkt->pkt_scbp = STATUS_CHECK;
7098 		sense = sata_arq_sense(spx);
7099 
7100 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7101 			scsipkt->pkt_reason = CMD_INCOMPLETE;
7102 			/*
7103 			 * We may not have ARQ data if there was a double
7104 			 * error. But sense data in sata packet was pre-set
7105 			 * with NO SENSE so it is valid even if HBA could
7106 			 * not retrieve a real sense data.
7107 			 * Just copy this sense data into scsi pkt sense area.
7108 			 */
7109 			bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense,
7110 			    SATA_ATAPI_MIN_RQSENSE_LEN);
7111 #ifdef SATA_DEBUG
7112 			if (sata_debug_flags & SATA_DBG_SCSI_IF) {
7113 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7114 				    "sata_txlt_atapi_completion: %02x\n"
7115 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
7116 				    "          %02x %02x %02x %02x %02x %02x "
7117 				    "          %02x %02x %02x %02x %02x %02x\n",
7118 				    scsipkt->pkt_reason,
7119 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7120 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7121 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7122 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7123 				    rqsp[16], rqsp[17]);
7124 			}
7125 #endif
7126 		} else {
7127 			switch (sata_pkt->satapkt_reason) {
7128 			case SATA_PKT_PORT_ERROR:
7129 				/*
7130 				 * We have no device data.
7131 				 */
7132 				scsipkt->pkt_reason = CMD_INCOMPLETE;
7133 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
7134 				    STATE_GOT_TARGET | STATE_SENT_CMD |
7135 				    STATE_GOT_STATUS);
7136 				sense->es_key = KEY_HARDWARE_ERROR;
7137 				break;
7138 
7139 			case SATA_PKT_TIMEOUT:
7140 				scsipkt->pkt_reason = CMD_TIMEOUT;
7141 				scsipkt->pkt_statistics |=
7142 				    STAT_TIMEOUT | STAT_DEV_RESET;
7143 				/*
7144 				 * Need to check if HARDWARE_ERROR/
7145 				 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more
7146 				 * appropriate.
7147 				 */
7148 				break;
7149 
7150 			case SATA_PKT_ABORTED:
7151 				scsipkt->pkt_reason = CMD_ABORTED;
7152 				scsipkt->pkt_statistics |= STAT_ABORTED;
7153 				/* Should we set key COMMAND_ABPRTED? */
7154 				break;
7155 
7156 			case SATA_PKT_RESET:
7157 				scsipkt->pkt_reason = CMD_RESET;
7158 				scsipkt->pkt_statistics |= STAT_DEV_RESET;
7159 				/*
7160 				 * May be we should set Unit Attention /
7161 				 * Reset. Perhaps the same should be
7162 				 * returned for disks....
7163 				 */
7164 				sense->es_key = KEY_UNIT_ATTENTION;
7165 				sense->es_add_code = SD_SCSI_ASC_RESET;
7166 				break;
7167 
7168 			default:
7169 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7170 				    "sata_txlt_atapi_completion: "
7171 				    "invalid packet completion reason"));
7172 				scsipkt->pkt_reason = CMD_TRAN_ERR;
7173 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
7174 				    STATE_GOT_TARGET | STATE_SENT_CMD |
7175 				    STATE_GOT_STATUS);
7176 				break;
7177 			}
7178 		}
7179 	}
7180 
7181 	SATAATAPITRACE(spx, 0);
7182 
7183 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7184 	    scsipkt->pkt_comp != NULL) {
7185 		/* scsi callback required */
7186 		(*scsipkt->pkt_comp)(scsipkt);
7187 	}
7188 }
7189 
7190 /*
7191  * Set up error retrieval sata command for ATAPI Packet Command error data
7192  * recovery.
7193  *
7194  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
7195  * returns SATA_FAILURE otherwise.
7196  */
7197 
7198 static int
7199 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
7200 {
7201 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
7202 	sata_cmd_t *scmd;
7203 	struct buf *bp;
7204 
7205 	/*
7206 	 * Allocate dma-able buffer error data.
7207 	 * Buffer allocation will take care of buffer alignment and other DMA
7208 	 * attributes.
7209 	 */
7210 	bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN);
7211 	if (bp == NULL) {
7212 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
7213 		    "sata_get_err_retrieval_pkt: "
7214 		    "cannot allocate buffer for error data", NULL);
7215 		return (SATA_FAILURE);
7216 	}
7217 	bp_mapin(bp); /* make data buffer accessible */
7218 
7219 	/* Operation modes are up to the caller */
7220 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7221 
7222 	/* Synchronous mode, no callback - may be changed by the caller */
7223 	spkt->satapkt_comp = NULL;
7224 	spkt->satapkt_time = sata_default_pkt_time;
7225 
7226 	scmd = &spkt->satapkt_cmd;
7227 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7228 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7229 
7230 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7231 
7232 	/*
7233 	 * Set-up acdb. Request Sense CDB (packet command content) is
7234 	 * not in DMA-able buffer. Its handling is HBA-specific (how
7235 	 * it is transfered into packet FIS).
7236 	 */
7237 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7238 	bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN);
7239 	/* Following zeroing of pad bytes may not be necessary */
7240 	bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN],
7241 	    sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN);
7242 
7243 	/*
7244 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
7245 	 * before accessing it. Handle is in usual place in translate struct.
7246 	 */
7247 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
7248 
7249 	/*
7250 	 * Preset request sense data to NO SENSE.
7251 	 * Here it is redundant, only for a symetry with scsi-originated
7252 	 * packets. It should not be used for anything but debugging.
7253 	 */
7254 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
7255 	sata_fixed_sense_data_preset(
7256 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7257 
7258 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7259 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7260 
7261 	return (SATA_SUCCESS);
7262 }
7263 
7264 /*
7265  * Set-up ATAPI packet command.
7266  * Data transfer direction has to be set-up in sata_cmd structure prior to
7267  * calling this function.
7268  *
7269  * Returns void
7270  */
7271 
7272 static void
7273 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo)
7274 {
7275 	scmd->satacmd_addr_type = 0;		/* N/A */
7276 	scmd->satacmd_sec_count_lsb = 0;	/* no tag */
7277 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
7278 	scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ;
7279 	scmd->satacmd_lba_high_lsb =
7280 	    (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8);
7281 	scmd->satacmd_cmd_reg = SATAC_PACKET;	/* Command */
7282 
7283 	/*
7284 	 * We want all data to be transfered via DMA.
7285 	 * But specify it only if drive supports DMA and DMA mode is
7286 	 * selected - some drives are sensitive about it.
7287 	 * Hopefully it wil work for all drives....
7288 	 */
7289 	if (sdinfo->satadrv_settings & SATA_DEV_DMA)
7290 		scmd->satacmd_features_reg = SATA_ATAPI_F_DMA;
7291 
7292 	/*
7293 	 * Features register requires special care for devices that use
7294 	 * Serial ATA bridge - they need an explicit specification of
7295 	 * the data transfer direction for Packet DMA commands.
7296 	 * Setting this bit is harmless if DMA is not used.
7297 	 *
7298 	 * Many drives do not implement word 80, specifying what ATA/ATAPI
7299 	 * spec they follow.
7300 	 * We are arbitrarily following the latest SerialATA 2.6 spec,
7301 	 * which uses ATA/ATAPI 6 specification for Identify Data, unless
7302 	 * ATA/ATAPI-7 support is explicitly indicated.
7303 	 */
7304 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
7305 	    sdinfo->satadrv_id.ai_majorversion != 0xffff &&
7306 	    (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) {
7307 		/*
7308 		 * Specification of major version is valid and version 7
7309 		 * is supported. It does automatically imply that all
7310 		 * spec features are supported. For now, we assume that
7311 		 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete.
7312 		 */
7313 		if ((sdinfo->satadrv_id.ai_dirdma &
7314 		    SATA_ATAPI_ID_DMADIR_REQ) != 0) {
7315 			if (scmd->satacmd_flags.sata_data_direction ==
7316 			    SATA_DIR_READ)
7317 			scmd->satacmd_features_reg |=
7318 			    SATA_ATAPI_F_DATA_DIR_READ;
7319 		}
7320 	}
7321 }
7322 
7323 
7324 #ifdef SATA_DEBUG
7325 
7326 /* Display 18 bytes of Inquiry data */
7327 static void
7328 sata_show_inqry_data(uint8_t *buf)
7329 {
7330 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
7331 	uint8_t *p;
7332 
7333 	cmn_err(CE_NOTE, "Inquiry data:");
7334 	cmn_err(CE_NOTE, "device type %x", inq->inq_dtype);
7335 	cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb);
7336 	cmn_err(CE_NOTE, "version %x", inq->inq_ansi);
7337 	cmn_err(CE_NOTE, "ATAPI transport version %d",
7338 	    SATA_ATAPI_TRANS_VERSION(inq));
7339 	cmn_err(CE_NOTE, "response data format %d, aenc %d",
7340 	    inq->inq_rdf, inq->inq_aenc);
7341 	cmn_err(CE_NOTE, " additional length %d", inq->inq_len);
7342 	cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs);
7343 	p = (uint8_t *)inq->inq_vid;
7344 	cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x "
7345 	    "%02x %02x %02x %02x",
7346 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
7347 	p = (uint8_t *)inq->inq_vid;
7348 	cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c",
7349 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
7350 
7351 	p = (uint8_t *)inq->inq_pid;
7352 	cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x "
7353 	    "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
7354 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7355 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7356 	p = (uint8_t *)inq->inq_pid;
7357 	cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c "
7358 	    "%c %c %c %c %c %c %c %c",
7359 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7360 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7361 
7362 	p = (uint8_t *)inq->inq_revision;
7363 	cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x",
7364 	    p[0], p[1], p[2], p[3]);
7365 	p = (uint8_t *)inq->inq_revision;
7366 	cmn_err(CE_NOTE, "revision: %c %c %c %c",
7367 	    p[0], p[1], p[2], p[3]);
7368 
7369 }
7370 
7371 
7372 static void
7373 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count)
7374 {
7375 	struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt;
7376 
7377 	if (scsi_pkt == NULL)
7378 		return;
7379 	if (count != 0) {
7380 		/* saving cdb */
7381 		bzero(sata_atapi_trace[sata_atapi_trace_index].acdb,
7382 		    SATA_ATAPI_MAX_CDB_LEN);
7383 		bcopy(scsi_pkt->pkt_cdbp,
7384 		    sata_atapi_trace[sata_atapi_trace_index].acdb, count);
7385 	} else {
7386 		bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)->
7387 		    sts_sensedata,
7388 		    sata_atapi_trace[sata_atapi_trace_index].arqs,
7389 		    SATA_ATAPI_MIN_RQSENSE_LEN);
7390 		sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason =
7391 		    scsi_pkt->pkt_reason;
7392 		sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason =
7393 		    spx->txlt_sata_pkt->satapkt_reason;
7394 
7395 		if (++sata_atapi_trace_index >= 64)
7396 			sata_atapi_trace_index = 0;
7397 	}
7398 }
7399 
7400 #endif
7401 
7402 /*
7403  * Fetch inquiry data from ATAPI device
7404  * Returns SATA_SUCCESS if operation was successfull, SATA_FAILURE otherwise.
7405  *
7406  * Note:
7407  * inqb pointer does not point to a DMA-able buffer. It is a local buffer
7408  * where the caller expects to see the inquiry data.
7409  *
7410  */
7411 
7412 static int
7413 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba,
7414     sata_address_t *saddr, struct scsi_inquiry *inq)
7415 {
7416 	sata_pkt_txlate_t *spx;
7417 	sata_pkt_t *spkt;
7418 	struct buf *bp;
7419 	sata_drive_info_t *sdinfo;
7420 	sata_cmd_t *scmd;
7421 	int rval;
7422 	uint8_t *rqsp;
7423 #ifdef SATA_DEBUG
7424 	char msg_buf[MAXPATHLEN];
7425 #endif
7426 
7427 	ASSERT(sata_hba != NULL);
7428 
7429 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
7430 	spx->txlt_sata_hba_inst = sata_hba;
7431 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
7432 	spkt = sata_pkt_alloc(spx, NULL);
7433 	if (spkt == NULL) {
7434 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7435 		return (SATA_FAILURE);
7436 	}
7437 	/* address is needed now */
7438 	spkt->satapkt_device.satadev_addr = *saddr;
7439 
7440 	/* scsi_inquiry size buffer */
7441 	bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry));
7442 	if (bp == NULL) {
7443 		sata_pkt_free(spx);
7444 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7445 		SATA_LOG_D((sata_hba, CE_WARN,
7446 		    "sata_get_atapi_inquiry_data: "
7447 		    "cannot allocate data buffer"));
7448 		return (SATA_FAILURE);
7449 	}
7450 	bp_mapin(bp); /* make data buffer accessible */
7451 
7452 	scmd = &spkt->satapkt_cmd;
7453 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7454 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7455 
7456 	/* Use synchronous mode */
7457 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7458 	spkt->satapkt_comp = NULL;
7459 	spkt->satapkt_time = sata_default_pkt_time;
7460 
7461 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
7462 
7463 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7464 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7465 
7466 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
7467 	sdinfo = sata_get_device_info(sata_hba,
7468 	    &spx->txlt_sata_pkt->satapkt_device);
7469 	if (sdinfo == NULL) {
7470 		/* we have to be carefull about the disapearing device */
7471 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7472 		rval = SATA_FAILURE;
7473 		goto cleanup;
7474 	}
7475 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7476 
7477 	/*
7478 	 * Set-up acdb. This works for atapi transport version 2 and later.
7479 	 */
7480 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7481 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7482 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
7483 	scmd->satacmd_acdb[1] = 0x00;
7484 	scmd->satacmd_acdb[2] = 0x00;
7485 	scmd->satacmd_acdb[3] = 0x00;
7486 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
7487 	scmd->satacmd_acdb[5] = 0x00;
7488 
7489 	sata_fixed_sense_data_preset(
7490 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7491 
7492 	/* Transfer command to HBA */
7493 	if (sata_hba_start(spx, &rval) != 0) {
7494 		/* Pkt not accepted for execution */
7495 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
7496 		    "sata_get_atapi_inquiry_data: "
7497 		    "Packet not accepted for execution - ret: %02x", rval);
7498 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7499 		rval = SATA_FAILURE;
7500 		goto cleanup;
7501 	}
7502 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7503 
7504 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
7505 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
7506 		    "sata_get_atapi_inquiry_data: "
7507 		    "Packet completed successfully - ret: %02x", rval);
7508 		if (spx->txlt_buf_dma_handle != NULL) {
7509 			/*
7510 			 * Sync buffer. Handle is in usual place in translate
7511 			 * struct.
7512 			 */
7513 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
7514 			    DDI_DMA_SYNC_FORCPU);
7515 			ASSERT(rval == DDI_SUCCESS);
7516 		}
7517 		/*
7518 		 * Normal completion - copy data into caller's buffer
7519 		 */
7520 		bcopy(bp->b_un.b_addr, (uint8_t *)inq,
7521 		    sizeof (struct scsi_inquiry));
7522 #ifdef SATA_DEBUG
7523 		if (sata_debug_flags & SATA_DBG_ATAPI) {
7524 			sata_show_inqry_data((uint8_t *)inq);
7525 		}
7526 #endif
7527 		rval = SATA_SUCCESS;
7528 	} else {
7529 		/*
7530 		 * Something went wrong - analyze return - check rqsense data
7531 		 */
7532 		rval = SATA_FAILURE;
7533 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7534 			/*
7535 			 * ARQ data hopefull show something other than NO SENSE
7536 			 */
7537 			rqsp = scmd->satacmd_rqsense;
7538 #ifdef SATA_DEBUG
7539 			if (sata_debug_flags & SATA_DBG_ATAPI) {
7540 				msg_buf[0] = '\0';
7541 				(void) snprintf(msg_buf, MAXPATHLEN,
7542 				    "ATAPI packet completion reason: %02x\n"
7543 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x\n"
7544 				    "          %02x %02x %02x %02x %02x %02x\n"
7545 				    "          %02x %02x %02x %02x %02x %02x",
7546 				    spkt->satapkt_reason,
7547 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7548 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7549 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7550 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7551 				    rqsp[16], rqsp[17]);
7552 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7553 				    "%s", msg_buf);
7554 			}
7555 #endif
7556 		} else {
7557 			switch (spkt->satapkt_reason) {
7558 			case SATA_PKT_PORT_ERROR:
7559 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7560 				    "sata_get_atapi_inquiry_data: "
7561 				    "packet reason: port error", NULL);
7562 				break;
7563 
7564 			case SATA_PKT_TIMEOUT:
7565 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7566 				    "sata_get_atapi_inquiry_data: "
7567 				    "packet reason: timeout", NULL);
7568 				break;
7569 
7570 			case SATA_PKT_ABORTED:
7571 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7572 				    "sata_get_atapi_inquiry_data: "
7573 				    "packet reason: aborted", NULL);
7574 				break;
7575 
7576 			case SATA_PKT_RESET:
7577 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7578 				    "sata_get_atapi_inquiry_data: "
7579 				    "packet reason: reset\n", NULL);
7580 				break;
7581 			default:
7582 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7583 				    "sata_get_atapi_inquiry_data: "
7584 				    "invalid packet reason: %02x\n",
7585 				    spkt->satapkt_reason);
7586 				break;
7587 			}
7588 		}
7589 	}
7590 cleanup:
7591 	sata_free_local_buffer(spx);
7592 	sata_pkt_free(spx);
7593 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
7594 	return (rval);
7595 }
7596 
7597 
7598 
7599 
7600 
7601 #if 0
7602 #ifdef SATA_DEBUG
7603 
7604 /*
7605  * Test ATAPI packet command.
7606  * Single threaded test: send packet command in synch mode, process completion
7607  *
7608  */
7609 static void
7610 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport)
7611 {
7612 	sata_pkt_txlate_t *spx;
7613 	sata_pkt_t *spkt;
7614 	struct buf *bp;
7615 	sata_device_t sata_device;
7616 	sata_drive_info_t *sdinfo;
7617 	sata_cmd_t *scmd;
7618 	int rval;
7619 	uint8_t *rqsp;
7620 
7621 	ASSERT(sata_hba_inst != NULL);
7622 	sata_device.satadev_addr.cport = cport;
7623 	sata_device.satadev_addr.pmport = 0;
7624 	sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
7625 	sata_device.satadev_rev = SATA_DEVICE_REV;
7626 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7627 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
7628 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7629 	if (sdinfo == NULL) {
7630 		sata_log(sata_hba_inst, CE_WARN,
7631 		    "sata_test_atapi_packet_command: "
7632 		    "no device info for cport %d",
7633 		    sata_device.satadev_addr.cport);
7634 		return;
7635 	}
7636 
7637 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
7638 	spx->txlt_sata_hba_inst = sata_hba_inst;
7639 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
7640 	spkt = sata_pkt_alloc(spx, NULL);
7641 	if (spkt == NULL) {
7642 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7643 		return;
7644 	}
7645 	/* address is needed now */
7646 	spkt->satapkt_device.satadev_addr = sata_device.satadev_addr;
7647 
7648 	/* 1024k buffer */
7649 	bp = sata_alloc_local_buffer(spx, 1024);
7650 	if (bp == NULL) {
7651 		sata_pkt_free(spx);
7652 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7653 		sata_log(sata_hba_inst, CE_WARN,
7654 		    "sata_test_atapi_packet_command: "
7655 		    "cannot allocate data buffer");
7656 		return;
7657 	}
7658 	bp_mapin(bp); /* make data buffer accessible */
7659 
7660 	scmd = &spkt->satapkt_cmd;
7661 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7662 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7663 
7664 	/* Use synchronous mode */
7665 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7666 
7667 	/* Synchronous mode, no callback - may be changed by the caller */
7668 	spkt->satapkt_comp = NULL;
7669 	spkt->satapkt_time = sata_default_pkt_time;
7670 
7671 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
7672 
7673 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7674 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7675 
7676 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7677 
7678 	/* Set-up acdb. */
7679 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7680 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7681 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
7682 	scmd->satacmd_acdb[1] = 0x00;
7683 	scmd->satacmd_acdb[2] = 0x00;
7684 	scmd->satacmd_acdb[3] = 0x00;
7685 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
7686 	scmd->satacmd_acdb[5] = 0x00;
7687 
7688 	sata_fixed_sense_data_preset(
7689 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7690 
7691 	/* Transfer command to HBA */
7692 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7693 	if (sata_hba_start(spx, &rval) != 0) {
7694 		/* Pkt not accepted for execution */
7695 		sata_log(sata_hba_inst, CE_WARN,
7696 		    "sata_test_atapi_packet_command: "
7697 		    "Packet not accepted for execution - ret: %02x", rval);
7698 		mutex_exit(
7699 		    &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7700 		goto cleanup;
7701 	}
7702 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7703 
7704 	if (spx->txlt_buf_dma_handle != NULL) {
7705 		/*
7706 		 * Sync buffer. Handle is in usual place in translate struct.
7707 		 */
7708 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
7709 		    DDI_DMA_SYNC_FORCPU);
7710 		ASSERT(rval == DDI_SUCCESS);
7711 	}
7712 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
7713 		sata_log(sata_hba_inst, CE_WARN,
7714 		    "sata_test_atapi_packet_command: "
7715 		    "Packet completed successfully");
7716 		/*
7717 		 * Normal completion - show inquiry data
7718 		 */
7719 		sata_show_inqry_data((uint8_t *)bp->b_un.b_addr);
7720 	} else {
7721 		/*
7722 		 * Something went wrong - analyze return - check rqsense data
7723 		 */
7724 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7725 			/*
7726 			 * ARQ data hopefull show something other than NO SENSE
7727 			 */
7728 			rqsp = scmd->satacmd_rqsense;
7729 			sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7730 			    "ATAPI packet completion reason: %02x\n"
7731 			    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
7732 			    "          %02x %02x %02x %02x %02x %02x "
7733 			    "          %02x %02x %02x %02x %02x %02x\n",
7734 			    spkt->satapkt_reason,
7735 			    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7736 			    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7737 			    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7738 			    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7739 			    rqsp[16], rqsp[17]);
7740 		} else {
7741 			switch (spkt->satapkt_reason) {
7742 			case SATA_PKT_PORT_ERROR:
7743 				sata_log(sata_hba_inst, CE_WARN,
7744 				    "sata_test_atapi_packet_command: "
7745 				    "packet reason: port error\n");
7746 				break;
7747 
7748 			case SATA_PKT_TIMEOUT:
7749 				sata_log(sata_hba_inst, CE_WARN,
7750 				    "sata_test_atapi_packet_command: "
7751 				    "packet reason: timeout\n");
7752 				break;
7753 
7754 			case SATA_PKT_ABORTED:
7755 				sata_log(sata_hba_inst, CE_WARN,
7756 				    "sata_test_atapi_packet_command: "
7757 				    "packet reason: aborted\n");
7758 				break;
7759 
7760 			case SATA_PKT_RESET:
7761 				sata_log(sata_hba_inst, CE_WARN,
7762 				    "sata_test_atapi_packet_command: "
7763 				    "packet reason: reset\n");
7764 				break;
7765 			default:
7766 				sata_log(sata_hba_inst, CE_WARN,
7767 				    "sata_test_atapi_packet_command: "
7768 				    "invalid packet reason: %02x\n",
7769 				    spkt->satapkt_reason);
7770 				break;
7771 			}
7772 		}
7773 	}
7774 cleanup:
7775 	sata_free_local_buffer(spx);
7776 	sata_pkt_free(spx);
7777 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
7778 }
7779 
7780 #endif /* SATA_DEBUG */
7781 #endif /* 1 */
7782 
7783 
7784 /* ************************** LOCAL HELPER FUNCTIONS *********************** */
7785 
7786 /*
7787  * Validate sata_tran info
7788  * SATA_FAILURE returns if structure is inconsistent or structure revision
7789  * does not match one used by the framework.
7790  *
7791  * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains
7792  * required function pointers.
7793  * Returns SATA_FAILURE otherwise.
7794  */
7795 static int
7796 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran)
7797 {
7798 	/*
7799 	 * SATA_TRAN_HBA_REV is the current (highest) revision number
7800 	 * of the SATA interface.
7801 	 */
7802 	if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) {
7803 		sata_log(NULL, CE_WARN,
7804 		    "sata: invalid sata_hba_tran version %d for driver %s",
7805 		    sata_tran->sata_tran_hba_rev, ddi_driver_name(dip));
7806 		return (SATA_FAILURE);
7807 	}
7808 
7809 	if (dip != sata_tran->sata_tran_hba_dip) {
7810 		SATA_LOG_D((NULL, CE_WARN,
7811 		    "sata: inconsistent sata_tran_hba_dip "
7812 		    "%p / %p", sata_tran->sata_tran_hba_dip, dip));
7813 		return (SATA_FAILURE);
7814 	}
7815 
7816 	if (sata_tran->sata_tran_probe_port == NULL ||
7817 	    sata_tran->sata_tran_start == NULL ||
7818 	    sata_tran->sata_tran_abort == NULL ||
7819 	    sata_tran->sata_tran_reset_dport == NULL ||
7820 	    sata_tran->sata_tran_hotplug_ops == NULL ||
7821 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL ||
7822 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate ==
7823 	    NULL) {
7824 		SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing "
7825 		    "required functions"));
7826 	}
7827 	return (SATA_SUCCESS);
7828 }
7829 
7830 /*
7831  * Remove HBA instance from sata_hba_list.
7832  */
7833 static void
7834 sata_remove_hba_instance(dev_info_t *dip)
7835 {
7836 	sata_hba_inst_t	*sata_hba_inst;
7837 
7838 	mutex_enter(&sata_mutex);
7839 	for (sata_hba_inst = sata_hba_list;
7840 	    sata_hba_inst != (struct sata_hba_inst *)NULL;
7841 	    sata_hba_inst = sata_hba_inst->satahba_next) {
7842 		if (sata_hba_inst->satahba_dip == dip)
7843 			break;
7844 	}
7845 
7846 	if (sata_hba_inst == (struct sata_hba_inst *)NULL) {
7847 #ifdef SATA_DEBUG
7848 		cmn_err(CE_WARN, "sata_remove_hba_instance: "
7849 		    "unknown HBA instance\n");
7850 #endif
7851 		ASSERT(FALSE);
7852 	}
7853 	if (sata_hba_inst == sata_hba_list) {
7854 		sata_hba_list = sata_hba_inst->satahba_next;
7855 		if (sata_hba_list) {
7856 			sata_hba_list->satahba_prev =
7857 			    (struct sata_hba_inst *)NULL;
7858 		}
7859 		if (sata_hba_inst == sata_hba_list_tail) {
7860 			sata_hba_list_tail = NULL;
7861 		}
7862 	} else if (sata_hba_inst == sata_hba_list_tail) {
7863 		sata_hba_list_tail = sata_hba_inst->satahba_prev;
7864 		if (sata_hba_list_tail) {
7865 			sata_hba_list_tail->satahba_next =
7866 			    (struct sata_hba_inst *)NULL;
7867 		}
7868 	} else {
7869 		sata_hba_inst->satahba_prev->satahba_next =
7870 		    sata_hba_inst->satahba_next;
7871 		sata_hba_inst->satahba_next->satahba_prev =
7872 		    sata_hba_inst->satahba_prev;
7873 	}
7874 	mutex_exit(&sata_mutex);
7875 }
7876 
7877 
7878 
7879 
7880 
7881 /*
7882  * Probe all SATA ports of the specified HBA instance.
7883  * The assumption is that there are no target and attachment point minor nodes
7884  * created by the boot subsystems, so we do not need to prune device tree.
7885  *
7886  * This function is called only from sata_hba_attach(). It does not have to
7887  * be protected by controller mutex, because the hba_attached flag is not set
7888  * yet and no one would be touching this HBA instance other than this thread.
7889  * Determines if port is active and what type of the device is attached
7890  * (if any). Allocates necessary structures for each port.
7891  *
7892  * An AP (Attachement Point) node is created for each SATA device port even
7893  * when there is no device attached.
7894  */
7895 
7896 static 	void
7897 sata_probe_ports(sata_hba_inst_t *sata_hba_inst)
7898 {
7899 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
7900 	int			ncport, npmport;
7901 	sata_cport_info_t 	*cportinfo;
7902 	sata_drive_info_t	*drive;
7903 	sata_pmult_info_t	*pminfo;
7904 	sata_pmport_info_t 	*pmportinfo;
7905 	sata_device_t		sata_device;
7906 	int			rval;
7907 	dev_t			minor_number;
7908 	char			name[16];
7909 	clock_t			start_time, cur_time;
7910 
7911 	/*
7912 	 * Probe controller ports first, to find port status and
7913 	 * any port multiplier attached.
7914 	 */
7915 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
7916 		/* allocate cport structure */
7917 		cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP);
7918 		ASSERT(cportinfo != NULL);
7919 		mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL);
7920 
7921 		mutex_enter(&cportinfo->cport_mutex);
7922 
7923 		cportinfo->cport_addr.cport = ncport;
7924 		cportinfo->cport_addr.pmport = 0;
7925 		cportinfo->cport_addr.qual = SATA_ADDR_CPORT;
7926 		cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
7927 		cportinfo->cport_state |= SATA_STATE_PROBING;
7928 		SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo;
7929 
7930 		/*
7931 		 * Regardless if a port is usable or not, create
7932 		 * an attachment point
7933 		 */
7934 		mutex_exit(&cportinfo->cport_mutex);
7935 		minor_number =
7936 		    SATA_MAKE_AP_MINOR(ddi_get_instance(dip), ncport, 0, 0);
7937 		(void) sprintf(name, "%d", ncport);
7938 		if (ddi_create_minor_node(dip, name, S_IFCHR,
7939 		    minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) !=
7940 		    DDI_SUCCESS) {
7941 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
7942 			    "cannot create SATA attachment point for port %d",
7943 			    ncport);
7944 		}
7945 
7946 		/* Probe port */
7947 		start_time = ddi_get_lbolt();
7948 	reprobe_cport:
7949 		sata_device.satadev_addr.cport = ncport;
7950 		sata_device.satadev_addr.pmport = 0;
7951 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
7952 		sata_device.satadev_rev = SATA_DEVICE_REV;
7953 
7954 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
7955 		    (dip, &sata_device);
7956 
7957 		mutex_enter(&cportinfo->cport_mutex);
7958 		sata_update_port_scr(&cportinfo->cport_scr, &sata_device);
7959 		if (rval != SATA_SUCCESS) {
7960 			/* Something went wrong? Fail the port */
7961 			cportinfo->cport_state = SATA_PSTATE_FAILED;
7962 			mutex_exit(&cportinfo->cport_mutex);
7963 			continue;
7964 		}
7965 		cportinfo->cport_state &= ~SATA_STATE_PROBING;
7966 		cportinfo->cport_state |= SATA_STATE_PROBED;
7967 		cportinfo->cport_dev_type = sata_device.satadev_type;
7968 
7969 		cportinfo->cport_state |= SATA_STATE_READY;
7970 		if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) {
7971 			mutex_exit(&cportinfo->cport_mutex);
7972 			continue;
7973 		}
7974 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
7975 			/*
7976 			 * There is some device attached.
7977 			 * Allocate device info structure
7978 			 */
7979 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) {
7980 				mutex_exit(&cportinfo->cport_mutex);
7981 				SATA_CPORTINFO_DRV_INFO(cportinfo) =
7982 				    kmem_zalloc(sizeof (sata_drive_info_t),
7983 				    KM_SLEEP);
7984 				mutex_enter(&cportinfo->cport_mutex);
7985 			}
7986 			drive = SATA_CPORTINFO_DRV_INFO(cportinfo);
7987 			drive->satadrv_addr = cportinfo->cport_addr;
7988 			drive->satadrv_addr.qual = SATA_ADDR_DCPORT;
7989 			drive->satadrv_type = cportinfo->cport_dev_type;
7990 			drive->satadrv_state = SATA_STATE_UNKNOWN;
7991 
7992 			mutex_exit(&cportinfo->cport_mutex);
7993 			if (sata_add_device(dip, sata_hba_inst, ncport, 0) !=
7994 			    SATA_SUCCESS) {
7995 				/*
7996 				 * Plugged device was not correctly identified.
7997 				 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT
7998 				 */
7999 				cur_time = ddi_get_lbolt();
8000 				if ((cur_time - start_time) <
8001 				    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
8002 					/* sleep for a while */
8003 					delay(drv_usectohz(
8004 					    SATA_DEV_RETRY_DLY));
8005 					goto reprobe_cport;
8006 				}
8007 			}
8008 		} else {
8009 			mutex_exit(&cportinfo->cport_mutex);
8010 			ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT);
8011 			pminfo = kmem_zalloc(sizeof (sata_pmult_info_t),
8012 			    KM_SLEEP);
8013 			mutex_enter(&cportinfo->cport_mutex);
8014 			ASSERT(pminfo != NULL);
8015 			SATA_CPORTINFO_PMULT_INFO(cportinfo) = pminfo;
8016 			pminfo->pmult_addr.cport = cportinfo->cport_addr.cport;
8017 			pminfo->pmult_addr.pmport = SATA_PMULT_HOSTPORT;
8018 			pminfo->pmult_addr.qual = SATA_ADDR_PMPORT;
8019 			pminfo->pmult_num_dev_ports =
8020 			    sata_device.satadev_add_info;
8021 			mutex_init(&pminfo->pmult_mutex, NULL, MUTEX_DRIVER,
8022 			    NULL);
8023 			pminfo->pmult_state = SATA_STATE_PROBING;
8024 			mutex_exit(&cportinfo->cport_mutex);
8025 
8026 			/* Probe Port Multiplier ports */
8027 			for (npmport = 0;
8028 			    npmport < pminfo->pmult_num_dev_ports;
8029 			    npmport++) {
8030 				pmportinfo = kmem_zalloc(
8031 				    sizeof (sata_pmport_info_t), KM_SLEEP);
8032 				mutex_enter(&cportinfo->cport_mutex);
8033 				ASSERT(pmportinfo != NULL);
8034 				pmportinfo->pmport_addr.cport = ncport;
8035 				pmportinfo->pmport_addr.pmport = npmport;
8036 				pmportinfo->pmport_addr.qual =
8037 				    SATA_ADDR_PMPORT;
8038 				pminfo->pmult_dev_port[npmport] = pmportinfo;
8039 
8040 				mutex_init(&pmportinfo->pmport_mutex, NULL,
8041 				    MUTEX_DRIVER, NULL);
8042 
8043 				mutex_exit(&cportinfo->cport_mutex);
8044 
8045 				/* Create an attachment point */
8046 				minor_number = SATA_MAKE_AP_MINOR(
8047 				    ddi_get_instance(dip), ncport, npmport, 1);
8048 				(void) sprintf(name, "%d.%d", ncport, npmport);
8049 				if (ddi_create_minor_node(dip, name, S_IFCHR,
8050 				    minor_number, DDI_NT_SATA_ATTACHMENT_POINT,
8051 				    0) != DDI_SUCCESS) {
8052 					sata_log(sata_hba_inst, CE_WARN,
8053 					    "sata_hba_attach: "
8054 					    "cannot create SATA attachment "
8055 					    "point for port %d pmult port %d",
8056 					    ncport, npmport);
8057 				}
8058 
8059 				start_time = ddi_get_lbolt();
8060 			reprobe_pmport:
8061 				sata_device.satadev_addr.pmport = npmport;
8062 				sata_device.satadev_addr.qual =
8063 				    SATA_ADDR_PMPORT;
8064 
8065 				rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8066 				    (dip, &sata_device);
8067 				mutex_enter(&cportinfo->cport_mutex);
8068 
8069 				/* sata_update_port_info() */
8070 				sata_update_port_scr(&pmportinfo->pmport_scr,
8071 				    &sata_device);
8072 
8073 				if (rval != SATA_SUCCESS) {
8074 					pmportinfo->pmport_state =
8075 					    SATA_PSTATE_FAILED;
8076 					mutex_exit(&cportinfo->cport_mutex);
8077 					continue;
8078 				}
8079 				pmportinfo->pmport_state &=
8080 				    ~SATA_STATE_PROBING;
8081 				pmportinfo->pmport_state |= SATA_STATE_PROBED;
8082 				pmportinfo->pmport_dev_type =
8083 				    sata_device.satadev_type;
8084 
8085 				pmportinfo->pmport_state |= SATA_STATE_READY;
8086 				if (pmportinfo->pmport_dev_type ==
8087 				    SATA_DTYPE_NONE) {
8088 					mutex_exit(&cportinfo->cport_mutex);
8089 					continue;
8090 				}
8091 				/* Port multipliers cannot be chained */
8092 				ASSERT(pmportinfo->pmport_dev_type !=
8093 				    SATA_DTYPE_PMULT);
8094 				/*
8095 				 * There is something attached to Port
8096 				 * Multiplier device port
8097 				 * Allocate device info structure
8098 				 */
8099 				if (pmportinfo->pmport_sata_drive == NULL) {
8100 					mutex_exit(&cportinfo->cport_mutex);
8101 					pmportinfo->pmport_sata_drive =
8102 					    kmem_zalloc(
8103 					    sizeof (sata_drive_info_t),
8104 					    KM_SLEEP);
8105 					mutex_enter(&cportinfo->cport_mutex);
8106 				}
8107 				drive = pmportinfo->pmport_sata_drive;
8108 				drive->satadrv_addr.cport =
8109 				    pmportinfo->pmport_addr.cport;
8110 				drive->satadrv_addr.pmport = npmport;
8111 				drive->satadrv_addr.qual = SATA_ADDR_DPMPORT;
8112 				drive->satadrv_type = pmportinfo->
8113 				    pmport_dev_type;
8114 				drive->satadrv_state = SATA_STATE_UNKNOWN;
8115 
8116 				mutex_exit(&cportinfo->cport_mutex);
8117 				if (sata_add_device(dip, sata_hba_inst, ncport,
8118 				    npmport) != SATA_SUCCESS) {
8119 					/*
8120 					 * Plugged device was not correctly
8121 					 * identified. Retry, within the
8122 					 * SATA_DEV_IDENTIFY_TIMEOUT
8123 					 */
8124 					cur_time = ddi_get_lbolt();
8125 					if ((cur_time - start_time) <
8126 					    drv_usectohz(
8127 					    SATA_DEV_IDENTIFY_TIMEOUT)) {
8128 						/* sleep for a while */
8129 						delay(drv_usectohz(
8130 						    SATA_DEV_RETRY_DLY));
8131 						goto reprobe_pmport;
8132 					}
8133 				}
8134 			}
8135 			pmportinfo->pmport_state =
8136 			    SATA_STATE_PROBED | SATA_STATE_READY;
8137 		}
8138 	}
8139 }
8140 
8141 /*
8142  * Add SATA device for specified HBA instance & port (SCSI target
8143  * device nodes).
8144  * This function is called (indirectly) only from sata_hba_attach().
8145  * A target node is created when there is a supported type device attached,
8146  * but may be removed if it cannot be put online.
8147  *
8148  * This function cannot be called from an interrupt context.
8149  *
8150  * ONLY DISK TARGET NODES ARE CREATED NOW
8151  *
8152  * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when
8153  * device identification failed - adding a device could be retried.
8154  *
8155  */
8156 static 	int
8157 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst, int cport,
8158     int pmport)
8159 {
8160 	sata_cport_info_t 	*cportinfo;
8161 	sata_pmult_info_t	*pminfo;
8162 	sata_pmport_info_t	*pmportinfo;
8163 	dev_info_t		*cdip;		/* child dip */
8164 	sata_device_t		sata_device;
8165 	int			rval;
8166 
8167 
8168 
8169 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
8170 	ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE);
8171 	mutex_enter(&cportinfo->cport_mutex);
8172 	/*
8173 	 * Some device is attached to a controller port.
8174 	 * We rely on controllers distinquishing between no-device,
8175 	 * attached port multiplier and other kind of attached device.
8176 	 * We need to get Identify Device data and determine
8177 	 * positively the dev type before trying to attach
8178 	 * the target driver.
8179 	 */
8180 	sata_device.satadev_rev = SATA_DEVICE_REV;
8181 	if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
8182 		/*
8183 		 * Not port multiplier.
8184 		 */
8185 		sata_device.satadev_addr = cportinfo->cport_addr;
8186 		sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
8187 		mutex_exit(&cportinfo->cport_mutex);
8188 
8189 		rval = sata_probe_device(sata_hba_inst, &sata_device);
8190 		if (rval != SATA_SUCCESS ||
8191 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN)
8192 			return (SATA_FAILURE);
8193 
8194 		mutex_enter(&cportinfo->cport_mutex);
8195 		sata_show_drive_info(sata_hba_inst,
8196 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
8197 
8198 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
8199 			/*
8200 			 * Could not determine device type or
8201 			 * a device is not supported.
8202 			 * Degrade this device to unknown.
8203 			 */
8204 			cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8205 			mutex_exit(&cportinfo->cport_mutex);
8206 			return (SATA_SUCCESS);
8207 		}
8208 		cportinfo->cport_dev_type = sata_device.satadev_type;
8209 		cportinfo->cport_tgtnode_clean = B_TRUE;
8210 		mutex_exit(&cportinfo->cport_mutex);
8211 
8212 		/*
8213 		 * Initialize device to the desired state. Even if it
8214 		 * fails, the device will still attach but syslog
8215 		 * will show the warning.
8216 		 */
8217 		if (sata_initialize_device(sata_hba_inst,
8218 		    SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS)
8219 			/* Retry */
8220 			(void) sata_initialize_device(sata_hba_inst,
8221 			    SATA_CPORTINFO_DRV_INFO(cportinfo));
8222 
8223 		cdip = sata_create_target_node(pdip, sata_hba_inst,
8224 		    &sata_device.satadev_addr);
8225 		mutex_enter(&cportinfo->cport_mutex);
8226 		if (cdip == NULL) {
8227 			/*
8228 			 * Attaching target node failed.
8229 			 * We retain sata_drive_info structure...
8230 			 */
8231 			mutex_exit(&cportinfo->cport_mutex);
8232 			return (SATA_SUCCESS);
8233 		}
8234 		(SATA_CPORTINFO_DRV_INFO(cportinfo))->
8235 		    satadrv_state = SATA_STATE_READY;
8236 	} else {
8237 		/* This must be Port Multiplier type */
8238 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
8239 			SATA_LOG_D((sata_hba_inst, CE_WARN,
8240 			    "sata_add_device: "
8241 			    "unrecognized dev type %x",
8242 			    cportinfo->cport_dev_type));
8243 			mutex_exit(&cportinfo->cport_mutex);
8244 			return (SATA_SUCCESS);
8245 		}
8246 		pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
8247 		pmportinfo = pminfo->pmult_dev_port[pmport];
8248 		sata_device.satadev_addr = pmportinfo->pmport_addr;
8249 		sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
8250 		mutex_exit(&cportinfo->cport_mutex);
8251 
8252 		rval = sata_probe_device(sata_hba_inst, &sata_device);
8253 		if (rval != SATA_SUCCESS ||
8254 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN) {
8255 			return (SATA_FAILURE);
8256 		}
8257 		mutex_enter(&cportinfo->cport_mutex);
8258 		sata_show_drive_info(sata_hba_inst,
8259 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
8260 
8261 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
8262 			/*
8263 			 * Could not determine device type.
8264 			 * Degrade this device to unknown.
8265 			 */
8266 			pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
8267 			mutex_exit(&cportinfo->cport_mutex);
8268 			return (SATA_SUCCESS);
8269 		}
8270 		pmportinfo->pmport_dev_type = sata_device.satadev_type;
8271 		pmportinfo->pmport_tgtnode_clean = B_TRUE;
8272 		mutex_exit(&cportinfo->cport_mutex);
8273 
8274 		/*
8275 		 * Initialize device to the desired state.
8276 		 * Even if it fails, the device will still
8277 		 * attach but syslog will show the warning.
8278 		 */
8279 		if (sata_initialize_device(sata_hba_inst,
8280 		    pmportinfo->pmport_sata_drive) != SATA_SUCCESS)
8281 			/* Retry */
8282 			(void) sata_initialize_device(sata_hba_inst,
8283 			    pmportinfo->pmport_sata_drive);
8284 
8285 		cdip = sata_create_target_node(pdip, sata_hba_inst,
8286 		    &sata_device.satadev_addr);
8287 		mutex_enter(&cportinfo->cport_mutex);
8288 		if (cdip == NULL) {
8289 			/*
8290 			 * Attaching target node failed.
8291 			 * We retain sata_drive_info structure...
8292 			 */
8293 			mutex_exit(&cportinfo->cport_mutex);
8294 			return (SATA_SUCCESS);
8295 		}
8296 		pmportinfo->pmport_sata_drive->satadrv_state |=
8297 		    SATA_STATE_READY;
8298 	}
8299 	mutex_exit(&cportinfo->cport_mutex);
8300 	return (SATA_SUCCESS);
8301 }
8302 
8303 
8304 
8305 /*
8306  * Create scsi target node for attached device, create node properties and
8307  * attach the node.
8308  * The node could be removed if the device onlining fails.
8309  *
8310  * A dev_info_t pointer is returned if operation is successful, NULL is
8311  * returned otherwise.
8312  *
8313  * No port multiplier support.
8314  */
8315 
8316 static dev_info_t *
8317 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst,
8318 			sata_address_t *sata_addr)
8319 {
8320 	dev_info_t *cdip = NULL;
8321 	int rval;
8322 	char *nname = NULL;
8323 	char **compatible = NULL;
8324 	int ncompatible;
8325 	struct scsi_inquiry inq;
8326 	sata_device_t sata_device;
8327 	sata_drive_info_t *sdinfo;
8328 	int target;
8329 	int i;
8330 
8331 	sata_device.satadev_rev = SATA_DEVICE_REV;
8332 	sata_device.satadev_addr = *sata_addr;
8333 
8334 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport)));
8335 
8336 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
8337 
8338 	target = SATA_TO_SCSI_TARGET(sata_addr->cport,
8339 	    sata_addr->pmport, sata_addr->qual);
8340 
8341 	if (sdinfo == NULL) {
8342 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8343 		    sata_addr->cport)));
8344 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8345 		    "sata_create_target_node: no sdinfo for target %x",
8346 		    target));
8347 		return (NULL);
8348 	}
8349 
8350 	/*
8351 	 * create or get scsi inquiry data, expected by
8352 	 * scsi_hba_nodename_compatible_get()
8353 	 * SATA hard disks get Identify Data translated into Inguiry Data.
8354 	 * ATAPI devices respond directly to Inquiry request.
8355 	 */
8356 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
8357 		sata_identdev_to_inquiry(sata_hba_inst, sdinfo,
8358 		    (uint8_t *)&inq);
8359 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8360 		    sata_addr->cport)));
8361 	} else { /* Assume supported ATAPI device */
8362 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8363 		    sata_addr->cport)));
8364 		if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr,
8365 		    &inq) == SATA_FAILURE)
8366 			return (NULL);
8367 		/*
8368 		 * Save supported ATAPI transport version
8369 		 */
8370 		sdinfo->satadrv_atapi_trans_ver =
8371 		    SATA_ATAPI_TRANS_VERSION(&inq);
8372 	}
8373 
8374 	/* determine the node name and compatible */
8375 	scsi_hba_nodename_compatible_get(&inq, NULL,
8376 	    inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
8377 
8378 #ifdef SATA_DEBUG
8379 	if (sata_debug_flags & SATA_DBG_NODES) {
8380 		if (nname == NULL) {
8381 			cmn_err(CE_NOTE, "sata_create_target_node: "
8382 			    "cannot determine nodename for target %d\n",
8383 			    target);
8384 		} else {
8385 			cmn_err(CE_WARN, "sata_create_target_node: "
8386 			    "target %d nodename: %s\n", target, nname);
8387 		}
8388 		if (compatible == NULL) {
8389 			cmn_err(CE_WARN,
8390 			    "sata_create_target_node: no compatible name\n");
8391 		} else {
8392 			for (i = 0; i < ncompatible; i++) {
8393 				cmn_err(CE_WARN, "sata_create_target_node: "
8394 				    "compatible name: %s\n", compatible[i]);
8395 			}
8396 		}
8397 	}
8398 #endif
8399 
8400 	/* if nodename can't be determined, log error and exit */
8401 	if (nname == NULL) {
8402 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8403 		    "sata_create_target_node: cannot determine nodename "
8404 		    "for target %d\n", target));
8405 		scsi_hba_nodename_compatible_free(nname, compatible);
8406 		return (NULL);
8407 	}
8408 	/*
8409 	 * Create scsi target node
8410 	 */
8411 	ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip);
8412 	rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
8413 	    "device-type", "scsi");
8414 
8415 	if (rval != DDI_PROP_SUCCESS) {
8416 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8417 		    "updating device_type prop failed %d", rval));
8418 		goto fail;
8419 	}
8420 
8421 	/*
8422 	 * Create target node properties: target & lun
8423 	 */
8424 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target);
8425 	if (rval != DDI_PROP_SUCCESS) {
8426 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8427 		    "updating target prop failed %d", rval));
8428 		goto fail;
8429 	}
8430 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0);
8431 	if (rval != DDI_PROP_SUCCESS) {
8432 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8433 		    "updating target prop failed %d", rval));
8434 		goto fail;
8435 	}
8436 
8437 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
8438 		/*
8439 		 * Add "variant" property
8440 		 */
8441 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
8442 		    "variant", "atapi");
8443 		if (rval != DDI_PROP_SUCCESS) {
8444 			SATA_LOG_D((sata_hba_inst, CE_WARN,
8445 			    "sata_create_target_node: variant atapi "
8446 			    "property could not be created: %d", rval));
8447 			goto fail;
8448 		}
8449 	}
8450 	/* decorate the node with compatible */
8451 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible",
8452 	    compatible, ncompatible) != DDI_PROP_SUCCESS) {
8453 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8454 		    "sata_create_target_node: FAIL compatible props cdip 0x%p",
8455 		    (void *)cdip));
8456 		goto fail;
8457 	}
8458 
8459 
8460 	/*
8461 	 * Now, try to attach the driver. If probing of the device fails,
8462 	 * the target node may be removed
8463 	 */
8464 	rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH);
8465 
8466 	scsi_hba_nodename_compatible_free(nname, compatible);
8467 
8468 	if (rval == NDI_SUCCESS)
8469 		return (cdip);
8470 
8471 	/* target node was removed - are we sure? */
8472 	return (NULL);
8473 
8474 fail:
8475 	scsi_hba_nodename_compatible_free(nname, compatible);
8476 	ddi_prop_remove_all(cdip);
8477 	rval = ndi_devi_free(cdip);
8478 	if (rval != NDI_SUCCESS) {
8479 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8480 		    "node removal failed %d", rval));
8481 	}
8482 	sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: "
8483 	    "cannot create target node for SATA device at port %d",
8484 	    sata_addr->cport);
8485 	return (NULL);
8486 }
8487 
8488 
8489 
8490 /*
8491  * Re-probe sata port, check for a device and attach info
8492  * structures when necessary. Identify Device data is fetched, if possible.
8493  * Assumption: sata address is already validated.
8494  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
8495  * the presence of a device and its type.
8496  *
8497  * flag arg specifies that the function should try multiple times to identify
8498  * device type and to initialize it, or it should return immediately on failure.
8499  * SATA_DEV_IDENTIFY_RETRY - retry
8500  * SATA_DEV_IDENTIFY_NORETRY - no retry
8501  *
8502  * SATA_FAILURE is returned if one of the operations failed.
8503  *
8504  * This function cannot be called in interrupt context - it may sleep.
8505  *
8506  * NOte: Port multiplier is not supported yet, although there may be some
8507  * pieces of code referencing to it.
8508  */
8509 static int
8510 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
8511     int flag)
8512 {
8513 	sata_cport_info_t *cportinfo;
8514 	sata_drive_info_t *sdinfo, *osdinfo;
8515 	boolean_t init_device = B_FALSE;
8516 	int prev_device_type = SATA_DTYPE_NONE;
8517 	int prev_device_settings = 0;
8518 	int prev_device_state = 0;
8519 	clock_t start_time;
8520 	int retry = B_FALSE;
8521 	int rval;
8522 
8523 	/* We only care about host sata cport for now */
8524 	cportinfo = SATA_CPORT_INFO(sata_hba_inst,
8525 	    sata_device->satadev_addr.cport);
8526 	osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8527 	if (osdinfo != NULL) {
8528 		/*
8529 		 * We are re-probing port with a previously attached device.
8530 		 * Save previous device type and settings.
8531 		 */
8532 		prev_device_type = cportinfo->cport_dev_type;
8533 		prev_device_settings = osdinfo->satadrv_settings;
8534 		prev_device_state = osdinfo->satadrv_state;
8535 	}
8536 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
8537 		start_time = ddi_get_lbolt();
8538 		retry = B_TRUE;
8539 	}
8540 retry_probe:
8541 
8542 	/* probe port */
8543 	mutex_enter(&cportinfo->cport_mutex);
8544 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
8545 	cportinfo->cport_state |= SATA_STATE_PROBING;
8546 	mutex_exit(&cportinfo->cport_mutex);
8547 
8548 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8549 	    (SATA_DIP(sata_hba_inst), sata_device);
8550 
8551 	mutex_enter(&cportinfo->cport_mutex);
8552 	if (rval != SATA_SUCCESS) {
8553 		cportinfo->cport_state = SATA_PSTATE_FAILED;
8554 		mutex_exit(&cportinfo->cport_mutex);
8555 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: "
8556 		    "SATA port %d probing failed",
8557 		    cportinfo->cport_addr.cport));
8558 		return (SATA_FAILURE);
8559 	}
8560 
8561 	/*
8562 	 * update sata port state and set device type
8563 	 */
8564 	sata_update_port_info(sata_hba_inst, sata_device);
8565 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
8566 
8567 	/*
8568 	 * Sanity check - Port is active? Is the link active?
8569 	 * Is there any device attached?
8570 	 */
8571 	if ((cportinfo->cport_state &
8572 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
8573 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
8574 	    SATA_PORT_DEVLINK_UP) {
8575 		/*
8576 		 * Port in non-usable state or no link active/no device.
8577 		 * Free info structure if necessary (direct attached drive
8578 		 * only, for now!
8579 		 */
8580 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8581 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8582 		/* Add here differentiation for device attached or not */
8583 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8584 		mutex_exit(&cportinfo->cport_mutex);
8585 		if (sdinfo != NULL)
8586 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8587 		return (SATA_SUCCESS);
8588 	}
8589 
8590 	cportinfo->cport_state |= SATA_STATE_READY;
8591 	cportinfo->cport_dev_type = sata_device->satadev_type;
8592 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8593 
8594 	/*
8595 	 * If we are re-probing the port, there may be
8596 	 * sata_drive_info structure attached
8597 	 * (or sata_pm_info, if PMult is supported).
8598 	 */
8599 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
8600 		/*
8601 		 * There is no device, so remove device info structure,
8602 		 * if necessary.
8603 		 * Only direct attached drive is considered now, until
8604 		 * port multiplier is supported. If the previously
8605 		 * attached device was a port multiplier, we would need
8606 		 * to take care of devices attached beyond the port
8607 		 * multiplier.
8608 		 */
8609 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8610 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8611 		if (sdinfo != NULL) {
8612 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8613 			sata_log(sata_hba_inst, CE_WARN,
8614 			    "SATA device detached "
8615 			    "from port %d", cportinfo->cport_addr.cport);
8616 		}
8617 		mutex_exit(&cportinfo->cport_mutex);
8618 		return (SATA_SUCCESS);
8619 	}
8620 
8621 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
8622 		if (sdinfo == NULL) {
8623 			/*
8624 			 * There is some device attached, but there is
8625 			 * no sata_drive_info structure - allocate one
8626 			 */
8627 			mutex_exit(&cportinfo->cport_mutex);
8628 			sdinfo = kmem_zalloc(
8629 			    sizeof (sata_drive_info_t), KM_SLEEP);
8630 			mutex_enter(&cportinfo->cport_mutex);
8631 			/*
8632 			 * Recheck, that the port state did not change when we
8633 			 * released mutex.
8634 			 */
8635 			if (cportinfo->cport_state & SATA_STATE_READY) {
8636 				SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo;
8637 				sdinfo->satadrv_addr = cportinfo->cport_addr;
8638 				sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT;
8639 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
8640 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
8641 			} else {
8642 				/*
8643 				 * Port is not in ready state, we
8644 				 * cannot attach a device.
8645 				 */
8646 				mutex_exit(&cportinfo->cport_mutex);
8647 				kmem_free(sdinfo, sizeof (sata_drive_info_t));
8648 				return (SATA_SUCCESS);
8649 			}
8650 			/*
8651 			 * Since we are adding device, presumably new one,
8652 			 * indicate that it  should be initalized,
8653 			 * as well as some internal framework states).
8654 			 */
8655 			init_device = B_TRUE;
8656 		}
8657 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8658 		sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
8659 	} else {
8660 		/*
8661 		 * The device is a port multiplier - not handled now.
8662 		 */
8663 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8664 		mutex_exit(&cportinfo->cport_mutex);
8665 		return (SATA_SUCCESS);
8666 	}
8667 	mutex_exit(&cportinfo->cport_mutex);
8668 	/*
8669 	 * Figure out what kind of device we are really
8670 	 * dealing with.
8671 	 */
8672 	rval = sata_probe_device(sata_hba_inst, sata_device);
8673 
8674 	mutex_enter(&cportinfo->cport_mutex);
8675 	if (rval == SATA_SUCCESS) {
8676 		/*
8677 		 * If we are dealing with the same type of a device as before,
8678 		 * restore its settings flags.
8679 		 */
8680 		if (osdinfo != NULL &&
8681 		    sata_device->satadev_type == prev_device_type)
8682 			sdinfo->satadrv_settings = prev_device_settings;
8683 
8684 		mutex_exit(&cportinfo->cport_mutex);
8685 		/* Set initial device features, if necessary */
8686 		if (init_device == B_TRUE) {
8687 			rval = sata_initialize_device(sata_hba_inst, sdinfo);
8688 		}
8689 		if (rval == SATA_SUCCESS)
8690 			return (rval);
8691 	} else {
8692 		/*
8693 		 * If there was some device info before we probe the device,
8694 		 * restore previous device setting, so we can retry from scratch
8695 		 * later. Providing, of course, that device has not disapear
8696 		 * during probing process.
8697 		 */
8698 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
8699 			if (osdinfo != NULL) {
8700 				cportinfo->cport_dev_type = prev_device_type;
8701 				sdinfo->satadrv_type = prev_device_type;
8702 				sdinfo->satadrv_state = prev_device_state;
8703 			}
8704 		} else {
8705 			/* device is gone */
8706 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8707 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8708 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8709 			mutex_exit(&cportinfo->cport_mutex);
8710 			return (SATA_SUCCESS);
8711 		}
8712 		mutex_exit(&cportinfo->cport_mutex);
8713 	}
8714 
8715 	if (retry) {
8716 		clock_t cur_time = ddi_get_lbolt();
8717 		/*
8718 		 * A device was not successfully identified or initialized.
8719 		 * Track retry time for device identification.
8720 		 */
8721 		if ((cur_time - start_time) <
8722 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
8723 			/* sleep for a while */
8724 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
8725 			goto retry_probe;
8726 		} else {
8727 			mutex_enter(&cportinfo->cport_mutex);
8728 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL)
8729 				SATA_CPORTINFO_DRV_INFO(cportinfo)->
8730 				    satadrv_state = SATA_DSTATE_FAILED;
8731 			mutex_exit(&cportinfo->cport_mutex);
8732 		}
8733 	}
8734 	return (SATA_SUCCESS);
8735 }
8736 
8737 /*
8738  * Initialize device
8739  * Specified device is initialized to a default state.
8740  *
8741  * Returns SATA_SUCCESS if all device features are set successfully,
8742  * SATA_FAILURE otherwise
8743  */
8744 static int
8745 sata_initialize_device(sata_hba_inst_t *sata_hba_inst,
8746     sata_drive_info_t *sdinfo)
8747 {
8748 	int rval;
8749 
8750 	sata_save_drive_settings(sdinfo);
8751 
8752 	sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
8753 
8754 	sata_init_write_cache_mode(sdinfo);
8755 
8756 	rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0);
8757 
8758 	/* Determine current data transfer mode */
8759 	if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) {
8760 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
8761 	} else if ((sdinfo->satadrv_id.ai_validinfo &
8762 	    SATA_VALIDINFO_88) != 0 &&
8763 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) {
8764 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
8765 	} else if ((sdinfo->satadrv_id.ai_dworddma &
8766 	    SATA_MDMA_SEL_MASK) != 0) {
8767 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
8768 	} else
8769 		/* DMA supported, not no DMA transfer mode is selected !? */
8770 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
8771 
8772 	return (rval);
8773 }
8774 
8775 
8776 /*
8777  * Initialize write cache mode.
8778  *
8779  * The default write cache setting for SATA HDD is provided by sata_write_cache
8780  * static variable. ATAPI CD/DVDs devices have write cache default is
8781  * determined by sata_atapicdvd_write_cache static variable.
8782  * 1 - enable
8783  * 0 - disable
8784  * any other value - current drive setting
8785  *
8786  * Although there is not reason to disable write cache on CD/DVD devices,
8787  * the default setting control is provided for the maximun flexibility.
8788  *
8789  * In the future, it may be overridden by the
8790  * disk-write-cache-enable property setting, if it is defined.
8791  * Returns SATA_SUCCESS if all device features are set successfully,
8792  * SATA_FAILURE otherwise.
8793  */
8794 static void
8795 sata_init_write_cache_mode(sata_drive_info_t *sdinfo)
8796 {
8797 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
8798 		if (sata_write_cache == 1)
8799 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
8800 		else if (sata_write_cache == 0)
8801 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
8802 		/*
8803 		 * When sata_write_cache value is not 0 or 1,
8804 		 * a current setting of the drive's write cache is used.
8805 		 */
8806 	} else { /* Assume ATAPI CD/DVD device */
8807 		if (sata_atapicdvd_write_cache == 1)
8808 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
8809 		else if (sata_atapicdvd_write_cache == 0)
8810 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
8811 		/*
8812 		 * When sata_write_cache value is not 0 or 1,
8813 		 * a current setting of the drive's write cache is used.
8814 		 */
8815 	}
8816 }
8817 
8818 
8819 /*
8820  * Validate sata address.
8821  * Specified cport, pmport and qualifier has to match
8822  * passed sata_scsi configuration info.
8823  * The presence of an attached device is not verified.
8824  *
8825  * Returns 0 when address is valid, -1 otherwise.
8826  */
8827 static int
8828 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport,
8829 	int pmport, int qual)
8830 {
8831 	if (qual == SATA_ADDR_DCPORT && pmport != 0)
8832 		goto invalid_address;
8833 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
8834 		goto invalid_address;
8835 	if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) &&
8836 	    ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) ||
8837 	    (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) ||
8838 	    (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport))))
8839 		goto invalid_address;
8840 
8841 	return (0);
8842 
8843 invalid_address:
8844 	return (-1);
8845 
8846 }
8847 
8848 /*
8849  * Validate scsi address
8850  * SCSI target address is translated into SATA cport/pmport and compared
8851  * with a controller port/device configuration. LUN has to be 0.
8852  * Returns 0 if a scsi target refers to an attached device,
8853  * returns 1 if address is valid but device is not attached,
8854  * returns -1 if bad address or device is of an unsupported type.
8855  * Upon return sata_device argument is set.
8856  */
8857 static int
8858 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst,
8859 	struct scsi_address *ap, sata_device_t *sata_device)
8860 {
8861 	int cport, pmport, qual, rval;
8862 
8863 	rval = -1;	/* Invalid address */
8864 	if (ap->a_lun != 0)
8865 		goto out;
8866 
8867 	qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
8868 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
8869 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
8870 
8871 	if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT)
8872 		goto out;
8873 
8874 	if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) ==
8875 	    0) {
8876 
8877 		sata_cport_info_t *cportinfo;
8878 		sata_pmult_info_t *pmultinfo;
8879 		sata_drive_info_t *sdinfo = NULL;
8880 
8881 		rval = 1;	/* Valid sata address */
8882 
8883 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
8884 		if (qual == SATA_ADDR_DCPORT) {
8885 			if (cportinfo == NULL ||
8886 			    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
8887 				goto out;
8888 
8889 			if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT ||
8890 			    (cportinfo->cport_dev_type &
8891 			    SATA_VALID_DEV_TYPE) == 0) {
8892 				rval = -1;
8893 				goto out;
8894 			}
8895 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8896 
8897 		} else if (qual == SATA_ADDR_DPMPORT) {
8898 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
8899 			if (pmultinfo == NULL) {
8900 				rval = -1;
8901 				goto out;
8902 			}
8903 			if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) ==
8904 			    NULL ||
8905 			    SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
8906 			    pmport) == SATA_DTYPE_NONE)
8907 				goto out;
8908 
8909 			sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport,
8910 			    pmport);
8911 		} else {
8912 			rval = -1;
8913 			goto out;
8914 		}
8915 		if ((sdinfo == NULL) ||
8916 		    (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0)
8917 			goto out;
8918 
8919 		sata_device->satadev_type = sdinfo->satadrv_type;
8920 		sata_device->satadev_addr.qual = qual;
8921 		sata_device->satadev_addr.cport = cport;
8922 		sata_device->satadev_addr.pmport = pmport;
8923 		sata_device->satadev_rev = SATA_DEVICE_REV_1;
8924 		return (0);
8925 	}
8926 out:
8927 	if (rval == 1) {
8928 		SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
8929 		    "sata_validate_scsi_address: no valid target %x lun %x",
8930 		    ap->a_target, ap->a_lun);
8931 	}
8932 	return (rval);
8933 }
8934 
8935 /*
8936  * Find dip corresponding to passed device number
8937  *
8938  * Returns NULL if invalid device number is passed or device cannot be found,
8939  * Returns dip is device is found.
8940  */
8941 static dev_info_t *
8942 sata_devt_to_devinfo(dev_t dev)
8943 {
8944 	dev_info_t *dip;
8945 #ifndef __lock_lint
8946 	struct devnames *dnp;
8947 	major_t major = getmajor(dev);
8948 	int instance = SATA_MINOR2INSTANCE(getminor(dev));
8949 
8950 	if (major >= devcnt)
8951 		return (NULL);
8952 
8953 	dnp = &devnamesp[major];
8954 	LOCK_DEV_OPS(&(dnp->dn_lock));
8955 	dip = dnp->dn_head;
8956 	while (dip && (ddi_get_instance(dip) != instance)) {
8957 		dip = ddi_get_next(dip);
8958 	}
8959 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
8960 #endif
8961 
8962 	return (dip);
8963 }
8964 
8965 
8966 /*
8967  * Probe device.
8968  * This function issues Identify Device command and initializes local
8969  * sata_drive_info structure if the device can be identified.
8970  * The device type is determined by examining Identify Device
8971  * command response.
8972  * If the sata_hba_inst has linked drive info structure for this
8973  * device address, the Identify Device data is stored into sata_drive_info
8974  * structure linked to the port info structure.
8975  *
8976  * sata_device has to refer to the valid sata port(s) for HBA described
8977  * by sata_hba_inst structure.
8978  *
8979  * Returns:
8980  *	SATA_SUCCESS if device type was successfully probed and port-linked
8981  *		drive info structure was updated;
8982  * 	SATA_FAILURE if there is no device, or device was not probed
8983  *		successully;
8984  *	SATA_RETRY if device probe can be retried later.
8985  * If a device cannot be identified, sata_device's dev_state and dev_type
8986  * fields are set to unknown.
8987  * There are no retries in this function. Any retries should be managed by
8988  * the caller.
8989  */
8990 
8991 
8992 static int
8993 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
8994 {
8995 	sata_drive_info_t *sdinfo;
8996 	sata_drive_info_t new_sdinfo;	/* local drive info struct */
8997 	int rval;
8998 
8999 	ASSERT((SATA_CPORT_STATE(sata_hba_inst,
9000 	    sata_device->satadev_addr.cport) &
9001 	    (SATA_STATE_PROBED | SATA_STATE_READY)) != 0);
9002 
9003 	sata_device->satadev_type = SATA_DTYPE_NONE;
9004 
9005 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9006 	    sata_device->satadev_addr.cport)));
9007 
9008 	/* Get pointer to port-linked sata device info structure */
9009 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9010 	if (sdinfo != NULL) {
9011 		sdinfo->satadrv_state &=
9012 		    ~(SATA_STATE_PROBED | SATA_STATE_READY);
9013 		sdinfo->satadrv_state |= SATA_STATE_PROBING;
9014 	} else {
9015 		/* No device to probe */
9016 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9017 		    sata_device->satadev_addr.cport)));
9018 		sata_device->satadev_type = SATA_DTYPE_NONE;
9019 		sata_device->satadev_state = SATA_STATE_UNKNOWN;
9020 		return (SATA_FAILURE);
9021 	}
9022 	/*
9023 	 * Need to issue both types of identify device command and
9024 	 * determine device type by examining retreived data/status.
9025 	 * First, ATA Identify Device.
9026 	 */
9027 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
9028 	new_sdinfo.satadrv_addr = sata_device->satadev_addr;
9029 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9030 	    sata_device->satadev_addr.cport)));
9031 	new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
9032 	rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
9033 	if (rval == SATA_RETRY) {
9034 		/* We may try to check for ATAPI device */
9035 		if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) {
9036 			/*
9037 			 * HBA supports ATAPI - try to issue Identify Packet
9038 			 * Device command.
9039 			 */
9040 			new_sdinfo.satadrv_type = SATA_DTYPE_ATAPICD;
9041 			rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
9042 		}
9043 	}
9044 	if (rval == SATA_SUCCESS) {
9045 		/*
9046 		 * Got something responding positively to ATA Identify Device
9047 		 * or to Identify Packet Device cmd.
9048 		 * Save last used device type.
9049 		 */
9050 		sata_device->satadev_type = new_sdinfo.satadrv_type;
9051 
9052 		/* save device info, if possible */
9053 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9054 		    sata_device->satadev_addr.cport)));
9055 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9056 		if (sdinfo == NULL) {
9057 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9058 			    sata_device->satadev_addr.cport)));
9059 			return (SATA_FAILURE);
9060 		}
9061 		/*
9062 		 * Copy drive info into the port-linked drive info structure.
9063 		 */
9064 		*sdinfo = new_sdinfo;
9065 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
9066 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
9067 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
9068 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
9069 			    sata_device->satadev_addr.cport) =
9070 			    sdinfo->satadrv_type;
9071 		else /* SATA_ADDR_DPMPORT */
9072 			SATA_PMPORT_DEV_TYPE(sata_hba_inst,
9073 			    sata_device->satadev_addr.cport,
9074 			    sata_device->satadev_addr.pmport) =
9075 			    sdinfo->satadrv_type;
9076 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9077 		    sata_device->satadev_addr.cport)));
9078 		return (SATA_SUCCESS);
9079 	}
9080 
9081 	/*
9082 	 * It may be SATA_RETRY or SATA_FAILURE return.
9083 	 * Looks like we cannot determine the device type at this time.
9084 	 */
9085 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9086 	    sata_device->satadev_addr.cport)));
9087 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9088 	if (sdinfo != NULL) {
9089 		sata_device->satadev_type = SATA_DTYPE_UNKNOWN;
9090 		sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9091 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
9092 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
9093 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
9094 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
9095 			    sata_device->satadev_addr.cport) =
9096 			    SATA_DTYPE_UNKNOWN;
9097 		else {
9098 			/* SATA_ADDR_DPMPORT */
9099 			if ((SATA_PMULT_INFO(sata_hba_inst,
9100 			    sata_device->satadev_addr.cport) != NULL) &&
9101 			    (SATA_PMPORT_INFO(sata_hba_inst,
9102 			    sata_device->satadev_addr.cport,
9103 			    sata_device->satadev_addr.pmport) != NULL))
9104 				SATA_PMPORT_DEV_TYPE(sata_hba_inst,
9105 				    sata_device->satadev_addr.cport,
9106 				    sata_device->satadev_addr.pmport) =
9107 				    SATA_DTYPE_UNKNOWN;
9108 		}
9109 	}
9110 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9111 	    sata_device->satadev_addr.cport)));
9112 	return (rval);
9113 }
9114 
9115 
9116 /*
9117  * Get pointer to sata_drive_info structure.
9118  *
9119  * The sata_device has to contain address (cport, pmport and qualifier) for
9120  * specified sata_scsi structure.
9121  *
9122  * Returns NULL if device address is not valid for this HBA configuration.
9123  * Otherwise, returns a pointer to sata_drive_info structure.
9124  *
9125  * This function should be called with a port mutex held.
9126  */
9127 static sata_drive_info_t *
9128 sata_get_device_info(sata_hba_inst_t *sata_hba_inst,
9129     sata_device_t *sata_device)
9130 {
9131 	uint8_t cport = sata_device->satadev_addr.cport;
9132 	uint8_t pmport = sata_device->satadev_addr.pmport;
9133 	uint8_t qual = sata_device->satadev_addr.qual;
9134 
9135 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
9136 		return (NULL);
9137 
9138 	if (!(SATA_CPORT_STATE(sata_hba_inst, cport) &
9139 	    (SATA_STATE_PROBED | SATA_STATE_READY)))
9140 		/* Port not probed yet */
9141 		return (NULL);
9142 
9143 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE)
9144 		return (NULL);
9145 
9146 	if (qual == SATA_ADDR_DCPORT) {
9147 		/* Request for a device on a controller port */
9148 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
9149 		    SATA_DTYPE_PMULT)
9150 			/* Port multiplier attached */
9151 			return (NULL);
9152 		return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport));
9153 	}
9154 	if (qual == SATA_ADDR_DPMPORT) {
9155 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
9156 		    SATA_DTYPE_PMULT)
9157 			return (NULL);
9158 
9159 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport))
9160 			return (NULL);
9161 
9162 		return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport));
9163 	}
9164 
9165 	/* we should not get here */
9166 	return (NULL);
9167 }
9168 
9169 
9170 /*
9171  * sata_identify_device.
9172  * Send Identify Device command to SATA HBA driver.
9173  * If command executes successfully, update sata_drive_info structure pointed
9174  * to by sdinfo argument, including Identify Device data.
9175  * If command fails, invalidate data in sata_drive_info.
9176  *
9177  * Cannot be called from interrupt level.
9178  *
9179  * Returns:
9180  * SATA_SUCCESS if the device was identified as a supported device,
9181  * SATA_RETRY if the device was not identified but could be retried,
9182  * SATA_FAILURE if the device was not identified and identify attempt
9183  *	should not be retried.
9184  */
9185 static int
9186 sata_identify_device(sata_hba_inst_t *sata_hba_inst,
9187     sata_drive_info_t *sdinfo)
9188 {
9189 	uint16_t cfg_word;
9190 	int rval;
9191 
9192 	/* fetch device identify data */
9193 	if ((rval = sata_fetch_device_identify_data(sata_hba_inst,
9194 	    sdinfo)) != 0)
9195 		goto fail_unknown;
9196 
9197 	cfg_word = sdinfo->satadrv_id.ai_config;
9198 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK &&
9199 	    (cfg_word & SATA_ATA_TYPE_MASK) != SATA_ATA_TYPE) {
9200 		/* Change device type to reflect Identify Device data */
9201 		if (((cfg_word & SATA_ATAPI_TYPE_MASK) ==
9202 		    SATA_ATAPI_TYPE) &&
9203 		    ((cfg_word & SATA_ATAPI_ID_DEV_TYPE) ==
9204 		    SATA_ATAPI_CDROM_DEV)) {
9205 			sdinfo->satadrv_type = SATA_DTYPE_ATAPICD;
9206 		} else {
9207 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9208 		}
9209 	} else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD &&
9210 	    (((cfg_word & SATA_ATAPI_TYPE_MASK) != SATA_ATAPI_TYPE) ||
9211 	    ((cfg_word & SATA_ATAPI_ID_DEV_TYPE) != SATA_ATAPI_CDROM_DEV))) {
9212 		/* Change device type to reflect Identify Device data ! */
9213 		if ((sdinfo->satadrv_id.ai_config & SATA_ATA_TYPE_MASK) ==
9214 		    SATA_ATA_TYPE) {
9215 			sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
9216 		} else {
9217 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9218 		}
9219 	}
9220 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9221 		if (sdinfo->satadrv_capacity == 0) {
9222 			/* Non-LBA disk. Too bad... */
9223 			sata_log(sata_hba_inst, CE_WARN,
9224 			    "SATA disk device at port %d does not support LBA",
9225 			    sdinfo->satadrv_addr.cport);
9226 			rval = SATA_FAILURE;
9227 			goto fail_unknown;
9228 		}
9229 	}
9230 #if 0
9231 	/* Left for historical reason */
9232 	/*
9233 	 * Some initial version of SATA spec indicated that at least
9234 	 * UDMA mode 4 has to be supported. It is not metioned in
9235 	 * SerialATA 2.6, so this restriction is removed.
9236 	 */
9237 	/* Check for Ultra DMA modes 6 through 0 being supported */
9238 	for (i = 6; i >= 0; --i) {
9239 		if (sdinfo->satadrv_id.ai_ultradma & (1 << i))
9240 			break;
9241 	}
9242 
9243 	/*
9244 	 * At least UDMA 4 mode has to be supported. If mode 4 or
9245 	 * higher are not supported by the device, fail this
9246 	 * device.
9247 	 */
9248 	if (i < 4) {
9249 		/* No required Ultra DMA mode supported */
9250 		sata_log(sata_hba_inst, CE_WARN,
9251 		    "SATA disk device at port %d does not support UDMA "
9252 		    "mode 4 or higher", sdinfo->satadrv_addr.cport);
9253 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9254 		    "mode 4 or higher required, %d supported", i));
9255 		rval = SATA_FAILURE;
9256 		goto fail_unknown;
9257 	}
9258 #endif
9259 
9260 	return (SATA_SUCCESS);
9261 
9262 fail_unknown:
9263 	/* Invalidate sata_drive_info ? */
9264 	sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9265 	sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
9266 	return (rval);
9267 }
9268 
9269 /*
9270  * Log/display device information
9271  */
9272 static void
9273 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst,
9274     sata_drive_info_t *sdinfo)
9275 {
9276 	int valid_version;
9277 	char msg_buf[MAXPATHLEN];
9278 	int i;
9279 
9280 	/* Show HBA path */
9281 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf);
9282 
9283 	cmn_err(CE_CONT, "?%s :\n", msg_buf);
9284 
9285 	if (sdinfo->satadrv_type == SATA_DTYPE_UNKNOWN) {
9286 		(void) sprintf(msg_buf,
9287 		    "Unsupported SATA device type (cfg 0x%x) at ",
9288 		    sdinfo->satadrv_id.ai_config);
9289 	} else {
9290 		(void) sprintf(msg_buf, "SATA %s device at",
9291 		    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
9292 		    "disk":"CD/DVD (ATAPI)");
9293 	}
9294 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT)
9295 		cmn_err(CE_CONT, "?\t%s port %d\n",
9296 		    msg_buf, sdinfo->satadrv_addr.cport);
9297 	else
9298 		cmn_err(CE_CONT, "?\t%s port %d pmport %d\n",
9299 		    msg_buf, sdinfo->satadrv_addr.cport,
9300 		    sdinfo->satadrv_addr.pmport);
9301 
9302 	bcopy(&sdinfo->satadrv_id.ai_model, msg_buf,
9303 	    sizeof (sdinfo->satadrv_id.ai_model));
9304 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model));
9305 	msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
9306 	cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf);
9307 
9308 	bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf,
9309 	    sizeof (sdinfo->satadrv_id.ai_fw));
9310 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw));
9311 	msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
9312 	cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf);
9313 
9314 	bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf,
9315 	    sizeof (sdinfo->satadrv_id.ai_drvser));
9316 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser));
9317 	msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
9318 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9319 		cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
9320 	} else {
9321 		/* Assuming ATAPI CD/DVD */
9322 		/*
9323 		 * SOme drives do not implement serial number and may
9324 		 * violate the spec by provinding spaces rather than zeros
9325 		 * in serial number field. Scan the buffer to detect it.
9326 		 */
9327 		for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) {
9328 			if (msg_buf[i] != '\0' && msg_buf[i] != ' ')
9329 				break;
9330 		}
9331 		if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) {
9332 			cmn_err(CE_CONT, "?\tserial number - none\n");
9333 		} else {
9334 			cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
9335 		}
9336 	}
9337 
9338 #ifdef SATA_DEBUG
9339 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
9340 	    sdinfo->satadrv_id.ai_majorversion != 0xffff) {
9341 		int i;
9342 		for (i = 14; i >= 2; i--) {
9343 			if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) {
9344 				valid_version = i;
9345 				break;
9346 			}
9347 		}
9348 		cmn_err(CE_CONT,
9349 		    "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
9350 		    valid_version,
9351 		    sdinfo->satadrv_id.ai_majorversion,
9352 		    sdinfo->satadrv_id.ai_minorversion);
9353 	}
9354 #endif
9355 	/* Log some info */
9356 	cmn_err(CE_CONT, "?\tsupported features:\n");
9357 	msg_buf[0] = '\0';
9358 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9359 		if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48)
9360 			(void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN);
9361 		else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28)
9362 			(void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN);
9363 	}
9364 	if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA)
9365 		(void) strlcat(msg_buf, "DMA", MAXPATHLEN);
9366 	if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ)
9367 		(void) strlcat(msg_buf, ", Native Command Queueing",
9368 		    MAXPATHLEN);
9369 	if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)
9370 		(void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN);
9371 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) &&
9372 	    (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED))
9373 		(void) strlcat(msg_buf, ", SMART", MAXPATHLEN);
9374 	if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) &&
9375 	    (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED))
9376 		(void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN);
9377 	cmn_err(CE_CONT, "?\t %s\n", msg_buf);
9378 	if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2)
9379 		cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n");
9380 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1)
9381 		cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n");
9382 	if (sdinfo->satadrv_features_support &
9383 	    (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) {
9384 		msg_buf[0] = '\0';
9385 		(void) snprintf(msg_buf, MAXPATHLEN,
9386 		    "Supported queue depth %d",
9387 		    sdinfo->satadrv_queue_depth);
9388 		if (!(sata_func_enable &
9389 		    (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ)))
9390 			(void) strlcat(msg_buf,
9391 			    " - queueing disabled globally", MAXPATHLEN);
9392 		else if (sdinfo->satadrv_queue_depth >
9393 		    sdinfo->satadrv_max_queue_depth) {
9394 			(void) snprintf(&msg_buf[strlen(msg_buf)],
9395 			    MAXPATHLEN - strlen(msg_buf), ", limited to %d",
9396 			    (int)sdinfo->satadrv_max_queue_depth);
9397 		}
9398 		cmn_err(CE_CONT, "?\t%s\n", msg_buf);
9399 	}
9400 
9401 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9402 #ifdef __i386
9403 		(void) sprintf(msg_buf, "\tcapacity = %llu sectors\n",
9404 		    sdinfo->satadrv_capacity);
9405 #else
9406 		(void) sprintf(msg_buf, "\tcapacity = %lu sectors\n",
9407 		    sdinfo->satadrv_capacity);
9408 #endif
9409 		cmn_err(CE_CONT, "?%s", msg_buf);
9410 	}
9411 }
9412 
9413 
9414 /*
9415  * sata_save_drive_settings extracts current setting of the device and stores
9416  * it for future reference, in case the device setup would need to be restored
9417  * after the device reset.
9418  *
9419  * For all devices read ahead and write cache settings are saved, if the
9420  * device supports these features at all.
9421  * For ATAPI devices the Removable Media Status Notification setting is saved.
9422  */
9423 static void
9424 sata_save_drive_settings(sata_drive_info_t *sdinfo)
9425 {
9426 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) ||
9427 	    (sdinfo->satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) {
9428 
9429 		/* Current setting of Read Ahead (and Read Cache) */
9430 		if (sdinfo->satadrv_id.ai_features85 & SATA_LOOK_AHEAD)
9431 			sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
9432 		else
9433 			sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD;
9434 
9435 		/* Current setting of Write Cache */
9436 		if (sdinfo->satadrv_id.ai_features85 & SATA_WRITE_CACHE)
9437 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
9438 		else
9439 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
9440 	}
9441 
9442 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
9443 		if (sdinfo->satadrv_id.ai_cmdset83 & SATA_RM_STATUS_NOTIFIC)
9444 			sdinfo->satadrv_settings |= SATA_DEV_RMSN;
9445 		else
9446 			sdinfo->satadrv_settings &= ~SATA_DEV_RMSN;
9447 	}
9448 }
9449 
9450 
9451 /*
9452  * sata_check_capacity function determines a disk capacity
9453  * and addressing mode (LBA28/LBA48) by examining a disk identify device data.
9454  *
9455  * NOTE: CHS mode is not supported! If a device does not support LBA,
9456  * this function is not called.
9457  *
9458  * Returns device capacity in number of blocks, i.e. largest addressable LBA+1
9459  */
9460 static uint64_t
9461 sata_check_capacity(sata_drive_info_t *sdinfo)
9462 {
9463 	uint64_t capacity = 0;
9464 	int i;
9465 
9466 	if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK ||
9467 	    !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT)
9468 		/* Capacity valid only for LBA-addressable disk devices */
9469 		return (0);
9470 
9471 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) &&
9472 	    (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) &&
9473 	    (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) {
9474 		/* LBA48 mode supported and enabled */
9475 		sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 |
9476 		    SATA_DEV_F_LBA28;
9477 		for (i = 3;  i >= 0;  --i) {
9478 			capacity <<= 16;
9479 			capacity += sdinfo->satadrv_id.ai_addrsecxt[i];
9480 		}
9481 	} else {
9482 		capacity = sdinfo->satadrv_id.ai_addrsec[1];
9483 		capacity <<= 16;
9484 		capacity += sdinfo->satadrv_id.ai_addrsec[0];
9485 		if (capacity >= 0x1000000)
9486 			/* LBA28 mode */
9487 			sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28;
9488 	}
9489 	return (capacity);
9490 }
9491 
9492 
9493 /*
9494  * Allocate consistent buffer for DMA transfer
9495  *
9496  * Cannot be called from interrupt level or with mutex held - it may sleep.
9497  *
9498  * Returns pointer to allocated buffer structure, or NULL if allocation failed.
9499  */
9500 static struct buf *
9501 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len)
9502 {
9503 	struct scsi_address ap;
9504 	struct buf *bp;
9505 	ddi_dma_attr_t	cur_dma_attr;
9506 
9507 	ASSERT(spx->txlt_sata_pkt != NULL);
9508 	ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran;
9509 	ap.a_target = SATA_TO_SCSI_TARGET(
9510 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport,
9511 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport,
9512 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual);
9513 	ap.a_lun = 0;
9514 
9515 	bp = scsi_alloc_consistent_buf(&ap, NULL, len,
9516 	    B_READ, SLEEP_FUNC, NULL);
9517 
9518 	if (bp != NULL) {
9519 		/* Allocate DMA resources for this buffer */
9520 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
9521 		/*
9522 		 * We use a local version of the dma_attr, to account
9523 		 * for a device addressing limitations.
9524 		 * sata_adjust_dma_attr() will handle sdinfo == NULL which
9525 		 * will cause dma attributes to be adjusted to a lowest
9526 		 * acceptable level.
9527 		 */
9528 		sata_adjust_dma_attr(NULL,
9529 		    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
9530 
9531 		if (sata_dma_buf_setup(spx, PKT_CONSISTENT,
9532 		    SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) {
9533 			scsi_free_consistent_buf(bp);
9534 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
9535 			bp = NULL;
9536 		}
9537 	}
9538 	return (bp);
9539 }
9540 
9541 /*
9542  * Release local buffer (consistent buffer for DMA transfer) allocated
9543  * via sata_alloc_local_buffer().
9544  */
9545 static void
9546 sata_free_local_buffer(sata_pkt_txlate_t *spx)
9547 {
9548 	ASSERT(spx->txlt_sata_pkt != NULL);
9549 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL);
9550 
9551 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0;
9552 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL;
9553 
9554 	if (spx->txlt_buf_dma_handle != NULL) {
9555 		/* Free DMA resources */
9556 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
9557 		ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
9558 		spx->txlt_buf_dma_handle = 0;
9559 
9560 		if (spx->txlt_dma_cookie_list != &spx->txlt_dma_cookie) {
9561 			kmem_free(spx->txlt_dma_cookie_list,
9562 			    spx->txlt_dma_cookie_list_len *
9563 			    sizeof (ddi_dma_cookie_t));
9564 			spx->txlt_dma_cookie_list = NULL;
9565 			spx->txlt_dma_cookie_list_len = 0;
9566 		}
9567 	}
9568 
9569 	/* Free buffer */
9570 	scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp);
9571 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
9572 }
9573 
9574 
9575 
9576 
9577 /*
9578  * Allocate sata_pkt
9579  * Pkt structure version and embedded strcutures version are initialized.
9580  * sata_pkt and sata_pkt_txlate structures are cross-linked.
9581  *
9582  * Since this may be called in interrupt context by sata_scsi_init_pkt,
9583  * callback argument determines if it can sleep or not.
9584  * Hence, it should not be called from interrupt context.
9585  *
9586  * If successful, non-NULL pointer to a sata pkt is returned.
9587  * Upon failure, NULL pointer is returned.
9588  */
9589 static sata_pkt_t *
9590 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t))
9591 {
9592 	sata_pkt_t *spkt;
9593 	int kmsflag;
9594 
9595 	kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
9596 	spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag);
9597 	if (spkt == NULL) {
9598 		SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9599 		    "sata_pkt_alloc: failed"));
9600 		return (NULL);
9601 	}
9602 	spkt->satapkt_rev = SATA_PKT_REV;
9603 	spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV;
9604 	spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
9605 	spkt->satapkt_framework_private = spx;
9606 	spx->txlt_sata_pkt = spkt;
9607 	return (spkt);
9608 }
9609 
9610 /*
9611  * Free sata pkt allocated via sata_pkt_alloc()
9612  */
9613 static void
9614 sata_pkt_free(sata_pkt_txlate_t *spx)
9615 {
9616 	ASSERT(spx->txlt_sata_pkt != NULL);
9617 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL);
9618 	kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t));
9619 	spx->txlt_sata_pkt = NULL;
9620 }
9621 
9622 
9623 /*
9624  * Adjust DMA attributes.
9625  * SCSI cmds block count is up to 24 bits, SATA cmd block count vary
9626  * from 8 bits to 16 bits, depending on a command being used.
9627  * Limiting max block count arbitrarily to 256 for all read/write
9628  * commands may affects performance, so check both the device and
9629  * controller capability before adjusting dma attributes.
9630  */
9631 void
9632 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr,
9633     ddi_dma_attr_t *adj_dma_attr)
9634 {
9635 	uint32_t count_max;
9636 
9637 	/* Copy original attributes */
9638 	*adj_dma_attr = *dma_attr;
9639 	/*
9640 	 * Things to consider: device addressing capability,
9641 	 * "excessive" controller DMA capabilities.
9642 	 * If a device is being probed/initialized, there are
9643 	 * no device info - use default limits then.
9644 	 */
9645 	if (sdinfo == NULL) {
9646 		count_max = dma_attr->dma_attr_granular * 0x100;
9647 		if (dma_attr->dma_attr_count_max > count_max)
9648 			adj_dma_attr->dma_attr_count_max = count_max;
9649 		if (dma_attr->dma_attr_maxxfer > count_max)
9650 			adj_dma_attr->dma_attr_maxxfer = count_max;
9651 		return;
9652 	}
9653 
9654 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9655 		if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) {
9656 			/*
9657 			 * 16-bit sector count may be used - we rely on
9658 			 * the assumption that only read and write cmds
9659 			 * will request more than 256 sectors worth of data
9660 			 */
9661 			count_max = adj_dma_attr->dma_attr_granular * 0x10000;
9662 		} else {
9663 			/*
9664 			 * 8-bit sector count will be used - default limits
9665 			 * for dma attributes
9666 			 */
9667 			count_max = adj_dma_attr->dma_attr_granular * 0x100;
9668 		}
9669 		/*
9670 		 * Adjust controler dma attributes, if necessary
9671 		 */
9672 		if (dma_attr->dma_attr_count_max > count_max)
9673 			adj_dma_attr->dma_attr_count_max = count_max;
9674 		if (dma_attr->dma_attr_maxxfer > count_max)
9675 			adj_dma_attr->dma_attr_maxxfer = count_max;
9676 	}
9677 }
9678 
9679 
9680 /*
9681  * Allocate DMA resources for the buffer
9682  * This function handles initial DMA resource allocation as well as
9683  * DMA window shift and may be called repeatedly for the same DMA window
9684  * until all DMA cookies in the DMA window are processed.
9685  * To guarantee that there is always a coherent set of cookies to process
9686  * by SATA HBA driver (observing alignment, device granularity, etc.),
9687  * the number of slots for DMA cookies is equal to lesser of  a number of
9688  * cookies in a DMA window and a max number of scatter/gather entries.
9689  *
9690  * Returns DDI_SUCCESS upon successful operation.
9691  * Return failure code of a failing command or DDI_FAILURE when
9692  * internal cleanup failed.
9693  */
9694 static int
9695 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags,
9696     int (*callback)(caddr_t), caddr_t arg,
9697     ddi_dma_attr_t *cur_dma_attr)
9698 {
9699 	int	rval;
9700 	off_t	offset;
9701 	size_t	size;
9702 	int	max_sg_len, req_len, i;
9703 	uint_t	dma_flags;
9704 	struct buf	*bp;
9705 	uint64_t	cur_txfer_len;
9706 
9707 
9708 	ASSERT(spx->txlt_sata_pkt != NULL);
9709 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9710 	ASSERT(bp != NULL);
9711 
9712 
9713 	if (spx->txlt_buf_dma_handle == NULL) {
9714 		/*
9715 		 * No DMA resources allocated so far - this is a first call
9716 		 * for this sata pkt.
9717 		 */
9718 		rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst),
9719 		    cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle);
9720 
9721 		if (rval != DDI_SUCCESS) {
9722 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9723 			    "sata_dma_buf_setup: no buf DMA resources %x",
9724 			    rval));
9725 			return (rval);
9726 		}
9727 
9728 		if (bp->b_flags & B_READ)
9729 			dma_flags = DDI_DMA_READ;
9730 		else
9731 			dma_flags = DDI_DMA_WRITE;
9732 
9733 		if (flags & PKT_CONSISTENT)
9734 			dma_flags |= DDI_DMA_CONSISTENT;
9735 
9736 		if (flags & PKT_DMA_PARTIAL)
9737 			dma_flags |= DDI_DMA_PARTIAL;
9738 
9739 		/*
9740 		 * Check buffer alignment and size against dma attributes
9741 		 * Consider dma_attr_align only. There may be requests
9742 		 * with the size lower than device granularity, but they
9743 		 * will not read/write from/to the device, so no adjustment
9744 		 * is necessary. The dma_attr_minxfer theoretically should
9745 		 * be considered, but no HBA driver is checking it.
9746 		 */
9747 		if (IS_P2ALIGNED(bp->b_un.b_addr,
9748 		    cur_dma_attr->dma_attr_align)) {
9749 			rval = ddi_dma_buf_bind_handle(
9750 			    spx->txlt_buf_dma_handle,
9751 			    bp, dma_flags, callback, arg,
9752 			    &spx->txlt_dma_cookie,
9753 			    &spx->txlt_curwin_num_dma_cookies);
9754 		} else { /* Buffer is not aligned */
9755 
9756 			int	(*ddicallback)(caddr_t);
9757 			size_t	bufsz;
9758 
9759 			/* Check id sleeping is allowed */
9760 			ddicallback = (callback == NULL_FUNC) ?
9761 			    DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
9762 
9763 			SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
9764 			    "mis-aligned buffer: addr=0x%p, cnt=%lu",
9765 			    (void *)bp->b_un.b_addr, bp->b_bcount);
9766 
9767 			if (bp->b_flags & (B_PAGEIO|B_PHYS))
9768 				/*
9769 				 * CPU will need to access data in the buffer
9770 				 * (for copying) so map it.
9771 				 */
9772 				bp_mapin(bp);
9773 
9774 			ASSERT(spx->txlt_tmp_buf == NULL);
9775 
9776 			/* Buffer may be padded by ddi_dma_mem_alloc()! */
9777 			rval = ddi_dma_mem_alloc(
9778 			    spx->txlt_buf_dma_handle,
9779 			    bp->b_bcount,
9780 			    &sata_acc_attr,
9781 			    DDI_DMA_STREAMING,
9782 			    ddicallback, NULL,
9783 			    &spx->txlt_tmp_buf,
9784 			    &bufsz,
9785 			    &spx->txlt_tmp_buf_handle);
9786 
9787 			if (rval != DDI_SUCCESS) {
9788 				/* DMA mapping failed */
9789 				(void) ddi_dma_free_handle(
9790 				    &spx->txlt_buf_dma_handle);
9791 				spx->txlt_buf_dma_handle = NULL;
9792 #ifdef SATA_DEBUG
9793 				mbuffail_count++;
9794 #endif
9795 				SATADBG1(SATA_DBG_DMA_SETUP,
9796 				    spx->txlt_sata_hba_inst,
9797 				    "sata_dma_buf_setup: "
9798 				    "buf dma mem alloc failed %x\n", rval);
9799 				return (rval);
9800 			}
9801 			ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf,
9802 			    cur_dma_attr->dma_attr_align));
9803 
9804 #ifdef SATA_DEBUG
9805 			mbuf_count++;
9806 
9807 			if (bp->b_bcount != bufsz)
9808 				/*
9809 				 * This will require special handling, because
9810 				 * DMA cookies will be based on the temporary
9811 				 * buffer size, not the original buffer
9812 				 * b_bcount, so the residue may have to
9813 				 * be counted differently.
9814 				 */
9815 				SATADBG2(SATA_DBG_DMA_SETUP,
9816 				    spx->txlt_sata_hba_inst,
9817 				    "sata_dma_buf_setup: bp size %x != "
9818 				    "bufsz %x\n", bp->b_bcount, bufsz);
9819 #endif
9820 			if (dma_flags & DDI_DMA_WRITE) {
9821 				/*
9822 				 * Write operation - copy data into
9823 				 * an aligned temporary buffer. Buffer will be
9824 				 * synced for device by ddi_dma_addr_bind_handle
9825 				 */
9826 				bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf,
9827 				    bp->b_bcount);
9828 			}
9829 
9830 			rval = ddi_dma_addr_bind_handle(
9831 			    spx->txlt_buf_dma_handle,
9832 			    NULL,
9833 			    spx->txlt_tmp_buf,
9834 			    bufsz, dma_flags, ddicallback, 0,
9835 			    &spx->txlt_dma_cookie,
9836 			    &spx->txlt_curwin_num_dma_cookies);
9837 		}
9838 
9839 		switch (rval) {
9840 		case DDI_DMA_PARTIAL_MAP:
9841 			SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
9842 			    "sata_dma_buf_setup: DMA Partial Map\n", NULL);
9843 			/*
9844 			 * Partial DMA mapping.
9845 			 * Retrieve number of DMA windows for this request.
9846 			 */
9847 			if (ddi_dma_numwin(spx->txlt_buf_dma_handle,
9848 			    &spx->txlt_num_dma_win) != DDI_SUCCESS) {
9849 				if (spx->txlt_tmp_buf != NULL) {
9850 					ddi_dma_mem_free(
9851 					    &spx->txlt_tmp_buf_handle);
9852 					spx->txlt_tmp_buf = NULL;
9853 				}
9854 				(void) ddi_dma_unbind_handle(
9855 				    spx->txlt_buf_dma_handle);
9856 				(void) ddi_dma_free_handle(
9857 				    &spx->txlt_buf_dma_handle);
9858 				spx->txlt_buf_dma_handle = NULL;
9859 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9860 				    "sata_dma_buf_setup: numwin failed\n"));
9861 				return (DDI_FAILURE);
9862 			}
9863 			SATADBG2(SATA_DBG_DMA_SETUP,
9864 			    spx->txlt_sata_hba_inst,
9865 			    "sata_dma_buf_setup: windows: %d, cookies: %d\n",
9866 			    spx->txlt_num_dma_win,
9867 			    spx->txlt_curwin_num_dma_cookies);
9868 			spx->txlt_cur_dma_win = 0;
9869 			break;
9870 
9871 		case DDI_DMA_MAPPED:
9872 			/* DMA fully mapped */
9873 			spx->txlt_num_dma_win = 1;
9874 			spx->txlt_cur_dma_win = 0;
9875 			SATADBG1(SATA_DBG_DMA_SETUP,
9876 			    spx->txlt_sata_hba_inst,
9877 			    "sata_dma_buf_setup: windows: 1 "
9878 			    "cookies: %d\n", spx->txlt_curwin_num_dma_cookies);
9879 			break;
9880 
9881 		default:
9882 			/* DMA mapping failed */
9883 			if (spx->txlt_tmp_buf != NULL) {
9884 				ddi_dma_mem_free(
9885 				    &spx->txlt_tmp_buf_handle);
9886 				spx->txlt_tmp_buf = NULL;
9887 			}
9888 			(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
9889 			spx->txlt_buf_dma_handle = NULL;
9890 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9891 			    "sata_dma_buf_setup: buf dma handle binding "
9892 			    "failed %x\n", rval));
9893 			return (rval);
9894 		}
9895 		spx->txlt_curwin_processed_dma_cookies = 0;
9896 		spx->txlt_dma_cookie_list = NULL;
9897 	} else {
9898 		/*
9899 		 * DMA setup is reused. Check if we need to process more
9900 		 * cookies in current window, or to get next window, if any.
9901 		 */
9902 
9903 		ASSERT(spx->txlt_curwin_processed_dma_cookies <=
9904 		    spx->txlt_curwin_num_dma_cookies);
9905 
9906 		if (spx->txlt_curwin_processed_dma_cookies ==
9907 		    spx->txlt_curwin_num_dma_cookies) {
9908 			/*
9909 			 * All cookies from current DMA window were processed.
9910 			 * Get next DMA window.
9911 			 */
9912 			spx->txlt_cur_dma_win++;
9913 			if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) {
9914 				(void) ddi_dma_getwin(spx->txlt_buf_dma_handle,
9915 				    spx->txlt_cur_dma_win, &offset, &size,
9916 				    &spx->txlt_dma_cookie,
9917 				    &spx->txlt_curwin_num_dma_cookies);
9918 				spx->txlt_curwin_processed_dma_cookies = 0;
9919 			} else {
9920 				/* No more windows! End of request! */
9921 				/* What to do? - panic for now */
9922 				ASSERT(spx->txlt_cur_dma_win >=
9923 				    spx->txlt_num_dma_win);
9924 
9925 				spx->txlt_curwin_num_dma_cookies = 0;
9926 				spx->txlt_curwin_processed_dma_cookies = 0;
9927 				spx->txlt_sata_pkt->
9928 				    satapkt_cmd.satacmd_num_dma_cookies = 0;
9929 				return (DDI_SUCCESS);
9930 			}
9931 		}
9932 	}
9933 	/* There better be at least one DMA cookie outstanding */
9934 	ASSERT((spx->txlt_curwin_num_dma_cookies -
9935 	    spx->txlt_curwin_processed_dma_cookies) > 0);
9936 
9937 	if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) {
9938 		/* The default cookie slot was used in previous run */
9939 		ASSERT(spx->txlt_curwin_processed_dma_cookies == 0);
9940 		spx->txlt_dma_cookie_list = NULL;
9941 		spx->txlt_dma_cookie_list_len = 0;
9942 	}
9943 	if (spx->txlt_curwin_processed_dma_cookies == 0) {
9944 		/*
9945 		 * Processing a new DMA window - set-up dma cookies list.
9946 		 * We may reuse previously allocated cookie array if it is
9947 		 * possible.
9948 		 */
9949 		if (spx->txlt_dma_cookie_list != NULL &&
9950 		    spx->txlt_dma_cookie_list_len <
9951 		    spx->txlt_curwin_num_dma_cookies) {
9952 			/*
9953 			 * New DMA window contains more cookies than
9954 			 * the previous one. We need larger cookie list - free
9955 			 * the old one.
9956 			 */
9957 			(void) kmem_free(spx->txlt_dma_cookie_list,
9958 			    spx->txlt_dma_cookie_list_len *
9959 			    sizeof (ddi_dma_cookie_t));
9960 			spx->txlt_dma_cookie_list = NULL;
9961 			spx->txlt_dma_cookie_list_len = 0;
9962 		}
9963 		if (spx->txlt_dma_cookie_list == NULL) {
9964 			/*
9965 			 * Calculate lesser of number of cookies in this
9966 			 * DMA window and number of s/g entries.
9967 			 */
9968 			max_sg_len = cur_dma_attr->dma_attr_sgllen;
9969 			req_len = MIN(max_sg_len,
9970 			    spx->txlt_curwin_num_dma_cookies);
9971 
9972 			/* Allocate new dma cookie array if necessary */
9973 			if (req_len == 1) {
9974 				/* Only one cookie - no need for a list */
9975 				spx->txlt_dma_cookie_list =
9976 				    &spx->txlt_dma_cookie;
9977 				spx->txlt_dma_cookie_list_len = 1;
9978 			} else {
9979 				/*
9980 				 * More than one cookie - try to allocate space.
9981 				 */
9982 				spx->txlt_dma_cookie_list = kmem_zalloc(
9983 				    sizeof (ddi_dma_cookie_t) * req_len,
9984 				    callback == NULL_FUNC ? KM_NOSLEEP :
9985 				    KM_SLEEP);
9986 				if (spx->txlt_dma_cookie_list == NULL) {
9987 					SATADBG1(SATA_DBG_DMA_SETUP,
9988 					    spx->txlt_sata_hba_inst,
9989 					    "sata_dma_buf_setup: cookie list "
9990 					    "allocation failed\n", NULL);
9991 					/*
9992 					 * We could not allocate space for
9993 					 * neccessary number of dma cookies in
9994 					 * this window, so we fail this request.
9995 					 * Next invocation would try again to
9996 					 * allocate space for cookie list.
9997 					 * Note:Packet residue was not modified.
9998 					 */
9999 					return (DDI_DMA_NORESOURCES);
10000 				} else {
10001 					spx->txlt_dma_cookie_list_len = req_len;
10002 				}
10003 			}
10004 		}
10005 		/*
10006 		 * Fetch DMA cookies into cookie list in sata_pkt_txlate.
10007 		 * First cookie was already fetched.
10008 		 */
10009 		*(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie;
10010 		cur_txfer_len =
10011 		    (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size;
10012 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1;
10013 		spx->txlt_curwin_processed_dma_cookies++;
10014 		for (i = 1; (i < spx->txlt_dma_cookie_list_len) &&
10015 		    (i < spx->txlt_curwin_num_dma_cookies); i++) {
10016 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
10017 			    &spx->txlt_dma_cookie_list[i]);
10018 			cur_txfer_len +=
10019 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
10020 			spx->txlt_curwin_processed_dma_cookies++;
10021 			spx->txlt_sata_pkt->
10022 			    satapkt_cmd.satacmd_num_dma_cookies += 1;
10023 		}
10024 	} else {
10025 		SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
10026 		    "sata_dma_buf_setup: sliding within DMA window, "
10027 		    "cur cookie %d, total cookies %d\n",
10028 		    spx->txlt_curwin_processed_dma_cookies,
10029 		    spx->txlt_curwin_num_dma_cookies);
10030 
10031 		/*
10032 		 * Not all cookies from the current dma window were used because
10033 		 * of s/g limitation.
10034 		 * There is no need to re-size the list - it was set at
10035 		 * optimal size, or only default entry is used (s/g = 1).
10036 		 */
10037 		if (spx->txlt_dma_cookie_list == NULL) {
10038 			spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie;
10039 			spx->txlt_dma_cookie_list_len = 1;
10040 		}
10041 		/*
10042 		 * Since we are processing remaining cookies in a DMA window,
10043 		 * there may be less of them than the number of entries in the
10044 		 * current dma cookie list.
10045 		 */
10046 		req_len = MIN(spx->txlt_dma_cookie_list_len,
10047 		    (spx->txlt_curwin_num_dma_cookies -
10048 		    spx->txlt_curwin_processed_dma_cookies));
10049 
10050 		/* Fetch the next batch of cookies */
10051 		for (i = 0, cur_txfer_len = 0; i < req_len; i++) {
10052 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
10053 			    &spx->txlt_dma_cookie_list[i]);
10054 			cur_txfer_len +=
10055 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
10056 			spx->txlt_sata_pkt->
10057 			    satapkt_cmd.satacmd_num_dma_cookies++;
10058 			spx->txlt_curwin_processed_dma_cookies++;
10059 		}
10060 	}
10061 
10062 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0);
10063 
10064 	/* Point sata_cmd to the cookie list */
10065 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list =
10066 	    &spx->txlt_dma_cookie_list[0];
10067 
10068 	/* Remember number of DMA cookies passed in sata packet */
10069 	spx->txlt_num_dma_cookies =
10070 	    spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies;
10071 
10072 	ASSERT(cur_txfer_len != 0);
10073 	if (cur_txfer_len <= bp->b_bcount)
10074 		spx->txlt_total_residue -= cur_txfer_len;
10075 	else {
10076 		/*
10077 		 * Temporary DMA buffer has been padded by
10078 		 * ddi_dma_mem_alloc()!
10079 		 * This requires special handling, because DMA cookies are
10080 		 * based on the temporary buffer size, not the b_bcount,
10081 		 * and we have extra bytes to transfer - but the packet
10082 		 * residue has to stay correct because we will copy only
10083 		 * the requested number of bytes.
10084 		 */
10085 		spx->txlt_total_residue -= bp->b_bcount;
10086 	}
10087 
10088 	return (DDI_SUCCESS);
10089 }
10090 
10091 /*
10092  * Common routine for releasing DMA resources
10093  */
10094 static void
10095 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx)
10096 {
10097 	if (spx->txlt_buf_dma_handle != NULL) {
10098 		if (spx->txlt_tmp_buf != NULL)  {
10099 			/*
10100 			 * Intermediate DMA buffer was allocated.
10101 			 * Free allocated buffer and associated access handle.
10102 			 */
10103 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
10104 			spx->txlt_tmp_buf = NULL;
10105 		}
10106 		/*
10107 		 * Free DMA resources - cookies and handles
10108 		 */
10109 		/* ASSERT(spx->txlt_dma_cookie_list != NULL); */
10110 		if (spx->txlt_dma_cookie_list != NULL) {
10111 			if (spx->txlt_dma_cookie_list !=
10112 			    &spx->txlt_dma_cookie) {
10113 				(void) kmem_free(spx->txlt_dma_cookie_list,
10114 				    spx->txlt_dma_cookie_list_len *
10115 				    sizeof (ddi_dma_cookie_t));
10116 				spx->txlt_dma_cookie_list = NULL;
10117 			}
10118 		}
10119 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
10120 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
10121 		spx->txlt_buf_dma_handle = NULL;
10122 	}
10123 }
10124 
10125 /*
10126  * Free DMA resources
10127  * Used by the HBA driver to release DMA resources that it does not use.
10128  *
10129  * Returns Void
10130  */
10131 void
10132 sata_free_dma_resources(sata_pkt_t *sata_pkt)
10133 {
10134 	sata_pkt_txlate_t *spx;
10135 
10136 	if (sata_pkt == NULL)
10137 		return;
10138 
10139 	spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
10140 
10141 	sata_common_free_dma_rsrcs(spx);
10142 }
10143 
10144 /*
10145  * Fetch Device Identify data.
10146  * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type)
10147  * command to a device and get the device identify data.
10148  * The device_info structure has to be set to device type (for selecting proper
10149  * device identify command).
10150  *
10151  * Returns:
10152  * SATA_SUCCESS if cmd succeeded
10153  * SATA_RETRY if cmd was rejected and could be retried,
10154  * SATA_FAILURE if cmd failed and should not be retried (port error)
10155  *
10156  * Cannot be called in an interrupt context.
10157  */
10158 
10159 static int
10160 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst,
10161     sata_drive_info_t *sdinfo)
10162 {
10163 	struct buf *bp;
10164 	sata_pkt_t *spkt;
10165 	sata_cmd_t *scmd;
10166 	sata_pkt_txlate_t *spx;
10167 	int rval;
10168 
10169 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10170 	spx->txlt_sata_hba_inst = sata_hba_inst;
10171 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
10172 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10173 	if (spkt == NULL) {
10174 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10175 		return (SATA_RETRY); /* may retry later */
10176 	}
10177 	/* address is needed now */
10178 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10179 
10180 	/*
10181 	 * Allocate buffer for Identify Data return data
10182 	 */
10183 	bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t));
10184 	if (bp == NULL) {
10185 		sata_pkt_free(spx);
10186 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10187 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10188 		    "sata_fetch_device_identify_data: "
10189 		    "cannot allocate buffer for ID"));
10190 		return (SATA_RETRY); /* may retry later */
10191 	}
10192 
10193 	/* Fill sata_pkt */
10194 	sdinfo->satadrv_state = SATA_STATE_PROBING;
10195 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10196 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10197 	/* Synchronous mode, no callback */
10198 	spkt->satapkt_comp = NULL;
10199 	/* Timeout 30s */
10200 	spkt->satapkt_time = sata_default_pkt_time;
10201 
10202 	scmd = &spkt->satapkt_cmd;
10203 	scmd->satacmd_bp = bp;
10204 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
10205 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10206 
10207 	/* Build Identify Device cmd in the sata_pkt */
10208 	scmd->satacmd_addr_type = 0;		/* N/A */
10209 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
10210 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
10211 	scmd->satacmd_lba_mid_lsb = 0;		/* N/A */
10212 	scmd->satacmd_lba_high_lsb = 0;		/* N/A */
10213 	scmd->satacmd_features_reg = 0;		/* N/A */
10214 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
10215 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
10216 		/* Identify Packet Device cmd */
10217 		scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE;
10218 	} else {
10219 		/* Identify Device cmd - mandatory for all other devices */
10220 		scmd->satacmd_cmd_reg = SATAC_ID_DEVICE;
10221 	}
10222 
10223 	/* Send pkt to SATA HBA driver */
10224 	rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt);
10225 
10226 #ifdef SATA_INJECT_FAULTS
10227 	if (sata_inject_fault == SATA_INJECT_PKT_FAULT)
10228 		if (sata_fault_cmd == scmd->satacmd_cmd_reg)
10229 			sata_inject_pkt_fault(spkt, scmd->satacmd_cmd_reg,
10230 			    &rval, sata_inject_fault_type);
10231 #endif
10232 
10233 	if (rval == SATA_TRAN_ACCEPTED &&
10234 	    spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10235 		if (spx->txlt_buf_dma_handle != NULL) {
10236 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10237 			    DDI_DMA_SYNC_FORKERNEL);
10238 			ASSERT(rval == DDI_SUCCESS);
10239 		}
10240 		if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config &
10241 		    SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) {
10242 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10243 			    "SATA disk device at port %d - "
10244 			    "partial Identify Data",
10245 			    sdinfo->satadrv_addr.cport));
10246 			rval = SATA_RETRY; /* may retry later */
10247 			goto fail;
10248 		}
10249 		/* Update sata_drive_info */
10250 		bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id,
10251 		    sizeof (sata_id_t));
10252 
10253 		sdinfo->satadrv_features_support = 0;
10254 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10255 			/*
10256 			 * Retrieve capacity (disks only) and addressing mode
10257 			 */
10258 			sdinfo->satadrv_capacity = sata_check_capacity(sdinfo);
10259 		} else {
10260 			/*
10261 			 * For ATAPI devices one would have to issue
10262 			 * Get Capacity cmd for media capacity. Not here.
10263 			 */
10264 			sdinfo->satadrv_capacity = 0;
10265 			/*
10266 			 * Check what cdb length is supported
10267 			 */
10268 			if ((sdinfo->satadrv_id.ai_config &
10269 			    SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B)
10270 				sdinfo->satadrv_atapi_cdb_len = 16;
10271 			else
10272 				sdinfo->satadrv_atapi_cdb_len = 12;
10273 		}
10274 		/* Setup supported features flags */
10275 		if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT)
10276 			sdinfo->satadrv_features_support |= SATA_DEV_F_DMA;
10277 
10278 		/* Check for SATA GEN and NCQ support */
10279 		if (sdinfo->satadrv_id.ai_satacap != 0 &&
10280 		    sdinfo->satadrv_id.ai_satacap != 0xffff) {
10281 			/* SATA compliance */
10282 			if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ)
10283 				sdinfo->satadrv_features_support |=
10284 				    SATA_DEV_F_NCQ;
10285 			if (sdinfo->satadrv_id.ai_satacap &
10286 			    (SATA_1_SPEED | SATA_2_SPEED)) {
10287 				if (sdinfo->satadrv_id.ai_satacap &
10288 				    SATA_2_SPEED)
10289 					sdinfo->satadrv_features_support |=
10290 					    SATA_DEV_F_SATA2;
10291 				if (sdinfo->satadrv_id.ai_satacap &
10292 				    SATA_1_SPEED)
10293 					sdinfo->satadrv_features_support |=
10294 					    SATA_DEV_F_SATA1;
10295 			} else {
10296 				sdinfo->satadrv_features_support |=
10297 				    SATA_DEV_F_SATA1;
10298 			}
10299 		}
10300 		if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) &&
10301 		    (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD))
10302 			sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ;
10303 
10304 		sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth;
10305 		if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) ||
10306 		    (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) {
10307 			++sdinfo->satadrv_queue_depth;
10308 			/* Adjust according to controller capabilities */
10309 			sdinfo->satadrv_max_queue_depth = MIN(
10310 			    sdinfo->satadrv_queue_depth,
10311 			    SATA_QDEPTH(sata_hba_inst));
10312 			/* Adjust according to global queue depth limit */
10313 			sdinfo->satadrv_max_queue_depth = MIN(
10314 			    sdinfo->satadrv_max_queue_depth,
10315 			    sata_current_max_qdepth);
10316 			if (sdinfo->satadrv_max_queue_depth == 0)
10317 				sdinfo->satadrv_max_queue_depth = 1;
10318 		} else
10319 			sdinfo->satadrv_max_queue_depth = 1;
10320 
10321 		rval = SATA_SUCCESS;
10322 	} else {
10323 		/*
10324 		 * Woops, no Identify Data.
10325 		 */
10326 		if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) {
10327 			rval = SATA_RETRY; /* may retry later */
10328 		} else if (rval == SATA_TRAN_ACCEPTED) {
10329 			if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR ||
10330 			    spkt->satapkt_reason == SATA_PKT_ABORTED ||
10331 			    spkt->satapkt_reason == SATA_PKT_TIMEOUT ||
10332 			    spkt->satapkt_reason == SATA_PKT_RESET)
10333 				rval = SATA_RETRY; /* may retry later */
10334 			else
10335 				rval = SATA_FAILURE;
10336 		} else {
10337 			rval = SATA_FAILURE;
10338 		}
10339 	}
10340 fail:
10341 	/* Free allocated resources */
10342 	sata_free_local_buffer(spx);
10343 	sata_pkt_free(spx);
10344 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
10345 
10346 	return (rval);
10347 }
10348 
10349 
10350 /*
10351  * Some devices may not come-up with default DMA mode (UDMA or MWDMA).
10352  * UDMA mode is checked first, followed by MWDMA mode.
10353  * set correctly, so this function is setting it to the highest supported level.
10354  * Older SATA spec required that the device supports at least DMA 4 mode and
10355  * UDMA mode is selected.  It is not mentioned in SerialATA 2.6, so this
10356  * restriction has been removed.
10357  *
10358  * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported.
10359  * Returns SATA_FAILURE if proper DMA mode could not be selected.
10360  *
10361  * NOTE: This function should be called only if DMA mode is supported.
10362  */
10363 static int
10364 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo)
10365 {
10366 	sata_pkt_t *spkt;
10367 	sata_cmd_t *scmd;
10368 	sata_pkt_txlate_t *spx;
10369 	int i, mode;
10370 	uint8_t subcmd;
10371 	int rval = SATA_SUCCESS;
10372 
10373 	ASSERT(sdinfo != NULL);
10374 	ASSERT(sata_hba_inst != NULL);
10375 
10376 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
10377 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) {
10378 		/* Find highest Ultra DMA mode supported */
10379 		for (mode = 6; mode >= 0; --mode) {
10380 			if (sdinfo->satadrv_id.ai_ultradma & (1 << mode))
10381 				break;
10382 		}
10383 #if 0
10384 		/* Left for historical reasons */
10385 		/*
10386 		 * Some initial version of SATA spec indicated that at least
10387 		 * UDMA mode 4 has to be supported. It is not mentioned in
10388 		 * SerialATA 2.6, so this restriction is removed.
10389 		 */
10390 		if (mode < 4)
10391 			return (SATA_FAILURE);
10392 #endif
10393 		/* Find UDMA mode currently selected */
10394 		for (i = 6; i >= 0; --i) {
10395 			if (sdinfo->satadrv_id.ai_ultradma & (1 << (i + 8)))
10396 				break;
10397 		}
10398 		if (i >= mode)
10399 			/* Nothing to do */
10400 			return (SATA_SUCCESS);
10401 
10402 		subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA;
10403 
10404 	} else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) {
10405 		/* Find highest MultiWord DMA mode supported */
10406 		for (mode = 2; mode >= 0; --mode) {
10407 			if (sdinfo->satadrv_id.ai_dworddma & (1 << mode))
10408 				break;
10409 		}
10410 		/* Find highest MultiWord DMA mode selected */
10411 		for (i = 2; i >= 0; --i) {
10412 			if (sdinfo->satadrv_id.ai_dworddma & (1 << (i + 8)))
10413 				break;
10414 		}
10415 		if (i >= mode)
10416 			/* Nothing to do */
10417 			return (SATA_SUCCESS);
10418 
10419 		subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA;
10420 	} else
10421 		return (SATA_SUCCESS);
10422 
10423 	/*
10424 	 * Set DMA mode via SET FEATURES COMMAND.
10425 	 * Prepare packet for SET FEATURES COMMAND.
10426 	 */
10427 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10428 	spx->txlt_sata_hba_inst = sata_hba_inst;
10429 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10430 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10431 	if (spkt == NULL) {
10432 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10433 		    "sata_set_dma_mode: could not set DMA mode %", mode));
10434 		rval = SATA_FAILURE;
10435 		goto done;
10436 	}
10437 	/* Fill sata_pkt */
10438 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10439 	/* Timeout 30s */
10440 	spkt->satapkt_time = sata_default_pkt_time;
10441 	/* Synchronous mode, no callback, interrupts */
10442 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10443 	spkt->satapkt_comp = NULL;
10444 	scmd = &spkt->satapkt_cmd;
10445 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10446 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10447 	scmd->satacmd_addr_type = 0;
10448 	scmd->satacmd_device_reg = 0;
10449 	scmd->satacmd_status_reg = 0;
10450 	scmd->satacmd_error_reg = 0;
10451 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10452 	scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE;
10453 	scmd->satacmd_sec_count_lsb = subcmd | mode;
10454 
10455 	/* Transfer command to HBA */
10456 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
10457 	    spkt) != SATA_TRAN_ACCEPTED ||
10458 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
10459 		/* Pkt execution failed */
10460 		rval = SATA_FAILURE;
10461 	}
10462 done:
10463 
10464 	/* Free allocated resources */
10465 	if (spkt != NULL)
10466 		sata_pkt_free(spx);
10467 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10468 
10469 	return (rval);
10470 }
10471 
10472 
10473 /*
10474  * Set device caching mode.
10475  * One of the following operations should be specified:
10476  * SATAC_SF_ENABLE_READ_AHEAD
10477  * SATAC_SF_DISABLE_READ_AHEAD
10478  * SATAC_SF_ENABLE_WRITE_CACHE
10479  * SATAC_SF_DISABLE_WRITE_CACHE
10480  *
10481  * If operation fails, system log messgage is emitted.
10482  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
10483  */
10484 
10485 static int
10486 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
10487     int cache_op)
10488 {
10489 	sata_pkt_t *spkt;
10490 	sata_cmd_t *scmd;
10491 	sata_pkt_txlate_t *spx;
10492 	int rval = SATA_SUCCESS;
10493 	char *infop;
10494 
10495 	ASSERT(sdinfo != NULL);
10496 	ASSERT(sata_hba_inst != NULL);
10497 	ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD ||
10498 	    cache_op == SATAC_SF_DISABLE_READ_AHEAD ||
10499 	    cache_op == SATAC_SF_ENABLE_WRITE_CACHE ||
10500 	    cache_op == SATAC_SF_DISABLE_WRITE_CACHE);
10501 
10502 
10503 	/* Prepare packet for SET FEATURES COMMAND */
10504 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10505 	spx->txlt_sata_hba_inst = sata_hba_inst;
10506 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10507 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10508 	if (spkt == NULL) {
10509 		rval = SATA_FAILURE;
10510 		goto failure;
10511 	}
10512 	/* Fill sata_pkt */
10513 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10514 	/* Timeout 30s */
10515 	spkt->satapkt_time = sata_default_pkt_time;
10516 	/* Synchronous mode, no callback, interrupts */
10517 	spkt->satapkt_op_mode =
10518 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10519 	spkt->satapkt_comp = NULL;
10520 	scmd = &spkt->satapkt_cmd;
10521 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10522 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10523 	scmd->satacmd_addr_type = 0;
10524 	scmd->satacmd_device_reg = 0;
10525 	scmd->satacmd_status_reg = 0;
10526 	scmd->satacmd_error_reg = 0;
10527 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10528 	scmd->satacmd_features_reg = cache_op;
10529 
10530 	/* Transfer command to HBA */
10531 	if (((*SATA_START_FUNC(sata_hba_inst))(
10532 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
10533 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
10534 		/* Pkt execution failed */
10535 		switch (cache_op) {
10536 		case SATAC_SF_ENABLE_READ_AHEAD:
10537 			infop = "enabling read ahead failed";
10538 			break;
10539 		case SATAC_SF_DISABLE_READ_AHEAD:
10540 			infop = "disabling read ahead failed";
10541 			break;
10542 		case SATAC_SF_ENABLE_WRITE_CACHE:
10543 			infop = "enabling write cache failed";
10544 			break;
10545 		case SATAC_SF_DISABLE_WRITE_CACHE:
10546 			infop = "disabling write cache failed";
10547 			break;
10548 		}
10549 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
10550 		rval = SATA_FAILURE;
10551 	}
10552 failure:
10553 	/* Free allocated resources */
10554 	if (spkt != NULL)
10555 		sata_pkt_free(spx);
10556 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10557 	return (rval);
10558 }
10559 
10560 /*
10561  * Set Removable Media Status Notification (enable/disable)
10562  * state == 0 , disable
10563  * state != 0 , enable
10564  *
10565  * If operation fails, system log messgage is emitted.
10566  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
10567  */
10568 
10569 static int
10570 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
10571     int state)
10572 {
10573 	sata_pkt_t *spkt;
10574 	sata_cmd_t *scmd;
10575 	sata_pkt_txlate_t *spx;
10576 	int rval = SATA_SUCCESS;
10577 	char *infop;
10578 
10579 	ASSERT(sdinfo != NULL);
10580 	ASSERT(sata_hba_inst != NULL);
10581 
10582 	/* Prepare packet for SET FEATURES COMMAND */
10583 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10584 	spx->txlt_sata_hba_inst = sata_hba_inst;
10585 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10586 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10587 	if (spkt == NULL) {
10588 		rval = SATA_FAILURE;
10589 		goto failure;
10590 	}
10591 	/* Fill sata_pkt */
10592 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10593 	/* Timeout 30s */
10594 	spkt->satapkt_time = sata_default_pkt_time;
10595 	/* Synchronous mode, no callback, interrupts */
10596 	spkt->satapkt_op_mode =
10597 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10598 	spkt->satapkt_comp = NULL;
10599 	scmd = &spkt->satapkt_cmd;
10600 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10601 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10602 	scmd->satacmd_addr_type = 0;
10603 	scmd->satacmd_device_reg = 0;
10604 	scmd->satacmd_status_reg = 0;
10605 	scmd->satacmd_error_reg = 0;
10606 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10607 	if (state == 0)
10608 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN;
10609 	else
10610 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN;
10611 
10612 	/* Transfer command to HBA */
10613 	if (((*SATA_START_FUNC(sata_hba_inst))(
10614 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
10615 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
10616 		/* Pkt execution failed */
10617 		if (state == 0)
10618 			infop = "disabling Removable Media Status "
10619 			    "Notification failed";
10620 		else
10621 			infop = "enabling Removable Media Status "
10622 			    "Notification failed";
10623 
10624 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
10625 		rval = SATA_FAILURE;
10626 	}
10627 failure:
10628 	/* Free allocated resources */
10629 	if (spkt != NULL)
10630 		sata_pkt_free(spx);
10631 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10632 	return (rval);
10633 }
10634 
10635 
10636 /*
10637  * Update port SCR block
10638  */
10639 static void
10640 sata_update_port_scr(sata_port_scr_t *port_scr, sata_device_t *device)
10641 {
10642 	port_scr->sstatus = device->satadev_scr.sstatus;
10643 	port_scr->serror = device->satadev_scr.serror;
10644 	port_scr->scontrol = device->satadev_scr.scontrol;
10645 	port_scr->sactive = device->satadev_scr.sactive;
10646 	port_scr->snotific = device->satadev_scr.snotific;
10647 }
10648 
10649 /*
10650  * Update state and copy port ss* values from passed sata_device structure.
10651  * sata_address is validated - if not valid, nothing is changed in sata_scsi
10652  * configuration struct.
10653  *
10654  * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function
10655  * regardless of the state in device argument.
10656  *
10657  * Port mutex should be held while calling this function.
10658  */
10659 static void
10660 sata_update_port_info(sata_hba_inst_t *sata_hba_inst,
10661 	sata_device_t *sata_device)
10662 {
10663 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst,
10664 	    sata_device->satadev_addr.cport)));
10665 
10666 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT ||
10667 	    sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
10668 
10669 		sata_cport_info_t *cportinfo;
10670 
10671 		if (SATA_NUM_CPORTS(sata_hba_inst) <=
10672 		    sata_device->satadev_addr.cport)
10673 			return;
10674 
10675 		cportinfo = SATA_CPORT_INFO(sata_hba_inst,
10676 		    sata_device->satadev_addr.cport);
10677 		sata_update_port_scr(&cportinfo->cport_scr, sata_device);
10678 
10679 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
10680 		cportinfo->cport_state &= ~(SATA_PSTATE_PWRON |
10681 		    SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
10682 		cportinfo->cport_state |=
10683 		    sata_device->satadev_state & SATA_PSTATE_VALID;
10684 	} else {
10685 		sata_pmport_info_t *pmportinfo;
10686 
10687 		if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT) ||
10688 		    (sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) ||
10689 		    SATA_NUM_PMPORTS(sata_hba_inst,
10690 		    sata_device->satadev_addr.cport) <
10691 		    sata_device->satadev_addr.pmport)
10692 			return;
10693 
10694 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
10695 		    sata_device->satadev_addr.cport,
10696 		    sata_device->satadev_addr.pmport);
10697 		sata_update_port_scr(&pmportinfo->pmport_scr, sata_device);
10698 
10699 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
10700 		pmportinfo->pmport_state &=
10701 		    ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF |
10702 		    SATA_PSTATE_FAILED);
10703 		pmportinfo->pmport_state |=
10704 		    sata_device->satadev_state & SATA_PSTATE_VALID;
10705 	}
10706 }
10707 
10708 
10709 
10710 /*
10711  * Extract SATA port specification from an IOCTL argument.
10712  *
10713  * This function return the port the user land send us as is, unless it
10714  * cannot retrieve port spec, then -1 is returned.
10715  *
10716  * Note: Only cport  - no port multiplier port.
10717  */
10718 static int32_t
10719 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp)
10720 {
10721 	int32_t port;
10722 
10723 	/* Extract port number from nvpair in dca structure  */
10724 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
10725 		SATA_LOG_D((sata_hba_inst, CE_NOTE,
10726 		    "sata_get_port_num: invalid port spec 0x%x in ioctl",
10727 		    port));
10728 		port = -1;
10729 	}
10730 
10731 	return (port);
10732 }
10733 
10734 /*
10735  * Get dev_info_t pointer to the device node pointed to by port argument.
10736  * NOTE: target argument is a value used in ioctls to identify
10737  * the AP - it is not a sata_address.
10738  * It is a combination of cport, pmport and address qualifier, encodded same
10739  * way as a scsi target number.
10740  * At this moment it carries only cport number.
10741  *
10742  * No PMult hotplug support.
10743  *
10744  * Returns dev_info_t pointer if target device was found, NULL otherwise.
10745  */
10746 
10747 static dev_info_t *
10748 sata_get_target_dip(dev_info_t *dip, int32_t port)
10749 {
10750 	dev_info_t	*cdip = NULL;
10751 	int		target, tgt;
10752 	int		ncport;
10753 	int 		circ;
10754 
10755 	ncport = port & SATA_CFGA_CPORT_MASK;
10756 	target = SATA_TO_SCSI_TARGET(ncport, 0, SATA_ADDR_DCPORT);
10757 
10758 	ndi_devi_enter(dip, &circ);
10759 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
10760 		dev_info_t *next = ddi_get_next_sibling(cdip);
10761 
10762 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
10763 		    DDI_PROP_DONTPASS, "target", -1);
10764 		if (tgt == -1) {
10765 			/*
10766 			 * This is actually an error condition, but not
10767 			 * a fatal one. Just continue the search.
10768 			 */
10769 			cdip = next;
10770 			continue;
10771 		}
10772 
10773 		if (tgt == target)
10774 			break;
10775 
10776 		cdip = next;
10777 	}
10778 	ndi_devi_exit(dip, circ);
10779 
10780 	return (cdip);
10781 }
10782 
10783 /*
10784  * Get dev_info_t pointer to the device node pointed to by port argument.
10785  * NOTE: target argument is a value used in ioctls to identify
10786  * the AP - it is not a sata_address.
10787  * It is a combination of cport, pmport and address qualifier, encoded same
10788  * way as a scsi target number.
10789  * At this moment it carries only cport number.
10790  *
10791  * No PMult hotplug support.
10792  *
10793  * Returns dev_info_t pointer if target device was found, NULL otherwise.
10794  */
10795 
10796 static dev_info_t *
10797 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr)
10798 {
10799 	dev_info_t	*cdip = NULL;
10800 	int		target, tgt;
10801 	int 		circ;
10802 
10803 	target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual);
10804 
10805 	ndi_devi_enter(dip, &circ);
10806 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
10807 		dev_info_t *next = ddi_get_next_sibling(cdip);
10808 
10809 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
10810 		    DDI_PROP_DONTPASS, "target", -1);
10811 		if (tgt == -1) {
10812 			/*
10813 			 * This is actually an error condition, but not
10814 			 * a fatal one. Just continue the search.
10815 			 */
10816 			cdip = next;
10817 			continue;
10818 		}
10819 
10820 		if (tgt == target)
10821 			break;
10822 
10823 		cdip = next;
10824 	}
10825 	ndi_devi_exit(dip, circ);
10826 
10827 	return (cdip);
10828 }
10829 
10830 /*
10831  * Process sata port disconnect request.
10832  * Normally, cfgadm sata plugin will try to offline (unconfigure) the device
10833  * before this request. Nevertheless, if a device is still configured,
10834  * we need to attempt to offline and unconfigure device.
10835  * Regardless of the unconfigure operation results the port is marked as
10836  * deactivated and no access to the attached device is possible.
10837  * If the target node remains because unconfigure operation failed, its state
10838  * will be set to DEVICE_REMOVED, preventing it to be used again when a device
10839  * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure
10840  * the device and remove old target node.
10841  *
10842  * This function invokes sata_hba_inst->satahba_tran->
10843  * sata_tran_hotplug_ops->sata_tran_port_deactivate().
10844  * If successful, the device structure (if any) attached to the specified port
10845  * is removed and state of the port marked appropriately.
10846  * Failure of the port_deactivate may keep port in the physically active state,
10847  * or may fail the port.
10848  *
10849  * NOTE: Port multiplier code is not completed nor tested.
10850  */
10851 
10852 static int
10853 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst,
10854     sata_device_t *sata_device)
10855 {
10856 	sata_drive_info_t *sdinfo = NULL;
10857 	sata_cport_info_t *cportinfo = NULL;
10858 	sata_pmport_info_t *pmportinfo = NULL;
10859 	sata_pmult_info_t *pmultinfo = NULL;
10860 	dev_info_t *tdip;
10861 	int cport, pmport, qual;
10862 	int rval = SATA_SUCCESS;
10863 	int rv = 0;
10864 
10865 	cport = sata_device->satadev_addr.cport;
10866 	pmport = sata_device->satadev_addr.pmport;
10867 	qual = sata_device->satadev_addr.qual;
10868 
10869 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
10870 
10871 	/*
10872 	 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran->
10873 	 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
10874 	 * Do the sanity check.
10875 	 */
10876 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
10877 		/* No physical port deactivation supported. */
10878 		return (EINVAL);
10879 	}
10880 
10881 	/* Check the current state of the port */
10882 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10883 	    (SATA_DIP(sata_hba_inst), sata_device);
10884 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10885 	sata_update_port_info(sata_hba_inst, sata_device);
10886 	if (rval != SATA_SUCCESS ||
10887 	    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
10888 		/* Device port status is unknown or it is in failed state */
10889 		if (qual == SATA_ADDR_PMPORT) {
10890 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
10891 			    SATA_PSTATE_FAILED;
10892 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
10893 			    "sata_hba_ioctl: connect: failed to deactivate "
10894 			    "SATA port %d", cport);
10895 		} else {
10896 			SATA_CPORT_STATE(sata_hba_inst, cport) =
10897 			    SATA_PSTATE_FAILED;
10898 			SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
10899 			    "sata_hba_ioctl: connect: failed to deactivate "
10900 			    "SATA port %d:%d", cport, pmport);
10901 		}
10902 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
10903 		    cport)->cport_mutex);
10904 		return (EIO);
10905 	}
10906 	/*
10907 	 * Set port's dev_state to not ready - this will disable
10908 	 * an access to a potentially attached device.
10909 	 */
10910 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
10911 	if (qual == SATA_ADDR_PMPORT) {
10912 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
10913 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
10914 			sdinfo = pmportinfo->pmport_sata_drive;
10915 			ASSERT(sdinfo != NULL);
10916 		}
10917 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
10918 	} else {
10919 		/* Assuming cport */
10920 
10921 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
10922 			if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
10923 				pmultinfo =
10924 				    cportinfo->cport_devp.cport_sata_pmult;
10925 				ASSERT(pmultinfo != NULL);
10926 			} else {
10927 				sdinfo = cportinfo->cport_devp.cport_sata_drive;
10928 			}
10929 		}
10930 		cportinfo->cport_state &= ~SATA_STATE_READY;
10931 	}
10932 	if (sdinfo != NULL) {
10933 		if ((sdinfo->satadrv_type & (SATA_VALID_DEV_TYPE)) != 0) {
10934 			/*
10935 			 * If a target node exists, try to offline
10936 			 * a device and remove target node.
10937 			 */
10938 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
10939 			    cport)->cport_mutex);
10940 			/* We are addressing attached device, not a port */
10941 			sata_device->satadev_addr.qual =
10942 			    sdinfo->satadrv_addr.qual;
10943 			tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
10944 			    &sata_device->satadev_addr);
10945 			if (tdip != NULL && ndi_devi_offline(tdip,
10946 			    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
10947 				/*
10948 				 * Problem
10949 				 * The target node remained attached.
10950 				 * This happens when the device file was open
10951 				 * or a node was waiting for resources.
10952 				 * Cannot do anything about it.
10953 				 */
10954 				if (qual == SATA_ADDR_CPORT) {
10955 					SATA_LOG_D((sata_hba_inst, CE_WARN,
10956 					    "sata_hba_ioctl: disconnect: could "
10957 					    "not unconfigure device before "
10958 					    "disconnecting the SATA port %d",
10959 					    cport));
10960 				} else {
10961 					SATA_LOG_D((sata_hba_inst, CE_WARN,
10962 					    "sata_hba_ioctl: disconnect: could "
10963 					    "not unconfigure device before "
10964 					    "disconnecting the SATA port %d:%d",
10965 					    cport, pmport));
10966 				}
10967 				/*
10968 				 * Set DEVICE REMOVED state in the target
10969 				 * node. It will prevent access to the device
10970 				 * even when a new device is attached, until
10971 				 * the old target node is released, removed and
10972 				 * recreated for a new  device.
10973 				 */
10974 				sata_set_device_removed(tdip);
10975 
10976 				/*
10977 				 * Instruct event daemon to try the target
10978 				 * node cleanup later.
10979 				 */
10980 				sata_set_target_node_cleanup(
10981 				    sata_hba_inst, &sata_device->satadev_addr);
10982 			}
10983 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
10984 			    cport)->cport_mutex);
10985 		}
10986 
10987 		/* Remove and release sata_drive info structure. */
10988 		if (pmportinfo != NULL) {
10989 			SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport) =
10990 			    NULL;
10991 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
10992 		} else {
10993 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
10994 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
10995 		}
10996 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
10997 	}
10998 #if 0
10999 	else if (pmultinfo != NULL) {
11000 		/*
11001 		 * Port Multiplier itself needs special handling.
11002 		 * All device ports need to be processed here!
11003 		 */
11004 	}
11005 #endif
11006 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11007 	/* Just ask HBA driver to deactivate port */
11008 	/*	sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; */
11009 
11010 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
11011 	    (SATA_DIP(sata_hba_inst), sata_device);
11012 
11013 	/*
11014 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11015 	 * without the hint (to force listener to investivate the state).
11016 	 */
11017 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11018 	    SE_NO_HINT);
11019 
11020 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11021 	sata_update_port_info(sata_hba_inst, sata_device);
11022 
11023 	if (rval != SATA_SUCCESS) {
11024 		/*
11025 		 * Port deactivation failure - do not
11026 		 * change port state unless the state
11027 		 * returned by HBA indicates a port failure.
11028 		 * NOTE: device structures were released, so devices now are
11029 		 * invisible! Port reset is needed to re-enumerate devices.
11030 		 */
11031 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11032 			if (pmportinfo != NULL)
11033 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
11034 			else
11035 				cportinfo->cport_state = SATA_PSTATE_FAILED;
11036 			rv = EIO;
11037 		}
11038 	} else {
11039 		/*
11040 		 * Deactivation succeded. From now on the sata framework
11041 		 * will not care what is happening to the device, until
11042 		 * the port is activated again.
11043 		 */
11044 		cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
11045 	}
11046 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11047 	return (rv);
11048 }
11049 
11050 
11051 
11052 /*
11053  * Process sata port connect request
11054  * The sata cfgadm pluging will invoke this operation only if port was found
11055  * in the disconnect state (failed state is also treated as the disconnected
11056  * state).
11057  * DEVCTL_AP_CONNECT would invoke  sata_hba_inst->satahba_tran->
11058  * sata_tran_hotplug_ops->sata_tran_port_activate().
11059  * If successful and a device is found attached to the port,
11060  * the initialization sequence is executed to attach a device structure to
11061  * a port structure. The state of the port and a device would be set
11062  * appropriately.
11063  * The device is not set in configured state (system-wise) by this operation.
11064  *
11065  * Note, that activating the port may generate link events,
11066  * so it is important that following processing and the
11067  * event processing does not interfere with each other!
11068  *
11069  * This operation may remove port failed state and will
11070  * try to make port active and in good standing.
11071  *
11072  * NOTE: Port multiplier code is not completed nor tested.
11073  */
11074 
11075 static int
11076 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst,
11077     sata_device_t *sata_device)
11078 {
11079 	int cport, pmport, qual;
11080 	int rv = 0;
11081 
11082 	cport = sata_device->satadev_addr.cport;
11083 	pmport = sata_device->satadev_addr.pmport;
11084 	qual = sata_device->satadev_addr.qual;
11085 
11086 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
11087 
11088 	/*
11089 	 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->
11090 	 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate().
11091 	 * Perform sanity check now.
11092 	 */
11093 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) {
11094 		/* No physical port activation supported. */
11095 		return (EINVAL);
11096 	}
11097 
11098 	/* Just ask HBA driver to activate port */
11099 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11100 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11101 		/*
11102 		 * Port activation failure.
11103 		 */
11104 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11105 		    cport)->cport_mutex);
11106 		sata_update_port_info(sata_hba_inst, sata_device);
11107 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11108 			if (qual == SATA_ADDR_DCPORT) {
11109 				SATA_CPORT_STATE(sata_hba_inst, cport) =
11110 				    SATA_PSTATE_FAILED;
11111 				SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
11112 				    "sata_hba_ioctl: connect: failed to "
11113 				    "activate SATA port %d", cport);
11114 			} else { /* port multiplier device port */
11115 				SATA_PMPORT_STATE(sata_hba_inst, cport,
11116 				    pmport) = SATA_PSTATE_FAILED;
11117 				SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
11118 				    "sata_hba_ioctl: connect: failed to "
11119 				    "activate SATA port %d:%d", cport, pmport);
11120 
11121 			}
11122 		}
11123 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11124 		    cport)->cport_mutex);
11125 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
11126 		    "sata_hba_ioctl: connect: failed to activate SATA "
11127 		    "port %d:%d", cport, pmport);
11128 		return (EIO);
11129 	}
11130 
11131 	/* Virgin port state - will be updated by the port re-probe. */
11132 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11133 	if (qual == SATA_ADDR_CPORT)
11134 		SATA_CPORT_STATE(sata_hba_inst, cport) = 0;
11135 	else /* port multiplier device port */
11136 		SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0;
11137 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11138 
11139 	/*
11140 	 * Probe the port to find its state and attached device.
11141 	 */
11142 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11143 	    SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE)
11144 		rv = EIO;
11145 
11146 	/*
11147 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11148 	 * without the hint
11149 	 */
11150 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11151 	    SE_NO_HINT);
11152 
11153 	/*
11154 	 * If there is a device attached to the port, emit
11155 	 * a message.
11156 	 */
11157 	if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11158 
11159 		if (qual == SATA_ADDR_CPORT) {
11160 			sata_log(sata_hba_inst, CE_WARN,
11161 			    "SATA device detected at port %d", cport);
11162 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11163 				/*
11164 				 * A device was not successfully identified
11165 				 */
11166 				sata_log(sata_hba_inst, CE_WARN,
11167 				    "Could not identify SATA "
11168 				    "device at port %d", cport);
11169 			}
11170 		} else { /* port multiplier device port */
11171 			sata_log(sata_hba_inst, CE_WARN,
11172 			    "SATA device detected at port %d:%d",
11173 			    cport, pmport);
11174 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11175 				/*
11176 				 * A device was not successfully identified
11177 				 */
11178 				sata_log(sata_hba_inst, CE_WARN,
11179 				    "Could not identify SATA "
11180 				    "device at port %d:%d", cport, pmport);
11181 			}
11182 		}
11183 	}
11184 
11185 	return (rv);
11186 }
11187 
11188 
11189 /*
11190  * Process sata device unconfigure request.
11191  * The unconfigure operation uses generic nexus operation to
11192  * offline a device. It leaves a target device node attached.
11193  * and obviously sata_drive_info attached as well, because
11194  * from the hardware point of view nothing has changed.
11195  */
11196 static int
11197 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst,
11198     sata_device_t *sata_device)
11199 {
11200 	int rv = 0;
11201 	dev_info_t *tdip;
11202 
11203 	/* We are addressing attached device, not a port */
11204 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
11205 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11206 	else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT)
11207 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11208 
11209 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11210 	    &sata_device->satadev_addr)) != NULL) {
11211 
11212 		if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) {
11213 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11214 			    "sata_hba_ioctl: unconfigure: "
11215 			    "failed to unconfigure device at SATA port %d:%d",
11216 			    sata_device->satadev_addr.cport,
11217 			    sata_device->satadev_addr.pmport));
11218 			rv = EIO;
11219 		}
11220 		/*
11221 		 * The target node devi_state should be marked with
11222 		 * DEVI_DEVICE_OFFLINE by ndi_devi_offline().
11223 		 * This would be the indication for cfgadm that
11224 		 * the AP node occupant state is 'unconfigured'.
11225 		 */
11226 
11227 	} else {
11228 		/*
11229 		 * This would indicate a failure on the part of cfgadm
11230 		 * to detect correct state of the node prior to this
11231 		 * call - one cannot unconfigure non-existing device.
11232 		 */
11233 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11234 		    "sata_hba_ioctl: unconfigure: "
11235 		    "attempt to unconfigure non-existing device "
11236 		    "at SATA port %d:%d",
11237 		    sata_device->satadev_addr.cport,
11238 		    sata_device->satadev_addr.pmport));
11239 		rv = ENXIO;
11240 	}
11241 	return (rv);
11242 }
11243 
11244 /*
11245  * Process sata device configure request
11246  * If port is in a failed state, operation is aborted - one has to use
11247  * an explicit connect or port activate request to try to get a port into
11248  * non-failed mode. Port reset wil also work in such situation.
11249  * If the port is in disconnected (shutdown) state, the connect operation is
11250  * attempted prior to any other action.
11251  * When port is in the active state, there is a device attached and the target
11252  * node exists, a device was most likely offlined.
11253  * If target node does not exist, a new target node is created. In both cases
11254  * an attempt is made to online (configure) the device.
11255  *
11256  * NOTE: Port multiplier code is not completed nor tested.
11257  */
11258 static int
11259 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst,
11260     sata_device_t *sata_device)
11261 {
11262 	int cport, pmport, qual;
11263 	int rval;
11264 	boolean_t target = TRUE;
11265 	sata_cport_info_t *cportinfo;
11266 	sata_pmport_info_t *pmportinfo = NULL;
11267 	dev_info_t *tdip;
11268 	sata_drive_info_t *sdinfo;
11269 
11270 	cport = sata_device->satadev_addr.cport;
11271 	pmport = sata_device->satadev_addr.pmport;
11272 	qual = sata_device->satadev_addr.qual;
11273 
11274 	/* Get current port state */
11275 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11276 	    (SATA_DIP(sata_hba_inst), sata_device);
11277 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11278 	sata_update_port_info(sata_hba_inst, sata_device);
11279 
11280 	if (rval != SATA_SUCCESS ||
11281 	    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
11282 		/*
11283 		 * Obviously, device on a failed port is not visible
11284 		 */
11285 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11286 		return (ENXIO);
11287 	}
11288 
11289 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11290 	if (qual == SATA_ADDR_PMPORT)
11291 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11292 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11293 
11294 	if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) {
11295 		/* need to activate port */
11296 		target = FALSE;
11297 
11298 		/* Sanity check */
11299 		if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
11300 			return (ENXIO);
11301 
11302 		/* Just let HBA driver to activate port */
11303 		if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11304 		    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11305 			/*
11306 			 * Port activation failure - do not change port state
11307 			 * unless the state returned by HBA indicates a port
11308 			 * failure.
11309 			 */
11310 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11311 			    cport)->cport_mutex);
11312 			sata_update_port_info(sata_hba_inst, sata_device);
11313 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11314 				if (qual == SATA_ADDR_PMPORT)
11315 					pmportinfo->pmport_state =
11316 					    SATA_PSTATE_FAILED;
11317 				else
11318 					cportinfo->cport_state =
11319 					    SATA_PSTATE_FAILED;
11320 			}
11321 			mutex_exit(&SATA_CPORT_INFO(
11322 			    sata_hba_inst, cport)->cport_mutex);
11323 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11324 			    "sata_hba_ioctl: configure: "
11325 			    "failed to activate SATA port %d:%d",
11326 			    cport, pmport));
11327 			return (EIO);
11328 		}
11329 		/*
11330 		 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11331 		 * without the hint.
11332 		 */
11333 		sata_gen_sysevent(sata_hba_inst,
11334 		    &sata_device->satadev_addr, SE_NO_HINT);
11335 
11336 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11337 		    cport_mutex);
11338 		/* Virgin port state */
11339 		if (qual == SATA_ADDR_PMPORT)
11340 			pmportinfo->pmport_state = 0;
11341 		else
11342 			cportinfo->cport_state = 0;
11343 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11344 	}
11345 	/*
11346 	 * Always reprobe port, to get current device info.
11347 	 */
11348 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11349 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
11350 		return (EIO);
11351 
11352 	if (sata_device->satadev_type != SATA_DTYPE_NONE && target == FALSE) {
11353 		if (qual == SATA_ADDR_PMPORT) {
11354 			/*
11355 			 * That's the transition from "inactive" port
11356 			 * to active one with device attached.
11357 			 */
11358 			sata_log(sata_hba_inst, CE_WARN,
11359 			    "SATA device detected at port %d:%d",
11360 			    cport, pmport);
11361 		} else {
11362 			/*
11363 			 * When PM is attached to the cport and cport is
11364 			 * activated, every PM device port needs to be reprobed.
11365 			 * We need to emit message for all devices detected
11366 			 * at port multiplier's device ports.
11367 			 * Add such code here.
11368 			 * For now, just inform about device attached to
11369 			 * cport.
11370 			 */
11371 			sata_log(sata_hba_inst, CE_WARN,
11372 			    "SATA device detected at port %d", cport);
11373 		}
11374 	}
11375 
11376 	/*
11377 	 * This is where real configuration operation starts.
11378 	 *
11379 	 * When PM is attached to the cport and cport is activated,
11380 	 * devices attached PM device ports may have to be configured
11381 	 * explicitly. This may change when port multiplier is supported.
11382 	 * For now, configure only disks and other valid target devices.
11383 	 */
11384 	if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) {
11385 		if (qual == SATA_ADDR_CPORT) {
11386 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11387 				/*
11388 				 * A device was not successfully identified
11389 				 */
11390 				sata_log(sata_hba_inst, CE_WARN,
11391 				    "Could not identify SATA "
11392 				    "device at port %d", cport);
11393 			}
11394 		} else { /* port multiplier device port */
11395 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11396 				/*
11397 				 * A device was not successfully identified
11398 				 */
11399 				sata_log(sata_hba_inst, CE_WARN,
11400 				    "Could not identify SATA "
11401 				    "device at port %d:%d", cport, pmport);
11402 			}
11403 		}
11404 		return (ENXIO);		/* No device to configure */
11405 	}
11406 
11407 	/*
11408 	 * Here we may have a device in reset condition,
11409 	 * but because we are just configuring it, there is
11410 	 * no need to process the reset other than just
11411 	 * to clear device reset condition in the HBA driver.
11412 	 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will
11413 	 * cause a first command sent the HBA driver with the request
11414 	 * to clear device reset condition.
11415 	 */
11416 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11417 	if (qual == SATA_ADDR_PMPORT)
11418 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11419 	else
11420 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11421 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
11422 	if (sdinfo == NULL) {
11423 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11424 		return (ENXIO);
11425 	}
11426 	if (sdinfo->satadrv_event_flags &
11427 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
11428 		sdinfo->satadrv_event_flags = 0;
11429 	}
11430 	sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
11431 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11432 
11433 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11434 	    &sata_device->satadev_addr)) != NULL) {
11435 		/*
11436 		 * Target node exists. Verify, that it belongs
11437 		 * to existing, attached device and not to
11438 		 * a removed device.
11439 		 */
11440 		if (sata_check_device_removed(tdip) == B_TRUE) {
11441 			if (qual == SATA_ADDR_DPMPORT)
11442 				sata_log(sata_hba_inst, CE_WARN,
11443 				    "SATA device at port %d cannot be "
11444 				    "configured. "
11445 				    "Application(s) accessing "
11446 				    "previously attached device "
11447 				    "have to release it before newly "
11448 				    "inserted device can be made accessible.",
11449 				    cport);
11450 			else
11451 				sata_log(sata_hba_inst, CE_WARN,
11452 				    "SATA device at port %d:%d cannot be"
11453 				    "configured. "
11454 				    "Application(s) accessing "
11455 				    "previously attached device "
11456 				    "have to release it before newly "
11457 				    "inserted device can be made accessible.",
11458 				    cport, pmport);
11459 			return (EIO);
11460 		}
11461 		/*
11462 		 * Device was not removed and re-inserted.
11463 		 * Try to online it.
11464 		 */
11465 		if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) {
11466 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11467 			    "sata_hba_ioctl: configure: "
11468 			    "onlining device at SATA port "
11469 			    "%d:%d failed", cport, pmport));
11470 			return (EIO);
11471 		}
11472 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11473 		    cport)->cport_mutex);
11474 
11475 		if (qual == SATA_ADDR_DPMPORT)
11476 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
11477 		else
11478 			cportinfo-> cport_tgtnode_clean = B_TRUE;
11479 
11480 		mutex_exit(&SATA_CPORT_INFO(
11481 		    sata_hba_inst, cport)->cport_mutex);
11482 	} else {
11483 		/*
11484 		 * No target node - need to create a new target node.
11485 		 */
11486 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11487 		    cport_mutex);
11488 		if (qual == SATA_ADDR_DPMPORT)
11489 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
11490 		else
11491 			cportinfo-> cport_tgtnode_clean = B_TRUE;
11492 
11493 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11494 		    cport_mutex);
11495 		tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
11496 		    sata_hba_inst, &sata_device->satadev_addr);
11497 		if (tdip == NULL) {
11498 			/* Configure operation failed */
11499 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11500 			    "sata_hba_ioctl: configure: "
11501 			    "configuring SATA device at port %d:%d "
11502 			    "failed", cport, pmport));
11503 			return (EIO);
11504 		}
11505 	}
11506 	return (0);
11507 }
11508 
11509 
11510 /*
11511  * Process ioctl deactivate port request.
11512  * Arbitrarily unconfigure attached device, if any.
11513  * Even if the unconfigure fails, proceed with the
11514  * port deactivation.
11515  *
11516  * NOTE: Port Multiplier code is not completed and tested.
11517  */
11518 
11519 static int
11520 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst,
11521     sata_device_t *sata_device)
11522 {
11523 	int cport, pmport, qual;
11524 	int rval, rv = 0;
11525 	sata_cport_info_t *cportinfo;
11526 	sata_pmport_info_t *pmportinfo = NULL;
11527 	dev_info_t *tdip;
11528 	sata_drive_info_t *sdinfo = NULL;
11529 
11530 	/* Sanity check */
11531 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL)
11532 		return (ENOTSUP);
11533 
11534 	cport = sata_device->satadev_addr.cport;
11535 	pmport = sata_device->satadev_addr.pmport;
11536 	qual = sata_device->satadev_addr.qual;
11537 
11538 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11539 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11540 	if (qual == SATA_ADDR_CPORT) {
11541 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11542 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
11543 			/*
11544 			 * For now, assume that port multiplier is not
11545 			 * supported, i.e. deal only with valid devices
11546 			 */
11547 			if ((cportinfo->cport_dev_type &
11548 			    SATA_VALID_DEV_TYPE) != 0)
11549 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11550 			/*
11551 			 * If attached device is a port multiplier, we will
11552 			 * have to unconfigure all devices attached to the
11553 			 * port multiplier. Add this code here.
11554 			 */
11555 		}
11556 		cportinfo->cport_state &= ~SATA_STATE_READY;
11557 	} else {
11558 		/* Port multiplier device port */
11559 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11560 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11561 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
11562 		    (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0)
11563 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11564 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
11565 	}
11566 
11567 	if (sdinfo != NULL) {
11568 		/*
11569 		 * If a target node exists, try to offline a device and
11570 		 * to remove a target node.
11571 		 */
11572 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11573 		    cport_mutex);
11574 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11575 		    &sata_device->satadev_addr);
11576 		if (tdip != NULL) {
11577 			/* target node exist */
11578 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
11579 			    "sata_hba_ioctl: port deactivate: "
11580 			    "target node exists.", NULL);
11581 
11582 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) !=
11583 			    NDI_SUCCESS) {
11584 				SATA_LOG_D((sata_hba_inst, CE_WARN,
11585 				    "sata_hba_ioctl: port deactivate: "
11586 				    "failed to unconfigure device at port "
11587 				    "%d:%d before deactivating the port",
11588 				    cport, pmport));
11589 				/*
11590 				 * Set DEVICE REMOVED state in the target
11591 				 * node. It will prevent an access to
11592 				 * the device even when a new device is
11593 				 * attached, until the old target node is
11594 				 * released, removed and recreated for a new
11595 				 * device.
11596 				 */
11597 				sata_set_device_removed(tdip);
11598 
11599 				/*
11600 				 * Instruct the event daemon to try the
11601 				 * target node cleanup later.
11602 				 */
11603 				sata_set_target_node_cleanup(sata_hba_inst,
11604 				    &sata_device->satadev_addr);
11605 			}
11606 		}
11607 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11608 		    cport_mutex);
11609 		/*
11610 		 * In any case, remove and release sata_drive_info
11611 		 * structure.
11612 		 */
11613 		if (qual == SATA_ADDR_CPORT) {
11614 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11615 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11616 		} else { /* port multiplier device port */
11617 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11618 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11619 		}
11620 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
11621 	}
11622 	if (qual == SATA_ADDR_CPORT) {
11623 		cportinfo->cport_state &= ~(SATA_STATE_PROBED |
11624 		    SATA_STATE_PROBING);
11625 	} else { /* port multiplier device port */
11626 		pmportinfo->pmport_state &= ~(SATA_STATE_PROBED |
11627 		    SATA_STATE_PROBING);
11628 	}
11629 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11630 
11631 	/* Just let HBA driver to deactivate port */
11632 	sata_device->satadev_addr.qual = qual;
11633 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
11634 	    (SATA_DIP(sata_hba_inst), sata_device);
11635 
11636 	/*
11637 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11638 	 * without the hint
11639 	 */
11640 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11641 	    SE_NO_HINT);
11642 
11643 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11644 	sata_update_port_info(sata_hba_inst, sata_device);
11645 	if (qual == SATA_ADDR_CPORT) {
11646 		if (rval != SATA_SUCCESS) {
11647 			/*
11648 			 * Port deactivation failure - do not change port state
11649 			 * unless the state returned by HBA indicates a port
11650 			 * failure.
11651 			 */
11652 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11653 				SATA_CPORT_STATE(sata_hba_inst, cport) =
11654 				    SATA_PSTATE_FAILED;
11655 			}
11656 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11657 			    "sata_hba_ioctl: port deactivate: "
11658 			    "cannot deactivate SATA port %d", cport));
11659 			rv = EIO;
11660 		} else {
11661 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
11662 		}
11663 	} else {
11664 		if (rval != SATA_SUCCESS) {
11665 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11666 				SATA_PMPORT_STATE(sata_hba_inst, cport,
11667 				    pmport) = SATA_PSTATE_FAILED;
11668 			}
11669 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11670 			    "sata_hba_ioctl: port deactivate: "
11671 			    "cannot deactivate SATA port %d:%d",
11672 			    cport, pmport));
11673 			rv = EIO;
11674 		} else {
11675 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
11676 		}
11677 	}
11678 
11679 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11680 
11681 	return (rv);
11682 }
11683 
11684 /*
11685  * Process ioctl port activate request.
11686  *
11687  * NOTE: Port multiplier code is not completed nor tested.
11688  */
11689 static int
11690 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst,
11691     sata_device_t *sata_device)
11692 {
11693 	int cport, pmport, qual;
11694 	sata_cport_info_t *cportinfo;
11695 	sata_pmport_info_t *pmportinfo = NULL;
11696 	boolean_t dev_existed = TRUE;
11697 
11698 	/* Sanity check */
11699 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
11700 		return (ENOTSUP);
11701 
11702 	cport = sata_device->satadev_addr.cport;
11703 	pmport = sata_device->satadev_addr.pmport;
11704 	qual = sata_device->satadev_addr.qual;
11705 
11706 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11707 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11708 	if (qual == SATA_ADDR_PMPORT) {
11709 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11710 		if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN ||
11711 		    pmportinfo->pmport_dev_type == SATA_DTYPE_NONE)
11712 			dev_existed = FALSE;
11713 	} else { /* cport */
11714 		if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN ||
11715 		    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
11716 			dev_existed = FALSE;
11717 	}
11718 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11719 
11720 	/* Just let HBA driver to activate port, if necessary */
11721 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11722 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11723 		/*
11724 		 * Port activation failure - do not change port state unless
11725 		 * the state returned by HBA indicates a port failure.
11726 		 */
11727 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11728 		    cport)->cport_mutex);
11729 		sata_update_port_info(sata_hba_inst, sata_device);
11730 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11731 			if (qual == SATA_ADDR_PMPORT)
11732 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
11733 			else
11734 				cportinfo->cport_state = SATA_PSTATE_FAILED;
11735 
11736 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11737 			    cport)->cport_mutex);
11738 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11739 			    "sata_hba_ioctl: port activate: cannot activate "
11740 			    "SATA port %d:%d", cport, pmport));
11741 			return (EIO);
11742 		}
11743 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11744 	}
11745 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11746 	if (qual == SATA_ADDR_PMPORT)
11747 		pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN;
11748 	else
11749 		cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN;
11750 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11751 
11752 	/*
11753 	 * Re-probe port to find its current state and possibly attached device.
11754 	 * Port re-probing may change the cportinfo device type if device is
11755 	 * found attached.
11756 	 * If port probing failed, the device type would be set to
11757 	 * SATA_DTYPE_NONE.
11758 	 */
11759 	(void) sata_reprobe_port(sata_hba_inst, sata_device,
11760 	    SATA_DEV_IDENTIFY_RETRY);
11761 
11762 	/*
11763 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11764 	 * without the hint.
11765 	 */
11766 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11767 	    SE_NO_HINT);
11768 
11769 	if (dev_existed == FALSE) {
11770 		if (qual == SATA_ADDR_PMPORT &&
11771 		    pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
11772 			/*
11773 			 * That's the transition from the "inactive" port state
11774 			 * or the active port without a device attached to the
11775 			 * active port state with a device attached.
11776 			 */
11777 			sata_log(sata_hba_inst, CE_WARN,
11778 			    "SATA device detected at port %d:%d",
11779 			    cport, pmport);
11780 		} else if (qual == SATA_ADDR_CPORT &&
11781 		    cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
11782 			/*
11783 			 * That's the transition from the "inactive" port state
11784 			 * or the active port without a device attached to the
11785 			 * active port state with a device attached.
11786 			 */
11787 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
11788 				sata_log(sata_hba_inst, CE_WARN,
11789 				    "SATA device detected at port %d", cport);
11790 			} else {
11791 				sata_log(sata_hba_inst, CE_WARN,
11792 				    "SATA port multiplier detected at port %d",
11793 				    cport);
11794 				/*
11795 				 * Because the detected device is a port
11796 				 * multiplier, we need to reprobe every device
11797 				 * port on the port multiplier and show every
11798 				 * device found attached.
11799 				 * Add this code here.
11800 				 */
11801 			}
11802 		}
11803 	}
11804 	return (0);
11805 }
11806 
11807 
11808 
11809 /*
11810  * Process ioctl reset port request.
11811  *
11812  * NOTE: Port multiplier code is not completed nor tested.
11813  */
11814 static int
11815 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst,
11816     sata_device_t *sata_device)
11817 {
11818 	int cport, pmport, qual;
11819 	int rv = 0;
11820 
11821 	cport = sata_device->satadev_addr.cport;
11822 	pmport = sata_device->satadev_addr.pmport;
11823 	qual = sata_device->satadev_addr.qual;
11824 
11825 	/* Sanity check */
11826 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
11827 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11828 		    "sata_hba_ioctl: sata_hba_tran missing required "
11829 		    "function sata_tran_reset_dport"));
11830 		return (ENOTSUP);
11831 	}
11832 
11833 	/* Ask HBA to reset port */
11834 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
11835 	    sata_device) != SATA_SUCCESS) {
11836 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11837 		    "sata_hba_ioctl: reset port: failed %d:%d",
11838 		    cport, pmport));
11839 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11840 		    cport_mutex);
11841 		sata_update_port_info(sata_hba_inst, sata_device);
11842 		if (qual == SATA_ADDR_CPORT)
11843 			SATA_CPORT_STATE(sata_hba_inst, cport) =
11844 			    SATA_PSTATE_FAILED;
11845 		else
11846 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
11847 			    SATA_PSTATE_FAILED;
11848 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11849 		    cport_mutex);
11850 		rv = EIO;
11851 	}
11852 	/*
11853 	 * Beacuse the port was reset, it should be probed and
11854 	 * attached device reinitialized. At this point the
11855 	 * port state is unknown - it's state is HBA-specific.
11856 	 * Re-probe port to get its state.
11857 	 */
11858 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11859 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) {
11860 		rv = EIO;
11861 	}
11862 	return (rv);
11863 }
11864 
11865 /*
11866  * Process ioctl reset device request.
11867  *
11868  * NOTE: Port multiplier code is not completed nor tested.
11869  */
11870 static int
11871 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst,
11872     sata_device_t *sata_device)
11873 {
11874 	sata_drive_info_t *sdinfo;
11875 	int cport, pmport;
11876 	int rv = 0;
11877 
11878 	/* Sanity check */
11879 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
11880 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11881 		    "sata_hba_ioctl: sata_hba_tran missing required "
11882 		    "function sata_tran_reset_dport"));
11883 		return (ENOTSUP);
11884 	}
11885 
11886 	cport = sata_device->satadev_addr.cport;
11887 	pmport = sata_device->satadev_addr.pmport;
11888 
11889 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11890 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) {
11891 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11892 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
11893 		    sata_device->satadev_addr.cport);
11894 	} else { /* port multiplier */
11895 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11896 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
11897 		    sata_device->satadev_addr.cport,
11898 		    sata_device->satadev_addr.pmport);
11899 	}
11900 	if (sdinfo == NULL) {
11901 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11902 		return (EINVAL);
11903 	}
11904 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11905 
11906 	/* Ask HBA to reset device */
11907 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
11908 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11909 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11910 		    "sata_hba_ioctl: reset device: failed at port %d:%d",
11911 		    cport, pmport));
11912 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11913 		    cport_mutex);
11914 		sata_update_port_info(sata_hba_inst, sata_device);
11915 		/*
11916 		 * Device info structure remains attached. Another device reset
11917 		 * or port disconnect/connect and re-probing is
11918 		 * needed to change it's state
11919 		 */
11920 		sdinfo->satadrv_state &= ~SATA_STATE_READY;
11921 		sdinfo->satadrv_state |= SATA_DSTATE_FAILED;
11922 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11923 		rv = EIO;
11924 	}
11925 	/*
11926 	 * If attached device was a port multiplier, some extra processing
11927 	 * may be needed, to bring it back (if port re-probing did not handle
11928 	 * it). Add such code here.
11929 	 */
11930 	return (rv);
11931 }
11932 
11933 
11934 /*
11935  * Process ioctl reset all request.
11936  *
11937  * NOTE: Port multiplier code is not completed nor tested.
11938  */
11939 static int
11940 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst)
11941 {
11942 	sata_device_t sata_device;
11943 	int rv = 0;
11944 	int tcport;
11945 	int tpmport = 0;
11946 
11947 	sata_device.satadev_rev = SATA_DEVICE_REV;
11948 
11949 	/*
11950 	 * There is no protection here for configured devices.
11951 	 */
11952 	/* Sanity check */
11953 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
11954 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11955 		    "sata_hba_ioctl: sata_hba_tran missing required "
11956 		    "function sata_tran_reset_dport"));
11957 		return (ENOTSUP);
11958 	}
11959 
11960 	/*
11961 	 * Need to lock all ports, not just one.
11962 	 * If any port is locked by event processing, fail the whole operation.
11963 	 * One port is already locked, but for simplicity lock it again.
11964 	 */
11965 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
11966 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11967 		    cport_mutex);
11968 		if (((SATA_CPORT_INFO(sata_hba_inst, tcport)->
11969 		    cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) {
11970 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11971 			    cport_mutex);
11972 			rv = EBUSY;
11973 			break;
11974 		} else {
11975 			SATA_CPORT_INFO(sata_hba_inst, tcport)->
11976 			    cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
11977 			/*
11978 			 * If there is a port multiplier attached, we may need
11979 			 * to lock its port as well. If so, add such code here.
11980 			 */
11981 		}
11982 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11983 		    cport_mutex);
11984 	}
11985 
11986 	if (rv == 0) {
11987 		/*
11988 		 * All cports were successfully locked.
11989 		 * Reset main SATA controller only for now - no PMult.
11990 		 * Set the device address to port 0, to have a valid device
11991 		 * address.
11992 		 */
11993 		sata_device.satadev_addr.qual = SATA_ADDR_CNTRL;
11994 		sata_device.satadev_addr.cport = 0;
11995 		sata_device.satadev_addr.pmport = 0;
11996 
11997 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
11998 		    (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) {
11999 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12000 			    "sata_hba_ioctl: reset controller failed"));
12001 			return (EIO);
12002 		}
12003 		/*
12004 		 * Because ports were reset, port states are unknown.
12005 		 * They should be re-probed to get their state and
12006 		 * attached devices should be reinitialized.
12007 		 * Add code here to re-probe port multiplier device ports.
12008 		 */
12009 		for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst);
12010 		    tcport++) {
12011 			sata_device.satadev_addr.cport = tcport;
12012 			sata_device.satadev_addr.pmport = tpmport;
12013 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
12014 
12015 			if (sata_reprobe_port(sata_hba_inst, &sata_device,
12016 			    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
12017 				rv = EIO;
12018 		}
12019 	}
12020 	/*
12021 	 * Unlock all ports
12022 	 */
12023 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
12024 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
12025 		    cport_mutex);
12026 		SATA_CPORT_INFO(sata_hba_inst, tcport)->
12027 		    cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
12028 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
12029 		    cport_mutex);
12030 	}
12031 
12032 	/*
12033 	 * This operation returns EFAULT if either reset
12034 	 * controller failed or a re-probing of any port failed.
12035 	 */
12036 	return (rv);
12037 }
12038 
12039 
12040 /*
12041  * Process ioctl port self test request.
12042  *
12043  * NOTE: Port multiplier code is not completed nor tested.
12044  */
12045 static int
12046 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst,
12047     sata_device_t *sata_device)
12048 {
12049 	int cport, pmport, qual;
12050 	int rv = 0;
12051 
12052 	/* Sanity check */
12053 	if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL)
12054 		return (ENOTSUP);
12055 
12056 	cport = sata_device->satadev_addr.cport;
12057 	pmport = sata_device->satadev_addr.pmport;
12058 	qual = sata_device->satadev_addr.qual;
12059 
12060 	/*
12061 	 * There is no protection here for a configured
12062 	 * device attached to this port.
12063 	 */
12064 
12065 	if ((*SATA_SELFTEST_FUNC(sata_hba_inst))
12066 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
12067 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12068 		    "sata_hba_ioctl: port selftest: "
12069 		    "failed port %d:%d", cport, pmport));
12070 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12071 		    cport_mutex);
12072 		sata_update_port_info(sata_hba_inst, sata_device);
12073 		if (qual == SATA_ADDR_CPORT)
12074 			SATA_CPORT_STATE(sata_hba_inst, cport) =
12075 			    SATA_PSTATE_FAILED;
12076 		else /* port ultiplier device port */
12077 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
12078 			    SATA_PSTATE_FAILED;
12079 
12080 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12081 		    cport_mutex);
12082 		return (EIO);
12083 	}
12084 	/*
12085 	 * Beacuse the port was reset in the course of testing, it should be
12086 	 * re-probed and attached device state should be restored. At this
12087 	 * point the port state is unknown - it's state is HBA-specific.
12088 	 * Force port re-probing to get it into a known state.
12089 	 */
12090 	if (sata_reprobe_port(sata_hba_inst, sata_device,
12091 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
12092 		rv = EIO;
12093 	return (rv);
12094 }
12095 
12096 
12097 /*
12098  * sata_cfgadm_state:
12099  * Use the sata port state and state of the target node to figure out
12100  * the cfgadm_state.
12101  *
12102  * The port argument is a value with encoded cport,
12103  * pmport and address qualifier, in the same manner as a scsi target number.
12104  * SCSI_TO_SATA_CPORT macro extracts cport number,
12105  * SCSI_TO_SATA_PMPORT extracts pmport number and
12106  * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag.
12107  *
12108  * For now, support is for cports only - no port multiplier device ports.
12109  */
12110 
12111 static void
12112 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port,
12113     devctl_ap_state_t *ap_state)
12114 {
12115 	uint16_t	cport;
12116 	int		port_state;
12117 	sata_drive_info_t *sdinfo;
12118 
12119 	/* Cport only */
12120 	cport = SCSI_TO_SATA_CPORT(port);
12121 
12122 	port_state = SATA_CPORT_STATE(sata_hba_inst, cport);
12123 	if (port_state & SATA_PSTATE_SHUTDOWN ||
12124 	    port_state & SATA_PSTATE_FAILED) {
12125 		ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
12126 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12127 		if (port_state & SATA_PSTATE_FAILED)
12128 			ap_state->ap_condition = AP_COND_FAILED;
12129 		else
12130 			ap_state->ap_condition = AP_COND_UNKNOWN;
12131 
12132 		return;
12133 	}
12134 
12135 	/* Need to check pmult device port here as well, when supported */
12136 
12137 	/* Port is enabled and ready */
12138 
12139 	switch (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport)) {
12140 	case SATA_DTYPE_NONE:
12141 	{
12142 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12143 		ap_state->ap_condition = AP_COND_OK;
12144 		/* No device attached */
12145 		ap_state->ap_rstate = AP_RSTATE_EMPTY;
12146 		break;
12147 	}
12148 	case SATA_DTYPE_UNKNOWN:
12149 	case SATA_DTYPE_ATAPINONCD:
12150 	case SATA_DTYPE_PMULT:	/* Until PMult is supported */
12151 	case SATA_DTYPE_ATADISK:
12152 	case SATA_DTYPE_ATAPICD:
12153 	{
12154 		dev_info_t *tdip = NULL;
12155 		dev_info_t *dip = NULL;
12156 		int circ;
12157 
12158 		dip = SATA_DIP(sata_hba_inst);
12159 		tdip = sata_get_target_dip(dip, port);
12160 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
12161 		if (tdip != NULL) {
12162 			ndi_devi_enter(dip, &circ);
12163 			mutex_enter(&(DEVI(tdip)->devi_lock));
12164 			if (DEVI_IS_DEVICE_REMOVED(tdip)) {
12165 				/*
12166 				 * There could be the case where previously
12167 				 * configured and opened device was removed
12168 				 * and unknown device was plugged.
12169 				 * In such case we want to show a device, and
12170 				 * its configured or unconfigured state but
12171 				 * indicate unusable condition untill the
12172 				 * old target node is released and removed.
12173 				 */
12174 				ap_state->ap_condition = AP_COND_UNUSABLE;
12175 			} else {
12176 				mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst,
12177 				    cport));
12178 				sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12179 				    cport);
12180 				if (sdinfo != NULL) {
12181 					if ((sdinfo->satadrv_state &
12182 					    SATA_DSTATE_FAILED) != 0)
12183 						ap_state->ap_condition =
12184 						    AP_COND_FAILED;
12185 					else
12186 						ap_state->ap_condition =
12187 						    AP_COND_OK;
12188 				} else {
12189 					ap_state->ap_condition =
12190 					    AP_COND_UNKNOWN;
12191 				}
12192 				mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst,
12193 				    cport));
12194 			}
12195 			if ((DEVI_IS_DEVICE_OFFLINE(tdip)) ||
12196 			    (DEVI_IS_DEVICE_DOWN(tdip))) {
12197 				ap_state->ap_ostate =
12198 				    AP_OSTATE_UNCONFIGURED;
12199 			} else {
12200 				ap_state->ap_ostate =
12201 				    AP_OSTATE_CONFIGURED;
12202 			}
12203 			mutex_exit(&(DEVI(tdip)->devi_lock));
12204 			ndi_devi_exit(dip, circ);
12205 		} else {
12206 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12207 			ap_state->ap_condition = AP_COND_UNKNOWN;
12208 		}
12209 		break;
12210 	}
12211 	default:
12212 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
12213 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12214 		ap_state->ap_condition = AP_COND_UNKNOWN;
12215 		/*
12216 		 * This is actually internal error condition (non fatal),
12217 		 * because we have already checked all defined device types.
12218 		 */
12219 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12220 		    "sata_cfgadm_state: Internal error: "
12221 		    "unknown device type"));
12222 		break;
12223 	}
12224 }
12225 
12226 
12227 /*
12228  * Process ioctl get device path request.
12229  *
12230  * NOTE: Port multiplier code is not completed nor tested.
12231  */
12232 static int
12233 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst,
12234     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12235 {
12236 	char path[MAXPATHLEN];
12237 	uint32_t size;
12238 	dev_info_t *tdip;
12239 
12240 	(void) strcpy(path, "/devices");
12241 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
12242 	    &sata_device->satadev_addr)) == NULL) {
12243 		/*
12244 		 * No such device. If this is a request for a size, do not
12245 		 * return EINVAL for non-existing target, because cfgadm
12246 		 * will then indicate a meaningless ioctl failure.
12247 		 * If this is a request for a path, indicate invalid
12248 		 * argument.
12249 		 */
12250 		if (ioc->get_size == 0)
12251 			return (EINVAL);
12252 	} else {
12253 		(void) ddi_pathname(tdip, path + strlen(path));
12254 	}
12255 	size = strlen(path) + 1;
12256 
12257 	if (ioc->get_size != 0) {
12258 		if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz,
12259 		    mode) != 0)
12260 			return (EFAULT);
12261 	} else {
12262 		if (ioc->bufsiz != size)
12263 			return (EINVAL);
12264 
12265 		else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz,
12266 		    mode) != 0)
12267 			return (EFAULT);
12268 	}
12269 	return (0);
12270 }
12271 
12272 /*
12273  * Process ioctl get attachment point type request.
12274  *
12275  * NOTE: Port multiplier code is not completed nor tested.
12276  */
12277 static	int
12278 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst,
12279     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12280 {
12281 	uint32_t	type_len;
12282 	const char	*ap_type;
12283 	int		dev_type;
12284 
12285 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12286 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst,
12287 		    sata_device->satadev_addr.cport);
12288 	else /* pmport */
12289 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12290 		    sata_device->satadev_addr.cport,
12291 		    sata_device->satadev_addr.pmport);
12292 
12293 	switch (dev_type) {
12294 	case SATA_DTYPE_NONE:
12295 		ap_type = "port";
12296 		break;
12297 
12298 	case SATA_DTYPE_ATADISK:
12299 		ap_type = "disk";
12300 		break;
12301 
12302 	case SATA_DTYPE_ATAPICD:
12303 		ap_type = "cd/dvd";
12304 		break;
12305 
12306 	case SATA_DTYPE_PMULT:
12307 		ap_type = "pmult";
12308 		break;
12309 
12310 	case SATA_DTYPE_UNKNOWN:
12311 		ap_type = "unknown";
12312 		break;
12313 
12314 	default:
12315 		ap_type = "unsupported";
12316 		break;
12317 
12318 	} /* end of dev_type switch */
12319 
12320 	type_len = strlen(ap_type) + 1;
12321 
12322 	if (ioc->get_size) {
12323 		if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz,
12324 		    mode) != 0)
12325 			return (EFAULT);
12326 	} else {
12327 		if (ioc->bufsiz != type_len)
12328 			return (EINVAL);
12329 
12330 		if (ddi_copyout((void *)ap_type, ioc->buf,
12331 		    ioc->bufsiz, mode) != 0)
12332 			return (EFAULT);
12333 	}
12334 	return (0);
12335 
12336 }
12337 
12338 /*
12339  * Process ioctl get device model info request.
12340  * This operation should return to cfgadm the device model
12341  * information string
12342  *
12343  * NOTE: Port multiplier code is not completed nor tested.
12344  */
12345 static	int
12346 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst,
12347     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12348 {
12349 	sata_drive_info_t *sdinfo;
12350 	uint32_t info_len;
12351 	char ap_info[SATA_ID_MODEL_LEN + 1];
12352 
12353 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12354 	    sata_device->satadev_addr.cport)->cport_mutex);
12355 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12356 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12357 		    sata_device->satadev_addr.cport);
12358 	else /* port multiplier */
12359 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12360 		    sata_device->satadev_addr.cport,
12361 		    sata_device->satadev_addr.pmport);
12362 	if (sdinfo == NULL) {
12363 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12364 		    sata_device->satadev_addr.cport)->cport_mutex);
12365 		return (EINVAL);
12366 	}
12367 
12368 #ifdef	_LITTLE_ENDIAN
12369 	swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
12370 #else	/* _LITTLE_ENDIAN */
12371 	bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
12372 #endif	/* _LITTLE_ENDIAN */
12373 
12374 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12375 	    sata_device->satadev_addr.cport)->cport_mutex);
12376 
12377 	ap_info[SATA_ID_MODEL_LEN] = '\0';
12378 
12379 	info_len = strlen(ap_info) + 1;
12380 
12381 	if (ioc->get_size) {
12382 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12383 		    mode) != 0)
12384 			return (EFAULT);
12385 	} else {
12386 		if (ioc->bufsiz < info_len)
12387 			return (EINVAL);
12388 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12389 		    mode) != 0)
12390 			return (EFAULT);
12391 	}
12392 	return (0);
12393 }
12394 
12395 
12396 /*
12397  * Process ioctl get device firmware revision info request.
12398  * This operation should return to cfgadm the device firmware revision
12399  * information string
12400  *
12401  * NOTE: Port multiplier code is not completed nor tested.
12402  */
12403 static	int
12404 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst,
12405     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12406 {
12407 	sata_drive_info_t *sdinfo;
12408 	uint32_t info_len;
12409 	char ap_info[SATA_ID_FW_LEN + 1];
12410 
12411 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12412 	    sata_device->satadev_addr.cport)->cport_mutex);
12413 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12414 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12415 		    sata_device->satadev_addr.cport);
12416 	else /* port multiplier */
12417 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12418 		    sata_device->satadev_addr.cport,
12419 		    sata_device->satadev_addr.pmport);
12420 	if (sdinfo == NULL) {
12421 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12422 		    sata_device->satadev_addr.cport)->cport_mutex);
12423 		return (EINVAL);
12424 	}
12425 
12426 #ifdef	_LITTLE_ENDIAN
12427 	swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
12428 #else	/* _LITTLE_ENDIAN */
12429 	bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
12430 #endif	/* _LITTLE_ENDIAN */
12431 
12432 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12433 	    sata_device->satadev_addr.cport)->cport_mutex);
12434 
12435 	ap_info[SATA_ID_FW_LEN] = '\0';
12436 
12437 	info_len = strlen(ap_info) + 1;
12438 
12439 	if (ioc->get_size) {
12440 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12441 		    mode) != 0)
12442 			return (EFAULT);
12443 	} else {
12444 		if (ioc->bufsiz < info_len)
12445 			return (EINVAL);
12446 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12447 		    mode) != 0)
12448 			return (EFAULT);
12449 	}
12450 	return (0);
12451 }
12452 
12453 
12454 /*
12455  * Process ioctl get device serial number info request.
12456  * This operation should return to cfgadm the device serial number string.
12457  *
12458  * NOTE: Port multiplier code is not completed nor tested.
12459  */
12460 static	int
12461 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst,
12462     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12463 {
12464 	sata_drive_info_t *sdinfo;
12465 	uint32_t info_len;
12466 	char ap_info[SATA_ID_SERIAL_LEN + 1];
12467 
12468 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12469 	    sata_device->satadev_addr.cport)->cport_mutex);
12470 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12471 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12472 		    sata_device->satadev_addr.cport);
12473 	else /* port multiplier */
12474 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12475 		    sata_device->satadev_addr.cport,
12476 		    sata_device->satadev_addr.pmport);
12477 	if (sdinfo == NULL) {
12478 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12479 		    sata_device->satadev_addr.cport)->cport_mutex);
12480 		return (EINVAL);
12481 	}
12482 
12483 #ifdef	_LITTLE_ENDIAN
12484 	swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
12485 #else	/* _LITTLE_ENDIAN */
12486 	bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
12487 #endif	/* _LITTLE_ENDIAN */
12488 
12489 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12490 	    sata_device->satadev_addr.cport)->cport_mutex);
12491 
12492 	ap_info[SATA_ID_SERIAL_LEN] = '\0';
12493 
12494 	info_len = strlen(ap_info) + 1;
12495 
12496 	if (ioc->get_size) {
12497 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12498 		    mode) != 0)
12499 			return (EFAULT);
12500 	} else {
12501 		if (ioc->bufsiz < info_len)
12502 			return (EINVAL);
12503 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12504 		    mode) != 0)
12505 			return (EFAULT);
12506 	}
12507 	return (0);
12508 }
12509 
12510 
12511 /*
12512  * Preset scsi extended sense data (to NO SENSE)
12513  * First 18 bytes of the sense data are preset to current valid sense
12514  * with a key NO SENSE data.
12515  *
12516  * Returns void
12517  */
12518 static void
12519 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense)
12520 {
12521 	sense->es_valid = 1;		/* Valid sense */
12522 	sense->es_class = CLASS_EXTENDED_SENSE;	/* 0x70 - current err */
12523 	sense->es_key = KEY_NO_SENSE;
12524 	sense->es_info_1 = 0;
12525 	sense->es_info_2 = 0;
12526 	sense->es_info_3 = 0;
12527 	sense->es_info_4 = 0;
12528 	sense->es_add_len = 10;	/* Additional length - replace with a def */
12529 	sense->es_cmd_info[0] = 0;
12530 	sense->es_cmd_info[1] = 0;
12531 	sense->es_cmd_info[2] = 0;
12532 	sense->es_cmd_info[3] = 0;
12533 	sense->es_add_code = 0;
12534 	sense->es_qual_code = 0;
12535 }
12536 
12537 /*
12538  * Register a legacy cmdk-style devid for the target (disk) device.
12539  *
12540  * Note: This function is called only when the HBA devinfo node has the
12541  * property "use-cmdk-devid-format" set. This property indicates that
12542  * devid compatible with old cmdk (target) driver is to be generated
12543  * for any target device attached to this controller. This will take
12544  * precedence over the devid generated by sd (target) driver.
12545  * This function is derived from cmdk_devid_setup() function in cmdk.c.
12546  */
12547 static void
12548 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo)
12549 {
12550 	char	*hwid;
12551 	int	modlen;
12552 	int	serlen;
12553 	int	rval;
12554 	ddi_devid_t	devid;
12555 
12556 	/*
12557 	 * device ID is a concatanation of model number, "=", serial number.
12558 	 */
12559 	hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP);
12560 	bcopy(&sdinfo->satadrv_id.ai_model, hwid,
12561 	    sizeof (sdinfo->satadrv_id.ai_model));
12562 	swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model));
12563 	modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model));
12564 	if (modlen == 0)
12565 		goto err;
12566 	hwid[modlen++] = '=';
12567 	bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen],
12568 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12569 	swab(&hwid[modlen], &hwid[modlen],
12570 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12571 	serlen = sata_check_modser(&hwid[modlen],
12572 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12573 	if (serlen == 0)
12574 		goto err;
12575 	hwid[modlen + serlen] = 0; /* terminate the hwid string */
12576 
12577 	/* initialize/register devid */
12578 	if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL,
12579 	    (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS)
12580 		rval = ddi_devid_register(dip, devid);
12581 
12582 	if (rval != DDI_SUCCESS)
12583 		cmn_err(CE_WARN, "sata: failed to create devid for the disk"
12584 		    " on port %d", sdinfo->satadrv_addr.cport);
12585 err:
12586 	kmem_free(hwid, LEGACY_HWID_LEN);
12587 }
12588 
12589 /*
12590  * valid model/serial string must contain a non-zero non-space characters.
12591  * trim trailing spaces/NULLs.
12592  */
12593 static int
12594 sata_check_modser(char *buf, int buf_len)
12595 {
12596 	boolean_t ret;
12597 	char *s;
12598 	int i;
12599 	int tb;
12600 	char ch;
12601 
12602 	ret = B_FALSE;
12603 	s = buf;
12604 	for (i = 0; i < buf_len; i++) {
12605 		ch = *s++;
12606 		if (ch != ' ' && ch != '\0')
12607 			tb = i + 1;
12608 		if (ch != ' ' && ch != '\0' && ch != '0')
12609 			ret = B_TRUE;
12610 	}
12611 
12612 	if (ret == B_FALSE)
12613 		return (0); /* invalid string */
12614 
12615 	return (tb); /* return length */
12616 }
12617 
12618 /*
12619  * sata_set_drive_features function compares current device features setting
12620  * with the saved device features settings and, if there is a difference,
12621  * it restores device features setting to the previously saved state.
12622  * It also arbitrarily tries to select the highest supported DMA mode.
12623  * Device Identify or Identify Packet Device data has to be current.
12624  * At the moment read ahead and write cache are considered for all devices.
12625  * For atapi devices, Removable Media Status Notification is set in addition
12626  * to common features.
12627  *
12628  * This function cannot be called in the interrupt context (it may sleep).
12629  *
12630  * The input argument sdinfo should point to the drive info structure
12631  * to be updated after features are set. Note, that only
12632  * device (packet) identify data is updated, not the flags indicating the
12633  * supported features.
12634  *
12635  * Returns TRUE if successful or there was nothing to do. Device Identify data
12636  * in the drive info structure pointed to by the sdinfo argumens is updated
12637  * even when no features were set or changed.
12638  *
12639  * Returns FALSE if device features could not be set.
12640  *
12641  * Note: This function may fail the port, making it inaccessible.
12642  * In such case the explicit port disconnect/connect or physical device
12643  * detach/attach is required to re-evaluate port state again.
12644  */
12645 
12646 static int
12647 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst,
12648     sata_drive_info_t *sdinfo, int restore)
12649 {
12650 	int rval = SATA_SUCCESS;
12651 	sata_drive_info_t new_sdinfo;
12652 	char *finfo = "sata_set_drive_features: cannot";
12653 	char *finfox;
12654 	int cache_op;
12655 
12656 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
12657 	new_sdinfo.satadrv_addr = sdinfo->satadrv_addr;
12658 	new_sdinfo.satadrv_type = sdinfo->satadrv_type;
12659 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
12660 		/*
12661 		 * Cannot get device identification - retry later
12662 		 */
12663 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12664 		    "%s fetch device identify data\n", finfo);
12665 		return (SATA_FAILURE);
12666 	}
12667 	finfox = (restore != 0) ? " restore device features" :
12668 	    " initialize device features\n";
12669 
12670 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12671 		/* Arbitrarily set UDMA mode */
12672 		if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
12673 		    SATA_SUCCESS) {
12674 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12675 			    "%s set UDMA mode\n", finfo));
12676 			return (SATA_FAILURE);
12677 		}
12678 	} else { /* Assume SATA ATAPI CD/DVD */
12679 		/*  Set Removable Media Status Notification, if necessary */
12680 		if ((new_sdinfo.satadrv_id.ai_cmdset83 &
12681 		    SATA_RM_STATUS_NOTIFIC) != 0 && restore != 0) {
12682 			if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) &&
12683 			    (!(new_sdinfo.satadrv_id.ai_features86 &
12684 			    SATA_RM_STATUS_NOTIFIC))) ||
12685 			    ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) &&
12686 			    (new_sdinfo.satadrv_id.ai_features86 &
12687 			    SATA_RM_STATUS_NOTIFIC))) {
12688 				/* Current setting does not match saved one */
12689 				if (sata_set_rmsn(sata_hba_inst, sdinfo,
12690 				    sdinfo->satadrv_settings &
12691 				    SATA_DEV_RMSN) != SATA_SUCCESS)
12692 					rval = SATA_FAILURE;
12693 			}
12694 		}
12695 		/*
12696 		 * We have to set Multiword DMA or UDMA, if it is supported, as
12697 		 * we want to use DMA transfer mode whenever possible.
12698 		 * Some devices require explicit setting of the DMA mode.
12699 		 */
12700 		if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) {
12701 			/* Set highest supported DMA mode */
12702 			if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
12703 			    SATA_SUCCESS) {
12704 				SATA_LOG_D((sata_hba_inst, CE_WARN,
12705 				    "%s set UDMA mode\n", finfo));
12706 				rval = SATA_FAILURE;
12707 			}
12708 		}
12709 	}
12710 
12711 	if (!(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) &&
12712 	    !(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) {
12713 		/* None of the features is supported - do nothing */
12714 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12715 		    "settable features not supported\n", NULL);
12716 		goto update_sdinfo;
12717 	}
12718 
12719 	if (((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) &&
12720 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) &&
12721 	    ((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) &&
12722 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
12723 		/* Nothing to do */
12724 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12725 		    "no device features to set\n", NULL);
12726 		goto update_sdinfo;
12727 	}
12728 
12729 	if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) &&
12730 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD))) {
12731 		if (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) {
12732 			/* Enable read ahead / read cache */
12733 			cache_op = SATAC_SF_ENABLE_READ_AHEAD;
12734 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12735 			    "enabling read cache\n", NULL);
12736 		} else {
12737 			/* Disable read ahead  / read cache */
12738 			cache_op = SATAC_SF_DISABLE_READ_AHEAD;
12739 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12740 			    "disabling read cache\n", NULL);
12741 		}
12742 
12743 		/* Try to set read cache mode */
12744 		if (sata_set_cache_mode(sata_hba_inst, &new_sdinfo,
12745 		    cache_op) != SATA_SUCCESS) {
12746 			/* Pkt execution failed */
12747 			rval = SATA_FAILURE;
12748 		}
12749 	}
12750 
12751 	if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) &&
12752 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
12753 		if (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) {
12754 			/* Enable write cache */
12755 			cache_op = SATAC_SF_ENABLE_WRITE_CACHE;
12756 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12757 			    "enabling write cache\n", NULL);
12758 		} else {
12759 			/* Disable write cache */
12760 			cache_op = SATAC_SF_DISABLE_WRITE_CACHE;
12761 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12762 			    "disabling write cache\n", NULL);
12763 		}
12764 		/* Try to set write cache mode */
12765 		if (sata_set_cache_mode(sata_hba_inst, &new_sdinfo,
12766 		    cache_op) != SATA_SUCCESS) {
12767 			/* Pkt execution failed */
12768 			rval = SATA_FAILURE;
12769 		}
12770 	}
12771 
12772 	if (rval == SATA_FAILURE)
12773 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12774 		    "%s %s", finfo, finfox));
12775 update_sdinfo:
12776 	/*
12777 	 * We need to fetch Device Identify data again
12778 	 */
12779 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
12780 		/*
12781 		 * Cannot get device identification - retry later
12782 		 */
12783 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12784 		    "%s re-fetch device identify data\n", finfo));
12785 		rval = SATA_FAILURE;
12786 	}
12787 	/* Copy device sata info. */
12788 	sdinfo->satadrv_id = new_sdinfo.satadrv_id;
12789 
12790 	return (rval);
12791 }
12792 
12793 
12794 /*
12795  *
12796  * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if
12797  * unable to determine.
12798  *
12799  * Cannot be called in an interrupt context.
12800  *
12801  * Called by sata_build_lsense_page_2f()
12802  */
12803 
12804 static int
12805 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst,
12806     sata_drive_info_t *sdinfo)
12807 {
12808 	sata_pkt_t *spkt;
12809 	sata_cmd_t *scmd;
12810 	sata_pkt_txlate_t *spx;
12811 	int rval;
12812 
12813 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12814 	spx->txlt_sata_hba_inst = sata_hba_inst;
12815 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12816 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12817 	if (spkt == NULL) {
12818 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12819 		return (-1);
12820 	}
12821 	/* address is needed now */
12822 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12823 
12824 
12825 	/* Fill sata_pkt */
12826 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12827 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12828 	/* Synchronous mode, no callback */
12829 	spkt->satapkt_comp = NULL;
12830 	/* Timeout 30s */
12831 	spkt->satapkt_time = sata_default_pkt_time;
12832 
12833 	scmd = &spkt->satapkt_cmd;
12834 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
12835 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
12836 
12837 	/* Set up which registers need to be returned */
12838 	scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE;
12839 	scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE;
12840 
12841 	/* Build SMART_RETURN_STATUS cmd in the sata_pkt */
12842 	scmd->satacmd_addr_type = 0;		/* N/A */
12843 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
12844 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
12845 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
12846 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
12847 	scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS;
12848 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12849 	scmd->satacmd_cmd_reg = SATAC_SMART;
12850 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12851 	    sdinfo->satadrv_addr.cport)));
12852 
12853 
12854 	/* Send pkt to SATA HBA driver */
12855 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12856 	    SATA_TRAN_ACCEPTED ||
12857 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12858 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12859 		    sdinfo->satadrv_addr.cport)));
12860 		/*
12861 		 * Whoops, no SMART RETURN STATUS
12862 		 */
12863 		rval = -1;
12864 	} else {
12865 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12866 		    sdinfo->satadrv_addr.cport)));
12867 		if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
12868 			rval = -1;
12869 			goto fail;
12870 		}
12871 		if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
12872 			rval = -1;
12873 			goto fail;
12874 		}
12875 		if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) &&
12876 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2))
12877 			rval = 0;
12878 		else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) &&
12879 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4))
12880 			rval = 1;
12881 		else {
12882 			rval = -1;
12883 			goto fail;
12884 		}
12885 	}
12886 fail:
12887 	/* Free allocated resources */
12888 	sata_pkt_free(spx);
12889 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12890 
12891 	return (rval);
12892 }
12893 
12894 /*
12895  *
12896  * Returns 0 if succeeded, -1 otherwise
12897  *
12898  * Cannot be called in an interrupt context.
12899  *
12900  */
12901 static int
12902 sata_fetch_smart_data(
12903 	sata_hba_inst_t *sata_hba_inst,
12904 	sata_drive_info_t *sdinfo,
12905 	struct smart_data *smart_data)
12906 {
12907 	sata_pkt_t *spkt;
12908 	sata_cmd_t *scmd;
12909 	sata_pkt_txlate_t *spx;
12910 	int rval;
12911 
12912 #if ! defined(lint)
12913 	ASSERT(sizeof (struct smart_data) == 512);
12914 #endif
12915 
12916 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12917 	spx->txlt_sata_hba_inst = sata_hba_inst;
12918 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12919 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12920 	if (spkt == NULL) {
12921 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12922 		return (-1);
12923 	}
12924 	/* address is needed now */
12925 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12926 
12927 
12928 	/* Fill sata_pkt */
12929 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12930 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12931 	/* Synchronous mode, no callback */
12932 	spkt->satapkt_comp = NULL;
12933 	/* Timeout 30s */
12934 	spkt->satapkt_time = sata_default_pkt_time;
12935 
12936 	scmd = &spkt->satapkt_cmd;
12937 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
12938 
12939 	/*
12940 	 * Allocate buffer for SMART data
12941 	 */
12942 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
12943 	    sizeof (struct smart_data));
12944 	if (scmd->satacmd_bp == NULL) {
12945 		sata_pkt_free(spx);
12946 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12947 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12948 		    "sata_fetch_smart_data: "
12949 		    "cannot allocate buffer"));
12950 		return (-1);
12951 	}
12952 
12953 
12954 	/* Build SMART_READ_DATA cmd in the sata_pkt */
12955 	scmd->satacmd_addr_type = 0;		/* N/A */
12956 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
12957 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
12958 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
12959 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
12960 	scmd->satacmd_features_reg = SATA_SMART_READ_DATA;
12961 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12962 	scmd->satacmd_cmd_reg = SATAC_SMART;
12963 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12964 	    sdinfo->satadrv_addr.cport)));
12965 
12966 	/* Send pkt to SATA HBA driver */
12967 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12968 	    SATA_TRAN_ACCEPTED ||
12969 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12970 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12971 		    sdinfo->satadrv_addr.cport)));
12972 		/*
12973 		 * Whoops, no SMART DATA available
12974 		 */
12975 		rval = -1;
12976 		goto fail;
12977 	} else {
12978 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12979 		    sdinfo->satadrv_addr.cport)));
12980 		if (spx->txlt_buf_dma_handle != NULL) {
12981 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
12982 			    DDI_DMA_SYNC_FORKERNEL);
12983 			ASSERT(rval == DDI_SUCCESS);
12984 		}
12985 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data,
12986 		    sizeof (struct smart_data));
12987 	}
12988 
12989 fail:
12990 	/* Free allocated resources */
12991 	sata_free_local_buffer(spx);
12992 	sata_pkt_free(spx);
12993 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12994 
12995 	return (rval);
12996 }
12997 
12998 /*
12999  * Used by LOG SENSE page 0x10
13000  *
13001  * return 0 for success, -1 otherwise
13002  *
13003  */
13004 static int
13005 sata_ext_smart_selftest_read_log(
13006 	sata_hba_inst_t *sata_hba_inst,
13007 	sata_drive_info_t *sdinfo,
13008 	struct smart_ext_selftest_log *ext_selftest_log,
13009 	uint16_t block_num)
13010 {
13011 	sata_pkt_txlate_t *spx;
13012 	sata_pkt_t *spkt;
13013 	sata_cmd_t *scmd;
13014 	int rval;
13015 
13016 #if ! defined(lint)
13017 	ASSERT(sizeof (struct smart_ext_selftest_log) == 512);
13018 #endif
13019 
13020 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13021 	spx->txlt_sata_hba_inst = sata_hba_inst;
13022 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13023 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13024 	if (spkt == NULL) {
13025 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13026 		return (-1);
13027 	}
13028 	/* address is needed now */
13029 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13030 
13031 
13032 	/* Fill sata_pkt */
13033 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13034 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13035 	/* Synchronous mode, no callback */
13036 	spkt->satapkt_comp = NULL;
13037 	/* Timeout 30s */
13038 	spkt->satapkt_time = sata_default_pkt_time;
13039 
13040 	scmd = &spkt->satapkt_cmd;
13041 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13042 
13043 	/*
13044 	 * Allocate buffer for SMART extended self-test log
13045 	 */
13046 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13047 	    sizeof (struct smart_ext_selftest_log));
13048 	if (scmd->satacmd_bp == NULL) {
13049 		sata_pkt_free(spx);
13050 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13051 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13052 		    "sata_ext_smart_selftest_log: "
13053 		    "cannot allocate buffer"));
13054 		return (-1);
13055 	}
13056 
13057 	/* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */
13058 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
13059 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of selftest log */
13060 	scmd->satacmd_sec_count_msb = 0;	/* One sector of selftest log */
13061 	scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE;
13062 	scmd->satacmd_lba_low_msb = 0;
13063 	scmd->satacmd_lba_mid_lsb = block_num & 0xff;
13064 	scmd->satacmd_lba_mid_msb = block_num >> 8;
13065 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13066 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
13067 
13068 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13069 	    sdinfo->satadrv_addr.cport)));
13070 
13071 	/* Send pkt to SATA HBA driver */
13072 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13073 	    SATA_TRAN_ACCEPTED ||
13074 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13075 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13076 		    sdinfo->satadrv_addr.cport)));
13077 
13078 		/*
13079 		 * Whoops, no SMART selftest log info available
13080 		 */
13081 		rval = -1;
13082 		goto fail;
13083 	} else {
13084 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13085 		    sdinfo->satadrv_addr.cport)));
13086 
13087 		if (spx->txlt_buf_dma_handle != NULL) {
13088 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13089 			    DDI_DMA_SYNC_FORKERNEL);
13090 			ASSERT(rval == DDI_SUCCESS);
13091 		}
13092 		bcopy(scmd->satacmd_bp->b_un.b_addr,
13093 		    (uint8_t *)ext_selftest_log,
13094 		    sizeof (struct smart_ext_selftest_log));
13095 		rval = 0;
13096 	}
13097 
13098 fail:
13099 	/* Free allocated resources */
13100 	sata_free_local_buffer(spx);
13101 	sata_pkt_free(spx);
13102 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13103 
13104 	return (rval);
13105 }
13106 
13107 /*
13108  * Returns 0 for success, -1 otherwise
13109  *
13110  * SMART self-test log data is returned in buffer pointed to by selftest_log
13111  */
13112 static int
13113 sata_smart_selftest_log(
13114 	sata_hba_inst_t *sata_hba_inst,
13115 	sata_drive_info_t *sdinfo,
13116 	struct smart_selftest_log *selftest_log)
13117 {
13118 	sata_pkt_t *spkt;
13119 	sata_cmd_t *scmd;
13120 	sata_pkt_txlate_t *spx;
13121 	int rval;
13122 
13123 #if ! defined(lint)
13124 	ASSERT(sizeof (struct smart_selftest_log) == 512);
13125 #endif
13126 
13127 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13128 	spx->txlt_sata_hba_inst = sata_hba_inst;
13129 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13130 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13131 	if (spkt == NULL) {
13132 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13133 		return (-1);
13134 	}
13135 	/* address is needed now */
13136 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13137 
13138 
13139 	/* Fill sata_pkt */
13140 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13141 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13142 	/* Synchronous mode, no callback */
13143 	spkt->satapkt_comp = NULL;
13144 	/* Timeout 30s */
13145 	spkt->satapkt_time = sata_default_pkt_time;
13146 
13147 	scmd = &spkt->satapkt_cmd;
13148 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13149 
13150 	/*
13151 	 * Allocate buffer for SMART SELFTEST LOG
13152 	 */
13153 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13154 	    sizeof (struct smart_selftest_log));
13155 	if (scmd->satacmd_bp == NULL) {
13156 		sata_pkt_free(spx);
13157 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13158 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13159 		    "sata_smart_selftest_log: "
13160 		    "cannot allocate buffer"));
13161 		return (-1);
13162 	}
13163 
13164 	/* Build SMART_READ_LOG cmd in the sata_pkt */
13165 	scmd->satacmd_addr_type = 0;		/* N/A */
13166 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of SMART log */
13167 	scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE;
13168 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13169 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13170 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
13171 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13172 	scmd->satacmd_cmd_reg = SATAC_SMART;
13173 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13174 	    sdinfo->satadrv_addr.cport)));
13175 
13176 	/* Send pkt to SATA HBA driver */
13177 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13178 	    SATA_TRAN_ACCEPTED ||
13179 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13180 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13181 		    sdinfo->satadrv_addr.cport)));
13182 		/*
13183 		 * Whoops, no SMART DATA available
13184 		 */
13185 		rval = -1;
13186 		goto fail;
13187 	} else {
13188 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13189 		    sdinfo->satadrv_addr.cport)));
13190 		if (spx->txlt_buf_dma_handle != NULL) {
13191 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13192 			    DDI_DMA_SYNC_FORKERNEL);
13193 			ASSERT(rval == DDI_SUCCESS);
13194 		}
13195 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log,
13196 		    sizeof (struct smart_selftest_log));
13197 		rval = 0;
13198 	}
13199 
13200 fail:
13201 	/* Free allocated resources */
13202 	sata_free_local_buffer(spx);
13203 	sata_pkt_free(spx);
13204 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13205 
13206 	return (rval);
13207 }
13208 
13209 
13210 /*
13211  * Returns 0 for success, -1 otherwise
13212  *
13213  * SMART READ LOG data is returned in buffer pointed to by smart_log
13214  */
13215 static int
13216 sata_smart_read_log(
13217 	sata_hba_inst_t *sata_hba_inst,
13218 	sata_drive_info_t *sdinfo,
13219 	uint8_t *smart_log,		/* where the data should be returned */
13220 	uint8_t which_log,		/* which log should be returned */
13221 	uint8_t log_size)		/* # of 512 bytes in log */
13222 {
13223 	sata_pkt_t *spkt;
13224 	sata_cmd_t *scmd;
13225 	sata_pkt_txlate_t *spx;
13226 	int rval;
13227 
13228 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13229 	spx->txlt_sata_hba_inst = sata_hba_inst;
13230 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13231 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13232 	if (spkt == NULL) {
13233 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13234 		return (-1);
13235 	}
13236 	/* address is needed now */
13237 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13238 
13239 
13240 	/* Fill sata_pkt */
13241 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13242 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13243 	/* Synchronous mode, no callback */
13244 	spkt->satapkt_comp = NULL;
13245 	/* Timeout 30s */
13246 	spkt->satapkt_time = sata_default_pkt_time;
13247 
13248 	scmd = &spkt->satapkt_cmd;
13249 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13250 
13251 	/*
13252 	 * Allocate buffer for SMART READ LOG
13253 	 */
13254 	scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512);
13255 	if (scmd->satacmd_bp == NULL) {
13256 		sata_pkt_free(spx);
13257 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13258 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13259 		    "sata_smart_read_log: " "cannot allocate buffer"));
13260 		return (-1);
13261 	}
13262 
13263 	/* Build SMART_READ_LOG cmd in the sata_pkt */
13264 	scmd->satacmd_addr_type = 0;		/* N/A */
13265 	scmd->satacmd_sec_count_lsb = log_size;	/* what the caller asked for */
13266 	scmd->satacmd_lba_low_lsb = which_log;	/* which log page */
13267 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13268 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13269 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
13270 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13271 	scmd->satacmd_cmd_reg = SATAC_SMART;
13272 
13273 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13274 	    sdinfo->satadrv_addr.cport)));
13275 
13276 	/* Send pkt to SATA HBA driver */
13277 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13278 	    SATA_TRAN_ACCEPTED ||
13279 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13280 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13281 		    sdinfo->satadrv_addr.cport)));
13282 
13283 		/*
13284 		 * Whoops, no SMART DATA available
13285 		 */
13286 		rval = -1;
13287 		goto fail;
13288 	} else {
13289 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13290 		    sdinfo->satadrv_addr.cport)));
13291 
13292 		if (spx->txlt_buf_dma_handle != NULL) {
13293 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13294 			    DDI_DMA_SYNC_FORKERNEL);
13295 			ASSERT(rval == DDI_SUCCESS);
13296 		}
13297 		bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512);
13298 		rval = 0;
13299 	}
13300 
13301 fail:
13302 	/* Free allocated resources */
13303 	sata_free_local_buffer(spx);
13304 	sata_pkt_free(spx);
13305 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13306 
13307 	return (rval);
13308 }
13309 
13310 /*
13311  * Used by LOG SENSE page 0x10
13312  *
13313  * return 0 for success, -1 otherwise
13314  *
13315  */
13316 static int
13317 sata_read_log_ext_directory(
13318 	sata_hba_inst_t *sata_hba_inst,
13319 	sata_drive_info_t *sdinfo,
13320 	struct read_log_ext_directory *logdir)
13321 {
13322 	sata_pkt_txlate_t *spx;
13323 	sata_pkt_t *spkt;
13324 	sata_cmd_t *scmd;
13325 	int rval;
13326 
13327 #if ! defined(lint)
13328 	ASSERT(sizeof (struct read_log_ext_directory) == 512);
13329 #endif
13330 
13331 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13332 	spx->txlt_sata_hba_inst = sata_hba_inst;
13333 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13334 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13335 	if (spkt == NULL) {
13336 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13337 		return (-1);
13338 	}
13339 
13340 	/* Fill sata_pkt */
13341 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13342 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13343 	/* Synchronous mode, no callback */
13344 	spkt->satapkt_comp = NULL;
13345 	/* Timeout 30s */
13346 	spkt->satapkt_time = sata_default_pkt_time;
13347 
13348 	scmd = &spkt->satapkt_cmd;
13349 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13350 
13351 	/*
13352 	 * Allocate buffer for SMART READ LOG EXTENDED command
13353 	 */
13354 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13355 	    sizeof (struct read_log_ext_directory));
13356 	if (scmd->satacmd_bp == NULL) {
13357 		sata_pkt_free(spx);
13358 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13359 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13360 		    "sata_read_log_ext_directory: "
13361 		    "cannot allocate buffer"));
13362 		return (-1);
13363 	}
13364 
13365 	/* Build READ LOG EXT w/ log directory cmd in the  sata_pkt */
13366 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
13367 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of directory */
13368 	scmd->satacmd_sec_count_msb = 0;	/* One sector of directory */
13369 	scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY;
13370 	scmd->satacmd_lba_low_msb = 0;
13371 	scmd->satacmd_lba_mid_lsb = 0;
13372 	scmd->satacmd_lba_mid_msb = 0;
13373 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13374 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
13375 
13376 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13377 	    sdinfo->satadrv_addr.cport)));
13378 
13379 	/* Send pkt to SATA HBA driver */
13380 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13381 	    SATA_TRAN_ACCEPTED ||
13382 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13383 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13384 		    sdinfo->satadrv_addr.cport)));
13385 		/*
13386 		 * Whoops, no SMART selftest log info available
13387 		 */
13388 		rval = -1;
13389 		goto fail;
13390 	} else {
13391 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13392 		    sdinfo->satadrv_addr.cport)));
13393 		if (spx->txlt_buf_dma_handle != NULL) {
13394 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13395 			    DDI_DMA_SYNC_FORKERNEL);
13396 			ASSERT(rval == DDI_SUCCESS);
13397 		}
13398 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir,
13399 		    sizeof (struct read_log_ext_directory));
13400 		rval = 0;
13401 	}
13402 
13403 fail:
13404 	/* Free allocated resources */
13405 	sata_free_local_buffer(spx);
13406 	sata_pkt_free(spx);
13407 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13408 
13409 	return (rval);
13410 }
13411 
13412 /*
13413  * Set up error retrieval sata command for NCQ command error data
13414  * recovery.
13415  *
13416  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
13417  * returns SATA_FAILURE otherwise.
13418  */
13419 static int
13420 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
13421 {
13422 #ifndef __lock_lint
13423 	_NOTE(ARGUNUSED(sdinfo))
13424 #endif
13425 
13426 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
13427 	sata_cmd_t *scmd;
13428 	struct buf *bp;
13429 
13430 	/* Operation modes are up to the caller */
13431 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13432 
13433 	/* Synchronous mode, no callback - may be changed by the caller */
13434 	spkt->satapkt_comp = NULL;
13435 	spkt->satapkt_time = sata_default_pkt_time;
13436 
13437 	scmd = &spkt->satapkt_cmd;
13438 	bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t));
13439 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
13440 
13441 	/*
13442 	 * Allocate dma_able buffer error data.
13443 	 * Buffer allocation will take care of buffer alignment and other DMA
13444 	 * attributes.
13445 	 */
13446 	bp = sata_alloc_local_buffer(spx,
13447 	    sizeof (struct sata_ncq_error_recovery_page));
13448 	if (bp == NULL)
13449 		return (SATA_FAILURE);
13450 
13451 	bp_mapin(bp); /* make data buffer accessible */
13452 	scmd->satacmd_bp = bp;
13453 
13454 	/*
13455 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
13456 	 * before accessing it. Handle is in usual place in translate struct.
13457 	 */
13458 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
13459 
13460 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
13461 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
13462 
13463 	return (SATA_SUCCESS);
13464 }
13465 
13466 /*
13467  * sata_xlate_errors() is used to translate (S)ATA error
13468  * information to SCSI information returned in the SCSI
13469  * packet.
13470  */
13471 static void
13472 sata_xlate_errors(sata_pkt_txlate_t *spx)
13473 {
13474 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
13475 	struct scsi_extended_sense *sense;
13476 
13477 	scsipkt->pkt_reason = CMD_INCOMPLETE;
13478 	*scsipkt->pkt_scbp = STATUS_CHECK;
13479 	sense = sata_arq_sense(spx);
13480 
13481 	switch (spx->txlt_sata_pkt->satapkt_reason) {
13482 	case SATA_PKT_PORT_ERROR:
13483 		/*
13484 		 * We have no device data. Assume no data transfered.
13485 		 */
13486 		sense->es_key = KEY_HARDWARE_ERROR;
13487 		break;
13488 
13489 	case SATA_PKT_DEV_ERROR:
13490 		if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
13491 		    SATA_STATUS_ERR) {
13492 			/*
13493 			 * determine dev error reason from error
13494 			 * reg content
13495 			 */
13496 			sata_decode_device_error(spx, sense);
13497 			break;
13498 		}
13499 		/* No extended sense key - no info available */
13500 		break;
13501 
13502 	case SATA_PKT_TIMEOUT:
13503 		scsipkt->pkt_reason = CMD_TIMEOUT;
13504 		scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET;
13505 		/* No extended sense key */
13506 		break;
13507 
13508 	case SATA_PKT_ABORTED:
13509 		scsipkt->pkt_reason = CMD_ABORTED;
13510 		scsipkt->pkt_statistics |= STAT_ABORTED;
13511 		/* No extended sense key */
13512 		break;
13513 
13514 	case SATA_PKT_RESET:
13515 		/*
13516 		 * pkt aborted either by an explicit reset request from
13517 		 * a host, or due to error recovery
13518 		 */
13519 		scsipkt->pkt_reason = CMD_RESET;
13520 		scsipkt->pkt_statistics |= STAT_DEV_RESET;
13521 		break;
13522 
13523 	default:
13524 		scsipkt->pkt_reason = CMD_TRAN_ERR;
13525 		break;
13526 	}
13527 }
13528 
13529 
13530 
13531 
13532 /*
13533  * Log sata message
13534  * dev pathname msg line preceeds the logged message.
13535  */
13536 
13537 static	void
13538 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...)
13539 {
13540 	char pathname[128];
13541 	dev_info_t *dip;
13542 	va_list ap;
13543 
13544 	mutex_enter(&sata_log_mutex);
13545 
13546 	va_start(ap, fmt);
13547 	(void) vsprintf(sata_log_buf, fmt, ap);
13548 	va_end(ap);
13549 
13550 	if (sata_hba_inst != NULL) {
13551 		dip = SATA_DIP(sata_hba_inst);
13552 		(void) ddi_pathname(dip, pathname);
13553 	} else {
13554 		pathname[0] = 0;
13555 	}
13556 	if (level == CE_CONT) {
13557 		if (sata_debug_flags == 0)
13558 			cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf);
13559 		else
13560 			cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf);
13561 	} else {
13562 		if (level != CE_NOTE) {
13563 			cmn_err(level, "%s:\n %s", pathname, sata_log_buf);
13564 		} else if (sata_msg) {
13565 			cmn_err(level, "%s:\n %s", pathname,
13566 			    sata_log_buf);
13567 		}
13568 	}
13569 
13570 	mutex_exit(&sata_log_mutex);
13571 }
13572 
13573 
13574 /* ******** Asynchronous HBA events handling & hotplugging support ******** */
13575 
13576 /*
13577  * Start or terminate the thread, depending on flag arg and current state
13578  */
13579 static void
13580 sata_event_thread_control(int startstop)
13581 {
13582 	static 	int sata_event_thread_terminating = 0;
13583 	static 	int sata_event_thread_starting = 0;
13584 	int i;
13585 
13586 	mutex_enter(&sata_event_mutex);
13587 
13588 	if (startstop == 0 && (sata_event_thread_starting == 1 ||
13589 	    sata_event_thread_terminating == 1)) {
13590 		mutex_exit(&sata_event_mutex);
13591 		return;
13592 	}
13593 	if (startstop == 1 && sata_event_thread_starting == 1) {
13594 		mutex_exit(&sata_event_mutex);
13595 		return;
13596 	}
13597 	if (startstop == 1 && sata_event_thread_terminating == 1) {
13598 		sata_event_thread_starting = 1;
13599 		/* wait til terminate operation completes */
13600 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
13601 		while (sata_event_thread_terminating == 1) {
13602 			if (i-- <= 0) {
13603 				sata_event_thread_starting = 0;
13604 				mutex_exit(&sata_event_mutex);
13605 #ifdef SATA_DEBUG
13606 				cmn_err(CE_WARN, "sata_event_thread_control: "
13607 				    "timeout waiting for thread to terminate");
13608 #endif
13609 				return;
13610 			}
13611 			mutex_exit(&sata_event_mutex);
13612 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
13613 			mutex_enter(&sata_event_mutex);
13614 		}
13615 	}
13616 	if (startstop == 1) {
13617 		if (sata_event_thread == NULL) {
13618 			sata_event_thread = thread_create(NULL, 0,
13619 			    (void (*)())sata_event_daemon,
13620 			    &sata_hba_list, 0, &p0, TS_RUN, minclsyspri);
13621 		}
13622 		sata_event_thread_starting = 0;
13623 		mutex_exit(&sata_event_mutex);
13624 		return;
13625 	}
13626 
13627 	/*
13628 	 * If we got here, thread may need to be terminated
13629 	 */
13630 	if (sata_event_thread != NULL) {
13631 		int i;
13632 		/* Signal event thread to go away */
13633 		sata_event_thread_terminating = 1;
13634 		sata_event_thread_terminate = 1;
13635 		cv_signal(&sata_event_cv);
13636 		/*
13637 		 * Wait til daemon terminates.
13638 		 */
13639 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
13640 		while (sata_event_thread_terminate == 1) {
13641 			mutex_exit(&sata_event_mutex);
13642 			if (i-- <= 0) {
13643 				/* Daemon did not go away !!! */
13644 #ifdef SATA_DEBUG
13645 				cmn_err(CE_WARN, "sata_event_thread_control: "
13646 				    "cannot terminate event daemon thread");
13647 #endif
13648 				mutex_enter(&sata_event_mutex);
13649 				break;
13650 			}
13651 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
13652 			mutex_enter(&sata_event_mutex);
13653 		}
13654 		sata_event_thread_terminating = 0;
13655 	}
13656 	ASSERT(sata_event_thread_terminating == 0);
13657 	ASSERT(sata_event_thread_starting == 0);
13658 	mutex_exit(&sata_event_mutex);
13659 }
13660 
13661 
13662 /*
13663  * SATA HBA event notification function.
13664  * Events reported by SATA HBA drivers per HBA instance relate to a change in
13665  * a port and/or device state or a controller itself.
13666  * Events for different addresses/addr types cannot be combined.
13667  * A warning message is generated for each event type.
13668  * Events are not processed by this function, so only the
13669  * event flag(s)is set for an affected entity and the event thread is
13670  * waken up. Event daemon thread processes all events.
13671  *
13672  * NOTE: Since more than one event may be reported at the same time, one
13673  * cannot determine a sequence of events when opposite event are reported, eg.
13674  * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing
13675  * is taking precedence over reported events, i.e. may cause ignoring some
13676  * events.
13677  */
13678 #define	SATA_EVENT_MAX_MSG_LENGTH	79
13679 
13680 void
13681 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event)
13682 {
13683 	sata_hba_inst_t *sata_hba_inst = NULL;
13684 	sata_address_t *saddr;
13685 	sata_drive_info_t *sdinfo;
13686 	sata_port_stats_t *pstats;
13687 	int cport, pmport;
13688 	char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1];
13689 	char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1];
13690 	char *lcp;
13691 	static char *err_msg_evnt_1 =
13692 	    "sata_hba_event_notify: invalid port event 0x%x ";
13693 	static char *err_msg_evnt_2 =
13694 	    "sata_hba_event_notify: invalid device event 0x%x ";
13695 	int linkevent;
13696 
13697 	/*
13698 	 * There is a possibility that an event will be generated on HBA
13699 	 * that has not completed attachment or is detaching.
13700 	 * HBA driver should prevent this, but just in case it does not,
13701 	 * we need to ignore events for such HBA.
13702 	 */
13703 	mutex_enter(&sata_mutex);
13704 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13705 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13706 		if (SATA_DIP(sata_hba_inst) == dip)
13707 			if (sata_hba_inst->satahba_attached == 1)
13708 				break;
13709 	}
13710 	mutex_exit(&sata_mutex);
13711 	if (sata_hba_inst == NULL)
13712 		/* HBA not attached */
13713 		return;
13714 
13715 	ASSERT(sata_device != NULL);
13716 
13717 	/*
13718 	 * Validate address before - do not proceed with invalid address.
13719 	 */
13720 	saddr = &sata_device->satadev_addr;
13721 	if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst))
13722 		return;
13723 	if (saddr->qual == SATA_ADDR_PMPORT ||
13724 	    saddr->qual == SATA_ADDR_DPMPORT)
13725 		/* Port Multiplier not supported yet */
13726 		return;
13727 
13728 	cport = saddr->cport;
13729 	pmport = saddr->pmport;
13730 
13731 	buf1[0] = buf2[0] = '\0';
13732 
13733 	/*
13734 	 * Events refer to devices, ports and controllers - each has
13735 	 * unique address. Events for different addresses cannot be combined.
13736 	 */
13737 	if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) {
13738 
13739 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13740 
13741 		/* qualify this event(s) */
13742 		if ((event & SATA_EVNT_PORT_EVENTS) == 0) {
13743 			/* Invalid event for the device port */
13744 			(void) sprintf(buf2, err_msg_evnt_1,
13745 			    event & SATA_EVNT_PORT_EVENTS);
13746 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13747 			goto event_info;
13748 		}
13749 		if (saddr->qual == SATA_ADDR_CPORT) {
13750 			/* Controller's device port event */
13751 
13752 			(SATA_CPORT_INFO(sata_hba_inst, cport))->
13753 			    cport_event_flags |=
13754 			    event & SATA_EVNT_PORT_EVENTS;
13755 			pstats =
13756 			    &(SATA_CPORT_INFO(sata_hba_inst, cport))->
13757 			    cport_stats;
13758 		} else {
13759 			/* Port multiplier's device port event */
13760 			(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
13761 			    pmport_event_flags |=
13762 			    event & SATA_EVNT_PORT_EVENTS;
13763 			pstats =
13764 			    &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
13765 			    pmport_stats;
13766 		}
13767 
13768 		/*
13769 		 * Add to statistics and log the message. We have to do it
13770 		 * here rather than in the event daemon, because there may be
13771 		 * multiple events occuring before they are processed.
13772 		 */
13773 		linkevent = event &
13774 		    (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED);
13775 		if (linkevent) {
13776 			if (linkevent == (SATA_EVNT_LINK_LOST |
13777 			    SATA_EVNT_LINK_ESTABLISHED)) {
13778 				/* This is likely event combination */
13779 				(void) strlcat(buf1, "link lost/established, ",
13780 				    SATA_EVENT_MAX_MSG_LENGTH);
13781 
13782 				if (pstats->link_lost < 0xffffffffffffffffULL)
13783 					pstats->link_lost++;
13784 				if (pstats->link_established <
13785 				    0xffffffffffffffffULL)
13786 					pstats->link_established++;
13787 				linkevent = 0;
13788 			} else if (linkevent & SATA_EVNT_LINK_LOST) {
13789 				(void) strlcat(buf1, "link lost, ",
13790 				    SATA_EVENT_MAX_MSG_LENGTH);
13791 
13792 				if (pstats->link_lost < 0xffffffffffffffffULL)
13793 					pstats->link_lost++;
13794 			} else {
13795 				(void) strlcat(buf1, "link established, ",
13796 				    SATA_EVENT_MAX_MSG_LENGTH);
13797 				if (pstats->link_established <
13798 				    0xffffffffffffffffULL)
13799 					pstats->link_established++;
13800 			}
13801 		}
13802 		if (event & SATA_EVNT_DEVICE_ATTACHED) {
13803 			(void) strlcat(buf1, "device attached, ",
13804 			    SATA_EVENT_MAX_MSG_LENGTH);
13805 			if (pstats->device_attached < 0xffffffffffffffffULL)
13806 				pstats->device_attached++;
13807 		}
13808 		if (event & SATA_EVNT_DEVICE_DETACHED) {
13809 			(void) strlcat(buf1, "device detached, ",
13810 			    SATA_EVENT_MAX_MSG_LENGTH);
13811 			if (pstats->device_detached < 0xffffffffffffffffULL)
13812 				pstats->device_detached++;
13813 		}
13814 		if (event & SATA_EVNT_PWR_LEVEL_CHANGED) {
13815 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
13816 			    "port %d power level changed", cport);
13817 			if (pstats->port_pwr_changed < 0xffffffffffffffffULL)
13818 				pstats->port_pwr_changed++;
13819 		}
13820 
13821 		if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) {
13822 			/* There should be no other events for this address */
13823 			(void) sprintf(buf2, err_msg_evnt_1,
13824 			    event & ~SATA_EVNT_PORT_EVENTS);
13825 		}
13826 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13827 
13828 	} else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) {
13829 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13830 
13831 		/* qualify this event */
13832 		if ((event & SATA_EVNT_DEVICE_RESET) == 0) {
13833 			/* Invalid event for a device */
13834 			(void) sprintf(buf2, err_msg_evnt_2,
13835 			    event & SATA_EVNT_DEVICE_RESET);
13836 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13837 			goto event_info;
13838 		}
13839 		/* drive event */
13840 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
13841 		if (sdinfo != NULL) {
13842 			if (event & SATA_EVNT_DEVICE_RESET) {
13843 				(void) strlcat(buf1, "device reset, ",
13844 				    SATA_EVENT_MAX_MSG_LENGTH);
13845 				if (sdinfo->satadrv_stats.drive_reset <
13846 				    0xffffffffffffffffULL)
13847 					sdinfo->satadrv_stats.drive_reset++;
13848 				sdinfo->satadrv_event_flags |=
13849 				    SATA_EVNT_DEVICE_RESET;
13850 			}
13851 		}
13852 		if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) {
13853 			/* Invalid event for a device */
13854 			(void) sprintf(buf2, err_msg_evnt_2,
13855 			    event & ~SATA_EVNT_DRIVE_EVENTS);
13856 		}
13857 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13858 	} else {
13859 		if (saddr->qual != SATA_ADDR_NULL) {
13860 			/* Wrong address qualifier */
13861 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13862 			    "sata_hba_event_notify: invalid address 0x%x",
13863 			    *(uint32_t *)saddr));
13864 			return;
13865 		}
13866 		if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 ||
13867 		    (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) {
13868 			/* Invalid event for the controller */
13869 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13870 			    "sata_hba_event_notify: invalid event 0x%x for "
13871 			    "controller",
13872 			    event & SATA_EVNT_CONTROLLER_EVENTS));
13873 			return;
13874 		}
13875 		buf1[0] = '\0';
13876 		/* This may be a frequent and not interesting event */
13877 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
13878 		    "controller power level changed\n", NULL);
13879 
13880 		mutex_enter(&sata_hba_inst->satahba_mutex);
13881 		if (sata_hba_inst->satahba_stats.ctrl_pwr_change <
13882 		    0xffffffffffffffffULL)
13883 			sata_hba_inst->satahba_stats.ctrl_pwr_change++;
13884 
13885 		sata_hba_inst->satahba_event_flags |=
13886 		    SATA_EVNT_PWR_LEVEL_CHANGED;
13887 		mutex_exit(&sata_hba_inst->satahba_mutex);
13888 	}
13889 	/*
13890 	 * If we got here, there is something to do with this HBA
13891 	 * instance.
13892 	 */
13893 	mutex_enter(&sata_hba_inst->satahba_mutex);
13894 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
13895 	mutex_exit(&sata_hba_inst->satahba_mutex);
13896 	mutex_enter(&sata_mutex);
13897 	sata_event_pending |= SATA_EVNT_MAIN;	/* global event indicator */
13898 	mutex_exit(&sata_mutex);
13899 
13900 	/* Tickle event thread */
13901 	mutex_enter(&sata_event_mutex);
13902 	if (sata_event_thread_active == 0)
13903 		cv_signal(&sata_event_cv);
13904 	mutex_exit(&sata_event_mutex);
13905 
13906 event_info:
13907 	if (buf1[0] != '\0') {
13908 		lcp = strrchr(buf1, ',');
13909 		if (lcp != NULL)
13910 			*lcp = '\0';
13911 	}
13912 	if (saddr->qual == SATA_ADDR_CPORT ||
13913 	    saddr->qual == SATA_ADDR_DCPORT) {
13914 		if (buf1[0] != '\0') {
13915 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
13916 			    cport, buf1);
13917 		}
13918 		if (buf2[0] != '\0') {
13919 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
13920 			    cport, buf2);
13921 		}
13922 	} else if (saddr->qual == SATA_ADDR_PMPORT ||
13923 	    saddr->qual == SATA_ADDR_DPMPORT) {
13924 		if (buf1[0] != '\0') {
13925 			sata_log(sata_hba_inst, CE_NOTE,
13926 			    "port %d pmport %d: %s\n", cport, pmport, buf1);
13927 		}
13928 		if (buf2[0] != '\0') {
13929 			sata_log(sata_hba_inst, CE_NOTE,
13930 			    "port %d pmport %d: %s\n", cport, pmport, buf2);
13931 		}
13932 	}
13933 }
13934 
13935 
13936 /*
13937  * Event processing thread.
13938  * Arg is a pointer to the sata_hba_list pointer.
13939  * It is not really needed, because sata_hba_list is global and static
13940  */
13941 static void
13942 sata_event_daemon(void *arg)
13943 {
13944 #ifndef __lock_lint
13945 	_NOTE(ARGUNUSED(arg))
13946 #endif
13947 	sata_hba_inst_t *sata_hba_inst;
13948 	clock_t lbolt;
13949 
13950 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13951 	    "SATA event daemon started\n", NULL);
13952 loop:
13953 	/*
13954 	 * Process events here. Walk through all registered HBAs
13955 	 */
13956 	mutex_enter(&sata_mutex);
13957 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13958 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13959 		ASSERT(sata_hba_inst != NULL);
13960 		mutex_enter(&sata_hba_inst->satahba_mutex);
13961 		if (sata_hba_inst->satahba_attached != 1 ||
13962 		    (sata_hba_inst->satahba_event_flags &
13963 		    SATA_EVNT_SKIP) != 0) {
13964 			mutex_exit(&sata_hba_inst->satahba_mutex);
13965 			continue;
13966 		}
13967 		if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) {
13968 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP;
13969 			mutex_exit(&sata_hba_inst->satahba_mutex);
13970 			mutex_exit(&sata_mutex);
13971 			/* Got the controller with pending event */
13972 			sata_process_controller_events(sata_hba_inst);
13973 			/*
13974 			 * Since global mutex was released, there is a
13975 			 * possibility that HBA list has changed, so start
13976 			 * over from the top. Just processed controller
13977 			 * will be passed-over because of the SKIP flag.
13978 			 */
13979 			goto loop;
13980 		}
13981 		mutex_exit(&sata_hba_inst->satahba_mutex);
13982 	}
13983 	/* Clear SKIP flag in all controllers */
13984 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13985 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13986 		mutex_enter(&sata_hba_inst->satahba_mutex);
13987 		sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP;
13988 		mutex_exit(&sata_hba_inst->satahba_mutex);
13989 	}
13990 	mutex_exit(&sata_mutex);
13991 
13992 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13993 	    "SATA EVENT DAEMON suspending itself", NULL);
13994 
13995 #ifdef SATA_DEBUG
13996 	if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) {
13997 		sata_log(sata_hba_inst, CE_WARN,
13998 		    "SATA EVENTS PROCESSING DISABLED\n");
13999 		thread_exit(); /* Daemon will not run again */
14000 	}
14001 #endif
14002 	mutex_enter(&sata_event_mutex);
14003 	sata_event_thread_active = 0;
14004 	mutex_exit(&sata_event_mutex);
14005 	/*
14006 	 * Go to sleep/suspend itself and wake up either because new event or
14007 	 * wait timeout. Exit if there is a termination request (driver
14008 	 * unload).
14009 	 */
14010 	do {
14011 		lbolt = ddi_get_lbolt();
14012 		lbolt += drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME);
14013 		mutex_enter(&sata_event_mutex);
14014 		(void) cv_timedwait(&sata_event_cv, &sata_event_mutex, lbolt);
14015 
14016 		if (sata_event_thread_active != 0) {
14017 			mutex_exit(&sata_event_mutex);
14018 			continue;
14019 		}
14020 
14021 		/* Check if it is time to go away */
14022 		if (sata_event_thread_terminate == 1) {
14023 			/*
14024 			 * It is up to the thread setting above flag to make
14025 			 * sure that this thread is not killed prematurely.
14026 			 */
14027 			sata_event_thread_terminate = 0;
14028 			sata_event_thread = NULL;
14029 			mutex_exit(&sata_event_mutex);
14030 			SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
14031 			    "SATA_EVENT_DAEMON_TERMINATING", NULL);
14032 			thread_exit();  { _NOTE(NOT_REACHED) }
14033 		}
14034 		mutex_exit(&sata_event_mutex);
14035 	} while (!(sata_event_pending & SATA_EVNT_MAIN));
14036 
14037 	mutex_enter(&sata_event_mutex);
14038 	sata_event_thread_active = 1;
14039 	mutex_exit(&sata_event_mutex);
14040 
14041 	mutex_enter(&sata_mutex);
14042 	sata_event_pending &= ~SATA_EVNT_MAIN;
14043 	mutex_exit(&sata_mutex);
14044 
14045 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
14046 	    "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL);
14047 
14048 	goto loop;
14049 }
14050 
14051 /*
14052  * Specific HBA instance event processing.
14053  *
14054  * NOTE: At the moment, device event processing is limited to hard disks
14055  * only.
14056  * cports only are supported - no pmports.
14057  */
14058 static void
14059 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst)
14060 {
14061 	int ncport;
14062 	uint32_t event_flags;
14063 	sata_address_t *saddr;
14064 
14065 	SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst,
14066 	    "Processing controller %d event(s)",
14067 	    ddi_get_instance(SATA_DIP(sata_hba_inst)));
14068 
14069 	mutex_enter(&sata_hba_inst->satahba_mutex);
14070 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN;
14071 	event_flags = sata_hba_inst->satahba_event_flags;
14072 	mutex_exit(&sata_hba_inst->satahba_mutex);
14073 	/*
14074 	 * Process controller power change first
14075 	 * HERE
14076 	 */
14077 	if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED)
14078 		sata_process_cntrl_pwr_level_change(sata_hba_inst);
14079 
14080 	/*
14081 	 * Search through ports/devices to identify affected port/device.
14082 	 * We may have to process events for more than one port/device.
14083 	 */
14084 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
14085 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14086 		event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))->
14087 		    cport_event_flags;
14088 		/* Check if port was locked by IOCTL processing */
14089 		if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) {
14090 			/*
14091 			 * We ignore port events because port is busy
14092 			 * with AP control processing. Set again
14093 			 * controller and main event flag, so that
14094 			 * events may be processed by the next daemon
14095 			 * run.
14096 			 */
14097 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14098 			mutex_enter(&sata_hba_inst->satahba_mutex);
14099 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14100 			mutex_exit(&sata_hba_inst->satahba_mutex);
14101 			mutex_enter(&sata_mutex);
14102 			sata_event_pending |= SATA_EVNT_MAIN;
14103 			mutex_exit(&sata_mutex);
14104 			SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst,
14105 			    "Event processing postponed until "
14106 			    "AP control processing completes",
14107 			    NULL);
14108 			/* Check other ports */
14109 			continue;
14110 		} else {
14111 			/*
14112 			 * Set BSY flag so that AP control would not
14113 			 * interfere with events processing for
14114 			 * this port.
14115 			 */
14116 			(SATA_CPORT_INFO(sata_hba_inst, ncport))->
14117 			    cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY;
14118 		}
14119 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14120 
14121 		saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr;
14122 
14123 		if ((event_flags &
14124 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
14125 			/*
14126 			 * Got port event.
14127 			 * We need some hierarchy of event processing as they
14128 			 * are affecting each other:
14129 			 * 1. port failed
14130 			 * 2. device detached/attached
14131 			 * 3. link events - link events may trigger device
14132 			 *    detached or device attached events in some
14133 			 *    circumstances.
14134 			 * 4. port power level changed
14135 			 */
14136 			if (event_flags & SATA_EVNT_PORT_FAILED) {
14137 				sata_process_port_failed_event(sata_hba_inst,
14138 				    saddr);
14139 			}
14140 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
14141 				sata_process_device_detached(sata_hba_inst,
14142 				    saddr);
14143 			}
14144 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
14145 				sata_process_device_attached(sata_hba_inst,
14146 				    saddr);
14147 			}
14148 			if (event_flags &
14149 			    (SATA_EVNT_LINK_ESTABLISHED |
14150 			    SATA_EVNT_LINK_LOST)) {
14151 				sata_process_port_link_events(sata_hba_inst,
14152 				    saddr);
14153 			}
14154 			if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) {
14155 				sata_process_port_pwr_change(sata_hba_inst,
14156 				    saddr);
14157 			}
14158 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
14159 				sata_process_target_node_cleanup(
14160 				    sata_hba_inst, saddr);
14161 			}
14162 			if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) {
14163 				sata_process_device_autoonline(
14164 				    sata_hba_inst, saddr);
14165 			}
14166 		}
14167 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14168 		if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) !=
14169 		    SATA_DTYPE_NONE) &&
14170 		    (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) {
14171 			if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)->
14172 			    satadrv_event_flags &
14173 			    (SATA_EVNT_DEVICE_RESET |
14174 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
14175 				/* Have device event */
14176 				sata_process_device_reset(sata_hba_inst,
14177 				    saddr);
14178 			}
14179 		}
14180 		/* Release PORT_BUSY flag */
14181 		(SATA_CPORT_INFO(sata_hba_inst, ncport))->
14182 		    cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
14183 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14184 
14185 	} /* End of loop through the controller SATA ports */
14186 }
14187 
14188 /*
14189  * Process HBA power level change reported by HBA driver.
14190  * Not implemented at this time - event is ignored.
14191  */
14192 static void
14193 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst)
14194 {
14195 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14196 	    "Processing controller power level change", NULL);
14197 
14198 	/* Ignoring it for now */
14199 	mutex_enter(&sata_hba_inst->satahba_mutex);
14200 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
14201 	mutex_exit(&sata_hba_inst->satahba_mutex);
14202 }
14203 
14204 /*
14205  * Process port power level change reported by HBA driver.
14206  * Not implemented at this time - event is ignored.
14207  */
14208 static void
14209 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst,
14210     sata_address_t *saddr)
14211 {
14212 	sata_cport_info_t *cportinfo;
14213 
14214 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14215 	    "Processing port power level change", NULL);
14216 
14217 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14218 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14219 	/* Reset event flag */
14220 	cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
14221 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14222 }
14223 
14224 /*
14225  * Process port failure reported by HBA driver.
14226  * cports support only - no pmports.
14227  */
14228 static void
14229 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst,
14230     sata_address_t *saddr)
14231 {
14232 	sata_cport_info_t *cportinfo;
14233 
14234 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14235 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14236 	/* Reset event flag first */
14237 	cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED;
14238 	/* If the port is in SHUTDOWN or FAILED state, ignore this event. */
14239 	if ((cportinfo->cport_state &
14240 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) {
14241 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14242 		    cport_mutex);
14243 		return;
14244 	}
14245 	/* Fail the port */
14246 	cportinfo->cport_state = SATA_PSTATE_FAILED;
14247 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14248 	sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport);
14249 }
14250 
14251 /*
14252  * Device Reset Event processing.
14253  * The seqeunce is managed by 3 stage flags:
14254  * - reset event reported,
14255  * - reset event being processed,
14256  * - request to clear device reset state.
14257  *
14258  * NOTE: This function has to be entered with cport mutex held. It exits with
14259  * mutex held as well, but can release mutex during the processing.
14260  */
14261 static void
14262 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst,
14263     sata_address_t *saddr)
14264 {
14265 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
14266 	sata_drive_info_t *sdinfo;
14267 	sata_cport_info_t *cportinfo;
14268 	sata_device_t sata_device;
14269 	int rval;
14270 
14271 	/* We only care about host sata cport for now */
14272 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14273 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14274 	/*
14275 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
14276 	 * state, ignore reset event.
14277 	 */
14278 	if (((cportinfo->cport_state &
14279 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
14280 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
14281 		sdinfo->satadrv_event_flags &=
14282 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
14283 		return;
14284 	}
14285 
14286 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) &
14287 	    SATA_VALID_DEV_TYPE) == 0) {
14288 		/*
14289 		 * This should not happen - coding error.
14290 		 * But we can recover, so do not panic, just clean up
14291 		 * and if in debug mode, log the message.
14292 		 */
14293 #ifdef SATA_DEBUG
14294 		sata_log(sata_hba_inst, CE_WARN,
14295 		    "sata_process_device_reset: "
14296 		    "Invalid device type with sdinfo!", NULL);
14297 #endif
14298 		sdinfo->satadrv_event_flags = 0;
14299 		return;
14300 	}
14301 
14302 #ifdef SATA_DEBUG
14303 	if ((sdinfo->satadrv_event_flags &
14304 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
14305 		/* Nothing to do */
14306 		/* Something is weird - why we are processing dev reset? */
14307 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14308 		    "No device reset event!!!!", NULL);
14309 
14310 		return;
14311 	}
14312 	if ((sdinfo->satadrv_event_flags &
14313 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
14314 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
14315 		/* Something is weird - new device reset event */
14316 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14317 		    "Overlapping device reset events!", NULL);
14318 	}
14319 #endif
14320 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14321 	    "Processing port %d device reset", saddr->cport);
14322 
14323 	/* Clear event flag */
14324 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
14325 
14326 	/* It seems that we always need to check the port state first */
14327 	sata_device.satadev_rev = SATA_DEVICE_REV;
14328 	sata_device.satadev_addr = *saddr;
14329 	/*
14330 	 * We have to exit mutex, because the HBA probe port function may
14331 	 * block on its own mutex.
14332 	 */
14333 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14334 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14335 	    (SATA_DIP(sata_hba_inst), &sata_device);
14336 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14337 	sata_update_port_info(sata_hba_inst, &sata_device);
14338 	if (rval != SATA_SUCCESS) {
14339 		/* Something went wrong? Fail the port */
14340 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14341 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14342 		if (sdinfo != NULL)
14343 			sdinfo->satadrv_event_flags = 0;
14344 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14345 		    cport_mutex);
14346 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14347 		    "SATA port %d probing failed",
14348 		    saddr->cport));
14349 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14350 		    saddr->cport)->cport_mutex);
14351 		return;
14352 	}
14353 	if ((sata_device.satadev_scr.sstatus  &
14354 	    SATA_PORT_DEVLINK_UP_MASK) !=
14355 	    SATA_PORT_DEVLINK_UP ||
14356 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
14357 		/*
14358 		 * No device to process, anymore. Some other event processing
14359 		 * would or have already performed port info cleanup.
14360 		 * To be safe (HBA may need it), request clearing device
14361 		 * reset condition.
14362 		 */
14363 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14364 		if (sdinfo != NULL) {
14365 			sdinfo->satadrv_event_flags &=
14366 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
14367 			sdinfo->satadrv_event_flags |=
14368 			    SATA_EVNT_CLEAR_DEVICE_RESET;
14369 		}
14370 		return;
14371 	}
14372 
14373 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14374 	if (sdinfo == NULL) {
14375 		return;
14376 	}
14377 	if ((sdinfo->satadrv_event_flags &
14378 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
14379 		/*
14380 		 * Start tracking time for device feature restoration and
14381 		 * identification. Save current time (lbolt value).
14382 		 */
14383 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
14384 	}
14385 	/* Mark device reset processing as active */
14386 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
14387 
14388 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
14389 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14390 
14391 	if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) ==
14392 	    SATA_FAILURE) {
14393 		/*
14394 		 * Restoring drive setting failed.
14395 		 * Probe the port first, to check if the port state has changed
14396 		 */
14397 		sata_device.satadev_rev = SATA_DEVICE_REV;
14398 		sata_device.satadev_addr = *saddr;
14399 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
14400 		/* probe port */
14401 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14402 		    (SATA_DIP(sata_hba_inst), &sata_device);
14403 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14404 		    cport_mutex);
14405 		if (rval == SATA_SUCCESS &&
14406 		    (sata_device.satadev_state &
14407 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
14408 		    (sata_device.satadev_scr.sstatus  &
14409 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
14410 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
14411 			/*
14412 			 * We may retry this a bit later - in-process reset
14413 			 * condition should be already set.
14414 			 * Track retry time for device identification.
14415 			 */
14416 			if ((cportinfo->cport_dev_type &
14417 			    SATA_VALID_DEV_TYPE) != 0 &&
14418 			    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL &&
14419 			    sdinfo->satadrv_reset_time != 0) {
14420 				clock_t cur_time = ddi_get_lbolt();
14421 				/*
14422 				 * If the retry time limit was not
14423 				 * exceeded, retry.
14424 				 */
14425 				if ((cur_time - sdinfo->satadrv_reset_time) <
14426 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
14427 					mutex_enter(
14428 					    &sata_hba_inst->satahba_mutex);
14429 					sata_hba_inst->satahba_event_flags |=
14430 					    SATA_EVNT_MAIN;
14431 					mutex_exit(
14432 					    &sata_hba_inst->satahba_mutex);
14433 					mutex_enter(&sata_mutex);
14434 					sata_event_pending |= SATA_EVNT_MAIN;
14435 					mutex_exit(&sata_mutex);
14436 					return;
14437 				}
14438 			}
14439 			/* Fail the drive */
14440 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
14441 
14442 			sata_log(sata_hba_inst, CE_WARN,
14443 			    "SATA device at port %d - device failed",
14444 			    saddr->cport);
14445 		} else {
14446 			/*
14447 			 * No point of retrying - some other event processing
14448 			 * would or already did port info cleanup.
14449 			 * To be safe (HBA may need it),
14450 			 * request clearing device reset condition.
14451 			 */
14452 			sdinfo->satadrv_event_flags |=
14453 			    SATA_EVNT_CLEAR_DEVICE_RESET;
14454 		}
14455 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
14456 		sdinfo->satadrv_reset_time = 0;
14457 		return;
14458 	}
14459 	/*
14460 	 * Raise the flag indicating that the next sata command could
14461 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
14462 	 * reset is reported.
14463 	 */
14464 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14465 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14466 		sdinfo->satadrv_reset_time = 0;
14467 		if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) {
14468 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14469 			sdinfo->satadrv_event_flags &=
14470 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
14471 			sdinfo->satadrv_event_flags |=
14472 			    SATA_EVNT_CLEAR_DEVICE_RESET;
14473 		}
14474 	}
14475 }
14476 
14477 
14478 /*
14479  * Port Link Events processing.
14480  * Every link established event may involve device reset (due to
14481  * COMRESET signal, equivalent of the hard reset) so arbitrarily
14482  * set device reset event for an attached device (if any).
14483  * If the port is in SHUTDOWN or FAILED state, ignore link events.
14484  *
14485  * The link established event processing varies, depending on the state
14486  * of the target node, HBA hotplugging capabilities, state of the port.
14487  * If the link is not active, the link established event is ignored.
14488  * If HBA cannot detect device attachment and there is no target node,
14489  * the link established event triggers device attach event processing.
14490  * Else, link established event triggers device reset event processing.
14491  *
14492  * The link lost event processing varies, depending on a HBA hotplugging
14493  * capability and the state of the port (link active or not active).
14494  * If the link is active, the lost link event is ignored.
14495  * If HBA cannot detect device removal, the lost link event triggers
14496  * device detached event processing after link lost timeout.
14497  * Else, the event is ignored.
14498  *
14499  * NOTE: Only cports are processed for now, i.e. no port multiplier ports
14500  */
14501 static void
14502 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst,
14503     sata_address_t *saddr)
14504 {
14505 	sata_device_t sata_device;
14506 	sata_cport_info_t *cportinfo;
14507 	sata_drive_info_t *sdinfo;
14508 	uint32_t event_flags;
14509 	int rval;
14510 
14511 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14512 	    "Processing port %d link event(s)", saddr->cport);
14513 
14514 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14515 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14516 	event_flags = cportinfo->cport_event_flags;
14517 
14518 	/* Reset event flags first */
14519 	cportinfo->cport_event_flags &=
14520 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
14521 
14522 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
14523 	if ((cportinfo->cport_state &
14524 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14525 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14526 		    cport_mutex);
14527 		return;
14528 	}
14529 
14530 	/*
14531 	 * For the sanity sake get current port state.
14532 	 * Set device address only. Other sata_device fields should be
14533 	 * set by HBA driver.
14534 	 */
14535 	sata_device.satadev_rev = SATA_DEVICE_REV;
14536 	sata_device.satadev_addr = *saddr;
14537 	/*
14538 	 * We have to exit mutex, because the HBA probe port function may
14539 	 * block on its own mutex.
14540 	 */
14541 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14542 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14543 	    (SATA_DIP(sata_hba_inst), &sata_device);
14544 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14545 	sata_update_port_info(sata_hba_inst, &sata_device);
14546 	if (rval != SATA_SUCCESS) {
14547 		/* Something went wrong? Fail the port */
14548 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14549 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14550 		    cport_mutex);
14551 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14552 		    "SATA port %d probing failed",
14553 		    saddr->cport));
14554 		/*
14555 		 * We may want to release device info structure, but
14556 		 * it is not necessary.
14557 		 */
14558 		return;
14559 	} else {
14560 		/* port probed successfully */
14561 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14562 	}
14563 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
14564 
14565 		if ((sata_device.satadev_scr.sstatus &
14566 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
14567 			/* Ignore event */
14568 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14569 			    "Ignoring port %d link established event - "
14570 			    "link down",
14571 			    saddr->cport);
14572 			goto linklost;
14573 		}
14574 
14575 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14576 		    "Processing port %d link established event",
14577 		    saddr->cport);
14578 
14579 		/*
14580 		 * For the sanity sake check if a device is attached - check
14581 		 * return state of a port probing.
14582 		 */
14583 		if (sata_device.satadev_type != SATA_DTYPE_NONE &&
14584 		    sata_device.satadev_type != SATA_DTYPE_PMULT) {
14585 			/*
14586 			 * HBA port probe indicated that there is a device
14587 			 * attached. Check if the framework had device info
14588 			 * structure attached for this device.
14589 			 */
14590 			if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
14591 				ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) !=
14592 				    NULL);
14593 
14594 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14595 				if ((sdinfo->satadrv_type &
14596 				    SATA_VALID_DEV_TYPE) != 0) {
14597 					/*
14598 					 * Dev info structure is present.
14599 					 * If dev_type is set to known type in
14600 					 * the framework's drive info struct
14601 					 * then the device existed before and
14602 					 * the link was probably lost
14603 					 * momentarily - in such case
14604 					 * we may want to check device
14605 					 * identity.
14606 					 * Identity check is not supported now.
14607 					 *
14608 					 * Link established event
14609 					 * triggers device reset event.
14610 					 */
14611 					(SATA_CPORTINFO_DRV_INFO(cportinfo))->
14612 					    satadrv_event_flags |=
14613 					    SATA_EVNT_DEVICE_RESET;
14614 				}
14615 			} else if (cportinfo->cport_dev_type ==
14616 			    SATA_DTYPE_NONE) {
14617 				/*
14618 				 * We got new device attached! If HBA does not
14619 				 * generate device attached events, trigger it
14620 				 * here.
14621 				 */
14622 				if (!(SATA_FEATURES(sata_hba_inst) &
14623 				    SATA_CTLF_HOTPLUG)) {
14624 					cportinfo->cport_event_flags |=
14625 					    SATA_EVNT_DEVICE_ATTACHED;
14626 				}
14627 			}
14628 			/* Reset link lost timeout */
14629 			cportinfo->cport_link_lost_time = 0;
14630 		}
14631 	}
14632 linklost:
14633 	if (event_flags & SATA_EVNT_LINK_LOST) {
14634 		if ((sata_device.satadev_scr.sstatus &
14635 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
14636 			/* Ignore event */
14637 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14638 			    "Ignoring port %d link lost event - link is up",
14639 			    saddr->cport);
14640 			goto done;
14641 		}
14642 #ifdef SATA_DEBUG
14643 		if (cportinfo->cport_link_lost_time == 0) {
14644 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14645 			    "Processing port %d link lost event",
14646 			    saddr->cport);
14647 		}
14648 #endif
14649 		/*
14650 		 * When HBA cannot generate device attached/detached events,
14651 		 * we need to track link lost time and eventually generate
14652 		 * device detach event.
14653 		 */
14654 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
14655 			/* We are tracking link lost time */
14656 			if (cportinfo->cport_link_lost_time == 0) {
14657 				/* save current time (lbolt value) */
14658 				cportinfo->cport_link_lost_time =
14659 				    ddi_get_lbolt();
14660 				/* just keep link lost event */
14661 				cportinfo->cport_event_flags |=
14662 				    SATA_EVNT_LINK_LOST;
14663 			} else {
14664 				clock_t cur_time = ddi_get_lbolt();
14665 				if ((cur_time -
14666 				    cportinfo->cport_link_lost_time) >=
14667 				    drv_usectohz(
14668 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
14669 					/* trigger device detach event */
14670 					cportinfo->cport_event_flags |=
14671 					    SATA_EVNT_DEVICE_DETACHED;
14672 					cportinfo->cport_link_lost_time = 0;
14673 					SATADBG1(SATA_DBG_EVENTS,
14674 					    sata_hba_inst,
14675 					    "Triggering port %d "
14676 					    "device detached event",
14677 					    saddr->cport);
14678 				} else {
14679 					/* keep link lost event */
14680 					cportinfo->cport_event_flags |=
14681 					    SATA_EVNT_LINK_LOST;
14682 				}
14683 			}
14684 		}
14685 		/*
14686 		 * We could change port state to disable/delay access to
14687 		 * the attached device until the link is recovered.
14688 		 */
14689 	}
14690 done:
14691 	event_flags = cportinfo->cport_event_flags;
14692 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14693 	if (event_flags != 0) {
14694 		mutex_enter(&sata_hba_inst->satahba_mutex);
14695 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14696 		mutex_exit(&sata_hba_inst->satahba_mutex);
14697 		mutex_enter(&sata_mutex);
14698 		sata_event_pending |= SATA_EVNT_MAIN;
14699 		mutex_exit(&sata_mutex);
14700 	}
14701 }
14702 
14703 /*
14704  * Device Detached Event processing.
14705  * Port is probed to find if a device is really gone. If so,
14706  * the device info structure is detached from the SATA port info structure
14707  * and released.
14708  * Port status is updated.
14709  *
14710  * NOTE: Process cports event only, no port multiplier ports.
14711  */
14712 static void
14713 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst,
14714     sata_address_t *saddr)
14715 {
14716 	sata_cport_info_t *cportinfo;
14717 	sata_drive_info_t *sdevinfo;
14718 	sata_device_t sata_device;
14719 	dev_info_t *tdip;
14720 	int rval;
14721 
14722 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14723 	    "Processing port %d device detached", saddr->cport);
14724 
14725 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14726 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14727 	/* Clear event flag */
14728 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
14729 
14730 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
14731 	if ((cportinfo->cport_state &
14732 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14733 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14734 		    cport_mutex);
14735 		return;
14736 	}
14737 	/* For sanity, re-probe the port */
14738 	sata_device.satadev_rev = SATA_DEVICE_REV;
14739 	sata_device.satadev_addr = *saddr;
14740 
14741 	/*
14742 	 * We have to exit mutex, because the HBA probe port function may
14743 	 * block on its own mutex.
14744 	 */
14745 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14746 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14747 	    (SATA_DIP(sata_hba_inst), &sata_device);
14748 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14749 	sata_update_port_info(sata_hba_inst, &sata_device);
14750 	if (rval != SATA_SUCCESS) {
14751 		/* Something went wrong? Fail the port */
14752 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14753 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14754 		    cport_mutex);
14755 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14756 		    "SATA port %d probing failed",
14757 		    saddr->cport));
14758 		/*
14759 		 * We may want to release device info structure, but
14760 		 * it is not necessary.
14761 		 */
14762 		return;
14763 	} else {
14764 		/* port probed successfully */
14765 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14766 	}
14767 	/*
14768 	 * Check if a device is still attached. For sanity, check also
14769 	 * link status - if no link, there is no device.
14770 	 */
14771 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
14772 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
14773 	    SATA_DTYPE_NONE) {
14774 		/*
14775 		 * Device is still attached - ignore detach event.
14776 		 */
14777 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14778 		    cport_mutex);
14779 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14780 		    "Ignoring detach - device still attached to port %d",
14781 		    sata_device.satadev_addr.cport);
14782 		return;
14783 	}
14784 	/*
14785 	 * We need to detach and release device info structure here
14786 	 */
14787 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14788 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14789 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14790 		(void) kmem_free((void *)sdevinfo,
14791 		    sizeof (sata_drive_info_t));
14792 	}
14793 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14794 	/*
14795 	 * Device cannot be reached anymore, even if the target node may be
14796 	 * still present.
14797 	 */
14798 
14799 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14800 	sata_log(sata_hba_inst, CE_WARN, "SATA device detached at port %d",
14801 	    sata_device.satadev_addr.cport);
14802 
14803 	/*
14804 	 * Try to offline a device and remove target node if it still exists
14805 	 */
14806 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
14807 	if (tdip != NULL) {
14808 		/*
14809 		 * Target node exists.  Unconfigure device then remove
14810 		 * the target node (one ndi operation).
14811 		 */
14812 		if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
14813 			/*
14814 			 * PROBLEM - no device, but target node remained
14815 			 * This happens when the file was open or node was
14816 			 * waiting for resources.
14817 			 */
14818 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14819 			    "sata_process_device_detached: "
14820 			    "Failed to remove target node for "
14821 			    "detached SATA device."));
14822 			/*
14823 			 * Set target node state to DEVI_DEVICE_REMOVED.
14824 			 * But re-check first that the node still exists.
14825 			 */
14826 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
14827 			    saddr->cport);
14828 			if (tdip != NULL) {
14829 				sata_set_device_removed(tdip);
14830 				/*
14831 				 * Instruct event daemon to retry the
14832 				 * cleanup later.
14833 				 */
14834 				sata_set_target_node_cleanup(sata_hba_inst,
14835 				    &sata_device.satadev_addr);
14836 			}
14837 		}
14838 	}
14839 	/*
14840 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14841 	 * with the hint: SE_HINT_REMOVE
14842 	 */
14843 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
14844 }
14845 
14846 
14847 /*
14848  * Device Attached Event processing.
14849  * Port state is checked to verify that a device is really attached. If so,
14850  * the device info structure is created and attached to the SATA port info
14851  * structure.
14852  *
14853  * If attached device cannot be identified or set-up, the retry for the
14854  * attach processing is set-up. Subsequent daemon run would try again to
14855  * identify the device, until the time limit is reached
14856  * (SATA_DEV_IDENTIFY_TIMEOUT).
14857  *
14858  * This function cannot be called in interrupt context (it may sleep).
14859  *
14860  * NOTE: Process cports event only, no port multiplier ports.
14861  */
14862 static void
14863 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst,
14864     sata_address_t *saddr)
14865 {
14866 	sata_cport_info_t *cportinfo;
14867 	sata_drive_info_t *sdevinfo;
14868 	sata_device_t sata_device;
14869 	dev_info_t *tdip;
14870 	uint32_t event_flags;
14871 	int rval;
14872 
14873 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14874 	    "Processing port %d device attached", saddr->cport);
14875 
14876 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14877 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14878 
14879 	/* Clear attach event flag first */
14880 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
14881 
14882 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
14883 	if ((cportinfo->cport_state &
14884 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14885 		cportinfo->cport_dev_attach_time = 0;
14886 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14887 		    cport_mutex);
14888 		return;
14889 	}
14890 
14891 	/*
14892 	 * If the sata_drive_info structure is found attached to the port info,
14893 	 * despite the fact the device was removed and now it is re-attached,
14894 	 * the old drive info structure was not removed.
14895 	 * Arbitrarily release device info structure.
14896 	 */
14897 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14898 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14899 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14900 		(void) kmem_free((void *)sdevinfo,
14901 		    sizeof (sata_drive_info_t));
14902 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14903 		    "Arbitrarily detaching old device info.", NULL);
14904 	}
14905 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14906 
14907 	/* For sanity, re-probe the port */
14908 	sata_device.satadev_rev = SATA_DEVICE_REV;
14909 	sata_device.satadev_addr = *saddr;
14910 
14911 	/*
14912 	 * We have to exit mutex, because the HBA probe port function may
14913 	 * block on its own mutex.
14914 	 */
14915 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14916 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14917 	    (SATA_DIP(sata_hba_inst), &sata_device);
14918 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14919 	sata_update_port_info(sata_hba_inst, &sata_device);
14920 	if (rval != SATA_SUCCESS) {
14921 		/* Something went wrong? Fail the port */
14922 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14923 		cportinfo->cport_dev_attach_time = 0;
14924 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14925 		    cport_mutex);
14926 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14927 		    "SATA port %d probing failed",
14928 		    saddr->cport));
14929 		return;
14930 	} else {
14931 		/* port probed successfully */
14932 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14933 	}
14934 	/*
14935 	 * Check if a device is still attached. For sanity, check also
14936 	 * link status - if no link, there is no device.
14937 	 */
14938 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
14939 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
14940 	    SATA_DTYPE_NONE) {
14941 		/*
14942 		 * No device - ignore attach event.
14943 		 */
14944 		cportinfo->cport_dev_attach_time = 0;
14945 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14946 		    cport_mutex);
14947 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14948 		    "Ignoring attach - no device connected to port %d",
14949 		    sata_device.satadev_addr.cport);
14950 		return;
14951 	}
14952 
14953 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14954 	/*
14955 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14956 	 * with the hint: SE_HINT_INSERT
14957 	 */
14958 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
14959 
14960 	/*
14961 	 * Port reprobing will take care of the creation of the device
14962 	 * info structure and determination of the device type.
14963 	 */
14964 	sata_device.satadev_addr = *saddr;
14965 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
14966 	    SATA_DEV_IDENTIFY_NORETRY);
14967 
14968 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14969 	    cport_mutex);
14970 	if ((cportinfo->cport_state & SATA_STATE_READY) &&
14971 	    (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) {
14972 		/* Some device is attached to the port */
14973 		if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) {
14974 			/*
14975 			 * A device was not successfully attached.
14976 			 * Track retry time for device identification.
14977 			 */
14978 			if (cportinfo->cport_dev_attach_time != 0) {
14979 				clock_t cur_time = ddi_get_lbolt();
14980 				/*
14981 				 * If the retry time limit was not exceeded,
14982 				 * reinstate attach event.
14983 				 */
14984 				if ((cur_time -
14985 				    cportinfo->cport_dev_attach_time) <
14986 				    drv_usectohz(
14987 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
14988 					/* OK, restore attach event */
14989 					cportinfo->cport_event_flags |=
14990 					    SATA_EVNT_DEVICE_ATTACHED;
14991 				} else {
14992 					/* Timeout - cannot identify device */
14993 					cportinfo->cport_dev_attach_time = 0;
14994 					sata_log(sata_hba_inst,
14995 					    CE_WARN,
14996 					    "Could not identify SATA device "
14997 					    "at port %d",
14998 					    saddr->cport);
14999 				}
15000 			} else {
15001 				/*
15002 				 * Start tracking time for device
15003 				 * identification.
15004 				 * Save current time (lbolt value).
15005 				 */
15006 				cportinfo->cport_dev_attach_time =
15007 				    ddi_get_lbolt();
15008 				/* Restore attach event */
15009 				cportinfo->cport_event_flags |=
15010 				    SATA_EVNT_DEVICE_ATTACHED;
15011 			}
15012 		} else {
15013 			/*
15014 			 * If device was successfully attached, the subsequent
15015 			 * action depends on a state of the
15016 			 * sata_auto_online variable. If it is set to zero.
15017 			 * an explicit 'configure' command will be needed to
15018 			 * configure it. If its value is non-zero, we will
15019 			 * attempt to online (configure) the device.
15020 			 * First, log the message indicating that a device
15021 			 * was attached.
15022 			 */
15023 			cportinfo->cport_dev_attach_time = 0;
15024 			sata_log(sata_hba_inst, CE_WARN,
15025 			    "SATA device detected at port %d", saddr->cport);
15026 
15027 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
15028 				sata_drive_info_t new_sdinfo;
15029 
15030 				/* Log device info data */
15031 				new_sdinfo = *(SATA_CPORTINFO_DRV_INFO(
15032 				    cportinfo));
15033 				sata_show_drive_info(sata_hba_inst,
15034 				    &new_sdinfo);
15035 			}
15036 
15037 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15038 			    saddr->cport)->cport_mutex);
15039 
15040 			/*
15041 			 * Make sure that there is no target node for that
15042 			 * device. If so, release it. It should not happen,
15043 			 * unless we had problem removing the node when
15044 			 * device was detached.
15045 			 */
15046 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
15047 			    saddr->cport);
15048 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15049 			    saddr->cport)->cport_mutex);
15050 			if (tdip != NULL) {
15051 
15052 #ifdef SATA_DEBUG
15053 				if ((cportinfo->cport_event_flags &
15054 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
15055 					sata_log(sata_hba_inst, CE_WARN,
15056 					    "sata_process_device_attached: "
15057 					    "old device target node exists!");
15058 #endif
15059 				/*
15060 				 * target node exists - try to unconfigure
15061 				 * device and remove the node.
15062 				 */
15063 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15064 				    saddr->cport)->cport_mutex);
15065 				rval = ndi_devi_offline(tdip,
15066 				    NDI_DEVI_REMOVE);
15067 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15068 				    saddr->cport)->cport_mutex);
15069 
15070 				if (rval == NDI_SUCCESS) {
15071 					cportinfo->cport_event_flags &=
15072 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
15073 					cportinfo->cport_tgtnode_clean = B_TRUE;
15074 				} else {
15075 					/*
15076 					 * PROBLEM - the target node remained
15077 					 * and it belongs to a previously
15078 					 * attached device.
15079 					 * This happens when the file was open
15080 					 * or the node was waiting for
15081 					 * resources at the time the
15082 					 * associated device was removed.
15083 					 * Instruct event daemon to retry the
15084 					 * cleanup later.
15085 					 */
15086 					sata_log(sata_hba_inst,
15087 					    CE_WARN,
15088 					    "Application(s) accessing "
15089 					    "previously attached SATA "
15090 					    "device have to release "
15091 					    "it before newly inserted "
15092 					    "device can be made accessible.",
15093 					    saddr->cport);
15094 					cportinfo->cport_event_flags |=
15095 					    SATA_EVNT_TARGET_NODE_CLEANUP;
15096 					cportinfo->cport_tgtnode_clean =
15097 					    B_FALSE;
15098 				}
15099 			}
15100 			if (sata_auto_online != 0) {
15101 				cportinfo->cport_event_flags |=
15102 				    SATA_EVNT_AUTOONLINE_DEVICE;
15103 			}
15104 
15105 		}
15106 	} else {
15107 		cportinfo->cport_dev_attach_time = 0;
15108 	}
15109 
15110 	event_flags = cportinfo->cport_event_flags;
15111 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15112 	if (event_flags != 0) {
15113 		mutex_enter(&sata_hba_inst->satahba_mutex);
15114 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15115 		mutex_exit(&sata_hba_inst->satahba_mutex);
15116 		mutex_enter(&sata_mutex);
15117 		sata_event_pending |= SATA_EVNT_MAIN;
15118 		mutex_exit(&sata_mutex);
15119 	}
15120 }
15121 
15122 
15123 /*
15124  * Device Target Node Cleanup Event processing.
15125  * If the target node associated with a sata port device is in
15126  * DEVI_DEVICE_REMOVED state, an attempt is made to remove it.
15127  * If the target node cannot be removed, the event flag is left intact,
15128  * so that event daemon may re-run this function later.
15129  *
15130  * This function cannot be called in interrupt context (it may sleep).
15131  *
15132  * NOTE: Processes cport events only, not port multiplier ports.
15133  */
15134 static void
15135 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
15136     sata_address_t *saddr)
15137 {
15138 	sata_cport_info_t *cportinfo;
15139 	dev_info_t *tdip;
15140 
15141 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15142 	    "Processing port %d device target node cleanup", saddr->cport);
15143 
15144 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15145 
15146 	/*
15147 	 * Check if there is target node for that device and it is in the
15148 	 * DEVI_DEVICE_REMOVED state. If so, release it.
15149 	 */
15150 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
15151 	if (tdip != NULL) {
15152 		/*
15153 		 * target node exists - check if it is target node of
15154 		 * a removed device.
15155 		 */
15156 		if (sata_check_device_removed(tdip) == B_TRUE) {
15157 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15158 			    "sata_process_target_node_cleanup: "
15159 			    "old device target node exists!", NULL);
15160 			/*
15161 			 * Unconfigure and remove the target node
15162 			 */
15163 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) ==
15164 			    NDI_SUCCESS) {
15165 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15166 				    saddr->cport)->cport_mutex);
15167 				cportinfo->cport_event_flags &=
15168 				    ~SATA_EVNT_TARGET_NODE_CLEANUP;
15169 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15170 				    saddr->cport)->cport_mutex);
15171 				return;
15172 			}
15173 			/*
15174 			 * Event daemon will retry the cleanup later.
15175 			 */
15176 			mutex_enter(&sata_hba_inst->satahba_mutex);
15177 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15178 			mutex_exit(&sata_hba_inst->satahba_mutex);
15179 			mutex_enter(&sata_mutex);
15180 			sata_event_pending |= SATA_EVNT_MAIN;
15181 			mutex_exit(&sata_mutex);
15182 		}
15183 	} else {
15184 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15185 		    saddr->cport)->cport_mutex);
15186 		cportinfo->cport_event_flags &=
15187 		    ~SATA_EVNT_TARGET_NODE_CLEANUP;
15188 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15189 		    saddr->cport)->cport_mutex);
15190 	}
15191 }
15192 
15193 /*
15194  * Device AutoOnline Event processing.
15195  * If attached device is to be onlined, an attempt is made to online this
15196  * device, but only if there is no lingering (old) target node present.
15197  * If the device cannot be onlined, the event flag is left intact,
15198  * so that event daemon may re-run this function later.
15199  *
15200  * This function cannot be called in interrupt context (it may sleep).
15201  *
15202  * NOTE: Processes cport events only, not port multiplier ports.
15203  */
15204 static void
15205 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst,
15206     sata_address_t *saddr)
15207 {
15208 	sata_cport_info_t *cportinfo;
15209 	sata_drive_info_t *sdinfo;
15210 	sata_device_t sata_device;
15211 	dev_info_t *tdip;
15212 
15213 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15214 	    "Processing port %d attached device auto-onlining", saddr->cport);
15215 
15216 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15217 
15218 	/*
15219 	 * Check if device is present and recognized. If not, reset event.
15220 	 */
15221 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15222 	if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
15223 		/* Nothing to online */
15224 		cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
15225 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15226 		    saddr->cport)->cport_mutex);
15227 		return;
15228 	}
15229 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15230 
15231 	/*
15232 	 * Check if there is target node for this device and if it is in the
15233 	 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep
15234 	 * the event for later processing.
15235 	 */
15236 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
15237 	if (tdip != NULL) {
15238 		/*
15239 		 * target node exists - check if it is target node of
15240 		 * a removed device.
15241 		 */
15242 		if (sata_check_device_removed(tdip) == B_TRUE) {
15243 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15244 			    "sata_process_device_autoonline: "
15245 			    "old device target node exists!", NULL);
15246 			/*
15247 			 * Event daemon will retry device onlining later.
15248 			 */
15249 			mutex_enter(&sata_hba_inst->satahba_mutex);
15250 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15251 			mutex_exit(&sata_hba_inst->satahba_mutex);
15252 			mutex_enter(&sata_mutex);
15253 			sata_event_pending |= SATA_EVNT_MAIN;
15254 			mutex_exit(&sata_mutex);
15255 			return;
15256 		}
15257 		/*
15258 		 * If the target node is not in the 'removed" state, assume
15259 		 * that it belongs to this device. There is nothing more to do,
15260 		 * but reset the event.
15261 		 */
15262 	} else {
15263 
15264 		/*
15265 		 * Try to online the device
15266 		 * If there is any reset-related event, remove it. We are
15267 		 * configuring the device and no state restoring is needed.
15268 		 */
15269 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15270 		    saddr->cport)->cport_mutex);
15271 		sata_device.satadev_addr = *saddr;
15272 		if (saddr->qual == SATA_ADDR_CPORT)
15273 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
15274 		else
15275 			sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
15276 		sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
15277 		if (sdinfo != NULL) {
15278 			if (sdinfo->satadrv_event_flags &
15279 			    (SATA_EVNT_DEVICE_RESET |
15280 			    SATA_EVNT_INPROC_DEVICE_RESET))
15281 				sdinfo->satadrv_event_flags = 0;
15282 			sdinfo->satadrv_event_flags |=
15283 			    SATA_EVNT_CLEAR_DEVICE_RESET;
15284 
15285 			/* Need to create a new target node. */
15286 			cportinfo->cport_tgtnode_clean = B_TRUE;
15287 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15288 			    saddr->cport)->cport_mutex);
15289 			tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
15290 			    sata_hba_inst, &sata_device.satadev_addr);
15291 			if (tdip == NULL) {
15292 				/*
15293 				 * Configure (onlining) failed.
15294 				 * We will NOT retry
15295 				 */
15296 				SATA_LOG_D((sata_hba_inst, CE_WARN,
15297 				    "sata_process_device_autoonline: "
15298 				    "configuring SATA device at port %d failed",
15299 				    saddr->cport));
15300 			}
15301 		} else {
15302 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15303 			    saddr->cport)->cport_mutex);
15304 		}
15305 
15306 	}
15307 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15308 	cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
15309 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15310 	    saddr->cport)->cport_mutex);
15311 }
15312 
15313 
15314 static void
15315 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr,
15316     int hint)
15317 {
15318 	char ap[MAXPATHLEN];
15319 	nvlist_t *ev_attr_list = NULL;
15320 	int err;
15321 
15322 	/* Allocate and build sysevent attribute list */
15323 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP);
15324 	if (err != 0) {
15325 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15326 		    "sata_gen_sysevent: "
15327 		    "cannot allocate memory for sysevent attributes\n"));
15328 		return;
15329 	}
15330 	/* Add hint attribute */
15331 	err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint));
15332 	if (err != 0) {
15333 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15334 		    "sata_gen_sysevent: "
15335 		    "failed to add DR_HINT attr for sysevent"));
15336 		nvlist_free(ev_attr_list);
15337 		return;
15338 	}
15339 	/*
15340 	 * Add AP attribute.
15341 	 * Get controller pathname and convert it into AP pathname by adding
15342 	 * a target number.
15343 	 */
15344 	(void) snprintf(ap, MAXPATHLEN, "/devices");
15345 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap));
15346 	(void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d",
15347 	    SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual));
15348 
15349 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap);
15350 	if (err != 0) {
15351 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15352 		    "sata_gen_sysevent: "
15353 		    "failed to add DR_AP_ID attr for sysevent"));
15354 		nvlist_free(ev_attr_list);
15355 		return;
15356 	}
15357 
15358 	/* Generate/log sysevent */
15359 	err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR,
15360 	    ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP);
15361 	if (err != DDI_SUCCESS) {
15362 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15363 		    "sata_gen_sysevent: "
15364 		    "cannot log sysevent, err code %x\n", err));
15365 	}
15366 
15367 	nvlist_free(ev_attr_list);
15368 }
15369 
15370 
15371 
15372 
15373 /*
15374  * Set DEVI_DEVICE_REMOVED state in the SATA device target node.
15375  */
15376 static void
15377 sata_set_device_removed(dev_info_t *tdip)
15378 {
15379 	int circ;
15380 
15381 	ASSERT(tdip != NULL);
15382 
15383 	ndi_devi_enter(tdip, &circ);
15384 	mutex_enter(&DEVI(tdip)->devi_lock);
15385 	DEVI_SET_DEVICE_REMOVED(tdip);
15386 	mutex_exit(&DEVI(tdip)->devi_lock);
15387 	ndi_devi_exit(tdip, circ);
15388 }
15389 
15390 
15391 /*
15392  * Set internal event instructing event daemon to try
15393  * to perform the target node cleanup.
15394  */
15395 static void
15396 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
15397     sata_address_t *saddr)
15398 {
15399 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15400 	SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |=
15401 	    SATA_EVNT_TARGET_NODE_CLEANUP;
15402 	SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_tgtnode_clean =
15403 	    B_FALSE;
15404 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15405 	mutex_enter(&sata_hba_inst->satahba_mutex);
15406 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15407 	mutex_exit(&sata_hba_inst->satahba_mutex);
15408 	mutex_enter(&sata_mutex);
15409 	sata_event_pending |= SATA_EVNT_MAIN;
15410 	mutex_exit(&sata_mutex);
15411 }
15412 
15413 
15414 /*
15415  * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state,
15416  * i.e. check if the target node state indicates that it belongs to a removed
15417  * device.
15418  *
15419  * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state,
15420  * B_FALSE otherwise.
15421  *
15422  * NOTE: No port multiplier support.
15423  */
15424 static boolean_t
15425 sata_check_device_removed(dev_info_t *tdip)
15426 {
15427 	ASSERT(tdip != NULL);
15428 
15429 	if (DEVI_IS_DEVICE_REMOVED(tdip))
15430 		return (B_TRUE);
15431 	else
15432 		return (B_FALSE);
15433 }
15434 
15435 /* ************************ FAULT INJECTTION **************************** */
15436 
15437 #ifdef SATA_INJECT_FAULTS
15438 
15439 static	uint32_t sata_fault_count = 0;
15440 static	uint32_t sata_fault_suspend_count = 0;
15441 
15442 /*
15443  * Inject sata pkt fault
15444  * It modifies returned values of the sata packet.
15445  * First argument is the pointer to the executed sata packet.
15446  * The second argument specifies SATA command to be affected (not all commands
15447  * are instrumented).
15448  * Third argument is a pointer to a value returned by the HBA tran_start
15449  * function.
15450  * Fourth argument specifies injected error. Injected sata packet faults
15451  * are the satapkt_reason values.
15452  * SATA_PKT_BUSY		-1	Not completed, busy
15453  * SATA_PKT_DEV_ERROR		1	Device reported error
15454  * SATA_PKT_QUEUE_FULL		2	Not accepted, queue full
15455  * SATA_PKT_PORT_ERROR		3	Not completed, port error
15456  * SATA_PKT_CMD_UNSUPPORTED	4	Cmd unsupported
15457  * SATA_PKT_ABORTED		5	Aborted by request
15458  * SATA_PKT_TIMEOUT		6	Operation timeut
15459  * SATA_PKT_RESET		7	Aborted by reset request
15460  *
15461  * sata_inject_fault_count variable specifies number of times in row the
15462  * error is injected. Value of -1 specifies permanent fault, ie. every time
15463  * the fault injection pointnis reached, the fault is injected and anu pause
15464  * between fault injection specified by sata_inject_fault_pause_count is
15465  * ignored).
15466  *
15467  * sata_inject_fault_pause_count variable specifies number of times a fault
15468  * injection is bypassed (pause between fault injections).
15469  * If set to 0, a fault is injected only a number of times specified by
15470  * sata_inject_fault_count.
15471  *
15472  * The fault counts are static, so for periodic errors they have to be manually
15473  * reset to start repetition sequence from scratch.
15474  * If the original value returned by the HBA tran_start function is not
15475  * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error
15476  * is injected (to avoid masking real problems);
15477  *
15478  * NOTE: In its current incarnation, this function should be invoked only for
15479  * commands executed in SYNCHRONOUS mode.
15480  */
15481 
15482 
15483 static	void
15484 sata_inject_pkt_fault(sata_pkt_t *spkt, uint8_t cmd, int *rval,
15485     int fault)
15486 {
15487 	if (fault == 0)
15488 		return;
15489 	if (sata_inject_fault_count == 0)
15490 		return;
15491 
15492 	if (spkt->satapkt_cmd.satacmd_cmd_reg != cmd)
15493 		return;
15494 
15495 	if (*rval != SATA_TRAN_ACCEPTED ||
15496 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
15497 		sata_fault_count = 0;
15498 		sata_fault_suspend_count = 0;
15499 		return;
15500 	}
15501 	if (sata_fault_count == 0 && sata_fault_suspend_count != 0) {
15502 		/* Pause in the injection */
15503 		sata_fault_suspend_count -= 1;
15504 		return;
15505 	}
15506 
15507 	if (sata_fault_count == 0 && sata_fault_suspend_count == 0) {
15508 		/*
15509 		 * Init inject fault cycle. If fault count is set to -1,
15510 		 * it is a permanent fault.
15511 		 */
15512 		if (sata_inject_fault_count != -1) {
15513 			sata_fault_count = sata_inject_fault_count;
15514 			sata_fault_suspend_count =
15515 			    sata_inject_fault_pause_count;
15516 			if (sata_fault_suspend_count == 0)
15517 				sata_inject_fault_count = 0;
15518 		}
15519 	}
15520 
15521 	if (sata_fault_count != 0)
15522 		sata_fault_count -= 1;
15523 
15524 	switch (fault) {
15525 	case SATA_PKT_BUSY:
15526 		*rval = SATA_TRAN_BUSY;
15527 		spkt->satapkt_reason = SATA_PKT_BUSY;
15528 		break;
15529 
15530 	case SATA_PKT_QUEUE_FULL:
15531 		*rval = SATA_TRAN_QUEUE_FULL;
15532 		spkt->satapkt_reason = SATA_PKT_QUEUE_FULL;
15533 		break;
15534 
15535 	case SATA_PKT_CMD_UNSUPPORTED:
15536 		*rval = SATA_TRAN_CMD_UNSUPPORTED;
15537 		spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED;
15538 		break;
15539 
15540 	case SATA_PKT_PORT_ERROR:
15541 		/* This is "rejected" command */
15542 		*rval = SATA_TRAN_PORT_ERROR;
15543 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
15544 		/* Additional error setup could be done here - port state */
15545 		break;
15546 
15547 	case SATA_PKT_DEV_ERROR:
15548 		spkt->satapkt_reason = SATA_PKT_DEV_ERROR;
15549 		/*
15550 		 * Additional error setup could be done here
15551 		 */
15552 		break;
15553 
15554 	case SATA_PKT_ABORTED:
15555 		spkt->satapkt_reason = SATA_PKT_ABORTED;
15556 		break;
15557 
15558 	case SATA_PKT_TIMEOUT:
15559 		spkt->satapkt_reason = SATA_PKT_TIMEOUT;
15560 		/* Additional error setup could be done here */
15561 		break;
15562 
15563 	case SATA_PKT_RESET:
15564 		spkt->satapkt_reason = SATA_PKT_RESET;
15565 		/*
15566 		 * Additional error setup could be done here - device reset
15567 		 */
15568 		break;
15569 
15570 	default:
15571 		break;
15572 	}
15573 }
15574 
15575 #endif
15576