xref: /titanic_41/usr/src/uts/common/io/sata/impl/sata.c (revision f6485eec0b33b15e863ae352c5279913ed8e6d29)
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 /*
67  * Global variable setting default maximum queue depth (NCQ or TCQ)
68  * Note:minimum queue depth is 1
69  */
70 int sata_max_queue_depth = SATA_MAX_QUEUE_DEPTH; /* max NCQ/TCQ queue depth */
71 
72 /*
73  * Currently used default NCQ/TCQ queue depth. It is set-up during the driver
74  * initialization, using value from sata_max_queue_depth
75  * It is adjusted to minimum supported by the controller and by the device,
76  * if queueing is enabled.
77  */
78 static	int sata_current_max_qdepth;
79 
80 #ifdef SATA_DEBUG
81 
82 #define	SATA_LOG_D(args)	sata_log args
83 uint64_t mbuf_count = 0;
84 uint64_t mbuffail_count = 0;
85 
86 sata_atapi_cmd_t sata_atapi_trace[64];
87 uint32_t sata_atapi_trace_index = 0;
88 int sata_atapi_trace_save = 1;
89 static	void sata_save_atapi_trace(sata_pkt_txlate_t *, int);
90 #define	SATAATAPITRACE(spx, count)	if (sata_atapi_trace_save) \
91     sata_save_atapi_trace(spx, count);
92 
93 #else
94 #define	SATA_LOG_D(arg)
95 #define	SATAATAPITRACE(spx, count)
96 #endif
97 
98 #if 0
99 static void
100 sata_test_atapi_packet_command(sata_hba_inst_t *, int);
101 #endif
102 #define	LEGACY_HWID_LEN	64	/* Model (40) + Serial (20) + pad */
103 
104 
105 /*
106  * SATA cb_ops functions
107  */
108 static 	int sata_hba_open(dev_t *, int, int, cred_t *);
109 static 	int sata_hba_close(dev_t, int, int, cred_t *);
110 static 	int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *,	int *);
111 
112 /*
113  * SCSA required entry points
114  */
115 static	int sata_scsi_tgt_init(dev_info_t *, dev_info_t *,
116     scsi_hba_tran_t *, struct scsi_device *);
117 static	int sata_scsi_tgt_probe(struct scsi_device *,
118     int (*callback)(void));
119 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *,
120     scsi_hba_tran_t *, struct scsi_device *);
121 static 	int sata_scsi_start(struct scsi_address *, struct scsi_pkt *);
122 static 	int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *);
123 static 	int sata_scsi_reset(struct scsi_address *, int);
124 static 	int sata_scsi_getcap(struct scsi_address *, char *, int);
125 static 	int sata_scsi_setcap(struct scsi_address *, char *, int, int);
126 static 	struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *,
127     struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t),
128     caddr_t);
129 static 	void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *);
130 static 	void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *);
131 static 	void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *);
132 
133 /*
134  * SATA HBA interface functions are defined in sata_hba.h header file
135  */
136 
137 /* Event processing functions */
138 static	void sata_event_daemon(void *);
139 static	void sata_event_thread_control(int);
140 static	void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst);
141 static	void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *);
142 static	void sata_process_port_failed_event(sata_hba_inst_t *,
143     sata_address_t *);
144 static	void sata_process_port_link_events(sata_hba_inst_t *,
145     sata_address_t *);
146 static	void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *);
147 static	void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *);
148 static	void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *);
149 static	void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *);
150 static	void sata_process_target_node_cleanup(sata_hba_inst_t *,
151     sata_address_t *);
152 
153 
154 /*
155  * Local translation functions
156  */
157 static	int sata_txlt_inquiry(sata_pkt_txlate_t *);
158 static	int sata_txlt_test_unit_ready(sata_pkt_txlate_t *);
159 static	int sata_txlt_start_stop_unit(sata_pkt_txlate_t *);
160 static	int sata_txlt_read_capacity(sata_pkt_txlate_t *);
161 static	int sata_txlt_request_sense(sata_pkt_txlate_t *);
162 static 	int sata_txlt_read(sata_pkt_txlate_t *);
163 static 	int sata_txlt_write(sata_pkt_txlate_t *);
164 static 	int sata_txlt_log_sense(sata_pkt_txlate_t *);
165 static 	int sata_txlt_log_select(sata_pkt_txlate_t *);
166 static 	int sata_txlt_mode_sense(sata_pkt_txlate_t *);
167 static 	int sata_txlt_mode_select(sata_pkt_txlate_t *);
168 static 	int sata_txlt_synchronize_cache(sata_pkt_txlate_t *);
169 static 	int sata_txlt_write_buffer(sata_pkt_txlate_t *);
170 static 	int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *);
171 
172 static 	int sata_hba_start(sata_pkt_txlate_t *, int *);
173 static	int sata_txlt_invalid_command(sata_pkt_txlate_t *);
174 static	int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *);
175 static 	void sata_txlt_rw_completion(sata_pkt_t *);
176 static 	void sata_txlt_nodata_cmd_completion(sata_pkt_t *);
177 static 	void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *);
178 static 	int sata_emul_rw_completion(sata_pkt_txlate_t *);
179 static 	struct scsi_extended_sense *sata_immediate_error_response(
180     sata_pkt_txlate_t *, int);
181 static	struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *);
182 
183 static 	int sata_txlt_atapi(sata_pkt_txlate_t *);
184 static 	void sata_txlt_atapi_completion(sata_pkt_t *);
185 
186 /*
187  * Local functions for ioctl
188  */
189 static	int32_t sata_get_port_num(sata_hba_inst_t *,  struct devctl_iocdata *);
190 static	void sata_cfgadm_state(sata_hba_inst_t *, int32_t,
191     devctl_ap_state_t *);
192 static	dev_info_t *sata_get_target_dip(dev_info_t *, int32_t);
193 static	dev_info_t *sata_devt_to_devinfo(dev_t);
194 
195 /*
196  * Local functions
197  */
198 static 	void sata_remove_hba_instance(dev_info_t *);
199 static 	int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *);
200 static 	void sata_probe_ports(sata_hba_inst_t *);
201 static 	int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int);
202 static 	int sata_add_device(dev_info_t *, sata_hba_inst_t *, int cport,
203     int pmport);
204 static 	dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *,
205     sata_address_t *);
206 static 	int sata_validate_scsi_address(sata_hba_inst_t *,
207     struct scsi_address *, sata_device_t *);
208 static 	int sata_validate_sata_address(sata_hba_inst_t *, int, int, int);
209 static	sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t));
210 static	void sata_pkt_free(sata_pkt_txlate_t *);
211 static	int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t),
212     caddr_t, ddi_dma_attr_t *);
213 static	int sata_probe_device(sata_hba_inst_t *, sata_device_t *);
214 static	sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *,
215     sata_device_t *);
216 static 	int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *);
217 static	struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int);
218 static 	void sata_free_local_buffer(sata_pkt_txlate_t *);
219 static 	uint64_t sata_check_capacity(sata_drive_info_t *);
220 void 	sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *,
221     ddi_dma_attr_t *);
222 static 	int sata_fetch_device_identify_data(sata_hba_inst_t *,
223     sata_drive_info_t *);
224 static	void sata_update_port_info(sata_hba_inst_t *, sata_device_t *);
225 static	void sata_update_port_scr(sata_port_scr_t *, sata_device_t *);
226 static	int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *);
227 static	int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int);
228 static	int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int);
229 static	int sata_set_drive_features(sata_hba_inst_t *,
230     sata_drive_info_t *, int flag);
231 static	void sata_init_write_cache_mode(sata_drive_info_t *sdinfo);
232 static	int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *);
233 static	void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *,
234     uint8_t *);
235 static	int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *,
236     struct scsi_inquiry *);
237 static	int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *);
238 static	int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *);
239 static	int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *);
240 static	int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *);
241 static	int sata_mode_select_page_8(sata_pkt_txlate_t *,
242     struct mode_cache_scsi3 *, int, int *, int *, int *);
243 static	int sata_mode_select_page_1c(sata_pkt_txlate_t *,
244     struct mode_info_excpt_page *, int, int *, int *, int *);
245 static	int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *);
246 static	int sata_mode_select_page_30(sata_pkt_txlate_t *,
247     struct mode_acoustic_management *, int, int *, int *, int *);
248 
249 static	int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *);
250 static	int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *,
251     sata_hba_inst_t *);
252 static	int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *,
253     sata_hba_inst_t *);
254 static	int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *,
255     sata_hba_inst_t *);
256 static	void sata_save_drive_settings(sata_drive_info_t *);
257 static	void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *);
258 static	void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...);
259 static	int sata_fetch_smart_return_status(sata_hba_inst_t *,
260     sata_drive_info_t *);
261 static	int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *,
262     struct smart_data *);
263 static	int sata_smart_selftest_log(sata_hba_inst_t *,
264     sata_drive_info_t *,
265     struct smart_selftest_log *);
266 static	int sata_ext_smart_selftest_read_log(sata_hba_inst_t *,
267     sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t);
268 static	int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *,
269     uint8_t *, uint8_t, uint8_t);
270 static	int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *,
271     struct read_log_ext_directory *);
272 static	void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int);
273 static	void sata_xlate_errors(sata_pkt_txlate_t *);
274 static	void sata_decode_device_error(sata_pkt_txlate_t *,
275     struct scsi_extended_sense *);
276 static	void sata_set_device_removed(dev_info_t *);
277 static	boolean_t sata_check_device_removed(dev_info_t *);
278 static	void sata_set_target_node_cleanup(sata_hba_inst_t *, int cport);
279 static	int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *,
280     sata_drive_info_t *);
281 static	int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *,
282     sata_drive_info_t *);
283 static	void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *);
284 static	void sata_fixed_sense_data_preset(struct scsi_extended_sense *);
285 static  void sata_target_devid_register(dev_info_t *, sata_drive_info_t *);
286 static  int sata_check_modser(char *, int);
287 
288 
289 
290 /*
291  * SATA Framework will ignore SATA HBA driver cb_ops structure and
292  * register following one with SCSA framework.
293  * Open & close are provided, so scsi framework will not use its own
294  */
295 static struct cb_ops sata_cb_ops = {
296 	sata_hba_open,			/* open */
297 	sata_hba_close,			/* close */
298 	nodev,				/* strategy */
299 	nodev,				/* print */
300 	nodev,				/* dump */
301 	nodev,				/* read */
302 	nodev,				/* write */
303 	sata_hba_ioctl,			/* ioctl */
304 	nodev,				/* devmap */
305 	nodev,				/* mmap */
306 	nodev,				/* segmap */
307 	nochpoll,			/* chpoll */
308 	ddi_prop_op,			/* cb_prop_op */
309 	0,				/* streamtab */
310 	D_NEW | D_MP,			/* cb_flag */
311 	CB_REV,				/* rev */
312 	nodev,				/* aread */
313 	nodev				/* awrite */
314 };
315 
316 
317 extern struct mod_ops mod_miscops;
318 extern uchar_t	scsi_cdb_size[];
319 
320 static struct modlmisc modlmisc = {
321 	&mod_miscops,			/* Type of module */
322 	"SATA Module v%I%"		/* module name */
323 };
324 
325 
326 static struct modlinkage modlinkage = {
327 	MODREV_1,
328 	(void *)&modlmisc,
329 	NULL
330 };
331 
332 /*
333  * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero,
334  * i.e. when scsi_pkt has not timeout specified.
335  */
336 static int sata_default_pkt_time = 60;	/* 60 seconds */
337 
338 /*
339  * Intermediate buffer device access attributes - they are required,
340  * but not necessarily used.
341  */
342 static ddi_device_acc_attr_t sata_acc_attr = {
343 	DDI_DEVICE_ATTR_V0,
344 	DDI_STRUCTURE_LE_ACC,
345 	DDI_STRICTORDER_ACC
346 };
347 
348 
349 /*
350  * Mutexes protecting structures in multithreaded operations.
351  * Because events are relatively rare, a single global mutex protecting
352  * data structures should be sufficient. To increase performance, add
353  * separate mutex per each sata port and use global mutex only to protect
354  * common data structures.
355  */
356 static	kmutex_t sata_mutex;		/* protects sata_hba_list */
357 static	kmutex_t sata_log_mutex;	/* protects log */
358 
359 static 	char sata_log_buf[256];
360 
361 /* Default write cache setting for SATA hard disks */
362 int	sata_write_cache = 1;		/* enabled */
363 
364 /* Default write cache setting for SATA ATAPI CD/DVD */
365 int 	sata_atapicdvd_write_cache = 1; /* enabled */
366 
367 /*
368  * Linked list of HBA instances
369  */
370 static 	sata_hba_inst_t *sata_hba_list = NULL;
371 static 	sata_hba_inst_t *sata_hba_list_tail = NULL;
372 /*
373  * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran
374  * structure and in sata soft state.
375  */
376 
377 /*
378  * Event daemon related variables
379  */
380 static 	kmutex_t sata_event_mutex;
381 static 	kcondvar_t sata_event_cv;
382 static 	kthread_t *sata_event_thread = NULL;
383 static 	int sata_event_thread_terminate = 0;
384 static 	int sata_event_pending = 0;
385 static 	int sata_event_thread_active = 0;
386 extern 	pri_t minclsyspri;
387 
388 /*
389  * NCQ error recovery command
390  */
391 static const sata_cmd_t sata_rle_cmd = {
392 	SATA_CMD_REV,
393 	NULL,
394 	{
395 		SATA_DIR_READ
396 	},
397 	ATA_ADDR_LBA48,
398 	0,
399 	0,
400 	0,
401 	0,
402 	0,
403 	1,
404 	READ_LOG_EXT_NCQ_ERROR_RECOVERY,
405 	0,
406 	0,
407 	0,
408 	SATAC_READ_LOG_EXT,
409 	0,
410 	0,
411 	0,
412 };
413 
414 /*
415  * ATAPI error recovery CDB
416  */
417 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = {
418 	SCMD_REQUEST_SENSE,
419 	0,			/* Only fixed RQ format is supported */
420 	0,
421 	0,
422 	SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */
423 	0
424 };
425 
426 
427 /* Warlock directives */
428 
429 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran))
430 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device))
431 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops))
432 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense))
433 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status))
434 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr))
435 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t))
436 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state))
437 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state))
438 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list))
439 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list))
440 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next))
441 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev))
442 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \
443     sata_hba_inst::satahba_scsi_tran))
444 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran))
445 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip))
446 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached))
447 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port))
448 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex,
449     sata_hba_inst::satahba_event_flags))
450 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
451     sata_cport_info::cport_devp))
452 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp))
453 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr))
454 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
455     sata_cport_info::cport_dev_type))
456 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type))
457 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
458     sata_cport_info::cport_state))
459 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state))
460 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type))
461 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive))
462 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port))
463 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports))
464 #ifdef SATA_DEBUG
465 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count))
466 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count))
467 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace))
468 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index))
469 #endif
470 
471 /* End of warlock directives */
472 
473 /* ************** loadable module configuration functions ************** */
474 
475 int
476 _init()
477 {
478 	int rval;
479 
480 	mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL);
481 	mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL);
482 	mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL);
483 	cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL);
484 	if ((rval = mod_install(&modlinkage)) != 0) {
485 #ifdef SATA_DEBUG
486 		cmn_err(CE_WARN, "sata: _init: mod_install failed\n");
487 #endif
488 		mutex_destroy(&sata_log_mutex);
489 		cv_destroy(&sata_event_cv);
490 		mutex_destroy(&sata_event_mutex);
491 		mutex_destroy(&sata_mutex);
492 	}
493 	return (rval);
494 }
495 
496 int
497 _fini()
498 {
499 	int rval;
500 
501 	if ((rval = mod_remove(&modlinkage)) != 0)
502 		return (rval);
503 
504 	mutex_destroy(&sata_log_mutex);
505 	cv_destroy(&sata_event_cv);
506 	mutex_destroy(&sata_event_mutex);
507 	mutex_destroy(&sata_mutex);
508 	return (rval);
509 }
510 
511 int
512 _info(struct modinfo *modinfop)
513 {
514 	return (mod_info(&modlinkage, modinfop));
515 }
516 
517 
518 
519 /* ********************* SATA HBA entry points ********************* */
520 
521 
522 /*
523  * Called by SATA HBA from _init().
524  * Registers HBA driver instance/sata framework pair with scsi framework, by
525  * calling scsi_hba_init().
526  *
527  * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used
528  * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver
529  * cb_ops pointer in SATA HBA driver dev_ops structure.
530  * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors.
531  *
532  * Return status of the scsi_hba_init() is returned to a calling SATA HBA
533  * driver.
534  */
535 int
536 sata_hba_init(struct modlinkage *modlp)
537 {
538 	int rval;
539 	struct dev_ops *hba_ops;
540 
541 	SATADBG1(SATA_DBG_HBA_IF, NULL,
542 	    "sata_hba_init: name %s \n",
543 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
544 	/*
545 	 * Fill-up cb_ops and dev_ops when necessary
546 	 */
547 	hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
548 	/*
549 	 * Provide pointer to SATA dev_ops
550 	 */
551 	hba_ops->devo_cb_ops = &sata_cb_ops;
552 
553 	/*
554 	 * Register SATA HBA with SCSI framework
555 	 */
556 	if ((rval = scsi_hba_init(modlp)) != 0) {
557 		SATADBG1(SATA_DBG_HBA_IF, NULL,
558 		    "sata_hba_init: scsi hba init failed\n", NULL);
559 		return (rval);
560 	}
561 
562 	return (0);
563 }
564 
565 
566 /* HBA attach stages */
567 #define	HBA_ATTACH_STAGE_SATA_HBA_INST	1
568 #define	HBA_ATTACH_STAGE_SCSI_ATTACHED	2
569 #define	HBA_ATTACH_STAGE_SETUP		4
570 #define	HBA_ATTACH_STAGE_LINKED		8
571 
572 
573 /*
574  *
575  * Called from SATA HBA driver's attach routine to attach an instance of
576  * the HBA.
577  *
578  * For DDI_ATTACH command:
579  * sata_hba_inst structure is allocated here and initialized with pointers to
580  * SATA framework implementation of required scsi tran functions.
581  * The scsi_tran's tran_hba_private field is used by SATA Framework to point
582  * to the soft structure (sata_hba_inst) allocated by SATA framework for
583  * SATA HBA instance related data.
584  * The scsi_tran's tran_hba_private field is used by SATA framework to
585  * store a pointer to per-HBA-instance of sata_hba_inst structure.
586  * The sata_hba_inst structure is cross-linked to scsi tran structure.
587  * Among other info, a pointer to sata_hba_tran structure is stored in
588  * sata_hba_inst. The sata_hba_inst structures for different HBA instances are
589  * linked together into the list, pointed to by sata_hba_list.
590  * On the first HBA instance attach the sata event thread is initialized.
591  * Attachment points are created for all SATA ports of the HBA being attached.
592  * All HBA instance's SATA ports are probed and type of plugged devices is
593  * determined. For each device of a supported type, a target node is created.
594  *
595  * DDI_SUCCESS is returned when attachment process is successful,
596  * DDI_FAILURE is returned otherwise.
597  *
598  * For DDI_RESUME command:
599  * Not implemented at this time (postponed until phase 2 of the development).
600  */
601 int
602 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran,
603     ddi_attach_cmd_t cmd)
604 {
605 	sata_hba_inst_t	*sata_hba_inst;
606 	scsi_hba_tran_t *scsi_tran = NULL;
607 	int hba_attach_state = 0;
608 	char taskq_name[MAXPATHLEN];
609 
610 	SATADBG3(SATA_DBG_HBA_IF, NULL,
611 	    "sata_hba_attach: node %s (%s%d)\n",
612 	    ddi_node_name(dip), ddi_driver_name(dip),
613 	    ddi_get_instance(dip));
614 
615 	if (cmd == DDI_RESUME) {
616 		/*
617 		 * Postponed until phase 2 of the development
618 		 */
619 		return (DDI_FAILURE);
620 	}
621 
622 	if (cmd != DDI_ATTACH) {
623 		return (DDI_FAILURE);
624 	}
625 
626 	/* cmd == DDI_ATTACH */
627 
628 	if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) {
629 		SATA_LOG_D((NULL, CE_WARN,
630 		    "sata_hba_attach: invalid sata_hba_tran"));
631 		return (DDI_FAILURE);
632 	}
633 	/*
634 	 * Allocate and initialize SCSI tran structure.
635 	 * SATA copy of tran_bus_config is provided to create port nodes.
636 	 */
637 	scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
638 	if (scsi_tran == NULL)
639 		return (DDI_FAILURE);
640 	/*
641 	 * Allocate soft structure for SATA HBA instance.
642 	 * There is a separate softstate for each HBA instance.
643 	 */
644 	sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP);
645 	ASSERT(sata_hba_inst != NULL); /* this should not fail */
646 	mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL);
647 	hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST;
648 
649 	/*
650 	 * scsi_trans's tran_hba_private is used by SATA Framework to point to
651 	 * soft structure allocated by SATA framework for
652 	 * SATA HBA instance related data.
653 	 */
654 	scsi_tran->tran_hba_private	= sata_hba_inst;
655 	scsi_tran->tran_tgt_private	= NULL;
656 
657 	scsi_tran->tran_tgt_init	= sata_scsi_tgt_init;
658 	scsi_tran->tran_tgt_probe	= sata_scsi_tgt_probe;
659 	scsi_tran->tran_tgt_free	= sata_scsi_tgt_free;
660 
661 	scsi_tran->tran_start		= sata_scsi_start;
662 	scsi_tran->tran_reset		= sata_scsi_reset;
663 	scsi_tran->tran_abort		= sata_scsi_abort;
664 	scsi_tran->tran_getcap		= sata_scsi_getcap;
665 	scsi_tran->tran_setcap		= sata_scsi_setcap;
666 	scsi_tran->tran_init_pkt	= sata_scsi_init_pkt;
667 	scsi_tran->tran_destroy_pkt	= sata_scsi_destroy_pkt;
668 
669 	scsi_tran->tran_dmafree		= sata_scsi_dmafree;
670 	scsi_tran->tran_sync_pkt	= sata_scsi_sync_pkt;
671 
672 	scsi_tran->tran_reset_notify	= NULL;
673 	scsi_tran->tran_get_bus_addr	= NULL;
674 	scsi_tran->tran_quiesce		= NULL;
675 	scsi_tran->tran_unquiesce	= NULL;
676 	scsi_tran->tran_bus_reset	= NULL;
677 
678 	if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr,
679 	    scsi_tran, 0) != DDI_SUCCESS) {
680 #ifdef SATA_DEBUG
681 		cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed",
682 		    ddi_driver_name(dip), ddi_get_instance(dip));
683 #endif
684 		goto fail;
685 	}
686 	hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED;
687 
688 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) {
689 		if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
690 		    "sata", 1) != DDI_PROP_SUCCESS) {
691 			SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: "
692 			    "failed to create hba sata prop"));
693 			goto fail;
694 		}
695 	}
696 
697 	/*
698 	 * Save pointers in hba instance soft state.
699 	 */
700 	sata_hba_inst->satahba_scsi_tran = scsi_tran;
701 	sata_hba_inst->satahba_tran = sata_tran;
702 	sata_hba_inst->satahba_dip = dip;
703 
704 	/*
705 	 * Create a task queue to handle emulated commands completion
706 	 * Use node name, dash, instance number as the queue name.
707 	 */
708 	taskq_name[0] = '\0';
709 	(void) strlcat(taskq_name, DEVI(dip)->devi_node_name,
710 	    sizeof (taskq_name));
711 	(void) snprintf(taskq_name + strlen(taskq_name),
712 	    sizeof (taskq_name) - strlen(taskq_name),
713 	    "-%d", DEVI(dip)->devi_instance);
714 	sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1,
715 	    minclsyspri, 1, sata_tran->sata_tran_hba_num_cports,
716 	    TASKQ_DYNAMIC);
717 
718 	hba_attach_state |= HBA_ATTACH_STAGE_SETUP;
719 
720 	/*
721 	 * Create events thread if not created yet.
722 	 */
723 	sata_event_thread_control(1);
724 
725 	/*
726 	 * Link this hba instance into the list.
727 	 */
728 	mutex_enter(&sata_mutex);
729 
730 	if (sata_hba_list == NULL) {
731 		/*
732 		 * The first instance of HBA is attached.
733 		 * Set current/active default maximum NCQ/TCQ queue depth for
734 		 * all SATA devices. It is done here and now, to eliminate the
735 		 * possibility of the dynamic, programatic modification of the
736 		 * queue depth via global (and public) sata_max_queue_depth
737 		 * variable (this would require special handling in HBA drivers)
738 		 */
739 		sata_current_max_qdepth = sata_max_queue_depth;
740 		if (sata_current_max_qdepth > 32)
741 			sata_current_max_qdepth = 32;
742 		else if (sata_current_max_qdepth < 1)
743 			sata_current_max_qdepth = 1;
744 	}
745 
746 	sata_hba_inst->satahba_next = NULL;
747 	sata_hba_inst->satahba_prev = sata_hba_list_tail;
748 	if (sata_hba_list == NULL) {
749 		sata_hba_list = sata_hba_inst;
750 	}
751 	if (sata_hba_list_tail != NULL) {
752 		sata_hba_list_tail->satahba_next = sata_hba_inst;
753 	}
754 	sata_hba_list_tail = sata_hba_inst;
755 	mutex_exit(&sata_mutex);
756 	hba_attach_state |= HBA_ATTACH_STAGE_LINKED;
757 
758 	/*
759 	 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl
760 	 * SATA HBA driver should not use its own open/close entry points.
761 	 *
762 	 * Make sure that instance number doesn't overflow
763 	 * when forming minor numbers.
764 	 */
765 	ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT));
766 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
767 	    INST2DEVCTL(ddi_get_instance(dip)),
768 	    DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) {
769 #ifdef SATA_DEBUG
770 		cmn_err(CE_WARN, "sata_hba_attach: "
771 		    "cannot create devctl minor node");
772 #endif
773 		goto fail;
774 	}
775 
776 
777 	/*
778 	 * Set-up kstats here, if necessary.
779 	 * (postponed until phase 2 of the development).
780 	 */
781 
782 
783 	/*
784 	 * Probe controller ports. This operation will describe a current
785 	 * controller/port/multipliers/device configuration and will create
786 	 * attachment points.
787 	 * We may end-up with just a controller with no devices attached.
788 	 * For the ports with a supported device attached, device target nodes
789 	 * are created and devices are initialized.
790 	 */
791 	sata_probe_ports(sata_hba_inst);
792 
793 	sata_hba_inst->satahba_attached = 1;
794 	return (DDI_SUCCESS);
795 
796 fail:
797 	if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) {
798 		(void) sata_remove_hba_instance(dip);
799 		if (sata_hba_list == NULL)
800 			sata_event_thread_control(0);
801 	}
802 
803 	if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) {
804 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
805 		taskq_destroy(sata_hba_inst->satahba_taskq);
806 	}
807 
808 	if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED)
809 		(void) scsi_hba_detach(dip);
810 
811 	if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) {
812 		mutex_destroy(&sata_hba_inst->satahba_mutex);
813 		kmem_free((void *)sata_hba_inst,
814 		    sizeof (struct sata_hba_inst));
815 		scsi_hba_tran_free(scsi_tran);
816 	}
817 
818 	sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed",
819 	    ddi_driver_name(dip), ddi_get_instance(dip));
820 
821 	return (DDI_FAILURE);
822 }
823 
824 
825 /*
826  * Called by SATA HBA from to detach an instance of the driver.
827  *
828  * For DDI_DETACH command:
829  * Free local structures allocated for SATA HBA instance during
830  * sata_hba_attach processing.
831  *
832  * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise.
833  *
834  * For DDI_SUSPEND command:
835  * Not implemented at this time (postponed until phase 2 of the development)
836  * Returnd DDI_SUCCESS.
837  *
838  * When the last HBA instance is detached, the event daemon is terminated.
839  *
840  * NOTE: cport support only, no port multiplier support.
841  */
842 int
843 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
844 {
845 	dev_info_t	*tdip;
846 	sata_hba_inst_t	*sata_hba_inst;
847 	scsi_hba_tran_t *scsi_hba_tran;
848 	sata_cport_info_t *cportinfo;
849 	sata_drive_info_t *sdinfo;
850 	int ncport;
851 
852 	SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n",
853 	    ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip));
854 
855 	switch (cmd) {
856 	case DDI_DETACH:
857 
858 		if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
859 			return (DDI_FAILURE);
860 
861 		sata_hba_inst = scsi_hba_tran->tran_hba_private;
862 		if (sata_hba_inst == NULL)
863 			return (DDI_FAILURE);
864 
865 		if (scsi_hba_detach(dip) == DDI_FAILURE) {
866 			sata_hba_inst->satahba_attached = 1;
867 			return (DDI_FAILURE);
868 		}
869 
870 		/*
871 		 * Free all target nodes - at this point
872 		 * devices should be at least offlined
873 		 * otherwise scsi_hba_detach() should not be called.
874 		 */
875 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
876 		    ncport++) {
877 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
878 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
879 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
880 				if (sdinfo != NULL) {
881 					tdip = sata_get_target_dip(dip,
882 					    ncport);
883 					if (tdip != NULL) {
884 						if (ndi_devi_offline(tdip,
885 						    NDI_DEVI_REMOVE) !=
886 						    NDI_SUCCESS) {
887 							SATA_LOG_D((
888 							    sata_hba_inst,
889 							    CE_WARN,
890 							    "sata_hba_detach: "
891 							    "Target node not "
892 							    "removed !"));
893 							return (DDI_FAILURE);
894 						}
895 					}
896 				}
897 			}
898 		}
899 		/*
900 		 * Disable sata event daemon processing for this HBA
901 		 */
902 		sata_hba_inst->satahba_attached = 0;
903 
904 		/*
905 		 * Remove event daemon thread, if it is last HBA instance.
906 		 */
907 
908 		mutex_enter(&sata_mutex);
909 		if (sata_hba_list->satahba_next == NULL) {
910 			mutex_exit(&sata_mutex);
911 			sata_event_thread_control(0);
912 			mutex_enter(&sata_mutex);
913 		}
914 		mutex_exit(&sata_mutex);
915 
916 		/* Remove this HBA instance from the HBA list */
917 		sata_remove_hba_instance(dip);
918 
919 		/*
920 		 * At this point there should be no target nodes attached.
921 		 * Detach and destroy device and port info structures.
922 		 */
923 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
924 		    ncport++) {
925 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
926 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
927 				sdinfo =
928 				    cportinfo->cport_devp.cport_sata_drive;
929 				if (sdinfo != NULL) {
930 					/* Release device structure */
931 					kmem_free(sdinfo,
932 					    sizeof (sata_drive_info_t));
933 				}
934 				/* Release cport info */
935 				mutex_destroy(&cportinfo->cport_mutex);
936 				kmem_free(cportinfo,
937 				    sizeof (sata_cport_info_t));
938 			}
939 		}
940 
941 		scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran);
942 
943 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
944 
945 		taskq_destroy(sata_hba_inst->satahba_taskq);
946 
947 		mutex_destroy(&sata_hba_inst->satahba_mutex);
948 		kmem_free((void *)sata_hba_inst,
949 		    sizeof (struct sata_hba_inst));
950 
951 		return (DDI_SUCCESS);
952 
953 	case DDI_SUSPEND:
954 		/*
955 		 * Postponed until phase 2
956 		 */
957 		return (DDI_FAILURE);
958 
959 	default:
960 		return (DDI_FAILURE);
961 	}
962 }
963 
964 
965 /*
966  * Called by an HBA drive from _fini() routine.
967  * Unregisters SATA HBA instance/SATA framework pair from the scsi framework.
968  */
969 void
970 sata_hba_fini(struct modlinkage *modlp)
971 {
972 	SATADBG1(SATA_DBG_HBA_IF, NULL,
973 	    "sata_hba_fini: name %s\n",
974 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
975 
976 	scsi_hba_fini(modlp);
977 }
978 
979 
980 /*
981  * Default open and close routine for sata_hba framework.
982  *
983  */
984 /*
985  * Open devctl node.
986  *
987  * Returns:
988  * 0 if node was open successfully, error code otherwise.
989  *
990  *
991  */
992 
993 static int
994 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
995 {
996 #ifndef __lock_lint
997 	_NOTE(ARGUNUSED(credp))
998 #endif
999 	int rv = 0;
1000 	dev_info_t *dip;
1001 	scsi_hba_tran_t *scsi_hba_tran;
1002 	sata_hba_inst_t	*sata_hba_inst;
1003 
1004 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL);
1005 
1006 	if (otyp != OTYP_CHR)
1007 		return (EINVAL);
1008 
1009 	dip = sata_devt_to_devinfo(*devp);
1010 	if (dip == NULL)
1011 		return (ENXIO);
1012 
1013 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1014 		return (ENXIO);
1015 
1016 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1017 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1018 		return (ENXIO);
1019 
1020 	mutex_enter(&sata_mutex);
1021 	if (flags & FEXCL) {
1022 		if (sata_hba_inst->satahba_open_flag != 0) {
1023 			rv = EBUSY;
1024 		} else {
1025 			sata_hba_inst->satahba_open_flag =
1026 			    SATA_DEVCTL_EXOPENED;
1027 		}
1028 	} else {
1029 		if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) {
1030 			rv = EBUSY;
1031 		} else {
1032 			sata_hba_inst->satahba_open_flag =
1033 			    SATA_DEVCTL_SOPENED;
1034 		}
1035 	}
1036 	mutex_exit(&sata_mutex);
1037 
1038 	return (rv);
1039 }
1040 
1041 
1042 /*
1043  * Close devctl node.
1044  * Returns:
1045  * 0 if node was closed successfully, error code otherwise.
1046  *
1047  */
1048 
1049 static int
1050 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
1051 {
1052 #ifndef __lock_lint
1053 	_NOTE(ARGUNUSED(credp))
1054 	_NOTE(ARGUNUSED(flag))
1055 #endif
1056 	dev_info_t *dip;
1057 	scsi_hba_tran_t *scsi_hba_tran;
1058 	sata_hba_inst_t	*sata_hba_inst;
1059 
1060 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL);
1061 
1062 	if (otyp != OTYP_CHR)
1063 		return (EINVAL);
1064 
1065 	dip = sata_devt_to_devinfo(dev);
1066 	if (dip == NULL)
1067 		return (ENXIO);
1068 
1069 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1070 		return (ENXIO);
1071 
1072 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1073 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1074 		return (ENXIO);
1075 
1076 	mutex_enter(&sata_mutex);
1077 	sata_hba_inst->satahba_open_flag = 0;
1078 	mutex_exit(&sata_mutex);
1079 	return (0);
1080 }
1081 
1082 
1083 
1084 /*
1085  * Standard IOCTL commands for SATA hotplugging.
1086  * Implemented DEVCTL_AP commands:
1087  * DEVCTL_AP_CONNECT
1088  * DEVCTL_AP_DISCONNECT
1089  * DEVCTL_AP_CONFIGURE
1090  * DEVCTL_UNCONFIGURE
1091  * DEVCTL_AP_CONTROL
1092  *
1093  * Commands passed to default ndi ioctl handler:
1094  * DEVCTL_DEVICE_GETSTATE
1095  * DEVCTL_DEVICE_ONLINE
1096  * DEVCTL_DEVICE_OFFLINE
1097  * DEVCTL_DEVICE_REMOVE
1098  * DEVCTL_DEVICE_INSERT
1099  * DEVCTL_BUS_GETSTATE
1100  *
1101  * All other cmds are passed to HBA if it provide ioctl handler, or failed
1102  * if not.
1103  *
1104  * Returns:
1105  * 0 if successful,
1106  * error code if operation failed.
1107  *
1108  * NOTE: Port Multiplier is not supported.
1109  *
1110  */
1111 
1112 static int
1113 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1114     int *rvalp)
1115 {
1116 #ifndef __lock_lint
1117 	_NOTE(ARGUNUSED(credp))
1118 	_NOTE(ARGUNUSED(rvalp))
1119 #endif
1120 	int rv = 0;
1121 	int32_t	comp_port = -1;
1122 	dev_info_t *dip, *tdip;
1123 	devctl_ap_state_t ap_state;
1124 	struct devctl_iocdata *dcp = NULL;
1125 	scsi_hba_tran_t *scsi_hba_tran;
1126 	sata_hba_inst_t *sata_hba_inst;
1127 	sata_device_t sata_device;
1128 	sata_drive_info_t *sdinfo;
1129 	sata_cport_info_t *cportinfo;
1130 	int cport, pmport, qual;
1131 	int rval = SATA_SUCCESS;
1132 
1133 	dip = sata_devt_to_devinfo(dev);
1134 	if (dip == NULL)
1135 		return (ENXIO);
1136 
1137 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1138 		return (ENXIO);
1139 
1140 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1141 	if (sata_hba_inst == NULL)
1142 		return (ENXIO);
1143 
1144 	if (sata_hba_inst->satahba_tran == NULL)
1145 		return (ENXIO);
1146 
1147 	switch (cmd) {
1148 
1149 	case DEVCTL_DEVICE_GETSTATE:
1150 	case DEVCTL_DEVICE_ONLINE:
1151 	case DEVCTL_DEVICE_OFFLINE:
1152 	case DEVCTL_DEVICE_REMOVE:
1153 	case DEVCTL_BUS_GETSTATE:
1154 		/*
1155 		 * There may be more cases that we want to pass to default
1156 		 * handler rather than fail them.
1157 		 */
1158 		return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
1159 	}
1160 
1161 	/* read devctl ioctl data */
1162 	if (cmd != DEVCTL_AP_CONTROL) {
1163 		if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1164 			return (EFAULT);
1165 
1166 		if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) ==
1167 		    -1) {
1168 			if (dcp)
1169 				ndi_dc_freehdl(dcp);
1170 			return (EINVAL);
1171 		}
1172 
1173 		cport = SCSI_TO_SATA_CPORT(comp_port);
1174 		pmport = SCSI_TO_SATA_PMPORT(comp_port);
1175 		/* Only cport is considered now, i.e. SATA_ADDR_CPORT */
1176 		qual = SATA_ADDR_CPORT;
1177 		if (sata_validate_sata_address(sata_hba_inst, cport, pmport,
1178 		    qual) != 0) {
1179 			ndi_dc_freehdl(dcp);
1180 			return (EINVAL);
1181 		}
1182 
1183 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1184 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1185 		    cport_mutex);
1186 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1187 			/*
1188 			 * Cannot process ioctl request now. Come back later.
1189 			 */
1190 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1191 			    cport_mutex);
1192 			ndi_dc_freehdl(dcp);
1193 			return (EBUSY);
1194 		}
1195 		/* Block event processing for this port */
1196 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1197 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1198 
1199 		sata_device.satadev_addr.cport = cport;
1200 		sata_device.satadev_addr.pmport = pmport;
1201 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
1202 		sata_device.satadev_rev = SATA_DEVICE_REV;
1203 	}
1204 
1205 	switch (cmd) {
1206 
1207 	case DEVCTL_AP_DISCONNECT:
1208 		/*
1209 		 * Normally, cfgadm sata plugin will try to offline
1210 		 * (unconfigure) device before this request. Nevertheless,
1211 		 * if a device is still configured, we need to
1212 		 * attempt to offline and unconfigure device first, and we will
1213 		 * deactivate the port regardless of the unconfigure
1214 		 * operation results.
1215 		 *
1216 		 * DEVCTL_AP_DISCONNECT invokes
1217 		 * sata_hba_inst->satahba_tran->
1218 		 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
1219 		 * If successful, the device structure (if any) attached
1220 		 * to a port is removed and state of the port marked
1221 		 * appropriately.
1222 		 * Failure of the port_deactivate may keep port in
1223 		 * the active state, or may fail the port.
1224 		 */
1225 
1226 		/* Check the current state of the port */
1227 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
1228 		    (dip, &sata_device);
1229 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1230 		    cport_mutex);
1231 		sata_update_port_info(sata_hba_inst, &sata_device);
1232 		if (rval != SATA_SUCCESS ||
1233 		    (sata_device.satadev_state & SATA_PSTATE_FAILED)) {
1234 			cportinfo->cport_state = SATA_PSTATE_FAILED;
1235 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1236 			    cport_mutex);
1237 			rv = EIO;
1238 			break;
1239 		}
1240 		/* Sanity check */
1241 		if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
1242 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1243 			    cport_mutex);
1244 			/* No physical port deactivation supported. */
1245 			break;
1246 		}
1247 
1248 		/*
1249 		 * set port's dev_state to not ready - this will disable
1250 		 * an access to an attached device.
1251 		 */
1252 		cportinfo->cport_state &= ~SATA_STATE_READY;
1253 
1254 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
1255 			sdinfo = cportinfo->cport_devp.cport_sata_drive;
1256 			ASSERT(sdinfo != NULL);
1257 			if ((sdinfo->satadrv_type &
1258 			    (SATA_VALID_DEV_TYPE))) {
1259 				/*
1260 				 * If a target node exists, try to offline
1261 				 * a device and remove target node.
1262 				 */
1263 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
1264 				    cport)->cport_mutex);
1265 				tdip = sata_get_target_dip(dip, comp_port);
1266 				if (tdip != NULL && ndi_devi_offline(tdip,
1267 				    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
1268 					/*
1269 					 * Problem
1270 					 * A target node remained
1271 					 * attached. This happens when
1272 					 * the file was open or a node
1273 					 * was waiting for resources.
1274 					 * Cannot do anything about it.
1275 					 */
1276 					SATA_LOG_D((sata_hba_inst, CE_WARN,
1277 					    "sata_hba_ioctl: "
1278 					    "disconnect: could not "
1279 					    "unconfigure device before "
1280 					    "disconnecting the SATA "
1281 					    "port %d", cport));
1282 
1283 					/*
1284 					 * Set DEVICE REMOVED state
1285 					 * in the target node. It
1286 					 * will prevent access to
1287 					 * the device even when a
1288 					 * new device is attached,
1289 					 * until the old target node
1290 					 * is released, removed and
1291 					 * recreated for a new
1292 					 * device.
1293 					 */
1294 					sata_set_device_removed(tdip);
1295 					/*
1296 					 * Instruct event daemon to
1297 					 * try the target node cleanup
1298 					 * later.
1299 					 */
1300 					sata_set_target_node_cleanup(
1301 					    sata_hba_inst, cport);
1302 				}
1303 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
1304 				    cport)->cport_mutex);
1305 				/*
1306 				 * Remove and release sata_drive_info
1307 				 * structure.
1308 				 */
1309 				if (SATA_CPORTINFO_DRV_INFO(cportinfo) !=
1310 				    NULL) {
1311 					SATA_CPORTINFO_DRV_INFO(cportinfo) =
1312 					    NULL;
1313 					(void) kmem_free((void *)sdinfo,
1314 					    sizeof (sata_drive_info_t));
1315 					cportinfo->cport_dev_type =
1316 					    SATA_DTYPE_NONE;
1317 				}
1318 			}
1319 			/*
1320 			 * Note: PMult info requires different handling.
1321 			 * Put PMult handling code here, when PMult is
1322 			 * supported.
1323 			 */
1324 
1325 		}
1326 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1327 		/* Just ask HBA driver to deactivate port */
1328 		sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
1329 
1330 		rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
1331 		    (dip, &sata_device);
1332 
1333 		/*
1334 		 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
1335 		 * without the hint.
1336 		 */
1337 		sata_gen_sysevent(sata_hba_inst,
1338 		    &sata_device.satadev_addr, SE_NO_HINT);
1339 
1340 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1341 		    cport_mutex);
1342 		sata_update_port_info(sata_hba_inst, &sata_device);
1343 
1344 		if (rval != SATA_SUCCESS) {
1345 			/*
1346 			 * Port deactivation failure - do not
1347 			 * change port state unless the state
1348 			 * returned by HBA indicates a port failure.
1349 			 */
1350 			if (sata_device.satadev_state & SATA_PSTATE_FAILED)
1351 				cportinfo->cport_state = SATA_PSTATE_FAILED;
1352 			rv = EIO;
1353 		} else {
1354 			/*
1355 			 * Deactivation succeded. From now on the framework
1356 			 * will not know what is happening to the device, until
1357 			 * the port is activated again.
1358 			 */
1359 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
1360 		}
1361 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1362 		break;
1363 
1364 	case DEVCTL_AP_UNCONFIGURE:
1365 
1366 		/*
1367 		 * The unconfigure operation uses generic nexus operation to
1368 		 * offline a device. It leaves a target device node attached.
1369 		 * and obviously sata_drive_info attached as well, because
1370 		 * from the hardware point of view nothing has changed.
1371 		 */
1372 		if ((tdip = sata_get_target_dip(dip, comp_port)) != NULL) {
1373 
1374 			if (ndi_devi_offline(tdip, NDI_UNCONFIG) !=
1375 			    NDI_SUCCESS) {
1376 				SATA_LOG_D((sata_hba_inst, CE_WARN,
1377 				    "sata_hba_ioctl: unconfigure: "
1378 				    "failed to unconfigure "
1379 				    "device at SATA port %d", cport));
1380 				rv = EIO;
1381 			}
1382 			/*
1383 			 * The target node devi_state should be marked with
1384 			 * DEVI_DEVICE_OFFLINE by ndi_devi_offline().
1385 			 * This would be the indication for cfgadm that
1386 			 * the AP node occupant state is 'unconfigured'.
1387 			 */
1388 
1389 		} else {
1390 			/*
1391 			 * This would indicate a failure on the part of cfgadm
1392 			 * to detect correct state of the node prior to this
1393 			 * call - one cannot unconfigure non-existing device.
1394 			 */
1395 			SATA_LOG_D((sata_hba_inst, CE_WARN,
1396 			    "sata_hba_ioctl: unconfigure: "
1397 			    "attempt to unconfigure non-existing device "
1398 			    "at SATA port %d", cport));
1399 			rv = ENXIO;
1400 		}
1401 
1402 		break;
1403 
1404 	case DEVCTL_AP_CONNECT:
1405 	{
1406 		/*
1407 		 * The sata cfgadm pluging will invoke this operation only if
1408 		 * port was found in the disconnect state (failed state
1409 		 * is also treated as the disconnected state).
1410 		 * DEVCTL_AP_CONNECT would invoke
1411 		 * sata_hba_inst->satahba_tran->
1412 		 * sata_tran_hotplug_ops->sata_tran_port_activate().
1413 		 * If successful and a device is found attached to the port,
1414 		 * the initialization sequence is executed to attach
1415 		 * a device structure to a port structure. The device is not
1416 		 * set in configured state (system-wise) by this operation.
1417 		 * The state of the port and a device would be set
1418 		 * appropriately.
1419 		 *
1420 		 * Note, that activating the port may generate link events,
1421 		 * so is is important that following processing and the
1422 		 * event processing does not interfere with each other!
1423 		 *
1424 		 * This operation may remove port failed state and will
1425 		 * try to make port active and in good standing.
1426 		 */
1427 
1428 		/* We only care about host sata cport for now */
1429 
1430 		if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) != NULL) {
1431 			/* Just let HBA driver to activate port */
1432 
1433 			if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
1434 			    (dip, &sata_device) != SATA_SUCCESS) {
1435 				/*
1436 				 * Port activation failure.
1437 				 */
1438 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
1439 				    cport)->cport_mutex);
1440 				sata_update_port_info(sata_hba_inst,
1441 				    &sata_device);
1442 				if (sata_device.satadev_state &
1443 				    SATA_PSTATE_FAILED) {
1444 					cportinfo->cport_state =
1445 					    SATA_PSTATE_FAILED;
1446 				}
1447 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
1448 				    cport)->cport_mutex);
1449 				SATA_LOG_D((sata_hba_inst, CE_WARN,
1450 				    "sata_hba_ioctl: connect: "
1451 				    "failed to activate SATA port %d",
1452 				    cport));
1453 				rv = EIO;
1454 				break;
1455 			}
1456 		}
1457 		/* Virgin port state - will be updated by the port re-probe. */
1458 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
1459 		    cport)->cport_mutex);
1460 		cportinfo->cport_state = 0;
1461 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
1462 		    cport)->cport_mutex);
1463 
1464 		/*
1465 		 * Probe the port to find its state and attached device.
1466 		 */
1467 		if (sata_reprobe_port(sata_hba_inst, &sata_device,
1468 		    SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE)
1469 			rv = EIO;
1470 		/*
1471 		 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
1472 		 * without the hint
1473 		 */
1474 		sata_gen_sysevent(sata_hba_inst,
1475 		    &sata_device.satadev_addr, SE_NO_HINT);
1476 		/*
1477 		 * If there is a device attached to the port, emit
1478 		 * a message.
1479 		 */
1480 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
1481 			sata_log(sata_hba_inst, CE_WARN,
1482 			    "SATA device detected at port %d", cport);
1483 		}
1484 		break;
1485 	}
1486 
1487 	case DEVCTL_AP_CONFIGURE:
1488 	{
1489 		boolean_t target = TRUE;
1490 
1491 		/*
1492 		 * A port may be in an active or shutdown state.
1493 		 * If port is in a failed state, operation is aborted - one
1494 		 * has to use explicit connect or port activate request
1495 		 * to try to get a port into non-failed mode.
1496 		 *
1497 		 * If a port is in a shutdown state, arbitrarily invoke
1498 		 * sata_tran_port_activate() prior to any other operation.
1499 		 *
1500 		 * Verify that port state is READY and there is a device
1501 		 * of a supported type attached to this port.
1502 		 * If target node exists, a device was most likely offlined.
1503 		 * If target node does not exist, create a target node an
1504 		 * attempt to online it.
1505 		 *		 *
1506 		 * NO PMult or devices beyond PMult are supported yet.
1507 		 */
1508 
1509 		/* We only care about host controller's sata cport for now. */
1510 		if (cportinfo->cport_state & SATA_PSTATE_FAILED) {
1511 			rv = ENXIO;
1512 			break;
1513 		}
1514 		/* Check the current state of the port */
1515 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
1516 
1517 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
1518 		    (dip, &sata_device);
1519 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1520 		    cport_mutex);
1521 		sata_update_port_info(sata_hba_inst, &sata_device);
1522 		if (rval != SATA_SUCCESS ||
1523 		    (sata_device.satadev_state & SATA_PSTATE_FAILED)) {
1524 			cportinfo->cport_state = SATA_PSTATE_FAILED;
1525 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1526 			    cport_mutex);
1527 			rv = EIO;
1528 			break;
1529 		}
1530 		if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN) {
1531 			target = FALSE;
1532 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1533 			    cport_mutex);
1534 
1535 			if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) != NULL) {
1536 				/* Just let HBA driver to activate port */
1537 				if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
1538 				    (dip, &sata_device) != SATA_SUCCESS) {
1539 					/*
1540 					 * Port activation failure - do not
1541 					 * change port state unless the state
1542 					 * returned by HBA indicates a port
1543 					 * failure.
1544 					 */
1545 					mutex_enter(&SATA_CPORT_INFO(
1546 					    sata_hba_inst, cport)->cport_mutex);
1547 					sata_update_port_info(sata_hba_inst,
1548 					    &sata_device);
1549 					if (sata_device.satadev_state &
1550 					    SATA_PSTATE_FAILED) {
1551 						cportinfo->cport_state =
1552 						    SATA_PSTATE_FAILED;
1553 					}
1554 					mutex_exit(&SATA_CPORT_INFO(
1555 					    sata_hba_inst, cport)->cport_mutex);
1556 					SATA_LOG_D((sata_hba_inst, CE_WARN,
1557 					    "sata_hba_ioctl: configure: "
1558 					    "failed to activate SATA port %d",
1559 					    cport));
1560 					rv = EIO;
1561 					break;
1562 				}
1563 			}
1564 			/*
1565 			 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
1566 			 * without the hint.
1567 			 */
1568 			sata_gen_sysevent(sata_hba_inst,
1569 			    &sata_device.satadev_addr, SE_NO_HINT);
1570 
1571 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1572 			    cport_mutex);
1573 			/* Virgin port state */
1574 			cportinfo->cport_state = 0;
1575 		}
1576 		/*
1577 		 * Always reprobe port, to get current device info.
1578 		 */
1579 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1580 		if (sata_reprobe_port(sata_hba_inst, &sata_device,
1581 		    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) {
1582 			rv = EIO;
1583 			break;
1584 		}
1585 		if (target == FALSE &&
1586 		    cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
1587 			/*
1588 			 * That's the transition from "inactive" port
1589 			 * to active one with device attached.
1590 			 */
1591 			sata_log(sata_hba_inst, CE_WARN,
1592 			    "SATA device detected at port %d",
1593 			    cport);
1594 		}
1595 
1596 		/*
1597 		 * This is where real configure starts.
1598 		 * Change following check for PMult support.
1599 		 */
1600 		if (!(sata_device.satadev_type & SATA_VALID_DEV_TYPE)) {
1601 			/* No device to configure */
1602 			rv = ENXIO; /* No device to configure */
1603 			break;
1604 		}
1605 
1606 		/*
1607 		 * Here we may have a device in reset condition,
1608 		 * but because we are just configuring it, there is
1609 		 * no need to process the reset other than just
1610 		 * to clear device reset condition in the HBA driver.
1611 		 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will
1612 		 * cause a first command sent the HBA driver with the request
1613 		 * to clear device reset condition.
1614 		 */
1615 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1616 		    cport_mutex);
1617 		sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
1618 		if (sdinfo == NULL) {
1619 			rv = ENXIO;
1620 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1621 			    cport_mutex);
1622 			break;
1623 		}
1624 		if (sdinfo->satadrv_event_flags &
1625 		    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET))
1626 			sdinfo->satadrv_event_flags = 0;
1627 		sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
1628 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1629 
1630 		if ((tdip = sata_get_target_dip(dip, comp_port)) != NULL) {
1631 			/*
1632 			 * Target node exists. Verify, that it belongs
1633 			 * to existing, attached device and not to
1634 			 * a removed device.
1635 			 */
1636 			if (sata_check_device_removed(tdip) == B_FALSE) {
1637 				if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) {
1638 					SATA_LOG_D((sata_hba_inst, CE_WARN,
1639 					    "sata_hba_ioctl: configure: "
1640 					    "onlining device at SATA port %d "
1641 					    "failed", cport));
1642 					rv = EIO;
1643 					break;
1644 				} else {
1645 					mutex_enter(&SATA_CPORT_INFO(
1646 					    sata_hba_inst, cport)->cport_mutex);
1647 					SATA_CPORT_INFO(sata_hba_inst, cport)->
1648 					    cport_tgtnode_clean = B_TRUE;
1649 					mutex_exit(&SATA_CPORT_INFO(
1650 					    sata_hba_inst, cport)->cport_mutex);
1651 				}
1652 			} else {
1653 				sata_log(sata_hba_inst, CE_WARN,
1654 				    "SATA device at port %d cannot be "
1655 				    "configured. "
1656 				    "Application(s) accessing previously "
1657 				    "attached device "
1658 				    "have to release it before newly inserted "
1659 				    "device can be made accessible.",
1660 				    cport);
1661 				break;
1662 			}
1663 		} else {
1664 			/*
1665 			 * No target node - need to create a new target node.
1666 			 */
1667 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1668 			    cport_mutex);
1669 			SATA_CPORT_INFO(sata_hba_inst, cport)->
1670 			    cport_tgtnode_clean = B_TRUE;
1671 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1672 			    cport_mutex);
1673 			tdip = sata_create_target_node(dip, sata_hba_inst,
1674 			    &sata_device.satadev_addr);
1675 			if (tdip == NULL) {
1676 				/* configure failed */
1677 				SATA_LOG_D((sata_hba_inst, CE_WARN,
1678 				    "sata_hba_ioctl: configure: "
1679 				    "configuring SATA device at port %d "
1680 				    "failed", cport));
1681 				rv = EIO;
1682 				break;
1683 			}
1684 		}
1685 
1686 		break;
1687 	}
1688 
1689 	case DEVCTL_AP_GETSTATE:
1690 
1691 		sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state);
1692 
1693 		ap_state.ap_last_change = (time_t)-1;
1694 		ap_state.ap_error_code = 0;
1695 		ap_state.ap_in_transition = 0;
1696 
1697 		/* Copy the return AP-state information to the user space */
1698 		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
1699 			rv = EFAULT;
1700 		}
1701 		break;
1702 
1703 	case DEVCTL_AP_CONTROL:
1704 	{
1705 		/*
1706 		 * Generic devctl for hardware specific functionality
1707 		 */
1708 		sata_ioctl_data_t	ioc;
1709 
1710 		ASSERT(dcp == NULL);
1711 
1712 		/* Copy in user ioctl data first */
1713 #ifdef _MULTI_DATAMODEL
1714 		if (ddi_model_convert_from(mode & FMODELS) ==
1715 		    DDI_MODEL_ILP32) {
1716 
1717 			sata_ioctl_data_32_t	ioc32;
1718 
1719 			if (ddi_copyin((void *)arg, (void *)&ioc32,
1720 			    sizeof (ioc32), mode) != 0) {
1721 				rv = EFAULT;
1722 				break;
1723 			}
1724 			ioc.cmd 	= (uint_t)ioc32.cmd;
1725 			ioc.port	= (uint_t)ioc32.port;
1726 			ioc.get_size	= (uint_t)ioc32.get_size;
1727 			ioc.buf		= (caddr_t)(uintptr_t)ioc32.buf;
1728 			ioc.bufsiz	= (uint_t)ioc32.bufsiz;
1729 			ioc.misc_arg	= (uint_t)ioc32.misc_arg;
1730 		} else
1731 #endif /* _MULTI_DATAMODEL */
1732 		if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
1733 		    mode) != 0) {
1734 			return (EFAULT);
1735 		}
1736 
1737 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
1738 		    "sata_hba_ioctl: DEVCTL_AP_CONTROL "
1739 		    "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port);
1740 
1741 		/*
1742 		 * To avoid BE/LE and 32/64 issues, a get_size always returns
1743 		 * a 32-bit number.
1744 		 */
1745 		if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
1746 			return (EINVAL);
1747 		}
1748 		/* validate address */
1749 		cport = SCSI_TO_SATA_CPORT(ioc.port);
1750 		pmport = SCSI_TO_SATA_PMPORT(ioc.port);
1751 		qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port);
1752 
1753 		/* Override address qualifier - handle cport only for now */
1754 		qual = SATA_ADDR_CPORT;
1755 
1756 		if (sata_validate_sata_address(sata_hba_inst, cport,
1757 		    pmport, qual) != 0)
1758 			return (EINVAL);
1759 
1760 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1761 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1762 		    cport_mutex);
1763 		/* Is the port locked by event processing daemon ? */
1764 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1765 			/*
1766 			 * Cannot process ioctl request now. Come back later
1767 			 */
1768 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1769 			    cport_mutex);
1770 			return (EBUSY);
1771 		}
1772 		/* Block event processing for this port */
1773 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1774 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1775 
1776 
1777 		sata_device.satadev_addr.cport = cport;
1778 		sata_device.satadev_addr.pmport = pmport;
1779 		sata_device.satadev_rev = SATA_DEVICE_REV;
1780 
1781 		switch (ioc.cmd) {
1782 
1783 		case SATA_CFGA_RESET_PORT:
1784 			/*
1785 			 * There is no protection here for configured
1786 			 * device.
1787 			 */
1788 
1789 			/* Sanity check */
1790 			if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
1791 				SATA_LOG_D((sata_hba_inst, CE_WARN,
1792 				    "sata_hba_ioctl: "
1793 				    "sata_hba_tran missing required "
1794 				    "function sata_tran_reset_dport"));
1795 				rv = EINVAL;
1796 				break;
1797 			}
1798 
1799 			/* handle cport only for now */
1800 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
1801 			if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
1802 			    (dip, &sata_device) != SATA_SUCCESS) {
1803 				SATA_LOG_D((sata_hba_inst, CE_WARN,
1804 				    "sata_hba_ioctl: reset port: "
1805 				    "failed cport %d pmport %d",
1806 				    cport, pmport));
1807 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
1808 				    cport)->cport_mutex);
1809 				sata_update_port_info(sata_hba_inst,
1810 				    &sata_device);
1811 				SATA_CPORT_STATE(sata_hba_inst, cport) =
1812 				    SATA_PSTATE_FAILED;
1813 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
1814 				    cport)->cport_mutex);
1815 				rv = EIO;
1816 			}
1817 			/*
1818 			 * Since the port was reset, it should be probed and
1819 			 * attached device reinitialized. At this point the
1820 			 * port state is unknown - it's state is HBA-specific.
1821 			 * Re-probe port to get its state.
1822 			 */
1823 			if (sata_reprobe_port(sata_hba_inst, &sata_device,
1824 			    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) {
1825 				rv = EIO;
1826 				break;
1827 			}
1828 			break;
1829 
1830 		case SATA_CFGA_RESET_DEVICE:
1831 			/*
1832 			 * There is no protection here for configured
1833 			 * device.
1834 			 */
1835 
1836 			/* Sanity check */
1837 			if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
1838 				SATA_LOG_D((sata_hba_inst, CE_WARN,
1839 				    "sata_hba_ioctl: "
1840 				    "sata_hba_tran missing required "
1841 				    "function sata_tran_reset_dport"));
1842 				rv = EINVAL;
1843 				break;
1844 			}
1845 
1846 			/* handle only device attached to cports, for now */
1847 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
1848 
1849 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1850 			    cport_mutex);
1851 			sdinfo = sata_get_device_info(sata_hba_inst,
1852 			    &sata_device);
1853 			if (sdinfo == NULL) {
1854 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
1855 				    cport)->cport_mutex);
1856 				rv = EINVAL;
1857 				break;
1858 			}
1859 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1860 			    cport_mutex);
1861 
1862 			/* only handle cport for now */
1863 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
1864 			if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
1865 			    (dip, &sata_device) != SATA_SUCCESS) {
1866 				SATA_LOG_D((sata_hba_inst, CE_WARN,
1867 				    "sata_hba_ioctl: reset device: failed "
1868 				    "cport %d pmport %d", cport, pmport));
1869 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
1870 				    cport)->cport_mutex);
1871 				sata_update_port_info(sata_hba_inst,
1872 				    &sata_device);
1873 				/*
1874 				 * Device info structure remains
1875 				 * attached. Another device reset or
1876 				 * port disconnect/connect and re-probing is
1877 				 * needed to change it's state
1878 				 */
1879 				sdinfo->satadrv_state &= ~SATA_STATE_READY;
1880 				sdinfo->satadrv_state |=
1881 				    SATA_DSTATE_FAILED;
1882 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
1883 				    cport)->cport_mutex);
1884 				rv = EIO;
1885 			}
1886 			/*
1887 			 * Since the device was reset, we expect reset event
1888 			 * to be reported and processed.
1889 			 */
1890 			break;
1891 
1892 		case SATA_CFGA_RESET_ALL:
1893 		{
1894 			int tcport;
1895 
1896 			/*
1897 			 * There is no protection here for configured
1898 			 * devices.
1899 			 */
1900 			/* Sanity check */
1901 			if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
1902 				SATA_LOG_D((sata_hba_inst, CE_WARN,
1903 				    "sata_hba_ioctl: "
1904 				    "sata_hba_tran missing required "
1905 				    "function sata_tran_reset_dport"));
1906 				rv = EINVAL;
1907 				break;
1908 			}
1909 
1910 			/*
1911 			 * Need to lock all ports, not just one.
1912 			 * If any port is locked by event processing, fail
1913 			 * the whole operation.
1914 			 * One port is already locked, but for simplicity
1915 			 * lock it again.
1916 			 */
1917 			for (tcport = 0;
1918 			    tcport < SATA_NUM_CPORTS(sata_hba_inst);
1919 			    tcport++) {
1920 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
1921 				    tcport)->cport_mutex);
1922 				if (((SATA_CPORT_INFO(sata_hba_inst, tcport)->
1923 				    cport_event_flags) &
1924 				    SATA_EVNT_LOCK_PORT_BUSY) != 0) {
1925 					rv = EBUSY;
1926 					mutex_exit(
1927 					    &SATA_CPORT_INFO(sata_hba_inst,
1928 					    tcport)->cport_mutex);
1929 					break;
1930 				} else {
1931 					SATA_CPORT_INFO(sata_hba_inst,
1932 					    tcport)->cport_event_flags |=
1933 					    SATA_APCTL_LOCK_PORT_BUSY;
1934 				}
1935 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
1936 				    tcport)->cport_mutex);
1937 			}
1938 
1939 			if (rv == 0) {
1940 				/*
1941 				 * All cports successfully locked.
1942 				 * Reset main SATA controller only for now -
1943 				 * no PMult.
1944 				 */
1945 				sata_device.satadev_addr.qual =
1946 				    SATA_ADDR_CNTRL;
1947 
1948 				if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
1949 				    (dip, &sata_device) != SATA_SUCCESS) {
1950 					SATA_LOG_D((sata_hba_inst, CE_WARN,
1951 					    "sata_hba_ioctl: reset controller "
1952 					    "failed"));
1953 					rv = EIO;
1954 				}
1955 
1956 				/*
1957 				 * Since ports were reset, they should be
1958 				 * re-probed and attached devices
1959 				 * reinitialized.
1960 				 * At this point port states are unknown,
1961 				 * Re-probe ports to get their state -
1962 				 * cports only for now.
1963 				 */
1964 				for (tcport = 0;
1965 				    tcport < SATA_NUM_CPORTS(sata_hba_inst);
1966 				    tcport++) {
1967 					sata_device.satadev_addr.cport =
1968 					    tcport;
1969 					sata_device.satadev_addr.qual =
1970 					    SATA_ADDR_CPORT;
1971 
1972 					if (sata_reprobe_port(sata_hba_inst,
1973 					    &sata_device,
1974 					    SATA_DEV_IDENTIFY_RETRY) !=
1975 					    SATA_SUCCESS)
1976 						rv = EIO;
1977 
1978 				}
1979 			}
1980 			/*
1981 			 * Unlock all ports
1982 			 */
1983 			for (tcport = 0;
1984 			    tcport < SATA_NUM_CPORTS(sata_hba_inst);
1985 			    tcport++) {
1986 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
1987 				    tcport)->cport_mutex);
1988 				SATA_CPORT_INFO(sata_hba_inst, tcport)->
1989 				    cport_event_flags &=
1990 				    ~SATA_APCTL_LOCK_PORT_BUSY;
1991 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
1992 				    tcport)->cport_mutex);
1993 			}
1994 
1995 			/*
1996 			 * This operation returns EFAULT if either reset
1997 			 * controller failed or a re-probing of any ports
1998 			 * failed.
1999 			 * We return here, because common return is for
2000 			 * a single cport operation.
2001 			 */
2002 			return (rv);
2003 		}
2004 
2005 		case SATA_CFGA_PORT_DEACTIVATE:
2006 			/* Sanity check */
2007 			if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
2008 				rv = ENOTSUP;
2009 				break;
2010 			}
2011 			/*
2012 			 * Arbitrarily unconfigure attached device, if any.
2013 			 * Even if the unconfigure fails, proceed with the
2014 			 * port deactivation.
2015 			 */
2016 
2017 			/* Handle only device attached to cports, for now */
2018 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
2019 
2020 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
2021 			    cport_mutex);
2022 			cportinfo->cport_state &= ~SATA_STATE_READY;
2023 			if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
2024 				/*
2025 				 * Handle only device attached to cports,
2026 				 * for now
2027 				 */
2028 				sata_device.satadev_addr.qual =
2029 				    SATA_ADDR_DCPORT;
2030 				sdinfo = sata_get_device_info(sata_hba_inst,
2031 				    &sata_device);
2032 				if (sdinfo != NULL &&
2033 				    (sdinfo->satadrv_type &
2034 				    SATA_VALID_DEV_TYPE)) {
2035 					/*
2036 					 * If a target node exists, try to
2037 					 * offline a device and remove target
2038 					 * node.
2039 					 */
2040 					mutex_exit(&SATA_CPORT_INFO(
2041 					    sata_hba_inst, cport)->cport_mutex);
2042 					tdip = sata_get_target_dip(dip, cport);
2043 					if (tdip != NULL) {
2044 						/* target node exist */
2045 						SATADBG1(SATA_DBG_IOCTL_IF,
2046 						    sata_hba_inst,
2047 						    "sata_hba_ioctl: "
2048 						    "port deactivate: "
2049 						    "target node exists.",
2050 						    NULL);
2051 
2052 						if (ndi_devi_offline(tdip,
2053 						    NDI_DEVI_REMOVE) !=
2054 						    NDI_SUCCESS) {
2055 							SATA_LOG_D((
2056 							    sata_hba_inst,
2057 							    CE_WARN,
2058 							    "sata_hba_ioctl:"
2059 							    "port deactivate: "
2060 							    "failed to "
2061 							    "unconfigure "
2062 							    "device at port "
2063 							    "%d before "
2064 							    "deactivating "
2065 							    "the port", cport));
2066 							/*
2067 							 * Set DEVICE REMOVED
2068 							 * state in the target
2069 							 * node. It will
2070 							 * prevent access to
2071 							 * the device even when
2072 							 * a new device is
2073 							 * attached, until the
2074 							 * old target node is
2075 							 * released, removed and
2076 							 * recreated for a new
2077 							 * device.
2078 							 */
2079 							sata_set_device_removed
2080 							    (tdip);
2081 							/*
2082 							 * Instruct event
2083 							 * daemon to try the
2084 							 * target node cleanup
2085 							 * later.
2086 							 */
2087 						sata_set_target_node_cleanup(
2088 						    sata_hba_inst, cport);
2089 						}
2090 					}
2091 					mutex_enter(&SATA_CPORT_INFO(
2092 					    sata_hba_inst, cport)->cport_mutex);
2093 					/*
2094 					 * In any case,
2095 					 * remove and release sata_drive_info
2096 					 * structure.
2097 					 * (cport attached device ony, for now)
2098 					 */
2099 					SATA_CPORTINFO_DRV_INFO(cportinfo) =
2100 					    NULL;
2101 					(void) kmem_free((void *)sdinfo,
2102 					    sizeof (sata_drive_info_t));
2103 					cportinfo->cport_dev_type =
2104 					    SATA_DTYPE_NONE;
2105 				}
2106 				/*
2107 				 * Note: PMult info requires different
2108 				 * handling. This comment is a placeholder for
2109 				 * a code handling PMult, to be implemented
2110 				 * in phase 2.
2111 				 */
2112 			}
2113 			cportinfo->cport_state &= ~(SATA_STATE_PROBED |
2114 			    SATA_STATE_PROBING);
2115 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
2116 			    cport_mutex);
2117 			/* handle cport only for now */
2118 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2119 			/* Just let HBA driver to deactivate port */
2120 			rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
2121 			    (dip, &sata_device);
2122 			/*
2123 			 * Generate sysevent -
2124 			 * EC_DR / ESC_DR_AP_STATE_CHANGE
2125 			 * without the hint
2126 			 */
2127 			sata_gen_sysevent(sata_hba_inst,
2128 			    &sata_device.satadev_addr, SE_NO_HINT);
2129 
2130 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
2131 			    cport_mutex);
2132 			sata_update_port_info(sata_hba_inst, &sata_device);
2133 			if (rval != SATA_SUCCESS) {
2134 				/*
2135 				 * Port deactivation failure - do not
2136 				 * change port state unless the state
2137 				 * returned by HBA indicates a port failure.
2138 				 */
2139 				if (sata_device.satadev_state &
2140 				    SATA_PSTATE_FAILED) {
2141 					SATA_CPORT_STATE(sata_hba_inst,
2142 					    cport) = SATA_PSTATE_FAILED;
2143 				}
2144 				SATA_LOG_D((sata_hba_inst, CE_WARN,
2145 				    "sata_hba_ioctl: port deactivate: "
2146 				    "cannot deactivate SATA port %d",
2147 				    cport));
2148 				rv = EIO;
2149 			} else {
2150 				cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
2151 			}
2152 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
2153 			    cport_mutex);
2154 
2155 			break;
2156 
2157 		case SATA_CFGA_PORT_ACTIVATE:
2158 		{
2159 			boolean_t dev_existed = TRUE;
2160 
2161 			/* Sanity check */
2162 			if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) {
2163 				rv = ENOTSUP;
2164 				break;
2165 			}
2166 			/* handle cport only for now */
2167 			if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN ||
2168 			    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
2169 				dev_existed = FALSE;
2170 
2171 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2172 			/* Just let HBA driver to activate port */
2173 			if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
2174 			    (dip, &sata_device) != SATA_SUCCESS) {
2175 				/*
2176 				 * Port activation failure - do not
2177 				 * change port state unless the state
2178 				 * returned by HBA indicates a port failure.
2179 				 */
2180 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
2181 				    cport)->cport_mutex);
2182 				sata_update_port_info(sata_hba_inst,
2183 				    &sata_device);
2184 				if (sata_device.satadev_state &
2185 				    SATA_PSTATE_FAILED) {
2186 					SATA_CPORT_STATE(sata_hba_inst,
2187 					    cport) = SATA_PSTATE_FAILED;
2188 				}
2189 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
2190 				    cport)->cport_mutex);
2191 				SATA_LOG_D((sata_hba_inst, CE_WARN,
2192 				    "sata_hba_ioctl: port activate: "
2193 				    "cannot activate SATA port %d",
2194 				    cport));
2195 				rv = EIO;
2196 				break;
2197 			}
2198 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
2199 			    cport_mutex);
2200 			cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN;
2201 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
2202 			    cport_mutex);
2203 
2204 			/*
2205 			 * Re-probe port to find its current state and
2206 			 * possibly attached device.
2207 			 * Port re-probing may change the cportinfo device
2208 			 * type if device is found attached.
2209 			 * If port probing failed, the device type would be
2210 			 * set to SATA_DTYPE_NONE.
2211 			 */
2212 			(void) sata_reprobe_port(sata_hba_inst, &sata_device,
2213 			    SATA_DEV_IDENTIFY_RETRY);
2214 
2215 			/*
2216 			 * Generate sysevent -
2217 			 * EC_DR / ESC_DR_AP_STATE_CHANGE
2218 			 * without the hint.
2219 			 */
2220 			sata_gen_sysevent(sata_hba_inst,
2221 			    &sata_device.satadev_addr, SE_NO_HINT);
2222 
2223 			if (dev_existed == FALSE &&
2224 			    cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
2225 				/*
2226 				 * That's the transition from "inactive" port
2227 				 * state or active port without a device
2228 				 * attached to the active port state with
2229 				 * a device attached.
2230 				 */
2231 				sata_log(sata_hba_inst, CE_WARN,
2232 				    "SATA device detected at port %d", cport);
2233 			}
2234 
2235 			break;
2236 		}
2237 
2238 		case SATA_CFGA_PORT_SELF_TEST:
2239 
2240 			/* Sanity check */
2241 			if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL) {
2242 				rv = ENOTSUP;
2243 				break;
2244 			}
2245 			/*
2246 			 * There is no protection here for a configured
2247 			 * device attached to this port.
2248 			 */
2249 
2250 			/* only handle cport for now */
2251 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2252 
2253 			if ((*SATA_SELFTEST_FUNC(sata_hba_inst))
2254 			    (dip, &sata_device) != SATA_SUCCESS) {
2255 				SATA_LOG_D((sata_hba_inst, CE_WARN,
2256 				    "sata_hba_ioctl: port selftest: "
2257 				    "failed cport %d pmport %d",
2258 				    cport, pmport));
2259 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
2260 				    cport)->cport_mutex);
2261 				sata_update_port_info(sata_hba_inst,
2262 				    &sata_device);
2263 				SATA_CPORT_STATE(sata_hba_inst, cport) =
2264 				    SATA_PSTATE_FAILED;
2265 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
2266 				    cport)->cport_mutex);
2267 				rv = EIO;
2268 				break;
2269 			}
2270 			/*
2271 			 * Since the port was reset, it should be probed and
2272 			 * attached device reinitialized. At this point the
2273 			 * port state is unknown - it's state is HBA-specific.
2274 			 * Force port re-probing to get it into a known state.
2275 			 */
2276 			if (sata_reprobe_port(sata_hba_inst, &sata_device,
2277 			    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) {
2278 				rv = EIO;
2279 				break;
2280 			}
2281 			break;
2282 
2283 		case SATA_CFGA_GET_DEVICE_PATH:
2284 		{
2285 			char		path[MAXPATHLEN];
2286 			uint32_t	size;
2287 
2288 			(void) strcpy(path, "/devices");
2289 			if ((tdip = sata_get_target_dip(dip, ioc.port)) ==
2290 			    NULL) {
2291 
2292 				/*
2293 				 * No such device.
2294 				 * If this is a request for a size, do not
2295 				 * return EINVAL for non-exisiting target,
2296 				 * because cfgadm will indicate a meaningless
2297 				 * ioctl failure.
2298 				 * If this is a real request for a path,
2299 				 * indicate invalid argument.
2300 				 */
2301 				if (!ioc.get_size) {
2302 					rv = EINVAL;
2303 					break;
2304 				}
2305 			} else {
2306 				(void) ddi_pathname(tdip, path + strlen(path));
2307 			}
2308 			size = strlen(path) + 1;
2309 
2310 			if (ioc.get_size) {
2311 				if (ddi_copyout((void *)&size,
2312 				    ioc.buf, ioc.bufsiz, mode) != 0) {
2313 					rv = EFAULT;
2314 				}
2315 			} else {
2316 				if (ioc.bufsiz != size) {
2317 					rv = EINVAL;
2318 				} else if (ddi_copyout((void *)&path,
2319 				    ioc.buf, ioc.bufsiz, mode) != 0) {
2320 					rv = EFAULT;
2321 				}
2322 			}
2323 			break;
2324 		}
2325 
2326 		case SATA_CFGA_GET_AP_TYPE:
2327 		{
2328 			uint32_t	type_len;
2329 			const char	*ap_type;
2330 
2331 			/* cport only, no port multiplier support */
2332 			switch (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport)) {
2333 			case SATA_DTYPE_NONE:
2334 				ap_type = "port";
2335 				break;
2336 
2337 			case SATA_DTYPE_ATADISK:
2338 				ap_type = "disk";
2339 				break;
2340 
2341 			case SATA_DTYPE_ATAPICD:
2342 				ap_type = "cd/dvd";
2343 				break;
2344 
2345 			case SATA_DTYPE_PMULT:
2346 				ap_type = "pmult";
2347 				break;
2348 
2349 			case SATA_DTYPE_UNKNOWN:
2350 				ap_type = "unknown";
2351 				break;
2352 
2353 			default:
2354 				ap_type = "unsupported";
2355 				break;
2356 
2357 			} /* end of dev_type switch */
2358 
2359 			type_len = strlen(ap_type) + 1;
2360 
2361 			if (ioc.get_size) {
2362 				if (ddi_copyout((void *)&type_len,
2363 				    ioc.buf, ioc.bufsiz, mode) != 0) {
2364 					rv = EFAULT;
2365 					break;
2366 				}
2367 			} else {
2368 				if (ioc.bufsiz != type_len) {
2369 					rv = EINVAL;
2370 					break;
2371 				}
2372 				if (ddi_copyout((void *)ap_type, ioc.buf,
2373 				    ioc.bufsiz, mode) != 0) {
2374 					rv = EFAULT;
2375 					break;
2376 				}
2377 			}
2378 
2379 			break;
2380 		}
2381 
2382 		case SATA_CFGA_GET_MODEL_INFO:
2383 		{
2384 			uint32_t info_len;
2385 			char ap_info[sizeof (sdinfo->satadrv_id.ai_model) + 1];
2386 
2387 			/*
2388 			 * This operation should return to cfgadm the
2389 			 * device model information string
2390 			 */
2391 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
2392 			    cport_mutex);
2393 			/* only handle device connected to cport for now */
2394 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
2395 			sdinfo = sata_get_device_info(sata_hba_inst,
2396 			    &sata_device);
2397 			if (sdinfo == NULL) {
2398 				rv = EINVAL;
2399 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
2400 				    cport)->cport_mutex);
2401 				break;
2402 			}
2403 			bcopy(sdinfo->satadrv_id.ai_model, ap_info,
2404 			    sizeof (sdinfo->satadrv_id.ai_model));
2405 			swab(ap_info, ap_info,
2406 			    sizeof (sdinfo->satadrv_id.ai_model));
2407 			ap_info[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
2408 
2409 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
2410 			    cport_mutex);
2411 
2412 			info_len = strlen(ap_info) + 1;
2413 
2414 			if (ioc.get_size) {
2415 				if (ddi_copyout((void *)&info_len,
2416 				    ioc.buf, ioc.bufsiz, mode) != 0) {
2417 					rv = EFAULT;
2418 					break;
2419 				}
2420 			} else {
2421 				if (ioc.bufsiz < info_len) {
2422 					rv = EINVAL;
2423 					break;
2424 				}
2425 				if (ddi_copyout((void *)ap_info, ioc.buf,
2426 				    ioc.bufsiz, mode) != 0) {
2427 					rv = EFAULT;
2428 					break;
2429 				}
2430 			}
2431 
2432 			break;
2433 		}
2434 
2435 		case SATA_CFGA_GET_REVFIRMWARE_INFO:
2436 		{
2437 			uint32_t info_len;
2438 			char ap_info[
2439 			    sizeof (sdinfo->satadrv_id.ai_fw) + 1];
2440 
2441 			/*
2442 			 * This operation should return to cfgadm the
2443 			 * device firmware revision information string
2444 			 */
2445 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
2446 			    cport_mutex);
2447 			/* only handle device connected to cport for now */
2448 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
2449 
2450 			sdinfo = sata_get_device_info(sata_hba_inst,
2451 			    &sata_device);
2452 			if (sdinfo == NULL) {
2453 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
2454 				    cport)->cport_mutex);
2455 				rv = EINVAL;
2456 				break;
2457 			}
2458 			bcopy(sdinfo->satadrv_id.ai_fw, ap_info,
2459 			    sizeof (sdinfo->satadrv_id.ai_fw));
2460 			swab(ap_info, ap_info,
2461 			    sizeof (sdinfo->satadrv_id.ai_fw));
2462 			ap_info[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
2463 
2464 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
2465 			    cport_mutex);
2466 
2467 			info_len = strlen(ap_info) + 1;
2468 
2469 			if (ioc.get_size) {
2470 				if (ddi_copyout((void *)&info_len,
2471 				    ioc.buf, ioc.bufsiz, mode) != 0) {
2472 					rv = EFAULT;
2473 					break;
2474 				}
2475 			} else {
2476 				if (ioc.bufsiz < info_len) {
2477 					rv = EINVAL;
2478 					break;
2479 				}
2480 				if (ddi_copyout((void *)ap_info, ioc.buf,
2481 				    ioc.bufsiz, mode) != 0) {
2482 					rv = EFAULT;
2483 					break;
2484 				}
2485 			}
2486 
2487 			break;
2488 		}
2489 
2490 		case SATA_CFGA_GET_SERIALNUMBER_INFO:
2491 		{
2492 			uint32_t info_len;
2493 			char ap_info[
2494 			    sizeof (sdinfo->satadrv_id.ai_drvser) + 1];
2495 
2496 			/*
2497 			 * This operation should return to cfgadm the
2498 			 * device serial number information string
2499 			 */
2500 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
2501 			    cport_mutex);
2502 			/* only handle device connected to cport for now */
2503 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
2504 
2505 			sdinfo = sata_get_device_info(sata_hba_inst,
2506 			    &sata_device);
2507 			if (sdinfo == NULL) {
2508 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
2509 				    cport)->cport_mutex);
2510 				rv = EINVAL;
2511 				break;
2512 			}
2513 			bcopy(sdinfo->satadrv_id.ai_drvser, ap_info,
2514 			    sizeof (sdinfo->satadrv_id.ai_drvser));
2515 			swab(ap_info, ap_info,
2516 			    sizeof (sdinfo->satadrv_id.ai_drvser));
2517 			ap_info[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
2518 
2519 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
2520 			    cport_mutex);
2521 
2522 			info_len = strlen(ap_info) + 1;
2523 
2524 			if (ioc.get_size) {
2525 				if (ddi_copyout((void *)&info_len,
2526 				    ioc.buf, ioc.bufsiz, mode) != 0) {
2527 					rv = EFAULT;
2528 					break;
2529 				}
2530 			} else {
2531 				if (ioc.bufsiz < info_len) {
2532 					rv = EINVAL;
2533 					break;
2534 				}
2535 				if (ddi_copyout((void *)ap_info, ioc.buf,
2536 				    ioc.bufsiz, mode) != 0) {
2537 					rv = EFAULT;
2538 					break;
2539 				}
2540 			}
2541 
2542 			break;
2543 		}
2544 
2545 		default:
2546 			rv = EINVAL;
2547 			break;
2548 
2549 		} /* End of DEVCTL_AP_CONTROL cmd switch */
2550 
2551 		break;
2552 	}
2553 
2554 	default:
2555 	{
2556 		/*
2557 		 * If we got here, we got an IOCTL that SATA HBA Framework
2558 		 * does not recognize. Pass ioctl to HBA driver, in case
2559 		 * it could process it.
2560 		 */
2561 		sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran;
2562 		dev_info_t	*mydip = SATA_DIP(sata_hba_inst);
2563 
2564 		SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
2565 		    "IOCTL 0x%2x not supported in SATA framework, "
2566 		    "passthrough to HBA", cmd);
2567 
2568 		if (sata_tran->sata_tran_ioctl == NULL) {
2569 			rv = EINVAL;
2570 			break;
2571 		}
2572 		rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg);
2573 		if (rval != 0) {
2574 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
2575 			    "IOCTL 0x%2x failed in HBA", cmd);
2576 			rv = rval;
2577 		}
2578 		break;
2579 	}
2580 
2581 	} /* End of main IOCTL switch */
2582 
2583 	if (dcp) {
2584 		ndi_dc_freehdl(dcp);
2585 	}
2586 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
2587 	cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
2588 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
2589 
2590 	return (rv);
2591 }
2592 
2593 
2594 /*
2595  * Create error retrieval sata packet
2596  *
2597  * A sata packet is allocated and set-up to contain specified error retrieval
2598  * command and appropriate dma-able data buffer.
2599  * No association with any scsi packet is made and no callback routine is
2600  * specified.
2601  *
2602  * Returns a pointer to sata packet upon successfull packet creation.
2603  * Returns NULL, if packet cannot be created.
2604  */
2605 sata_pkt_t *
2606 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device,
2607     int pkt_type)
2608 {
2609 	sata_hba_inst_t	*sata_hba_inst;
2610 	sata_pkt_txlate_t *spx;
2611 	sata_pkt_t *spkt;
2612 	sata_drive_info_t *sdinfo;
2613 
2614 	mutex_enter(&sata_mutex);
2615 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
2616 	    sata_hba_inst = sata_hba_inst->satahba_next) {
2617 		if (SATA_DIP(sata_hba_inst) == dip)
2618 			break;
2619 	}
2620 	mutex_exit(&sata_mutex);
2621 	ASSERT(sata_hba_inst != NULL);
2622 
2623 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
2624 	if (sdinfo == NULL) {
2625 		sata_log(sata_hba_inst, CE_WARN,
2626 		    "sata: error recovery request for non-attached device at "
2627 		    "cport %d", sata_device->satadev_addr.cport);
2628 		return (NULL);
2629 	}
2630 
2631 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
2632 	spx->txlt_sata_hba_inst = sata_hba_inst;
2633 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
2634 	spkt = sata_pkt_alloc(spx, NULL);
2635 	if (spkt == NULL) {
2636 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
2637 		return (NULL);
2638 	}
2639 	/* address is needed now */
2640 	spkt->satapkt_device.satadev_addr = sata_device->satadev_addr;
2641 
2642 	switch (pkt_type) {
2643 	case SATA_ERR_RETR_PKT_TYPE_NCQ:
2644 		if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS)
2645 			return (spkt);
2646 		break;
2647 
2648 	case SATA_ERR_RETR_PKT_TYPE_ATAPI:
2649 		if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS)
2650 			return (spkt);
2651 		break;
2652 
2653 	default:
2654 		break;
2655 	}
2656 
2657 	sata_pkt_free(spx);
2658 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
2659 	return (NULL);
2660 
2661 }
2662 
2663 
2664 /*
2665  * Free error retrieval sata packet
2666  *
2667  * Free sata packet and any associated resources allocated previously by
2668  * sata_get_error_retrieval_pkt().
2669  *
2670  * Void return.
2671  */
2672 void
2673 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt)
2674 {
2675 	sata_pkt_txlate_t *spx =
2676 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
2677 
2678 	ASSERT(sata_pkt != NULL);
2679 
2680 	sata_free_local_buffer(spx);
2681 	sata_pkt_free(spx);
2682 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
2683 
2684 }
2685 
2686 
2687 /* ****************** SCSA required entry points *********************** */
2688 
2689 /*
2690  * Implementation of scsi tran_tgt_init.
2691  * sata_scsi_tgt_init() initializes scsi_device structure
2692  *
2693  * If successful, DDI_SUCCESS is returned.
2694  * DDI_FAILURE is returned if addressed device does not exist
2695  */
2696 
2697 static int
2698 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2699     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2700 {
2701 #ifndef __lock_lint
2702 	_NOTE(ARGUNUSED(hba_dip))
2703 	_NOTE(ARGUNUSED(tgt_dip))
2704 #endif
2705 	sata_device_t		sata_device;
2706 	sata_drive_info_t	*sdinfo;
2707 	struct sata_id		*sid;
2708 	sata_hba_inst_t		*sata_hba_inst;
2709 	char			model[SATA_ID_MODEL_LEN + 1];
2710 	char			fw[SATA_ID_FW_LEN + 1];
2711 	char			*vid, *pid;
2712 	int			i;
2713 
2714 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2715 
2716 	/* Validate scsi device address */
2717 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2718 	    &sata_device) != 0)
2719 		return (DDI_FAILURE);
2720 
2721 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2722 	    sata_device.satadev_addr.cport)));
2723 
2724 	/* sata_device now contains a valid sata address */
2725 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2726 	if (sdinfo == NULL) {
2727 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2728 		    sata_device.satadev_addr.cport)));
2729 		return (DDI_FAILURE);
2730 	}
2731 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2732 	    sata_device.satadev_addr.cport)));
2733 
2734 	/*
2735 	 * Check if we need to create a legacy devid (i.e cmdk style) for
2736 	 * the target disks.
2737 	 *
2738 	 * HBA devinfo node will have the property "use-cmdk-devid-format"
2739 	 * if we need to create cmdk-style devid for all the disk devices
2740 	 * attached to this controller. This property may have been set
2741 	 * from HBA driver's .conf file or by the HBA driver in its
2742 	 * attach(9F) function.
2743 	 */
2744 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2745 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2746 	    "use-cmdk-devid-format", 0) == 1)) {
2747 		/* register a legacy devid for this target node */
2748 		sata_target_devid_register(tgt_dip, sdinfo);
2749 	}
2750 
2751 
2752 	/*
2753 	 * 'Identify Device Data' does not always fit in standard SCSI
2754 	 * INQUIRY data, so establish INQUIRY_* properties with full-form
2755 	 * of information.
2756 	 */
2757 	sid = &sdinfo->satadrv_id;
2758 #ifdef	_LITTLE_ENDIAN
2759 	swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
2760 	swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
2761 #else	/* _LITTLE_ENDIAN */
2762 	bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN);
2763 	bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN);
2764 #endif	/* _LITTLE_ENDIAN */
2765 	model[SATA_ID_MODEL_LEN] = 0;
2766 	fw[SATA_ID_FW_LEN] = 0;
2767 
2768 	/* split model into into vid/pid */
2769 	for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++)
2770 		if ((*pid == ' ') || (*pid == '\t'))
2771 			break;
2772 	if (i < SATA_ID_MODEL_LEN) {
2773 		vid = model;
2774 		*pid++ = 0;		/* terminate vid, establish pid */
2775 	} else {
2776 		vid = NULL;		/* vid will stay "ATA     " */
2777 		pid = model;		/* model is all pid */
2778 	}
2779 
2780 	if (vid)
2781 		(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_VENDOR_ID,
2782 		    vid, strlen(vid));
2783 	if (pid)
2784 		(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID,
2785 		    pid, strlen(pid));
2786 	(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_REVISION_ID,
2787 	    fw, strlen(fw));
2788 
2789 	return (DDI_SUCCESS);
2790 }
2791 
2792 /*
2793  * Implementation of scsi tran_tgt_probe.
2794  * Probe target, by calling default scsi routine scsi_hba_probe()
2795  */
2796 static int
2797 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void))
2798 {
2799 	sata_hba_inst_t *sata_hba_inst =
2800 	    (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private);
2801 	int rval;
2802 
2803 	rval = scsi_hba_probe(sd, callback);
2804 
2805 	if (rval == SCSIPROBE_EXISTS) {
2806 		/*
2807 		 * Set property "pm-capable" on the target device node, so that
2808 		 * the target driver will not try to fetch scsi cycle counters
2809 		 * before enabling device power-management.
2810 		 */
2811 		if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
2812 		    "pm-capable", 1)) != DDI_PROP_SUCCESS) {
2813 			sata_log(sata_hba_inst, CE_WARN,
2814 			    "SATA device at port %d: "
2815 			    "will not be power-managed ",
2816 			    SCSI_TO_SATA_CPORT(sd->sd_address.a_target));
2817 			SATA_LOG_D((sata_hba_inst, CE_WARN,
2818 			    "failure updating pm-capable property"));
2819 		}
2820 	}
2821 	return (rval);
2822 }
2823 
2824 /*
2825  * Implementation of scsi tran_tgt_free.
2826  * Release all resources allocated for scsi_device
2827  */
2828 static void
2829 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2830     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2831 {
2832 #ifndef __lock_lint
2833 	_NOTE(ARGUNUSED(hba_dip))
2834 #endif
2835 	sata_device_t		sata_device;
2836 	sata_drive_info_t	*sdinfo;
2837 	sata_hba_inst_t		*sata_hba_inst;
2838 	ddi_devid_t		devid;
2839 
2840 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
2841 
2842 	/* Validate scsi device address */
2843 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
2844 	    &sata_device) != 0)
2845 		return;
2846 
2847 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2848 	    sata_device.satadev_addr.cport)));
2849 
2850 	/* sata_device now should contain a valid sata address */
2851 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
2852 	if (sdinfo == NULL) {
2853 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2854 		    sata_device.satadev_addr.cport)));
2855 		return;
2856 	}
2857 	/*
2858 	 * We did not allocate any resources in sata_scsi_tgt_init()
2859 	 * other than few properties.
2860 	 * Free them.
2861 	 */
2862 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2863 	    sata_device.satadev_addr.cport)));
2864 	if (ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable") !=
2865 	    DDI_PROP_SUCCESS)
2866 		SATA_LOG_D((sata_hba_inst, CE_WARN,
2867 		    "sata_scsi_tgt_free: pm-capable "
2868 		    "property could not be removed"));
2869 
2870 	/*
2871 	 * If devid was previously created but not freed up from
2872 	 * sd(7D) driver (i.e during detach(9F)) then do it here.
2873 	 */
2874 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
2875 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
2876 	    "use-cmdk-devid-format", 0) == 1) &&
2877 	    (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) {
2878 		ddi_devid_unregister(tgt_dip);
2879 		ddi_devid_free(devid);
2880 	}
2881 }
2882 
2883 /*
2884  * Implementation of scsi tran_init_pkt
2885  * Upon successful return, scsi pkt buffer has DMA resources allocated.
2886  *
2887  * It seems that we should always allocate pkt, even if the address is
2888  * for non-existing device - just use some default for dma_attr.
2889  * The reason is that there is no way to communicate this to a caller here.
2890  * Subsequent call to sata_scsi_start may fail appropriately.
2891  * Simply returning NULL does not seem to discourage a target driver...
2892  *
2893  * Returns a pointer to initialized scsi_pkt, or NULL otherwise.
2894  */
2895 static struct scsi_pkt *
2896 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
2897     struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
2898     int (*callback)(caddr_t), caddr_t arg)
2899 {
2900 	sata_hba_inst_t *sata_hba_inst =
2901 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2902 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
2903 	sata_device_t sata_device;
2904 	sata_drive_info_t *sdinfo;
2905 	sata_pkt_txlate_t *spx;
2906 	ddi_dma_attr_t cur_dma_attr;
2907 	int rval;
2908 	boolean_t new_pkt = TRUE;
2909 
2910 	ASSERT(ap->a_hba_tran->tran_hba_dip == dip);
2911 
2912 	/*
2913 	 * We need to translate the address, even if it could be
2914 	 * a bogus one, for a non-existing device
2915 	 */
2916 	sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
2917 	sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target);
2918 	sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
2919 	sata_device.satadev_rev = SATA_DEVICE_REV;
2920 
2921 	if (pkt == NULL) {
2922 		/*
2923 		 * Have to allocate a brand new scsi packet.
2924 		 * We need to operate with auto request sense enabled.
2925 		 */
2926 		pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen,
2927 		    MAX(statuslen, sizeof (struct scsi_arq_status)),
2928 		    tgtlen, sizeof (sata_pkt_txlate_t), callback, arg);
2929 
2930 		if (pkt == NULL)
2931 			return (NULL);
2932 
2933 		/* Fill scsi packet structure */
2934 		pkt->pkt_comp		= (void (*)())NULL;
2935 		pkt->pkt_time		= 0;
2936 		pkt->pkt_resid		= 0;
2937 		pkt->pkt_statistics	= 0;
2938 		pkt->pkt_reason		= 0;
2939 
2940 		/*
2941 		 * pkt_hba_private will point to sata pkt txlate structure
2942 		 */
2943 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2944 		bzero(spx, sizeof (sata_pkt_txlate_t));
2945 
2946 		spx->txlt_scsi_pkt = pkt;
2947 		spx->txlt_sata_hba_inst = sata_hba_inst;
2948 
2949 		/* Allocate sata_pkt */
2950 		spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback);
2951 		if (spx->txlt_sata_pkt == NULL) {
2952 			/* Could not allocate sata pkt */
2953 			scsi_hba_pkt_free(ap, pkt);
2954 			return (NULL);
2955 		}
2956 		/* Set sata address */
2957 		spx->txlt_sata_pkt->satapkt_device.satadev_addr =
2958 		    sata_device.satadev_addr;
2959 		spx->txlt_sata_pkt->satapkt_device.satadev_rev =
2960 		    sata_device.satadev_rev;
2961 
2962 		if ((bp == NULL) || (bp->b_bcount == 0))
2963 			return (pkt);
2964 
2965 		spx->txlt_total_residue = bp->b_bcount;
2966 	} else {
2967 		new_pkt = FALSE;
2968 		/*
2969 		 * Packet was preallocated/initialized by previous call
2970 		 */
2971 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2972 
2973 		if ((bp == NULL) || (bp->b_bcount == 0)) {
2974 			return (pkt);
2975 		}
2976 		ASSERT(spx->txlt_buf_dma_handle != NULL);
2977 
2978 		/* Pkt is available already: spx->txlt_scsi_pkt == pkt; */
2979 	}
2980 
2981 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
2982 
2983 	/*
2984 	 * We use an adjusted version of the dma_attr, to account
2985 	 * for device addressing limitations.
2986 	 * sata_adjust_dma_attr() will handle sdinfo == NULL which may
2987 	 * happen when a device is not yet configured.
2988 	 */
2989 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2990 	    sata_device.satadev_addr.cport)));
2991 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2992 	    &spx->txlt_sata_pkt->satapkt_device);
2993 	/* NULL sdinfo may be passsed to sata_adjust_dma_attr() */
2994 	sata_adjust_dma_attr(sdinfo,
2995 	    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
2996 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2997 	    sata_device.satadev_addr.cport)));
2998 	/*
2999 	 * Allocate necessary DMA resources for the packet's data buffer
3000 	 * NOTE:
3001 	 * In case of read/write commands, DMA resource allocation here is
3002 	 * based on the premise that the transfer length specified in
3003 	 * the read/write scsi cdb will match exactly DMA resources -
3004 	 * returning correct packet residue is crucial.
3005 	 */
3006 	if ((rval = sata_dma_buf_setup(spx, flags, callback, arg,
3007 	    &cur_dma_attr)) != DDI_SUCCESS) {
3008 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
3009 		sata_pkt_free(spx);
3010 		/*
3011 		 * If a DMA allocation request fails with
3012 		 * DDI_DMA_NOMAPPING, indicate the error by calling
3013 		 * bioerror(9F) with bp and an error code of EFAULT.
3014 		 * If a DMA allocation request fails with
3015 		 * DDI_DMA_TOOBIG, indicate the error by calling
3016 		 * bioerror(9F) with bp and an error code of EINVAL.
3017 		 */
3018 		switch (rval) {
3019 		case DDI_DMA_NORESOURCES:
3020 			bioerror(bp, 0);
3021 			break;
3022 		case DDI_DMA_NOMAPPING:
3023 		case DDI_DMA_BADATTR:
3024 			bioerror(bp, EFAULT);
3025 			break;
3026 		case DDI_DMA_TOOBIG:
3027 		default:
3028 			bioerror(bp, EINVAL);
3029 			break;
3030 		}
3031 		if (new_pkt == TRUE)
3032 			scsi_hba_pkt_free(ap, pkt);
3033 		return (NULL);
3034 	}
3035 	/* Set number of bytes that are not yet accounted for */
3036 	pkt->pkt_resid = spx->txlt_total_residue;
3037 	ASSERT(pkt->pkt_resid >= 0);
3038 
3039 	return (pkt);
3040 }
3041 
3042 /*
3043  * Implementation of scsi tran_start.
3044  * Translate scsi cmd into sata operation and return status.
3045  * ATAPI CDBs are passed to ATAPI devices - the device determines what commands
3046  * are supported.
3047  * For SATA hard disks, supported scsi commands:
3048  * SCMD_INQUIRY
3049  * SCMD_TEST_UNIT_READY
3050  * SCMD_START_STOP
3051  * SCMD_READ_CAPACITY
3052  * SCMD_REQUEST_SENSE
3053  * SCMD_LOG_SENSE_G1
3054  * SCMD_LOG_SELECT_G1
3055  * SCMD_MODE_SENSE	(specific pages)
3056  * SCMD_MODE_SENSE_G1	(specific pages)
3057  * SCMD_MODE_SELECT	(specific pages)
3058  * SCMD_MODE_SELECT_G1	(specific pages)
3059  * SCMD_SYNCHRONIZE_CACHE
3060  * SCMD_SYNCHRONIZE_CACHE_G1
3061  * SCMD_READ
3062  * SCMD_READ_G1
3063  * SCMD_READ_G4
3064  * SCMD_READ_G5
3065  * SCMD_WRITE
3066  * SCMD_WRITE_BUFFER
3067  * SCMD_WRITE_G1
3068  * SCMD_WRITE_G4
3069  * SCMD_WRITE_G5
3070  * SCMD_SEEK		(noop)
3071  * SCMD_SDIAG
3072  *
3073  * All other commands are rejected as unsupported.
3074  *
3075  * Returns:
3076  * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver
3077  * for execution. TRAN_ACCEPT may be returned also if device was removed but
3078  * a callback could be scheduled.
3079  * TRAN_BADPKT if cmd was directed to invalid address.
3080  * TRAN_FATAL_ERROR is command was rejected due to hardware error, including
3081  * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device
3082  * was removed and there was no callback specified in scsi pkt.
3083  * TRAN_BUSY if command could not be executed becasue HBA driver or SATA
3084  * framework was busy performing some other operation(s).
3085  *
3086  */
3087 static int
3088 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
3089 {
3090 	sata_hba_inst_t *sata_hba_inst =
3091 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
3092 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3093 	sata_drive_info_t *sdinfo;
3094 	struct buf *bp;
3095 	int cport;
3096 	int rval;
3097 
3098 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
3099 	    "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]);
3100 
3101 	ASSERT(spx != NULL &&
3102 	    spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL);
3103 
3104 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
3105 
3106 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
3107 	sdinfo = sata_get_device_info(sata_hba_inst,
3108 	    &spx->txlt_sata_pkt->satapkt_device);
3109 	if (sdinfo == NULL ||
3110 	    SATA_CPORT_INFO(sata_hba_inst, cport)->cport_tgtnode_clean ==
3111 	    B_FALSE) {
3112 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
3113 		pkt->pkt_reason = CMD_DEV_GONE;
3114 		/*
3115 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
3116 		 * only in callback function (for normal requests) and
3117 		 * in the dump code path.
3118 		 * So, if the callback is available, we need to do
3119 		 * the callback rather than returning TRAN_FATAL_ERROR here.
3120 		 */
3121 		if (pkt->pkt_comp != NULL) {
3122 			/* scsi callback required */
3123 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3124 			    (task_func_t *)pkt->pkt_comp,
3125 			    (void *)pkt, TQ_SLEEP) == NULL)
3126 				/* Scheduling the callback failed */
3127 				return (TRAN_BUSY);
3128 			return (TRAN_ACCEPT);
3129 		}
3130 		/* No callback available */
3131 		return (TRAN_FATAL_ERROR);
3132 	}
3133 
3134 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
3135 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
3136 		rval = sata_txlt_atapi(spx);
3137 		SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
3138 		    "sata_scsi_start atapi: rval %d\n", rval);
3139 		return (rval);
3140 	}
3141 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
3142 
3143 	/* ATA Disk commands processing starts here */
3144 
3145 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3146 
3147 	switch (pkt->pkt_cdbp[0]) {
3148 
3149 	case SCMD_INQUIRY:
3150 		/* Mapped to identify device */
3151 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
3152 			bp_mapin(bp);
3153 		rval = sata_txlt_inquiry(spx);
3154 		break;
3155 
3156 	case SCMD_TEST_UNIT_READY:
3157 		/*
3158 		 * SAT "SATA to ATA Translation" doc specifies translation
3159 		 * to ATA CHECK POWER MODE.
3160 		 */
3161 		rval = sata_txlt_test_unit_ready(spx);
3162 		break;
3163 
3164 	case SCMD_START_STOP:
3165 		/* Mapping depends on the command */
3166 		rval = sata_txlt_start_stop_unit(spx);
3167 		break;
3168 
3169 	case SCMD_READ_CAPACITY:
3170 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
3171 			bp_mapin(bp);
3172 		rval = sata_txlt_read_capacity(spx);
3173 		break;
3174 
3175 	case SCMD_REQUEST_SENSE:
3176 		/*
3177 		 * Always No Sense, since we force ARQ
3178 		 */
3179 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
3180 			bp_mapin(bp);
3181 		rval = sata_txlt_request_sense(spx);
3182 		break;
3183 
3184 	case SCMD_LOG_SENSE_G1:
3185 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
3186 			bp_mapin(bp);
3187 		rval = sata_txlt_log_sense(spx);
3188 		break;
3189 
3190 	case SCMD_LOG_SELECT_G1:
3191 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
3192 			bp_mapin(bp);
3193 		rval = sata_txlt_log_select(spx);
3194 		break;
3195 
3196 	case SCMD_MODE_SENSE:
3197 	case SCMD_MODE_SENSE_G1:
3198 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
3199 			bp_mapin(bp);
3200 		rval = sata_txlt_mode_sense(spx);
3201 		break;
3202 
3203 
3204 	case SCMD_MODE_SELECT:
3205 	case SCMD_MODE_SELECT_G1:
3206 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
3207 			bp_mapin(bp);
3208 		rval = sata_txlt_mode_select(spx);
3209 		break;
3210 
3211 	case SCMD_SYNCHRONIZE_CACHE:
3212 	case SCMD_SYNCHRONIZE_CACHE_G1:
3213 		rval = sata_txlt_synchronize_cache(spx);
3214 		break;
3215 
3216 	case SCMD_READ:
3217 	case SCMD_READ_G1:
3218 	case SCMD_READ_G4:
3219 	case SCMD_READ_G5:
3220 		rval = sata_txlt_read(spx);
3221 		break;
3222 	case SCMD_WRITE_BUFFER:
3223 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
3224 			bp_mapin(bp);
3225 		rval = sata_txlt_write_buffer(spx);
3226 		break;
3227 
3228 	case SCMD_WRITE:
3229 	case SCMD_WRITE_G1:
3230 	case SCMD_WRITE_G4:
3231 	case SCMD_WRITE_G5:
3232 		rval = sata_txlt_write(spx);
3233 		break;
3234 
3235 	case SCMD_SEEK:
3236 		rval = sata_txlt_nodata_cmd_immediate(spx);
3237 		break;
3238 
3239 		/* Other cases will be filed later */
3240 		/* postponed until phase 2 of the development */
3241 	default:
3242 		rval = sata_txlt_invalid_command(spx);
3243 		break;
3244 	}
3245 
3246 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
3247 	    "sata_scsi_start: rval %d\n", rval);
3248 
3249 	return (rval);
3250 }
3251 
3252 /*
3253  * Implementation of scsi tran_abort.
3254  * Abort specific pkt or all packets.
3255  *
3256  * Returns 1 if one or more packets were aborted, returns 0 otherwise
3257  *
3258  * May be called from an interrupt level.
3259  */
3260 static int
3261 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt)
3262 {
3263 	sata_hba_inst_t *sata_hba_inst =
3264 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
3265 	sata_device_t	sata_device;
3266 	sata_pkt_t	*sata_pkt;
3267 
3268 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
3269 	    "sata_scsi_abort: %s at target: 0x%x\n",
3270 	    scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target);
3271 
3272 	/* Validate address */
3273 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0)
3274 		/* Invalid address */
3275 		return (0);
3276 
3277 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
3278 	    sata_device.satadev_addr.cport)));
3279 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
3280 		/* invalid address */
3281 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3282 		    sata_device.satadev_addr.cport)));
3283 		return (0);
3284 	}
3285 	if (scsi_pkt == NULL) {
3286 		/*
3287 		 * Abort all packets.
3288 		 * Although we do not have specific packet, we still need
3289 		 * dummy packet structure to pass device address to HBA.
3290 		 * Allocate one, without sleeping. Fail if pkt cannot be
3291 		 * allocated.
3292 		 */
3293 		sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP);
3294 		if (sata_pkt == NULL) {
3295 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3296 			    sata_device.satadev_addr.cport)));
3297 			SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: "
3298 			    "could not allocate sata_pkt"));
3299 			return (0);
3300 		}
3301 		sata_pkt->satapkt_rev = SATA_PKT_REV;
3302 		sata_pkt->satapkt_device = sata_device;
3303 		sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
3304 	} else {
3305 		if (scsi_pkt->pkt_ha_private == NULL) {
3306 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3307 			    sata_device.satadev_addr.cport)));
3308 			return (0); /* Bad scsi pkt */
3309 		}
3310 		/* extract pointer to sata pkt */
3311 		sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)->
3312 		    txlt_sata_pkt;
3313 	}
3314 
3315 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3316 	    sata_device.satadev_addr.cport)));
3317 	/* Send abort request to HBA */
3318 	if ((*SATA_ABORT_FUNC(sata_hba_inst))
3319 	    (SATA_DIP(sata_hba_inst), sata_pkt,
3320 	    scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) ==
3321 	    SATA_SUCCESS) {
3322 		if (scsi_pkt == NULL)
3323 			kmem_free(sata_pkt, sizeof (sata_pkt_t));
3324 		/* Success */
3325 		return (1);
3326 	}
3327 	/* Else, something did not go right */
3328 	if (scsi_pkt == NULL)
3329 		kmem_free(sata_pkt, sizeof (sata_pkt_t));
3330 	/* Failure */
3331 	return (0);
3332 }
3333 
3334 
3335 /*
3336  * Implementation of scsi tran_reset.
3337  * RESET_ALL request is translated into port reset.
3338  * RESET_TARGET requests is translated into a device reset,
3339  * RESET_LUN request is accepted only for LUN 0 and translated into
3340  * device reset.
3341  * The target reset should cause all HBA active and queued packets to
3342  * be terminated and returned with pkt reason SATA_PKT_RESET prior to
3343  * the return. HBA should report reset event for the device.
3344  *
3345  * Returns 1 upon success, 0 upon failure.
3346  */
3347 static int
3348 sata_scsi_reset(struct scsi_address *ap, int level)
3349 {
3350 	sata_hba_inst_t	*sata_hba_inst =
3351 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
3352 	sata_device_t	sata_device;
3353 	int		val;
3354 
3355 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
3356 	    "sata_scsi_reset: level %d target: 0x%x\n",
3357 	    level, ap->a_target);
3358 
3359 	/* Validate address */
3360 	val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device);
3361 	if (val == -1)
3362 		/* Invalid address */
3363 		return (0);
3364 
3365 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
3366 	    sata_device.satadev_addr.cport)));
3367 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
3368 		/* invalid address */
3369 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3370 		    sata_device.satadev_addr.cport)));
3371 		return (0);
3372 	}
3373 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3374 	    sata_device.satadev_addr.cport)));
3375 	if (level == RESET_ALL) {
3376 		/* port reset - cport only */
3377 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
3378 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
3379 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
3380 			return (1);
3381 		else
3382 			return (0);
3383 
3384 	} else if (val == 0 &&
3385 	    (level == RESET_TARGET || level == RESET_LUN)) {
3386 		/* reset device (device attached) */
3387 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
3388 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
3389 			return (1);
3390 		else
3391 			return (0);
3392 	}
3393 	return (0);
3394 }
3395 
3396 
3397 /*
3398  * Implementation of scsi tran_getcap (get transport/device capabilities).
3399  * Supported capabilities for SATA hard disks:
3400  * auto-rqsense		(always supported)
3401  * tagged-qing		(supported if HBA supports it)
3402  * untagged-qing	(could be supported if disk supports it, but because
3403  *			 caching behavior allowing untagged queuing actually
3404  *			 results in reduced performance.  sd tries to throttle
3405  *			 back to only 3 outstanding commands, which may
3406  *			 work for real SCSI disks, but with read ahead
3407  *			 caching, having more than 1 outstanding command
3408  *			 results in cache thrashing.)
3409  * sector_size
3410  * dma_max
3411  * interconnect-type	(INTERCONNECT_SATA)
3412  *
3413  * Supported capabilities for ATAPI devices (CD/DVD):
3414  * auto-rqsense		(always supported)
3415  * sector_size
3416  * dma_max
3417  * interconnect-type	(INTERCONNECT_SATA)
3418  *
3419  * Request for other capabilities is rejected as unsupported.
3420  *
3421  * Returns supported capability value, or -1 if capability is unsuppported or
3422  * the address is invalid - no device.
3423  */
3424 
3425 static int
3426 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
3427 {
3428 
3429 	sata_hba_inst_t 	*sata_hba_inst =
3430 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
3431 	sata_device_t		sata_device;
3432 	sata_drive_info_t	*sdinfo;
3433 	ddi_dma_attr_t		adj_dma_attr;
3434 	int 			rval;
3435 
3436 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
3437 	    "sata_scsi_getcap: target: 0x%x, cap: %s\n",
3438 	    ap->a_target, cap);
3439 
3440 	/*
3441 	 * We want to process the capabilities on per port granularity.
3442 	 * So, we are specifically restricting ourselves to whom != 0
3443 	 * to exclude the controller wide handling.
3444 	 */
3445 	if (cap == NULL || whom == 0)
3446 		return (-1);
3447 
3448 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
3449 		/* Invalid address */
3450 		return (-1);
3451 	}
3452 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
3453 	    sata_device.satadev_addr.cport)));
3454 	if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) ==
3455 	    NULL) {
3456 		/* invalid address */
3457 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3458 		    sata_device.satadev_addr.cport)));
3459 		return (-1);
3460 	}
3461 
3462 	switch (scsi_hba_lookup_capstr(cap)) {
3463 	case SCSI_CAP_ARQ:
3464 		rval = 1;		/* ARQ supported, turned on */
3465 		break;
3466 
3467 	case SCSI_CAP_SECTOR_SIZE:
3468 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK)
3469 			rval = SATA_DISK_SECTOR_SIZE;	/* fixed size */
3470 		else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD)
3471 			rval = SATA_ATAPI_SECTOR_SIZE;
3472 		else rval = -1;
3473 		break;
3474 
3475 	/*
3476 	 * untagged queuing cause a performance inversion because of
3477 	 * the way sd operates.  Because of this reason we do not
3478 	 * use it when available.
3479 	 */
3480 	case SCSI_CAP_UNTAGGED_QING:
3481 		if (sdinfo->satadrv_features_enabled &
3482 		    SATA_DEV_F_E_UNTAGGED_QING)
3483 			rval = 1;	/* Untagged queuing available */
3484 		else
3485 			rval = -1;	/* Untagged queuing not available */
3486 		break;
3487 
3488 	case SCSI_CAP_TAGGED_QING:
3489 		if ((sdinfo->satadrv_features_enabled &
3490 		    SATA_DEV_F_E_TAGGED_QING) &&
3491 		    (sdinfo->satadrv_max_queue_depth > 1))
3492 			rval = 1;	/* Tagged queuing available */
3493 		else
3494 			rval = -1;	/* Tagged queuing not available */
3495 		break;
3496 
3497 	case SCSI_CAP_DMA_MAX:
3498 		sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst),
3499 		    &adj_dma_attr);
3500 		rval = (int)adj_dma_attr.dma_attr_maxxfer;
3501 		/* We rely on the fact that dma_attr_maxxfer < 0x80000000 */
3502 		break;
3503 
3504 	case SCSI_CAP_INTERCONNECT_TYPE:
3505 		rval = INTERCONNECT_SATA;	/* SATA interconnect type */
3506 		break;
3507 
3508 	default:
3509 		rval = -1;
3510 		break;
3511 	}
3512 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3513 	    sata_device.satadev_addr.cport)));
3514 	return (rval);
3515 }
3516 
3517 /*
3518  * Implementation of scsi tran_setcap
3519  *
3520  * Only SCSI_CAP_UNTAGGED_QING and  SCSI_CAP_TAGGED_QING are changeable.
3521  *
3522  */
3523 static int
3524 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
3525 {
3526 	sata_hba_inst_t	*sata_hba_inst =
3527 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
3528 	sata_device_t	sata_device;
3529 	sata_drive_info_t	*sdinfo;
3530 	int		rval;
3531 
3532 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
3533 	    "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap);
3534 
3535 	/*
3536 	 * We want to process the capabilities on per port granularity.
3537 	 * So, we are specifically restricting ourselves to whom != 0
3538 	 * to exclude the controller wide handling.
3539 	 */
3540 	if (cap == NULL || whom == 0) {
3541 		return (-1);
3542 	}
3543 
3544 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
3545 		/* Invalid address */
3546 		return (-1);
3547 	}
3548 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
3549 	    sata_device.satadev_addr.cport)));
3550 	if ((sdinfo = sata_get_device_info(sata_hba_inst,
3551 	    &sata_device)) == NULL) {
3552 		/* invalid address */
3553 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3554 		    sata_device.satadev_addr.cport)));
3555 		return (-1);
3556 	}
3557 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
3558 	    sata_device.satadev_addr.cport)));
3559 
3560 	switch (scsi_hba_lookup_capstr(cap)) {
3561 	case SCSI_CAP_ARQ:
3562 	case SCSI_CAP_SECTOR_SIZE:
3563 	case SCSI_CAP_DMA_MAX:
3564 	case SCSI_CAP_INTERCONNECT_TYPE:
3565 		rval = 0;
3566 		break;
3567 	case SCSI_CAP_UNTAGGED_QING:
3568 		if (SATA_QDEPTH(sata_hba_inst) > 1) {
3569 			rval = 1;
3570 			if (value == 1) {
3571 				sdinfo->satadrv_features_enabled |=
3572 				    SATA_DEV_F_E_UNTAGGED_QING;
3573 			} else if (value == 0) {
3574 				sdinfo->satadrv_features_enabled &=
3575 				    ~SATA_DEV_F_E_UNTAGGED_QING;
3576 			} else {
3577 				rval = -1;
3578 			}
3579 		} else {
3580 			rval = 0;
3581 		}
3582 		break;
3583 	case SCSI_CAP_TAGGED_QING:
3584 		/* This can TCQ or NCQ */
3585 		if (sata_func_enable & SATA_ENABLE_QUEUING &&
3586 		    ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ &&
3587 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) ||
3588 		    (sata_func_enable & SATA_ENABLE_NCQ &&
3589 		    sdinfo->satadrv_features_support & SATA_DEV_F_NCQ &&
3590 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) &&
3591 		    (sdinfo->satadrv_max_queue_depth > 1)) {
3592 			rval = 1;
3593 			if (value == 1) {
3594 				sdinfo->satadrv_features_enabled |=
3595 				    SATA_DEV_F_E_TAGGED_QING;
3596 			} else if (value == 0) {
3597 				sdinfo->satadrv_features_enabled &=
3598 				    ~SATA_DEV_F_E_TAGGED_QING;
3599 			} else {
3600 				rval = -1;
3601 			}
3602 		} else {
3603 			rval = 0;
3604 		}
3605 		break;
3606 	default:
3607 		rval = -1;
3608 		break;
3609 	}
3610 	return (rval);
3611 }
3612 
3613 /*
3614  * Implementations of scsi tran_destroy_pkt.
3615  * Free resources allocated by sata_scsi_init_pkt()
3616  */
3617 static void
3618 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3619 {
3620 	sata_pkt_txlate_t *spx;
3621 
3622 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3623 
3624 	if (spx->txlt_buf_dma_handle != NULL) {
3625 		if (spx->txlt_tmp_buf != NULL)  {
3626 			ASSERT(spx->txlt_tmp_buf_handle != 0);
3627 			/*
3628 			 * Intermediate DMA buffer was allocated.
3629 			 * Free allocated buffer and associated access handle.
3630 			 */
3631 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
3632 			spx->txlt_tmp_buf = NULL;
3633 		}
3634 		/*
3635 		 * Free DMA resources - cookies and handles
3636 		 */
3637 		if (spx->txlt_dma_cookie_list != NULL) {
3638 			if (spx->txlt_dma_cookie_list !=
3639 			    &spx->txlt_dma_cookie) {
3640 				(void) kmem_free(spx->txlt_dma_cookie_list,
3641 				    spx->txlt_dma_cookie_list_len *
3642 				    sizeof (ddi_dma_cookie_t));
3643 				spx->txlt_dma_cookie_list = NULL;
3644 			}
3645 		}
3646 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
3647 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
3648 	}
3649 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
3650 	sata_pkt_free(spx);
3651 
3652 	scsi_hba_pkt_free(ap, pkt);
3653 }
3654 
3655 /*
3656  * Implementation of scsi tran_dmafree.
3657  * Free DMA resources allocated by sata_scsi_init_pkt()
3658  */
3659 
3660 static void
3661 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
3662 {
3663 #ifndef __lock_lint
3664 	_NOTE(ARGUNUSED(ap))
3665 #endif
3666 	sata_pkt_txlate_t *spx;
3667 
3668 	ASSERT(pkt != NULL);
3669 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3670 
3671 	if (spx->txlt_buf_dma_handle != NULL) {
3672 		if (spx->txlt_tmp_buf != NULL)  {
3673 			/*
3674 			 * Intermediate DMA buffer was allocated.
3675 			 * Free allocated buffer and associated access handle.
3676 			 */
3677 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
3678 			spx->txlt_tmp_buf = NULL;
3679 		}
3680 		/*
3681 		 * Free DMA resources - cookies and handles
3682 		 */
3683 		/* ASSERT(spx->txlt_dma_cookie_list != NULL); */
3684 		if (spx->txlt_dma_cookie_list != NULL) {
3685 			if (spx->txlt_dma_cookie_list !=
3686 			    &spx->txlt_dma_cookie) {
3687 				(void) kmem_free(spx->txlt_dma_cookie_list,
3688 				    spx->txlt_dma_cookie_list_len *
3689 				    sizeof (ddi_dma_cookie_t));
3690 				spx->txlt_dma_cookie_list = NULL;
3691 			}
3692 		}
3693 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
3694 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
3695 		spx->txlt_buf_dma_handle = NULL;
3696 	}
3697 }
3698 
3699 /*
3700  * Implementation of scsi tran_sync_pkt.
3701  *
3702  * The assumption below is that pkt is unique - there is no need to check ap
3703  *
3704  * Synchronize DMA buffer and, if the intermediate buffer is used, copy data
3705  * into/from the real buffer.
3706  */
3707 static void
3708 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3709 {
3710 #ifndef __lock_lint
3711 	_NOTE(ARGUNUSED(ap))
3712 #endif
3713 	int rval;
3714 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
3715 	struct buf *bp;
3716 	int direction;
3717 
3718 	ASSERT(spx != NULL);
3719 	if (spx->txlt_buf_dma_handle != NULL) {
3720 		direction = spx->txlt_sata_pkt->
3721 		    satapkt_cmd.satacmd_flags.sata_data_direction;
3722 		if (spx->txlt_sata_pkt != NULL &&
3723 		    direction != SATA_DIR_NODATA_XFER) {
3724 			if (spx->txlt_tmp_buf != NULL) {
3725 				/* Intermediate DMA buffer used */
3726 				bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3727 
3728 				if (direction & SATA_DIR_WRITE) {
3729 					bcopy(bp->b_un.b_addr,
3730 					    spx->txlt_tmp_buf, bp->b_bcount);
3731 				}
3732 			}
3733 			/* Sync the buffer for device or for CPU */
3734 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle,   0, 0,
3735 			    (direction & SATA_DIR_WRITE) ?
3736 			    DDI_DMA_SYNC_FORDEV :  DDI_DMA_SYNC_FORCPU);
3737 			ASSERT(rval == DDI_SUCCESS);
3738 			if (spx->txlt_tmp_buf != NULL &&
3739 			    !(direction & SATA_DIR_WRITE)) {
3740 				/* Intermediate DMA buffer used for read */
3741 				bcopy(spx->txlt_tmp_buf,
3742 				    bp->b_un.b_addr, bp->b_bcount);
3743 			}
3744 
3745 		}
3746 	}
3747 }
3748 
3749 
3750 
3751 /* *******************  SATA - SCSI Translation functions **************** */
3752 /*
3753  * SCSI to SATA pkt and command translation and SATA to SCSI status/error
3754  * translation.
3755  */
3756 
3757 /*
3758  * Checks if a device exists and can be access and translates common
3759  * scsi_pkt data to sata_pkt data.
3760  *
3761  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and
3762  * sata_pkt was set-up.
3763  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not
3764  * exist and pkt_comp callback was scheduled.
3765  * Returns other TRAN_XXXXX values when error occured and command should be
3766  * rejected with the returned TRAN_XXXXX value.
3767  *
3768  * This function should be called with port mutex held.
3769  */
3770 static int
3771 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx)
3772 {
3773 	sata_drive_info_t *sdinfo;
3774 	sata_device_t sata_device;
3775 	const struct sata_cmd_flags sata_initial_cmd_flags = {
3776 		SATA_DIR_NODATA_XFER,
3777 		/* all other values to 0/FALSE */
3778 	};
3779 	/*
3780 	 * Pkt_reason has to be set if the pkt_comp callback is invoked,
3781 	 * and that implies TRAN_ACCEPT return value. Any other returned value
3782 	 * indicates that the scsi packet was not accepted (the reason will not
3783 	 * be checked by the scsi traget driver).
3784 	 * To make debugging easier, we set pkt_reason to know value here.
3785 	 * It may be changed later when different completion reason is
3786 	 * determined.
3787 	 */
3788 	spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
3789 
3790 	/* Validate address */
3791 	switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst,
3792 	    &spx->txlt_scsi_pkt->pkt_address, &sata_device)) {
3793 
3794 	case -1:
3795 		/* Invalid address or invalid device type */
3796 		return (TRAN_BADPKT);
3797 	case 1:
3798 		/* valid address but no device - it has disappeared ? */
3799 		spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE;
3800 		/*
3801 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
3802 		 * only in callback function (for normal requests) and
3803 		 * in the dump code path.
3804 		 * So, if the callback is available, we need to do
3805 		 * the callback rather than returning TRAN_FATAL_ERROR here.
3806 		 */
3807 		if (spx->txlt_scsi_pkt->pkt_comp != NULL) {
3808 			/* scsi callback required */
3809 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3810 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3811 			    (void *)spx->txlt_scsi_pkt,
3812 			    TQ_SLEEP) == NULL)
3813 				/* Scheduling the callback failed */
3814 				return (TRAN_BUSY);
3815 
3816 			return (TRAN_ACCEPT);
3817 		}
3818 		return (TRAN_FATAL_ERROR);
3819 	default:
3820 		/* all OK */
3821 		break;
3822 	}
3823 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3824 	    &spx->txlt_sata_pkt->satapkt_device);
3825 
3826 	/*
3827 	 * If device is in reset condition, reject the packet with
3828 	 * TRAN_BUSY, unless:
3829 	 * 1. system is panicking (dumping)
3830 	 * In such case only one thread is running and there is no way to
3831 	 * process reset.
3832 	 * 2. cfgadm operation is is progress (internal APCTL lock is set)
3833 	 * Some cfgadm operations involve drive commands, so reset condition
3834 	 * needs to be ignored for IOCTL operations.
3835 	 */
3836 	if ((sdinfo->satadrv_event_flags &
3837 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
3838 
3839 		if (!ddi_in_panic() &&
3840 		    ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst,
3841 		    sata_device.satadev_addr.cport) &
3842 		    SATA_APCTL_LOCK_PORT_BUSY) == 0)) {
3843 			spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
3844 			SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3845 			    "sata_scsi_start: rejecting command because "
3846 			    "of device reset state\n", NULL);
3847 			return (TRAN_BUSY);
3848 		}
3849 	}
3850 
3851 	/*
3852 	 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by
3853 	 * sata_scsi_pkt_init() because pkt init had to work also with
3854 	 * non-existing devices.
3855 	 * Now we know that the packet was set-up for a real device, so its
3856 	 * type is known.
3857 	 */
3858 	spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type;
3859 
3860 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags;
3861 	if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst,
3862 	    sata_device.satadev_addr.cport)->cport_event_flags &
3863 	    SATA_APCTL_LOCK_PORT_BUSY) != 0) {
3864 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3865 		    sata_ignore_dev_reset = B_TRUE;
3866 	}
3867 	/*
3868 	 * At this point the generic translation routine determined that the
3869 	 * scsi packet should be accepted. Packet completion reason may be
3870 	 * changed later when a different completion reason is determined.
3871 	 */
3872 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3873 
3874 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
3875 		/* Synchronous execution */
3876 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH |
3877 		    SATA_OPMODE_POLLING;
3878 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
3879 		    sata_ignore_dev_reset = ddi_in_panic();
3880 	} else {
3881 		/* Asynchronous execution */
3882 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH |
3883 		    SATA_OPMODE_INTERRUPTS;
3884 	}
3885 	/* Convert queuing information */
3886 	if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG)
3887 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag =
3888 		    B_TRUE;
3889 	else if (spx->txlt_scsi_pkt->pkt_flags &
3890 	    (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD))
3891 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag =
3892 		    B_TRUE;
3893 
3894 	/* Always limit pkt time */
3895 	if (spx->txlt_scsi_pkt->pkt_time == 0)
3896 		spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time;
3897 	else
3898 		/* Pass on scsi_pkt time */
3899 		spx->txlt_sata_pkt->satapkt_time =
3900 		    spx->txlt_scsi_pkt->pkt_time;
3901 
3902 	return (TRAN_ACCEPT);
3903 }
3904 
3905 
3906 /*
3907  * Translate ATA Identify Device data to SCSI Inquiry data.
3908  * This function may be called only for ATA devices.
3909  * This function should not be called for ATAPI devices - they
3910  * respond directly to SCSI Inquiry command.
3911  *
3912  * SATA Identify Device data has to be valid in sata_rive_info.
3913  * Buffer has to accomodate the inquiry length (36 bytes).
3914  *
3915  * This function should be called with a port mutex held.
3916  */
3917 static	void
3918 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst,
3919     sata_drive_info_t *sdinfo, uint8_t *buf)
3920 {
3921 
3922 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
3923 	struct sata_id *sid = &sdinfo->satadrv_id;
3924 
3925 	/* Start with a nice clean slate */
3926 	bzero((void *)inq, sizeof (struct scsi_inquiry));
3927 
3928 	/*
3929 	 * Rely on the dev_type for setting paripheral qualifier.
3930 	 * Assume that  DTYPE_RODIRECT applies to CD/DVD R/W devices.
3931 	 * It could be that DTYPE_OPTICAL could also qualify in the future.
3932 	 * ATAPI Inquiry may provide more data to the target driver.
3933 	 */
3934 	inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3935 	    DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */
3936 
3937 	inq->inq_rmb = sid->ai_config & SATA_REM_MEDIA ? 1 : 0;
3938 	inq->inq_qual = 0;	/* Device type qualifier (obsolete in SCSI3? */
3939 	inq->inq_iso = 0;	/* ISO version */
3940 	inq->inq_ecma = 0;	/* ECMA version */
3941 	inq->inq_ansi = 3;	/* ANSI version - SCSI 3 */
3942 	inq->inq_aenc = 0;	/* Async event notification cap. */
3943 	inq->inq_trmiop = 0;	/* Supports TERMINATE I/O PROC msg - NO */
3944 	inq->inq_normaca = 0;	/* setting NACA bit supported - NO */
3945 	inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */
3946 	inq->inq_len = 31;	/* Additional length */
3947 	inq->inq_dualp = 0;	/* dual port device - NO */
3948 	inq->inq_reladdr = 0;	/* Supports relative addressing - NO */
3949 	inq->inq_sync = 0;	/* Supports synchronous data xfers - NO */
3950 	inq->inq_linked = 0;	/* Supports linked commands - NO */
3951 				/*
3952 				 * Queuing support - controller has to
3953 				 * support some sort of command queuing.
3954 				 */
3955 	if (SATA_QDEPTH(sata_hba_inst) > 1)
3956 		inq->inq_cmdque = 1; /* Supports command queueing - YES */
3957 	else
3958 		inq->inq_cmdque = 0; /* Supports command queueing - NO */
3959 	inq->inq_sftre = 0;	/* Supports Soft Reset option - NO ??? */
3960 	inq->inq_wbus32 = 0;	/* Supports 32 bit wide data xfers - NO */
3961 	inq->inq_wbus16 = 0;	/* Supports 16 bit wide data xfers - NO */
3962 
3963 #ifdef	_LITTLE_ENDIAN
3964 	/* Swap text fields to match SCSI format */
3965 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3966 	swab(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3967 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3968 		swab(sid->ai_fw, inq->inq_revision, 4);	/* Revision level */
3969 	else
3970 		swab(&sid->ai_fw[4], inq->inq_revision, 4);	/* Rev. level */
3971 #else	/* _LITTLE_ENDIAN */
3972 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
3973 	bcopy(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
3974 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
3975 		bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
3976 	else
3977 		bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
3978 #endif	/* _LITTLE_ENDIAN */
3979 }
3980 
3981 
3982 /*
3983  * Scsi response set up for invalid command (command not supported)
3984  *
3985  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3986  */
3987 static int
3988 sata_txlt_invalid_command(sata_pkt_txlate_t *spx)
3989 {
3990 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3991 	struct scsi_extended_sense *sense;
3992 
3993 	scsipkt->pkt_reason = CMD_CMPLT;
3994 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3995 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3996 
3997 	*scsipkt->pkt_scbp = STATUS_CHECK;
3998 
3999 	sense = sata_arq_sense(spx);
4000 	sense->es_key = KEY_ILLEGAL_REQUEST;
4001 	sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE;
4002 
4003 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4004 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4005 
4006 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4007 	    scsipkt->pkt_comp != NULL)
4008 		/* scsi callback required */
4009 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4010 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4011 		    (void *)spx->txlt_scsi_pkt,
4012 		    TQ_SLEEP) == NULL)
4013 			/* Scheduling the callback failed */
4014 			return (TRAN_BUSY);
4015 	return (TRAN_ACCEPT);
4016 }
4017 
4018 /*
4019  * Scsi response setup for
4020  * emulated non-data command that requires no action/return data
4021  *
4022  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4023  */
4024 static 	int
4025 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx)
4026 {
4027 	int rval;
4028 
4029 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4030 
4031 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
4032 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
4033 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4034 		return (rval);
4035 	}
4036 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4037 
4038 	spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4039 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4040 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
4041 	*(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD;
4042 
4043 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4044 	    "Scsi_pkt completion reason %x\n",
4045 	    spx->txlt_scsi_pkt->pkt_reason);
4046 
4047 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 &&
4048 	    spx->txlt_scsi_pkt->pkt_comp != NULL)
4049 		/* scsi callback required */
4050 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4051 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
4052 		    (void *)spx->txlt_scsi_pkt,
4053 		    TQ_SLEEP) == NULL)
4054 			/* Scheduling the callback failed */
4055 			return (TRAN_BUSY);
4056 	return (TRAN_ACCEPT);
4057 }
4058 
4059 
4060 /*
4061  * SATA translate command: Inquiry / Identify Device
4062  * Use cached Identify Device data for now, rather than issuing actual
4063  * Device Identify cmd request. If device is detached and re-attached,
4064  * asynchromous event processing should fetch and refresh Identify Device
4065  * data.
4066  * Two VPD pages are supported now:
4067  * Vital Product Data page
4068  * Unit Serial Number page
4069  *
4070  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4071  */
4072 
4073 #define	EVPD			1	/* Extended Vital Product Data flag */
4074 #define	CMDDT			2	/* Command Support Data - Obsolete */
4075 #define	INQUIRY_SUP_VPD_PAGE	0	/* Supported VDP Pages Page COde */
4076 #define	INQUIRY_USN_PAGE	0x80	/* Unit Serial Number Page Code */
4077 #define	INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */
4078 
4079 static int
4080 sata_txlt_inquiry(sata_pkt_txlate_t *spx)
4081 {
4082 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4083 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4084 	sata_drive_info_t *sdinfo;
4085 	struct scsi_extended_sense *sense;
4086 	int count;
4087 	uint8_t *p;
4088 	int i, j;
4089 	uint8_t page_buf[0xff]; /* Max length */
4090 	int rval;
4091 
4092 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4093 
4094 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
4095 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
4096 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4097 		return (rval);
4098 	}
4099 
4100 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4101 	    &spx->txlt_sata_pkt->satapkt_device);
4102 
4103 	ASSERT(sdinfo != NULL);
4104 
4105 	scsipkt->pkt_reason = CMD_CMPLT;
4106 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4107 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4108 
4109 	/* Reject not supported request */
4110 	if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */
4111 		*scsipkt->pkt_scbp = STATUS_CHECK;
4112 		sense = sata_arq_sense(spx);
4113 		sense->es_key = KEY_ILLEGAL_REQUEST;
4114 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4115 		goto done;
4116 	}
4117 
4118 	/* Valid Inquiry request */
4119 	*scsipkt->pkt_scbp = STATUS_GOOD;
4120 
4121 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4122 
4123 		/*
4124 		 * Because it is fully emulated command storing data
4125 		 * programatically in the specified buffer, release
4126 		 * preallocated DMA resources before storing data in the buffer,
4127 		 * so no unwanted DMA sync would take place.
4128 		 */
4129 		sata_scsi_dmafree(NULL, scsipkt);
4130 
4131 		if (!(scsipkt->pkt_cdbp[1] & EVPD)) {
4132 			/* Standard Inquiry Data request */
4133 			struct scsi_inquiry inq;
4134 			unsigned int bufsize;
4135 
4136 			sata_identdev_to_inquiry(spx->txlt_sata_hba_inst,
4137 			    sdinfo, (uint8_t *)&inq);
4138 			/* Copy no more than requested */
4139 			count = MIN(bp->b_bcount,
4140 			    sizeof (struct scsi_inquiry));
4141 			bufsize = scsipkt->pkt_cdbp[4];
4142 			bufsize |= scsipkt->pkt_cdbp[3] << 8;
4143 			count = MIN(count, bufsize);
4144 			bcopy(&inq, bp->b_un.b_addr, count);
4145 
4146 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
4147 			scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
4148 			    bufsize - count : 0;
4149 		} else {
4150 			/*
4151 			 * peripheral_qualifier = 0;
4152 			 *
4153 			 * We are dealing only with HD and will be
4154 			 * dealing with CD/DVD devices soon
4155 			 */
4156 			uint8_t peripheral_device_type =
4157 			    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
4158 			    DTYPE_DIRECT : DTYPE_RODIRECT;
4159 
4160 			switch ((uint_t)scsipkt->pkt_cdbp[2]) {
4161 			case INQUIRY_SUP_VPD_PAGE:
4162 				/*
4163 				 * Request for suported Vital Product Data
4164 				 * pages - assuming only 2 page codes
4165 				 * supported
4166 				 */
4167 				page_buf[0] = peripheral_device_type;
4168 				page_buf[1] = INQUIRY_SUP_VPD_PAGE;
4169 				page_buf[2] = 0;
4170 				page_buf[3] = 2; /* page length */
4171 				page_buf[4] = INQUIRY_SUP_VPD_PAGE;
4172 				page_buf[5] = INQUIRY_USN_PAGE;
4173 				/* Copy no more than requested */
4174 				count = MIN(bp->b_bcount, 6);
4175 				bcopy(page_buf, bp->b_un.b_addr, count);
4176 				break;
4177 			case INQUIRY_USN_PAGE:
4178 				/*
4179 				 * Request for Unit Serial Number page
4180 				 */
4181 				page_buf[0] = peripheral_device_type;
4182 				page_buf[1] = INQUIRY_USN_PAGE;
4183 				page_buf[2] = 0;
4184 				page_buf[3] = 20; /* remaining page length */
4185 				p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser);
4186 #ifdef	_LITTLE_ENDIAN
4187 				swab(p, &page_buf[4], 20);
4188 #else
4189 				bcopy(p, &page_buf[4], 20);
4190 #endif
4191 				for (i = 0; i < 20; i++) {
4192 					if (page_buf[4 + i] == '\0' ||
4193 					    page_buf[4 + i] == '\040') {
4194 						break;
4195 					}
4196 				}
4197 				/*
4198 				 * 'i' contains string length.
4199 				 *
4200 				 * Least significant character of the serial
4201 				 * number shall appear as the last byte,
4202 				 * according to SBC-3 spec.
4203 				 */
4204 				p = &page_buf[20 + 4 - 1];
4205 				for (j = i; j > 0; j--, p--) {
4206 					*p = *(p - 20 + i);
4207 				}
4208 				p = &page_buf[4];
4209 				for (j = 20 - i; j > 0; j--) {
4210 					*p++ = '\040';
4211 				}
4212 				count = MIN(bp->b_bcount, 24);
4213 				bcopy(page_buf, bp->b_un.b_addr, count);
4214 				break;
4215 
4216 			case INQUIRY_DEV_IDENTIFICATION_PAGE:
4217 				/*
4218 				 * We may want to implement this page, when
4219 				 * identifiers are common for SATA devices
4220 				 * But not now.
4221 				 */
4222 				/*FALLTHROUGH*/
4223 
4224 			default:
4225 				/* Request for unsupported VPD page */
4226 				*scsipkt->pkt_scbp = STATUS_CHECK;
4227 				sense = sata_arq_sense(spx);
4228 				sense->es_key = KEY_ILLEGAL_REQUEST;
4229 				sense->es_add_code =
4230 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4231 				goto done;
4232 			}
4233 		}
4234 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4235 		scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
4236 		    scsipkt->pkt_cdbp[4] - count : 0;
4237 	}
4238 done:
4239 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4240 
4241 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4242 	    "Scsi_pkt completion reason %x\n",
4243 	    scsipkt->pkt_reason);
4244 
4245 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4246 	    scsipkt->pkt_comp != NULL) {
4247 		/* scsi callback required */
4248 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4249 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4250 		    TQ_SLEEP) == NULL)
4251 			/* Scheduling the callback failed */
4252 			return (TRAN_BUSY);
4253 	}
4254 	return (TRAN_ACCEPT);
4255 }
4256 
4257 /*
4258  * SATA translate command: Request Sense.
4259  * Emulated command (ATA version for SATA hard disks)
4260  * Always NO SENSE, because any sense data should be reported by ARQ sense.
4261  *
4262  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4263  */
4264 static int
4265 sata_txlt_request_sense(sata_pkt_txlate_t *spx)
4266 {
4267 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4268 	struct scsi_extended_sense sense;
4269 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4270 	int rval;
4271 
4272 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4273 
4274 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
4275 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
4276 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4277 		return (rval);
4278 	}
4279 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4280 
4281 
4282 	scsipkt->pkt_reason = CMD_CMPLT;
4283 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4284 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4285 	*scsipkt->pkt_scbp = STATUS_GOOD;
4286 
4287 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4288 		/*
4289 		 * Because it is fully emulated command storing data
4290 		 * programatically in the specified buffer, release
4291 		 * preallocated DMA resources before storing data in the buffer,
4292 		 * so no unwanted DMA sync would take place.
4293 		 */
4294 		int count = MIN(bp->b_bcount,
4295 		    sizeof (struct scsi_extended_sense));
4296 		sata_scsi_dmafree(NULL, scsipkt);
4297 		bzero(&sense, sizeof (struct scsi_extended_sense));
4298 		sense.es_valid = 0;	/* Valid LBA */
4299 		sense.es_class = 7;	/* Response code 0x70 - current err */
4300 		sense.es_key = KEY_NO_SENSE;
4301 		sense.es_add_len = 6;	/* Additional length */
4302 		/* Copy no more than requested */
4303 		bcopy(&sense, bp->b_un.b_addr, count);
4304 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4305 		scsipkt->pkt_resid = 0;
4306 	}
4307 
4308 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4309 	    "Scsi_pkt completion reason %x\n",
4310 	    scsipkt->pkt_reason);
4311 
4312 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4313 	    scsipkt->pkt_comp != NULL)
4314 		/* scsi callback required */
4315 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4316 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4317 		    TQ_SLEEP) == NULL)
4318 			/* Scheduling the callback failed */
4319 			return (TRAN_BUSY);
4320 	return (TRAN_ACCEPT);
4321 }
4322 
4323 /*
4324  * SATA translate command: Test Unit Ready
4325  * At the moment this is an emulated command (ATA version for SATA hard disks).
4326  * May be translated into Check Power Mode command in the future
4327  *
4328  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4329  */
4330 static int
4331 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx)
4332 {
4333 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4334 	struct scsi_extended_sense *sense;
4335 	int power_state;
4336 	int rval;
4337 
4338 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4339 
4340 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
4341 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
4342 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4343 		return (rval);
4344 	}
4345 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4346 
4347 	/* At this moment, emulate it rather than execute anything */
4348 	power_state = SATA_PWRMODE_ACTIVE;
4349 
4350 	scsipkt->pkt_reason = CMD_CMPLT;
4351 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4352 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4353 
4354 	switch (power_state) {
4355 	case SATA_PWRMODE_ACTIVE:
4356 	case SATA_PWRMODE_IDLE:
4357 		*scsipkt->pkt_scbp = STATUS_GOOD;
4358 		break;
4359 	default:
4360 		/* PWR mode standby */
4361 		*scsipkt->pkt_scbp = STATUS_CHECK;
4362 		sense = sata_arq_sense(spx);
4363 		sense->es_key = KEY_NOT_READY;
4364 		sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY;
4365 		break;
4366 	}
4367 
4368 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4369 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4370 
4371 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4372 	    scsipkt->pkt_comp != NULL)
4373 		/* scsi callback required */
4374 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4375 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4376 		    TQ_SLEEP) == NULL)
4377 			/* Scheduling the callback failed */
4378 			return (TRAN_BUSY);
4379 
4380 	return (TRAN_ACCEPT);
4381 }
4382 
4383 
4384 /*
4385  * SATA translate command: Start Stop Unit
4386  * Translation depends on a command:
4387  *	Start Unit translated into Idle Immediate
4388  *	Stop Unit translated into Standby Immediate
4389  *	Unload Media / NOT SUPPORTED YET
4390  *	Load Media / NOT SUPPROTED YET
4391  * Power condition bits are ignored, so is Immediate bit
4392  * Requesting synchronous execution.
4393  *
4394  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4395  * appropriate values in scsi_pkt fields.
4396  */
4397 static int
4398 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx)
4399 {
4400 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4401 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4402 	struct scsi_extended_sense *sense;
4403 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4404 	int cport = SATA_TXLT_CPORT(spx);
4405 	int rval;
4406 	int synch;
4407 
4408 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4409 	    "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1);
4410 
4411 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
4412 
4413 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
4414 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
4415 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4416 		return (rval);
4417 	}
4418 
4419 	if (scsipkt->pkt_cdbp[4] & 2) {
4420 		/* Load/Unload Media - invalid request */
4421 		*scsipkt->pkt_scbp = STATUS_CHECK;
4422 		sense = sata_arq_sense(spx);
4423 		sense->es_key = KEY_ILLEGAL_REQUEST;
4424 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4425 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4426 
4427 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4428 		    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4429 
4430 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4431 		    scsipkt->pkt_comp != NULL)
4432 			/* scsi callback required */
4433 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4434 			    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4435 			    TQ_SLEEP) == NULL)
4436 				/* Scheduling the callback failed */
4437 				return (TRAN_BUSY);
4438 
4439 		return (TRAN_ACCEPT);
4440 	}
4441 	scmd->satacmd_addr_type = 0;
4442 	scmd->satacmd_sec_count_lsb = 0;
4443 	scmd->satacmd_lba_low_lsb = 0;
4444 	scmd->satacmd_lba_mid_lsb = 0;
4445 	scmd->satacmd_lba_high_lsb = 0;
4446 	scmd->satacmd_features_reg = 0;
4447 	scmd->satacmd_device_reg = 0;
4448 	scmd->satacmd_status_reg = 0;
4449 	if (scsipkt->pkt_cdbp[4] & 1) {
4450 		/* Start Unit */
4451 		scmd->satacmd_cmd_reg = SATAC_IDLE_IM;
4452 	} else {
4453 		/* Stop Unit */
4454 		scmd->satacmd_cmd_reg = SATAC_STANDBY_IM;
4455 	}
4456 
4457 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4458 		/* Need to set-up a callback function */
4459 		spx->txlt_sata_pkt->satapkt_comp =
4460 		    sata_txlt_nodata_cmd_completion;
4461 		synch = FALSE;
4462 	} else {
4463 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
4464 		synch = TRUE;
4465 	}
4466 
4467 	/* Transfer command to HBA */
4468 	if (sata_hba_start(spx, &rval) != 0) {
4469 		/* Pkt not accepted for execution */
4470 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4471 		return (rval);
4472 	}
4473 
4474 	/*
4475 	 * If execution is non-synchronous,
4476 	 * a callback function will handle potential errors, translate
4477 	 * the response and will do a callback to a target driver.
4478 	 * If it was synchronous, check execution status using the same
4479 	 * framework callback.
4480 	 */
4481 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4482 	if (synch) {
4483 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4484 		    "synchronous execution status %x\n",
4485 		    spx->txlt_sata_pkt->satapkt_reason);
4486 
4487 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
4488 	}
4489 	return (TRAN_ACCEPT);
4490 
4491 }
4492 
4493 
4494 /*
4495  * SATA translate command:  Read Capacity.
4496  * Emulated command for SATA disks.
4497  * Capacity is retrieved from cached Idenifty Device data.
4498  * Identify Device data shows effective disk capacity, not the native
4499  * capacity, which may be limitted by Set Max Address command.
4500  * This is ATA version for SATA hard disks.
4501  *
4502  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4503  */
4504 static int
4505 sata_txlt_read_capacity(sata_pkt_txlate_t *spx)
4506 {
4507 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4508 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4509 	sata_drive_info_t *sdinfo;
4510 	uint64_t val;
4511 	uchar_t *rbuf;
4512 	int rval;
4513 
4514 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4515 	    "sata_txlt_read_capacity: ", NULL);
4516 
4517 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4518 
4519 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
4520 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
4521 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4522 		return (rval);
4523 	}
4524 
4525 	scsipkt->pkt_reason = CMD_CMPLT;
4526 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4527 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4528 	*scsipkt->pkt_scbp = STATUS_GOOD;
4529 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4530 		/*
4531 		 * Because it is fully emulated command storing data
4532 		 * programatically in the specified buffer, release
4533 		 * preallocated DMA resources before storing data in the buffer,
4534 		 * so no unwanted DMA sync would take place.
4535 		 */
4536 		sata_scsi_dmafree(NULL, scsipkt);
4537 
4538 		sdinfo = sata_get_device_info(
4539 		    spx->txlt_sata_hba_inst,
4540 		    &spx->txlt_sata_pkt->satapkt_device);
4541 		/* Last logical block address */
4542 		val = sdinfo->satadrv_capacity - 1;
4543 		rbuf = (uchar_t *)bp->b_un.b_addr;
4544 		/* Need to swap endians to match scsi format */
4545 		rbuf[0] = (val >> 24) & 0xff;
4546 		rbuf[1] = (val >> 16) & 0xff;
4547 		rbuf[2] = (val >> 8) & 0xff;
4548 		rbuf[3] = val & 0xff;
4549 		/* block size - always 512 bytes, for now */
4550 		rbuf[4] = 0;
4551 		rbuf[5] = 0;
4552 		rbuf[6] = 0x02;
4553 		rbuf[7] = 0;
4554 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4555 		scsipkt->pkt_resid = 0;
4556 
4557 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n",
4558 		    sdinfo->satadrv_capacity -1);
4559 	}
4560 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4561 	/*
4562 	 * If a callback was requested, do it now.
4563 	 */
4564 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4565 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4566 
4567 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4568 	    scsipkt->pkt_comp != NULL)
4569 		/* scsi callback required */
4570 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4571 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4572 		    TQ_SLEEP) == NULL)
4573 			/* Scheduling the callback failed */
4574 			return (TRAN_BUSY);
4575 
4576 	return (TRAN_ACCEPT);
4577 }
4578 
4579 /*
4580  * SATA translate command: Mode Sense.
4581  * Translated into appropriate SATA command or emulated.
4582  * Saved Values Page Control (03) are not supported.
4583  *
4584  * NOTE: only caching mode sense page is currently implemented.
4585  *
4586  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4587  */
4588 
4589 static int
4590 sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
4591 {
4592 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4593 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4594 	sata_drive_info_t *sdinfo;
4595 	sata_id_t *sata_id;
4596 	struct scsi_extended_sense *sense;
4597 	int 		len, bdlen, count, alc_len;
4598 	int		pc;	/* Page Control code */
4599 	uint8_t		*buf;	/* mode sense buffer */
4600 	int		rval;
4601 
4602 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4603 	    "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
4604 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4605 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4606 
4607 	buf = kmem_zalloc(1024, KM_SLEEP);
4608 
4609 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4610 
4611 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
4612 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
4613 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4614 		kmem_free(buf, 1024);
4615 		return (rval);
4616 	}
4617 
4618 	scsipkt->pkt_reason = CMD_CMPLT;
4619 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4620 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4621 
4622 	pc = scsipkt->pkt_cdbp[2] >> 6;
4623 
4624 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4625 		/*
4626 		 * Because it is fully emulated command storing data
4627 		 * programatically in the specified buffer, release
4628 		 * preallocated DMA resources before storing data in the buffer,
4629 		 * so no unwanted DMA sync would take place.
4630 		 */
4631 		sata_scsi_dmafree(NULL, scsipkt);
4632 
4633 		len = 0;
4634 		bdlen = 0;
4635 		if (!(scsipkt->pkt_cdbp[1] & 8)) {
4636 			if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
4637 			    (scsipkt->pkt_cdbp[0] & 0x10))
4638 				bdlen = 16;
4639 			else
4640 				bdlen = 8;
4641 		}
4642 		/* Build mode parameter header */
4643 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
4644 			/* 4-byte mode parameter header */
4645 			buf[len++] = 0;   	/* mode data length */
4646 			buf[len++] = 0;		/* medium type */
4647 			buf[len++] = 0;		/* dev-specific param */
4648 			buf[len++] = bdlen;	/* Block Descriptor length */
4649 		} else {
4650 			/* 8-byte mode parameter header */
4651 			buf[len++] = 0;		/* mode data length */
4652 			buf[len++] = 0;
4653 			buf[len++] = 0;		/* medium type */
4654 			buf[len++] = 0;		/* dev-specific param */
4655 			if (bdlen == 16)
4656 				buf[len++] = 1;	/* long lba descriptor */
4657 			else
4658 				buf[len++] = 0;
4659 			buf[len++] = 0;
4660 			buf[len++] = 0;		/* Block Descriptor length */
4661 			buf[len++] = bdlen;
4662 		}
4663 
4664 		sdinfo = sata_get_device_info(
4665 		    spx->txlt_sata_hba_inst,
4666 		    &spx->txlt_sata_pkt->satapkt_device);
4667 
4668 		/* Build block descriptor only if not disabled (DBD) */
4669 		if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
4670 			/* Block descriptor - direct-access device format */
4671 			if (bdlen == 8) {
4672 				/* build regular block descriptor */
4673 				buf[len++] =
4674 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
4675 				buf[len++] =
4676 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
4677 				buf[len++] =
4678 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
4679 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
4680 				buf[len++] = 0; /* density code */
4681 				buf[len++] = 0;
4682 				if (sdinfo->satadrv_type ==
4683 				    SATA_DTYPE_ATADISK)
4684 					buf[len++] = 2;
4685 				else
4686 					/* ATAPI */
4687 					buf[len++] = 8;
4688 				buf[len++] = 0;
4689 			} else if (bdlen == 16) {
4690 				/* Long LBA Accepted */
4691 				/* build long lba block descriptor */
4692 #ifndef __lock_lint
4693 				buf[len++] =
4694 				    (sdinfo->satadrv_capacity >> 56) & 0xff;
4695 				buf[len++] =
4696 				    (sdinfo->satadrv_capacity >> 48) & 0xff;
4697 				buf[len++] =
4698 				    (sdinfo->satadrv_capacity >> 40) & 0xff;
4699 				buf[len++] =
4700 				    (sdinfo->satadrv_capacity >> 32) & 0xff;
4701 #endif
4702 				buf[len++] =
4703 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
4704 				buf[len++] =
4705 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
4706 				buf[len++] =
4707 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
4708 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
4709 				buf[len++] = 0;
4710 				buf[len++] = 0; /* density code */
4711 				buf[len++] = 0;
4712 				buf[len++] = 0;
4713 				if (sdinfo->satadrv_type ==
4714 				    SATA_DTYPE_ATADISK)
4715 					buf[len++] = 2;
4716 				else
4717 					/* ATAPI */
4718 					buf[len++] = 8;
4719 				buf[len++] = 0;
4720 			}
4721 		}
4722 
4723 		sata_id = &sdinfo->satadrv_id;
4724 
4725 		/*
4726 		 * Add requested pages.
4727 		 * Page 3 and 4 are obsolete and we are not supporting them.
4728 		 * We deal now with:
4729 		 * caching (read/write cache control).
4730 		 * We should eventually deal with following mode pages:
4731 		 * error recovery  (0x01),
4732 		 * power condition (0x1a),
4733 		 * exception control page (enables SMART) (0x1c),
4734 		 * enclosure management (ses),
4735 		 * protocol-specific port mode (port control).
4736 		 */
4737 		switch (scsipkt->pkt_cdbp[2] & 0x3f) {
4738 		case MODEPAGE_RW_ERRRECOV:
4739 			/* DAD_MODE_ERR_RECOV */
4740 			/* R/W recovery */
4741 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
4742 			break;
4743 		case MODEPAGE_CACHING:
4744 			/* DAD_MODE_CACHE */
4745 			/* Reject not supported request for saved parameters */
4746 			if (pc == 3) {
4747 				*scsipkt->pkt_scbp = STATUS_CHECK;
4748 				sense = sata_arq_sense(spx);
4749 				sense->es_key = KEY_ILLEGAL_REQUEST;
4750 				sense->es_add_code =
4751 				    SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
4752 				goto done;
4753 			}
4754 
4755 			/* caching */
4756 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
4757 			break;
4758 		case MODEPAGE_INFO_EXCPT:
4759 			/* exception cntrl */
4760 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
4761 				len += sata_build_msense_page_1c(sdinfo, pc,
4762 				    buf+len);
4763 			}
4764 			else
4765 				goto err;
4766 			break;
4767 		case MODEPAGE_POWER_COND:
4768 			/* DAD_MODE_POWER_COND */
4769 			/* power condition */
4770 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
4771 			break;
4772 
4773 		case MODEPAGE_ACOUSTIC_MANAG:
4774 			/* acoustic management */
4775 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
4776 			break;
4777 		case MODEPAGE_ALLPAGES:
4778 			/* all pages */
4779 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
4780 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
4781 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
4782 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
4783 				len += sata_build_msense_page_1c(sdinfo, pc,
4784 				    buf+len);
4785 			}
4786 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
4787 			break;
4788 		default:
4789 		err:
4790 			/* Invalid request */
4791 			*scsipkt->pkt_scbp = STATUS_CHECK;
4792 			sense = sata_arq_sense(spx);
4793 			sense->es_key = KEY_ILLEGAL_REQUEST;
4794 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4795 			goto done;
4796 		}
4797 
4798 		/* fix total mode data length */
4799 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
4800 			/* 4-byte mode parameter header */
4801 			buf[0] = len - 1;   	/* mode data length */
4802 		} else {
4803 			buf[0] = (len -2) >> 8;
4804 			buf[1] = (len -2) & 0xff;
4805 		}
4806 
4807 
4808 		/* Check allocation length */
4809 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
4810 			alc_len = scsipkt->pkt_cdbp[4];
4811 		} else {
4812 			alc_len = scsipkt->pkt_cdbp[7];
4813 			alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
4814 		}
4815 		/*
4816 		 * We do not check for possible parameters truncation
4817 		 * (alc_len < len) assuming that the target driver works
4818 		 * correctly. Just avoiding overrun.
4819 		 * Copy no more than requested and possible, buffer-wise.
4820 		 */
4821 		count = MIN(alc_len, len);
4822 		count = MIN(bp->b_bcount, count);
4823 		bcopy(buf, bp->b_un.b_addr, count);
4824 
4825 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4826 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
4827 	}
4828 	*scsipkt->pkt_scbp = STATUS_GOOD;
4829 done:
4830 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4831 	(void) kmem_free(buf, 1024);
4832 
4833 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4834 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4835 
4836 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4837 	    scsipkt->pkt_comp != NULL)
4838 		/* scsi callback required */
4839 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4840 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4841 		    TQ_SLEEP) == NULL)
4842 			/* Scheduling the callback failed */
4843 			return (TRAN_BUSY);
4844 
4845 	return (TRAN_ACCEPT);
4846 }
4847 
4848 
4849 /*
4850  * SATA translate command: Mode Select.
4851  * Translated into appropriate SATA command or emulated.
4852  * Saving parameters is not supported.
4853  * Changing device capacity is not supported (although theoretically
4854  * possible by executing SET FEATURES/SET MAX ADDRESS)
4855  *
4856  * Assumption is that the target driver is working correctly.
4857  *
4858  * More than one SATA command may be executed to perform operations specified
4859  * by mode select pages. The first error terminates further execution.
4860  * Operations performed successully are not backed-up in such case.
4861  *
4862  * NOTE: only caching mode select page is implemented.
4863  * Caching setup is remembered so it could be re-stored in case of
4864  * an unexpected device reset.
4865  *
4866  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
4867  */
4868 
4869 static int
4870 sata_txlt_mode_select(sata_pkt_txlate_t *spx)
4871 {
4872 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4873 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4874 	struct scsi_extended_sense *sense;
4875 	int len, pagelen, count, pllen;
4876 	uint8_t *buf;	/* mode select buffer */
4877 	int rval, stat;
4878 	uint_t nointr_flag;
4879 	int dmod = 0;
4880 
4881 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4882 	    "sata_txlt_mode_select, pc %x page code 0x%02x\n",
4883 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4884 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4885 
4886 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4887 
4888 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
4889 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
4890 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4891 		return (rval);
4892 	}
4893 
4894 	rval = TRAN_ACCEPT;
4895 
4896 	scsipkt->pkt_reason = CMD_CMPLT;
4897 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4898 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4899 
4900 	/* Reject not supported request */
4901 	if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
4902 		*scsipkt->pkt_scbp = STATUS_CHECK;
4903 		sense = sata_arq_sense(spx);
4904 		sense->es_key = KEY_ILLEGAL_REQUEST;
4905 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4906 		goto done;
4907 	}
4908 
4909 	if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
4910 		pllen = scsipkt->pkt_cdbp[4];
4911 	} else {
4912 		pllen = scsipkt->pkt_cdbp[7];
4913 		pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
4914 	}
4915 
4916 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
4917 
4918 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
4919 		buf = (uint8_t *)bp->b_un.b_addr;
4920 		count = MIN(bp->b_bcount, pllen);
4921 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4922 		scsipkt->pkt_resid = 0;
4923 		pllen = count;
4924 
4925 		/*
4926 		 * Check the header to skip the block descriptor(s) - we
4927 		 * do not support setting device capacity.
4928 		 * Existing macros do not recognize long LBA dscriptor,
4929 		 * hence manual calculation.
4930 		 */
4931 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
4932 			/* 6-bytes CMD, 4 bytes header */
4933 			if (count <= 4)
4934 				goto done;		/* header only */
4935 			len = buf[3] + 4;
4936 		} else {
4937 			/* 10-bytes CMD, 8 bytes header */
4938 			if (count <= 8)
4939 				goto done;		/* header only */
4940 			len = buf[6];
4941 			len = (len << 8) + buf[7] + 8;
4942 		}
4943 		if (len >= count)
4944 			goto done;	/* header + descriptor(s) only */
4945 
4946 		pllen -= len;		/* remaining data length */
4947 
4948 		/*
4949 		 * We may be executing SATA command and want to execute it
4950 		 * in SYNCH mode, regardless of scsi_pkt setting.
4951 		 * Save scsi_pkt setting and indicate SYNCH mode
4952 		 */
4953 		nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
4954 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4955 		    scsipkt->pkt_comp != NULL) {
4956 			scsipkt->pkt_flags |= FLAG_NOINTR;
4957 		}
4958 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
4959 
4960 		/*
4961 		 * len is now the offset to a first mode select page
4962 		 * Process all pages
4963 		 */
4964 		while (pllen > 0) {
4965 			switch ((int)buf[len]) {
4966 			case MODEPAGE_CACHING:
4967 				/* No support for SP (saving) */
4968 				if (scsipkt->pkt_cdbp[1] & 0x01) {
4969 					*scsipkt->pkt_scbp = STATUS_CHECK;
4970 					sense = sata_arq_sense(spx);
4971 					sense->es_key = KEY_ILLEGAL_REQUEST;
4972 					sense->es_add_code =
4973 					    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4974 					goto done;
4975 				}
4976 				stat = sata_mode_select_page_8(spx,
4977 				    (struct mode_cache_scsi3 *)&buf[len],
4978 				    pllen, &pagelen, &rval, &dmod);
4979 				/*
4980 				 * The pagelen value indicates the number of
4981 				 * parameter bytes already processed.
4982 				 * The rval is the return value from
4983 				 * sata_tran_start().
4984 				 * The stat indicates the overall status of
4985 				 * the operation(s).
4986 				 */
4987 				if (stat != SATA_SUCCESS)
4988 					/*
4989 					 * Page processing did not succeed -
4990 					 * all error info is already set-up,
4991 					 * just return
4992 					 */
4993 					pllen = 0; /* this breaks the loop */
4994 				else {
4995 					len += pagelen;
4996 					pllen -= pagelen;
4997 				}
4998 				break;
4999 
5000 			case MODEPAGE_INFO_EXCPT:
5001 				stat = sata_mode_select_page_1c(spx,
5002 				    (struct mode_info_excpt_page *)&buf[len],
5003 				    pllen, &pagelen, &rval, &dmod);
5004 				/*
5005 				 * The pagelen value indicates the number of
5006 				 * parameter bytes already processed.
5007 				 * The rval is the return value from
5008 				 * sata_tran_start().
5009 				 * The stat indicates the overall status of
5010 				 * the operation(s).
5011 				 */
5012 				if (stat != SATA_SUCCESS)
5013 					/*
5014 					 * Page processing did not succeed -
5015 					 * all error info is already set-up,
5016 					 * just return
5017 					 */
5018 					pllen = 0; /* this breaks the loop */
5019 				else {
5020 					len += pagelen;
5021 					pllen -= pagelen;
5022 				}
5023 				break;
5024 
5025 			case MODEPAGE_ACOUSTIC_MANAG:
5026 				stat = sata_mode_select_page_30(spx,
5027 				    (struct mode_acoustic_management *)
5028 				    &buf[len], pllen, &pagelen, &rval, &dmod);
5029 				/*
5030 				 * The pagelen value indicates the number of
5031 				 * parameter bytes already processed.
5032 				 * The rval is the return value from
5033 				 * sata_tran_start().
5034 				 * The stat indicates the overall status of
5035 				 * the operation(s).
5036 				 */
5037 				if (stat != SATA_SUCCESS)
5038 					/*
5039 					 * Page processing did not succeed -
5040 					 * all error info is already set-up,
5041 					 * just return
5042 					 */
5043 					pllen = 0; /* this breaks the loop */
5044 				else {
5045 					len += pagelen;
5046 					pllen -= pagelen;
5047 				}
5048 
5049 				break;
5050 			default:
5051 				*scsipkt->pkt_scbp = STATUS_CHECK;
5052 				sense = sata_arq_sense(spx);
5053 				sense->es_key = KEY_ILLEGAL_REQUEST;
5054 				sense->es_add_code =
5055 				    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
5056 				goto done;
5057 			}
5058 		}
5059 	}
5060 done:
5061 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5062 	/*
5063 	 * If device parameters were modified, fetch and store the new
5064 	 * Identify Device data. Since port mutex could have been released
5065 	 * for accessing HBA driver, we need to re-check device existence.
5066 	 */
5067 	if (dmod != 0) {
5068 		sata_drive_info_t new_sdinfo, *sdinfo;
5069 		int rv;
5070 
5071 		new_sdinfo.satadrv_addr =
5072 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr;
5073 		rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
5074 		    &new_sdinfo);
5075 
5076 		mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5077 		/*
5078 		 * Since port mutex could have been released when
5079 		 * accessing HBA driver, we need to re-check that the
5080 		 * framework still holds the device info structure.
5081 		 */
5082 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
5083 		    &spx->txlt_sata_pkt->satapkt_device);
5084 		if (sdinfo != NULL) {
5085 			/*
5086 			 * Device still has info structure in the
5087 			 * sata framework. Copy newly fetched info
5088 			 */
5089 			if (rv == 0) {
5090 				sdinfo->satadrv_id = new_sdinfo.satadrv_id;
5091 				sata_save_drive_settings(sdinfo);
5092 			} else {
5093 				/*
5094 				 * Could not fetch new data - invalidate
5095 				 * sata_drive_info. That makes device
5096 				 * unusable.
5097 				 */
5098 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
5099 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
5100 			}
5101 		}
5102 		if (rv != 0 || sdinfo == NULL) {
5103 			/*
5104 			 * This changes the overall mode select completion
5105 			 * reason to a failed one !!!!!
5106 			 */
5107 			*scsipkt->pkt_scbp = STATUS_CHECK;
5108 			sense = sata_arq_sense(spx);
5109 			scsipkt->pkt_reason = CMD_INCOMPLETE;
5110 			rval = TRAN_ACCEPT;
5111 		}
5112 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5113 	}
5114 	/* Restore the scsi pkt flags */
5115 	scsipkt->pkt_flags &= ~FLAG_NOINTR;
5116 	scsipkt->pkt_flags |= nointr_flag;
5117 
5118 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5119 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5120 
5121 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5122 	    scsipkt->pkt_comp != NULL)
5123 		/* scsi callback required */
5124 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5125 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5126 		    TQ_SLEEP) == NULL)
5127 			/* Scheduling the callback failed */
5128 			return (TRAN_BUSY);
5129 
5130 	return (rval);
5131 }
5132 
5133 
5134 
5135 /*
5136  * Translate command: Log Sense
5137  */
5138 static 	int
5139 sata_txlt_log_sense(sata_pkt_txlate_t *spx)
5140 {
5141 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
5142 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5143 	sata_drive_info_t *sdinfo;
5144 	struct scsi_extended_sense *sense;
5145 	int 		len, count, alc_len;
5146 	int		pc;	/* Page Control code */
5147 	int		page_code;	/* Page code */
5148 	uint8_t		*buf;	/* log sense buffer */
5149 	int		rval;
5150 #define	MAX_LOG_SENSE_PAGE_SIZE	512
5151 
5152 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5153 	    "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
5154 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
5155 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
5156 
5157 	buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
5158 
5159 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5160 
5161 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
5162 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
5163 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5164 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
5165 		return (rval);
5166 	}
5167 
5168 	scsipkt->pkt_reason = CMD_CMPLT;
5169 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5170 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5171 
5172 	pc = scsipkt->pkt_cdbp[2] >> 6;
5173 	page_code = scsipkt->pkt_cdbp[2] & 0x3f;
5174 
5175 	/* Reject not supported request for all but cumulative values */
5176 	switch (pc) {
5177 	case PC_CUMULATIVE_VALUES:
5178 		break;
5179 	default:
5180 		*scsipkt->pkt_scbp = STATUS_CHECK;
5181 		sense = sata_arq_sense(spx);
5182 		sense->es_key = KEY_ILLEGAL_REQUEST;
5183 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5184 		goto done;
5185 	}
5186 
5187 	switch (page_code) {
5188 	case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5189 	case PAGE_CODE_SELF_TEST_RESULTS:
5190 	case PAGE_CODE_INFORMATION_EXCEPTIONS:
5191 	case PAGE_CODE_SMART_READ_DATA:
5192 		break;
5193 	default:
5194 		*scsipkt->pkt_scbp = STATUS_CHECK;
5195 		sense = sata_arq_sense(spx);
5196 		sense->es_key = KEY_ILLEGAL_REQUEST;
5197 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5198 		goto done;
5199 	}
5200 
5201 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
5202 		/*
5203 		 * Because log sense uses local buffers for data retrieval from
5204 		 * the devices and sets the data programatically in the
5205 		 * original specified buffer, release preallocated DMA
5206 		 * resources before storing data in the original buffer,
5207 		 * so no unwanted DMA sync would take place.
5208 		 */
5209 		sata_id_t *sata_id;
5210 
5211 		sata_scsi_dmafree(NULL, scsipkt);
5212 
5213 		len = 0;
5214 
5215 		/* Build log parameter header */
5216 		buf[len++] = page_code;	/* page code as in the CDB */
5217 		buf[len++] = 0;		/* reserved */
5218 		buf[len++] = 0;		/* Zero out page length for now (MSB) */
5219 		buf[len++] = 0;		/* (LSB) */
5220 
5221 		sdinfo = sata_get_device_info(
5222 		    spx->txlt_sata_hba_inst,
5223 		    &spx->txlt_sata_pkt->satapkt_device);
5224 
5225 
5226 		/*
5227 		 * Add requested pages.
5228 		 */
5229 		switch (page_code) {
5230 		case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
5231 			len = sata_build_lsense_page_0(sdinfo, buf + len);
5232 			break;
5233 		case PAGE_CODE_SELF_TEST_RESULTS:
5234 			sata_id = &sdinfo->satadrv_id;
5235 			if ((! (sata_id->ai_cmdset84 &
5236 			    SATA_SMART_SELF_TEST_SUPPORTED)) ||
5237 			    (! (sata_id->ai_features87 &
5238 			    SATA_SMART_SELF_TEST_SUPPORTED))) {
5239 				*scsipkt->pkt_scbp = STATUS_CHECK;
5240 				sense = sata_arq_sense(spx);
5241 				sense->es_key = KEY_ILLEGAL_REQUEST;
5242 				sense->es_add_code =
5243 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5244 
5245 				goto done;
5246 			}
5247 			len = sata_build_lsense_page_10(sdinfo, buf + len,
5248 			    spx->txlt_sata_hba_inst);
5249 			break;
5250 		case PAGE_CODE_INFORMATION_EXCEPTIONS:
5251 			sata_id = &sdinfo->satadrv_id;
5252 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5253 				*scsipkt->pkt_scbp = STATUS_CHECK;
5254 				sense = sata_arq_sense(spx);
5255 				sense->es_key = KEY_ILLEGAL_REQUEST;
5256 				sense->es_add_code =
5257 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5258 
5259 				goto done;
5260 			}
5261 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5262 				*scsipkt->pkt_scbp = STATUS_CHECK;
5263 				sense = sata_arq_sense(spx);
5264 				sense->es_key = KEY_ABORTED_COMMAND;
5265 				sense->es_add_code =
5266 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5267 				sense->es_qual_code =
5268 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5269 
5270 				goto done;
5271 			}
5272 
5273 			len = sata_build_lsense_page_2f(sdinfo, buf + len,
5274 			    spx->txlt_sata_hba_inst);
5275 			break;
5276 		case PAGE_CODE_SMART_READ_DATA:
5277 			sata_id = &sdinfo->satadrv_id;
5278 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
5279 				*scsipkt->pkt_scbp = STATUS_CHECK;
5280 				sense = sata_arq_sense(spx);
5281 				sense->es_key = KEY_ILLEGAL_REQUEST;
5282 				sense->es_add_code =
5283 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5284 
5285 				goto done;
5286 			}
5287 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
5288 				*scsipkt->pkt_scbp = STATUS_CHECK;
5289 				sense = sata_arq_sense(spx);
5290 				sense->es_key = KEY_ABORTED_COMMAND;
5291 				sense->es_add_code =
5292 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
5293 				sense->es_qual_code =
5294 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
5295 
5296 				goto done;
5297 			}
5298 
5299 			/* This page doesn't include a page header */
5300 			len = sata_build_lsense_page_30(sdinfo, buf,
5301 			    spx->txlt_sata_hba_inst);
5302 			goto no_header;
5303 		default:
5304 			/* Invalid request */
5305 			*scsipkt->pkt_scbp = STATUS_CHECK;
5306 			sense = sata_arq_sense(spx);
5307 			sense->es_key = KEY_ILLEGAL_REQUEST;
5308 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5309 			goto done;
5310 		}
5311 
5312 		/* set parameter log sense data length */
5313 		buf[2] = len >> 8;	/* log sense length (MSB) */
5314 		buf[3] = len & 0xff;	/* log sense length (LSB) */
5315 
5316 		len += SCSI_LOG_PAGE_HDR_LEN;
5317 		ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
5318 
5319 no_header:
5320 		/* Check allocation length */
5321 		alc_len = scsipkt->pkt_cdbp[7];
5322 		alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
5323 
5324 		/*
5325 		 * We do not check for possible parameters truncation
5326 		 * (alc_len < len) assuming that the target driver works
5327 		 * correctly. Just avoiding overrun.
5328 		 * Copy no more than requested and possible, buffer-wise.
5329 		 */
5330 		count = MIN(alc_len, len);
5331 		count = MIN(bp->b_bcount, count);
5332 		bcopy(buf, bp->b_un.b_addr, count);
5333 
5334 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
5335 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
5336 	}
5337 	*scsipkt->pkt_scbp = STATUS_GOOD;
5338 done:
5339 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5340 	(void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
5341 
5342 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5343 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5344 
5345 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5346 	    scsipkt->pkt_comp != NULL)
5347 		/* scsi callback required */
5348 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5349 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5350 		    TQ_SLEEP) == NULL)
5351 			/* Scheduling the callback failed */
5352 			return (TRAN_BUSY);
5353 
5354 	return (TRAN_ACCEPT);
5355 }
5356 
5357 /*
5358  * Translate command: Log Select
5359  * Not implemented at this time - returns invalid command response.
5360  */
5361 static 	int
5362 sata_txlt_log_select(sata_pkt_txlate_t *spx)
5363 {
5364 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5365 	    "sata_txlt_log_select\n", NULL);
5366 
5367 	return (sata_txlt_invalid_command(spx));
5368 }
5369 
5370 
5371 /*
5372  * Translate command: Read (various types).
5373  * Translated into appropriate type of ATA READ command
5374  * for SATA hard disks.
5375  * Both the device capabilities and requested operation mode are
5376  * considered.
5377  *
5378  * Following scsi cdb fields are ignored:
5379  * rdprotect, dpo, fua, fua_nv, group_number.
5380  *
5381  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
5382  * enable variable sata_func_enable), the capability of the controller and
5383  * capability of a device are checked and if both support queueing, read
5384  * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
5385  * command rather than plain READ_XXX command.
5386  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
5387  * both the controller and device suport such functionality, the read
5388  * request will be translated to READ_FPDMA_QUEUED command.
5389  * In both cases the maximum queue depth is derived as minimum of:
5390  * HBA capability,device capability and sata_max_queue_depth variable setting.
5391  * The value passed to HBA driver is decremented by 1, because only 5 bits are
5392  * used to pass max queue depth value, and the maximum possible queue depth
5393  * is 32.
5394  *
5395  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
5396  * appropriate values in scsi_pkt fields.
5397  */
5398 static int
5399 sata_txlt_read(sata_pkt_txlate_t *spx)
5400 {
5401 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5402 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5403 	sata_drive_info_t *sdinfo;
5404 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
5405 	int cport = SATA_TXLT_CPORT(spx);
5406 	uint16_t sec_count;
5407 	uint64_t lba;
5408 	int rval;
5409 	int synch;
5410 
5411 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5412 
5413 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
5414 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
5415 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5416 		return (rval);
5417 	}
5418 
5419 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
5420 	    &spx->txlt_sata_pkt->satapkt_device);
5421 
5422 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
5423 	/*
5424 	 * Extract LBA and sector count from scsi CDB.
5425 	 */
5426 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
5427 	case SCMD_READ:
5428 		/* 6-byte scsi read cmd : 0x08 */
5429 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
5430 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
5431 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5432 		sec_count = scsipkt->pkt_cdbp[4];
5433 		/* sec_count 0 will be interpreted as 256 by a device */
5434 		break;
5435 	case SCMD_READ_G1:
5436 		/* 10-bytes scsi read command : 0x28 */
5437 		lba = scsipkt->pkt_cdbp[2];
5438 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5439 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5440 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5441 		sec_count = scsipkt->pkt_cdbp[7];
5442 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
5443 		break;
5444 	case SCMD_READ_G5:
5445 		/* 12-bytes scsi read command : 0xA8 */
5446 		lba = scsipkt->pkt_cdbp[2];
5447 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5448 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5449 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5450 		sec_count = scsipkt->pkt_cdbp[6];
5451 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
5452 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
5453 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
5454 		break;
5455 	case SCMD_READ_G4:
5456 		/* 16-bytes scsi read command : 0x88 */
5457 		lba = scsipkt->pkt_cdbp[2];
5458 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5459 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5460 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5461 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
5462 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5463 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5464 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
5465 		sec_count = scsipkt->pkt_cdbp[10];
5466 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
5467 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
5468 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
5469 		break;
5470 	default:
5471 		/* Unsupported command */
5472 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5473 		return (sata_txlt_invalid_command(spx));
5474 	}
5475 
5476 	/*
5477 	 * Check if specified address exceeds device capacity
5478 	 */
5479 	if ((lba >= sdinfo->satadrv_capacity) ||
5480 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
5481 		/* LBA out of range */
5482 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5483 		return (sata_txlt_lba_out_of_range(spx));
5484 	}
5485 
5486 	/*
5487 	 * For zero-length transfer, emulate good completion of the command
5488 	 * (reasons for rejecting the command were already checked).
5489 	 * No DMA resources were allocated.
5490 	 */
5491 	if (spx->txlt_dma_cookie_list == NULL) {
5492 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5493 		return (sata_emul_rw_completion(spx));
5494 	}
5495 
5496 	/*
5497 	 * Build cmd block depending on the device capability and
5498 	 * requested operation mode.
5499 	 * Do not bother with non-dma mode - we are working only with
5500 	 * devices supporting DMA.
5501 	 */
5502 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
5503 	scmd->satacmd_device_reg = SATA_ADH_LBA;
5504 	scmd->satacmd_cmd_reg = SATAC_READ_DMA;
5505 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
5506 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
5507 		scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT;
5508 		scmd->satacmd_sec_count_msb = sec_count >> 8;
5509 #ifndef __lock_lint
5510 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
5511 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
5512 		scmd->satacmd_lba_high_msb = lba >> 40;
5513 #endif
5514 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
5515 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
5516 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
5517 	}
5518 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
5519 	scmd->satacmd_lba_low_lsb = lba & 0xff;
5520 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
5521 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
5522 	scmd->satacmd_features_reg = 0;
5523 	scmd->satacmd_status_reg = 0;
5524 	scmd->satacmd_error_reg = 0;
5525 
5526 	/*
5527 	 * Check if queueing commands should be used and switch
5528 	 * to appropriate command if possible
5529 	 */
5530 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
5531 		boolean_t using_queuing;
5532 
5533 		/* Queuing supported by controller and device? */
5534 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
5535 		    (sdinfo->satadrv_features_support &
5536 		    SATA_DEV_F_NCQ) &&
5537 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
5538 		    SATA_CTLF_NCQ)) {
5539 			using_queuing = B_TRUE;
5540 
5541 			/* NCQ supported - use FPDMA READ */
5542 			scmd->satacmd_cmd_reg =
5543 			    SATAC_READ_FPDMA_QUEUED;
5544 			scmd->satacmd_features_reg_ext =
5545 			    scmd->satacmd_sec_count_msb;
5546 			scmd->satacmd_sec_count_msb = 0;
5547 		} else if ((sdinfo->satadrv_features_support &
5548 		    SATA_DEV_F_TCQ) &&
5549 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
5550 		    SATA_CTLF_QCMD)) {
5551 			using_queuing = B_TRUE;
5552 
5553 			/* Legacy queueing */
5554 			if (sdinfo->satadrv_features_support &
5555 			    SATA_DEV_F_LBA48) {
5556 				scmd->satacmd_cmd_reg =
5557 				    SATAC_READ_DMA_QUEUED_EXT;
5558 				scmd->satacmd_features_reg_ext =
5559 				    scmd->satacmd_sec_count_msb;
5560 				scmd->satacmd_sec_count_msb = 0;
5561 			} else {
5562 				scmd->satacmd_cmd_reg =
5563 				    SATAC_READ_DMA_QUEUED;
5564 			}
5565 		} else	/* NCQ nor legacy queuing not supported */
5566 			using_queuing = B_FALSE;
5567 
5568 		/*
5569 		 * If queuing, the sector count goes in the features register
5570 		 * and the secount count will contain the tag.
5571 		 */
5572 		if (using_queuing) {
5573 			scmd->satacmd_features_reg =
5574 			    scmd->satacmd_sec_count_lsb;
5575 			scmd->satacmd_sec_count_lsb = 0;
5576 			scmd->satacmd_flags.sata_queued = B_TRUE;
5577 
5578 			/* Set-up maximum queue depth */
5579 			scmd->satacmd_flags.sata_max_queue_depth =
5580 			    sdinfo->satadrv_max_queue_depth - 1;
5581 		} else if (sdinfo->satadrv_features_enabled &
5582 		    SATA_DEV_F_E_UNTAGGED_QING) {
5583 			/*
5584 			 * Although NCQ/TCQ is not enabled, untagged queuing
5585 			 * may be still used.
5586 			 * Set-up the maximum untagged queue depth.
5587 			 * Use controller's queue depth from sata_hba_tran.
5588 			 * SATA HBA drivers may ignore this value and rely on
5589 			 * the internal limits.For drivers that do not
5590 			 * ignore untaged queue depth, limit the value to
5591 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
5592 			 * largest value that can be passed via
5593 			 * satacmd_flags.sata_max_queue_depth.
5594 			 */
5595 			scmd->satacmd_flags.sata_max_queue_depth =
5596 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
5597 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
5598 
5599 		} else {
5600 			scmd->satacmd_flags.sata_max_queue_depth = 0;
5601 		}
5602 	} else
5603 		scmd->satacmd_flags.sata_max_queue_depth = 0;
5604 
5605 	SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst,
5606 	    "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n",
5607 	    scmd->satacmd_cmd_reg, lba, sec_count);
5608 
5609 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5610 		/* Need callback function */
5611 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
5612 		synch = FALSE;
5613 	} else
5614 		synch = TRUE;
5615 
5616 	/* Transfer command to HBA */
5617 	if (sata_hba_start(spx, &rval) != 0) {
5618 		/* Pkt not accepted for execution */
5619 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5620 		return (rval);
5621 	}
5622 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5623 	/*
5624 	 * If execution is non-synchronous,
5625 	 * a callback function will handle potential errors, translate
5626 	 * the response and will do a callback to a target driver.
5627 	 * If it was synchronous, check execution status using the same
5628 	 * framework callback.
5629 	 */
5630 	if (synch) {
5631 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5632 		    "synchronous execution status %x\n",
5633 		    spx->txlt_sata_pkt->satapkt_reason);
5634 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
5635 	}
5636 	return (TRAN_ACCEPT);
5637 }
5638 
5639 
5640 /*
5641  * SATA translate command: Write (various types)
5642  * Translated into appropriate type of ATA WRITE command
5643  * for SATA hard disks.
5644  * Both the device capabilities and requested operation mode are
5645  * considered.
5646  *
5647  * Following scsi cdb fields are ignored:
5648  * rwprotect, dpo, fua, fua_nv, group_number.
5649  *
5650  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
5651  * enable variable sata_func_enable), the capability of the controller and
5652  * capability of a device are checked and if both support queueing, write
5653  * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT
5654  * command rather than plain WRITE_XXX command.
5655  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
5656  * both the controller and device suport such functionality, the write
5657  * request will be translated to WRITE_FPDMA_QUEUED command.
5658  * In both cases the maximum queue depth is derived as minimum of:
5659  * HBA capability,device capability and sata_max_queue_depth variable setting.
5660  * The value passed to HBA driver is decremented by 1, because only 5 bits are
5661  * used to pass max queue depth value, and the maximum possible queue depth
5662  * is 32.
5663  *
5664  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
5665  * appropriate values in scsi_pkt fields.
5666  */
5667 static int
5668 sata_txlt_write(sata_pkt_txlate_t *spx)
5669 {
5670 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5671 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5672 	sata_drive_info_t *sdinfo;
5673 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
5674 	int cport = SATA_TXLT_CPORT(spx);
5675 	uint16_t sec_count;
5676 	uint64_t lba;
5677 	int rval;
5678 	int synch;
5679 
5680 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5681 
5682 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
5683 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
5684 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5685 		return (rval);
5686 	}
5687 
5688 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
5689 	    &spx->txlt_sata_pkt->satapkt_device);
5690 
5691 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
5692 	/*
5693 	 * Extract LBA and sector count from scsi CDB
5694 	 */
5695 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
5696 	case SCMD_WRITE:
5697 		/* 6-byte scsi read cmd : 0x0A */
5698 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
5699 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
5700 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5701 		sec_count = scsipkt->pkt_cdbp[4];
5702 		/* sec_count 0 will be interpreted as 256 by a device */
5703 		break;
5704 	case SCMD_WRITE_G1:
5705 		/* 10-bytes scsi write command : 0x2A */
5706 		lba = scsipkt->pkt_cdbp[2];
5707 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5708 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5709 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5710 		sec_count = scsipkt->pkt_cdbp[7];
5711 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
5712 		break;
5713 	case SCMD_WRITE_G5:
5714 		/* 12-bytes scsi read command : 0xAA */
5715 		lba = scsipkt->pkt_cdbp[2];
5716 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5717 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5718 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5719 		sec_count = scsipkt->pkt_cdbp[6];
5720 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
5721 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
5722 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
5723 		break;
5724 	case SCMD_WRITE_G4:
5725 		/* 16-bytes scsi write command : 0x8A */
5726 		lba = scsipkt->pkt_cdbp[2];
5727 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
5728 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
5729 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
5730 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
5731 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
5732 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
5733 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
5734 		sec_count = scsipkt->pkt_cdbp[10];
5735 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
5736 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
5737 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
5738 		break;
5739 	default:
5740 		/* Unsupported command */
5741 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5742 		return (sata_txlt_invalid_command(spx));
5743 	}
5744 
5745 	/*
5746 	 * Check if specified address and length exceeds device capacity
5747 	 */
5748 	if ((lba >= sdinfo->satadrv_capacity) ||
5749 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
5750 		/* LBA out of range */
5751 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5752 		return (sata_txlt_lba_out_of_range(spx));
5753 	}
5754 
5755 	/*
5756 	 * For zero-length transfer, emulate good completion of the command
5757 	 * (reasons for rejecting the command were already checked).
5758 	 * No DMA resources were allocated.
5759 	 */
5760 	if (spx->txlt_dma_cookie_list == NULL) {
5761 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5762 		return (sata_emul_rw_completion(spx));
5763 	}
5764 
5765 	/*
5766 	 * Build cmd block depending on the device capability and
5767 	 * requested operation mode.
5768 	 * Do not bother with non-dma mode- we are working only with
5769 	 * devices supporting DMA.
5770 	 */
5771 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
5772 	scmd->satacmd_device_reg = SATA_ADH_LBA;
5773 	scmd->satacmd_cmd_reg = SATAC_WRITE_DMA;
5774 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
5775 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
5776 		scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT;
5777 		scmd->satacmd_sec_count_msb = sec_count >> 8;
5778 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
5779 #ifndef __lock_lint
5780 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
5781 		scmd->satacmd_lba_high_msb = lba >> 40;
5782 #endif
5783 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
5784 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
5785 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
5786 	}
5787 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
5788 	scmd->satacmd_lba_low_lsb = lba & 0xff;
5789 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
5790 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
5791 	scmd->satacmd_features_reg = 0;
5792 	scmd->satacmd_status_reg = 0;
5793 	scmd->satacmd_error_reg = 0;
5794 
5795 	/*
5796 	 * Check if queueing commands should be used and switch
5797 	 * to appropriate command if possible
5798 	 */
5799 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
5800 		boolean_t using_queuing;
5801 
5802 		/* Queuing supported by controller and device? */
5803 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
5804 		    (sdinfo->satadrv_features_support &
5805 		    SATA_DEV_F_NCQ) &&
5806 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
5807 		    SATA_CTLF_NCQ)) {
5808 			using_queuing = B_TRUE;
5809 
5810 			/* NCQ supported - use FPDMA WRITE */
5811 			scmd->satacmd_cmd_reg =
5812 			    SATAC_WRITE_FPDMA_QUEUED;
5813 			scmd->satacmd_features_reg_ext =
5814 			    scmd->satacmd_sec_count_msb;
5815 			scmd->satacmd_sec_count_msb = 0;
5816 		} else if ((sdinfo->satadrv_features_support &
5817 		    SATA_DEV_F_TCQ) &&
5818 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
5819 		    SATA_CTLF_QCMD)) {
5820 			using_queuing = B_TRUE;
5821 
5822 			/* Legacy queueing */
5823 			if (sdinfo->satadrv_features_support &
5824 			    SATA_DEV_F_LBA48) {
5825 				scmd->satacmd_cmd_reg =
5826 				    SATAC_WRITE_DMA_QUEUED_EXT;
5827 				scmd->satacmd_features_reg_ext =
5828 				    scmd->satacmd_sec_count_msb;
5829 				scmd->satacmd_sec_count_msb = 0;
5830 			} else {
5831 				scmd->satacmd_cmd_reg =
5832 				    SATAC_WRITE_DMA_QUEUED;
5833 			}
5834 		} else	/*  NCQ nor legacy queuing not supported */
5835 			using_queuing = B_FALSE;
5836 
5837 		if (using_queuing) {
5838 			scmd->satacmd_features_reg =
5839 			    scmd->satacmd_sec_count_lsb;
5840 			scmd->satacmd_sec_count_lsb = 0;
5841 			scmd->satacmd_flags.sata_queued = B_TRUE;
5842 			/* Set-up maximum queue depth */
5843 			scmd->satacmd_flags.sata_max_queue_depth =
5844 			    sdinfo->satadrv_max_queue_depth - 1;
5845 		} else if (sdinfo->satadrv_features_enabled &
5846 		    SATA_DEV_F_E_UNTAGGED_QING) {
5847 			/*
5848 			 * Although NCQ/TCQ is not enabled, untagged queuing
5849 			 * may be still used.
5850 			 * Set-up the maximum untagged queue depth.
5851 			 * Use controller's queue depth from sata_hba_tran.
5852 			 * SATA HBA drivers may ignore this value and rely on
5853 			 * the internal limits. For drivera that do not
5854 			 * ignore untaged queue depth, limit the value to
5855 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
5856 			 * largest value that can be passed via
5857 			 * satacmd_flags.sata_max_queue_depth.
5858 			 */
5859 			scmd->satacmd_flags.sata_max_queue_depth =
5860 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
5861 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
5862 
5863 		} else {
5864 			scmd->satacmd_flags.sata_max_queue_depth = 0;
5865 		}
5866 	} else
5867 		scmd->satacmd_flags.sata_max_queue_depth = 0;
5868 
5869 	SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5870 	    "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n",
5871 	    scmd->satacmd_cmd_reg, lba, sec_count);
5872 
5873 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5874 		/* Need callback function */
5875 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
5876 		synch = FALSE;
5877 	} else
5878 		synch = TRUE;
5879 
5880 	/* Transfer command to HBA */
5881 	if (sata_hba_start(spx, &rval) != 0) {
5882 		/* Pkt not accepted for execution */
5883 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5884 		return (rval);
5885 	}
5886 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5887 
5888 	/*
5889 	 * If execution is non-synchronous,
5890 	 * a callback function will handle potential errors, translate
5891 	 * the response and will do a callback to a target driver.
5892 	 * If it was synchronous, check execution status using the same
5893 	 * framework callback.
5894 	 */
5895 	if (synch) {
5896 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5897 		    "synchronous execution status %x\n",
5898 		    spx->txlt_sata_pkt->satapkt_reason);
5899 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
5900 	}
5901 	return (TRAN_ACCEPT);
5902 }
5903 
5904 
5905 /*
5906  * Implements SCSI SBC WRITE BUFFER command download microcode option
5907  */
5908 static int
5909 sata_txlt_write_buffer(sata_pkt_txlate_t *spx)
5910 {
5911 #define	WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE			4
5912 #define	WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE			5
5913 
5914 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5915 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5916 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5917 	struct scsi_extended_sense *sense;
5918 	int rval, mode, sector_count;
5919 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
5920 	int cport = SATA_TXLT_CPORT(spx);
5921 	boolean_t synch;
5922 
5923 	synch = (spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH) != 0;
5924 	mode = scsipkt->pkt_cdbp[1] & 0x1f;
5925 
5926 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5927 	    "sata_txlt_write_buffer, mode 0x%x\n", mode);
5928 
5929 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5930 
5931 	if ((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) {
5932 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5933 		return (rval);
5934 	}
5935 
5936 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
5937 
5938 	scsipkt->pkt_reason = CMD_CMPLT;
5939 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5940 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5941 
5942 	/*
5943 	 * The SCSI to ATA translation specification only calls
5944 	 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE.
5945 	 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but
5946 	 * ATA 8 (draft) got rid of download microcode for temp
5947 	 * and it is even optional for ATA 7, so it may be aborted.
5948 	 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as
5949 	 * it is not specified and the buffer offset for SCSI is a 16-bit
5950 	 * value in bytes, but for ATA it is a 16-bit offset in 512 byte
5951 	 * sectors.  Thus the offset really doesn't buy us anything.
5952 	 * If and when ATA 8 is stabilized and the SCSI to ATA specification
5953 	 * is revised, this can be revisisted.
5954 	 */
5955 	/* Reject not supported request */
5956 	switch (mode) {
5957 	case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE:
5958 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP;
5959 		break;
5960 	case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE:
5961 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE;
5962 		break;
5963 	default:
5964 		goto bad_param;
5965 	}
5966 
5967 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
5968 
5969 	scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE;
5970 	if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0)
5971 		goto bad_param;
5972 	sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE;
5973 	scmd->satacmd_sec_count_lsb = (uint8_t)sector_count;
5974 	scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8;
5975 	scmd->satacmd_lba_mid_lsb = 0;
5976 	scmd->satacmd_lba_high_lsb = 0;
5977 	scmd->satacmd_device_reg = 0;
5978 	spx->txlt_sata_pkt->satapkt_comp =
5979 	    sata_txlt_download_mcode_cmd_completion;
5980 	scmd->satacmd_addr_type = 0;
5981 
5982 	/* Transfer command to HBA */
5983 	if (sata_hba_start(spx, &rval) != 0) {
5984 		/* Pkt not accepted for execution */
5985 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5986 		return (rval);
5987 	}
5988 
5989 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5990 	/*
5991 	 * If execution is non-synchronous,
5992 	 * a callback function will handle potential errors, translate
5993 	 * the response and will do a callback to a target driver.
5994 	 * If it was synchronous, check execution status using the same
5995 	 * framework callback.
5996 	 */
5997 	if (synch) {
5998 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5999 		    "synchronous execution\n", NULL);
6000 		/* Calling pre-set completion routine */
6001 		(*spx->txlt_sata_pkt->satapkt_comp)(spx->txlt_sata_pkt);
6002 	}
6003 	return (TRAN_ACCEPT);
6004 
6005 bad_param:
6006 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6007 	*scsipkt->pkt_scbp = STATUS_CHECK;
6008 	sense = sata_arq_sense(spx);
6009 	sense->es_key = KEY_ILLEGAL_REQUEST;
6010 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6011 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6012 	    scsipkt->pkt_comp != NULL) {
6013 		/* scsi callback required */
6014 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6015 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
6016 		    TQ_SLEEP) == 0) {
6017 			/* Scheduling the callback failed */
6018 			rval = TRAN_BUSY;
6019 		}
6020 	}
6021 	return (rval);
6022 }
6023 
6024 
6025 /*
6026  * Retry identify device when command returns SATA_INCOMPLETE_DATA
6027  * after doing a firmware download.
6028  */
6029 static void
6030 sata_retry_identify_device(void *arg)
6031 {
6032 #define	DOWNLOAD_WAIT_TIME_SECS	60
6033 #define	DOWNLOAD_WAIT_INTERVAL_SECS	1
6034 	int rval;
6035 	int retry_cnt;
6036 	sata_pkt_t *sata_pkt = (sata_pkt_t *)arg;
6037 	sata_pkt_txlate_t *spx =
6038 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
6039 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6040 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
6041 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
6042 	sata_drive_info_t *sdinfo;
6043 
6044 	/*
6045 	 * Before returning good status, probe device.
6046 	 * Device probing will get IDENTIFY DEVICE data, if possible.
6047 	 * The assumption is that the new microcode is applied by the
6048 	 * device. It is a caller responsibility to verify this.
6049 	 */
6050 	for (retry_cnt = 0;
6051 	    retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS;
6052 	    retry_cnt++) {
6053 		rval = sata_probe_device(sata_hba_inst, &sata_device);
6054 
6055 		if (rval == SATA_SUCCESS) { /* Set default features */
6056 			sdinfo = sata_get_device_info(sata_hba_inst,
6057 			    &sata_device);
6058 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
6059 			    SATA_SUCCESS) {
6060 				/* retry */
6061 				(void) sata_initialize_device(sata_hba_inst,
6062 				    sdinfo);
6063 			}
6064 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6065 			    scsipkt->pkt_comp != NULL)
6066 				(*scsipkt->pkt_comp)(scsipkt);
6067 			return;
6068 		} else if (rval == SATA_RETRY) {
6069 			delay(drv_usectohz(1000000 *
6070 			    DOWNLOAD_WAIT_INTERVAL_SECS));
6071 			continue;
6072 		} else	/* failed - no reason to retry */
6073 			break;
6074 	}
6075 
6076 	/*
6077 	 * Something went wrong, device probing failed.
6078 	 */
6079 	SATA_LOG_D((sata_hba_inst, CE_WARN,
6080 	    "Cannot probe device after downloading microcode\n"));
6081 
6082 	/* Reset device to force retrying the probe. */
6083 	(void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst))
6084 	    (SATA_DIP(sata_hba_inst), &sata_device);
6085 
6086 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6087 	    scsipkt->pkt_comp != NULL)
6088 		(*scsipkt->pkt_comp)(scsipkt);
6089 }
6090 
6091 /*
6092  * Translate completion status of download microcode command.
6093  * pkt completion_reason is checked to determine the completion status.
6094  * Do scsi callback if necessary (FLAG_NOINTR == 0)
6095  *
6096  * Note: this function may be called also for synchronously executed
6097  * command.
6098  * This function may be used only if scsi_pkt is non-NULL.
6099  */
6100 static void
6101 sata_txlt_download_mcode_cmd_completion(sata_pkt_t *sata_pkt)
6102 {
6103 	sata_pkt_txlate_t *spx =
6104 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
6105 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6106 	struct scsi_extended_sense *sense;
6107 	sata_drive_info_t *sdinfo;
6108 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
6109 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
6110 	int rval;
6111 
6112 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6113 	    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
6114 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
6115 		scsipkt->pkt_reason = CMD_CMPLT;
6116 
6117 		rval = sata_probe_device(sata_hba_inst, &sata_device);
6118 
6119 		if (rval == SATA_SUCCESS) { /* Set default features */
6120 			sdinfo = sata_get_device_info(sata_hba_inst,
6121 			    &sata_device);
6122 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
6123 			    SATA_SUCCESS) {
6124 				/* retry */
6125 				(void) sata_initialize_device(sata_hba_inst,
6126 				    sdinfo);
6127 			}
6128 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6129 			    scsipkt->pkt_comp != NULL)
6130 				(*scsipkt->pkt_comp)(scsipkt);
6131 		} else {
6132 			(void) ddi_taskq_dispatch(
6133 			    (ddi_taskq_t *)SATA_TXLT_TASKQ(spx),
6134 			    sata_retry_identify_device,
6135 			    (void *)sata_pkt, TQ_NOSLEEP);
6136 		}
6137 
6138 
6139 	} else {
6140 		/* Something went wrong, microcode download command failed */
6141 		scsipkt->pkt_reason = CMD_INCOMPLETE;
6142 		*scsipkt->pkt_scbp = STATUS_CHECK;
6143 		sense = sata_arq_sense(spx);
6144 		switch (sata_pkt->satapkt_reason) {
6145 		case SATA_PKT_PORT_ERROR:
6146 			/*
6147 			 * We have no device data. Assume no data transfered.
6148 			 */
6149 			sense->es_key = KEY_HARDWARE_ERROR;
6150 			break;
6151 
6152 		case SATA_PKT_DEV_ERROR:
6153 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
6154 			    SATA_STATUS_ERR) {
6155 				/*
6156 				 * determine dev error reason from error
6157 				 * reg content
6158 				 */
6159 				sata_decode_device_error(spx, sense);
6160 				break;
6161 			}
6162 			/* No extended sense key - no info available */
6163 			break;
6164 
6165 		case SATA_PKT_TIMEOUT:
6166 			/* scsipkt->pkt_reason = CMD_TIMEOUT; */
6167 			scsipkt->pkt_reason = CMD_INCOMPLETE;
6168 			/* No extended sense key ? */
6169 			break;
6170 
6171 		case SATA_PKT_ABORTED:
6172 			scsipkt->pkt_reason = CMD_ABORTED;
6173 			/* No extended sense key ? */
6174 			break;
6175 
6176 		case SATA_PKT_RESET:
6177 			/* pkt aborted by an explicit reset from a host */
6178 			scsipkt->pkt_reason = CMD_RESET;
6179 			break;
6180 
6181 		default:
6182 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
6183 			    "sata_txlt_nodata_cmd_completion: "
6184 			    "invalid packet completion reason %d",
6185 			    sata_pkt->satapkt_reason));
6186 			scsipkt->pkt_reason = CMD_TRAN_ERR;
6187 			break;
6188 		}
6189 
6190 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6191 		    "scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6192 
6193 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6194 		    scsipkt->pkt_comp != NULL)
6195 			/* scsi callback required */
6196 			(*scsipkt->pkt_comp)(scsipkt);
6197 	}
6198 }
6199 
6200 
6201 
6202 
6203 /*
6204  * Translate command: Synchronize Cache.
6205  * Translates into Flush Cache command for SATA hard disks.
6206  *
6207  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
6208  * appropriate values in scsi_pkt fields.
6209  */
6210 static 	int
6211 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx)
6212 {
6213 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6214 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
6215 	int cport = SATA_TXLT_CPORT(spx);
6216 	int rval;
6217 	int synch;
6218 
6219 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
6220 
6221 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
6222 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
6223 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6224 		return (rval);
6225 	}
6226 
6227 	scmd->satacmd_addr_type = 0;
6228 	scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE;
6229 	scmd->satacmd_device_reg = 0;
6230 	scmd->satacmd_sec_count_lsb = 0;
6231 	scmd->satacmd_lba_low_lsb = 0;
6232 	scmd->satacmd_lba_mid_lsb = 0;
6233 	scmd->satacmd_lba_high_lsb = 0;
6234 	scmd->satacmd_features_reg = 0;
6235 	scmd->satacmd_status_reg = 0;
6236 	scmd->satacmd_error_reg = 0;
6237 
6238 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6239 	    "sata_txlt_synchronize_cache\n", NULL);
6240 
6241 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6242 		/* Need to set-up a callback function */
6243 		spx->txlt_sata_pkt->satapkt_comp =
6244 		    sata_txlt_nodata_cmd_completion;
6245 		synch = FALSE;
6246 	} else
6247 		synch = TRUE;
6248 
6249 	/* Transfer command to HBA */
6250 	if (sata_hba_start(spx, &rval) != 0) {
6251 		/* Pkt not accepted for execution */
6252 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
6253 		return (rval);
6254 	}
6255 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
6256 
6257 	/*
6258 	 * If execution non-synchronous, it had to be completed
6259 	 * a callback function will handle potential errors, translate
6260 	 * the response and will do a callback to a target driver.
6261 	 * If it was synchronous, check status, using the same
6262 	 * framework callback.
6263 	 */
6264 	if (synch) {
6265 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6266 		    "synchronous execution status %x\n",
6267 		    spx->txlt_sata_pkt->satapkt_reason);
6268 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
6269 	}
6270 	return (TRAN_ACCEPT);
6271 }
6272 
6273 
6274 /*
6275  * Send pkt to SATA HBA driver
6276  *
6277  * This function may be called only if the operation is requested by scsi_pkt,
6278  * i.e. scsi_pkt is not NULL.
6279  *
6280  * This function has to be called with cport mutex held. It does release
6281  * the mutex when it calls HBA driver sata_tran_start function and
6282  * re-acquires it afterwards.
6283  *
6284  * If return value is 0, pkt was accepted, -1 otherwise
6285  * rval is set to appropriate sata_scsi_start return value.
6286  *
6287  * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not
6288  * have called the sata_pkt callback function for this packet.
6289  *
6290  * The scsi callback has to be performed by the caller of this routine.
6291  *
6292  * Note 2: No port multiplier support for now.
6293  */
6294 static int
6295 sata_hba_start(sata_pkt_txlate_t *spx, int *rval)
6296 {
6297 	int stat, cport;
6298 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
6299 	sata_drive_info_t *sdinfo;
6300 	sata_device_t *sata_device;
6301 	uint8_t cmd;
6302 	struct sata_cmd_flags cmd_flags;
6303 
6304 	ASSERT(spx->txlt_sata_pkt != NULL);
6305 
6306 	cport = SATA_TXLT_CPORT(spx);
6307 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6308 
6309 	sdinfo = sata_get_device_info(sata_hba_inst,
6310 	    &spx->txlt_sata_pkt->satapkt_device);
6311 	ASSERT(sdinfo != NULL);
6312 
6313 	/* Clear device reset state? */
6314 	if (sdinfo->satadrv_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) {
6315 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
6316 		    sata_clear_dev_reset = B_TRUE;
6317 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_CLEAR_DEVICE_RESET;
6318 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6319 		    "sata_hba_start: clearing device reset state\n", NULL);
6320 	}
6321 	cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg;
6322 	cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags;
6323 	sata_device = &spx->txlt_sata_pkt->satapkt_device;
6324 
6325 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6326 
6327 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6328 	    "Sata cmd 0x%2x\n", cmd);
6329 
6330 	stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
6331 	    spx->txlt_sata_pkt);
6332 
6333 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6334 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
6335 	/*
6336 	 * If sata pkt was accepted and executed in asynchronous mode, i.e.
6337 	 * with the sata callback, the sata_pkt could be already destroyed
6338 	 * by the time we check ther return status from the hba_start()
6339 	 * function, because sata_scsi_destroy_pkt() could have been already
6340 	 * called (perhaps in the interrupt context). So, in such case, there
6341 	 * should be no references to it. In other cases, sata_pkt still
6342 	 * exists.
6343 	 */
6344 	switch (stat) {
6345 	case SATA_TRAN_ACCEPTED:
6346 		/*
6347 		 * pkt accepted for execution.
6348 		 * If it was executed synchronously, it is already completed
6349 		 * and pkt completion_reason indicates completion status.
6350 		 */
6351 		*rval = TRAN_ACCEPT;
6352 		return (0);
6353 
6354 	case SATA_TRAN_QUEUE_FULL:
6355 		/*
6356 		 * Controller detected queue full condition.
6357 		 */
6358 		SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst,
6359 		    "sata_hba_start: queue full\n", NULL);
6360 
6361 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
6362 		*spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL;
6363 
6364 		*rval = TRAN_BUSY;
6365 		break;
6366 
6367 	case SATA_TRAN_PORT_ERROR:
6368 		/*
6369 		 * Communication/link with device or general port error
6370 		 * detected before pkt execution begun.
6371 		 */
6372 		if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
6373 		    SATA_ADDR_CPORT ||
6374 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
6375 		    SATA_ADDR_DCPORT)
6376 			sata_log(sata_hba_inst, CE_CONT,
6377 			    "SATA port %d error",
6378 			    sata_device->satadev_addr.cport);
6379 		else
6380 			sata_log(sata_hba_inst, CE_CONT,
6381 			    "SATA port %d pmport %d error\n",
6382 			    sata_device->satadev_addr.cport,
6383 			    sata_device->satadev_addr.pmport);
6384 
6385 		/*
6386 		 * Update the port/device structure.
6387 		 * sata_pkt should be still valid. Since port error is
6388 		 * returned, sata_device content should reflect port
6389 		 * state - it means, that sata address have been changed,
6390 		 * because original packet's sata address refered to a device
6391 		 * attached to some port.
6392 		 */
6393 		sata_update_port_info(sata_hba_inst, sata_device);
6394 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
6395 		*rval = TRAN_FATAL_ERROR;
6396 		break;
6397 
6398 	case SATA_TRAN_CMD_UNSUPPORTED:
6399 		/*
6400 		 * Command rejected by HBA as unsupported. It was HBA driver
6401 		 * that rejected the command, command was not sent to
6402 		 * an attached device.
6403 		 */
6404 		if ((sdinfo != NULL) &&
6405 		    (sdinfo->satadrv_state & SATA_DSTATE_RESET))
6406 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6407 			    "sat_hba_start: cmd 0x%2x rejected "
6408 			    "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd);
6409 
6410 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6411 		(void) sata_txlt_invalid_command(spx);
6412 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
6413 
6414 		*rval = TRAN_ACCEPT;
6415 		break;
6416 
6417 	case SATA_TRAN_BUSY:
6418 		/*
6419 		 * Command rejected by HBA because other operation prevents
6420 		 * accepting the packet, or device is in RESET condition.
6421 		 */
6422 		if (sdinfo != NULL) {
6423 			sdinfo->satadrv_state =
6424 			    spx->txlt_sata_pkt->satapkt_device.satadev_state;
6425 
6426 			if (sdinfo->satadrv_state & SATA_DSTATE_RESET) {
6427 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6428 				    "sata_hba_start: cmd 0x%2x rejected "
6429 				    "because of device reset condition\n",
6430 				    cmd);
6431 			} else {
6432 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
6433 				    "sata_hba_start: cmd 0x%2x rejected "
6434 				    "with SATA_TRAN_BUSY status\n",
6435 				    cmd);
6436 			}
6437 		}
6438 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
6439 		*rval = TRAN_BUSY;
6440 		break;
6441 
6442 	default:
6443 		/* Unrecognized HBA response */
6444 		SATA_LOG_D((sata_hba_inst, CE_WARN,
6445 		    "sata_hba_start: unrecognized HBA response "
6446 		    "to cmd : 0x%2x resp 0x%x", cmd, rval));
6447 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
6448 		*rval = TRAN_FATAL_ERROR;
6449 		break;
6450 	}
6451 
6452 	/*
6453 	 * If we got here, the packet was rejected.
6454 	 * Check if we need to remember reset state clearing request
6455 	 */
6456 	if (cmd_flags.sata_clear_dev_reset) {
6457 		/*
6458 		 * Check if device is still configured - it may have
6459 		 * disapeared from the configuration
6460 		 */
6461 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
6462 		if (sdinfo != NULL) {
6463 			/*
6464 			 * Restore the flag that requests clearing of
6465 			 * the device reset state,
6466 			 * so the next sata packet may carry it to HBA.
6467 			 */
6468 			sdinfo->satadrv_event_flags |=
6469 			    SATA_EVNT_CLEAR_DEVICE_RESET;
6470 		}
6471 	}
6472 	return (-1);
6473 }
6474 
6475 /*
6476  * Scsi response setup for invalid LBA
6477  *
6478  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
6479  */
6480 static int
6481 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx)
6482 {
6483 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6484 	struct scsi_extended_sense *sense;
6485 
6486 	scsipkt->pkt_reason = CMD_CMPLT;
6487 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6488 	    STATE_SENT_CMD | STATE_GOT_STATUS;
6489 	*scsipkt->pkt_scbp = STATUS_CHECK;
6490 
6491 	*scsipkt->pkt_scbp = STATUS_CHECK;
6492 	sense = sata_arq_sense(spx);
6493 	sense->es_key = KEY_ILLEGAL_REQUEST;
6494 	sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
6495 
6496 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6497 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6498 
6499 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6500 	    scsipkt->pkt_comp != NULL)
6501 		/* scsi callback required */
6502 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6503 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
6504 		    TQ_SLEEP) == NULL)
6505 			/* Scheduling the callback failed */
6506 			return (TRAN_BUSY);
6507 	return (TRAN_ACCEPT);
6508 }
6509 
6510 
6511 /*
6512  * Analyze device status and error registers and translate them into
6513  * appropriate scsi sense codes.
6514  * NOTE: non-packet commands only for now
6515  */
6516 static void
6517 sata_decode_device_error(sata_pkt_txlate_t *spx,
6518     struct scsi_extended_sense *sense)
6519 {
6520 	uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg;
6521 
6522 	ASSERT(sense != NULL);
6523 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
6524 	    SATA_STATUS_ERR);
6525 
6526 
6527 	if (err_reg & SATA_ERROR_ICRC) {
6528 		sense->es_key = KEY_ABORTED_COMMAND;
6529 		sense->es_add_code = 0x08; /* Communication failure */
6530 		return;
6531 	}
6532 
6533 	if (err_reg & SATA_ERROR_UNC) {
6534 		sense->es_key = KEY_MEDIUM_ERROR;
6535 		/* Information bytes (LBA) need to be set by a caller */
6536 		return;
6537 	}
6538 
6539 	/* ADD HERE: MC error bit handling for ATAPI CD/DVD */
6540 	if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) {
6541 		sense->es_key = KEY_UNIT_ATTENTION;
6542 		sense->es_add_code = 0x3a; /* No media present */
6543 		return;
6544 	}
6545 
6546 	if (err_reg & SATA_ERROR_IDNF) {
6547 		if (err_reg & SATA_ERROR_ABORT) {
6548 			sense->es_key = KEY_ABORTED_COMMAND;
6549 		} else {
6550 			sense->es_key = KEY_ILLEGAL_REQUEST;
6551 			sense->es_add_code = 0x21; /* LBA out of range */
6552 		}
6553 		return;
6554 	}
6555 
6556 	if (err_reg & SATA_ERROR_ABORT) {
6557 		ASSERT(spx->txlt_sata_pkt != NULL);
6558 		sense->es_key = KEY_ABORTED_COMMAND;
6559 		return;
6560 	}
6561 }
6562 
6563 /*
6564  * Extract error LBA from sata_pkt.satapkt_cmd register fields
6565  */
6566 static void
6567 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba)
6568 {
6569 	sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd;
6570 
6571 	*lba = 0;
6572 	if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) {
6573 		*lba = sata_cmd->satacmd_lba_high_msb;
6574 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb;
6575 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb;
6576 	} else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) {
6577 		*lba = sata_cmd->satacmd_device_reg & 0xf;
6578 	}
6579 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb;
6580 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb;
6581 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb;
6582 }
6583 
6584 /*
6585  * This is fixed sense format - if LBA exceeds the info field size,
6586  * no valid info will be returned (valid bit in extended sense will
6587  * be set to 0).
6588  */
6589 static struct scsi_extended_sense *
6590 sata_arq_sense(sata_pkt_txlate_t *spx)
6591 {
6592 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6593 	struct scsi_arq_status *arqs;
6594 	struct scsi_extended_sense *sense;
6595 
6596 	/* Fill ARQ sense data */
6597 	scsipkt->pkt_state |= STATE_ARQ_DONE;
6598 	arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp;
6599 	*(uchar_t *)&arqs->sts_status = STATUS_CHECK;
6600 	*(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD;
6601 	arqs->sts_rqpkt_reason = CMD_CMPLT;
6602 	arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6603 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
6604 	arqs->sts_rqpkt_resid = 0;
6605 	sense = &arqs->sts_sensedata;
6606 	bzero(sense, sizeof (struct scsi_extended_sense));
6607 	sata_fixed_sense_data_preset(sense);
6608 	return (sense);
6609 }
6610 
6611 
6612 /*
6613  * Emulated SATA Read/Write command completion for zero-length requests.
6614  * This request always succedes, so in synchronous mode it always returns
6615  * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the
6616  * callback cannot be scheduled.
6617  */
6618 static int
6619 sata_emul_rw_completion(sata_pkt_txlate_t *spx)
6620 {
6621 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6622 
6623 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6624 	    STATE_SENT_CMD | STATE_GOT_STATUS;
6625 	scsipkt->pkt_reason = CMD_CMPLT;
6626 	*scsipkt->pkt_scbp = STATUS_GOOD;
6627 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
6628 		/* scsi callback required - have to schedule it */
6629 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
6630 		    (task_func_t *)scsipkt->pkt_comp,
6631 		    (void *)scsipkt, TQ_SLEEP) == NULL)
6632 			/* Scheduling the callback failed */
6633 			return (TRAN_BUSY);
6634 	}
6635 	return (TRAN_ACCEPT);
6636 }
6637 
6638 
6639 /*
6640  * Translate completion status of SATA read/write commands into scsi response.
6641  * pkt completion_reason is checked to determine the completion status.
6642  * Do scsi callback if necessary.
6643  *
6644  * Note: this function may be called also for synchronously executed
6645  * commands.
6646  * This function may be used only if scsi_pkt is non-NULL.
6647  */
6648 static void
6649 sata_txlt_rw_completion(sata_pkt_t *sata_pkt)
6650 {
6651 	sata_pkt_txlate_t *spx =
6652 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
6653 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
6654 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6655 	struct scsi_extended_sense *sense;
6656 	uint64_t lba;
6657 	struct buf *bp;
6658 	int rval;
6659 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
6660 		/* Normal completion */
6661 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6662 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
6663 		scsipkt->pkt_reason = CMD_CMPLT;
6664 		*scsipkt->pkt_scbp = STATUS_GOOD;
6665 		if (spx->txlt_tmp_buf != NULL) {
6666 			/* Temporary buffer was used */
6667 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
6668 			if (bp->b_flags & B_READ) {
6669 				rval = ddi_dma_sync(
6670 				    spx->txlt_buf_dma_handle, 0, 0,
6671 				    DDI_DMA_SYNC_FORCPU);
6672 				ASSERT(rval == DDI_SUCCESS);
6673 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
6674 				    bp->b_bcount);
6675 			}
6676 		}
6677 	} else {
6678 		/*
6679 		 * Something went wrong - analyze return
6680 		 */
6681 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6682 		    STATE_SENT_CMD | STATE_GOT_STATUS;
6683 		scsipkt->pkt_reason = CMD_INCOMPLETE;
6684 		*scsipkt->pkt_scbp = STATUS_CHECK;
6685 		sense = sata_arq_sense(spx);
6686 		ASSERT(sense != NULL);
6687 
6688 		/*
6689 		 * SATA_PKT_DEV_ERROR is the only case where we may be able to
6690 		 * extract from device registers the failing LBA.
6691 		 */
6692 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
6693 			if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) &&
6694 			    (scmd->satacmd_lba_mid_msb != 0 ||
6695 			    scmd->satacmd_lba_high_msb != 0)) {
6696 				/*
6697 				 * We have problem reporting this cmd LBA
6698 				 * in fixed sense data format, because of
6699 				 * the size of the scsi LBA fields.
6700 				 */
6701 				sense->es_valid = 0;
6702 			} else {
6703 				sata_extract_error_lba(spx, &lba);
6704 				sense->es_info_1 = (lba & 0xFF000000) >> 24;
6705 				sense->es_info_2 = (lba & 0xFF0000) >> 16;
6706 				sense->es_info_3 = (lba & 0xFF00) >> 8;
6707 				sense->es_info_4 = lba & 0xFF;
6708 			}
6709 		} else {
6710 			/* Invalid extended sense info */
6711 			sense->es_valid = 0;
6712 		}
6713 
6714 		switch (sata_pkt->satapkt_reason) {
6715 		case SATA_PKT_PORT_ERROR:
6716 			/* We may want to handle DEV GONE state as well */
6717 			/*
6718 			 * We have no device data. Assume no data transfered.
6719 			 */
6720 			sense->es_key = KEY_HARDWARE_ERROR;
6721 			break;
6722 
6723 		case SATA_PKT_DEV_ERROR:
6724 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
6725 			    SATA_STATUS_ERR) {
6726 				/*
6727 				 * determine dev error reason from error
6728 				 * reg content
6729 				 */
6730 				sata_decode_device_error(spx, sense);
6731 				if (sense->es_key == KEY_MEDIUM_ERROR) {
6732 					switch (scmd->satacmd_cmd_reg) {
6733 					case SATAC_READ_DMA:
6734 					case SATAC_READ_DMA_EXT:
6735 					case SATAC_READ_DMA_QUEUED:
6736 					case SATAC_READ_DMA_QUEUED_EXT:
6737 					case SATAC_READ_FPDMA_QUEUED:
6738 						/* Unrecovered read error */
6739 						sense->es_add_code =
6740 						SD_SCSI_ASC_UNREC_READ_ERROR;
6741 						break;
6742 					case SATAC_WRITE_DMA:
6743 					case SATAC_WRITE_DMA_EXT:
6744 					case SATAC_WRITE_DMA_QUEUED:
6745 					case SATAC_WRITE_DMA_QUEUED_EXT:
6746 					case SATAC_WRITE_FPDMA_QUEUED:
6747 						/* Write error */
6748 						sense->es_add_code =
6749 						    SD_SCSI_ASC_WRITE_ERROR;
6750 						break;
6751 					default:
6752 						/* Internal error */
6753 						SATA_LOG_D((
6754 						    spx->txlt_sata_hba_inst,
6755 						    CE_WARN,
6756 						    "sata_txlt_rw_completion :"
6757 						    "internal error - invalid "
6758 						    "command 0x%2x",
6759 						    scmd->satacmd_cmd_reg));
6760 						break;
6761 					}
6762 				}
6763 				break;
6764 			}
6765 			/* No extended sense key - no info available */
6766 			scsipkt->pkt_reason = CMD_INCOMPLETE;
6767 			break;
6768 
6769 		case SATA_PKT_TIMEOUT:
6770 			/* scsipkt->pkt_reason = CMD_TIMEOUT; */
6771 			scsipkt->pkt_reason = CMD_INCOMPLETE;
6772 			/* No extended sense key ? */
6773 			break;
6774 
6775 		case SATA_PKT_ABORTED:
6776 			scsipkt->pkt_reason = CMD_ABORTED;
6777 			/* No extended sense key ? */
6778 			break;
6779 
6780 		case SATA_PKT_RESET:
6781 			scsipkt->pkt_reason = CMD_RESET;
6782 			break;
6783 
6784 		default:
6785 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
6786 			    "sata_txlt_rw_completion: "
6787 			    "invalid packet completion reason"));
6788 			scsipkt->pkt_reason = CMD_TRAN_ERR;
6789 			break;
6790 		}
6791 	}
6792 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6793 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6794 
6795 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6796 	    scsipkt->pkt_comp != NULL)
6797 		/* scsi callback required */
6798 		(*scsipkt->pkt_comp)(scsipkt);
6799 }
6800 
6801 
6802 /*
6803  * Translate completion status of non-data commands (i.e. commands returning
6804  * no data).
6805  * pkt completion_reason is checked to determine the completion status.
6806  * Do scsi callback if necessary (FLAG_NOINTR == 0)
6807  *
6808  * Note: this function may be called also for synchronously executed
6809  * commands.
6810  * This function may be used only if scsi_pkt is non-NULL.
6811  */
6812 
6813 static 	void
6814 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt)
6815 {
6816 	sata_pkt_txlate_t *spx =
6817 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
6818 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6819 	struct scsi_extended_sense *sense;
6820 
6821 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
6822 	    STATE_SENT_CMD | STATE_GOT_STATUS;
6823 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
6824 		/* Normal completion */
6825 		scsipkt->pkt_reason = CMD_CMPLT;
6826 		*scsipkt->pkt_scbp = STATUS_GOOD;
6827 	} else {
6828 		/* Something went wrong */
6829 		scsipkt->pkt_reason = CMD_INCOMPLETE;
6830 		*scsipkt->pkt_scbp = STATUS_CHECK;
6831 		sense = sata_arq_sense(spx);
6832 		switch (sata_pkt->satapkt_reason) {
6833 		case SATA_PKT_PORT_ERROR:
6834 			/*
6835 			 * We have no device data. Assume no data transfered.
6836 			 */
6837 			sense->es_key = KEY_HARDWARE_ERROR;
6838 			break;
6839 
6840 		case SATA_PKT_DEV_ERROR:
6841 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
6842 			    SATA_STATUS_ERR) {
6843 				/*
6844 				 * determine dev error reason from error
6845 				 * reg content
6846 				 */
6847 				sata_decode_device_error(spx, sense);
6848 				break;
6849 			}
6850 			/* No extended sense key - no info available */
6851 			break;
6852 
6853 		case SATA_PKT_TIMEOUT:
6854 			/* scsipkt->pkt_reason = CMD_TIMEOUT; */
6855 			scsipkt->pkt_reason = CMD_INCOMPLETE;
6856 			/* No extended sense key ? */
6857 			break;
6858 
6859 		case SATA_PKT_ABORTED:
6860 			scsipkt->pkt_reason = CMD_ABORTED;
6861 			/* No extended sense key ? */
6862 			break;
6863 
6864 		case SATA_PKT_RESET:
6865 			/* pkt aborted by an explicit reset from a host */
6866 			scsipkt->pkt_reason = CMD_RESET;
6867 			break;
6868 
6869 		default:
6870 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
6871 			    "sata_txlt_nodata_cmd_completion: "
6872 			    "invalid packet completion reason %d",
6873 			    sata_pkt->satapkt_reason));
6874 			scsipkt->pkt_reason = CMD_TRAN_ERR;
6875 			break;
6876 		}
6877 
6878 	}
6879 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
6880 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
6881 
6882 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
6883 	    scsipkt->pkt_comp != NULL)
6884 		/* scsi callback required */
6885 		(*scsipkt->pkt_comp)(scsipkt);
6886 }
6887 
6888 
6889 /*
6890  * Build Mode sense R/W recovery page
6891  * NOT IMPLEMENTED
6892  */
6893 
6894 static int
6895 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6896 {
6897 #ifndef __lock_lint
6898 	_NOTE(ARGUNUSED(sdinfo))
6899 	_NOTE(ARGUNUSED(pcntrl))
6900 	_NOTE(ARGUNUSED(buf))
6901 #endif
6902 	return (0);
6903 }
6904 
6905 /*
6906  * Build Mode sense caching page  -  scsi-3 implementation.
6907  * Page length distinguishes previous format from scsi-3 format.
6908  * buf must have space for 0x12 bytes.
6909  * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable.
6910  *
6911  */
6912 static int
6913 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6914 {
6915 	struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf;
6916 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6917 
6918 	/*
6919 	 * Most of the fields are set to 0, being not supported and/or disabled
6920 	 */
6921 	bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3);
6922 
6923 	/* Saved paramters not supported */
6924 	if (pcntrl == 3)
6925 		return (0);
6926 	if (pcntrl == 0 || pcntrl == 2) {
6927 		/*
6928 		 * For now treat current and default parameters as same
6929 		 * That may have to change, if target driver will complain
6930 		 */
6931 		page->mode_page.code = MODEPAGE_CACHING;	/* PS = 0 */
6932 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
6933 
6934 		if ((sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) &&
6935 		    !(sata_id->ai_features85 & SATA_LOOK_AHEAD)) {
6936 			page->dra = 1;		/* Read Ahead disabled */
6937 			page->rcd = 1;		/* Read Cache disabled */
6938 		}
6939 		if ((sata_id->ai_cmdset82 & SATA_WRITE_CACHE) &&
6940 		    (sata_id->ai_features85 & SATA_WRITE_CACHE))
6941 			page->wce = 1;		/* Write Cache enabled */
6942 	} else {
6943 		/* Changeable parameters */
6944 		page->mode_page.code = MODEPAGE_CACHING;
6945 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
6946 		if (sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) {
6947 			page->dra = 1;
6948 			page->rcd = 1;
6949 		}
6950 		if (sata_id->ai_cmdset82 & SATA_WRITE_CACHE)
6951 			page->wce = 1;
6952 	}
6953 	return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
6954 	    sizeof (struct mode_page));
6955 }
6956 
6957 /*
6958  * Build Mode sense exception cntrl page
6959  */
6960 static int
6961 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6962 {
6963 	struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf;
6964 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6965 
6966 	/*
6967 	 * Most of the fields are set to 0, being not supported and/or disabled
6968 	 */
6969 	bzero(buf, PAGELENGTH_INFO_EXCPT);
6970 
6971 	page->mode_page.code = MODEPAGE_INFO_EXCPT;
6972 	page->mode_page.length = PAGELENGTH_INFO_EXCPT;
6973 
6974 	/* Indicate that this is page is saveable */
6975 	page->mode_page.ps = 1;
6976 
6977 	/*
6978 	 * We will return the same data for default, current and saved page.
6979 	 * The only changeable bit is dexcpt and that bit is required
6980 	 * by the ATA specification to be preserved across power cycles.
6981 	 */
6982 	if (pcntrl != 1) {
6983 		page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED);
6984 		page->mrie = MRIE_ONLY_ON_REQUEST;
6985 	}
6986 	else
6987 		page->dexcpt = 1;	/* Only changeable parameter */
6988 
6989 	return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_info_excpt_page));
6990 }
6991 
6992 
6993 static int
6994 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6995 {
6996 	struct mode_acoustic_management *page =
6997 	    (struct mode_acoustic_management *)buf;
6998 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6999 
7000 	/*
7001 	 * Most of the fields are set to 0, being not supported and/or disabled
7002 	 */
7003 	bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT);
7004 
7005 	switch (pcntrl) {
7006 	case P_CNTRL_DEFAULT:
7007 		/*  default paramters not supported */
7008 		return (0);
7009 
7010 	case P_CNTRL_CURRENT:
7011 	case P_CNTRL_SAVED:
7012 		/* Saved and current are supported and are identical */
7013 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
7014 		page->mode_page.length =
7015 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
7016 		page->mode_page.ps = 1;
7017 
7018 		/* Word 83 indicates if feature is supported */
7019 		/* If feature is not supported */
7020 		if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) {
7021 			page->acoustic_manag_enable =
7022 			    ACOUSTIC_DISABLED;
7023 		} else {
7024 			page->acoustic_manag_enable =
7025 			    ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT)
7026 			    != 0);
7027 			/* Word 94 inidicates the value */
7028 #ifdef	_LITTLE_ENDIAN
7029 			page->acoustic_manag_level =
7030 			    (uchar_t)sata_id->ai_acoustic;
7031 			page->vendor_recommended_value =
7032 			    sata_id->ai_acoustic >> 8;
7033 #else
7034 			page->acoustic_manag_level =
7035 			    sata_id->ai_acoustic >> 8;
7036 			page->vendor_recommended_value =
7037 			    (uchar_t)sata_id->ai_acoustic;
7038 #endif
7039 		}
7040 		break;
7041 
7042 	case P_CNTRL_CHANGEABLE:
7043 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
7044 		page->mode_page.length =
7045 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
7046 		page->mode_page.ps = 1;
7047 
7048 		/* Word 83 indicates if the feature is supported */
7049 		if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) {
7050 			page->acoustic_manag_enable =
7051 			    ACOUSTIC_ENABLED;
7052 			page->acoustic_manag_level = 0xff;
7053 		}
7054 		break;
7055 	}
7056 	return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
7057 	    sizeof (struct mode_page));
7058 }
7059 
7060 
7061 /*
7062  * Build Mode sense power condition page
7063  * NOT IMPLEMENTED.
7064  */
7065 static int
7066 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
7067 {
7068 #ifndef __lock_lint
7069 	_NOTE(ARGUNUSED(sdinfo))
7070 	_NOTE(ARGUNUSED(pcntrl))
7071 	_NOTE(ARGUNUSED(buf))
7072 #endif
7073 	return (0);
7074 }
7075 
7076 
7077 /*
7078  * Process mode select caching page 8 (scsi3 format only).
7079  * Read Ahead (same as read cache) and Write Cache may be turned on and off
7080  * if these features are supported by the device. If these features are not
7081  * supported, quietly ignore them.
7082  * This function fails only if the SET FEATURE command sent to
7083  * the device fails. The page format is not varified, assuming that the
7084  * target driver operates correctly - if parameters length is too short,
7085  * we just drop the page.
7086  * Two command may be sent if both Read Cache/Read Ahead and Write Cache
7087  * setting have to be changed.
7088  * SET FEATURE command is executed synchronously, i.e. we wait here until
7089  * it is completed, regardless of the scsi pkt directives.
7090  *
7091  * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e.
7092  * changing DRA will change RCD.
7093  *
7094  * More than one SATA command may be executed to perform operations specified
7095  * by mode select pages. The first error terminates further execution.
7096  * Operations performed successully are not backed-up in such case.
7097  *
7098  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
7099  * If operation resulted in changing device setup, dmod flag should be set to
7100  * one (1). If parameters were not changed, dmod flag should be set to 0.
7101  * Upon return, if operation required sending command to the device, the rval
7102  * should be set to the value returned by sata_hba_start. If operation
7103  * did not require device access, rval should be set to TRAN_ACCEPT.
7104  * The pagelen should be set to the length of the page.
7105  *
7106  * This function has to be called with a port mutex held.
7107  *
7108  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
7109  */
7110 int
7111 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page,
7112     int parmlen, int *pagelen, int *rval, int *dmod)
7113 {
7114 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7115 	sata_drive_info_t *sdinfo;
7116 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
7117 	sata_id_t *sata_id;
7118 	struct scsi_extended_sense *sense;
7119 	int wce, dra;	/* Current settings */
7120 
7121 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
7122 	    &spx->txlt_sata_pkt->satapkt_device);
7123 	sata_id = &sdinfo->satadrv_id;
7124 	*dmod = 0;
7125 
7126 	/* Verify parameters length. If too short, drop it */
7127 	if (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
7128 	    sizeof (struct mode_page) < parmlen) {
7129 		*scsipkt->pkt_scbp = STATUS_CHECK;
7130 		sense = sata_arq_sense(spx);
7131 		sense->es_key = KEY_ILLEGAL_REQUEST;
7132 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
7133 		*pagelen = parmlen;
7134 		*rval = TRAN_ACCEPT;
7135 		return (SATA_FAILURE);
7136 	}
7137 
7138 	*pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page);
7139 
7140 	/*
7141 	 * We can manipulate only write cache and read ahead
7142 	 * (read cache) setting.
7143 	 */
7144 	if (!(sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) &&
7145 	    !(sata_id->ai_cmdset82 & SATA_WRITE_CACHE)) {
7146 		/*
7147 		 * None of the features is supported - ignore
7148 		 */
7149 		*rval = TRAN_ACCEPT;
7150 		return (SATA_SUCCESS);
7151 	}
7152 
7153 	/* Current setting of Read Ahead (and Read Cache) */
7154 	if (sata_id->ai_features85 & SATA_LOOK_AHEAD)
7155 		dra = 0;	/* 0 == not disabled */
7156 	else
7157 		dra = 1;
7158 	/* Current setting of Write Cache */
7159 	if (sata_id->ai_features85 & SATA_WRITE_CACHE)
7160 		wce = 1;
7161 	else
7162 		wce = 0;
7163 
7164 	if (page->dra == dra && page->wce == wce && page->rcd == dra) {
7165 		/* nothing to do */
7166 		*rval = TRAN_ACCEPT;
7167 		return (SATA_SUCCESS);
7168 	}
7169 	/*
7170 	 * Need to flip some setting
7171 	 * Set-up Internal SET FEATURES command(s)
7172 	 */
7173 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
7174 	scmd->satacmd_addr_type = 0;
7175 	scmd->satacmd_device_reg = 0;
7176 	scmd->satacmd_status_reg = 0;
7177 	scmd->satacmd_error_reg = 0;
7178 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
7179 	if (page->dra != dra || page->rcd != dra) {
7180 		/* Need to flip read ahead setting */
7181 		if (dra == 0)
7182 			/* Disable read ahead / read cache */
7183 			scmd->satacmd_features_reg =
7184 			    SATAC_SF_DISABLE_READ_AHEAD;
7185 		else
7186 			/* Enable read ahead  / read cache */
7187 			scmd->satacmd_features_reg =
7188 			    SATAC_SF_ENABLE_READ_AHEAD;
7189 
7190 		/* Transfer command to HBA */
7191 		if (sata_hba_start(spx, rval) != 0)
7192 			/*
7193 			 * Pkt not accepted for execution.
7194 			 */
7195 			return (SATA_FAILURE);
7196 
7197 		*dmod = 1;
7198 
7199 		/* Now process return */
7200 		if (spx->txlt_sata_pkt->satapkt_reason !=
7201 		    SATA_PKT_COMPLETED) {
7202 			goto failure;	/* Terminate */
7203 		}
7204 	}
7205 
7206 	/* Note that the packet is not removed, so it could be re-used */
7207 	if (page->wce != wce) {
7208 		/* Need to flip Write Cache setting */
7209 		if (page->wce == 1)
7210 			/* Enable write cache */
7211 			scmd->satacmd_features_reg =
7212 			    SATAC_SF_ENABLE_WRITE_CACHE;
7213 		else
7214 			/* Disable write cache */
7215 			scmd->satacmd_features_reg =
7216 			    SATAC_SF_DISABLE_WRITE_CACHE;
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;
7226 
7227 		/* Now process return */
7228 		if (spx->txlt_sata_pkt->satapkt_reason !=
7229 		    SATA_PKT_COMPLETED) {
7230 			goto failure;
7231 		}
7232 	}
7233 	return (SATA_SUCCESS);
7234 
7235 failure:
7236 	sata_xlate_errors(spx);
7237 
7238 	return (SATA_FAILURE);
7239 }
7240 
7241 /*
7242  * Process mode select informational exceptions control page 0x1c
7243  *
7244  * The only changeable bit is dexcpt (disable exceptions).
7245  * MRIE (method of reporting informational exceptions) must be
7246  * "only on request".
7247  *
7248  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
7249  * If operation resulted in changing device setup, dmod flag should be set to
7250  * one (1). If parameters were not changed, dmod flag should be set to 0.
7251  * Upon return, if operation required sending command to the device, the rval
7252  * should be set to the value returned by sata_hba_start. If operation
7253  * did not require device access, rval should be set to TRAN_ACCEPT.
7254  * The pagelen should be set to the length of the page.
7255  *
7256  * This function has to be called with a port mutex held.
7257  *
7258  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
7259  */
7260 static	int
7261 sata_mode_select_page_1c(
7262 	sata_pkt_txlate_t *spx,
7263 	struct mode_info_excpt_page *page,
7264 	int parmlen,
7265 	int *pagelen,
7266 	int *rval,
7267 	int *dmod)
7268 {
7269 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7270 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
7271 	sata_drive_info_t *sdinfo;
7272 	sata_id_t *sata_id;
7273 	struct scsi_extended_sense *sense;
7274 
7275 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
7276 	    &spx->txlt_sata_pkt->satapkt_device);
7277 	sata_id = &sdinfo->satadrv_id;
7278 
7279 	*dmod = 0;
7280 
7281 	/* Verify parameters length. If too short, drop it */
7282 	if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) < parmlen) ||
7283 	    page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) {
7284 		*scsipkt->pkt_scbp = STATUS_CHECK;
7285 		sense = sata_arq_sense(spx);
7286 		sense->es_key = KEY_ILLEGAL_REQUEST;
7287 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
7288 		*pagelen = parmlen;
7289 		*rval = TRAN_ACCEPT;
7290 		return (SATA_FAILURE);
7291 	}
7292 
7293 	*pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page);
7294 
7295 	if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
7296 		*scsipkt->pkt_scbp = STATUS_CHECK;
7297 		sense = sata_arq_sense(spx);
7298 		sense->es_key = KEY_ILLEGAL_REQUEST;
7299 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
7300 		*pagelen = parmlen;
7301 		*rval = TRAN_ACCEPT;
7302 		return (SATA_FAILURE);
7303 	}
7304 
7305 	/* If already in the state requested, we are done */
7306 	if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
7307 		/* nothing to do */
7308 		*rval = TRAN_ACCEPT;
7309 		return (SATA_SUCCESS);
7310 	}
7311 
7312 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
7313 
7314 	/* Build SMART_ENABLE or SMART_DISABLE command */
7315 	scmd->satacmd_addr_type = 0;		/* N/A */
7316 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
7317 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
7318 	scmd->satacmd_features_reg = page->dexcpt ?
7319 	    SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS;
7320 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
7321 	scmd->satacmd_cmd_reg = SATAC_SMART;
7322 
7323 	/* Transfer command to HBA */
7324 	if (sata_hba_start(spx, rval) != 0)
7325 		/*
7326 		 * Pkt not accepted for execution.
7327 		 */
7328 		return (SATA_FAILURE);
7329 
7330 	*dmod = 1;	/* At least may have been modified */
7331 
7332 	/* Now process return */
7333 	if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED)
7334 		return (SATA_SUCCESS);
7335 
7336 	/* Packet did not complete successfully */
7337 	sata_xlate_errors(spx);
7338 
7339 	return (SATA_FAILURE);
7340 }
7341 
7342 int
7343 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct
7344     mode_acoustic_management *page, int parmlen, int *pagelen,
7345     int *rval, int *dmod)
7346 {
7347 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7348 	sata_drive_info_t *sdinfo;
7349 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
7350 	sata_id_t *sata_id;
7351 	struct scsi_extended_sense *sense;
7352 
7353 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
7354 	    &spx->txlt_sata_pkt->satapkt_device);
7355 	sata_id = &sdinfo->satadrv_id;
7356 	*dmod = 0;
7357 
7358 	/* If parmlen is too short or the feature is not supported, drop it */
7359 	if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
7360 	    sizeof (struct mode_page)) < parmlen) ||
7361 	    (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) {
7362 		*scsipkt->pkt_scbp = STATUS_CHECK;
7363 		sense = sata_arq_sense(spx);
7364 		sense->es_key = KEY_ILLEGAL_REQUEST;
7365 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
7366 		*pagelen = parmlen;
7367 		*rval = TRAN_ACCEPT;
7368 		return (SATA_FAILURE);
7369 	}
7370 
7371 	*pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
7372 	    sizeof (struct mode_page);
7373 
7374 	/*
7375 	 * We can enable and disable acoustice management and
7376 	 * set the acoustic management level.
7377 	 */
7378 
7379 	/*
7380 	 * Set-up Internal SET FEATURES command(s)
7381 	 */
7382 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
7383 	scmd->satacmd_addr_type = 0;
7384 	scmd->satacmd_device_reg = 0;
7385 	scmd->satacmd_status_reg = 0;
7386 	scmd->satacmd_error_reg = 0;
7387 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
7388 	if (page->acoustic_manag_enable) {
7389 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC;
7390 		scmd->satacmd_sec_count_lsb = page->acoustic_manag_level;
7391 	} else {	/* disabling acoustic management */
7392 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC;
7393 	}
7394 
7395 	/* Transfer command to HBA */
7396 	if (sata_hba_start(spx, rval) != 0)
7397 		/*
7398 		 * Pkt not accepted for execution.
7399 		 */
7400 		return (SATA_FAILURE);
7401 
7402 	/* Now process return */
7403 	if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) {
7404 		sata_xlate_errors(spx);
7405 		return (SATA_FAILURE);
7406 	}
7407 
7408 	*dmod = 1;
7409 
7410 	return (SATA_SUCCESS);
7411 }
7412 
7413 
7414 
7415 
7416 /*
7417  * sata_build_lsense_page0() is used to create the
7418  * SCSI LOG SENSE page 0 (supported log pages)
7419  *
7420  * Currently supported pages are 0, 0x10, 0x2f and 0x30
7421  * (supported log pages, self-test results, informational exceptions
7422  *  and Sun vendor specific ATA SMART data).
7423  *
7424  * Takes a sata_drive_info t * and the address of a buffer
7425  * in which to create the page information.
7426  *
7427  * Returns the number of bytes valid in the buffer.
7428  */
7429 static	int
7430 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf)
7431 {
7432 	struct log_parameter *lpp = (struct log_parameter *)buf;
7433 	uint8_t *page_ptr = (uint8_t *)lpp->param_values;
7434 	int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */
7435 	sata_id_t *sata_id = &sdinfo->satadrv_id;
7436 
7437 	lpp->param_code[0] = 0;
7438 	lpp->param_code[1] = 0;
7439 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
7440 	*page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES;
7441 
7442 	if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
7443 		if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) {
7444 			*page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS;
7445 			++num_pages_supported;
7446 		}
7447 		*page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS;
7448 		++num_pages_supported;
7449 		*page_ptr++ = PAGE_CODE_SMART_READ_DATA;
7450 		++num_pages_supported;
7451 	}
7452 
7453 	lpp->param_len = num_pages_supported;
7454 
7455 	return ((&lpp->param_values[0] - (uint8_t *)lpp) +
7456 	    num_pages_supported);
7457 }
7458 
7459 /*
7460  * sata_build_lsense_page_10() is used to create the
7461  * SCSI LOG SENSE page 0x10 (self-test results)
7462  *
7463  * Takes a sata_drive_info t * and the address of a buffer
7464  * in which to create the page information as well as a sata_hba_inst_t *.
7465  *
7466  * Returns the number of bytes valid in the buffer.
7467  */
7468 static	int
7469 sata_build_lsense_page_10(
7470 	sata_drive_info_t *sdinfo,
7471 	uint8_t *buf,
7472 	sata_hba_inst_t *sata_hba_inst)
7473 {
7474 	struct log_parameter *lpp = (struct log_parameter *)buf;
7475 	int rval;
7476 
7477 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
7478 		struct smart_ext_selftest_log *ext_selftest_log;
7479 
7480 		ext_selftest_log = kmem_zalloc(
7481 		    sizeof (struct smart_ext_selftest_log), KM_SLEEP);
7482 
7483 		rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo,
7484 		    ext_selftest_log, 0);
7485 		if (rval == 0) {
7486 			int index, start_index;
7487 			struct smart_ext_selftest_log_entry *entry;
7488 			static const struct smart_ext_selftest_log_entry empty =
7489 			    {0};
7490 			uint16_t block_num;
7491 			int count;
7492 			boolean_t only_one_block = B_FALSE;
7493 
7494 			index = ext_selftest_log->
7495 			    smart_ext_selftest_log_index[0];
7496 			index |= ext_selftest_log->
7497 			    smart_ext_selftest_log_index[1] << 8;
7498 			if (index == 0)
7499 				goto out;
7500 
7501 			--index;	/* Correct for 0 origin */
7502 			start_index = index;	/* remember where we started */
7503 			block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
7504 			if (block_num != 0) {
7505 				rval = sata_ext_smart_selftest_read_log(
7506 				    sata_hba_inst, sdinfo, ext_selftest_log,
7507 				    block_num);
7508 				if (rval != 0)
7509 					goto out;
7510 			}
7511 			index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
7512 			entry =
7513 			    &ext_selftest_log->
7514 			    smart_ext_selftest_log_entries[index];
7515 
7516 			for (count = 1;
7517 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
7518 			    ++count) {
7519 				uint8_t status;
7520 				uint8_t code;
7521 				uint8_t sense_key;
7522 				uint8_t add_sense_code;
7523 				uint8_t add_sense_code_qual;
7524 
7525 				/* If this is an unused entry, we are done */
7526 				if (bcmp(entry, &empty, sizeof (empty)) == 0) {
7527 					/* Broken firmware on some disks */
7528 					if (index + 1 ==
7529 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK) {
7530 						--entry;
7531 						--index;
7532 						if (bcmp(entry, &empty,
7533 						    sizeof (empty)) == 0)
7534 							goto out;
7535 					} else
7536 						goto out;
7537 				}
7538 
7539 				if (only_one_block &&
7540 				    start_index == index)
7541 					goto out;
7542 
7543 				lpp->param_code[0] = 0;
7544 				lpp->param_code[1] = count;
7545 				lpp->param_ctrl_flags =
7546 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
7547 				lpp->param_len =
7548 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
7549 
7550 				status = entry->smart_ext_selftest_log_status;
7551 				status >>= 4;
7552 				switch (status) {
7553 				case 0:
7554 				default:
7555 					sense_key = KEY_NO_SENSE;
7556 					add_sense_code =
7557 					    SD_SCSI_ASC_NO_ADD_SENSE;
7558 					add_sense_code_qual = 0;
7559 					break;
7560 				case 1:
7561 					sense_key = KEY_ABORTED_COMMAND;
7562 					add_sense_code =
7563 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7564 					add_sense_code_qual = SCSI_COMPONENT_81;
7565 					break;
7566 				case 2:
7567 					sense_key = KEY_ABORTED_COMMAND;
7568 					add_sense_code =
7569 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7570 					add_sense_code_qual = SCSI_COMPONENT_82;
7571 					break;
7572 				case 3:
7573 					sense_key = KEY_ABORTED_COMMAND;
7574 					add_sense_code =
7575 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7576 					add_sense_code_qual = SCSI_COMPONENT_83;
7577 					break;
7578 				case 4:
7579 					sense_key = KEY_HARDWARE_ERROR;
7580 					add_sense_code =
7581 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7582 					add_sense_code_qual = SCSI_COMPONENT_84;
7583 					break;
7584 				case 5:
7585 					sense_key = KEY_HARDWARE_ERROR;
7586 					add_sense_code =
7587 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7588 					add_sense_code_qual = SCSI_COMPONENT_85;
7589 					break;
7590 				case 6:
7591 					sense_key = KEY_HARDWARE_ERROR;
7592 					add_sense_code =
7593 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7594 					add_sense_code_qual = SCSI_COMPONENT_86;
7595 					break;
7596 				case 7:
7597 					sense_key = KEY_MEDIUM_ERROR;
7598 					add_sense_code =
7599 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7600 					add_sense_code_qual = SCSI_COMPONENT_87;
7601 					break;
7602 				case 8:
7603 					sense_key = KEY_HARDWARE_ERROR;
7604 					add_sense_code =
7605 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7606 					add_sense_code_qual = SCSI_COMPONENT_88;
7607 					break;
7608 				}
7609 				code = 0;	/* unspecified */
7610 				status |= (code << 4);
7611 				lpp->param_values[0] = status;
7612 				lpp->param_values[1] = 0; /* unspecified */
7613 				lpp->param_values[2] = entry->
7614 				    smart_ext_selftest_log_timestamp[1];
7615 				lpp->param_values[3] = entry->
7616 				    smart_ext_selftest_log_timestamp[0];
7617 				if (status != 0) {
7618 					lpp->param_values[4] = 0;
7619 					lpp->param_values[5] = 0;
7620 					lpp->param_values[6] = entry->
7621 					    smart_ext_selftest_log_failing_lba
7622 					    [5];
7623 					lpp->param_values[7] = entry->
7624 					    smart_ext_selftest_log_failing_lba
7625 					    [4];
7626 					lpp->param_values[8] = entry->
7627 					    smart_ext_selftest_log_failing_lba
7628 					    [3];
7629 					lpp->param_values[9] = entry->
7630 					    smart_ext_selftest_log_failing_lba
7631 					    [2];
7632 					lpp->param_values[10] = entry->
7633 					    smart_ext_selftest_log_failing_lba
7634 					    [1];
7635 					lpp->param_values[11] = entry->
7636 					    smart_ext_selftest_log_failing_lba
7637 					    [0];
7638 				} else {	/* No bad block address */
7639 					lpp->param_values[4] = 0xff;
7640 					lpp->param_values[5] = 0xff;
7641 					lpp->param_values[6] = 0xff;
7642 					lpp->param_values[7] = 0xff;
7643 					lpp->param_values[8] = 0xff;
7644 					lpp->param_values[9] = 0xff;
7645 					lpp->param_values[10] = 0xff;
7646 					lpp->param_values[11] = 0xff;
7647 				}
7648 
7649 				lpp->param_values[12] = sense_key;
7650 				lpp->param_values[13] = add_sense_code;
7651 				lpp->param_values[14] = add_sense_code_qual;
7652 				lpp->param_values[15] = 0; /* undefined */
7653 
7654 				lpp = (struct log_parameter *)
7655 				    (((uint8_t *)lpp) +
7656 				    SCSI_LOG_PARAM_HDR_LEN +
7657 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
7658 
7659 				--index;	/* Back up to previous entry */
7660 				if (index < 0) {
7661 					if (block_num > 0) {
7662 						--block_num;
7663 					} else {
7664 						struct read_log_ext_directory
7665 						    logdir;
7666 
7667 						rval =
7668 						    sata_read_log_ext_directory(
7669 						    sata_hba_inst, sdinfo,
7670 						    &logdir);
7671 						if (rval == -1)
7672 							goto out;
7673 						if ((logdir.read_log_ext_vers
7674 						    [0] == 0) &&
7675 						    (logdir.read_log_ext_vers
7676 						    [1] == 0))
7677 							goto out;
7678 						block_num =
7679 						    logdir.read_log_ext_nblks
7680 						    [EXT_SMART_SELFTEST_LOG_PAGE
7681 						    - 1][0];
7682 						block_num |= logdir.
7683 						    read_log_ext_nblks
7684 						    [EXT_SMART_SELFTEST_LOG_PAGE
7685 						    - 1][1] << 8;
7686 						--block_num;
7687 						only_one_block =
7688 						    (block_num == 0);
7689 					}
7690 					rval = sata_ext_smart_selftest_read_log(
7691 					    sata_hba_inst, sdinfo,
7692 					    ext_selftest_log, block_num);
7693 					if (rval != 0)
7694 						goto out;
7695 
7696 					index =
7697 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK -
7698 					    1;
7699 				}
7700 				index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
7701 				entry = &ext_selftest_log->
7702 				    smart_ext_selftest_log_entries[index];
7703 			}
7704 		}
7705 out:
7706 		kmem_free(ext_selftest_log,
7707 		    sizeof (struct smart_ext_selftest_log));
7708 	} else {
7709 		struct smart_selftest_log *selftest_log;
7710 
7711 		selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log),
7712 		    KM_SLEEP);
7713 
7714 		rval = sata_smart_selftest_log(sata_hba_inst, sdinfo,
7715 		    selftest_log);
7716 
7717 		if (rval == 0) {
7718 			int index;
7719 			int count;
7720 			struct smart_selftest_log_entry *entry;
7721 			static const struct smart_selftest_log_entry empty =
7722 			    { 0 };
7723 
7724 			index = selftest_log->smart_selftest_log_index;
7725 			if (index == 0)
7726 				goto done;
7727 			--index;	/* Correct for 0 origin */
7728 			entry = &selftest_log->
7729 			    smart_selftest_log_entries[index];
7730 			for (count = 1;
7731 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
7732 			    ++count) {
7733 				uint8_t status;
7734 				uint8_t code;
7735 				uint8_t sense_key;
7736 				uint8_t add_sense_code;
7737 				uint8_t add_sense_code_qual;
7738 
7739 				if (bcmp(entry, &empty, sizeof (empty)) == 0)
7740 					goto done;
7741 
7742 				lpp->param_code[0] = 0;
7743 				lpp->param_code[1] = count;
7744 				lpp->param_ctrl_flags =
7745 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
7746 				lpp->param_len =
7747 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
7748 
7749 				status = entry->smart_selftest_log_status;
7750 				status >>= 4;
7751 				switch (status) {
7752 				case 0:
7753 				default:
7754 					sense_key = KEY_NO_SENSE;
7755 					add_sense_code =
7756 					    SD_SCSI_ASC_NO_ADD_SENSE;
7757 					break;
7758 				case 1:
7759 					sense_key = KEY_ABORTED_COMMAND;
7760 					add_sense_code =
7761 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7762 					add_sense_code_qual = SCSI_COMPONENT_81;
7763 					break;
7764 				case 2:
7765 					sense_key = KEY_ABORTED_COMMAND;
7766 					add_sense_code =
7767 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7768 					add_sense_code_qual = SCSI_COMPONENT_82;
7769 					break;
7770 				case 3:
7771 					sense_key = KEY_ABORTED_COMMAND;
7772 					add_sense_code =
7773 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7774 					add_sense_code_qual = SCSI_COMPONENT_83;
7775 					break;
7776 				case 4:
7777 					sense_key = KEY_HARDWARE_ERROR;
7778 					add_sense_code =
7779 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7780 					add_sense_code_qual = SCSI_COMPONENT_84;
7781 					break;
7782 				case 5:
7783 					sense_key = KEY_HARDWARE_ERROR;
7784 					add_sense_code =
7785 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7786 					add_sense_code_qual = SCSI_COMPONENT_85;
7787 					break;
7788 				case 6:
7789 					sense_key = KEY_HARDWARE_ERROR;
7790 					add_sense_code =
7791 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7792 					add_sense_code_qual = SCSI_COMPONENT_86;
7793 					break;
7794 				case 7:
7795 					sense_key = KEY_MEDIUM_ERROR;
7796 					add_sense_code =
7797 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7798 					add_sense_code_qual = SCSI_COMPONENT_87;
7799 					break;
7800 				case 8:
7801 					sense_key = KEY_HARDWARE_ERROR;
7802 					add_sense_code =
7803 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
7804 					add_sense_code_qual = SCSI_COMPONENT_88;
7805 					break;
7806 				}
7807 				code = 0;	/* unspecified */
7808 				status |= (code << 4);
7809 				lpp->param_values[0] = status;
7810 				lpp->param_values[1] = 0; /* unspecified */
7811 				lpp->param_values[2] = entry->
7812 				    smart_selftest_log_timestamp[1];
7813 				lpp->param_values[3] = entry->
7814 				    smart_selftest_log_timestamp[0];
7815 				if (status != 0) {
7816 					lpp->param_values[4] = 0;
7817 					lpp->param_values[5] = 0;
7818 					lpp->param_values[6] = 0;
7819 					lpp->param_values[7] = 0;
7820 					lpp->param_values[8] = entry->
7821 					    smart_selftest_log_failing_lba[3];
7822 					lpp->param_values[9] = entry->
7823 					    smart_selftest_log_failing_lba[2];
7824 					lpp->param_values[10] = entry->
7825 					    smart_selftest_log_failing_lba[1];
7826 					lpp->param_values[11] = entry->
7827 					    smart_selftest_log_failing_lba[0];
7828 				} else {	/* No block address */
7829 					lpp->param_values[4] = 0xff;
7830 					lpp->param_values[5] = 0xff;
7831 					lpp->param_values[6] = 0xff;
7832 					lpp->param_values[7] = 0xff;
7833 					lpp->param_values[8] = 0xff;
7834 					lpp->param_values[9] = 0xff;
7835 					lpp->param_values[10] = 0xff;
7836 					lpp->param_values[11] = 0xff;
7837 				}
7838 				lpp->param_values[12] = sense_key;
7839 				lpp->param_values[13] = add_sense_code;
7840 				lpp->param_values[14] = add_sense_code_qual;
7841 				lpp->param_values[15] = 0; /* undefined */
7842 
7843 				lpp = (struct log_parameter *)
7844 				    (((uint8_t *)lpp) +
7845 				    SCSI_LOG_PARAM_HDR_LEN +
7846 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
7847 				--index;	/* back up to previous entry */
7848 				if (index < 0) {
7849 					index =
7850 					    NUM_SMART_SELFTEST_LOG_ENTRIES - 1;
7851 				}
7852 				entry = &selftest_log->
7853 				    smart_selftest_log_entries[index];
7854 			}
7855 		}
7856 done:
7857 		kmem_free(selftest_log, sizeof (struct smart_selftest_log));
7858 	}
7859 
7860 	return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) *
7861 	    SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS);
7862 }
7863 
7864 /*
7865  * sata_build_lsense_page_2f() is used to create the
7866  * SCSI LOG SENSE page 0x10 (informational exceptions)
7867  *
7868  * Takes a sata_drive_info t * and the address of a buffer
7869  * in which to create the page information as well as a sata_hba_inst_t *.
7870  *
7871  * Returns the number of bytes valid in the buffer.
7872  */
7873 static	int
7874 sata_build_lsense_page_2f(
7875 	sata_drive_info_t *sdinfo,
7876 	uint8_t *buf,
7877 	sata_hba_inst_t *sata_hba_inst)
7878 {
7879 	struct log_parameter *lpp = (struct log_parameter *)buf;
7880 	int rval;
7881 	uint8_t *smart_data;
7882 	uint8_t temp;
7883 	sata_id_t *sata_id;
7884 #define	SMART_NO_TEMP	0xff
7885 
7886 	lpp->param_code[0] = 0;
7887 	lpp->param_code[1] = 0;
7888 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
7889 
7890 	/* Now get the SMART status w.r.t. threshold exceeded */
7891 	rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo);
7892 	switch (rval) {
7893 	case 1:
7894 		lpp->param_values[0] = SCSI_PREDICTED_FAILURE;
7895 		lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE;
7896 		break;
7897 	case 0:
7898 	case -1:	/* failed to get data */
7899 		lpp->param_values[0] = 0;	/* No failure predicted */
7900 		lpp->param_values[1] = 0;
7901 		break;
7902 #if defined(SATA_DEBUG)
7903 	default:
7904 		cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value");
7905 		/* NOTREACHED */
7906 #endif
7907 	}
7908 
7909 	sata_id = &sdinfo->satadrv_id;
7910 	if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP))
7911 		temp = SMART_NO_TEMP;
7912 	else {
7913 		/* Now get the temperature */
7914 		smart_data = kmem_zalloc(512, KM_SLEEP);
7915 		rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data,
7916 		    SCT_STATUS_LOG_PAGE, 1);
7917 		if (rval == -1)
7918 			temp = SMART_NO_TEMP;
7919 		else {
7920 			temp = smart_data[200];
7921 			if (temp & 0x80) {
7922 				if (temp & 0x7f)
7923 					temp = 0;
7924 				else
7925 					temp = SMART_NO_TEMP;
7926 			}
7927 		}
7928 		kmem_free(smart_data, 512);
7929 	}
7930 
7931 	lpp->param_values[2] = temp;	/* most recent temperature */
7932 	lpp->param_values[3] = 0;	/* required vendor specific byte */
7933 
7934 	lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN;
7935 
7936 
7937 	return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN);
7938 }
7939 
7940 /*
7941  * sata_build_lsense_page_30() is used to create the
7942  * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data).
7943  *
7944  * Takes a sata_drive_info t * and the address of a buffer
7945  * in which to create the page information as well as a sata_hba_inst_t *.
7946  *
7947  * Returns the number of bytes valid in the buffer.
7948  */
7949 static int
7950 sata_build_lsense_page_30(
7951 	sata_drive_info_t *sdinfo,
7952 	uint8_t *buf,
7953 	sata_hba_inst_t *sata_hba_inst)
7954 {
7955 	struct smart_data *smart_data = (struct smart_data *)buf;
7956 	int rval;
7957 
7958 	/* Now do the SMART READ DATA */
7959 	rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data);
7960 	if (rval == -1)
7961 		return (0);
7962 
7963 	return (sizeof (struct smart_data));
7964 }
7965 
7966 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */
7967 
7968 /*
7969  * Start command for ATAPI device.
7970  * This function processes scsi_pkt requests.
7971  * Only CD/DVD devices are supported.
7972  * Most commands are packet without any translation into Packet Command.
7973  * Some may be trapped and executed as SATA commands (not clear which one).
7974  *
7975  * Returns TRAN_ACCEPT if command is accepted for execution (or completed
7976  * execution).
7977  * Returns other TRAN_XXXX codes if command is not accepted or completed
7978  * (see return values for sata_hba_start()).
7979  *
7980  * Note:
7981  * Inquiry cdb format differs between transport version 2 and 3.
7982  * However, the transport version 3 devices that were checked did not adhere
7983  * to the specification (ignored MSB of the allocation length). Therefore,
7984  * the transport version is not checked, but Inquiry allocation length is
7985  * truncated to 255 bytes if the original allocation length set-up by the
7986  * target driver is greater than 255 bytes.
7987  */
7988 static int
7989 sata_txlt_atapi(sata_pkt_txlate_t *spx)
7990 {
7991 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7992 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
7993 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7994 	sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx);
7995 	sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba,
7996 	    &spx->txlt_sata_pkt->satapkt_device);
7997 	int cport = SATA_TXLT_CPORT(spx);
7998 	int cdblen;
7999 	int rval;
8000 	int synch;
8001 	union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp;
8002 
8003 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
8004 
8005 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
8006 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
8007 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
8008 		return (rval);
8009 	}
8010 
8011 	/*
8012 	 * ATAPI device executes some ATA commands in addition to MMC command
8013 	 * set. These ATA commands may be executed by the regular SATA
8014 	 * translation functions. None needs to be captured now.
8015 	 * Other commands belong to MMC command set and are delivered
8016 	 * to ATAPI device via Packet Command.
8017 	 */
8018 
8019 	/* Check the size of cdb */
8020 	cdblen = scsi_cdb_size[GETGROUP(cdbp)];
8021 	if (cdblen > sdinfo->satadrv_atapi_cdb_len) {
8022 		sata_log(NULL, CE_WARN,
8023 		    "sata: invalid ATAPI cdb length %d",
8024 		    scsipkt->pkt_cdblen);
8025 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
8026 		return (TRAN_BADPKT);
8027 	}
8028 
8029 	SATAATAPITRACE(spx, cdblen);
8030 
8031 	/*
8032 	 * For non-read/write commands we need to
8033 	 * map buffer
8034 	 */
8035 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
8036 	case SCMD_READ:
8037 	case SCMD_READ_G1:
8038 	case SCMD_READ_G5:
8039 	case SCMD_READ_G4:
8040 	case SCMD_WRITE:
8041 	case SCMD_WRITE_G1:
8042 	case SCMD_WRITE_G5:
8043 	case SCMD_WRITE_G4:
8044 		break;
8045 	default:
8046 		if (bp != NULL) {
8047 			if (bp->b_flags & (B_PHYS | B_PAGEIO))
8048 				bp_mapin(bp);
8049 		}
8050 		break;
8051 	}
8052 	/*
8053 	 * scmd->satacmd_flags.sata_data_direction default -
8054 	 * SATA_DIR_NODATA_XFER - is set by
8055 	 * sata_txlt_generic_pkt_info().
8056 	 */
8057 	if (scmd->satacmd_bp) {
8058 		if (scmd->satacmd_bp->b_flags & B_READ) {
8059 			scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
8060 		} else {
8061 			scmd->satacmd_flags.sata_data_direction =
8062 			    SATA_DIR_WRITE;
8063 		}
8064 	}
8065 
8066 	/*
8067 	 * Set up ATAPI packet command.
8068 	 */
8069 
8070 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
8071 
8072 	/* Copy cdb into sata_cmd */
8073 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
8074 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
8075 	bcopy(cdbp, scmd->satacmd_acdb, cdblen);
8076 
8077 	/* See note in the command header */
8078 	if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) {
8079 		if (scmd->satacmd_acdb[3] != 0)
8080 			scmd->satacmd_acdb[4] = 255;
8081 	}
8082 
8083 #ifdef SATA_DEBUG
8084 	if (sata_debug_flags & SATA_DBG_ATAPI) {
8085 		uint8_t *p = scmd->satacmd_acdb;
8086 		char buf[3 * SATA_ATAPI_MAX_CDB_LEN];
8087 
8088 		(void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN,
8089 		    "%02x %02x %02x %02x %02x %02x %02x %02x "
8090 		    "%2x %02x %02x %02x %02x %02x %02x %02x",
8091 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
8092 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
8093 		buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0';
8094 		cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf);
8095 	}
8096 #endif
8097 
8098 	/*
8099 	 * Preset request sense data to NO SENSE.
8100 	 * If there is no way to get error information via Request Sense,
8101 	 * the packet request sense data would not have to be modified by HBA,
8102 	 * but it could be returned as is.
8103 	 */
8104 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
8105 	sata_fixed_sense_data_preset(
8106 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
8107 
8108 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
8109 		/* Need callback function */
8110 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion;
8111 		synch = FALSE;
8112 	} else
8113 		synch = TRUE;
8114 
8115 	/* Transfer command to HBA */
8116 	if (sata_hba_start(spx, &rval) != 0) {
8117 		/* Pkt not accepted for execution */
8118 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
8119 		return (rval);
8120 	}
8121 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
8122 	/*
8123 	 * If execution is non-synchronous,
8124 	 * a callback function will handle potential errors, translate
8125 	 * the response and will do a callback to a target driver.
8126 	 * If it was synchronous, use the same framework callback to check
8127 	 * an execution status.
8128 	 */
8129 	if (synch) {
8130 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
8131 		    "synchronous execution status %x\n",
8132 		    spx->txlt_sata_pkt->satapkt_reason);
8133 		sata_txlt_atapi_completion(spx->txlt_sata_pkt);
8134 	}
8135 	return (TRAN_ACCEPT);
8136 }
8137 
8138 
8139 /*
8140  * ATAPI Packet command completion.
8141  *
8142  * Failure of the command passed via Packet command are considered device
8143  * error. SATA HBA driver would have to retrieve error data (via Request
8144  * Sense command delivered via error retrieval sata packet) and copy it
8145  * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data.
8146  */
8147 static void
8148 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt)
8149 {
8150 	sata_pkt_txlate_t *spx =
8151 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
8152 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
8153 	struct scsi_extended_sense *sense;
8154 	struct buf *bp;
8155 	int rval;
8156 
8157 #ifdef SATA_DEBUG
8158 	uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense;
8159 #endif
8160 
8161 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
8162 	    STATE_SENT_CMD | STATE_GOT_STATUS;
8163 
8164 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
8165 		/* Normal completion */
8166 		if (sata_pkt->satapkt_cmd.satacmd_bp != NULL)
8167 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
8168 		scsipkt->pkt_reason = CMD_CMPLT;
8169 		*scsipkt->pkt_scbp = STATUS_GOOD;
8170 		if (spx->txlt_tmp_buf != NULL) {
8171 			/* Temporary buffer was used */
8172 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
8173 			if (bp->b_flags & B_READ) {
8174 				rval = ddi_dma_sync(
8175 				    spx->txlt_buf_dma_handle, 0, 0,
8176 				    DDI_DMA_SYNC_FORCPU);
8177 				ASSERT(rval == DDI_SUCCESS);
8178 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
8179 				    bp->b_bcount);
8180 			}
8181 		}
8182 	} else {
8183 		/*
8184 		 * Something went wrong - analyze return
8185 		 */
8186 		*scsipkt->pkt_scbp = STATUS_CHECK;
8187 		sense = sata_arq_sense(spx);
8188 
8189 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
8190 			scsipkt->pkt_reason = CMD_INCOMPLETE;
8191 			/*
8192 			 * We may not have ARQ data if there was a double
8193 			 * error. But sense data in sata packet was pre-set
8194 			 * with NO SENSE so it is valid even if HBA could
8195 			 * not retrieve a real sense data.
8196 			 * Just copy this sense data into scsi pkt sense area.
8197 			 */
8198 			bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense,
8199 			    SATA_ATAPI_MIN_RQSENSE_LEN);
8200 #ifdef SATA_DEBUG
8201 			if (sata_debug_flags & SATA_DBG_SCSI_IF) {
8202 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
8203 				    "sata_txlt_atapi_completion: %02x\n"
8204 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
8205 				    "          %02x %02x %02x %02x %02x %02x "
8206 				    "          %02x %02x %02x %02x %02x %02x\n",
8207 				    scsipkt->pkt_reason,
8208 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
8209 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
8210 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
8211 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
8212 				    rqsp[16], rqsp[17]);
8213 			}
8214 #endif
8215 		} else {
8216 			switch (sata_pkt->satapkt_reason) {
8217 			case SATA_PKT_PORT_ERROR:
8218 				/*
8219 				 * We have no device data.
8220 				 */
8221 				scsipkt->pkt_reason = CMD_INCOMPLETE;
8222 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
8223 				    STATE_GOT_TARGET | STATE_SENT_CMD |
8224 				    STATE_GOT_STATUS);
8225 				sense->es_key = KEY_HARDWARE_ERROR;
8226 
8227 				/* No extended sense key - no info available */
8228 				scsipkt->pkt_reason = CMD_INCOMPLETE;
8229 				break;
8230 
8231 			case SATA_PKT_TIMEOUT:
8232 				/* scsipkt->pkt_reason = CMD_TIMEOUT; */
8233 				/* No extended sense key */
8234 				/*
8235 				 * Need to check if HARDWARE_ERROR/
8236 				 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more
8237 				 * appropriate.
8238 				 */
8239 				break;
8240 
8241 			case SATA_PKT_ABORTED:
8242 				scsipkt->pkt_reason = CMD_ABORTED;
8243 				/* Should we set key COMMAND_ABPRTED? */
8244 				break;
8245 
8246 			case SATA_PKT_RESET:
8247 				scsipkt->pkt_reason = CMD_RESET;
8248 				/*
8249 				 * May be we should set Unit Attention /
8250 				 * Reset. Perhaps the same should be
8251 				 * returned for disks....
8252 				 */
8253 				sense->es_key = KEY_UNIT_ATTENTION;
8254 				sense->es_add_code = SD_SCSI_ASC_RESET;
8255 				break;
8256 
8257 			default:
8258 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
8259 				    "sata_txlt_atapi_completion: "
8260 				    "invalid packet completion reason"));
8261 				scsipkt->pkt_reason = CMD_TRAN_ERR;
8262 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
8263 				    STATE_GOT_TARGET | STATE_SENT_CMD |
8264 				    STATE_GOT_STATUS);
8265 				break;
8266 			}
8267 		}
8268 	}
8269 
8270 	SATAATAPITRACE(spx, 0);
8271 
8272 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
8273 	    scsipkt->pkt_comp != NULL) {
8274 		/* scsi callback required */
8275 		(*scsipkt->pkt_comp)(scsipkt);
8276 	}
8277 }
8278 
8279 /*
8280  * Set up error retrieval sata command for ATAPI Packet Command error data
8281  * recovery.
8282  *
8283  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
8284  * returns SATA_FAILURE otherwise.
8285  */
8286 
8287 static int
8288 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
8289 {
8290 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
8291 	sata_cmd_t *scmd;
8292 	struct buf *bp;
8293 
8294 	/*
8295 	 * Allocate dma-able buffer error data.
8296 	 * Buffer allocation will take care of buffer alignment and other DMA
8297 	 * attributes.
8298 	 */
8299 	bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN);
8300 	if (bp == NULL) {
8301 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
8302 		    "sata_get_err_retrieval_pkt: "
8303 		    "cannot allocate buffer for error data", NULL);
8304 		return (SATA_FAILURE);
8305 	}
8306 	bp_mapin(bp); /* make data buffer accessible */
8307 
8308 	/* Operation modes are up to the caller */
8309 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
8310 
8311 	/* Synchronous mode, no callback - may be changed by the caller */
8312 	spkt->satapkt_comp = NULL;
8313 	spkt->satapkt_time = sata_default_pkt_time;
8314 
8315 	scmd = &spkt->satapkt_cmd;
8316 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
8317 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
8318 
8319 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
8320 
8321 	/*
8322 	 * Set-up acdb. Request Sense CDB (packet command content) is
8323 	 * not in DMA-able buffer. Its handling is HBA-specific (how
8324 	 * it is transfered into packet FIS).
8325 	 */
8326 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
8327 	bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN);
8328 	/* Following zeroing of pad bytes may not be necessary */
8329 	bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN],
8330 	    sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN);
8331 
8332 	/*
8333 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
8334 	 * before accessing it. Handle is in usual place in translate struct.
8335 	 */
8336 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
8337 
8338 	/*
8339 	 * Preset request sense data to NO SENSE.
8340 	 * Here it is redundant, only for a symetry with scsi-originated
8341 	 * packets. It should not be used for anything but debugging.
8342 	 */
8343 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
8344 	sata_fixed_sense_data_preset(
8345 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
8346 
8347 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
8348 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
8349 
8350 	return (SATA_SUCCESS);
8351 }
8352 
8353 /*
8354  * Set-up ATAPI packet command.
8355  * Data transfer direction has to be set-up in sata_cmd structure prior to
8356  * calling this function.
8357  *
8358  * Returns void
8359  */
8360 
8361 static void
8362 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo)
8363 {
8364 	scmd->satacmd_addr_type = 0;		/* N/A */
8365 	scmd->satacmd_sec_count_lsb = 0;	/* no tag */
8366 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
8367 	scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ;
8368 	scmd->satacmd_lba_high_lsb =
8369 	    (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8);
8370 	scmd->satacmd_cmd_reg = SATAC_PACKET;	/* Command */
8371 
8372 	/*
8373 	 * We want all data to be transfered via DMA.
8374 	 * But specify it only if drive supports DMA and DMA mode is
8375 	 * selected - some drives are sensitive about it.
8376 	 * Hopefully it wil work for all drives....
8377 	 */
8378 	if (sdinfo->satadrv_settings & SATA_DEV_DMA)
8379 		scmd->satacmd_features_reg = SATA_ATAPI_F_DMA;
8380 
8381 	/*
8382 	 * Features register requires special care for devices that use
8383 	 * Serial ATA bridge - they need an explicit specification of
8384 	 * the data transfer direction for Packet DMA commands.
8385 	 * Setting this bit is harmless if DMA is not used.
8386 	 *
8387 	 * Many drives do not implement word 80, specifying what ATA/ATAPI
8388 	 * spec they follow.
8389 	 * We are arbitrarily following the latest SerialATA 2.6 spec,
8390 	 * which uses ATA/ATAPI 6 specification for Identify Data, unless
8391 	 * ATA/ATAPI-7 support is explicitly indicated.
8392 	 */
8393 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
8394 	    sdinfo->satadrv_id.ai_majorversion != 0xffff &&
8395 	    (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) {
8396 		/*
8397 		 * Specification of major version is valid and version 7
8398 		 * is supported. It does automatically imply that all
8399 		 * spec features are supported. For now, we assume that
8400 		 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete.
8401 		 */
8402 		if ((sdinfo->satadrv_id.ai_dirdma &
8403 		    SATA_ATAPI_ID_DMADIR_REQ) != 0) {
8404 			if (scmd->satacmd_flags.sata_data_direction ==
8405 			    SATA_DIR_READ)
8406 			scmd->satacmd_features_reg |=
8407 			    SATA_ATAPI_F_DATA_DIR_READ;
8408 		}
8409 	}
8410 }
8411 
8412 
8413 #ifdef SATA_DEBUG
8414 
8415 /* Display 18 bytes of Inquiry data */
8416 static void
8417 sata_show_inqry_data(uint8_t *buf)
8418 {
8419 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
8420 	uint8_t *p;
8421 
8422 	cmn_err(CE_NOTE, "Inquiry data:");
8423 	cmn_err(CE_NOTE, "device type %x", inq->inq_dtype);
8424 	cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb);
8425 	cmn_err(CE_NOTE, "version %x", inq->inq_ansi);
8426 	cmn_err(CE_NOTE, "ATAPI transport version %d",
8427 	    SATA_ATAPI_TRANS_VERSION(inq));
8428 	cmn_err(CE_NOTE, "response data format %d, aenc %d",
8429 	    inq->inq_rdf, inq->inq_aenc);
8430 	cmn_err(CE_NOTE, " additional length %d", inq->inq_len);
8431 	cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs);
8432 	p = (uint8_t *)inq->inq_vid;
8433 	cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x "
8434 	    "%02x %02x %02x %02x",
8435 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
8436 	p = (uint8_t *)inq->inq_vid;
8437 	cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c",
8438 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
8439 
8440 	p = (uint8_t *)inq->inq_pid;
8441 	cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x "
8442 	    "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
8443 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
8444 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
8445 	p = (uint8_t *)inq->inq_pid;
8446 	cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c "
8447 	    "%c %c %c %c %c %c %c %c",
8448 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
8449 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
8450 
8451 	p = (uint8_t *)inq->inq_revision;
8452 	cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x",
8453 	    p[0], p[1], p[2], p[3]);
8454 	p = (uint8_t *)inq->inq_revision;
8455 	cmn_err(CE_NOTE, "revision: %c %c %c %c",
8456 	    p[0], p[1], p[2], p[3]);
8457 
8458 }
8459 
8460 
8461 static void
8462 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count)
8463 {
8464 	struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt;
8465 
8466 	if (scsi_pkt == NULL)
8467 		return;
8468 	if (count != 0) {
8469 		/* saving cdb */
8470 		bzero(sata_atapi_trace[sata_atapi_trace_index].acdb,
8471 		    SATA_ATAPI_MAX_CDB_LEN);
8472 		bcopy(scsi_pkt->pkt_cdbp,
8473 		    sata_atapi_trace[sata_atapi_trace_index].acdb, count);
8474 	} else {
8475 		bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)->
8476 		    sts_sensedata,
8477 		    sata_atapi_trace[sata_atapi_trace_index].arqs,
8478 		    SATA_ATAPI_MIN_RQSENSE_LEN);
8479 		sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason =
8480 		    scsi_pkt->pkt_reason;
8481 		sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason =
8482 		    spx->txlt_sata_pkt->satapkt_reason;
8483 
8484 		if (++sata_atapi_trace_index >= 64)
8485 			sata_atapi_trace_index = 0;
8486 	}
8487 }
8488 
8489 #endif
8490 
8491 /*
8492  * Fetch inquiry data from ATAPI device
8493  * Returns SATA_SUCCESS if operation was successfull, SATA_FAILURE otherwise.
8494  *
8495  * Note:
8496  * inqb pointer does not point to a DMA-able buffer. It is a local buffer
8497  * where the caller expects to see the inquiry data.
8498  *
8499  */
8500 
8501 static int
8502 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba,
8503     sata_address_t *saddr, struct scsi_inquiry *inq)
8504 {
8505 	sata_pkt_txlate_t *spx;
8506 	sata_pkt_t *spkt;
8507 	struct buf *bp;
8508 	sata_drive_info_t *sdinfo;
8509 	sata_cmd_t *scmd;
8510 	int rval;
8511 	uint8_t *rqsp;
8512 #ifdef SATA_DEBUG
8513 	char msg_buf[MAXPATHLEN];
8514 #endif
8515 
8516 	ASSERT(sata_hba != NULL);
8517 
8518 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
8519 	spx->txlt_sata_hba_inst = sata_hba;
8520 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
8521 	spkt = sata_pkt_alloc(spx, NULL);
8522 	if (spkt == NULL) {
8523 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
8524 		return (SATA_FAILURE);
8525 	}
8526 	/* address is needed now */
8527 	spkt->satapkt_device.satadev_addr = *saddr;
8528 
8529 	/* scsi_inquiry size buffer */
8530 	bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry));
8531 	if (bp == NULL) {
8532 		sata_pkt_free(spx);
8533 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
8534 		SATA_LOG_D((sata_hba, CE_WARN,
8535 		    "sata_get_atapi_inquiry_data: "
8536 		    "cannot allocate data buffer"));
8537 		return (SATA_FAILURE);
8538 	}
8539 	bp_mapin(bp); /* make data buffer accessible */
8540 
8541 	scmd = &spkt->satapkt_cmd;
8542 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
8543 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
8544 
8545 	/* Use synchronous mode */
8546 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
8547 	spkt->satapkt_comp = NULL;
8548 	spkt->satapkt_time = sata_default_pkt_time;
8549 
8550 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
8551 
8552 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
8553 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
8554 
8555 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
8556 	sdinfo = sata_get_device_info(sata_hba,
8557 	    &spx->txlt_sata_pkt->satapkt_device);
8558 	if (sdinfo == NULL) {
8559 		/* we have to be carefull about the disapearing device */
8560 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
8561 		rval = SATA_FAILURE;
8562 		goto cleanup;
8563 	}
8564 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
8565 
8566 	/*
8567 	 * Set-up acdb. This works for atapi transport version 2 and later.
8568 	 */
8569 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
8570 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
8571 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
8572 	scmd->satacmd_acdb[1] = 0x00;
8573 	scmd->satacmd_acdb[2] = 0x00;
8574 	scmd->satacmd_acdb[3] = 0x00;
8575 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
8576 	scmd->satacmd_acdb[5] = 0x00;
8577 
8578 	sata_fixed_sense_data_preset(
8579 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
8580 
8581 	/* Transfer command to HBA */
8582 	if (sata_hba_start(spx, &rval) != 0) {
8583 		/* Pkt not accepted for execution */
8584 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
8585 		    "sata_get_atapi_inquiry_data: "
8586 		    "Packet not accepted for execution - ret: %02x", rval);
8587 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
8588 		rval = SATA_FAILURE;
8589 		goto cleanup;
8590 	}
8591 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
8592 
8593 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
8594 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
8595 		    "sata_get_atapi_inquiry_data: "
8596 		    "Packet completed successfully - ret: %02x", rval);
8597 		/*
8598 		 * Sync buffer. Handle is in usual place in translate struct.
8599 		 * Normal completion - copy data into caller's buffer
8600 		 */
8601 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
8602 		    DDI_DMA_SYNC_FORCPU);
8603 		ASSERT(rval == DDI_SUCCESS);
8604 		bcopy(bp->b_un.b_addr, (uint8_t *)inq,
8605 		    sizeof (struct scsi_inquiry));
8606 #ifdef SATA_DEBUG
8607 		if (sata_debug_flags & SATA_DBG_ATAPI) {
8608 			sata_show_inqry_data((uint8_t *)inq);
8609 		}
8610 #endif
8611 		rval = SATA_SUCCESS;
8612 	} else {
8613 		/*
8614 		 * Something went wrong - analyze return - check rqsense data
8615 		 */
8616 		rval = SATA_FAILURE;
8617 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
8618 			/*
8619 			 * ARQ data hopefull show something other than NO SENSE
8620 			 */
8621 			rqsp = scmd->satacmd_rqsense;
8622 #ifdef SATA_DEBUG
8623 			if (sata_debug_flags & SATA_DBG_ATAPI) {
8624 				msg_buf[0] = '\0';
8625 				(void) snprintf(msg_buf, MAXPATHLEN,
8626 				    "ATAPI packet completion reason: %02x\n"
8627 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x\n"
8628 				    "          %02x %02x %02x %02x %02x %02x\n"
8629 				    "          %02x %02x %02x %02x %02x %02x",
8630 				    spkt->satapkt_reason,
8631 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
8632 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
8633 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
8634 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
8635 				    rqsp[16], rqsp[17]);
8636 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
8637 				    "%s", msg_buf);
8638 			}
8639 #endif
8640 		} else {
8641 			switch (spkt->satapkt_reason) {
8642 			case SATA_PKT_PORT_ERROR:
8643 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
8644 				    "sata_get_atapi_inquiry_data: "
8645 				    "packet reason: port error", NULL);
8646 				break;
8647 
8648 			case SATA_PKT_TIMEOUT:
8649 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
8650 				    "sata_get_atapi_inquiry_data: "
8651 				    "packet reason: timeout", NULL);
8652 				break;
8653 
8654 			case SATA_PKT_ABORTED:
8655 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
8656 				    "sata_get_atapi_inquiry_data: "
8657 				    "packet reason: aborted", NULL);
8658 				break;
8659 
8660 			case SATA_PKT_RESET:
8661 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
8662 				    "sata_get_atapi_inquiry_data: "
8663 				    "packet reason: reset\n", NULL);
8664 				break;
8665 			default:
8666 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
8667 				    "sata_get_atapi_inquiry_data: "
8668 				    "invalid packet reason: %02x\n",
8669 				    spkt->satapkt_reason);
8670 				break;
8671 			}
8672 		}
8673 	}
8674 cleanup:
8675 	sata_free_local_buffer(spx);
8676 	sata_pkt_free(spx);
8677 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
8678 	return (rval);
8679 }
8680 
8681 
8682 
8683 
8684 
8685 #if 0
8686 #ifdef SATA_DEBUG
8687 
8688 /*
8689  * Test ATAPI packet command.
8690  * Single threaded test: send packet command in synch mode, process completion
8691  *
8692  */
8693 static void
8694 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport)
8695 {
8696 	sata_pkt_txlate_t *spx;
8697 	sata_pkt_t *spkt;
8698 	struct buf *bp;
8699 	sata_device_t sata_device;
8700 	sata_drive_info_t *sdinfo;
8701 	sata_cmd_t *scmd;
8702 	int rval;
8703 	uint8_t *rqsp;
8704 
8705 	ASSERT(sata_hba_inst != NULL);
8706 	sata_device.satadev_addr.cport = cport;
8707 	sata_device.satadev_addr.pmport = 0;
8708 	sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
8709 	sata_device.satadev_rev = SATA_DEVICE_REV;
8710 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8711 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
8712 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8713 	if (sdinfo == NULL) {
8714 		sata_log(sata_hba_inst, CE_WARN,
8715 		    "sata_test_atapi_packet_command: "
8716 		    "no device info for cport %d",
8717 		    sata_device.satadev_addr.cport);
8718 		return;
8719 	}
8720 
8721 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
8722 	spx->txlt_sata_hba_inst = sata_hba_inst;
8723 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
8724 	spkt = sata_pkt_alloc(spx, NULL);
8725 	if (spkt == NULL) {
8726 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
8727 		return;
8728 	}
8729 	/* address is needed now */
8730 	spkt->satapkt_device.satadev_addr = sata_device.satadev_addr;
8731 
8732 	/* 1024k buffer */
8733 	bp = sata_alloc_local_buffer(spx, 1024);
8734 	if (bp == NULL) {
8735 		sata_pkt_free(spx);
8736 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
8737 		sata_log(sata_hba_inst, CE_WARN,
8738 		    "sata_test_atapi_packet_command: "
8739 		    "cannot allocate data buffer");
8740 		return;
8741 	}
8742 	bp_mapin(bp); /* make data buffer accessible */
8743 
8744 	scmd = &spkt->satapkt_cmd;
8745 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
8746 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
8747 
8748 	/* Use synchronous mode */
8749 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
8750 
8751 	/* Synchronous mode, no callback - may be changed by the caller */
8752 	spkt->satapkt_comp = NULL;
8753 	spkt->satapkt_time = sata_default_pkt_time;
8754 
8755 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
8756 
8757 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
8758 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
8759 
8760 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
8761 
8762 	/* Set-up acdb. */
8763 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
8764 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
8765 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
8766 	scmd->satacmd_acdb[1] = 0x00;
8767 	scmd->satacmd_acdb[2] = 0x00;
8768 	scmd->satacmd_acdb[3] = 0x00;
8769 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
8770 	scmd->satacmd_acdb[5] = 0x00;
8771 
8772 	sata_fixed_sense_data_preset(
8773 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
8774 
8775 	/* Transfer command to HBA */
8776 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8777 	if (sata_hba_start(spx, &rval) != 0) {
8778 		/* Pkt not accepted for execution */
8779 		sata_log(sata_hba_inst, CE_WARN,
8780 		    "sata_test_atapi_packet_command: "
8781 		    "Packet not accepted for execution - ret: %02x", rval);
8782 		mutex_exit(
8783 		    &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8784 		goto cleanup;
8785 	}
8786 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
8787 
8788 	/*
8789 	 * Sync buffer. Handle is in usual place in translate struct.
8790 	 */
8791 	rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
8792 	    DDI_DMA_SYNC_FORCPU);
8793 	ASSERT(rval == DDI_SUCCESS);
8794 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
8795 		sata_log(sata_hba_inst, CE_WARN,
8796 		    "sata_test_atapi_packet_command: "
8797 		    "Packet completed successfully");
8798 		/*
8799 		 * Normal completion - show inquiry data
8800 		 */
8801 		sata_show_inqry_data((uint8_t *)bp->b_un.b_addr);
8802 	} else {
8803 		/*
8804 		 * Something went wrong - analyze return - check rqsense data
8805 		 */
8806 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
8807 			/*
8808 			 * ARQ data hopefull show something other than NO SENSE
8809 			 */
8810 			rqsp = scmd->satacmd_rqsense;
8811 			sata_log(spx->txlt_sata_hba_inst, CE_WARN,
8812 			    "ATAPI packet completion reason: %02x\n"
8813 			    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
8814 			    "          %02x %02x %02x %02x %02x %02x "
8815 			    "          %02x %02x %02x %02x %02x %02x\n",
8816 			    spkt->satapkt_reason,
8817 			    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
8818 			    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
8819 			    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
8820 			    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
8821 			    rqsp[16], rqsp[17]);
8822 		} else {
8823 			switch (spkt->satapkt_reason) {
8824 			case SATA_PKT_PORT_ERROR:
8825 				sata_log(sata_hba_inst, CE_WARN,
8826 				    "sata_test_atapi_packet_command: "
8827 				    "packet reason: port error\n");
8828 				break;
8829 
8830 			case SATA_PKT_TIMEOUT:
8831 				sata_log(sata_hba_inst, CE_WARN,
8832 				    "sata_test_atapi_packet_command: "
8833 				    "packet reason: timeout\n");
8834 				break;
8835 
8836 			case SATA_PKT_ABORTED:
8837 				sata_log(sata_hba_inst, CE_WARN,
8838 				    "sata_test_atapi_packet_command: "
8839 				    "packet reason: aborted\n");
8840 				break;
8841 
8842 			case SATA_PKT_RESET:
8843 				sata_log(sata_hba_inst, CE_WARN,
8844 				    "sata_test_atapi_packet_command: "
8845 				    "packet reason: reset\n");
8846 				break;
8847 			default:
8848 				sata_log(sata_hba_inst, CE_WARN,
8849 				    "sata_test_atapi_packet_command: "
8850 				    "invalid packet reason: %02x\n",
8851 				    spkt->satapkt_reason);
8852 				break;
8853 			}
8854 		}
8855 	}
8856 cleanup:
8857 	sata_free_local_buffer(spx);
8858 	sata_pkt_free(spx);
8859 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
8860 }
8861 
8862 #endif /* SATA_DEBUG */
8863 #endif /* 1 */
8864 
8865 
8866 /* ************************** LOCAL HELPER FUNCTIONS *********************** */
8867 
8868 /*
8869  * Validate sata_tran info
8870  * SATA_FAILURE returns if structure is inconsistent or structure revision
8871  * does not match one used by the framework.
8872  *
8873  * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains
8874  * required function pointers.
8875  * Returns SATA_FAILURE otherwise.
8876  */
8877 static int
8878 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran)
8879 {
8880 	/*
8881 	 * SATA_TRAN_HBA_REV is the current (highest) revision number
8882 	 * of the SATA interface.
8883 	 */
8884 	if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) {
8885 		sata_log(NULL, CE_WARN,
8886 		    "sata: invalid sata_hba_tran version %d for driver %s",
8887 		    sata_tran->sata_tran_hba_rev, ddi_driver_name(dip));
8888 		return (SATA_FAILURE);
8889 	}
8890 
8891 	if (dip != sata_tran->sata_tran_hba_dip) {
8892 		SATA_LOG_D((NULL, CE_WARN,
8893 		    "sata: inconsistent sata_tran_hba_dip "
8894 		    "%p / %p", sata_tran->sata_tran_hba_dip, dip));
8895 		return (SATA_FAILURE);
8896 	}
8897 
8898 	if (sata_tran->sata_tran_probe_port == NULL ||
8899 	    sata_tran->sata_tran_start == NULL ||
8900 	    sata_tran->sata_tran_abort == NULL ||
8901 	    sata_tran->sata_tran_reset_dport == NULL ||
8902 	    sata_tran->sata_tran_hotplug_ops == NULL ||
8903 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL ||
8904 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate ==
8905 	    NULL) {
8906 		SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing "
8907 		    "required functions"));
8908 	}
8909 	return (SATA_SUCCESS);
8910 }
8911 
8912 /*
8913  * Remove HBA instance from sata_hba_list.
8914  */
8915 static void
8916 sata_remove_hba_instance(dev_info_t *dip)
8917 {
8918 	sata_hba_inst_t	*sata_hba_inst;
8919 
8920 	mutex_enter(&sata_mutex);
8921 	for (sata_hba_inst = sata_hba_list;
8922 	    sata_hba_inst != (struct sata_hba_inst *)NULL;
8923 	    sata_hba_inst = sata_hba_inst->satahba_next) {
8924 		if (sata_hba_inst->satahba_dip == dip)
8925 			break;
8926 	}
8927 
8928 	if (sata_hba_inst == (struct sata_hba_inst *)NULL) {
8929 #ifdef SATA_DEBUG
8930 		cmn_err(CE_WARN, "sata_remove_hba_instance: "
8931 		    "unknown HBA instance\n");
8932 #endif
8933 		ASSERT(FALSE);
8934 	}
8935 	if (sata_hba_inst == sata_hba_list) {
8936 		sata_hba_list = sata_hba_inst->satahba_next;
8937 		if (sata_hba_list) {
8938 			sata_hba_list->satahba_prev =
8939 			    (struct sata_hba_inst *)NULL;
8940 		}
8941 		if (sata_hba_inst == sata_hba_list_tail) {
8942 			sata_hba_list_tail = NULL;
8943 		}
8944 	} else if (sata_hba_inst == sata_hba_list_tail) {
8945 		sata_hba_list_tail = sata_hba_inst->satahba_prev;
8946 		if (sata_hba_list_tail) {
8947 			sata_hba_list_tail->satahba_next =
8948 			    (struct sata_hba_inst *)NULL;
8949 		}
8950 	} else {
8951 		sata_hba_inst->satahba_prev->satahba_next =
8952 		    sata_hba_inst->satahba_next;
8953 		sata_hba_inst->satahba_next->satahba_prev =
8954 		    sata_hba_inst->satahba_prev;
8955 	}
8956 	mutex_exit(&sata_mutex);
8957 }
8958 
8959 
8960 
8961 
8962 
8963 /*
8964  * Probe all SATA ports of the specified HBA instance.
8965  * The assumption is that there are no target and attachment point minor nodes
8966  * created by the boot subsystems, so we do not need to prune device tree.
8967  *
8968  * This function is called only from sata_hba_attach(). It does not have to
8969  * be protected by controller mutex, because the hba_attached flag is not set
8970  * yet and no one would be touching this HBA instance other than this thread.
8971  * Determines if port is active and what type of the device is attached
8972  * (if any). Allocates necessary structures for each port.
8973  *
8974  * An AP (Attachement Point) node is created for each SATA device port even
8975  * when there is no device attached.
8976  */
8977 
8978 static 	void
8979 sata_probe_ports(sata_hba_inst_t *sata_hba_inst)
8980 {
8981 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
8982 	int			ncport, npmport;
8983 	sata_cport_info_t 	*cportinfo;
8984 	sata_drive_info_t	*drive;
8985 	sata_pmult_info_t	*pminfo;
8986 	sata_pmport_info_t 	*pmportinfo;
8987 	sata_device_t		sata_device;
8988 	int			rval;
8989 	dev_t			minor_number;
8990 	char			name[16];
8991 	clock_t			start_time, cur_time;
8992 
8993 	/*
8994 	 * Probe controller ports first, to find port status and
8995 	 * any port multiplier attached.
8996 	 */
8997 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
8998 		/* allocate cport structure */
8999 		cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP);
9000 		ASSERT(cportinfo != NULL);
9001 		mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL);
9002 
9003 		mutex_enter(&cportinfo->cport_mutex);
9004 
9005 		cportinfo->cport_addr.cport = ncport;
9006 		cportinfo->cport_addr.pmport = 0;
9007 		cportinfo->cport_addr.qual = SATA_ADDR_CPORT;
9008 		cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
9009 		cportinfo->cport_state |= SATA_STATE_PROBING;
9010 		SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo;
9011 
9012 		/*
9013 		 * Regardless if a port is usable or not, create
9014 		 * an attachment point
9015 		 */
9016 		mutex_exit(&cportinfo->cport_mutex);
9017 		minor_number =
9018 		    SATA_MAKE_AP_MINOR(ddi_get_instance(dip), ncport, 0, 0);
9019 		(void) sprintf(name, "%d", ncport);
9020 		if (ddi_create_minor_node(dip, name, S_IFCHR,
9021 		    minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) !=
9022 		    DDI_SUCCESS) {
9023 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
9024 			    "cannot create SATA attachment point for port %d",
9025 			    ncport);
9026 		}
9027 
9028 		/* Probe port */
9029 		start_time = ddi_get_lbolt();
9030 	reprobe_cport:
9031 		sata_device.satadev_addr.cport = ncport;
9032 		sata_device.satadev_addr.pmport = 0;
9033 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
9034 		sata_device.satadev_rev = SATA_DEVICE_REV;
9035 
9036 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
9037 		    (dip, &sata_device);
9038 
9039 		mutex_enter(&cportinfo->cport_mutex);
9040 		sata_update_port_scr(&cportinfo->cport_scr, &sata_device);
9041 		if (rval != SATA_SUCCESS) {
9042 			/* Something went wrong? Fail the port */
9043 			cportinfo->cport_state = SATA_PSTATE_FAILED;
9044 			mutex_exit(&cportinfo->cport_mutex);
9045 			continue;
9046 		}
9047 		cportinfo->cport_state &= ~SATA_STATE_PROBING;
9048 		cportinfo->cport_state |= SATA_STATE_PROBED;
9049 		cportinfo->cport_dev_type = sata_device.satadev_type;
9050 
9051 		cportinfo->cport_state |= SATA_STATE_READY;
9052 		if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) {
9053 			mutex_exit(&cportinfo->cport_mutex);
9054 			continue;
9055 		}
9056 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
9057 			/*
9058 			 * There is some device attached.
9059 			 * Allocate device info structure
9060 			 */
9061 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) {
9062 				mutex_exit(&cportinfo->cport_mutex);
9063 				SATA_CPORTINFO_DRV_INFO(cportinfo) =
9064 				    kmem_zalloc(sizeof (sata_drive_info_t),
9065 				    KM_SLEEP);
9066 				mutex_enter(&cportinfo->cport_mutex);
9067 			}
9068 			drive = SATA_CPORTINFO_DRV_INFO(cportinfo);
9069 			drive->satadrv_addr = cportinfo->cport_addr;
9070 			drive->satadrv_addr.qual = SATA_ADDR_DCPORT;
9071 			drive->satadrv_type = cportinfo->cport_dev_type;
9072 			drive->satadrv_state = SATA_STATE_UNKNOWN;
9073 
9074 			mutex_exit(&cportinfo->cport_mutex);
9075 			if (sata_add_device(dip, sata_hba_inst, ncport, 0) !=
9076 			    SATA_SUCCESS) {
9077 				/*
9078 				 * Plugged device was not correctly identified.
9079 				 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT
9080 				 */
9081 				cur_time = ddi_get_lbolt();
9082 				if ((cur_time - start_time) <
9083 				    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
9084 					/* sleep for a while */
9085 					delay(drv_usectohz(
9086 					    SATA_DEV_IDENTIFY_RETRY_DELAY));
9087 					goto reprobe_cport;
9088 				}
9089 			}
9090 		} else {
9091 			mutex_exit(&cportinfo->cport_mutex);
9092 			ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT);
9093 			pminfo = kmem_zalloc(sizeof (sata_pmult_info_t),
9094 			    KM_SLEEP);
9095 			mutex_enter(&cportinfo->cport_mutex);
9096 			ASSERT(pminfo != NULL);
9097 			SATA_CPORTINFO_PMULT_INFO(cportinfo) = pminfo;
9098 			pminfo->pmult_addr.cport = cportinfo->cport_addr.cport;
9099 			pminfo->pmult_addr.pmport = SATA_PMULT_HOSTPORT;
9100 			pminfo->pmult_addr.qual = SATA_ADDR_PMPORT;
9101 			pminfo->pmult_num_dev_ports =
9102 			    sata_device.satadev_add_info;
9103 			mutex_init(&pminfo->pmult_mutex, NULL, MUTEX_DRIVER,
9104 			    NULL);
9105 			pminfo->pmult_state = SATA_STATE_PROBING;
9106 			mutex_exit(&cportinfo->cport_mutex);
9107 
9108 			/* Probe Port Multiplier ports */
9109 			for (npmport = 0;
9110 			    npmport < pminfo->pmult_num_dev_ports;
9111 			    npmport++) {
9112 				pmportinfo = kmem_zalloc(
9113 				    sizeof (sata_pmport_info_t), KM_SLEEP);
9114 				mutex_enter(&cportinfo->cport_mutex);
9115 				ASSERT(pmportinfo != NULL);
9116 				pmportinfo->pmport_addr.cport = ncport;
9117 				pmportinfo->pmport_addr.pmport = npmport;
9118 				pmportinfo->pmport_addr.qual =
9119 				    SATA_ADDR_PMPORT;
9120 				pminfo->pmult_dev_port[npmport] = pmportinfo;
9121 
9122 				mutex_init(&pmportinfo->pmport_mutex, NULL,
9123 				    MUTEX_DRIVER, NULL);
9124 
9125 				mutex_exit(&cportinfo->cport_mutex);
9126 
9127 				/* Create an attachment point */
9128 				minor_number = SATA_MAKE_AP_MINOR(
9129 				    ddi_get_instance(dip), ncport, npmport, 1);
9130 				(void) sprintf(name, "%d.%d", ncport, npmport);
9131 				if (ddi_create_minor_node(dip, name, S_IFCHR,
9132 				    minor_number, DDI_NT_SATA_ATTACHMENT_POINT,
9133 				    0) != DDI_SUCCESS) {
9134 					sata_log(sata_hba_inst, CE_WARN,
9135 					    "sata_hba_attach: "
9136 					    "cannot create SATA attachment "
9137 					    "point for port %d pmult port %d",
9138 					    ncport, npmport);
9139 				}
9140 
9141 				start_time = ddi_get_lbolt();
9142 			reprobe_pmport:
9143 				sata_device.satadev_addr.pmport = npmport;
9144 				sata_device.satadev_addr.qual =
9145 				    SATA_ADDR_PMPORT;
9146 
9147 				rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
9148 				    (dip, &sata_device);
9149 				mutex_enter(&cportinfo->cport_mutex);
9150 
9151 				/* sata_update_port_info() */
9152 				sata_update_port_scr(&pmportinfo->pmport_scr,
9153 				    &sata_device);
9154 
9155 				if (rval != SATA_SUCCESS) {
9156 					pmportinfo->pmport_state =
9157 					    SATA_PSTATE_FAILED;
9158 					mutex_exit(&cportinfo->cport_mutex);
9159 					continue;
9160 				}
9161 				pmportinfo->pmport_state &=
9162 				    ~SATA_STATE_PROBING;
9163 				pmportinfo->pmport_state |= SATA_STATE_PROBED;
9164 				pmportinfo->pmport_dev_type =
9165 				    sata_device.satadev_type;
9166 
9167 				pmportinfo->pmport_state |= SATA_STATE_READY;
9168 				if (pmportinfo->pmport_dev_type ==
9169 				    SATA_DTYPE_NONE) {
9170 					mutex_exit(&cportinfo->cport_mutex);
9171 					continue;
9172 				}
9173 				/* Port multipliers cannot be chained */
9174 				ASSERT(pmportinfo->pmport_dev_type !=
9175 				    SATA_DTYPE_PMULT);
9176 				/*
9177 				 * There is something attached to Port
9178 				 * Multiplier device port
9179 				 * Allocate device info structure
9180 				 */
9181 				if (pmportinfo->pmport_sata_drive == NULL) {
9182 					mutex_exit(&cportinfo->cport_mutex);
9183 					pmportinfo->pmport_sata_drive =
9184 					    kmem_zalloc(
9185 					    sizeof (sata_drive_info_t),
9186 					    KM_SLEEP);
9187 					mutex_enter(&cportinfo->cport_mutex);
9188 				}
9189 				drive = pmportinfo->pmport_sata_drive;
9190 				drive->satadrv_addr.cport =
9191 				    pmportinfo->pmport_addr.cport;
9192 				drive->satadrv_addr.pmport = npmport;
9193 				drive->satadrv_addr.qual = SATA_ADDR_DPMPORT;
9194 				drive->satadrv_type = pmportinfo->
9195 				    pmport_dev_type;
9196 				drive->satadrv_state = SATA_STATE_UNKNOWN;
9197 
9198 				mutex_exit(&cportinfo->cport_mutex);
9199 				if (sata_add_device(dip, sata_hba_inst, ncport,
9200 				    npmport) != SATA_SUCCESS) {
9201 					/*
9202 					 * Plugged device was not correctly
9203 					 * identified. Retry, within the
9204 					 * SATA_DEV_IDENTIFY_TIMEOUT
9205 					 */
9206 					cur_time = ddi_get_lbolt();
9207 					if ((cur_time - start_time) <
9208 					    drv_usectohz(
9209 					    SATA_DEV_IDENTIFY_TIMEOUT)) {
9210 						/* sleep for a while */
9211 						delay(drv_usectohz(
9212 						SATA_DEV_IDENTIFY_RETRY_DELAY));
9213 						goto reprobe_pmport;
9214 					}
9215 				}
9216 			}
9217 			pmportinfo->pmport_state =
9218 			    SATA_STATE_PROBED | SATA_STATE_READY;
9219 		}
9220 	}
9221 }
9222 
9223 /*
9224  * Add SATA device for specified HBA instance & port (SCSI target
9225  * device nodes).
9226  * This function is called (indirectly) only from sata_hba_attach().
9227  * A target node is created when there is a supported type device attached,
9228  * but may be removed if it cannot be put online.
9229  *
9230  * This function cannot be called from an interrupt context.
9231  *
9232  * ONLY DISK TARGET NODES ARE CREATED NOW
9233  *
9234  * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when
9235  * device identification failed - adding a device could be retried.
9236  *
9237  */
9238 static 	int
9239 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst, int cport,
9240     int pmport)
9241 {
9242 	sata_cport_info_t 	*cportinfo;
9243 	sata_pmult_info_t	*pminfo;
9244 	sata_pmport_info_t	*pmportinfo;
9245 	dev_info_t		*cdip;		/* child dip */
9246 	sata_device_t		sata_device;
9247 	int			rval;
9248 
9249 
9250 
9251 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
9252 	ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE);
9253 	mutex_enter(&cportinfo->cport_mutex);
9254 	/*
9255 	 * Some device is attached to a controller port.
9256 	 * We rely on controllers distinquishing between no-device,
9257 	 * attached port multiplier and other kind of attached device.
9258 	 * We need to get Identify Device data and determine
9259 	 * positively the dev type before trying to attach
9260 	 * the target driver.
9261 	 */
9262 	sata_device.satadev_rev = SATA_DEVICE_REV;
9263 	if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
9264 		/*
9265 		 * Not port multiplier.
9266 		 */
9267 		sata_device.satadev_addr = cportinfo->cport_addr;
9268 		sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
9269 		mutex_exit(&cportinfo->cport_mutex);
9270 
9271 		rval = sata_probe_device(sata_hba_inst, &sata_device);
9272 		if (rval != SATA_SUCCESS ||
9273 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN)
9274 			return (SATA_FAILURE);
9275 
9276 		mutex_enter(&cportinfo->cport_mutex);
9277 		sata_show_drive_info(sata_hba_inst,
9278 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
9279 
9280 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
9281 			/*
9282 			 * Could not determine device type or
9283 			 * a device is not supported.
9284 			 * Degrade this device to unknown.
9285 			 */
9286 			cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
9287 			mutex_exit(&cportinfo->cport_mutex);
9288 			return (SATA_SUCCESS);
9289 		}
9290 		cportinfo->cport_dev_type = sata_device.satadev_type;
9291 		cportinfo->cport_tgtnode_clean = B_TRUE;
9292 		mutex_exit(&cportinfo->cport_mutex);
9293 
9294 		/*
9295 		 * Initialize device to the desired state. Even if it
9296 		 * fails, the device will still attach but syslog
9297 		 * will show the warning.
9298 		 */
9299 		if (sata_initialize_device(sata_hba_inst,
9300 		    SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS)
9301 			/* Retry */
9302 			(void) sata_initialize_device(sata_hba_inst,
9303 			    SATA_CPORTINFO_DRV_INFO(cportinfo));
9304 
9305 		cdip = sata_create_target_node(pdip, sata_hba_inst,
9306 		    &sata_device.satadev_addr);
9307 		mutex_enter(&cportinfo->cport_mutex);
9308 		if (cdip == NULL) {
9309 			/*
9310 			 * Attaching target node failed.
9311 			 * We retain sata_drive_info structure...
9312 			 */
9313 			mutex_exit(&cportinfo->cport_mutex);
9314 			return (SATA_SUCCESS);
9315 		}
9316 		(SATA_CPORTINFO_DRV_INFO(cportinfo))->
9317 		    satadrv_state = SATA_STATE_READY;
9318 	} else {
9319 		/* This must be Port Multiplier type */
9320 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
9321 			SATA_LOG_D((sata_hba_inst, CE_WARN,
9322 			    "sata_add_device: "
9323 			    "unrecognized dev type %x",
9324 			    cportinfo->cport_dev_type));
9325 			mutex_exit(&cportinfo->cport_mutex);
9326 			return (SATA_SUCCESS);
9327 		}
9328 		pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
9329 		pmportinfo = pminfo->pmult_dev_port[pmport];
9330 		sata_device.satadev_addr = pmportinfo->pmport_addr;
9331 		sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
9332 		mutex_exit(&cportinfo->cport_mutex);
9333 
9334 		rval = sata_probe_device(sata_hba_inst, &sata_device);
9335 		if (rval != SATA_SUCCESS ||
9336 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN) {
9337 			return (SATA_FAILURE);
9338 		}
9339 		mutex_enter(&cportinfo->cport_mutex);
9340 		sata_show_drive_info(sata_hba_inst,
9341 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
9342 
9343 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
9344 			/*
9345 			 * Could not determine device type.
9346 			 * Degrade this device to unknown.
9347 			 */
9348 			pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
9349 			mutex_exit(&cportinfo->cport_mutex);
9350 			return (SATA_SUCCESS);
9351 		}
9352 		pmportinfo->pmport_dev_type = sata_device.satadev_type;
9353 		pmportinfo->pmport_tgtnode_clean = B_TRUE;
9354 		mutex_exit(&cportinfo->cport_mutex);
9355 
9356 		/*
9357 		 * Initialize device to the desired state.
9358 		 * Even if it fails, the device will still
9359 		 * attach but syslog will show the warning.
9360 		 */
9361 		if (sata_initialize_device(sata_hba_inst,
9362 		    pmportinfo->pmport_sata_drive) != SATA_SUCCESS)
9363 			/* Retry */
9364 			(void) sata_initialize_device(sata_hba_inst,
9365 			    pmportinfo->pmport_sata_drive);
9366 
9367 		cdip = sata_create_target_node(pdip, sata_hba_inst,
9368 		    &sata_device.satadev_addr);
9369 		mutex_enter(&cportinfo->cport_mutex);
9370 		if (cdip == NULL) {
9371 			/*
9372 			 * Attaching target node failed.
9373 			 * We retain sata_drive_info structure...
9374 			 */
9375 			mutex_exit(&cportinfo->cport_mutex);
9376 			return (SATA_SUCCESS);
9377 		}
9378 		pmportinfo->pmport_sata_drive->satadrv_state |=
9379 		    SATA_STATE_READY;
9380 	}
9381 	mutex_exit(&cportinfo->cport_mutex);
9382 	return (SATA_SUCCESS);
9383 }
9384 
9385 
9386 
9387 /*
9388  * Create scsi target node for attached device, create node properties and
9389  * attach the node.
9390  * The node could be removed if the device onlining fails.
9391  *
9392  * A dev_info_t pointer is returned if operation is successful, NULL is
9393  * returned otherwise.
9394  *
9395  * No port multiplier support.
9396  */
9397 
9398 static dev_info_t *
9399 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst,
9400 			sata_address_t *sata_addr)
9401 {
9402 	dev_info_t *cdip = NULL;
9403 	int rval;
9404 	char *nname = NULL;
9405 	char **compatible = NULL;
9406 	int ncompatible;
9407 	struct scsi_inquiry inq;
9408 	sata_device_t sata_device;
9409 	sata_drive_info_t *sdinfo;
9410 	int target;
9411 	int i;
9412 
9413 	sata_device.satadev_rev = SATA_DEVICE_REV;
9414 	sata_device.satadev_addr = *sata_addr;
9415 
9416 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport)));
9417 
9418 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
9419 
9420 	target = SATA_TO_SCSI_TARGET(sata_addr->cport,
9421 	    sata_addr->pmport, sata_addr->qual);
9422 
9423 	if (sdinfo == NULL) {
9424 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9425 		    sata_addr->cport)));
9426 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9427 		    "sata_create_target_node: no sdinfo for target %x",
9428 		    target));
9429 		return (NULL);
9430 	}
9431 
9432 	/*
9433 	 * create or get scsi inquiry data, expected by
9434 	 * scsi_hba_nodename_compatible_get()
9435 	 * SATA hard disks get Identify Data translated into Inguiry Data.
9436 	 * ATAPI devices respond directly to Inquiry request.
9437 	 */
9438 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9439 		sata_identdev_to_inquiry(sata_hba_inst, sdinfo,
9440 		    (uint8_t *)&inq);
9441 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9442 		    sata_addr->cport)));
9443 	} else { /* Assume supported ATAPI device */
9444 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9445 		    sata_addr->cport)));
9446 		if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr,
9447 		    &inq) == SATA_FAILURE)
9448 			return (NULL);
9449 		/*
9450 		 * Save supported ATAPI transport version
9451 		 */
9452 		sdinfo->satadrv_atapi_trans_ver =
9453 		    SATA_ATAPI_TRANS_VERSION(&inq);
9454 	}
9455 
9456 	/* determine the node name and compatible */
9457 	scsi_hba_nodename_compatible_get(&inq, NULL,
9458 	    inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
9459 
9460 #ifdef SATA_DEBUG
9461 	if (sata_debug_flags & SATA_DBG_NODES) {
9462 		if (nname == NULL) {
9463 			cmn_err(CE_NOTE, "sata_create_target_node: "
9464 			    "cannot determine nodename for target %d\n",
9465 			    target);
9466 		} else {
9467 			cmn_err(CE_WARN, "sata_create_target_node: "
9468 			    "target %d nodename: %s\n", target, nname);
9469 		}
9470 		if (compatible == NULL) {
9471 			cmn_err(CE_WARN,
9472 			    "sata_create_target_node: no compatible name\n");
9473 		} else {
9474 			for (i = 0; i < ncompatible; i++) {
9475 				cmn_err(CE_WARN, "sata_create_target_node: "
9476 				    "compatible name: %s\n", compatible[i]);
9477 			}
9478 		}
9479 	}
9480 #endif
9481 
9482 	/* if nodename can't be determined, log error and exit */
9483 	if (nname == NULL) {
9484 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9485 		    "sata_create_target_node: cannot determine nodename "
9486 		    "for target %d\n", target));
9487 		scsi_hba_nodename_compatible_free(nname, compatible);
9488 		return (NULL);
9489 	}
9490 	/*
9491 	 * Create scsi target node
9492 	 */
9493 	ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip);
9494 	rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
9495 	    "device-type", "scsi");
9496 
9497 	if (rval != DDI_PROP_SUCCESS) {
9498 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
9499 		    "updating device_type prop failed %d", rval));
9500 		goto fail;
9501 	}
9502 
9503 	/*
9504 	 * Create target node properties: target & lun
9505 	 */
9506 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target);
9507 	if (rval != DDI_PROP_SUCCESS) {
9508 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
9509 		    "updating target prop failed %d", rval));
9510 		goto fail;
9511 	}
9512 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0);
9513 	if (rval != DDI_PROP_SUCCESS) {
9514 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
9515 		    "updating target prop failed %d", rval));
9516 		goto fail;
9517 	}
9518 
9519 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
9520 		/*
9521 		 * Add "variant" property
9522 		 */
9523 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
9524 		    "variant", "atapi");
9525 		if (rval != DDI_PROP_SUCCESS) {
9526 			SATA_LOG_D((sata_hba_inst, CE_WARN,
9527 			    "sata_create_target_node: variant atapi "
9528 			    "property could not be created: %d", rval));
9529 			goto fail;
9530 		}
9531 	}
9532 	/* decorate the node with compatible */
9533 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible",
9534 	    compatible, ncompatible) != DDI_PROP_SUCCESS) {
9535 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9536 		    "sata_create_target_node: FAIL compatible props cdip 0x%p",
9537 		    (void *)cdip));
9538 		goto fail;
9539 	}
9540 
9541 
9542 	/*
9543 	 * Now, try to attach the driver. If probing of the device fails,
9544 	 * the target node may be removed
9545 	 */
9546 	rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH);
9547 
9548 	scsi_hba_nodename_compatible_free(nname, compatible);
9549 
9550 	if (rval == NDI_SUCCESS)
9551 		return (cdip);
9552 
9553 	/* target node was removed - are we sure? */
9554 	return (NULL);
9555 
9556 fail:
9557 	scsi_hba_nodename_compatible_free(nname, compatible);
9558 	ddi_prop_remove_all(cdip);
9559 	rval = ndi_devi_free(cdip);
9560 	if (rval != NDI_SUCCESS) {
9561 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
9562 		    "node removal failed %d", rval));
9563 	}
9564 	sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: "
9565 	    "cannot create target node for SATA device at port %d",
9566 	    sata_addr->cport);
9567 	return (NULL);
9568 }
9569 
9570 
9571 
9572 /*
9573  * Re-probe sata port, check for a device and attach info
9574  * structures when necessary. Identify Device data is fetched, if possible.
9575  * Assumption: sata address is already validated.
9576  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
9577  * the presence of a device and its type.
9578  *
9579  * flag arg specifies that the function should try multiple times to identify
9580  * device type and to initialize it, or it should return immediately on failure.
9581  * SATA_DEV_IDENTIFY_RETRY - retry
9582  * SATA_DEV_IDENTIFY_NORETRY - no retry
9583  *
9584  * SATA_FAILURE is returned if one of the operations failed.
9585  *
9586  * This function cannot be called in interrupt context - it may sleep.
9587  */
9588 static int
9589 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
9590     int flag)
9591 {
9592 	sata_cport_info_t *cportinfo;
9593 	sata_drive_info_t *sdinfo;
9594 	boolean_t init_device = B_FALSE;
9595 	int prev_device_type = SATA_DTYPE_NONE;
9596 	int prev_device_settings = 0;
9597 	clock_t start_time;
9598 	int retry = B_FALSE;
9599 	int rval;
9600 
9601 	/* We only care about host sata cport for now */
9602 	cportinfo = SATA_CPORT_INFO(sata_hba_inst,
9603 	    sata_device->satadev_addr.cport);
9604 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
9605 	if (sdinfo != NULL) {
9606 		/*
9607 		 * We are re-probing port with a previously attached device.
9608 		 * Save previous device type and settings
9609 		 */
9610 		prev_device_type = cportinfo->cport_dev_type;
9611 		prev_device_settings = sdinfo->satadrv_settings;
9612 	}
9613 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
9614 		start_time = ddi_get_lbolt();
9615 		retry = B_TRUE;
9616 	}
9617 retry_probe:
9618 
9619 	/* probe port */
9620 	mutex_enter(&cportinfo->cport_mutex);
9621 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
9622 	cportinfo->cport_state |= SATA_STATE_PROBING;
9623 	mutex_exit(&cportinfo->cport_mutex);
9624 
9625 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
9626 	    (SATA_DIP(sata_hba_inst), sata_device);
9627 
9628 	mutex_enter(&cportinfo->cport_mutex);
9629 	if (rval != SATA_SUCCESS) {
9630 		cportinfo->cport_state = SATA_PSTATE_FAILED;
9631 		mutex_exit(&cportinfo->cport_mutex);
9632 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: "
9633 		    "SATA port %d probing failed",
9634 		    cportinfo->cport_addr.cport));
9635 		return (SATA_FAILURE);
9636 	}
9637 
9638 	/*
9639 	 * update sata port state and set device type
9640 	 */
9641 	sata_update_port_info(sata_hba_inst, sata_device);
9642 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
9643 
9644 	/*
9645 	 * Sanity check - Port is active? Is the link active?
9646 	 * Is there any device attached?
9647 	 */
9648 	if ((cportinfo->cport_state &
9649 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
9650 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
9651 	    SATA_PORT_DEVLINK_UP) {
9652 		/*
9653 		 * Port in non-usable state or no link active/no device.
9654 		 * Free info structure if necessary (direct attached drive
9655 		 * only, for now!
9656 		 */
9657 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
9658 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
9659 		/* Add here differentiation for device attached or not */
9660 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
9661 		mutex_exit(&cportinfo->cport_mutex);
9662 		if (sdinfo != NULL)
9663 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
9664 		return (SATA_SUCCESS);
9665 	}
9666 
9667 	cportinfo->cport_state |= SATA_STATE_READY;
9668 	cportinfo->cport_dev_type = sata_device->satadev_type;
9669 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
9670 
9671 	/*
9672 	 * If we are re-probing the port, there may be
9673 	 * sata_drive_info structure attached
9674 	 * (or sata_pm_info, if PMult is supported).
9675 	 */
9676 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
9677 		/*
9678 		 * There is no device, so remove device info structure,
9679 		 * if necessary. Direct attached drive only!
9680 		 */
9681 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
9682 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
9683 		if (sdinfo != NULL) {
9684 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
9685 			sata_log(sata_hba_inst, CE_WARN,
9686 			    "SATA device detached "
9687 			    "from port %d", cportinfo->cport_addr.cport);
9688 		}
9689 		mutex_exit(&cportinfo->cport_mutex);
9690 		return (SATA_SUCCESS);
9691 	}
9692 
9693 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
9694 		if (sdinfo == NULL) {
9695 			/*
9696 			 * There is some device attached, but there is
9697 			 * no sata_drive_info structure - allocate one
9698 			 */
9699 			mutex_exit(&cportinfo->cport_mutex);
9700 			sdinfo = kmem_zalloc(
9701 			    sizeof (sata_drive_info_t), KM_SLEEP);
9702 			mutex_enter(&cportinfo->cport_mutex);
9703 			/*
9704 			 * Recheck, that the port state did not change when we
9705 			 * released mutex.
9706 			 */
9707 			if (cportinfo->cport_state & SATA_STATE_READY) {
9708 				SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo;
9709 				sdinfo->satadrv_addr = cportinfo->cport_addr;
9710 				sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT;
9711 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9712 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
9713 			} else {
9714 				/*
9715 				 * Port is not in ready state, we
9716 				 * cannot attach a device.
9717 				 */
9718 				mutex_exit(&cportinfo->cport_mutex);
9719 				kmem_free(sdinfo, sizeof (sata_drive_info_t));
9720 				return (SATA_SUCCESS);
9721 			}
9722 			/*
9723 			 * Since we are adding device, presumably new one,
9724 			 * indicate that it  should be initalized,
9725 			 * as well as some internal framework states).
9726 			 */
9727 			init_device = B_TRUE;
9728 		}
9729 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
9730 		sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
9731 	} else {
9732 		/*
9733 		 * The device is a port multiplier - not handled now.
9734 		 */
9735 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
9736 		mutex_exit(&cportinfo->cport_mutex);
9737 		return (SATA_SUCCESS);
9738 	}
9739 	mutex_exit(&cportinfo->cport_mutex);
9740 	/*
9741 	 * Figure out what kind of device we are really
9742 	 * dealing with.
9743 	 */
9744 	rval = sata_probe_device(sata_hba_inst, sata_device);
9745 
9746 	if (rval == SATA_SUCCESS) {
9747 		/*
9748 		 * If we are dealing with the same type of a device as before,
9749 		 * restore its settings flags.
9750 		 */
9751 		if (sata_device->satadev_type == prev_device_type)
9752 			sdinfo->satadrv_settings = prev_device_settings;
9753 
9754 		/* Set initial device features, if necessary */
9755 		if (init_device == B_TRUE) {
9756 			rval = sata_initialize_device(sata_hba_inst, sdinfo);
9757 		}
9758 		if (rval == SATA_SUCCESS)
9759 			return (rval);
9760 	}
9761 
9762 	if (retry) {
9763 		clock_t cur_time = ddi_get_lbolt();
9764 		/*
9765 		 * A device was not successfully identified or initialized.
9766 		 * Track retry time for device identification.
9767 		 */
9768 		if ((cur_time - start_time) <
9769 		    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
9770 			/* sleep for a while */
9771 			delay(drv_usectohz(SATA_DEV_IDENTIFY_RETRY_DELAY));
9772 			goto retry_probe;
9773 		}
9774 	}
9775 	return (rval);
9776 }
9777 
9778 /*
9779  * Initialize device
9780  * Specified device is initialized to a default state.
9781  *
9782  * Returns SATA_SUCCESS if all device features are set successfully,
9783  * SATA_FAILURE otherwise
9784  */
9785 static int
9786 sata_initialize_device(sata_hba_inst_t *sata_hba_inst,
9787     sata_drive_info_t *sdinfo)
9788 {
9789 	int rval;
9790 
9791 	sata_save_drive_settings(sdinfo);
9792 
9793 	sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
9794 
9795 	sata_init_write_cache_mode(sdinfo);
9796 
9797 	rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0);
9798 
9799 	/* Determine current data transfer mode */
9800 	if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) {
9801 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
9802 	} else if ((sdinfo->satadrv_id.ai_validinfo &
9803 	    SATA_VALIDINFO_88) != 0 &&
9804 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) {
9805 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
9806 	} else if ((sdinfo->satadrv_id.ai_dworddma &
9807 	    SATA_MDMA_SEL_MASK) != 0) {
9808 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
9809 	} else
9810 		/* DMA supported, not no DMA transfer mode is selected !? */
9811 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
9812 
9813 	return (rval);
9814 }
9815 
9816 
9817 /*
9818  * Initialize write cache mode.
9819  *
9820  * The default write cache setting for SATA HDD is provided by sata_write_cache
9821  * static variable. ATAPI CD/DVDs devices have write cache default is
9822  * determined by sata_atapicdvd_write_cache static variable.
9823  * 1 - enable
9824  * 0 - disable
9825  * any other value - current drive setting
9826  *
9827  * Although there is not reason to disable write cache on CD/DVD devices,
9828  * the default setting control is provided for the maximun flexibility.
9829  *
9830  * In the future, it may be overridden by the
9831  * disk-write-cache-enable property setting, if it is defined.
9832  * Returns SATA_SUCCESS if all device features are set successfully,
9833  * SATA_FAILURE otherwise.
9834  */
9835 static void
9836 sata_init_write_cache_mode(sata_drive_info_t *sdinfo)
9837 {
9838 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9839 		if (sata_write_cache == 1)
9840 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
9841 		else if (sata_write_cache == 0)
9842 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
9843 		/*
9844 		 * When sata_write_cache value is not 0 or 1,
9845 		 * a current setting of the drive's write cache is used.
9846 		 */
9847 	} else { /* Assume ATAPI CD/DVD device */
9848 		if (sata_atapicdvd_write_cache == 1)
9849 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
9850 		else if (sata_atapicdvd_write_cache == 0)
9851 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
9852 		/*
9853 		 * When sata_write_cache value is not 0 or 1,
9854 		 * a current setting of the drive's write cache is used.
9855 		 */
9856 	}
9857 }
9858 
9859 
9860 /*
9861  * Validate sata address.
9862  * Specified cport, pmport and qualifier has to match
9863  * passed sata_scsi configuration info.
9864  * The presence of an attached device is not verified.
9865  *
9866  * Returns 0 when address is valid, -1 otherwise.
9867  */
9868 static int
9869 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport,
9870 	int pmport, int qual)
9871 {
9872 	if (qual == SATA_ADDR_DCPORT && pmport != 0)
9873 		goto invalid_address;
9874 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
9875 		goto invalid_address;
9876 	if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) &&
9877 	    ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) ||
9878 	    (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) ||
9879 	    (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport))))
9880 		goto invalid_address;
9881 
9882 	return (0);
9883 
9884 invalid_address:
9885 	return (-1);
9886 
9887 }
9888 
9889 /*
9890  * Validate scsi address
9891  * SCSI target address is translated into SATA cport/pmport and compared
9892  * with a controller port/device configuration. LUN has to be 0.
9893  * Returns 0 if a scsi target refers to an attached device,
9894  * returns 1 if address is valid but device is not attached,
9895  * returns -1 if bad address or device is of an unsupported type.
9896  * Upon return sata_device argument is set.
9897  */
9898 static int
9899 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst,
9900 	struct scsi_address *ap, sata_device_t *sata_device)
9901 {
9902 	int cport, pmport, qual, rval;
9903 
9904 	rval = -1;	/* Invalid address */
9905 	if (ap->a_lun != 0)
9906 		goto out;
9907 
9908 	qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
9909 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
9910 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
9911 
9912 	if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT)
9913 		goto out;
9914 
9915 	if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) ==
9916 	    0) {
9917 
9918 		sata_cport_info_t *cportinfo;
9919 		sata_pmult_info_t *pmultinfo;
9920 		sata_drive_info_t *sdinfo = NULL;
9921 
9922 		rval = 1;	/* Valid sata address */
9923 
9924 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
9925 		if (qual == SATA_ADDR_DCPORT) {
9926 			if (cportinfo == NULL ||
9927 			    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
9928 				goto out;
9929 
9930 			if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT ||
9931 			    (cportinfo->cport_dev_type &
9932 			    SATA_VALID_DEV_TYPE) == 0) {
9933 				rval = -1;
9934 				goto out;
9935 			}
9936 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
9937 
9938 		} else if (qual == SATA_ADDR_DPMPORT) {
9939 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
9940 			if (pmultinfo == NULL) {
9941 				rval = -1;
9942 				goto out;
9943 			}
9944 			if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) ==
9945 			    NULL ||
9946 			    SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
9947 			    pmport) == SATA_DTYPE_NONE)
9948 				goto out;
9949 
9950 			sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport,
9951 			    pmport);
9952 		} else {
9953 			rval = -1;
9954 			goto out;
9955 		}
9956 		if ((sdinfo == NULL) ||
9957 		    (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0)
9958 			goto out;
9959 
9960 		sata_device->satadev_type = sdinfo->satadrv_type;
9961 		sata_device->satadev_addr.qual = qual;
9962 		sata_device->satadev_addr.cport = cport;
9963 		sata_device->satadev_addr.pmport = pmport;
9964 		sata_device->satadev_rev = SATA_DEVICE_REV_1;
9965 		return (0);
9966 	}
9967 out:
9968 	if (rval == 1) {
9969 		SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
9970 		    "sata_validate_scsi_address: no valid target %x lun %x",
9971 		    ap->a_target, ap->a_lun);
9972 	}
9973 	return (rval);
9974 }
9975 
9976 /*
9977  * Find dip corresponding to passed device number
9978  *
9979  * Returns NULL if invalid device number is passed or device cannot be found,
9980  * Returns dip is device is found.
9981  */
9982 static dev_info_t *
9983 sata_devt_to_devinfo(dev_t dev)
9984 {
9985 	dev_info_t *dip;
9986 #ifndef __lock_lint
9987 	struct devnames *dnp;
9988 	major_t major = getmajor(dev);
9989 	int instance = SATA_MINOR2INSTANCE(getminor(dev));
9990 
9991 	if (major >= devcnt)
9992 		return (NULL);
9993 
9994 	dnp = &devnamesp[major];
9995 	LOCK_DEV_OPS(&(dnp->dn_lock));
9996 	dip = dnp->dn_head;
9997 	while (dip && (ddi_get_instance(dip) != instance)) {
9998 		dip = ddi_get_next(dip);
9999 	}
10000 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
10001 #endif
10002 
10003 	return (dip);
10004 }
10005 
10006 
10007 /*
10008  * Probe device.
10009  * This function issues Identify Device command and initializes local
10010  * sata_drive_info structure if the device can be identified.
10011  * The device type is determined by examining Identify Device
10012  * command response.
10013  * If the sata_hba_inst has linked drive info structure for this
10014  * device address, the Identify Device data is stored into sata_drive_info
10015  * structure linked to the port info structure.
10016  *
10017  * sata_device has to refer to the valid sata port(s) for HBA described
10018  * by sata_hba_inst structure.
10019  *
10020  * Returns:
10021  *	SATA_SUCCESS if device type was successfully probed and port-linked
10022  *		drive info structure was updated;
10023  * 	SATA_FAILURE if there is no device, or device was not probed
10024  *		successully;
10025  *	SATA_RETRY if device probe can be retried later.
10026  * If a device cannot be identified, sata_device's dev_state and dev_type
10027  * fields are set to unknown.
10028  * There are no retries in this function. Any retries should be managed by
10029  * the caller.
10030  */
10031 
10032 
10033 static int
10034 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
10035 {
10036 	sata_drive_info_t *sdinfo;
10037 	sata_drive_info_t new_sdinfo;	/* local drive info struct */
10038 	int rval;
10039 
10040 	ASSERT((SATA_CPORT_STATE(sata_hba_inst,
10041 	    sata_device->satadev_addr.cport) &
10042 	    (SATA_STATE_PROBED | SATA_STATE_READY)) != 0);
10043 
10044 	sata_device->satadev_type = SATA_DTYPE_NONE;
10045 
10046 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
10047 	    sata_device->satadev_addr.cport)));
10048 
10049 	/* Get pointer to port-linked sata device info structure */
10050 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
10051 	if (sdinfo != NULL) {
10052 		sdinfo->satadrv_state &=
10053 		    ~(SATA_STATE_PROBED | SATA_STATE_READY);
10054 		sdinfo->satadrv_state |= SATA_STATE_PROBING;
10055 	} else {
10056 		/* No device to probe */
10057 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10058 		    sata_device->satadev_addr.cport)));
10059 		sata_device->satadev_type = SATA_DTYPE_NONE;
10060 		sata_device->satadev_state = SATA_STATE_UNKNOWN;
10061 		return (SATA_FAILURE);
10062 	}
10063 	/*
10064 	 * Need to issue both types of identify device command and
10065 	 * determine device type by examining retreived data/status.
10066 	 * First, ATA Identify Device.
10067 	 */
10068 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
10069 	new_sdinfo.satadrv_addr = sata_device->satadev_addr;
10070 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10071 	    sata_device->satadev_addr.cport)));
10072 	new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
10073 	rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
10074 	if (rval == SATA_RETRY) {
10075 		/* We may try to check for ATAPI device */
10076 		if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) {
10077 			/*
10078 			 * HBA supports ATAPI - try to issue Identify Packet
10079 			 * Device command.
10080 			 */
10081 			new_sdinfo.satadrv_type = SATA_DTYPE_ATAPICD;
10082 			rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
10083 		}
10084 	}
10085 	if (rval == SATA_SUCCESS) {
10086 		/*
10087 		 * Got something responding positively to ATA Identify Device
10088 		 * or to Identify Packet Device cmd.
10089 		 * Save last used device type.
10090 		 */
10091 		sata_device->satadev_type = new_sdinfo.satadrv_type;
10092 
10093 		/* save device info, if possible */
10094 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
10095 		    sata_device->satadev_addr.cport)));
10096 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
10097 		if (sdinfo == NULL) {
10098 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10099 			    sata_device->satadev_addr.cport)));
10100 			return (SATA_FAILURE);
10101 		}
10102 		/*
10103 		 * Copy drive info into the port-linked drive info structure.
10104 		 */
10105 		*sdinfo = new_sdinfo;
10106 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
10107 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
10108 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
10109 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
10110 			    sata_device->satadev_addr.cport) =
10111 			    sdinfo->satadrv_type;
10112 		else /* SATA_ADDR_DPMPORT */
10113 			SATA_PMPORT_DEV_TYPE(sata_hba_inst,
10114 			    sata_device->satadev_addr.cport,
10115 			    sata_device->satadev_addr.pmport) =
10116 			    sdinfo->satadrv_type;
10117 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10118 		    sata_device->satadev_addr.cport)));
10119 		return (SATA_SUCCESS);
10120 	}
10121 
10122 	/*
10123 	 * It may be SATA_RETRY or SATA_FAILURE return.
10124 	 * Looks like we cannot determine the device type at this time.
10125 	 */
10126 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
10127 	    sata_device->satadev_addr.cport)));
10128 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
10129 	if (sdinfo != NULL) {
10130 		sata_device->satadev_type = SATA_DTYPE_UNKNOWN;
10131 		sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
10132 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
10133 		sdinfo->satadrv_state = SATA_STATE_PROBED;
10134 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
10135 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
10136 			    sata_device->satadev_addr.cport) =
10137 			    SATA_DTYPE_UNKNOWN;
10138 		else {
10139 			/* SATA_ADDR_DPMPORT */
10140 			if ((SATA_PMULT_INFO(sata_hba_inst,
10141 			    sata_device->satadev_addr.cport) != NULL) &&
10142 			    (SATA_PMPORT_INFO(sata_hba_inst,
10143 			    sata_device->satadev_addr.cport,
10144 			    sata_device->satadev_addr.pmport) != NULL))
10145 				SATA_PMPORT_DEV_TYPE(sata_hba_inst,
10146 				    sata_device->satadev_addr.cport,
10147 				    sata_device->satadev_addr.pmport) =
10148 				    SATA_DTYPE_UNKNOWN;
10149 		}
10150 	}
10151 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
10152 	    sata_device->satadev_addr.cport)));
10153 	return (rval);
10154 }
10155 
10156 
10157 /*
10158  * Get pointer to sata_drive_info structure.
10159  *
10160  * The sata_device has to contain address (cport, pmport and qualifier) for
10161  * specified sata_scsi structure.
10162  *
10163  * Returns NULL if device address is not valid for this HBA configuration.
10164  * Otherwise, returns a pointer to sata_drive_info structure.
10165  *
10166  * This function should be called with a port mutex held.
10167  */
10168 static sata_drive_info_t *
10169 sata_get_device_info(sata_hba_inst_t *sata_hba_inst,
10170     sata_device_t *sata_device)
10171 {
10172 	uint8_t cport = sata_device->satadev_addr.cport;
10173 	uint8_t pmport = sata_device->satadev_addr.pmport;
10174 	uint8_t qual = sata_device->satadev_addr.qual;
10175 
10176 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
10177 		return (NULL);
10178 
10179 	if (!(SATA_CPORT_STATE(sata_hba_inst, cport) &
10180 	    (SATA_STATE_PROBED | SATA_STATE_READY)))
10181 		/* Port not probed yet */
10182 		return (NULL);
10183 
10184 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE)
10185 		return (NULL);
10186 
10187 	if (qual == SATA_ADDR_DCPORT) {
10188 		/* Request for a device on a controller port */
10189 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
10190 		    SATA_DTYPE_PMULT)
10191 			/* Port multiplier attached */
10192 			return (NULL);
10193 		return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport));
10194 	}
10195 	if (qual == SATA_ADDR_DPMPORT) {
10196 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
10197 		    SATA_DTYPE_PMULT)
10198 			return (NULL);
10199 
10200 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport))
10201 			return (NULL);
10202 
10203 		return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport));
10204 	}
10205 
10206 	/* we should not get here */
10207 	return (NULL);
10208 }
10209 
10210 
10211 /*
10212  * sata_identify_device.
10213  * Send Identify Device command to SATA HBA driver.
10214  * If command executes successfully, update sata_drive_info structure pointed
10215  * to by sdinfo argument, including Identify Device data.
10216  * If command fails, invalidate data in sata_drive_info.
10217  *
10218  * Cannot be called from interrupt level.
10219  *
10220  * Returns:
10221  * SATA_SUCCESS if the device was identified as a supported device,
10222  * SATA_RETRY if the device was not identified but could be retried,
10223  * SATA_FAILURE if the device was not identified and identify attempt
10224  *	should not be retried.
10225  */
10226 static int
10227 sata_identify_device(sata_hba_inst_t *sata_hba_inst,
10228     sata_drive_info_t *sdinfo)
10229 {
10230 	uint16_t cfg_word;
10231 	int rval;
10232 
10233 	/* fetch device identify data */
10234 	if ((rval = sata_fetch_device_identify_data(sata_hba_inst,
10235 	    sdinfo)) != 0)
10236 		goto fail_unknown;
10237 
10238 	cfg_word = sdinfo->satadrv_id.ai_config;
10239 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK &&
10240 	    (cfg_word & SATA_ATA_TYPE_MASK) != SATA_ATA_TYPE) {
10241 		/* Change device type to reflect Identify Device data */
10242 		if (((cfg_word & SATA_ATAPI_TYPE_MASK) ==
10243 		    SATA_ATAPI_TYPE) &&
10244 		    ((cfg_word & SATA_ATAPI_ID_DEV_TYPE) ==
10245 		    SATA_ATAPI_CDROM_DEV)) {
10246 			sdinfo->satadrv_type = SATA_DTYPE_ATAPICD;
10247 		} else {
10248 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
10249 		}
10250 	} else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD &&
10251 	    (((cfg_word & SATA_ATAPI_TYPE_MASK) != SATA_ATAPI_TYPE) ||
10252 	    ((cfg_word & SATA_ATAPI_ID_DEV_TYPE) != SATA_ATAPI_CDROM_DEV))) {
10253 		/* Change device type to reflect Identify Device data ! */
10254 		if ((sdinfo->satadrv_id.ai_config & SATA_ATA_TYPE_MASK) ==
10255 		    SATA_ATA_TYPE) {
10256 			sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
10257 		} else {
10258 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
10259 		}
10260 	}
10261 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10262 		if (sdinfo->satadrv_capacity == 0) {
10263 			/* Non-LBA disk. Too bad... */
10264 			sata_log(sata_hba_inst, CE_WARN,
10265 			    "SATA disk device at port %d does not support LBA",
10266 			    sdinfo->satadrv_addr.cport);
10267 			rval = SATA_FAILURE;
10268 			goto fail_unknown;
10269 		}
10270 	}
10271 #if 0
10272 	/* Left for historical reason */
10273 	/*
10274 	 * Some initial version of SATA spec indicated that at least
10275 	 * UDMA mode 4 has to be supported. It is not metioned in
10276 	 * SerialATA 2.6, so this restriction is removed.
10277 	 */
10278 	/* Check for Ultra DMA modes 6 through 0 being supported */
10279 	for (i = 6; i >= 0; --i) {
10280 		if (sdinfo->satadrv_id.ai_ultradma & (1 << i))
10281 			break;
10282 	}
10283 
10284 	/*
10285 	 * At least UDMA 4 mode has to be supported. If mode 4 or
10286 	 * higher are not supported by the device, fail this
10287 	 * device.
10288 	 */
10289 	if (i < 4) {
10290 		/* No required Ultra DMA mode supported */
10291 		sata_log(sata_hba_inst, CE_WARN,
10292 		    "SATA disk device at port %d does not support UDMA "
10293 		    "mode 4 or higher", sdinfo->satadrv_addr.cport);
10294 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10295 		    "mode 4 or higher required, %d supported", i));
10296 		rval = SATA_FAILURE;
10297 		goto fail_unknown;
10298 	}
10299 #endif
10300 
10301 	return (SATA_SUCCESS);
10302 
10303 fail_unknown:
10304 	/* Invalidate sata_drive_info ? */
10305 	sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
10306 	sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
10307 	return (rval);
10308 }
10309 
10310 /*
10311  * Log/display device information
10312  */
10313 static void
10314 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst,
10315     sata_drive_info_t *sdinfo)
10316 {
10317 	int valid_version;
10318 	char msg_buf[MAXPATHLEN];
10319 	int i;
10320 
10321 	/* Show HBA path */
10322 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf);
10323 
10324 	cmn_err(CE_CONT, "?%s :\n", msg_buf);
10325 
10326 	if (sdinfo->satadrv_type == SATA_DTYPE_UNKNOWN) {
10327 		(void) sprintf(msg_buf,
10328 		    "Unsupported SATA device type (cfg 0x%x) at ",
10329 		    sdinfo->satadrv_id.ai_config);
10330 	} else {
10331 		(void) sprintf(msg_buf, "SATA %s device at",
10332 		    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
10333 		    "disk":"CD/DVD (ATAPI)");
10334 	}
10335 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT)
10336 		cmn_err(CE_CONT, "?\t%s port %d\n",
10337 		    msg_buf, sdinfo->satadrv_addr.cport);
10338 	else
10339 		cmn_err(CE_CONT, "?\t%s port %d pmport %d\n",
10340 		    msg_buf, sdinfo->satadrv_addr.cport,
10341 		    sdinfo->satadrv_addr.pmport);
10342 
10343 	bcopy(&sdinfo->satadrv_id.ai_model, msg_buf,
10344 	    sizeof (sdinfo->satadrv_id.ai_model));
10345 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model));
10346 	msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
10347 	cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf);
10348 
10349 	bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf,
10350 	    sizeof (sdinfo->satadrv_id.ai_fw));
10351 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw));
10352 	msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
10353 	cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf);
10354 
10355 	bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf,
10356 	    sizeof (sdinfo->satadrv_id.ai_drvser));
10357 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser));
10358 	msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
10359 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10360 		cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
10361 	} else {
10362 		/* Assuming ATAPI CD/DVD */
10363 		/*
10364 		 * SOme drives do not implement serial number and may
10365 		 * violate the spec by provinding spaces rather than zeros
10366 		 * in serial number field. Scan the buffer to detect it.
10367 		 */
10368 		for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) {
10369 			if (msg_buf[i] != '\0' && msg_buf[i] != ' ')
10370 				break;
10371 		}
10372 		if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) {
10373 			cmn_err(CE_CONT, "?\tserial number - none\n");
10374 		} else {
10375 			cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
10376 		}
10377 	}
10378 
10379 #ifdef SATA_DEBUG
10380 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
10381 	    sdinfo->satadrv_id.ai_majorversion != 0xffff) {
10382 		int i;
10383 		for (i = 14; i >= 2; i--) {
10384 			if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) {
10385 				valid_version = i;
10386 				break;
10387 			}
10388 		}
10389 		cmn_err(CE_CONT,
10390 		    "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
10391 		    valid_version,
10392 		    sdinfo->satadrv_id.ai_majorversion,
10393 		    sdinfo->satadrv_id.ai_minorversion);
10394 	}
10395 #endif
10396 	/* Log some info */
10397 	cmn_err(CE_CONT, "?\tsupported features:\n");
10398 	msg_buf[0] = '\0';
10399 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10400 		if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48)
10401 			(void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN);
10402 		else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28)
10403 			(void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN);
10404 	}
10405 	if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA)
10406 		(void) strlcat(msg_buf, "DMA", MAXPATHLEN);
10407 	if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ)
10408 		(void) strlcat(msg_buf, ", Native Command Queueing",
10409 		    MAXPATHLEN);
10410 	if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)
10411 		(void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN);
10412 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) &&
10413 	    (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED))
10414 		(void) strlcat(msg_buf, ", SMART", MAXPATHLEN);
10415 	if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) &&
10416 	    (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED))
10417 		(void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN);
10418 	cmn_err(CE_CONT, "?\t %s\n", msg_buf);
10419 	if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2)
10420 		cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n");
10421 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1)
10422 		cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n");
10423 	if (sdinfo->satadrv_features_support &
10424 	    (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) {
10425 		msg_buf[0] = '\0';
10426 		(void) snprintf(msg_buf, MAXPATHLEN,
10427 		    "Supported queue depth %d",
10428 		    sdinfo->satadrv_queue_depth);
10429 		if (!(sata_func_enable &
10430 		    (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ)))
10431 			(void) strlcat(msg_buf,
10432 			    " - queueing disabled globally", MAXPATHLEN);
10433 		else if (sdinfo->satadrv_queue_depth >
10434 		    sdinfo->satadrv_max_queue_depth) {
10435 			(void) snprintf(&msg_buf[strlen(msg_buf)],
10436 			    MAXPATHLEN - strlen(msg_buf), ", limited to %d",
10437 			    (int)sdinfo->satadrv_max_queue_depth);
10438 		}
10439 		cmn_err(CE_CONT, "?\t%s\n", msg_buf);
10440 	}
10441 
10442 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10443 #ifdef __i386
10444 		(void) sprintf(msg_buf, "\tcapacity = %llu sectors\n",
10445 		    sdinfo->satadrv_capacity);
10446 #else
10447 		(void) sprintf(msg_buf, "\tcapacity = %lu sectors\n",
10448 		    sdinfo->satadrv_capacity);
10449 #endif
10450 		cmn_err(CE_CONT, "?%s", msg_buf);
10451 	}
10452 }
10453 
10454 
10455 /*
10456  * sata_save_drive_settings extracts current setting of the device and stores
10457  * it for future reference, in case the device setup would need to be restored
10458  * after the device reset.
10459  *
10460  * For all devices read ahead and write cache settings are saved, if the
10461  * device supports these features at all.
10462  * For ATAPI devices the Removable Media Status Notification setting is saved.
10463  */
10464 static void
10465 sata_save_drive_settings(sata_drive_info_t *sdinfo)
10466 {
10467 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) ||
10468 	    (sdinfo->satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) {
10469 
10470 		/* Current setting of Read Ahead (and Read Cache) */
10471 		if (sdinfo->satadrv_id.ai_features85 & SATA_LOOK_AHEAD)
10472 			sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
10473 		else
10474 			sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD;
10475 
10476 		/* Current setting of Write Cache */
10477 		if (sdinfo->satadrv_id.ai_features85 & SATA_WRITE_CACHE)
10478 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
10479 		else
10480 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
10481 	}
10482 
10483 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
10484 		if (sdinfo->satadrv_id.ai_cmdset83 & SATA_RM_STATUS_NOTIFIC)
10485 			sdinfo->satadrv_settings |= SATA_DEV_RMSN;
10486 		else
10487 			sdinfo->satadrv_settings &= ~SATA_DEV_RMSN;
10488 	}
10489 }
10490 
10491 
10492 /*
10493  * sata_check_capacity function determines a disk capacity
10494  * and addressing mode (LBA28/LBA48) by examining a disk identify device data.
10495  *
10496  * NOTE: CHS mode is not supported! If a device does not support LBA,
10497  * this function is not called.
10498  *
10499  * Returns device capacity in number of blocks, i.e. largest addressable LBA+1
10500  */
10501 static uint64_t
10502 sata_check_capacity(sata_drive_info_t *sdinfo)
10503 {
10504 	uint64_t capacity = 0;
10505 	int i;
10506 
10507 	if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK ||
10508 	    !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT)
10509 		/* Capacity valid only for LBA-addressable disk devices */
10510 		return (0);
10511 
10512 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) &&
10513 	    (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) &&
10514 	    (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) {
10515 		/* LBA48 mode supported and enabled */
10516 		sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 |
10517 		    SATA_DEV_F_LBA28;
10518 		for (i = 3;  i >= 0;  --i) {
10519 			capacity <<= 16;
10520 			capacity += sdinfo->satadrv_id.ai_addrsecxt[i];
10521 		}
10522 	} else {
10523 		capacity = sdinfo->satadrv_id.ai_addrsec[1];
10524 		capacity <<= 16;
10525 		capacity += sdinfo->satadrv_id.ai_addrsec[0];
10526 		if (capacity >= 0x1000000)
10527 			/* LBA28 mode */
10528 			sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28;
10529 	}
10530 	return (capacity);
10531 }
10532 
10533 
10534 /*
10535  * Allocate consistent buffer for DMA transfer
10536  *
10537  * Cannot be called from interrupt level or with mutex held - it may sleep.
10538  *
10539  * Returns pointer to allocated buffer structure, or NULL if allocation failed.
10540  */
10541 static struct buf *
10542 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len)
10543 {
10544 	struct scsi_address ap;
10545 	struct buf *bp;
10546 	ddi_dma_attr_t	cur_dma_attr;
10547 
10548 	ASSERT(spx->txlt_sata_pkt != NULL);
10549 	ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran;
10550 	ap.a_target = SATA_TO_SCSI_TARGET(
10551 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport,
10552 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport,
10553 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual);
10554 	ap.a_lun = 0;
10555 
10556 	bp = scsi_alloc_consistent_buf(&ap, NULL, len,
10557 	    B_READ, SLEEP_FUNC, NULL);
10558 
10559 	if (bp != NULL) {
10560 		/* Allocate DMA resources for this buffer */
10561 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
10562 		/*
10563 		 * We use a local version of the dma_attr, to account
10564 		 * for a device addressing limitations.
10565 		 * sata_adjust_dma_attr() will handle sdinfo == NULL which
10566 		 * will cause dma attributes to be adjusted to a lowest
10567 		 * acceptable level.
10568 		 */
10569 		sata_adjust_dma_attr(NULL,
10570 		    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
10571 
10572 		if (sata_dma_buf_setup(spx, PKT_CONSISTENT,
10573 		    SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) {
10574 			scsi_free_consistent_buf(bp);
10575 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
10576 			bp = NULL;
10577 		}
10578 	}
10579 	return (bp);
10580 }
10581 
10582 /*
10583  * Release local buffer (consistent buffer for DMA transfer) allocated
10584  * via sata_alloc_local_buffer().
10585  */
10586 static void
10587 sata_free_local_buffer(sata_pkt_txlate_t *spx)
10588 {
10589 	ASSERT(spx->txlt_sata_pkt != NULL);
10590 	ASSERT(spx->txlt_dma_cookie_list != NULL);
10591 	ASSERT(spx->txlt_dma_cookie_list_len != 0);
10592 	ASSERT(spx->txlt_buf_dma_handle != NULL);
10593 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL);
10594 
10595 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0;
10596 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL;
10597 
10598 	/* Free DMA resources */
10599 	(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
10600 	ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
10601 	spx->txlt_buf_dma_handle = 0;
10602 
10603 	if (spx->txlt_dma_cookie_list != &spx->txlt_dma_cookie) {
10604 		kmem_free(spx->txlt_dma_cookie_list,
10605 		    spx->txlt_dma_cookie_list_len * sizeof (ddi_dma_cookie_t));
10606 		spx->txlt_dma_cookie_list = NULL;
10607 		spx->txlt_dma_cookie_list_len = 0;
10608 	}
10609 	/* Free buffer */
10610 	scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp);
10611 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
10612 }
10613 
10614 
10615 
10616 
10617 /*
10618  * Allocate sata_pkt
10619  * Pkt structure version and embedded strcutures version are initialized.
10620  * sata_pkt and sata_pkt_txlate structures are cross-linked.
10621  *
10622  * Since this may be called in interrupt context by sata_scsi_init_pkt,
10623  * callback argument determines if it can sleep or not.
10624  * Hence, it should not be called from interrupt context.
10625  *
10626  * If successful, non-NULL pointer to a sata pkt is returned.
10627  * Upon failure, NULL pointer is returned.
10628  */
10629 static sata_pkt_t *
10630 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t))
10631 {
10632 	sata_pkt_t *spkt;
10633 	int kmsflag;
10634 
10635 	kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
10636 	spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag);
10637 	if (spkt == NULL) {
10638 		SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
10639 		    "sata_pkt_alloc: failed"));
10640 		return (NULL);
10641 	}
10642 	spkt->satapkt_rev = SATA_PKT_REV;
10643 	spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV;
10644 	spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
10645 	spkt->satapkt_framework_private = spx;
10646 	spx->txlt_sata_pkt = spkt;
10647 	return (spkt);
10648 }
10649 
10650 /*
10651  * Free sata pkt allocated via sata_pkt_alloc()
10652  */
10653 static void
10654 sata_pkt_free(sata_pkt_txlate_t *spx)
10655 {
10656 	ASSERT(spx->txlt_sata_pkt != NULL);
10657 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL);
10658 	kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t));
10659 	spx->txlt_sata_pkt = NULL;
10660 }
10661 
10662 
10663 /*
10664  * Adjust DMA attributes.
10665  * SCSI cmds block count is up to 24 bits, SATA cmd block count vary
10666  * from 8 bits to 16 bits, depending on a command being used.
10667  * Limiting max block count arbitrarily to 256 for all read/write
10668  * commands may affects performance, so check both the device and
10669  * controller capability before adjusting dma attributes.
10670  */
10671 void
10672 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr,
10673     ddi_dma_attr_t *adj_dma_attr)
10674 {
10675 	uint32_t count_max;
10676 
10677 	/* Copy original attributes */
10678 	*adj_dma_attr = *dma_attr;
10679 	/*
10680 	 * Things to consider: device addressing capability,
10681 	 * "excessive" controller DMA capabilities.
10682 	 * If a device is being probed/initialized, there are
10683 	 * no device info - use default limits then.
10684 	 */
10685 	if (sdinfo == NULL) {
10686 		count_max = dma_attr->dma_attr_granular * 0x100;
10687 		if (dma_attr->dma_attr_count_max > count_max)
10688 			adj_dma_attr->dma_attr_count_max = count_max;
10689 		if (dma_attr->dma_attr_maxxfer > count_max)
10690 			adj_dma_attr->dma_attr_maxxfer = count_max;
10691 		return;
10692 	}
10693 
10694 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10695 		if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) {
10696 			/*
10697 			 * 16-bit sector count may be used - we rely on
10698 			 * the assumption that only read and write cmds
10699 			 * will request more than 256 sectors worth of data
10700 			 */
10701 			count_max = adj_dma_attr->dma_attr_granular * 0x10000;
10702 		} else {
10703 			/*
10704 			 * 8-bit sector count will be used - default limits
10705 			 * for dma attributes
10706 			 */
10707 			count_max = adj_dma_attr->dma_attr_granular * 0x100;
10708 		}
10709 		/*
10710 		 * Adjust controler dma attributes, if necessary
10711 		 */
10712 		if (dma_attr->dma_attr_count_max > count_max)
10713 			adj_dma_attr->dma_attr_count_max = count_max;
10714 		if (dma_attr->dma_attr_maxxfer > count_max)
10715 			adj_dma_attr->dma_attr_maxxfer = count_max;
10716 	}
10717 }
10718 
10719 
10720 /*
10721  * Allocate DMA resources for the buffer
10722  * This function handles initial DMA resource allocation as well as
10723  * DMA window shift and may be called repeatedly for the same DMA window
10724  * until all DMA cookies in the DMA window are processed.
10725  * To guarantee that there is always a coherent set of cookies to process
10726  * by SATA HBA driver (observing alignment, device granularity, etc.),
10727  * the number of slots for DMA cookies is equal to lesser of  a number of
10728  * cookies in a DMA window and a max number of scatter/gather entries.
10729  *
10730  * Returns DDI_SUCCESS upon successful operation.
10731  * Return failure code of a failing command or DDI_FAILURE when
10732  * internal cleanup failed.
10733  */
10734 static int
10735 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags,
10736     int (*callback)(caddr_t), caddr_t arg,
10737     ddi_dma_attr_t *cur_dma_attr)
10738 {
10739 	int	rval;
10740 	off_t	offset;
10741 	size_t	size;
10742 	int	max_sg_len, req_len, i;
10743 	uint_t	dma_flags;
10744 	struct buf	*bp;
10745 	uint64_t	cur_txfer_len;
10746 
10747 
10748 	ASSERT(spx->txlt_sata_pkt != NULL);
10749 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
10750 	ASSERT(bp != NULL);
10751 
10752 
10753 	if (spx->txlt_buf_dma_handle == NULL) {
10754 		/*
10755 		 * No DMA resources allocated so far - this is a first call
10756 		 * for this sata pkt.
10757 		 */
10758 		rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst),
10759 		    cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle);
10760 
10761 		if (rval != DDI_SUCCESS) {
10762 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
10763 			    "sata_dma_buf_setup: no buf DMA resources %x",
10764 			    rval));
10765 			return (rval);
10766 		}
10767 
10768 		if (bp->b_flags & B_READ)
10769 			dma_flags = DDI_DMA_READ;
10770 		else
10771 			dma_flags = DDI_DMA_WRITE;
10772 
10773 		if (flags & PKT_CONSISTENT)
10774 			dma_flags |= DDI_DMA_CONSISTENT;
10775 
10776 		if (flags & PKT_DMA_PARTIAL)
10777 			dma_flags |= DDI_DMA_PARTIAL;
10778 
10779 		/*
10780 		 * Check buffer alignment and size against dma attributes
10781 		 * Consider dma_attr_align only. There may be requests
10782 		 * with the size lower than device granularity, but they
10783 		 * will not read/write from/to the device, so no adjustment
10784 		 * is necessary. The dma_attr_minxfer theoretically should
10785 		 * be considered, but no HBA driver is checking it.
10786 		 */
10787 		if (IS_P2ALIGNED(bp->b_un.b_addr,
10788 		    cur_dma_attr->dma_attr_align)) {
10789 			rval = ddi_dma_buf_bind_handle(
10790 			    spx->txlt_buf_dma_handle,
10791 			    bp, dma_flags, callback, arg,
10792 			    &spx->txlt_dma_cookie,
10793 			    &spx->txlt_curwin_num_dma_cookies);
10794 		} else { /* Buffer is not aligned */
10795 
10796 			int	(*ddicallback)(caddr_t);
10797 			size_t	bufsz;
10798 
10799 			/* Check id sleeping is allowed */
10800 			ddicallback = (callback == NULL_FUNC) ?
10801 			    DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
10802 
10803 			SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
10804 			    "mis-aligned buffer: addr=0x%p, cnt=%lu",
10805 			    (void *)bp->b_un.b_addr, bp->b_bcount);
10806 
10807 			if (bp->b_flags & (B_PAGEIO|B_PHYS))
10808 				/*
10809 				 * CPU will need to access data in the buffer
10810 				 * (for copying) so map it.
10811 				 */
10812 				bp_mapin(bp);
10813 
10814 			ASSERT(spx->txlt_tmp_buf == NULL);
10815 
10816 			/* Buffer may be padded by ddi_dma_mem_alloc()! */
10817 			rval = ddi_dma_mem_alloc(
10818 			    spx->txlt_buf_dma_handle,
10819 			    bp->b_bcount,
10820 			    &sata_acc_attr,
10821 			    DDI_DMA_STREAMING,
10822 			    ddicallback, NULL,
10823 			    &spx->txlt_tmp_buf,
10824 			    &bufsz,
10825 			    &spx->txlt_tmp_buf_handle);
10826 
10827 			if (rval != DDI_SUCCESS) {
10828 				/* DMA mapping failed */
10829 				(void) ddi_dma_free_handle(
10830 				    &spx->txlt_buf_dma_handle);
10831 				spx->txlt_buf_dma_handle = NULL;
10832 #ifdef SATA_DEBUG
10833 				mbuffail_count++;
10834 #endif
10835 				SATADBG1(SATA_DBG_DMA_SETUP,
10836 				    spx->txlt_sata_hba_inst,
10837 				    "sata_dma_buf_setup: "
10838 				    "buf dma mem alloc failed %x\n", rval);
10839 				return (rval);
10840 			}
10841 			ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf,
10842 			    cur_dma_attr->dma_attr_align));
10843 
10844 #ifdef SATA_DEBUG
10845 			mbuf_count++;
10846 
10847 			if (bp->b_bcount != bufsz)
10848 				/*
10849 				 * This will require special handling, because
10850 				 * DMA cookies will be based on the temporary
10851 				 * buffer size, not the original buffer
10852 				 * b_bcount, so the residue may have to
10853 				 * be counted differently.
10854 				 */
10855 				SATADBG2(SATA_DBG_DMA_SETUP,
10856 				    spx->txlt_sata_hba_inst,
10857 				    "sata_dma_buf_setup: bp size %x != "
10858 				    "bufsz %x\n", bp->b_bcount, bufsz);
10859 #endif
10860 			if (dma_flags & DDI_DMA_WRITE) {
10861 				/*
10862 				 * Write operation - copy data into
10863 				 * an aligned temporary buffer. Buffer will be
10864 				 * synced for device by ddi_dma_addr_bind_handle
10865 				 */
10866 				bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf,
10867 				    bp->b_bcount);
10868 			}
10869 
10870 			rval = ddi_dma_addr_bind_handle(
10871 			    spx->txlt_buf_dma_handle,
10872 			    NULL,
10873 			    spx->txlt_tmp_buf,
10874 			    bufsz, dma_flags, ddicallback, 0,
10875 			    &spx->txlt_dma_cookie,
10876 			    &spx->txlt_curwin_num_dma_cookies);
10877 		}
10878 
10879 		switch (rval) {
10880 		case DDI_DMA_PARTIAL_MAP:
10881 			SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
10882 			    "sata_dma_buf_setup: DMA Partial Map\n", NULL);
10883 			/*
10884 			 * Partial DMA mapping.
10885 			 * Retrieve number of DMA windows for this request.
10886 			 */
10887 			if (ddi_dma_numwin(spx->txlt_buf_dma_handle,
10888 			    &spx->txlt_num_dma_win) != DDI_SUCCESS) {
10889 				if (spx->txlt_tmp_buf != NULL) {
10890 					ddi_dma_mem_free(
10891 					    &spx->txlt_tmp_buf_handle);
10892 					spx->txlt_tmp_buf = NULL;
10893 				}
10894 				(void) ddi_dma_unbind_handle(
10895 				    spx->txlt_buf_dma_handle);
10896 				(void) ddi_dma_free_handle(
10897 				    &spx->txlt_buf_dma_handle);
10898 				spx->txlt_buf_dma_handle = NULL;
10899 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
10900 				    "sata_dma_buf_setup: numwin failed\n"));
10901 				return (DDI_FAILURE);
10902 			}
10903 			SATADBG2(SATA_DBG_DMA_SETUP,
10904 			    spx->txlt_sata_hba_inst,
10905 			    "sata_dma_buf_setup: windows: %d, cookies: %d\n",
10906 			    spx->txlt_num_dma_win,
10907 			    spx->txlt_curwin_num_dma_cookies);
10908 			spx->txlt_cur_dma_win = 0;
10909 			break;
10910 
10911 		case DDI_DMA_MAPPED:
10912 			/* DMA fully mapped */
10913 			spx->txlt_num_dma_win = 1;
10914 			spx->txlt_cur_dma_win = 0;
10915 			SATADBG1(SATA_DBG_DMA_SETUP,
10916 			    spx->txlt_sata_hba_inst,
10917 			    "sata_dma_buf_setup: windows: 1 "
10918 			    "cookies: %d\n", spx->txlt_curwin_num_dma_cookies);
10919 			break;
10920 
10921 		default:
10922 			/* DMA mapping failed */
10923 			if (spx->txlt_tmp_buf != NULL) {
10924 				ddi_dma_mem_free(
10925 				    &spx->txlt_tmp_buf_handle);
10926 				spx->txlt_tmp_buf = NULL;
10927 			}
10928 			(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
10929 			spx->txlt_buf_dma_handle = NULL;
10930 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
10931 			    "sata_dma_buf_setup: buf dma handle binding "
10932 			    "failed %x\n", rval));
10933 			return (rval);
10934 		}
10935 		spx->txlt_curwin_processed_dma_cookies = 0;
10936 		spx->txlt_dma_cookie_list = NULL;
10937 	} else {
10938 		/*
10939 		 * DMA setup is reused. Check if we need to process more
10940 		 * cookies in current window, or to get next window, if any.
10941 		 */
10942 
10943 		ASSERT(spx->txlt_curwin_processed_dma_cookies <=
10944 		    spx->txlt_curwin_num_dma_cookies);
10945 
10946 		if (spx->txlt_curwin_processed_dma_cookies ==
10947 		    spx->txlt_curwin_num_dma_cookies) {
10948 			/*
10949 			 * All cookies from current DMA window were processed.
10950 			 * Get next DMA window.
10951 			 */
10952 			spx->txlt_cur_dma_win++;
10953 			if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) {
10954 				(void) ddi_dma_getwin(spx->txlt_buf_dma_handle,
10955 				    spx->txlt_cur_dma_win, &offset, &size,
10956 				    &spx->txlt_dma_cookie,
10957 				    &spx->txlt_curwin_num_dma_cookies);
10958 				spx->txlt_curwin_processed_dma_cookies = 0;
10959 			} else {
10960 				/* No more windows! End of request! */
10961 				/* What to do? - panic for now */
10962 				ASSERT(spx->txlt_cur_dma_win >=
10963 				    spx->txlt_num_dma_win);
10964 
10965 				spx->txlt_curwin_num_dma_cookies = 0;
10966 				spx->txlt_curwin_processed_dma_cookies = 0;
10967 				spx->txlt_sata_pkt->
10968 				    satapkt_cmd.satacmd_num_dma_cookies = 0;
10969 				return (DDI_SUCCESS);
10970 			}
10971 		}
10972 	}
10973 	/* There better be at least one DMA cookie outstanding */
10974 	ASSERT((spx->txlt_curwin_num_dma_cookies -
10975 	    spx->txlt_curwin_processed_dma_cookies) > 0);
10976 
10977 	if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) {
10978 		/* The default cookie slot was used in previous run */
10979 		ASSERT(spx->txlt_curwin_processed_dma_cookies == 0);
10980 		spx->txlt_dma_cookie_list = NULL;
10981 		spx->txlt_dma_cookie_list_len = 0;
10982 	}
10983 	if (spx->txlt_curwin_processed_dma_cookies == 0) {
10984 		/*
10985 		 * Processing a new DMA window - set-up dma cookies list.
10986 		 * We may reuse previously allocated cookie array if it is
10987 		 * possible.
10988 		 */
10989 		if (spx->txlt_dma_cookie_list != NULL &&
10990 		    spx->txlt_dma_cookie_list_len <
10991 		    spx->txlt_curwin_num_dma_cookies) {
10992 			/*
10993 			 * New DMA window contains more cookies than
10994 			 * the previous one. We need larger cookie list - free
10995 			 * the old one.
10996 			 */
10997 			(void) kmem_free(spx->txlt_dma_cookie_list,
10998 			    spx->txlt_dma_cookie_list_len *
10999 			    sizeof (ddi_dma_cookie_t));
11000 			spx->txlt_dma_cookie_list = NULL;
11001 			spx->txlt_dma_cookie_list_len = 0;
11002 		}
11003 		if (spx->txlt_dma_cookie_list == NULL) {
11004 			/*
11005 			 * Calculate lesser of number of cookies in this
11006 			 * DMA window and number of s/g entries.
11007 			 */
11008 			max_sg_len = cur_dma_attr->dma_attr_sgllen;
11009 			req_len = MIN(max_sg_len,
11010 			    spx->txlt_curwin_num_dma_cookies);
11011 
11012 			/* Allocate new dma cookie array if necessary */
11013 			if (req_len == 1) {
11014 				/* Only one cookie - no need for a list */
11015 				spx->txlt_dma_cookie_list =
11016 				    &spx->txlt_dma_cookie;
11017 				spx->txlt_dma_cookie_list_len = 1;
11018 			} else {
11019 				/*
11020 				 * More than one cookie - try to allocate space.
11021 				 */
11022 				spx->txlt_dma_cookie_list = kmem_zalloc(
11023 				    sizeof (ddi_dma_cookie_t) * req_len,
11024 				    callback == NULL_FUNC ? KM_NOSLEEP :
11025 				    KM_SLEEP);
11026 				if (spx->txlt_dma_cookie_list == NULL) {
11027 					SATADBG1(SATA_DBG_DMA_SETUP,
11028 					    spx->txlt_sata_hba_inst,
11029 					    "sata_dma_buf_setup: cookie list "
11030 					    "allocation failed\n", NULL);
11031 					/*
11032 					 * We could not allocate space for
11033 					 * neccessary number of dma cookies in
11034 					 * this window, so we fail this request.
11035 					 * Next invocation would try again to
11036 					 * allocate space for cookie list.
11037 					 * Note:Packet residue was not modified.
11038 					 */
11039 					return (DDI_DMA_NORESOURCES);
11040 				} else {
11041 					spx->txlt_dma_cookie_list_len = req_len;
11042 				}
11043 			}
11044 		}
11045 		/*
11046 		 * Fetch DMA cookies into cookie list in sata_pkt_txlate.
11047 		 * First cookie was already fetched.
11048 		 */
11049 		*(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie;
11050 		cur_txfer_len =
11051 		    (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size;
11052 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1;
11053 		spx->txlt_curwin_processed_dma_cookies++;
11054 		for (i = 1; (i < spx->txlt_dma_cookie_list_len) &&
11055 		    (i < spx->txlt_curwin_num_dma_cookies); i++) {
11056 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
11057 			    &spx->txlt_dma_cookie_list[i]);
11058 			cur_txfer_len +=
11059 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
11060 			spx->txlt_curwin_processed_dma_cookies++;
11061 			spx->txlt_sata_pkt->
11062 			    satapkt_cmd.satacmd_num_dma_cookies += 1;
11063 		}
11064 	} else {
11065 		SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
11066 		    "sata_dma_buf_setup: sliding within DMA window, "
11067 		    "cur cookie %d, total cookies %d\n",
11068 		    spx->txlt_curwin_processed_dma_cookies,
11069 		    spx->txlt_curwin_num_dma_cookies);
11070 
11071 		/*
11072 		 * Not all cookies from the current dma window were used because
11073 		 * of s/g limitation.
11074 		 * There is no need to re-size the list - it was set at
11075 		 * optimal size, or only default entry is used (s/g = 1).
11076 		 */
11077 		if (spx->txlt_dma_cookie_list == NULL) {
11078 			spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie;
11079 			spx->txlt_dma_cookie_list_len = 1;
11080 		}
11081 		/*
11082 		 * Since we are processing remaining cookies in a DMA window,
11083 		 * there may be less of them than the number of entries in the
11084 		 * current dma cookie list.
11085 		 */
11086 		req_len = MIN(spx->txlt_dma_cookie_list_len,
11087 		    (spx->txlt_curwin_num_dma_cookies -
11088 		    spx->txlt_curwin_processed_dma_cookies));
11089 
11090 		/* Fetch the next batch of cookies */
11091 		for (i = 0, cur_txfer_len = 0; i < req_len; i++) {
11092 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
11093 			    &spx->txlt_dma_cookie_list[i]);
11094 			cur_txfer_len +=
11095 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
11096 			spx->txlt_sata_pkt->
11097 			    satapkt_cmd.satacmd_num_dma_cookies++;
11098 			spx->txlt_curwin_processed_dma_cookies++;
11099 		}
11100 	}
11101 
11102 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0);
11103 
11104 	/* Point sata_cmd to the cookie list */
11105 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list =
11106 	    &spx->txlt_dma_cookie_list[0];
11107 
11108 	/* Remember number of DMA cookies passed in sata packet */
11109 	spx->txlt_num_dma_cookies =
11110 	    spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies;
11111 
11112 	ASSERT(cur_txfer_len != 0);
11113 	if (cur_txfer_len <= bp->b_bcount)
11114 		spx->txlt_total_residue -= cur_txfer_len;
11115 	else {
11116 		/*
11117 		 * Temporary DMA buffer has been padded by
11118 		 * ddi_dma_mem_alloc()!
11119 		 * This requires special handling, because DMA cookies are
11120 		 * based on the temporary buffer size, not the b_bcount,
11121 		 * and we have extra bytes to transfer - but the packet
11122 		 * residue has to stay correct because we will copy only
11123 		 * the requested number of bytes.
11124 		 */
11125 		spx->txlt_total_residue -= bp->b_bcount;
11126 	}
11127 
11128 	return (DDI_SUCCESS);
11129 }
11130 
11131 
11132 /*
11133  * Fetch Device Identify data.
11134  * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type)
11135  * command to a device and get the device identify data.
11136  * The device_info structure has to be set to device type (for selecting proper
11137  * device identify command).
11138  *
11139  * Returns:
11140  * SATA_SUCCESS if cmd succeeded
11141  * SATA_RETRY if cmd was rejected and could be retried,
11142  * SATA_FAILURE if cmd failed and should not be retried (port error)
11143  *
11144  * Cannot be called in an interrupt context.
11145  */
11146 
11147 static int
11148 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst,
11149     sata_drive_info_t *sdinfo)
11150 {
11151 	struct buf *bp;
11152 	sata_pkt_t *spkt;
11153 	sata_cmd_t *scmd;
11154 	sata_pkt_txlate_t *spx;
11155 	int rval;
11156 
11157 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
11158 	spx->txlt_sata_hba_inst = sata_hba_inst;
11159 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
11160 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
11161 	if (spkt == NULL) {
11162 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
11163 		return (SATA_RETRY); /* may retry later */
11164 	}
11165 	/* address is needed now */
11166 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
11167 
11168 	/*
11169 	 * Allocate buffer for Identify Data return data
11170 	 */
11171 	bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t));
11172 	if (bp == NULL) {
11173 		sata_pkt_free(spx);
11174 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
11175 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11176 		    "sata_fetch_device_identify_data: "
11177 		    "cannot allocate buffer for ID"));
11178 		return (SATA_RETRY); /* may retry later */
11179 	}
11180 
11181 	/* Fill sata_pkt */
11182 	sdinfo->satadrv_state = SATA_STATE_PROBING;
11183 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
11184 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
11185 	/* Synchronous mode, no callback */
11186 	spkt->satapkt_comp = NULL;
11187 	/* Timeout 30s */
11188 	spkt->satapkt_time = sata_default_pkt_time;
11189 
11190 	scmd = &spkt->satapkt_cmd;
11191 	scmd->satacmd_bp = bp;
11192 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
11193 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
11194 
11195 	/* Build Identify Device cmd in the sata_pkt */
11196 	scmd->satacmd_addr_type = 0;		/* N/A */
11197 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
11198 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
11199 	scmd->satacmd_lba_mid_lsb = 0;		/* N/A */
11200 	scmd->satacmd_lba_high_lsb = 0;		/* N/A */
11201 	scmd->satacmd_features_reg = 0;		/* N/A */
11202 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
11203 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
11204 		/* Identify Packet Device cmd */
11205 		scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE;
11206 	} else {
11207 		/* Identify Device cmd - mandatory for all other devices */
11208 		scmd->satacmd_cmd_reg = SATAC_ID_DEVICE;
11209 	}
11210 
11211 	/* Send pkt to SATA HBA driver */
11212 	rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt);
11213 	if (rval == SATA_TRAN_ACCEPTED &&
11214 	    spkt->satapkt_reason == SATA_PKT_COMPLETED) {
11215 		if ((sdinfo->satadrv_id.ai_config & SATA_INCOMPLETE_DATA) ==
11216 		    SATA_INCOMPLETE_DATA) {
11217 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11218 			    "SATA disk device at port %d - "
11219 			    "partial Identify Data",
11220 			    sdinfo->satadrv_addr.cport));
11221 			rval = SATA_RETRY; /* may retry later */
11222 			goto fail;
11223 		}
11224 		/* Update sata_drive_info */
11225 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
11226 		    DDI_DMA_SYNC_FORKERNEL);
11227 		ASSERT(rval == DDI_SUCCESS);
11228 		bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id,
11229 		    sizeof (sata_id_t));
11230 
11231 		sdinfo->satadrv_features_support = 0;
11232 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
11233 			/*
11234 			 * Retrieve capacity (disks only) and addressing mode
11235 			 */
11236 			sdinfo->satadrv_capacity = sata_check_capacity(sdinfo);
11237 		} else {
11238 			/*
11239 			 * For ATAPI devices one would have to issue
11240 			 * Get Capacity cmd for media capacity. Not here.
11241 			 */
11242 			sdinfo->satadrv_capacity = 0;
11243 			/*
11244 			 * Check what cdb length is supported
11245 			 */
11246 			if ((sdinfo->satadrv_id.ai_config &
11247 			    SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B)
11248 				sdinfo->satadrv_atapi_cdb_len = 16;
11249 			else
11250 				sdinfo->satadrv_atapi_cdb_len = 12;
11251 		}
11252 		/* Setup supported features flags */
11253 		if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT)
11254 			sdinfo->satadrv_features_support |= SATA_DEV_F_DMA;
11255 
11256 		/* Check for SATA GEN and NCQ support */
11257 		if (sdinfo->satadrv_id.ai_satacap != 0 &&
11258 		    sdinfo->satadrv_id.ai_satacap != 0xffff) {
11259 			/* SATA compliance */
11260 			if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ)
11261 				sdinfo->satadrv_features_support |=
11262 				    SATA_DEV_F_NCQ;
11263 			if (sdinfo->satadrv_id.ai_satacap &
11264 			    (SATA_1_SPEED | SATA_2_SPEED)) {
11265 				if (sdinfo->satadrv_id.ai_satacap &
11266 				    SATA_2_SPEED)
11267 					sdinfo->satadrv_features_support |=
11268 					    SATA_DEV_F_SATA2;
11269 				if (sdinfo->satadrv_id.ai_satacap &
11270 				    SATA_1_SPEED)
11271 					sdinfo->satadrv_features_support |=
11272 					    SATA_DEV_F_SATA1;
11273 			} else {
11274 				sdinfo->satadrv_features_support |=
11275 				    SATA_DEV_F_SATA1;
11276 			}
11277 		}
11278 		if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) &&
11279 		    (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD))
11280 			sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ;
11281 
11282 		sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth;
11283 		if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) ||
11284 		    (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) {
11285 			++sdinfo->satadrv_queue_depth;
11286 			/* Adjust according to controller capabilities */
11287 			sdinfo->satadrv_max_queue_depth = MIN(
11288 			    sdinfo->satadrv_queue_depth,
11289 			    SATA_QDEPTH(sata_hba_inst));
11290 			/* Adjust according to global queue depth limit */
11291 			sdinfo->satadrv_max_queue_depth = MIN(
11292 			    sdinfo->satadrv_max_queue_depth,
11293 			    sata_current_max_qdepth);
11294 			if (sdinfo->satadrv_max_queue_depth == 0)
11295 				sdinfo->satadrv_max_queue_depth = 1;
11296 		} else
11297 			sdinfo->satadrv_max_queue_depth = 1;
11298 
11299 		rval = SATA_SUCCESS;
11300 	} else {
11301 		/*
11302 		 * Woops, no Identify Data.
11303 		 */
11304 		if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) {
11305 			rval = SATA_RETRY; /* may retry later */
11306 		} else if (rval == SATA_TRAN_ACCEPTED) {
11307 			if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR ||
11308 			    spkt->satapkt_reason == SATA_PKT_ABORTED ||
11309 			    spkt->satapkt_reason == SATA_PKT_TIMEOUT ||
11310 			    spkt->satapkt_reason == SATA_PKT_RESET)
11311 				rval = SATA_RETRY; /* may retry later */
11312 			else
11313 				rval = SATA_FAILURE;
11314 		} else {
11315 			rval = SATA_FAILURE;
11316 		}
11317 	}
11318 fail:
11319 	/* Free allocated resources */
11320 	sata_free_local_buffer(spx);
11321 	sata_pkt_free(spx);
11322 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
11323 
11324 	return (rval);
11325 }
11326 
11327 
11328 /*
11329  * Some devices may not come-up with default DMA mode (UDMA or MWDMA).
11330  * UDMA mode is checked first, followed by MWDMA mode.
11331  * set correctly, so this function is setting it to the highest supported level.
11332  * Older SATA spec required that the device supports at least DMA 4 mode and
11333  * UDMA mode is selected.  It is not mentioned in SerialATA 2.6, so this
11334  * restriction has been removed.
11335  *
11336  * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported.
11337  * Returns SATA_FAILURE if proper DMA mode could not be selected.
11338  *
11339  * NOTE: This function should be called only if DMA mode is supported.
11340  */
11341 static int
11342 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo)
11343 {
11344 	sata_pkt_t *spkt;
11345 	sata_cmd_t *scmd;
11346 	sata_pkt_txlate_t *spx;
11347 	int i, mode;
11348 	uint8_t subcmd;
11349 	int rval = SATA_SUCCESS;
11350 
11351 	ASSERT(sdinfo != NULL);
11352 	ASSERT(sata_hba_inst != NULL);
11353 
11354 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
11355 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) {
11356 		/* Find highest Ultra DMA mode supported */
11357 		for (mode = 6; mode >= 0; --mode) {
11358 			if (sdinfo->satadrv_id.ai_ultradma & (1 << mode))
11359 				break;
11360 		}
11361 #if 0
11362 		/* Left for historical reasons */
11363 		/*
11364 		 * Some initial version of SATA spec indicated that at least
11365 		 * UDMA mode 4 has to be supported. It is not mentioned in
11366 		 * SerialATA 2.6, so this restriction is removed.
11367 		 */
11368 		if (mode < 4)
11369 			return (SATA_FAILURE);
11370 #endif
11371 		/* Find UDMA mode currently selected */
11372 		for (i = 6; i >= 0; --i) {
11373 			if (sdinfo->satadrv_id.ai_ultradma & (1 << (i + 8)))
11374 				break;
11375 		}
11376 		if (i >= mode)
11377 			/* Nothing to do */
11378 			return (SATA_SUCCESS);
11379 
11380 		subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA;
11381 
11382 	} else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) {
11383 		/* Find highest MultiWord DMA mode supported */
11384 		for (mode = 2; mode >= 0; --mode) {
11385 			if (sdinfo->satadrv_id.ai_dworddma & (1 << mode))
11386 				break;
11387 		}
11388 		/* Find highest MultiWord DMA mode selected */
11389 		for (i = 2; i >= 0; --i) {
11390 			if (sdinfo->satadrv_id.ai_dworddma & (1 << (i + 8)))
11391 				break;
11392 		}
11393 		if (i >= mode)
11394 			/* Nothing to do */
11395 			return (SATA_SUCCESS);
11396 
11397 		subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA;
11398 	} else
11399 		return (SATA_SUCCESS);
11400 
11401 	/*
11402 	 * Set DMA mode via SET FEATURES COMMAND.
11403 	 * Prepare packet for SET FEATURES COMMAND.
11404 	 */
11405 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
11406 	spx->txlt_sata_hba_inst = sata_hba_inst;
11407 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
11408 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
11409 	if (spkt == NULL) {
11410 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11411 		    "sata_set_dma_mode: could not set DMA mode %", mode));
11412 		rval = SATA_FAILURE;
11413 		goto done;
11414 	}
11415 	/* Fill sata_pkt */
11416 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
11417 	/* Timeout 30s */
11418 	spkt->satapkt_time = sata_default_pkt_time;
11419 	/* Synchronous mode, no callback, interrupts */
11420 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
11421 	spkt->satapkt_comp = NULL;
11422 	scmd = &spkt->satapkt_cmd;
11423 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
11424 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
11425 	scmd->satacmd_addr_type = 0;
11426 	scmd->satacmd_device_reg = 0;
11427 	scmd->satacmd_status_reg = 0;
11428 	scmd->satacmd_error_reg = 0;
11429 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
11430 	scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE;
11431 	scmd->satacmd_sec_count_lsb = subcmd | mode;
11432 
11433 	/* Transfer command to HBA */
11434 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
11435 	    spkt) != SATA_TRAN_ACCEPTED ||
11436 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
11437 		/* Pkt execution failed */
11438 		rval = SATA_FAILURE;
11439 	}
11440 done:
11441 
11442 	/* Free allocated resources */
11443 	if (spkt != NULL)
11444 		sata_pkt_free(spx);
11445 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
11446 
11447 	return (rval);
11448 }
11449 
11450 
11451 /*
11452  * Set device caching mode.
11453  * One of the following operations should be specified:
11454  * SATAC_SF_ENABLE_READ_AHEAD
11455  * SATAC_SF_DISABLE_READ_AHEAD
11456  * SATAC_SF_ENABLE_WRITE_CACHE
11457  * SATAC_SF_DISABLE_WRITE_CACHE
11458  *
11459  * If operation fails, system log messgage is emitted.
11460  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
11461  */
11462 
11463 static int
11464 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
11465     int cache_op)
11466 {
11467 	sata_pkt_t *spkt;
11468 	sata_cmd_t *scmd;
11469 	sata_pkt_txlate_t *spx;
11470 	int rval = SATA_SUCCESS;
11471 	char *infop;
11472 
11473 	ASSERT(sdinfo != NULL);
11474 	ASSERT(sata_hba_inst != NULL);
11475 	ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD ||
11476 	    cache_op == SATAC_SF_DISABLE_READ_AHEAD ||
11477 	    cache_op == SATAC_SF_ENABLE_WRITE_CACHE ||
11478 	    cache_op == SATAC_SF_DISABLE_WRITE_CACHE);
11479 
11480 
11481 	/* Prepare packet for SET FEATURES COMMAND */
11482 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
11483 	spx->txlt_sata_hba_inst = sata_hba_inst;
11484 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
11485 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
11486 	if (spkt == NULL) {
11487 		rval = SATA_FAILURE;
11488 		goto failure;
11489 	}
11490 	/* Fill sata_pkt */
11491 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
11492 	/* Timeout 30s */
11493 	spkt->satapkt_time = sata_default_pkt_time;
11494 	/* Synchronous mode, no callback, interrupts */
11495 	spkt->satapkt_op_mode =
11496 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
11497 	spkt->satapkt_comp = NULL;
11498 	scmd = &spkt->satapkt_cmd;
11499 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
11500 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
11501 	scmd->satacmd_addr_type = 0;
11502 	scmd->satacmd_device_reg = 0;
11503 	scmd->satacmd_status_reg = 0;
11504 	scmd->satacmd_error_reg = 0;
11505 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
11506 	scmd->satacmd_features_reg = cache_op;
11507 
11508 	/* Transfer command to HBA */
11509 	if (((*SATA_START_FUNC(sata_hba_inst))(
11510 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
11511 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
11512 		/* Pkt execution failed */
11513 		switch (cache_op) {
11514 		case SATAC_SF_ENABLE_READ_AHEAD:
11515 			infop = "enabling read ahead failed";
11516 			break;
11517 		case SATAC_SF_DISABLE_READ_AHEAD:
11518 			infop = "disabling read ahead failed";
11519 			break;
11520 		case SATAC_SF_ENABLE_WRITE_CACHE:
11521 			infop = "enabling write cache failed";
11522 			break;
11523 		case SATAC_SF_DISABLE_WRITE_CACHE:
11524 			infop = "disabling write cache failed";
11525 			break;
11526 		}
11527 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
11528 		rval = SATA_FAILURE;
11529 	}
11530 failure:
11531 	/* Free allocated resources */
11532 	if (spkt != NULL)
11533 		sata_pkt_free(spx);
11534 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
11535 	return (rval);
11536 }
11537 
11538 /*
11539  * Set Removable Media Status Notification (enable/disable)
11540  * state == 0 , disable
11541  * state != 0 , enable
11542  *
11543  * If operation fails, system log messgage is emitted.
11544  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
11545  */
11546 
11547 static int
11548 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
11549     int state)
11550 {
11551 	sata_pkt_t *spkt;
11552 	sata_cmd_t *scmd;
11553 	sata_pkt_txlate_t *spx;
11554 	int rval = SATA_SUCCESS;
11555 	char *infop;
11556 
11557 	ASSERT(sdinfo != NULL);
11558 	ASSERT(sata_hba_inst != NULL);
11559 
11560 	/* Prepare packet for SET FEATURES COMMAND */
11561 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
11562 	spx->txlt_sata_hba_inst = sata_hba_inst;
11563 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
11564 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
11565 	if (spkt == NULL) {
11566 		rval = SATA_FAILURE;
11567 		goto failure;
11568 	}
11569 	/* Fill sata_pkt */
11570 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
11571 	/* Timeout 30s */
11572 	spkt->satapkt_time = sata_default_pkt_time;
11573 	/* Synchronous mode, no callback, interrupts */
11574 	spkt->satapkt_op_mode =
11575 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
11576 	spkt->satapkt_comp = NULL;
11577 	scmd = &spkt->satapkt_cmd;
11578 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
11579 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
11580 	scmd->satacmd_addr_type = 0;
11581 	scmd->satacmd_device_reg = 0;
11582 	scmd->satacmd_status_reg = 0;
11583 	scmd->satacmd_error_reg = 0;
11584 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
11585 	if (state == 0)
11586 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN;
11587 	else
11588 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN;
11589 
11590 	/* Transfer command to HBA */
11591 	if (((*SATA_START_FUNC(sata_hba_inst))(
11592 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
11593 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
11594 		/* Pkt execution failed */
11595 		if (state == 0)
11596 			infop = "disabling Removable Media Status "
11597 			    "Notification failed";
11598 		else
11599 			infop = "enabling Removable Media Status "
11600 			    "Notification failed";
11601 
11602 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
11603 		rval = SATA_FAILURE;
11604 	}
11605 failure:
11606 	/* Free allocated resources */
11607 	if (spkt != NULL)
11608 		sata_pkt_free(spx);
11609 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
11610 	return (rval);
11611 }
11612 
11613 
11614 /*
11615  * Update port SCR block
11616  */
11617 static void
11618 sata_update_port_scr(sata_port_scr_t *port_scr, sata_device_t *device)
11619 {
11620 	port_scr->sstatus = device->satadev_scr.sstatus;
11621 	port_scr->serror = device->satadev_scr.serror;
11622 	port_scr->scontrol = device->satadev_scr.scontrol;
11623 	port_scr->sactive = device->satadev_scr.sactive;
11624 	port_scr->snotific = device->satadev_scr.snotific;
11625 }
11626 
11627 /*
11628  * Update state and copy port ss* values from passed sata_device structure.
11629  * sata_address is validated - if not valid, nothing is changed in sata_scsi
11630  * configuration struct.
11631  *
11632  * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function
11633  * regardless of the state in device argument.
11634  *
11635  * Port mutex should be held while calling this function.
11636  */
11637 static void
11638 sata_update_port_info(sata_hba_inst_t *sata_hba_inst,
11639 	sata_device_t *sata_device)
11640 {
11641 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst,
11642 	    sata_device->satadev_addr.cport)));
11643 
11644 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT ||
11645 	    sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
11646 
11647 		sata_cport_info_t *cportinfo;
11648 
11649 		if (SATA_NUM_CPORTS(sata_hba_inst) <=
11650 		    sata_device->satadev_addr.cport)
11651 			return;
11652 
11653 		cportinfo = SATA_CPORT_INFO(sata_hba_inst,
11654 		    sata_device->satadev_addr.cport);
11655 		sata_update_port_scr(&cportinfo->cport_scr, sata_device);
11656 
11657 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
11658 		cportinfo->cport_state &= ~(SATA_PSTATE_PWRON |
11659 		    SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
11660 		cportinfo->cport_state |=
11661 		    sata_device->satadev_state & SATA_PSTATE_VALID;
11662 	} else {
11663 		sata_pmport_info_t *pmportinfo;
11664 
11665 		if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT) ||
11666 		    (sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) ||
11667 		    SATA_NUM_PMPORTS(sata_hba_inst,
11668 		    sata_device->satadev_addr.cport) <
11669 		    sata_device->satadev_addr.pmport)
11670 			return;
11671 
11672 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
11673 		    sata_device->satadev_addr.cport,
11674 		    sata_device->satadev_addr.pmport);
11675 		sata_update_port_scr(&pmportinfo->pmport_scr, sata_device);
11676 
11677 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
11678 		pmportinfo->pmport_state &=
11679 		    ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF |
11680 		    SATA_PSTATE_FAILED);
11681 		pmportinfo->pmport_state |=
11682 		    sata_device->satadev_state & SATA_PSTATE_VALID;
11683 	}
11684 }
11685 
11686 
11687 
11688 /*
11689  * Extract SATA port specification from an IOCTL argument.
11690  *
11691  * This function return the port the user land send us as is, unless it
11692  * cannot retrieve port spec, then -1 is returned.
11693  *
11694  * Note: Only cport  - no port multiplier port.
11695  */
11696 static int32_t
11697 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp)
11698 {
11699 	int32_t port;
11700 
11701 	/* Extract port number from nvpair in dca structure  */
11702 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
11703 		SATA_LOG_D((sata_hba_inst, CE_NOTE,
11704 		    "sata_get_port_num: invalid port spec 0x%x in ioctl",
11705 		    port));
11706 		port = -1;
11707 	}
11708 
11709 	return (port);
11710 }
11711 
11712 /*
11713  * Get dev_info_t pointer to the device node pointed to by port argument.
11714  * NOTE: target argument is a value used in ioctls to identify
11715  * the AP - it is not a sata_address.
11716  * It is a combination of cport, pmport and address qualifier, encodded same
11717  * way as a scsi target number.
11718  * At this moment it carries only cport number.
11719  *
11720  * No PMult hotplug support.
11721  *
11722  * Returns dev_info_t pointer if target device was found, NULL otherwise.
11723  */
11724 
11725 static dev_info_t *
11726 sata_get_target_dip(dev_info_t *dip, int32_t port)
11727 {
11728 	dev_info_t	*cdip = NULL;
11729 	int		target, tgt;
11730 	int		ncport;
11731 	int 		circ;
11732 
11733 	ncport = port & SATA_CFGA_CPORT_MASK;
11734 	target = SATA_TO_SCSI_TARGET(ncport, 0, SATA_ADDR_DCPORT);
11735 
11736 	ndi_devi_enter(dip, &circ);
11737 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
11738 		dev_info_t *next = ddi_get_next_sibling(cdip);
11739 
11740 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
11741 		    DDI_PROP_DONTPASS, "target", -1);
11742 		if (tgt == -1) {
11743 			/*
11744 			 * This is actually an error condition, but not
11745 			 * a fatal one. Just continue the search.
11746 			 */
11747 			cdip = next;
11748 			continue;
11749 		}
11750 
11751 		if (tgt == target)
11752 			break;
11753 
11754 		cdip = next;
11755 	}
11756 	ndi_devi_exit(dip, circ);
11757 
11758 	return (cdip);
11759 }
11760 
11761 
11762 /*
11763  * sata_cfgadm_state:
11764  * Use the sata port state and state of the target node to figure out
11765  * the cfgadm_state.
11766  *
11767  * The port argument is a value with encoded cport,
11768  * pmport and address qualifier, in the same manner as a scsi target number.
11769  * SCSI_TO_SATA_CPORT macro extracts cport number,
11770  * SCSI_TO_SATA_PMPORT extracts pmport number and
11771  * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag.
11772  *
11773  * For now, support is for cports only - no port multiplier device ports.
11774  */
11775 
11776 static void
11777 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port,
11778     devctl_ap_state_t *ap_state)
11779 {
11780 	uint16_t	cport;
11781 	int		port_state;
11782 
11783 	/* Cport only */
11784 	cport = SCSI_TO_SATA_CPORT(port);
11785 
11786 	port_state = SATA_CPORT_STATE(sata_hba_inst, cport);
11787 	if (port_state & SATA_PSTATE_SHUTDOWN ||
11788 	    port_state & SATA_PSTATE_FAILED) {
11789 		ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
11790 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
11791 		if (port_state & SATA_PSTATE_FAILED)
11792 			ap_state->ap_condition = AP_COND_FAILED;
11793 		else
11794 			ap_state->ap_condition = AP_COND_UNKNOWN;
11795 
11796 		return;
11797 	}
11798 
11799 	/* Need to check pmult device port here as well, when supported */
11800 
11801 	/* Port is enabled and ready */
11802 
11803 	switch (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport)) {
11804 	case SATA_DTYPE_NONE:
11805 	{
11806 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
11807 		ap_state->ap_condition = AP_COND_OK;
11808 		/* No device attached */
11809 		ap_state->ap_rstate = AP_RSTATE_EMPTY;
11810 		break;
11811 	}
11812 	case SATA_DTYPE_UNKNOWN:
11813 	case SATA_DTYPE_ATAPINONCD:
11814 	case SATA_DTYPE_PMULT:	/* Until PMult is supported */
11815 	case SATA_DTYPE_ATADISK:
11816 	case SATA_DTYPE_ATAPICD:
11817 	{
11818 		dev_info_t *tdip = NULL;
11819 		dev_info_t *dip = NULL;
11820 		int circ;
11821 
11822 		dip = SATA_DIP(sata_hba_inst);
11823 		tdip = sata_get_target_dip(dip, port);
11824 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
11825 		if (tdip != NULL) {
11826 			ndi_devi_enter(dip, &circ);
11827 			mutex_enter(&(DEVI(tdip)->devi_lock));
11828 			if (DEVI_IS_DEVICE_REMOVED(tdip)) {
11829 				/*
11830 				 * There could be the case where previously
11831 				 * configured and opened device was removed
11832 				 * and unknown device was plugged.
11833 				 * In such case we want to show a device, and
11834 				 * its configured or unconfigured state but
11835 				 * indicate unusable condition untill the
11836 				 * old target node is released and removed.
11837 				 */
11838 				ap_state->ap_condition = AP_COND_UNUSABLE;
11839 			} else {
11840 				ap_state->ap_condition = AP_COND_OK;
11841 			}
11842 			if ((DEVI_IS_DEVICE_OFFLINE(tdip)) ||
11843 			    (DEVI_IS_DEVICE_DOWN(tdip))) {
11844 				ap_state->ap_ostate =
11845 				    AP_OSTATE_UNCONFIGURED;
11846 			} else {
11847 				ap_state->ap_ostate =
11848 				    AP_OSTATE_CONFIGURED;
11849 			}
11850 			mutex_exit(&(DEVI(tdip)->devi_lock));
11851 			ndi_devi_exit(dip, circ);
11852 		} else {
11853 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
11854 			ap_state->ap_condition = AP_COND_UNKNOWN;
11855 		}
11856 		break;
11857 	}
11858 	default:
11859 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
11860 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
11861 		ap_state->ap_condition = AP_COND_UNKNOWN;
11862 		/*
11863 		 * This is actually internal error condition (non fatal),
11864 		 * because we have already checked all defined device types.
11865 		 */
11866 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11867 		    "sata_cfgadm_state: Internal error: "
11868 		    "unknown device type"));
11869 		break;
11870 	}
11871 }
11872 
11873 
11874 /*
11875  * Preset scsi extended sense data (to NO SENSE)
11876  * First 18 bytes of the sense data are preset to current valid sense
11877  * with a key NO SENSE data.
11878  *
11879  * Returns void
11880  */
11881 static void
11882 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense)
11883 {
11884 	sense->es_valid = 1;		/* Valid sense */
11885 	sense->es_class = CLASS_EXTENDED_SENSE;	/* 0x70 - current err */
11886 	sense->es_key = KEY_NO_SENSE;
11887 	sense->es_info_1 = 0;
11888 	sense->es_info_2 = 0;
11889 	sense->es_info_3 = 0;
11890 	sense->es_info_4 = 0;
11891 	sense->es_add_len = 10;	/* Additional length - replace with a def */
11892 	sense->es_cmd_info[0] = 0;
11893 	sense->es_cmd_info[1] = 0;
11894 	sense->es_cmd_info[2] = 0;
11895 	sense->es_cmd_info[3] = 0;
11896 	sense->es_add_code = 0;
11897 	sense->es_qual_code = 0;
11898 }
11899 
11900 /*
11901  * Register a legacy cmdk-style devid for the target (disk) device.
11902  *
11903  * Note: This function is called only when the HBA devinfo node has the
11904  * property "use-cmdk-devid-format" set. This property indicates that
11905  * devid compatible with old cmdk (target) driver is to be generated
11906  * for any target device attached to this controller. This will take
11907  * precedence over the devid generated by sd (target) driver.
11908  * This function is derived from cmdk_devid_setup() function in cmdk.c.
11909  */
11910 static void
11911 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo)
11912 {
11913 	char	*hwid;
11914 	int	modlen;
11915 	int	serlen;
11916 	int	rval;
11917 	ddi_devid_t	devid;
11918 
11919 	/*
11920 	 * device ID is a concatanation of model number, "=", serial number.
11921 	 */
11922 	hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP);
11923 	bcopy(&sdinfo->satadrv_id.ai_model, hwid,
11924 	    sizeof (sdinfo->satadrv_id.ai_model));
11925 	swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model));
11926 	modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model));
11927 	if (modlen == 0)
11928 		goto err;
11929 	hwid[modlen++] = '=';
11930 	bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen],
11931 	    sizeof (sdinfo->satadrv_id.ai_drvser));
11932 	swab(&hwid[modlen], &hwid[modlen],
11933 	    sizeof (sdinfo->satadrv_id.ai_drvser));
11934 	serlen = sata_check_modser(&hwid[modlen],
11935 	    sizeof (sdinfo->satadrv_id.ai_drvser));
11936 	if (serlen == 0)
11937 		goto err;
11938 	hwid[modlen + serlen] = 0; /* terminate the hwid string */
11939 
11940 	/* initialize/register devid */
11941 	if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL,
11942 	    (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS)
11943 		rval = ddi_devid_register(dip, devid);
11944 
11945 	if (rval != DDI_SUCCESS)
11946 		cmn_err(CE_WARN, "sata: failed to create devid for the disk"
11947 		    " on port %d", sdinfo->satadrv_addr.cport);
11948 err:
11949 	kmem_free(hwid, LEGACY_HWID_LEN);
11950 }
11951 
11952 /*
11953  * valid model/serial string must contain a non-zero non-space characters.
11954  * trim trailing spaces/NULLs.
11955  */
11956 static int
11957 sata_check_modser(char *buf, int buf_len)
11958 {
11959 	boolean_t ret;
11960 	char *s;
11961 	int i;
11962 	int tb;
11963 	char ch;
11964 
11965 	ret = B_FALSE;
11966 	s = buf;
11967 	for (i = 0; i < buf_len; i++) {
11968 		ch = *s++;
11969 		if (ch != ' ' && ch != '\0')
11970 			tb = i + 1;
11971 		if (ch != ' ' && ch != '\0' && ch != '0')
11972 			ret = B_TRUE;
11973 	}
11974 
11975 	if (ret == B_FALSE)
11976 		return (0); /* invalid string */
11977 
11978 	return (tb); /* return length */
11979 }
11980 
11981 /*
11982  * sata_set_drive_features function compares current device features setting
11983  * with the saved device features settings and, if there is a difference,
11984  * it restores device features setting to the previously saved state.
11985  * It also arbitrarily tries to select the highest supported DMA mode.
11986  * Device Identify or Identify Packet Device data has to be current.
11987  * At the moment read ahead and write cache are considered for all devices.
11988  * For atapi devices, Removable Media Status Notification is set in addition
11989  * to common features.
11990  *
11991  * This function cannot be called in the interrupt context (it may sleep).
11992  *
11993  * The input argument sdinfo should point to the drive info structure
11994  * to be updated after features are set. Note, that only
11995  * device (packet) identify data is updated, not the flags indicating the
11996  * supported features.
11997  *
11998  * Returns TRUE if successful or there was nothing to do. Device Identify data
11999  * in the drive info structure pointed to by the sdinfo argumens is updated
12000  * even when no features were set or changed.
12001  *
12002  * Returns FALSE if device features could not be set.
12003  *
12004  * Note: This function may fail the port, making it inaccessible.
12005  * In such case the explicit port disconnect/connect or physical device
12006  * detach/attach is required to re-evaluate port state again.
12007  */
12008 
12009 static int
12010 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst,
12011     sata_drive_info_t *sdinfo, int restore)
12012 {
12013 	int rval = SATA_SUCCESS;
12014 	sata_drive_info_t new_sdinfo;
12015 	char *finfo = "sata_set_drive_features: cannot";
12016 	char *finfox;
12017 	int cache_op;
12018 
12019 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
12020 	new_sdinfo.satadrv_addr = sdinfo->satadrv_addr;
12021 	new_sdinfo.satadrv_type = sdinfo->satadrv_type;
12022 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
12023 		/*
12024 		 * Cannot get device identification - retry later
12025 		 */
12026 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12027 		    "%s fetch device identify data\n", finfo));
12028 		return (SATA_FAILURE);
12029 	}
12030 	finfox = (restore != 0) ? " restore device features" :
12031 	    " initialize device features\n";
12032 
12033 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12034 		/* Arbitrarily set UDMA mode */
12035 		if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
12036 		    SATA_SUCCESS) {
12037 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12038 			    "%s set UDMA mode\n", finfo));
12039 			return (SATA_FAILURE);
12040 		}
12041 	} else { /* Assume SATA ATAPI CD/DVD */
12042 		/*  Set Removable Media Status Notification, if necessary */
12043 		if ((new_sdinfo.satadrv_id.ai_cmdset83 &
12044 		    SATA_RM_STATUS_NOTIFIC) != 0 && restore != 0) {
12045 			if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) &&
12046 			    (!(new_sdinfo.satadrv_id.ai_features86 &
12047 			    SATA_RM_STATUS_NOTIFIC))) ||
12048 			    ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) &&
12049 			    (new_sdinfo.satadrv_id.ai_features86 &
12050 			    SATA_RM_STATUS_NOTIFIC))) {
12051 				/* Current setting does not match saved one */
12052 				if (sata_set_rmsn(sata_hba_inst, sdinfo,
12053 				    sdinfo->satadrv_settings &
12054 				    SATA_DEV_RMSN) != SATA_SUCCESS)
12055 					rval = SATA_FAILURE;
12056 			}
12057 		}
12058 		/*
12059 		 * We have to set Multiword DMA or UDMA, if it is supported, as
12060 		 * we want to use DMA transfer mode whenever possible.
12061 		 * Some devices require explicit setting of the DMA mode.
12062 		 */
12063 		if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) {
12064 			/* Set highest supported DMA mode */
12065 			if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
12066 			    SATA_SUCCESS) {
12067 				SATA_LOG_D((sata_hba_inst, CE_WARN,
12068 				    "%s set UDMA mode\n", finfo));
12069 				rval = SATA_FAILURE;
12070 			}
12071 		}
12072 	}
12073 
12074 	if (!(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) &&
12075 	    !(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) {
12076 		/* None of the features is supported - do nothing */
12077 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12078 		    "settable features not supported\n", NULL);
12079 		goto update_sdinfo;
12080 	}
12081 
12082 	if (((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) &&
12083 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) &&
12084 	    ((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) &&
12085 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
12086 		/* Nothing to do */
12087 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12088 		    "no device features to set\n", NULL);
12089 		goto update_sdinfo;
12090 	}
12091 
12092 	if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) &&
12093 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD))) {
12094 		if (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) {
12095 			/* Enable read ahead / read cache */
12096 			cache_op = SATAC_SF_ENABLE_READ_AHEAD;
12097 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12098 			    "enabling read cache\n", NULL);
12099 		} else {
12100 			/* Disable read ahead  / read cache */
12101 			cache_op = SATAC_SF_DISABLE_READ_AHEAD;
12102 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12103 			    "disabling read cache\n", NULL);
12104 		}
12105 
12106 		/* Try to set read cache mode */
12107 		if (sata_set_cache_mode(sata_hba_inst, &new_sdinfo,
12108 		    cache_op) != SATA_SUCCESS) {
12109 			/* Pkt execution failed */
12110 			rval = SATA_FAILURE;
12111 		}
12112 	}
12113 
12114 	if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) &&
12115 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
12116 		if (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) {
12117 			/* Enable write cache */
12118 			cache_op = SATAC_SF_ENABLE_WRITE_CACHE;
12119 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12120 			    "enabling write cache\n", NULL);
12121 		} else {
12122 			/* Disable write cache */
12123 			cache_op = SATAC_SF_DISABLE_WRITE_CACHE;
12124 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12125 			    "disabling write cache\n", NULL);
12126 		}
12127 		/* Try to set write cache mode */
12128 		if (sata_set_cache_mode(sata_hba_inst, &new_sdinfo,
12129 		    cache_op) != SATA_SUCCESS) {
12130 			/* Pkt execution failed */
12131 			rval = SATA_FAILURE;
12132 		}
12133 	}
12134 
12135 	if (rval == SATA_FAILURE)
12136 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12137 		    "%s %s", finfo, finfox));
12138 update_sdinfo:
12139 	/*
12140 	 * We need to fetch Device Identify data again
12141 	 */
12142 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
12143 		/*
12144 		 * Cannot get device identification - retry later
12145 		 */
12146 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12147 		    "%s cannot re-fetch device identify data\n"));
12148 		rval = SATA_FAILURE;
12149 	}
12150 	/* Copy device sata info. */
12151 	sdinfo->satadrv_id = new_sdinfo.satadrv_id;
12152 
12153 	return (rval);
12154 }
12155 
12156 
12157 /*
12158  *
12159  * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if
12160  * unable to determine.
12161  *
12162  * Cannot be called in an interrupt context.
12163  *
12164  * Called by sata_build_lsense_page_2f()
12165  */
12166 
12167 static int
12168 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst,
12169     sata_drive_info_t *sdinfo)
12170 {
12171 	sata_pkt_t *spkt;
12172 	sata_cmd_t *scmd;
12173 	sata_pkt_txlate_t *spx;
12174 	int rval;
12175 
12176 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12177 	spx->txlt_sata_hba_inst = sata_hba_inst;
12178 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12179 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12180 	if (spkt == NULL) {
12181 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12182 		return (-1);
12183 	}
12184 	/* address is needed now */
12185 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12186 
12187 
12188 	/* Fill sata_pkt */
12189 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12190 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12191 	/* Synchronous mode, no callback */
12192 	spkt->satapkt_comp = NULL;
12193 	/* Timeout 30s */
12194 	spkt->satapkt_time = sata_default_pkt_time;
12195 
12196 	scmd = &spkt->satapkt_cmd;
12197 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
12198 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
12199 
12200 	/* Set up which registers need to be returned */
12201 	scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE;
12202 	scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE;
12203 
12204 	/* Build SMART_RETURN_STATUS cmd in the sata_pkt */
12205 	scmd->satacmd_addr_type = 0;		/* N/A */
12206 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
12207 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
12208 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
12209 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
12210 	scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS;
12211 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12212 	scmd->satacmd_cmd_reg = SATAC_SMART;
12213 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12214 	    sdinfo->satadrv_addr.cport)));
12215 
12216 
12217 	/* Send pkt to SATA HBA driver */
12218 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12219 	    SATA_TRAN_ACCEPTED ||
12220 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12221 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12222 		    sdinfo->satadrv_addr.cport)));
12223 		/*
12224 		 * Whoops, no SMART RETURN STATUS
12225 		 */
12226 		rval = -1;
12227 	} else {
12228 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12229 		    sdinfo->satadrv_addr.cport)));
12230 		if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
12231 			rval = -1;
12232 			goto fail;
12233 		}
12234 		if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
12235 			rval = -1;
12236 			goto fail;
12237 		}
12238 		if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) &&
12239 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2))
12240 			rval = 0;
12241 		else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) &&
12242 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4))
12243 			rval = 1;
12244 		else {
12245 			rval = -1;
12246 			goto fail;
12247 		}
12248 	}
12249 fail:
12250 	/* Free allocated resources */
12251 	sata_pkt_free(spx);
12252 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12253 
12254 	return (rval);
12255 }
12256 
12257 /*
12258  *
12259  * Returns 0 if succeeded, -1 otherwise
12260  *
12261  * Cannot be called in an interrupt context.
12262  *
12263  */
12264 static int
12265 sata_fetch_smart_data(
12266 	sata_hba_inst_t *sata_hba_inst,
12267 	sata_drive_info_t *sdinfo,
12268 	struct smart_data *smart_data)
12269 {
12270 	sata_pkt_t *spkt;
12271 	sata_cmd_t *scmd;
12272 	sata_pkt_txlate_t *spx;
12273 	int rval;
12274 
12275 #if ! defined(lint)
12276 	ASSERT(sizeof (struct smart_data) == 512);
12277 #endif
12278 
12279 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12280 	spx->txlt_sata_hba_inst = sata_hba_inst;
12281 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12282 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12283 	if (spkt == NULL) {
12284 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12285 		return (-1);
12286 	}
12287 	/* address is needed now */
12288 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12289 
12290 
12291 	/* Fill sata_pkt */
12292 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12293 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12294 	/* Synchronous mode, no callback */
12295 	spkt->satapkt_comp = NULL;
12296 	/* Timeout 30s */
12297 	spkt->satapkt_time = sata_default_pkt_time;
12298 
12299 	scmd = &spkt->satapkt_cmd;
12300 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
12301 
12302 	/*
12303 	 * Allocate buffer for SMART data
12304 	 */
12305 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
12306 	    sizeof (struct smart_data));
12307 	if (scmd->satacmd_bp == NULL) {
12308 		sata_pkt_free(spx);
12309 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12310 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12311 		    "sata_fetch_smart_data: "
12312 		    "cannot allocate buffer"));
12313 		return (-1);
12314 	}
12315 
12316 
12317 	/* Build SMART_READ_DATA cmd in the sata_pkt */
12318 	scmd->satacmd_addr_type = 0;		/* N/A */
12319 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
12320 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
12321 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
12322 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
12323 	scmd->satacmd_features_reg = SATA_SMART_READ_DATA;
12324 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12325 	scmd->satacmd_cmd_reg = SATAC_SMART;
12326 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12327 	    sdinfo->satadrv_addr.cport)));
12328 
12329 	/* Send pkt to SATA HBA driver */
12330 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12331 	    SATA_TRAN_ACCEPTED ||
12332 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12333 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12334 		    sdinfo->satadrv_addr.cport)));
12335 		/*
12336 		 * Whoops, no SMART DATA available
12337 		 */
12338 		rval = -1;
12339 		goto fail;
12340 	} else {
12341 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12342 		    sdinfo->satadrv_addr.cport)));
12343 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
12344 		    DDI_DMA_SYNC_FORKERNEL);
12345 		ASSERT(rval == DDI_SUCCESS);
12346 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data,
12347 		    sizeof (struct smart_data));
12348 	}
12349 
12350 fail:
12351 	/* Free allocated resources */
12352 	sata_free_local_buffer(spx);
12353 	sata_pkt_free(spx);
12354 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12355 
12356 	return (rval);
12357 }
12358 
12359 /*
12360  * Used by LOG SENSE page 0x10
12361  *
12362  * return 0 for success, -1 otherwise
12363  *
12364  */
12365 static int
12366 sata_ext_smart_selftest_read_log(
12367 	sata_hba_inst_t *sata_hba_inst,
12368 	sata_drive_info_t *sdinfo,
12369 	struct smart_ext_selftest_log *ext_selftest_log,
12370 	uint16_t block_num)
12371 {
12372 	sata_pkt_txlate_t *spx;
12373 	sata_pkt_t *spkt;
12374 	sata_cmd_t *scmd;
12375 	int rval;
12376 
12377 #if ! defined(lint)
12378 	ASSERT(sizeof (struct smart_ext_selftest_log) == 512);
12379 #endif
12380 
12381 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12382 	spx->txlt_sata_hba_inst = sata_hba_inst;
12383 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12384 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12385 	if (spkt == NULL) {
12386 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12387 		return (-1);
12388 	}
12389 	/* address is needed now */
12390 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12391 
12392 
12393 	/* Fill sata_pkt */
12394 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12395 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12396 	/* Synchronous mode, no callback */
12397 	spkt->satapkt_comp = NULL;
12398 	/* Timeout 30s */
12399 	spkt->satapkt_time = sata_default_pkt_time;
12400 
12401 	scmd = &spkt->satapkt_cmd;
12402 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
12403 
12404 	/*
12405 	 * Allocate buffer for SMART extended self-test log
12406 	 */
12407 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
12408 	    sizeof (struct smart_ext_selftest_log));
12409 	if (scmd->satacmd_bp == NULL) {
12410 		sata_pkt_free(spx);
12411 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12412 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12413 		    "sata_ext_smart_selftest_log: "
12414 		    "cannot allocate buffer"));
12415 		return (-1);
12416 	}
12417 
12418 	/* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */
12419 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
12420 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of selftest log */
12421 	scmd->satacmd_sec_count_msb = 0;	/* One sector of selftest log */
12422 	scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE;
12423 	scmd->satacmd_lba_low_msb = 0;
12424 	scmd->satacmd_lba_mid_lsb = block_num & 0xff;
12425 	scmd->satacmd_lba_mid_msb = block_num >> 8;
12426 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12427 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
12428 
12429 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12430 	    sdinfo->satadrv_addr.cport)));
12431 
12432 	/* Send pkt to SATA HBA driver */
12433 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12434 	    SATA_TRAN_ACCEPTED ||
12435 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12436 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12437 		    sdinfo->satadrv_addr.cport)));
12438 
12439 		/*
12440 		 * Whoops, no SMART selftest log info available
12441 		 */
12442 		rval = -1;
12443 		goto fail;
12444 	} else {
12445 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12446 		    sdinfo->satadrv_addr.cport)));
12447 
12448 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
12449 		    DDI_DMA_SYNC_FORKERNEL);
12450 		ASSERT(rval == DDI_SUCCESS);
12451 		bcopy(scmd->satacmd_bp->b_un.b_addr,
12452 		    (uint8_t *)ext_selftest_log,
12453 		    sizeof (struct smart_ext_selftest_log));
12454 		rval = 0;
12455 	}
12456 
12457 fail:
12458 	/* Free allocated resources */
12459 	sata_free_local_buffer(spx);
12460 	sata_pkt_free(spx);
12461 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12462 
12463 	return (rval);
12464 }
12465 
12466 /*
12467  * Returns 0 for success, -1 otherwise
12468  *
12469  * SMART self-test log data is returned in buffer pointed to by selftest_log
12470  */
12471 static int
12472 sata_smart_selftest_log(
12473 	sata_hba_inst_t *sata_hba_inst,
12474 	sata_drive_info_t *sdinfo,
12475 	struct smart_selftest_log *selftest_log)
12476 {
12477 	sata_pkt_t *spkt;
12478 	sata_cmd_t *scmd;
12479 	sata_pkt_txlate_t *spx;
12480 	int rval;
12481 
12482 #if ! defined(lint)
12483 	ASSERT(sizeof (struct smart_selftest_log) == 512);
12484 #endif
12485 
12486 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12487 	spx->txlt_sata_hba_inst = sata_hba_inst;
12488 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12489 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12490 	if (spkt == NULL) {
12491 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12492 		return (-1);
12493 	}
12494 	/* address is needed now */
12495 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12496 
12497 
12498 	/* Fill sata_pkt */
12499 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12500 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12501 	/* Synchronous mode, no callback */
12502 	spkt->satapkt_comp = NULL;
12503 	/* Timeout 30s */
12504 	spkt->satapkt_time = sata_default_pkt_time;
12505 
12506 	scmd = &spkt->satapkt_cmd;
12507 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
12508 
12509 	/*
12510 	 * Allocate buffer for SMART SELFTEST LOG
12511 	 */
12512 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
12513 	    sizeof (struct smart_selftest_log));
12514 	if (scmd->satacmd_bp == NULL) {
12515 		sata_pkt_free(spx);
12516 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12517 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12518 		    "sata_smart_selftest_log: "
12519 		    "cannot allocate buffer"));
12520 		return (-1);
12521 	}
12522 
12523 	/* Build SMART_READ_LOG cmd in the sata_pkt */
12524 	scmd->satacmd_addr_type = 0;		/* N/A */
12525 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of SMART log */
12526 	scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE;
12527 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
12528 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
12529 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
12530 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12531 	scmd->satacmd_cmd_reg = SATAC_SMART;
12532 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12533 	    sdinfo->satadrv_addr.cport)));
12534 
12535 	/* Send pkt to SATA HBA driver */
12536 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12537 	    SATA_TRAN_ACCEPTED ||
12538 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12539 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12540 		    sdinfo->satadrv_addr.cport)));
12541 		/*
12542 		 * Whoops, no SMART DATA available
12543 		 */
12544 		rval = -1;
12545 		goto fail;
12546 	} else {
12547 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12548 		    sdinfo->satadrv_addr.cport)));
12549 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
12550 		    DDI_DMA_SYNC_FORKERNEL);
12551 		ASSERT(rval == DDI_SUCCESS);
12552 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log,
12553 		    sizeof (struct smart_selftest_log));
12554 		rval = 0;
12555 	}
12556 
12557 fail:
12558 	/* Free allocated resources */
12559 	sata_free_local_buffer(spx);
12560 	sata_pkt_free(spx);
12561 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12562 
12563 	return (rval);
12564 }
12565 
12566 
12567 /*
12568  * Returns 0 for success, -1 otherwise
12569  *
12570  * SMART READ LOG data is returned in buffer pointed to by smart_log
12571  */
12572 static int
12573 sata_smart_read_log(
12574 	sata_hba_inst_t *sata_hba_inst,
12575 	sata_drive_info_t *sdinfo,
12576 	uint8_t *smart_log,		/* where the data should be returned */
12577 	uint8_t which_log,		/* which log should be returned */
12578 	uint8_t log_size)		/* # of 512 bytes in log */
12579 {
12580 	sata_pkt_t *spkt;
12581 	sata_cmd_t *scmd;
12582 	sata_pkt_txlate_t *spx;
12583 	int rval;
12584 
12585 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12586 	spx->txlt_sata_hba_inst = sata_hba_inst;
12587 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12588 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12589 	if (spkt == NULL) {
12590 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12591 		return (-1);
12592 	}
12593 	/* address is needed now */
12594 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12595 
12596 
12597 	/* Fill sata_pkt */
12598 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12599 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12600 	/* Synchronous mode, no callback */
12601 	spkt->satapkt_comp = NULL;
12602 	/* Timeout 30s */
12603 	spkt->satapkt_time = sata_default_pkt_time;
12604 
12605 	scmd = &spkt->satapkt_cmd;
12606 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
12607 
12608 	/*
12609 	 * Allocate buffer for SMART READ LOG
12610 	 */
12611 	scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512);
12612 	if (scmd->satacmd_bp == NULL) {
12613 		sata_pkt_free(spx);
12614 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12615 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12616 		    "sata_smart_read_log: " "cannot allocate buffer"));
12617 		return (-1);
12618 	}
12619 
12620 	/* Build SMART_READ_LOG cmd in the sata_pkt */
12621 	scmd->satacmd_addr_type = 0;		/* N/A */
12622 	scmd->satacmd_sec_count_lsb = log_size;	/* what the caller asked for */
12623 	scmd->satacmd_lba_low_lsb = which_log;	/* which log page */
12624 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
12625 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
12626 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
12627 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12628 	scmd->satacmd_cmd_reg = SATAC_SMART;
12629 
12630 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12631 	    sdinfo->satadrv_addr.cport)));
12632 
12633 	/* Send pkt to SATA HBA driver */
12634 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12635 	    SATA_TRAN_ACCEPTED ||
12636 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12637 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12638 		    sdinfo->satadrv_addr.cport)));
12639 
12640 		/*
12641 		 * Whoops, no SMART DATA available
12642 		 */
12643 		rval = -1;
12644 		goto fail;
12645 	} else {
12646 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12647 		    sdinfo->satadrv_addr.cport)));
12648 
12649 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
12650 		    DDI_DMA_SYNC_FORKERNEL);
12651 		ASSERT(rval == DDI_SUCCESS);
12652 		bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512);
12653 		rval = 0;
12654 	}
12655 
12656 fail:
12657 	/* Free allocated resources */
12658 	sata_free_local_buffer(spx);
12659 	sata_pkt_free(spx);
12660 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12661 
12662 	return (rval);
12663 }
12664 
12665 /*
12666  * Used by LOG SENSE page 0x10
12667  *
12668  * return 0 for success, -1 otherwise
12669  *
12670  */
12671 static int
12672 sata_read_log_ext_directory(
12673 	sata_hba_inst_t *sata_hba_inst,
12674 	sata_drive_info_t *sdinfo,
12675 	struct read_log_ext_directory *logdir)
12676 {
12677 	sata_pkt_txlate_t *spx;
12678 	sata_pkt_t *spkt;
12679 	sata_cmd_t *scmd;
12680 	int rval;
12681 
12682 #if ! defined(lint)
12683 	ASSERT(sizeof (struct read_log_ext_directory) == 512);
12684 #endif
12685 
12686 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12687 	spx->txlt_sata_hba_inst = sata_hba_inst;
12688 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12689 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12690 	if (spkt == NULL) {
12691 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12692 		return (-1);
12693 	}
12694 
12695 	/* Fill sata_pkt */
12696 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12697 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12698 	/* Synchronous mode, no callback */
12699 	spkt->satapkt_comp = NULL;
12700 	/* Timeout 30s */
12701 	spkt->satapkt_time = sata_default_pkt_time;
12702 
12703 	scmd = &spkt->satapkt_cmd;
12704 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
12705 
12706 	/*
12707 	 * Allocate buffer for SMART READ LOG EXTENDED command
12708 	 */
12709 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
12710 	    sizeof (struct read_log_ext_directory));
12711 	if (scmd->satacmd_bp == NULL) {
12712 		sata_pkt_free(spx);
12713 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12714 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12715 		    "sata_read_log_ext_directory: "
12716 		    "cannot allocate buffer"));
12717 		return (-1);
12718 	}
12719 
12720 	/* Build READ LOG EXT w/ log directory cmd in the  sata_pkt */
12721 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
12722 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of directory */
12723 	scmd->satacmd_sec_count_msb = 0;	/* One sector of directory */
12724 	scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY;
12725 	scmd->satacmd_lba_low_msb = 0;
12726 	scmd->satacmd_lba_mid_lsb = 0;
12727 	scmd->satacmd_lba_mid_msb = 0;
12728 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12729 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
12730 
12731 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12732 	    sdinfo->satadrv_addr.cport)));
12733 
12734 	/* Send pkt to SATA HBA driver */
12735 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12736 	    SATA_TRAN_ACCEPTED ||
12737 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12738 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12739 		    sdinfo->satadrv_addr.cport)));
12740 		/*
12741 		 * Whoops, no SMART selftest log info available
12742 		 */
12743 		rval = -1;
12744 		goto fail;
12745 	} else {
12746 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12747 		    sdinfo->satadrv_addr.cport)));
12748 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
12749 		    DDI_DMA_SYNC_FORKERNEL);
12750 		ASSERT(rval == DDI_SUCCESS);
12751 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir,
12752 		    sizeof (struct read_log_ext_directory));
12753 		rval = 0;
12754 	}
12755 
12756 fail:
12757 	/* Free allocated resources */
12758 	sata_free_local_buffer(spx);
12759 	sata_pkt_free(spx);
12760 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12761 
12762 	return (rval);
12763 }
12764 
12765 /*
12766  * Set up error retrieval sata command for NCQ command error data
12767  * recovery.
12768  *
12769  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
12770  * returns SATA_FAILURE otherwise.
12771  */
12772 static int
12773 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
12774 {
12775 #ifndef __lock_lint
12776 	_NOTE(ARGUNUSED(sdinfo))
12777 #endif
12778 
12779 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
12780 	sata_cmd_t *scmd;
12781 	struct buf *bp;
12782 
12783 	/* Operation modes are up to the caller */
12784 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12785 
12786 	/* Synchronous mode, no callback - may be changed by the caller */
12787 	spkt->satapkt_comp = NULL;
12788 	spkt->satapkt_time = sata_default_pkt_time;
12789 
12790 	scmd = &spkt->satapkt_cmd;
12791 	bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t));
12792 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
12793 
12794 	/*
12795 	 * Allocate dma_able buffer error data.
12796 	 * Buffer allocation will take care of buffer alignment and other DMA
12797 	 * attributes.
12798 	 */
12799 	bp = sata_alloc_local_buffer(spx,
12800 	    sizeof (struct sata_ncq_error_recovery_page));
12801 	if (bp == NULL)
12802 		return (SATA_FAILURE);
12803 
12804 	bp_mapin(bp); /* make data buffer accessible */
12805 	scmd->satacmd_bp = bp;
12806 
12807 	/*
12808 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
12809 	 * before accessing it. Handle is in usual place in translate struct.
12810 	 */
12811 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
12812 
12813 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
12814 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
12815 
12816 	return (SATA_SUCCESS);
12817 }
12818 
12819 /*
12820  * sata_xlate_errors() is used to translate (S)ATA error
12821  * information to SCSI information returned in the SCSI
12822  * packet.
12823  */
12824 static void
12825 sata_xlate_errors(sata_pkt_txlate_t *spx)
12826 {
12827 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
12828 	struct scsi_extended_sense *sense;
12829 
12830 	scsipkt->pkt_reason = CMD_INCOMPLETE;
12831 	*scsipkt->pkt_scbp = STATUS_CHECK;
12832 	sense = sata_arq_sense(spx);
12833 
12834 	switch (spx->txlt_sata_pkt->satapkt_reason) {
12835 	case SATA_PKT_PORT_ERROR:
12836 		/*
12837 		 * We have no device data. Assume no data transfered.
12838 		 */
12839 		sense->es_key = KEY_HARDWARE_ERROR;
12840 		break;
12841 
12842 	case SATA_PKT_DEV_ERROR:
12843 		if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
12844 		    SATA_STATUS_ERR) {
12845 			/*
12846 			 * determine dev error reason from error
12847 			 * reg content
12848 			 */
12849 			sata_decode_device_error(spx, sense);
12850 			break;
12851 		}
12852 		/* No extended sense key - no info available */
12853 		break;
12854 
12855 	case SATA_PKT_TIMEOUT:
12856 		/*
12857 		 * scsipkt->pkt_reason = CMD_TIMEOUT; This causes problems.
12858 		 */
12859 		scsipkt->pkt_reason = CMD_INCOMPLETE;
12860 		/* No extended sense key */
12861 		break;
12862 
12863 	case SATA_PKT_ABORTED:
12864 		scsipkt->pkt_reason = CMD_ABORTED;
12865 		/* No extended sense key */
12866 		break;
12867 
12868 	case SATA_PKT_RESET:
12869 		/*
12870 		 * pkt aborted either by an explicit reset request from
12871 		 * a host, or due to error recovery
12872 		 */
12873 		scsipkt->pkt_reason = CMD_RESET;
12874 		break;
12875 
12876 	default:
12877 		scsipkt->pkt_reason = CMD_TRAN_ERR;
12878 		break;
12879 	}
12880 }
12881 
12882 
12883 
12884 
12885 /*
12886  * Log sata message
12887  * dev pathname msg line preceeds the logged message.
12888  */
12889 
12890 static	void
12891 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...)
12892 {
12893 	char pathname[128];
12894 	dev_info_t *dip;
12895 	va_list ap;
12896 
12897 	mutex_enter(&sata_log_mutex);
12898 
12899 	va_start(ap, fmt);
12900 	(void) vsprintf(sata_log_buf, fmt, ap);
12901 	va_end(ap);
12902 
12903 	if (sata_hba_inst != NULL) {
12904 		dip = SATA_DIP(sata_hba_inst);
12905 		(void) ddi_pathname(dip, pathname);
12906 	} else {
12907 		pathname[0] = 0;
12908 	}
12909 	if (level == CE_CONT) {
12910 		if (sata_debug_flags == 0)
12911 			cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf);
12912 		else
12913 			cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf);
12914 	} else
12915 		cmn_err(level, "%s:\n %s", pathname, sata_log_buf);
12916 
12917 	mutex_exit(&sata_log_mutex);
12918 }
12919 
12920 
12921 /* ******** Asynchronous HBA events handling & hotplugging support ******** */
12922 
12923 /*
12924  * Start or terminate the thread, depending on flag arg and current state
12925  */
12926 static void
12927 sata_event_thread_control(int startstop)
12928 {
12929 	static 	int sata_event_thread_terminating = 0;
12930 	static 	int sata_event_thread_starting = 0;
12931 	int i;
12932 
12933 	mutex_enter(&sata_event_mutex);
12934 
12935 	if (startstop == 0 && (sata_event_thread_starting == 1 ||
12936 	    sata_event_thread_terminating == 1)) {
12937 		mutex_exit(&sata_event_mutex);
12938 		return;
12939 	}
12940 	if (startstop == 1 && sata_event_thread_starting == 1) {
12941 		mutex_exit(&sata_event_mutex);
12942 		return;
12943 	}
12944 	if (startstop == 1 && sata_event_thread_terminating == 1) {
12945 		sata_event_thread_starting = 1;
12946 		/* wait til terminate operation completes */
12947 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
12948 		while (sata_event_thread_terminating == 1) {
12949 			if (i-- <= 0) {
12950 				sata_event_thread_starting = 0;
12951 				mutex_exit(&sata_event_mutex);
12952 #ifdef SATA_DEBUG
12953 				cmn_err(CE_WARN, "sata_event_thread_control: "
12954 				    "timeout waiting for thread to terminate");
12955 #endif
12956 				return;
12957 			}
12958 			mutex_exit(&sata_event_mutex);
12959 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
12960 			mutex_enter(&sata_event_mutex);
12961 		}
12962 	}
12963 	if (startstop == 1) {
12964 		if (sata_event_thread == NULL) {
12965 			sata_event_thread = thread_create(NULL, 0,
12966 			    (void (*)())sata_event_daemon,
12967 			    &sata_hba_list, 0, &p0, TS_RUN, minclsyspri);
12968 		}
12969 		sata_event_thread_starting = 0;
12970 		mutex_exit(&sata_event_mutex);
12971 		return;
12972 	}
12973 
12974 	/*
12975 	 * If we got here, thread may need to be terminated
12976 	 */
12977 	if (sata_event_thread != NULL) {
12978 		int i;
12979 		/* Signal event thread to go away */
12980 		sata_event_thread_terminating = 1;
12981 		sata_event_thread_terminate = 1;
12982 		cv_signal(&sata_event_cv);
12983 		/*
12984 		 * Wait til daemon terminates.
12985 		 */
12986 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
12987 		while (sata_event_thread_terminate == 1) {
12988 			mutex_exit(&sata_event_mutex);
12989 			if (i-- <= 0) {
12990 				/* Daemon did not go away !!! */
12991 #ifdef SATA_DEBUG
12992 				cmn_err(CE_WARN, "sata_event_thread_control: "
12993 				    "cannot terminate event daemon thread");
12994 #endif
12995 				mutex_enter(&sata_event_mutex);
12996 				break;
12997 			}
12998 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
12999 			mutex_enter(&sata_event_mutex);
13000 		}
13001 		sata_event_thread_terminating = 0;
13002 	}
13003 	ASSERT(sata_event_thread_terminating == 0);
13004 	ASSERT(sata_event_thread_starting == 0);
13005 	mutex_exit(&sata_event_mutex);
13006 }
13007 
13008 
13009 /*
13010  * SATA HBA event notification function.
13011  * Events reported by SATA HBA drivers per HBA instance relate to a change in
13012  * a port and/or device state or a controller itself.
13013  * Events for different addresses/addr types cannot be combined.
13014  * A warning message is generated for each event type.
13015  * Events are not processed by this function, so only the
13016  * event flag(s)is set for an affected entity and the event thread is
13017  * waken up. Event daemon thread processes all events.
13018  *
13019  * NOTE: Since more than one event may be reported at the same time, one
13020  * cannot determine a sequence of events when opposite event are reported, eg.
13021  * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing
13022  * is taking precedence over reported events, i.e. may cause ignoring some
13023  * events.
13024  */
13025 #define	SATA_EVENT_MAX_MSG_LENGTH	79
13026 
13027 void
13028 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event)
13029 {
13030 	sata_hba_inst_t *sata_hba_inst = NULL;
13031 	sata_address_t *saddr;
13032 	sata_drive_info_t *sdinfo;
13033 	sata_port_stats_t *pstats;
13034 	int cport, pmport;
13035 	char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1];
13036 	char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1];
13037 	char *lcp;
13038 	static char *err_msg_evnt_1 =
13039 	    "sata_hba_event_notify: invalid port event 0x%x ";
13040 	static char *err_msg_evnt_2 =
13041 	    "sata_hba_event_notify: invalid device event 0x%x ";
13042 	int linkevent;
13043 
13044 	/*
13045 	 * There is a possibility that an event will be generated on HBA
13046 	 * that has not completed attachment or is detaching.
13047 	 * HBA driver should prevent this, but just in case it does not,
13048 	 * we need to ignore events for such HBA.
13049 	 */
13050 	mutex_enter(&sata_mutex);
13051 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13052 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13053 		if (SATA_DIP(sata_hba_inst) == dip)
13054 			if (sata_hba_inst->satahba_attached == 1)
13055 				break;
13056 	}
13057 	mutex_exit(&sata_mutex);
13058 	if (sata_hba_inst == NULL)
13059 		/* HBA not attached */
13060 		return;
13061 
13062 	ASSERT(sata_device != NULL);
13063 
13064 	/*
13065 	 * Validate address before - do not proceed with invalid address.
13066 	 */
13067 	saddr = &sata_device->satadev_addr;
13068 	if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst))
13069 		return;
13070 	if (saddr->qual == SATA_ADDR_PMPORT ||
13071 	    saddr->qual == SATA_ADDR_DPMPORT)
13072 		/* Port Multiplier not supported yet */
13073 		return;
13074 
13075 	cport = saddr->cport;
13076 	pmport = saddr->pmport;
13077 
13078 	buf1[0] = buf2[0] = '\0';
13079 
13080 	/*
13081 	 * Events refer to devices, ports and controllers - each has
13082 	 * unique address. Events for different addresses cannot be combined.
13083 	 */
13084 	if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) {
13085 
13086 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13087 
13088 		/* qualify this event(s) */
13089 		if ((event & SATA_EVNT_PORT_EVENTS) == 0) {
13090 			/* Invalid event for the device port */
13091 			(void) sprintf(buf2, err_msg_evnt_1,
13092 			    event & SATA_EVNT_PORT_EVENTS);
13093 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13094 			goto event_info;
13095 		}
13096 		if (saddr->qual == SATA_ADDR_CPORT) {
13097 			/* Controller's device port event */
13098 
13099 			(SATA_CPORT_INFO(sata_hba_inst, cport))->
13100 			    cport_event_flags |=
13101 			    event & SATA_EVNT_PORT_EVENTS;
13102 			pstats =
13103 			    &(SATA_CPORT_INFO(sata_hba_inst, cport))->
13104 			    cport_stats;
13105 		} else {
13106 			/* Port multiplier's device port event */
13107 			(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
13108 			    pmport_event_flags |=
13109 			    event & SATA_EVNT_PORT_EVENTS;
13110 			pstats =
13111 			    &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
13112 			    pmport_stats;
13113 		}
13114 
13115 		/*
13116 		 * Add to statistics and log the message. We have to do it
13117 		 * here rather than in the event daemon, because there may be
13118 		 * multiple events occuring before they are processed.
13119 		 */
13120 		linkevent = event &
13121 		    (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED);
13122 		if (linkevent) {
13123 			if (linkevent == (SATA_EVNT_LINK_LOST |
13124 			    SATA_EVNT_LINK_ESTABLISHED)) {
13125 				/* This is likely event combination */
13126 				(void) strlcat(buf1, "link lost/established, ",
13127 				    SATA_EVENT_MAX_MSG_LENGTH);
13128 
13129 				if (pstats->link_lost < 0xffffffffffffffffULL)
13130 					pstats->link_lost++;
13131 				if (pstats->link_established <
13132 				    0xffffffffffffffffULL)
13133 					pstats->link_established++;
13134 				linkevent = 0;
13135 			} else if (linkevent & SATA_EVNT_LINK_LOST) {
13136 				(void) strlcat(buf1, "link lost, ",
13137 				    SATA_EVENT_MAX_MSG_LENGTH);
13138 
13139 				if (pstats->link_lost < 0xffffffffffffffffULL)
13140 					pstats->link_lost++;
13141 			} else {
13142 				(void) strlcat(buf1, "link established, ",
13143 				    SATA_EVENT_MAX_MSG_LENGTH);
13144 				if (pstats->link_established <
13145 				    0xffffffffffffffffULL)
13146 					pstats->link_established++;
13147 			}
13148 		}
13149 		if (event & SATA_EVNT_DEVICE_ATTACHED) {
13150 			(void) strlcat(buf1, "device attached, ",
13151 			    SATA_EVENT_MAX_MSG_LENGTH);
13152 			if (pstats->device_attached < 0xffffffffffffffffULL)
13153 				pstats->device_attached++;
13154 		}
13155 		if (event & SATA_EVNT_DEVICE_DETACHED) {
13156 			(void) strlcat(buf1, "device detached, ",
13157 			    SATA_EVENT_MAX_MSG_LENGTH);
13158 			if (pstats->device_detached < 0xffffffffffffffffULL)
13159 				pstats->device_detached++;
13160 		}
13161 		if (event & SATA_EVNT_PWR_LEVEL_CHANGED) {
13162 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
13163 			    "port %d power level changed", cport);
13164 			if (pstats->port_pwr_changed < 0xffffffffffffffffULL)
13165 				pstats->port_pwr_changed++;
13166 		}
13167 
13168 		if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) {
13169 			/* There should be no other events for this address */
13170 			(void) sprintf(buf2, err_msg_evnt_1,
13171 			    event & ~SATA_EVNT_PORT_EVENTS);
13172 		}
13173 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13174 
13175 	} else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) {
13176 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13177 
13178 		/* qualify this event */
13179 		if ((event & SATA_EVNT_DEVICE_RESET) == 0) {
13180 			/* Invalid event for a device */
13181 			(void) sprintf(buf2, err_msg_evnt_2,
13182 			    event & SATA_EVNT_DEVICE_RESET);
13183 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13184 			goto event_info;
13185 		}
13186 		/* drive event */
13187 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
13188 		if (sdinfo != NULL) {
13189 			if (event & SATA_EVNT_DEVICE_RESET) {
13190 				(void) strlcat(buf1, "device reset, ",
13191 				    SATA_EVENT_MAX_MSG_LENGTH);
13192 				if (sdinfo->satadrv_stats.drive_reset <
13193 				    0xffffffffffffffffULL)
13194 					sdinfo->satadrv_stats.drive_reset++;
13195 				sdinfo->satadrv_event_flags |=
13196 				    SATA_EVNT_DEVICE_RESET;
13197 			}
13198 		}
13199 		if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) {
13200 			/* Invalid event for a device */
13201 			(void) sprintf(buf2, err_msg_evnt_2,
13202 			    event & ~SATA_EVNT_DRIVE_EVENTS);
13203 		}
13204 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13205 	} else {
13206 		if (saddr->qual != SATA_ADDR_NULL) {
13207 			/* Wrong address qualifier */
13208 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13209 			    "sata_hba_event_notify: invalid address 0x%x",
13210 			    *(uint32_t *)saddr));
13211 			return;
13212 		}
13213 		if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 ||
13214 		    (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) {
13215 			/* Invalid event for the controller */
13216 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13217 			    "sata_hba_event_notify: invalid event 0x%x for "
13218 			    "controller",
13219 			    event & SATA_EVNT_CONTROLLER_EVENTS));
13220 			return;
13221 		}
13222 		buf1[0] = '\0';
13223 		/* This may be a frequent and not interesting event */
13224 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
13225 		    "controller power level changed\n", NULL);
13226 
13227 		mutex_enter(&sata_hba_inst->satahba_mutex);
13228 		if (sata_hba_inst->satahba_stats.ctrl_pwr_change <
13229 		    0xffffffffffffffffULL)
13230 			sata_hba_inst->satahba_stats.ctrl_pwr_change++;
13231 
13232 		sata_hba_inst->satahba_event_flags |=
13233 		    SATA_EVNT_PWR_LEVEL_CHANGED;
13234 		mutex_exit(&sata_hba_inst->satahba_mutex);
13235 	}
13236 	/*
13237 	 * If we got here, there is something to do with this HBA
13238 	 * instance.
13239 	 */
13240 	mutex_enter(&sata_hba_inst->satahba_mutex);
13241 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
13242 	mutex_exit(&sata_hba_inst->satahba_mutex);
13243 	mutex_enter(&sata_mutex);
13244 	sata_event_pending |= SATA_EVNT_MAIN;	/* global event indicator */
13245 	mutex_exit(&sata_mutex);
13246 
13247 	/* Tickle event thread */
13248 	mutex_enter(&sata_event_mutex);
13249 	if (sata_event_thread_active == 0)
13250 		cv_signal(&sata_event_cv);
13251 	mutex_exit(&sata_event_mutex);
13252 
13253 event_info:
13254 	if (buf1[0] != '\0') {
13255 		lcp = strrchr(buf1, ',');
13256 		if (lcp != NULL)
13257 			*lcp = '\0';
13258 	}
13259 	if (saddr->qual == SATA_ADDR_CPORT ||
13260 	    saddr->qual == SATA_ADDR_DCPORT) {
13261 		if (buf1[0] != '\0') {
13262 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
13263 			    cport, buf1);
13264 		}
13265 		if (buf2[0] != '\0') {
13266 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
13267 			    cport, buf2);
13268 		}
13269 	} else if (saddr->qual == SATA_ADDR_PMPORT ||
13270 	    saddr->qual == SATA_ADDR_DPMPORT) {
13271 		if (buf1[0] != '\0') {
13272 			sata_log(sata_hba_inst, CE_NOTE,
13273 			    "port %d pmport %d: %s\n", cport, pmport, buf1);
13274 		}
13275 		if (buf2[0] != '\0') {
13276 			sata_log(sata_hba_inst, CE_NOTE,
13277 			    "port %d pmport %d: %s\n", cport, pmport, buf2);
13278 		}
13279 	}
13280 }
13281 
13282 
13283 /*
13284  * Event processing thread.
13285  * Arg is a pointer to the sata_hba_list pointer.
13286  * It is not really needed, because sata_hba_list is global and static
13287  */
13288 static void
13289 sata_event_daemon(void *arg)
13290 {
13291 #ifndef __lock_lint
13292 	_NOTE(ARGUNUSED(arg))
13293 #endif
13294 	sata_hba_inst_t *sata_hba_inst;
13295 	clock_t lbolt;
13296 
13297 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13298 	    "SATA event daemon started\n", NULL);
13299 loop:
13300 	/*
13301 	 * Process events here. Walk through all registered HBAs
13302 	 */
13303 	mutex_enter(&sata_mutex);
13304 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13305 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13306 		ASSERT(sata_hba_inst != NULL);
13307 		mutex_enter(&sata_hba_inst->satahba_mutex);
13308 		if (sata_hba_inst->satahba_attached != 1 ||
13309 		    (sata_hba_inst->satahba_event_flags &
13310 		    SATA_EVNT_SKIP) != 0) {
13311 			mutex_exit(&sata_hba_inst->satahba_mutex);
13312 			continue;
13313 		}
13314 		if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) {
13315 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP;
13316 			mutex_exit(&sata_hba_inst->satahba_mutex);
13317 			mutex_exit(&sata_mutex);
13318 			/* Got the controller with pending event */
13319 			sata_process_controller_events(sata_hba_inst);
13320 			/*
13321 			 * Since global mutex was released, there is a
13322 			 * possibility that HBA list has changed, so start
13323 			 * over from the top. Just processed controller
13324 			 * will be passed-over because of the SKIP flag.
13325 			 */
13326 			goto loop;
13327 		}
13328 		mutex_exit(&sata_hba_inst->satahba_mutex);
13329 	}
13330 	/* Clear SKIP flag in all controllers */
13331 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13332 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13333 		mutex_enter(&sata_hba_inst->satahba_mutex);
13334 		sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP;
13335 		mutex_exit(&sata_hba_inst->satahba_mutex);
13336 	}
13337 	mutex_exit(&sata_mutex);
13338 
13339 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13340 	    "SATA EVENT DAEMON suspending itself", NULL);
13341 
13342 #ifdef SATA_DEBUG
13343 	if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) {
13344 		sata_log(sata_hba_inst, CE_WARN,
13345 		    "SATA EVENTS PROCESSING DISABLED\n");
13346 		thread_exit(); /* Daemon will not run again */
13347 	}
13348 #endif
13349 	mutex_enter(&sata_event_mutex);
13350 	sata_event_thread_active = 0;
13351 	mutex_exit(&sata_event_mutex);
13352 	/*
13353 	 * Go to sleep/suspend itself and wake up either because new event or
13354 	 * wait timeout. Exit if there is a termination request (driver
13355 	 * unload).
13356 	 */
13357 	do {
13358 		lbolt = ddi_get_lbolt();
13359 		lbolt += drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME);
13360 		mutex_enter(&sata_event_mutex);
13361 		(void) cv_timedwait(&sata_event_cv, &sata_event_mutex, lbolt);
13362 
13363 		if (sata_event_thread_active != 0) {
13364 			mutex_exit(&sata_event_mutex);
13365 			continue;
13366 		}
13367 
13368 		/* Check if it is time to go away */
13369 		if (sata_event_thread_terminate == 1) {
13370 			/*
13371 			 * It is up to the thread setting above flag to make
13372 			 * sure that this thread is not killed prematurely.
13373 			 */
13374 			sata_event_thread_terminate = 0;
13375 			sata_event_thread = NULL;
13376 			mutex_exit(&sata_event_mutex);
13377 			SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13378 			    "SATA_EVENT_DAEMON_TERMINATING", NULL);
13379 			thread_exit();  { _NOTE(NOT_REACHED) }
13380 		}
13381 		mutex_exit(&sata_event_mutex);
13382 	} while (!(sata_event_pending & SATA_EVNT_MAIN));
13383 
13384 	mutex_enter(&sata_event_mutex);
13385 	sata_event_thread_active = 1;
13386 	mutex_exit(&sata_event_mutex);
13387 
13388 	mutex_enter(&sata_mutex);
13389 	sata_event_pending &= ~SATA_EVNT_MAIN;
13390 	mutex_exit(&sata_mutex);
13391 
13392 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13393 	    "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL);
13394 
13395 	goto loop;
13396 }
13397 
13398 /*
13399  * Specific HBA instance event processing.
13400  *
13401  * NOTE: At the moment, device event processing is limited to hard disks
13402  * only.
13403  * cports only are supported - no pmports.
13404  */
13405 static void
13406 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst)
13407 {
13408 	int ncport;
13409 	uint32_t event_flags;
13410 	sata_address_t *saddr;
13411 
13412 	SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst,
13413 	    "Processing controller %d event(s)",
13414 	    ddi_get_instance(SATA_DIP(sata_hba_inst)));
13415 
13416 	mutex_enter(&sata_hba_inst->satahba_mutex);
13417 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN;
13418 	event_flags = sata_hba_inst->satahba_event_flags;
13419 	mutex_exit(&sata_hba_inst->satahba_mutex);
13420 	/*
13421 	 * Process controller power change first
13422 	 * HERE
13423 	 */
13424 	if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED)
13425 		sata_process_cntrl_pwr_level_change(sata_hba_inst);
13426 
13427 	/*
13428 	 * Search through ports/devices to identify affected port/device.
13429 	 * We may have to process events for more than one port/device.
13430 	 */
13431 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
13432 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
13433 		event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))->
13434 		    cport_event_flags;
13435 		/* Check if port was locked by IOCTL processing */
13436 		if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) {
13437 			/*
13438 			 * We ignore port events because port is busy
13439 			 * with AP control processing. Set again
13440 			 * controller and main event flag, so that
13441 			 * events may be processed by the next daemon
13442 			 * run.
13443 			 */
13444 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
13445 			mutex_enter(&sata_hba_inst->satahba_mutex);
13446 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
13447 			mutex_exit(&sata_hba_inst->satahba_mutex);
13448 			mutex_enter(&sata_mutex);
13449 			sata_event_pending |= SATA_EVNT_MAIN;
13450 			mutex_exit(&sata_mutex);
13451 			SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst,
13452 			    "Event processing postponed until "
13453 			    "AP control processing completes",
13454 			    NULL);
13455 			/* Check other ports */
13456 			continue;
13457 		} else {
13458 			/*
13459 			 * Set BSY flag so that AP control would not
13460 			 * interfere with events processing for
13461 			 * this port.
13462 			 */
13463 			(SATA_CPORT_INFO(sata_hba_inst, ncport))->
13464 			    cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY;
13465 		}
13466 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
13467 
13468 		saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr;
13469 
13470 		if ((event_flags &
13471 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
13472 			/*
13473 			 * Got port event.
13474 			 * We need some hierarchy of event processing as they
13475 			 * are affecting each other:
13476 			 * 1. port failed
13477 			 * 2. device detached/attached
13478 			 * 3. link events - link events may trigger device
13479 			 *    detached or device attached events in some
13480 			 *    circumstances.
13481 			 * 4. port power level changed
13482 			 */
13483 			if (event_flags & SATA_EVNT_PORT_FAILED) {
13484 				sata_process_port_failed_event(sata_hba_inst,
13485 				    saddr);
13486 			}
13487 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
13488 				sata_process_device_detached(sata_hba_inst,
13489 				    saddr);
13490 			}
13491 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
13492 				sata_process_device_attached(sata_hba_inst,
13493 				    saddr);
13494 			}
13495 			if (event_flags &
13496 			    (SATA_EVNT_LINK_ESTABLISHED |
13497 			    SATA_EVNT_LINK_LOST)) {
13498 				sata_process_port_link_events(sata_hba_inst,
13499 				    saddr);
13500 			}
13501 			if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) {
13502 				sata_process_port_pwr_change(sata_hba_inst,
13503 				    saddr);
13504 			}
13505 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
13506 				sata_process_target_node_cleanup(
13507 				    sata_hba_inst, saddr);
13508 			}
13509 		}
13510 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) !=
13511 		    SATA_DTYPE_NONE) {
13512 			/* May have device event */
13513 			sata_process_device_reset(sata_hba_inst, saddr);
13514 		}
13515 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
13516 		/* Release PORT_BUSY flag */
13517 		(SATA_CPORT_INFO(sata_hba_inst, ncport))->
13518 		    cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
13519 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
13520 
13521 	} /* End of loop through the controller SATA ports */
13522 }
13523 
13524 /*
13525  * Process HBA power level change reported by HBA driver.
13526  * Not implemented at this time - event is ignored.
13527  */
13528 static void
13529 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst)
13530 {
13531 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
13532 	    "Processing controller power level change", NULL);
13533 
13534 	/* Ignoring it for now */
13535 	mutex_enter(&sata_hba_inst->satahba_mutex);
13536 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
13537 	mutex_exit(&sata_hba_inst->satahba_mutex);
13538 }
13539 
13540 /*
13541  * Process port power level change reported by HBA driver.
13542  * Not implemented at this time - event is ignored.
13543  */
13544 static void
13545 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst,
13546     sata_address_t *saddr)
13547 {
13548 	sata_cport_info_t *cportinfo;
13549 
13550 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
13551 	    "Processing port power level change", NULL);
13552 
13553 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
13554 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13555 	/* Reset event flag */
13556 	cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
13557 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13558 }
13559 
13560 /*
13561  * Process port failure reported by HBA driver.
13562  * cports support only - no pmports.
13563  */
13564 static void
13565 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst,
13566     sata_address_t *saddr)
13567 {
13568 	sata_cport_info_t *cportinfo;
13569 
13570 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
13571 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13572 	/* Reset event flag first */
13573 	cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED;
13574 	/* If the port is in SHUTDOWN or FAILED state, ignore this event. */
13575 	if ((cportinfo->cport_state &
13576 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) {
13577 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
13578 		    cport_mutex);
13579 		return;
13580 	}
13581 	/* Fail the port */
13582 	cportinfo->cport_state = SATA_PSTATE_FAILED;
13583 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13584 	sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport);
13585 }
13586 
13587 /*
13588  * Device Reset Event processing.
13589  * The seqeunce is managed by 3 stage flags:
13590  * - reset event reported,
13591  * - reset event being processed,
13592  * - request to clear device reset state.
13593  */
13594 static void
13595 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst,
13596     sata_address_t *saddr)
13597 {
13598 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
13599 	sata_drive_info_t *sdinfo;
13600 	sata_cport_info_t *cportinfo;
13601 	sata_device_t sata_device;
13602 	int rval;
13603 
13604 	/* We only care about host sata cport for now */
13605 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
13606 
13607 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13608 
13609 	/* If the port is in SHUTDOWN or FAILED state, ignore reset event. */
13610 	if ((cportinfo->cport_state &
13611 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
13612 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
13613 		    cport_mutex);
13614 		return;
13615 	}
13616 
13617 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) &
13618 	    SATA_VALID_DEV_TYPE) == 0) {
13619 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
13620 		    cport_mutex);
13621 		return;
13622 	}
13623 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
13624 	if (sdinfo == NULL) {
13625 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
13626 		    cport_mutex);
13627 		return;
13628 	}
13629 
13630 	if ((sdinfo->satadrv_event_flags &
13631 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
13632 		/* Nothing to do */
13633 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
13634 		    cport_mutex);
13635 		return;
13636 	}
13637 #ifdef SATA_DEBUG
13638 	if ((sdinfo->satadrv_event_flags &
13639 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
13640 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
13641 		/* Something is weird - new device reset event */
13642 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
13643 		    "Overlapping device reset events!", NULL);
13644 	}
13645 #endif
13646 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
13647 	    "Processing port %d device reset", saddr->cport);
13648 
13649 	/* Clear event flag */
13650 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
13651 
13652 	/* It seems that we always need to check the port state first */
13653 	sata_device.satadev_rev = SATA_DEVICE_REV;
13654 	sata_device.satadev_addr = *saddr;
13655 	/*
13656 	 * We have to exit mutex, because the HBA probe port function may
13657 	 * block on its own mutex.
13658 	 */
13659 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13660 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
13661 	    (SATA_DIP(sata_hba_inst), &sata_device);
13662 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13663 	sata_update_port_info(sata_hba_inst, &sata_device);
13664 	if (rval != SATA_SUCCESS) {
13665 		/* Something went wrong? Fail the port */
13666 		cportinfo->cport_state = SATA_PSTATE_FAILED;
13667 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
13668 		    cport_mutex);
13669 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13670 		    "SATA port %d probing failed",
13671 		    saddr->cport));
13672 		return;
13673 	}
13674 	if ((sata_device.satadev_scr.sstatus  &
13675 	    SATA_PORT_DEVLINK_UP_MASK) !=
13676 	    SATA_PORT_DEVLINK_UP ||
13677 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
13678 		/*
13679 		 * No device to process, anymore. Some other event processing
13680 		 * would or have already performed port info cleanup.
13681 		 * To be safe (HBA may need it), request clearing device
13682 		 * reset condition.
13683 		 */
13684 		sdinfo->satadrv_event_flags = 0;
13685 		sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
13686 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
13687 		    cport_mutex);
13688 		return;
13689 	}
13690 
13691 	/* Mark device reset processing as active */
13692 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
13693 
13694 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
13695 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13696 
13697 	if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) ==
13698 	    SATA_FAILURE) {
13699 		/*
13700 		 * Restoring drive setting failed.
13701 		 * Probe the port first, to check if the port state has changed
13702 		 */
13703 		sata_device.satadev_rev = SATA_DEVICE_REV;
13704 		sata_device.satadev_addr = *saddr;
13705 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
13706 		/* probe port */
13707 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
13708 		    (SATA_DIP(sata_hba_inst), &sata_device);
13709 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
13710 		    cport_mutex);
13711 		if (rval == SATA_SUCCESS &&
13712 		    (sata_device.satadev_state &
13713 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
13714 		    (sata_device.satadev_scr.sstatus  &
13715 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
13716 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
13717 			/*
13718 			 * We may retry this a bit later - in-process reset
13719 			 * condition should be already set.
13720 			 */
13721 			if ((cportinfo->cport_dev_type &
13722 			    SATA_VALID_DEV_TYPE) != 0 &&
13723 			    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
13724 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
13725 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
13726 				    saddr->cport)->cport_mutex);
13727 				mutex_enter(&sata_hba_inst->satahba_mutex);
13728 				sata_hba_inst->satahba_event_flags |=
13729 				    SATA_EVNT_MAIN;
13730 				mutex_exit(&sata_hba_inst->satahba_mutex);
13731 				mutex_enter(&sata_mutex);
13732 				sata_event_pending |= SATA_EVNT_MAIN;
13733 				mutex_exit(&sata_mutex);
13734 				return;
13735 			}
13736 		} else {
13737 			/*
13738 			 * No point of retrying - some other event processing
13739 			 * would or already did port info cleanup.
13740 			 * To be safe (HBA may need it),
13741 			 * request clearing device reset condition.
13742 			 */
13743 			sdinfo->satadrv_event_flags = 0;
13744 			sdinfo->satadrv_event_flags |=
13745 			    SATA_EVNT_CLEAR_DEVICE_RESET;
13746 		}
13747 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
13748 		    cport_mutex);
13749 		return;
13750 	}
13751 
13752 	/*
13753 	 * Raise the flag indicating that the next sata command could
13754 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
13755 	 * reset is reported.
13756 	 */
13757 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13758 	if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0 &&
13759 	    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
13760 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
13761 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
13762 		sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
13763 	}
13764 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13765 }
13766 
13767 
13768 /*
13769  * Port Link Events processing.
13770  * Every link established event may involve device reset (due to
13771  * COMRESET signal, equivalent of the hard reset) so arbitrarily
13772  * set device reset event for an attached device (if any).
13773  * If the port is in SHUTDOWN or FAILED state, ignore link events.
13774  *
13775  * The link established event processing varies, depending on the state
13776  * of the target node, HBA hotplugging capabilities, state of the port.
13777  * If the link is not active, the link established event is ignored.
13778  * If HBA cannot detect device attachment and there is no target node,
13779  * the link established event triggers device attach event processing.
13780  * Else, link established event triggers device reset event processing.
13781  *
13782  * The link lost event processing varies, depending on a HBA hotplugging
13783  * capability and the state of the port (link active or not active).
13784  * If the link is active, the lost link event is ignored.
13785  * If HBA cannot detect device removal, the lost link event triggers
13786  * device detached event processing after link lost timeout.
13787  * Else, the event is ignored.
13788  *
13789  * NOTE: Only cports are processed for now, i.e. no port multiplier ports
13790  */
13791 static void
13792 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst,
13793     sata_address_t *saddr)
13794 {
13795 	sata_device_t sata_device;
13796 	sata_cport_info_t *cportinfo;
13797 	sata_drive_info_t *sdinfo;
13798 	uint32_t event_flags;
13799 	int rval;
13800 
13801 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
13802 	    "Processing port %d link event(s)", saddr->cport);
13803 
13804 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
13805 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13806 	event_flags = cportinfo->cport_event_flags;
13807 
13808 	/* Reset event flags first */
13809 	cportinfo->cport_event_flags &=
13810 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
13811 
13812 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
13813 	if ((cportinfo->cport_state &
13814 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
13815 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
13816 		    cport_mutex);
13817 		return;
13818 	}
13819 
13820 	/*
13821 	 * For the sanity sake get current port state.
13822 	 * Set device address only. Other sata_device fields should be
13823 	 * set by HBA driver.
13824 	 */
13825 	sata_device.satadev_rev = SATA_DEVICE_REV;
13826 	sata_device.satadev_addr = *saddr;
13827 	/*
13828 	 * We have to exit mutex, because the HBA probe port function may
13829 	 * block on its own mutex.
13830 	 */
13831 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13832 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
13833 	    (SATA_DIP(sata_hba_inst), &sata_device);
13834 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13835 	sata_update_port_info(sata_hba_inst, &sata_device);
13836 	if (rval != SATA_SUCCESS) {
13837 		/* Something went wrong? Fail the port */
13838 		cportinfo->cport_state = SATA_PSTATE_FAILED;
13839 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
13840 		    cport_mutex);
13841 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13842 		    "SATA port %d probing failed",
13843 		    saddr->cport));
13844 		/*
13845 		 * We may want to release device info structure, but
13846 		 * it is not necessary.
13847 		 */
13848 		return;
13849 	} else {
13850 		/* port probed successfully */
13851 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
13852 	}
13853 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
13854 
13855 		if ((sata_device.satadev_scr.sstatus &
13856 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
13857 			/* Ignore event */
13858 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
13859 			    "Ignoring port %d link established event - "
13860 			    "link down",
13861 			    saddr->cport);
13862 			goto linklost;
13863 		}
13864 
13865 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
13866 		    "Processing port %d link established event",
13867 		    saddr->cport);
13868 
13869 		/*
13870 		 * For the sanity sake check if a device is attached - check
13871 		 * return state of a port probing.
13872 		 */
13873 		if (sata_device.satadev_type != SATA_DTYPE_NONE &&
13874 		    sata_device.satadev_type != SATA_DTYPE_PMULT) {
13875 			/*
13876 			 * HBA port probe indicated that there is a device
13877 			 * attached. Check if the framework had device info
13878 			 * structure attached for this device.
13879 			 */
13880 			if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
13881 				ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) !=
13882 				    NULL);
13883 
13884 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
13885 				if ((sdinfo->satadrv_type &
13886 				    SATA_VALID_DEV_TYPE) != 0) {
13887 					/*
13888 					 * Dev info structure is present.
13889 					 * If dev_type is set to known type in
13890 					 * the framework's drive info struct
13891 					 * then the device existed before and
13892 					 * the link was probably lost
13893 					 * momentarily - in such case
13894 					 * we may want to check device
13895 					 * identity.
13896 					 * Identity check is not supported now.
13897 					 *
13898 					 * Link established event
13899 					 * triggers device reset event.
13900 					 */
13901 					(SATA_CPORTINFO_DRV_INFO(cportinfo))->
13902 					    satadrv_event_flags |=
13903 					    SATA_EVNT_DEVICE_RESET;
13904 				}
13905 			} else if (cportinfo->cport_dev_type ==
13906 			    SATA_DTYPE_NONE) {
13907 				/*
13908 				 * We got new device attached! If HBA does not
13909 				 * generate device attached events, trigger it
13910 				 * here.
13911 				 */
13912 				if (!(SATA_FEATURES(sata_hba_inst) &
13913 				    SATA_CTLF_HOTPLUG)) {
13914 					cportinfo->cport_event_flags |=
13915 					    SATA_EVNT_DEVICE_ATTACHED;
13916 				}
13917 			}
13918 			/* Reset link lost timeout */
13919 			cportinfo->cport_link_lost_time = 0;
13920 		}
13921 	}
13922 linklost:
13923 	if (event_flags & SATA_EVNT_LINK_LOST) {
13924 		if ((sata_device.satadev_scr.sstatus &
13925 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
13926 			/* Ignore event */
13927 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
13928 			    "Ignoring port %d link lost event - link is up",
13929 			    saddr->cport);
13930 			goto done;
13931 		}
13932 #ifdef SATA_DEBUG
13933 		if (cportinfo->cport_link_lost_time == 0) {
13934 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
13935 			    "Processing port %d link lost event",
13936 			    saddr->cport);
13937 		}
13938 #endif
13939 		/*
13940 		 * When HBA cannot generate device attached/detached events,
13941 		 * we need to track link lost time and eventually generate
13942 		 * device detach event.
13943 		 */
13944 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
13945 			/* We are tracking link lost time */
13946 			if (cportinfo->cport_link_lost_time == 0) {
13947 				/* save current time (lbolt value) */
13948 				cportinfo->cport_link_lost_time =
13949 				    ddi_get_lbolt();
13950 				/* just keep link lost event */
13951 				cportinfo->cport_event_flags |=
13952 				    SATA_EVNT_LINK_LOST;
13953 			} else {
13954 				clock_t cur_time = ddi_get_lbolt();
13955 				if ((cur_time -
13956 				    cportinfo->cport_link_lost_time) >=
13957 				    drv_usectohz(
13958 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
13959 					/* trigger device detach event */
13960 					cportinfo->cport_event_flags |=
13961 					    SATA_EVNT_DEVICE_DETACHED;
13962 					cportinfo->cport_link_lost_time = 0;
13963 					SATADBG1(SATA_DBG_EVENTS,
13964 					    sata_hba_inst,
13965 					    "Triggering port %d "
13966 					    "device detached event",
13967 					    saddr->cport);
13968 				} else {
13969 					/* keep link lost event */
13970 					cportinfo->cport_event_flags |=
13971 					    SATA_EVNT_LINK_LOST;
13972 				}
13973 			}
13974 		}
13975 		/*
13976 		 * We could change port state to disable/delay access to
13977 		 * the attached device until the link is recovered.
13978 		 */
13979 	}
13980 done:
13981 	event_flags = cportinfo->cport_event_flags;
13982 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
13983 	if (event_flags != 0) {
13984 		mutex_enter(&sata_hba_inst->satahba_mutex);
13985 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
13986 		mutex_exit(&sata_hba_inst->satahba_mutex);
13987 		mutex_enter(&sata_mutex);
13988 		sata_event_pending |= SATA_EVNT_MAIN;
13989 		mutex_exit(&sata_mutex);
13990 	}
13991 }
13992 
13993 /*
13994  * Device Detached Event processing.
13995  * Port is probed to find if a device is really gone. If so,
13996  * the device info structure is detached from the SATA port info structure
13997  * and released.
13998  * Port status is updated.
13999  *
14000  * NOTE: Process cports event only, no port multiplier ports.
14001  */
14002 static void
14003 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst,
14004     sata_address_t *saddr)
14005 {
14006 	sata_cport_info_t *cportinfo;
14007 	sata_drive_info_t *sdevinfo;
14008 	sata_device_t sata_device;
14009 	dev_info_t *tdip;
14010 	int rval;
14011 
14012 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14013 	    "Processing port %d device detached", saddr->cport);
14014 
14015 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14016 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14017 	/* Clear event flag */
14018 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
14019 
14020 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
14021 	if ((cportinfo->cport_state &
14022 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14023 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14024 		    cport_mutex);
14025 		return;
14026 	}
14027 	/* For sanity, re-probe the port */
14028 	sata_device.satadev_rev = SATA_DEVICE_REV;
14029 	sata_device.satadev_addr = *saddr;
14030 
14031 	/*
14032 	 * We have to exit mutex, because the HBA probe port function may
14033 	 * block on its own mutex.
14034 	 */
14035 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14036 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14037 	    (SATA_DIP(sata_hba_inst), &sata_device);
14038 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14039 	sata_update_port_info(sata_hba_inst, &sata_device);
14040 	if (rval != SATA_SUCCESS) {
14041 		/* Something went wrong? Fail the port */
14042 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14043 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14044 		    cport_mutex);
14045 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14046 		    "SATA port %d probing failed",
14047 		    saddr->cport));
14048 		/*
14049 		 * We may want to release device info structure, but
14050 		 * it is not necessary.
14051 		 */
14052 		return;
14053 	} else {
14054 		/* port probed successfully */
14055 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14056 	}
14057 	/*
14058 	 * Check if a device is still attached. For sanity, check also
14059 	 * link status - if no link, there is no device.
14060 	 */
14061 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
14062 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
14063 	    SATA_DTYPE_NONE) {
14064 		/*
14065 		 * Device is still attached - ignore detach event.
14066 		 */
14067 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14068 		    cport_mutex);
14069 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14070 		    "Ignoring detach - device still attached to port %d",
14071 		    sata_device.satadev_addr.cport);
14072 		return;
14073 	}
14074 	/*
14075 	 * We need to detach and release device info structure here
14076 	 */
14077 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14078 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14079 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14080 		(void) kmem_free((void *)sdevinfo,
14081 		    sizeof (sata_drive_info_t));
14082 	}
14083 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14084 	/*
14085 	 * Device cannot be reached anymore, even if the target node may be
14086 	 * still present.
14087 	 */
14088 
14089 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14090 	sata_log(sata_hba_inst, CE_WARN, "SATA device detached at port %d",
14091 	    sata_device.satadev_addr.cport);
14092 
14093 	/*
14094 	 * Try to offline a device and remove target node if it still exists
14095 	 */
14096 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
14097 	if (tdip != NULL) {
14098 		/*
14099 		 * Target node exists.  Unconfigure device then remove
14100 		 * the target node (one ndi operation).
14101 		 */
14102 		if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
14103 			/*
14104 			 * PROBLEM - no device, but target node remained
14105 			 * This happens when the file was open or node was
14106 			 * waiting for resources.
14107 			 */
14108 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14109 			    "sata_process_device_detached: "
14110 			    "Failed to remove target node for "
14111 			    "detached SATA device."));
14112 			/*
14113 			 * Set target node state to DEVI_DEVICE_REMOVED.
14114 			 * But re-check first that the node still exists.
14115 			 */
14116 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
14117 			    saddr->cport);
14118 			if (tdip != NULL) {
14119 				sata_set_device_removed(tdip);
14120 				/*
14121 				 * Instruct event daemon to retry the
14122 				 * cleanup later.
14123 				 */
14124 				sata_set_target_node_cleanup(sata_hba_inst,
14125 				    saddr->cport);
14126 			}
14127 		}
14128 	}
14129 	/*
14130 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14131 	 * with the hint: SE_HINT_REMOVE
14132 	 */
14133 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
14134 }
14135 
14136 
14137 /*
14138  * Device Attached Event processing.
14139  * Port state is checked to verify that a device is really attached. If so,
14140  * the device info structure is created and attached to the SATA port info
14141  * structure.
14142  *
14143  * If attached device cannot be identified or set-up, the retry for the
14144  * attach processing is set-up. Subsequent daemon run would try again to
14145  * identify the device, until the time limit is reached
14146  * (SATA_DEV_IDENTIFY_TIMEOUT).
14147  *
14148  * This function cannot be called in interrupt context (it may sleep).
14149  *
14150  * NOTE: Process cports event only, no port multiplier ports.
14151  */
14152 static void
14153 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst,
14154     sata_address_t *saddr)
14155 {
14156 	sata_cport_info_t *cportinfo;
14157 	sata_drive_info_t *sdevinfo;
14158 	sata_device_t sata_device;
14159 	dev_info_t *tdip;
14160 	uint32_t event_flags;
14161 	int rval;
14162 
14163 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14164 	    "Processing port %d device attached", saddr->cport);
14165 
14166 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14167 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14168 
14169 	/* Clear attach event flag first */
14170 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
14171 
14172 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
14173 	if ((cportinfo->cport_state &
14174 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14175 		cportinfo->cport_dev_attach_time = 0;
14176 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14177 		    cport_mutex);
14178 		return;
14179 	}
14180 
14181 	/*
14182 	 * If the sata_drive_info structure is found attached to the port info,
14183 	 * despite the fact the device was removed and now it is re-attached,
14184 	 * the old drive info structure was not removed.
14185 	 * Arbitrarily release device info structure.
14186 	 */
14187 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14188 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14189 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14190 		(void) kmem_free((void *)sdevinfo,
14191 		    sizeof (sata_drive_info_t));
14192 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14193 		    "Arbitrarily detaching old device info.", NULL);
14194 	}
14195 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14196 
14197 	/* For sanity, re-probe the port */
14198 	sata_device.satadev_rev = SATA_DEVICE_REV;
14199 	sata_device.satadev_addr = *saddr;
14200 
14201 	/*
14202 	 * We have to exit mutex, because the HBA probe port function may
14203 	 * block on its own mutex.
14204 	 */
14205 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14206 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14207 	    (SATA_DIP(sata_hba_inst), &sata_device);
14208 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14209 	sata_update_port_info(sata_hba_inst, &sata_device);
14210 	if (rval != SATA_SUCCESS) {
14211 		/* Something went wrong? Fail the port */
14212 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14213 		cportinfo->cport_dev_attach_time = 0;
14214 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14215 		    cport_mutex);
14216 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14217 		    "SATA port %d probing failed",
14218 		    saddr->cport));
14219 		return;
14220 	} else {
14221 		/* port probed successfully */
14222 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14223 	}
14224 	/*
14225 	 * Check if a device is still attached. For sanity, check also
14226 	 * link status - if no link, there is no device.
14227 	 */
14228 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
14229 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
14230 	    SATA_DTYPE_NONE) {
14231 		/*
14232 		 * No device - ignore attach event.
14233 		 */
14234 		cportinfo->cport_dev_attach_time = 0;
14235 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14236 		    cport_mutex);
14237 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14238 		    "Ignoring attach - no device connected to port %d",
14239 		    sata_device.satadev_addr.cport);
14240 		return;
14241 	}
14242 
14243 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14244 	/*
14245 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14246 	 * with the hint: SE_HINT_INSERT
14247 	 */
14248 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
14249 
14250 	/*
14251 	 * Port reprobing will take care of the creation of the device
14252 	 * info structure and determination of the device type.
14253 	 */
14254 	sata_device.satadev_addr = *saddr;
14255 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
14256 	    SATA_DEV_IDENTIFY_NORETRY);
14257 
14258 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14259 	    cport_mutex);
14260 	if ((cportinfo->cport_state & SATA_STATE_READY) &&
14261 	    (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) {
14262 		/* Some device is attached to the port */
14263 		if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) {
14264 			/*
14265 			 * A device was not successfully attached.
14266 			 * Track retry time for device identification.
14267 			 */
14268 			if (cportinfo->cport_dev_attach_time != 0) {
14269 				clock_t cur_time = ddi_get_lbolt();
14270 				/*
14271 				 * If the retry time limit was not exceeded,
14272 				 * reinstate attach event.
14273 				 */
14274 				if ((cur_time -
14275 				    cportinfo->cport_dev_attach_time) <
14276 				    drv_usectohz(
14277 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
14278 					/* OK, restore attach event */
14279 					cportinfo->cport_event_flags |=
14280 					    SATA_EVNT_DEVICE_ATTACHED;
14281 				} else {
14282 					/* Timeout - cannot identify device */
14283 					cportinfo->cport_dev_attach_time = 0;
14284 					sata_log(sata_hba_inst,
14285 					    CE_WARN,
14286 					    "Cannot identify SATA device "
14287 					    "at port %d - device will not be "
14288 					    "attached.",
14289 					    saddr->cport);
14290 				}
14291 			} else {
14292 				/*
14293 				 * Start tracking time for device
14294 				 * identification.
14295 				 * Save current time (lbolt value).
14296 				 */
14297 				cportinfo->cport_dev_attach_time =
14298 				    ddi_get_lbolt();
14299 				/* Restore attach event */
14300 				cportinfo->cport_event_flags |=
14301 				    SATA_EVNT_DEVICE_ATTACHED;
14302 			}
14303 		} else {
14304 			/*
14305 			 * If device was successfully attached, an explicit
14306 			 * 'configure' command will be needed to configure it.
14307 			 * Log the message indicating that a device
14308 			 * was attached.
14309 			 */
14310 			cportinfo->cport_dev_attach_time = 0;
14311 			sata_log(sata_hba_inst, CE_WARN,
14312 			    "SATA device detected at port %d", saddr->cport);
14313 
14314 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14315 				sata_drive_info_t new_sdinfo;
14316 
14317 				/* Log device info data */
14318 				new_sdinfo = *(SATA_CPORTINFO_DRV_INFO(
14319 				    cportinfo));
14320 				sata_show_drive_info(sata_hba_inst,
14321 				    &new_sdinfo);
14322 			}
14323 
14324 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14325 			    saddr->cport)->cport_mutex);
14326 
14327 			/*
14328 			 * Make sure that there is no target node for that
14329 			 * device. If so, release it. It should not happen,
14330 			 * unless we had problem removing the node when
14331 			 * device was detached.
14332 			 */
14333 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
14334 			    saddr->cport);
14335 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14336 			    saddr->cport)->cport_mutex);
14337 			if (tdip != NULL) {
14338 
14339 #ifdef SATA_DEBUG
14340 				if ((cportinfo->cport_event_flags &
14341 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
14342 					sata_log(sata_hba_inst, CE_WARN,
14343 					    "sata_process_device_attached: "
14344 					    "old device target node exists!");
14345 #endif
14346 				/*
14347 				 * target node exists - try to unconfigure
14348 				 * device and remove the node.
14349 				 */
14350 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14351 				    saddr->cport)->cport_mutex);
14352 				rval = ndi_devi_offline(tdip,
14353 				    NDI_DEVI_REMOVE);
14354 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14355 				    saddr->cport)->cport_mutex);
14356 
14357 				if (rval == NDI_SUCCESS) {
14358 					cportinfo->cport_event_flags &=
14359 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
14360 					cportinfo->cport_tgtnode_clean = B_TRUE;
14361 				} else {
14362 					/*
14363 					 * PROBLEM - the target node remained
14364 					 * and it belongs to a previously
14365 					 * attached device.
14366 					 * This happens when the file was open
14367 					 * or the node was waiting for
14368 					 * resources at the time the
14369 					 * associated device was removed.
14370 					 * Instruct event daemon to retry the
14371 					 * cleanup later.
14372 					 */
14373 					sata_log(sata_hba_inst,
14374 					    CE_WARN,
14375 					    "Application(s) accessing "
14376 					    "previously attached SATA "
14377 					    "device have to release "
14378 					    "it before newly inserted "
14379 					    "device can be made accessible.",
14380 					    saddr->cport);
14381 					cportinfo->cport_event_flags |=
14382 					    SATA_EVNT_TARGET_NODE_CLEANUP;
14383 					cportinfo->cport_tgtnode_clean =
14384 					    B_FALSE;
14385 				}
14386 			}
14387 
14388 		}
14389 	} else {
14390 		cportinfo->cport_dev_attach_time = 0;
14391 	}
14392 
14393 	event_flags = cportinfo->cport_event_flags;
14394 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14395 	if (event_flags != 0) {
14396 		mutex_enter(&sata_hba_inst->satahba_mutex);
14397 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14398 		mutex_exit(&sata_hba_inst->satahba_mutex);
14399 		mutex_enter(&sata_mutex);
14400 		sata_event_pending |= SATA_EVNT_MAIN;
14401 		mutex_exit(&sata_mutex);
14402 	}
14403 }
14404 
14405 
14406 /*
14407  * Device Target Node Cleanup Event processing.
14408  * If the target node associated with a sata port device is in
14409  * DEVI_DEVICE_REMOVED state, an attempt is made to remove it.
14410  * If the target node cannot be removed, the event flag is left intact,
14411  * so that event daemon may re-run this function later.
14412  *
14413  * This function cannot be called in interrupt context (it may sleep).
14414  *
14415  * NOTE: Processes cport events only, not port multiplier ports.
14416  */
14417 static void
14418 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
14419     sata_address_t *saddr)
14420 {
14421 	sata_cport_info_t *cportinfo;
14422 	dev_info_t *tdip;
14423 
14424 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14425 	    "Processing port %d device target node cleanup", saddr->cport);
14426 
14427 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14428 
14429 	/*
14430 	 * Check if there is target node for that device and it is in the
14431 	 * DEVI_DEVICE_REMOVED state. If so, release it.
14432 	 */
14433 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
14434 	if (tdip != NULL) {
14435 		/*
14436 		 * target node exists - check if it is target node of
14437 		 * a removed device.
14438 		 */
14439 		if (sata_check_device_removed(tdip) == B_TRUE) {
14440 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14441 			    "sata_process_target_node_cleanup: "
14442 			    "old device target node exists!", NULL);
14443 			/*
14444 			 * Unconfigure and remove the target node
14445 			 */
14446 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) ==
14447 			    NDI_SUCCESS) {
14448 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14449 				    saddr->cport)->cport_mutex);
14450 				cportinfo->cport_event_flags &=
14451 				    ~SATA_EVNT_TARGET_NODE_CLEANUP;
14452 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14453 				    saddr->cport)->cport_mutex);
14454 				return;
14455 			}
14456 			/*
14457 			 * Event daemon will retry the cleanup later.
14458 			 */
14459 			mutex_enter(&sata_hba_inst->satahba_mutex);
14460 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14461 			mutex_exit(&sata_hba_inst->satahba_mutex);
14462 			mutex_enter(&sata_mutex);
14463 			sata_event_pending |= SATA_EVNT_MAIN;
14464 			mutex_exit(&sata_mutex);
14465 		}
14466 	} else {
14467 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14468 		    saddr->cport)->cport_mutex);
14469 		cportinfo->cport_event_flags &=
14470 		    ~SATA_EVNT_TARGET_NODE_CLEANUP;
14471 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14472 		    saddr->cport)->cport_mutex);
14473 	}
14474 }
14475 
14476 static void
14477 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr,
14478     int hint)
14479 {
14480 	char ap[MAXPATHLEN];
14481 	nvlist_t *ev_attr_list = NULL;
14482 	int err;
14483 
14484 	/* Allocate and build sysevent attribute list */
14485 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP);
14486 	if (err != 0) {
14487 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14488 		    "sata_gen_sysevent: "
14489 		    "cannot allocate memory for sysevent attributes\n"));
14490 		return;
14491 	}
14492 	/* Add hint attribute */
14493 	err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint));
14494 	if (err != 0) {
14495 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14496 		    "sata_gen_sysevent: "
14497 		    "failed to add DR_HINT attr for sysevent"));
14498 		nvlist_free(ev_attr_list);
14499 		return;
14500 	}
14501 	/*
14502 	 * Add AP attribute.
14503 	 * Get controller pathname and convert it into AP pathname by adding
14504 	 * a target number.
14505 	 */
14506 	(void) snprintf(ap, MAXPATHLEN, "/devices");
14507 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap));
14508 	(void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d",
14509 	    SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual));
14510 
14511 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap);
14512 	if (err != 0) {
14513 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14514 		    "sata_gen_sysevent: "
14515 		    "failed to add DR_AP_ID attr for sysevent"));
14516 		nvlist_free(ev_attr_list);
14517 		return;
14518 	}
14519 
14520 	/* Generate/log sysevent */
14521 	err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR,
14522 	    ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP);
14523 	if (err != DDI_SUCCESS) {
14524 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14525 		    "sata_gen_sysevent: "
14526 		    "cannot log sysevent, err code %x\n", err));
14527 	}
14528 
14529 	nvlist_free(ev_attr_list);
14530 }
14531 
14532 
14533 
14534 
14535 /*
14536  * Set DEVI_DEVICE_REMOVED state in the SATA device target node.
14537  */
14538 static void
14539 sata_set_device_removed(dev_info_t *tdip)
14540 {
14541 	int circ;
14542 
14543 	ASSERT(tdip != NULL);
14544 
14545 	ndi_devi_enter(tdip, &circ);
14546 	mutex_enter(&DEVI(tdip)->devi_lock);
14547 	DEVI_SET_DEVICE_REMOVED(tdip);
14548 	mutex_exit(&DEVI(tdip)->devi_lock);
14549 	ndi_devi_exit(tdip, circ);
14550 }
14551 
14552 
14553 /*
14554  * Set internal event instructing event daemon to try
14555  * to perform the target node cleanup.
14556  */
14557 static void
14558 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst, int cport)
14559 {
14560 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14561 	SATA_CPORT_EVENT_FLAGS(sata_hba_inst, cport) |=
14562 	    SATA_EVNT_TARGET_NODE_CLEANUP;
14563 	SATA_CPORT_INFO(sata_hba_inst, cport)->cport_tgtnode_clean = B_FALSE;
14564 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
14565 	mutex_enter(&sata_hba_inst->satahba_mutex);
14566 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14567 	mutex_exit(&sata_hba_inst->satahba_mutex);
14568 	mutex_enter(&sata_mutex);
14569 	sata_event_pending |= SATA_EVNT_MAIN;
14570 	mutex_exit(&sata_mutex);
14571 }
14572 
14573 
14574 /*
14575  * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state,
14576  * i.e. check if the target node state indicates that it belongs to a removed
14577  * device.
14578  *
14579  * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state,
14580  * B_FALSE otherwise.
14581  *
14582  * NOTE: No port multiplier support.
14583  */
14584 static boolean_t
14585 sata_check_device_removed(dev_info_t *tdip)
14586 {
14587 	ASSERT(tdip != NULL);
14588 
14589 	if (DEVI_IS_DEVICE_REMOVED(tdip))
14590 		return (B_TRUE);
14591 	else
14592 		return (B_FALSE);
14593 }
14594