xref: /titanic_50/usr/src/uts/common/io/sata/impl/sata.c (revision 7b2cbac6bdd0f33171865b3ab8e08e8528c60d2c)
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 #define	LEGACY_HWID_LEN	64	/* Model (40) + Serial (20) + pad */
114 
115 
116 /*
117  * SATA cb_ops functions
118  */
119 static 	int sata_hba_open(dev_t *, int, int, cred_t *);
120 static 	int sata_hba_close(dev_t, int, int, cred_t *);
121 static 	int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *,	int *);
122 
123 /*
124  * SCSA required entry points
125  */
126 static	int sata_scsi_tgt_init(dev_info_t *, dev_info_t *,
127     scsi_hba_tran_t *, struct scsi_device *);
128 static	int sata_scsi_tgt_probe(struct scsi_device *,
129     int (*callback)(void));
130 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *,
131     scsi_hba_tran_t *, struct scsi_device *);
132 static 	int sata_scsi_start(struct scsi_address *, struct scsi_pkt *);
133 static 	int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *);
134 static 	int sata_scsi_reset(struct scsi_address *, int);
135 static 	int sata_scsi_getcap(struct scsi_address *, char *, int);
136 static 	int sata_scsi_setcap(struct scsi_address *, char *, int, int);
137 static 	struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *,
138     struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t),
139     caddr_t);
140 static 	void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *);
141 static 	void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *);
142 static 	void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *);
143 
144 /*
145  * SATA HBA interface functions are defined in sata_hba.h header file
146  */
147 
148 /* Event processing functions */
149 static	void sata_event_daemon(void *);
150 static	void sata_event_thread_control(int);
151 static	void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst);
152 static	void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *);
153 static	void sata_process_port_failed_event(sata_hba_inst_t *,
154     sata_address_t *);
155 static	void sata_process_port_link_events(sata_hba_inst_t *,
156     sata_address_t *);
157 static	void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *);
158 static	void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *);
159 static	void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *);
160 static	void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *);
161 static	void sata_process_target_node_cleanup(sata_hba_inst_t *,
162     sata_address_t *);
163 static	void sata_process_device_autoonline(sata_hba_inst_t *,
164     sata_address_t *saddr);
165 
166 /*
167  * Local translation functions
168  */
169 static	int sata_txlt_inquiry(sata_pkt_txlate_t *);
170 static	int sata_txlt_test_unit_ready(sata_pkt_txlate_t *);
171 static	int sata_txlt_start_stop_unit(sata_pkt_txlate_t *);
172 static	int sata_txlt_read_capacity(sata_pkt_txlate_t *);
173 static	int sata_txlt_request_sense(sata_pkt_txlate_t *);
174 static 	int sata_txlt_read(sata_pkt_txlate_t *);
175 static 	int sata_txlt_write(sata_pkt_txlate_t *);
176 static 	int sata_txlt_log_sense(sata_pkt_txlate_t *);
177 static 	int sata_txlt_log_select(sata_pkt_txlate_t *);
178 static 	int sata_txlt_mode_sense(sata_pkt_txlate_t *);
179 static 	int sata_txlt_mode_select(sata_pkt_txlate_t *);
180 static 	int sata_txlt_synchronize_cache(sata_pkt_txlate_t *);
181 static 	int sata_txlt_write_buffer(sata_pkt_txlate_t *);
182 static 	int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *);
183 
184 static 	int sata_hba_start(sata_pkt_txlate_t *, int *);
185 static	int sata_txlt_invalid_command(sata_pkt_txlate_t *);
186 static	int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *);
187 static 	void sata_txlt_rw_completion(sata_pkt_t *);
188 static 	void sata_txlt_nodata_cmd_completion(sata_pkt_t *);
189 static 	void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *);
190 static 	int sata_emul_rw_completion(sata_pkt_txlate_t *);
191 static 	struct scsi_extended_sense *sata_immediate_error_response(
192     sata_pkt_txlate_t *, int);
193 static	struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *);
194 
195 static 	int sata_txlt_atapi(sata_pkt_txlate_t *);
196 static 	void sata_txlt_atapi_completion(sata_pkt_t *);
197 
198 /*
199  * Local functions for ioctl
200  */
201 static	int32_t sata_get_port_num(sata_hba_inst_t *,  struct devctl_iocdata *);
202 static	void sata_cfgadm_state(sata_hba_inst_t *, int32_t,
203     devctl_ap_state_t *);
204 static	dev_info_t *sata_get_target_dip(dev_info_t *, int32_t);
205 static	dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *);
206 static	dev_info_t *sata_devt_to_devinfo(dev_t);
207 static	int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *);
208 static	int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *);
209 static	int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *);
210 static	int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *);
211 static	int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *);
212 static	int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *);
213 static	int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *);
214 static	int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *);
215 static	int sata_ioctl_reset_all(sata_hba_inst_t *);
216 static	int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *);
217 static	int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *,
218     sata_ioctl_data_t *, int mode);
219 static	int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *,
220     sata_ioctl_data_t *, int mode);
221 static	int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *,
222     sata_ioctl_data_t *, int mode);
223 static	int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *,
224     sata_ioctl_data_t *, int mode);
225 static	int sata_ioctl_get_serialnumber_info(sata_hba_inst_t *,
226     sata_device_t *, sata_ioctl_data_t *, int mode);
227 
228 /*
229  * Local functions
230  */
231 static 	void sata_remove_hba_instance(dev_info_t *);
232 static 	int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *);
233 static 	void sata_probe_ports(sata_hba_inst_t *);
234 static 	int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int);
235 static 	int sata_add_device(dev_info_t *, sata_hba_inst_t *, int cport,
236     int pmport);
237 static 	dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *,
238     sata_address_t *);
239 static 	int sata_validate_scsi_address(sata_hba_inst_t *,
240     struct scsi_address *, sata_device_t *);
241 static 	int sata_validate_sata_address(sata_hba_inst_t *, int, int, int);
242 static	sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t));
243 static	void sata_pkt_free(sata_pkt_txlate_t *);
244 static	int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t),
245     caddr_t, ddi_dma_attr_t *);
246 static	int sata_probe_device(sata_hba_inst_t *, sata_device_t *);
247 static	sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *,
248     sata_device_t *);
249 static 	int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *);
250 static	struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int);
251 static 	void sata_free_local_buffer(sata_pkt_txlate_t *);
252 static 	uint64_t sata_check_capacity(sata_drive_info_t *);
253 void 	sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *,
254     ddi_dma_attr_t *);
255 static 	int sata_fetch_device_identify_data(sata_hba_inst_t *,
256     sata_drive_info_t *);
257 static	void sata_update_port_info(sata_hba_inst_t *, sata_device_t *);
258 static	void sata_update_port_scr(sata_port_scr_t *, sata_device_t *);
259 static	int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *);
260 static	int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int);
261 static	int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int);
262 static	int sata_set_drive_features(sata_hba_inst_t *,
263     sata_drive_info_t *, int flag);
264 static	void sata_init_write_cache_mode(sata_drive_info_t *sdinfo);
265 static	int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *);
266 static	void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *,
267     uint8_t *);
268 static	int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *,
269     struct scsi_inquiry *);
270 static	int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *);
271 static	int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *);
272 static	int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *);
273 static	int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *);
274 static	int sata_mode_select_page_8(sata_pkt_txlate_t *,
275     struct mode_cache_scsi3 *, int, int *, int *, int *);
276 static	int sata_mode_select_page_1c(sata_pkt_txlate_t *,
277     struct mode_info_excpt_page *, int, int *, int *, int *);
278 static	int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *);
279 static	int sata_mode_select_page_30(sata_pkt_txlate_t *,
280     struct mode_acoustic_management *, int, int *, int *, int *);
281 
282 static	int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *);
283 static	int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *,
284     sata_hba_inst_t *);
285 static	int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *,
286     sata_hba_inst_t *);
287 static	int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *,
288     sata_hba_inst_t *);
289 static	void sata_save_drive_settings(sata_drive_info_t *);
290 static	void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *);
291 static	void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...);
292 static	int sata_fetch_smart_return_status(sata_hba_inst_t *,
293     sata_drive_info_t *);
294 static	int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *,
295     struct smart_data *);
296 static	int sata_smart_selftest_log(sata_hba_inst_t *,
297     sata_drive_info_t *,
298     struct smart_selftest_log *);
299 static	int sata_ext_smart_selftest_read_log(sata_hba_inst_t *,
300     sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t);
301 static	int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *,
302     uint8_t *, uint8_t, uint8_t);
303 static	int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *,
304     struct read_log_ext_directory *);
305 static	void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int);
306 static	void sata_xlate_errors(sata_pkt_txlate_t *);
307 static	void sata_decode_device_error(sata_pkt_txlate_t *,
308     struct scsi_extended_sense *);
309 static	void sata_set_device_removed(dev_info_t *);
310 static	boolean_t sata_check_device_removed(dev_info_t *);
311 static	void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *);
312 static	int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *,
313     sata_drive_info_t *);
314 static	int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *,
315     sata_drive_info_t *);
316 static	void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *);
317 static	void sata_fixed_sense_data_preset(struct scsi_extended_sense *);
318 static  void sata_target_devid_register(dev_info_t *, sata_drive_info_t *);
319 static  int sata_check_modser(char *, int);
320 
321 
322 
323 /*
324  * SATA Framework will ignore SATA HBA driver cb_ops structure and
325  * register following one with SCSA framework.
326  * Open & close are provided, so scsi framework will not use its own
327  */
328 static struct cb_ops sata_cb_ops = {
329 	sata_hba_open,			/* open */
330 	sata_hba_close,			/* close */
331 	nodev,				/* strategy */
332 	nodev,				/* print */
333 	nodev,				/* dump */
334 	nodev,				/* read */
335 	nodev,				/* write */
336 	sata_hba_ioctl,			/* ioctl */
337 	nodev,				/* devmap */
338 	nodev,				/* mmap */
339 	nodev,				/* segmap */
340 	nochpoll,			/* chpoll */
341 	ddi_prop_op,			/* cb_prop_op */
342 	0,				/* streamtab */
343 	D_NEW | D_MP,			/* cb_flag */
344 	CB_REV,				/* rev */
345 	nodev,				/* aread */
346 	nodev				/* awrite */
347 };
348 
349 
350 extern struct mod_ops mod_miscops;
351 extern uchar_t	scsi_cdb_size[];
352 
353 static struct modlmisc modlmisc = {
354 	&mod_miscops,			/* Type of module */
355 	"SATA Module v%I%"		/* module name */
356 };
357 
358 
359 static struct modlinkage modlinkage = {
360 	MODREV_1,
361 	(void *)&modlmisc,
362 	NULL
363 };
364 
365 /*
366  * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero,
367  * i.e. when scsi_pkt has not timeout specified.
368  */
369 static int sata_default_pkt_time = 60;	/* 60 seconds */
370 
371 /*
372  * Intermediate buffer device access attributes - they are required,
373  * but not necessarily used.
374  */
375 static ddi_device_acc_attr_t sata_acc_attr = {
376 	DDI_DEVICE_ATTR_V0,
377 	DDI_STRUCTURE_LE_ACC,
378 	DDI_STRICTORDER_ACC
379 };
380 
381 
382 /*
383  * Mutexes protecting structures in multithreaded operations.
384  * Because events are relatively rare, a single global mutex protecting
385  * data structures should be sufficient. To increase performance, add
386  * separate mutex per each sata port and use global mutex only to protect
387  * common data structures.
388  */
389 static	kmutex_t sata_mutex;		/* protects sata_hba_list */
390 static	kmutex_t sata_log_mutex;	/* protects log */
391 
392 static 	char sata_log_buf[256];
393 
394 /* Default write cache setting for SATA hard disks */
395 int	sata_write_cache = 1;		/* enabled */
396 
397 /* Default write cache setting for SATA ATAPI CD/DVD */
398 int 	sata_atapicdvd_write_cache = 1; /* enabled */
399 
400 /*
401  * Linked list of HBA instances
402  */
403 static 	sata_hba_inst_t *sata_hba_list = NULL;
404 static 	sata_hba_inst_t *sata_hba_list_tail = NULL;
405 /*
406  * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran
407  * structure and in sata soft state.
408  */
409 
410 /*
411  * Event daemon related variables
412  */
413 static 	kmutex_t sata_event_mutex;
414 static 	kcondvar_t sata_event_cv;
415 static 	kthread_t *sata_event_thread = NULL;
416 static 	int sata_event_thread_terminate = 0;
417 static 	int sata_event_pending = 0;
418 static 	int sata_event_thread_active = 0;
419 extern 	pri_t minclsyspri;
420 
421 /*
422  * NCQ error recovery command
423  */
424 static const sata_cmd_t sata_rle_cmd = {
425 	SATA_CMD_REV,
426 	NULL,
427 	{
428 		SATA_DIR_READ
429 	},
430 	ATA_ADDR_LBA48,
431 	0,
432 	0,
433 	0,
434 	0,
435 	0,
436 	1,
437 	READ_LOG_EXT_NCQ_ERROR_RECOVERY,
438 	0,
439 	0,
440 	0,
441 	SATAC_READ_LOG_EXT,
442 	0,
443 	0,
444 	0,
445 };
446 
447 /*
448  * ATAPI error recovery CDB
449  */
450 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = {
451 	SCMD_REQUEST_SENSE,
452 	0,			/* Only fixed RQ format is supported */
453 	0,
454 	0,
455 	SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */
456 	0
457 };
458 
459 
460 /* Warlock directives */
461 
462 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran))
463 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device))
464 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops))
465 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense))
466 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status))
467 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr))
468 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t))
469 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state))
470 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state))
471 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list))
472 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list))
473 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next))
474 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev))
475 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \
476     sata_hba_inst::satahba_scsi_tran))
477 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran))
478 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip))
479 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached))
480 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port))
481 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex,
482     sata_hba_inst::satahba_event_flags))
483 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
484     sata_cport_info::cport_devp))
485 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp))
486 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr))
487 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
488     sata_cport_info::cport_dev_type))
489 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type))
490 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
491     sata_cport_info::cport_state))
492 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state))
493 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
494     sata_pmport_info::pmport_state))
495 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state))
496 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type))
497 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive))
498 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port))
499 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports))
500 #ifdef SATA_DEBUG
501 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count))
502 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count))
503 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace))
504 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index))
505 #endif
506 
507 /* End of warlock directives */
508 
509 /* ************** loadable module configuration functions ************** */
510 
511 int
512 _init()
513 {
514 	int rval;
515 
516 	mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL);
517 	mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL);
518 	mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL);
519 	cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL);
520 	if ((rval = mod_install(&modlinkage)) != 0) {
521 #ifdef SATA_DEBUG
522 		cmn_err(CE_WARN, "sata: _init: mod_install failed\n");
523 #endif
524 		mutex_destroy(&sata_log_mutex);
525 		cv_destroy(&sata_event_cv);
526 		mutex_destroy(&sata_event_mutex);
527 		mutex_destroy(&sata_mutex);
528 	}
529 	return (rval);
530 }
531 
532 int
533 _fini()
534 {
535 	int rval;
536 
537 	if ((rval = mod_remove(&modlinkage)) != 0)
538 		return (rval);
539 
540 	mutex_destroy(&sata_log_mutex);
541 	cv_destroy(&sata_event_cv);
542 	mutex_destroy(&sata_event_mutex);
543 	mutex_destroy(&sata_mutex);
544 	return (rval);
545 }
546 
547 int
548 _info(struct modinfo *modinfop)
549 {
550 	return (mod_info(&modlinkage, modinfop));
551 }
552 
553 
554 
555 /* ********************* SATA HBA entry points ********************* */
556 
557 
558 /*
559  * Called by SATA HBA from _init().
560  * Registers HBA driver instance/sata framework pair with scsi framework, by
561  * calling scsi_hba_init().
562  *
563  * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used
564  * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver
565  * cb_ops pointer in SATA HBA driver dev_ops structure.
566  * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors.
567  *
568  * Return status of the scsi_hba_init() is returned to a calling SATA HBA
569  * driver.
570  */
571 int
572 sata_hba_init(struct modlinkage *modlp)
573 {
574 	int rval;
575 	struct dev_ops *hba_ops;
576 
577 	SATADBG1(SATA_DBG_HBA_IF, NULL,
578 	    "sata_hba_init: name %s \n",
579 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
580 	/*
581 	 * Fill-up cb_ops and dev_ops when necessary
582 	 */
583 	hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
584 	/*
585 	 * Provide pointer to SATA dev_ops
586 	 */
587 	hba_ops->devo_cb_ops = &sata_cb_ops;
588 
589 	/*
590 	 * Register SATA HBA with SCSI framework
591 	 */
592 	if ((rval = scsi_hba_init(modlp)) != 0) {
593 		SATADBG1(SATA_DBG_HBA_IF, NULL,
594 		    "sata_hba_init: scsi hba init failed\n", NULL);
595 		return (rval);
596 	}
597 
598 	return (0);
599 }
600 
601 
602 /* HBA attach stages */
603 #define	HBA_ATTACH_STAGE_SATA_HBA_INST	1
604 #define	HBA_ATTACH_STAGE_SCSI_ATTACHED	2
605 #define	HBA_ATTACH_STAGE_SETUP		4
606 #define	HBA_ATTACH_STAGE_LINKED		8
607 
608 
609 /*
610  *
611  * Called from SATA HBA driver's attach routine to attach an instance of
612  * the HBA.
613  *
614  * For DDI_ATTACH command:
615  * sata_hba_inst structure is allocated here and initialized with pointers to
616  * SATA framework implementation of required scsi tran functions.
617  * The scsi_tran's tran_hba_private field is used by SATA Framework to point
618  * to the soft structure (sata_hba_inst) allocated by SATA framework for
619  * SATA HBA instance related data.
620  * The scsi_tran's tran_hba_private field is used by SATA framework to
621  * store a pointer to per-HBA-instance of sata_hba_inst structure.
622  * The sata_hba_inst structure is cross-linked to scsi tran structure.
623  * Among other info, a pointer to sata_hba_tran structure is stored in
624  * sata_hba_inst. The sata_hba_inst structures for different HBA instances are
625  * linked together into the list, pointed to by sata_hba_list.
626  * On the first HBA instance attach the sata event thread is initialized.
627  * Attachment points are created for all SATA ports of the HBA being attached.
628  * All HBA instance's SATA ports are probed and type of plugged devices is
629  * determined. For each device of a supported type, a target node is created.
630  *
631  * DDI_SUCCESS is returned when attachment process is successful,
632  * DDI_FAILURE is returned otherwise.
633  *
634  * For DDI_RESUME command:
635  * Not implemented at this time (postponed until phase 2 of the development).
636  */
637 int
638 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran,
639     ddi_attach_cmd_t cmd)
640 {
641 	sata_hba_inst_t	*sata_hba_inst;
642 	scsi_hba_tran_t *scsi_tran = NULL;
643 	int hba_attach_state = 0;
644 	char taskq_name[MAXPATHLEN];
645 
646 	SATADBG3(SATA_DBG_HBA_IF, NULL,
647 	    "sata_hba_attach: node %s (%s%d)\n",
648 	    ddi_node_name(dip), ddi_driver_name(dip),
649 	    ddi_get_instance(dip));
650 
651 	if (cmd == DDI_RESUME) {
652 		/*
653 		 * Postponed until phase 2 of the development
654 		 */
655 		return (DDI_FAILURE);
656 	}
657 
658 	if (cmd != DDI_ATTACH) {
659 		return (DDI_FAILURE);
660 	}
661 
662 	/* cmd == DDI_ATTACH */
663 
664 	if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) {
665 		SATA_LOG_D((NULL, CE_WARN,
666 		    "sata_hba_attach: invalid sata_hba_tran"));
667 		return (DDI_FAILURE);
668 	}
669 	/*
670 	 * Allocate and initialize SCSI tran structure.
671 	 * SATA copy of tran_bus_config is provided to create port nodes.
672 	 */
673 	scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
674 	if (scsi_tran == NULL)
675 		return (DDI_FAILURE);
676 	/*
677 	 * Allocate soft structure for SATA HBA instance.
678 	 * There is a separate softstate for each HBA instance.
679 	 */
680 	sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP);
681 	ASSERT(sata_hba_inst != NULL); /* this should not fail */
682 	mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL);
683 	hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST;
684 
685 	/*
686 	 * scsi_trans's tran_hba_private is used by SATA Framework to point to
687 	 * soft structure allocated by SATA framework for
688 	 * SATA HBA instance related data.
689 	 */
690 	scsi_tran->tran_hba_private	= sata_hba_inst;
691 	scsi_tran->tran_tgt_private	= NULL;
692 
693 	scsi_tran->tran_tgt_init	= sata_scsi_tgt_init;
694 	scsi_tran->tran_tgt_probe	= sata_scsi_tgt_probe;
695 	scsi_tran->tran_tgt_free	= sata_scsi_tgt_free;
696 
697 	scsi_tran->tran_start		= sata_scsi_start;
698 	scsi_tran->tran_reset		= sata_scsi_reset;
699 	scsi_tran->tran_abort		= sata_scsi_abort;
700 	scsi_tran->tran_getcap		= sata_scsi_getcap;
701 	scsi_tran->tran_setcap		= sata_scsi_setcap;
702 	scsi_tran->tran_init_pkt	= sata_scsi_init_pkt;
703 	scsi_tran->tran_destroy_pkt	= sata_scsi_destroy_pkt;
704 
705 	scsi_tran->tran_dmafree		= sata_scsi_dmafree;
706 	scsi_tran->tran_sync_pkt	= sata_scsi_sync_pkt;
707 
708 	scsi_tran->tran_reset_notify	= NULL;
709 	scsi_tran->tran_get_bus_addr	= NULL;
710 	scsi_tran->tran_quiesce		= NULL;
711 	scsi_tran->tran_unquiesce	= NULL;
712 	scsi_tran->tran_bus_reset	= NULL;
713 
714 	if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr,
715 	    scsi_tran, 0) != DDI_SUCCESS) {
716 #ifdef SATA_DEBUG
717 		cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed",
718 		    ddi_driver_name(dip), ddi_get_instance(dip));
719 #endif
720 		goto fail;
721 	}
722 	hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED;
723 
724 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) {
725 		if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
726 		    "sata", 1) != DDI_PROP_SUCCESS) {
727 			SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: "
728 			    "failed to create hba sata prop"));
729 			goto fail;
730 		}
731 	}
732 
733 	/*
734 	 * Save pointers in hba instance soft state.
735 	 */
736 	sata_hba_inst->satahba_scsi_tran = scsi_tran;
737 	sata_hba_inst->satahba_tran = sata_tran;
738 	sata_hba_inst->satahba_dip = dip;
739 
740 	/*
741 	 * Create a task queue to handle emulated commands completion
742 	 * Use node name, dash, instance number as the queue name.
743 	 */
744 	taskq_name[0] = '\0';
745 	(void) strlcat(taskq_name, DEVI(dip)->devi_node_name,
746 	    sizeof (taskq_name));
747 	(void) snprintf(taskq_name + strlen(taskq_name),
748 	    sizeof (taskq_name) - strlen(taskq_name),
749 	    "-%d", DEVI(dip)->devi_instance);
750 	sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1,
751 	    minclsyspri, 1, sata_tran->sata_tran_hba_num_cports,
752 	    TASKQ_DYNAMIC);
753 
754 	hba_attach_state |= HBA_ATTACH_STAGE_SETUP;
755 
756 	/*
757 	 * Create events thread if not created yet.
758 	 */
759 	sata_event_thread_control(1);
760 
761 	/*
762 	 * Link this hba instance into the list.
763 	 */
764 	mutex_enter(&sata_mutex);
765 
766 	if (sata_hba_list == NULL) {
767 		/*
768 		 * The first instance of HBA is attached.
769 		 * Set current/active default maximum NCQ/TCQ queue depth for
770 		 * all SATA devices. It is done here and now, to eliminate the
771 		 * possibility of the dynamic, programatic modification of the
772 		 * queue depth via global (and public) sata_max_queue_depth
773 		 * variable (this would require special handling in HBA drivers)
774 		 */
775 		sata_current_max_qdepth = sata_max_queue_depth;
776 		if (sata_current_max_qdepth > 32)
777 			sata_current_max_qdepth = 32;
778 		else if (sata_current_max_qdepth < 1)
779 			sata_current_max_qdepth = 1;
780 	}
781 
782 	sata_hba_inst->satahba_next = NULL;
783 	sata_hba_inst->satahba_prev = sata_hba_list_tail;
784 	if (sata_hba_list == NULL) {
785 		sata_hba_list = sata_hba_inst;
786 	}
787 	if (sata_hba_list_tail != NULL) {
788 		sata_hba_list_tail->satahba_next = sata_hba_inst;
789 	}
790 	sata_hba_list_tail = sata_hba_inst;
791 	mutex_exit(&sata_mutex);
792 	hba_attach_state |= HBA_ATTACH_STAGE_LINKED;
793 
794 	/*
795 	 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl
796 	 * SATA HBA driver should not use its own open/close entry points.
797 	 *
798 	 * Make sure that instance number doesn't overflow
799 	 * when forming minor numbers.
800 	 */
801 	ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT));
802 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
803 	    INST2DEVCTL(ddi_get_instance(dip)),
804 	    DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) {
805 #ifdef SATA_DEBUG
806 		cmn_err(CE_WARN, "sata_hba_attach: "
807 		    "cannot create devctl minor node");
808 #endif
809 		goto fail;
810 	}
811 
812 
813 	/*
814 	 * Set-up kstats here, if necessary.
815 	 * (postponed until phase 2 of the development).
816 	 */
817 
818 
819 	/*
820 	 * Probe controller ports. This operation will describe a current
821 	 * controller/port/multipliers/device configuration and will create
822 	 * attachment points.
823 	 * We may end-up with just a controller with no devices attached.
824 	 * For the ports with a supported device attached, device target nodes
825 	 * are created and devices are initialized.
826 	 */
827 	sata_probe_ports(sata_hba_inst);
828 
829 	sata_hba_inst->satahba_attached = 1;
830 	return (DDI_SUCCESS);
831 
832 fail:
833 	if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) {
834 		(void) sata_remove_hba_instance(dip);
835 		if (sata_hba_list == NULL)
836 			sata_event_thread_control(0);
837 	}
838 
839 	if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) {
840 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
841 		taskq_destroy(sata_hba_inst->satahba_taskq);
842 	}
843 
844 	if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED)
845 		(void) scsi_hba_detach(dip);
846 
847 	if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) {
848 		mutex_destroy(&sata_hba_inst->satahba_mutex);
849 		kmem_free((void *)sata_hba_inst,
850 		    sizeof (struct sata_hba_inst));
851 		scsi_hba_tran_free(scsi_tran);
852 	}
853 
854 	sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed",
855 	    ddi_driver_name(dip), ddi_get_instance(dip));
856 
857 	return (DDI_FAILURE);
858 }
859 
860 
861 /*
862  * Called by SATA HBA from to detach an instance of the driver.
863  *
864  * For DDI_DETACH command:
865  * Free local structures allocated for SATA HBA instance during
866  * sata_hba_attach processing.
867  *
868  * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise.
869  *
870  * For DDI_SUSPEND command:
871  * Not implemented at this time (postponed until phase 2 of the development)
872  * Returnd DDI_SUCCESS.
873  *
874  * When the last HBA instance is detached, the event daemon is terminated.
875  *
876  * NOTE: cport support only, no port multiplier support.
877  */
878 int
879 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
880 {
881 	dev_info_t	*tdip;
882 	sata_hba_inst_t	*sata_hba_inst;
883 	scsi_hba_tran_t *scsi_hba_tran;
884 	sata_cport_info_t *cportinfo;
885 	sata_drive_info_t *sdinfo;
886 	int ncport;
887 
888 	SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n",
889 	    ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip));
890 
891 	switch (cmd) {
892 	case DDI_DETACH:
893 
894 		if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
895 			return (DDI_FAILURE);
896 
897 		sata_hba_inst = scsi_hba_tran->tran_hba_private;
898 		if (sata_hba_inst == NULL)
899 			return (DDI_FAILURE);
900 
901 		if (scsi_hba_detach(dip) == DDI_FAILURE) {
902 			sata_hba_inst->satahba_attached = 1;
903 			return (DDI_FAILURE);
904 		}
905 
906 		/*
907 		 * Free all target nodes - at this point
908 		 * devices should be at least offlined
909 		 * otherwise scsi_hba_detach() should not be called.
910 		 */
911 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
912 		    ncport++) {
913 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
914 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
915 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
916 				if (sdinfo != NULL) {
917 					tdip = sata_get_target_dip(dip,
918 					    ncport);
919 					if (tdip != NULL) {
920 						if (ndi_devi_offline(tdip,
921 						    NDI_DEVI_REMOVE) !=
922 						    NDI_SUCCESS) {
923 							SATA_LOG_D((
924 							    sata_hba_inst,
925 							    CE_WARN,
926 							    "sata_hba_detach: "
927 							    "Target node not "
928 							    "removed !"));
929 							return (DDI_FAILURE);
930 						}
931 					}
932 				}
933 			}
934 		}
935 		/*
936 		 * Disable sata event daemon processing for this HBA
937 		 */
938 		sata_hba_inst->satahba_attached = 0;
939 
940 		/*
941 		 * Remove event daemon thread, if it is last HBA instance.
942 		 */
943 
944 		mutex_enter(&sata_mutex);
945 		if (sata_hba_list->satahba_next == NULL) {
946 			mutex_exit(&sata_mutex);
947 			sata_event_thread_control(0);
948 			mutex_enter(&sata_mutex);
949 		}
950 		mutex_exit(&sata_mutex);
951 
952 		/* Remove this HBA instance from the HBA list */
953 		sata_remove_hba_instance(dip);
954 
955 		/*
956 		 * At this point there should be no target nodes attached.
957 		 * Detach and destroy device and port info structures.
958 		 */
959 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
960 		    ncport++) {
961 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
962 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
963 				sdinfo =
964 				    cportinfo->cport_devp.cport_sata_drive;
965 				if (sdinfo != NULL) {
966 					/* Release device structure */
967 					kmem_free(sdinfo,
968 					    sizeof (sata_drive_info_t));
969 				}
970 				/* Release cport info */
971 				mutex_destroy(&cportinfo->cport_mutex);
972 				kmem_free(cportinfo,
973 				    sizeof (sata_cport_info_t));
974 			}
975 		}
976 
977 		scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran);
978 
979 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
980 
981 		taskq_destroy(sata_hba_inst->satahba_taskq);
982 
983 		mutex_destroy(&sata_hba_inst->satahba_mutex);
984 		kmem_free((void *)sata_hba_inst,
985 		    sizeof (struct sata_hba_inst));
986 
987 		return (DDI_SUCCESS);
988 
989 	case DDI_SUSPEND:
990 		/*
991 		 * Postponed until phase 2
992 		 */
993 		return (DDI_FAILURE);
994 
995 	default:
996 		return (DDI_FAILURE);
997 	}
998 }
999 
1000 
1001 /*
1002  * Called by an HBA drive from _fini() routine.
1003  * Unregisters SATA HBA instance/SATA framework pair from the scsi framework.
1004  */
1005 void
1006 sata_hba_fini(struct modlinkage *modlp)
1007 {
1008 	SATADBG1(SATA_DBG_HBA_IF, NULL,
1009 	    "sata_hba_fini: name %s\n",
1010 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
1011 
1012 	scsi_hba_fini(modlp);
1013 }
1014 
1015 
1016 /*
1017  * Default open and close routine for sata_hba framework.
1018  *
1019  */
1020 /*
1021  * Open devctl node.
1022  *
1023  * Returns:
1024  * 0 if node was open successfully, error code otherwise.
1025  *
1026  *
1027  */
1028 
1029 static int
1030 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1031 {
1032 #ifndef __lock_lint
1033 	_NOTE(ARGUNUSED(credp))
1034 #endif
1035 	int rv = 0;
1036 	dev_info_t *dip;
1037 	scsi_hba_tran_t *scsi_hba_tran;
1038 	sata_hba_inst_t	*sata_hba_inst;
1039 
1040 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL);
1041 
1042 	if (otyp != OTYP_CHR)
1043 		return (EINVAL);
1044 
1045 	dip = sata_devt_to_devinfo(*devp);
1046 	if (dip == NULL)
1047 		return (ENXIO);
1048 
1049 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1050 		return (ENXIO);
1051 
1052 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1053 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1054 		return (ENXIO);
1055 
1056 	mutex_enter(&sata_mutex);
1057 	if (flags & FEXCL) {
1058 		if (sata_hba_inst->satahba_open_flag != 0) {
1059 			rv = EBUSY;
1060 		} else {
1061 			sata_hba_inst->satahba_open_flag =
1062 			    SATA_DEVCTL_EXOPENED;
1063 		}
1064 	} else {
1065 		if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) {
1066 			rv = EBUSY;
1067 		} else {
1068 			sata_hba_inst->satahba_open_flag =
1069 			    SATA_DEVCTL_SOPENED;
1070 		}
1071 	}
1072 	mutex_exit(&sata_mutex);
1073 
1074 	return (rv);
1075 }
1076 
1077 
1078 /*
1079  * Close devctl node.
1080  * Returns:
1081  * 0 if node was closed successfully, error code otherwise.
1082  *
1083  */
1084 
1085 static int
1086 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
1087 {
1088 #ifndef __lock_lint
1089 	_NOTE(ARGUNUSED(credp))
1090 	_NOTE(ARGUNUSED(flag))
1091 #endif
1092 	dev_info_t *dip;
1093 	scsi_hba_tran_t *scsi_hba_tran;
1094 	sata_hba_inst_t	*sata_hba_inst;
1095 
1096 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL);
1097 
1098 	if (otyp != OTYP_CHR)
1099 		return (EINVAL);
1100 
1101 	dip = sata_devt_to_devinfo(dev);
1102 	if (dip == NULL)
1103 		return (ENXIO);
1104 
1105 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1106 		return (ENXIO);
1107 
1108 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1109 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1110 		return (ENXIO);
1111 
1112 	mutex_enter(&sata_mutex);
1113 	sata_hba_inst->satahba_open_flag = 0;
1114 	mutex_exit(&sata_mutex);
1115 	return (0);
1116 }
1117 
1118 
1119 
1120 /*
1121  * Standard IOCTL commands for SATA hotplugging.
1122  * Implemented DEVCTL_AP commands:
1123  * DEVCTL_AP_CONNECT
1124  * DEVCTL_AP_DISCONNECT
1125  * DEVCTL_AP_CONFIGURE
1126  * DEVCTL_UNCONFIGURE
1127  * DEVCTL_AP_CONTROL
1128  *
1129  * Commands passed to default ndi ioctl handler:
1130  * DEVCTL_DEVICE_GETSTATE
1131  * DEVCTL_DEVICE_ONLINE
1132  * DEVCTL_DEVICE_OFFLINE
1133  * DEVCTL_DEVICE_REMOVE
1134  * DEVCTL_DEVICE_INSERT
1135  * DEVCTL_BUS_GETSTATE
1136  *
1137  * All other cmds are passed to HBA if it provide ioctl handler, or failed
1138  * if not.
1139  *
1140  * Returns:
1141  * 0 if successful,
1142  * error code if operation failed.
1143  *
1144  * NOTE: Port Multiplier is not supported.
1145  *
1146  */
1147 
1148 static int
1149 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1150     int *rvalp)
1151 {
1152 #ifndef __lock_lint
1153 	_NOTE(ARGUNUSED(credp))
1154 	_NOTE(ARGUNUSED(rvalp))
1155 #endif
1156 	int rv = 0;
1157 	int32_t	comp_port = -1;
1158 	dev_info_t *dip;
1159 	devctl_ap_state_t ap_state;
1160 	struct devctl_iocdata *dcp = NULL;
1161 	scsi_hba_tran_t *scsi_hba_tran;
1162 	sata_hba_inst_t *sata_hba_inst;
1163 	sata_device_t sata_device;
1164 	sata_cport_info_t *cportinfo;
1165 	int cport, pmport, qual;
1166 	int rval = SATA_SUCCESS;
1167 
1168 	dip = sata_devt_to_devinfo(dev);
1169 	if (dip == NULL)
1170 		return (ENXIO);
1171 
1172 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1173 		return (ENXIO);
1174 
1175 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1176 	if (sata_hba_inst == NULL)
1177 		return (ENXIO);
1178 
1179 	if (sata_hba_inst->satahba_tran == NULL)
1180 		return (ENXIO);
1181 
1182 	switch (cmd) {
1183 
1184 	case DEVCTL_DEVICE_GETSTATE:
1185 	case DEVCTL_DEVICE_ONLINE:
1186 	case DEVCTL_DEVICE_OFFLINE:
1187 	case DEVCTL_DEVICE_REMOVE:
1188 	case DEVCTL_BUS_GETSTATE:
1189 		/*
1190 		 * There may be more cases that we want to pass to default
1191 		 * handler rather than fail them.
1192 		 */
1193 		return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
1194 	}
1195 
1196 	/* read devctl ioctl data */
1197 	if (cmd != DEVCTL_AP_CONTROL) {
1198 		if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1199 			return (EFAULT);
1200 
1201 		if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) ==
1202 		    -1) {
1203 			if (dcp)
1204 				ndi_dc_freehdl(dcp);
1205 			return (EINVAL);
1206 		}
1207 
1208 		cport = SCSI_TO_SATA_CPORT(comp_port);
1209 		pmport = SCSI_TO_SATA_PMPORT(comp_port);
1210 		/* Only cport is considered now, i.e. SATA_ADDR_CPORT */
1211 		qual = SATA_ADDR_CPORT;
1212 		if (sata_validate_sata_address(sata_hba_inst, cport, pmport,
1213 		    qual) != 0) {
1214 			ndi_dc_freehdl(dcp);
1215 			return (EINVAL);
1216 		}
1217 
1218 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1219 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1220 		    cport_mutex);
1221 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1222 			/*
1223 			 * Cannot process ioctl request now. Come back later.
1224 			 */
1225 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1226 			    cport_mutex);
1227 			ndi_dc_freehdl(dcp);
1228 			return (EBUSY);
1229 		}
1230 		/* Block event processing for this port */
1231 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1232 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1233 
1234 		sata_device.satadev_addr.cport = cport;
1235 		sata_device.satadev_addr.pmport = pmport;
1236 		sata_device.satadev_addr.qual = qual;
1237 		sata_device.satadev_rev = SATA_DEVICE_REV;
1238 	}
1239 
1240 	switch (cmd) {
1241 
1242 	case DEVCTL_AP_DISCONNECT:
1243 
1244 		/*
1245 		 * Normally, cfgadm sata plugin will try to offline
1246 		 * (unconfigure) device before this request. Nevertheless,
1247 		 * if a device is still configured, we need to
1248 		 * attempt to offline and unconfigure device first, and we will
1249 		 * deactivate the port regardless of the unconfigure
1250 		 * operation results.
1251 		 *
1252 		 */
1253 		rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device);
1254 
1255 		break;
1256 
1257 	case DEVCTL_AP_UNCONFIGURE:
1258 
1259 		/*
1260 		 * The unconfigure operation uses generic nexus operation to
1261 		 * offline a device. It leaves a target device node attached.
1262 		 * and obviously sata_drive_info attached as well, because
1263 		 * from the hardware point of view nothing has changed.
1264 		 */
1265 		rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device);
1266 		break;
1267 
1268 	case DEVCTL_AP_CONNECT:
1269 	{
1270 		/*
1271 		 * The sata cfgadm pluging will invoke this operation only if
1272 		 * port was found in the disconnect state (failed state
1273 		 * is also treated as the disconnected state).
1274 		 * If port activation is successful and a device is found
1275 		 * attached to the port, the initialization sequence is
1276 		 * executed to probe the port and attach
1277 		 * a device structure to a port structure. The device is not
1278 		 * set in configured state (system-wise) by this operation.
1279 		 */
1280 
1281 		rv = sata_ioctl_connect(sata_hba_inst, &sata_device);
1282 
1283 		break;
1284 	}
1285 
1286 	case DEVCTL_AP_CONFIGURE:
1287 	{
1288 		/*
1289 		 * A port may be in an active or shutdown state.
1290 		 * If port is in a failed state, operation is aborted.
1291 		 * If a port is in a shutdown state, sata_tran_port_activate()
1292 		 * is invoked prior to any other operation.
1293 		 *
1294 		 * Onlining the device involves creating a new target node.
1295 		 * If there is an old target node present (belonging to
1296 		 * previously removed device), the operation is aborted - the
1297 		 * old node has to be released and removed before configure
1298 		 * operation is attempted.
1299 		 */
1300 
1301 		rv = sata_ioctl_configure(sata_hba_inst, &sata_device);
1302 
1303 		break;
1304 	}
1305 
1306 	case DEVCTL_AP_GETSTATE:
1307 
1308 		sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state);
1309 
1310 		ap_state.ap_last_change = (time_t)-1;
1311 		ap_state.ap_error_code = 0;
1312 		ap_state.ap_in_transition = 0;
1313 
1314 		/* Copy the return AP-state information to the user space */
1315 		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
1316 			rv = EFAULT;
1317 		}
1318 		break;
1319 
1320 	case DEVCTL_AP_CONTROL:
1321 	{
1322 		/*
1323 		 * Generic devctl for hardware specific functionality
1324 		 */
1325 		sata_ioctl_data_t	ioc;
1326 
1327 		ASSERT(dcp == NULL);
1328 
1329 		/* Copy in user ioctl data first */
1330 #ifdef _MULTI_DATAMODEL
1331 		if (ddi_model_convert_from(mode & FMODELS) ==
1332 		    DDI_MODEL_ILP32) {
1333 
1334 			sata_ioctl_data_32_t	ioc32;
1335 
1336 			if (ddi_copyin((void *)arg, (void *)&ioc32,
1337 			    sizeof (ioc32), mode) != 0) {
1338 				rv = EFAULT;
1339 				break;
1340 			}
1341 			ioc.cmd 	= (uint_t)ioc32.cmd;
1342 			ioc.port	= (uint_t)ioc32.port;
1343 			ioc.get_size	= (uint_t)ioc32.get_size;
1344 			ioc.buf		= (caddr_t)(uintptr_t)ioc32.buf;
1345 			ioc.bufsiz	= (uint_t)ioc32.bufsiz;
1346 			ioc.misc_arg	= (uint_t)ioc32.misc_arg;
1347 		} else
1348 #endif /* _MULTI_DATAMODEL */
1349 		if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
1350 		    mode) != 0) {
1351 			return (EFAULT);
1352 		}
1353 
1354 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
1355 		    "sata_hba_ioctl: DEVCTL_AP_CONTROL "
1356 		    "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port);
1357 
1358 		/*
1359 		 * To avoid BE/LE and 32/64 issues, a get_size always returns
1360 		 * a 32-bit number.
1361 		 */
1362 		if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
1363 			return (EINVAL);
1364 		}
1365 		/* validate address */
1366 		cport = SCSI_TO_SATA_CPORT(ioc.port);
1367 		pmport = SCSI_TO_SATA_PMPORT(ioc.port);
1368 		qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port);
1369 
1370 		/* Override address qualifier - handle cport only for now */
1371 		qual = SATA_ADDR_CPORT;
1372 
1373 		if (sata_validate_sata_address(sata_hba_inst, cport,
1374 		    pmport, qual) != 0)
1375 			return (EINVAL);
1376 
1377 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1378 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1379 		    cport_mutex);
1380 		/* Is the port locked by event processing daemon ? */
1381 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1382 			/*
1383 			 * Cannot process ioctl request now. Come back later
1384 			 */
1385 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1386 			    cport_mutex);
1387 			return (EBUSY);
1388 		}
1389 		/* Block event processing for this port */
1390 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1391 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1392 
1393 
1394 		sata_device.satadev_addr.cport = cport;
1395 		sata_device.satadev_addr.pmport = pmport;
1396 		sata_device.satadev_addr.qual = qual;
1397 		sata_device.satadev_rev = SATA_DEVICE_REV;
1398 
1399 		switch (ioc.cmd) {
1400 
1401 		case SATA_CFGA_RESET_PORT:
1402 			/*
1403 			 * There is no protection for configured device.
1404 			 */
1405 			rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device);
1406 			break;
1407 
1408 		case SATA_CFGA_RESET_DEVICE:
1409 			/*
1410 			 * There is no protection for configured device.
1411 			 */
1412 			rv = sata_ioctl_reset_device(sata_hba_inst,
1413 			    &sata_device);
1414 			break;
1415 
1416 		case SATA_CFGA_RESET_ALL:
1417 			/*
1418 			 * There is no protection for configured devices.
1419 			 */
1420 			rv = sata_ioctl_reset_all(sata_hba_inst);
1421 			/*
1422 			 * We return here, because common return is for
1423 			 * a single port operation - we have already unlocked
1424 			 * all ports and no dc handle was allocated.
1425 			 */
1426 			return (rv);
1427 
1428 		case SATA_CFGA_PORT_DEACTIVATE:
1429 			/*
1430 			 * Arbitrarily unconfigure attached device, if any.
1431 			 * Even if the unconfigure fails, proceed with the
1432 			 * port deactivation.
1433 			 */
1434 			rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device);
1435 
1436 			break;
1437 
1438 		case SATA_CFGA_PORT_ACTIVATE:
1439 
1440 			rv = sata_ioctl_activate(sata_hba_inst, &sata_device);
1441 			break;
1442 
1443 		case SATA_CFGA_PORT_SELF_TEST:
1444 
1445 			rv = sata_ioctl_port_self_test(sata_hba_inst,
1446 			    &sata_device);
1447 			break;
1448 
1449 		case SATA_CFGA_GET_DEVICE_PATH:
1450 			if (qual == SATA_ADDR_CPORT)
1451 				sata_device.satadev_addr.qual =
1452 				    SATA_ADDR_DCPORT;
1453 			else
1454 				sata_device.satadev_addr.qual =
1455 				    SATA_ADDR_DPMPORT;
1456 			rv = sata_ioctl_get_device_path(sata_hba_inst,
1457 			    &sata_device, &ioc, mode);
1458 			break;
1459 
1460 		case SATA_CFGA_GET_AP_TYPE:
1461 
1462 			rv = sata_ioctl_get_ap_type(sata_hba_inst,
1463 			    &sata_device, &ioc, mode);
1464 			break;
1465 
1466 		case SATA_CFGA_GET_MODEL_INFO:
1467 
1468 			rv = sata_ioctl_get_model_info(sata_hba_inst,
1469 			    &sata_device, &ioc, mode);
1470 			break;
1471 
1472 		case SATA_CFGA_GET_REVFIRMWARE_INFO:
1473 
1474 			rv = sata_ioctl_get_revfirmware_info(sata_hba_inst,
1475 			    &sata_device, &ioc, mode);
1476 			break;
1477 
1478 		case SATA_CFGA_GET_SERIALNUMBER_INFO:
1479 
1480 			rv = sata_ioctl_get_serialnumber_info(sata_hba_inst,
1481 			    &sata_device, &ioc, mode);
1482 			break;
1483 
1484 		default:
1485 			rv = EINVAL;
1486 			break;
1487 
1488 		} /* End of DEVCTL_AP_CONTROL cmd switch */
1489 
1490 		break;
1491 	}
1492 
1493 	default:
1494 	{
1495 		/*
1496 		 * If we got here, we got an IOCTL that SATA HBA Framework
1497 		 * does not recognize. Pass ioctl to HBA driver, in case
1498 		 * it could process it.
1499 		 */
1500 		sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran;
1501 		dev_info_t	*mydip = SATA_DIP(sata_hba_inst);
1502 
1503 		SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1504 		    "IOCTL 0x%2x not supported in SATA framework, "
1505 		    "passthrough to HBA", cmd);
1506 
1507 		if (sata_tran->sata_tran_ioctl == NULL) {
1508 			rv = EINVAL;
1509 			break;
1510 		}
1511 		rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg);
1512 		if (rval != 0) {
1513 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1514 			    "IOCTL 0x%2x failed in HBA", cmd);
1515 			rv = rval;
1516 		}
1517 		break;
1518 	}
1519 
1520 	} /* End of main IOCTL switch */
1521 
1522 	if (dcp) {
1523 		ndi_dc_freehdl(dcp);
1524 	}
1525 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1526 	cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
1527 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1528 
1529 	return (rv);
1530 }
1531 
1532 
1533 /*
1534  * Create error retrieval sata packet
1535  *
1536  * A sata packet is allocated and set-up to contain specified error retrieval
1537  * command and appropriate dma-able data buffer.
1538  * No association with any scsi packet is made and no callback routine is
1539  * specified.
1540  *
1541  * Returns a pointer to sata packet upon successfull packet creation.
1542  * Returns NULL, if packet cannot be created.
1543  */
1544 sata_pkt_t *
1545 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device,
1546     int pkt_type)
1547 {
1548 	sata_hba_inst_t	*sata_hba_inst;
1549 	sata_pkt_txlate_t *spx;
1550 	sata_pkt_t *spkt;
1551 	sata_drive_info_t *sdinfo;
1552 
1553 	mutex_enter(&sata_mutex);
1554 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1555 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1556 		if (SATA_DIP(sata_hba_inst) == dip)
1557 			break;
1558 	}
1559 	mutex_exit(&sata_mutex);
1560 	ASSERT(sata_hba_inst != NULL);
1561 
1562 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
1563 	if (sdinfo == NULL) {
1564 		sata_log(sata_hba_inst, CE_WARN,
1565 		    "sata: error recovery request for non-attached device at "
1566 		    "cport %d", sata_device->satadev_addr.cport);
1567 		return (NULL);
1568 	}
1569 
1570 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1571 	spx->txlt_sata_hba_inst = sata_hba_inst;
1572 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
1573 	spkt = sata_pkt_alloc(spx, NULL);
1574 	if (spkt == NULL) {
1575 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1576 		return (NULL);
1577 	}
1578 	/* address is needed now */
1579 	spkt->satapkt_device.satadev_addr = sata_device->satadev_addr;
1580 
1581 	switch (pkt_type) {
1582 	case SATA_ERR_RETR_PKT_TYPE_NCQ:
1583 		if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS)
1584 			return (spkt);
1585 		break;
1586 
1587 	case SATA_ERR_RETR_PKT_TYPE_ATAPI:
1588 		if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS)
1589 			return (spkt);
1590 		break;
1591 
1592 	default:
1593 		break;
1594 	}
1595 
1596 	sata_pkt_free(spx);
1597 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1598 	return (NULL);
1599 
1600 }
1601 
1602 
1603 /*
1604  * Free error retrieval sata packet
1605  *
1606  * Free sata packet and any associated resources allocated previously by
1607  * sata_get_error_retrieval_pkt().
1608  *
1609  * Void return.
1610  */
1611 void
1612 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt)
1613 {
1614 	sata_pkt_txlate_t *spx =
1615 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1616 
1617 	ASSERT(sata_pkt != NULL);
1618 
1619 	sata_free_local_buffer(spx);
1620 	sata_pkt_free(spx);
1621 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1622 
1623 }
1624 
1625 
1626 /* ****************** SCSA required entry points *********************** */
1627 
1628 /*
1629  * Implementation of scsi tran_tgt_init.
1630  * sata_scsi_tgt_init() initializes scsi_device structure
1631  *
1632  * If successful, DDI_SUCCESS is returned.
1633  * DDI_FAILURE is returned if addressed device does not exist
1634  */
1635 
1636 static int
1637 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1638     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1639 {
1640 #ifndef __lock_lint
1641 	_NOTE(ARGUNUSED(hba_dip))
1642 	_NOTE(ARGUNUSED(tgt_dip))
1643 #endif
1644 	sata_device_t		sata_device;
1645 	sata_drive_info_t	*sdinfo;
1646 	struct sata_id		*sid;
1647 	sata_hba_inst_t		*sata_hba_inst;
1648 	char			model[SATA_ID_MODEL_LEN + 1];
1649 	char			fw[SATA_ID_FW_LEN + 1];
1650 	char			*vid, *pid;
1651 	int			i;
1652 
1653 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
1654 
1655 	/* Validate scsi device address */
1656 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
1657 	    &sata_device) != 0)
1658 		return (DDI_FAILURE);
1659 
1660 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1661 	    sata_device.satadev_addr.cport)));
1662 
1663 	/* sata_device now contains a valid sata address */
1664 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
1665 	if (sdinfo == NULL) {
1666 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1667 		    sata_device.satadev_addr.cport)));
1668 		return (DDI_FAILURE);
1669 	}
1670 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1671 	    sata_device.satadev_addr.cport)));
1672 
1673 	/*
1674 	 * Check if we need to create a legacy devid (i.e cmdk style) for
1675 	 * the target disks.
1676 	 *
1677 	 * HBA devinfo node will have the property "use-cmdk-devid-format"
1678 	 * if we need to create cmdk-style devid for all the disk devices
1679 	 * attached to this controller. This property may have been set
1680 	 * from HBA driver's .conf file or by the HBA driver in its
1681 	 * attach(9F) function.
1682 	 */
1683 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
1684 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
1685 	    "use-cmdk-devid-format", 0) == 1)) {
1686 		/* register a legacy devid for this target node */
1687 		sata_target_devid_register(tgt_dip, sdinfo);
1688 	}
1689 
1690 
1691 	/*
1692 	 * 'Identify Device Data' does not always fit in standard SCSI
1693 	 * INQUIRY data, so establish INQUIRY_* properties with full-form
1694 	 * of information.
1695 	 */
1696 	sid = &sdinfo->satadrv_id;
1697 #ifdef	_LITTLE_ENDIAN
1698 	swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
1699 	swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
1700 #else	/* _LITTLE_ENDIAN */
1701 	bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN);
1702 	bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN);
1703 #endif	/* _LITTLE_ENDIAN */
1704 	model[SATA_ID_MODEL_LEN] = 0;
1705 	fw[SATA_ID_FW_LEN] = 0;
1706 
1707 	/* split model into into vid/pid */
1708 	for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++)
1709 		if ((*pid == ' ') || (*pid == '\t'))
1710 			break;
1711 	if (i < SATA_ID_MODEL_LEN) {
1712 		vid = model;
1713 		*pid++ = 0;		/* terminate vid, establish pid */
1714 	} else {
1715 		vid = NULL;		/* vid will stay "ATA     " */
1716 		pid = model;		/* model is all pid */
1717 	}
1718 
1719 	if (vid)
1720 		(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_VENDOR_ID,
1721 		    vid, strlen(vid));
1722 	if (pid)
1723 		(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID,
1724 		    pid, strlen(pid));
1725 	(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_REVISION_ID,
1726 	    fw, strlen(fw));
1727 
1728 	return (DDI_SUCCESS);
1729 }
1730 
1731 /*
1732  * Implementation of scsi tran_tgt_probe.
1733  * Probe target, by calling default scsi routine scsi_hba_probe()
1734  */
1735 static int
1736 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void))
1737 {
1738 	sata_hba_inst_t *sata_hba_inst =
1739 	    (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private);
1740 	int rval;
1741 
1742 	rval = scsi_hba_probe(sd, callback);
1743 
1744 	if (rval == SCSIPROBE_EXISTS) {
1745 		/*
1746 		 * Set property "pm-capable" on the target device node, so that
1747 		 * the target driver will not try to fetch scsi cycle counters
1748 		 * before enabling device power-management.
1749 		 */
1750 		if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
1751 		    "pm-capable", 1)) != DDI_PROP_SUCCESS) {
1752 			sata_log(sata_hba_inst, CE_WARN,
1753 			    "SATA device at port %d: "
1754 			    "will not be power-managed ",
1755 			    SCSI_TO_SATA_CPORT(sd->sd_address.a_target));
1756 			SATA_LOG_D((sata_hba_inst, CE_WARN,
1757 			    "failure updating pm-capable property"));
1758 		}
1759 	}
1760 	return (rval);
1761 }
1762 
1763 /*
1764  * Implementation of scsi tran_tgt_free.
1765  * Release all resources allocated for scsi_device
1766  */
1767 static void
1768 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1769     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1770 {
1771 #ifndef __lock_lint
1772 	_NOTE(ARGUNUSED(hba_dip))
1773 #endif
1774 	sata_device_t		sata_device;
1775 	sata_drive_info_t	*sdinfo;
1776 	sata_hba_inst_t		*sata_hba_inst;
1777 	ddi_devid_t		devid;
1778 
1779 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
1780 
1781 	/* Validate scsi device address */
1782 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
1783 	    &sata_device) != 0)
1784 		return;
1785 
1786 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1787 	    sata_device.satadev_addr.cport)));
1788 
1789 	/* sata_device now should contain a valid sata address */
1790 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
1791 	if (sdinfo == NULL) {
1792 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1793 		    sata_device.satadev_addr.cport)));
1794 		return;
1795 	}
1796 	/*
1797 	 * We did not allocate any resources in sata_scsi_tgt_init()
1798 	 * other than few properties.
1799 	 * Free them.
1800 	 */
1801 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1802 	    sata_device.satadev_addr.cport)));
1803 	(void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable");
1804 
1805 	/*
1806 	 * If devid was previously created but not freed up from
1807 	 * sd(7D) driver (i.e during detach(9F)) then do it here.
1808 	 */
1809 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
1810 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
1811 	    "use-cmdk-devid-format", 0) == 1) &&
1812 	    (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) {
1813 		ddi_devid_unregister(tgt_dip);
1814 		ddi_devid_free(devid);
1815 	}
1816 }
1817 
1818 /*
1819  * Implementation of scsi tran_init_pkt
1820  * Upon successful return, scsi pkt buffer has DMA resources allocated.
1821  *
1822  * It seems that we should always allocate pkt, even if the address is
1823  * for non-existing device - just use some default for dma_attr.
1824  * The reason is that there is no way to communicate this to a caller here.
1825  * Subsequent call to sata_scsi_start may fail appropriately.
1826  * Simply returning NULL does not seem to discourage a target driver...
1827  *
1828  * Returns a pointer to initialized scsi_pkt, or NULL otherwise.
1829  */
1830 static struct scsi_pkt *
1831 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
1832     struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
1833     int (*callback)(caddr_t), caddr_t arg)
1834 {
1835 	sata_hba_inst_t *sata_hba_inst =
1836 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
1837 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
1838 	sata_device_t sata_device;
1839 	sata_drive_info_t *sdinfo;
1840 	sata_pkt_txlate_t *spx;
1841 	ddi_dma_attr_t cur_dma_attr;
1842 	int rval;
1843 	boolean_t new_pkt = TRUE;
1844 
1845 	ASSERT(ap->a_hba_tran->tran_hba_dip == dip);
1846 
1847 	/*
1848 	 * We need to translate the address, even if it could be
1849 	 * a bogus one, for a non-existing device
1850 	 */
1851 	sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
1852 	sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target);
1853 	sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
1854 	sata_device.satadev_rev = SATA_DEVICE_REV;
1855 
1856 	if (pkt == NULL) {
1857 		/*
1858 		 * Have to allocate a brand new scsi packet.
1859 		 * We need to operate with auto request sense enabled.
1860 		 */
1861 		pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen,
1862 		    MAX(statuslen, sizeof (struct scsi_arq_status)),
1863 		    tgtlen, sizeof (sata_pkt_txlate_t), callback, arg);
1864 
1865 		if (pkt == NULL)
1866 			return (NULL);
1867 
1868 		/* Fill scsi packet structure */
1869 		pkt->pkt_comp		= (void (*)())NULL;
1870 		pkt->pkt_time		= 0;
1871 		pkt->pkt_resid		= 0;
1872 		pkt->pkt_statistics	= 0;
1873 		pkt->pkt_reason		= 0;
1874 
1875 		/*
1876 		 * pkt_hba_private will point to sata pkt txlate structure
1877 		 */
1878 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
1879 		bzero(spx, sizeof (sata_pkt_txlate_t));
1880 
1881 		spx->txlt_scsi_pkt = pkt;
1882 		spx->txlt_sata_hba_inst = sata_hba_inst;
1883 
1884 		/* Allocate sata_pkt */
1885 		spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback);
1886 		if (spx->txlt_sata_pkt == NULL) {
1887 			/* Could not allocate sata pkt */
1888 			scsi_hba_pkt_free(ap, pkt);
1889 			return (NULL);
1890 		}
1891 		/* Set sata address */
1892 		spx->txlt_sata_pkt->satapkt_device.satadev_addr =
1893 		    sata_device.satadev_addr;
1894 		spx->txlt_sata_pkt->satapkt_device.satadev_rev =
1895 		    sata_device.satadev_rev;
1896 
1897 		if ((bp == NULL) || (bp->b_bcount == 0))
1898 			return (pkt);
1899 
1900 		spx->txlt_total_residue = bp->b_bcount;
1901 	} else {
1902 		new_pkt = FALSE;
1903 		/*
1904 		 * Packet was preallocated/initialized by previous call
1905 		 */
1906 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
1907 
1908 		if ((bp == NULL) || (bp->b_bcount == 0)) {
1909 			return (pkt);
1910 		}
1911 		ASSERT(spx->txlt_buf_dma_handle != NULL);
1912 
1913 		/* Pkt is available already: spx->txlt_scsi_pkt == pkt; */
1914 	}
1915 
1916 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
1917 
1918 	/*
1919 	 * We use an adjusted version of the dma_attr, to account
1920 	 * for device addressing limitations.
1921 	 * sata_adjust_dma_attr() will handle sdinfo == NULL which may
1922 	 * happen when a device is not yet configured.
1923 	 */
1924 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1925 	    sata_device.satadev_addr.cport)));
1926 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
1927 	    &spx->txlt_sata_pkt->satapkt_device);
1928 	/* NULL sdinfo may be passsed to sata_adjust_dma_attr() */
1929 	sata_adjust_dma_attr(sdinfo,
1930 	    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
1931 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1932 	    sata_device.satadev_addr.cport)));
1933 	/*
1934 	 * Allocate necessary DMA resources for the packet's data buffer
1935 	 * NOTE:
1936 	 * In case of read/write commands, DMA resource allocation here is
1937 	 * based on the premise that the transfer length specified in
1938 	 * the read/write scsi cdb will match exactly DMA resources -
1939 	 * returning correct packet residue is crucial.
1940 	 */
1941 	if ((rval = sata_dma_buf_setup(spx, flags, callback, arg,
1942 	    &cur_dma_attr)) != DDI_SUCCESS) {
1943 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
1944 		sata_pkt_free(spx);
1945 		/*
1946 		 * If a DMA allocation request fails with
1947 		 * DDI_DMA_NOMAPPING, indicate the error by calling
1948 		 * bioerror(9F) with bp and an error code of EFAULT.
1949 		 * If a DMA allocation request fails with
1950 		 * DDI_DMA_TOOBIG, indicate the error by calling
1951 		 * bioerror(9F) with bp and an error code of EINVAL.
1952 		 */
1953 		switch (rval) {
1954 		case DDI_DMA_NORESOURCES:
1955 			bioerror(bp, 0);
1956 			break;
1957 		case DDI_DMA_NOMAPPING:
1958 		case DDI_DMA_BADATTR:
1959 			bioerror(bp, EFAULT);
1960 			break;
1961 		case DDI_DMA_TOOBIG:
1962 		default:
1963 			bioerror(bp, EINVAL);
1964 			break;
1965 		}
1966 		if (new_pkt == TRUE)
1967 			scsi_hba_pkt_free(ap, pkt);
1968 		return (NULL);
1969 	}
1970 	/* Set number of bytes that are not yet accounted for */
1971 	pkt->pkt_resid = spx->txlt_total_residue;
1972 	ASSERT(pkt->pkt_resid >= 0);
1973 
1974 	return (pkt);
1975 }
1976 
1977 /*
1978  * Implementation of scsi tran_start.
1979  * Translate scsi cmd into sata operation and return status.
1980  * ATAPI CDBs are passed to ATAPI devices - the device determines what commands
1981  * are supported.
1982  * For SATA hard disks, supported scsi commands:
1983  * SCMD_INQUIRY
1984  * SCMD_TEST_UNIT_READY
1985  * SCMD_START_STOP
1986  * SCMD_READ_CAPACITY
1987  * SCMD_REQUEST_SENSE
1988  * SCMD_LOG_SENSE_G1
1989  * SCMD_LOG_SELECT_G1
1990  * SCMD_MODE_SENSE	(specific pages)
1991  * SCMD_MODE_SENSE_G1	(specific pages)
1992  * SCMD_MODE_SELECT	(specific pages)
1993  * SCMD_MODE_SELECT_G1	(specific pages)
1994  * SCMD_SYNCHRONIZE_CACHE
1995  * SCMD_SYNCHRONIZE_CACHE_G1
1996  * SCMD_READ
1997  * SCMD_READ_G1
1998  * SCMD_READ_G4
1999  * SCMD_READ_G5
2000  * SCMD_WRITE
2001  * SCMD_WRITE_BUFFER
2002  * SCMD_WRITE_G1
2003  * SCMD_WRITE_G4
2004  * SCMD_WRITE_G5
2005  * SCMD_SEEK		(noop)
2006  * SCMD_SDIAG
2007  *
2008  * All other commands are rejected as unsupported.
2009  *
2010  * Returns:
2011  * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver
2012  * for execution. TRAN_ACCEPT may be returned also if device was removed but
2013  * a callback could be scheduled.
2014  * TRAN_BADPKT if cmd was directed to invalid address.
2015  * TRAN_FATAL_ERROR is command was rejected due to hardware error, including
2016  * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device
2017  * was removed and there was no callback specified in scsi pkt.
2018  * TRAN_BUSY if command could not be executed becasue HBA driver or SATA
2019  * framework was busy performing some other operation(s).
2020  *
2021  */
2022 static int
2023 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
2024 {
2025 	sata_hba_inst_t *sata_hba_inst =
2026 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2027 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2028 	sata_drive_info_t *sdinfo;
2029 	struct buf *bp;
2030 	int cport;
2031 	int rval;
2032 
2033 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2034 	    "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]);
2035 
2036 	ASSERT(spx != NULL &&
2037 	    spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL);
2038 
2039 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
2040 
2041 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2042 	sdinfo = sata_get_device_info(sata_hba_inst,
2043 	    &spx->txlt_sata_pkt->satapkt_device);
2044 	if (sdinfo == NULL ||
2045 	    SATA_CPORT_INFO(sata_hba_inst, cport)->cport_tgtnode_clean ==
2046 	    B_FALSE) {
2047 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2048 		pkt->pkt_reason = CMD_DEV_GONE;
2049 		/*
2050 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2051 		 * only in callback function (for normal requests) and
2052 		 * in the dump code path.
2053 		 * So, if the callback is available, we need to do
2054 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2055 		 */
2056 		if (pkt->pkt_comp != NULL) {
2057 			/* scsi callback required */
2058 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2059 			    (task_func_t *)pkt->pkt_comp,
2060 			    (void *)pkt, TQ_SLEEP) == NULL)
2061 				/* Scheduling the callback failed */
2062 				return (TRAN_BUSY);
2063 			return (TRAN_ACCEPT);
2064 		}
2065 		/* No callback available */
2066 		return (TRAN_FATAL_ERROR);
2067 	}
2068 
2069 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
2070 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2071 		rval = sata_txlt_atapi(spx);
2072 		SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2073 		    "sata_scsi_start atapi: rval %d\n", rval);
2074 		return (rval);
2075 	}
2076 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2077 
2078 	/* ATA Disk commands processing starts here */
2079 
2080 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2081 
2082 	switch (pkt->pkt_cdbp[0]) {
2083 
2084 	case SCMD_INQUIRY:
2085 		/* Mapped to identify device */
2086 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2087 			bp_mapin(bp);
2088 		rval = sata_txlt_inquiry(spx);
2089 		break;
2090 
2091 	case SCMD_TEST_UNIT_READY:
2092 		/*
2093 		 * SAT "SATA to ATA Translation" doc specifies translation
2094 		 * to ATA CHECK POWER MODE.
2095 		 */
2096 		rval = sata_txlt_test_unit_ready(spx);
2097 		break;
2098 
2099 	case SCMD_START_STOP:
2100 		/* Mapping depends on the command */
2101 		rval = sata_txlt_start_stop_unit(spx);
2102 		break;
2103 
2104 	case SCMD_READ_CAPACITY:
2105 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2106 			bp_mapin(bp);
2107 		rval = sata_txlt_read_capacity(spx);
2108 		break;
2109 
2110 	case SCMD_REQUEST_SENSE:
2111 		/*
2112 		 * Always No Sense, since we force ARQ
2113 		 */
2114 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2115 			bp_mapin(bp);
2116 		rval = sata_txlt_request_sense(spx);
2117 		break;
2118 
2119 	case SCMD_LOG_SENSE_G1:
2120 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2121 			bp_mapin(bp);
2122 		rval = sata_txlt_log_sense(spx);
2123 		break;
2124 
2125 	case SCMD_LOG_SELECT_G1:
2126 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2127 			bp_mapin(bp);
2128 		rval = sata_txlt_log_select(spx);
2129 		break;
2130 
2131 	case SCMD_MODE_SENSE:
2132 	case SCMD_MODE_SENSE_G1:
2133 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2134 			bp_mapin(bp);
2135 		rval = sata_txlt_mode_sense(spx);
2136 		break;
2137 
2138 
2139 	case SCMD_MODE_SELECT:
2140 	case SCMD_MODE_SELECT_G1:
2141 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2142 			bp_mapin(bp);
2143 		rval = sata_txlt_mode_select(spx);
2144 		break;
2145 
2146 	case SCMD_SYNCHRONIZE_CACHE:
2147 	case SCMD_SYNCHRONIZE_CACHE_G1:
2148 		rval = sata_txlt_synchronize_cache(spx);
2149 		break;
2150 
2151 	case SCMD_READ:
2152 	case SCMD_READ_G1:
2153 	case SCMD_READ_G4:
2154 	case SCMD_READ_G5:
2155 		rval = sata_txlt_read(spx);
2156 		break;
2157 	case SCMD_WRITE_BUFFER:
2158 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2159 			bp_mapin(bp);
2160 		rval = sata_txlt_write_buffer(spx);
2161 		break;
2162 
2163 	case SCMD_WRITE:
2164 	case SCMD_WRITE_G1:
2165 	case SCMD_WRITE_G4:
2166 	case SCMD_WRITE_G5:
2167 		rval = sata_txlt_write(spx);
2168 		break;
2169 
2170 	case SCMD_SEEK:
2171 		rval = sata_txlt_nodata_cmd_immediate(spx);
2172 		break;
2173 
2174 		/* Other cases will be filed later */
2175 		/* postponed until phase 2 of the development */
2176 	default:
2177 		rval = sata_txlt_invalid_command(spx);
2178 		break;
2179 	}
2180 
2181 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2182 	    "sata_scsi_start: rval %d\n", rval);
2183 
2184 	return (rval);
2185 }
2186 
2187 /*
2188  * Implementation of scsi tran_abort.
2189  * Abort specific pkt or all packets.
2190  *
2191  * Returns 1 if one or more packets were aborted, returns 0 otherwise
2192  *
2193  * May be called from an interrupt level.
2194  */
2195 static int
2196 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt)
2197 {
2198 	sata_hba_inst_t *sata_hba_inst =
2199 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2200 	sata_device_t	sata_device;
2201 	sata_pkt_t	*sata_pkt;
2202 
2203 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2204 	    "sata_scsi_abort: %s at target: 0x%x\n",
2205 	    scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target);
2206 
2207 	/* Validate address */
2208 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0)
2209 		/* Invalid address */
2210 		return (0);
2211 
2212 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2213 	    sata_device.satadev_addr.cport)));
2214 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2215 		/* invalid address */
2216 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2217 		    sata_device.satadev_addr.cport)));
2218 		return (0);
2219 	}
2220 	if (scsi_pkt == NULL) {
2221 		/*
2222 		 * Abort all packets.
2223 		 * Although we do not have specific packet, we still need
2224 		 * dummy packet structure to pass device address to HBA.
2225 		 * Allocate one, without sleeping. Fail if pkt cannot be
2226 		 * allocated.
2227 		 */
2228 		sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP);
2229 		if (sata_pkt == NULL) {
2230 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2231 			    sata_device.satadev_addr.cport)));
2232 			SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: "
2233 			    "could not allocate sata_pkt"));
2234 			return (0);
2235 		}
2236 		sata_pkt->satapkt_rev = SATA_PKT_REV;
2237 		sata_pkt->satapkt_device = sata_device;
2238 		sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
2239 	} else {
2240 		if (scsi_pkt->pkt_ha_private == NULL) {
2241 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2242 			    sata_device.satadev_addr.cport)));
2243 			return (0); /* Bad scsi pkt */
2244 		}
2245 		/* extract pointer to sata pkt */
2246 		sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)->
2247 		    txlt_sata_pkt;
2248 	}
2249 
2250 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2251 	    sata_device.satadev_addr.cport)));
2252 	/* Send abort request to HBA */
2253 	if ((*SATA_ABORT_FUNC(sata_hba_inst))
2254 	    (SATA_DIP(sata_hba_inst), sata_pkt,
2255 	    scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) ==
2256 	    SATA_SUCCESS) {
2257 		if (scsi_pkt == NULL)
2258 			kmem_free(sata_pkt, sizeof (sata_pkt_t));
2259 		/* Success */
2260 		return (1);
2261 	}
2262 	/* Else, something did not go right */
2263 	if (scsi_pkt == NULL)
2264 		kmem_free(sata_pkt, sizeof (sata_pkt_t));
2265 	/* Failure */
2266 	return (0);
2267 }
2268 
2269 
2270 /*
2271  * Implementation of scsi tran_reset.
2272  * RESET_ALL request is translated into port reset.
2273  * RESET_TARGET requests is translated into a device reset,
2274  * RESET_LUN request is accepted only for LUN 0 and translated into
2275  * device reset.
2276  * The target reset should cause all HBA active and queued packets to
2277  * be terminated and returned with pkt reason SATA_PKT_RESET prior to
2278  * the return. HBA should report reset event for the device.
2279  *
2280  * Returns 1 upon success, 0 upon failure.
2281  */
2282 static int
2283 sata_scsi_reset(struct scsi_address *ap, int level)
2284 {
2285 	sata_hba_inst_t	*sata_hba_inst =
2286 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2287 	sata_device_t	sata_device;
2288 	int		val;
2289 
2290 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2291 	    "sata_scsi_reset: level %d target: 0x%x\n",
2292 	    level, ap->a_target);
2293 
2294 	/* Validate address */
2295 	val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device);
2296 	if (val == -1)
2297 		/* Invalid address */
2298 		return (0);
2299 
2300 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2301 	    sata_device.satadev_addr.cport)));
2302 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2303 		/* invalid address */
2304 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2305 		    sata_device.satadev_addr.cport)));
2306 		return (0);
2307 	}
2308 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2309 	    sata_device.satadev_addr.cport)));
2310 	if (level == RESET_ALL) {
2311 		/* port reset - cport only */
2312 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2313 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2314 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2315 			return (1);
2316 		else
2317 			return (0);
2318 
2319 	} else if (val == 0 &&
2320 	    (level == RESET_TARGET || level == RESET_LUN)) {
2321 		/* reset device (device attached) */
2322 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2323 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2324 			return (1);
2325 		else
2326 			return (0);
2327 	}
2328 	return (0);
2329 }
2330 
2331 
2332 /*
2333  * Implementation of scsi tran_getcap (get transport/device capabilities).
2334  * Supported capabilities for SATA hard disks:
2335  * auto-rqsense		(always supported)
2336  * tagged-qing		(supported if HBA supports it)
2337  * untagged-qing	(could be supported if disk supports it, but because
2338  *			 caching behavior allowing untagged queuing actually
2339  *			 results in reduced performance.  sd tries to throttle
2340  *			 back to only 3 outstanding commands, which may
2341  *			 work for real SCSI disks, but with read ahead
2342  *			 caching, having more than 1 outstanding command
2343  *			 results in cache thrashing.)
2344  * sector_size
2345  * dma_max
2346  * interconnect-type	(INTERCONNECT_SATA)
2347  *
2348  * Supported capabilities for ATAPI devices (CD/DVD):
2349  * auto-rqsense		(always supported)
2350  * sector_size
2351  * dma_max
2352  * interconnect-type	(INTERCONNECT_SATA)
2353  *
2354  * Request for other capabilities is rejected as unsupported.
2355  *
2356  * Returns supported capability value, or -1 if capability is unsuppported or
2357  * the address is invalid - no device.
2358  */
2359 
2360 static int
2361 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
2362 {
2363 
2364 	sata_hba_inst_t 	*sata_hba_inst =
2365 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2366 	sata_device_t		sata_device;
2367 	sata_drive_info_t	*sdinfo;
2368 	ddi_dma_attr_t		adj_dma_attr;
2369 	int 			rval;
2370 
2371 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2372 	    "sata_scsi_getcap: target: 0x%x, cap: %s\n",
2373 	    ap->a_target, cap);
2374 
2375 	/*
2376 	 * We want to process the capabilities on per port granularity.
2377 	 * So, we are specifically restricting ourselves to whom != 0
2378 	 * to exclude the controller wide handling.
2379 	 */
2380 	if (cap == NULL || whom == 0)
2381 		return (-1);
2382 
2383 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2384 		/* Invalid address */
2385 		return (-1);
2386 	}
2387 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2388 	    sata_device.satadev_addr.cport)));
2389 	if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) ==
2390 	    NULL) {
2391 		/* invalid address */
2392 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2393 		    sata_device.satadev_addr.cport)));
2394 		return (-1);
2395 	}
2396 
2397 	switch (scsi_hba_lookup_capstr(cap)) {
2398 	case SCSI_CAP_ARQ:
2399 		rval = 1;		/* ARQ supported, turned on */
2400 		break;
2401 
2402 	case SCSI_CAP_SECTOR_SIZE:
2403 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK)
2404 			rval = SATA_DISK_SECTOR_SIZE;	/* fixed size */
2405 		else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD)
2406 			rval = SATA_ATAPI_SECTOR_SIZE;
2407 		else rval = -1;
2408 		break;
2409 
2410 	/*
2411 	 * untagged queuing cause a performance inversion because of
2412 	 * the way sd operates.  Because of this reason we do not
2413 	 * use it when available.
2414 	 */
2415 	case SCSI_CAP_UNTAGGED_QING:
2416 		if (sdinfo->satadrv_features_enabled &
2417 		    SATA_DEV_F_E_UNTAGGED_QING)
2418 			rval = 1;	/* Untagged queuing available */
2419 		else
2420 			rval = -1;	/* Untagged queuing not available */
2421 		break;
2422 
2423 	case SCSI_CAP_TAGGED_QING:
2424 		if ((sdinfo->satadrv_features_enabled &
2425 		    SATA_DEV_F_E_TAGGED_QING) &&
2426 		    (sdinfo->satadrv_max_queue_depth > 1))
2427 			rval = 1;	/* Tagged queuing available */
2428 		else
2429 			rval = -1;	/* Tagged queuing not available */
2430 		break;
2431 
2432 	case SCSI_CAP_DMA_MAX:
2433 		sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst),
2434 		    &adj_dma_attr);
2435 		rval = (int)adj_dma_attr.dma_attr_maxxfer;
2436 		/* We rely on the fact that dma_attr_maxxfer < 0x80000000 */
2437 		break;
2438 
2439 	case SCSI_CAP_INTERCONNECT_TYPE:
2440 		rval = INTERCONNECT_SATA;	/* SATA interconnect type */
2441 		break;
2442 
2443 	default:
2444 		rval = -1;
2445 		break;
2446 	}
2447 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2448 	    sata_device.satadev_addr.cport)));
2449 	return (rval);
2450 }
2451 
2452 /*
2453  * Implementation of scsi tran_setcap
2454  *
2455  * Only SCSI_CAP_UNTAGGED_QING and  SCSI_CAP_TAGGED_QING are changeable.
2456  *
2457  */
2458 static int
2459 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
2460 {
2461 	sata_hba_inst_t	*sata_hba_inst =
2462 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2463 	sata_device_t	sata_device;
2464 	sata_drive_info_t	*sdinfo;
2465 	int		rval;
2466 
2467 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2468 	    "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap);
2469 
2470 	/*
2471 	 * We want to process the capabilities on per port granularity.
2472 	 * So, we are specifically restricting ourselves to whom != 0
2473 	 * to exclude the controller wide handling.
2474 	 */
2475 	if (cap == NULL || whom == 0) {
2476 		return (-1);
2477 	}
2478 
2479 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2480 		/* Invalid address */
2481 		return (-1);
2482 	}
2483 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2484 	    sata_device.satadev_addr.cport)));
2485 	if ((sdinfo = sata_get_device_info(sata_hba_inst,
2486 	    &sata_device)) == NULL) {
2487 		/* invalid address */
2488 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2489 		    sata_device.satadev_addr.cport)));
2490 		return (-1);
2491 	}
2492 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2493 	    sata_device.satadev_addr.cport)));
2494 
2495 	switch (scsi_hba_lookup_capstr(cap)) {
2496 	case SCSI_CAP_ARQ:
2497 	case SCSI_CAP_SECTOR_SIZE:
2498 	case SCSI_CAP_DMA_MAX:
2499 	case SCSI_CAP_INTERCONNECT_TYPE:
2500 		rval = 0;
2501 		break;
2502 	case SCSI_CAP_UNTAGGED_QING:
2503 		if (SATA_QDEPTH(sata_hba_inst) > 1) {
2504 			rval = 1;
2505 			if (value == 1) {
2506 				sdinfo->satadrv_features_enabled |=
2507 				    SATA_DEV_F_E_UNTAGGED_QING;
2508 			} else if (value == 0) {
2509 				sdinfo->satadrv_features_enabled &=
2510 				    ~SATA_DEV_F_E_UNTAGGED_QING;
2511 			} else {
2512 				rval = -1;
2513 			}
2514 		} else {
2515 			rval = 0;
2516 		}
2517 		break;
2518 	case SCSI_CAP_TAGGED_QING:
2519 		/* This can TCQ or NCQ */
2520 		if (sata_func_enable & SATA_ENABLE_QUEUING &&
2521 		    ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ &&
2522 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) ||
2523 		    (sata_func_enable & SATA_ENABLE_NCQ &&
2524 		    sdinfo->satadrv_features_support & SATA_DEV_F_NCQ &&
2525 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) &&
2526 		    (sdinfo->satadrv_max_queue_depth > 1)) {
2527 			rval = 1;
2528 			if (value == 1) {
2529 				sdinfo->satadrv_features_enabled |=
2530 				    SATA_DEV_F_E_TAGGED_QING;
2531 			} else if (value == 0) {
2532 				sdinfo->satadrv_features_enabled &=
2533 				    ~SATA_DEV_F_E_TAGGED_QING;
2534 			} else {
2535 				rval = -1;
2536 			}
2537 		} else {
2538 			rval = 0;
2539 		}
2540 		break;
2541 	default:
2542 		rval = -1;
2543 		break;
2544 	}
2545 	return (rval);
2546 }
2547 
2548 /*
2549  * Implementations of scsi tran_destroy_pkt.
2550  * Free resources allocated by sata_scsi_init_pkt()
2551  */
2552 static void
2553 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2554 {
2555 	sata_pkt_txlate_t *spx;
2556 
2557 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2558 
2559 	if (spx->txlt_buf_dma_handle != NULL) {
2560 		if (spx->txlt_tmp_buf != NULL)  {
2561 			ASSERT(spx->txlt_tmp_buf_handle != 0);
2562 			/*
2563 			 * Intermediate DMA buffer was allocated.
2564 			 * Free allocated buffer and associated access handle.
2565 			 */
2566 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
2567 			spx->txlt_tmp_buf = NULL;
2568 		}
2569 		/*
2570 		 * Free DMA resources - cookies and handles
2571 		 */
2572 		if (spx->txlt_dma_cookie_list != NULL) {
2573 			if (spx->txlt_dma_cookie_list !=
2574 			    &spx->txlt_dma_cookie) {
2575 				(void) kmem_free(spx->txlt_dma_cookie_list,
2576 				    spx->txlt_dma_cookie_list_len *
2577 				    sizeof (ddi_dma_cookie_t));
2578 				spx->txlt_dma_cookie_list = NULL;
2579 			}
2580 		}
2581 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
2582 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
2583 	}
2584 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2585 	sata_pkt_free(spx);
2586 
2587 	scsi_hba_pkt_free(ap, pkt);
2588 }
2589 
2590 /*
2591  * Implementation of scsi tran_dmafree.
2592  * Free DMA resources allocated by sata_scsi_init_pkt()
2593  */
2594 
2595 static void
2596 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
2597 {
2598 #ifndef __lock_lint
2599 	_NOTE(ARGUNUSED(ap))
2600 #endif
2601 	sata_pkt_txlate_t *spx;
2602 
2603 	ASSERT(pkt != NULL);
2604 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2605 
2606 	if (spx->txlt_buf_dma_handle != NULL) {
2607 		if (spx->txlt_tmp_buf != NULL)  {
2608 			/*
2609 			 * Intermediate DMA buffer was allocated.
2610 			 * Free allocated buffer and associated access handle.
2611 			 */
2612 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
2613 			spx->txlt_tmp_buf = NULL;
2614 		}
2615 		/*
2616 		 * Free DMA resources - cookies and handles
2617 		 */
2618 		/* ASSERT(spx->txlt_dma_cookie_list != NULL); */
2619 		if (spx->txlt_dma_cookie_list != NULL) {
2620 			if (spx->txlt_dma_cookie_list !=
2621 			    &spx->txlt_dma_cookie) {
2622 				(void) kmem_free(spx->txlt_dma_cookie_list,
2623 				    spx->txlt_dma_cookie_list_len *
2624 				    sizeof (ddi_dma_cookie_t));
2625 				spx->txlt_dma_cookie_list = NULL;
2626 			}
2627 		}
2628 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
2629 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
2630 		spx->txlt_buf_dma_handle = NULL;
2631 	}
2632 }
2633 
2634 /*
2635  * Implementation of scsi tran_sync_pkt.
2636  *
2637  * The assumption below is that pkt is unique - there is no need to check ap
2638  *
2639  * Synchronize DMA buffer and, if the intermediate buffer is used, copy data
2640  * into/from the real buffer.
2641  */
2642 static void
2643 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2644 {
2645 #ifndef __lock_lint
2646 	_NOTE(ARGUNUSED(ap))
2647 #endif
2648 	int rval;
2649 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2650 	struct buf *bp;
2651 	int direction;
2652 
2653 	ASSERT(spx != NULL);
2654 	if (spx->txlt_buf_dma_handle != NULL) {
2655 		direction = spx->txlt_sata_pkt->
2656 		    satapkt_cmd.satacmd_flags.sata_data_direction;
2657 		if (spx->txlt_sata_pkt != NULL &&
2658 		    direction != SATA_DIR_NODATA_XFER) {
2659 			if (spx->txlt_tmp_buf != NULL) {
2660 				/* Intermediate DMA buffer used */
2661 				bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2662 
2663 				if (direction & SATA_DIR_WRITE) {
2664 					bcopy(bp->b_un.b_addr,
2665 					    spx->txlt_tmp_buf, bp->b_bcount);
2666 				}
2667 			}
2668 			/* Sync the buffer for device or for CPU */
2669 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle,   0, 0,
2670 			    (direction & SATA_DIR_WRITE) ?
2671 			    DDI_DMA_SYNC_FORDEV :  DDI_DMA_SYNC_FORCPU);
2672 			ASSERT(rval == DDI_SUCCESS);
2673 			if (spx->txlt_tmp_buf != NULL &&
2674 			    !(direction & SATA_DIR_WRITE)) {
2675 				/* Intermediate DMA buffer used for read */
2676 				bcopy(spx->txlt_tmp_buf,
2677 				    bp->b_un.b_addr, bp->b_bcount);
2678 			}
2679 
2680 		}
2681 	}
2682 }
2683 
2684 
2685 
2686 /* *******************  SATA - SCSI Translation functions **************** */
2687 /*
2688  * SCSI to SATA pkt and command translation and SATA to SCSI status/error
2689  * translation.
2690  */
2691 
2692 /*
2693  * Checks if a device exists and can be access and translates common
2694  * scsi_pkt data to sata_pkt data.
2695  *
2696  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and
2697  * sata_pkt was set-up.
2698  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not
2699  * exist and pkt_comp callback was scheduled.
2700  * Returns other TRAN_XXXXX values when error occured and command should be
2701  * rejected with the returned TRAN_XXXXX value.
2702  *
2703  * This function should be called with port mutex held.
2704  */
2705 static int
2706 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx)
2707 {
2708 	sata_drive_info_t *sdinfo;
2709 	sata_device_t sata_device;
2710 	const struct sata_cmd_flags sata_initial_cmd_flags = {
2711 		SATA_DIR_NODATA_XFER,
2712 		/* all other values to 0/FALSE */
2713 	};
2714 	/*
2715 	 * Pkt_reason has to be set if the pkt_comp callback is invoked,
2716 	 * and that implies TRAN_ACCEPT return value. Any other returned value
2717 	 * indicates that the scsi packet was not accepted (the reason will not
2718 	 * be checked by the scsi traget driver).
2719 	 * To make debugging easier, we set pkt_reason to know value here.
2720 	 * It may be changed later when different completion reason is
2721 	 * determined.
2722 	 */
2723 	spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
2724 
2725 	/* Validate address */
2726 	switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst,
2727 	    &spx->txlt_scsi_pkt->pkt_address, &sata_device)) {
2728 
2729 	case -1:
2730 		/* Invalid address or invalid device type */
2731 		return (TRAN_BADPKT);
2732 	case 1:
2733 		/* valid address but no device - it has disappeared ? */
2734 		spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE;
2735 		/*
2736 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2737 		 * only in callback function (for normal requests) and
2738 		 * in the dump code path.
2739 		 * So, if the callback is available, we need to do
2740 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2741 		 */
2742 		if (spx->txlt_scsi_pkt->pkt_comp != NULL) {
2743 			/* scsi callback required */
2744 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2745 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2746 			    (void *)spx->txlt_scsi_pkt,
2747 			    TQ_SLEEP) == NULL)
2748 				/* Scheduling the callback failed */
2749 				return (TRAN_BUSY);
2750 
2751 			return (TRAN_ACCEPT);
2752 		}
2753 		return (TRAN_FATAL_ERROR);
2754 	default:
2755 		/* all OK */
2756 		break;
2757 	}
2758 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2759 	    &spx->txlt_sata_pkt->satapkt_device);
2760 
2761 	/*
2762 	 * If device is in reset condition, reject the packet with
2763 	 * TRAN_BUSY, unless:
2764 	 * 1. system is panicking (dumping)
2765 	 * In such case only one thread is running and there is no way to
2766 	 * process reset.
2767 	 * 2. cfgadm operation is is progress (internal APCTL lock is set)
2768 	 * Some cfgadm operations involve drive commands, so reset condition
2769 	 * needs to be ignored for IOCTL operations.
2770 	 */
2771 	if ((sdinfo->satadrv_event_flags &
2772 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
2773 
2774 		if (!ddi_in_panic() &&
2775 		    ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst,
2776 		    sata_device.satadev_addr.cport) &
2777 		    SATA_APCTL_LOCK_PORT_BUSY) == 0)) {
2778 			spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
2779 			SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
2780 			    "sata_scsi_start: rejecting command because "
2781 			    "of device reset state\n", NULL);
2782 			return (TRAN_BUSY);
2783 		}
2784 	}
2785 
2786 	/*
2787 	 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by
2788 	 * sata_scsi_pkt_init() because pkt init had to work also with
2789 	 * non-existing devices.
2790 	 * Now we know that the packet was set-up for a real device, so its
2791 	 * type is known.
2792 	 */
2793 	spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type;
2794 
2795 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags;
2796 	if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst,
2797 	    sata_device.satadev_addr.cport)->cport_event_flags &
2798 	    SATA_APCTL_LOCK_PORT_BUSY) != 0) {
2799 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
2800 		    sata_ignore_dev_reset = B_TRUE;
2801 	}
2802 	/*
2803 	 * At this point the generic translation routine determined that the
2804 	 * scsi packet should be accepted. Packet completion reason may be
2805 	 * changed later when a different completion reason is determined.
2806 	 */
2807 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
2808 
2809 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
2810 		/* Synchronous execution */
2811 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH |
2812 		    SATA_OPMODE_POLLING;
2813 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
2814 		    sata_ignore_dev_reset = ddi_in_panic();
2815 	} else {
2816 		/* Asynchronous execution */
2817 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH |
2818 		    SATA_OPMODE_INTERRUPTS;
2819 	}
2820 	/* Convert queuing information */
2821 	if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG)
2822 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag =
2823 		    B_TRUE;
2824 	else if (spx->txlt_scsi_pkt->pkt_flags &
2825 	    (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD))
2826 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag =
2827 		    B_TRUE;
2828 
2829 	/* Always limit pkt time */
2830 	if (spx->txlt_scsi_pkt->pkt_time == 0)
2831 		spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time;
2832 	else
2833 		/* Pass on scsi_pkt time */
2834 		spx->txlt_sata_pkt->satapkt_time =
2835 		    spx->txlt_scsi_pkt->pkt_time;
2836 
2837 	return (TRAN_ACCEPT);
2838 }
2839 
2840 
2841 /*
2842  * Translate ATA Identify Device data to SCSI Inquiry data.
2843  * This function may be called only for ATA devices.
2844  * This function should not be called for ATAPI devices - they
2845  * respond directly to SCSI Inquiry command.
2846  *
2847  * SATA Identify Device data has to be valid in sata_rive_info.
2848  * Buffer has to accomodate the inquiry length (36 bytes).
2849  *
2850  * This function should be called with a port mutex held.
2851  */
2852 static	void
2853 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst,
2854     sata_drive_info_t *sdinfo, uint8_t *buf)
2855 {
2856 
2857 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
2858 	struct sata_id *sid = &sdinfo->satadrv_id;
2859 
2860 	/* Start with a nice clean slate */
2861 	bzero((void *)inq, sizeof (struct scsi_inquiry));
2862 
2863 	/*
2864 	 * Rely on the dev_type for setting paripheral qualifier.
2865 	 * Assume that  DTYPE_RODIRECT applies to CD/DVD R/W devices.
2866 	 * It could be that DTYPE_OPTICAL could also qualify in the future.
2867 	 * ATAPI Inquiry may provide more data to the target driver.
2868 	 */
2869 	inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
2870 	    DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */
2871 
2872 	inq->inq_rmb = sid->ai_config & SATA_REM_MEDIA ? 1 : 0;
2873 	inq->inq_qual = 0;	/* Device type qualifier (obsolete in SCSI3? */
2874 	inq->inq_iso = 0;	/* ISO version */
2875 	inq->inq_ecma = 0;	/* ECMA version */
2876 	inq->inq_ansi = 3;	/* ANSI version - SCSI 3 */
2877 	inq->inq_aenc = 0;	/* Async event notification cap. */
2878 	inq->inq_trmiop = 0;	/* Supports TERMINATE I/O PROC msg - NO */
2879 	inq->inq_normaca = 0;	/* setting NACA bit supported - NO */
2880 	inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */
2881 	inq->inq_len = 31;	/* Additional length */
2882 	inq->inq_dualp = 0;	/* dual port device - NO */
2883 	inq->inq_reladdr = 0;	/* Supports relative addressing - NO */
2884 	inq->inq_sync = 0;	/* Supports synchronous data xfers - NO */
2885 	inq->inq_linked = 0;	/* Supports linked commands - NO */
2886 				/*
2887 				 * Queuing support - controller has to
2888 				 * support some sort of command queuing.
2889 				 */
2890 	if (SATA_QDEPTH(sata_hba_inst) > 1)
2891 		inq->inq_cmdque = 1; /* Supports command queueing - YES */
2892 	else
2893 		inq->inq_cmdque = 0; /* Supports command queueing - NO */
2894 	inq->inq_sftre = 0;	/* Supports Soft Reset option - NO ??? */
2895 	inq->inq_wbus32 = 0;	/* Supports 32 bit wide data xfers - NO */
2896 	inq->inq_wbus16 = 0;	/* Supports 16 bit wide data xfers - NO */
2897 
2898 #ifdef	_LITTLE_ENDIAN
2899 	/* Swap text fields to match SCSI format */
2900 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
2901 	swab(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
2902 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
2903 		swab(sid->ai_fw, inq->inq_revision, 4);	/* Revision level */
2904 	else
2905 		swab(&sid->ai_fw[4], inq->inq_revision, 4);	/* Rev. level */
2906 #else	/* _LITTLE_ENDIAN */
2907 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
2908 	bcopy(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
2909 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
2910 		bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
2911 	else
2912 		bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
2913 #endif	/* _LITTLE_ENDIAN */
2914 }
2915 
2916 
2917 /*
2918  * Scsi response set up for invalid command (command not supported)
2919  *
2920  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
2921  */
2922 static int
2923 sata_txlt_invalid_command(sata_pkt_txlate_t *spx)
2924 {
2925 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
2926 	struct scsi_extended_sense *sense;
2927 
2928 	scsipkt->pkt_reason = CMD_CMPLT;
2929 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
2930 	    STATE_SENT_CMD | STATE_GOT_STATUS;
2931 
2932 	*scsipkt->pkt_scbp = STATUS_CHECK;
2933 
2934 	sense = sata_arq_sense(spx);
2935 	sense->es_key = KEY_ILLEGAL_REQUEST;
2936 	sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE;
2937 
2938 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
2939 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
2940 
2941 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
2942 	    scsipkt->pkt_comp != NULL)
2943 		/* scsi callback required */
2944 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2945 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2946 		    (void *)spx->txlt_scsi_pkt,
2947 		    TQ_SLEEP) == NULL)
2948 			/* Scheduling the callback failed */
2949 			return (TRAN_BUSY);
2950 	return (TRAN_ACCEPT);
2951 }
2952 
2953 /*
2954  * Scsi response setup for
2955  * emulated non-data command that requires no action/return data
2956  *
2957  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
2958  */
2959 static 	int
2960 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx)
2961 {
2962 	int rval;
2963 
2964 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
2965 
2966 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
2967 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
2968 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
2969 		return (rval);
2970 	}
2971 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
2972 
2973 	spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
2974 	    STATE_SENT_CMD | STATE_GOT_STATUS;
2975 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
2976 	*(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD;
2977 
2978 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
2979 	    "Scsi_pkt completion reason %x\n",
2980 	    spx->txlt_scsi_pkt->pkt_reason);
2981 
2982 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 &&
2983 	    spx->txlt_scsi_pkt->pkt_comp != NULL)
2984 		/* scsi callback required */
2985 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2986 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2987 		    (void *)spx->txlt_scsi_pkt,
2988 		    TQ_SLEEP) == NULL)
2989 			/* Scheduling the callback failed */
2990 			return (TRAN_BUSY);
2991 	return (TRAN_ACCEPT);
2992 }
2993 
2994 
2995 /*
2996  * SATA translate command: Inquiry / Identify Device
2997  * Use cached Identify Device data for now, rather than issuing actual
2998  * Device Identify cmd request. If device is detached and re-attached,
2999  * asynchromous event processing should fetch and refresh Identify Device
3000  * data.
3001  * Two VPD pages are supported now:
3002  * Vital Product Data page
3003  * Unit Serial Number page
3004  *
3005  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3006  */
3007 
3008 #define	EVPD			1	/* Extended Vital Product Data flag */
3009 #define	CMDDT			2	/* Command Support Data - Obsolete */
3010 #define	INQUIRY_SUP_VPD_PAGE	0	/* Supported VDP Pages Page COde */
3011 #define	INQUIRY_USN_PAGE	0x80	/* Unit Serial Number Page Code */
3012 #define	INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */
3013 
3014 static int
3015 sata_txlt_inquiry(sata_pkt_txlate_t *spx)
3016 {
3017 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3018 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3019 	sata_drive_info_t *sdinfo;
3020 	struct scsi_extended_sense *sense;
3021 	int count;
3022 	uint8_t *p;
3023 	int i, j;
3024 	uint8_t page_buf[0xff]; /* Max length */
3025 	int rval;
3026 
3027 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3028 
3029 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
3030 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
3031 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3032 		return (rval);
3033 	}
3034 
3035 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3036 	    &spx->txlt_sata_pkt->satapkt_device);
3037 
3038 	ASSERT(sdinfo != NULL);
3039 
3040 	scsipkt->pkt_reason = CMD_CMPLT;
3041 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3042 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3043 
3044 	/* Reject not supported request */
3045 	if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */
3046 		*scsipkt->pkt_scbp = STATUS_CHECK;
3047 		sense = sata_arq_sense(spx);
3048 		sense->es_key = KEY_ILLEGAL_REQUEST;
3049 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3050 		goto done;
3051 	}
3052 
3053 	/* Valid Inquiry request */
3054 	*scsipkt->pkt_scbp = STATUS_GOOD;
3055 
3056 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3057 
3058 		/*
3059 		 * Because it is fully emulated command storing data
3060 		 * programatically in the specified buffer, release
3061 		 * preallocated DMA resources before storing data in the buffer,
3062 		 * so no unwanted DMA sync would take place.
3063 		 */
3064 		sata_scsi_dmafree(NULL, scsipkt);
3065 
3066 		if (!(scsipkt->pkt_cdbp[1] & EVPD)) {
3067 			/* Standard Inquiry Data request */
3068 			struct scsi_inquiry inq;
3069 			unsigned int bufsize;
3070 
3071 			sata_identdev_to_inquiry(spx->txlt_sata_hba_inst,
3072 			    sdinfo, (uint8_t *)&inq);
3073 			/* Copy no more than requested */
3074 			count = MIN(bp->b_bcount,
3075 			    sizeof (struct scsi_inquiry));
3076 			bufsize = scsipkt->pkt_cdbp[4];
3077 			bufsize |= scsipkt->pkt_cdbp[3] << 8;
3078 			count = MIN(count, bufsize);
3079 			bcopy(&inq, bp->b_un.b_addr, count);
3080 
3081 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
3082 			scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3083 			    bufsize - count : 0;
3084 		} else {
3085 			/*
3086 			 * peripheral_qualifier = 0;
3087 			 *
3088 			 * We are dealing only with HD and will be
3089 			 * dealing with CD/DVD devices soon
3090 			 */
3091 			uint8_t peripheral_device_type =
3092 			    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3093 			    DTYPE_DIRECT : DTYPE_RODIRECT;
3094 
3095 			switch ((uint_t)scsipkt->pkt_cdbp[2]) {
3096 			case INQUIRY_SUP_VPD_PAGE:
3097 				/*
3098 				 * Request for suported Vital Product Data
3099 				 * pages - assuming only 2 page codes
3100 				 * supported
3101 				 */
3102 				page_buf[0] = peripheral_device_type;
3103 				page_buf[1] = INQUIRY_SUP_VPD_PAGE;
3104 				page_buf[2] = 0;
3105 				page_buf[3] = 2; /* page length */
3106 				page_buf[4] = INQUIRY_SUP_VPD_PAGE;
3107 				page_buf[5] = INQUIRY_USN_PAGE;
3108 				/* Copy no more than requested */
3109 				count = MIN(bp->b_bcount, 6);
3110 				bcopy(page_buf, bp->b_un.b_addr, count);
3111 				break;
3112 			case INQUIRY_USN_PAGE:
3113 				/*
3114 				 * Request for Unit Serial Number page
3115 				 */
3116 				page_buf[0] = peripheral_device_type;
3117 				page_buf[1] = INQUIRY_USN_PAGE;
3118 				page_buf[2] = 0;
3119 				page_buf[3] = 20; /* remaining page length */
3120 				p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser);
3121 #ifdef	_LITTLE_ENDIAN
3122 				swab(p, &page_buf[4], 20);
3123 #else
3124 				bcopy(p, &page_buf[4], 20);
3125 #endif
3126 				for (i = 0; i < 20; i++) {
3127 					if (page_buf[4 + i] == '\0' ||
3128 					    page_buf[4 + i] == '\040') {
3129 						break;
3130 					}
3131 				}
3132 				/*
3133 				 * 'i' contains string length.
3134 				 *
3135 				 * Least significant character of the serial
3136 				 * number shall appear as the last byte,
3137 				 * according to SBC-3 spec.
3138 				 */
3139 				p = &page_buf[20 + 4 - 1];
3140 				for (j = i; j > 0; j--, p--) {
3141 					*p = *(p - 20 + i);
3142 				}
3143 				p = &page_buf[4];
3144 				for (j = 20 - i; j > 0; j--) {
3145 					*p++ = '\040';
3146 				}
3147 				count = MIN(bp->b_bcount, 24);
3148 				bcopy(page_buf, bp->b_un.b_addr, count);
3149 				break;
3150 
3151 			case INQUIRY_DEV_IDENTIFICATION_PAGE:
3152 				/*
3153 				 * We may want to implement this page, when
3154 				 * identifiers are common for SATA devices
3155 				 * But not now.
3156 				 */
3157 				/*FALLTHROUGH*/
3158 
3159 			default:
3160 				/* Request for unsupported VPD page */
3161 				*scsipkt->pkt_scbp = STATUS_CHECK;
3162 				sense = sata_arq_sense(spx);
3163 				sense->es_key = KEY_ILLEGAL_REQUEST;
3164 				sense->es_add_code =
3165 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3166 				goto done;
3167 			}
3168 		}
3169 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3170 		scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3171 		    scsipkt->pkt_cdbp[4] - count : 0;
3172 	}
3173 done:
3174 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3175 
3176 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3177 	    "Scsi_pkt completion reason %x\n",
3178 	    scsipkt->pkt_reason);
3179 
3180 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3181 	    scsipkt->pkt_comp != NULL) {
3182 		/* scsi callback required */
3183 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3184 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3185 		    TQ_SLEEP) == NULL)
3186 			/* Scheduling the callback failed */
3187 			return (TRAN_BUSY);
3188 	}
3189 	return (TRAN_ACCEPT);
3190 }
3191 
3192 /*
3193  * SATA translate command: Request Sense.
3194  * Emulated command (ATA version for SATA hard disks)
3195  * Always NO SENSE, because any sense data should be reported by ARQ sense.
3196  *
3197  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3198  */
3199 static int
3200 sata_txlt_request_sense(sata_pkt_txlate_t *spx)
3201 {
3202 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3203 	struct scsi_extended_sense sense;
3204 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3205 	int rval;
3206 
3207 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3208 
3209 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
3210 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
3211 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3212 		return (rval);
3213 	}
3214 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3215 
3216 
3217 	scsipkt->pkt_reason = CMD_CMPLT;
3218 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3219 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3220 	*scsipkt->pkt_scbp = STATUS_GOOD;
3221 
3222 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3223 		/*
3224 		 * Because it is fully emulated command storing data
3225 		 * programatically in the specified buffer, release
3226 		 * preallocated DMA resources before storing data in the buffer,
3227 		 * so no unwanted DMA sync would take place.
3228 		 */
3229 		int count = MIN(bp->b_bcount,
3230 		    sizeof (struct scsi_extended_sense));
3231 		sata_scsi_dmafree(NULL, scsipkt);
3232 		bzero(&sense, sizeof (struct scsi_extended_sense));
3233 		sense.es_valid = 0;	/* Valid LBA */
3234 		sense.es_class = 7;	/* Response code 0x70 - current err */
3235 		sense.es_key = KEY_NO_SENSE;
3236 		sense.es_add_len = 6;	/* Additional length */
3237 		/* Copy no more than requested */
3238 		bcopy(&sense, bp->b_un.b_addr, count);
3239 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3240 		scsipkt->pkt_resid = 0;
3241 	}
3242 
3243 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3244 	    "Scsi_pkt completion reason %x\n",
3245 	    scsipkt->pkt_reason);
3246 
3247 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3248 	    scsipkt->pkt_comp != NULL)
3249 		/* scsi callback required */
3250 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3251 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3252 		    TQ_SLEEP) == NULL)
3253 			/* Scheduling the callback failed */
3254 			return (TRAN_BUSY);
3255 	return (TRAN_ACCEPT);
3256 }
3257 
3258 /*
3259  * SATA translate command: Test Unit Ready
3260  * At the moment this is an emulated command (ATA version for SATA hard disks).
3261  * May be translated into Check Power Mode command in the future
3262  *
3263  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3264  */
3265 static int
3266 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx)
3267 {
3268 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3269 	struct scsi_extended_sense *sense;
3270 	int power_state;
3271 	int rval;
3272 
3273 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3274 
3275 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
3276 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
3277 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3278 		return (rval);
3279 	}
3280 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3281 
3282 	/* At this moment, emulate it rather than execute anything */
3283 	power_state = SATA_PWRMODE_ACTIVE;
3284 
3285 	scsipkt->pkt_reason = CMD_CMPLT;
3286 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3287 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3288 
3289 	switch (power_state) {
3290 	case SATA_PWRMODE_ACTIVE:
3291 	case SATA_PWRMODE_IDLE:
3292 		*scsipkt->pkt_scbp = STATUS_GOOD;
3293 		break;
3294 	default:
3295 		/* PWR mode standby */
3296 		*scsipkt->pkt_scbp = STATUS_CHECK;
3297 		sense = sata_arq_sense(spx);
3298 		sense->es_key = KEY_NOT_READY;
3299 		sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY;
3300 		break;
3301 	}
3302 
3303 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3304 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3305 
3306 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3307 	    scsipkt->pkt_comp != NULL)
3308 		/* scsi callback required */
3309 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3310 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3311 		    TQ_SLEEP) == NULL)
3312 			/* Scheduling the callback failed */
3313 			return (TRAN_BUSY);
3314 
3315 	return (TRAN_ACCEPT);
3316 }
3317 
3318 
3319 /*
3320  * SATA translate command: Start Stop Unit
3321  * Translation depends on a command:
3322  *	Start Unit translated into Idle Immediate
3323  *	Stop Unit translated into Standby Immediate
3324  *	Unload Media / NOT SUPPORTED YET
3325  *	Load Media / NOT SUPPROTED YET
3326  * Power condition bits are ignored, so is Immediate bit
3327  * Requesting synchronous execution.
3328  *
3329  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
3330  * appropriate values in scsi_pkt fields.
3331  */
3332 static int
3333 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx)
3334 {
3335 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3336 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3337 	struct scsi_extended_sense *sense;
3338 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3339 	int cport = SATA_TXLT_CPORT(spx);
3340 	int rval;
3341 	int synch;
3342 
3343 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3344 	    "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1);
3345 
3346 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3347 
3348 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
3349 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
3350 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3351 		return (rval);
3352 	}
3353 
3354 	if (scsipkt->pkt_cdbp[4] & 2) {
3355 		/* Load/Unload Media - invalid request */
3356 		*scsipkt->pkt_scbp = STATUS_CHECK;
3357 		sense = sata_arq_sense(spx);
3358 		sense->es_key = KEY_ILLEGAL_REQUEST;
3359 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3360 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3361 
3362 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3363 		    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3364 
3365 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3366 		    scsipkt->pkt_comp != NULL)
3367 			/* scsi callback required */
3368 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3369 			    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3370 			    TQ_SLEEP) == NULL)
3371 				/* Scheduling the callback failed */
3372 				return (TRAN_BUSY);
3373 
3374 		return (TRAN_ACCEPT);
3375 	}
3376 	scmd->satacmd_addr_type = 0;
3377 	scmd->satacmd_sec_count_lsb = 0;
3378 	scmd->satacmd_lba_low_lsb = 0;
3379 	scmd->satacmd_lba_mid_lsb = 0;
3380 	scmd->satacmd_lba_high_lsb = 0;
3381 	scmd->satacmd_features_reg = 0;
3382 	scmd->satacmd_device_reg = 0;
3383 	scmd->satacmd_status_reg = 0;
3384 	if (scsipkt->pkt_cdbp[4] & 1) {
3385 		/* Start Unit */
3386 		scmd->satacmd_cmd_reg = SATAC_IDLE_IM;
3387 	} else {
3388 		/* Stop Unit */
3389 		scmd->satacmd_cmd_reg = SATAC_STANDBY_IM;
3390 	}
3391 
3392 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
3393 		/* Need to set-up a callback function */
3394 		spx->txlt_sata_pkt->satapkt_comp =
3395 		    sata_txlt_nodata_cmd_completion;
3396 		synch = FALSE;
3397 	} else {
3398 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
3399 		synch = TRUE;
3400 	}
3401 
3402 	/* Transfer command to HBA */
3403 	if (sata_hba_start(spx, &rval) != 0) {
3404 		/* Pkt not accepted for execution */
3405 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3406 		return (rval);
3407 	}
3408 
3409 	/*
3410 	 * If execution is non-synchronous,
3411 	 * a callback function will handle potential errors, translate
3412 	 * the response and will do a callback to a target driver.
3413 	 * If it was synchronous, check execution status using the same
3414 	 * framework callback.
3415 	 */
3416 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3417 	if (synch) {
3418 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3419 		    "synchronous execution status %x\n",
3420 		    spx->txlt_sata_pkt->satapkt_reason);
3421 
3422 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
3423 	}
3424 	return (TRAN_ACCEPT);
3425 
3426 }
3427 
3428 
3429 /*
3430  * SATA translate command:  Read Capacity.
3431  * Emulated command for SATA disks.
3432  * Capacity is retrieved from cached Idenifty Device data.
3433  * Identify Device data shows effective disk capacity, not the native
3434  * capacity, which may be limitted by Set Max Address command.
3435  * This is ATA version for SATA hard disks.
3436  *
3437  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3438  */
3439 static int
3440 sata_txlt_read_capacity(sata_pkt_txlate_t *spx)
3441 {
3442 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3443 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3444 	sata_drive_info_t *sdinfo;
3445 	uint64_t val;
3446 	uchar_t *rbuf;
3447 	int rval;
3448 
3449 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3450 	    "sata_txlt_read_capacity: ", NULL);
3451 
3452 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3453 
3454 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
3455 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
3456 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3457 		return (rval);
3458 	}
3459 
3460 	scsipkt->pkt_reason = CMD_CMPLT;
3461 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3462 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3463 	*scsipkt->pkt_scbp = STATUS_GOOD;
3464 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3465 		/*
3466 		 * Because it is fully emulated command storing data
3467 		 * programatically in the specified buffer, release
3468 		 * preallocated DMA resources before storing data in the buffer,
3469 		 * so no unwanted DMA sync would take place.
3470 		 */
3471 		sata_scsi_dmafree(NULL, scsipkt);
3472 
3473 		sdinfo = sata_get_device_info(
3474 		    spx->txlt_sata_hba_inst,
3475 		    &spx->txlt_sata_pkt->satapkt_device);
3476 		/* Last logical block address */
3477 		val = sdinfo->satadrv_capacity - 1;
3478 		rbuf = (uchar_t *)bp->b_un.b_addr;
3479 		/* Need to swap endians to match scsi format */
3480 		rbuf[0] = (val >> 24) & 0xff;
3481 		rbuf[1] = (val >> 16) & 0xff;
3482 		rbuf[2] = (val >> 8) & 0xff;
3483 		rbuf[3] = val & 0xff;
3484 		/* block size - always 512 bytes, for now */
3485 		rbuf[4] = 0;
3486 		rbuf[5] = 0;
3487 		rbuf[6] = 0x02;
3488 		rbuf[7] = 0;
3489 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3490 		scsipkt->pkt_resid = 0;
3491 
3492 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n",
3493 		    sdinfo->satadrv_capacity -1);
3494 	}
3495 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3496 	/*
3497 	 * If a callback was requested, do it now.
3498 	 */
3499 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3500 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3501 
3502 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3503 	    scsipkt->pkt_comp != NULL)
3504 		/* scsi callback required */
3505 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3506 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3507 		    TQ_SLEEP) == NULL)
3508 			/* Scheduling the callback failed */
3509 			return (TRAN_BUSY);
3510 
3511 	return (TRAN_ACCEPT);
3512 }
3513 
3514 /*
3515  * SATA translate command: Mode Sense.
3516  * Translated into appropriate SATA command or emulated.
3517  * Saved Values Page Control (03) are not supported.
3518  *
3519  * NOTE: only caching mode sense page is currently implemented.
3520  *
3521  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3522  */
3523 
3524 static int
3525 sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
3526 {
3527 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
3528 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3529 	sata_drive_info_t *sdinfo;
3530 	sata_id_t *sata_id;
3531 	struct scsi_extended_sense *sense;
3532 	int 		len, bdlen, count, alc_len;
3533 	int		pc;	/* Page Control code */
3534 	uint8_t		*buf;	/* mode sense buffer */
3535 	int		rval;
3536 
3537 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3538 	    "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
3539 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
3540 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
3541 
3542 	buf = kmem_zalloc(1024, KM_SLEEP);
3543 
3544 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3545 
3546 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
3547 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
3548 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3549 		kmem_free(buf, 1024);
3550 		return (rval);
3551 	}
3552 
3553 	scsipkt->pkt_reason = CMD_CMPLT;
3554 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3555 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3556 
3557 	pc = scsipkt->pkt_cdbp[2] >> 6;
3558 
3559 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3560 		/*
3561 		 * Because it is fully emulated command storing data
3562 		 * programatically in the specified buffer, release
3563 		 * preallocated DMA resources before storing data in the buffer,
3564 		 * so no unwanted DMA sync would take place.
3565 		 */
3566 		sata_scsi_dmafree(NULL, scsipkt);
3567 
3568 		len = 0;
3569 		bdlen = 0;
3570 		if (!(scsipkt->pkt_cdbp[1] & 8)) {
3571 			if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
3572 			    (scsipkt->pkt_cdbp[0] & 0x10))
3573 				bdlen = 16;
3574 			else
3575 				bdlen = 8;
3576 		}
3577 		/* Build mode parameter header */
3578 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3579 			/* 4-byte mode parameter header */
3580 			buf[len++] = 0;   	/* mode data length */
3581 			buf[len++] = 0;		/* medium type */
3582 			buf[len++] = 0;		/* dev-specific param */
3583 			buf[len++] = bdlen;	/* Block Descriptor length */
3584 		} else {
3585 			/* 8-byte mode parameter header */
3586 			buf[len++] = 0;		/* mode data length */
3587 			buf[len++] = 0;
3588 			buf[len++] = 0;		/* medium type */
3589 			buf[len++] = 0;		/* dev-specific param */
3590 			if (bdlen == 16)
3591 				buf[len++] = 1;	/* long lba descriptor */
3592 			else
3593 				buf[len++] = 0;
3594 			buf[len++] = 0;
3595 			buf[len++] = 0;		/* Block Descriptor length */
3596 			buf[len++] = bdlen;
3597 		}
3598 
3599 		sdinfo = sata_get_device_info(
3600 		    spx->txlt_sata_hba_inst,
3601 		    &spx->txlt_sata_pkt->satapkt_device);
3602 
3603 		/* Build block descriptor only if not disabled (DBD) */
3604 		if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
3605 			/* Block descriptor - direct-access device format */
3606 			if (bdlen == 8) {
3607 				/* build regular block descriptor */
3608 				buf[len++] =
3609 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
3610 				buf[len++] =
3611 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
3612 				buf[len++] =
3613 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
3614 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
3615 				buf[len++] = 0; /* density code */
3616 				buf[len++] = 0;
3617 				if (sdinfo->satadrv_type ==
3618 				    SATA_DTYPE_ATADISK)
3619 					buf[len++] = 2;
3620 				else
3621 					/* ATAPI */
3622 					buf[len++] = 8;
3623 				buf[len++] = 0;
3624 			} else if (bdlen == 16) {
3625 				/* Long LBA Accepted */
3626 				/* build long lba block descriptor */
3627 #ifndef __lock_lint
3628 				buf[len++] =
3629 				    (sdinfo->satadrv_capacity >> 56) & 0xff;
3630 				buf[len++] =
3631 				    (sdinfo->satadrv_capacity >> 48) & 0xff;
3632 				buf[len++] =
3633 				    (sdinfo->satadrv_capacity >> 40) & 0xff;
3634 				buf[len++] =
3635 				    (sdinfo->satadrv_capacity >> 32) & 0xff;
3636 #endif
3637 				buf[len++] =
3638 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
3639 				buf[len++] =
3640 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
3641 				buf[len++] =
3642 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
3643 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
3644 				buf[len++] = 0;
3645 				buf[len++] = 0; /* density code */
3646 				buf[len++] = 0;
3647 				buf[len++] = 0;
3648 				if (sdinfo->satadrv_type ==
3649 				    SATA_DTYPE_ATADISK)
3650 					buf[len++] = 2;
3651 				else
3652 					/* ATAPI */
3653 					buf[len++] = 8;
3654 				buf[len++] = 0;
3655 			}
3656 		}
3657 
3658 		sata_id = &sdinfo->satadrv_id;
3659 
3660 		/*
3661 		 * Add requested pages.
3662 		 * Page 3 and 4 are obsolete and we are not supporting them.
3663 		 * We deal now with:
3664 		 * caching (read/write cache control).
3665 		 * We should eventually deal with following mode pages:
3666 		 * error recovery  (0x01),
3667 		 * power condition (0x1a),
3668 		 * exception control page (enables SMART) (0x1c),
3669 		 * enclosure management (ses),
3670 		 * protocol-specific port mode (port control).
3671 		 */
3672 		switch (scsipkt->pkt_cdbp[2] & 0x3f) {
3673 		case MODEPAGE_RW_ERRRECOV:
3674 			/* DAD_MODE_ERR_RECOV */
3675 			/* R/W recovery */
3676 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
3677 			break;
3678 		case MODEPAGE_CACHING:
3679 			/* DAD_MODE_CACHE */
3680 			/* Reject not supported request for saved parameters */
3681 			if (pc == 3) {
3682 				*scsipkt->pkt_scbp = STATUS_CHECK;
3683 				sense = sata_arq_sense(spx);
3684 				sense->es_key = KEY_ILLEGAL_REQUEST;
3685 				sense->es_add_code =
3686 				    SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
3687 				goto done;
3688 			}
3689 
3690 			/* caching */
3691 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
3692 			break;
3693 		case MODEPAGE_INFO_EXCPT:
3694 			/* exception cntrl */
3695 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
3696 				len += sata_build_msense_page_1c(sdinfo, pc,
3697 				    buf+len);
3698 			}
3699 			else
3700 				goto err;
3701 			break;
3702 		case MODEPAGE_POWER_COND:
3703 			/* DAD_MODE_POWER_COND */
3704 			/* power condition */
3705 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
3706 			break;
3707 
3708 		case MODEPAGE_ACOUSTIC_MANAG:
3709 			/* acoustic management */
3710 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
3711 			break;
3712 		case MODEPAGE_ALLPAGES:
3713 			/* all pages */
3714 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
3715 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
3716 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
3717 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
3718 				len += sata_build_msense_page_1c(sdinfo, pc,
3719 				    buf+len);
3720 			}
3721 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
3722 			break;
3723 		default:
3724 		err:
3725 			/* Invalid request */
3726 			*scsipkt->pkt_scbp = STATUS_CHECK;
3727 			sense = sata_arq_sense(spx);
3728 			sense->es_key = KEY_ILLEGAL_REQUEST;
3729 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3730 			goto done;
3731 		}
3732 
3733 		/* fix total mode data length */
3734 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3735 			/* 4-byte mode parameter header */
3736 			buf[0] = len - 1;   	/* mode data length */
3737 		} else {
3738 			buf[0] = (len -2) >> 8;
3739 			buf[1] = (len -2) & 0xff;
3740 		}
3741 
3742 
3743 		/* Check allocation length */
3744 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3745 			alc_len = scsipkt->pkt_cdbp[4];
3746 		} else {
3747 			alc_len = scsipkt->pkt_cdbp[7];
3748 			alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
3749 		}
3750 		/*
3751 		 * We do not check for possible parameters truncation
3752 		 * (alc_len < len) assuming that the target driver works
3753 		 * correctly. Just avoiding overrun.
3754 		 * Copy no more than requested and possible, buffer-wise.
3755 		 */
3756 		count = MIN(alc_len, len);
3757 		count = MIN(bp->b_bcount, count);
3758 		bcopy(buf, bp->b_un.b_addr, count);
3759 
3760 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3761 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
3762 	}
3763 	*scsipkt->pkt_scbp = STATUS_GOOD;
3764 done:
3765 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3766 	(void) kmem_free(buf, 1024);
3767 
3768 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3769 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3770 
3771 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3772 	    scsipkt->pkt_comp != NULL)
3773 		/* scsi callback required */
3774 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3775 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3776 		    TQ_SLEEP) == NULL)
3777 			/* Scheduling the callback failed */
3778 			return (TRAN_BUSY);
3779 
3780 	return (TRAN_ACCEPT);
3781 }
3782 
3783 
3784 /*
3785  * SATA translate command: Mode Select.
3786  * Translated into appropriate SATA command or emulated.
3787  * Saving parameters is not supported.
3788  * Changing device capacity is not supported (although theoretically
3789  * possible by executing SET FEATURES/SET MAX ADDRESS)
3790  *
3791  * Assumption is that the target driver is working correctly.
3792  *
3793  * More than one SATA command may be executed to perform operations specified
3794  * by mode select pages. The first error terminates further execution.
3795  * Operations performed successully are not backed-up in such case.
3796  *
3797  * NOTE: only caching mode select page is implemented.
3798  * Caching setup is remembered so it could be re-stored in case of
3799  * an unexpected device reset.
3800  *
3801  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3802  */
3803 
3804 static int
3805 sata_txlt_mode_select(sata_pkt_txlate_t *spx)
3806 {
3807 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3808 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3809 	struct scsi_extended_sense *sense;
3810 	int len, pagelen, count, pllen;
3811 	uint8_t *buf;	/* mode select buffer */
3812 	int rval, stat;
3813 	uint_t nointr_flag;
3814 	int dmod = 0;
3815 
3816 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3817 	    "sata_txlt_mode_select, pc %x page code 0x%02x\n",
3818 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
3819 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
3820 
3821 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3822 
3823 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
3824 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
3825 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3826 		return (rval);
3827 	}
3828 
3829 	rval = TRAN_ACCEPT;
3830 
3831 	scsipkt->pkt_reason = CMD_CMPLT;
3832 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3833 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3834 
3835 	/* Reject not supported request */
3836 	if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
3837 		*scsipkt->pkt_scbp = STATUS_CHECK;
3838 		sense = sata_arq_sense(spx);
3839 		sense->es_key = KEY_ILLEGAL_REQUEST;
3840 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3841 		goto done;
3842 	}
3843 
3844 	if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
3845 		pllen = scsipkt->pkt_cdbp[4];
3846 	} else {
3847 		pllen = scsipkt->pkt_cdbp[7];
3848 		pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
3849 	}
3850 
3851 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
3852 
3853 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
3854 		buf = (uint8_t *)bp->b_un.b_addr;
3855 		count = MIN(bp->b_bcount, pllen);
3856 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3857 		scsipkt->pkt_resid = 0;
3858 		pllen = count;
3859 
3860 		/*
3861 		 * Check the header to skip the block descriptor(s) - we
3862 		 * do not support setting device capacity.
3863 		 * Existing macros do not recognize long LBA dscriptor,
3864 		 * hence manual calculation.
3865 		 */
3866 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
3867 			/* 6-bytes CMD, 4 bytes header */
3868 			if (count <= 4)
3869 				goto done;		/* header only */
3870 			len = buf[3] + 4;
3871 		} else {
3872 			/* 10-bytes CMD, 8 bytes header */
3873 			if (count <= 8)
3874 				goto done;		/* header only */
3875 			len = buf[6];
3876 			len = (len << 8) + buf[7] + 8;
3877 		}
3878 		if (len >= count)
3879 			goto done;	/* header + descriptor(s) only */
3880 
3881 		pllen -= len;		/* remaining data length */
3882 
3883 		/*
3884 		 * We may be executing SATA command and want to execute it
3885 		 * in SYNCH mode, regardless of scsi_pkt setting.
3886 		 * Save scsi_pkt setting and indicate SYNCH mode
3887 		 */
3888 		nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
3889 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3890 		    scsipkt->pkt_comp != NULL) {
3891 			scsipkt->pkt_flags |= FLAG_NOINTR;
3892 		}
3893 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
3894 
3895 		/*
3896 		 * len is now the offset to a first mode select page
3897 		 * Process all pages
3898 		 */
3899 		while (pllen > 0) {
3900 			switch ((int)buf[len]) {
3901 			case MODEPAGE_CACHING:
3902 				/* No support for SP (saving) */
3903 				if (scsipkt->pkt_cdbp[1] & 0x01) {
3904 					*scsipkt->pkt_scbp = STATUS_CHECK;
3905 					sense = sata_arq_sense(spx);
3906 					sense->es_key = KEY_ILLEGAL_REQUEST;
3907 					sense->es_add_code =
3908 					    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3909 					goto done;
3910 				}
3911 				stat = sata_mode_select_page_8(spx,
3912 				    (struct mode_cache_scsi3 *)&buf[len],
3913 				    pllen, &pagelen, &rval, &dmod);
3914 				/*
3915 				 * The pagelen value indicates the number of
3916 				 * parameter bytes already processed.
3917 				 * The rval is the return value from
3918 				 * sata_tran_start().
3919 				 * The stat indicates the overall status of
3920 				 * the operation(s).
3921 				 */
3922 				if (stat != SATA_SUCCESS)
3923 					/*
3924 					 * Page processing did not succeed -
3925 					 * all error info is already set-up,
3926 					 * just return
3927 					 */
3928 					pllen = 0; /* this breaks the loop */
3929 				else {
3930 					len += pagelen;
3931 					pllen -= pagelen;
3932 				}
3933 				break;
3934 
3935 			case MODEPAGE_INFO_EXCPT:
3936 				stat = sata_mode_select_page_1c(spx,
3937 				    (struct mode_info_excpt_page *)&buf[len],
3938 				    pllen, &pagelen, &rval, &dmod);
3939 				/*
3940 				 * The pagelen value indicates the number of
3941 				 * parameter bytes already processed.
3942 				 * The rval is the return value from
3943 				 * sata_tran_start().
3944 				 * The stat indicates the overall status of
3945 				 * the operation(s).
3946 				 */
3947 				if (stat != SATA_SUCCESS)
3948 					/*
3949 					 * Page processing did not succeed -
3950 					 * all error info is already set-up,
3951 					 * just return
3952 					 */
3953 					pllen = 0; /* this breaks the loop */
3954 				else {
3955 					len += pagelen;
3956 					pllen -= pagelen;
3957 				}
3958 				break;
3959 
3960 			case MODEPAGE_ACOUSTIC_MANAG:
3961 				stat = sata_mode_select_page_30(spx,
3962 				    (struct mode_acoustic_management *)
3963 				    &buf[len], pllen, &pagelen, &rval, &dmod);
3964 				/*
3965 				 * The pagelen value indicates the number of
3966 				 * parameter bytes already processed.
3967 				 * The rval is the return value from
3968 				 * sata_tran_start().
3969 				 * The stat indicates the overall status of
3970 				 * the operation(s).
3971 				 */
3972 				if (stat != SATA_SUCCESS)
3973 					/*
3974 					 * Page processing did not succeed -
3975 					 * all error info is already set-up,
3976 					 * just return
3977 					 */
3978 					pllen = 0; /* this breaks the loop */
3979 				else {
3980 					len += pagelen;
3981 					pllen -= pagelen;
3982 				}
3983 
3984 				break;
3985 			default:
3986 				*scsipkt->pkt_scbp = STATUS_CHECK;
3987 				sense = sata_arq_sense(spx);
3988 				sense->es_key = KEY_ILLEGAL_REQUEST;
3989 				sense->es_add_code =
3990 				    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
3991 				goto done;
3992 			}
3993 		}
3994 	}
3995 done:
3996 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3997 	/*
3998 	 * If device parameters were modified, fetch and store the new
3999 	 * Identify Device data. Since port mutex could have been released
4000 	 * for accessing HBA driver, we need to re-check device existence.
4001 	 */
4002 	if (dmod != 0) {
4003 		sata_drive_info_t new_sdinfo, *sdinfo;
4004 		int rv = 0;
4005 
4006 		/*
4007 		 * Following statement has to be changed if this function is
4008 		 * used for devices other than SATA hard disks.
4009 		 */
4010 		new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
4011 
4012 		new_sdinfo.satadrv_addr =
4013 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr;
4014 		rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
4015 		    &new_sdinfo);
4016 
4017 		mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4018 		/*
4019 		 * Since port mutex could have been released when
4020 		 * accessing HBA driver, we need to re-check that the
4021 		 * framework still holds the device info structure.
4022 		 */
4023 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4024 		    &spx->txlt_sata_pkt->satapkt_device);
4025 		if (sdinfo != NULL) {
4026 			/*
4027 			 * Device still has info structure in the
4028 			 * sata framework. Copy newly fetched info
4029 			 */
4030 			if (rv == 0) {
4031 				sdinfo->satadrv_id = new_sdinfo.satadrv_id;
4032 				sata_save_drive_settings(sdinfo);
4033 			} else {
4034 				/*
4035 				 * Could not fetch new data - invalidate
4036 				 * sata_drive_info. That makes device
4037 				 * unusable.
4038 				 */
4039 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
4040 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
4041 			}
4042 		}
4043 		if (rv != 0 || sdinfo == NULL) {
4044 			/*
4045 			 * This changes the overall mode select completion
4046 			 * reason to a failed one !!!!!
4047 			 */
4048 			*scsipkt->pkt_scbp = STATUS_CHECK;
4049 			sense = sata_arq_sense(spx);
4050 			scsipkt->pkt_reason = CMD_INCOMPLETE;
4051 			rval = TRAN_ACCEPT;
4052 		}
4053 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4054 	}
4055 	/* Restore the scsi pkt flags */
4056 	scsipkt->pkt_flags &= ~FLAG_NOINTR;
4057 	scsipkt->pkt_flags |= nointr_flag;
4058 
4059 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4060 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4061 
4062 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4063 	    scsipkt->pkt_comp != NULL)
4064 		/* scsi callback required */
4065 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4066 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4067 		    TQ_SLEEP) == NULL)
4068 			/* Scheduling the callback failed */
4069 			return (TRAN_BUSY);
4070 
4071 	return (rval);
4072 }
4073 
4074 
4075 
4076 /*
4077  * Translate command: Log Sense
4078  */
4079 static 	int
4080 sata_txlt_log_sense(sata_pkt_txlate_t *spx)
4081 {
4082 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4083 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4084 	sata_drive_info_t *sdinfo;
4085 	struct scsi_extended_sense *sense;
4086 	int 		len, count, alc_len;
4087 	int		pc;	/* Page Control code */
4088 	int		page_code;	/* Page code */
4089 	uint8_t		*buf;	/* log sense buffer */
4090 	int		rval;
4091 #define	MAX_LOG_SENSE_PAGE_SIZE	512
4092 
4093 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4094 	    "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
4095 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4096 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4097 
4098 	buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
4099 
4100 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4101 
4102 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
4103 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
4104 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4105 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4106 		return (rval);
4107 	}
4108 
4109 	scsipkt->pkt_reason = CMD_CMPLT;
4110 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4111 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4112 
4113 	pc = scsipkt->pkt_cdbp[2] >> 6;
4114 	page_code = scsipkt->pkt_cdbp[2] & 0x3f;
4115 
4116 	/* Reject not supported request for all but cumulative values */
4117 	switch (pc) {
4118 	case PC_CUMULATIVE_VALUES:
4119 		break;
4120 	default:
4121 		*scsipkt->pkt_scbp = STATUS_CHECK;
4122 		sense = sata_arq_sense(spx);
4123 		sense->es_key = KEY_ILLEGAL_REQUEST;
4124 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4125 		goto done;
4126 	}
4127 
4128 	switch (page_code) {
4129 	case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
4130 	case PAGE_CODE_SELF_TEST_RESULTS:
4131 	case PAGE_CODE_INFORMATION_EXCEPTIONS:
4132 	case PAGE_CODE_SMART_READ_DATA:
4133 		break;
4134 	default:
4135 		*scsipkt->pkt_scbp = STATUS_CHECK;
4136 		sense = sata_arq_sense(spx);
4137 		sense->es_key = KEY_ILLEGAL_REQUEST;
4138 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4139 		goto done;
4140 	}
4141 
4142 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4143 		/*
4144 		 * Because log sense uses local buffers for data retrieval from
4145 		 * the devices and sets the data programatically in the
4146 		 * original specified buffer, release preallocated DMA
4147 		 * resources before storing data in the original buffer,
4148 		 * so no unwanted DMA sync would take place.
4149 		 */
4150 		sata_id_t *sata_id;
4151 
4152 		sata_scsi_dmafree(NULL, scsipkt);
4153 
4154 		len = 0;
4155 
4156 		/* Build log parameter header */
4157 		buf[len++] = page_code;	/* page code as in the CDB */
4158 		buf[len++] = 0;		/* reserved */
4159 		buf[len++] = 0;		/* Zero out page length for now (MSB) */
4160 		buf[len++] = 0;		/* (LSB) */
4161 
4162 		sdinfo = sata_get_device_info(
4163 		    spx->txlt_sata_hba_inst,
4164 		    &spx->txlt_sata_pkt->satapkt_device);
4165 
4166 
4167 		/*
4168 		 * Add requested pages.
4169 		 */
4170 		switch (page_code) {
4171 		case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
4172 			len = sata_build_lsense_page_0(sdinfo, buf + len);
4173 			break;
4174 		case PAGE_CODE_SELF_TEST_RESULTS:
4175 			sata_id = &sdinfo->satadrv_id;
4176 			if ((! (sata_id->ai_cmdset84 &
4177 			    SATA_SMART_SELF_TEST_SUPPORTED)) ||
4178 			    (! (sata_id->ai_features87 &
4179 			    SATA_SMART_SELF_TEST_SUPPORTED))) {
4180 				*scsipkt->pkt_scbp = STATUS_CHECK;
4181 				sense = sata_arq_sense(spx);
4182 				sense->es_key = KEY_ILLEGAL_REQUEST;
4183 				sense->es_add_code =
4184 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4185 
4186 				goto done;
4187 			}
4188 			len = sata_build_lsense_page_10(sdinfo, buf + len,
4189 			    spx->txlt_sata_hba_inst);
4190 			break;
4191 		case PAGE_CODE_INFORMATION_EXCEPTIONS:
4192 			sata_id = &sdinfo->satadrv_id;
4193 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
4194 				*scsipkt->pkt_scbp = STATUS_CHECK;
4195 				sense = sata_arq_sense(spx);
4196 				sense->es_key = KEY_ILLEGAL_REQUEST;
4197 				sense->es_add_code =
4198 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4199 
4200 				goto done;
4201 			}
4202 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
4203 				*scsipkt->pkt_scbp = STATUS_CHECK;
4204 				sense = sata_arq_sense(spx);
4205 				sense->es_key = KEY_ABORTED_COMMAND;
4206 				sense->es_add_code =
4207 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
4208 				sense->es_qual_code =
4209 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
4210 
4211 				goto done;
4212 			}
4213 
4214 			len = sata_build_lsense_page_2f(sdinfo, buf + len,
4215 			    spx->txlt_sata_hba_inst);
4216 			break;
4217 		case PAGE_CODE_SMART_READ_DATA:
4218 			sata_id = &sdinfo->satadrv_id;
4219 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
4220 				*scsipkt->pkt_scbp = STATUS_CHECK;
4221 				sense = sata_arq_sense(spx);
4222 				sense->es_key = KEY_ILLEGAL_REQUEST;
4223 				sense->es_add_code =
4224 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4225 
4226 				goto done;
4227 			}
4228 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
4229 				*scsipkt->pkt_scbp = STATUS_CHECK;
4230 				sense = sata_arq_sense(spx);
4231 				sense->es_key = KEY_ABORTED_COMMAND;
4232 				sense->es_add_code =
4233 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
4234 				sense->es_qual_code =
4235 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
4236 
4237 				goto done;
4238 			}
4239 
4240 			/* This page doesn't include a page header */
4241 			len = sata_build_lsense_page_30(sdinfo, buf,
4242 			    spx->txlt_sata_hba_inst);
4243 			goto no_header;
4244 		default:
4245 			/* Invalid request */
4246 			*scsipkt->pkt_scbp = STATUS_CHECK;
4247 			sense = sata_arq_sense(spx);
4248 			sense->es_key = KEY_ILLEGAL_REQUEST;
4249 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4250 			goto done;
4251 		}
4252 
4253 		/* set parameter log sense data length */
4254 		buf[2] = len >> 8;	/* log sense length (MSB) */
4255 		buf[3] = len & 0xff;	/* log sense length (LSB) */
4256 
4257 		len += SCSI_LOG_PAGE_HDR_LEN;
4258 		ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
4259 
4260 no_header:
4261 		/* Check allocation length */
4262 		alc_len = scsipkt->pkt_cdbp[7];
4263 		alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
4264 
4265 		/*
4266 		 * We do not check for possible parameters truncation
4267 		 * (alc_len < len) assuming that the target driver works
4268 		 * correctly. Just avoiding overrun.
4269 		 * Copy no more than requested and possible, buffer-wise.
4270 		 */
4271 		count = MIN(alc_len, len);
4272 		count = MIN(bp->b_bcount, count);
4273 		bcopy(buf, bp->b_un.b_addr, count);
4274 
4275 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4276 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
4277 	}
4278 	*scsipkt->pkt_scbp = STATUS_GOOD;
4279 done:
4280 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4281 	(void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4282 
4283 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4284 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4285 
4286 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4287 	    scsipkt->pkt_comp != NULL)
4288 		/* scsi callback required */
4289 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4290 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4291 		    TQ_SLEEP) == NULL)
4292 			/* Scheduling the callback failed */
4293 			return (TRAN_BUSY);
4294 
4295 	return (TRAN_ACCEPT);
4296 }
4297 
4298 /*
4299  * Translate command: Log Select
4300  * Not implemented at this time - returns invalid command response.
4301  */
4302 static 	int
4303 sata_txlt_log_select(sata_pkt_txlate_t *spx)
4304 {
4305 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4306 	    "sata_txlt_log_select\n", NULL);
4307 
4308 	return (sata_txlt_invalid_command(spx));
4309 }
4310 
4311 
4312 /*
4313  * Translate command: Read (various types).
4314  * Translated into appropriate type of ATA READ command
4315  * for SATA hard disks.
4316  * Both the device capabilities and requested operation mode are
4317  * considered.
4318  *
4319  * Following scsi cdb fields are ignored:
4320  * rdprotect, dpo, fua, fua_nv, group_number.
4321  *
4322  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
4323  * enable variable sata_func_enable), the capability of the controller and
4324  * capability of a device are checked and if both support queueing, read
4325  * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
4326  * command rather than plain READ_XXX command.
4327  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
4328  * both the controller and device suport such functionality, the read
4329  * request will be translated to READ_FPDMA_QUEUED command.
4330  * In both cases the maximum queue depth is derived as minimum of:
4331  * HBA capability,device capability and sata_max_queue_depth variable setting.
4332  * The value passed to HBA driver is decremented by 1, because only 5 bits are
4333  * used to pass max queue depth value, and the maximum possible queue depth
4334  * is 32.
4335  *
4336  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4337  * appropriate values in scsi_pkt fields.
4338  */
4339 static int
4340 sata_txlt_read(sata_pkt_txlate_t *spx)
4341 {
4342 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4343 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4344 	sata_drive_info_t *sdinfo;
4345 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4346 	int cport = SATA_TXLT_CPORT(spx);
4347 	uint16_t sec_count;
4348 	uint64_t lba;
4349 	int rval;
4350 	int synch;
4351 
4352 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4353 
4354 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
4355 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
4356 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4357 		return (rval);
4358 	}
4359 
4360 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4361 	    &spx->txlt_sata_pkt->satapkt_device);
4362 
4363 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
4364 	/*
4365 	 * Extract LBA and sector count from scsi CDB.
4366 	 */
4367 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
4368 	case SCMD_READ:
4369 		/* 6-byte scsi read cmd : 0x08 */
4370 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
4371 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
4372 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4373 		sec_count = scsipkt->pkt_cdbp[4];
4374 		/* sec_count 0 will be interpreted as 256 by a device */
4375 		break;
4376 	case SCMD_READ_G1:
4377 		/* 10-bytes scsi read command : 0x28 */
4378 		lba = scsipkt->pkt_cdbp[2];
4379 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4380 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4381 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4382 		sec_count = scsipkt->pkt_cdbp[7];
4383 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4384 		break;
4385 	case SCMD_READ_G5:
4386 		/* 12-bytes scsi read command : 0xA8 */
4387 		lba = scsipkt->pkt_cdbp[2];
4388 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4389 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4390 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4391 		sec_count = scsipkt->pkt_cdbp[6];
4392 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
4393 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4394 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
4395 		break;
4396 	case SCMD_READ_G4:
4397 		/* 16-bytes scsi read command : 0x88 */
4398 		lba = scsipkt->pkt_cdbp[2];
4399 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4400 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4401 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4402 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
4403 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
4404 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
4405 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
4406 		sec_count = scsipkt->pkt_cdbp[10];
4407 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
4408 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
4409 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
4410 		break;
4411 	default:
4412 		/* Unsupported command */
4413 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4414 		return (sata_txlt_invalid_command(spx));
4415 	}
4416 
4417 	/*
4418 	 * Check if specified address exceeds device capacity
4419 	 */
4420 	if ((lba >= sdinfo->satadrv_capacity) ||
4421 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
4422 		/* LBA out of range */
4423 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4424 		return (sata_txlt_lba_out_of_range(spx));
4425 	}
4426 
4427 	/*
4428 	 * For zero-length transfer, emulate good completion of the command
4429 	 * (reasons for rejecting the command were already checked).
4430 	 * No DMA resources were allocated.
4431 	 */
4432 	if (spx->txlt_dma_cookie_list == NULL) {
4433 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4434 		return (sata_emul_rw_completion(spx));
4435 	}
4436 
4437 	/*
4438 	 * Build cmd block depending on the device capability and
4439 	 * requested operation mode.
4440 	 * Do not bother with non-dma mode - we are working only with
4441 	 * devices supporting DMA.
4442 	 */
4443 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
4444 	scmd->satacmd_device_reg = SATA_ADH_LBA;
4445 	scmd->satacmd_cmd_reg = SATAC_READ_DMA;
4446 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
4447 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4448 		scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT;
4449 		scmd->satacmd_sec_count_msb = sec_count >> 8;
4450 #ifndef __lock_lint
4451 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
4452 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
4453 		scmd->satacmd_lba_high_msb = lba >> 40;
4454 #endif
4455 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
4456 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
4457 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
4458 	}
4459 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
4460 	scmd->satacmd_lba_low_lsb = lba & 0xff;
4461 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
4462 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
4463 	scmd->satacmd_features_reg = 0;
4464 	scmd->satacmd_status_reg = 0;
4465 	scmd->satacmd_error_reg = 0;
4466 
4467 	/*
4468 	 * Check if queueing commands should be used and switch
4469 	 * to appropriate command if possible
4470 	 */
4471 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
4472 		boolean_t using_queuing;
4473 
4474 		/* Queuing supported by controller and device? */
4475 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
4476 		    (sdinfo->satadrv_features_support &
4477 		    SATA_DEV_F_NCQ) &&
4478 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4479 		    SATA_CTLF_NCQ)) {
4480 			using_queuing = B_TRUE;
4481 
4482 			/* NCQ supported - use FPDMA READ */
4483 			scmd->satacmd_cmd_reg =
4484 			    SATAC_READ_FPDMA_QUEUED;
4485 			scmd->satacmd_features_reg_ext =
4486 			    scmd->satacmd_sec_count_msb;
4487 			scmd->satacmd_sec_count_msb = 0;
4488 		} else if ((sdinfo->satadrv_features_support &
4489 		    SATA_DEV_F_TCQ) &&
4490 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4491 		    SATA_CTLF_QCMD)) {
4492 			using_queuing = B_TRUE;
4493 
4494 			/* Legacy queueing */
4495 			if (sdinfo->satadrv_features_support &
4496 			    SATA_DEV_F_LBA48) {
4497 				scmd->satacmd_cmd_reg =
4498 				    SATAC_READ_DMA_QUEUED_EXT;
4499 				scmd->satacmd_features_reg_ext =
4500 				    scmd->satacmd_sec_count_msb;
4501 				scmd->satacmd_sec_count_msb = 0;
4502 			} else {
4503 				scmd->satacmd_cmd_reg =
4504 				    SATAC_READ_DMA_QUEUED;
4505 			}
4506 		} else	/* NCQ nor legacy queuing not supported */
4507 			using_queuing = B_FALSE;
4508 
4509 		/*
4510 		 * If queuing, the sector count goes in the features register
4511 		 * and the secount count will contain the tag.
4512 		 */
4513 		if (using_queuing) {
4514 			scmd->satacmd_features_reg =
4515 			    scmd->satacmd_sec_count_lsb;
4516 			scmd->satacmd_sec_count_lsb = 0;
4517 			scmd->satacmd_flags.sata_queued = B_TRUE;
4518 
4519 			/* Set-up maximum queue depth */
4520 			scmd->satacmd_flags.sata_max_queue_depth =
4521 			    sdinfo->satadrv_max_queue_depth - 1;
4522 		} else if (sdinfo->satadrv_features_enabled &
4523 		    SATA_DEV_F_E_UNTAGGED_QING) {
4524 			/*
4525 			 * Although NCQ/TCQ is not enabled, untagged queuing
4526 			 * may be still used.
4527 			 * Set-up the maximum untagged queue depth.
4528 			 * Use controller's queue depth from sata_hba_tran.
4529 			 * SATA HBA drivers may ignore this value and rely on
4530 			 * the internal limits.For drivers that do not
4531 			 * ignore untaged queue depth, limit the value to
4532 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
4533 			 * largest value that can be passed via
4534 			 * satacmd_flags.sata_max_queue_depth.
4535 			 */
4536 			scmd->satacmd_flags.sata_max_queue_depth =
4537 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
4538 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
4539 
4540 		} else {
4541 			scmd->satacmd_flags.sata_max_queue_depth = 0;
4542 		}
4543 	} else
4544 		scmd->satacmd_flags.sata_max_queue_depth = 0;
4545 
4546 	SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst,
4547 	    "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n",
4548 	    scmd->satacmd_cmd_reg, lba, sec_count);
4549 
4550 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4551 		/* Need callback function */
4552 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
4553 		synch = FALSE;
4554 	} else
4555 		synch = TRUE;
4556 
4557 	/* Transfer command to HBA */
4558 	if (sata_hba_start(spx, &rval) != 0) {
4559 		/* Pkt not accepted for execution */
4560 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4561 		return (rval);
4562 	}
4563 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4564 	/*
4565 	 * If execution is non-synchronous,
4566 	 * a callback function will handle potential errors, translate
4567 	 * the response and will do a callback to a target driver.
4568 	 * If it was synchronous, check execution status using the same
4569 	 * framework callback.
4570 	 */
4571 	if (synch) {
4572 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4573 		    "synchronous execution status %x\n",
4574 		    spx->txlt_sata_pkt->satapkt_reason);
4575 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
4576 	}
4577 	return (TRAN_ACCEPT);
4578 }
4579 
4580 
4581 /*
4582  * SATA translate command: Write (various types)
4583  * Translated into appropriate type of ATA WRITE command
4584  * for SATA hard disks.
4585  * Both the device capabilities and requested operation mode are
4586  * considered.
4587  *
4588  * Following scsi cdb fields are ignored:
4589  * rwprotect, dpo, fua, fua_nv, group_number.
4590  *
4591  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
4592  * enable variable sata_func_enable), the capability of the controller and
4593  * capability of a device are checked and if both support queueing, write
4594  * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT
4595  * command rather than plain WRITE_XXX command.
4596  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
4597  * both the controller and device suport such functionality, the write
4598  * request will be translated to WRITE_FPDMA_QUEUED command.
4599  * In both cases the maximum queue depth is derived as minimum of:
4600  * HBA capability,device capability and sata_max_queue_depth variable setting.
4601  * The value passed to HBA driver is decremented by 1, because only 5 bits are
4602  * used to pass max queue depth value, and the maximum possible queue depth
4603  * is 32.
4604  *
4605  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4606  * appropriate values in scsi_pkt fields.
4607  */
4608 static int
4609 sata_txlt_write(sata_pkt_txlate_t *spx)
4610 {
4611 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4612 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4613 	sata_drive_info_t *sdinfo;
4614 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4615 	int cport = SATA_TXLT_CPORT(spx);
4616 	uint16_t sec_count;
4617 	uint64_t lba;
4618 	int rval;
4619 	int synch;
4620 
4621 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4622 
4623 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
4624 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
4625 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4626 		return (rval);
4627 	}
4628 
4629 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4630 	    &spx->txlt_sata_pkt->satapkt_device);
4631 
4632 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4633 	/*
4634 	 * Extract LBA and sector count from scsi CDB
4635 	 */
4636 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
4637 	case SCMD_WRITE:
4638 		/* 6-byte scsi read cmd : 0x0A */
4639 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
4640 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
4641 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4642 		sec_count = scsipkt->pkt_cdbp[4];
4643 		/* sec_count 0 will be interpreted as 256 by a device */
4644 		break;
4645 	case SCMD_WRITE_G1:
4646 		/* 10-bytes scsi write command : 0x2A */
4647 		lba = scsipkt->pkt_cdbp[2];
4648 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4649 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4650 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4651 		sec_count = scsipkt->pkt_cdbp[7];
4652 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4653 		break;
4654 	case SCMD_WRITE_G5:
4655 		/* 12-bytes scsi read command : 0xAA */
4656 		lba = scsipkt->pkt_cdbp[2];
4657 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4658 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4659 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4660 		sec_count = scsipkt->pkt_cdbp[6];
4661 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
4662 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4663 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
4664 		break;
4665 	case SCMD_WRITE_G4:
4666 		/* 16-bytes scsi write command : 0x8A */
4667 		lba = scsipkt->pkt_cdbp[2];
4668 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4669 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4670 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4671 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
4672 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
4673 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
4674 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
4675 		sec_count = scsipkt->pkt_cdbp[10];
4676 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
4677 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
4678 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
4679 		break;
4680 	default:
4681 		/* Unsupported command */
4682 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4683 		return (sata_txlt_invalid_command(spx));
4684 	}
4685 
4686 	/*
4687 	 * Check if specified address and length exceeds device capacity
4688 	 */
4689 	if ((lba >= sdinfo->satadrv_capacity) ||
4690 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
4691 		/* LBA out of range */
4692 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4693 		return (sata_txlt_lba_out_of_range(spx));
4694 	}
4695 
4696 	/*
4697 	 * For zero-length transfer, emulate good completion of the command
4698 	 * (reasons for rejecting the command were already checked).
4699 	 * No DMA resources were allocated.
4700 	 */
4701 	if (spx->txlt_dma_cookie_list == NULL) {
4702 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4703 		return (sata_emul_rw_completion(spx));
4704 	}
4705 
4706 	/*
4707 	 * Build cmd block depending on the device capability and
4708 	 * requested operation mode.
4709 	 * Do not bother with non-dma mode- we are working only with
4710 	 * devices supporting DMA.
4711 	 */
4712 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
4713 	scmd->satacmd_device_reg = SATA_ADH_LBA;
4714 	scmd->satacmd_cmd_reg = SATAC_WRITE_DMA;
4715 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
4716 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4717 		scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT;
4718 		scmd->satacmd_sec_count_msb = sec_count >> 8;
4719 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
4720 #ifndef __lock_lint
4721 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
4722 		scmd->satacmd_lba_high_msb = lba >> 40;
4723 #endif
4724 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
4725 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
4726 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
4727 	}
4728 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
4729 	scmd->satacmd_lba_low_lsb = lba & 0xff;
4730 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
4731 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
4732 	scmd->satacmd_features_reg = 0;
4733 	scmd->satacmd_status_reg = 0;
4734 	scmd->satacmd_error_reg = 0;
4735 
4736 	/*
4737 	 * Check if queueing commands should be used and switch
4738 	 * to appropriate command if possible
4739 	 */
4740 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
4741 		boolean_t using_queuing;
4742 
4743 		/* Queuing supported by controller and device? */
4744 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
4745 		    (sdinfo->satadrv_features_support &
4746 		    SATA_DEV_F_NCQ) &&
4747 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4748 		    SATA_CTLF_NCQ)) {
4749 			using_queuing = B_TRUE;
4750 
4751 			/* NCQ supported - use FPDMA WRITE */
4752 			scmd->satacmd_cmd_reg =
4753 			    SATAC_WRITE_FPDMA_QUEUED;
4754 			scmd->satacmd_features_reg_ext =
4755 			    scmd->satacmd_sec_count_msb;
4756 			scmd->satacmd_sec_count_msb = 0;
4757 		} else if ((sdinfo->satadrv_features_support &
4758 		    SATA_DEV_F_TCQ) &&
4759 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4760 		    SATA_CTLF_QCMD)) {
4761 			using_queuing = B_TRUE;
4762 
4763 			/* Legacy queueing */
4764 			if (sdinfo->satadrv_features_support &
4765 			    SATA_DEV_F_LBA48) {
4766 				scmd->satacmd_cmd_reg =
4767 				    SATAC_WRITE_DMA_QUEUED_EXT;
4768 				scmd->satacmd_features_reg_ext =
4769 				    scmd->satacmd_sec_count_msb;
4770 				scmd->satacmd_sec_count_msb = 0;
4771 			} else {
4772 				scmd->satacmd_cmd_reg =
4773 				    SATAC_WRITE_DMA_QUEUED;
4774 			}
4775 		} else	/*  NCQ nor legacy queuing not supported */
4776 			using_queuing = B_FALSE;
4777 
4778 		if (using_queuing) {
4779 			scmd->satacmd_features_reg =
4780 			    scmd->satacmd_sec_count_lsb;
4781 			scmd->satacmd_sec_count_lsb = 0;
4782 			scmd->satacmd_flags.sata_queued = B_TRUE;
4783 			/* Set-up maximum queue depth */
4784 			scmd->satacmd_flags.sata_max_queue_depth =
4785 			    sdinfo->satadrv_max_queue_depth - 1;
4786 		} else if (sdinfo->satadrv_features_enabled &
4787 		    SATA_DEV_F_E_UNTAGGED_QING) {
4788 			/*
4789 			 * Although NCQ/TCQ is not enabled, untagged queuing
4790 			 * may be still used.
4791 			 * Set-up the maximum untagged queue depth.
4792 			 * Use controller's queue depth from sata_hba_tran.
4793 			 * SATA HBA drivers may ignore this value and rely on
4794 			 * the internal limits. For drivera that do not
4795 			 * ignore untaged queue depth, limit the value to
4796 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
4797 			 * largest value that can be passed via
4798 			 * satacmd_flags.sata_max_queue_depth.
4799 			 */
4800 			scmd->satacmd_flags.sata_max_queue_depth =
4801 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
4802 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
4803 
4804 		} else {
4805 			scmd->satacmd_flags.sata_max_queue_depth = 0;
4806 		}
4807 	} else
4808 		scmd->satacmd_flags.sata_max_queue_depth = 0;
4809 
4810 	SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4811 	    "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n",
4812 	    scmd->satacmd_cmd_reg, lba, sec_count);
4813 
4814 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4815 		/* Need callback function */
4816 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
4817 		synch = FALSE;
4818 	} else
4819 		synch = TRUE;
4820 
4821 	/* Transfer command to HBA */
4822 	if (sata_hba_start(spx, &rval) != 0) {
4823 		/* Pkt not accepted for execution */
4824 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4825 		return (rval);
4826 	}
4827 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4828 
4829 	/*
4830 	 * If execution is non-synchronous,
4831 	 * a callback function will handle potential errors, translate
4832 	 * the response and will do a callback to a target driver.
4833 	 * If it was synchronous, check execution status using the same
4834 	 * framework callback.
4835 	 */
4836 	if (synch) {
4837 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4838 		    "synchronous execution status %x\n",
4839 		    spx->txlt_sata_pkt->satapkt_reason);
4840 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
4841 	}
4842 	return (TRAN_ACCEPT);
4843 }
4844 
4845 
4846 /*
4847  * Implements SCSI SBC WRITE BUFFER command download microcode option
4848  */
4849 static int
4850 sata_txlt_write_buffer(sata_pkt_txlate_t *spx)
4851 {
4852 #define	WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE			4
4853 #define	WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE			5
4854 
4855 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4856 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4857 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4858 	struct scsi_extended_sense *sense;
4859 	int rval, mode, sector_count;
4860 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4861 	int cport = SATA_TXLT_CPORT(spx);
4862 	boolean_t synch;
4863 
4864 	synch = (spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH) != 0;
4865 	mode = scsipkt->pkt_cdbp[1] & 0x1f;
4866 
4867 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4868 	    "sata_txlt_write_buffer, mode 0x%x\n", mode);
4869 
4870 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4871 
4872 	if ((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) {
4873 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4874 		return (rval);
4875 	}
4876 
4877 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4878 
4879 	scsipkt->pkt_reason = CMD_CMPLT;
4880 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4881 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4882 
4883 	/*
4884 	 * The SCSI to ATA translation specification only calls
4885 	 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE.
4886 	 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but
4887 	 * ATA 8 (draft) got rid of download microcode for temp
4888 	 * and it is even optional for ATA 7, so it may be aborted.
4889 	 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as
4890 	 * it is not specified and the buffer offset for SCSI is a 16-bit
4891 	 * value in bytes, but for ATA it is a 16-bit offset in 512 byte
4892 	 * sectors.  Thus the offset really doesn't buy us anything.
4893 	 * If and when ATA 8 is stabilized and the SCSI to ATA specification
4894 	 * is revised, this can be revisisted.
4895 	 */
4896 	/* Reject not supported request */
4897 	switch (mode) {
4898 	case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE:
4899 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP;
4900 		break;
4901 	case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE:
4902 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE;
4903 		break;
4904 	default:
4905 		goto bad_param;
4906 	}
4907 
4908 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
4909 
4910 	scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE;
4911 	if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0)
4912 		goto bad_param;
4913 	sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE;
4914 	scmd->satacmd_sec_count_lsb = (uint8_t)sector_count;
4915 	scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8;
4916 	scmd->satacmd_lba_mid_lsb = 0;
4917 	scmd->satacmd_lba_high_lsb = 0;
4918 	scmd->satacmd_device_reg = 0;
4919 	spx->txlt_sata_pkt->satapkt_comp =
4920 	    sata_txlt_download_mcode_cmd_completion;
4921 	scmd->satacmd_addr_type = 0;
4922 
4923 	/* Transfer command to HBA */
4924 	if (sata_hba_start(spx, &rval) != 0) {
4925 		/* Pkt not accepted for execution */
4926 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4927 		return (rval);
4928 	}
4929 
4930 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4931 	/*
4932 	 * If execution is non-synchronous,
4933 	 * a callback function will handle potential errors, translate
4934 	 * the response and will do a callback to a target driver.
4935 	 * If it was synchronous, check execution status using the same
4936 	 * framework callback.
4937 	 */
4938 	if (synch) {
4939 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4940 		    "synchronous execution\n", NULL);
4941 		/* Calling pre-set completion routine */
4942 		(*spx->txlt_sata_pkt->satapkt_comp)(spx->txlt_sata_pkt);
4943 	}
4944 	return (TRAN_ACCEPT);
4945 
4946 bad_param:
4947 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4948 	*scsipkt->pkt_scbp = STATUS_CHECK;
4949 	sense = sata_arq_sense(spx);
4950 	sense->es_key = KEY_ILLEGAL_REQUEST;
4951 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4952 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4953 	    scsipkt->pkt_comp != NULL) {
4954 		/* scsi callback required */
4955 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4956 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4957 		    TQ_SLEEP) == 0) {
4958 			/* Scheduling the callback failed */
4959 			rval = TRAN_BUSY;
4960 		}
4961 	}
4962 	return (rval);
4963 }
4964 
4965 
4966 /*
4967  * Retry identify device when command returns SATA_INCOMPLETE_DATA
4968  * after doing a firmware download.
4969  */
4970 static void
4971 sata_retry_identify_device(void *arg)
4972 {
4973 #define	DOWNLOAD_WAIT_TIME_SECS	60
4974 #define	DOWNLOAD_WAIT_INTERVAL_SECS	1
4975 	int rval;
4976 	int retry_cnt;
4977 	sata_pkt_t *sata_pkt = (sata_pkt_t *)arg;
4978 	sata_pkt_txlate_t *spx =
4979 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
4980 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4981 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
4982 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
4983 	sata_drive_info_t *sdinfo;
4984 
4985 	/*
4986 	 * Before returning good status, probe device.
4987 	 * Device probing will get IDENTIFY DEVICE data, if possible.
4988 	 * The assumption is that the new microcode is applied by the
4989 	 * device. It is a caller responsibility to verify this.
4990 	 */
4991 	for (retry_cnt = 0;
4992 	    retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS;
4993 	    retry_cnt++) {
4994 		rval = sata_probe_device(sata_hba_inst, &sata_device);
4995 
4996 		if (rval == SATA_SUCCESS) { /* Set default features */
4997 			sdinfo = sata_get_device_info(sata_hba_inst,
4998 			    &sata_device);
4999 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
5000 			    SATA_SUCCESS) {
5001 				/* retry */
5002 				(void) sata_initialize_device(sata_hba_inst,
5003 				    sdinfo);
5004 			}
5005 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5006 			    scsipkt->pkt_comp != NULL)
5007 				(*scsipkt->pkt_comp)(scsipkt);
5008 			return;
5009 		} else if (rval == SATA_RETRY) {
5010 			delay(drv_usectohz(1000000 *
5011 			    DOWNLOAD_WAIT_INTERVAL_SECS));
5012 			continue;
5013 		} else	/* failed - no reason to retry */
5014 			break;
5015 	}
5016 
5017 	/*
5018 	 * Something went wrong, device probing failed.
5019 	 */
5020 	SATA_LOG_D((sata_hba_inst, CE_WARN,
5021 	    "Cannot probe device after downloading microcode\n"));
5022 
5023 	/* Reset device to force retrying the probe. */
5024 	(void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst))
5025 	    (SATA_DIP(sata_hba_inst), &sata_device);
5026 
5027 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5028 	    scsipkt->pkt_comp != NULL)
5029 		(*scsipkt->pkt_comp)(scsipkt);
5030 }
5031 
5032 /*
5033  * Translate completion status of download microcode command.
5034  * pkt completion_reason is checked to determine the completion status.
5035  * Do scsi callback if necessary (FLAG_NOINTR == 0)
5036  *
5037  * Note: this function may be called also for synchronously executed
5038  * command.
5039  * This function may be used only if scsi_pkt is non-NULL.
5040  */
5041 static void
5042 sata_txlt_download_mcode_cmd_completion(sata_pkt_t *sata_pkt)
5043 {
5044 	sata_pkt_txlate_t *spx =
5045 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
5046 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5047 	struct scsi_extended_sense *sense;
5048 	sata_drive_info_t *sdinfo;
5049 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
5050 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
5051 	int rval;
5052 
5053 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5054 	    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
5055 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5056 		scsipkt->pkt_reason = CMD_CMPLT;
5057 
5058 		rval = sata_probe_device(sata_hba_inst, &sata_device);
5059 
5060 		if (rval == SATA_SUCCESS) { /* Set default features */
5061 			sdinfo = sata_get_device_info(sata_hba_inst,
5062 			    &sata_device);
5063 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
5064 			    SATA_SUCCESS) {
5065 				/* retry */
5066 				(void) sata_initialize_device(sata_hba_inst,
5067 				    sdinfo);
5068 			}
5069 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5070 			    scsipkt->pkt_comp != NULL)
5071 				(*scsipkt->pkt_comp)(scsipkt);
5072 		} else {
5073 			(void) ddi_taskq_dispatch(
5074 			    (ddi_taskq_t *)SATA_TXLT_TASKQ(spx),
5075 			    sata_retry_identify_device,
5076 			    (void *)sata_pkt, TQ_NOSLEEP);
5077 		}
5078 
5079 
5080 	} else {
5081 		/* Something went wrong, microcode download command failed */
5082 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5083 		*scsipkt->pkt_scbp = STATUS_CHECK;
5084 		sense = sata_arq_sense(spx);
5085 		switch (sata_pkt->satapkt_reason) {
5086 		case SATA_PKT_PORT_ERROR:
5087 			/*
5088 			 * We have no device data. Assume no data transfered.
5089 			 */
5090 			sense->es_key = KEY_HARDWARE_ERROR;
5091 			break;
5092 
5093 		case SATA_PKT_DEV_ERROR:
5094 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5095 			    SATA_STATUS_ERR) {
5096 				/*
5097 				 * determine dev error reason from error
5098 				 * reg content
5099 				 */
5100 				sata_decode_device_error(spx, sense);
5101 				break;
5102 			}
5103 			/* No extended sense key - no info available */
5104 			break;
5105 
5106 		case SATA_PKT_TIMEOUT:
5107 			scsipkt->pkt_reason = CMD_TIMEOUT;
5108 			scsipkt->pkt_statistics |=
5109 			    STAT_TIMEOUT | STAT_DEV_RESET;
5110 			/* No extended sense key ? */
5111 			break;
5112 
5113 		case SATA_PKT_ABORTED:
5114 			scsipkt->pkt_reason = CMD_ABORTED;
5115 			scsipkt->pkt_statistics |= STAT_ABORTED;
5116 			/* No extended sense key ? */
5117 			break;
5118 
5119 		case SATA_PKT_RESET:
5120 			/* pkt aborted by an explicit reset from a host */
5121 			scsipkt->pkt_reason = CMD_RESET;
5122 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5123 			break;
5124 
5125 		default:
5126 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5127 			    "sata_txlt_nodata_cmd_completion: "
5128 			    "invalid packet completion reason %d",
5129 			    sata_pkt->satapkt_reason));
5130 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5131 			break;
5132 		}
5133 
5134 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5135 		    "scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5136 
5137 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5138 		    scsipkt->pkt_comp != NULL)
5139 			/* scsi callback required */
5140 			(*scsipkt->pkt_comp)(scsipkt);
5141 	}
5142 }
5143 
5144 
5145 
5146 
5147 /*
5148  * Translate command: Synchronize Cache.
5149  * Translates into Flush Cache command for SATA hard disks.
5150  *
5151  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
5152  * appropriate values in scsi_pkt fields.
5153  */
5154 static 	int
5155 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx)
5156 {
5157 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5158 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
5159 	int cport = SATA_TXLT_CPORT(spx);
5160 	int rval;
5161 	int synch;
5162 
5163 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5164 
5165 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
5166 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
5167 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5168 		return (rval);
5169 	}
5170 
5171 	scmd->satacmd_addr_type = 0;
5172 	scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE;
5173 	scmd->satacmd_device_reg = 0;
5174 	scmd->satacmd_sec_count_lsb = 0;
5175 	scmd->satacmd_lba_low_lsb = 0;
5176 	scmd->satacmd_lba_mid_lsb = 0;
5177 	scmd->satacmd_lba_high_lsb = 0;
5178 	scmd->satacmd_features_reg = 0;
5179 	scmd->satacmd_status_reg = 0;
5180 	scmd->satacmd_error_reg = 0;
5181 
5182 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5183 	    "sata_txlt_synchronize_cache\n", NULL);
5184 
5185 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5186 		/* Need to set-up a callback function */
5187 		spx->txlt_sata_pkt->satapkt_comp =
5188 		    sata_txlt_nodata_cmd_completion;
5189 		synch = FALSE;
5190 	} else
5191 		synch = TRUE;
5192 
5193 	/* Transfer command to HBA */
5194 	if (sata_hba_start(spx, &rval) != 0) {
5195 		/* Pkt not accepted for execution */
5196 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5197 		return (rval);
5198 	}
5199 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5200 
5201 	/*
5202 	 * If execution non-synchronous, it had to be completed
5203 	 * a callback function will handle potential errors, translate
5204 	 * the response and will do a callback to a target driver.
5205 	 * If it was synchronous, check status, using the same
5206 	 * framework callback.
5207 	 */
5208 	if (synch) {
5209 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5210 		    "synchronous execution status %x\n",
5211 		    spx->txlt_sata_pkt->satapkt_reason);
5212 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
5213 	}
5214 	return (TRAN_ACCEPT);
5215 }
5216 
5217 
5218 /*
5219  * Send pkt to SATA HBA driver
5220  *
5221  * This function may be called only if the operation is requested by scsi_pkt,
5222  * i.e. scsi_pkt is not NULL.
5223  *
5224  * This function has to be called with cport mutex held. It does release
5225  * the mutex when it calls HBA driver sata_tran_start function and
5226  * re-acquires it afterwards.
5227  *
5228  * If return value is 0, pkt was accepted, -1 otherwise
5229  * rval is set to appropriate sata_scsi_start return value.
5230  *
5231  * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not
5232  * have called the sata_pkt callback function for this packet.
5233  *
5234  * The scsi callback has to be performed by the caller of this routine.
5235  *
5236  * Note 2: No port multiplier support for now.
5237  */
5238 static int
5239 sata_hba_start(sata_pkt_txlate_t *spx, int *rval)
5240 {
5241 	int stat, cport;
5242 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
5243 	sata_drive_info_t *sdinfo;
5244 	sata_device_t *sata_device;
5245 	uint8_t cmd;
5246 	struct sata_cmd_flags cmd_flags;
5247 
5248 	ASSERT(spx->txlt_sata_pkt != NULL);
5249 
5250 	cport = SATA_TXLT_CPORT(spx);
5251 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5252 
5253 	sdinfo = sata_get_device_info(sata_hba_inst,
5254 	    &spx->txlt_sata_pkt->satapkt_device);
5255 	ASSERT(sdinfo != NULL);
5256 
5257 	/* Clear device reset state? */
5258 	if (sdinfo->satadrv_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) {
5259 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
5260 		    sata_clear_dev_reset = B_TRUE;
5261 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_CLEAR_DEVICE_RESET;
5262 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5263 		    "sata_hba_start: clearing device reset state\n", NULL);
5264 	}
5265 	cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg;
5266 	cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags;
5267 	sata_device = &spx->txlt_sata_pkt->satapkt_device;
5268 
5269 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5270 
5271 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5272 	    "Sata cmd 0x%2x\n", cmd);
5273 
5274 	stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
5275 	    spx->txlt_sata_pkt);
5276 
5277 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5278 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
5279 	/*
5280 	 * If sata pkt was accepted and executed in asynchronous mode, i.e.
5281 	 * with the sata callback, the sata_pkt could be already destroyed
5282 	 * by the time we check ther return status from the hba_start()
5283 	 * function, because sata_scsi_destroy_pkt() could have been already
5284 	 * called (perhaps in the interrupt context). So, in such case, there
5285 	 * should be no references to it. In other cases, sata_pkt still
5286 	 * exists.
5287 	 */
5288 	switch (stat) {
5289 	case SATA_TRAN_ACCEPTED:
5290 		/*
5291 		 * pkt accepted for execution.
5292 		 * If it was executed synchronously, it is already completed
5293 		 * and pkt completion_reason indicates completion status.
5294 		 */
5295 		*rval = TRAN_ACCEPT;
5296 		return (0);
5297 
5298 	case SATA_TRAN_QUEUE_FULL:
5299 		/*
5300 		 * Controller detected queue full condition.
5301 		 */
5302 		SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst,
5303 		    "sata_hba_start: queue full\n", NULL);
5304 
5305 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
5306 		*spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL;
5307 
5308 		*rval = TRAN_BUSY;
5309 		break;
5310 
5311 	case SATA_TRAN_PORT_ERROR:
5312 		/*
5313 		 * Communication/link with device or general port error
5314 		 * detected before pkt execution begun.
5315 		 */
5316 		if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
5317 		    SATA_ADDR_CPORT ||
5318 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
5319 		    SATA_ADDR_DCPORT)
5320 			sata_log(sata_hba_inst, CE_CONT,
5321 			    "SATA port %d error",
5322 			    sata_device->satadev_addr.cport);
5323 		else
5324 			sata_log(sata_hba_inst, CE_CONT,
5325 			    "SATA port %d pmport %d error\n",
5326 			    sata_device->satadev_addr.cport,
5327 			    sata_device->satadev_addr.pmport);
5328 
5329 		/*
5330 		 * Update the port/device structure.
5331 		 * sata_pkt should be still valid. Since port error is
5332 		 * returned, sata_device content should reflect port
5333 		 * state - it means, that sata address have been changed,
5334 		 * because original packet's sata address refered to a device
5335 		 * attached to some port.
5336 		 */
5337 		sata_update_port_info(sata_hba_inst, sata_device);
5338 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
5339 		*rval = TRAN_FATAL_ERROR;
5340 		break;
5341 
5342 	case SATA_TRAN_CMD_UNSUPPORTED:
5343 		/*
5344 		 * Command rejected by HBA as unsupported. It was HBA driver
5345 		 * that rejected the command, command was not sent to
5346 		 * an attached device.
5347 		 */
5348 		if ((sdinfo != NULL) &&
5349 		    (sdinfo->satadrv_state & SATA_DSTATE_RESET))
5350 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5351 			    "sat_hba_start: cmd 0x%2x rejected "
5352 			    "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd);
5353 
5354 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5355 		(void) sata_txlt_invalid_command(spx);
5356 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5357 
5358 		*rval = TRAN_ACCEPT;
5359 		break;
5360 
5361 	case SATA_TRAN_BUSY:
5362 		/*
5363 		 * Command rejected by HBA because other operation prevents
5364 		 * accepting the packet, or device is in RESET condition.
5365 		 */
5366 		if (sdinfo != NULL) {
5367 			sdinfo->satadrv_state =
5368 			    spx->txlt_sata_pkt->satapkt_device.satadev_state;
5369 
5370 			if (sdinfo->satadrv_state & SATA_DSTATE_RESET) {
5371 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5372 				    "sata_hba_start: cmd 0x%2x rejected "
5373 				    "because of device reset condition\n",
5374 				    cmd);
5375 			} else {
5376 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5377 				    "sata_hba_start: cmd 0x%2x rejected "
5378 				    "with SATA_TRAN_BUSY status\n",
5379 				    cmd);
5380 			}
5381 		}
5382 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
5383 		*rval = TRAN_BUSY;
5384 		break;
5385 
5386 	default:
5387 		/* Unrecognized HBA response */
5388 		SATA_LOG_D((sata_hba_inst, CE_WARN,
5389 		    "sata_hba_start: unrecognized HBA response "
5390 		    "to cmd : 0x%2x resp 0x%x", cmd, rval));
5391 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
5392 		*rval = TRAN_FATAL_ERROR;
5393 		break;
5394 	}
5395 
5396 	/*
5397 	 * If we got here, the packet was rejected.
5398 	 * Check if we need to remember reset state clearing request
5399 	 */
5400 	if (cmd_flags.sata_clear_dev_reset) {
5401 		/*
5402 		 * Check if device is still configured - it may have
5403 		 * disapeared from the configuration
5404 		 */
5405 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
5406 		if (sdinfo != NULL) {
5407 			/*
5408 			 * Restore the flag that requests clearing of
5409 			 * the device reset state,
5410 			 * so the next sata packet may carry it to HBA.
5411 			 */
5412 			sdinfo->satadrv_event_flags |=
5413 			    SATA_EVNT_CLEAR_DEVICE_RESET;
5414 		}
5415 	}
5416 	return (-1);
5417 }
5418 
5419 /*
5420  * Scsi response setup for invalid LBA
5421  *
5422  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
5423  */
5424 static int
5425 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx)
5426 {
5427 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5428 	struct scsi_extended_sense *sense;
5429 
5430 	scsipkt->pkt_reason = CMD_CMPLT;
5431 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5432 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5433 	*scsipkt->pkt_scbp = STATUS_CHECK;
5434 
5435 	*scsipkt->pkt_scbp = STATUS_CHECK;
5436 	sense = sata_arq_sense(spx);
5437 	sense->es_key = KEY_ILLEGAL_REQUEST;
5438 	sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
5439 
5440 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5441 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5442 
5443 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5444 	    scsipkt->pkt_comp != NULL)
5445 		/* scsi callback required */
5446 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5447 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5448 		    TQ_SLEEP) == NULL)
5449 			/* Scheduling the callback failed */
5450 			return (TRAN_BUSY);
5451 	return (TRAN_ACCEPT);
5452 }
5453 
5454 
5455 /*
5456  * Analyze device status and error registers and translate them into
5457  * appropriate scsi sense codes.
5458  * NOTE: non-packet commands only for now
5459  */
5460 static void
5461 sata_decode_device_error(sata_pkt_txlate_t *spx,
5462     struct scsi_extended_sense *sense)
5463 {
5464 	uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg;
5465 
5466 	ASSERT(sense != NULL);
5467 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
5468 	    SATA_STATUS_ERR);
5469 
5470 
5471 	if (err_reg & SATA_ERROR_ICRC) {
5472 		sense->es_key = KEY_ABORTED_COMMAND;
5473 		sense->es_add_code = 0x08; /* Communication failure */
5474 		return;
5475 	}
5476 
5477 	if (err_reg & SATA_ERROR_UNC) {
5478 		sense->es_key = KEY_MEDIUM_ERROR;
5479 		/* Information bytes (LBA) need to be set by a caller */
5480 		return;
5481 	}
5482 
5483 	/* ADD HERE: MC error bit handling for ATAPI CD/DVD */
5484 	if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) {
5485 		sense->es_key = KEY_UNIT_ATTENTION;
5486 		sense->es_add_code = 0x3a; /* No media present */
5487 		return;
5488 	}
5489 
5490 	if (err_reg & SATA_ERROR_IDNF) {
5491 		if (err_reg & SATA_ERROR_ABORT) {
5492 			sense->es_key = KEY_ABORTED_COMMAND;
5493 		} else {
5494 			sense->es_key = KEY_ILLEGAL_REQUEST;
5495 			sense->es_add_code = 0x21; /* LBA out of range */
5496 		}
5497 		return;
5498 	}
5499 
5500 	if (err_reg & SATA_ERROR_ABORT) {
5501 		ASSERT(spx->txlt_sata_pkt != NULL);
5502 		sense->es_key = KEY_ABORTED_COMMAND;
5503 		return;
5504 	}
5505 }
5506 
5507 /*
5508  * Extract error LBA from sata_pkt.satapkt_cmd register fields
5509  */
5510 static void
5511 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba)
5512 {
5513 	sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd;
5514 
5515 	*lba = 0;
5516 	if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) {
5517 		*lba = sata_cmd->satacmd_lba_high_msb;
5518 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb;
5519 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb;
5520 	} else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) {
5521 		*lba = sata_cmd->satacmd_device_reg & 0xf;
5522 	}
5523 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb;
5524 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb;
5525 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb;
5526 }
5527 
5528 /*
5529  * This is fixed sense format - if LBA exceeds the info field size,
5530  * no valid info will be returned (valid bit in extended sense will
5531  * be set to 0).
5532  */
5533 static struct scsi_extended_sense *
5534 sata_arq_sense(sata_pkt_txlate_t *spx)
5535 {
5536 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5537 	struct scsi_arq_status *arqs;
5538 	struct scsi_extended_sense *sense;
5539 
5540 	/* Fill ARQ sense data */
5541 	scsipkt->pkt_state |= STATE_ARQ_DONE;
5542 	arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp;
5543 	*(uchar_t *)&arqs->sts_status = STATUS_CHECK;
5544 	*(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD;
5545 	arqs->sts_rqpkt_reason = CMD_CMPLT;
5546 	arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5547 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
5548 	arqs->sts_rqpkt_resid = 0;
5549 	sense = &arqs->sts_sensedata;
5550 	bzero(sense, sizeof (struct scsi_extended_sense));
5551 	sata_fixed_sense_data_preset(sense);
5552 	return (sense);
5553 }
5554 
5555 
5556 /*
5557  * Emulated SATA Read/Write command completion for zero-length requests.
5558  * This request always succedes, so in synchronous mode it always returns
5559  * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the
5560  * callback cannot be scheduled.
5561  */
5562 static int
5563 sata_emul_rw_completion(sata_pkt_txlate_t *spx)
5564 {
5565 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5566 
5567 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5568 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5569 	scsipkt->pkt_reason = CMD_CMPLT;
5570 	*scsipkt->pkt_scbp = STATUS_GOOD;
5571 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5572 		/* scsi callback required - have to schedule it */
5573 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5574 		    (task_func_t *)scsipkt->pkt_comp,
5575 		    (void *)scsipkt, TQ_SLEEP) == NULL)
5576 			/* Scheduling the callback failed */
5577 			return (TRAN_BUSY);
5578 	}
5579 	return (TRAN_ACCEPT);
5580 }
5581 
5582 
5583 /*
5584  * Translate completion status of SATA read/write commands into scsi response.
5585  * pkt completion_reason is checked to determine the completion status.
5586  * Do scsi callback if necessary.
5587  *
5588  * Note: this function may be called also for synchronously executed
5589  * commands.
5590  * This function may be used only if scsi_pkt is non-NULL.
5591  */
5592 static void
5593 sata_txlt_rw_completion(sata_pkt_t *sata_pkt)
5594 {
5595 	sata_pkt_txlate_t *spx =
5596 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
5597 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
5598 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5599 	struct scsi_extended_sense *sense;
5600 	uint64_t lba;
5601 	struct buf *bp;
5602 	int rval;
5603 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5604 		/* Normal completion */
5605 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5606 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
5607 		scsipkt->pkt_reason = CMD_CMPLT;
5608 		*scsipkt->pkt_scbp = STATUS_GOOD;
5609 		if (spx->txlt_tmp_buf != NULL) {
5610 			/* Temporary buffer was used */
5611 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5612 			if (bp->b_flags & B_READ) {
5613 				rval = ddi_dma_sync(
5614 				    spx->txlt_buf_dma_handle, 0, 0,
5615 				    DDI_DMA_SYNC_FORCPU);
5616 				ASSERT(rval == DDI_SUCCESS);
5617 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
5618 				    bp->b_bcount);
5619 			}
5620 		}
5621 	} else {
5622 		/*
5623 		 * Something went wrong - analyze return
5624 		 */
5625 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5626 		    STATE_SENT_CMD | STATE_GOT_STATUS;
5627 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5628 		*scsipkt->pkt_scbp = STATUS_CHECK;
5629 		sense = sata_arq_sense(spx);
5630 		ASSERT(sense != NULL);
5631 
5632 		/*
5633 		 * SATA_PKT_DEV_ERROR is the only case where we may be able to
5634 		 * extract from device registers the failing LBA.
5635 		 */
5636 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
5637 			if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) &&
5638 			    (scmd->satacmd_lba_mid_msb != 0 ||
5639 			    scmd->satacmd_lba_high_msb != 0)) {
5640 				/*
5641 				 * We have problem reporting this cmd LBA
5642 				 * in fixed sense data format, because of
5643 				 * the size of the scsi LBA fields.
5644 				 */
5645 				sense->es_valid = 0;
5646 			} else {
5647 				sata_extract_error_lba(spx, &lba);
5648 				sense->es_info_1 = (lba & 0xFF000000) >> 24;
5649 				sense->es_info_2 = (lba & 0xFF0000) >> 16;
5650 				sense->es_info_3 = (lba & 0xFF00) >> 8;
5651 				sense->es_info_4 = lba & 0xFF;
5652 			}
5653 		} else {
5654 			/* Invalid extended sense info */
5655 			sense->es_valid = 0;
5656 		}
5657 
5658 		switch (sata_pkt->satapkt_reason) {
5659 		case SATA_PKT_PORT_ERROR:
5660 			/* We may want to handle DEV GONE state as well */
5661 			/*
5662 			 * We have no device data. Assume no data transfered.
5663 			 */
5664 			sense->es_key = KEY_HARDWARE_ERROR;
5665 			break;
5666 
5667 		case SATA_PKT_DEV_ERROR:
5668 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5669 			    SATA_STATUS_ERR) {
5670 				/*
5671 				 * determine dev error reason from error
5672 				 * reg content
5673 				 */
5674 				sata_decode_device_error(spx, sense);
5675 				if (sense->es_key == KEY_MEDIUM_ERROR) {
5676 					switch (scmd->satacmd_cmd_reg) {
5677 					case SATAC_READ_DMA:
5678 					case SATAC_READ_DMA_EXT:
5679 					case SATAC_READ_DMA_QUEUED:
5680 					case SATAC_READ_DMA_QUEUED_EXT:
5681 					case SATAC_READ_FPDMA_QUEUED:
5682 						/* Unrecovered read error */
5683 						sense->es_add_code =
5684 						    SD_SCSI_ASC_UNREC_READ_ERR;
5685 						break;
5686 					case SATAC_WRITE_DMA:
5687 					case SATAC_WRITE_DMA_EXT:
5688 					case SATAC_WRITE_DMA_QUEUED:
5689 					case SATAC_WRITE_DMA_QUEUED_EXT:
5690 					case SATAC_WRITE_FPDMA_QUEUED:
5691 						/* Write error */
5692 						sense->es_add_code =
5693 						    SD_SCSI_ASC_WRITE_ERR;
5694 						break;
5695 					default:
5696 						/* Internal error */
5697 						SATA_LOG_D((
5698 						    spx->txlt_sata_hba_inst,
5699 						    CE_WARN,
5700 						    "sata_txlt_rw_completion :"
5701 						    "internal error - invalid "
5702 						    "command 0x%2x",
5703 						    scmd->satacmd_cmd_reg));
5704 						break;
5705 					}
5706 				}
5707 				break;
5708 			}
5709 			/* No extended sense key - no info available */
5710 			scsipkt->pkt_reason = CMD_INCOMPLETE;
5711 			break;
5712 
5713 		case SATA_PKT_TIMEOUT:
5714 			scsipkt->pkt_reason = CMD_TIMEOUT;
5715 			scsipkt->pkt_statistics |=
5716 			    STAT_TIMEOUT | STAT_DEV_RESET;
5717 			sense->es_key = KEY_ABORTED_COMMAND;
5718 			break;
5719 
5720 		case SATA_PKT_ABORTED:
5721 			scsipkt->pkt_reason = CMD_ABORTED;
5722 			scsipkt->pkt_statistics |= STAT_ABORTED;
5723 			sense->es_key = KEY_ABORTED_COMMAND;
5724 			break;
5725 
5726 		case SATA_PKT_RESET:
5727 			scsipkt->pkt_reason = CMD_RESET;
5728 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5729 			sense->es_key = KEY_ABORTED_COMMAND;
5730 			break;
5731 
5732 		default:
5733 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5734 			    "sata_txlt_rw_completion: "
5735 			    "invalid packet completion reason"));
5736 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5737 			break;
5738 		}
5739 	}
5740 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5741 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5742 
5743 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5744 	    scsipkt->pkt_comp != NULL)
5745 		/* scsi callback required */
5746 		(*scsipkt->pkt_comp)(scsipkt);
5747 }
5748 
5749 
5750 /*
5751  * Translate completion status of non-data commands (i.e. commands returning
5752  * no data).
5753  * pkt completion_reason is checked to determine the completion status.
5754  * Do scsi callback if necessary (FLAG_NOINTR == 0)
5755  *
5756  * Note: this function may be called also for synchronously executed
5757  * commands.
5758  * This function may be used only if scsi_pkt is non-NULL.
5759  */
5760 
5761 static 	void
5762 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt)
5763 {
5764 	sata_pkt_txlate_t *spx =
5765 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
5766 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5767 	struct scsi_extended_sense *sense;
5768 
5769 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5770 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5771 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5772 		/* Normal completion */
5773 		scsipkt->pkt_reason = CMD_CMPLT;
5774 		*scsipkt->pkt_scbp = STATUS_GOOD;
5775 	} else {
5776 		/* Something went wrong */
5777 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5778 		*scsipkt->pkt_scbp = STATUS_CHECK;
5779 		sense = sata_arq_sense(spx);
5780 		switch (sata_pkt->satapkt_reason) {
5781 		case SATA_PKT_PORT_ERROR:
5782 			/*
5783 			 * We have no device data. Assume no data transfered.
5784 			 */
5785 			sense->es_key = KEY_HARDWARE_ERROR;
5786 			break;
5787 
5788 		case SATA_PKT_DEV_ERROR:
5789 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5790 			    SATA_STATUS_ERR) {
5791 				/*
5792 				 * determine dev error reason from error
5793 				 * reg content
5794 				 */
5795 				sata_decode_device_error(spx, sense);
5796 				break;
5797 			}
5798 			/* No extended sense key - no info available */
5799 			break;
5800 
5801 		case SATA_PKT_TIMEOUT:
5802 			scsipkt->pkt_reason = CMD_TIMEOUT;
5803 			scsipkt->pkt_statistics |=
5804 			    STAT_TIMEOUT | STAT_DEV_RESET;
5805 			/* No extended sense key ? */
5806 			break;
5807 
5808 		case SATA_PKT_ABORTED:
5809 			scsipkt->pkt_reason = CMD_ABORTED;
5810 			scsipkt->pkt_statistics |= STAT_ABORTED;
5811 			/* No extended sense key ? */
5812 			break;
5813 
5814 		case SATA_PKT_RESET:
5815 			/* pkt aborted by an explicit reset from a host */
5816 			scsipkt->pkt_reason = CMD_RESET;
5817 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5818 			break;
5819 
5820 		default:
5821 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5822 			    "sata_txlt_nodata_cmd_completion: "
5823 			    "invalid packet completion reason %d",
5824 			    sata_pkt->satapkt_reason));
5825 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5826 			break;
5827 		}
5828 
5829 	}
5830 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5831 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5832 
5833 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5834 	    scsipkt->pkt_comp != NULL)
5835 		/* scsi callback required */
5836 		(*scsipkt->pkt_comp)(scsipkt);
5837 }
5838 
5839 
5840 /*
5841  * Build Mode sense R/W recovery page
5842  * NOT IMPLEMENTED
5843  */
5844 
5845 static int
5846 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5847 {
5848 #ifndef __lock_lint
5849 	_NOTE(ARGUNUSED(sdinfo))
5850 	_NOTE(ARGUNUSED(pcntrl))
5851 	_NOTE(ARGUNUSED(buf))
5852 #endif
5853 	return (0);
5854 }
5855 
5856 /*
5857  * Build Mode sense caching page  -  scsi-3 implementation.
5858  * Page length distinguishes previous format from scsi-3 format.
5859  * buf must have space for 0x12 bytes.
5860  * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable.
5861  *
5862  */
5863 static int
5864 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5865 {
5866 	struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf;
5867 	sata_id_t *sata_id = &sdinfo->satadrv_id;
5868 
5869 	/*
5870 	 * Most of the fields are set to 0, being not supported and/or disabled
5871 	 */
5872 	bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3);
5873 
5874 	/* Saved paramters not supported */
5875 	if (pcntrl == 3)
5876 		return (0);
5877 	if (pcntrl == 0 || pcntrl == 2) {
5878 		/*
5879 		 * For now treat current and default parameters as same
5880 		 * That may have to change, if target driver will complain
5881 		 */
5882 		page->mode_page.code = MODEPAGE_CACHING;	/* PS = 0 */
5883 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
5884 
5885 		if ((sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) &&
5886 		    !(sata_id->ai_features85 & SATA_LOOK_AHEAD)) {
5887 			page->dra = 1;		/* Read Ahead disabled */
5888 			page->rcd = 1;		/* Read Cache disabled */
5889 		}
5890 		if ((sata_id->ai_cmdset82 & SATA_WRITE_CACHE) &&
5891 		    (sata_id->ai_features85 & SATA_WRITE_CACHE))
5892 			page->wce = 1;		/* Write Cache enabled */
5893 	} else {
5894 		/* Changeable parameters */
5895 		page->mode_page.code = MODEPAGE_CACHING;
5896 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
5897 		if (sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) {
5898 			page->dra = 1;
5899 			page->rcd = 1;
5900 		}
5901 		if (sata_id->ai_cmdset82 & SATA_WRITE_CACHE)
5902 			page->wce = 1;
5903 	}
5904 	return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
5905 	    sizeof (struct mode_page));
5906 }
5907 
5908 /*
5909  * Build Mode sense exception cntrl page
5910  */
5911 static int
5912 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5913 {
5914 	struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf;
5915 	sata_id_t *sata_id = &sdinfo->satadrv_id;
5916 
5917 	/*
5918 	 * Most of the fields are set to 0, being not supported and/or disabled
5919 	 */
5920 	bzero(buf, PAGELENGTH_INFO_EXCPT);
5921 
5922 	page->mode_page.code = MODEPAGE_INFO_EXCPT;
5923 	page->mode_page.length = PAGELENGTH_INFO_EXCPT;
5924 
5925 	/* Indicate that this is page is saveable */
5926 	page->mode_page.ps = 1;
5927 
5928 	/*
5929 	 * We will return the same data for default, current and saved page.
5930 	 * The only changeable bit is dexcpt and that bit is required
5931 	 * by the ATA specification to be preserved across power cycles.
5932 	 */
5933 	if (pcntrl != 1) {
5934 		page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED);
5935 		page->mrie = MRIE_ONLY_ON_REQUEST;
5936 	}
5937 	else
5938 		page->dexcpt = 1;	/* Only changeable parameter */
5939 
5940 	return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_info_excpt_page));
5941 }
5942 
5943 
5944 static int
5945 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5946 {
5947 	struct mode_acoustic_management *page =
5948 	    (struct mode_acoustic_management *)buf;
5949 	sata_id_t *sata_id = &sdinfo->satadrv_id;
5950 
5951 	/*
5952 	 * Most of the fields are set to 0, being not supported and/or disabled
5953 	 */
5954 	bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT);
5955 
5956 	switch (pcntrl) {
5957 	case P_CNTRL_DEFAULT:
5958 		/*  default paramters not supported */
5959 		return (0);
5960 
5961 	case P_CNTRL_CURRENT:
5962 	case P_CNTRL_SAVED:
5963 		/* Saved and current are supported and are identical */
5964 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
5965 		page->mode_page.length =
5966 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
5967 		page->mode_page.ps = 1;
5968 
5969 		/* Word 83 indicates if feature is supported */
5970 		/* If feature is not supported */
5971 		if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) {
5972 			page->acoustic_manag_enable =
5973 			    ACOUSTIC_DISABLED;
5974 		} else {
5975 			page->acoustic_manag_enable =
5976 			    ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT)
5977 			    != 0);
5978 			/* Word 94 inidicates the value */
5979 #ifdef	_LITTLE_ENDIAN
5980 			page->acoustic_manag_level =
5981 			    (uchar_t)sata_id->ai_acoustic;
5982 			page->vendor_recommended_value =
5983 			    sata_id->ai_acoustic >> 8;
5984 #else
5985 			page->acoustic_manag_level =
5986 			    sata_id->ai_acoustic >> 8;
5987 			page->vendor_recommended_value =
5988 			    (uchar_t)sata_id->ai_acoustic;
5989 #endif
5990 		}
5991 		break;
5992 
5993 	case P_CNTRL_CHANGEABLE:
5994 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
5995 		page->mode_page.length =
5996 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
5997 		page->mode_page.ps = 1;
5998 
5999 		/* Word 83 indicates if the feature is supported */
6000 		if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) {
6001 			page->acoustic_manag_enable =
6002 			    ACOUSTIC_ENABLED;
6003 			page->acoustic_manag_level = 0xff;
6004 		}
6005 		break;
6006 	}
6007 	return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6008 	    sizeof (struct mode_page));
6009 }
6010 
6011 
6012 /*
6013  * Build Mode sense power condition page
6014  * NOT IMPLEMENTED.
6015  */
6016 static int
6017 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6018 {
6019 #ifndef __lock_lint
6020 	_NOTE(ARGUNUSED(sdinfo))
6021 	_NOTE(ARGUNUSED(pcntrl))
6022 	_NOTE(ARGUNUSED(buf))
6023 #endif
6024 	return (0);
6025 }
6026 
6027 
6028 /*
6029  * Process mode select caching page 8 (scsi3 format only).
6030  * Read Ahead (same as read cache) and Write Cache may be turned on and off
6031  * if these features are supported by the device. If these features are not
6032  * supported, quietly ignore them.
6033  * This function fails only if the SET FEATURE command sent to
6034  * the device fails. The page format is not varified, assuming that the
6035  * target driver operates correctly - if parameters length is too short,
6036  * we just drop the page.
6037  * Two command may be sent if both Read Cache/Read Ahead and Write Cache
6038  * setting have to be changed.
6039  * SET FEATURE command is executed synchronously, i.e. we wait here until
6040  * it is completed, regardless of the scsi pkt directives.
6041  *
6042  * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e.
6043  * changing DRA will change RCD.
6044  *
6045  * More than one SATA command may be executed to perform operations specified
6046  * by mode select pages. The first error terminates further execution.
6047  * Operations performed successully are not backed-up in such case.
6048  *
6049  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
6050  * If operation resulted in changing device setup, dmod flag should be set to
6051  * one (1). If parameters were not changed, dmod flag should be set to 0.
6052  * Upon return, if operation required sending command to the device, the rval
6053  * should be set to the value returned by sata_hba_start. If operation
6054  * did not require device access, rval should be set to TRAN_ACCEPT.
6055  * The pagelen should be set to the length of the page.
6056  *
6057  * This function has to be called with a port mutex held.
6058  *
6059  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6060  */
6061 int
6062 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page,
6063     int parmlen, int *pagelen, int *rval, int *dmod)
6064 {
6065 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6066 	sata_drive_info_t *sdinfo;
6067 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6068 	sata_id_t *sata_id;
6069 	struct scsi_extended_sense *sense;
6070 	int wce, dra;	/* Current settings */
6071 
6072 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6073 	    &spx->txlt_sata_pkt->satapkt_device);
6074 	sata_id = &sdinfo->satadrv_id;
6075 	*dmod = 0;
6076 
6077 	/* Verify parameters length. If too short, drop it */
6078 	if (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
6079 	    sizeof (struct mode_page) < parmlen) {
6080 		*scsipkt->pkt_scbp = STATUS_CHECK;
6081 		sense = sata_arq_sense(spx);
6082 		sense->es_key = KEY_ILLEGAL_REQUEST;
6083 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6084 		*pagelen = parmlen;
6085 		*rval = TRAN_ACCEPT;
6086 		return (SATA_FAILURE);
6087 	}
6088 
6089 	*pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page);
6090 
6091 	/*
6092 	 * We can manipulate only write cache and read ahead
6093 	 * (read cache) setting.
6094 	 */
6095 	if (!(sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) &&
6096 	    !(sata_id->ai_cmdset82 & SATA_WRITE_CACHE)) {
6097 		/*
6098 		 * None of the features is supported - ignore
6099 		 */
6100 		*rval = TRAN_ACCEPT;
6101 		return (SATA_SUCCESS);
6102 	}
6103 
6104 	/* Current setting of Read Ahead (and Read Cache) */
6105 	if (sata_id->ai_features85 & SATA_LOOK_AHEAD)
6106 		dra = 0;	/* 0 == not disabled */
6107 	else
6108 		dra = 1;
6109 	/* Current setting of Write Cache */
6110 	if (sata_id->ai_features85 & SATA_WRITE_CACHE)
6111 		wce = 1;
6112 	else
6113 		wce = 0;
6114 
6115 	if (page->dra == dra && page->wce == wce && page->rcd == dra) {
6116 		/* nothing to do */
6117 		*rval = TRAN_ACCEPT;
6118 		return (SATA_SUCCESS);
6119 	}
6120 	/*
6121 	 * Need to flip some setting
6122 	 * Set-up Internal SET FEATURES command(s)
6123 	 */
6124 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6125 	scmd->satacmd_addr_type = 0;
6126 	scmd->satacmd_device_reg = 0;
6127 	scmd->satacmd_status_reg = 0;
6128 	scmd->satacmd_error_reg = 0;
6129 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
6130 	if (page->dra != dra || page->rcd != dra) {
6131 		/* Need to flip read ahead setting */
6132 		if (dra == 0)
6133 			/* Disable read ahead / read cache */
6134 			scmd->satacmd_features_reg =
6135 			    SATAC_SF_DISABLE_READ_AHEAD;
6136 		else
6137 			/* Enable read ahead  / read cache */
6138 			scmd->satacmd_features_reg =
6139 			    SATAC_SF_ENABLE_READ_AHEAD;
6140 
6141 		/* Transfer command to HBA */
6142 		if (sata_hba_start(spx, rval) != 0)
6143 			/*
6144 			 * Pkt not accepted for execution.
6145 			 */
6146 			return (SATA_FAILURE);
6147 
6148 		*dmod = 1;
6149 
6150 		/* Now process return */
6151 		if (spx->txlt_sata_pkt->satapkt_reason !=
6152 		    SATA_PKT_COMPLETED) {
6153 			goto failure;	/* Terminate */
6154 		}
6155 	}
6156 
6157 	/* Note that the packet is not removed, so it could be re-used */
6158 	if (page->wce != wce) {
6159 		/* Need to flip Write Cache setting */
6160 		if (page->wce == 1)
6161 			/* Enable write cache */
6162 			scmd->satacmd_features_reg =
6163 			    SATAC_SF_ENABLE_WRITE_CACHE;
6164 		else
6165 			/* Disable write cache */
6166 			scmd->satacmd_features_reg =
6167 			    SATAC_SF_DISABLE_WRITE_CACHE;
6168 
6169 		/* Transfer command to HBA */
6170 		if (sata_hba_start(spx, rval) != 0)
6171 			/*
6172 			 * Pkt not accepted for execution.
6173 			 */
6174 			return (SATA_FAILURE);
6175 
6176 		*dmod = 1;
6177 
6178 		/* Now process return */
6179 		if (spx->txlt_sata_pkt->satapkt_reason !=
6180 		    SATA_PKT_COMPLETED) {
6181 			goto failure;
6182 		}
6183 	}
6184 	return (SATA_SUCCESS);
6185 
6186 failure:
6187 	sata_xlate_errors(spx);
6188 
6189 	return (SATA_FAILURE);
6190 }
6191 
6192 /*
6193  * Process mode select informational exceptions control page 0x1c
6194  *
6195  * The only changeable bit is dexcpt (disable exceptions).
6196  * MRIE (method of reporting informational exceptions) must be
6197  * "only on request".
6198  *
6199  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
6200  * If operation resulted in changing device setup, dmod flag should be set to
6201  * one (1). If parameters were not changed, dmod flag should be set to 0.
6202  * Upon return, if operation required sending command to the device, the rval
6203  * should be set to the value returned by sata_hba_start. If operation
6204  * did not require device access, rval should be set to TRAN_ACCEPT.
6205  * The pagelen should be set to the length of the page.
6206  *
6207  * This function has to be called with a port mutex held.
6208  *
6209  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6210  */
6211 static	int
6212 sata_mode_select_page_1c(
6213 	sata_pkt_txlate_t *spx,
6214 	struct mode_info_excpt_page *page,
6215 	int parmlen,
6216 	int *pagelen,
6217 	int *rval,
6218 	int *dmod)
6219 {
6220 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6221 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6222 	sata_drive_info_t *sdinfo;
6223 	sata_id_t *sata_id;
6224 	struct scsi_extended_sense *sense;
6225 
6226 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6227 	    &spx->txlt_sata_pkt->satapkt_device);
6228 	sata_id = &sdinfo->satadrv_id;
6229 
6230 	*dmod = 0;
6231 
6232 	/* Verify parameters length. If too short, drop it */
6233 	if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) < parmlen) ||
6234 	    page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) {
6235 		*scsipkt->pkt_scbp = STATUS_CHECK;
6236 		sense = sata_arq_sense(spx);
6237 		sense->es_key = KEY_ILLEGAL_REQUEST;
6238 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6239 		*pagelen = parmlen;
6240 		*rval = TRAN_ACCEPT;
6241 		return (SATA_FAILURE);
6242 	}
6243 
6244 	*pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page);
6245 
6246 	if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
6247 		*scsipkt->pkt_scbp = STATUS_CHECK;
6248 		sense = sata_arq_sense(spx);
6249 		sense->es_key = KEY_ILLEGAL_REQUEST;
6250 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6251 		*pagelen = parmlen;
6252 		*rval = TRAN_ACCEPT;
6253 		return (SATA_FAILURE);
6254 	}
6255 
6256 	/* If already in the state requested, we are done */
6257 	if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
6258 		/* nothing to do */
6259 		*rval = TRAN_ACCEPT;
6260 		return (SATA_SUCCESS);
6261 	}
6262 
6263 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6264 
6265 	/* Build SMART_ENABLE or SMART_DISABLE command */
6266 	scmd->satacmd_addr_type = 0;		/* N/A */
6267 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
6268 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
6269 	scmd->satacmd_features_reg = page->dexcpt ?
6270 	    SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS;
6271 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
6272 	scmd->satacmd_cmd_reg = SATAC_SMART;
6273 
6274 	/* Transfer command to HBA */
6275 	if (sata_hba_start(spx, rval) != 0)
6276 		/*
6277 		 * Pkt not accepted for execution.
6278 		 */
6279 		return (SATA_FAILURE);
6280 
6281 	*dmod = 1;	/* At least may have been modified */
6282 
6283 	/* Now process return */
6284 	if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED)
6285 		return (SATA_SUCCESS);
6286 
6287 	/* Packet did not complete successfully */
6288 	sata_xlate_errors(spx);
6289 
6290 	return (SATA_FAILURE);
6291 }
6292 
6293 int
6294 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct
6295     mode_acoustic_management *page, int parmlen, int *pagelen,
6296     int *rval, int *dmod)
6297 {
6298 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6299 	sata_drive_info_t *sdinfo;
6300 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6301 	sata_id_t *sata_id;
6302 	struct scsi_extended_sense *sense;
6303 
6304 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6305 	    &spx->txlt_sata_pkt->satapkt_device);
6306 	sata_id = &sdinfo->satadrv_id;
6307 	*dmod = 0;
6308 
6309 	/* If parmlen is too short or the feature is not supported, drop it */
6310 	if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6311 	    sizeof (struct mode_page)) < parmlen) ||
6312 	    (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) {
6313 		*scsipkt->pkt_scbp = STATUS_CHECK;
6314 		sense = sata_arq_sense(spx);
6315 		sense->es_key = KEY_ILLEGAL_REQUEST;
6316 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6317 		*pagelen = parmlen;
6318 		*rval = TRAN_ACCEPT;
6319 		return (SATA_FAILURE);
6320 	}
6321 
6322 	*pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6323 	    sizeof (struct mode_page);
6324 
6325 	/*
6326 	 * We can enable and disable acoustice management and
6327 	 * set the acoustic management level.
6328 	 */
6329 
6330 	/*
6331 	 * Set-up Internal SET FEATURES command(s)
6332 	 */
6333 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6334 	scmd->satacmd_addr_type = 0;
6335 	scmd->satacmd_device_reg = 0;
6336 	scmd->satacmd_status_reg = 0;
6337 	scmd->satacmd_error_reg = 0;
6338 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
6339 	if (page->acoustic_manag_enable) {
6340 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC;
6341 		scmd->satacmd_sec_count_lsb = page->acoustic_manag_level;
6342 	} else {	/* disabling acoustic management */
6343 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC;
6344 	}
6345 
6346 	/* Transfer command to HBA */
6347 	if (sata_hba_start(spx, rval) != 0)
6348 		/*
6349 		 * Pkt not accepted for execution.
6350 		 */
6351 		return (SATA_FAILURE);
6352 
6353 	/* Now process return */
6354 	if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) {
6355 		sata_xlate_errors(spx);
6356 		return (SATA_FAILURE);
6357 	}
6358 
6359 	*dmod = 1;
6360 
6361 	return (SATA_SUCCESS);
6362 }
6363 
6364 
6365 
6366 
6367 /*
6368  * sata_build_lsense_page0() is used to create the
6369  * SCSI LOG SENSE page 0 (supported log pages)
6370  *
6371  * Currently supported pages are 0, 0x10, 0x2f and 0x30
6372  * (supported log pages, self-test results, informational exceptions
6373  *  and Sun vendor specific ATA SMART data).
6374  *
6375  * Takes a sata_drive_info t * and the address of a buffer
6376  * in which to create the page information.
6377  *
6378  * Returns the number of bytes valid in the buffer.
6379  */
6380 static	int
6381 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf)
6382 {
6383 	struct log_parameter *lpp = (struct log_parameter *)buf;
6384 	uint8_t *page_ptr = (uint8_t *)lpp->param_values;
6385 	int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */
6386 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6387 
6388 	lpp->param_code[0] = 0;
6389 	lpp->param_code[1] = 0;
6390 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
6391 	*page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES;
6392 
6393 	if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
6394 		if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) {
6395 			*page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS;
6396 			++num_pages_supported;
6397 		}
6398 		*page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS;
6399 		++num_pages_supported;
6400 		*page_ptr++ = PAGE_CODE_SMART_READ_DATA;
6401 		++num_pages_supported;
6402 	}
6403 
6404 	lpp->param_len = num_pages_supported;
6405 
6406 	return ((&lpp->param_values[0] - (uint8_t *)lpp) +
6407 	    num_pages_supported);
6408 }
6409 
6410 /*
6411  * sata_build_lsense_page_10() is used to create the
6412  * SCSI LOG SENSE page 0x10 (self-test results)
6413  *
6414  * Takes a sata_drive_info t * and the address of a buffer
6415  * in which to create the page information as well as a sata_hba_inst_t *.
6416  *
6417  * Returns the number of bytes valid in the buffer.
6418  */
6419 static	int
6420 sata_build_lsense_page_10(
6421 	sata_drive_info_t *sdinfo,
6422 	uint8_t *buf,
6423 	sata_hba_inst_t *sata_hba_inst)
6424 {
6425 	struct log_parameter *lpp = (struct log_parameter *)buf;
6426 	int rval;
6427 
6428 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6429 		struct smart_ext_selftest_log *ext_selftest_log;
6430 
6431 		ext_selftest_log = kmem_zalloc(
6432 		    sizeof (struct smart_ext_selftest_log), KM_SLEEP);
6433 
6434 		rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo,
6435 		    ext_selftest_log, 0);
6436 		if (rval == 0) {
6437 			int index, start_index;
6438 			struct smart_ext_selftest_log_entry *entry;
6439 			static const struct smart_ext_selftest_log_entry empty =
6440 			    {0};
6441 			uint16_t block_num;
6442 			int count;
6443 			boolean_t only_one_block = B_FALSE;
6444 
6445 			index = ext_selftest_log->
6446 			    smart_ext_selftest_log_index[0];
6447 			index |= ext_selftest_log->
6448 			    smart_ext_selftest_log_index[1] << 8;
6449 			if (index == 0)
6450 				goto out;
6451 
6452 			--index;	/* Correct for 0 origin */
6453 			start_index = index;	/* remember where we started */
6454 			block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6455 			if (block_num != 0) {
6456 				rval = sata_ext_smart_selftest_read_log(
6457 				    sata_hba_inst, sdinfo, ext_selftest_log,
6458 				    block_num);
6459 				if (rval != 0)
6460 					goto out;
6461 			}
6462 			index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6463 			entry =
6464 			    &ext_selftest_log->
6465 			    smart_ext_selftest_log_entries[index];
6466 
6467 			for (count = 1;
6468 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
6469 			    ++count) {
6470 				uint8_t status;
6471 				uint8_t code;
6472 				uint8_t sense_key;
6473 				uint8_t add_sense_code;
6474 				uint8_t add_sense_code_qual;
6475 
6476 				/* If this is an unused entry, we are done */
6477 				if (bcmp(entry, &empty, sizeof (empty)) == 0) {
6478 					/* Broken firmware on some disks */
6479 					if (index + 1 ==
6480 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK) {
6481 						--entry;
6482 						--index;
6483 						if (bcmp(entry, &empty,
6484 						    sizeof (empty)) == 0)
6485 							goto out;
6486 					} else
6487 						goto out;
6488 				}
6489 
6490 				if (only_one_block &&
6491 				    start_index == index)
6492 					goto out;
6493 
6494 				lpp->param_code[0] = 0;
6495 				lpp->param_code[1] = count;
6496 				lpp->param_ctrl_flags =
6497 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
6498 				lpp->param_len =
6499 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
6500 
6501 				status = entry->smart_ext_selftest_log_status;
6502 				status >>= 4;
6503 				switch (status) {
6504 				case 0:
6505 				default:
6506 					sense_key = KEY_NO_SENSE;
6507 					add_sense_code =
6508 					    SD_SCSI_ASC_NO_ADD_SENSE;
6509 					add_sense_code_qual = 0;
6510 					break;
6511 				case 1:
6512 					sense_key = KEY_ABORTED_COMMAND;
6513 					add_sense_code =
6514 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6515 					add_sense_code_qual = SCSI_COMPONENT_81;
6516 					break;
6517 				case 2:
6518 					sense_key = KEY_ABORTED_COMMAND;
6519 					add_sense_code =
6520 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6521 					add_sense_code_qual = SCSI_COMPONENT_82;
6522 					break;
6523 				case 3:
6524 					sense_key = KEY_ABORTED_COMMAND;
6525 					add_sense_code =
6526 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6527 					add_sense_code_qual = SCSI_COMPONENT_83;
6528 					break;
6529 				case 4:
6530 					sense_key = KEY_HARDWARE_ERROR;
6531 					add_sense_code =
6532 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6533 					add_sense_code_qual = SCSI_COMPONENT_84;
6534 					break;
6535 				case 5:
6536 					sense_key = KEY_HARDWARE_ERROR;
6537 					add_sense_code =
6538 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6539 					add_sense_code_qual = SCSI_COMPONENT_85;
6540 					break;
6541 				case 6:
6542 					sense_key = KEY_HARDWARE_ERROR;
6543 					add_sense_code =
6544 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6545 					add_sense_code_qual = SCSI_COMPONENT_86;
6546 					break;
6547 				case 7:
6548 					sense_key = KEY_MEDIUM_ERROR;
6549 					add_sense_code =
6550 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6551 					add_sense_code_qual = SCSI_COMPONENT_87;
6552 					break;
6553 				case 8:
6554 					sense_key = KEY_HARDWARE_ERROR;
6555 					add_sense_code =
6556 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6557 					add_sense_code_qual = SCSI_COMPONENT_88;
6558 					break;
6559 				}
6560 				code = 0;	/* unspecified */
6561 				status |= (code << 4);
6562 				lpp->param_values[0] = status;
6563 				lpp->param_values[1] = 0; /* unspecified */
6564 				lpp->param_values[2] = entry->
6565 				    smart_ext_selftest_log_timestamp[1];
6566 				lpp->param_values[3] = entry->
6567 				    smart_ext_selftest_log_timestamp[0];
6568 				if (status != 0) {
6569 					lpp->param_values[4] = 0;
6570 					lpp->param_values[5] = 0;
6571 					lpp->param_values[6] = entry->
6572 					    smart_ext_selftest_log_failing_lba
6573 					    [5];
6574 					lpp->param_values[7] = entry->
6575 					    smart_ext_selftest_log_failing_lba
6576 					    [4];
6577 					lpp->param_values[8] = entry->
6578 					    smart_ext_selftest_log_failing_lba
6579 					    [3];
6580 					lpp->param_values[9] = entry->
6581 					    smart_ext_selftest_log_failing_lba
6582 					    [2];
6583 					lpp->param_values[10] = entry->
6584 					    smart_ext_selftest_log_failing_lba
6585 					    [1];
6586 					lpp->param_values[11] = entry->
6587 					    smart_ext_selftest_log_failing_lba
6588 					    [0];
6589 				} else {	/* No bad block address */
6590 					lpp->param_values[4] = 0xff;
6591 					lpp->param_values[5] = 0xff;
6592 					lpp->param_values[6] = 0xff;
6593 					lpp->param_values[7] = 0xff;
6594 					lpp->param_values[8] = 0xff;
6595 					lpp->param_values[9] = 0xff;
6596 					lpp->param_values[10] = 0xff;
6597 					lpp->param_values[11] = 0xff;
6598 				}
6599 
6600 				lpp->param_values[12] = sense_key;
6601 				lpp->param_values[13] = add_sense_code;
6602 				lpp->param_values[14] = add_sense_code_qual;
6603 				lpp->param_values[15] = 0; /* undefined */
6604 
6605 				lpp = (struct log_parameter *)
6606 				    (((uint8_t *)lpp) +
6607 				    SCSI_LOG_PARAM_HDR_LEN +
6608 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
6609 
6610 				--index;	/* Back up to previous entry */
6611 				if (index < 0) {
6612 					if (block_num > 0) {
6613 						--block_num;
6614 					} else {
6615 						struct read_log_ext_directory
6616 						    logdir;
6617 
6618 						rval =
6619 						    sata_read_log_ext_directory(
6620 						    sata_hba_inst, sdinfo,
6621 						    &logdir);
6622 						if (rval == -1)
6623 							goto out;
6624 						if ((logdir.read_log_ext_vers
6625 						    [0] == 0) &&
6626 						    (logdir.read_log_ext_vers
6627 						    [1] == 0))
6628 							goto out;
6629 						block_num =
6630 						    logdir.read_log_ext_nblks
6631 						    [EXT_SMART_SELFTEST_LOG_PAGE
6632 						    - 1][0];
6633 						block_num |= logdir.
6634 						    read_log_ext_nblks
6635 						    [EXT_SMART_SELFTEST_LOG_PAGE
6636 						    - 1][1] << 8;
6637 						--block_num;
6638 						only_one_block =
6639 						    (block_num == 0);
6640 					}
6641 					rval = sata_ext_smart_selftest_read_log(
6642 					    sata_hba_inst, sdinfo,
6643 					    ext_selftest_log, block_num);
6644 					if (rval != 0)
6645 						goto out;
6646 
6647 					index =
6648 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK -
6649 					    1;
6650 				}
6651 				index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6652 				entry = &ext_selftest_log->
6653 				    smart_ext_selftest_log_entries[index];
6654 			}
6655 		}
6656 out:
6657 		kmem_free(ext_selftest_log,
6658 		    sizeof (struct smart_ext_selftest_log));
6659 	} else {
6660 		struct smart_selftest_log *selftest_log;
6661 
6662 		selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log),
6663 		    KM_SLEEP);
6664 
6665 		rval = sata_smart_selftest_log(sata_hba_inst, sdinfo,
6666 		    selftest_log);
6667 
6668 		if (rval == 0) {
6669 			int index;
6670 			int count;
6671 			struct smart_selftest_log_entry *entry;
6672 			static const struct smart_selftest_log_entry empty =
6673 			    { 0 };
6674 
6675 			index = selftest_log->smart_selftest_log_index;
6676 			if (index == 0)
6677 				goto done;
6678 			--index;	/* Correct for 0 origin */
6679 			entry = &selftest_log->
6680 			    smart_selftest_log_entries[index];
6681 			for (count = 1;
6682 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
6683 			    ++count) {
6684 				uint8_t status;
6685 				uint8_t code;
6686 				uint8_t sense_key;
6687 				uint8_t add_sense_code;
6688 				uint8_t add_sense_code_qual;
6689 
6690 				if (bcmp(entry, &empty, sizeof (empty)) == 0)
6691 					goto done;
6692 
6693 				lpp->param_code[0] = 0;
6694 				lpp->param_code[1] = count;
6695 				lpp->param_ctrl_flags =
6696 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
6697 				lpp->param_len =
6698 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
6699 
6700 				status = entry->smart_selftest_log_status;
6701 				status >>= 4;
6702 				switch (status) {
6703 				case 0:
6704 				default:
6705 					sense_key = KEY_NO_SENSE;
6706 					add_sense_code =
6707 					    SD_SCSI_ASC_NO_ADD_SENSE;
6708 					break;
6709 				case 1:
6710 					sense_key = KEY_ABORTED_COMMAND;
6711 					add_sense_code =
6712 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6713 					add_sense_code_qual = SCSI_COMPONENT_81;
6714 					break;
6715 				case 2:
6716 					sense_key = KEY_ABORTED_COMMAND;
6717 					add_sense_code =
6718 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6719 					add_sense_code_qual = SCSI_COMPONENT_82;
6720 					break;
6721 				case 3:
6722 					sense_key = KEY_ABORTED_COMMAND;
6723 					add_sense_code =
6724 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6725 					add_sense_code_qual = SCSI_COMPONENT_83;
6726 					break;
6727 				case 4:
6728 					sense_key = KEY_HARDWARE_ERROR;
6729 					add_sense_code =
6730 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6731 					add_sense_code_qual = SCSI_COMPONENT_84;
6732 					break;
6733 				case 5:
6734 					sense_key = KEY_HARDWARE_ERROR;
6735 					add_sense_code =
6736 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6737 					add_sense_code_qual = SCSI_COMPONENT_85;
6738 					break;
6739 				case 6:
6740 					sense_key = KEY_HARDWARE_ERROR;
6741 					add_sense_code =
6742 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6743 					add_sense_code_qual = SCSI_COMPONENT_86;
6744 					break;
6745 				case 7:
6746 					sense_key = KEY_MEDIUM_ERROR;
6747 					add_sense_code =
6748 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6749 					add_sense_code_qual = SCSI_COMPONENT_87;
6750 					break;
6751 				case 8:
6752 					sense_key = KEY_HARDWARE_ERROR;
6753 					add_sense_code =
6754 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6755 					add_sense_code_qual = SCSI_COMPONENT_88;
6756 					break;
6757 				}
6758 				code = 0;	/* unspecified */
6759 				status |= (code << 4);
6760 				lpp->param_values[0] = status;
6761 				lpp->param_values[1] = 0; /* unspecified */
6762 				lpp->param_values[2] = entry->
6763 				    smart_selftest_log_timestamp[1];
6764 				lpp->param_values[3] = entry->
6765 				    smart_selftest_log_timestamp[0];
6766 				if (status != 0) {
6767 					lpp->param_values[4] = 0;
6768 					lpp->param_values[5] = 0;
6769 					lpp->param_values[6] = 0;
6770 					lpp->param_values[7] = 0;
6771 					lpp->param_values[8] = entry->
6772 					    smart_selftest_log_failing_lba[3];
6773 					lpp->param_values[9] = entry->
6774 					    smart_selftest_log_failing_lba[2];
6775 					lpp->param_values[10] = entry->
6776 					    smart_selftest_log_failing_lba[1];
6777 					lpp->param_values[11] = entry->
6778 					    smart_selftest_log_failing_lba[0];
6779 				} else {	/* No block address */
6780 					lpp->param_values[4] = 0xff;
6781 					lpp->param_values[5] = 0xff;
6782 					lpp->param_values[6] = 0xff;
6783 					lpp->param_values[7] = 0xff;
6784 					lpp->param_values[8] = 0xff;
6785 					lpp->param_values[9] = 0xff;
6786 					lpp->param_values[10] = 0xff;
6787 					lpp->param_values[11] = 0xff;
6788 				}
6789 				lpp->param_values[12] = sense_key;
6790 				lpp->param_values[13] = add_sense_code;
6791 				lpp->param_values[14] = add_sense_code_qual;
6792 				lpp->param_values[15] = 0; /* undefined */
6793 
6794 				lpp = (struct log_parameter *)
6795 				    (((uint8_t *)lpp) +
6796 				    SCSI_LOG_PARAM_HDR_LEN +
6797 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
6798 				--index;	/* back up to previous entry */
6799 				if (index < 0) {
6800 					index =
6801 					    NUM_SMART_SELFTEST_LOG_ENTRIES - 1;
6802 				}
6803 				entry = &selftest_log->
6804 				    smart_selftest_log_entries[index];
6805 			}
6806 		}
6807 done:
6808 		kmem_free(selftest_log, sizeof (struct smart_selftest_log));
6809 	}
6810 
6811 	return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) *
6812 	    SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS);
6813 }
6814 
6815 /*
6816  * sata_build_lsense_page_2f() is used to create the
6817  * SCSI LOG SENSE page 0x10 (informational exceptions)
6818  *
6819  * Takes a sata_drive_info t * and the address of a buffer
6820  * in which to create the page information as well as a sata_hba_inst_t *.
6821  *
6822  * Returns the number of bytes valid in the buffer.
6823  */
6824 static	int
6825 sata_build_lsense_page_2f(
6826 	sata_drive_info_t *sdinfo,
6827 	uint8_t *buf,
6828 	sata_hba_inst_t *sata_hba_inst)
6829 {
6830 	struct log_parameter *lpp = (struct log_parameter *)buf;
6831 	int rval;
6832 	uint8_t *smart_data;
6833 	uint8_t temp;
6834 	sata_id_t *sata_id;
6835 #define	SMART_NO_TEMP	0xff
6836 
6837 	lpp->param_code[0] = 0;
6838 	lpp->param_code[1] = 0;
6839 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
6840 
6841 	/* Now get the SMART status w.r.t. threshold exceeded */
6842 	rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo);
6843 	switch (rval) {
6844 	case 1:
6845 		lpp->param_values[0] = SCSI_PREDICTED_FAILURE;
6846 		lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE;
6847 		break;
6848 	case 0:
6849 	case -1:	/* failed to get data */
6850 		lpp->param_values[0] = 0;	/* No failure predicted */
6851 		lpp->param_values[1] = 0;
6852 		break;
6853 #if defined(SATA_DEBUG)
6854 	default:
6855 		cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value");
6856 		/* NOTREACHED */
6857 #endif
6858 	}
6859 
6860 	sata_id = &sdinfo->satadrv_id;
6861 	if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP))
6862 		temp = SMART_NO_TEMP;
6863 	else {
6864 		/* Now get the temperature */
6865 		smart_data = kmem_zalloc(512, KM_SLEEP);
6866 		rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data,
6867 		    SCT_STATUS_LOG_PAGE, 1);
6868 		if (rval == -1)
6869 			temp = SMART_NO_TEMP;
6870 		else {
6871 			temp = smart_data[200];
6872 			if (temp & 0x80) {
6873 				if (temp & 0x7f)
6874 					temp = 0;
6875 				else
6876 					temp = SMART_NO_TEMP;
6877 			}
6878 		}
6879 		kmem_free(smart_data, 512);
6880 	}
6881 
6882 	lpp->param_values[2] = temp;	/* most recent temperature */
6883 	lpp->param_values[3] = 0;	/* required vendor specific byte */
6884 
6885 	lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN;
6886 
6887 
6888 	return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN);
6889 }
6890 
6891 /*
6892  * sata_build_lsense_page_30() is used to create the
6893  * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data).
6894  *
6895  * Takes a sata_drive_info t * and the address of a buffer
6896  * in which to create the page information as well as a sata_hba_inst_t *.
6897  *
6898  * Returns the number of bytes valid in the buffer.
6899  */
6900 static int
6901 sata_build_lsense_page_30(
6902 	sata_drive_info_t *sdinfo,
6903 	uint8_t *buf,
6904 	sata_hba_inst_t *sata_hba_inst)
6905 {
6906 	struct smart_data *smart_data = (struct smart_data *)buf;
6907 	int rval;
6908 
6909 	/* Now do the SMART READ DATA */
6910 	rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data);
6911 	if (rval == -1)
6912 		return (0);
6913 
6914 	return (sizeof (struct smart_data));
6915 }
6916 
6917 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */
6918 
6919 /*
6920  * Start command for ATAPI device.
6921  * This function processes scsi_pkt requests.
6922  * Only CD/DVD devices are supported.
6923  * Most commands are packet without any translation into Packet Command.
6924  * Some may be trapped and executed as SATA commands (not clear which one).
6925  *
6926  * Returns TRAN_ACCEPT if command is accepted for execution (or completed
6927  * execution).
6928  * Returns other TRAN_XXXX codes if command is not accepted or completed
6929  * (see return values for sata_hba_start()).
6930  *
6931  * Note:
6932  * Inquiry cdb format differs between transport version 2 and 3.
6933  * However, the transport version 3 devices that were checked did not adhere
6934  * to the specification (ignored MSB of the allocation length). Therefore,
6935  * the transport version is not checked, but Inquiry allocation length is
6936  * truncated to 255 bytes if the original allocation length set-up by the
6937  * target driver is greater than 255 bytes.
6938  */
6939 static int
6940 sata_txlt_atapi(sata_pkt_txlate_t *spx)
6941 {
6942 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6943 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6944 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
6945 	sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx);
6946 	sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba,
6947 	    &spx->txlt_sata_pkt->satapkt_device);
6948 	int cport = SATA_TXLT_CPORT(spx);
6949 	int cdblen;
6950 	int rval;
6951 	int synch;
6952 	union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp;
6953 
6954 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
6955 
6956 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
6957 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
6958 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6959 		return (rval);
6960 	}
6961 
6962 	/*
6963 	 * ATAPI device executes some ATA commands in addition to MMC command
6964 	 * set. These ATA commands may be executed by the regular SATA
6965 	 * translation functions. None needs to be captured now.
6966 	 * Other commands belong to MMC command set and are delivered
6967 	 * to ATAPI device via Packet Command.
6968 	 */
6969 
6970 	/* Check the size of cdb */
6971 	cdblen = scsi_cdb_size[GETGROUP(cdbp)];
6972 	if (cdblen > sdinfo->satadrv_atapi_cdb_len) {
6973 		sata_log(NULL, CE_WARN,
6974 		    "sata: invalid ATAPI cdb length %d",
6975 		    scsipkt->pkt_cdblen);
6976 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6977 		return (TRAN_BADPKT);
6978 	}
6979 
6980 	SATAATAPITRACE(spx, cdblen);
6981 
6982 	/*
6983 	 * For non-read/write commands we need to
6984 	 * map buffer
6985 	 */
6986 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6987 	case SCMD_READ:
6988 	case SCMD_READ_G1:
6989 	case SCMD_READ_G5:
6990 	case SCMD_READ_G4:
6991 	case SCMD_WRITE:
6992 	case SCMD_WRITE_G1:
6993 	case SCMD_WRITE_G5:
6994 	case SCMD_WRITE_G4:
6995 		break;
6996 	default:
6997 		if (bp != NULL) {
6998 			if (bp->b_flags & (B_PHYS | B_PAGEIO))
6999 				bp_mapin(bp);
7000 		}
7001 		break;
7002 	}
7003 	/*
7004 	 * scmd->satacmd_flags.sata_data_direction default -
7005 	 * SATA_DIR_NODATA_XFER - is set by
7006 	 * sata_txlt_generic_pkt_info().
7007 	 */
7008 	if (scmd->satacmd_bp) {
7009 		if (scmd->satacmd_bp->b_flags & B_READ) {
7010 			scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7011 		} else {
7012 			scmd->satacmd_flags.sata_data_direction =
7013 			    SATA_DIR_WRITE;
7014 		}
7015 	}
7016 
7017 	/*
7018 	 * Set up ATAPI packet command.
7019 	 */
7020 
7021 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7022 
7023 	/* Copy cdb into sata_cmd */
7024 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7025 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7026 	bcopy(cdbp, scmd->satacmd_acdb, cdblen);
7027 
7028 	/* See note in the command header */
7029 	if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) {
7030 		if (scmd->satacmd_acdb[3] != 0)
7031 			scmd->satacmd_acdb[4] = 255;
7032 	}
7033 
7034 #ifdef SATA_DEBUG
7035 	if (sata_debug_flags & SATA_DBG_ATAPI) {
7036 		uint8_t *p = scmd->satacmd_acdb;
7037 		char buf[3 * SATA_ATAPI_MAX_CDB_LEN];
7038 
7039 		(void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN,
7040 		    "%02x %02x %02x %02x %02x %02x %02x %02x "
7041 		    "%2x %02x %02x %02x %02x %02x %02x %02x",
7042 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7043 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7044 		buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0';
7045 		cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf);
7046 	}
7047 #endif
7048 
7049 	/*
7050 	 * Preset request sense data to NO SENSE.
7051 	 * If there is no way to get error information via Request Sense,
7052 	 * the packet request sense data would not have to be modified by HBA,
7053 	 * but it could be returned as is.
7054 	 */
7055 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
7056 	sata_fixed_sense_data_preset(
7057 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7058 
7059 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
7060 		/* Need callback function */
7061 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion;
7062 		synch = FALSE;
7063 	} else
7064 		synch = TRUE;
7065 
7066 	/* Transfer command to HBA */
7067 	if (sata_hba_start(spx, &rval) != 0) {
7068 		/* Pkt not accepted for execution */
7069 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
7070 		return (rval);
7071 	}
7072 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
7073 	/*
7074 	 * If execution is non-synchronous,
7075 	 * a callback function will handle potential errors, translate
7076 	 * the response and will do a callback to a target driver.
7077 	 * If it was synchronous, use the same framework callback to check
7078 	 * an execution status.
7079 	 */
7080 	if (synch) {
7081 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7082 		    "synchronous execution status %x\n",
7083 		    spx->txlt_sata_pkt->satapkt_reason);
7084 		sata_txlt_atapi_completion(spx->txlt_sata_pkt);
7085 	}
7086 	return (TRAN_ACCEPT);
7087 }
7088 
7089 
7090 /*
7091  * ATAPI Packet command completion.
7092  *
7093  * Failure of the command passed via Packet command are considered device
7094  * error. SATA HBA driver would have to retrieve error data (via Request
7095  * Sense command delivered via error retrieval sata packet) and copy it
7096  * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data.
7097  */
7098 static void
7099 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt)
7100 {
7101 	sata_pkt_txlate_t *spx =
7102 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7103 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7104 	struct scsi_extended_sense *sense;
7105 	struct buf *bp;
7106 	int rval;
7107 
7108 #ifdef SATA_DEBUG
7109 	uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense;
7110 #endif
7111 
7112 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7113 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7114 
7115 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7116 		/* Normal completion */
7117 		if (sata_pkt->satapkt_cmd.satacmd_bp != NULL)
7118 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
7119 		scsipkt->pkt_reason = CMD_CMPLT;
7120 		*scsipkt->pkt_scbp = STATUS_GOOD;
7121 		if (spx->txlt_tmp_buf != NULL) {
7122 			/* Temporary buffer was used */
7123 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7124 			if (bp->b_flags & B_READ) {
7125 				rval = ddi_dma_sync(
7126 				    spx->txlt_buf_dma_handle, 0, 0,
7127 				    DDI_DMA_SYNC_FORCPU);
7128 				ASSERT(rval == DDI_SUCCESS);
7129 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7130 				    bp->b_bcount);
7131 			}
7132 		}
7133 	} else {
7134 		/*
7135 		 * Something went wrong - analyze return
7136 		 */
7137 		*scsipkt->pkt_scbp = STATUS_CHECK;
7138 		sense = sata_arq_sense(spx);
7139 
7140 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7141 			scsipkt->pkt_reason = CMD_INCOMPLETE;
7142 			/*
7143 			 * We may not have ARQ data if there was a double
7144 			 * error. But sense data in sata packet was pre-set
7145 			 * with NO SENSE so it is valid even if HBA could
7146 			 * not retrieve a real sense data.
7147 			 * Just copy this sense data into scsi pkt sense area.
7148 			 */
7149 			bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense,
7150 			    SATA_ATAPI_MIN_RQSENSE_LEN);
7151 #ifdef SATA_DEBUG
7152 			if (sata_debug_flags & SATA_DBG_SCSI_IF) {
7153 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7154 				    "sata_txlt_atapi_completion: %02x\n"
7155 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
7156 				    "          %02x %02x %02x %02x %02x %02x "
7157 				    "          %02x %02x %02x %02x %02x %02x\n",
7158 				    scsipkt->pkt_reason,
7159 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7160 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7161 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7162 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7163 				    rqsp[16], rqsp[17]);
7164 			}
7165 #endif
7166 		} else {
7167 			switch (sata_pkt->satapkt_reason) {
7168 			case SATA_PKT_PORT_ERROR:
7169 				/*
7170 				 * We have no device data.
7171 				 */
7172 				scsipkt->pkt_reason = CMD_INCOMPLETE;
7173 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
7174 				    STATE_GOT_TARGET | STATE_SENT_CMD |
7175 				    STATE_GOT_STATUS);
7176 				sense->es_key = KEY_HARDWARE_ERROR;
7177 				break;
7178 
7179 			case SATA_PKT_TIMEOUT:
7180 				scsipkt->pkt_reason = CMD_TIMEOUT;
7181 				scsipkt->pkt_statistics |=
7182 				    STAT_TIMEOUT | STAT_DEV_RESET;
7183 				/*
7184 				 * Need to check if HARDWARE_ERROR/
7185 				 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more
7186 				 * appropriate.
7187 				 */
7188 				break;
7189 
7190 			case SATA_PKT_ABORTED:
7191 				scsipkt->pkt_reason = CMD_ABORTED;
7192 				scsipkt->pkt_statistics |= STAT_ABORTED;
7193 				/* Should we set key COMMAND_ABPRTED? */
7194 				break;
7195 
7196 			case SATA_PKT_RESET:
7197 				scsipkt->pkt_reason = CMD_RESET;
7198 				scsipkt->pkt_statistics |= STAT_DEV_RESET;
7199 				/*
7200 				 * May be we should set Unit Attention /
7201 				 * Reset. Perhaps the same should be
7202 				 * returned for disks....
7203 				 */
7204 				sense->es_key = KEY_UNIT_ATTENTION;
7205 				sense->es_add_code = SD_SCSI_ASC_RESET;
7206 				break;
7207 
7208 			default:
7209 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7210 				    "sata_txlt_atapi_completion: "
7211 				    "invalid packet completion reason"));
7212 				scsipkt->pkt_reason = CMD_TRAN_ERR;
7213 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
7214 				    STATE_GOT_TARGET | STATE_SENT_CMD |
7215 				    STATE_GOT_STATUS);
7216 				break;
7217 			}
7218 		}
7219 	}
7220 
7221 	SATAATAPITRACE(spx, 0);
7222 
7223 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7224 	    scsipkt->pkt_comp != NULL) {
7225 		/* scsi callback required */
7226 		(*scsipkt->pkt_comp)(scsipkt);
7227 	}
7228 }
7229 
7230 /*
7231  * Set up error retrieval sata command for ATAPI Packet Command error data
7232  * recovery.
7233  *
7234  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
7235  * returns SATA_FAILURE otherwise.
7236  */
7237 
7238 static int
7239 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
7240 {
7241 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
7242 	sata_cmd_t *scmd;
7243 	struct buf *bp;
7244 
7245 	/*
7246 	 * Allocate dma-able buffer error data.
7247 	 * Buffer allocation will take care of buffer alignment and other DMA
7248 	 * attributes.
7249 	 */
7250 	bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN);
7251 	if (bp == NULL) {
7252 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
7253 		    "sata_get_err_retrieval_pkt: "
7254 		    "cannot allocate buffer for error data", NULL);
7255 		return (SATA_FAILURE);
7256 	}
7257 	bp_mapin(bp); /* make data buffer accessible */
7258 
7259 	/* Operation modes are up to the caller */
7260 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7261 
7262 	/* Synchronous mode, no callback - may be changed by the caller */
7263 	spkt->satapkt_comp = NULL;
7264 	spkt->satapkt_time = sata_default_pkt_time;
7265 
7266 	scmd = &spkt->satapkt_cmd;
7267 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7268 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7269 
7270 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7271 
7272 	/*
7273 	 * Set-up acdb. Request Sense CDB (packet command content) is
7274 	 * not in DMA-able buffer. Its handling is HBA-specific (how
7275 	 * it is transfered into packet FIS).
7276 	 */
7277 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7278 	bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN);
7279 	/* Following zeroing of pad bytes may not be necessary */
7280 	bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN],
7281 	    sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN);
7282 
7283 	/*
7284 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
7285 	 * before accessing it. Handle is in usual place in translate struct.
7286 	 */
7287 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
7288 
7289 	/*
7290 	 * Preset request sense data to NO SENSE.
7291 	 * Here it is redundant, only for a symetry with scsi-originated
7292 	 * packets. It should not be used for anything but debugging.
7293 	 */
7294 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
7295 	sata_fixed_sense_data_preset(
7296 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7297 
7298 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7299 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7300 
7301 	return (SATA_SUCCESS);
7302 }
7303 
7304 /*
7305  * Set-up ATAPI packet command.
7306  * Data transfer direction has to be set-up in sata_cmd structure prior to
7307  * calling this function.
7308  *
7309  * Returns void
7310  */
7311 
7312 static void
7313 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo)
7314 {
7315 	scmd->satacmd_addr_type = 0;		/* N/A */
7316 	scmd->satacmd_sec_count_lsb = 0;	/* no tag */
7317 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
7318 	scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ;
7319 	scmd->satacmd_lba_high_lsb =
7320 	    (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8);
7321 	scmd->satacmd_cmd_reg = SATAC_PACKET;	/* Command */
7322 
7323 	/*
7324 	 * We want all data to be transfered via DMA.
7325 	 * But specify it only if drive supports DMA and DMA mode is
7326 	 * selected - some drives are sensitive about it.
7327 	 * Hopefully it wil work for all drives....
7328 	 */
7329 	if (sdinfo->satadrv_settings & SATA_DEV_DMA)
7330 		scmd->satacmd_features_reg = SATA_ATAPI_F_DMA;
7331 
7332 	/*
7333 	 * Features register requires special care for devices that use
7334 	 * Serial ATA bridge - they need an explicit specification of
7335 	 * the data transfer direction for Packet DMA commands.
7336 	 * Setting this bit is harmless if DMA is not used.
7337 	 *
7338 	 * Many drives do not implement word 80, specifying what ATA/ATAPI
7339 	 * spec they follow.
7340 	 * We are arbitrarily following the latest SerialATA 2.6 spec,
7341 	 * which uses ATA/ATAPI 6 specification for Identify Data, unless
7342 	 * ATA/ATAPI-7 support is explicitly indicated.
7343 	 */
7344 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
7345 	    sdinfo->satadrv_id.ai_majorversion != 0xffff &&
7346 	    (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) {
7347 		/*
7348 		 * Specification of major version is valid and version 7
7349 		 * is supported. It does automatically imply that all
7350 		 * spec features are supported. For now, we assume that
7351 		 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete.
7352 		 */
7353 		if ((sdinfo->satadrv_id.ai_dirdma &
7354 		    SATA_ATAPI_ID_DMADIR_REQ) != 0) {
7355 			if (scmd->satacmd_flags.sata_data_direction ==
7356 			    SATA_DIR_READ)
7357 			scmd->satacmd_features_reg |=
7358 			    SATA_ATAPI_F_DATA_DIR_READ;
7359 		}
7360 	}
7361 }
7362 
7363 
7364 #ifdef SATA_DEBUG
7365 
7366 /* Display 18 bytes of Inquiry data */
7367 static void
7368 sata_show_inqry_data(uint8_t *buf)
7369 {
7370 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
7371 	uint8_t *p;
7372 
7373 	cmn_err(CE_NOTE, "Inquiry data:");
7374 	cmn_err(CE_NOTE, "device type %x", inq->inq_dtype);
7375 	cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb);
7376 	cmn_err(CE_NOTE, "version %x", inq->inq_ansi);
7377 	cmn_err(CE_NOTE, "ATAPI transport version %d",
7378 	    SATA_ATAPI_TRANS_VERSION(inq));
7379 	cmn_err(CE_NOTE, "response data format %d, aenc %d",
7380 	    inq->inq_rdf, inq->inq_aenc);
7381 	cmn_err(CE_NOTE, " additional length %d", inq->inq_len);
7382 	cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs);
7383 	p = (uint8_t *)inq->inq_vid;
7384 	cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x "
7385 	    "%02x %02x %02x %02x",
7386 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
7387 	p = (uint8_t *)inq->inq_vid;
7388 	cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c",
7389 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
7390 
7391 	p = (uint8_t *)inq->inq_pid;
7392 	cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x "
7393 	    "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
7394 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7395 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7396 	p = (uint8_t *)inq->inq_pid;
7397 	cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c "
7398 	    "%c %c %c %c %c %c %c %c",
7399 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7400 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7401 
7402 	p = (uint8_t *)inq->inq_revision;
7403 	cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x",
7404 	    p[0], p[1], p[2], p[3]);
7405 	p = (uint8_t *)inq->inq_revision;
7406 	cmn_err(CE_NOTE, "revision: %c %c %c %c",
7407 	    p[0], p[1], p[2], p[3]);
7408 
7409 }
7410 
7411 
7412 static void
7413 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count)
7414 {
7415 	struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt;
7416 
7417 	if (scsi_pkt == NULL)
7418 		return;
7419 	if (count != 0) {
7420 		/* saving cdb */
7421 		bzero(sata_atapi_trace[sata_atapi_trace_index].acdb,
7422 		    SATA_ATAPI_MAX_CDB_LEN);
7423 		bcopy(scsi_pkt->pkt_cdbp,
7424 		    sata_atapi_trace[sata_atapi_trace_index].acdb, count);
7425 	} else {
7426 		bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)->
7427 		    sts_sensedata,
7428 		    sata_atapi_trace[sata_atapi_trace_index].arqs,
7429 		    SATA_ATAPI_MIN_RQSENSE_LEN);
7430 		sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason =
7431 		    scsi_pkt->pkt_reason;
7432 		sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason =
7433 		    spx->txlt_sata_pkt->satapkt_reason;
7434 
7435 		if (++sata_atapi_trace_index >= 64)
7436 			sata_atapi_trace_index = 0;
7437 	}
7438 }
7439 
7440 #endif
7441 
7442 /*
7443  * Fetch inquiry data from ATAPI device
7444  * Returns SATA_SUCCESS if operation was successfull, SATA_FAILURE otherwise.
7445  *
7446  * Note:
7447  * inqb pointer does not point to a DMA-able buffer. It is a local buffer
7448  * where the caller expects to see the inquiry data.
7449  *
7450  */
7451 
7452 static int
7453 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba,
7454     sata_address_t *saddr, struct scsi_inquiry *inq)
7455 {
7456 	sata_pkt_txlate_t *spx;
7457 	sata_pkt_t *spkt;
7458 	struct buf *bp;
7459 	sata_drive_info_t *sdinfo;
7460 	sata_cmd_t *scmd;
7461 	int rval;
7462 	uint8_t *rqsp;
7463 #ifdef SATA_DEBUG
7464 	char msg_buf[MAXPATHLEN];
7465 #endif
7466 
7467 	ASSERT(sata_hba != NULL);
7468 
7469 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
7470 	spx->txlt_sata_hba_inst = sata_hba;
7471 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
7472 	spkt = sata_pkt_alloc(spx, NULL);
7473 	if (spkt == NULL) {
7474 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7475 		return (SATA_FAILURE);
7476 	}
7477 	/* address is needed now */
7478 	spkt->satapkt_device.satadev_addr = *saddr;
7479 
7480 	/* scsi_inquiry size buffer */
7481 	bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry));
7482 	if (bp == NULL) {
7483 		sata_pkt_free(spx);
7484 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7485 		SATA_LOG_D((sata_hba, CE_WARN,
7486 		    "sata_get_atapi_inquiry_data: "
7487 		    "cannot allocate data buffer"));
7488 		return (SATA_FAILURE);
7489 	}
7490 	bp_mapin(bp); /* make data buffer accessible */
7491 
7492 	scmd = &spkt->satapkt_cmd;
7493 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7494 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7495 
7496 	/* Use synchronous mode */
7497 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7498 	spkt->satapkt_comp = NULL;
7499 	spkt->satapkt_time = sata_default_pkt_time;
7500 
7501 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
7502 
7503 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7504 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7505 
7506 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
7507 	sdinfo = sata_get_device_info(sata_hba,
7508 	    &spx->txlt_sata_pkt->satapkt_device);
7509 	if (sdinfo == NULL) {
7510 		/* we have to be carefull about the disapearing device */
7511 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7512 		rval = SATA_FAILURE;
7513 		goto cleanup;
7514 	}
7515 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7516 
7517 	/*
7518 	 * Set-up acdb. This works for atapi transport version 2 and later.
7519 	 */
7520 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7521 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7522 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
7523 	scmd->satacmd_acdb[1] = 0x00;
7524 	scmd->satacmd_acdb[2] = 0x00;
7525 	scmd->satacmd_acdb[3] = 0x00;
7526 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
7527 	scmd->satacmd_acdb[5] = 0x00;
7528 
7529 	sata_fixed_sense_data_preset(
7530 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7531 
7532 	/* Transfer command to HBA */
7533 	if (sata_hba_start(spx, &rval) != 0) {
7534 		/* Pkt not accepted for execution */
7535 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
7536 		    "sata_get_atapi_inquiry_data: "
7537 		    "Packet not accepted for execution - ret: %02x", rval);
7538 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7539 		rval = SATA_FAILURE;
7540 		goto cleanup;
7541 	}
7542 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7543 
7544 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
7545 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
7546 		    "sata_get_atapi_inquiry_data: "
7547 		    "Packet completed successfully - ret: %02x", rval);
7548 		/*
7549 		 * Sync buffer. Handle is in usual place in translate struct.
7550 		 * Normal completion - copy data into caller's buffer
7551 		 */
7552 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
7553 		    DDI_DMA_SYNC_FORCPU);
7554 		ASSERT(rval == DDI_SUCCESS);
7555 		bcopy(bp->b_un.b_addr, (uint8_t *)inq,
7556 		    sizeof (struct scsi_inquiry));
7557 #ifdef SATA_DEBUG
7558 		if (sata_debug_flags & SATA_DBG_ATAPI) {
7559 			sata_show_inqry_data((uint8_t *)inq);
7560 		}
7561 #endif
7562 		rval = SATA_SUCCESS;
7563 	} else {
7564 		/*
7565 		 * Something went wrong - analyze return - check rqsense data
7566 		 */
7567 		rval = SATA_FAILURE;
7568 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7569 			/*
7570 			 * ARQ data hopefull show something other than NO SENSE
7571 			 */
7572 			rqsp = scmd->satacmd_rqsense;
7573 #ifdef SATA_DEBUG
7574 			if (sata_debug_flags & SATA_DBG_ATAPI) {
7575 				msg_buf[0] = '\0';
7576 				(void) snprintf(msg_buf, MAXPATHLEN,
7577 				    "ATAPI packet completion reason: %02x\n"
7578 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x\n"
7579 				    "          %02x %02x %02x %02x %02x %02x\n"
7580 				    "          %02x %02x %02x %02x %02x %02x",
7581 				    spkt->satapkt_reason,
7582 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7583 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7584 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7585 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7586 				    rqsp[16], rqsp[17]);
7587 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7588 				    "%s", msg_buf);
7589 			}
7590 #endif
7591 		} else {
7592 			switch (spkt->satapkt_reason) {
7593 			case SATA_PKT_PORT_ERROR:
7594 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7595 				    "sata_get_atapi_inquiry_data: "
7596 				    "packet reason: port error", NULL);
7597 				break;
7598 
7599 			case SATA_PKT_TIMEOUT:
7600 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7601 				    "sata_get_atapi_inquiry_data: "
7602 				    "packet reason: timeout", NULL);
7603 				break;
7604 
7605 			case SATA_PKT_ABORTED:
7606 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7607 				    "sata_get_atapi_inquiry_data: "
7608 				    "packet reason: aborted", NULL);
7609 				break;
7610 
7611 			case SATA_PKT_RESET:
7612 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7613 				    "sata_get_atapi_inquiry_data: "
7614 				    "packet reason: reset\n", NULL);
7615 				break;
7616 			default:
7617 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7618 				    "sata_get_atapi_inquiry_data: "
7619 				    "invalid packet reason: %02x\n",
7620 				    spkt->satapkt_reason);
7621 				break;
7622 			}
7623 		}
7624 	}
7625 cleanup:
7626 	sata_free_local_buffer(spx);
7627 	sata_pkt_free(spx);
7628 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
7629 	return (rval);
7630 }
7631 
7632 
7633 
7634 
7635 
7636 #if 0
7637 #ifdef SATA_DEBUG
7638 
7639 /*
7640  * Test ATAPI packet command.
7641  * Single threaded test: send packet command in synch mode, process completion
7642  *
7643  */
7644 static void
7645 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport)
7646 {
7647 	sata_pkt_txlate_t *spx;
7648 	sata_pkt_t *spkt;
7649 	struct buf *bp;
7650 	sata_device_t sata_device;
7651 	sata_drive_info_t *sdinfo;
7652 	sata_cmd_t *scmd;
7653 	int rval;
7654 	uint8_t *rqsp;
7655 
7656 	ASSERT(sata_hba_inst != NULL);
7657 	sata_device.satadev_addr.cport = cport;
7658 	sata_device.satadev_addr.pmport = 0;
7659 	sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
7660 	sata_device.satadev_rev = SATA_DEVICE_REV;
7661 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7662 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
7663 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7664 	if (sdinfo == NULL) {
7665 		sata_log(sata_hba_inst, CE_WARN,
7666 		    "sata_test_atapi_packet_command: "
7667 		    "no device info for cport %d",
7668 		    sata_device.satadev_addr.cport);
7669 		return;
7670 	}
7671 
7672 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
7673 	spx->txlt_sata_hba_inst = sata_hba_inst;
7674 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
7675 	spkt = sata_pkt_alloc(spx, NULL);
7676 	if (spkt == NULL) {
7677 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7678 		return;
7679 	}
7680 	/* address is needed now */
7681 	spkt->satapkt_device.satadev_addr = sata_device.satadev_addr;
7682 
7683 	/* 1024k buffer */
7684 	bp = sata_alloc_local_buffer(spx, 1024);
7685 	if (bp == NULL) {
7686 		sata_pkt_free(spx);
7687 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7688 		sata_log(sata_hba_inst, CE_WARN,
7689 		    "sata_test_atapi_packet_command: "
7690 		    "cannot allocate data buffer");
7691 		return;
7692 	}
7693 	bp_mapin(bp); /* make data buffer accessible */
7694 
7695 	scmd = &spkt->satapkt_cmd;
7696 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7697 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7698 
7699 	/* Use synchronous mode */
7700 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7701 
7702 	/* Synchronous mode, no callback - may be changed by the caller */
7703 	spkt->satapkt_comp = NULL;
7704 	spkt->satapkt_time = sata_default_pkt_time;
7705 
7706 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
7707 
7708 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7709 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7710 
7711 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7712 
7713 	/* Set-up acdb. */
7714 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7715 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7716 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
7717 	scmd->satacmd_acdb[1] = 0x00;
7718 	scmd->satacmd_acdb[2] = 0x00;
7719 	scmd->satacmd_acdb[3] = 0x00;
7720 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
7721 	scmd->satacmd_acdb[5] = 0x00;
7722 
7723 	sata_fixed_sense_data_preset(
7724 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7725 
7726 	/* Transfer command to HBA */
7727 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7728 	if (sata_hba_start(spx, &rval) != 0) {
7729 		/* Pkt not accepted for execution */
7730 		sata_log(sata_hba_inst, CE_WARN,
7731 		    "sata_test_atapi_packet_command: "
7732 		    "Packet not accepted for execution - ret: %02x", rval);
7733 		mutex_exit(
7734 		    &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7735 		goto cleanup;
7736 	}
7737 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7738 
7739 	/*
7740 	 * Sync buffer. Handle is in usual place in translate struct.
7741 	 */
7742 	rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
7743 	    DDI_DMA_SYNC_FORCPU);
7744 	ASSERT(rval == DDI_SUCCESS);
7745 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
7746 		sata_log(sata_hba_inst, CE_WARN,
7747 		    "sata_test_atapi_packet_command: "
7748 		    "Packet completed successfully");
7749 		/*
7750 		 * Normal completion - show inquiry data
7751 		 */
7752 		sata_show_inqry_data((uint8_t *)bp->b_un.b_addr);
7753 	} else {
7754 		/*
7755 		 * Something went wrong - analyze return - check rqsense data
7756 		 */
7757 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7758 			/*
7759 			 * ARQ data hopefull show something other than NO SENSE
7760 			 */
7761 			rqsp = scmd->satacmd_rqsense;
7762 			sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7763 			    "ATAPI packet completion reason: %02x\n"
7764 			    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
7765 			    "          %02x %02x %02x %02x %02x %02x "
7766 			    "          %02x %02x %02x %02x %02x %02x\n",
7767 			    spkt->satapkt_reason,
7768 			    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7769 			    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7770 			    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7771 			    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7772 			    rqsp[16], rqsp[17]);
7773 		} else {
7774 			switch (spkt->satapkt_reason) {
7775 			case SATA_PKT_PORT_ERROR:
7776 				sata_log(sata_hba_inst, CE_WARN,
7777 				    "sata_test_atapi_packet_command: "
7778 				    "packet reason: port error\n");
7779 				break;
7780 
7781 			case SATA_PKT_TIMEOUT:
7782 				sata_log(sata_hba_inst, CE_WARN,
7783 				    "sata_test_atapi_packet_command: "
7784 				    "packet reason: timeout\n");
7785 				break;
7786 
7787 			case SATA_PKT_ABORTED:
7788 				sata_log(sata_hba_inst, CE_WARN,
7789 				    "sata_test_atapi_packet_command: "
7790 				    "packet reason: aborted\n");
7791 				break;
7792 
7793 			case SATA_PKT_RESET:
7794 				sata_log(sata_hba_inst, CE_WARN,
7795 				    "sata_test_atapi_packet_command: "
7796 				    "packet reason: reset\n");
7797 				break;
7798 			default:
7799 				sata_log(sata_hba_inst, CE_WARN,
7800 				    "sata_test_atapi_packet_command: "
7801 				    "invalid packet reason: %02x\n",
7802 				    spkt->satapkt_reason);
7803 				break;
7804 			}
7805 		}
7806 	}
7807 cleanup:
7808 	sata_free_local_buffer(spx);
7809 	sata_pkt_free(spx);
7810 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
7811 }
7812 
7813 #endif /* SATA_DEBUG */
7814 #endif /* 1 */
7815 
7816 
7817 /* ************************** LOCAL HELPER FUNCTIONS *********************** */
7818 
7819 /*
7820  * Validate sata_tran info
7821  * SATA_FAILURE returns if structure is inconsistent or structure revision
7822  * does not match one used by the framework.
7823  *
7824  * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains
7825  * required function pointers.
7826  * Returns SATA_FAILURE otherwise.
7827  */
7828 static int
7829 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran)
7830 {
7831 	/*
7832 	 * SATA_TRAN_HBA_REV is the current (highest) revision number
7833 	 * of the SATA interface.
7834 	 */
7835 	if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) {
7836 		sata_log(NULL, CE_WARN,
7837 		    "sata: invalid sata_hba_tran version %d for driver %s",
7838 		    sata_tran->sata_tran_hba_rev, ddi_driver_name(dip));
7839 		return (SATA_FAILURE);
7840 	}
7841 
7842 	if (dip != sata_tran->sata_tran_hba_dip) {
7843 		SATA_LOG_D((NULL, CE_WARN,
7844 		    "sata: inconsistent sata_tran_hba_dip "
7845 		    "%p / %p", sata_tran->sata_tran_hba_dip, dip));
7846 		return (SATA_FAILURE);
7847 	}
7848 
7849 	if (sata_tran->sata_tran_probe_port == NULL ||
7850 	    sata_tran->sata_tran_start == NULL ||
7851 	    sata_tran->sata_tran_abort == NULL ||
7852 	    sata_tran->sata_tran_reset_dport == NULL ||
7853 	    sata_tran->sata_tran_hotplug_ops == NULL ||
7854 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL ||
7855 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate ==
7856 	    NULL) {
7857 		SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing "
7858 		    "required functions"));
7859 	}
7860 	return (SATA_SUCCESS);
7861 }
7862 
7863 /*
7864  * Remove HBA instance from sata_hba_list.
7865  */
7866 static void
7867 sata_remove_hba_instance(dev_info_t *dip)
7868 {
7869 	sata_hba_inst_t	*sata_hba_inst;
7870 
7871 	mutex_enter(&sata_mutex);
7872 	for (sata_hba_inst = sata_hba_list;
7873 	    sata_hba_inst != (struct sata_hba_inst *)NULL;
7874 	    sata_hba_inst = sata_hba_inst->satahba_next) {
7875 		if (sata_hba_inst->satahba_dip == dip)
7876 			break;
7877 	}
7878 
7879 	if (sata_hba_inst == (struct sata_hba_inst *)NULL) {
7880 #ifdef SATA_DEBUG
7881 		cmn_err(CE_WARN, "sata_remove_hba_instance: "
7882 		    "unknown HBA instance\n");
7883 #endif
7884 		ASSERT(FALSE);
7885 	}
7886 	if (sata_hba_inst == sata_hba_list) {
7887 		sata_hba_list = sata_hba_inst->satahba_next;
7888 		if (sata_hba_list) {
7889 			sata_hba_list->satahba_prev =
7890 			    (struct sata_hba_inst *)NULL;
7891 		}
7892 		if (sata_hba_inst == sata_hba_list_tail) {
7893 			sata_hba_list_tail = NULL;
7894 		}
7895 	} else if (sata_hba_inst == sata_hba_list_tail) {
7896 		sata_hba_list_tail = sata_hba_inst->satahba_prev;
7897 		if (sata_hba_list_tail) {
7898 			sata_hba_list_tail->satahba_next =
7899 			    (struct sata_hba_inst *)NULL;
7900 		}
7901 	} else {
7902 		sata_hba_inst->satahba_prev->satahba_next =
7903 		    sata_hba_inst->satahba_next;
7904 		sata_hba_inst->satahba_next->satahba_prev =
7905 		    sata_hba_inst->satahba_prev;
7906 	}
7907 	mutex_exit(&sata_mutex);
7908 }
7909 
7910 
7911 
7912 
7913 
7914 /*
7915  * Probe all SATA ports of the specified HBA instance.
7916  * The assumption is that there are no target and attachment point minor nodes
7917  * created by the boot subsystems, so we do not need to prune device tree.
7918  *
7919  * This function is called only from sata_hba_attach(). It does not have to
7920  * be protected by controller mutex, because the hba_attached flag is not set
7921  * yet and no one would be touching this HBA instance other than this thread.
7922  * Determines if port is active and what type of the device is attached
7923  * (if any). Allocates necessary structures for each port.
7924  *
7925  * An AP (Attachement Point) node is created for each SATA device port even
7926  * when there is no device attached.
7927  */
7928 
7929 static 	void
7930 sata_probe_ports(sata_hba_inst_t *sata_hba_inst)
7931 {
7932 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
7933 	int			ncport, npmport;
7934 	sata_cport_info_t 	*cportinfo;
7935 	sata_drive_info_t	*drive;
7936 	sata_pmult_info_t	*pminfo;
7937 	sata_pmport_info_t 	*pmportinfo;
7938 	sata_device_t		sata_device;
7939 	int			rval;
7940 	dev_t			minor_number;
7941 	char			name[16];
7942 	clock_t			start_time, cur_time;
7943 
7944 	/*
7945 	 * Probe controller ports first, to find port status and
7946 	 * any port multiplier attached.
7947 	 */
7948 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
7949 		/* allocate cport structure */
7950 		cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP);
7951 		ASSERT(cportinfo != NULL);
7952 		mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL);
7953 
7954 		mutex_enter(&cportinfo->cport_mutex);
7955 
7956 		cportinfo->cport_addr.cport = ncport;
7957 		cportinfo->cport_addr.pmport = 0;
7958 		cportinfo->cport_addr.qual = SATA_ADDR_CPORT;
7959 		cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
7960 		cportinfo->cport_state |= SATA_STATE_PROBING;
7961 		SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo;
7962 
7963 		/*
7964 		 * Regardless if a port is usable or not, create
7965 		 * an attachment point
7966 		 */
7967 		mutex_exit(&cportinfo->cport_mutex);
7968 		minor_number =
7969 		    SATA_MAKE_AP_MINOR(ddi_get_instance(dip), ncport, 0, 0);
7970 		(void) sprintf(name, "%d", ncport);
7971 		if (ddi_create_minor_node(dip, name, S_IFCHR,
7972 		    minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) !=
7973 		    DDI_SUCCESS) {
7974 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
7975 			    "cannot create SATA attachment point for port %d",
7976 			    ncport);
7977 		}
7978 
7979 		/* Probe port */
7980 		start_time = ddi_get_lbolt();
7981 	reprobe_cport:
7982 		sata_device.satadev_addr.cport = ncport;
7983 		sata_device.satadev_addr.pmport = 0;
7984 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
7985 		sata_device.satadev_rev = SATA_DEVICE_REV;
7986 
7987 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
7988 		    (dip, &sata_device);
7989 
7990 		mutex_enter(&cportinfo->cport_mutex);
7991 		sata_update_port_scr(&cportinfo->cport_scr, &sata_device);
7992 		if (rval != SATA_SUCCESS) {
7993 			/* Something went wrong? Fail the port */
7994 			cportinfo->cport_state = SATA_PSTATE_FAILED;
7995 			mutex_exit(&cportinfo->cport_mutex);
7996 			continue;
7997 		}
7998 		cportinfo->cport_state &= ~SATA_STATE_PROBING;
7999 		cportinfo->cport_state |= SATA_STATE_PROBED;
8000 		cportinfo->cport_dev_type = sata_device.satadev_type;
8001 
8002 		cportinfo->cport_state |= SATA_STATE_READY;
8003 		if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) {
8004 			mutex_exit(&cportinfo->cport_mutex);
8005 			continue;
8006 		}
8007 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
8008 			/*
8009 			 * There is some device attached.
8010 			 * Allocate device info structure
8011 			 */
8012 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) {
8013 				mutex_exit(&cportinfo->cport_mutex);
8014 				SATA_CPORTINFO_DRV_INFO(cportinfo) =
8015 				    kmem_zalloc(sizeof (sata_drive_info_t),
8016 				    KM_SLEEP);
8017 				mutex_enter(&cportinfo->cport_mutex);
8018 			}
8019 			drive = SATA_CPORTINFO_DRV_INFO(cportinfo);
8020 			drive->satadrv_addr = cportinfo->cport_addr;
8021 			drive->satadrv_addr.qual = SATA_ADDR_DCPORT;
8022 			drive->satadrv_type = cportinfo->cport_dev_type;
8023 			drive->satadrv_state = SATA_STATE_UNKNOWN;
8024 
8025 			mutex_exit(&cportinfo->cport_mutex);
8026 			if (sata_add_device(dip, sata_hba_inst, ncport, 0) !=
8027 			    SATA_SUCCESS) {
8028 				/*
8029 				 * Plugged device was not correctly identified.
8030 				 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT
8031 				 */
8032 				cur_time = ddi_get_lbolt();
8033 				if ((cur_time - start_time) <
8034 				    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
8035 					/* sleep for a while */
8036 					delay(drv_usectohz(
8037 					    SATA_DEV_IDENTIFY_RTR_DLY));
8038 					goto reprobe_cport;
8039 				}
8040 			}
8041 		} else {
8042 			mutex_exit(&cportinfo->cport_mutex);
8043 			ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT);
8044 			pminfo = kmem_zalloc(sizeof (sata_pmult_info_t),
8045 			    KM_SLEEP);
8046 			mutex_enter(&cportinfo->cport_mutex);
8047 			ASSERT(pminfo != NULL);
8048 			SATA_CPORTINFO_PMULT_INFO(cportinfo) = pminfo;
8049 			pminfo->pmult_addr.cport = cportinfo->cport_addr.cport;
8050 			pminfo->pmult_addr.pmport = SATA_PMULT_HOSTPORT;
8051 			pminfo->pmult_addr.qual = SATA_ADDR_PMPORT;
8052 			pminfo->pmult_num_dev_ports =
8053 			    sata_device.satadev_add_info;
8054 			mutex_init(&pminfo->pmult_mutex, NULL, MUTEX_DRIVER,
8055 			    NULL);
8056 			pminfo->pmult_state = SATA_STATE_PROBING;
8057 			mutex_exit(&cportinfo->cport_mutex);
8058 
8059 			/* Probe Port Multiplier ports */
8060 			for (npmport = 0;
8061 			    npmport < pminfo->pmult_num_dev_ports;
8062 			    npmport++) {
8063 				pmportinfo = kmem_zalloc(
8064 				    sizeof (sata_pmport_info_t), KM_SLEEP);
8065 				mutex_enter(&cportinfo->cport_mutex);
8066 				ASSERT(pmportinfo != NULL);
8067 				pmportinfo->pmport_addr.cport = ncport;
8068 				pmportinfo->pmport_addr.pmport = npmport;
8069 				pmportinfo->pmport_addr.qual =
8070 				    SATA_ADDR_PMPORT;
8071 				pminfo->pmult_dev_port[npmport] = pmportinfo;
8072 
8073 				mutex_init(&pmportinfo->pmport_mutex, NULL,
8074 				    MUTEX_DRIVER, NULL);
8075 
8076 				mutex_exit(&cportinfo->cport_mutex);
8077 
8078 				/* Create an attachment point */
8079 				minor_number = SATA_MAKE_AP_MINOR(
8080 				    ddi_get_instance(dip), ncport, npmport, 1);
8081 				(void) sprintf(name, "%d.%d", ncport, npmport);
8082 				if (ddi_create_minor_node(dip, name, S_IFCHR,
8083 				    minor_number, DDI_NT_SATA_ATTACHMENT_POINT,
8084 				    0) != DDI_SUCCESS) {
8085 					sata_log(sata_hba_inst, CE_WARN,
8086 					    "sata_hba_attach: "
8087 					    "cannot create SATA attachment "
8088 					    "point for port %d pmult port %d",
8089 					    ncport, npmport);
8090 				}
8091 
8092 				start_time = ddi_get_lbolt();
8093 			reprobe_pmport:
8094 				sata_device.satadev_addr.pmport = npmport;
8095 				sata_device.satadev_addr.qual =
8096 				    SATA_ADDR_PMPORT;
8097 
8098 				rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8099 				    (dip, &sata_device);
8100 				mutex_enter(&cportinfo->cport_mutex);
8101 
8102 				/* sata_update_port_info() */
8103 				sata_update_port_scr(&pmportinfo->pmport_scr,
8104 				    &sata_device);
8105 
8106 				if (rval != SATA_SUCCESS) {
8107 					pmportinfo->pmport_state =
8108 					    SATA_PSTATE_FAILED;
8109 					mutex_exit(&cportinfo->cport_mutex);
8110 					continue;
8111 				}
8112 				pmportinfo->pmport_state &=
8113 				    ~SATA_STATE_PROBING;
8114 				pmportinfo->pmport_state |= SATA_STATE_PROBED;
8115 				pmportinfo->pmport_dev_type =
8116 				    sata_device.satadev_type;
8117 
8118 				pmportinfo->pmport_state |= SATA_STATE_READY;
8119 				if (pmportinfo->pmport_dev_type ==
8120 				    SATA_DTYPE_NONE) {
8121 					mutex_exit(&cportinfo->cport_mutex);
8122 					continue;
8123 				}
8124 				/* Port multipliers cannot be chained */
8125 				ASSERT(pmportinfo->pmport_dev_type !=
8126 				    SATA_DTYPE_PMULT);
8127 				/*
8128 				 * There is something attached to Port
8129 				 * Multiplier device port
8130 				 * Allocate device info structure
8131 				 */
8132 				if (pmportinfo->pmport_sata_drive == NULL) {
8133 					mutex_exit(&cportinfo->cport_mutex);
8134 					pmportinfo->pmport_sata_drive =
8135 					    kmem_zalloc(
8136 					    sizeof (sata_drive_info_t),
8137 					    KM_SLEEP);
8138 					mutex_enter(&cportinfo->cport_mutex);
8139 				}
8140 				drive = pmportinfo->pmport_sata_drive;
8141 				drive->satadrv_addr.cport =
8142 				    pmportinfo->pmport_addr.cport;
8143 				drive->satadrv_addr.pmport = npmport;
8144 				drive->satadrv_addr.qual = SATA_ADDR_DPMPORT;
8145 				drive->satadrv_type = pmportinfo->
8146 				    pmport_dev_type;
8147 				drive->satadrv_state = SATA_STATE_UNKNOWN;
8148 
8149 				mutex_exit(&cportinfo->cport_mutex);
8150 				if (sata_add_device(dip, sata_hba_inst, ncport,
8151 				    npmport) != SATA_SUCCESS) {
8152 					/*
8153 					 * Plugged device was not correctly
8154 					 * identified. Retry, within the
8155 					 * SATA_DEV_IDENTIFY_TIMEOUT
8156 					 */
8157 					cur_time = ddi_get_lbolt();
8158 					if ((cur_time - start_time) <
8159 					    drv_usectohz(
8160 					    SATA_DEV_IDENTIFY_TIMEOUT)) {
8161 						/* sleep for a while */
8162 						delay(drv_usectohz(
8163 						    SATA_DEV_IDENTIFY_RTR_DLY));
8164 						goto reprobe_pmport;
8165 					}
8166 				}
8167 			}
8168 			pmportinfo->pmport_state =
8169 			    SATA_STATE_PROBED | SATA_STATE_READY;
8170 		}
8171 	}
8172 }
8173 
8174 /*
8175  * Add SATA device for specified HBA instance & port (SCSI target
8176  * device nodes).
8177  * This function is called (indirectly) only from sata_hba_attach().
8178  * A target node is created when there is a supported type device attached,
8179  * but may be removed if it cannot be put online.
8180  *
8181  * This function cannot be called from an interrupt context.
8182  *
8183  * ONLY DISK TARGET NODES ARE CREATED NOW
8184  *
8185  * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when
8186  * device identification failed - adding a device could be retried.
8187  *
8188  */
8189 static 	int
8190 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst, int cport,
8191     int pmport)
8192 {
8193 	sata_cport_info_t 	*cportinfo;
8194 	sata_pmult_info_t	*pminfo;
8195 	sata_pmport_info_t	*pmportinfo;
8196 	dev_info_t		*cdip;		/* child dip */
8197 	sata_device_t		sata_device;
8198 	int			rval;
8199 
8200 
8201 
8202 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
8203 	ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE);
8204 	mutex_enter(&cportinfo->cport_mutex);
8205 	/*
8206 	 * Some device is attached to a controller port.
8207 	 * We rely on controllers distinquishing between no-device,
8208 	 * attached port multiplier and other kind of attached device.
8209 	 * We need to get Identify Device data and determine
8210 	 * positively the dev type before trying to attach
8211 	 * the target driver.
8212 	 */
8213 	sata_device.satadev_rev = SATA_DEVICE_REV;
8214 	if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
8215 		/*
8216 		 * Not port multiplier.
8217 		 */
8218 		sata_device.satadev_addr = cportinfo->cport_addr;
8219 		sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
8220 		mutex_exit(&cportinfo->cport_mutex);
8221 
8222 		rval = sata_probe_device(sata_hba_inst, &sata_device);
8223 		if (rval != SATA_SUCCESS ||
8224 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN)
8225 			return (SATA_FAILURE);
8226 
8227 		mutex_enter(&cportinfo->cport_mutex);
8228 		sata_show_drive_info(sata_hba_inst,
8229 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
8230 
8231 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
8232 			/*
8233 			 * Could not determine device type or
8234 			 * a device is not supported.
8235 			 * Degrade this device to unknown.
8236 			 */
8237 			cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8238 			mutex_exit(&cportinfo->cport_mutex);
8239 			return (SATA_SUCCESS);
8240 		}
8241 		cportinfo->cport_dev_type = sata_device.satadev_type;
8242 		cportinfo->cport_tgtnode_clean = B_TRUE;
8243 		mutex_exit(&cportinfo->cport_mutex);
8244 
8245 		/*
8246 		 * Initialize device to the desired state. Even if it
8247 		 * fails, the device will still attach but syslog
8248 		 * will show the warning.
8249 		 */
8250 		if (sata_initialize_device(sata_hba_inst,
8251 		    SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS)
8252 			/* Retry */
8253 			(void) sata_initialize_device(sata_hba_inst,
8254 			    SATA_CPORTINFO_DRV_INFO(cportinfo));
8255 
8256 		cdip = sata_create_target_node(pdip, sata_hba_inst,
8257 		    &sata_device.satadev_addr);
8258 		mutex_enter(&cportinfo->cport_mutex);
8259 		if (cdip == NULL) {
8260 			/*
8261 			 * Attaching target node failed.
8262 			 * We retain sata_drive_info structure...
8263 			 */
8264 			mutex_exit(&cportinfo->cport_mutex);
8265 			return (SATA_SUCCESS);
8266 		}
8267 		(SATA_CPORTINFO_DRV_INFO(cportinfo))->
8268 		    satadrv_state = SATA_STATE_READY;
8269 	} else {
8270 		/* This must be Port Multiplier type */
8271 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
8272 			SATA_LOG_D((sata_hba_inst, CE_WARN,
8273 			    "sata_add_device: "
8274 			    "unrecognized dev type %x",
8275 			    cportinfo->cport_dev_type));
8276 			mutex_exit(&cportinfo->cport_mutex);
8277 			return (SATA_SUCCESS);
8278 		}
8279 		pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
8280 		pmportinfo = pminfo->pmult_dev_port[pmport];
8281 		sata_device.satadev_addr = pmportinfo->pmport_addr;
8282 		sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
8283 		mutex_exit(&cportinfo->cport_mutex);
8284 
8285 		rval = sata_probe_device(sata_hba_inst, &sata_device);
8286 		if (rval != SATA_SUCCESS ||
8287 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN) {
8288 			return (SATA_FAILURE);
8289 		}
8290 		mutex_enter(&cportinfo->cport_mutex);
8291 		sata_show_drive_info(sata_hba_inst,
8292 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
8293 
8294 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
8295 			/*
8296 			 * Could not determine device type.
8297 			 * Degrade this device to unknown.
8298 			 */
8299 			pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
8300 			mutex_exit(&cportinfo->cport_mutex);
8301 			return (SATA_SUCCESS);
8302 		}
8303 		pmportinfo->pmport_dev_type = sata_device.satadev_type;
8304 		pmportinfo->pmport_tgtnode_clean = B_TRUE;
8305 		mutex_exit(&cportinfo->cport_mutex);
8306 
8307 		/*
8308 		 * Initialize device to the desired state.
8309 		 * Even if it fails, the device will still
8310 		 * attach but syslog will show the warning.
8311 		 */
8312 		if (sata_initialize_device(sata_hba_inst,
8313 		    pmportinfo->pmport_sata_drive) != SATA_SUCCESS)
8314 			/* Retry */
8315 			(void) sata_initialize_device(sata_hba_inst,
8316 			    pmportinfo->pmport_sata_drive);
8317 
8318 		cdip = sata_create_target_node(pdip, sata_hba_inst,
8319 		    &sata_device.satadev_addr);
8320 		mutex_enter(&cportinfo->cport_mutex);
8321 		if (cdip == NULL) {
8322 			/*
8323 			 * Attaching target node failed.
8324 			 * We retain sata_drive_info structure...
8325 			 */
8326 			mutex_exit(&cportinfo->cport_mutex);
8327 			return (SATA_SUCCESS);
8328 		}
8329 		pmportinfo->pmport_sata_drive->satadrv_state |=
8330 		    SATA_STATE_READY;
8331 	}
8332 	mutex_exit(&cportinfo->cport_mutex);
8333 	return (SATA_SUCCESS);
8334 }
8335 
8336 
8337 
8338 /*
8339  * Create scsi target node for attached device, create node properties and
8340  * attach the node.
8341  * The node could be removed if the device onlining fails.
8342  *
8343  * A dev_info_t pointer is returned if operation is successful, NULL is
8344  * returned otherwise.
8345  *
8346  * No port multiplier support.
8347  */
8348 
8349 static dev_info_t *
8350 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst,
8351 			sata_address_t *sata_addr)
8352 {
8353 	dev_info_t *cdip = NULL;
8354 	int rval;
8355 	char *nname = NULL;
8356 	char **compatible = NULL;
8357 	int ncompatible;
8358 	struct scsi_inquiry inq;
8359 	sata_device_t sata_device;
8360 	sata_drive_info_t *sdinfo;
8361 	int target;
8362 	int i;
8363 
8364 	sata_device.satadev_rev = SATA_DEVICE_REV;
8365 	sata_device.satadev_addr = *sata_addr;
8366 
8367 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport)));
8368 
8369 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
8370 
8371 	target = SATA_TO_SCSI_TARGET(sata_addr->cport,
8372 	    sata_addr->pmport, sata_addr->qual);
8373 
8374 	if (sdinfo == NULL) {
8375 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8376 		    sata_addr->cport)));
8377 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8378 		    "sata_create_target_node: no sdinfo for target %x",
8379 		    target));
8380 		return (NULL);
8381 	}
8382 
8383 	/*
8384 	 * create or get scsi inquiry data, expected by
8385 	 * scsi_hba_nodename_compatible_get()
8386 	 * SATA hard disks get Identify Data translated into Inguiry Data.
8387 	 * ATAPI devices respond directly to Inquiry request.
8388 	 */
8389 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
8390 		sata_identdev_to_inquiry(sata_hba_inst, sdinfo,
8391 		    (uint8_t *)&inq);
8392 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8393 		    sata_addr->cport)));
8394 	} else { /* Assume supported ATAPI device */
8395 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8396 		    sata_addr->cport)));
8397 		if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr,
8398 		    &inq) == SATA_FAILURE)
8399 			return (NULL);
8400 		/*
8401 		 * Save supported ATAPI transport version
8402 		 */
8403 		sdinfo->satadrv_atapi_trans_ver =
8404 		    SATA_ATAPI_TRANS_VERSION(&inq);
8405 	}
8406 
8407 	/* determine the node name and compatible */
8408 	scsi_hba_nodename_compatible_get(&inq, NULL,
8409 	    inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
8410 
8411 #ifdef SATA_DEBUG
8412 	if (sata_debug_flags & SATA_DBG_NODES) {
8413 		if (nname == NULL) {
8414 			cmn_err(CE_NOTE, "sata_create_target_node: "
8415 			    "cannot determine nodename for target %d\n",
8416 			    target);
8417 		} else {
8418 			cmn_err(CE_WARN, "sata_create_target_node: "
8419 			    "target %d nodename: %s\n", target, nname);
8420 		}
8421 		if (compatible == NULL) {
8422 			cmn_err(CE_WARN,
8423 			    "sata_create_target_node: no compatible name\n");
8424 		} else {
8425 			for (i = 0; i < ncompatible; i++) {
8426 				cmn_err(CE_WARN, "sata_create_target_node: "
8427 				    "compatible name: %s\n", compatible[i]);
8428 			}
8429 		}
8430 	}
8431 #endif
8432 
8433 	/* if nodename can't be determined, log error and exit */
8434 	if (nname == NULL) {
8435 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8436 		    "sata_create_target_node: cannot determine nodename "
8437 		    "for target %d\n", target));
8438 		scsi_hba_nodename_compatible_free(nname, compatible);
8439 		return (NULL);
8440 	}
8441 	/*
8442 	 * Create scsi target node
8443 	 */
8444 	ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip);
8445 	rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
8446 	    "device-type", "scsi");
8447 
8448 	if (rval != DDI_PROP_SUCCESS) {
8449 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8450 		    "updating device_type prop failed %d", rval));
8451 		goto fail;
8452 	}
8453 
8454 	/*
8455 	 * Create target node properties: target & lun
8456 	 */
8457 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target);
8458 	if (rval != DDI_PROP_SUCCESS) {
8459 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8460 		    "updating target prop failed %d", rval));
8461 		goto fail;
8462 	}
8463 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0);
8464 	if (rval != DDI_PROP_SUCCESS) {
8465 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8466 		    "updating target prop failed %d", rval));
8467 		goto fail;
8468 	}
8469 
8470 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
8471 		/*
8472 		 * Add "variant" property
8473 		 */
8474 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
8475 		    "variant", "atapi");
8476 		if (rval != DDI_PROP_SUCCESS) {
8477 			SATA_LOG_D((sata_hba_inst, CE_WARN,
8478 			    "sata_create_target_node: variant atapi "
8479 			    "property could not be created: %d", rval));
8480 			goto fail;
8481 		}
8482 	}
8483 	/* decorate the node with compatible */
8484 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible",
8485 	    compatible, ncompatible) != DDI_PROP_SUCCESS) {
8486 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8487 		    "sata_create_target_node: FAIL compatible props cdip 0x%p",
8488 		    (void *)cdip));
8489 		goto fail;
8490 	}
8491 
8492 
8493 	/*
8494 	 * Now, try to attach the driver. If probing of the device fails,
8495 	 * the target node may be removed
8496 	 */
8497 	rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH);
8498 
8499 	scsi_hba_nodename_compatible_free(nname, compatible);
8500 
8501 	if (rval == NDI_SUCCESS)
8502 		return (cdip);
8503 
8504 	/* target node was removed - are we sure? */
8505 	return (NULL);
8506 
8507 fail:
8508 	scsi_hba_nodename_compatible_free(nname, compatible);
8509 	ddi_prop_remove_all(cdip);
8510 	rval = ndi_devi_free(cdip);
8511 	if (rval != NDI_SUCCESS) {
8512 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8513 		    "node removal failed %d", rval));
8514 	}
8515 	sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: "
8516 	    "cannot create target node for SATA device at port %d",
8517 	    sata_addr->cport);
8518 	return (NULL);
8519 }
8520 
8521 
8522 
8523 /*
8524  * Re-probe sata port, check for a device and attach info
8525  * structures when necessary. Identify Device data is fetched, if possible.
8526  * Assumption: sata address is already validated.
8527  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
8528  * the presence of a device and its type.
8529  *
8530  * flag arg specifies that the function should try multiple times to identify
8531  * device type and to initialize it, or it should return immediately on failure.
8532  * SATA_DEV_IDENTIFY_RETRY - retry
8533  * SATA_DEV_IDENTIFY_NORETRY - no retry
8534  *
8535  * SATA_FAILURE is returned if one of the operations failed.
8536  *
8537  * This function cannot be called in interrupt context - it may sleep.
8538  */
8539 static int
8540 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
8541     int flag)
8542 {
8543 	sata_cport_info_t *cportinfo;
8544 	sata_drive_info_t *sdinfo;
8545 	boolean_t init_device = B_FALSE;
8546 	int prev_device_type = SATA_DTYPE_NONE;
8547 	int prev_device_settings = 0;
8548 	clock_t start_time;
8549 	int retry = B_FALSE;
8550 	int rval;
8551 
8552 	/* We only care about host sata cport for now */
8553 	cportinfo = SATA_CPORT_INFO(sata_hba_inst,
8554 	    sata_device->satadev_addr.cport);
8555 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8556 	if (sdinfo != NULL) {
8557 		/*
8558 		 * We are re-probing port with a previously attached device.
8559 		 * Save previous device type and settings
8560 		 */
8561 		prev_device_type = cportinfo->cport_dev_type;
8562 		prev_device_settings = sdinfo->satadrv_settings;
8563 	}
8564 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
8565 		start_time = ddi_get_lbolt();
8566 		retry = B_TRUE;
8567 	}
8568 retry_probe:
8569 
8570 	/* probe port */
8571 	mutex_enter(&cportinfo->cport_mutex);
8572 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
8573 	cportinfo->cport_state |= SATA_STATE_PROBING;
8574 	mutex_exit(&cportinfo->cport_mutex);
8575 
8576 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8577 	    (SATA_DIP(sata_hba_inst), sata_device);
8578 
8579 	mutex_enter(&cportinfo->cport_mutex);
8580 	if (rval != SATA_SUCCESS) {
8581 		cportinfo->cport_state = SATA_PSTATE_FAILED;
8582 		mutex_exit(&cportinfo->cport_mutex);
8583 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: "
8584 		    "SATA port %d probing failed",
8585 		    cportinfo->cport_addr.cport));
8586 		return (SATA_FAILURE);
8587 	}
8588 
8589 	/*
8590 	 * update sata port state and set device type
8591 	 */
8592 	sata_update_port_info(sata_hba_inst, sata_device);
8593 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
8594 
8595 	/*
8596 	 * Sanity check - Port is active? Is the link active?
8597 	 * Is there any device attached?
8598 	 */
8599 	if ((cportinfo->cport_state &
8600 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
8601 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
8602 	    SATA_PORT_DEVLINK_UP) {
8603 		/*
8604 		 * Port in non-usable state or no link active/no device.
8605 		 * Free info structure if necessary (direct attached drive
8606 		 * only, for now!
8607 		 */
8608 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8609 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8610 		/* Add here differentiation for device attached or not */
8611 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8612 		mutex_exit(&cportinfo->cport_mutex);
8613 		if (sdinfo != NULL)
8614 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8615 		return (SATA_SUCCESS);
8616 	}
8617 
8618 	cportinfo->cport_state |= SATA_STATE_READY;
8619 	cportinfo->cport_dev_type = sata_device->satadev_type;
8620 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8621 
8622 	/*
8623 	 * If we are re-probing the port, there may be
8624 	 * sata_drive_info structure attached
8625 	 * (or sata_pm_info, if PMult is supported).
8626 	 */
8627 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
8628 		/*
8629 		 * There is no device, so remove device info structure,
8630 		 * if necessary. Direct attached drive only!
8631 		 */
8632 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8633 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8634 		if (sdinfo != NULL) {
8635 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8636 			sata_log(sata_hba_inst, CE_WARN,
8637 			    "SATA device detached "
8638 			    "from port %d", cportinfo->cport_addr.cport);
8639 		}
8640 		mutex_exit(&cportinfo->cport_mutex);
8641 		return (SATA_SUCCESS);
8642 	}
8643 
8644 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
8645 		if (sdinfo == NULL) {
8646 			/*
8647 			 * There is some device attached, but there is
8648 			 * no sata_drive_info structure - allocate one
8649 			 */
8650 			mutex_exit(&cportinfo->cport_mutex);
8651 			sdinfo = kmem_zalloc(
8652 			    sizeof (sata_drive_info_t), KM_SLEEP);
8653 			mutex_enter(&cportinfo->cport_mutex);
8654 			/*
8655 			 * Recheck, that the port state did not change when we
8656 			 * released mutex.
8657 			 */
8658 			if (cportinfo->cport_state & SATA_STATE_READY) {
8659 				SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo;
8660 				sdinfo->satadrv_addr = cportinfo->cport_addr;
8661 				sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT;
8662 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
8663 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
8664 			} else {
8665 				/*
8666 				 * Port is not in ready state, we
8667 				 * cannot attach a device.
8668 				 */
8669 				mutex_exit(&cportinfo->cport_mutex);
8670 				kmem_free(sdinfo, sizeof (sata_drive_info_t));
8671 				return (SATA_SUCCESS);
8672 			}
8673 			/*
8674 			 * Since we are adding device, presumably new one,
8675 			 * indicate that it  should be initalized,
8676 			 * as well as some internal framework states).
8677 			 */
8678 			init_device = B_TRUE;
8679 		}
8680 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8681 		sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
8682 	} else {
8683 		/*
8684 		 * The device is a port multiplier - not handled now.
8685 		 */
8686 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8687 		mutex_exit(&cportinfo->cport_mutex);
8688 		return (SATA_SUCCESS);
8689 	}
8690 	mutex_exit(&cportinfo->cport_mutex);
8691 	/*
8692 	 * Figure out what kind of device we are really
8693 	 * dealing with.
8694 	 */
8695 	rval = sata_probe_device(sata_hba_inst, sata_device);
8696 
8697 	if (rval == SATA_SUCCESS) {
8698 		/*
8699 		 * If we are dealing with the same type of a device as before,
8700 		 * restore its settings flags.
8701 		 */
8702 		if (sata_device->satadev_type == prev_device_type)
8703 			sdinfo->satadrv_settings = prev_device_settings;
8704 
8705 		/* Set initial device features, if necessary */
8706 		if (init_device == B_TRUE) {
8707 			rval = sata_initialize_device(sata_hba_inst, sdinfo);
8708 		}
8709 		if (rval == SATA_SUCCESS)
8710 			return (rval);
8711 	}
8712 
8713 	if (retry) {
8714 		clock_t cur_time = ddi_get_lbolt();
8715 		/*
8716 		 * A device was not successfully identified or initialized.
8717 		 * Track retry time for device identification.
8718 		 */
8719 		if ((cur_time - start_time) <
8720 		    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
8721 			/* sleep for a while */
8722 			delay(drv_usectohz(SATA_DEV_IDENTIFY_RTR_DLY));
8723 			goto retry_probe;
8724 		}
8725 	}
8726 	return (rval);
8727 }
8728 
8729 /*
8730  * Initialize device
8731  * Specified device is initialized to a default state.
8732  *
8733  * Returns SATA_SUCCESS if all device features are set successfully,
8734  * SATA_FAILURE otherwise
8735  */
8736 static int
8737 sata_initialize_device(sata_hba_inst_t *sata_hba_inst,
8738     sata_drive_info_t *sdinfo)
8739 {
8740 	int rval;
8741 
8742 	sata_save_drive_settings(sdinfo);
8743 
8744 	sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
8745 
8746 	sata_init_write_cache_mode(sdinfo);
8747 
8748 	rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0);
8749 
8750 	/* Determine current data transfer mode */
8751 	if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) {
8752 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
8753 	} else if ((sdinfo->satadrv_id.ai_validinfo &
8754 	    SATA_VALIDINFO_88) != 0 &&
8755 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) {
8756 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
8757 	} else if ((sdinfo->satadrv_id.ai_dworddma &
8758 	    SATA_MDMA_SEL_MASK) != 0) {
8759 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
8760 	} else
8761 		/* DMA supported, not no DMA transfer mode is selected !? */
8762 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
8763 
8764 	return (rval);
8765 }
8766 
8767 
8768 /*
8769  * Initialize write cache mode.
8770  *
8771  * The default write cache setting for SATA HDD is provided by sata_write_cache
8772  * static variable. ATAPI CD/DVDs devices have write cache default is
8773  * determined by sata_atapicdvd_write_cache static variable.
8774  * 1 - enable
8775  * 0 - disable
8776  * any other value - current drive setting
8777  *
8778  * Although there is not reason to disable write cache on CD/DVD devices,
8779  * the default setting control is provided for the maximun flexibility.
8780  *
8781  * In the future, it may be overridden by the
8782  * disk-write-cache-enable property setting, if it is defined.
8783  * Returns SATA_SUCCESS if all device features are set successfully,
8784  * SATA_FAILURE otherwise.
8785  */
8786 static void
8787 sata_init_write_cache_mode(sata_drive_info_t *sdinfo)
8788 {
8789 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
8790 		if (sata_write_cache == 1)
8791 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
8792 		else if (sata_write_cache == 0)
8793 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
8794 		/*
8795 		 * When sata_write_cache value is not 0 or 1,
8796 		 * a current setting of the drive's write cache is used.
8797 		 */
8798 	} else { /* Assume ATAPI CD/DVD device */
8799 		if (sata_atapicdvd_write_cache == 1)
8800 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
8801 		else if (sata_atapicdvd_write_cache == 0)
8802 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
8803 		/*
8804 		 * When sata_write_cache value is not 0 or 1,
8805 		 * a current setting of the drive's write cache is used.
8806 		 */
8807 	}
8808 }
8809 
8810 
8811 /*
8812  * Validate sata address.
8813  * Specified cport, pmport and qualifier has to match
8814  * passed sata_scsi configuration info.
8815  * The presence of an attached device is not verified.
8816  *
8817  * Returns 0 when address is valid, -1 otherwise.
8818  */
8819 static int
8820 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport,
8821 	int pmport, int qual)
8822 {
8823 	if (qual == SATA_ADDR_DCPORT && pmport != 0)
8824 		goto invalid_address;
8825 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
8826 		goto invalid_address;
8827 	if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) &&
8828 	    ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) ||
8829 	    (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) ||
8830 	    (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport))))
8831 		goto invalid_address;
8832 
8833 	return (0);
8834 
8835 invalid_address:
8836 	return (-1);
8837 
8838 }
8839 
8840 /*
8841  * Validate scsi address
8842  * SCSI target address is translated into SATA cport/pmport and compared
8843  * with a controller port/device configuration. LUN has to be 0.
8844  * Returns 0 if a scsi target refers to an attached device,
8845  * returns 1 if address is valid but device is not attached,
8846  * returns -1 if bad address or device is of an unsupported type.
8847  * Upon return sata_device argument is set.
8848  */
8849 static int
8850 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst,
8851 	struct scsi_address *ap, sata_device_t *sata_device)
8852 {
8853 	int cport, pmport, qual, rval;
8854 
8855 	rval = -1;	/* Invalid address */
8856 	if (ap->a_lun != 0)
8857 		goto out;
8858 
8859 	qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
8860 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
8861 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
8862 
8863 	if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT)
8864 		goto out;
8865 
8866 	if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) ==
8867 	    0) {
8868 
8869 		sata_cport_info_t *cportinfo;
8870 		sata_pmult_info_t *pmultinfo;
8871 		sata_drive_info_t *sdinfo = NULL;
8872 
8873 		rval = 1;	/* Valid sata address */
8874 
8875 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
8876 		if (qual == SATA_ADDR_DCPORT) {
8877 			if (cportinfo == NULL ||
8878 			    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
8879 				goto out;
8880 
8881 			if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT ||
8882 			    (cportinfo->cport_dev_type &
8883 			    SATA_VALID_DEV_TYPE) == 0) {
8884 				rval = -1;
8885 				goto out;
8886 			}
8887 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8888 
8889 		} else if (qual == SATA_ADDR_DPMPORT) {
8890 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
8891 			if (pmultinfo == NULL) {
8892 				rval = -1;
8893 				goto out;
8894 			}
8895 			if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) ==
8896 			    NULL ||
8897 			    SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
8898 			    pmport) == SATA_DTYPE_NONE)
8899 				goto out;
8900 
8901 			sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport,
8902 			    pmport);
8903 		} else {
8904 			rval = -1;
8905 			goto out;
8906 		}
8907 		if ((sdinfo == NULL) ||
8908 		    (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0)
8909 			goto out;
8910 
8911 		sata_device->satadev_type = sdinfo->satadrv_type;
8912 		sata_device->satadev_addr.qual = qual;
8913 		sata_device->satadev_addr.cport = cport;
8914 		sata_device->satadev_addr.pmport = pmport;
8915 		sata_device->satadev_rev = SATA_DEVICE_REV_1;
8916 		return (0);
8917 	}
8918 out:
8919 	if (rval == 1) {
8920 		SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
8921 		    "sata_validate_scsi_address: no valid target %x lun %x",
8922 		    ap->a_target, ap->a_lun);
8923 	}
8924 	return (rval);
8925 }
8926 
8927 /*
8928  * Find dip corresponding to passed device number
8929  *
8930  * Returns NULL if invalid device number is passed or device cannot be found,
8931  * Returns dip is device is found.
8932  */
8933 static dev_info_t *
8934 sata_devt_to_devinfo(dev_t dev)
8935 {
8936 	dev_info_t *dip;
8937 #ifndef __lock_lint
8938 	struct devnames *dnp;
8939 	major_t major = getmajor(dev);
8940 	int instance = SATA_MINOR2INSTANCE(getminor(dev));
8941 
8942 	if (major >= devcnt)
8943 		return (NULL);
8944 
8945 	dnp = &devnamesp[major];
8946 	LOCK_DEV_OPS(&(dnp->dn_lock));
8947 	dip = dnp->dn_head;
8948 	while (dip && (ddi_get_instance(dip) != instance)) {
8949 		dip = ddi_get_next(dip);
8950 	}
8951 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
8952 #endif
8953 
8954 	return (dip);
8955 }
8956 
8957 
8958 /*
8959  * Probe device.
8960  * This function issues Identify Device command and initializes local
8961  * sata_drive_info structure if the device can be identified.
8962  * The device type is determined by examining Identify Device
8963  * command response.
8964  * If the sata_hba_inst has linked drive info structure for this
8965  * device address, the Identify Device data is stored into sata_drive_info
8966  * structure linked to the port info structure.
8967  *
8968  * sata_device has to refer to the valid sata port(s) for HBA described
8969  * by sata_hba_inst structure.
8970  *
8971  * Returns:
8972  *	SATA_SUCCESS if device type was successfully probed and port-linked
8973  *		drive info structure was updated;
8974  * 	SATA_FAILURE if there is no device, or device was not probed
8975  *		successully;
8976  *	SATA_RETRY if device probe can be retried later.
8977  * If a device cannot be identified, sata_device's dev_state and dev_type
8978  * fields are set to unknown.
8979  * There are no retries in this function. Any retries should be managed by
8980  * the caller.
8981  */
8982 
8983 
8984 static int
8985 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
8986 {
8987 	sata_drive_info_t *sdinfo;
8988 	sata_drive_info_t new_sdinfo;	/* local drive info struct */
8989 	int rval;
8990 
8991 	ASSERT((SATA_CPORT_STATE(sata_hba_inst,
8992 	    sata_device->satadev_addr.cport) &
8993 	    (SATA_STATE_PROBED | SATA_STATE_READY)) != 0);
8994 
8995 	sata_device->satadev_type = SATA_DTYPE_NONE;
8996 
8997 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
8998 	    sata_device->satadev_addr.cport)));
8999 
9000 	/* Get pointer to port-linked sata device info structure */
9001 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9002 	if (sdinfo != NULL) {
9003 		sdinfo->satadrv_state &=
9004 		    ~(SATA_STATE_PROBED | SATA_STATE_READY);
9005 		sdinfo->satadrv_state |= SATA_STATE_PROBING;
9006 	} else {
9007 		/* No device to probe */
9008 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9009 		    sata_device->satadev_addr.cport)));
9010 		sata_device->satadev_type = SATA_DTYPE_NONE;
9011 		sata_device->satadev_state = SATA_STATE_UNKNOWN;
9012 		return (SATA_FAILURE);
9013 	}
9014 	/*
9015 	 * Need to issue both types of identify device command and
9016 	 * determine device type by examining retreived data/status.
9017 	 * First, ATA Identify Device.
9018 	 */
9019 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
9020 	new_sdinfo.satadrv_addr = sata_device->satadev_addr;
9021 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9022 	    sata_device->satadev_addr.cport)));
9023 	new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
9024 	rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
9025 	if (rval == SATA_RETRY) {
9026 		/* We may try to check for ATAPI device */
9027 		if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) {
9028 			/*
9029 			 * HBA supports ATAPI - try to issue Identify Packet
9030 			 * Device command.
9031 			 */
9032 			new_sdinfo.satadrv_type = SATA_DTYPE_ATAPICD;
9033 			rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
9034 		}
9035 	}
9036 	if (rval == SATA_SUCCESS) {
9037 		/*
9038 		 * Got something responding positively to ATA Identify Device
9039 		 * or to Identify Packet Device cmd.
9040 		 * Save last used device type.
9041 		 */
9042 		sata_device->satadev_type = new_sdinfo.satadrv_type;
9043 
9044 		/* save device info, if possible */
9045 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9046 		    sata_device->satadev_addr.cport)));
9047 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9048 		if (sdinfo == NULL) {
9049 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9050 			    sata_device->satadev_addr.cport)));
9051 			return (SATA_FAILURE);
9052 		}
9053 		/*
9054 		 * Copy drive info into the port-linked drive info structure.
9055 		 */
9056 		*sdinfo = new_sdinfo;
9057 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
9058 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
9059 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
9060 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
9061 			    sata_device->satadev_addr.cport) =
9062 			    sdinfo->satadrv_type;
9063 		else /* SATA_ADDR_DPMPORT */
9064 			SATA_PMPORT_DEV_TYPE(sata_hba_inst,
9065 			    sata_device->satadev_addr.cport,
9066 			    sata_device->satadev_addr.pmport) =
9067 			    sdinfo->satadrv_type;
9068 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9069 		    sata_device->satadev_addr.cport)));
9070 		return (SATA_SUCCESS);
9071 	}
9072 
9073 	/*
9074 	 * It may be SATA_RETRY or SATA_FAILURE return.
9075 	 * Looks like we cannot determine the device type at this time.
9076 	 */
9077 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9078 	    sata_device->satadev_addr.cport)));
9079 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9080 	if (sdinfo != NULL) {
9081 		sata_device->satadev_type = SATA_DTYPE_UNKNOWN;
9082 		sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9083 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
9084 		sdinfo->satadrv_state = SATA_STATE_PROBED;
9085 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
9086 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
9087 			    sata_device->satadev_addr.cport) =
9088 			    SATA_DTYPE_UNKNOWN;
9089 		else {
9090 			/* SATA_ADDR_DPMPORT */
9091 			if ((SATA_PMULT_INFO(sata_hba_inst,
9092 			    sata_device->satadev_addr.cport) != NULL) &&
9093 			    (SATA_PMPORT_INFO(sata_hba_inst,
9094 			    sata_device->satadev_addr.cport,
9095 			    sata_device->satadev_addr.pmport) != NULL))
9096 				SATA_PMPORT_DEV_TYPE(sata_hba_inst,
9097 				    sata_device->satadev_addr.cport,
9098 				    sata_device->satadev_addr.pmport) =
9099 				    SATA_DTYPE_UNKNOWN;
9100 		}
9101 	}
9102 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9103 	    sata_device->satadev_addr.cport)));
9104 	return (rval);
9105 }
9106 
9107 
9108 /*
9109  * Get pointer to sata_drive_info structure.
9110  *
9111  * The sata_device has to contain address (cport, pmport and qualifier) for
9112  * specified sata_scsi structure.
9113  *
9114  * Returns NULL if device address is not valid for this HBA configuration.
9115  * Otherwise, returns a pointer to sata_drive_info structure.
9116  *
9117  * This function should be called with a port mutex held.
9118  */
9119 static sata_drive_info_t *
9120 sata_get_device_info(sata_hba_inst_t *sata_hba_inst,
9121     sata_device_t *sata_device)
9122 {
9123 	uint8_t cport = sata_device->satadev_addr.cport;
9124 	uint8_t pmport = sata_device->satadev_addr.pmport;
9125 	uint8_t qual = sata_device->satadev_addr.qual;
9126 
9127 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
9128 		return (NULL);
9129 
9130 	if (!(SATA_CPORT_STATE(sata_hba_inst, cport) &
9131 	    (SATA_STATE_PROBED | SATA_STATE_READY)))
9132 		/* Port not probed yet */
9133 		return (NULL);
9134 
9135 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE)
9136 		return (NULL);
9137 
9138 	if (qual == SATA_ADDR_DCPORT) {
9139 		/* Request for a device on a controller port */
9140 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
9141 		    SATA_DTYPE_PMULT)
9142 			/* Port multiplier attached */
9143 			return (NULL);
9144 		return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport));
9145 	}
9146 	if (qual == SATA_ADDR_DPMPORT) {
9147 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
9148 		    SATA_DTYPE_PMULT)
9149 			return (NULL);
9150 
9151 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport))
9152 			return (NULL);
9153 
9154 		return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport));
9155 	}
9156 
9157 	/* we should not get here */
9158 	return (NULL);
9159 }
9160 
9161 
9162 /*
9163  * sata_identify_device.
9164  * Send Identify Device command to SATA HBA driver.
9165  * If command executes successfully, update sata_drive_info structure pointed
9166  * to by sdinfo argument, including Identify Device data.
9167  * If command fails, invalidate data in sata_drive_info.
9168  *
9169  * Cannot be called from interrupt level.
9170  *
9171  * Returns:
9172  * SATA_SUCCESS if the device was identified as a supported device,
9173  * SATA_RETRY if the device was not identified but could be retried,
9174  * SATA_FAILURE if the device was not identified and identify attempt
9175  *	should not be retried.
9176  */
9177 static int
9178 sata_identify_device(sata_hba_inst_t *sata_hba_inst,
9179     sata_drive_info_t *sdinfo)
9180 {
9181 	uint16_t cfg_word;
9182 	int rval;
9183 
9184 	/* fetch device identify data */
9185 	if ((rval = sata_fetch_device_identify_data(sata_hba_inst,
9186 	    sdinfo)) != 0)
9187 		goto fail_unknown;
9188 
9189 	cfg_word = sdinfo->satadrv_id.ai_config;
9190 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK &&
9191 	    (cfg_word & SATA_ATA_TYPE_MASK) != SATA_ATA_TYPE) {
9192 		/* Change device type to reflect Identify Device data */
9193 		if (((cfg_word & SATA_ATAPI_TYPE_MASK) ==
9194 		    SATA_ATAPI_TYPE) &&
9195 		    ((cfg_word & SATA_ATAPI_ID_DEV_TYPE) ==
9196 		    SATA_ATAPI_CDROM_DEV)) {
9197 			sdinfo->satadrv_type = SATA_DTYPE_ATAPICD;
9198 		} else {
9199 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9200 		}
9201 	} else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD &&
9202 	    (((cfg_word & SATA_ATAPI_TYPE_MASK) != SATA_ATAPI_TYPE) ||
9203 	    ((cfg_word & SATA_ATAPI_ID_DEV_TYPE) != SATA_ATAPI_CDROM_DEV))) {
9204 		/* Change device type to reflect Identify Device data ! */
9205 		if ((sdinfo->satadrv_id.ai_config & SATA_ATA_TYPE_MASK) ==
9206 		    SATA_ATA_TYPE) {
9207 			sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
9208 		} else {
9209 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9210 		}
9211 	}
9212 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9213 		if (sdinfo->satadrv_capacity == 0) {
9214 			/* Non-LBA disk. Too bad... */
9215 			sata_log(sata_hba_inst, CE_WARN,
9216 			    "SATA disk device at port %d does not support LBA",
9217 			    sdinfo->satadrv_addr.cport);
9218 			rval = SATA_FAILURE;
9219 			goto fail_unknown;
9220 		}
9221 	}
9222 #if 0
9223 	/* Left for historical reason */
9224 	/*
9225 	 * Some initial version of SATA spec indicated that at least
9226 	 * UDMA mode 4 has to be supported. It is not metioned in
9227 	 * SerialATA 2.6, so this restriction is removed.
9228 	 */
9229 	/* Check for Ultra DMA modes 6 through 0 being supported */
9230 	for (i = 6; i >= 0; --i) {
9231 		if (sdinfo->satadrv_id.ai_ultradma & (1 << i))
9232 			break;
9233 	}
9234 
9235 	/*
9236 	 * At least UDMA 4 mode has to be supported. If mode 4 or
9237 	 * higher are not supported by the device, fail this
9238 	 * device.
9239 	 */
9240 	if (i < 4) {
9241 		/* No required Ultra DMA mode supported */
9242 		sata_log(sata_hba_inst, CE_WARN,
9243 		    "SATA disk device at port %d does not support UDMA "
9244 		    "mode 4 or higher", sdinfo->satadrv_addr.cport);
9245 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9246 		    "mode 4 or higher required, %d supported", i));
9247 		rval = SATA_FAILURE;
9248 		goto fail_unknown;
9249 	}
9250 #endif
9251 
9252 	return (SATA_SUCCESS);
9253 
9254 fail_unknown:
9255 	/* Invalidate sata_drive_info ? */
9256 	sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9257 	sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
9258 	return (rval);
9259 }
9260 
9261 /*
9262  * Log/display device information
9263  */
9264 static void
9265 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst,
9266     sata_drive_info_t *sdinfo)
9267 {
9268 	int valid_version;
9269 	char msg_buf[MAXPATHLEN];
9270 	int i;
9271 
9272 	/* Show HBA path */
9273 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf);
9274 
9275 	cmn_err(CE_CONT, "?%s :\n", msg_buf);
9276 
9277 	if (sdinfo->satadrv_type == SATA_DTYPE_UNKNOWN) {
9278 		(void) sprintf(msg_buf,
9279 		    "Unsupported SATA device type (cfg 0x%x) at ",
9280 		    sdinfo->satadrv_id.ai_config);
9281 	} else {
9282 		(void) sprintf(msg_buf, "SATA %s device at",
9283 		    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
9284 		    "disk":"CD/DVD (ATAPI)");
9285 	}
9286 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT)
9287 		cmn_err(CE_CONT, "?\t%s port %d\n",
9288 		    msg_buf, sdinfo->satadrv_addr.cport);
9289 	else
9290 		cmn_err(CE_CONT, "?\t%s port %d pmport %d\n",
9291 		    msg_buf, sdinfo->satadrv_addr.cport,
9292 		    sdinfo->satadrv_addr.pmport);
9293 
9294 	bcopy(&sdinfo->satadrv_id.ai_model, msg_buf,
9295 	    sizeof (sdinfo->satadrv_id.ai_model));
9296 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model));
9297 	msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
9298 	cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf);
9299 
9300 	bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf,
9301 	    sizeof (sdinfo->satadrv_id.ai_fw));
9302 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw));
9303 	msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
9304 	cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf);
9305 
9306 	bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf,
9307 	    sizeof (sdinfo->satadrv_id.ai_drvser));
9308 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser));
9309 	msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
9310 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9311 		cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
9312 	} else {
9313 		/* Assuming ATAPI CD/DVD */
9314 		/*
9315 		 * SOme drives do not implement serial number and may
9316 		 * violate the spec by provinding spaces rather than zeros
9317 		 * in serial number field. Scan the buffer to detect it.
9318 		 */
9319 		for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) {
9320 			if (msg_buf[i] != '\0' && msg_buf[i] != ' ')
9321 				break;
9322 		}
9323 		if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) {
9324 			cmn_err(CE_CONT, "?\tserial number - none\n");
9325 		} else {
9326 			cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
9327 		}
9328 	}
9329 
9330 #ifdef SATA_DEBUG
9331 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
9332 	    sdinfo->satadrv_id.ai_majorversion != 0xffff) {
9333 		int i;
9334 		for (i = 14; i >= 2; i--) {
9335 			if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) {
9336 				valid_version = i;
9337 				break;
9338 			}
9339 		}
9340 		cmn_err(CE_CONT,
9341 		    "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
9342 		    valid_version,
9343 		    sdinfo->satadrv_id.ai_majorversion,
9344 		    sdinfo->satadrv_id.ai_minorversion);
9345 	}
9346 #endif
9347 	/* Log some info */
9348 	cmn_err(CE_CONT, "?\tsupported features:\n");
9349 	msg_buf[0] = '\0';
9350 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9351 		if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48)
9352 			(void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN);
9353 		else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28)
9354 			(void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN);
9355 	}
9356 	if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA)
9357 		(void) strlcat(msg_buf, "DMA", MAXPATHLEN);
9358 	if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ)
9359 		(void) strlcat(msg_buf, ", Native Command Queueing",
9360 		    MAXPATHLEN);
9361 	if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)
9362 		(void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN);
9363 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) &&
9364 	    (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED))
9365 		(void) strlcat(msg_buf, ", SMART", MAXPATHLEN);
9366 	if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) &&
9367 	    (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED))
9368 		(void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN);
9369 	cmn_err(CE_CONT, "?\t %s\n", msg_buf);
9370 	if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2)
9371 		cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n");
9372 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1)
9373 		cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n");
9374 	if (sdinfo->satadrv_features_support &
9375 	    (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) {
9376 		msg_buf[0] = '\0';
9377 		(void) snprintf(msg_buf, MAXPATHLEN,
9378 		    "Supported queue depth %d",
9379 		    sdinfo->satadrv_queue_depth);
9380 		if (!(sata_func_enable &
9381 		    (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ)))
9382 			(void) strlcat(msg_buf,
9383 			    " - queueing disabled globally", MAXPATHLEN);
9384 		else if (sdinfo->satadrv_queue_depth >
9385 		    sdinfo->satadrv_max_queue_depth) {
9386 			(void) snprintf(&msg_buf[strlen(msg_buf)],
9387 			    MAXPATHLEN - strlen(msg_buf), ", limited to %d",
9388 			    (int)sdinfo->satadrv_max_queue_depth);
9389 		}
9390 		cmn_err(CE_CONT, "?\t%s\n", msg_buf);
9391 	}
9392 
9393 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9394 #ifdef __i386
9395 		(void) sprintf(msg_buf, "\tcapacity = %llu sectors\n",
9396 		    sdinfo->satadrv_capacity);
9397 #else
9398 		(void) sprintf(msg_buf, "\tcapacity = %lu sectors\n",
9399 		    sdinfo->satadrv_capacity);
9400 #endif
9401 		cmn_err(CE_CONT, "?%s", msg_buf);
9402 	}
9403 }
9404 
9405 
9406 /*
9407  * sata_save_drive_settings extracts current setting of the device and stores
9408  * it for future reference, in case the device setup would need to be restored
9409  * after the device reset.
9410  *
9411  * For all devices read ahead and write cache settings are saved, if the
9412  * device supports these features at all.
9413  * For ATAPI devices the Removable Media Status Notification setting is saved.
9414  */
9415 static void
9416 sata_save_drive_settings(sata_drive_info_t *sdinfo)
9417 {
9418 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) ||
9419 	    (sdinfo->satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) {
9420 
9421 		/* Current setting of Read Ahead (and Read Cache) */
9422 		if (sdinfo->satadrv_id.ai_features85 & SATA_LOOK_AHEAD)
9423 			sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
9424 		else
9425 			sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD;
9426 
9427 		/* Current setting of Write Cache */
9428 		if (sdinfo->satadrv_id.ai_features85 & SATA_WRITE_CACHE)
9429 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
9430 		else
9431 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
9432 	}
9433 
9434 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
9435 		if (sdinfo->satadrv_id.ai_cmdset83 & SATA_RM_STATUS_NOTIFIC)
9436 			sdinfo->satadrv_settings |= SATA_DEV_RMSN;
9437 		else
9438 			sdinfo->satadrv_settings &= ~SATA_DEV_RMSN;
9439 	}
9440 }
9441 
9442 
9443 /*
9444  * sata_check_capacity function determines a disk capacity
9445  * and addressing mode (LBA28/LBA48) by examining a disk identify device data.
9446  *
9447  * NOTE: CHS mode is not supported! If a device does not support LBA,
9448  * this function is not called.
9449  *
9450  * Returns device capacity in number of blocks, i.e. largest addressable LBA+1
9451  */
9452 static uint64_t
9453 sata_check_capacity(sata_drive_info_t *sdinfo)
9454 {
9455 	uint64_t capacity = 0;
9456 	int i;
9457 
9458 	if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK ||
9459 	    !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT)
9460 		/* Capacity valid only for LBA-addressable disk devices */
9461 		return (0);
9462 
9463 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) &&
9464 	    (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) &&
9465 	    (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) {
9466 		/* LBA48 mode supported and enabled */
9467 		sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 |
9468 		    SATA_DEV_F_LBA28;
9469 		for (i = 3;  i >= 0;  --i) {
9470 			capacity <<= 16;
9471 			capacity += sdinfo->satadrv_id.ai_addrsecxt[i];
9472 		}
9473 	} else {
9474 		capacity = sdinfo->satadrv_id.ai_addrsec[1];
9475 		capacity <<= 16;
9476 		capacity += sdinfo->satadrv_id.ai_addrsec[0];
9477 		if (capacity >= 0x1000000)
9478 			/* LBA28 mode */
9479 			sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28;
9480 	}
9481 	return (capacity);
9482 }
9483 
9484 
9485 /*
9486  * Allocate consistent buffer for DMA transfer
9487  *
9488  * Cannot be called from interrupt level or with mutex held - it may sleep.
9489  *
9490  * Returns pointer to allocated buffer structure, or NULL if allocation failed.
9491  */
9492 static struct buf *
9493 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len)
9494 {
9495 	struct scsi_address ap;
9496 	struct buf *bp;
9497 	ddi_dma_attr_t	cur_dma_attr;
9498 
9499 	ASSERT(spx->txlt_sata_pkt != NULL);
9500 	ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran;
9501 	ap.a_target = SATA_TO_SCSI_TARGET(
9502 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport,
9503 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport,
9504 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual);
9505 	ap.a_lun = 0;
9506 
9507 	bp = scsi_alloc_consistent_buf(&ap, NULL, len,
9508 	    B_READ, SLEEP_FUNC, NULL);
9509 
9510 	if (bp != NULL) {
9511 		/* Allocate DMA resources for this buffer */
9512 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
9513 		/*
9514 		 * We use a local version of the dma_attr, to account
9515 		 * for a device addressing limitations.
9516 		 * sata_adjust_dma_attr() will handle sdinfo == NULL which
9517 		 * will cause dma attributes to be adjusted to a lowest
9518 		 * acceptable level.
9519 		 */
9520 		sata_adjust_dma_attr(NULL,
9521 		    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
9522 
9523 		if (sata_dma_buf_setup(spx, PKT_CONSISTENT,
9524 		    SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) {
9525 			scsi_free_consistent_buf(bp);
9526 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
9527 			bp = NULL;
9528 		}
9529 	}
9530 	return (bp);
9531 }
9532 
9533 /*
9534  * Release local buffer (consistent buffer for DMA transfer) allocated
9535  * via sata_alloc_local_buffer().
9536  */
9537 static void
9538 sata_free_local_buffer(sata_pkt_txlate_t *spx)
9539 {
9540 	ASSERT(spx->txlt_sata_pkt != NULL);
9541 	ASSERT(spx->txlt_dma_cookie_list != NULL);
9542 	ASSERT(spx->txlt_dma_cookie_list_len != 0);
9543 	ASSERT(spx->txlt_buf_dma_handle != NULL);
9544 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL);
9545 
9546 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0;
9547 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL;
9548 
9549 	/* Free DMA resources */
9550 	(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
9551 	ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
9552 	spx->txlt_buf_dma_handle = 0;
9553 
9554 	if (spx->txlt_dma_cookie_list != &spx->txlt_dma_cookie) {
9555 		kmem_free(spx->txlt_dma_cookie_list,
9556 		    spx->txlt_dma_cookie_list_len * sizeof (ddi_dma_cookie_t));
9557 		spx->txlt_dma_cookie_list = NULL;
9558 		spx->txlt_dma_cookie_list_len = 0;
9559 	}
9560 	/* Free buffer */
9561 	scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp);
9562 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
9563 }
9564 
9565 
9566 
9567 
9568 /*
9569  * Allocate sata_pkt
9570  * Pkt structure version and embedded strcutures version are initialized.
9571  * sata_pkt and sata_pkt_txlate structures are cross-linked.
9572  *
9573  * Since this may be called in interrupt context by sata_scsi_init_pkt,
9574  * callback argument determines if it can sleep or not.
9575  * Hence, it should not be called from interrupt context.
9576  *
9577  * If successful, non-NULL pointer to a sata pkt is returned.
9578  * Upon failure, NULL pointer is returned.
9579  */
9580 static sata_pkt_t *
9581 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t))
9582 {
9583 	sata_pkt_t *spkt;
9584 	int kmsflag;
9585 
9586 	kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
9587 	spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag);
9588 	if (spkt == NULL) {
9589 		SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9590 		    "sata_pkt_alloc: failed"));
9591 		return (NULL);
9592 	}
9593 	spkt->satapkt_rev = SATA_PKT_REV;
9594 	spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV;
9595 	spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
9596 	spkt->satapkt_framework_private = spx;
9597 	spx->txlt_sata_pkt = spkt;
9598 	return (spkt);
9599 }
9600 
9601 /*
9602  * Free sata pkt allocated via sata_pkt_alloc()
9603  */
9604 static void
9605 sata_pkt_free(sata_pkt_txlate_t *spx)
9606 {
9607 	ASSERT(spx->txlt_sata_pkt != NULL);
9608 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL);
9609 	kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t));
9610 	spx->txlt_sata_pkt = NULL;
9611 }
9612 
9613 
9614 /*
9615  * Adjust DMA attributes.
9616  * SCSI cmds block count is up to 24 bits, SATA cmd block count vary
9617  * from 8 bits to 16 bits, depending on a command being used.
9618  * Limiting max block count arbitrarily to 256 for all read/write
9619  * commands may affects performance, so check both the device and
9620  * controller capability before adjusting dma attributes.
9621  */
9622 void
9623 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr,
9624     ddi_dma_attr_t *adj_dma_attr)
9625 {
9626 	uint32_t count_max;
9627 
9628 	/* Copy original attributes */
9629 	*adj_dma_attr = *dma_attr;
9630 	/*
9631 	 * Things to consider: device addressing capability,
9632 	 * "excessive" controller DMA capabilities.
9633 	 * If a device is being probed/initialized, there are
9634 	 * no device info - use default limits then.
9635 	 */
9636 	if (sdinfo == NULL) {
9637 		count_max = dma_attr->dma_attr_granular * 0x100;
9638 		if (dma_attr->dma_attr_count_max > count_max)
9639 			adj_dma_attr->dma_attr_count_max = count_max;
9640 		if (dma_attr->dma_attr_maxxfer > count_max)
9641 			adj_dma_attr->dma_attr_maxxfer = count_max;
9642 		return;
9643 	}
9644 
9645 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9646 		if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) {
9647 			/*
9648 			 * 16-bit sector count may be used - we rely on
9649 			 * the assumption that only read and write cmds
9650 			 * will request more than 256 sectors worth of data
9651 			 */
9652 			count_max = adj_dma_attr->dma_attr_granular * 0x10000;
9653 		} else {
9654 			/*
9655 			 * 8-bit sector count will be used - default limits
9656 			 * for dma attributes
9657 			 */
9658 			count_max = adj_dma_attr->dma_attr_granular * 0x100;
9659 		}
9660 		/*
9661 		 * Adjust controler dma attributes, if necessary
9662 		 */
9663 		if (dma_attr->dma_attr_count_max > count_max)
9664 			adj_dma_attr->dma_attr_count_max = count_max;
9665 		if (dma_attr->dma_attr_maxxfer > count_max)
9666 			adj_dma_attr->dma_attr_maxxfer = count_max;
9667 	}
9668 }
9669 
9670 
9671 /*
9672  * Allocate DMA resources for the buffer
9673  * This function handles initial DMA resource allocation as well as
9674  * DMA window shift and may be called repeatedly for the same DMA window
9675  * until all DMA cookies in the DMA window are processed.
9676  * To guarantee that there is always a coherent set of cookies to process
9677  * by SATA HBA driver (observing alignment, device granularity, etc.),
9678  * the number of slots for DMA cookies is equal to lesser of  a number of
9679  * cookies in a DMA window and a max number of scatter/gather entries.
9680  *
9681  * Returns DDI_SUCCESS upon successful operation.
9682  * Return failure code of a failing command or DDI_FAILURE when
9683  * internal cleanup failed.
9684  */
9685 static int
9686 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags,
9687     int (*callback)(caddr_t), caddr_t arg,
9688     ddi_dma_attr_t *cur_dma_attr)
9689 {
9690 	int	rval;
9691 	off_t	offset;
9692 	size_t	size;
9693 	int	max_sg_len, req_len, i;
9694 	uint_t	dma_flags;
9695 	struct buf	*bp;
9696 	uint64_t	cur_txfer_len;
9697 
9698 
9699 	ASSERT(spx->txlt_sata_pkt != NULL);
9700 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9701 	ASSERT(bp != NULL);
9702 
9703 
9704 	if (spx->txlt_buf_dma_handle == NULL) {
9705 		/*
9706 		 * No DMA resources allocated so far - this is a first call
9707 		 * for this sata pkt.
9708 		 */
9709 		rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst),
9710 		    cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle);
9711 
9712 		if (rval != DDI_SUCCESS) {
9713 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9714 			    "sata_dma_buf_setup: no buf DMA resources %x",
9715 			    rval));
9716 			return (rval);
9717 		}
9718 
9719 		if (bp->b_flags & B_READ)
9720 			dma_flags = DDI_DMA_READ;
9721 		else
9722 			dma_flags = DDI_DMA_WRITE;
9723 
9724 		if (flags & PKT_CONSISTENT)
9725 			dma_flags |= DDI_DMA_CONSISTENT;
9726 
9727 		if (flags & PKT_DMA_PARTIAL)
9728 			dma_flags |= DDI_DMA_PARTIAL;
9729 
9730 		/*
9731 		 * Check buffer alignment and size against dma attributes
9732 		 * Consider dma_attr_align only. There may be requests
9733 		 * with the size lower than device granularity, but they
9734 		 * will not read/write from/to the device, so no adjustment
9735 		 * is necessary. The dma_attr_minxfer theoretically should
9736 		 * be considered, but no HBA driver is checking it.
9737 		 */
9738 		if (IS_P2ALIGNED(bp->b_un.b_addr,
9739 		    cur_dma_attr->dma_attr_align)) {
9740 			rval = ddi_dma_buf_bind_handle(
9741 			    spx->txlt_buf_dma_handle,
9742 			    bp, dma_flags, callback, arg,
9743 			    &spx->txlt_dma_cookie,
9744 			    &spx->txlt_curwin_num_dma_cookies);
9745 		} else { /* Buffer is not aligned */
9746 
9747 			int	(*ddicallback)(caddr_t);
9748 			size_t	bufsz;
9749 
9750 			/* Check id sleeping is allowed */
9751 			ddicallback = (callback == NULL_FUNC) ?
9752 			    DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
9753 
9754 			SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
9755 			    "mis-aligned buffer: addr=0x%p, cnt=%lu",
9756 			    (void *)bp->b_un.b_addr, bp->b_bcount);
9757 
9758 			if (bp->b_flags & (B_PAGEIO|B_PHYS))
9759 				/*
9760 				 * CPU will need to access data in the buffer
9761 				 * (for copying) so map it.
9762 				 */
9763 				bp_mapin(bp);
9764 
9765 			ASSERT(spx->txlt_tmp_buf == NULL);
9766 
9767 			/* Buffer may be padded by ddi_dma_mem_alloc()! */
9768 			rval = ddi_dma_mem_alloc(
9769 			    spx->txlt_buf_dma_handle,
9770 			    bp->b_bcount,
9771 			    &sata_acc_attr,
9772 			    DDI_DMA_STREAMING,
9773 			    ddicallback, NULL,
9774 			    &spx->txlt_tmp_buf,
9775 			    &bufsz,
9776 			    &spx->txlt_tmp_buf_handle);
9777 
9778 			if (rval != DDI_SUCCESS) {
9779 				/* DMA mapping failed */
9780 				(void) ddi_dma_free_handle(
9781 				    &spx->txlt_buf_dma_handle);
9782 				spx->txlt_buf_dma_handle = NULL;
9783 #ifdef SATA_DEBUG
9784 				mbuffail_count++;
9785 #endif
9786 				SATADBG1(SATA_DBG_DMA_SETUP,
9787 				    spx->txlt_sata_hba_inst,
9788 				    "sata_dma_buf_setup: "
9789 				    "buf dma mem alloc failed %x\n", rval);
9790 				return (rval);
9791 			}
9792 			ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf,
9793 			    cur_dma_attr->dma_attr_align));
9794 
9795 #ifdef SATA_DEBUG
9796 			mbuf_count++;
9797 
9798 			if (bp->b_bcount != bufsz)
9799 				/*
9800 				 * This will require special handling, because
9801 				 * DMA cookies will be based on the temporary
9802 				 * buffer size, not the original buffer
9803 				 * b_bcount, so the residue may have to
9804 				 * be counted differently.
9805 				 */
9806 				SATADBG2(SATA_DBG_DMA_SETUP,
9807 				    spx->txlt_sata_hba_inst,
9808 				    "sata_dma_buf_setup: bp size %x != "
9809 				    "bufsz %x\n", bp->b_bcount, bufsz);
9810 #endif
9811 			if (dma_flags & DDI_DMA_WRITE) {
9812 				/*
9813 				 * Write operation - copy data into
9814 				 * an aligned temporary buffer. Buffer will be
9815 				 * synced for device by ddi_dma_addr_bind_handle
9816 				 */
9817 				bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf,
9818 				    bp->b_bcount);
9819 			}
9820 
9821 			rval = ddi_dma_addr_bind_handle(
9822 			    spx->txlt_buf_dma_handle,
9823 			    NULL,
9824 			    spx->txlt_tmp_buf,
9825 			    bufsz, dma_flags, ddicallback, 0,
9826 			    &spx->txlt_dma_cookie,
9827 			    &spx->txlt_curwin_num_dma_cookies);
9828 		}
9829 
9830 		switch (rval) {
9831 		case DDI_DMA_PARTIAL_MAP:
9832 			SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
9833 			    "sata_dma_buf_setup: DMA Partial Map\n", NULL);
9834 			/*
9835 			 * Partial DMA mapping.
9836 			 * Retrieve number of DMA windows for this request.
9837 			 */
9838 			if (ddi_dma_numwin(spx->txlt_buf_dma_handle,
9839 			    &spx->txlt_num_dma_win) != DDI_SUCCESS) {
9840 				if (spx->txlt_tmp_buf != NULL) {
9841 					ddi_dma_mem_free(
9842 					    &spx->txlt_tmp_buf_handle);
9843 					spx->txlt_tmp_buf = NULL;
9844 				}
9845 				(void) ddi_dma_unbind_handle(
9846 				    spx->txlt_buf_dma_handle);
9847 				(void) ddi_dma_free_handle(
9848 				    &spx->txlt_buf_dma_handle);
9849 				spx->txlt_buf_dma_handle = NULL;
9850 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9851 				    "sata_dma_buf_setup: numwin failed\n"));
9852 				return (DDI_FAILURE);
9853 			}
9854 			SATADBG2(SATA_DBG_DMA_SETUP,
9855 			    spx->txlt_sata_hba_inst,
9856 			    "sata_dma_buf_setup: windows: %d, cookies: %d\n",
9857 			    spx->txlt_num_dma_win,
9858 			    spx->txlt_curwin_num_dma_cookies);
9859 			spx->txlt_cur_dma_win = 0;
9860 			break;
9861 
9862 		case DDI_DMA_MAPPED:
9863 			/* DMA fully mapped */
9864 			spx->txlt_num_dma_win = 1;
9865 			spx->txlt_cur_dma_win = 0;
9866 			SATADBG1(SATA_DBG_DMA_SETUP,
9867 			    spx->txlt_sata_hba_inst,
9868 			    "sata_dma_buf_setup: windows: 1 "
9869 			    "cookies: %d\n", spx->txlt_curwin_num_dma_cookies);
9870 			break;
9871 
9872 		default:
9873 			/* DMA mapping failed */
9874 			if (spx->txlt_tmp_buf != NULL) {
9875 				ddi_dma_mem_free(
9876 				    &spx->txlt_tmp_buf_handle);
9877 				spx->txlt_tmp_buf = NULL;
9878 			}
9879 			(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
9880 			spx->txlt_buf_dma_handle = NULL;
9881 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9882 			    "sata_dma_buf_setup: buf dma handle binding "
9883 			    "failed %x\n", rval));
9884 			return (rval);
9885 		}
9886 		spx->txlt_curwin_processed_dma_cookies = 0;
9887 		spx->txlt_dma_cookie_list = NULL;
9888 	} else {
9889 		/*
9890 		 * DMA setup is reused. Check if we need to process more
9891 		 * cookies in current window, or to get next window, if any.
9892 		 */
9893 
9894 		ASSERT(spx->txlt_curwin_processed_dma_cookies <=
9895 		    spx->txlt_curwin_num_dma_cookies);
9896 
9897 		if (spx->txlt_curwin_processed_dma_cookies ==
9898 		    spx->txlt_curwin_num_dma_cookies) {
9899 			/*
9900 			 * All cookies from current DMA window were processed.
9901 			 * Get next DMA window.
9902 			 */
9903 			spx->txlt_cur_dma_win++;
9904 			if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) {
9905 				(void) ddi_dma_getwin(spx->txlt_buf_dma_handle,
9906 				    spx->txlt_cur_dma_win, &offset, &size,
9907 				    &spx->txlt_dma_cookie,
9908 				    &spx->txlt_curwin_num_dma_cookies);
9909 				spx->txlt_curwin_processed_dma_cookies = 0;
9910 			} else {
9911 				/* No more windows! End of request! */
9912 				/* What to do? - panic for now */
9913 				ASSERT(spx->txlt_cur_dma_win >=
9914 				    spx->txlt_num_dma_win);
9915 
9916 				spx->txlt_curwin_num_dma_cookies = 0;
9917 				spx->txlt_curwin_processed_dma_cookies = 0;
9918 				spx->txlt_sata_pkt->
9919 				    satapkt_cmd.satacmd_num_dma_cookies = 0;
9920 				return (DDI_SUCCESS);
9921 			}
9922 		}
9923 	}
9924 	/* There better be at least one DMA cookie outstanding */
9925 	ASSERT((spx->txlt_curwin_num_dma_cookies -
9926 	    spx->txlt_curwin_processed_dma_cookies) > 0);
9927 
9928 	if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) {
9929 		/* The default cookie slot was used in previous run */
9930 		ASSERT(spx->txlt_curwin_processed_dma_cookies == 0);
9931 		spx->txlt_dma_cookie_list = NULL;
9932 		spx->txlt_dma_cookie_list_len = 0;
9933 	}
9934 	if (spx->txlt_curwin_processed_dma_cookies == 0) {
9935 		/*
9936 		 * Processing a new DMA window - set-up dma cookies list.
9937 		 * We may reuse previously allocated cookie array if it is
9938 		 * possible.
9939 		 */
9940 		if (spx->txlt_dma_cookie_list != NULL &&
9941 		    spx->txlt_dma_cookie_list_len <
9942 		    spx->txlt_curwin_num_dma_cookies) {
9943 			/*
9944 			 * New DMA window contains more cookies than
9945 			 * the previous one. We need larger cookie list - free
9946 			 * the old one.
9947 			 */
9948 			(void) kmem_free(spx->txlt_dma_cookie_list,
9949 			    spx->txlt_dma_cookie_list_len *
9950 			    sizeof (ddi_dma_cookie_t));
9951 			spx->txlt_dma_cookie_list = NULL;
9952 			spx->txlt_dma_cookie_list_len = 0;
9953 		}
9954 		if (spx->txlt_dma_cookie_list == NULL) {
9955 			/*
9956 			 * Calculate lesser of number of cookies in this
9957 			 * DMA window and number of s/g entries.
9958 			 */
9959 			max_sg_len = cur_dma_attr->dma_attr_sgllen;
9960 			req_len = MIN(max_sg_len,
9961 			    spx->txlt_curwin_num_dma_cookies);
9962 
9963 			/* Allocate new dma cookie array if necessary */
9964 			if (req_len == 1) {
9965 				/* Only one cookie - no need for a list */
9966 				spx->txlt_dma_cookie_list =
9967 				    &spx->txlt_dma_cookie;
9968 				spx->txlt_dma_cookie_list_len = 1;
9969 			} else {
9970 				/*
9971 				 * More than one cookie - try to allocate space.
9972 				 */
9973 				spx->txlt_dma_cookie_list = kmem_zalloc(
9974 				    sizeof (ddi_dma_cookie_t) * req_len,
9975 				    callback == NULL_FUNC ? KM_NOSLEEP :
9976 				    KM_SLEEP);
9977 				if (spx->txlt_dma_cookie_list == NULL) {
9978 					SATADBG1(SATA_DBG_DMA_SETUP,
9979 					    spx->txlt_sata_hba_inst,
9980 					    "sata_dma_buf_setup: cookie list "
9981 					    "allocation failed\n", NULL);
9982 					/*
9983 					 * We could not allocate space for
9984 					 * neccessary number of dma cookies in
9985 					 * this window, so we fail this request.
9986 					 * Next invocation would try again to
9987 					 * allocate space for cookie list.
9988 					 * Note:Packet residue was not modified.
9989 					 */
9990 					return (DDI_DMA_NORESOURCES);
9991 				} else {
9992 					spx->txlt_dma_cookie_list_len = req_len;
9993 				}
9994 			}
9995 		}
9996 		/*
9997 		 * Fetch DMA cookies into cookie list in sata_pkt_txlate.
9998 		 * First cookie was already fetched.
9999 		 */
10000 		*(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie;
10001 		cur_txfer_len =
10002 		    (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size;
10003 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1;
10004 		spx->txlt_curwin_processed_dma_cookies++;
10005 		for (i = 1; (i < spx->txlt_dma_cookie_list_len) &&
10006 		    (i < spx->txlt_curwin_num_dma_cookies); i++) {
10007 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
10008 			    &spx->txlt_dma_cookie_list[i]);
10009 			cur_txfer_len +=
10010 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
10011 			spx->txlt_curwin_processed_dma_cookies++;
10012 			spx->txlt_sata_pkt->
10013 			    satapkt_cmd.satacmd_num_dma_cookies += 1;
10014 		}
10015 	} else {
10016 		SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
10017 		    "sata_dma_buf_setup: sliding within DMA window, "
10018 		    "cur cookie %d, total cookies %d\n",
10019 		    spx->txlt_curwin_processed_dma_cookies,
10020 		    spx->txlt_curwin_num_dma_cookies);
10021 
10022 		/*
10023 		 * Not all cookies from the current dma window were used because
10024 		 * of s/g limitation.
10025 		 * There is no need to re-size the list - it was set at
10026 		 * optimal size, or only default entry is used (s/g = 1).
10027 		 */
10028 		if (spx->txlt_dma_cookie_list == NULL) {
10029 			spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie;
10030 			spx->txlt_dma_cookie_list_len = 1;
10031 		}
10032 		/*
10033 		 * Since we are processing remaining cookies in a DMA window,
10034 		 * there may be less of them than the number of entries in the
10035 		 * current dma cookie list.
10036 		 */
10037 		req_len = MIN(spx->txlt_dma_cookie_list_len,
10038 		    (spx->txlt_curwin_num_dma_cookies -
10039 		    spx->txlt_curwin_processed_dma_cookies));
10040 
10041 		/* Fetch the next batch of cookies */
10042 		for (i = 0, cur_txfer_len = 0; i < req_len; i++) {
10043 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
10044 			    &spx->txlt_dma_cookie_list[i]);
10045 			cur_txfer_len +=
10046 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
10047 			spx->txlt_sata_pkt->
10048 			    satapkt_cmd.satacmd_num_dma_cookies++;
10049 			spx->txlt_curwin_processed_dma_cookies++;
10050 		}
10051 	}
10052 
10053 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0);
10054 
10055 	/* Point sata_cmd to the cookie list */
10056 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list =
10057 	    &spx->txlt_dma_cookie_list[0];
10058 
10059 	/* Remember number of DMA cookies passed in sata packet */
10060 	spx->txlt_num_dma_cookies =
10061 	    spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies;
10062 
10063 	ASSERT(cur_txfer_len != 0);
10064 	if (cur_txfer_len <= bp->b_bcount)
10065 		spx->txlt_total_residue -= cur_txfer_len;
10066 	else {
10067 		/*
10068 		 * Temporary DMA buffer has been padded by
10069 		 * ddi_dma_mem_alloc()!
10070 		 * This requires special handling, because DMA cookies are
10071 		 * based on the temporary buffer size, not the b_bcount,
10072 		 * and we have extra bytes to transfer - but the packet
10073 		 * residue has to stay correct because we will copy only
10074 		 * the requested number of bytes.
10075 		 */
10076 		spx->txlt_total_residue -= bp->b_bcount;
10077 	}
10078 
10079 	return (DDI_SUCCESS);
10080 }
10081 
10082 
10083 /*
10084  * Fetch Device Identify data.
10085  * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type)
10086  * command to a device and get the device identify data.
10087  * The device_info structure has to be set to device type (for selecting proper
10088  * device identify command).
10089  *
10090  * Returns:
10091  * SATA_SUCCESS if cmd succeeded
10092  * SATA_RETRY if cmd was rejected and could be retried,
10093  * SATA_FAILURE if cmd failed and should not be retried (port error)
10094  *
10095  * Cannot be called in an interrupt context.
10096  */
10097 
10098 static int
10099 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst,
10100     sata_drive_info_t *sdinfo)
10101 {
10102 	struct buf *bp;
10103 	sata_pkt_t *spkt;
10104 	sata_cmd_t *scmd;
10105 	sata_pkt_txlate_t *spx;
10106 	int rval;
10107 
10108 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10109 	spx->txlt_sata_hba_inst = sata_hba_inst;
10110 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
10111 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10112 	if (spkt == NULL) {
10113 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10114 		return (SATA_RETRY); /* may retry later */
10115 	}
10116 	/* address is needed now */
10117 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10118 
10119 	/*
10120 	 * Allocate buffer for Identify Data return data
10121 	 */
10122 	bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t));
10123 	if (bp == NULL) {
10124 		sata_pkt_free(spx);
10125 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10126 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10127 		    "sata_fetch_device_identify_data: "
10128 		    "cannot allocate buffer for ID"));
10129 		return (SATA_RETRY); /* may retry later */
10130 	}
10131 
10132 	/* Fill sata_pkt */
10133 	sdinfo->satadrv_state = SATA_STATE_PROBING;
10134 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10135 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10136 	/* Synchronous mode, no callback */
10137 	spkt->satapkt_comp = NULL;
10138 	/* Timeout 30s */
10139 	spkt->satapkt_time = sata_default_pkt_time;
10140 
10141 	scmd = &spkt->satapkt_cmd;
10142 	scmd->satacmd_bp = bp;
10143 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
10144 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10145 
10146 	/* Build Identify Device cmd in the sata_pkt */
10147 	scmd->satacmd_addr_type = 0;		/* N/A */
10148 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
10149 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
10150 	scmd->satacmd_lba_mid_lsb = 0;		/* N/A */
10151 	scmd->satacmd_lba_high_lsb = 0;		/* N/A */
10152 	scmd->satacmd_features_reg = 0;		/* N/A */
10153 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
10154 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
10155 		/* Identify Packet Device cmd */
10156 		scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE;
10157 	} else {
10158 		/* Identify Device cmd - mandatory for all other devices */
10159 		scmd->satacmd_cmd_reg = SATAC_ID_DEVICE;
10160 	}
10161 
10162 	/* Send pkt to SATA HBA driver */
10163 	rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt);
10164 	if (rval == SATA_TRAN_ACCEPTED &&
10165 	    spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10166 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10167 		    DDI_DMA_SYNC_FORKERNEL);
10168 		ASSERT(rval == DDI_SUCCESS);
10169 		if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config &
10170 		    SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) {
10171 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10172 			    "SATA disk device at port %d - "
10173 			    "partial Identify Data",
10174 			    sdinfo->satadrv_addr.cport));
10175 			rval = SATA_RETRY; /* may retry later */
10176 			goto fail;
10177 		}
10178 		/* Update sata_drive_info */
10179 		bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id,
10180 		    sizeof (sata_id_t));
10181 
10182 		sdinfo->satadrv_features_support = 0;
10183 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10184 			/*
10185 			 * Retrieve capacity (disks only) and addressing mode
10186 			 */
10187 			sdinfo->satadrv_capacity = sata_check_capacity(sdinfo);
10188 		} else {
10189 			/*
10190 			 * For ATAPI devices one would have to issue
10191 			 * Get Capacity cmd for media capacity. Not here.
10192 			 */
10193 			sdinfo->satadrv_capacity = 0;
10194 			/*
10195 			 * Check what cdb length is supported
10196 			 */
10197 			if ((sdinfo->satadrv_id.ai_config &
10198 			    SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B)
10199 				sdinfo->satadrv_atapi_cdb_len = 16;
10200 			else
10201 				sdinfo->satadrv_atapi_cdb_len = 12;
10202 		}
10203 		/* Setup supported features flags */
10204 		if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT)
10205 			sdinfo->satadrv_features_support |= SATA_DEV_F_DMA;
10206 
10207 		/* Check for SATA GEN and NCQ support */
10208 		if (sdinfo->satadrv_id.ai_satacap != 0 &&
10209 		    sdinfo->satadrv_id.ai_satacap != 0xffff) {
10210 			/* SATA compliance */
10211 			if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ)
10212 				sdinfo->satadrv_features_support |=
10213 				    SATA_DEV_F_NCQ;
10214 			if (sdinfo->satadrv_id.ai_satacap &
10215 			    (SATA_1_SPEED | SATA_2_SPEED)) {
10216 				if (sdinfo->satadrv_id.ai_satacap &
10217 				    SATA_2_SPEED)
10218 					sdinfo->satadrv_features_support |=
10219 					    SATA_DEV_F_SATA2;
10220 				if (sdinfo->satadrv_id.ai_satacap &
10221 				    SATA_1_SPEED)
10222 					sdinfo->satadrv_features_support |=
10223 					    SATA_DEV_F_SATA1;
10224 			} else {
10225 				sdinfo->satadrv_features_support |=
10226 				    SATA_DEV_F_SATA1;
10227 			}
10228 		}
10229 		if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) &&
10230 		    (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD))
10231 			sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ;
10232 
10233 		sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth;
10234 		if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) ||
10235 		    (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) {
10236 			++sdinfo->satadrv_queue_depth;
10237 			/* Adjust according to controller capabilities */
10238 			sdinfo->satadrv_max_queue_depth = MIN(
10239 			    sdinfo->satadrv_queue_depth,
10240 			    SATA_QDEPTH(sata_hba_inst));
10241 			/* Adjust according to global queue depth limit */
10242 			sdinfo->satadrv_max_queue_depth = MIN(
10243 			    sdinfo->satadrv_max_queue_depth,
10244 			    sata_current_max_qdepth);
10245 			if (sdinfo->satadrv_max_queue_depth == 0)
10246 				sdinfo->satadrv_max_queue_depth = 1;
10247 		} else
10248 			sdinfo->satadrv_max_queue_depth = 1;
10249 
10250 		rval = SATA_SUCCESS;
10251 	} else {
10252 		/*
10253 		 * Woops, no Identify Data.
10254 		 */
10255 		if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) {
10256 			rval = SATA_RETRY; /* may retry later */
10257 		} else if (rval == SATA_TRAN_ACCEPTED) {
10258 			if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR ||
10259 			    spkt->satapkt_reason == SATA_PKT_ABORTED ||
10260 			    spkt->satapkt_reason == SATA_PKT_TIMEOUT ||
10261 			    spkt->satapkt_reason == SATA_PKT_RESET)
10262 				rval = SATA_RETRY; /* may retry later */
10263 			else
10264 				rval = SATA_FAILURE;
10265 		} else {
10266 			rval = SATA_FAILURE;
10267 		}
10268 	}
10269 fail:
10270 	/* Free allocated resources */
10271 	sata_free_local_buffer(spx);
10272 	sata_pkt_free(spx);
10273 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
10274 
10275 	return (rval);
10276 }
10277 
10278 
10279 /*
10280  * Some devices may not come-up with default DMA mode (UDMA or MWDMA).
10281  * UDMA mode is checked first, followed by MWDMA mode.
10282  * set correctly, so this function is setting it to the highest supported level.
10283  * Older SATA spec required that the device supports at least DMA 4 mode and
10284  * UDMA mode is selected.  It is not mentioned in SerialATA 2.6, so this
10285  * restriction has been removed.
10286  *
10287  * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported.
10288  * Returns SATA_FAILURE if proper DMA mode could not be selected.
10289  *
10290  * NOTE: This function should be called only if DMA mode is supported.
10291  */
10292 static int
10293 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo)
10294 {
10295 	sata_pkt_t *spkt;
10296 	sata_cmd_t *scmd;
10297 	sata_pkt_txlate_t *spx;
10298 	int i, mode;
10299 	uint8_t subcmd;
10300 	int rval = SATA_SUCCESS;
10301 
10302 	ASSERT(sdinfo != NULL);
10303 	ASSERT(sata_hba_inst != NULL);
10304 
10305 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
10306 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) {
10307 		/* Find highest Ultra DMA mode supported */
10308 		for (mode = 6; mode >= 0; --mode) {
10309 			if (sdinfo->satadrv_id.ai_ultradma & (1 << mode))
10310 				break;
10311 		}
10312 #if 0
10313 		/* Left for historical reasons */
10314 		/*
10315 		 * Some initial version of SATA spec indicated that at least
10316 		 * UDMA mode 4 has to be supported. It is not mentioned in
10317 		 * SerialATA 2.6, so this restriction is removed.
10318 		 */
10319 		if (mode < 4)
10320 			return (SATA_FAILURE);
10321 #endif
10322 		/* Find UDMA mode currently selected */
10323 		for (i = 6; i >= 0; --i) {
10324 			if (sdinfo->satadrv_id.ai_ultradma & (1 << (i + 8)))
10325 				break;
10326 		}
10327 		if (i >= mode)
10328 			/* Nothing to do */
10329 			return (SATA_SUCCESS);
10330 
10331 		subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA;
10332 
10333 	} else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) {
10334 		/* Find highest MultiWord DMA mode supported */
10335 		for (mode = 2; mode >= 0; --mode) {
10336 			if (sdinfo->satadrv_id.ai_dworddma & (1 << mode))
10337 				break;
10338 		}
10339 		/* Find highest MultiWord DMA mode selected */
10340 		for (i = 2; i >= 0; --i) {
10341 			if (sdinfo->satadrv_id.ai_dworddma & (1 << (i + 8)))
10342 				break;
10343 		}
10344 		if (i >= mode)
10345 			/* Nothing to do */
10346 			return (SATA_SUCCESS);
10347 
10348 		subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA;
10349 	} else
10350 		return (SATA_SUCCESS);
10351 
10352 	/*
10353 	 * Set DMA mode via SET FEATURES COMMAND.
10354 	 * Prepare packet for SET FEATURES COMMAND.
10355 	 */
10356 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10357 	spx->txlt_sata_hba_inst = sata_hba_inst;
10358 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10359 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10360 	if (spkt == NULL) {
10361 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10362 		    "sata_set_dma_mode: could not set DMA mode %", mode));
10363 		rval = SATA_FAILURE;
10364 		goto done;
10365 	}
10366 	/* Fill sata_pkt */
10367 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10368 	/* Timeout 30s */
10369 	spkt->satapkt_time = sata_default_pkt_time;
10370 	/* Synchronous mode, no callback, interrupts */
10371 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10372 	spkt->satapkt_comp = NULL;
10373 	scmd = &spkt->satapkt_cmd;
10374 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10375 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10376 	scmd->satacmd_addr_type = 0;
10377 	scmd->satacmd_device_reg = 0;
10378 	scmd->satacmd_status_reg = 0;
10379 	scmd->satacmd_error_reg = 0;
10380 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10381 	scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE;
10382 	scmd->satacmd_sec_count_lsb = subcmd | mode;
10383 
10384 	/* Transfer command to HBA */
10385 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
10386 	    spkt) != SATA_TRAN_ACCEPTED ||
10387 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
10388 		/* Pkt execution failed */
10389 		rval = SATA_FAILURE;
10390 	}
10391 done:
10392 
10393 	/* Free allocated resources */
10394 	if (spkt != NULL)
10395 		sata_pkt_free(spx);
10396 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10397 
10398 	return (rval);
10399 }
10400 
10401 
10402 /*
10403  * Set device caching mode.
10404  * One of the following operations should be specified:
10405  * SATAC_SF_ENABLE_READ_AHEAD
10406  * SATAC_SF_DISABLE_READ_AHEAD
10407  * SATAC_SF_ENABLE_WRITE_CACHE
10408  * SATAC_SF_DISABLE_WRITE_CACHE
10409  *
10410  * If operation fails, system log messgage is emitted.
10411  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
10412  */
10413 
10414 static int
10415 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
10416     int cache_op)
10417 {
10418 	sata_pkt_t *spkt;
10419 	sata_cmd_t *scmd;
10420 	sata_pkt_txlate_t *spx;
10421 	int rval = SATA_SUCCESS;
10422 	char *infop;
10423 
10424 	ASSERT(sdinfo != NULL);
10425 	ASSERT(sata_hba_inst != NULL);
10426 	ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD ||
10427 	    cache_op == SATAC_SF_DISABLE_READ_AHEAD ||
10428 	    cache_op == SATAC_SF_ENABLE_WRITE_CACHE ||
10429 	    cache_op == SATAC_SF_DISABLE_WRITE_CACHE);
10430 
10431 
10432 	/* Prepare packet for SET FEATURES COMMAND */
10433 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10434 	spx->txlt_sata_hba_inst = sata_hba_inst;
10435 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10436 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10437 	if (spkt == NULL) {
10438 		rval = SATA_FAILURE;
10439 		goto failure;
10440 	}
10441 	/* Fill sata_pkt */
10442 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10443 	/* Timeout 30s */
10444 	spkt->satapkt_time = sata_default_pkt_time;
10445 	/* Synchronous mode, no callback, interrupts */
10446 	spkt->satapkt_op_mode =
10447 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10448 	spkt->satapkt_comp = NULL;
10449 	scmd = &spkt->satapkt_cmd;
10450 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10451 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10452 	scmd->satacmd_addr_type = 0;
10453 	scmd->satacmd_device_reg = 0;
10454 	scmd->satacmd_status_reg = 0;
10455 	scmd->satacmd_error_reg = 0;
10456 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10457 	scmd->satacmd_features_reg = cache_op;
10458 
10459 	/* Transfer command to HBA */
10460 	if (((*SATA_START_FUNC(sata_hba_inst))(
10461 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
10462 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
10463 		/* Pkt execution failed */
10464 		switch (cache_op) {
10465 		case SATAC_SF_ENABLE_READ_AHEAD:
10466 			infop = "enabling read ahead failed";
10467 			break;
10468 		case SATAC_SF_DISABLE_READ_AHEAD:
10469 			infop = "disabling read ahead failed";
10470 			break;
10471 		case SATAC_SF_ENABLE_WRITE_CACHE:
10472 			infop = "enabling write cache failed";
10473 			break;
10474 		case SATAC_SF_DISABLE_WRITE_CACHE:
10475 			infop = "disabling write cache failed";
10476 			break;
10477 		}
10478 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
10479 		rval = SATA_FAILURE;
10480 	}
10481 failure:
10482 	/* Free allocated resources */
10483 	if (spkt != NULL)
10484 		sata_pkt_free(spx);
10485 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10486 	return (rval);
10487 }
10488 
10489 /*
10490  * Set Removable Media Status Notification (enable/disable)
10491  * state == 0 , disable
10492  * state != 0 , enable
10493  *
10494  * If operation fails, system log messgage is emitted.
10495  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
10496  */
10497 
10498 static int
10499 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
10500     int state)
10501 {
10502 	sata_pkt_t *spkt;
10503 	sata_cmd_t *scmd;
10504 	sata_pkt_txlate_t *spx;
10505 	int rval = SATA_SUCCESS;
10506 	char *infop;
10507 
10508 	ASSERT(sdinfo != NULL);
10509 	ASSERT(sata_hba_inst != NULL);
10510 
10511 	/* Prepare packet for SET FEATURES COMMAND */
10512 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10513 	spx->txlt_sata_hba_inst = sata_hba_inst;
10514 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10515 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10516 	if (spkt == NULL) {
10517 		rval = SATA_FAILURE;
10518 		goto failure;
10519 	}
10520 	/* Fill sata_pkt */
10521 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10522 	/* Timeout 30s */
10523 	spkt->satapkt_time = sata_default_pkt_time;
10524 	/* Synchronous mode, no callback, interrupts */
10525 	spkt->satapkt_op_mode =
10526 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10527 	spkt->satapkt_comp = NULL;
10528 	scmd = &spkt->satapkt_cmd;
10529 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10530 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10531 	scmd->satacmd_addr_type = 0;
10532 	scmd->satacmd_device_reg = 0;
10533 	scmd->satacmd_status_reg = 0;
10534 	scmd->satacmd_error_reg = 0;
10535 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10536 	if (state == 0)
10537 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN;
10538 	else
10539 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN;
10540 
10541 	/* Transfer command to HBA */
10542 	if (((*SATA_START_FUNC(sata_hba_inst))(
10543 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
10544 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
10545 		/* Pkt execution failed */
10546 		if (state == 0)
10547 			infop = "disabling Removable Media Status "
10548 			    "Notification failed";
10549 		else
10550 			infop = "enabling Removable Media Status "
10551 			    "Notification failed";
10552 
10553 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
10554 		rval = SATA_FAILURE;
10555 	}
10556 failure:
10557 	/* Free allocated resources */
10558 	if (spkt != NULL)
10559 		sata_pkt_free(spx);
10560 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10561 	return (rval);
10562 }
10563 
10564 
10565 /*
10566  * Update port SCR block
10567  */
10568 static void
10569 sata_update_port_scr(sata_port_scr_t *port_scr, sata_device_t *device)
10570 {
10571 	port_scr->sstatus = device->satadev_scr.sstatus;
10572 	port_scr->serror = device->satadev_scr.serror;
10573 	port_scr->scontrol = device->satadev_scr.scontrol;
10574 	port_scr->sactive = device->satadev_scr.sactive;
10575 	port_scr->snotific = device->satadev_scr.snotific;
10576 }
10577 
10578 /*
10579  * Update state and copy port ss* values from passed sata_device structure.
10580  * sata_address is validated - if not valid, nothing is changed in sata_scsi
10581  * configuration struct.
10582  *
10583  * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function
10584  * regardless of the state in device argument.
10585  *
10586  * Port mutex should be held while calling this function.
10587  */
10588 static void
10589 sata_update_port_info(sata_hba_inst_t *sata_hba_inst,
10590 	sata_device_t *sata_device)
10591 {
10592 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst,
10593 	    sata_device->satadev_addr.cport)));
10594 
10595 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT ||
10596 	    sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
10597 
10598 		sata_cport_info_t *cportinfo;
10599 
10600 		if (SATA_NUM_CPORTS(sata_hba_inst) <=
10601 		    sata_device->satadev_addr.cport)
10602 			return;
10603 
10604 		cportinfo = SATA_CPORT_INFO(sata_hba_inst,
10605 		    sata_device->satadev_addr.cport);
10606 		sata_update_port_scr(&cportinfo->cport_scr, sata_device);
10607 
10608 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
10609 		cportinfo->cport_state &= ~(SATA_PSTATE_PWRON |
10610 		    SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
10611 		cportinfo->cport_state |=
10612 		    sata_device->satadev_state & SATA_PSTATE_VALID;
10613 	} else {
10614 		sata_pmport_info_t *pmportinfo;
10615 
10616 		if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT) ||
10617 		    (sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) ||
10618 		    SATA_NUM_PMPORTS(sata_hba_inst,
10619 		    sata_device->satadev_addr.cport) <
10620 		    sata_device->satadev_addr.pmport)
10621 			return;
10622 
10623 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
10624 		    sata_device->satadev_addr.cport,
10625 		    sata_device->satadev_addr.pmport);
10626 		sata_update_port_scr(&pmportinfo->pmport_scr, sata_device);
10627 
10628 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
10629 		pmportinfo->pmport_state &=
10630 		    ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF |
10631 		    SATA_PSTATE_FAILED);
10632 		pmportinfo->pmport_state |=
10633 		    sata_device->satadev_state & SATA_PSTATE_VALID;
10634 	}
10635 }
10636 
10637 
10638 
10639 /*
10640  * Extract SATA port specification from an IOCTL argument.
10641  *
10642  * This function return the port the user land send us as is, unless it
10643  * cannot retrieve port spec, then -1 is returned.
10644  *
10645  * Note: Only cport  - no port multiplier port.
10646  */
10647 static int32_t
10648 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp)
10649 {
10650 	int32_t port;
10651 
10652 	/* Extract port number from nvpair in dca structure  */
10653 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
10654 		SATA_LOG_D((sata_hba_inst, CE_NOTE,
10655 		    "sata_get_port_num: invalid port spec 0x%x in ioctl",
10656 		    port));
10657 		port = -1;
10658 	}
10659 
10660 	return (port);
10661 }
10662 
10663 /*
10664  * Get dev_info_t pointer to the device node pointed to by port argument.
10665  * NOTE: target argument is a value used in ioctls to identify
10666  * the AP - it is not a sata_address.
10667  * It is a combination of cport, pmport and address qualifier, encodded same
10668  * way as a scsi target number.
10669  * At this moment it carries only cport number.
10670  *
10671  * No PMult hotplug support.
10672  *
10673  * Returns dev_info_t pointer if target device was found, NULL otherwise.
10674  */
10675 
10676 static dev_info_t *
10677 sata_get_target_dip(dev_info_t *dip, int32_t port)
10678 {
10679 	dev_info_t	*cdip = NULL;
10680 	int		target, tgt;
10681 	int		ncport;
10682 	int 		circ;
10683 
10684 	ncport = port & SATA_CFGA_CPORT_MASK;
10685 	target = SATA_TO_SCSI_TARGET(ncport, 0, SATA_ADDR_DCPORT);
10686 
10687 	ndi_devi_enter(dip, &circ);
10688 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
10689 		dev_info_t *next = ddi_get_next_sibling(cdip);
10690 
10691 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
10692 		    DDI_PROP_DONTPASS, "target", -1);
10693 		if (tgt == -1) {
10694 			/*
10695 			 * This is actually an error condition, but not
10696 			 * a fatal one. Just continue the search.
10697 			 */
10698 			cdip = next;
10699 			continue;
10700 		}
10701 
10702 		if (tgt == target)
10703 			break;
10704 
10705 		cdip = next;
10706 	}
10707 	ndi_devi_exit(dip, circ);
10708 
10709 	return (cdip);
10710 }
10711 
10712 /*
10713  * Get dev_info_t pointer to the device node pointed to by port argument.
10714  * NOTE: target argument is a value used in ioctls to identify
10715  * the AP - it is not a sata_address.
10716  * It is a combination of cport, pmport and address qualifier, encoded same
10717  * way as a scsi target number.
10718  * At this moment it carries only cport number.
10719  *
10720  * No PMult hotplug support.
10721  *
10722  * Returns dev_info_t pointer if target device was found, NULL otherwise.
10723  */
10724 
10725 static dev_info_t *
10726 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr)
10727 {
10728 	dev_info_t	*cdip = NULL;
10729 	int		target, tgt;
10730 	int 		circ;
10731 
10732 	target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual);
10733 
10734 	ndi_devi_enter(dip, &circ);
10735 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
10736 		dev_info_t *next = ddi_get_next_sibling(cdip);
10737 
10738 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
10739 		    DDI_PROP_DONTPASS, "target", -1);
10740 		if (tgt == -1) {
10741 			/*
10742 			 * This is actually an error condition, but not
10743 			 * a fatal one. Just continue the search.
10744 			 */
10745 			cdip = next;
10746 			continue;
10747 		}
10748 
10749 		if (tgt == target)
10750 			break;
10751 
10752 		cdip = next;
10753 	}
10754 	ndi_devi_exit(dip, circ);
10755 
10756 	return (cdip);
10757 }
10758 
10759 /*
10760  * Process sata port disconnect request.
10761  * Normally, cfgadm sata plugin will try to offline (unconfigure) the device
10762  * before this request. Nevertheless, if a device is still configured,
10763  * we need to attempt to offline and unconfigure device.
10764  * Regardless of the unconfigure operation results the port is marked as
10765  * deactivated and no access to the attached device is possible.
10766  * If the target node remains because unconfigure operation failed, its state
10767  * will be set to DEVICE_REMOVED, preventing it to be used again when a device
10768  * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure
10769  * the device and remove old target node.
10770  *
10771  * This function invokes sata_hba_inst->satahba_tran->
10772  * sata_tran_hotplug_ops->sata_tran_port_deactivate().
10773  * If successful, the device structure (if any) attached to the specified port
10774  * is removed and state of the port marked appropriately.
10775  * Failure of the port_deactivate may keep port in the physically active state,
10776  * or may fail the port.
10777  *
10778  * NOTE: Port multiplier code is not completed nor tested.
10779  */
10780 
10781 static int
10782 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst,
10783     sata_device_t *sata_device)
10784 {
10785 	sata_drive_info_t *sdinfo = NULL;
10786 	sata_cport_info_t *cportinfo = NULL;
10787 	sata_pmport_info_t *pmportinfo = NULL;
10788 	sata_pmult_info_t *pmultinfo = NULL;
10789 	dev_info_t *tdip;
10790 	int cport, pmport, qual;
10791 	int rval = SATA_SUCCESS;
10792 	int rv = 0;
10793 
10794 	cport = sata_device->satadev_addr.cport;
10795 	pmport = sata_device->satadev_addr.pmport;
10796 	qual = sata_device->satadev_addr.qual;
10797 
10798 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
10799 
10800 	/*
10801 	 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran->
10802 	 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
10803 	 * Do the sanity check.
10804 	 */
10805 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
10806 		/* No physical port deactivation supported. */
10807 		return (EINVAL);
10808 	}
10809 
10810 	/* Check the current state of the port */
10811 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10812 	    (SATA_DIP(sata_hba_inst), sata_device);
10813 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10814 	sata_update_port_info(sata_hba_inst, sata_device);
10815 	if (rval != SATA_SUCCESS ||
10816 	    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
10817 		/* Device port status is unknown or it is in failed state */
10818 		if (qual == SATA_ADDR_PMPORT) {
10819 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
10820 			    SATA_PSTATE_FAILED;
10821 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
10822 			    "sata_hba_ioctl: connect: failed to deactivate "
10823 			    "SATA port %d", cport);
10824 		} else {
10825 			SATA_CPORT_STATE(sata_hba_inst, cport) =
10826 			    SATA_PSTATE_FAILED;
10827 			SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
10828 			    "sata_hba_ioctl: connect: failed to deactivate "
10829 			    "SATA port %d:%d", cport, pmport);
10830 		}
10831 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
10832 		    cport)->cport_mutex);
10833 		return (EIO);
10834 	}
10835 	/*
10836 	 * Set port's dev_state to not ready - this will disable
10837 	 * an access to a potentially attached device.
10838 	 */
10839 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
10840 	if (qual == SATA_ADDR_PMPORT) {
10841 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
10842 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
10843 			sdinfo = pmportinfo->pmport_sata_drive;
10844 			ASSERT(sdinfo != NULL);
10845 		}
10846 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
10847 	} else {
10848 		/* Assuming cport */
10849 
10850 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
10851 			if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
10852 				pmultinfo =
10853 				    cportinfo->cport_devp.cport_sata_pmult;
10854 				ASSERT(pmultinfo != NULL);
10855 			} else if (cportinfo->cport_dev_type &
10856 			    SATA_VALID_DEV_TYPE) {
10857 				sdinfo = cportinfo->cport_devp.cport_sata_drive;
10858 				ASSERT(sdinfo != NULL);
10859 			}
10860 		}
10861 		cportinfo->cport_state &= ~SATA_STATE_READY;
10862 	}
10863 	if (sdinfo != NULL) {
10864 		if ((sdinfo->satadrv_type & (SATA_VALID_DEV_TYPE)) != 0) {
10865 			/*
10866 			 * If a target node exists, try to offline
10867 			 * a device and remove target node.
10868 			 */
10869 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
10870 			    cport)->cport_mutex);
10871 			/* We are addressing attached device, not a port */
10872 			sata_device->satadev_addr.qual =
10873 			    sdinfo->satadrv_addr.qual;
10874 			tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
10875 			    &sata_device->satadev_addr);
10876 			if (tdip != NULL && ndi_devi_offline(tdip,
10877 			    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
10878 				/*
10879 				 * Problem
10880 				 * The target node remained attached.
10881 				 * This happens when the device file was open
10882 				 * or a node was waiting for resources.
10883 				 * Cannot do anything about it.
10884 				 */
10885 				if (qual == SATA_ADDR_CPORT) {
10886 					SATA_LOG_D((sata_hba_inst, CE_WARN,
10887 					    "sata_hba_ioctl: disconnect: could "
10888 					    "not unconfigure device before "
10889 					    "disconnecting the SATA port %d",
10890 					    cport));
10891 				} else {
10892 					SATA_LOG_D((sata_hba_inst, CE_WARN,
10893 					    "sata_hba_ioctl: disconnect: could "
10894 					    "not unconfigure device before "
10895 					    "disconnecting the SATA port %d:%d",
10896 					    cport, pmport));
10897 				}
10898 				/*
10899 				 * Set DEVICE REMOVED state in the target
10900 				 * node. It will prevent access to the device
10901 				 * even when a new device is attached, until
10902 				 * the old target node is released, removed and
10903 				 * recreated for a new  device.
10904 				 */
10905 				sata_set_device_removed(tdip);
10906 
10907 				/*
10908 				 * Instruct event daemon to try the target
10909 				 * node cleanup later.
10910 				 */
10911 				sata_set_target_node_cleanup(
10912 				    sata_hba_inst, &sata_device->satadev_addr);
10913 			}
10914 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
10915 			    cport)->cport_mutex);
10916 		}
10917 
10918 		/* Remove and release sata_drive info structure. */
10919 		if (pmportinfo != NULL) {
10920 			SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport) =
10921 			    NULL;
10922 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
10923 		} else {
10924 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
10925 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
10926 		}
10927 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
10928 	}
10929 #if 0
10930 	else if (pmultinfo != NULL) {
10931 		/*
10932 		 * Port Multiplier itself needs special handling.
10933 		 * All device ports need to be processed here!
10934 		 */
10935 	}
10936 #endif
10937 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10938 	/* Just ask HBA driver to deactivate port */
10939 	/*	sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; */
10940 
10941 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
10942 	    (SATA_DIP(sata_hba_inst), sata_device);
10943 
10944 	/*
10945 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
10946 	 * without the hint (to force listener to investivate the state).
10947 	 */
10948 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
10949 	    SE_NO_HINT);
10950 
10951 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10952 	sata_update_port_info(sata_hba_inst, sata_device);
10953 
10954 	if (rval != SATA_SUCCESS) {
10955 		/*
10956 		 * Port deactivation failure - do not
10957 		 * change port state unless the state
10958 		 * returned by HBA indicates a port failure.
10959 		 * NOTE: device structures were released, so devices now are
10960 		 * invisible! Port reset is needed to re-enumerate devices.
10961 		 */
10962 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
10963 			if (pmportinfo != NULL)
10964 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
10965 			else
10966 				cportinfo->cport_state = SATA_PSTATE_FAILED;
10967 			rv = EIO;
10968 		}
10969 	} else {
10970 		/*
10971 		 * Deactivation succeded. From now on the sata framework
10972 		 * will not care what is happening to the device, until
10973 		 * the port is activated again.
10974 		 */
10975 		cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
10976 	}
10977 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10978 	return (rv);
10979 }
10980 
10981 
10982 
10983 /*
10984  * Process sata port connect request
10985  * The sata cfgadm pluging will invoke this operation only if port was found
10986  * in the disconnect state (failed state is also treated as the disconnected
10987  * state).
10988  * DEVCTL_AP_CONNECT would invoke  sata_hba_inst->satahba_tran->
10989  * sata_tran_hotplug_ops->sata_tran_port_activate().
10990  * If successful and a device is found attached to the port,
10991  * the initialization sequence is executed to attach a device structure to
10992  * a port structure. The state of the port and a device would be set
10993  * appropriately.
10994  * The device is not set in configured state (system-wise) by this operation.
10995  *
10996  * Note, that activating the port may generate link events,
10997  * so it is important that following processing and the
10998  * event processing does not interfere with each other!
10999  *
11000  * This operation may remove port failed state and will
11001  * try to make port active and in good standing.
11002  *
11003  * NOTE: Port multiplier code is not completed nor tested.
11004  */
11005 
11006 static int
11007 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst,
11008     sata_device_t *sata_device)
11009 {
11010 	int cport, pmport, qual;
11011 	int rv = 0;
11012 
11013 	cport = sata_device->satadev_addr.cport;
11014 	pmport = sata_device->satadev_addr.pmport;
11015 	qual = sata_device->satadev_addr.qual;
11016 
11017 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
11018 
11019 	/*
11020 	 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->
11021 	 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate().
11022 	 * Perform sanity check now.
11023 	 */
11024 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) {
11025 		/* No physical port activation supported. */
11026 		return (EINVAL);
11027 	}
11028 
11029 	/* Just ask HBA driver to activate port */
11030 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11031 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11032 		/*
11033 		 * Port activation failure.
11034 		 */
11035 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11036 		    cport)->cport_mutex);
11037 		sata_update_port_info(sata_hba_inst, sata_device);
11038 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11039 			if (qual == SATA_ADDR_DCPORT) {
11040 				SATA_CPORT_STATE(sata_hba_inst, cport) =
11041 				    SATA_PSTATE_FAILED;
11042 				SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
11043 				    "sata_hba_ioctl: connect: failed to "
11044 				    "activate SATA port %d", cport);
11045 			} else { /* port multiplier device port */
11046 				SATA_PMPORT_STATE(sata_hba_inst, cport,
11047 				    pmport) = SATA_PSTATE_FAILED;
11048 				SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
11049 				    "sata_hba_ioctl: connect: failed to "
11050 				    "activate SATA port %d:%d", cport, pmport);
11051 
11052 			}
11053 		}
11054 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11055 		    cport)->cport_mutex);
11056 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
11057 		    "sata_hba_ioctl: connect: failed to activate SATA "
11058 		    "port %d:%d", cport, pmport);
11059 		return (EIO);
11060 	}
11061 
11062 	/* Virgin port state - will be updated by the port re-probe. */
11063 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11064 	if (qual == SATA_ADDR_CPORT)
11065 		SATA_CPORT_STATE(sata_hba_inst, cport) = 0;
11066 	else /* port multiplier device port */
11067 		SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0;
11068 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11069 
11070 	/*
11071 	 * Probe the port to find its state and attached device.
11072 	 */
11073 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11074 	    SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE)
11075 		rv = EIO;
11076 
11077 	/*
11078 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11079 	 * without the hint
11080 	 */
11081 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11082 	    SE_NO_HINT);
11083 
11084 	/*
11085 	 * If there is a device attached to the port, emit
11086 	 * a message.
11087 	 */
11088 	if (qual == SATA_ADDR_CPORT) {
11089 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
11090 		    SATA_DTYPE_NONE)
11091 			sata_log(sata_hba_inst, CE_WARN,
11092 			    "SATA device detected at port %d", cport);
11093 	} else { /* port multiplier device port */
11094 		if (SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) !=
11095 		    SATA_DTYPE_NONE)
11096 			sata_log(sata_hba_inst, CE_WARN,
11097 			    "SATA device detected at port %d:%d",
11098 			    cport, pmport);
11099 	}
11100 
11101 	return (rv);
11102 }
11103 
11104 
11105 /*
11106  * Process sata device unconfigure request.
11107  * The unconfigure operation uses generic nexus operation to
11108  * offline a device. It leaves a target device node attached.
11109  * and obviously sata_drive_info attached as well, because
11110  * from the hardware point of view nothing has changed.
11111  */
11112 static int
11113 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst,
11114     sata_device_t *sata_device)
11115 {
11116 	int rv = 0;
11117 	dev_info_t *tdip;
11118 
11119 	/* We are addressing attached device, not a port */
11120 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
11121 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11122 	else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT)
11123 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11124 
11125 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11126 	    &sata_device->satadev_addr)) != NULL) {
11127 
11128 		if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) {
11129 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11130 			    "sata_hba_ioctl: unconfigure: "
11131 			    "failed to unconfigure device at SATA port %d:%d",
11132 			    sata_device->satadev_addr.cport,
11133 			    sata_device->satadev_addr.pmport));
11134 			rv = EIO;
11135 		}
11136 		/*
11137 		 * The target node devi_state should be marked with
11138 		 * DEVI_DEVICE_OFFLINE by ndi_devi_offline().
11139 		 * This would be the indication for cfgadm that
11140 		 * the AP node occupant state is 'unconfigured'.
11141 		 */
11142 
11143 	} else {
11144 		/*
11145 		 * This would indicate a failure on the part of cfgadm
11146 		 * to detect correct state of the node prior to this
11147 		 * call - one cannot unconfigure non-existing device.
11148 		 */
11149 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11150 		    "sata_hba_ioctl: unconfigure: "
11151 		    "attempt to unconfigure non-existing device "
11152 		    "at SATA port %d:%d",
11153 		    sata_device->satadev_addr.cport,
11154 		    sata_device->satadev_addr.pmport));
11155 		rv = ENXIO;
11156 	}
11157 	return (rv);
11158 }
11159 
11160 /*
11161  * Process sata device configure request
11162  * If port is in a failed state, operation is aborted - one has to use
11163  * an explicit connect or port activate request to try to get a port into
11164  * non-failed mode. Port reset wil also work in such situation.
11165  * If the port is in disconnected (shutdown) state, the connect operation is
11166  * attempted prior to any other action.
11167  * When port is in the active state, there is a device attached and the target
11168  * node exists, a device was most likely offlined.
11169  * If target node does not exist, a new target node is created. In both cases
11170  * an attempt is made to online (configure) the device.
11171  *
11172  * NOTE: Port multiplier code is not completed nor tested.
11173  */
11174 static int
11175 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst,
11176     sata_device_t *sata_device)
11177 {
11178 	int cport, pmport, qual;
11179 	int rval;
11180 	boolean_t target = TRUE;
11181 	sata_cport_info_t *cportinfo;
11182 	sata_pmport_info_t *pmportinfo = NULL;
11183 	dev_info_t *tdip;
11184 	sata_drive_info_t *sdinfo;
11185 
11186 	cport = sata_device->satadev_addr.cport;
11187 	pmport = sata_device->satadev_addr.pmport;
11188 	qual = sata_device->satadev_addr.qual;
11189 
11190 	/* Get current port state */
11191 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11192 	    (SATA_DIP(sata_hba_inst), sata_device);
11193 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11194 	sata_update_port_info(sata_hba_inst, sata_device);
11195 
11196 	if (rval != SATA_SUCCESS ||
11197 	    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
11198 		/* Obviously, device on a failed port is not visible */
11199 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11200 		return (ENXIO);
11201 	}
11202 
11203 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11204 	if (qual == SATA_ADDR_PMPORT)
11205 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11206 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11207 
11208 	if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) {
11209 		/* need to activate port */
11210 		target = FALSE;
11211 
11212 		/* Sanity check */
11213 		if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
11214 			return (ENXIO);
11215 
11216 		/* Just let HBA driver to activate port */
11217 		if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11218 		    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11219 			/*
11220 			 * Port activation failure - do not change port state
11221 			 * unless the state returned by HBA indicates a port
11222 			 * failure.
11223 			 */
11224 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11225 			    cport)->cport_mutex);
11226 			sata_update_port_info(sata_hba_inst, sata_device);
11227 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11228 				if (qual == SATA_ADDR_PMPORT)
11229 					pmportinfo->pmport_state =
11230 					    SATA_PSTATE_FAILED;
11231 				else
11232 					cportinfo->cport_state =
11233 					    SATA_PSTATE_FAILED;
11234 			}
11235 			mutex_exit(&SATA_CPORT_INFO(
11236 			    sata_hba_inst, cport)->cport_mutex);
11237 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11238 			    "sata_hba_ioctl: configure: "
11239 			    "failed to activate SATA port %d:%d",
11240 			    cport, pmport));
11241 			return (EIO);
11242 		}
11243 		/*
11244 		 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11245 		 * without the hint.
11246 		 */
11247 		sata_gen_sysevent(sata_hba_inst,
11248 		    &sata_device->satadev_addr, SE_NO_HINT);
11249 
11250 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11251 		    cport_mutex);
11252 		/* Virgin port state */
11253 		if (qual == SATA_ADDR_PMPORT)
11254 			pmportinfo->pmport_state = 0;
11255 		else
11256 			cportinfo->cport_state = 0;
11257 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11258 	}
11259 	/*
11260 	 * Always reprobe port, to get current device info.
11261 	 */
11262 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11263 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
11264 		return (EIO);
11265 
11266 	if (qual == SATA_ADDR_PMPORT) {
11267 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
11268 		    target == FALSE) {
11269 			/*
11270 			 * That's the transition from "inactive" port
11271 			 * to active one with device attached.
11272 			 */
11273 			sata_log(sata_hba_inst, CE_WARN,
11274 			    "SATA device detected at port %d:%d",
11275 			    cport, pmport);
11276 		}
11277 	} else {
11278 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE &&
11279 		    target == FALSE) {
11280 			/*
11281 			 * When PM is attached to the cport and cport is
11282 			 * activated, every PM device port needs to be reprobed.
11283 			 * We need to emit message for all devices detected
11284 			 * at port multiplier's device ports.
11285 			 * Add such code here.
11286 			 * For now, just inform about device attached to
11287 			 * cport.
11288 			 */
11289 			sata_log(sata_hba_inst, CE_WARN,
11290 			    "SATA device detected at port %d", cport);
11291 		}
11292 	}
11293 
11294 	/*
11295 	 * This is where real configuration operation starts.
11296 	 *
11297 	 * When PM is attached to the cport and cport is activated,
11298 	 * devices attached PM device ports may have to be configured
11299 	 * explicitly. This may change when port multiplier is supported.
11300 	 * For now, configure only disks and other valid target devices.
11301 	 */
11302 	if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) {
11303 		return (ENXIO);		/* No device to configure */
11304 	}
11305 
11306 	/*
11307 	 * Here we may have a device in reset condition,
11308 	 * but because we are just configuring it, there is
11309 	 * no need to process the reset other than just
11310 	 * to clear device reset condition in the HBA driver.
11311 	 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will
11312 	 * cause a first command sent the HBA driver with the request
11313 	 * to clear device reset condition.
11314 	 */
11315 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11316 	if (qual == SATA_ADDR_PMPORT)
11317 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11318 	else
11319 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11320 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
11321 	if (sdinfo == NULL) {
11322 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11323 		return (ENXIO);
11324 	}
11325 	if (sdinfo->satadrv_event_flags &
11326 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
11327 		sdinfo->satadrv_event_flags = 0;
11328 		sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
11329 	}
11330 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11331 
11332 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11333 	    &sata_device->satadev_addr)) != NULL) {
11334 		/*
11335 		 * Target node exists. Verify, that it belongs
11336 		 * to existing, attached device and not to
11337 		 * a removed device.
11338 		 */
11339 		if (sata_check_device_removed(tdip) == B_TRUE) {
11340 			if (qual == SATA_ADDR_DPMPORT)
11341 				sata_log(sata_hba_inst, CE_WARN,
11342 				    "SATA device at port %d cannot be "
11343 				    "configured. "
11344 				    "Application(s) accessing "
11345 				    "previously attached device "
11346 				    "have to release it before newly "
11347 				    "inserted device can be made accessible.",
11348 				    cport);
11349 			else
11350 				sata_log(sata_hba_inst, CE_WARN,
11351 				    "SATA device at port %d:%d cannot be"
11352 				    "configured. "
11353 				    "Application(s) accessing "
11354 				    "previously attached device "
11355 				    "have to release it before newly "
11356 				    "inserted device can be made accessible.",
11357 				    cport, pmport);
11358 			return (EIO);
11359 		}
11360 		/*
11361 		 * Device was not removed and re-inserted.
11362 		 * Try to online it.
11363 		 */
11364 		if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) {
11365 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11366 			    "sata_hba_ioctl: configure: "
11367 			    "onlining device at SATA port "
11368 			    "%d:%d failed", cport, pmport));
11369 			return (EIO);
11370 		}
11371 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11372 		    cport)->cport_mutex);
11373 
11374 		if (qual == SATA_ADDR_DPMPORT)
11375 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
11376 		else
11377 			cportinfo-> cport_tgtnode_clean = B_TRUE;
11378 
11379 		mutex_exit(&SATA_CPORT_INFO(
11380 		    sata_hba_inst, cport)->cport_mutex);
11381 	} else {
11382 		/*
11383 		 * No target node - need to create a new target node.
11384 		 */
11385 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11386 		    cport_mutex);
11387 		if (qual == SATA_ADDR_DPMPORT)
11388 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
11389 		else
11390 			cportinfo-> cport_tgtnode_clean = B_TRUE;
11391 
11392 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11393 		    cport_mutex);
11394 		tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
11395 		    sata_hba_inst, &sata_device->satadev_addr);
11396 		if (tdip == NULL) {
11397 			/* Configure operation failed */
11398 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11399 			    "sata_hba_ioctl: configure: "
11400 			    "configuring SATA device at port %d:%d "
11401 			    "failed", cport, pmport));
11402 			return (EIO);
11403 		}
11404 	}
11405 	return (0);
11406 }
11407 
11408 
11409 /*
11410  * Process ioctl deactivate port request.
11411  * Arbitrarily unconfigure attached device, if any.
11412  * Even if the unconfigure fails, proceed with the
11413  * port deactivation.
11414  *
11415  * NOTE: Port Multiplier code is not completed and tested.
11416  */
11417 
11418 static int
11419 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst,
11420     sata_device_t *sata_device)
11421 {
11422 	int cport, pmport, qual;
11423 	int rval, rv = 0;
11424 	sata_cport_info_t *cportinfo;
11425 	sata_pmport_info_t *pmportinfo = NULL;
11426 	dev_info_t *tdip;
11427 	sata_drive_info_t *sdinfo = NULL;
11428 
11429 	/* Sanity check */
11430 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL)
11431 		return (ENOTSUP);
11432 
11433 	cport = sata_device->satadev_addr.cport;
11434 	pmport = sata_device->satadev_addr.pmport;
11435 	qual = sata_device->satadev_addr.qual;
11436 
11437 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11438 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11439 	if (qual == SATA_ADDR_CPORT) {
11440 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11441 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
11442 			/*
11443 			 * For now, assume that port multiplier is not
11444 			 * supported, i.e. deal only with valid devices
11445 			 */
11446 			if ((cportinfo->cport_dev_type &
11447 			    SATA_VALID_DEV_TYPE) != 0)
11448 				sdinfo = sata_get_device_info(sata_hba_inst,
11449 				    sata_device);
11450 			/*
11451 			 * If attached device is a port multiplier, we will
11452 			 * have to unconfigure all devices attached to the
11453 			 * port multiplier. Add this code here.
11454 			 */
11455 		}
11456 		cportinfo->cport_state &= ~SATA_STATE_READY;
11457 	} else {
11458 		/* Port multiplier device port */
11459 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11460 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11461 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
11462 		    (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0)
11463 			sdinfo = sata_get_device_info(sata_hba_inst,
11464 			    sata_device);
11465 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
11466 	}
11467 
11468 	if (sdinfo != NULL) {
11469 		/*
11470 		 * If a target node exists, try to offline a device and
11471 		 * to remove a target node.
11472 		 */
11473 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11474 		    cport_mutex);
11475 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11476 		    &sata_device->satadev_addr);
11477 		if (tdip != NULL) {
11478 			/* target node exist */
11479 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
11480 			    "sata_hba_ioctl: port deactivate: "
11481 			    "target node exists.", NULL);
11482 
11483 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) !=
11484 			    NDI_SUCCESS) {
11485 				SATA_LOG_D((sata_hba_inst, CE_WARN,
11486 				    "sata_hba_ioctl: port deactivate: "
11487 				    "failed to unconfigure device at port "
11488 				    "%d:%d before deactivating the port",
11489 				    cport, pmport));
11490 				/*
11491 				 * Set DEVICE REMOVED state in the target
11492 				 * node. It will prevent an access to
11493 				 * the device even when a new device is
11494 				 * attached, until the old target node is
11495 				 * released, removed and recreated for a new
11496 				 * device.
11497 				 */
11498 				sata_set_device_removed(tdip);
11499 
11500 				/*
11501 				 * Instruct the event daemon to try the
11502 				 * target node cleanup later.
11503 				 */
11504 				sata_set_target_node_cleanup(sata_hba_inst,
11505 				    &sata_device->satadev_addr);
11506 			}
11507 		}
11508 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11509 		    cport_mutex);
11510 		/*
11511 		 * In any case, remove and release sata_drive_info
11512 		 * structure.
11513 		 */
11514 		if (qual == SATA_ADDR_CPORT) {
11515 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11516 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11517 		} else { /* port multiplier device port */
11518 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11519 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11520 		}
11521 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
11522 	}
11523 	if (qual == SATA_ADDR_CPORT) {
11524 		cportinfo->cport_state &= ~(SATA_STATE_PROBED |
11525 		    SATA_STATE_PROBING);
11526 	} else { /* port multiplier device port */
11527 		pmportinfo->pmport_state &= ~(SATA_STATE_PROBED |
11528 		    SATA_STATE_PROBING);
11529 	}
11530 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11531 
11532 	/* Just let HBA driver to deactivate port */
11533 	sata_device->satadev_addr.qual = qual;
11534 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
11535 	    (SATA_DIP(sata_hba_inst), sata_device);
11536 
11537 	/*
11538 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11539 	 * without the hint
11540 	 */
11541 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11542 	    SE_NO_HINT);
11543 
11544 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11545 	sata_update_port_info(sata_hba_inst, sata_device);
11546 	if (qual == SATA_ADDR_CPORT) {
11547 		if (rval != SATA_SUCCESS) {
11548 			/*
11549 			 * Port deactivation failure - do not change port state
11550 			 * unless the state returned by HBA indicates a port
11551 			 * failure.
11552 			 */
11553 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11554 				SATA_CPORT_STATE(sata_hba_inst, cport) =
11555 				    SATA_PSTATE_FAILED;
11556 			}
11557 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11558 			    "sata_hba_ioctl: port deactivate: "
11559 			    "cannot deactivate SATA port %d", cport));
11560 			rv = EIO;
11561 		} else {
11562 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
11563 		}
11564 	} else {
11565 		if (rval != SATA_SUCCESS) {
11566 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11567 				SATA_PMPORT_STATE(sata_hba_inst, cport,
11568 				    pmport) = SATA_PSTATE_FAILED;
11569 			}
11570 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11571 			    "sata_hba_ioctl: port deactivate: "
11572 			    "cannot deactivate SATA port %d:%d",
11573 			    cport, pmport));
11574 			rv = EIO;
11575 		} else {
11576 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
11577 		}
11578 	}
11579 
11580 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11581 
11582 	return (rv);
11583 }
11584 
11585 /*
11586  * Process ioctl port activate request.
11587  *
11588  * NOTE: Port multiplier code is not completed nor tested.
11589  */
11590 static int
11591 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst,
11592     sata_device_t *sata_device)
11593 {
11594 	int cport, pmport, qual;
11595 	sata_cport_info_t *cportinfo;
11596 	sata_pmport_info_t *pmportinfo = NULL;
11597 	boolean_t dev_existed = TRUE;
11598 
11599 	/* Sanity check */
11600 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
11601 		return (ENOTSUP);
11602 
11603 	cport = sata_device->satadev_addr.cport;
11604 	pmport = sata_device->satadev_addr.pmport;
11605 	qual = sata_device->satadev_addr.qual;
11606 
11607 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11608 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11609 	if (qual == SATA_ADDR_PMPORT) {
11610 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11611 		if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN ||
11612 		    pmportinfo->pmport_dev_type == SATA_DTYPE_NONE)
11613 			dev_existed = FALSE;
11614 	} else { /* cport */
11615 		if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN ||
11616 		    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
11617 			dev_existed = FALSE;
11618 	}
11619 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11620 
11621 	/* Just let HBA driver to activate port, if necessary */
11622 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11623 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11624 		/*
11625 		 * Port activation failure - do not change port state unless
11626 		 * the state returned by HBA indicates a port failure.
11627 		 */
11628 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11629 		    cport)->cport_mutex);
11630 		sata_update_port_info(sata_hba_inst, sata_device);
11631 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11632 			if (qual == SATA_ADDR_PMPORT)
11633 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
11634 			else
11635 				cportinfo->cport_state = SATA_PSTATE_FAILED;
11636 
11637 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11638 			    cport)->cport_mutex);
11639 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11640 			    "sata_hba_ioctl: port activate: cannot activate "
11641 			    "SATA port %d:%d", cport, pmport));
11642 			return (EIO);
11643 		}
11644 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11645 	}
11646 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11647 	if (qual == SATA_ADDR_PMPORT)
11648 		pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN;
11649 	else
11650 		cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN;
11651 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11652 
11653 	/*
11654 	 * Re-probe port to find its current state and possibly attached device.
11655 	 * Port re-probing may change the cportinfo device type if device is
11656 	 * found attached.
11657 	 * If port probing failed, the device type would be set to
11658 	 * SATA_DTYPE_NONE.
11659 	 */
11660 	(void) sata_reprobe_port(sata_hba_inst, sata_device,
11661 	    SATA_DEV_IDENTIFY_RETRY);
11662 
11663 	/*
11664 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11665 	 * without the hint.
11666 	 */
11667 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11668 	    SE_NO_HINT);
11669 
11670 	if (dev_existed == FALSE) {
11671 		if (qual == SATA_ADDR_PMPORT &&
11672 		    pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
11673 			/*
11674 			 * That's the transition from the "inactive" port state
11675 			 * or the active port without a device attached to the
11676 			 * active port state with a device attached.
11677 			 */
11678 			sata_log(sata_hba_inst, CE_WARN,
11679 			    "SATA device detected at port %d:%d",
11680 			    cport, pmport);
11681 		} else if (qual == SATA_ADDR_CPORT &&
11682 		    cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
11683 			/*
11684 			 * That's the transition from the "inactive" port state
11685 			 * or the active port without a device attached to the
11686 			 * active port state with a device attached.
11687 			 */
11688 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
11689 				sata_log(sata_hba_inst, CE_WARN,
11690 				    "SATA device detected at port %d", cport);
11691 			} else {
11692 				sata_log(sata_hba_inst, CE_WARN,
11693 				    "SATA port multiplier detected at port %d",
11694 				    cport);
11695 				/*
11696 				 * Because the detected device is a port
11697 				 * multiplier, we need to reprobe every device
11698 				 * port on the port multiplier and show every
11699 				 * device found attached.
11700 				 * Add this code here.
11701 				 */
11702 			}
11703 		}
11704 	}
11705 	return (0);
11706 }
11707 
11708 
11709 
11710 /*
11711  * Process ioctl reset port request.
11712  *
11713  * NOTE: Port multiplier code is not completed nor tested.
11714  */
11715 static int
11716 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst,
11717     sata_device_t *sata_device)
11718 {
11719 	int cport, pmport, qual;
11720 	int rv = 0;
11721 
11722 	cport = sata_device->satadev_addr.cport;
11723 	pmport = sata_device->satadev_addr.pmport;
11724 	qual = sata_device->satadev_addr.qual;
11725 
11726 	/* Sanity check */
11727 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
11728 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11729 		    "sata_hba_ioctl: sata_hba_tran missing required "
11730 		    "function sata_tran_reset_dport"));
11731 		return (ENOTSUP);
11732 	}
11733 
11734 	/* Ask HBA to reset port */
11735 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
11736 	    sata_device) != SATA_SUCCESS) {
11737 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11738 		    "sata_hba_ioctl: reset port: failed %d:%d",
11739 		    cport, pmport));
11740 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11741 		    cport_mutex);
11742 		sata_update_port_info(sata_hba_inst, sata_device);
11743 		if (qual == SATA_ADDR_CPORT)
11744 			SATA_CPORT_STATE(sata_hba_inst, cport) =
11745 			    SATA_PSTATE_FAILED;
11746 		else
11747 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
11748 			    SATA_PSTATE_FAILED;
11749 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11750 		    cport_mutex);
11751 		rv = EIO;
11752 	}
11753 	/*
11754 	 * Beacuse the port was reset, it should be probed and
11755 	 * attached device reinitialized. At this point the
11756 	 * port state is unknown - it's state is HBA-specific.
11757 	 * Re-probe port to get its state.
11758 	 */
11759 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11760 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) {
11761 		rv = EIO;
11762 	}
11763 	return (rv);
11764 }
11765 
11766 /*
11767  * Process ioctl reset device request.
11768  *
11769  * NOTE: Port multiplier code is not completed nor tested.
11770  */
11771 static int
11772 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst,
11773     sata_device_t *sata_device)
11774 {
11775 	sata_drive_info_t *sdinfo;
11776 	int cport, pmport, qual;
11777 	int rv = 0;
11778 
11779 	/* Sanity check */
11780 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
11781 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11782 		    "sata_hba_ioctl: sata_hba_tran missing required "
11783 		    "function sata_tran_reset_dport"));
11784 		return (ENOTSUP);
11785 	}
11786 
11787 	cport = sata_device->satadev_addr.cport;
11788 	pmport = sata_device->satadev_addr.pmport;
11789 	qual = sata_device->satadev_addr.qual;
11790 
11791 	if (qual == SATA_ADDR_CPORT)
11792 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11793 	else
11794 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11795 
11796 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11797 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
11798 	if (sdinfo == NULL) {
11799 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11800 		return (EINVAL);
11801 	}
11802 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11803 
11804 	/* Ask HBA to reset device */
11805 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
11806 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11807 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11808 		    "sata_hba_ioctl: reset device: failed at port %d:%d",
11809 		    cport, pmport));
11810 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11811 		    cport_mutex);
11812 		sata_update_port_info(sata_hba_inst, sata_device);
11813 		/*
11814 		 * Device info structure remains attached. Another device reset
11815 		 * or port disconnect/connect and re-probing is
11816 		 * needed to change it's state
11817 		 */
11818 		sdinfo->satadrv_state &= ~SATA_STATE_READY;
11819 		sdinfo->satadrv_state |= SATA_DSTATE_FAILED;
11820 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11821 		rv = EIO;
11822 	}
11823 	/*
11824 	 * If attached device was a port multiplier, some extra processing
11825 	 * may be needed, to bring it back (if port re-probing did not handle
11826 	 * it). Add such code here.
11827 	 */
11828 	return (rv);
11829 }
11830 
11831 
11832 /*
11833  * Process ioctl reset all request.
11834  *
11835  * NOTE: Port multiplier code is not completed nor tested.
11836  */
11837 static int
11838 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst)
11839 {
11840 	sata_device_t sata_device;
11841 	int rv = 0;
11842 	int tcport;
11843 	int tpmport = 0;
11844 
11845 	sata_device.satadev_rev = SATA_DEVICE_REV;
11846 
11847 	/*
11848 	 * There is no protection here for configured devices.
11849 	 */
11850 	/* Sanity check */
11851 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
11852 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11853 		    "sata_hba_ioctl: sata_hba_tran missing required "
11854 		    "function sata_tran_reset_dport"));
11855 		return (ENOTSUP);
11856 	}
11857 
11858 	/*
11859 	 * Need to lock all ports, not just one.
11860 	 * If any port is locked by event processing, fail the whole operation.
11861 	 * One port is already locked, but for simplicity lock it again.
11862 	 */
11863 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
11864 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11865 		    cport_mutex);
11866 		if (((SATA_CPORT_INFO(sata_hba_inst, tcport)->
11867 		    cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) {
11868 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11869 			    cport_mutex);
11870 			rv = EBUSY;
11871 			break;
11872 		} else {
11873 			SATA_CPORT_INFO(sata_hba_inst, tcport)->
11874 			    cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
11875 			/*
11876 			 * If there is a port multiplier attached, we may need
11877 			 * to lock its port as well. If so, add such code here.
11878 			 */
11879 		}
11880 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11881 		    cport_mutex);
11882 	}
11883 
11884 	if (rv == 0) {
11885 		/*
11886 		 * All cports were successfully locked.
11887 		 * Reset main SATA controller only for now - no PMult.
11888 		 */
11889 		sata_device.satadev_addr.qual = SATA_ADDR_CNTRL;
11890 
11891 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
11892 		    (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) {
11893 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11894 			    "sata_hba_ioctl: reset controller failed"));
11895 			return (EIO);
11896 		}
11897 		/*
11898 		 * Because ports were reset, port states are unknown.
11899 		 * They should be re-probed to get their state and
11900 		 * attached devices should be reinitialized.
11901 		 * Add code here to re-probe port multiplier device ports.
11902 		 */
11903 		for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst);
11904 		    tcport++) {
11905 			sata_device.satadev_addr.cport = tcport;
11906 			sata_device.satadev_addr.pmport = tpmport;
11907 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
11908 
11909 			if (sata_reprobe_port(sata_hba_inst, &sata_device,
11910 			    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
11911 				rv = EIO;
11912 		}
11913 	}
11914 	/*
11915 	 * Unlock all ports
11916 	 */
11917 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
11918 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11919 		    cport_mutex);
11920 		SATA_CPORT_INFO(sata_hba_inst, tcport)->
11921 		    cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
11922 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11923 		    cport_mutex);
11924 	}
11925 
11926 	/*
11927 	 * This operation returns EFAULT if either reset
11928 	 * controller failed or a re-probing of any ports failed.
11929 	 */
11930 	return (rv);
11931 }
11932 
11933 
11934 /*
11935  * Process ioctl port self test request.
11936  *
11937  * NOTE: Port multiplier code is not completed nor tested.
11938  */
11939 static int
11940 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst,
11941     sata_device_t *sata_device)
11942 {
11943 	int cport, pmport, qual;
11944 	int rv = 0;
11945 
11946 	/* Sanity check */
11947 	if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL)
11948 		return (ENOTSUP);
11949 
11950 	cport = sata_device->satadev_addr.cport;
11951 	pmport = sata_device->satadev_addr.pmport;
11952 	qual = sata_device->satadev_addr.qual;
11953 
11954 	/*
11955 	 * There is no protection here for a configured
11956 	 * device attached to this port.
11957 	 */
11958 
11959 	if ((*SATA_SELFTEST_FUNC(sata_hba_inst))
11960 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11961 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11962 		    "sata_hba_ioctl: port selftest: "
11963 		    "failed port %d:%d", cport, pmport));
11964 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11965 		    cport_mutex);
11966 		sata_update_port_info(sata_hba_inst, sata_device);
11967 		if (qual == SATA_ADDR_CPORT)
11968 			SATA_CPORT_STATE(sata_hba_inst, cport) =
11969 			    SATA_PSTATE_FAILED;
11970 		else /* port ultiplier device port */
11971 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
11972 			    SATA_PSTATE_FAILED;
11973 
11974 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11975 		    cport_mutex);
11976 		return (EIO);
11977 	}
11978 	/*
11979 	 * Beacuse the port was reset in the course of testing, it should be
11980 	 * re-probed and attached device state should be restored. At this
11981 	 * point the port state is unknown - it's state is HBA-specific.
11982 	 * Force port re-probing to get it into a known state.
11983 	 */
11984 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11985 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
11986 		rv = EIO;
11987 	return (rv);
11988 }
11989 
11990 
11991 /*
11992  * sata_cfgadm_state:
11993  * Use the sata port state and state of the target node to figure out
11994  * the cfgadm_state.
11995  *
11996  * The port argument is a value with encoded cport,
11997  * pmport and address qualifier, in the same manner as a scsi target number.
11998  * SCSI_TO_SATA_CPORT macro extracts cport number,
11999  * SCSI_TO_SATA_PMPORT extracts pmport number and
12000  * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag.
12001  *
12002  * For now, support is for cports only - no port multiplier device ports.
12003  */
12004 
12005 static void
12006 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port,
12007     devctl_ap_state_t *ap_state)
12008 {
12009 	uint16_t	cport;
12010 	int		port_state;
12011 
12012 	/* Cport only */
12013 	cport = SCSI_TO_SATA_CPORT(port);
12014 
12015 	port_state = SATA_CPORT_STATE(sata_hba_inst, cport);
12016 	if (port_state & SATA_PSTATE_SHUTDOWN ||
12017 	    port_state & SATA_PSTATE_FAILED) {
12018 		ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
12019 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12020 		if (port_state & SATA_PSTATE_FAILED)
12021 			ap_state->ap_condition = AP_COND_FAILED;
12022 		else
12023 			ap_state->ap_condition = AP_COND_UNKNOWN;
12024 
12025 		return;
12026 	}
12027 
12028 	/* Need to check pmult device port here as well, when supported */
12029 
12030 	/* Port is enabled and ready */
12031 
12032 	switch (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport)) {
12033 	case SATA_DTYPE_NONE:
12034 	{
12035 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12036 		ap_state->ap_condition = AP_COND_OK;
12037 		/* No device attached */
12038 		ap_state->ap_rstate = AP_RSTATE_EMPTY;
12039 		break;
12040 	}
12041 	case SATA_DTYPE_UNKNOWN:
12042 	case SATA_DTYPE_ATAPINONCD:
12043 	case SATA_DTYPE_PMULT:	/* Until PMult is supported */
12044 	case SATA_DTYPE_ATADISK:
12045 	case SATA_DTYPE_ATAPICD:
12046 	{
12047 		dev_info_t *tdip = NULL;
12048 		dev_info_t *dip = NULL;
12049 		int circ;
12050 
12051 		dip = SATA_DIP(sata_hba_inst);
12052 		tdip = sata_get_target_dip(dip, port);
12053 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
12054 		if (tdip != NULL) {
12055 			ndi_devi_enter(dip, &circ);
12056 			mutex_enter(&(DEVI(tdip)->devi_lock));
12057 			if (DEVI_IS_DEVICE_REMOVED(tdip)) {
12058 				/*
12059 				 * There could be the case where previously
12060 				 * configured and opened device was removed
12061 				 * and unknown device was plugged.
12062 				 * In such case we want to show a device, and
12063 				 * its configured or unconfigured state but
12064 				 * indicate unusable condition untill the
12065 				 * old target node is released and removed.
12066 				 */
12067 				ap_state->ap_condition = AP_COND_UNUSABLE;
12068 			} else {
12069 				ap_state->ap_condition = AP_COND_OK;
12070 			}
12071 			if ((DEVI_IS_DEVICE_OFFLINE(tdip)) ||
12072 			    (DEVI_IS_DEVICE_DOWN(tdip))) {
12073 				ap_state->ap_ostate =
12074 				    AP_OSTATE_UNCONFIGURED;
12075 			} else {
12076 				ap_state->ap_ostate =
12077 				    AP_OSTATE_CONFIGURED;
12078 			}
12079 			mutex_exit(&(DEVI(tdip)->devi_lock));
12080 			ndi_devi_exit(dip, circ);
12081 		} else {
12082 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12083 			ap_state->ap_condition = AP_COND_UNKNOWN;
12084 		}
12085 		break;
12086 	}
12087 	default:
12088 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
12089 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12090 		ap_state->ap_condition = AP_COND_UNKNOWN;
12091 		/*
12092 		 * This is actually internal error condition (non fatal),
12093 		 * because we have already checked all defined device types.
12094 		 */
12095 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12096 		    "sata_cfgadm_state: Internal error: "
12097 		    "unknown device type"));
12098 		break;
12099 	}
12100 }
12101 
12102 
12103 /*
12104  * Process ioctl get device path request.
12105  *
12106  * NOTE: Port multiplier code is not completed nor tested.
12107  */
12108 static int
12109 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst,
12110     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12111 {
12112 	char path[MAXPATHLEN];
12113 	uint32_t size;
12114 	dev_info_t *tdip;
12115 
12116 	(void) strcpy(path, "/devices");
12117 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
12118 	    &sata_device->satadev_addr)) == NULL) {
12119 		/*
12120 		 * No such device. If this is a request for a size, do not
12121 		 * return EINVAL for non-existing target, because cfgadm
12122 		 * will then indicate a meaningless ioctl failure.
12123 		 * If this is a request for a path, indicate invalid
12124 		 * argument.
12125 		 */
12126 		if (ioc->get_size == 0)
12127 			return (EINVAL);
12128 	} else {
12129 		(void) ddi_pathname(tdip, path + strlen(path));
12130 	}
12131 	size = strlen(path) + 1;
12132 
12133 	if (ioc->get_size != 0) {
12134 		if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz,
12135 		    mode) != 0)
12136 			return (EFAULT);
12137 	} else {
12138 		if (ioc->bufsiz != size)
12139 			return (EINVAL);
12140 
12141 		else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz,
12142 		    mode) != 0)
12143 			return (EFAULT);
12144 	}
12145 	return (0);
12146 }
12147 
12148 /*
12149  * Process ioctl get attachment point type request.
12150  *
12151  * NOTE: Port multiplier code is not completed nor tested.
12152  */
12153 static	int
12154 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst,
12155     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12156 {
12157 	uint32_t	type_len;
12158 	const char	*ap_type;
12159 	int		dev_type;
12160 
12161 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12162 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst,
12163 		    sata_device->satadev_addr.cport);
12164 	else /* pmport */
12165 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12166 		    sata_device->satadev_addr.cport,
12167 		    sata_device->satadev_addr.pmport);
12168 
12169 	switch (dev_type) {
12170 	case SATA_DTYPE_NONE:
12171 		ap_type = "port";
12172 		break;
12173 
12174 	case SATA_DTYPE_ATADISK:
12175 		ap_type = "disk";
12176 		break;
12177 
12178 	case SATA_DTYPE_ATAPICD:
12179 		ap_type = "cd/dvd";
12180 		break;
12181 
12182 	case SATA_DTYPE_PMULT:
12183 		ap_type = "pmult";
12184 		break;
12185 
12186 	case SATA_DTYPE_UNKNOWN:
12187 		ap_type = "unknown";
12188 		break;
12189 
12190 	default:
12191 		ap_type = "unsupported";
12192 		break;
12193 
12194 	} /* end of dev_type switch */
12195 
12196 	type_len = strlen(ap_type) + 1;
12197 
12198 	if (ioc->get_size) {
12199 		if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz,
12200 		    mode) != 0)
12201 			return (EFAULT);
12202 	} else {
12203 		if (ioc->bufsiz != type_len)
12204 			return (EINVAL);
12205 
12206 		if (ddi_copyout((void *)ap_type, ioc->buf,
12207 		    ioc->bufsiz, mode) != 0)
12208 			return (EFAULT);
12209 	}
12210 	return (0);
12211 
12212 }
12213 
12214 /*
12215  * Process ioctl get device model info request.
12216  * This operation should return to cfgadm the device model
12217  * information string
12218  *
12219  * NOTE: Port multiplier code is not completed nor tested.
12220  */
12221 static	int
12222 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst,
12223     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12224 {
12225 	sata_drive_info_t *sdinfo;
12226 	uint32_t info_len;
12227 	char ap_info[SATA_ID_MODEL_LEN + 1];
12228 
12229 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12230 	    sata_device->satadev_addr.cport)->cport_mutex);
12231 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12232 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
12233 	else /* port multiplier */
12234 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
12235 
12236 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12237 	if (sdinfo == NULL) {
12238 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12239 		    sata_device->satadev_addr.cport)->cport_mutex);
12240 		return (EINVAL);
12241 	}
12242 
12243 #ifdef	_LITTLE_ENDIAN
12244 	swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
12245 #else	/* _LITTLE_ENDIAN */
12246 	bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
12247 #endif	/* _LITTLE_ENDIAN */
12248 
12249 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12250 	    sata_device->satadev_addr.cport)->cport_mutex);
12251 
12252 	ap_info[SATA_ID_MODEL_LEN] = '\0';
12253 
12254 	info_len = strlen(ap_info) + 1;
12255 
12256 	if (ioc->get_size) {
12257 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12258 		    mode) != 0)
12259 			return (EFAULT);
12260 	} else {
12261 		if (ioc->bufsiz < info_len)
12262 			return (EINVAL);
12263 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12264 		    mode) != 0)
12265 			return (EFAULT);
12266 	}
12267 	return (0);
12268 }
12269 
12270 
12271 /*
12272  * Process ioctl get device firmware revision info request.
12273  * This operation should return to cfgadm the device firmware revision
12274  * information string
12275  *
12276  * NOTE: Port multiplier code is not completed nor tested.
12277  */
12278 static	int
12279 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst,
12280     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12281 {
12282 	sata_drive_info_t *sdinfo;
12283 	uint32_t info_len;
12284 	char ap_info[SATA_ID_FW_LEN + 1];
12285 
12286 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12287 	    sata_device->satadev_addr.cport)->cport_mutex);
12288 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12289 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
12290 	else /* port multiplier */
12291 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
12292 
12293 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12294 	if (sdinfo == NULL) {
12295 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12296 		    sata_device->satadev_addr.cport)->cport_mutex);
12297 		return (EINVAL);
12298 	}
12299 
12300 #ifdef	_LITTLE_ENDIAN
12301 	swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
12302 #else	/* _LITTLE_ENDIAN */
12303 	bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
12304 #endif	/* _LITTLE_ENDIAN */
12305 
12306 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12307 	    sata_device->satadev_addr.cport)->cport_mutex);
12308 
12309 	ap_info[SATA_ID_FW_LEN] = '\0';
12310 
12311 	info_len = strlen(ap_info) + 1;
12312 
12313 	if (ioc->get_size) {
12314 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12315 		    mode) != 0)
12316 			return (EFAULT);
12317 	} else {
12318 		if (ioc->bufsiz < info_len)
12319 			return (EINVAL);
12320 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12321 		    mode) != 0)
12322 			return (EFAULT);
12323 	}
12324 	return (0);
12325 }
12326 
12327 
12328 /*
12329  * Process ioctl get device serial number info request.
12330  * This operation should return to cfgadm the device serial number string.
12331  *
12332  * NOTE: Port multiplier code is not completed nor tested.
12333  */
12334 static	int
12335 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst,
12336     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12337 {
12338 	sata_drive_info_t *sdinfo;
12339 	uint32_t info_len;
12340 	char ap_info[SATA_ID_SERIAL_LEN + 1];
12341 
12342 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12343 	    sata_device->satadev_addr.cport)->cport_mutex);
12344 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12345 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
12346 	else /* port multiplier */
12347 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
12348 
12349 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12350 	if (sdinfo == NULL) {
12351 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12352 		    sata_device->satadev_addr.cport)->cport_mutex);
12353 		return (EINVAL);
12354 	}
12355 
12356 #ifdef	_LITTLE_ENDIAN
12357 	swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
12358 #else	/* _LITTLE_ENDIAN */
12359 	bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
12360 #endif	/* _LITTLE_ENDIAN */
12361 
12362 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12363 	    sata_device->satadev_addr.cport)->cport_mutex);
12364 
12365 	ap_info[SATA_ID_SERIAL_LEN] = '\0';
12366 
12367 	info_len = strlen(ap_info) + 1;
12368 
12369 	if (ioc->get_size) {
12370 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12371 		    mode) != 0)
12372 			return (EFAULT);
12373 	} else {
12374 		if (ioc->bufsiz < info_len)
12375 			return (EINVAL);
12376 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12377 		    mode) != 0)
12378 			return (EFAULT);
12379 	}
12380 	return (0);
12381 }
12382 
12383 
12384 /*
12385  * Preset scsi extended sense data (to NO SENSE)
12386  * First 18 bytes of the sense data are preset to current valid sense
12387  * with a key NO SENSE data.
12388  *
12389  * Returns void
12390  */
12391 static void
12392 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense)
12393 {
12394 	sense->es_valid = 1;		/* Valid sense */
12395 	sense->es_class = CLASS_EXTENDED_SENSE;	/* 0x70 - current err */
12396 	sense->es_key = KEY_NO_SENSE;
12397 	sense->es_info_1 = 0;
12398 	sense->es_info_2 = 0;
12399 	sense->es_info_3 = 0;
12400 	sense->es_info_4 = 0;
12401 	sense->es_add_len = 10;	/* Additional length - replace with a def */
12402 	sense->es_cmd_info[0] = 0;
12403 	sense->es_cmd_info[1] = 0;
12404 	sense->es_cmd_info[2] = 0;
12405 	sense->es_cmd_info[3] = 0;
12406 	sense->es_add_code = 0;
12407 	sense->es_qual_code = 0;
12408 }
12409 
12410 /*
12411  * Register a legacy cmdk-style devid for the target (disk) device.
12412  *
12413  * Note: This function is called only when the HBA devinfo node has the
12414  * property "use-cmdk-devid-format" set. This property indicates that
12415  * devid compatible with old cmdk (target) driver is to be generated
12416  * for any target device attached to this controller. This will take
12417  * precedence over the devid generated by sd (target) driver.
12418  * This function is derived from cmdk_devid_setup() function in cmdk.c.
12419  */
12420 static void
12421 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo)
12422 {
12423 	char	*hwid;
12424 	int	modlen;
12425 	int	serlen;
12426 	int	rval;
12427 	ddi_devid_t	devid;
12428 
12429 	/*
12430 	 * device ID is a concatanation of model number, "=", serial number.
12431 	 */
12432 	hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP);
12433 	bcopy(&sdinfo->satadrv_id.ai_model, hwid,
12434 	    sizeof (sdinfo->satadrv_id.ai_model));
12435 	swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model));
12436 	modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model));
12437 	if (modlen == 0)
12438 		goto err;
12439 	hwid[modlen++] = '=';
12440 	bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen],
12441 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12442 	swab(&hwid[modlen], &hwid[modlen],
12443 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12444 	serlen = sata_check_modser(&hwid[modlen],
12445 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12446 	if (serlen == 0)
12447 		goto err;
12448 	hwid[modlen + serlen] = 0; /* terminate the hwid string */
12449 
12450 	/* initialize/register devid */
12451 	if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL,
12452 	    (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS)
12453 		rval = ddi_devid_register(dip, devid);
12454 
12455 	if (rval != DDI_SUCCESS)
12456 		cmn_err(CE_WARN, "sata: failed to create devid for the disk"
12457 		    " on port %d", sdinfo->satadrv_addr.cport);
12458 err:
12459 	kmem_free(hwid, LEGACY_HWID_LEN);
12460 }
12461 
12462 /*
12463  * valid model/serial string must contain a non-zero non-space characters.
12464  * trim trailing spaces/NULLs.
12465  */
12466 static int
12467 sata_check_modser(char *buf, int buf_len)
12468 {
12469 	boolean_t ret;
12470 	char *s;
12471 	int i;
12472 	int tb;
12473 	char ch;
12474 
12475 	ret = B_FALSE;
12476 	s = buf;
12477 	for (i = 0; i < buf_len; i++) {
12478 		ch = *s++;
12479 		if (ch != ' ' && ch != '\0')
12480 			tb = i + 1;
12481 		if (ch != ' ' && ch != '\0' && ch != '0')
12482 			ret = B_TRUE;
12483 	}
12484 
12485 	if (ret == B_FALSE)
12486 		return (0); /* invalid string */
12487 
12488 	return (tb); /* return length */
12489 }
12490 
12491 /*
12492  * sata_set_drive_features function compares current device features setting
12493  * with the saved device features settings and, if there is a difference,
12494  * it restores device features setting to the previously saved state.
12495  * It also arbitrarily tries to select the highest supported DMA mode.
12496  * Device Identify or Identify Packet Device data has to be current.
12497  * At the moment read ahead and write cache are considered for all devices.
12498  * For atapi devices, Removable Media Status Notification is set in addition
12499  * to common features.
12500  *
12501  * This function cannot be called in the interrupt context (it may sleep).
12502  *
12503  * The input argument sdinfo should point to the drive info structure
12504  * to be updated after features are set. Note, that only
12505  * device (packet) identify data is updated, not the flags indicating the
12506  * supported features.
12507  *
12508  * Returns TRUE if successful or there was nothing to do. Device Identify data
12509  * in the drive info structure pointed to by the sdinfo argumens is updated
12510  * even when no features were set or changed.
12511  *
12512  * Returns FALSE if device features could not be set.
12513  *
12514  * Note: This function may fail the port, making it inaccessible.
12515  * In such case the explicit port disconnect/connect or physical device
12516  * detach/attach is required to re-evaluate port state again.
12517  */
12518 
12519 static int
12520 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst,
12521     sata_drive_info_t *sdinfo, int restore)
12522 {
12523 	int rval = SATA_SUCCESS;
12524 	sata_drive_info_t new_sdinfo;
12525 	char *finfo = "sata_set_drive_features: cannot";
12526 	char *finfox;
12527 	int cache_op;
12528 
12529 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
12530 	new_sdinfo.satadrv_addr = sdinfo->satadrv_addr;
12531 	new_sdinfo.satadrv_type = sdinfo->satadrv_type;
12532 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
12533 		/*
12534 		 * Cannot get device identification - retry later
12535 		 */
12536 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12537 		    "%s fetch device identify data\n", finfo));
12538 		return (SATA_FAILURE);
12539 	}
12540 	finfox = (restore != 0) ? " restore device features" :
12541 	    " initialize device features\n";
12542 
12543 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12544 		/* Arbitrarily set UDMA mode */
12545 		if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
12546 		    SATA_SUCCESS) {
12547 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12548 			    "%s set UDMA mode\n", finfo));
12549 			return (SATA_FAILURE);
12550 		}
12551 	} else { /* Assume SATA ATAPI CD/DVD */
12552 		/*  Set Removable Media Status Notification, if necessary */
12553 		if ((new_sdinfo.satadrv_id.ai_cmdset83 &
12554 		    SATA_RM_STATUS_NOTIFIC) != 0 && restore != 0) {
12555 			if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) &&
12556 			    (!(new_sdinfo.satadrv_id.ai_features86 &
12557 			    SATA_RM_STATUS_NOTIFIC))) ||
12558 			    ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) &&
12559 			    (new_sdinfo.satadrv_id.ai_features86 &
12560 			    SATA_RM_STATUS_NOTIFIC))) {
12561 				/* Current setting does not match saved one */
12562 				if (sata_set_rmsn(sata_hba_inst, sdinfo,
12563 				    sdinfo->satadrv_settings &
12564 				    SATA_DEV_RMSN) != SATA_SUCCESS)
12565 					rval = SATA_FAILURE;
12566 			}
12567 		}
12568 		/*
12569 		 * We have to set Multiword DMA or UDMA, if it is supported, as
12570 		 * we want to use DMA transfer mode whenever possible.
12571 		 * Some devices require explicit setting of the DMA mode.
12572 		 */
12573 		if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) {
12574 			/* Set highest supported DMA mode */
12575 			if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
12576 			    SATA_SUCCESS) {
12577 				SATA_LOG_D((sata_hba_inst, CE_WARN,
12578 				    "%s set UDMA mode\n", finfo));
12579 				rval = SATA_FAILURE;
12580 			}
12581 		}
12582 	}
12583 
12584 	if (!(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) &&
12585 	    !(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) {
12586 		/* None of the features is supported - do nothing */
12587 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12588 		    "settable features not supported\n", NULL);
12589 		goto update_sdinfo;
12590 	}
12591 
12592 	if (((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) &&
12593 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) &&
12594 	    ((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) &&
12595 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
12596 		/* Nothing to do */
12597 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12598 		    "no device features to set\n", NULL);
12599 		goto update_sdinfo;
12600 	}
12601 
12602 	if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) &&
12603 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD))) {
12604 		if (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) {
12605 			/* Enable read ahead / read cache */
12606 			cache_op = SATAC_SF_ENABLE_READ_AHEAD;
12607 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12608 			    "enabling read cache\n", NULL);
12609 		} else {
12610 			/* Disable read ahead  / read cache */
12611 			cache_op = SATAC_SF_DISABLE_READ_AHEAD;
12612 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12613 			    "disabling read cache\n", NULL);
12614 		}
12615 
12616 		/* Try to set read cache mode */
12617 		if (sata_set_cache_mode(sata_hba_inst, &new_sdinfo,
12618 		    cache_op) != SATA_SUCCESS) {
12619 			/* Pkt execution failed */
12620 			rval = SATA_FAILURE;
12621 		}
12622 	}
12623 
12624 	if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) &&
12625 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
12626 		if (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) {
12627 			/* Enable write cache */
12628 			cache_op = SATAC_SF_ENABLE_WRITE_CACHE;
12629 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12630 			    "enabling write cache\n", NULL);
12631 		} else {
12632 			/* Disable write cache */
12633 			cache_op = SATAC_SF_DISABLE_WRITE_CACHE;
12634 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12635 			    "disabling write cache\n", NULL);
12636 		}
12637 		/* Try to set write cache mode */
12638 		if (sata_set_cache_mode(sata_hba_inst, &new_sdinfo,
12639 		    cache_op) != SATA_SUCCESS) {
12640 			/* Pkt execution failed */
12641 			rval = SATA_FAILURE;
12642 		}
12643 	}
12644 
12645 	if (rval == SATA_FAILURE)
12646 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12647 		    "%s %s", finfo, finfox));
12648 update_sdinfo:
12649 	/*
12650 	 * We need to fetch Device Identify data again
12651 	 */
12652 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
12653 		/*
12654 		 * Cannot get device identification - retry later
12655 		 */
12656 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12657 		    "%s re-fetch device identify data\n", finfo));
12658 		rval = SATA_FAILURE;
12659 	}
12660 	/* Copy device sata info. */
12661 	sdinfo->satadrv_id = new_sdinfo.satadrv_id;
12662 
12663 	return (rval);
12664 }
12665 
12666 
12667 /*
12668  *
12669  * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if
12670  * unable to determine.
12671  *
12672  * Cannot be called in an interrupt context.
12673  *
12674  * Called by sata_build_lsense_page_2f()
12675  */
12676 
12677 static int
12678 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst,
12679     sata_drive_info_t *sdinfo)
12680 {
12681 	sata_pkt_t *spkt;
12682 	sata_cmd_t *scmd;
12683 	sata_pkt_txlate_t *spx;
12684 	int rval;
12685 
12686 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12687 	spx->txlt_sata_hba_inst = sata_hba_inst;
12688 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12689 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12690 	if (spkt == NULL) {
12691 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12692 		return (-1);
12693 	}
12694 	/* address is needed now */
12695 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12696 
12697 
12698 	/* Fill sata_pkt */
12699 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12700 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12701 	/* Synchronous mode, no callback */
12702 	spkt->satapkt_comp = NULL;
12703 	/* Timeout 30s */
12704 	spkt->satapkt_time = sata_default_pkt_time;
12705 
12706 	scmd = &spkt->satapkt_cmd;
12707 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
12708 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
12709 
12710 	/* Set up which registers need to be returned */
12711 	scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE;
12712 	scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE;
12713 
12714 	/* Build SMART_RETURN_STATUS cmd in the sata_pkt */
12715 	scmd->satacmd_addr_type = 0;		/* N/A */
12716 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
12717 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
12718 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
12719 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
12720 	scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS;
12721 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12722 	scmd->satacmd_cmd_reg = SATAC_SMART;
12723 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12724 	    sdinfo->satadrv_addr.cport)));
12725 
12726 
12727 	/* Send pkt to SATA HBA driver */
12728 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12729 	    SATA_TRAN_ACCEPTED ||
12730 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12731 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12732 		    sdinfo->satadrv_addr.cport)));
12733 		/*
12734 		 * Whoops, no SMART RETURN STATUS
12735 		 */
12736 		rval = -1;
12737 	} else {
12738 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12739 		    sdinfo->satadrv_addr.cport)));
12740 		if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
12741 			rval = -1;
12742 			goto fail;
12743 		}
12744 		if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
12745 			rval = -1;
12746 			goto fail;
12747 		}
12748 		if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) &&
12749 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2))
12750 			rval = 0;
12751 		else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) &&
12752 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4))
12753 			rval = 1;
12754 		else {
12755 			rval = -1;
12756 			goto fail;
12757 		}
12758 	}
12759 fail:
12760 	/* Free allocated resources */
12761 	sata_pkt_free(spx);
12762 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12763 
12764 	return (rval);
12765 }
12766 
12767 /*
12768  *
12769  * Returns 0 if succeeded, -1 otherwise
12770  *
12771  * Cannot be called in an interrupt context.
12772  *
12773  */
12774 static int
12775 sata_fetch_smart_data(
12776 	sata_hba_inst_t *sata_hba_inst,
12777 	sata_drive_info_t *sdinfo,
12778 	struct smart_data *smart_data)
12779 {
12780 	sata_pkt_t *spkt;
12781 	sata_cmd_t *scmd;
12782 	sata_pkt_txlate_t *spx;
12783 	int rval;
12784 
12785 #if ! defined(lint)
12786 	ASSERT(sizeof (struct smart_data) == 512);
12787 #endif
12788 
12789 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12790 	spx->txlt_sata_hba_inst = sata_hba_inst;
12791 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12792 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12793 	if (spkt == NULL) {
12794 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12795 		return (-1);
12796 	}
12797 	/* address is needed now */
12798 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12799 
12800 
12801 	/* Fill sata_pkt */
12802 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12803 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12804 	/* Synchronous mode, no callback */
12805 	spkt->satapkt_comp = NULL;
12806 	/* Timeout 30s */
12807 	spkt->satapkt_time = sata_default_pkt_time;
12808 
12809 	scmd = &spkt->satapkt_cmd;
12810 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
12811 
12812 	/*
12813 	 * Allocate buffer for SMART data
12814 	 */
12815 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
12816 	    sizeof (struct smart_data));
12817 	if (scmd->satacmd_bp == NULL) {
12818 		sata_pkt_free(spx);
12819 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12820 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12821 		    "sata_fetch_smart_data: "
12822 		    "cannot allocate buffer"));
12823 		return (-1);
12824 	}
12825 
12826 
12827 	/* Build SMART_READ_DATA cmd in the sata_pkt */
12828 	scmd->satacmd_addr_type = 0;		/* N/A */
12829 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
12830 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
12831 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
12832 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
12833 	scmd->satacmd_features_reg = SATA_SMART_READ_DATA;
12834 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12835 	scmd->satacmd_cmd_reg = SATAC_SMART;
12836 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12837 	    sdinfo->satadrv_addr.cport)));
12838 
12839 	/* Send pkt to SATA HBA driver */
12840 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12841 	    SATA_TRAN_ACCEPTED ||
12842 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12843 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12844 		    sdinfo->satadrv_addr.cport)));
12845 		/*
12846 		 * Whoops, no SMART DATA available
12847 		 */
12848 		rval = -1;
12849 		goto fail;
12850 	} else {
12851 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12852 		    sdinfo->satadrv_addr.cport)));
12853 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
12854 		    DDI_DMA_SYNC_FORKERNEL);
12855 		ASSERT(rval == DDI_SUCCESS);
12856 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data,
12857 		    sizeof (struct smart_data));
12858 	}
12859 
12860 fail:
12861 	/* Free allocated resources */
12862 	sata_free_local_buffer(spx);
12863 	sata_pkt_free(spx);
12864 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12865 
12866 	return (rval);
12867 }
12868 
12869 /*
12870  * Used by LOG SENSE page 0x10
12871  *
12872  * return 0 for success, -1 otherwise
12873  *
12874  */
12875 static int
12876 sata_ext_smart_selftest_read_log(
12877 	sata_hba_inst_t *sata_hba_inst,
12878 	sata_drive_info_t *sdinfo,
12879 	struct smart_ext_selftest_log *ext_selftest_log,
12880 	uint16_t block_num)
12881 {
12882 	sata_pkt_txlate_t *spx;
12883 	sata_pkt_t *spkt;
12884 	sata_cmd_t *scmd;
12885 	int rval;
12886 
12887 #if ! defined(lint)
12888 	ASSERT(sizeof (struct smart_ext_selftest_log) == 512);
12889 #endif
12890 
12891 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12892 	spx->txlt_sata_hba_inst = sata_hba_inst;
12893 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12894 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12895 	if (spkt == NULL) {
12896 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12897 		return (-1);
12898 	}
12899 	/* address is needed now */
12900 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12901 
12902 
12903 	/* Fill sata_pkt */
12904 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12905 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12906 	/* Synchronous mode, no callback */
12907 	spkt->satapkt_comp = NULL;
12908 	/* Timeout 30s */
12909 	spkt->satapkt_time = sata_default_pkt_time;
12910 
12911 	scmd = &spkt->satapkt_cmd;
12912 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
12913 
12914 	/*
12915 	 * Allocate buffer for SMART extended self-test log
12916 	 */
12917 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
12918 	    sizeof (struct smart_ext_selftest_log));
12919 	if (scmd->satacmd_bp == NULL) {
12920 		sata_pkt_free(spx);
12921 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12922 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12923 		    "sata_ext_smart_selftest_log: "
12924 		    "cannot allocate buffer"));
12925 		return (-1);
12926 	}
12927 
12928 	/* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */
12929 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
12930 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of selftest log */
12931 	scmd->satacmd_sec_count_msb = 0;	/* One sector of selftest log */
12932 	scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE;
12933 	scmd->satacmd_lba_low_msb = 0;
12934 	scmd->satacmd_lba_mid_lsb = block_num & 0xff;
12935 	scmd->satacmd_lba_mid_msb = block_num >> 8;
12936 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12937 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
12938 
12939 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12940 	    sdinfo->satadrv_addr.cport)));
12941 
12942 	/* Send pkt to SATA HBA driver */
12943 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12944 	    SATA_TRAN_ACCEPTED ||
12945 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12946 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12947 		    sdinfo->satadrv_addr.cport)));
12948 
12949 		/*
12950 		 * Whoops, no SMART selftest log info available
12951 		 */
12952 		rval = -1;
12953 		goto fail;
12954 	} else {
12955 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12956 		    sdinfo->satadrv_addr.cport)));
12957 
12958 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
12959 		    DDI_DMA_SYNC_FORKERNEL);
12960 		ASSERT(rval == DDI_SUCCESS);
12961 		bcopy(scmd->satacmd_bp->b_un.b_addr,
12962 		    (uint8_t *)ext_selftest_log,
12963 		    sizeof (struct smart_ext_selftest_log));
12964 		rval = 0;
12965 	}
12966 
12967 fail:
12968 	/* Free allocated resources */
12969 	sata_free_local_buffer(spx);
12970 	sata_pkt_free(spx);
12971 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12972 
12973 	return (rval);
12974 }
12975 
12976 /*
12977  * Returns 0 for success, -1 otherwise
12978  *
12979  * SMART self-test log data is returned in buffer pointed to by selftest_log
12980  */
12981 static int
12982 sata_smart_selftest_log(
12983 	sata_hba_inst_t *sata_hba_inst,
12984 	sata_drive_info_t *sdinfo,
12985 	struct smart_selftest_log *selftest_log)
12986 {
12987 	sata_pkt_t *spkt;
12988 	sata_cmd_t *scmd;
12989 	sata_pkt_txlate_t *spx;
12990 	int rval;
12991 
12992 #if ! defined(lint)
12993 	ASSERT(sizeof (struct smart_selftest_log) == 512);
12994 #endif
12995 
12996 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12997 	spx->txlt_sata_hba_inst = sata_hba_inst;
12998 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12999 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13000 	if (spkt == NULL) {
13001 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13002 		return (-1);
13003 	}
13004 	/* address is needed now */
13005 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13006 
13007 
13008 	/* Fill sata_pkt */
13009 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13010 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13011 	/* Synchronous mode, no callback */
13012 	spkt->satapkt_comp = NULL;
13013 	/* Timeout 30s */
13014 	spkt->satapkt_time = sata_default_pkt_time;
13015 
13016 	scmd = &spkt->satapkt_cmd;
13017 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13018 
13019 	/*
13020 	 * Allocate buffer for SMART SELFTEST LOG
13021 	 */
13022 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13023 	    sizeof (struct smart_selftest_log));
13024 	if (scmd->satacmd_bp == NULL) {
13025 		sata_pkt_free(spx);
13026 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13027 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13028 		    "sata_smart_selftest_log: "
13029 		    "cannot allocate buffer"));
13030 		return (-1);
13031 	}
13032 
13033 	/* Build SMART_READ_LOG cmd in the sata_pkt */
13034 	scmd->satacmd_addr_type = 0;		/* N/A */
13035 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of SMART log */
13036 	scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE;
13037 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13038 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13039 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
13040 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13041 	scmd->satacmd_cmd_reg = SATAC_SMART;
13042 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13043 	    sdinfo->satadrv_addr.cport)));
13044 
13045 	/* Send pkt to SATA HBA driver */
13046 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13047 	    SATA_TRAN_ACCEPTED ||
13048 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13049 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13050 		    sdinfo->satadrv_addr.cport)));
13051 		/*
13052 		 * Whoops, no SMART DATA available
13053 		 */
13054 		rval = -1;
13055 		goto fail;
13056 	} else {
13057 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13058 		    sdinfo->satadrv_addr.cport)));
13059 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13060 		    DDI_DMA_SYNC_FORKERNEL);
13061 		ASSERT(rval == DDI_SUCCESS);
13062 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log,
13063 		    sizeof (struct smart_selftest_log));
13064 		rval = 0;
13065 	}
13066 
13067 fail:
13068 	/* Free allocated resources */
13069 	sata_free_local_buffer(spx);
13070 	sata_pkt_free(spx);
13071 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13072 
13073 	return (rval);
13074 }
13075 
13076 
13077 /*
13078  * Returns 0 for success, -1 otherwise
13079  *
13080  * SMART READ LOG data is returned in buffer pointed to by smart_log
13081  */
13082 static int
13083 sata_smart_read_log(
13084 	sata_hba_inst_t *sata_hba_inst,
13085 	sata_drive_info_t *sdinfo,
13086 	uint8_t *smart_log,		/* where the data should be returned */
13087 	uint8_t which_log,		/* which log should be returned */
13088 	uint8_t log_size)		/* # of 512 bytes in log */
13089 {
13090 	sata_pkt_t *spkt;
13091 	sata_cmd_t *scmd;
13092 	sata_pkt_txlate_t *spx;
13093 	int rval;
13094 
13095 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13096 	spx->txlt_sata_hba_inst = sata_hba_inst;
13097 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13098 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13099 	if (spkt == NULL) {
13100 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13101 		return (-1);
13102 	}
13103 	/* address is needed now */
13104 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13105 
13106 
13107 	/* Fill sata_pkt */
13108 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13109 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13110 	/* Synchronous mode, no callback */
13111 	spkt->satapkt_comp = NULL;
13112 	/* Timeout 30s */
13113 	spkt->satapkt_time = sata_default_pkt_time;
13114 
13115 	scmd = &spkt->satapkt_cmd;
13116 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13117 
13118 	/*
13119 	 * Allocate buffer for SMART READ LOG
13120 	 */
13121 	scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512);
13122 	if (scmd->satacmd_bp == NULL) {
13123 		sata_pkt_free(spx);
13124 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13125 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13126 		    "sata_smart_read_log: " "cannot allocate buffer"));
13127 		return (-1);
13128 	}
13129 
13130 	/* Build SMART_READ_LOG cmd in the sata_pkt */
13131 	scmd->satacmd_addr_type = 0;		/* N/A */
13132 	scmd->satacmd_sec_count_lsb = log_size;	/* what the caller asked for */
13133 	scmd->satacmd_lba_low_lsb = which_log;	/* which log page */
13134 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13135 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13136 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
13137 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13138 	scmd->satacmd_cmd_reg = SATAC_SMART;
13139 
13140 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13141 	    sdinfo->satadrv_addr.cport)));
13142 
13143 	/* Send pkt to SATA HBA driver */
13144 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13145 	    SATA_TRAN_ACCEPTED ||
13146 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13147 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13148 		    sdinfo->satadrv_addr.cport)));
13149 
13150 		/*
13151 		 * Whoops, no SMART DATA available
13152 		 */
13153 		rval = -1;
13154 		goto fail;
13155 	} else {
13156 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13157 		    sdinfo->satadrv_addr.cport)));
13158 
13159 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13160 		    DDI_DMA_SYNC_FORKERNEL);
13161 		ASSERT(rval == DDI_SUCCESS);
13162 		bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512);
13163 		rval = 0;
13164 	}
13165 
13166 fail:
13167 	/* Free allocated resources */
13168 	sata_free_local_buffer(spx);
13169 	sata_pkt_free(spx);
13170 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13171 
13172 	return (rval);
13173 }
13174 
13175 /*
13176  * Used by LOG SENSE page 0x10
13177  *
13178  * return 0 for success, -1 otherwise
13179  *
13180  */
13181 static int
13182 sata_read_log_ext_directory(
13183 	sata_hba_inst_t *sata_hba_inst,
13184 	sata_drive_info_t *sdinfo,
13185 	struct read_log_ext_directory *logdir)
13186 {
13187 	sata_pkt_txlate_t *spx;
13188 	sata_pkt_t *spkt;
13189 	sata_cmd_t *scmd;
13190 	int rval;
13191 
13192 #if ! defined(lint)
13193 	ASSERT(sizeof (struct read_log_ext_directory) == 512);
13194 #endif
13195 
13196 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13197 	spx->txlt_sata_hba_inst = sata_hba_inst;
13198 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13199 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13200 	if (spkt == NULL) {
13201 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13202 		return (-1);
13203 	}
13204 
13205 	/* Fill sata_pkt */
13206 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13207 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13208 	/* Synchronous mode, no callback */
13209 	spkt->satapkt_comp = NULL;
13210 	/* Timeout 30s */
13211 	spkt->satapkt_time = sata_default_pkt_time;
13212 
13213 	scmd = &spkt->satapkt_cmd;
13214 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13215 
13216 	/*
13217 	 * Allocate buffer for SMART READ LOG EXTENDED command
13218 	 */
13219 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13220 	    sizeof (struct read_log_ext_directory));
13221 	if (scmd->satacmd_bp == NULL) {
13222 		sata_pkt_free(spx);
13223 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13224 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13225 		    "sata_read_log_ext_directory: "
13226 		    "cannot allocate buffer"));
13227 		return (-1);
13228 	}
13229 
13230 	/* Build READ LOG EXT w/ log directory cmd in the  sata_pkt */
13231 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
13232 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of directory */
13233 	scmd->satacmd_sec_count_msb = 0;	/* One sector of directory */
13234 	scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY;
13235 	scmd->satacmd_lba_low_msb = 0;
13236 	scmd->satacmd_lba_mid_lsb = 0;
13237 	scmd->satacmd_lba_mid_msb = 0;
13238 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13239 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
13240 
13241 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13242 	    sdinfo->satadrv_addr.cport)));
13243 
13244 	/* Send pkt to SATA HBA driver */
13245 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13246 	    SATA_TRAN_ACCEPTED ||
13247 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13248 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13249 		    sdinfo->satadrv_addr.cport)));
13250 		/*
13251 		 * Whoops, no SMART selftest log info available
13252 		 */
13253 		rval = -1;
13254 		goto fail;
13255 	} else {
13256 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13257 		    sdinfo->satadrv_addr.cport)));
13258 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13259 		    DDI_DMA_SYNC_FORKERNEL);
13260 		ASSERT(rval == DDI_SUCCESS);
13261 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir,
13262 		    sizeof (struct read_log_ext_directory));
13263 		rval = 0;
13264 	}
13265 
13266 fail:
13267 	/* Free allocated resources */
13268 	sata_free_local_buffer(spx);
13269 	sata_pkt_free(spx);
13270 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13271 
13272 	return (rval);
13273 }
13274 
13275 /*
13276  * Set up error retrieval sata command for NCQ command error data
13277  * recovery.
13278  *
13279  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
13280  * returns SATA_FAILURE otherwise.
13281  */
13282 static int
13283 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
13284 {
13285 #ifndef __lock_lint
13286 	_NOTE(ARGUNUSED(sdinfo))
13287 #endif
13288 
13289 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
13290 	sata_cmd_t *scmd;
13291 	struct buf *bp;
13292 
13293 	/* Operation modes are up to the caller */
13294 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13295 
13296 	/* Synchronous mode, no callback - may be changed by the caller */
13297 	spkt->satapkt_comp = NULL;
13298 	spkt->satapkt_time = sata_default_pkt_time;
13299 
13300 	scmd = &spkt->satapkt_cmd;
13301 	bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t));
13302 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
13303 
13304 	/*
13305 	 * Allocate dma_able buffer error data.
13306 	 * Buffer allocation will take care of buffer alignment and other DMA
13307 	 * attributes.
13308 	 */
13309 	bp = sata_alloc_local_buffer(spx,
13310 	    sizeof (struct sata_ncq_error_recovery_page));
13311 	if (bp == NULL)
13312 		return (SATA_FAILURE);
13313 
13314 	bp_mapin(bp); /* make data buffer accessible */
13315 	scmd->satacmd_bp = bp;
13316 
13317 	/*
13318 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
13319 	 * before accessing it. Handle is in usual place in translate struct.
13320 	 */
13321 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
13322 
13323 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
13324 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
13325 
13326 	return (SATA_SUCCESS);
13327 }
13328 
13329 /*
13330  * sata_xlate_errors() is used to translate (S)ATA error
13331  * information to SCSI information returned in the SCSI
13332  * packet.
13333  */
13334 static void
13335 sata_xlate_errors(sata_pkt_txlate_t *spx)
13336 {
13337 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
13338 	struct scsi_extended_sense *sense;
13339 
13340 	scsipkt->pkt_reason = CMD_INCOMPLETE;
13341 	*scsipkt->pkt_scbp = STATUS_CHECK;
13342 	sense = sata_arq_sense(spx);
13343 
13344 	switch (spx->txlt_sata_pkt->satapkt_reason) {
13345 	case SATA_PKT_PORT_ERROR:
13346 		/*
13347 		 * We have no device data. Assume no data transfered.
13348 		 */
13349 		sense->es_key = KEY_HARDWARE_ERROR;
13350 		break;
13351 
13352 	case SATA_PKT_DEV_ERROR:
13353 		if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
13354 		    SATA_STATUS_ERR) {
13355 			/*
13356 			 * determine dev error reason from error
13357 			 * reg content
13358 			 */
13359 			sata_decode_device_error(spx, sense);
13360 			break;
13361 		}
13362 		/* No extended sense key - no info available */
13363 		break;
13364 
13365 	case SATA_PKT_TIMEOUT:
13366 		scsipkt->pkt_reason = CMD_TIMEOUT;
13367 		scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET;
13368 		/* No extended sense key */
13369 		break;
13370 
13371 	case SATA_PKT_ABORTED:
13372 		scsipkt->pkt_reason = CMD_ABORTED;
13373 		scsipkt->pkt_statistics |= STAT_ABORTED;
13374 		/* No extended sense key */
13375 		break;
13376 
13377 	case SATA_PKT_RESET:
13378 		/*
13379 		 * pkt aborted either by an explicit reset request from
13380 		 * a host, or due to error recovery
13381 		 */
13382 		scsipkt->pkt_reason = CMD_RESET;
13383 		scsipkt->pkt_statistics |= STAT_DEV_RESET;
13384 		break;
13385 
13386 	default:
13387 		scsipkt->pkt_reason = CMD_TRAN_ERR;
13388 		break;
13389 	}
13390 }
13391 
13392 
13393 
13394 
13395 /*
13396  * Log sata message
13397  * dev pathname msg line preceeds the logged message.
13398  */
13399 
13400 static	void
13401 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...)
13402 {
13403 	char pathname[128];
13404 	dev_info_t *dip;
13405 	va_list ap;
13406 
13407 	mutex_enter(&sata_log_mutex);
13408 
13409 	va_start(ap, fmt);
13410 	(void) vsprintf(sata_log_buf, fmt, ap);
13411 	va_end(ap);
13412 
13413 	if (sata_hba_inst != NULL) {
13414 		dip = SATA_DIP(sata_hba_inst);
13415 		(void) ddi_pathname(dip, pathname);
13416 	} else {
13417 		pathname[0] = 0;
13418 	}
13419 	if (level == CE_CONT) {
13420 		if (sata_debug_flags == 0)
13421 			cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf);
13422 		else
13423 			cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf);
13424 	} else {
13425 		if (level != CE_NOTE) {
13426 			cmn_err(level, "%s:\n %s", pathname, sata_log_buf);
13427 		} else if (sata_msg) {
13428 			cmn_err(level, "%s:\n %s", pathname,
13429 			    sata_log_buf);
13430 		}
13431 	}
13432 
13433 	mutex_exit(&sata_log_mutex);
13434 }
13435 
13436 
13437 /* ******** Asynchronous HBA events handling & hotplugging support ******** */
13438 
13439 /*
13440  * Start or terminate the thread, depending on flag arg and current state
13441  */
13442 static void
13443 sata_event_thread_control(int startstop)
13444 {
13445 	static 	int sata_event_thread_terminating = 0;
13446 	static 	int sata_event_thread_starting = 0;
13447 	int i;
13448 
13449 	mutex_enter(&sata_event_mutex);
13450 
13451 	if (startstop == 0 && (sata_event_thread_starting == 1 ||
13452 	    sata_event_thread_terminating == 1)) {
13453 		mutex_exit(&sata_event_mutex);
13454 		return;
13455 	}
13456 	if (startstop == 1 && sata_event_thread_starting == 1) {
13457 		mutex_exit(&sata_event_mutex);
13458 		return;
13459 	}
13460 	if (startstop == 1 && sata_event_thread_terminating == 1) {
13461 		sata_event_thread_starting = 1;
13462 		/* wait til terminate operation completes */
13463 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
13464 		while (sata_event_thread_terminating == 1) {
13465 			if (i-- <= 0) {
13466 				sata_event_thread_starting = 0;
13467 				mutex_exit(&sata_event_mutex);
13468 #ifdef SATA_DEBUG
13469 				cmn_err(CE_WARN, "sata_event_thread_control: "
13470 				    "timeout waiting for thread to terminate");
13471 #endif
13472 				return;
13473 			}
13474 			mutex_exit(&sata_event_mutex);
13475 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
13476 			mutex_enter(&sata_event_mutex);
13477 		}
13478 	}
13479 	if (startstop == 1) {
13480 		if (sata_event_thread == NULL) {
13481 			sata_event_thread = thread_create(NULL, 0,
13482 			    (void (*)())sata_event_daemon,
13483 			    &sata_hba_list, 0, &p0, TS_RUN, minclsyspri);
13484 		}
13485 		sata_event_thread_starting = 0;
13486 		mutex_exit(&sata_event_mutex);
13487 		return;
13488 	}
13489 
13490 	/*
13491 	 * If we got here, thread may need to be terminated
13492 	 */
13493 	if (sata_event_thread != NULL) {
13494 		int i;
13495 		/* Signal event thread to go away */
13496 		sata_event_thread_terminating = 1;
13497 		sata_event_thread_terminate = 1;
13498 		cv_signal(&sata_event_cv);
13499 		/*
13500 		 * Wait til daemon terminates.
13501 		 */
13502 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
13503 		while (sata_event_thread_terminate == 1) {
13504 			mutex_exit(&sata_event_mutex);
13505 			if (i-- <= 0) {
13506 				/* Daemon did not go away !!! */
13507 #ifdef SATA_DEBUG
13508 				cmn_err(CE_WARN, "sata_event_thread_control: "
13509 				    "cannot terminate event daemon thread");
13510 #endif
13511 				mutex_enter(&sata_event_mutex);
13512 				break;
13513 			}
13514 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
13515 			mutex_enter(&sata_event_mutex);
13516 		}
13517 		sata_event_thread_terminating = 0;
13518 	}
13519 	ASSERT(sata_event_thread_terminating == 0);
13520 	ASSERT(sata_event_thread_starting == 0);
13521 	mutex_exit(&sata_event_mutex);
13522 }
13523 
13524 
13525 /*
13526  * SATA HBA event notification function.
13527  * Events reported by SATA HBA drivers per HBA instance relate to a change in
13528  * a port and/or device state or a controller itself.
13529  * Events for different addresses/addr types cannot be combined.
13530  * A warning message is generated for each event type.
13531  * Events are not processed by this function, so only the
13532  * event flag(s)is set for an affected entity and the event thread is
13533  * waken up. Event daemon thread processes all events.
13534  *
13535  * NOTE: Since more than one event may be reported at the same time, one
13536  * cannot determine a sequence of events when opposite event are reported, eg.
13537  * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing
13538  * is taking precedence over reported events, i.e. may cause ignoring some
13539  * events.
13540  */
13541 #define	SATA_EVENT_MAX_MSG_LENGTH	79
13542 
13543 void
13544 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event)
13545 {
13546 	sata_hba_inst_t *sata_hba_inst = NULL;
13547 	sata_address_t *saddr;
13548 	sata_drive_info_t *sdinfo;
13549 	sata_port_stats_t *pstats;
13550 	int cport, pmport;
13551 	char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1];
13552 	char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1];
13553 	char *lcp;
13554 	static char *err_msg_evnt_1 =
13555 	    "sata_hba_event_notify: invalid port event 0x%x ";
13556 	static char *err_msg_evnt_2 =
13557 	    "sata_hba_event_notify: invalid device event 0x%x ";
13558 	int linkevent;
13559 
13560 	/*
13561 	 * There is a possibility that an event will be generated on HBA
13562 	 * that has not completed attachment or is detaching.
13563 	 * HBA driver should prevent this, but just in case it does not,
13564 	 * we need to ignore events for such HBA.
13565 	 */
13566 	mutex_enter(&sata_mutex);
13567 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13568 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13569 		if (SATA_DIP(sata_hba_inst) == dip)
13570 			if (sata_hba_inst->satahba_attached == 1)
13571 				break;
13572 	}
13573 	mutex_exit(&sata_mutex);
13574 	if (sata_hba_inst == NULL)
13575 		/* HBA not attached */
13576 		return;
13577 
13578 	ASSERT(sata_device != NULL);
13579 
13580 	/*
13581 	 * Validate address before - do not proceed with invalid address.
13582 	 */
13583 	saddr = &sata_device->satadev_addr;
13584 	if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst))
13585 		return;
13586 	if (saddr->qual == SATA_ADDR_PMPORT ||
13587 	    saddr->qual == SATA_ADDR_DPMPORT)
13588 		/* Port Multiplier not supported yet */
13589 		return;
13590 
13591 	cport = saddr->cport;
13592 	pmport = saddr->pmport;
13593 
13594 	buf1[0] = buf2[0] = '\0';
13595 
13596 	/*
13597 	 * Events refer to devices, ports and controllers - each has
13598 	 * unique address. Events for different addresses cannot be combined.
13599 	 */
13600 	if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) {
13601 
13602 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13603 
13604 		/* qualify this event(s) */
13605 		if ((event & SATA_EVNT_PORT_EVENTS) == 0) {
13606 			/* Invalid event for the device port */
13607 			(void) sprintf(buf2, err_msg_evnt_1,
13608 			    event & SATA_EVNT_PORT_EVENTS);
13609 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13610 			goto event_info;
13611 		}
13612 		if (saddr->qual == SATA_ADDR_CPORT) {
13613 			/* Controller's device port event */
13614 
13615 			(SATA_CPORT_INFO(sata_hba_inst, cport))->
13616 			    cport_event_flags |=
13617 			    event & SATA_EVNT_PORT_EVENTS;
13618 			pstats =
13619 			    &(SATA_CPORT_INFO(sata_hba_inst, cport))->
13620 			    cport_stats;
13621 		} else {
13622 			/* Port multiplier's device port event */
13623 			(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
13624 			    pmport_event_flags |=
13625 			    event & SATA_EVNT_PORT_EVENTS;
13626 			pstats =
13627 			    &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
13628 			    pmport_stats;
13629 		}
13630 
13631 		/*
13632 		 * Add to statistics and log the message. We have to do it
13633 		 * here rather than in the event daemon, because there may be
13634 		 * multiple events occuring before they are processed.
13635 		 */
13636 		linkevent = event &
13637 		    (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED);
13638 		if (linkevent) {
13639 			if (linkevent == (SATA_EVNT_LINK_LOST |
13640 			    SATA_EVNT_LINK_ESTABLISHED)) {
13641 				/* This is likely event combination */
13642 				(void) strlcat(buf1, "link lost/established, ",
13643 				    SATA_EVENT_MAX_MSG_LENGTH);
13644 
13645 				if (pstats->link_lost < 0xffffffffffffffffULL)
13646 					pstats->link_lost++;
13647 				if (pstats->link_established <
13648 				    0xffffffffffffffffULL)
13649 					pstats->link_established++;
13650 				linkevent = 0;
13651 			} else if (linkevent & SATA_EVNT_LINK_LOST) {
13652 				(void) strlcat(buf1, "link lost, ",
13653 				    SATA_EVENT_MAX_MSG_LENGTH);
13654 
13655 				if (pstats->link_lost < 0xffffffffffffffffULL)
13656 					pstats->link_lost++;
13657 			} else {
13658 				(void) strlcat(buf1, "link established, ",
13659 				    SATA_EVENT_MAX_MSG_LENGTH);
13660 				if (pstats->link_established <
13661 				    0xffffffffffffffffULL)
13662 					pstats->link_established++;
13663 			}
13664 		}
13665 		if (event & SATA_EVNT_DEVICE_ATTACHED) {
13666 			(void) strlcat(buf1, "device attached, ",
13667 			    SATA_EVENT_MAX_MSG_LENGTH);
13668 			if (pstats->device_attached < 0xffffffffffffffffULL)
13669 				pstats->device_attached++;
13670 		}
13671 		if (event & SATA_EVNT_DEVICE_DETACHED) {
13672 			(void) strlcat(buf1, "device detached, ",
13673 			    SATA_EVENT_MAX_MSG_LENGTH);
13674 			if (pstats->device_detached < 0xffffffffffffffffULL)
13675 				pstats->device_detached++;
13676 		}
13677 		if (event & SATA_EVNT_PWR_LEVEL_CHANGED) {
13678 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
13679 			    "port %d power level changed", cport);
13680 			if (pstats->port_pwr_changed < 0xffffffffffffffffULL)
13681 				pstats->port_pwr_changed++;
13682 		}
13683 
13684 		if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) {
13685 			/* There should be no other events for this address */
13686 			(void) sprintf(buf2, err_msg_evnt_1,
13687 			    event & ~SATA_EVNT_PORT_EVENTS);
13688 		}
13689 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13690 
13691 	} else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) {
13692 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13693 
13694 		/* qualify this event */
13695 		if ((event & SATA_EVNT_DEVICE_RESET) == 0) {
13696 			/* Invalid event for a device */
13697 			(void) sprintf(buf2, err_msg_evnt_2,
13698 			    event & SATA_EVNT_DEVICE_RESET);
13699 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13700 			goto event_info;
13701 		}
13702 		/* drive event */
13703 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
13704 		if (sdinfo != NULL) {
13705 			if (event & SATA_EVNT_DEVICE_RESET) {
13706 				(void) strlcat(buf1, "device reset, ",
13707 				    SATA_EVENT_MAX_MSG_LENGTH);
13708 				if (sdinfo->satadrv_stats.drive_reset <
13709 				    0xffffffffffffffffULL)
13710 					sdinfo->satadrv_stats.drive_reset++;
13711 				sdinfo->satadrv_event_flags |=
13712 				    SATA_EVNT_DEVICE_RESET;
13713 			}
13714 		}
13715 		if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) {
13716 			/* Invalid event for a device */
13717 			(void) sprintf(buf2, err_msg_evnt_2,
13718 			    event & ~SATA_EVNT_DRIVE_EVENTS);
13719 		}
13720 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13721 	} else {
13722 		if (saddr->qual != SATA_ADDR_NULL) {
13723 			/* Wrong address qualifier */
13724 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13725 			    "sata_hba_event_notify: invalid address 0x%x",
13726 			    *(uint32_t *)saddr));
13727 			return;
13728 		}
13729 		if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 ||
13730 		    (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) {
13731 			/* Invalid event for the controller */
13732 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13733 			    "sata_hba_event_notify: invalid event 0x%x for "
13734 			    "controller",
13735 			    event & SATA_EVNT_CONTROLLER_EVENTS));
13736 			return;
13737 		}
13738 		buf1[0] = '\0';
13739 		/* This may be a frequent and not interesting event */
13740 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
13741 		    "controller power level changed\n", NULL);
13742 
13743 		mutex_enter(&sata_hba_inst->satahba_mutex);
13744 		if (sata_hba_inst->satahba_stats.ctrl_pwr_change <
13745 		    0xffffffffffffffffULL)
13746 			sata_hba_inst->satahba_stats.ctrl_pwr_change++;
13747 
13748 		sata_hba_inst->satahba_event_flags |=
13749 		    SATA_EVNT_PWR_LEVEL_CHANGED;
13750 		mutex_exit(&sata_hba_inst->satahba_mutex);
13751 	}
13752 	/*
13753 	 * If we got here, there is something to do with this HBA
13754 	 * instance.
13755 	 */
13756 	mutex_enter(&sata_hba_inst->satahba_mutex);
13757 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
13758 	mutex_exit(&sata_hba_inst->satahba_mutex);
13759 	mutex_enter(&sata_mutex);
13760 	sata_event_pending |= SATA_EVNT_MAIN;	/* global event indicator */
13761 	mutex_exit(&sata_mutex);
13762 
13763 	/* Tickle event thread */
13764 	mutex_enter(&sata_event_mutex);
13765 	if (sata_event_thread_active == 0)
13766 		cv_signal(&sata_event_cv);
13767 	mutex_exit(&sata_event_mutex);
13768 
13769 event_info:
13770 	if (buf1[0] != '\0') {
13771 		lcp = strrchr(buf1, ',');
13772 		if (lcp != NULL)
13773 			*lcp = '\0';
13774 	}
13775 	if (saddr->qual == SATA_ADDR_CPORT ||
13776 	    saddr->qual == SATA_ADDR_DCPORT) {
13777 		if (buf1[0] != '\0') {
13778 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
13779 			    cport, buf1);
13780 		}
13781 		if (buf2[0] != '\0') {
13782 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
13783 			    cport, buf2);
13784 		}
13785 	} else if (saddr->qual == SATA_ADDR_PMPORT ||
13786 	    saddr->qual == SATA_ADDR_DPMPORT) {
13787 		if (buf1[0] != '\0') {
13788 			sata_log(sata_hba_inst, CE_NOTE,
13789 			    "port %d pmport %d: %s\n", cport, pmport, buf1);
13790 		}
13791 		if (buf2[0] != '\0') {
13792 			sata_log(sata_hba_inst, CE_NOTE,
13793 			    "port %d pmport %d: %s\n", cport, pmport, buf2);
13794 		}
13795 	}
13796 }
13797 
13798 
13799 /*
13800  * Event processing thread.
13801  * Arg is a pointer to the sata_hba_list pointer.
13802  * It is not really needed, because sata_hba_list is global and static
13803  */
13804 static void
13805 sata_event_daemon(void *arg)
13806 {
13807 #ifndef __lock_lint
13808 	_NOTE(ARGUNUSED(arg))
13809 #endif
13810 	sata_hba_inst_t *sata_hba_inst;
13811 	clock_t lbolt;
13812 
13813 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13814 	    "SATA event daemon started\n", NULL);
13815 loop:
13816 	/*
13817 	 * Process events here. Walk through all registered HBAs
13818 	 */
13819 	mutex_enter(&sata_mutex);
13820 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13821 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13822 		ASSERT(sata_hba_inst != NULL);
13823 		mutex_enter(&sata_hba_inst->satahba_mutex);
13824 		if (sata_hba_inst->satahba_attached != 1 ||
13825 		    (sata_hba_inst->satahba_event_flags &
13826 		    SATA_EVNT_SKIP) != 0) {
13827 			mutex_exit(&sata_hba_inst->satahba_mutex);
13828 			continue;
13829 		}
13830 		if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) {
13831 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP;
13832 			mutex_exit(&sata_hba_inst->satahba_mutex);
13833 			mutex_exit(&sata_mutex);
13834 			/* Got the controller with pending event */
13835 			sata_process_controller_events(sata_hba_inst);
13836 			/*
13837 			 * Since global mutex was released, there is a
13838 			 * possibility that HBA list has changed, so start
13839 			 * over from the top. Just processed controller
13840 			 * will be passed-over because of the SKIP flag.
13841 			 */
13842 			goto loop;
13843 		}
13844 		mutex_exit(&sata_hba_inst->satahba_mutex);
13845 	}
13846 	/* Clear SKIP flag in all controllers */
13847 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13848 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13849 		mutex_enter(&sata_hba_inst->satahba_mutex);
13850 		sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP;
13851 		mutex_exit(&sata_hba_inst->satahba_mutex);
13852 	}
13853 	mutex_exit(&sata_mutex);
13854 
13855 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13856 	    "SATA EVENT DAEMON suspending itself", NULL);
13857 
13858 #ifdef SATA_DEBUG
13859 	if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) {
13860 		sata_log(sata_hba_inst, CE_WARN,
13861 		    "SATA EVENTS PROCESSING DISABLED\n");
13862 		thread_exit(); /* Daemon will not run again */
13863 	}
13864 #endif
13865 	mutex_enter(&sata_event_mutex);
13866 	sata_event_thread_active = 0;
13867 	mutex_exit(&sata_event_mutex);
13868 	/*
13869 	 * Go to sleep/suspend itself and wake up either because new event or
13870 	 * wait timeout. Exit if there is a termination request (driver
13871 	 * unload).
13872 	 */
13873 	do {
13874 		lbolt = ddi_get_lbolt();
13875 		lbolt += drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME);
13876 		mutex_enter(&sata_event_mutex);
13877 		(void) cv_timedwait(&sata_event_cv, &sata_event_mutex, lbolt);
13878 
13879 		if (sata_event_thread_active != 0) {
13880 			mutex_exit(&sata_event_mutex);
13881 			continue;
13882 		}
13883 
13884 		/* Check if it is time to go away */
13885 		if (sata_event_thread_terminate == 1) {
13886 			/*
13887 			 * It is up to the thread setting above flag to make
13888 			 * sure that this thread is not killed prematurely.
13889 			 */
13890 			sata_event_thread_terminate = 0;
13891 			sata_event_thread = NULL;
13892 			mutex_exit(&sata_event_mutex);
13893 			SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13894 			    "SATA_EVENT_DAEMON_TERMINATING", NULL);
13895 			thread_exit();  { _NOTE(NOT_REACHED) }
13896 		}
13897 		mutex_exit(&sata_event_mutex);
13898 	} while (!(sata_event_pending & SATA_EVNT_MAIN));
13899 
13900 	mutex_enter(&sata_event_mutex);
13901 	sata_event_thread_active = 1;
13902 	mutex_exit(&sata_event_mutex);
13903 
13904 	mutex_enter(&sata_mutex);
13905 	sata_event_pending &= ~SATA_EVNT_MAIN;
13906 	mutex_exit(&sata_mutex);
13907 
13908 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13909 	    "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL);
13910 
13911 	goto loop;
13912 }
13913 
13914 /*
13915  * Specific HBA instance event processing.
13916  *
13917  * NOTE: At the moment, device event processing is limited to hard disks
13918  * only.
13919  * cports only are supported - no pmports.
13920  */
13921 static void
13922 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst)
13923 {
13924 	int ncport;
13925 	uint32_t event_flags;
13926 	sata_address_t *saddr;
13927 
13928 	SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst,
13929 	    "Processing controller %d event(s)",
13930 	    ddi_get_instance(SATA_DIP(sata_hba_inst)));
13931 
13932 	mutex_enter(&sata_hba_inst->satahba_mutex);
13933 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN;
13934 	event_flags = sata_hba_inst->satahba_event_flags;
13935 	mutex_exit(&sata_hba_inst->satahba_mutex);
13936 	/*
13937 	 * Process controller power change first
13938 	 * HERE
13939 	 */
13940 	if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED)
13941 		sata_process_cntrl_pwr_level_change(sata_hba_inst);
13942 
13943 	/*
13944 	 * Search through ports/devices to identify affected port/device.
13945 	 * We may have to process events for more than one port/device.
13946 	 */
13947 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
13948 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
13949 		event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))->
13950 		    cport_event_flags;
13951 		/* Check if port was locked by IOCTL processing */
13952 		if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) {
13953 			/*
13954 			 * We ignore port events because port is busy
13955 			 * with AP control processing. Set again
13956 			 * controller and main event flag, so that
13957 			 * events may be processed by the next daemon
13958 			 * run.
13959 			 */
13960 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
13961 			mutex_enter(&sata_hba_inst->satahba_mutex);
13962 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
13963 			mutex_exit(&sata_hba_inst->satahba_mutex);
13964 			mutex_enter(&sata_mutex);
13965 			sata_event_pending |= SATA_EVNT_MAIN;
13966 			mutex_exit(&sata_mutex);
13967 			SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst,
13968 			    "Event processing postponed until "
13969 			    "AP control processing completes",
13970 			    NULL);
13971 			/* Check other ports */
13972 			continue;
13973 		} else {
13974 			/*
13975 			 * Set BSY flag so that AP control would not
13976 			 * interfere with events processing for
13977 			 * this port.
13978 			 */
13979 			(SATA_CPORT_INFO(sata_hba_inst, ncport))->
13980 			    cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY;
13981 		}
13982 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
13983 
13984 		saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr;
13985 
13986 		if ((event_flags &
13987 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
13988 			/*
13989 			 * Got port event.
13990 			 * We need some hierarchy of event processing as they
13991 			 * are affecting each other:
13992 			 * 1. port failed
13993 			 * 2. device detached/attached
13994 			 * 3. link events - link events may trigger device
13995 			 *    detached or device attached events in some
13996 			 *    circumstances.
13997 			 * 4. port power level changed
13998 			 */
13999 			if (event_flags & SATA_EVNT_PORT_FAILED) {
14000 				sata_process_port_failed_event(sata_hba_inst,
14001 				    saddr);
14002 			}
14003 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
14004 				sata_process_device_detached(sata_hba_inst,
14005 				    saddr);
14006 			}
14007 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
14008 				sata_process_device_attached(sata_hba_inst,
14009 				    saddr);
14010 			}
14011 			if (event_flags &
14012 			    (SATA_EVNT_LINK_ESTABLISHED |
14013 			    SATA_EVNT_LINK_LOST)) {
14014 				sata_process_port_link_events(sata_hba_inst,
14015 				    saddr);
14016 			}
14017 			if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) {
14018 				sata_process_port_pwr_change(sata_hba_inst,
14019 				    saddr);
14020 			}
14021 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
14022 				sata_process_target_node_cleanup(
14023 				    sata_hba_inst, saddr);
14024 			}
14025 			if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) {
14026 				sata_process_device_autoonline(
14027 				    sata_hba_inst, saddr);
14028 			}
14029 		}
14030 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) !=
14031 		    SATA_DTYPE_NONE) {
14032 			/* May have device event */
14033 			sata_process_device_reset(sata_hba_inst, saddr);
14034 		}
14035 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14036 		/* Release PORT_BUSY flag */
14037 		(SATA_CPORT_INFO(sata_hba_inst, ncport))->
14038 		    cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
14039 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14040 
14041 	} /* End of loop through the controller SATA ports */
14042 }
14043 
14044 /*
14045  * Process HBA power level change reported by HBA driver.
14046  * Not implemented at this time - event is ignored.
14047  */
14048 static void
14049 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst)
14050 {
14051 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14052 	    "Processing controller power level change", NULL);
14053 
14054 	/* Ignoring it for now */
14055 	mutex_enter(&sata_hba_inst->satahba_mutex);
14056 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
14057 	mutex_exit(&sata_hba_inst->satahba_mutex);
14058 }
14059 
14060 /*
14061  * Process port power level change reported by HBA driver.
14062  * Not implemented at this time - event is ignored.
14063  */
14064 static void
14065 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst,
14066     sata_address_t *saddr)
14067 {
14068 	sata_cport_info_t *cportinfo;
14069 
14070 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14071 	    "Processing port power level change", NULL);
14072 
14073 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14074 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14075 	/* Reset event flag */
14076 	cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
14077 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14078 }
14079 
14080 /*
14081  * Process port failure reported by HBA driver.
14082  * cports support only - no pmports.
14083  */
14084 static void
14085 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst,
14086     sata_address_t *saddr)
14087 {
14088 	sata_cport_info_t *cportinfo;
14089 
14090 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14091 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14092 	/* Reset event flag first */
14093 	cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED;
14094 	/* If the port is in SHUTDOWN or FAILED state, ignore this event. */
14095 	if ((cportinfo->cport_state &
14096 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) {
14097 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14098 		    cport_mutex);
14099 		return;
14100 	}
14101 	/* Fail the port */
14102 	cportinfo->cport_state = SATA_PSTATE_FAILED;
14103 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14104 	sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport);
14105 }
14106 
14107 /*
14108  * Device Reset Event processing.
14109  * The seqeunce is managed by 3 stage flags:
14110  * - reset event reported,
14111  * - reset event being processed,
14112  * - request to clear device reset state.
14113  */
14114 static void
14115 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst,
14116     sata_address_t *saddr)
14117 {
14118 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
14119 	sata_drive_info_t *sdinfo;
14120 	sata_cport_info_t *cportinfo;
14121 	sata_device_t sata_device;
14122 	int rval;
14123 
14124 	/* We only care about host sata cport for now */
14125 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14126 
14127 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14128 
14129 	/* If the port is in SHUTDOWN or FAILED state, ignore reset event. */
14130 	if ((cportinfo->cport_state &
14131 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14132 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14133 		    cport_mutex);
14134 		return;
14135 	}
14136 
14137 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) &
14138 	    SATA_VALID_DEV_TYPE) == 0) {
14139 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14140 		    cport_mutex);
14141 		return;
14142 	}
14143 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14144 	if (sdinfo == NULL) {
14145 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14146 		    cport_mutex);
14147 		return;
14148 	}
14149 
14150 	if ((sdinfo->satadrv_event_flags &
14151 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
14152 		/* Nothing to do */
14153 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14154 		    cport_mutex);
14155 		return;
14156 	}
14157 #ifdef SATA_DEBUG
14158 	if ((sdinfo->satadrv_event_flags &
14159 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
14160 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
14161 		/* Something is weird - new device reset event */
14162 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14163 		    "Overlapping device reset events!", NULL);
14164 	}
14165 #endif
14166 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14167 	    "Processing port %d device reset", saddr->cport);
14168 
14169 	/* Clear event flag */
14170 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
14171 
14172 	/* It seems that we always need to check the port state first */
14173 	sata_device.satadev_rev = SATA_DEVICE_REV;
14174 	sata_device.satadev_addr = *saddr;
14175 	/*
14176 	 * We have to exit mutex, because the HBA probe port function may
14177 	 * block on its own mutex.
14178 	 */
14179 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14180 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14181 	    (SATA_DIP(sata_hba_inst), &sata_device);
14182 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14183 	sata_update_port_info(sata_hba_inst, &sata_device);
14184 	if (rval != SATA_SUCCESS) {
14185 		/* Something went wrong? Fail the port */
14186 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14187 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14188 		    cport_mutex);
14189 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14190 		    "SATA port %d probing failed",
14191 		    saddr->cport));
14192 		return;
14193 	}
14194 	if ((sata_device.satadev_scr.sstatus  &
14195 	    SATA_PORT_DEVLINK_UP_MASK) !=
14196 	    SATA_PORT_DEVLINK_UP ||
14197 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
14198 		/*
14199 		 * No device to process, anymore. Some other event processing
14200 		 * would or have already performed port info cleanup.
14201 		 * To be safe (HBA may need it), request clearing device
14202 		 * reset condition.
14203 		 */
14204 		sdinfo->satadrv_event_flags = 0;
14205 		sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
14206 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14207 		    cport_mutex);
14208 		return;
14209 	}
14210 
14211 	/* Mark device reset processing as active */
14212 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
14213 
14214 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
14215 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14216 
14217 	if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) ==
14218 	    SATA_FAILURE) {
14219 		/*
14220 		 * Restoring drive setting failed.
14221 		 * Probe the port first, to check if the port state has changed
14222 		 */
14223 		sata_device.satadev_rev = SATA_DEVICE_REV;
14224 		sata_device.satadev_addr = *saddr;
14225 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
14226 		/* probe port */
14227 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14228 		    (SATA_DIP(sata_hba_inst), &sata_device);
14229 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14230 		    cport_mutex);
14231 		if (rval == SATA_SUCCESS &&
14232 		    (sata_device.satadev_state &
14233 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
14234 		    (sata_device.satadev_scr.sstatus  &
14235 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
14236 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
14237 			/*
14238 			 * We may retry this a bit later - in-process reset
14239 			 * condition should be already set.
14240 			 */
14241 			if ((cportinfo->cport_dev_type &
14242 			    SATA_VALID_DEV_TYPE) != 0 &&
14243 			    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14244 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14245 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14246 				    saddr->cport)->cport_mutex);
14247 				mutex_enter(&sata_hba_inst->satahba_mutex);
14248 				sata_hba_inst->satahba_event_flags |=
14249 				    SATA_EVNT_MAIN;
14250 				mutex_exit(&sata_hba_inst->satahba_mutex);
14251 				mutex_enter(&sata_mutex);
14252 				sata_event_pending |= SATA_EVNT_MAIN;
14253 				mutex_exit(&sata_mutex);
14254 				return;
14255 			}
14256 		} else {
14257 			/*
14258 			 * No point of retrying - some other event processing
14259 			 * would or already did port info cleanup.
14260 			 * To be safe (HBA may need it),
14261 			 * request clearing device reset condition.
14262 			 */
14263 			sdinfo->satadrv_event_flags = 0;
14264 			sdinfo->satadrv_event_flags |=
14265 			    SATA_EVNT_CLEAR_DEVICE_RESET;
14266 		}
14267 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14268 		    cport_mutex);
14269 		return;
14270 	}
14271 
14272 	/*
14273 	 * Raise the flag indicating that the next sata command could
14274 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
14275 	 * reset is reported.
14276 	 */
14277 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14278 	if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0 &&
14279 	    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14280 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14281 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
14282 		sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
14283 	}
14284 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14285 }
14286 
14287 
14288 /*
14289  * Port Link Events processing.
14290  * Every link established event may involve device reset (due to
14291  * COMRESET signal, equivalent of the hard reset) so arbitrarily
14292  * set device reset event for an attached device (if any).
14293  * If the port is in SHUTDOWN or FAILED state, ignore link events.
14294  *
14295  * The link established event processing varies, depending on the state
14296  * of the target node, HBA hotplugging capabilities, state of the port.
14297  * If the link is not active, the link established event is ignored.
14298  * If HBA cannot detect device attachment and there is no target node,
14299  * the link established event triggers device attach event processing.
14300  * Else, link established event triggers device reset event processing.
14301  *
14302  * The link lost event processing varies, depending on a HBA hotplugging
14303  * capability and the state of the port (link active or not active).
14304  * If the link is active, the lost link event is ignored.
14305  * If HBA cannot detect device removal, the lost link event triggers
14306  * device detached event processing after link lost timeout.
14307  * Else, the event is ignored.
14308  *
14309  * NOTE: Only cports are processed for now, i.e. no port multiplier ports
14310  */
14311 static void
14312 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst,
14313     sata_address_t *saddr)
14314 {
14315 	sata_device_t sata_device;
14316 	sata_cport_info_t *cportinfo;
14317 	sata_drive_info_t *sdinfo;
14318 	uint32_t event_flags;
14319 	int rval;
14320 
14321 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14322 	    "Processing port %d link event(s)", saddr->cport);
14323 
14324 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14325 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14326 	event_flags = cportinfo->cport_event_flags;
14327 
14328 	/* Reset event flags first */
14329 	cportinfo->cport_event_flags &=
14330 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
14331 
14332 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
14333 	if ((cportinfo->cport_state &
14334 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14335 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14336 		    cport_mutex);
14337 		return;
14338 	}
14339 
14340 	/*
14341 	 * For the sanity sake get current port state.
14342 	 * Set device address only. Other sata_device fields should be
14343 	 * set by HBA driver.
14344 	 */
14345 	sata_device.satadev_rev = SATA_DEVICE_REV;
14346 	sata_device.satadev_addr = *saddr;
14347 	/*
14348 	 * We have to exit mutex, because the HBA probe port function may
14349 	 * block on its own mutex.
14350 	 */
14351 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14352 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14353 	    (SATA_DIP(sata_hba_inst), &sata_device);
14354 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14355 	sata_update_port_info(sata_hba_inst, &sata_device);
14356 	if (rval != SATA_SUCCESS) {
14357 		/* Something went wrong? Fail the port */
14358 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14359 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14360 		    cport_mutex);
14361 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14362 		    "SATA port %d probing failed",
14363 		    saddr->cport));
14364 		/*
14365 		 * We may want to release device info structure, but
14366 		 * it is not necessary.
14367 		 */
14368 		return;
14369 	} else {
14370 		/* port probed successfully */
14371 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14372 	}
14373 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
14374 
14375 		if ((sata_device.satadev_scr.sstatus &
14376 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
14377 			/* Ignore event */
14378 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14379 			    "Ignoring port %d link established event - "
14380 			    "link down",
14381 			    saddr->cport);
14382 			goto linklost;
14383 		}
14384 
14385 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14386 		    "Processing port %d link established event",
14387 		    saddr->cport);
14388 
14389 		/*
14390 		 * For the sanity sake check if a device is attached - check
14391 		 * return state of a port probing.
14392 		 */
14393 		if (sata_device.satadev_type != SATA_DTYPE_NONE &&
14394 		    sata_device.satadev_type != SATA_DTYPE_PMULT) {
14395 			/*
14396 			 * HBA port probe indicated that there is a device
14397 			 * attached. Check if the framework had device info
14398 			 * structure attached for this device.
14399 			 */
14400 			if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
14401 				ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) !=
14402 				    NULL);
14403 
14404 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14405 				if ((sdinfo->satadrv_type &
14406 				    SATA_VALID_DEV_TYPE) != 0) {
14407 					/*
14408 					 * Dev info structure is present.
14409 					 * If dev_type is set to known type in
14410 					 * the framework's drive info struct
14411 					 * then the device existed before and
14412 					 * the link was probably lost
14413 					 * momentarily - in such case
14414 					 * we may want to check device
14415 					 * identity.
14416 					 * Identity check is not supported now.
14417 					 *
14418 					 * Link established event
14419 					 * triggers device reset event.
14420 					 */
14421 					(SATA_CPORTINFO_DRV_INFO(cportinfo))->
14422 					    satadrv_event_flags |=
14423 					    SATA_EVNT_DEVICE_RESET;
14424 				}
14425 			} else if (cportinfo->cport_dev_type ==
14426 			    SATA_DTYPE_NONE) {
14427 				/*
14428 				 * We got new device attached! If HBA does not
14429 				 * generate device attached events, trigger it
14430 				 * here.
14431 				 */
14432 				if (!(SATA_FEATURES(sata_hba_inst) &
14433 				    SATA_CTLF_HOTPLUG)) {
14434 					cportinfo->cport_event_flags |=
14435 					    SATA_EVNT_DEVICE_ATTACHED;
14436 				}
14437 			}
14438 			/* Reset link lost timeout */
14439 			cportinfo->cport_link_lost_time = 0;
14440 		}
14441 	}
14442 linklost:
14443 	if (event_flags & SATA_EVNT_LINK_LOST) {
14444 		if ((sata_device.satadev_scr.sstatus &
14445 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
14446 			/* Ignore event */
14447 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14448 			    "Ignoring port %d link lost event - link is up",
14449 			    saddr->cport);
14450 			goto done;
14451 		}
14452 #ifdef SATA_DEBUG
14453 		if (cportinfo->cport_link_lost_time == 0) {
14454 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14455 			    "Processing port %d link lost event",
14456 			    saddr->cport);
14457 		}
14458 #endif
14459 		/*
14460 		 * When HBA cannot generate device attached/detached events,
14461 		 * we need to track link lost time and eventually generate
14462 		 * device detach event.
14463 		 */
14464 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
14465 			/* We are tracking link lost time */
14466 			if (cportinfo->cport_link_lost_time == 0) {
14467 				/* save current time (lbolt value) */
14468 				cportinfo->cport_link_lost_time =
14469 				    ddi_get_lbolt();
14470 				/* just keep link lost event */
14471 				cportinfo->cport_event_flags |=
14472 				    SATA_EVNT_LINK_LOST;
14473 			} else {
14474 				clock_t cur_time = ddi_get_lbolt();
14475 				if ((cur_time -
14476 				    cportinfo->cport_link_lost_time) >=
14477 				    drv_usectohz(
14478 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
14479 					/* trigger device detach event */
14480 					cportinfo->cport_event_flags |=
14481 					    SATA_EVNT_DEVICE_DETACHED;
14482 					cportinfo->cport_link_lost_time = 0;
14483 					SATADBG1(SATA_DBG_EVENTS,
14484 					    sata_hba_inst,
14485 					    "Triggering port %d "
14486 					    "device detached event",
14487 					    saddr->cport);
14488 				} else {
14489 					/* keep link lost event */
14490 					cportinfo->cport_event_flags |=
14491 					    SATA_EVNT_LINK_LOST;
14492 				}
14493 			}
14494 		}
14495 		/*
14496 		 * We could change port state to disable/delay access to
14497 		 * the attached device until the link is recovered.
14498 		 */
14499 	}
14500 done:
14501 	event_flags = cportinfo->cport_event_flags;
14502 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14503 	if (event_flags != 0) {
14504 		mutex_enter(&sata_hba_inst->satahba_mutex);
14505 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14506 		mutex_exit(&sata_hba_inst->satahba_mutex);
14507 		mutex_enter(&sata_mutex);
14508 		sata_event_pending |= SATA_EVNT_MAIN;
14509 		mutex_exit(&sata_mutex);
14510 	}
14511 }
14512 
14513 /*
14514  * Device Detached Event processing.
14515  * Port is probed to find if a device is really gone. If so,
14516  * the device info structure is detached from the SATA port info structure
14517  * and released.
14518  * Port status is updated.
14519  *
14520  * NOTE: Process cports event only, no port multiplier ports.
14521  */
14522 static void
14523 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst,
14524     sata_address_t *saddr)
14525 {
14526 	sata_cport_info_t *cportinfo;
14527 	sata_drive_info_t *sdevinfo;
14528 	sata_device_t sata_device;
14529 	dev_info_t *tdip;
14530 	int rval;
14531 
14532 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14533 	    "Processing port %d device detached", saddr->cport);
14534 
14535 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14536 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14537 	/* Clear event flag */
14538 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
14539 
14540 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
14541 	if ((cportinfo->cport_state &
14542 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14543 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14544 		    cport_mutex);
14545 		return;
14546 	}
14547 	/* For sanity, re-probe the port */
14548 	sata_device.satadev_rev = SATA_DEVICE_REV;
14549 	sata_device.satadev_addr = *saddr;
14550 
14551 	/*
14552 	 * We have to exit mutex, because the HBA probe port function may
14553 	 * block on its own mutex.
14554 	 */
14555 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14556 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14557 	    (SATA_DIP(sata_hba_inst), &sata_device);
14558 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14559 	sata_update_port_info(sata_hba_inst, &sata_device);
14560 	if (rval != SATA_SUCCESS) {
14561 		/* Something went wrong? Fail the port */
14562 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14563 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14564 		    cport_mutex);
14565 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14566 		    "SATA port %d probing failed",
14567 		    saddr->cport));
14568 		/*
14569 		 * We may want to release device info structure, but
14570 		 * it is not necessary.
14571 		 */
14572 		return;
14573 	} else {
14574 		/* port probed successfully */
14575 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14576 	}
14577 	/*
14578 	 * Check if a device is still attached. For sanity, check also
14579 	 * link status - if no link, there is no device.
14580 	 */
14581 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
14582 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
14583 	    SATA_DTYPE_NONE) {
14584 		/*
14585 		 * Device is still attached - ignore detach event.
14586 		 */
14587 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14588 		    cport_mutex);
14589 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14590 		    "Ignoring detach - device still attached to port %d",
14591 		    sata_device.satadev_addr.cport);
14592 		return;
14593 	}
14594 	/*
14595 	 * We need to detach and release device info structure here
14596 	 */
14597 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14598 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14599 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14600 		(void) kmem_free((void *)sdevinfo,
14601 		    sizeof (sata_drive_info_t));
14602 	}
14603 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14604 	/*
14605 	 * Device cannot be reached anymore, even if the target node may be
14606 	 * still present.
14607 	 */
14608 
14609 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14610 	sata_log(sata_hba_inst, CE_WARN, "SATA device detached at port %d",
14611 	    sata_device.satadev_addr.cport);
14612 
14613 	/*
14614 	 * Try to offline a device and remove target node if it still exists
14615 	 */
14616 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
14617 	if (tdip != NULL) {
14618 		/*
14619 		 * Target node exists.  Unconfigure device then remove
14620 		 * the target node (one ndi operation).
14621 		 */
14622 		if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
14623 			/*
14624 			 * PROBLEM - no device, but target node remained
14625 			 * This happens when the file was open or node was
14626 			 * waiting for resources.
14627 			 */
14628 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14629 			    "sata_process_device_detached: "
14630 			    "Failed to remove target node for "
14631 			    "detached SATA device."));
14632 			/*
14633 			 * Set target node state to DEVI_DEVICE_REMOVED.
14634 			 * But re-check first that the node still exists.
14635 			 */
14636 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
14637 			    saddr->cport);
14638 			if (tdip != NULL) {
14639 				sata_set_device_removed(tdip);
14640 				/*
14641 				 * Instruct event daemon to retry the
14642 				 * cleanup later.
14643 				 */
14644 				sata_set_target_node_cleanup(sata_hba_inst,
14645 				    &sata_device.satadev_addr);
14646 			}
14647 		}
14648 	}
14649 	/*
14650 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14651 	 * with the hint: SE_HINT_REMOVE
14652 	 */
14653 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
14654 }
14655 
14656 
14657 /*
14658  * Device Attached Event processing.
14659  * Port state is checked to verify that a device is really attached. If so,
14660  * the device info structure is created and attached to the SATA port info
14661  * structure.
14662  *
14663  * If attached device cannot be identified or set-up, the retry for the
14664  * attach processing is set-up. Subsequent daemon run would try again to
14665  * identify the device, until the time limit is reached
14666  * (SATA_DEV_IDENTIFY_TIMEOUT).
14667  *
14668  * This function cannot be called in interrupt context (it may sleep).
14669  *
14670  * NOTE: Process cports event only, no port multiplier ports.
14671  */
14672 static void
14673 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst,
14674     sata_address_t *saddr)
14675 {
14676 	sata_cport_info_t *cportinfo;
14677 	sata_drive_info_t *sdevinfo;
14678 	sata_device_t sata_device;
14679 	dev_info_t *tdip;
14680 	uint32_t event_flags;
14681 	int rval;
14682 
14683 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14684 	    "Processing port %d device attached", saddr->cport);
14685 
14686 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14687 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14688 
14689 	/* Clear attach event flag first */
14690 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
14691 
14692 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
14693 	if ((cportinfo->cport_state &
14694 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14695 		cportinfo->cport_dev_attach_time = 0;
14696 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14697 		    cport_mutex);
14698 		return;
14699 	}
14700 
14701 	/*
14702 	 * If the sata_drive_info structure is found attached to the port info,
14703 	 * despite the fact the device was removed and now it is re-attached,
14704 	 * the old drive info structure was not removed.
14705 	 * Arbitrarily release device info structure.
14706 	 */
14707 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14708 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14709 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14710 		(void) kmem_free((void *)sdevinfo,
14711 		    sizeof (sata_drive_info_t));
14712 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14713 		    "Arbitrarily detaching old device info.", NULL);
14714 	}
14715 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14716 
14717 	/* For sanity, re-probe the port */
14718 	sata_device.satadev_rev = SATA_DEVICE_REV;
14719 	sata_device.satadev_addr = *saddr;
14720 
14721 	/*
14722 	 * We have to exit mutex, because the HBA probe port function may
14723 	 * block on its own mutex.
14724 	 */
14725 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14726 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14727 	    (SATA_DIP(sata_hba_inst), &sata_device);
14728 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14729 	sata_update_port_info(sata_hba_inst, &sata_device);
14730 	if (rval != SATA_SUCCESS) {
14731 		/* Something went wrong? Fail the port */
14732 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14733 		cportinfo->cport_dev_attach_time = 0;
14734 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14735 		    cport_mutex);
14736 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14737 		    "SATA port %d probing failed",
14738 		    saddr->cport));
14739 		return;
14740 	} else {
14741 		/* port probed successfully */
14742 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14743 	}
14744 	/*
14745 	 * Check if a device is still attached. For sanity, check also
14746 	 * link status - if no link, there is no device.
14747 	 */
14748 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
14749 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
14750 	    SATA_DTYPE_NONE) {
14751 		/*
14752 		 * No device - ignore attach event.
14753 		 */
14754 		cportinfo->cport_dev_attach_time = 0;
14755 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14756 		    cport_mutex);
14757 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14758 		    "Ignoring attach - no device connected to port %d",
14759 		    sata_device.satadev_addr.cport);
14760 		return;
14761 	}
14762 
14763 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14764 	/*
14765 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14766 	 * with the hint: SE_HINT_INSERT
14767 	 */
14768 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
14769 
14770 	/*
14771 	 * Port reprobing will take care of the creation of the device
14772 	 * info structure and determination of the device type.
14773 	 */
14774 	sata_device.satadev_addr = *saddr;
14775 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
14776 	    SATA_DEV_IDENTIFY_NORETRY);
14777 
14778 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14779 	    cport_mutex);
14780 	if ((cportinfo->cport_state & SATA_STATE_READY) &&
14781 	    (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) {
14782 		/* Some device is attached to the port */
14783 		if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) {
14784 			/*
14785 			 * A device was not successfully attached.
14786 			 * Track retry time for device identification.
14787 			 */
14788 			if (cportinfo->cport_dev_attach_time != 0) {
14789 				clock_t cur_time = ddi_get_lbolt();
14790 				/*
14791 				 * If the retry time limit was not exceeded,
14792 				 * reinstate attach event.
14793 				 */
14794 				if ((cur_time -
14795 				    cportinfo->cport_dev_attach_time) <
14796 				    drv_usectohz(
14797 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
14798 					/* OK, restore attach event */
14799 					cportinfo->cport_event_flags |=
14800 					    SATA_EVNT_DEVICE_ATTACHED;
14801 				} else {
14802 					/* Timeout - cannot identify device */
14803 					cportinfo->cport_dev_attach_time = 0;
14804 					sata_log(sata_hba_inst,
14805 					    CE_WARN,
14806 					    "Cannot identify SATA device "
14807 					    "at port %d - device will not be "
14808 					    "attached.",
14809 					    saddr->cport);
14810 				}
14811 			} else {
14812 				/*
14813 				 * Start tracking time for device
14814 				 * identification.
14815 				 * Save current time (lbolt value).
14816 				 */
14817 				cportinfo->cport_dev_attach_time =
14818 				    ddi_get_lbolt();
14819 				/* Restore attach event */
14820 				cportinfo->cport_event_flags |=
14821 				    SATA_EVNT_DEVICE_ATTACHED;
14822 			}
14823 		} else {
14824 			/*
14825 			 * If device was successfully attached, the subsequent
14826 			 * action depends on a state of the
14827 			 * sata_auto_online variable. If it is set to zero.
14828 			 * an explicit 'configure' command will be needed to
14829 			 * configure it. If its value is non-zero, we will
14830 			 * attempt to online (configure) the device.
14831 			 * First, log the message indicating that a device
14832 			 * was attached.
14833 			 */
14834 			cportinfo->cport_dev_attach_time = 0;
14835 			sata_log(sata_hba_inst, CE_WARN,
14836 			    "SATA device detected at port %d", saddr->cport);
14837 
14838 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14839 				sata_drive_info_t new_sdinfo;
14840 
14841 				/* Log device info data */
14842 				new_sdinfo = *(SATA_CPORTINFO_DRV_INFO(
14843 				    cportinfo));
14844 				sata_show_drive_info(sata_hba_inst,
14845 				    &new_sdinfo);
14846 			}
14847 
14848 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14849 			    saddr->cport)->cport_mutex);
14850 
14851 			/*
14852 			 * Make sure that there is no target node for that
14853 			 * device. If so, release it. It should not happen,
14854 			 * unless we had problem removing the node when
14855 			 * device was detached.
14856 			 */
14857 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
14858 			    saddr->cport);
14859 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14860 			    saddr->cport)->cport_mutex);
14861 			if (tdip != NULL) {
14862 
14863 #ifdef SATA_DEBUG
14864 				if ((cportinfo->cport_event_flags &
14865 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
14866 					sata_log(sata_hba_inst, CE_WARN,
14867 					    "sata_process_device_attached: "
14868 					    "old device target node exists!");
14869 #endif
14870 				/*
14871 				 * target node exists - try to unconfigure
14872 				 * device and remove the node.
14873 				 */
14874 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14875 				    saddr->cport)->cport_mutex);
14876 				rval = ndi_devi_offline(tdip,
14877 				    NDI_DEVI_REMOVE);
14878 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14879 				    saddr->cport)->cport_mutex);
14880 
14881 				if (rval == NDI_SUCCESS) {
14882 					cportinfo->cport_event_flags &=
14883 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
14884 					cportinfo->cport_tgtnode_clean = B_TRUE;
14885 				} else {
14886 					/*
14887 					 * PROBLEM - the target node remained
14888 					 * and it belongs to a previously
14889 					 * attached device.
14890 					 * This happens when the file was open
14891 					 * or the node was waiting for
14892 					 * resources at the time the
14893 					 * associated device was removed.
14894 					 * Instruct event daemon to retry the
14895 					 * cleanup later.
14896 					 */
14897 					sata_log(sata_hba_inst,
14898 					    CE_WARN,
14899 					    "Application(s) accessing "
14900 					    "previously attached SATA "
14901 					    "device have to release "
14902 					    "it before newly inserted "
14903 					    "device can be made accessible.",
14904 					    saddr->cport);
14905 					cportinfo->cport_event_flags |=
14906 					    SATA_EVNT_TARGET_NODE_CLEANUP;
14907 					cportinfo->cport_tgtnode_clean =
14908 					    B_FALSE;
14909 				}
14910 			}
14911 			if (sata_auto_online != 0) {
14912 				cportinfo->cport_event_flags |=
14913 				    SATA_EVNT_AUTOONLINE_DEVICE;
14914 			}
14915 
14916 		}
14917 	} else {
14918 		cportinfo->cport_dev_attach_time = 0;
14919 	}
14920 
14921 	event_flags = cportinfo->cport_event_flags;
14922 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14923 	if (event_flags != 0) {
14924 		mutex_enter(&sata_hba_inst->satahba_mutex);
14925 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14926 		mutex_exit(&sata_hba_inst->satahba_mutex);
14927 		mutex_enter(&sata_mutex);
14928 		sata_event_pending |= SATA_EVNT_MAIN;
14929 		mutex_exit(&sata_mutex);
14930 	}
14931 }
14932 
14933 
14934 /*
14935  * Device Target Node Cleanup Event processing.
14936  * If the target node associated with a sata port device is in
14937  * DEVI_DEVICE_REMOVED state, an attempt is made to remove it.
14938  * If the target node cannot be removed, the event flag is left intact,
14939  * so that event daemon may re-run this function later.
14940  *
14941  * This function cannot be called in interrupt context (it may sleep).
14942  *
14943  * NOTE: Processes cport events only, not port multiplier ports.
14944  */
14945 static void
14946 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
14947     sata_address_t *saddr)
14948 {
14949 	sata_cport_info_t *cportinfo;
14950 	dev_info_t *tdip;
14951 
14952 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14953 	    "Processing port %d device target node cleanup", saddr->cport);
14954 
14955 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14956 
14957 	/*
14958 	 * Check if there is target node for that device and it is in the
14959 	 * DEVI_DEVICE_REMOVED state. If so, release it.
14960 	 */
14961 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
14962 	if (tdip != NULL) {
14963 		/*
14964 		 * target node exists - check if it is target node of
14965 		 * a removed device.
14966 		 */
14967 		if (sata_check_device_removed(tdip) == B_TRUE) {
14968 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14969 			    "sata_process_target_node_cleanup: "
14970 			    "old device target node exists!", NULL);
14971 			/*
14972 			 * Unconfigure and remove the target node
14973 			 */
14974 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) ==
14975 			    NDI_SUCCESS) {
14976 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14977 				    saddr->cport)->cport_mutex);
14978 				cportinfo->cport_event_flags &=
14979 				    ~SATA_EVNT_TARGET_NODE_CLEANUP;
14980 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14981 				    saddr->cport)->cport_mutex);
14982 				return;
14983 			}
14984 			/*
14985 			 * Event daemon will retry the cleanup later.
14986 			 */
14987 			mutex_enter(&sata_hba_inst->satahba_mutex);
14988 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14989 			mutex_exit(&sata_hba_inst->satahba_mutex);
14990 			mutex_enter(&sata_mutex);
14991 			sata_event_pending |= SATA_EVNT_MAIN;
14992 			mutex_exit(&sata_mutex);
14993 		}
14994 	} else {
14995 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14996 		    saddr->cport)->cport_mutex);
14997 		cportinfo->cport_event_flags &=
14998 		    ~SATA_EVNT_TARGET_NODE_CLEANUP;
14999 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15000 		    saddr->cport)->cport_mutex);
15001 	}
15002 }
15003 
15004 /*
15005  * Device AutoOnline Event processing.
15006  * If attached device is to be onlined, an attempt is made to online this
15007  * device, but only if there is no lingering (old) target node present.
15008  * If the device cannot be onlined, the event flag is left intact,
15009  * so that event daemon may re-run this function later.
15010  *
15011  * This function cannot be called in interrupt context (it may sleep).
15012  *
15013  * NOTE: Processes cport events only, not port multiplier ports.
15014  */
15015 static void
15016 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst,
15017     sata_address_t *saddr)
15018 {
15019 	sata_cport_info_t *cportinfo;
15020 	sata_drive_info_t *sdinfo;
15021 	sata_device_t sata_device;
15022 	dev_info_t *tdip;
15023 
15024 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15025 	    "Processing port %d attached device auto-onlining", saddr->cport);
15026 
15027 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15028 
15029 	/*
15030 	 * Check if device is present and recognized. If not, reset event.
15031 	 */
15032 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15033 	if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
15034 		/* Nothing to online */
15035 		cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
15036 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15037 		    saddr->cport)->cport_mutex);
15038 		return;
15039 	}
15040 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15041 
15042 	/*
15043 	 * Check if there is target node for this device and if it is in the
15044 	 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep
15045 	 * the event for later processing.
15046 	 */
15047 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
15048 	if (tdip != NULL) {
15049 		/*
15050 		 * target node exists - check if it is target node of
15051 		 * a removed device.
15052 		 */
15053 		if (sata_check_device_removed(tdip) == B_TRUE) {
15054 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15055 			    "sata_process_device_autoonline: "
15056 			    "old device target node exists!", NULL);
15057 			/*
15058 			 * Event daemon will retry device onlining later.
15059 			 */
15060 			mutex_enter(&sata_hba_inst->satahba_mutex);
15061 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15062 			mutex_exit(&sata_hba_inst->satahba_mutex);
15063 			mutex_enter(&sata_mutex);
15064 			sata_event_pending |= SATA_EVNT_MAIN;
15065 			mutex_exit(&sata_mutex);
15066 			return;
15067 		}
15068 		/*
15069 		 * If the target node is not in the 'removed" state, assume
15070 		 * that it belongs to this device. There is nothing more to do,
15071 		 * but reset the event.
15072 		 */
15073 	} else {
15074 
15075 		/*
15076 		 * Try to online the device
15077 		 * If there is any reset-related event, remove it. We are
15078 		 * configuring the device and no state restoring is needed.
15079 		 */
15080 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15081 		    saddr->cport)->cport_mutex);
15082 		sata_device.satadev_addr = *saddr;
15083 		if (saddr->qual == SATA_ADDR_CPORT)
15084 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
15085 		else
15086 			sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
15087 		sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
15088 		if (sdinfo != NULL) {
15089 			if (sdinfo->satadrv_event_flags &
15090 			    (SATA_EVNT_DEVICE_RESET |
15091 			    SATA_EVNT_INPROC_DEVICE_RESET))
15092 				sdinfo->satadrv_event_flags = 0;
15093 			sdinfo->satadrv_event_flags |=
15094 			    SATA_EVNT_CLEAR_DEVICE_RESET;
15095 
15096 			/* Need to create a new target node. */
15097 			cportinfo->cport_tgtnode_clean = B_TRUE;
15098 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15099 			    saddr->cport)->cport_mutex);
15100 			tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
15101 			    sata_hba_inst, &sata_device.satadev_addr);
15102 			if (tdip == NULL) {
15103 				/*
15104 				 * Configure (onlining) failed.
15105 				 * We will NOT retry
15106 				 */
15107 				SATA_LOG_D((sata_hba_inst, CE_WARN,
15108 				    "sata_process_device_autoonline: "
15109 				    "configuring SATA device at port %d failed",
15110 				    saddr->cport));
15111 			}
15112 		} else {
15113 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15114 			    saddr->cport)->cport_mutex);
15115 		}
15116 
15117 	}
15118 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15119 	cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
15120 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15121 	    saddr->cport)->cport_mutex);
15122 }
15123 
15124 
15125 static void
15126 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr,
15127     int hint)
15128 {
15129 	char ap[MAXPATHLEN];
15130 	nvlist_t *ev_attr_list = NULL;
15131 	int err;
15132 
15133 	/* Allocate and build sysevent attribute list */
15134 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP);
15135 	if (err != 0) {
15136 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15137 		    "sata_gen_sysevent: "
15138 		    "cannot allocate memory for sysevent attributes\n"));
15139 		return;
15140 	}
15141 	/* Add hint attribute */
15142 	err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint));
15143 	if (err != 0) {
15144 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15145 		    "sata_gen_sysevent: "
15146 		    "failed to add DR_HINT attr for sysevent"));
15147 		nvlist_free(ev_attr_list);
15148 		return;
15149 	}
15150 	/*
15151 	 * Add AP attribute.
15152 	 * Get controller pathname and convert it into AP pathname by adding
15153 	 * a target number.
15154 	 */
15155 	(void) snprintf(ap, MAXPATHLEN, "/devices");
15156 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap));
15157 	(void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d",
15158 	    SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual));
15159 
15160 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap);
15161 	if (err != 0) {
15162 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15163 		    "sata_gen_sysevent: "
15164 		    "failed to add DR_AP_ID attr for sysevent"));
15165 		nvlist_free(ev_attr_list);
15166 		return;
15167 	}
15168 
15169 	/* Generate/log sysevent */
15170 	err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR,
15171 	    ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP);
15172 	if (err != DDI_SUCCESS) {
15173 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15174 		    "sata_gen_sysevent: "
15175 		    "cannot log sysevent, err code %x\n", err));
15176 	}
15177 
15178 	nvlist_free(ev_attr_list);
15179 }
15180 
15181 
15182 
15183 
15184 /*
15185  * Set DEVI_DEVICE_REMOVED state in the SATA device target node.
15186  */
15187 static void
15188 sata_set_device_removed(dev_info_t *tdip)
15189 {
15190 	int circ;
15191 
15192 	ASSERT(tdip != NULL);
15193 
15194 	ndi_devi_enter(tdip, &circ);
15195 	mutex_enter(&DEVI(tdip)->devi_lock);
15196 	DEVI_SET_DEVICE_REMOVED(tdip);
15197 	mutex_exit(&DEVI(tdip)->devi_lock);
15198 	ndi_devi_exit(tdip, circ);
15199 }
15200 
15201 
15202 /*
15203  * Set internal event instructing event daemon to try
15204  * to perform the target node cleanup.
15205  */
15206 static void
15207 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
15208     sata_address_t *saddr)
15209 {
15210 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15211 	SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |=
15212 	    SATA_EVNT_TARGET_NODE_CLEANUP;
15213 	SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_tgtnode_clean =
15214 	    B_FALSE;
15215 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15216 	mutex_enter(&sata_hba_inst->satahba_mutex);
15217 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15218 	mutex_exit(&sata_hba_inst->satahba_mutex);
15219 	mutex_enter(&sata_mutex);
15220 	sata_event_pending |= SATA_EVNT_MAIN;
15221 	mutex_exit(&sata_mutex);
15222 }
15223 
15224 
15225 /*
15226  * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state,
15227  * i.e. check if the target node state indicates that it belongs to a removed
15228  * device.
15229  *
15230  * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state,
15231  * B_FALSE otherwise.
15232  *
15233  * NOTE: No port multiplier support.
15234  */
15235 static boolean_t
15236 sata_check_device_removed(dev_info_t *tdip)
15237 {
15238 	ASSERT(tdip != NULL);
15239 
15240 	if (DEVI_IS_DEVICE_REMOVED(tdip))
15241 		return (B_TRUE);
15242 	else
15243 		return (B_FALSE);
15244 }
15245