xref: /titanic_41/usr/src/uts/common/io/sata/impl/sata.c (revision af3025fd013117424096c94bd9e116ecc538cc6b)
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 2007 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 
57 /*
58  * Flags enabling selected SATA HBA framework functionality
59  */
60 #define	SATA_ENABLE_QUEUING		1
61 #define	SATA_ENABLE_NCQ			2
62 #define	SATA_ENABLE_PROCESS_EVENTS	4
63 int sata_func_enable =
64 	SATA_ENABLE_PROCESS_EVENTS | SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ;
65 
66 #ifdef SATA_DEBUG
67 
68 #define	SATA_LOG_D(args)	sata_log args
69 uint64_t mbuf_count = 0;
70 uint64_t mbuffail_count = 0;
71 
72 sata_atapi_cmd_t sata_atapi_trace[64];
73 uint32_t sata_atapi_trace_index = 0;
74 int sata_atapi_trace_save = 1;
75 static	void sata_save_atapi_trace(sata_pkt_txlate_t *, int);
76 #define	SATAATAPITRACE(spx, count)	if (sata_atapi_trace_save) \
77     sata_save_atapi_trace(spx, count);
78 
79 #else
80 #define	SATA_LOG_D(arg)
81 #define	SATAATAPITRACE(spx, count)
82 #endif
83 
84 #if 0
85 static void
86 sata_test_atapi_packet_command(sata_hba_inst_t *, int);
87 #endif
88 #define	LEGACY_HWID_LEN	64	/* Model (40) + Serial (20) + pad */
89 
90 
91 /*
92  * SATA cb_ops functions
93  */
94 static 	int sata_hba_open(dev_t *, int, int, cred_t *);
95 static 	int sata_hba_close(dev_t, int, int, cred_t *);
96 static 	int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *,	int *);
97 
98 /*
99  * SCSA required entry points
100  */
101 static	int sata_scsi_tgt_init(dev_info_t *, dev_info_t *,
102     scsi_hba_tran_t *, struct scsi_device *);
103 static	int sata_scsi_tgt_probe(struct scsi_device *,
104     int (*callback)(void));
105 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *,
106     scsi_hba_tran_t *, struct scsi_device *);
107 static 	int sata_scsi_start(struct scsi_address *, struct scsi_pkt *);
108 static 	int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *);
109 static 	int sata_scsi_reset(struct scsi_address *, int);
110 static 	int sata_scsi_getcap(struct scsi_address *, char *, int);
111 static 	int sata_scsi_setcap(struct scsi_address *, char *, int, int);
112 static 	struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *,
113     struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t),
114     caddr_t);
115 static 	void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *);
116 static 	void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *);
117 static 	void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *);
118 
119 /*
120  * SATA HBA interface functions are defined in sata_hba.h header file
121  */
122 
123 /* Event processing functions */
124 static	void sata_event_daemon(void *);
125 static	void sata_event_thread_control(int);
126 static	void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst);
127 static	void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *);
128 static	void sata_process_port_failed_event(sata_hba_inst_t *,
129     sata_address_t *);
130 static	void sata_process_port_link_events(sata_hba_inst_t *,
131     sata_address_t *);
132 static	void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *);
133 static	void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *);
134 static	void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *);
135 static	void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *);
136 static	void sata_process_target_node_cleanup(sata_hba_inst_t *,
137     sata_address_t *);
138 
139 
140 /*
141  * Local translation functions
142  */
143 static	int sata_txlt_inquiry(sata_pkt_txlate_t *);
144 static	int sata_txlt_test_unit_ready(sata_pkt_txlate_t *);
145 static	int sata_txlt_start_stop_unit(sata_pkt_txlate_t *);
146 static	int sata_txlt_read_capacity(sata_pkt_txlate_t *);
147 static	int sata_txlt_request_sense(sata_pkt_txlate_t *);
148 static 	int sata_txlt_read(sata_pkt_txlate_t *);
149 static 	int sata_txlt_write(sata_pkt_txlate_t *);
150 static 	int sata_txlt_log_sense(sata_pkt_txlate_t *);
151 static 	int sata_txlt_log_select(sata_pkt_txlate_t *);
152 static 	int sata_txlt_mode_sense(sata_pkt_txlate_t *);
153 static 	int sata_txlt_mode_select(sata_pkt_txlate_t *);
154 static 	int sata_txlt_synchronize_cache(sata_pkt_txlate_t *);
155 static 	int sata_txlt_write_buffer(sata_pkt_txlate_t *);
156 static 	int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *);
157 
158 static 	int sata_hba_start(sata_pkt_txlate_t *, int *);
159 static	int sata_txlt_invalid_command(sata_pkt_txlate_t *);
160 static	int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *);
161 static 	void sata_txlt_rw_completion(sata_pkt_t *);
162 static 	void sata_txlt_nodata_cmd_completion(sata_pkt_t *);
163 static 	void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *);
164 static 	int sata_emul_rw_completion(sata_pkt_txlate_t *);
165 static 	struct scsi_extended_sense *sata_immediate_error_response(
166     sata_pkt_txlate_t *, int);
167 static	struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *);
168 
169 static 	int sata_txlt_atapi(sata_pkt_txlate_t *);
170 static 	void sata_txlt_atapi_completion(sata_pkt_t *);
171 
172 /*
173  * Local functions for ioctl
174  */
175 static	int32_t sata_get_port_num(sata_hba_inst_t *,  struct devctl_iocdata *);
176 static	void sata_cfgadm_state(sata_hba_inst_t *, int32_t,
177     devctl_ap_state_t *);
178 static	dev_info_t *sata_get_target_dip(dev_info_t *, int32_t);
179 static	dev_info_t *sata_devt_to_devinfo(dev_t);
180 
181 /*
182  * Local functions
183  */
184 static 	void sata_remove_hba_instance(dev_info_t *);
185 static 	int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *);
186 static 	void sata_probe_ports(sata_hba_inst_t *);
187 static 	int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int);
188 static 	int sata_add_device(dev_info_t *, sata_hba_inst_t *, int cport,
189     int pmport);
190 static 	dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *,
191     sata_address_t *);
192 static 	int sata_validate_scsi_address(sata_hba_inst_t *,
193     struct scsi_address *, sata_device_t *);
194 static 	int sata_validate_sata_address(sata_hba_inst_t *, int, int, int);
195 static	sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t));
196 static	void sata_pkt_free(sata_pkt_txlate_t *);
197 static	int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t),
198     caddr_t, ddi_dma_attr_t *);
199 static	int sata_probe_device(sata_hba_inst_t *, sata_device_t *);
200 static	sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *,
201     sata_device_t *);
202 static 	int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *);
203 static	struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int);
204 static 	void sata_free_local_buffer(sata_pkt_txlate_t *);
205 static 	uint64_t sata_check_capacity(sata_drive_info_t *);
206 void 	sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *,
207     ddi_dma_attr_t *);
208 static 	int sata_fetch_device_identify_data(sata_hba_inst_t *,
209     sata_drive_info_t *);
210 static	void sata_update_port_info(sata_hba_inst_t *, sata_device_t *);
211 static	void sata_update_port_scr(sata_port_scr_t *, sata_device_t *);
212 static	int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *);
213 static	int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int);
214 static	int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int);
215 static	int sata_set_drive_features(sata_hba_inst_t *,
216     sata_drive_info_t *, int flag);
217 static	void sata_init_write_cache_mode(sata_drive_info_t *sdinfo);
218 static	int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *);
219 static	void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *,
220     uint8_t *);
221 static	int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *,
222     struct scsi_inquiry *);
223 static	int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *);
224 static	int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *);
225 static	int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *);
226 static	int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *);
227 static	int sata_mode_select_page_8(sata_pkt_txlate_t *,
228     struct mode_cache_scsi3 *, int, int *, int *, int *);
229 static	int sata_mode_select_page_1c(sata_pkt_txlate_t *,
230     struct mode_info_excpt_page *, int, int *, int *, int *);
231 static	int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *);
232 static	int sata_mode_select_page_30(sata_pkt_txlate_t *,
233     struct mode_acoustic_management *, int, int *, int *, int *);
234 
235 static	int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *);
236 static	int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *,
237     sata_hba_inst_t *);
238 static	int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *,
239     sata_hba_inst_t *);
240 static	int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *,
241     sata_hba_inst_t *);
242 static	void sata_save_drive_settings(sata_drive_info_t *);
243 static	void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *);
244 static	void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...);
245 static	int sata_fetch_smart_return_status(sata_hba_inst_t *,
246     sata_drive_info_t *);
247 static	int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *,
248     struct smart_data *);
249 static	int sata_smart_selftest_log(sata_hba_inst_t *,
250     sata_drive_info_t *,
251     struct smart_selftest_log *);
252 static	int sata_ext_smart_selftest_read_log(sata_hba_inst_t *,
253     sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t);
254 static	int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *,
255     uint8_t *, uint8_t, uint8_t);
256 static	int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *,
257     struct read_log_ext_directory *);
258 static	void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int);
259 static	void sata_xlate_errors(sata_pkt_txlate_t *);
260 static	void sata_decode_device_error(sata_pkt_txlate_t *,
261     struct scsi_extended_sense *);
262 static	void sata_set_device_removed(dev_info_t *);
263 static	boolean_t sata_check_device_removed(dev_info_t *);
264 static	void sata_set_target_node_cleanup(sata_hba_inst_t *, int cport);
265 static	int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *,
266     sata_drive_info_t *);
267 static	void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *);
268 static	void sata_fixed_sense_data_preset(struct scsi_extended_sense *);
269 static  void sata_target_devid_register(dev_info_t *, sata_drive_info_t *);
270 static  int sata_check_modser(char *, int);
271 
272 
273 
274 /*
275  * SATA Framework will ignore SATA HBA driver cb_ops structure and
276  * register following one with SCSA framework.
277  * Open & close are provided, so scsi framework will not use its own
278  */
279 static struct cb_ops sata_cb_ops = {
280 	sata_hba_open,			/* open */
281 	sata_hba_close,			/* close */
282 	nodev,				/* strategy */
283 	nodev,				/* print */
284 	nodev,				/* dump */
285 	nodev,				/* read */
286 	nodev,				/* write */
287 	sata_hba_ioctl,			/* ioctl */
288 	nodev,				/* devmap */
289 	nodev,				/* mmap */
290 	nodev,				/* segmap */
291 	nochpoll,			/* chpoll */
292 	ddi_prop_op,			/* cb_prop_op */
293 	0,				/* streamtab */
294 	D_NEW | D_MP,			/* cb_flag */
295 	CB_REV,				/* rev */
296 	nodev,				/* aread */
297 	nodev				/* awrite */
298 };
299 
300 
301 extern struct mod_ops mod_miscops;
302 extern uchar_t	scsi_cdb_size[];
303 
304 static struct modlmisc modlmisc = {
305 	&mod_miscops,			/* Type of module */
306 	"SATA Module v%I%"		/* module name */
307 };
308 
309 
310 static struct modlinkage modlinkage = {
311 	MODREV_1,
312 	(void *)&modlmisc,
313 	NULL
314 };
315 
316 /*
317  * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero,
318  * i.e. when scsi_pkt has not timeout specified.
319  */
320 static int sata_default_pkt_time = 60;	/* 60 seconds */
321 
322 /*
323  * Intermediate buffer device access attributes - they are required,
324  * but not necessarily used.
325  */
326 static ddi_device_acc_attr_t sata_acc_attr = {
327 	DDI_DEVICE_ATTR_V0,
328 	DDI_STRUCTURE_LE_ACC,
329 	DDI_STRICTORDER_ACC
330 };
331 
332 
333 /*
334  * Mutexes protecting structures in multithreaded operations.
335  * Because events are relatively rare, a single global mutex protecting
336  * data structures should be sufficient. To increase performance, add
337  * separate mutex per each sata port and use global mutex only to protect
338  * common data structures.
339  */
340 static	kmutex_t sata_mutex;		/* protects sata_hba_list */
341 static	kmutex_t sata_log_mutex;	/* protects log */
342 
343 static 	char sata_log_buf[256];
344 
345 /* Default write cache setting for SATA hard disks */
346 int	sata_write_cache = 1;		/* enabled */
347 
348 /* Default write cache setting for SATA ATAPI CD/DVD */
349 int 	sata_atapicdvd_write_cache = 1; /* enabled */
350 
351 /*
352  * Linked list of HBA instances
353  */
354 static 	sata_hba_inst_t *sata_hba_list = NULL;
355 static 	sata_hba_inst_t *sata_hba_list_tail = NULL;
356 /*
357  * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran
358  * structure and in sata soft state.
359  */
360 
361 /*
362  * Event daemon related variables
363  */
364 static 	kmutex_t sata_event_mutex;
365 static 	kcondvar_t sata_event_cv;
366 static 	kthread_t *sata_event_thread = NULL;
367 static 	int sata_event_thread_terminate = 0;
368 static 	int sata_event_pending = 0;
369 static 	int sata_event_thread_active = 0;
370 extern 	pri_t minclsyspri;
371 
372 /*
373  * NCQ error recovery command
374  */
375 static const sata_cmd_t sata_rle_cmd = {
376 	SATA_CMD_REV,
377 	NULL,
378 	{
379 		SATA_DIR_READ
380 	},
381 	ATA_ADDR_LBA48,
382 	0,
383 	0,
384 	0,
385 	0,
386 	0,
387 	1,
388 	READ_LOG_EXT_NCQ_ERROR_RECOVERY,
389 	0,
390 	0,
391 	0,
392 	SATAC_READ_LOG_EXT,
393 	0,
394 	0,
395 	0,
396 };
397 
398 /*
399  * ATAPI error recovery CDB
400  */
401 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = {
402 	SCMD_REQUEST_SENSE,
403 	0,			/* Only fixed RQ format is supported */
404 	0,
405 	0,
406 	SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */
407 	0
408 };
409 
410 
411 /* Warlock directives */
412 
413 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran))
414 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device))
415 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops))
416 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense))
417 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status))
418 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr))
419 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t))
420 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state))
421 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state))
422 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list))
423 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list))
424 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next))
425 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev))
426 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \
427     sata_hba_inst::satahba_scsi_tran))
428 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran))
429 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip))
430 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached))
431 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port))
432 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex,
433     sata_hba_inst::satahba_event_flags))
434 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
435     sata_cport_info::cport_devp))
436 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp))
437 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr))
438 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
439     sata_cport_info::cport_dev_type))
440 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type))
441 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
442     sata_cport_info::cport_state))
443 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state))
444 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type))
445 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive))
446 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port))
447 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports))
448 #ifdef SATA_DEBUG
449 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count))
450 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count))
451 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace))
452 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index))
453 #endif
454 
455 /* End of warlock directives */
456 
457 /* ************** loadable module configuration functions ************** */
458 
459 int
460 _init()
461 {
462 	int rval;
463 
464 	mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL);
465 	mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL);
466 	mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL);
467 	cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL);
468 	if ((rval = mod_install(&modlinkage)) != 0) {
469 #ifdef SATA_DEBUG
470 		cmn_err(CE_WARN, "sata: _init: mod_install failed\n");
471 #endif
472 		mutex_destroy(&sata_log_mutex);
473 		cv_destroy(&sata_event_cv);
474 		mutex_destroy(&sata_event_mutex);
475 		mutex_destroy(&sata_mutex);
476 	}
477 	return (rval);
478 }
479 
480 int
481 _fini()
482 {
483 	int rval;
484 
485 	if ((rval = mod_remove(&modlinkage)) != 0)
486 		return (rval);
487 
488 	mutex_destroy(&sata_log_mutex);
489 	cv_destroy(&sata_event_cv);
490 	mutex_destroy(&sata_event_mutex);
491 	mutex_destroy(&sata_mutex);
492 	return (rval);
493 }
494 
495 int
496 _info(struct modinfo *modinfop)
497 {
498 	return (mod_info(&modlinkage, modinfop));
499 }
500 
501 
502 
503 /* ********************* SATA HBA entry points ********************* */
504 
505 
506 /*
507  * Called by SATA HBA from _init().
508  * Registers HBA driver instance/sata framework pair with scsi framework, by
509  * calling scsi_hba_init().
510  *
511  * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used
512  * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver
513  * cb_ops pointer in SATA HBA driver dev_ops structure.
514  * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors.
515  *
516  * Return status of the scsi_hba_init() is returned to a calling SATA HBA
517  * driver.
518  */
519 int
520 sata_hba_init(struct modlinkage *modlp)
521 {
522 	int rval;
523 	struct dev_ops *hba_ops;
524 
525 	SATADBG1(SATA_DBG_HBA_IF, NULL,
526 	    "sata_hba_init: name %s \n",
527 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
528 	/*
529 	 * Fill-up cb_ops and dev_ops when necessary
530 	 */
531 	hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
532 	/*
533 	 * Provide pointer to SATA dev_ops
534 	 */
535 	hba_ops->devo_cb_ops = &sata_cb_ops;
536 
537 	/*
538 	 * Register SATA HBA with SCSI framework
539 	 */
540 	if ((rval = scsi_hba_init(modlp)) != 0) {
541 		SATADBG1(SATA_DBG_HBA_IF, NULL,
542 		    "sata_hba_init: scsi hba init failed\n", NULL);
543 		return (rval);
544 	}
545 
546 	return (0);
547 }
548 
549 
550 /* HBA attach stages */
551 #define	HBA_ATTACH_STAGE_SATA_HBA_INST	1
552 #define	HBA_ATTACH_STAGE_SCSI_ATTACHED	2
553 #define	HBA_ATTACH_STAGE_SETUP		4
554 #define	HBA_ATTACH_STAGE_LINKED		8
555 
556 
557 /*
558  *
559  * Called from SATA HBA driver's attach routine to attach an instance of
560  * the HBA.
561  *
562  * For DDI_ATTACH command:
563  * sata_hba_inst structure is allocated here and initialized with pointers to
564  * SATA framework implementation of required scsi tran functions.
565  * The scsi_tran's tran_hba_private field is used by SATA Framework to point
566  * to the soft structure (sata_hba_inst) allocated by SATA framework for
567  * SATA HBA instance related data.
568  * The scsi_tran's tran_hba_private field is used by SATA framework to
569  * store a pointer to per-HBA-instance of sata_hba_inst structure.
570  * The sata_hba_inst structure is cross-linked to scsi tran structure.
571  * Among other info, a pointer to sata_hba_tran structure is stored in
572  * sata_hba_inst. The sata_hba_inst structures for different HBA instances are
573  * linked together into the list, pointed to by sata_hba_list.
574  * On the first HBA instance attach the sata event thread is initialized.
575  * Attachment points are created for all SATA ports of the HBA being attached.
576  * All HBA instance's SATA ports are probed and type of plugged devices is
577  * determined. For each device of a supported type, a target node is created.
578  *
579  * DDI_SUCCESS is returned when attachment process is successful,
580  * DDI_FAILURE is returned otherwise.
581  *
582  * For DDI_RESUME command:
583  * Not implemented at this time (postponed until phase 2 of the development).
584  */
585 int
586 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran,
587     ddi_attach_cmd_t cmd)
588 {
589 	sata_hba_inst_t	*sata_hba_inst;
590 	scsi_hba_tran_t *scsi_tran = NULL;
591 	int hba_attach_state = 0;
592 	char taskq_name[MAXPATHLEN];
593 
594 	SATADBG3(SATA_DBG_HBA_IF, NULL,
595 	    "sata_hba_attach: node %s (%s%d)\n",
596 	    ddi_node_name(dip), ddi_driver_name(dip),
597 	    ddi_get_instance(dip));
598 
599 	if (cmd == DDI_RESUME) {
600 		/*
601 		 * Postponed until phase 2 of the development
602 		 */
603 		return (DDI_FAILURE);
604 	}
605 
606 	if (cmd != DDI_ATTACH) {
607 		return (DDI_FAILURE);
608 	}
609 
610 	/* cmd == DDI_ATTACH */
611 
612 	if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) {
613 		SATA_LOG_D((NULL, CE_WARN,
614 		    "sata_hba_attach: invalid sata_hba_tran"));
615 		return (DDI_FAILURE);
616 	}
617 	/*
618 	 * Allocate and initialize SCSI tran structure.
619 	 * SATA copy of tran_bus_config is provided to create port nodes.
620 	 */
621 	scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
622 	if (scsi_tran == NULL)
623 		return (DDI_FAILURE);
624 	/*
625 	 * Allocate soft structure for SATA HBA instance.
626 	 * There is a separate softstate for each HBA instance.
627 	 */
628 	sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP);
629 	ASSERT(sata_hba_inst != NULL); /* this should not fail */
630 	mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL);
631 	hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST;
632 
633 	/*
634 	 * scsi_trans's tran_hba_private is used by SATA Framework to point to
635 	 * soft structure allocated by SATA framework for
636 	 * SATA HBA instance related data.
637 	 */
638 	scsi_tran->tran_hba_private	= sata_hba_inst;
639 	scsi_tran->tran_tgt_private	= NULL;
640 
641 	scsi_tran->tran_tgt_init	= sata_scsi_tgt_init;
642 	scsi_tran->tran_tgt_probe	= sata_scsi_tgt_probe;
643 	scsi_tran->tran_tgt_free	= sata_scsi_tgt_free;
644 
645 	scsi_tran->tran_start		= sata_scsi_start;
646 	scsi_tran->tran_reset		= sata_scsi_reset;
647 	scsi_tran->tran_abort		= sata_scsi_abort;
648 	scsi_tran->tran_getcap		= sata_scsi_getcap;
649 	scsi_tran->tran_setcap		= sata_scsi_setcap;
650 	scsi_tran->tran_init_pkt	= sata_scsi_init_pkt;
651 	scsi_tran->tran_destroy_pkt	= sata_scsi_destroy_pkt;
652 
653 	scsi_tran->tran_dmafree		= sata_scsi_dmafree;
654 	scsi_tran->tran_sync_pkt	= sata_scsi_sync_pkt;
655 
656 	scsi_tran->tran_reset_notify	= NULL;
657 	scsi_tran->tran_get_bus_addr	= NULL;
658 	scsi_tran->tran_quiesce		= NULL;
659 	scsi_tran->tran_unquiesce	= NULL;
660 	scsi_tran->tran_bus_reset	= NULL;
661 
662 	if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr,
663 	    scsi_tran, 0) != DDI_SUCCESS) {
664 #ifdef SATA_DEBUG
665 		cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed",
666 		    ddi_driver_name(dip), ddi_get_instance(dip));
667 #endif
668 		goto fail;
669 	}
670 	hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED;
671 
672 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) {
673 		if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
674 		    "sata", 1) != DDI_PROP_SUCCESS) {
675 			SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: "
676 			    "failed to create hba sata prop"));
677 			goto fail;
678 		}
679 	}
680 
681 	/*
682 	 * Save pointers in hba instance soft state.
683 	 */
684 	sata_hba_inst->satahba_scsi_tran = scsi_tran;
685 	sata_hba_inst->satahba_tran = sata_tran;
686 	sata_hba_inst->satahba_dip = dip;
687 
688 	/*
689 	 * Create a task queue to handle emulated commands completion
690 	 * Use node name, dash, instance number as the queue name.
691 	 */
692 	taskq_name[0] = '\0';
693 	(void) strlcat(taskq_name, DEVI(dip)->devi_node_name,
694 	    sizeof (taskq_name));
695 	(void) snprintf(taskq_name + strlen(taskq_name),
696 	    sizeof (taskq_name) - strlen(taskq_name),
697 	    "-%d", DEVI(dip)->devi_instance);
698 	sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1,
699 	    minclsyspri, 1, sata_tran->sata_tran_hba_num_cports,
700 	    TASKQ_DYNAMIC);
701 
702 	hba_attach_state |= HBA_ATTACH_STAGE_SETUP;
703 
704 	/*
705 	 * Create events thread if not created yet.
706 	 */
707 	sata_event_thread_control(1);
708 
709 	/*
710 	 * Link this hba instance into the list.
711 	 */
712 	mutex_enter(&sata_mutex);
713 
714 
715 	sata_hba_inst->satahba_next = NULL;
716 	sata_hba_inst->satahba_prev = sata_hba_list_tail;
717 	if (sata_hba_list == NULL) {
718 		sata_hba_list = sata_hba_inst;
719 	}
720 	if (sata_hba_list_tail != NULL) {
721 		sata_hba_list_tail->satahba_next = sata_hba_inst;
722 	}
723 	sata_hba_list_tail = sata_hba_inst;
724 	mutex_exit(&sata_mutex);
725 	hba_attach_state |= HBA_ATTACH_STAGE_LINKED;
726 
727 	/*
728 	 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl
729 	 * SATA HBA driver should not use its own open/close entry points.
730 	 *
731 	 * Make sure that instance number doesn't overflow
732 	 * when forming minor numbers.
733 	 */
734 	ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT));
735 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
736 	    INST2DEVCTL(ddi_get_instance(dip)),
737 	    DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) {
738 #ifdef SATA_DEBUG
739 		cmn_err(CE_WARN, "sata_hba_attach: "
740 		    "cannot create devctl minor node");
741 #endif
742 		goto fail;
743 	}
744 
745 
746 	/*
747 	 * Set-up kstats here, if necessary.
748 	 * (postponed until phase 2 of the development).
749 	 */
750 
751 
752 	/*
753 	 * Probe controller ports. This operation will describe a current
754 	 * controller/port/multipliers/device configuration and will create
755 	 * attachment points.
756 	 * We may end-up with just a controller with no devices attached.
757 	 * For the ports with a supported device attached, device target nodes
758 	 * are created and devices are initialized.
759 	 */
760 	sata_probe_ports(sata_hba_inst);
761 
762 	sata_hba_inst->satahba_attached = 1;
763 	return (DDI_SUCCESS);
764 
765 fail:
766 	if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) {
767 		(void) sata_remove_hba_instance(dip);
768 		if (sata_hba_list == NULL)
769 			sata_event_thread_control(0);
770 	}
771 
772 	if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) {
773 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
774 		taskq_destroy(sata_hba_inst->satahba_taskq);
775 	}
776 
777 	if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED)
778 		(void) scsi_hba_detach(dip);
779 
780 	if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) {
781 		mutex_destroy(&sata_hba_inst->satahba_mutex);
782 		kmem_free((void *)sata_hba_inst,
783 		    sizeof (struct sata_hba_inst));
784 		scsi_hba_tran_free(scsi_tran);
785 	}
786 
787 	sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed",
788 	    ddi_driver_name(dip), ddi_get_instance(dip));
789 
790 	return (DDI_FAILURE);
791 }
792 
793 
794 /*
795  * Called by SATA HBA from to detach an instance of the driver.
796  *
797  * For DDI_DETACH command:
798  * Free local structures allocated for SATA HBA instance during
799  * sata_hba_attach processing.
800  *
801  * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise.
802  *
803  * For DDI_SUSPEND command:
804  * Not implemented at this time (postponed until phase 2 of the development)
805  * Returnd DDI_SUCCESS.
806  *
807  * When the last HBA instance is detached, the event daemon is terminated.
808  *
809  * NOTE: cport support only, no port multiplier support.
810  */
811 int
812 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
813 {
814 	dev_info_t	*tdip;
815 	sata_hba_inst_t	*sata_hba_inst;
816 	scsi_hba_tran_t *scsi_hba_tran;
817 	sata_cport_info_t *cportinfo;
818 	sata_drive_info_t *sdinfo;
819 	int ncport;
820 
821 	SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n",
822 	    ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip));
823 
824 	switch (cmd) {
825 	case DDI_DETACH:
826 
827 		if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
828 			return (DDI_FAILURE);
829 
830 		sata_hba_inst = scsi_hba_tran->tran_hba_private;
831 		if (sata_hba_inst == NULL)
832 			return (DDI_FAILURE);
833 
834 		if (scsi_hba_detach(dip) == DDI_FAILURE) {
835 			sata_hba_inst->satahba_attached = 1;
836 			return (DDI_FAILURE);
837 		}
838 
839 		/*
840 		 * Free all target nodes - at this point
841 		 * devices should be at least offlined
842 		 * otherwise scsi_hba_detach() should not be called.
843 		 */
844 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
845 		    ncport++) {
846 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
847 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
848 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
849 				if (sdinfo != NULL) {
850 					tdip = sata_get_target_dip(dip,
851 					    ncport);
852 					if (tdip != NULL) {
853 						if (ndi_devi_offline(tdip,
854 						    NDI_DEVI_REMOVE) !=
855 						    NDI_SUCCESS) {
856 							SATA_LOG_D((
857 							    sata_hba_inst,
858 							    CE_WARN,
859 							    "sata_hba_detach: "
860 							    "Target node not "
861 							    "removed !"));
862 							return (DDI_FAILURE);
863 						}
864 					}
865 				}
866 			}
867 		}
868 		/*
869 		 * Disable sata event daemon processing for this HBA
870 		 */
871 		sata_hba_inst->satahba_attached = 0;
872 
873 		/*
874 		 * Remove event daemon thread, if it is last HBA instance.
875 		 */
876 
877 		mutex_enter(&sata_mutex);
878 		if (sata_hba_list->satahba_next == NULL) {
879 			mutex_exit(&sata_mutex);
880 			sata_event_thread_control(0);
881 			mutex_enter(&sata_mutex);
882 		}
883 		mutex_exit(&sata_mutex);
884 
885 		/* Remove this HBA instance from the HBA list */
886 		sata_remove_hba_instance(dip);
887 
888 		/*
889 		 * At this point there should be no target nodes attached.
890 		 * Detach and destroy device and port info structures.
891 		 */
892 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
893 		    ncport++) {
894 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
895 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
896 				sdinfo =
897 				    cportinfo->cport_devp.cport_sata_drive;
898 				if (sdinfo != NULL) {
899 					/* Release device structure */
900 					kmem_free(sdinfo,
901 					    sizeof (sata_drive_info_t));
902 				}
903 				/* Release cport info */
904 				mutex_destroy(&cportinfo->cport_mutex);
905 				kmem_free(cportinfo,
906 				    sizeof (sata_cport_info_t));
907 			}
908 		}
909 
910 		scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran);
911 
912 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
913 
914 		taskq_destroy(sata_hba_inst->satahba_taskq);
915 
916 		mutex_destroy(&sata_hba_inst->satahba_mutex);
917 		kmem_free((void *)sata_hba_inst,
918 		    sizeof (struct sata_hba_inst));
919 
920 		return (DDI_SUCCESS);
921 
922 	case DDI_SUSPEND:
923 		/*
924 		 * Postponed until phase 2
925 		 */
926 		return (DDI_FAILURE);
927 
928 	default:
929 		return (DDI_FAILURE);
930 	}
931 }
932 
933 
934 /*
935  * Called by an HBA drive from _fini() routine.
936  * Unregisters SATA HBA instance/SATA framework pair from the scsi framework.
937  */
938 void
939 sata_hba_fini(struct modlinkage *modlp)
940 {
941 	SATADBG1(SATA_DBG_HBA_IF, NULL,
942 	    "sata_hba_fini: name %s\n",
943 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
944 
945 	scsi_hba_fini(modlp);
946 }
947 
948 
949 /*
950  * Default open and close routine for sata_hba framework.
951  *
952  */
953 /*
954  * Open devctl node.
955  *
956  * Returns:
957  * 0 if node was open successfully, error code otherwise.
958  *
959  *
960  */
961 
962 static int
963 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
964 {
965 #ifndef __lock_lint
966 	_NOTE(ARGUNUSED(credp))
967 #endif
968 	int rv = 0;
969 	dev_info_t *dip;
970 	scsi_hba_tran_t *scsi_hba_tran;
971 	sata_hba_inst_t	*sata_hba_inst;
972 
973 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL);
974 
975 	if (otyp != OTYP_CHR)
976 		return (EINVAL);
977 
978 	dip = sata_devt_to_devinfo(*devp);
979 	if (dip == NULL)
980 		return (ENXIO);
981 
982 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
983 		return (ENXIO);
984 
985 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
986 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
987 		return (ENXIO);
988 
989 	mutex_enter(&sata_mutex);
990 	if (flags & FEXCL) {
991 		if (sata_hba_inst->satahba_open_flag != 0) {
992 			rv = EBUSY;
993 		} else {
994 			sata_hba_inst->satahba_open_flag =
995 			    SATA_DEVCTL_EXOPENED;
996 		}
997 	} else {
998 		if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) {
999 			rv = EBUSY;
1000 		} else {
1001 			sata_hba_inst->satahba_open_flag =
1002 			    SATA_DEVCTL_SOPENED;
1003 		}
1004 	}
1005 	mutex_exit(&sata_mutex);
1006 
1007 	return (rv);
1008 }
1009 
1010 
1011 /*
1012  * Close devctl node.
1013  * Returns:
1014  * 0 if node was closed successfully, error code otherwise.
1015  *
1016  */
1017 
1018 static int
1019 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
1020 {
1021 #ifndef __lock_lint
1022 	_NOTE(ARGUNUSED(credp))
1023 	_NOTE(ARGUNUSED(flag))
1024 #endif
1025 	dev_info_t *dip;
1026 	scsi_hba_tran_t *scsi_hba_tran;
1027 	sata_hba_inst_t	*sata_hba_inst;
1028 
1029 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL);
1030 
1031 	if (otyp != OTYP_CHR)
1032 		return (EINVAL);
1033 
1034 	dip = sata_devt_to_devinfo(dev);
1035 	if (dip == NULL)
1036 		return (ENXIO);
1037 
1038 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1039 		return (ENXIO);
1040 
1041 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1042 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1043 		return (ENXIO);
1044 
1045 	mutex_enter(&sata_mutex);
1046 	sata_hba_inst->satahba_open_flag = 0;
1047 	mutex_exit(&sata_mutex);
1048 	return (0);
1049 }
1050 
1051 
1052 
1053 /*
1054  * Standard IOCTL commands for SATA hotplugging.
1055  * Implemented DEVCTL_AP commands:
1056  * DEVCTL_AP_CONNECT
1057  * DEVCTL_AP_DISCONNECT
1058  * DEVCTL_AP_CONFIGURE
1059  * DEVCTL_UNCONFIGURE
1060  * DEVCTL_AP_CONTROL
1061  *
1062  * Commands passed to default ndi ioctl handler:
1063  * DEVCTL_DEVICE_GETSTATE
1064  * DEVCTL_DEVICE_ONLINE
1065  * DEVCTL_DEVICE_OFFLINE
1066  * DEVCTL_DEVICE_REMOVE
1067  * DEVCTL_DEVICE_INSERT
1068  * DEVCTL_BUS_GETSTATE
1069  *
1070  * All other cmds are passed to HBA if it provide ioctl handler, or failed
1071  * if not.
1072  *
1073  * Returns:
1074  * 0 if successful,
1075  * error code if operation failed.
1076  *
1077  * NOTE: Port Multiplier is not supported.
1078  *
1079  */
1080 
1081 static int
1082 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1083     int *rvalp)
1084 {
1085 #ifndef __lock_lint
1086 	_NOTE(ARGUNUSED(credp))
1087 	_NOTE(ARGUNUSED(rvalp))
1088 #endif
1089 	int rv = 0;
1090 	int32_t	comp_port = -1;
1091 	dev_info_t *dip, *tdip;
1092 	devctl_ap_state_t ap_state;
1093 	struct devctl_iocdata *dcp = NULL;
1094 	scsi_hba_tran_t *scsi_hba_tran;
1095 	sata_hba_inst_t *sata_hba_inst;
1096 	sata_device_t sata_device;
1097 	sata_drive_info_t *sdinfo;
1098 	sata_cport_info_t *cportinfo;
1099 	int cport, pmport, qual;
1100 	int rval = SATA_SUCCESS;
1101 
1102 	dip = sata_devt_to_devinfo(dev);
1103 	if (dip == NULL)
1104 		return (ENXIO);
1105 
1106 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1107 		return (ENXIO);
1108 
1109 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1110 	if (sata_hba_inst == NULL)
1111 		return (ENXIO);
1112 
1113 	if (sata_hba_inst->satahba_tran == NULL)
1114 		return (ENXIO);
1115 
1116 	switch (cmd) {
1117 
1118 	case DEVCTL_DEVICE_GETSTATE:
1119 	case DEVCTL_DEVICE_ONLINE:
1120 	case DEVCTL_DEVICE_OFFLINE:
1121 	case DEVCTL_DEVICE_REMOVE:
1122 	case DEVCTL_BUS_GETSTATE:
1123 		/*
1124 		 * There may be more cases that we want to pass to default
1125 		 * handler rather than fail them.
1126 		 */
1127 		return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
1128 	}
1129 
1130 	/* read devctl ioctl data */
1131 	if (cmd != DEVCTL_AP_CONTROL) {
1132 		if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1133 			return (EFAULT);
1134 
1135 		if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) ==
1136 		    -1) {
1137 			if (dcp)
1138 				ndi_dc_freehdl(dcp);
1139 			return (EINVAL);
1140 		}
1141 
1142 		cport = SCSI_TO_SATA_CPORT(comp_port);
1143 		pmport = SCSI_TO_SATA_PMPORT(comp_port);
1144 		/* Only cport is considered now, i.e. SATA_ADDR_CPORT */
1145 		qual = SATA_ADDR_CPORT;
1146 		if (sata_validate_sata_address(sata_hba_inst, cport, pmport,
1147 		    qual) != 0) {
1148 			ndi_dc_freehdl(dcp);
1149 			return (EINVAL);
1150 		}
1151 
1152 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1153 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1154 		    cport_mutex);
1155 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1156 			/*
1157 			 * Cannot process ioctl request now. Come back later.
1158 			 */
1159 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1160 			    cport_mutex);
1161 			ndi_dc_freehdl(dcp);
1162 			return (EBUSY);
1163 		}
1164 		/* Block event processing for this port */
1165 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1166 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1167 
1168 		sata_device.satadev_addr.cport = cport;
1169 		sata_device.satadev_addr.pmport = pmport;
1170 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
1171 		sata_device.satadev_rev = SATA_DEVICE_REV;
1172 	}
1173 
1174 	switch (cmd) {
1175 
1176 	case DEVCTL_AP_DISCONNECT:
1177 		/*
1178 		 * Normally, cfgadm sata plugin will try to offline
1179 		 * (unconfigure) device before this request. Nevertheless,
1180 		 * if a device is still configured, we need to
1181 		 * attempt to offline and unconfigure device first, and we will
1182 		 * deactivate the port regardless of the unconfigure
1183 		 * operation results.
1184 		 *
1185 		 * DEVCTL_AP_DISCONNECT invokes
1186 		 * sata_hba_inst->satahba_tran->
1187 		 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
1188 		 * If successful, the device structure (if any) attached
1189 		 * to a port is removed and state of the port marked
1190 		 * appropriately.
1191 		 * Failure of the port_deactivate may keep port in
1192 		 * the active state, or may fail the port.
1193 		 */
1194 
1195 		/* Check the current state of the port */
1196 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
1197 		    (dip, &sata_device);
1198 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1199 		    cport_mutex);
1200 		sata_update_port_info(sata_hba_inst, &sata_device);
1201 		if (rval != SATA_SUCCESS ||
1202 		    (sata_device.satadev_state & SATA_PSTATE_FAILED)) {
1203 			cportinfo->cport_state = SATA_PSTATE_FAILED;
1204 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1205 			    cport_mutex);
1206 			rv = EIO;
1207 			break;
1208 		}
1209 		/* Sanity check */
1210 		if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
1211 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1212 			    cport_mutex);
1213 			/* No physical port deactivation supported. */
1214 			break;
1215 		}
1216 
1217 		/*
1218 		 * set port's dev_state to not ready - this will disable
1219 		 * an access to an attached device.
1220 		 */
1221 		cportinfo->cport_state &= ~SATA_STATE_READY;
1222 
1223 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
1224 			sdinfo = cportinfo->cport_devp.cport_sata_drive;
1225 			ASSERT(sdinfo != NULL);
1226 			if ((sdinfo->satadrv_type &
1227 			    (SATA_VALID_DEV_TYPE))) {
1228 				/*
1229 				 * If a target node exists, try to offline
1230 				 * a device and remove target node.
1231 				 */
1232 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
1233 				    cport)->cport_mutex);
1234 				tdip = sata_get_target_dip(dip, comp_port);
1235 				if (tdip != NULL && ndi_devi_offline(tdip,
1236 				    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
1237 					/*
1238 					 * Problem
1239 					 * A target node remained
1240 					 * attached. This happens when
1241 					 * the file was open or a node
1242 					 * was waiting for resources.
1243 					 * Cannot do anything about it.
1244 					 */
1245 					SATA_LOG_D((sata_hba_inst, CE_WARN,
1246 					    "sata_hba_ioctl: "
1247 					    "disconnect: could not "
1248 					    "unconfigure device before "
1249 					    "disconnecting the SATA "
1250 					    "port %d", cport));
1251 
1252 					/*
1253 					 * Set DEVICE REMOVED state
1254 					 * in the target node. It
1255 					 * will prevent access to
1256 					 * the device even when a
1257 					 * new device is attached,
1258 					 * until the old target node
1259 					 * is released, removed and
1260 					 * recreated for a new
1261 					 * device.
1262 					 */
1263 					sata_set_device_removed(tdip);
1264 					/*
1265 					 * Instruct event daemon to
1266 					 * try the target node cleanup
1267 					 * later.
1268 					 */
1269 					sata_set_target_node_cleanup(
1270 					    sata_hba_inst, cport);
1271 				}
1272 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
1273 				    cport)->cport_mutex);
1274 				/*
1275 				 * Remove and release sata_drive_info
1276 				 * structure.
1277 				 */
1278 				if (SATA_CPORTINFO_DRV_INFO(cportinfo) !=
1279 				    NULL) {
1280 					SATA_CPORTINFO_DRV_INFO(cportinfo) =
1281 					    NULL;
1282 					(void) kmem_free((void *)sdinfo,
1283 					    sizeof (sata_drive_info_t));
1284 					cportinfo->cport_dev_type =
1285 					    SATA_DTYPE_NONE;
1286 				}
1287 			}
1288 			/*
1289 			 * Note: PMult info requires different handling.
1290 			 * Put PMult handling code here, when PMult is
1291 			 * supported.
1292 			 */
1293 
1294 		}
1295 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1296 		/* Just ask HBA driver to deactivate port */
1297 		sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
1298 
1299 		rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
1300 		    (dip, &sata_device);
1301 
1302 		/*
1303 		 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
1304 		 * without the hint.
1305 		 */
1306 		sata_gen_sysevent(sata_hba_inst,
1307 		    &sata_device.satadev_addr, SE_NO_HINT);
1308 
1309 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1310 		    cport_mutex);
1311 		sata_update_port_info(sata_hba_inst, &sata_device);
1312 
1313 		if (rval != SATA_SUCCESS) {
1314 			/*
1315 			 * Port deactivation failure - do not
1316 			 * change port state unless the state
1317 			 * returned by HBA indicates a port failure.
1318 			 */
1319 			if (sata_device.satadev_state & SATA_PSTATE_FAILED)
1320 				cportinfo->cport_state = SATA_PSTATE_FAILED;
1321 			rv = EIO;
1322 		} else {
1323 			/*
1324 			 * Deactivation succeded. From now on the framework
1325 			 * will not know what is happening to the device, until
1326 			 * the port is activated again.
1327 			 */
1328 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
1329 		}
1330 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1331 		break;
1332 
1333 	case DEVCTL_AP_UNCONFIGURE:
1334 
1335 		/*
1336 		 * The unconfigure operation uses generic nexus operation to
1337 		 * offline a device. It leaves a target device node attached.
1338 		 * and obviously sata_drive_info attached as well, because
1339 		 * from the hardware point of view nothing has changed.
1340 		 */
1341 		if ((tdip = sata_get_target_dip(dip, comp_port)) != NULL) {
1342 
1343 			if (ndi_devi_offline(tdip, NDI_UNCONFIG) !=
1344 			    NDI_SUCCESS) {
1345 				SATA_LOG_D((sata_hba_inst, CE_WARN,
1346 				    "sata_hba_ioctl: unconfigure: "
1347 				    "failed to unconfigure "
1348 				    "device at SATA port %d", cport));
1349 				rv = EIO;
1350 			}
1351 			/*
1352 			 * The target node devi_state should be marked with
1353 			 * DEVI_DEVICE_OFFLINE by ndi_devi_offline().
1354 			 * This would be the indication for cfgadm that
1355 			 * the AP node occupant state is 'unconfigured'.
1356 			 */
1357 
1358 		} else {
1359 			/*
1360 			 * This would indicate a failure on the part of cfgadm
1361 			 * to detect correct state of the node prior to this
1362 			 * call - one cannot unconfigure non-existing device.
1363 			 */
1364 			SATA_LOG_D((sata_hba_inst, CE_WARN,
1365 			    "sata_hba_ioctl: unconfigure: "
1366 			    "attempt to unconfigure non-existing device "
1367 			    "at SATA port %d", cport));
1368 			rv = ENXIO;
1369 		}
1370 
1371 		break;
1372 
1373 	case DEVCTL_AP_CONNECT:
1374 	{
1375 		/*
1376 		 * The sata cfgadm pluging will invoke this operation only if
1377 		 * port was found in the disconnect state (failed state
1378 		 * is also treated as the disconnected state).
1379 		 * DEVCTL_AP_CONNECT would invoke
1380 		 * sata_hba_inst->satahba_tran->
1381 		 * sata_tran_hotplug_ops->sata_tran_port_activate().
1382 		 * If successful and a device is found attached to the port,
1383 		 * the initialization sequence is executed to attach
1384 		 * a device structure to a port structure. The device is not
1385 		 * set in configured state (system-wise) by this operation.
1386 		 * The state of the port and a device would be set
1387 		 * appropriately.
1388 		 *
1389 		 * Note, that activating the port may generate link events,
1390 		 * so is is important that following processing and the
1391 		 * event processing does not interfere with each other!
1392 		 *
1393 		 * This operation may remove port failed state and will
1394 		 * try to make port active and in good standing.
1395 		 */
1396 
1397 		/* We only care about host sata cport for now */
1398 
1399 		if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) != NULL) {
1400 			/* Just let HBA driver to activate port */
1401 
1402 			if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
1403 			    (dip, &sata_device) != SATA_SUCCESS) {
1404 				/*
1405 				 * Port activation failure.
1406 				 */
1407 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
1408 				    cport)->cport_mutex);
1409 				sata_update_port_info(sata_hba_inst,
1410 				    &sata_device);
1411 				if (sata_device.satadev_state &
1412 				    SATA_PSTATE_FAILED) {
1413 					cportinfo->cport_state =
1414 					    SATA_PSTATE_FAILED;
1415 				}
1416 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
1417 				    cport)->cport_mutex);
1418 				SATA_LOG_D((sata_hba_inst, CE_WARN,
1419 				    "sata_hba_ioctl: connect: "
1420 				    "failed to activate SATA port %d",
1421 				    cport));
1422 				rv = EIO;
1423 				break;
1424 			}
1425 		}
1426 		/* Virgin port state - will be updated by the port re-probe. */
1427 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
1428 		    cport)->cport_mutex);
1429 		cportinfo->cport_state = 0;
1430 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
1431 		    cport)->cport_mutex);
1432 
1433 		/*
1434 		 * Probe the port to find its state and attached device.
1435 		 */
1436 		if (sata_reprobe_port(sata_hba_inst, &sata_device,
1437 		    SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE)
1438 			rv = EIO;
1439 		/*
1440 		 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
1441 		 * without the hint
1442 		 */
1443 		sata_gen_sysevent(sata_hba_inst,
1444 		    &sata_device.satadev_addr, SE_NO_HINT);
1445 		/*
1446 		 * If there is a device attached to the port, emit
1447 		 * a message.
1448 		 */
1449 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
1450 			sata_log(sata_hba_inst, CE_WARN,
1451 			    "SATA device detected at port %d", cport);
1452 		}
1453 		break;
1454 	}
1455 
1456 	case DEVCTL_AP_CONFIGURE:
1457 	{
1458 		boolean_t target = TRUE;
1459 
1460 		/*
1461 		 * A port may be in an active or shutdown state.
1462 		 * If port is in a failed state, operation is aborted - one
1463 		 * has to use explicit connect or port activate request
1464 		 * to try to get a port into non-failed mode.
1465 		 *
1466 		 * If a port is in a shutdown state, arbitrarily invoke
1467 		 * sata_tran_port_activate() prior to any other operation.
1468 		 *
1469 		 * Verify that port state is READY and there is a device
1470 		 * of a supported type attached to this port.
1471 		 * If target node exists, a device was most likely offlined.
1472 		 * If target node does not exist, create a target node an
1473 		 * attempt to online it.
1474 		 *		 *
1475 		 * NO PMult or devices beyond PMult are supported yet.
1476 		 */
1477 
1478 		/* We only care about host controller's sata cport for now. */
1479 		if (cportinfo->cport_state & SATA_PSTATE_FAILED) {
1480 			rv = ENXIO;
1481 			break;
1482 		}
1483 		/* Check the current state of the port */
1484 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
1485 
1486 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
1487 		    (dip, &sata_device);
1488 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1489 		    cport_mutex);
1490 		sata_update_port_info(sata_hba_inst, &sata_device);
1491 		if (rval != SATA_SUCCESS ||
1492 		    (sata_device.satadev_state & SATA_PSTATE_FAILED)) {
1493 			cportinfo->cport_state = SATA_PSTATE_FAILED;
1494 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1495 			    cport_mutex);
1496 			rv = EIO;
1497 			break;
1498 		}
1499 		if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN) {
1500 			target = FALSE;
1501 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1502 			    cport_mutex);
1503 
1504 			if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) != NULL) {
1505 				/* Just let HBA driver to activate port */
1506 				if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
1507 				    (dip, &sata_device) != SATA_SUCCESS) {
1508 					/*
1509 					 * Port activation failure - do not
1510 					 * change port state unless the state
1511 					 * returned by HBA indicates a port
1512 					 * failure.
1513 					 */
1514 					mutex_enter(&SATA_CPORT_INFO(
1515 					    sata_hba_inst, cport)->cport_mutex);
1516 					sata_update_port_info(sata_hba_inst,
1517 					    &sata_device);
1518 					if (sata_device.satadev_state &
1519 					    SATA_PSTATE_FAILED) {
1520 						cportinfo->cport_state =
1521 						    SATA_PSTATE_FAILED;
1522 					}
1523 					mutex_exit(&SATA_CPORT_INFO(
1524 					    sata_hba_inst, cport)->cport_mutex);
1525 					SATA_LOG_D((sata_hba_inst, CE_WARN,
1526 					    "sata_hba_ioctl: configure: "
1527 					    "failed to activate SATA port %d",
1528 					    cport));
1529 					rv = EIO;
1530 					break;
1531 				}
1532 			}
1533 			/*
1534 			 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
1535 			 * without the hint.
1536 			 */
1537 			sata_gen_sysevent(sata_hba_inst,
1538 			    &sata_device.satadev_addr, SE_NO_HINT);
1539 
1540 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1541 			    cport_mutex);
1542 			/* Virgin port state */
1543 			cportinfo->cport_state = 0;
1544 		}
1545 		/*
1546 		 * Always reprobe port, to get current device info.
1547 		 */
1548 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1549 		if (sata_reprobe_port(sata_hba_inst, &sata_device,
1550 		    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) {
1551 			rv = EIO;
1552 			break;
1553 		}
1554 		if (target == FALSE &&
1555 		    cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
1556 			/*
1557 			 * That's the transition from "inactive" port
1558 			 * to active one with device attached.
1559 			 */
1560 			sata_log(sata_hba_inst, CE_WARN,
1561 			    "SATA device detected at port %d",
1562 			    cport);
1563 		}
1564 
1565 		/*
1566 		 * This is where real configure starts.
1567 		 * Change following check for PMult support.
1568 		 */
1569 		if (!(sata_device.satadev_type & SATA_VALID_DEV_TYPE)) {
1570 			/* No device to configure */
1571 			rv = ENXIO; /* No device to configure */
1572 			break;
1573 		}
1574 
1575 		/*
1576 		 * Here we may have a device in reset condition,
1577 		 * but because we are just configuring it, there is
1578 		 * no need to process the reset other than just
1579 		 * to clear device reset condition in the HBA driver.
1580 		 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will
1581 		 * cause a first command sent the HBA driver with the request
1582 		 * to clear device reset condition.
1583 		 */
1584 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1585 		    cport_mutex);
1586 		sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
1587 		if (sdinfo == NULL) {
1588 			rv = ENXIO;
1589 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1590 			    cport_mutex);
1591 			break;
1592 		}
1593 		if (sdinfo->satadrv_event_flags &
1594 		    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET))
1595 			sdinfo->satadrv_event_flags = 0;
1596 		sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
1597 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1598 
1599 		if ((tdip = sata_get_target_dip(dip, comp_port)) != NULL) {
1600 			/*
1601 			 * Target node exists. Verify, that it belongs
1602 			 * to existing, attached device and not to
1603 			 * a removed device.
1604 			 */
1605 			if (sata_check_device_removed(tdip) == B_FALSE) {
1606 				if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) {
1607 					SATA_LOG_D((sata_hba_inst, CE_WARN,
1608 					    "sata_hba_ioctl: configure: "
1609 					    "onlining device at SATA port %d "
1610 					    "failed", cport));
1611 					rv = EIO;
1612 					break;
1613 				} else {
1614 					mutex_enter(&SATA_CPORT_INFO(
1615 					    sata_hba_inst, cport)->cport_mutex);
1616 					SATA_CPORT_INFO(sata_hba_inst, cport)->
1617 					    cport_tgtnode_clean = B_TRUE;
1618 					mutex_exit(&SATA_CPORT_INFO(
1619 					    sata_hba_inst, cport)->cport_mutex);
1620 				}
1621 			} else {
1622 				sata_log(sata_hba_inst, CE_WARN,
1623 				    "SATA device at port %d cannot be "
1624 				    "configured. "
1625 				    "Application(s) accessing previously "
1626 				    "attached device "
1627 				    "have to release it before newly inserted "
1628 				    "device can be made accessible.",
1629 				    cport);
1630 				break;
1631 			}
1632 		} else {
1633 			/*
1634 			 * No target node - need to create a new target node.
1635 			 */
1636 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1637 			    cport_mutex);
1638 			SATA_CPORT_INFO(sata_hba_inst, cport)->
1639 			    cport_tgtnode_clean = B_TRUE;
1640 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1641 			    cport_mutex);
1642 			tdip = sata_create_target_node(dip, sata_hba_inst,
1643 			    &sata_device.satadev_addr);
1644 			if (tdip == NULL) {
1645 				/* configure failed */
1646 				SATA_LOG_D((sata_hba_inst, CE_WARN,
1647 				    "sata_hba_ioctl: configure: "
1648 				    "configuring SATA device at port %d "
1649 				    "failed", cport));
1650 				rv = EIO;
1651 				break;
1652 			}
1653 		}
1654 
1655 		break;
1656 	}
1657 
1658 	case DEVCTL_AP_GETSTATE:
1659 
1660 		sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state);
1661 
1662 		ap_state.ap_last_change = (time_t)-1;
1663 		ap_state.ap_error_code = 0;
1664 		ap_state.ap_in_transition = 0;
1665 
1666 		/* Copy the return AP-state information to the user space */
1667 		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
1668 			rv = EFAULT;
1669 		}
1670 		break;
1671 
1672 	case DEVCTL_AP_CONTROL:
1673 	{
1674 		/*
1675 		 * Generic devctl for hardware specific functionality
1676 		 */
1677 		sata_ioctl_data_t	ioc;
1678 
1679 		ASSERT(dcp == NULL);
1680 
1681 		/* Copy in user ioctl data first */
1682 #ifdef _MULTI_DATAMODEL
1683 		if (ddi_model_convert_from(mode & FMODELS) ==
1684 		    DDI_MODEL_ILP32) {
1685 
1686 			sata_ioctl_data_32_t	ioc32;
1687 
1688 			if (ddi_copyin((void *)arg, (void *)&ioc32,
1689 			    sizeof (ioc32), mode) != 0) {
1690 				rv = EFAULT;
1691 				break;
1692 			}
1693 			ioc.cmd 	= (uint_t)ioc32.cmd;
1694 			ioc.port	= (uint_t)ioc32.port;
1695 			ioc.get_size	= (uint_t)ioc32.get_size;
1696 			ioc.buf		= (caddr_t)(uintptr_t)ioc32.buf;
1697 			ioc.bufsiz	= (uint_t)ioc32.bufsiz;
1698 			ioc.misc_arg	= (uint_t)ioc32.misc_arg;
1699 		} else
1700 #endif /* _MULTI_DATAMODEL */
1701 		if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
1702 		    mode) != 0) {
1703 			return (EFAULT);
1704 		}
1705 
1706 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
1707 		    "sata_hba_ioctl: DEVCTL_AP_CONTROL "
1708 		    "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port);
1709 
1710 		/*
1711 		 * To avoid BE/LE and 32/64 issues, a get_size always returns
1712 		 * a 32-bit number.
1713 		 */
1714 		if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
1715 			return (EINVAL);
1716 		}
1717 		/* validate address */
1718 		cport = SCSI_TO_SATA_CPORT(ioc.port);
1719 		pmport = SCSI_TO_SATA_PMPORT(ioc.port);
1720 		qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port);
1721 
1722 		/* Override address qualifier - handle cport only for now */
1723 		qual = SATA_ADDR_CPORT;
1724 
1725 		if (sata_validate_sata_address(sata_hba_inst, cport,
1726 		    pmport, qual) != 0)
1727 			return (EINVAL);
1728 
1729 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1730 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1731 		    cport_mutex);
1732 		/* Is the port locked by event processing daemon ? */
1733 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1734 			/*
1735 			 * Cannot process ioctl request now. Come back later
1736 			 */
1737 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1738 			    cport_mutex);
1739 			return (EBUSY);
1740 		}
1741 		/* Block event processing for this port */
1742 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1743 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1744 
1745 
1746 		sata_device.satadev_addr.cport = cport;
1747 		sata_device.satadev_addr.pmport = pmport;
1748 		sata_device.satadev_rev = SATA_DEVICE_REV;
1749 
1750 		switch (ioc.cmd) {
1751 
1752 		case SATA_CFGA_RESET_PORT:
1753 			/*
1754 			 * There is no protection here for configured
1755 			 * device.
1756 			 */
1757 
1758 			/* Sanity check */
1759 			if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
1760 				SATA_LOG_D((sata_hba_inst, CE_WARN,
1761 				    "sata_hba_ioctl: "
1762 				    "sata_hba_tran missing required "
1763 				    "function sata_tran_reset_dport"));
1764 				rv = EINVAL;
1765 				break;
1766 			}
1767 
1768 			/* handle cport only for now */
1769 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
1770 			if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
1771 			    (dip, &sata_device) != SATA_SUCCESS) {
1772 				SATA_LOG_D((sata_hba_inst, CE_WARN,
1773 				    "sata_hba_ioctl: reset port: "
1774 				    "failed cport %d pmport %d",
1775 				    cport, pmport));
1776 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
1777 				    cport)->cport_mutex);
1778 				sata_update_port_info(sata_hba_inst,
1779 				    &sata_device);
1780 				SATA_CPORT_STATE(sata_hba_inst, cport) =
1781 				    SATA_PSTATE_FAILED;
1782 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
1783 				    cport)->cport_mutex);
1784 				rv = EIO;
1785 			}
1786 			/*
1787 			 * Since the port was reset, it should be probed and
1788 			 * attached device reinitialized. At this point the
1789 			 * port state is unknown - it's state is HBA-specific.
1790 			 * Re-probe port to get its state.
1791 			 */
1792 			if (sata_reprobe_port(sata_hba_inst, &sata_device,
1793 			    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) {
1794 				rv = EIO;
1795 				break;
1796 			}
1797 			break;
1798 
1799 		case SATA_CFGA_RESET_DEVICE:
1800 			/*
1801 			 * There is no protection here for configured
1802 			 * device.
1803 			 */
1804 
1805 			/* Sanity check */
1806 			if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
1807 				SATA_LOG_D((sata_hba_inst, CE_WARN,
1808 				    "sata_hba_ioctl: "
1809 				    "sata_hba_tran missing required "
1810 				    "function sata_tran_reset_dport"));
1811 				rv = EINVAL;
1812 				break;
1813 			}
1814 
1815 			/* handle only device attached to cports, for now */
1816 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
1817 
1818 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1819 			    cport_mutex);
1820 			sdinfo = sata_get_device_info(sata_hba_inst,
1821 			    &sata_device);
1822 			if (sdinfo == NULL) {
1823 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
1824 				    cport)->cport_mutex);
1825 				rv = EINVAL;
1826 				break;
1827 			}
1828 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1829 			    cport_mutex);
1830 
1831 			/* only handle cport for now */
1832 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
1833 			if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
1834 			    (dip, &sata_device) != SATA_SUCCESS) {
1835 				SATA_LOG_D((sata_hba_inst, CE_WARN,
1836 				    "sata_hba_ioctl: reset device: failed "
1837 				    "cport %d pmport %d", cport, pmport));
1838 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
1839 				    cport)->cport_mutex);
1840 				sata_update_port_info(sata_hba_inst,
1841 				    &sata_device);
1842 				/*
1843 				 * Device info structure remains
1844 				 * attached. Another device reset or
1845 				 * port disconnect/connect and re-probing is
1846 				 * needed to change it's state
1847 				 */
1848 				sdinfo->satadrv_state &= ~SATA_STATE_READY;
1849 				sdinfo->satadrv_state |=
1850 				    SATA_DSTATE_FAILED;
1851 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
1852 				    cport)->cport_mutex);
1853 				rv = EIO;
1854 			}
1855 			/*
1856 			 * Since the device was reset, we expect reset event
1857 			 * to be reported and processed.
1858 			 */
1859 			break;
1860 
1861 		case SATA_CFGA_RESET_ALL:
1862 		{
1863 			int tcport;
1864 
1865 			/*
1866 			 * There is no protection here for configured
1867 			 * devices.
1868 			 */
1869 			/* Sanity check */
1870 			if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
1871 				SATA_LOG_D((sata_hba_inst, CE_WARN,
1872 				    "sata_hba_ioctl: "
1873 				    "sata_hba_tran missing required "
1874 				    "function sata_tran_reset_dport"));
1875 				rv = EINVAL;
1876 				break;
1877 			}
1878 
1879 			/*
1880 			 * Need to lock all ports, not just one.
1881 			 * If any port is locked by event processing, fail
1882 			 * the whole operation.
1883 			 * One port is already locked, but for simplicity
1884 			 * lock it again.
1885 			 */
1886 			for (tcport = 0;
1887 			    tcport < SATA_NUM_CPORTS(sata_hba_inst);
1888 			    tcport++) {
1889 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
1890 				    tcport)->cport_mutex);
1891 				if (((SATA_CPORT_INFO(sata_hba_inst, tcport)->
1892 				    cport_event_flags) &
1893 				    SATA_EVNT_LOCK_PORT_BUSY) != 0) {
1894 					rv = EBUSY;
1895 					mutex_exit(
1896 					    &SATA_CPORT_INFO(sata_hba_inst,
1897 					    tcport)->cport_mutex);
1898 					break;
1899 				} else {
1900 					SATA_CPORT_INFO(sata_hba_inst,
1901 					    tcport)->cport_event_flags |=
1902 					    SATA_APCTL_LOCK_PORT_BUSY;
1903 				}
1904 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
1905 				    tcport)->cport_mutex);
1906 			}
1907 
1908 			if (rv == 0) {
1909 				/*
1910 				 * All cports successfully locked.
1911 				 * Reset main SATA controller only for now -
1912 				 * no PMult.
1913 				 */
1914 				sata_device.satadev_addr.qual =
1915 				    SATA_ADDR_CNTRL;
1916 
1917 				if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
1918 				    (dip, &sata_device) != SATA_SUCCESS) {
1919 					SATA_LOG_D((sata_hba_inst, CE_WARN,
1920 					    "sata_hba_ioctl: reset controller "
1921 					    "failed"));
1922 					rv = EIO;
1923 				}
1924 
1925 				/*
1926 				 * Since ports were reset, they should be
1927 				 * re-probed and attached devices
1928 				 * reinitialized.
1929 				 * At this point port states are unknown,
1930 				 * Re-probe ports to get their state -
1931 				 * cports only for now.
1932 				 */
1933 				for (tcport = 0;
1934 				    tcport < SATA_NUM_CPORTS(sata_hba_inst);
1935 				    tcport++) {
1936 					sata_device.satadev_addr.cport =
1937 					    tcport;
1938 					sata_device.satadev_addr.qual =
1939 					    SATA_ADDR_CPORT;
1940 
1941 					if (sata_reprobe_port(sata_hba_inst,
1942 					    &sata_device,
1943 					    SATA_DEV_IDENTIFY_RETRY) !=
1944 					    SATA_SUCCESS)
1945 						rv = EIO;
1946 
1947 				}
1948 			}
1949 			/*
1950 			 * Unlock all ports
1951 			 */
1952 			for (tcport = 0;
1953 			    tcport < SATA_NUM_CPORTS(sata_hba_inst);
1954 			    tcport++) {
1955 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
1956 				    tcport)->cport_mutex);
1957 				SATA_CPORT_INFO(sata_hba_inst, tcport)->
1958 				    cport_event_flags &=
1959 				    ~SATA_APCTL_LOCK_PORT_BUSY;
1960 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
1961 				    tcport)->cport_mutex);
1962 			}
1963 
1964 			/*
1965 			 * This operation returns EFAULT if either reset
1966 			 * controller failed or a re-probing of any ports
1967 			 * failed.
1968 			 * We return here, because common return is for
1969 			 * a single cport operation.
1970 			 */
1971 			return (rv);
1972 		}
1973 
1974 		case SATA_CFGA_PORT_DEACTIVATE:
1975 			/* Sanity check */
1976 			if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
1977 				rv = ENOTSUP;
1978 				break;
1979 			}
1980 			/*
1981 			 * Arbitrarily unconfigure attached device, if any.
1982 			 * Even if the unconfigure fails, proceed with the
1983 			 * port deactivation.
1984 			 */
1985 
1986 			/* Handle only device attached to cports, for now */
1987 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
1988 
1989 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1990 			    cport_mutex);
1991 			cportinfo->cport_state &= ~SATA_STATE_READY;
1992 			if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
1993 				/*
1994 				 * Handle only device attached to cports,
1995 				 * for now
1996 				 */
1997 				sata_device.satadev_addr.qual =
1998 				    SATA_ADDR_DCPORT;
1999 				sdinfo = sata_get_device_info(sata_hba_inst,
2000 				    &sata_device);
2001 				if (sdinfo != NULL &&
2002 				    (sdinfo->satadrv_type &
2003 				    SATA_VALID_DEV_TYPE)) {
2004 					/*
2005 					 * If a target node exists, try to
2006 					 * offline a device and remove target
2007 					 * node.
2008 					 */
2009 					mutex_exit(&SATA_CPORT_INFO(
2010 					    sata_hba_inst, cport)->cport_mutex);
2011 					tdip = sata_get_target_dip(dip, cport);
2012 					if (tdip != NULL) {
2013 						/* target node exist */
2014 						SATADBG1(SATA_DBG_IOCTL_IF,
2015 						    sata_hba_inst,
2016 						    "sata_hba_ioctl: "
2017 						    "port deactivate: "
2018 						    "target node exists.",
2019 						    NULL);
2020 
2021 						if (ndi_devi_offline(tdip,
2022 						    NDI_DEVI_REMOVE) !=
2023 						    NDI_SUCCESS) {
2024 							SATA_LOG_D((
2025 							    sata_hba_inst,
2026 							    CE_WARN,
2027 							    "sata_hba_ioctl:"
2028 							    "port deactivate: "
2029 							    "failed to "
2030 							    "unconfigure "
2031 							    "device at port "
2032 							    "%d before "
2033 							    "deactivating "
2034 							    "the port", cport));
2035 							/*
2036 							 * Set DEVICE REMOVED
2037 							 * state in the target
2038 							 * node. It will
2039 							 * prevent access to
2040 							 * the device even when
2041 							 * a new device is
2042 							 * attached, until the
2043 							 * old target node is
2044 							 * released, removed and
2045 							 * recreated for a new
2046 							 * device.
2047 							 */
2048 							sata_set_device_removed
2049 							    (tdip);
2050 							/*
2051 							 * Instruct event
2052 							 * daemon to try the
2053 							 * target node cleanup
2054 							 * later.
2055 							 */
2056 						sata_set_target_node_cleanup(
2057 						    sata_hba_inst, cport);
2058 						}
2059 					}
2060 					mutex_enter(&SATA_CPORT_INFO(
2061 					    sata_hba_inst, cport)->cport_mutex);
2062 					/*
2063 					 * In any case,
2064 					 * remove and release sata_drive_info
2065 					 * structure.
2066 					 * (cport attached device ony, for now)
2067 					 */
2068 					SATA_CPORTINFO_DRV_INFO(cportinfo) =
2069 					    NULL;
2070 					(void) kmem_free((void *)sdinfo,
2071 					    sizeof (sata_drive_info_t));
2072 					cportinfo->cport_dev_type =
2073 					    SATA_DTYPE_NONE;
2074 				}
2075 				/*
2076 				 * Note: PMult info requires different
2077 				 * handling. This comment is a placeholder for
2078 				 * a code handling PMult, to be implemented
2079 				 * in phase 2.
2080 				 */
2081 			}
2082 			cportinfo->cport_state &= ~(SATA_STATE_PROBED |
2083 			    SATA_STATE_PROBING);
2084 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
2085 			    cport_mutex);
2086 			/* handle cport only for now */
2087 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2088 			/* Just let HBA driver to deactivate port */
2089 			rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
2090 			    (dip, &sata_device);
2091 			/*
2092 			 * Generate sysevent -
2093 			 * EC_DR / ESC_DR_AP_STATE_CHANGE
2094 			 * without the hint
2095 			 */
2096 			sata_gen_sysevent(sata_hba_inst,
2097 			    &sata_device.satadev_addr, SE_NO_HINT);
2098 
2099 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
2100 			    cport_mutex);
2101 			sata_update_port_info(sata_hba_inst, &sata_device);
2102 			if (rval != SATA_SUCCESS) {
2103 				/*
2104 				 * Port deactivation failure - do not
2105 				 * change port state unless the state
2106 				 * returned by HBA indicates a port failure.
2107 				 */
2108 				if (sata_device.satadev_state &
2109 				    SATA_PSTATE_FAILED) {
2110 					SATA_CPORT_STATE(sata_hba_inst,
2111 					    cport) = SATA_PSTATE_FAILED;
2112 				}
2113 				SATA_LOG_D((sata_hba_inst, CE_WARN,
2114 				    "sata_hba_ioctl: port deactivate: "
2115 				    "cannot deactivate SATA port %d",
2116 				    cport));
2117 				rv = EIO;
2118 			} else {
2119 				cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
2120 			}
2121 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
2122 			    cport_mutex);
2123 
2124 			break;
2125 
2126 		case SATA_CFGA_PORT_ACTIVATE:
2127 		{
2128 			boolean_t dev_existed = TRUE;
2129 
2130 			/* Sanity check */
2131 			if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) {
2132 				rv = ENOTSUP;
2133 				break;
2134 			}
2135 			/* handle cport only for now */
2136 			if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN ||
2137 			    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
2138 				dev_existed = FALSE;
2139 
2140 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2141 			/* Just let HBA driver to activate port */
2142 			if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
2143 			    (dip, &sata_device) != SATA_SUCCESS) {
2144 				/*
2145 				 * Port activation failure - do not
2146 				 * change port state unless the state
2147 				 * returned by HBA indicates a port failure.
2148 				 */
2149 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
2150 				    cport)->cport_mutex);
2151 				sata_update_port_info(sata_hba_inst,
2152 				    &sata_device);
2153 				if (sata_device.satadev_state &
2154 				    SATA_PSTATE_FAILED) {
2155 					SATA_CPORT_STATE(sata_hba_inst,
2156 					    cport) = SATA_PSTATE_FAILED;
2157 				}
2158 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
2159 				    cport)->cport_mutex);
2160 				SATA_LOG_D((sata_hba_inst, CE_WARN,
2161 				    "sata_hba_ioctl: port activate: "
2162 				    "cannot activate SATA port %d",
2163 				    cport));
2164 				rv = EIO;
2165 				break;
2166 			}
2167 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
2168 			    cport_mutex);
2169 			cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN;
2170 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
2171 			    cport_mutex);
2172 
2173 			/*
2174 			 * Re-probe port to find its current state and
2175 			 * possibly attached device.
2176 			 * Port re-probing may change the cportinfo device
2177 			 * type if device is found attached.
2178 			 * If port probing failed, the device type would be
2179 			 * set to SATA_DTYPE_NONE.
2180 			 */
2181 			(void) sata_reprobe_port(sata_hba_inst, &sata_device,
2182 			    SATA_DEV_IDENTIFY_RETRY);
2183 
2184 			/*
2185 			 * Generate sysevent -
2186 			 * EC_DR / ESC_DR_AP_STATE_CHANGE
2187 			 * without the hint.
2188 			 */
2189 			sata_gen_sysevent(sata_hba_inst,
2190 			    &sata_device.satadev_addr, SE_NO_HINT);
2191 
2192 			if (dev_existed == FALSE &&
2193 			    cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
2194 				/*
2195 				 * That's the transition from "inactive" port
2196 				 * state or active port without a device
2197 				 * attached to the active port state with
2198 				 * a device attached.
2199 				 */
2200 				sata_log(sata_hba_inst, CE_WARN,
2201 				    "SATA device detected at port %d", cport);
2202 			}
2203 
2204 			break;
2205 		}
2206 
2207 		case SATA_CFGA_PORT_SELF_TEST:
2208 
2209 			/* Sanity check */
2210 			if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL) {
2211 				rv = ENOTSUP;
2212 				break;
2213 			}
2214 			/*
2215 			 * There is no protection here for a configured
2216 			 * device attached to this port.
2217 			 */
2218 
2219 			/* only handle cport for now */
2220 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2221 
2222 			if ((*SATA_SELFTEST_FUNC(sata_hba_inst))
2223 			    (dip, &sata_device) != SATA_SUCCESS) {
2224 				SATA_LOG_D((sata_hba_inst, CE_WARN,
2225 				    "sata_hba_ioctl: port selftest: "
2226 				    "failed cport %d pmport %d",
2227 				    cport, pmport));
2228 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
2229 				    cport)->cport_mutex);
2230 				sata_update_port_info(sata_hba_inst,
2231 				    &sata_device);
2232 				SATA_CPORT_STATE(sata_hba_inst, cport) =
2233 				    SATA_PSTATE_FAILED;
2234 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
2235 				    cport)->cport_mutex);
2236 				rv = EIO;
2237 				break;
2238 			}
2239 			/*
2240 			 * Since the port was reset, it should be probed and
2241 			 * attached device reinitialized. At this point the
2242 			 * port state is unknown - it's state is HBA-specific.
2243 			 * Force port re-probing to get it into a known state.
2244 			 */
2245 			if (sata_reprobe_port(sata_hba_inst, &sata_device,
2246 			    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) {
2247 				rv = EIO;
2248 				break;
2249 			}
2250 			break;
2251 
2252 		case SATA_CFGA_GET_DEVICE_PATH:
2253 		{
2254 			char		path[MAXPATHLEN];
2255 			uint32_t	size;
2256 
2257 			(void) strcpy(path, "/devices");
2258 			if ((tdip = sata_get_target_dip(dip, ioc.port)) ==
2259 			    NULL) {
2260 
2261 				/*
2262 				 * No such device.
2263 				 * If this is a request for a size, do not
2264 				 * return EINVAL for non-exisiting target,
2265 				 * because cfgadm will indicate a meaningless
2266 				 * ioctl failure.
2267 				 * If this is a real request for a path,
2268 				 * indicate invalid argument.
2269 				 */
2270 				if (!ioc.get_size) {
2271 					rv = EINVAL;
2272 					break;
2273 				}
2274 			} else {
2275 				(void) ddi_pathname(tdip, path + strlen(path));
2276 			}
2277 			size = strlen(path) + 1;
2278 
2279 			if (ioc.get_size) {
2280 				if (ddi_copyout((void *)&size,
2281 				    ioc.buf, ioc.bufsiz, mode) != 0) {
2282 					rv = EFAULT;
2283 				}
2284 			} else {
2285 				if (ioc.bufsiz != size) {
2286 					rv = EINVAL;
2287 				} else if (ddi_copyout((void *)&path,
2288 				    ioc.buf, ioc.bufsiz, mode) != 0) {
2289 					rv = EFAULT;
2290 				}
2291 			}
2292 			break;
2293 		}
2294 
2295 		case SATA_CFGA_GET_AP_TYPE:
2296 		{
2297 			uint32_t	type_len;
2298 			const char	*ap_type;
2299 
2300 			/* cport only, no port multiplier support */
2301 			switch (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport)) {
2302 			case SATA_DTYPE_NONE:
2303 				ap_type = "port";
2304 				break;
2305 
2306 			case SATA_DTYPE_ATADISK:
2307 				ap_type = "disk";
2308 				break;
2309 
2310 			case SATA_DTYPE_ATAPICD:
2311 				ap_type = "cd/dvd";
2312 				break;
2313 
2314 			case SATA_DTYPE_PMULT:
2315 				ap_type = "pmult";
2316 				break;
2317 
2318 			case SATA_DTYPE_UNKNOWN:
2319 				ap_type = "unknown";
2320 				break;
2321 
2322 			default:
2323 				ap_type = "unsupported";
2324 				break;
2325 
2326 			} /* end of dev_type switch */
2327 
2328 			type_len = strlen(ap_type) + 1;
2329 
2330 			if (ioc.get_size) {
2331 				if (ddi_copyout((void *)&type_len,
2332 				    ioc.buf, ioc.bufsiz, mode) != 0) {
2333 					rv = EFAULT;
2334 					break;
2335 				}
2336 			} else {
2337 				if (ioc.bufsiz != type_len) {
2338 					rv = EINVAL;
2339 					break;
2340 				}
2341 				if (ddi_copyout((void *)ap_type, ioc.buf,
2342 				    ioc.bufsiz, mode) != 0) {
2343 					rv = EFAULT;
2344 					break;
2345 				}
2346 			}
2347 
2348 			break;
2349 		}
2350 
2351 		case SATA_CFGA_GET_MODEL_INFO:
2352 		{
2353 			uint32_t info_len;
2354 			char ap_info[sizeof (sdinfo->satadrv_id.ai_model) + 1];
2355 
2356 			/*
2357 			 * This operation should return to cfgadm the
2358 			 * device model information string
2359 			 */
2360 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
2361 			    cport_mutex);
2362 			/* only handle device connected to cport for now */
2363 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
2364 			sdinfo = sata_get_device_info(sata_hba_inst,
2365 			    &sata_device);
2366 			if (sdinfo == NULL) {
2367 				rv = EINVAL;
2368 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
2369 				    cport)->cport_mutex);
2370 				break;
2371 			}
2372 			bcopy(sdinfo->satadrv_id.ai_model, ap_info,
2373 			    sizeof (sdinfo->satadrv_id.ai_model));
2374 			swab(ap_info, ap_info,
2375 			    sizeof (sdinfo->satadrv_id.ai_model));
2376 			ap_info[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
2377 
2378 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
2379 			    cport_mutex);
2380 
2381 			info_len = strlen(ap_info) + 1;
2382 
2383 			if (ioc.get_size) {
2384 				if (ddi_copyout((void *)&info_len,
2385 				    ioc.buf, ioc.bufsiz, mode) != 0) {
2386 					rv = EFAULT;
2387 					break;
2388 				}
2389 			} else {
2390 				if (ioc.bufsiz < info_len) {
2391 					rv = EINVAL;
2392 					break;
2393 				}
2394 				if (ddi_copyout((void *)ap_info, ioc.buf,
2395 				    ioc.bufsiz, mode) != 0) {
2396 					rv = EFAULT;
2397 					break;
2398 				}
2399 			}
2400 
2401 			break;
2402 		}
2403 
2404 		case SATA_CFGA_GET_REVFIRMWARE_INFO:
2405 		{
2406 			uint32_t info_len;
2407 			char ap_info[
2408 			    sizeof (sdinfo->satadrv_id.ai_fw) + 1];
2409 
2410 			/*
2411 			 * This operation should return to cfgadm the
2412 			 * device firmware revision information string
2413 			 */
2414 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
2415 			    cport_mutex);
2416 			/* only handle device connected to cport for now */
2417 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
2418 
2419 			sdinfo = sata_get_device_info(sata_hba_inst,
2420 			    &sata_device);
2421 			if (sdinfo == NULL) {
2422 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
2423 				    cport)->cport_mutex);
2424 				rv = EINVAL;
2425 				break;
2426 			}
2427 			bcopy(sdinfo->satadrv_id.ai_fw, ap_info,
2428 			    sizeof (sdinfo->satadrv_id.ai_fw));
2429 			swab(ap_info, ap_info,
2430 			    sizeof (sdinfo->satadrv_id.ai_fw));
2431 			ap_info[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
2432 
2433 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
2434 			    cport_mutex);
2435 
2436 			info_len = strlen(ap_info) + 1;
2437 
2438 			if (ioc.get_size) {
2439 				if (ddi_copyout((void *)&info_len,
2440 				    ioc.buf, ioc.bufsiz, mode) != 0) {
2441 					rv = EFAULT;
2442 					break;
2443 				}
2444 			} else {
2445 				if (ioc.bufsiz < info_len) {
2446 					rv = EINVAL;
2447 					break;
2448 				}
2449 				if (ddi_copyout((void *)ap_info, ioc.buf,
2450 				    ioc.bufsiz, mode) != 0) {
2451 					rv = EFAULT;
2452 					break;
2453 				}
2454 			}
2455 
2456 			break;
2457 		}
2458 
2459 		case SATA_CFGA_GET_SERIALNUMBER_INFO:
2460 		{
2461 			uint32_t info_len;
2462 			char ap_info[
2463 			    sizeof (sdinfo->satadrv_id.ai_drvser) + 1];
2464 
2465 			/*
2466 			 * This operation should return to cfgadm the
2467 			 * device serial number information string
2468 			 */
2469 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
2470 			    cport_mutex);
2471 			/* only handle device connected to cport for now */
2472 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
2473 
2474 			sdinfo = sata_get_device_info(sata_hba_inst,
2475 			    &sata_device);
2476 			if (sdinfo == NULL) {
2477 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
2478 				    cport)->cport_mutex);
2479 				rv = EINVAL;
2480 				break;
2481 			}
2482 			bcopy(sdinfo->satadrv_id.ai_drvser, ap_info,
2483 			    sizeof (sdinfo->satadrv_id.ai_drvser));
2484 			swab(ap_info, ap_info,
2485 			    sizeof (sdinfo->satadrv_id.ai_drvser));
2486 			ap_info[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
2487 
2488 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
2489 			    cport_mutex);
2490 
2491 			info_len = strlen(ap_info) + 1;
2492 
2493 			if (ioc.get_size) {
2494 				if (ddi_copyout((void *)&info_len,
2495 				    ioc.buf, ioc.bufsiz, mode) != 0) {
2496 					rv = EFAULT;
2497 					break;
2498 				}
2499 			} else {
2500 				if (ioc.bufsiz < info_len) {
2501 					rv = EINVAL;
2502 					break;
2503 				}
2504 				if (ddi_copyout((void *)ap_info, ioc.buf,
2505 				    ioc.bufsiz, mode) != 0) {
2506 					rv = EFAULT;
2507 					break;
2508 				}
2509 			}
2510 
2511 			break;
2512 		}
2513 
2514 		default:
2515 			rv = EINVAL;
2516 			break;
2517 
2518 		} /* End of DEVCTL_AP_CONTROL cmd switch */
2519 
2520 		break;
2521 	}
2522 
2523 	default:
2524 	{
2525 		/*
2526 		 * If we got here, we got an IOCTL that SATA HBA Framework
2527 		 * does not recognize. Pass ioctl to HBA driver, in case
2528 		 * it could process it.
2529 		 */
2530 		sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran;
2531 		dev_info_t	*mydip = SATA_DIP(sata_hba_inst);
2532 
2533 		SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
2534 		    "IOCTL 0x%2x not supported in SATA framework, "
2535 		    "passthrough to HBA", cmd);
2536 
2537 		if (sata_tran->sata_tran_ioctl == NULL) {
2538 			rv = EINVAL;
2539 			break;
2540 		}
2541 		rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg);
2542 		if (rval != 0) {
2543 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
2544 			    "IOCTL 0x%2x failed in HBA", cmd);
2545 			rv = rval;
2546 		}
2547 		break;
2548 	}
2549 
2550 	} /* End of main IOCTL switch */
2551 
2552 	if (dcp) {
2553 		ndi_dc_freehdl(dcp);
2554 	}
2555 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
2556 	cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
2557 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
2558 
2559 	return (rv);
2560 }
2561 
2562 
2563 /*
2564  * Create error retrieval sata packet
2565  *
2566  * A sata packet is allocated and set-up to contain specified error retrieval
2567  * command and appropriate dma-able data buffer.
2568  * No association with any scsi packet is made and no callback routine is
2569  * specified.
2570  *
2571  * Returns a pointer to sata packet upon successfull packet creation.
2572  * Returns NULL, if packet cannot be created.
2573  */
2574 sata_pkt_t *
2575 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device,
2576     int pkt_type)
2577 {
2578 	sata_hba_inst_t	*sata_hba_inst;
2579 	sata_pkt_txlate_t *spx;
2580 	sata_pkt_t *spkt;
2581 	sata_drive_info_t *sdinfo;
2582 
2583 	mutex_enter(&sata_mutex);
2584 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
2585 	    sata_hba_inst = sata_hba_inst->satahba_next) {
2586 		if (SATA_DIP(sata_hba_inst) == dip)
2587 			break;
2588 	}
2589 	mutex_exit(&sata_mutex);
2590 	ASSERT(sata_hba_inst != NULL);
2591 
2592 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
2593 	if (sdinfo == NULL) {
2594 		sata_log(sata_hba_inst, CE_WARN,
2595 		    "sata: error recovery request for non-attached device at "
2596 		    "cport %d", sata_device->satadev_addr.cport);
2597 		return (NULL);
2598 	}
2599 
2600 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
2601 	spx->txlt_sata_hba_inst = sata_hba_inst;
2602 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
2603 	spkt = sata_pkt_alloc(spx, NULL);
2604 	if (spkt == NULL) {
2605 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
2606 		return (NULL);
2607 	}
2608 	/* address is needed now */
2609 	spkt->satapkt_device.satadev_addr = sata_device->satadev_addr;
2610 
2611 	switch (pkt_type) {
2612 	case SATA_ERR_RETR_PKT_TYPE_NCQ:
2613 		/* Placeholder only */
2614 		break;
2615 
2616 	case SATA_ERR_RETR_PKT_TYPE_ATAPI:
2617 		if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS)
2618 			return (spkt);
2619 		break;
2620 
2621 	default:
2622 		break;
2623 	}
2624 
2625 	sata_pkt_free(spx);
2626 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
2627 	return (NULL);
2628 
2629 }
2630 
2631 
2632 /*
2633  * Free error retrieval sata packet
2634  *
2635  * Free sata packet and any associated resources allocated previously by
2636  * sata_get_error_retrieval_pkt().
2637  *
2638  * Void return.
2639  */
2640 void
2641 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt)
2642 {
2643 	sata_pkt_txlate_t *spx =
2644 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
2645 
2646 	ASSERT(sata_pkt != NULL);
2647 
2648 	sata_free_local_buffer(spx);
2649 	sata_pkt_free(spx);
2650 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
2651 
2652 }
2653 
2654 
2655 /* ****************** SCSA required entry points *********************** */
2656 
2657 /*
2658  * Implementation of scsi tran_tgt_init.
2659  * sata_scsi_tgt_init() initializes scsi_device structure
2660  *
2661  * If successful, DDI_SUCCESS is returned.
2662  * DDI_FAILURE is returned if addressed device does not exist
2663  */
2664 
2665 static int
2666 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2667     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2668 {
2669 #ifndef __lock_lint
2670 	_NOTE(ARGUNUSED(hba_dip))
2671 	_NOTE(ARGUNUSED(tgt_dip))
2672 #endif
2673 	sata_device_t		sata_device;
2674 	sata_drive_info_t	*sdinfo;
2675 	struct sata_id		*sid;
2676 	sata_hba_inst_t		*sata_hba_inst;
2677 	char			model[SATA_ID_MODEL_LEN + 1];
2678 	char			fw[SATA_ID_FW_LEN + 1];
2679 	char			*vid, *pid;
2680 	int			i;
2681 
2682 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2683 
2684 	/* Validate scsi device address */
2685 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2686 	    &sata_device) != 0)
2687 		return (DDI_FAILURE);
2688 
2689 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2690 	    sata_device.satadev_addr.cport)));
2691 
2692 	/* sata_device now contains a valid sata address */
2693 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2694 	if (sdinfo == NULL) {
2695 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2696 		    sata_device.satadev_addr.cport)));
2697 		return (DDI_FAILURE);
2698 	}
2699 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2700 	    sata_device.satadev_addr.cport)));
2701 
2702 	/*
2703 	 * Check if we need to create a legacy devid (i.e cmdk style) for
2704 	 * the target disks.
2705 	 *
2706 	 * HBA devinfo node will have the property "use-cmdk-devid-format"
2707 	 * if we need to create cmdk-style devid for all the disk devices
2708 	 * attached to this controller. This property may have been set
2709 	 * from HBA driver's .conf file or by the HBA driver in its
2710 	 * attach(9F) function.
2711 	 */
2712 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2713 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2714 	    "use-cmdk-devid-format", 0) == 1)) {
2715 		/* register a legacy devid for this target node */
2716 		sata_target_devid_register(tgt_dip, sdinfo);
2717 	}
2718 
2719 
2720 	/*
2721 	 * 'Identify Device Data' does not always fit in standard SCSI
2722 	 * INQUIRY data, so establish INQUIRY_* properties with full-form
2723 	 * of information.
2724 	 */
2725 	sid = &sdinfo->satadrv_id;
2726 #ifdef	_LITTLE_ENDIAN
2727 	swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
2728 	swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
2729 #else	/* _LITTLE_ENDIAN */
2730 	bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN);
2731 	bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN);
2732 #endif	/* _LITTLE_ENDIAN */
2733 	model[SATA_ID_MODEL_LEN] = 0;
2734 	fw[SATA_ID_FW_LEN] = 0;
2735 
2736 	/* split model into into vid/pid */
2737 	for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++)
2738 		if ((*pid == ' ') || (*pid == '\t'))
2739 			break;
2740 	if (i < SATA_ID_MODEL_LEN) {
2741 		vid = model;
2742 		*pid++ = 0;		/* terminate vid, establish pid */
2743 	} else {
2744 		vid = NULL;		/* vid will stay "ATA     " */
2745 		pid = model;		/* model is all pid */
2746 	}
2747 
2748 	if (vid)
2749 		(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_VENDOR_ID,
2750 		    vid, strlen(vid));
2751 	if (pid)
2752 		(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID,
2753 		    pid, strlen(pid));
2754 	(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_REVISION_ID,
2755 	    fw, strlen(fw));
2756 
2757 	return (DDI_SUCCESS);
2758 }
2759 
2760 /*
2761  * Implementation of scsi tran_tgt_probe.
2762  * Probe target, by calling default scsi routine scsi_hba_probe()
2763  */
2764 static int
2765 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void))
2766 {
2767 	sata_hba_inst_t *sata_hba_inst =
2768 	    (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private);
2769 	int rval;
2770 
2771 	rval = scsi_hba_probe(sd, callback);
2772 
2773 	if (rval == SCSIPROBE_EXISTS) {
2774 		/*
2775 		 * Set property "pm-capable" on the target device node, so that
2776 		 * the target driver will not try to fetch scsi cycle counters
2777 		 * before enabling device power-management.
2778 		 */
2779 		if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
2780 		    "pm-capable", 1)) != DDI_PROP_SUCCESS) {
2781 			sata_log(sata_hba_inst, CE_WARN,
2782 			    "SATA device at port %d: "
2783 			    "will not be power-managed ",
2784 			    SCSI_TO_SATA_CPORT(sd->sd_address.a_target));
2785 			SATA_LOG_D((sata_hba_inst, CE_WARN,
2786 			    "failure updating pm-capable property"));
2787 		}
2788 	}
2789 	return (rval);
2790 }
2791 
2792 /*
2793  * Implementation of scsi tran_tgt_free.
2794  * Release all resources allocated for scsi_device
2795  */
2796 static void
2797 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2798     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2799 {
2800 #ifndef __lock_lint
2801 	_NOTE(ARGUNUSED(hba_dip))
2802 #endif
2803 	sata_device_t		sata_device;
2804 	sata_drive_info_t	*sdinfo;
2805 	sata_hba_inst_t		*sata_hba_inst;
2806 	ddi_devid_t		devid;
2807 
2808 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2809 
2810 	/* Validate scsi device address */
2811 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2812 	    &sata_device) != 0)
2813 		return;
2814 
2815 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2816 	    sata_device.satadev_addr.cport)));
2817 
2818 	/* sata_device now should contain a valid sata address */
2819 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2820 	if (sdinfo == NULL) {
2821 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2822 		    sata_device.satadev_addr.cport)));
2823 		return;
2824 	}
2825 	/*
2826 	 * We did not allocate any resources in sata_scsi_tgt_init()
2827 	 * other than few properties.
2828 	 * Free them.
2829 	 */
2830 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2831 	    sata_device.satadev_addr.cport)));
2832 	if (ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable") !=
2833 	    DDI_PROP_SUCCESS)
2834 		SATA_LOG_D((sata_hba_inst, CE_WARN,
2835 		    "sata_scsi_tgt_free: pm-capable "
2836 		    "property could not be removed"));
2837 
2838 	/*
2839 	 * If devid was previously created but not freed up from
2840 	 * sd(7D) driver (i.e during detach(9F)) then do it here.
2841 	 */
2842 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2843 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2844 	    "use-cmdk-devid-format", 0) == 1) &&
2845 	    (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) {
2846 		ddi_devid_unregister(tgt_dip);
2847 		ddi_devid_free(devid);
2848 	}
2849 }
2850 
2851 /*
2852  * Implementation of scsi tran_init_pkt
2853  * Upon successful return, scsi pkt buffer has DMA resources allocated.
2854  *
2855  * It seems that we should always allocate pkt, even if the address is
2856  * for non-existing device - just use some default for dma_attr.
2857  * The reason is that there is no way to communicate this to a caller here.
2858  * Subsequent call to sata_scsi_start may fail appropriately.
2859  * Simply returning NULL does not seem to discourage a target driver...
2860  *
2861  * Returns a pointer to initialized scsi_pkt, or NULL otherwise.
2862  */
2863 static struct scsi_pkt *
2864 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
2865     struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
2866     int (*callback)(caddr_t), caddr_t arg)
2867 {
2868 	sata_hba_inst_t *sata_hba_inst =
2869 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2870 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
2871 	sata_device_t sata_device;
2872 	sata_drive_info_t *sdinfo;
2873 	sata_pkt_txlate_t *spx;
2874 	ddi_dma_attr_t cur_dma_attr;
2875 	int rval;
2876 	boolean_t new_pkt = TRUE;
2877 
2878 	ASSERT(ap->a_hba_tran->tran_hba_dip == dip);
2879 
2880 	/*
2881 	 * We need to translate the address, even if it could be
2882 	 * a bogus one, for a non-existing device
2883 	 */
2884 	sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
2885 	sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target);
2886 	sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2887 	sata_device.satadev_rev = SATA_DEVICE_REV;
2888 
2889 	if (pkt == NULL) {
2890 		/*
2891 		 * Have to allocate a brand new scsi packet.
2892 		 * We need to operate with auto request sense enabled.
2893 		 */
2894 		pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen,
2895 		    MAX(statuslen, sizeof (struct scsi_arq_status)),
2896 		    tgtlen, sizeof (sata_pkt_txlate_t), callback, arg);
2897 
2898 		if (pkt == NULL)
2899 			return (NULL);
2900 
2901 		/* Fill scsi packet structure */
2902 		pkt->pkt_comp		= (void (*)())NULL;
2903 		pkt->pkt_time		= 0;
2904 		pkt->pkt_resid		= 0;
2905 		pkt->pkt_statistics	= 0;
2906 		pkt->pkt_reason		= 0;
2907 
2908 		/*
2909 		 * pkt_hba_private will point to sata pkt txlate structure
2910 		 */
2911 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2912 		bzero(spx, sizeof (sata_pkt_txlate_t));
2913 
2914 		spx->txlt_scsi_pkt = pkt;
2915 		spx->txlt_sata_hba_inst = sata_hba_inst;
2916 
2917 		/* Allocate sata_pkt */
2918 		spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback);
2919 		if (spx->txlt_sata_pkt == NULL) {
2920 			/* Could not allocate sata pkt */
2921 			scsi_hba_pkt_free(ap, pkt);
2922 			return (NULL);
2923 		}
2924 		/* Set sata address */
2925 		spx->txlt_sata_pkt->satapkt_device.satadev_addr =
2926 		    sata_device.satadev_addr;
2927 		spx->txlt_sata_pkt->satapkt_device.satadev_rev =
2928 		    sata_device.satadev_rev;
2929 
2930 		if ((bp == NULL) || (bp->b_bcount == 0))
2931 			return (pkt);
2932 
2933 		spx->txlt_total_residue = bp->b_bcount;
2934 	} else {
2935 		new_pkt = FALSE;
2936 		/*
2937 		 * Packet was preallocated/initialized by previous call
2938 		 */
2939 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2940 
2941 		if ((bp == NULL) || (bp->b_bcount == 0)) {
2942 			return (pkt);
2943 		}
2944 		ASSERT(spx->txlt_buf_dma_handle != NULL);
2945 
2946 		/* Pkt is available already: spx->txlt_scsi_pkt == pkt; */
2947 	}
2948 
2949 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
2950 
2951 	/*
2952 	 * We use an adjusted version of the dma_attr, to account
2953 	 * for device addressing limitations.
2954 	 * sata_adjust_dma_attr() will handle sdinfo == NULL which may
2955 	 * happen when a device is not yet configured.
2956 	 */
2957 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2958 	    sata_device.satadev_addr.cport)));
2959 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2960 	    &spx->txlt_sata_pkt->satapkt_device);
2961 	/* NULL sdinfo may be passsed to sata_adjust_dma_attr() */
2962 	sata_adjust_dma_attr(sdinfo,
2963 	    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
2964 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2965 	    sata_device.satadev_addr.cport)));
2966 	/*
2967 	 * Allocate necessary DMA resources for the packet's data buffer
2968 	 * NOTE:
2969 	 * In case of read/write commands, DMA resource allocation here is
2970 	 * based on the premise that the transfer length specified in
2971 	 * the read/write scsi cdb will match exactly DMA resources -
2972 	 * returning correct packet residue is crucial.
2973 	 */
2974 	if ((rval = sata_dma_buf_setup(spx, flags, callback, arg,
2975 	    &cur_dma_attr)) != DDI_SUCCESS) {
2976 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2977 		sata_pkt_free(spx);
2978 		/*
2979 		 * If a DMA allocation request fails with
2980 		 * DDI_DMA_NOMAPPING, indicate the error by calling
2981 		 * bioerror(9F) with bp and an error code of EFAULT.
2982 		 * If a DMA allocation request fails with
2983 		 * DDI_DMA_TOOBIG, indicate the error by calling
2984 		 * bioerror(9F) with bp and an error code of EINVAL.
2985 		 */
2986 		switch (rval) {
2987 		case DDI_DMA_NORESOURCES:
2988 			bioerror(bp, 0);
2989 			break;
2990 		case DDI_DMA_NOMAPPING:
2991 		case DDI_DMA_BADATTR:
2992 			bioerror(bp, EFAULT);
2993 			break;
2994 		case DDI_DMA_TOOBIG:
2995 		default:
2996 			bioerror(bp, EINVAL);
2997 			break;
2998 		}
2999 		if (new_pkt == TRUE)
3000 			scsi_hba_pkt_free(ap, pkt);
3001 		return (NULL);
3002 	}
3003 	/* Set number of bytes that are not yet accounted for */
3004 	pkt->pkt_resid = spx->txlt_total_residue;
3005 	ASSERT(pkt->pkt_resid >= 0);
3006 
3007 	return (pkt);
3008 }
3009 
3010 /*
3011  * Implementation of scsi tran_start.
3012  * Translate scsi cmd into sata operation and return status.
3013  * Supported scsi commands:
3014  * SCMD_INQUIRY
3015  * SCMD_TEST_UNIT_READY
3016  * SCMD_START_STOP
3017  * SCMD_READ_CAPACITY
3018  * SCMD_REQUEST_SENSE
3019  * SCMD_LOG_SENSE_G1
3020  * SCMD_LOG_SELECT_G1
3021  * SCMD_MODE_SENSE	(specific pages)
3022  * SCMD_MODE_SENSE_G1	(specific pages)
3023  * SCMD_MODE_SELECT	(specific pages)
3024  * SCMD_MODE_SELECT_G1	(specific pages)
3025  * SCMD_SYNCHRONIZE_CACHE
3026  * SCMD_SYNCHRONIZE_CACHE_G1
3027  * SCMD_READ
3028  * SCMD_READ_G1
3029  * SCMD_READ_G4
3030  * SCMD_READ_G5
3031  * SCMD_WRITE
3032  * SCMD_WRITE_BUFFER
3033  * SCMD_WRITE_G1
3034  * SCMD_WRITE_G4
3035  * SCMD_WRITE_G5
3036  * SCMD_SEEK		(noop)
3037  * SCMD_SDIAG
3038  *
3039  * All other commands are rejected as unsupported.
3040  *
3041  * Returns:
3042  * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver
3043  * for execution. TRAN_ACCEPT may be returned also if device was removed but
3044  * a callback could be scheduled.
3045  * TRAN_BADPKT if cmd was directed to invalid address.
3046  * TRAN_FATAL_ERROR is command was rejected due to hardware error, including
3047  * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device
3048  * was removed and there was no callback specified in scsi pkt.
3049  * TRAN_BUSY if command could not be executed becasue HBA driver or SATA
3050  * framework was busy performing some other operation(s).
3051  *
3052  */
3053 static int
3054 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
3055 {
3056 	sata_hba_inst_t *sata_hba_inst =
3057 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
3058 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3059 	sata_drive_info_t *sdinfo;
3060 	struct buf *bp;
3061 	int cport;
3062 	int rval;
3063 
3064 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
3065 	    "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]);
3066 
3067 	ASSERT(spx != NULL &&
3068 	    spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL);
3069 
3070 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
3071 
3072 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
3073 	sdinfo = sata_get_device_info(sata_hba_inst,
3074 	    &spx->txlt_sata_pkt->satapkt_device);
3075 	if (sdinfo == NULL ||
3076 	    SATA_CPORT_INFO(sata_hba_inst, cport)->cport_tgtnode_clean ==
3077 	    B_FALSE) {
3078 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
3079 		pkt->pkt_reason = CMD_DEV_GONE;
3080 		/*
3081 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
3082 		 * only in callback function (for normal requests) and
3083 		 * in the dump code path.
3084 		 * So, if the callback is available, we need to do
3085 		 * the callback rather than returning TRAN_FATAL_ERROR here.
3086 		 */
3087 		if (pkt->pkt_comp != NULL) {
3088 			/* scsi callback required */
3089 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3090 			    (task_func_t *)pkt->pkt_comp,
3091 			    (void *)pkt, TQ_SLEEP) == NULL)
3092 				/* Scheduling the callback failed */
3093 				return (TRAN_BUSY);
3094 			return (TRAN_ACCEPT);
3095 		}
3096 		/* No callback available */
3097 		return (TRAN_FATAL_ERROR);
3098 	}
3099 
3100 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
3101 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
3102 		rval = sata_txlt_atapi(spx);
3103 		SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
3104 		    "sata_scsi_start atapi: rval %d\n", rval);
3105 		return (rval);
3106 	}
3107 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
3108 
3109 	/* ATA Disk commands processing starts here */
3110 
3111 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3112 
3113 	switch (pkt->pkt_cdbp[0]) {
3114 
3115 	case SCMD_INQUIRY:
3116 		/* Mapped to identify device */
3117 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
3118 			bp_mapin(bp);
3119 		rval = sata_txlt_inquiry(spx);
3120 		break;
3121 
3122 	case SCMD_TEST_UNIT_READY:
3123 		/*
3124 		 * SAT "SATA to ATA Translation" doc specifies translation
3125 		 * to ATA CHECK POWER MODE.
3126 		 */
3127 		rval = sata_txlt_test_unit_ready(spx);
3128 		break;
3129 
3130 	case SCMD_START_STOP:
3131 		/* Mapping depends on the command */
3132 		rval = sata_txlt_start_stop_unit(spx);
3133 		break;
3134 
3135 	case SCMD_READ_CAPACITY:
3136 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
3137 			bp_mapin(bp);
3138 		rval = sata_txlt_read_capacity(spx);
3139 		break;
3140 
3141 	case SCMD_REQUEST_SENSE:
3142 		/*
3143 		 * Always No Sense, since we force ARQ
3144 		 */
3145 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
3146 			bp_mapin(bp);
3147 		rval = sata_txlt_request_sense(spx);
3148 		break;
3149 
3150 	case SCMD_LOG_SENSE_G1:
3151 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
3152 			bp_mapin(bp);
3153 		rval = sata_txlt_log_sense(spx);
3154 		break;
3155 
3156 	case SCMD_LOG_SELECT_G1:
3157 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
3158 			bp_mapin(bp);
3159 		rval = sata_txlt_log_select(spx);
3160 		break;
3161 
3162 	case SCMD_MODE_SENSE:
3163 	case SCMD_MODE_SENSE_G1:
3164 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
3165 			bp_mapin(bp);
3166 		rval = sata_txlt_mode_sense(spx);
3167 		break;
3168 
3169 
3170 	case SCMD_MODE_SELECT:
3171 	case SCMD_MODE_SELECT_G1:
3172 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
3173 			bp_mapin(bp);
3174 		rval = sata_txlt_mode_select(spx);
3175 		break;
3176 
3177 	case SCMD_SYNCHRONIZE_CACHE:
3178 	case SCMD_SYNCHRONIZE_CACHE_G1:
3179 		rval = sata_txlt_synchronize_cache(spx);
3180 		break;
3181 
3182 	case SCMD_READ:
3183 	case SCMD_READ_G1:
3184 	case SCMD_READ_G4:
3185 	case SCMD_READ_G5:
3186 		rval = sata_txlt_read(spx);
3187 		break;
3188 	case SCMD_WRITE_BUFFER:
3189 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
3190 			bp_mapin(bp);
3191 		rval = sata_txlt_write_buffer(spx);
3192 		break;
3193 
3194 	case SCMD_WRITE:
3195 	case SCMD_WRITE_G1:
3196 	case SCMD_WRITE_G4:
3197 	case SCMD_WRITE_G5:
3198 		rval = sata_txlt_write(spx);
3199 		break;
3200 
3201 	case SCMD_SEEK:
3202 		rval = sata_txlt_nodata_cmd_immediate(spx);
3203 		break;
3204 
3205 		/* Other cases will be filed later */
3206 		/* postponed until phase 2 of the development */
3207 	default:
3208 		rval = sata_txlt_invalid_command(spx);
3209 		break;
3210 	}
3211 
3212 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
3213 	    "sata_scsi_start: rval %d\n", rval);
3214 
3215 	return (rval);
3216 }
3217 
3218 /*
3219  * Implementation of scsi tran_abort.
3220  * Abort specific pkt or all packets.
3221  *
3222  * Returns 1 if one or more packets were aborted, returns 0 otherwise
3223  *
3224  * May be called from an interrupt level.
3225  */
3226 static int
3227 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt)
3228 {
3229 	sata_hba_inst_t *sata_hba_inst =
3230 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
3231 	sata_device_t	sata_device;
3232 	sata_pkt_t	*sata_pkt;
3233 
3234 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
3235 	    "sata_scsi_abort: %s at target: 0x%x\n",
3236 	    scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target);
3237 
3238 	/* Validate address */
3239 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0)
3240 		/* Invalid address */
3241 		return (0);
3242 
3243 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
3244 	    sata_device.satadev_addr.cport)));
3245 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
3246 		/* invalid address */
3247 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3248 		    sata_device.satadev_addr.cport)));
3249 		return (0);
3250 	}
3251 	if (scsi_pkt == NULL) {
3252 		/*
3253 		 * Abort all packets.
3254 		 * Although we do not have specific packet, we still need
3255 		 * dummy packet structure to pass device address to HBA.
3256 		 * Allocate one, without sleeping. Fail if pkt cannot be
3257 		 * allocated.
3258 		 */
3259 		sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP);
3260 		if (sata_pkt == NULL) {
3261 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3262 			    sata_device.satadev_addr.cport)));
3263 			SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: "
3264 			    "could not allocate sata_pkt"));
3265 			return (0);
3266 		}
3267 		sata_pkt->satapkt_rev = SATA_PKT_REV;
3268 		sata_pkt->satapkt_device = sata_device;
3269 		sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
3270 	} else {
3271 		if (scsi_pkt->pkt_ha_private == NULL) {
3272 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3273 			    sata_device.satadev_addr.cport)));
3274 			return (0); /* Bad scsi pkt */
3275 		}
3276 		/* extract pointer to sata pkt */
3277 		sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)->
3278 		    txlt_sata_pkt;
3279 	}
3280 
3281 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3282 	    sata_device.satadev_addr.cport)));
3283 	/* Send abort request to HBA */
3284 	if ((*SATA_ABORT_FUNC(sata_hba_inst))
3285 	    (SATA_DIP(sata_hba_inst), sata_pkt,
3286 	    scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) ==
3287 	    SATA_SUCCESS) {
3288 		if (scsi_pkt == NULL)
3289 			kmem_free(sata_pkt, sizeof (sata_pkt_t));
3290 		/* Success */
3291 		return (1);
3292 	}
3293 	/* Else, something did not go right */
3294 	if (scsi_pkt == NULL)
3295 		kmem_free(sata_pkt, sizeof (sata_pkt_t));
3296 	/* Failure */
3297 	return (0);
3298 }
3299 
3300 
3301 /*
3302  * Implementation of scsi tran_reset.
3303  * RESET_ALL request is translated into port reset.
3304  * RESET_TARGET requests is translated into a device reset,
3305  * RESET_LUN request is accepted only for LUN 0 and translated into
3306  * device reset.
3307  * The target reset should cause all HBA active and queued packets to
3308  * be terminated and returned with pkt reason SATA_PKT_RESET prior to
3309  * the return. HBA should report reset event for the device.
3310  *
3311  * Returns 1 upon success, 0 upon failure.
3312  */
3313 static int
3314 sata_scsi_reset(struct scsi_address *ap, int level)
3315 {
3316 	sata_hba_inst_t	*sata_hba_inst =
3317 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
3318 	sata_device_t	sata_device;
3319 	int		val;
3320 
3321 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
3322 	    "sata_scsi_reset: level %d target: 0x%x\n",
3323 	    level, ap->a_target);
3324 
3325 	/* Validate address */
3326 	val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device);
3327 	if (val == -1)
3328 		/* Invalid address */
3329 		return (0);
3330 
3331 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
3332 	    sata_device.satadev_addr.cport)));
3333 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
3334 		/* invalid address */
3335 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3336 		    sata_device.satadev_addr.cport)));
3337 		return (0);
3338 	}
3339 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3340 	    sata_device.satadev_addr.cport)));
3341 	if (level == RESET_ALL) {
3342 		/* port reset - cport only */
3343 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
3344 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
3345 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
3346 			return (1);
3347 		else
3348 			return (0);
3349 
3350 	} else if (val == 0 &&
3351 	    (level == RESET_TARGET || level == RESET_LUN)) {
3352 		/* reset device (device attached) */
3353 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
3354 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
3355 			return (1);
3356 		else
3357 			return (0);
3358 	}
3359 	return (0);
3360 }
3361 
3362 
3363 /*
3364  * Implementation of scsi tran_getcap (get transport/device capabilities).
3365  * Supported capabilities for SATA hard disks:
3366  * auto-rqsense		(always supported)
3367  * tagged-qing		(supported if HBA supports it)
3368  * untagged-qing	(could be supported if disk supports it, but because
3369  *			 caching behavior allowing untagged queuing actually
3370  *			 results in reduced performance.  sd tries to throttle
3371  *			 back to only 3 outstanding commands, which may
3372  *			 work for real SCSI disks, but with read ahead
3373  *			 caching, having more than 1 outstanding command
3374  *			 results in cache thrashing.)
3375  * sector_size
3376  * dma_max
3377  * interconnect-type	(INTERCONNECT_SATA)
3378  *
3379  * Supported capabilities for ATAPI devices (CD/DVD):
3380  * auto-rqsense		(always supported)
3381  * sector_size
3382  * dma_max
3383  * interconnect-type	(INTERCONNECT_SATA)
3384  *
3385  * Request for other capabilities is rejected as unsupported.
3386  *
3387  * Returns supported capability value, or -1 if capability is unsuppported or
3388  * the address is invalid - no device.
3389  */
3390 
3391 static int
3392 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
3393 {
3394 
3395 	sata_hba_inst_t 	*sata_hba_inst =
3396 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
3397 	sata_device_t		sata_device;
3398 	sata_drive_info_t	*sdinfo;
3399 	ddi_dma_attr_t		adj_dma_attr;
3400 	int 			rval;
3401 
3402 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
3403 	    "sata_scsi_getcap: target: 0x%x, cap: %s\n",
3404 	    ap->a_target, cap);
3405 
3406 	/*
3407 	 * We want to process the capabilities on per port granularity.
3408 	 * So, we are specifically restricting ourselves to whom != 0
3409 	 * to exclude the controller wide handling.
3410 	 */
3411 	if (cap == NULL || whom == 0)
3412 		return (-1);
3413 
3414 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
3415 		/* Invalid address */
3416 		return (-1);
3417 	}
3418 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
3419 	    sata_device.satadev_addr.cport)));
3420 	if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) ==
3421 	    NULL) {
3422 		/* invalid address */
3423 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3424 		    sata_device.satadev_addr.cport)));
3425 		return (-1);
3426 	}
3427 
3428 	switch (scsi_hba_lookup_capstr(cap)) {
3429 	case SCSI_CAP_ARQ:
3430 		rval = 1;		/* ARQ supported, turned on */
3431 		break;
3432 
3433 	case SCSI_CAP_SECTOR_SIZE:
3434 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK)
3435 			rval = SATA_DISK_SECTOR_SIZE;	/* fixed size */
3436 		else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD)
3437 			rval = SATA_ATAPI_SECTOR_SIZE;
3438 		else rval = -1;
3439 		break;
3440 
3441 	/*
3442 	 * untagged queuing cause a performance inversion because of
3443 	 * the way sd operates.  Because of this reason we do not
3444 	 * use it when available.
3445 	 */
3446 	case SCSI_CAP_UNTAGGED_QING:
3447 		if (sdinfo->satadrv_features_enabled &
3448 		    SATA_DEV_F_E_UNTAGGED_QING)
3449 			rval = 1;	/* Untagged queuing available */
3450 		else
3451 			rval = -1;	/* Untagged queuing not available */
3452 		break;
3453 
3454 	case SCSI_CAP_TAGGED_QING:
3455 		if (sdinfo->satadrv_features_enabled & SATA_DEV_F_E_TAGGED_QING)
3456 			rval = 1;	/* Tagged queuing available */
3457 		else
3458 			rval = -1;	/* Tagged queuing not available */
3459 		break;
3460 
3461 	case SCSI_CAP_DMA_MAX:
3462 		sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst),
3463 		    &adj_dma_attr);
3464 		rval = (int)adj_dma_attr.dma_attr_maxxfer;
3465 		/* We rely on the fact that dma_attr_maxxfer < 0x80000000 */
3466 		break;
3467 
3468 	case SCSI_CAP_INTERCONNECT_TYPE:
3469 		rval = INTERCONNECT_SATA;	/* SATA interconnect type */
3470 		break;
3471 
3472 	default:
3473 		rval = -1;
3474 		break;
3475 	}
3476 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3477 	    sata_device.satadev_addr.cport)));
3478 	return (rval);
3479 }
3480 
3481 /*
3482  * Implementation of scsi tran_setcap
3483  *
3484  * Only SCSI_CAP_UNTAGGED_QING and  SCSI_CAP_TAGGED_QING are changeable.
3485  *
3486  */
3487 static int
3488 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
3489 {
3490 	sata_hba_inst_t	*sata_hba_inst =
3491 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
3492 	sata_device_t	sata_device;
3493 	sata_drive_info_t	*sdinfo;
3494 	int		rval;
3495 
3496 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
3497 	    "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap);
3498 
3499 	/*
3500 	 * We want to process the capabilities on per port granularity.
3501 	 * So, we are specifically restricting ourselves to whom != 0
3502 	 * to exclude the controller wide handling.
3503 	 */
3504 	if (cap == NULL || whom == 0) {
3505 		return (-1);
3506 	}
3507 
3508 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
3509 		/* Invalid address */
3510 		return (-1);
3511 	}
3512 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
3513 	    sata_device.satadev_addr.cport)));
3514 	if ((sdinfo = sata_get_device_info(sata_hba_inst,
3515 	    &sata_device)) == NULL) {
3516 		/* invalid address */
3517 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3518 		    sata_device.satadev_addr.cport)));
3519 		return (-1);
3520 	}
3521 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3522 	    sata_device.satadev_addr.cport)));
3523 
3524 	switch (scsi_hba_lookup_capstr(cap)) {
3525 	case SCSI_CAP_ARQ:
3526 	case SCSI_CAP_SECTOR_SIZE:
3527 	case SCSI_CAP_DMA_MAX:
3528 	case SCSI_CAP_INTERCONNECT_TYPE:
3529 		rval = 0;
3530 		break;
3531 	case SCSI_CAP_UNTAGGED_QING:
3532 		if (SATA_QDEPTH(sata_hba_inst) > 1) {
3533 			rval = 1;
3534 			if (value == 1) {
3535 				sdinfo->satadrv_features_enabled |=
3536 				    SATA_DEV_F_E_UNTAGGED_QING;
3537 			} else if (value == 0) {
3538 				sdinfo->satadrv_features_enabled &=
3539 				    ~SATA_DEV_F_E_UNTAGGED_QING;
3540 			} else {
3541 				rval = -1;
3542 			}
3543 		} else {
3544 			rval = 0;
3545 		}
3546 		break;
3547 	case SCSI_CAP_TAGGED_QING:
3548 		/* This can TCQ or NCQ */
3549 		if (sata_func_enable & SATA_ENABLE_QUEUING &&
3550 		    ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ &&
3551 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) ||
3552 		    (sata_func_enable & SATA_ENABLE_NCQ &&
3553 		    sdinfo->satadrv_features_support & SATA_DEV_F_NCQ &&
3554 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ))) {
3555 			rval = 1;
3556 			if (value == 1) {
3557 				sdinfo->satadrv_features_enabled |=
3558 				    SATA_DEV_F_E_TAGGED_QING;
3559 			} else if (value == 0) {
3560 				sdinfo->satadrv_features_enabled &=
3561 				    ~SATA_DEV_F_E_TAGGED_QING;
3562 			} else {
3563 				rval = -1;
3564 			}
3565 		} else {
3566 			rval = 0;
3567 		}
3568 		break;
3569 	default:
3570 		rval = -1;
3571 		break;
3572 	}
3573 	return (rval);
3574 }
3575 
3576 /*
3577  * Implementations of scsi tran_destroy_pkt.
3578  * Free resources allocated by sata_scsi_init_pkt()
3579  */
3580 static void
3581 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3582 {
3583 	sata_pkt_txlate_t *spx;
3584 
3585 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3586 
3587 	if (spx->txlt_buf_dma_handle != NULL) {
3588 		if (spx->txlt_tmp_buf != NULL)  {
3589 			ASSERT(spx->txlt_tmp_buf_handle != 0);
3590 			/*
3591 			 * Intermediate DMA buffer was allocated.
3592 			 * Free allocated buffer and associated access handle.
3593 			 */
3594 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
3595 			spx->txlt_tmp_buf = NULL;
3596 		}
3597 		/*
3598 		 * Free DMA resources - cookies and handles
3599 		 */
3600 		if (spx->txlt_dma_cookie_list != NULL) {
3601 			if (spx->txlt_dma_cookie_list !=
3602 			    &spx->txlt_dma_cookie) {
3603 				(void) kmem_free(spx->txlt_dma_cookie_list,
3604 				    spx->txlt_dma_cookie_list_len *
3605 				    sizeof (ddi_dma_cookie_t));
3606 				spx->txlt_dma_cookie_list = NULL;
3607 			}
3608 		}
3609 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
3610 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
3611 	}
3612 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
3613 	sata_pkt_free(spx);
3614 
3615 	scsi_hba_pkt_free(ap, pkt);
3616 }
3617 
3618 /*
3619  * Implementation of scsi tran_dmafree.
3620  * Free DMA resources allocated by sata_scsi_init_pkt()
3621  */
3622 
3623 static void
3624 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
3625 {
3626 #ifndef __lock_lint
3627 	_NOTE(ARGUNUSED(ap))
3628 #endif
3629 	sata_pkt_txlate_t *spx;
3630 
3631 	ASSERT(pkt != NULL);
3632 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3633 
3634 	if (spx->txlt_buf_dma_handle != NULL) {
3635 		if (spx->txlt_tmp_buf != NULL)  {
3636 			/*
3637 			 * Intermediate DMA buffer was allocated.
3638 			 * Free allocated buffer and associated access handle.
3639 			 */
3640 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
3641 			spx->txlt_tmp_buf = NULL;
3642 		}
3643 		/*
3644 		 * Free DMA resources - cookies and handles
3645 		 */
3646 		/* ASSERT(spx->txlt_dma_cookie_list != NULL); */
3647 		if (spx->txlt_dma_cookie_list != NULL) {
3648 			if (spx->txlt_dma_cookie_list !=
3649 			    &spx->txlt_dma_cookie) {
3650 				(void) kmem_free(spx->txlt_dma_cookie_list,
3651 				    spx->txlt_dma_cookie_list_len *
3652 				    sizeof (ddi_dma_cookie_t));
3653 				spx->txlt_dma_cookie_list = NULL;
3654 			}
3655 		}
3656 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
3657 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
3658 		spx->txlt_buf_dma_handle = NULL;
3659 	}
3660 }
3661 
3662 /*
3663  * Implementation of scsi tran_sync_pkt.
3664  *
3665  * The assumption below is that pkt is unique - there is no need to check ap
3666  *
3667  * Synchronize DMA buffer and, if the intermediate buffer is used, copy data
3668  * into/from the real buffer.
3669  */
3670 static void
3671 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3672 {
3673 #ifndef __lock_lint
3674 	_NOTE(ARGUNUSED(ap))
3675 #endif
3676 	int rval;
3677 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3678 	struct buf *bp;
3679 	int direction;
3680 
3681 	ASSERT(spx != NULL);
3682 	if (spx->txlt_buf_dma_handle != NULL) {
3683 		direction = spx->txlt_sata_pkt->
3684 		    satapkt_cmd.satacmd_flags.sata_data_direction;
3685 		if (spx->txlt_sata_pkt != NULL &&
3686 		    direction != SATA_DIR_NODATA_XFER) {
3687 			if (spx->txlt_tmp_buf != NULL) {
3688 				/* Intermediate DMA buffer used */
3689 				bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3690 
3691 				if (direction & SATA_DIR_WRITE) {
3692 					bcopy(bp->b_un.b_addr,
3693 					    spx->txlt_tmp_buf, bp->b_bcount);
3694 				}
3695 			}
3696 			/* Sync the buffer for device or for CPU */
3697 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle,   0, 0,
3698 			    (direction & SATA_DIR_WRITE) ?
3699 			    DDI_DMA_SYNC_FORDEV :  DDI_DMA_SYNC_FORCPU);
3700 			ASSERT(rval == DDI_SUCCESS);
3701 			if (spx->txlt_tmp_buf != NULL &&
3702 			    !(direction & SATA_DIR_WRITE)) {
3703 				/* Intermediate DMA buffer used for read */
3704 				bcopy(spx->txlt_tmp_buf,
3705 				    bp->b_un.b_addr, bp->b_bcount);
3706 			}
3707 
3708 		}
3709 	}
3710 }
3711 
3712 
3713 
3714 /* *******************  SATA - SCSI Translation functions **************** */
3715 /*
3716  * SCSI to SATA pkt and command translation and SATA to SCSI status/error
3717  * translation.
3718  */
3719 
3720 /*
3721  * Checks if a device exists and can be access and translates common
3722  * scsi_pkt data to sata_pkt data.
3723  *
3724  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and
3725  * sata_pkt was set-up.
3726  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not
3727  * exist and pkt_comp callback was scheduled.
3728  * Returns other TRAN_XXXXX values when error occured and command should be
3729  * rejected with the returned TRAN_XXXXX value.
3730  *
3731  * This function should be called with port mutex held.
3732  */
3733 static int
3734 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx)
3735 {
3736 	sata_drive_info_t *sdinfo;
3737 	sata_device_t sata_device;
3738 	const struct sata_cmd_flags sata_initial_cmd_flags = {
3739 		SATA_DIR_NODATA_XFER,
3740 		/* all other values to 0/FALSE */
3741 	};
3742 	/*
3743 	 * Pkt_reason has to be set if the pkt_comp callback is invoked,
3744 	 * and that implies TRAN_ACCEPT return value. Any other returned value
3745 	 * indicates that the scsi packet was not accepted (the reason will not
3746 	 * be checked by the scsi traget driver).
3747 	 * To make debugging easier, we set pkt_reason to know value here.
3748 	 * It may be changed later when different completion reason is
3749 	 * determined.
3750 	 */
3751 	spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
3752 
3753 	/* Validate address */
3754 	switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst,
3755 	    &spx->txlt_scsi_pkt->pkt_address, &sata_device)) {
3756 
3757 	case -1:
3758 		/* Invalid address or invalid device type */
3759 		return (TRAN_BADPKT);
3760 	case 1:
3761 		/* valid address but no device - it has disappeared ? */
3762 		spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE;
3763 		/*
3764 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
3765 		 * only in callback function (for normal requests) and
3766 		 * in the dump code path.
3767 		 * So, if the callback is available, we need to do
3768 		 * the callback rather than returning TRAN_FATAL_ERROR here.
3769 		 */
3770 		if (spx->txlt_scsi_pkt->pkt_comp != NULL) {
3771 			/* scsi callback required */
3772 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3773 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3774 			    (void *)spx->txlt_scsi_pkt,
3775 			    TQ_SLEEP) == NULL)
3776 				/* Scheduling the callback failed */
3777 				return (TRAN_BUSY);
3778 
3779 			return (TRAN_ACCEPT);
3780 		}
3781 		return (TRAN_FATAL_ERROR);
3782 	default:
3783 		/* all OK */
3784 		break;
3785 	}
3786 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3787 	    &spx->txlt_sata_pkt->satapkt_device);
3788 
3789 	/*
3790 	 * If device is in reset condition, reject the packet with
3791 	 * TRAN_BUSY, unless:
3792 	 * 1. system is panicking (dumping)
3793 	 * In such case only one thread is running and there is no way to
3794 	 * process reset.
3795 	 * 2. cfgadm operation is is progress (internal APCTL lock is set)
3796 	 * Some cfgadm operations involve drive commands, so reset condition
3797 	 * needs to be ignored for IOCTL operations.
3798 	 */
3799 	if ((sdinfo->satadrv_event_flags &
3800 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3801 
3802 		if (!ddi_in_panic() &&
3803 		    ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst,
3804 		    sata_device.satadev_addr.cport) &
3805 		    SATA_APCTL_LOCK_PORT_BUSY) == 0)) {
3806 			spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3807 			SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3808 			    "sata_scsi_start: rejecting command because "
3809 			    "of device reset state\n", NULL);
3810 			return (TRAN_BUSY);
3811 		}
3812 	}
3813 
3814 	/*
3815 	 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by
3816 	 * sata_scsi_pkt_init() because pkt init had to work also with
3817 	 * non-existing devices.
3818 	 * Now we know that the packet was set-up for a real device, so its
3819 	 * type is known.
3820 	 */
3821 	spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type;
3822 
3823 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags;
3824 	if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst,
3825 	    sata_device.satadev_addr.cport)->cport_event_flags &
3826 	    SATA_APCTL_LOCK_PORT_BUSY) != 0) {
3827 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3828 		    sata_ignore_dev_reset = B_TRUE;
3829 	}
3830 	/*
3831 	 * At this point the generic translation routine determined that the
3832 	 * scsi packet should be accepted. Packet completion reason may be
3833 	 * changed later when a different completion reason is determined.
3834 	 */
3835 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3836 
3837 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
3838 		/* Synchronous execution */
3839 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH |
3840 		    SATA_OPMODE_POLLING;
3841 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3842 		    sata_ignore_dev_reset = ddi_in_panic();
3843 	} else {
3844 		/* Asynchronous execution */
3845 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH |
3846 		    SATA_OPMODE_INTERRUPTS;
3847 	}
3848 	/* Convert queuing information */
3849 	if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG)
3850 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag =
3851 		    B_TRUE;
3852 	else if (spx->txlt_scsi_pkt->pkt_flags &
3853 	    (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD))
3854 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag =
3855 		    B_TRUE;
3856 
3857 	/* Always limit pkt time */
3858 	if (spx->txlt_scsi_pkt->pkt_time == 0)
3859 		spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time;
3860 	else
3861 		/* Pass on scsi_pkt time */
3862 		spx->txlt_sata_pkt->satapkt_time =
3863 		    spx->txlt_scsi_pkt->pkt_time;
3864 
3865 	return (TRAN_ACCEPT);
3866 }
3867 
3868 
3869 /*
3870  * Translate ATA Identify Device data to SCSI Inquiry data.
3871  * This function may be called only for ATA devices.
3872  * This function should not be called for ATAPI devices - they
3873  * respond directly to SCSI Inquiry command.
3874  *
3875  * SATA Identify Device data has to be valid in sata_rive_info.
3876  * Buffer has to accomodate the inquiry length (36 bytes).
3877  *
3878  * This function should be called with a port mutex held.
3879  */
3880 static	void
3881 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst,
3882     sata_drive_info_t *sdinfo, uint8_t *buf)
3883 {
3884 
3885 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
3886 	struct sata_id *sid = &sdinfo->satadrv_id;
3887 
3888 	/* Start with a nice clean slate */
3889 	bzero((void *)inq, sizeof (struct scsi_inquiry));
3890 
3891 	/*
3892 	 * Rely on the dev_type for setting paripheral qualifier.
3893 	 * Assume that  DTYPE_RODIRECT applies to CD/DVD R/W devices.
3894 	 * It could be that DTYPE_OPTICAL could also qualify in the future.
3895 	 * ATAPI Inquiry may provide more data to the target driver.
3896 	 */
3897 	inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3898 	    DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */
3899 
3900 	inq->inq_rmb = sid->ai_config & SATA_REM_MEDIA ? 1 : 0;
3901 	inq->inq_qual = 0;	/* Device type qualifier (obsolete in SCSI3? */
3902 	inq->inq_iso = 0;	/* ISO version */
3903 	inq->inq_ecma = 0;	/* ECMA version */
3904 	inq->inq_ansi = 3;	/* ANSI version - SCSI 3 */
3905 	inq->inq_aenc = 0;	/* Async event notification cap. */
3906 	inq->inq_trmiop = 0;	/* Supports TERMINATE I/O PROC msg - NO */
3907 	inq->inq_normaca = 0;	/* setting NACA bit supported - NO */
3908 	inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */
3909 	inq->inq_len = 31;	/* Additional length */
3910 	inq->inq_dualp = 0;	/* dual port device - NO */
3911 	inq->inq_reladdr = 0;	/* Supports relative addressing - NO */
3912 	inq->inq_sync = 0;	/* Supports synchronous data xfers - NO */
3913 	inq->inq_linked = 0;	/* Supports linked commands - NO */
3914 				/*
3915 				 * Queuing support - controller has to
3916 				 * support some sort of command queuing.
3917 				 */
3918 	if (SATA_QDEPTH(sata_hba_inst) > 1)
3919 		inq->inq_cmdque = 1; /* Supports command queueing - YES */
3920 	else
3921 		inq->inq_cmdque = 0; /* Supports command queueing - NO */
3922 	inq->inq_sftre = 0;	/* Supports Soft Reset option - NO ??? */
3923 	inq->inq_wbus32 = 0;	/* Supports 32 bit wide data xfers - NO */
3924 	inq->inq_wbus16 = 0;	/* Supports 16 bit wide data xfers - NO */
3925 
3926 #ifdef	_LITTLE_ENDIAN
3927 	/* Swap text fields to match SCSI format */
3928 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3929 	swab(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3930 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3931 		swab(sid->ai_fw, inq->inq_revision, 4);	/* Revision level */
3932 	else
3933 		swab(&sid->ai_fw[4], inq->inq_revision, 4);	/* Rev. level */
3934 #else	/* _LITTLE_ENDIAN */
3935 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3936 	bcopy(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3937 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3938 		bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
3939 	else
3940 		bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
3941 #endif	/* _LITTLE_ENDIAN */
3942 }
3943 
3944 
3945 /*
3946  * Scsi response set up for invalid command (command not supported)
3947  *
3948  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3949  */
3950 static int
3951 sata_txlt_invalid_command(sata_pkt_txlate_t *spx)
3952 {
3953 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3954 	struct scsi_extended_sense *sense;
3955 
3956 	scsipkt->pkt_reason = CMD_CMPLT;
3957 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3958 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3959 
3960 	*scsipkt->pkt_scbp = STATUS_CHECK;
3961 
3962 	sense = sata_arq_sense(spx);
3963 	sense->es_key = KEY_ILLEGAL_REQUEST;
3964 	sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE;
3965 
3966 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3967 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3968 
3969 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3970 	    scsipkt->pkt_comp != NULL)
3971 		/* scsi callback required */
3972 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3973 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3974 		    (void *)spx->txlt_scsi_pkt,
3975 		    TQ_SLEEP) == NULL)
3976 			/* Scheduling the callback failed */
3977 			return (TRAN_BUSY);
3978 	return (TRAN_ACCEPT);
3979 }
3980 
3981 /*
3982  * Scsi response setup for
3983  * emulated non-data command that requires no action/return data
3984  *
3985  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3986  */
3987 static 	int
3988 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx)
3989 {
3990 	int rval;
3991 
3992 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3993 
3994 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
3995 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
3996 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3997 		return (rval);
3998 	}
3999 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4000 
4001 	spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4002 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4003 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
4004 	*(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD;
4005 
4006 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4007 	    "Scsi_pkt completion reason %x\n",
4008 	    spx->txlt_scsi_pkt->pkt_reason);
4009 
4010 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 &&
4011 	    spx->txlt_scsi_pkt->pkt_comp != NULL)
4012 		/* scsi callback required */
4013 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4014 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4015 		    (void *)spx->txlt_scsi_pkt,
4016 		    TQ_SLEEP) == NULL)
4017 			/* Scheduling the callback failed */
4018 			return (TRAN_BUSY);
4019 	return (TRAN_ACCEPT);
4020 }
4021 
4022 
4023 /*
4024  * SATA translate command: Inquiry / Identify Device
4025  * Use cached Identify Device data for now, rather than issuing actual
4026  * Device Identify cmd request. If device is detached and re-attached,
4027  * asynchromous event processing should fetch and refresh Identify Device
4028  * data.
4029  * Two VPD pages are supported now:
4030  * Vital Product Data page
4031  * Unit Serial Number page
4032  *
4033  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4034  */
4035 
4036 #define	EVPD			1	/* Extended Vital Product Data flag */
4037 #define	CMDDT			2	/* Command Support Data - Obsolete */
4038 #define	INQUIRY_SUP_VPD_PAGE	0	/* Supported VDP Pages Page COde */
4039 #define	INQUIRY_USN_PAGE	0x80	/* Unit Serial Number Page Code */
4040 #define	INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */
4041 
4042 static int
4043 sata_txlt_inquiry(sata_pkt_txlate_t *spx)
4044 {
4045 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4046 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4047 	sata_drive_info_t *sdinfo;
4048 	struct scsi_extended_sense *sense;
4049 	int count;
4050 	uint8_t *p;
4051 	int i, j;
4052 	uint8_t page_buf[0xff]; /* Max length */
4053 	int rval;
4054 
4055 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4056 
4057 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
4058 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
4059 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4060 		return (rval);
4061 	}
4062 
4063 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4064 	    &spx->txlt_sata_pkt->satapkt_device);
4065 
4066 	ASSERT(sdinfo != NULL);
4067 
4068 	scsipkt->pkt_reason = CMD_CMPLT;
4069 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4070 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4071 
4072 	/* Reject not supported request */
4073 	if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */
4074 		*scsipkt->pkt_scbp = STATUS_CHECK;
4075 		sense = sata_arq_sense(spx);
4076 		sense->es_key = KEY_ILLEGAL_REQUEST;
4077 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4078 		goto done;
4079 	}
4080 
4081 	/* Valid Inquiry request */
4082 	*scsipkt->pkt_scbp = STATUS_GOOD;
4083 
4084 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4085 
4086 		/*
4087 		 * Because it is fully emulated command storing data
4088 		 * programatically in the specified buffer, release
4089 		 * preallocated DMA resources before storing data in the buffer,
4090 		 * so no unwanted DMA sync would take place.
4091 		 */
4092 		sata_scsi_dmafree(NULL, scsipkt);
4093 
4094 		if (!(scsipkt->pkt_cdbp[1] & EVPD)) {
4095 			/* Standard Inquiry Data request */
4096 			struct scsi_inquiry inq;
4097 			unsigned int bufsize;
4098 
4099 			sata_identdev_to_inquiry(spx->txlt_sata_hba_inst,
4100 			    sdinfo, (uint8_t *)&inq);
4101 			/* Copy no more than requested */
4102 			count = MIN(bp->b_bcount,
4103 			    sizeof (struct scsi_inquiry));
4104 			bufsize = scsipkt->pkt_cdbp[4];
4105 			bufsize |= scsipkt->pkt_cdbp[3] << 8;
4106 			count = MIN(count, bufsize);
4107 			bcopy(&inq, bp->b_un.b_addr, count);
4108 
4109 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
4110 			scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
4111 			    bufsize - count : 0;
4112 		} else {
4113 			/*
4114 			 * peripheral_qualifier = 0;
4115 			 *
4116 			 * We are dealing only with HD and will be
4117 			 * dealing with CD/DVD devices soon
4118 			 */
4119 			uint8_t peripheral_device_type =
4120 			    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
4121 			    DTYPE_DIRECT : DTYPE_RODIRECT;
4122 
4123 			switch ((uint_t)scsipkt->pkt_cdbp[2]) {
4124 			case INQUIRY_SUP_VPD_PAGE:
4125 				/*
4126 				 * Request for suported Vital Product Data
4127 				 * pages - assuming only 2 page codes
4128 				 * supported
4129 				 */
4130 				page_buf[0] = peripheral_device_type;
4131 				page_buf[1] = INQUIRY_SUP_VPD_PAGE;
4132 				page_buf[2] = 0;
4133 				page_buf[3] = 2; /* page length */
4134 				page_buf[4] = INQUIRY_SUP_VPD_PAGE;
4135 				page_buf[5] = INQUIRY_USN_PAGE;
4136 				/* Copy no more than requested */
4137 				count = MIN(bp->b_bcount, 6);
4138 				bcopy(page_buf, bp->b_un.b_addr, count);
4139 				break;
4140 			case INQUIRY_USN_PAGE:
4141 				/*
4142 				 * Request for Unit Serial Number page
4143 				 */
4144 				page_buf[0] = peripheral_device_type;
4145 				page_buf[1] = INQUIRY_USN_PAGE;
4146 				page_buf[2] = 0;
4147 				page_buf[3] = 20; /* remaining page length */
4148 				p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser);
4149 #ifdef	_LITTLE_ENDIAN
4150 				swab(p, &page_buf[4], 20);
4151 #else
4152 				bcopy(p, &page_buf[4], 20);
4153 #endif
4154 				for (i = 0; i < 20; i++) {
4155 					if (page_buf[4 + i] == '\0' ||
4156 					    page_buf[4 + i] == '\040') {
4157 						break;
4158 					}
4159 				}
4160 				/*
4161 				 * 'i' contains string length.
4162 				 *
4163 				 * Least significant character of the serial
4164 				 * number shall appear as the last byte,
4165 				 * according to SBC-3 spec.
4166 				 */
4167 				p = &page_buf[20 + 4 - 1];
4168 				for (j = i; j > 0; j--, p--) {
4169 					*p = *(p - 20 + i);
4170 				}
4171 				p = &page_buf[4];
4172 				for (j = 20 - i; j > 0; j--) {
4173 					*p++ = '\040';
4174 				}
4175 				count = MIN(bp->b_bcount, 24);
4176 				bcopy(page_buf, bp->b_un.b_addr, count);
4177 				break;
4178 
4179 			case INQUIRY_DEV_IDENTIFICATION_PAGE:
4180 				/*
4181 				 * We may want to implement this page, when
4182 				 * identifiers are common for SATA devices
4183 				 * But not now.
4184 				 */
4185 				/*FALLTHROUGH*/
4186 
4187 			default:
4188 				/* Request for unsupported VPD page */
4189 				*scsipkt->pkt_scbp = STATUS_CHECK;
4190 				sense = sata_arq_sense(spx);
4191 				sense->es_key = KEY_ILLEGAL_REQUEST;
4192 				sense->es_add_code =
4193 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4194 				goto done;
4195 			}
4196 		}
4197 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4198 		scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
4199 		    scsipkt->pkt_cdbp[4] - count : 0;
4200 	}
4201 done:
4202 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4203 
4204 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4205 	    "Scsi_pkt completion reason %x\n",
4206 	    scsipkt->pkt_reason);
4207 
4208 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4209 	    scsipkt->pkt_comp != NULL) {
4210 		/* scsi callback required */
4211 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4212 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4213 		    TQ_SLEEP) == NULL)
4214 			/* Scheduling the callback failed */
4215 			return (TRAN_BUSY);
4216 	}
4217 	return (TRAN_ACCEPT);
4218 }
4219 
4220 /*
4221  * SATA translate command: Request Sense.
4222  * Emulated command (ATA version for SATA hard disks)
4223  * Always NO SENSE, because any sense data should be reported by ARQ sense.
4224  *
4225  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4226  */
4227 static int
4228 sata_txlt_request_sense(sata_pkt_txlate_t *spx)
4229 {
4230 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4231 	struct scsi_extended_sense sense;
4232 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4233 	int rval;
4234 
4235 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4236 
4237 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
4238 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
4239 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4240 		return (rval);
4241 	}
4242 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4243 
4244 
4245 	scsipkt->pkt_reason = CMD_CMPLT;
4246 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4247 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4248 	*scsipkt->pkt_scbp = STATUS_GOOD;
4249 
4250 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4251 		/*
4252 		 * Because it is fully emulated command storing data
4253 		 * programatically in the specified buffer, release
4254 		 * preallocated DMA resources before storing data in the buffer,
4255 		 * so no unwanted DMA sync would take place.
4256 		 */
4257 		int count = MIN(bp->b_bcount,
4258 		    sizeof (struct scsi_extended_sense));
4259 		sata_scsi_dmafree(NULL, scsipkt);
4260 		bzero(&sense, sizeof (struct scsi_extended_sense));
4261 		sense.es_valid = 0;	/* Valid LBA */
4262 		sense.es_class = 7;	/* Response code 0x70 - current err */
4263 		sense.es_key = KEY_NO_SENSE;
4264 		sense.es_add_len = 6;	/* Additional length */
4265 		/* Copy no more than requested */
4266 		bcopy(&sense, bp->b_un.b_addr, count);
4267 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4268 		scsipkt->pkt_resid = 0;
4269 	}
4270 
4271 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4272 	    "Scsi_pkt completion reason %x\n",
4273 	    scsipkt->pkt_reason);
4274 
4275 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4276 	    scsipkt->pkt_comp != NULL)
4277 		/* scsi callback required */
4278 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4279 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4280 		    TQ_SLEEP) == NULL)
4281 			/* Scheduling the callback failed */
4282 			return (TRAN_BUSY);
4283 	return (TRAN_ACCEPT);
4284 }
4285 
4286 /*
4287  * SATA translate command: Test Unit Ready
4288  * At the moment this is an emulated command (ATA version for SATA hard disks).
4289  * May be translated into Check Power Mode command in the future
4290  *
4291  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4292  */
4293 static int
4294 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx)
4295 {
4296 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4297 	struct scsi_extended_sense *sense;
4298 	int power_state;
4299 	int rval;
4300 
4301 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4302 
4303 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
4304 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
4305 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4306 		return (rval);
4307 	}
4308 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4309 
4310 	/* At this moment, emulate it rather than execute anything */
4311 	power_state = SATA_PWRMODE_ACTIVE;
4312 
4313 	scsipkt->pkt_reason = CMD_CMPLT;
4314 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4315 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4316 
4317 	switch (power_state) {
4318 	case SATA_PWRMODE_ACTIVE:
4319 	case SATA_PWRMODE_IDLE:
4320 		*scsipkt->pkt_scbp = STATUS_GOOD;
4321 		break;
4322 	default:
4323 		/* PWR mode standby */
4324 		*scsipkt->pkt_scbp = STATUS_CHECK;
4325 		sense = sata_arq_sense(spx);
4326 		sense->es_key = KEY_NOT_READY;
4327 		sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY;
4328 		break;
4329 	}
4330 
4331 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4332 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4333 
4334 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4335 	    scsipkt->pkt_comp != NULL)
4336 		/* scsi callback required */
4337 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4338 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4339 		    TQ_SLEEP) == NULL)
4340 			/* Scheduling the callback failed */
4341 			return (TRAN_BUSY);
4342 
4343 	return (TRAN_ACCEPT);
4344 }
4345 
4346 
4347 /*
4348  * SATA translate command: Start Stop Unit
4349  * Translation depends on a command:
4350  *	Start Unit translated into Idle Immediate
4351  *	Stop Unit translated into Standby Immediate
4352  *	Unload Media / NOT SUPPORTED YET
4353  *	Load Media / NOT SUPPROTED YET
4354  * Power condition bits are ignored, so is Immediate bit
4355  * Requesting synchronous execution.
4356  *
4357  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4358  * appropriate values in scsi_pkt fields.
4359  */
4360 static int
4361 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx)
4362 {
4363 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4364 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4365 	struct scsi_extended_sense *sense;
4366 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4367 	int cport = SATA_TXLT_CPORT(spx);
4368 	int rval;
4369 	int synch;
4370 
4371 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4372 	    "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1);
4373 
4374 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
4375 
4376 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
4377 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
4378 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4379 		return (rval);
4380 	}
4381 
4382 	if (scsipkt->pkt_cdbp[4] & 2) {
4383 		/* Load/Unload Media - invalid request */
4384 		*scsipkt->pkt_scbp = STATUS_CHECK;
4385 		sense = sata_arq_sense(spx);
4386 		sense->es_key = KEY_ILLEGAL_REQUEST;
4387 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4388 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4389 
4390 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4391 		    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4392 
4393 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4394 		    scsipkt->pkt_comp != NULL)
4395 			/* scsi callback required */
4396 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4397 			    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4398 			    TQ_SLEEP) == NULL)
4399 				/* Scheduling the callback failed */
4400 				return (TRAN_BUSY);
4401 
4402 		return (TRAN_ACCEPT);
4403 	}
4404 	scmd->satacmd_addr_type = 0;
4405 	scmd->satacmd_sec_count_lsb = 0;
4406 	scmd->satacmd_lba_low_lsb = 0;
4407 	scmd->satacmd_lba_mid_lsb = 0;
4408 	scmd->satacmd_lba_high_lsb = 0;
4409 	scmd->satacmd_features_reg = 0;
4410 	scmd->satacmd_device_reg = 0;
4411 	scmd->satacmd_status_reg = 0;
4412 	if (scsipkt->pkt_cdbp[4] & 1) {
4413 		/* Start Unit */
4414 		scmd->satacmd_cmd_reg = SATAC_IDLE_IM;
4415 	} else {
4416 		/* Stop Unit */
4417 		scmd->satacmd_cmd_reg = SATAC_STANDBY_IM;
4418 	}
4419 
4420 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4421 		/* Need to set-up a callback function */
4422 		spx->txlt_sata_pkt->satapkt_comp =
4423 		    sata_txlt_nodata_cmd_completion;
4424 		synch = FALSE;
4425 	} else {
4426 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
4427 		synch = TRUE;
4428 	}
4429 
4430 	/* Transfer command to HBA */
4431 	if (sata_hba_start(spx, &rval) != 0) {
4432 		/* Pkt not accepted for execution */
4433 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4434 		return (rval);
4435 	}
4436 
4437 	/*
4438 	 * If execution is non-synchronous,
4439 	 * a callback function will handle potential errors, translate
4440 	 * the response and will do a callback to a target driver.
4441 	 * If it was synchronous, check execution status using the same
4442 	 * framework callback.
4443 	 */
4444 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4445 	if (synch) {
4446 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4447 		    "synchronous execution status %x\n",
4448 		    spx->txlt_sata_pkt->satapkt_reason);
4449 
4450 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
4451 	}
4452 	return (TRAN_ACCEPT);
4453 
4454 }
4455 
4456 
4457 /*
4458  * SATA translate command:  Read Capacity.
4459  * Emulated command for SATA disks.
4460  * Capacity is retrieved from cached Idenifty Device data.
4461  * Identify Device data shows effective disk capacity, not the native
4462  * capacity, which may be limitted by Set Max Address command.
4463  * This is ATA version for SATA hard disks.
4464  *
4465  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4466  */
4467 static int
4468 sata_txlt_read_capacity(sata_pkt_txlate_t *spx)
4469 {
4470 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4471 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4472 	sata_drive_info_t *sdinfo;
4473 	uint64_t val;
4474 	uchar_t *rbuf;
4475 	int rval;
4476 
4477 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4478 	    "sata_txlt_read_capacity: ", NULL);
4479 
4480 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4481 
4482 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
4483 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
4484 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4485 		return (rval);
4486 	}
4487 
4488 	scsipkt->pkt_reason = CMD_CMPLT;
4489 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4490 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4491 	*scsipkt->pkt_scbp = STATUS_GOOD;
4492 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4493 		/*
4494 		 * Because it is fully emulated command storing data
4495 		 * programatically in the specified buffer, release
4496 		 * preallocated DMA resources before storing data in the buffer,
4497 		 * so no unwanted DMA sync would take place.
4498 		 */
4499 		sata_scsi_dmafree(NULL, scsipkt);
4500 
4501 		sdinfo = sata_get_device_info(
4502 		    spx->txlt_sata_hba_inst,
4503 		    &spx->txlt_sata_pkt->satapkt_device);
4504 		/* Last logical block address */
4505 		val = sdinfo->satadrv_capacity - 1;
4506 		rbuf = (uchar_t *)bp->b_un.b_addr;
4507 		/* Need to swap endians to match scsi format */
4508 		rbuf[0] = (val >> 24) & 0xff;
4509 		rbuf[1] = (val >> 16) & 0xff;
4510 		rbuf[2] = (val >> 8) & 0xff;
4511 		rbuf[3] = val & 0xff;
4512 		/* block size - always 512 bytes, for now */
4513 		rbuf[4] = 0;
4514 		rbuf[5] = 0;
4515 		rbuf[6] = 0x02;
4516 		rbuf[7] = 0;
4517 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4518 		scsipkt->pkt_resid = 0;
4519 
4520 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n",
4521 		    sdinfo->satadrv_capacity -1);
4522 	}
4523 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4524 	/*
4525 	 * If a callback was requested, do it now.
4526 	 */
4527 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4528 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4529 
4530 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4531 	    scsipkt->pkt_comp != NULL)
4532 		/* scsi callback required */
4533 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4534 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4535 		    TQ_SLEEP) == NULL)
4536 			/* Scheduling the callback failed */
4537 			return (TRAN_BUSY);
4538 
4539 	return (TRAN_ACCEPT);
4540 }
4541 
4542 /*
4543  * SATA translate command: Mode Sense.
4544  * Translated into appropriate SATA command or emulated.
4545  * Saved Values Page Control (03) are not supported.
4546  *
4547  * NOTE: only caching mode sense page is currently implemented.
4548  *
4549  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4550  */
4551 
4552 static int
4553 sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
4554 {
4555 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4556 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4557 	sata_drive_info_t *sdinfo;
4558 	sata_id_t *sata_id;
4559 	struct scsi_extended_sense *sense;
4560 	int 		len, bdlen, count, alc_len;
4561 	int		pc;	/* Page Control code */
4562 	uint8_t		*buf;	/* mode sense buffer */
4563 	int		rval;
4564 
4565 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4566 	    "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
4567 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4568 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4569 
4570 	buf = kmem_zalloc(1024, KM_SLEEP);
4571 
4572 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4573 
4574 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
4575 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
4576 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4577 		kmem_free(buf, 1024);
4578 		return (rval);
4579 	}
4580 
4581 	scsipkt->pkt_reason = CMD_CMPLT;
4582 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4583 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4584 
4585 	pc = scsipkt->pkt_cdbp[2] >> 6;
4586 
4587 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4588 		/*
4589 		 * Because it is fully emulated command storing data
4590 		 * programatically in the specified buffer, release
4591 		 * preallocated DMA resources before storing data in the buffer,
4592 		 * so no unwanted DMA sync would take place.
4593 		 */
4594 		sata_scsi_dmafree(NULL, scsipkt);
4595 
4596 		len = 0;
4597 		bdlen = 0;
4598 		if (!(scsipkt->pkt_cdbp[1] & 8)) {
4599 			if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
4600 			    (scsipkt->pkt_cdbp[0] & 0x10))
4601 				bdlen = 16;
4602 			else
4603 				bdlen = 8;
4604 		}
4605 		/* Build mode parameter header */
4606 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
4607 			/* 4-byte mode parameter header */
4608 			buf[len++] = 0;   	/* mode data length */
4609 			buf[len++] = 0;		/* medium type */
4610 			buf[len++] = 0;		/* dev-specific param */
4611 			buf[len++] = bdlen;	/* Block Descriptor length */
4612 		} else {
4613 			/* 8-byte mode parameter header */
4614 			buf[len++] = 0;		/* mode data length */
4615 			buf[len++] = 0;
4616 			buf[len++] = 0;		/* medium type */
4617 			buf[len++] = 0;		/* dev-specific param */
4618 			if (bdlen == 16)
4619 				buf[len++] = 1;	/* long lba descriptor */
4620 			else
4621 				buf[len++] = 0;
4622 			buf[len++] = 0;
4623 			buf[len++] = 0;		/* Block Descriptor length */
4624 			buf[len++] = bdlen;
4625 		}
4626 
4627 		sdinfo = sata_get_device_info(
4628 		    spx->txlt_sata_hba_inst,
4629 		    &spx->txlt_sata_pkt->satapkt_device);
4630 
4631 		/* Build block descriptor only if not disabled (DBD) */
4632 		if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
4633 			/* Block descriptor - direct-access device format */
4634 			if (bdlen == 8) {
4635 				/* build regular block descriptor */
4636 				buf[len++] =
4637 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
4638 				buf[len++] =
4639 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
4640 				buf[len++] =
4641 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
4642 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
4643 				buf[len++] = 0; /* density code */
4644 				buf[len++] = 0;
4645 				if (sdinfo->satadrv_type ==
4646 				    SATA_DTYPE_ATADISK)
4647 					buf[len++] = 2;
4648 				else
4649 					/* ATAPI */
4650 					buf[len++] = 8;
4651 				buf[len++] = 0;
4652 			} else if (bdlen == 16) {
4653 				/* Long LBA Accepted */
4654 				/* build long lba block descriptor */
4655 #ifndef __lock_lint
4656 				buf[len++] =
4657 				    (sdinfo->satadrv_capacity >> 56) & 0xff;
4658 				buf[len++] =
4659 				    (sdinfo->satadrv_capacity >> 48) & 0xff;
4660 				buf[len++] =
4661 				    (sdinfo->satadrv_capacity >> 40) & 0xff;
4662 				buf[len++] =
4663 				    (sdinfo->satadrv_capacity >> 32) & 0xff;
4664 #endif
4665 				buf[len++] =
4666 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
4667 				buf[len++] =
4668 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
4669 				buf[len++] =
4670 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
4671 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
4672 				buf[len++] = 0;
4673 				buf[len++] = 0; /* density code */
4674 				buf[len++] = 0;
4675 				buf[len++] = 0;
4676 				if (sdinfo->satadrv_type ==
4677 				    SATA_DTYPE_ATADISK)
4678 					buf[len++] = 2;
4679 				else
4680 					/* ATAPI */
4681 					buf[len++] = 8;
4682 				buf[len++] = 0;
4683 			}
4684 		}
4685 
4686 		sata_id = &sdinfo->satadrv_id;
4687 
4688 		/*
4689 		 * Add requested pages.
4690 		 * Page 3 and 4 are obsolete and we are not supporting them.
4691 		 * We deal now with:
4692 		 * caching (read/write cache control).
4693 		 * We should eventually deal with following mode pages:
4694 		 * error recovery  (0x01),
4695 		 * power condition (0x1a),
4696 		 * exception control page (enables SMART) (0x1c),
4697 		 * enclosure management (ses),
4698 		 * protocol-specific port mode (port control).
4699 		 */
4700 		switch (scsipkt->pkt_cdbp[2] & 0x3f) {
4701 		case MODEPAGE_RW_ERRRECOV:
4702 			/* DAD_MODE_ERR_RECOV */
4703 			/* R/W recovery */
4704 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
4705 			break;
4706 		case MODEPAGE_CACHING:
4707 			/* DAD_MODE_CACHE */
4708 			/* Reject not supported request for saved parameters */
4709 			if (pc == 3) {
4710 				*scsipkt->pkt_scbp = STATUS_CHECK;
4711 				sense = sata_arq_sense(spx);
4712 				sense->es_key = KEY_ILLEGAL_REQUEST;
4713 				sense->es_add_code =
4714 				    SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
4715 				goto done;
4716 			}
4717 
4718 			/* caching */
4719 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
4720 			break;
4721 		case MODEPAGE_INFO_EXCPT:
4722 			/* exception cntrl */
4723 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
4724 				len += sata_build_msense_page_1c(sdinfo, pc,
4725 				    buf+len);
4726 			}
4727 			else
4728 				goto err;
4729 			break;
4730 		case MODEPAGE_POWER_COND:
4731 			/* DAD_MODE_POWER_COND */
4732 			/* power condition */
4733 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
4734 			break;
4735 
4736 		case MODEPAGE_ACOUSTIC_MANAG:
4737 			/* acoustic management */
4738 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
4739 			break;
4740 		case MODEPAGE_ALLPAGES:
4741 			/* all pages */
4742 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
4743 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
4744 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
4745 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
4746 				len += sata_build_msense_page_1c(sdinfo, pc,
4747 				    buf+len);
4748 			}
4749 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
4750 			break;
4751 		default:
4752 		err:
4753 			/* Invalid request */
4754 			*scsipkt->pkt_scbp = STATUS_CHECK;
4755 			sense = sata_arq_sense(spx);
4756 			sense->es_key = KEY_ILLEGAL_REQUEST;
4757 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4758 			goto done;
4759 		}
4760 
4761 		/* fix total mode data length */
4762 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
4763 			/* 4-byte mode parameter header */
4764 			buf[0] = len - 1;   	/* mode data length */
4765 		} else {
4766 			buf[0] = (len -2) >> 8;
4767 			buf[1] = (len -2) & 0xff;
4768 		}
4769 
4770 
4771 		/* Check allocation length */
4772 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
4773 			alc_len = scsipkt->pkt_cdbp[4];
4774 		} else {
4775 			alc_len = scsipkt->pkt_cdbp[7];
4776 			alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
4777 		}
4778 		/*
4779 		 * We do not check for possible parameters truncation
4780 		 * (alc_len < len) assuming that the target driver works
4781 		 * correctly. Just avoiding overrun.
4782 		 * Copy no more than requested and possible, buffer-wise.
4783 		 */
4784 		count = MIN(alc_len, len);
4785 		count = MIN(bp->b_bcount, count);
4786 		bcopy(buf, bp->b_un.b_addr, count);
4787 
4788 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4789 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
4790 	}
4791 	*scsipkt->pkt_scbp = STATUS_GOOD;
4792 done:
4793 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4794 	(void) kmem_free(buf, 1024);
4795 
4796 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4797 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4798 
4799 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4800 	    scsipkt->pkt_comp != NULL)
4801 		/* scsi callback required */
4802 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4803 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4804 		    TQ_SLEEP) == NULL)
4805 			/* Scheduling the callback failed */
4806 			return (TRAN_BUSY);
4807 
4808 	return (TRAN_ACCEPT);
4809 }
4810 
4811 
4812 /*
4813  * SATA translate command: Mode Select.
4814  * Translated into appropriate SATA command or emulated.
4815  * Saving parameters is not supported.
4816  * Changing device capacity is not supported (although theoretically
4817  * possible by executing SET FEATURES/SET MAX ADDRESS)
4818  *
4819  * Assumption is that the target driver is working correctly.
4820  *
4821  * More than one SATA command may be executed to perform operations specified
4822  * by mode select pages. The first error terminates further execution.
4823  * Operations performed successully are not backed-up in such case.
4824  *
4825  * NOTE: only caching mode select page is implemented.
4826  * Caching setup is remembered so it could be re-stored in case of
4827  * an unexpected device reset.
4828  *
4829  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4830  */
4831 
4832 static int
4833 sata_txlt_mode_select(sata_pkt_txlate_t *spx)
4834 {
4835 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4836 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4837 	struct scsi_extended_sense *sense;
4838 	int len, pagelen, count, pllen;
4839 	uint8_t *buf;	/* mode select buffer */
4840 	int rval, stat;
4841 	uint_t nointr_flag;
4842 	int dmod = 0;
4843 
4844 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4845 	    "sata_txlt_mode_select, pc %x page code 0x%02x\n",
4846 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4847 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4848 
4849 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4850 
4851 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
4852 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
4853 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4854 		return (rval);
4855 	}
4856 
4857 	rval = TRAN_ACCEPT;
4858 
4859 	scsipkt->pkt_reason = CMD_CMPLT;
4860 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4861 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4862 
4863 	/* Reject not supported request */
4864 	if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
4865 		*scsipkt->pkt_scbp = STATUS_CHECK;
4866 		sense = sata_arq_sense(spx);
4867 		sense->es_key = KEY_ILLEGAL_REQUEST;
4868 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4869 		goto done;
4870 	}
4871 
4872 	if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
4873 		pllen = scsipkt->pkt_cdbp[4];
4874 	} else {
4875 		pllen = scsipkt->pkt_cdbp[7];
4876 		pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
4877 	}
4878 
4879 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
4880 
4881 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
4882 		buf = (uint8_t *)bp->b_un.b_addr;
4883 		count = MIN(bp->b_bcount, pllen);
4884 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4885 		scsipkt->pkt_resid = 0;
4886 		pllen = count;
4887 
4888 		/*
4889 		 * Check the header to skip the block descriptor(s) - we
4890 		 * do not support setting device capacity.
4891 		 * Existing macros do not recognize long LBA dscriptor,
4892 		 * hence manual calculation.
4893 		 */
4894 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
4895 			/* 6-bytes CMD, 4 bytes header */
4896 			if (count <= 4)
4897 				goto done;		/* header only */
4898 			len = buf[3] + 4;
4899 		} else {
4900 			/* 10-bytes CMD, 8 bytes header */
4901 			if (count <= 8)
4902 				goto done;		/* header only */
4903 			len = buf[6];
4904 			len = (len << 8) + buf[7] + 8;
4905 		}
4906 		if (len >= count)
4907 			goto done;	/* header + descriptor(s) only */
4908 
4909 		pllen -= len;		/* remaining data length */
4910 
4911 		/*
4912 		 * We may be executing SATA command and want to execute it
4913 		 * in SYNCH mode, regardless of scsi_pkt setting.
4914 		 * Save scsi_pkt setting and indicate SYNCH mode
4915 		 */
4916 		nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
4917 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4918 		    scsipkt->pkt_comp != NULL) {
4919 			scsipkt->pkt_flags |= FLAG_NOINTR;
4920 		}
4921 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
4922 
4923 		/*
4924 		 * len is now the offset to a first mode select page
4925 		 * Process all pages
4926 		 */
4927 		while (pllen > 0) {
4928 			switch ((int)buf[len]) {
4929 			case MODEPAGE_CACHING:
4930 				/* No support for SP (saving) */
4931 				if (scsipkt->pkt_cdbp[1] & 0x01) {
4932 					*scsipkt->pkt_scbp = STATUS_CHECK;
4933 					sense = sata_arq_sense(spx);
4934 					sense->es_key = KEY_ILLEGAL_REQUEST;
4935 					sense->es_add_code =
4936 					    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4937 					goto done;
4938 				}
4939 				stat = sata_mode_select_page_8(spx,
4940 				    (struct mode_cache_scsi3 *)&buf[len],
4941 				    pllen, &pagelen, &rval, &dmod);
4942 				/*
4943 				 * The pagelen value indicates the number of
4944 				 * parameter bytes already processed.
4945 				 * The rval is the return value from
4946 				 * sata_tran_start().
4947 				 * The stat indicates the overall status of
4948 				 * the operation(s).
4949 				 */
4950 				if (stat != SATA_SUCCESS)
4951 					/*
4952 					 * Page processing did not succeed -
4953 					 * all error info is already set-up,
4954 					 * just return
4955 					 */
4956 					pllen = 0; /* this breaks the loop */
4957 				else {
4958 					len += pagelen;
4959 					pllen -= pagelen;
4960 				}
4961 				break;
4962 
4963 			case MODEPAGE_INFO_EXCPT:
4964 				stat = sata_mode_select_page_1c(spx,
4965 				    (struct mode_info_excpt_page *)&buf[len],
4966 				    pllen, &pagelen, &rval, &dmod);
4967 				/*
4968 				 * The pagelen value indicates the number of
4969 				 * parameter bytes already processed.
4970 				 * The rval is the return value from
4971 				 * sata_tran_start().
4972 				 * The stat indicates the overall status of
4973 				 * the operation(s).
4974 				 */
4975 				if (stat != SATA_SUCCESS)
4976 					/*
4977 					 * Page processing did not succeed -
4978 					 * all error info is already set-up,
4979 					 * just return
4980 					 */
4981 					pllen = 0; /* this breaks the loop */
4982 				else {
4983 					len += pagelen;
4984 					pllen -= pagelen;
4985 				}
4986 				break;
4987 
4988 			case MODEPAGE_ACOUSTIC_MANAG:
4989 				stat = sata_mode_select_page_30(spx,
4990 				    (struct mode_acoustic_management *)
4991 				    &buf[len], pllen, &pagelen, &rval, &dmod);
4992 				/*
4993 				 * The pagelen value indicates the number of
4994 				 * parameter bytes already processed.
4995 				 * The rval is the return value from
4996 				 * sata_tran_start().
4997 				 * The stat indicates the overall status of
4998 				 * the operation(s).
4999 				 */
5000 				if (stat != SATA_SUCCESS)
5001 					/*
5002 					 * Page processing did not succeed -
5003 					 * all error info is already set-up,
5004 					 * just return
5005 					 */
5006 					pllen = 0; /* this breaks the loop */
5007 				else {
5008 					len += pagelen;
5009 					pllen -= pagelen;
5010 				}
5011 
5012 				break;
5013 			default:
5014 				*scsipkt->pkt_scbp = STATUS_CHECK;
5015 				sense = sata_arq_sense(spx);
5016 				sense->es_key = KEY_ILLEGAL_REQUEST;
5017 				sense->es_add_code =
5018 				    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
5019 				goto done;
5020 			}
5021 		}
5022 	}
5023 done:
5024 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5025 	/*
5026 	 * If device parameters were modified, fetch and store the new
5027 	 * Identify Device data. Since port mutex could have been released
5028 	 * for accessing HBA driver, we need to re-check device existence.
5029 	 */
5030 	if (dmod != 0) {
5031 		sata_drive_info_t new_sdinfo, *sdinfo;
5032 		int rv;
5033 
5034 		new_sdinfo.satadrv_addr =
5035 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr;
5036 		rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
5037 		    &new_sdinfo);
5038 
5039 		mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5040 		/*
5041 		 * Since port mutex could have been released when
5042 		 * accessing HBA driver, we need to re-check that the
5043 		 * framework still holds the device info structure.
5044 		 */
5045 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
5046 		    &spx->txlt_sata_pkt->satapkt_device);
5047 		if (sdinfo != NULL) {
5048 			/*
5049 			 * Device still has info structure in the
5050 			 * sata framework. Copy newly fetched info
5051 			 */
5052 			if (rv == 0) {
5053 				sdinfo->satadrv_id = new_sdinfo.satadrv_id;
5054 				sata_save_drive_settings(sdinfo);
5055 			} else {
5056 				/*
5057 				 * Could not fetch new data - invalidate
5058 				 * sata_drive_info. That makes device
5059 				 * unusable.
5060 				 */
5061 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
5062 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
5063 			}
5064 		}
5065 		if (rv != 0 || sdinfo == NULL) {
5066 			/*
5067 			 * This changes the overall mode select completion
5068 			 * reason to a failed one !!!!!
5069 			 */
5070 			*scsipkt->pkt_scbp = STATUS_CHECK;
5071 			sense = sata_arq_sense(spx);
5072 			scsipkt->pkt_reason = CMD_INCOMPLETE;
5073 			rval = TRAN_ACCEPT;
5074 		}
5075 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5076 	}
5077 	/* Restore the scsi pkt flags */
5078 	scsipkt->pkt_flags &= ~FLAG_NOINTR;
5079 	scsipkt->pkt_flags |= nointr_flag;
5080 
5081 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5082 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5083 
5084 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5085 	    scsipkt->pkt_comp != NULL)
5086 		/* scsi callback required */
5087 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5088 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5089 		    TQ_SLEEP) == NULL)
5090 			/* Scheduling the callback failed */
5091 			return (TRAN_BUSY);
5092 
5093 	return (rval);
5094 }
5095 
5096 
5097 
5098 /*
5099  * Translate command: Log Sense
5100  */
5101 static 	int
5102 sata_txlt_log_sense(sata_pkt_txlate_t *spx)
5103 {
5104 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
5105 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5106 	sata_drive_info_t *sdinfo;
5107 	struct scsi_extended_sense *sense;
5108 	int 		len, count, alc_len;
5109 	int		pc;	/* Page Control code */
5110 	int		page_code;	/* Page code */
5111 	uint8_t		*buf;	/* log sense buffer */
5112 	int		rval;
5113 #define	MAX_LOG_SENSE_PAGE_SIZE	512
5114 
5115 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5116 	    "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
5117 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5118 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5119 
5120 	buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
5121 
5122 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5123 
5124 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
5125 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
5126 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5127 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
5128 		return (rval);
5129 	}
5130 
5131 	scsipkt->pkt_reason = CMD_CMPLT;
5132 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5133 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5134 
5135 	pc = scsipkt->pkt_cdbp[2] >> 6;
5136 	page_code = scsipkt->pkt_cdbp[2] & 0x3f;
5137 
5138 	/* Reject not supported request for all but cumulative values */
5139 	switch (pc) {
5140 	case PC_CUMULATIVE_VALUES:
5141 		break;
5142 	default:
5143 		*scsipkt->pkt_scbp = STATUS_CHECK;
5144 		sense = sata_arq_sense(spx);
5145 		sense->es_key = KEY_ILLEGAL_REQUEST;
5146 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5147 		goto done;
5148 	}
5149 
5150 	switch (page_code) {
5151 	case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5152 	case PAGE_CODE_SELF_TEST_RESULTS:
5153 	case PAGE_CODE_INFORMATION_EXCEPTIONS:
5154 	case PAGE_CODE_SMART_READ_DATA:
5155 		break;
5156 	default:
5157 		*scsipkt->pkt_scbp = STATUS_CHECK;
5158 		sense = sata_arq_sense(spx);
5159 		sense->es_key = KEY_ILLEGAL_REQUEST;
5160 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5161 		goto done;
5162 	}
5163 
5164 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
5165 		/*
5166 		 * Because log sense uses local buffers for data retrieval from
5167 		 * the devices and sets the data programatically in the
5168 		 * original specified buffer, release preallocated DMA
5169 		 * resources before storing data in the original buffer,
5170 		 * so no unwanted DMA sync would take place.
5171 		 */
5172 		sata_id_t *sata_id;
5173 
5174 		sata_scsi_dmafree(NULL, scsipkt);
5175 
5176 		len = 0;
5177 
5178 		/* Build log parameter header */
5179 		buf[len++] = page_code;	/* page code as in the CDB */
5180 		buf[len++] = 0;		/* reserved */
5181 		buf[len++] = 0;		/* Zero out page length for now (MSB) */
5182 		buf[len++] = 0;		/* (LSB) */
5183 
5184 		sdinfo = sata_get_device_info(
5185 		    spx->txlt_sata_hba_inst,
5186 		    &spx->txlt_sata_pkt->satapkt_device);
5187 
5188 
5189 		/*
5190 		 * Add requested pages.
5191 		 */
5192 		switch (page_code) {
5193 		case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5194 			len = sata_build_lsense_page_0(sdinfo, buf + len);
5195 			break;
5196 		case PAGE_CODE_SELF_TEST_RESULTS:
5197 			sata_id = &sdinfo->satadrv_id;
5198 			if ((! (sata_id->ai_cmdset84 &
5199 			    SATA_SMART_SELF_TEST_SUPPORTED)) ||
5200 			    (! (sata_id->ai_features87 &
5201 			    SATA_SMART_SELF_TEST_SUPPORTED))) {
5202 				*scsipkt->pkt_scbp = STATUS_CHECK;
5203 				sense = sata_arq_sense(spx);
5204 				sense->es_key = KEY_ILLEGAL_REQUEST;
5205 				sense->es_add_code =
5206 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5207 
5208 				goto done;
5209 			}
5210 			len = sata_build_lsense_page_10(sdinfo, buf + len,
5211 			    spx->txlt_sata_hba_inst);
5212 			break;
5213 		case PAGE_CODE_INFORMATION_EXCEPTIONS:
5214 			sata_id = &sdinfo->satadrv_id;
5215 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5216 				*scsipkt->pkt_scbp = STATUS_CHECK;
5217 				sense = sata_arq_sense(spx);
5218 				sense->es_key = KEY_ILLEGAL_REQUEST;
5219 				sense->es_add_code =
5220 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5221 
5222 				goto done;
5223 			}
5224 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5225 				*scsipkt->pkt_scbp = STATUS_CHECK;
5226 				sense = sata_arq_sense(spx);
5227 				sense->es_key = KEY_ABORTED_COMMAND;
5228 				sense->es_add_code =
5229 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5230 				sense->es_qual_code =
5231 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5232 
5233 				goto done;
5234 			}
5235 
5236 			len = sata_build_lsense_page_2f(sdinfo, buf + len,
5237 			    spx->txlt_sata_hba_inst);
5238 			break;
5239 		case PAGE_CODE_SMART_READ_DATA:
5240 			sata_id = &sdinfo->satadrv_id;
5241 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5242 				*scsipkt->pkt_scbp = STATUS_CHECK;
5243 				sense = sata_arq_sense(spx);
5244 				sense->es_key = KEY_ILLEGAL_REQUEST;
5245 				sense->es_add_code =
5246 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5247 
5248 				goto done;
5249 			}
5250 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5251 				*scsipkt->pkt_scbp = STATUS_CHECK;
5252 				sense = sata_arq_sense(spx);
5253 				sense->es_key = KEY_ABORTED_COMMAND;
5254 				sense->es_add_code =
5255 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5256 				sense->es_qual_code =
5257 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5258 
5259 				goto done;
5260 			}
5261 
5262 			/* This page doesn't include a page header */
5263 			len = sata_build_lsense_page_30(sdinfo, buf,
5264 			    spx->txlt_sata_hba_inst);
5265 			goto no_header;
5266 		default:
5267 			/* Invalid request */
5268 			*scsipkt->pkt_scbp = STATUS_CHECK;
5269 			sense = sata_arq_sense(spx);
5270 			sense->es_key = KEY_ILLEGAL_REQUEST;
5271 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5272 			goto done;
5273 		}
5274 
5275 		/* set parameter log sense data length */
5276 		buf[2] = len >> 8;	/* log sense length (MSB) */
5277 		buf[3] = len & 0xff;	/* log sense length (LSB) */
5278 
5279 		len += SCSI_LOG_PAGE_HDR_LEN;
5280 		ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
5281 
5282 no_header:
5283 		/* Check allocation length */
5284 		alc_len = scsipkt->pkt_cdbp[7];
5285 		alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
5286 
5287 		/*
5288 		 * We do not check for possible parameters truncation
5289 		 * (alc_len < len) assuming that the target driver works
5290 		 * correctly. Just avoiding overrun.
5291 		 * Copy no more than requested and possible, buffer-wise.
5292 		 */
5293 		count = MIN(alc_len, len);
5294 		count = MIN(bp->b_bcount, count);
5295 		bcopy(buf, bp->b_un.b_addr, count);
5296 
5297 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
5298 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
5299 	}
5300 	*scsipkt->pkt_scbp = STATUS_GOOD;
5301 done:
5302 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5303 	(void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
5304 
5305 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5306 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5307 
5308 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5309 	    scsipkt->pkt_comp != NULL)
5310 		/* scsi callback required */
5311 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5312 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5313 		    TQ_SLEEP) == NULL)
5314 			/* Scheduling the callback failed */
5315 			return (TRAN_BUSY);
5316 
5317 	return (TRAN_ACCEPT);
5318 }
5319 
5320 /*
5321  * Translate command: Log Select
5322  * Not implemented at this time - returns invalid command response.
5323  */
5324 static 	int
5325 sata_txlt_log_select(sata_pkt_txlate_t *spx)
5326 {
5327 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5328 	    "sata_txlt_log_select\n", NULL);
5329 
5330 	return (sata_txlt_invalid_command(spx));
5331 }
5332 
5333 
5334 /*
5335  * Translate command: Read (various types).
5336  * Translated into appropriate type of ATA READ command
5337  * for SATA hard disks.
5338  * Both the device capabilities and requested operation mode are
5339  * considered.
5340  *
5341  * Following scsi cdb fields are ignored:
5342  * rdprotect, dpo, fua, fua_nv, group_number.
5343  *
5344  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
5345  * enable variable sata_func_enable), the capability of the controller and
5346  * capability of a device are checked and if both support queueing, read
5347  * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
5348  * command rather than plain READ_XXX command.
5349  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
5350  * both the controller and device suport such functionality, the read
5351  * request will be translated to READ_FPDMA_QUEUED command.
5352  *
5353  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
5354  * appropriate values in scsi_pkt fields.
5355  */
5356 static int
5357 sata_txlt_read(sata_pkt_txlate_t *spx)
5358 {
5359 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5360 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5361 	sata_drive_info_t *sdinfo;
5362 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
5363 	int cport = SATA_TXLT_CPORT(spx);
5364 	uint16_t sec_count;
5365 	uint64_t lba;
5366 	int rval;
5367 	int synch;
5368 
5369 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5370 
5371 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
5372 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
5373 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5374 		return (rval);
5375 	}
5376 
5377 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
5378 	    &spx->txlt_sata_pkt->satapkt_device);
5379 
5380 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
5381 	/*
5382 	 * Extract LBA and sector count from scsi CDB.
5383 	 */
5384 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
5385 	case SCMD_READ:
5386 		/* 6-byte scsi read cmd : 0x08 */
5387 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
5388 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
5389 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5390 		sec_count = scsipkt->pkt_cdbp[4];
5391 		/* sec_count 0 will be interpreted as 256 by a device */
5392 		break;
5393 	case SCMD_READ_G1:
5394 		/* 10-bytes scsi read command : 0x28 */
5395 		lba = scsipkt->pkt_cdbp[2];
5396 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5397 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5398 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5399 		sec_count = scsipkt->pkt_cdbp[7];
5400 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
5401 		break;
5402 	case SCMD_READ_G5:
5403 		/* 12-bytes scsi read command : 0xA8 */
5404 		lba = scsipkt->pkt_cdbp[2];
5405 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5406 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5407 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5408 		sec_count = scsipkt->pkt_cdbp[6];
5409 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
5410 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
5411 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
5412 		break;
5413 	case SCMD_READ_G4:
5414 		/* 16-bytes scsi read command : 0x88 */
5415 		lba = scsipkt->pkt_cdbp[2];
5416 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5417 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5418 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5419 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
5420 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5421 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5422 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
5423 		sec_count = scsipkt->pkt_cdbp[10];
5424 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
5425 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
5426 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
5427 		break;
5428 	default:
5429 		/* Unsupported command */
5430 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5431 		return (sata_txlt_invalid_command(spx));
5432 	}
5433 
5434 	/*
5435 	 * Check if specified address exceeds device capacity
5436 	 */
5437 	if ((lba >= sdinfo->satadrv_capacity) ||
5438 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
5439 		/* LBA out of range */
5440 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5441 		return (sata_txlt_lba_out_of_range(spx));
5442 	}
5443 
5444 	/*
5445 	 * For zero-length transfer, emulate good completion of the command
5446 	 * (reasons for rejecting the command were already checked).
5447 	 * No DMA resources were allocated.
5448 	 */
5449 	if (spx->txlt_dma_cookie_list == NULL) {
5450 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5451 		return (sata_emul_rw_completion(spx));
5452 	}
5453 
5454 	/*
5455 	 * Build cmd block depending on the device capability and
5456 	 * requested operation mode.
5457 	 * Do not bother with non-dma mode - we are working only with
5458 	 * devices supporting DMA.
5459 	 */
5460 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
5461 	scmd->satacmd_device_reg = SATA_ADH_LBA;
5462 	scmd->satacmd_cmd_reg = SATAC_READ_DMA;
5463 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
5464 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
5465 		scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT;
5466 		scmd->satacmd_sec_count_msb = sec_count >> 8;
5467 #ifndef __lock_lint
5468 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
5469 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
5470 		scmd->satacmd_lba_high_msb = lba >> 40;
5471 #endif
5472 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
5473 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
5474 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
5475 	}
5476 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
5477 	scmd->satacmd_lba_low_lsb = lba & 0xff;
5478 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
5479 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
5480 	scmd->satacmd_features_reg = 0;
5481 	scmd->satacmd_status_reg = 0;
5482 	scmd->satacmd_error_reg = 0;
5483 
5484 	/*
5485 	 * Check if queueing commands should be used and switch
5486 	 * to appropriate command if possible
5487 	 */
5488 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
5489 		boolean_t using_queuing;
5490 
5491 		/* Queuing supported by controller and device? */
5492 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
5493 		    (sdinfo->satadrv_features_support &
5494 		    SATA_DEV_F_NCQ) &&
5495 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
5496 		    SATA_CTLF_NCQ)) {
5497 			using_queuing = B_TRUE;
5498 
5499 			/* NCQ supported - use FPDMA READ */
5500 			scmd->satacmd_cmd_reg =
5501 			    SATAC_READ_FPDMA_QUEUED;
5502 			scmd->satacmd_features_reg_ext =
5503 			    scmd->satacmd_sec_count_msb;
5504 			scmd->satacmd_sec_count_msb = 0;
5505 		} else if ((sdinfo->satadrv_features_support &
5506 		    SATA_DEV_F_TCQ) &&
5507 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
5508 		    SATA_CTLF_QCMD)) {
5509 			using_queuing = B_TRUE;
5510 
5511 			/* Legacy queueing */
5512 			if (sdinfo->satadrv_features_support &
5513 			    SATA_DEV_F_LBA48) {
5514 				scmd->satacmd_cmd_reg =
5515 				    SATAC_READ_DMA_QUEUED_EXT;
5516 				scmd->satacmd_features_reg_ext =
5517 				    scmd->satacmd_sec_count_msb;
5518 				scmd->satacmd_sec_count_msb = 0;
5519 			} else {
5520 				scmd->satacmd_cmd_reg =
5521 				    SATAC_READ_DMA_QUEUED;
5522 			}
5523 		} else	/* Queuing not supported */
5524 			using_queuing = B_FALSE;
5525 
5526 		/*
5527 		 * If queuing, the sector count goes in the features register
5528 		 * and the secount count will contain the tag.
5529 		 */
5530 		if (using_queuing) {
5531 			scmd->satacmd_features_reg =
5532 			    scmd->satacmd_sec_count_lsb;
5533 			scmd->satacmd_sec_count_lsb = 0;
5534 			scmd->satacmd_flags.sata_queued = B_TRUE;
5535 		}
5536 	}
5537 
5538 	SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst,
5539 	    "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n",
5540 	    scmd->satacmd_cmd_reg, lba, sec_count);
5541 
5542 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5543 		/* Need callback function */
5544 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
5545 		synch = FALSE;
5546 	} else
5547 		synch = TRUE;
5548 
5549 	/* Transfer command to HBA */
5550 	if (sata_hba_start(spx, &rval) != 0) {
5551 		/* Pkt not accepted for execution */
5552 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5553 		return (rval);
5554 	}
5555 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5556 	/*
5557 	 * If execution is non-synchronous,
5558 	 * a callback function will handle potential errors, translate
5559 	 * the response and will do a callback to a target driver.
5560 	 * If it was synchronous, check execution status using the same
5561 	 * framework callback.
5562 	 */
5563 	if (synch) {
5564 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5565 		    "synchronous execution status %x\n",
5566 		    spx->txlt_sata_pkt->satapkt_reason);
5567 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
5568 	}
5569 	return (TRAN_ACCEPT);
5570 }
5571 
5572 
5573 /*
5574  * SATA translate command: Write (various types)
5575  * Translated into appropriate type of ATA WRITE command
5576  * for SATA hard disks.
5577  * Both the device capabilities and requested operation mode are
5578  * considered.
5579  *
5580  * Following scsi cdb fields are ignored:
5581  * rwprotect, dpo, fua, fua_nv, group_number.
5582  *
5583  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
5584  * appropriate values in scsi_pkt fields.
5585  */
5586 static int
5587 sata_txlt_write(sata_pkt_txlate_t *spx)
5588 {
5589 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5590 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5591 	sata_drive_info_t *sdinfo;
5592 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
5593 	int cport = SATA_TXLT_CPORT(spx);
5594 	uint16_t sec_count;
5595 	uint64_t lba;
5596 	int rval;
5597 	int synch;
5598 
5599 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5600 
5601 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
5602 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
5603 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5604 		return (rval);
5605 	}
5606 
5607 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
5608 	    &spx->txlt_sata_pkt->satapkt_device);
5609 
5610 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
5611 	/*
5612 	 * Extract LBA and sector count from scsi CDB
5613 	 */
5614 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
5615 	case SCMD_WRITE:
5616 		/* 6-byte scsi read cmd : 0x0A */
5617 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
5618 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
5619 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5620 		sec_count = scsipkt->pkt_cdbp[4];
5621 		/* sec_count 0 will be interpreted as 256 by a device */
5622 		break;
5623 	case SCMD_WRITE_G1:
5624 		/* 10-bytes scsi write command : 0x2A */
5625 		lba = scsipkt->pkt_cdbp[2];
5626 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5627 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5628 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5629 		sec_count = scsipkt->pkt_cdbp[7];
5630 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
5631 		break;
5632 	case SCMD_WRITE_G5:
5633 		/* 12-bytes scsi read command : 0xAA */
5634 		lba = scsipkt->pkt_cdbp[2];
5635 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5636 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5637 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5638 		sec_count = scsipkt->pkt_cdbp[6];
5639 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
5640 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
5641 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
5642 		break;
5643 	case SCMD_WRITE_G4:
5644 		/* 16-bytes scsi write command : 0x8A */
5645 		lba = scsipkt->pkt_cdbp[2];
5646 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5647 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5648 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5649 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
5650 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5651 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5652 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
5653 		sec_count = scsipkt->pkt_cdbp[10];
5654 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
5655 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
5656 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
5657 		break;
5658 	default:
5659 		/* Unsupported command */
5660 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5661 		return (sata_txlt_invalid_command(spx));
5662 	}
5663 
5664 	/*
5665 	 * Check if specified address and length exceeds device capacity
5666 	 */
5667 	if ((lba >= sdinfo->satadrv_capacity) ||
5668 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
5669 		/* LBA out of range */
5670 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5671 		return (sata_txlt_lba_out_of_range(spx));
5672 	}
5673 
5674 	/*
5675 	 * For zero-length transfer, emulate good completion of the command
5676 	 * (reasons for rejecting the command were already checked).
5677 	 * No DMA resources were allocated.
5678 	 */
5679 	if (spx->txlt_dma_cookie_list == NULL) {
5680 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5681 		return (sata_emul_rw_completion(spx));
5682 	}
5683 
5684 	/*
5685 	 * Build cmd block depending on the device capability and
5686 	 * requested operation mode.
5687 	 * Do not bother with non-dma mode- we are working only with
5688 	 * devices supporting DMA.
5689 	 */
5690 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
5691 	scmd->satacmd_device_reg = SATA_ADH_LBA;
5692 	scmd->satacmd_cmd_reg = SATAC_WRITE_DMA;
5693 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
5694 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
5695 		scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT;
5696 		scmd->satacmd_sec_count_msb = sec_count >> 8;
5697 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
5698 #ifndef __lock_lint
5699 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
5700 		scmd->satacmd_lba_high_msb = lba >> 40;
5701 #endif
5702 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
5703 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
5704 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
5705 	}
5706 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
5707 	scmd->satacmd_lba_low_lsb = lba & 0xff;
5708 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
5709 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
5710 	scmd->satacmd_features_reg = 0;
5711 	scmd->satacmd_status_reg = 0;
5712 	scmd->satacmd_error_reg = 0;
5713 
5714 	/*
5715 	 * Check if queueing commands should be used and switch
5716 	 * to appropriate command if possible
5717 	 */
5718 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
5719 		boolean_t using_queuing;
5720 
5721 		/* Queuing supported by controller and device? */
5722 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
5723 		    (sdinfo->satadrv_features_support &
5724 		    SATA_DEV_F_NCQ) &&
5725 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
5726 		    SATA_CTLF_NCQ)) {
5727 			using_queuing = B_TRUE;
5728 
5729 			/* NCQ supported - use FPDMA WRITE */
5730 			scmd->satacmd_cmd_reg =
5731 			    SATAC_WRITE_FPDMA_QUEUED;
5732 			scmd->satacmd_features_reg_ext =
5733 			    scmd->satacmd_sec_count_msb;
5734 			scmd->satacmd_sec_count_msb = 0;
5735 		} else if ((sdinfo->satadrv_features_support &
5736 		    SATA_DEV_F_TCQ) &&
5737 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
5738 		    SATA_CTLF_QCMD)) {
5739 			using_queuing = B_TRUE;
5740 
5741 			/* Legacy queueing */
5742 			if (sdinfo->satadrv_features_support &
5743 			    SATA_DEV_F_LBA48) {
5744 				scmd->satacmd_cmd_reg =
5745 				    SATAC_WRITE_DMA_QUEUED_EXT;
5746 				scmd->satacmd_features_reg_ext =
5747 				    scmd->satacmd_sec_count_msb;
5748 				scmd->satacmd_sec_count_msb = 0;
5749 			} else {
5750 				scmd->satacmd_cmd_reg =
5751 				    SATAC_WRITE_DMA_QUEUED;
5752 			}
5753 		} else	/* Queuing not supported */
5754 			using_queuing = B_FALSE;
5755 
5756 		if (using_queuing) {
5757 			scmd->satacmd_features_reg =
5758 			    scmd->satacmd_sec_count_lsb;
5759 			scmd->satacmd_sec_count_lsb = 0;
5760 			scmd->satacmd_flags.sata_queued = B_TRUE;
5761 		}
5762 	}
5763 
5764 	SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5765 	    "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n",
5766 	    scmd->satacmd_cmd_reg, lba, sec_count);
5767 
5768 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5769 		/* Need callback function */
5770 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
5771 		synch = FALSE;
5772 	} else
5773 		synch = TRUE;
5774 
5775 	/* Transfer command to HBA */
5776 	if (sata_hba_start(spx, &rval) != 0) {
5777 		/* Pkt not accepted for execution */
5778 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5779 		return (rval);
5780 	}
5781 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5782 
5783 	/*
5784 	 * If execution is non-synchronous,
5785 	 * a callback function will handle potential errors, translate
5786 	 * the response and will do a callback to a target driver.
5787 	 * If it was synchronous, check execution status using the same
5788 	 * framework callback.
5789 	 */
5790 	if (synch) {
5791 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5792 		    "synchronous execution status %x\n",
5793 		    spx->txlt_sata_pkt->satapkt_reason);
5794 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
5795 	}
5796 	return (TRAN_ACCEPT);
5797 }
5798 
5799 
5800 /*
5801  * Implements SCSI SBC WRITE BUFFER command download microcode option
5802  */
5803 static int
5804 sata_txlt_write_buffer(sata_pkt_txlate_t *spx)
5805 {
5806 #define	WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE			4
5807 #define	WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE			5
5808 
5809 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5810 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5811 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5812 	struct scsi_extended_sense *sense;
5813 	int rval, mode, sector_count;
5814 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
5815 	int cport = SATA_TXLT_CPORT(spx);
5816 	boolean_t synch;
5817 
5818 	synch = (spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH) != 0;
5819 	mode = scsipkt->pkt_cdbp[1] & 0x1f;
5820 
5821 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5822 	    "sata_txlt_write_buffer, mode 0x%x\n", mode);
5823 
5824 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5825 
5826 	if ((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) {
5827 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5828 		return (rval);
5829 	}
5830 
5831 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
5832 
5833 	scsipkt->pkt_reason = CMD_CMPLT;
5834 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5835 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5836 
5837 	/*
5838 	 * The SCSI to ATA translation specification only calls
5839 	 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE.
5840 	 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but
5841 	 * ATA 8 (draft) got rid of download microcode for temp
5842 	 * and it is even optional for ATA 7, so it may be aborted.
5843 	 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as
5844 	 * it is not specified and the buffer offset for SCSI is a 16-bit
5845 	 * value in bytes, but for ATA it is a 16-bit offset in 512 byte
5846 	 * sectors.  Thus the offset really doesn't buy us anything.
5847 	 * If and when ATA 8 is stabilized and the SCSI to ATA specification
5848 	 * is revised, this can be revisisted.
5849 	 */
5850 	/* Reject not supported request */
5851 	switch (mode) {
5852 	case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE:
5853 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP;
5854 		break;
5855 	case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE:
5856 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE;
5857 		break;
5858 	default:
5859 		goto bad_param;
5860 	}
5861 
5862 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
5863 
5864 	scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE;
5865 	if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0)
5866 		goto bad_param;
5867 	sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE;
5868 	scmd->satacmd_sec_count_lsb = (uint8_t)sector_count;
5869 	scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8;
5870 	scmd->satacmd_lba_mid_lsb = 0;
5871 	scmd->satacmd_lba_high_lsb = 0;
5872 	scmd->satacmd_device_reg = 0;
5873 	spx->txlt_sata_pkt->satapkt_comp =
5874 	    sata_txlt_download_mcode_cmd_completion;
5875 	scmd->satacmd_addr_type = 0;
5876 
5877 	/* Transfer command to HBA */
5878 	if (sata_hba_start(spx, &rval) != 0) {
5879 		/* Pkt not accepted for execution */
5880 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5881 		return (rval);
5882 	}
5883 
5884 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5885 	/*
5886 	 * If execution is non-synchronous,
5887 	 * a callback function will handle potential errors, translate
5888 	 * the response and will do a callback to a target driver.
5889 	 * If it was synchronous, check execution status using the same
5890 	 * framework callback.
5891 	 */
5892 	if (synch) {
5893 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5894 		    "synchronous execution\n", NULL);
5895 		/* Calling pre-set completion routine */
5896 		(*spx->txlt_sata_pkt->satapkt_comp)(spx->txlt_sata_pkt);
5897 	}
5898 	return (TRAN_ACCEPT);
5899 
5900 bad_param:
5901 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5902 	*scsipkt->pkt_scbp = STATUS_CHECK;
5903 	sense = sata_arq_sense(spx);
5904 	sense->es_key = KEY_ILLEGAL_REQUEST;
5905 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5906 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5907 	    scsipkt->pkt_comp != NULL) {
5908 		/* scsi callback required */
5909 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5910 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5911 		    TQ_SLEEP) == 0) {
5912 			/* Scheduling the callback failed */
5913 			rval = TRAN_BUSY;
5914 		}
5915 	}
5916 	return (rval);
5917 }
5918 
5919 
5920 /*
5921  * Retry identify device when command returns SATA_INCOMPLETE_DATA
5922  * after doing a firmware download.
5923  */
5924 static void
5925 sata_retry_identify_device(void *arg)
5926 {
5927 #define	DOWNLOAD_WAIT_TIME_SECS	60
5928 #define	DOWNLOAD_WAIT_INTERVAL_SECS	1
5929 	int rval;
5930 	int retry_cnt;
5931 	sata_pkt_t *sata_pkt = (sata_pkt_t *)arg;
5932 	sata_pkt_txlate_t *spx =
5933 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
5934 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5935 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
5936 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
5937 	sata_drive_info_t *sdinfo;
5938 
5939 	/*
5940 	 * Before returning good status, probe device.
5941 	 * Device probing will get IDENTIFY DEVICE data, if possible.
5942 	 * The assumption is that the new microcode is applied by the
5943 	 * device. It is a caller responsibility to verify this.
5944 	 */
5945 	for (retry_cnt = 0;
5946 	    retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS;
5947 	    retry_cnt++) {
5948 		rval = sata_probe_device(sata_hba_inst, &sata_device);
5949 
5950 		if (rval == SATA_SUCCESS) { /* Set default features */
5951 			sdinfo = sata_get_device_info(sata_hba_inst,
5952 			    &sata_device);
5953 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
5954 			    SATA_SUCCESS) {
5955 				/* retry */
5956 				(void) sata_initialize_device(sata_hba_inst,
5957 				    sdinfo);
5958 			}
5959 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5960 			    scsipkt->pkt_comp != NULL)
5961 				(*scsipkt->pkt_comp)(scsipkt);
5962 			return;
5963 		} else if (rval == SATA_RETRY) {
5964 			delay(drv_usectohz(1000000 *
5965 			    DOWNLOAD_WAIT_INTERVAL_SECS));
5966 			continue;
5967 		} else	/* failed - no reason to retry */
5968 			break;
5969 	}
5970 
5971 	/*
5972 	 * Something went wrong, device probing failed.
5973 	 */
5974 	SATA_LOG_D((sata_hba_inst, CE_WARN,
5975 	    "Cannot probe device after downloading microcode\n"));
5976 
5977 	/* Reset device to force retrying the probe. */
5978 	(void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst))
5979 	    (SATA_DIP(sata_hba_inst), &sata_device);
5980 
5981 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5982 	    scsipkt->pkt_comp != NULL)
5983 		(*scsipkt->pkt_comp)(scsipkt);
5984 }
5985 
5986 /*
5987  * Translate completion status of download microcode command.
5988  * pkt completion_reason is checked to determine the completion status.
5989  * Do scsi callback if necessary (FLAG_NOINTR == 0)
5990  *
5991  * Note: this function may be called also for synchronously executed
5992  * command.
5993  * This function may be used only if scsi_pkt is non-NULL.
5994  */
5995 static void
5996 sata_txlt_download_mcode_cmd_completion(sata_pkt_t *sata_pkt)
5997 {
5998 	sata_pkt_txlate_t *spx =
5999 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
6000 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6001 	struct scsi_extended_sense *sense;
6002 	sata_drive_info_t *sdinfo;
6003 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
6004 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
6005 	int rval;
6006 
6007 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6008 	    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
6009 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
6010 		scsipkt->pkt_reason = CMD_CMPLT;
6011 
6012 		rval = sata_probe_device(sata_hba_inst, &sata_device);
6013 
6014 		if (rval == SATA_SUCCESS) { /* Set default features */
6015 			sdinfo = sata_get_device_info(sata_hba_inst,
6016 			    &sata_device);
6017 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
6018 			    SATA_SUCCESS) {
6019 				/* retry */
6020 				(void) sata_initialize_device(sata_hba_inst,
6021 				    sdinfo);
6022 			}
6023 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6024 			    scsipkt->pkt_comp != NULL)
6025 				(*scsipkt->pkt_comp)(scsipkt);
6026 		} else {
6027 			(void) ddi_taskq_dispatch(
6028 			    (ddi_taskq_t *)SATA_TXLT_TASKQ(spx),
6029 			    sata_retry_identify_device,
6030 			    (void *)sata_pkt, TQ_NOSLEEP);
6031 		}
6032 
6033 
6034 	} else {
6035 		/* Something went wrong, microcode download command failed */
6036 		scsipkt->pkt_reason = CMD_INCOMPLETE;
6037 		*scsipkt->pkt_scbp = STATUS_CHECK;
6038 		sense = sata_arq_sense(spx);
6039 		switch (sata_pkt->satapkt_reason) {
6040 		case SATA_PKT_PORT_ERROR:
6041 			/*
6042 			 * We have no device data. Assume no data transfered.
6043 			 */
6044 			sense->es_key = KEY_HARDWARE_ERROR;
6045 			break;
6046 
6047 		case SATA_PKT_DEV_ERROR:
6048 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
6049 			    SATA_STATUS_ERR) {
6050 				/*
6051 				 * determine dev error reason from error
6052 				 * reg content
6053 				 */
6054 				sata_decode_device_error(spx, sense);
6055 				break;
6056 			}
6057 			/* No extended sense key - no info available */
6058 			break;
6059 
6060 		case SATA_PKT_TIMEOUT:
6061 			/* scsipkt->pkt_reason = CMD_TIMEOUT; */
6062 			scsipkt->pkt_reason = CMD_INCOMPLETE;
6063 			/* No extended sense key ? */
6064 			break;
6065 
6066 		case SATA_PKT_ABORTED:
6067 			scsipkt->pkt_reason = CMD_ABORTED;
6068 			/* No extended sense key ? */
6069 			break;
6070 
6071 		case SATA_PKT_RESET:
6072 			/* pkt aborted by an explicit reset from a host */
6073 			scsipkt->pkt_reason = CMD_RESET;
6074 			break;
6075 
6076 		default:
6077 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
6078 			    "sata_txlt_nodata_cmd_completion: "
6079 			    "invalid packet completion reason %d",
6080 			    sata_pkt->satapkt_reason));
6081 			scsipkt->pkt_reason = CMD_TRAN_ERR;
6082 			break;
6083 		}
6084 
6085 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6086 		    "scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6087 
6088 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6089 		    scsipkt->pkt_comp != NULL)
6090 			/* scsi callback required */
6091 			(*scsipkt->pkt_comp)(scsipkt);
6092 	}
6093 }
6094 
6095 
6096 
6097 
6098 /*
6099  * Translate command: Synchronize Cache.
6100  * Translates into Flush Cache command for SATA hard disks.
6101  *
6102  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6103  * appropriate values in scsi_pkt fields.
6104  */
6105 static 	int
6106 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx)
6107 {
6108 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6109 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
6110 	int cport = SATA_TXLT_CPORT(spx);
6111 	int rval;
6112 	int synch;
6113 
6114 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
6115 
6116 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
6117 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
6118 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6119 		return (rval);
6120 	}
6121 
6122 	scmd->satacmd_addr_type = 0;
6123 	scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE;
6124 	scmd->satacmd_device_reg = 0;
6125 	scmd->satacmd_sec_count_lsb = 0;
6126 	scmd->satacmd_lba_low_lsb = 0;
6127 	scmd->satacmd_lba_mid_lsb = 0;
6128 	scmd->satacmd_lba_high_lsb = 0;
6129 	scmd->satacmd_features_reg = 0;
6130 	scmd->satacmd_status_reg = 0;
6131 	scmd->satacmd_error_reg = 0;
6132 
6133 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6134 	    "sata_txlt_synchronize_cache\n", NULL);
6135 
6136 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6137 		/* Need to set-up a callback function */
6138 		spx->txlt_sata_pkt->satapkt_comp =
6139 		    sata_txlt_nodata_cmd_completion;
6140 		synch = FALSE;
6141 	} else
6142 		synch = TRUE;
6143 
6144 	/* Transfer command to HBA */
6145 	if (sata_hba_start(spx, &rval) != 0) {
6146 		/* Pkt not accepted for execution */
6147 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
6148 		return (rval);
6149 	}
6150 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
6151 
6152 	/*
6153 	 * If execution non-synchronous, it had to be completed
6154 	 * a callback function will handle potential errors, translate
6155 	 * the response and will do a callback to a target driver.
6156 	 * If it was synchronous, check status, using the same
6157 	 * framework callback.
6158 	 */
6159 	if (synch) {
6160 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6161 		    "synchronous execution status %x\n",
6162 		    spx->txlt_sata_pkt->satapkt_reason);
6163 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
6164 	}
6165 	return (TRAN_ACCEPT);
6166 }
6167 
6168 
6169 /*
6170  * Send pkt to SATA HBA driver
6171  *
6172  * This function may be called only if the operation is requested by scsi_pkt,
6173  * i.e. scsi_pkt is not NULL.
6174  *
6175  * This function has to be called with cport mutex held. It does release
6176  * the mutex when it calls HBA driver sata_tran_start function and
6177  * re-acquires it afterwards.
6178  *
6179  * If return value is 0, pkt was accepted, -1 otherwise
6180  * rval is set to appropriate sata_scsi_start return value.
6181  *
6182  * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not
6183  * have called the sata_pkt callback function for this packet.
6184  *
6185  * The scsi callback has to be performed by the caller of this routine.
6186  *
6187  * Note 2: No port multiplier support for now.
6188  */
6189 static int
6190 sata_hba_start(sata_pkt_txlate_t *spx, int *rval)
6191 {
6192 	int stat, cport;
6193 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
6194 	sata_drive_info_t *sdinfo;
6195 	sata_device_t *sata_device;
6196 	uint8_t cmd;
6197 	struct sata_cmd_flags cmd_flags;
6198 
6199 	ASSERT(spx->txlt_sata_pkt != NULL);
6200 
6201 	cport = SATA_TXLT_CPORT(spx);
6202 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6203 
6204 	sdinfo = sata_get_device_info(sata_hba_inst,
6205 	    &spx->txlt_sata_pkt->satapkt_device);
6206 	ASSERT(sdinfo != NULL);
6207 
6208 	/* Clear device reset state? */
6209 	if (sdinfo->satadrv_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) {
6210 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
6211 		    sata_clear_dev_reset = B_TRUE;
6212 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_CLEAR_DEVICE_RESET;
6213 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6214 		    "sata_hba_start: clearing device reset state\n", NULL);
6215 	}
6216 	cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg;
6217 	cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags;
6218 	sata_device = &spx->txlt_sata_pkt->satapkt_device;
6219 
6220 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6221 
6222 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6223 	    "Sata cmd 0x%2x\n", cmd);
6224 
6225 	stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
6226 	    spx->txlt_sata_pkt);
6227 
6228 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6229 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
6230 	/*
6231 	 * If sata pkt was accepted and executed in asynchronous mode, i.e.
6232 	 * with the sata callback, the sata_pkt could be already destroyed
6233 	 * by the time we check ther return status from the hba_start()
6234 	 * function, because sata_scsi_destroy_pkt() could have been already
6235 	 * called (perhaps in the interrupt context). So, in such case, there
6236 	 * should be no references to it. In other cases, sata_pkt still
6237 	 * exists.
6238 	 */
6239 	switch (stat) {
6240 	case SATA_TRAN_ACCEPTED:
6241 		/*
6242 		 * pkt accepted for execution.
6243 		 * If it was executed synchronously, it is already completed
6244 		 * and pkt completion_reason indicates completion status.
6245 		 */
6246 		*rval = TRAN_ACCEPT;
6247 		return (0);
6248 
6249 	case SATA_TRAN_QUEUE_FULL:
6250 		/*
6251 		 * Controller detected queue full condition.
6252 		 */
6253 		SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst,
6254 		    "sata_hba_start: queue full\n", NULL);
6255 
6256 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
6257 		*spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL;
6258 
6259 		*rval = TRAN_BUSY;
6260 		break;
6261 
6262 	case SATA_TRAN_PORT_ERROR:
6263 		/*
6264 		 * Communication/link with device or general port error
6265 		 * detected before pkt execution begun.
6266 		 */
6267 		if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
6268 		    SATA_ADDR_CPORT ||
6269 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
6270 		    SATA_ADDR_DCPORT)
6271 			sata_log(sata_hba_inst, CE_CONT,
6272 			    "SATA port %d error",
6273 			    sata_device->satadev_addr.cport);
6274 		else
6275 			sata_log(sata_hba_inst, CE_CONT,
6276 			    "SATA port %d pmport %d error\n",
6277 			    sata_device->satadev_addr.cport,
6278 			    sata_device->satadev_addr.pmport);
6279 
6280 		/*
6281 		 * Update the port/device structure.
6282 		 * sata_pkt should be still valid. Since port error is
6283 		 * returned, sata_device content should reflect port
6284 		 * state - it means, that sata address have been changed,
6285 		 * because original packet's sata address refered to a device
6286 		 * attached to some port.
6287 		 */
6288 		sata_update_port_info(sata_hba_inst, sata_device);
6289 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
6290 		*rval = TRAN_FATAL_ERROR;
6291 		break;
6292 
6293 	case SATA_TRAN_CMD_UNSUPPORTED:
6294 		/*
6295 		 * Command rejected by HBA as unsupported. It was HBA driver
6296 		 * that rejected the command, command was not sent to
6297 		 * an attached device.
6298 		 */
6299 		if ((sdinfo != NULL) &&
6300 		    (sdinfo->satadrv_state & SATA_DSTATE_RESET))
6301 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6302 			    "sat_hba_start: cmd 0x%2x rejected "
6303 			    "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd);
6304 
6305 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6306 		(void) sata_txlt_invalid_command(spx);
6307 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6308 
6309 		*rval = TRAN_ACCEPT;
6310 		break;
6311 
6312 	case SATA_TRAN_BUSY:
6313 		/*
6314 		 * Command rejected by HBA because other operation prevents
6315 		 * accepting the packet, or device is in RESET condition.
6316 		 */
6317 		if (sdinfo != NULL) {
6318 			sdinfo->satadrv_state =
6319 			    spx->txlt_sata_pkt->satapkt_device.satadev_state;
6320 
6321 			if (sdinfo->satadrv_state & SATA_DSTATE_RESET) {
6322 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6323 				    "sata_hba_start: cmd 0x%2x rejected "
6324 				    "because of device reset condition\n",
6325 				    cmd);
6326 			} else {
6327 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6328 				    "sata_hba_start: cmd 0x%2x rejected "
6329 				    "with SATA_TRAN_BUSY status\n",
6330 				    cmd);
6331 			}
6332 		}
6333 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
6334 		*rval = TRAN_BUSY;
6335 		break;
6336 
6337 	default:
6338 		/* Unrecognized HBA response */
6339 		SATA_LOG_D((sata_hba_inst, CE_WARN,
6340 		    "sata_hba_start: unrecognized HBA response "
6341 		    "to cmd : 0x%2x resp 0x%x", cmd, rval));
6342 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
6343 		*rval = TRAN_FATAL_ERROR;
6344 		break;
6345 	}
6346 
6347 	/*
6348 	 * If we got here, the packet was rejected.
6349 	 * Check if we need to remember reset state clearing request
6350 	 */
6351 	if (cmd_flags.sata_clear_dev_reset) {
6352 		/*
6353 		 * Check if device is still configured - it may have
6354 		 * disapeared from the configuration
6355 		 */
6356 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
6357 		if (sdinfo != NULL) {
6358 			/*
6359 			 * Restore the flag that requests clearing of
6360 			 * the device reset state,
6361 			 * so the next sata packet may carry it to HBA.
6362 			 */
6363 			sdinfo->satadrv_event_flags |=
6364 			    SATA_EVNT_CLEAR_DEVICE_RESET;
6365 		}
6366 	}
6367 	return (-1);
6368 }
6369 
6370 /*
6371  * Scsi response setup for invalid LBA
6372  *
6373  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
6374  */
6375 static int
6376 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx)
6377 {
6378 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6379 	struct scsi_extended_sense *sense;
6380 
6381 	scsipkt->pkt_reason = CMD_CMPLT;
6382 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6383 	    STATE_SENT_CMD | STATE_GOT_STATUS;
6384 	*scsipkt->pkt_scbp = STATUS_CHECK;
6385 
6386 	*scsipkt->pkt_scbp = STATUS_CHECK;
6387 	sense = sata_arq_sense(spx);
6388 	sense->es_key = KEY_ILLEGAL_REQUEST;
6389 	sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
6390 
6391 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6392 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6393 
6394 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6395 	    scsipkt->pkt_comp != NULL)
6396 		/* scsi callback required */
6397 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6398 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
6399 		    TQ_SLEEP) == NULL)
6400 			/* Scheduling the callback failed */
6401 			return (TRAN_BUSY);
6402 	return (TRAN_ACCEPT);
6403 }
6404 
6405 
6406 /*
6407  * Analyze device status and error registers and translate them into
6408  * appropriate scsi sense codes.
6409  * NOTE: non-packet commands only for now
6410  */
6411 static void
6412 sata_decode_device_error(sata_pkt_txlate_t *spx,
6413     struct scsi_extended_sense *sense)
6414 {
6415 	uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg;
6416 
6417 	ASSERT(sense != NULL);
6418 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
6419 	    SATA_STATUS_ERR);
6420 
6421 
6422 	if (err_reg & SATA_ERROR_ICRC) {
6423 		sense->es_key = KEY_ABORTED_COMMAND;
6424 		sense->es_add_code = 0x08; /* Communication failure */
6425 		return;
6426 	}
6427 
6428 	if (err_reg & SATA_ERROR_UNC) {
6429 		sense->es_key = KEY_MEDIUM_ERROR;
6430 		/* Information bytes (LBA) need to be set by a caller */
6431 		return;
6432 	}
6433 
6434 	/* ADD HERE: MC error bit handling for ATAPI CD/DVD */
6435 	if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) {
6436 		sense->es_key = KEY_UNIT_ATTENTION;
6437 		sense->es_add_code = 0x3a; /* No media present */
6438 		return;
6439 	}
6440 
6441 	if (err_reg & SATA_ERROR_IDNF) {
6442 		if (err_reg & SATA_ERROR_ABORT) {
6443 			sense->es_key = KEY_ABORTED_COMMAND;
6444 		} else {
6445 			sense->es_key = KEY_ILLEGAL_REQUEST;
6446 			sense->es_add_code = 0x21; /* LBA out of range */
6447 		}
6448 		return;
6449 	}
6450 
6451 	if (err_reg & SATA_ERROR_ABORT) {
6452 		ASSERT(spx->txlt_sata_pkt != NULL);
6453 		sense->es_key = KEY_ABORTED_COMMAND;
6454 		return;
6455 	}
6456 }
6457 
6458 /*
6459  * Extract error LBA from sata_pkt.satapkt_cmd register fields
6460  */
6461 static void
6462 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba)
6463 {
6464 	sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd;
6465 
6466 	*lba = 0;
6467 	if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) {
6468 		*lba = sata_cmd->satacmd_lba_high_msb;
6469 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb;
6470 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb;
6471 	} else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) {
6472 		*lba = sata_cmd->satacmd_device_reg & 0xf;
6473 	}
6474 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb;
6475 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb;
6476 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb;
6477 }
6478 
6479 /*
6480  * This is fixed sense format - if LBA exceeds the info field size,
6481  * no valid info will be returned (valid bit in extended sense will
6482  * be set to 0).
6483  */
6484 static struct scsi_extended_sense *
6485 sata_arq_sense(sata_pkt_txlate_t *spx)
6486 {
6487 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6488 	struct scsi_arq_status *arqs;
6489 	struct scsi_extended_sense *sense;
6490 
6491 	/* Fill ARQ sense data */
6492 	scsipkt->pkt_state |= STATE_ARQ_DONE;
6493 	arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp;
6494 	*(uchar_t *)&arqs->sts_status = STATUS_CHECK;
6495 	*(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD;
6496 	arqs->sts_rqpkt_reason = CMD_CMPLT;
6497 	arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6498 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
6499 	arqs->sts_rqpkt_resid = 0;
6500 	sense = &arqs->sts_sensedata;
6501 	bzero(sense, sizeof (struct scsi_extended_sense));
6502 	sata_fixed_sense_data_preset(sense);
6503 	return (sense);
6504 }
6505 
6506 
6507 /*
6508  * Emulated SATA Read/Write command completion for zero-length requests.
6509  * This request always succedes, so in synchronous mode it always returns
6510  * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the
6511  * callback cannot be scheduled.
6512  */
6513 static int
6514 sata_emul_rw_completion(sata_pkt_txlate_t *spx)
6515 {
6516 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6517 
6518 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6519 	    STATE_SENT_CMD | STATE_GOT_STATUS;
6520 	scsipkt->pkt_reason = CMD_CMPLT;
6521 	*scsipkt->pkt_scbp = STATUS_GOOD;
6522 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6523 		/* scsi callback required - have to schedule it */
6524 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6525 		    (task_func_t *)scsipkt->pkt_comp,
6526 		    (void *)scsipkt, TQ_SLEEP) == NULL)
6527 			/* Scheduling the callback failed */
6528 			return (TRAN_BUSY);
6529 	}
6530 	return (TRAN_ACCEPT);
6531 }
6532 
6533 
6534 /*
6535  * Translate completion status of SATA read/write commands into scsi response.
6536  * pkt completion_reason is checked to determine the completion status.
6537  * Do scsi callback if necessary.
6538  *
6539  * Note: this function may be called also for synchronously executed
6540  * commands.
6541  * This function may be used only if scsi_pkt is non-NULL.
6542  */
6543 static void
6544 sata_txlt_rw_completion(sata_pkt_t *sata_pkt)
6545 {
6546 	sata_pkt_txlate_t *spx =
6547 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
6548 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
6549 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6550 	struct scsi_extended_sense *sense;
6551 	uint64_t lba;
6552 	struct buf *bp;
6553 	int rval;
6554 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
6555 		/* Normal completion */
6556 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6557 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
6558 		scsipkt->pkt_reason = CMD_CMPLT;
6559 		*scsipkt->pkt_scbp = STATUS_GOOD;
6560 		if (spx->txlt_tmp_buf != NULL) {
6561 			/* Temporary buffer was used */
6562 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
6563 			if (bp->b_flags & B_READ) {
6564 				rval = ddi_dma_sync(
6565 				    spx->txlt_buf_dma_handle, 0, 0,
6566 				    DDI_DMA_SYNC_FORCPU);
6567 				ASSERT(rval == DDI_SUCCESS);
6568 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
6569 				    bp->b_bcount);
6570 			}
6571 		}
6572 	} else {
6573 		/*
6574 		 * Something went wrong - analyze return
6575 		 */
6576 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6577 		    STATE_SENT_CMD | STATE_GOT_STATUS;
6578 		scsipkt->pkt_reason = CMD_INCOMPLETE;
6579 		*scsipkt->pkt_scbp = STATUS_CHECK;
6580 		sense = sata_arq_sense(spx);
6581 		ASSERT(sense != NULL);
6582 
6583 		/*
6584 		 * SATA_PKT_DEV_ERROR is the only case where we may be able to
6585 		 * extract from device registers the failing LBA.
6586 		 */
6587 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
6588 			if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) &&
6589 			    (scmd->satacmd_lba_mid_msb != 0 ||
6590 			    scmd->satacmd_lba_high_msb != 0)) {
6591 				/*
6592 				 * We have problem reporting this cmd LBA
6593 				 * in fixed sense data format, because of
6594 				 * the size of the scsi LBA fields.
6595 				 */
6596 				sense->es_valid = 0;
6597 			} else {
6598 				sata_extract_error_lba(spx, &lba);
6599 				sense->es_info_1 = (lba & 0xFF000000) >> 24;
6600 				sense->es_info_2 = (lba & 0xFF0000) >> 16;
6601 				sense->es_info_3 = (lba & 0xFF00) >> 8;
6602 				sense->es_info_4 = lba & 0xFF;
6603 			}
6604 		} else {
6605 			/* Invalid extended sense info */
6606 			sense->es_valid = 0;
6607 		}
6608 
6609 		switch (sata_pkt->satapkt_reason) {
6610 		case SATA_PKT_PORT_ERROR:
6611 			/* We may want to handle DEV GONE state as well */
6612 			/*
6613 			 * We have no device data. Assume no data transfered.
6614 			 */
6615 			sense->es_key = KEY_HARDWARE_ERROR;
6616 			break;
6617 
6618 		case SATA_PKT_DEV_ERROR:
6619 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
6620 			    SATA_STATUS_ERR) {
6621 				/*
6622 				 * determine dev error reason from error
6623 				 * reg content
6624 				 */
6625 				sata_decode_device_error(spx, sense);
6626 				if (sense->es_key == KEY_MEDIUM_ERROR) {
6627 					switch (scmd->satacmd_cmd_reg) {
6628 					case SATAC_READ_DMA:
6629 					case SATAC_READ_DMA_EXT:
6630 					case SATAC_READ_DMA_QUEUED:
6631 					case SATAC_READ_DMA_QUEUED_EXT:
6632 					case SATAC_READ_FPDMA_QUEUED:
6633 						/* Unrecovered read error */
6634 						sense->es_add_code =
6635 						SD_SCSI_ASC_UNREC_READ_ERROR;
6636 						break;
6637 					case SATAC_WRITE_DMA:
6638 					case SATAC_WRITE_DMA_EXT:
6639 					case SATAC_WRITE_DMA_QUEUED:
6640 					case SATAC_WRITE_DMA_QUEUED_EXT:
6641 					case SATAC_WRITE_FPDMA_QUEUED:
6642 						/* Write error */
6643 						sense->es_add_code =
6644 						    SD_SCSI_ASC_WRITE_ERROR;
6645 						break;
6646 					default:
6647 						/* Internal error */
6648 						SATA_LOG_D((
6649 						    spx->txlt_sata_hba_inst,
6650 						    CE_WARN,
6651 						    "sata_txlt_rw_completion :"
6652 						    "internal error - invalid "
6653 						    "command 0x%2x",
6654 						    scmd->satacmd_cmd_reg));
6655 						break;
6656 					}
6657 				}
6658 				break;
6659 			}
6660 			/* No extended sense key - no info available */
6661 			scsipkt->pkt_reason = CMD_INCOMPLETE;
6662 			break;
6663 
6664 		case SATA_PKT_TIMEOUT:
6665 			/* scsipkt->pkt_reason = CMD_TIMEOUT; */
6666 			scsipkt->pkt_reason = CMD_INCOMPLETE;
6667 			/* No extended sense key ? */
6668 			break;
6669 
6670 		case SATA_PKT_ABORTED:
6671 			scsipkt->pkt_reason = CMD_ABORTED;
6672 			/* No extended sense key ? */
6673 			break;
6674 
6675 		case SATA_PKT_RESET:
6676 			scsipkt->pkt_reason = CMD_RESET;
6677 			break;
6678 
6679 		default:
6680 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
6681 			    "sata_txlt_rw_completion: "
6682 			    "invalid packet completion reason"));
6683 			scsipkt->pkt_reason = CMD_TRAN_ERR;
6684 			break;
6685 		}
6686 	}
6687 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6688 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6689 
6690 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6691 	    scsipkt->pkt_comp != NULL)
6692 		/* scsi callback required */
6693 		(*scsipkt->pkt_comp)(scsipkt);
6694 }
6695 
6696 
6697 /*
6698  * Translate completion status of non-data commands (i.e. commands returning
6699  * no data).
6700  * pkt completion_reason is checked to determine the completion status.
6701  * Do scsi callback if necessary (FLAG_NOINTR == 0)
6702  *
6703  * Note: this function may be called also for synchronously executed
6704  * commands.
6705  * This function may be used only if scsi_pkt is non-NULL.
6706  */
6707 
6708 static 	void
6709 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt)
6710 {
6711 	sata_pkt_txlate_t *spx =
6712 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
6713 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6714 	struct scsi_extended_sense *sense;
6715 
6716 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6717 	    STATE_SENT_CMD | STATE_GOT_STATUS;
6718 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
6719 		/* Normal completion */
6720 		scsipkt->pkt_reason = CMD_CMPLT;
6721 		*scsipkt->pkt_scbp = STATUS_GOOD;
6722 	} else {
6723 		/* Something went wrong */
6724 		scsipkt->pkt_reason = CMD_INCOMPLETE;
6725 		*scsipkt->pkt_scbp = STATUS_CHECK;
6726 		sense = sata_arq_sense(spx);
6727 		switch (sata_pkt->satapkt_reason) {
6728 		case SATA_PKT_PORT_ERROR:
6729 			/*
6730 			 * We have no device data. Assume no data transfered.
6731 			 */
6732 			sense->es_key = KEY_HARDWARE_ERROR;
6733 			break;
6734 
6735 		case SATA_PKT_DEV_ERROR:
6736 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
6737 			    SATA_STATUS_ERR) {
6738 				/*
6739 				 * determine dev error reason from error
6740 				 * reg content
6741 				 */
6742 				sata_decode_device_error(spx, sense);
6743 				break;
6744 			}
6745 			/* No extended sense key - no info available */
6746 			break;
6747 
6748 		case SATA_PKT_TIMEOUT:
6749 			/* scsipkt->pkt_reason = CMD_TIMEOUT; */
6750 			scsipkt->pkt_reason = CMD_INCOMPLETE;
6751 			/* No extended sense key ? */
6752 			break;
6753 
6754 		case SATA_PKT_ABORTED:
6755 			scsipkt->pkt_reason = CMD_ABORTED;
6756 			/* No extended sense key ? */
6757 			break;
6758 
6759 		case SATA_PKT_RESET:
6760 			/* pkt aborted by an explicit reset from a host */
6761 			scsipkt->pkt_reason = CMD_RESET;
6762 			break;
6763 
6764 		default:
6765 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
6766 			    "sata_txlt_nodata_cmd_completion: "
6767 			    "invalid packet completion reason %d",
6768 			    sata_pkt->satapkt_reason));
6769 			scsipkt->pkt_reason = CMD_TRAN_ERR;
6770 			break;
6771 		}
6772 
6773 	}
6774 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6775 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6776 
6777 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6778 	    scsipkt->pkt_comp != NULL)
6779 		/* scsi callback required */
6780 		(*scsipkt->pkt_comp)(scsipkt);
6781 }
6782 
6783 
6784 /*
6785  * Build Mode sense R/W recovery page
6786  * NOT IMPLEMENTED
6787  */
6788 
6789 static int
6790 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6791 {
6792 #ifndef __lock_lint
6793 	_NOTE(ARGUNUSED(sdinfo))
6794 	_NOTE(ARGUNUSED(pcntrl))
6795 	_NOTE(ARGUNUSED(buf))
6796 #endif
6797 	return (0);
6798 }
6799 
6800 /*
6801  * Build Mode sense caching page  -  scsi-3 implementation.
6802  * Page length distinguishes previous format from scsi-3 format.
6803  * buf must have space for 0x12 bytes.
6804  * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable.
6805  *
6806  */
6807 static int
6808 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6809 {
6810 	struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf;
6811 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6812 
6813 	/*
6814 	 * Most of the fields are set to 0, being not supported and/or disabled
6815 	 */
6816 	bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3);
6817 
6818 	/* Saved paramters not supported */
6819 	if (pcntrl == 3)
6820 		return (0);
6821 	if (pcntrl == 0 || pcntrl == 2) {
6822 		/*
6823 		 * For now treat current and default parameters as same
6824 		 * That may have to change, if target driver will complain
6825 		 */
6826 		page->mode_page.code = MODEPAGE_CACHING;	/* PS = 0 */
6827 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
6828 
6829 		if ((sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) &&
6830 		    !(sata_id->ai_features85 & SATA_LOOK_AHEAD)) {
6831 			page->dra = 1;		/* Read Ahead disabled */
6832 			page->rcd = 1;		/* Read Cache disabled */
6833 		}
6834 		if ((sata_id->ai_cmdset82 & SATA_WRITE_CACHE) &&
6835 		    (sata_id->ai_features85 & SATA_WRITE_CACHE))
6836 			page->wce = 1;		/* Write Cache enabled */
6837 	} else {
6838 		/* Changeable parameters */
6839 		page->mode_page.code = MODEPAGE_CACHING;
6840 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
6841 		if (sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) {
6842 			page->dra = 1;
6843 			page->rcd = 1;
6844 		}
6845 		if (sata_id->ai_cmdset82 & SATA_WRITE_CACHE)
6846 			page->wce = 1;
6847 	}
6848 	return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
6849 	    sizeof (struct mode_page));
6850 }
6851 
6852 /*
6853  * Build Mode sense exception cntrl page
6854  */
6855 static int
6856 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6857 {
6858 	struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf;
6859 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6860 
6861 	/*
6862 	 * Most of the fields are set to 0, being not supported and/or disabled
6863 	 */
6864 	bzero(buf, PAGELENGTH_INFO_EXCPT);
6865 
6866 	page->mode_page.code = MODEPAGE_INFO_EXCPT;
6867 	page->mode_page.length = PAGELENGTH_INFO_EXCPT;
6868 
6869 	/* Indicate that this is page is saveable */
6870 	page->mode_page.ps = 1;
6871 
6872 	/*
6873 	 * We will return the same data for default, current and saved page.
6874 	 * The only changeable bit is dexcpt and that bit is required
6875 	 * by the ATA specification to be preserved across power cycles.
6876 	 */
6877 	if (pcntrl != 1) {
6878 		page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED);
6879 		page->mrie = MRIE_ONLY_ON_REQUEST;
6880 	}
6881 	else
6882 		page->dexcpt = 1;	/* Only changeable parameter */
6883 
6884 	return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_info_excpt_page));
6885 }
6886 
6887 
6888 static int
6889 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6890 {
6891 	struct mode_acoustic_management *page =
6892 	    (struct mode_acoustic_management *)buf;
6893 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6894 
6895 	/*
6896 	 * Most of the fields are set to 0, being not supported and/or disabled
6897 	 */
6898 	bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT);
6899 
6900 	switch (pcntrl) {
6901 	case P_CNTRL_DEFAULT:
6902 		/*  default paramters not supported */
6903 		return (0);
6904 
6905 	case P_CNTRL_CURRENT:
6906 	case P_CNTRL_SAVED:
6907 		/* Saved and current are supported and are identical */
6908 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
6909 		page->mode_page.length =
6910 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
6911 		page->mode_page.ps = 1;
6912 
6913 		/* Word 83 indicates if feature is supported */
6914 		/* If feature is not supported */
6915 		if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) {
6916 			page->acoustic_manag_enable =
6917 			    ACOUSTIC_DISABLED;
6918 		} else {
6919 			page->acoustic_manag_enable =
6920 			    ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT)
6921 			    != 0);
6922 			/* Word 94 inidicates the value */
6923 #ifdef	_LITTLE_ENDIAN
6924 			page->acoustic_manag_level =
6925 			    (uchar_t)sata_id->ai_acoustic;
6926 			page->vendor_recommended_value =
6927 			    sata_id->ai_acoustic >> 8;
6928 #else
6929 			page->acoustic_manag_level =
6930 			    sata_id->ai_acoustic >> 8;
6931 			page->vendor_recommended_value =
6932 			    (uchar_t)sata_id->ai_acoustic;
6933 #endif
6934 		}
6935 		break;
6936 
6937 	case P_CNTRL_CHANGEABLE:
6938 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
6939 		page->mode_page.length =
6940 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
6941 		page->mode_page.ps = 1;
6942 
6943 		/* Word 83 indicates if the feature is supported */
6944 		if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) {
6945 			page->acoustic_manag_enable =
6946 			    ACOUSTIC_ENABLED;
6947 			page->acoustic_manag_level = 0xff;
6948 		}
6949 		break;
6950 	}
6951 	return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6952 	    sizeof (struct mode_page));
6953 }
6954 
6955 
6956 /*
6957  * Build Mode sense power condition page
6958  * NOT IMPLEMENTED.
6959  */
6960 static int
6961 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6962 {
6963 #ifndef __lock_lint
6964 	_NOTE(ARGUNUSED(sdinfo))
6965 	_NOTE(ARGUNUSED(pcntrl))
6966 	_NOTE(ARGUNUSED(buf))
6967 #endif
6968 	return (0);
6969 }
6970 
6971 
6972 /*
6973  * Process mode select caching page 8 (scsi3 format only).
6974  * Read Ahead (same as read cache) and Write Cache may be turned on and off
6975  * if these features are supported by the device. If these features are not
6976  * supported, quietly ignore them.
6977  * This function fails only if the SET FEATURE command sent to
6978  * the device fails. The page format is not varified, assuming that the
6979  * target driver operates correctly - if parameters length is too short,
6980  * we just drop the page.
6981  * Two command may be sent if both Read Cache/Read Ahead and Write Cache
6982  * setting have to be changed.
6983  * SET FEATURE command is executed synchronously, i.e. we wait here until
6984  * it is completed, regardless of the scsi pkt directives.
6985  *
6986  * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e.
6987  * changing DRA will change RCD.
6988  *
6989  * More than one SATA command may be executed to perform operations specified
6990  * by mode select pages. The first error terminates further execution.
6991  * Operations performed successully are not backed-up in such case.
6992  *
6993  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
6994  * If operation resulted in changing device setup, dmod flag should be set to
6995  * one (1). If parameters were not changed, dmod flag should be set to 0.
6996  * Upon return, if operation required sending command to the device, the rval
6997  * should be set to the value returned by sata_hba_start. If operation
6998  * did not require device access, rval should be set to TRAN_ACCEPT.
6999  * The pagelen should be set to the length of the page.
7000  *
7001  * This function has to be called with a port mutex held.
7002  *
7003  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
7004  */
7005 int
7006 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page,
7007     int parmlen, int *pagelen, int *rval, int *dmod)
7008 {
7009 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7010 	sata_drive_info_t *sdinfo;
7011 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
7012 	sata_id_t *sata_id;
7013 	struct scsi_extended_sense *sense;
7014 	int wce, dra;	/* Current settings */
7015 
7016 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
7017 	    &spx->txlt_sata_pkt->satapkt_device);
7018 	sata_id = &sdinfo->satadrv_id;
7019 	*dmod = 0;
7020 
7021 	/* Verify parameters length. If too short, drop it */
7022 	if (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
7023 	    sizeof (struct mode_page) < parmlen) {
7024 		*scsipkt->pkt_scbp = STATUS_CHECK;
7025 		sense = sata_arq_sense(spx);
7026 		sense->es_key = KEY_ILLEGAL_REQUEST;
7027 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
7028 		*pagelen = parmlen;
7029 		*rval = TRAN_ACCEPT;
7030 		return (SATA_FAILURE);
7031 	}
7032 
7033 	*pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page);
7034 
7035 	/*
7036 	 * We can manipulate only write cache and read ahead
7037 	 * (read cache) setting.
7038 	 */
7039 	if (!(sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) &&
7040 	    !(sata_id->ai_cmdset82 & SATA_WRITE_CACHE)) {
7041 		/*
7042 		 * None of the features is supported - ignore
7043 		 */
7044 		*rval = TRAN_ACCEPT;
7045 		return (SATA_SUCCESS);
7046 	}
7047 
7048 	/* Current setting of Read Ahead (and Read Cache) */
7049 	if (sata_id->ai_features85 & SATA_LOOK_AHEAD)
7050 		dra = 0;	/* 0 == not disabled */
7051 	else
7052 		dra = 1;
7053 	/* Current setting of Write Cache */
7054 	if (sata_id->ai_features85 & SATA_WRITE_CACHE)
7055 		wce = 1;
7056 	else
7057 		wce = 0;
7058 
7059 	if (page->dra == dra && page->wce == wce && page->rcd == dra) {
7060 		/* nothing to do */
7061 		*rval = TRAN_ACCEPT;
7062 		return (SATA_SUCCESS);
7063 	}
7064 	/*
7065 	 * Need to flip some setting
7066 	 * Set-up Internal SET FEATURES command(s)
7067 	 */
7068 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
7069 	scmd->satacmd_addr_type = 0;
7070 	scmd->satacmd_device_reg = 0;
7071 	scmd->satacmd_status_reg = 0;
7072 	scmd->satacmd_error_reg = 0;
7073 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
7074 	if (page->dra != dra || page->rcd != dra) {
7075 		/* Need to flip read ahead setting */
7076 		if (dra == 0)
7077 			/* Disable read ahead / read cache */
7078 			scmd->satacmd_features_reg =
7079 			    SATAC_SF_DISABLE_READ_AHEAD;
7080 		else
7081 			/* Enable read ahead  / read cache */
7082 			scmd->satacmd_features_reg =
7083 			    SATAC_SF_ENABLE_READ_AHEAD;
7084 
7085 		/* Transfer command to HBA */
7086 		if (sata_hba_start(spx, rval) != 0)
7087 			/*
7088 			 * Pkt not accepted for execution.
7089 			 */
7090 			return (SATA_FAILURE);
7091 
7092 		*dmod = 1;
7093 
7094 		/* Now process return */
7095 		if (spx->txlt_sata_pkt->satapkt_reason !=
7096 		    SATA_PKT_COMPLETED) {
7097 			goto failure;	/* Terminate */
7098 		}
7099 	}
7100 
7101 	/* Note that the packet is not removed, so it could be re-used */
7102 	if (page->wce != wce) {
7103 		/* Need to flip Write Cache setting */
7104 		if (page->wce == 1)
7105 			/* Enable write cache */
7106 			scmd->satacmd_features_reg =
7107 			    SATAC_SF_ENABLE_WRITE_CACHE;
7108 		else
7109 			/* Disable write cache */
7110 			scmd->satacmd_features_reg =
7111 			    SATAC_SF_DISABLE_WRITE_CACHE;
7112 
7113 		/* Transfer command to HBA */
7114 		if (sata_hba_start(spx, rval) != 0)
7115 			/*
7116 			 * Pkt not accepted for execution.
7117 			 */
7118 			return (SATA_FAILURE);
7119 
7120 		*dmod = 1;
7121 
7122 		/* Now process return */
7123 		if (spx->txlt_sata_pkt->satapkt_reason !=
7124 		    SATA_PKT_COMPLETED) {
7125 			goto failure;
7126 		}
7127 	}
7128 	return (SATA_SUCCESS);
7129 
7130 failure:
7131 	sata_xlate_errors(spx);
7132 
7133 	return (SATA_FAILURE);
7134 }
7135 
7136 /*
7137  * Process mode select informational exceptions control page 0x1c
7138  *
7139  * The only changeable bit is dexcpt (disable exceptions).
7140  * MRIE (method of reporting informational exceptions) must be
7141  * "only on request".
7142  *
7143  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
7144  * If operation resulted in changing device setup, dmod flag should be set to
7145  * one (1). If parameters were not changed, dmod flag should be set to 0.
7146  * Upon return, if operation required sending command to the device, the rval
7147  * should be set to the value returned by sata_hba_start. If operation
7148  * did not require device access, rval should be set to TRAN_ACCEPT.
7149  * The pagelen should be set to the length of the page.
7150  *
7151  * This function has to be called with a port mutex held.
7152  *
7153  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
7154  */
7155 static	int
7156 sata_mode_select_page_1c(
7157 	sata_pkt_txlate_t *spx,
7158 	struct mode_info_excpt_page *page,
7159 	int parmlen,
7160 	int *pagelen,
7161 	int *rval,
7162 	int *dmod)
7163 {
7164 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7165 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
7166 	sata_drive_info_t *sdinfo;
7167 	sata_id_t *sata_id;
7168 	struct scsi_extended_sense *sense;
7169 
7170 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
7171 	    &spx->txlt_sata_pkt->satapkt_device);
7172 	sata_id = &sdinfo->satadrv_id;
7173 
7174 	*dmod = 0;
7175 
7176 	/* Verify parameters length. If too short, drop it */
7177 	if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) < parmlen) ||
7178 	    page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) {
7179 		*scsipkt->pkt_scbp = STATUS_CHECK;
7180 		sense = sata_arq_sense(spx);
7181 		sense->es_key = KEY_ILLEGAL_REQUEST;
7182 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
7183 		*pagelen = parmlen;
7184 		*rval = TRAN_ACCEPT;
7185 		return (SATA_FAILURE);
7186 	}
7187 
7188 	*pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page);
7189 
7190 	if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
7191 		*scsipkt->pkt_scbp = STATUS_CHECK;
7192 		sense = sata_arq_sense(spx);
7193 		sense->es_key = KEY_ILLEGAL_REQUEST;
7194 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
7195 		*pagelen = parmlen;
7196 		*rval = TRAN_ACCEPT;
7197 		return (SATA_FAILURE);
7198 	}
7199 
7200 	/* If already in the state requested, we are done */
7201 	if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
7202 		/* nothing to do */
7203 		*rval = TRAN_ACCEPT;
7204 		return (SATA_SUCCESS);
7205 	}
7206 
7207 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
7208 
7209 	/* Build SMART_ENABLE or SMART_DISABLE command */
7210 	scmd->satacmd_addr_type = 0;		/* N/A */
7211 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
7212 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
7213 	scmd->satacmd_features_reg = page->dexcpt ?
7214 	    SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS;
7215 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
7216 	scmd->satacmd_cmd_reg = SATAC_SMART;
7217 
7218 	/* Transfer command to HBA */
7219 	if (sata_hba_start(spx, rval) != 0)
7220 		/*
7221 		 * Pkt not accepted for execution.
7222 		 */
7223 		return (SATA_FAILURE);
7224 
7225 	*dmod = 1;	/* At least may have been modified */
7226 
7227 	/* Now process return */
7228 	if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED)
7229 		return (SATA_SUCCESS);
7230 
7231 	/* Packet did not complete successfully */
7232 	sata_xlate_errors(spx);
7233 
7234 	return (SATA_FAILURE);
7235 }
7236 
7237 int
7238 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct
7239     mode_acoustic_management *page, int parmlen, int *pagelen,
7240     int *rval, int *dmod)
7241 {
7242 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7243 	sata_drive_info_t *sdinfo;
7244 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
7245 	sata_id_t *sata_id;
7246 	struct scsi_extended_sense *sense;
7247 
7248 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
7249 	    &spx->txlt_sata_pkt->satapkt_device);
7250 	sata_id = &sdinfo->satadrv_id;
7251 	*dmod = 0;
7252 
7253 	/* If parmlen is too short or the feature is not supported, drop it */
7254 	if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
7255 	    sizeof (struct mode_page)) < parmlen) ||
7256 	    (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) {
7257 		*scsipkt->pkt_scbp = STATUS_CHECK;
7258 		sense = sata_arq_sense(spx);
7259 		sense->es_key = KEY_ILLEGAL_REQUEST;
7260 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
7261 		*pagelen = parmlen;
7262 		*rval = TRAN_ACCEPT;
7263 		return (SATA_FAILURE);
7264 	}
7265 
7266 	*pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
7267 	    sizeof (struct mode_page);
7268 
7269 	/*
7270 	 * We can enable and disable acoustice management and
7271 	 * set the acoustic management level.
7272 	 */
7273 
7274 	/*
7275 	 * Set-up Internal SET FEATURES command(s)
7276 	 */
7277 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
7278 	scmd->satacmd_addr_type = 0;
7279 	scmd->satacmd_device_reg = 0;
7280 	scmd->satacmd_status_reg = 0;
7281 	scmd->satacmd_error_reg = 0;
7282 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
7283 	if (page->acoustic_manag_enable) {
7284 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC;
7285 		scmd->satacmd_sec_count_lsb = page->acoustic_manag_level;
7286 	} else {	/* disabling acoustic management */
7287 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC;
7288 	}
7289 
7290 	/* Transfer command to HBA */
7291 	if (sata_hba_start(spx, rval) != 0)
7292 		/*
7293 		 * Pkt not accepted for execution.
7294 		 */
7295 		return (SATA_FAILURE);
7296 
7297 	/* Now process return */
7298 	if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) {
7299 		sata_xlate_errors(spx);
7300 		return (SATA_FAILURE);
7301 	}
7302 
7303 	*dmod = 1;
7304 
7305 	return (SATA_SUCCESS);
7306 }
7307 
7308 
7309 
7310 
7311 /*
7312  * sata_build_lsense_page0() is used to create the
7313  * SCSI LOG SENSE page 0 (supported log pages)
7314  *
7315  * Currently supported pages are 0, 0x10, 0x2f and 0x30
7316  * (supported log pages, self-test results, informational exceptions
7317  *  and Sun vendor specific ATA SMART data).
7318  *
7319  * Takes a sata_drive_info t * and the address of a buffer
7320  * in which to create the page information.
7321  *
7322  * Returns the number of bytes valid in the buffer.
7323  */
7324 static	int
7325 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf)
7326 {
7327 	struct log_parameter *lpp = (struct log_parameter *)buf;
7328 	uint8_t *page_ptr = (uint8_t *)lpp->param_values;
7329 	int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */
7330 	sata_id_t *sata_id = &sdinfo->satadrv_id;
7331 
7332 	lpp->param_code[0] = 0;
7333 	lpp->param_code[1] = 0;
7334 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
7335 	*page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES;
7336 
7337 	if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
7338 		if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) {
7339 			*page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS;
7340 			++num_pages_supported;
7341 		}
7342 		*page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS;
7343 		++num_pages_supported;
7344 		*page_ptr++ = PAGE_CODE_SMART_READ_DATA;
7345 		++num_pages_supported;
7346 	}
7347 
7348 	lpp->param_len = num_pages_supported;
7349 
7350 	return ((&lpp->param_values[0] - (uint8_t *)lpp) +
7351 	    num_pages_supported);
7352 }
7353 
7354 /*
7355  * sata_build_lsense_page_10() is used to create the
7356  * SCSI LOG SENSE page 0x10 (self-test results)
7357  *
7358  * Takes a sata_drive_info t * and the address of a buffer
7359  * in which to create the page information as well as a sata_hba_inst_t *.
7360  *
7361  * Returns the number of bytes valid in the buffer.
7362  */
7363 static	int
7364 sata_build_lsense_page_10(
7365 	sata_drive_info_t *sdinfo,
7366 	uint8_t *buf,
7367 	sata_hba_inst_t *sata_hba_inst)
7368 {
7369 	struct log_parameter *lpp = (struct log_parameter *)buf;
7370 	int rval;
7371 
7372 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
7373 		struct smart_ext_selftest_log *ext_selftest_log;
7374 
7375 		ext_selftest_log = kmem_zalloc(
7376 		    sizeof (struct smart_ext_selftest_log), KM_SLEEP);
7377 
7378 		rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo,
7379 		    ext_selftest_log, 0);
7380 		if (rval == 0) {
7381 			int index, start_index;
7382 			struct smart_ext_selftest_log_entry *entry;
7383 			static const struct smart_ext_selftest_log_entry empty =
7384 			    {0};
7385 			uint16_t block_num;
7386 			int count;
7387 			boolean_t only_one_block = B_FALSE;
7388 
7389 			index = ext_selftest_log->
7390 			    smart_ext_selftest_log_index[0];
7391 			index |= ext_selftest_log->
7392 			    smart_ext_selftest_log_index[1] << 8;
7393 			if (index == 0)
7394 				goto out;
7395 
7396 			--index;	/* Correct for 0 origin */
7397 			start_index = index;	/* remember where we started */
7398 			block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
7399 			if (block_num != 0) {
7400 				rval = sata_ext_smart_selftest_read_log(
7401 				    sata_hba_inst, sdinfo, ext_selftest_log,
7402 				    block_num);
7403 				if (rval != 0)
7404 					goto out;
7405 			}
7406 			index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
7407 			entry =
7408 			    &ext_selftest_log->
7409 			    smart_ext_selftest_log_entries[index];
7410 
7411 			for (count = 1;
7412 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
7413 			    ++count) {
7414 				uint8_t status;
7415 				uint8_t code;
7416 				uint8_t sense_key;
7417 				uint8_t add_sense_code;
7418 				uint8_t add_sense_code_qual;
7419 
7420 				/* If this is an unused entry, we are done */
7421 				if (bcmp(entry, &empty, sizeof (empty)) == 0) {
7422 					/* Broken firmware on some disks */
7423 					if (index + 1 ==
7424 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK) {
7425 						--entry;
7426 						--index;
7427 						if (bcmp(entry, &empty,
7428 						    sizeof (empty)) == 0)
7429 							goto out;
7430 					} else
7431 						goto out;
7432 				}
7433 
7434 				if (only_one_block &&
7435 				    start_index == index)
7436 					goto out;
7437 
7438 				lpp->param_code[0] = 0;
7439 				lpp->param_code[1] = count;
7440 				lpp->param_ctrl_flags =
7441 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
7442 				lpp->param_len =
7443 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
7444 
7445 				status = entry->smart_ext_selftest_log_status;
7446 				status >>= 4;
7447 				switch (status) {
7448 				case 0:
7449 				default:
7450 					sense_key = KEY_NO_SENSE;
7451 					add_sense_code =
7452 					    SD_SCSI_ASC_NO_ADD_SENSE;
7453 					add_sense_code_qual = 0;
7454 					break;
7455 				case 1:
7456 					sense_key = KEY_ABORTED_COMMAND;
7457 					add_sense_code =
7458 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7459 					add_sense_code_qual = SCSI_COMPONENT_81;
7460 					break;
7461 				case 2:
7462 					sense_key = KEY_ABORTED_COMMAND;
7463 					add_sense_code =
7464 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7465 					add_sense_code_qual = SCSI_COMPONENT_82;
7466 					break;
7467 				case 3:
7468 					sense_key = KEY_ABORTED_COMMAND;
7469 					add_sense_code =
7470 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7471 					add_sense_code_qual = SCSI_COMPONENT_83;
7472 					break;
7473 				case 4:
7474 					sense_key = KEY_HARDWARE_ERROR;
7475 					add_sense_code =
7476 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7477 					add_sense_code_qual = SCSI_COMPONENT_84;
7478 					break;
7479 				case 5:
7480 					sense_key = KEY_HARDWARE_ERROR;
7481 					add_sense_code =
7482 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7483 					add_sense_code_qual = SCSI_COMPONENT_85;
7484 					break;
7485 				case 6:
7486 					sense_key = KEY_HARDWARE_ERROR;
7487 					add_sense_code =
7488 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7489 					add_sense_code_qual = SCSI_COMPONENT_86;
7490 					break;
7491 				case 7:
7492 					sense_key = KEY_MEDIUM_ERROR;
7493 					add_sense_code =
7494 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7495 					add_sense_code_qual = SCSI_COMPONENT_87;
7496 					break;
7497 				case 8:
7498 					sense_key = KEY_HARDWARE_ERROR;
7499 					add_sense_code =
7500 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7501 					add_sense_code_qual = SCSI_COMPONENT_88;
7502 					break;
7503 				}
7504 				code = 0;	/* unspecified */
7505 				status |= (code << 4);
7506 				lpp->param_values[0] = status;
7507 				lpp->param_values[1] = 0; /* unspecified */
7508 				lpp->param_values[2] = entry->
7509 				    smart_ext_selftest_log_timestamp[1];
7510 				lpp->param_values[3] = entry->
7511 				    smart_ext_selftest_log_timestamp[0];
7512 				if (status != 0) {
7513 					lpp->param_values[4] = 0;
7514 					lpp->param_values[5] = 0;
7515 					lpp->param_values[6] = entry->
7516 					    smart_ext_selftest_log_failing_lba
7517 					    [5];
7518 					lpp->param_values[7] = entry->
7519 					    smart_ext_selftest_log_failing_lba
7520 					    [4];
7521 					lpp->param_values[8] = entry->
7522 					    smart_ext_selftest_log_failing_lba
7523 					    [3];
7524 					lpp->param_values[9] = entry->
7525 					    smart_ext_selftest_log_failing_lba
7526 					    [2];
7527 					lpp->param_values[10] = entry->
7528 					    smart_ext_selftest_log_failing_lba
7529 					    [1];
7530 					lpp->param_values[11] = entry->
7531 					    smart_ext_selftest_log_failing_lba
7532 					    [0];
7533 				} else {	/* No bad block address */
7534 					lpp->param_values[4] = 0xff;
7535 					lpp->param_values[5] = 0xff;
7536 					lpp->param_values[6] = 0xff;
7537 					lpp->param_values[7] = 0xff;
7538 					lpp->param_values[8] = 0xff;
7539 					lpp->param_values[9] = 0xff;
7540 					lpp->param_values[10] = 0xff;
7541 					lpp->param_values[11] = 0xff;
7542 				}
7543 
7544 				lpp->param_values[12] = sense_key;
7545 				lpp->param_values[13] = add_sense_code;
7546 				lpp->param_values[14] = add_sense_code_qual;
7547 				lpp->param_values[15] = 0; /* undefined */
7548 
7549 				lpp = (struct log_parameter *)
7550 				    (((uint8_t *)lpp) +
7551 				    SCSI_LOG_PARAM_HDR_LEN +
7552 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
7553 
7554 				--index;	/* Back up to previous entry */
7555 				if (index < 0) {
7556 					if (block_num > 0) {
7557 						--block_num;
7558 					} else {
7559 						struct read_log_ext_directory
7560 						    logdir;
7561 
7562 						rval =
7563 						    sata_read_log_ext_directory(
7564 						    sata_hba_inst, sdinfo,
7565 						    &logdir);
7566 						if (rval == -1)
7567 							goto out;
7568 						if ((logdir.read_log_ext_vers
7569 						    [0] == 0) &&
7570 						    (logdir.read_log_ext_vers
7571 						    [1] == 0))
7572 							goto out;
7573 						block_num =
7574 						    logdir.read_log_ext_nblks
7575 						    [EXT_SMART_SELFTEST_LOG_PAGE
7576 						    - 1][0];
7577 						block_num |= logdir.
7578 						    read_log_ext_nblks
7579 						    [EXT_SMART_SELFTEST_LOG_PAGE
7580 						    - 1][1] << 8;
7581 						--block_num;
7582 						only_one_block =
7583 						    (block_num == 0);
7584 					}
7585 					rval = sata_ext_smart_selftest_read_log(
7586 					    sata_hba_inst, sdinfo,
7587 					    ext_selftest_log, block_num);
7588 					if (rval != 0)
7589 						goto out;
7590 
7591 					index =
7592 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK -
7593 					    1;
7594 				}
7595 				index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
7596 				entry = &ext_selftest_log->
7597 				    smart_ext_selftest_log_entries[index];
7598 			}
7599 		}
7600 out:
7601 		kmem_free(ext_selftest_log,
7602 		    sizeof (struct smart_ext_selftest_log));
7603 	} else {
7604 		struct smart_selftest_log *selftest_log;
7605 
7606 		selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log),
7607 		    KM_SLEEP);
7608 
7609 		rval = sata_smart_selftest_log(sata_hba_inst, sdinfo,
7610 		    selftest_log);
7611 
7612 		if (rval == 0) {
7613 			int index;
7614 			int count;
7615 			struct smart_selftest_log_entry *entry;
7616 			static const struct smart_selftest_log_entry empty =
7617 			    { 0 };
7618 
7619 			index = selftest_log->smart_selftest_log_index;
7620 			if (index == 0)
7621 				goto done;
7622 			--index;	/* Correct for 0 origin */
7623 			entry = &selftest_log->
7624 			    smart_selftest_log_entries[index];
7625 			for (count = 1;
7626 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
7627 			    ++count) {
7628 				uint8_t status;
7629 				uint8_t code;
7630 				uint8_t sense_key;
7631 				uint8_t add_sense_code;
7632 				uint8_t add_sense_code_qual;
7633 
7634 				if (bcmp(entry, &empty, sizeof (empty)) == 0)
7635 					goto done;
7636 
7637 				lpp->param_code[0] = 0;
7638 				lpp->param_code[1] = count;
7639 				lpp->param_ctrl_flags =
7640 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
7641 				lpp->param_len =
7642 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
7643 
7644 				status = entry->smart_selftest_log_status;
7645 				status >>= 4;
7646 				switch (status) {
7647 				case 0:
7648 				default:
7649 					sense_key = KEY_NO_SENSE;
7650 					add_sense_code =
7651 					    SD_SCSI_ASC_NO_ADD_SENSE;
7652 					break;
7653 				case 1:
7654 					sense_key = KEY_ABORTED_COMMAND;
7655 					add_sense_code =
7656 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7657 					add_sense_code_qual = SCSI_COMPONENT_81;
7658 					break;
7659 				case 2:
7660 					sense_key = KEY_ABORTED_COMMAND;
7661 					add_sense_code =
7662 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7663 					add_sense_code_qual = SCSI_COMPONENT_82;
7664 					break;
7665 				case 3:
7666 					sense_key = KEY_ABORTED_COMMAND;
7667 					add_sense_code =
7668 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7669 					add_sense_code_qual = SCSI_COMPONENT_83;
7670 					break;
7671 				case 4:
7672 					sense_key = KEY_HARDWARE_ERROR;
7673 					add_sense_code =
7674 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7675 					add_sense_code_qual = SCSI_COMPONENT_84;
7676 					break;
7677 				case 5:
7678 					sense_key = KEY_HARDWARE_ERROR;
7679 					add_sense_code =
7680 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7681 					add_sense_code_qual = SCSI_COMPONENT_85;
7682 					break;
7683 				case 6:
7684 					sense_key = KEY_HARDWARE_ERROR;
7685 					add_sense_code =
7686 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7687 					add_sense_code_qual = SCSI_COMPONENT_86;
7688 					break;
7689 				case 7:
7690 					sense_key = KEY_MEDIUM_ERROR;
7691 					add_sense_code =
7692 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7693 					add_sense_code_qual = SCSI_COMPONENT_87;
7694 					break;
7695 				case 8:
7696 					sense_key = KEY_HARDWARE_ERROR;
7697 					add_sense_code =
7698 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7699 					add_sense_code_qual = SCSI_COMPONENT_88;
7700 					break;
7701 				}
7702 				code = 0;	/* unspecified */
7703 				status |= (code << 4);
7704 				lpp->param_values[0] = status;
7705 				lpp->param_values[1] = 0; /* unspecified */
7706 				lpp->param_values[2] = entry->
7707 				    smart_selftest_log_timestamp[1];
7708 				lpp->param_values[3] = entry->
7709 				    smart_selftest_log_timestamp[0];
7710 				if (status != 0) {
7711 					lpp->param_values[4] = 0;
7712 					lpp->param_values[5] = 0;
7713 					lpp->param_values[6] = 0;
7714 					lpp->param_values[7] = 0;
7715 					lpp->param_values[8] = entry->
7716 					    smart_selftest_log_failing_lba[3];
7717 					lpp->param_values[9] = entry->
7718 					    smart_selftest_log_failing_lba[2];
7719 					lpp->param_values[10] = entry->
7720 					    smart_selftest_log_failing_lba[1];
7721 					lpp->param_values[11] = entry->
7722 					    smart_selftest_log_failing_lba[0];
7723 				} else {	/* No block address */
7724 					lpp->param_values[4] = 0xff;
7725 					lpp->param_values[5] = 0xff;
7726 					lpp->param_values[6] = 0xff;
7727 					lpp->param_values[7] = 0xff;
7728 					lpp->param_values[8] = 0xff;
7729 					lpp->param_values[9] = 0xff;
7730 					lpp->param_values[10] = 0xff;
7731 					lpp->param_values[11] = 0xff;
7732 				}
7733 				lpp->param_values[12] = sense_key;
7734 				lpp->param_values[13] = add_sense_code;
7735 				lpp->param_values[14] = add_sense_code_qual;
7736 				lpp->param_values[15] = 0; /* undefined */
7737 
7738 				lpp = (struct log_parameter *)
7739 				    (((uint8_t *)lpp) +
7740 				    SCSI_LOG_PARAM_HDR_LEN +
7741 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
7742 				--index;	/* back up to previous entry */
7743 				if (index < 0) {
7744 					index =
7745 					    NUM_SMART_SELFTEST_LOG_ENTRIES - 1;
7746 				}
7747 				entry = &selftest_log->
7748 				    smart_selftest_log_entries[index];
7749 			}
7750 		}
7751 done:
7752 		kmem_free(selftest_log, sizeof (struct smart_selftest_log));
7753 	}
7754 
7755 	return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) *
7756 	    SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS);
7757 }
7758 
7759 /*
7760  * sata_build_lsense_page_2f() is used to create the
7761  * SCSI LOG SENSE page 0x10 (informational exceptions)
7762  *
7763  * Takes a sata_drive_info t * and the address of a buffer
7764  * in which to create the page information as well as a sata_hba_inst_t *.
7765  *
7766  * Returns the number of bytes valid in the buffer.
7767  */
7768 static	int
7769 sata_build_lsense_page_2f(
7770 	sata_drive_info_t *sdinfo,
7771 	uint8_t *buf,
7772 	sata_hba_inst_t *sata_hba_inst)
7773 {
7774 	struct log_parameter *lpp = (struct log_parameter *)buf;
7775 	int rval;
7776 	uint8_t *smart_data;
7777 	uint8_t temp;
7778 	sata_id_t *sata_id;
7779 #define	SMART_NO_TEMP	0xff
7780 
7781 	lpp->param_code[0] = 0;
7782 	lpp->param_code[1] = 0;
7783 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
7784 
7785 	/* Now get the SMART status w.r.t. threshold exceeded */
7786 	rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo);
7787 	switch (rval) {
7788 	case 1:
7789 		lpp->param_values[0] = SCSI_PREDICTED_FAILURE;
7790 		lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE;
7791 		break;
7792 	case 0:
7793 	case -1:	/* failed to get data */
7794 		lpp->param_values[0] = 0;	/* No failure predicted */
7795 		lpp->param_values[1] = 0;
7796 		break;
7797 #if defined(SATA_DEBUG)
7798 	default:
7799 		cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value");
7800 		/* NOTREACHED */
7801 #endif
7802 	}
7803 
7804 	sata_id = &sdinfo->satadrv_id;
7805 	if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP))
7806 		temp = SMART_NO_TEMP;
7807 	else {
7808 		/* Now get the temperature */
7809 		smart_data = kmem_zalloc(512, KM_SLEEP);
7810 		rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data,
7811 		    SCT_STATUS_LOG_PAGE, 1);
7812 		if (rval == -1)
7813 			temp = SMART_NO_TEMP;
7814 		else {
7815 			temp = smart_data[200];
7816 			if (temp & 0x80) {
7817 				if (temp & 0x7f)
7818 					temp = 0;
7819 				else
7820 					temp = SMART_NO_TEMP;
7821 			}
7822 		}
7823 		kmem_free(smart_data, 512);
7824 	}
7825 
7826 	lpp->param_values[2] = temp;	/* most recent temperature */
7827 	lpp->param_values[3] = 0;	/* required vendor specific byte */
7828 
7829 	lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN;
7830 
7831 
7832 	return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN);
7833 }
7834 
7835 /*
7836  * sata_build_lsense_page_30() is used to create the
7837  * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data).
7838  *
7839  * Takes a sata_drive_info t * and the address of a buffer
7840  * in which to create the page information as well as a sata_hba_inst_t *.
7841  *
7842  * Returns the number of bytes valid in the buffer.
7843  */
7844 static int
7845 sata_build_lsense_page_30(
7846 	sata_drive_info_t *sdinfo,
7847 	uint8_t *buf,
7848 	sata_hba_inst_t *sata_hba_inst)
7849 {
7850 	struct smart_data *smart_data = (struct smart_data *)buf;
7851 	int rval;
7852 
7853 	/* Now do the SMART READ DATA */
7854 	rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data);
7855 	if (rval == -1)
7856 		return (0);
7857 
7858 	return (sizeof (struct smart_data));
7859 }
7860 
7861 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */
7862 
7863 /*
7864  * Start command for ATAPI device.
7865  * This function processes scsi_pkt requests.
7866  * Only CD/DVD devices are supported.
7867  * Most commands are packet without any translation into Packet Command.
7868  * Some may be trapped and executed as SATA commands (not clear which one).
7869  *
7870  * Returns TRAN_ACCEPT if command is accepted for execution (or completed
7871  * execution).
7872  * Returns other TRAN_XXXX codes if command is not accepted or completed
7873  * (see return values for sata_hba_start()).
7874  *
7875  * Note:
7876  * Inquiry cdb format differs between transport version 2 and 3.
7877  * However, the transport version 3 devices that were checked did not adhere
7878  * to the specification (ignored MSB of the allocation length). Therefore,
7879  * the transport version is not checked, but Inquiry allocation length is
7880  * truncated to 255 bytes if the original allocation length set-up by the
7881  * target driver is greater than 255 bytes.
7882  */
7883 static int
7884 sata_txlt_atapi(sata_pkt_txlate_t *spx)
7885 {
7886 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7887 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
7888 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7889 	sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx);
7890 	sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba,
7891 	    &spx->txlt_sata_pkt->satapkt_device);
7892 	int cport = SATA_TXLT_CPORT(spx);
7893 	int cdblen;
7894 	int rval;
7895 	int synch;
7896 	union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp;
7897 
7898 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
7899 
7900 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
7901 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
7902 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7903 		return (rval);
7904 	}
7905 
7906 	/*
7907 	 * ATAPI device executes some ATA commands in addition to MMC command
7908 	 * set. These ATA commands may be executed by the regular SATA
7909 	 * translation functions. None needs to be captured now.
7910 	 * Other commands belong to MMC command set and are delivered
7911 	 * to ATAPI device via Packet Command.
7912 	 */
7913 
7914 	/* Check the size of cdb */
7915 	cdblen = scsi_cdb_size[GETGROUP(cdbp)];
7916 	if (cdblen > sdinfo->satadrv_atapi_cdb_len) {
7917 		sata_log(NULL, CE_WARN,
7918 		    "sata: invalid ATAPI cdb length %d",
7919 		    scsipkt->pkt_cdblen);
7920 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7921 		return (TRAN_BADPKT);
7922 	}
7923 
7924 	SATAATAPITRACE(spx, cdblen);
7925 
7926 	/*
7927 	 * For non-read/write commands we need to
7928 	 * map buffer
7929 	 */
7930 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
7931 	case SCMD_READ:
7932 	case SCMD_READ_G1:
7933 	case SCMD_READ_G5:
7934 	case SCMD_READ_G4:
7935 	case SCMD_WRITE:
7936 	case SCMD_WRITE_G1:
7937 	case SCMD_WRITE_G5:
7938 	case SCMD_WRITE_G4:
7939 		break;
7940 	default:
7941 		if (bp != NULL) {
7942 			if (bp->b_flags & (B_PHYS | B_PAGEIO))
7943 				bp_mapin(bp);
7944 		}
7945 		break;
7946 	}
7947 	/*
7948 	 * scmd->satacmd_flags.sata_data_direction default -
7949 	 * SATA_DIR_NODATA_XFER - is set by
7950 	 * sata_txlt_generic_pkt_info().
7951 	 */
7952 	if (scmd->satacmd_bp) {
7953 		if (scmd->satacmd_bp->b_flags & B_READ) {
7954 			scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7955 		} else {
7956 			scmd->satacmd_flags.sata_data_direction =
7957 			    SATA_DIR_WRITE;
7958 		}
7959 	}
7960 
7961 	/*
7962 	 * Set up ATAPI packet command.
7963 	 */
7964 
7965 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7966 
7967 	/* Copy cdb into sata_cmd */
7968 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7969 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7970 	bcopy(cdbp, scmd->satacmd_acdb, cdblen);
7971 
7972 	/* See note in the command header */
7973 	if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) {
7974 		if (scmd->satacmd_acdb[3] != 0)
7975 			scmd->satacmd_acdb[4] = 255;
7976 	}
7977 
7978 #ifdef SATA_DEBUG
7979 	if (sata_debug_flags & SATA_DBG_ATAPI) {
7980 		uint8_t *p = scmd->satacmd_acdb;
7981 		char buf[3 * SATA_ATAPI_MAX_CDB_LEN];
7982 
7983 		(void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN,
7984 		    "%02x %02x %02x %02x %02x %02x %02x %02x "
7985 		    "%2x %02x %02x %02x %02x %02x %02x %02x",
7986 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7987 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7988 		buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0';
7989 		cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf);
7990 	}
7991 #endif
7992 
7993 	/*
7994 	 * Preset request sense data to NO SENSE.
7995 	 * If there is no way to get error information via Request Sense,
7996 	 * the packet request sense data would not have to be modified by HBA,
7997 	 * but it could be returned as is.
7998 	 */
7999 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
8000 	sata_fixed_sense_data_preset(
8001 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
8002 
8003 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
8004 		/* Need callback function */
8005 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion;
8006 		synch = FALSE;
8007 	} else
8008 		synch = TRUE;
8009 
8010 	/* Transfer command to HBA */
8011 	if (sata_hba_start(spx, &rval) != 0) {
8012 		/* Pkt not accepted for execution */
8013 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
8014 		return (rval);
8015 	}
8016 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
8017 	/*
8018 	 * If execution is non-synchronous,
8019 	 * a callback function will handle potential errors, translate
8020 	 * the response and will do a callback to a target driver.
8021 	 * If it was synchronous, use the same framework callback to check
8022 	 * an execution status.
8023 	 */
8024 	if (synch) {
8025 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
8026 		    "synchronous execution status %x\n",
8027 		    spx->txlt_sata_pkt->satapkt_reason);
8028 		sata_txlt_atapi_completion(spx->txlt_sata_pkt);
8029 	}
8030 	return (TRAN_ACCEPT);
8031 }
8032 
8033 
8034 /*
8035  * ATAPI Packet command completion.
8036  *
8037  * Failure of the command passed via Packet command are considered device
8038  * error. SATA HBA driver would have to retrieve error data (via Request
8039  * Sense command delivered via error retrieval sata packet) and copy it
8040  * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data.
8041  */
8042 static void
8043 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt)
8044 {
8045 	sata_pkt_txlate_t *spx =
8046 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
8047 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8048 	struct scsi_extended_sense *sense;
8049 	struct buf *bp;
8050 	int rval;
8051 
8052 #ifdef SATA_DEBUG
8053 	uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense;
8054 #endif
8055 
8056 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
8057 	    STATE_SENT_CMD | STATE_GOT_STATUS;
8058 
8059 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
8060 		/* Normal completion */
8061 		if (sata_pkt->satapkt_cmd.satacmd_bp != NULL)
8062 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
8063 		scsipkt->pkt_reason = CMD_CMPLT;
8064 		*scsipkt->pkt_scbp = STATUS_GOOD;
8065 		if (spx->txlt_tmp_buf != NULL) {
8066 			/* Temporary buffer was used */
8067 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
8068 			if (bp->b_flags & B_READ) {
8069 				rval = ddi_dma_sync(
8070 				    spx->txlt_buf_dma_handle, 0, 0,
8071 				    DDI_DMA_SYNC_FORCPU);
8072 				ASSERT(rval == DDI_SUCCESS);
8073 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
8074 				    bp->b_bcount);
8075 			}
8076 		}
8077 	} else {
8078 		/*
8079 		 * Something went wrong - analyze return
8080 		 */
8081 		*scsipkt->pkt_scbp = STATUS_CHECK;
8082 		sense = sata_arq_sense(spx);
8083 
8084 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
8085 			scsipkt->pkt_reason = CMD_INCOMPLETE;
8086 			/*
8087 			 * We may not have ARQ data if there was a double
8088 			 * error. But sense data in sata packet was pre-set
8089 			 * with NO SENSE so it is valid even if HBA could
8090 			 * not retrieve a real sense data.
8091 			 * Just copy this sense data into scsi pkt sense area.
8092 			 */
8093 			bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense,
8094 			    SATA_ATAPI_MIN_RQSENSE_LEN);
8095 #ifdef SATA_DEBUG
8096 			if (sata_debug_flags & SATA_DBG_SCSI_IF) {
8097 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
8098 				    "sata_txlt_atapi_completion: %02x\n"
8099 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
8100 				    "          %02x %02x %02x %02x %02x %02x "
8101 				    "          %02x %02x %02x %02x %02x %02x\n",
8102 				    scsipkt->pkt_reason,
8103 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
8104 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
8105 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
8106 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
8107 				    rqsp[16], rqsp[17]);
8108 			}
8109 #endif
8110 		} else {
8111 			switch (sata_pkt->satapkt_reason) {
8112 			case SATA_PKT_PORT_ERROR:
8113 				/*
8114 				 * We have no device data.
8115 				 */
8116 				scsipkt->pkt_reason = CMD_INCOMPLETE;
8117 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
8118 				    STATE_GOT_TARGET | STATE_SENT_CMD |
8119 				    STATE_GOT_STATUS);
8120 				sense->es_key = KEY_HARDWARE_ERROR;
8121 
8122 				/* No extended sense key - no info available */
8123 				scsipkt->pkt_reason = CMD_INCOMPLETE;
8124 				break;
8125 
8126 			case SATA_PKT_TIMEOUT:
8127 				/* scsipkt->pkt_reason = CMD_TIMEOUT; */
8128 				/* No extended sense key */
8129 				/*
8130 				 * Need to check if HARDWARE_ERROR/
8131 				 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more
8132 				 * appropriate.
8133 				 */
8134 				break;
8135 
8136 			case SATA_PKT_ABORTED:
8137 				scsipkt->pkt_reason = CMD_ABORTED;
8138 				/* Should we set key COMMAND_ABPRTED? */
8139 				break;
8140 
8141 			case SATA_PKT_RESET:
8142 				scsipkt->pkt_reason = CMD_RESET;
8143 				/*
8144 				 * May be we should set Unit Attention /
8145 				 * Reset. Perhaps the same should be
8146 				 * returned for disks....
8147 				 */
8148 				sense->es_key = KEY_UNIT_ATTENTION;
8149 				sense->es_add_code = SD_SCSI_ASC_RESET;
8150 				break;
8151 
8152 			default:
8153 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
8154 				    "sata_txlt_atapi_completion: "
8155 				    "invalid packet completion reason"));
8156 				scsipkt->pkt_reason = CMD_TRAN_ERR;
8157 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
8158 				    STATE_GOT_TARGET | STATE_SENT_CMD |
8159 				    STATE_GOT_STATUS);
8160 				break;
8161 			}
8162 		}
8163 	}
8164 
8165 	SATAATAPITRACE(spx, 0);
8166 
8167 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
8168 	    scsipkt->pkt_comp != NULL) {
8169 		/* scsi callback required */
8170 		(*scsipkt->pkt_comp)(scsipkt);
8171 	}
8172 }
8173 
8174 /*
8175  * Set up error retrieval sata command for ATAPI Packet Command error data
8176  * recovery.
8177  *
8178  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
8179  * returns SATA_FAILURE otherwise.
8180  */
8181 
8182 static int
8183 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
8184 {
8185 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
8186 	sata_cmd_t *scmd;
8187 	struct buf *bp;
8188 
8189 	/*
8190 	 * Allocate dma-able buffer error data.
8191 	 * Buffer allocation will take care of buffer alignment and other DMA
8192 	 * attributes.
8193 	 */
8194 	bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN);
8195 	if (bp == NULL) {
8196 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
8197 		    "sata_get_err_retrieval_pkt: "
8198 		    "cannot allocate buffer for error data", NULL);
8199 		return (SATA_FAILURE);
8200 	}
8201 	bp_mapin(bp); /* make data buffer accessible */
8202 
8203 	/* Operation modes are up to the caller */
8204 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
8205 
8206 	/* Synchronous mode, no callback - may be changed by the caller */
8207 	spkt->satapkt_comp = NULL;
8208 	spkt->satapkt_time = sata_default_pkt_time;
8209 
8210 	scmd = &spkt->satapkt_cmd;
8211 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
8212 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
8213 
8214 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
8215 
8216 	/*
8217 	 * Set-up acdb. Request Sense CDB (packet command content) is
8218 	 * not in DMA-able buffer. Its handling is HBA-specific (how
8219 	 * it is transfered into packet FIS).
8220 	 */
8221 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
8222 	bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN);
8223 	/* Following zeroing of pad bytes may not be necessary */
8224 	bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN],
8225 	    sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN);
8226 
8227 	/*
8228 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
8229 	 * before accessing it. Handle is in usual place in translate struct.
8230 	 */
8231 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
8232 
8233 	/*
8234 	 * Preset request sense data to NO SENSE.
8235 	 * Here it is redundant, only for a symetry with scsi-originated
8236 	 * packets. It should not be used for anything but debugging.
8237 	 */
8238 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
8239 	sata_fixed_sense_data_preset(
8240 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
8241 
8242 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
8243 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
8244 
8245 	return (SATA_SUCCESS);
8246 }
8247 
8248 /*
8249  * Set-up ATAPI packet command.
8250  * Data transfer direction has to be set-up in sata_cmd structure prior to
8251  * calling this function.
8252  *
8253  * Returns void
8254  */
8255 
8256 static void
8257 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo)
8258 {
8259 	scmd->satacmd_addr_type = 0;		/* N/A */
8260 	scmd->satacmd_sec_count_lsb = 0;	/* no tag */
8261 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
8262 	scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ;
8263 	scmd->satacmd_lba_high_lsb =
8264 	    (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8);
8265 	scmd->satacmd_cmd_reg = SATAC_PACKET;	/* Command */
8266 
8267 	/*
8268 	 * We want all data to be transfered via DMA.
8269 	 * But specify it only if drive supports DMA and DMA mode is
8270 	 * selected - some drives are sensitive about it.
8271 	 * Hopefully it wil work for all drives....
8272 	 */
8273 	if (sdinfo->satadrv_settings & SATA_DEV_DMA)
8274 		scmd->satacmd_features_reg = SATA_ATAPI_F_DMA;
8275 
8276 	/*
8277 	 * Features register requires special care for devices that use
8278 	 * Serial ATA bridge - they need an explicit specification of
8279 	 * the data transfer direction for Packet DMA commands.
8280 	 * Setting this bit is harmless if DMA is not used.
8281 	 *
8282 	 * Many drives do not implement word 80, specifying what ATA/ATAPI
8283 	 * spec they follow.
8284 	 * We are arbitrarily following the latest SerialATA 2.6 spec,
8285 	 * which uses ATA/ATAPI 6 specification for Identify Data, unless
8286 	 * ATA/ATAPI-7 support is explicitly indicated.
8287 	 */
8288 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
8289 	    sdinfo->satadrv_id.ai_majorversion != 0xffff &&
8290 	    (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) {
8291 		/*
8292 		 * Specification of major version is valid and version 7
8293 		 * is supported. It does automatically imply that all
8294 		 * spec features are supported. For now, we assume that
8295 		 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete.
8296 		 */
8297 		if ((sdinfo->satadrv_id.ai_dirdma &
8298 		    SATA_ATAPI_ID_DMADIR_REQ) != 0) {
8299 			if (scmd->satacmd_flags.sata_data_direction ==
8300 			    SATA_DIR_READ)
8301 			scmd->satacmd_features_reg |=
8302 			    SATA_ATAPI_F_DATA_DIR_READ;
8303 		}
8304 	}
8305 }
8306 
8307 
8308 #ifdef SATA_DEBUG
8309 
8310 /* Display 18 bytes of Inquiry data */
8311 static void
8312 sata_show_inqry_data(uint8_t *buf)
8313 {
8314 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
8315 	uint8_t *p;
8316 
8317 	cmn_err(CE_NOTE, "Inquiry data:");
8318 	cmn_err(CE_NOTE, "device type %x", inq->inq_dtype);
8319 	cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb);
8320 	cmn_err(CE_NOTE, "version %x", inq->inq_ansi);
8321 	cmn_err(CE_NOTE, "ATAPI transport version %d",
8322 	    SATA_ATAPI_TRANS_VERSION(inq));
8323 	cmn_err(CE_NOTE, "response data format %d, aenc %d",
8324 	    inq->inq_rdf, inq->inq_aenc);
8325 	cmn_err(CE_NOTE, " additional length %d", inq->inq_len);
8326 	cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs);
8327 	p = (uint8_t *)inq->inq_vid;
8328 	cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x "
8329 	    "%02x %02x %02x %02x",
8330 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
8331 	p = (uint8_t *)inq->inq_vid;
8332 	cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c",
8333 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
8334 
8335 	p = (uint8_t *)inq->inq_pid;
8336 	cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x "
8337 	    "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
8338 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
8339 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
8340 	p = (uint8_t *)inq->inq_pid;
8341 	cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c "
8342 	    "%c %c %c %c %c %c %c %c",
8343 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
8344 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
8345 
8346 	p = (uint8_t *)inq->inq_revision;
8347 	cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x",
8348 	    p[0], p[1], p[2], p[3]);
8349 	p = (uint8_t *)inq->inq_revision;
8350 	cmn_err(CE_NOTE, "revision: %c %c %c %c",
8351 	    p[0], p[1], p[2], p[3]);
8352 
8353 }
8354 
8355 
8356 static void
8357 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count)
8358 {
8359 	struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt;
8360 
8361 	if (scsi_pkt == NULL)
8362 		return;
8363 	if (count != 0) {
8364 		/* saving cdb */
8365 		bzero(sata_atapi_trace[sata_atapi_trace_index].acdb,
8366 		    SATA_ATAPI_MAX_CDB_LEN);
8367 		bcopy(scsi_pkt->pkt_cdbp,
8368 		    sata_atapi_trace[sata_atapi_trace_index].acdb, count);
8369 	} else {
8370 		bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)->
8371 		    sts_sensedata,
8372 		    sata_atapi_trace[sata_atapi_trace_index].arqs,
8373 		    SATA_ATAPI_MIN_RQSENSE_LEN);
8374 		sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason =
8375 		    scsi_pkt->pkt_reason;
8376 		sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason =
8377 		    spx->txlt_sata_pkt->satapkt_reason;
8378 
8379 		if (++sata_atapi_trace_index >= 64)
8380 			sata_atapi_trace_index = 0;
8381 	}
8382 }
8383 
8384 #endif
8385 
8386 /*
8387  * Fetch inquiry data from ATAPI device
8388  * Returns SATA_SUCCESS if operation was successfull, SATA_FAILURE otherwise.
8389  *
8390  * inqb pointer does not points to a DMA-able buffer. It is a local buffer
8391  * where the caller expects to see the inquiry data.
8392  *
8393  */
8394 
8395 static int
8396 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba,
8397     sata_address_t *saddr, struct scsi_inquiry *inq)
8398 {
8399 	sata_pkt_txlate_t *spx;
8400 	sata_pkt_t *spkt;
8401 	struct buf *bp;
8402 	sata_drive_info_t *sdinfo;
8403 	sata_cmd_t *scmd;
8404 	int rval;
8405 	uint8_t *rqsp;
8406 
8407 	ASSERT(sata_hba != NULL);
8408 
8409 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
8410 	spx->txlt_sata_hba_inst = sata_hba;
8411 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
8412 	spkt = sata_pkt_alloc(spx, NULL);
8413 	if (spkt == NULL) {
8414 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
8415 		return (SATA_FAILURE);
8416 	}
8417 	/* address is needed now */
8418 	spkt->satapkt_device.satadev_addr = *saddr;
8419 
8420 	/* scsi_inquiry size buffer */
8421 	bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry));
8422 	if (bp == NULL) {
8423 		sata_pkt_free(spx);
8424 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
8425 		SATA_LOG_D((sata_hba, CE_WARN,
8426 		    "sata_get_atapi_inquiry_data: "
8427 		    "cannot allocate data buffer"));
8428 		return (SATA_FAILURE);
8429 	}
8430 	bp_mapin(bp); /* make data buffer accessible */
8431 
8432 	scmd = &spkt->satapkt_cmd;
8433 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
8434 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
8435 
8436 	/* Use synchronous mode */
8437 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
8438 	spkt->satapkt_comp = NULL;
8439 	spkt->satapkt_time = sata_default_pkt_time;
8440 
8441 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
8442 
8443 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
8444 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
8445 
8446 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
8447 	sdinfo = sata_get_device_info(sata_hba,
8448 	    &spx->txlt_sata_pkt->satapkt_device);
8449 	if (sdinfo == NULL) {
8450 		/* we have to be carefull about the disapearing device */
8451 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
8452 		rval = SATA_FAILURE;
8453 		goto cleanup;
8454 	}
8455 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
8456 
8457 	/*
8458 	 * Set-up acdb. This works for atapi transport version 2 and later.
8459 	 */
8460 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
8461 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
8462 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
8463 	scmd->satacmd_acdb[1] = 0x00;
8464 	scmd->satacmd_acdb[2] = 0x00;
8465 	scmd->satacmd_acdb[3] = 0x00;
8466 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
8467 	scmd->satacmd_acdb[5] = 0x00;
8468 
8469 	sata_fixed_sense_data_preset(
8470 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
8471 
8472 	/* Transfer command to HBA */
8473 	if (sata_hba_start(spx, &rval) != 0) {
8474 		/* Pkt not accepted for execution */
8475 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
8476 		    "sata_get_atapi_inquiry_data: "
8477 		    "Packet not accepted for execution - ret: %02x", rval);
8478 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
8479 		rval = SATA_FAILURE;
8480 		goto cleanup;
8481 	}
8482 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
8483 
8484 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
8485 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
8486 		    "sata_get_atapi_inquiry_data: "
8487 		    "Packet completed successfully - ret: %02x", rval);
8488 		/*
8489 		 * Sync buffer. Handle is in usual place in translate struct.
8490 		 * Normal completion - copy data into caller's buffer
8491 		 */
8492 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
8493 		    DDI_DMA_SYNC_FORCPU);
8494 		ASSERT(rval == DDI_SUCCESS);
8495 		bcopy(bp->b_un.b_addr, (uint8_t *)inq,
8496 		    sizeof (struct scsi_inquiry));
8497 #ifdef SATA_DEBUG
8498 		if (sata_debug_flags & SATA_DBG_ATAPI) {
8499 			sata_show_inqry_data((uint8_t *)inq);
8500 		}
8501 #endif
8502 		rval = SATA_SUCCESS;
8503 	} else {
8504 		/*
8505 		 * Something went wrong - analyze return - check rqsense data
8506 		 */
8507 		rval = SATA_FAILURE;
8508 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
8509 			/*
8510 			 * ARQ data hopefull show something other than NO SENSE
8511 			 */
8512 			rqsp = scmd->satacmd_rqsense;
8513 			sata_log(spx->txlt_sata_hba_inst, CE_WARN,
8514 			    "ATAPI packet completion reason: %02x\n"
8515 			    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
8516 			    "          %02x %02x %02x %02x %02x %02x "
8517 			    "          %02x %02x %02x %02x %02x %02x\n",
8518 			    spkt->satapkt_reason,
8519 			    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
8520 			    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
8521 			    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
8522 			    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
8523 			    rqsp[16], rqsp[17]);
8524 		} else {
8525 			switch (spkt->satapkt_reason) {
8526 			case SATA_PKT_PORT_ERROR:
8527 				sata_log(sata_hba, CE_WARN,
8528 				    "sata_get_atapi_inquiry_data: "
8529 				    "packet reason: port error\n");
8530 				break;
8531 
8532 			case SATA_PKT_TIMEOUT:
8533 				sata_log(sata_hba, CE_WARN,
8534 				    "sata_get_atapi_inquiry_data: "
8535 				    "packet reason: timeout\n");
8536 				break;
8537 
8538 			case SATA_PKT_ABORTED:
8539 				sata_log(sata_hba, CE_WARN,
8540 				    "sata_get_atapi_inquiry_data: "
8541 				    "packet reason: aborted\n");
8542 				break;
8543 
8544 			case SATA_PKT_RESET:
8545 				sata_log(sata_hba, CE_WARN,
8546 				    "sata_get_atapi_inquiry_data: "
8547 				    "packet reason: reset\n");
8548 				break;
8549 			default:
8550 				sata_log(sata_hba, CE_WARN,
8551 				    "sata_get_atapi_inquiry_data: "
8552 				    "invalid packet reason: %02x\n",
8553 				    spkt->satapkt_reason);
8554 				break;
8555 			}
8556 		}
8557 	}
8558 cleanup:
8559 	sata_free_local_buffer(spx);
8560 	sata_pkt_free(spx);
8561 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
8562 	return (rval);
8563 }
8564 
8565 
8566 
8567 
8568 
8569 #if 0
8570 #ifdef SATA_DEBUG
8571 
8572 /*
8573  * Test ATAPI packet command.
8574  * Single threaded test: send packet command in synch mode, process completion
8575  *
8576  */
8577 static void
8578 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport)
8579 {
8580 	sata_pkt_txlate_t *spx;
8581 	sata_pkt_t *spkt;
8582 	struct buf *bp;
8583 	sata_device_t sata_device;
8584 	sata_drive_info_t *sdinfo;
8585 	sata_cmd_t *scmd;
8586 	int rval;
8587 	uint8_t *rqsp;
8588 
8589 	ASSERT(sata_hba_inst != NULL);
8590 	sata_device.satadev_addr.cport = cport;
8591 	sata_device.satadev_addr.pmport = 0;
8592 	sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
8593 	sata_device.satadev_rev = SATA_DEVICE_REV;
8594 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8595 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
8596 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8597 	if (sdinfo == NULL) {
8598 		sata_log(sata_hba_inst, CE_WARN,
8599 		    "sata_test_atapi_packet_command: "
8600 		    "no device info for cport %d",
8601 		    sata_device.satadev_addr.cport);
8602 		return;
8603 	}
8604 
8605 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
8606 	spx->txlt_sata_hba_inst = sata_hba_inst;
8607 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
8608 	spkt = sata_pkt_alloc(spx, NULL);
8609 	if (spkt == NULL) {
8610 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
8611 		return;
8612 	}
8613 	/* address is needed now */
8614 	spkt->satapkt_device.satadev_addr = sata_device.satadev_addr;
8615 
8616 	/* 1024k buffer */
8617 	bp = sata_alloc_local_buffer(spx, 1024);
8618 	if (bp == NULL) {
8619 		sata_pkt_free(spx);
8620 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
8621 		sata_log(sata_hba_inst, CE_WARN,
8622 		    "sata_test_atapi_packet_command: "
8623 		    "cannot allocate data buffer");
8624 		return;
8625 	}
8626 	bp_mapin(bp); /* make data buffer accessible */
8627 
8628 	scmd = &spkt->satapkt_cmd;
8629 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
8630 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
8631 
8632 	/* Use synchronous mode */
8633 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
8634 
8635 	/* Synchronous mode, no callback - may be changed by the caller */
8636 	spkt->satapkt_comp = NULL;
8637 	spkt->satapkt_time = sata_default_pkt_time;
8638 
8639 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
8640 
8641 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
8642 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
8643 
8644 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
8645 
8646 	/* Set-up acdb. */
8647 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
8648 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
8649 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
8650 	scmd->satacmd_acdb[1] = 0x00;
8651 	scmd->satacmd_acdb[2] = 0x00;
8652 	scmd->satacmd_acdb[3] = 0x00;
8653 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
8654 	scmd->satacmd_acdb[5] = 0x00;
8655 
8656 	sata_fixed_sense_data_preset(
8657 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
8658 
8659 	/* Transfer command to HBA */
8660 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8661 	if (sata_hba_start(spx, &rval) != 0) {
8662 		/* Pkt not accepted for execution */
8663 		sata_log(sata_hba_inst, CE_WARN,
8664 		    "sata_test_atapi_packet_command: "
8665 		    "Packet not accepted for execution - ret: %02x", rval);
8666 		mutex_exit(
8667 		    &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8668 		goto cleanup;
8669 	}
8670 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8671 
8672 	/*
8673 	 * Sync buffer. Handle is in usual place in translate struct.
8674 	 */
8675 	rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
8676 	    DDI_DMA_SYNC_FORCPU);
8677 	ASSERT(rval == DDI_SUCCESS);
8678 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
8679 		sata_log(sata_hba_inst, CE_WARN,
8680 		    "sata_test_atapi_packet_command: "
8681 		    "Packet completed successfully\n");
8682 		/*
8683 		 * Normal completion - show inquiry data
8684 		 */
8685 		sata_show_inqry_data((uint8_t *)bp->b_un.b_addr);
8686 	} else {
8687 		/*
8688 		 * Something went wrong - analyze return - check rqsense data
8689 		 */
8690 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
8691 			/*
8692 			 * ARQ data hopefull show something other than NO SENSE
8693 			 */
8694 			rqsp = scmd->satacmd_rqsense;
8695 			sata_log(spx->txlt_sata_hba_inst, CE_WARN,
8696 			    "ATAPI packet completion reason: %02x\n"
8697 			    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
8698 			    "          %02x %02x %02x %02x %02x %02x "
8699 			    "          %02x %02x %02x %02x %02x %02x\n",
8700 			    spkt->satapkt_reason,
8701 			    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
8702 			    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
8703 			    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
8704 			    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
8705 			    rqsp[16], rqsp[17]);
8706 		} else {
8707 			switch (spkt->satapkt_reason) {
8708 			case SATA_PKT_PORT_ERROR:
8709 				sata_log(sata_hba_inst, CE_WARN,
8710 				    "sata_test_atapi_packet_command: "
8711 				    "packet reason: port error\n");
8712 				break;
8713 
8714 			case SATA_PKT_TIMEOUT:
8715 				sata_log(sata_hba_inst, CE_WARN,
8716 				    "sata_test_atapi_packet_command: "
8717 				    "packet reason: timeout\n");
8718 				break;
8719 
8720 			case SATA_PKT_ABORTED:
8721 				sata_log(sata_hba_inst, CE_WARN,
8722 				    "sata_test_atapi_packet_command: "
8723 				    "packet reason: aborted\n");
8724 				break;
8725 
8726 			case SATA_PKT_RESET:
8727 				sata_log(sata_hba_inst, CE_WARN,
8728 				    "sata_test_atapi_packet_command: "
8729 				    "packet reason: reset\n");
8730 				break;
8731 			default:
8732 				sata_log(sata_hba_inst, CE_WARN,
8733 				    "sata_test_atapi_packet_command: "
8734 				    "invalid packet reason: %02x\n",
8735 				    spkt->satapkt_reason);
8736 				break;
8737 			}
8738 		}
8739 	}
8740 cleanup:
8741 	sata_free_local_buffer(spx);
8742 	sata_pkt_free(spx);
8743 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
8744 }
8745 
8746 #endif /* SATA_DEBUG */
8747 #endif /* 1 */
8748 
8749 
8750 /* ************************** LOCAL HELPER FUNCTIONS *********************** */
8751 
8752 /*
8753  * Validate sata_tran info
8754  * SATA_FAILURE returns if structure is inconsistent or structure revision
8755  * does not match one used by the framework.
8756  *
8757  * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains
8758  * required function pointers.
8759  * Returns SATA_FAILURE otherwise.
8760  */
8761 static int
8762 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran)
8763 {
8764 	/*
8765 	 * SATA_TRAN_HBA_REV is the current (highest) revision number
8766 	 * of the SATA interface.
8767 	 */
8768 	if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) {
8769 		sata_log(NULL, CE_WARN,
8770 		    "sata: invalid sata_hba_tran version %d for driver %s",
8771 		    sata_tran->sata_tran_hba_rev, ddi_driver_name(dip));
8772 		return (SATA_FAILURE);
8773 	}
8774 
8775 	if (dip != sata_tran->sata_tran_hba_dip) {
8776 		SATA_LOG_D((NULL, CE_WARN,
8777 		    "sata: inconsistent sata_tran_hba_dip "
8778 		    "%p / %p", sata_tran->sata_tran_hba_dip, dip));
8779 		return (SATA_FAILURE);
8780 	}
8781 
8782 	if (sata_tran->sata_tran_probe_port == NULL ||
8783 	    sata_tran->sata_tran_start == NULL ||
8784 	    sata_tran->sata_tran_abort == NULL ||
8785 	    sata_tran->sata_tran_reset_dport == NULL ||
8786 	    sata_tran->sata_tran_hotplug_ops == NULL ||
8787 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL ||
8788 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate ==
8789 	    NULL) {
8790 		SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing "
8791 		    "required functions"));
8792 	}
8793 	return (SATA_SUCCESS);
8794 }
8795 
8796 /*
8797  * Remove HBA instance from sata_hba_list.
8798  */
8799 static void
8800 sata_remove_hba_instance(dev_info_t *dip)
8801 {
8802 	sata_hba_inst_t	*sata_hba_inst;
8803 
8804 	mutex_enter(&sata_mutex);
8805 	for (sata_hba_inst = sata_hba_list;
8806 	    sata_hba_inst != (struct sata_hba_inst *)NULL;
8807 	    sata_hba_inst = sata_hba_inst->satahba_next) {
8808 		if (sata_hba_inst->satahba_dip == dip)
8809 			break;
8810 	}
8811 
8812 	if (sata_hba_inst == (struct sata_hba_inst *)NULL) {
8813 #ifdef SATA_DEBUG
8814 		cmn_err(CE_WARN, "sata_remove_hba_instance: "
8815 		    "unknown HBA instance\n");
8816 #endif
8817 		ASSERT(FALSE);
8818 	}
8819 	if (sata_hba_inst == sata_hba_list) {
8820 		sata_hba_list = sata_hba_inst->satahba_next;
8821 		if (sata_hba_list) {
8822 			sata_hba_list->satahba_prev =
8823 			    (struct sata_hba_inst *)NULL;
8824 		}
8825 		if (sata_hba_inst == sata_hba_list_tail) {
8826 			sata_hba_list_tail = NULL;
8827 		}
8828 	} else if (sata_hba_inst == sata_hba_list_tail) {
8829 		sata_hba_list_tail = sata_hba_inst->satahba_prev;
8830 		if (sata_hba_list_tail) {
8831 			sata_hba_list_tail->satahba_next =
8832 			    (struct sata_hba_inst *)NULL;
8833 		}
8834 	} else {
8835 		sata_hba_inst->satahba_prev->satahba_next =
8836 		    sata_hba_inst->satahba_next;
8837 		sata_hba_inst->satahba_next->satahba_prev =
8838 		    sata_hba_inst->satahba_prev;
8839 	}
8840 	mutex_exit(&sata_mutex);
8841 }
8842 
8843 
8844 
8845 
8846 
8847 /*
8848  * Probe all SATA ports of the specified HBA instance.
8849  * The assumption is that there are no target and attachment point minor nodes
8850  * created by the boot subsystems, so we do not need to prune device tree.
8851  *
8852  * This function is called only from sata_hba_attach(). It does not have to
8853  * be protected by controller mutex, because the hba_attached flag is not set
8854  * yet and no one would be touching this HBA instance other than this thread.
8855  * Determines if port is active and what type of the device is attached
8856  * (if any). Allocates necessary structures for each port.
8857  *
8858  * An AP (Attachement Point) node is created for each SATA device port even
8859  * when there is no device attached.
8860  */
8861 
8862 static 	void
8863 sata_probe_ports(sata_hba_inst_t *sata_hba_inst)
8864 {
8865 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
8866 	int			ncport, npmport;
8867 	sata_cport_info_t 	*cportinfo;
8868 	sata_drive_info_t	*drive;
8869 	sata_pmult_info_t	*pminfo;
8870 	sata_pmport_info_t 	*pmportinfo;
8871 	sata_device_t		sata_device;
8872 	int			rval;
8873 	dev_t			minor_number;
8874 	char			name[16];
8875 	clock_t			start_time, cur_time;
8876 
8877 	/*
8878 	 * Probe controller ports first, to find port status and
8879 	 * any port multiplier attached.
8880 	 */
8881 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
8882 		/* allocate cport structure */
8883 		cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP);
8884 		ASSERT(cportinfo != NULL);
8885 		mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL);
8886 
8887 		mutex_enter(&cportinfo->cport_mutex);
8888 
8889 		cportinfo->cport_addr.cport = ncport;
8890 		cportinfo->cport_addr.pmport = 0;
8891 		cportinfo->cport_addr.qual = SATA_ADDR_CPORT;
8892 		cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
8893 		cportinfo->cport_state |= SATA_STATE_PROBING;
8894 		SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo;
8895 
8896 		/*
8897 		 * Regardless if a port is usable or not, create
8898 		 * an attachment point
8899 		 */
8900 		mutex_exit(&cportinfo->cport_mutex);
8901 		minor_number =
8902 		    SATA_MAKE_AP_MINOR(ddi_get_instance(dip), ncport, 0, 0);
8903 		(void) sprintf(name, "%d", ncport);
8904 		if (ddi_create_minor_node(dip, name, S_IFCHR,
8905 		    minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) !=
8906 		    DDI_SUCCESS) {
8907 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
8908 			    "cannot create SATA attachment point for port %d",
8909 			    ncport);
8910 		}
8911 
8912 		/* Probe port */
8913 		start_time = ddi_get_lbolt();
8914 	reprobe_cport:
8915 		sata_device.satadev_addr.cport = ncport;
8916 		sata_device.satadev_addr.pmport = 0;
8917 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
8918 		sata_device.satadev_rev = SATA_DEVICE_REV;
8919 
8920 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8921 		    (dip, &sata_device);
8922 
8923 		mutex_enter(&cportinfo->cport_mutex);
8924 		sata_update_port_scr(&cportinfo->cport_scr, &sata_device);
8925 		if (rval != SATA_SUCCESS) {
8926 			/* Something went wrong? Fail the port */
8927 			cportinfo->cport_state = SATA_PSTATE_FAILED;
8928 			mutex_exit(&cportinfo->cport_mutex);
8929 			continue;
8930 		}
8931 		cportinfo->cport_state &= ~SATA_STATE_PROBING;
8932 		cportinfo->cport_state |= SATA_STATE_PROBED;
8933 		cportinfo->cport_dev_type = sata_device.satadev_type;
8934 
8935 		cportinfo->cport_state |= SATA_STATE_READY;
8936 		if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) {
8937 			mutex_exit(&cportinfo->cport_mutex);
8938 			continue;
8939 		}
8940 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
8941 			/*
8942 			 * There is some device attached.
8943 			 * Allocate device info structure
8944 			 */
8945 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) {
8946 				mutex_exit(&cportinfo->cport_mutex);
8947 				SATA_CPORTINFO_DRV_INFO(cportinfo) =
8948 				    kmem_zalloc(sizeof (sata_drive_info_t),
8949 				    KM_SLEEP);
8950 				mutex_enter(&cportinfo->cport_mutex);
8951 			}
8952 			drive = SATA_CPORTINFO_DRV_INFO(cportinfo);
8953 			drive->satadrv_addr = cportinfo->cport_addr;
8954 			drive->satadrv_addr.qual = SATA_ADDR_DCPORT;
8955 			drive->satadrv_type = cportinfo->cport_dev_type;
8956 			drive->satadrv_state = SATA_STATE_UNKNOWN;
8957 
8958 			mutex_exit(&cportinfo->cport_mutex);
8959 			if (sata_add_device(dip, sata_hba_inst, ncport, 0) !=
8960 			    SATA_SUCCESS) {
8961 				/*
8962 				 * Plugged device was not correctly identified.
8963 				 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT
8964 				 */
8965 				cur_time = ddi_get_lbolt();
8966 				if ((cur_time - start_time) <
8967 				    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
8968 					/* sleep for a while */
8969 					delay(drv_usectohz(
8970 					    SATA_DEV_IDENTIFY_RETRY_DELAY));
8971 					goto reprobe_cport;
8972 				}
8973 			}
8974 		} else {
8975 			mutex_exit(&cportinfo->cport_mutex);
8976 			ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT);
8977 			pminfo = kmem_zalloc(sizeof (sata_pmult_info_t),
8978 			    KM_SLEEP);
8979 			mutex_enter(&cportinfo->cport_mutex);
8980 			ASSERT(pminfo != NULL);
8981 			SATA_CPORTINFO_PMULT_INFO(cportinfo) = pminfo;
8982 			pminfo->pmult_addr.cport = cportinfo->cport_addr.cport;
8983 			pminfo->pmult_addr.pmport = SATA_PMULT_HOSTPORT;
8984 			pminfo->pmult_addr.qual = SATA_ADDR_PMPORT;
8985 			pminfo->pmult_num_dev_ports =
8986 			    sata_device.satadev_add_info;
8987 			mutex_init(&pminfo->pmult_mutex, NULL, MUTEX_DRIVER,
8988 			    NULL);
8989 			pminfo->pmult_state = SATA_STATE_PROBING;
8990 			mutex_exit(&cportinfo->cport_mutex);
8991 
8992 			/* Probe Port Multiplier ports */
8993 			for (npmport = 0;
8994 			    npmport < pminfo->pmult_num_dev_ports;
8995 			    npmport++) {
8996 				pmportinfo = kmem_zalloc(
8997 				    sizeof (sata_pmport_info_t), KM_SLEEP);
8998 				mutex_enter(&cportinfo->cport_mutex);
8999 				ASSERT(pmportinfo != NULL);
9000 				pmportinfo->pmport_addr.cport = ncport;
9001 				pmportinfo->pmport_addr.pmport = npmport;
9002 				pmportinfo->pmport_addr.qual =
9003 				    SATA_ADDR_PMPORT;
9004 				pminfo->pmult_dev_port[npmport] = pmportinfo;
9005 
9006 				mutex_init(&pmportinfo->pmport_mutex, NULL,
9007 				    MUTEX_DRIVER, NULL);
9008 
9009 				mutex_exit(&cportinfo->cport_mutex);
9010 
9011 				/* Create an attachment point */
9012 				minor_number = SATA_MAKE_AP_MINOR(
9013 				    ddi_get_instance(dip), ncport, npmport, 1);
9014 				(void) sprintf(name, "%d.%d", ncport, npmport);
9015 				if (ddi_create_minor_node(dip, name, S_IFCHR,
9016 				    minor_number, DDI_NT_SATA_ATTACHMENT_POINT,
9017 				    0) != DDI_SUCCESS) {
9018 					sata_log(sata_hba_inst, CE_WARN,
9019 					    "sata_hba_attach: "
9020 					    "cannot create SATA attachment "
9021 					    "point for port %d pmult port %d",
9022 					    ncport, npmport);
9023 				}
9024 
9025 				start_time = ddi_get_lbolt();
9026 			reprobe_pmport:
9027 				sata_device.satadev_addr.pmport = npmport;
9028 				sata_device.satadev_addr.qual =
9029 				    SATA_ADDR_PMPORT;
9030 
9031 				rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
9032 				    (dip, &sata_device);
9033 				mutex_enter(&cportinfo->cport_mutex);
9034 
9035 				/* sata_update_port_info() */
9036 				sata_update_port_scr(&pmportinfo->pmport_scr,
9037 				    &sata_device);
9038 
9039 				if (rval != SATA_SUCCESS) {
9040 					pmportinfo->pmport_state =
9041 					    SATA_PSTATE_FAILED;
9042 					mutex_exit(&cportinfo->cport_mutex);
9043 					continue;
9044 				}
9045 				pmportinfo->pmport_state &=
9046 				    ~SATA_STATE_PROBING;
9047 				pmportinfo->pmport_state |= SATA_STATE_PROBED;
9048 				pmportinfo->pmport_dev_type =
9049 				    sata_device.satadev_type;
9050 
9051 				pmportinfo->pmport_state |= SATA_STATE_READY;
9052 				if (pmportinfo->pmport_dev_type ==
9053 				    SATA_DTYPE_NONE) {
9054 					mutex_exit(&cportinfo->cport_mutex);
9055 					continue;
9056 				}
9057 				/* Port multipliers cannot be chained */
9058 				ASSERT(pmportinfo->pmport_dev_type !=
9059 				    SATA_DTYPE_PMULT);
9060 				/*
9061 				 * There is something attached to Port
9062 				 * Multiplier device port
9063 				 * Allocate device info structure
9064 				 */
9065 				if (pmportinfo->pmport_sata_drive == NULL) {
9066 					mutex_exit(&cportinfo->cport_mutex);
9067 					pmportinfo->pmport_sata_drive =
9068 					    kmem_zalloc(
9069 					    sizeof (sata_drive_info_t),
9070 					    KM_SLEEP);
9071 					mutex_enter(&cportinfo->cport_mutex);
9072 				}
9073 				drive = pmportinfo->pmport_sata_drive;
9074 				drive->satadrv_addr.cport =
9075 				    pmportinfo->pmport_addr.cport;
9076 				drive->satadrv_addr.pmport = npmport;
9077 				drive->satadrv_addr.qual = SATA_ADDR_DPMPORT;
9078 				drive->satadrv_type = pmportinfo->
9079 				    pmport_dev_type;
9080 				drive->satadrv_state = SATA_STATE_UNKNOWN;
9081 
9082 				mutex_exit(&cportinfo->cport_mutex);
9083 				if (sata_add_device(dip, sata_hba_inst, ncport,
9084 				    npmport) != SATA_SUCCESS) {
9085 					/*
9086 					 * Plugged device was not correctly
9087 					 * identified. Retry, within the
9088 					 * SATA_DEV_IDENTIFY_TIMEOUT
9089 					 */
9090 					cur_time = ddi_get_lbolt();
9091 					if ((cur_time - start_time) <
9092 					    drv_usectohz(
9093 					    SATA_DEV_IDENTIFY_TIMEOUT)) {
9094 						/* sleep for a while */
9095 						delay(drv_usectohz(
9096 						SATA_DEV_IDENTIFY_RETRY_DELAY));
9097 						goto reprobe_pmport;
9098 					}
9099 				}
9100 			}
9101 			pmportinfo->pmport_state =
9102 			    SATA_STATE_PROBED | SATA_STATE_READY;
9103 		}
9104 	}
9105 }
9106 
9107 /*
9108  * Add SATA device for specified HBA instance & port (SCSI target
9109  * device nodes).
9110  * This function is called (indirectly) only from sata_hba_attach().
9111  * A target node is created when there is a supported type device attached,
9112  * but may be removed if it cannot be put online.
9113  *
9114  * This function cannot be called from an interrupt context.
9115  *
9116  * ONLY DISK TARGET NODES ARE CREATED NOW
9117  *
9118  * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when
9119  * device identification failed - adding a device could be retried.
9120  *
9121  */
9122 static 	int
9123 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst, int cport,
9124     int pmport)
9125 {
9126 	sata_cport_info_t 	*cportinfo;
9127 	sata_pmult_info_t	*pminfo;
9128 	sata_pmport_info_t	*pmportinfo;
9129 	dev_info_t		*cdip;		/* child dip */
9130 	sata_device_t		sata_device;
9131 	int			rval;
9132 
9133 
9134 
9135 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
9136 	ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE);
9137 	mutex_enter(&cportinfo->cport_mutex);
9138 	/*
9139 	 * Some device is attached to a controller port.
9140 	 * We rely on controllers distinquishing between no-device,
9141 	 * attached port multiplier and other kind of attached device.
9142 	 * We need to get Identify Device data and determine
9143 	 * positively the dev type before trying to attach
9144 	 * the target driver.
9145 	 */
9146 	sata_device.satadev_rev = SATA_DEVICE_REV;
9147 	if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
9148 		/*
9149 		 * Not port multiplier.
9150 		 */
9151 		sata_device.satadev_addr = cportinfo->cport_addr;
9152 		sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
9153 		mutex_exit(&cportinfo->cport_mutex);
9154 
9155 		rval = sata_probe_device(sata_hba_inst, &sata_device);
9156 		if (rval != SATA_SUCCESS ||
9157 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN)
9158 			return (SATA_FAILURE);
9159 
9160 		mutex_enter(&cportinfo->cport_mutex);
9161 		sata_show_drive_info(sata_hba_inst,
9162 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
9163 
9164 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
9165 			/*
9166 			 * Could not determine device type or
9167 			 * a device is not supported.
9168 			 * Degrade this device to unknown.
9169 			 */
9170 			cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
9171 			mutex_exit(&cportinfo->cport_mutex);
9172 			return (SATA_SUCCESS);
9173 		}
9174 		cportinfo->cport_dev_type = sata_device.satadev_type;
9175 		cportinfo->cport_tgtnode_clean = B_TRUE;
9176 		mutex_exit(&cportinfo->cport_mutex);
9177 
9178 		/*
9179 		 * Initialize device to the desired state. Even if it
9180 		 * fails, the device will still attach but syslog
9181 		 * will show the warning.
9182 		 */
9183 		if (sata_initialize_device(sata_hba_inst,
9184 		    SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS)
9185 			/* Retry */
9186 			(void) sata_initialize_device(sata_hba_inst,
9187 			    SATA_CPORTINFO_DRV_INFO(cportinfo));
9188 
9189 		cdip = sata_create_target_node(pdip, sata_hba_inst,
9190 		    &sata_device.satadev_addr);
9191 		mutex_enter(&cportinfo->cport_mutex);
9192 		if (cdip == NULL) {
9193 			/*
9194 			 * Attaching target node failed.
9195 			 * We retain sata_drive_info structure...
9196 			 */
9197 			mutex_exit(&cportinfo->cport_mutex);
9198 			return (SATA_SUCCESS);
9199 		}
9200 		(SATA_CPORTINFO_DRV_INFO(cportinfo))->
9201 		    satadrv_state = SATA_STATE_READY;
9202 	} else {
9203 		/* This must be Port Multiplier type */
9204 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
9205 			SATA_LOG_D((sata_hba_inst, CE_WARN,
9206 			    "sata_add_device: "
9207 			    "unrecognized dev type %x",
9208 			    cportinfo->cport_dev_type));
9209 			mutex_exit(&cportinfo->cport_mutex);
9210 			return (SATA_SUCCESS);
9211 		}
9212 		pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
9213 		pmportinfo = pminfo->pmult_dev_port[pmport];
9214 		sata_device.satadev_addr = pmportinfo->pmport_addr;
9215 		sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
9216 		mutex_exit(&cportinfo->cport_mutex);
9217 
9218 		rval = sata_probe_device(sata_hba_inst, &sata_device);
9219 		if (rval != SATA_SUCCESS ||
9220 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN) {
9221 			return (SATA_FAILURE);
9222 		}
9223 		mutex_enter(&cportinfo->cport_mutex);
9224 		sata_show_drive_info(sata_hba_inst,
9225 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
9226 
9227 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
9228 			/*
9229 			 * Could not determine device type.
9230 			 * Degrade this device to unknown.
9231 			 */
9232 			pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
9233 			mutex_exit(&cportinfo->cport_mutex);
9234 			return (SATA_SUCCESS);
9235 		}
9236 		pmportinfo->pmport_dev_type = sata_device.satadev_type;
9237 		pmportinfo->pmport_tgtnode_clean = B_TRUE;
9238 		mutex_exit(&cportinfo->cport_mutex);
9239 
9240 		/*
9241 		 * Initialize device to the desired state.
9242 		 * Even if it fails, the device will still
9243 		 * attach but syslog will show the warning.
9244 		 */
9245 		if (sata_initialize_device(sata_hba_inst,
9246 		    pmportinfo->pmport_sata_drive) != SATA_SUCCESS)
9247 			/* Retry */
9248 			(void) sata_initialize_device(sata_hba_inst,
9249 			    pmportinfo->pmport_sata_drive);
9250 
9251 		cdip = sata_create_target_node(pdip, sata_hba_inst,
9252 		    &sata_device.satadev_addr);
9253 		mutex_enter(&cportinfo->cport_mutex);
9254 		if (cdip == NULL) {
9255 			/*
9256 			 * Attaching target node failed.
9257 			 * We retain sata_drive_info structure...
9258 			 */
9259 			mutex_exit(&cportinfo->cport_mutex);
9260 			return (SATA_SUCCESS);
9261 		}
9262 		pmportinfo->pmport_sata_drive->satadrv_state |=
9263 		    SATA_STATE_READY;
9264 	}
9265 	mutex_exit(&cportinfo->cport_mutex);
9266 	return (SATA_SUCCESS);
9267 }
9268 
9269 
9270 
9271 /*
9272  * Create scsi target node for attached device, create node properties and
9273  * attach the node.
9274  * The node could be removed if the device onlining fails.
9275  *
9276  * A dev_info_t pointer is returned if operation is successful, NULL is
9277  * returned otherwise.
9278  *
9279  * No port multiplier support.
9280  */
9281 
9282 static dev_info_t *
9283 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst,
9284 			sata_address_t *sata_addr)
9285 {
9286 	dev_info_t *cdip = NULL;
9287 	int rval;
9288 	char *nname = NULL;
9289 	char **compatible = NULL;
9290 	int ncompatible;
9291 	struct scsi_inquiry inq;
9292 	sata_device_t sata_device;
9293 	sata_drive_info_t *sdinfo;
9294 	int target;
9295 	int i;
9296 
9297 	sata_device.satadev_rev = SATA_DEVICE_REV;
9298 	sata_device.satadev_addr = *sata_addr;
9299 
9300 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport)));
9301 
9302 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
9303 
9304 	target = SATA_TO_SCSI_TARGET(sata_addr->cport,
9305 	    sata_addr->pmport, sata_addr->qual);
9306 
9307 	if (sdinfo == NULL) {
9308 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9309 		    sata_addr->cport)));
9310 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9311 		    "sata_create_target_node: no sdinfo for target %x",
9312 		    target));
9313 		return (NULL);
9314 	}
9315 
9316 	/*
9317 	 * create or get scsi inquiry data, expected by
9318 	 * scsi_hba_nodename_compatible_get()
9319 	 * SATA hard disks get Identify Data translated into Inguiry Data.
9320 	 * ATAPI devices respond directly to Inquiry request.
9321 	 */
9322 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9323 		sata_identdev_to_inquiry(sata_hba_inst, sdinfo,
9324 		    (uint8_t *)&inq);
9325 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9326 		    sata_addr->cport)));
9327 	} else { /* Assume supported ATAPI device */
9328 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9329 		    sata_addr->cport)));
9330 		if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr,
9331 		    &inq) == SATA_FAILURE)
9332 			return (NULL);
9333 		/*
9334 		 * Save supported ATAPI transport version
9335 		 */
9336 		sdinfo->satadrv_atapi_trans_ver =
9337 		    SATA_ATAPI_TRANS_VERSION(&inq);
9338 	}
9339 
9340 	/* determine the node name and compatible */
9341 	scsi_hba_nodename_compatible_get(&inq, NULL,
9342 	    inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
9343 
9344 #ifdef SATA_DEBUG
9345 	if (sata_debug_flags & SATA_DBG_NODES) {
9346 		if (nname == NULL) {
9347 			cmn_err(CE_NOTE, "sata_create_target_node: "
9348 			    "cannot determine nodename for target %d\n",
9349 			    target);
9350 		} else {
9351 			cmn_err(CE_WARN, "sata_create_target_node: "
9352 			    "target %d nodename: %s\n", target, nname);
9353 		}
9354 		if (compatible == NULL) {
9355 			cmn_err(CE_WARN,
9356 			    "sata_create_target_node: no compatible name\n");
9357 		} else {
9358 			for (i = 0; i < ncompatible; i++) {
9359 				cmn_err(CE_WARN, "sata_create_target_node: "
9360 				    "compatible name: %s\n", compatible[i]);
9361 			}
9362 		}
9363 	}
9364 #endif
9365 
9366 	/* if nodename can't be determined, log error and exit */
9367 	if (nname == NULL) {
9368 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9369 		    "sata_create_target_node: cannot determine nodename "
9370 		    "for target %d\n", target));
9371 		scsi_hba_nodename_compatible_free(nname, compatible);
9372 		return (NULL);
9373 	}
9374 	/*
9375 	 * Create scsi target node
9376 	 */
9377 	ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip);
9378 	rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
9379 	    "device-type", "scsi");
9380 
9381 	if (rval != DDI_PROP_SUCCESS) {
9382 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
9383 		    "updating device_type prop failed %d", rval));
9384 		goto fail;
9385 	}
9386 
9387 	/*
9388 	 * Create target node properties: target & lun
9389 	 */
9390 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target);
9391 	if (rval != DDI_PROP_SUCCESS) {
9392 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
9393 		    "updating target prop failed %d", rval));
9394 		goto fail;
9395 	}
9396 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0);
9397 	if (rval != DDI_PROP_SUCCESS) {
9398 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
9399 		    "updating target prop failed %d", rval));
9400 		goto fail;
9401 	}
9402 
9403 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
9404 		/*
9405 		 * Add "variant" property
9406 		 */
9407 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
9408 		    "variant", "atapi");
9409 		if (rval != DDI_PROP_SUCCESS) {
9410 			SATA_LOG_D((sata_hba_inst, CE_WARN,
9411 			    "sata_create_target_node: variant atapi "
9412 			    "property could not be created: %d", rval));
9413 			goto fail;
9414 		}
9415 	}
9416 	/* decorate the node with compatible */
9417 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible",
9418 	    compatible, ncompatible) != DDI_PROP_SUCCESS) {
9419 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9420 		    "sata_create_target_node: FAIL compatible props cdip 0x%p",
9421 		    (void *)cdip));
9422 		goto fail;
9423 	}
9424 
9425 
9426 	/*
9427 	 * Now, try to attach the driver. If probing of the device fails,
9428 	 * the target node may be removed
9429 	 */
9430 	rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH);
9431 
9432 	scsi_hba_nodename_compatible_free(nname, compatible);
9433 
9434 	if (rval == NDI_SUCCESS)
9435 		return (cdip);
9436 
9437 	/* target node was removed - are we sure? */
9438 	return (NULL);
9439 
9440 fail:
9441 	scsi_hba_nodename_compatible_free(nname, compatible);
9442 	ddi_prop_remove_all(cdip);
9443 	rval = ndi_devi_free(cdip);
9444 	if (rval != NDI_SUCCESS) {
9445 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
9446 		    "node removal failed %d", rval));
9447 	}
9448 	sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: "
9449 	    "cannot create target node for SATA device at port %d",
9450 	    sata_addr->cport);
9451 	return (NULL);
9452 }
9453 
9454 
9455 
9456 /*
9457  * Re-probe sata port, check for a device and attach info
9458  * structures when necessary. Identify Device data is fetched, if possible.
9459  * Assumption: sata address is already validated.
9460  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
9461  * the presence of a device and its type.
9462  *
9463  * flag arg specifies that the function should try multiple times to identify
9464  * device type and to initialize it, or it should return immediately on failure.
9465  * SATA_DEV_IDENTIFY_RETRY - retry
9466  * SATA_DEV_IDENTIFY_NORETRY - no retry
9467  *
9468  * SATA_FAILURE is returned if one of the operations failed.
9469  *
9470  * This function cannot be called in interrupt context - it may sleep.
9471  */
9472 static int
9473 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
9474     int flag)
9475 {
9476 	sata_cport_info_t *cportinfo;
9477 	sata_drive_info_t *sdinfo;
9478 	boolean_t init_device = B_FALSE;
9479 	int prev_device_type = SATA_DTYPE_NONE;
9480 	int prev_device_settings = 0;
9481 	clock_t start_time;
9482 	int retry = B_FALSE;
9483 	int rval;
9484 
9485 	/* We only care about host sata cport for now */
9486 	cportinfo = SATA_CPORT_INFO(sata_hba_inst,
9487 	    sata_device->satadev_addr.cport);
9488 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
9489 	if (sdinfo != NULL) {
9490 		/*
9491 		 * We are re-probing port with a previously attached device.
9492 		 * Save previous device type and settings
9493 		 */
9494 		prev_device_type = cportinfo->cport_dev_type;
9495 		prev_device_settings = sdinfo->satadrv_settings;
9496 	}
9497 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
9498 		start_time = ddi_get_lbolt();
9499 		retry = B_TRUE;
9500 	}
9501 retry_probe:
9502 
9503 	/* probe port */
9504 	mutex_enter(&cportinfo->cport_mutex);
9505 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
9506 	cportinfo->cport_state |= SATA_STATE_PROBING;
9507 	mutex_exit(&cportinfo->cport_mutex);
9508 
9509 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
9510 	    (SATA_DIP(sata_hba_inst), sata_device);
9511 
9512 	mutex_enter(&cportinfo->cport_mutex);
9513 	if (rval != SATA_SUCCESS) {
9514 		cportinfo->cport_state = SATA_PSTATE_FAILED;
9515 		mutex_exit(&cportinfo->cport_mutex);
9516 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: "
9517 		    "SATA port %d probing failed",
9518 		    cportinfo->cport_addr.cport));
9519 		return (SATA_FAILURE);
9520 	}
9521 
9522 	/*
9523 	 * update sata port state and set device type
9524 	 */
9525 	sata_update_port_info(sata_hba_inst, sata_device);
9526 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
9527 
9528 	/*
9529 	 * Sanity check - Port is active? Is the link active?
9530 	 * Is there any device attached?
9531 	 */
9532 	if ((cportinfo->cport_state &
9533 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
9534 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
9535 	    SATA_PORT_DEVLINK_UP) {
9536 		/*
9537 		 * Port in non-usable state or no link active/no device.
9538 		 * Free info structure if necessary (direct attached drive
9539 		 * only, for now!
9540 		 */
9541 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
9542 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
9543 		/* Add here differentiation for device attached or not */
9544 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
9545 		mutex_exit(&cportinfo->cport_mutex);
9546 		if (sdinfo != NULL)
9547 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
9548 		return (SATA_SUCCESS);
9549 	}
9550 
9551 	cportinfo->cport_state |= SATA_STATE_READY;
9552 	cportinfo->cport_dev_type = sata_device->satadev_type;
9553 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
9554 
9555 	/*
9556 	 * If we are re-probing the port, there may be
9557 	 * sata_drive_info structure attached
9558 	 * (or sata_pm_info, if PMult is supported).
9559 	 */
9560 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
9561 		/*
9562 		 * There is no device, so remove device info structure,
9563 		 * if necessary. Direct attached drive only!
9564 		 */
9565 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
9566 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
9567 		if (sdinfo != NULL) {
9568 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
9569 			sata_log(sata_hba_inst, CE_WARN,
9570 			    "SATA device detached "
9571 			    "from port %d", cportinfo->cport_addr.cport);
9572 		}
9573 		mutex_exit(&cportinfo->cport_mutex);
9574 		return (SATA_SUCCESS);
9575 	}
9576 
9577 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
9578 		if (sdinfo == NULL) {
9579 			/*
9580 			 * There is some device attached, but there is
9581 			 * no sata_drive_info structure - allocate one
9582 			 */
9583 			mutex_exit(&cportinfo->cport_mutex);
9584 			sdinfo = kmem_zalloc(
9585 			    sizeof (sata_drive_info_t), KM_SLEEP);
9586 			mutex_enter(&cportinfo->cport_mutex);
9587 			/*
9588 			 * Recheck, that the port state did not change when we
9589 			 * released mutex.
9590 			 */
9591 			if (cportinfo->cport_state & SATA_STATE_READY) {
9592 				SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo;
9593 				sdinfo->satadrv_addr = cportinfo->cport_addr;
9594 				sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT;
9595 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9596 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
9597 			} else {
9598 				/*
9599 				 * Port is not in ready state, we
9600 				 * cannot attach a device.
9601 				 */
9602 				mutex_exit(&cportinfo->cport_mutex);
9603 				kmem_free(sdinfo, sizeof (sata_drive_info_t));
9604 				return (SATA_SUCCESS);
9605 			}
9606 			/*
9607 			 * Since we are adding device, presumably new one,
9608 			 * indicate that it  should be initalized,
9609 			 * as well as some internal framework states).
9610 			 */
9611 			init_device = B_TRUE;
9612 		}
9613 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
9614 		sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
9615 	} else {
9616 		/*
9617 		 * The device is a port multiplier - not handled now.
9618 		 */
9619 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
9620 		mutex_exit(&cportinfo->cport_mutex);
9621 		return (SATA_SUCCESS);
9622 	}
9623 	mutex_exit(&cportinfo->cport_mutex);
9624 	/*
9625 	 * Figure out what kind of device we are really
9626 	 * dealing with.
9627 	 */
9628 	rval = sata_probe_device(sata_hba_inst, sata_device);
9629 
9630 	if (rval == SATA_SUCCESS) {
9631 		/*
9632 		 * If we are dealing with the same type of a device as before,
9633 		 * restore its settings flags.
9634 		 */
9635 		if (sata_device->satadev_type == prev_device_type)
9636 			sdinfo->satadrv_settings = prev_device_settings;
9637 
9638 		/* Set initial device features, if necessary */
9639 		if (init_device == B_TRUE) {
9640 			rval = sata_initialize_device(sata_hba_inst, sdinfo);
9641 		}
9642 		if (rval == SATA_SUCCESS)
9643 			return (rval);
9644 	}
9645 
9646 	if (retry) {
9647 		clock_t cur_time = ddi_get_lbolt();
9648 		/*
9649 		 * A device was not successfully identified or initialized.
9650 		 * Track retry time for device identification.
9651 		 */
9652 		if ((cur_time - start_time) <
9653 		    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
9654 			/* sleep for a while */
9655 			delay(drv_usectohz(SATA_DEV_IDENTIFY_RETRY_DELAY));
9656 			goto retry_probe;
9657 		}
9658 	}
9659 	return (rval);
9660 }
9661 
9662 /*
9663  * Initialize device
9664  * Specified device is initialized to a default state.
9665  *
9666  * Returns SATA_SUCCESS if all device features are set successfully,
9667  * SATA_FAILURE otherwise
9668  */
9669 static int
9670 sata_initialize_device(sata_hba_inst_t *sata_hba_inst,
9671     sata_drive_info_t *sdinfo)
9672 {
9673 	int rval;
9674 
9675 	sata_save_drive_settings(sdinfo);
9676 
9677 	sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
9678 
9679 	sata_init_write_cache_mode(sdinfo);
9680 
9681 	rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0);
9682 
9683 	/* Determine current data transfer mode */
9684 	if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) {
9685 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
9686 	} else if ((sdinfo->satadrv_id.ai_validinfo &
9687 	    SATA_VALIDINFO_88) != 0 &&
9688 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) {
9689 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
9690 	} else if ((sdinfo->satadrv_id.ai_dworddma &
9691 	    SATA_MDMA_SEL_MASK) != 0) {
9692 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
9693 	} else
9694 		/* DMA supported, not no DMA transfer mode is selected !? */
9695 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
9696 
9697 	return (rval);
9698 }
9699 
9700 
9701 /*
9702  * Initialize write cache mode.
9703  *
9704  * The default write cache setting for SATA HDD is provided by sata_write_cache
9705  * static variable. ATAPI CD/DVDs devices have write cache default is
9706  * determined by sata_atapicdvd_write_cache static variable.
9707  * 1 - enable
9708  * 0 - disable
9709  * any other value - current drive setting
9710  *
9711  * Although there is not reason to disable write cache on CD/DVD devices,
9712  * the default setting control is provided for the maximun flexibility.
9713  *
9714  * In the future, it may be overridden by the
9715  * disk-write-cache-enable property setting, if it is defined.
9716  * Returns SATA_SUCCESS if all device features are set successfully,
9717  * SATA_FAILURE otherwise.
9718  */
9719 static void
9720 sata_init_write_cache_mode(sata_drive_info_t *sdinfo)
9721 {
9722 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9723 		if (sata_write_cache == 1)
9724 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
9725 		else if (sata_write_cache == 0)
9726 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
9727 		/*
9728 		 * When sata_write_cache value is not 0 or 1,
9729 		 * a current setting of the drive's write cache is used.
9730 		 */
9731 	} else { /* Assume ATAPI CD/DVD device */
9732 		if (sata_atapicdvd_write_cache == 1)
9733 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
9734 		else if (sata_atapicdvd_write_cache == 0)
9735 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
9736 		/*
9737 		 * When sata_write_cache value is not 0 or 1,
9738 		 * a current setting of the drive's write cache is used.
9739 		 */
9740 	}
9741 }
9742 
9743 
9744 /*
9745  * Validate sata address.
9746  * Specified cport, pmport and qualifier has to match
9747  * passed sata_scsi configuration info.
9748  * The presence of an attached device is not verified.
9749  *
9750  * Returns 0 when address is valid, -1 otherwise.
9751  */
9752 static int
9753 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport,
9754 	int pmport, int qual)
9755 {
9756 	if (qual == SATA_ADDR_DCPORT && pmport != 0)
9757 		goto invalid_address;
9758 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
9759 		goto invalid_address;
9760 	if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) &&
9761 	    ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) ||
9762 	    (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) ||
9763 	    (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport))))
9764 		goto invalid_address;
9765 
9766 	return (0);
9767 
9768 invalid_address:
9769 	return (-1);
9770 
9771 }
9772 
9773 /*
9774  * Validate scsi address
9775  * SCSI target address is translated into SATA cport/pmport and compared
9776  * with a controller port/device configuration. LUN has to be 0.
9777  * Returns 0 if a scsi target refers to an attached device,
9778  * returns 1 if address is valid but device is not attached,
9779  * returns -1 if bad address or device is of an unsupported type.
9780  * Upon return sata_device argument is set.
9781  */
9782 static int
9783 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst,
9784 	struct scsi_address *ap, sata_device_t *sata_device)
9785 {
9786 	int cport, pmport, qual, rval;
9787 
9788 	rval = -1;	/* Invalid address */
9789 	if (ap->a_lun != 0)
9790 		goto out;
9791 
9792 	qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
9793 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
9794 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
9795 
9796 	if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT)
9797 		goto out;
9798 
9799 	if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) ==
9800 	    0) {
9801 
9802 		sata_cport_info_t *cportinfo;
9803 		sata_pmult_info_t *pmultinfo;
9804 		sata_drive_info_t *sdinfo = NULL;
9805 
9806 		rval = 1;	/* Valid sata address */
9807 
9808 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
9809 		if (qual == SATA_ADDR_DCPORT) {
9810 			if (cportinfo == NULL ||
9811 			    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
9812 				goto out;
9813 
9814 			if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT ||
9815 			    (cportinfo->cport_dev_type &
9816 			    SATA_VALID_DEV_TYPE) == 0) {
9817 				rval = -1;
9818 				goto out;
9819 			}
9820 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
9821 
9822 		} else if (qual == SATA_ADDR_DPMPORT) {
9823 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
9824 			if (pmultinfo == NULL) {
9825 				rval = -1;
9826 				goto out;
9827 			}
9828 			if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) ==
9829 			    NULL ||
9830 			    SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
9831 			    pmport) == SATA_DTYPE_NONE)
9832 				goto out;
9833 
9834 			sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport,
9835 			    pmport);
9836 		} else {
9837 			rval = -1;
9838 			goto out;
9839 		}
9840 		if ((sdinfo == NULL) ||
9841 		    (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0)
9842 			goto out;
9843 
9844 		sata_device->satadev_type = sdinfo->satadrv_type;
9845 		sata_device->satadev_addr.qual = qual;
9846 		sata_device->satadev_addr.cport = cport;
9847 		sata_device->satadev_addr.pmport = pmport;
9848 		sata_device->satadev_rev = SATA_DEVICE_REV_1;
9849 		return (0);
9850 	}
9851 out:
9852 	if (rval == 1) {
9853 		SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
9854 		    "sata_validate_scsi_address: no valid target %x lun %x",
9855 		    ap->a_target, ap->a_lun);
9856 	}
9857 	return (rval);
9858 }
9859 
9860 /*
9861  * Find dip corresponding to passed device number
9862  *
9863  * Returns NULL if invalid device number is passed or device cannot be found,
9864  * Returns dip is device is found.
9865  */
9866 static dev_info_t *
9867 sata_devt_to_devinfo(dev_t dev)
9868 {
9869 	dev_info_t *dip;
9870 #ifndef __lock_lint
9871 	struct devnames *dnp;
9872 	major_t major = getmajor(dev);
9873 	int instance = SATA_MINOR2INSTANCE(getminor(dev));
9874 
9875 	if (major >= devcnt)
9876 		return (NULL);
9877 
9878 	dnp = &devnamesp[major];
9879 	LOCK_DEV_OPS(&(dnp->dn_lock));
9880 	dip = dnp->dn_head;
9881 	while (dip && (ddi_get_instance(dip) != instance)) {
9882 		dip = ddi_get_next(dip);
9883 	}
9884 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
9885 #endif
9886 
9887 	return (dip);
9888 }
9889 
9890 
9891 /*
9892  * Probe device.
9893  * This function issues Identify Device command and initializes local
9894  * sata_drive_info structure if the device can be identified.
9895  * The device type is determined by examining Identify Device
9896  * command response.
9897  * If the sata_hba_inst has linked drive info structure for this
9898  * device address, the Identify Device data is stored into sata_drive_info
9899  * structure linked to the port info structure.
9900  *
9901  * sata_device has to refer to the valid sata port(s) for HBA described
9902  * by sata_hba_inst structure.
9903  *
9904  * Returns:
9905  *	SATA_SUCCESS if device type was successfully probed and port-linked
9906  *		drive info structure was updated;
9907  * 	SATA_FAILURE if there is no device, or device was not probed
9908  *		successully;
9909  *	SATA_RETRY if device probe can be retried later.
9910  * If a device cannot be identified, sata_device's dev_state and dev_type
9911  * fields are set to unknown.
9912  * There are no retries in this function. Any retries should be managed by
9913  * the caller.
9914  */
9915 
9916 
9917 static int
9918 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
9919 {
9920 	sata_drive_info_t *sdinfo;
9921 	sata_drive_info_t new_sdinfo;	/* local drive info struct */
9922 	int rval;
9923 
9924 	ASSERT((SATA_CPORT_STATE(sata_hba_inst,
9925 	    sata_device->satadev_addr.cport) &
9926 	    (SATA_STATE_PROBED | SATA_STATE_READY)) != 0);
9927 
9928 	sata_device->satadev_type = SATA_DTYPE_NONE;
9929 
9930 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9931 	    sata_device->satadev_addr.cport)));
9932 
9933 	/* Get pointer to port-linked sata device info structure */
9934 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9935 	if (sdinfo != NULL) {
9936 		sdinfo->satadrv_state &=
9937 		    ~(SATA_STATE_PROBED | SATA_STATE_READY);
9938 		sdinfo->satadrv_state |= SATA_STATE_PROBING;
9939 	} else {
9940 		/* No device to probe */
9941 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9942 		    sata_device->satadev_addr.cport)));
9943 		sata_device->satadev_type = SATA_DTYPE_NONE;
9944 		sata_device->satadev_state = SATA_STATE_UNKNOWN;
9945 		return (SATA_FAILURE);
9946 	}
9947 	/*
9948 	 * Need to issue both types of identify device command and
9949 	 * determine device type by examining retreived data/status.
9950 	 * First, ATA Identify Device.
9951 	 */
9952 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
9953 	new_sdinfo.satadrv_addr = sata_device->satadev_addr;
9954 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9955 	    sata_device->satadev_addr.cport)));
9956 	new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
9957 	rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
9958 	if (rval == SATA_RETRY) {
9959 		/* We may try to check for ATAPI device */
9960 		if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) {
9961 			/*
9962 			 * HBA supports ATAPI - try to issue Identify Packet
9963 			 * Device command.
9964 			 */
9965 			new_sdinfo.satadrv_type = SATA_DTYPE_ATAPICD;
9966 			rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
9967 		}
9968 	}
9969 	if (rval == SATA_SUCCESS) {
9970 		/*
9971 		 * Got something responding positively to ATA Identify Device
9972 		 * or to Identify Packet Device cmd.
9973 		 * Save last used device type.
9974 		 */
9975 		sata_device->satadev_type = new_sdinfo.satadrv_type;
9976 
9977 		/* save device info, if possible */
9978 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9979 		    sata_device->satadev_addr.cport)));
9980 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9981 		if (sdinfo == NULL) {
9982 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9983 			    sata_device->satadev_addr.cport)));
9984 			return (SATA_FAILURE);
9985 		}
9986 		/*
9987 		 * Copy drive info into the port-linked drive info structure.
9988 		 */
9989 		*sdinfo = new_sdinfo;
9990 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
9991 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
9992 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
9993 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
9994 			    sata_device->satadev_addr.cport) =
9995 			    sdinfo->satadrv_type;
9996 		else /* SATA_ADDR_DPMPORT */
9997 			SATA_PMPORT_DEV_TYPE(sata_hba_inst,
9998 			    sata_device->satadev_addr.cport,
9999 			    sata_device->satadev_addr.pmport) =
10000 			    sdinfo->satadrv_type;
10001 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10002 		    sata_device->satadev_addr.cport)));
10003 		return (SATA_SUCCESS);
10004 	}
10005 
10006 	/*
10007 	 * It may be SATA_RETRY or SATA_FAILURE return.
10008 	 * Looks like we cannot determine the device type at this time.
10009 	 */
10010 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
10011 	    sata_device->satadev_addr.cport)));
10012 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
10013 	if (sdinfo != NULL) {
10014 		sata_device->satadev_type = SATA_DTYPE_UNKNOWN;
10015 		sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
10016 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
10017 		sdinfo->satadrv_state = SATA_STATE_PROBED;
10018 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
10019 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
10020 			    sata_device->satadev_addr.cport) =
10021 			    SATA_DTYPE_UNKNOWN;
10022 		else {
10023 			/* SATA_ADDR_DPMPORT */
10024 			if ((SATA_PMULT_INFO(sata_hba_inst,
10025 			    sata_device->satadev_addr.cport) != NULL) &&
10026 			    (SATA_PMPORT_INFO(sata_hba_inst,
10027 			    sata_device->satadev_addr.cport,
10028 			    sata_device->satadev_addr.pmport) != NULL))
10029 				SATA_PMPORT_DEV_TYPE(sata_hba_inst,
10030 				    sata_device->satadev_addr.cport,
10031 				    sata_device->satadev_addr.pmport) =
10032 				    SATA_DTYPE_UNKNOWN;
10033 		}
10034 	}
10035 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10036 	    sata_device->satadev_addr.cport)));
10037 	return (rval);
10038 }
10039 
10040 
10041 /*
10042  * Get pointer to sata_drive_info structure.
10043  *
10044  * The sata_device has to contain address (cport, pmport and qualifier) for
10045  * specified sata_scsi structure.
10046  *
10047  * Returns NULL if device address is not valid for this HBA configuration.
10048  * Otherwise, returns a pointer to sata_drive_info structure.
10049  *
10050  * This function should be called with a port mutex held.
10051  */
10052 static sata_drive_info_t *
10053 sata_get_device_info(sata_hba_inst_t *sata_hba_inst,
10054     sata_device_t *sata_device)
10055 {
10056 	uint8_t cport = sata_device->satadev_addr.cport;
10057 	uint8_t pmport = sata_device->satadev_addr.pmport;
10058 	uint8_t qual = sata_device->satadev_addr.qual;
10059 
10060 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
10061 		return (NULL);
10062 
10063 	if (!(SATA_CPORT_STATE(sata_hba_inst, cport) &
10064 	    (SATA_STATE_PROBED | SATA_STATE_READY)))
10065 		/* Port not probed yet */
10066 		return (NULL);
10067 
10068 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE)
10069 		return (NULL);
10070 
10071 	if (qual == SATA_ADDR_DCPORT) {
10072 		/* Request for a device on a controller port */
10073 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
10074 		    SATA_DTYPE_PMULT)
10075 			/* Port multiplier attached */
10076 			return (NULL);
10077 		return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport));
10078 	}
10079 	if (qual == SATA_ADDR_DPMPORT) {
10080 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
10081 		    SATA_DTYPE_PMULT)
10082 			return (NULL);
10083 
10084 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport))
10085 			return (NULL);
10086 
10087 		return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport));
10088 	}
10089 
10090 	/* we should not get here */
10091 	return (NULL);
10092 }
10093 
10094 
10095 /*
10096  * sata_identify_device.
10097  * Send Identify Device command to SATA HBA driver.
10098  * If command executes successfully, update sata_drive_info structure pointed
10099  * to by sdinfo argument, including Identify Device data.
10100  * If command fails, invalidate data in sata_drive_info.
10101  *
10102  * Cannot be called from interrupt level.
10103  *
10104  * Returns:
10105  * SATA_SUCCESS if the device was identified as a supported device,
10106  * SATA_RETRY if the device was not identified but could be retried,
10107  * SATA_FAILURE if the device was not identified and identify attempt
10108  *	should not be retried.
10109  */
10110 static int
10111 sata_identify_device(sata_hba_inst_t *sata_hba_inst,
10112     sata_drive_info_t *sdinfo)
10113 {
10114 	uint16_t cfg_word;
10115 	int rval;
10116 
10117 	/* fetch device identify data */
10118 	if ((rval = sata_fetch_device_identify_data(sata_hba_inst,
10119 	    sdinfo)) != 0)
10120 		goto fail_unknown;
10121 
10122 	cfg_word = sdinfo->satadrv_id.ai_config;
10123 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK &&
10124 	    (cfg_word & SATA_ATA_TYPE_MASK) != SATA_ATA_TYPE) {
10125 		/* Change device type to reflect Identify Device data */
10126 		if (((cfg_word & SATA_ATAPI_TYPE_MASK) ==
10127 		    SATA_ATAPI_TYPE) &&
10128 		    ((cfg_word & SATA_ATAPI_ID_DEV_TYPE) ==
10129 		    SATA_ATAPI_CDROM_DEV)) {
10130 			sdinfo->satadrv_type = SATA_DTYPE_ATAPICD;
10131 		} else {
10132 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
10133 		}
10134 	} else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD &&
10135 	    (((cfg_word & SATA_ATAPI_TYPE_MASK) != SATA_ATAPI_TYPE) ||
10136 	    ((cfg_word & SATA_ATAPI_ID_DEV_TYPE) != SATA_ATAPI_CDROM_DEV))) {
10137 		/* Change device type to reflect Identify Device data ! */
10138 		if ((sdinfo->satadrv_id.ai_config & SATA_ATA_TYPE_MASK) ==
10139 		    SATA_ATA_TYPE) {
10140 			sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
10141 		} else {
10142 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
10143 		}
10144 	}
10145 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10146 		if (sdinfo->satadrv_capacity == 0) {
10147 			/* Non-LBA disk. Too bad... */
10148 			sata_log(sata_hba_inst, CE_WARN,
10149 			    "SATA disk device at port %d does not support LBA",
10150 			    sdinfo->satadrv_addr.cport);
10151 			rval = SATA_FAILURE;
10152 			goto fail_unknown;
10153 		}
10154 	}
10155 #if 0
10156 	/* Left for historical reason */
10157 	/*
10158 	 * Some initial version of SATA spec indicated that at least
10159 	 * UDMA mode 4 has to be supported. It is not metioned in
10160 	 * SerialATA 2.6, so this restriction is removed.
10161 	 */
10162 	/* Check for Ultra DMA modes 6 through 0 being supported */
10163 	for (i = 6; i >= 0; --i) {
10164 		if (sdinfo->satadrv_id.ai_ultradma & (1 << i))
10165 			break;
10166 	}
10167 
10168 	/*
10169 	 * At least UDMA 4 mode has to be supported. If mode 4 or
10170 	 * higher are not supported by the device, fail this
10171 	 * device.
10172 	 */
10173 	if (i < 4) {
10174 		/* No required Ultra DMA mode supported */
10175 		sata_log(sata_hba_inst, CE_WARN,
10176 		    "SATA disk device at port %d does not support UDMA "
10177 		    "mode 4 or higher", sdinfo->satadrv_addr.cport);
10178 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10179 		    "mode 4 or higher required, %d supported", i));
10180 		rval = SATA_FAILURE;
10181 		goto fail_unknown;
10182 	}
10183 #endif
10184 
10185 	return (SATA_SUCCESS);
10186 
10187 fail_unknown:
10188 	/* Invalidate sata_drive_info ? */
10189 	sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
10190 	sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
10191 	return (rval);
10192 }
10193 
10194 /*
10195  * Log/display device information
10196  */
10197 static void
10198 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst,
10199     sata_drive_info_t *sdinfo)
10200 {
10201 	int valid_version;
10202 	char msg_buf[MAXPATHLEN];
10203 	int i;
10204 
10205 	/* Show HBA path */
10206 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf);
10207 
10208 	cmn_err(CE_CONT, "?%s :\n", msg_buf);
10209 
10210 	if (sdinfo->satadrv_type == SATA_DTYPE_UNKNOWN) {
10211 		(void) sprintf(msg_buf,
10212 		    "Unsupported SATA device type (cfg 0x%x) at ",
10213 		    sdinfo->satadrv_id.ai_config);
10214 	} else {
10215 		(void) sprintf(msg_buf, "SATA %s device at",
10216 		    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
10217 		    "disk":"CD/DVD (ATAPI)");
10218 	}
10219 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT)
10220 		cmn_err(CE_CONT, "?\t%s port %d\n",
10221 		    msg_buf, sdinfo->satadrv_addr.cport);
10222 	else
10223 		cmn_err(CE_CONT, "?\t%s port %d pmport %d\n",
10224 		    msg_buf, sdinfo->satadrv_addr.cport,
10225 		    sdinfo->satadrv_addr.pmport);
10226 
10227 	bcopy(&sdinfo->satadrv_id.ai_model, msg_buf,
10228 	    sizeof (sdinfo->satadrv_id.ai_model));
10229 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model));
10230 	msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
10231 	cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf);
10232 
10233 	bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf,
10234 	    sizeof (sdinfo->satadrv_id.ai_fw));
10235 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw));
10236 	msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
10237 	cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf);
10238 
10239 	bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf,
10240 	    sizeof (sdinfo->satadrv_id.ai_drvser));
10241 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser));
10242 	msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
10243 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10244 		cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
10245 	} else {
10246 		/* Assuming ATAPI CD/DVD */
10247 		/*
10248 		 * SOme drives do not implement serial number and may
10249 		 * violate the spec by provinding spaces rather than zeros
10250 		 * in serial number field. Scan the buffer to detect it.
10251 		 */
10252 		for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) {
10253 			if (msg_buf[i] != '\0' && msg_buf[i] != ' ')
10254 				break;
10255 		}
10256 		if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) {
10257 			cmn_err(CE_CONT, "?\tserial number - none\n");
10258 		} else {
10259 			cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
10260 		}
10261 	}
10262 
10263 #ifdef SATA_DEBUG
10264 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
10265 	    sdinfo->satadrv_id.ai_majorversion != 0xffff) {
10266 		int i;
10267 		for (i = 14; i >= 2; i--) {
10268 			if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) {
10269 				valid_version = i;
10270 				break;
10271 			}
10272 		}
10273 		cmn_err(CE_CONT,
10274 		    "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
10275 		    valid_version,
10276 		    sdinfo->satadrv_id.ai_majorversion,
10277 		    sdinfo->satadrv_id.ai_minorversion);
10278 	}
10279 #endif
10280 	/* Log some info */
10281 	cmn_err(CE_CONT, "?\tsupported features:\n");
10282 	msg_buf[0] = '\0';
10283 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10284 		if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48)
10285 			(void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN);
10286 		else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28)
10287 			(void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN);
10288 	}
10289 	if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA)
10290 		(void) strlcat(msg_buf, "DMA", MAXPATHLEN);
10291 	if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ)
10292 		(void) strlcat(msg_buf, ", Native Command Queueing",
10293 		    MAXPATHLEN);
10294 	if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)
10295 		(void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN);
10296 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) &&
10297 	    (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED))
10298 		(void) strlcat(msg_buf, ", SMART", MAXPATHLEN);
10299 	if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) &&
10300 	    (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED))
10301 		(void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN);
10302 	cmn_err(CE_CONT, "?\t %s\n", msg_buf);
10303 	if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2)
10304 		cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n");
10305 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1)
10306 		cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n");
10307 	if (sdinfo->satadrv_features_support &
10308 	    (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) {
10309 		cmn_err(CE_CONT, "?\tQueue depth %d\n",
10310 		    sdinfo->satadrv_queue_depth);
10311 	}
10312 
10313 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10314 #ifdef __i386
10315 		(void) sprintf(msg_buf, "\tcapacity = %llu sectors\n",
10316 		    sdinfo->satadrv_capacity);
10317 #else
10318 		(void) sprintf(msg_buf, "\tcapacity = %lu sectors\n",
10319 		    sdinfo->satadrv_capacity);
10320 #endif
10321 		cmn_err(CE_CONT, "?%s", msg_buf);
10322 	}
10323 }
10324 
10325 
10326 /*
10327  * sata_save_drive_settings extracts current setting of the device and stores
10328  * it for future reference, in case the device setup would need to be restored
10329  * after the device reset.
10330  *
10331  * For all devices read ahead and write cache settings are saved, if the
10332  * device supports these features at all.
10333  * For ATAPI devices the Removable Media Status Notification setting is saved.
10334  */
10335 static void
10336 sata_save_drive_settings(sata_drive_info_t *sdinfo)
10337 {
10338 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) ||
10339 	    (sdinfo->satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) {
10340 
10341 		/* Current setting of Read Ahead (and Read Cache) */
10342 		if (sdinfo->satadrv_id.ai_features85 & SATA_LOOK_AHEAD)
10343 			sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
10344 		else
10345 			sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD;
10346 
10347 		/* Current setting of Write Cache */
10348 		if (sdinfo->satadrv_id.ai_features85 & SATA_WRITE_CACHE)
10349 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
10350 		else
10351 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
10352 	}
10353 
10354 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
10355 		if (sdinfo->satadrv_id.ai_cmdset83 & SATA_RM_STATUS_NOTIFIC)
10356 			sdinfo->satadrv_settings |= SATA_DEV_RMSN;
10357 		else
10358 			sdinfo->satadrv_settings &= ~SATA_DEV_RMSN;
10359 	}
10360 }
10361 
10362 
10363 /*
10364  * sata_check_capacity function determines a disk capacity
10365  * and addressing mode (LBA28/LBA48) by examining a disk identify device data.
10366  *
10367  * NOTE: CHS mode is not supported! If a device does not support LBA,
10368  * this function is not called.
10369  *
10370  * Returns device capacity in number of blocks, i.e. largest addressable LBA+1
10371  */
10372 static uint64_t
10373 sata_check_capacity(sata_drive_info_t *sdinfo)
10374 {
10375 	uint64_t capacity = 0;
10376 	int i;
10377 
10378 	if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK ||
10379 	    !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT)
10380 		/* Capacity valid only for LBA-addressable disk devices */
10381 		return (0);
10382 
10383 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) &&
10384 	    (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) &&
10385 	    (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) {
10386 		/* LBA48 mode supported and enabled */
10387 		sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 |
10388 		    SATA_DEV_F_LBA28;
10389 		for (i = 3;  i >= 0;  --i) {
10390 			capacity <<= 16;
10391 			capacity += sdinfo->satadrv_id.ai_addrsecxt[i];
10392 		}
10393 	} else {
10394 		capacity = sdinfo->satadrv_id.ai_addrsec[1];
10395 		capacity <<= 16;
10396 		capacity += sdinfo->satadrv_id.ai_addrsec[0];
10397 		if (capacity >= 0x1000000)
10398 			/* LBA28 mode */
10399 			sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28;
10400 	}
10401 	return (capacity);
10402 }
10403 
10404 
10405 /*
10406  * Allocate consistent buffer for DMA transfer
10407  *
10408  * Cannot be called from interrupt level or with mutex held - it may sleep.
10409  *
10410  * Returns pointer to allocated buffer structure, or NULL if allocation failed.
10411  */
10412 static struct buf *
10413 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len)
10414 {
10415 	struct scsi_address ap;
10416 	struct buf *bp;
10417 	ddi_dma_attr_t	cur_dma_attr;
10418 
10419 	ASSERT(spx->txlt_sata_pkt != NULL);
10420 	ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran;
10421 	ap.a_target = SATA_TO_SCSI_TARGET(
10422 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport,
10423 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport,
10424 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual);
10425 	ap.a_lun = 0;
10426 
10427 	bp = scsi_alloc_consistent_buf(&ap, NULL, len,
10428 	    B_READ, SLEEP_FUNC, NULL);
10429 
10430 	if (bp != NULL) {
10431 		/* Allocate DMA resources for this buffer */
10432 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
10433 		/*
10434 		 * We use a local version of the dma_attr, to account
10435 		 * for a device addressing limitations.
10436 		 * sata_adjust_dma_attr() will handle sdinfo == NULL which
10437 		 * will cause dma attributes to be adjusted to a lowest
10438 		 * acceptable level.
10439 		 */
10440 		sata_adjust_dma_attr(NULL,
10441 		    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
10442 
10443 		if (sata_dma_buf_setup(spx, PKT_CONSISTENT,
10444 		    SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) {
10445 			scsi_free_consistent_buf(bp);
10446 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
10447 			bp = NULL;
10448 		}
10449 	}
10450 	return (bp);
10451 }
10452 
10453 /*
10454  * Release local buffer (consistent buffer for DMA transfer) allocated
10455  * via sata_alloc_local_buffer().
10456  */
10457 static void
10458 sata_free_local_buffer(sata_pkt_txlate_t *spx)
10459 {
10460 	ASSERT(spx->txlt_sata_pkt != NULL);
10461 	ASSERT(spx->txlt_dma_cookie_list != NULL);
10462 	ASSERT(spx->txlt_dma_cookie_list_len != 0);
10463 	ASSERT(spx->txlt_buf_dma_handle != NULL);
10464 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL);
10465 
10466 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0;
10467 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL;
10468 
10469 	/* Free DMA resources */
10470 	(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
10471 	ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
10472 	spx->txlt_buf_dma_handle = 0;
10473 
10474 	if (spx->txlt_dma_cookie_list != &spx->txlt_dma_cookie) {
10475 		kmem_free(spx->txlt_dma_cookie_list,
10476 		    spx->txlt_dma_cookie_list_len * sizeof (ddi_dma_cookie_t));
10477 		spx->txlt_dma_cookie_list = NULL;
10478 		spx->txlt_dma_cookie_list_len = 0;
10479 	}
10480 	/* Free buffer */
10481 	scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp);
10482 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
10483 }
10484 
10485 
10486 
10487 
10488 /*
10489  * Allocate sata_pkt
10490  * Pkt structure version and embedded strcutures version are initialized.
10491  * sata_pkt and sata_pkt_txlate structures are cross-linked.
10492  *
10493  * Since this may be called in interrupt context by sata_scsi_init_pkt,
10494  * callback argument determines if it can sleep or not.
10495  * Hence, it should not be called from interrupt context.
10496  *
10497  * If successful, non-NULL pointer to a sata pkt is returned.
10498  * Upon failure, NULL pointer is returned.
10499  */
10500 static sata_pkt_t *
10501 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t))
10502 {
10503 	sata_pkt_t *spkt;
10504 	int kmsflag;
10505 
10506 	kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
10507 	spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag);
10508 	if (spkt == NULL) {
10509 		SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
10510 		    "sata_pkt_alloc: failed"));
10511 		return (NULL);
10512 	}
10513 	spkt->satapkt_rev = SATA_PKT_REV;
10514 	spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV;
10515 	spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
10516 	spkt->satapkt_framework_private = spx;
10517 	spx->txlt_sata_pkt = spkt;
10518 	return (spkt);
10519 }
10520 
10521 /*
10522  * Free sata pkt allocated via sata_pkt_alloc()
10523  */
10524 static void
10525 sata_pkt_free(sata_pkt_txlate_t *spx)
10526 {
10527 	ASSERT(spx->txlt_sata_pkt != NULL);
10528 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL);
10529 	kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t));
10530 	spx->txlt_sata_pkt = NULL;
10531 }
10532 
10533 
10534 /*
10535  * Adjust DMA attributes.
10536  * SCSI cmds block count is up to 24 bits, SATA cmd block count vary
10537  * from 8 bits to 16 bits, depending on a command being used.
10538  * Limiting max block count arbitrarily to 256 for all read/write
10539  * commands may affects performance, so check both the device and
10540  * controller capability before adjusting dma attributes.
10541  */
10542 void
10543 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr,
10544     ddi_dma_attr_t *adj_dma_attr)
10545 {
10546 	uint32_t count_max;
10547 
10548 	/* Copy original attributes */
10549 	*adj_dma_attr = *dma_attr;
10550 	/*
10551 	 * Things to consider: device addressing capability,
10552 	 * "excessive" controller DMA capabilities.
10553 	 * If a device is being probed/initialized, there are
10554 	 * no device info - use default limits then.
10555 	 */
10556 	if (sdinfo == NULL) {
10557 		count_max = dma_attr->dma_attr_granular * 0x100;
10558 		if (dma_attr->dma_attr_count_max > count_max)
10559 			adj_dma_attr->dma_attr_count_max = count_max;
10560 		if (dma_attr->dma_attr_maxxfer > count_max)
10561 			adj_dma_attr->dma_attr_maxxfer = count_max;
10562 		return;
10563 	}
10564 
10565 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10566 		if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) {
10567 			/*
10568 			 * 16-bit sector count may be used - we rely on
10569 			 * the assumption that only read and write cmds
10570 			 * will request more than 256 sectors worth of data
10571 			 */
10572 			count_max = adj_dma_attr->dma_attr_granular * 0x10000;
10573 		} else {
10574 			/*
10575 			 * 8-bit sector count will be used - default limits
10576 			 * for dma attributes
10577 			 */
10578 			count_max = adj_dma_attr->dma_attr_granular * 0x100;
10579 		}
10580 		/*
10581 		 * Adjust controler dma attributes, if necessary
10582 		 */
10583 		if (dma_attr->dma_attr_count_max > count_max)
10584 			adj_dma_attr->dma_attr_count_max = count_max;
10585 		if (dma_attr->dma_attr_maxxfer > count_max)
10586 			adj_dma_attr->dma_attr_maxxfer = count_max;
10587 	}
10588 }
10589 
10590 
10591 /*
10592  * Allocate DMA resources for the buffer
10593  * This function handles initial DMA resource allocation as well as
10594  * DMA window shift and may be called repeatedly for the same DMA window
10595  * until all DMA cookies in the DMA window are processed.
10596  * To guarantee that there is always a coherent set of cookies to process
10597  * by SATA HBA driver (observing alignment, device granularity, etc.),
10598  * the number of slots for DMA cookies is equal to lesser of  a number of
10599  * cookies in a DMA window and a max number of scatter/gather entries.
10600  *
10601  * Returns DDI_SUCCESS upon successful operation.
10602  * Return failure code of a failing command or DDI_FAILURE when
10603  * internal cleanup failed.
10604  */
10605 static int
10606 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags,
10607     int (*callback)(caddr_t), caddr_t arg,
10608     ddi_dma_attr_t *cur_dma_attr)
10609 {
10610 	int	rval;
10611 	off_t	offset;
10612 	size_t	size;
10613 	int	max_sg_len, req_len, i;
10614 	uint_t	dma_flags;
10615 	struct buf	*bp;
10616 	uint64_t	cur_txfer_len;
10617 
10618 
10619 	ASSERT(spx->txlt_sata_pkt != NULL);
10620 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
10621 	ASSERT(bp != NULL);
10622 
10623 
10624 	if (spx->txlt_buf_dma_handle == NULL) {
10625 		/*
10626 		 * No DMA resources allocated so far - this is a first call
10627 		 * for this sata pkt.
10628 		 */
10629 		rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst),
10630 		    cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle);
10631 
10632 		if (rval != DDI_SUCCESS) {
10633 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
10634 			    "sata_dma_buf_setup: no buf DMA resources %x",
10635 			    rval));
10636 			return (rval);
10637 		}
10638 
10639 		if (bp->b_flags & B_READ)
10640 			dma_flags = DDI_DMA_READ;
10641 		else
10642 			dma_flags = DDI_DMA_WRITE;
10643 
10644 		if (flags & PKT_CONSISTENT)
10645 			dma_flags |= DDI_DMA_CONSISTENT;
10646 
10647 		if (flags & PKT_DMA_PARTIAL)
10648 			dma_flags |= DDI_DMA_PARTIAL;
10649 
10650 		/*
10651 		 * Check buffer alignment and size against dma attributes
10652 		 * Consider dma_attr_align only. There may be requests
10653 		 * with the size lower than device granularity, but they
10654 		 * will not read/write from/to the device, so no adjustment
10655 		 * is necessary. The dma_attr_minxfer theoretically should
10656 		 * be considered, but no HBA driver is checking it.
10657 		 */
10658 		if (IS_P2ALIGNED(bp->b_un.b_addr,
10659 		    cur_dma_attr->dma_attr_align)) {
10660 			rval = ddi_dma_buf_bind_handle(
10661 			    spx->txlt_buf_dma_handle,
10662 			    bp, dma_flags, callback, arg,
10663 			    &spx->txlt_dma_cookie,
10664 			    &spx->txlt_curwin_num_dma_cookies);
10665 		} else { /* Buffer is not aligned */
10666 
10667 			int	(*ddicallback)(caddr_t);
10668 			size_t	bufsz;
10669 
10670 			/* Check id sleeping is allowed */
10671 			ddicallback = (callback == NULL_FUNC) ?
10672 			    DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
10673 
10674 			SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
10675 			    "mis-aligned buffer: addr=0x%p, cnt=%lu",
10676 			    (void *)bp->b_un.b_addr, bp->b_bcount);
10677 
10678 			if (bp->b_flags & (B_PAGEIO|B_PHYS))
10679 				/*
10680 				 * CPU will need to access data in the buffer
10681 				 * (for copying) so map it.
10682 				 */
10683 				bp_mapin(bp);
10684 
10685 			ASSERT(spx->txlt_tmp_buf == NULL);
10686 
10687 			/* Buffer may be padded by ddi_dma_mem_alloc()! */
10688 			rval = ddi_dma_mem_alloc(
10689 			    spx->txlt_buf_dma_handle,
10690 			    bp->b_bcount,
10691 			    &sata_acc_attr,
10692 			    DDI_DMA_STREAMING,
10693 			    ddicallback, NULL,
10694 			    &spx->txlt_tmp_buf,
10695 			    &bufsz,
10696 			    &spx->txlt_tmp_buf_handle);
10697 
10698 			if (rval != DDI_SUCCESS) {
10699 				/* DMA mapping failed */
10700 				(void) ddi_dma_free_handle(
10701 				    &spx->txlt_buf_dma_handle);
10702 				spx->txlt_buf_dma_handle = NULL;
10703 #ifdef SATA_DEBUG
10704 				mbuffail_count++;
10705 #endif
10706 				SATADBG1(SATA_DBG_DMA_SETUP,
10707 				    spx->txlt_sata_hba_inst,
10708 				    "sata_dma_buf_setup: "
10709 				    "buf dma mem alloc failed %x\n", rval);
10710 				return (rval);
10711 			}
10712 			ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf,
10713 			    cur_dma_attr->dma_attr_align));
10714 
10715 #ifdef SATA_DEBUG
10716 			mbuf_count++;
10717 
10718 			if (bp->b_bcount != bufsz)
10719 				/*
10720 				 * This will require special handling, because
10721 				 * DMA cookies will be based on the temporary
10722 				 * buffer size, not the original buffer
10723 				 * b_bcount, so the residue may have to
10724 				 * be counted differently.
10725 				 */
10726 				SATADBG2(SATA_DBG_DMA_SETUP,
10727 				    spx->txlt_sata_hba_inst,
10728 				    "sata_dma_buf_setup: bp size %x != "
10729 				    "bufsz %x\n", bp->b_bcount, bufsz);
10730 #endif
10731 			if (dma_flags & DDI_DMA_WRITE) {
10732 				/*
10733 				 * Write operation - copy data into
10734 				 * an aligned temporary buffer. Buffer will be
10735 				 * synced for device by ddi_dma_addr_bind_handle
10736 				 */
10737 				bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf,
10738 				    bp->b_bcount);
10739 			}
10740 
10741 			rval = ddi_dma_addr_bind_handle(
10742 			    spx->txlt_buf_dma_handle,
10743 			    NULL,
10744 			    spx->txlt_tmp_buf,
10745 			    bufsz, dma_flags, ddicallback, 0,
10746 			    &spx->txlt_dma_cookie,
10747 			    &spx->txlt_curwin_num_dma_cookies);
10748 		}
10749 
10750 		switch (rval) {
10751 		case DDI_DMA_PARTIAL_MAP:
10752 			SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
10753 			    "sata_dma_buf_setup: DMA Partial Map\n", NULL);
10754 			/*
10755 			 * Partial DMA mapping.
10756 			 * Retrieve number of DMA windows for this request.
10757 			 */
10758 			if (ddi_dma_numwin(spx->txlt_buf_dma_handle,
10759 			    &spx->txlt_num_dma_win) != DDI_SUCCESS) {
10760 				if (spx->txlt_tmp_buf != NULL) {
10761 					ddi_dma_mem_free(
10762 					    &spx->txlt_tmp_buf_handle);
10763 					spx->txlt_tmp_buf = NULL;
10764 				}
10765 				(void) ddi_dma_unbind_handle(
10766 				    spx->txlt_buf_dma_handle);
10767 				(void) ddi_dma_free_handle(
10768 				    &spx->txlt_buf_dma_handle);
10769 				spx->txlt_buf_dma_handle = NULL;
10770 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
10771 				    "sata_dma_buf_setup: numwin failed\n"));
10772 				return (DDI_FAILURE);
10773 			}
10774 			SATADBG2(SATA_DBG_DMA_SETUP,
10775 			    spx->txlt_sata_hba_inst,
10776 			    "sata_dma_buf_setup: windows: %d, cookies: %d\n",
10777 			    spx->txlt_num_dma_win,
10778 			    spx->txlt_curwin_num_dma_cookies);
10779 			spx->txlt_cur_dma_win = 0;
10780 			break;
10781 
10782 		case DDI_DMA_MAPPED:
10783 			/* DMA fully mapped */
10784 			spx->txlt_num_dma_win = 1;
10785 			spx->txlt_cur_dma_win = 0;
10786 			SATADBG1(SATA_DBG_DMA_SETUP,
10787 			    spx->txlt_sata_hba_inst,
10788 			    "sata_dma_buf_setup: windows: 1 "
10789 			    "cookies: %d\n", spx->txlt_curwin_num_dma_cookies);
10790 			break;
10791 
10792 		default:
10793 			/* DMA mapping failed */
10794 			if (spx->txlt_tmp_buf != NULL) {
10795 				ddi_dma_mem_free(
10796 				    &spx->txlt_tmp_buf_handle);
10797 				spx->txlt_tmp_buf = NULL;
10798 			}
10799 			(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
10800 			spx->txlt_buf_dma_handle = NULL;
10801 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
10802 			    "sata_dma_buf_setup: buf dma handle binding "
10803 			    "failed %x\n", rval));
10804 			return (rval);
10805 		}
10806 		spx->txlt_curwin_processed_dma_cookies = 0;
10807 		spx->txlt_dma_cookie_list = NULL;
10808 	} else {
10809 		/*
10810 		 * DMA setup is reused. Check if we need to process more
10811 		 * cookies in current window, or to get next window, if any.
10812 		 */
10813 
10814 		ASSERT(spx->txlt_curwin_processed_dma_cookies <=
10815 		    spx->txlt_curwin_num_dma_cookies);
10816 
10817 		if (spx->txlt_curwin_processed_dma_cookies ==
10818 		    spx->txlt_curwin_num_dma_cookies) {
10819 			/*
10820 			 * All cookies from current DMA window were processed.
10821 			 * Get next DMA window.
10822 			 */
10823 			spx->txlt_cur_dma_win++;
10824 			if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) {
10825 				(void) ddi_dma_getwin(spx->txlt_buf_dma_handle,
10826 				    spx->txlt_cur_dma_win, &offset, &size,
10827 				    &spx->txlt_dma_cookie,
10828 				    &spx->txlt_curwin_num_dma_cookies);
10829 				spx->txlt_curwin_processed_dma_cookies = 0;
10830 			} else {
10831 				/* No more windows! End of request! */
10832 				/* What to do? - panic for now */
10833 				ASSERT(spx->txlt_cur_dma_win >=
10834 				    spx->txlt_num_dma_win);
10835 
10836 				spx->txlt_curwin_num_dma_cookies = 0;
10837 				spx->txlt_curwin_processed_dma_cookies = 0;
10838 				spx->txlt_sata_pkt->
10839 				    satapkt_cmd.satacmd_num_dma_cookies = 0;
10840 				return (DDI_SUCCESS);
10841 			}
10842 		}
10843 	}
10844 	/* There better be at least one DMA cookie outstanding */
10845 	ASSERT((spx->txlt_curwin_num_dma_cookies -
10846 	    spx->txlt_curwin_processed_dma_cookies) > 0);
10847 
10848 	if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) {
10849 		/* The default cookie slot was used in previous run */
10850 		ASSERT(spx->txlt_curwin_processed_dma_cookies == 0);
10851 		spx->txlt_dma_cookie_list = NULL;
10852 		spx->txlt_dma_cookie_list_len = 0;
10853 	}
10854 	if (spx->txlt_curwin_processed_dma_cookies == 0) {
10855 		/*
10856 		 * Processing a new DMA window - set-up dma cookies list.
10857 		 * We may reuse previously allocated cookie array if it is
10858 		 * possible.
10859 		 */
10860 		if (spx->txlt_dma_cookie_list != NULL &&
10861 		    spx->txlt_dma_cookie_list_len <
10862 		    spx->txlt_curwin_num_dma_cookies) {
10863 			/*
10864 			 * New DMA window contains more cookies than
10865 			 * the previous one. We need larger cookie list - free
10866 			 * the old one.
10867 			 */
10868 			(void) kmem_free(spx->txlt_dma_cookie_list,
10869 			    spx->txlt_dma_cookie_list_len *
10870 			    sizeof (ddi_dma_cookie_t));
10871 			spx->txlt_dma_cookie_list = NULL;
10872 			spx->txlt_dma_cookie_list_len = 0;
10873 		}
10874 		if (spx->txlt_dma_cookie_list == NULL) {
10875 			/*
10876 			 * Calculate lesser of number of cookies in this
10877 			 * DMA window and number of s/g entries.
10878 			 */
10879 			max_sg_len = cur_dma_attr->dma_attr_sgllen;
10880 			req_len = MIN(max_sg_len,
10881 			    spx->txlt_curwin_num_dma_cookies);
10882 
10883 			/* Allocate new dma cookie array if necessary */
10884 			if (req_len == 1) {
10885 				/* Only one cookie - no need for a list */
10886 				spx->txlt_dma_cookie_list =
10887 				    &spx->txlt_dma_cookie;
10888 				spx->txlt_dma_cookie_list_len = 1;
10889 			} else {
10890 				/*
10891 				 * More than one cookie - try to allocate space.
10892 				 */
10893 				spx->txlt_dma_cookie_list = kmem_zalloc(
10894 				    sizeof (ddi_dma_cookie_t) * req_len,
10895 				    callback == NULL_FUNC ? KM_NOSLEEP :
10896 				    KM_SLEEP);
10897 				if (spx->txlt_dma_cookie_list == NULL) {
10898 					SATADBG1(SATA_DBG_DMA_SETUP,
10899 					    spx->txlt_sata_hba_inst,
10900 					    "sata_dma_buf_setup: cookie list "
10901 					    "allocation failed\n", NULL);
10902 					/*
10903 					 * We could not allocate space for
10904 					 * neccessary number of dma cookies in
10905 					 * this window, so we fail this request.
10906 					 * Next invocation would try again to
10907 					 * allocate space for cookie list.
10908 					 * Note:Packet residue was not modified.
10909 					 */
10910 					return (DDI_DMA_NORESOURCES);
10911 				} else {
10912 					spx->txlt_dma_cookie_list_len = req_len;
10913 				}
10914 			}
10915 		}
10916 		/*
10917 		 * Fetch DMA cookies into cookie list in sata_pkt_txlate.
10918 		 * First cookie was already fetched.
10919 		 */
10920 		*(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie;
10921 		cur_txfer_len =
10922 		    (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size;
10923 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1;
10924 		spx->txlt_curwin_processed_dma_cookies++;
10925 		for (i = 1; (i < spx->txlt_dma_cookie_list_len) &&
10926 		    (i < spx->txlt_curwin_num_dma_cookies); i++) {
10927 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
10928 			    &spx->txlt_dma_cookie_list[i]);
10929 			cur_txfer_len +=
10930 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
10931 			spx->txlt_curwin_processed_dma_cookies++;
10932 			spx->txlt_sata_pkt->
10933 			    satapkt_cmd.satacmd_num_dma_cookies += 1;
10934 		}
10935 	} else {
10936 		SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
10937 		    "sata_dma_buf_setup: sliding within DMA window, "
10938 		    "cur cookie %d, total cookies %d\n",
10939 		    spx->txlt_curwin_processed_dma_cookies,
10940 		    spx->txlt_curwin_num_dma_cookies);
10941 
10942 		/*
10943 		 * Not all cookies from the current dma window were used because
10944 		 * of s/g limitation.
10945 		 * There is no need to re-size the list - it was set at
10946 		 * optimal size, or only default entry is used (s/g = 1).
10947 		 */
10948 		if (spx->txlt_dma_cookie_list == NULL) {
10949 			spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie;
10950 			spx->txlt_dma_cookie_list_len = 1;
10951 		}
10952 		/*
10953 		 * Since we are processing remaining cookies in a DMA window,
10954 		 * there may be less of them than the number of entries in the
10955 		 * current dma cookie list.
10956 		 */
10957 		req_len = MIN(spx->txlt_dma_cookie_list_len,
10958 		    (spx->txlt_curwin_num_dma_cookies -
10959 		    spx->txlt_curwin_processed_dma_cookies));
10960 
10961 		/* Fetch the next batch of cookies */
10962 		for (i = 0, cur_txfer_len = 0; i < req_len; i++) {
10963 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
10964 			    &spx->txlt_dma_cookie_list[i]);
10965 			cur_txfer_len +=
10966 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
10967 			spx->txlt_sata_pkt->
10968 			    satapkt_cmd.satacmd_num_dma_cookies++;
10969 			spx->txlt_curwin_processed_dma_cookies++;
10970 		}
10971 	}
10972 
10973 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0);
10974 
10975 	/* Point sata_cmd to the cookie list */
10976 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list =
10977 	    &spx->txlt_dma_cookie_list[0];
10978 
10979 	/* Remember number of DMA cookies passed in sata packet */
10980 	spx->txlt_num_dma_cookies =
10981 	    spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies;
10982 
10983 	ASSERT(cur_txfer_len != 0);
10984 	if (cur_txfer_len <= bp->b_bcount)
10985 		spx->txlt_total_residue -= cur_txfer_len;
10986 	else {
10987 		/*
10988 		 * Temporary DMA buffer has been padded by
10989 		 * ddi_dma_mem_alloc()!
10990 		 * This requires special handling, because DMA cookies are
10991 		 * based on the temporary buffer size, not the b_bcount,
10992 		 * and we have extra bytes to transfer - but the packet
10993 		 * residue has to stay correct because we will copy only
10994 		 * the requested number of bytes.
10995 		 */
10996 		spx->txlt_total_residue -= bp->b_bcount;
10997 	}
10998 
10999 	return (DDI_SUCCESS);
11000 }
11001 
11002 
11003 /*
11004  * Fetch Device Identify data.
11005  * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type)
11006  * command to a device and get the device identify data.
11007  * The device_info structure has to be set to device type (for selecting proper
11008  * device identify command).
11009  *
11010  * Returns:
11011  * SATA_SUCCESS if cmd succeeded
11012  * SATA_RETRY if cmd was rejected and could be retried,
11013  * SATA_FAILURE if cmd failed and should not be retried (port error)
11014  *
11015  * Cannot be called in an interrupt context.
11016  */
11017 
11018 static int
11019 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst,
11020     sata_drive_info_t *sdinfo)
11021 {
11022 	struct buf *bp;
11023 	sata_pkt_t *spkt;
11024 	sata_cmd_t *scmd;
11025 	sata_pkt_txlate_t *spx;
11026 	int rval;
11027 
11028 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
11029 	spx->txlt_sata_hba_inst = sata_hba_inst;
11030 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
11031 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
11032 	if (spkt == NULL) {
11033 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
11034 		return (SATA_RETRY); /* may retry later */
11035 	}
11036 	/* address is needed now */
11037 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
11038 
11039 	/*
11040 	 * Allocate buffer for Identify Data return data
11041 	 */
11042 	bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t));
11043 	if (bp == NULL) {
11044 		sata_pkt_free(spx);
11045 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
11046 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11047 		    "sata_fetch_device_identify_data: "
11048 		    "cannot allocate buffer for ID"));
11049 		return (SATA_RETRY); /* may retry later */
11050 	}
11051 
11052 	/* Fill sata_pkt */
11053 	sdinfo->satadrv_state = SATA_STATE_PROBING;
11054 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
11055 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
11056 	/* Synchronous mode, no callback */
11057 	spkt->satapkt_comp = NULL;
11058 	/* Timeout 30s */
11059 	spkt->satapkt_time = sata_default_pkt_time;
11060 
11061 	scmd = &spkt->satapkt_cmd;
11062 	scmd->satacmd_bp = bp;
11063 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
11064 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
11065 
11066 	/* Build Identify Device cmd in the sata_pkt */
11067 	scmd->satacmd_addr_type = 0;		/* N/A */
11068 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
11069 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
11070 	scmd->satacmd_lba_mid_lsb = 0;		/* N/A */
11071 	scmd->satacmd_lba_high_lsb = 0;		/* N/A */
11072 	scmd->satacmd_features_reg = 0;		/* N/A */
11073 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
11074 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
11075 		/* Identify Packet Device cmd */
11076 		scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE;
11077 	} else {
11078 		/* Identify Device cmd - mandatory for all other devices */
11079 		scmd->satacmd_cmd_reg = SATAC_ID_DEVICE;
11080 	}
11081 
11082 	/* Send pkt to SATA HBA driver */
11083 	rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt);
11084 	if (rval == SATA_TRAN_ACCEPTED &&
11085 	    spkt->satapkt_reason == SATA_PKT_COMPLETED) {
11086 		if ((sdinfo->satadrv_id.ai_config & SATA_INCOMPLETE_DATA) ==
11087 		    SATA_INCOMPLETE_DATA) {
11088 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11089 			    "SATA disk device at port %d - "
11090 			    "partial Identify Data",
11091 			    sdinfo->satadrv_addr.cport));
11092 			rval = SATA_RETRY; /* may retry later */
11093 			goto fail;
11094 		}
11095 		/* Update sata_drive_info */
11096 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
11097 		    DDI_DMA_SYNC_FORKERNEL);
11098 		ASSERT(rval == DDI_SUCCESS);
11099 		bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id,
11100 		    sizeof (sata_id_t));
11101 
11102 		sdinfo->satadrv_features_support = 0;
11103 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
11104 			/*
11105 			 * Retrieve capacity (disks only) and addressing mode
11106 			 */
11107 			sdinfo->satadrv_capacity = sata_check_capacity(sdinfo);
11108 		} else {
11109 			/*
11110 			 * For ATAPI devices one would have to issue
11111 			 * Get Capacity cmd for media capacity. Not here.
11112 			 */
11113 			sdinfo->satadrv_capacity = 0;
11114 			/*
11115 			 * Check what cdb length is supported
11116 			 */
11117 			if ((sdinfo->satadrv_id.ai_config &
11118 			    SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B)
11119 				sdinfo->satadrv_atapi_cdb_len = 16;
11120 			else
11121 				sdinfo->satadrv_atapi_cdb_len = 12;
11122 		}
11123 		/* Setup supported features flags */
11124 		if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT)
11125 			sdinfo->satadrv_features_support |= SATA_DEV_F_DMA;
11126 
11127 		/* Check for SATA GEN and NCQ support */
11128 		if (sdinfo->satadrv_id.ai_satacap != 0 &&
11129 		    sdinfo->satadrv_id.ai_satacap != 0xffff) {
11130 			/* SATA compliance */
11131 			if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ)
11132 				sdinfo->satadrv_features_support |=
11133 				    SATA_DEV_F_NCQ;
11134 			if (sdinfo->satadrv_id.ai_satacap &
11135 			    (SATA_1_SPEED | SATA_2_SPEED)) {
11136 				if (sdinfo->satadrv_id.ai_satacap &
11137 				    SATA_2_SPEED)
11138 					sdinfo->satadrv_features_support |=
11139 					    SATA_DEV_F_SATA2;
11140 				if (sdinfo->satadrv_id.ai_satacap &
11141 				    SATA_1_SPEED)
11142 					sdinfo->satadrv_features_support |=
11143 					    SATA_DEV_F_SATA1;
11144 			} else {
11145 				sdinfo->satadrv_features_support |=
11146 				    SATA_DEV_F_SATA1;
11147 			}
11148 		}
11149 		if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) &&
11150 		    (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD))
11151 			sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ;
11152 
11153 		sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth;
11154 		if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) ||
11155 		    (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ))
11156 			++sdinfo->satadrv_queue_depth;
11157 		rval = SATA_SUCCESS;
11158 	} else {
11159 		/*
11160 		 * Woops, no Identify Data.
11161 		 */
11162 		if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) {
11163 			rval = SATA_RETRY; /* may retry later */
11164 		} else if (rval == SATA_TRAN_ACCEPTED) {
11165 			if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR ||
11166 			    spkt->satapkt_reason == SATA_PKT_ABORTED ||
11167 			    spkt->satapkt_reason == SATA_PKT_TIMEOUT ||
11168 			    spkt->satapkt_reason == SATA_PKT_RESET)
11169 				rval = SATA_RETRY; /* may retry later */
11170 			else
11171 				rval = SATA_FAILURE;
11172 		} else {
11173 			rval = SATA_FAILURE;
11174 		}
11175 	}
11176 fail:
11177 	/* Free allocated resources */
11178 	sata_free_local_buffer(spx);
11179 	sata_pkt_free(spx);
11180 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
11181 
11182 	return (rval);
11183 }
11184 
11185 
11186 /*
11187  * Some devices may not come-up with default DMA mode (UDMA or MWDMA).
11188  * UDMA mode is checked first, followed by MWDMA mode.
11189  * set correctly, so this function is setting it to the highest supported level.
11190  * Older SATA spec required that the device supports at least DMA 4 mode and
11191  * UDMA mode is selected.  It is not mentioned in SerialATA 2.6, so this
11192  * restriction has been removed.
11193  *
11194  * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported.
11195  * Returns SATA_FAILURE if proper DMA mode could not be selected.
11196  *
11197  * NOTE: This function should be called only if DMA mode is supported.
11198  */
11199 static int
11200 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo)
11201 {
11202 	sata_pkt_t *spkt;
11203 	sata_cmd_t *scmd;
11204 	sata_pkt_txlate_t *spx;
11205 	int i, mode;
11206 	uint8_t subcmd;
11207 	int rval = SATA_SUCCESS;
11208 
11209 	ASSERT(sdinfo != NULL);
11210 	ASSERT(sata_hba_inst != NULL);
11211 
11212 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
11213 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) {
11214 		/* Find highest Ultra DMA mode supported */
11215 		for (mode = 6; mode >= 0; --mode) {
11216 			if (sdinfo->satadrv_id.ai_ultradma & (1 << mode))
11217 				break;
11218 		}
11219 #if 0
11220 		/* Left for historical reasons */
11221 		/*
11222 		 * Some initial version of SATA spec indicated that at least
11223 		 * UDMA mode 4 has to be supported. It is not mentioned in
11224 		 * SerialATA 2.6, so this restriction is removed.
11225 		 */
11226 		if (mode < 4)
11227 			return (SATA_FAILURE);
11228 #endif
11229 		/* Find UDMA mode currently selected */
11230 		for (i = 6; i >= 0; --i) {
11231 			if (sdinfo->satadrv_id.ai_ultradma & (1 << (i + 8)))
11232 				break;
11233 		}
11234 		if (i >= mode)
11235 			/* Nothing to do */
11236 			return (SATA_SUCCESS);
11237 
11238 		subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA;
11239 
11240 	} else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) {
11241 		/* Find highest MultiWord DMA mode supported */
11242 		for (mode = 2; mode >= 0; --mode) {
11243 			if (sdinfo->satadrv_id.ai_dworddma & (1 << mode))
11244 				break;
11245 		}
11246 		/* Find highest MultiWord DMA mode selected */
11247 		for (i = 2; i >= 0; --i) {
11248 			if (sdinfo->satadrv_id.ai_dworddma & (1 << (i + 8)))
11249 				break;
11250 		}
11251 		if (i >= mode)
11252 			/* Nothing to do */
11253 			return (SATA_SUCCESS);
11254 
11255 		subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA;
11256 	} else
11257 		return (SATA_SUCCESS);
11258 
11259 	/*
11260 	 * Set DMA mode via SET FEATURES COMMAND.
11261 	 * Prepare packet for SET FEATURES COMMAND.
11262 	 */
11263 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
11264 	spx->txlt_sata_hba_inst = sata_hba_inst;
11265 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
11266 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
11267 	if (spkt == NULL) {
11268 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11269 		    "sata_set_dma_mode: could not set DMA mode %", mode));
11270 		rval = SATA_FAILURE;
11271 		goto done;
11272 	}
11273 	/* Fill sata_pkt */
11274 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
11275 	/* Timeout 30s */
11276 	spkt->satapkt_time = sata_default_pkt_time;
11277 	/* Synchronous mode, no callback, interrupts */
11278 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
11279 	spkt->satapkt_comp = NULL;
11280 	scmd = &spkt->satapkt_cmd;
11281 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
11282 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
11283 	scmd->satacmd_addr_type = 0;
11284 	scmd->satacmd_device_reg = 0;
11285 	scmd->satacmd_status_reg = 0;
11286 	scmd->satacmd_error_reg = 0;
11287 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
11288 	scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE;
11289 	scmd->satacmd_sec_count_lsb = subcmd | mode;
11290 
11291 	/* Transfer command to HBA */
11292 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
11293 	    spkt) != SATA_TRAN_ACCEPTED ||
11294 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
11295 		/* Pkt execution failed */
11296 		rval = SATA_FAILURE;
11297 	}
11298 done:
11299 
11300 	/* Free allocated resources */
11301 	if (spkt != NULL)
11302 		sata_pkt_free(spx);
11303 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
11304 
11305 	return (rval);
11306 }
11307 
11308 
11309 /*
11310  * Set device caching mode.
11311  * One of the following operations should be specified:
11312  * SATAC_SF_ENABLE_READ_AHEAD
11313  * SATAC_SF_DISABLE_READ_AHEAD
11314  * SATAC_SF_ENABLE_WRITE_CACHE
11315  * SATAC_SF_DISABLE_WRITE_CACHE
11316  *
11317  * If operation fails, system log messgage is emitted.
11318  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
11319  */
11320 
11321 static int
11322 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
11323     int cache_op)
11324 {
11325 	sata_pkt_t *spkt;
11326 	sata_cmd_t *scmd;
11327 	sata_pkt_txlate_t *spx;
11328 	int rval = SATA_SUCCESS;
11329 	char *infop;
11330 
11331 	ASSERT(sdinfo != NULL);
11332 	ASSERT(sata_hba_inst != NULL);
11333 	ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD ||
11334 	    cache_op == SATAC_SF_DISABLE_READ_AHEAD ||
11335 	    cache_op == SATAC_SF_ENABLE_WRITE_CACHE ||
11336 	    cache_op == SATAC_SF_DISABLE_WRITE_CACHE);
11337 
11338 
11339 	/* Prepare packet for SET FEATURES COMMAND */
11340 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
11341 	spx->txlt_sata_hba_inst = sata_hba_inst;
11342 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
11343 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
11344 	if (spkt == NULL) {
11345 		rval = SATA_FAILURE;
11346 		goto failure;
11347 	}
11348 	/* Fill sata_pkt */
11349 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
11350 	/* Timeout 30s */
11351 	spkt->satapkt_time = sata_default_pkt_time;
11352 	/* Synchronous mode, no callback, interrupts */
11353 	spkt->satapkt_op_mode =
11354 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
11355 	spkt->satapkt_comp = NULL;
11356 	scmd = &spkt->satapkt_cmd;
11357 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
11358 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
11359 	scmd->satacmd_addr_type = 0;
11360 	scmd->satacmd_device_reg = 0;
11361 	scmd->satacmd_status_reg = 0;
11362 	scmd->satacmd_error_reg = 0;
11363 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
11364 	scmd->satacmd_features_reg = cache_op;
11365 
11366 	/* Transfer command to HBA */
11367 	if (((*SATA_START_FUNC(sata_hba_inst))(
11368 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
11369 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
11370 		/* Pkt execution failed */
11371 		switch (cache_op) {
11372 		case SATAC_SF_ENABLE_READ_AHEAD:
11373 			infop = "enabling read ahead failed";
11374 			break;
11375 		case SATAC_SF_DISABLE_READ_AHEAD:
11376 			infop = "disabling read ahead failed";
11377 			break;
11378 		case SATAC_SF_ENABLE_WRITE_CACHE:
11379 			infop = "enabling write cache failed";
11380 			break;
11381 		case SATAC_SF_DISABLE_WRITE_CACHE:
11382 			infop = "disabling write cache failed";
11383 			break;
11384 		}
11385 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
11386 		rval = SATA_FAILURE;
11387 	}
11388 failure:
11389 	/* Free allocated resources */
11390 	if (spkt != NULL)
11391 		sata_pkt_free(spx);
11392 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
11393 	return (rval);
11394 }
11395 
11396 /*
11397  * Set Removable Media Status Notification (enable/disable)
11398  * state == 0 , disable
11399  * state != 0 , enable
11400  *
11401  * If operation fails, system log messgage is emitted.
11402  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
11403  */
11404 
11405 static int
11406 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
11407     int state)
11408 {
11409 	sata_pkt_t *spkt;
11410 	sata_cmd_t *scmd;
11411 	sata_pkt_txlate_t *spx;
11412 	int rval = SATA_SUCCESS;
11413 	char *infop;
11414 
11415 	ASSERT(sdinfo != NULL);
11416 	ASSERT(sata_hba_inst != NULL);
11417 
11418 	/* Prepare packet for SET FEATURES COMMAND */
11419 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
11420 	spx->txlt_sata_hba_inst = sata_hba_inst;
11421 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
11422 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
11423 	if (spkt == NULL) {
11424 		rval = SATA_FAILURE;
11425 		goto failure;
11426 	}
11427 	/* Fill sata_pkt */
11428 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
11429 	/* Timeout 30s */
11430 	spkt->satapkt_time = sata_default_pkt_time;
11431 	/* Synchronous mode, no callback, interrupts */
11432 	spkt->satapkt_op_mode =
11433 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
11434 	spkt->satapkt_comp = NULL;
11435 	scmd = &spkt->satapkt_cmd;
11436 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
11437 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
11438 	scmd->satacmd_addr_type = 0;
11439 	scmd->satacmd_device_reg = 0;
11440 	scmd->satacmd_status_reg = 0;
11441 	scmd->satacmd_error_reg = 0;
11442 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
11443 	if (state == 0)
11444 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN;
11445 	else
11446 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN;
11447 
11448 	/* Transfer command to HBA */
11449 	if (((*SATA_START_FUNC(sata_hba_inst))(
11450 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
11451 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
11452 		/* Pkt execution failed */
11453 		if (state == 0)
11454 			infop = "disabling Removable Media Status "
11455 			    "Notification failed";
11456 		else
11457 			infop = "enabling Removable Media Status "
11458 			    "Notification failed";
11459 
11460 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
11461 		rval = SATA_FAILURE;
11462 	}
11463 failure:
11464 	/* Free allocated resources */
11465 	if (spkt != NULL)
11466 		sata_pkt_free(spx);
11467 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
11468 	return (rval);
11469 }
11470 
11471 
11472 /*
11473  * Update port SCR block
11474  */
11475 static void
11476 sata_update_port_scr(sata_port_scr_t *port_scr, sata_device_t *device)
11477 {
11478 	port_scr->sstatus = device->satadev_scr.sstatus;
11479 	port_scr->serror = device->satadev_scr.serror;
11480 	port_scr->scontrol = device->satadev_scr.scontrol;
11481 	port_scr->sactive = device->satadev_scr.sactive;
11482 	port_scr->snotific = device->satadev_scr.snotific;
11483 }
11484 
11485 /*
11486  * Update state and copy port ss* values from passed sata_device structure.
11487  * sata_address is validated - if not valid, nothing is changed in sata_scsi
11488  * configuration struct.
11489  *
11490  * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function
11491  * regardless of the state in device argument.
11492  *
11493  * Port mutex should be held while calling this function.
11494  */
11495 static void
11496 sata_update_port_info(sata_hba_inst_t *sata_hba_inst,
11497 	sata_device_t *sata_device)
11498 {
11499 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst,
11500 	    sata_device->satadev_addr.cport)));
11501 
11502 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT ||
11503 	    sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
11504 
11505 		sata_cport_info_t *cportinfo;
11506 
11507 		if (SATA_NUM_CPORTS(sata_hba_inst) <=
11508 		    sata_device->satadev_addr.cport)
11509 			return;
11510 
11511 		cportinfo = SATA_CPORT_INFO(sata_hba_inst,
11512 		    sata_device->satadev_addr.cport);
11513 		sata_update_port_scr(&cportinfo->cport_scr, sata_device);
11514 
11515 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
11516 		cportinfo->cport_state &= ~(SATA_PSTATE_PWRON |
11517 		    SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
11518 		cportinfo->cport_state |=
11519 		    sata_device->satadev_state & SATA_PSTATE_VALID;
11520 	} else {
11521 		sata_pmport_info_t *pmportinfo;
11522 
11523 		if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT) ||
11524 		    (sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) ||
11525 		    SATA_NUM_PMPORTS(sata_hba_inst,
11526 		    sata_device->satadev_addr.cport) <
11527 		    sata_device->satadev_addr.pmport)
11528 			return;
11529 
11530 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
11531 		    sata_device->satadev_addr.cport,
11532 		    sata_device->satadev_addr.pmport);
11533 		sata_update_port_scr(&pmportinfo->pmport_scr, sata_device);
11534 
11535 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
11536 		pmportinfo->pmport_state &=
11537 		    ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF |
11538 		    SATA_PSTATE_FAILED);
11539 		pmportinfo->pmport_state |=
11540 		    sata_device->satadev_state & SATA_PSTATE_VALID;
11541 	}
11542 }
11543 
11544 
11545 
11546 /*
11547  * Extract SATA port specification from an IOCTL argument.
11548  *
11549  * This function return the port the user land send us as is, unless it
11550  * cannot retrieve port spec, then -1 is returned.
11551  *
11552  * Note: Only cport  - no port multiplier port.
11553  */
11554 static int32_t
11555 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp)
11556 {
11557 	int32_t port;
11558 
11559 	/* Extract port number from nvpair in dca structure  */
11560 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
11561 		SATA_LOG_D((sata_hba_inst, CE_NOTE,
11562 		    "sata_get_port_num: invalid port spec 0x%x in ioctl",
11563 		    port));
11564 		port = -1;
11565 	}
11566 
11567 	return (port);
11568 }
11569 
11570 /*
11571  * Get dev_info_t pointer to the device node pointed to by port argument.
11572  * NOTE: target argument is a value used in ioctls to identify
11573  * the AP - it is not a sata_address.
11574  * It is a combination of cport, pmport and address qualifier, encodded same
11575  * way as a scsi target number.
11576  * At this moment it carries only cport number.
11577  *
11578  * No PMult hotplug support.
11579  *
11580  * Returns dev_info_t pointer if target device was found, NULL otherwise.
11581  */
11582 
11583 static dev_info_t *
11584 sata_get_target_dip(dev_info_t *dip, int32_t port)
11585 {
11586 	dev_info_t	*cdip = NULL;
11587 	int		target, tgt;
11588 	int		ncport;
11589 	int 		circ;
11590 
11591 	ncport = port & SATA_CFGA_CPORT_MASK;
11592 	target = SATA_TO_SCSI_TARGET(ncport, 0, SATA_ADDR_DCPORT);
11593 
11594 	ndi_devi_enter(dip, &circ);
11595 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
11596 		dev_info_t *next = ddi_get_next_sibling(cdip);
11597 
11598 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
11599 		    DDI_PROP_DONTPASS, "target", -1);
11600 		if (tgt == -1) {
11601 			/*
11602 			 * This is actually an error condition, but not
11603 			 * a fatal one. Just continue the search.
11604 			 */
11605 			cdip = next;
11606 			continue;
11607 		}
11608 
11609 		if (tgt == target)
11610 			break;
11611 
11612 		cdip = next;
11613 	}
11614 	ndi_devi_exit(dip, circ);
11615 
11616 	return (cdip);
11617 }
11618 
11619 
11620 /*
11621  * sata_cfgadm_state:
11622  * Use the sata port state and state of the target node to figure out
11623  * the cfgadm_state.
11624  *
11625  * The port argument is a value with encoded cport,
11626  * pmport and address qualifier, in the same manner as a scsi target number.
11627  * SCSI_TO_SATA_CPORT macro extracts cport number,
11628  * SCSI_TO_SATA_PMPORT extracts pmport number and
11629  * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag.
11630  *
11631  * For now, support is for cports only - no port multiplier device ports.
11632  */
11633 
11634 static void
11635 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port,
11636     devctl_ap_state_t *ap_state)
11637 {
11638 	uint16_t	cport;
11639 	int		port_state;
11640 
11641 	/* Cport only */
11642 	cport = SCSI_TO_SATA_CPORT(port);
11643 
11644 	port_state = SATA_CPORT_STATE(sata_hba_inst, cport);
11645 	if (port_state & SATA_PSTATE_SHUTDOWN ||
11646 	    port_state & SATA_PSTATE_FAILED) {
11647 		ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
11648 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
11649 		if (port_state & SATA_PSTATE_FAILED)
11650 			ap_state->ap_condition = AP_COND_FAILED;
11651 		else
11652 			ap_state->ap_condition = AP_COND_UNKNOWN;
11653 
11654 		return;
11655 	}
11656 
11657 	/* Need to check pmult device port here as well, when supported */
11658 
11659 	/* Port is enabled and ready */
11660 
11661 	switch (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport)) {
11662 	case SATA_DTYPE_NONE:
11663 	{
11664 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
11665 		ap_state->ap_condition = AP_COND_OK;
11666 		/* No device attached */
11667 		ap_state->ap_rstate = AP_RSTATE_EMPTY;
11668 		break;
11669 	}
11670 	case SATA_DTYPE_UNKNOWN:
11671 	case SATA_DTYPE_ATAPINONCD:
11672 	case SATA_DTYPE_PMULT:	/* Until PMult is supported */
11673 	case SATA_DTYPE_ATADISK:
11674 	case SATA_DTYPE_ATAPICD:
11675 	{
11676 		dev_info_t *tdip = NULL;
11677 		dev_info_t *dip = NULL;
11678 		int circ;
11679 
11680 		dip = SATA_DIP(sata_hba_inst);
11681 		tdip = sata_get_target_dip(dip, port);
11682 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
11683 		if (tdip != NULL) {
11684 			ndi_devi_enter(dip, &circ);
11685 			mutex_enter(&(DEVI(tdip)->devi_lock));
11686 			if (DEVI_IS_DEVICE_REMOVED(tdip)) {
11687 				/*
11688 				 * There could be the case where previously
11689 				 * configured and opened device was removed
11690 				 * and unknown device was plugged.
11691 				 * In such case we want to show a device, and
11692 				 * its configured or unconfigured state but
11693 				 * indicate unusable condition untill the
11694 				 * old target node is released and removed.
11695 				 */
11696 				ap_state->ap_condition = AP_COND_UNUSABLE;
11697 			} else {
11698 				ap_state->ap_condition = AP_COND_OK;
11699 			}
11700 			if ((DEVI_IS_DEVICE_OFFLINE(tdip)) ||
11701 			    (DEVI_IS_DEVICE_DOWN(tdip))) {
11702 				ap_state->ap_ostate =
11703 				    AP_OSTATE_UNCONFIGURED;
11704 			} else {
11705 				ap_state->ap_ostate =
11706 				    AP_OSTATE_CONFIGURED;
11707 			}
11708 			mutex_exit(&(DEVI(tdip)->devi_lock));
11709 			ndi_devi_exit(dip, circ);
11710 		} else {
11711 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
11712 			ap_state->ap_condition = AP_COND_UNKNOWN;
11713 		}
11714 		break;
11715 	}
11716 	default:
11717 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
11718 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
11719 		ap_state->ap_condition = AP_COND_UNKNOWN;
11720 		/*
11721 		 * This is actually internal error condition (non fatal),
11722 		 * because we have already checked all defined device types.
11723 		 */
11724 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11725 		    "sata_cfgadm_state: Internal error: "
11726 		    "unknown device type"));
11727 		break;
11728 	}
11729 }
11730 
11731 
11732 /*
11733  * Preset scsi extended sense data (to NO SENSE)
11734  * First 18 bytes of the sense data are preset to current valid sense
11735  * with a key NO SENSE data.
11736  *
11737  * Returns void
11738  */
11739 static void
11740 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense)
11741 {
11742 	sense->es_valid = 1;		/* Valid sense */
11743 	sense->es_class = CLASS_EXTENDED_SENSE;	/* 0x70 - current err */
11744 	sense->es_key = KEY_NO_SENSE;
11745 	sense->es_info_1 = 0;
11746 	sense->es_info_2 = 0;
11747 	sense->es_info_3 = 0;
11748 	sense->es_info_4 = 0;
11749 	sense->es_add_len = 10;	/* Additional length - replace with a def */
11750 	sense->es_cmd_info[0] = 0;
11751 	sense->es_cmd_info[1] = 0;
11752 	sense->es_cmd_info[2] = 0;
11753 	sense->es_cmd_info[3] = 0;
11754 	sense->es_add_code = 0;
11755 	sense->es_qual_code = 0;
11756 }
11757 
11758 /*
11759  * Register a legacy cmdk-style devid for the target (disk) device.
11760  *
11761  * Note: This function is called only when the HBA devinfo node has the
11762  * property "use-cmdk-devid-format" set. This property indicates that
11763  * devid compatible with old cmdk (target) driver is to be generated
11764  * for any target device attached to this controller. This will take
11765  * precedence over the devid generated by sd (target) driver.
11766  * This function is derived from cmdk_devid_setup() function in cmdk.c.
11767  */
11768 static void
11769 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo)
11770 {
11771 	char	*hwid;
11772 	int	modlen;
11773 	int	serlen;
11774 	int	rval;
11775 	ddi_devid_t	devid;
11776 
11777 	/*
11778 	 * device ID is a concatanation of model number, "=", serial number.
11779 	 */
11780 	hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP);
11781 	bcopy(&sdinfo->satadrv_id.ai_model, hwid,
11782 	    sizeof (sdinfo->satadrv_id.ai_model));
11783 	swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model));
11784 	modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model));
11785 	if (modlen == 0)
11786 		goto err;
11787 	hwid[modlen++] = '=';
11788 	bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen],
11789 	    sizeof (sdinfo->satadrv_id.ai_drvser));
11790 	swab(&hwid[modlen], &hwid[modlen],
11791 		sizeof (sdinfo->satadrv_id.ai_drvser));
11792 	serlen = sata_check_modser(&hwid[modlen],
11793 		sizeof (sdinfo->satadrv_id.ai_drvser));
11794 	if (serlen == 0)
11795 		goto err;
11796 	hwid[modlen + serlen] = 0; /* terminate the hwid string */
11797 
11798 	/* initialize/register devid */
11799 	if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL,
11800 		(ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS)
11801 		rval = ddi_devid_register(dip, devid);
11802 
11803 	if (rval != DDI_SUCCESS)
11804 		cmn_err(CE_WARN, "sata: failed to create devid for the disk"
11805 			" on port %d", sdinfo->satadrv_addr.cport);
11806 err:
11807 	kmem_free(hwid, LEGACY_HWID_LEN);
11808 }
11809 
11810 /*
11811  * valid model/serial string must contain a non-zero non-space characters.
11812  * trim trailing spaces/NULLs.
11813  */
11814 static int
11815 sata_check_modser(char *buf, int buf_len)
11816 {
11817 	boolean_t ret;
11818 	char *s;
11819 	int i;
11820 	int tb;
11821 	char ch;
11822 
11823 	ret = B_FALSE;
11824 	s = buf;
11825 	for (i = 0; i < buf_len; i++) {
11826 		ch = *s++;
11827 		if (ch != ' ' && ch != '\0')
11828 			tb = i + 1;
11829 		if (ch != ' ' && ch != '\0' && ch != '0')
11830 			ret = B_TRUE;
11831 	}
11832 
11833 	if (ret == B_FALSE)
11834 		return (0); /* invalid string */
11835 
11836 	return (tb); /* return length */
11837 }
11838 
11839 /*
11840  * sata_set_drive_features function compares current device features setting
11841  * with the saved device features settings and, if there is a difference,
11842  * it restores device features setting to the previously saved state.
11843  * It also arbitrarily tries to select the highest supported DMA mode.
11844  * Device Identify or Identify Packet Device data has to be current.
11845  * At the moment read ahead and write cache are considered for all devices.
11846  * For atapi devices, Removable Media Status Notification is set in addition
11847  * to common features.
11848  *
11849  * This function cannot be called in the interrupt context (it may sleep).
11850  *
11851  * The input argument sdinfo should point to the drive info structure
11852  * to be updated after features are set. Note, that only
11853  * device (packet) identify data is updated, not the flags indicating the
11854  * supported features.
11855  *
11856  * Returns TRUE if successful or there was nothing to do. Device Identify data
11857  * in the drive info structure pointed to by the sdinfo argumens is updated
11858  * even when no features were set or changed.
11859  *
11860  * Returns FALSE if device features could not be set.
11861  *
11862  * Note: This function may fail the port, making it inaccessible.
11863  * In such case the explicit port disconnect/connect or physical device
11864  * detach/attach is required to re-evaluate port state again.
11865  */
11866 
11867 static int
11868 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst,
11869     sata_drive_info_t *sdinfo, int restore)
11870 {
11871 	int rval = SATA_SUCCESS;
11872 	sata_drive_info_t new_sdinfo;
11873 	char *finfo = "sata_set_drive_features: cannot";
11874 	char *finfox;
11875 	int cache_op;
11876 
11877 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
11878 	new_sdinfo.satadrv_addr = sdinfo->satadrv_addr;
11879 	new_sdinfo.satadrv_type = sdinfo->satadrv_type;
11880 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
11881 		/*
11882 		 * Cannot get device identification - retry later
11883 		 */
11884 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11885 		    "%s fetch device identify data\n", finfo));
11886 		return (SATA_FAILURE);
11887 	}
11888 	finfox = (restore != 0) ? " restore device features" :
11889 	    " initialize device features\n";
11890 
11891 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
11892 		/* Arbitrarily set UDMA mode */
11893 		if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
11894 		    SATA_SUCCESS) {
11895 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11896 			    "%s set UDMA mode\n", finfo));
11897 			return (SATA_FAILURE);
11898 		}
11899 	} else { /* Assume SATA ATAPI CD/DVD */
11900 		/*  Set Removable Media Status Notification, if necessary */
11901 		if ((new_sdinfo.satadrv_id.ai_cmdset83 &
11902 		    SATA_RM_STATUS_NOTIFIC) != 0 && restore != 0) {
11903 			if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) &&
11904 			    (!(new_sdinfo.satadrv_id.ai_features86 &
11905 			    SATA_RM_STATUS_NOTIFIC))) ||
11906 			    ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) &&
11907 			    (new_sdinfo.satadrv_id.ai_features86 &
11908 			    SATA_RM_STATUS_NOTIFIC))) {
11909 				/* Current setting does not match saved one */
11910 				if (sata_set_rmsn(sata_hba_inst, sdinfo,
11911 				    sdinfo->satadrv_settings &
11912 				    SATA_DEV_RMSN) != SATA_SUCCESS)
11913 					rval = SATA_FAILURE;
11914 			}
11915 		}
11916 		/*
11917 		 * We have to set Multiword DMA or UDMA, if it is supported, as
11918 		 * we want to use DMA transfer mode whenever possible.
11919 		 * Some devices require explicit setting of the DMA mode.
11920 		 */
11921 		if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) {
11922 			/* Set highest supported DMA mode */
11923 			if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
11924 			    SATA_SUCCESS) {
11925 				SATA_LOG_D((sata_hba_inst, CE_WARN,
11926 				    "%s set UDMA mode\n", finfo));
11927 				rval = SATA_FAILURE;
11928 			}
11929 		}
11930 	}
11931 
11932 	if (!(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) &&
11933 	    !(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) {
11934 		/* None of the features is supported - do nothing */
11935 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
11936 		    "settable features not supported\n", NULL);
11937 		goto update_sdinfo;
11938 	}
11939 
11940 	if (((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) &&
11941 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) &&
11942 	    ((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) &&
11943 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
11944 		/* Nothing to do */
11945 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
11946 		    "no device features to set\n", NULL);
11947 		goto update_sdinfo;
11948 	}
11949 
11950 	if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) &&
11951 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD))) {
11952 		if (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) {
11953 			/* Enable read ahead / read cache */
11954 			cache_op = SATAC_SF_ENABLE_READ_AHEAD;
11955 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
11956 			    "enabling read cache\n", NULL);
11957 		} else {
11958 			/* Disable read ahead  / read cache */
11959 			cache_op = SATAC_SF_DISABLE_READ_AHEAD;
11960 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
11961 			    "disabling read cache\n", NULL);
11962 		}
11963 
11964 		/* Try to set read cache mode */
11965 		if (sata_set_cache_mode(sata_hba_inst, &new_sdinfo,
11966 		    cache_op) != SATA_SUCCESS) {
11967 			/* Pkt execution failed */
11968 			rval = SATA_FAILURE;
11969 		}
11970 	}
11971 
11972 	if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) &&
11973 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
11974 		if (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) {
11975 			/* Enable write cache */
11976 			cache_op = SATAC_SF_ENABLE_WRITE_CACHE;
11977 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
11978 			    "enabling write cache\n", NULL);
11979 		} else {
11980 			/* Disable write cache */
11981 			cache_op = SATAC_SF_DISABLE_WRITE_CACHE;
11982 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
11983 			    "disabling write cache\n", NULL);
11984 		}
11985 		/* Try to set write cache mode */
11986 		if (sata_set_cache_mode(sata_hba_inst, &new_sdinfo,
11987 		    cache_op) != SATA_SUCCESS) {
11988 			/* Pkt execution failed */
11989 			rval = SATA_FAILURE;
11990 		}
11991 	}
11992 
11993 	if (rval == SATA_FAILURE)
11994 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11995 		    "%s %s", finfo, finfox));
11996 update_sdinfo:
11997 	/*
11998 	 * We need to fetch Device Identify data again
11999 	 */
12000 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
12001 		/*
12002 		 * Cannot get device identification - retry later
12003 		 */
12004 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12005 		    "%s cannot re-fetch device identify data\n"));
12006 		rval = SATA_FAILURE;
12007 	}
12008 	/* Copy device sata info. */
12009 	sdinfo->satadrv_id = new_sdinfo.satadrv_id;
12010 
12011 	return (rval);
12012 }
12013 
12014 
12015 /*
12016  *
12017  * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if
12018  * unable to determine.
12019  *
12020  * Cannot be called in an interrupt context.
12021  *
12022  * Called by sata_build_lsense_page_2f()
12023  */
12024 
12025 static int
12026 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst,
12027     sata_drive_info_t *sdinfo)
12028 {
12029 	sata_pkt_t *spkt;
12030 	sata_cmd_t *scmd;
12031 	sata_pkt_txlate_t *spx;
12032 	int rval;
12033 
12034 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12035 	spx->txlt_sata_hba_inst = sata_hba_inst;
12036 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12037 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12038 	if (spkt == NULL) {
12039 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12040 		return (-1);
12041 	}
12042 	/* address is needed now */
12043 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12044 
12045 
12046 	/* Fill sata_pkt */
12047 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12048 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12049 	/* Synchronous mode, no callback */
12050 	spkt->satapkt_comp = NULL;
12051 	/* Timeout 30s */
12052 	spkt->satapkt_time = sata_default_pkt_time;
12053 
12054 	scmd = &spkt->satapkt_cmd;
12055 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
12056 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
12057 
12058 	/* Set up which registers need to be returned */
12059 	scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE;
12060 	scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE;
12061 
12062 	/* Build SMART_RETURN_STATUS cmd in the sata_pkt */
12063 	scmd->satacmd_addr_type = 0;		/* N/A */
12064 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
12065 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
12066 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
12067 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
12068 	scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS;
12069 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12070 	scmd->satacmd_cmd_reg = SATAC_SMART;
12071 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12072 	    sdinfo->satadrv_addr.cport)));
12073 
12074 
12075 	/* Send pkt to SATA HBA driver */
12076 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12077 	    SATA_TRAN_ACCEPTED ||
12078 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12079 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12080 		    sdinfo->satadrv_addr.cport)));
12081 		/*
12082 		 * Whoops, no SMART RETURN STATUS
12083 		 */
12084 		rval = -1;
12085 	} else {
12086 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12087 		    sdinfo->satadrv_addr.cport)));
12088 		if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
12089 			rval = -1;
12090 			goto fail;
12091 		}
12092 		if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
12093 			rval = -1;
12094 			goto fail;
12095 		}
12096 		if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) &&
12097 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2))
12098 			rval = 0;
12099 		else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) &&
12100 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4))
12101 			rval = 1;
12102 		else {
12103 			rval = -1;
12104 			goto fail;
12105 		}
12106 	}
12107 fail:
12108 	/* Free allocated resources */
12109 	sata_pkt_free(spx);
12110 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12111 
12112 	return (rval);
12113 }
12114 
12115 /*
12116  *
12117  * Returns 0 if succeeded, -1 otherwise
12118  *
12119  * Cannot be called in an interrupt context.
12120  *
12121  */
12122 static int
12123 sata_fetch_smart_data(
12124 	sata_hba_inst_t *sata_hba_inst,
12125 	sata_drive_info_t *sdinfo,
12126 	struct smart_data *smart_data)
12127 {
12128 	sata_pkt_t *spkt;
12129 	sata_cmd_t *scmd;
12130 	sata_pkt_txlate_t *spx;
12131 	int rval;
12132 
12133 #if ! defined(lint)
12134 	ASSERT(sizeof (struct smart_data) == 512);
12135 #endif
12136 
12137 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12138 	spx->txlt_sata_hba_inst = sata_hba_inst;
12139 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12140 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12141 	if (spkt == NULL) {
12142 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12143 		return (-1);
12144 	}
12145 	/* address is needed now */
12146 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12147 
12148 
12149 	/* Fill sata_pkt */
12150 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12151 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12152 	/* Synchronous mode, no callback */
12153 	spkt->satapkt_comp = NULL;
12154 	/* Timeout 30s */
12155 	spkt->satapkt_time = sata_default_pkt_time;
12156 
12157 	scmd = &spkt->satapkt_cmd;
12158 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
12159 
12160 	/*
12161 	 * Allocate buffer for SMART data
12162 	 */
12163 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
12164 	    sizeof (struct smart_data));
12165 	if (scmd->satacmd_bp == NULL) {
12166 		sata_pkt_free(spx);
12167 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12168 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12169 		    "sata_fetch_smart_data: "
12170 		    "cannot allocate buffer"));
12171 		return (-1);
12172 	}
12173 
12174 
12175 	/* Build SMART_READ_DATA cmd in the sata_pkt */
12176 	scmd->satacmd_addr_type = 0;		/* N/A */
12177 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
12178 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
12179 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
12180 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
12181 	scmd->satacmd_features_reg = SATA_SMART_READ_DATA;
12182 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12183 	scmd->satacmd_cmd_reg = SATAC_SMART;
12184 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12185 	    sdinfo->satadrv_addr.cport)));
12186 
12187 	/* Send pkt to SATA HBA driver */
12188 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12189 	    SATA_TRAN_ACCEPTED ||
12190 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12191 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12192 		    sdinfo->satadrv_addr.cport)));
12193 		/*
12194 		 * Whoops, no SMART DATA available
12195 		 */
12196 		rval = -1;
12197 		goto fail;
12198 	} else {
12199 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12200 		    sdinfo->satadrv_addr.cport)));
12201 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
12202 		    DDI_DMA_SYNC_FORKERNEL);
12203 		ASSERT(rval == DDI_SUCCESS);
12204 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data,
12205 		    sizeof (struct smart_data));
12206 	}
12207 
12208 fail:
12209 	/* Free allocated resources */
12210 	sata_free_local_buffer(spx);
12211 	sata_pkt_free(spx);
12212 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12213 
12214 	return (rval);
12215 }
12216 
12217 /*
12218  * Used by LOG SENSE page 0x10
12219  *
12220  * return 0 for success, -1 otherwise
12221  *
12222  */
12223 static int
12224 sata_ext_smart_selftest_read_log(
12225 	sata_hba_inst_t *sata_hba_inst,
12226 	sata_drive_info_t *sdinfo,
12227 	struct smart_ext_selftest_log *ext_selftest_log,
12228 	uint16_t block_num)
12229 {
12230 	sata_pkt_txlate_t *spx;
12231 	sata_pkt_t *spkt;
12232 	sata_cmd_t *scmd;
12233 	int rval;
12234 
12235 #if ! defined(lint)
12236 	ASSERT(sizeof (struct smart_ext_selftest_log) == 512);
12237 #endif
12238 
12239 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12240 	spx->txlt_sata_hba_inst = sata_hba_inst;
12241 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12242 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12243 	if (spkt == NULL) {
12244 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12245 		return (-1);
12246 	}
12247 	/* address is needed now */
12248 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12249 
12250 
12251 	/* Fill sata_pkt */
12252 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12253 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12254 	/* Synchronous mode, no callback */
12255 	spkt->satapkt_comp = NULL;
12256 	/* Timeout 30s */
12257 	spkt->satapkt_time = sata_default_pkt_time;
12258 
12259 	scmd = &spkt->satapkt_cmd;
12260 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
12261 
12262 	/*
12263 	 * Allocate buffer for SMART extended self-test log
12264 	 */
12265 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
12266 	    sizeof (struct smart_ext_selftest_log));
12267 	if (scmd->satacmd_bp == NULL) {
12268 		sata_pkt_free(spx);
12269 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12270 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12271 		    "sata_ext_smart_selftest_log: "
12272 		    "cannot allocate buffer"));
12273 		return (-1);
12274 	}
12275 
12276 	/* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */
12277 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
12278 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of selftest log */
12279 	scmd->satacmd_sec_count_msb = 0;	/* One sector of selftest log */
12280 	scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE;
12281 	scmd->satacmd_lba_low_msb = 0;
12282 	scmd->satacmd_lba_mid_lsb = block_num & 0xff;
12283 	scmd->satacmd_lba_mid_msb = block_num >> 8;
12284 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12285 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
12286 
12287 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12288 	    sdinfo->satadrv_addr.cport)));
12289 
12290 	/* Send pkt to SATA HBA driver */
12291 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12292 	    SATA_TRAN_ACCEPTED ||
12293 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12294 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12295 		    sdinfo->satadrv_addr.cport)));
12296 
12297 		/*
12298 		 * Whoops, no SMART selftest log info available
12299 		 */
12300 		rval = -1;
12301 		goto fail;
12302 	} else {
12303 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12304 		    sdinfo->satadrv_addr.cport)));
12305 
12306 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
12307 		    DDI_DMA_SYNC_FORKERNEL);
12308 		ASSERT(rval == DDI_SUCCESS);
12309 		bcopy(scmd->satacmd_bp->b_un.b_addr,
12310 		    (uint8_t *)ext_selftest_log,
12311 		    sizeof (struct smart_ext_selftest_log));
12312 		rval = 0;
12313 	}
12314 
12315 fail:
12316 	/* Free allocated resources */
12317 	sata_free_local_buffer(spx);
12318 	sata_pkt_free(spx);
12319 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12320 
12321 	return (rval);
12322 }
12323 
12324 /*
12325  * Returns 0 for success, -1 otherwise
12326  *
12327  * SMART self-test log data is returned in buffer pointed to by selftest_log
12328  */
12329 static int
12330 sata_smart_selftest_log(
12331 	sata_hba_inst_t *sata_hba_inst,
12332 	sata_drive_info_t *sdinfo,
12333 	struct smart_selftest_log *selftest_log)
12334 {
12335 	sata_pkt_t *spkt;
12336 	sata_cmd_t *scmd;
12337 	sata_pkt_txlate_t *spx;
12338 	int rval;
12339 
12340 #if ! defined(lint)
12341 	ASSERT(sizeof (struct smart_selftest_log) == 512);
12342 #endif
12343 
12344 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12345 	spx->txlt_sata_hba_inst = sata_hba_inst;
12346 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12347 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12348 	if (spkt == NULL) {
12349 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12350 		return (-1);
12351 	}
12352 	/* address is needed now */
12353 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12354 
12355 
12356 	/* Fill sata_pkt */
12357 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12358 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12359 	/* Synchronous mode, no callback */
12360 	spkt->satapkt_comp = NULL;
12361 	/* Timeout 30s */
12362 	spkt->satapkt_time = sata_default_pkt_time;
12363 
12364 	scmd = &spkt->satapkt_cmd;
12365 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
12366 
12367 	/*
12368 	 * Allocate buffer for SMART SELFTEST LOG
12369 	 */
12370 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
12371 	    sizeof (struct smart_selftest_log));
12372 	if (scmd->satacmd_bp == NULL) {
12373 		sata_pkt_free(spx);
12374 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12375 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12376 		    "sata_smart_selftest_log: "
12377 		    "cannot allocate buffer"));
12378 		return (-1);
12379 	}
12380 
12381 	/* Build SMART_READ_LOG cmd in the sata_pkt */
12382 	scmd->satacmd_addr_type = 0;		/* N/A */
12383 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of SMART log */
12384 	scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE;
12385 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
12386 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
12387 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
12388 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12389 	scmd->satacmd_cmd_reg = SATAC_SMART;
12390 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12391 	    sdinfo->satadrv_addr.cport)));
12392 
12393 	/* Send pkt to SATA HBA driver */
12394 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12395 	    SATA_TRAN_ACCEPTED ||
12396 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12397 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12398 		    sdinfo->satadrv_addr.cport)));
12399 		/*
12400 		 * Whoops, no SMART DATA available
12401 		 */
12402 		rval = -1;
12403 		goto fail;
12404 	} else {
12405 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12406 		    sdinfo->satadrv_addr.cport)));
12407 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
12408 		    DDI_DMA_SYNC_FORKERNEL);
12409 		ASSERT(rval == DDI_SUCCESS);
12410 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log,
12411 		    sizeof (struct smart_selftest_log));
12412 		rval = 0;
12413 	}
12414 
12415 fail:
12416 	/* Free allocated resources */
12417 	sata_free_local_buffer(spx);
12418 	sata_pkt_free(spx);
12419 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12420 
12421 	return (rval);
12422 }
12423 
12424 
12425 /*
12426  * Returns 0 for success, -1 otherwise
12427  *
12428  * SMART READ LOG data is returned in buffer pointed to by smart_log
12429  */
12430 static int
12431 sata_smart_read_log(
12432 	sata_hba_inst_t *sata_hba_inst,
12433 	sata_drive_info_t *sdinfo,
12434 	uint8_t *smart_log,		/* where the data should be returned */
12435 	uint8_t which_log,		/* which log should be returned */
12436 	uint8_t log_size)		/* # of 512 bytes in log */
12437 {
12438 	sata_pkt_t *spkt;
12439 	sata_cmd_t *scmd;
12440 	sata_pkt_txlate_t *spx;
12441 	int rval;
12442 
12443 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12444 	spx->txlt_sata_hba_inst = sata_hba_inst;
12445 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12446 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12447 	if (spkt == NULL) {
12448 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12449 		return (-1);
12450 	}
12451 	/* address is needed now */
12452 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12453 
12454 
12455 	/* Fill sata_pkt */
12456 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12457 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12458 	/* Synchronous mode, no callback */
12459 	spkt->satapkt_comp = NULL;
12460 	/* Timeout 30s */
12461 	spkt->satapkt_time = sata_default_pkt_time;
12462 
12463 	scmd = &spkt->satapkt_cmd;
12464 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
12465 
12466 	/*
12467 	 * Allocate buffer for SMART READ LOG
12468 	 */
12469 	scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512);
12470 	if (scmd->satacmd_bp == NULL) {
12471 		sata_pkt_free(spx);
12472 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12473 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12474 		    "sata_smart_read_log: " "cannot allocate buffer"));
12475 		return (-1);
12476 	}
12477 
12478 	/* Build SMART_READ_LOG cmd in the sata_pkt */
12479 	scmd->satacmd_addr_type = 0;		/* N/A */
12480 	scmd->satacmd_sec_count_lsb = log_size;	/* what the caller asked for */
12481 	scmd->satacmd_lba_low_lsb = which_log;	/* which log page */
12482 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
12483 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
12484 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
12485 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12486 	scmd->satacmd_cmd_reg = SATAC_SMART;
12487 
12488 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12489 	    sdinfo->satadrv_addr.cport)));
12490 
12491 	/* Send pkt to SATA HBA driver */
12492 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12493 	    SATA_TRAN_ACCEPTED ||
12494 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12495 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12496 		    sdinfo->satadrv_addr.cport)));
12497 
12498 		/*
12499 		 * Whoops, no SMART DATA available
12500 		 */
12501 		rval = -1;
12502 		goto fail;
12503 	} else {
12504 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12505 		    sdinfo->satadrv_addr.cport)));
12506 
12507 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
12508 		    DDI_DMA_SYNC_FORKERNEL);
12509 		ASSERT(rval == DDI_SUCCESS);
12510 		bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512);
12511 		rval = 0;
12512 	}
12513 
12514 fail:
12515 	/* Free allocated resources */
12516 	sata_free_local_buffer(spx);
12517 	sata_pkt_free(spx);
12518 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12519 
12520 	return (rval);
12521 }
12522 
12523 /*
12524  * Used by LOG SENSE page 0x10
12525  *
12526  * return 0 for success, -1 otherwise
12527  *
12528  */
12529 static int
12530 sata_read_log_ext_directory(
12531 	sata_hba_inst_t *sata_hba_inst,
12532 	sata_drive_info_t *sdinfo,
12533 	struct read_log_ext_directory *logdir)
12534 {
12535 	sata_pkt_txlate_t *spx;
12536 	sata_pkt_t *spkt;
12537 	sata_cmd_t *scmd;
12538 	int rval;
12539 
12540 #if ! defined(lint)
12541 	ASSERT(sizeof (struct read_log_ext_directory) == 512);
12542 #endif
12543 
12544 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12545 	spx->txlt_sata_hba_inst = sata_hba_inst;
12546 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12547 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12548 	if (spkt == NULL) {
12549 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12550 		return (-1);
12551 	}
12552 
12553 	/* Fill sata_pkt */
12554 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12555 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12556 	/* Synchronous mode, no callback */
12557 	spkt->satapkt_comp = NULL;
12558 	/* Timeout 30s */
12559 	spkt->satapkt_time = sata_default_pkt_time;
12560 
12561 	scmd = &spkt->satapkt_cmd;
12562 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
12563 
12564 	/*
12565 	 * Allocate buffer for SMART READ LOG EXTENDED command
12566 	 */
12567 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
12568 	    sizeof (struct read_log_ext_directory));
12569 	if (scmd->satacmd_bp == NULL) {
12570 		sata_pkt_free(spx);
12571 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12572 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12573 		    "sata_read_log_ext_directory: "
12574 		    "cannot allocate buffer"));
12575 		return (-1);
12576 	}
12577 
12578 	/* Build READ LOG EXT w/ log directory cmd in the  sata_pkt */
12579 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
12580 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of directory */
12581 	scmd->satacmd_sec_count_msb = 0;	/* One sector of directory */
12582 	scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY;
12583 	scmd->satacmd_lba_low_msb = 0;
12584 	scmd->satacmd_lba_mid_lsb = 0;
12585 	scmd->satacmd_lba_mid_msb = 0;
12586 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12587 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
12588 
12589 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12590 	    sdinfo->satadrv_addr.cport)));
12591 
12592 	/* Send pkt to SATA HBA driver */
12593 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12594 	    SATA_TRAN_ACCEPTED ||
12595 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12596 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12597 		    sdinfo->satadrv_addr.cport)));
12598 		/*
12599 		 * Whoops, no SMART selftest log info available
12600 		 */
12601 		rval = -1;
12602 		goto fail;
12603 	} else {
12604 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12605 		    sdinfo->satadrv_addr.cport)));
12606 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
12607 		    DDI_DMA_SYNC_FORKERNEL);
12608 		ASSERT(rval == DDI_SUCCESS);
12609 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir,
12610 		    sizeof (struct read_log_ext_directory));
12611 		rval = 0;
12612 	}
12613 
12614 fail:
12615 	/* Free allocated resources */
12616 	sata_free_local_buffer(spx);
12617 	sata_pkt_free(spx);
12618 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12619 
12620 	return (rval);
12621 }
12622 
12623 
12624 /*
12625  * sata_xlate_errors() is used to translate (S)ATA error
12626  * information to SCSI information returned in the SCSI
12627  * packet.
12628  */
12629 static void
12630 sata_xlate_errors(sata_pkt_txlate_t *spx)
12631 {
12632 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
12633 	struct scsi_extended_sense *sense;
12634 
12635 	scsipkt->pkt_reason = CMD_INCOMPLETE;
12636 	*scsipkt->pkt_scbp = STATUS_CHECK;
12637 	sense = sata_arq_sense(spx);
12638 
12639 	switch (spx->txlt_sata_pkt->satapkt_reason) {
12640 	case SATA_PKT_PORT_ERROR:
12641 		/*
12642 		 * We have no device data. Assume no data transfered.
12643 		 */
12644 		sense->es_key = KEY_HARDWARE_ERROR;
12645 		break;
12646 
12647 	case SATA_PKT_DEV_ERROR:
12648 		if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
12649 		    SATA_STATUS_ERR) {
12650 			/*
12651 			 * determine dev error reason from error
12652 			 * reg content
12653 			 */
12654 			sata_decode_device_error(spx, sense);
12655 			break;
12656 		}
12657 		/* No extended sense key - no info available */
12658 		break;
12659 
12660 	case SATA_PKT_TIMEOUT:
12661 		/*
12662 		 * scsipkt->pkt_reason = CMD_TIMEOUT; This causes problems.
12663 		 */
12664 		scsipkt->pkt_reason = CMD_INCOMPLETE;
12665 		/* No extended sense key */
12666 		break;
12667 
12668 	case SATA_PKT_ABORTED:
12669 		scsipkt->pkt_reason = CMD_ABORTED;
12670 		/* No extended sense key */
12671 		break;
12672 
12673 	case SATA_PKT_RESET:
12674 		/*
12675 		 * pkt aborted either by an explicit reset request from
12676 		 * a host, or due to error recovery
12677 		 */
12678 		scsipkt->pkt_reason = CMD_RESET;
12679 		break;
12680 
12681 	default:
12682 		scsipkt->pkt_reason = CMD_TRAN_ERR;
12683 		break;
12684 	}
12685 }
12686 
12687 
12688 
12689 
12690 /*
12691  * Log sata message
12692  * dev pathname msg line preceeds the logged message.
12693  */
12694 
12695 static	void
12696 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...)
12697 {
12698 	char pathname[128];
12699 	dev_info_t *dip;
12700 	va_list ap;
12701 
12702 	mutex_enter(&sata_log_mutex);
12703 
12704 	va_start(ap, fmt);
12705 	(void) vsprintf(sata_log_buf, fmt, ap);
12706 	va_end(ap);
12707 
12708 	if (sata_hba_inst != NULL) {
12709 		dip = SATA_DIP(sata_hba_inst);
12710 		(void) ddi_pathname(dip, pathname);
12711 	} else {
12712 		pathname[0] = 0;
12713 	}
12714 	if (level == CE_CONT) {
12715 		if (sata_debug_flags == 0)
12716 			cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf);
12717 		else
12718 			cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf);
12719 	} else
12720 		cmn_err(level, "%s:\n %s", pathname, sata_log_buf);
12721 
12722 	mutex_exit(&sata_log_mutex);
12723 }
12724 
12725 
12726 /* ******** Asynchronous HBA events handling & hotplugging support ******** */
12727 
12728 /*
12729  * Start or terminate the thread, depending on flag arg and current state
12730  */
12731 static void
12732 sata_event_thread_control(int startstop)
12733 {
12734 	static 	int sata_event_thread_terminating = 0;
12735 	static 	int sata_event_thread_starting = 0;
12736 	int i;
12737 
12738 	mutex_enter(&sata_event_mutex);
12739 
12740 	if (startstop == 0 && (sata_event_thread_starting == 1 ||
12741 	    sata_event_thread_terminating == 1)) {
12742 		mutex_exit(&sata_event_mutex);
12743 		return;
12744 	}
12745 	if (startstop == 1 && sata_event_thread_starting == 1) {
12746 		mutex_exit(&sata_event_mutex);
12747 		return;
12748 	}
12749 	if (startstop == 1 && sata_event_thread_terminating == 1) {
12750 		sata_event_thread_starting = 1;
12751 		/* wait til terminate operation completes */
12752 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
12753 		while (sata_event_thread_terminating == 1) {
12754 			if (i-- <= 0) {
12755 				sata_event_thread_starting = 0;
12756 				mutex_exit(&sata_event_mutex);
12757 #ifdef SATA_DEBUG
12758 				cmn_err(CE_WARN, "sata_event_thread_control: "
12759 				    "timeout waiting for thread to terminate");
12760 #endif
12761 				return;
12762 			}
12763 			mutex_exit(&sata_event_mutex);
12764 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
12765 			mutex_enter(&sata_event_mutex);
12766 		}
12767 	}
12768 	if (startstop == 1) {
12769 		if (sata_event_thread == NULL) {
12770 			sata_event_thread = thread_create(NULL, 0,
12771 			    (void (*)())sata_event_daemon,
12772 			    &sata_hba_list, 0, &p0, TS_RUN, minclsyspri);
12773 		}
12774 		sata_event_thread_starting = 0;
12775 		mutex_exit(&sata_event_mutex);
12776 		return;
12777 	}
12778 
12779 	/*
12780 	 * If we got here, thread may need to be terminated
12781 	 */
12782 	if (sata_event_thread != NULL) {
12783 		int i;
12784 		/* Signal event thread to go away */
12785 		sata_event_thread_terminating = 1;
12786 		sata_event_thread_terminate = 1;
12787 		cv_signal(&sata_event_cv);
12788 		/*
12789 		 * Wait til daemon terminates.
12790 		 */
12791 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
12792 		while (sata_event_thread_terminate == 1) {
12793 			mutex_exit(&sata_event_mutex);
12794 			if (i-- <= 0) {
12795 				/* Daemon did not go away !!! */
12796 #ifdef SATA_DEBUG
12797 				cmn_err(CE_WARN, "sata_event_thread_control: "
12798 				    "cannot terminate event daemon thread");
12799 #endif
12800 				mutex_enter(&sata_event_mutex);
12801 				break;
12802 			}
12803 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
12804 			mutex_enter(&sata_event_mutex);
12805 		}
12806 		sata_event_thread_terminating = 0;
12807 	}
12808 	ASSERT(sata_event_thread_terminating == 0);
12809 	ASSERT(sata_event_thread_starting == 0);
12810 	mutex_exit(&sata_event_mutex);
12811 }
12812 
12813 
12814 /*
12815  * SATA HBA event notification function.
12816  * Events reported by SATA HBA drivers per HBA instance relate to a change in
12817  * a port and/or device state or a controller itself.
12818  * Events for different addresses/addr types cannot be combined.
12819  * A warning message is generated for each event type.
12820  * Events are not processed by this function, so only the
12821  * event flag(s)is set for an affected entity and the event thread is
12822  * waken up. Event daemon thread processes all events.
12823  *
12824  * NOTE: Since more than one event may be reported at the same time, one
12825  * cannot determine a sequence of events when opposite event are reported, eg.
12826  * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing
12827  * is taking precedence over reported events, i.e. may cause ignoring some
12828  * events.
12829  */
12830 #define	SATA_EVENT_MAX_MSG_LENGTH	79
12831 
12832 void
12833 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event)
12834 {
12835 	sata_hba_inst_t *sata_hba_inst = NULL;
12836 	sata_address_t *saddr;
12837 	sata_drive_info_t *sdinfo;
12838 	sata_port_stats_t *pstats;
12839 	int cport, pmport;
12840 	char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1];
12841 	char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1];
12842 	char *lcp;
12843 	static char *err_msg_evnt_1 =
12844 	    "sata_hba_event_notify: invalid port event 0x%x ";
12845 	static char *err_msg_evnt_2 =
12846 	    "sata_hba_event_notify: invalid device event 0x%x ";
12847 	int linkevent;
12848 
12849 	/*
12850 	 * There is a possibility that an event will be generated on HBA
12851 	 * that has not completed attachment or is detaching.
12852 	 * HBA driver should prevent this, but just in case it does not,
12853 	 * we need to ignore events for such HBA.
12854 	 */
12855 	mutex_enter(&sata_mutex);
12856 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
12857 	    sata_hba_inst = sata_hba_inst->satahba_next) {
12858 		if (SATA_DIP(sata_hba_inst) == dip)
12859 			if (sata_hba_inst->satahba_attached == 1)
12860 				break;
12861 	}
12862 	mutex_exit(&sata_mutex);
12863 	if (sata_hba_inst == NULL)
12864 		/* HBA not attached */
12865 		return;
12866 
12867 	ASSERT(sata_device != NULL);
12868 
12869 	/*
12870 	 * Validate address before - do not proceed with invalid address.
12871 	 */
12872 	saddr = &sata_device->satadev_addr;
12873 	if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst))
12874 		return;
12875 	if (saddr->qual == SATA_ADDR_PMPORT ||
12876 	    saddr->qual == SATA_ADDR_DPMPORT)
12877 		/* Port Multiplier not supported yet */
12878 		return;
12879 
12880 	cport = saddr->cport;
12881 	pmport = saddr->pmport;
12882 
12883 	buf1[0] = buf2[0] = '\0';
12884 
12885 	/*
12886 	 * Events refer to devices, ports and controllers - each has
12887 	 * unique address. Events for different addresses cannot be combined.
12888 	 */
12889 	if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) {
12890 
12891 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
12892 
12893 		/* qualify this event(s) */
12894 		if ((event & SATA_EVNT_PORT_EVENTS) == 0) {
12895 			/* Invalid event for the device port */
12896 			(void) sprintf(buf2, err_msg_evnt_1,
12897 			    event & SATA_EVNT_PORT_EVENTS);
12898 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
12899 			goto event_info;
12900 		}
12901 		if (saddr->qual == SATA_ADDR_CPORT) {
12902 			/* Controller's device port event */
12903 
12904 			(SATA_CPORT_INFO(sata_hba_inst, cport))->
12905 			    cport_event_flags |=
12906 			    event & SATA_EVNT_PORT_EVENTS;
12907 			pstats =
12908 			    &(SATA_CPORT_INFO(sata_hba_inst, cport))->
12909 			    cport_stats;
12910 		} else {
12911 			/* Port multiplier's device port event */
12912 			(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
12913 			    pmport_event_flags |=
12914 			    event & SATA_EVNT_PORT_EVENTS;
12915 			pstats =
12916 			    &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
12917 			    pmport_stats;
12918 		}
12919 
12920 		/*
12921 		 * Add to statistics and log the message. We have to do it
12922 		 * here rather than in the event daemon, because there may be
12923 		 * multiple events occuring before they are processed.
12924 		 */
12925 		linkevent = event &
12926 		    (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED);
12927 		if (linkevent) {
12928 			if (linkevent == (SATA_EVNT_LINK_LOST |
12929 			    SATA_EVNT_LINK_ESTABLISHED)) {
12930 				/* This is likely event combination */
12931 				(void) strlcat(buf1, "link lost/established, ",
12932 				    SATA_EVENT_MAX_MSG_LENGTH);
12933 
12934 				if (pstats->link_lost < 0xffffffffffffffffULL)
12935 					pstats->link_lost++;
12936 				if (pstats->link_established <
12937 				    0xffffffffffffffffULL)
12938 					pstats->link_established++;
12939 				linkevent = 0;
12940 			} else if (linkevent & SATA_EVNT_LINK_LOST) {
12941 				(void) strlcat(buf1, "link lost, ",
12942 				    SATA_EVENT_MAX_MSG_LENGTH);
12943 
12944 				if (pstats->link_lost < 0xffffffffffffffffULL)
12945 					pstats->link_lost++;
12946 			} else {
12947 				(void) strlcat(buf1, "link established, ",
12948 				    SATA_EVENT_MAX_MSG_LENGTH);
12949 				if (pstats->link_established <
12950 				    0xffffffffffffffffULL)
12951 					pstats->link_established++;
12952 			}
12953 		}
12954 		if (event & SATA_EVNT_DEVICE_ATTACHED) {
12955 			(void) strlcat(buf1, "device attached, ",
12956 			    SATA_EVENT_MAX_MSG_LENGTH);
12957 			if (pstats->device_attached < 0xffffffffffffffffULL)
12958 				pstats->device_attached++;
12959 		}
12960 		if (event & SATA_EVNT_DEVICE_DETACHED) {
12961 			(void) strlcat(buf1, "device detached, ",
12962 			    SATA_EVENT_MAX_MSG_LENGTH);
12963 			if (pstats->device_detached < 0xffffffffffffffffULL)
12964 				pstats->device_detached++;
12965 		}
12966 		if (event & SATA_EVNT_PWR_LEVEL_CHANGED) {
12967 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
12968 			    "port %d power level changed", cport);
12969 			if (pstats->port_pwr_changed < 0xffffffffffffffffULL)
12970 				pstats->port_pwr_changed++;
12971 		}
12972 
12973 		if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) {
12974 			/* There should be no other events for this address */
12975 			(void) sprintf(buf2, err_msg_evnt_1,
12976 			    event & ~SATA_EVNT_PORT_EVENTS);
12977 		}
12978 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
12979 
12980 	} else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) {
12981 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
12982 
12983 		/* qualify this event */
12984 		if ((event & SATA_EVNT_DEVICE_RESET) == 0) {
12985 			/* Invalid event for a device */
12986 			(void) sprintf(buf2, err_msg_evnt_2,
12987 			    event & SATA_EVNT_DEVICE_RESET);
12988 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
12989 			goto event_info;
12990 		}
12991 		/* drive event */
12992 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12993 		if (sdinfo != NULL) {
12994 			if (event & SATA_EVNT_DEVICE_RESET) {
12995 				(void) strlcat(buf1, "device reset, ",
12996 				    SATA_EVENT_MAX_MSG_LENGTH);
12997 				if (sdinfo->satadrv_stats.drive_reset <
12998 				    0xffffffffffffffffULL)
12999 					sdinfo->satadrv_stats.drive_reset++;
13000 				sdinfo->satadrv_event_flags |=
13001 				    SATA_EVNT_DEVICE_RESET;
13002 			}
13003 		}
13004 		if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) {
13005 			/* Invalid event for a device */
13006 			(void) sprintf(buf2, err_msg_evnt_2,
13007 			    event & ~SATA_EVNT_DRIVE_EVENTS);
13008 		}
13009 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13010 	} else {
13011 		if (saddr->qual != SATA_ADDR_NULL) {
13012 			/* Wrong address qualifier */
13013 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13014 			    "sata_hba_event_notify: invalid address 0x%x",
13015 			    *(uint32_t *)saddr));
13016 			return;
13017 		}
13018 		if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 ||
13019 		    (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) {
13020 			/* Invalid event for the controller */
13021 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13022 			    "sata_hba_event_notify: invalid event 0x%x for "
13023 			    "controller",
13024 			    event & SATA_EVNT_CONTROLLER_EVENTS));
13025 			return;
13026 		}
13027 		buf1[0] = '\0';
13028 		/* This may be a frequent and not interesting event */
13029 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
13030 		    "controller power level changed\n", NULL);
13031 
13032 		mutex_enter(&sata_hba_inst->satahba_mutex);
13033 		if (sata_hba_inst->satahba_stats.ctrl_pwr_change <
13034 		    0xffffffffffffffffULL)
13035 			sata_hba_inst->satahba_stats.ctrl_pwr_change++;
13036 
13037 		sata_hba_inst->satahba_event_flags |=
13038 		    SATA_EVNT_PWR_LEVEL_CHANGED;
13039 		mutex_exit(&sata_hba_inst->satahba_mutex);
13040 	}
13041 	/*
13042 	 * If we got here, there is something to do with this HBA
13043 	 * instance.
13044 	 */
13045 	mutex_enter(&sata_hba_inst->satahba_mutex);
13046 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
13047 	mutex_exit(&sata_hba_inst->satahba_mutex);
13048 	mutex_enter(&sata_mutex);
13049 	sata_event_pending |= SATA_EVNT_MAIN;	/* global event indicator */
13050 	mutex_exit(&sata_mutex);
13051 
13052 	/* Tickle event thread */
13053 	mutex_enter(&sata_event_mutex);
13054 	if (sata_event_thread_active == 0)
13055 		cv_signal(&sata_event_cv);
13056 	mutex_exit(&sata_event_mutex);
13057 
13058 event_info:
13059 	if (buf1[0] != '\0') {
13060 		lcp = strrchr(buf1, ',');
13061 		if (lcp != NULL)
13062 			*lcp = '\0';
13063 	}
13064 	if (saddr->qual == SATA_ADDR_CPORT ||
13065 	    saddr->qual == SATA_ADDR_DCPORT) {
13066 		if (buf1[0] != '\0') {
13067 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
13068 			    cport, buf1);
13069 		}
13070 		if (buf2[0] != '\0') {
13071 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
13072 			    cport, buf2);
13073 		}
13074 	} else if (saddr->qual == SATA_ADDR_PMPORT ||
13075 	    saddr->qual == SATA_ADDR_DPMPORT) {
13076 		if (buf1[0] != '\0') {
13077 			sata_log(sata_hba_inst, CE_NOTE,
13078 			    "port %d pmport %d: %s\n", cport, pmport, buf1);
13079 		}
13080 		if (buf2[0] != '\0') {
13081 			sata_log(sata_hba_inst, CE_NOTE,
13082 			    "port %d pmport %d: %s\n", cport, pmport, buf2);
13083 		}
13084 	}
13085 }
13086 
13087 
13088 /*
13089  * Event processing thread.
13090  * Arg is a pointer to the sata_hba_list pointer.
13091  * It is not really needed, because sata_hba_list is global and static
13092  */
13093 static void
13094 sata_event_daemon(void *arg)
13095 {
13096 #ifndef __lock_lint
13097 	_NOTE(ARGUNUSED(arg))
13098 #endif
13099 	sata_hba_inst_t *sata_hba_inst;
13100 	clock_t lbolt;
13101 
13102 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13103 	    "SATA event daemon started\n", NULL);
13104 loop:
13105 	/*
13106 	 * Process events here. Walk through all registered HBAs
13107 	 */
13108 	mutex_enter(&sata_mutex);
13109 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13110 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13111 		ASSERT(sata_hba_inst != NULL);
13112 		mutex_enter(&sata_hba_inst->satahba_mutex);
13113 		if (sata_hba_inst->satahba_attached != 1 ||
13114 		    (sata_hba_inst->satahba_event_flags &
13115 		    SATA_EVNT_SKIP) != 0) {
13116 			mutex_exit(&sata_hba_inst->satahba_mutex);
13117 			continue;
13118 		}
13119 		if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) {
13120 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP;
13121 			mutex_exit(&sata_hba_inst->satahba_mutex);
13122 			mutex_exit(&sata_mutex);
13123 			/* Got the controller with pending event */
13124 			sata_process_controller_events(sata_hba_inst);
13125 			/*
13126 			 * Since global mutex was released, there is a
13127 			 * possibility that HBA list has changed, so start
13128 			 * over from the top. Just processed controller
13129 			 * will be passed-over because of the SKIP flag.
13130 			 */
13131 			goto loop;
13132 		}
13133 		mutex_exit(&sata_hba_inst->satahba_mutex);
13134 	}
13135 	/* Clear SKIP flag in all controllers */
13136 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13137 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13138 		mutex_enter(&sata_hba_inst->satahba_mutex);
13139 		sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP;
13140 		mutex_exit(&sata_hba_inst->satahba_mutex);
13141 	}
13142 	mutex_exit(&sata_mutex);
13143 
13144 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13145 	    "SATA EVENT DAEMON suspending itself", NULL);
13146 
13147 #ifdef SATA_DEBUG
13148 	if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) {
13149 		sata_log(sata_hba_inst, CE_WARN,
13150 		    "SATA EVENTS PROCESSING DISABLED\n");
13151 		thread_exit(); /* Daemon will not run again */
13152 	}
13153 #endif
13154 	mutex_enter(&sata_event_mutex);
13155 	sata_event_thread_active = 0;
13156 	mutex_exit(&sata_event_mutex);
13157 	/*
13158 	 * Go to sleep/suspend itself and wake up either because new event or
13159 	 * wait timeout. Exit if there is a termination request (driver
13160 	 * unload).
13161 	 */
13162 	do {
13163 		lbolt = ddi_get_lbolt();
13164 		lbolt += drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME);
13165 		mutex_enter(&sata_event_mutex);
13166 		(void) cv_timedwait(&sata_event_cv, &sata_event_mutex, lbolt);
13167 
13168 		if (sata_event_thread_active != 0) {
13169 			mutex_exit(&sata_event_mutex);
13170 			continue;
13171 		}
13172 
13173 		/* Check if it is time to go away */
13174 		if (sata_event_thread_terminate == 1) {
13175 			/*
13176 			 * It is up to the thread setting above flag to make
13177 			 * sure that this thread is not killed prematurely.
13178 			 */
13179 			sata_event_thread_terminate = 0;
13180 			sata_event_thread = NULL;
13181 			mutex_exit(&sata_event_mutex);
13182 			SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13183 			    "SATA_EVENT_DAEMON_TERMINATING", NULL);
13184 			thread_exit();  { _NOTE(NOT_REACHED) }
13185 		}
13186 		mutex_exit(&sata_event_mutex);
13187 	} while (!(sata_event_pending & SATA_EVNT_MAIN));
13188 
13189 	mutex_enter(&sata_event_mutex);
13190 	sata_event_thread_active = 1;
13191 	mutex_exit(&sata_event_mutex);
13192 
13193 	mutex_enter(&sata_mutex);
13194 	sata_event_pending &= ~SATA_EVNT_MAIN;
13195 	mutex_exit(&sata_mutex);
13196 
13197 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13198 	    "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL);
13199 
13200 	goto loop;
13201 }
13202 
13203 /*
13204  * Specific HBA instance event processing.
13205  *
13206  * NOTE: At the moment, device event processing is limited to hard disks
13207  * only.
13208  * cports only are supported - no pmports.
13209  */
13210 static void
13211 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst)
13212 {
13213 	int ncport;
13214 	uint32_t event_flags;
13215 	sata_address_t *saddr;
13216 
13217 	SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst,
13218 	    "Processing controller %d event(s)",
13219 	    ddi_get_instance(SATA_DIP(sata_hba_inst)));
13220 
13221 	mutex_enter(&sata_hba_inst->satahba_mutex);
13222 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN;
13223 	event_flags = sata_hba_inst->satahba_event_flags;
13224 	mutex_exit(&sata_hba_inst->satahba_mutex);
13225 	/*
13226 	 * Process controller power change first
13227 	 * HERE
13228 	 */
13229 	if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED)
13230 		sata_process_cntrl_pwr_level_change(sata_hba_inst);
13231 
13232 	/*
13233 	 * Search through ports/devices to identify affected port/device.
13234 	 * We may have to process events for more than one port/device.
13235 	 */
13236 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
13237 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
13238 		event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))->
13239 		    cport_event_flags;
13240 		/* Check if port was locked by IOCTL processing */
13241 		if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) {
13242 			/*
13243 			 * We ignore port events because port is busy
13244 			 * with AP control processing. Set again
13245 			 * controller and main event flag, so that
13246 			 * events may be processed by the next daemon
13247 			 * run.
13248 			 */
13249 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
13250 			mutex_enter(&sata_hba_inst->satahba_mutex);
13251 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
13252 			mutex_exit(&sata_hba_inst->satahba_mutex);
13253 			mutex_enter(&sata_mutex);
13254 			sata_event_pending |= SATA_EVNT_MAIN;
13255 			mutex_exit(&sata_mutex);
13256 			SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst,
13257 			    "Event processing postponed until "
13258 			    "AP control processing completes",
13259 			    NULL);
13260 			/* Check other ports */
13261 			continue;
13262 		} else {
13263 			/*
13264 			 * Set BSY flag so that AP control would not
13265 			 * interfere with events processing for
13266 			 * this port.
13267 			 */
13268 			(SATA_CPORT_INFO(sata_hba_inst, ncport))->
13269 			    cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY;
13270 		}
13271 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
13272 
13273 		saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr;
13274 
13275 		if ((event_flags &
13276 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
13277 			/*
13278 			 * Got port event.
13279 			 * We need some hierarchy of event processing as they
13280 			 * are affecting each other:
13281 			 * 1. port failed
13282 			 * 2. device detached/attached
13283 			 * 3. link events - link events may trigger device
13284 			 *    detached or device attached events in some
13285 			 *    circumstances.
13286 			 * 4. port power level changed
13287 			 */
13288 			if (event_flags & SATA_EVNT_PORT_FAILED) {
13289 				sata_process_port_failed_event(sata_hba_inst,
13290 				    saddr);
13291 			}
13292 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
13293 				sata_process_device_detached(sata_hba_inst,
13294 				    saddr);
13295 			}
13296 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
13297 				sata_process_device_attached(sata_hba_inst,
13298 				    saddr);
13299 			}
13300 			if (event_flags &
13301 			    (SATA_EVNT_LINK_ESTABLISHED |
13302 			    SATA_EVNT_LINK_LOST)) {
13303 				sata_process_port_link_events(sata_hba_inst,
13304 				    saddr);
13305 			}
13306 			if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) {
13307 				sata_process_port_pwr_change(sata_hba_inst,
13308 				    saddr);
13309 			}
13310 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
13311 				sata_process_target_node_cleanup(
13312 				    sata_hba_inst, saddr);
13313 			}
13314 		}
13315 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) !=
13316 		    SATA_DTYPE_NONE) {
13317 			/* May have device event */
13318 			sata_process_device_reset(sata_hba_inst, saddr);
13319 		}
13320 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
13321 		/* Release PORT_BUSY flag */
13322 		(SATA_CPORT_INFO(sata_hba_inst, ncport))->
13323 		    cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
13324 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
13325 
13326 	} /* End of loop through the controller SATA ports */
13327 }
13328 
13329 /*
13330  * Process HBA power level change reported by HBA driver.
13331  * Not implemented at this time - event is ignored.
13332  */
13333 static void
13334 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst)
13335 {
13336 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
13337 	    "Processing controller power level change", NULL);
13338 
13339 	/* Ignoring it for now */
13340 	mutex_enter(&sata_hba_inst->satahba_mutex);
13341 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
13342 	mutex_exit(&sata_hba_inst->satahba_mutex);
13343 }
13344 
13345 /*
13346  * Process port power level change reported by HBA driver.
13347  * Not implemented at this time - event is ignored.
13348  */
13349 static void
13350 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst,
13351     sata_address_t *saddr)
13352 {
13353 	sata_cport_info_t *cportinfo;
13354 
13355 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
13356 	    "Processing port power level change", NULL);
13357 
13358 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
13359 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13360 	/* Reset event flag */
13361 	cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
13362 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13363 }
13364 
13365 /*
13366  * Process port failure reported by HBA driver.
13367  * cports support only - no pmports.
13368  */
13369 static void
13370 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst,
13371     sata_address_t *saddr)
13372 {
13373 	sata_cport_info_t *cportinfo;
13374 
13375 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
13376 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13377 	/* Reset event flag first */
13378 	cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED;
13379 	/* If the port is in SHUTDOWN or FAILED state, ignore this event. */
13380 	if ((cportinfo->cport_state &
13381 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) {
13382 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
13383 		    cport_mutex);
13384 		return;
13385 	}
13386 	/* Fail the port */
13387 	cportinfo->cport_state = SATA_PSTATE_FAILED;
13388 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13389 	sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport);
13390 }
13391 
13392 /*
13393  * Device Reset Event processing.
13394  * The seqeunce is managed by 3 stage flags:
13395  * - reset event reported,
13396  * - reset event being processed,
13397  * - request to clear device reset state.
13398  */
13399 static void
13400 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst,
13401     sata_address_t *saddr)
13402 {
13403 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
13404 	sata_drive_info_t *sdinfo;
13405 	sata_cport_info_t *cportinfo;
13406 	sata_device_t sata_device;
13407 	int rval;
13408 
13409 	/* We only care about host sata cport for now */
13410 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
13411 
13412 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13413 
13414 	/* If the port is in SHUTDOWN or FAILED state, ignore reset event. */
13415 	if ((cportinfo->cport_state &
13416 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
13417 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
13418 		    cport_mutex);
13419 		return;
13420 	}
13421 
13422 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) &
13423 	    SATA_VALID_DEV_TYPE) == 0) {
13424 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
13425 		    cport_mutex);
13426 		return;
13427 	}
13428 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
13429 	if (sdinfo == NULL) {
13430 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
13431 		    cport_mutex);
13432 		return;
13433 	}
13434 
13435 	if ((sdinfo->satadrv_event_flags &
13436 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
13437 		/* Nothing to do */
13438 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
13439 		    cport_mutex);
13440 		return;
13441 	}
13442 #ifdef SATA_DEBUG
13443 	if ((sdinfo->satadrv_event_flags &
13444 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
13445 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
13446 		/* Something is weird - new device reset event */
13447 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
13448 		    "Overlapping device reset events!", NULL);
13449 	}
13450 #endif
13451 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
13452 	    "Processing port %d device reset", saddr->cport);
13453 
13454 	/* Clear event flag */
13455 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
13456 
13457 	/* It seems that we always need to check the port state first */
13458 	sata_device.satadev_rev = SATA_DEVICE_REV;
13459 	sata_device.satadev_addr = *saddr;
13460 	/*
13461 	 * We have to exit mutex, because the HBA probe port function may
13462 	 * block on its own mutex.
13463 	 */
13464 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13465 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
13466 	    (SATA_DIP(sata_hba_inst), &sata_device);
13467 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13468 	sata_update_port_info(sata_hba_inst, &sata_device);
13469 	if (rval != SATA_SUCCESS) {
13470 		/* Something went wrong? Fail the port */
13471 		cportinfo->cport_state = SATA_PSTATE_FAILED;
13472 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
13473 		    cport_mutex);
13474 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13475 		    "SATA port %d probing failed",
13476 		    saddr->cport));
13477 		return;
13478 	}
13479 	if ((sata_device.satadev_scr.sstatus  &
13480 	    SATA_PORT_DEVLINK_UP_MASK) !=
13481 	    SATA_PORT_DEVLINK_UP ||
13482 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
13483 		/*
13484 		 * No device to process, anymore. Some other event processing
13485 		 * would or have already performed port info cleanup.
13486 		 * To be safe (HBA may need it), request clearing device
13487 		 * reset condition.
13488 		 */
13489 		sdinfo->satadrv_event_flags = 0;
13490 		sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
13491 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
13492 		    cport_mutex);
13493 		return;
13494 	}
13495 
13496 	/* Mark device reset processing as active */
13497 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
13498 
13499 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
13500 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13501 
13502 	if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) ==
13503 	    SATA_FAILURE) {
13504 		/*
13505 		 * Restoring drive setting failed.
13506 		 * Probe the port first, to check if the port state has changed
13507 		 */
13508 		sata_device.satadev_rev = SATA_DEVICE_REV;
13509 		sata_device.satadev_addr = *saddr;
13510 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
13511 		/* probe port */
13512 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
13513 		    (SATA_DIP(sata_hba_inst), &sata_device);
13514 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
13515 		    cport_mutex);
13516 		if (rval == SATA_SUCCESS &&
13517 		    (sata_device.satadev_state &
13518 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
13519 		    (sata_device.satadev_scr.sstatus  &
13520 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
13521 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
13522 			/*
13523 			 * We may retry this a bit later - in-process reset
13524 			 * condition should be already set.
13525 			 */
13526 			if ((cportinfo->cport_dev_type &
13527 			    SATA_VALID_DEV_TYPE) != 0 &&
13528 			    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
13529 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
13530 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
13531 				    saddr->cport)->cport_mutex);
13532 				mutex_enter(&sata_hba_inst->satahba_mutex);
13533 				sata_hba_inst->satahba_event_flags |=
13534 				    SATA_EVNT_MAIN;
13535 				mutex_exit(&sata_hba_inst->satahba_mutex);
13536 				mutex_enter(&sata_mutex);
13537 				sata_event_pending |= SATA_EVNT_MAIN;
13538 				mutex_exit(&sata_mutex);
13539 				return;
13540 			}
13541 		} else {
13542 			/*
13543 			 * No point of retrying - some other event processing
13544 			 * would or already did port info cleanup.
13545 			 * To be safe (HBA may need it),
13546 			 * request clearing device reset condition.
13547 			 */
13548 			sdinfo->satadrv_event_flags = 0;
13549 			sdinfo->satadrv_event_flags |=
13550 			    SATA_EVNT_CLEAR_DEVICE_RESET;
13551 		}
13552 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
13553 		    cport_mutex);
13554 		return;
13555 	}
13556 
13557 	/*
13558 	 * Raise the flag indicating that the next sata command could
13559 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
13560 	 * reset is reported.
13561 	 */
13562 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13563 	if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0 &&
13564 	    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
13565 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
13566 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
13567 		sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
13568 	}
13569 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13570 }
13571 
13572 
13573 /*
13574  * Port Link Events processing.
13575  * Every link established event may involve device reset (due to
13576  * COMRESET signal, equivalent of the hard reset) so arbitrarily
13577  * set device reset event for an attached device (if any).
13578  * If the port is in SHUTDOWN or FAILED state, ignore link events.
13579  *
13580  * The link established event processing varies, depending on the state
13581  * of the target node, HBA hotplugging capabilities, state of the port.
13582  * If the link is not active, the link established event is ignored.
13583  * If HBA cannot detect device attachment and there is no target node,
13584  * the link established event triggers device attach event processing.
13585  * Else, link established event triggers device reset event processing.
13586  *
13587  * The link lost event processing varies, depending on a HBA hotplugging
13588  * capability and the state of the port (link active or not active).
13589  * If the link is active, the lost link event is ignored.
13590  * If HBA cannot detect device removal, the lost link event triggers
13591  * device detached event processing after link lost timeout.
13592  * Else, the event is ignored.
13593  *
13594  * NOTE: Only cports are processed for now, i.e. no port multiplier ports
13595  */
13596 static void
13597 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst,
13598     sata_address_t *saddr)
13599 {
13600 	sata_device_t sata_device;
13601 	sata_cport_info_t *cportinfo;
13602 	sata_drive_info_t *sdinfo;
13603 	uint32_t event_flags;
13604 	int rval;
13605 
13606 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
13607 	    "Processing port %d link event(s)", saddr->cport);
13608 
13609 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
13610 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13611 	event_flags = cportinfo->cport_event_flags;
13612 
13613 	/* Reset event flags first */
13614 	cportinfo->cport_event_flags &=
13615 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
13616 
13617 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
13618 	if ((cportinfo->cport_state &
13619 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
13620 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
13621 		    cport_mutex);
13622 		return;
13623 	}
13624 
13625 	/*
13626 	 * For the sanity sake get current port state.
13627 	 * Set device address only. Other sata_device fields should be
13628 	 * set by HBA driver.
13629 	 */
13630 	sata_device.satadev_rev = SATA_DEVICE_REV;
13631 	sata_device.satadev_addr = *saddr;
13632 	/*
13633 	 * We have to exit mutex, because the HBA probe port function may
13634 	 * block on its own mutex.
13635 	 */
13636 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13637 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
13638 	    (SATA_DIP(sata_hba_inst), &sata_device);
13639 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13640 	sata_update_port_info(sata_hba_inst, &sata_device);
13641 	if (rval != SATA_SUCCESS) {
13642 		/* Something went wrong? Fail the port */
13643 		cportinfo->cport_state = SATA_PSTATE_FAILED;
13644 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
13645 		    cport_mutex);
13646 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13647 		    "SATA port %d probing failed",
13648 		    saddr->cport));
13649 		/*
13650 		 * We may want to release device info structure, but
13651 		 * it is not necessary.
13652 		 */
13653 		return;
13654 	} else {
13655 		/* port probed successfully */
13656 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
13657 	}
13658 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
13659 
13660 		if ((sata_device.satadev_scr.sstatus &
13661 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
13662 			/* Ignore event */
13663 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
13664 			    "Ignoring port %d link established event - "
13665 			    "link down",
13666 			    saddr->cport);
13667 			goto linklost;
13668 		}
13669 
13670 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
13671 		    "Processing port %d link established event",
13672 		    saddr->cport);
13673 
13674 		/*
13675 		 * For the sanity sake check if a device is attached - check
13676 		 * return state of a port probing.
13677 		 */
13678 		if (sata_device.satadev_type != SATA_DTYPE_NONE &&
13679 		    sata_device.satadev_type != SATA_DTYPE_PMULT) {
13680 			/*
13681 			 * HBA port probe indicated that there is a device
13682 			 * attached. Check if the framework had device info
13683 			 * structure attached for this device.
13684 			 */
13685 			if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
13686 				ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) !=
13687 				    NULL);
13688 
13689 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
13690 				if ((sdinfo->satadrv_type &
13691 				    SATA_VALID_DEV_TYPE) != 0) {
13692 					/*
13693 					 * Dev info structure is present.
13694 					 * If dev_type is set to known type in
13695 					 * the framework's drive info struct
13696 					 * then the device existed before and
13697 					 * the link was probably lost
13698 					 * momentarily - in such case
13699 					 * we may want to check device
13700 					 * identity.
13701 					 * Identity check is not supported now.
13702 					 *
13703 					 * Link established event
13704 					 * triggers device reset event.
13705 					 */
13706 					(SATA_CPORTINFO_DRV_INFO(cportinfo))->
13707 					    satadrv_event_flags |=
13708 					    SATA_EVNT_DEVICE_RESET;
13709 				}
13710 			} else if (cportinfo->cport_dev_type ==
13711 			    SATA_DTYPE_NONE) {
13712 				/*
13713 				 * We got new device attached! If HBA does not
13714 				 * generate device attached events, trigger it
13715 				 * here.
13716 				 */
13717 				if (!(SATA_FEATURES(sata_hba_inst) &
13718 				    SATA_CTLF_HOTPLUG)) {
13719 					cportinfo->cport_event_flags |=
13720 					    SATA_EVNT_DEVICE_ATTACHED;
13721 				}
13722 			}
13723 			/* Reset link lost timeout */
13724 			cportinfo->cport_link_lost_time = 0;
13725 		}
13726 	}
13727 linklost:
13728 	if (event_flags & SATA_EVNT_LINK_LOST) {
13729 		if ((sata_device.satadev_scr.sstatus &
13730 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
13731 			/* Ignore event */
13732 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
13733 			    "Ignoring port %d link lost event - link is up",
13734 			    saddr->cport);
13735 			goto done;
13736 		}
13737 #ifdef SATA_DEBUG
13738 		if (cportinfo->cport_link_lost_time == 0) {
13739 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
13740 			    "Processing port %d link lost event",
13741 			    saddr->cport);
13742 		}
13743 #endif
13744 		/*
13745 		 * When HBA cannot generate device attached/detached events,
13746 		 * we need to track link lost time and eventually generate
13747 		 * device detach event.
13748 		 */
13749 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
13750 			/* We are tracking link lost time */
13751 			if (cportinfo->cport_link_lost_time == 0) {
13752 				/* save current time (lbolt value) */
13753 				cportinfo->cport_link_lost_time =
13754 				    ddi_get_lbolt();
13755 				/* just keep link lost event */
13756 				cportinfo->cport_event_flags |=
13757 				    SATA_EVNT_LINK_LOST;
13758 			} else {
13759 				clock_t cur_time = ddi_get_lbolt();
13760 				if ((cur_time -
13761 				    cportinfo->cport_link_lost_time) >=
13762 				    drv_usectohz(
13763 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
13764 					/* trigger device detach event */
13765 					cportinfo->cport_event_flags |=
13766 					    SATA_EVNT_DEVICE_DETACHED;
13767 					cportinfo->cport_link_lost_time = 0;
13768 					SATADBG1(SATA_DBG_EVENTS,
13769 					    sata_hba_inst,
13770 					    "Triggering port %d "
13771 					    "device detached event",
13772 					    saddr->cport);
13773 				} else {
13774 					/* keep link lost event */
13775 					cportinfo->cport_event_flags |=
13776 					    SATA_EVNT_LINK_LOST;
13777 				}
13778 			}
13779 		}
13780 		/*
13781 		 * We could change port state to disable/delay access to
13782 		 * the attached device until the link is recovered.
13783 		 */
13784 	}
13785 done:
13786 	event_flags = cportinfo->cport_event_flags;
13787 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13788 	if (event_flags != 0) {
13789 		mutex_enter(&sata_hba_inst->satahba_mutex);
13790 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
13791 		mutex_exit(&sata_hba_inst->satahba_mutex);
13792 		mutex_enter(&sata_mutex);
13793 		sata_event_pending |= SATA_EVNT_MAIN;
13794 		mutex_exit(&sata_mutex);
13795 	}
13796 }
13797 
13798 /*
13799  * Device Detached Event processing.
13800  * Port is probed to find if a device is really gone. If so,
13801  * the device info structure is detached from the SATA port info structure
13802  * and released.
13803  * Port status is updated.
13804  *
13805  * NOTE: Process cports event only, no port multiplier ports.
13806  */
13807 static void
13808 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst,
13809     sata_address_t *saddr)
13810 {
13811 	sata_cport_info_t *cportinfo;
13812 	sata_drive_info_t *sdevinfo;
13813 	sata_device_t sata_device;
13814 	dev_info_t *tdip;
13815 	int rval;
13816 
13817 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
13818 	    "Processing port %d device detached", saddr->cport);
13819 
13820 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
13821 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13822 	/* Clear event flag */
13823 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
13824 
13825 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
13826 	if ((cportinfo->cport_state &
13827 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
13828 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
13829 		    cport_mutex);
13830 		return;
13831 	}
13832 	/* For sanity, re-probe the port */
13833 	sata_device.satadev_rev = SATA_DEVICE_REV;
13834 	sata_device.satadev_addr = *saddr;
13835 
13836 	/*
13837 	 * We have to exit mutex, because the HBA probe port function may
13838 	 * block on its own mutex.
13839 	 */
13840 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13841 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
13842 	    (SATA_DIP(sata_hba_inst), &sata_device);
13843 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13844 	sata_update_port_info(sata_hba_inst, &sata_device);
13845 	if (rval != SATA_SUCCESS) {
13846 		/* Something went wrong? Fail the port */
13847 		cportinfo->cport_state = SATA_PSTATE_FAILED;
13848 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
13849 		    cport_mutex);
13850 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13851 		    "SATA port %d probing failed",
13852 		    saddr->cport));
13853 		/*
13854 		 * We may want to release device info structure, but
13855 		 * it is not necessary.
13856 		 */
13857 		return;
13858 	} else {
13859 		/* port probed successfully */
13860 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
13861 	}
13862 	/*
13863 	 * Check if a device is still attached. For sanity, check also
13864 	 * link status - if no link, there is no device.
13865 	 */
13866 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
13867 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
13868 	    SATA_DTYPE_NONE) {
13869 		/*
13870 		 * Device is still attached - ignore detach event.
13871 		 */
13872 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
13873 		    cport_mutex);
13874 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
13875 		    "Ignoring detach - device still attached to port %d",
13876 		    sata_device.satadev_addr.cport);
13877 		return;
13878 	}
13879 	/*
13880 	 * We need to detach and release device info structure here
13881 	 */
13882 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
13883 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
13884 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
13885 		(void) kmem_free((void *)sdevinfo,
13886 		    sizeof (sata_drive_info_t));
13887 	}
13888 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
13889 	/*
13890 	 * Device cannot be reached anymore, even if the target node may be
13891 	 * still present.
13892 	 */
13893 
13894 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13895 	sata_log(sata_hba_inst, CE_WARN, "SATA device detached at port %d",
13896 	    sata_device.satadev_addr.cport);
13897 
13898 	/*
13899 	 * Try to offline a device and remove target node if it still exists
13900 	 */
13901 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
13902 	if (tdip != NULL) {
13903 		/*
13904 		 * Target node exists.  Unconfigure device then remove
13905 		 * the target node (one ndi operation).
13906 		 */
13907 		if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
13908 			/*
13909 			 * PROBLEM - no device, but target node remained
13910 			 * This happens when the file was open or node was
13911 			 * waiting for resources.
13912 			 */
13913 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13914 			    "sata_process_device_detached: "
13915 			    "Failed to remove target node for "
13916 			    "detached SATA device."));
13917 			/*
13918 			 * Set target node state to DEVI_DEVICE_REMOVED.
13919 			 * But re-check first that the node still exists.
13920 			 */
13921 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
13922 			    saddr->cport);
13923 			if (tdip != NULL) {
13924 				sata_set_device_removed(tdip);
13925 				/*
13926 				 * Instruct event daemon to retry the
13927 				 * cleanup later.
13928 				 */
13929 				sata_set_target_node_cleanup(sata_hba_inst,
13930 				    saddr->cport);
13931 			}
13932 		}
13933 	}
13934 	/*
13935 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
13936 	 * with the hint: SE_HINT_REMOVE
13937 	 */
13938 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
13939 }
13940 
13941 
13942 /*
13943  * Device Attached Event processing.
13944  * Port state is checked to verify that a device is really attached. If so,
13945  * the device info structure is created and attached to the SATA port info
13946  * structure.
13947  *
13948  * If attached device cannot be identified or set-up, the retry for the
13949  * attach processing is set-up. Subsequent daemon run would try again to
13950  * identify the device, until the time limit is reached
13951  * (SATA_DEV_IDENTIFY_TIMEOUT).
13952  *
13953  * This function cannot be called in interrupt context (it may sleep).
13954  *
13955  * NOTE: Process cports event only, no port multiplier ports.
13956  */
13957 static void
13958 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst,
13959     sata_address_t *saddr)
13960 {
13961 	sata_cport_info_t *cportinfo;
13962 	sata_drive_info_t *sdevinfo;
13963 	sata_device_t sata_device;
13964 	dev_info_t *tdip;
13965 	uint32_t event_flags;
13966 	int rval;
13967 
13968 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
13969 	    "Processing port %d device attached", saddr->cport);
13970 
13971 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
13972 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13973 
13974 	/* Clear attach event flag first */
13975 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
13976 
13977 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
13978 	if ((cportinfo->cport_state &
13979 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
13980 		cportinfo->cport_dev_attach_time = 0;
13981 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
13982 		    cport_mutex);
13983 		return;
13984 	}
13985 
13986 	/*
13987 	 * If the sata_drive_info structure is found attached to the port info,
13988 	 * despite the fact the device was removed and now it is re-attached,
13989 	 * the old drive info structure was not removed.
13990 	 * Arbitrarily release device info structure.
13991 	 */
13992 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
13993 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
13994 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
13995 		(void) kmem_free((void *)sdevinfo,
13996 		    sizeof (sata_drive_info_t));
13997 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
13998 		    "Arbitrarily detaching old device info.", NULL);
13999 	}
14000 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14001 
14002 	/* For sanity, re-probe the port */
14003 	sata_device.satadev_rev = SATA_DEVICE_REV;
14004 	sata_device.satadev_addr = *saddr;
14005 
14006 	/*
14007 	 * We have to exit mutex, because the HBA probe port function may
14008 	 * block on its own mutex.
14009 	 */
14010 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14011 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14012 	    (SATA_DIP(sata_hba_inst), &sata_device);
14013 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14014 	sata_update_port_info(sata_hba_inst, &sata_device);
14015 	if (rval != SATA_SUCCESS) {
14016 		/* Something went wrong? Fail the port */
14017 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14018 		cportinfo->cport_dev_attach_time = 0;
14019 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14020 		    cport_mutex);
14021 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14022 		    "SATA port %d probing failed",
14023 		    saddr->cport));
14024 		return;
14025 	} else {
14026 		/* port probed successfully */
14027 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14028 	}
14029 	/*
14030 	 * Check if a device is still attached. For sanity, check also
14031 	 * link status - if no link, there is no device.
14032 	 */
14033 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
14034 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
14035 	    SATA_DTYPE_NONE) {
14036 		/*
14037 		 * No device - ignore attach event.
14038 		 */
14039 		cportinfo->cport_dev_attach_time = 0;
14040 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14041 		    cport_mutex);
14042 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14043 		    "Ignoring attach - no device connected to port %d",
14044 		    sata_device.satadev_addr.cport);
14045 		return;
14046 	}
14047 
14048 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14049 	/*
14050 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14051 	 * with the hint: SE_HINT_INSERT
14052 	 */
14053 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
14054 
14055 	/*
14056 	 * Port reprobing will take care of the creation of the device
14057 	 * info structure and determination of the device type.
14058 	 */
14059 	sata_device.satadev_addr = *saddr;
14060 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
14061 	    SATA_DEV_IDENTIFY_NORETRY);
14062 
14063 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14064 	    cport_mutex);
14065 	if ((cportinfo->cport_state & SATA_STATE_READY) &&
14066 	    (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) {
14067 		/* Some device is attached to the port */
14068 		if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) {
14069 			/*
14070 			 * A device was not successfully attached.
14071 			 * Track retry time for device identification.
14072 			 */
14073 			if (cportinfo->cport_dev_attach_time != 0) {
14074 				clock_t cur_time = ddi_get_lbolt();
14075 				/*
14076 				 * If the retry time limit was not exceeded,
14077 				 * reinstate attach event.
14078 				 */
14079 				if ((cur_time -
14080 				    cportinfo->cport_dev_attach_time) <
14081 				    drv_usectohz(
14082 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
14083 					/* OK, restore attach event */
14084 					cportinfo->cport_event_flags |=
14085 					    SATA_EVNT_DEVICE_ATTACHED;
14086 				} else {
14087 					/* Timeout - cannot identify device */
14088 					cportinfo->cport_dev_attach_time = 0;
14089 					sata_log(sata_hba_inst,
14090 					    CE_WARN,
14091 					    "Cannot identify SATA device "
14092 					    "at port %d - device will not be "
14093 					    "attached.",
14094 					    saddr->cport);
14095 				}
14096 			} else {
14097 				/*
14098 				 * Start tracking time for device
14099 				 * identification.
14100 				 * Save current time (lbolt value).
14101 				 */
14102 				cportinfo->cport_dev_attach_time =
14103 				    ddi_get_lbolt();
14104 				/* Restore attach event */
14105 				cportinfo->cport_event_flags |=
14106 				    SATA_EVNT_DEVICE_ATTACHED;
14107 			}
14108 		} else {
14109 			/*
14110 			 * If device was successfully attached, an explicit
14111 			 * 'configure' command will be needed to configure it.
14112 			 * Log the message indicating that a device
14113 			 * was attached.
14114 			 */
14115 			cportinfo->cport_dev_attach_time = 0;
14116 			sata_log(sata_hba_inst, CE_WARN,
14117 			    "SATA device detected at port %d", saddr->cport);
14118 
14119 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14120 				sata_drive_info_t new_sdinfo;
14121 
14122 				/* Log device info data */
14123 				new_sdinfo = *(SATA_CPORTINFO_DRV_INFO(
14124 				    cportinfo));
14125 				sata_show_drive_info(sata_hba_inst,
14126 				    &new_sdinfo);
14127 			}
14128 
14129 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14130 			    saddr->cport)->cport_mutex);
14131 
14132 			/*
14133 			 * Make sure that there is no target node for that
14134 			 * device. If so, release it. It should not happen,
14135 			 * unless we had problem removing the node when
14136 			 * device was detached.
14137 			 */
14138 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
14139 			    saddr->cport);
14140 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14141 			    saddr->cport)->cport_mutex);
14142 			if (tdip != NULL) {
14143 
14144 #ifdef SATA_DEBUG
14145 				if ((cportinfo->cport_event_flags &
14146 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
14147 					sata_log(sata_hba_inst, CE_WARN,
14148 					    "sata_process_device_attached: "
14149 					    "old device target node exists!");
14150 #endif
14151 				/*
14152 				 * target node exists - try to unconfigure
14153 				 * device and remove the node.
14154 				 */
14155 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14156 				    saddr->cport)->cport_mutex);
14157 				rval = ndi_devi_offline(tdip,
14158 				    NDI_DEVI_REMOVE);
14159 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14160 				    saddr->cport)->cport_mutex);
14161 
14162 				if (rval == NDI_SUCCESS) {
14163 					cportinfo->cport_event_flags &=
14164 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
14165 					cportinfo->cport_tgtnode_clean = B_TRUE;
14166 				} else {
14167 					/*
14168 					 * PROBLEM - the target node remained
14169 					 * and it belongs to a previously
14170 					 * attached device.
14171 					 * This happens when the file was open
14172 					 * or the node was waiting for
14173 					 * resources at the time the
14174 					 * associated device was removed.
14175 					 * Instruct event daemon to retry the
14176 					 * cleanup later.
14177 					 */
14178 					sata_log(sata_hba_inst,
14179 					    CE_WARN,
14180 					    "Application(s) accessing "
14181 					    "previously attached SATA "
14182 					    "device have to release "
14183 					    "it before newly inserted "
14184 					    "device can be made accessible.",
14185 					    saddr->cport);
14186 					cportinfo->cport_event_flags |=
14187 					    SATA_EVNT_TARGET_NODE_CLEANUP;
14188 					cportinfo->cport_tgtnode_clean =
14189 					    B_FALSE;
14190 				}
14191 			}
14192 
14193 		}
14194 	} else {
14195 		cportinfo->cport_dev_attach_time = 0;
14196 	}
14197 
14198 	event_flags = cportinfo->cport_event_flags;
14199 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14200 	if (event_flags != 0) {
14201 		mutex_enter(&sata_hba_inst->satahba_mutex);
14202 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14203 		mutex_exit(&sata_hba_inst->satahba_mutex);
14204 		mutex_enter(&sata_mutex);
14205 		sata_event_pending |= SATA_EVNT_MAIN;
14206 		mutex_exit(&sata_mutex);
14207 	}
14208 }
14209 
14210 
14211 /*
14212  * Device Target Node Cleanup Event processing.
14213  * If the target node associated with a sata port device is in
14214  * DEVI_DEVICE_REMOVED state, an attempt is made to remove it.
14215  * If the target node cannot be removed, the event flag is left intact,
14216  * so that event daemon may re-run this function later.
14217  *
14218  * This function cannot be called in interrupt context (it may sleep).
14219  *
14220  * NOTE: Processes cport events only, not port multiplier ports.
14221  */
14222 static void
14223 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
14224     sata_address_t *saddr)
14225 {
14226 	sata_cport_info_t *cportinfo;
14227 	dev_info_t *tdip;
14228 
14229 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14230 	    "Processing port %d device target node cleanup", saddr->cport);
14231 
14232 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14233 
14234 	/*
14235 	 * Check if there is target node for that device and it is in the
14236 	 * DEVI_DEVICE_REMOVED state. If so, release it.
14237 	 */
14238 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
14239 	if (tdip != NULL) {
14240 		/*
14241 		 * target node exists - check if it is target node of
14242 		 * a removed device.
14243 		 */
14244 		if (sata_check_device_removed(tdip) == B_TRUE) {
14245 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14246 			    "sata_process_target_node_cleanup: "
14247 			    "old device target node exists!", NULL);
14248 			/*
14249 			 * Unconfigure and remove the target node
14250 			 */
14251 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) ==
14252 			    NDI_SUCCESS) {
14253 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14254 				    saddr->cport)->cport_mutex);
14255 				cportinfo->cport_event_flags &=
14256 				    ~SATA_EVNT_TARGET_NODE_CLEANUP;
14257 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14258 				    saddr->cport)->cport_mutex);
14259 				return;
14260 			}
14261 			/*
14262 			 * Event daemon will retry the cleanup later.
14263 			 */
14264 			mutex_enter(&sata_hba_inst->satahba_mutex);
14265 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14266 			mutex_exit(&sata_hba_inst->satahba_mutex);
14267 			mutex_enter(&sata_mutex);
14268 			sata_event_pending |= SATA_EVNT_MAIN;
14269 			mutex_exit(&sata_mutex);
14270 		}
14271 	} else {
14272 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14273 		    saddr->cport)->cport_mutex);
14274 		cportinfo->cport_event_flags &=
14275 		    ~SATA_EVNT_TARGET_NODE_CLEANUP;
14276 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14277 		    saddr->cport)->cport_mutex);
14278 	}
14279 }
14280 
14281 static void
14282 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr,
14283     int hint)
14284 {
14285 	char ap[MAXPATHLEN];
14286 	nvlist_t *ev_attr_list = NULL;
14287 	int err;
14288 
14289 	/* Allocate and build sysevent attribute list */
14290 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP);
14291 	if (err != 0) {
14292 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14293 		    "sata_gen_sysevent: "
14294 		    "cannot allocate memory for sysevent attributes\n"));
14295 		return;
14296 	}
14297 	/* Add hint attribute */
14298 	err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint));
14299 	if (err != 0) {
14300 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14301 		    "sata_gen_sysevent: "
14302 		    "failed to add DR_HINT attr for sysevent"));
14303 		nvlist_free(ev_attr_list);
14304 		return;
14305 	}
14306 	/*
14307 	 * Add AP attribute.
14308 	 * Get controller pathname and convert it into AP pathname by adding
14309 	 * a target number.
14310 	 */
14311 	(void) snprintf(ap, MAXPATHLEN, "/devices");
14312 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap));
14313 	(void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d",
14314 	    SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual));
14315 
14316 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap);
14317 	if (err != 0) {
14318 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14319 		    "sata_gen_sysevent: "
14320 		    "failed to add DR_AP_ID attr for sysevent"));
14321 		nvlist_free(ev_attr_list);
14322 		return;
14323 	}
14324 
14325 	/* Generate/log sysevent */
14326 	err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR,
14327 	    ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP);
14328 	if (err != DDI_SUCCESS) {
14329 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14330 		    "sata_gen_sysevent: "
14331 		    "cannot log sysevent, err code %x\n", err));
14332 	}
14333 
14334 	nvlist_free(ev_attr_list);
14335 }
14336 
14337 
14338 
14339 
14340 /*
14341  * Set DEVI_DEVICE_REMOVED state in the SATA device target node.
14342  */
14343 static void
14344 sata_set_device_removed(dev_info_t *tdip)
14345 {
14346 	int circ;
14347 
14348 	ASSERT(tdip != NULL);
14349 
14350 	ndi_devi_enter(tdip, &circ);
14351 	mutex_enter(&DEVI(tdip)->devi_lock);
14352 	DEVI_SET_DEVICE_REMOVED(tdip);
14353 	mutex_exit(&DEVI(tdip)->devi_lock);
14354 	ndi_devi_exit(tdip, circ);
14355 }
14356 
14357 
14358 /*
14359  * Set internal event instructing event daemon to try
14360  * to perform the target node cleanup.
14361  */
14362 static void
14363 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst, int cport)
14364 {
14365 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14366 	SATA_CPORT_EVENT_FLAGS(sata_hba_inst, cport) |=
14367 	    SATA_EVNT_TARGET_NODE_CLEANUP;
14368 	SATA_CPORT_INFO(sata_hba_inst, cport)->cport_tgtnode_clean = B_FALSE;
14369 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14370 	mutex_enter(&sata_hba_inst->satahba_mutex);
14371 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14372 	mutex_exit(&sata_hba_inst->satahba_mutex);
14373 	mutex_enter(&sata_mutex);
14374 	sata_event_pending |= SATA_EVNT_MAIN;
14375 	mutex_exit(&sata_mutex);
14376 }
14377 
14378 
14379 /*
14380  * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state,
14381  * i.e. check if the target node state indicates that it belongs to a removed
14382  * device.
14383  *
14384  * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state,
14385  * B_FALSE otherwise.
14386  *
14387  * NOTE: No port multiplier support.
14388  */
14389 static boolean_t
14390 sata_check_device_removed(dev_info_t *tdip)
14391 {
14392 	ASSERT(tdip != NULL);
14393 
14394 	if (DEVI_IS_DEVICE_REMOVED(tdip))
14395 		return (B_TRUE);
14396 	else
14397 		return (B_FALSE);
14398 }
14399