xref: /illumos-gate/usr/src/uts/common/io/sata/impl/sata.c (revision 9af3851a3a831b4de34b42482c22351e14f33f16)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * SATA Framework
31  * Generic SATA Host Adapter Implementation
32  */
33 
34 #include <sys/conf.h>
35 #include <sys/file.h>
36 #include <sys/ddi.h>
37 #include <sys/sunddi.h>
38 #include <sys/modctl.h>
39 #include <sys/cmn_err.h>
40 #include <sys/errno.h>
41 #include <sys/thread.h>
42 #include <sys/kstat.h>
43 #include <sys/note.h>
44 #include <sys/sysevent.h>
45 #include <sys/sysevent/eventdefs.h>
46 #include <sys/sysevent/dr.h>
47 #include <sys/taskq.h>
48 
49 #include <sys/sata/impl/sata.h>
50 #include <sys/sata/sata_hba.h>
51 #include <sys/sata/sata_defs.h>
52 #include <sys/sata/sata_cfgadm.h>
53 
54 /* Debug flags - defined in sata.h */
55 int	sata_debug_flags = 0;
56 int	sata_msg = 0;
57 
58 /*
59  * Flags enabling selected SATA HBA framework functionality
60  */
61 #define	SATA_ENABLE_QUEUING		1
62 #define	SATA_ENABLE_NCQ			2
63 #define	SATA_ENABLE_PROCESS_EVENTS	4
64 int sata_func_enable =
65 	SATA_ENABLE_PROCESS_EVENTS | SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ;
66 
67 /*
68  * Global variable setting default maximum queue depth (NCQ or TCQ)
69  * Note:minimum queue depth is 1
70  */
71 int sata_max_queue_depth = SATA_MAX_QUEUE_DEPTH; /* max NCQ/TCQ queue depth */
72 
73 /*
74  * Currently used default NCQ/TCQ queue depth. It is set-up during the driver
75  * initialization, using value from sata_max_queue_depth
76  * It is adjusted to minimum supported by the controller and by the device,
77  * if queueing is enabled.
78  */
79 static	int sata_current_max_qdepth;
80 
81 /*
82  * Global variable determining the default behavior after device hotpluggin.
83  * If non-zero, the hotplugged device is onlined (if possible) without explicit
84  * IOCTL request (AP_CONFIGURE).
85  * If zero, hotplugged device is identified, but not onlined.
86  * Enabling (AP_CONNECT) device port with an attached device does not result
87  * in device onlining regardless of the flag setting
88  */
89 int sata_auto_online = 0;
90 
91 #ifdef SATA_DEBUG
92 
93 #define	SATA_LOG_D(args)	sata_log args
94 uint64_t mbuf_count = 0;
95 uint64_t mbuffail_count = 0;
96 
97 sata_atapi_cmd_t sata_atapi_trace[64];
98 uint32_t sata_atapi_trace_index = 0;
99 int sata_atapi_trace_save = 1;
100 static	void sata_save_atapi_trace(sata_pkt_txlate_t *, int);
101 #define	SATAATAPITRACE(spx, count)	if (sata_atapi_trace_save) \
102     sata_save_atapi_trace(spx, count);
103 
104 #else
105 #define	SATA_LOG_D(arg)
106 #define	SATAATAPITRACE(spx, count)
107 #endif
108 
109 #if 0
110 static void
111 sata_test_atapi_packet_command(sata_hba_inst_t *, int);
112 #endif
113 
114 #ifdef SATA_INJECT_FAULTS
115 
116 #define		SATA_INJECT_PKT_FAULT	1
117 uint32_t	sata_inject_fault = 0;
118 
119 uint32_t	sata_fault_cmd = 0;
120 uint32_t	sata_inject_fault_type = 0;
121 uint32_t	sata_inject_fault_count = 0;
122 uint32_t	sata_inject_fault_pause_count = 0;
123 
124 static	void sata_inject_pkt_fault(sata_pkt_t *, uint8_t, int *, int);
125 
126 #endif
127 
128 #define	LEGACY_HWID_LEN	64	/* Model (40) + Serial (20) + pad */
129 
130 /*
131  * SATA cb_ops functions
132  */
133 static 	int sata_hba_open(dev_t *, int, int, cred_t *);
134 static 	int sata_hba_close(dev_t, int, int, cred_t *);
135 static 	int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *,	int *);
136 
137 /*
138  * SCSA required entry points
139  */
140 static	int sata_scsi_tgt_init(dev_info_t *, dev_info_t *,
141     scsi_hba_tran_t *, struct scsi_device *);
142 static	int sata_scsi_tgt_probe(struct scsi_device *,
143     int (*callback)(void));
144 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *,
145     scsi_hba_tran_t *, struct scsi_device *);
146 static 	int sata_scsi_start(struct scsi_address *, struct scsi_pkt *);
147 static 	int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *);
148 static 	int sata_scsi_reset(struct scsi_address *, int);
149 static 	int sata_scsi_getcap(struct scsi_address *, char *, int);
150 static 	int sata_scsi_setcap(struct scsi_address *, char *, int, int);
151 static 	struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *,
152     struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t),
153     caddr_t);
154 static 	void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *);
155 static 	void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *);
156 static 	void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *);
157 
158 /*
159  * SATA HBA interface functions are defined in sata_hba.h header file
160  */
161 
162 /* Event processing functions */
163 static	void sata_event_daemon(void *);
164 static	void sata_event_thread_control(int);
165 static	void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst);
166 static	void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *);
167 static	void sata_process_port_failed_event(sata_hba_inst_t *,
168     sata_address_t *);
169 static	void sata_process_port_link_events(sata_hba_inst_t *,
170     sata_address_t *);
171 static	void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *);
172 static	void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *);
173 static	void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *);
174 static	void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *);
175 static	void sata_process_target_node_cleanup(sata_hba_inst_t *,
176     sata_address_t *);
177 static	void sata_process_device_autoonline(sata_hba_inst_t *,
178     sata_address_t *saddr);
179 
180 /*
181  * Local translation functions
182  */
183 static	int sata_txlt_inquiry(sata_pkt_txlate_t *);
184 static	int sata_txlt_test_unit_ready(sata_pkt_txlate_t *);
185 static	int sata_txlt_start_stop_unit(sata_pkt_txlate_t *);
186 static	int sata_txlt_read_capacity(sata_pkt_txlate_t *);
187 static	int sata_txlt_request_sense(sata_pkt_txlate_t *);
188 static 	int sata_txlt_read(sata_pkt_txlate_t *);
189 static 	int sata_txlt_write(sata_pkt_txlate_t *);
190 static 	int sata_txlt_log_sense(sata_pkt_txlate_t *);
191 static 	int sata_txlt_log_select(sata_pkt_txlate_t *);
192 static 	int sata_txlt_mode_sense(sata_pkt_txlate_t *);
193 static 	int sata_txlt_mode_select(sata_pkt_txlate_t *);
194 static 	int sata_txlt_synchronize_cache(sata_pkt_txlate_t *);
195 static 	int sata_txlt_write_buffer(sata_pkt_txlate_t *);
196 static 	int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *);
197 
198 static 	int sata_hba_start(sata_pkt_txlate_t *, int *);
199 static	int sata_txlt_invalid_command(sata_pkt_txlate_t *);
200 static	int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *);
201 static 	void sata_txlt_rw_completion(sata_pkt_t *);
202 static 	void sata_txlt_nodata_cmd_completion(sata_pkt_t *);
203 static 	void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *);
204 static 	int sata_emul_rw_completion(sata_pkt_txlate_t *);
205 static 	struct scsi_extended_sense *sata_immediate_error_response(
206     sata_pkt_txlate_t *, int);
207 static	struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *);
208 
209 static 	int sata_txlt_atapi(sata_pkt_txlate_t *);
210 static 	void sata_txlt_atapi_completion(sata_pkt_t *);
211 
212 /*
213  * Local functions for ioctl
214  */
215 static	int32_t sata_get_port_num(sata_hba_inst_t *,  struct devctl_iocdata *);
216 static	void sata_cfgadm_state(sata_hba_inst_t *, int32_t,
217     devctl_ap_state_t *);
218 static	dev_info_t *sata_get_target_dip(dev_info_t *, int32_t);
219 static	dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *);
220 static	dev_info_t *sata_devt_to_devinfo(dev_t);
221 static	int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *);
222 static	int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *);
223 static	int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *);
224 static	int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *);
225 static	int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *);
226 static	int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *);
227 static	int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *);
228 static	int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *);
229 static	int sata_ioctl_reset_all(sata_hba_inst_t *);
230 static	int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *);
231 static	int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *,
232     sata_ioctl_data_t *, int mode);
233 static	int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *,
234     sata_ioctl_data_t *, int mode);
235 static	int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *,
236     sata_ioctl_data_t *, int mode);
237 static	int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *,
238     sata_ioctl_data_t *, int mode);
239 static	int sata_ioctl_get_serialnumber_info(sata_hba_inst_t *,
240     sata_device_t *, sata_ioctl_data_t *, int mode);
241 
242 /*
243  * Local functions
244  */
245 static 	void sata_remove_hba_instance(dev_info_t *);
246 static 	int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *);
247 static 	void sata_probe_ports(sata_hba_inst_t *);
248 static 	int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int);
249 static 	int sata_add_device(dev_info_t *, sata_hba_inst_t *, int cport,
250     int pmport);
251 static 	dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *,
252     sata_address_t *);
253 static 	int sata_validate_scsi_address(sata_hba_inst_t *,
254     struct scsi_address *, sata_device_t *);
255 static 	int sata_validate_sata_address(sata_hba_inst_t *, int, int, int);
256 static	sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t));
257 static	void sata_pkt_free(sata_pkt_txlate_t *);
258 static	int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t),
259     caddr_t, ddi_dma_attr_t *);
260 static	void sata_common_free_dma_rsrcs(sata_pkt_txlate_t *);
261 static	int sata_probe_device(sata_hba_inst_t *, sata_device_t *);
262 static	sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *,
263     sata_device_t *);
264 static 	int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *);
265 static	void sata_reidentify_device(sata_pkt_txlate_t *);
266 static	struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int);
267 static 	void sata_free_local_buffer(sata_pkt_txlate_t *);
268 static 	uint64_t sata_check_capacity(sata_drive_info_t *);
269 void 	sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *,
270     ddi_dma_attr_t *);
271 static 	int sata_fetch_device_identify_data(sata_hba_inst_t *,
272     sata_drive_info_t *);
273 static	void sata_update_port_info(sata_hba_inst_t *, sata_device_t *);
274 static	void sata_update_port_scr(sata_port_scr_t *, sata_device_t *);
275 static	int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *);
276 static	int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int);
277 static	int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int);
278 static	int sata_set_drive_features(sata_hba_inst_t *,
279     sata_drive_info_t *, int flag);
280 static	void sata_init_write_cache_mode(sata_drive_info_t *sdinfo);
281 static	int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *);
282 static	void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *,
283     uint8_t *);
284 static	int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *,
285     struct scsi_inquiry *);
286 static	int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *);
287 static	int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *);
288 static	int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *);
289 static	int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *);
290 static	int sata_mode_select_page_8(sata_pkt_txlate_t *,
291     struct mode_cache_scsi3 *, int, int *, int *, int *);
292 static	int sata_mode_select_page_1c(sata_pkt_txlate_t *,
293     struct mode_info_excpt_page *, int, int *, int *, int *);
294 static	int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *);
295 static	int sata_mode_select_page_30(sata_pkt_txlate_t *,
296     struct mode_acoustic_management *, int, int *, int *, int *);
297 
298 static	int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *);
299 static	int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *,
300     sata_hba_inst_t *);
301 static	int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *,
302     sata_hba_inst_t *);
303 static	int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *,
304     sata_hba_inst_t *);
305 static	void sata_save_drive_settings(sata_drive_info_t *);
306 static	void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *);
307 static	void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...);
308 static	int sata_fetch_smart_return_status(sata_hba_inst_t *,
309     sata_drive_info_t *);
310 static	int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *,
311     struct smart_data *);
312 static	int sata_smart_selftest_log(sata_hba_inst_t *,
313     sata_drive_info_t *,
314     struct smart_selftest_log *);
315 static	int sata_ext_smart_selftest_read_log(sata_hba_inst_t *,
316     sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t);
317 static	int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *,
318     uint8_t *, uint8_t, uint8_t);
319 static	int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *,
320     struct read_log_ext_directory *);
321 static	void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int);
322 static	void sata_xlate_errors(sata_pkt_txlate_t *);
323 static	void sata_decode_device_error(sata_pkt_txlate_t *,
324     struct scsi_extended_sense *);
325 static	void sata_set_device_removed(dev_info_t *);
326 static	boolean_t sata_check_device_removed(dev_info_t *);
327 static	void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *);
328 static	int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *,
329     sata_drive_info_t *);
330 static	int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *,
331     sata_drive_info_t *);
332 static	void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *);
333 static	void sata_fixed_sense_data_preset(struct scsi_extended_sense *);
334 static  void sata_target_devid_register(dev_info_t *, sata_drive_info_t *);
335 static  int sata_check_modser(char *, int);
336 
337 
338 
339 /*
340  * SATA Framework will ignore SATA HBA driver cb_ops structure and
341  * register following one with SCSA framework.
342  * Open & close are provided, so scsi framework will not use its own
343  */
344 static struct cb_ops sata_cb_ops = {
345 	sata_hba_open,			/* open */
346 	sata_hba_close,			/* close */
347 	nodev,				/* strategy */
348 	nodev,				/* print */
349 	nodev,				/* dump */
350 	nodev,				/* read */
351 	nodev,				/* write */
352 	sata_hba_ioctl,			/* ioctl */
353 	nodev,				/* devmap */
354 	nodev,				/* mmap */
355 	nodev,				/* segmap */
356 	nochpoll,			/* chpoll */
357 	ddi_prop_op,			/* cb_prop_op */
358 	0,				/* streamtab */
359 	D_NEW | D_MP,			/* cb_flag */
360 	CB_REV,				/* rev */
361 	nodev,				/* aread */
362 	nodev				/* awrite */
363 };
364 
365 
366 extern struct mod_ops mod_miscops;
367 extern uchar_t	scsi_cdb_size[];
368 
369 static struct modlmisc modlmisc = {
370 	&mod_miscops,			/* Type of module */
371 	"SATA Module v%I%"		/* module name */
372 };
373 
374 
375 static struct modlinkage modlinkage = {
376 	MODREV_1,
377 	(void *)&modlmisc,
378 	NULL
379 };
380 
381 /*
382  * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero,
383  * i.e. when scsi_pkt has not timeout specified.
384  */
385 static int sata_default_pkt_time = 60;	/* 60 seconds */
386 
387 /*
388  * Intermediate buffer device access attributes - they are required,
389  * but not necessarily used.
390  */
391 static ddi_device_acc_attr_t sata_acc_attr = {
392 	DDI_DEVICE_ATTR_V0,
393 	DDI_STRUCTURE_LE_ACC,
394 	DDI_STRICTORDER_ACC
395 };
396 
397 
398 /*
399  * Mutexes protecting structures in multithreaded operations.
400  * Because events are relatively rare, a single global mutex protecting
401  * data structures should be sufficient. To increase performance, add
402  * separate mutex per each sata port and use global mutex only to protect
403  * common data structures.
404  */
405 static	kmutex_t sata_mutex;		/* protects sata_hba_list */
406 static	kmutex_t sata_log_mutex;	/* protects log */
407 
408 static 	char sata_log_buf[256];
409 
410 /* Default write cache setting for SATA hard disks */
411 int	sata_write_cache = 1;		/* enabled */
412 
413 /* Default write cache setting for SATA ATAPI CD/DVD */
414 int 	sata_atapicdvd_write_cache = 1; /* enabled */
415 
416 /*
417  * Linked list of HBA instances
418  */
419 static 	sata_hba_inst_t *sata_hba_list = NULL;
420 static 	sata_hba_inst_t *sata_hba_list_tail = NULL;
421 /*
422  * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran
423  * structure and in sata soft state.
424  */
425 
426 /*
427  * Event daemon related variables
428  */
429 static 	kmutex_t sata_event_mutex;
430 static 	kcondvar_t sata_event_cv;
431 static 	kthread_t *sata_event_thread = NULL;
432 static 	int sata_event_thread_terminate = 0;
433 static 	int sata_event_pending = 0;
434 static 	int sata_event_thread_active = 0;
435 extern 	pri_t minclsyspri;
436 
437 /*
438  * NCQ error recovery command
439  */
440 static const sata_cmd_t sata_rle_cmd = {
441 	SATA_CMD_REV,
442 	NULL,
443 	{
444 		SATA_DIR_READ
445 	},
446 	ATA_ADDR_LBA48,
447 	0,
448 	0,
449 	0,
450 	0,
451 	0,
452 	1,
453 	READ_LOG_EXT_NCQ_ERROR_RECOVERY,
454 	0,
455 	0,
456 	0,
457 	SATAC_READ_LOG_EXT,
458 	0,
459 	0,
460 	0,
461 };
462 
463 /*
464  * ATAPI error recovery CDB
465  */
466 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = {
467 	SCMD_REQUEST_SENSE,
468 	0,			/* Only fixed RQ format is supported */
469 	0,
470 	0,
471 	SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */
472 	0
473 };
474 
475 
476 /* Warlock directives */
477 
478 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran))
479 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device))
480 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops))
481 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense))
482 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status))
483 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr))
484 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t))
485 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state))
486 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state))
487 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list))
488 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list))
489 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next))
490 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev))
491 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \
492     sata_hba_inst::satahba_scsi_tran))
493 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran))
494 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip))
495 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached))
496 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port))
497 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex,
498     sata_hba_inst::satahba_event_flags))
499 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
500     sata_cport_info::cport_devp))
501 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp))
502 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr))
503 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
504     sata_cport_info::cport_dev_type))
505 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type))
506 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
507     sata_cport_info::cport_state))
508 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state))
509 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
510     sata_pmport_info::pmport_state))
511 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state))
512 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type))
513 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive))
514 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port))
515 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports))
516 #ifdef SATA_DEBUG
517 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count))
518 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count))
519 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace))
520 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index))
521 #endif
522 
523 /* End of warlock directives */
524 
525 /* ************** loadable module configuration functions ************** */
526 
527 int
528 _init()
529 {
530 	int rval;
531 
532 	mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL);
533 	mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL);
534 	mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL);
535 	cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL);
536 	if ((rval = mod_install(&modlinkage)) != 0) {
537 #ifdef SATA_DEBUG
538 		cmn_err(CE_WARN, "sata: _init: mod_install failed\n");
539 #endif
540 		mutex_destroy(&sata_log_mutex);
541 		cv_destroy(&sata_event_cv);
542 		mutex_destroy(&sata_event_mutex);
543 		mutex_destroy(&sata_mutex);
544 	}
545 	return (rval);
546 }
547 
548 int
549 _fini()
550 {
551 	int rval;
552 
553 	if ((rval = mod_remove(&modlinkage)) != 0)
554 		return (rval);
555 
556 	mutex_destroy(&sata_log_mutex);
557 	cv_destroy(&sata_event_cv);
558 	mutex_destroy(&sata_event_mutex);
559 	mutex_destroy(&sata_mutex);
560 	return (rval);
561 }
562 
563 int
564 _info(struct modinfo *modinfop)
565 {
566 	return (mod_info(&modlinkage, modinfop));
567 }
568 
569 
570 
571 /* ********************* SATA HBA entry points ********************* */
572 
573 
574 /*
575  * Called by SATA HBA from _init().
576  * Registers HBA driver instance/sata framework pair with scsi framework, by
577  * calling scsi_hba_init().
578  *
579  * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used
580  * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver
581  * cb_ops pointer in SATA HBA driver dev_ops structure.
582  * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors.
583  *
584  * Return status of the scsi_hba_init() is returned to a calling SATA HBA
585  * driver.
586  */
587 int
588 sata_hba_init(struct modlinkage *modlp)
589 {
590 	int rval;
591 	struct dev_ops *hba_ops;
592 
593 	SATADBG1(SATA_DBG_HBA_IF, NULL,
594 	    "sata_hba_init: name %s \n",
595 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
596 	/*
597 	 * Fill-up cb_ops and dev_ops when necessary
598 	 */
599 	hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
600 	/*
601 	 * Provide pointer to SATA dev_ops
602 	 */
603 	hba_ops->devo_cb_ops = &sata_cb_ops;
604 
605 	/*
606 	 * Register SATA HBA with SCSI framework
607 	 */
608 	if ((rval = scsi_hba_init(modlp)) != 0) {
609 		SATADBG1(SATA_DBG_HBA_IF, NULL,
610 		    "sata_hba_init: scsi hba init failed\n", NULL);
611 		return (rval);
612 	}
613 
614 	return (0);
615 }
616 
617 
618 /* HBA attach stages */
619 #define	HBA_ATTACH_STAGE_SATA_HBA_INST	1
620 #define	HBA_ATTACH_STAGE_SCSI_ATTACHED	2
621 #define	HBA_ATTACH_STAGE_SETUP		4
622 #define	HBA_ATTACH_STAGE_LINKED		8
623 
624 
625 /*
626  *
627  * Called from SATA HBA driver's attach routine to attach an instance of
628  * the HBA.
629  *
630  * For DDI_ATTACH command:
631  * sata_hba_inst structure is allocated here and initialized with pointers to
632  * SATA framework implementation of required scsi tran functions.
633  * The scsi_tran's tran_hba_private field is used by SATA Framework to point
634  * to the soft structure (sata_hba_inst) allocated by SATA framework for
635  * SATA HBA instance related data.
636  * The scsi_tran's tran_hba_private field is used by SATA framework to
637  * store a pointer to per-HBA-instance of sata_hba_inst structure.
638  * The sata_hba_inst structure is cross-linked to scsi tran structure.
639  * Among other info, a pointer to sata_hba_tran structure is stored in
640  * sata_hba_inst. The sata_hba_inst structures for different HBA instances are
641  * linked together into the list, pointed to by sata_hba_list.
642  * On the first HBA instance attach the sata event thread is initialized.
643  * Attachment points are created for all SATA ports of the HBA being attached.
644  * All HBA instance's SATA ports are probed and type of plugged devices is
645  * determined. For each device of a supported type, a target node is created.
646  *
647  * DDI_SUCCESS is returned when attachment process is successful,
648  * DDI_FAILURE is returned otherwise.
649  *
650  * For DDI_RESUME command:
651  * Not implemented at this time (postponed until phase 2 of the development).
652  */
653 int
654 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran,
655     ddi_attach_cmd_t cmd)
656 {
657 	sata_hba_inst_t	*sata_hba_inst;
658 	scsi_hba_tran_t *scsi_tran = NULL;
659 	int hba_attach_state = 0;
660 	char taskq_name[MAXPATHLEN];
661 
662 	SATADBG3(SATA_DBG_HBA_IF, NULL,
663 	    "sata_hba_attach: node %s (%s%d)\n",
664 	    ddi_node_name(dip), ddi_driver_name(dip),
665 	    ddi_get_instance(dip));
666 
667 	if (cmd == DDI_RESUME) {
668 		/*
669 		 * Postponed until phase 2 of the development
670 		 */
671 		return (DDI_FAILURE);
672 	}
673 
674 	if (cmd != DDI_ATTACH) {
675 		return (DDI_FAILURE);
676 	}
677 
678 	/* cmd == DDI_ATTACH */
679 
680 	if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) {
681 		SATA_LOG_D((NULL, CE_WARN,
682 		    "sata_hba_attach: invalid sata_hba_tran"));
683 		return (DDI_FAILURE);
684 	}
685 	/*
686 	 * Allocate and initialize SCSI tran structure.
687 	 * SATA copy of tran_bus_config is provided to create port nodes.
688 	 */
689 	scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
690 	if (scsi_tran == NULL)
691 		return (DDI_FAILURE);
692 	/*
693 	 * Allocate soft structure for SATA HBA instance.
694 	 * There is a separate softstate for each HBA instance.
695 	 */
696 	sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP);
697 	ASSERT(sata_hba_inst != NULL); /* this should not fail */
698 	mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL);
699 	hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST;
700 
701 	/*
702 	 * scsi_trans's tran_hba_private is used by SATA Framework to point to
703 	 * soft structure allocated by SATA framework for
704 	 * SATA HBA instance related data.
705 	 */
706 	scsi_tran->tran_hba_private	= sata_hba_inst;
707 	scsi_tran->tran_tgt_private	= NULL;
708 
709 	scsi_tran->tran_tgt_init	= sata_scsi_tgt_init;
710 	scsi_tran->tran_tgt_probe	= sata_scsi_tgt_probe;
711 	scsi_tran->tran_tgt_free	= sata_scsi_tgt_free;
712 
713 	scsi_tran->tran_start		= sata_scsi_start;
714 	scsi_tran->tran_reset		= sata_scsi_reset;
715 	scsi_tran->tran_abort		= sata_scsi_abort;
716 	scsi_tran->tran_getcap		= sata_scsi_getcap;
717 	scsi_tran->tran_setcap		= sata_scsi_setcap;
718 	scsi_tran->tran_init_pkt	= sata_scsi_init_pkt;
719 	scsi_tran->tran_destroy_pkt	= sata_scsi_destroy_pkt;
720 
721 	scsi_tran->tran_dmafree		= sata_scsi_dmafree;
722 	scsi_tran->tran_sync_pkt	= sata_scsi_sync_pkt;
723 
724 	scsi_tran->tran_reset_notify	= NULL;
725 	scsi_tran->tran_get_bus_addr	= NULL;
726 	scsi_tran->tran_quiesce		= NULL;
727 	scsi_tran->tran_unquiesce	= NULL;
728 	scsi_tran->tran_bus_reset	= NULL;
729 
730 	if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr,
731 	    scsi_tran, 0) != DDI_SUCCESS) {
732 #ifdef SATA_DEBUG
733 		cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed",
734 		    ddi_driver_name(dip), ddi_get_instance(dip));
735 #endif
736 		goto fail;
737 	}
738 	hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED;
739 
740 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) {
741 		if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
742 		    "sata", 1) != DDI_PROP_SUCCESS) {
743 			SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: "
744 			    "failed to create hba sata prop"));
745 			goto fail;
746 		}
747 	}
748 
749 	/*
750 	 * Save pointers in hba instance soft state.
751 	 */
752 	sata_hba_inst->satahba_scsi_tran = scsi_tran;
753 	sata_hba_inst->satahba_tran = sata_tran;
754 	sata_hba_inst->satahba_dip = dip;
755 
756 	/*
757 	 * Create a task queue to handle emulated commands completion
758 	 * Use node name, dash, instance number as the queue name.
759 	 */
760 	taskq_name[0] = '\0';
761 	(void) strlcat(taskq_name, DEVI(dip)->devi_node_name,
762 	    sizeof (taskq_name));
763 	(void) snprintf(taskq_name + strlen(taskq_name),
764 	    sizeof (taskq_name) - strlen(taskq_name),
765 	    "-%d", DEVI(dip)->devi_instance);
766 	sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1,
767 	    minclsyspri, 1, sata_tran->sata_tran_hba_num_cports,
768 	    TASKQ_DYNAMIC);
769 
770 	hba_attach_state |= HBA_ATTACH_STAGE_SETUP;
771 
772 	/*
773 	 * Create events thread if not created yet.
774 	 */
775 	sata_event_thread_control(1);
776 
777 	/*
778 	 * Link this hba instance into the list.
779 	 */
780 	mutex_enter(&sata_mutex);
781 
782 	if (sata_hba_list == NULL) {
783 		/*
784 		 * The first instance of HBA is attached.
785 		 * Set current/active default maximum NCQ/TCQ queue depth for
786 		 * all SATA devices. It is done here and now, to eliminate the
787 		 * possibility of the dynamic, programatic modification of the
788 		 * queue depth via global (and public) sata_max_queue_depth
789 		 * variable (this would require special handling in HBA drivers)
790 		 */
791 		sata_current_max_qdepth = sata_max_queue_depth;
792 		if (sata_current_max_qdepth > 32)
793 			sata_current_max_qdepth = 32;
794 		else if (sata_current_max_qdepth < 1)
795 			sata_current_max_qdepth = 1;
796 	}
797 
798 	sata_hba_inst->satahba_next = NULL;
799 	sata_hba_inst->satahba_prev = sata_hba_list_tail;
800 	if (sata_hba_list == NULL) {
801 		sata_hba_list = sata_hba_inst;
802 	}
803 	if (sata_hba_list_tail != NULL) {
804 		sata_hba_list_tail->satahba_next = sata_hba_inst;
805 	}
806 	sata_hba_list_tail = sata_hba_inst;
807 	mutex_exit(&sata_mutex);
808 	hba_attach_state |= HBA_ATTACH_STAGE_LINKED;
809 
810 	/*
811 	 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl
812 	 * SATA HBA driver should not use its own open/close entry points.
813 	 *
814 	 * Make sure that instance number doesn't overflow
815 	 * when forming minor numbers.
816 	 */
817 	ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT));
818 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
819 	    INST2DEVCTL(ddi_get_instance(dip)),
820 	    DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) {
821 #ifdef SATA_DEBUG
822 		cmn_err(CE_WARN, "sata_hba_attach: "
823 		    "cannot create devctl minor node");
824 #endif
825 		goto fail;
826 	}
827 
828 
829 	/*
830 	 * Set-up kstats here, if necessary.
831 	 * (postponed until phase 2 of the development).
832 	 */
833 
834 
835 	/*
836 	 * Probe controller ports. This operation will describe a current
837 	 * controller/port/multipliers/device configuration and will create
838 	 * attachment points.
839 	 * We may end-up with just a controller with no devices attached.
840 	 * For the ports with a supported device attached, device target nodes
841 	 * are created and devices are initialized.
842 	 */
843 	sata_probe_ports(sata_hba_inst);
844 
845 	sata_hba_inst->satahba_attached = 1;
846 	return (DDI_SUCCESS);
847 
848 fail:
849 	if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) {
850 		(void) sata_remove_hba_instance(dip);
851 		if (sata_hba_list == NULL)
852 			sata_event_thread_control(0);
853 	}
854 
855 	if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) {
856 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
857 		taskq_destroy(sata_hba_inst->satahba_taskq);
858 	}
859 
860 	if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED)
861 		(void) scsi_hba_detach(dip);
862 
863 	if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) {
864 		mutex_destroy(&sata_hba_inst->satahba_mutex);
865 		kmem_free((void *)sata_hba_inst,
866 		    sizeof (struct sata_hba_inst));
867 		scsi_hba_tran_free(scsi_tran);
868 	}
869 
870 	sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed",
871 	    ddi_driver_name(dip), ddi_get_instance(dip));
872 
873 	return (DDI_FAILURE);
874 }
875 
876 
877 /*
878  * Called by SATA HBA from to detach an instance of the driver.
879  *
880  * For DDI_DETACH command:
881  * Free local structures allocated for SATA HBA instance during
882  * sata_hba_attach processing.
883  *
884  * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise.
885  *
886  * For DDI_SUSPEND command:
887  * Not implemented at this time (postponed until phase 2 of the development)
888  * Returnd DDI_SUCCESS.
889  *
890  * When the last HBA instance is detached, the event daemon is terminated.
891  *
892  * NOTE: cport support only, no port multiplier support.
893  */
894 int
895 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
896 {
897 	dev_info_t	*tdip;
898 	sata_hba_inst_t	*sata_hba_inst;
899 	scsi_hba_tran_t *scsi_hba_tran;
900 	sata_cport_info_t *cportinfo;
901 	sata_drive_info_t *sdinfo;
902 	int ncport;
903 
904 	SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n",
905 	    ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip));
906 
907 	switch (cmd) {
908 	case DDI_DETACH:
909 
910 		if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
911 			return (DDI_FAILURE);
912 
913 		sata_hba_inst = scsi_hba_tran->tran_hba_private;
914 		if (sata_hba_inst == NULL)
915 			return (DDI_FAILURE);
916 
917 		if (scsi_hba_detach(dip) == DDI_FAILURE) {
918 			sata_hba_inst->satahba_attached = 1;
919 			return (DDI_FAILURE);
920 		}
921 
922 		/*
923 		 * Free all target nodes - at this point
924 		 * devices should be at least offlined
925 		 * otherwise scsi_hba_detach() should not be called.
926 		 */
927 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
928 		    ncport++) {
929 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
930 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
931 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
932 				if (sdinfo != NULL) {
933 					tdip = sata_get_target_dip(dip,
934 					    ncport);
935 					if (tdip != NULL) {
936 						if (ndi_devi_offline(tdip,
937 						    NDI_DEVI_REMOVE) !=
938 						    NDI_SUCCESS) {
939 							SATA_LOG_D((
940 							    sata_hba_inst,
941 							    CE_WARN,
942 							    "sata_hba_detach: "
943 							    "Target node not "
944 							    "removed !"));
945 							return (DDI_FAILURE);
946 						}
947 					}
948 				}
949 			}
950 		}
951 		/*
952 		 * Disable sata event daemon processing for this HBA
953 		 */
954 		sata_hba_inst->satahba_attached = 0;
955 
956 		/*
957 		 * Remove event daemon thread, if it is last HBA instance.
958 		 */
959 
960 		mutex_enter(&sata_mutex);
961 		if (sata_hba_list->satahba_next == NULL) {
962 			mutex_exit(&sata_mutex);
963 			sata_event_thread_control(0);
964 			mutex_enter(&sata_mutex);
965 		}
966 		mutex_exit(&sata_mutex);
967 
968 		/* Remove this HBA instance from the HBA list */
969 		sata_remove_hba_instance(dip);
970 
971 		/*
972 		 * At this point there should be no target nodes attached.
973 		 * Detach and destroy device and port info structures.
974 		 */
975 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
976 		    ncport++) {
977 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
978 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
979 				sdinfo =
980 				    cportinfo->cport_devp.cport_sata_drive;
981 				if (sdinfo != NULL) {
982 					/* Release device structure */
983 					kmem_free(sdinfo,
984 					    sizeof (sata_drive_info_t));
985 				}
986 				/* Release cport info */
987 				mutex_destroy(&cportinfo->cport_mutex);
988 				kmem_free(cportinfo,
989 				    sizeof (sata_cport_info_t));
990 			}
991 		}
992 
993 		scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran);
994 
995 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
996 
997 		taskq_destroy(sata_hba_inst->satahba_taskq);
998 
999 		mutex_destroy(&sata_hba_inst->satahba_mutex);
1000 		kmem_free((void *)sata_hba_inst,
1001 		    sizeof (struct sata_hba_inst));
1002 
1003 		return (DDI_SUCCESS);
1004 
1005 	case DDI_SUSPEND:
1006 		/*
1007 		 * Postponed until phase 2
1008 		 */
1009 		return (DDI_FAILURE);
1010 
1011 	default:
1012 		return (DDI_FAILURE);
1013 	}
1014 }
1015 
1016 
1017 /*
1018  * Called by an HBA drive from _fini() routine.
1019  * Unregisters SATA HBA instance/SATA framework pair from the scsi framework.
1020  */
1021 void
1022 sata_hba_fini(struct modlinkage *modlp)
1023 {
1024 	SATADBG1(SATA_DBG_HBA_IF, NULL,
1025 	    "sata_hba_fini: name %s\n",
1026 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
1027 
1028 	scsi_hba_fini(modlp);
1029 }
1030 
1031 
1032 /*
1033  * Default open and close routine for sata_hba framework.
1034  *
1035  */
1036 /*
1037  * Open devctl node.
1038  *
1039  * Returns:
1040  * 0 if node was open successfully, error code otherwise.
1041  *
1042  *
1043  */
1044 
1045 static int
1046 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1047 {
1048 #ifndef __lock_lint
1049 	_NOTE(ARGUNUSED(credp))
1050 #endif
1051 	int rv = 0;
1052 	dev_info_t *dip;
1053 	scsi_hba_tran_t *scsi_hba_tran;
1054 	sata_hba_inst_t	*sata_hba_inst;
1055 
1056 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL);
1057 
1058 	if (otyp != OTYP_CHR)
1059 		return (EINVAL);
1060 
1061 	dip = sata_devt_to_devinfo(*devp);
1062 	if (dip == NULL)
1063 		return (ENXIO);
1064 
1065 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1066 		return (ENXIO);
1067 
1068 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1069 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1070 		return (ENXIO);
1071 
1072 	mutex_enter(&sata_mutex);
1073 	if (flags & FEXCL) {
1074 		if (sata_hba_inst->satahba_open_flag != 0) {
1075 			rv = EBUSY;
1076 		} else {
1077 			sata_hba_inst->satahba_open_flag =
1078 			    SATA_DEVCTL_EXOPENED;
1079 		}
1080 	} else {
1081 		if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) {
1082 			rv = EBUSY;
1083 		} else {
1084 			sata_hba_inst->satahba_open_flag =
1085 			    SATA_DEVCTL_SOPENED;
1086 		}
1087 	}
1088 	mutex_exit(&sata_mutex);
1089 
1090 	return (rv);
1091 }
1092 
1093 
1094 /*
1095  * Close devctl node.
1096  * Returns:
1097  * 0 if node was closed successfully, error code otherwise.
1098  *
1099  */
1100 
1101 static int
1102 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
1103 {
1104 #ifndef __lock_lint
1105 	_NOTE(ARGUNUSED(credp))
1106 	_NOTE(ARGUNUSED(flag))
1107 #endif
1108 	dev_info_t *dip;
1109 	scsi_hba_tran_t *scsi_hba_tran;
1110 	sata_hba_inst_t	*sata_hba_inst;
1111 
1112 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL);
1113 
1114 	if (otyp != OTYP_CHR)
1115 		return (EINVAL);
1116 
1117 	dip = sata_devt_to_devinfo(dev);
1118 	if (dip == NULL)
1119 		return (ENXIO);
1120 
1121 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1122 		return (ENXIO);
1123 
1124 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1125 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1126 		return (ENXIO);
1127 
1128 	mutex_enter(&sata_mutex);
1129 	sata_hba_inst->satahba_open_flag = 0;
1130 	mutex_exit(&sata_mutex);
1131 	return (0);
1132 }
1133 
1134 
1135 
1136 /*
1137  * Standard IOCTL commands for SATA hotplugging.
1138  * Implemented DEVCTL_AP commands:
1139  * DEVCTL_AP_CONNECT
1140  * DEVCTL_AP_DISCONNECT
1141  * DEVCTL_AP_CONFIGURE
1142  * DEVCTL_UNCONFIGURE
1143  * DEVCTL_AP_CONTROL
1144  *
1145  * Commands passed to default ndi ioctl handler:
1146  * DEVCTL_DEVICE_GETSTATE
1147  * DEVCTL_DEVICE_ONLINE
1148  * DEVCTL_DEVICE_OFFLINE
1149  * DEVCTL_DEVICE_REMOVE
1150  * DEVCTL_DEVICE_INSERT
1151  * DEVCTL_BUS_GETSTATE
1152  *
1153  * All other cmds are passed to HBA if it provide ioctl handler, or failed
1154  * if not.
1155  *
1156  * Returns:
1157  * 0 if successful,
1158  * error code if operation failed.
1159  *
1160  * NOTE: Port Multiplier is not supported.
1161  *
1162  */
1163 
1164 static int
1165 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1166     int *rvalp)
1167 {
1168 #ifndef __lock_lint
1169 	_NOTE(ARGUNUSED(credp))
1170 	_NOTE(ARGUNUSED(rvalp))
1171 #endif
1172 	int rv = 0;
1173 	int32_t	comp_port = -1;
1174 	dev_info_t *dip;
1175 	devctl_ap_state_t ap_state;
1176 	struct devctl_iocdata *dcp = NULL;
1177 	scsi_hba_tran_t *scsi_hba_tran;
1178 	sata_hba_inst_t *sata_hba_inst;
1179 	sata_device_t sata_device;
1180 	sata_cport_info_t *cportinfo;
1181 	int cport, pmport, qual;
1182 	int rval = SATA_SUCCESS;
1183 
1184 	dip = sata_devt_to_devinfo(dev);
1185 	if (dip == NULL)
1186 		return (ENXIO);
1187 
1188 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1189 		return (ENXIO);
1190 
1191 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1192 	if (sata_hba_inst == NULL)
1193 		return (ENXIO);
1194 
1195 	if (sata_hba_inst->satahba_tran == NULL)
1196 		return (ENXIO);
1197 
1198 	switch (cmd) {
1199 
1200 	case DEVCTL_DEVICE_GETSTATE:
1201 	case DEVCTL_DEVICE_ONLINE:
1202 	case DEVCTL_DEVICE_OFFLINE:
1203 	case DEVCTL_DEVICE_REMOVE:
1204 	case DEVCTL_BUS_GETSTATE:
1205 		/*
1206 		 * There may be more cases that we want to pass to default
1207 		 * handler rather than fail them.
1208 		 */
1209 		return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
1210 	}
1211 
1212 	/* read devctl ioctl data */
1213 	if (cmd != DEVCTL_AP_CONTROL) {
1214 		if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1215 			return (EFAULT);
1216 
1217 		if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) ==
1218 		    -1) {
1219 			if (dcp)
1220 				ndi_dc_freehdl(dcp);
1221 			return (EINVAL);
1222 		}
1223 
1224 		cport = SCSI_TO_SATA_CPORT(comp_port);
1225 		pmport = SCSI_TO_SATA_PMPORT(comp_port);
1226 		/* Only cport is considered now, i.e. SATA_ADDR_CPORT */
1227 		qual = SATA_ADDR_CPORT;
1228 		if (sata_validate_sata_address(sata_hba_inst, cport, pmport,
1229 		    qual) != 0) {
1230 			ndi_dc_freehdl(dcp);
1231 			return (EINVAL);
1232 		}
1233 
1234 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1235 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1236 		    cport_mutex);
1237 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1238 			/*
1239 			 * Cannot process ioctl request now. Come back later.
1240 			 */
1241 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1242 			    cport_mutex);
1243 			ndi_dc_freehdl(dcp);
1244 			return (EBUSY);
1245 		}
1246 		/* Block event processing for this port */
1247 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1248 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1249 
1250 		sata_device.satadev_addr.cport = cport;
1251 		sata_device.satadev_addr.pmport = pmport;
1252 		sata_device.satadev_addr.qual = qual;
1253 		sata_device.satadev_rev = SATA_DEVICE_REV;
1254 	}
1255 
1256 	switch (cmd) {
1257 
1258 	case DEVCTL_AP_DISCONNECT:
1259 
1260 		/*
1261 		 * Normally, cfgadm sata plugin will try to offline
1262 		 * (unconfigure) device before this request. Nevertheless,
1263 		 * if a device is still configured, we need to
1264 		 * attempt to offline and unconfigure device first, and we will
1265 		 * deactivate the port regardless of the unconfigure
1266 		 * operation results.
1267 		 *
1268 		 */
1269 		rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device);
1270 
1271 		break;
1272 
1273 	case DEVCTL_AP_UNCONFIGURE:
1274 
1275 		/*
1276 		 * The unconfigure operation uses generic nexus operation to
1277 		 * offline a device. It leaves a target device node attached.
1278 		 * and obviously sata_drive_info attached as well, because
1279 		 * from the hardware point of view nothing has changed.
1280 		 */
1281 		rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device);
1282 		break;
1283 
1284 	case DEVCTL_AP_CONNECT:
1285 	{
1286 		/*
1287 		 * The sata cfgadm pluging will invoke this operation only if
1288 		 * port was found in the disconnect state (failed state
1289 		 * is also treated as the disconnected state).
1290 		 * If port activation is successful and a device is found
1291 		 * attached to the port, the initialization sequence is
1292 		 * executed to probe the port and attach
1293 		 * a device structure to a port structure. The device is not
1294 		 * set in configured state (system-wise) by this operation.
1295 		 */
1296 
1297 		rv = sata_ioctl_connect(sata_hba_inst, &sata_device);
1298 
1299 		break;
1300 	}
1301 
1302 	case DEVCTL_AP_CONFIGURE:
1303 	{
1304 		/*
1305 		 * A port may be in an active or shutdown state.
1306 		 * If port is in a failed state, operation is aborted.
1307 		 * If a port is in a shutdown state, sata_tran_port_activate()
1308 		 * is invoked prior to any other operation.
1309 		 *
1310 		 * Onlining the device involves creating a new target node.
1311 		 * If there is an old target node present (belonging to
1312 		 * previously removed device), the operation is aborted - the
1313 		 * old node has to be released and removed before configure
1314 		 * operation is attempted.
1315 		 */
1316 
1317 		rv = sata_ioctl_configure(sata_hba_inst, &sata_device);
1318 
1319 		break;
1320 	}
1321 
1322 	case DEVCTL_AP_GETSTATE:
1323 
1324 		sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state);
1325 
1326 		ap_state.ap_last_change = (time_t)-1;
1327 		ap_state.ap_error_code = 0;
1328 		ap_state.ap_in_transition = 0;
1329 
1330 		/* Copy the return AP-state information to the user space */
1331 		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
1332 			rv = EFAULT;
1333 		}
1334 		break;
1335 
1336 	case DEVCTL_AP_CONTROL:
1337 	{
1338 		/*
1339 		 * Generic devctl for hardware specific functionality
1340 		 */
1341 		sata_ioctl_data_t	ioc;
1342 
1343 		ASSERT(dcp == NULL);
1344 
1345 		/* Copy in user ioctl data first */
1346 #ifdef _MULTI_DATAMODEL
1347 		if (ddi_model_convert_from(mode & FMODELS) ==
1348 		    DDI_MODEL_ILP32) {
1349 
1350 			sata_ioctl_data_32_t	ioc32;
1351 
1352 			if (ddi_copyin((void *)arg, (void *)&ioc32,
1353 			    sizeof (ioc32), mode) != 0) {
1354 				rv = EFAULT;
1355 				break;
1356 			}
1357 			ioc.cmd 	= (uint_t)ioc32.cmd;
1358 			ioc.port	= (uint_t)ioc32.port;
1359 			ioc.get_size	= (uint_t)ioc32.get_size;
1360 			ioc.buf		= (caddr_t)(uintptr_t)ioc32.buf;
1361 			ioc.bufsiz	= (uint_t)ioc32.bufsiz;
1362 			ioc.misc_arg	= (uint_t)ioc32.misc_arg;
1363 		} else
1364 #endif /* _MULTI_DATAMODEL */
1365 		if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
1366 		    mode) != 0) {
1367 			return (EFAULT);
1368 		}
1369 
1370 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
1371 		    "sata_hba_ioctl: DEVCTL_AP_CONTROL "
1372 		    "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port);
1373 
1374 		/*
1375 		 * To avoid BE/LE and 32/64 issues, a get_size always returns
1376 		 * a 32-bit number.
1377 		 */
1378 		if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
1379 			return (EINVAL);
1380 		}
1381 		/* validate address */
1382 		cport = SCSI_TO_SATA_CPORT(ioc.port);
1383 		pmport = SCSI_TO_SATA_PMPORT(ioc.port);
1384 		qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port);
1385 
1386 		/* Override address qualifier - handle cport only for now */
1387 		qual = SATA_ADDR_CPORT;
1388 
1389 		if (sata_validate_sata_address(sata_hba_inst, cport,
1390 		    pmport, qual) != 0)
1391 			return (EINVAL);
1392 
1393 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1394 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1395 		    cport_mutex);
1396 		/* Is the port locked by event processing daemon ? */
1397 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1398 			/*
1399 			 * Cannot process ioctl request now. Come back later
1400 			 */
1401 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1402 			    cport_mutex);
1403 			return (EBUSY);
1404 		}
1405 		/* Block event processing for this port */
1406 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1407 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1408 
1409 
1410 		sata_device.satadev_addr.cport = cport;
1411 		sata_device.satadev_addr.pmport = pmport;
1412 		sata_device.satadev_addr.qual = qual;
1413 		sata_device.satadev_rev = SATA_DEVICE_REV;
1414 
1415 		switch (ioc.cmd) {
1416 
1417 		case SATA_CFGA_RESET_PORT:
1418 			/*
1419 			 * There is no protection for configured device.
1420 			 */
1421 			rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device);
1422 			break;
1423 
1424 		case SATA_CFGA_RESET_DEVICE:
1425 			/*
1426 			 * There is no protection for configured device.
1427 			 */
1428 			rv = sata_ioctl_reset_device(sata_hba_inst,
1429 			    &sata_device);
1430 			break;
1431 
1432 		case SATA_CFGA_RESET_ALL:
1433 			/*
1434 			 * There is no protection for configured devices.
1435 			 */
1436 			rv = sata_ioctl_reset_all(sata_hba_inst);
1437 			/*
1438 			 * We return here, because common return is for
1439 			 * a single port operation - we have already unlocked
1440 			 * all ports and no dc handle was allocated.
1441 			 */
1442 			return (rv);
1443 
1444 		case SATA_CFGA_PORT_DEACTIVATE:
1445 			/*
1446 			 * Arbitrarily unconfigure attached device, if any.
1447 			 * Even if the unconfigure fails, proceed with the
1448 			 * port deactivation.
1449 			 */
1450 			rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device);
1451 
1452 			break;
1453 
1454 		case SATA_CFGA_PORT_ACTIVATE:
1455 
1456 			rv = sata_ioctl_activate(sata_hba_inst, &sata_device);
1457 			break;
1458 
1459 		case SATA_CFGA_PORT_SELF_TEST:
1460 
1461 			rv = sata_ioctl_port_self_test(sata_hba_inst,
1462 			    &sata_device);
1463 			break;
1464 
1465 		case SATA_CFGA_GET_DEVICE_PATH:
1466 			if (qual == SATA_ADDR_CPORT)
1467 				sata_device.satadev_addr.qual =
1468 				    SATA_ADDR_DCPORT;
1469 			else
1470 				sata_device.satadev_addr.qual =
1471 				    SATA_ADDR_DPMPORT;
1472 			rv = sata_ioctl_get_device_path(sata_hba_inst,
1473 			    &sata_device, &ioc, mode);
1474 			break;
1475 
1476 		case SATA_CFGA_GET_AP_TYPE:
1477 
1478 			rv = sata_ioctl_get_ap_type(sata_hba_inst,
1479 			    &sata_device, &ioc, mode);
1480 			break;
1481 
1482 		case SATA_CFGA_GET_MODEL_INFO:
1483 
1484 			rv = sata_ioctl_get_model_info(sata_hba_inst,
1485 			    &sata_device, &ioc, mode);
1486 			break;
1487 
1488 		case SATA_CFGA_GET_REVFIRMWARE_INFO:
1489 
1490 			rv = sata_ioctl_get_revfirmware_info(sata_hba_inst,
1491 			    &sata_device, &ioc, mode);
1492 			break;
1493 
1494 		case SATA_CFGA_GET_SERIALNUMBER_INFO:
1495 
1496 			rv = sata_ioctl_get_serialnumber_info(sata_hba_inst,
1497 			    &sata_device, &ioc, mode);
1498 			break;
1499 
1500 		default:
1501 			rv = EINVAL;
1502 			break;
1503 
1504 		} /* End of DEVCTL_AP_CONTROL cmd switch */
1505 
1506 		break;
1507 	}
1508 
1509 	default:
1510 	{
1511 		/*
1512 		 * If we got here, we got an IOCTL that SATA HBA Framework
1513 		 * does not recognize. Pass ioctl to HBA driver, in case
1514 		 * it could process it.
1515 		 */
1516 		sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran;
1517 		dev_info_t	*mydip = SATA_DIP(sata_hba_inst);
1518 
1519 		SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1520 		    "IOCTL 0x%2x not supported in SATA framework, "
1521 		    "passthrough to HBA", cmd);
1522 
1523 		if (sata_tran->sata_tran_ioctl == NULL) {
1524 			rv = EINVAL;
1525 			break;
1526 		}
1527 		rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg);
1528 		if (rval != 0) {
1529 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1530 			    "IOCTL 0x%2x failed in HBA", cmd);
1531 			rv = rval;
1532 		}
1533 		break;
1534 	}
1535 
1536 	} /* End of main IOCTL switch */
1537 
1538 	if (dcp) {
1539 		ndi_dc_freehdl(dcp);
1540 	}
1541 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1542 	cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
1543 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1544 
1545 	return (rv);
1546 }
1547 
1548 
1549 /*
1550  * Create error retrieval sata packet
1551  *
1552  * A sata packet is allocated and set-up to contain specified error retrieval
1553  * command and appropriate dma-able data buffer.
1554  * No association with any scsi packet is made and no callback routine is
1555  * specified.
1556  *
1557  * Returns a pointer to sata packet upon successfull packet creation.
1558  * Returns NULL, if packet cannot be created.
1559  */
1560 sata_pkt_t *
1561 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device,
1562     int pkt_type)
1563 {
1564 	sata_hba_inst_t	*sata_hba_inst;
1565 	sata_pkt_txlate_t *spx;
1566 	sata_pkt_t *spkt;
1567 	sata_drive_info_t *sdinfo;
1568 
1569 	mutex_enter(&sata_mutex);
1570 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1571 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1572 		if (SATA_DIP(sata_hba_inst) == dip)
1573 			break;
1574 	}
1575 	mutex_exit(&sata_mutex);
1576 	ASSERT(sata_hba_inst != NULL);
1577 
1578 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
1579 	if (sdinfo == NULL) {
1580 		sata_log(sata_hba_inst, CE_WARN,
1581 		    "sata: error recovery request for non-attached device at "
1582 		    "cport %d", sata_device->satadev_addr.cport);
1583 		return (NULL);
1584 	}
1585 
1586 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1587 	spx->txlt_sata_hba_inst = sata_hba_inst;
1588 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
1589 	spkt = sata_pkt_alloc(spx, NULL);
1590 	if (spkt == NULL) {
1591 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1592 		return (NULL);
1593 	}
1594 	/* address is needed now */
1595 	spkt->satapkt_device.satadev_addr = sata_device->satadev_addr;
1596 
1597 	switch (pkt_type) {
1598 	case SATA_ERR_RETR_PKT_TYPE_NCQ:
1599 		if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS)
1600 			return (spkt);
1601 		break;
1602 
1603 	case SATA_ERR_RETR_PKT_TYPE_ATAPI:
1604 		if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS)
1605 			return (spkt);
1606 		break;
1607 
1608 	default:
1609 		break;
1610 	}
1611 
1612 	sata_pkt_free(spx);
1613 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1614 	return (NULL);
1615 
1616 }
1617 
1618 
1619 /*
1620  * Free error retrieval sata packet
1621  *
1622  * Free sata packet and any associated resources allocated previously by
1623  * sata_get_error_retrieval_pkt().
1624  *
1625  * Void return.
1626  */
1627 void
1628 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt)
1629 {
1630 	sata_pkt_txlate_t *spx =
1631 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1632 
1633 	ASSERT(sata_pkt != NULL);
1634 
1635 	sata_free_local_buffer(spx);
1636 	sata_pkt_free(spx);
1637 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1638 
1639 }
1640 
1641 /*
1642  * sata_name_child is for composing the name of the node
1643  * the format of the name is "target,0".
1644  */
1645 static int
1646 sata_name_child(dev_info_t *dip, char *name, int namelen)
1647 {
1648 	int target;
1649 
1650 	target = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1651 	    DDI_PROP_DONTPASS, "target", -1);
1652 	if (target == -1)
1653 		return (DDI_FAILURE);
1654 	(void) snprintf(name, namelen, "%x,0", target);
1655 	return (DDI_SUCCESS);
1656 }
1657 
1658 
1659 
1660 /* ****************** SCSA required entry points *********************** */
1661 
1662 /*
1663  * Implementation of scsi tran_tgt_init.
1664  * sata_scsi_tgt_init() initializes scsi_device structure
1665  *
1666  * If successful, DDI_SUCCESS is returned.
1667  * DDI_FAILURE is returned if addressed device does not exist
1668  */
1669 
1670 static int
1671 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1672     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1673 {
1674 #ifndef __lock_lint
1675 	_NOTE(ARGUNUSED(hba_dip))
1676 	_NOTE(ARGUNUSED(tgt_dip))
1677 #endif
1678 	sata_device_t		sata_device;
1679 	sata_drive_info_t	*sdinfo;
1680 	struct sata_id		*sid;
1681 	sata_hba_inst_t		*sata_hba_inst;
1682 	char			model[SATA_ID_MODEL_LEN + 1];
1683 	char			fw[SATA_ID_FW_LEN + 1];
1684 	char			*vid, *pid;
1685 	int			i;
1686 
1687 	/*
1688 	 * Fail tran_tgt_init for .conf stub node
1689 	 */
1690 	if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
1691 		(void) ndi_merge_node(tgt_dip, sata_name_child);
1692 		ddi_set_name_addr(tgt_dip, NULL);
1693 		return (DDI_FAILURE);
1694 	}
1695 
1696 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
1697 
1698 	/* Validate scsi device address */
1699 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
1700 	    &sata_device) != 0)
1701 		return (DDI_FAILURE);
1702 
1703 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1704 	    sata_device.satadev_addr.cport)));
1705 
1706 	/* sata_device now contains a valid sata address */
1707 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
1708 	if (sdinfo == NULL) {
1709 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1710 		    sata_device.satadev_addr.cport)));
1711 		return (DDI_FAILURE);
1712 	}
1713 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1714 	    sata_device.satadev_addr.cport)));
1715 
1716 	/*
1717 	 * Check if we need to create a legacy devid (i.e cmdk style) for
1718 	 * the target disks.
1719 	 *
1720 	 * HBA devinfo node will have the property "use-cmdk-devid-format"
1721 	 * if we need to create cmdk-style devid for all the disk devices
1722 	 * attached to this controller. This property may have been set
1723 	 * from HBA driver's .conf file or by the HBA driver in its
1724 	 * attach(9F) function.
1725 	 */
1726 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
1727 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
1728 	    "use-cmdk-devid-format", 0) == 1)) {
1729 		/* register a legacy devid for this target node */
1730 		sata_target_devid_register(tgt_dip, sdinfo);
1731 	}
1732 
1733 
1734 	/*
1735 	 * 'Identify Device Data' does not always fit in standard SCSI
1736 	 * INQUIRY data, so establish INQUIRY_* properties with full-form
1737 	 * of information.
1738 	 */
1739 	sid = &sdinfo->satadrv_id;
1740 #ifdef	_LITTLE_ENDIAN
1741 	swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
1742 	swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
1743 #else	/* _LITTLE_ENDIAN */
1744 	bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN);
1745 	bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN);
1746 #endif	/* _LITTLE_ENDIAN */
1747 	model[SATA_ID_MODEL_LEN] = 0;
1748 	fw[SATA_ID_FW_LEN] = 0;
1749 
1750 	/* split model into into vid/pid */
1751 	for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++)
1752 		if ((*pid == ' ') || (*pid == '\t'))
1753 			break;
1754 	if (i < SATA_ID_MODEL_LEN) {
1755 		vid = model;
1756 		*pid++ = 0;		/* terminate vid, establish pid */
1757 	} else {
1758 		vid = NULL;		/* vid will stay "ATA     " */
1759 		pid = model;		/* model is all pid */
1760 	}
1761 
1762 	if (vid)
1763 		(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_VENDOR_ID,
1764 		    vid, strlen(vid));
1765 	if (pid)
1766 		(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID,
1767 		    pid, strlen(pid));
1768 	(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_REVISION_ID,
1769 	    fw, strlen(fw));
1770 
1771 	return (DDI_SUCCESS);
1772 }
1773 
1774 /*
1775  * Implementation of scsi tran_tgt_probe.
1776  * Probe target, by calling default scsi routine scsi_hba_probe()
1777  */
1778 static int
1779 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void))
1780 {
1781 	sata_hba_inst_t *sata_hba_inst =
1782 	    (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private);
1783 	int rval;
1784 
1785 	rval = scsi_hba_probe(sd, callback);
1786 
1787 	if (rval == SCSIPROBE_EXISTS) {
1788 		/*
1789 		 * Set property "pm-capable" on the target device node, so that
1790 		 * the target driver will not try to fetch scsi cycle counters
1791 		 * before enabling device power-management.
1792 		 */
1793 		if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
1794 		    "pm-capable", 1)) != DDI_PROP_SUCCESS) {
1795 			sata_log(sata_hba_inst, CE_WARN,
1796 			    "SATA device at port %d: "
1797 			    "will not be power-managed ",
1798 			    SCSI_TO_SATA_CPORT(sd->sd_address.a_target));
1799 			SATA_LOG_D((sata_hba_inst, CE_WARN,
1800 			    "failure updating pm-capable property"));
1801 		}
1802 	}
1803 	return (rval);
1804 }
1805 
1806 /*
1807  * Implementation of scsi tran_tgt_free.
1808  * Release all resources allocated for scsi_device
1809  */
1810 static void
1811 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1812     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1813 {
1814 #ifndef __lock_lint
1815 	_NOTE(ARGUNUSED(hba_dip))
1816 #endif
1817 	sata_device_t		sata_device;
1818 	sata_drive_info_t	*sdinfo;
1819 	sata_hba_inst_t		*sata_hba_inst;
1820 	ddi_devid_t		devid;
1821 
1822 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
1823 
1824 	/* Validate scsi device address */
1825 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
1826 	    &sata_device) != 0)
1827 		return;
1828 
1829 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1830 	    sata_device.satadev_addr.cport)));
1831 
1832 	/* sata_device now should contain a valid sata address */
1833 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
1834 	if (sdinfo == NULL) {
1835 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1836 		    sata_device.satadev_addr.cport)));
1837 		return;
1838 	}
1839 	/*
1840 	 * We did not allocate any resources in sata_scsi_tgt_init()
1841 	 * other than few properties.
1842 	 * Free them.
1843 	 */
1844 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1845 	    sata_device.satadev_addr.cport)));
1846 	(void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable");
1847 
1848 	/*
1849 	 * If devid was previously created but not freed up from
1850 	 * sd(7D) driver (i.e during detach(9F)) then do it here.
1851 	 */
1852 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
1853 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
1854 	    "use-cmdk-devid-format", 0) == 1) &&
1855 	    (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) {
1856 		ddi_devid_unregister(tgt_dip);
1857 		ddi_devid_free(devid);
1858 	}
1859 }
1860 
1861 /*
1862  * Implementation of scsi tran_init_pkt
1863  * Upon successful return, scsi pkt buffer has DMA resources allocated.
1864  *
1865  * It seems that we should always allocate pkt, even if the address is
1866  * for non-existing device - just use some default for dma_attr.
1867  * The reason is that there is no way to communicate this to a caller here.
1868  * Subsequent call to sata_scsi_start may fail appropriately.
1869  * Simply returning NULL does not seem to discourage a target driver...
1870  *
1871  * Returns a pointer to initialized scsi_pkt, or NULL otherwise.
1872  */
1873 static struct scsi_pkt *
1874 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
1875     struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
1876     int (*callback)(caddr_t), caddr_t arg)
1877 {
1878 	sata_hba_inst_t *sata_hba_inst =
1879 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
1880 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
1881 	sata_device_t sata_device;
1882 	sata_drive_info_t *sdinfo;
1883 	sata_pkt_txlate_t *spx;
1884 	ddi_dma_attr_t cur_dma_attr;
1885 	int rval;
1886 	boolean_t new_pkt = TRUE;
1887 
1888 	ASSERT(ap->a_hba_tran->tran_hba_dip == dip);
1889 
1890 	/*
1891 	 * We need to translate the address, even if it could be
1892 	 * a bogus one, for a non-existing device
1893 	 */
1894 	sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
1895 	sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target);
1896 	sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
1897 	sata_device.satadev_rev = SATA_DEVICE_REV;
1898 
1899 	if (pkt == NULL) {
1900 		/*
1901 		 * Have to allocate a brand new scsi packet.
1902 		 * We need to operate with auto request sense enabled.
1903 		 */
1904 		pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen,
1905 		    MAX(statuslen, sizeof (struct scsi_arq_status)),
1906 		    tgtlen, sizeof (sata_pkt_txlate_t), callback, arg);
1907 
1908 		if (pkt == NULL)
1909 			return (NULL);
1910 
1911 		/* Fill scsi packet structure */
1912 		pkt->pkt_comp		= (void (*)())NULL;
1913 		pkt->pkt_time		= 0;
1914 		pkt->pkt_resid		= 0;
1915 		pkt->pkt_statistics	= 0;
1916 		pkt->pkt_reason		= 0;
1917 
1918 		/*
1919 		 * pkt_hba_private will point to sata pkt txlate structure
1920 		 */
1921 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
1922 		bzero(spx, sizeof (sata_pkt_txlate_t));
1923 
1924 		spx->txlt_scsi_pkt = pkt;
1925 		spx->txlt_sata_hba_inst = sata_hba_inst;
1926 
1927 		/* Allocate sata_pkt */
1928 		spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback);
1929 		if (spx->txlt_sata_pkt == NULL) {
1930 			/* Could not allocate sata pkt */
1931 			scsi_hba_pkt_free(ap, pkt);
1932 			return (NULL);
1933 		}
1934 		/* Set sata address */
1935 		spx->txlt_sata_pkt->satapkt_device.satadev_addr =
1936 		    sata_device.satadev_addr;
1937 		spx->txlt_sata_pkt->satapkt_device.satadev_rev =
1938 		    sata_device.satadev_rev;
1939 
1940 		if ((bp == NULL) || (bp->b_bcount == 0))
1941 			return (pkt);
1942 
1943 		spx->txlt_total_residue = bp->b_bcount;
1944 	} else {
1945 		new_pkt = FALSE;
1946 		/*
1947 		 * Packet was preallocated/initialized by previous call
1948 		 */
1949 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
1950 
1951 		if ((bp == NULL) || (bp->b_bcount == 0)) {
1952 			return (pkt);
1953 		}
1954 
1955 		/* Pkt is available already: spx->txlt_scsi_pkt == pkt; */
1956 	}
1957 
1958 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
1959 
1960 	/*
1961 	 * We use an adjusted version of the dma_attr, to account
1962 	 * for device addressing limitations.
1963 	 * sata_adjust_dma_attr() will handle sdinfo == NULL which may
1964 	 * happen when a device is not yet configured.
1965 	 */
1966 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1967 	    sata_device.satadev_addr.cport)));
1968 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
1969 	    &spx->txlt_sata_pkt->satapkt_device);
1970 	/* NULL sdinfo may be passsed to sata_adjust_dma_attr() */
1971 	sata_adjust_dma_attr(sdinfo,
1972 	    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
1973 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1974 	    sata_device.satadev_addr.cport)));
1975 	/*
1976 	 * Allocate necessary DMA resources for the packet's data buffer
1977 	 * NOTE:
1978 	 * In case of read/write commands, DMA resource allocation here is
1979 	 * based on the premise that the transfer length specified in
1980 	 * the read/write scsi cdb will match exactly DMA resources -
1981 	 * returning correct packet residue is crucial.
1982 	 */
1983 	if ((rval = sata_dma_buf_setup(spx, flags, callback, arg,
1984 	    &cur_dma_attr)) != DDI_SUCCESS) {
1985 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
1986 		sata_pkt_free(spx);
1987 		/*
1988 		 * If a DMA allocation request fails with
1989 		 * DDI_DMA_NOMAPPING, indicate the error by calling
1990 		 * bioerror(9F) with bp and an error code of EFAULT.
1991 		 * If a DMA allocation request fails with
1992 		 * DDI_DMA_TOOBIG, indicate the error by calling
1993 		 * bioerror(9F) with bp and an error code of EINVAL.
1994 		 */
1995 		switch (rval) {
1996 		case DDI_DMA_NORESOURCES:
1997 			bioerror(bp, 0);
1998 			break;
1999 		case DDI_DMA_NOMAPPING:
2000 		case DDI_DMA_BADATTR:
2001 			bioerror(bp, EFAULT);
2002 			break;
2003 		case DDI_DMA_TOOBIG:
2004 		default:
2005 			bioerror(bp, EINVAL);
2006 			break;
2007 		}
2008 		if (new_pkt == TRUE)
2009 			scsi_hba_pkt_free(ap, pkt);
2010 		return (NULL);
2011 	}
2012 	/* Set number of bytes that are not yet accounted for */
2013 	pkt->pkt_resid = spx->txlt_total_residue;
2014 	ASSERT(pkt->pkt_resid >= 0);
2015 
2016 	return (pkt);
2017 }
2018 
2019 /*
2020  * Implementation of scsi tran_start.
2021  * Translate scsi cmd into sata operation and return status.
2022  * ATAPI CDBs are passed to ATAPI devices - the device determines what commands
2023  * are supported.
2024  * For SATA hard disks, supported scsi commands:
2025  * SCMD_INQUIRY
2026  * SCMD_TEST_UNIT_READY
2027  * SCMD_START_STOP
2028  * SCMD_READ_CAPACITY
2029  * SCMD_REQUEST_SENSE
2030  * SCMD_LOG_SENSE_G1
2031  * SCMD_LOG_SELECT_G1
2032  * SCMD_MODE_SENSE	(specific pages)
2033  * SCMD_MODE_SENSE_G1	(specific pages)
2034  * SCMD_MODE_SELECT	(specific pages)
2035  * SCMD_MODE_SELECT_G1	(specific pages)
2036  * SCMD_SYNCHRONIZE_CACHE
2037  * SCMD_SYNCHRONIZE_CACHE_G1
2038  * SCMD_READ
2039  * SCMD_READ_G1
2040  * SCMD_READ_G4
2041  * SCMD_READ_G5
2042  * SCMD_WRITE
2043  * SCMD_WRITE_BUFFER
2044  * SCMD_WRITE_G1
2045  * SCMD_WRITE_G4
2046  * SCMD_WRITE_G5
2047  * SCMD_SEEK		(noop)
2048  * SCMD_SDIAG
2049  *
2050  * All other commands are rejected as unsupported.
2051  *
2052  * Returns:
2053  * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver
2054  * for execution. TRAN_ACCEPT may be returned also if device was removed but
2055  * a callback could be scheduled.
2056  * TRAN_BADPKT if cmd was directed to invalid address.
2057  * TRAN_FATAL_ERROR is command was rejected due to hardware error, including
2058  * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device
2059  * was removed and there was no callback specified in scsi pkt.
2060  * TRAN_BUSY if command could not be executed becasue HBA driver or SATA
2061  * framework was busy performing some other operation(s).
2062  *
2063  */
2064 static int
2065 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
2066 {
2067 	sata_hba_inst_t *sata_hba_inst =
2068 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2069 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2070 	sata_drive_info_t *sdinfo;
2071 	struct buf *bp;
2072 	int cport;
2073 	int rval;
2074 
2075 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2076 	    "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]);
2077 
2078 	ASSERT(spx != NULL &&
2079 	    spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL);
2080 
2081 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
2082 
2083 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2084 	sdinfo = sata_get_device_info(sata_hba_inst,
2085 	    &spx->txlt_sata_pkt->satapkt_device);
2086 	if (sdinfo == NULL ||
2087 	    SATA_CPORT_INFO(sata_hba_inst, cport)->cport_tgtnode_clean ==
2088 	    B_FALSE ||
2089 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2090 
2091 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2092 		pkt->pkt_reason = CMD_DEV_GONE;
2093 		/*
2094 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2095 		 * only in callback function (for normal requests) and
2096 		 * in the dump code path.
2097 		 * So, if the callback is available, we need to do
2098 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2099 		 */
2100 		if (pkt->pkt_comp != NULL) {
2101 			/* scsi callback required */
2102 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2103 			    (task_func_t *)pkt->pkt_comp,
2104 			    (void *)pkt, TQ_SLEEP) == NULL)
2105 				/* Scheduling the callback failed */
2106 				return (TRAN_BUSY);
2107 			return (TRAN_ACCEPT);
2108 		}
2109 		/* No callback available */
2110 		return (TRAN_FATAL_ERROR);
2111 	}
2112 
2113 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
2114 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2115 		rval = sata_txlt_atapi(spx);
2116 		SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2117 		    "sata_scsi_start atapi: rval %d\n", rval);
2118 		return (rval);
2119 	}
2120 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2121 
2122 	/* ATA Disk commands processing starts here */
2123 
2124 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2125 
2126 	switch (pkt->pkt_cdbp[0]) {
2127 
2128 	case SCMD_INQUIRY:
2129 		/* Mapped to identify device */
2130 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2131 			bp_mapin(bp);
2132 		rval = sata_txlt_inquiry(spx);
2133 		break;
2134 
2135 	case SCMD_TEST_UNIT_READY:
2136 		/*
2137 		 * SAT "SATA to ATA Translation" doc specifies translation
2138 		 * to ATA CHECK POWER MODE.
2139 		 */
2140 		rval = sata_txlt_test_unit_ready(spx);
2141 		break;
2142 
2143 	case SCMD_START_STOP:
2144 		/* Mapping depends on the command */
2145 		rval = sata_txlt_start_stop_unit(spx);
2146 		break;
2147 
2148 	case SCMD_READ_CAPACITY:
2149 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2150 			bp_mapin(bp);
2151 		rval = sata_txlt_read_capacity(spx);
2152 		break;
2153 
2154 	case SCMD_REQUEST_SENSE:
2155 		/*
2156 		 * Always No Sense, since we force ARQ
2157 		 */
2158 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2159 			bp_mapin(bp);
2160 		rval = sata_txlt_request_sense(spx);
2161 		break;
2162 
2163 	case SCMD_LOG_SENSE_G1:
2164 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2165 			bp_mapin(bp);
2166 		rval = sata_txlt_log_sense(spx);
2167 		break;
2168 
2169 	case SCMD_LOG_SELECT_G1:
2170 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2171 			bp_mapin(bp);
2172 		rval = sata_txlt_log_select(spx);
2173 		break;
2174 
2175 	case SCMD_MODE_SENSE:
2176 	case SCMD_MODE_SENSE_G1:
2177 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2178 			bp_mapin(bp);
2179 		rval = sata_txlt_mode_sense(spx);
2180 		break;
2181 
2182 
2183 	case SCMD_MODE_SELECT:
2184 	case SCMD_MODE_SELECT_G1:
2185 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2186 			bp_mapin(bp);
2187 		rval = sata_txlt_mode_select(spx);
2188 		break;
2189 
2190 	case SCMD_SYNCHRONIZE_CACHE:
2191 	case SCMD_SYNCHRONIZE_CACHE_G1:
2192 		rval = sata_txlt_synchronize_cache(spx);
2193 		break;
2194 
2195 	case SCMD_READ:
2196 	case SCMD_READ_G1:
2197 	case SCMD_READ_G4:
2198 	case SCMD_READ_G5:
2199 		rval = sata_txlt_read(spx);
2200 		break;
2201 	case SCMD_WRITE_BUFFER:
2202 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2203 			bp_mapin(bp);
2204 		rval = sata_txlt_write_buffer(spx);
2205 		break;
2206 
2207 	case SCMD_WRITE:
2208 	case SCMD_WRITE_G1:
2209 	case SCMD_WRITE_G4:
2210 	case SCMD_WRITE_G5:
2211 		rval = sata_txlt_write(spx);
2212 		break;
2213 
2214 	case SCMD_SEEK:
2215 		rval = sata_txlt_nodata_cmd_immediate(spx);
2216 		break;
2217 
2218 		/* Other cases will be filed later */
2219 		/* postponed until phase 2 of the development */
2220 	default:
2221 		rval = sata_txlt_invalid_command(spx);
2222 		break;
2223 	}
2224 
2225 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2226 	    "sata_scsi_start: rval %d\n", rval);
2227 
2228 	return (rval);
2229 }
2230 
2231 /*
2232  * Implementation of scsi tran_abort.
2233  * Abort specific pkt or all packets.
2234  *
2235  * Returns 1 if one or more packets were aborted, returns 0 otherwise
2236  *
2237  * May be called from an interrupt level.
2238  */
2239 static int
2240 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt)
2241 {
2242 	sata_hba_inst_t *sata_hba_inst =
2243 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2244 	sata_device_t	sata_device;
2245 	sata_pkt_t	*sata_pkt;
2246 
2247 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2248 	    "sata_scsi_abort: %s at target: 0x%x\n",
2249 	    scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target);
2250 
2251 	/* Validate address */
2252 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0)
2253 		/* Invalid address */
2254 		return (0);
2255 
2256 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2257 	    sata_device.satadev_addr.cport)));
2258 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2259 		/* invalid address */
2260 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2261 		    sata_device.satadev_addr.cport)));
2262 		return (0);
2263 	}
2264 	if (scsi_pkt == NULL) {
2265 		/*
2266 		 * Abort all packets.
2267 		 * Although we do not have specific packet, we still need
2268 		 * dummy packet structure to pass device address to HBA.
2269 		 * Allocate one, without sleeping. Fail if pkt cannot be
2270 		 * allocated.
2271 		 */
2272 		sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP);
2273 		if (sata_pkt == NULL) {
2274 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2275 			    sata_device.satadev_addr.cport)));
2276 			SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: "
2277 			    "could not allocate sata_pkt"));
2278 			return (0);
2279 		}
2280 		sata_pkt->satapkt_rev = SATA_PKT_REV;
2281 		sata_pkt->satapkt_device = sata_device;
2282 		sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
2283 	} else {
2284 		if (scsi_pkt->pkt_ha_private == NULL) {
2285 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2286 			    sata_device.satadev_addr.cport)));
2287 			return (0); /* Bad scsi pkt */
2288 		}
2289 		/* extract pointer to sata pkt */
2290 		sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)->
2291 		    txlt_sata_pkt;
2292 	}
2293 
2294 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2295 	    sata_device.satadev_addr.cport)));
2296 	/* Send abort request to HBA */
2297 	if ((*SATA_ABORT_FUNC(sata_hba_inst))
2298 	    (SATA_DIP(sata_hba_inst), sata_pkt,
2299 	    scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) ==
2300 	    SATA_SUCCESS) {
2301 		if (scsi_pkt == NULL)
2302 			kmem_free(sata_pkt, sizeof (sata_pkt_t));
2303 		/* Success */
2304 		return (1);
2305 	}
2306 	/* Else, something did not go right */
2307 	if (scsi_pkt == NULL)
2308 		kmem_free(sata_pkt, sizeof (sata_pkt_t));
2309 	/* Failure */
2310 	return (0);
2311 }
2312 
2313 
2314 /*
2315  * Implementation of scsi tran_reset.
2316  * RESET_ALL request is translated into port reset.
2317  * RESET_TARGET requests is translated into a device reset,
2318  * RESET_LUN request is accepted only for LUN 0 and translated into
2319  * device reset.
2320  * The target reset should cause all HBA active and queued packets to
2321  * be terminated and returned with pkt reason SATA_PKT_RESET prior to
2322  * the return. HBA should report reset event for the device.
2323  *
2324  * Returns 1 upon success, 0 upon failure.
2325  */
2326 static int
2327 sata_scsi_reset(struct scsi_address *ap, int level)
2328 {
2329 	sata_hba_inst_t	*sata_hba_inst =
2330 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2331 	sata_device_t	sata_device;
2332 	int		val;
2333 
2334 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2335 	    "sata_scsi_reset: level %d target: 0x%x\n",
2336 	    level, ap->a_target);
2337 
2338 	/* Validate address */
2339 	val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device);
2340 	if (val == -1)
2341 		/* Invalid address */
2342 		return (0);
2343 
2344 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2345 	    sata_device.satadev_addr.cport)));
2346 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2347 		/* invalid address */
2348 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2349 		    sata_device.satadev_addr.cport)));
2350 		return (0);
2351 	}
2352 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2353 	    sata_device.satadev_addr.cport)));
2354 	if (level == RESET_ALL) {
2355 		/* port reset - cport only */
2356 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2357 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2358 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2359 			return (1);
2360 		else
2361 			return (0);
2362 
2363 	} else if (val == 0 &&
2364 	    (level == RESET_TARGET || level == RESET_LUN)) {
2365 		/* reset device (device attached) */
2366 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2367 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2368 			return (1);
2369 		else
2370 			return (0);
2371 	}
2372 	return (0);
2373 }
2374 
2375 
2376 /*
2377  * Implementation of scsi tran_getcap (get transport/device capabilities).
2378  * Supported capabilities for SATA hard disks:
2379  * auto-rqsense		(always supported)
2380  * tagged-qing		(supported if HBA supports it)
2381  * untagged-qing	(could be supported if disk supports it, but because
2382  *			 caching behavior allowing untagged queuing actually
2383  *			 results in reduced performance.  sd tries to throttle
2384  *			 back to only 3 outstanding commands, which may
2385  *			 work for real SCSI disks, but with read ahead
2386  *			 caching, having more than 1 outstanding command
2387  *			 results in cache thrashing.)
2388  * sector_size
2389  * dma_max
2390  * interconnect-type	(INTERCONNECT_SATA)
2391  *
2392  * Supported capabilities for ATAPI devices (CD/DVD):
2393  * auto-rqsense		(always supported)
2394  * sector_size
2395  * dma_max
2396  * interconnect-type	(INTERCONNECT_SATA)
2397  *
2398  * Request for other capabilities is rejected as unsupported.
2399  *
2400  * Returns supported capability value, or -1 if capability is unsuppported or
2401  * the address is invalid - no device.
2402  */
2403 
2404 static int
2405 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
2406 {
2407 
2408 	sata_hba_inst_t 	*sata_hba_inst =
2409 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2410 	sata_device_t		sata_device;
2411 	sata_drive_info_t	*sdinfo;
2412 	ddi_dma_attr_t		adj_dma_attr;
2413 	int 			rval;
2414 
2415 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2416 	    "sata_scsi_getcap: target: 0x%x, cap: %s\n",
2417 	    ap->a_target, cap);
2418 
2419 	/*
2420 	 * We want to process the capabilities on per port granularity.
2421 	 * So, we are specifically restricting ourselves to whom != 0
2422 	 * to exclude the controller wide handling.
2423 	 */
2424 	if (cap == NULL || whom == 0)
2425 		return (-1);
2426 
2427 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2428 		/* Invalid address */
2429 		return (-1);
2430 	}
2431 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2432 	    sata_device.satadev_addr.cport)));
2433 	if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) ==
2434 	    NULL) {
2435 		/* invalid address */
2436 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2437 		    sata_device.satadev_addr.cport)));
2438 		return (-1);
2439 	}
2440 
2441 	switch (scsi_hba_lookup_capstr(cap)) {
2442 	case SCSI_CAP_ARQ:
2443 		rval = 1;		/* ARQ supported, turned on */
2444 		break;
2445 
2446 	case SCSI_CAP_SECTOR_SIZE:
2447 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK)
2448 			rval = SATA_DISK_SECTOR_SIZE;	/* fixed size */
2449 		else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD)
2450 			rval = SATA_ATAPI_SECTOR_SIZE;
2451 		else rval = -1;
2452 		break;
2453 
2454 	/*
2455 	 * untagged queuing cause a performance inversion because of
2456 	 * the way sd operates.  Because of this reason we do not
2457 	 * use it when available.
2458 	 */
2459 	case SCSI_CAP_UNTAGGED_QING:
2460 		if (sdinfo->satadrv_features_enabled &
2461 		    SATA_DEV_F_E_UNTAGGED_QING)
2462 			rval = 1;	/* Untagged queuing available */
2463 		else
2464 			rval = -1;	/* Untagged queuing not available */
2465 		break;
2466 
2467 	case SCSI_CAP_TAGGED_QING:
2468 		if ((sdinfo->satadrv_features_enabled &
2469 		    SATA_DEV_F_E_TAGGED_QING) &&
2470 		    (sdinfo->satadrv_max_queue_depth > 1))
2471 			rval = 1;	/* Tagged queuing available */
2472 		else
2473 			rval = -1;	/* Tagged queuing not available */
2474 		break;
2475 
2476 	case SCSI_CAP_DMA_MAX:
2477 		sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst),
2478 		    &adj_dma_attr);
2479 		rval = (int)adj_dma_attr.dma_attr_maxxfer;
2480 		/* We rely on the fact that dma_attr_maxxfer < 0x80000000 */
2481 		break;
2482 
2483 	case SCSI_CAP_INTERCONNECT_TYPE:
2484 		rval = INTERCONNECT_SATA;	/* SATA interconnect type */
2485 		break;
2486 
2487 	default:
2488 		rval = -1;
2489 		break;
2490 	}
2491 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2492 	    sata_device.satadev_addr.cport)));
2493 	return (rval);
2494 }
2495 
2496 /*
2497  * Implementation of scsi tran_setcap
2498  *
2499  * Only SCSI_CAP_UNTAGGED_QING and  SCSI_CAP_TAGGED_QING are changeable.
2500  *
2501  */
2502 static int
2503 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
2504 {
2505 	sata_hba_inst_t	*sata_hba_inst =
2506 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2507 	sata_device_t	sata_device;
2508 	sata_drive_info_t	*sdinfo;
2509 	int		rval;
2510 
2511 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2512 	    "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap);
2513 
2514 	/*
2515 	 * We want to process the capabilities on per port granularity.
2516 	 * So, we are specifically restricting ourselves to whom != 0
2517 	 * to exclude the controller wide handling.
2518 	 */
2519 	if (cap == NULL || whom == 0) {
2520 		return (-1);
2521 	}
2522 
2523 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2524 		/* Invalid address */
2525 		return (-1);
2526 	}
2527 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2528 	    sata_device.satadev_addr.cport)));
2529 	if ((sdinfo = sata_get_device_info(sata_hba_inst,
2530 	    &sata_device)) == NULL) {
2531 		/* invalid address */
2532 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2533 		    sata_device.satadev_addr.cport)));
2534 		return (-1);
2535 	}
2536 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2537 	    sata_device.satadev_addr.cport)));
2538 
2539 	switch (scsi_hba_lookup_capstr(cap)) {
2540 	case SCSI_CAP_ARQ:
2541 	case SCSI_CAP_SECTOR_SIZE:
2542 	case SCSI_CAP_DMA_MAX:
2543 	case SCSI_CAP_INTERCONNECT_TYPE:
2544 		rval = 0;
2545 		break;
2546 	case SCSI_CAP_UNTAGGED_QING:
2547 		if (SATA_QDEPTH(sata_hba_inst) > 1) {
2548 			rval = 1;
2549 			if (value == 1) {
2550 				sdinfo->satadrv_features_enabled |=
2551 				    SATA_DEV_F_E_UNTAGGED_QING;
2552 			} else if (value == 0) {
2553 				sdinfo->satadrv_features_enabled &=
2554 				    ~SATA_DEV_F_E_UNTAGGED_QING;
2555 			} else {
2556 				rval = -1;
2557 			}
2558 		} else {
2559 			rval = 0;
2560 		}
2561 		break;
2562 	case SCSI_CAP_TAGGED_QING:
2563 		/* This can TCQ or NCQ */
2564 		if (sata_func_enable & SATA_ENABLE_QUEUING &&
2565 		    ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ &&
2566 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) ||
2567 		    (sata_func_enable & SATA_ENABLE_NCQ &&
2568 		    sdinfo->satadrv_features_support & SATA_DEV_F_NCQ &&
2569 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) &&
2570 		    (sdinfo->satadrv_max_queue_depth > 1)) {
2571 			rval = 1;
2572 			if (value == 1) {
2573 				sdinfo->satadrv_features_enabled |=
2574 				    SATA_DEV_F_E_TAGGED_QING;
2575 			} else if (value == 0) {
2576 				sdinfo->satadrv_features_enabled &=
2577 				    ~SATA_DEV_F_E_TAGGED_QING;
2578 			} else {
2579 				rval = -1;
2580 			}
2581 		} else {
2582 			rval = 0;
2583 		}
2584 		break;
2585 	default:
2586 		rval = -1;
2587 		break;
2588 	}
2589 	return (rval);
2590 }
2591 
2592 /*
2593  * Implementations of scsi tran_destroy_pkt.
2594  * Free resources allocated by sata_scsi_init_pkt()
2595  */
2596 static void
2597 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2598 {
2599 	sata_pkt_txlate_t *spx;
2600 
2601 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2602 
2603 	sata_common_free_dma_rsrcs(spx);
2604 
2605 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2606 	sata_pkt_free(spx);
2607 
2608 	scsi_hba_pkt_free(ap, pkt);
2609 }
2610 
2611 /*
2612  * Implementation of scsi tran_dmafree.
2613  * Free DMA resources allocated by sata_scsi_init_pkt()
2614  */
2615 
2616 static void
2617 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
2618 {
2619 #ifndef __lock_lint
2620 	_NOTE(ARGUNUSED(ap))
2621 #endif
2622 	sata_pkt_txlate_t *spx;
2623 
2624 	ASSERT(pkt != NULL);
2625 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2626 
2627 	sata_common_free_dma_rsrcs(spx);
2628 }
2629 
2630 /*
2631  * Implementation of scsi tran_sync_pkt.
2632  *
2633  * The assumption below is that pkt is unique - there is no need to check ap
2634  *
2635  * Synchronize DMA buffer and, if the intermediate buffer is used, copy data
2636  * into/from the real buffer.
2637  */
2638 static void
2639 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2640 {
2641 #ifndef __lock_lint
2642 	_NOTE(ARGUNUSED(ap))
2643 #endif
2644 	int rval;
2645 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2646 	struct buf *bp;
2647 	int direction;
2648 
2649 	ASSERT(spx != NULL);
2650 	if (spx->txlt_buf_dma_handle != NULL) {
2651 		direction = spx->txlt_sata_pkt->
2652 		    satapkt_cmd.satacmd_flags.sata_data_direction;
2653 		if (spx->txlt_sata_pkt != NULL &&
2654 		    direction != SATA_DIR_NODATA_XFER) {
2655 			if (spx->txlt_tmp_buf != NULL) {
2656 				/* Intermediate DMA buffer used */
2657 				bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2658 
2659 				if (direction & SATA_DIR_WRITE) {
2660 					bcopy(bp->b_un.b_addr,
2661 					    spx->txlt_tmp_buf, bp->b_bcount);
2662 				}
2663 			}
2664 			/* Sync the buffer for device or for CPU */
2665 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle,   0, 0,
2666 			    (direction & SATA_DIR_WRITE) ?
2667 			    DDI_DMA_SYNC_FORDEV :  DDI_DMA_SYNC_FORCPU);
2668 			ASSERT(rval == DDI_SUCCESS);
2669 			if (spx->txlt_tmp_buf != NULL &&
2670 			    !(direction & SATA_DIR_WRITE)) {
2671 				/* Intermediate DMA buffer used for read */
2672 				bcopy(spx->txlt_tmp_buf,
2673 				    bp->b_un.b_addr, bp->b_bcount);
2674 			}
2675 
2676 		}
2677 	}
2678 }
2679 
2680 
2681 
2682 /* *******************  SATA - SCSI Translation functions **************** */
2683 /*
2684  * SCSI to SATA pkt and command translation and SATA to SCSI status/error
2685  * translation.
2686  */
2687 
2688 /*
2689  * Checks if a device exists and can be access and translates common
2690  * scsi_pkt data to sata_pkt data.
2691  *
2692  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and
2693  * sata_pkt was set-up.
2694  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not
2695  * exist and pkt_comp callback was scheduled.
2696  * Returns other TRAN_XXXXX values when error occured and command should be
2697  * rejected with the returned TRAN_XXXXX value.
2698  *
2699  * This function should be called with port mutex held.
2700  */
2701 static int
2702 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason)
2703 {
2704 	sata_drive_info_t *sdinfo;
2705 	sata_device_t sata_device;
2706 	const struct sata_cmd_flags sata_initial_cmd_flags = {
2707 		SATA_DIR_NODATA_XFER,
2708 		/* all other values to 0/FALSE */
2709 	};
2710 	/*
2711 	 * Pkt_reason has to be set if the pkt_comp callback is invoked,
2712 	 * and that implies TRAN_ACCEPT return value. Any other returned value
2713 	 * indicates that the scsi packet was not accepted (the reason will not
2714 	 * be checked by the scsi target driver).
2715 	 * To make debugging easier, we set pkt_reason to know value here.
2716 	 * It may be changed later when different completion reason is
2717 	 * determined.
2718 	 */
2719 	spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
2720 	*reason = CMD_TRAN_ERR;
2721 
2722 	/* Validate address */
2723 	switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst,
2724 	    &spx->txlt_scsi_pkt->pkt_address, &sata_device)) {
2725 
2726 	case -1:
2727 		/* Invalid address or invalid device type */
2728 		return (TRAN_BADPKT);
2729 	case 1:
2730 		/* valid address but no device - it has disappeared ? */
2731 		spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE;
2732 		*reason = CMD_DEV_GONE;
2733 		/*
2734 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2735 		 * only in callback function (for normal requests) and
2736 		 * in the dump code path.
2737 		 * So, if the callback is available, we need to do
2738 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2739 		 */
2740 		if (spx->txlt_scsi_pkt->pkt_comp != NULL) {
2741 			/* scsi callback required */
2742 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2743 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2744 			    (void *)spx->txlt_scsi_pkt,
2745 			    TQ_SLEEP) == NULL)
2746 				/* Scheduling the callback failed */
2747 				return (TRAN_BUSY);
2748 
2749 			return (TRAN_ACCEPT);
2750 		}
2751 		return (TRAN_FATAL_ERROR);
2752 	default:
2753 		/* all OK; pkt reason will be overwritten later */
2754 		break;
2755 	}
2756 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2757 	    &spx->txlt_sata_pkt->satapkt_device);
2758 
2759 	/*
2760 	 * If device is in reset condition, reject the packet with
2761 	 * TRAN_BUSY, unless:
2762 	 * 1. system is panicking (dumping)
2763 	 * In such case only one thread is running and there is no way to
2764 	 * process reset.
2765 	 * 2. cfgadm operation is is progress (internal APCTL lock is set)
2766 	 * Some cfgadm operations involve drive commands, so reset condition
2767 	 * needs to be ignored for IOCTL operations.
2768 	 */
2769 	if ((sdinfo->satadrv_event_flags &
2770 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
2771 
2772 		if (!ddi_in_panic() &&
2773 		    ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst,
2774 		    sata_device.satadev_addr.cport) &
2775 		    SATA_APCTL_LOCK_PORT_BUSY) == 0)) {
2776 			spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
2777 			*reason = CMD_INCOMPLETE;
2778 			SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
2779 			    "sata_scsi_start: rejecting command because "
2780 			    "of device reset state\n", NULL);
2781 			return (TRAN_BUSY);
2782 		}
2783 	}
2784 
2785 	/*
2786 	 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by
2787 	 * sata_scsi_pkt_init() because pkt init had to work also with
2788 	 * non-existing devices.
2789 	 * Now we know that the packet was set-up for a real device, so its
2790 	 * type is known.
2791 	 */
2792 	spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type;
2793 
2794 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags;
2795 	if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst,
2796 	    sata_device.satadev_addr.cport)->cport_event_flags &
2797 	    SATA_APCTL_LOCK_PORT_BUSY) != 0) {
2798 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
2799 		    sata_ignore_dev_reset = B_TRUE;
2800 	}
2801 	/*
2802 	 * At this point the generic translation routine determined that the
2803 	 * scsi packet should be accepted. Packet completion reason may be
2804 	 * changed later when a different completion reason is determined.
2805 	 */
2806 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
2807 	*reason = CMD_CMPLT;
2808 
2809 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
2810 		/* Synchronous execution */
2811 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH |
2812 		    SATA_OPMODE_POLLING;
2813 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
2814 		    sata_ignore_dev_reset = ddi_in_panic();
2815 	} else {
2816 		/* Asynchronous execution */
2817 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH |
2818 		    SATA_OPMODE_INTERRUPTS;
2819 	}
2820 	/* Convert queuing information */
2821 	if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG)
2822 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag =
2823 		    B_TRUE;
2824 	else if (spx->txlt_scsi_pkt->pkt_flags &
2825 	    (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD))
2826 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag =
2827 		    B_TRUE;
2828 
2829 	/* Always limit pkt time */
2830 	if (spx->txlt_scsi_pkt->pkt_time == 0)
2831 		spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time;
2832 	else
2833 		/* Pass on scsi_pkt time */
2834 		spx->txlt_sata_pkt->satapkt_time =
2835 		    spx->txlt_scsi_pkt->pkt_time;
2836 
2837 	return (TRAN_ACCEPT);
2838 }
2839 
2840 
2841 /*
2842  * Translate ATA Identify Device data to SCSI Inquiry data.
2843  * This function may be called only for ATA devices.
2844  * This function should not be called for ATAPI devices - they
2845  * respond directly to SCSI Inquiry command.
2846  *
2847  * SATA Identify Device data has to be valid in sata_rive_info.
2848  * Buffer has to accomodate the inquiry length (36 bytes).
2849  *
2850  * This function should be called with a port mutex held.
2851  */
2852 static	void
2853 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst,
2854     sata_drive_info_t *sdinfo, uint8_t *buf)
2855 {
2856 
2857 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
2858 	struct sata_id *sid = &sdinfo->satadrv_id;
2859 
2860 	/* Start with a nice clean slate */
2861 	bzero((void *)inq, sizeof (struct scsi_inquiry));
2862 
2863 	/*
2864 	 * Rely on the dev_type for setting paripheral qualifier.
2865 	 * Assume that  DTYPE_RODIRECT applies to CD/DVD R/W devices.
2866 	 * It could be that DTYPE_OPTICAL could also qualify in the future.
2867 	 * ATAPI Inquiry may provide more data to the target driver.
2868 	 */
2869 	inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
2870 	    DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */
2871 
2872 	inq->inq_rmb = sid->ai_config & SATA_REM_MEDIA ? 1 : 0;
2873 	inq->inq_qual = 0;	/* Device type qualifier (obsolete in SCSI3? */
2874 	inq->inq_iso = 0;	/* ISO version */
2875 	inq->inq_ecma = 0;	/* ECMA version */
2876 	inq->inq_ansi = 3;	/* ANSI version - SCSI 3 */
2877 	inq->inq_aenc = 0;	/* Async event notification cap. */
2878 	inq->inq_trmiop = 0;	/* Supports TERMINATE I/O PROC msg - NO */
2879 	inq->inq_normaca = 0;	/* setting NACA bit supported - NO */
2880 	inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */
2881 	inq->inq_len = 31;	/* Additional length */
2882 	inq->inq_dualp = 0;	/* dual port device - NO */
2883 	inq->inq_reladdr = 0;	/* Supports relative addressing - NO */
2884 	inq->inq_sync = 0;	/* Supports synchronous data xfers - NO */
2885 	inq->inq_linked = 0;	/* Supports linked commands - NO */
2886 				/*
2887 				 * Queuing support - controller has to
2888 				 * support some sort of command queuing.
2889 				 */
2890 	if (SATA_QDEPTH(sata_hba_inst) > 1)
2891 		inq->inq_cmdque = 1; /* Supports command queueing - YES */
2892 	else
2893 		inq->inq_cmdque = 0; /* Supports command queueing - NO */
2894 	inq->inq_sftre = 0;	/* Supports Soft Reset option - NO ??? */
2895 	inq->inq_wbus32 = 0;	/* Supports 32 bit wide data xfers - NO */
2896 	inq->inq_wbus16 = 0;	/* Supports 16 bit wide data xfers - NO */
2897 
2898 #ifdef	_LITTLE_ENDIAN
2899 	/* Swap text fields to match SCSI format */
2900 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
2901 	swab(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
2902 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
2903 		swab(sid->ai_fw, inq->inq_revision, 4);	/* Revision level */
2904 	else
2905 		swab(&sid->ai_fw[4], inq->inq_revision, 4);	/* Rev. level */
2906 #else	/* _LITTLE_ENDIAN */
2907 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
2908 	bcopy(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
2909 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
2910 		bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
2911 	else
2912 		bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
2913 #endif	/* _LITTLE_ENDIAN */
2914 }
2915 
2916 
2917 /*
2918  * Scsi response set up for invalid command (command not supported)
2919  *
2920  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
2921  */
2922 static int
2923 sata_txlt_invalid_command(sata_pkt_txlate_t *spx)
2924 {
2925 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
2926 	struct scsi_extended_sense *sense;
2927 
2928 	scsipkt->pkt_reason = CMD_CMPLT;
2929 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
2930 	    STATE_SENT_CMD | STATE_GOT_STATUS;
2931 
2932 	*scsipkt->pkt_scbp = STATUS_CHECK;
2933 
2934 	sense = sata_arq_sense(spx);
2935 	sense->es_key = KEY_ILLEGAL_REQUEST;
2936 	sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE;
2937 
2938 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
2939 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
2940 
2941 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
2942 	    scsipkt->pkt_comp != NULL)
2943 		/* scsi callback required */
2944 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2945 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2946 		    (void *)spx->txlt_scsi_pkt,
2947 		    TQ_SLEEP) == NULL)
2948 			/* Scheduling the callback failed */
2949 			return (TRAN_BUSY);
2950 	return (TRAN_ACCEPT);
2951 }
2952 
2953 /*
2954  * Scsi response setup for
2955  * emulated non-data command that requires no action/return data
2956  *
2957  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
2958  */
2959 static 	int
2960 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx)
2961 {
2962 	int rval;
2963 	int reason;
2964 
2965 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
2966 
2967 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
2968 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
2969 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
2970 		return (rval);
2971 	}
2972 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
2973 
2974 	spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
2975 	    STATE_SENT_CMD | STATE_GOT_STATUS;
2976 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
2977 	*(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD;
2978 
2979 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
2980 	    "Scsi_pkt completion reason %x\n",
2981 	    spx->txlt_scsi_pkt->pkt_reason);
2982 
2983 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 &&
2984 	    spx->txlt_scsi_pkt->pkt_comp != NULL)
2985 		/* scsi callback required */
2986 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2987 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2988 		    (void *)spx->txlt_scsi_pkt,
2989 		    TQ_SLEEP) == NULL)
2990 			/* Scheduling the callback failed */
2991 			return (TRAN_BUSY);
2992 	return (TRAN_ACCEPT);
2993 }
2994 
2995 
2996 /*
2997  * SATA translate command: Inquiry / Identify Device
2998  * Use cached Identify Device data for now, rather than issuing actual
2999  * Device Identify cmd request. If device is detached and re-attached,
3000  * asynchromous event processing should fetch and refresh Identify Device
3001  * data.
3002  * Two VPD pages are supported now:
3003  * Vital Product Data page
3004  * Unit Serial Number page
3005  *
3006  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3007  */
3008 
3009 #define	EVPD			1	/* Extended Vital Product Data flag */
3010 #define	CMDDT			2	/* Command Support Data - Obsolete */
3011 #define	INQUIRY_SUP_VPD_PAGE	0	/* Supported VDP Pages Page COde */
3012 #define	INQUIRY_USN_PAGE	0x80	/* Unit Serial Number Page Code */
3013 #define	INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */
3014 
3015 static int
3016 sata_txlt_inquiry(sata_pkt_txlate_t *spx)
3017 {
3018 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3019 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3020 	sata_drive_info_t *sdinfo;
3021 	struct scsi_extended_sense *sense;
3022 	int count;
3023 	uint8_t *p;
3024 	int i, j;
3025 	uint8_t page_buf[0xff]; /* Max length */
3026 	int rval, reason;
3027 
3028 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3029 
3030 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3031 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3032 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3033 		return (rval);
3034 	}
3035 
3036 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3037 	    &spx->txlt_sata_pkt->satapkt_device);
3038 
3039 	ASSERT(sdinfo != NULL);
3040 
3041 	scsipkt->pkt_reason = CMD_CMPLT;
3042 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3043 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3044 
3045 	/* Reject not supported request */
3046 	if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */
3047 		*scsipkt->pkt_scbp = STATUS_CHECK;
3048 		sense = sata_arq_sense(spx);
3049 		sense->es_key = KEY_ILLEGAL_REQUEST;
3050 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3051 		goto done;
3052 	}
3053 
3054 	/* Valid Inquiry request */
3055 	*scsipkt->pkt_scbp = STATUS_GOOD;
3056 
3057 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3058 
3059 		/*
3060 		 * Because it is fully emulated command storing data
3061 		 * programatically in the specified buffer, release
3062 		 * preallocated DMA resources before storing data in the buffer,
3063 		 * so no unwanted DMA sync would take place.
3064 		 */
3065 		sata_scsi_dmafree(NULL, scsipkt);
3066 
3067 		if (!(scsipkt->pkt_cdbp[1] & EVPD)) {
3068 			/* Standard Inquiry Data request */
3069 			struct scsi_inquiry inq;
3070 			unsigned int bufsize;
3071 
3072 			sata_identdev_to_inquiry(spx->txlt_sata_hba_inst,
3073 			    sdinfo, (uint8_t *)&inq);
3074 			/* Copy no more than requested */
3075 			count = MIN(bp->b_bcount,
3076 			    sizeof (struct scsi_inquiry));
3077 			bufsize = scsipkt->pkt_cdbp[4];
3078 			bufsize |= scsipkt->pkt_cdbp[3] << 8;
3079 			count = MIN(count, bufsize);
3080 			bcopy(&inq, bp->b_un.b_addr, count);
3081 
3082 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
3083 			scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3084 			    bufsize - count : 0;
3085 		} else {
3086 			/*
3087 			 * peripheral_qualifier = 0;
3088 			 *
3089 			 * We are dealing only with HD and will be
3090 			 * dealing with CD/DVD devices soon
3091 			 */
3092 			uint8_t peripheral_device_type =
3093 			    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3094 			    DTYPE_DIRECT : DTYPE_RODIRECT;
3095 
3096 			switch ((uint_t)scsipkt->pkt_cdbp[2]) {
3097 			case INQUIRY_SUP_VPD_PAGE:
3098 				/*
3099 				 * Request for suported Vital Product Data
3100 				 * pages - assuming only 2 page codes
3101 				 * supported
3102 				 */
3103 				page_buf[0] = peripheral_device_type;
3104 				page_buf[1] = INQUIRY_SUP_VPD_PAGE;
3105 				page_buf[2] = 0;
3106 				page_buf[3] = 2; /* page length */
3107 				page_buf[4] = INQUIRY_SUP_VPD_PAGE;
3108 				page_buf[5] = INQUIRY_USN_PAGE;
3109 				/* Copy no more than requested */
3110 				count = MIN(bp->b_bcount, 6);
3111 				bcopy(page_buf, bp->b_un.b_addr, count);
3112 				break;
3113 			case INQUIRY_USN_PAGE:
3114 				/*
3115 				 * Request for Unit Serial Number page
3116 				 */
3117 				page_buf[0] = peripheral_device_type;
3118 				page_buf[1] = INQUIRY_USN_PAGE;
3119 				page_buf[2] = 0;
3120 				page_buf[3] = 20; /* remaining page length */
3121 				p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser);
3122 #ifdef	_LITTLE_ENDIAN
3123 				swab(p, &page_buf[4], 20);
3124 #else
3125 				bcopy(p, &page_buf[4], 20);
3126 #endif
3127 				for (i = 0; i < 20; i++) {
3128 					if (page_buf[4 + i] == '\0' ||
3129 					    page_buf[4 + i] == '\040') {
3130 						break;
3131 					}
3132 				}
3133 				/*
3134 				 * 'i' contains string length.
3135 				 *
3136 				 * Least significant character of the serial
3137 				 * number shall appear as the last byte,
3138 				 * according to SBC-3 spec.
3139 				 */
3140 				p = &page_buf[20 + 4 - 1];
3141 				for (j = i; j > 0; j--, p--) {
3142 					*p = *(p - 20 + i);
3143 				}
3144 				p = &page_buf[4];
3145 				for (j = 20 - i; j > 0; j--) {
3146 					*p++ = '\040';
3147 				}
3148 				count = MIN(bp->b_bcount, 24);
3149 				bcopy(page_buf, bp->b_un.b_addr, count);
3150 				break;
3151 
3152 			case INQUIRY_DEV_IDENTIFICATION_PAGE:
3153 				/*
3154 				 * We may want to implement this page, when
3155 				 * identifiers are common for SATA devices
3156 				 * But not now.
3157 				 */
3158 				/*FALLTHROUGH*/
3159 
3160 			default:
3161 				/* Request for unsupported VPD page */
3162 				*scsipkt->pkt_scbp = STATUS_CHECK;
3163 				sense = sata_arq_sense(spx);
3164 				sense->es_key = KEY_ILLEGAL_REQUEST;
3165 				sense->es_add_code =
3166 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3167 				goto done;
3168 			}
3169 		}
3170 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3171 		scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3172 		    scsipkt->pkt_cdbp[4] - count : 0;
3173 	}
3174 done:
3175 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3176 
3177 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3178 	    "Scsi_pkt completion reason %x\n",
3179 	    scsipkt->pkt_reason);
3180 
3181 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3182 	    scsipkt->pkt_comp != NULL) {
3183 		/* scsi callback required */
3184 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3185 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3186 		    TQ_SLEEP) == NULL)
3187 			/* Scheduling the callback failed */
3188 			return (TRAN_BUSY);
3189 	}
3190 	return (TRAN_ACCEPT);
3191 }
3192 
3193 /*
3194  * SATA translate command: Request Sense.
3195  * Emulated command (ATA version for SATA hard disks)
3196  * Always NO SENSE, because any sense data should be reported by ARQ sense.
3197  *
3198  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3199  */
3200 static int
3201 sata_txlt_request_sense(sata_pkt_txlate_t *spx)
3202 {
3203 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3204 	struct scsi_extended_sense sense;
3205 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3206 	int rval, reason;
3207 
3208 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3209 
3210 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3211 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3212 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3213 		return (rval);
3214 	}
3215 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3216 
3217 
3218 	scsipkt->pkt_reason = CMD_CMPLT;
3219 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3220 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3221 	*scsipkt->pkt_scbp = STATUS_GOOD;
3222 
3223 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3224 		/*
3225 		 * Because it is fully emulated command storing data
3226 		 * programatically in the specified buffer, release
3227 		 * preallocated DMA resources before storing data in the buffer,
3228 		 * so no unwanted DMA sync would take place.
3229 		 */
3230 		int count = MIN(bp->b_bcount,
3231 		    sizeof (struct scsi_extended_sense));
3232 		sata_scsi_dmafree(NULL, scsipkt);
3233 		bzero(&sense, sizeof (struct scsi_extended_sense));
3234 		sense.es_valid = 0;	/* Valid LBA */
3235 		sense.es_class = 7;	/* Response code 0x70 - current err */
3236 		sense.es_key = KEY_NO_SENSE;
3237 		sense.es_add_len = 6;	/* Additional length */
3238 		/* Copy no more than requested */
3239 		bcopy(&sense, bp->b_un.b_addr, count);
3240 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3241 		scsipkt->pkt_resid = 0;
3242 	}
3243 
3244 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3245 	    "Scsi_pkt completion reason %x\n",
3246 	    scsipkt->pkt_reason);
3247 
3248 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3249 	    scsipkt->pkt_comp != NULL)
3250 		/* scsi callback required */
3251 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3252 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3253 		    TQ_SLEEP) == NULL)
3254 			/* Scheduling the callback failed */
3255 			return (TRAN_BUSY);
3256 	return (TRAN_ACCEPT);
3257 }
3258 
3259 /*
3260  * SATA translate command: Test Unit Ready
3261  * At the moment this is an emulated command (ATA version for SATA hard disks).
3262  * May be translated into Check Power Mode command in the future
3263  *
3264  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3265  */
3266 static int
3267 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx)
3268 {
3269 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3270 	struct scsi_extended_sense *sense;
3271 	int power_state;
3272 	int rval, reason;
3273 
3274 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3275 
3276 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3277 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3278 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3279 		return (rval);
3280 	}
3281 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3282 
3283 	/* At this moment, emulate it rather than execute anything */
3284 	power_state = SATA_PWRMODE_ACTIVE;
3285 
3286 	scsipkt->pkt_reason = CMD_CMPLT;
3287 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3288 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3289 
3290 	switch (power_state) {
3291 	case SATA_PWRMODE_ACTIVE:
3292 	case SATA_PWRMODE_IDLE:
3293 		*scsipkt->pkt_scbp = STATUS_GOOD;
3294 		break;
3295 	default:
3296 		/* PWR mode standby */
3297 		*scsipkt->pkt_scbp = STATUS_CHECK;
3298 		sense = sata_arq_sense(spx);
3299 		sense->es_key = KEY_NOT_READY;
3300 		sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY;
3301 		break;
3302 	}
3303 
3304 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3305 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3306 
3307 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3308 	    scsipkt->pkt_comp != NULL)
3309 		/* scsi callback required */
3310 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3311 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3312 		    TQ_SLEEP) == NULL)
3313 			/* Scheduling the callback failed */
3314 			return (TRAN_BUSY);
3315 
3316 	return (TRAN_ACCEPT);
3317 }
3318 
3319 
3320 /*
3321  * SATA translate command: Start Stop Unit
3322  * Translation depends on a command:
3323  *	Start Unit translated into Idle Immediate
3324  *	Stop Unit translated into Standby Immediate
3325  *	Unload Media / NOT SUPPORTED YET
3326  *	Load Media / NOT SUPPROTED YET
3327  * Power condition bits are ignored, so is Immediate bit
3328  * Requesting synchronous execution.
3329  *
3330  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
3331  * appropriate values in scsi_pkt fields.
3332  */
3333 static int
3334 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx)
3335 {
3336 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3337 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3338 	struct scsi_extended_sense *sense;
3339 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3340 	int cport = SATA_TXLT_CPORT(spx);
3341 	int rval, reason;
3342 	int synch;
3343 
3344 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3345 	    "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1);
3346 
3347 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3348 
3349 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3350 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3351 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3352 		return (rval);
3353 	}
3354 
3355 	if (scsipkt->pkt_cdbp[4] & 2) {
3356 		/* Load/Unload Media - invalid request */
3357 		*scsipkt->pkt_scbp = STATUS_CHECK;
3358 		sense = sata_arq_sense(spx);
3359 		sense->es_key = KEY_ILLEGAL_REQUEST;
3360 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3361 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3362 
3363 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3364 		    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3365 
3366 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3367 		    scsipkt->pkt_comp != NULL)
3368 			/* scsi callback required */
3369 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3370 			    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3371 			    TQ_SLEEP) == NULL)
3372 				/* Scheduling the callback failed */
3373 				return (TRAN_BUSY);
3374 
3375 		return (TRAN_ACCEPT);
3376 	}
3377 	scmd->satacmd_addr_type = 0;
3378 	scmd->satacmd_sec_count_lsb = 0;
3379 	scmd->satacmd_lba_low_lsb = 0;
3380 	scmd->satacmd_lba_mid_lsb = 0;
3381 	scmd->satacmd_lba_high_lsb = 0;
3382 	scmd->satacmd_features_reg = 0;
3383 	scmd->satacmd_device_reg = 0;
3384 	scmd->satacmd_status_reg = 0;
3385 	if (scsipkt->pkt_cdbp[4] & 1) {
3386 		/* Start Unit */
3387 		scmd->satacmd_cmd_reg = SATAC_IDLE_IM;
3388 	} else {
3389 		/* Stop Unit */
3390 		scmd->satacmd_cmd_reg = SATAC_STANDBY_IM;
3391 	}
3392 
3393 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
3394 		/* Need to set-up a callback function */
3395 		spx->txlt_sata_pkt->satapkt_comp =
3396 		    sata_txlt_nodata_cmd_completion;
3397 		synch = FALSE;
3398 	} else {
3399 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
3400 		synch = TRUE;
3401 	}
3402 
3403 	/* Transfer command to HBA */
3404 	if (sata_hba_start(spx, &rval) != 0) {
3405 		/* Pkt not accepted for execution */
3406 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3407 		return (rval);
3408 	}
3409 
3410 	/*
3411 	 * If execution is non-synchronous,
3412 	 * a callback function will handle potential errors, translate
3413 	 * the response and will do a callback to a target driver.
3414 	 * If it was synchronous, check execution status using the same
3415 	 * framework callback.
3416 	 */
3417 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3418 	if (synch) {
3419 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3420 		    "synchronous execution status %x\n",
3421 		    spx->txlt_sata_pkt->satapkt_reason);
3422 
3423 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
3424 	}
3425 	return (TRAN_ACCEPT);
3426 
3427 }
3428 
3429 
3430 /*
3431  * SATA translate command:  Read Capacity.
3432  * Emulated command for SATA disks.
3433  * Capacity is retrieved from cached Idenifty Device data.
3434  * Identify Device data shows effective disk capacity, not the native
3435  * capacity, which may be limitted by Set Max Address command.
3436  * This is ATA version for SATA hard disks.
3437  *
3438  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3439  */
3440 static int
3441 sata_txlt_read_capacity(sata_pkt_txlate_t *spx)
3442 {
3443 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3444 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3445 	sata_drive_info_t *sdinfo;
3446 	uint64_t val;
3447 	uchar_t *rbuf;
3448 	int rval, reason;
3449 
3450 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3451 	    "sata_txlt_read_capacity: ", NULL);
3452 
3453 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3454 
3455 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3456 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3457 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3458 		return (rval);
3459 	}
3460 
3461 	scsipkt->pkt_reason = CMD_CMPLT;
3462 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3463 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3464 	*scsipkt->pkt_scbp = STATUS_GOOD;
3465 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3466 		/*
3467 		 * Because it is fully emulated command storing data
3468 		 * programatically in the specified buffer, release
3469 		 * preallocated DMA resources before storing data in the buffer,
3470 		 * so no unwanted DMA sync would take place.
3471 		 */
3472 		sata_scsi_dmafree(NULL, scsipkt);
3473 
3474 		sdinfo = sata_get_device_info(
3475 		    spx->txlt_sata_hba_inst,
3476 		    &spx->txlt_sata_pkt->satapkt_device);
3477 		/* Last logical block address */
3478 		val = sdinfo->satadrv_capacity - 1;
3479 		rbuf = (uchar_t *)bp->b_un.b_addr;
3480 		/* Need to swap endians to match scsi format */
3481 		rbuf[0] = (val >> 24) & 0xff;
3482 		rbuf[1] = (val >> 16) & 0xff;
3483 		rbuf[2] = (val >> 8) & 0xff;
3484 		rbuf[3] = val & 0xff;
3485 		/* block size - always 512 bytes, for now */
3486 		rbuf[4] = 0;
3487 		rbuf[5] = 0;
3488 		rbuf[6] = 0x02;
3489 		rbuf[7] = 0;
3490 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3491 		scsipkt->pkt_resid = 0;
3492 
3493 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n",
3494 		    sdinfo->satadrv_capacity -1);
3495 	}
3496 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3497 	/*
3498 	 * If a callback was requested, do it now.
3499 	 */
3500 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3501 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3502 
3503 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3504 	    scsipkt->pkt_comp != NULL)
3505 		/* scsi callback required */
3506 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3507 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3508 		    TQ_SLEEP) == NULL)
3509 			/* Scheduling the callback failed */
3510 			return (TRAN_BUSY);
3511 
3512 	return (TRAN_ACCEPT);
3513 }
3514 
3515 /*
3516  * SATA translate command: Mode Sense.
3517  * Translated into appropriate SATA command or emulated.
3518  * Saved Values Page Control (03) are not supported.
3519  *
3520  * NOTE: only caching mode sense page is currently implemented.
3521  *
3522  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3523  */
3524 
3525 static int
3526 sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
3527 {
3528 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
3529 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3530 	sata_drive_info_t *sdinfo;
3531 	sata_id_t *sata_id;
3532 	struct scsi_extended_sense *sense;
3533 	int 		len, bdlen, count, alc_len;
3534 	int		pc;	/* Page Control code */
3535 	uint8_t		*buf;	/* mode sense buffer */
3536 	int		rval, reason;
3537 
3538 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3539 	    "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
3540 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
3541 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
3542 
3543 	buf = kmem_zalloc(1024, KM_SLEEP);
3544 
3545 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3546 
3547 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3548 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3549 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3550 		kmem_free(buf, 1024);
3551 		return (rval);
3552 	}
3553 
3554 	scsipkt->pkt_reason = CMD_CMPLT;
3555 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3556 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3557 
3558 	pc = scsipkt->pkt_cdbp[2] >> 6;
3559 
3560 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3561 		/*
3562 		 * Because it is fully emulated command storing data
3563 		 * programatically in the specified buffer, release
3564 		 * preallocated DMA resources before storing data in the buffer,
3565 		 * so no unwanted DMA sync would take place.
3566 		 */
3567 		sata_scsi_dmafree(NULL, scsipkt);
3568 
3569 		len = 0;
3570 		bdlen = 0;
3571 		if (!(scsipkt->pkt_cdbp[1] & 8)) {
3572 			if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
3573 			    (scsipkt->pkt_cdbp[0] & 0x10))
3574 				bdlen = 16;
3575 			else
3576 				bdlen = 8;
3577 		}
3578 		/* Build mode parameter header */
3579 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3580 			/* 4-byte mode parameter header */
3581 			buf[len++] = 0;   	/* mode data length */
3582 			buf[len++] = 0;		/* medium type */
3583 			buf[len++] = 0;		/* dev-specific param */
3584 			buf[len++] = bdlen;	/* Block Descriptor length */
3585 		} else {
3586 			/* 8-byte mode parameter header */
3587 			buf[len++] = 0;		/* mode data length */
3588 			buf[len++] = 0;
3589 			buf[len++] = 0;		/* medium type */
3590 			buf[len++] = 0;		/* dev-specific param */
3591 			if (bdlen == 16)
3592 				buf[len++] = 1;	/* long lba descriptor */
3593 			else
3594 				buf[len++] = 0;
3595 			buf[len++] = 0;
3596 			buf[len++] = 0;		/* Block Descriptor length */
3597 			buf[len++] = bdlen;
3598 		}
3599 
3600 		sdinfo = sata_get_device_info(
3601 		    spx->txlt_sata_hba_inst,
3602 		    &spx->txlt_sata_pkt->satapkt_device);
3603 
3604 		/* Build block descriptor only if not disabled (DBD) */
3605 		if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
3606 			/* Block descriptor - direct-access device format */
3607 			if (bdlen == 8) {
3608 				/* build regular block descriptor */
3609 				buf[len++] =
3610 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
3611 				buf[len++] =
3612 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
3613 				buf[len++] =
3614 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
3615 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
3616 				buf[len++] = 0; /* density code */
3617 				buf[len++] = 0;
3618 				if (sdinfo->satadrv_type ==
3619 				    SATA_DTYPE_ATADISK)
3620 					buf[len++] = 2;
3621 				else
3622 					/* ATAPI */
3623 					buf[len++] = 8;
3624 				buf[len++] = 0;
3625 			} else if (bdlen == 16) {
3626 				/* Long LBA Accepted */
3627 				/* build long lba block descriptor */
3628 #ifndef __lock_lint
3629 				buf[len++] =
3630 				    (sdinfo->satadrv_capacity >> 56) & 0xff;
3631 				buf[len++] =
3632 				    (sdinfo->satadrv_capacity >> 48) & 0xff;
3633 				buf[len++] =
3634 				    (sdinfo->satadrv_capacity >> 40) & 0xff;
3635 				buf[len++] =
3636 				    (sdinfo->satadrv_capacity >> 32) & 0xff;
3637 #endif
3638 				buf[len++] =
3639 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
3640 				buf[len++] =
3641 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
3642 				buf[len++] =
3643 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
3644 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
3645 				buf[len++] = 0;
3646 				buf[len++] = 0; /* density code */
3647 				buf[len++] = 0;
3648 				buf[len++] = 0;
3649 				if (sdinfo->satadrv_type ==
3650 				    SATA_DTYPE_ATADISK)
3651 					buf[len++] = 2;
3652 				else
3653 					/* ATAPI */
3654 					buf[len++] = 8;
3655 				buf[len++] = 0;
3656 			}
3657 		}
3658 
3659 		sata_id = &sdinfo->satadrv_id;
3660 
3661 		/*
3662 		 * Add requested pages.
3663 		 * Page 3 and 4 are obsolete and we are not supporting them.
3664 		 * We deal now with:
3665 		 * caching (read/write cache control).
3666 		 * We should eventually deal with following mode pages:
3667 		 * error recovery  (0x01),
3668 		 * power condition (0x1a),
3669 		 * exception control page (enables SMART) (0x1c),
3670 		 * enclosure management (ses),
3671 		 * protocol-specific port mode (port control).
3672 		 */
3673 		switch (scsipkt->pkt_cdbp[2] & 0x3f) {
3674 		case MODEPAGE_RW_ERRRECOV:
3675 			/* DAD_MODE_ERR_RECOV */
3676 			/* R/W recovery */
3677 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
3678 			break;
3679 		case MODEPAGE_CACHING:
3680 			/* DAD_MODE_CACHE */
3681 			/* Reject not supported request for saved parameters */
3682 			if (pc == 3) {
3683 				*scsipkt->pkt_scbp = STATUS_CHECK;
3684 				sense = sata_arq_sense(spx);
3685 				sense->es_key = KEY_ILLEGAL_REQUEST;
3686 				sense->es_add_code =
3687 				    SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
3688 				goto done;
3689 			}
3690 
3691 			/* caching */
3692 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
3693 			break;
3694 		case MODEPAGE_INFO_EXCPT:
3695 			/* exception cntrl */
3696 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
3697 				len += sata_build_msense_page_1c(sdinfo, pc,
3698 				    buf+len);
3699 			}
3700 			else
3701 				goto err;
3702 			break;
3703 		case MODEPAGE_POWER_COND:
3704 			/* DAD_MODE_POWER_COND */
3705 			/* power condition */
3706 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
3707 			break;
3708 
3709 		case MODEPAGE_ACOUSTIC_MANAG:
3710 			/* acoustic management */
3711 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
3712 			break;
3713 		case MODEPAGE_ALLPAGES:
3714 			/* all pages */
3715 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
3716 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
3717 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
3718 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
3719 				len += sata_build_msense_page_1c(sdinfo, pc,
3720 				    buf+len);
3721 			}
3722 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
3723 			break;
3724 		default:
3725 		err:
3726 			/* Invalid request */
3727 			*scsipkt->pkt_scbp = STATUS_CHECK;
3728 			sense = sata_arq_sense(spx);
3729 			sense->es_key = KEY_ILLEGAL_REQUEST;
3730 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3731 			goto done;
3732 		}
3733 
3734 		/* fix total mode data length */
3735 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3736 			/* 4-byte mode parameter header */
3737 			buf[0] = len - 1;   	/* mode data length */
3738 		} else {
3739 			buf[0] = (len -2) >> 8;
3740 			buf[1] = (len -2) & 0xff;
3741 		}
3742 
3743 
3744 		/* Check allocation length */
3745 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3746 			alc_len = scsipkt->pkt_cdbp[4];
3747 		} else {
3748 			alc_len = scsipkt->pkt_cdbp[7];
3749 			alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
3750 		}
3751 		/*
3752 		 * We do not check for possible parameters truncation
3753 		 * (alc_len < len) assuming that the target driver works
3754 		 * correctly. Just avoiding overrun.
3755 		 * Copy no more than requested and possible, buffer-wise.
3756 		 */
3757 		count = MIN(alc_len, len);
3758 		count = MIN(bp->b_bcount, count);
3759 		bcopy(buf, bp->b_un.b_addr, count);
3760 
3761 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3762 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
3763 	}
3764 	*scsipkt->pkt_scbp = STATUS_GOOD;
3765 done:
3766 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3767 	(void) kmem_free(buf, 1024);
3768 
3769 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3770 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3771 
3772 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3773 	    scsipkt->pkt_comp != NULL)
3774 		/* scsi callback required */
3775 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3776 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3777 		    TQ_SLEEP) == NULL)
3778 			/* Scheduling the callback failed */
3779 			return (TRAN_BUSY);
3780 
3781 	return (TRAN_ACCEPT);
3782 }
3783 
3784 
3785 /*
3786  * SATA translate command: Mode Select.
3787  * Translated into appropriate SATA command or emulated.
3788  * Saving parameters is not supported.
3789  * Changing device capacity is not supported (although theoretically
3790  * possible by executing SET FEATURES/SET MAX ADDRESS)
3791  *
3792  * Assumption is that the target driver is working correctly.
3793  *
3794  * More than one SATA command may be executed to perform operations specified
3795  * by mode select pages. The first error terminates further execution.
3796  * Operations performed successully are not backed-up in such case.
3797  *
3798  * NOTE: only caching mode select page is implemented.
3799  * Caching setup is remembered so it could be re-stored in case of
3800  * an unexpected device reset.
3801  *
3802  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3803  */
3804 
3805 static int
3806 sata_txlt_mode_select(sata_pkt_txlate_t *spx)
3807 {
3808 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3809 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3810 	struct scsi_extended_sense *sense;
3811 	int len, pagelen, count, pllen;
3812 	uint8_t *buf;	/* mode select buffer */
3813 	int rval, stat, reason;
3814 	uint_t nointr_flag;
3815 	int dmod = 0;
3816 
3817 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3818 	    "sata_txlt_mode_select, pc %x page code 0x%02x\n",
3819 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
3820 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
3821 
3822 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3823 
3824 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3825 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3826 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3827 		return (rval);
3828 	}
3829 
3830 	rval = TRAN_ACCEPT;
3831 
3832 	scsipkt->pkt_reason = CMD_CMPLT;
3833 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3834 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3835 
3836 	/* Reject not supported request */
3837 	if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
3838 		*scsipkt->pkt_scbp = STATUS_CHECK;
3839 		sense = sata_arq_sense(spx);
3840 		sense->es_key = KEY_ILLEGAL_REQUEST;
3841 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3842 		goto done;
3843 	}
3844 
3845 	if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
3846 		pllen = scsipkt->pkt_cdbp[4];
3847 	} else {
3848 		pllen = scsipkt->pkt_cdbp[7];
3849 		pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
3850 	}
3851 
3852 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
3853 
3854 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
3855 		buf = (uint8_t *)bp->b_un.b_addr;
3856 		count = MIN(bp->b_bcount, pllen);
3857 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3858 		scsipkt->pkt_resid = 0;
3859 		pllen = count;
3860 
3861 		/*
3862 		 * Check the header to skip the block descriptor(s) - we
3863 		 * do not support setting device capacity.
3864 		 * Existing macros do not recognize long LBA dscriptor,
3865 		 * hence manual calculation.
3866 		 */
3867 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
3868 			/* 6-bytes CMD, 4 bytes header */
3869 			if (count <= 4)
3870 				goto done;		/* header only */
3871 			len = buf[3] + 4;
3872 		} else {
3873 			/* 10-bytes CMD, 8 bytes header */
3874 			if (count <= 8)
3875 				goto done;		/* header only */
3876 			len = buf[6];
3877 			len = (len << 8) + buf[7] + 8;
3878 		}
3879 		if (len >= count)
3880 			goto done;	/* header + descriptor(s) only */
3881 
3882 		pllen -= len;		/* remaining data length */
3883 
3884 		/*
3885 		 * We may be executing SATA command and want to execute it
3886 		 * in SYNCH mode, regardless of scsi_pkt setting.
3887 		 * Save scsi_pkt setting and indicate SYNCH mode
3888 		 */
3889 		nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
3890 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3891 		    scsipkt->pkt_comp != NULL) {
3892 			scsipkt->pkt_flags |= FLAG_NOINTR;
3893 		}
3894 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
3895 
3896 		/*
3897 		 * len is now the offset to a first mode select page
3898 		 * Process all pages
3899 		 */
3900 		while (pllen > 0) {
3901 			switch ((int)buf[len]) {
3902 			case MODEPAGE_CACHING:
3903 				/* No support for SP (saving) */
3904 				if (scsipkt->pkt_cdbp[1] & 0x01) {
3905 					*scsipkt->pkt_scbp = STATUS_CHECK;
3906 					sense = sata_arq_sense(spx);
3907 					sense->es_key = KEY_ILLEGAL_REQUEST;
3908 					sense->es_add_code =
3909 					    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3910 					goto done;
3911 				}
3912 				stat = sata_mode_select_page_8(spx,
3913 				    (struct mode_cache_scsi3 *)&buf[len],
3914 				    pllen, &pagelen, &rval, &dmod);
3915 				/*
3916 				 * The pagelen value indicates the number of
3917 				 * parameter bytes already processed.
3918 				 * The rval is the return value from
3919 				 * sata_tran_start().
3920 				 * The stat indicates the overall status of
3921 				 * the operation(s).
3922 				 */
3923 				if (stat != SATA_SUCCESS)
3924 					/*
3925 					 * Page processing did not succeed -
3926 					 * all error info is already set-up,
3927 					 * just return
3928 					 */
3929 					pllen = 0; /* this breaks the loop */
3930 				else {
3931 					len += pagelen;
3932 					pllen -= pagelen;
3933 				}
3934 				break;
3935 
3936 			case MODEPAGE_INFO_EXCPT:
3937 				stat = sata_mode_select_page_1c(spx,
3938 				    (struct mode_info_excpt_page *)&buf[len],
3939 				    pllen, &pagelen, &rval, &dmod);
3940 				/*
3941 				 * The pagelen value indicates the number of
3942 				 * parameter bytes already processed.
3943 				 * The rval is the return value from
3944 				 * sata_tran_start().
3945 				 * The stat indicates the overall status of
3946 				 * the operation(s).
3947 				 */
3948 				if (stat != SATA_SUCCESS)
3949 					/*
3950 					 * Page processing did not succeed -
3951 					 * all error info is already set-up,
3952 					 * just return
3953 					 */
3954 					pllen = 0; /* this breaks the loop */
3955 				else {
3956 					len += pagelen;
3957 					pllen -= pagelen;
3958 				}
3959 				break;
3960 
3961 			case MODEPAGE_ACOUSTIC_MANAG:
3962 				stat = sata_mode_select_page_30(spx,
3963 				    (struct mode_acoustic_management *)
3964 				    &buf[len], pllen, &pagelen, &rval, &dmod);
3965 				/*
3966 				 * The pagelen value indicates the number of
3967 				 * parameter bytes already processed.
3968 				 * The rval is the return value from
3969 				 * sata_tran_start().
3970 				 * The stat indicates the overall status of
3971 				 * the operation(s).
3972 				 */
3973 				if (stat != SATA_SUCCESS)
3974 					/*
3975 					 * Page processing did not succeed -
3976 					 * all error info is already set-up,
3977 					 * just return
3978 					 */
3979 					pllen = 0; /* this breaks the loop */
3980 				else {
3981 					len += pagelen;
3982 					pllen -= pagelen;
3983 				}
3984 
3985 				break;
3986 			default:
3987 				*scsipkt->pkt_scbp = STATUS_CHECK;
3988 				sense = sata_arq_sense(spx);
3989 				sense->es_key = KEY_ILLEGAL_REQUEST;
3990 				sense->es_add_code =
3991 				    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
3992 				goto done;
3993 			}
3994 		}
3995 	}
3996 done:
3997 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3998 	/*
3999 	 * If device parameters were modified, fetch and store the new
4000 	 * Identify Device data. Since port mutex could have been released
4001 	 * for accessing HBA driver, we need to re-check device existence.
4002 	 */
4003 	if (dmod != 0) {
4004 		sata_drive_info_t new_sdinfo, *sdinfo;
4005 		int rv = 0;
4006 
4007 		/*
4008 		 * Following statement has to be changed if this function is
4009 		 * used for devices other than SATA hard disks.
4010 		 */
4011 		new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
4012 
4013 		new_sdinfo.satadrv_addr =
4014 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr;
4015 		rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
4016 		    &new_sdinfo);
4017 
4018 		mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4019 		/*
4020 		 * Since port mutex could have been released when
4021 		 * accessing HBA driver, we need to re-check that the
4022 		 * framework still holds the device info structure.
4023 		 */
4024 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4025 		    &spx->txlt_sata_pkt->satapkt_device);
4026 		if (sdinfo != NULL) {
4027 			/*
4028 			 * Device still has info structure in the
4029 			 * sata framework. Copy newly fetched info
4030 			 */
4031 			if (rv == 0) {
4032 				sdinfo->satadrv_id = new_sdinfo.satadrv_id;
4033 				sata_save_drive_settings(sdinfo);
4034 			} else {
4035 				/*
4036 				 * Could not fetch new data - invalidate
4037 				 * sata_drive_info. That makes device
4038 				 * unusable.
4039 				 */
4040 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
4041 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
4042 			}
4043 		}
4044 		if (rv != 0 || sdinfo == NULL) {
4045 			/*
4046 			 * This changes the overall mode select completion
4047 			 * reason to a failed one !!!!!
4048 			 */
4049 			*scsipkt->pkt_scbp = STATUS_CHECK;
4050 			sense = sata_arq_sense(spx);
4051 			scsipkt->pkt_reason = CMD_INCOMPLETE;
4052 			rval = TRAN_ACCEPT;
4053 		}
4054 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4055 	}
4056 	/* Restore the scsi pkt flags */
4057 	scsipkt->pkt_flags &= ~FLAG_NOINTR;
4058 	scsipkt->pkt_flags |= nointr_flag;
4059 
4060 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4061 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4062 
4063 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4064 	    scsipkt->pkt_comp != NULL)
4065 		/* scsi callback required */
4066 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4067 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4068 		    TQ_SLEEP) == NULL)
4069 			/* Scheduling the callback failed */
4070 			return (TRAN_BUSY);
4071 
4072 	return (rval);
4073 }
4074 
4075 
4076 
4077 /*
4078  * Translate command: Log Sense
4079  */
4080 static 	int
4081 sata_txlt_log_sense(sata_pkt_txlate_t *spx)
4082 {
4083 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4084 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4085 	sata_drive_info_t *sdinfo;
4086 	struct scsi_extended_sense *sense;
4087 	int 		len, count, alc_len;
4088 	int		pc;	/* Page Control code */
4089 	int		page_code;	/* Page code */
4090 	uint8_t		*buf;	/* log sense buffer */
4091 	int		rval, reason;
4092 #define	MAX_LOG_SENSE_PAGE_SIZE	512
4093 
4094 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4095 	    "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
4096 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4097 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4098 
4099 	buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
4100 
4101 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4102 
4103 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4104 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4105 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4106 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4107 		return (rval);
4108 	}
4109 
4110 	scsipkt->pkt_reason = CMD_CMPLT;
4111 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4112 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4113 
4114 	pc = scsipkt->pkt_cdbp[2] >> 6;
4115 	page_code = scsipkt->pkt_cdbp[2] & 0x3f;
4116 
4117 	/* Reject not supported request for all but cumulative values */
4118 	switch (pc) {
4119 	case PC_CUMULATIVE_VALUES:
4120 		break;
4121 	default:
4122 		*scsipkt->pkt_scbp = STATUS_CHECK;
4123 		sense = sata_arq_sense(spx);
4124 		sense->es_key = KEY_ILLEGAL_REQUEST;
4125 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4126 		goto done;
4127 	}
4128 
4129 	switch (page_code) {
4130 	case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
4131 	case PAGE_CODE_SELF_TEST_RESULTS:
4132 	case PAGE_CODE_INFORMATION_EXCEPTIONS:
4133 	case PAGE_CODE_SMART_READ_DATA:
4134 		break;
4135 	default:
4136 		*scsipkt->pkt_scbp = STATUS_CHECK;
4137 		sense = sata_arq_sense(spx);
4138 		sense->es_key = KEY_ILLEGAL_REQUEST;
4139 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4140 		goto done;
4141 	}
4142 
4143 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4144 		/*
4145 		 * Because log sense uses local buffers for data retrieval from
4146 		 * the devices and sets the data programatically in the
4147 		 * original specified buffer, release preallocated DMA
4148 		 * resources before storing data in the original buffer,
4149 		 * so no unwanted DMA sync would take place.
4150 		 */
4151 		sata_id_t *sata_id;
4152 
4153 		sata_scsi_dmafree(NULL, scsipkt);
4154 
4155 		len = 0;
4156 
4157 		/* Build log parameter header */
4158 		buf[len++] = page_code;	/* page code as in the CDB */
4159 		buf[len++] = 0;		/* reserved */
4160 		buf[len++] = 0;		/* Zero out page length for now (MSB) */
4161 		buf[len++] = 0;		/* (LSB) */
4162 
4163 		sdinfo = sata_get_device_info(
4164 		    spx->txlt_sata_hba_inst,
4165 		    &spx->txlt_sata_pkt->satapkt_device);
4166 
4167 
4168 		/*
4169 		 * Add requested pages.
4170 		 */
4171 		switch (page_code) {
4172 		case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
4173 			len = sata_build_lsense_page_0(sdinfo, buf + len);
4174 			break;
4175 		case PAGE_CODE_SELF_TEST_RESULTS:
4176 			sata_id = &sdinfo->satadrv_id;
4177 			if ((! (sata_id->ai_cmdset84 &
4178 			    SATA_SMART_SELF_TEST_SUPPORTED)) ||
4179 			    (! (sata_id->ai_features87 &
4180 			    SATA_SMART_SELF_TEST_SUPPORTED))) {
4181 				*scsipkt->pkt_scbp = STATUS_CHECK;
4182 				sense = sata_arq_sense(spx);
4183 				sense->es_key = KEY_ILLEGAL_REQUEST;
4184 				sense->es_add_code =
4185 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4186 
4187 				goto done;
4188 			}
4189 			len = sata_build_lsense_page_10(sdinfo, buf + len,
4190 			    spx->txlt_sata_hba_inst);
4191 			break;
4192 		case PAGE_CODE_INFORMATION_EXCEPTIONS:
4193 			sata_id = &sdinfo->satadrv_id;
4194 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
4195 				*scsipkt->pkt_scbp = STATUS_CHECK;
4196 				sense = sata_arq_sense(spx);
4197 				sense->es_key = KEY_ILLEGAL_REQUEST;
4198 				sense->es_add_code =
4199 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4200 
4201 				goto done;
4202 			}
4203 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
4204 				*scsipkt->pkt_scbp = STATUS_CHECK;
4205 				sense = sata_arq_sense(spx);
4206 				sense->es_key = KEY_ABORTED_COMMAND;
4207 				sense->es_add_code =
4208 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
4209 				sense->es_qual_code =
4210 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
4211 
4212 				goto done;
4213 			}
4214 
4215 			len = sata_build_lsense_page_2f(sdinfo, buf + len,
4216 			    spx->txlt_sata_hba_inst);
4217 			break;
4218 		case PAGE_CODE_SMART_READ_DATA:
4219 			sata_id = &sdinfo->satadrv_id;
4220 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
4221 				*scsipkt->pkt_scbp = STATUS_CHECK;
4222 				sense = sata_arq_sense(spx);
4223 				sense->es_key = KEY_ILLEGAL_REQUEST;
4224 				sense->es_add_code =
4225 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4226 
4227 				goto done;
4228 			}
4229 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
4230 				*scsipkt->pkt_scbp = STATUS_CHECK;
4231 				sense = sata_arq_sense(spx);
4232 				sense->es_key = KEY_ABORTED_COMMAND;
4233 				sense->es_add_code =
4234 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
4235 				sense->es_qual_code =
4236 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
4237 
4238 				goto done;
4239 			}
4240 
4241 			/* This page doesn't include a page header */
4242 			len = sata_build_lsense_page_30(sdinfo, buf,
4243 			    spx->txlt_sata_hba_inst);
4244 			goto no_header;
4245 		default:
4246 			/* Invalid request */
4247 			*scsipkt->pkt_scbp = STATUS_CHECK;
4248 			sense = sata_arq_sense(spx);
4249 			sense->es_key = KEY_ILLEGAL_REQUEST;
4250 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4251 			goto done;
4252 		}
4253 
4254 		/* set parameter log sense data length */
4255 		buf[2] = len >> 8;	/* log sense length (MSB) */
4256 		buf[3] = len & 0xff;	/* log sense length (LSB) */
4257 
4258 		len += SCSI_LOG_PAGE_HDR_LEN;
4259 		ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
4260 
4261 no_header:
4262 		/* Check allocation length */
4263 		alc_len = scsipkt->pkt_cdbp[7];
4264 		alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
4265 
4266 		/*
4267 		 * We do not check for possible parameters truncation
4268 		 * (alc_len < len) assuming that the target driver works
4269 		 * correctly. Just avoiding overrun.
4270 		 * Copy no more than requested and possible, buffer-wise.
4271 		 */
4272 		count = MIN(alc_len, len);
4273 		count = MIN(bp->b_bcount, count);
4274 		bcopy(buf, bp->b_un.b_addr, count);
4275 
4276 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4277 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
4278 	}
4279 	*scsipkt->pkt_scbp = STATUS_GOOD;
4280 done:
4281 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4282 	(void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4283 
4284 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4285 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4286 
4287 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4288 	    scsipkt->pkt_comp != NULL)
4289 		/* scsi callback required */
4290 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4291 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4292 		    TQ_SLEEP) == NULL)
4293 			/* Scheduling the callback failed */
4294 			return (TRAN_BUSY);
4295 
4296 	return (TRAN_ACCEPT);
4297 }
4298 
4299 /*
4300  * Translate command: Log Select
4301  * Not implemented at this time - returns invalid command response.
4302  */
4303 static 	int
4304 sata_txlt_log_select(sata_pkt_txlate_t *spx)
4305 {
4306 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4307 	    "sata_txlt_log_select\n", NULL);
4308 
4309 	return (sata_txlt_invalid_command(spx));
4310 }
4311 
4312 
4313 /*
4314  * Translate command: Read (various types).
4315  * Translated into appropriate type of ATA READ command
4316  * for SATA hard disks.
4317  * Both the device capabilities and requested operation mode are
4318  * considered.
4319  *
4320  * Following scsi cdb fields are ignored:
4321  * rdprotect, dpo, fua, fua_nv, group_number.
4322  *
4323  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
4324  * enable variable sata_func_enable), the capability of the controller and
4325  * capability of a device are checked and if both support queueing, read
4326  * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
4327  * command rather than plain READ_XXX command.
4328  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
4329  * both the controller and device suport such functionality, the read
4330  * request will be translated to READ_FPDMA_QUEUED command.
4331  * In both cases the maximum queue depth is derived as minimum of:
4332  * HBA capability,device capability and sata_max_queue_depth variable setting.
4333  * The value passed to HBA driver is decremented by 1, because only 5 bits are
4334  * used to pass max queue depth value, and the maximum possible queue depth
4335  * is 32.
4336  *
4337  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4338  * appropriate values in scsi_pkt fields.
4339  */
4340 static int
4341 sata_txlt_read(sata_pkt_txlate_t *spx)
4342 {
4343 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4344 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4345 	sata_drive_info_t *sdinfo;
4346 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4347 	int cport = SATA_TXLT_CPORT(spx);
4348 	uint16_t sec_count;
4349 	uint64_t lba;
4350 	int rval, reason;
4351 	int synch;
4352 
4353 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4354 
4355 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4356 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4357 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4358 		return (rval);
4359 	}
4360 
4361 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4362 	    &spx->txlt_sata_pkt->satapkt_device);
4363 
4364 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
4365 	/*
4366 	 * Extract LBA and sector count from scsi CDB.
4367 	 */
4368 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
4369 	case SCMD_READ:
4370 		/* 6-byte scsi read cmd : 0x08 */
4371 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
4372 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
4373 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4374 		sec_count = scsipkt->pkt_cdbp[4];
4375 		/* sec_count 0 will be interpreted as 256 by a device */
4376 		break;
4377 	case SCMD_READ_G1:
4378 		/* 10-bytes scsi read command : 0x28 */
4379 		lba = scsipkt->pkt_cdbp[2];
4380 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4381 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4382 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4383 		sec_count = scsipkt->pkt_cdbp[7];
4384 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4385 		break;
4386 	case SCMD_READ_G5:
4387 		/* 12-bytes scsi read command : 0xA8 */
4388 		lba = scsipkt->pkt_cdbp[2];
4389 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4390 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4391 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4392 		sec_count = scsipkt->pkt_cdbp[6];
4393 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
4394 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4395 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
4396 		break;
4397 	case SCMD_READ_G4:
4398 		/* 16-bytes scsi read command : 0x88 */
4399 		lba = scsipkt->pkt_cdbp[2];
4400 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4401 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4402 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4403 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
4404 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
4405 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
4406 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
4407 		sec_count = scsipkt->pkt_cdbp[10];
4408 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
4409 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
4410 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
4411 		break;
4412 	default:
4413 		/* Unsupported command */
4414 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4415 		return (sata_txlt_invalid_command(spx));
4416 	}
4417 
4418 	/*
4419 	 * Check if specified address exceeds device capacity
4420 	 */
4421 	if ((lba >= sdinfo->satadrv_capacity) ||
4422 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
4423 		/* LBA out of range */
4424 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4425 		return (sata_txlt_lba_out_of_range(spx));
4426 	}
4427 
4428 	/*
4429 	 * For zero-length transfer, emulate good completion of the command
4430 	 * (reasons for rejecting the command were already checked).
4431 	 * No DMA resources were allocated.
4432 	 */
4433 	if (spx->txlt_dma_cookie_list == NULL) {
4434 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4435 		return (sata_emul_rw_completion(spx));
4436 	}
4437 
4438 	/*
4439 	 * Build cmd block depending on the device capability and
4440 	 * requested operation mode.
4441 	 * Do not bother with non-dma mode - we are working only with
4442 	 * devices supporting DMA.
4443 	 */
4444 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
4445 	scmd->satacmd_device_reg = SATA_ADH_LBA;
4446 	scmd->satacmd_cmd_reg = SATAC_READ_DMA;
4447 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
4448 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4449 		scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT;
4450 		scmd->satacmd_sec_count_msb = sec_count >> 8;
4451 #ifndef __lock_lint
4452 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
4453 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
4454 		scmd->satacmd_lba_high_msb = lba >> 40;
4455 #endif
4456 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
4457 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
4458 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
4459 	}
4460 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
4461 	scmd->satacmd_lba_low_lsb = lba & 0xff;
4462 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
4463 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
4464 	scmd->satacmd_features_reg = 0;
4465 	scmd->satacmd_status_reg = 0;
4466 	scmd->satacmd_error_reg = 0;
4467 
4468 	/*
4469 	 * Check if queueing commands should be used and switch
4470 	 * to appropriate command if possible
4471 	 */
4472 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
4473 		boolean_t using_queuing;
4474 
4475 		/* Queuing supported by controller and device? */
4476 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
4477 		    (sdinfo->satadrv_features_support &
4478 		    SATA_DEV_F_NCQ) &&
4479 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4480 		    SATA_CTLF_NCQ)) {
4481 			using_queuing = B_TRUE;
4482 
4483 			/* NCQ supported - use FPDMA READ */
4484 			scmd->satacmd_cmd_reg =
4485 			    SATAC_READ_FPDMA_QUEUED;
4486 			scmd->satacmd_features_reg_ext =
4487 			    scmd->satacmd_sec_count_msb;
4488 			scmd->satacmd_sec_count_msb = 0;
4489 		} else if ((sdinfo->satadrv_features_support &
4490 		    SATA_DEV_F_TCQ) &&
4491 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4492 		    SATA_CTLF_QCMD)) {
4493 			using_queuing = B_TRUE;
4494 
4495 			/* Legacy queueing */
4496 			if (sdinfo->satadrv_features_support &
4497 			    SATA_DEV_F_LBA48) {
4498 				scmd->satacmd_cmd_reg =
4499 				    SATAC_READ_DMA_QUEUED_EXT;
4500 				scmd->satacmd_features_reg_ext =
4501 				    scmd->satacmd_sec_count_msb;
4502 				scmd->satacmd_sec_count_msb = 0;
4503 			} else {
4504 				scmd->satacmd_cmd_reg =
4505 				    SATAC_READ_DMA_QUEUED;
4506 			}
4507 		} else	/* NCQ nor legacy queuing not supported */
4508 			using_queuing = B_FALSE;
4509 
4510 		/*
4511 		 * If queuing, the sector count goes in the features register
4512 		 * and the secount count will contain the tag.
4513 		 */
4514 		if (using_queuing) {
4515 			scmd->satacmd_features_reg =
4516 			    scmd->satacmd_sec_count_lsb;
4517 			scmd->satacmd_sec_count_lsb = 0;
4518 			scmd->satacmd_flags.sata_queued = B_TRUE;
4519 
4520 			/* Set-up maximum queue depth */
4521 			scmd->satacmd_flags.sata_max_queue_depth =
4522 			    sdinfo->satadrv_max_queue_depth - 1;
4523 		} else if (sdinfo->satadrv_features_enabled &
4524 		    SATA_DEV_F_E_UNTAGGED_QING) {
4525 			/*
4526 			 * Although NCQ/TCQ is not enabled, untagged queuing
4527 			 * may be still used.
4528 			 * Set-up the maximum untagged queue depth.
4529 			 * Use controller's queue depth from sata_hba_tran.
4530 			 * SATA HBA drivers may ignore this value and rely on
4531 			 * the internal limits.For drivers that do not
4532 			 * ignore untaged queue depth, limit the value to
4533 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
4534 			 * largest value that can be passed via
4535 			 * satacmd_flags.sata_max_queue_depth.
4536 			 */
4537 			scmd->satacmd_flags.sata_max_queue_depth =
4538 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
4539 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
4540 
4541 		} else {
4542 			scmd->satacmd_flags.sata_max_queue_depth = 0;
4543 		}
4544 	} else
4545 		scmd->satacmd_flags.sata_max_queue_depth = 0;
4546 
4547 	SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst,
4548 	    "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n",
4549 	    scmd->satacmd_cmd_reg, lba, sec_count);
4550 
4551 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4552 		/* Need callback function */
4553 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
4554 		synch = FALSE;
4555 	} else
4556 		synch = TRUE;
4557 
4558 	/* Transfer command to HBA */
4559 	if (sata_hba_start(spx, &rval) != 0) {
4560 		/* Pkt not accepted for execution */
4561 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4562 		return (rval);
4563 	}
4564 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4565 	/*
4566 	 * If execution is non-synchronous,
4567 	 * a callback function will handle potential errors, translate
4568 	 * the response and will do a callback to a target driver.
4569 	 * If it was synchronous, check execution status using the same
4570 	 * framework callback.
4571 	 */
4572 	if (synch) {
4573 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4574 		    "synchronous execution status %x\n",
4575 		    spx->txlt_sata_pkt->satapkt_reason);
4576 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
4577 	}
4578 	return (TRAN_ACCEPT);
4579 }
4580 
4581 
4582 /*
4583  * SATA translate command: Write (various types)
4584  * Translated into appropriate type of ATA WRITE command
4585  * for SATA hard disks.
4586  * Both the device capabilities and requested operation mode are
4587  * considered.
4588  *
4589  * Following scsi cdb fields are ignored:
4590  * rwprotect, dpo, fua, fua_nv, group_number.
4591  *
4592  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
4593  * enable variable sata_func_enable), the capability of the controller and
4594  * capability of a device are checked and if both support queueing, write
4595  * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT
4596  * command rather than plain WRITE_XXX command.
4597  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
4598  * both the controller and device suport such functionality, the write
4599  * request will be translated to WRITE_FPDMA_QUEUED command.
4600  * In both cases the maximum queue depth is derived as minimum of:
4601  * HBA capability,device capability and sata_max_queue_depth variable setting.
4602  * The value passed to HBA driver is decremented by 1, because only 5 bits are
4603  * used to pass max queue depth value, and the maximum possible queue depth
4604  * is 32.
4605  *
4606  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4607  * appropriate values in scsi_pkt fields.
4608  */
4609 static int
4610 sata_txlt_write(sata_pkt_txlate_t *spx)
4611 {
4612 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4613 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4614 	sata_drive_info_t *sdinfo;
4615 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4616 	int cport = SATA_TXLT_CPORT(spx);
4617 	uint16_t sec_count;
4618 	uint64_t lba;
4619 	int rval, reason;
4620 	int synch;
4621 
4622 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4623 
4624 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4625 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4626 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4627 		return (rval);
4628 	}
4629 
4630 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4631 	    &spx->txlt_sata_pkt->satapkt_device);
4632 
4633 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4634 	/*
4635 	 * Extract LBA and sector count from scsi CDB
4636 	 */
4637 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
4638 	case SCMD_WRITE:
4639 		/* 6-byte scsi read cmd : 0x0A */
4640 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
4641 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
4642 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4643 		sec_count = scsipkt->pkt_cdbp[4];
4644 		/* sec_count 0 will be interpreted as 256 by a device */
4645 		break;
4646 	case SCMD_WRITE_G1:
4647 		/* 10-bytes scsi write command : 0x2A */
4648 		lba = scsipkt->pkt_cdbp[2];
4649 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4650 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4651 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4652 		sec_count = scsipkt->pkt_cdbp[7];
4653 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4654 		break;
4655 	case SCMD_WRITE_G5:
4656 		/* 12-bytes scsi read command : 0xAA */
4657 		lba = scsipkt->pkt_cdbp[2];
4658 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4659 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4660 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4661 		sec_count = scsipkt->pkt_cdbp[6];
4662 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
4663 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4664 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
4665 		break;
4666 	case SCMD_WRITE_G4:
4667 		/* 16-bytes scsi write command : 0x8A */
4668 		lba = scsipkt->pkt_cdbp[2];
4669 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4670 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4671 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4672 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
4673 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
4674 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
4675 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
4676 		sec_count = scsipkt->pkt_cdbp[10];
4677 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
4678 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
4679 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
4680 		break;
4681 	default:
4682 		/* Unsupported command */
4683 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4684 		return (sata_txlt_invalid_command(spx));
4685 	}
4686 
4687 	/*
4688 	 * Check if specified address and length exceeds device capacity
4689 	 */
4690 	if ((lba >= sdinfo->satadrv_capacity) ||
4691 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
4692 		/* LBA out of range */
4693 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4694 		return (sata_txlt_lba_out_of_range(spx));
4695 	}
4696 
4697 	/*
4698 	 * For zero-length transfer, emulate good completion of the command
4699 	 * (reasons for rejecting the command were already checked).
4700 	 * No DMA resources were allocated.
4701 	 */
4702 	if (spx->txlt_dma_cookie_list == NULL) {
4703 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4704 		return (sata_emul_rw_completion(spx));
4705 	}
4706 
4707 	/*
4708 	 * Build cmd block depending on the device capability and
4709 	 * requested operation mode.
4710 	 * Do not bother with non-dma mode- we are working only with
4711 	 * devices supporting DMA.
4712 	 */
4713 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
4714 	scmd->satacmd_device_reg = SATA_ADH_LBA;
4715 	scmd->satacmd_cmd_reg = SATAC_WRITE_DMA;
4716 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
4717 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4718 		scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT;
4719 		scmd->satacmd_sec_count_msb = sec_count >> 8;
4720 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
4721 #ifndef __lock_lint
4722 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
4723 		scmd->satacmd_lba_high_msb = lba >> 40;
4724 #endif
4725 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
4726 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
4727 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
4728 	}
4729 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
4730 	scmd->satacmd_lba_low_lsb = lba & 0xff;
4731 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
4732 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
4733 	scmd->satacmd_features_reg = 0;
4734 	scmd->satacmd_status_reg = 0;
4735 	scmd->satacmd_error_reg = 0;
4736 
4737 	/*
4738 	 * Check if queueing commands should be used and switch
4739 	 * to appropriate command if possible
4740 	 */
4741 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
4742 		boolean_t using_queuing;
4743 
4744 		/* Queuing supported by controller and device? */
4745 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
4746 		    (sdinfo->satadrv_features_support &
4747 		    SATA_DEV_F_NCQ) &&
4748 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4749 		    SATA_CTLF_NCQ)) {
4750 			using_queuing = B_TRUE;
4751 
4752 			/* NCQ supported - use FPDMA WRITE */
4753 			scmd->satacmd_cmd_reg =
4754 			    SATAC_WRITE_FPDMA_QUEUED;
4755 			scmd->satacmd_features_reg_ext =
4756 			    scmd->satacmd_sec_count_msb;
4757 			scmd->satacmd_sec_count_msb = 0;
4758 		} else if ((sdinfo->satadrv_features_support &
4759 		    SATA_DEV_F_TCQ) &&
4760 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4761 		    SATA_CTLF_QCMD)) {
4762 			using_queuing = B_TRUE;
4763 
4764 			/* Legacy queueing */
4765 			if (sdinfo->satadrv_features_support &
4766 			    SATA_DEV_F_LBA48) {
4767 				scmd->satacmd_cmd_reg =
4768 				    SATAC_WRITE_DMA_QUEUED_EXT;
4769 				scmd->satacmd_features_reg_ext =
4770 				    scmd->satacmd_sec_count_msb;
4771 				scmd->satacmd_sec_count_msb = 0;
4772 			} else {
4773 				scmd->satacmd_cmd_reg =
4774 				    SATAC_WRITE_DMA_QUEUED;
4775 			}
4776 		} else	/*  NCQ nor legacy queuing not supported */
4777 			using_queuing = B_FALSE;
4778 
4779 		if (using_queuing) {
4780 			scmd->satacmd_features_reg =
4781 			    scmd->satacmd_sec_count_lsb;
4782 			scmd->satacmd_sec_count_lsb = 0;
4783 			scmd->satacmd_flags.sata_queued = B_TRUE;
4784 			/* Set-up maximum queue depth */
4785 			scmd->satacmd_flags.sata_max_queue_depth =
4786 			    sdinfo->satadrv_max_queue_depth - 1;
4787 		} else if (sdinfo->satadrv_features_enabled &
4788 		    SATA_DEV_F_E_UNTAGGED_QING) {
4789 			/*
4790 			 * Although NCQ/TCQ is not enabled, untagged queuing
4791 			 * may be still used.
4792 			 * Set-up the maximum untagged queue depth.
4793 			 * Use controller's queue depth from sata_hba_tran.
4794 			 * SATA HBA drivers may ignore this value and rely on
4795 			 * the internal limits. For drivera that do not
4796 			 * ignore untaged queue depth, limit the value to
4797 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
4798 			 * largest value that can be passed via
4799 			 * satacmd_flags.sata_max_queue_depth.
4800 			 */
4801 			scmd->satacmd_flags.sata_max_queue_depth =
4802 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
4803 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
4804 
4805 		} else {
4806 			scmd->satacmd_flags.sata_max_queue_depth = 0;
4807 		}
4808 	} else
4809 		scmd->satacmd_flags.sata_max_queue_depth = 0;
4810 
4811 	SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4812 	    "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n",
4813 	    scmd->satacmd_cmd_reg, lba, sec_count);
4814 
4815 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4816 		/* Need callback function */
4817 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
4818 		synch = FALSE;
4819 	} else
4820 		synch = TRUE;
4821 
4822 	/* Transfer command to HBA */
4823 	if (sata_hba_start(spx, &rval) != 0) {
4824 		/* Pkt not accepted for execution */
4825 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4826 		return (rval);
4827 	}
4828 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4829 
4830 	/*
4831 	 * If execution is non-synchronous,
4832 	 * a callback function will handle potential errors, translate
4833 	 * the response and will do a callback to a target driver.
4834 	 * If it was synchronous, check execution status using the same
4835 	 * framework callback.
4836 	 */
4837 	if (synch) {
4838 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4839 		    "synchronous execution status %x\n",
4840 		    spx->txlt_sata_pkt->satapkt_reason);
4841 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
4842 	}
4843 	return (TRAN_ACCEPT);
4844 }
4845 
4846 
4847 /*
4848  * Implements SCSI SBC WRITE BUFFER command download microcode option
4849  */
4850 static int
4851 sata_txlt_write_buffer(sata_pkt_txlate_t *spx)
4852 {
4853 #define	WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE			4
4854 #define	WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE			5
4855 
4856 	sata_hba_inst_t *sata_hba_inst = SATA_TXLT_HBA_INST(spx);
4857 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4858 	struct sata_pkt *sata_pkt = spx->txlt_sata_pkt;
4859 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4860 
4861 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4862 	struct scsi_extended_sense *sense;
4863 	int rval, mode, sector_count, reason;
4864 	int cport = SATA_TXLT_CPORT(spx);
4865 
4866 	mode = scsipkt->pkt_cdbp[1] & 0x1f;
4867 
4868 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4869 	    "sata_txlt_write_buffer, mode 0x%x\n", mode);
4870 
4871 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4872 
4873 	if ((rval = sata_txlt_generic_pkt_info(spx, &reason)) != TRAN_ACCEPT) {
4874 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4875 		return (rval);
4876 	}
4877 
4878 	/* Use synchronous mode */
4879 	spx->txlt_sata_pkt->satapkt_op_mode
4880 	    |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
4881 
4882 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4883 
4884 	scsipkt->pkt_reason = CMD_CMPLT;
4885 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4886 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4887 
4888 	/*
4889 	 * The SCSI to ATA translation specification only calls
4890 	 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE.
4891 	 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but
4892 	 * ATA 8 (draft) got rid of download microcode for temp
4893 	 * and it is even optional for ATA 7, so it may be aborted.
4894 	 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as
4895 	 * it is not specified and the buffer offset for SCSI is a 16-bit
4896 	 * value in bytes, but for ATA it is a 16-bit offset in 512 byte
4897 	 * sectors.  Thus the offset really doesn't buy us anything.
4898 	 * If and when ATA 8 is stabilized and the SCSI to ATA specification
4899 	 * is revised, this can be revisisted.
4900 	 */
4901 	/* Reject not supported request */
4902 	switch (mode) {
4903 	case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE:
4904 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP;
4905 		break;
4906 	case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE:
4907 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE;
4908 		break;
4909 	default:
4910 		goto bad_param;
4911 	}
4912 
4913 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
4914 
4915 	scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE;
4916 	if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0)
4917 		goto bad_param;
4918 	sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE;
4919 	scmd->satacmd_sec_count_lsb = (uint8_t)sector_count;
4920 	scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8;
4921 	scmd->satacmd_lba_mid_lsb = 0;
4922 	scmd->satacmd_lba_high_lsb = 0;
4923 	scmd->satacmd_device_reg = 0;
4924 	spx->txlt_sata_pkt->satapkt_comp = NULL;
4925 	scmd->satacmd_addr_type = 0;
4926 
4927 	/* Transfer command to HBA */
4928 	if (sata_hba_start(spx, &rval) != 0) {
4929 		/* Pkt not accepted for execution */
4930 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
4931 		return (rval);
4932 	}
4933 
4934 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
4935 
4936 	/* Then we need synchronous check the status of the disk */
4937 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4938 	    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
4939 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
4940 		scsipkt->pkt_reason = CMD_CMPLT;
4941 
4942 		/* Download commmand succeed, so probe and identify device */
4943 		sata_reidentify_device(spx);
4944 	} else {
4945 		/* Something went wrong, microcode download command failed */
4946 		scsipkt->pkt_reason = CMD_INCOMPLETE;
4947 		*scsipkt->pkt_scbp = STATUS_CHECK;
4948 		sense = sata_arq_sense(spx);
4949 		switch (sata_pkt->satapkt_reason) {
4950 		case SATA_PKT_PORT_ERROR:
4951 			/*
4952 			 * We have no device data. Assume no data transfered.
4953 			 */
4954 			sense->es_key = KEY_HARDWARE_ERROR;
4955 			break;
4956 
4957 		case SATA_PKT_DEV_ERROR:
4958 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
4959 			    SATA_STATUS_ERR) {
4960 				/*
4961 				 * determine dev error reason from error
4962 				 * reg content
4963 				 */
4964 				sata_decode_device_error(spx, sense);
4965 				break;
4966 			}
4967 			/* No extended sense key - no info available */
4968 			break;
4969 
4970 		case SATA_PKT_TIMEOUT:
4971 			scsipkt->pkt_reason = CMD_TIMEOUT;
4972 			scsipkt->pkt_statistics |=
4973 			    STAT_TIMEOUT | STAT_DEV_RESET;
4974 			/* No extended sense key ? */
4975 			break;
4976 
4977 		case SATA_PKT_ABORTED:
4978 			scsipkt->pkt_reason = CMD_ABORTED;
4979 			scsipkt->pkt_statistics |= STAT_ABORTED;
4980 			/* No extended sense key ? */
4981 			break;
4982 
4983 		case SATA_PKT_RESET:
4984 			/* pkt aborted by an explicit reset from a host */
4985 			scsipkt->pkt_reason = CMD_RESET;
4986 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
4987 			break;
4988 
4989 		default:
4990 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
4991 			    "sata_txlt_nodata_cmd_completion: "
4992 			    "invalid packet completion reason %d",
4993 			    sata_pkt->satapkt_reason));
4994 			scsipkt->pkt_reason = CMD_TRAN_ERR;
4995 			break;
4996 		}
4997 
4998 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4999 		    "scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5000 
5001 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5002 		    scsipkt->pkt_comp != NULL)
5003 			/* scsi callback required */
5004 			(*scsipkt->pkt_comp)(scsipkt);
5005 	}
5006 	return (TRAN_ACCEPT);
5007 
5008 bad_param:
5009 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5010 	*scsipkt->pkt_scbp = STATUS_CHECK;
5011 	sense = sata_arq_sense(spx);
5012 	sense->es_key = KEY_ILLEGAL_REQUEST;
5013 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5014 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5015 	    scsipkt->pkt_comp != NULL) {
5016 		/* scsi callback required */
5017 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5018 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5019 		    TQ_SLEEP) == 0) {
5020 			/* Scheduling the callback failed */
5021 			rval = TRAN_BUSY;
5022 		}
5023 	}
5024 	return (rval);
5025 }
5026 
5027 /*
5028  * Re-identify device after doing a firmware download.
5029  */
5030 static void
5031 sata_reidentify_device(sata_pkt_txlate_t *spx)
5032 {
5033 #define	DOWNLOAD_WAIT_TIME_SECS	60
5034 #define	DOWNLOAD_WAIT_INTERVAL_SECS	1
5035 	int rval;
5036 	int retry_cnt;
5037 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5038 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
5039 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
5040 	sata_drive_info_t *sdinfo;
5041 
5042 	/*
5043 	 * Before returning good status, probe device.
5044 	 * Device probing will get IDENTIFY DEVICE data, if possible.
5045 	 * The assumption is that the new microcode is applied by the
5046 	 * device. It is a caller responsibility to verify this.
5047 	 */
5048 	for (retry_cnt = 0;
5049 	    retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS;
5050 	    retry_cnt++) {
5051 		rval = sata_probe_device(sata_hba_inst, &sata_device);
5052 
5053 		if (rval == SATA_SUCCESS) { /* Set default features */
5054 			sdinfo = sata_get_device_info(sata_hba_inst,
5055 			    &sata_device);
5056 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
5057 			    SATA_SUCCESS) {
5058 				/* retry */
5059 				(void) sata_initialize_device(sata_hba_inst,
5060 				    sdinfo);
5061 			}
5062 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5063 			    scsipkt->pkt_comp != NULL)
5064 				(*scsipkt->pkt_comp)(scsipkt);
5065 			return;
5066 		} else if (rval == SATA_RETRY) {
5067 			delay(drv_usectohz(1000000 *
5068 			    DOWNLOAD_WAIT_INTERVAL_SECS));
5069 			continue;
5070 		} else	/* failed - no reason to retry */
5071 			break;
5072 	}
5073 
5074 	/*
5075 	 * Something went wrong, device probing failed.
5076 	 */
5077 	SATA_LOG_D((sata_hba_inst, CE_WARN,
5078 	    "Cannot probe device after downloading microcode\n"));
5079 
5080 	/* Reset device to force retrying the probe. */
5081 	(void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst))
5082 	    (SATA_DIP(sata_hba_inst), &sata_device);
5083 
5084 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5085 	    scsipkt->pkt_comp != NULL)
5086 		(*scsipkt->pkt_comp)(scsipkt);
5087 }
5088 
5089 
5090 /*
5091  * Translate command: Synchronize Cache.
5092  * Translates into Flush Cache command for SATA hard disks.
5093  *
5094  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
5095  * appropriate values in scsi_pkt fields.
5096  */
5097 static 	int
5098 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx)
5099 {
5100 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5101 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
5102 	int cport = SATA_TXLT_CPORT(spx);
5103 	int rval, reason;
5104 	int synch;
5105 
5106 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5107 
5108 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
5109 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5110 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5111 		return (rval);
5112 	}
5113 
5114 	scmd->satacmd_addr_type = 0;
5115 	scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE;
5116 	scmd->satacmd_device_reg = 0;
5117 	scmd->satacmd_sec_count_lsb = 0;
5118 	scmd->satacmd_lba_low_lsb = 0;
5119 	scmd->satacmd_lba_mid_lsb = 0;
5120 	scmd->satacmd_lba_high_lsb = 0;
5121 	scmd->satacmd_features_reg = 0;
5122 	scmd->satacmd_status_reg = 0;
5123 	scmd->satacmd_error_reg = 0;
5124 
5125 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5126 	    "sata_txlt_synchronize_cache\n", NULL);
5127 
5128 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5129 		/* Need to set-up a callback function */
5130 		spx->txlt_sata_pkt->satapkt_comp =
5131 		    sata_txlt_nodata_cmd_completion;
5132 		synch = FALSE;
5133 	} else
5134 		synch = TRUE;
5135 
5136 	/* Transfer command to HBA */
5137 	if (sata_hba_start(spx, &rval) != 0) {
5138 		/* Pkt not accepted for execution */
5139 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5140 		return (rval);
5141 	}
5142 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5143 
5144 	/*
5145 	 * If execution non-synchronous, it had to be completed
5146 	 * a callback function will handle potential errors, translate
5147 	 * the response and will do a callback to a target driver.
5148 	 * If it was synchronous, check status, using the same
5149 	 * framework callback.
5150 	 */
5151 	if (synch) {
5152 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5153 		    "synchronous execution status %x\n",
5154 		    spx->txlt_sata_pkt->satapkt_reason);
5155 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
5156 	}
5157 	return (TRAN_ACCEPT);
5158 }
5159 
5160 
5161 /*
5162  * Send pkt to SATA HBA driver
5163  *
5164  * This function may be called only if the operation is requested by scsi_pkt,
5165  * i.e. scsi_pkt is not NULL.
5166  *
5167  * This function has to be called with cport mutex held. It does release
5168  * the mutex when it calls HBA driver sata_tran_start function and
5169  * re-acquires it afterwards.
5170  *
5171  * If return value is 0, pkt was accepted, -1 otherwise
5172  * rval is set to appropriate sata_scsi_start return value.
5173  *
5174  * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not
5175  * have called the sata_pkt callback function for this packet.
5176  *
5177  * The scsi callback has to be performed by the caller of this routine.
5178  *
5179  * Note 2: No port multiplier support for now.
5180  */
5181 static int
5182 sata_hba_start(sata_pkt_txlate_t *spx, int *rval)
5183 {
5184 	int stat, cport;
5185 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
5186 	sata_drive_info_t *sdinfo;
5187 	sata_device_t *sata_device;
5188 	uint8_t cmd;
5189 	struct sata_cmd_flags cmd_flags;
5190 
5191 	ASSERT(spx->txlt_sata_pkt != NULL);
5192 
5193 	cport = SATA_TXLT_CPORT(spx);
5194 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5195 
5196 	sdinfo = sata_get_device_info(sata_hba_inst,
5197 	    &spx->txlt_sata_pkt->satapkt_device);
5198 	ASSERT(sdinfo != NULL);
5199 
5200 	/* Clear device reset state? */
5201 	if (sdinfo->satadrv_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) {
5202 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
5203 		    sata_clear_dev_reset = B_TRUE;
5204 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_CLEAR_DEVICE_RESET;
5205 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5206 		    "sata_hba_start: clearing device reset state\n", NULL);
5207 	}
5208 	cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg;
5209 	cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags;
5210 	sata_device = &spx->txlt_sata_pkt->satapkt_device;
5211 
5212 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5213 
5214 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5215 	    "Sata cmd 0x%2x\n", cmd);
5216 
5217 	stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
5218 	    spx->txlt_sata_pkt);
5219 
5220 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5221 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
5222 	/*
5223 	 * If sata pkt was accepted and executed in asynchronous mode, i.e.
5224 	 * with the sata callback, the sata_pkt could be already destroyed
5225 	 * by the time we check ther return status from the hba_start()
5226 	 * function, because sata_scsi_destroy_pkt() could have been already
5227 	 * called (perhaps in the interrupt context). So, in such case, there
5228 	 * should be no references to it. In other cases, sata_pkt still
5229 	 * exists.
5230 	 */
5231 	switch (stat) {
5232 	case SATA_TRAN_ACCEPTED:
5233 		/*
5234 		 * pkt accepted for execution.
5235 		 * If it was executed synchronously, it is already completed
5236 		 * and pkt completion_reason indicates completion status.
5237 		 */
5238 		*rval = TRAN_ACCEPT;
5239 		return (0);
5240 
5241 	case SATA_TRAN_QUEUE_FULL:
5242 		/*
5243 		 * Controller detected queue full condition.
5244 		 */
5245 		SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst,
5246 		    "sata_hba_start: queue full\n", NULL);
5247 
5248 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
5249 		*spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL;
5250 
5251 		*rval = TRAN_BUSY;
5252 		break;
5253 
5254 	case SATA_TRAN_PORT_ERROR:
5255 		/*
5256 		 * Communication/link with device or general port error
5257 		 * detected before pkt execution begun.
5258 		 */
5259 		if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
5260 		    SATA_ADDR_CPORT ||
5261 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
5262 		    SATA_ADDR_DCPORT)
5263 			sata_log(sata_hba_inst, CE_CONT,
5264 			    "SATA port %d error",
5265 			    sata_device->satadev_addr.cport);
5266 		else
5267 			sata_log(sata_hba_inst, CE_CONT,
5268 			    "SATA port %d pmport %d error\n",
5269 			    sata_device->satadev_addr.cport,
5270 			    sata_device->satadev_addr.pmport);
5271 
5272 		/*
5273 		 * Update the port/device structure.
5274 		 * sata_pkt should be still valid. Since port error is
5275 		 * returned, sata_device content should reflect port
5276 		 * state - it means, that sata address have been changed,
5277 		 * because original packet's sata address refered to a device
5278 		 * attached to some port.
5279 		 */
5280 		sata_update_port_info(sata_hba_inst, sata_device);
5281 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
5282 		*rval = TRAN_FATAL_ERROR;
5283 		break;
5284 
5285 	case SATA_TRAN_CMD_UNSUPPORTED:
5286 		/*
5287 		 * Command rejected by HBA as unsupported. It was HBA driver
5288 		 * that rejected the command, command was not sent to
5289 		 * an attached device.
5290 		 */
5291 		if ((sdinfo != NULL) &&
5292 		    (sdinfo->satadrv_state & SATA_DSTATE_RESET))
5293 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5294 			    "sat_hba_start: cmd 0x%2x rejected "
5295 			    "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd);
5296 
5297 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5298 		(void) sata_txlt_invalid_command(spx);
5299 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5300 
5301 		*rval = TRAN_ACCEPT;
5302 		break;
5303 
5304 	case SATA_TRAN_BUSY:
5305 		/*
5306 		 * Command rejected by HBA because other operation prevents
5307 		 * accepting the packet, or device is in RESET condition.
5308 		 */
5309 		if (sdinfo != NULL) {
5310 			sdinfo->satadrv_state =
5311 			    spx->txlt_sata_pkt->satapkt_device.satadev_state;
5312 
5313 			if (sdinfo->satadrv_state & SATA_DSTATE_RESET) {
5314 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5315 				    "sata_hba_start: cmd 0x%2x rejected "
5316 				    "because of device reset condition\n",
5317 				    cmd);
5318 			} else {
5319 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5320 				    "sata_hba_start: cmd 0x%2x rejected "
5321 				    "with SATA_TRAN_BUSY status\n",
5322 				    cmd);
5323 			}
5324 		}
5325 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
5326 		*rval = TRAN_BUSY;
5327 		break;
5328 
5329 	default:
5330 		/* Unrecognized HBA response */
5331 		SATA_LOG_D((sata_hba_inst, CE_WARN,
5332 		    "sata_hba_start: unrecognized HBA response "
5333 		    "to cmd : 0x%2x resp 0x%x", cmd, rval));
5334 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
5335 		*rval = TRAN_FATAL_ERROR;
5336 		break;
5337 	}
5338 
5339 	/*
5340 	 * If we got here, the packet was rejected.
5341 	 * Check if we need to remember reset state clearing request
5342 	 */
5343 	if (cmd_flags.sata_clear_dev_reset) {
5344 		/*
5345 		 * Check if device is still configured - it may have
5346 		 * disapeared from the configuration
5347 		 */
5348 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
5349 		if (sdinfo != NULL) {
5350 			/*
5351 			 * Restore the flag that requests clearing of
5352 			 * the device reset state,
5353 			 * so the next sata packet may carry it to HBA.
5354 			 */
5355 			sdinfo->satadrv_event_flags |=
5356 			    SATA_EVNT_CLEAR_DEVICE_RESET;
5357 		}
5358 	}
5359 	return (-1);
5360 }
5361 
5362 /*
5363  * Scsi response setup for invalid LBA
5364  *
5365  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
5366  */
5367 static int
5368 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx)
5369 {
5370 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5371 	struct scsi_extended_sense *sense;
5372 
5373 	scsipkt->pkt_reason = CMD_CMPLT;
5374 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5375 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5376 	*scsipkt->pkt_scbp = STATUS_CHECK;
5377 
5378 	*scsipkt->pkt_scbp = STATUS_CHECK;
5379 	sense = sata_arq_sense(spx);
5380 	sense->es_key = KEY_ILLEGAL_REQUEST;
5381 	sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
5382 
5383 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5384 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5385 
5386 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5387 	    scsipkt->pkt_comp != NULL)
5388 		/* scsi callback required */
5389 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5390 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5391 		    TQ_SLEEP) == NULL)
5392 			/* Scheduling the callback failed */
5393 			return (TRAN_BUSY);
5394 	return (TRAN_ACCEPT);
5395 }
5396 
5397 
5398 /*
5399  * Analyze device status and error registers and translate them into
5400  * appropriate scsi sense codes.
5401  * NOTE: non-packet commands only for now
5402  */
5403 static void
5404 sata_decode_device_error(sata_pkt_txlate_t *spx,
5405     struct scsi_extended_sense *sense)
5406 {
5407 	uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg;
5408 
5409 	ASSERT(sense != NULL);
5410 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
5411 	    SATA_STATUS_ERR);
5412 
5413 
5414 	if (err_reg & SATA_ERROR_ICRC) {
5415 		sense->es_key = KEY_ABORTED_COMMAND;
5416 		sense->es_add_code = 0x08; /* Communication failure */
5417 		return;
5418 	}
5419 
5420 	if (err_reg & SATA_ERROR_UNC) {
5421 		sense->es_key = KEY_MEDIUM_ERROR;
5422 		/* Information bytes (LBA) need to be set by a caller */
5423 		return;
5424 	}
5425 
5426 	/* ADD HERE: MC error bit handling for ATAPI CD/DVD */
5427 	if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) {
5428 		sense->es_key = KEY_UNIT_ATTENTION;
5429 		sense->es_add_code = 0x3a; /* No media present */
5430 		return;
5431 	}
5432 
5433 	if (err_reg & SATA_ERROR_IDNF) {
5434 		if (err_reg & SATA_ERROR_ABORT) {
5435 			sense->es_key = KEY_ABORTED_COMMAND;
5436 		} else {
5437 			sense->es_key = KEY_ILLEGAL_REQUEST;
5438 			sense->es_add_code = 0x21; /* LBA out of range */
5439 		}
5440 		return;
5441 	}
5442 
5443 	if (err_reg & SATA_ERROR_ABORT) {
5444 		ASSERT(spx->txlt_sata_pkt != NULL);
5445 		sense->es_key = KEY_ABORTED_COMMAND;
5446 		return;
5447 	}
5448 }
5449 
5450 /*
5451  * Extract error LBA from sata_pkt.satapkt_cmd register fields
5452  */
5453 static void
5454 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba)
5455 {
5456 	sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd;
5457 
5458 	*lba = 0;
5459 	if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) {
5460 		*lba = sata_cmd->satacmd_lba_high_msb;
5461 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb;
5462 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb;
5463 	} else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) {
5464 		*lba = sata_cmd->satacmd_device_reg & 0xf;
5465 	}
5466 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb;
5467 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb;
5468 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb;
5469 }
5470 
5471 /*
5472  * This is fixed sense format - if LBA exceeds the info field size,
5473  * no valid info will be returned (valid bit in extended sense will
5474  * be set to 0).
5475  */
5476 static struct scsi_extended_sense *
5477 sata_arq_sense(sata_pkt_txlate_t *spx)
5478 {
5479 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5480 	struct scsi_arq_status *arqs;
5481 	struct scsi_extended_sense *sense;
5482 
5483 	/* Fill ARQ sense data */
5484 	scsipkt->pkt_state |= STATE_ARQ_DONE;
5485 	arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp;
5486 	*(uchar_t *)&arqs->sts_status = STATUS_CHECK;
5487 	*(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD;
5488 	arqs->sts_rqpkt_reason = CMD_CMPLT;
5489 	arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5490 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
5491 	arqs->sts_rqpkt_resid = 0;
5492 	sense = &arqs->sts_sensedata;
5493 	bzero(sense, sizeof (struct scsi_extended_sense));
5494 	sata_fixed_sense_data_preset(sense);
5495 	return (sense);
5496 }
5497 
5498 
5499 /*
5500  * Emulated SATA Read/Write command completion for zero-length requests.
5501  * This request always succedes, so in synchronous mode it always returns
5502  * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the
5503  * callback cannot be scheduled.
5504  */
5505 static int
5506 sata_emul_rw_completion(sata_pkt_txlate_t *spx)
5507 {
5508 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5509 
5510 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5511 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5512 	scsipkt->pkt_reason = CMD_CMPLT;
5513 	*scsipkt->pkt_scbp = STATUS_GOOD;
5514 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5515 		/* scsi callback required - have to schedule it */
5516 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5517 		    (task_func_t *)scsipkt->pkt_comp,
5518 		    (void *)scsipkt, TQ_SLEEP) == NULL)
5519 			/* Scheduling the callback failed */
5520 			return (TRAN_BUSY);
5521 	}
5522 	return (TRAN_ACCEPT);
5523 }
5524 
5525 
5526 /*
5527  * Translate completion status of SATA read/write commands into scsi response.
5528  * pkt completion_reason is checked to determine the completion status.
5529  * Do scsi callback if necessary.
5530  *
5531  * Note: this function may be called also for synchronously executed
5532  * commands.
5533  * This function may be used only if scsi_pkt is non-NULL.
5534  */
5535 static void
5536 sata_txlt_rw_completion(sata_pkt_t *sata_pkt)
5537 {
5538 	sata_pkt_txlate_t *spx =
5539 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
5540 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
5541 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5542 	struct scsi_extended_sense *sense;
5543 	uint64_t lba;
5544 	struct buf *bp;
5545 	int rval;
5546 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5547 		/* Normal completion */
5548 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5549 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
5550 		scsipkt->pkt_reason = CMD_CMPLT;
5551 		*scsipkt->pkt_scbp = STATUS_GOOD;
5552 		if (spx->txlt_tmp_buf != NULL) {
5553 			/* Temporary buffer was used */
5554 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5555 			if (bp->b_flags & B_READ) {
5556 				rval = ddi_dma_sync(
5557 				    spx->txlt_buf_dma_handle, 0, 0,
5558 				    DDI_DMA_SYNC_FORCPU);
5559 				ASSERT(rval == DDI_SUCCESS);
5560 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
5561 				    bp->b_bcount);
5562 			}
5563 		}
5564 	} else {
5565 		/*
5566 		 * Something went wrong - analyze return
5567 		 */
5568 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5569 		    STATE_SENT_CMD | STATE_GOT_STATUS;
5570 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5571 		*scsipkt->pkt_scbp = STATUS_CHECK;
5572 		sense = sata_arq_sense(spx);
5573 		ASSERT(sense != NULL);
5574 
5575 		/*
5576 		 * SATA_PKT_DEV_ERROR is the only case where we may be able to
5577 		 * extract from device registers the failing LBA.
5578 		 */
5579 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
5580 			if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) &&
5581 			    (scmd->satacmd_lba_mid_msb != 0 ||
5582 			    scmd->satacmd_lba_high_msb != 0)) {
5583 				/*
5584 				 * We have problem reporting this cmd LBA
5585 				 * in fixed sense data format, because of
5586 				 * the size of the scsi LBA fields.
5587 				 */
5588 				sense->es_valid = 0;
5589 			} else {
5590 				sata_extract_error_lba(spx, &lba);
5591 				sense->es_info_1 = (lba & 0xFF000000) >> 24;
5592 				sense->es_info_2 = (lba & 0xFF0000) >> 16;
5593 				sense->es_info_3 = (lba & 0xFF00) >> 8;
5594 				sense->es_info_4 = lba & 0xFF;
5595 			}
5596 		} else {
5597 			/* Invalid extended sense info */
5598 			sense->es_valid = 0;
5599 		}
5600 
5601 		switch (sata_pkt->satapkt_reason) {
5602 		case SATA_PKT_PORT_ERROR:
5603 			/* We may want to handle DEV GONE state as well */
5604 			/*
5605 			 * We have no device data. Assume no data transfered.
5606 			 */
5607 			sense->es_key = KEY_HARDWARE_ERROR;
5608 			break;
5609 
5610 		case SATA_PKT_DEV_ERROR:
5611 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5612 			    SATA_STATUS_ERR) {
5613 				/*
5614 				 * determine dev error reason from error
5615 				 * reg content
5616 				 */
5617 				sata_decode_device_error(spx, sense);
5618 				if (sense->es_key == KEY_MEDIUM_ERROR) {
5619 					switch (scmd->satacmd_cmd_reg) {
5620 					case SATAC_READ_DMA:
5621 					case SATAC_READ_DMA_EXT:
5622 					case SATAC_READ_DMA_QUEUED:
5623 					case SATAC_READ_DMA_QUEUED_EXT:
5624 					case SATAC_READ_FPDMA_QUEUED:
5625 						/* Unrecovered read error */
5626 						sense->es_add_code =
5627 						    SD_SCSI_ASC_UNREC_READ_ERR;
5628 						break;
5629 					case SATAC_WRITE_DMA:
5630 					case SATAC_WRITE_DMA_EXT:
5631 					case SATAC_WRITE_DMA_QUEUED:
5632 					case SATAC_WRITE_DMA_QUEUED_EXT:
5633 					case SATAC_WRITE_FPDMA_QUEUED:
5634 						/* Write error */
5635 						sense->es_add_code =
5636 						    SD_SCSI_ASC_WRITE_ERR;
5637 						break;
5638 					default:
5639 						/* Internal error */
5640 						SATA_LOG_D((
5641 						    spx->txlt_sata_hba_inst,
5642 						    CE_WARN,
5643 						    "sata_txlt_rw_completion :"
5644 						    "internal error - invalid "
5645 						    "command 0x%2x",
5646 						    scmd->satacmd_cmd_reg));
5647 						break;
5648 					}
5649 				}
5650 				break;
5651 			}
5652 			/* No extended sense key - no info available */
5653 			scsipkt->pkt_reason = CMD_INCOMPLETE;
5654 			break;
5655 
5656 		case SATA_PKT_TIMEOUT:
5657 			scsipkt->pkt_reason = CMD_TIMEOUT;
5658 			scsipkt->pkt_statistics |=
5659 			    STAT_TIMEOUT | STAT_DEV_RESET;
5660 			sense->es_key = KEY_ABORTED_COMMAND;
5661 			break;
5662 
5663 		case SATA_PKT_ABORTED:
5664 			scsipkt->pkt_reason = CMD_ABORTED;
5665 			scsipkt->pkt_statistics |= STAT_ABORTED;
5666 			sense->es_key = KEY_ABORTED_COMMAND;
5667 			break;
5668 
5669 		case SATA_PKT_RESET:
5670 			scsipkt->pkt_reason = CMD_RESET;
5671 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5672 			sense->es_key = KEY_ABORTED_COMMAND;
5673 			break;
5674 
5675 		default:
5676 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5677 			    "sata_txlt_rw_completion: "
5678 			    "invalid packet completion reason"));
5679 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5680 			break;
5681 		}
5682 	}
5683 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5684 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5685 
5686 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5687 	    scsipkt->pkt_comp != NULL)
5688 		/* scsi callback required */
5689 		(*scsipkt->pkt_comp)(scsipkt);
5690 }
5691 
5692 
5693 /*
5694  * Translate completion status of non-data commands (i.e. commands returning
5695  * no data).
5696  * pkt completion_reason is checked to determine the completion status.
5697  * Do scsi callback if necessary (FLAG_NOINTR == 0)
5698  *
5699  * Note: this function may be called also for synchronously executed
5700  * commands.
5701  * This function may be used only if scsi_pkt is non-NULL.
5702  */
5703 
5704 static 	void
5705 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt)
5706 {
5707 	sata_pkt_txlate_t *spx =
5708 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
5709 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5710 	struct scsi_extended_sense *sense;
5711 
5712 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5713 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5714 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5715 		/* Normal completion */
5716 		scsipkt->pkt_reason = CMD_CMPLT;
5717 		*scsipkt->pkt_scbp = STATUS_GOOD;
5718 	} else {
5719 		/* Something went wrong */
5720 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5721 		*scsipkt->pkt_scbp = STATUS_CHECK;
5722 		sense = sata_arq_sense(spx);
5723 		switch (sata_pkt->satapkt_reason) {
5724 		case SATA_PKT_PORT_ERROR:
5725 			/*
5726 			 * We have no device data. Assume no data transfered.
5727 			 */
5728 			sense->es_key = KEY_HARDWARE_ERROR;
5729 			break;
5730 
5731 		case SATA_PKT_DEV_ERROR:
5732 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5733 			    SATA_STATUS_ERR) {
5734 				/*
5735 				 * determine dev error reason from error
5736 				 * reg content
5737 				 */
5738 				sata_decode_device_error(spx, sense);
5739 				break;
5740 			}
5741 			/* No extended sense key - no info available */
5742 			break;
5743 
5744 		case SATA_PKT_TIMEOUT:
5745 			scsipkt->pkt_reason = CMD_TIMEOUT;
5746 			scsipkt->pkt_statistics |=
5747 			    STAT_TIMEOUT | STAT_DEV_RESET;
5748 			/* No extended sense key ? */
5749 			break;
5750 
5751 		case SATA_PKT_ABORTED:
5752 			scsipkt->pkt_reason = CMD_ABORTED;
5753 			scsipkt->pkt_statistics |= STAT_ABORTED;
5754 			/* No extended sense key ? */
5755 			break;
5756 
5757 		case SATA_PKT_RESET:
5758 			/* pkt aborted by an explicit reset from a host */
5759 			scsipkt->pkt_reason = CMD_RESET;
5760 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5761 			break;
5762 
5763 		default:
5764 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5765 			    "sata_txlt_nodata_cmd_completion: "
5766 			    "invalid packet completion reason %d",
5767 			    sata_pkt->satapkt_reason));
5768 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5769 			break;
5770 		}
5771 
5772 	}
5773 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5774 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5775 
5776 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5777 	    scsipkt->pkt_comp != NULL)
5778 		/* scsi callback required */
5779 		(*scsipkt->pkt_comp)(scsipkt);
5780 }
5781 
5782 
5783 /*
5784  * Build Mode sense R/W recovery page
5785  * NOT IMPLEMENTED
5786  */
5787 
5788 static int
5789 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5790 {
5791 #ifndef __lock_lint
5792 	_NOTE(ARGUNUSED(sdinfo))
5793 	_NOTE(ARGUNUSED(pcntrl))
5794 	_NOTE(ARGUNUSED(buf))
5795 #endif
5796 	return (0);
5797 }
5798 
5799 /*
5800  * Build Mode sense caching page  -  scsi-3 implementation.
5801  * Page length distinguishes previous format from scsi-3 format.
5802  * buf must have space for 0x12 bytes.
5803  * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable.
5804  *
5805  */
5806 static int
5807 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5808 {
5809 	struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf;
5810 	sata_id_t *sata_id = &sdinfo->satadrv_id;
5811 
5812 	/*
5813 	 * Most of the fields are set to 0, being not supported and/or disabled
5814 	 */
5815 	bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3);
5816 
5817 	/* Saved paramters not supported */
5818 	if (pcntrl == 3)
5819 		return (0);
5820 	if (pcntrl == 0 || pcntrl == 2) {
5821 		/*
5822 		 * For now treat current and default parameters as same
5823 		 * That may have to change, if target driver will complain
5824 		 */
5825 		page->mode_page.code = MODEPAGE_CACHING;	/* PS = 0 */
5826 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
5827 
5828 		if ((sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) &&
5829 		    !(sata_id->ai_features85 & SATA_LOOK_AHEAD)) {
5830 			page->dra = 1;		/* Read Ahead disabled */
5831 			page->rcd = 1;		/* Read Cache disabled */
5832 		}
5833 		if ((sata_id->ai_cmdset82 & SATA_WRITE_CACHE) &&
5834 		    (sata_id->ai_features85 & SATA_WRITE_CACHE))
5835 			page->wce = 1;		/* Write Cache enabled */
5836 	} else {
5837 		/* Changeable parameters */
5838 		page->mode_page.code = MODEPAGE_CACHING;
5839 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
5840 		if (sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) {
5841 			page->dra = 1;
5842 			page->rcd = 1;
5843 		}
5844 		if (sata_id->ai_cmdset82 & SATA_WRITE_CACHE)
5845 			page->wce = 1;
5846 	}
5847 	return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
5848 	    sizeof (struct mode_page));
5849 }
5850 
5851 /*
5852  * Build Mode sense exception cntrl page
5853  */
5854 static int
5855 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5856 {
5857 	struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf;
5858 	sata_id_t *sata_id = &sdinfo->satadrv_id;
5859 
5860 	/*
5861 	 * Most of the fields are set to 0, being not supported and/or disabled
5862 	 */
5863 	bzero(buf, PAGELENGTH_INFO_EXCPT);
5864 
5865 	page->mode_page.code = MODEPAGE_INFO_EXCPT;
5866 	page->mode_page.length = PAGELENGTH_INFO_EXCPT;
5867 
5868 	/* Indicate that this is page is saveable */
5869 	page->mode_page.ps = 1;
5870 
5871 	/*
5872 	 * We will return the same data for default, current and saved page.
5873 	 * The only changeable bit is dexcpt and that bit is required
5874 	 * by the ATA specification to be preserved across power cycles.
5875 	 */
5876 	if (pcntrl != 1) {
5877 		page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED);
5878 		page->mrie = MRIE_ONLY_ON_REQUEST;
5879 	}
5880 	else
5881 		page->dexcpt = 1;	/* Only changeable parameter */
5882 
5883 	return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_info_excpt_page));
5884 }
5885 
5886 
5887 static int
5888 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5889 {
5890 	struct mode_acoustic_management *page =
5891 	    (struct mode_acoustic_management *)buf;
5892 	sata_id_t *sata_id = &sdinfo->satadrv_id;
5893 
5894 	/*
5895 	 * Most of the fields are set to 0, being not supported and/or disabled
5896 	 */
5897 	bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT);
5898 
5899 	switch (pcntrl) {
5900 	case P_CNTRL_DEFAULT:
5901 		/*  default paramters not supported */
5902 		return (0);
5903 
5904 	case P_CNTRL_CURRENT:
5905 	case P_CNTRL_SAVED:
5906 		/* Saved and current are supported and are identical */
5907 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
5908 		page->mode_page.length =
5909 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
5910 		page->mode_page.ps = 1;
5911 
5912 		/* Word 83 indicates if feature is supported */
5913 		/* If feature is not supported */
5914 		if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) {
5915 			page->acoustic_manag_enable =
5916 			    ACOUSTIC_DISABLED;
5917 		} else {
5918 			page->acoustic_manag_enable =
5919 			    ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT)
5920 			    != 0);
5921 			/* Word 94 inidicates the value */
5922 #ifdef	_LITTLE_ENDIAN
5923 			page->acoustic_manag_level =
5924 			    (uchar_t)sata_id->ai_acoustic;
5925 			page->vendor_recommended_value =
5926 			    sata_id->ai_acoustic >> 8;
5927 #else
5928 			page->acoustic_manag_level =
5929 			    sata_id->ai_acoustic >> 8;
5930 			page->vendor_recommended_value =
5931 			    (uchar_t)sata_id->ai_acoustic;
5932 #endif
5933 		}
5934 		break;
5935 
5936 	case P_CNTRL_CHANGEABLE:
5937 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
5938 		page->mode_page.length =
5939 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
5940 		page->mode_page.ps = 1;
5941 
5942 		/* Word 83 indicates if the feature is supported */
5943 		if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) {
5944 			page->acoustic_manag_enable =
5945 			    ACOUSTIC_ENABLED;
5946 			page->acoustic_manag_level = 0xff;
5947 		}
5948 		break;
5949 	}
5950 	return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
5951 	    sizeof (struct mode_page));
5952 }
5953 
5954 
5955 /*
5956  * Build Mode sense power condition page
5957  * NOT IMPLEMENTED.
5958  */
5959 static int
5960 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5961 {
5962 #ifndef __lock_lint
5963 	_NOTE(ARGUNUSED(sdinfo))
5964 	_NOTE(ARGUNUSED(pcntrl))
5965 	_NOTE(ARGUNUSED(buf))
5966 #endif
5967 	return (0);
5968 }
5969 
5970 
5971 /*
5972  * Process mode select caching page 8 (scsi3 format only).
5973  * Read Ahead (same as read cache) and Write Cache may be turned on and off
5974  * if these features are supported by the device. If these features are not
5975  * supported, quietly ignore them.
5976  * This function fails only if the SET FEATURE command sent to
5977  * the device fails. The page format is not varified, assuming that the
5978  * target driver operates correctly - if parameters length is too short,
5979  * we just drop the page.
5980  * Two command may be sent if both Read Cache/Read Ahead and Write Cache
5981  * setting have to be changed.
5982  * SET FEATURE command is executed synchronously, i.e. we wait here until
5983  * it is completed, regardless of the scsi pkt directives.
5984  *
5985  * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e.
5986  * changing DRA will change RCD.
5987  *
5988  * More than one SATA command may be executed to perform operations specified
5989  * by mode select pages. The first error terminates further execution.
5990  * Operations performed successully are not backed-up in such case.
5991  *
5992  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
5993  * If operation resulted in changing device setup, dmod flag should be set to
5994  * one (1). If parameters were not changed, dmod flag should be set to 0.
5995  * Upon return, if operation required sending command to the device, the rval
5996  * should be set to the value returned by sata_hba_start. If operation
5997  * did not require device access, rval should be set to TRAN_ACCEPT.
5998  * The pagelen should be set to the length of the page.
5999  *
6000  * This function has to be called with a port mutex held.
6001  *
6002  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6003  */
6004 int
6005 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page,
6006     int parmlen, int *pagelen, int *rval, int *dmod)
6007 {
6008 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6009 	sata_drive_info_t *sdinfo;
6010 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6011 	sata_id_t *sata_id;
6012 	struct scsi_extended_sense *sense;
6013 	int wce, dra;	/* Current settings */
6014 
6015 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6016 	    &spx->txlt_sata_pkt->satapkt_device);
6017 	sata_id = &sdinfo->satadrv_id;
6018 	*dmod = 0;
6019 
6020 	/* Verify parameters length. If too short, drop it */
6021 	if (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
6022 	    sizeof (struct mode_page) < parmlen) {
6023 		*scsipkt->pkt_scbp = STATUS_CHECK;
6024 		sense = sata_arq_sense(spx);
6025 		sense->es_key = KEY_ILLEGAL_REQUEST;
6026 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6027 		*pagelen = parmlen;
6028 		*rval = TRAN_ACCEPT;
6029 		return (SATA_FAILURE);
6030 	}
6031 
6032 	*pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page);
6033 
6034 	/*
6035 	 * We can manipulate only write cache and read ahead
6036 	 * (read cache) setting.
6037 	 */
6038 	if (!(sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) &&
6039 	    !(sata_id->ai_cmdset82 & SATA_WRITE_CACHE)) {
6040 		/*
6041 		 * None of the features is supported - ignore
6042 		 */
6043 		*rval = TRAN_ACCEPT;
6044 		return (SATA_SUCCESS);
6045 	}
6046 
6047 	/* Current setting of Read Ahead (and Read Cache) */
6048 	if (sata_id->ai_features85 & SATA_LOOK_AHEAD)
6049 		dra = 0;	/* 0 == not disabled */
6050 	else
6051 		dra = 1;
6052 	/* Current setting of Write Cache */
6053 	if (sata_id->ai_features85 & SATA_WRITE_CACHE)
6054 		wce = 1;
6055 	else
6056 		wce = 0;
6057 
6058 	if (page->dra == dra && page->wce == wce && page->rcd == dra) {
6059 		/* nothing to do */
6060 		*rval = TRAN_ACCEPT;
6061 		return (SATA_SUCCESS);
6062 	}
6063 	/*
6064 	 * Need to flip some setting
6065 	 * Set-up Internal SET FEATURES command(s)
6066 	 */
6067 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6068 	scmd->satacmd_addr_type = 0;
6069 	scmd->satacmd_device_reg = 0;
6070 	scmd->satacmd_status_reg = 0;
6071 	scmd->satacmd_error_reg = 0;
6072 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
6073 	if (page->dra != dra || page->rcd != dra) {
6074 		/* Need to flip read ahead setting */
6075 		if (dra == 0)
6076 			/* Disable read ahead / read cache */
6077 			scmd->satacmd_features_reg =
6078 			    SATAC_SF_DISABLE_READ_AHEAD;
6079 		else
6080 			/* Enable read ahead  / read cache */
6081 			scmd->satacmd_features_reg =
6082 			    SATAC_SF_ENABLE_READ_AHEAD;
6083 
6084 		/* Transfer command to HBA */
6085 		if (sata_hba_start(spx, rval) != 0)
6086 			/*
6087 			 * Pkt not accepted for execution.
6088 			 */
6089 			return (SATA_FAILURE);
6090 
6091 		*dmod = 1;
6092 
6093 		/* Now process return */
6094 		if (spx->txlt_sata_pkt->satapkt_reason !=
6095 		    SATA_PKT_COMPLETED) {
6096 			goto failure;	/* Terminate */
6097 		}
6098 	}
6099 
6100 	/* Note that the packet is not removed, so it could be re-used */
6101 	if (page->wce != wce) {
6102 		/* Need to flip Write Cache setting */
6103 		if (page->wce == 1)
6104 			/* Enable write cache */
6105 			scmd->satacmd_features_reg =
6106 			    SATAC_SF_ENABLE_WRITE_CACHE;
6107 		else
6108 			/* Disable write cache */
6109 			scmd->satacmd_features_reg =
6110 			    SATAC_SF_DISABLE_WRITE_CACHE;
6111 
6112 		/* Transfer command to HBA */
6113 		if (sata_hba_start(spx, rval) != 0)
6114 			/*
6115 			 * Pkt not accepted for execution.
6116 			 */
6117 			return (SATA_FAILURE);
6118 
6119 		*dmod = 1;
6120 
6121 		/* Now process return */
6122 		if (spx->txlt_sata_pkt->satapkt_reason !=
6123 		    SATA_PKT_COMPLETED) {
6124 			goto failure;
6125 		}
6126 	}
6127 	return (SATA_SUCCESS);
6128 
6129 failure:
6130 	sata_xlate_errors(spx);
6131 
6132 	return (SATA_FAILURE);
6133 }
6134 
6135 /*
6136  * Process mode select informational exceptions control page 0x1c
6137  *
6138  * The only changeable bit is dexcpt (disable exceptions).
6139  * MRIE (method of reporting informational exceptions) must be
6140  * "only on request".
6141  *
6142  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
6143  * If operation resulted in changing device setup, dmod flag should be set to
6144  * one (1). If parameters were not changed, dmod flag should be set to 0.
6145  * Upon return, if operation required sending command to the device, the rval
6146  * should be set to the value returned by sata_hba_start. If operation
6147  * did not require device access, rval should be set to TRAN_ACCEPT.
6148  * The pagelen should be set to the length of the page.
6149  *
6150  * This function has to be called with a port mutex held.
6151  *
6152  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6153  */
6154 static	int
6155 sata_mode_select_page_1c(
6156 	sata_pkt_txlate_t *spx,
6157 	struct mode_info_excpt_page *page,
6158 	int parmlen,
6159 	int *pagelen,
6160 	int *rval,
6161 	int *dmod)
6162 {
6163 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6164 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6165 	sata_drive_info_t *sdinfo;
6166 	sata_id_t *sata_id;
6167 	struct scsi_extended_sense *sense;
6168 
6169 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6170 	    &spx->txlt_sata_pkt->satapkt_device);
6171 	sata_id = &sdinfo->satadrv_id;
6172 
6173 	*dmod = 0;
6174 
6175 	/* Verify parameters length. If too short, drop it */
6176 	if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) < parmlen) ||
6177 	    page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) {
6178 		*scsipkt->pkt_scbp = STATUS_CHECK;
6179 		sense = sata_arq_sense(spx);
6180 		sense->es_key = KEY_ILLEGAL_REQUEST;
6181 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6182 		*pagelen = parmlen;
6183 		*rval = TRAN_ACCEPT;
6184 		return (SATA_FAILURE);
6185 	}
6186 
6187 	*pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page);
6188 
6189 	if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
6190 		*scsipkt->pkt_scbp = STATUS_CHECK;
6191 		sense = sata_arq_sense(spx);
6192 		sense->es_key = KEY_ILLEGAL_REQUEST;
6193 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6194 		*pagelen = parmlen;
6195 		*rval = TRAN_ACCEPT;
6196 		return (SATA_FAILURE);
6197 	}
6198 
6199 	/* If already in the state requested, we are done */
6200 	if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
6201 		/* nothing to do */
6202 		*rval = TRAN_ACCEPT;
6203 		return (SATA_SUCCESS);
6204 	}
6205 
6206 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6207 
6208 	/* Build SMART_ENABLE or SMART_DISABLE command */
6209 	scmd->satacmd_addr_type = 0;		/* N/A */
6210 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
6211 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
6212 	scmd->satacmd_features_reg = page->dexcpt ?
6213 	    SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS;
6214 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
6215 	scmd->satacmd_cmd_reg = SATAC_SMART;
6216 
6217 	/* Transfer command to HBA */
6218 	if (sata_hba_start(spx, rval) != 0)
6219 		/*
6220 		 * Pkt not accepted for execution.
6221 		 */
6222 		return (SATA_FAILURE);
6223 
6224 	*dmod = 1;	/* At least may have been modified */
6225 
6226 	/* Now process return */
6227 	if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED)
6228 		return (SATA_SUCCESS);
6229 
6230 	/* Packet did not complete successfully */
6231 	sata_xlate_errors(spx);
6232 
6233 	return (SATA_FAILURE);
6234 }
6235 
6236 int
6237 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct
6238     mode_acoustic_management *page, int parmlen, int *pagelen,
6239     int *rval, int *dmod)
6240 {
6241 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6242 	sata_drive_info_t *sdinfo;
6243 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6244 	sata_id_t *sata_id;
6245 	struct scsi_extended_sense *sense;
6246 
6247 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6248 	    &spx->txlt_sata_pkt->satapkt_device);
6249 	sata_id = &sdinfo->satadrv_id;
6250 	*dmod = 0;
6251 
6252 	/* If parmlen is too short or the feature is not supported, drop it */
6253 	if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6254 	    sizeof (struct mode_page)) < parmlen) ||
6255 	    (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) {
6256 		*scsipkt->pkt_scbp = STATUS_CHECK;
6257 		sense = sata_arq_sense(spx);
6258 		sense->es_key = KEY_ILLEGAL_REQUEST;
6259 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6260 		*pagelen = parmlen;
6261 		*rval = TRAN_ACCEPT;
6262 		return (SATA_FAILURE);
6263 	}
6264 
6265 	*pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6266 	    sizeof (struct mode_page);
6267 
6268 	/*
6269 	 * We can enable and disable acoustice management and
6270 	 * set the acoustic management level.
6271 	 */
6272 
6273 	/*
6274 	 * Set-up Internal SET FEATURES command(s)
6275 	 */
6276 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6277 	scmd->satacmd_addr_type = 0;
6278 	scmd->satacmd_device_reg = 0;
6279 	scmd->satacmd_status_reg = 0;
6280 	scmd->satacmd_error_reg = 0;
6281 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
6282 	if (page->acoustic_manag_enable) {
6283 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC;
6284 		scmd->satacmd_sec_count_lsb = page->acoustic_manag_level;
6285 	} else {	/* disabling acoustic management */
6286 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC;
6287 	}
6288 
6289 	/* Transfer command to HBA */
6290 	if (sata_hba_start(spx, rval) != 0)
6291 		/*
6292 		 * Pkt not accepted for execution.
6293 		 */
6294 		return (SATA_FAILURE);
6295 
6296 	/* Now process return */
6297 	if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) {
6298 		sata_xlate_errors(spx);
6299 		return (SATA_FAILURE);
6300 	}
6301 
6302 	*dmod = 1;
6303 
6304 	return (SATA_SUCCESS);
6305 }
6306 
6307 
6308 
6309 
6310 /*
6311  * sata_build_lsense_page0() is used to create the
6312  * SCSI LOG SENSE page 0 (supported log pages)
6313  *
6314  * Currently supported pages are 0, 0x10, 0x2f and 0x30
6315  * (supported log pages, self-test results, informational exceptions
6316  *  and Sun vendor specific ATA SMART data).
6317  *
6318  * Takes a sata_drive_info t * and the address of a buffer
6319  * in which to create the page information.
6320  *
6321  * Returns the number of bytes valid in the buffer.
6322  */
6323 static	int
6324 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf)
6325 {
6326 	struct log_parameter *lpp = (struct log_parameter *)buf;
6327 	uint8_t *page_ptr = (uint8_t *)lpp->param_values;
6328 	int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */
6329 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6330 
6331 	lpp->param_code[0] = 0;
6332 	lpp->param_code[1] = 0;
6333 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
6334 	*page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES;
6335 
6336 	if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
6337 		if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) {
6338 			*page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS;
6339 			++num_pages_supported;
6340 		}
6341 		*page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS;
6342 		++num_pages_supported;
6343 		*page_ptr++ = PAGE_CODE_SMART_READ_DATA;
6344 		++num_pages_supported;
6345 	}
6346 
6347 	lpp->param_len = num_pages_supported;
6348 
6349 	return ((&lpp->param_values[0] - (uint8_t *)lpp) +
6350 	    num_pages_supported);
6351 }
6352 
6353 /*
6354  * sata_build_lsense_page_10() is used to create the
6355  * SCSI LOG SENSE page 0x10 (self-test results)
6356  *
6357  * Takes a sata_drive_info t * and the address of a buffer
6358  * in which to create the page information as well as a sata_hba_inst_t *.
6359  *
6360  * Returns the number of bytes valid in the buffer.
6361  */
6362 static	int
6363 sata_build_lsense_page_10(
6364 	sata_drive_info_t *sdinfo,
6365 	uint8_t *buf,
6366 	sata_hba_inst_t *sata_hba_inst)
6367 {
6368 	struct log_parameter *lpp = (struct log_parameter *)buf;
6369 	int rval;
6370 
6371 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6372 		struct smart_ext_selftest_log *ext_selftest_log;
6373 
6374 		ext_selftest_log = kmem_zalloc(
6375 		    sizeof (struct smart_ext_selftest_log), KM_SLEEP);
6376 
6377 		rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo,
6378 		    ext_selftest_log, 0);
6379 		if (rval == 0) {
6380 			int index, start_index;
6381 			struct smart_ext_selftest_log_entry *entry;
6382 			static const struct smart_ext_selftest_log_entry empty =
6383 			    {0};
6384 			uint16_t block_num;
6385 			int count;
6386 			boolean_t only_one_block = B_FALSE;
6387 
6388 			index = ext_selftest_log->
6389 			    smart_ext_selftest_log_index[0];
6390 			index |= ext_selftest_log->
6391 			    smart_ext_selftest_log_index[1] << 8;
6392 			if (index == 0)
6393 				goto out;
6394 
6395 			--index;	/* Correct for 0 origin */
6396 			start_index = index;	/* remember where we started */
6397 			block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6398 			if (block_num != 0) {
6399 				rval = sata_ext_smart_selftest_read_log(
6400 				    sata_hba_inst, sdinfo, ext_selftest_log,
6401 				    block_num);
6402 				if (rval != 0)
6403 					goto out;
6404 			}
6405 			index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6406 			entry =
6407 			    &ext_selftest_log->
6408 			    smart_ext_selftest_log_entries[index];
6409 
6410 			for (count = 1;
6411 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
6412 			    ++count) {
6413 				uint8_t status;
6414 				uint8_t code;
6415 				uint8_t sense_key;
6416 				uint8_t add_sense_code;
6417 				uint8_t add_sense_code_qual;
6418 
6419 				/* If this is an unused entry, we are done */
6420 				if (bcmp(entry, &empty, sizeof (empty)) == 0) {
6421 					/* Broken firmware on some disks */
6422 					if (index + 1 ==
6423 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK) {
6424 						--entry;
6425 						--index;
6426 						if (bcmp(entry, &empty,
6427 						    sizeof (empty)) == 0)
6428 							goto out;
6429 					} else
6430 						goto out;
6431 				}
6432 
6433 				if (only_one_block &&
6434 				    start_index == index)
6435 					goto out;
6436 
6437 				lpp->param_code[0] = 0;
6438 				lpp->param_code[1] = count;
6439 				lpp->param_ctrl_flags =
6440 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
6441 				lpp->param_len =
6442 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
6443 
6444 				status = entry->smart_ext_selftest_log_status;
6445 				status >>= 4;
6446 				switch (status) {
6447 				case 0:
6448 				default:
6449 					sense_key = KEY_NO_SENSE;
6450 					add_sense_code =
6451 					    SD_SCSI_ASC_NO_ADD_SENSE;
6452 					add_sense_code_qual = 0;
6453 					break;
6454 				case 1:
6455 					sense_key = KEY_ABORTED_COMMAND;
6456 					add_sense_code =
6457 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6458 					add_sense_code_qual = SCSI_COMPONENT_81;
6459 					break;
6460 				case 2:
6461 					sense_key = KEY_ABORTED_COMMAND;
6462 					add_sense_code =
6463 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6464 					add_sense_code_qual = SCSI_COMPONENT_82;
6465 					break;
6466 				case 3:
6467 					sense_key = KEY_ABORTED_COMMAND;
6468 					add_sense_code =
6469 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6470 					add_sense_code_qual = SCSI_COMPONENT_83;
6471 					break;
6472 				case 4:
6473 					sense_key = KEY_HARDWARE_ERROR;
6474 					add_sense_code =
6475 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6476 					add_sense_code_qual = SCSI_COMPONENT_84;
6477 					break;
6478 				case 5:
6479 					sense_key = KEY_HARDWARE_ERROR;
6480 					add_sense_code =
6481 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6482 					add_sense_code_qual = SCSI_COMPONENT_85;
6483 					break;
6484 				case 6:
6485 					sense_key = KEY_HARDWARE_ERROR;
6486 					add_sense_code =
6487 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6488 					add_sense_code_qual = SCSI_COMPONENT_86;
6489 					break;
6490 				case 7:
6491 					sense_key = KEY_MEDIUM_ERROR;
6492 					add_sense_code =
6493 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6494 					add_sense_code_qual = SCSI_COMPONENT_87;
6495 					break;
6496 				case 8:
6497 					sense_key = KEY_HARDWARE_ERROR;
6498 					add_sense_code =
6499 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6500 					add_sense_code_qual = SCSI_COMPONENT_88;
6501 					break;
6502 				}
6503 				code = 0;	/* unspecified */
6504 				status |= (code << 4);
6505 				lpp->param_values[0] = status;
6506 				lpp->param_values[1] = 0; /* unspecified */
6507 				lpp->param_values[2] = entry->
6508 				    smart_ext_selftest_log_timestamp[1];
6509 				lpp->param_values[3] = entry->
6510 				    smart_ext_selftest_log_timestamp[0];
6511 				if (status != 0) {
6512 					lpp->param_values[4] = 0;
6513 					lpp->param_values[5] = 0;
6514 					lpp->param_values[6] = entry->
6515 					    smart_ext_selftest_log_failing_lba
6516 					    [5];
6517 					lpp->param_values[7] = entry->
6518 					    smart_ext_selftest_log_failing_lba
6519 					    [4];
6520 					lpp->param_values[8] = entry->
6521 					    smart_ext_selftest_log_failing_lba
6522 					    [3];
6523 					lpp->param_values[9] = entry->
6524 					    smart_ext_selftest_log_failing_lba
6525 					    [2];
6526 					lpp->param_values[10] = entry->
6527 					    smart_ext_selftest_log_failing_lba
6528 					    [1];
6529 					lpp->param_values[11] = entry->
6530 					    smart_ext_selftest_log_failing_lba
6531 					    [0];
6532 				} else {	/* No bad block address */
6533 					lpp->param_values[4] = 0xff;
6534 					lpp->param_values[5] = 0xff;
6535 					lpp->param_values[6] = 0xff;
6536 					lpp->param_values[7] = 0xff;
6537 					lpp->param_values[8] = 0xff;
6538 					lpp->param_values[9] = 0xff;
6539 					lpp->param_values[10] = 0xff;
6540 					lpp->param_values[11] = 0xff;
6541 				}
6542 
6543 				lpp->param_values[12] = sense_key;
6544 				lpp->param_values[13] = add_sense_code;
6545 				lpp->param_values[14] = add_sense_code_qual;
6546 				lpp->param_values[15] = 0; /* undefined */
6547 
6548 				lpp = (struct log_parameter *)
6549 				    (((uint8_t *)lpp) +
6550 				    SCSI_LOG_PARAM_HDR_LEN +
6551 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
6552 
6553 				--index;	/* Back up to previous entry */
6554 				if (index < 0) {
6555 					if (block_num > 0) {
6556 						--block_num;
6557 					} else {
6558 						struct read_log_ext_directory
6559 						    logdir;
6560 
6561 						rval =
6562 						    sata_read_log_ext_directory(
6563 						    sata_hba_inst, sdinfo,
6564 						    &logdir);
6565 						if (rval == -1)
6566 							goto out;
6567 						if ((logdir.read_log_ext_vers
6568 						    [0] == 0) &&
6569 						    (logdir.read_log_ext_vers
6570 						    [1] == 0))
6571 							goto out;
6572 						block_num =
6573 						    logdir.read_log_ext_nblks
6574 						    [EXT_SMART_SELFTEST_LOG_PAGE
6575 						    - 1][0];
6576 						block_num |= logdir.
6577 						    read_log_ext_nblks
6578 						    [EXT_SMART_SELFTEST_LOG_PAGE
6579 						    - 1][1] << 8;
6580 						--block_num;
6581 						only_one_block =
6582 						    (block_num == 0);
6583 					}
6584 					rval = sata_ext_smart_selftest_read_log(
6585 					    sata_hba_inst, sdinfo,
6586 					    ext_selftest_log, block_num);
6587 					if (rval != 0)
6588 						goto out;
6589 
6590 					index =
6591 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK -
6592 					    1;
6593 				}
6594 				index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6595 				entry = &ext_selftest_log->
6596 				    smart_ext_selftest_log_entries[index];
6597 			}
6598 		}
6599 out:
6600 		kmem_free(ext_selftest_log,
6601 		    sizeof (struct smart_ext_selftest_log));
6602 	} else {
6603 		struct smart_selftest_log *selftest_log;
6604 
6605 		selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log),
6606 		    KM_SLEEP);
6607 
6608 		rval = sata_smart_selftest_log(sata_hba_inst, sdinfo,
6609 		    selftest_log);
6610 
6611 		if (rval == 0) {
6612 			int index;
6613 			int count;
6614 			struct smart_selftest_log_entry *entry;
6615 			static const struct smart_selftest_log_entry empty =
6616 			    { 0 };
6617 
6618 			index = selftest_log->smart_selftest_log_index;
6619 			if (index == 0)
6620 				goto done;
6621 			--index;	/* Correct for 0 origin */
6622 			entry = &selftest_log->
6623 			    smart_selftest_log_entries[index];
6624 			for (count = 1;
6625 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
6626 			    ++count) {
6627 				uint8_t status;
6628 				uint8_t code;
6629 				uint8_t sense_key;
6630 				uint8_t add_sense_code;
6631 				uint8_t add_sense_code_qual;
6632 
6633 				if (bcmp(entry, &empty, sizeof (empty)) == 0)
6634 					goto done;
6635 
6636 				lpp->param_code[0] = 0;
6637 				lpp->param_code[1] = count;
6638 				lpp->param_ctrl_flags =
6639 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
6640 				lpp->param_len =
6641 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
6642 
6643 				status = entry->smart_selftest_log_status;
6644 				status >>= 4;
6645 				switch (status) {
6646 				case 0:
6647 				default:
6648 					sense_key = KEY_NO_SENSE;
6649 					add_sense_code =
6650 					    SD_SCSI_ASC_NO_ADD_SENSE;
6651 					break;
6652 				case 1:
6653 					sense_key = KEY_ABORTED_COMMAND;
6654 					add_sense_code =
6655 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6656 					add_sense_code_qual = SCSI_COMPONENT_81;
6657 					break;
6658 				case 2:
6659 					sense_key = KEY_ABORTED_COMMAND;
6660 					add_sense_code =
6661 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6662 					add_sense_code_qual = SCSI_COMPONENT_82;
6663 					break;
6664 				case 3:
6665 					sense_key = KEY_ABORTED_COMMAND;
6666 					add_sense_code =
6667 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6668 					add_sense_code_qual = SCSI_COMPONENT_83;
6669 					break;
6670 				case 4:
6671 					sense_key = KEY_HARDWARE_ERROR;
6672 					add_sense_code =
6673 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6674 					add_sense_code_qual = SCSI_COMPONENT_84;
6675 					break;
6676 				case 5:
6677 					sense_key = KEY_HARDWARE_ERROR;
6678 					add_sense_code =
6679 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6680 					add_sense_code_qual = SCSI_COMPONENT_85;
6681 					break;
6682 				case 6:
6683 					sense_key = KEY_HARDWARE_ERROR;
6684 					add_sense_code =
6685 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6686 					add_sense_code_qual = SCSI_COMPONENT_86;
6687 					break;
6688 				case 7:
6689 					sense_key = KEY_MEDIUM_ERROR;
6690 					add_sense_code =
6691 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6692 					add_sense_code_qual = SCSI_COMPONENT_87;
6693 					break;
6694 				case 8:
6695 					sense_key = KEY_HARDWARE_ERROR;
6696 					add_sense_code =
6697 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6698 					add_sense_code_qual = SCSI_COMPONENT_88;
6699 					break;
6700 				}
6701 				code = 0;	/* unspecified */
6702 				status |= (code << 4);
6703 				lpp->param_values[0] = status;
6704 				lpp->param_values[1] = 0; /* unspecified */
6705 				lpp->param_values[2] = entry->
6706 				    smart_selftest_log_timestamp[1];
6707 				lpp->param_values[3] = entry->
6708 				    smart_selftest_log_timestamp[0];
6709 				if (status != 0) {
6710 					lpp->param_values[4] = 0;
6711 					lpp->param_values[5] = 0;
6712 					lpp->param_values[6] = 0;
6713 					lpp->param_values[7] = 0;
6714 					lpp->param_values[8] = entry->
6715 					    smart_selftest_log_failing_lba[3];
6716 					lpp->param_values[9] = entry->
6717 					    smart_selftest_log_failing_lba[2];
6718 					lpp->param_values[10] = entry->
6719 					    smart_selftest_log_failing_lba[1];
6720 					lpp->param_values[11] = entry->
6721 					    smart_selftest_log_failing_lba[0];
6722 				} else {	/* No block address */
6723 					lpp->param_values[4] = 0xff;
6724 					lpp->param_values[5] = 0xff;
6725 					lpp->param_values[6] = 0xff;
6726 					lpp->param_values[7] = 0xff;
6727 					lpp->param_values[8] = 0xff;
6728 					lpp->param_values[9] = 0xff;
6729 					lpp->param_values[10] = 0xff;
6730 					lpp->param_values[11] = 0xff;
6731 				}
6732 				lpp->param_values[12] = sense_key;
6733 				lpp->param_values[13] = add_sense_code;
6734 				lpp->param_values[14] = add_sense_code_qual;
6735 				lpp->param_values[15] = 0; /* undefined */
6736 
6737 				lpp = (struct log_parameter *)
6738 				    (((uint8_t *)lpp) +
6739 				    SCSI_LOG_PARAM_HDR_LEN +
6740 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
6741 				--index;	/* back up to previous entry */
6742 				if (index < 0) {
6743 					index =
6744 					    NUM_SMART_SELFTEST_LOG_ENTRIES - 1;
6745 				}
6746 				entry = &selftest_log->
6747 				    smart_selftest_log_entries[index];
6748 			}
6749 		}
6750 done:
6751 		kmem_free(selftest_log, sizeof (struct smart_selftest_log));
6752 	}
6753 
6754 	return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) *
6755 	    SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS);
6756 }
6757 
6758 /*
6759  * sata_build_lsense_page_2f() is used to create the
6760  * SCSI LOG SENSE page 0x10 (informational exceptions)
6761  *
6762  * Takes a sata_drive_info t * and the address of a buffer
6763  * in which to create the page information as well as a sata_hba_inst_t *.
6764  *
6765  * Returns the number of bytes valid in the buffer.
6766  */
6767 static	int
6768 sata_build_lsense_page_2f(
6769 	sata_drive_info_t *sdinfo,
6770 	uint8_t *buf,
6771 	sata_hba_inst_t *sata_hba_inst)
6772 {
6773 	struct log_parameter *lpp = (struct log_parameter *)buf;
6774 	int rval;
6775 	uint8_t *smart_data;
6776 	uint8_t temp;
6777 	sata_id_t *sata_id;
6778 #define	SMART_NO_TEMP	0xff
6779 
6780 	lpp->param_code[0] = 0;
6781 	lpp->param_code[1] = 0;
6782 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
6783 
6784 	/* Now get the SMART status w.r.t. threshold exceeded */
6785 	rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo);
6786 	switch (rval) {
6787 	case 1:
6788 		lpp->param_values[0] = SCSI_PREDICTED_FAILURE;
6789 		lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE;
6790 		break;
6791 	case 0:
6792 	case -1:	/* failed to get data */
6793 		lpp->param_values[0] = 0;	/* No failure predicted */
6794 		lpp->param_values[1] = 0;
6795 		break;
6796 #if defined(SATA_DEBUG)
6797 	default:
6798 		cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value");
6799 		/* NOTREACHED */
6800 #endif
6801 	}
6802 
6803 	sata_id = &sdinfo->satadrv_id;
6804 	if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP))
6805 		temp = SMART_NO_TEMP;
6806 	else {
6807 		/* Now get the temperature */
6808 		smart_data = kmem_zalloc(512, KM_SLEEP);
6809 		rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data,
6810 		    SCT_STATUS_LOG_PAGE, 1);
6811 		if (rval == -1)
6812 			temp = SMART_NO_TEMP;
6813 		else {
6814 			temp = smart_data[200];
6815 			if (temp & 0x80) {
6816 				if (temp & 0x7f)
6817 					temp = 0;
6818 				else
6819 					temp = SMART_NO_TEMP;
6820 			}
6821 		}
6822 		kmem_free(smart_data, 512);
6823 	}
6824 
6825 	lpp->param_values[2] = temp;	/* most recent temperature */
6826 	lpp->param_values[3] = 0;	/* required vendor specific byte */
6827 
6828 	lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN;
6829 
6830 
6831 	return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN);
6832 }
6833 
6834 /*
6835  * sata_build_lsense_page_30() is used to create the
6836  * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data).
6837  *
6838  * Takes a sata_drive_info t * and the address of a buffer
6839  * in which to create the page information as well as a sata_hba_inst_t *.
6840  *
6841  * Returns the number of bytes valid in the buffer.
6842  */
6843 static int
6844 sata_build_lsense_page_30(
6845 	sata_drive_info_t *sdinfo,
6846 	uint8_t *buf,
6847 	sata_hba_inst_t *sata_hba_inst)
6848 {
6849 	struct smart_data *smart_data = (struct smart_data *)buf;
6850 	int rval;
6851 
6852 	/* Now do the SMART READ DATA */
6853 	rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data);
6854 	if (rval == -1)
6855 		return (0);
6856 
6857 	return (sizeof (struct smart_data));
6858 }
6859 
6860 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */
6861 
6862 /*
6863  * Start command for ATAPI device.
6864  * This function processes scsi_pkt requests.
6865  * Only CD/DVD devices are supported.
6866  * Most commands are packet without any translation into Packet Command.
6867  * Some may be trapped and executed as SATA commands (not clear which one).
6868  *
6869  * Returns TRAN_ACCEPT if command is accepted for execution (or completed
6870  * execution).
6871  * Returns other TRAN_XXXX codes if command is not accepted or completed
6872  * (see return values for sata_hba_start()).
6873  *
6874  * Note:
6875  * Inquiry cdb format differs between transport version 2 and 3.
6876  * However, the transport version 3 devices that were checked did not adhere
6877  * to the specification (ignored MSB of the allocation length). Therefore,
6878  * the transport version is not checked, but Inquiry allocation length is
6879  * truncated to 255 bytes if the original allocation length set-up by the
6880  * target driver is greater than 255 bytes.
6881  */
6882 static int
6883 sata_txlt_atapi(sata_pkt_txlate_t *spx)
6884 {
6885 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6886 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6887 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
6888 	sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx);
6889 	sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba,
6890 	    &spx->txlt_sata_pkt->satapkt_device);
6891 	int cport = SATA_TXLT_CPORT(spx);
6892 	int cdblen;
6893 	int rval, reason;
6894 	int synch;
6895 	union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp;
6896 
6897 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
6898 
6899 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
6900 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6901 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6902 		return (rval);
6903 	}
6904 
6905 	/*
6906 	 * ATAPI device executes some ATA commands in addition to MMC command
6907 	 * set. These ATA commands may be executed by the regular SATA
6908 	 * translation functions. None needs to be captured now.
6909 	 * Other commands belong to MMC command set and are delivered
6910 	 * to ATAPI device via Packet Command.
6911 	 */
6912 
6913 	/* Check the size of cdb */
6914 	cdblen = scsi_cdb_size[GETGROUP(cdbp)];
6915 	if (cdblen > sdinfo->satadrv_atapi_cdb_len) {
6916 		sata_log(NULL, CE_WARN,
6917 		    "sata: invalid ATAPI cdb length %d",
6918 		    scsipkt->pkt_cdblen);
6919 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6920 		return (TRAN_BADPKT);
6921 	}
6922 
6923 	SATAATAPITRACE(spx, cdblen);
6924 
6925 	/*
6926 	 * For non-read/write commands we need to
6927 	 * map buffer
6928 	 */
6929 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6930 	case SCMD_READ:
6931 	case SCMD_READ_G1:
6932 	case SCMD_READ_G5:
6933 	case SCMD_READ_G4:
6934 	case SCMD_WRITE:
6935 	case SCMD_WRITE_G1:
6936 	case SCMD_WRITE_G5:
6937 	case SCMD_WRITE_G4:
6938 		break;
6939 	default:
6940 		if (bp != NULL) {
6941 			if (bp->b_flags & (B_PHYS | B_PAGEIO))
6942 				bp_mapin(bp);
6943 		}
6944 		break;
6945 	}
6946 	/*
6947 	 * scmd->satacmd_flags.sata_data_direction default -
6948 	 * SATA_DIR_NODATA_XFER - is set by
6949 	 * sata_txlt_generic_pkt_info().
6950 	 */
6951 	if (scmd->satacmd_bp) {
6952 		if (scmd->satacmd_bp->b_flags & B_READ) {
6953 			scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
6954 		} else {
6955 			scmd->satacmd_flags.sata_data_direction =
6956 			    SATA_DIR_WRITE;
6957 		}
6958 	}
6959 
6960 	/*
6961 	 * Set up ATAPI packet command.
6962 	 */
6963 
6964 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
6965 
6966 	/* Copy cdb into sata_cmd */
6967 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
6968 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
6969 	bcopy(cdbp, scmd->satacmd_acdb, cdblen);
6970 
6971 	/* See note in the command header */
6972 	if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) {
6973 		if (scmd->satacmd_acdb[3] != 0)
6974 			scmd->satacmd_acdb[4] = 255;
6975 	}
6976 
6977 #ifdef SATA_DEBUG
6978 	if (sata_debug_flags & SATA_DBG_ATAPI) {
6979 		uint8_t *p = scmd->satacmd_acdb;
6980 		char buf[3 * SATA_ATAPI_MAX_CDB_LEN];
6981 
6982 		(void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN,
6983 		    "%02x %02x %02x %02x %02x %02x %02x %02x "
6984 		    "%2x %02x %02x %02x %02x %02x %02x %02x",
6985 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
6986 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
6987 		buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0';
6988 		cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf);
6989 	}
6990 #endif
6991 
6992 	/*
6993 	 * Preset request sense data to NO SENSE.
6994 	 * If there is no way to get error information via Request Sense,
6995 	 * the packet request sense data would not have to be modified by HBA,
6996 	 * but it could be returned as is.
6997 	 */
6998 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
6999 	sata_fixed_sense_data_preset(
7000 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7001 
7002 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
7003 		/* Need callback function */
7004 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion;
7005 		synch = FALSE;
7006 	} else
7007 		synch = TRUE;
7008 
7009 	/* Transfer command to HBA */
7010 	if (sata_hba_start(spx, &rval) != 0) {
7011 		/* Pkt not accepted for execution */
7012 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
7013 		return (rval);
7014 	}
7015 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
7016 	/*
7017 	 * If execution is non-synchronous,
7018 	 * a callback function will handle potential errors, translate
7019 	 * the response and will do a callback to a target driver.
7020 	 * If it was synchronous, use the same framework callback to check
7021 	 * an execution status.
7022 	 */
7023 	if (synch) {
7024 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7025 		    "synchronous execution status %x\n",
7026 		    spx->txlt_sata_pkt->satapkt_reason);
7027 		sata_txlt_atapi_completion(spx->txlt_sata_pkt);
7028 	}
7029 	return (TRAN_ACCEPT);
7030 }
7031 
7032 
7033 /*
7034  * ATAPI Packet command completion.
7035  *
7036  * Failure of the command passed via Packet command are considered device
7037  * error. SATA HBA driver would have to retrieve error data (via Request
7038  * Sense command delivered via error retrieval sata packet) and copy it
7039  * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data.
7040  */
7041 static void
7042 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt)
7043 {
7044 	sata_pkt_txlate_t *spx =
7045 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7046 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7047 	struct scsi_extended_sense *sense;
7048 	struct buf *bp;
7049 	int rval;
7050 
7051 #ifdef SATA_DEBUG
7052 	uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense;
7053 #endif
7054 
7055 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7056 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7057 
7058 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7059 		/* Normal completion */
7060 		if (sata_pkt->satapkt_cmd.satacmd_bp != NULL)
7061 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
7062 		scsipkt->pkt_reason = CMD_CMPLT;
7063 		*scsipkt->pkt_scbp = STATUS_GOOD;
7064 		if (spx->txlt_tmp_buf != NULL) {
7065 			/* Temporary buffer was used */
7066 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7067 			if (bp->b_flags & B_READ) {
7068 				rval = ddi_dma_sync(
7069 				    spx->txlt_buf_dma_handle, 0, 0,
7070 				    DDI_DMA_SYNC_FORCPU);
7071 				ASSERT(rval == DDI_SUCCESS);
7072 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7073 				    bp->b_bcount);
7074 			}
7075 		}
7076 	} else {
7077 		/*
7078 		 * Something went wrong - analyze return
7079 		 */
7080 		*scsipkt->pkt_scbp = STATUS_CHECK;
7081 		sense = sata_arq_sense(spx);
7082 
7083 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7084 			scsipkt->pkt_reason = CMD_INCOMPLETE;
7085 			/*
7086 			 * We may not have ARQ data if there was a double
7087 			 * error. But sense data in sata packet was pre-set
7088 			 * with NO SENSE so it is valid even if HBA could
7089 			 * not retrieve a real sense data.
7090 			 * Just copy this sense data into scsi pkt sense area.
7091 			 */
7092 			bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense,
7093 			    SATA_ATAPI_MIN_RQSENSE_LEN);
7094 #ifdef SATA_DEBUG
7095 			if (sata_debug_flags & SATA_DBG_SCSI_IF) {
7096 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7097 				    "sata_txlt_atapi_completion: %02x\n"
7098 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
7099 				    "          %02x %02x %02x %02x %02x %02x "
7100 				    "          %02x %02x %02x %02x %02x %02x\n",
7101 				    scsipkt->pkt_reason,
7102 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7103 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7104 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7105 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7106 				    rqsp[16], rqsp[17]);
7107 			}
7108 #endif
7109 		} else {
7110 			switch (sata_pkt->satapkt_reason) {
7111 			case SATA_PKT_PORT_ERROR:
7112 				/*
7113 				 * We have no device data.
7114 				 */
7115 				scsipkt->pkt_reason = CMD_INCOMPLETE;
7116 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
7117 				    STATE_GOT_TARGET | STATE_SENT_CMD |
7118 				    STATE_GOT_STATUS);
7119 				sense->es_key = KEY_HARDWARE_ERROR;
7120 				break;
7121 
7122 			case SATA_PKT_TIMEOUT:
7123 				scsipkt->pkt_reason = CMD_TIMEOUT;
7124 				scsipkt->pkt_statistics |=
7125 				    STAT_TIMEOUT | STAT_DEV_RESET;
7126 				/*
7127 				 * Need to check if HARDWARE_ERROR/
7128 				 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more
7129 				 * appropriate.
7130 				 */
7131 				break;
7132 
7133 			case SATA_PKT_ABORTED:
7134 				scsipkt->pkt_reason = CMD_ABORTED;
7135 				scsipkt->pkt_statistics |= STAT_ABORTED;
7136 				/* Should we set key COMMAND_ABPRTED? */
7137 				break;
7138 
7139 			case SATA_PKT_RESET:
7140 				scsipkt->pkt_reason = CMD_RESET;
7141 				scsipkt->pkt_statistics |= STAT_DEV_RESET;
7142 				/*
7143 				 * May be we should set Unit Attention /
7144 				 * Reset. Perhaps the same should be
7145 				 * returned for disks....
7146 				 */
7147 				sense->es_key = KEY_UNIT_ATTENTION;
7148 				sense->es_add_code = SD_SCSI_ASC_RESET;
7149 				break;
7150 
7151 			default:
7152 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7153 				    "sata_txlt_atapi_completion: "
7154 				    "invalid packet completion reason"));
7155 				scsipkt->pkt_reason = CMD_TRAN_ERR;
7156 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
7157 				    STATE_GOT_TARGET | STATE_SENT_CMD |
7158 				    STATE_GOT_STATUS);
7159 				break;
7160 			}
7161 		}
7162 	}
7163 
7164 	SATAATAPITRACE(spx, 0);
7165 
7166 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7167 	    scsipkt->pkt_comp != NULL) {
7168 		/* scsi callback required */
7169 		(*scsipkt->pkt_comp)(scsipkt);
7170 	}
7171 }
7172 
7173 /*
7174  * Set up error retrieval sata command for ATAPI Packet Command error data
7175  * recovery.
7176  *
7177  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
7178  * returns SATA_FAILURE otherwise.
7179  */
7180 
7181 static int
7182 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
7183 {
7184 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
7185 	sata_cmd_t *scmd;
7186 	struct buf *bp;
7187 
7188 	/*
7189 	 * Allocate dma-able buffer error data.
7190 	 * Buffer allocation will take care of buffer alignment and other DMA
7191 	 * attributes.
7192 	 */
7193 	bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN);
7194 	if (bp == NULL) {
7195 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
7196 		    "sata_get_err_retrieval_pkt: "
7197 		    "cannot allocate buffer for error data", NULL);
7198 		return (SATA_FAILURE);
7199 	}
7200 	bp_mapin(bp); /* make data buffer accessible */
7201 
7202 	/* Operation modes are up to the caller */
7203 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7204 
7205 	/* Synchronous mode, no callback - may be changed by the caller */
7206 	spkt->satapkt_comp = NULL;
7207 	spkt->satapkt_time = sata_default_pkt_time;
7208 
7209 	scmd = &spkt->satapkt_cmd;
7210 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7211 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7212 
7213 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7214 
7215 	/*
7216 	 * Set-up acdb. Request Sense CDB (packet command content) is
7217 	 * not in DMA-able buffer. Its handling is HBA-specific (how
7218 	 * it is transfered into packet FIS).
7219 	 */
7220 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7221 	bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN);
7222 	/* Following zeroing of pad bytes may not be necessary */
7223 	bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN],
7224 	    sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN);
7225 
7226 	/*
7227 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
7228 	 * before accessing it. Handle is in usual place in translate struct.
7229 	 */
7230 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
7231 
7232 	/*
7233 	 * Preset request sense data to NO SENSE.
7234 	 * Here it is redundant, only for a symetry with scsi-originated
7235 	 * packets. It should not be used for anything but debugging.
7236 	 */
7237 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
7238 	sata_fixed_sense_data_preset(
7239 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7240 
7241 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7242 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7243 
7244 	return (SATA_SUCCESS);
7245 }
7246 
7247 /*
7248  * Set-up ATAPI packet command.
7249  * Data transfer direction has to be set-up in sata_cmd structure prior to
7250  * calling this function.
7251  *
7252  * Returns void
7253  */
7254 
7255 static void
7256 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo)
7257 {
7258 	scmd->satacmd_addr_type = 0;		/* N/A */
7259 	scmd->satacmd_sec_count_lsb = 0;	/* no tag */
7260 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
7261 	scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ;
7262 	scmd->satacmd_lba_high_lsb =
7263 	    (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8);
7264 	scmd->satacmd_cmd_reg = SATAC_PACKET;	/* Command */
7265 
7266 	/*
7267 	 * We want all data to be transfered via DMA.
7268 	 * But specify it only if drive supports DMA and DMA mode is
7269 	 * selected - some drives are sensitive about it.
7270 	 * Hopefully it wil work for all drives....
7271 	 */
7272 	if (sdinfo->satadrv_settings & SATA_DEV_DMA)
7273 		scmd->satacmd_features_reg = SATA_ATAPI_F_DMA;
7274 
7275 	/*
7276 	 * Features register requires special care for devices that use
7277 	 * Serial ATA bridge - they need an explicit specification of
7278 	 * the data transfer direction for Packet DMA commands.
7279 	 * Setting this bit is harmless if DMA is not used.
7280 	 *
7281 	 * Many drives do not implement word 80, specifying what ATA/ATAPI
7282 	 * spec they follow.
7283 	 * We are arbitrarily following the latest SerialATA 2.6 spec,
7284 	 * which uses ATA/ATAPI 6 specification for Identify Data, unless
7285 	 * ATA/ATAPI-7 support is explicitly indicated.
7286 	 */
7287 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
7288 	    sdinfo->satadrv_id.ai_majorversion != 0xffff &&
7289 	    (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) {
7290 		/*
7291 		 * Specification of major version is valid and version 7
7292 		 * is supported. It does automatically imply that all
7293 		 * spec features are supported. For now, we assume that
7294 		 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete.
7295 		 */
7296 		if ((sdinfo->satadrv_id.ai_dirdma &
7297 		    SATA_ATAPI_ID_DMADIR_REQ) != 0) {
7298 			if (scmd->satacmd_flags.sata_data_direction ==
7299 			    SATA_DIR_READ)
7300 			scmd->satacmd_features_reg |=
7301 			    SATA_ATAPI_F_DATA_DIR_READ;
7302 		}
7303 	}
7304 }
7305 
7306 
7307 #ifdef SATA_DEBUG
7308 
7309 /* Display 18 bytes of Inquiry data */
7310 static void
7311 sata_show_inqry_data(uint8_t *buf)
7312 {
7313 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
7314 	uint8_t *p;
7315 
7316 	cmn_err(CE_NOTE, "Inquiry data:");
7317 	cmn_err(CE_NOTE, "device type %x", inq->inq_dtype);
7318 	cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb);
7319 	cmn_err(CE_NOTE, "version %x", inq->inq_ansi);
7320 	cmn_err(CE_NOTE, "ATAPI transport version %d",
7321 	    SATA_ATAPI_TRANS_VERSION(inq));
7322 	cmn_err(CE_NOTE, "response data format %d, aenc %d",
7323 	    inq->inq_rdf, inq->inq_aenc);
7324 	cmn_err(CE_NOTE, " additional length %d", inq->inq_len);
7325 	cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs);
7326 	p = (uint8_t *)inq->inq_vid;
7327 	cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x "
7328 	    "%02x %02x %02x %02x",
7329 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
7330 	p = (uint8_t *)inq->inq_vid;
7331 	cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c",
7332 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
7333 
7334 	p = (uint8_t *)inq->inq_pid;
7335 	cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x "
7336 	    "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
7337 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7338 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7339 	p = (uint8_t *)inq->inq_pid;
7340 	cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c "
7341 	    "%c %c %c %c %c %c %c %c",
7342 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7343 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7344 
7345 	p = (uint8_t *)inq->inq_revision;
7346 	cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x",
7347 	    p[0], p[1], p[2], p[3]);
7348 	p = (uint8_t *)inq->inq_revision;
7349 	cmn_err(CE_NOTE, "revision: %c %c %c %c",
7350 	    p[0], p[1], p[2], p[3]);
7351 
7352 }
7353 
7354 
7355 static void
7356 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count)
7357 {
7358 	struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt;
7359 
7360 	if (scsi_pkt == NULL)
7361 		return;
7362 	if (count != 0) {
7363 		/* saving cdb */
7364 		bzero(sata_atapi_trace[sata_atapi_trace_index].acdb,
7365 		    SATA_ATAPI_MAX_CDB_LEN);
7366 		bcopy(scsi_pkt->pkt_cdbp,
7367 		    sata_atapi_trace[sata_atapi_trace_index].acdb, count);
7368 	} else {
7369 		bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)->
7370 		    sts_sensedata,
7371 		    sata_atapi_trace[sata_atapi_trace_index].arqs,
7372 		    SATA_ATAPI_MIN_RQSENSE_LEN);
7373 		sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason =
7374 		    scsi_pkt->pkt_reason;
7375 		sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason =
7376 		    spx->txlt_sata_pkt->satapkt_reason;
7377 
7378 		if (++sata_atapi_trace_index >= 64)
7379 			sata_atapi_trace_index = 0;
7380 	}
7381 }
7382 
7383 #endif
7384 
7385 /*
7386  * Fetch inquiry data from ATAPI device
7387  * Returns SATA_SUCCESS if operation was successfull, SATA_FAILURE otherwise.
7388  *
7389  * Note:
7390  * inqb pointer does not point to a DMA-able buffer. It is a local buffer
7391  * where the caller expects to see the inquiry data.
7392  *
7393  */
7394 
7395 static int
7396 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba,
7397     sata_address_t *saddr, struct scsi_inquiry *inq)
7398 {
7399 	sata_pkt_txlate_t *spx;
7400 	sata_pkt_t *spkt;
7401 	struct buf *bp;
7402 	sata_drive_info_t *sdinfo;
7403 	sata_cmd_t *scmd;
7404 	int rval;
7405 	uint8_t *rqsp;
7406 #ifdef SATA_DEBUG
7407 	char msg_buf[MAXPATHLEN];
7408 #endif
7409 
7410 	ASSERT(sata_hba != NULL);
7411 
7412 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
7413 	spx->txlt_sata_hba_inst = sata_hba;
7414 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
7415 	spkt = sata_pkt_alloc(spx, NULL);
7416 	if (spkt == NULL) {
7417 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7418 		return (SATA_FAILURE);
7419 	}
7420 	/* address is needed now */
7421 	spkt->satapkt_device.satadev_addr = *saddr;
7422 
7423 	/* scsi_inquiry size buffer */
7424 	bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry));
7425 	if (bp == NULL) {
7426 		sata_pkt_free(spx);
7427 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7428 		SATA_LOG_D((sata_hba, CE_WARN,
7429 		    "sata_get_atapi_inquiry_data: "
7430 		    "cannot allocate data buffer"));
7431 		return (SATA_FAILURE);
7432 	}
7433 	bp_mapin(bp); /* make data buffer accessible */
7434 
7435 	scmd = &spkt->satapkt_cmd;
7436 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7437 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7438 
7439 	/* Use synchronous mode */
7440 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7441 	spkt->satapkt_comp = NULL;
7442 	spkt->satapkt_time = sata_default_pkt_time;
7443 
7444 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
7445 
7446 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7447 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7448 
7449 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
7450 	sdinfo = sata_get_device_info(sata_hba,
7451 	    &spx->txlt_sata_pkt->satapkt_device);
7452 	if (sdinfo == NULL) {
7453 		/* we have to be carefull about the disapearing device */
7454 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7455 		rval = SATA_FAILURE;
7456 		goto cleanup;
7457 	}
7458 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7459 
7460 	/*
7461 	 * Set-up acdb. This works for atapi transport version 2 and later.
7462 	 */
7463 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7464 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7465 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
7466 	scmd->satacmd_acdb[1] = 0x00;
7467 	scmd->satacmd_acdb[2] = 0x00;
7468 	scmd->satacmd_acdb[3] = 0x00;
7469 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
7470 	scmd->satacmd_acdb[5] = 0x00;
7471 
7472 	sata_fixed_sense_data_preset(
7473 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7474 
7475 	/* Transfer command to HBA */
7476 	if (sata_hba_start(spx, &rval) != 0) {
7477 		/* Pkt not accepted for execution */
7478 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
7479 		    "sata_get_atapi_inquiry_data: "
7480 		    "Packet not accepted for execution - ret: %02x", rval);
7481 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7482 		rval = SATA_FAILURE;
7483 		goto cleanup;
7484 	}
7485 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7486 
7487 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
7488 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
7489 		    "sata_get_atapi_inquiry_data: "
7490 		    "Packet completed successfully - ret: %02x", rval);
7491 		if (spx->txlt_buf_dma_handle != NULL) {
7492 			/*
7493 			 * Sync buffer. Handle is in usual place in translate
7494 			 * struct.
7495 			 */
7496 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
7497 			    DDI_DMA_SYNC_FORCPU);
7498 			ASSERT(rval == DDI_SUCCESS);
7499 		}
7500 		/*
7501 		 * Normal completion - copy data into caller's buffer
7502 		 */
7503 		bcopy(bp->b_un.b_addr, (uint8_t *)inq,
7504 		    sizeof (struct scsi_inquiry));
7505 #ifdef SATA_DEBUG
7506 		if (sata_debug_flags & SATA_DBG_ATAPI) {
7507 			sata_show_inqry_data((uint8_t *)inq);
7508 		}
7509 #endif
7510 		rval = SATA_SUCCESS;
7511 	} else {
7512 		/*
7513 		 * Something went wrong - analyze return - check rqsense data
7514 		 */
7515 		rval = SATA_FAILURE;
7516 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7517 			/*
7518 			 * ARQ data hopefull show something other than NO SENSE
7519 			 */
7520 			rqsp = scmd->satacmd_rqsense;
7521 #ifdef SATA_DEBUG
7522 			if (sata_debug_flags & SATA_DBG_ATAPI) {
7523 				msg_buf[0] = '\0';
7524 				(void) snprintf(msg_buf, MAXPATHLEN,
7525 				    "ATAPI packet completion reason: %02x\n"
7526 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x\n"
7527 				    "          %02x %02x %02x %02x %02x %02x\n"
7528 				    "          %02x %02x %02x %02x %02x %02x",
7529 				    spkt->satapkt_reason,
7530 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7531 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7532 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7533 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7534 				    rqsp[16], rqsp[17]);
7535 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7536 				    "%s", msg_buf);
7537 			}
7538 #endif
7539 		} else {
7540 			switch (spkt->satapkt_reason) {
7541 			case SATA_PKT_PORT_ERROR:
7542 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7543 				    "sata_get_atapi_inquiry_data: "
7544 				    "packet reason: port error", NULL);
7545 				break;
7546 
7547 			case SATA_PKT_TIMEOUT:
7548 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7549 				    "sata_get_atapi_inquiry_data: "
7550 				    "packet reason: timeout", NULL);
7551 				break;
7552 
7553 			case SATA_PKT_ABORTED:
7554 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7555 				    "sata_get_atapi_inquiry_data: "
7556 				    "packet reason: aborted", NULL);
7557 				break;
7558 
7559 			case SATA_PKT_RESET:
7560 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7561 				    "sata_get_atapi_inquiry_data: "
7562 				    "packet reason: reset\n", NULL);
7563 				break;
7564 			default:
7565 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7566 				    "sata_get_atapi_inquiry_data: "
7567 				    "invalid packet reason: %02x\n",
7568 				    spkt->satapkt_reason);
7569 				break;
7570 			}
7571 		}
7572 	}
7573 cleanup:
7574 	sata_free_local_buffer(spx);
7575 	sata_pkt_free(spx);
7576 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
7577 	return (rval);
7578 }
7579 
7580 
7581 
7582 
7583 
7584 #if 0
7585 #ifdef SATA_DEBUG
7586 
7587 /*
7588  * Test ATAPI packet command.
7589  * Single threaded test: send packet command in synch mode, process completion
7590  *
7591  */
7592 static void
7593 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport)
7594 {
7595 	sata_pkt_txlate_t *spx;
7596 	sata_pkt_t *spkt;
7597 	struct buf *bp;
7598 	sata_device_t sata_device;
7599 	sata_drive_info_t *sdinfo;
7600 	sata_cmd_t *scmd;
7601 	int rval;
7602 	uint8_t *rqsp;
7603 
7604 	ASSERT(sata_hba_inst != NULL);
7605 	sata_device.satadev_addr.cport = cport;
7606 	sata_device.satadev_addr.pmport = 0;
7607 	sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
7608 	sata_device.satadev_rev = SATA_DEVICE_REV;
7609 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7610 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
7611 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7612 	if (sdinfo == NULL) {
7613 		sata_log(sata_hba_inst, CE_WARN,
7614 		    "sata_test_atapi_packet_command: "
7615 		    "no device info for cport %d",
7616 		    sata_device.satadev_addr.cport);
7617 		return;
7618 	}
7619 
7620 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
7621 	spx->txlt_sata_hba_inst = sata_hba_inst;
7622 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
7623 	spkt = sata_pkt_alloc(spx, NULL);
7624 	if (spkt == NULL) {
7625 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7626 		return;
7627 	}
7628 	/* address is needed now */
7629 	spkt->satapkt_device.satadev_addr = sata_device.satadev_addr;
7630 
7631 	/* 1024k buffer */
7632 	bp = sata_alloc_local_buffer(spx, 1024);
7633 	if (bp == NULL) {
7634 		sata_pkt_free(spx);
7635 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7636 		sata_log(sata_hba_inst, CE_WARN,
7637 		    "sata_test_atapi_packet_command: "
7638 		    "cannot allocate data buffer");
7639 		return;
7640 	}
7641 	bp_mapin(bp); /* make data buffer accessible */
7642 
7643 	scmd = &spkt->satapkt_cmd;
7644 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7645 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7646 
7647 	/* Use synchronous mode */
7648 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7649 
7650 	/* Synchronous mode, no callback - may be changed by the caller */
7651 	spkt->satapkt_comp = NULL;
7652 	spkt->satapkt_time = sata_default_pkt_time;
7653 
7654 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
7655 
7656 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7657 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7658 
7659 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7660 
7661 	/* Set-up acdb. */
7662 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7663 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7664 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
7665 	scmd->satacmd_acdb[1] = 0x00;
7666 	scmd->satacmd_acdb[2] = 0x00;
7667 	scmd->satacmd_acdb[3] = 0x00;
7668 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
7669 	scmd->satacmd_acdb[5] = 0x00;
7670 
7671 	sata_fixed_sense_data_preset(
7672 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7673 
7674 	/* Transfer command to HBA */
7675 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7676 	if (sata_hba_start(spx, &rval) != 0) {
7677 		/* Pkt not accepted for execution */
7678 		sata_log(sata_hba_inst, CE_WARN,
7679 		    "sata_test_atapi_packet_command: "
7680 		    "Packet not accepted for execution - ret: %02x", rval);
7681 		mutex_exit(
7682 		    &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7683 		goto cleanup;
7684 	}
7685 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7686 
7687 	if (spx->txlt_buf_dma_handle != NULL) {
7688 		/*
7689 		 * Sync buffer. Handle is in usual place in translate struct.
7690 		 */
7691 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
7692 		    DDI_DMA_SYNC_FORCPU);
7693 		ASSERT(rval == DDI_SUCCESS);
7694 	}
7695 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
7696 		sata_log(sata_hba_inst, CE_WARN,
7697 		    "sata_test_atapi_packet_command: "
7698 		    "Packet completed successfully");
7699 		/*
7700 		 * Normal completion - show inquiry data
7701 		 */
7702 		sata_show_inqry_data((uint8_t *)bp->b_un.b_addr);
7703 	} else {
7704 		/*
7705 		 * Something went wrong - analyze return - check rqsense data
7706 		 */
7707 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7708 			/*
7709 			 * ARQ data hopefull show something other than NO SENSE
7710 			 */
7711 			rqsp = scmd->satacmd_rqsense;
7712 			sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7713 			    "ATAPI packet completion reason: %02x\n"
7714 			    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
7715 			    "          %02x %02x %02x %02x %02x %02x "
7716 			    "          %02x %02x %02x %02x %02x %02x\n",
7717 			    spkt->satapkt_reason,
7718 			    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7719 			    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7720 			    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7721 			    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7722 			    rqsp[16], rqsp[17]);
7723 		} else {
7724 			switch (spkt->satapkt_reason) {
7725 			case SATA_PKT_PORT_ERROR:
7726 				sata_log(sata_hba_inst, CE_WARN,
7727 				    "sata_test_atapi_packet_command: "
7728 				    "packet reason: port error\n");
7729 				break;
7730 
7731 			case SATA_PKT_TIMEOUT:
7732 				sata_log(sata_hba_inst, CE_WARN,
7733 				    "sata_test_atapi_packet_command: "
7734 				    "packet reason: timeout\n");
7735 				break;
7736 
7737 			case SATA_PKT_ABORTED:
7738 				sata_log(sata_hba_inst, CE_WARN,
7739 				    "sata_test_atapi_packet_command: "
7740 				    "packet reason: aborted\n");
7741 				break;
7742 
7743 			case SATA_PKT_RESET:
7744 				sata_log(sata_hba_inst, CE_WARN,
7745 				    "sata_test_atapi_packet_command: "
7746 				    "packet reason: reset\n");
7747 				break;
7748 			default:
7749 				sata_log(sata_hba_inst, CE_WARN,
7750 				    "sata_test_atapi_packet_command: "
7751 				    "invalid packet reason: %02x\n",
7752 				    spkt->satapkt_reason);
7753 				break;
7754 			}
7755 		}
7756 	}
7757 cleanup:
7758 	sata_free_local_buffer(spx);
7759 	sata_pkt_free(spx);
7760 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
7761 }
7762 
7763 #endif /* SATA_DEBUG */
7764 #endif /* 1 */
7765 
7766 
7767 /* ************************** LOCAL HELPER FUNCTIONS *********************** */
7768 
7769 /*
7770  * Validate sata_tran info
7771  * SATA_FAILURE returns if structure is inconsistent or structure revision
7772  * does not match one used by the framework.
7773  *
7774  * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains
7775  * required function pointers.
7776  * Returns SATA_FAILURE otherwise.
7777  */
7778 static int
7779 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran)
7780 {
7781 	/*
7782 	 * SATA_TRAN_HBA_REV is the current (highest) revision number
7783 	 * of the SATA interface.
7784 	 */
7785 	if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) {
7786 		sata_log(NULL, CE_WARN,
7787 		    "sata: invalid sata_hba_tran version %d for driver %s",
7788 		    sata_tran->sata_tran_hba_rev, ddi_driver_name(dip));
7789 		return (SATA_FAILURE);
7790 	}
7791 
7792 	if (dip != sata_tran->sata_tran_hba_dip) {
7793 		SATA_LOG_D((NULL, CE_WARN,
7794 		    "sata: inconsistent sata_tran_hba_dip "
7795 		    "%p / %p", sata_tran->sata_tran_hba_dip, dip));
7796 		return (SATA_FAILURE);
7797 	}
7798 
7799 	if (sata_tran->sata_tran_probe_port == NULL ||
7800 	    sata_tran->sata_tran_start == NULL ||
7801 	    sata_tran->sata_tran_abort == NULL ||
7802 	    sata_tran->sata_tran_reset_dport == NULL ||
7803 	    sata_tran->sata_tran_hotplug_ops == NULL ||
7804 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL ||
7805 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate ==
7806 	    NULL) {
7807 		SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing "
7808 		    "required functions"));
7809 	}
7810 	return (SATA_SUCCESS);
7811 }
7812 
7813 /*
7814  * Remove HBA instance from sata_hba_list.
7815  */
7816 static void
7817 sata_remove_hba_instance(dev_info_t *dip)
7818 {
7819 	sata_hba_inst_t	*sata_hba_inst;
7820 
7821 	mutex_enter(&sata_mutex);
7822 	for (sata_hba_inst = sata_hba_list;
7823 	    sata_hba_inst != (struct sata_hba_inst *)NULL;
7824 	    sata_hba_inst = sata_hba_inst->satahba_next) {
7825 		if (sata_hba_inst->satahba_dip == dip)
7826 			break;
7827 	}
7828 
7829 	if (sata_hba_inst == (struct sata_hba_inst *)NULL) {
7830 #ifdef SATA_DEBUG
7831 		cmn_err(CE_WARN, "sata_remove_hba_instance: "
7832 		    "unknown HBA instance\n");
7833 #endif
7834 		ASSERT(FALSE);
7835 	}
7836 	if (sata_hba_inst == sata_hba_list) {
7837 		sata_hba_list = sata_hba_inst->satahba_next;
7838 		if (sata_hba_list) {
7839 			sata_hba_list->satahba_prev =
7840 			    (struct sata_hba_inst *)NULL;
7841 		}
7842 		if (sata_hba_inst == sata_hba_list_tail) {
7843 			sata_hba_list_tail = NULL;
7844 		}
7845 	} else if (sata_hba_inst == sata_hba_list_tail) {
7846 		sata_hba_list_tail = sata_hba_inst->satahba_prev;
7847 		if (sata_hba_list_tail) {
7848 			sata_hba_list_tail->satahba_next =
7849 			    (struct sata_hba_inst *)NULL;
7850 		}
7851 	} else {
7852 		sata_hba_inst->satahba_prev->satahba_next =
7853 		    sata_hba_inst->satahba_next;
7854 		sata_hba_inst->satahba_next->satahba_prev =
7855 		    sata_hba_inst->satahba_prev;
7856 	}
7857 	mutex_exit(&sata_mutex);
7858 }
7859 
7860 
7861 
7862 
7863 
7864 /*
7865  * Probe all SATA ports of the specified HBA instance.
7866  * The assumption is that there are no target and attachment point minor nodes
7867  * created by the boot subsystems, so we do not need to prune device tree.
7868  *
7869  * This function is called only from sata_hba_attach(). It does not have to
7870  * be protected by controller mutex, because the hba_attached flag is not set
7871  * yet and no one would be touching this HBA instance other than this thread.
7872  * Determines if port is active and what type of the device is attached
7873  * (if any). Allocates necessary structures for each port.
7874  *
7875  * An AP (Attachement Point) node is created for each SATA device port even
7876  * when there is no device attached.
7877  */
7878 
7879 static 	void
7880 sata_probe_ports(sata_hba_inst_t *sata_hba_inst)
7881 {
7882 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
7883 	int			ncport, npmport;
7884 	sata_cport_info_t 	*cportinfo;
7885 	sata_drive_info_t	*drive;
7886 	sata_pmult_info_t	*pminfo;
7887 	sata_pmport_info_t 	*pmportinfo;
7888 	sata_device_t		sata_device;
7889 	int			rval;
7890 	dev_t			minor_number;
7891 	char			name[16];
7892 	clock_t			start_time, cur_time;
7893 
7894 	/*
7895 	 * Probe controller ports first, to find port status and
7896 	 * any port multiplier attached.
7897 	 */
7898 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
7899 		/* allocate cport structure */
7900 		cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP);
7901 		ASSERT(cportinfo != NULL);
7902 		mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL);
7903 
7904 		mutex_enter(&cportinfo->cport_mutex);
7905 
7906 		cportinfo->cport_addr.cport = ncport;
7907 		cportinfo->cport_addr.pmport = 0;
7908 		cportinfo->cport_addr.qual = SATA_ADDR_CPORT;
7909 		cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
7910 		cportinfo->cport_state |= SATA_STATE_PROBING;
7911 		SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo;
7912 
7913 		/*
7914 		 * Regardless if a port is usable or not, create
7915 		 * an attachment point
7916 		 */
7917 		mutex_exit(&cportinfo->cport_mutex);
7918 		minor_number =
7919 		    SATA_MAKE_AP_MINOR(ddi_get_instance(dip), ncport, 0, 0);
7920 		(void) sprintf(name, "%d", ncport);
7921 		if (ddi_create_minor_node(dip, name, S_IFCHR,
7922 		    minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) !=
7923 		    DDI_SUCCESS) {
7924 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
7925 			    "cannot create SATA attachment point for port %d",
7926 			    ncport);
7927 		}
7928 
7929 		/* Probe port */
7930 		start_time = ddi_get_lbolt();
7931 	reprobe_cport:
7932 		sata_device.satadev_addr.cport = ncport;
7933 		sata_device.satadev_addr.pmport = 0;
7934 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
7935 		sata_device.satadev_rev = SATA_DEVICE_REV;
7936 
7937 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
7938 		    (dip, &sata_device);
7939 
7940 		mutex_enter(&cportinfo->cport_mutex);
7941 		sata_update_port_scr(&cportinfo->cport_scr, &sata_device);
7942 		if (rval != SATA_SUCCESS) {
7943 			/* Something went wrong? Fail the port */
7944 			cportinfo->cport_state = SATA_PSTATE_FAILED;
7945 			mutex_exit(&cportinfo->cport_mutex);
7946 			continue;
7947 		}
7948 		cportinfo->cport_state &= ~SATA_STATE_PROBING;
7949 		cportinfo->cport_state |= SATA_STATE_PROBED;
7950 		cportinfo->cport_dev_type = sata_device.satadev_type;
7951 
7952 		cportinfo->cport_state |= SATA_STATE_READY;
7953 		if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) {
7954 			mutex_exit(&cportinfo->cport_mutex);
7955 			continue;
7956 		}
7957 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
7958 			/*
7959 			 * There is some device attached.
7960 			 * Allocate device info structure
7961 			 */
7962 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) {
7963 				mutex_exit(&cportinfo->cport_mutex);
7964 				SATA_CPORTINFO_DRV_INFO(cportinfo) =
7965 				    kmem_zalloc(sizeof (sata_drive_info_t),
7966 				    KM_SLEEP);
7967 				mutex_enter(&cportinfo->cport_mutex);
7968 			}
7969 			drive = SATA_CPORTINFO_DRV_INFO(cportinfo);
7970 			drive->satadrv_addr = cportinfo->cport_addr;
7971 			drive->satadrv_addr.qual = SATA_ADDR_DCPORT;
7972 			drive->satadrv_type = cportinfo->cport_dev_type;
7973 			drive->satadrv_state = SATA_STATE_UNKNOWN;
7974 
7975 			mutex_exit(&cportinfo->cport_mutex);
7976 			if (sata_add_device(dip, sata_hba_inst, ncport, 0) !=
7977 			    SATA_SUCCESS) {
7978 				/*
7979 				 * Plugged device was not correctly identified.
7980 				 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT
7981 				 */
7982 				cur_time = ddi_get_lbolt();
7983 				if ((cur_time - start_time) <
7984 				    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
7985 					/* sleep for a while */
7986 					delay(drv_usectohz(
7987 					    SATA_DEV_RETRY_DLY));
7988 					goto reprobe_cport;
7989 				}
7990 			}
7991 		} else {
7992 			mutex_exit(&cportinfo->cport_mutex);
7993 			ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT);
7994 			pminfo = kmem_zalloc(sizeof (sata_pmult_info_t),
7995 			    KM_SLEEP);
7996 			mutex_enter(&cportinfo->cport_mutex);
7997 			ASSERT(pminfo != NULL);
7998 			SATA_CPORTINFO_PMULT_INFO(cportinfo) = pminfo;
7999 			pminfo->pmult_addr.cport = cportinfo->cport_addr.cport;
8000 			pminfo->pmult_addr.pmport = SATA_PMULT_HOSTPORT;
8001 			pminfo->pmult_addr.qual = SATA_ADDR_PMPORT;
8002 			pminfo->pmult_num_dev_ports =
8003 			    sata_device.satadev_add_info;
8004 			mutex_init(&pminfo->pmult_mutex, NULL, MUTEX_DRIVER,
8005 			    NULL);
8006 			pminfo->pmult_state = SATA_STATE_PROBING;
8007 			mutex_exit(&cportinfo->cport_mutex);
8008 
8009 			/* Probe Port Multiplier ports */
8010 			for (npmport = 0;
8011 			    npmport < pminfo->pmult_num_dev_ports;
8012 			    npmport++) {
8013 				pmportinfo = kmem_zalloc(
8014 				    sizeof (sata_pmport_info_t), KM_SLEEP);
8015 				mutex_enter(&cportinfo->cport_mutex);
8016 				ASSERT(pmportinfo != NULL);
8017 				pmportinfo->pmport_addr.cport = ncport;
8018 				pmportinfo->pmport_addr.pmport = npmport;
8019 				pmportinfo->pmport_addr.qual =
8020 				    SATA_ADDR_PMPORT;
8021 				pminfo->pmult_dev_port[npmport] = pmportinfo;
8022 
8023 				mutex_init(&pmportinfo->pmport_mutex, NULL,
8024 				    MUTEX_DRIVER, NULL);
8025 
8026 				mutex_exit(&cportinfo->cport_mutex);
8027 
8028 				/* Create an attachment point */
8029 				minor_number = SATA_MAKE_AP_MINOR(
8030 				    ddi_get_instance(dip), ncport, npmport, 1);
8031 				(void) sprintf(name, "%d.%d", ncport, npmport);
8032 				if (ddi_create_minor_node(dip, name, S_IFCHR,
8033 				    minor_number, DDI_NT_SATA_ATTACHMENT_POINT,
8034 				    0) != DDI_SUCCESS) {
8035 					sata_log(sata_hba_inst, CE_WARN,
8036 					    "sata_hba_attach: "
8037 					    "cannot create SATA attachment "
8038 					    "point for port %d pmult port %d",
8039 					    ncport, npmport);
8040 				}
8041 
8042 				start_time = ddi_get_lbolt();
8043 			reprobe_pmport:
8044 				sata_device.satadev_addr.pmport = npmport;
8045 				sata_device.satadev_addr.qual =
8046 				    SATA_ADDR_PMPORT;
8047 
8048 				rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8049 				    (dip, &sata_device);
8050 				mutex_enter(&cportinfo->cport_mutex);
8051 
8052 				/* sata_update_port_info() */
8053 				sata_update_port_scr(&pmportinfo->pmport_scr,
8054 				    &sata_device);
8055 
8056 				if (rval != SATA_SUCCESS) {
8057 					pmportinfo->pmport_state =
8058 					    SATA_PSTATE_FAILED;
8059 					mutex_exit(&cportinfo->cport_mutex);
8060 					continue;
8061 				}
8062 				pmportinfo->pmport_state &=
8063 				    ~SATA_STATE_PROBING;
8064 				pmportinfo->pmport_state |= SATA_STATE_PROBED;
8065 				pmportinfo->pmport_dev_type =
8066 				    sata_device.satadev_type;
8067 
8068 				pmportinfo->pmport_state |= SATA_STATE_READY;
8069 				if (pmportinfo->pmport_dev_type ==
8070 				    SATA_DTYPE_NONE) {
8071 					mutex_exit(&cportinfo->cport_mutex);
8072 					continue;
8073 				}
8074 				/* Port multipliers cannot be chained */
8075 				ASSERT(pmportinfo->pmport_dev_type !=
8076 				    SATA_DTYPE_PMULT);
8077 				/*
8078 				 * There is something attached to Port
8079 				 * Multiplier device port
8080 				 * Allocate device info structure
8081 				 */
8082 				if (pmportinfo->pmport_sata_drive == NULL) {
8083 					mutex_exit(&cportinfo->cport_mutex);
8084 					pmportinfo->pmport_sata_drive =
8085 					    kmem_zalloc(
8086 					    sizeof (sata_drive_info_t),
8087 					    KM_SLEEP);
8088 					mutex_enter(&cportinfo->cport_mutex);
8089 				}
8090 				drive = pmportinfo->pmport_sata_drive;
8091 				drive->satadrv_addr.cport =
8092 				    pmportinfo->pmport_addr.cport;
8093 				drive->satadrv_addr.pmport = npmport;
8094 				drive->satadrv_addr.qual = SATA_ADDR_DPMPORT;
8095 				drive->satadrv_type = pmportinfo->
8096 				    pmport_dev_type;
8097 				drive->satadrv_state = SATA_STATE_UNKNOWN;
8098 
8099 				mutex_exit(&cportinfo->cport_mutex);
8100 				if (sata_add_device(dip, sata_hba_inst, ncport,
8101 				    npmport) != SATA_SUCCESS) {
8102 					/*
8103 					 * Plugged device was not correctly
8104 					 * identified. Retry, within the
8105 					 * SATA_DEV_IDENTIFY_TIMEOUT
8106 					 */
8107 					cur_time = ddi_get_lbolt();
8108 					if ((cur_time - start_time) <
8109 					    drv_usectohz(
8110 					    SATA_DEV_IDENTIFY_TIMEOUT)) {
8111 						/* sleep for a while */
8112 						delay(drv_usectohz(
8113 						    SATA_DEV_RETRY_DLY));
8114 						goto reprobe_pmport;
8115 					}
8116 				}
8117 			}
8118 			pmportinfo->pmport_state =
8119 			    SATA_STATE_PROBED | SATA_STATE_READY;
8120 		}
8121 	}
8122 }
8123 
8124 /*
8125  * Add SATA device for specified HBA instance & port (SCSI target
8126  * device nodes).
8127  * This function is called (indirectly) only from sata_hba_attach().
8128  * A target node is created when there is a supported type device attached,
8129  * but may be removed if it cannot be put online.
8130  *
8131  * This function cannot be called from an interrupt context.
8132  *
8133  * ONLY DISK TARGET NODES ARE CREATED NOW
8134  *
8135  * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when
8136  * device identification failed - adding a device could be retried.
8137  *
8138  */
8139 static 	int
8140 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst, int cport,
8141     int pmport)
8142 {
8143 	sata_cport_info_t 	*cportinfo;
8144 	sata_pmult_info_t	*pminfo;
8145 	sata_pmport_info_t	*pmportinfo;
8146 	dev_info_t		*cdip;		/* child dip */
8147 	sata_device_t		sata_device;
8148 	int			rval;
8149 
8150 
8151 
8152 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
8153 	ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE);
8154 	mutex_enter(&cportinfo->cport_mutex);
8155 	/*
8156 	 * Some device is attached to a controller port.
8157 	 * We rely on controllers distinquishing between no-device,
8158 	 * attached port multiplier and other kind of attached device.
8159 	 * We need to get Identify Device data and determine
8160 	 * positively the dev type before trying to attach
8161 	 * the target driver.
8162 	 */
8163 	sata_device.satadev_rev = SATA_DEVICE_REV;
8164 	if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
8165 		/*
8166 		 * Not port multiplier.
8167 		 */
8168 		sata_device.satadev_addr = cportinfo->cport_addr;
8169 		sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
8170 		mutex_exit(&cportinfo->cport_mutex);
8171 
8172 		rval = sata_probe_device(sata_hba_inst, &sata_device);
8173 		if (rval != SATA_SUCCESS ||
8174 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN)
8175 			return (SATA_FAILURE);
8176 
8177 		mutex_enter(&cportinfo->cport_mutex);
8178 		sata_show_drive_info(sata_hba_inst,
8179 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
8180 
8181 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
8182 			/*
8183 			 * Could not determine device type or
8184 			 * a device is not supported.
8185 			 * Degrade this device to unknown.
8186 			 */
8187 			cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8188 			mutex_exit(&cportinfo->cport_mutex);
8189 			return (SATA_SUCCESS);
8190 		}
8191 		cportinfo->cport_dev_type = sata_device.satadev_type;
8192 		cportinfo->cport_tgtnode_clean = B_TRUE;
8193 		mutex_exit(&cportinfo->cport_mutex);
8194 
8195 		/*
8196 		 * Initialize device to the desired state. Even if it
8197 		 * fails, the device will still attach but syslog
8198 		 * will show the warning.
8199 		 */
8200 		if (sata_initialize_device(sata_hba_inst,
8201 		    SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS)
8202 			/* Retry */
8203 			(void) sata_initialize_device(sata_hba_inst,
8204 			    SATA_CPORTINFO_DRV_INFO(cportinfo));
8205 
8206 		cdip = sata_create_target_node(pdip, sata_hba_inst,
8207 		    &sata_device.satadev_addr);
8208 		mutex_enter(&cportinfo->cport_mutex);
8209 		if (cdip == NULL) {
8210 			/*
8211 			 * Attaching target node failed.
8212 			 * We retain sata_drive_info structure...
8213 			 */
8214 			mutex_exit(&cportinfo->cport_mutex);
8215 			return (SATA_SUCCESS);
8216 		}
8217 		(SATA_CPORTINFO_DRV_INFO(cportinfo))->
8218 		    satadrv_state = SATA_STATE_READY;
8219 	} else {
8220 		/* This must be Port Multiplier type */
8221 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
8222 			SATA_LOG_D((sata_hba_inst, CE_WARN,
8223 			    "sata_add_device: "
8224 			    "unrecognized dev type %x",
8225 			    cportinfo->cport_dev_type));
8226 			mutex_exit(&cportinfo->cport_mutex);
8227 			return (SATA_SUCCESS);
8228 		}
8229 		pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
8230 		pmportinfo = pminfo->pmult_dev_port[pmport];
8231 		sata_device.satadev_addr = pmportinfo->pmport_addr;
8232 		sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
8233 		mutex_exit(&cportinfo->cport_mutex);
8234 
8235 		rval = sata_probe_device(sata_hba_inst, &sata_device);
8236 		if (rval != SATA_SUCCESS ||
8237 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN) {
8238 			return (SATA_FAILURE);
8239 		}
8240 		mutex_enter(&cportinfo->cport_mutex);
8241 		sata_show_drive_info(sata_hba_inst,
8242 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
8243 
8244 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
8245 			/*
8246 			 * Could not determine device type.
8247 			 * Degrade this device to unknown.
8248 			 */
8249 			pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
8250 			mutex_exit(&cportinfo->cport_mutex);
8251 			return (SATA_SUCCESS);
8252 		}
8253 		pmportinfo->pmport_dev_type = sata_device.satadev_type;
8254 		pmportinfo->pmport_tgtnode_clean = B_TRUE;
8255 		mutex_exit(&cportinfo->cport_mutex);
8256 
8257 		/*
8258 		 * Initialize device to the desired state.
8259 		 * Even if it fails, the device will still
8260 		 * attach but syslog will show the warning.
8261 		 */
8262 		if (sata_initialize_device(sata_hba_inst,
8263 		    pmportinfo->pmport_sata_drive) != SATA_SUCCESS)
8264 			/* Retry */
8265 			(void) sata_initialize_device(sata_hba_inst,
8266 			    pmportinfo->pmport_sata_drive);
8267 
8268 		cdip = sata_create_target_node(pdip, sata_hba_inst,
8269 		    &sata_device.satadev_addr);
8270 		mutex_enter(&cportinfo->cport_mutex);
8271 		if (cdip == NULL) {
8272 			/*
8273 			 * Attaching target node failed.
8274 			 * We retain sata_drive_info structure...
8275 			 */
8276 			mutex_exit(&cportinfo->cport_mutex);
8277 			return (SATA_SUCCESS);
8278 		}
8279 		pmportinfo->pmport_sata_drive->satadrv_state |=
8280 		    SATA_STATE_READY;
8281 	}
8282 	mutex_exit(&cportinfo->cport_mutex);
8283 	return (SATA_SUCCESS);
8284 }
8285 
8286 
8287 
8288 /*
8289  * Create scsi target node for attached device, create node properties and
8290  * attach the node.
8291  * The node could be removed if the device onlining fails.
8292  *
8293  * A dev_info_t pointer is returned if operation is successful, NULL is
8294  * returned otherwise.
8295  *
8296  * No port multiplier support.
8297  */
8298 
8299 static dev_info_t *
8300 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst,
8301 			sata_address_t *sata_addr)
8302 {
8303 	dev_info_t *cdip = NULL;
8304 	int rval;
8305 	char *nname = NULL;
8306 	char **compatible = NULL;
8307 	int ncompatible;
8308 	struct scsi_inquiry inq;
8309 	sata_device_t sata_device;
8310 	sata_drive_info_t *sdinfo;
8311 	int target;
8312 	int i;
8313 
8314 	sata_device.satadev_rev = SATA_DEVICE_REV;
8315 	sata_device.satadev_addr = *sata_addr;
8316 
8317 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport)));
8318 
8319 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
8320 
8321 	target = SATA_TO_SCSI_TARGET(sata_addr->cport,
8322 	    sata_addr->pmport, sata_addr->qual);
8323 
8324 	if (sdinfo == NULL) {
8325 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8326 		    sata_addr->cport)));
8327 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8328 		    "sata_create_target_node: no sdinfo for target %x",
8329 		    target));
8330 		return (NULL);
8331 	}
8332 
8333 	/*
8334 	 * create or get scsi inquiry data, expected by
8335 	 * scsi_hba_nodename_compatible_get()
8336 	 * SATA hard disks get Identify Data translated into Inguiry Data.
8337 	 * ATAPI devices respond directly to Inquiry request.
8338 	 */
8339 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
8340 		sata_identdev_to_inquiry(sata_hba_inst, sdinfo,
8341 		    (uint8_t *)&inq);
8342 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8343 		    sata_addr->cport)));
8344 	} else { /* Assume supported ATAPI device */
8345 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8346 		    sata_addr->cport)));
8347 		if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr,
8348 		    &inq) == SATA_FAILURE)
8349 			return (NULL);
8350 		/*
8351 		 * Save supported ATAPI transport version
8352 		 */
8353 		sdinfo->satadrv_atapi_trans_ver =
8354 		    SATA_ATAPI_TRANS_VERSION(&inq);
8355 	}
8356 
8357 	/* determine the node name and compatible */
8358 	scsi_hba_nodename_compatible_get(&inq, NULL,
8359 	    inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
8360 
8361 #ifdef SATA_DEBUG
8362 	if (sata_debug_flags & SATA_DBG_NODES) {
8363 		if (nname == NULL) {
8364 			cmn_err(CE_NOTE, "sata_create_target_node: "
8365 			    "cannot determine nodename for target %d\n",
8366 			    target);
8367 		} else {
8368 			cmn_err(CE_WARN, "sata_create_target_node: "
8369 			    "target %d nodename: %s\n", target, nname);
8370 		}
8371 		if (compatible == NULL) {
8372 			cmn_err(CE_WARN,
8373 			    "sata_create_target_node: no compatible name\n");
8374 		} else {
8375 			for (i = 0; i < ncompatible; i++) {
8376 				cmn_err(CE_WARN, "sata_create_target_node: "
8377 				    "compatible name: %s\n", compatible[i]);
8378 			}
8379 		}
8380 	}
8381 #endif
8382 
8383 	/* if nodename can't be determined, log error and exit */
8384 	if (nname == NULL) {
8385 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8386 		    "sata_create_target_node: cannot determine nodename "
8387 		    "for target %d\n", target));
8388 		scsi_hba_nodename_compatible_free(nname, compatible);
8389 		return (NULL);
8390 	}
8391 	/*
8392 	 * Create scsi target node
8393 	 */
8394 	ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip);
8395 	rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
8396 	    "device-type", "scsi");
8397 
8398 	if (rval != DDI_PROP_SUCCESS) {
8399 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8400 		    "updating device_type prop failed %d", rval));
8401 		goto fail;
8402 	}
8403 
8404 	/*
8405 	 * Create target node properties: target & lun
8406 	 */
8407 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target);
8408 	if (rval != DDI_PROP_SUCCESS) {
8409 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8410 		    "updating target prop failed %d", rval));
8411 		goto fail;
8412 	}
8413 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0);
8414 	if (rval != DDI_PROP_SUCCESS) {
8415 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8416 		    "updating target prop failed %d", rval));
8417 		goto fail;
8418 	}
8419 
8420 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
8421 		/*
8422 		 * Add "variant" property
8423 		 */
8424 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
8425 		    "variant", "atapi");
8426 		if (rval != DDI_PROP_SUCCESS) {
8427 			SATA_LOG_D((sata_hba_inst, CE_WARN,
8428 			    "sata_create_target_node: variant atapi "
8429 			    "property could not be created: %d", rval));
8430 			goto fail;
8431 		}
8432 	}
8433 	/* decorate the node with compatible */
8434 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible",
8435 	    compatible, ncompatible) != DDI_PROP_SUCCESS) {
8436 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8437 		    "sata_create_target_node: FAIL compatible props cdip 0x%p",
8438 		    (void *)cdip));
8439 		goto fail;
8440 	}
8441 
8442 
8443 	/*
8444 	 * Now, try to attach the driver. If probing of the device fails,
8445 	 * the target node may be removed
8446 	 */
8447 	rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH);
8448 
8449 	scsi_hba_nodename_compatible_free(nname, compatible);
8450 
8451 	if (rval == NDI_SUCCESS)
8452 		return (cdip);
8453 
8454 	/* target node was removed - are we sure? */
8455 	return (NULL);
8456 
8457 fail:
8458 	scsi_hba_nodename_compatible_free(nname, compatible);
8459 	ddi_prop_remove_all(cdip);
8460 	rval = ndi_devi_free(cdip);
8461 	if (rval != NDI_SUCCESS) {
8462 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8463 		    "node removal failed %d", rval));
8464 	}
8465 	sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: "
8466 	    "cannot create target node for SATA device at port %d",
8467 	    sata_addr->cport);
8468 	return (NULL);
8469 }
8470 
8471 
8472 
8473 /*
8474  * Re-probe sata port, check for a device and attach info
8475  * structures when necessary. Identify Device data is fetched, if possible.
8476  * Assumption: sata address is already validated.
8477  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
8478  * the presence of a device and its type.
8479  *
8480  * flag arg specifies that the function should try multiple times to identify
8481  * device type and to initialize it, or it should return immediately on failure.
8482  * SATA_DEV_IDENTIFY_RETRY - retry
8483  * SATA_DEV_IDENTIFY_NORETRY - no retry
8484  *
8485  * SATA_FAILURE is returned if one of the operations failed.
8486  *
8487  * This function cannot be called in interrupt context - it may sleep.
8488  *
8489  * NOte: Port multiplier is not supported yet, although there may be some
8490  * pieces of code referencing to it.
8491  */
8492 static int
8493 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
8494     int flag)
8495 {
8496 	sata_cport_info_t *cportinfo;
8497 	sata_drive_info_t *sdinfo, *osdinfo;
8498 	boolean_t init_device = B_FALSE;
8499 	int prev_device_type = SATA_DTYPE_NONE;
8500 	int prev_device_settings = 0;
8501 	int prev_device_state = 0;
8502 	clock_t start_time;
8503 	int retry = B_FALSE;
8504 	int rval;
8505 
8506 	/* We only care about host sata cport for now */
8507 	cportinfo = SATA_CPORT_INFO(sata_hba_inst,
8508 	    sata_device->satadev_addr.cport);
8509 	osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8510 	if (osdinfo != NULL) {
8511 		/*
8512 		 * We are re-probing port with a previously attached device.
8513 		 * Save previous device type and settings.
8514 		 */
8515 		prev_device_type = cportinfo->cport_dev_type;
8516 		prev_device_settings = osdinfo->satadrv_settings;
8517 		prev_device_state = osdinfo->satadrv_state;
8518 	}
8519 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
8520 		start_time = ddi_get_lbolt();
8521 		retry = B_TRUE;
8522 	}
8523 retry_probe:
8524 
8525 	/* probe port */
8526 	mutex_enter(&cportinfo->cport_mutex);
8527 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
8528 	cportinfo->cport_state |= SATA_STATE_PROBING;
8529 	mutex_exit(&cportinfo->cport_mutex);
8530 
8531 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8532 	    (SATA_DIP(sata_hba_inst), sata_device);
8533 
8534 	mutex_enter(&cportinfo->cport_mutex);
8535 	if (rval != SATA_SUCCESS) {
8536 		cportinfo->cport_state = SATA_PSTATE_FAILED;
8537 		mutex_exit(&cportinfo->cport_mutex);
8538 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: "
8539 		    "SATA port %d probing failed",
8540 		    cportinfo->cport_addr.cport));
8541 		return (SATA_FAILURE);
8542 	}
8543 
8544 	/*
8545 	 * update sata port state and set device type
8546 	 */
8547 	sata_update_port_info(sata_hba_inst, sata_device);
8548 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
8549 
8550 	/*
8551 	 * Sanity check - Port is active? Is the link active?
8552 	 * Is there any device attached?
8553 	 */
8554 	if ((cportinfo->cport_state &
8555 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
8556 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
8557 	    SATA_PORT_DEVLINK_UP) {
8558 		/*
8559 		 * Port in non-usable state or no link active/no device.
8560 		 * Free info structure if necessary (direct attached drive
8561 		 * only, for now!
8562 		 */
8563 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8564 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8565 		/* Add here differentiation for device attached or not */
8566 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8567 		mutex_exit(&cportinfo->cport_mutex);
8568 		if (sdinfo != NULL)
8569 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8570 		return (SATA_SUCCESS);
8571 	}
8572 
8573 	cportinfo->cport_state |= SATA_STATE_READY;
8574 	cportinfo->cport_dev_type = sata_device->satadev_type;
8575 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8576 
8577 	/*
8578 	 * If we are re-probing the port, there may be
8579 	 * sata_drive_info structure attached
8580 	 * (or sata_pm_info, if PMult is supported).
8581 	 */
8582 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
8583 		/*
8584 		 * There is no device, so remove device info structure,
8585 		 * if necessary.
8586 		 * Only direct attached drive is considered now, until
8587 		 * port multiplier is supported. If the previously
8588 		 * attached device was a port multiplier, we would need
8589 		 * to take care of devices attached beyond the port
8590 		 * multiplier.
8591 		 */
8592 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8593 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8594 		if (sdinfo != NULL) {
8595 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8596 			sata_log(sata_hba_inst, CE_WARN,
8597 			    "SATA device detached "
8598 			    "from port %d", cportinfo->cport_addr.cport);
8599 		}
8600 		mutex_exit(&cportinfo->cport_mutex);
8601 		return (SATA_SUCCESS);
8602 	}
8603 
8604 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
8605 		if (sdinfo == NULL) {
8606 			/*
8607 			 * There is some device attached, but there is
8608 			 * no sata_drive_info structure - allocate one
8609 			 */
8610 			mutex_exit(&cportinfo->cport_mutex);
8611 			sdinfo = kmem_zalloc(
8612 			    sizeof (sata_drive_info_t), KM_SLEEP);
8613 			mutex_enter(&cportinfo->cport_mutex);
8614 			/*
8615 			 * Recheck, that the port state did not change when we
8616 			 * released mutex.
8617 			 */
8618 			if (cportinfo->cport_state & SATA_STATE_READY) {
8619 				SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo;
8620 				sdinfo->satadrv_addr = cportinfo->cport_addr;
8621 				sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT;
8622 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
8623 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
8624 			} else {
8625 				/*
8626 				 * Port is not in ready state, we
8627 				 * cannot attach a device.
8628 				 */
8629 				mutex_exit(&cportinfo->cport_mutex);
8630 				kmem_free(sdinfo, sizeof (sata_drive_info_t));
8631 				return (SATA_SUCCESS);
8632 			}
8633 			/*
8634 			 * Since we are adding device, presumably new one,
8635 			 * indicate that it  should be initalized,
8636 			 * as well as some internal framework states).
8637 			 */
8638 			init_device = B_TRUE;
8639 		}
8640 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8641 		sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
8642 	} else {
8643 		/*
8644 		 * The device is a port multiplier - not handled now.
8645 		 */
8646 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8647 		mutex_exit(&cportinfo->cport_mutex);
8648 		return (SATA_SUCCESS);
8649 	}
8650 	mutex_exit(&cportinfo->cport_mutex);
8651 	/*
8652 	 * Figure out what kind of device we are really
8653 	 * dealing with.
8654 	 */
8655 	rval = sata_probe_device(sata_hba_inst, sata_device);
8656 
8657 	mutex_enter(&cportinfo->cport_mutex);
8658 	if (rval == SATA_SUCCESS) {
8659 		/*
8660 		 * If we are dealing with the same type of a device as before,
8661 		 * restore its settings flags.
8662 		 */
8663 		if (osdinfo != NULL &&
8664 		    sata_device->satadev_type == prev_device_type)
8665 			sdinfo->satadrv_settings = prev_device_settings;
8666 
8667 		mutex_exit(&cportinfo->cport_mutex);
8668 		/* Set initial device features, if necessary */
8669 		if (init_device == B_TRUE) {
8670 			rval = sata_initialize_device(sata_hba_inst, sdinfo);
8671 		}
8672 		if (rval == SATA_SUCCESS)
8673 			return (rval);
8674 	} else {
8675 		/*
8676 		 * If there was some device info before we probe the device,
8677 		 * restore previous device setting, so we can retry from scratch
8678 		 * later. Providing, of course, that device has not disapear
8679 		 * during probing process.
8680 		 */
8681 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
8682 			if (osdinfo != NULL) {
8683 				cportinfo->cport_dev_type = prev_device_type;
8684 				sdinfo->satadrv_type = prev_device_type;
8685 				sdinfo->satadrv_state = prev_device_state;
8686 			}
8687 		} else {
8688 			/* device is gone */
8689 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8690 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8691 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8692 			mutex_exit(&cportinfo->cport_mutex);
8693 			return (SATA_SUCCESS);
8694 		}
8695 		mutex_exit(&cportinfo->cport_mutex);
8696 	}
8697 
8698 	if (retry) {
8699 		clock_t cur_time = ddi_get_lbolt();
8700 		/*
8701 		 * A device was not successfully identified or initialized.
8702 		 * Track retry time for device identification.
8703 		 */
8704 		if ((cur_time - start_time) <
8705 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
8706 			/* sleep for a while */
8707 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
8708 			goto retry_probe;
8709 		} else {
8710 			mutex_enter(&cportinfo->cport_mutex);
8711 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL)
8712 				SATA_CPORTINFO_DRV_INFO(cportinfo)->
8713 				    satadrv_state = SATA_DSTATE_FAILED;
8714 			mutex_exit(&cportinfo->cport_mutex);
8715 		}
8716 	}
8717 	return (SATA_SUCCESS);
8718 }
8719 
8720 /*
8721  * Initialize device
8722  * Specified device is initialized to a default state.
8723  *
8724  * Returns SATA_SUCCESS if all device features are set successfully,
8725  * SATA_FAILURE otherwise
8726  */
8727 static int
8728 sata_initialize_device(sata_hba_inst_t *sata_hba_inst,
8729     sata_drive_info_t *sdinfo)
8730 {
8731 	int rval;
8732 
8733 	sata_save_drive_settings(sdinfo);
8734 
8735 	sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
8736 
8737 	sata_init_write_cache_mode(sdinfo);
8738 
8739 	rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0);
8740 
8741 	/* Determine current data transfer mode */
8742 	if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) {
8743 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
8744 	} else if ((sdinfo->satadrv_id.ai_validinfo &
8745 	    SATA_VALIDINFO_88) != 0 &&
8746 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) {
8747 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
8748 	} else if ((sdinfo->satadrv_id.ai_dworddma &
8749 	    SATA_MDMA_SEL_MASK) != 0) {
8750 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
8751 	} else
8752 		/* DMA supported, not no DMA transfer mode is selected !? */
8753 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
8754 
8755 	return (rval);
8756 }
8757 
8758 
8759 /*
8760  * Initialize write cache mode.
8761  *
8762  * The default write cache setting for SATA HDD is provided by sata_write_cache
8763  * static variable. ATAPI CD/DVDs devices have write cache default is
8764  * determined by sata_atapicdvd_write_cache static variable.
8765  * 1 - enable
8766  * 0 - disable
8767  * any other value - current drive setting
8768  *
8769  * Although there is not reason to disable write cache on CD/DVD devices,
8770  * the default setting control is provided for the maximun flexibility.
8771  *
8772  * In the future, it may be overridden by the
8773  * disk-write-cache-enable property setting, if it is defined.
8774  * Returns SATA_SUCCESS if all device features are set successfully,
8775  * SATA_FAILURE otherwise.
8776  */
8777 static void
8778 sata_init_write_cache_mode(sata_drive_info_t *sdinfo)
8779 {
8780 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
8781 		if (sata_write_cache == 1)
8782 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
8783 		else if (sata_write_cache == 0)
8784 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
8785 		/*
8786 		 * When sata_write_cache value is not 0 or 1,
8787 		 * a current setting of the drive's write cache is used.
8788 		 */
8789 	} else { /* Assume ATAPI CD/DVD device */
8790 		if (sata_atapicdvd_write_cache == 1)
8791 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
8792 		else if (sata_atapicdvd_write_cache == 0)
8793 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
8794 		/*
8795 		 * When sata_write_cache value is not 0 or 1,
8796 		 * a current setting of the drive's write cache is used.
8797 		 */
8798 	}
8799 }
8800 
8801 
8802 /*
8803  * Validate sata address.
8804  * Specified cport, pmport and qualifier has to match
8805  * passed sata_scsi configuration info.
8806  * The presence of an attached device is not verified.
8807  *
8808  * Returns 0 when address is valid, -1 otherwise.
8809  */
8810 static int
8811 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport,
8812 	int pmport, int qual)
8813 {
8814 	if (qual == SATA_ADDR_DCPORT && pmport != 0)
8815 		goto invalid_address;
8816 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
8817 		goto invalid_address;
8818 	if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) &&
8819 	    ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) ||
8820 	    (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) ||
8821 	    (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport))))
8822 		goto invalid_address;
8823 
8824 	return (0);
8825 
8826 invalid_address:
8827 	return (-1);
8828 
8829 }
8830 
8831 /*
8832  * Validate scsi address
8833  * SCSI target address is translated into SATA cport/pmport and compared
8834  * with a controller port/device configuration. LUN has to be 0.
8835  * Returns 0 if a scsi target refers to an attached device,
8836  * returns 1 if address is valid but device is not attached,
8837  * returns -1 if bad address or device is of an unsupported type.
8838  * Upon return sata_device argument is set.
8839  */
8840 static int
8841 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst,
8842 	struct scsi_address *ap, sata_device_t *sata_device)
8843 {
8844 	int cport, pmport, qual, rval;
8845 
8846 	rval = -1;	/* Invalid address */
8847 	if (ap->a_lun != 0)
8848 		goto out;
8849 
8850 	qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
8851 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
8852 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
8853 
8854 	if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT)
8855 		goto out;
8856 
8857 	if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) ==
8858 	    0) {
8859 
8860 		sata_cport_info_t *cportinfo;
8861 		sata_pmult_info_t *pmultinfo;
8862 		sata_drive_info_t *sdinfo = NULL;
8863 
8864 		rval = 1;	/* Valid sata address */
8865 
8866 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
8867 		if (qual == SATA_ADDR_DCPORT) {
8868 			if (cportinfo == NULL ||
8869 			    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
8870 				goto out;
8871 
8872 			if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT ||
8873 			    (cportinfo->cport_dev_type &
8874 			    SATA_VALID_DEV_TYPE) == 0) {
8875 				rval = -1;
8876 				goto out;
8877 			}
8878 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8879 
8880 		} else if (qual == SATA_ADDR_DPMPORT) {
8881 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
8882 			if (pmultinfo == NULL) {
8883 				rval = -1;
8884 				goto out;
8885 			}
8886 			if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) ==
8887 			    NULL ||
8888 			    SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
8889 			    pmport) == SATA_DTYPE_NONE)
8890 				goto out;
8891 
8892 			sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport,
8893 			    pmport);
8894 		} else {
8895 			rval = -1;
8896 			goto out;
8897 		}
8898 		if ((sdinfo == NULL) ||
8899 		    (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0)
8900 			goto out;
8901 
8902 		sata_device->satadev_type = sdinfo->satadrv_type;
8903 		sata_device->satadev_addr.qual = qual;
8904 		sata_device->satadev_addr.cport = cport;
8905 		sata_device->satadev_addr.pmport = pmport;
8906 		sata_device->satadev_rev = SATA_DEVICE_REV_1;
8907 		return (0);
8908 	}
8909 out:
8910 	if (rval == 1) {
8911 		SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
8912 		    "sata_validate_scsi_address: no valid target %x lun %x",
8913 		    ap->a_target, ap->a_lun);
8914 	}
8915 	return (rval);
8916 }
8917 
8918 /*
8919  * Find dip corresponding to passed device number
8920  *
8921  * Returns NULL if invalid device number is passed or device cannot be found,
8922  * Returns dip is device is found.
8923  */
8924 static dev_info_t *
8925 sata_devt_to_devinfo(dev_t dev)
8926 {
8927 	dev_info_t *dip;
8928 #ifndef __lock_lint
8929 	struct devnames *dnp;
8930 	major_t major = getmajor(dev);
8931 	int instance = SATA_MINOR2INSTANCE(getminor(dev));
8932 
8933 	if (major >= devcnt)
8934 		return (NULL);
8935 
8936 	dnp = &devnamesp[major];
8937 	LOCK_DEV_OPS(&(dnp->dn_lock));
8938 	dip = dnp->dn_head;
8939 	while (dip && (ddi_get_instance(dip) != instance)) {
8940 		dip = ddi_get_next(dip);
8941 	}
8942 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
8943 #endif
8944 
8945 	return (dip);
8946 }
8947 
8948 
8949 /*
8950  * Probe device.
8951  * This function issues Identify Device command and initializes local
8952  * sata_drive_info structure if the device can be identified.
8953  * The device type is determined by examining Identify Device
8954  * command response.
8955  * If the sata_hba_inst has linked drive info structure for this
8956  * device address, the Identify Device data is stored into sata_drive_info
8957  * structure linked to the port info structure.
8958  *
8959  * sata_device has to refer to the valid sata port(s) for HBA described
8960  * by sata_hba_inst structure.
8961  *
8962  * Returns:
8963  *	SATA_SUCCESS if device type was successfully probed and port-linked
8964  *		drive info structure was updated;
8965  * 	SATA_FAILURE if there is no device, or device was not probed
8966  *		successully;
8967  *	SATA_RETRY if device probe can be retried later.
8968  * If a device cannot be identified, sata_device's dev_state and dev_type
8969  * fields are set to unknown.
8970  * There are no retries in this function. Any retries should be managed by
8971  * the caller.
8972  */
8973 
8974 
8975 static int
8976 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
8977 {
8978 	sata_drive_info_t *sdinfo;
8979 	sata_drive_info_t new_sdinfo;	/* local drive info struct */
8980 	int rval;
8981 
8982 	ASSERT((SATA_CPORT_STATE(sata_hba_inst,
8983 	    sata_device->satadev_addr.cport) &
8984 	    (SATA_STATE_PROBED | SATA_STATE_READY)) != 0);
8985 
8986 	sata_device->satadev_type = SATA_DTYPE_NONE;
8987 
8988 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
8989 	    sata_device->satadev_addr.cport)));
8990 
8991 	/* Get pointer to port-linked sata device info structure */
8992 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
8993 	if (sdinfo != NULL) {
8994 		sdinfo->satadrv_state &=
8995 		    ~(SATA_STATE_PROBED | SATA_STATE_READY);
8996 		sdinfo->satadrv_state |= SATA_STATE_PROBING;
8997 	} else {
8998 		/* No device to probe */
8999 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9000 		    sata_device->satadev_addr.cport)));
9001 		sata_device->satadev_type = SATA_DTYPE_NONE;
9002 		sata_device->satadev_state = SATA_STATE_UNKNOWN;
9003 		return (SATA_FAILURE);
9004 	}
9005 	/*
9006 	 * Need to issue both types of identify device command and
9007 	 * determine device type by examining retreived data/status.
9008 	 * First, ATA Identify Device.
9009 	 */
9010 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
9011 	new_sdinfo.satadrv_addr = sata_device->satadev_addr;
9012 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9013 	    sata_device->satadev_addr.cport)));
9014 	new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
9015 	rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
9016 	if (rval == SATA_RETRY) {
9017 		/* We may try to check for ATAPI device */
9018 		if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) {
9019 			/*
9020 			 * HBA supports ATAPI - try to issue Identify Packet
9021 			 * Device command.
9022 			 */
9023 			new_sdinfo.satadrv_type = SATA_DTYPE_ATAPICD;
9024 			rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
9025 		}
9026 	}
9027 	if (rval == SATA_SUCCESS) {
9028 		/*
9029 		 * Got something responding positively to ATA Identify Device
9030 		 * or to Identify Packet Device cmd.
9031 		 * Save last used device type.
9032 		 */
9033 		sata_device->satadev_type = new_sdinfo.satadrv_type;
9034 
9035 		/* save device info, if possible */
9036 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9037 		    sata_device->satadev_addr.cport)));
9038 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9039 		if (sdinfo == NULL) {
9040 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9041 			    sata_device->satadev_addr.cport)));
9042 			return (SATA_FAILURE);
9043 		}
9044 		/*
9045 		 * Copy drive info into the port-linked drive info structure.
9046 		 */
9047 		*sdinfo = new_sdinfo;
9048 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
9049 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
9050 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
9051 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
9052 			    sata_device->satadev_addr.cport) =
9053 			    sdinfo->satadrv_type;
9054 		else /* SATA_ADDR_DPMPORT */
9055 			SATA_PMPORT_DEV_TYPE(sata_hba_inst,
9056 			    sata_device->satadev_addr.cport,
9057 			    sata_device->satadev_addr.pmport) =
9058 			    sdinfo->satadrv_type;
9059 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9060 		    sata_device->satadev_addr.cport)));
9061 		return (SATA_SUCCESS);
9062 	}
9063 
9064 	/*
9065 	 * It may be SATA_RETRY or SATA_FAILURE return.
9066 	 * Looks like we cannot determine the device type at this time.
9067 	 */
9068 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9069 	    sata_device->satadev_addr.cport)));
9070 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9071 	if (sdinfo != NULL) {
9072 		sata_device->satadev_type = SATA_DTYPE_UNKNOWN;
9073 		sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9074 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
9075 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
9076 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
9077 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
9078 			    sata_device->satadev_addr.cport) =
9079 			    SATA_DTYPE_UNKNOWN;
9080 		else {
9081 			/* SATA_ADDR_DPMPORT */
9082 			if ((SATA_PMULT_INFO(sata_hba_inst,
9083 			    sata_device->satadev_addr.cport) != NULL) &&
9084 			    (SATA_PMPORT_INFO(sata_hba_inst,
9085 			    sata_device->satadev_addr.cport,
9086 			    sata_device->satadev_addr.pmport) != NULL))
9087 				SATA_PMPORT_DEV_TYPE(sata_hba_inst,
9088 				    sata_device->satadev_addr.cport,
9089 				    sata_device->satadev_addr.pmport) =
9090 				    SATA_DTYPE_UNKNOWN;
9091 		}
9092 	}
9093 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9094 	    sata_device->satadev_addr.cport)));
9095 	return (rval);
9096 }
9097 
9098 
9099 /*
9100  * Get pointer to sata_drive_info structure.
9101  *
9102  * The sata_device has to contain address (cport, pmport and qualifier) for
9103  * specified sata_scsi structure.
9104  *
9105  * Returns NULL if device address is not valid for this HBA configuration.
9106  * Otherwise, returns a pointer to sata_drive_info structure.
9107  *
9108  * This function should be called with a port mutex held.
9109  */
9110 static sata_drive_info_t *
9111 sata_get_device_info(sata_hba_inst_t *sata_hba_inst,
9112     sata_device_t *sata_device)
9113 {
9114 	uint8_t cport = sata_device->satadev_addr.cport;
9115 	uint8_t pmport = sata_device->satadev_addr.pmport;
9116 	uint8_t qual = sata_device->satadev_addr.qual;
9117 
9118 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
9119 		return (NULL);
9120 
9121 	if (!(SATA_CPORT_STATE(sata_hba_inst, cport) &
9122 	    (SATA_STATE_PROBED | SATA_STATE_READY)))
9123 		/* Port not probed yet */
9124 		return (NULL);
9125 
9126 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE)
9127 		return (NULL);
9128 
9129 	if (qual == SATA_ADDR_DCPORT) {
9130 		/* Request for a device on a controller port */
9131 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
9132 		    SATA_DTYPE_PMULT)
9133 			/* Port multiplier attached */
9134 			return (NULL);
9135 		return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport));
9136 	}
9137 	if (qual == SATA_ADDR_DPMPORT) {
9138 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
9139 		    SATA_DTYPE_PMULT)
9140 			return (NULL);
9141 
9142 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport))
9143 			return (NULL);
9144 
9145 		return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport));
9146 	}
9147 
9148 	/* we should not get here */
9149 	return (NULL);
9150 }
9151 
9152 
9153 /*
9154  * sata_identify_device.
9155  * Send Identify Device command to SATA HBA driver.
9156  * If command executes successfully, update sata_drive_info structure pointed
9157  * to by sdinfo argument, including Identify Device data.
9158  * If command fails, invalidate data in sata_drive_info.
9159  *
9160  * Cannot be called from interrupt level.
9161  *
9162  * Returns:
9163  * SATA_SUCCESS if the device was identified as a supported device,
9164  * SATA_RETRY if the device was not identified but could be retried,
9165  * SATA_FAILURE if the device was not identified and identify attempt
9166  *	should not be retried.
9167  */
9168 static int
9169 sata_identify_device(sata_hba_inst_t *sata_hba_inst,
9170     sata_drive_info_t *sdinfo)
9171 {
9172 	uint16_t cfg_word;
9173 	int rval;
9174 
9175 	/* fetch device identify data */
9176 	if ((rval = sata_fetch_device_identify_data(sata_hba_inst,
9177 	    sdinfo)) != 0)
9178 		goto fail_unknown;
9179 
9180 	cfg_word = sdinfo->satadrv_id.ai_config;
9181 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK &&
9182 	    (cfg_word & SATA_ATA_TYPE_MASK) != SATA_ATA_TYPE) {
9183 		/* Change device type to reflect Identify Device data */
9184 		if (((cfg_word & SATA_ATAPI_TYPE_MASK) ==
9185 		    SATA_ATAPI_TYPE) &&
9186 		    ((cfg_word & SATA_ATAPI_ID_DEV_TYPE) ==
9187 		    SATA_ATAPI_CDROM_DEV)) {
9188 			sdinfo->satadrv_type = SATA_DTYPE_ATAPICD;
9189 		} else {
9190 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9191 		}
9192 	} else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD &&
9193 	    (((cfg_word & SATA_ATAPI_TYPE_MASK) != SATA_ATAPI_TYPE) ||
9194 	    ((cfg_word & SATA_ATAPI_ID_DEV_TYPE) != SATA_ATAPI_CDROM_DEV))) {
9195 		/* Change device type to reflect Identify Device data ! */
9196 		if ((sdinfo->satadrv_id.ai_config & SATA_ATA_TYPE_MASK) ==
9197 		    SATA_ATA_TYPE) {
9198 			sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
9199 		} else {
9200 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9201 		}
9202 	}
9203 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9204 		if (sdinfo->satadrv_capacity == 0) {
9205 			/* Non-LBA disk. Too bad... */
9206 			sata_log(sata_hba_inst, CE_WARN,
9207 			    "SATA disk device at port %d does not support LBA",
9208 			    sdinfo->satadrv_addr.cport);
9209 			rval = SATA_FAILURE;
9210 			goto fail_unknown;
9211 		}
9212 	}
9213 #if 0
9214 	/* Left for historical reason */
9215 	/*
9216 	 * Some initial version of SATA spec indicated that at least
9217 	 * UDMA mode 4 has to be supported. It is not metioned in
9218 	 * SerialATA 2.6, so this restriction is removed.
9219 	 */
9220 	/* Check for Ultra DMA modes 6 through 0 being supported */
9221 	for (i = 6; i >= 0; --i) {
9222 		if (sdinfo->satadrv_id.ai_ultradma & (1 << i))
9223 			break;
9224 	}
9225 
9226 	/*
9227 	 * At least UDMA 4 mode has to be supported. If mode 4 or
9228 	 * higher are not supported by the device, fail this
9229 	 * device.
9230 	 */
9231 	if (i < 4) {
9232 		/* No required Ultra DMA mode supported */
9233 		sata_log(sata_hba_inst, CE_WARN,
9234 		    "SATA disk device at port %d does not support UDMA "
9235 		    "mode 4 or higher", sdinfo->satadrv_addr.cport);
9236 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9237 		    "mode 4 or higher required, %d supported", i));
9238 		rval = SATA_FAILURE;
9239 		goto fail_unknown;
9240 	}
9241 #endif
9242 
9243 	return (SATA_SUCCESS);
9244 
9245 fail_unknown:
9246 	/* Invalidate sata_drive_info ? */
9247 	sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9248 	sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
9249 	return (rval);
9250 }
9251 
9252 /*
9253  * Log/display device information
9254  */
9255 static void
9256 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst,
9257     sata_drive_info_t *sdinfo)
9258 {
9259 	int valid_version;
9260 	char msg_buf[MAXPATHLEN];
9261 	int i;
9262 
9263 	/* Show HBA path */
9264 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf);
9265 
9266 	cmn_err(CE_CONT, "?%s :\n", msg_buf);
9267 
9268 	if (sdinfo->satadrv_type == SATA_DTYPE_UNKNOWN) {
9269 		(void) sprintf(msg_buf,
9270 		    "Unsupported SATA device type (cfg 0x%x) at ",
9271 		    sdinfo->satadrv_id.ai_config);
9272 	} else {
9273 		(void) sprintf(msg_buf, "SATA %s device at",
9274 		    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
9275 		    "disk":"CD/DVD (ATAPI)");
9276 	}
9277 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT)
9278 		cmn_err(CE_CONT, "?\t%s port %d\n",
9279 		    msg_buf, sdinfo->satadrv_addr.cport);
9280 	else
9281 		cmn_err(CE_CONT, "?\t%s port %d pmport %d\n",
9282 		    msg_buf, sdinfo->satadrv_addr.cport,
9283 		    sdinfo->satadrv_addr.pmport);
9284 
9285 	bcopy(&sdinfo->satadrv_id.ai_model, msg_buf,
9286 	    sizeof (sdinfo->satadrv_id.ai_model));
9287 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model));
9288 	msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
9289 	cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf);
9290 
9291 	bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf,
9292 	    sizeof (sdinfo->satadrv_id.ai_fw));
9293 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw));
9294 	msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
9295 	cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf);
9296 
9297 	bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf,
9298 	    sizeof (sdinfo->satadrv_id.ai_drvser));
9299 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser));
9300 	msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
9301 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9302 		cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
9303 	} else {
9304 		/* Assuming ATAPI CD/DVD */
9305 		/*
9306 		 * SOme drives do not implement serial number and may
9307 		 * violate the spec by provinding spaces rather than zeros
9308 		 * in serial number field. Scan the buffer to detect it.
9309 		 */
9310 		for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) {
9311 			if (msg_buf[i] != '\0' && msg_buf[i] != ' ')
9312 				break;
9313 		}
9314 		if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) {
9315 			cmn_err(CE_CONT, "?\tserial number - none\n");
9316 		} else {
9317 			cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
9318 		}
9319 	}
9320 
9321 #ifdef SATA_DEBUG
9322 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
9323 	    sdinfo->satadrv_id.ai_majorversion != 0xffff) {
9324 		int i;
9325 		for (i = 14; i >= 2; i--) {
9326 			if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) {
9327 				valid_version = i;
9328 				break;
9329 			}
9330 		}
9331 		cmn_err(CE_CONT,
9332 		    "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
9333 		    valid_version,
9334 		    sdinfo->satadrv_id.ai_majorversion,
9335 		    sdinfo->satadrv_id.ai_minorversion);
9336 	}
9337 #endif
9338 	/* Log some info */
9339 	cmn_err(CE_CONT, "?\tsupported features:\n");
9340 	msg_buf[0] = '\0';
9341 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9342 		if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48)
9343 			(void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN);
9344 		else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28)
9345 			(void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN);
9346 	}
9347 	if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA)
9348 		(void) strlcat(msg_buf, "DMA", MAXPATHLEN);
9349 	if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ)
9350 		(void) strlcat(msg_buf, ", Native Command Queueing",
9351 		    MAXPATHLEN);
9352 	if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)
9353 		(void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN);
9354 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) &&
9355 	    (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED))
9356 		(void) strlcat(msg_buf, ", SMART", MAXPATHLEN);
9357 	if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) &&
9358 	    (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED))
9359 		(void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN);
9360 	cmn_err(CE_CONT, "?\t %s\n", msg_buf);
9361 	if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2)
9362 		cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n");
9363 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1)
9364 		cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n");
9365 	if (sdinfo->satadrv_features_support &
9366 	    (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) {
9367 		msg_buf[0] = '\0';
9368 		(void) snprintf(msg_buf, MAXPATHLEN,
9369 		    "Supported queue depth %d",
9370 		    sdinfo->satadrv_queue_depth);
9371 		if (!(sata_func_enable &
9372 		    (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ)))
9373 			(void) strlcat(msg_buf,
9374 			    " - queueing disabled globally", MAXPATHLEN);
9375 		else if (sdinfo->satadrv_queue_depth >
9376 		    sdinfo->satadrv_max_queue_depth) {
9377 			(void) snprintf(&msg_buf[strlen(msg_buf)],
9378 			    MAXPATHLEN - strlen(msg_buf), ", limited to %d",
9379 			    (int)sdinfo->satadrv_max_queue_depth);
9380 		}
9381 		cmn_err(CE_CONT, "?\t%s\n", msg_buf);
9382 	}
9383 
9384 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9385 #ifdef __i386
9386 		(void) sprintf(msg_buf, "\tcapacity = %llu sectors\n",
9387 		    sdinfo->satadrv_capacity);
9388 #else
9389 		(void) sprintf(msg_buf, "\tcapacity = %lu sectors\n",
9390 		    sdinfo->satadrv_capacity);
9391 #endif
9392 		cmn_err(CE_CONT, "?%s", msg_buf);
9393 	}
9394 }
9395 
9396 
9397 /*
9398  * sata_save_drive_settings extracts current setting of the device and stores
9399  * it for future reference, in case the device setup would need to be restored
9400  * after the device reset.
9401  *
9402  * For all devices read ahead and write cache settings are saved, if the
9403  * device supports these features at all.
9404  * For ATAPI devices the Removable Media Status Notification setting is saved.
9405  */
9406 static void
9407 sata_save_drive_settings(sata_drive_info_t *sdinfo)
9408 {
9409 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) ||
9410 	    (sdinfo->satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) {
9411 
9412 		/* Current setting of Read Ahead (and Read Cache) */
9413 		if (sdinfo->satadrv_id.ai_features85 & SATA_LOOK_AHEAD)
9414 			sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
9415 		else
9416 			sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD;
9417 
9418 		/* Current setting of Write Cache */
9419 		if (sdinfo->satadrv_id.ai_features85 & SATA_WRITE_CACHE)
9420 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
9421 		else
9422 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
9423 	}
9424 
9425 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
9426 		if (sdinfo->satadrv_id.ai_cmdset83 & SATA_RM_STATUS_NOTIFIC)
9427 			sdinfo->satadrv_settings |= SATA_DEV_RMSN;
9428 		else
9429 			sdinfo->satadrv_settings &= ~SATA_DEV_RMSN;
9430 	}
9431 }
9432 
9433 
9434 /*
9435  * sata_check_capacity function determines a disk capacity
9436  * and addressing mode (LBA28/LBA48) by examining a disk identify device data.
9437  *
9438  * NOTE: CHS mode is not supported! If a device does not support LBA,
9439  * this function is not called.
9440  *
9441  * Returns device capacity in number of blocks, i.e. largest addressable LBA+1
9442  */
9443 static uint64_t
9444 sata_check_capacity(sata_drive_info_t *sdinfo)
9445 {
9446 	uint64_t capacity = 0;
9447 	int i;
9448 
9449 	if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK ||
9450 	    !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT)
9451 		/* Capacity valid only for LBA-addressable disk devices */
9452 		return (0);
9453 
9454 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) &&
9455 	    (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) &&
9456 	    (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) {
9457 		/* LBA48 mode supported and enabled */
9458 		sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 |
9459 		    SATA_DEV_F_LBA28;
9460 		for (i = 3;  i >= 0;  --i) {
9461 			capacity <<= 16;
9462 			capacity += sdinfo->satadrv_id.ai_addrsecxt[i];
9463 		}
9464 	} else {
9465 		capacity = sdinfo->satadrv_id.ai_addrsec[1];
9466 		capacity <<= 16;
9467 		capacity += sdinfo->satadrv_id.ai_addrsec[0];
9468 		if (capacity >= 0x1000000)
9469 			/* LBA28 mode */
9470 			sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28;
9471 	}
9472 	return (capacity);
9473 }
9474 
9475 
9476 /*
9477  * Allocate consistent buffer for DMA transfer
9478  *
9479  * Cannot be called from interrupt level or with mutex held - it may sleep.
9480  *
9481  * Returns pointer to allocated buffer structure, or NULL if allocation failed.
9482  */
9483 static struct buf *
9484 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len)
9485 {
9486 	struct scsi_address ap;
9487 	struct buf *bp;
9488 	ddi_dma_attr_t	cur_dma_attr;
9489 
9490 	ASSERT(spx->txlt_sata_pkt != NULL);
9491 	ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran;
9492 	ap.a_target = SATA_TO_SCSI_TARGET(
9493 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport,
9494 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport,
9495 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual);
9496 	ap.a_lun = 0;
9497 
9498 	bp = scsi_alloc_consistent_buf(&ap, NULL, len,
9499 	    B_READ, SLEEP_FUNC, NULL);
9500 
9501 	if (bp != NULL) {
9502 		/* Allocate DMA resources for this buffer */
9503 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
9504 		/*
9505 		 * We use a local version of the dma_attr, to account
9506 		 * for a device addressing limitations.
9507 		 * sata_adjust_dma_attr() will handle sdinfo == NULL which
9508 		 * will cause dma attributes to be adjusted to a lowest
9509 		 * acceptable level.
9510 		 */
9511 		sata_adjust_dma_attr(NULL,
9512 		    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
9513 
9514 		if (sata_dma_buf_setup(spx, PKT_CONSISTENT,
9515 		    SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) {
9516 			scsi_free_consistent_buf(bp);
9517 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
9518 			bp = NULL;
9519 		}
9520 	}
9521 	return (bp);
9522 }
9523 
9524 /*
9525  * Release local buffer (consistent buffer for DMA transfer) allocated
9526  * via sata_alloc_local_buffer().
9527  */
9528 static void
9529 sata_free_local_buffer(sata_pkt_txlate_t *spx)
9530 {
9531 	ASSERT(spx->txlt_sata_pkt != NULL);
9532 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL);
9533 
9534 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0;
9535 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL;
9536 
9537 	if (spx->txlt_buf_dma_handle != NULL) {
9538 		/* Free DMA resources */
9539 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
9540 		ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
9541 		spx->txlt_buf_dma_handle = 0;
9542 
9543 		if (spx->txlt_dma_cookie_list != &spx->txlt_dma_cookie) {
9544 			kmem_free(spx->txlt_dma_cookie_list,
9545 			    spx->txlt_dma_cookie_list_len *
9546 			    sizeof (ddi_dma_cookie_t));
9547 			spx->txlt_dma_cookie_list = NULL;
9548 			spx->txlt_dma_cookie_list_len = 0;
9549 		}
9550 	}
9551 
9552 	/* Free buffer */
9553 	scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp);
9554 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
9555 }
9556 
9557 
9558 
9559 
9560 /*
9561  * Allocate sata_pkt
9562  * Pkt structure version and embedded strcutures version are initialized.
9563  * sata_pkt and sata_pkt_txlate structures are cross-linked.
9564  *
9565  * Since this may be called in interrupt context by sata_scsi_init_pkt,
9566  * callback argument determines if it can sleep or not.
9567  * Hence, it should not be called from interrupt context.
9568  *
9569  * If successful, non-NULL pointer to a sata pkt is returned.
9570  * Upon failure, NULL pointer is returned.
9571  */
9572 static sata_pkt_t *
9573 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t))
9574 {
9575 	sata_pkt_t *spkt;
9576 	int kmsflag;
9577 
9578 	kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
9579 	spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag);
9580 	if (spkt == NULL) {
9581 		SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9582 		    "sata_pkt_alloc: failed"));
9583 		return (NULL);
9584 	}
9585 	spkt->satapkt_rev = SATA_PKT_REV;
9586 	spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV;
9587 	spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
9588 	spkt->satapkt_framework_private = spx;
9589 	spx->txlt_sata_pkt = spkt;
9590 	return (spkt);
9591 }
9592 
9593 /*
9594  * Free sata pkt allocated via sata_pkt_alloc()
9595  */
9596 static void
9597 sata_pkt_free(sata_pkt_txlate_t *spx)
9598 {
9599 	ASSERT(spx->txlt_sata_pkt != NULL);
9600 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL);
9601 	kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t));
9602 	spx->txlt_sata_pkt = NULL;
9603 }
9604 
9605 
9606 /*
9607  * Adjust DMA attributes.
9608  * SCSI cmds block count is up to 24 bits, SATA cmd block count vary
9609  * from 8 bits to 16 bits, depending on a command being used.
9610  * Limiting max block count arbitrarily to 256 for all read/write
9611  * commands may affects performance, so check both the device and
9612  * controller capability before adjusting dma attributes.
9613  */
9614 void
9615 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr,
9616     ddi_dma_attr_t *adj_dma_attr)
9617 {
9618 	uint32_t count_max;
9619 
9620 	/* Copy original attributes */
9621 	*adj_dma_attr = *dma_attr;
9622 	/*
9623 	 * Things to consider: device addressing capability,
9624 	 * "excessive" controller DMA capabilities.
9625 	 * If a device is being probed/initialized, there are
9626 	 * no device info - use default limits then.
9627 	 */
9628 	if (sdinfo == NULL) {
9629 		count_max = dma_attr->dma_attr_granular * 0x100;
9630 		if (dma_attr->dma_attr_count_max > count_max)
9631 			adj_dma_attr->dma_attr_count_max = count_max;
9632 		if (dma_attr->dma_attr_maxxfer > count_max)
9633 			adj_dma_attr->dma_attr_maxxfer = count_max;
9634 		return;
9635 	}
9636 
9637 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9638 		if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) {
9639 			/*
9640 			 * 16-bit sector count may be used - we rely on
9641 			 * the assumption that only read and write cmds
9642 			 * will request more than 256 sectors worth of data
9643 			 */
9644 			count_max = adj_dma_attr->dma_attr_granular * 0x10000;
9645 		} else {
9646 			/*
9647 			 * 8-bit sector count will be used - default limits
9648 			 * for dma attributes
9649 			 */
9650 			count_max = adj_dma_attr->dma_attr_granular * 0x100;
9651 		}
9652 		/*
9653 		 * Adjust controler dma attributes, if necessary
9654 		 */
9655 		if (dma_attr->dma_attr_count_max > count_max)
9656 			adj_dma_attr->dma_attr_count_max = count_max;
9657 		if (dma_attr->dma_attr_maxxfer > count_max)
9658 			adj_dma_attr->dma_attr_maxxfer = count_max;
9659 	}
9660 }
9661 
9662 
9663 /*
9664  * Allocate DMA resources for the buffer
9665  * This function handles initial DMA resource allocation as well as
9666  * DMA window shift and may be called repeatedly for the same DMA window
9667  * until all DMA cookies in the DMA window are processed.
9668  * To guarantee that there is always a coherent set of cookies to process
9669  * by SATA HBA driver (observing alignment, device granularity, etc.),
9670  * the number of slots for DMA cookies is equal to lesser of  a number of
9671  * cookies in a DMA window and a max number of scatter/gather entries.
9672  *
9673  * Returns DDI_SUCCESS upon successful operation.
9674  * Return failure code of a failing command or DDI_FAILURE when
9675  * internal cleanup failed.
9676  */
9677 static int
9678 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags,
9679     int (*callback)(caddr_t), caddr_t arg,
9680     ddi_dma_attr_t *cur_dma_attr)
9681 {
9682 	int	rval;
9683 	off_t	offset;
9684 	size_t	size;
9685 	int	max_sg_len, req_len, i;
9686 	uint_t	dma_flags;
9687 	struct buf	*bp;
9688 	uint64_t	cur_txfer_len;
9689 
9690 
9691 	ASSERT(spx->txlt_sata_pkt != NULL);
9692 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9693 	ASSERT(bp != NULL);
9694 
9695 
9696 	if (spx->txlt_buf_dma_handle == NULL) {
9697 		/*
9698 		 * No DMA resources allocated so far - this is a first call
9699 		 * for this sata pkt.
9700 		 */
9701 		rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst),
9702 		    cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle);
9703 
9704 		if (rval != DDI_SUCCESS) {
9705 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9706 			    "sata_dma_buf_setup: no buf DMA resources %x",
9707 			    rval));
9708 			return (rval);
9709 		}
9710 
9711 		if (bp->b_flags & B_READ)
9712 			dma_flags = DDI_DMA_READ;
9713 		else
9714 			dma_flags = DDI_DMA_WRITE;
9715 
9716 		if (flags & PKT_CONSISTENT)
9717 			dma_flags |= DDI_DMA_CONSISTENT;
9718 
9719 		if (flags & PKT_DMA_PARTIAL)
9720 			dma_flags |= DDI_DMA_PARTIAL;
9721 
9722 		/*
9723 		 * Check buffer alignment and size against dma attributes
9724 		 * Consider dma_attr_align only. There may be requests
9725 		 * with the size lower than device granularity, but they
9726 		 * will not read/write from/to the device, so no adjustment
9727 		 * is necessary. The dma_attr_minxfer theoretically should
9728 		 * be considered, but no HBA driver is checking it.
9729 		 */
9730 		if (IS_P2ALIGNED(bp->b_un.b_addr,
9731 		    cur_dma_attr->dma_attr_align)) {
9732 			rval = ddi_dma_buf_bind_handle(
9733 			    spx->txlt_buf_dma_handle,
9734 			    bp, dma_flags, callback, arg,
9735 			    &spx->txlt_dma_cookie,
9736 			    &spx->txlt_curwin_num_dma_cookies);
9737 		} else { /* Buffer is not aligned */
9738 
9739 			int	(*ddicallback)(caddr_t);
9740 			size_t	bufsz;
9741 
9742 			/* Check id sleeping is allowed */
9743 			ddicallback = (callback == NULL_FUNC) ?
9744 			    DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
9745 
9746 			SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
9747 			    "mis-aligned buffer: addr=0x%p, cnt=%lu",
9748 			    (void *)bp->b_un.b_addr, bp->b_bcount);
9749 
9750 			if (bp->b_flags & (B_PAGEIO|B_PHYS))
9751 				/*
9752 				 * CPU will need to access data in the buffer
9753 				 * (for copying) so map it.
9754 				 */
9755 				bp_mapin(bp);
9756 
9757 			ASSERT(spx->txlt_tmp_buf == NULL);
9758 
9759 			/* Buffer may be padded by ddi_dma_mem_alloc()! */
9760 			rval = ddi_dma_mem_alloc(
9761 			    spx->txlt_buf_dma_handle,
9762 			    bp->b_bcount,
9763 			    &sata_acc_attr,
9764 			    DDI_DMA_STREAMING,
9765 			    ddicallback, NULL,
9766 			    &spx->txlt_tmp_buf,
9767 			    &bufsz,
9768 			    &spx->txlt_tmp_buf_handle);
9769 
9770 			if (rval != DDI_SUCCESS) {
9771 				/* DMA mapping failed */
9772 				(void) ddi_dma_free_handle(
9773 				    &spx->txlt_buf_dma_handle);
9774 				spx->txlt_buf_dma_handle = NULL;
9775 #ifdef SATA_DEBUG
9776 				mbuffail_count++;
9777 #endif
9778 				SATADBG1(SATA_DBG_DMA_SETUP,
9779 				    spx->txlt_sata_hba_inst,
9780 				    "sata_dma_buf_setup: "
9781 				    "buf dma mem alloc failed %x\n", rval);
9782 				return (rval);
9783 			}
9784 			ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf,
9785 			    cur_dma_attr->dma_attr_align));
9786 
9787 #ifdef SATA_DEBUG
9788 			mbuf_count++;
9789 
9790 			if (bp->b_bcount != bufsz)
9791 				/*
9792 				 * This will require special handling, because
9793 				 * DMA cookies will be based on the temporary
9794 				 * buffer size, not the original buffer
9795 				 * b_bcount, so the residue may have to
9796 				 * be counted differently.
9797 				 */
9798 				SATADBG2(SATA_DBG_DMA_SETUP,
9799 				    spx->txlt_sata_hba_inst,
9800 				    "sata_dma_buf_setup: bp size %x != "
9801 				    "bufsz %x\n", bp->b_bcount, bufsz);
9802 #endif
9803 			if (dma_flags & DDI_DMA_WRITE) {
9804 				/*
9805 				 * Write operation - copy data into
9806 				 * an aligned temporary buffer. Buffer will be
9807 				 * synced for device by ddi_dma_addr_bind_handle
9808 				 */
9809 				bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf,
9810 				    bp->b_bcount);
9811 			}
9812 
9813 			rval = ddi_dma_addr_bind_handle(
9814 			    spx->txlt_buf_dma_handle,
9815 			    NULL,
9816 			    spx->txlt_tmp_buf,
9817 			    bufsz, dma_flags, ddicallback, 0,
9818 			    &spx->txlt_dma_cookie,
9819 			    &spx->txlt_curwin_num_dma_cookies);
9820 		}
9821 
9822 		switch (rval) {
9823 		case DDI_DMA_PARTIAL_MAP:
9824 			SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
9825 			    "sata_dma_buf_setup: DMA Partial Map\n", NULL);
9826 			/*
9827 			 * Partial DMA mapping.
9828 			 * Retrieve number of DMA windows for this request.
9829 			 */
9830 			if (ddi_dma_numwin(spx->txlt_buf_dma_handle,
9831 			    &spx->txlt_num_dma_win) != DDI_SUCCESS) {
9832 				if (spx->txlt_tmp_buf != NULL) {
9833 					ddi_dma_mem_free(
9834 					    &spx->txlt_tmp_buf_handle);
9835 					spx->txlt_tmp_buf = NULL;
9836 				}
9837 				(void) ddi_dma_unbind_handle(
9838 				    spx->txlt_buf_dma_handle);
9839 				(void) ddi_dma_free_handle(
9840 				    &spx->txlt_buf_dma_handle);
9841 				spx->txlt_buf_dma_handle = NULL;
9842 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9843 				    "sata_dma_buf_setup: numwin failed\n"));
9844 				return (DDI_FAILURE);
9845 			}
9846 			SATADBG2(SATA_DBG_DMA_SETUP,
9847 			    spx->txlt_sata_hba_inst,
9848 			    "sata_dma_buf_setup: windows: %d, cookies: %d\n",
9849 			    spx->txlt_num_dma_win,
9850 			    spx->txlt_curwin_num_dma_cookies);
9851 			spx->txlt_cur_dma_win = 0;
9852 			break;
9853 
9854 		case DDI_DMA_MAPPED:
9855 			/* DMA fully mapped */
9856 			spx->txlt_num_dma_win = 1;
9857 			spx->txlt_cur_dma_win = 0;
9858 			SATADBG1(SATA_DBG_DMA_SETUP,
9859 			    spx->txlt_sata_hba_inst,
9860 			    "sata_dma_buf_setup: windows: 1 "
9861 			    "cookies: %d\n", spx->txlt_curwin_num_dma_cookies);
9862 			break;
9863 
9864 		default:
9865 			/* DMA mapping failed */
9866 			if (spx->txlt_tmp_buf != NULL) {
9867 				ddi_dma_mem_free(
9868 				    &spx->txlt_tmp_buf_handle);
9869 				spx->txlt_tmp_buf = NULL;
9870 			}
9871 			(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
9872 			spx->txlt_buf_dma_handle = NULL;
9873 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9874 			    "sata_dma_buf_setup: buf dma handle binding "
9875 			    "failed %x\n", rval));
9876 			return (rval);
9877 		}
9878 		spx->txlt_curwin_processed_dma_cookies = 0;
9879 		spx->txlt_dma_cookie_list = NULL;
9880 	} else {
9881 		/*
9882 		 * DMA setup is reused. Check if we need to process more
9883 		 * cookies in current window, or to get next window, if any.
9884 		 */
9885 
9886 		ASSERT(spx->txlt_curwin_processed_dma_cookies <=
9887 		    spx->txlt_curwin_num_dma_cookies);
9888 
9889 		if (spx->txlt_curwin_processed_dma_cookies ==
9890 		    spx->txlt_curwin_num_dma_cookies) {
9891 			/*
9892 			 * All cookies from current DMA window were processed.
9893 			 * Get next DMA window.
9894 			 */
9895 			spx->txlt_cur_dma_win++;
9896 			if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) {
9897 				(void) ddi_dma_getwin(spx->txlt_buf_dma_handle,
9898 				    spx->txlt_cur_dma_win, &offset, &size,
9899 				    &spx->txlt_dma_cookie,
9900 				    &spx->txlt_curwin_num_dma_cookies);
9901 				spx->txlt_curwin_processed_dma_cookies = 0;
9902 			} else {
9903 				/* No more windows! End of request! */
9904 				/* What to do? - panic for now */
9905 				ASSERT(spx->txlt_cur_dma_win >=
9906 				    spx->txlt_num_dma_win);
9907 
9908 				spx->txlt_curwin_num_dma_cookies = 0;
9909 				spx->txlt_curwin_processed_dma_cookies = 0;
9910 				spx->txlt_sata_pkt->
9911 				    satapkt_cmd.satacmd_num_dma_cookies = 0;
9912 				return (DDI_SUCCESS);
9913 			}
9914 		}
9915 	}
9916 	/* There better be at least one DMA cookie outstanding */
9917 	ASSERT((spx->txlt_curwin_num_dma_cookies -
9918 	    spx->txlt_curwin_processed_dma_cookies) > 0);
9919 
9920 	if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) {
9921 		/* The default cookie slot was used in previous run */
9922 		ASSERT(spx->txlt_curwin_processed_dma_cookies == 0);
9923 		spx->txlt_dma_cookie_list = NULL;
9924 		spx->txlt_dma_cookie_list_len = 0;
9925 	}
9926 	if (spx->txlt_curwin_processed_dma_cookies == 0) {
9927 		/*
9928 		 * Processing a new DMA window - set-up dma cookies list.
9929 		 * We may reuse previously allocated cookie array if it is
9930 		 * possible.
9931 		 */
9932 		if (spx->txlt_dma_cookie_list != NULL &&
9933 		    spx->txlt_dma_cookie_list_len <
9934 		    spx->txlt_curwin_num_dma_cookies) {
9935 			/*
9936 			 * New DMA window contains more cookies than
9937 			 * the previous one. We need larger cookie list - free
9938 			 * the old one.
9939 			 */
9940 			(void) kmem_free(spx->txlt_dma_cookie_list,
9941 			    spx->txlt_dma_cookie_list_len *
9942 			    sizeof (ddi_dma_cookie_t));
9943 			spx->txlt_dma_cookie_list = NULL;
9944 			spx->txlt_dma_cookie_list_len = 0;
9945 		}
9946 		if (spx->txlt_dma_cookie_list == NULL) {
9947 			/*
9948 			 * Calculate lesser of number of cookies in this
9949 			 * DMA window and number of s/g entries.
9950 			 */
9951 			max_sg_len = cur_dma_attr->dma_attr_sgllen;
9952 			req_len = MIN(max_sg_len,
9953 			    spx->txlt_curwin_num_dma_cookies);
9954 
9955 			/* Allocate new dma cookie array if necessary */
9956 			if (req_len == 1) {
9957 				/* Only one cookie - no need for a list */
9958 				spx->txlt_dma_cookie_list =
9959 				    &spx->txlt_dma_cookie;
9960 				spx->txlt_dma_cookie_list_len = 1;
9961 			} else {
9962 				/*
9963 				 * More than one cookie - try to allocate space.
9964 				 */
9965 				spx->txlt_dma_cookie_list = kmem_zalloc(
9966 				    sizeof (ddi_dma_cookie_t) * req_len,
9967 				    callback == NULL_FUNC ? KM_NOSLEEP :
9968 				    KM_SLEEP);
9969 				if (spx->txlt_dma_cookie_list == NULL) {
9970 					SATADBG1(SATA_DBG_DMA_SETUP,
9971 					    spx->txlt_sata_hba_inst,
9972 					    "sata_dma_buf_setup: cookie list "
9973 					    "allocation failed\n", NULL);
9974 					/*
9975 					 * We could not allocate space for
9976 					 * neccessary number of dma cookies in
9977 					 * this window, so we fail this request.
9978 					 * Next invocation would try again to
9979 					 * allocate space for cookie list.
9980 					 * Note:Packet residue was not modified.
9981 					 */
9982 					return (DDI_DMA_NORESOURCES);
9983 				} else {
9984 					spx->txlt_dma_cookie_list_len = req_len;
9985 				}
9986 			}
9987 		}
9988 		/*
9989 		 * Fetch DMA cookies into cookie list in sata_pkt_txlate.
9990 		 * First cookie was already fetched.
9991 		 */
9992 		*(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie;
9993 		cur_txfer_len =
9994 		    (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size;
9995 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1;
9996 		spx->txlt_curwin_processed_dma_cookies++;
9997 		for (i = 1; (i < spx->txlt_dma_cookie_list_len) &&
9998 		    (i < spx->txlt_curwin_num_dma_cookies); i++) {
9999 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
10000 			    &spx->txlt_dma_cookie_list[i]);
10001 			cur_txfer_len +=
10002 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
10003 			spx->txlt_curwin_processed_dma_cookies++;
10004 			spx->txlt_sata_pkt->
10005 			    satapkt_cmd.satacmd_num_dma_cookies += 1;
10006 		}
10007 	} else {
10008 		SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
10009 		    "sata_dma_buf_setup: sliding within DMA window, "
10010 		    "cur cookie %d, total cookies %d\n",
10011 		    spx->txlt_curwin_processed_dma_cookies,
10012 		    spx->txlt_curwin_num_dma_cookies);
10013 
10014 		/*
10015 		 * Not all cookies from the current dma window were used because
10016 		 * of s/g limitation.
10017 		 * There is no need to re-size the list - it was set at
10018 		 * optimal size, or only default entry is used (s/g = 1).
10019 		 */
10020 		if (spx->txlt_dma_cookie_list == NULL) {
10021 			spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie;
10022 			spx->txlt_dma_cookie_list_len = 1;
10023 		}
10024 		/*
10025 		 * Since we are processing remaining cookies in a DMA window,
10026 		 * there may be less of them than the number of entries in the
10027 		 * current dma cookie list.
10028 		 */
10029 		req_len = MIN(spx->txlt_dma_cookie_list_len,
10030 		    (spx->txlt_curwin_num_dma_cookies -
10031 		    spx->txlt_curwin_processed_dma_cookies));
10032 
10033 		/* Fetch the next batch of cookies */
10034 		for (i = 0, cur_txfer_len = 0; i < req_len; i++) {
10035 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
10036 			    &spx->txlt_dma_cookie_list[i]);
10037 			cur_txfer_len +=
10038 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
10039 			spx->txlt_sata_pkt->
10040 			    satapkt_cmd.satacmd_num_dma_cookies++;
10041 			spx->txlt_curwin_processed_dma_cookies++;
10042 		}
10043 	}
10044 
10045 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0);
10046 
10047 	/* Point sata_cmd to the cookie list */
10048 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list =
10049 	    &spx->txlt_dma_cookie_list[0];
10050 
10051 	/* Remember number of DMA cookies passed in sata packet */
10052 	spx->txlt_num_dma_cookies =
10053 	    spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies;
10054 
10055 	ASSERT(cur_txfer_len != 0);
10056 	if (cur_txfer_len <= bp->b_bcount)
10057 		spx->txlt_total_residue -= cur_txfer_len;
10058 	else {
10059 		/*
10060 		 * Temporary DMA buffer has been padded by
10061 		 * ddi_dma_mem_alloc()!
10062 		 * This requires special handling, because DMA cookies are
10063 		 * based on the temporary buffer size, not the b_bcount,
10064 		 * and we have extra bytes to transfer - but the packet
10065 		 * residue has to stay correct because we will copy only
10066 		 * the requested number of bytes.
10067 		 */
10068 		spx->txlt_total_residue -= bp->b_bcount;
10069 	}
10070 
10071 	return (DDI_SUCCESS);
10072 }
10073 
10074 /*
10075  * Common routine for releasing DMA resources
10076  */
10077 static void
10078 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx)
10079 {
10080 	if (spx->txlt_buf_dma_handle != NULL) {
10081 		if (spx->txlt_tmp_buf != NULL)  {
10082 			/*
10083 			 * Intermediate DMA buffer was allocated.
10084 			 * Free allocated buffer and associated access handle.
10085 			 */
10086 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
10087 			spx->txlt_tmp_buf = NULL;
10088 		}
10089 		/*
10090 		 * Free DMA resources - cookies and handles
10091 		 */
10092 		/* ASSERT(spx->txlt_dma_cookie_list != NULL); */
10093 		if (spx->txlt_dma_cookie_list != NULL) {
10094 			if (spx->txlt_dma_cookie_list !=
10095 			    &spx->txlt_dma_cookie) {
10096 				(void) kmem_free(spx->txlt_dma_cookie_list,
10097 				    spx->txlt_dma_cookie_list_len *
10098 				    sizeof (ddi_dma_cookie_t));
10099 				spx->txlt_dma_cookie_list = NULL;
10100 			}
10101 		}
10102 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
10103 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
10104 		spx->txlt_buf_dma_handle = NULL;
10105 	}
10106 }
10107 
10108 /*
10109  * Free DMA resources
10110  * Used by the HBA driver to release DMA resources that it does not use.
10111  *
10112  * Returns Void
10113  */
10114 void
10115 sata_free_dma_resources(sata_pkt_t *sata_pkt)
10116 {
10117 	sata_pkt_txlate_t *spx;
10118 
10119 	if (sata_pkt == NULL)
10120 		return;
10121 
10122 	spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
10123 
10124 	sata_common_free_dma_rsrcs(spx);
10125 }
10126 
10127 /*
10128  * Fetch Device Identify data.
10129  * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type)
10130  * command to a device and get the device identify data.
10131  * The device_info structure has to be set to device type (for selecting proper
10132  * device identify command).
10133  *
10134  * Returns:
10135  * SATA_SUCCESS if cmd succeeded
10136  * SATA_RETRY if cmd was rejected and could be retried,
10137  * SATA_FAILURE if cmd failed and should not be retried (port error)
10138  *
10139  * Cannot be called in an interrupt context.
10140  */
10141 
10142 static int
10143 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst,
10144     sata_drive_info_t *sdinfo)
10145 {
10146 	struct buf *bp;
10147 	sata_pkt_t *spkt;
10148 	sata_cmd_t *scmd;
10149 	sata_pkt_txlate_t *spx;
10150 	int rval;
10151 
10152 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10153 	spx->txlt_sata_hba_inst = sata_hba_inst;
10154 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
10155 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10156 	if (spkt == NULL) {
10157 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10158 		return (SATA_RETRY); /* may retry later */
10159 	}
10160 	/* address is needed now */
10161 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10162 
10163 	/*
10164 	 * Allocate buffer for Identify Data return data
10165 	 */
10166 	bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t));
10167 	if (bp == NULL) {
10168 		sata_pkt_free(spx);
10169 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10170 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10171 		    "sata_fetch_device_identify_data: "
10172 		    "cannot allocate buffer for ID"));
10173 		return (SATA_RETRY); /* may retry later */
10174 	}
10175 
10176 	/* Fill sata_pkt */
10177 	sdinfo->satadrv_state = SATA_STATE_PROBING;
10178 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10179 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10180 	/* Synchronous mode, no callback */
10181 	spkt->satapkt_comp = NULL;
10182 	/* Timeout 30s */
10183 	spkt->satapkt_time = sata_default_pkt_time;
10184 
10185 	scmd = &spkt->satapkt_cmd;
10186 	scmd->satacmd_bp = bp;
10187 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
10188 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10189 
10190 	/* Build Identify Device cmd in the sata_pkt */
10191 	scmd->satacmd_addr_type = 0;		/* N/A */
10192 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
10193 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
10194 	scmd->satacmd_lba_mid_lsb = 0;		/* N/A */
10195 	scmd->satacmd_lba_high_lsb = 0;		/* N/A */
10196 	scmd->satacmd_features_reg = 0;		/* N/A */
10197 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
10198 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
10199 		/* Identify Packet Device cmd */
10200 		scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE;
10201 	} else {
10202 		/* Identify Device cmd - mandatory for all other devices */
10203 		scmd->satacmd_cmd_reg = SATAC_ID_DEVICE;
10204 	}
10205 
10206 	/* Send pkt to SATA HBA driver */
10207 	rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt);
10208 
10209 #ifdef SATA_INJECT_FAULTS
10210 	if (sata_inject_fault == SATA_INJECT_PKT_FAULT)
10211 		if (sata_fault_cmd == scmd->satacmd_cmd_reg)
10212 			sata_inject_pkt_fault(spkt, scmd->satacmd_cmd_reg,
10213 			    &rval, sata_inject_fault_type);
10214 #endif
10215 
10216 	if (rval == SATA_TRAN_ACCEPTED &&
10217 	    spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10218 		if (spx->txlt_buf_dma_handle != NULL) {
10219 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10220 			    DDI_DMA_SYNC_FORKERNEL);
10221 			ASSERT(rval == DDI_SUCCESS);
10222 		}
10223 		if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config &
10224 		    SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) {
10225 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10226 			    "SATA disk device at port %d - "
10227 			    "partial Identify Data",
10228 			    sdinfo->satadrv_addr.cport));
10229 			rval = SATA_RETRY; /* may retry later */
10230 			goto fail;
10231 		}
10232 		/* Update sata_drive_info */
10233 		bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id,
10234 		    sizeof (sata_id_t));
10235 
10236 		sdinfo->satadrv_features_support = 0;
10237 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10238 			/*
10239 			 * Retrieve capacity (disks only) and addressing mode
10240 			 */
10241 			sdinfo->satadrv_capacity = sata_check_capacity(sdinfo);
10242 		} else {
10243 			/*
10244 			 * For ATAPI devices one would have to issue
10245 			 * Get Capacity cmd for media capacity. Not here.
10246 			 */
10247 			sdinfo->satadrv_capacity = 0;
10248 			/*
10249 			 * Check what cdb length is supported
10250 			 */
10251 			if ((sdinfo->satadrv_id.ai_config &
10252 			    SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B)
10253 				sdinfo->satadrv_atapi_cdb_len = 16;
10254 			else
10255 				sdinfo->satadrv_atapi_cdb_len = 12;
10256 		}
10257 		/* Setup supported features flags */
10258 		if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT)
10259 			sdinfo->satadrv_features_support |= SATA_DEV_F_DMA;
10260 
10261 		/* Check for SATA GEN and NCQ support */
10262 		if (sdinfo->satadrv_id.ai_satacap != 0 &&
10263 		    sdinfo->satadrv_id.ai_satacap != 0xffff) {
10264 			/* SATA compliance */
10265 			if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ)
10266 				sdinfo->satadrv_features_support |=
10267 				    SATA_DEV_F_NCQ;
10268 			if (sdinfo->satadrv_id.ai_satacap &
10269 			    (SATA_1_SPEED | SATA_2_SPEED)) {
10270 				if (sdinfo->satadrv_id.ai_satacap &
10271 				    SATA_2_SPEED)
10272 					sdinfo->satadrv_features_support |=
10273 					    SATA_DEV_F_SATA2;
10274 				if (sdinfo->satadrv_id.ai_satacap &
10275 				    SATA_1_SPEED)
10276 					sdinfo->satadrv_features_support |=
10277 					    SATA_DEV_F_SATA1;
10278 			} else {
10279 				sdinfo->satadrv_features_support |=
10280 				    SATA_DEV_F_SATA1;
10281 			}
10282 		}
10283 		if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) &&
10284 		    (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD))
10285 			sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ;
10286 
10287 		sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth;
10288 		if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) ||
10289 		    (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) {
10290 			++sdinfo->satadrv_queue_depth;
10291 			/* Adjust according to controller capabilities */
10292 			sdinfo->satadrv_max_queue_depth = MIN(
10293 			    sdinfo->satadrv_queue_depth,
10294 			    SATA_QDEPTH(sata_hba_inst));
10295 			/* Adjust according to global queue depth limit */
10296 			sdinfo->satadrv_max_queue_depth = MIN(
10297 			    sdinfo->satadrv_max_queue_depth,
10298 			    sata_current_max_qdepth);
10299 			if (sdinfo->satadrv_max_queue_depth == 0)
10300 				sdinfo->satadrv_max_queue_depth = 1;
10301 		} else
10302 			sdinfo->satadrv_max_queue_depth = 1;
10303 
10304 		rval = SATA_SUCCESS;
10305 	} else {
10306 		/*
10307 		 * Woops, no Identify Data.
10308 		 */
10309 		if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) {
10310 			rval = SATA_RETRY; /* may retry later */
10311 		} else if (rval == SATA_TRAN_ACCEPTED) {
10312 			if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR ||
10313 			    spkt->satapkt_reason == SATA_PKT_ABORTED ||
10314 			    spkt->satapkt_reason == SATA_PKT_TIMEOUT ||
10315 			    spkt->satapkt_reason == SATA_PKT_RESET)
10316 				rval = SATA_RETRY; /* may retry later */
10317 			else
10318 				rval = SATA_FAILURE;
10319 		} else {
10320 			rval = SATA_FAILURE;
10321 		}
10322 	}
10323 fail:
10324 	/* Free allocated resources */
10325 	sata_free_local_buffer(spx);
10326 	sata_pkt_free(spx);
10327 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
10328 
10329 	return (rval);
10330 }
10331 
10332 
10333 /*
10334  * Some devices may not come-up with default DMA mode (UDMA or MWDMA).
10335  * UDMA mode is checked first, followed by MWDMA mode.
10336  * set correctly, so this function is setting it to the highest supported level.
10337  * Older SATA spec required that the device supports at least DMA 4 mode and
10338  * UDMA mode is selected.  It is not mentioned in SerialATA 2.6, so this
10339  * restriction has been removed.
10340  *
10341  * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported.
10342  * Returns SATA_FAILURE if proper DMA mode could not be selected.
10343  *
10344  * NOTE: This function should be called only if DMA mode is supported.
10345  */
10346 static int
10347 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo)
10348 {
10349 	sata_pkt_t *spkt;
10350 	sata_cmd_t *scmd;
10351 	sata_pkt_txlate_t *spx;
10352 	int i, mode;
10353 	uint8_t subcmd;
10354 	int rval = SATA_SUCCESS;
10355 
10356 	ASSERT(sdinfo != NULL);
10357 	ASSERT(sata_hba_inst != NULL);
10358 
10359 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
10360 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) {
10361 		/* Find highest Ultra DMA mode supported */
10362 		for (mode = 6; mode >= 0; --mode) {
10363 			if (sdinfo->satadrv_id.ai_ultradma & (1 << mode))
10364 				break;
10365 		}
10366 #if 0
10367 		/* Left for historical reasons */
10368 		/*
10369 		 * Some initial version of SATA spec indicated that at least
10370 		 * UDMA mode 4 has to be supported. It is not mentioned in
10371 		 * SerialATA 2.6, so this restriction is removed.
10372 		 */
10373 		if (mode < 4)
10374 			return (SATA_FAILURE);
10375 #endif
10376 		/* Find UDMA mode currently selected */
10377 		for (i = 6; i >= 0; --i) {
10378 			if (sdinfo->satadrv_id.ai_ultradma & (1 << (i + 8)))
10379 				break;
10380 		}
10381 		if (i >= mode)
10382 			/* Nothing to do */
10383 			return (SATA_SUCCESS);
10384 
10385 		subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA;
10386 
10387 	} else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) {
10388 		/* Find highest MultiWord DMA mode supported */
10389 		for (mode = 2; mode >= 0; --mode) {
10390 			if (sdinfo->satadrv_id.ai_dworddma & (1 << mode))
10391 				break;
10392 		}
10393 		/* Find highest MultiWord DMA mode selected */
10394 		for (i = 2; i >= 0; --i) {
10395 			if (sdinfo->satadrv_id.ai_dworddma & (1 << (i + 8)))
10396 				break;
10397 		}
10398 		if (i >= mode)
10399 			/* Nothing to do */
10400 			return (SATA_SUCCESS);
10401 
10402 		subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA;
10403 	} else
10404 		return (SATA_SUCCESS);
10405 
10406 	/*
10407 	 * Set DMA mode via SET FEATURES COMMAND.
10408 	 * Prepare packet for SET FEATURES COMMAND.
10409 	 */
10410 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10411 	spx->txlt_sata_hba_inst = sata_hba_inst;
10412 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10413 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10414 	if (spkt == NULL) {
10415 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10416 		    "sata_set_dma_mode: could not set DMA mode %", mode));
10417 		rval = SATA_FAILURE;
10418 		goto done;
10419 	}
10420 	/* Fill sata_pkt */
10421 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10422 	/* Timeout 30s */
10423 	spkt->satapkt_time = sata_default_pkt_time;
10424 	/* Synchronous mode, no callback, interrupts */
10425 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10426 	spkt->satapkt_comp = NULL;
10427 	scmd = &spkt->satapkt_cmd;
10428 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10429 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10430 	scmd->satacmd_addr_type = 0;
10431 	scmd->satacmd_device_reg = 0;
10432 	scmd->satacmd_status_reg = 0;
10433 	scmd->satacmd_error_reg = 0;
10434 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10435 	scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE;
10436 	scmd->satacmd_sec_count_lsb = subcmd | mode;
10437 
10438 	/* Transfer command to HBA */
10439 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
10440 	    spkt) != SATA_TRAN_ACCEPTED ||
10441 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
10442 		/* Pkt execution failed */
10443 		rval = SATA_FAILURE;
10444 	}
10445 done:
10446 
10447 	/* Free allocated resources */
10448 	if (spkt != NULL)
10449 		sata_pkt_free(spx);
10450 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10451 
10452 	return (rval);
10453 }
10454 
10455 
10456 /*
10457  * Set device caching mode.
10458  * One of the following operations should be specified:
10459  * SATAC_SF_ENABLE_READ_AHEAD
10460  * SATAC_SF_DISABLE_READ_AHEAD
10461  * SATAC_SF_ENABLE_WRITE_CACHE
10462  * SATAC_SF_DISABLE_WRITE_CACHE
10463  *
10464  * If operation fails, system log messgage is emitted.
10465  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
10466  */
10467 
10468 static int
10469 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
10470     int cache_op)
10471 {
10472 	sata_pkt_t *spkt;
10473 	sata_cmd_t *scmd;
10474 	sata_pkt_txlate_t *spx;
10475 	int rval = SATA_SUCCESS;
10476 	char *infop;
10477 
10478 	ASSERT(sdinfo != NULL);
10479 	ASSERT(sata_hba_inst != NULL);
10480 	ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD ||
10481 	    cache_op == SATAC_SF_DISABLE_READ_AHEAD ||
10482 	    cache_op == SATAC_SF_ENABLE_WRITE_CACHE ||
10483 	    cache_op == SATAC_SF_DISABLE_WRITE_CACHE);
10484 
10485 
10486 	/* Prepare packet for SET FEATURES COMMAND */
10487 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10488 	spx->txlt_sata_hba_inst = sata_hba_inst;
10489 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10490 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10491 	if (spkt == NULL) {
10492 		rval = SATA_FAILURE;
10493 		goto failure;
10494 	}
10495 	/* Fill sata_pkt */
10496 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10497 	/* Timeout 30s */
10498 	spkt->satapkt_time = sata_default_pkt_time;
10499 	/* Synchronous mode, no callback, interrupts */
10500 	spkt->satapkt_op_mode =
10501 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10502 	spkt->satapkt_comp = NULL;
10503 	scmd = &spkt->satapkt_cmd;
10504 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10505 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10506 	scmd->satacmd_addr_type = 0;
10507 	scmd->satacmd_device_reg = 0;
10508 	scmd->satacmd_status_reg = 0;
10509 	scmd->satacmd_error_reg = 0;
10510 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10511 	scmd->satacmd_features_reg = cache_op;
10512 
10513 	/* Transfer command to HBA */
10514 	if (((*SATA_START_FUNC(sata_hba_inst))(
10515 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
10516 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
10517 		/* Pkt execution failed */
10518 		switch (cache_op) {
10519 		case SATAC_SF_ENABLE_READ_AHEAD:
10520 			infop = "enabling read ahead failed";
10521 			break;
10522 		case SATAC_SF_DISABLE_READ_AHEAD:
10523 			infop = "disabling read ahead failed";
10524 			break;
10525 		case SATAC_SF_ENABLE_WRITE_CACHE:
10526 			infop = "enabling write cache failed";
10527 			break;
10528 		case SATAC_SF_DISABLE_WRITE_CACHE:
10529 			infop = "disabling write cache failed";
10530 			break;
10531 		}
10532 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
10533 		rval = SATA_FAILURE;
10534 	}
10535 failure:
10536 	/* Free allocated resources */
10537 	if (spkt != NULL)
10538 		sata_pkt_free(spx);
10539 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10540 	return (rval);
10541 }
10542 
10543 /*
10544  * Set Removable Media Status Notification (enable/disable)
10545  * state == 0 , disable
10546  * state != 0 , enable
10547  *
10548  * If operation fails, system log messgage is emitted.
10549  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
10550  */
10551 
10552 static int
10553 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
10554     int state)
10555 {
10556 	sata_pkt_t *spkt;
10557 	sata_cmd_t *scmd;
10558 	sata_pkt_txlate_t *spx;
10559 	int rval = SATA_SUCCESS;
10560 	char *infop;
10561 
10562 	ASSERT(sdinfo != NULL);
10563 	ASSERT(sata_hba_inst != NULL);
10564 
10565 	/* Prepare packet for SET FEATURES COMMAND */
10566 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10567 	spx->txlt_sata_hba_inst = sata_hba_inst;
10568 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10569 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10570 	if (spkt == NULL) {
10571 		rval = SATA_FAILURE;
10572 		goto failure;
10573 	}
10574 	/* Fill sata_pkt */
10575 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10576 	/* Timeout 30s */
10577 	spkt->satapkt_time = sata_default_pkt_time;
10578 	/* Synchronous mode, no callback, interrupts */
10579 	spkt->satapkt_op_mode =
10580 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10581 	spkt->satapkt_comp = NULL;
10582 	scmd = &spkt->satapkt_cmd;
10583 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10584 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10585 	scmd->satacmd_addr_type = 0;
10586 	scmd->satacmd_device_reg = 0;
10587 	scmd->satacmd_status_reg = 0;
10588 	scmd->satacmd_error_reg = 0;
10589 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10590 	if (state == 0)
10591 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN;
10592 	else
10593 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN;
10594 
10595 	/* Transfer command to HBA */
10596 	if (((*SATA_START_FUNC(sata_hba_inst))(
10597 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
10598 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
10599 		/* Pkt execution failed */
10600 		if (state == 0)
10601 			infop = "disabling Removable Media Status "
10602 			    "Notification failed";
10603 		else
10604 			infop = "enabling Removable Media Status "
10605 			    "Notification failed";
10606 
10607 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
10608 		rval = SATA_FAILURE;
10609 	}
10610 failure:
10611 	/* Free allocated resources */
10612 	if (spkt != NULL)
10613 		sata_pkt_free(spx);
10614 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10615 	return (rval);
10616 }
10617 
10618 
10619 /*
10620  * Update port SCR block
10621  */
10622 static void
10623 sata_update_port_scr(sata_port_scr_t *port_scr, sata_device_t *device)
10624 {
10625 	port_scr->sstatus = device->satadev_scr.sstatus;
10626 	port_scr->serror = device->satadev_scr.serror;
10627 	port_scr->scontrol = device->satadev_scr.scontrol;
10628 	port_scr->sactive = device->satadev_scr.sactive;
10629 	port_scr->snotific = device->satadev_scr.snotific;
10630 }
10631 
10632 /*
10633  * Update state and copy port ss* values from passed sata_device structure.
10634  * sata_address is validated - if not valid, nothing is changed in sata_scsi
10635  * configuration struct.
10636  *
10637  * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function
10638  * regardless of the state in device argument.
10639  *
10640  * Port mutex should be held while calling this function.
10641  */
10642 static void
10643 sata_update_port_info(sata_hba_inst_t *sata_hba_inst,
10644 	sata_device_t *sata_device)
10645 {
10646 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst,
10647 	    sata_device->satadev_addr.cport)));
10648 
10649 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT ||
10650 	    sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
10651 
10652 		sata_cport_info_t *cportinfo;
10653 
10654 		if (SATA_NUM_CPORTS(sata_hba_inst) <=
10655 		    sata_device->satadev_addr.cport)
10656 			return;
10657 
10658 		cportinfo = SATA_CPORT_INFO(sata_hba_inst,
10659 		    sata_device->satadev_addr.cport);
10660 		sata_update_port_scr(&cportinfo->cport_scr, sata_device);
10661 
10662 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
10663 		cportinfo->cport_state &= ~(SATA_PSTATE_PWRON |
10664 		    SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
10665 		cportinfo->cport_state |=
10666 		    sata_device->satadev_state & SATA_PSTATE_VALID;
10667 	} else {
10668 		sata_pmport_info_t *pmportinfo;
10669 
10670 		if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT) ||
10671 		    (sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) ||
10672 		    SATA_NUM_PMPORTS(sata_hba_inst,
10673 		    sata_device->satadev_addr.cport) <
10674 		    sata_device->satadev_addr.pmport)
10675 			return;
10676 
10677 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
10678 		    sata_device->satadev_addr.cport,
10679 		    sata_device->satadev_addr.pmport);
10680 		sata_update_port_scr(&pmportinfo->pmport_scr, sata_device);
10681 
10682 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
10683 		pmportinfo->pmport_state &=
10684 		    ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF |
10685 		    SATA_PSTATE_FAILED);
10686 		pmportinfo->pmport_state |=
10687 		    sata_device->satadev_state & SATA_PSTATE_VALID;
10688 	}
10689 }
10690 
10691 
10692 
10693 /*
10694  * Extract SATA port specification from an IOCTL argument.
10695  *
10696  * This function return the port the user land send us as is, unless it
10697  * cannot retrieve port spec, then -1 is returned.
10698  *
10699  * Note: Only cport  - no port multiplier port.
10700  */
10701 static int32_t
10702 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp)
10703 {
10704 	int32_t port;
10705 
10706 	/* Extract port number from nvpair in dca structure  */
10707 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
10708 		SATA_LOG_D((sata_hba_inst, CE_NOTE,
10709 		    "sata_get_port_num: invalid port spec 0x%x in ioctl",
10710 		    port));
10711 		port = -1;
10712 	}
10713 
10714 	return (port);
10715 }
10716 
10717 /*
10718  * Get dev_info_t pointer to the device node pointed to by port argument.
10719  * NOTE: target argument is a value used in ioctls to identify
10720  * the AP - it is not a sata_address.
10721  * It is a combination of cport, pmport and address qualifier, encodded same
10722  * way as a scsi target number.
10723  * At this moment it carries only cport number.
10724  *
10725  * No PMult hotplug support.
10726  *
10727  * Returns dev_info_t pointer if target device was found, NULL otherwise.
10728  */
10729 
10730 static dev_info_t *
10731 sata_get_target_dip(dev_info_t *dip, int32_t port)
10732 {
10733 	dev_info_t	*cdip = NULL;
10734 	int		target, tgt;
10735 	int		ncport;
10736 	int 		circ;
10737 
10738 	ncport = port & SATA_CFGA_CPORT_MASK;
10739 	target = SATA_TO_SCSI_TARGET(ncport, 0, SATA_ADDR_DCPORT);
10740 
10741 	ndi_devi_enter(dip, &circ);
10742 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
10743 		dev_info_t *next = ddi_get_next_sibling(cdip);
10744 
10745 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
10746 		    DDI_PROP_DONTPASS, "target", -1);
10747 		if (tgt == -1) {
10748 			/*
10749 			 * This is actually an error condition, but not
10750 			 * a fatal one. Just continue the search.
10751 			 */
10752 			cdip = next;
10753 			continue;
10754 		}
10755 
10756 		if (tgt == target)
10757 			break;
10758 
10759 		cdip = next;
10760 	}
10761 	ndi_devi_exit(dip, circ);
10762 
10763 	return (cdip);
10764 }
10765 
10766 /*
10767  * Get dev_info_t pointer to the device node pointed to by port argument.
10768  * NOTE: target argument is a value used in ioctls to identify
10769  * the AP - it is not a sata_address.
10770  * It is a combination of cport, pmport and address qualifier, encoded same
10771  * way as a scsi target number.
10772  * At this moment it carries only cport number.
10773  *
10774  * No PMult hotplug support.
10775  *
10776  * Returns dev_info_t pointer if target device was found, NULL otherwise.
10777  */
10778 
10779 static dev_info_t *
10780 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr)
10781 {
10782 	dev_info_t	*cdip = NULL;
10783 	int		target, tgt;
10784 	int 		circ;
10785 
10786 	target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual);
10787 
10788 	ndi_devi_enter(dip, &circ);
10789 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
10790 		dev_info_t *next = ddi_get_next_sibling(cdip);
10791 
10792 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
10793 		    DDI_PROP_DONTPASS, "target", -1);
10794 		if (tgt == -1) {
10795 			/*
10796 			 * This is actually an error condition, but not
10797 			 * a fatal one. Just continue the search.
10798 			 */
10799 			cdip = next;
10800 			continue;
10801 		}
10802 
10803 		if (tgt == target)
10804 			break;
10805 
10806 		cdip = next;
10807 	}
10808 	ndi_devi_exit(dip, circ);
10809 
10810 	return (cdip);
10811 }
10812 
10813 /*
10814  * Process sata port disconnect request.
10815  * Normally, cfgadm sata plugin will try to offline (unconfigure) the device
10816  * before this request. Nevertheless, if a device is still configured,
10817  * we need to attempt to offline and unconfigure device.
10818  * Regardless of the unconfigure operation results the port is marked as
10819  * deactivated and no access to the attached device is possible.
10820  * If the target node remains because unconfigure operation failed, its state
10821  * will be set to DEVICE_REMOVED, preventing it to be used again when a device
10822  * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure
10823  * the device and remove old target node.
10824  *
10825  * This function invokes sata_hba_inst->satahba_tran->
10826  * sata_tran_hotplug_ops->sata_tran_port_deactivate().
10827  * If successful, the device structure (if any) attached to the specified port
10828  * is removed and state of the port marked appropriately.
10829  * Failure of the port_deactivate may keep port in the physically active state,
10830  * or may fail the port.
10831  *
10832  * NOTE: Port multiplier code is not completed nor tested.
10833  */
10834 
10835 static int
10836 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst,
10837     sata_device_t *sata_device)
10838 {
10839 	sata_drive_info_t *sdinfo = NULL;
10840 	sata_cport_info_t *cportinfo = NULL;
10841 	sata_pmport_info_t *pmportinfo = NULL;
10842 	sata_pmult_info_t *pmultinfo = NULL;
10843 	dev_info_t *tdip;
10844 	int cport, pmport, qual;
10845 	int rval = SATA_SUCCESS;
10846 	int rv = 0;
10847 
10848 	cport = sata_device->satadev_addr.cport;
10849 	pmport = sata_device->satadev_addr.pmport;
10850 	qual = sata_device->satadev_addr.qual;
10851 
10852 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
10853 
10854 	/*
10855 	 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran->
10856 	 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
10857 	 * Do the sanity check.
10858 	 */
10859 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
10860 		/* No physical port deactivation supported. */
10861 		return (EINVAL);
10862 	}
10863 
10864 	/* Check the current state of the port */
10865 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10866 	    (SATA_DIP(sata_hba_inst), sata_device);
10867 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10868 	sata_update_port_info(sata_hba_inst, sata_device);
10869 	if (rval != SATA_SUCCESS ||
10870 	    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
10871 		/* Device port status is unknown or it is in failed state */
10872 		if (qual == SATA_ADDR_PMPORT) {
10873 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
10874 			    SATA_PSTATE_FAILED;
10875 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
10876 			    "sata_hba_ioctl: connect: failed to deactivate "
10877 			    "SATA port %d", cport);
10878 		} else {
10879 			SATA_CPORT_STATE(sata_hba_inst, cport) =
10880 			    SATA_PSTATE_FAILED;
10881 			SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
10882 			    "sata_hba_ioctl: connect: failed to deactivate "
10883 			    "SATA port %d:%d", cport, pmport);
10884 		}
10885 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
10886 		    cport)->cport_mutex);
10887 		return (EIO);
10888 	}
10889 	/*
10890 	 * Set port's dev_state to not ready - this will disable
10891 	 * an access to a potentially attached device.
10892 	 */
10893 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
10894 	if (qual == SATA_ADDR_PMPORT) {
10895 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
10896 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
10897 			sdinfo = pmportinfo->pmport_sata_drive;
10898 			ASSERT(sdinfo != NULL);
10899 		}
10900 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
10901 	} else {
10902 		/* Assuming cport */
10903 
10904 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
10905 			if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
10906 				pmultinfo =
10907 				    cportinfo->cport_devp.cport_sata_pmult;
10908 				ASSERT(pmultinfo != NULL);
10909 			} else {
10910 				sdinfo = cportinfo->cport_devp.cport_sata_drive;
10911 			}
10912 		}
10913 		cportinfo->cport_state &= ~SATA_STATE_READY;
10914 	}
10915 	if (sdinfo != NULL) {
10916 		if ((sdinfo->satadrv_type & (SATA_VALID_DEV_TYPE)) != 0) {
10917 			/*
10918 			 * If a target node exists, try to offline
10919 			 * a device and remove target node.
10920 			 */
10921 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
10922 			    cport)->cport_mutex);
10923 			/* We are addressing attached device, not a port */
10924 			sata_device->satadev_addr.qual =
10925 			    sdinfo->satadrv_addr.qual;
10926 			tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
10927 			    &sata_device->satadev_addr);
10928 			if (tdip != NULL && ndi_devi_offline(tdip,
10929 			    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
10930 				/*
10931 				 * Problem
10932 				 * The target node remained attached.
10933 				 * This happens when the device file was open
10934 				 * or a node was waiting for resources.
10935 				 * Cannot do anything about it.
10936 				 */
10937 				if (qual == SATA_ADDR_CPORT) {
10938 					SATA_LOG_D((sata_hba_inst, CE_WARN,
10939 					    "sata_hba_ioctl: disconnect: could "
10940 					    "not unconfigure device before "
10941 					    "disconnecting the SATA port %d",
10942 					    cport));
10943 				} else {
10944 					SATA_LOG_D((sata_hba_inst, CE_WARN,
10945 					    "sata_hba_ioctl: disconnect: could "
10946 					    "not unconfigure device before "
10947 					    "disconnecting the SATA port %d:%d",
10948 					    cport, pmport));
10949 				}
10950 				/*
10951 				 * Set DEVICE REMOVED state in the target
10952 				 * node. It will prevent access to the device
10953 				 * even when a new device is attached, until
10954 				 * the old target node is released, removed and
10955 				 * recreated for a new  device.
10956 				 */
10957 				sata_set_device_removed(tdip);
10958 
10959 				/*
10960 				 * Instruct event daemon to try the target
10961 				 * node cleanup later.
10962 				 */
10963 				sata_set_target_node_cleanup(
10964 				    sata_hba_inst, &sata_device->satadev_addr);
10965 			}
10966 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
10967 			    cport)->cport_mutex);
10968 		}
10969 
10970 		/* Remove and release sata_drive info structure. */
10971 		if (pmportinfo != NULL) {
10972 			SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport) =
10973 			    NULL;
10974 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
10975 		} else {
10976 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
10977 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
10978 		}
10979 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
10980 	}
10981 #if 0
10982 	else if (pmultinfo != NULL) {
10983 		/*
10984 		 * Port Multiplier itself needs special handling.
10985 		 * All device ports need to be processed here!
10986 		 */
10987 	}
10988 #endif
10989 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10990 	/* Just ask HBA driver to deactivate port */
10991 	/*	sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; */
10992 
10993 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
10994 	    (SATA_DIP(sata_hba_inst), sata_device);
10995 
10996 	/*
10997 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
10998 	 * without the hint (to force listener to investivate the state).
10999 	 */
11000 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11001 	    SE_NO_HINT);
11002 
11003 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11004 	sata_update_port_info(sata_hba_inst, sata_device);
11005 
11006 	if (rval != SATA_SUCCESS) {
11007 		/*
11008 		 * Port deactivation failure - do not
11009 		 * change port state unless the state
11010 		 * returned by HBA indicates a port failure.
11011 		 * NOTE: device structures were released, so devices now are
11012 		 * invisible! Port reset is needed to re-enumerate devices.
11013 		 */
11014 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11015 			if (pmportinfo != NULL)
11016 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
11017 			else
11018 				cportinfo->cport_state = SATA_PSTATE_FAILED;
11019 			rv = EIO;
11020 		}
11021 	} else {
11022 		/*
11023 		 * Deactivation succeded. From now on the sata framework
11024 		 * will not care what is happening to the device, until
11025 		 * the port is activated again.
11026 		 */
11027 		cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
11028 	}
11029 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11030 	return (rv);
11031 }
11032 
11033 
11034 
11035 /*
11036  * Process sata port connect request
11037  * The sata cfgadm pluging will invoke this operation only if port was found
11038  * in the disconnect state (failed state is also treated as the disconnected
11039  * state).
11040  * DEVCTL_AP_CONNECT would invoke  sata_hba_inst->satahba_tran->
11041  * sata_tran_hotplug_ops->sata_tran_port_activate().
11042  * If successful and a device is found attached to the port,
11043  * the initialization sequence is executed to attach a device structure to
11044  * a port structure. The state of the port and a device would be set
11045  * appropriately.
11046  * The device is not set in configured state (system-wise) by this operation.
11047  *
11048  * Note, that activating the port may generate link events,
11049  * so it is important that following processing and the
11050  * event processing does not interfere with each other!
11051  *
11052  * This operation may remove port failed state and will
11053  * try to make port active and in good standing.
11054  *
11055  * NOTE: Port multiplier code is not completed nor tested.
11056  */
11057 
11058 static int
11059 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst,
11060     sata_device_t *sata_device)
11061 {
11062 	int cport, pmport, qual;
11063 	int rv = 0;
11064 
11065 	cport = sata_device->satadev_addr.cport;
11066 	pmport = sata_device->satadev_addr.pmport;
11067 	qual = sata_device->satadev_addr.qual;
11068 
11069 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
11070 
11071 	/*
11072 	 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->
11073 	 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate().
11074 	 * Perform sanity check now.
11075 	 */
11076 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) {
11077 		/* No physical port activation supported. */
11078 		return (EINVAL);
11079 	}
11080 
11081 	/* Just ask HBA driver to activate port */
11082 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11083 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11084 		/*
11085 		 * Port activation failure.
11086 		 */
11087 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11088 		    cport)->cport_mutex);
11089 		sata_update_port_info(sata_hba_inst, sata_device);
11090 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11091 			if (qual == SATA_ADDR_DCPORT) {
11092 				SATA_CPORT_STATE(sata_hba_inst, cport) =
11093 				    SATA_PSTATE_FAILED;
11094 				SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
11095 				    "sata_hba_ioctl: connect: failed to "
11096 				    "activate SATA port %d", cport);
11097 			} else { /* port multiplier device port */
11098 				SATA_PMPORT_STATE(sata_hba_inst, cport,
11099 				    pmport) = SATA_PSTATE_FAILED;
11100 				SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
11101 				    "sata_hba_ioctl: connect: failed to "
11102 				    "activate SATA port %d:%d", cport, pmport);
11103 
11104 			}
11105 		}
11106 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11107 		    cport)->cport_mutex);
11108 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
11109 		    "sata_hba_ioctl: connect: failed to activate SATA "
11110 		    "port %d:%d", cport, pmport);
11111 		return (EIO);
11112 	}
11113 
11114 	/* Virgin port state - will be updated by the port re-probe. */
11115 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11116 	if (qual == SATA_ADDR_CPORT)
11117 		SATA_CPORT_STATE(sata_hba_inst, cport) = 0;
11118 	else /* port multiplier device port */
11119 		SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0;
11120 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11121 
11122 	/*
11123 	 * Probe the port to find its state and attached device.
11124 	 */
11125 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11126 	    SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE)
11127 		rv = EIO;
11128 
11129 	/*
11130 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11131 	 * without the hint
11132 	 */
11133 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11134 	    SE_NO_HINT);
11135 
11136 	/*
11137 	 * If there is a device attached to the port, emit
11138 	 * a message.
11139 	 */
11140 	if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11141 
11142 		if (qual == SATA_ADDR_CPORT) {
11143 			sata_log(sata_hba_inst, CE_WARN,
11144 			    "SATA device detected at port %d", cport);
11145 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11146 				/*
11147 				 * A device was not successfully identified
11148 				 */
11149 				sata_log(sata_hba_inst, CE_WARN,
11150 				    "Could not identify SATA "
11151 				    "device at port %d", cport);
11152 			}
11153 		} else { /* port multiplier device port */
11154 			sata_log(sata_hba_inst, CE_WARN,
11155 			    "SATA device detected at port %d:%d",
11156 			    cport, pmport);
11157 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11158 				/*
11159 				 * A device was not successfully identified
11160 				 */
11161 				sata_log(sata_hba_inst, CE_WARN,
11162 				    "Could not identify SATA "
11163 				    "device at port %d:%d", cport, pmport);
11164 			}
11165 		}
11166 	}
11167 
11168 	return (rv);
11169 }
11170 
11171 
11172 /*
11173  * Process sata device unconfigure request.
11174  * The unconfigure operation uses generic nexus operation to
11175  * offline a device. It leaves a target device node attached.
11176  * and obviously sata_drive_info attached as well, because
11177  * from the hardware point of view nothing has changed.
11178  */
11179 static int
11180 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst,
11181     sata_device_t *sata_device)
11182 {
11183 	int rv = 0;
11184 	dev_info_t *tdip;
11185 
11186 	/* We are addressing attached device, not a port */
11187 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
11188 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11189 	else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT)
11190 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11191 
11192 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11193 	    &sata_device->satadev_addr)) != NULL) {
11194 
11195 		if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) {
11196 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11197 			    "sata_hba_ioctl: unconfigure: "
11198 			    "failed to unconfigure device at SATA port %d:%d",
11199 			    sata_device->satadev_addr.cport,
11200 			    sata_device->satadev_addr.pmport));
11201 			rv = EIO;
11202 		}
11203 		/*
11204 		 * The target node devi_state should be marked with
11205 		 * DEVI_DEVICE_OFFLINE by ndi_devi_offline().
11206 		 * This would be the indication for cfgadm that
11207 		 * the AP node occupant state is 'unconfigured'.
11208 		 */
11209 
11210 	} else {
11211 		/*
11212 		 * This would indicate a failure on the part of cfgadm
11213 		 * to detect correct state of the node prior to this
11214 		 * call - one cannot unconfigure non-existing device.
11215 		 */
11216 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11217 		    "sata_hba_ioctl: unconfigure: "
11218 		    "attempt to unconfigure non-existing device "
11219 		    "at SATA port %d:%d",
11220 		    sata_device->satadev_addr.cport,
11221 		    sata_device->satadev_addr.pmport));
11222 		rv = ENXIO;
11223 	}
11224 	return (rv);
11225 }
11226 
11227 /*
11228  * Process sata device configure request
11229  * If port is in a failed state, operation is aborted - one has to use
11230  * an explicit connect or port activate request to try to get a port into
11231  * non-failed mode. Port reset wil also work in such situation.
11232  * If the port is in disconnected (shutdown) state, the connect operation is
11233  * attempted prior to any other action.
11234  * When port is in the active state, there is a device attached and the target
11235  * node exists, a device was most likely offlined.
11236  * If target node does not exist, a new target node is created. In both cases
11237  * an attempt is made to online (configure) the device.
11238  *
11239  * NOTE: Port multiplier code is not completed nor tested.
11240  */
11241 static int
11242 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst,
11243     sata_device_t *sata_device)
11244 {
11245 	int cport, pmport, qual;
11246 	int rval;
11247 	boolean_t target = TRUE;
11248 	sata_cport_info_t *cportinfo;
11249 	sata_pmport_info_t *pmportinfo = NULL;
11250 	dev_info_t *tdip;
11251 	sata_drive_info_t *sdinfo;
11252 
11253 	cport = sata_device->satadev_addr.cport;
11254 	pmport = sata_device->satadev_addr.pmport;
11255 	qual = sata_device->satadev_addr.qual;
11256 
11257 	/* Get current port state */
11258 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11259 	    (SATA_DIP(sata_hba_inst), sata_device);
11260 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11261 	sata_update_port_info(sata_hba_inst, sata_device);
11262 
11263 	if (rval != SATA_SUCCESS ||
11264 	    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
11265 		/*
11266 		 * Obviously, device on a failed port is not visible
11267 		 */
11268 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11269 		return (ENXIO);
11270 	}
11271 
11272 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11273 	if (qual == SATA_ADDR_PMPORT)
11274 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11275 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11276 
11277 	if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) {
11278 		/* need to activate port */
11279 		target = FALSE;
11280 
11281 		/* Sanity check */
11282 		if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
11283 			return (ENXIO);
11284 
11285 		/* Just let HBA driver to activate port */
11286 		if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11287 		    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11288 			/*
11289 			 * Port activation failure - do not change port state
11290 			 * unless the state returned by HBA indicates a port
11291 			 * failure.
11292 			 */
11293 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11294 			    cport)->cport_mutex);
11295 			sata_update_port_info(sata_hba_inst, sata_device);
11296 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11297 				if (qual == SATA_ADDR_PMPORT)
11298 					pmportinfo->pmport_state =
11299 					    SATA_PSTATE_FAILED;
11300 				else
11301 					cportinfo->cport_state =
11302 					    SATA_PSTATE_FAILED;
11303 			}
11304 			mutex_exit(&SATA_CPORT_INFO(
11305 			    sata_hba_inst, cport)->cport_mutex);
11306 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11307 			    "sata_hba_ioctl: configure: "
11308 			    "failed to activate SATA port %d:%d",
11309 			    cport, pmport));
11310 			return (EIO);
11311 		}
11312 		/*
11313 		 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11314 		 * without the hint.
11315 		 */
11316 		sata_gen_sysevent(sata_hba_inst,
11317 		    &sata_device->satadev_addr, SE_NO_HINT);
11318 
11319 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11320 		    cport_mutex);
11321 		/* Virgin port state */
11322 		if (qual == SATA_ADDR_PMPORT)
11323 			pmportinfo->pmport_state = 0;
11324 		else
11325 			cportinfo->cport_state = 0;
11326 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11327 	}
11328 	/*
11329 	 * Always reprobe port, to get current device info.
11330 	 */
11331 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11332 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
11333 		return (EIO);
11334 
11335 	if (sata_device->satadev_type != SATA_DTYPE_NONE && target == FALSE) {
11336 		if (qual == SATA_ADDR_PMPORT) {
11337 			/*
11338 			 * That's the transition from "inactive" port
11339 			 * to active one with device attached.
11340 			 */
11341 			sata_log(sata_hba_inst, CE_WARN,
11342 			    "SATA device detected at port %d:%d",
11343 			    cport, pmport);
11344 		} else {
11345 			/*
11346 			 * When PM is attached to the cport and cport is
11347 			 * activated, every PM device port needs to be reprobed.
11348 			 * We need to emit message for all devices detected
11349 			 * at port multiplier's device ports.
11350 			 * Add such code here.
11351 			 * For now, just inform about device attached to
11352 			 * cport.
11353 			 */
11354 			sata_log(sata_hba_inst, CE_WARN,
11355 			    "SATA device detected at port %d", cport);
11356 		}
11357 	}
11358 
11359 	/*
11360 	 * This is where real configuration operation starts.
11361 	 *
11362 	 * When PM is attached to the cport and cport is activated,
11363 	 * devices attached PM device ports may have to be configured
11364 	 * explicitly. This may change when port multiplier is supported.
11365 	 * For now, configure only disks and other valid target devices.
11366 	 */
11367 	if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) {
11368 		if (qual == SATA_ADDR_CPORT) {
11369 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11370 				/*
11371 				 * A device was not successfully identified
11372 				 */
11373 				sata_log(sata_hba_inst, CE_WARN,
11374 				    "Could not identify SATA "
11375 				    "device at port %d", cport);
11376 			}
11377 		} else { /* port multiplier device port */
11378 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11379 				/*
11380 				 * A device was not successfully identified
11381 				 */
11382 				sata_log(sata_hba_inst, CE_WARN,
11383 				    "Could not identify SATA "
11384 				    "device at port %d:%d", cport, pmport);
11385 			}
11386 		}
11387 		return (ENXIO);		/* No device to configure */
11388 	}
11389 
11390 	/*
11391 	 * Here we may have a device in reset condition,
11392 	 * but because we are just configuring it, there is
11393 	 * no need to process the reset other than just
11394 	 * to clear device reset condition in the HBA driver.
11395 	 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will
11396 	 * cause a first command sent the HBA driver with the request
11397 	 * to clear device reset condition.
11398 	 */
11399 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11400 	if (qual == SATA_ADDR_PMPORT)
11401 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11402 	else
11403 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11404 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
11405 	if (sdinfo == NULL) {
11406 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11407 		return (ENXIO);
11408 	}
11409 	if (sdinfo->satadrv_event_flags &
11410 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
11411 		sdinfo->satadrv_event_flags = 0;
11412 	}
11413 	sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
11414 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11415 
11416 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11417 	    &sata_device->satadev_addr)) != NULL) {
11418 		/*
11419 		 * Target node exists. Verify, that it belongs
11420 		 * to existing, attached device and not to
11421 		 * a removed device.
11422 		 */
11423 		if (sata_check_device_removed(tdip) == B_TRUE) {
11424 			if (qual == SATA_ADDR_DPMPORT)
11425 				sata_log(sata_hba_inst, CE_WARN,
11426 				    "SATA device at port %d cannot be "
11427 				    "configured. "
11428 				    "Application(s) accessing "
11429 				    "previously attached device "
11430 				    "have to release it before newly "
11431 				    "inserted device can be made accessible.",
11432 				    cport);
11433 			else
11434 				sata_log(sata_hba_inst, CE_WARN,
11435 				    "SATA device at port %d:%d cannot be"
11436 				    "configured. "
11437 				    "Application(s) accessing "
11438 				    "previously attached device "
11439 				    "have to release it before newly "
11440 				    "inserted device can be made accessible.",
11441 				    cport, pmport);
11442 			return (EIO);
11443 		}
11444 		/*
11445 		 * Device was not removed and re-inserted.
11446 		 * Try to online it.
11447 		 */
11448 		if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) {
11449 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11450 			    "sata_hba_ioctl: configure: "
11451 			    "onlining device at SATA port "
11452 			    "%d:%d failed", cport, pmport));
11453 			return (EIO);
11454 		}
11455 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11456 		    cport)->cport_mutex);
11457 
11458 		if (qual == SATA_ADDR_DPMPORT)
11459 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
11460 		else
11461 			cportinfo-> cport_tgtnode_clean = B_TRUE;
11462 
11463 		mutex_exit(&SATA_CPORT_INFO(
11464 		    sata_hba_inst, cport)->cport_mutex);
11465 	} else {
11466 		/*
11467 		 * No target node - need to create a new target node.
11468 		 */
11469 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11470 		    cport_mutex);
11471 		if (qual == SATA_ADDR_DPMPORT)
11472 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
11473 		else
11474 			cportinfo-> cport_tgtnode_clean = B_TRUE;
11475 
11476 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11477 		    cport_mutex);
11478 		tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
11479 		    sata_hba_inst, &sata_device->satadev_addr);
11480 		if (tdip == NULL) {
11481 			/* Configure operation failed */
11482 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11483 			    "sata_hba_ioctl: configure: "
11484 			    "configuring SATA device at port %d:%d "
11485 			    "failed", cport, pmport));
11486 			return (EIO);
11487 		}
11488 	}
11489 	return (0);
11490 }
11491 
11492 
11493 /*
11494  * Process ioctl deactivate port request.
11495  * Arbitrarily unconfigure attached device, if any.
11496  * Even if the unconfigure fails, proceed with the
11497  * port deactivation.
11498  *
11499  * NOTE: Port Multiplier code is not completed and tested.
11500  */
11501 
11502 static int
11503 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst,
11504     sata_device_t *sata_device)
11505 {
11506 	int cport, pmport, qual;
11507 	int rval, rv = 0;
11508 	sata_cport_info_t *cportinfo;
11509 	sata_pmport_info_t *pmportinfo = NULL;
11510 	dev_info_t *tdip;
11511 	sata_drive_info_t *sdinfo = NULL;
11512 
11513 	/* Sanity check */
11514 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL)
11515 		return (ENOTSUP);
11516 
11517 	cport = sata_device->satadev_addr.cport;
11518 	pmport = sata_device->satadev_addr.pmport;
11519 	qual = sata_device->satadev_addr.qual;
11520 
11521 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11522 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11523 	if (qual == SATA_ADDR_CPORT) {
11524 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11525 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
11526 			/*
11527 			 * For now, assume that port multiplier is not
11528 			 * supported, i.e. deal only with valid devices
11529 			 */
11530 			if ((cportinfo->cport_dev_type &
11531 			    SATA_VALID_DEV_TYPE) != 0)
11532 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11533 			/*
11534 			 * If attached device is a port multiplier, we will
11535 			 * have to unconfigure all devices attached to the
11536 			 * port multiplier. Add this code here.
11537 			 */
11538 		}
11539 		cportinfo->cport_state &= ~SATA_STATE_READY;
11540 	} else {
11541 		/* Port multiplier device port */
11542 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11543 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11544 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
11545 		    (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0)
11546 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11547 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
11548 	}
11549 
11550 	if (sdinfo != NULL) {
11551 		/*
11552 		 * If a target node exists, try to offline a device and
11553 		 * to remove a target node.
11554 		 */
11555 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11556 		    cport_mutex);
11557 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11558 		    &sata_device->satadev_addr);
11559 		if (tdip != NULL) {
11560 			/* target node exist */
11561 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
11562 			    "sata_hba_ioctl: port deactivate: "
11563 			    "target node exists.", NULL);
11564 
11565 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) !=
11566 			    NDI_SUCCESS) {
11567 				SATA_LOG_D((sata_hba_inst, CE_WARN,
11568 				    "sata_hba_ioctl: port deactivate: "
11569 				    "failed to unconfigure device at port "
11570 				    "%d:%d before deactivating the port",
11571 				    cport, pmport));
11572 				/*
11573 				 * Set DEVICE REMOVED state in the target
11574 				 * node. It will prevent an access to
11575 				 * the device even when a new device is
11576 				 * attached, until the old target node is
11577 				 * released, removed and recreated for a new
11578 				 * device.
11579 				 */
11580 				sata_set_device_removed(tdip);
11581 
11582 				/*
11583 				 * Instruct the event daemon to try the
11584 				 * target node cleanup later.
11585 				 */
11586 				sata_set_target_node_cleanup(sata_hba_inst,
11587 				    &sata_device->satadev_addr);
11588 			}
11589 		}
11590 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11591 		    cport_mutex);
11592 		/*
11593 		 * In any case, remove and release sata_drive_info
11594 		 * structure.
11595 		 */
11596 		if (qual == SATA_ADDR_CPORT) {
11597 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11598 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11599 		} else { /* port multiplier device port */
11600 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11601 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11602 		}
11603 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
11604 	}
11605 	if (qual == SATA_ADDR_CPORT) {
11606 		cportinfo->cport_state &= ~(SATA_STATE_PROBED |
11607 		    SATA_STATE_PROBING);
11608 	} else { /* port multiplier device port */
11609 		pmportinfo->pmport_state &= ~(SATA_STATE_PROBED |
11610 		    SATA_STATE_PROBING);
11611 	}
11612 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11613 
11614 	/* Just let HBA driver to deactivate port */
11615 	sata_device->satadev_addr.qual = qual;
11616 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
11617 	    (SATA_DIP(sata_hba_inst), sata_device);
11618 
11619 	/*
11620 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11621 	 * without the hint
11622 	 */
11623 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11624 	    SE_NO_HINT);
11625 
11626 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11627 	sata_update_port_info(sata_hba_inst, sata_device);
11628 	if (qual == SATA_ADDR_CPORT) {
11629 		if (rval != SATA_SUCCESS) {
11630 			/*
11631 			 * Port deactivation failure - do not change port state
11632 			 * unless the state returned by HBA indicates a port
11633 			 * failure.
11634 			 */
11635 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11636 				SATA_CPORT_STATE(sata_hba_inst, cport) =
11637 				    SATA_PSTATE_FAILED;
11638 			}
11639 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11640 			    "sata_hba_ioctl: port deactivate: "
11641 			    "cannot deactivate SATA port %d", cport));
11642 			rv = EIO;
11643 		} else {
11644 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
11645 		}
11646 	} else {
11647 		if (rval != SATA_SUCCESS) {
11648 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11649 				SATA_PMPORT_STATE(sata_hba_inst, cport,
11650 				    pmport) = SATA_PSTATE_FAILED;
11651 			}
11652 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11653 			    "sata_hba_ioctl: port deactivate: "
11654 			    "cannot deactivate SATA port %d:%d",
11655 			    cport, pmport));
11656 			rv = EIO;
11657 		} else {
11658 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
11659 		}
11660 	}
11661 
11662 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11663 
11664 	return (rv);
11665 }
11666 
11667 /*
11668  * Process ioctl port activate request.
11669  *
11670  * NOTE: Port multiplier code is not completed nor tested.
11671  */
11672 static int
11673 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst,
11674     sata_device_t *sata_device)
11675 {
11676 	int cport, pmport, qual;
11677 	sata_cport_info_t *cportinfo;
11678 	sata_pmport_info_t *pmportinfo = NULL;
11679 	boolean_t dev_existed = TRUE;
11680 
11681 	/* Sanity check */
11682 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
11683 		return (ENOTSUP);
11684 
11685 	cport = sata_device->satadev_addr.cport;
11686 	pmport = sata_device->satadev_addr.pmport;
11687 	qual = sata_device->satadev_addr.qual;
11688 
11689 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11690 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11691 	if (qual == SATA_ADDR_PMPORT) {
11692 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11693 		if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN ||
11694 		    pmportinfo->pmport_dev_type == SATA_DTYPE_NONE)
11695 			dev_existed = FALSE;
11696 	} else { /* cport */
11697 		if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN ||
11698 		    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
11699 			dev_existed = FALSE;
11700 	}
11701 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11702 
11703 	/* Just let HBA driver to activate port, if necessary */
11704 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11705 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11706 		/*
11707 		 * Port activation failure - do not change port state unless
11708 		 * the state returned by HBA indicates a port failure.
11709 		 */
11710 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11711 		    cport)->cport_mutex);
11712 		sata_update_port_info(sata_hba_inst, sata_device);
11713 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11714 			if (qual == SATA_ADDR_PMPORT)
11715 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
11716 			else
11717 				cportinfo->cport_state = SATA_PSTATE_FAILED;
11718 
11719 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11720 			    cport)->cport_mutex);
11721 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11722 			    "sata_hba_ioctl: port activate: cannot activate "
11723 			    "SATA port %d:%d", cport, pmport));
11724 			return (EIO);
11725 		}
11726 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11727 	}
11728 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11729 	if (qual == SATA_ADDR_PMPORT)
11730 		pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN;
11731 	else
11732 		cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN;
11733 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11734 
11735 	/*
11736 	 * Re-probe port to find its current state and possibly attached device.
11737 	 * Port re-probing may change the cportinfo device type if device is
11738 	 * found attached.
11739 	 * If port probing failed, the device type would be set to
11740 	 * SATA_DTYPE_NONE.
11741 	 */
11742 	(void) sata_reprobe_port(sata_hba_inst, sata_device,
11743 	    SATA_DEV_IDENTIFY_RETRY);
11744 
11745 	/*
11746 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11747 	 * without the hint.
11748 	 */
11749 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11750 	    SE_NO_HINT);
11751 
11752 	if (dev_existed == FALSE) {
11753 		if (qual == SATA_ADDR_PMPORT &&
11754 		    pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
11755 			/*
11756 			 * That's the transition from the "inactive" port state
11757 			 * or the active port without a device attached to the
11758 			 * active port state with a device attached.
11759 			 */
11760 			sata_log(sata_hba_inst, CE_WARN,
11761 			    "SATA device detected at port %d:%d",
11762 			    cport, pmport);
11763 		} else if (qual == SATA_ADDR_CPORT &&
11764 		    cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
11765 			/*
11766 			 * That's the transition from the "inactive" port state
11767 			 * or the active port without a device attached to the
11768 			 * active port state with a device attached.
11769 			 */
11770 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
11771 				sata_log(sata_hba_inst, CE_WARN,
11772 				    "SATA device detected at port %d", cport);
11773 			} else {
11774 				sata_log(sata_hba_inst, CE_WARN,
11775 				    "SATA port multiplier detected at port %d",
11776 				    cport);
11777 				/*
11778 				 * Because the detected device is a port
11779 				 * multiplier, we need to reprobe every device
11780 				 * port on the port multiplier and show every
11781 				 * device found attached.
11782 				 * Add this code here.
11783 				 */
11784 			}
11785 		}
11786 	}
11787 	return (0);
11788 }
11789 
11790 
11791 
11792 /*
11793  * Process ioctl reset port request.
11794  *
11795  * NOTE: Port multiplier code is not completed nor tested.
11796  */
11797 static int
11798 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst,
11799     sata_device_t *sata_device)
11800 {
11801 	int cport, pmport, qual;
11802 	int rv = 0;
11803 
11804 	cport = sata_device->satadev_addr.cport;
11805 	pmport = sata_device->satadev_addr.pmport;
11806 	qual = sata_device->satadev_addr.qual;
11807 
11808 	/* Sanity check */
11809 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
11810 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11811 		    "sata_hba_ioctl: sata_hba_tran missing required "
11812 		    "function sata_tran_reset_dport"));
11813 		return (ENOTSUP);
11814 	}
11815 
11816 	/* Ask HBA to reset port */
11817 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
11818 	    sata_device) != SATA_SUCCESS) {
11819 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11820 		    "sata_hba_ioctl: reset port: failed %d:%d",
11821 		    cport, pmport));
11822 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11823 		    cport_mutex);
11824 		sata_update_port_info(sata_hba_inst, sata_device);
11825 		if (qual == SATA_ADDR_CPORT)
11826 			SATA_CPORT_STATE(sata_hba_inst, cport) =
11827 			    SATA_PSTATE_FAILED;
11828 		else
11829 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
11830 			    SATA_PSTATE_FAILED;
11831 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11832 		    cport_mutex);
11833 		rv = EIO;
11834 	}
11835 	/*
11836 	 * Beacuse the port was reset, it should be probed and
11837 	 * attached device reinitialized. At this point the
11838 	 * port state is unknown - it's state is HBA-specific.
11839 	 * Re-probe port to get its state.
11840 	 */
11841 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11842 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) {
11843 		rv = EIO;
11844 	}
11845 	return (rv);
11846 }
11847 
11848 /*
11849  * Process ioctl reset device request.
11850  *
11851  * NOTE: Port multiplier code is not completed nor tested.
11852  */
11853 static int
11854 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst,
11855     sata_device_t *sata_device)
11856 {
11857 	sata_drive_info_t *sdinfo;
11858 	int cport, pmport;
11859 	int rv = 0;
11860 
11861 	/* Sanity check */
11862 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
11863 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11864 		    "sata_hba_ioctl: sata_hba_tran missing required "
11865 		    "function sata_tran_reset_dport"));
11866 		return (ENOTSUP);
11867 	}
11868 
11869 	cport = sata_device->satadev_addr.cport;
11870 	pmport = sata_device->satadev_addr.pmport;
11871 
11872 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11873 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) {
11874 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11875 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
11876 		    sata_device->satadev_addr.cport);
11877 	} else { /* port multiplier */
11878 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11879 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
11880 		    sata_device->satadev_addr.cport,
11881 		    sata_device->satadev_addr.pmport);
11882 	}
11883 	if (sdinfo == NULL) {
11884 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11885 		return (EINVAL);
11886 	}
11887 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11888 
11889 	/* Ask HBA to reset device */
11890 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
11891 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11892 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11893 		    "sata_hba_ioctl: reset device: failed at port %d:%d",
11894 		    cport, pmport));
11895 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11896 		    cport_mutex);
11897 		sata_update_port_info(sata_hba_inst, sata_device);
11898 		/*
11899 		 * Device info structure remains attached. Another device reset
11900 		 * or port disconnect/connect and re-probing is
11901 		 * needed to change it's state
11902 		 */
11903 		sdinfo->satadrv_state &= ~SATA_STATE_READY;
11904 		sdinfo->satadrv_state |= SATA_DSTATE_FAILED;
11905 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11906 		rv = EIO;
11907 	}
11908 	/*
11909 	 * If attached device was a port multiplier, some extra processing
11910 	 * may be needed, to bring it back (if port re-probing did not handle
11911 	 * it). Add such code here.
11912 	 */
11913 	return (rv);
11914 }
11915 
11916 
11917 /*
11918  * Process ioctl reset all request.
11919  *
11920  * NOTE: Port multiplier code is not completed nor tested.
11921  */
11922 static int
11923 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst)
11924 {
11925 	sata_device_t sata_device;
11926 	int rv = 0;
11927 	int tcport;
11928 	int tpmport = 0;
11929 
11930 	sata_device.satadev_rev = SATA_DEVICE_REV;
11931 
11932 	/*
11933 	 * There is no protection here for configured devices.
11934 	 */
11935 	/* Sanity check */
11936 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
11937 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11938 		    "sata_hba_ioctl: sata_hba_tran missing required "
11939 		    "function sata_tran_reset_dport"));
11940 		return (ENOTSUP);
11941 	}
11942 
11943 	/*
11944 	 * Need to lock all ports, not just one.
11945 	 * If any port is locked by event processing, fail the whole operation.
11946 	 * One port is already locked, but for simplicity lock it again.
11947 	 */
11948 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
11949 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11950 		    cport_mutex);
11951 		if (((SATA_CPORT_INFO(sata_hba_inst, tcport)->
11952 		    cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) {
11953 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11954 			    cport_mutex);
11955 			rv = EBUSY;
11956 			break;
11957 		} else {
11958 			SATA_CPORT_INFO(sata_hba_inst, tcport)->
11959 			    cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
11960 			/*
11961 			 * If there is a port multiplier attached, we may need
11962 			 * to lock its port as well. If so, add such code here.
11963 			 */
11964 		}
11965 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11966 		    cport_mutex);
11967 	}
11968 
11969 	if (rv == 0) {
11970 		/*
11971 		 * All cports were successfully locked.
11972 		 * Reset main SATA controller only for now - no PMult.
11973 		 * Set the device address to port 0, to have a valid device
11974 		 * address.
11975 		 */
11976 		sata_device.satadev_addr.qual = SATA_ADDR_CNTRL;
11977 		sata_device.satadev_addr.cport = 0;
11978 		sata_device.satadev_addr.pmport = 0;
11979 
11980 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
11981 		    (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) {
11982 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11983 			    "sata_hba_ioctl: reset controller failed"));
11984 			return (EIO);
11985 		}
11986 		/*
11987 		 * Because ports were reset, port states are unknown.
11988 		 * They should be re-probed to get their state and
11989 		 * attached devices should be reinitialized.
11990 		 * Add code here to re-probe port multiplier device ports.
11991 		 */
11992 		for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst);
11993 		    tcport++) {
11994 			sata_device.satadev_addr.cport = tcport;
11995 			sata_device.satadev_addr.pmport = tpmport;
11996 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
11997 
11998 			if (sata_reprobe_port(sata_hba_inst, &sata_device,
11999 			    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
12000 				rv = EIO;
12001 		}
12002 	}
12003 	/*
12004 	 * Unlock all ports
12005 	 */
12006 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
12007 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
12008 		    cport_mutex);
12009 		SATA_CPORT_INFO(sata_hba_inst, tcport)->
12010 		    cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
12011 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
12012 		    cport_mutex);
12013 	}
12014 
12015 	/*
12016 	 * This operation returns EFAULT if either reset
12017 	 * controller failed or a re-probing of any port failed.
12018 	 */
12019 	return (rv);
12020 }
12021 
12022 
12023 /*
12024  * Process ioctl port self test request.
12025  *
12026  * NOTE: Port multiplier code is not completed nor tested.
12027  */
12028 static int
12029 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst,
12030     sata_device_t *sata_device)
12031 {
12032 	int cport, pmport, qual;
12033 	int rv = 0;
12034 
12035 	/* Sanity check */
12036 	if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL)
12037 		return (ENOTSUP);
12038 
12039 	cport = sata_device->satadev_addr.cport;
12040 	pmport = sata_device->satadev_addr.pmport;
12041 	qual = sata_device->satadev_addr.qual;
12042 
12043 	/*
12044 	 * There is no protection here for a configured
12045 	 * device attached to this port.
12046 	 */
12047 
12048 	if ((*SATA_SELFTEST_FUNC(sata_hba_inst))
12049 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
12050 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12051 		    "sata_hba_ioctl: port selftest: "
12052 		    "failed port %d:%d", cport, pmport));
12053 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12054 		    cport_mutex);
12055 		sata_update_port_info(sata_hba_inst, sata_device);
12056 		if (qual == SATA_ADDR_CPORT)
12057 			SATA_CPORT_STATE(sata_hba_inst, cport) =
12058 			    SATA_PSTATE_FAILED;
12059 		else /* port ultiplier device port */
12060 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
12061 			    SATA_PSTATE_FAILED;
12062 
12063 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12064 		    cport_mutex);
12065 		return (EIO);
12066 	}
12067 	/*
12068 	 * Beacuse the port was reset in the course of testing, it should be
12069 	 * re-probed and attached device state should be restored. At this
12070 	 * point the port state is unknown - it's state is HBA-specific.
12071 	 * Force port re-probing to get it into a known state.
12072 	 */
12073 	if (sata_reprobe_port(sata_hba_inst, sata_device,
12074 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
12075 		rv = EIO;
12076 	return (rv);
12077 }
12078 
12079 
12080 /*
12081  * sata_cfgadm_state:
12082  * Use the sata port state and state of the target node to figure out
12083  * the cfgadm_state.
12084  *
12085  * The port argument is a value with encoded cport,
12086  * pmport and address qualifier, in the same manner as a scsi target number.
12087  * SCSI_TO_SATA_CPORT macro extracts cport number,
12088  * SCSI_TO_SATA_PMPORT extracts pmport number and
12089  * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag.
12090  *
12091  * For now, support is for cports only - no port multiplier device ports.
12092  */
12093 
12094 static void
12095 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port,
12096     devctl_ap_state_t *ap_state)
12097 {
12098 	uint16_t	cport;
12099 	int		port_state;
12100 	sata_drive_info_t *sdinfo;
12101 
12102 	/* Cport only */
12103 	cport = SCSI_TO_SATA_CPORT(port);
12104 
12105 	port_state = SATA_CPORT_STATE(sata_hba_inst, cport);
12106 	if (port_state & SATA_PSTATE_SHUTDOWN ||
12107 	    port_state & SATA_PSTATE_FAILED) {
12108 		ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
12109 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12110 		if (port_state & SATA_PSTATE_FAILED)
12111 			ap_state->ap_condition = AP_COND_FAILED;
12112 		else
12113 			ap_state->ap_condition = AP_COND_UNKNOWN;
12114 
12115 		return;
12116 	}
12117 
12118 	/* Need to check pmult device port here as well, when supported */
12119 
12120 	/* Port is enabled and ready */
12121 
12122 	switch (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport)) {
12123 	case SATA_DTYPE_NONE:
12124 	{
12125 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12126 		ap_state->ap_condition = AP_COND_OK;
12127 		/* No device attached */
12128 		ap_state->ap_rstate = AP_RSTATE_EMPTY;
12129 		break;
12130 	}
12131 	case SATA_DTYPE_UNKNOWN:
12132 	case SATA_DTYPE_ATAPINONCD:
12133 	case SATA_DTYPE_PMULT:	/* Until PMult is supported */
12134 	case SATA_DTYPE_ATADISK:
12135 	case SATA_DTYPE_ATAPICD:
12136 	{
12137 		dev_info_t *tdip = NULL;
12138 		dev_info_t *dip = NULL;
12139 		int circ;
12140 
12141 		dip = SATA_DIP(sata_hba_inst);
12142 		tdip = sata_get_target_dip(dip, port);
12143 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
12144 		if (tdip != NULL) {
12145 			ndi_devi_enter(dip, &circ);
12146 			mutex_enter(&(DEVI(tdip)->devi_lock));
12147 			if (DEVI_IS_DEVICE_REMOVED(tdip)) {
12148 				/*
12149 				 * There could be the case where previously
12150 				 * configured and opened device was removed
12151 				 * and unknown device was plugged.
12152 				 * In such case we want to show a device, and
12153 				 * its configured or unconfigured state but
12154 				 * indicate unusable condition untill the
12155 				 * old target node is released and removed.
12156 				 */
12157 				ap_state->ap_condition = AP_COND_UNUSABLE;
12158 			} else {
12159 				mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst,
12160 				    cport));
12161 				sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12162 				    cport);
12163 				if (sdinfo != NULL) {
12164 					if ((sdinfo->satadrv_state &
12165 					    SATA_DSTATE_FAILED) != 0)
12166 						ap_state->ap_condition =
12167 						    AP_COND_FAILED;
12168 					else
12169 						ap_state->ap_condition =
12170 						    AP_COND_OK;
12171 				} else {
12172 					ap_state->ap_condition =
12173 					    AP_COND_UNKNOWN;
12174 				}
12175 				mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst,
12176 				    cport));
12177 			}
12178 			if ((DEVI_IS_DEVICE_OFFLINE(tdip)) ||
12179 			    (DEVI_IS_DEVICE_DOWN(tdip))) {
12180 				ap_state->ap_ostate =
12181 				    AP_OSTATE_UNCONFIGURED;
12182 			} else {
12183 				ap_state->ap_ostate =
12184 				    AP_OSTATE_CONFIGURED;
12185 			}
12186 			mutex_exit(&(DEVI(tdip)->devi_lock));
12187 			ndi_devi_exit(dip, circ);
12188 		} else {
12189 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12190 			ap_state->ap_condition = AP_COND_UNKNOWN;
12191 		}
12192 		break;
12193 	}
12194 	default:
12195 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
12196 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12197 		ap_state->ap_condition = AP_COND_UNKNOWN;
12198 		/*
12199 		 * This is actually internal error condition (non fatal),
12200 		 * because we have already checked all defined device types.
12201 		 */
12202 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12203 		    "sata_cfgadm_state: Internal error: "
12204 		    "unknown device type"));
12205 		break;
12206 	}
12207 }
12208 
12209 
12210 /*
12211  * Process ioctl get device path request.
12212  *
12213  * NOTE: Port multiplier code is not completed nor tested.
12214  */
12215 static int
12216 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst,
12217     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12218 {
12219 	char path[MAXPATHLEN];
12220 	uint32_t size;
12221 	dev_info_t *tdip;
12222 
12223 	(void) strcpy(path, "/devices");
12224 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
12225 	    &sata_device->satadev_addr)) == NULL) {
12226 		/*
12227 		 * No such device. If this is a request for a size, do not
12228 		 * return EINVAL for non-existing target, because cfgadm
12229 		 * will then indicate a meaningless ioctl failure.
12230 		 * If this is a request for a path, indicate invalid
12231 		 * argument.
12232 		 */
12233 		if (ioc->get_size == 0)
12234 			return (EINVAL);
12235 	} else {
12236 		(void) ddi_pathname(tdip, path + strlen(path));
12237 	}
12238 	size = strlen(path) + 1;
12239 
12240 	if (ioc->get_size != 0) {
12241 		if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz,
12242 		    mode) != 0)
12243 			return (EFAULT);
12244 	} else {
12245 		if (ioc->bufsiz != size)
12246 			return (EINVAL);
12247 
12248 		else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz,
12249 		    mode) != 0)
12250 			return (EFAULT);
12251 	}
12252 	return (0);
12253 }
12254 
12255 /*
12256  * Process ioctl get attachment point type request.
12257  *
12258  * NOTE: Port multiplier code is not completed nor tested.
12259  */
12260 static	int
12261 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst,
12262     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12263 {
12264 	uint32_t	type_len;
12265 	const char	*ap_type;
12266 	int		dev_type;
12267 
12268 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12269 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst,
12270 		    sata_device->satadev_addr.cport);
12271 	else /* pmport */
12272 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12273 		    sata_device->satadev_addr.cport,
12274 		    sata_device->satadev_addr.pmport);
12275 
12276 	switch (dev_type) {
12277 	case SATA_DTYPE_NONE:
12278 		ap_type = "port";
12279 		break;
12280 
12281 	case SATA_DTYPE_ATADISK:
12282 		ap_type = "disk";
12283 		break;
12284 
12285 	case SATA_DTYPE_ATAPICD:
12286 		ap_type = "cd/dvd";
12287 		break;
12288 
12289 	case SATA_DTYPE_PMULT:
12290 		ap_type = "pmult";
12291 		break;
12292 
12293 	case SATA_DTYPE_UNKNOWN:
12294 		ap_type = "unknown";
12295 		break;
12296 
12297 	default:
12298 		ap_type = "unsupported";
12299 		break;
12300 
12301 	} /* end of dev_type switch */
12302 
12303 	type_len = strlen(ap_type) + 1;
12304 
12305 	if (ioc->get_size) {
12306 		if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz,
12307 		    mode) != 0)
12308 			return (EFAULT);
12309 	} else {
12310 		if (ioc->bufsiz != type_len)
12311 			return (EINVAL);
12312 
12313 		if (ddi_copyout((void *)ap_type, ioc->buf,
12314 		    ioc->bufsiz, mode) != 0)
12315 			return (EFAULT);
12316 	}
12317 	return (0);
12318 
12319 }
12320 
12321 /*
12322  * Process ioctl get device model info request.
12323  * This operation should return to cfgadm the device model
12324  * information string
12325  *
12326  * NOTE: Port multiplier code is not completed nor tested.
12327  */
12328 static	int
12329 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst,
12330     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12331 {
12332 	sata_drive_info_t *sdinfo;
12333 	uint32_t info_len;
12334 	char ap_info[SATA_ID_MODEL_LEN + 1];
12335 
12336 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12337 	    sata_device->satadev_addr.cport)->cport_mutex);
12338 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12339 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12340 		    sata_device->satadev_addr.cport);
12341 	else /* port multiplier */
12342 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12343 		    sata_device->satadev_addr.cport,
12344 		    sata_device->satadev_addr.pmport);
12345 	if (sdinfo == NULL) {
12346 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12347 		    sata_device->satadev_addr.cport)->cport_mutex);
12348 		return (EINVAL);
12349 	}
12350 
12351 #ifdef	_LITTLE_ENDIAN
12352 	swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
12353 #else	/* _LITTLE_ENDIAN */
12354 	bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
12355 #endif	/* _LITTLE_ENDIAN */
12356 
12357 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12358 	    sata_device->satadev_addr.cport)->cport_mutex);
12359 
12360 	ap_info[SATA_ID_MODEL_LEN] = '\0';
12361 
12362 	info_len = strlen(ap_info) + 1;
12363 
12364 	if (ioc->get_size) {
12365 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12366 		    mode) != 0)
12367 			return (EFAULT);
12368 	} else {
12369 		if (ioc->bufsiz < info_len)
12370 			return (EINVAL);
12371 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12372 		    mode) != 0)
12373 			return (EFAULT);
12374 	}
12375 	return (0);
12376 }
12377 
12378 
12379 /*
12380  * Process ioctl get device firmware revision info request.
12381  * This operation should return to cfgadm the device firmware revision
12382  * information string
12383  *
12384  * NOTE: Port multiplier code is not completed nor tested.
12385  */
12386 static	int
12387 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst,
12388     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12389 {
12390 	sata_drive_info_t *sdinfo;
12391 	uint32_t info_len;
12392 	char ap_info[SATA_ID_FW_LEN + 1];
12393 
12394 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12395 	    sata_device->satadev_addr.cport)->cport_mutex);
12396 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12397 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12398 		    sata_device->satadev_addr.cport);
12399 	else /* port multiplier */
12400 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12401 		    sata_device->satadev_addr.cport,
12402 		    sata_device->satadev_addr.pmport);
12403 	if (sdinfo == NULL) {
12404 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12405 		    sata_device->satadev_addr.cport)->cport_mutex);
12406 		return (EINVAL);
12407 	}
12408 
12409 #ifdef	_LITTLE_ENDIAN
12410 	swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
12411 #else	/* _LITTLE_ENDIAN */
12412 	bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
12413 #endif	/* _LITTLE_ENDIAN */
12414 
12415 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12416 	    sata_device->satadev_addr.cport)->cport_mutex);
12417 
12418 	ap_info[SATA_ID_FW_LEN] = '\0';
12419 
12420 	info_len = strlen(ap_info) + 1;
12421 
12422 	if (ioc->get_size) {
12423 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12424 		    mode) != 0)
12425 			return (EFAULT);
12426 	} else {
12427 		if (ioc->bufsiz < info_len)
12428 			return (EINVAL);
12429 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12430 		    mode) != 0)
12431 			return (EFAULT);
12432 	}
12433 	return (0);
12434 }
12435 
12436 
12437 /*
12438  * Process ioctl get device serial number info request.
12439  * This operation should return to cfgadm the device serial number string.
12440  *
12441  * NOTE: Port multiplier code is not completed nor tested.
12442  */
12443 static	int
12444 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst,
12445     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12446 {
12447 	sata_drive_info_t *sdinfo;
12448 	uint32_t info_len;
12449 	char ap_info[SATA_ID_SERIAL_LEN + 1];
12450 
12451 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12452 	    sata_device->satadev_addr.cport)->cport_mutex);
12453 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12454 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12455 		    sata_device->satadev_addr.cport);
12456 	else /* port multiplier */
12457 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12458 		    sata_device->satadev_addr.cport,
12459 		    sata_device->satadev_addr.pmport);
12460 	if (sdinfo == NULL) {
12461 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12462 		    sata_device->satadev_addr.cport)->cport_mutex);
12463 		return (EINVAL);
12464 	}
12465 
12466 #ifdef	_LITTLE_ENDIAN
12467 	swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
12468 #else	/* _LITTLE_ENDIAN */
12469 	bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
12470 #endif	/* _LITTLE_ENDIAN */
12471 
12472 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12473 	    sata_device->satadev_addr.cport)->cport_mutex);
12474 
12475 	ap_info[SATA_ID_SERIAL_LEN] = '\0';
12476 
12477 	info_len = strlen(ap_info) + 1;
12478 
12479 	if (ioc->get_size) {
12480 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12481 		    mode) != 0)
12482 			return (EFAULT);
12483 	} else {
12484 		if (ioc->bufsiz < info_len)
12485 			return (EINVAL);
12486 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12487 		    mode) != 0)
12488 			return (EFAULT);
12489 	}
12490 	return (0);
12491 }
12492 
12493 
12494 /*
12495  * Preset scsi extended sense data (to NO SENSE)
12496  * First 18 bytes of the sense data are preset to current valid sense
12497  * with a key NO SENSE data.
12498  *
12499  * Returns void
12500  */
12501 static void
12502 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense)
12503 {
12504 	sense->es_valid = 1;		/* Valid sense */
12505 	sense->es_class = CLASS_EXTENDED_SENSE;	/* 0x70 - current err */
12506 	sense->es_key = KEY_NO_SENSE;
12507 	sense->es_info_1 = 0;
12508 	sense->es_info_2 = 0;
12509 	sense->es_info_3 = 0;
12510 	sense->es_info_4 = 0;
12511 	sense->es_add_len = 10;	/* Additional length - replace with a def */
12512 	sense->es_cmd_info[0] = 0;
12513 	sense->es_cmd_info[1] = 0;
12514 	sense->es_cmd_info[2] = 0;
12515 	sense->es_cmd_info[3] = 0;
12516 	sense->es_add_code = 0;
12517 	sense->es_qual_code = 0;
12518 }
12519 
12520 /*
12521  * Register a legacy cmdk-style devid for the target (disk) device.
12522  *
12523  * Note: This function is called only when the HBA devinfo node has the
12524  * property "use-cmdk-devid-format" set. This property indicates that
12525  * devid compatible with old cmdk (target) driver is to be generated
12526  * for any target device attached to this controller. This will take
12527  * precedence over the devid generated by sd (target) driver.
12528  * This function is derived from cmdk_devid_setup() function in cmdk.c.
12529  */
12530 static void
12531 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo)
12532 {
12533 	char	*hwid;
12534 	int	modlen;
12535 	int	serlen;
12536 	int	rval;
12537 	ddi_devid_t	devid;
12538 
12539 	/*
12540 	 * device ID is a concatanation of model number, "=", serial number.
12541 	 */
12542 	hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP);
12543 	bcopy(&sdinfo->satadrv_id.ai_model, hwid,
12544 	    sizeof (sdinfo->satadrv_id.ai_model));
12545 	swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model));
12546 	modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model));
12547 	if (modlen == 0)
12548 		goto err;
12549 	hwid[modlen++] = '=';
12550 	bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen],
12551 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12552 	swab(&hwid[modlen], &hwid[modlen],
12553 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12554 	serlen = sata_check_modser(&hwid[modlen],
12555 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12556 	if (serlen == 0)
12557 		goto err;
12558 	hwid[modlen + serlen] = 0; /* terminate the hwid string */
12559 
12560 	/* initialize/register devid */
12561 	if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL,
12562 	    (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS)
12563 		rval = ddi_devid_register(dip, devid);
12564 
12565 	if (rval != DDI_SUCCESS)
12566 		cmn_err(CE_WARN, "sata: failed to create devid for the disk"
12567 		    " on port %d", sdinfo->satadrv_addr.cport);
12568 err:
12569 	kmem_free(hwid, LEGACY_HWID_LEN);
12570 }
12571 
12572 /*
12573  * valid model/serial string must contain a non-zero non-space characters.
12574  * trim trailing spaces/NULLs.
12575  */
12576 static int
12577 sata_check_modser(char *buf, int buf_len)
12578 {
12579 	boolean_t ret;
12580 	char *s;
12581 	int i;
12582 	int tb;
12583 	char ch;
12584 
12585 	ret = B_FALSE;
12586 	s = buf;
12587 	for (i = 0; i < buf_len; i++) {
12588 		ch = *s++;
12589 		if (ch != ' ' && ch != '\0')
12590 			tb = i + 1;
12591 		if (ch != ' ' && ch != '\0' && ch != '0')
12592 			ret = B_TRUE;
12593 	}
12594 
12595 	if (ret == B_FALSE)
12596 		return (0); /* invalid string */
12597 
12598 	return (tb); /* return length */
12599 }
12600 
12601 /*
12602  * sata_set_drive_features function compares current device features setting
12603  * with the saved device features settings and, if there is a difference,
12604  * it restores device features setting to the previously saved state.
12605  * It also arbitrarily tries to select the highest supported DMA mode.
12606  * Device Identify or Identify Packet Device data has to be current.
12607  * At the moment read ahead and write cache are considered for all devices.
12608  * For atapi devices, Removable Media Status Notification is set in addition
12609  * to common features.
12610  *
12611  * This function cannot be called in the interrupt context (it may sleep).
12612  *
12613  * The input argument sdinfo should point to the drive info structure
12614  * to be updated after features are set. Note, that only
12615  * device (packet) identify data is updated, not the flags indicating the
12616  * supported features.
12617  *
12618  * Returns TRUE if successful or there was nothing to do. Device Identify data
12619  * in the drive info structure pointed to by the sdinfo argumens is updated
12620  * even when no features were set or changed.
12621  *
12622  * Returns FALSE if device features could not be set.
12623  *
12624  * Note: This function may fail the port, making it inaccessible.
12625  * In such case the explicit port disconnect/connect or physical device
12626  * detach/attach is required to re-evaluate port state again.
12627  */
12628 
12629 static int
12630 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst,
12631     sata_drive_info_t *sdinfo, int restore)
12632 {
12633 	int rval = SATA_SUCCESS;
12634 	sata_drive_info_t new_sdinfo;
12635 	char *finfo = "sata_set_drive_features: cannot";
12636 	char *finfox;
12637 	int cache_op;
12638 
12639 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
12640 	new_sdinfo.satadrv_addr = sdinfo->satadrv_addr;
12641 	new_sdinfo.satadrv_type = sdinfo->satadrv_type;
12642 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
12643 		/*
12644 		 * Cannot get device identification - retry later
12645 		 */
12646 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12647 		    "%s fetch device identify data\n", finfo);
12648 		return (SATA_FAILURE);
12649 	}
12650 	finfox = (restore != 0) ? " restore device features" :
12651 	    " initialize device features\n";
12652 
12653 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12654 		/* Arbitrarily set UDMA mode */
12655 		if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
12656 		    SATA_SUCCESS) {
12657 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12658 			    "%s set UDMA mode\n", finfo));
12659 			return (SATA_FAILURE);
12660 		}
12661 	} else { /* Assume SATA ATAPI CD/DVD */
12662 		/*  Set Removable Media Status Notification, if necessary */
12663 		if ((new_sdinfo.satadrv_id.ai_cmdset83 &
12664 		    SATA_RM_STATUS_NOTIFIC) != 0 && restore != 0) {
12665 			if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) &&
12666 			    (!(new_sdinfo.satadrv_id.ai_features86 &
12667 			    SATA_RM_STATUS_NOTIFIC))) ||
12668 			    ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) &&
12669 			    (new_sdinfo.satadrv_id.ai_features86 &
12670 			    SATA_RM_STATUS_NOTIFIC))) {
12671 				/* Current setting does not match saved one */
12672 				if (sata_set_rmsn(sata_hba_inst, sdinfo,
12673 				    sdinfo->satadrv_settings &
12674 				    SATA_DEV_RMSN) != SATA_SUCCESS)
12675 					rval = SATA_FAILURE;
12676 			}
12677 		}
12678 		/*
12679 		 * We have to set Multiword DMA or UDMA, if it is supported, as
12680 		 * we want to use DMA transfer mode whenever possible.
12681 		 * Some devices require explicit setting of the DMA mode.
12682 		 */
12683 		if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) {
12684 			/* Set highest supported DMA mode */
12685 			if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
12686 			    SATA_SUCCESS) {
12687 				SATA_LOG_D((sata_hba_inst, CE_WARN,
12688 				    "%s set UDMA mode\n", finfo));
12689 				rval = SATA_FAILURE;
12690 			}
12691 		}
12692 	}
12693 
12694 	if (!(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) &&
12695 	    !(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) {
12696 		/* None of the features is supported - do nothing */
12697 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12698 		    "settable features not supported\n", NULL);
12699 		goto update_sdinfo;
12700 	}
12701 
12702 	if (((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) &&
12703 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) &&
12704 	    ((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) &&
12705 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
12706 		/* Nothing to do */
12707 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12708 		    "no device features to set\n", NULL);
12709 		goto update_sdinfo;
12710 	}
12711 
12712 	if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) &&
12713 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD))) {
12714 		if (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) {
12715 			/* Enable read ahead / read cache */
12716 			cache_op = SATAC_SF_ENABLE_READ_AHEAD;
12717 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12718 			    "enabling read cache\n", NULL);
12719 		} else {
12720 			/* Disable read ahead  / read cache */
12721 			cache_op = SATAC_SF_DISABLE_READ_AHEAD;
12722 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12723 			    "disabling read cache\n", NULL);
12724 		}
12725 
12726 		/* Try to set read cache mode */
12727 		if (sata_set_cache_mode(sata_hba_inst, &new_sdinfo,
12728 		    cache_op) != SATA_SUCCESS) {
12729 			/* Pkt execution failed */
12730 			rval = SATA_FAILURE;
12731 		}
12732 	}
12733 
12734 	if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) &&
12735 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
12736 		if (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) {
12737 			/* Enable write cache */
12738 			cache_op = SATAC_SF_ENABLE_WRITE_CACHE;
12739 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12740 			    "enabling write cache\n", NULL);
12741 		} else {
12742 			/* Disable write cache */
12743 			cache_op = SATAC_SF_DISABLE_WRITE_CACHE;
12744 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12745 			    "disabling write cache\n", NULL);
12746 		}
12747 		/* Try to set write cache mode */
12748 		if (sata_set_cache_mode(sata_hba_inst, &new_sdinfo,
12749 		    cache_op) != SATA_SUCCESS) {
12750 			/* Pkt execution failed */
12751 			rval = SATA_FAILURE;
12752 		}
12753 	}
12754 
12755 	if (rval == SATA_FAILURE)
12756 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12757 		    "%s %s", finfo, finfox));
12758 update_sdinfo:
12759 	/*
12760 	 * We need to fetch Device Identify data again
12761 	 */
12762 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
12763 		/*
12764 		 * Cannot get device identification - retry later
12765 		 */
12766 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12767 		    "%s re-fetch device identify data\n", finfo));
12768 		rval = SATA_FAILURE;
12769 	}
12770 	/* Copy device sata info. */
12771 	sdinfo->satadrv_id = new_sdinfo.satadrv_id;
12772 
12773 	return (rval);
12774 }
12775 
12776 
12777 /*
12778  *
12779  * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if
12780  * unable to determine.
12781  *
12782  * Cannot be called in an interrupt context.
12783  *
12784  * Called by sata_build_lsense_page_2f()
12785  */
12786 
12787 static int
12788 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst,
12789     sata_drive_info_t *sdinfo)
12790 {
12791 	sata_pkt_t *spkt;
12792 	sata_cmd_t *scmd;
12793 	sata_pkt_txlate_t *spx;
12794 	int rval;
12795 
12796 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12797 	spx->txlt_sata_hba_inst = sata_hba_inst;
12798 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12799 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12800 	if (spkt == NULL) {
12801 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12802 		return (-1);
12803 	}
12804 	/* address is needed now */
12805 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12806 
12807 
12808 	/* Fill sata_pkt */
12809 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12810 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12811 	/* Synchronous mode, no callback */
12812 	spkt->satapkt_comp = NULL;
12813 	/* Timeout 30s */
12814 	spkt->satapkt_time = sata_default_pkt_time;
12815 
12816 	scmd = &spkt->satapkt_cmd;
12817 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
12818 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
12819 
12820 	/* Set up which registers need to be returned */
12821 	scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE;
12822 	scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE;
12823 
12824 	/* Build SMART_RETURN_STATUS cmd in the sata_pkt */
12825 	scmd->satacmd_addr_type = 0;		/* N/A */
12826 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
12827 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
12828 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
12829 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
12830 	scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS;
12831 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12832 	scmd->satacmd_cmd_reg = SATAC_SMART;
12833 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12834 	    sdinfo->satadrv_addr.cport)));
12835 
12836 
12837 	/* Send pkt to SATA HBA driver */
12838 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12839 	    SATA_TRAN_ACCEPTED ||
12840 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12841 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12842 		    sdinfo->satadrv_addr.cport)));
12843 		/*
12844 		 * Whoops, no SMART RETURN STATUS
12845 		 */
12846 		rval = -1;
12847 	} else {
12848 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12849 		    sdinfo->satadrv_addr.cport)));
12850 		if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
12851 			rval = -1;
12852 			goto fail;
12853 		}
12854 		if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
12855 			rval = -1;
12856 			goto fail;
12857 		}
12858 		if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) &&
12859 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2))
12860 			rval = 0;
12861 		else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) &&
12862 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4))
12863 			rval = 1;
12864 		else {
12865 			rval = -1;
12866 			goto fail;
12867 		}
12868 	}
12869 fail:
12870 	/* Free allocated resources */
12871 	sata_pkt_free(spx);
12872 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12873 
12874 	return (rval);
12875 }
12876 
12877 /*
12878  *
12879  * Returns 0 if succeeded, -1 otherwise
12880  *
12881  * Cannot be called in an interrupt context.
12882  *
12883  */
12884 static int
12885 sata_fetch_smart_data(
12886 	sata_hba_inst_t *sata_hba_inst,
12887 	sata_drive_info_t *sdinfo,
12888 	struct smart_data *smart_data)
12889 {
12890 	sata_pkt_t *spkt;
12891 	sata_cmd_t *scmd;
12892 	sata_pkt_txlate_t *spx;
12893 	int rval;
12894 
12895 #if ! defined(lint)
12896 	ASSERT(sizeof (struct smart_data) == 512);
12897 #endif
12898 
12899 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12900 	spx->txlt_sata_hba_inst = sata_hba_inst;
12901 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12902 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12903 	if (spkt == NULL) {
12904 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12905 		return (-1);
12906 	}
12907 	/* address is needed now */
12908 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12909 
12910 
12911 	/* Fill sata_pkt */
12912 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12913 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12914 	/* Synchronous mode, no callback */
12915 	spkt->satapkt_comp = NULL;
12916 	/* Timeout 30s */
12917 	spkt->satapkt_time = sata_default_pkt_time;
12918 
12919 	scmd = &spkt->satapkt_cmd;
12920 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
12921 
12922 	/*
12923 	 * Allocate buffer for SMART data
12924 	 */
12925 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
12926 	    sizeof (struct smart_data));
12927 	if (scmd->satacmd_bp == NULL) {
12928 		sata_pkt_free(spx);
12929 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12930 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12931 		    "sata_fetch_smart_data: "
12932 		    "cannot allocate buffer"));
12933 		return (-1);
12934 	}
12935 
12936 
12937 	/* Build SMART_READ_DATA cmd in the sata_pkt */
12938 	scmd->satacmd_addr_type = 0;		/* N/A */
12939 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
12940 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
12941 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
12942 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
12943 	scmd->satacmd_features_reg = SATA_SMART_READ_DATA;
12944 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12945 	scmd->satacmd_cmd_reg = SATAC_SMART;
12946 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12947 	    sdinfo->satadrv_addr.cport)));
12948 
12949 	/* Send pkt to SATA HBA driver */
12950 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12951 	    SATA_TRAN_ACCEPTED ||
12952 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12953 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12954 		    sdinfo->satadrv_addr.cport)));
12955 		/*
12956 		 * Whoops, no SMART DATA available
12957 		 */
12958 		rval = -1;
12959 		goto fail;
12960 	} else {
12961 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12962 		    sdinfo->satadrv_addr.cport)));
12963 		if (spx->txlt_buf_dma_handle != NULL) {
12964 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
12965 			    DDI_DMA_SYNC_FORKERNEL);
12966 			ASSERT(rval == DDI_SUCCESS);
12967 		}
12968 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data,
12969 		    sizeof (struct smart_data));
12970 	}
12971 
12972 fail:
12973 	/* Free allocated resources */
12974 	sata_free_local_buffer(spx);
12975 	sata_pkt_free(spx);
12976 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12977 
12978 	return (rval);
12979 }
12980 
12981 /*
12982  * Used by LOG SENSE page 0x10
12983  *
12984  * return 0 for success, -1 otherwise
12985  *
12986  */
12987 static int
12988 sata_ext_smart_selftest_read_log(
12989 	sata_hba_inst_t *sata_hba_inst,
12990 	sata_drive_info_t *sdinfo,
12991 	struct smart_ext_selftest_log *ext_selftest_log,
12992 	uint16_t block_num)
12993 {
12994 	sata_pkt_txlate_t *spx;
12995 	sata_pkt_t *spkt;
12996 	sata_cmd_t *scmd;
12997 	int rval;
12998 
12999 #if ! defined(lint)
13000 	ASSERT(sizeof (struct smart_ext_selftest_log) == 512);
13001 #endif
13002 
13003 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13004 	spx->txlt_sata_hba_inst = sata_hba_inst;
13005 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13006 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13007 	if (spkt == NULL) {
13008 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13009 		return (-1);
13010 	}
13011 	/* address is needed now */
13012 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13013 
13014 
13015 	/* Fill sata_pkt */
13016 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13017 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13018 	/* Synchronous mode, no callback */
13019 	spkt->satapkt_comp = NULL;
13020 	/* Timeout 30s */
13021 	spkt->satapkt_time = sata_default_pkt_time;
13022 
13023 	scmd = &spkt->satapkt_cmd;
13024 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13025 
13026 	/*
13027 	 * Allocate buffer for SMART extended self-test log
13028 	 */
13029 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13030 	    sizeof (struct smart_ext_selftest_log));
13031 	if (scmd->satacmd_bp == NULL) {
13032 		sata_pkt_free(spx);
13033 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13034 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13035 		    "sata_ext_smart_selftest_log: "
13036 		    "cannot allocate buffer"));
13037 		return (-1);
13038 	}
13039 
13040 	/* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */
13041 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
13042 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of selftest log */
13043 	scmd->satacmd_sec_count_msb = 0;	/* One sector of selftest log */
13044 	scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE;
13045 	scmd->satacmd_lba_low_msb = 0;
13046 	scmd->satacmd_lba_mid_lsb = block_num & 0xff;
13047 	scmd->satacmd_lba_mid_msb = block_num >> 8;
13048 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13049 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
13050 
13051 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13052 	    sdinfo->satadrv_addr.cport)));
13053 
13054 	/* Send pkt to SATA HBA driver */
13055 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13056 	    SATA_TRAN_ACCEPTED ||
13057 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13058 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13059 		    sdinfo->satadrv_addr.cport)));
13060 
13061 		/*
13062 		 * Whoops, no SMART selftest log info available
13063 		 */
13064 		rval = -1;
13065 		goto fail;
13066 	} else {
13067 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13068 		    sdinfo->satadrv_addr.cport)));
13069 
13070 		if (spx->txlt_buf_dma_handle != NULL) {
13071 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13072 			    DDI_DMA_SYNC_FORKERNEL);
13073 			ASSERT(rval == DDI_SUCCESS);
13074 		}
13075 		bcopy(scmd->satacmd_bp->b_un.b_addr,
13076 		    (uint8_t *)ext_selftest_log,
13077 		    sizeof (struct smart_ext_selftest_log));
13078 		rval = 0;
13079 	}
13080 
13081 fail:
13082 	/* Free allocated resources */
13083 	sata_free_local_buffer(spx);
13084 	sata_pkt_free(spx);
13085 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13086 
13087 	return (rval);
13088 }
13089 
13090 /*
13091  * Returns 0 for success, -1 otherwise
13092  *
13093  * SMART self-test log data is returned in buffer pointed to by selftest_log
13094  */
13095 static int
13096 sata_smart_selftest_log(
13097 	sata_hba_inst_t *sata_hba_inst,
13098 	sata_drive_info_t *sdinfo,
13099 	struct smart_selftest_log *selftest_log)
13100 {
13101 	sata_pkt_t *spkt;
13102 	sata_cmd_t *scmd;
13103 	sata_pkt_txlate_t *spx;
13104 	int rval;
13105 
13106 #if ! defined(lint)
13107 	ASSERT(sizeof (struct smart_selftest_log) == 512);
13108 #endif
13109 
13110 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13111 	spx->txlt_sata_hba_inst = sata_hba_inst;
13112 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13113 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13114 	if (spkt == NULL) {
13115 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13116 		return (-1);
13117 	}
13118 	/* address is needed now */
13119 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13120 
13121 
13122 	/* Fill sata_pkt */
13123 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13124 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13125 	/* Synchronous mode, no callback */
13126 	spkt->satapkt_comp = NULL;
13127 	/* Timeout 30s */
13128 	spkt->satapkt_time = sata_default_pkt_time;
13129 
13130 	scmd = &spkt->satapkt_cmd;
13131 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13132 
13133 	/*
13134 	 * Allocate buffer for SMART SELFTEST LOG
13135 	 */
13136 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13137 	    sizeof (struct smart_selftest_log));
13138 	if (scmd->satacmd_bp == NULL) {
13139 		sata_pkt_free(spx);
13140 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13141 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13142 		    "sata_smart_selftest_log: "
13143 		    "cannot allocate buffer"));
13144 		return (-1);
13145 	}
13146 
13147 	/* Build SMART_READ_LOG cmd in the sata_pkt */
13148 	scmd->satacmd_addr_type = 0;		/* N/A */
13149 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of SMART log */
13150 	scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE;
13151 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13152 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13153 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
13154 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13155 	scmd->satacmd_cmd_reg = SATAC_SMART;
13156 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13157 	    sdinfo->satadrv_addr.cport)));
13158 
13159 	/* Send pkt to SATA HBA driver */
13160 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13161 	    SATA_TRAN_ACCEPTED ||
13162 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13163 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13164 		    sdinfo->satadrv_addr.cport)));
13165 		/*
13166 		 * Whoops, no SMART DATA available
13167 		 */
13168 		rval = -1;
13169 		goto fail;
13170 	} else {
13171 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13172 		    sdinfo->satadrv_addr.cport)));
13173 		if (spx->txlt_buf_dma_handle != NULL) {
13174 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13175 			    DDI_DMA_SYNC_FORKERNEL);
13176 			ASSERT(rval == DDI_SUCCESS);
13177 		}
13178 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log,
13179 		    sizeof (struct smart_selftest_log));
13180 		rval = 0;
13181 	}
13182 
13183 fail:
13184 	/* Free allocated resources */
13185 	sata_free_local_buffer(spx);
13186 	sata_pkt_free(spx);
13187 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13188 
13189 	return (rval);
13190 }
13191 
13192 
13193 /*
13194  * Returns 0 for success, -1 otherwise
13195  *
13196  * SMART READ LOG data is returned in buffer pointed to by smart_log
13197  */
13198 static int
13199 sata_smart_read_log(
13200 	sata_hba_inst_t *sata_hba_inst,
13201 	sata_drive_info_t *sdinfo,
13202 	uint8_t *smart_log,		/* where the data should be returned */
13203 	uint8_t which_log,		/* which log should be returned */
13204 	uint8_t log_size)		/* # of 512 bytes in log */
13205 {
13206 	sata_pkt_t *spkt;
13207 	sata_cmd_t *scmd;
13208 	sata_pkt_txlate_t *spx;
13209 	int rval;
13210 
13211 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13212 	spx->txlt_sata_hba_inst = sata_hba_inst;
13213 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13214 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13215 	if (spkt == NULL) {
13216 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13217 		return (-1);
13218 	}
13219 	/* address is needed now */
13220 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13221 
13222 
13223 	/* Fill sata_pkt */
13224 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13225 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13226 	/* Synchronous mode, no callback */
13227 	spkt->satapkt_comp = NULL;
13228 	/* Timeout 30s */
13229 	spkt->satapkt_time = sata_default_pkt_time;
13230 
13231 	scmd = &spkt->satapkt_cmd;
13232 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13233 
13234 	/*
13235 	 * Allocate buffer for SMART READ LOG
13236 	 */
13237 	scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512);
13238 	if (scmd->satacmd_bp == NULL) {
13239 		sata_pkt_free(spx);
13240 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13241 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13242 		    "sata_smart_read_log: " "cannot allocate buffer"));
13243 		return (-1);
13244 	}
13245 
13246 	/* Build SMART_READ_LOG cmd in the sata_pkt */
13247 	scmd->satacmd_addr_type = 0;		/* N/A */
13248 	scmd->satacmd_sec_count_lsb = log_size;	/* what the caller asked for */
13249 	scmd->satacmd_lba_low_lsb = which_log;	/* which log page */
13250 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13251 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13252 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
13253 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13254 	scmd->satacmd_cmd_reg = SATAC_SMART;
13255 
13256 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13257 	    sdinfo->satadrv_addr.cport)));
13258 
13259 	/* Send pkt to SATA HBA driver */
13260 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13261 	    SATA_TRAN_ACCEPTED ||
13262 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13263 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13264 		    sdinfo->satadrv_addr.cport)));
13265 
13266 		/*
13267 		 * Whoops, no SMART DATA available
13268 		 */
13269 		rval = -1;
13270 		goto fail;
13271 	} else {
13272 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13273 		    sdinfo->satadrv_addr.cport)));
13274 
13275 		if (spx->txlt_buf_dma_handle != NULL) {
13276 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13277 			    DDI_DMA_SYNC_FORKERNEL);
13278 			ASSERT(rval == DDI_SUCCESS);
13279 		}
13280 		bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512);
13281 		rval = 0;
13282 	}
13283 
13284 fail:
13285 	/* Free allocated resources */
13286 	sata_free_local_buffer(spx);
13287 	sata_pkt_free(spx);
13288 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13289 
13290 	return (rval);
13291 }
13292 
13293 /*
13294  * Used by LOG SENSE page 0x10
13295  *
13296  * return 0 for success, -1 otherwise
13297  *
13298  */
13299 static int
13300 sata_read_log_ext_directory(
13301 	sata_hba_inst_t *sata_hba_inst,
13302 	sata_drive_info_t *sdinfo,
13303 	struct read_log_ext_directory *logdir)
13304 {
13305 	sata_pkt_txlate_t *spx;
13306 	sata_pkt_t *spkt;
13307 	sata_cmd_t *scmd;
13308 	int rval;
13309 
13310 #if ! defined(lint)
13311 	ASSERT(sizeof (struct read_log_ext_directory) == 512);
13312 #endif
13313 
13314 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13315 	spx->txlt_sata_hba_inst = sata_hba_inst;
13316 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13317 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13318 	if (spkt == NULL) {
13319 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13320 		return (-1);
13321 	}
13322 
13323 	/* Fill sata_pkt */
13324 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13325 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13326 	/* Synchronous mode, no callback */
13327 	spkt->satapkt_comp = NULL;
13328 	/* Timeout 30s */
13329 	spkt->satapkt_time = sata_default_pkt_time;
13330 
13331 	scmd = &spkt->satapkt_cmd;
13332 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13333 
13334 	/*
13335 	 * Allocate buffer for SMART READ LOG EXTENDED command
13336 	 */
13337 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13338 	    sizeof (struct read_log_ext_directory));
13339 	if (scmd->satacmd_bp == NULL) {
13340 		sata_pkt_free(spx);
13341 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13342 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13343 		    "sata_read_log_ext_directory: "
13344 		    "cannot allocate buffer"));
13345 		return (-1);
13346 	}
13347 
13348 	/* Build READ LOG EXT w/ log directory cmd in the  sata_pkt */
13349 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
13350 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of directory */
13351 	scmd->satacmd_sec_count_msb = 0;	/* One sector of directory */
13352 	scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY;
13353 	scmd->satacmd_lba_low_msb = 0;
13354 	scmd->satacmd_lba_mid_lsb = 0;
13355 	scmd->satacmd_lba_mid_msb = 0;
13356 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13357 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
13358 
13359 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13360 	    sdinfo->satadrv_addr.cport)));
13361 
13362 	/* Send pkt to SATA HBA driver */
13363 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13364 	    SATA_TRAN_ACCEPTED ||
13365 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13366 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13367 		    sdinfo->satadrv_addr.cport)));
13368 		/*
13369 		 * Whoops, no SMART selftest log info available
13370 		 */
13371 		rval = -1;
13372 		goto fail;
13373 	} else {
13374 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13375 		    sdinfo->satadrv_addr.cport)));
13376 		if (spx->txlt_buf_dma_handle != NULL) {
13377 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13378 			    DDI_DMA_SYNC_FORKERNEL);
13379 			ASSERT(rval == DDI_SUCCESS);
13380 		}
13381 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir,
13382 		    sizeof (struct read_log_ext_directory));
13383 		rval = 0;
13384 	}
13385 
13386 fail:
13387 	/* Free allocated resources */
13388 	sata_free_local_buffer(spx);
13389 	sata_pkt_free(spx);
13390 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13391 
13392 	return (rval);
13393 }
13394 
13395 /*
13396  * Set up error retrieval sata command for NCQ command error data
13397  * recovery.
13398  *
13399  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
13400  * returns SATA_FAILURE otherwise.
13401  */
13402 static int
13403 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
13404 {
13405 #ifndef __lock_lint
13406 	_NOTE(ARGUNUSED(sdinfo))
13407 #endif
13408 
13409 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
13410 	sata_cmd_t *scmd;
13411 	struct buf *bp;
13412 
13413 	/* Operation modes are up to the caller */
13414 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13415 
13416 	/* Synchronous mode, no callback - may be changed by the caller */
13417 	spkt->satapkt_comp = NULL;
13418 	spkt->satapkt_time = sata_default_pkt_time;
13419 
13420 	scmd = &spkt->satapkt_cmd;
13421 	bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t));
13422 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
13423 
13424 	/*
13425 	 * Allocate dma_able buffer error data.
13426 	 * Buffer allocation will take care of buffer alignment and other DMA
13427 	 * attributes.
13428 	 */
13429 	bp = sata_alloc_local_buffer(spx,
13430 	    sizeof (struct sata_ncq_error_recovery_page));
13431 	if (bp == NULL)
13432 		return (SATA_FAILURE);
13433 
13434 	bp_mapin(bp); /* make data buffer accessible */
13435 	scmd->satacmd_bp = bp;
13436 
13437 	/*
13438 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
13439 	 * before accessing it. Handle is in usual place in translate struct.
13440 	 */
13441 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
13442 
13443 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
13444 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
13445 
13446 	return (SATA_SUCCESS);
13447 }
13448 
13449 /*
13450  * sata_xlate_errors() is used to translate (S)ATA error
13451  * information to SCSI information returned in the SCSI
13452  * packet.
13453  */
13454 static void
13455 sata_xlate_errors(sata_pkt_txlate_t *spx)
13456 {
13457 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
13458 	struct scsi_extended_sense *sense;
13459 
13460 	scsipkt->pkt_reason = CMD_INCOMPLETE;
13461 	*scsipkt->pkt_scbp = STATUS_CHECK;
13462 	sense = sata_arq_sense(spx);
13463 
13464 	switch (spx->txlt_sata_pkt->satapkt_reason) {
13465 	case SATA_PKT_PORT_ERROR:
13466 		/*
13467 		 * We have no device data. Assume no data transfered.
13468 		 */
13469 		sense->es_key = KEY_HARDWARE_ERROR;
13470 		break;
13471 
13472 	case SATA_PKT_DEV_ERROR:
13473 		if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
13474 		    SATA_STATUS_ERR) {
13475 			/*
13476 			 * determine dev error reason from error
13477 			 * reg content
13478 			 */
13479 			sata_decode_device_error(spx, sense);
13480 			break;
13481 		}
13482 		/* No extended sense key - no info available */
13483 		break;
13484 
13485 	case SATA_PKT_TIMEOUT:
13486 		scsipkt->pkt_reason = CMD_TIMEOUT;
13487 		scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET;
13488 		/* No extended sense key */
13489 		break;
13490 
13491 	case SATA_PKT_ABORTED:
13492 		scsipkt->pkt_reason = CMD_ABORTED;
13493 		scsipkt->pkt_statistics |= STAT_ABORTED;
13494 		/* No extended sense key */
13495 		break;
13496 
13497 	case SATA_PKT_RESET:
13498 		/*
13499 		 * pkt aborted either by an explicit reset request from
13500 		 * a host, or due to error recovery
13501 		 */
13502 		scsipkt->pkt_reason = CMD_RESET;
13503 		scsipkt->pkt_statistics |= STAT_DEV_RESET;
13504 		break;
13505 
13506 	default:
13507 		scsipkt->pkt_reason = CMD_TRAN_ERR;
13508 		break;
13509 	}
13510 }
13511 
13512 
13513 
13514 
13515 /*
13516  * Log sata message
13517  * dev pathname msg line preceeds the logged message.
13518  */
13519 
13520 static	void
13521 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...)
13522 {
13523 	char pathname[128];
13524 	dev_info_t *dip;
13525 	va_list ap;
13526 
13527 	mutex_enter(&sata_log_mutex);
13528 
13529 	va_start(ap, fmt);
13530 	(void) vsprintf(sata_log_buf, fmt, ap);
13531 	va_end(ap);
13532 
13533 	if (sata_hba_inst != NULL) {
13534 		dip = SATA_DIP(sata_hba_inst);
13535 		(void) ddi_pathname(dip, pathname);
13536 	} else {
13537 		pathname[0] = 0;
13538 	}
13539 	if (level == CE_CONT) {
13540 		if (sata_debug_flags == 0)
13541 			cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf);
13542 		else
13543 			cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf);
13544 	} else {
13545 		if (level != CE_NOTE) {
13546 			cmn_err(level, "%s:\n %s", pathname, sata_log_buf);
13547 		} else if (sata_msg) {
13548 			cmn_err(level, "%s:\n %s", pathname,
13549 			    sata_log_buf);
13550 		}
13551 	}
13552 
13553 	mutex_exit(&sata_log_mutex);
13554 }
13555 
13556 
13557 /* ******** Asynchronous HBA events handling & hotplugging support ******** */
13558 
13559 /*
13560  * Start or terminate the thread, depending on flag arg and current state
13561  */
13562 static void
13563 sata_event_thread_control(int startstop)
13564 {
13565 	static 	int sata_event_thread_terminating = 0;
13566 	static 	int sata_event_thread_starting = 0;
13567 	int i;
13568 
13569 	mutex_enter(&sata_event_mutex);
13570 
13571 	if (startstop == 0 && (sata_event_thread_starting == 1 ||
13572 	    sata_event_thread_terminating == 1)) {
13573 		mutex_exit(&sata_event_mutex);
13574 		return;
13575 	}
13576 	if (startstop == 1 && sata_event_thread_starting == 1) {
13577 		mutex_exit(&sata_event_mutex);
13578 		return;
13579 	}
13580 	if (startstop == 1 && sata_event_thread_terminating == 1) {
13581 		sata_event_thread_starting = 1;
13582 		/* wait til terminate operation completes */
13583 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
13584 		while (sata_event_thread_terminating == 1) {
13585 			if (i-- <= 0) {
13586 				sata_event_thread_starting = 0;
13587 				mutex_exit(&sata_event_mutex);
13588 #ifdef SATA_DEBUG
13589 				cmn_err(CE_WARN, "sata_event_thread_control: "
13590 				    "timeout waiting for thread to terminate");
13591 #endif
13592 				return;
13593 			}
13594 			mutex_exit(&sata_event_mutex);
13595 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
13596 			mutex_enter(&sata_event_mutex);
13597 		}
13598 	}
13599 	if (startstop == 1) {
13600 		if (sata_event_thread == NULL) {
13601 			sata_event_thread = thread_create(NULL, 0,
13602 			    (void (*)())sata_event_daemon,
13603 			    &sata_hba_list, 0, &p0, TS_RUN, minclsyspri);
13604 		}
13605 		sata_event_thread_starting = 0;
13606 		mutex_exit(&sata_event_mutex);
13607 		return;
13608 	}
13609 
13610 	/*
13611 	 * If we got here, thread may need to be terminated
13612 	 */
13613 	if (sata_event_thread != NULL) {
13614 		int i;
13615 		/* Signal event thread to go away */
13616 		sata_event_thread_terminating = 1;
13617 		sata_event_thread_terminate = 1;
13618 		cv_signal(&sata_event_cv);
13619 		/*
13620 		 * Wait til daemon terminates.
13621 		 */
13622 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
13623 		while (sata_event_thread_terminate == 1) {
13624 			mutex_exit(&sata_event_mutex);
13625 			if (i-- <= 0) {
13626 				/* Daemon did not go away !!! */
13627 #ifdef SATA_DEBUG
13628 				cmn_err(CE_WARN, "sata_event_thread_control: "
13629 				    "cannot terminate event daemon thread");
13630 #endif
13631 				mutex_enter(&sata_event_mutex);
13632 				break;
13633 			}
13634 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
13635 			mutex_enter(&sata_event_mutex);
13636 		}
13637 		sata_event_thread_terminating = 0;
13638 	}
13639 	ASSERT(sata_event_thread_terminating == 0);
13640 	ASSERT(sata_event_thread_starting == 0);
13641 	mutex_exit(&sata_event_mutex);
13642 }
13643 
13644 
13645 /*
13646  * SATA HBA event notification function.
13647  * Events reported by SATA HBA drivers per HBA instance relate to a change in
13648  * a port and/or device state or a controller itself.
13649  * Events for different addresses/addr types cannot be combined.
13650  * A warning message is generated for each event type.
13651  * Events are not processed by this function, so only the
13652  * event flag(s)is set for an affected entity and the event thread is
13653  * waken up. Event daemon thread processes all events.
13654  *
13655  * NOTE: Since more than one event may be reported at the same time, one
13656  * cannot determine a sequence of events when opposite event are reported, eg.
13657  * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing
13658  * is taking precedence over reported events, i.e. may cause ignoring some
13659  * events.
13660  */
13661 #define	SATA_EVENT_MAX_MSG_LENGTH	79
13662 
13663 void
13664 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event)
13665 {
13666 	sata_hba_inst_t *sata_hba_inst = NULL;
13667 	sata_address_t *saddr;
13668 	sata_drive_info_t *sdinfo;
13669 	sata_port_stats_t *pstats;
13670 	int cport, pmport;
13671 	char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1];
13672 	char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1];
13673 	char *lcp;
13674 	static char *err_msg_evnt_1 =
13675 	    "sata_hba_event_notify: invalid port event 0x%x ";
13676 	static char *err_msg_evnt_2 =
13677 	    "sata_hba_event_notify: invalid device event 0x%x ";
13678 	int linkevent;
13679 
13680 	/*
13681 	 * There is a possibility that an event will be generated on HBA
13682 	 * that has not completed attachment or is detaching.
13683 	 * HBA driver should prevent this, but just in case it does not,
13684 	 * we need to ignore events for such HBA.
13685 	 */
13686 	mutex_enter(&sata_mutex);
13687 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13688 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13689 		if (SATA_DIP(sata_hba_inst) == dip)
13690 			if (sata_hba_inst->satahba_attached == 1)
13691 				break;
13692 	}
13693 	mutex_exit(&sata_mutex);
13694 	if (sata_hba_inst == NULL)
13695 		/* HBA not attached */
13696 		return;
13697 
13698 	ASSERT(sata_device != NULL);
13699 
13700 	/*
13701 	 * Validate address before - do not proceed with invalid address.
13702 	 */
13703 	saddr = &sata_device->satadev_addr;
13704 	if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst))
13705 		return;
13706 	if (saddr->qual == SATA_ADDR_PMPORT ||
13707 	    saddr->qual == SATA_ADDR_DPMPORT)
13708 		/* Port Multiplier not supported yet */
13709 		return;
13710 
13711 	cport = saddr->cport;
13712 	pmport = saddr->pmport;
13713 
13714 	buf1[0] = buf2[0] = '\0';
13715 
13716 	/*
13717 	 * Events refer to devices, ports and controllers - each has
13718 	 * unique address. Events for different addresses cannot be combined.
13719 	 */
13720 	if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) {
13721 
13722 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13723 
13724 		/* qualify this event(s) */
13725 		if ((event & SATA_EVNT_PORT_EVENTS) == 0) {
13726 			/* Invalid event for the device port */
13727 			(void) sprintf(buf2, err_msg_evnt_1,
13728 			    event & SATA_EVNT_PORT_EVENTS);
13729 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13730 			goto event_info;
13731 		}
13732 		if (saddr->qual == SATA_ADDR_CPORT) {
13733 			/* Controller's device port event */
13734 
13735 			(SATA_CPORT_INFO(sata_hba_inst, cport))->
13736 			    cport_event_flags |=
13737 			    event & SATA_EVNT_PORT_EVENTS;
13738 			pstats =
13739 			    &(SATA_CPORT_INFO(sata_hba_inst, cport))->
13740 			    cport_stats;
13741 		} else {
13742 			/* Port multiplier's device port event */
13743 			(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
13744 			    pmport_event_flags |=
13745 			    event & SATA_EVNT_PORT_EVENTS;
13746 			pstats =
13747 			    &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
13748 			    pmport_stats;
13749 		}
13750 
13751 		/*
13752 		 * Add to statistics and log the message. We have to do it
13753 		 * here rather than in the event daemon, because there may be
13754 		 * multiple events occuring before they are processed.
13755 		 */
13756 		linkevent = event &
13757 		    (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED);
13758 		if (linkevent) {
13759 			if (linkevent == (SATA_EVNT_LINK_LOST |
13760 			    SATA_EVNT_LINK_ESTABLISHED)) {
13761 				/* This is likely event combination */
13762 				(void) strlcat(buf1, "link lost/established, ",
13763 				    SATA_EVENT_MAX_MSG_LENGTH);
13764 
13765 				if (pstats->link_lost < 0xffffffffffffffffULL)
13766 					pstats->link_lost++;
13767 				if (pstats->link_established <
13768 				    0xffffffffffffffffULL)
13769 					pstats->link_established++;
13770 				linkevent = 0;
13771 			} else if (linkevent & SATA_EVNT_LINK_LOST) {
13772 				(void) strlcat(buf1, "link lost, ",
13773 				    SATA_EVENT_MAX_MSG_LENGTH);
13774 
13775 				if (pstats->link_lost < 0xffffffffffffffffULL)
13776 					pstats->link_lost++;
13777 			} else {
13778 				(void) strlcat(buf1, "link established, ",
13779 				    SATA_EVENT_MAX_MSG_LENGTH);
13780 				if (pstats->link_established <
13781 				    0xffffffffffffffffULL)
13782 					pstats->link_established++;
13783 			}
13784 		}
13785 		if (event & SATA_EVNT_DEVICE_ATTACHED) {
13786 			(void) strlcat(buf1, "device attached, ",
13787 			    SATA_EVENT_MAX_MSG_LENGTH);
13788 			if (pstats->device_attached < 0xffffffffffffffffULL)
13789 				pstats->device_attached++;
13790 		}
13791 		if (event & SATA_EVNT_DEVICE_DETACHED) {
13792 			(void) strlcat(buf1, "device detached, ",
13793 			    SATA_EVENT_MAX_MSG_LENGTH);
13794 			if (pstats->device_detached < 0xffffffffffffffffULL)
13795 				pstats->device_detached++;
13796 		}
13797 		if (event & SATA_EVNT_PWR_LEVEL_CHANGED) {
13798 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
13799 			    "port %d power level changed", cport);
13800 			if (pstats->port_pwr_changed < 0xffffffffffffffffULL)
13801 				pstats->port_pwr_changed++;
13802 		}
13803 
13804 		if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) {
13805 			/* There should be no other events for this address */
13806 			(void) sprintf(buf2, err_msg_evnt_1,
13807 			    event & ~SATA_EVNT_PORT_EVENTS);
13808 		}
13809 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13810 
13811 	} else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) {
13812 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13813 
13814 		/* qualify this event */
13815 		if ((event & SATA_EVNT_DEVICE_RESET) == 0) {
13816 			/* Invalid event for a device */
13817 			(void) sprintf(buf2, err_msg_evnt_2,
13818 			    event & SATA_EVNT_DEVICE_RESET);
13819 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13820 			goto event_info;
13821 		}
13822 		/* drive event */
13823 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
13824 		if (sdinfo != NULL) {
13825 			if (event & SATA_EVNT_DEVICE_RESET) {
13826 				(void) strlcat(buf1, "device reset, ",
13827 				    SATA_EVENT_MAX_MSG_LENGTH);
13828 				if (sdinfo->satadrv_stats.drive_reset <
13829 				    0xffffffffffffffffULL)
13830 					sdinfo->satadrv_stats.drive_reset++;
13831 				sdinfo->satadrv_event_flags |=
13832 				    SATA_EVNT_DEVICE_RESET;
13833 			}
13834 		}
13835 		if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) {
13836 			/* Invalid event for a device */
13837 			(void) sprintf(buf2, err_msg_evnt_2,
13838 			    event & ~SATA_EVNT_DRIVE_EVENTS);
13839 		}
13840 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13841 	} else {
13842 		if (saddr->qual != SATA_ADDR_NULL) {
13843 			/* Wrong address qualifier */
13844 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13845 			    "sata_hba_event_notify: invalid address 0x%x",
13846 			    *(uint32_t *)saddr));
13847 			return;
13848 		}
13849 		if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 ||
13850 		    (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) {
13851 			/* Invalid event for the controller */
13852 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13853 			    "sata_hba_event_notify: invalid event 0x%x for "
13854 			    "controller",
13855 			    event & SATA_EVNT_CONTROLLER_EVENTS));
13856 			return;
13857 		}
13858 		buf1[0] = '\0';
13859 		/* This may be a frequent and not interesting event */
13860 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
13861 		    "controller power level changed\n", NULL);
13862 
13863 		mutex_enter(&sata_hba_inst->satahba_mutex);
13864 		if (sata_hba_inst->satahba_stats.ctrl_pwr_change <
13865 		    0xffffffffffffffffULL)
13866 			sata_hba_inst->satahba_stats.ctrl_pwr_change++;
13867 
13868 		sata_hba_inst->satahba_event_flags |=
13869 		    SATA_EVNT_PWR_LEVEL_CHANGED;
13870 		mutex_exit(&sata_hba_inst->satahba_mutex);
13871 	}
13872 	/*
13873 	 * If we got here, there is something to do with this HBA
13874 	 * instance.
13875 	 */
13876 	mutex_enter(&sata_hba_inst->satahba_mutex);
13877 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
13878 	mutex_exit(&sata_hba_inst->satahba_mutex);
13879 	mutex_enter(&sata_mutex);
13880 	sata_event_pending |= SATA_EVNT_MAIN;	/* global event indicator */
13881 	mutex_exit(&sata_mutex);
13882 
13883 	/* Tickle event thread */
13884 	mutex_enter(&sata_event_mutex);
13885 	if (sata_event_thread_active == 0)
13886 		cv_signal(&sata_event_cv);
13887 	mutex_exit(&sata_event_mutex);
13888 
13889 event_info:
13890 	if (buf1[0] != '\0') {
13891 		lcp = strrchr(buf1, ',');
13892 		if (lcp != NULL)
13893 			*lcp = '\0';
13894 	}
13895 	if (saddr->qual == SATA_ADDR_CPORT ||
13896 	    saddr->qual == SATA_ADDR_DCPORT) {
13897 		if (buf1[0] != '\0') {
13898 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
13899 			    cport, buf1);
13900 		}
13901 		if (buf2[0] != '\0') {
13902 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
13903 			    cport, buf2);
13904 		}
13905 	} else if (saddr->qual == SATA_ADDR_PMPORT ||
13906 	    saddr->qual == SATA_ADDR_DPMPORT) {
13907 		if (buf1[0] != '\0') {
13908 			sata_log(sata_hba_inst, CE_NOTE,
13909 			    "port %d pmport %d: %s\n", cport, pmport, buf1);
13910 		}
13911 		if (buf2[0] != '\0') {
13912 			sata_log(sata_hba_inst, CE_NOTE,
13913 			    "port %d pmport %d: %s\n", cport, pmport, buf2);
13914 		}
13915 	}
13916 }
13917 
13918 
13919 /*
13920  * Event processing thread.
13921  * Arg is a pointer to the sata_hba_list pointer.
13922  * It is not really needed, because sata_hba_list is global and static
13923  */
13924 static void
13925 sata_event_daemon(void *arg)
13926 {
13927 #ifndef __lock_lint
13928 	_NOTE(ARGUNUSED(arg))
13929 #endif
13930 	sata_hba_inst_t *sata_hba_inst;
13931 	clock_t lbolt;
13932 
13933 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13934 	    "SATA event daemon started\n", NULL);
13935 loop:
13936 	/*
13937 	 * Process events here. Walk through all registered HBAs
13938 	 */
13939 	mutex_enter(&sata_mutex);
13940 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13941 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13942 		ASSERT(sata_hba_inst != NULL);
13943 		mutex_enter(&sata_hba_inst->satahba_mutex);
13944 		if (sata_hba_inst->satahba_attached != 1 ||
13945 		    (sata_hba_inst->satahba_event_flags &
13946 		    SATA_EVNT_SKIP) != 0) {
13947 			mutex_exit(&sata_hba_inst->satahba_mutex);
13948 			continue;
13949 		}
13950 		if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) {
13951 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP;
13952 			mutex_exit(&sata_hba_inst->satahba_mutex);
13953 			mutex_exit(&sata_mutex);
13954 			/* Got the controller with pending event */
13955 			sata_process_controller_events(sata_hba_inst);
13956 			/*
13957 			 * Since global mutex was released, there is a
13958 			 * possibility that HBA list has changed, so start
13959 			 * over from the top. Just processed controller
13960 			 * will be passed-over because of the SKIP flag.
13961 			 */
13962 			goto loop;
13963 		}
13964 		mutex_exit(&sata_hba_inst->satahba_mutex);
13965 	}
13966 	/* Clear SKIP flag in all controllers */
13967 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13968 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13969 		mutex_enter(&sata_hba_inst->satahba_mutex);
13970 		sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP;
13971 		mutex_exit(&sata_hba_inst->satahba_mutex);
13972 	}
13973 	mutex_exit(&sata_mutex);
13974 
13975 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13976 	    "SATA EVENT DAEMON suspending itself", NULL);
13977 
13978 #ifdef SATA_DEBUG
13979 	if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) {
13980 		sata_log(sata_hba_inst, CE_WARN,
13981 		    "SATA EVENTS PROCESSING DISABLED\n");
13982 		thread_exit(); /* Daemon will not run again */
13983 	}
13984 #endif
13985 	mutex_enter(&sata_event_mutex);
13986 	sata_event_thread_active = 0;
13987 	mutex_exit(&sata_event_mutex);
13988 	/*
13989 	 * Go to sleep/suspend itself and wake up either because new event or
13990 	 * wait timeout. Exit if there is a termination request (driver
13991 	 * unload).
13992 	 */
13993 	do {
13994 		lbolt = ddi_get_lbolt();
13995 		lbolt += drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME);
13996 		mutex_enter(&sata_event_mutex);
13997 		(void) cv_timedwait(&sata_event_cv, &sata_event_mutex, lbolt);
13998 
13999 		if (sata_event_thread_active != 0) {
14000 			mutex_exit(&sata_event_mutex);
14001 			continue;
14002 		}
14003 
14004 		/* Check if it is time to go away */
14005 		if (sata_event_thread_terminate == 1) {
14006 			/*
14007 			 * It is up to the thread setting above flag to make
14008 			 * sure that this thread is not killed prematurely.
14009 			 */
14010 			sata_event_thread_terminate = 0;
14011 			sata_event_thread = NULL;
14012 			mutex_exit(&sata_event_mutex);
14013 			SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
14014 			    "SATA_EVENT_DAEMON_TERMINATING", NULL);
14015 			thread_exit();  { _NOTE(NOT_REACHED) }
14016 		}
14017 		mutex_exit(&sata_event_mutex);
14018 	} while (!(sata_event_pending & SATA_EVNT_MAIN));
14019 
14020 	mutex_enter(&sata_event_mutex);
14021 	sata_event_thread_active = 1;
14022 	mutex_exit(&sata_event_mutex);
14023 
14024 	mutex_enter(&sata_mutex);
14025 	sata_event_pending &= ~SATA_EVNT_MAIN;
14026 	mutex_exit(&sata_mutex);
14027 
14028 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
14029 	    "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL);
14030 
14031 	goto loop;
14032 }
14033 
14034 /*
14035  * Specific HBA instance event processing.
14036  *
14037  * NOTE: At the moment, device event processing is limited to hard disks
14038  * only.
14039  * cports only are supported - no pmports.
14040  */
14041 static void
14042 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst)
14043 {
14044 	int ncport;
14045 	uint32_t event_flags;
14046 	sata_address_t *saddr;
14047 
14048 	SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst,
14049 	    "Processing controller %d event(s)",
14050 	    ddi_get_instance(SATA_DIP(sata_hba_inst)));
14051 
14052 	mutex_enter(&sata_hba_inst->satahba_mutex);
14053 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN;
14054 	event_flags = sata_hba_inst->satahba_event_flags;
14055 	mutex_exit(&sata_hba_inst->satahba_mutex);
14056 	/*
14057 	 * Process controller power change first
14058 	 * HERE
14059 	 */
14060 	if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED)
14061 		sata_process_cntrl_pwr_level_change(sata_hba_inst);
14062 
14063 	/*
14064 	 * Search through ports/devices to identify affected port/device.
14065 	 * We may have to process events for more than one port/device.
14066 	 */
14067 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
14068 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14069 		event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))->
14070 		    cport_event_flags;
14071 		/* Check if port was locked by IOCTL processing */
14072 		if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) {
14073 			/*
14074 			 * We ignore port events because port is busy
14075 			 * with AP control processing. Set again
14076 			 * controller and main event flag, so that
14077 			 * events may be processed by the next daemon
14078 			 * run.
14079 			 */
14080 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14081 			mutex_enter(&sata_hba_inst->satahba_mutex);
14082 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14083 			mutex_exit(&sata_hba_inst->satahba_mutex);
14084 			mutex_enter(&sata_mutex);
14085 			sata_event_pending |= SATA_EVNT_MAIN;
14086 			mutex_exit(&sata_mutex);
14087 			SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst,
14088 			    "Event processing postponed until "
14089 			    "AP control processing completes",
14090 			    NULL);
14091 			/* Check other ports */
14092 			continue;
14093 		} else {
14094 			/*
14095 			 * Set BSY flag so that AP control would not
14096 			 * interfere with events processing for
14097 			 * this port.
14098 			 */
14099 			(SATA_CPORT_INFO(sata_hba_inst, ncport))->
14100 			    cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY;
14101 		}
14102 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14103 
14104 		saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr;
14105 
14106 		if ((event_flags &
14107 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
14108 			/*
14109 			 * Got port event.
14110 			 * We need some hierarchy of event processing as they
14111 			 * are affecting each other:
14112 			 * 1. port failed
14113 			 * 2. device detached/attached
14114 			 * 3. link events - link events may trigger device
14115 			 *    detached or device attached events in some
14116 			 *    circumstances.
14117 			 * 4. port power level changed
14118 			 */
14119 			if (event_flags & SATA_EVNT_PORT_FAILED) {
14120 				sata_process_port_failed_event(sata_hba_inst,
14121 				    saddr);
14122 			}
14123 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
14124 				sata_process_device_detached(sata_hba_inst,
14125 				    saddr);
14126 			}
14127 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
14128 				sata_process_device_attached(sata_hba_inst,
14129 				    saddr);
14130 			}
14131 			if (event_flags &
14132 			    (SATA_EVNT_LINK_ESTABLISHED |
14133 			    SATA_EVNT_LINK_LOST)) {
14134 				sata_process_port_link_events(sata_hba_inst,
14135 				    saddr);
14136 			}
14137 			if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) {
14138 				sata_process_port_pwr_change(sata_hba_inst,
14139 				    saddr);
14140 			}
14141 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
14142 				sata_process_target_node_cleanup(
14143 				    sata_hba_inst, saddr);
14144 			}
14145 			if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) {
14146 				sata_process_device_autoonline(
14147 				    sata_hba_inst, saddr);
14148 			}
14149 		}
14150 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14151 		if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) !=
14152 		    SATA_DTYPE_NONE) &&
14153 		    (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) {
14154 			if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)->
14155 			    satadrv_event_flags &
14156 			    (SATA_EVNT_DEVICE_RESET |
14157 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
14158 				/* Have device event */
14159 				sata_process_device_reset(sata_hba_inst,
14160 				    saddr);
14161 			}
14162 		}
14163 		/* Release PORT_BUSY flag */
14164 		(SATA_CPORT_INFO(sata_hba_inst, ncport))->
14165 		    cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
14166 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14167 
14168 	} /* End of loop through the controller SATA ports */
14169 }
14170 
14171 /*
14172  * Process HBA power level change reported by HBA driver.
14173  * Not implemented at this time - event is ignored.
14174  */
14175 static void
14176 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst)
14177 {
14178 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14179 	    "Processing controller power level change", NULL);
14180 
14181 	/* Ignoring it for now */
14182 	mutex_enter(&sata_hba_inst->satahba_mutex);
14183 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
14184 	mutex_exit(&sata_hba_inst->satahba_mutex);
14185 }
14186 
14187 /*
14188  * Process port power level change reported by HBA driver.
14189  * Not implemented at this time - event is ignored.
14190  */
14191 static void
14192 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst,
14193     sata_address_t *saddr)
14194 {
14195 	sata_cport_info_t *cportinfo;
14196 
14197 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14198 	    "Processing port power level change", NULL);
14199 
14200 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14201 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14202 	/* Reset event flag */
14203 	cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
14204 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14205 }
14206 
14207 /*
14208  * Process port failure reported by HBA driver.
14209  * cports support only - no pmports.
14210  */
14211 static void
14212 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst,
14213     sata_address_t *saddr)
14214 {
14215 	sata_cport_info_t *cportinfo;
14216 
14217 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14218 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14219 	/* Reset event flag first */
14220 	cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED;
14221 	/* If the port is in SHUTDOWN or FAILED state, ignore this event. */
14222 	if ((cportinfo->cport_state &
14223 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) {
14224 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14225 		    cport_mutex);
14226 		return;
14227 	}
14228 	/* Fail the port */
14229 	cportinfo->cport_state = SATA_PSTATE_FAILED;
14230 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14231 	sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport);
14232 }
14233 
14234 /*
14235  * Device Reset Event processing.
14236  * The seqeunce is managed by 3 stage flags:
14237  * - reset event reported,
14238  * - reset event being processed,
14239  * - request to clear device reset state.
14240  *
14241  * NOTE: This function has to be entered with cport mutex held. It exits with
14242  * mutex held as well, but can release mutex during the processing.
14243  */
14244 static void
14245 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst,
14246     sata_address_t *saddr)
14247 {
14248 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
14249 	sata_drive_info_t *sdinfo;
14250 	sata_cport_info_t *cportinfo;
14251 	sata_device_t sata_device;
14252 	int rval;
14253 
14254 	/* We only care about host sata cport for now */
14255 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14256 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14257 	/*
14258 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
14259 	 * state, ignore reset event.
14260 	 */
14261 	if (((cportinfo->cport_state &
14262 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
14263 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
14264 		sdinfo->satadrv_event_flags &=
14265 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
14266 		return;
14267 	}
14268 
14269 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) &
14270 	    SATA_VALID_DEV_TYPE) == 0) {
14271 		/*
14272 		 * This should not happen - coding error.
14273 		 * But we can recover, so do not panic, just clean up
14274 		 * and if in debug mode, log the message.
14275 		 */
14276 #ifdef SATA_DEBUG
14277 		sata_log(sata_hba_inst, CE_WARN,
14278 		    "sata_process_device_reset: "
14279 		    "Invalid device type with sdinfo!", NULL);
14280 #endif
14281 		sdinfo->satadrv_event_flags = 0;
14282 		return;
14283 	}
14284 
14285 #ifdef SATA_DEBUG
14286 	if ((sdinfo->satadrv_event_flags &
14287 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
14288 		/* Nothing to do */
14289 		/* Something is weird - why we are processing dev reset? */
14290 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14291 		    "No device reset event!!!!", NULL);
14292 
14293 		return;
14294 	}
14295 	if ((sdinfo->satadrv_event_flags &
14296 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
14297 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
14298 		/* Something is weird - new device reset event */
14299 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14300 		    "Overlapping device reset events!", NULL);
14301 	}
14302 #endif
14303 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14304 	    "Processing port %d device reset", saddr->cport);
14305 
14306 	/* Clear event flag */
14307 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
14308 
14309 	/* It seems that we always need to check the port state first */
14310 	sata_device.satadev_rev = SATA_DEVICE_REV;
14311 	sata_device.satadev_addr = *saddr;
14312 	/*
14313 	 * We have to exit mutex, because the HBA probe port function may
14314 	 * block on its own mutex.
14315 	 */
14316 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14317 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14318 	    (SATA_DIP(sata_hba_inst), &sata_device);
14319 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14320 	sata_update_port_info(sata_hba_inst, &sata_device);
14321 	if (rval != SATA_SUCCESS) {
14322 		/* Something went wrong? Fail the port */
14323 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14324 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14325 		if (sdinfo != NULL)
14326 			sdinfo->satadrv_event_flags = 0;
14327 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14328 		    cport_mutex);
14329 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14330 		    "SATA port %d probing failed",
14331 		    saddr->cport));
14332 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14333 		    saddr->cport)->cport_mutex);
14334 		return;
14335 	}
14336 	if ((sata_device.satadev_scr.sstatus  &
14337 	    SATA_PORT_DEVLINK_UP_MASK) !=
14338 	    SATA_PORT_DEVLINK_UP ||
14339 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
14340 		/*
14341 		 * No device to process, anymore. Some other event processing
14342 		 * would or have already performed port info cleanup.
14343 		 * To be safe (HBA may need it), request clearing device
14344 		 * reset condition.
14345 		 */
14346 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14347 		if (sdinfo != NULL) {
14348 			sdinfo->satadrv_event_flags &=
14349 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
14350 			sdinfo->satadrv_event_flags |=
14351 			    SATA_EVNT_CLEAR_DEVICE_RESET;
14352 		}
14353 		return;
14354 	}
14355 
14356 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14357 	if (sdinfo == NULL) {
14358 		return;
14359 	}
14360 	if ((sdinfo->satadrv_event_flags &
14361 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
14362 		/*
14363 		 * Start tracking time for device feature restoration and
14364 		 * identification. Save current time (lbolt value).
14365 		 */
14366 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
14367 	}
14368 	/* Mark device reset processing as active */
14369 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
14370 
14371 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
14372 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14373 
14374 	if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) ==
14375 	    SATA_FAILURE) {
14376 		/*
14377 		 * Restoring drive setting failed.
14378 		 * Probe the port first, to check if the port state has changed
14379 		 */
14380 		sata_device.satadev_rev = SATA_DEVICE_REV;
14381 		sata_device.satadev_addr = *saddr;
14382 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
14383 		/* probe port */
14384 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14385 		    (SATA_DIP(sata_hba_inst), &sata_device);
14386 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14387 		    cport_mutex);
14388 		if (rval == SATA_SUCCESS &&
14389 		    (sata_device.satadev_state &
14390 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
14391 		    (sata_device.satadev_scr.sstatus  &
14392 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
14393 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
14394 			/*
14395 			 * We may retry this a bit later - in-process reset
14396 			 * condition should be already set.
14397 			 * Track retry time for device identification.
14398 			 */
14399 			if ((cportinfo->cport_dev_type &
14400 			    SATA_VALID_DEV_TYPE) != 0 &&
14401 			    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL &&
14402 			    sdinfo->satadrv_reset_time != 0) {
14403 				clock_t cur_time = ddi_get_lbolt();
14404 				/*
14405 				 * If the retry time limit was not
14406 				 * exceeded, retry.
14407 				 */
14408 				if ((cur_time - sdinfo->satadrv_reset_time) <
14409 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
14410 					mutex_enter(
14411 					    &sata_hba_inst->satahba_mutex);
14412 					sata_hba_inst->satahba_event_flags |=
14413 					    SATA_EVNT_MAIN;
14414 					mutex_exit(
14415 					    &sata_hba_inst->satahba_mutex);
14416 					mutex_enter(&sata_mutex);
14417 					sata_event_pending |= SATA_EVNT_MAIN;
14418 					mutex_exit(&sata_mutex);
14419 					return;
14420 				}
14421 			}
14422 			/* Fail the drive */
14423 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
14424 
14425 			sata_log(sata_hba_inst, CE_WARN,
14426 			    "SATA device at port %d - device failed",
14427 			    saddr->cport);
14428 		} else {
14429 			/*
14430 			 * No point of retrying - some other event processing
14431 			 * would or already did port info cleanup.
14432 			 * To be safe (HBA may need it),
14433 			 * request clearing device reset condition.
14434 			 */
14435 			sdinfo->satadrv_event_flags |=
14436 			    SATA_EVNT_CLEAR_DEVICE_RESET;
14437 		}
14438 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
14439 		sdinfo->satadrv_reset_time = 0;
14440 		return;
14441 	}
14442 	/*
14443 	 * Raise the flag indicating that the next sata command could
14444 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
14445 	 * reset is reported.
14446 	 */
14447 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14448 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14449 		sdinfo->satadrv_reset_time = 0;
14450 		if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) {
14451 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14452 			sdinfo->satadrv_event_flags &=
14453 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
14454 			sdinfo->satadrv_event_flags |=
14455 			    SATA_EVNT_CLEAR_DEVICE_RESET;
14456 		}
14457 	}
14458 }
14459 
14460 
14461 /*
14462  * Port Link Events processing.
14463  * Every link established event may involve device reset (due to
14464  * COMRESET signal, equivalent of the hard reset) so arbitrarily
14465  * set device reset event for an attached device (if any).
14466  * If the port is in SHUTDOWN or FAILED state, ignore link events.
14467  *
14468  * The link established event processing varies, depending on the state
14469  * of the target node, HBA hotplugging capabilities, state of the port.
14470  * If the link is not active, the link established event is ignored.
14471  * If HBA cannot detect device attachment and there is no target node,
14472  * the link established event triggers device attach event processing.
14473  * Else, link established event triggers device reset event processing.
14474  *
14475  * The link lost event processing varies, depending on a HBA hotplugging
14476  * capability and the state of the port (link active or not active).
14477  * If the link is active, the lost link event is ignored.
14478  * If HBA cannot detect device removal, the lost link event triggers
14479  * device detached event processing after link lost timeout.
14480  * Else, the event is ignored.
14481  *
14482  * NOTE: Only cports are processed for now, i.e. no port multiplier ports
14483  */
14484 static void
14485 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst,
14486     sata_address_t *saddr)
14487 {
14488 	sata_device_t sata_device;
14489 	sata_cport_info_t *cportinfo;
14490 	sata_drive_info_t *sdinfo;
14491 	uint32_t event_flags;
14492 	int rval;
14493 
14494 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14495 	    "Processing port %d link event(s)", saddr->cport);
14496 
14497 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14498 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14499 	event_flags = cportinfo->cport_event_flags;
14500 
14501 	/* Reset event flags first */
14502 	cportinfo->cport_event_flags &=
14503 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
14504 
14505 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
14506 	if ((cportinfo->cport_state &
14507 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14508 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14509 		    cport_mutex);
14510 		return;
14511 	}
14512 
14513 	/*
14514 	 * For the sanity sake get current port state.
14515 	 * Set device address only. Other sata_device fields should be
14516 	 * set by HBA driver.
14517 	 */
14518 	sata_device.satadev_rev = SATA_DEVICE_REV;
14519 	sata_device.satadev_addr = *saddr;
14520 	/*
14521 	 * We have to exit mutex, because the HBA probe port function may
14522 	 * block on its own mutex.
14523 	 */
14524 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14525 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14526 	    (SATA_DIP(sata_hba_inst), &sata_device);
14527 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14528 	sata_update_port_info(sata_hba_inst, &sata_device);
14529 	if (rval != SATA_SUCCESS) {
14530 		/* Something went wrong? Fail the port */
14531 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14532 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14533 		    cport_mutex);
14534 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14535 		    "SATA port %d probing failed",
14536 		    saddr->cport));
14537 		/*
14538 		 * We may want to release device info structure, but
14539 		 * it is not necessary.
14540 		 */
14541 		return;
14542 	} else {
14543 		/* port probed successfully */
14544 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14545 	}
14546 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
14547 
14548 		if ((sata_device.satadev_scr.sstatus &
14549 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
14550 			/* Ignore event */
14551 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14552 			    "Ignoring port %d link established event - "
14553 			    "link down",
14554 			    saddr->cport);
14555 			goto linklost;
14556 		}
14557 
14558 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14559 		    "Processing port %d link established event",
14560 		    saddr->cport);
14561 
14562 		/*
14563 		 * For the sanity sake check if a device is attached - check
14564 		 * return state of a port probing.
14565 		 */
14566 		if (sata_device.satadev_type != SATA_DTYPE_NONE &&
14567 		    sata_device.satadev_type != SATA_DTYPE_PMULT) {
14568 			/*
14569 			 * HBA port probe indicated that there is a device
14570 			 * attached. Check if the framework had device info
14571 			 * structure attached for this device.
14572 			 */
14573 			if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
14574 				ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) !=
14575 				    NULL);
14576 
14577 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14578 				if ((sdinfo->satadrv_type &
14579 				    SATA_VALID_DEV_TYPE) != 0) {
14580 					/*
14581 					 * Dev info structure is present.
14582 					 * If dev_type is set to known type in
14583 					 * the framework's drive info struct
14584 					 * then the device existed before and
14585 					 * the link was probably lost
14586 					 * momentarily - in such case
14587 					 * we may want to check device
14588 					 * identity.
14589 					 * Identity check is not supported now.
14590 					 *
14591 					 * Link established event
14592 					 * triggers device reset event.
14593 					 */
14594 					(SATA_CPORTINFO_DRV_INFO(cportinfo))->
14595 					    satadrv_event_flags |=
14596 					    SATA_EVNT_DEVICE_RESET;
14597 				}
14598 			} else if (cportinfo->cport_dev_type ==
14599 			    SATA_DTYPE_NONE) {
14600 				/*
14601 				 * We got new device attached! If HBA does not
14602 				 * generate device attached events, trigger it
14603 				 * here.
14604 				 */
14605 				if (!(SATA_FEATURES(sata_hba_inst) &
14606 				    SATA_CTLF_HOTPLUG)) {
14607 					cportinfo->cport_event_flags |=
14608 					    SATA_EVNT_DEVICE_ATTACHED;
14609 				}
14610 			}
14611 			/* Reset link lost timeout */
14612 			cportinfo->cport_link_lost_time = 0;
14613 		}
14614 	}
14615 linklost:
14616 	if (event_flags & SATA_EVNT_LINK_LOST) {
14617 		if ((sata_device.satadev_scr.sstatus &
14618 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
14619 			/* Ignore event */
14620 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14621 			    "Ignoring port %d link lost event - link is up",
14622 			    saddr->cport);
14623 			goto done;
14624 		}
14625 #ifdef SATA_DEBUG
14626 		if (cportinfo->cport_link_lost_time == 0) {
14627 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14628 			    "Processing port %d link lost event",
14629 			    saddr->cport);
14630 		}
14631 #endif
14632 		/*
14633 		 * When HBA cannot generate device attached/detached events,
14634 		 * we need to track link lost time and eventually generate
14635 		 * device detach event.
14636 		 */
14637 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
14638 			/* We are tracking link lost time */
14639 			if (cportinfo->cport_link_lost_time == 0) {
14640 				/* save current time (lbolt value) */
14641 				cportinfo->cport_link_lost_time =
14642 				    ddi_get_lbolt();
14643 				/* just keep link lost event */
14644 				cportinfo->cport_event_flags |=
14645 				    SATA_EVNT_LINK_LOST;
14646 			} else {
14647 				clock_t cur_time = ddi_get_lbolt();
14648 				if ((cur_time -
14649 				    cportinfo->cport_link_lost_time) >=
14650 				    drv_usectohz(
14651 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
14652 					/* trigger device detach event */
14653 					cportinfo->cport_event_flags |=
14654 					    SATA_EVNT_DEVICE_DETACHED;
14655 					cportinfo->cport_link_lost_time = 0;
14656 					SATADBG1(SATA_DBG_EVENTS,
14657 					    sata_hba_inst,
14658 					    "Triggering port %d "
14659 					    "device detached event",
14660 					    saddr->cport);
14661 				} else {
14662 					/* keep link lost event */
14663 					cportinfo->cport_event_flags |=
14664 					    SATA_EVNT_LINK_LOST;
14665 				}
14666 			}
14667 		}
14668 		/*
14669 		 * We could change port state to disable/delay access to
14670 		 * the attached device until the link is recovered.
14671 		 */
14672 	}
14673 done:
14674 	event_flags = cportinfo->cport_event_flags;
14675 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14676 	if (event_flags != 0) {
14677 		mutex_enter(&sata_hba_inst->satahba_mutex);
14678 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14679 		mutex_exit(&sata_hba_inst->satahba_mutex);
14680 		mutex_enter(&sata_mutex);
14681 		sata_event_pending |= SATA_EVNT_MAIN;
14682 		mutex_exit(&sata_mutex);
14683 	}
14684 }
14685 
14686 /*
14687  * Device Detached Event processing.
14688  * Port is probed to find if a device is really gone. If so,
14689  * the device info structure is detached from the SATA port info structure
14690  * and released.
14691  * Port status is updated.
14692  *
14693  * NOTE: Process cports event only, no port multiplier ports.
14694  */
14695 static void
14696 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst,
14697     sata_address_t *saddr)
14698 {
14699 	sata_cport_info_t *cportinfo;
14700 	sata_drive_info_t *sdevinfo;
14701 	sata_device_t sata_device;
14702 	dev_info_t *tdip;
14703 	int rval;
14704 
14705 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14706 	    "Processing port %d device detached", saddr->cport);
14707 
14708 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14709 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14710 	/* Clear event flag */
14711 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
14712 
14713 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
14714 	if ((cportinfo->cport_state &
14715 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14716 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14717 		    cport_mutex);
14718 		return;
14719 	}
14720 	/* For sanity, re-probe the port */
14721 	sata_device.satadev_rev = SATA_DEVICE_REV;
14722 	sata_device.satadev_addr = *saddr;
14723 
14724 	/*
14725 	 * We have to exit mutex, because the HBA probe port function may
14726 	 * block on its own mutex.
14727 	 */
14728 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14729 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14730 	    (SATA_DIP(sata_hba_inst), &sata_device);
14731 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14732 	sata_update_port_info(sata_hba_inst, &sata_device);
14733 	if (rval != SATA_SUCCESS) {
14734 		/* Something went wrong? Fail the port */
14735 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14736 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14737 		    cport_mutex);
14738 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14739 		    "SATA port %d probing failed",
14740 		    saddr->cport));
14741 		/*
14742 		 * We may want to release device info structure, but
14743 		 * it is not necessary.
14744 		 */
14745 		return;
14746 	} else {
14747 		/* port probed successfully */
14748 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14749 	}
14750 	/*
14751 	 * Check if a device is still attached. For sanity, check also
14752 	 * link status - if no link, there is no device.
14753 	 */
14754 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
14755 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
14756 	    SATA_DTYPE_NONE) {
14757 		/*
14758 		 * Device is still attached - ignore detach event.
14759 		 */
14760 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14761 		    cport_mutex);
14762 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14763 		    "Ignoring detach - device still attached to port %d",
14764 		    sata_device.satadev_addr.cport);
14765 		return;
14766 	}
14767 	/*
14768 	 * We need to detach and release device info structure here
14769 	 */
14770 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14771 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14772 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14773 		(void) kmem_free((void *)sdevinfo,
14774 		    sizeof (sata_drive_info_t));
14775 	}
14776 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14777 	/*
14778 	 * Device cannot be reached anymore, even if the target node may be
14779 	 * still present.
14780 	 */
14781 
14782 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14783 	sata_log(sata_hba_inst, CE_WARN, "SATA device detached at port %d",
14784 	    sata_device.satadev_addr.cport);
14785 
14786 	/*
14787 	 * Try to offline a device and remove target node if it still exists
14788 	 */
14789 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
14790 	if (tdip != NULL) {
14791 		/*
14792 		 * Target node exists.  Unconfigure device then remove
14793 		 * the target node (one ndi operation).
14794 		 */
14795 		if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
14796 			/*
14797 			 * PROBLEM - no device, but target node remained
14798 			 * This happens when the file was open or node was
14799 			 * waiting for resources.
14800 			 */
14801 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14802 			    "sata_process_device_detached: "
14803 			    "Failed to remove target node for "
14804 			    "detached SATA device."));
14805 			/*
14806 			 * Set target node state to DEVI_DEVICE_REMOVED.
14807 			 * But re-check first that the node still exists.
14808 			 */
14809 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
14810 			    saddr->cport);
14811 			if (tdip != NULL) {
14812 				sata_set_device_removed(tdip);
14813 				/*
14814 				 * Instruct event daemon to retry the
14815 				 * cleanup later.
14816 				 */
14817 				sata_set_target_node_cleanup(sata_hba_inst,
14818 				    &sata_device.satadev_addr);
14819 			}
14820 		}
14821 	}
14822 	/*
14823 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14824 	 * with the hint: SE_HINT_REMOVE
14825 	 */
14826 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
14827 }
14828 
14829 
14830 /*
14831  * Device Attached Event processing.
14832  * Port state is checked to verify that a device is really attached. If so,
14833  * the device info structure is created and attached to the SATA port info
14834  * structure.
14835  *
14836  * If attached device cannot be identified or set-up, the retry for the
14837  * attach processing is set-up. Subsequent daemon run would try again to
14838  * identify the device, until the time limit is reached
14839  * (SATA_DEV_IDENTIFY_TIMEOUT).
14840  *
14841  * This function cannot be called in interrupt context (it may sleep).
14842  *
14843  * NOTE: Process cports event only, no port multiplier ports.
14844  */
14845 static void
14846 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst,
14847     sata_address_t *saddr)
14848 {
14849 	sata_cport_info_t *cportinfo;
14850 	sata_drive_info_t *sdevinfo;
14851 	sata_device_t sata_device;
14852 	dev_info_t *tdip;
14853 	uint32_t event_flags;
14854 	int rval;
14855 
14856 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14857 	    "Processing port %d device attached", saddr->cport);
14858 
14859 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14860 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14861 
14862 	/* Clear attach event flag first */
14863 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
14864 
14865 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
14866 	if ((cportinfo->cport_state &
14867 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14868 		cportinfo->cport_dev_attach_time = 0;
14869 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14870 		    cport_mutex);
14871 		return;
14872 	}
14873 
14874 	/*
14875 	 * If the sata_drive_info structure is found attached to the port info,
14876 	 * despite the fact the device was removed and now it is re-attached,
14877 	 * the old drive info structure was not removed.
14878 	 * Arbitrarily release device info structure.
14879 	 */
14880 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14881 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14882 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14883 		(void) kmem_free((void *)sdevinfo,
14884 		    sizeof (sata_drive_info_t));
14885 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14886 		    "Arbitrarily detaching old device info.", NULL);
14887 	}
14888 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14889 
14890 	/* For sanity, re-probe the port */
14891 	sata_device.satadev_rev = SATA_DEVICE_REV;
14892 	sata_device.satadev_addr = *saddr;
14893 
14894 	/*
14895 	 * We have to exit mutex, because the HBA probe port function may
14896 	 * block on its own mutex.
14897 	 */
14898 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14899 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14900 	    (SATA_DIP(sata_hba_inst), &sata_device);
14901 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14902 	sata_update_port_info(sata_hba_inst, &sata_device);
14903 	if (rval != SATA_SUCCESS) {
14904 		/* Something went wrong? Fail the port */
14905 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14906 		cportinfo->cport_dev_attach_time = 0;
14907 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14908 		    cport_mutex);
14909 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14910 		    "SATA port %d probing failed",
14911 		    saddr->cport));
14912 		return;
14913 	} else {
14914 		/* port probed successfully */
14915 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14916 	}
14917 	/*
14918 	 * Check if a device is still attached. For sanity, check also
14919 	 * link status - if no link, there is no device.
14920 	 */
14921 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
14922 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
14923 	    SATA_DTYPE_NONE) {
14924 		/*
14925 		 * No device - ignore attach event.
14926 		 */
14927 		cportinfo->cport_dev_attach_time = 0;
14928 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14929 		    cport_mutex);
14930 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14931 		    "Ignoring attach - no device connected to port %d",
14932 		    sata_device.satadev_addr.cport);
14933 		return;
14934 	}
14935 
14936 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14937 	/*
14938 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14939 	 * with the hint: SE_HINT_INSERT
14940 	 */
14941 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
14942 
14943 	/*
14944 	 * Port reprobing will take care of the creation of the device
14945 	 * info structure and determination of the device type.
14946 	 */
14947 	sata_device.satadev_addr = *saddr;
14948 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
14949 	    SATA_DEV_IDENTIFY_NORETRY);
14950 
14951 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14952 	    cport_mutex);
14953 	if ((cportinfo->cport_state & SATA_STATE_READY) &&
14954 	    (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) {
14955 		/* Some device is attached to the port */
14956 		if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) {
14957 			/*
14958 			 * A device was not successfully attached.
14959 			 * Track retry time for device identification.
14960 			 */
14961 			if (cportinfo->cport_dev_attach_time != 0) {
14962 				clock_t cur_time = ddi_get_lbolt();
14963 				/*
14964 				 * If the retry time limit was not exceeded,
14965 				 * reinstate attach event.
14966 				 */
14967 				if ((cur_time -
14968 				    cportinfo->cport_dev_attach_time) <
14969 				    drv_usectohz(
14970 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
14971 					/* OK, restore attach event */
14972 					cportinfo->cport_event_flags |=
14973 					    SATA_EVNT_DEVICE_ATTACHED;
14974 				} else {
14975 					/* Timeout - cannot identify device */
14976 					cportinfo->cport_dev_attach_time = 0;
14977 					sata_log(sata_hba_inst,
14978 					    CE_WARN,
14979 					    "Could not identify SATA device "
14980 					    "at port %d",
14981 					    saddr->cport);
14982 				}
14983 			} else {
14984 				/*
14985 				 * Start tracking time for device
14986 				 * identification.
14987 				 * Save current time (lbolt value).
14988 				 */
14989 				cportinfo->cport_dev_attach_time =
14990 				    ddi_get_lbolt();
14991 				/* Restore attach event */
14992 				cportinfo->cport_event_flags |=
14993 				    SATA_EVNT_DEVICE_ATTACHED;
14994 			}
14995 		} else {
14996 			/*
14997 			 * If device was successfully attached, the subsequent
14998 			 * action depends on a state of the
14999 			 * sata_auto_online variable. If it is set to zero.
15000 			 * an explicit 'configure' command will be needed to
15001 			 * configure it. If its value is non-zero, we will
15002 			 * attempt to online (configure) the device.
15003 			 * First, log the message indicating that a device
15004 			 * was attached.
15005 			 */
15006 			cportinfo->cport_dev_attach_time = 0;
15007 			sata_log(sata_hba_inst, CE_WARN,
15008 			    "SATA device detected at port %d", saddr->cport);
15009 
15010 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
15011 				sata_drive_info_t new_sdinfo;
15012 
15013 				/* Log device info data */
15014 				new_sdinfo = *(SATA_CPORTINFO_DRV_INFO(
15015 				    cportinfo));
15016 				sata_show_drive_info(sata_hba_inst,
15017 				    &new_sdinfo);
15018 			}
15019 
15020 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15021 			    saddr->cport)->cport_mutex);
15022 
15023 			/*
15024 			 * Make sure that there is no target node for that
15025 			 * device. If so, release it. It should not happen,
15026 			 * unless we had problem removing the node when
15027 			 * device was detached.
15028 			 */
15029 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
15030 			    saddr->cport);
15031 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15032 			    saddr->cport)->cport_mutex);
15033 			if (tdip != NULL) {
15034 
15035 #ifdef SATA_DEBUG
15036 				if ((cportinfo->cport_event_flags &
15037 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
15038 					sata_log(sata_hba_inst, CE_WARN,
15039 					    "sata_process_device_attached: "
15040 					    "old device target node exists!");
15041 #endif
15042 				/*
15043 				 * target node exists - try to unconfigure
15044 				 * device and remove the node.
15045 				 */
15046 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15047 				    saddr->cport)->cport_mutex);
15048 				rval = ndi_devi_offline(tdip,
15049 				    NDI_DEVI_REMOVE);
15050 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15051 				    saddr->cport)->cport_mutex);
15052 
15053 				if (rval == NDI_SUCCESS) {
15054 					cportinfo->cport_event_flags &=
15055 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
15056 					cportinfo->cport_tgtnode_clean = B_TRUE;
15057 				} else {
15058 					/*
15059 					 * PROBLEM - the target node remained
15060 					 * and it belongs to a previously
15061 					 * attached device.
15062 					 * This happens when the file was open
15063 					 * or the node was waiting for
15064 					 * resources at the time the
15065 					 * associated device was removed.
15066 					 * Instruct event daemon to retry the
15067 					 * cleanup later.
15068 					 */
15069 					sata_log(sata_hba_inst,
15070 					    CE_WARN,
15071 					    "Application(s) accessing "
15072 					    "previously attached SATA "
15073 					    "device have to release "
15074 					    "it before newly inserted "
15075 					    "device can be made accessible.",
15076 					    saddr->cport);
15077 					cportinfo->cport_event_flags |=
15078 					    SATA_EVNT_TARGET_NODE_CLEANUP;
15079 					cportinfo->cport_tgtnode_clean =
15080 					    B_FALSE;
15081 				}
15082 			}
15083 			if (sata_auto_online != 0) {
15084 				cportinfo->cport_event_flags |=
15085 				    SATA_EVNT_AUTOONLINE_DEVICE;
15086 			}
15087 
15088 		}
15089 	} else {
15090 		cportinfo->cport_dev_attach_time = 0;
15091 	}
15092 
15093 	event_flags = cportinfo->cport_event_flags;
15094 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15095 	if (event_flags != 0) {
15096 		mutex_enter(&sata_hba_inst->satahba_mutex);
15097 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15098 		mutex_exit(&sata_hba_inst->satahba_mutex);
15099 		mutex_enter(&sata_mutex);
15100 		sata_event_pending |= SATA_EVNT_MAIN;
15101 		mutex_exit(&sata_mutex);
15102 	}
15103 }
15104 
15105 
15106 /*
15107  * Device Target Node Cleanup Event processing.
15108  * If the target node associated with a sata port device is in
15109  * DEVI_DEVICE_REMOVED state, an attempt is made to remove it.
15110  * If the target node cannot be removed, the event flag is left intact,
15111  * so that event daemon may re-run this function later.
15112  *
15113  * This function cannot be called in interrupt context (it may sleep).
15114  *
15115  * NOTE: Processes cport events only, not port multiplier ports.
15116  */
15117 static void
15118 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
15119     sata_address_t *saddr)
15120 {
15121 	sata_cport_info_t *cportinfo;
15122 	dev_info_t *tdip;
15123 
15124 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15125 	    "Processing port %d device target node cleanup", saddr->cport);
15126 
15127 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15128 
15129 	/*
15130 	 * Check if there is target node for that device and it is in the
15131 	 * DEVI_DEVICE_REMOVED state. If so, release it.
15132 	 */
15133 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
15134 	if (tdip != NULL) {
15135 		/*
15136 		 * target node exists - check if it is target node of
15137 		 * a removed device.
15138 		 */
15139 		if (sata_check_device_removed(tdip) == B_TRUE) {
15140 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15141 			    "sata_process_target_node_cleanup: "
15142 			    "old device target node exists!", NULL);
15143 			/*
15144 			 * Unconfigure and remove the target node
15145 			 */
15146 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) ==
15147 			    NDI_SUCCESS) {
15148 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15149 				    saddr->cport)->cport_mutex);
15150 				cportinfo->cport_event_flags &=
15151 				    ~SATA_EVNT_TARGET_NODE_CLEANUP;
15152 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15153 				    saddr->cport)->cport_mutex);
15154 				return;
15155 			}
15156 			/*
15157 			 * Event daemon will retry the cleanup later.
15158 			 */
15159 			mutex_enter(&sata_hba_inst->satahba_mutex);
15160 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15161 			mutex_exit(&sata_hba_inst->satahba_mutex);
15162 			mutex_enter(&sata_mutex);
15163 			sata_event_pending |= SATA_EVNT_MAIN;
15164 			mutex_exit(&sata_mutex);
15165 		}
15166 	} else {
15167 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15168 		    saddr->cport)->cport_mutex);
15169 		cportinfo->cport_event_flags &=
15170 		    ~SATA_EVNT_TARGET_NODE_CLEANUP;
15171 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15172 		    saddr->cport)->cport_mutex);
15173 	}
15174 }
15175 
15176 /*
15177  * Device AutoOnline Event processing.
15178  * If attached device is to be onlined, an attempt is made to online this
15179  * device, but only if there is no lingering (old) target node present.
15180  * If the device cannot be onlined, the event flag is left intact,
15181  * so that event daemon may re-run this function later.
15182  *
15183  * This function cannot be called in interrupt context (it may sleep).
15184  *
15185  * NOTE: Processes cport events only, not port multiplier ports.
15186  */
15187 static void
15188 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst,
15189     sata_address_t *saddr)
15190 {
15191 	sata_cport_info_t *cportinfo;
15192 	sata_drive_info_t *sdinfo;
15193 	sata_device_t sata_device;
15194 	dev_info_t *tdip;
15195 
15196 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15197 	    "Processing port %d attached device auto-onlining", saddr->cport);
15198 
15199 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15200 
15201 	/*
15202 	 * Check if device is present and recognized. If not, reset event.
15203 	 */
15204 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15205 	if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
15206 		/* Nothing to online */
15207 		cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
15208 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15209 		    saddr->cport)->cport_mutex);
15210 		return;
15211 	}
15212 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15213 
15214 	/*
15215 	 * Check if there is target node for this device and if it is in the
15216 	 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep
15217 	 * the event for later processing.
15218 	 */
15219 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
15220 	if (tdip != NULL) {
15221 		/*
15222 		 * target node exists - check if it is target node of
15223 		 * a removed device.
15224 		 */
15225 		if (sata_check_device_removed(tdip) == B_TRUE) {
15226 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15227 			    "sata_process_device_autoonline: "
15228 			    "old device target node exists!", NULL);
15229 			/*
15230 			 * Event daemon will retry device onlining later.
15231 			 */
15232 			mutex_enter(&sata_hba_inst->satahba_mutex);
15233 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15234 			mutex_exit(&sata_hba_inst->satahba_mutex);
15235 			mutex_enter(&sata_mutex);
15236 			sata_event_pending |= SATA_EVNT_MAIN;
15237 			mutex_exit(&sata_mutex);
15238 			return;
15239 		}
15240 		/*
15241 		 * If the target node is not in the 'removed" state, assume
15242 		 * that it belongs to this device. There is nothing more to do,
15243 		 * but reset the event.
15244 		 */
15245 	} else {
15246 
15247 		/*
15248 		 * Try to online the device
15249 		 * If there is any reset-related event, remove it. We are
15250 		 * configuring the device and no state restoring is needed.
15251 		 */
15252 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15253 		    saddr->cport)->cport_mutex);
15254 		sata_device.satadev_addr = *saddr;
15255 		if (saddr->qual == SATA_ADDR_CPORT)
15256 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
15257 		else
15258 			sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
15259 		sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
15260 		if (sdinfo != NULL) {
15261 			if (sdinfo->satadrv_event_flags &
15262 			    (SATA_EVNT_DEVICE_RESET |
15263 			    SATA_EVNT_INPROC_DEVICE_RESET))
15264 				sdinfo->satadrv_event_flags = 0;
15265 			sdinfo->satadrv_event_flags |=
15266 			    SATA_EVNT_CLEAR_DEVICE_RESET;
15267 
15268 			/* Need to create a new target node. */
15269 			cportinfo->cport_tgtnode_clean = B_TRUE;
15270 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15271 			    saddr->cport)->cport_mutex);
15272 			tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
15273 			    sata_hba_inst, &sata_device.satadev_addr);
15274 			if (tdip == NULL) {
15275 				/*
15276 				 * Configure (onlining) failed.
15277 				 * We will NOT retry
15278 				 */
15279 				SATA_LOG_D((sata_hba_inst, CE_WARN,
15280 				    "sata_process_device_autoonline: "
15281 				    "configuring SATA device at port %d failed",
15282 				    saddr->cport));
15283 			}
15284 		} else {
15285 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15286 			    saddr->cport)->cport_mutex);
15287 		}
15288 
15289 	}
15290 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15291 	cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
15292 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15293 	    saddr->cport)->cport_mutex);
15294 }
15295 
15296 
15297 static void
15298 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr,
15299     int hint)
15300 {
15301 	char ap[MAXPATHLEN];
15302 	nvlist_t *ev_attr_list = NULL;
15303 	int err;
15304 
15305 	/* Allocate and build sysevent attribute list */
15306 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP);
15307 	if (err != 0) {
15308 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15309 		    "sata_gen_sysevent: "
15310 		    "cannot allocate memory for sysevent attributes\n"));
15311 		return;
15312 	}
15313 	/* Add hint attribute */
15314 	err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint));
15315 	if (err != 0) {
15316 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15317 		    "sata_gen_sysevent: "
15318 		    "failed to add DR_HINT attr for sysevent"));
15319 		nvlist_free(ev_attr_list);
15320 		return;
15321 	}
15322 	/*
15323 	 * Add AP attribute.
15324 	 * Get controller pathname and convert it into AP pathname by adding
15325 	 * a target number.
15326 	 */
15327 	(void) snprintf(ap, MAXPATHLEN, "/devices");
15328 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap));
15329 	(void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d",
15330 	    SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual));
15331 
15332 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap);
15333 	if (err != 0) {
15334 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15335 		    "sata_gen_sysevent: "
15336 		    "failed to add DR_AP_ID attr for sysevent"));
15337 		nvlist_free(ev_attr_list);
15338 		return;
15339 	}
15340 
15341 	/* Generate/log sysevent */
15342 	err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR,
15343 	    ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP);
15344 	if (err != DDI_SUCCESS) {
15345 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15346 		    "sata_gen_sysevent: "
15347 		    "cannot log sysevent, err code %x\n", err));
15348 	}
15349 
15350 	nvlist_free(ev_attr_list);
15351 }
15352 
15353 
15354 
15355 
15356 /*
15357  * Set DEVI_DEVICE_REMOVED state in the SATA device target node.
15358  */
15359 static void
15360 sata_set_device_removed(dev_info_t *tdip)
15361 {
15362 	int circ;
15363 
15364 	ASSERT(tdip != NULL);
15365 
15366 	ndi_devi_enter(tdip, &circ);
15367 	mutex_enter(&DEVI(tdip)->devi_lock);
15368 	DEVI_SET_DEVICE_REMOVED(tdip);
15369 	mutex_exit(&DEVI(tdip)->devi_lock);
15370 	ndi_devi_exit(tdip, circ);
15371 }
15372 
15373 
15374 /*
15375  * Set internal event instructing event daemon to try
15376  * to perform the target node cleanup.
15377  */
15378 static void
15379 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
15380     sata_address_t *saddr)
15381 {
15382 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15383 	SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |=
15384 	    SATA_EVNT_TARGET_NODE_CLEANUP;
15385 	SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_tgtnode_clean =
15386 	    B_FALSE;
15387 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15388 	mutex_enter(&sata_hba_inst->satahba_mutex);
15389 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15390 	mutex_exit(&sata_hba_inst->satahba_mutex);
15391 	mutex_enter(&sata_mutex);
15392 	sata_event_pending |= SATA_EVNT_MAIN;
15393 	mutex_exit(&sata_mutex);
15394 }
15395 
15396 
15397 /*
15398  * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state,
15399  * i.e. check if the target node state indicates that it belongs to a removed
15400  * device.
15401  *
15402  * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state,
15403  * B_FALSE otherwise.
15404  *
15405  * NOTE: No port multiplier support.
15406  */
15407 static boolean_t
15408 sata_check_device_removed(dev_info_t *tdip)
15409 {
15410 	ASSERT(tdip != NULL);
15411 
15412 	if (DEVI_IS_DEVICE_REMOVED(tdip))
15413 		return (B_TRUE);
15414 	else
15415 		return (B_FALSE);
15416 }
15417 
15418 /* ************************ FAULT INJECTTION **************************** */
15419 
15420 #ifdef SATA_INJECT_FAULTS
15421 
15422 static	uint32_t sata_fault_count = 0;
15423 static	uint32_t sata_fault_suspend_count = 0;
15424 
15425 /*
15426  * Inject sata pkt fault
15427  * It modifies returned values of the sata packet.
15428  * First argument is the pointer to the executed sata packet.
15429  * The second argument specifies SATA command to be affected (not all commands
15430  * are instrumented).
15431  * Third argument is a pointer to a value returned by the HBA tran_start
15432  * function.
15433  * Fourth argument specifies injected error. Injected sata packet faults
15434  * are the satapkt_reason values.
15435  * SATA_PKT_BUSY		-1	Not completed, busy
15436  * SATA_PKT_DEV_ERROR		1	Device reported error
15437  * SATA_PKT_QUEUE_FULL		2	Not accepted, queue full
15438  * SATA_PKT_PORT_ERROR		3	Not completed, port error
15439  * SATA_PKT_CMD_UNSUPPORTED	4	Cmd unsupported
15440  * SATA_PKT_ABORTED		5	Aborted by request
15441  * SATA_PKT_TIMEOUT		6	Operation timeut
15442  * SATA_PKT_RESET		7	Aborted by reset request
15443  *
15444  * sata_inject_fault_count variable specifies number of times in row the
15445  * error is injected. Value of -1 specifies permanent fault, ie. every time
15446  * the fault injection pointnis reached, the fault is injected and anu pause
15447  * between fault injection specified by sata_inject_fault_pause_count is
15448  * ignored).
15449  *
15450  * sata_inject_fault_pause_count variable specifies number of times a fault
15451  * injection is bypassed (pause between fault injections).
15452  * If set to 0, a fault is injected only a number of times specified by
15453  * sata_inject_fault_count.
15454  *
15455  * The fault counts are static, so for periodic errors they have to be manually
15456  * reset to start repetition sequence from scratch.
15457  * If the original value returned by the HBA tran_start function is not
15458  * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error
15459  * is injected (to avoid masking real problems);
15460  *
15461  * NOTE: In its current incarnation, this function should be invoked only for
15462  * commands executed in SYNCHRONOUS mode.
15463  */
15464 
15465 
15466 static	void
15467 sata_inject_pkt_fault(sata_pkt_t *spkt, uint8_t cmd, int *rval,
15468     int fault)
15469 {
15470 	if (fault == 0)
15471 		return;
15472 	if (sata_inject_fault_count == 0)
15473 		return;
15474 
15475 	if (spkt->satapkt_cmd.satacmd_cmd_reg != cmd)
15476 		return;
15477 
15478 	if (*rval != SATA_TRAN_ACCEPTED ||
15479 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
15480 		sata_fault_count = 0;
15481 		sata_fault_suspend_count = 0;
15482 		return;
15483 	}
15484 	if (sata_fault_count == 0 && sata_fault_suspend_count != 0) {
15485 		/* Pause in the injection */
15486 		sata_fault_suspend_count -= 1;
15487 		return;
15488 	}
15489 
15490 	if (sata_fault_count == 0 && sata_fault_suspend_count == 0) {
15491 		/*
15492 		 * Init inject fault cycle. If fault count is set to -1,
15493 		 * it is a permanent fault.
15494 		 */
15495 		if (sata_inject_fault_count != -1) {
15496 			sata_fault_count = sata_inject_fault_count;
15497 			sata_fault_suspend_count =
15498 			    sata_inject_fault_pause_count;
15499 			if (sata_fault_suspend_count == 0)
15500 				sata_inject_fault_count = 0;
15501 		}
15502 	}
15503 
15504 	if (sata_fault_count != 0)
15505 		sata_fault_count -= 1;
15506 
15507 	switch (fault) {
15508 	case SATA_PKT_BUSY:
15509 		*rval = SATA_TRAN_BUSY;
15510 		spkt->satapkt_reason = SATA_PKT_BUSY;
15511 		break;
15512 
15513 	case SATA_PKT_QUEUE_FULL:
15514 		*rval = SATA_TRAN_QUEUE_FULL;
15515 		spkt->satapkt_reason = SATA_PKT_QUEUE_FULL;
15516 		break;
15517 
15518 	case SATA_PKT_CMD_UNSUPPORTED:
15519 		*rval = SATA_TRAN_CMD_UNSUPPORTED;
15520 		spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED;
15521 		break;
15522 
15523 	case SATA_PKT_PORT_ERROR:
15524 		/* This is "rejected" command */
15525 		*rval = SATA_TRAN_PORT_ERROR;
15526 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
15527 		/* Additional error setup could be done here - port state */
15528 		break;
15529 
15530 	case SATA_PKT_DEV_ERROR:
15531 		spkt->satapkt_reason = SATA_PKT_DEV_ERROR;
15532 		/*
15533 		 * Additional error setup could be done here
15534 		 */
15535 		break;
15536 
15537 	case SATA_PKT_ABORTED:
15538 		spkt->satapkt_reason = SATA_PKT_ABORTED;
15539 		break;
15540 
15541 	case SATA_PKT_TIMEOUT:
15542 		spkt->satapkt_reason = SATA_PKT_TIMEOUT;
15543 		/* Additional error setup could be done here */
15544 		break;
15545 
15546 	case SATA_PKT_RESET:
15547 		spkt->satapkt_reason = SATA_PKT_RESET;
15548 		/*
15549 		 * Additional error setup could be done here - device reset
15550 		 */
15551 		break;
15552 
15553 	default:
15554 		break;
15555 	}
15556 }
15557 
15558 #endif
15559