xref: /titanic_41/usr/src/uts/common/io/sata/impl/sata.c (revision ae5b046d8f8cec187d40041c4b74b43f561d5ac7)
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	int sata_probe_device(sata_hba_inst_t *, sata_device_t *);
261 static	sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *,
262     sata_device_t *);
263 static 	int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *);
264 static	void sata_reidentify_device(sata_pkt_txlate_t *);
265 static	struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int);
266 static 	void sata_free_local_buffer(sata_pkt_txlate_t *);
267 static 	uint64_t sata_check_capacity(sata_drive_info_t *);
268 void 	sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *,
269     ddi_dma_attr_t *);
270 static 	int sata_fetch_device_identify_data(sata_hba_inst_t *,
271     sata_drive_info_t *);
272 static	void sata_update_port_info(sata_hba_inst_t *, sata_device_t *);
273 static	void sata_update_port_scr(sata_port_scr_t *, sata_device_t *);
274 static	int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *);
275 static	int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int);
276 static	int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int);
277 static	int sata_set_drive_features(sata_hba_inst_t *,
278     sata_drive_info_t *, int flag);
279 static	void sata_init_write_cache_mode(sata_drive_info_t *sdinfo);
280 static	int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *);
281 static	void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *,
282     uint8_t *);
283 static	int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *,
284     struct scsi_inquiry *);
285 static	int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *);
286 static	int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *);
287 static	int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *);
288 static	int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *);
289 static	int sata_mode_select_page_8(sata_pkt_txlate_t *,
290     struct mode_cache_scsi3 *, int, int *, int *, int *);
291 static	int sata_mode_select_page_1c(sata_pkt_txlate_t *,
292     struct mode_info_excpt_page *, int, int *, int *, int *);
293 static	int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *);
294 static	int sata_mode_select_page_30(sata_pkt_txlate_t *,
295     struct mode_acoustic_management *, int, int *, int *, int *);
296 
297 static	int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *);
298 static	int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *,
299     sata_hba_inst_t *);
300 static	int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *,
301     sata_hba_inst_t *);
302 static	int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *,
303     sata_hba_inst_t *);
304 static	void sata_save_drive_settings(sata_drive_info_t *);
305 static	void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *);
306 static	void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...);
307 static	int sata_fetch_smart_return_status(sata_hba_inst_t *,
308     sata_drive_info_t *);
309 static	int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *,
310     struct smart_data *);
311 static	int sata_smart_selftest_log(sata_hba_inst_t *,
312     sata_drive_info_t *,
313     struct smart_selftest_log *);
314 static	int sata_ext_smart_selftest_read_log(sata_hba_inst_t *,
315     sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t);
316 static	int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *,
317     uint8_t *, uint8_t, uint8_t);
318 static	int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *,
319     struct read_log_ext_directory *);
320 static	void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int);
321 static	void sata_xlate_errors(sata_pkt_txlate_t *);
322 static	void sata_decode_device_error(sata_pkt_txlate_t *,
323     struct scsi_extended_sense *);
324 static	void sata_set_device_removed(dev_info_t *);
325 static	boolean_t sata_check_device_removed(dev_info_t *);
326 static	void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *);
327 static	int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *,
328     sata_drive_info_t *);
329 static	int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *,
330     sata_drive_info_t *);
331 static	void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *);
332 static	void sata_fixed_sense_data_preset(struct scsi_extended_sense *);
333 static  void sata_target_devid_register(dev_info_t *, sata_drive_info_t *);
334 static  int sata_check_modser(char *, int);
335 
336 
337 
338 /*
339  * SATA Framework will ignore SATA HBA driver cb_ops structure and
340  * register following one with SCSA framework.
341  * Open & close are provided, so scsi framework will not use its own
342  */
343 static struct cb_ops sata_cb_ops = {
344 	sata_hba_open,			/* open */
345 	sata_hba_close,			/* close */
346 	nodev,				/* strategy */
347 	nodev,				/* print */
348 	nodev,				/* dump */
349 	nodev,				/* read */
350 	nodev,				/* write */
351 	sata_hba_ioctl,			/* ioctl */
352 	nodev,				/* devmap */
353 	nodev,				/* mmap */
354 	nodev,				/* segmap */
355 	nochpoll,			/* chpoll */
356 	ddi_prop_op,			/* cb_prop_op */
357 	0,				/* streamtab */
358 	D_NEW | D_MP,			/* cb_flag */
359 	CB_REV,				/* rev */
360 	nodev,				/* aread */
361 	nodev				/* awrite */
362 };
363 
364 
365 extern struct mod_ops mod_miscops;
366 extern uchar_t	scsi_cdb_size[];
367 
368 static struct modlmisc modlmisc = {
369 	&mod_miscops,			/* Type of module */
370 	"SATA Module v%I%"		/* module name */
371 };
372 
373 
374 static struct modlinkage modlinkage = {
375 	MODREV_1,
376 	(void *)&modlmisc,
377 	NULL
378 };
379 
380 /*
381  * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero,
382  * i.e. when scsi_pkt has not timeout specified.
383  */
384 static int sata_default_pkt_time = 60;	/* 60 seconds */
385 
386 /*
387  * Intermediate buffer device access attributes - they are required,
388  * but not necessarily used.
389  */
390 static ddi_device_acc_attr_t sata_acc_attr = {
391 	DDI_DEVICE_ATTR_V0,
392 	DDI_STRUCTURE_LE_ACC,
393 	DDI_STRICTORDER_ACC
394 };
395 
396 
397 /*
398  * Mutexes protecting structures in multithreaded operations.
399  * Because events are relatively rare, a single global mutex protecting
400  * data structures should be sufficient. To increase performance, add
401  * separate mutex per each sata port and use global mutex only to protect
402  * common data structures.
403  */
404 static	kmutex_t sata_mutex;		/* protects sata_hba_list */
405 static	kmutex_t sata_log_mutex;	/* protects log */
406 
407 static 	char sata_log_buf[256];
408 
409 /* Default write cache setting for SATA hard disks */
410 int	sata_write_cache = 1;		/* enabled */
411 
412 /* Default write cache setting for SATA ATAPI CD/DVD */
413 int 	sata_atapicdvd_write_cache = 1; /* enabled */
414 
415 /*
416  * Linked list of HBA instances
417  */
418 static 	sata_hba_inst_t *sata_hba_list = NULL;
419 static 	sata_hba_inst_t *sata_hba_list_tail = NULL;
420 /*
421  * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran
422  * structure and in sata soft state.
423  */
424 
425 /*
426  * Event daemon related variables
427  */
428 static 	kmutex_t sata_event_mutex;
429 static 	kcondvar_t sata_event_cv;
430 static 	kthread_t *sata_event_thread = NULL;
431 static 	int sata_event_thread_terminate = 0;
432 static 	int sata_event_pending = 0;
433 static 	int sata_event_thread_active = 0;
434 extern 	pri_t minclsyspri;
435 
436 /*
437  * NCQ error recovery command
438  */
439 static const sata_cmd_t sata_rle_cmd = {
440 	SATA_CMD_REV,
441 	NULL,
442 	{
443 		SATA_DIR_READ
444 	},
445 	ATA_ADDR_LBA48,
446 	0,
447 	0,
448 	0,
449 	0,
450 	0,
451 	1,
452 	READ_LOG_EXT_NCQ_ERROR_RECOVERY,
453 	0,
454 	0,
455 	0,
456 	SATAC_READ_LOG_EXT,
457 	0,
458 	0,
459 	0,
460 };
461 
462 /*
463  * ATAPI error recovery CDB
464  */
465 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = {
466 	SCMD_REQUEST_SENSE,
467 	0,			/* Only fixed RQ format is supported */
468 	0,
469 	0,
470 	SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */
471 	0
472 };
473 
474 
475 /* Warlock directives */
476 
477 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran))
478 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device))
479 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops))
480 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense))
481 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status))
482 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr))
483 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t))
484 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state))
485 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state))
486 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list))
487 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list))
488 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next))
489 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev))
490 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \
491     sata_hba_inst::satahba_scsi_tran))
492 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran))
493 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip))
494 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached))
495 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port))
496 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex,
497     sata_hba_inst::satahba_event_flags))
498 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
499     sata_cport_info::cport_devp))
500 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp))
501 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr))
502 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
503     sata_cport_info::cport_dev_type))
504 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type))
505 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
506     sata_cport_info::cport_state))
507 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state))
508 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
509     sata_pmport_info::pmport_state))
510 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state))
511 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type))
512 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive))
513 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port))
514 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports))
515 #ifdef SATA_DEBUG
516 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count))
517 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count))
518 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace))
519 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index))
520 #endif
521 
522 /* End of warlock directives */
523 
524 /* ************** loadable module configuration functions ************** */
525 
526 int
527 _init()
528 {
529 	int rval;
530 
531 	mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL);
532 	mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL);
533 	mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL);
534 	cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL);
535 	if ((rval = mod_install(&modlinkage)) != 0) {
536 #ifdef SATA_DEBUG
537 		cmn_err(CE_WARN, "sata: _init: mod_install failed\n");
538 #endif
539 		mutex_destroy(&sata_log_mutex);
540 		cv_destroy(&sata_event_cv);
541 		mutex_destroy(&sata_event_mutex);
542 		mutex_destroy(&sata_mutex);
543 	}
544 	return (rval);
545 }
546 
547 int
548 _fini()
549 {
550 	int rval;
551 
552 	if ((rval = mod_remove(&modlinkage)) != 0)
553 		return (rval);
554 
555 	mutex_destroy(&sata_log_mutex);
556 	cv_destroy(&sata_event_cv);
557 	mutex_destroy(&sata_event_mutex);
558 	mutex_destroy(&sata_mutex);
559 	return (rval);
560 }
561 
562 int
563 _info(struct modinfo *modinfop)
564 {
565 	return (mod_info(&modlinkage, modinfop));
566 }
567 
568 
569 
570 /* ********************* SATA HBA entry points ********************* */
571 
572 
573 /*
574  * Called by SATA HBA from _init().
575  * Registers HBA driver instance/sata framework pair with scsi framework, by
576  * calling scsi_hba_init().
577  *
578  * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used
579  * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver
580  * cb_ops pointer in SATA HBA driver dev_ops structure.
581  * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors.
582  *
583  * Return status of the scsi_hba_init() is returned to a calling SATA HBA
584  * driver.
585  */
586 int
587 sata_hba_init(struct modlinkage *modlp)
588 {
589 	int rval;
590 	struct dev_ops *hba_ops;
591 
592 	SATADBG1(SATA_DBG_HBA_IF, NULL,
593 	    "sata_hba_init: name %s \n",
594 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
595 	/*
596 	 * Fill-up cb_ops and dev_ops when necessary
597 	 */
598 	hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
599 	/*
600 	 * Provide pointer to SATA dev_ops
601 	 */
602 	hba_ops->devo_cb_ops = &sata_cb_ops;
603 
604 	/*
605 	 * Register SATA HBA with SCSI framework
606 	 */
607 	if ((rval = scsi_hba_init(modlp)) != 0) {
608 		SATADBG1(SATA_DBG_HBA_IF, NULL,
609 		    "sata_hba_init: scsi hba init failed\n", NULL);
610 		return (rval);
611 	}
612 
613 	return (0);
614 }
615 
616 
617 /* HBA attach stages */
618 #define	HBA_ATTACH_STAGE_SATA_HBA_INST	1
619 #define	HBA_ATTACH_STAGE_SCSI_ATTACHED	2
620 #define	HBA_ATTACH_STAGE_SETUP		4
621 #define	HBA_ATTACH_STAGE_LINKED		8
622 
623 
624 /*
625  *
626  * Called from SATA HBA driver's attach routine to attach an instance of
627  * the HBA.
628  *
629  * For DDI_ATTACH command:
630  * sata_hba_inst structure is allocated here and initialized with pointers to
631  * SATA framework implementation of required scsi tran functions.
632  * The scsi_tran's tran_hba_private field is used by SATA Framework to point
633  * to the soft structure (sata_hba_inst) allocated by SATA framework for
634  * SATA HBA instance related data.
635  * The scsi_tran's tran_hba_private field is used by SATA framework to
636  * store a pointer to per-HBA-instance of sata_hba_inst structure.
637  * The sata_hba_inst structure is cross-linked to scsi tran structure.
638  * Among other info, a pointer to sata_hba_tran structure is stored in
639  * sata_hba_inst. The sata_hba_inst structures for different HBA instances are
640  * linked together into the list, pointed to by sata_hba_list.
641  * On the first HBA instance attach the sata event thread is initialized.
642  * Attachment points are created for all SATA ports of the HBA being attached.
643  * All HBA instance's SATA ports are probed and type of plugged devices is
644  * determined. For each device of a supported type, a target node is created.
645  *
646  * DDI_SUCCESS is returned when attachment process is successful,
647  * DDI_FAILURE is returned otherwise.
648  *
649  * For DDI_RESUME command:
650  * Not implemented at this time (postponed until phase 2 of the development).
651  */
652 int
653 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran,
654     ddi_attach_cmd_t cmd)
655 {
656 	sata_hba_inst_t	*sata_hba_inst;
657 	scsi_hba_tran_t *scsi_tran = NULL;
658 	int hba_attach_state = 0;
659 	char taskq_name[MAXPATHLEN];
660 
661 	SATADBG3(SATA_DBG_HBA_IF, NULL,
662 	    "sata_hba_attach: node %s (%s%d)\n",
663 	    ddi_node_name(dip), ddi_driver_name(dip),
664 	    ddi_get_instance(dip));
665 
666 	if (cmd == DDI_RESUME) {
667 		/*
668 		 * Postponed until phase 2 of the development
669 		 */
670 		return (DDI_FAILURE);
671 	}
672 
673 	if (cmd != DDI_ATTACH) {
674 		return (DDI_FAILURE);
675 	}
676 
677 	/* cmd == DDI_ATTACH */
678 
679 	if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) {
680 		SATA_LOG_D((NULL, CE_WARN,
681 		    "sata_hba_attach: invalid sata_hba_tran"));
682 		return (DDI_FAILURE);
683 	}
684 	/*
685 	 * Allocate and initialize SCSI tran structure.
686 	 * SATA copy of tran_bus_config is provided to create port nodes.
687 	 */
688 	scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
689 	if (scsi_tran == NULL)
690 		return (DDI_FAILURE);
691 	/*
692 	 * Allocate soft structure for SATA HBA instance.
693 	 * There is a separate softstate for each HBA instance.
694 	 */
695 	sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP);
696 	ASSERT(sata_hba_inst != NULL); /* this should not fail */
697 	mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL);
698 	hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST;
699 
700 	/*
701 	 * scsi_trans's tran_hba_private is used by SATA Framework to point to
702 	 * soft structure allocated by SATA framework for
703 	 * SATA HBA instance related data.
704 	 */
705 	scsi_tran->tran_hba_private	= sata_hba_inst;
706 	scsi_tran->tran_tgt_private	= NULL;
707 
708 	scsi_tran->tran_tgt_init	= sata_scsi_tgt_init;
709 	scsi_tran->tran_tgt_probe	= sata_scsi_tgt_probe;
710 	scsi_tran->tran_tgt_free	= sata_scsi_tgt_free;
711 
712 	scsi_tran->tran_start		= sata_scsi_start;
713 	scsi_tran->tran_reset		= sata_scsi_reset;
714 	scsi_tran->tran_abort		= sata_scsi_abort;
715 	scsi_tran->tran_getcap		= sata_scsi_getcap;
716 	scsi_tran->tran_setcap		= sata_scsi_setcap;
717 	scsi_tran->tran_init_pkt	= sata_scsi_init_pkt;
718 	scsi_tran->tran_destroy_pkt	= sata_scsi_destroy_pkt;
719 
720 	scsi_tran->tran_dmafree		= sata_scsi_dmafree;
721 	scsi_tran->tran_sync_pkt	= sata_scsi_sync_pkt;
722 
723 	scsi_tran->tran_reset_notify	= NULL;
724 	scsi_tran->tran_get_bus_addr	= NULL;
725 	scsi_tran->tran_quiesce		= NULL;
726 	scsi_tran->tran_unquiesce	= NULL;
727 	scsi_tran->tran_bus_reset	= NULL;
728 
729 	if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr,
730 	    scsi_tran, 0) != DDI_SUCCESS) {
731 #ifdef SATA_DEBUG
732 		cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed",
733 		    ddi_driver_name(dip), ddi_get_instance(dip));
734 #endif
735 		goto fail;
736 	}
737 	hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED;
738 
739 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) {
740 		if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
741 		    "sata", 1) != DDI_PROP_SUCCESS) {
742 			SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: "
743 			    "failed to create hba sata prop"));
744 			goto fail;
745 		}
746 	}
747 
748 	/*
749 	 * Save pointers in hba instance soft state.
750 	 */
751 	sata_hba_inst->satahba_scsi_tran = scsi_tran;
752 	sata_hba_inst->satahba_tran = sata_tran;
753 	sata_hba_inst->satahba_dip = dip;
754 
755 	/*
756 	 * Create a task queue to handle emulated commands completion
757 	 * Use node name, dash, instance number as the queue name.
758 	 */
759 	taskq_name[0] = '\0';
760 	(void) strlcat(taskq_name, DEVI(dip)->devi_node_name,
761 	    sizeof (taskq_name));
762 	(void) snprintf(taskq_name + strlen(taskq_name),
763 	    sizeof (taskq_name) - strlen(taskq_name),
764 	    "-%d", DEVI(dip)->devi_instance);
765 	sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1,
766 	    minclsyspri, 1, sata_tran->sata_tran_hba_num_cports,
767 	    TASKQ_DYNAMIC);
768 
769 	hba_attach_state |= HBA_ATTACH_STAGE_SETUP;
770 
771 	/*
772 	 * Create events thread if not created yet.
773 	 */
774 	sata_event_thread_control(1);
775 
776 	/*
777 	 * Link this hba instance into the list.
778 	 */
779 	mutex_enter(&sata_mutex);
780 
781 	if (sata_hba_list == NULL) {
782 		/*
783 		 * The first instance of HBA is attached.
784 		 * Set current/active default maximum NCQ/TCQ queue depth for
785 		 * all SATA devices. It is done here and now, to eliminate the
786 		 * possibility of the dynamic, programatic modification of the
787 		 * queue depth via global (and public) sata_max_queue_depth
788 		 * variable (this would require special handling in HBA drivers)
789 		 */
790 		sata_current_max_qdepth = sata_max_queue_depth;
791 		if (sata_current_max_qdepth > 32)
792 			sata_current_max_qdepth = 32;
793 		else if (sata_current_max_qdepth < 1)
794 			sata_current_max_qdepth = 1;
795 	}
796 
797 	sata_hba_inst->satahba_next = NULL;
798 	sata_hba_inst->satahba_prev = sata_hba_list_tail;
799 	if (sata_hba_list == NULL) {
800 		sata_hba_list = sata_hba_inst;
801 	}
802 	if (sata_hba_list_tail != NULL) {
803 		sata_hba_list_tail->satahba_next = sata_hba_inst;
804 	}
805 	sata_hba_list_tail = sata_hba_inst;
806 	mutex_exit(&sata_mutex);
807 	hba_attach_state |= HBA_ATTACH_STAGE_LINKED;
808 
809 	/*
810 	 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl
811 	 * SATA HBA driver should not use its own open/close entry points.
812 	 *
813 	 * Make sure that instance number doesn't overflow
814 	 * when forming minor numbers.
815 	 */
816 	ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT));
817 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
818 	    INST2DEVCTL(ddi_get_instance(dip)),
819 	    DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) {
820 #ifdef SATA_DEBUG
821 		cmn_err(CE_WARN, "sata_hba_attach: "
822 		    "cannot create devctl minor node");
823 #endif
824 		goto fail;
825 	}
826 
827 
828 	/*
829 	 * Set-up kstats here, if necessary.
830 	 * (postponed until phase 2 of the development).
831 	 */
832 
833 
834 	/*
835 	 * Probe controller ports. This operation will describe a current
836 	 * controller/port/multipliers/device configuration and will create
837 	 * attachment points.
838 	 * We may end-up with just a controller with no devices attached.
839 	 * For the ports with a supported device attached, device target nodes
840 	 * are created and devices are initialized.
841 	 */
842 	sata_probe_ports(sata_hba_inst);
843 
844 	sata_hba_inst->satahba_attached = 1;
845 	return (DDI_SUCCESS);
846 
847 fail:
848 	if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) {
849 		(void) sata_remove_hba_instance(dip);
850 		if (sata_hba_list == NULL)
851 			sata_event_thread_control(0);
852 	}
853 
854 	if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) {
855 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
856 		taskq_destroy(sata_hba_inst->satahba_taskq);
857 	}
858 
859 	if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED)
860 		(void) scsi_hba_detach(dip);
861 
862 	if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) {
863 		mutex_destroy(&sata_hba_inst->satahba_mutex);
864 		kmem_free((void *)sata_hba_inst,
865 		    sizeof (struct sata_hba_inst));
866 		scsi_hba_tran_free(scsi_tran);
867 	}
868 
869 	sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed",
870 	    ddi_driver_name(dip), ddi_get_instance(dip));
871 
872 	return (DDI_FAILURE);
873 }
874 
875 
876 /*
877  * Called by SATA HBA from to detach an instance of the driver.
878  *
879  * For DDI_DETACH command:
880  * Free local structures allocated for SATA HBA instance during
881  * sata_hba_attach processing.
882  *
883  * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise.
884  *
885  * For DDI_SUSPEND command:
886  * Not implemented at this time (postponed until phase 2 of the development)
887  * Returnd DDI_SUCCESS.
888  *
889  * When the last HBA instance is detached, the event daemon is terminated.
890  *
891  * NOTE: cport support only, no port multiplier support.
892  */
893 int
894 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
895 {
896 	dev_info_t	*tdip;
897 	sata_hba_inst_t	*sata_hba_inst;
898 	scsi_hba_tran_t *scsi_hba_tran;
899 	sata_cport_info_t *cportinfo;
900 	sata_drive_info_t *sdinfo;
901 	int ncport;
902 
903 	SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n",
904 	    ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip));
905 
906 	switch (cmd) {
907 	case DDI_DETACH:
908 
909 		if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
910 			return (DDI_FAILURE);
911 
912 		sata_hba_inst = scsi_hba_tran->tran_hba_private;
913 		if (sata_hba_inst == NULL)
914 			return (DDI_FAILURE);
915 
916 		if (scsi_hba_detach(dip) == DDI_FAILURE) {
917 			sata_hba_inst->satahba_attached = 1;
918 			return (DDI_FAILURE);
919 		}
920 
921 		/*
922 		 * Free all target nodes - at this point
923 		 * devices should be at least offlined
924 		 * otherwise scsi_hba_detach() should not be called.
925 		 */
926 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
927 		    ncport++) {
928 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
929 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
930 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
931 				if (sdinfo != NULL) {
932 					tdip = sata_get_target_dip(dip,
933 					    ncport);
934 					if (tdip != NULL) {
935 						if (ndi_devi_offline(tdip,
936 						    NDI_DEVI_REMOVE) !=
937 						    NDI_SUCCESS) {
938 							SATA_LOG_D((
939 							    sata_hba_inst,
940 							    CE_WARN,
941 							    "sata_hba_detach: "
942 							    "Target node not "
943 							    "removed !"));
944 							return (DDI_FAILURE);
945 						}
946 					}
947 				}
948 			}
949 		}
950 		/*
951 		 * Disable sata event daemon processing for this HBA
952 		 */
953 		sata_hba_inst->satahba_attached = 0;
954 
955 		/*
956 		 * Remove event daemon thread, if it is last HBA instance.
957 		 */
958 
959 		mutex_enter(&sata_mutex);
960 		if (sata_hba_list->satahba_next == NULL) {
961 			mutex_exit(&sata_mutex);
962 			sata_event_thread_control(0);
963 			mutex_enter(&sata_mutex);
964 		}
965 		mutex_exit(&sata_mutex);
966 
967 		/* Remove this HBA instance from the HBA list */
968 		sata_remove_hba_instance(dip);
969 
970 		/*
971 		 * At this point there should be no target nodes attached.
972 		 * Detach and destroy device and port info structures.
973 		 */
974 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
975 		    ncport++) {
976 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
977 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
978 				sdinfo =
979 				    cportinfo->cport_devp.cport_sata_drive;
980 				if (sdinfo != NULL) {
981 					/* Release device structure */
982 					kmem_free(sdinfo,
983 					    sizeof (sata_drive_info_t));
984 				}
985 				/* Release cport info */
986 				mutex_destroy(&cportinfo->cport_mutex);
987 				kmem_free(cportinfo,
988 				    sizeof (sata_cport_info_t));
989 			}
990 		}
991 
992 		scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran);
993 
994 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
995 
996 		taskq_destroy(sata_hba_inst->satahba_taskq);
997 
998 		mutex_destroy(&sata_hba_inst->satahba_mutex);
999 		kmem_free((void *)sata_hba_inst,
1000 		    sizeof (struct sata_hba_inst));
1001 
1002 		return (DDI_SUCCESS);
1003 
1004 	case DDI_SUSPEND:
1005 		/*
1006 		 * Postponed until phase 2
1007 		 */
1008 		return (DDI_FAILURE);
1009 
1010 	default:
1011 		return (DDI_FAILURE);
1012 	}
1013 }
1014 
1015 
1016 /*
1017  * Called by an HBA drive from _fini() routine.
1018  * Unregisters SATA HBA instance/SATA framework pair from the scsi framework.
1019  */
1020 void
1021 sata_hba_fini(struct modlinkage *modlp)
1022 {
1023 	SATADBG1(SATA_DBG_HBA_IF, NULL,
1024 	    "sata_hba_fini: name %s\n",
1025 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
1026 
1027 	scsi_hba_fini(modlp);
1028 }
1029 
1030 
1031 /*
1032  * Default open and close routine for sata_hba framework.
1033  *
1034  */
1035 /*
1036  * Open devctl node.
1037  *
1038  * Returns:
1039  * 0 if node was open successfully, error code otherwise.
1040  *
1041  *
1042  */
1043 
1044 static int
1045 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1046 {
1047 #ifndef __lock_lint
1048 	_NOTE(ARGUNUSED(credp))
1049 #endif
1050 	int rv = 0;
1051 	dev_info_t *dip;
1052 	scsi_hba_tran_t *scsi_hba_tran;
1053 	sata_hba_inst_t	*sata_hba_inst;
1054 
1055 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL);
1056 
1057 	if (otyp != OTYP_CHR)
1058 		return (EINVAL);
1059 
1060 	dip = sata_devt_to_devinfo(*devp);
1061 	if (dip == NULL)
1062 		return (ENXIO);
1063 
1064 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1065 		return (ENXIO);
1066 
1067 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1068 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1069 		return (ENXIO);
1070 
1071 	mutex_enter(&sata_mutex);
1072 	if (flags & FEXCL) {
1073 		if (sata_hba_inst->satahba_open_flag != 0) {
1074 			rv = EBUSY;
1075 		} else {
1076 			sata_hba_inst->satahba_open_flag =
1077 			    SATA_DEVCTL_EXOPENED;
1078 		}
1079 	} else {
1080 		if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) {
1081 			rv = EBUSY;
1082 		} else {
1083 			sata_hba_inst->satahba_open_flag =
1084 			    SATA_DEVCTL_SOPENED;
1085 		}
1086 	}
1087 	mutex_exit(&sata_mutex);
1088 
1089 	return (rv);
1090 }
1091 
1092 
1093 /*
1094  * Close devctl node.
1095  * Returns:
1096  * 0 if node was closed successfully, error code otherwise.
1097  *
1098  */
1099 
1100 static int
1101 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
1102 {
1103 #ifndef __lock_lint
1104 	_NOTE(ARGUNUSED(credp))
1105 	_NOTE(ARGUNUSED(flag))
1106 #endif
1107 	dev_info_t *dip;
1108 	scsi_hba_tran_t *scsi_hba_tran;
1109 	sata_hba_inst_t	*sata_hba_inst;
1110 
1111 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL);
1112 
1113 	if (otyp != OTYP_CHR)
1114 		return (EINVAL);
1115 
1116 	dip = sata_devt_to_devinfo(dev);
1117 	if (dip == NULL)
1118 		return (ENXIO);
1119 
1120 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1121 		return (ENXIO);
1122 
1123 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1124 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1125 		return (ENXIO);
1126 
1127 	mutex_enter(&sata_mutex);
1128 	sata_hba_inst->satahba_open_flag = 0;
1129 	mutex_exit(&sata_mutex);
1130 	return (0);
1131 }
1132 
1133 
1134 
1135 /*
1136  * Standard IOCTL commands for SATA hotplugging.
1137  * Implemented DEVCTL_AP commands:
1138  * DEVCTL_AP_CONNECT
1139  * DEVCTL_AP_DISCONNECT
1140  * DEVCTL_AP_CONFIGURE
1141  * DEVCTL_UNCONFIGURE
1142  * DEVCTL_AP_CONTROL
1143  *
1144  * Commands passed to default ndi ioctl handler:
1145  * DEVCTL_DEVICE_GETSTATE
1146  * DEVCTL_DEVICE_ONLINE
1147  * DEVCTL_DEVICE_OFFLINE
1148  * DEVCTL_DEVICE_REMOVE
1149  * DEVCTL_DEVICE_INSERT
1150  * DEVCTL_BUS_GETSTATE
1151  *
1152  * All other cmds are passed to HBA if it provide ioctl handler, or failed
1153  * if not.
1154  *
1155  * Returns:
1156  * 0 if successful,
1157  * error code if operation failed.
1158  *
1159  * NOTE: Port Multiplier is not supported.
1160  *
1161  */
1162 
1163 static int
1164 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1165     int *rvalp)
1166 {
1167 #ifndef __lock_lint
1168 	_NOTE(ARGUNUSED(credp))
1169 	_NOTE(ARGUNUSED(rvalp))
1170 #endif
1171 	int rv = 0;
1172 	int32_t	comp_port = -1;
1173 	dev_info_t *dip;
1174 	devctl_ap_state_t ap_state;
1175 	struct devctl_iocdata *dcp = NULL;
1176 	scsi_hba_tran_t *scsi_hba_tran;
1177 	sata_hba_inst_t *sata_hba_inst;
1178 	sata_device_t sata_device;
1179 	sata_cport_info_t *cportinfo;
1180 	int cport, pmport, qual;
1181 	int rval = SATA_SUCCESS;
1182 
1183 	dip = sata_devt_to_devinfo(dev);
1184 	if (dip == NULL)
1185 		return (ENXIO);
1186 
1187 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1188 		return (ENXIO);
1189 
1190 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1191 	if (sata_hba_inst == NULL)
1192 		return (ENXIO);
1193 
1194 	if (sata_hba_inst->satahba_tran == NULL)
1195 		return (ENXIO);
1196 
1197 	switch (cmd) {
1198 
1199 	case DEVCTL_DEVICE_GETSTATE:
1200 	case DEVCTL_DEVICE_ONLINE:
1201 	case DEVCTL_DEVICE_OFFLINE:
1202 	case DEVCTL_DEVICE_REMOVE:
1203 	case DEVCTL_BUS_GETSTATE:
1204 		/*
1205 		 * There may be more cases that we want to pass to default
1206 		 * handler rather than fail them.
1207 		 */
1208 		return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
1209 	}
1210 
1211 	/* read devctl ioctl data */
1212 	if (cmd != DEVCTL_AP_CONTROL) {
1213 		if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1214 			return (EFAULT);
1215 
1216 		if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) ==
1217 		    -1) {
1218 			if (dcp)
1219 				ndi_dc_freehdl(dcp);
1220 			return (EINVAL);
1221 		}
1222 
1223 		cport = SCSI_TO_SATA_CPORT(comp_port);
1224 		pmport = SCSI_TO_SATA_PMPORT(comp_port);
1225 		/* Only cport is considered now, i.e. SATA_ADDR_CPORT */
1226 		qual = SATA_ADDR_CPORT;
1227 		if (sata_validate_sata_address(sata_hba_inst, cport, pmport,
1228 		    qual) != 0) {
1229 			ndi_dc_freehdl(dcp);
1230 			return (EINVAL);
1231 		}
1232 
1233 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1234 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1235 		    cport_mutex);
1236 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1237 			/*
1238 			 * Cannot process ioctl request now. Come back later.
1239 			 */
1240 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1241 			    cport_mutex);
1242 			ndi_dc_freehdl(dcp);
1243 			return (EBUSY);
1244 		}
1245 		/* Block event processing for this port */
1246 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1247 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1248 
1249 		sata_device.satadev_addr.cport = cport;
1250 		sata_device.satadev_addr.pmport = pmport;
1251 		sata_device.satadev_addr.qual = qual;
1252 		sata_device.satadev_rev = SATA_DEVICE_REV;
1253 	}
1254 
1255 	switch (cmd) {
1256 
1257 	case DEVCTL_AP_DISCONNECT:
1258 
1259 		/*
1260 		 * Normally, cfgadm sata plugin will try to offline
1261 		 * (unconfigure) device before this request. Nevertheless,
1262 		 * if a device is still configured, we need to
1263 		 * attempt to offline and unconfigure device first, and we will
1264 		 * deactivate the port regardless of the unconfigure
1265 		 * operation results.
1266 		 *
1267 		 */
1268 		rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device);
1269 
1270 		break;
1271 
1272 	case DEVCTL_AP_UNCONFIGURE:
1273 
1274 		/*
1275 		 * The unconfigure operation uses generic nexus operation to
1276 		 * offline a device. It leaves a target device node attached.
1277 		 * and obviously sata_drive_info attached as well, because
1278 		 * from the hardware point of view nothing has changed.
1279 		 */
1280 		rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device);
1281 		break;
1282 
1283 	case DEVCTL_AP_CONNECT:
1284 	{
1285 		/*
1286 		 * The sata cfgadm pluging will invoke this operation only if
1287 		 * port was found in the disconnect state (failed state
1288 		 * is also treated as the disconnected state).
1289 		 * If port activation is successful and a device is found
1290 		 * attached to the port, the initialization sequence is
1291 		 * executed to probe the port and attach
1292 		 * a device structure to a port structure. The device is not
1293 		 * set in configured state (system-wise) by this operation.
1294 		 */
1295 
1296 		rv = sata_ioctl_connect(sata_hba_inst, &sata_device);
1297 
1298 		break;
1299 	}
1300 
1301 	case DEVCTL_AP_CONFIGURE:
1302 	{
1303 		/*
1304 		 * A port may be in an active or shutdown state.
1305 		 * If port is in a failed state, operation is aborted.
1306 		 * If a port is in a shutdown state, sata_tran_port_activate()
1307 		 * is invoked prior to any other operation.
1308 		 *
1309 		 * Onlining the device involves creating a new target node.
1310 		 * If there is an old target node present (belonging to
1311 		 * previously removed device), the operation is aborted - the
1312 		 * old node has to be released and removed before configure
1313 		 * operation is attempted.
1314 		 */
1315 
1316 		rv = sata_ioctl_configure(sata_hba_inst, &sata_device);
1317 
1318 		break;
1319 	}
1320 
1321 	case DEVCTL_AP_GETSTATE:
1322 
1323 		sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state);
1324 
1325 		ap_state.ap_last_change = (time_t)-1;
1326 		ap_state.ap_error_code = 0;
1327 		ap_state.ap_in_transition = 0;
1328 
1329 		/* Copy the return AP-state information to the user space */
1330 		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
1331 			rv = EFAULT;
1332 		}
1333 		break;
1334 
1335 	case DEVCTL_AP_CONTROL:
1336 	{
1337 		/*
1338 		 * Generic devctl for hardware specific functionality
1339 		 */
1340 		sata_ioctl_data_t	ioc;
1341 
1342 		ASSERT(dcp == NULL);
1343 
1344 		/* Copy in user ioctl data first */
1345 #ifdef _MULTI_DATAMODEL
1346 		if (ddi_model_convert_from(mode & FMODELS) ==
1347 		    DDI_MODEL_ILP32) {
1348 
1349 			sata_ioctl_data_32_t	ioc32;
1350 
1351 			if (ddi_copyin((void *)arg, (void *)&ioc32,
1352 			    sizeof (ioc32), mode) != 0) {
1353 				rv = EFAULT;
1354 				break;
1355 			}
1356 			ioc.cmd 	= (uint_t)ioc32.cmd;
1357 			ioc.port	= (uint_t)ioc32.port;
1358 			ioc.get_size	= (uint_t)ioc32.get_size;
1359 			ioc.buf		= (caddr_t)(uintptr_t)ioc32.buf;
1360 			ioc.bufsiz	= (uint_t)ioc32.bufsiz;
1361 			ioc.misc_arg	= (uint_t)ioc32.misc_arg;
1362 		} else
1363 #endif /* _MULTI_DATAMODEL */
1364 		if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
1365 		    mode) != 0) {
1366 			return (EFAULT);
1367 		}
1368 
1369 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
1370 		    "sata_hba_ioctl: DEVCTL_AP_CONTROL "
1371 		    "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port);
1372 
1373 		/*
1374 		 * To avoid BE/LE and 32/64 issues, a get_size always returns
1375 		 * a 32-bit number.
1376 		 */
1377 		if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
1378 			return (EINVAL);
1379 		}
1380 		/* validate address */
1381 		cport = SCSI_TO_SATA_CPORT(ioc.port);
1382 		pmport = SCSI_TO_SATA_PMPORT(ioc.port);
1383 		qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port);
1384 
1385 		/* Override address qualifier - handle cport only for now */
1386 		qual = SATA_ADDR_CPORT;
1387 
1388 		if (sata_validate_sata_address(sata_hba_inst, cport,
1389 		    pmport, qual) != 0)
1390 			return (EINVAL);
1391 
1392 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1393 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1394 		    cport_mutex);
1395 		/* Is the port locked by event processing daemon ? */
1396 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1397 			/*
1398 			 * Cannot process ioctl request now. Come back later
1399 			 */
1400 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1401 			    cport_mutex);
1402 			return (EBUSY);
1403 		}
1404 		/* Block event processing for this port */
1405 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1406 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1407 
1408 
1409 		sata_device.satadev_addr.cport = cport;
1410 		sata_device.satadev_addr.pmport = pmport;
1411 		sata_device.satadev_addr.qual = qual;
1412 		sata_device.satadev_rev = SATA_DEVICE_REV;
1413 
1414 		switch (ioc.cmd) {
1415 
1416 		case SATA_CFGA_RESET_PORT:
1417 			/*
1418 			 * There is no protection for configured device.
1419 			 */
1420 			rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device);
1421 			break;
1422 
1423 		case SATA_CFGA_RESET_DEVICE:
1424 			/*
1425 			 * There is no protection for configured device.
1426 			 */
1427 			rv = sata_ioctl_reset_device(sata_hba_inst,
1428 			    &sata_device);
1429 			break;
1430 
1431 		case SATA_CFGA_RESET_ALL:
1432 			/*
1433 			 * There is no protection for configured devices.
1434 			 */
1435 			rv = sata_ioctl_reset_all(sata_hba_inst);
1436 			/*
1437 			 * We return here, because common return is for
1438 			 * a single port operation - we have already unlocked
1439 			 * all ports and no dc handle was allocated.
1440 			 */
1441 			return (rv);
1442 
1443 		case SATA_CFGA_PORT_DEACTIVATE:
1444 			/*
1445 			 * Arbitrarily unconfigure attached device, if any.
1446 			 * Even if the unconfigure fails, proceed with the
1447 			 * port deactivation.
1448 			 */
1449 			rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device);
1450 
1451 			break;
1452 
1453 		case SATA_CFGA_PORT_ACTIVATE:
1454 
1455 			rv = sata_ioctl_activate(sata_hba_inst, &sata_device);
1456 			break;
1457 
1458 		case SATA_CFGA_PORT_SELF_TEST:
1459 
1460 			rv = sata_ioctl_port_self_test(sata_hba_inst,
1461 			    &sata_device);
1462 			break;
1463 
1464 		case SATA_CFGA_GET_DEVICE_PATH:
1465 			if (qual == SATA_ADDR_CPORT)
1466 				sata_device.satadev_addr.qual =
1467 				    SATA_ADDR_DCPORT;
1468 			else
1469 				sata_device.satadev_addr.qual =
1470 				    SATA_ADDR_DPMPORT;
1471 			rv = sata_ioctl_get_device_path(sata_hba_inst,
1472 			    &sata_device, &ioc, mode);
1473 			break;
1474 
1475 		case SATA_CFGA_GET_AP_TYPE:
1476 
1477 			rv = sata_ioctl_get_ap_type(sata_hba_inst,
1478 			    &sata_device, &ioc, mode);
1479 			break;
1480 
1481 		case SATA_CFGA_GET_MODEL_INFO:
1482 
1483 			rv = sata_ioctl_get_model_info(sata_hba_inst,
1484 			    &sata_device, &ioc, mode);
1485 			break;
1486 
1487 		case SATA_CFGA_GET_REVFIRMWARE_INFO:
1488 
1489 			rv = sata_ioctl_get_revfirmware_info(sata_hba_inst,
1490 			    &sata_device, &ioc, mode);
1491 			break;
1492 
1493 		case SATA_CFGA_GET_SERIALNUMBER_INFO:
1494 
1495 			rv = sata_ioctl_get_serialnumber_info(sata_hba_inst,
1496 			    &sata_device, &ioc, mode);
1497 			break;
1498 
1499 		default:
1500 			rv = EINVAL;
1501 			break;
1502 
1503 		} /* End of DEVCTL_AP_CONTROL cmd switch */
1504 
1505 		break;
1506 	}
1507 
1508 	default:
1509 	{
1510 		/*
1511 		 * If we got here, we got an IOCTL that SATA HBA Framework
1512 		 * does not recognize. Pass ioctl to HBA driver, in case
1513 		 * it could process it.
1514 		 */
1515 		sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran;
1516 		dev_info_t	*mydip = SATA_DIP(sata_hba_inst);
1517 
1518 		SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1519 		    "IOCTL 0x%2x not supported in SATA framework, "
1520 		    "passthrough to HBA", cmd);
1521 
1522 		if (sata_tran->sata_tran_ioctl == NULL) {
1523 			rv = EINVAL;
1524 			break;
1525 		}
1526 		rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg);
1527 		if (rval != 0) {
1528 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1529 			    "IOCTL 0x%2x failed in HBA", cmd);
1530 			rv = rval;
1531 		}
1532 		break;
1533 	}
1534 
1535 	} /* End of main IOCTL switch */
1536 
1537 	if (dcp) {
1538 		ndi_dc_freehdl(dcp);
1539 	}
1540 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1541 	cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
1542 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1543 
1544 	return (rv);
1545 }
1546 
1547 
1548 /*
1549  * Create error retrieval sata packet
1550  *
1551  * A sata packet is allocated and set-up to contain specified error retrieval
1552  * command and appropriate dma-able data buffer.
1553  * No association with any scsi packet is made and no callback routine is
1554  * specified.
1555  *
1556  * Returns a pointer to sata packet upon successfull packet creation.
1557  * Returns NULL, if packet cannot be created.
1558  */
1559 sata_pkt_t *
1560 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device,
1561     int pkt_type)
1562 {
1563 	sata_hba_inst_t	*sata_hba_inst;
1564 	sata_pkt_txlate_t *spx;
1565 	sata_pkt_t *spkt;
1566 	sata_drive_info_t *sdinfo;
1567 
1568 	mutex_enter(&sata_mutex);
1569 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1570 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1571 		if (SATA_DIP(sata_hba_inst) == dip)
1572 			break;
1573 	}
1574 	mutex_exit(&sata_mutex);
1575 	ASSERT(sata_hba_inst != NULL);
1576 
1577 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
1578 	if (sdinfo == NULL) {
1579 		sata_log(sata_hba_inst, CE_WARN,
1580 		    "sata: error recovery request for non-attached device at "
1581 		    "cport %d", sata_device->satadev_addr.cport);
1582 		return (NULL);
1583 	}
1584 
1585 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1586 	spx->txlt_sata_hba_inst = sata_hba_inst;
1587 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
1588 	spkt = sata_pkt_alloc(spx, NULL);
1589 	if (spkt == NULL) {
1590 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1591 		return (NULL);
1592 	}
1593 	/* address is needed now */
1594 	spkt->satapkt_device.satadev_addr = sata_device->satadev_addr;
1595 
1596 	switch (pkt_type) {
1597 	case SATA_ERR_RETR_PKT_TYPE_NCQ:
1598 		if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS)
1599 			return (spkt);
1600 		break;
1601 
1602 	case SATA_ERR_RETR_PKT_TYPE_ATAPI:
1603 		if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS)
1604 			return (spkt);
1605 		break;
1606 
1607 	default:
1608 		break;
1609 	}
1610 
1611 	sata_pkt_free(spx);
1612 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1613 	return (NULL);
1614 
1615 }
1616 
1617 
1618 /*
1619  * Free error retrieval sata packet
1620  *
1621  * Free sata packet and any associated resources allocated previously by
1622  * sata_get_error_retrieval_pkt().
1623  *
1624  * Void return.
1625  */
1626 void
1627 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt)
1628 {
1629 	sata_pkt_txlate_t *spx =
1630 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1631 
1632 	ASSERT(sata_pkt != NULL);
1633 
1634 	sata_free_local_buffer(spx);
1635 	sata_pkt_free(spx);
1636 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1637 
1638 }
1639 
1640 /*
1641  * sata_name_child is for composing the name of the node
1642  * the format of the name is "target,0".
1643  */
1644 static int
1645 sata_name_child(dev_info_t *dip, char *name, int namelen)
1646 {
1647 	int target;
1648 
1649 	target = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1650 	    DDI_PROP_DONTPASS, "target", -1);
1651 	if (target == -1)
1652 		return (DDI_FAILURE);
1653 	(void) snprintf(name, namelen, "%x,0", target);
1654 	return (DDI_SUCCESS);
1655 }
1656 
1657 
1658 
1659 /* ****************** SCSA required entry points *********************** */
1660 
1661 /*
1662  * Implementation of scsi tran_tgt_init.
1663  * sata_scsi_tgt_init() initializes scsi_device structure
1664  *
1665  * If successful, DDI_SUCCESS is returned.
1666  * DDI_FAILURE is returned if addressed device does not exist
1667  */
1668 
1669 static int
1670 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1671     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1672 {
1673 #ifndef __lock_lint
1674 	_NOTE(ARGUNUSED(hba_dip))
1675 	_NOTE(ARGUNUSED(tgt_dip))
1676 #endif
1677 	sata_device_t		sata_device;
1678 	sata_drive_info_t	*sdinfo;
1679 	struct sata_id		*sid;
1680 	sata_hba_inst_t		*sata_hba_inst;
1681 	char			model[SATA_ID_MODEL_LEN + 1];
1682 	char			fw[SATA_ID_FW_LEN + 1];
1683 	char			*vid, *pid;
1684 	int			i;
1685 
1686 	/*
1687 	 * Fail tran_tgt_init for .conf stub node
1688 	 */
1689 	if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
1690 		(void) ndi_merge_node(tgt_dip, sata_name_child);
1691 		ddi_set_name_addr(tgt_dip, NULL);
1692 		return (DDI_FAILURE);
1693 	}
1694 
1695 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
1696 
1697 	/* Validate scsi device address */
1698 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
1699 	    &sata_device) != 0)
1700 		return (DDI_FAILURE);
1701 
1702 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1703 	    sata_device.satadev_addr.cport)));
1704 
1705 	/* sata_device now contains a valid sata address */
1706 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
1707 	if (sdinfo == NULL) {
1708 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1709 		    sata_device.satadev_addr.cport)));
1710 		return (DDI_FAILURE);
1711 	}
1712 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1713 	    sata_device.satadev_addr.cport)));
1714 
1715 	/*
1716 	 * Check if we need to create a legacy devid (i.e cmdk style) for
1717 	 * the target disks.
1718 	 *
1719 	 * HBA devinfo node will have the property "use-cmdk-devid-format"
1720 	 * if we need to create cmdk-style devid for all the disk devices
1721 	 * attached to this controller. This property may have been set
1722 	 * from HBA driver's .conf file or by the HBA driver in its
1723 	 * attach(9F) function.
1724 	 */
1725 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
1726 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
1727 	    "use-cmdk-devid-format", 0) == 1)) {
1728 		/* register a legacy devid for this target node */
1729 		sata_target_devid_register(tgt_dip, sdinfo);
1730 	}
1731 
1732 
1733 	/*
1734 	 * 'Identify Device Data' does not always fit in standard SCSI
1735 	 * INQUIRY data, so establish INQUIRY_* properties with full-form
1736 	 * of information.
1737 	 */
1738 	sid = &sdinfo->satadrv_id;
1739 #ifdef	_LITTLE_ENDIAN
1740 	swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
1741 	swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
1742 #else	/* _LITTLE_ENDIAN */
1743 	bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN);
1744 	bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN);
1745 #endif	/* _LITTLE_ENDIAN */
1746 	model[SATA_ID_MODEL_LEN] = 0;
1747 	fw[SATA_ID_FW_LEN] = 0;
1748 
1749 	/* split model into into vid/pid */
1750 	for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++)
1751 		if ((*pid == ' ') || (*pid == '\t'))
1752 			break;
1753 	if (i < SATA_ID_MODEL_LEN) {
1754 		vid = model;
1755 		*pid++ = 0;		/* terminate vid, establish pid */
1756 	} else {
1757 		vid = NULL;		/* vid will stay "ATA     " */
1758 		pid = model;		/* model is all pid */
1759 	}
1760 
1761 	if (vid)
1762 		(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_VENDOR_ID,
1763 		    vid, strlen(vid));
1764 	if (pid)
1765 		(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID,
1766 		    pid, strlen(pid));
1767 	(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_REVISION_ID,
1768 	    fw, strlen(fw));
1769 
1770 	return (DDI_SUCCESS);
1771 }
1772 
1773 /*
1774  * Implementation of scsi tran_tgt_probe.
1775  * Probe target, by calling default scsi routine scsi_hba_probe()
1776  */
1777 static int
1778 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void))
1779 {
1780 	sata_hba_inst_t *sata_hba_inst =
1781 	    (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private);
1782 	int rval;
1783 
1784 	rval = scsi_hba_probe(sd, callback);
1785 
1786 	if (rval == SCSIPROBE_EXISTS) {
1787 		/*
1788 		 * Set property "pm-capable" on the target device node, so that
1789 		 * the target driver will not try to fetch scsi cycle counters
1790 		 * before enabling device power-management.
1791 		 */
1792 		if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
1793 		    "pm-capable", 1)) != DDI_PROP_SUCCESS) {
1794 			sata_log(sata_hba_inst, CE_WARN,
1795 			    "SATA device at port %d: "
1796 			    "will not be power-managed ",
1797 			    SCSI_TO_SATA_CPORT(sd->sd_address.a_target));
1798 			SATA_LOG_D((sata_hba_inst, CE_WARN,
1799 			    "failure updating pm-capable property"));
1800 		}
1801 	}
1802 	return (rval);
1803 }
1804 
1805 /*
1806  * Implementation of scsi tran_tgt_free.
1807  * Release all resources allocated for scsi_device
1808  */
1809 static void
1810 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1811     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1812 {
1813 #ifndef __lock_lint
1814 	_NOTE(ARGUNUSED(hba_dip))
1815 #endif
1816 	sata_device_t		sata_device;
1817 	sata_drive_info_t	*sdinfo;
1818 	sata_hba_inst_t		*sata_hba_inst;
1819 	ddi_devid_t		devid;
1820 
1821 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
1822 
1823 	/* Validate scsi device address */
1824 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
1825 	    &sata_device) != 0)
1826 		return;
1827 
1828 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1829 	    sata_device.satadev_addr.cport)));
1830 
1831 	/* sata_device now should contain a valid sata address */
1832 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
1833 	if (sdinfo == NULL) {
1834 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1835 		    sata_device.satadev_addr.cport)));
1836 		return;
1837 	}
1838 	/*
1839 	 * We did not allocate any resources in sata_scsi_tgt_init()
1840 	 * other than few properties.
1841 	 * Free them.
1842 	 */
1843 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1844 	    sata_device.satadev_addr.cport)));
1845 	(void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable");
1846 
1847 	/*
1848 	 * If devid was previously created but not freed up from
1849 	 * sd(7D) driver (i.e during detach(9F)) then do it here.
1850 	 */
1851 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
1852 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
1853 	    "use-cmdk-devid-format", 0) == 1) &&
1854 	    (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) {
1855 		ddi_devid_unregister(tgt_dip);
1856 		ddi_devid_free(devid);
1857 	}
1858 }
1859 
1860 /*
1861  * Implementation of scsi tran_init_pkt
1862  * Upon successful return, scsi pkt buffer has DMA resources allocated.
1863  *
1864  * It seems that we should always allocate pkt, even if the address is
1865  * for non-existing device - just use some default for dma_attr.
1866  * The reason is that there is no way to communicate this to a caller here.
1867  * Subsequent call to sata_scsi_start may fail appropriately.
1868  * Simply returning NULL does not seem to discourage a target driver...
1869  *
1870  * Returns a pointer to initialized scsi_pkt, or NULL otherwise.
1871  */
1872 static struct scsi_pkt *
1873 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
1874     struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
1875     int (*callback)(caddr_t), caddr_t arg)
1876 {
1877 	sata_hba_inst_t *sata_hba_inst =
1878 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
1879 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
1880 	sata_device_t sata_device;
1881 	sata_drive_info_t *sdinfo;
1882 	sata_pkt_txlate_t *spx;
1883 	ddi_dma_attr_t cur_dma_attr;
1884 	int rval;
1885 	boolean_t new_pkt = TRUE;
1886 
1887 	ASSERT(ap->a_hba_tran->tran_hba_dip == dip);
1888 
1889 	/*
1890 	 * We need to translate the address, even if it could be
1891 	 * a bogus one, for a non-existing device
1892 	 */
1893 	sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
1894 	sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target);
1895 	sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
1896 	sata_device.satadev_rev = SATA_DEVICE_REV;
1897 
1898 	if (pkt == NULL) {
1899 		/*
1900 		 * Have to allocate a brand new scsi packet.
1901 		 * We need to operate with auto request sense enabled.
1902 		 */
1903 		pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen,
1904 		    MAX(statuslen, sizeof (struct scsi_arq_status)),
1905 		    tgtlen, sizeof (sata_pkt_txlate_t), callback, arg);
1906 
1907 		if (pkt == NULL)
1908 			return (NULL);
1909 
1910 		/* Fill scsi packet structure */
1911 		pkt->pkt_comp		= (void (*)())NULL;
1912 		pkt->pkt_time		= 0;
1913 		pkt->pkt_resid		= 0;
1914 		pkt->pkt_statistics	= 0;
1915 		pkt->pkt_reason		= 0;
1916 
1917 		/*
1918 		 * pkt_hba_private will point to sata pkt txlate structure
1919 		 */
1920 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
1921 		bzero(spx, sizeof (sata_pkt_txlate_t));
1922 
1923 		spx->txlt_scsi_pkt = pkt;
1924 		spx->txlt_sata_hba_inst = sata_hba_inst;
1925 
1926 		/* Allocate sata_pkt */
1927 		spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback);
1928 		if (spx->txlt_sata_pkt == NULL) {
1929 			/* Could not allocate sata pkt */
1930 			scsi_hba_pkt_free(ap, pkt);
1931 			return (NULL);
1932 		}
1933 		/* Set sata address */
1934 		spx->txlt_sata_pkt->satapkt_device.satadev_addr =
1935 		    sata_device.satadev_addr;
1936 		spx->txlt_sata_pkt->satapkt_device.satadev_rev =
1937 		    sata_device.satadev_rev;
1938 
1939 		if ((bp == NULL) || (bp->b_bcount == 0))
1940 			return (pkt);
1941 
1942 		spx->txlt_total_residue = bp->b_bcount;
1943 	} else {
1944 		new_pkt = FALSE;
1945 		/*
1946 		 * Packet was preallocated/initialized by previous call
1947 		 */
1948 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
1949 
1950 		if ((bp == NULL) || (bp->b_bcount == 0)) {
1951 			return (pkt);
1952 		}
1953 		ASSERT(spx->txlt_buf_dma_handle != NULL);
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 	if (spx->txlt_buf_dma_handle != NULL) {
2604 		if (spx->txlt_tmp_buf != NULL)  {
2605 			ASSERT(spx->txlt_tmp_buf_handle != 0);
2606 			/*
2607 			 * Intermediate DMA buffer was allocated.
2608 			 * Free allocated buffer and associated access handle.
2609 			 */
2610 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
2611 			spx->txlt_tmp_buf = NULL;
2612 		}
2613 		/*
2614 		 * Free DMA resources - cookies and handles
2615 		 */
2616 		if (spx->txlt_dma_cookie_list != NULL) {
2617 			if (spx->txlt_dma_cookie_list !=
2618 			    &spx->txlt_dma_cookie) {
2619 				(void) kmem_free(spx->txlt_dma_cookie_list,
2620 				    spx->txlt_dma_cookie_list_len *
2621 				    sizeof (ddi_dma_cookie_t));
2622 				spx->txlt_dma_cookie_list = NULL;
2623 			}
2624 		}
2625 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
2626 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
2627 	}
2628 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2629 	sata_pkt_free(spx);
2630 
2631 	scsi_hba_pkt_free(ap, pkt);
2632 }
2633 
2634 /*
2635  * Implementation of scsi tran_dmafree.
2636  * Free DMA resources allocated by sata_scsi_init_pkt()
2637  */
2638 
2639 static void
2640 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
2641 {
2642 #ifndef __lock_lint
2643 	_NOTE(ARGUNUSED(ap))
2644 #endif
2645 	sata_pkt_txlate_t *spx;
2646 
2647 	ASSERT(pkt != NULL);
2648 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2649 
2650 	if (spx->txlt_buf_dma_handle != NULL) {
2651 		if (spx->txlt_tmp_buf != NULL)  {
2652 			/*
2653 			 * Intermediate DMA buffer was allocated.
2654 			 * Free allocated buffer and associated access handle.
2655 			 */
2656 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
2657 			spx->txlt_tmp_buf = NULL;
2658 		}
2659 		/*
2660 		 * Free DMA resources - cookies and handles
2661 		 */
2662 		/* ASSERT(spx->txlt_dma_cookie_list != NULL); */
2663 		if (spx->txlt_dma_cookie_list != NULL) {
2664 			if (spx->txlt_dma_cookie_list !=
2665 			    &spx->txlt_dma_cookie) {
2666 				(void) kmem_free(spx->txlt_dma_cookie_list,
2667 				    spx->txlt_dma_cookie_list_len *
2668 				    sizeof (ddi_dma_cookie_t));
2669 				spx->txlt_dma_cookie_list = NULL;
2670 			}
2671 		}
2672 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
2673 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
2674 		spx->txlt_buf_dma_handle = NULL;
2675 	}
2676 }
2677 
2678 /*
2679  * Implementation of scsi tran_sync_pkt.
2680  *
2681  * The assumption below is that pkt is unique - there is no need to check ap
2682  *
2683  * Synchronize DMA buffer and, if the intermediate buffer is used, copy data
2684  * into/from the real buffer.
2685  */
2686 static void
2687 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2688 {
2689 #ifndef __lock_lint
2690 	_NOTE(ARGUNUSED(ap))
2691 #endif
2692 	int rval;
2693 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2694 	struct buf *bp;
2695 	int direction;
2696 
2697 	ASSERT(spx != NULL);
2698 	if (spx->txlt_buf_dma_handle != NULL) {
2699 		direction = spx->txlt_sata_pkt->
2700 		    satapkt_cmd.satacmd_flags.sata_data_direction;
2701 		if (spx->txlt_sata_pkt != NULL &&
2702 		    direction != SATA_DIR_NODATA_XFER) {
2703 			if (spx->txlt_tmp_buf != NULL) {
2704 				/* Intermediate DMA buffer used */
2705 				bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2706 
2707 				if (direction & SATA_DIR_WRITE) {
2708 					bcopy(bp->b_un.b_addr,
2709 					    spx->txlt_tmp_buf, bp->b_bcount);
2710 				}
2711 			}
2712 			/* Sync the buffer for device or for CPU */
2713 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle,   0, 0,
2714 			    (direction & SATA_DIR_WRITE) ?
2715 			    DDI_DMA_SYNC_FORDEV :  DDI_DMA_SYNC_FORCPU);
2716 			ASSERT(rval == DDI_SUCCESS);
2717 			if (spx->txlt_tmp_buf != NULL &&
2718 			    !(direction & SATA_DIR_WRITE)) {
2719 				/* Intermediate DMA buffer used for read */
2720 				bcopy(spx->txlt_tmp_buf,
2721 				    bp->b_un.b_addr, bp->b_bcount);
2722 			}
2723 
2724 		}
2725 	}
2726 }
2727 
2728 
2729 
2730 /* *******************  SATA - SCSI Translation functions **************** */
2731 /*
2732  * SCSI to SATA pkt and command translation and SATA to SCSI status/error
2733  * translation.
2734  */
2735 
2736 /*
2737  * Checks if a device exists and can be access and translates common
2738  * scsi_pkt data to sata_pkt data.
2739  *
2740  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and
2741  * sata_pkt was set-up.
2742  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not
2743  * exist and pkt_comp callback was scheduled.
2744  * Returns other TRAN_XXXXX values when error occured and command should be
2745  * rejected with the returned TRAN_XXXXX value.
2746  *
2747  * This function should be called with port mutex held.
2748  */
2749 static int
2750 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason)
2751 {
2752 	sata_drive_info_t *sdinfo;
2753 	sata_device_t sata_device;
2754 	const struct sata_cmd_flags sata_initial_cmd_flags = {
2755 		SATA_DIR_NODATA_XFER,
2756 		/* all other values to 0/FALSE */
2757 	};
2758 	/*
2759 	 * Pkt_reason has to be set if the pkt_comp callback is invoked,
2760 	 * and that implies TRAN_ACCEPT return value. Any other returned value
2761 	 * indicates that the scsi packet was not accepted (the reason will not
2762 	 * be checked by the scsi target driver).
2763 	 * To make debugging easier, we set pkt_reason to know value here.
2764 	 * It may be changed later when different completion reason is
2765 	 * determined.
2766 	 */
2767 	spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
2768 	*reason = CMD_TRAN_ERR;
2769 
2770 	/* Validate address */
2771 	switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst,
2772 	    &spx->txlt_scsi_pkt->pkt_address, &sata_device)) {
2773 
2774 	case -1:
2775 		/* Invalid address or invalid device type */
2776 		return (TRAN_BADPKT);
2777 	case 1:
2778 		/* valid address but no device - it has disappeared ? */
2779 		spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE;
2780 		*reason = CMD_DEV_GONE;
2781 		/*
2782 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2783 		 * only in callback function (for normal requests) and
2784 		 * in the dump code path.
2785 		 * So, if the callback is available, we need to do
2786 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2787 		 */
2788 		if (spx->txlt_scsi_pkt->pkt_comp != NULL) {
2789 			/* scsi callback required */
2790 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2791 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2792 			    (void *)spx->txlt_scsi_pkt,
2793 			    TQ_SLEEP) == NULL)
2794 				/* Scheduling the callback failed */
2795 				return (TRAN_BUSY);
2796 
2797 			return (TRAN_ACCEPT);
2798 		}
2799 		return (TRAN_FATAL_ERROR);
2800 	default:
2801 		/* all OK; pkt reason will be overwritten later */
2802 		break;
2803 	}
2804 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2805 	    &spx->txlt_sata_pkt->satapkt_device);
2806 
2807 	/*
2808 	 * If device is in reset condition, reject the packet with
2809 	 * TRAN_BUSY, unless:
2810 	 * 1. system is panicking (dumping)
2811 	 * In such case only one thread is running and there is no way to
2812 	 * process reset.
2813 	 * 2. cfgadm operation is is progress (internal APCTL lock is set)
2814 	 * Some cfgadm operations involve drive commands, so reset condition
2815 	 * needs to be ignored for IOCTL operations.
2816 	 */
2817 	if ((sdinfo->satadrv_event_flags &
2818 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
2819 
2820 		if (!ddi_in_panic() &&
2821 		    ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst,
2822 		    sata_device.satadev_addr.cport) &
2823 		    SATA_APCTL_LOCK_PORT_BUSY) == 0)) {
2824 			spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
2825 			*reason = CMD_INCOMPLETE;
2826 			SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
2827 			    "sata_scsi_start: rejecting command because "
2828 			    "of device reset state\n", NULL);
2829 			return (TRAN_BUSY);
2830 		}
2831 	}
2832 
2833 	/*
2834 	 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by
2835 	 * sata_scsi_pkt_init() because pkt init had to work also with
2836 	 * non-existing devices.
2837 	 * Now we know that the packet was set-up for a real device, so its
2838 	 * type is known.
2839 	 */
2840 	spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type;
2841 
2842 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags;
2843 	if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst,
2844 	    sata_device.satadev_addr.cport)->cport_event_flags &
2845 	    SATA_APCTL_LOCK_PORT_BUSY) != 0) {
2846 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
2847 		    sata_ignore_dev_reset = B_TRUE;
2848 	}
2849 	/*
2850 	 * At this point the generic translation routine determined that the
2851 	 * scsi packet should be accepted. Packet completion reason may be
2852 	 * changed later when a different completion reason is determined.
2853 	 */
2854 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
2855 	*reason = CMD_CMPLT;
2856 
2857 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
2858 		/* Synchronous execution */
2859 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH |
2860 		    SATA_OPMODE_POLLING;
2861 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
2862 		    sata_ignore_dev_reset = ddi_in_panic();
2863 	} else {
2864 		/* Asynchronous execution */
2865 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH |
2866 		    SATA_OPMODE_INTERRUPTS;
2867 	}
2868 	/* Convert queuing information */
2869 	if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG)
2870 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag =
2871 		    B_TRUE;
2872 	else if (spx->txlt_scsi_pkt->pkt_flags &
2873 	    (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD))
2874 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag =
2875 		    B_TRUE;
2876 
2877 	/* Always limit pkt time */
2878 	if (spx->txlt_scsi_pkt->pkt_time == 0)
2879 		spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time;
2880 	else
2881 		/* Pass on scsi_pkt time */
2882 		spx->txlt_sata_pkt->satapkt_time =
2883 		    spx->txlt_scsi_pkt->pkt_time;
2884 
2885 	return (TRAN_ACCEPT);
2886 }
2887 
2888 
2889 /*
2890  * Translate ATA Identify Device data to SCSI Inquiry data.
2891  * This function may be called only for ATA devices.
2892  * This function should not be called for ATAPI devices - they
2893  * respond directly to SCSI Inquiry command.
2894  *
2895  * SATA Identify Device data has to be valid in sata_rive_info.
2896  * Buffer has to accomodate the inquiry length (36 bytes).
2897  *
2898  * This function should be called with a port mutex held.
2899  */
2900 static	void
2901 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst,
2902     sata_drive_info_t *sdinfo, uint8_t *buf)
2903 {
2904 
2905 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
2906 	struct sata_id *sid = &sdinfo->satadrv_id;
2907 
2908 	/* Start with a nice clean slate */
2909 	bzero((void *)inq, sizeof (struct scsi_inquiry));
2910 
2911 	/*
2912 	 * Rely on the dev_type for setting paripheral qualifier.
2913 	 * Assume that  DTYPE_RODIRECT applies to CD/DVD R/W devices.
2914 	 * It could be that DTYPE_OPTICAL could also qualify in the future.
2915 	 * ATAPI Inquiry may provide more data to the target driver.
2916 	 */
2917 	inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
2918 	    DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */
2919 
2920 	inq->inq_rmb = sid->ai_config & SATA_REM_MEDIA ? 1 : 0;
2921 	inq->inq_qual = 0;	/* Device type qualifier (obsolete in SCSI3? */
2922 	inq->inq_iso = 0;	/* ISO version */
2923 	inq->inq_ecma = 0;	/* ECMA version */
2924 	inq->inq_ansi = 3;	/* ANSI version - SCSI 3 */
2925 	inq->inq_aenc = 0;	/* Async event notification cap. */
2926 	inq->inq_trmiop = 0;	/* Supports TERMINATE I/O PROC msg - NO */
2927 	inq->inq_normaca = 0;	/* setting NACA bit supported - NO */
2928 	inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */
2929 	inq->inq_len = 31;	/* Additional length */
2930 	inq->inq_dualp = 0;	/* dual port device - NO */
2931 	inq->inq_reladdr = 0;	/* Supports relative addressing - NO */
2932 	inq->inq_sync = 0;	/* Supports synchronous data xfers - NO */
2933 	inq->inq_linked = 0;	/* Supports linked commands - NO */
2934 				/*
2935 				 * Queuing support - controller has to
2936 				 * support some sort of command queuing.
2937 				 */
2938 	if (SATA_QDEPTH(sata_hba_inst) > 1)
2939 		inq->inq_cmdque = 1; /* Supports command queueing - YES */
2940 	else
2941 		inq->inq_cmdque = 0; /* Supports command queueing - NO */
2942 	inq->inq_sftre = 0;	/* Supports Soft Reset option - NO ??? */
2943 	inq->inq_wbus32 = 0;	/* Supports 32 bit wide data xfers - NO */
2944 	inq->inq_wbus16 = 0;	/* Supports 16 bit wide data xfers - NO */
2945 
2946 #ifdef	_LITTLE_ENDIAN
2947 	/* Swap text fields to match SCSI format */
2948 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
2949 	swab(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
2950 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
2951 		swab(sid->ai_fw, inq->inq_revision, 4);	/* Revision level */
2952 	else
2953 		swab(&sid->ai_fw[4], inq->inq_revision, 4);	/* Rev. level */
2954 #else	/* _LITTLE_ENDIAN */
2955 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
2956 	bcopy(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
2957 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
2958 		bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
2959 	else
2960 		bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
2961 #endif	/* _LITTLE_ENDIAN */
2962 }
2963 
2964 
2965 /*
2966  * Scsi response set up for invalid command (command not supported)
2967  *
2968  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
2969  */
2970 static int
2971 sata_txlt_invalid_command(sata_pkt_txlate_t *spx)
2972 {
2973 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
2974 	struct scsi_extended_sense *sense;
2975 
2976 	scsipkt->pkt_reason = CMD_CMPLT;
2977 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
2978 	    STATE_SENT_CMD | STATE_GOT_STATUS;
2979 
2980 	*scsipkt->pkt_scbp = STATUS_CHECK;
2981 
2982 	sense = sata_arq_sense(spx);
2983 	sense->es_key = KEY_ILLEGAL_REQUEST;
2984 	sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE;
2985 
2986 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
2987 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
2988 
2989 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
2990 	    scsipkt->pkt_comp != NULL)
2991 		/* scsi callback required */
2992 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2993 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2994 		    (void *)spx->txlt_scsi_pkt,
2995 		    TQ_SLEEP) == NULL)
2996 			/* Scheduling the callback failed */
2997 			return (TRAN_BUSY);
2998 	return (TRAN_ACCEPT);
2999 }
3000 
3001 /*
3002  * Scsi response setup for
3003  * emulated non-data command that requires no action/return data
3004  *
3005  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3006  */
3007 static 	int
3008 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx)
3009 {
3010 	int rval;
3011 	int reason;
3012 
3013 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3014 
3015 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3016 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3017 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3018 		return (rval);
3019 	}
3020 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3021 
3022 	spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3023 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3024 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3025 	*(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD;
3026 
3027 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3028 	    "Scsi_pkt completion reason %x\n",
3029 	    spx->txlt_scsi_pkt->pkt_reason);
3030 
3031 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 &&
3032 	    spx->txlt_scsi_pkt->pkt_comp != NULL)
3033 		/* scsi callback required */
3034 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3035 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3036 		    (void *)spx->txlt_scsi_pkt,
3037 		    TQ_SLEEP) == NULL)
3038 			/* Scheduling the callback failed */
3039 			return (TRAN_BUSY);
3040 	return (TRAN_ACCEPT);
3041 }
3042 
3043 
3044 /*
3045  * SATA translate command: Inquiry / Identify Device
3046  * Use cached Identify Device data for now, rather than issuing actual
3047  * Device Identify cmd request. If device is detached and re-attached,
3048  * asynchromous event processing should fetch and refresh Identify Device
3049  * data.
3050  * Two VPD pages are supported now:
3051  * Vital Product Data page
3052  * Unit Serial Number page
3053  *
3054  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3055  */
3056 
3057 #define	EVPD			1	/* Extended Vital Product Data flag */
3058 #define	CMDDT			2	/* Command Support Data - Obsolete */
3059 #define	INQUIRY_SUP_VPD_PAGE	0	/* Supported VDP Pages Page COde */
3060 #define	INQUIRY_USN_PAGE	0x80	/* Unit Serial Number Page Code */
3061 #define	INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */
3062 
3063 static int
3064 sata_txlt_inquiry(sata_pkt_txlate_t *spx)
3065 {
3066 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3067 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3068 	sata_drive_info_t *sdinfo;
3069 	struct scsi_extended_sense *sense;
3070 	int count;
3071 	uint8_t *p;
3072 	int i, j;
3073 	uint8_t page_buf[0xff]; /* Max length */
3074 	int rval, reason;
3075 
3076 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3077 
3078 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3079 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3080 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3081 		return (rval);
3082 	}
3083 
3084 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3085 	    &spx->txlt_sata_pkt->satapkt_device);
3086 
3087 	ASSERT(sdinfo != NULL);
3088 
3089 	scsipkt->pkt_reason = CMD_CMPLT;
3090 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3091 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3092 
3093 	/* Reject not supported request */
3094 	if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */
3095 		*scsipkt->pkt_scbp = STATUS_CHECK;
3096 		sense = sata_arq_sense(spx);
3097 		sense->es_key = KEY_ILLEGAL_REQUEST;
3098 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3099 		goto done;
3100 	}
3101 
3102 	/* Valid Inquiry request */
3103 	*scsipkt->pkt_scbp = STATUS_GOOD;
3104 
3105 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3106 
3107 		/*
3108 		 * Because it is fully emulated command storing data
3109 		 * programatically in the specified buffer, release
3110 		 * preallocated DMA resources before storing data in the buffer,
3111 		 * so no unwanted DMA sync would take place.
3112 		 */
3113 		sata_scsi_dmafree(NULL, scsipkt);
3114 
3115 		if (!(scsipkt->pkt_cdbp[1] & EVPD)) {
3116 			/* Standard Inquiry Data request */
3117 			struct scsi_inquiry inq;
3118 			unsigned int bufsize;
3119 
3120 			sata_identdev_to_inquiry(spx->txlt_sata_hba_inst,
3121 			    sdinfo, (uint8_t *)&inq);
3122 			/* Copy no more than requested */
3123 			count = MIN(bp->b_bcount,
3124 			    sizeof (struct scsi_inquiry));
3125 			bufsize = scsipkt->pkt_cdbp[4];
3126 			bufsize |= scsipkt->pkt_cdbp[3] << 8;
3127 			count = MIN(count, bufsize);
3128 			bcopy(&inq, bp->b_un.b_addr, count);
3129 
3130 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
3131 			scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3132 			    bufsize - count : 0;
3133 		} else {
3134 			/*
3135 			 * peripheral_qualifier = 0;
3136 			 *
3137 			 * We are dealing only with HD and will be
3138 			 * dealing with CD/DVD devices soon
3139 			 */
3140 			uint8_t peripheral_device_type =
3141 			    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3142 			    DTYPE_DIRECT : DTYPE_RODIRECT;
3143 
3144 			switch ((uint_t)scsipkt->pkt_cdbp[2]) {
3145 			case INQUIRY_SUP_VPD_PAGE:
3146 				/*
3147 				 * Request for suported Vital Product Data
3148 				 * pages - assuming only 2 page codes
3149 				 * supported
3150 				 */
3151 				page_buf[0] = peripheral_device_type;
3152 				page_buf[1] = INQUIRY_SUP_VPD_PAGE;
3153 				page_buf[2] = 0;
3154 				page_buf[3] = 2; /* page length */
3155 				page_buf[4] = INQUIRY_SUP_VPD_PAGE;
3156 				page_buf[5] = INQUIRY_USN_PAGE;
3157 				/* Copy no more than requested */
3158 				count = MIN(bp->b_bcount, 6);
3159 				bcopy(page_buf, bp->b_un.b_addr, count);
3160 				break;
3161 			case INQUIRY_USN_PAGE:
3162 				/*
3163 				 * Request for Unit Serial Number page
3164 				 */
3165 				page_buf[0] = peripheral_device_type;
3166 				page_buf[1] = INQUIRY_USN_PAGE;
3167 				page_buf[2] = 0;
3168 				page_buf[3] = 20; /* remaining page length */
3169 				p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser);
3170 #ifdef	_LITTLE_ENDIAN
3171 				swab(p, &page_buf[4], 20);
3172 #else
3173 				bcopy(p, &page_buf[4], 20);
3174 #endif
3175 				for (i = 0; i < 20; i++) {
3176 					if (page_buf[4 + i] == '\0' ||
3177 					    page_buf[4 + i] == '\040') {
3178 						break;
3179 					}
3180 				}
3181 				/*
3182 				 * 'i' contains string length.
3183 				 *
3184 				 * Least significant character of the serial
3185 				 * number shall appear as the last byte,
3186 				 * according to SBC-3 spec.
3187 				 */
3188 				p = &page_buf[20 + 4 - 1];
3189 				for (j = i; j > 0; j--, p--) {
3190 					*p = *(p - 20 + i);
3191 				}
3192 				p = &page_buf[4];
3193 				for (j = 20 - i; j > 0; j--) {
3194 					*p++ = '\040';
3195 				}
3196 				count = MIN(bp->b_bcount, 24);
3197 				bcopy(page_buf, bp->b_un.b_addr, count);
3198 				break;
3199 
3200 			case INQUIRY_DEV_IDENTIFICATION_PAGE:
3201 				/*
3202 				 * We may want to implement this page, when
3203 				 * identifiers are common for SATA devices
3204 				 * But not now.
3205 				 */
3206 				/*FALLTHROUGH*/
3207 
3208 			default:
3209 				/* Request for unsupported VPD page */
3210 				*scsipkt->pkt_scbp = STATUS_CHECK;
3211 				sense = sata_arq_sense(spx);
3212 				sense->es_key = KEY_ILLEGAL_REQUEST;
3213 				sense->es_add_code =
3214 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3215 				goto done;
3216 			}
3217 		}
3218 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3219 		scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3220 		    scsipkt->pkt_cdbp[4] - count : 0;
3221 	}
3222 done:
3223 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3224 
3225 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3226 	    "Scsi_pkt completion reason %x\n",
3227 	    scsipkt->pkt_reason);
3228 
3229 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3230 	    scsipkt->pkt_comp != NULL) {
3231 		/* scsi callback required */
3232 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3233 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3234 		    TQ_SLEEP) == NULL)
3235 			/* Scheduling the callback failed */
3236 			return (TRAN_BUSY);
3237 	}
3238 	return (TRAN_ACCEPT);
3239 }
3240 
3241 /*
3242  * SATA translate command: Request Sense.
3243  * Emulated command (ATA version for SATA hard disks)
3244  * Always NO SENSE, because any sense data should be reported by ARQ sense.
3245  *
3246  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3247  */
3248 static int
3249 sata_txlt_request_sense(sata_pkt_txlate_t *spx)
3250 {
3251 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3252 	struct scsi_extended_sense sense;
3253 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3254 	int rval, reason;
3255 
3256 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3257 
3258 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3259 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3260 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3261 		return (rval);
3262 	}
3263 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3264 
3265 
3266 	scsipkt->pkt_reason = CMD_CMPLT;
3267 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3268 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3269 	*scsipkt->pkt_scbp = STATUS_GOOD;
3270 
3271 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3272 		/*
3273 		 * Because it is fully emulated command storing data
3274 		 * programatically in the specified buffer, release
3275 		 * preallocated DMA resources before storing data in the buffer,
3276 		 * so no unwanted DMA sync would take place.
3277 		 */
3278 		int count = MIN(bp->b_bcount,
3279 		    sizeof (struct scsi_extended_sense));
3280 		sata_scsi_dmafree(NULL, scsipkt);
3281 		bzero(&sense, sizeof (struct scsi_extended_sense));
3282 		sense.es_valid = 0;	/* Valid LBA */
3283 		sense.es_class = 7;	/* Response code 0x70 - current err */
3284 		sense.es_key = KEY_NO_SENSE;
3285 		sense.es_add_len = 6;	/* Additional length */
3286 		/* Copy no more than requested */
3287 		bcopy(&sense, bp->b_un.b_addr, count);
3288 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3289 		scsipkt->pkt_resid = 0;
3290 	}
3291 
3292 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3293 	    "Scsi_pkt completion reason %x\n",
3294 	    scsipkt->pkt_reason);
3295 
3296 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3297 	    scsipkt->pkt_comp != NULL)
3298 		/* scsi callback required */
3299 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3300 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3301 		    TQ_SLEEP) == NULL)
3302 			/* Scheduling the callback failed */
3303 			return (TRAN_BUSY);
3304 	return (TRAN_ACCEPT);
3305 }
3306 
3307 /*
3308  * SATA translate command: Test Unit Ready
3309  * At the moment this is an emulated command (ATA version for SATA hard disks).
3310  * May be translated into Check Power Mode command in the future
3311  *
3312  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3313  */
3314 static int
3315 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx)
3316 {
3317 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3318 	struct scsi_extended_sense *sense;
3319 	int power_state;
3320 	int rval, reason;
3321 
3322 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3323 
3324 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3325 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3326 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3327 		return (rval);
3328 	}
3329 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3330 
3331 	/* At this moment, emulate it rather than execute anything */
3332 	power_state = SATA_PWRMODE_ACTIVE;
3333 
3334 	scsipkt->pkt_reason = CMD_CMPLT;
3335 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3336 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3337 
3338 	switch (power_state) {
3339 	case SATA_PWRMODE_ACTIVE:
3340 	case SATA_PWRMODE_IDLE:
3341 		*scsipkt->pkt_scbp = STATUS_GOOD;
3342 		break;
3343 	default:
3344 		/* PWR mode standby */
3345 		*scsipkt->pkt_scbp = STATUS_CHECK;
3346 		sense = sata_arq_sense(spx);
3347 		sense->es_key = KEY_NOT_READY;
3348 		sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY;
3349 		break;
3350 	}
3351 
3352 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3353 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3354 
3355 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3356 	    scsipkt->pkt_comp != NULL)
3357 		/* scsi callback required */
3358 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3359 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3360 		    TQ_SLEEP) == NULL)
3361 			/* Scheduling the callback failed */
3362 			return (TRAN_BUSY);
3363 
3364 	return (TRAN_ACCEPT);
3365 }
3366 
3367 
3368 /*
3369  * SATA translate command: Start Stop Unit
3370  * Translation depends on a command:
3371  *	Start Unit translated into Idle Immediate
3372  *	Stop Unit translated into Standby Immediate
3373  *	Unload Media / NOT SUPPORTED YET
3374  *	Load Media / NOT SUPPROTED YET
3375  * Power condition bits are ignored, so is Immediate bit
3376  * Requesting synchronous execution.
3377  *
3378  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
3379  * appropriate values in scsi_pkt fields.
3380  */
3381 static int
3382 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx)
3383 {
3384 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3385 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3386 	struct scsi_extended_sense *sense;
3387 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3388 	int cport = SATA_TXLT_CPORT(spx);
3389 	int rval, reason;
3390 	int synch;
3391 
3392 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3393 	    "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1);
3394 
3395 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3396 
3397 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3398 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3399 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3400 		return (rval);
3401 	}
3402 
3403 	if (scsipkt->pkt_cdbp[4] & 2) {
3404 		/* Load/Unload Media - invalid request */
3405 		*scsipkt->pkt_scbp = STATUS_CHECK;
3406 		sense = sata_arq_sense(spx);
3407 		sense->es_key = KEY_ILLEGAL_REQUEST;
3408 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3409 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3410 
3411 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3412 		    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3413 
3414 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3415 		    scsipkt->pkt_comp != NULL)
3416 			/* scsi callback required */
3417 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3418 			    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3419 			    TQ_SLEEP) == NULL)
3420 				/* Scheduling the callback failed */
3421 				return (TRAN_BUSY);
3422 
3423 		return (TRAN_ACCEPT);
3424 	}
3425 	scmd->satacmd_addr_type = 0;
3426 	scmd->satacmd_sec_count_lsb = 0;
3427 	scmd->satacmd_lba_low_lsb = 0;
3428 	scmd->satacmd_lba_mid_lsb = 0;
3429 	scmd->satacmd_lba_high_lsb = 0;
3430 	scmd->satacmd_features_reg = 0;
3431 	scmd->satacmd_device_reg = 0;
3432 	scmd->satacmd_status_reg = 0;
3433 	if (scsipkt->pkt_cdbp[4] & 1) {
3434 		/* Start Unit */
3435 		scmd->satacmd_cmd_reg = SATAC_IDLE_IM;
3436 	} else {
3437 		/* Stop Unit */
3438 		scmd->satacmd_cmd_reg = SATAC_STANDBY_IM;
3439 	}
3440 
3441 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
3442 		/* Need to set-up a callback function */
3443 		spx->txlt_sata_pkt->satapkt_comp =
3444 		    sata_txlt_nodata_cmd_completion;
3445 		synch = FALSE;
3446 	} else {
3447 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
3448 		synch = TRUE;
3449 	}
3450 
3451 	/* Transfer command to HBA */
3452 	if (sata_hba_start(spx, &rval) != 0) {
3453 		/* Pkt not accepted for execution */
3454 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3455 		return (rval);
3456 	}
3457 
3458 	/*
3459 	 * If execution is non-synchronous,
3460 	 * a callback function will handle potential errors, translate
3461 	 * the response and will do a callback to a target driver.
3462 	 * If it was synchronous, check execution status using the same
3463 	 * framework callback.
3464 	 */
3465 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3466 	if (synch) {
3467 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3468 		    "synchronous execution status %x\n",
3469 		    spx->txlt_sata_pkt->satapkt_reason);
3470 
3471 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
3472 	}
3473 	return (TRAN_ACCEPT);
3474 
3475 }
3476 
3477 
3478 /*
3479  * SATA translate command:  Read Capacity.
3480  * Emulated command for SATA disks.
3481  * Capacity is retrieved from cached Idenifty Device data.
3482  * Identify Device data shows effective disk capacity, not the native
3483  * capacity, which may be limitted by Set Max Address command.
3484  * This is ATA version for SATA hard disks.
3485  *
3486  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3487  */
3488 static int
3489 sata_txlt_read_capacity(sata_pkt_txlate_t *spx)
3490 {
3491 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3492 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3493 	sata_drive_info_t *sdinfo;
3494 	uint64_t val;
3495 	uchar_t *rbuf;
3496 	int rval, reason;
3497 
3498 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3499 	    "sata_txlt_read_capacity: ", NULL);
3500 
3501 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3502 
3503 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3504 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3505 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3506 		return (rval);
3507 	}
3508 
3509 	scsipkt->pkt_reason = CMD_CMPLT;
3510 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3511 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3512 	*scsipkt->pkt_scbp = STATUS_GOOD;
3513 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3514 		/*
3515 		 * Because it is fully emulated command storing data
3516 		 * programatically in the specified buffer, release
3517 		 * preallocated DMA resources before storing data in the buffer,
3518 		 * so no unwanted DMA sync would take place.
3519 		 */
3520 		sata_scsi_dmafree(NULL, scsipkt);
3521 
3522 		sdinfo = sata_get_device_info(
3523 		    spx->txlt_sata_hba_inst,
3524 		    &spx->txlt_sata_pkt->satapkt_device);
3525 		/* Last logical block address */
3526 		val = sdinfo->satadrv_capacity - 1;
3527 		rbuf = (uchar_t *)bp->b_un.b_addr;
3528 		/* Need to swap endians to match scsi format */
3529 		rbuf[0] = (val >> 24) & 0xff;
3530 		rbuf[1] = (val >> 16) & 0xff;
3531 		rbuf[2] = (val >> 8) & 0xff;
3532 		rbuf[3] = val & 0xff;
3533 		/* block size - always 512 bytes, for now */
3534 		rbuf[4] = 0;
3535 		rbuf[5] = 0;
3536 		rbuf[6] = 0x02;
3537 		rbuf[7] = 0;
3538 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3539 		scsipkt->pkt_resid = 0;
3540 
3541 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n",
3542 		    sdinfo->satadrv_capacity -1);
3543 	}
3544 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3545 	/*
3546 	 * If a callback was requested, do it now.
3547 	 */
3548 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3549 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3550 
3551 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3552 	    scsipkt->pkt_comp != NULL)
3553 		/* scsi callback required */
3554 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3555 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3556 		    TQ_SLEEP) == NULL)
3557 			/* Scheduling the callback failed */
3558 			return (TRAN_BUSY);
3559 
3560 	return (TRAN_ACCEPT);
3561 }
3562 
3563 /*
3564  * SATA translate command: Mode Sense.
3565  * Translated into appropriate SATA command or emulated.
3566  * Saved Values Page Control (03) are not supported.
3567  *
3568  * NOTE: only caching mode sense page is currently implemented.
3569  *
3570  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3571  */
3572 
3573 static int
3574 sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
3575 {
3576 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
3577 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3578 	sata_drive_info_t *sdinfo;
3579 	sata_id_t *sata_id;
3580 	struct scsi_extended_sense *sense;
3581 	int 		len, bdlen, count, alc_len;
3582 	int		pc;	/* Page Control code */
3583 	uint8_t		*buf;	/* mode sense buffer */
3584 	int		rval, reason;
3585 
3586 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3587 	    "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
3588 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
3589 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
3590 
3591 	buf = kmem_zalloc(1024, KM_SLEEP);
3592 
3593 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3594 
3595 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3596 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3597 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3598 		kmem_free(buf, 1024);
3599 		return (rval);
3600 	}
3601 
3602 	scsipkt->pkt_reason = CMD_CMPLT;
3603 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3604 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3605 
3606 	pc = scsipkt->pkt_cdbp[2] >> 6;
3607 
3608 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3609 		/*
3610 		 * Because it is fully emulated command storing data
3611 		 * programatically in the specified buffer, release
3612 		 * preallocated DMA resources before storing data in the buffer,
3613 		 * so no unwanted DMA sync would take place.
3614 		 */
3615 		sata_scsi_dmafree(NULL, scsipkt);
3616 
3617 		len = 0;
3618 		bdlen = 0;
3619 		if (!(scsipkt->pkt_cdbp[1] & 8)) {
3620 			if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
3621 			    (scsipkt->pkt_cdbp[0] & 0x10))
3622 				bdlen = 16;
3623 			else
3624 				bdlen = 8;
3625 		}
3626 		/* Build mode parameter header */
3627 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3628 			/* 4-byte mode parameter header */
3629 			buf[len++] = 0;   	/* mode data length */
3630 			buf[len++] = 0;		/* medium type */
3631 			buf[len++] = 0;		/* dev-specific param */
3632 			buf[len++] = bdlen;	/* Block Descriptor length */
3633 		} else {
3634 			/* 8-byte mode parameter header */
3635 			buf[len++] = 0;		/* mode data length */
3636 			buf[len++] = 0;
3637 			buf[len++] = 0;		/* medium type */
3638 			buf[len++] = 0;		/* dev-specific param */
3639 			if (bdlen == 16)
3640 				buf[len++] = 1;	/* long lba descriptor */
3641 			else
3642 				buf[len++] = 0;
3643 			buf[len++] = 0;
3644 			buf[len++] = 0;		/* Block Descriptor length */
3645 			buf[len++] = bdlen;
3646 		}
3647 
3648 		sdinfo = sata_get_device_info(
3649 		    spx->txlt_sata_hba_inst,
3650 		    &spx->txlt_sata_pkt->satapkt_device);
3651 
3652 		/* Build block descriptor only if not disabled (DBD) */
3653 		if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
3654 			/* Block descriptor - direct-access device format */
3655 			if (bdlen == 8) {
3656 				/* build regular block descriptor */
3657 				buf[len++] =
3658 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
3659 				buf[len++] =
3660 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
3661 				buf[len++] =
3662 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
3663 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
3664 				buf[len++] = 0; /* density code */
3665 				buf[len++] = 0;
3666 				if (sdinfo->satadrv_type ==
3667 				    SATA_DTYPE_ATADISK)
3668 					buf[len++] = 2;
3669 				else
3670 					/* ATAPI */
3671 					buf[len++] = 8;
3672 				buf[len++] = 0;
3673 			} else if (bdlen == 16) {
3674 				/* Long LBA Accepted */
3675 				/* build long lba block descriptor */
3676 #ifndef __lock_lint
3677 				buf[len++] =
3678 				    (sdinfo->satadrv_capacity >> 56) & 0xff;
3679 				buf[len++] =
3680 				    (sdinfo->satadrv_capacity >> 48) & 0xff;
3681 				buf[len++] =
3682 				    (sdinfo->satadrv_capacity >> 40) & 0xff;
3683 				buf[len++] =
3684 				    (sdinfo->satadrv_capacity >> 32) & 0xff;
3685 #endif
3686 				buf[len++] =
3687 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
3688 				buf[len++] =
3689 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
3690 				buf[len++] =
3691 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
3692 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
3693 				buf[len++] = 0;
3694 				buf[len++] = 0; /* density code */
3695 				buf[len++] = 0;
3696 				buf[len++] = 0;
3697 				if (sdinfo->satadrv_type ==
3698 				    SATA_DTYPE_ATADISK)
3699 					buf[len++] = 2;
3700 				else
3701 					/* ATAPI */
3702 					buf[len++] = 8;
3703 				buf[len++] = 0;
3704 			}
3705 		}
3706 
3707 		sata_id = &sdinfo->satadrv_id;
3708 
3709 		/*
3710 		 * Add requested pages.
3711 		 * Page 3 and 4 are obsolete and we are not supporting them.
3712 		 * We deal now with:
3713 		 * caching (read/write cache control).
3714 		 * We should eventually deal with following mode pages:
3715 		 * error recovery  (0x01),
3716 		 * power condition (0x1a),
3717 		 * exception control page (enables SMART) (0x1c),
3718 		 * enclosure management (ses),
3719 		 * protocol-specific port mode (port control).
3720 		 */
3721 		switch (scsipkt->pkt_cdbp[2] & 0x3f) {
3722 		case MODEPAGE_RW_ERRRECOV:
3723 			/* DAD_MODE_ERR_RECOV */
3724 			/* R/W recovery */
3725 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
3726 			break;
3727 		case MODEPAGE_CACHING:
3728 			/* DAD_MODE_CACHE */
3729 			/* Reject not supported request for saved parameters */
3730 			if (pc == 3) {
3731 				*scsipkt->pkt_scbp = STATUS_CHECK;
3732 				sense = sata_arq_sense(spx);
3733 				sense->es_key = KEY_ILLEGAL_REQUEST;
3734 				sense->es_add_code =
3735 				    SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
3736 				goto done;
3737 			}
3738 
3739 			/* caching */
3740 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
3741 			break;
3742 		case MODEPAGE_INFO_EXCPT:
3743 			/* exception cntrl */
3744 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
3745 				len += sata_build_msense_page_1c(sdinfo, pc,
3746 				    buf+len);
3747 			}
3748 			else
3749 				goto err;
3750 			break;
3751 		case MODEPAGE_POWER_COND:
3752 			/* DAD_MODE_POWER_COND */
3753 			/* power condition */
3754 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
3755 			break;
3756 
3757 		case MODEPAGE_ACOUSTIC_MANAG:
3758 			/* acoustic management */
3759 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
3760 			break;
3761 		case MODEPAGE_ALLPAGES:
3762 			/* all pages */
3763 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
3764 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
3765 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
3766 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
3767 				len += sata_build_msense_page_1c(sdinfo, pc,
3768 				    buf+len);
3769 			}
3770 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
3771 			break;
3772 		default:
3773 		err:
3774 			/* Invalid request */
3775 			*scsipkt->pkt_scbp = STATUS_CHECK;
3776 			sense = sata_arq_sense(spx);
3777 			sense->es_key = KEY_ILLEGAL_REQUEST;
3778 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3779 			goto done;
3780 		}
3781 
3782 		/* fix total mode data length */
3783 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3784 			/* 4-byte mode parameter header */
3785 			buf[0] = len - 1;   	/* mode data length */
3786 		} else {
3787 			buf[0] = (len -2) >> 8;
3788 			buf[1] = (len -2) & 0xff;
3789 		}
3790 
3791 
3792 		/* Check allocation length */
3793 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3794 			alc_len = scsipkt->pkt_cdbp[4];
3795 		} else {
3796 			alc_len = scsipkt->pkt_cdbp[7];
3797 			alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
3798 		}
3799 		/*
3800 		 * We do not check for possible parameters truncation
3801 		 * (alc_len < len) assuming that the target driver works
3802 		 * correctly. Just avoiding overrun.
3803 		 * Copy no more than requested and possible, buffer-wise.
3804 		 */
3805 		count = MIN(alc_len, len);
3806 		count = MIN(bp->b_bcount, count);
3807 		bcopy(buf, bp->b_un.b_addr, count);
3808 
3809 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3810 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
3811 	}
3812 	*scsipkt->pkt_scbp = STATUS_GOOD;
3813 done:
3814 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3815 	(void) kmem_free(buf, 1024);
3816 
3817 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3818 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3819 
3820 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3821 	    scsipkt->pkt_comp != NULL)
3822 		/* scsi callback required */
3823 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3824 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3825 		    TQ_SLEEP) == NULL)
3826 			/* Scheduling the callback failed */
3827 			return (TRAN_BUSY);
3828 
3829 	return (TRAN_ACCEPT);
3830 }
3831 
3832 
3833 /*
3834  * SATA translate command: Mode Select.
3835  * Translated into appropriate SATA command or emulated.
3836  * Saving parameters is not supported.
3837  * Changing device capacity is not supported (although theoretically
3838  * possible by executing SET FEATURES/SET MAX ADDRESS)
3839  *
3840  * Assumption is that the target driver is working correctly.
3841  *
3842  * More than one SATA command may be executed to perform operations specified
3843  * by mode select pages. The first error terminates further execution.
3844  * Operations performed successully are not backed-up in such case.
3845  *
3846  * NOTE: only caching mode select page is implemented.
3847  * Caching setup is remembered so it could be re-stored in case of
3848  * an unexpected device reset.
3849  *
3850  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3851  */
3852 
3853 static int
3854 sata_txlt_mode_select(sata_pkt_txlate_t *spx)
3855 {
3856 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3857 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3858 	struct scsi_extended_sense *sense;
3859 	int len, pagelen, count, pllen;
3860 	uint8_t *buf;	/* mode select buffer */
3861 	int rval, stat, reason;
3862 	uint_t nointr_flag;
3863 	int dmod = 0;
3864 
3865 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3866 	    "sata_txlt_mode_select, pc %x page code 0x%02x\n",
3867 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
3868 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
3869 
3870 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3871 
3872 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3873 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3874 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3875 		return (rval);
3876 	}
3877 
3878 	rval = TRAN_ACCEPT;
3879 
3880 	scsipkt->pkt_reason = CMD_CMPLT;
3881 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3882 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3883 
3884 	/* Reject not supported request */
3885 	if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
3886 		*scsipkt->pkt_scbp = STATUS_CHECK;
3887 		sense = sata_arq_sense(spx);
3888 		sense->es_key = KEY_ILLEGAL_REQUEST;
3889 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3890 		goto done;
3891 	}
3892 
3893 	if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
3894 		pllen = scsipkt->pkt_cdbp[4];
3895 	} else {
3896 		pllen = scsipkt->pkt_cdbp[7];
3897 		pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
3898 	}
3899 
3900 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
3901 
3902 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
3903 		buf = (uint8_t *)bp->b_un.b_addr;
3904 		count = MIN(bp->b_bcount, pllen);
3905 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3906 		scsipkt->pkt_resid = 0;
3907 		pllen = count;
3908 
3909 		/*
3910 		 * Check the header to skip the block descriptor(s) - we
3911 		 * do not support setting device capacity.
3912 		 * Existing macros do not recognize long LBA dscriptor,
3913 		 * hence manual calculation.
3914 		 */
3915 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
3916 			/* 6-bytes CMD, 4 bytes header */
3917 			if (count <= 4)
3918 				goto done;		/* header only */
3919 			len = buf[3] + 4;
3920 		} else {
3921 			/* 10-bytes CMD, 8 bytes header */
3922 			if (count <= 8)
3923 				goto done;		/* header only */
3924 			len = buf[6];
3925 			len = (len << 8) + buf[7] + 8;
3926 		}
3927 		if (len >= count)
3928 			goto done;	/* header + descriptor(s) only */
3929 
3930 		pllen -= len;		/* remaining data length */
3931 
3932 		/*
3933 		 * We may be executing SATA command and want to execute it
3934 		 * in SYNCH mode, regardless of scsi_pkt setting.
3935 		 * Save scsi_pkt setting and indicate SYNCH mode
3936 		 */
3937 		nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
3938 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3939 		    scsipkt->pkt_comp != NULL) {
3940 			scsipkt->pkt_flags |= FLAG_NOINTR;
3941 		}
3942 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
3943 
3944 		/*
3945 		 * len is now the offset to a first mode select page
3946 		 * Process all pages
3947 		 */
3948 		while (pllen > 0) {
3949 			switch ((int)buf[len]) {
3950 			case MODEPAGE_CACHING:
3951 				/* No support for SP (saving) */
3952 				if (scsipkt->pkt_cdbp[1] & 0x01) {
3953 					*scsipkt->pkt_scbp = STATUS_CHECK;
3954 					sense = sata_arq_sense(spx);
3955 					sense->es_key = KEY_ILLEGAL_REQUEST;
3956 					sense->es_add_code =
3957 					    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3958 					goto done;
3959 				}
3960 				stat = sata_mode_select_page_8(spx,
3961 				    (struct mode_cache_scsi3 *)&buf[len],
3962 				    pllen, &pagelen, &rval, &dmod);
3963 				/*
3964 				 * The pagelen value indicates the number of
3965 				 * parameter bytes already processed.
3966 				 * The rval is the return value from
3967 				 * sata_tran_start().
3968 				 * The stat indicates the overall status of
3969 				 * the operation(s).
3970 				 */
3971 				if (stat != SATA_SUCCESS)
3972 					/*
3973 					 * Page processing did not succeed -
3974 					 * all error info is already set-up,
3975 					 * just return
3976 					 */
3977 					pllen = 0; /* this breaks the loop */
3978 				else {
3979 					len += pagelen;
3980 					pllen -= pagelen;
3981 				}
3982 				break;
3983 
3984 			case MODEPAGE_INFO_EXCPT:
3985 				stat = sata_mode_select_page_1c(spx,
3986 				    (struct mode_info_excpt_page *)&buf[len],
3987 				    pllen, &pagelen, &rval, &dmod);
3988 				/*
3989 				 * The pagelen value indicates the number of
3990 				 * parameter bytes already processed.
3991 				 * The rval is the return value from
3992 				 * sata_tran_start().
3993 				 * The stat indicates the overall status of
3994 				 * the operation(s).
3995 				 */
3996 				if (stat != SATA_SUCCESS)
3997 					/*
3998 					 * Page processing did not succeed -
3999 					 * all error info is already set-up,
4000 					 * just return
4001 					 */
4002 					pllen = 0; /* this breaks the loop */
4003 				else {
4004 					len += pagelen;
4005 					pllen -= pagelen;
4006 				}
4007 				break;
4008 
4009 			case MODEPAGE_ACOUSTIC_MANAG:
4010 				stat = sata_mode_select_page_30(spx,
4011 				    (struct mode_acoustic_management *)
4012 				    &buf[len], pllen, &pagelen, &rval, &dmod);
4013 				/*
4014 				 * The pagelen value indicates the number of
4015 				 * parameter bytes already processed.
4016 				 * The rval is the return value from
4017 				 * sata_tran_start().
4018 				 * The stat indicates the overall status of
4019 				 * the operation(s).
4020 				 */
4021 				if (stat != SATA_SUCCESS)
4022 					/*
4023 					 * Page processing did not succeed -
4024 					 * all error info is already set-up,
4025 					 * just return
4026 					 */
4027 					pllen = 0; /* this breaks the loop */
4028 				else {
4029 					len += pagelen;
4030 					pllen -= pagelen;
4031 				}
4032 
4033 				break;
4034 			default:
4035 				*scsipkt->pkt_scbp = STATUS_CHECK;
4036 				sense = sata_arq_sense(spx);
4037 				sense->es_key = KEY_ILLEGAL_REQUEST;
4038 				sense->es_add_code =
4039 				    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
4040 				goto done;
4041 			}
4042 		}
4043 	}
4044 done:
4045 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4046 	/*
4047 	 * If device parameters were modified, fetch and store the new
4048 	 * Identify Device data. Since port mutex could have been released
4049 	 * for accessing HBA driver, we need to re-check device existence.
4050 	 */
4051 	if (dmod != 0) {
4052 		sata_drive_info_t new_sdinfo, *sdinfo;
4053 		int rv = 0;
4054 
4055 		/*
4056 		 * Following statement has to be changed if this function is
4057 		 * used for devices other than SATA hard disks.
4058 		 */
4059 		new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
4060 
4061 		new_sdinfo.satadrv_addr =
4062 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr;
4063 		rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
4064 		    &new_sdinfo);
4065 
4066 		mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4067 		/*
4068 		 * Since port mutex could have been released when
4069 		 * accessing HBA driver, we need to re-check that the
4070 		 * framework still holds the device info structure.
4071 		 */
4072 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4073 		    &spx->txlt_sata_pkt->satapkt_device);
4074 		if (sdinfo != NULL) {
4075 			/*
4076 			 * Device still has info structure in the
4077 			 * sata framework. Copy newly fetched info
4078 			 */
4079 			if (rv == 0) {
4080 				sdinfo->satadrv_id = new_sdinfo.satadrv_id;
4081 				sata_save_drive_settings(sdinfo);
4082 			} else {
4083 				/*
4084 				 * Could not fetch new data - invalidate
4085 				 * sata_drive_info. That makes device
4086 				 * unusable.
4087 				 */
4088 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
4089 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
4090 			}
4091 		}
4092 		if (rv != 0 || sdinfo == NULL) {
4093 			/*
4094 			 * This changes the overall mode select completion
4095 			 * reason to a failed one !!!!!
4096 			 */
4097 			*scsipkt->pkt_scbp = STATUS_CHECK;
4098 			sense = sata_arq_sense(spx);
4099 			scsipkt->pkt_reason = CMD_INCOMPLETE;
4100 			rval = TRAN_ACCEPT;
4101 		}
4102 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4103 	}
4104 	/* Restore the scsi pkt flags */
4105 	scsipkt->pkt_flags &= ~FLAG_NOINTR;
4106 	scsipkt->pkt_flags |= nointr_flag;
4107 
4108 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4109 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4110 
4111 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4112 	    scsipkt->pkt_comp != NULL)
4113 		/* scsi callback required */
4114 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4115 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4116 		    TQ_SLEEP) == NULL)
4117 			/* Scheduling the callback failed */
4118 			return (TRAN_BUSY);
4119 
4120 	return (rval);
4121 }
4122 
4123 
4124 
4125 /*
4126  * Translate command: Log Sense
4127  */
4128 static 	int
4129 sata_txlt_log_sense(sata_pkt_txlate_t *spx)
4130 {
4131 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4132 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4133 	sata_drive_info_t *sdinfo;
4134 	struct scsi_extended_sense *sense;
4135 	int 		len, count, alc_len;
4136 	int		pc;	/* Page Control code */
4137 	int		page_code;	/* Page code */
4138 	uint8_t		*buf;	/* log sense buffer */
4139 	int		rval, reason;
4140 #define	MAX_LOG_SENSE_PAGE_SIZE	512
4141 
4142 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4143 	    "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
4144 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4145 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4146 
4147 	buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
4148 
4149 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4150 
4151 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4152 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4153 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4154 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4155 		return (rval);
4156 	}
4157 
4158 	scsipkt->pkt_reason = CMD_CMPLT;
4159 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4160 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4161 
4162 	pc = scsipkt->pkt_cdbp[2] >> 6;
4163 	page_code = scsipkt->pkt_cdbp[2] & 0x3f;
4164 
4165 	/* Reject not supported request for all but cumulative values */
4166 	switch (pc) {
4167 	case PC_CUMULATIVE_VALUES:
4168 		break;
4169 	default:
4170 		*scsipkt->pkt_scbp = STATUS_CHECK;
4171 		sense = sata_arq_sense(spx);
4172 		sense->es_key = KEY_ILLEGAL_REQUEST;
4173 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4174 		goto done;
4175 	}
4176 
4177 	switch (page_code) {
4178 	case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
4179 	case PAGE_CODE_SELF_TEST_RESULTS:
4180 	case PAGE_CODE_INFORMATION_EXCEPTIONS:
4181 	case PAGE_CODE_SMART_READ_DATA:
4182 		break;
4183 	default:
4184 		*scsipkt->pkt_scbp = STATUS_CHECK;
4185 		sense = sata_arq_sense(spx);
4186 		sense->es_key = KEY_ILLEGAL_REQUEST;
4187 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4188 		goto done;
4189 	}
4190 
4191 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4192 		/*
4193 		 * Because log sense uses local buffers for data retrieval from
4194 		 * the devices and sets the data programatically in the
4195 		 * original specified buffer, release preallocated DMA
4196 		 * resources before storing data in the original buffer,
4197 		 * so no unwanted DMA sync would take place.
4198 		 */
4199 		sata_id_t *sata_id;
4200 
4201 		sata_scsi_dmafree(NULL, scsipkt);
4202 
4203 		len = 0;
4204 
4205 		/* Build log parameter header */
4206 		buf[len++] = page_code;	/* page code as in the CDB */
4207 		buf[len++] = 0;		/* reserved */
4208 		buf[len++] = 0;		/* Zero out page length for now (MSB) */
4209 		buf[len++] = 0;		/* (LSB) */
4210 
4211 		sdinfo = sata_get_device_info(
4212 		    spx->txlt_sata_hba_inst,
4213 		    &spx->txlt_sata_pkt->satapkt_device);
4214 
4215 
4216 		/*
4217 		 * Add requested pages.
4218 		 */
4219 		switch (page_code) {
4220 		case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
4221 			len = sata_build_lsense_page_0(sdinfo, buf + len);
4222 			break;
4223 		case PAGE_CODE_SELF_TEST_RESULTS:
4224 			sata_id = &sdinfo->satadrv_id;
4225 			if ((! (sata_id->ai_cmdset84 &
4226 			    SATA_SMART_SELF_TEST_SUPPORTED)) ||
4227 			    (! (sata_id->ai_features87 &
4228 			    SATA_SMART_SELF_TEST_SUPPORTED))) {
4229 				*scsipkt->pkt_scbp = STATUS_CHECK;
4230 				sense = sata_arq_sense(spx);
4231 				sense->es_key = KEY_ILLEGAL_REQUEST;
4232 				sense->es_add_code =
4233 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4234 
4235 				goto done;
4236 			}
4237 			len = sata_build_lsense_page_10(sdinfo, buf + len,
4238 			    spx->txlt_sata_hba_inst);
4239 			break;
4240 		case PAGE_CODE_INFORMATION_EXCEPTIONS:
4241 			sata_id = &sdinfo->satadrv_id;
4242 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
4243 				*scsipkt->pkt_scbp = STATUS_CHECK;
4244 				sense = sata_arq_sense(spx);
4245 				sense->es_key = KEY_ILLEGAL_REQUEST;
4246 				sense->es_add_code =
4247 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4248 
4249 				goto done;
4250 			}
4251 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
4252 				*scsipkt->pkt_scbp = STATUS_CHECK;
4253 				sense = sata_arq_sense(spx);
4254 				sense->es_key = KEY_ABORTED_COMMAND;
4255 				sense->es_add_code =
4256 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
4257 				sense->es_qual_code =
4258 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
4259 
4260 				goto done;
4261 			}
4262 
4263 			len = sata_build_lsense_page_2f(sdinfo, buf + len,
4264 			    spx->txlt_sata_hba_inst);
4265 			break;
4266 		case PAGE_CODE_SMART_READ_DATA:
4267 			sata_id = &sdinfo->satadrv_id;
4268 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
4269 				*scsipkt->pkt_scbp = STATUS_CHECK;
4270 				sense = sata_arq_sense(spx);
4271 				sense->es_key = KEY_ILLEGAL_REQUEST;
4272 				sense->es_add_code =
4273 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4274 
4275 				goto done;
4276 			}
4277 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
4278 				*scsipkt->pkt_scbp = STATUS_CHECK;
4279 				sense = sata_arq_sense(spx);
4280 				sense->es_key = KEY_ABORTED_COMMAND;
4281 				sense->es_add_code =
4282 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
4283 				sense->es_qual_code =
4284 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
4285 
4286 				goto done;
4287 			}
4288 
4289 			/* This page doesn't include a page header */
4290 			len = sata_build_lsense_page_30(sdinfo, buf,
4291 			    spx->txlt_sata_hba_inst);
4292 			goto no_header;
4293 		default:
4294 			/* Invalid request */
4295 			*scsipkt->pkt_scbp = STATUS_CHECK;
4296 			sense = sata_arq_sense(spx);
4297 			sense->es_key = KEY_ILLEGAL_REQUEST;
4298 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4299 			goto done;
4300 		}
4301 
4302 		/* set parameter log sense data length */
4303 		buf[2] = len >> 8;	/* log sense length (MSB) */
4304 		buf[3] = len & 0xff;	/* log sense length (LSB) */
4305 
4306 		len += SCSI_LOG_PAGE_HDR_LEN;
4307 		ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
4308 
4309 no_header:
4310 		/* Check allocation length */
4311 		alc_len = scsipkt->pkt_cdbp[7];
4312 		alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
4313 
4314 		/*
4315 		 * We do not check for possible parameters truncation
4316 		 * (alc_len < len) assuming that the target driver works
4317 		 * correctly. Just avoiding overrun.
4318 		 * Copy no more than requested and possible, buffer-wise.
4319 		 */
4320 		count = MIN(alc_len, len);
4321 		count = MIN(bp->b_bcount, count);
4322 		bcopy(buf, bp->b_un.b_addr, count);
4323 
4324 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4325 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
4326 	}
4327 	*scsipkt->pkt_scbp = STATUS_GOOD;
4328 done:
4329 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4330 	(void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4331 
4332 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4333 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4334 
4335 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4336 	    scsipkt->pkt_comp != NULL)
4337 		/* scsi callback required */
4338 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4339 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4340 		    TQ_SLEEP) == NULL)
4341 			/* Scheduling the callback failed */
4342 			return (TRAN_BUSY);
4343 
4344 	return (TRAN_ACCEPT);
4345 }
4346 
4347 /*
4348  * Translate command: Log Select
4349  * Not implemented at this time - returns invalid command response.
4350  */
4351 static 	int
4352 sata_txlt_log_select(sata_pkt_txlate_t *spx)
4353 {
4354 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4355 	    "sata_txlt_log_select\n", NULL);
4356 
4357 	return (sata_txlt_invalid_command(spx));
4358 }
4359 
4360 
4361 /*
4362  * Translate command: Read (various types).
4363  * Translated into appropriate type of ATA READ command
4364  * for SATA hard disks.
4365  * Both the device capabilities and requested operation mode are
4366  * considered.
4367  *
4368  * Following scsi cdb fields are ignored:
4369  * rdprotect, dpo, fua, fua_nv, group_number.
4370  *
4371  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
4372  * enable variable sata_func_enable), the capability of the controller and
4373  * capability of a device are checked and if both support queueing, read
4374  * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
4375  * command rather than plain READ_XXX command.
4376  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
4377  * both the controller and device suport such functionality, the read
4378  * request will be translated to READ_FPDMA_QUEUED command.
4379  * In both cases the maximum queue depth is derived as minimum of:
4380  * HBA capability,device capability and sata_max_queue_depth variable setting.
4381  * The value passed to HBA driver is decremented by 1, because only 5 bits are
4382  * used to pass max queue depth value, and the maximum possible queue depth
4383  * is 32.
4384  *
4385  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4386  * appropriate values in scsi_pkt fields.
4387  */
4388 static int
4389 sata_txlt_read(sata_pkt_txlate_t *spx)
4390 {
4391 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4392 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4393 	sata_drive_info_t *sdinfo;
4394 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4395 	int cport = SATA_TXLT_CPORT(spx);
4396 	uint16_t sec_count;
4397 	uint64_t lba;
4398 	int rval, reason;
4399 	int synch;
4400 
4401 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4402 
4403 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4404 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4405 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4406 		return (rval);
4407 	}
4408 
4409 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4410 	    &spx->txlt_sata_pkt->satapkt_device);
4411 
4412 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
4413 	/*
4414 	 * Extract LBA and sector count from scsi CDB.
4415 	 */
4416 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
4417 	case SCMD_READ:
4418 		/* 6-byte scsi read cmd : 0x08 */
4419 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
4420 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
4421 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4422 		sec_count = scsipkt->pkt_cdbp[4];
4423 		/* sec_count 0 will be interpreted as 256 by a device */
4424 		break;
4425 	case SCMD_READ_G1:
4426 		/* 10-bytes scsi read command : 0x28 */
4427 		lba = scsipkt->pkt_cdbp[2];
4428 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4429 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4430 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4431 		sec_count = scsipkt->pkt_cdbp[7];
4432 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4433 		break;
4434 	case SCMD_READ_G5:
4435 		/* 12-bytes scsi read command : 0xA8 */
4436 		lba = scsipkt->pkt_cdbp[2];
4437 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4438 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4439 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4440 		sec_count = scsipkt->pkt_cdbp[6];
4441 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
4442 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4443 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
4444 		break;
4445 	case SCMD_READ_G4:
4446 		/* 16-bytes scsi read command : 0x88 */
4447 		lba = scsipkt->pkt_cdbp[2];
4448 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4449 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4450 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4451 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
4452 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
4453 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
4454 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
4455 		sec_count = scsipkt->pkt_cdbp[10];
4456 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
4457 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
4458 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
4459 		break;
4460 	default:
4461 		/* Unsupported command */
4462 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4463 		return (sata_txlt_invalid_command(spx));
4464 	}
4465 
4466 	/*
4467 	 * Check if specified address exceeds device capacity
4468 	 */
4469 	if ((lba >= sdinfo->satadrv_capacity) ||
4470 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
4471 		/* LBA out of range */
4472 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4473 		return (sata_txlt_lba_out_of_range(spx));
4474 	}
4475 
4476 	/*
4477 	 * For zero-length transfer, emulate good completion of the command
4478 	 * (reasons for rejecting the command were already checked).
4479 	 * No DMA resources were allocated.
4480 	 */
4481 	if (spx->txlt_dma_cookie_list == NULL) {
4482 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4483 		return (sata_emul_rw_completion(spx));
4484 	}
4485 
4486 	/*
4487 	 * Build cmd block depending on the device capability and
4488 	 * requested operation mode.
4489 	 * Do not bother with non-dma mode - we are working only with
4490 	 * devices supporting DMA.
4491 	 */
4492 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
4493 	scmd->satacmd_device_reg = SATA_ADH_LBA;
4494 	scmd->satacmd_cmd_reg = SATAC_READ_DMA;
4495 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
4496 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4497 		scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT;
4498 		scmd->satacmd_sec_count_msb = sec_count >> 8;
4499 #ifndef __lock_lint
4500 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
4501 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
4502 		scmd->satacmd_lba_high_msb = lba >> 40;
4503 #endif
4504 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
4505 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
4506 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
4507 	}
4508 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
4509 	scmd->satacmd_lba_low_lsb = lba & 0xff;
4510 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
4511 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
4512 	scmd->satacmd_features_reg = 0;
4513 	scmd->satacmd_status_reg = 0;
4514 	scmd->satacmd_error_reg = 0;
4515 
4516 	/*
4517 	 * Check if queueing commands should be used and switch
4518 	 * to appropriate command if possible
4519 	 */
4520 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
4521 		boolean_t using_queuing;
4522 
4523 		/* Queuing supported by controller and device? */
4524 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
4525 		    (sdinfo->satadrv_features_support &
4526 		    SATA_DEV_F_NCQ) &&
4527 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4528 		    SATA_CTLF_NCQ)) {
4529 			using_queuing = B_TRUE;
4530 
4531 			/* NCQ supported - use FPDMA READ */
4532 			scmd->satacmd_cmd_reg =
4533 			    SATAC_READ_FPDMA_QUEUED;
4534 			scmd->satacmd_features_reg_ext =
4535 			    scmd->satacmd_sec_count_msb;
4536 			scmd->satacmd_sec_count_msb = 0;
4537 		} else if ((sdinfo->satadrv_features_support &
4538 		    SATA_DEV_F_TCQ) &&
4539 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4540 		    SATA_CTLF_QCMD)) {
4541 			using_queuing = B_TRUE;
4542 
4543 			/* Legacy queueing */
4544 			if (sdinfo->satadrv_features_support &
4545 			    SATA_DEV_F_LBA48) {
4546 				scmd->satacmd_cmd_reg =
4547 				    SATAC_READ_DMA_QUEUED_EXT;
4548 				scmd->satacmd_features_reg_ext =
4549 				    scmd->satacmd_sec_count_msb;
4550 				scmd->satacmd_sec_count_msb = 0;
4551 			} else {
4552 				scmd->satacmd_cmd_reg =
4553 				    SATAC_READ_DMA_QUEUED;
4554 			}
4555 		} else	/* NCQ nor legacy queuing not supported */
4556 			using_queuing = B_FALSE;
4557 
4558 		/*
4559 		 * If queuing, the sector count goes in the features register
4560 		 * and the secount count will contain the tag.
4561 		 */
4562 		if (using_queuing) {
4563 			scmd->satacmd_features_reg =
4564 			    scmd->satacmd_sec_count_lsb;
4565 			scmd->satacmd_sec_count_lsb = 0;
4566 			scmd->satacmd_flags.sata_queued = B_TRUE;
4567 
4568 			/* Set-up maximum queue depth */
4569 			scmd->satacmd_flags.sata_max_queue_depth =
4570 			    sdinfo->satadrv_max_queue_depth - 1;
4571 		} else if (sdinfo->satadrv_features_enabled &
4572 		    SATA_DEV_F_E_UNTAGGED_QING) {
4573 			/*
4574 			 * Although NCQ/TCQ is not enabled, untagged queuing
4575 			 * may be still used.
4576 			 * Set-up the maximum untagged queue depth.
4577 			 * Use controller's queue depth from sata_hba_tran.
4578 			 * SATA HBA drivers may ignore this value and rely on
4579 			 * the internal limits.For drivers that do not
4580 			 * ignore untaged queue depth, limit the value to
4581 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
4582 			 * largest value that can be passed via
4583 			 * satacmd_flags.sata_max_queue_depth.
4584 			 */
4585 			scmd->satacmd_flags.sata_max_queue_depth =
4586 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
4587 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
4588 
4589 		} else {
4590 			scmd->satacmd_flags.sata_max_queue_depth = 0;
4591 		}
4592 	} else
4593 		scmd->satacmd_flags.sata_max_queue_depth = 0;
4594 
4595 	SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst,
4596 	    "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n",
4597 	    scmd->satacmd_cmd_reg, lba, sec_count);
4598 
4599 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4600 		/* Need callback function */
4601 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
4602 		synch = FALSE;
4603 	} else
4604 		synch = TRUE;
4605 
4606 	/* Transfer command to HBA */
4607 	if (sata_hba_start(spx, &rval) != 0) {
4608 		/* Pkt not accepted for execution */
4609 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4610 		return (rval);
4611 	}
4612 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4613 	/*
4614 	 * If execution is non-synchronous,
4615 	 * a callback function will handle potential errors, translate
4616 	 * the response and will do a callback to a target driver.
4617 	 * If it was synchronous, check execution status using the same
4618 	 * framework callback.
4619 	 */
4620 	if (synch) {
4621 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4622 		    "synchronous execution status %x\n",
4623 		    spx->txlt_sata_pkt->satapkt_reason);
4624 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
4625 	}
4626 	return (TRAN_ACCEPT);
4627 }
4628 
4629 
4630 /*
4631  * SATA translate command: Write (various types)
4632  * Translated into appropriate type of ATA WRITE command
4633  * for SATA hard disks.
4634  * Both the device capabilities and requested operation mode are
4635  * considered.
4636  *
4637  * Following scsi cdb fields are ignored:
4638  * rwprotect, dpo, fua, fua_nv, group_number.
4639  *
4640  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
4641  * enable variable sata_func_enable), the capability of the controller and
4642  * capability of a device are checked and if both support queueing, write
4643  * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT
4644  * command rather than plain WRITE_XXX command.
4645  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
4646  * both the controller and device suport such functionality, the write
4647  * request will be translated to WRITE_FPDMA_QUEUED command.
4648  * In both cases the maximum queue depth is derived as minimum of:
4649  * HBA capability,device capability and sata_max_queue_depth variable setting.
4650  * The value passed to HBA driver is decremented by 1, because only 5 bits are
4651  * used to pass max queue depth value, and the maximum possible queue depth
4652  * is 32.
4653  *
4654  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4655  * appropriate values in scsi_pkt fields.
4656  */
4657 static int
4658 sata_txlt_write(sata_pkt_txlate_t *spx)
4659 {
4660 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4661 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4662 	sata_drive_info_t *sdinfo;
4663 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4664 	int cport = SATA_TXLT_CPORT(spx);
4665 	uint16_t sec_count;
4666 	uint64_t lba;
4667 	int rval, reason;
4668 	int synch;
4669 
4670 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4671 
4672 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4673 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4674 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4675 		return (rval);
4676 	}
4677 
4678 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4679 	    &spx->txlt_sata_pkt->satapkt_device);
4680 
4681 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4682 	/*
4683 	 * Extract LBA and sector count from scsi CDB
4684 	 */
4685 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
4686 	case SCMD_WRITE:
4687 		/* 6-byte scsi read cmd : 0x0A */
4688 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
4689 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
4690 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4691 		sec_count = scsipkt->pkt_cdbp[4];
4692 		/* sec_count 0 will be interpreted as 256 by a device */
4693 		break;
4694 	case SCMD_WRITE_G1:
4695 		/* 10-bytes scsi write command : 0x2A */
4696 		lba = scsipkt->pkt_cdbp[2];
4697 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4698 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4699 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4700 		sec_count = scsipkt->pkt_cdbp[7];
4701 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4702 		break;
4703 	case SCMD_WRITE_G5:
4704 		/* 12-bytes scsi read command : 0xAA */
4705 		lba = scsipkt->pkt_cdbp[2];
4706 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4707 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4708 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4709 		sec_count = scsipkt->pkt_cdbp[6];
4710 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
4711 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4712 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
4713 		break;
4714 	case SCMD_WRITE_G4:
4715 		/* 16-bytes scsi write command : 0x8A */
4716 		lba = scsipkt->pkt_cdbp[2];
4717 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4718 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4719 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4720 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
4721 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
4722 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
4723 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
4724 		sec_count = scsipkt->pkt_cdbp[10];
4725 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
4726 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
4727 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
4728 		break;
4729 	default:
4730 		/* Unsupported command */
4731 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4732 		return (sata_txlt_invalid_command(spx));
4733 	}
4734 
4735 	/*
4736 	 * Check if specified address and length exceeds device capacity
4737 	 */
4738 	if ((lba >= sdinfo->satadrv_capacity) ||
4739 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
4740 		/* LBA out of range */
4741 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4742 		return (sata_txlt_lba_out_of_range(spx));
4743 	}
4744 
4745 	/*
4746 	 * For zero-length transfer, emulate good completion of the command
4747 	 * (reasons for rejecting the command were already checked).
4748 	 * No DMA resources were allocated.
4749 	 */
4750 	if (spx->txlt_dma_cookie_list == NULL) {
4751 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4752 		return (sata_emul_rw_completion(spx));
4753 	}
4754 
4755 	/*
4756 	 * Build cmd block depending on the device capability and
4757 	 * requested operation mode.
4758 	 * Do not bother with non-dma mode- we are working only with
4759 	 * devices supporting DMA.
4760 	 */
4761 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
4762 	scmd->satacmd_device_reg = SATA_ADH_LBA;
4763 	scmd->satacmd_cmd_reg = SATAC_WRITE_DMA;
4764 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
4765 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4766 		scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT;
4767 		scmd->satacmd_sec_count_msb = sec_count >> 8;
4768 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
4769 #ifndef __lock_lint
4770 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
4771 		scmd->satacmd_lba_high_msb = lba >> 40;
4772 #endif
4773 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
4774 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
4775 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
4776 	}
4777 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
4778 	scmd->satacmd_lba_low_lsb = lba & 0xff;
4779 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
4780 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
4781 	scmd->satacmd_features_reg = 0;
4782 	scmd->satacmd_status_reg = 0;
4783 	scmd->satacmd_error_reg = 0;
4784 
4785 	/*
4786 	 * Check if queueing commands should be used and switch
4787 	 * to appropriate command if possible
4788 	 */
4789 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
4790 		boolean_t using_queuing;
4791 
4792 		/* Queuing supported by controller and device? */
4793 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
4794 		    (sdinfo->satadrv_features_support &
4795 		    SATA_DEV_F_NCQ) &&
4796 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4797 		    SATA_CTLF_NCQ)) {
4798 			using_queuing = B_TRUE;
4799 
4800 			/* NCQ supported - use FPDMA WRITE */
4801 			scmd->satacmd_cmd_reg =
4802 			    SATAC_WRITE_FPDMA_QUEUED;
4803 			scmd->satacmd_features_reg_ext =
4804 			    scmd->satacmd_sec_count_msb;
4805 			scmd->satacmd_sec_count_msb = 0;
4806 		} else if ((sdinfo->satadrv_features_support &
4807 		    SATA_DEV_F_TCQ) &&
4808 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4809 		    SATA_CTLF_QCMD)) {
4810 			using_queuing = B_TRUE;
4811 
4812 			/* Legacy queueing */
4813 			if (sdinfo->satadrv_features_support &
4814 			    SATA_DEV_F_LBA48) {
4815 				scmd->satacmd_cmd_reg =
4816 				    SATAC_WRITE_DMA_QUEUED_EXT;
4817 				scmd->satacmd_features_reg_ext =
4818 				    scmd->satacmd_sec_count_msb;
4819 				scmd->satacmd_sec_count_msb = 0;
4820 			} else {
4821 				scmd->satacmd_cmd_reg =
4822 				    SATAC_WRITE_DMA_QUEUED;
4823 			}
4824 		} else	/*  NCQ nor legacy queuing not supported */
4825 			using_queuing = B_FALSE;
4826 
4827 		if (using_queuing) {
4828 			scmd->satacmd_features_reg =
4829 			    scmd->satacmd_sec_count_lsb;
4830 			scmd->satacmd_sec_count_lsb = 0;
4831 			scmd->satacmd_flags.sata_queued = B_TRUE;
4832 			/* Set-up maximum queue depth */
4833 			scmd->satacmd_flags.sata_max_queue_depth =
4834 			    sdinfo->satadrv_max_queue_depth - 1;
4835 		} else if (sdinfo->satadrv_features_enabled &
4836 		    SATA_DEV_F_E_UNTAGGED_QING) {
4837 			/*
4838 			 * Although NCQ/TCQ is not enabled, untagged queuing
4839 			 * may be still used.
4840 			 * Set-up the maximum untagged queue depth.
4841 			 * Use controller's queue depth from sata_hba_tran.
4842 			 * SATA HBA drivers may ignore this value and rely on
4843 			 * the internal limits. For drivera that do not
4844 			 * ignore untaged queue depth, limit the value to
4845 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
4846 			 * largest value that can be passed via
4847 			 * satacmd_flags.sata_max_queue_depth.
4848 			 */
4849 			scmd->satacmd_flags.sata_max_queue_depth =
4850 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
4851 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
4852 
4853 		} else {
4854 			scmd->satacmd_flags.sata_max_queue_depth = 0;
4855 		}
4856 	} else
4857 		scmd->satacmd_flags.sata_max_queue_depth = 0;
4858 
4859 	SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4860 	    "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n",
4861 	    scmd->satacmd_cmd_reg, lba, sec_count);
4862 
4863 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4864 		/* Need callback function */
4865 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
4866 		synch = FALSE;
4867 	} else
4868 		synch = TRUE;
4869 
4870 	/* Transfer command to HBA */
4871 	if (sata_hba_start(spx, &rval) != 0) {
4872 		/* Pkt not accepted for execution */
4873 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4874 		return (rval);
4875 	}
4876 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4877 
4878 	/*
4879 	 * If execution is non-synchronous,
4880 	 * a callback function will handle potential errors, translate
4881 	 * the response and will do a callback to a target driver.
4882 	 * If it was synchronous, check execution status using the same
4883 	 * framework callback.
4884 	 */
4885 	if (synch) {
4886 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4887 		    "synchronous execution status %x\n",
4888 		    spx->txlt_sata_pkt->satapkt_reason);
4889 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
4890 	}
4891 	return (TRAN_ACCEPT);
4892 }
4893 
4894 
4895 /*
4896  * Implements SCSI SBC WRITE BUFFER command download microcode option
4897  */
4898 static int
4899 sata_txlt_write_buffer(sata_pkt_txlate_t *spx)
4900 {
4901 #define	WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE			4
4902 #define	WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE			5
4903 
4904 	sata_hba_inst_t *sata_hba_inst = SATA_TXLT_HBA_INST(spx);
4905 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4906 	struct sata_pkt *sata_pkt = spx->txlt_sata_pkt;
4907 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4908 
4909 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4910 	struct scsi_extended_sense *sense;
4911 	int rval, mode, sector_count, reason;
4912 	int cport = SATA_TXLT_CPORT(spx);
4913 
4914 	mode = scsipkt->pkt_cdbp[1] & 0x1f;
4915 
4916 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4917 	    "sata_txlt_write_buffer, mode 0x%x\n", mode);
4918 
4919 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4920 
4921 	if ((rval = sata_txlt_generic_pkt_info(spx, &reason)) != TRAN_ACCEPT) {
4922 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4923 		return (rval);
4924 	}
4925 
4926 	/* Use synchronous mode */
4927 	spx->txlt_sata_pkt->satapkt_op_mode
4928 	    |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
4929 
4930 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4931 
4932 	scsipkt->pkt_reason = CMD_CMPLT;
4933 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4934 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4935 
4936 	/*
4937 	 * The SCSI to ATA translation specification only calls
4938 	 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE.
4939 	 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but
4940 	 * ATA 8 (draft) got rid of download microcode for temp
4941 	 * and it is even optional for ATA 7, so it may be aborted.
4942 	 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as
4943 	 * it is not specified and the buffer offset for SCSI is a 16-bit
4944 	 * value in bytes, but for ATA it is a 16-bit offset in 512 byte
4945 	 * sectors.  Thus the offset really doesn't buy us anything.
4946 	 * If and when ATA 8 is stabilized and the SCSI to ATA specification
4947 	 * is revised, this can be revisisted.
4948 	 */
4949 	/* Reject not supported request */
4950 	switch (mode) {
4951 	case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE:
4952 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP;
4953 		break;
4954 	case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE:
4955 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE;
4956 		break;
4957 	default:
4958 		goto bad_param;
4959 	}
4960 
4961 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
4962 
4963 	scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE;
4964 	if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0)
4965 		goto bad_param;
4966 	sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE;
4967 	scmd->satacmd_sec_count_lsb = (uint8_t)sector_count;
4968 	scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8;
4969 	scmd->satacmd_lba_mid_lsb = 0;
4970 	scmd->satacmd_lba_high_lsb = 0;
4971 	scmd->satacmd_device_reg = 0;
4972 	spx->txlt_sata_pkt->satapkt_comp = NULL;
4973 	scmd->satacmd_addr_type = 0;
4974 
4975 	/* Transfer command to HBA */
4976 	if (sata_hba_start(spx, &rval) != 0) {
4977 		/* Pkt not accepted for execution */
4978 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
4979 		return (rval);
4980 	}
4981 
4982 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
4983 
4984 	/* Then we need synchronous check the status of the disk */
4985 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4986 	    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
4987 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
4988 		scsipkt->pkt_reason = CMD_CMPLT;
4989 
4990 		/* Download commmand succeed, so probe and identify device */
4991 		sata_reidentify_device(spx);
4992 	} else {
4993 		/* Something went wrong, microcode download command failed */
4994 		scsipkt->pkt_reason = CMD_INCOMPLETE;
4995 		*scsipkt->pkt_scbp = STATUS_CHECK;
4996 		sense = sata_arq_sense(spx);
4997 		switch (sata_pkt->satapkt_reason) {
4998 		case SATA_PKT_PORT_ERROR:
4999 			/*
5000 			 * We have no device data. Assume no data transfered.
5001 			 */
5002 			sense->es_key = KEY_HARDWARE_ERROR;
5003 			break;
5004 
5005 		case SATA_PKT_DEV_ERROR:
5006 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5007 			    SATA_STATUS_ERR) {
5008 				/*
5009 				 * determine dev error reason from error
5010 				 * reg content
5011 				 */
5012 				sata_decode_device_error(spx, sense);
5013 				break;
5014 			}
5015 			/* No extended sense key - no info available */
5016 			break;
5017 
5018 		case SATA_PKT_TIMEOUT:
5019 			scsipkt->pkt_reason = CMD_TIMEOUT;
5020 			scsipkt->pkt_statistics |=
5021 			    STAT_TIMEOUT | STAT_DEV_RESET;
5022 			/* No extended sense key ? */
5023 			break;
5024 
5025 		case SATA_PKT_ABORTED:
5026 			scsipkt->pkt_reason = CMD_ABORTED;
5027 			scsipkt->pkt_statistics |= STAT_ABORTED;
5028 			/* No extended sense key ? */
5029 			break;
5030 
5031 		case SATA_PKT_RESET:
5032 			/* pkt aborted by an explicit reset from a host */
5033 			scsipkt->pkt_reason = CMD_RESET;
5034 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5035 			break;
5036 
5037 		default:
5038 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5039 			    "sata_txlt_nodata_cmd_completion: "
5040 			    "invalid packet completion reason %d",
5041 			    sata_pkt->satapkt_reason));
5042 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5043 			break;
5044 		}
5045 
5046 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5047 		    "scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5048 
5049 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5050 		    scsipkt->pkt_comp != NULL)
5051 			/* scsi callback required */
5052 			(*scsipkt->pkt_comp)(scsipkt);
5053 	}
5054 	return (TRAN_ACCEPT);
5055 
5056 bad_param:
5057 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5058 	*scsipkt->pkt_scbp = STATUS_CHECK;
5059 	sense = sata_arq_sense(spx);
5060 	sense->es_key = KEY_ILLEGAL_REQUEST;
5061 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5062 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5063 	    scsipkt->pkt_comp != NULL) {
5064 		/* scsi callback required */
5065 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5066 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5067 		    TQ_SLEEP) == 0) {
5068 			/* Scheduling the callback failed */
5069 			rval = TRAN_BUSY;
5070 		}
5071 	}
5072 	return (rval);
5073 }
5074 
5075 /*
5076  * Re-identify device after doing a firmware download.
5077  */
5078 static void
5079 sata_reidentify_device(sata_pkt_txlate_t *spx)
5080 {
5081 #define	DOWNLOAD_WAIT_TIME_SECS	60
5082 #define	DOWNLOAD_WAIT_INTERVAL_SECS	1
5083 	int rval;
5084 	int retry_cnt;
5085 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5086 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
5087 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
5088 	sata_drive_info_t *sdinfo;
5089 
5090 	/*
5091 	 * Before returning good status, probe device.
5092 	 * Device probing will get IDENTIFY DEVICE data, if possible.
5093 	 * The assumption is that the new microcode is applied by the
5094 	 * device. It is a caller responsibility to verify this.
5095 	 */
5096 	for (retry_cnt = 0;
5097 	    retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS;
5098 	    retry_cnt++) {
5099 		rval = sata_probe_device(sata_hba_inst, &sata_device);
5100 
5101 		if (rval == SATA_SUCCESS) { /* Set default features */
5102 			sdinfo = sata_get_device_info(sata_hba_inst,
5103 			    &sata_device);
5104 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
5105 			    SATA_SUCCESS) {
5106 				/* retry */
5107 				(void) sata_initialize_device(sata_hba_inst,
5108 				    sdinfo);
5109 			}
5110 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5111 			    scsipkt->pkt_comp != NULL)
5112 				(*scsipkt->pkt_comp)(scsipkt);
5113 			return;
5114 		} else if (rval == SATA_RETRY) {
5115 			delay(drv_usectohz(1000000 *
5116 			    DOWNLOAD_WAIT_INTERVAL_SECS));
5117 			continue;
5118 		} else	/* failed - no reason to retry */
5119 			break;
5120 	}
5121 
5122 	/*
5123 	 * Something went wrong, device probing failed.
5124 	 */
5125 	SATA_LOG_D((sata_hba_inst, CE_WARN,
5126 	    "Cannot probe device after downloading microcode\n"));
5127 
5128 	/* Reset device to force retrying the probe. */
5129 	(void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst))
5130 	    (SATA_DIP(sata_hba_inst), &sata_device);
5131 
5132 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5133 	    scsipkt->pkt_comp != NULL)
5134 		(*scsipkt->pkt_comp)(scsipkt);
5135 }
5136 
5137 
5138 /*
5139  * Translate command: Synchronize Cache.
5140  * Translates into Flush Cache command for SATA hard disks.
5141  *
5142  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
5143  * appropriate values in scsi_pkt fields.
5144  */
5145 static 	int
5146 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx)
5147 {
5148 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5149 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
5150 	int cport = SATA_TXLT_CPORT(spx);
5151 	int rval, reason;
5152 	int synch;
5153 
5154 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5155 
5156 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
5157 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5158 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5159 		return (rval);
5160 	}
5161 
5162 	scmd->satacmd_addr_type = 0;
5163 	scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE;
5164 	scmd->satacmd_device_reg = 0;
5165 	scmd->satacmd_sec_count_lsb = 0;
5166 	scmd->satacmd_lba_low_lsb = 0;
5167 	scmd->satacmd_lba_mid_lsb = 0;
5168 	scmd->satacmd_lba_high_lsb = 0;
5169 	scmd->satacmd_features_reg = 0;
5170 	scmd->satacmd_status_reg = 0;
5171 	scmd->satacmd_error_reg = 0;
5172 
5173 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5174 	    "sata_txlt_synchronize_cache\n", NULL);
5175 
5176 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5177 		/* Need to set-up a callback function */
5178 		spx->txlt_sata_pkt->satapkt_comp =
5179 		    sata_txlt_nodata_cmd_completion;
5180 		synch = FALSE;
5181 	} else
5182 		synch = TRUE;
5183 
5184 	/* Transfer command to HBA */
5185 	if (sata_hba_start(spx, &rval) != 0) {
5186 		/* Pkt not accepted for execution */
5187 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5188 		return (rval);
5189 	}
5190 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5191 
5192 	/*
5193 	 * If execution non-synchronous, it had to be completed
5194 	 * a callback function will handle potential errors, translate
5195 	 * the response and will do a callback to a target driver.
5196 	 * If it was synchronous, check status, using the same
5197 	 * framework callback.
5198 	 */
5199 	if (synch) {
5200 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5201 		    "synchronous execution status %x\n",
5202 		    spx->txlt_sata_pkt->satapkt_reason);
5203 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
5204 	}
5205 	return (TRAN_ACCEPT);
5206 }
5207 
5208 
5209 /*
5210  * Send pkt to SATA HBA driver
5211  *
5212  * This function may be called only if the operation is requested by scsi_pkt,
5213  * i.e. scsi_pkt is not NULL.
5214  *
5215  * This function has to be called with cport mutex held. It does release
5216  * the mutex when it calls HBA driver sata_tran_start function and
5217  * re-acquires it afterwards.
5218  *
5219  * If return value is 0, pkt was accepted, -1 otherwise
5220  * rval is set to appropriate sata_scsi_start return value.
5221  *
5222  * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not
5223  * have called the sata_pkt callback function for this packet.
5224  *
5225  * The scsi callback has to be performed by the caller of this routine.
5226  *
5227  * Note 2: No port multiplier support for now.
5228  */
5229 static int
5230 sata_hba_start(sata_pkt_txlate_t *spx, int *rval)
5231 {
5232 	int stat, cport;
5233 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
5234 	sata_drive_info_t *sdinfo;
5235 	sata_device_t *sata_device;
5236 	uint8_t cmd;
5237 	struct sata_cmd_flags cmd_flags;
5238 
5239 	ASSERT(spx->txlt_sata_pkt != NULL);
5240 
5241 	cport = SATA_TXLT_CPORT(spx);
5242 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5243 
5244 	sdinfo = sata_get_device_info(sata_hba_inst,
5245 	    &spx->txlt_sata_pkt->satapkt_device);
5246 	ASSERT(sdinfo != NULL);
5247 
5248 	/* Clear device reset state? */
5249 	if (sdinfo->satadrv_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) {
5250 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
5251 		    sata_clear_dev_reset = B_TRUE;
5252 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_CLEAR_DEVICE_RESET;
5253 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5254 		    "sata_hba_start: clearing device reset state\n", NULL);
5255 	}
5256 	cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg;
5257 	cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags;
5258 	sata_device = &spx->txlt_sata_pkt->satapkt_device;
5259 
5260 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5261 
5262 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5263 	    "Sata cmd 0x%2x\n", cmd);
5264 
5265 	stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
5266 	    spx->txlt_sata_pkt);
5267 
5268 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5269 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
5270 	/*
5271 	 * If sata pkt was accepted and executed in asynchronous mode, i.e.
5272 	 * with the sata callback, the sata_pkt could be already destroyed
5273 	 * by the time we check ther return status from the hba_start()
5274 	 * function, because sata_scsi_destroy_pkt() could have been already
5275 	 * called (perhaps in the interrupt context). So, in such case, there
5276 	 * should be no references to it. In other cases, sata_pkt still
5277 	 * exists.
5278 	 */
5279 	switch (stat) {
5280 	case SATA_TRAN_ACCEPTED:
5281 		/*
5282 		 * pkt accepted for execution.
5283 		 * If it was executed synchronously, it is already completed
5284 		 * and pkt completion_reason indicates completion status.
5285 		 */
5286 		*rval = TRAN_ACCEPT;
5287 		return (0);
5288 
5289 	case SATA_TRAN_QUEUE_FULL:
5290 		/*
5291 		 * Controller detected queue full condition.
5292 		 */
5293 		SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst,
5294 		    "sata_hba_start: queue full\n", NULL);
5295 
5296 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
5297 		*spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL;
5298 
5299 		*rval = TRAN_BUSY;
5300 		break;
5301 
5302 	case SATA_TRAN_PORT_ERROR:
5303 		/*
5304 		 * Communication/link with device or general port error
5305 		 * detected before pkt execution begun.
5306 		 */
5307 		if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
5308 		    SATA_ADDR_CPORT ||
5309 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
5310 		    SATA_ADDR_DCPORT)
5311 			sata_log(sata_hba_inst, CE_CONT,
5312 			    "SATA port %d error",
5313 			    sata_device->satadev_addr.cport);
5314 		else
5315 			sata_log(sata_hba_inst, CE_CONT,
5316 			    "SATA port %d pmport %d error\n",
5317 			    sata_device->satadev_addr.cport,
5318 			    sata_device->satadev_addr.pmport);
5319 
5320 		/*
5321 		 * Update the port/device structure.
5322 		 * sata_pkt should be still valid. Since port error is
5323 		 * returned, sata_device content should reflect port
5324 		 * state - it means, that sata address have been changed,
5325 		 * because original packet's sata address refered to a device
5326 		 * attached to some port.
5327 		 */
5328 		sata_update_port_info(sata_hba_inst, sata_device);
5329 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
5330 		*rval = TRAN_FATAL_ERROR;
5331 		break;
5332 
5333 	case SATA_TRAN_CMD_UNSUPPORTED:
5334 		/*
5335 		 * Command rejected by HBA as unsupported. It was HBA driver
5336 		 * that rejected the command, command was not sent to
5337 		 * an attached device.
5338 		 */
5339 		if ((sdinfo != NULL) &&
5340 		    (sdinfo->satadrv_state & SATA_DSTATE_RESET))
5341 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5342 			    "sat_hba_start: cmd 0x%2x rejected "
5343 			    "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd);
5344 
5345 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5346 		(void) sata_txlt_invalid_command(spx);
5347 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5348 
5349 		*rval = TRAN_ACCEPT;
5350 		break;
5351 
5352 	case SATA_TRAN_BUSY:
5353 		/*
5354 		 * Command rejected by HBA because other operation prevents
5355 		 * accepting the packet, or device is in RESET condition.
5356 		 */
5357 		if (sdinfo != NULL) {
5358 			sdinfo->satadrv_state =
5359 			    spx->txlt_sata_pkt->satapkt_device.satadev_state;
5360 
5361 			if (sdinfo->satadrv_state & SATA_DSTATE_RESET) {
5362 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5363 				    "sata_hba_start: cmd 0x%2x rejected "
5364 				    "because of device reset condition\n",
5365 				    cmd);
5366 			} else {
5367 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5368 				    "sata_hba_start: cmd 0x%2x rejected "
5369 				    "with SATA_TRAN_BUSY status\n",
5370 				    cmd);
5371 			}
5372 		}
5373 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
5374 		*rval = TRAN_BUSY;
5375 		break;
5376 
5377 	default:
5378 		/* Unrecognized HBA response */
5379 		SATA_LOG_D((sata_hba_inst, CE_WARN,
5380 		    "sata_hba_start: unrecognized HBA response "
5381 		    "to cmd : 0x%2x resp 0x%x", cmd, rval));
5382 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
5383 		*rval = TRAN_FATAL_ERROR;
5384 		break;
5385 	}
5386 
5387 	/*
5388 	 * If we got here, the packet was rejected.
5389 	 * Check if we need to remember reset state clearing request
5390 	 */
5391 	if (cmd_flags.sata_clear_dev_reset) {
5392 		/*
5393 		 * Check if device is still configured - it may have
5394 		 * disapeared from the configuration
5395 		 */
5396 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
5397 		if (sdinfo != NULL) {
5398 			/*
5399 			 * Restore the flag that requests clearing of
5400 			 * the device reset state,
5401 			 * so the next sata packet may carry it to HBA.
5402 			 */
5403 			sdinfo->satadrv_event_flags |=
5404 			    SATA_EVNT_CLEAR_DEVICE_RESET;
5405 		}
5406 	}
5407 	return (-1);
5408 }
5409 
5410 /*
5411  * Scsi response setup for invalid LBA
5412  *
5413  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
5414  */
5415 static int
5416 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx)
5417 {
5418 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5419 	struct scsi_extended_sense *sense;
5420 
5421 	scsipkt->pkt_reason = CMD_CMPLT;
5422 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5423 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5424 	*scsipkt->pkt_scbp = STATUS_CHECK;
5425 
5426 	*scsipkt->pkt_scbp = STATUS_CHECK;
5427 	sense = sata_arq_sense(spx);
5428 	sense->es_key = KEY_ILLEGAL_REQUEST;
5429 	sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
5430 
5431 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5432 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5433 
5434 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5435 	    scsipkt->pkt_comp != NULL)
5436 		/* scsi callback required */
5437 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5438 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5439 		    TQ_SLEEP) == NULL)
5440 			/* Scheduling the callback failed */
5441 			return (TRAN_BUSY);
5442 	return (TRAN_ACCEPT);
5443 }
5444 
5445 
5446 /*
5447  * Analyze device status and error registers and translate them into
5448  * appropriate scsi sense codes.
5449  * NOTE: non-packet commands only for now
5450  */
5451 static void
5452 sata_decode_device_error(sata_pkt_txlate_t *spx,
5453     struct scsi_extended_sense *sense)
5454 {
5455 	uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg;
5456 
5457 	ASSERT(sense != NULL);
5458 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
5459 	    SATA_STATUS_ERR);
5460 
5461 
5462 	if (err_reg & SATA_ERROR_ICRC) {
5463 		sense->es_key = KEY_ABORTED_COMMAND;
5464 		sense->es_add_code = 0x08; /* Communication failure */
5465 		return;
5466 	}
5467 
5468 	if (err_reg & SATA_ERROR_UNC) {
5469 		sense->es_key = KEY_MEDIUM_ERROR;
5470 		/* Information bytes (LBA) need to be set by a caller */
5471 		return;
5472 	}
5473 
5474 	/* ADD HERE: MC error bit handling for ATAPI CD/DVD */
5475 	if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) {
5476 		sense->es_key = KEY_UNIT_ATTENTION;
5477 		sense->es_add_code = 0x3a; /* No media present */
5478 		return;
5479 	}
5480 
5481 	if (err_reg & SATA_ERROR_IDNF) {
5482 		if (err_reg & SATA_ERROR_ABORT) {
5483 			sense->es_key = KEY_ABORTED_COMMAND;
5484 		} else {
5485 			sense->es_key = KEY_ILLEGAL_REQUEST;
5486 			sense->es_add_code = 0x21; /* LBA out of range */
5487 		}
5488 		return;
5489 	}
5490 
5491 	if (err_reg & SATA_ERROR_ABORT) {
5492 		ASSERT(spx->txlt_sata_pkt != NULL);
5493 		sense->es_key = KEY_ABORTED_COMMAND;
5494 		return;
5495 	}
5496 }
5497 
5498 /*
5499  * Extract error LBA from sata_pkt.satapkt_cmd register fields
5500  */
5501 static void
5502 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba)
5503 {
5504 	sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd;
5505 
5506 	*lba = 0;
5507 	if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) {
5508 		*lba = sata_cmd->satacmd_lba_high_msb;
5509 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb;
5510 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb;
5511 	} else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) {
5512 		*lba = sata_cmd->satacmd_device_reg & 0xf;
5513 	}
5514 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb;
5515 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb;
5516 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb;
5517 }
5518 
5519 /*
5520  * This is fixed sense format - if LBA exceeds the info field size,
5521  * no valid info will be returned (valid bit in extended sense will
5522  * be set to 0).
5523  */
5524 static struct scsi_extended_sense *
5525 sata_arq_sense(sata_pkt_txlate_t *spx)
5526 {
5527 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5528 	struct scsi_arq_status *arqs;
5529 	struct scsi_extended_sense *sense;
5530 
5531 	/* Fill ARQ sense data */
5532 	scsipkt->pkt_state |= STATE_ARQ_DONE;
5533 	arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp;
5534 	*(uchar_t *)&arqs->sts_status = STATUS_CHECK;
5535 	*(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD;
5536 	arqs->sts_rqpkt_reason = CMD_CMPLT;
5537 	arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5538 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
5539 	arqs->sts_rqpkt_resid = 0;
5540 	sense = &arqs->sts_sensedata;
5541 	bzero(sense, sizeof (struct scsi_extended_sense));
5542 	sata_fixed_sense_data_preset(sense);
5543 	return (sense);
5544 }
5545 
5546 
5547 /*
5548  * Emulated SATA Read/Write command completion for zero-length requests.
5549  * This request always succedes, so in synchronous mode it always returns
5550  * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the
5551  * callback cannot be scheduled.
5552  */
5553 static int
5554 sata_emul_rw_completion(sata_pkt_txlate_t *spx)
5555 {
5556 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5557 
5558 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5559 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5560 	scsipkt->pkt_reason = CMD_CMPLT;
5561 	*scsipkt->pkt_scbp = STATUS_GOOD;
5562 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5563 		/* scsi callback required - have to schedule it */
5564 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5565 		    (task_func_t *)scsipkt->pkt_comp,
5566 		    (void *)scsipkt, TQ_SLEEP) == NULL)
5567 			/* Scheduling the callback failed */
5568 			return (TRAN_BUSY);
5569 	}
5570 	return (TRAN_ACCEPT);
5571 }
5572 
5573 
5574 /*
5575  * Translate completion status of SATA read/write commands into scsi response.
5576  * pkt completion_reason is checked to determine the completion status.
5577  * Do scsi callback if necessary.
5578  *
5579  * Note: this function may be called also for synchronously executed
5580  * commands.
5581  * This function may be used only if scsi_pkt is non-NULL.
5582  */
5583 static void
5584 sata_txlt_rw_completion(sata_pkt_t *sata_pkt)
5585 {
5586 	sata_pkt_txlate_t *spx =
5587 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
5588 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
5589 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5590 	struct scsi_extended_sense *sense;
5591 	uint64_t lba;
5592 	struct buf *bp;
5593 	int rval;
5594 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5595 		/* Normal completion */
5596 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5597 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
5598 		scsipkt->pkt_reason = CMD_CMPLT;
5599 		*scsipkt->pkt_scbp = STATUS_GOOD;
5600 		if (spx->txlt_tmp_buf != NULL) {
5601 			/* Temporary buffer was used */
5602 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5603 			if (bp->b_flags & B_READ) {
5604 				rval = ddi_dma_sync(
5605 				    spx->txlt_buf_dma_handle, 0, 0,
5606 				    DDI_DMA_SYNC_FORCPU);
5607 				ASSERT(rval == DDI_SUCCESS);
5608 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
5609 				    bp->b_bcount);
5610 			}
5611 		}
5612 	} else {
5613 		/*
5614 		 * Something went wrong - analyze return
5615 		 */
5616 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5617 		    STATE_SENT_CMD | STATE_GOT_STATUS;
5618 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5619 		*scsipkt->pkt_scbp = STATUS_CHECK;
5620 		sense = sata_arq_sense(spx);
5621 		ASSERT(sense != NULL);
5622 
5623 		/*
5624 		 * SATA_PKT_DEV_ERROR is the only case where we may be able to
5625 		 * extract from device registers the failing LBA.
5626 		 */
5627 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
5628 			if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) &&
5629 			    (scmd->satacmd_lba_mid_msb != 0 ||
5630 			    scmd->satacmd_lba_high_msb != 0)) {
5631 				/*
5632 				 * We have problem reporting this cmd LBA
5633 				 * in fixed sense data format, because of
5634 				 * the size of the scsi LBA fields.
5635 				 */
5636 				sense->es_valid = 0;
5637 			} else {
5638 				sata_extract_error_lba(spx, &lba);
5639 				sense->es_info_1 = (lba & 0xFF000000) >> 24;
5640 				sense->es_info_2 = (lba & 0xFF0000) >> 16;
5641 				sense->es_info_3 = (lba & 0xFF00) >> 8;
5642 				sense->es_info_4 = lba & 0xFF;
5643 			}
5644 		} else {
5645 			/* Invalid extended sense info */
5646 			sense->es_valid = 0;
5647 		}
5648 
5649 		switch (sata_pkt->satapkt_reason) {
5650 		case SATA_PKT_PORT_ERROR:
5651 			/* We may want to handle DEV GONE state as well */
5652 			/*
5653 			 * We have no device data. Assume no data transfered.
5654 			 */
5655 			sense->es_key = KEY_HARDWARE_ERROR;
5656 			break;
5657 
5658 		case SATA_PKT_DEV_ERROR:
5659 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5660 			    SATA_STATUS_ERR) {
5661 				/*
5662 				 * determine dev error reason from error
5663 				 * reg content
5664 				 */
5665 				sata_decode_device_error(spx, sense);
5666 				if (sense->es_key == KEY_MEDIUM_ERROR) {
5667 					switch (scmd->satacmd_cmd_reg) {
5668 					case SATAC_READ_DMA:
5669 					case SATAC_READ_DMA_EXT:
5670 					case SATAC_READ_DMA_QUEUED:
5671 					case SATAC_READ_DMA_QUEUED_EXT:
5672 					case SATAC_READ_FPDMA_QUEUED:
5673 						/* Unrecovered read error */
5674 						sense->es_add_code =
5675 						    SD_SCSI_ASC_UNREC_READ_ERR;
5676 						break;
5677 					case SATAC_WRITE_DMA:
5678 					case SATAC_WRITE_DMA_EXT:
5679 					case SATAC_WRITE_DMA_QUEUED:
5680 					case SATAC_WRITE_DMA_QUEUED_EXT:
5681 					case SATAC_WRITE_FPDMA_QUEUED:
5682 						/* Write error */
5683 						sense->es_add_code =
5684 						    SD_SCSI_ASC_WRITE_ERR;
5685 						break;
5686 					default:
5687 						/* Internal error */
5688 						SATA_LOG_D((
5689 						    spx->txlt_sata_hba_inst,
5690 						    CE_WARN,
5691 						    "sata_txlt_rw_completion :"
5692 						    "internal error - invalid "
5693 						    "command 0x%2x",
5694 						    scmd->satacmd_cmd_reg));
5695 						break;
5696 					}
5697 				}
5698 				break;
5699 			}
5700 			/* No extended sense key - no info available */
5701 			scsipkt->pkt_reason = CMD_INCOMPLETE;
5702 			break;
5703 
5704 		case SATA_PKT_TIMEOUT:
5705 			scsipkt->pkt_reason = CMD_TIMEOUT;
5706 			scsipkt->pkt_statistics |=
5707 			    STAT_TIMEOUT | STAT_DEV_RESET;
5708 			sense->es_key = KEY_ABORTED_COMMAND;
5709 			break;
5710 
5711 		case SATA_PKT_ABORTED:
5712 			scsipkt->pkt_reason = CMD_ABORTED;
5713 			scsipkt->pkt_statistics |= STAT_ABORTED;
5714 			sense->es_key = KEY_ABORTED_COMMAND;
5715 			break;
5716 
5717 		case SATA_PKT_RESET:
5718 			scsipkt->pkt_reason = CMD_RESET;
5719 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5720 			sense->es_key = KEY_ABORTED_COMMAND;
5721 			break;
5722 
5723 		default:
5724 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5725 			    "sata_txlt_rw_completion: "
5726 			    "invalid packet completion reason"));
5727 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5728 			break;
5729 		}
5730 	}
5731 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5732 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5733 
5734 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5735 	    scsipkt->pkt_comp != NULL)
5736 		/* scsi callback required */
5737 		(*scsipkt->pkt_comp)(scsipkt);
5738 }
5739 
5740 
5741 /*
5742  * Translate completion status of non-data commands (i.e. commands returning
5743  * no data).
5744  * pkt completion_reason is checked to determine the completion status.
5745  * Do scsi callback if necessary (FLAG_NOINTR == 0)
5746  *
5747  * Note: this function may be called also for synchronously executed
5748  * commands.
5749  * This function may be used only if scsi_pkt is non-NULL.
5750  */
5751 
5752 static 	void
5753 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt)
5754 {
5755 	sata_pkt_txlate_t *spx =
5756 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
5757 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5758 	struct scsi_extended_sense *sense;
5759 
5760 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5761 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5762 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5763 		/* Normal completion */
5764 		scsipkt->pkt_reason = CMD_CMPLT;
5765 		*scsipkt->pkt_scbp = STATUS_GOOD;
5766 	} else {
5767 		/* Something went wrong */
5768 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5769 		*scsipkt->pkt_scbp = STATUS_CHECK;
5770 		sense = sata_arq_sense(spx);
5771 		switch (sata_pkt->satapkt_reason) {
5772 		case SATA_PKT_PORT_ERROR:
5773 			/*
5774 			 * We have no device data. Assume no data transfered.
5775 			 */
5776 			sense->es_key = KEY_HARDWARE_ERROR;
5777 			break;
5778 
5779 		case SATA_PKT_DEV_ERROR:
5780 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5781 			    SATA_STATUS_ERR) {
5782 				/*
5783 				 * determine dev error reason from error
5784 				 * reg content
5785 				 */
5786 				sata_decode_device_error(spx, sense);
5787 				break;
5788 			}
5789 			/* No extended sense key - no info available */
5790 			break;
5791 
5792 		case SATA_PKT_TIMEOUT:
5793 			scsipkt->pkt_reason = CMD_TIMEOUT;
5794 			scsipkt->pkt_statistics |=
5795 			    STAT_TIMEOUT | STAT_DEV_RESET;
5796 			/* No extended sense key ? */
5797 			break;
5798 
5799 		case SATA_PKT_ABORTED:
5800 			scsipkt->pkt_reason = CMD_ABORTED;
5801 			scsipkt->pkt_statistics |= STAT_ABORTED;
5802 			/* No extended sense key ? */
5803 			break;
5804 
5805 		case SATA_PKT_RESET:
5806 			/* pkt aborted by an explicit reset from a host */
5807 			scsipkt->pkt_reason = CMD_RESET;
5808 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5809 			break;
5810 
5811 		default:
5812 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5813 			    "sata_txlt_nodata_cmd_completion: "
5814 			    "invalid packet completion reason %d",
5815 			    sata_pkt->satapkt_reason));
5816 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5817 			break;
5818 		}
5819 
5820 	}
5821 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5822 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5823 
5824 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5825 	    scsipkt->pkt_comp != NULL)
5826 		/* scsi callback required */
5827 		(*scsipkt->pkt_comp)(scsipkt);
5828 }
5829 
5830 
5831 /*
5832  * Build Mode sense R/W recovery page
5833  * NOT IMPLEMENTED
5834  */
5835 
5836 static int
5837 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5838 {
5839 #ifndef __lock_lint
5840 	_NOTE(ARGUNUSED(sdinfo))
5841 	_NOTE(ARGUNUSED(pcntrl))
5842 	_NOTE(ARGUNUSED(buf))
5843 #endif
5844 	return (0);
5845 }
5846 
5847 /*
5848  * Build Mode sense caching page  -  scsi-3 implementation.
5849  * Page length distinguishes previous format from scsi-3 format.
5850  * buf must have space for 0x12 bytes.
5851  * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable.
5852  *
5853  */
5854 static int
5855 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5856 {
5857 	struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)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_DAD_MODE_CACHE_SCSI3);
5864 
5865 	/* Saved paramters not supported */
5866 	if (pcntrl == 3)
5867 		return (0);
5868 	if (pcntrl == 0 || pcntrl == 2) {
5869 		/*
5870 		 * For now treat current and default parameters as same
5871 		 * That may have to change, if target driver will complain
5872 		 */
5873 		page->mode_page.code = MODEPAGE_CACHING;	/* PS = 0 */
5874 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
5875 
5876 		if ((sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) &&
5877 		    !(sata_id->ai_features85 & SATA_LOOK_AHEAD)) {
5878 			page->dra = 1;		/* Read Ahead disabled */
5879 			page->rcd = 1;		/* Read Cache disabled */
5880 		}
5881 		if ((sata_id->ai_cmdset82 & SATA_WRITE_CACHE) &&
5882 		    (sata_id->ai_features85 & SATA_WRITE_CACHE))
5883 			page->wce = 1;		/* Write Cache enabled */
5884 	} else {
5885 		/* Changeable parameters */
5886 		page->mode_page.code = MODEPAGE_CACHING;
5887 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
5888 		if (sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) {
5889 			page->dra = 1;
5890 			page->rcd = 1;
5891 		}
5892 		if (sata_id->ai_cmdset82 & SATA_WRITE_CACHE)
5893 			page->wce = 1;
5894 	}
5895 	return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
5896 	    sizeof (struct mode_page));
5897 }
5898 
5899 /*
5900  * Build Mode sense exception cntrl page
5901  */
5902 static int
5903 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5904 {
5905 	struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf;
5906 	sata_id_t *sata_id = &sdinfo->satadrv_id;
5907 
5908 	/*
5909 	 * Most of the fields are set to 0, being not supported and/or disabled
5910 	 */
5911 	bzero(buf, PAGELENGTH_INFO_EXCPT);
5912 
5913 	page->mode_page.code = MODEPAGE_INFO_EXCPT;
5914 	page->mode_page.length = PAGELENGTH_INFO_EXCPT;
5915 
5916 	/* Indicate that this is page is saveable */
5917 	page->mode_page.ps = 1;
5918 
5919 	/*
5920 	 * We will return the same data for default, current and saved page.
5921 	 * The only changeable bit is dexcpt and that bit is required
5922 	 * by the ATA specification to be preserved across power cycles.
5923 	 */
5924 	if (pcntrl != 1) {
5925 		page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED);
5926 		page->mrie = MRIE_ONLY_ON_REQUEST;
5927 	}
5928 	else
5929 		page->dexcpt = 1;	/* Only changeable parameter */
5930 
5931 	return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_info_excpt_page));
5932 }
5933 
5934 
5935 static int
5936 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5937 {
5938 	struct mode_acoustic_management *page =
5939 	    (struct mode_acoustic_management *)buf;
5940 	sata_id_t *sata_id = &sdinfo->satadrv_id;
5941 
5942 	/*
5943 	 * Most of the fields are set to 0, being not supported and/or disabled
5944 	 */
5945 	bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT);
5946 
5947 	switch (pcntrl) {
5948 	case P_CNTRL_DEFAULT:
5949 		/*  default paramters not supported */
5950 		return (0);
5951 
5952 	case P_CNTRL_CURRENT:
5953 	case P_CNTRL_SAVED:
5954 		/* Saved and current are supported and are identical */
5955 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
5956 		page->mode_page.length =
5957 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
5958 		page->mode_page.ps = 1;
5959 
5960 		/* Word 83 indicates if feature is supported */
5961 		/* If feature is not supported */
5962 		if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) {
5963 			page->acoustic_manag_enable =
5964 			    ACOUSTIC_DISABLED;
5965 		} else {
5966 			page->acoustic_manag_enable =
5967 			    ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT)
5968 			    != 0);
5969 			/* Word 94 inidicates the value */
5970 #ifdef	_LITTLE_ENDIAN
5971 			page->acoustic_manag_level =
5972 			    (uchar_t)sata_id->ai_acoustic;
5973 			page->vendor_recommended_value =
5974 			    sata_id->ai_acoustic >> 8;
5975 #else
5976 			page->acoustic_manag_level =
5977 			    sata_id->ai_acoustic >> 8;
5978 			page->vendor_recommended_value =
5979 			    (uchar_t)sata_id->ai_acoustic;
5980 #endif
5981 		}
5982 		break;
5983 
5984 	case P_CNTRL_CHANGEABLE:
5985 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
5986 		page->mode_page.length =
5987 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
5988 		page->mode_page.ps = 1;
5989 
5990 		/* Word 83 indicates if the feature is supported */
5991 		if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) {
5992 			page->acoustic_manag_enable =
5993 			    ACOUSTIC_ENABLED;
5994 			page->acoustic_manag_level = 0xff;
5995 		}
5996 		break;
5997 	}
5998 	return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
5999 	    sizeof (struct mode_page));
6000 }
6001 
6002 
6003 /*
6004  * Build Mode sense power condition page
6005  * NOT IMPLEMENTED.
6006  */
6007 static int
6008 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6009 {
6010 #ifndef __lock_lint
6011 	_NOTE(ARGUNUSED(sdinfo))
6012 	_NOTE(ARGUNUSED(pcntrl))
6013 	_NOTE(ARGUNUSED(buf))
6014 #endif
6015 	return (0);
6016 }
6017 
6018 
6019 /*
6020  * Process mode select caching page 8 (scsi3 format only).
6021  * Read Ahead (same as read cache) and Write Cache may be turned on and off
6022  * if these features are supported by the device. If these features are not
6023  * supported, quietly ignore them.
6024  * This function fails only if the SET FEATURE command sent to
6025  * the device fails. The page format is not varified, assuming that the
6026  * target driver operates correctly - if parameters length is too short,
6027  * we just drop the page.
6028  * Two command may be sent if both Read Cache/Read Ahead and Write Cache
6029  * setting have to be changed.
6030  * SET FEATURE command is executed synchronously, i.e. we wait here until
6031  * it is completed, regardless of the scsi pkt directives.
6032  *
6033  * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e.
6034  * changing DRA will change RCD.
6035  *
6036  * More than one SATA command may be executed to perform operations specified
6037  * by mode select pages. The first error terminates further execution.
6038  * Operations performed successully are not backed-up in such case.
6039  *
6040  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
6041  * If operation resulted in changing device setup, dmod flag should be set to
6042  * one (1). If parameters were not changed, dmod flag should be set to 0.
6043  * Upon return, if operation required sending command to the device, the rval
6044  * should be set to the value returned by sata_hba_start. If operation
6045  * did not require device access, rval should be set to TRAN_ACCEPT.
6046  * The pagelen should be set to the length of the page.
6047  *
6048  * This function has to be called with a port mutex held.
6049  *
6050  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6051  */
6052 int
6053 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page,
6054     int parmlen, int *pagelen, int *rval, int *dmod)
6055 {
6056 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6057 	sata_drive_info_t *sdinfo;
6058 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6059 	sata_id_t *sata_id;
6060 	struct scsi_extended_sense *sense;
6061 	int wce, dra;	/* Current settings */
6062 
6063 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6064 	    &spx->txlt_sata_pkt->satapkt_device);
6065 	sata_id = &sdinfo->satadrv_id;
6066 	*dmod = 0;
6067 
6068 	/* Verify parameters length. If too short, drop it */
6069 	if (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
6070 	    sizeof (struct mode_page) < parmlen) {
6071 		*scsipkt->pkt_scbp = STATUS_CHECK;
6072 		sense = sata_arq_sense(spx);
6073 		sense->es_key = KEY_ILLEGAL_REQUEST;
6074 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6075 		*pagelen = parmlen;
6076 		*rval = TRAN_ACCEPT;
6077 		return (SATA_FAILURE);
6078 	}
6079 
6080 	*pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page);
6081 
6082 	/*
6083 	 * We can manipulate only write cache and read ahead
6084 	 * (read cache) setting.
6085 	 */
6086 	if (!(sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) &&
6087 	    !(sata_id->ai_cmdset82 & SATA_WRITE_CACHE)) {
6088 		/*
6089 		 * None of the features is supported - ignore
6090 		 */
6091 		*rval = TRAN_ACCEPT;
6092 		return (SATA_SUCCESS);
6093 	}
6094 
6095 	/* Current setting of Read Ahead (and Read Cache) */
6096 	if (sata_id->ai_features85 & SATA_LOOK_AHEAD)
6097 		dra = 0;	/* 0 == not disabled */
6098 	else
6099 		dra = 1;
6100 	/* Current setting of Write Cache */
6101 	if (sata_id->ai_features85 & SATA_WRITE_CACHE)
6102 		wce = 1;
6103 	else
6104 		wce = 0;
6105 
6106 	if (page->dra == dra && page->wce == wce && page->rcd == dra) {
6107 		/* nothing to do */
6108 		*rval = TRAN_ACCEPT;
6109 		return (SATA_SUCCESS);
6110 	}
6111 	/*
6112 	 * Need to flip some setting
6113 	 * Set-up Internal SET FEATURES command(s)
6114 	 */
6115 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6116 	scmd->satacmd_addr_type = 0;
6117 	scmd->satacmd_device_reg = 0;
6118 	scmd->satacmd_status_reg = 0;
6119 	scmd->satacmd_error_reg = 0;
6120 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
6121 	if (page->dra != dra || page->rcd != dra) {
6122 		/* Need to flip read ahead setting */
6123 		if (dra == 0)
6124 			/* Disable read ahead / read cache */
6125 			scmd->satacmd_features_reg =
6126 			    SATAC_SF_DISABLE_READ_AHEAD;
6127 		else
6128 			/* Enable read ahead  / read cache */
6129 			scmd->satacmd_features_reg =
6130 			    SATAC_SF_ENABLE_READ_AHEAD;
6131 
6132 		/* Transfer command to HBA */
6133 		if (sata_hba_start(spx, rval) != 0)
6134 			/*
6135 			 * Pkt not accepted for execution.
6136 			 */
6137 			return (SATA_FAILURE);
6138 
6139 		*dmod = 1;
6140 
6141 		/* Now process return */
6142 		if (spx->txlt_sata_pkt->satapkt_reason !=
6143 		    SATA_PKT_COMPLETED) {
6144 			goto failure;	/* Terminate */
6145 		}
6146 	}
6147 
6148 	/* Note that the packet is not removed, so it could be re-used */
6149 	if (page->wce != wce) {
6150 		/* Need to flip Write Cache setting */
6151 		if (page->wce == 1)
6152 			/* Enable write cache */
6153 			scmd->satacmd_features_reg =
6154 			    SATAC_SF_ENABLE_WRITE_CACHE;
6155 		else
6156 			/* Disable write cache */
6157 			scmd->satacmd_features_reg =
6158 			    SATAC_SF_DISABLE_WRITE_CACHE;
6159 
6160 		/* Transfer command to HBA */
6161 		if (sata_hba_start(spx, rval) != 0)
6162 			/*
6163 			 * Pkt not accepted for execution.
6164 			 */
6165 			return (SATA_FAILURE);
6166 
6167 		*dmod = 1;
6168 
6169 		/* Now process return */
6170 		if (spx->txlt_sata_pkt->satapkt_reason !=
6171 		    SATA_PKT_COMPLETED) {
6172 			goto failure;
6173 		}
6174 	}
6175 	return (SATA_SUCCESS);
6176 
6177 failure:
6178 	sata_xlate_errors(spx);
6179 
6180 	return (SATA_FAILURE);
6181 }
6182 
6183 /*
6184  * Process mode select informational exceptions control page 0x1c
6185  *
6186  * The only changeable bit is dexcpt (disable exceptions).
6187  * MRIE (method of reporting informational exceptions) must be
6188  * "only on request".
6189  *
6190  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
6191  * If operation resulted in changing device setup, dmod flag should be set to
6192  * one (1). If parameters were not changed, dmod flag should be set to 0.
6193  * Upon return, if operation required sending command to the device, the rval
6194  * should be set to the value returned by sata_hba_start. If operation
6195  * did not require device access, rval should be set to TRAN_ACCEPT.
6196  * The pagelen should be set to the length of the page.
6197  *
6198  * This function has to be called with a port mutex held.
6199  *
6200  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6201  */
6202 static	int
6203 sata_mode_select_page_1c(
6204 	sata_pkt_txlate_t *spx,
6205 	struct mode_info_excpt_page *page,
6206 	int parmlen,
6207 	int *pagelen,
6208 	int *rval,
6209 	int *dmod)
6210 {
6211 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6212 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6213 	sata_drive_info_t *sdinfo;
6214 	sata_id_t *sata_id;
6215 	struct scsi_extended_sense *sense;
6216 
6217 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6218 	    &spx->txlt_sata_pkt->satapkt_device);
6219 	sata_id = &sdinfo->satadrv_id;
6220 
6221 	*dmod = 0;
6222 
6223 	/* Verify parameters length. If too short, drop it */
6224 	if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) < parmlen) ||
6225 	    page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) {
6226 		*scsipkt->pkt_scbp = STATUS_CHECK;
6227 		sense = sata_arq_sense(spx);
6228 		sense->es_key = KEY_ILLEGAL_REQUEST;
6229 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6230 		*pagelen = parmlen;
6231 		*rval = TRAN_ACCEPT;
6232 		return (SATA_FAILURE);
6233 	}
6234 
6235 	*pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page);
6236 
6237 	if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
6238 		*scsipkt->pkt_scbp = STATUS_CHECK;
6239 		sense = sata_arq_sense(spx);
6240 		sense->es_key = KEY_ILLEGAL_REQUEST;
6241 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6242 		*pagelen = parmlen;
6243 		*rval = TRAN_ACCEPT;
6244 		return (SATA_FAILURE);
6245 	}
6246 
6247 	/* If already in the state requested, we are done */
6248 	if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
6249 		/* nothing to do */
6250 		*rval = TRAN_ACCEPT;
6251 		return (SATA_SUCCESS);
6252 	}
6253 
6254 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6255 
6256 	/* Build SMART_ENABLE or SMART_DISABLE command */
6257 	scmd->satacmd_addr_type = 0;		/* N/A */
6258 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
6259 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
6260 	scmd->satacmd_features_reg = page->dexcpt ?
6261 	    SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS;
6262 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
6263 	scmd->satacmd_cmd_reg = SATAC_SMART;
6264 
6265 	/* Transfer command to HBA */
6266 	if (sata_hba_start(spx, rval) != 0)
6267 		/*
6268 		 * Pkt not accepted for execution.
6269 		 */
6270 		return (SATA_FAILURE);
6271 
6272 	*dmod = 1;	/* At least may have been modified */
6273 
6274 	/* Now process return */
6275 	if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED)
6276 		return (SATA_SUCCESS);
6277 
6278 	/* Packet did not complete successfully */
6279 	sata_xlate_errors(spx);
6280 
6281 	return (SATA_FAILURE);
6282 }
6283 
6284 int
6285 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct
6286     mode_acoustic_management *page, int parmlen, int *pagelen,
6287     int *rval, int *dmod)
6288 {
6289 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6290 	sata_drive_info_t *sdinfo;
6291 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6292 	sata_id_t *sata_id;
6293 	struct scsi_extended_sense *sense;
6294 
6295 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6296 	    &spx->txlt_sata_pkt->satapkt_device);
6297 	sata_id = &sdinfo->satadrv_id;
6298 	*dmod = 0;
6299 
6300 	/* If parmlen is too short or the feature is not supported, drop it */
6301 	if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6302 	    sizeof (struct mode_page)) < parmlen) ||
6303 	    (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) {
6304 		*scsipkt->pkt_scbp = STATUS_CHECK;
6305 		sense = sata_arq_sense(spx);
6306 		sense->es_key = KEY_ILLEGAL_REQUEST;
6307 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6308 		*pagelen = parmlen;
6309 		*rval = TRAN_ACCEPT;
6310 		return (SATA_FAILURE);
6311 	}
6312 
6313 	*pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6314 	    sizeof (struct mode_page);
6315 
6316 	/*
6317 	 * We can enable and disable acoustice management and
6318 	 * set the acoustic management level.
6319 	 */
6320 
6321 	/*
6322 	 * Set-up Internal SET FEATURES command(s)
6323 	 */
6324 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6325 	scmd->satacmd_addr_type = 0;
6326 	scmd->satacmd_device_reg = 0;
6327 	scmd->satacmd_status_reg = 0;
6328 	scmd->satacmd_error_reg = 0;
6329 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
6330 	if (page->acoustic_manag_enable) {
6331 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC;
6332 		scmd->satacmd_sec_count_lsb = page->acoustic_manag_level;
6333 	} else {	/* disabling acoustic management */
6334 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC;
6335 	}
6336 
6337 	/* Transfer command to HBA */
6338 	if (sata_hba_start(spx, rval) != 0)
6339 		/*
6340 		 * Pkt not accepted for execution.
6341 		 */
6342 		return (SATA_FAILURE);
6343 
6344 	/* Now process return */
6345 	if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) {
6346 		sata_xlate_errors(spx);
6347 		return (SATA_FAILURE);
6348 	}
6349 
6350 	*dmod = 1;
6351 
6352 	return (SATA_SUCCESS);
6353 }
6354 
6355 
6356 
6357 
6358 /*
6359  * sata_build_lsense_page0() is used to create the
6360  * SCSI LOG SENSE page 0 (supported log pages)
6361  *
6362  * Currently supported pages are 0, 0x10, 0x2f and 0x30
6363  * (supported log pages, self-test results, informational exceptions
6364  *  and Sun vendor specific ATA SMART data).
6365  *
6366  * Takes a sata_drive_info t * and the address of a buffer
6367  * in which to create the page information.
6368  *
6369  * Returns the number of bytes valid in the buffer.
6370  */
6371 static	int
6372 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf)
6373 {
6374 	struct log_parameter *lpp = (struct log_parameter *)buf;
6375 	uint8_t *page_ptr = (uint8_t *)lpp->param_values;
6376 	int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */
6377 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6378 
6379 	lpp->param_code[0] = 0;
6380 	lpp->param_code[1] = 0;
6381 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
6382 	*page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES;
6383 
6384 	if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
6385 		if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) {
6386 			*page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS;
6387 			++num_pages_supported;
6388 		}
6389 		*page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS;
6390 		++num_pages_supported;
6391 		*page_ptr++ = PAGE_CODE_SMART_READ_DATA;
6392 		++num_pages_supported;
6393 	}
6394 
6395 	lpp->param_len = num_pages_supported;
6396 
6397 	return ((&lpp->param_values[0] - (uint8_t *)lpp) +
6398 	    num_pages_supported);
6399 }
6400 
6401 /*
6402  * sata_build_lsense_page_10() is used to create the
6403  * SCSI LOG SENSE page 0x10 (self-test results)
6404  *
6405  * Takes a sata_drive_info t * and the address of a buffer
6406  * in which to create the page information as well as a sata_hba_inst_t *.
6407  *
6408  * Returns the number of bytes valid in the buffer.
6409  */
6410 static	int
6411 sata_build_lsense_page_10(
6412 	sata_drive_info_t *sdinfo,
6413 	uint8_t *buf,
6414 	sata_hba_inst_t *sata_hba_inst)
6415 {
6416 	struct log_parameter *lpp = (struct log_parameter *)buf;
6417 	int rval;
6418 
6419 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6420 		struct smart_ext_selftest_log *ext_selftest_log;
6421 
6422 		ext_selftest_log = kmem_zalloc(
6423 		    sizeof (struct smart_ext_selftest_log), KM_SLEEP);
6424 
6425 		rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo,
6426 		    ext_selftest_log, 0);
6427 		if (rval == 0) {
6428 			int index, start_index;
6429 			struct smart_ext_selftest_log_entry *entry;
6430 			static const struct smart_ext_selftest_log_entry empty =
6431 			    {0};
6432 			uint16_t block_num;
6433 			int count;
6434 			boolean_t only_one_block = B_FALSE;
6435 
6436 			index = ext_selftest_log->
6437 			    smart_ext_selftest_log_index[0];
6438 			index |= ext_selftest_log->
6439 			    smart_ext_selftest_log_index[1] << 8;
6440 			if (index == 0)
6441 				goto out;
6442 
6443 			--index;	/* Correct for 0 origin */
6444 			start_index = index;	/* remember where we started */
6445 			block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6446 			if (block_num != 0) {
6447 				rval = sata_ext_smart_selftest_read_log(
6448 				    sata_hba_inst, sdinfo, ext_selftest_log,
6449 				    block_num);
6450 				if (rval != 0)
6451 					goto out;
6452 			}
6453 			index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6454 			entry =
6455 			    &ext_selftest_log->
6456 			    smart_ext_selftest_log_entries[index];
6457 
6458 			for (count = 1;
6459 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
6460 			    ++count) {
6461 				uint8_t status;
6462 				uint8_t code;
6463 				uint8_t sense_key;
6464 				uint8_t add_sense_code;
6465 				uint8_t add_sense_code_qual;
6466 
6467 				/* If this is an unused entry, we are done */
6468 				if (bcmp(entry, &empty, sizeof (empty)) == 0) {
6469 					/* Broken firmware on some disks */
6470 					if (index + 1 ==
6471 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK) {
6472 						--entry;
6473 						--index;
6474 						if (bcmp(entry, &empty,
6475 						    sizeof (empty)) == 0)
6476 							goto out;
6477 					} else
6478 						goto out;
6479 				}
6480 
6481 				if (only_one_block &&
6482 				    start_index == index)
6483 					goto out;
6484 
6485 				lpp->param_code[0] = 0;
6486 				lpp->param_code[1] = count;
6487 				lpp->param_ctrl_flags =
6488 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
6489 				lpp->param_len =
6490 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
6491 
6492 				status = entry->smart_ext_selftest_log_status;
6493 				status >>= 4;
6494 				switch (status) {
6495 				case 0:
6496 				default:
6497 					sense_key = KEY_NO_SENSE;
6498 					add_sense_code =
6499 					    SD_SCSI_ASC_NO_ADD_SENSE;
6500 					add_sense_code_qual = 0;
6501 					break;
6502 				case 1:
6503 					sense_key = KEY_ABORTED_COMMAND;
6504 					add_sense_code =
6505 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6506 					add_sense_code_qual = SCSI_COMPONENT_81;
6507 					break;
6508 				case 2:
6509 					sense_key = KEY_ABORTED_COMMAND;
6510 					add_sense_code =
6511 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6512 					add_sense_code_qual = SCSI_COMPONENT_82;
6513 					break;
6514 				case 3:
6515 					sense_key = KEY_ABORTED_COMMAND;
6516 					add_sense_code =
6517 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6518 					add_sense_code_qual = SCSI_COMPONENT_83;
6519 					break;
6520 				case 4:
6521 					sense_key = KEY_HARDWARE_ERROR;
6522 					add_sense_code =
6523 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6524 					add_sense_code_qual = SCSI_COMPONENT_84;
6525 					break;
6526 				case 5:
6527 					sense_key = KEY_HARDWARE_ERROR;
6528 					add_sense_code =
6529 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6530 					add_sense_code_qual = SCSI_COMPONENT_85;
6531 					break;
6532 				case 6:
6533 					sense_key = KEY_HARDWARE_ERROR;
6534 					add_sense_code =
6535 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6536 					add_sense_code_qual = SCSI_COMPONENT_86;
6537 					break;
6538 				case 7:
6539 					sense_key = KEY_MEDIUM_ERROR;
6540 					add_sense_code =
6541 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6542 					add_sense_code_qual = SCSI_COMPONENT_87;
6543 					break;
6544 				case 8:
6545 					sense_key = KEY_HARDWARE_ERROR;
6546 					add_sense_code =
6547 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6548 					add_sense_code_qual = SCSI_COMPONENT_88;
6549 					break;
6550 				}
6551 				code = 0;	/* unspecified */
6552 				status |= (code << 4);
6553 				lpp->param_values[0] = status;
6554 				lpp->param_values[1] = 0; /* unspecified */
6555 				lpp->param_values[2] = entry->
6556 				    smart_ext_selftest_log_timestamp[1];
6557 				lpp->param_values[3] = entry->
6558 				    smart_ext_selftest_log_timestamp[0];
6559 				if (status != 0) {
6560 					lpp->param_values[4] = 0;
6561 					lpp->param_values[5] = 0;
6562 					lpp->param_values[6] = entry->
6563 					    smart_ext_selftest_log_failing_lba
6564 					    [5];
6565 					lpp->param_values[7] = entry->
6566 					    smart_ext_selftest_log_failing_lba
6567 					    [4];
6568 					lpp->param_values[8] = entry->
6569 					    smart_ext_selftest_log_failing_lba
6570 					    [3];
6571 					lpp->param_values[9] = entry->
6572 					    smart_ext_selftest_log_failing_lba
6573 					    [2];
6574 					lpp->param_values[10] = entry->
6575 					    smart_ext_selftest_log_failing_lba
6576 					    [1];
6577 					lpp->param_values[11] = entry->
6578 					    smart_ext_selftest_log_failing_lba
6579 					    [0];
6580 				} else {	/* No bad block address */
6581 					lpp->param_values[4] = 0xff;
6582 					lpp->param_values[5] = 0xff;
6583 					lpp->param_values[6] = 0xff;
6584 					lpp->param_values[7] = 0xff;
6585 					lpp->param_values[8] = 0xff;
6586 					lpp->param_values[9] = 0xff;
6587 					lpp->param_values[10] = 0xff;
6588 					lpp->param_values[11] = 0xff;
6589 				}
6590 
6591 				lpp->param_values[12] = sense_key;
6592 				lpp->param_values[13] = add_sense_code;
6593 				lpp->param_values[14] = add_sense_code_qual;
6594 				lpp->param_values[15] = 0; /* undefined */
6595 
6596 				lpp = (struct log_parameter *)
6597 				    (((uint8_t *)lpp) +
6598 				    SCSI_LOG_PARAM_HDR_LEN +
6599 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
6600 
6601 				--index;	/* Back up to previous entry */
6602 				if (index < 0) {
6603 					if (block_num > 0) {
6604 						--block_num;
6605 					} else {
6606 						struct read_log_ext_directory
6607 						    logdir;
6608 
6609 						rval =
6610 						    sata_read_log_ext_directory(
6611 						    sata_hba_inst, sdinfo,
6612 						    &logdir);
6613 						if (rval == -1)
6614 							goto out;
6615 						if ((logdir.read_log_ext_vers
6616 						    [0] == 0) &&
6617 						    (logdir.read_log_ext_vers
6618 						    [1] == 0))
6619 							goto out;
6620 						block_num =
6621 						    logdir.read_log_ext_nblks
6622 						    [EXT_SMART_SELFTEST_LOG_PAGE
6623 						    - 1][0];
6624 						block_num |= logdir.
6625 						    read_log_ext_nblks
6626 						    [EXT_SMART_SELFTEST_LOG_PAGE
6627 						    - 1][1] << 8;
6628 						--block_num;
6629 						only_one_block =
6630 						    (block_num == 0);
6631 					}
6632 					rval = sata_ext_smart_selftest_read_log(
6633 					    sata_hba_inst, sdinfo,
6634 					    ext_selftest_log, block_num);
6635 					if (rval != 0)
6636 						goto out;
6637 
6638 					index =
6639 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK -
6640 					    1;
6641 				}
6642 				index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6643 				entry = &ext_selftest_log->
6644 				    smart_ext_selftest_log_entries[index];
6645 			}
6646 		}
6647 out:
6648 		kmem_free(ext_selftest_log,
6649 		    sizeof (struct smart_ext_selftest_log));
6650 	} else {
6651 		struct smart_selftest_log *selftest_log;
6652 
6653 		selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log),
6654 		    KM_SLEEP);
6655 
6656 		rval = sata_smart_selftest_log(sata_hba_inst, sdinfo,
6657 		    selftest_log);
6658 
6659 		if (rval == 0) {
6660 			int index;
6661 			int count;
6662 			struct smart_selftest_log_entry *entry;
6663 			static const struct smart_selftest_log_entry empty =
6664 			    { 0 };
6665 
6666 			index = selftest_log->smart_selftest_log_index;
6667 			if (index == 0)
6668 				goto done;
6669 			--index;	/* Correct for 0 origin */
6670 			entry = &selftest_log->
6671 			    smart_selftest_log_entries[index];
6672 			for (count = 1;
6673 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
6674 			    ++count) {
6675 				uint8_t status;
6676 				uint8_t code;
6677 				uint8_t sense_key;
6678 				uint8_t add_sense_code;
6679 				uint8_t add_sense_code_qual;
6680 
6681 				if (bcmp(entry, &empty, sizeof (empty)) == 0)
6682 					goto done;
6683 
6684 				lpp->param_code[0] = 0;
6685 				lpp->param_code[1] = count;
6686 				lpp->param_ctrl_flags =
6687 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
6688 				lpp->param_len =
6689 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
6690 
6691 				status = entry->smart_selftest_log_status;
6692 				status >>= 4;
6693 				switch (status) {
6694 				case 0:
6695 				default:
6696 					sense_key = KEY_NO_SENSE;
6697 					add_sense_code =
6698 					    SD_SCSI_ASC_NO_ADD_SENSE;
6699 					break;
6700 				case 1:
6701 					sense_key = KEY_ABORTED_COMMAND;
6702 					add_sense_code =
6703 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6704 					add_sense_code_qual = SCSI_COMPONENT_81;
6705 					break;
6706 				case 2:
6707 					sense_key = KEY_ABORTED_COMMAND;
6708 					add_sense_code =
6709 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6710 					add_sense_code_qual = SCSI_COMPONENT_82;
6711 					break;
6712 				case 3:
6713 					sense_key = KEY_ABORTED_COMMAND;
6714 					add_sense_code =
6715 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6716 					add_sense_code_qual = SCSI_COMPONENT_83;
6717 					break;
6718 				case 4:
6719 					sense_key = KEY_HARDWARE_ERROR;
6720 					add_sense_code =
6721 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6722 					add_sense_code_qual = SCSI_COMPONENT_84;
6723 					break;
6724 				case 5:
6725 					sense_key = KEY_HARDWARE_ERROR;
6726 					add_sense_code =
6727 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6728 					add_sense_code_qual = SCSI_COMPONENT_85;
6729 					break;
6730 				case 6:
6731 					sense_key = KEY_HARDWARE_ERROR;
6732 					add_sense_code =
6733 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6734 					add_sense_code_qual = SCSI_COMPONENT_86;
6735 					break;
6736 				case 7:
6737 					sense_key = KEY_MEDIUM_ERROR;
6738 					add_sense_code =
6739 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6740 					add_sense_code_qual = SCSI_COMPONENT_87;
6741 					break;
6742 				case 8:
6743 					sense_key = KEY_HARDWARE_ERROR;
6744 					add_sense_code =
6745 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6746 					add_sense_code_qual = SCSI_COMPONENT_88;
6747 					break;
6748 				}
6749 				code = 0;	/* unspecified */
6750 				status |= (code << 4);
6751 				lpp->param_values[0] = status;
6752 				lpp->param_values[1] = 0; /* unspecified */
6753 				lpp->param_values[2] = entry->
6754 				    smart_selftest_log_timestamp[1];
6755 				lpp->param_values[3] = entry->
6756 				    smart_selftest_log_timestamp[0];
6757 				if (status != 0) {
6758 					lpp->param_values[4] = 0;
6759 					lpp->param_values[5] = 0;
6760 					lpp->param_values[6] = 0;
6761 					lpp->param_values[7] = 0;
6762 					lpp->param_values[8] = entry->
6763 					    smart_selftest_log_failing_lba[3];
6764 					lpp->param_values[9] = entry->
6765 					    smart_selftest_log_failing_lba[2];
6766 					lpp->param_values[10] = entry->
6767 					    smart_selftest_log_failing_lba[1];
6768 					lpp->param_values[11] = entry->
6769 					    smart_selftest_log_failing_lba[0];
6770 				} else {	/* No block address */
6771 					lpp->param_values[4] = 0xff;
6772 					lpp->param_values[5] = 0xff;
6773 					lpp->param_values[6] = 0xff;
6774 					lpp->param_values[7] = 0xff;
6775 					lpp->param_values[8] = 0xff;
6776 					lpp->param_values[9] = 0xff;
6777 					lpp->param_values[10] = 0xff;
6778 					lpp->param_values[11] = 0xff;
6779 				}
6780 				lpp->param_values[12] = sense_key;
6781 				lpp->param_values[13] = add_sense_code;
6782 				lpp->param_values[14] = add_sense_code_qual;
6783 				lpp->param_values[15] = 0; /* undefined */
6784 
6785 				lpp = (struct log_parameter *)
6786 				    (((uint8_t *)lpp) +
6787 				    SCSI_LOG_PARAM_HDR_LEN +
6788 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
6789 				--index;	/* back up to previous entry */
6790 				if (index < 0) {
6791 					index =
6792 					    NUM_SMART_SELFTEST_LOG_ENTRIES - 1;
6793 				}
6794 				entry = &selftest_log->
6795 				    smart_selftest_log_entries[index];
6796 			}
6797 		}
6798 done:
6799 		kmem_free(selftest_log, sizeof (struct smart_selftest_log));
6800 	}
6801 
6802 	return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) *
6803 	    SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS);
6804 }
6805 
6806 /*
6807  * sata_build_lsense_page_2f() is used to create the
6808  * SCSI LOG SENSE page 0x10 (informational exceptions)
6809  *
6810  * Takes a sata_drive_info t * and the address of a buffer
6811  * in which to create the page information as well as a sata_hba_inst_t *.
6812  *
6813  * Returns the number of bytes valid in the buffer.
6814  */
6815 static	int
6816 sata_build_lsense_page_2f(
6817 	sata_drive_info_t *sdinfo,
6818 	uint8_t *buf,
6819 	sata_hba_inst_t *sata_hba_inst)
6820 {
6821 	struct log_parameter *lpp = (struct log_parameter *)buf;
6822 	int rval;
6823 	uint8_t *smart_data;
6824 	uint8_t temp;
6825 	sata_id_t *sata_id;
6826 #define	SMART_NO_TEMP	0xff
6827 
6828 	lpp->param_code[0] = 0;
6829 	lpp->param_code[1] = 0;
6830 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
6831 
6832 	/* Now get the SMART status w.r.t. threshold exceeded */
6833 	rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo);
6834 	switch (rval) {
6835 	case 1:
6836 		lpp->param_values[0] = SCSI_PREDICTED_FAILURE;
6837 		lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE;
6838 		break;
6839 	case 0:
6840 	case -1:	/* failed to get data */
6841 		lpp->param_values[0] = 0;	/* No failure predicted */
6842 		lpp->param_values[1] = 0;
6843 		break;
6844 #if defined(SATA_DEBUG)
6845 	default:
6846 		cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value");
6847 		/* NOTREACHED */
6848 #endif
6849 	}
6850 
6851 	sata_id = &sdinfo->satadrv_id;
6852 	if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP))
6853 		temp = SMART_NO_TEMP;
6854 	else {
6855 		/* Now get the temperature */
6856 		smart_data = kmem_zalloc(512, KM_SLEEP);
6857 		rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data,
6858 		    SCT_STATUS_LOG_PAGE, 1);
6859 		if (rval == -1)
6860 			temp = SMART_NO_TEMP;
6861 		else {
6862 			temp = smart_data[200];
6863 			if (temp & 0x80) {
6864 				if (temp & 0x7f)
6865 					temp = 0;
6866 				else
6867 					temp = SMART_NO_TEMP;
6868 			}
6869 		}
6870 		kmem_free(smart_data, 512);
6871 	}
6872 
6873 	lpp->param_values[2] = temp;	/* most recent temperature */
6874 	lpp->param_values[3] = 0;	/* required vendor specific byte */
6875 
6876 	lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN;
6877 
6878 
6879 	return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN);
6880 }
6881 
6882 /*
6883  * sata_build_lsense_page_30() is used to create the
6884  * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data).
6885  *
6886  * Takes a sata_drive_info t * and the address of a buffer
6887  * in which to create the page information as well as a sata_hba_inst_t *.
6888  *
6889  * Returns the number of bytes valid in the buffer.
6890  */
6891 static int
6892 sata_build_lsense_page_30(
6893 	sata_drive_info_t *sdinfo,
6894 	uint8_t *buf,
6895 	sata_hba_inst_t *sata_hba_inst)
6896 {
6897 	struct smart_data *smart_data = (struct smart_data *)buf;
6898 	int rval;
6899 
6900 	/* Now do the SMART READ DATA */
6901 	rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data);
6902 	if (rval == -1)
6903 		return (0);
6904 
6905 	return (sizeof (struct smart_data));
6906 }
6907 
6908 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */
6909 
6910 /*
6911  * Start command for ATAPI device.
6912  * This function processes scsi_pkt requests.
6913  * Only CD/DVD devices are supported.
6914  * Most commands are packet without any translation into Packet Command.
6915  * Some may be trapped and executed as SATA commands (not clear which one).
6916  *
6917  * Returns TRAN_ACCEPT if command is accepted for execution (or completed
6918  * execution).
6919  * Returns other TRAN_XXXX codes if command is not accepted or completed
6920  * (see return values for sata_hba_start()).
6921  *
6922  * Note:
6923  * Inquiry cdb format differs between transport version 2 and 3.
6924  * However, the transport version 3 devices that were checked did not adhere
6925  * to the specification (ignored MSB of the allocation length). Therefore,
6926  * the transport version is not checked, but Inquiry allocation length is
6927  * truncated to 255 bytes if the original allocation length set-up by the
6928  * target driver is greater than 255 bytes.
6929  */
6930 static int
6931 sata_txlt_atapi(sata_pkt_txlate_t *spx)
6932 {
6933 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6934 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6935 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
6936 	sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx);
6937 	sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba,
6938 	    &spx->txlt_sata_pkt->satapkt_device);
6939 	int cport = SATA_TXLT_CPORT(spx);
6940 	int cdblen;
6941 	int rval, reason;
6942 	int synch;
6943 	union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp;
6944 
6945 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
6946 
6947 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
6948 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6949 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6950 		return (rval);
6951 	}
6952 
6953 	/*
6954 	 * ATAPI device executes some ATA commands in addition to MMC command
6955 	 * set. These ATA commands may be executed by the regular SATA
6956 	 * translation functions. None needs to be captured now.
6957 	 * Other commands belong to MMC command set and are delivered
6958 	 * to ATAPI device via Packet Command.
6959 	 */
6960 
6961 	/* Check the size of cdb */
6962 	cdblen = scsi_cdb_size[GETGROUP(cdbp)];
6963 	if (cdblen > sdinfo->satadrv_atapi_cdb_len) {
6964 		sata_log(NULL, CE_WARN,
6965 		    "sata: invalid ATAPI cdb length %d",
6966 		    scsipkt->pkt_cdblen);
6967 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6968 		return (TRAN_BADPKT);
6969 	}
6970 
6971 	SATAATAPITRACE(spx, cdblen);
6972 
6973 	/*
6974 	 * For non-read/write commands we need to
6975 	 * map buffer
6976 	 */
6977 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6978 	case SCMD_READ:
6979 	case SCMD_READ_G1:
6980 	case SCMD_READ_G5:
6981 	case SCMD_READ_G4:
6982 	case SCMD_WRITE:
6983 	case SCMD_WRITE_G1:
6984 	case SCMD_WRITE_G5:
6985 	case SCMD_WRITE_G4:
6986 		break;
6987 	default:
6988 		if (bp != NULL) {
6989 			if (bp->b_flags & (B_PHYS | B_PAGEIO))
6990 				bp_mapin(bp);
6991 		}
6992 		break;
6993 	}
6994 	/*
6995 	 * scmd->satacmd_flags.sata_data_direction default -
6996 	 * SATA_DIR_NODATA_XFER - is set by
6997 	 * sata_txlt_generic_pkt_info().
6998 	 */
6999 	if (scmd->satacmd_bp) {
7000 		if (scmd->satacmd_bp->b_flags & B_READ) {
7001 			scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7002 		} else {
7003 			scmd->satacmd_flags.sata_data_direction =
7004 			    SATA_DIR_WRITE;
7005 		}
7006 	}
7007 
7008 	/*
7009 	 * Set up ATAPI packet command.
7010 	 */
7011 
7012 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7013 
7014 	/* Copy cdb into sata_cmd */
7015 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7016 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7017 	bcopy(cdbp, scmd->satacmd_acdb, cdblen);
7018 
7019 	/* See note in the command header */
7020 	if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) {
7021 		if (scmd->satacmd_acdb[3] != 0)
7022 			scmd->satacmd_acdb[4] = 255;
7023 	}
7024 
7025 #ifdef SATA_DEBUG
7026 	if (sata_debug_flags & SATA_DBG_ATAPI) {
7027 		uint8_t *p = scmd->satacmd_acdb;
7028 		char buf[3 * SATA_ATAPI_MAX_CDB_LEN];
7029 
7030 		(void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN,
7031 		    "%02x %02x %02x %02x %02x %02x %02x %02x "
7032 		    "%2x %02x %02x %02x %02x %02x %02x %02x",
7033 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7034 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7035 		buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0';
7036 		cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf);
7037 	}
7038 #endif
7039 
7040 	/*
7041 	 * Preset request sense data to NO SENSE.
7042 	 * If there is no way to get error information via Request Sense,
7043 	 * the packet request sense data would not have to be modified by HBA,
7044 	 * but it could be returned as is.
7045 	 */
7046 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
7047 	sata_fixed_sense_data_preset(
7048 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7049 
7050 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
7051 		/* Need callback function */
7052 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion;
7053 		synch = FALSE;
7054 	} else
7055 		synch = TRUE;
7056 
7057 	/* Transfer command to HBA */
7058 	if (sata_hba_start(spx, &rval) != 0) {
7059 		/* Pkt not accepted for execution */
7060 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
7061 		return (rval);
7062 	}
7063 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
7064 	/*
7065 	 * If execution is non-synchronous,
7066 	 * a callback function will handle potential errors, translate
7067 	 * the response and will do a callback to a target driver.
7068 	 * If it was synchronous, use the same framework callback to check
7069 	 * an execution status.
7070 	 */
7071 	if (synch) {
7072 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7073 		    "synchronous execution status %x\n",
7074 		    spx->txlt_sata_pkt->satapkt_reason);
7075 		sata_txlt_atapi_completion(spx->txlt_sata_pkt);
7076 	}
7077 	return (TRAN_ACCEPT);
7078 }
7079 
7080 
7081 /*
7082  * ATAPI Packet command completion.
7083  *
7084  * Failure of the command passed via Packet command are considered device
7085  * error. SATA HBA driver would have to retrieve error data (via Request
7086  * Sense command delivered via error retrieval sata packet) and copy it
7087  * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data.
7088  */
7089 static void
7090 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt)
7091 {
7092 	sata_pkt_txlate_t *spx =
7093 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7094 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7095 	struct scsi_extended_sense *sense;
7096 	struct buf *bp;
7097 	int rval;
7098 
7099 #ifdef SATA_DEBUG
7100 	uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense;
7101 #endif
7102 
7103 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7104 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7105 
7106 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7107 		/* Normal completion */
7108 		if (sata_pkt->satapkt_cmd.satacmd_bp != NULL)
7109 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
7110 		scsipkt->pkt_reason = CMD_CMPLT;
7111 		*scsipkt->pkt_scbp = STATUS_GOOD;
7112 		if (spx->txlt_tmp_buf != NULL) {
7113 			/* Temporary buffer was used */
7114 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7115 			if (bp->b_flags & B_READ) {
7116 				rval = ddi_dma_sync(
7117 				    spx->txlt_buf_dma_handle, 0, 0,
7118 				    DDI_DMA_SYNC_FORCPU);
7119 				ASSERT(rval == DDI_SUCCESS);
7120 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7121 				    bp->b_bcount);
7122 			}
7123 		}
7124 	} else {
7125 		/*
7126 		 * Something went wrong - analyze return
7127 		 */
7128 		*scsipkt->pkt_scbp = STATUS_CHECK;
7129 		sense = sata_arq_sense(spx);
7130 
7131 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7132 			scsipkt->pkt_reason = CMD_INCOMPLETE;
7133 			/*
7134 			 * We may not have ARQ data if there was a double
7135 			 * error. But sense data in sata packet was pre-set
7136 			 * with NO SENSE so it is valid even if HBA could
7137 			 * not retrieve a real sense data.
7138 			 * Just copy this sense data into scsi pkt sense area.
7139 			 */
7140 			bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense,
7141 			    SATA_ATAPI_MIN_RQSENSE_LEN);
7142 #ifdef SATA_DEBUG
7143 			if (sata_debug_flags & SATA_DBG_SCSI_IF) {
7144 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7145 				    "sata_txlt_atapi_completion: %02x\n"
7146 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
7147 				    "          %02x %02x %02x %02x %02x %02x "
7148 				    "          %02x %02x %02x %02x %02x %02x\n",
7149 				    scsipkt->pkt_reason,
7150 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7151 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7152 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7153 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7154 				    rqsp[16], rqsp[17]);
7155 			}
7156 #endif
7157 		} else {
7158 			switch (sata_pkt->satapkt_reason) {
7159 			case SATA_PKT_PORT_ERROR:
7160 				/*
7161 				 * We have no device data.
7162 				 */
7163 				scsipkt->pkt_reason = CMD_INCOMPLETE;
7164 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
7165 				    STATE_GOT_TARGET | STATE_SENT_CMD |
7166 				    STATE_GOT_STATUS);
7167 				sense->es_key = KEY_HARDWARE_ERROR;
7168 				break;
7169 
7170 			case SATA_PKT_TIMEOUT:
7171 				scsipkt->pkt_reason = CMD_TIMEOUT;
7172 				scsipkt->pkt_statistics |=
7173 				    STAT_TIMEOUT | STAT_DEV_RESET;
7174 				/*
7175 				 * Need to check if HARDWARE_ERROR/
7176 				 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more
7177 				 * appropriate.
7178 				 */
7179 				break;
7180 
7181 			case SATA_PKT_ABORTED:
7182 				scsipkt->pkt_reason = CMD_ABORTED;
7183 				scsipkt->pkt_statistics |= STAT_ABORTED;
7184 				/* Should we set key COMMAND_ABPRTED? */
7185 				break;
7186 
7187 			case SATA_PKT_RESET:
7188 				scsipkt->pkt_reason = CMD_RESET;
7189 				scsipkt->pkt_statistics |= STAT_DEV_RESET;
7190 				/*
7191 				 * May be we should set Unit Attention /
7192 				 * Reset. Perhaps the same should be
7193 				 * returned for disks....
7194 				 */
7195 				sense->es_key = KEY_UNIT_ATTENTION;
7196 				sense->es_add_code = SD_SCSI_ASC_RESET;
7197 				break;
7198 
7199 			default:
7200 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7201 				    "sata_txlt_atapi_completion: "
7202 				    "invalid packet completion reason"));
7203 				scsipkt->pkt_reason = CMD_TRAN_ERR;
7204 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
7205 				    STATE_GOT_TARGET | STATE_SENT_CMD |
7206 				    STATE_GOT_STATUS);
7207 				break;
7208 			}
7209 		}
7210 	}
7211 
7212 	SATAATAPITRACE(spx, 0);
7213 
7214 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7215 	    scsipkt->pkt_comp != NULL) {
7216 		/* scsi callback required */
7217 		(*scsipkt->pkt_comp)(scsipkt);
7218 	}
7219 }
7220 
7221 /*
7222  * Set up error retrieval sata command for ATAPI Packet Command error data
7223  * recovery.
7224  *
7225  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
7226  * returns SATA_FAILURE otherwise.
7227  */
7228 
7229 static int
7230 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
7231 {
7232 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
7233 	sata_cmd_t *scmd;
7234 	struct buf *bp;
7235 
7236 	/*
7237 	 * Allocate dma-able buffer error data.
7238 	 * Buffer allocation will take care of buffer alignment and other DMA
7239 	 * attributes.
7240 	 */
7241 	bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN);
7242 	if (bp == NULL) {
7243 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
7244 		    "sata_get_err_retrieval_pkt: "
7245 		    "cannot allocate buffer for error data", NULL);
7246 		return (SATA_FAILURE);
7247 	}
7248 	bp_mapin(bp); /* make data buffer accessible */
7249 
7250 	/* Operation modes are up to the caller */
7251 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7252 
7253 	/* Synchronous mode, no callback - may be changed by the caller */
7254 	spkt->satapkt_comp = NULL;
7255 	spkt->satapkt_time = sata_default_pkt_time;
7256 
7257 	scmd = &spkt->satapkt_cmd;
7258 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7259 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7260 
7261 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7262 
7263 	/*
7264 	 * Set-up acdb. Request Sense CDB (packet command content) is
7265 	 * not in DMA-able buffer. Its handling is HBA-specific (how
7266 	 * it is transfered into packet FIS).
7267 	 */
7268 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7269 	bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN);
7270 	/* Following zeroing of pad bytes may not be necessary */
7271 	bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN],
7272 	    sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN);
7273 
7274 	/*
7275 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
7276 	 * before accessing it. Handle is in usual place in translate struct.
7277 	 */
7278 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
7279 
7280 	/*
7281 	 * Preset request sense data to NO SENSE.
7282 	 * Here it is redundant, only for a symetry with scsi-originated
7283 	 * packets. It should not be used for anything but debugging.
7284 	 */
7285 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
7286 	sata_fixed_sense_data_preset(
7287 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7288 
7289 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7290 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7291 
7292 	return (SATA_SUCCESS);
7293 }
7294 
7295 /*
7296  * Set-up ATAPI packet command.
7297  * Data transfer direction has to be set-up in sata_cmd structure prior to
7298  * calling this function.
7299  *
7300  * Returns void
7301  */
7302 
7303 static void
7304 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo)
7305 {
7306 	scmd->satacmd_addr_type = 0;		/* N/A */
7307 	scmd->satacmd_sec_count_lsb = 0;	/* no tag */
7308 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
7309 	scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ;
7310 	scmd->satacmd_lba_high_lsb =
7311 	    (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8);
7312 	scmd->satacmd_cmd_reg = SATAC_PACKET;	/* Command */
7313 
7314 	/*
7315 	 * We want all data to be transfered via DMA.
7316 	 * But specify it only if drive supports DMA and DMA mode is
7317 	 * selected - some drives are sensitive about it.
7318 	 * Hopefully it wil work for all drives....
7319 	 */
7320 	if (sdinfo->satadrv_settings & SATA_DEV_DMA)
7321 		scmd->satacmd_features_reg = SATA_ATAPI_F_DMA;
7322 
7323 	/*
7324 	 * Features register requires special care for devices that use
7325 	 * Serial ATA bridge - they need an explicit specification of
7326 	 * the data transfer direction for Packet DMA commands.
7327 	 * Setting this bit is harmless if DMA is not used.
7328 	 *
7329 	 * Many drives do not implement word 80, specifying what ATA/ATAPI
7330 	 * spec they follow.
7331 	 * We are arbitrarily following the latest SerialATA 2.6 spec,
7332 	 * which uses ATA/ATAPI 6 specification for Identify Data, unless
7333 	 * ATA/ATAPI-7 support is explicitly indicated.
7334 	 */
7335 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
7336 	    sdinfo->satadrv_id.ai_majorversion != 0xffff &&
7337 	    (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) {
7338 		/*
7339 		 * Specification of major version is valid and version 7
7340 		 * is supported. It does automatically imply that all
7341 		 * spec features are supported. For now, we assume that
7342 		 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete.
7343 		 */
7344 		if ((sdinfo->satadrv_id.ai_dirdma &
7345 		    SATA_ATAPI_ID_DMADIR_REQ) != 0) {
7346 			if (scmd->satacmd_flags.sata_data_direction ==
7347 			    SATA_DIR_READ)
7348 			scmd->satacmd_features_reg |=
7349 			    SATA_ATAPI_F_DATA_DIR_READ;
7350 		}
7351 	}
7352 }
7353 
7354 
7355 #ifdef SATA_DEBUG
7356 
7357 /* Display 18 bytes of Inquiry data */
7358 static void
7359 sata_show_inqry_data(uint8_t *buf)
7360 {
7361 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
7362 	uint8_t *p;
7363 
7364 	cmn_err(CE_NOTE, "Inquiry data:");
7365 	cmn_err(CE_NOTE, "device type %x", inq->inq_dtype);
7366 	cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb);
7367 	cmn_err(CE_NOTE, "version %x", inq->inq_ansi);
7368 	cmn_err(CE_NOTE, "ATAPI transport version %d",
7369 	    SATA_ATAPI_TRANS_VERSION(inq));
7370 	cmn_err(CE_NOTE, "response data format %d, aenc %d",
7371 	    inq->inq_rdf, inq->inq_aenc);
7372 	cmn_err(CE_NOTE, " additional length %d", inq->inq_len);
7373 	cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs);
7374 	p = (uint8_t *)inq->inq_vid;
7375 	cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x "
7376 	    "%02x %02x %02x %02x",
7377 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
7378 	p = (uint8_t *)inq->inq_vid;
7379 	cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c",
7380 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
7381 
7382 	p = (uint8_t *)inq->inq_pid;
7383 	cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x "
7384 	    "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
7385 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7386 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7387 	p = (uint8_t *)inq->inq_pid;
7388 	cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c "
7389 	    "%c %c %c %c %c %c %c %c",
7390 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7391 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7392 
7393 	p = (uint8_t *)inq->inq_revision;
7394 	cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x",
7395 	    p[0], p[1], p[2], p[3]);
7396 	p = (uint8_t *)inq->inq_revision;
7397 	cmn_err(CE_NOTE, "revision: %c %c %c %c",
7398 	    p[0], p[1], p[2], p[3]);
7399 
7400 }
7401 
7402 
7403 static void
7404 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count)
7405 {
7406 	struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt;
7407 
7408 	if (scsi_pkt == NULL)
7409 		return;
7410 	if (count != 0) {
7411 		/* saving cdb */
7412 		bzero(sata_atapi_trace[sata_atapi_trace_index].acdb,
7413 		    SATA_ATAPI_MAX_CDB_LEN);
7414 		bcopy(scsi_pkt->pkt_cdbp,
7415 		    sata_atapi_trace[sata_atapi_trace_index].acdb, count);
7416 	} else {
7417 		bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)->
7418 		    sts_sensedata,
7419 		    sata_atapi_trace[sata_atapi_trace_index].arqs,
7420 		    SATA_ATAPI_MIN_RQSENSE_LEN);
7421 		sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason =
7422 		    scsi_pkt->pkt_reason;
7423 		sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason =
7424 		    spx->txlt_sata_pkt->satapkt_reason;
7425 
7426 		if (++sata_atapi_trace_index >= 64)
7427 			sata_atapi_trace_index = 0;
7428 	}
7429 }
7430 
7431 #endif
7432 
7433 /*
7434  * Fetch inquiry data from ATAPI device
7435  * Returns SATA_SUCCESS if operation was successfull, SATA_FAILURE otherwise.
7436  *
7437  * Note:
7438  * inqb pointer does not point to a DMA-able buffer. It is a local buffer
7439  * where the caller expects to see the inquiry data.
7440  *
7441  */
7442 
7443 static int
7444 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba,
7445     sata_address_t *saddr, struct scsi_inquiry *inq)
7446 {
7447 	sata_pkt_txlate_t *spx;
7448 	sata_pkt_t *spkt;
7449 	struct buf *bp;
7450 	sata_drive_info_t *sdinfo;
7451 	sata_cmd_t *scmd;
7452 	int rval;
7453 	uint8_t *rqsp;
7454 #ifdef SATA_DEBUG
7455 	char msg_buf[MAXPATHLEN];
7456 #endif
7457 
7458 	ASSERT(sata_hba != NULL);
7459 
7460 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
7461 	spx->txlt_sata_hba_inst = sata_hba;
7462 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
7463 	spkt = sata_pkt_alloc(spx, NULL);
7464 	if (spkt == NULL) {
7465 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7466 		return (SATA_FAILURE);
7467 	}
7468 	/* address is needed now */
7469 	spkt->satapkt_device.satadev_addr = *saddr;
7470 
7471 	/* scsi_inquiry size buffer */
7472 	bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry));
7473 	if (bp == NULL) {
7474 		sata_pkt_free(spx);
7475 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7476 		SATA_LOG_D((sata_hba, CE_WARN,
7477 		    "sata_get_atapi_inquiry_data: "
7478 		    "cannot allocate data buffer"));
7479 		return (SATA_FAILURE);
7480 	}
7481 	bp_mapin(bp); /* make data buffer accessible */
7482 
7483 	scmd = &spkt->satapkt_cmd;
7484 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7485 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7486 
7487 	/* Use synchronous mode */
7488 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7489 	spkt->satapkt_comp = NULL;
7490 	spkt->satapkt_time = sata_default_pkt_time;
7491 
7492 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
7493 
7494 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7495 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7496 
7497 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
7498 	sdinfo = sata_get_device_info(sata_hba,
7499 	    &spx->txlt_sata_pkt->satapkt_device);
7500 	if (sdinfo == NULL) {
7501 		/* we have to be carefull about the disapearing device */
7502 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7503 		rval = SATA_FAILURE;
7504 		goto cleanup;
7505 	}
7506 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7507 
7508 	/*
7509 	 * Set-up acdb. This works for atapi transport version 2 and later.
7510 	 */
7511 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7512 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7513 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
7514 	scmd->satacmd_acdb[1] = 0x00;
7515 	scmd->satacmd_acdb[2] = 0x00;
7516 	scmd->satacmd_acdb[3] = 0x00;
7517 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
7518 	scmd->satacmd_acdb[5] = 0x00;
7519 
7520 	sata_fixed_sense_data_preset(
7521 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7522 
7523 	/* Transfer command to HBA */
7524 	if (sata_hba_start(spx, &rval) != 0) {
7525 		/* Pkt not accepted for execution */
7526 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
7527 		    "sata_get_atapi_inquiry_data: "
7528 		    "Packet not accepted for execution - ret: %02x", rval);
7529 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7530 		rval = SATA_FAILURE;
7531 		goto cleanup;
7532 	}
7533 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7534 
7535 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
7536 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
7537 		    "sata_get_atapi_inquiry_data: "
7538 		    "Packet completed successfully - ret: %02x", rval);
7539 		/*
7540 		 * Sync buffer. Handle is in usual place in translate struct.
7541 		 * Normal completion - copy data into caller's buffer
7542 		 */
7543 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
7544 		    DDI_DMA_SYNC_FORCPU);
7545 		ASSERT(rval == DDI_SUCCESS);
7546 		bcopy(bp->b_un.b_addr, (uint8_t *)inq,
7547 		    sizeof (struct scsi_inquiry));
7548 #ifdef SATA_DEBUG
7549 		if (sata_debug_flags & SATA_DBG_ATAPI) {
7550 			sata_show_inqry_data((uint8_t *)inq);
7551 		}
7552 #endif
7553 		rval = SATA_SUCCESS;
7554 	} else {
7555 		/*
7556 		 * Something went wrong - analyze return - check rqsense data
7557 		 */
7558 		rval = SATA_FAILURE;
7559 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7560 			/*
7561 			 * ARQ data hopefull show something other than NO SENSE
7562 			 */
7563 			rqsp = scmd->satacmd_rqsense;
7564 #ifdef SATA_DEBUG
7565 			if (sata_debug_flags & SATA_DBG_ATAPI) {
7566 				msg_buf[0] = '\0';
7567 				(void) snprintf(msg_buf, MAXPATHLEN,
7568 				    "ATAPI packet completion reason: %02x\n"
7569 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x\n"
7570 				    "          %02x %02x %02x %02x %02x %02x\n"
7571 				    "          %02x %02x %02x %02x %02x %02x",
7572 				    spkt->satapkt_reason,
7573 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7574 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7575 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7576 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7577 				    rqsp[16], rqsp[17]);
7578 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7579 				    "%s", msg_buf);
7580 			}
7581 #endif
7582 		} else {
7583 			switch (spkt->satapkt_reason) {
7584 			case SATA_PKT_PORT_ERROR:
7585 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7586 				    "sata_get_atapi_inquiry_data: "
7587 				    "packet reason: port error", NULL);
7588 				break;
7589 
7590 			case SATA_PKT_TIMEOUT:
7591 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7592 				    "sata_get_atapi_inquiry_data: "
7593 				    "packet reason: timeout", NULL);
7594 				break;
7595 
7596 			case SATA_PKT_ABORTED:
7597 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7598 				    "sata_get_atapi_inquiry_data: "
7599 				    "packet reason: aborted", NULL);
7600 				break;
7601 
7602 			case SATA_PKT_RESET:
7603 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7604 				    "sata_get_atapi_inquiry_data: "
7605 				    "packet reason: reset\n", NULL);
7606 				break;
7607 			default:
7608 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7609 				    "sata_get_atapi_inquiry_data: "
7610 				    "invalid packet reason: %02x\n",
7611 				    spkt->satapkt_reason);
7612 				break;
7613 			}
7614 		}
7615 	}
7616 cleanup:
7617 	sata_free_local_buffer(spx);
7618 	sata_pkt_free(spx);
7619 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
7620 	return (rval);
7621 }
7622 
7623 
7624 
7625 
7626 
7627 #if 0
7628 #ifdef SATA_DEBUG
7629 
7630 /*
7631  * Test ATAPI packet command.
7632  * Single threaded test: send packet command in synch mode, process completion
7633  *
7634  */
7635 static void
7636 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport)
7637 {
7638 	sata_pkt_txlate_t *spx;
7639 	sata_pkt_t *spkt;
7640 	struct buf *bp;
7641 	sata_device_t sata_device;
7642 	sata_drive_info_t *sdinfo;
7643 	sata_cmd_t *scmd;
7644 	int rval;
7645 	uint8_t *rqsp;
7646 
7647 	ASSERT(sata_hba_inst != NULL);
7648 	sata_device.satadev_addr.cport = cport;
7649 	sata_device.satadev_addr.pmport = 0;
7650 	sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
7651 	sata_device.satadev_rev = SATA_DEVICE_REV;
7652 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7653 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
7654 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7655 	if (sdinfo == NULL) {
7656 		sata_log(sata_hba_inst, CE_WARN,
7657 		    "sata_test_atapi_packet_command: "
7658 		    "no device info for cport %d",
7659 		    sata_device.satadev_addr.cport);
7660 		return;
7661 	}
7662 
7663 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
7664 	spx->txlt_sata_hba_inst = sata_hba_inst;
7665 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
7666 	spkt = sata_pkt_alloc(spx, NULL);
7667 	if (spkt == NULL) {
7668 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7669 		return;
7670 	}
7671 	/* address is needed now */
7672 	spkt->satapkt_device.satadev_addr = sata_device.satadev_addr;
7673 
7674 	/* 1024k buffer */
7675 	bp = sata_alloc_local_buffer(spx, 1024);
7676 	if (bp == NULL) {
7677 		sata_pkt_free(spx);
7678 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7679 		sata_log(sata_hba_inst, CE_WARN,
7680 		    "sata_test_atapi_packet_command: "
7681 		    "cannot allocate data buffer");
7682 		return;
7683 	}
7684 	bp_mapin(bp); /* make data buffer accessible */
7685 
7686 	scmd = &spkt->satapkt_cmd;
7687 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7688 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7689 
7690 	/* Use synchronous mode */
7691 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7692 
7693 	/* Synchronous mode, no callback - may be changed by the caller */
7694 	spkt->satapkt_comp = NULL;
7695 	spkt->satapkt_time = sata_default_pkt_time;
7696 
7697 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
7698 
7699 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7700 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7701 
7702 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7703 
7704 	/* Set-up acdb. */
7705 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7706 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7707 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
7708 	scmd->satacmd_acdb[1] = 0x00;
7709 	scmd->satacmd_acdb[2] = 0x00;
7710 	scmd->satacmd_acdb[3] = 0x00;
7711 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
7712 	scmd->satacmd_acdb[5] = 0x00;
7713 
7714 	sata_fixed_sense_data_preset(
7715 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7716 
7717 	/* Transfer command to HBA */
7718 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7719 	if (sata_hba_start(spx, &rval) != 0) {
7720 		/* Pkt not accepted for execution */
7721 		sata_log(sata_hba_inst, CE_WARN,
7722 		    "sata_test_atapi_packet_command: "
7723 		    "Packet not accepted for execution - ret: %02x", rval);
7724 		mutex_exit(
7725 		    &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7726 		goto cleanup;
7727 	}
7728 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7729 
7730 	/*
7731 	 * Sync buffer. Handle is in usual place in translate struct.
7732 	 */
7733 	rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
7734 	    DDI_DMA_SYNC_FORCPU);
7735 	ASSERT(rval == DDI_SUCCESS);
7736 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
7737 		sata_log(sata_hba_inst, CE_WARN,
7738 		    "sata_test_atapi_packet_command: "
7739 		    "Packet completed successfully");
7740 		/*
7741 		 * Normal completion - show inquiry data
7742 		 */
7743 		sata_show_inqry_data((uint8_t *)bp->b_un.b_addr);
7744 	} else {
7745 		/*
7746 		 * Something went wrong - analyze return - check rqsense data
7747 		 */
7748 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7749 			/*
7750 			 * ARQ data hopefull show something other than NO SENSE
7751 			 */
7752 			rqsp = scmd->satacmd_rqsense;
7753 			sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7754 			    "ATAPI packet completion reason: %02x\n"
7755 			    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
7756 			    "          %02x %02x %02x %02x %02x %02x "
7757 			    "          %02x %02x %02x %02x %02x %02x\n",
7758 			    spkt->satapkt_reason,
7759 			    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7760 			    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7761 			    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7762 			    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7763 			    rqsp[16], rqsp[17]);
7764 		} else {
7765 			switch (spkt->satapkt_reason) {
7766 			case SATA_PKT_PORT_ERROR:
7767 				sata_log(sata_hba_inst, CE_WARN,
7768 				    "sata_test_atapi_packet_command: "
7769 				    "packet reason: port error\n");
7770 				break;
7771 
7772 			case SATA_PKT_TIMEOUT:
7773 				sata_log(sata_hba_inst, CE_WARN,
7774 				    "sata_test_atapi_packet_command: "
7775 				    "packet reason: timeout\n");
7776 				break;
7777 
7778 			case SATA_PKT_ABORTED:
7779 				sata_log(sata_hba_inst, CE_WARN,
7780 				    "sata_test_atapi_packet_command: "
7781 				    "packet reason: aborted\n");
7782 				break;
7783 
7784 			case SATA_PKT_RESET:
7785 				sata_log(sata_hba_inst, CE_WARN,
7786 				    "sata_test_atapi_packet_command: "
7787 				    "packet reason: reset\n");
7788 				break;
7789 			default:
7790 				sata_log(sata_hba_inst, CE_WARN,
7791 				    "sata_test_atapi_packet_command: "
7792 				    "invalid packet reason: %02x\n",
7793 				    spkt->satapkt_reason);
7794 				break;
7795 			}
7796 		}
7797 	}
7798 cleanup:
7799 	sata_free_local_buffer(spx);
7800 	sata_pkt_free(spx);
7801 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
7802 }
7803 
7804 #endif /* SATA_DEBUG */
7805 #endif /* 1 */
7806 
7807 
7808 /* ************************** LOCAL HELPER FUNCTIONS *********************** */
7809 
7810 /*
7811  * Validate sata_tran info
7812  * SATA_FAILURE returns if structure is inconsistent or structure revision
7813  * does not match one used by the framework.
7814  *
7815  * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains
7816  * required function pointers.
7817  * Returns SATA_FAILURE otherwise.
7818  */
7819 static int
7820 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran)
7821 {
7822 	/*
7823 	 * SATA_TRAN_HBA_REV is the current (highest) revision number
7824 	 * of the SATA interface.
7825 	 */
7826 	if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) {
7827 		sata_log(NULL, CE_WARN,
7828 		    "sata: invalid sata_hba_tran version %d for driver %s",
7829 		    sata_tran->sata_tran_hba_rev, ddi_driver_name(dip));
7830 		return (SATA_FAILURE);
7831 	}
7832 
7833 	if (dip != sata_tran->sata_tran_hba_dip) {
7834 		SATA_LOG_D((NULL, CE_WARN,
7835 		    "sata: inconsistent sata_tran_hba_dip "
7836 		    "%p / %p", sata_tran->sata_tran_hba_dip, dip));
7837 		return (SATA_FAILURE);
7838 	}
7839 
7840 	if (sata_tran->sata_tran_probe_port == NULL ||
7841 	    sata_tran->sata_tran_start == NULL ||
7842 	    sata_tran->sata_tran_abort == NULL ||
7843 	    sata_tran->sata_tran_reset_dport == NULL ||
7844 	    sata_tran->sata_tran_hotplug_ops == NULL ||
7845 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL ||
7846 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate ==
7847 	    NULL) {
7848 		SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing "
7849 		    "required functions"));
7850 	}
7851 	return (SATA_SUCCESS);
7852 }
7853 
7854 /*
7855  * Remove HBA instance from sata_hba_list.
7856  */
7857 static void
7858 sata_remove_hba_instance(dev_info_t *dip)
7859 {
7860 	sata_hba_inst_t	*sata_hba_inst;
7861 
7862 	mutex_enter(&sata_mutex);
7863 	for (sata_hba_inst = sata_hba_list;
7864 	    sata_hba_inst != (struct sata_hba_inst *)NULL;
7865 	    sata_hba_inst = sata_hba_inst->satahba_next) {
7866 		if (sata_hba_inst->satahba_dip == dip)
7867 			break;
7868 	}
7869 
7870 	if (sata_hba_inst == (struct sata_hba_inst *)NULL) {
7871 #ifdef SATA_DEBUG
7872 		cmn_err(CE_WARN, "sata_remove_hba_instance: "
7873 		    "unknown HBA instance\n");
7874 #endif
7875 		ASSERT(FALSE);
7876 	}
7877 	if (sata_hba_inst == sata_hba_list) {
7878 		sata_hba_list = sata_hba_inst->satahba_next;
7879 		if (sata_hba_list) {
7880 			sata_hba_list->satahba_prev =
7881 			    (struct sata_hba_inst *)NULL;
7882 		}
7883 		if (sata_hba_inst == sata_hba_list_tail) {
7884 			sata_hba_list_tail = NULL;
7885 		}
7886 	} else if (sata_hba_inst == sata_hba_list_tail) {
7887 		sata_hba_list_tail = sata_hba_inst->satahba_prev;
7888 		if (sata_hba_list_tail) {
7889 			sata_hba_list_tail->satahba_next =
7890 			    (struct sata_hba_inst *)NULL;
7891 		}
7892 	} else {
7893 		sata_hba_inst->satahba_prev->satahba_next =
7894 		    sata_hba_inst->satahba_next;
7895 		sata_hba_inst->satahba_next->satahba_prev =
7896 		    sata_hba_inst->satahba_prev;
7897 	}
7898 	mutex_exit(&sata_mutex);
7899 }
7900 
7901 
7902 
7903 
7904 
7905 /*
7906  * Probe all SATA ports of the specified HBA instance.
7907  * The assumption is that there are no target and attachment point minor nodes
7908  * created by the boot subsystems, so we do not need to prune device tree.
7909  *
7910  * This function is called only from sata_hba_attach(). It does not have to
7911  * be protected by controller mutex, because the hba_attached flag is not set
7912  * yet and no one would be touching this HBA instance other than this thread.
7913  * Determines if port is active and what type of the device is attached
7914  * (if any). Allocates necessary structures for each port.
7915  *
7916  * An AP (Attachement Point) node is created for each SATA device port even
7917  * when there is no device attached.
7918  */
7919 
7920 static 	void
7921 sata_probe_ports(sata_hba_inst_t *sata_hba_inst)
7922 {
7923 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
7924 	int			ncport, npmport;
7925 	sata_cport_info_t 	*cportinfo;
7926 	sata_drive_info_t	*drive;
7927 	sata_pmult_info_t	*pminfo;
7928 	sata_pmport_info_t 	*pmportinfo;
7929 	sata_device_t		sata_device;
7930 	int			rval;
7931 	dev_t			minor_number;
7932 	char			name[16];
7933 	clock_t			start_time, cur_time;
7934 
7935 	/*
7936 	 * Probe controller ports first, to find port status and
7937 	 * any port multiplier attached.
7938 	 */
7939 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
7940 		/* allocate cport structure */
7941 		cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP);
7942 		ASSERT(cportinfo != NULL);
7943 		mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL);
7944 
7945 		mutex_enter(&cportinfo->cport_mutex);
7946 
7947 		cportinfo->cport_addr.cport = ncport;
7948 		cportinfo->cport_addr.pmport = 0;
7949 		cportinfo->cport_addr.qual = SATA_ADDR_CPORT;
7950 		cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
7951 		cportinfo->cport_state |= SATA_STATE_PROBING;
7952 		SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo;
7953 
7954 		/*
7955 		 * Regardless if a port is usable or not, create
7956 		 * an attachment point
7957 		 */
7958 		mutex_exit(&cportinfo->cport_mutex);
7959 		minor_number =
7960 		    SATA_MAKE_AP_MINOR(ddi_get_instance(dip), ncport, 0, 0);
7961 		(void) sprintf(name, "%d", ncport);
7962 		if (ddi_create_minor_node(dip, name, S_IFCHR,
7963 		    minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) !=
7964 		    DDI_SUCCESS) {
7965 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
7966 			    "cannot create SATA attachment point for port %d",
7967 			    ncport);
7968 		}
7969 
7970 		/* Probe port */
7971 		start_time = ddi_get_lbolt();
7972 	reprobe_cport:
7973 		sata_device.satadev_addr.cport = ncport;
7974 		sata_device.satadev_addr.pmport = 0;
7975 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
7976 		sata_device.satadev_rev = SATA_DEVICE_REV;
7977 
7978 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
7979 		    (dip, &sata_device);
7980 
7981 		mutex_enter(&cportinfo->cport_mutex);
7982 		sata_update_port_scr(&cportinfo->cport_scr, &sata_device);
7983 		if (rval != SATA_SUCCESS) {
7984 			/* Something went wrong? Fail the port */
7985 			cportinfo->cport_state = SATA_PSTATE_FAILED;
7986 			mutex_exit(&cportinfo->cport_mutex);
7987 			continue;
7988 		}
7989 		cportinfo->cport_state &= ~SATA_STATE_PROBING;
7990 		cportinfo->cport_state |= SATA_STATE_PROBED;
7991 		cportinfo->cport_dev_type = sata_device.satadev_type;
7992 
7993 		cportinfo->cport_state |= SATA_STATE_READY;
7994 		if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) {
7995 			mutex_exit(&cportinfo->cport_mutex);
7996 			continue;
7997 		}
7998 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
7999 			/*
8000 			 * There is some device attached.
8001 			 * Allocate device info structure
8002 			 */
8003 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) {
8004 				mutex_exit(&cportinfo->cport_mutex);
8005 				SATA_CPORTINFO_DRV_INFO(cportinfo) =
8006 				    kmem_zalloc(sizeof (sata_drive_info_t),
8007 				    KM_SLEEP);
8008 				mutex_enter(&cportinfo->cport_mutex);
8009 			}
8010 			drive = SATA_CPORTINFO_DRV_INFO(cportinfo);
8011 			drive->satadrv_addr = cportinfo->cport_addr;
8012 			drive->satadrv_addr.qual = SATA_ADDR_DCPORT;
8013 			drive->satadrv_type = cportinfo->cport_dev_type;
8014 			drive->satadrv_state = SATA_STATE_UNKNOWN;
8015 
8016 			mutex_exit(&cportinfo->cport_mutex);
8017 			if (sata_add_device(dip, sata_hba_inst, ncport, 0) !=
8018 			    SATA_SUCCESS) {
8019 				/*
8020 				 * Plugged device was not correctly identified.
8021 				 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT
8022 				 */
8023 				cur_time = ddi_get_lbolt();
8024 				if ((cur_time - start_time) <
8025 				    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
8026 					/* sleep for a while */
8027 					delay(drv_usectohz(
8028 					    SATA_DEV_RETRY_DLY));
8029 					goto reprobe_cport;
8030 				}
8031 			}
8032 		} else {
8033 			mutex_exit(&cportinfo->cport_mutex);
8034 			ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT);
8035 			pminfo = kmem_zalloc(sizeof (sata_pmult_info_t),
8036 			    KM_SLEEP);
8037 			mutex_enter(&cportinfo->cport_mutex);
8038 			ASSERT(pminfo != NULL);
8039 			SATA_CPORTINFO_PMULT_INFO(cportinfo) = pminfo;
8040 			pminfo->pmult_addr.cport = cportinfo->cport_addr.cport;
8041 			pminfo->pmult_addr.pmport = SATA_PMULT_HOSTPORT;
8042 			pminfo->pmult_addr.qual = SATA_ADDR_PMPORT;
8043 			pminfo->pmult_num_dev_ports =
8044 			    sata_device.satadev_add_info;
8045 			mutex_init(&pminfo->pmult_mutex, NULL, MUTEX_DRIVER,
8046 			    NULL);
8047 			pminfo->pmult_state = SATA_STATE_PROBING;
8048 			mutex_exit(&cportinfo->cport_mutex);
8049 
8050 			/* Probe Port Multiplier ports */
8051 			for (npmport = 0;
8052 			    npmport < pminfo->pmult_num_dev_ports;
8053 			    npmport++) {
8054 				pmportinfo = kmem_zalloc(
8055 				    sizeof (sata_pmport_info_t), KM_SLEEP);
8056 				mutex_enter(&cportinfo->cport_mutex);
8057 				ASSERT(pmportinfo != NULL);
8058 				pmportinfo->pmport_addr.cport = ncport;
8059 				pmportinfo->pmport_addr.pmport = npmport;
8060 				pmportinfo->pmport_addr.qual =
8061 				    SATA_ADDR_PMPORT;
8062 				pminfo->pmult_dev_port[npmport] = pmportinfo;
8063 
8064 				mutex_init(&pmportinfo->pmport_mutex, NULL,
8065 				    MUTEX_DRIVER, NULL);
8066 
8067 				mutex_exit(&cportinfo->cport_mutex);
8068 
8069 				/* Create an attachment point */
8070 				minor_number = SATA_MAKE_AP_MINOR(
8071 				    ddi_get_instance(dip), ncport, npmport, 1);
8072 				(void) sprintf(name, "%d.%d", ncport, npmport);
8073 				if (ddi_create_minor_node(dip, name, S_IFCHR,
8074 				    minor_number, DDI_NT_SATA_ATTACHMENT_POINT,
8075 				    0) != DDI_SUCCESS) {
8076 					sata_log(sata_hba_inst, CE_WARN,
8077 					    "sata_hba_attach: "
8078 					    "cannot create SATA attachment "
8079 					    "point for port %d pmult port %d",
8080 					    ncport, npmport);
8081 				}
8082 
8083 				start_time = ddi_get_lbolt();
8084 			reprobe_pmport:
8085 				sata_device.satadev_addr.pmport = npmport;
8086 				sata_device.satadev_addr.qual =
8087 				    SATA_ADDR_PMPORT;
8088 
8089 				rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8090 				    (dip, &sata_device);
8091 				mutex_enter(&cportinfo->cport_mutex);
8092 
8093 				/* sata_update_port_info() */
8094 				sata_update_port_scr(&pmportinfo->pmport_scr,
8095 				    &sata_device);
8096 
8097 				if (rval != SATA_SUCCESS) {
8098 					pmportinfo->pmport_state =
8099 					    SATA_PSTATE_FAILED;
8100 					mutex_exit(&cportinfo->cport_mutex);
8101 					continue;
8102 				}
8103 				pmportinfo->pmport_state &=
8104 				    ~SATA_STATE_PROBING;
8105 				pmportinfo->pmport_state |= SATA_STATE_PROBED;
8106 				pmportinfo->pmport_dev_type =
8107 				    sata_device.satadev_type;
8108 
8109 				pmportinfo->pmport_state |= SATA_STATE_READY;
8110 				if (pmportinfo->pmport_dev_type ==
8111 				    SATA_DTYPE_NONE) {
8112 					mutex_exit(&cportinfo->cport_mutex);
8113 					continue;
8114 				}
8115 				/* Port multipliers cannot be chained */
8116 				ASSERT(pmportinfo->pmport_dev_type !=
8117 				    SATA_DTYPE_PMULT);
8118 				/*
8119 				 * There is something attached to Port
8120 				 * Multiplier device port
8121 				 * Allocate device info structure
8122 				 */
8123 				if (pmportinfo->pmport_sata_drive == NULL) {
8124 					mutex_exit(&cportinfo->cport_mutex);
8125 					pmportinfo->pmport_sata_drive =
8126 					    kmem_zalloc(
8127 					    sizeof (sata_drive_info_t),
8128 					    KM_SLEEP);
8129 					mutex_enter(&cportinfo->cport_mutex);
8130 				}
8131 				drive = pmportinfo->pmport_sata_drive;
8132 				drive->satadrv_addr.cport =
8133 				    pmportinfo->pmport_addr.cport;
8134 				drive->satadrv_addr.pmport = npmport;
8135 				drive->satadrv_addr.qual = SATA_ADDR_DPMPORT;
8136 				drive->satadrv_type = pmportinfo->
8137 				    pmport_dev_type;
8138 				drive->satadrv_state = SATA_STATE_UNKNOWN;
8139 
8140 				mutex_exit(&cportinfo->cport_mutex);
8141 				if (sata_add_device(dip, sata_hba_inst, ncport,
8142 				    npmport) != SATA_SUCCESS) {
8143 					/*
8144 					 * Plugged device was not correctly
8145 					 * identified. Retry, within the
8146 					 * SATA_DEV_IDENTIFY_TIMEOUT
8147 					 */
8148 					cur_time = ddi_get_lbolt();
8149 					if ((cur_time - start_time) <
8150 					    drv_usectohz(
8151 					    SATA_DEV_IDENTIFY_TIMEOUT)) {
8152 						/* sleep for a while */
8153 						delay(drv_usectohz(
8154 						    SATA_DEV_RETRY_DLY));
8155 						goto reprobe_pmport;
8156 					}
8157 				}
8158 			}
8159 			pmportinfo->pmport_state =
8160 			    SATA_STATE_PROBED | SATA_STATE_READY;
8161 		}
8162 	}
8163 }
8164 
8165 /*
8166  * Add SATA device for specified HBA instance & port (SCSI target
8167  * device nodes).
8168  * This function is called (indirectly) only from sata_hba_attach().
8169  * A target node is created when there is a supported type device attached,
8170  * but may be removed if it cannot be put online.
8171  *
8172  * This function cannot be called from an interrupt context.
8173  *
8174  * ONLY DISK TARGET NODES ARE CREATED NOW
8175  *
8176  * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when
8177  * device identification failed - adding a device could be retried.
8178  *
8179  */
8180 static 	int
8181 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst, int cport,
8182     int pmport)
8183 {
8184 	sata_cport_info_t 	*cportinfo;
8185 	sata_pmult_info_t	*pminfo;
8186 	sata_pmport_info_t	*pmportinfo;
8187 	dev_info_t		*cdip;		/* child dip */
8188 	sata_device_t		sata_device;
8189 	int			rval;
8190 
8191 
8192 
8193 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
8194 	ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE);
8195 	mutex_enter(&cportinfo->cport_mutex);
8196 	/*
8197 	 * Some device is attached to a controller port.
8198 	 * We rely on controllers distinquishing between no-device,
8199 	 * attached port multiplier and other kind of attached device.
8200 	 * We need to get Identify Device data and determine
8201 	 * positively the dev type before trying to attach
8202 	 * the target driver.
8203 	 */
8204 	sata_device.satadev_rev = SATA_DEVICE_REV;
8205 	if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
8206 		/*
8207 		 * Not port multiplier.
8208 		 */
8209 		sata_device.satadev_addr = cportinfo->cport_addr;
8210 		sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
8211 		mutex_exit(&cportinfo->cport_mutex);
8212 
8213 		rval = sata_probe_device(sata_hba_inst, &sata_device);
8214 		if (rval != SATA_SUCCESS ||
8215 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN)
8216 			return (SATA_FAILURE);
8217 
8218 		mutex_enter(&cportinfo->cport_mutex);
8219 		sata_show_drive_info(sata_hba_inst,
8220 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
8221 
8222 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
8223 			/*
8224 			 * Could not determine device type or
8225 			 * a device is not supported.
8226 			 * Degrade this device to unknown.
8227 			 */
8228 			cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8229 			mutex_exit(&cportinfo->cport_mutex);
8230 			return (SATA_SUCCESS);
8231 		}
8232 		cportinfo->cport_dev_type = sata_device.satadev_type;
8233 		cportinfo->cport_tgtnode_clean = B_TRUE;
8234 		mutex_exit(&cportinfo->cport_mutex);
8235 
8236 		/*
8237 		 * Initialize device to the desired state. Even if it
8238 		 * fails, the device will still attach but syslog
8239 		 * will show the warning.
8240 		 */
8241 		if (sata_initialize_device(sata_hba_inst,
8242 		    SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS)
8243 			/* Retry */
8244 			(void) sata_initialize_device(sata_hba_inst,
8245 			    SATA_CPORTINFO_DRV_INFO(cportinfo));
8246 
8247 		cdip = sata_create_target_node(pdip, sata_hba_inst,
8248 		    &sata_device.satadev_addr);
8249 		mutex_enter(&cportinfo->cport_mutex);
8250 		if (cdip == NULL) {
8251 			/*
8252 			 * Attaching target node failed.
8253 			 * We retain sata_drive_info structure...
8254 			 */
8255 			mutex_exit(&cportinfo->cport_mutex);
8256 			return (SATA_SUCCESS);
8257 		}
8258 		(SATA_CPORTINFO_DRV_INFO(cportinfo))->
8259 		    satadrv_state = SATA_STATE_READY;
8260 	} else {
8261 		/* This must be Port Multiplier type */
8262 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
8263 			SATA_LOG_D((sata_hba_inst, CE_WARN,
8264 			    "sata_add_device: "
8265 			    "unrecognized dev type %x",
8266 			    cportinfo->cport_dev_type));
8267 			mutex_exit(&cportinfo->cport_mutex);
8268 			return (SATA_SUCCESS);
8269 		}
8270 		pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
8271 		pmportinfo = pminfo->pmult_dev_port[pmport];
8272 		sata_device.satadev_addr = pmportinfo->pmport_addr;
8273 		sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
8274 		mutex_exit(&cportinfo->cport_mutex);
8275 
8276 		rval = sata_probe_device(sata_hba_inst, &sata_device);
8277 		if (rval != SATA_SUCCESS ||
8278 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN) {
8279 			return (SATA_FAILURE);
8280 		}
8281 		mutex_enter(&cportinfo->cport_mutex);
8282 		sata_show_drive_info(sata_hba_inst,
8283 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
8284 
8285 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
8286 			/*
8287 			 * Could not determine device type.
8288 			 * Degrade this device to unknown.
8289 			 */
8290 			pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
8291 			mutex_exit(&cportinfo->cport_mutex);
8292 			return (SATA_SUCCESS);
8293 		}
8294 		pmportinfo->pmport_dev_type = sata_device.satadev_type;
8295 		pmportinfo->pmport_tgtnode_clean = B_TRUE;
8296 		mutex_exit(&cportinfo->cport_mutex);
8297 
8298 		/*
8299 		 * Initialize device to the desired state.
8300 		 * Even if it fails, the device will still
8301 		 * attach but syslog will show the warning.
8302 		 */
8303 		if (sata_initialize_device(sata_hba_inst,
8304 		    pmportinfo->pmport_sata_drive) != SATA_SUCCESS)
8305 			/* Retry */
8306 			(void) sata_initialize_device(sata_hba_inst,
8307 			    pmportinfo->pmport_sata_drive);
8308 
8309 		cdip = sata_create_target_node(pdip, sata_hba_inst,
8310 		    &sata_device.satadev_addr);
8311 		mutex_enter(&cportinfo->cport_mutex);
8312 		if (cdip == NULL) {
8313 			/*
8314 			 * Attaching target node failed.
8315 			 * We retain sata_drive_info structure...
8316 			 */
8317 			mutex_exit(&cportinfo->cport_mutex);
8318 			return (SATA_SUCCESS);
8319 		}
8320 		pmportinfo->pmport_sata_drive->satadrv_state |=
8321 		    SATA_STATE_READY;
8322 	}
8323 	mutex_exit(&cportinfo->cport_mutex);
8324 	return (SATA_SUCCESS);
8325 }
8326 
8327 
8328 
8329 /*
8330  * Create scsi target node for attached device, create node properties and
8331  * attach the node.
8332  * The node could be removed if the device onlining fails.
8333  *
8334  * A dev_info_t pointer is returned if operation is successful, NULL is
8335  * returned otherwise.
8336  *
8337  * No port multiplier support.
8338  */
8339 
8340 static dev_info_t *
8341 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst,
8342 			sata_address_t *sata_addr)
8343 {
8344 	dev_info_t *cdip = NULL;
8345 	int rval;
8346 	char *nname = NULL;
8347 	char **compatible = NULL;
8348 	int ncompatible;
8349 	struct scsi_inquiry inq;
8350 	sata_device_t sata_device;
8351 	sata_drive_info_t *sdinfo;
8352 	int target;
8353 	int i;
8354 
8355 	sata_device.satadev_rev = SATA_DEVICE_REV;
8356 	sata_device.satadev_addr = *sata_addr;
8357 
8358 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport)));
8359 
8360 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
8361 
8362 	target = SATA_TO_SCSI_TARGET(sata_addr->cport,
8363 	    sata_addr->pmport, sata_addr->qual);
8364 
8365 	if (sdinfo == NULL) {
8366 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8367 		    sata_addr->cport)));
8368 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8369 		    "sata_create_target_node: no sdinfo for target %x",
8370 		    target));
8371 		return (NULL);
8372 	}
8373 
8374 	/*
8375 	 * create or get scsi inquiry data, expected by
8376 	 * scsi_hba_nodename_compatible_get()
8377 	 * SATA hard disks get Identify Data translated into Inguiry Data.
8378 	 * ATAPI devices respond directly to Inquiry request.
8379 	 */
8380 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
8381 		sata_identdev_to_inquiry(sata_hba_inst, sdinfo,
8382 		    (uint8_t *)&inq);
8383 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8384 		    sata_addr->cport)));
8385 	} else { /* Assume supported ATAPI device */
8386 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8387 		    sata_addr->cport)));
8388 		if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr,
8389 		    &inq) == SATA_FAILURE)
8390 			return (NULL);
8391 		/*
8392 		 * Save supported ATAPI transport version
8393 		 */
8394 		sdinfo->satadrv_atapi_trans_ver =
8395 		    SATA_ATAPI_TRANS_VERSION(&inq);
8396 	}
8397 
8398 	/* determine the node name and compatible */
8399 	scsi_hba_nodename_compatible_get(&inq, NULL,
8400 	    inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
8401 
8402 #ifdef SATA_DEBUG
8403 	if (sata_debug_flags & SATA_DBG_NODES) {
8404 		if (nname == NULL) {
8405 			cmn_err(CE_NOTE, "sata_create_target_node: "
8406 			    "cannot determine nodename for target %d\n",
8407 			    target);
8408 		} else {
8409 			cmn_err(CE_WARN, "sata_create_target_node: "
8410 			    "target %d nodename: %s\n", target, nname);
8411 		}
8412 		if (compatible == NULL) {
8413 			cmn_err(CE_WARN,
8414 			    "sata_create_target_node: no compatible name\n");
8415 		} else {
8416 			for (i = 0; i < ncompatible; i++) {
8417 				cmn_err(CE_WARN, "sata_create_target_node: "
8418 				    "compatible name: %s\n", compatible[i]);
8419 			}
8420 		}
8421 	}
8422 #endif
8423 
8424 	/* if nodename can't be determined, log error and exit */
8425 	if (nname == NULL) {
8426 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8427 		    "sata_create_target_node: cannot determine nodename "
8428 		    "for target %d\n", target));
8429 		scsi_hba_nodename_compatible_free(nname, compatible);
8430 		return (NULL);
8431 	}
8432 	/*
8433 	 * Create scsi target node
8434 	 */
8435 	ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip);
8436 	rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
8437 	    "device-type", "scsi");
8438 
8439 	if (rval != DDI_PROP_SUCCESS) {
8440 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8441 		    "updating device_type prop failed %d", rval));
8442 		goto fail;
8443 	}
8444 
8445 	/*
8446 	 * Create target node properties: target & lun
8447 	 */
8448 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target);
8449 	if (rval != DDI_PROP_SUCCESS) {
8450 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8451 		    "updating target prop failed %d", rval));
8452 		goto fail;
8453 	}
8454 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0);
8455 	if (rval != DDI_PROP_SUCCESS) {
8456 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8457 		    "updating target prop failed %d", rval));
8458 		goto fail;
8459 	}
8460 
8461 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
8462 		/*
8463 		 * Add "variant" property
8464 		 */
8465 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
8466 		    "variant", "atapi");
8467 		if (rval != DDI_PROP_SUCCESS) {
8468 			SATA_LOG_D((sata_hba_inst, CE_WARN,
8469 			    "sata_create_target_node: variant atapi "
8470 			    "property could not be created: %d", rval));
8471 			goto fail;
8472 		}
8473 	}
8474 	/* decorate the node with compatible */
8475 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible",
8476 	    compatible, ncompatible) != DDI_PROP_SUCCESS) {
8477 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8478 		    "sata_create_target_node: FAIL compatible props cdip 0x%p",
8479 		    (void *)cdip));
8480 		goto fail;
8481 	}
8482 
8483 
8484 	/*
8485 	 * Now, try to attach the driver. If probing of the device fails,
8486 	 * the target node may be removed
8487 	 */
8488 	rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH);
8489 
8490 	scsi_hba_nodename_compatible_free(nname, compatible);
8491 
8492 	if (rval == NDI_SUCCESS)
8493 		return (cdip);
8494 
8495 	/* target node was removed - are we sure? */
8496 	return (NULL);
8497 
8498 fail:
8499 	scsi_hba_nodename_compatible_free(nname, compatible);
8500 	ddi_prop_remove_all(cdip);
8501 	rval = ndi_devi_free(cdip);
8502 	if (rval != NDI_SUCCESS) {
8503 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8504 		    "node removal failed %d", rval));
8505 	}
8506 	sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: "
8507 	    "cannot create target node for SATA device at port %d",
8508 	    sata_addr->cport);
8509 	return (NULL);
8510 }
8511 
8512 
8513 
8514 /*
8515  * Re-probe sata port, check for a device and attach info
8516  * structures when necessary. Identify Device data is fetched, if possible.
8517  * Assumption: sata address is already validated.
8518  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
8519  * the presence of a device and its type.
8520  *
8521  * flag arg specifies that the function should try multiple times to identify
8522  * device type and to initialize it, or it should return immediately on failure.
8523  * SATA_DEV_IDENTIFY_RETRY - retry
8524  * SATA_DEV_IDENTIFY_NORETRY - no retry
8525  *
8526  * SATA_FAILURE is returned if one of the operations failed.
8527  *
8528  * This function cannot be called in interrupt context - it may sleep.
8529  *
8530  * NOte: Port multiplier is not supported yet, although there may be some
8531  * pieces of code referencing to it.
8532  */
8533 static int
8534 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
8535     int flag)
8536 {
8537 	sata_cport_info_t *cportinfo;
8538 	sata_drive_info_t *sdinfo, *osdinfo;
8539 	boolean_t init_device = B_FALSE;
8540 	int prev_device_type = SATA_DTYPE_NONE;
8541 	int prev_device_settings = 0;
8542 	int prev_device_state = 0;
8543 	clock_t start_time;
8544 	int retry = B_FALSE;
8545 	int rval;
8546 
8547 	/* We only care about host sata cport for now */
8548 	cportinfo = SATA_CPORT_INFO(sata_hba_inst,
8549 	    sata_device->satadev_addr.cport);
8550 	osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8551 	if (osdinfo != NULL) {
8552 		/*
8553 		 * We are re-probing port with a previously attached device.
8554 		 * Save previous device type and settings.
8555 		 */
8556 		prev_device_type = cportinfo->cport_dev_type;
8557 		prev_device_settings = osdinfo->satadrv_settings;
8558 		prev_device_state = osdinfo->satadrv_state;
8559 	}
8560 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
8561 		start_time = ddi_get_lbolt();
8562 		retry = B_TRUE;
8563 	}
8564 retry_probe:
8565 
8566 	/* probe port */
8567 	mutex_enter(&cportinfo->cport_mutex);
8568 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
8569 	cportinfo->cport_state |= SATA_STATE_PROBING;
8570 	mutex_exit(&cportinfo->cport_mutex);
8571 
8572 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8573 	    (SATA_DIP(sata_hba_inst), sata_device);
8574 
8575 	mutex_enter(&cportinfo->cport_mutex);
8576 	if (rval != SATA_SUCCESS) {
8577 		cportinfo->cport_state = SATA_PSTATE_FAILED;
8578 		mutex_exit(&cportinfo->cport_mutex);
8579 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: "
8580 		    "SATA port %d probing failed",
8581 		    cportinfo->cport_addr.cport));
8582 		return (SATA_FAILURE);
8583 	}
8584 
8585 	/*
8586 	 * update sata port state and set device type
8587 	 */
8588 	sata_update_port_info(sata_hba_inst, sata_device);
8589 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
8590 
8591 	/*
8592 	 * Sanity check - Port is active? Is the link active?
8593 	 * Is there any device attached?
8594 	 */
8595 	if ((cportinfo->cport_state &
8596 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
8597 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
8598 	    SATA_PORT_DEVLINK_UP) {
8599 		/*
8600 		 * Port in non-usable state or no link active/no device.
8601 		 * Free info structure if necessary (direct attached drive
8602 		 * only, for now!
8603 		 */
8604 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8605 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8606 		/* Add here differentiation for device attached or not */
8607 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8608 		mutex_exit(&cportinfo->cport_mutex);
8609 		if (sdinfo != NULL)
8610 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8611 		return (SATA_SUCCESS);
8612 	}
8613 
8614 	cportinfo->cport_state |= SATA_STATE_READY;
8615 	cportinfo->cport_dev_type = sata_device->satadev_type;
8616 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8617 
8618 	/*
8619 	 * If we are re-probing the port, there may be
8620 	 * sata_drive_info structure attached
8621 	 * (or sata_pm_info, if PMult is supported).
8622 	 */
8623 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
8624 		/*
8625 		 * There is no device, so remove device info structure,
8626 		 * if necessary.
8627 		 * Only direct attached drive is considered now, until
8628 		 * port multiplier is supported. If the previously
8629 		 * attached device was a port multiplier, we would need
8630 		 * to take care of devices attached beyond the port
8631 		 * multiplier.
8632 		 */
8633 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8634 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8635 		if (sdinfo != NULL) {
8636 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8637 			sata_log(sata_hba_inst, CE_WARN,
8638 			    "SATA device detached "
8639 			    "from port %d", cportinfo->cport_addr.cport);
8640 		}
8641 		mutex_exit(&cportinfo->cport_mutex);
8642 		return (SATA_SUCCESS);
8643 	}
8644 
8645 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
8646 		if (sdinfo == NULL) {
8647 			/*
8648 			 * There is some device attached, but there is
8649 			 * no sata_drive_info structure - allocate one
8650 			 */
8651 			mutex_exit(&cportinfo->cport_mutex);
8652 			sdinfo = kmem_zalloc(
8653 			    sizeof (sata_drive_info_t), KM_SLEEP);
8654 			mutex_enter(&cportinfo->cport_mutex);
8655 			/*
8656 			 * Recheck, that the port state did not change when we
8657 			 * released mutex.
8658 			 */
8659 			if (cportinfo->cport_state & SATA_STATE_READY) {
8660 				SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo;
8661 				sdinfo->satadrv_addr = cportinfo->cport_addr;
8662 				sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT;
8663 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
8664 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
8665 			} else {
8666 				/*
8667 				 * Port is not in ready state, we
8668 				 * cannot attach a device.
8669 				 */
8670 				mutex_exit(&cportinfo->cport_mutex);
8671 				kmem_free(sdinfo, sizeof (sata_drive_info_t));
8672 				return (SATA_SUCCESS);
8673 			}
8674 			/*
8675 			 * Since we are adding device, presumably new one,
8676 			 * indicate that it  should be initalized,
8677 			 * as well as some internal framework states).
8678 			 */
8679 			init_device = B_TRUE;
8680 		}
8681 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8682 		sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
8683 	} else {
8684 		/*
8685 		 * The device is a port multiplier - not handled now.
8686 		 */
8687 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8688 		mutex_exit(&cportinfo->cport_mutex);
8689 		return (SATA_SUCCESS);
8690 	}
8691 	mutex_exit(&cportinfo->cport_mutex);
8692 	/*
8693 	 * Figure out what kind of device we are really
8694 	 * dealing with.
8695 	 */
8696 	rval = sata_probe_device(sata_hba_inst, sata_device);
8697 
8698 	mutex_enter(&cportinfo->cport_mutex);
8699 	if (rval == SATA_SUCCESS) {
8700 		/*
8701 		 * If we are dealing with the same type of a device as before,
8702 		 * restore its settings flags.
8703 		 */
8704 		if (osdinfo != NULL &&
8705 		    sata_device->satadev_type == prev_device_type)
8706 			sdinfo->satadrv_settings = prev_device_settings;
8707 
8708 		mutex_exit(&cportinfo->cport_mutex);
8709 		/* Set initial device features, if necessary */
8710 		if (init_device == B_TRUE) {
8711 			rval = sata_initialize_device(sata_hba_inst, sdinfo);
8712 		}
8713 		if (rval == SATA_SUCCESS)
8714 			return (rval);
8715 	} else {
8716 		/*
8717 		 * If there was some device info before we probe the device,
8718 		 * restore previous device setting, so we can retry from scratch
8719 		 * later. Providing, of course, that device has not disapear
8720 		 * during probing process.
8721 		 */
8722 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
8723 			if (osdinfo != NULL) {
8724 				cportinfo->cport_dev_type = prev_device_type;
8725 				sdinfo->satadrv_type = prev_device_type;
8726 				sdinfo->satadrv_state = prev_device_state;
8727 			}
8728 		} else {
8729 			/* device is gone */
8730 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8731 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8732 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8733 			mutex_exit(&cportinfo->cport_mutex);
8734 			return (SATA_SUCCESS);
8735 		}
8736 		mutex_exit(&cportinfo->cport_mutex);
8737 	}
8738 
8739 	if (retry) {
8740 		clock_t cur_time = ddi_get_lbolt();
8741 		/*
8742 		 * A device was not successfully identified or initialized.
8743 		 * Track retry time for device identification.
8744 		 */
8745 		if ((cur_time - start_time) <
8746 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
8747 			/* sleep for a while */
8748 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
8749 			goto retry_probe;
8750 		} else {
8751 			mutex_enter(&cportinfo->cport_mutex);
8752 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL)
8753 				SATA_CPORTINFO_DRV_INFO(cportinfo)->
8754 				    satadrv_state = SATA_DSTATE_FAILED;
8755 			mutex_exit(&cportinfo->cport_mutex);
8756 		}
8757 	}
8758 	return (SATA_SUCCESS);
8759 }
8760 
8761 /*
8762  * Initialize device
8763  * Specified device is initialized to a default state.
8764  *
8765  * Returns SATA_SUCCESS if all device features are set successfully,
8766  * SATA_FAILURE otherwise
8767  */
8768 static int
8769 sata_initialize_device(sata_hba_inst_t *sata_hba_inst,
8770     sata_drive_info_t *sdinfo)
8771 {
8772 	int rval;
8773 
8774 	sata_save_drive_settings(sdinfo);
8775 
8776 	sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
8777 
8778 	sata_init_write_cache_mode(sdinfo);
8779 
8780 	rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0);
8781 
8782 	/* Determine current data transfer mode */
8783 	if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) {
8784 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
8785 	} else if ((sdinfo->satadrv_id.ai_validinfo &
8786 	    SATA_VALIDINFO_88) != 0 &&
8787 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) {
8788 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
8789 	} else if ((sdinfo->satadrv_id.ai_dworddma &
8790 	    SATA_MDMA_SEL_MASK) != 0) {
8791 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
8792 	} else
8793 		/* DMA supported, not no DMA transfer mode is selected !? */
8794 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
8795 
8796 	return (rval);
8797 }
8798 
8799 
8800 /*
8801  * Initialize write cache mode.
8802  *
8803  * The default write cache setting for SATA HDD is provided by sata_write_cache
8804  * static variable. ATAPI CD/DVDs devices have write cache default is
8805  * determined by sata_atapicdvd_write_cache static variable.
8806  * 1 - enable
8807  * 0 - disable
8808  * any other value - current drive setting
8809  *
8810  * Although there is not reason to disable write cache on CD/DVD devices,
8811  * the default setting control is provided for the maximun flexibility.
8812  *
8813  * In the future, it may be overridden by the
8814  * disk-write-cache-enable property setting, if it is defined.
8815  * Returns SATA_SUCCESS if all device features are set successfully,
8816  * SATA_FAILURE otherwise.
8817  */
8818 static void
8819 sata_init_write_cache_mode(sata_drive_info_t *sdinfo)
8820 {
8821 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
8822 		if (sata_write_cache == 1)
8823 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
8824 		else if (sata_write_cache == 0)
8825 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
8826 		/*
8827 		 * When sata_write_cache value is not 0 or 1,
8828 		 * a current setting of the drive's write cache is used.
8829 		 */
8830 	} else { /* Assume ATAPI CD/DVD device */
8831 		if (sata_atapicdvd_write_cache == 1)
8832 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
8833 		else if (sata_atapicdvd_write_cache == 0)
8834 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
8835 		/*
8836 		 * When sata_write_cache value is not 0 or 1,
8837 		 * a current setting of the drive's write cache is used.
8838 		 */
8839 	}
8840 }
8841 
8842 
8843 /*
8844  * Validate sata address.
8845  * Specified cport, pmport and qualifier has to match
8846  * passed sata_scsi configuration info.
8847  * The presence of an attached device is not verified.
8848  *
8849  * Returns 0 when address is valid, -1 otherwise.
8850  */
8851 static int
8852 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport,
8853 	int pmport, int qual)
8854 {
8855 	if (qual == SATA_ADDR_DCPORT && pmport != 0)
8856 		goto invalid_address;
8857 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
8858 		goto invalid_address;
8859 	if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) &&
8860 	    ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) ||
8861 	    (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) ||
8862 	    (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport))))
8863 		goto invalid_address;
8864 
8865 	return (0);
8866 
8867 invalid_address:
8868 	return (-1);
8869 
8870 }
8871 
8872 /*
8873  * Validate scsi address
8874  * SCSI target address is translated into SATA cport/pmport and compared
8875  * with a controller port/device configuration. LUN has to be 0.
8876  * Returns 0 if a scsi target refers to an attached device,
8877  * returns 1 if address is valid but device is not attached,
8878  * returns -1 if bad address or device is of an unsupported type.
8879  * Upon return sata_device argument is set.
8880  */
8881 static int
8882 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst,
8883 	struct scsi_address *ap, sata_device_t *sata_device)
8884 {
8885 	int cport, pmport, qual, rval;
8886 
8887 	rval = -1;	/* Invalid address */
8888 	if (ap->a_lun != 0)
8889 		goto out;
8890 
8891 	qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
8892 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
8893 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
8894 
8895 	if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT)
8896 		goto out;
8897 
8898 	if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) ==
8899 	    0) {
8900 
8901 		sata_cport_info_t *cportinfo;
8902 		sata_pmult_info_t *pmultinfo;
8903 		sata_drive_info_t *sdinfo = NULL;
8904 
8905 		rval = 1;	/* Valid sata address */
8906 
8907 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
8908 		if (qual == SATA_ADDR_DCPORT) {
8909 			if (cportinfo == NULL ||
8910 			    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
8911 				goto out;
8912 
8913 			if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT ||
8914 			    (cportinfo->cport_dev_type &
8915 			    SATA_VALID_DEV_TYPE) == 0) {
8916 				rval = -1;
8917 				goto out;
8918 			}
8919 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8920 
8921 		} else if (qual == SATA_ADDR_DPMPORT) {
8922 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
8923 			if (pmultinfo == NULL) {
8924 				rval = -1;
8925 				goto out;
8926 			}
8927 			if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) ==
8928 			    NULL ||
8929 			    SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
8930 			    pmport) == SATA_DTYPE_NONE)
8931 				goto out;
8932 
8933 			sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport,
8934 			    pmport);
8935 		} else {
8936 			rval = -1;
8937 			goto out;
8938 		}
8939 		if ((sdinfo == NULL) ||
8940 		    (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0)
8941 			goto out;
8942 
8943 		sata_device->satadev_type = sdinfo->satadrv_type;
8944 		sata_device->satadev_addr.qual = qual;
8945 		sata_device->satadev_addr.cport = cport;
8946 		sata_device->satadev_addr.pmport = pmport;
8947 		sata_device->satadev_rev = SATA_DEVICE_REV_1;
8948 		return (0);
8949 	}
8950 out:
8951 	if (rval == 1) {
8952 		SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
8953 		    "sata_validate_scsi_address: no valid target %x lun %x",
8954 		    ap->a_target, ap->a_lun);
8955 	}
8956 	return (rval);
8957 }
8958 
8959 /*
8960  * Find dip corresponding to passed device number
8961  *
8962  * Returns NULL if invalid device number is passed or device cannot be found,
8963  * Returns dip is device is found.
8964  */
8965 static dev_info_t *
8966 sata_devt_to_devinfo(dev_t dev)
8967 {
8968 	dev_info_t *dip;
8969 #ifndef __lock_lint
8970 	struct devnames *dnp;
8971 	major_t major = getmajor(dev);
8972 	int instance = SATA_MINOR2INSTANCE(getminor(dev));
8973 
8974 	if (major >= devcnt)
8975 		return (NULL);
8976 
8977 	dnp = &devnamesp[major];
8978 	LOCK_DEV_OPS(&(dnp->dn_lock));
8979 	dip = dnp->dn_head;
8980 	while (dip && (ddi_get_instance(dip) != instance)) {
8981 		dip = ddi_get_next(dip);
8982 	}
8983 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
8984 #endif
8985 
8986 	return (dip);
8987 }
8988 
8989 
8990 /*
8991  * Probe device.
8992  * This function issues Identify Device command and initializes local
8993  * sata_drive_info structure if the device can be identified.
8994  * The device type is determined by examining Identify Device
8995  * command response.
8996  * If the sata_hba_inst has linked drive info structure for this
8997  * device address, the Identify Device data is stored into sata_drive_info
8998  * structure linked to the port info structure.
8999  *
9000  * sata_device has to refer to the valid sata port(s) for HBA described
9001  * by sata_hba_inst structure.
9002  *
9003  * Returns:
9004  *	SATA_SUCCESS if device type was successfully probed and port-linked
9005  *		drive info structure was updated;
9006  * 	SATA_FAILURE if there is no device, or device was not probed
9007  *		successully;
9008  *	SATA_RETRY if device probe can be retried later.
9009  * If a device cannot be identified, sata_device's dev_state and dev_type
9010  * fields are set to unknown.
9011  * There are no retries in this function. Any retries should be managed by
9012  * the caller.
9013  */
9014 
9015 
9016 static int
9017 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
9018 {
9019 	sata_drive_info_t *sdinfo;
9020 	sata_drive_info_t new_sdinfo;	/* local drive info struct */
9021 	int rval;
9022 
9023 	ASSERT((SATA_CPORT_STATE(sata_hba_inst,
9024 	    sata_device->satadev_addr.cport) &
9025 	    (SATA_STATE_PROBED | SATA_STATE_READY)) != 0);
9026 
9027 	sata_device->satadev_type = SATA_DTYPE_NONE;
9028 
9029 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9030 	    sata_device->satadev_addr.cport)));
9031 
9032 	/* Get pointer to port-linked sata device info structure */
9033 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9034 	if (sdinfo != NULL) {
9035 		sdinfo->satadrv_state &=
9036 		    ~(SATA_STATE_PROBED | SATA_STATE_READY);
9037 		sdinfo->satadrv_state |= SATA_STATE_PROBING;
9038 	} else {
9039 		/* No device to probe */
9040 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9041 		    sata_device->satadev_addr.cport)));
9042 		sata_device->satadev_type = SATA_DTYPE_NONE;
9043 		sata_device->satadev_state = SATA_STATE_UNKNOWN;
9044 		return (SATA_FAILURE);
9045 	}
9046 	/*
9047 	 * Need to issue both types of identify device command and
9048 	 * determine device type by examining retreived data/status.
9049 	 * First, ATA Identify Device.
9050 	 */
9051 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
9052 	new_sdinfo.satadrv_addr = sata_device->satadev_addr;
9053 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9054 	    sata_device->satadev_addr.cport)));
9055 	new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
9056 	rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
9057 	if (rval == SATA_RETRY) {
9058 		/* We may try to check for ATAPI device */
9059 		if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) {
9060 			/*
9061 			 * HBA supports ATAPI - try to issue Identify Packet
9062 			 * Device command.
9063 			 */
9064 			new_sdinfo.satadrv_type = SATA_DTYPE_ATAPICD;
9065 			rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
9066 		}
9067 	}
9068 	if (rval == SATA_SUCCESS) {
9069 		/*
9070 		 * Got something responding positively to ATA Identify Device
9071 		 * or to Identify Packet Device cmd.
9072 		 * Save last used device type.
9073 		 */
9074 		sata_device->satadev_type = new_sdinfo.satadrv_type;
9075 
9076 		/* save device info, if possible */
9077 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9078 		    sata_device->satadev_addr.cport)));
9079 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9080 		if (sdinfo == NULL) {
9081 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9082 			    sata_device->satadev_addr.cport)));
9083 			return (SATA_FAILURE);
9084 		}
9085 		/*
9086 		 * Copy drive info into the port-linked drive info structure.
9087 		 */
9088 		*sdinfo = new_sdinfo;
9089 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
9090 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
9091 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
9092 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
9093 			    sata_device->satadev_addr.cport) =
9094 			    sdinfo->satadrv_type;
9095 		else /* SATA_ADDR_DPMPORT */
9096 			SATA_PMPORT_DEV_TYPE(sata_hba_inst,
9097 			    sata_device->satadev_addr.cport,
9098 			    sata_device->satadev_addr.pmport) =
9099 			    sdinfo->satadrv_type;
9100 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9101 		    sata_device->satadev_addr.cport)));
9102 		return (SATA_SUCCESS);
9103 	}
9104 
9105 	/*
9106 	 * It may be SATA_RETRY or SATA_FAILURE return.
9107 	 * Looks like we cannot determine the device type at this time.
9108 	 */
9109 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9110 	    sata_device->satadev_addr.cport)));
9111 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9112 	if (sdinfo != NULL) {
9113 		sata_device->satadev_type = SATA_DTYPE_UNKNOWN;
9114 		sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9115 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
9116 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
9117 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
9118 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
9119 			    sata_device->satadev_addr.cport) =
9120 			    SATA_DTYPE_UNKNOWN;
9121 		else {
9122 			/* SATA_ADDR_DPMPORT */
9123 			if ((SATA_PMULT_INFO(sata_hba_inst,
9124 			    sata_device->satadev_addr.cport) != NULL) &&
9125 			    (SATA_PMPORT_INFO(sata_hba_inst,
9126 			    sata_device->satadev_addr.cport,
9127 			    sata_device->satadev_addr.pmport) != NULL))
9128 				SATA_PMPORT_DEV_TYPE(sata_hba_inst,
9129 				    sata_device->satadev_addr.cport,
9130 				    sata_device->satadev_addr.pmport) =
9131 				    SATA_DTYPE_UNKNOWN;
9132 		}
9133 	}
9134 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9135 	    sata_device->satadev_addr.cport)));
9136 	return (rval);
9137 }
9138 
9139 
9140 /*
9141  * Get pointer to sata_drive_info structure.
9142  *
9143  * The sata_device has to contain address (cport, pmport and qualifier) for
9144  * specified sata_scsi structure.
9145  *
9146  * Returns NULL if device address is not valid for this HBA configuration.
9147  * Otherwise, returns a pointer to sata_drive_info structure.
9148  *
9149  * This function should be called with a port mutex held.
9150  */
9151 static sata_drive_info_t *
9152 sata_get_device_info(sata_hba_inst_t *sata_hba_inst,
9153     sata_device_t *sata_device)
9154 {
9155 	uint8_t cport = sata_device->satadev_addr.cport;
9156 	uint8_t pmport = sata_device->satadev_addr.pmport;
9157 	uint8_t qual = sata_device->satadev_addr.qual;
9158 
9159 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
9160 		return (NULL);
9161 
9162 	if (!(SATA_CPORT_STATE(sata_hba_inst, cport) &
9163 	    (SATA_STATE_PROBED | SATA_STATE_READY)))
9164 		/* Port not probed yet */
9165 		return (NULL);
9166 
9167 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE)
9168 		return (NULL);
9169 
9170 	if (qual == SATA_ADDR_DCPORT) {
9171 		/* Request for a device on a controller port */
9172 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
9173 		    SATA_DTYPE_PMULT)
9174 			/* Port multiplier attached */
9175 			return (NULL);
9176 		return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport));
9177 	}
9178 	if (qual == SATA_ADDR_DPMPORT) {
9179 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
9180 		    SATA_DTYPE_PMULT)
9181 			return (NULL);
9182 
9183 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport))
9184 			return (NULL);
9185 
9186 		return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport));
9187 	}
9188 
9189 	/* we should not get here */
9190 	return (NULL);
9191 }
9192 
9193 
9194 /*
9195  * sata_identify_device.
9196  * Send Identify Device command to SATA HBA driver.
9197  * If command executes successfully, update sata_drive_info structure pointed
9198  * to by sdinfo argument, including Identify Device data.
9199  * If command fails, invalidate data in sata_drive_info.
9200  *
9201  * Cannot be called from interrupt level.
9202  *
9203  * Returns:
9204  * SATA_SUCCESS if the device was identified as a supported device,
9205  * SATA_RETRY if the device was not identified but could be retried,
9206  * SATA_FAILURE if the device was not identified and identify attempt
9207  *	should not be retried.
9208  */
9209 static int
9210 sata_identify_device(sata_hba_inst_t *sata_hba_inst,
9211     sata_drive_info_t *sdinfo)
9212 {
9213 	uint16_t cfg_word;
9214 	int rval;
9215 
9216 	/* fetch device identify data */
9217 	if ((rval = sata_fetch_device_identify_data(sata_hba_inst,
9218 	    sdinfo)) != 0)
9219 		goto fail_unknown;
9220 
9221 	cfg_word = sdinfo->satadrv_id.ai_config;
9222 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK &&
9223 	    (cfg_word & SATA_ATA_TYPE_MASK) != SATA_ATA_TYPE) {
9224 		/* Change device type to reflect Identify Device data */
9225 		if (((cfg_word & SATA_ATAPI_TYPE_MASK) ==
9226 		    SATA_ATAPI_TYPE) &&
9227 		    ((cfg_word & SATA_ATAPI_ID_DEV_TYPE) ==
9228 		    SATA_ATAPI_CDROM_DEV)) {
9229 			sdinfo->satadrv_type = SATA_DTYPE_ATAPICD;
9230 		} else {
9231 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9232 		}
9233 	} else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD &&
9234 	    (((cfg_word & SATA_ATAPI_TYPE_MASK) != SATA_ATAPI_TYPE) ||
9235 	    ((cfg_word & SATA_ATAPI_ID_DEV_TYPE) != SATA_ATAPI_CDROM_DEV))) {
9236 		/* Change device type to reflect Identify Device data ! */
9237 		if ((sdinfo->satadrv_id.ai_config & SATA_ATA_TYPE_MASK) ==
9238 		    SATA_ATA_TYPE) {
9239 			sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
9240 		} else {
9241 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9242 		}
9243 	}
9244 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9245 		if (sdinfo->satadrv_capacity == 0) {
9246 			/* Non-LBA disk. Too bad... */
9247 			sata_log(sata_hba_inst, CE_WARN,
9248 			    "SATA disk device at port %d does not support LBA",
9249 			    sdinfo->satadrv_addr.cport);
9250 			rval = SATA_FAILURE;
9251 			goto fail_unknown;
9252 		}
9253 	}
9254 #if 0
9255 	/* Left for historical reason */
9256 	/*
9257 	 * Some initial version of SATA spec indicated that at least
9258 	 * UDMA mode 4 has to be supported. It is not metioned in
9259 	 * SerialATA 2.6, so this restriction is removed.
9260 	 */
9261 	/* Check for Ultra DMA modes 6 through 0 being supported */
9262 	for (i = 6; i >= 0; --i) {
9263 		if (sdinfo->satadrv_id.ai_ultradma & (1 << i))
9264 			break;
9265 	}
9266 
9267 	/*
9268 	 * At least UDMA 4 mode has to be supported. If mode 4 or
9269 	 * higher are not supported by the device, fail this
9270 	 * device.
9271 	 */
9272 	if (i < 4) {
9273 		/* No required Ultra DMA mode supported */
9274 		sata_log(sata_hba_inst, CE_WARN,
9275 		    "SATA disk device at port %d does not support UDMA "
9276 		    "mode 4 or higher", sdinfo->satadrv_addr.cport);
9277 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9278 		    "mode 4 or higher required, %d supported", i));
9279 		rval = SATA_FAILURE;
9280 		goto fail_unknown;
9281 	}
9282 #endif
9283 
9284 	return (SATA_SUCCESS);
9285 
9286 fail_unknown:
9287 	/* Invalidate sata_drive_info ? */
9288 	sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9289 	sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
9290 	return (rval);
9291 }
9292 
9293 /*
9294  * Log/display device information
9295  */
9296 static void
9297 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst,
9298     sata_drive_info_t *sdinfo)
9299 {
9300 	int valid_version;
9301 	char msg_buf[MAXPATHLEN];
9302 	int i;
9303 
9304 	/* Show HBA path */
9305 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf);
9306 
9307 	cmn_err(CE_CONT, "?%s :\n", msg_buf);
9308 
9309 	if (sdinfo->satadrv_type == SATA_DTYPE_UNKNOWN) {
9310 		(void) sprintf(msg_buf,
9311 		    "Unsupported SATA device type (cfg 0x%x) at ",
9312 		    sdinfo->satadrv_id.ai_config);
9313 	} else {
9314 		(void) sprintf(msg_buf, "SATA %s device at",
9315 		    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
9316 		    "disk":"CD/DVD (ATAPI)");
9317 	}
9318 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT)
9319 		cmn_err(CE_CONT, "?\t%s port %d\n",
9320 		    msg_buf, sdinfo->satadrv_addr.cport);
9321 	else
9322 		cmn_err(CE_CONT, "?\t%s port %d pmport %d\n",
9323 		    msg_buf, sdinfo->satadrv_addr.cport,
9324 		    sdinfo->satadrv_addr.pmport);
9325 
9326 	bcopy(&sdinfo->satadrv_id.ai_model, msg_buf,
9327 	    sizeof (sdinfo->satadrv_id.ai_model));
9328 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model));
9329 	msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
9330 	cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf);
9331 
9332 	bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf,
9333 	    sizeof (sdinfo->satadrv_id.ai_fw));
9334 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw));
9335 	msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
9336 	cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf);
9337 
9338 	bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf,
9339 	    sizeof (sdinfo->satadrv_id.ai_drvser));
9340 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser));
9341 	msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
9342 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9343 		cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
9344 	} else {
9345 		/* Assuming ATAPI CD/DVD */
9346 		/*
9347 		 * SOme drives do not implement serial number and may
9348 		 * violate the spec by provinding spaces rather than zeros
9349 		 * in serial number field. Scan the buffer to detect it.
9350 		 */
9351 		for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) {
9352 			if (msg_buf[i] != '\0' && msg_buf[i] != ' ')
9353 				break;
9354 		}
9355 		if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) {
9356 			cmn_err(CE_CONT, "?\tserial number - none\n");
9357 		} else {
9358 			cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
9359 		}
9360 	}
9361 
9362 #ifdef SATA_DEBUG
9363 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
9364 	    sdinfo->satadrv_id.ai_majorversion != 0xffff) {
9365 		int i;
9366 		for (i = 14; i >= 2; i--) {
9367 			if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) {
9368 				valid_version = i;
9369 				break;
9370 			}
9371 		}
9372 		cmn_err(CE_CONT,
9373 		    "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
9374 		    valid_version,
9375 		    sdinfo->satadrv_id.ai_majorversion,
9376 		    sdinfo->satadrv_id.ai_minorversion);
9377 	}
9378 #endif
9379 	/* Log some info */
9380 	cmn_err(CE_CONT, "?\tsupported features:\n");
9381 	msg_buf[0] = '\0';
9382 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9383 		if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48)
9384 			(void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN);
9385 		else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28)
9386 			(void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN);
9387 	}
9388 	if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA)
9389 		(void) strlcat(msg_buf, "DMA", MAXPATHLEN);
9390 	if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ)
9391 		(void) strlcat(msg_buf, ", Native Command Queueing",
9392 		    MAXPATHLEN);
9393 	if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)
9394 		(void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN);
9395 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) &&
9396 	    (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED))
9397 		(void) strlcat(msg_buf, ", SMART", MAXPATHLEN);
9398 	if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) &&
9399 	    (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED))
9400 		(void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN);
9401 	cmn_err(CE_CONT, "?\t %s\n", msg_buf);
9402 	if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2)
9403 		cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n");
9404 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1)
9405 		cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n");
9406 	if (sdinfo->satadrv_features_support &
9407 	    (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) {
9408 		msg_buf[0] = '\0';
9409 		(void) snprintf(msg_buf, MAXPATHLEN,
9410 		    "Supported queue depth %d",
9411 		    sdinfo->satadrv_queue_depth);
9412 		if (!(sata_func_enable &
9413 		    (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ)))
9414 			(void) strlcat(msg_buf,
9415 			    " - queueing disabled globally", MAXPATHLEN);
9416 		else if (sdinfo->satadrv_queue_depth >
9417 		    sdinfo->satadrv_max_queue_depth) {
9418 			(void) snprintf(&msg_buf[strlen(msg_buf)],
9419 			    MAXPATHLEN - strlen(msg_buf), ", limited to %d",
9420 			    (int)sdinfo->satadrv_max_queue_depth);
9421 		}
9422 		cmn_err(CE_CONT, "?\t%s\n", msg_buf);
9423 	}
9424 
9425 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9426 #ifdef __i386
9427 		(void) sprintf(msg_buf, "\tcapacity = %llu sectors\n",
9428 		    sdinfo->satadrv_capacity);
9429 #else
9430 		(void) sprintf(msg_buf, "\tcapacity = %lu sectors\n",
9431 		    sdinfo->satadrv_capacity);
9432 #endif
9433 		cmn_err(CE_CONT, "?%s", msg_buf);
9434 	}
9435 }
9436 
9437 
9438 /*
9439  * sata_save_drive_settings extracts current setting of the device and stores
9440  * it for future reference, in case the device setup would need to be restored
9441  * after the device reset.
9442  *
9443  * For all devices read ahead and write cache settings are saved, if the
9444  * device supports these features at all.
9445  * For ATAPI devices the Removable Media Status Notification setting is saved.
9446  */
9447 static void
9448 sata_save_drive_settings(sata_drive_info_t *sdinfo)
9449 {
9450 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) ||
9451 	    (sdinfo->satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) {
9452 
9453 		/* Current setting of Read Ahead (and Read Cache) */
9454 		if (sdinfo->satadrv_id.ai_features85 & SATA_LOOK_AHEAD)
9455 			sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
9456 		else
9457 			sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD;
9458 
9459 		/* Current setting of Write Cache */
9460 		if (sdinfo->satadrv_id.ai_features85 & SATA_WRITE_CACHE)
9461 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
9462 		else
9463 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
9464 	}
9465 
9466 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
9467 		if (sdinfo->satadrv_id.ai_cmdset83 & SATA_RM_STATUS_NOTIFIC)
9468 			sdinfo->satadrv_settings |= SATA_DEV_RMSN;
9469 		else
9470 			sdinfo->satadrv_settings &= ~SATA_DEV_RMSN;
9471 	}
9472 }
9473 
9474 
9475 /*
9476  * sata_check_capacity function determines a disk capacity
9477  * and addressing mode (LBA28/LBA48) by examining a disk identify device data.
9478  *
9479  * NOTE: CHS mode is not supported! If a device does not support LBA,
9480  * this function is not called.
9481  *
9482  * Returns device capacity in number of blocks, i.e. largest addressable LBA+1
9483  */
9484 static uint64_t
9485 sata_check_capacity(sata_drive_info_t *sdinfo)
9486 {
9487 	uint64_t capacity = 0;
9488 	int i;
9489 
9490 	if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK ||
9491 	    !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT)
9492 		/* Capacity valid only for LBA-addressable disk devices */
9493 		return (0);
9494 
9495 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) &&
9496 	    (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) &&
9497 	    (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) {
9498 		/* LBA48 mode supported and enabled */
9499 		sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 |
9500 		    SATA_DEV_F_LBA28;
9501 		for (i = 3;  i >= 0;  --i) {
9502 			capacity <<= 16;
9503 			capacity += sdinfo->satadrv_id.ai_addrsecxt[i];
9504 		}
9505 	} else {
9506 		capacity = sdinfo->satadrv_id.ai_addrsec[1];
9507 		capacity <<= 16;
9508 		capacity += sdinfo->satadrv_id.ai_addrsec[0];
9509 		if (capacity >= 0x1000000)
9510 			/* LBA28 mode */
9511 			sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28;
9512 	}
9513 	return (capacity);
9514 }
9515 
9516 
9517 /*
9518  * Allocate consistent buffer for DMA transfer
9519  *
9520  * Cannot be called from interrupt level or with mutex held - it may sleep.
9521  *
9522  * Returns pointer to allocated buffer structure, or NULL if allocation failed.
9523  */
9524 static struct buf *
9525 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len)
9526 {
9527 	struct scsi_address ap;
9528 	struct buf *bp;
9529 	ddi_dma_attr_t	cur_dma_attr;
9530 
9531 	ASSERT(spx->txlt_sata_pkt != NULL);
9532 	ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran;
9533 	ap.a_target = SATA_TO_SCSI_TARGET(
9534 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport,
9535 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport,
9536 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual);
9537 	ap.a_lun = 0;
9538 
9539 	bp = scsi_alloc_consistent_buf(&ap, NULL, len,
9540 	    B_READ, SLEEP_FUNC, NULL);
9541 
9542 	if (bp != NULL) {
9543 		/* Allocate DMA resources for this buffer */
9544 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
9545 		/*
9546 		 * We use a local version of the dma_attr, to account
9547 		 * for a device addressing limitations.
9548 		 * sata_adjust_dma_attr() will handle sdinfo == NULL which
9549 		 * will cause dma attributes to be adjusted to a lowest
9550 		 * acceptable level.
9551 		 */
9552 		sata_adjust_dma_attr(NULL,
9553 		    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
9554 
9555 		if (sata_dma_buf_setup(spx, PKT_CONSISTENT,
9556 		    SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) {
9557 			scsi_free_consistent_buf(bp);
9558 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
9559 			bp = NULL;
9560 		}
9561 	}
9562 	return (bp);
9563 }
9564 
9565 /*
9566  * Release local buffer (consistent buffer for DMA transfer) allocated
9567  * via sata_alloc_local_buffer().
9568  */
9569 static void
9570 sata_free_local_buffer(sata_pkt_txlate_t *spx)
9571 {
9572 	ASSERT(spx->txlt_sata_pkt != NULL);
9573 	ASSERT(spx->txlt_dma_cookie_list != NULL);
9574 	ASSERT(spx->txlt_dma_cookie_list_len != 0);
9575 	ASSERT(spx->txlt_buf_dma_handle != NULL);
9576 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL);
9577 
9578 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0;
9579 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL;
9580 
9581 	/* Free DMA resources */
9582 	(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
9583 	ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
9584 	spx->txlt_buf_dma_handle = 0;
9585 
9586 	if (spx->txlt_dma_cookie_list != &spx->txlt_dma_cookie) {
9587 		kmem_free(spx->txlt_dma_cookie_list,
9588 		    spx->txlt_dma_cookie_list_len * sizeof (ddi_dma_cookie_t));
9589 		spx->txlt_dma_cookie_list = NULL;
9590 		spx->txlt_dma_cookie_list_len = 0;
9591 	}
9592 	/* Free buffer */
9593 	scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp);
9594 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
9595 }
9596 
9597 
9598 
9599 
9600 /*
9601  * Allocate sata_pkt
9602  * Pkt structure version and embedded strcutures version are initialized.
9603  * sata_pkt and sata_pkt_txlate structures are cross-linked.
9604  *
9605  * Since this may be called in interrupt context by sata_scsi_init_pkt,
9606  * callback argument determines if it can sleep or not.
9607  * Hence, it should not be called from interrupt context.
9608  *
9609  * If successful, non-NULL pointer to a sata pkt is returned.
9610  * Upon failure, NULL pointer is returned.
9611  */
9612 static sata_pkt_t *
9613 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t))
9614 {
9615 	sata_pkt_t *spkt;
9616 	int kmsflag;
9617 
9618 	kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
9619 	spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag);
9620 	if (spkt == NULL) {
9621 		SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9622 		    "sata_pkt_alloc: failed"));
9623 		return (NULL);
9624 	}
9625 	spkt->satapkt_rev = SATA_PKT_REV;
9626 	spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV;
9627 	spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
9628 	spkt->satapkt_framework_private = spx;
9629 	spx->txlt_sata_pkt = spkt;
9630 	return (spkt);
9631 }
9632 
9633 /*
9634  * Free sata pkt allocated via sata_pkt_alloc()
9635  */
9636 static void
9637 sata_pkt_free(sata_pkt_txlate_t *spx)
9638 {
9639 	ASSERT(spx->txlt_sata_pkt != NULL);
9640 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL);
9641 	kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t));
9642 	spx->txlt_sata_pkt = NULL;
9643 }
9644 
9645 
9646 /*
9647  * Adjust DMA attributes.
9648  * SCSI cmds block count is up to 24 bits, SATA cmd block count vary
9649  * from 8 bits to 16 bits, depending on a command being used.
9650  * Limiting max block count arbitrarily to 256 for all read/write
9651  * commands may affects performance, so check both the device and
9652  * controller capability before adjusting dma attributes.
9653  */
9654 void
9655 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr,
9656     ddi_dma_attr_t *adj_dma_attr)
9657 {
9658 	uint32_t count_max;
9659 
9660 	/* Copy original attributes */
9661 	*adj_dma_attr = *dma_attr;
9662 	/*
9663 	 * Things to consider: device addressing capability,
9664 	 * "excessive" controller DMA capabilities.
9665 	 * If a device is being probed/initialized, there are
9666 	 * no device info - use default limits then.
9667 	 */
9668 	if (sdinfo == NULL) {
9669 		count_max = dma_attr->dma_attr_granular * 0x100;
9670 		if (dma_attr->dma_attr_count_max > count_max)
9671 			adj_dma_attr->dma_attr_count_max = count_max;
9672 		if (dma_attr->dma_attr_maxxfer > count_max)
9673 			adj_dma_attr->dma_attr_maxxfer = count_max;
9674 		return;
9675 	}
9676 
9677 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9678 		if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) {
9679 			/*
9680 			 * 16-bit sector count may be used - we rely on
9681 			 * the assumption that only read and write cmds
9682 			 * will request more than 256 sectors worth of data
9683 			 */
9684 			count_max = adj_dma_attr->dma_attr_granular * 0x10000;
9685 		} else {
9686 			/*
9687 			 * 8-bit sector count will be used - default limits
9688 			 * for dma attributes
9689 			 */
9690 			count_max = adj_dma_attr->dma_attr_granular * 0x100;
9691 		}
9692 		/*
9693 		 * Adjust controler dma attributes, if necessary
9694 		 */
9695 		if (dma_attr->dma_attr_count_max > count_max)
9696 			adj_dma_attr->dma_attr_count_max = count_max;
9697 		if (dma_attr->dma_attr_maxxfer > count_max)
9698 			adj_dma_attr->dma_attr_maxxfer = count_max;
9699 	}
9700 }
9701 
9702 
9703 /*
9704  * Allocate DMA resources for the buffer
9705  * This function handles initial DMA resource allocation as well as
9706  * DMA window shift and may be called repeatedly for the same DMA window
9707  * until all DMA cookies in the DMA window are processed.
9708  * To guarantee that there is always a coherent set of cookies to process
9709  * by SATA HBA driver (observing alignment, device granularity, etc.),
9710  * the number of slots for DMA cookies is equal to lesser of  a number of
9711  * cookies in a DMA window and a max number of scatter/gather entries.
9712  *
9713  * Returns DDI_SUCCESS upon successful operation.
9714  * Return failure code of a failing command or DDI_FAILURE when
9715  * internal cleanup failed.
9716  */
9717 static int
9718 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags,
9719     int (*callback)(caddr_t), caddr_t arg,
9720     ddi_dma_attr_t *cur_dma_attr)
9721 {
9722 	int	rval;
9723 	off_t	offset;
9724 	size_t	size;
9725 	int	max_sg_len, req_len, i;
9726 	uint_t	dma_flags;
9727 	struct buf	*bp;
9728 	uint64_t	cur_txfer_len;
9729 
9730 
9731 	ASSERT(spx->txlt_sata_pkt != NULL);
9732 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9733 	ASSERT(bp != NULL);
9734 
9735 
9736 	if (spx->txlt_buf_dma_handle == NULL) {
9737 		/*
9738 		 * No DMA resources allocated so far - this is a first call
9739 		 * for this sata pkt.
9740 		 */
9741 		rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst),
9742 		    cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle);
9743 
9744 		if (rval != DDI_SUCCESS) {
9745 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9746 			    "sata_dma_buf_setup: no buf DMA resources %x",
9747 			    rval));
9748 			return (rval);
9749 		}
9750 
9751 		if (bp->b_flags & B_READ)
9752 			dma_flags = DDI_DMA_READ;
9753 		else
9754 			dma_flags = DDI_DMA_WRITE;
9755 
9756 		if (flags & PKT_CONSISTENT)
9757 			dma_flags |= DDI_DMA_CONSISTENT;
9758 
9759 		if (flags & PKT_DMA_PARTIAL)
9760 			dma_flags |= DDI_DMA_PARTIAL;
9761 
9762 		/*
9763 		 * Check buffer alignment and size against dma attributes
9764 		 * Consider dma_attr_align only. There may be requests
9765 		 * with the size lower than device granularity, but they
9766 		 * will not read/write from/to the device, so no adjustment
9767 		 * is necessary. The dma_attr_minxfer theoretically should
9768 		 * be considered, but no HBA driver is checking it.
9769 		 */
9770 		if (IS_P2ALIGNED(bp->b_un.b_addr,
9771 		    cur_dma_attr->dma_attr_align)) {
9772 			rval = ddi_dma_buf_bind_handle(
9773 			    spx->txlt_buf_dma_handle,
9774 			    bp, dma_flags, callback, arg,
9775 			    &spx->txlt_dma_cookie,
9776 			    &spx->txlt_curwin_num_dma_cookies);
9777 		} else { /* Buffer is not aligned */
9778 
9779 			int	(*ddicallback)(caddr_t);
9780 			size_t	bufsz;
9781 
9782 			/* Check id sleeping is allowed */
9783 			ddicallback = (callback == NULL_FUNC) ?
9784 			    DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
9785 
9786 			SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
9787 			    "mis-aligned buffer: addr=0x%p, cnt=%lu",
9788 			    (void *)bp->b_un.b_addr, bp->b_bcount);
9789 
9790 			if (bp->b_flags & (B_PAGEIO|B_PHYS))
9791 				/*
9792 				 * CPU will need to access data in the buffer
9793 				 * (for copying) so map it.
9794 				 */
9795 				bp_mapin(bp);
9796 
9797 			ASSERT(spx->txlt_tmp_buf == NULL);
9798 
9799 			/* Buffer may be padded by ddi_dma_mem_alloc()! */
9800 			rval = ddi_dma_mem_alloc(
9801 			    spx->txlt_buf_dma_handle,
9802 			    bp->b_bcount,
9803 			    &sata_acc_attr,
9804 			    DDI_DMA_STREAMING,
9805 			    ddicallback, NULL,
9806 			    &spx->txlt_tmp_buf,
9807 			    &bufsz,
9808 			    &spx->txlt_tmp_buf_handle);
9809 
9810 			if (rval != DDI_SUCCESS) {
9811 				/* DMA mapping failed */
9812 				(void) ddi_dma_free_handle(
9813 				    &spx->txlt_buf_dma_handle);
9814 				spx->txlt_buf_dma_handle = NULL;
9815 #ifdef SATA_DEBUG
9816 				mbuffail_count++;
9817 #endif
9818 				SATADBG1(SATA_DBG_DMA_SETUP,
9819 				    spx->txlt_sata_hba_inst,
9820 				    "sata_dma_buf_setup: "
9821 				    "buf dma mem alloc failed %x\n", rval);
9822 				return (rval);
9823 			}
9824 			ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf,
9825 			    cur_dma_attr->dma_attr_align));
9826 
9827 #ifdef SATA_DEBUG
9828 			mbuf_count++;
9829 
9830 			if (bp->b_bcount != bufsz)
9831 				/*
9832 				 * This will require special handling, because
9833 				 * DMA cookies will be based on the temporary
9834 				 * buffer size, not the original buffer
9835 				 * b_bcount, so the residue may have to
9836 				 * be counted differently.
9837 				 */
9838 				SATADBG2(SATA_DBG_DMA_SETUP,
9839 				    spx->txlt_sata_hba_inst,
9840 				    "sata_dma_buf_setup: bp size %x != "
9841 				    "bufsz %x\n", bp->b_bcount, bufsz);
9842 #endif
9843 			if (dma_flags & DDI_DMA_WRITE) {
9844 				/*
9845 				 * Write operation - copy data into
9846 				 * an aligned temporary buffer. Buffer will be
9847 				 * synced for device by ddi_dma_addr_bind_handle
9848 				 */
9849 				bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf,
9850 				    bp->b_bcount);
9851 			}
9852 
9853 			rval = ddi_dma_addr_bind_handle(
9854 			    spx->txlt_buf_dma_handle,
9855 			    NULL,
9856 			    spx->txlt_tmp_buf,
9857 			    bufsz, dma_flags, ddicallback, 0,
9858 			    &spx->txlt_dma_cookie,
9859 			    &spx->txlt_curwin_num_dma_cookies);
9860 		}
9861 
9862 		switch (rval) {
9863 		case DDI_DMA_PARTIAL_MAP:
9864 			SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
9865 			    "sata_dma_buf_setup: DMA Partial Map\n", NULL);
9866 			/*
9867 			 * Partial DMA mapping.
9868 			 * Retrieve number of DMA windows for this request.
9869 			 */
9870 			if (ddi_dma_numwin(spx->txlt_buf_dma_handle,
9871 			    &spx->txlt_num_dma_win) != DDI_SUCCESS) {
9872 				if (spx->txlt_tmp_buf != NULL) {
9873 					ddi_dma_mem_free(
9874 					    &spx->txlt_tmp_buf_handle);
9875 					spx->txlt_tmp_buf = NULL;
9876 				}
9877 				(void) ddi_dma_unbind_handle(
9878 				    spx->txlt_buf_dma_handle);
9879 				(void) ddi_dma_free_handle(
9880 				    &spx->txlt_buf_dma_handle);
9881 				spx->txlt_buf_dma_handle = NULL;
9882 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9883 				    "sata_dma_buf_setup: numwin failed\n"));
9884 				return (DDI_FAILURE);
9885 			}
9886 			SATADBG2(SATA_DBG_DMA_SETUP,
9887 			    spx->txlt_sata_hba_inst,
9888 			    "sata_dma_buf_setup: windows: %d, cookies: %d\n",
9889 			    spx->txlt_num_dma_win,
9890 			    spx->txlt_curwin_num_dma_cookies);
9891 			spx->txlt_cur_dma_win = 0;
9892 			break;
9893 
9894 		case DDI_DMA_MAPPED:
9895 			/* DMA fully mapped */
9896 			spx->txlt_num_dma_win = 1;
9897 			spx->txlt_cur_dma_win = 0;
9898 			SATADBG1(SATA_DBG_DMA_SETUP,
9899 			    spx->txlt_sata_hba_inst,
9900 			    "sata_dma_buf_setup: windows: 1 "
9901 			    "cookies: %d\n", spx->txlt_curwin_num_dma_cookies);
9902 			break;
9903 
9904 		default:
9905 			/* DMA mapping failed */
9906 			if (spx->txlt_tmp_buf != NULL) {
9907 				ddi_dma_mem_free(
9908 				    &spx->txlt_tmp_buf_handle);
9909 				spx->txlt_tmp_buf = NULL;
9910 			}
9911 			(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
9912 			spx->txlt_buf_dma_handle = NULL;
9913 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9914 			    "sata_dma_buf_setup: buf dma handle binding "
9915 			    "failed %x\n", rval));
9916 			return (rval);
9917 		}
9918 		spx->txlt_curwin_processed_dma_cookies = 0;
9919 		spx->txlt_dma_cookie_list = NULL;
9920 	} else {
9921 		/*
9922 		 * DMA setup is reused. Check if we need to process more
9923 		 * cookies in current window, or to get next window, if any.
9924 		 */
9925 
9926 		ASSERT(spx->txlt_curwin_processed_dma_cookies <=
9927 		    spx->txlt_curwin_num_dma_cookies);
9928 
9929 		if (spx->txlt_curwin_processed_dma_cookies ==
9930 		    spx->txlt_curwin_num_dma_cookies) {
9931 			/*
9932 			 * All cookies from current DMA window were processed.
9933 			 * Get next DMA window.
9934 			 */
9935 			spx->txlt_cur_dma_win++;
9936 			if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) {
9937 				(void) ddi_dma_getwin(spx->txlt_buf_dma_handle,
9938 				    spx->txlt_cur_dma_win, &offset, &size,
9939 				    &spx->txlt_dma_cookie,
9940 				    &spx->txlt_curwin_num_dma_cookies);
9941 				spx->txlt_curwin_processed_dma_cookies = 0;
9942 			} else {
9943 				/* No more windows! End of request! */
9944 				/* What to do? - panic for now */
9945 				ASSERT(spx->txlt_cur_dma_win >=
9946 				    spx->txlt_num_dma_win);
9947 
9948 				spx->txlt_curwin_num_dma_cookies = 0;
9949 				spx->txlt_curwin_processed_dma_cookies = 0;
9950 				spx->txlt_sata_pkt->
9951 				    satapkt_cmd.satacmd_num_dma_cookies = 0;
9952 				return (DDI_SUCCESS);
9953 			}
9954 		}
9955 	}
9956 	/* There better be at least one DMA cookie outstanding */
9957 	ASSERT((spx->txlt_curwin_num_dma_cookies -
9958 	    spx->txlt_curwin_processed_dma_cookies) > 0);
9959 
9960 	if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) {
9961 		/* The default cookie slot was used in previous run */
9962 		ASSERT(spx->txlt_curwin_processed_dma_cookies == 0);
9963 		spx->txlt_dma_cookie_list = NULL;
9964 		spx->txlt_dma_cookie_list_len = 0;
9965 	}
9966 	if (spx->txlt_curwin_processed_dma_cookies == 0) {
9967 		/*
9968 		 * Processing a new DMA window - set-up dma cookies list.
9969 		 * We may reuse previously allocated cookie array if it is
9970 		 * possible.
9971 		 */
9972 		if (spx->txlt_dma_cookie_list != NULL &&
9973 		    spx->txlt_dma_cookie_list_len <
9974 		    spx->txlt_curwin_num_dma_cookies) {
9975 			/*
9976 			 * New DMA window contains more cookies than
9977 			 * the previous one. We need larger cookie list - free
9978 			 * the old one.
9979 			 */
9980 			(void) kmem_free(spx->txlt_dma_cookie_list,
9981 			    spx->txlt_dma_cookie_list_len *
9982 			    sizeof (ddi_dma_cookie_t));
9983 			spx->txlt_dma_cookie_list = NULL;
9984 			spx->txlt_dma_cookie_list_len = 0;
9985 		}
9986 		if (spx->txlt_dma_cookie_list == NULL) {
9987 			/*
9988 			 * Calculate lesser of number of cookies in this
9989 			 * DMA window and number of s/g entries.
9990 			 */
9991 			max_sg_len = cur_dma_attr->dma_attr_sgllen;
9992 			req_len = MIN(max_sg_len,
9993 			    spx->txlt_curwin_num_dma_cookies);
9994 
9995 			/* Allocate new dma cookie array if necessary */
9996 			if (req_len == 1) {
9997 				/* Only one cookie - no need for a list */
9998 				spx->txlt_dma_cookie_list =
9999 				    &spx->txlt_dma_cookie;
10000 				spx->txlt_dma_cookie_list_len = 1;
10001 			} else {
10002 				/*
10003 				 * More than one cookie - try to allocate space.
10004 				 */
10005 				spx->txlt_dma_cookie_list = kmem_zalloc(
10006 				    sizeof (ddi_dma_cookie_t) * req_len,
10007 				    callback == NULL_FUNC ? KM_NOSLEEP :
10008 				    KM_SLEEP);
10009 				if (spx->txlt_dma_cookie_list == NULL) {
10010 					SATADBG1(SATA_DBG_DMA_SETUP,
10011 					    spx->txlt_sata_hba_inst,
10012 					    "sata_dma_buf_setup: cookie list "
10013 					    "allocation failed\n", NULL);
10014 					/*
10015 					 * We could not allocate space for
10016 					 * neccessary number of dma cookies in
10017 					 * this window, so we fail this request.
10018 					 * Next invocation would try again to
10019 					 * allocate space for cookie list.
10020 					 * Note:Packet residue was not modified.
10021 					 */
10022 					return (DDI_DMA_NORESOURCES);
10023 				} else {
10024 					spx->txlt_dma_cookie_list_len = req_len;
10025 				}
10026 			}
10027 		}
10028 		/*
10029 		 * Fetch DMA cookies into cookie list in sata_pkt_txlate.
10030 		 * First cookie was already fetched.
10031 		 */
10032 		*(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie;
10033 		cur_txfer_len =
10034 		    (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size;
10035 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1;
10036 		spx->txlt_curwin_processed_dma_cookies++;
10037 		for (i = 1; (i < spx->txlt_dma_cookie_list_len) &&
10038 		    (i < spx->txlt_curwin_num_dma_cookies); i++) {
10039 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
10040 			    &spx->txlt_dma_cookie_list[i]);
10041 			cur_txfer_len +=
10042 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
10043 			spx->txlt_curwin_processed_dma_cookies++;
10044 			spx->txlt_sata_pkt->
10045 			    satapkt_cmd.satacmd_num_dma_cookies += 1;
10046 		}
10047 	} else {
10048 		SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
10049 		    "sata_dma_buf_setup: sliding within DMA window, "
10050 		    "cur cookie %d, total cookies %d\n",
10051 		    spx->txlt_curwin_processed_dma_cookies,
10052 		    spx->txlt_curwin_num_dma_cookies);
10053 
10054 		/*
10055 		 * Not all cookies from the current dma window were used because
10056 		 * of s/g limitation.
10057 		 * There is no need to re-size the list - it was set at
10058 		 * optimal size, or only default entry is used (s/g = 1).
10059 		 */
10060 		if (spx->txlt_dma_cookie_list == NULL) {
10061 			spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie;
10062 			spx->txlt_dma_cookie_list_len = 1;
10063 		}
10064 		/*
10065 		 * Since we are processing remaining cookies in a DMA window,
10066 		 * there may be less of them than the number of entries in the
10067 		 * current dma cookie list.
10068 		 */
10069 		req_len = MIN(spx->txlt_dma_cookie_list_len,
10070 		    (spx->txlt_curwin_num_dma_cookies -
10071 		    spx->txlt_curwin_processed_dma_cookies));
10072 
10073 		/* Fetch the next batch of cookies */
10074 		for (i = 0, cur_txfer_len = 0; i < req_len; i++) {
10075 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
10076 			    &spx->txlt_dma_cookie_list[i]);
10077 			cur_txfer_len +=
10078 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
10079 			spx->txlt_sata_pkt->
10080 			    satapkt_cmd.satacmd_num_dma_cookies++;
10081 			spx->txlt_curwin_processed_dma_cookies++;
10082 		}
10083 	}
10084 
10085 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0);
10086 
10087 	/* Point sata_cmd to the cookie list */
10088 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list =
10089 	    &spx->txlt_dma_cookie_list[0];
10090 
10091 	/* Remember number of DMA cookies passed in sata packet */
10092 	spx->txlt_num_dma_cookies =
10093 	    spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies;
10094 
10095 	ASSERT(cur_txfer_len != 0);
10096 	if (cur_txfer_len <= bp->b_bcount)
10097 		spx->txlt_total_residue -= cur_txfer_len;
10098 	else {
10099 		/*
10100 		 * Temporary DMA buffer has been padded by
10101 		 * ddi_dma_mem_alloc()!
10102 		 * This requires special handling, because DMA cookies are
10103 		 * based on the temporary buffer size, not the b_bcount,
10104 		 * and we have extra bytes to transfer - but the packet
10105 		 * residue has to stay correct because we will copy only
10106 		 * the requested number of bytes.
10107 		 */
10108 		spx->txlt_total_residue -= bp->b_bcount;
10109 	}
10110 
10111 	return (DDI_SUCCESS);
10112 }
10113 
10114 
10115 /*
10116  * Fetch Device Identify data.
10117  * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type)
10118  * command to a device and get the device identify data.
10119  * The device_info structure has to be set to device type (for selecting proper
10120  * device identify command).
10121  *
10122  * Returns:
10123  * SATA_SUCCESS if cmd succeeded
10124  * SATA_RETRY if cmd was rejected and could be retried,
10125  * SATA_FAILURE if cmd failed and should not be retried (port error)
10126  *
10127  * Cannot be called in an interrupt context.
10128  */
10129 
10130 static int
10131 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst,
10132     sata_drive_info_t *sdinfo)
10133 {
10134 	struct buf *bp;
10135 	sata_pkt_t *spkt;
10136 	sata_cmd_t *scmd;
10137 	sata_pkt_txlate_t *spx;
10138 	int rval;
10139 
10140 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10141 	spx->txlt_sata_hba_inst = sata_hba_inst;
10142 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
10143 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10144 	if (spkt == NULL) {
10145 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10146 		return (SATA_RETRY); /* may retry later */
10147 	}
10148 	/* address is needed now */
10149 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10150 
10151 	/*
10152 	 * Allocate buffer for Identify Data return data
10153 	 */
10154 	bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t));
10155 	if (bp == NULL) {
10156 		sata_pkt_free(spx);
10157 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10158 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10159 		    "sata_fetch_device_identify_data: "
10160 		    "cannot allocate buffer for ID"));
10161 		return (SATA_RETRY); /* may retry later */
10162 	}
10163 
10164 	/* Fill sata_pkt */
10165 	sdinfo->satadrv_state = SATA_STATE_PROBING;
10166 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10167 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10168 	/* Synchronous mode, no callback */
10169 	spkt->satapkt_comp = NULL;
10170 	/* Timeout 30s */
10171 	spkt->satapkt_time = sata_default_pkt_time;
10172 
10173 	scmd = &spkt->satapkt_cmd;
10174 	scmd->satacmd_bp = bp;
10175 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
10176 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10177 
10178 	/* Build Identify Device cmd in the sata_pkt */
10179 	scmd->satacmd_addr_type = 0;		/* N/A */
10180 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
10181 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
10182 	scmd->satacmd_lba_mid_lsb = 0;		/* N/A */
10183 	scmd->satacmd_lba_high_lsb = 0;		/* N/A */
10184 	scmd->satacmd_features_reg = 0;		/* N/A */
10185 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
10186 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
10187 		/* Identify Packet Device cmd */
10188 		scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE;
10189 	} else {
10190 		/* Identify Device cmd - mandatory for all other devices */
10191 		scmd->satacmd_cmd_reg = SATAC_ID_DEVICE;
10192 	}
10193 
10194 	/* Send pkt to SATA HBA driver */
10195 	rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt);
10196 
10197 #ifdef SATA_INJECT_FAULTS
10198 	if (sata_inject_fault == SATA_INJECT_PKT_FAULT)
10199 		if (sata_fault_cmd == scmd->satacmd_cmd_reg)
10200 			sata_inject_pkt_fault(spkt, scmd->satacmd_cmd_reg,
10201 			    &rval, sata_inject_fault_type);
10202 #endif
10203 
10204 	if (rval == SATA_TRAN_ACCEPTED &&
10205 	    spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10206 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10207 		    DDI_DMA_SYNC_FORKERNEL);
10208 		ASSERT(rval == DDI_SUCCESS);
10209 		if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config &
10210 		    SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) {
10211 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10212 			    "SATA disk device at port %d - "
10213 			    "partial Identify Data",
10214 			    sdinfo->satadrv_addr.cport));
10215 			rval = SATA_RETRY; /* may retry later */
10216 			goto fail;
10217 		}
10218 		/* Update sata_drive_info */
10219 		bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id,
10220 		    sizeof (sata_id_t));
10221 
10222 		sdinfo->satadrv_features_support = 0;
10223 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10224 			/*
10225 			 * Retrieve capacity (disks only) and addressing mode
10226 			 */
10227 			sdinfo->satadrv_capacity = sata_check_capacity(sdinfo);
10228 		} else {
10229 			/*
10230 			 * For ATAPI devices one would have to issue
10231 			 * Get Capacity cmd for media capacity. Not here.
10232 			 */
10233 			sdinfo->satadrv_capacity = 0;
10234 			/*
10235 			 * Check what cdb length is supported
10236 			 */
10237 			if ((sdinfo->satadrv_id.ai_config &
10238 			    SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B)
10239 				sdinfo->satadrv_atapi_cdb_len = 16;
10240 			else
10241 				sdinfo->satadrv_atapi_cdb_len = 12;
10242 		}
10243 		/* Setup supported features flags */
10244 		if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT)
10245 			sdinfo->satadrv_features_support |= SATA_DEV_F_DMA;
10246 
10247 		/* Check for SATA GEN and NCQ support */
10248 		if (sdinfo->satadrv_id.ai_satacap != 0 &&
10249 		    sdinfo->satadrv_id.ai_satacap != 0xffff) {
10250 			/* SATA compliance */
10251 			if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ)
10252 				sdinfo->satadrv_features_support |=
10253 				    SATA_DEV_F_NCQ;
10254 			if (sdinfo->satadrv_id.ai_satacap &
10255 			    (SATA_1_SPEED | SATA_2_SPEED)) {
10256 				if (sdinfo->satadrv_id.ai_satacap &
10257 				    SATA_2_SPEED)
10258 					sdinfo->satadrv_features_support |=
10259 					    SATA_DEV_F_SATA2;
10260 				if (sdinfo->satadrv_id.ai_satacap &
10261 				    SATA_1_SPEED)
10262 					sdinfo->satadrv_features_support |=
10263 					    SATA_DEV_F_SATA1;
10264 			} else {
10265 				sdinfo->satadrv_features_support |=
10266 				    SATA_DEV_F_SATA1;
10267 			}
10268 		}
10269 		if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) &&
10270 		    (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD))
10271 			sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ;
10272 
10273 		sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth;
10274 		if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) ||
10275 		    (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) {
10276 			++sdinfo->satadrv_queue_depth;
10277 			/* Adjust according to controller capabilities */
10278 			sdinfo->satadrv_max_queue_depth = MIN(
10279 			    sdinfo->satadrv_queue_depth,
10280 			    SATA_QDEPTH(sata_hba_inst));
10281 			/* Adjust according to global queue depth limit */
10282 			sdinfo->satadrv_max_queue_depth = MIN(
10283 			    sdinfo->satadrv_max_queue_depth,
10284 			    sata_current_max_qdepth);
10285 			if (sdinfo->satadrv_max_queue_depth == 0)
10286 				sdinfo->satadrv_max_queue_depth = 1;
10287 		} else
10288 			sdinfo->satadrv_max_queue_depth = 1;
10289 
10290 		rval = SATA_SUCCESS;
10291 	} else {
10292 		/*
10293 		 * Woops, no Identify Data.
10294 		 */
10295 		if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) {
10296 			rval = SATA_RETRY; /* may retry later */
10297 		} else if (rval == SATA_TRAN_ACCEPTED) {
10298 			if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR ||
10299 			    spkt->satapkt_reason == SATA_PKT_ABORTED ||
10300 			    spkt->satapkt_reason == SATA_PKT_TIMEOUT ||
10301 			    spkt->satapkt_reason == SATA_PKT_RESET)
10302 				rval = SATA_RETRY; /* may retry later */
10303 			else
10304 				rval = SATA_FAILURE;
10305 		} else {
10306 			rval = SATA_FAILURE;
10307 		}
10308 	}
10309 fail:
10310 	/* Free allocated resources */
10311 	sata_free_local_buffer(spx);
10312 	sata_pkt_free(spx);
10313 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
10314 
10315 	return (rval);
10316 }
10317 
10318 
10319 /*
10320  * Some devices may not come-up with default DMA mode (UDMA or MWDMA).
10321  * UDMA mode is checked first, followed by MWDMA mode.
10322  * set correctly, so this function is setting it to the highest supported level.
10323  * Older SATA spec required that the device supports at least DMA 4 mode and
10324  * UDMA mode is selected.  It is not mentioned in SerialATA 2.6, so this
10325  * restriction has been removed.
10326  *
10327  * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported.
10328  * Returns SATA_FAILURE if proper DMA mode could not be selected.
10329  *
10330  * NOTE: This function should be called only if DMA mode is supported.
10331  */
10332 static int
10333 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo)
10334 {
10335 	sata_pkt_t *spkt;
10336 	sata_cmd_t *scmd;
10337 	sata_pkt_txlate_t *spx;
10338 	int i, mode;
10339 	uint8_t subcmd;
10340 	int rval = SATA_SUCCESS;
10341 
10342 	ASSERT(sdinfo != NULL);
10343 	ASSERT(sata_hba_inst != NULL);
10344 
10345 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
10346 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) {
10347 		/* Find highest Ultra DMA mode supported */
10348 		for (mode = 6; mode >= 0; --mode) {
10349 			if (sdinfo->satadrv_id.ai_ultradma & (1 << mode))
10350 				break;
10351 		}
10352 #if 0
10353 		/* Left for historical reasons */
10354 		/*
10355 		 * Some initial version of SATA spec indicated that at least
10356 		 * UDMA mode 4 has to be supported. It is not mentioned in
10357 		 * SerialATA 2.6, so this restriction is removed.
10358 		 */
10359 		if (mode < 4)
10360 			return (SATA_FAILURE);
10361 #endif
10362 		/* Find UDMA mode currently selected */
10363 		for (i = 6; i >= 0; --i) {
10364 			if (sdinfo->satadrv_id.ai_ultradma & (1 << (i + 8)))
10365 				break;
10366 		}
10367 		if (i >= mode)
10368 			/* Nothing to do */
10369 			return (SATA_SUCCESS);
10370 
10371 		subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA;
10372 
10373 	} else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) {
10374 		/* Find highest MultiWord DMA mode supported */
10375 		for (mode = 2; mode >= 0; --mode) {
10376 			if (sdinfo->satadrv_id.ai_dworddma & (1 << mode))
10377 				break;
10378 		}
10379 		/* Find highest MultiWord DMA mode selected */
10380 		for (i = 2; i >= 0; --i) {
10381 			if (sdinfo->satadrv_id.ai_dworddma & (1 << (i + 8)))
10382 				break;
10383 		}
10384 		if (i >= mode)
10385 			/* Nothing to do */
10386 			return (SATA_SUCCESS);
10387 
10388 		subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA;
10389 	} else
10390 		return (SATA_SUCCESS);
10391 
10392 	/*
10393 	 * Set DMA mode via SET FEATURES COMMAND.
10394 	 * Prepare packet for SET FEATURES COMMAND.
10395 	 */
10396 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10397 	spx->txlt_sata_hba_inst = sata_hba_inst;
10398 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10399 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10400 	if (spkt == NULL) {
10401 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10402 		    "sata_set_dma_mode: could not set DMA mode %", mode));
10403 		rval = SATA_FAILURE;
10404 		goto done;
10405 	}
10406 	/* Fill sata_pkt */
10407 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10408 	/* Timeout 30s */
10409 	spkt->satapkt_time = sata_default_pkt_time;
10410 	/* Synchronous mode, no callback, interrupts */
10411 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10412 	spkt->satapkt_comp = NULL;
10413 	scmd = &spkt->satapkt_cmd;
10414 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10415 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10416 	scmd->satacmd_addr_type = 0;
10417 	scmd->satacmd_device_reg = 0;
10418 	scmd->satacmd_status_reg = 0;
10419 	scmd->satacmd_error_reg = 0;
10420 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10421 	scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE;
10422 	scmd->satacmd_sec_count_lsb = subcmd | mode;
10423 
10424 	/* Transfer command to HBA */
10425 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
10426 	    spkt) != SATA_TRAN_ACCEPTED ||
10427 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
10428 		/* Pkt execution failed */
10429 		rval = SATA_FAILURE;
10430 	}
10431 done:
10432 
10433 	/* Free allocated resources */
10434 	if (spkt != NULL)
10435 		sata_pkt_free(spx);
10436 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10437 
10438 	return (rval);
10439 }
10440 
10441 
10442 /*
10443  * Set device caching mode.
10444  * One of the following operations should be specified:
10445  * SATAC_SF_ENABLE_READ_AHEAD
10446  * SATAC_SF_DISABLE_READ_AHEAD
10447  * SATAC_SF_ENABLE_WRITE_CACHE
10448  * SATAC_SF_DISABLE_WRITE_CACHE
10449  *
10450  * If operation fails, system log messgage is emitted.
10451  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
10452  */
10453 
10454 static int
10455 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
10456     int cache_op)
10457 {
10458 	sata_pkt_t *spkt;
10459 	sata_cmd_t *scmd;
10460 	sata_pkt_txlate_t *spx;
10461 	int rval = SATA_SUCCESS;
10462 	char *infop;
10463 
10464 	ASSERT(sdinfo != NULL);
10465 	ASSERT(sata_hba_inst != NULL);
10466 	ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD ||
10467 	    cache_op == SATAC_SF_DISABLE_READ_AHEAD ||
10468 	    cache_op == SATAC_SF_ENABLE_WRITE_CACHE ||
10469 	    cache_op == SATAC_SF_DISABLE_WRITE_CACHE);
10470 
10471 
10472 	/* Prepare packet for SET FEATURES COMMAND */
10473 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10474 	spx->txlt_sata_hba_inst = sata_hba_inst;
10475 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10476 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10477 	if (spkt == NULL) {
10478 		rval = SATA_FAILURE;
10479 		goto failure;
10480 	}
10481 	/* Fill sata_pkt */
10482 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10483 	/* Timeout 30s */
10484 	spkt->satapkt_time = sata_default_pkt_time;
10485 	/* Synchronous mode, no callback, interrupts */
10486 	spkt->satapkt_op_mode =
10487 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10488 	spkt->satapkt_comp = NULL;
10489 	scmd = &spkt->satapkt_cmd;
10490 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10491 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10492 	scmd->satacmd_addr_type = 0;
10493 	scmd->satacmd_device_reg = 0;
10494 	scmd->satacmd_status_reg = 0;
10495 	scmd->satacmd_error_reg = 0;
10496 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10497 	scmd->satacmd_features_reg = cache_op;
10498 
10499 	/* Transfer command to HBA */
10500 	if (((*SATA_START_FUNC(sata_hba_inst))(
10501 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
10502 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
10503 		/* Pkt execution failed */
10504 		switch (cache_op) {
10505 		case SATAC_SF_ENABLE_READ_AHEAD:
10506 			infop = "enabling read ahead failed";
10507 			break;
10508 		case SATAC_SF_DISABLE_READ_AHEAD:
10509 			infop = "disabling read ahead failed";
10510 			break;
10511 		case SATAC_SF_ENABLE_WRITE_CACHE:
10512 			infop = "enabling write cache failed";
10513 			break;
10514 		case SATAC_SF_DISABLE_WRITE_CACHE:
10515 			infop = "disabling write cache failed";
10516 			break;
10517 		}
10518 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
10519 		rval = SATA_FAILURE;
10520 	}
10521 failure:
10522 	/* Free allocated resources */
10523 	if (spkt != NULL)
10524 		sata_pkt_free(spx);
10525 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10526 	return (rval);
10527 }
10528 
10529 /*
10530  * Set Removable Media Status Notification (enable/disable)
10531  * state == 0 , disable
10532  * state != 0 , enable
10533  *
10534  * If operation fails, system log messgage is emitted.
10535  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
10536  */
10537 
10538 static int
10539 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
10540     int state)
10541 {
10542 	sata_pkt_t *spkt;
10543 	sata_cmd_t *scmd;
10544 	sata_pkt_txlate_t *spx;
10545 	int rval = SATA_SUCCESS;
10546 	char *infop;
10547 
10548 	ASSERT(sdinfo != NULL);
10549 	ASSERT(sata_hba_inst != NULL);
10550 
10551 	/* Prepare packet for SET FEATURES COMMAND */
10552 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10553 	spx->txlt_sata_hba_inst = sata_hba_inst;
10554 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10555 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10556 	if (spkt == NULL) {
10557 		rval = SATA_FAILURE;
10558 		goto failure;
10559 	}
10560 	/* Fill sata_pkt */
10561 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10562 	/* Timeout 30s */
10563 	spkt->satapkt_time = sata_default_pkt_time;
10564 	/* Synchronous mode, no callback, interrupts */
10565 	spkt->satapkt_op_mode =
10566 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10567 	spkt->satapkt_comp = NULL;
10568 	scmd = &spkt->satapkt_cmd;
10569 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10570 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10571 	scmd->satacmd_addr_type = 0;
10572 	scmd->satacmd_device_reg = 0;
10573 	scmd->satacmd_status_reg = 0;
10574 	scmd->satacmd_error_reg = 0;
10575 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10576 	if (state == 0)
10577 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN;
10578 	else
10579 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN;
10580 
10581 	/* Transfer command to HBA */
10582 	if (((*SATA_START_FUNC(sata_hba_inst))(
10583 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
10584 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
10585 		/* Pkt execution failed */
10586 		if (state == 0)
10587 			infop = "disabling Removable Media Status "
10588 			    "Notification failed";
10589 		else
10590 			infop = "enabling Removable Media Status "
10591 			    "Notification failed";
10592 
10593 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
10594 		rval = SATA_FAILURE;
10595 	}
10596 failure:
10597 	/* Free allocated resources */
10598 	if (spkt != NULL)
10599 		sata_pkt_free(spx);
10600 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10601 	return (rval);
10602 }
10603 
10604 
10605 /*
10606  * Update port SCR block
10607  */
10608 static void
10609 sata_update_port_scr(sata_port_scr_t *port_scr, sata_device_t *device)
10610 {
10611 	port_scr->sstatus = device->satadev_scr.sstatus;
10612 	port_scr->serror = device->satadev_scr.serror;
10613 	port_scr->scontrol = device->satadev_scr.scontrol;
10614 	port_scr->sactive = device->satadev_scr.sactive;
10615 	port_scr->snotific = device->satadev_scr.snotific;
10616 }
10617 
10618 /*
10619  * Update state and copy port ss* values from passed sata_device structure.
10620  * sata_address is validated - if not valid, nothing is changed in sata_scsi
10621  * configuration struct.
10622  *
10623  * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function
10624  * regardless of the state in device argument.
10625  *
10626  * Port mutex should be held while calling this function.
10627  */
10628 static void
10629 sata_update_port_info(sata_hba_inst_t *sata_hba_inst,
10630 	sata_device_t *sata_device)
10631 {
10632 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst,
10633 	    sata_device->satadev_addr.cport)));
10634 
10635 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT ||
10636 	    sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
10637 
10638 		sata_cport_info_t *cportinfo;
10639 
10640 		if (SATA_NUM_CPORTS(sata_hba_inst) <=
10641 		    sata_device->satadev_addr.cport)
10642 			return;
10643 
10644 		cportinfo = SATA_CPORT_INFO(sata_hba_inst,
10645 		    sata_device->satadev_addr.cport);
10646 		sata_update_port_scr(&cportinfo->cport_scr, sata_device);
10647 
10648 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
10649 		cportinfo->cport_state &= ~(SATA_PSTATE_PWRON |
10650 		    SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
10651 		cportinfo->cport_state |=
10652 		    sata_device->satadev_state & SATA_PSTATE_VALID;
10653 	} else {
10654 		sata_pmport_info_t *pmportinfo;
10655 
10656 		if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT) ||
10657 		    (sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) ||
10658 		    SATA_NUM_PMPORTS(sata_hba_inst,
10659 		    sata_device->satadev_addr.cport) <
10660 		    sata_device->satadev_addr.pmport)
10661 			return;
10662 
10663 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
10664 		    sata_device->satadev_addr.cport,
10665 		    sata_device->satadev_addr.pmport);
10666 		sata_update_port_scr(&pmportinfo->pmport_scr, sata_device);
10667 
10668 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
10669 		pmportinfo->pmport_state &=
10670 		    ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF |
10671 		    SATA_PSTATE_FAILED);
10672 		pmportinfo->pmport_state |=
10673 		    sata_device->satadev_state & SATA_PSTATE_VALID;
10674 	}
10675 }
10676 
10677 
10678 
10679 /*
10680  * Extract SATA port specification from an IOCTL argument.
10681  *
10682  * This function return the port the user land send us as is, unless it
10683  * cannot retrieve port spec, then -1 is returned.
10684  *
10685  * Note: Only cport  - no port multiplier port.
10686  */
10687 static int32_t
10688 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp)
10689 {
10690 	int32_t port;
10691 
10692 	/* Extract port number from nvpair in dca structure  */
10693 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
10694 		SATA_LOG_D((sata_hba_inst, CE_NOTE,
10695 		    "sata_get_port_num: invalid port spec 0x%x in ioctl",
10696 		    port));
10697 		port = -1;
10698 	}
10699 
10700 	return (port);
10701 }
10702 
10703 /*
10704  * Get dev_info_t pointer to the device node pointed to by port argument.
10705  * NOTE: target argument is a value used in ioctls to identify
10706  * the AP - it is not a sata_address.
10707  * It is a combination of cport, pmport and address qualifier, encodded same
10708  * way as a scsi target number.
10709  * At this moment it carries only cport number.
10710  *
10711  * No PMult hotplug support.
10712  *
10713  * Returns dev_info_t pointer if target device was found, NULL otherwise.
10714  */
10715 
10716 static dev_info_t *
10717 sata_get_target_dip(dev_info_t *dip, int32_t port)
10718 {
10719 	dev_info_t	*cdip = NULL;
10720 	int		target, tgt;
10721 	int		ncport;
10722 	int 		circ;
10723 
10724 	ncport = port & SATA_CFGA_CPORT_MASK;
10725 	target = SATA_TO_SCSI_TARGET(ncport, 0, SATA_ADDR_DCPORT);
10726 
10727 	ndi_devi_enter(dip, &circ);
10728 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
10729 		dev_info_t *next = ddi_get_next_sibling(cdip);
10730 
10731 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
10732 		    DDI_PROP_DONTPASS, "target", -1);
10733 		if (tgt == -1) {
10734 			/*
10735 			 * This is actually an error condition, but not
10736 			 * a fatal one. Just continue the search.
10737 			 */
10738 			cdip = next;
10739 			continue;
10740 		}
10741 
10742 		if (tgt == target)
10743 			break;
10744 
10745 		cdip = next;
10746 	}
10747 	ndi_devi_exit(dip, circ);
10748 
10749 	return (cdip);
10750 }
10751 
10752 /*
10753  * Get dev_info_t pointer to the device node pointed to by port argument.
10754  * NOTE: target argument is a value used in ioctls to identify
10755  * the AP - it is not a sata_address.
10756  * It is a combination of cport, pmport and address qualifier, encoded same
10757  * way as a scsi target number.
10758  * At this moment it carries only cport number.
10759  *
10760  * No PMult hotplug support.
10761  *
10762  * Returns dev_info_t pointer if target device was found, NULL otherwise.
10763  */
10764 
10765 static dev_info_t *
10766 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr)
10767 {
10768 	dev_info_t	*cdip = NULL;
10769 	int		target, tgt;
10770 	int 		circ;
10771 
10772 	target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual);
10773 
10774 	ndi_devi_enter(dip, &circ);
10775 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
10776 		dev_info_t *next = ddi_get_next_sibling(cdip);
10777 
10778 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
10779 		    DDI_PROP_DONTPASS, "target", -1);
10780 		if (tgt == -1) {
10781 			/*
10782 			 * This is actually an error condition, but not
10783 			 * a fatal one. Just continue the search.
10784 			 */
10785 			cdip = next;
10786 			continue;
10787 		}
10788 
10789 		if (tgt == target)
10790 			break;
10791 
10792 		cdip = next;
10793 	}
10794 	ndi_devi_exit(dip, circ);
10795 
10796 	return (cdip);
10797 }
10798 
10799 /*
10800  * Process sata port disconnect request.
10801  * Normally, cfgadm sata plugin will try to offline (unconfigure) the device
10802  * before this request. Nevertheless, if a device is still configured,
10803  * we need to attempt to offline and unconfigure device.
10804  * Regardless of the unconfigure operation results the port is marked as
10805  * deactivated and no access to the attached device is possible.
10806  * If the target node remains because unconfigure operation failed, its state
10807  * will be set to DEVICE_REMOVED, preventing it to be used again when a device
10808  * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure
10809  * the device and remove old target node.
10810  *
10811  * This function invokes sata_hba_inst->satahba_tran->
10812  * sata_tran_hotplug_ops->sata_tran_port_deactivate().
10813  * If successful, the device structure (if any) attached to the specified port
10814  * is removed and state of the port marked appropriately.
10815  * Failure of the port_deactivate may keep port in the physically active state,
10816  * or may fail the port.
10817  *
10818  * NOTE: Port multiplier code is not completed nor tested.
10819  */
10820 
10821 static int
10822 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst,
10823     sata_device_t *sata_device)
10824 {
10825 	sata_drive_info_t *sdinfo = NULL;
10826 	sata_cport_info_t *cportinfo = NULL;
10827 	sata_pmport_info_t *pmportinfo = NULL;
10828 	sata_pmult_info_t *pmultinfo = NULL;
10829 	dev_info_t *tdip;
10830 	int cport, pmport, qual;
10831 	int rval = SATA_SUCCESS;
10832 	int rv = 0;
10833 
10834 	cport = sata_device->satadev_addr.cport;
10835 	pmport = sata_device->satadev_addr.pmport;
10836 	qual = sata_device->satadev_addr.qual;
10837 
10838 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
10839 
10840 	/*
10841 	 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran->
10842 	 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
10843 	 * Do the sanity check.
10844 	 */
10845 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
10846 		/* No physical port deactivation supported. */
10847 		return (EINVAL);
10848 	}
10849 
10850 	/* Check the current state of the port */
10851 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10852 	    (SATA_DIP(sata_hba_inst), sata_device);
10853 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10854 	sata_update_port_info(sata_hba_inst, sata_device);
10855 	if (rval != SATA_SUCCESS ||
10856 	    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
10857 		/* Device port status is unknown or it is in failed state */
10858 		if (qual == SATA_ADDR_PMPORT) {
10859 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
10860 			    SATA_PSTATE_FAILED;
10861 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
10862 			    "sata_hba_ioctl: connect: failed to deactivate "
10863 			    "SATA port %d", cport);
10864 		} else {
10865 			SATA_CPORT_STATE(sata_hba_inst, cport) =
10866 			    SATA_PSTATE_FAILED;
10867 			SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
10868 			    "sata_hba_ioctl: connect: failed to deactivate "
10869 			    "SATA port %d:%d", cport, pmport);
10870 		}
10871 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
10872 		    cport)->cport_mutex);
10873 		return (EIO);
10874 	}
10875 	/*
10876 	 * Set port's dev_state to not ready - this will disable
10877 	 * an access to a potentially attached device.
10878 	 */
10879 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
10880 	if (qual == SATA_ADDR_PMPORT) {
10881 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
10882 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
10883 			sdinfo = pmportinfo->pmport_sata_drive;
10884 			ASSERT(sdinfo != NULL);
10885 		}
10886 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
10887 	} else {
10888 		/* Assuming cport */
10889 
10890 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
10891 			if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
10892 				pmultinfo =
10893 				    cportinfo->cport_devp.cport_sata_pmult;
10894 				ASSERT(pmultinfo != NULL);
10895 			} else {
10896 				sdinfo = cportinfo->cport_devp.cport_sata_drive;
10897 			}
10898 		}
10899 		cportinfo->cport_state &= ~SATA_STATE_READY;
10900 	}
10901 	if (sdinfo != NULL) {
10902 		if ((sdinfo->satadrv_type & (SATA_VALID_DEV_TYPE)) != 0) {
10903 			/*
10904 			 * If a target node exists, try to offline
10905 			 * a device and remove target node.
10906 			 */
10907 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
10908 			    cport)->cport_mutex);
10909 			/* We are addressing attached device, not a port */
10910 			sata_device->satadev_addr.qual =
10911 			    sdinfo->satadrv_addr.qual;
10912 			tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
10913 			    &sata_device->satadev_addr);
10914 			if (tdip != NULL && ndi_devi_offline(tdip,
10915 			    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
10916 				/*
10917 				 * Problem
10918 				 * The target node remained attached.
10919 				 * This happens when the device file was open
10920 				 * or a node was waiting for resources.
10921 				 * Cannot do anything about it.
10922 				 */
10923 				if (qual == SATA_ADDR_CPORT) {
10924 					SATA_LOG_D((sata_hba_inst, CE_WARN,
10925 					    "sata_hba_ioctl: disconnect: could "
10926 					    "not unconfigure device before "
10927 					    "disconnecting the SATA port %d",
10928 					    cport));
10929 				} else {
10930 					SATA_LOG_D((sata_hba_inst, CE_WARN,
10931 					    "sata_hba_ioctl: disconnect: could "
10932 					    "not unconfigure device before "
10933 					    "disconnecting the SATA port %d:%d",
10934 					    cport, pmport));
10935 				}
10936 				/*
10937 				 * Set DEVICE REMOVED state in the target
10938 				 * node. It will prevent access to the device
10939 				 * even when a new device is attached, until
10940 				 * the old target node is released, removed and
10941 				 * recreated for a new  device.
10942 				 */
10943 				sata_set_device_removed(tdip);
10944 
10945 				/*
10946 				 * Instruct event daemon to try the target
10947 				 * node cleanup later.
10948 				 */
10949 				sata_set_target_node_cleanup(
10950 				    sata_hba_inst, &sata_device->satadev_addr);
10951 			}
10952 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
10953 			    cport)->cport_mutex);
10954 		}
10955 
10956 		/* Remove and release sata_drive info structure. */
10957 		if (pmportinfo != NULL) {
10958 			SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport) =
10959 			    NULL;
10960 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
10961 		} else {
10962 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
10963 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
10964 		}
10965 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
10966 	}
10967 #if 0
10968 	else if (pmultinfo != NULL) {
10969 		/*
10970 		 * Port Multiplier itself needs special handling.
10971 		 * All device ports need to be processed here!
10972 		 */
10973 	}
10974 #endif
10975 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10976 	/* Just ask HBA driver to deactivate port */
10977 	/*	sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; */
10978 
10979 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
10980 	    (SATA_DIP(sata_hba_inst), sata_device);
10981 
10982 	/*
10983 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
10984 	 * without the hint (to force listener to investivate the state).
10985 	 */
10986 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
10987 	    SE_NO_HINT);
10988 
10989 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10990 	sata_update_port_info(sata_hba_inst, sata_device);
10991 
10992 	if (rval != SATA_SUCCESS) {
10993 		/*
10994 		 * Port deactivation failure - do not
10995 		 * change port state unless the state
10996 		 * returned by HBA indicates a port failure.
10997 		 * NOTE: device structures were released, so devices now are
10998 		 * invisible! Port reset is needed to re-enumerate devices.
10999 		 */
11000 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11001 			if (pmportinfo != NULL)
11002 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
11003 			else
11004 				cportinfo->cport_state = SATA_PSTATE_FAILED;
11005 			rv = EIO;
11006 		}
11007 	} else {
11008 		/*
11009 		 * Deactivation succeded. From now on the sata framework
11010 		 * will not care what is happening to the device, until
11011 		 * the port is activated again.
11012 		 */
11013 		cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
11014 	}
11015 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11016 	return (rv);
11017 }
11018 
11019 
11020 
11021 /*
11022  * Process sata port connect request
11023  * The sata cfgadm pluging will invoke this operation only if port was found
11024  * in the disconnect state (failed state is also treated as the disconnected
11025  * state).
11026  * DEVCTL_AP_CONNECT would invoke  sata_hba_inst->satahba_tran->
11027  * sata_tran_hotplug_ops->sata_tran_port_activate().
11028  * If successful and a device is found attached to the port,
11029  * the initialization sequence is executed to attach a device structure to
11030  * a port structure. The state of the port and a device would be set
11031  * appropriately.
11032  * The device is not set in configured state (system-wise) by this operation.
11033  *
11034  * Note, that activating the port may generate link events,
11035  * so it is important that following processing and the
11036  * event processing does not interfere with each other!
11037  *
11038  * This operation may remove port failed state and will
11039  * try to make port active and in good standing.
11040  *
11041  * NOTE: Port multiplier code is not completed nor tested.
11042  */
11043 
11044 static int
11045 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst,
11046     sata_device_t *sata_device)
11047 {
11048 	int cport, pmport, qual;
11049 	int rv = 0;
11050 
11051 	cport = sata_device->satadev_addr.cport;
11052 	pmport = sata_device->satadev_addr.pmport;
11053 	qual = sata_device->satadev_addr.qual;
11054 
11055 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
11056 
11057 	/*
11058 	 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->
11059 	 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate().
11060 	 * Perform sanity check now.
11061 	 */
11062 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) {
11063 		/* No physical port activation supported. */
11064 		return (EINVAL);
11065 	}
11066 
11067 	/* Just ask HBA driver to activate port */
11068 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11069 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11070 		/*
11071 		 * Port activation failure.
11072 		 */
11073 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11074 		    cport)->cport_mutex);
11075 		sata_update_port_info(sata_hba_inst, sata_device);
11076 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11077 			if (qual == SATA_ADDR_DCPORT) {
11078 				SATA_CPORT_STATE(sata_hba_inst, cport) =
11079 				    SATA_PSTATE_FAILED;
11080 				SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
11081 				    "sata_hba_ioctl: connect: failed to "
11082 				    "activate SATA port %d", cport);
11083 			} else { /* port multiplier device port */
11084 				SATA_PMPORT_STATE(sata_hba_inst, cport,
11085 				    pmport) = SATA_PSTATE_FAILED;
11086 				SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
11087 				    "sata_hba_ioctl: connect: failed to "
11088 				    "activate SATA port %d:%d", cport, pmport);
11089 
11090 			}
11091 		}
11092 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11093 		    cport)->cport_mutex);
11094 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
11095 		    "sata_hba_ioctl: connect: failed to activate SATA "
11096 		    "port %d:%d", cport, pmport);
11097 		return (EIO);
11098 	}
11099 
11100 	/* Virgin port state - will be updated by the port re-probe. */
11101 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11102 	if (qual == SATA_ADDR_CPORT)
11103 		SATA_CPORT_STATE(sata_hba_inst, cport) = 0;
11104 	else /* port multiplier device port */
11105 		SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0;
11106 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11107 
11108 	/*
11109 	 * Probe the port to find its state and attached device.
11110 	 */
11111 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11112 	    SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE)
11113 		rv = EIO;
11114 
11115 	/*
11116 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11117 	 * without the hint
11118 	 */
11119 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11120 	    SE_NO_HINT);
11121 
11122 	/*
11123 	 * If there is a device attached to the port, emit
11124 	 * a message.
11125 	 */
11126 	if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11127 
11128 		if (qual == SATA_ADDR_CPORT) {
11129 			sata_log(sata_hba_inst, CE_WARN,
11130 			    "SATA device detected at port %d", cport);
11131 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11132 				/*
11133 				 * A device was not successfully identified
11134 				 */
11135 				sata_log(sata_hba_inst, CE_WARN,
11136 				    "Could not identify SATA "
11137 				    "device at port %d", cport);
11138 			}
11139 		} else { /* port multiplier device port */
11140 			sata_log(sata_hba_inst, CE_WARN,
11141 			    "SATA device detected at port %d:%d",
11142 			    cport, pmport);
11143 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11144 				/*
11145 				 * A device was not successfully identified
11146 				 */
11147 				sata_log(sata_hba_inst, CE_WARN,
11148 				    "Could not identify SATA "
11149 				    "device at port %d:%d", cport, pmport);
11150 			}
11151 		}
11152 	}
11153 
11154 	return (rv);
11155 }
11156 
11157 
11158 /*
11159  * Process sata device unconfigure request.
11160  * The unconfigure operation uses generic nexus operation to
11161  * offline a device. It leaves a target device node attached.
11162  * and obviously sata_drive_info attached as well, because
11163  * from the hardware point of view nothing has changed.
11164  */
11165 static int
11166 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst,
11167     sata_device_t *sata_device)
11168 {
11169 	int rv = 0;
11170 	dev_info_t *tdip;
11171 
11172 	/* We are addressing attached device, not a port */
11173 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
11174 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11175 	else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT)
11176 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11177 
11178 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11179 	    &sata_device->satadev_addr)) != NULL) {
11180 
11181 		if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) {
11182 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11183 			    "sata_hba_ioctl: unconfigure: "
11184 			    "failed to unconfigure device at SATA port %d:%d",
11185 			    sata_device->satadev_addr.cport,
11186 			    sata_device->satadev_addr.pmport));
11187 			rv = EIO;
11188 		}
11189 		/*
11190 		 * The target node devi_state should be marked with
11191 		 * DEVI_DEVICE_OFFLINE by ndi_devi_offline().
11192 		 * This would be the indication for cfgadm that
11193 		 * the AP node occupant state is 'unconfigured'.
11194 		 */
11195 
11196 	} else {
11197 		/*
11198 		 * This would indicate a failure on the part of cfgadm
11199 		 * to detect correct state of the node prior to this
11200 		 * call - one cannot unconfigure non-existing device.
11201 		 */
11202 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11203 		    "sata_hba_ioctl: unconfigure: "
11204 		    "attempt to unconfigure non-existing device "
11205 		    "at SATA port %d:%d",
11206 		    sata_device->satadev_addr.cport,
11207 		    sata_device->satadev_addr.pmport));
11208 		rv = ENXIO;
11209 	}
11210 	return (rv);
11211 }
11212 
11213 /*
11214  * Process sata device configure request
11215  * If port is in a failed state, operation is aborted - one has to use
11216  * an explicit connect or port activate request to try to get a port into
11217  * non-failed mode. Port reset wil also work in such situation.
11218  * If the port is in disconnected (shutdown) state, the connect operation is
11219  * attempted prior to any other action.
11220  * When port is in the active state, there is a device attached and the target
11221  * node exists, a device was most likely offlined.
11222  * If target node does not exist, a new target node is created. In both cases
11223  * an attempt is made to online (configure) the device.
11224  *
11225  * NOTE: Port multiplier code is not completed nor tested.
11226  */
11227 static int
11228 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst,
11229     sata_device_t *sata_device)
11230 {
11231 	int cport, pmport, qual;
11232 	int rval;
11233 	boolean_t target = TRUE;
11234 	sata_cport_info_t *cportinfo;
11235 	sata_pmport_info_t *pmportinfo = NULL;
11236 	dev_info_t *tdip;
11237 	sata_drive_info_t *sdinfo;
11238 
11239 	cport = sata_device->satadev_addr.cport;
11240 	pmport = sata_device->satadev_addr.pmport;
11241 	qual = sata_device->satadev_addr.qual;
11242 
11243 	/* Get current port state */
11244 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11245 	    (SATA_DIP(sata_hba_inst), sata_device);
11246 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11247 	sata_update_port_info(sata_hba_inst, sata_device);
11248 
11249 	if (rval != SATA_SUCCESS ||
11250 	    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
11251 		/*
11252 		 * Obviously, device on a failed port is not visible
11253 		 */
11254 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11255 		return (ENXIO);
11256 	}
11257 
11258 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11259 	if (qual == SATA_ADDR_PMPORT)
11260 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11261 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11262 
11263 	if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) {
11264 		/* need to activate port */
11265 		target = FALSE;
11266 
11267 		/* Sanity check */
11268 		if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
11269 			return (ENXIO);
11270 
11271 		/* Just let HBA driver to activate port */
11272 		if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11273 		    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11274 			/*
11275 			 * Port activation failure - do not change port state
11276 			 * unless the state returned by HBA indicates a port
11277 			 * failure.
11278 			 */
11279 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11280 			    cport)->cport_mutex);
11281 			sata_update_port_info(sata_hba_inst, sata_device);
11282 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11283 				if (qual == SATA_ADDR_PMPORT)
11284 					pmportinfo->pmport_state =
11285 					    SATA_PSTATE_FAILED;
11286 				else
11287 					cportinfo->cport_state =
11288 					    SATA_PSTATE_FAILED;
11289 			}
11290 			mutex_exit(&SATA_CPORT_INFO(
11291 			    sata_hba_inst, cport)->cport_mutex);
11292 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11293 			    "sata_hba_ioctl: configure: "
11294 			    "failed to activate SATA port %d:%d",
11295 			    cport, pmport));
11296 			return (EIO);
11297 		}
11298 		/*
11299 		 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11300 		 * without the hint.
11301 		 */
11302 		sata_gen_sysevent(sata_hba_inst,
11303 		    &sata_device->satadev_addr, SE_NO_HINT);
11304 
11305 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11306 		    cport_mutex);
11307 		/* Virgin port state */
11308 		if (qual == SATA_ADDR_PMPORT)
11309 			pmportinfo->pmport_state = 0;
11310 		else
11311 			cportinfo->cport_state = 0;
11312 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11313 	}
11314 	/*
11315 	 * Always reprobe port, to get current device info.
11316 	 */
11317 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11318 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
11319 		return (EIO);
11320 
11321 	if (sata_device->satadev_type != SATA_DTYPE_NONE && target == FALSE) {
11322 		if (qual == SATA_ADDR_PMPORT) {
11323 			/*
11324 			 * That's the transition from "inactive" port
11325 			 * to active one with device attached.
11326 			 */
11327 			sata_log(sata_hba_inst, CE_WARN,
11328 			    "SATA device detected at port %d:%d",
11329 			    cport, pmport);
11330 		} else {
11331 			/*
11332 			 * When PM is attached to the cport and cport is
11333 			 * activated, every PM device port needs to be reprobed.
11334 			 * We need to emit message for all devices detected
11335 			 * at port multiplier's device ports.
11336 			 * Add such code here.
11337 			 * For now, just inform about device attached to
11338 			 * cport.
11339 			 */
11340 			sata_log(sata_hba_inst, CE_WARN,
11341 			    "SATA device detected at port %d", cport);
11342 		}
11343 	}
11344 
11345 	/*
11346 	 * This is where real configuration operation starts.
11347 	 *
11348 	 * When PM is attached to the cport and cport is activated,
11349 	 * devices attached PM device ports may have to be configured
11350 	 * explicitly. This may change when port multiplier is supported.
11351 	 * For now, configure only disks and other valid target devices.
11352 	 */
11353 	if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) {
11354 		if (qual == SATA_ADDR_CPORT) {
11355 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11356 				/*
11357 				 * A device was not successfully identified
11358 				 */
11359 				sata_log(sata_hba_inst, CE_WARN,
11360 				    "Could not identify SATA "
11361 				    "device at port %d", cport);
11362 			}
11363 		} else { /* port multiplier device port */
11364 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11365 				/*
11366 				 * A device was not successfully identified
11367 				 */
11368 				sata_log(sata_hba_inst, CE_WARN,
11369 				    "Could not identify SATA "
11370 				    "device at port %d:%d", cport, pmport);
11371 			}
11372 		}
11373 		return (ENXIO);		/* No device to configure */
11374 	}
11375 
11376 	/*
11377 	 * Here we may have a device in reset condition,
11378 	 * but because we are just configuring it, there is
11379 	 * no need to process the reset other than just
11380 	 * to clear device reset condition in the HBA driver.
11381 	 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will
11382 	 * cause a first command sent the HBA driver with the request
11383 	 * to clear device reset condition.
11384 	 */
11385 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11386 	if (qual == SATA_ADDR_PMPORT)
11387 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11388 	else
11389 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11390 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
11391 	if (sdinfo == NULL) {
11392 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11393 		return (ENXIO);
11394 	}
11395 	if (sdinfo->satadrv_event_flags &
11396 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
11397 		sdinfo->satadrv_event_flags = 0;
11398 	}
11399 	sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
11400 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11401 
11402 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11403 	    &sata_device->satadev_addr)) != NULL) {
11404 		/*
11405 		 * Target node exists. Verify, that it belongs
11406 		 * to existing, attached device and not to
11407 		 * a removed device.
11408 		 */
11409 		if (sata_check_device_removed(tdip) == B_TRUE) {
11410 			if (qual == SATA_ADDR_DPMPORT)
11411 				sata_log(sata_hba_inst, CE_WARN,
11412 				    "SATA device at port %d cannot be "
11413 				    "configured. "
11414 				    "Application(s) accessing "
11415 				    "previously attached device "
11416 				    "have to release it before newly "
11417 				    "inserted device can be made accessible.",
11418 				    cport);
11419 			else
11420 				sata_log(sata_hba_inst, CE_WARN,
11421 				    "SATA device at port %d:%d cannot be"
11422 				    "configured. "
11423 				    "Application(s) accessing "
11424 				    "previously attached device "
11425 				    "have to release it before newly "
11426 				    "inserted device can be made accessible.",
11427 				    cport, pmport);
11428 			return (EIO);
11429 		}
11430 		/*
11431 		 * Device was not removed and re-inserted.
11432 		 * Try to online it.
11433 		 */
11434 		if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) {
11435 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11436 			    "sata_hba_ioctl: configure: "
11437 			    "onlining device at SATA port "
11438 			    "%d:%d failed", cport, pmport));
11439 			return (EIO);
11440 		}
11441 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11442 		    cport)->cport_mutex);
11443 
11444 		if (qual == SATA_ADDR_DPMPORT)
11445 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
11446 		else
11447 			cportinfo-> cport_tgtnode_clean = B_TRUE;
11448 
11449 		mutex_exit(&SATA_CPORT_INFO(
11450 		    sata_hba_inst, cport)->cport_mutex);
11451 	} else {
11452 		/*
11453 		 * No target node - need to create a new target node.
11454 		 */
11455 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11456 		    cport_mutex);
11457 		if (qual == SATA_ADDR_DPMPORT)
11458 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
11459 		else
11460 			cportinfo-> cport_tgtnode_clean = B_TRUE;
11461 
11462 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11463 		    cport_mutex);
11464 		tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
11465 		    sata_hba_inst, &sata_device->satadev_addr);
11466 		if (tdip == NULL) {
11467 			/* Configure operation failed */
11468 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11469 			    "sata_hba_ioctl: configure: "
11470 			    "configuring SATA device at port %d:%d "
11471 			    "failed", cport, pmport));
11472 			return (EIO);
11473 		}
11474 	}
11475 	return (0);
11476 }
11477 
11478 
11479 /*
11480  * Process ioctl deactivate port request.
11481  * Arbitrarily unconfigure attached device, if any.
11482  * Even if the unconfigure fails, proceed with the
11483  * port deactivation.
11484  *
11485  * NOTE: Port Multiplier code is not completed and tested.
11486  */
11487 
11488 static int
11489 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst,
11490     sata_device_t *sata_device)
11491 {
11492 	int cport, pmport, qual;
11493 	int rval, rv = 0;
11494 	sata_cport_info_t *cportinfo;
11495 	sata_pmport_info_t *pmportinfo = NULL;
11496 	dev_info_t *tdip;
11497 	sata_drive_info_t *sdinfo = NULL;
11498 
11499 	/* Sanity check */
11500 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL)
11501 		return (ENOTSUP);
11502 
11503 	cport = sata_device->satadev_addr.cport;
11504 	pmport = sata_device->satadev_addr.pmport;
11505 	qual = sata_device->satadev_addr.qual;
11506 
11507 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11508 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11509 	if (qual == SATA_ADDR_CPORT) {
11510 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11511 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
11512 			/*
11513 			 * For now, assume that port multiplier is not
11514 			 * supported, i.e. deal only with valid devices
11515 			 */
11516 			if ((cportinfo->cport_dev_type &
11517 			    SATA_VALID_DEV_TYPE) != 0)
11518 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11519 			/*
11520 			 * If attached device is a port multiplier, we will
11521 			 * have to unconfigure all devices attached to the
11522 			 * port multiplier. Add this code here.
11523 			 */
11524 		}
11525 		cportinfo->cport_state &= ~SATA_STATE_READY;
11526 	} else {
11527 		/* Port multiplier device port */
11528 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11529 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11530 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
11531 		    (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0)
11532 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11533 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
11534 	}
11535 
11536 	if (sdinfo != NULL) {
11537 		/*
11538 		 * If a target node exists, try to offline a device and
11539 		 * to remove a target node.
11540 		 */
11541 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11542 		    cport_mutex);
11543 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11544 		    &sata_device->satadev_addr);
11545 		if (tdip != NULL) {
11546 			/* target node exist */
11547 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
11548 			    "sata_hba_ioctl: port deactivate: "
11549 			    "target node exists.", NULL);
11550 
11551 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) !=
11552 			    NDI_SUCCESS) {
11553 				SATA_LOG_D((sata_hba_inst, CE_WARN,
11554 				    "sata_hba_ioctl: port deactivate: "
11555 				    "failed to unconfigure device at port "
11556 				    "%d:%d before deactivating the port",
11557 				    cport, pmport));
11558 				/*
11559 				 * Set DEVICE REMOVED state in the target
11560 				 * node. It will prevent an access to
11561 				 * the device even when a new device is
11562 				 * attached, until the old target node is
11563 				 * released, removed and recreated for a new
11564 				 * device.
11565 				 */
11566 				sata_set_device_removed(tdip);
11567 
11568 				/*
11569 				 * Instruct the event daemon to try the
11570 				 * target node cleanup later.
11571 				 */
11572 				sata_set_target_node_cleanup(sata_hba_inst,
11573 				    &sata_device->satadev_addr);
11574 			}
11575 		}
11576 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11577 		    cport_mutex);
11578 		/*
11579 		 * In any case, remove and release sata_drive_info
11580 		 * structure.
11581 		 */
11582 		if (qual == SATA_ADDR_CPORT) {
11583 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11584 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11585 		} else { /* port multiplier device port */
11586 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11587 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11588 		}
11589 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
11590 	}
11591 	if (qual == SATA_ADDR_CPORT) {
11592 		cportinfo->cport_state &= ~(SATA_STATE_PROBED |
11593 		    SATA_STATE_PROBING);
11594 	} else { /* port multiplier device port */
11595 		pmportinfo->pmport_state &= ~(SATA_STATE_PROBED |
11596 		    SATA_STATE_PROBING);
11597 	}
11598 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11599 
11600 	/* Just let HBA driver to deactivate port */
11601 	sata_device->satadev_addr.qual = qual;
11602 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
11603 	    (SATA_DIP(sata_hba_inst), sata_device);
11604 
11605 	/*
11606 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11607 	 * without the hint
11608 	 */
11609 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11610 	    SE_NO_HINT);
11611 
11612 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11613 	sata_update_port_info(sata_hba_inst, sata_device);
11614 	if (qual == SATA_ADDR_CPORT) {
11615 		if (rval != SATA_SUCCESS) {
11616 			/*
11617 			 * Port deactivation failure - do not change port state
11618 			 * unless the state returned by HBA indicates a port
11619 			 * failure.
11620 			 */
11621 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11622 				SATA_CPORT_STATE(sata_hba_inst, cport) =
11623 				    SATA_PSTATE_FAILED;
11624 			}
11625 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11626 			    "sata_hba_ioctl: port deactivate: "
11627 			    "cannot deactivate SATA port %d", cport));
11628 			rv = EIO;
11629 		} else {
11630 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
11631 		}
11632 	} else {
11633 		if (rval != SATA_SUCCESS) {
11634 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11635 				SATA_PMPORT_STATE(sata_hba_inst, cport,
11636 				    pmport) = SATA_PSTATE_FAILED;
11637 			}
11638 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11639 			    "sata_hba_ioctl: port deactivate: "
11640 			    "cannot deactivate SATA port %d:%d",
11641 			    cport, pmport));
11642 			rv = EIO;
11643 		} else {
11644 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
11645 		}
11646 	}
11647 
11648 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11649 
11650 	return (rv);
11651 }
11652 
11653 /*
11654  * Process ioctl port activate request.
11655  *
11656  * NOTE: Port multiplier code is not completed nor tested.
11657  */
11658 static int
11659 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst,
11660     sata_device_t *sata_device)
11661 {
11662 	int cport, pmport, qual;
11663 	sata_cport_info_t *cportinfo;
11664 	sata_pmport_info_t *pmportinfo = NULL;
11665 	boolean_t dev_existed = TRUE;
11666 
11667 	/* Sanity check */
11668 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
11669 		return (ENOTSUP);
11670 
11671 	cport = sata_device->satadev_addr.cport;
11672 	pmport = sata_device->satadev_addr.pmport;
11673 	qual = sata_device->satadev_addr.qual;
11674 
11675 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11676 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11677 	if (qual == SATA_ADDR_PMPORT) {
11678 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11679 		if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN ||
11680 		    pmportinfo->pmport_dev_type == SATA_DTYPE_NONE)
11681 			dev_existed = FALSE;
11682 	} else { /* cport */
11683 		if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN ||
11684 		    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
11685 			dev_existed = FALSE;
11686 	}
11687 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11688 
11689 	/* Just let HBA driver to activate port, if necessary */
11690 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11691 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11692 		/*
11693 		 * Port activation failure - do not change port state unless
11694 		 * the state returned by HBA indicates a port failure.
11695 		 */
11696 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11697 		    cport)->cport_mutex);
11698 		sata_update_port_info(sata_hba_inst, sata_device);
11699 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11700 			if (qual == SATA_ADDR_PMPORT)
11701 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
11702 			else
11703 				cportinfo->cport_state = SATA_PSTATE_FAILED;
11704 
11705 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11706 			    cport)->cport_mutex);
11707 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11708 			    "sata_hba_ioctl: port activate: cannot activate "
11709 			    "SATA port %d:%d", cport, pmport));
11710 			return (EIO);
11711 		}
11712 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11713 	}
11714 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11715 	if (qual == SATA_ADDR_PMPORT)
11716 		pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN;
11717 	else
11718 		cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN;
11719 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11720 
11721 	/*
11722 	 * Re-probe port to find its current state and possibly attached device.
11723 	 * Port re-probing may change the cportinfo device type if device is
11724 	 * found attached.
11725 	 * If port probing failed, the device type would be set to
11726 	 * SATA_DTYPE_NONE.
11727 	 */
11728 	(void) sata_reprobe_port(sata_hba_inst, sata_device,
11729 	    SATA_DEV_IDENTIFY_RETRY);
11730 
11731 	/*
11732 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11733 	 * without the hint.
11734 	 */
11735 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11736 	    SE_NO_HINT);
11737 
11738 	if (dev_existed == FALSE) {
11739 		if (qual == SATA_ADDR_PMPORT &&
11740 		    pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
11741 			/*
11742 			 * That's the transition from the "inactive" port state
11743 			 * or the active port without a device attached to the
11744 			 * active port state with a device attached.
11745 			 */
11746 			sata_log(sata_hba_inst, CE_WARN,
11747 			    "SATA device detected at port %d:%d",
11748 			    cport, pmport);
11749 		} else if (qual == SATA_ADDR_CPORT &&
11750 		    cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
11751 			/*
11752 			 * That's the transition from the "inactive" port state
11753 			 * or the active port without a device attached to the
11754 			 * active port state with a device attached.
11755 			 */
11756 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
11757 				sata_log(sata_hba_inst, CE_WARN,
11758 				    "SATA device detected at port %d", cport);
11759 			} else {
11760 				sata_log(sata_hba_inst, CE_WARN,
11761 				    "SATA port multiplier detected at port %d",
11762 				    cport);
11763 				/*
11764 				 * Because the detected device is a port
11765 				 * multiplier, we need to reprobe every device
11766 				 * port on the port multiplier and show every
11767 				 * device found attached.
11768 				 * Add this code here.
11769 				 */
11770 			}
11771 		}
11772 	}
11773 	return (0);
11774 }
11775 
11776 
11777 
11778 /*
11779  * Process ioctl reset port request.
11780  *
11781  * NOTE: Port multiplier code is not completed nor tested.
11782  */
11783 static int
11784 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst,
11785     sata_device_t *sata_device)
11786 {
11787 	int cport, pmport, qual;
11788 	int rv = 0;
11789 
11790 	cport = sata_device->satadev_addr.cport;
11791 	pmport = sata_device->satadev_addr.pmport;
11792 	qual = sata_device->satadev_addr.qual;
11793 
11794 	/* Sanity check */
11795 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
11796 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11797 		    "sata_hba_ioctl: sata_hba_tran missing required "
11798 		    "function sata_tran_reset_dport"));
11799 		return (ENOTSUP);
11800 	}
11801 
11802 	/* Ask HBA to reset port */
11803 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
11804 	    sata_device) != SATA_SUCCESS) {
11805 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11806 		    "sata_hba_ioctl: reset port: failed %d:%d",
11807 		    cport, pmport));
11808 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11809 		    cport_mutex);
11810 		sata_update_port_info(sata_hba_inst, sata_device);
11811 		if (qual == SATA_ADDR_CPORT)
11812 			SATA_CPORT_STATE(sata_hba_inst, cport) =
11813 			    SATA_PSTATE_FAILED;
11814 		else
11815 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
11816 			    SATA_PSTATE_FAILED;
11817 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11818 		    cport_mutex);
11819 		rv = EIO;
11820 	}
11821 	/*
11822 	 * Beacuse the port was reset, it should be probed and
11823 	 * attached device reinitialized. At this point the
11824 	 * port state is unknown - it's state is HBA-specific.
11825 	 * Re-probe port to get its state.
11826 	 */
11827 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11828 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) {
11829 		rv = EIO;
11830 	}
11831 	return (rv);
11832 }
11833 
11834 /*
11835  * Process ioctl reset device request.
11836  *
11837  * NOTE: Port multiplier code is not completed nor tested.
11838  */
11839 static int
11840 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst,
11841     sata_device_t *sata_device)
11842 {
11843 	sata_drive_info_t *sdinfo;
11844 	int cport, pmport;
11845 	int rv = 0;
11846 
11847 	/* Sanity check */
11848 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
11849 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11850 		    "sata_hba_ioctl: sata_hba_tran missing required "
11851 		    "function sata_tran_reset_dport"));
11852 		return (ENOTSUP);
11853 	}
11854 
11855 	cport = sata_device->satadev_addr.cport;
11856 	pmport = sata_device->satadev_addr.pmport;
11857 
11858 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11859 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) {
11860 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11861 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
11862 		    sata_device->satadev_addr.cport);
11863 	} else { /* port multiplier */
11864 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11865 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
11866 		    sata_device->satadev_addr.cport,
11867 		    sata_device->satadev_addr.pmport);
11868 	}
11869 	if (sdinfo == NULL) {
11870 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11871 		return (EINVAL);
11872 	}
11873 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11874 
11875 	/* Ask HBA to reset device */
11876 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
11877 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11878 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11879 		    "sata_hba_ioctl: reset device: failed at port %d:%d",
11880 		    cport, pmport));
11881 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11882 		    cport_mutex);
11883 		sata_update_port_info(sata_hba_inst, sata_device);
11884 		/*
11885 		 * Device info structure remains attached. Another device reset
11886 		 * or port disconnect/connect and re-probing is
11887 		 * needed to change it's state
11888 		 */
11889 		sdinfo->satadrv_state &= ~SATA_STATE_READY;
11890 		sdinfo->satadrv_state |= SATA_DSTATE_FAILED;
11891 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11892 		rv = EIO;
11893 	}
11894 	/*
11895 	 * If attached device was a port multiplier, some extra processing
11896 	 * may be needed, to bring it back (if port re-probing did not handle
11897 	 * it). Add such code here.
11898 	 */
11899 	return (rv);
11900 }
11901 
11902 
11903 /*
11904  * Process ioctl reset all request.
11905  *
11906  * NOTE: Port multiplier code is not completed nor tested.
11907  */
11908 static int
11909 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst)
11910 {
11911 	sata_device_t sata_device;
11912 	int rv = 0;
11913 	int tcport;
11914 	int tpmport = 0;
11915 
11916 	sata_device.satadev_rev = SATA_DEVICE_REV;
11917 
11918 	/*
11919 	 * There is no protection here for configured devices.
11920 	 */
11921 	/* Sanity check */
11922 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
11923 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11924 		    "sata_hba_ioctl: sata_hba_tran missing required "
11925 		    "function sata_tran_reset_dport"));
11926 		return (ENOTSUP);
11927 	}
11928 
11929 	/*
11930 	 * Need to lock all ports, not just one.
11931 	 * If any port is locked by event processing, fail the whole operation.
11932 	 * One port is already locked, but for simplicity lock it again.
11933 	 */
11934 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
11935 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11936 		    cport_mutex);
11937 		if (((SATA_CPORT_INFO(sata_hba_inst, tcport)->
11938 		    cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) {
11939 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11940 			    cport_mutex);
11941 			rv = EBUSY;
11942 			break;
11943 		} else {
11944 			SATA_CPORT_INFO(sata_hba_inst, tcport)->
11945 			    cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
11946 			/*
11947 			 * If there is a port multiplier attached, we may need
11948 			 * to lock its port as well. If so, add such code here.
11949 			 */
11950 		}
11951 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11952 		    cport_mutex);
11953 	}
11954 
11955 	if (rv == 0) {
11956 		/*
11957 		 * All cports were successfully locked.
11958 		 * Reset main SATA controller only for now - no PMult.
11959 		 * Set the device address to port 0, to have a valid device
11960 		 * address.
11961 		 */
11962 		sata_device.satadev_addr.qual = SATA_ADDR_CNTRL;
11963 		sata_device.satadev_addr.cport = 0;
11964 		sata_device.satadev_addr.pmport = 0;
11965 
11966 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
11967 		    (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) {
11968 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11969 			    "sata_hba_ioctl: reset controller failed"));
11970 			return (EIO);
11971 		}
11972 		/*
11973 		 * Because ports were reset, port states are unknown.
11974 		 * They should be re-probed to get their state and
11975 		 * attached devices should be reinitialized.
11976 		 * Add code here to re-probe port multiplier device ports.
11977 		 */
11978 		for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst);
11979 		    tcport++) {
11980 			sata_device.satadev_addr.cport = tcport;
11981 			sata_device.satadev_addr.pmport = tpmport;
11982 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
11983 
11984 			if (sata_reprobe_port(sata_hba_inst, &sata_device,
11985 			    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
11986 				rv = EIO;
11987 		}
11988 	}
11989 	/*
11990 	 * Unlock all ports
11991 	 */
11992 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
11993 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11994 		    cport_mutex);
11995 		SATA_CPORT_INFO(sata_hba_inst, tcport)->
11996 		    cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
11997 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11998 		    cport_mutex);
11999 	}
12000 
12001 	/*
12002 	 * This operation returns EFAULT if either reset
12003 	 * controller failed or a re-probing of any port failed.
12004 	 */
12005 	return (rv);
12006 }
12007 
12008 
12009 /*
12010  * Process ioctl port self test request.
12011  *
12012  * NOTE: Port multiplier code is not completed nor tested.
12013  */
12014 static int
12015 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst,
12016     sata_device_t *sata_device)
12017 {
12018 	int cport, pmport, qual;
12019 	int rv = 0;
12020 
12021 	/* Sanity check */
12022 	if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL)
12023 		return (ENOTSUP);
12024 
12025 	cport = sata_device->satadev_addr.cport;
12026 	pmport = sata_device->satadev_addr.pmport;
12027 	qual = sata_device->satadev_addr.qual;
12028 
12029 	/*
12030 	 * There is no protection here for a configured
12031 	 * device attached to this port.
12032 	 */
12033 
12034 	if ((*SATA_SELFTEST_FUNC(sata_hba_inst))
12035 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
12036 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12037 		    "sata_hba_ioctl: port selftest: "
12038 		    "failed port %d:%d", cport, pmport));
12039 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12040 		    cport_mutex);
12041 		sata_update_port_info(sata_hba_inst, sata_device);
12042 		if (qual == SATA_ADDR_CPORT)
12043 			SATA_CPORT_STATE(sata_hba_inst, cport) =
12044 			    SATA_PSTATE_FAILED;
12045 		else /* port ultiplier device port */
12046 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
12047 			    SATA_PSTATE_FAILED;
12048 
12049 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12050 		    cport_mutex);
12051 		return (EIO);
12052 	}
12053 	/*
12054 	 * Beacuse the port was reset in the course of testing, it should be
12055 	 * re-probed and attached device state should be restored. At this
12056 	 * point the port state is unknown - it's state is HBA-specific.
12057 	 * Force port re-probing to get it into a known state.
12058 	 */
12059 	if (sata_reprobe_port(sata_hba_inst, sata_device,
12060 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
12061 		rv = EIO;
12062 	return (rv);
12063 }
12064 
12065 
12066 /*
12067  * sata_cfgadm_state:
12068  * Use the sata port state and state of the target node to figure out
12069  * the cfgadm_state.
12070  *
12071  * The port argument is a value with encoded cport,
12072  * pmport and address qualifier, in the same manner as a scsi target number.
12073  * SCSI_TO_SATA_CPORT macro extracts cport number,
12074  * SCSI_TO_SATA_PMPORT extracts pmport number and
12075  * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag.
12076  *
12077  * For now, support is for cports only - no port multiplier device ports.
12078  */
12079 
12080 static void
12081 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port,
12082     devctl_ap_state_t *ap_state)
12083 {
12084 	uint16_t	cport;
12085 	int		port_state;
12086 	sata_drive_info_t *sdinfo;
12087 
12088 	/* Cport only */
12089 	cport = SCSI_TO_SATA_CPORT(port);
12090 
12091 	port_state = SATA_CPORT_STATE(sata_hba_inst, cport);
12092 	if (port_state & SATA_PSTATE_SHUTDOWN ||
12093 	    port_state & SATA_PSTATE_FAILED) {
12094 		ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
12095 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12096 		if (port_state & SATA_PSTATE_FAILED)
12097 			ap_state->ap_condition = AP_COND_FAILED;
12098 		else
12099 			ap_state->ap_condition = AP_COND_UNKNOWN;
12100 
12101 		return;
12102 	}
12103 
12104 	/* Need to check pmult device port here as well, when supported */
12105 
12106 	/* Port is enabled and ready */
12107 
12108 	switch (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport)) {
12109 	case SATA_DTYPE_NONE:
12110 	{
12111 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12112 		ap_state->ap_condition = AP_COND_OK;
12113 		/* No device attached */
12114 		ap_state->ap_rstate = AP_RSTATE_EMPTY;
12115 		break;
12116 	}
12117 	case SATA_DTYPE_UNKNOWN:
12118 	case SATA_DTYPE_ATAPINONCD:
12119 	case SATA_DTYPE_PMULT:	/* Until PMult is supported */
12120 	case SATA_DTYPE_ATADISK:
12121 	case SATA_DTYPE_ATAPICD:
12122 	{
12123 		dev_info_t *tdip = NULL;
12124 		dev_info_t *dip = NULL;
12125 		int circ;
12126 
12127 		dip = SATA_DIP(sata_hba_inst);
12128 		tdip = sata_get_target_dip(dip, port);
12129 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
12130 		if (tdip != NULL) {
12131 			ndi_devi_enter(dip, &circ);
12132 			mutex_enter(&(DEVI(tdip)->devi_lock));
12133 			if (DEVI_IS_DEVICE_REMOVED(tdip)) {
12134 				/*
12135 				 * There could be the case where previously
12136 				 * configured and opened device was removed
12137 				 * and unknown device was plugged.
12138 				 * In such case we want to show a device, and
12139 				 * its configured or unconfigured state but
12140 				 * indicate unusable condition untill the
12141 				 * old target node is released and removed.
12142 				 */
12143 				ap_state->ap_condition = AP_COND_UNUSABLE;
12144 			} else {
12145 				mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst,
12146 				    cport));
12147 				sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12148 				    cport);
12149 				if (sdinfo != NULL) {
12150 					if ((sdinfo->satadrv_state &
12151 					    SATA_DSTATE_FAILED) != 0)
12152 						ap_state->ap_condition =
12153 						    AP_COND_FAILED;
12154 					else
12155 						ap_state->ap_condition =
12156 						    AP_COND_OK;
12157 				} else {
12158 					ap_state->ap_condition =
12159 					    AP_COND_UNKNOWN;
12160 				}
12161 				mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst,
12162 				    cport));
12163 			}
12164 			if ((DEVI_IS_DEVICE_OFFLINE(tdip)) ||
12165 			    (DEVI_IS_DEVICE_DOWN(tdip))) {
12166 				ap_state->ap_ostate =
12167 				    AP_OSTATE_UNCONFIGURED;
12168 			} else {
12169 				ap_state->ap_ostate =
12170 				    AP_OSTATE_CONFIGURED;
12171 			}
12172 			mutex_exit(&(DEVI(tdip)->devi_lock));
12173 			ndi_devi_exit(dip, circ);
12174 		} else {
12175 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12176 			ap_state->ap_condition = AP_COND_UNKNOWN;
12177 		}
12178 		break;
12179 	}
12180 	default:
12181 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
12182 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12183 		ap_state->ap_condition = AP_COND_UNKNOWN;
12184 		/*
12185 		 * This is actually internal error condition (non fatal),
12186 		 * because we have already checked all defined device types.
12187 		 */
12188 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12189 		    "sata_cfgadm_state: Internal error: "
12190 		    "unknown device type"));
12191 		break;
12192 	}
12193 }
12194 
12195 
12196 /*
12197  * Process ioctl get device path request.
12198  *
12199  * NOTE: Port multiplier code is not completed nor tested.
12200  */
12201 static int
12202 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst,
12203     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12204 {
12205 	char path[MAXPATHLEN];
12206 	uint32_t size;
12207 	dev_info_t *tdip;
12208 
12209 	(void) strcpy(path, "/devices");
12210 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
12211 	    &sata_device->satadev_addr)) == NULL) {
12212 		/*
12213 		 * No such device. If this is a request for a size, do not
12214 		 * return EINVAL for non-existing target, because cfgadm
12215 		 * will then indicate a meaningless ioctl failure.
12216 		 * If this is a request for a path, indicate invalid
12217 		 * argument.
12218 		 */
12219 		if (ioc->get_size == 0)
12220 			return (EINVAL);
12221 	} else {
12222 		(void) ddi_pathname(tdip, path + strlen(path));
12223 	}
12224 	size = strlen(path) + 1;
12225 
12226 	if (ioc->get_size != 0) {
12227 		if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz,
12228 		    mode) != 0)
12229 			return (EFAULT);
12230 	} else {
12231 		if (ioc->bufsiz != size)
12232 			return (EINVAL);
12233 
12234 		else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz,
12235 		    mode) != 0)
12236 			return (EFAULT);
12237 	}
12238 	return (0);
12239 }
12240 
12241 /*
12242  * Process ioctl get attachment point type request.
12243  *
12244  * NOTE: Port multiplier code is not completed nor tested.
12245  */
12246 static	int
12247 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst,
12248     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12249 {
12250 	uint32_t	type_len;
12251 	const char	*ap_type;
12252 	int		dev_type;
12253 
12254 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12255 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst,
12256 		    sata_device->satadev_addr.cport);
12257 	else /* pmport */
12258 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12259 		    sata_device->satadev_addr.cport,
12260 		    sata_device->satadev_addr.pmport);
12261 
12262 	switch (dev_type) {
12263 	case SATA_DTYPE_NONE:
12264 		ap_type = "port";
12265 		break;
12266 
12267 	case SATA_DTYPE_ATADISK:
12268 		ap_type = "disk";
12269 		break;
12270 
12271 	case SATA_DTYPE_ATAPICD:
12272 		ap_type = "cd/dvd";
12273 		break;
12274 
12275 	case SATA_DTYPE_PMULT:
12276 		ap_type = "pmult";
12277 		break;
12278 
12279 	case SATA_DTYPE_UNKNOWN:
12280 		ap_type = "unknown";
12281 		break;
12282 
12283 	default:
12284 		ap_type = "unsupported";
12285 		break;
12286 
12287 	} /* end of dev_type switch */
12288 
12289 	type_len = strlen(ap_type) + 1;
12290 
12291 	if (ioc->get_size) {
12292 		if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz,
12293 		    mode) != 0)
12294 			return (EFAULT);
12295 	} else {
12296 		if (ioc->bufsiz != type_len)
12297 			return (EINVAL);
12298 
12299 		if (ddi_copyout((void *)ap_type, ioc->buf,
12300 		    ioc->bufsiz, mode) != 0)
12301 			return (EFAULT);
12302 	}
12303 	return (0);
12304 
12305 }
12306 
12307 /*
12308  * Process ioctl get device model info request.
12309  * This operation should return to cfgadm the device model
12310  * information string
12311  *
12312  * NOTE: Port multiplier code is not completed nor tested.
12313  */
12314 static	int
12315 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst,
12316     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12317 {
12318 	sata_drive_info_t *sdinfo;
12319 	uint32_t info_len;
12320 	char ap_info[SATA_ID_MODEL_LEN + 1];
12321 
12322 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12323 	    sata_device->satadev_addr.cport)->cport_mutex);
12324 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12325 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12326 		    sata_device->satadev_addr.cport);
12327 	else /* port multiplier */
12328 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12329 		    sata_device->satadev_addr.cport,
12330 		    sata_device->satadev_addr.pmport);
12331 	if (sdinfo == NULL) {
12332 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12333 		    sata_device->satadev_addr.cport)->cport_mutex);
12334 		return (EINVAL);
12335 	}
12336 
12337 #ifdef	_LITTLE_ENDIAN
12338 	swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
12339 #else	/* _LITTLE_ENDIAN */
12340 	bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
12341 #endif	/* _LITTLE_ENDIAN */
12342 
12343 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12344 	    sata_device->satadev_addr.cport)->cport_mutex);
12345 
12346 	ap_info[SATA_ID_MODEL_LEN] = '\0';
12347 
12348 	info_len = strlen(ap_info) + 1;
12349 
12350 	if (ioc->get_size) {
12351 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12352 		    mode) != 0)
12353 			return (EFAULT);
12354 	} else {
12355 		if (ioc->bufsiz < info_len)
12356 			return (EINVAL);
12357 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12358 		    mode) != 0)
12359 			return (EFAULT);
12360 	}
12361 	return (0);
12362 }
12363 
12364 
12365 /*
12366  * Process ioctl get device firmware revision info request.
12367  * This operation should return to cfgadm the device firmware revision
12368  * information string
12369  *
12370  * NOTE: Port multiplier code is not completed nor tested.
12371  */
12372 static	int
12373 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst,
12374     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12375 {
12376 	sata_drive_info_t *sdinfo;
12377 	uint32_t info_len;
12378 	char ap_info[SATA_ID_FW_LEN + 1];
12379 
12380 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12381 	    sata_device->satadev_addr.cport)->cport_mutex);
12382 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12383 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12384 		    sata_device->satadev_addr.cport);
12385 	else /* port multiplier */
12386 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12387 		    sata_device->satadev_addr.cport,
12388 		    sata_device->satadev_addr.pmport);
12389 	if (sdinfo == NULL) {
12390 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12391 		    sata_device->satadev_addr.cport)->cport_mutex);
12392 		return (EINVAL);
12393 	}
12394 
12395 #ifdef	_LITTLE_ENDIAN
12396 	swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
12397 #else	/* _LITTLE_ENDIAN */
12398 	bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
12399 #endif	/* _LITTLE_ENDIAN */
12400 
12401 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12402 	    sata_device->satadev_addr.cport)->cport_mutex);
12403 
12404 	ap_info[SATA_ID_FW_LEN] = '\0';
12405 
12406 	info_len = strlen(ap_info) + 1;
12407 
12408 	if (ioc->get_size) {
12409 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12410 		    mode) != 0)
12411 			return (EFAULT);
12412 	} else {
12413 		if (ioc->bufsiz < info_len)
12414 			return (EINVAL);
12415 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12416 		    mode) != 0)
12417 			return (EFAULT);
12418 	}
12419 	return (0);
12420 }
12421 
12422 
12423 /*
12424  * Process ioctl get device serial number info request.
12425  * This operation should return to cfgadm the device serial number string.
12426  *
12427  * NOTE: Port multiplier code is not completed nor tested.
12428  */
12429 static	int
12430 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst,
12431     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12432 {
12433 	sata_drive_info_t *sdinfo;
12434 	uint32_t info_len;
12435 	char ap_info[SATA_ID_SERIAL_LEN + 1];
12436 
12437 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12438 	    sata_device->satadev_addr.cport)->cport_mutex);
12439 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12440 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12441 		    sata_device->satadev_addr.cport);
12442 	else /* port multiplier */
12443 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12444 		    sata_device->satadev_addr.cport,
12445 		    sata_device->satadev_addr.pmport);
12446 	if (sdinfo == NULL) {
12447 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12448 		    sata_device->satadev_addr.cport)->cport_mutex);
12449 		return (EINVAL);
12450 	}
12451 
12452 #ifdef	_LITTLE_ENDIAN
12453 	swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
12454 #else	/* _LITTLE_ENDIAN */
12455 	bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
12456 #endif	/* _LITTLE_ENDIAN */
12457 
12458 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12459 	    sata_device->satadev_addr.cport)->cport_mutex);
12460 
12461 	ap_info[SATA_ID_SERIAL_LEN] = '\0';
12462 
12463 	info_len = strlen(ap_info) + 1;
12464 
12465 	if (ioc->get_size) {
12466 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12467 		    mode) != 0)
12468 			return (EFAULT);
12469 	} else {
12470 		if (ioc->bufsiz < info_len)
12471 			return (EINVAL);
12472 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12473 		    mode) != 0)
12474 			return (EFAULT);
12475 	}
12476 	return (0);
12477 }
12478 
12479 
12480 /*
12481  * Preset scsi extended sense data (to NO SENSE)
12482  * First 18 bytes of the sense data are preset to current valid sense
12483  * with a key NO SENSE data.
12484  *
12485  * Returns void
12486  */
12487 static void
12488 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense)
12489 {
12490 	sense->es_valid = 1;		/* Valid sense */
12491 	sense->es_class = CLASS_EXTENDED_SENSE;	/* 0x70 - current err */
12492 	sense->es_key = KEY_NO_SENSE;
12493 	sense->es_info_1 = 0;
12494 	sense->es_info_2 = 0;
12495 	sense->es_info_3 = 0;
12496 	sense->es_info_4 = 0;
12497 	sense->es_add_len = 10;	/* Additional length - replace with a def */
12498 	sense->es_cmd_info[0] = 0;
12499 	sense->es_cmd_info[1] = 0;
12500 	sense->es_cmd_info[2] = 0;
12501 	sense->es_cmd_info[3] = 0;
12502 	sense->es_add_code = 0;
12503 	sense->es_qual_code = 0;
12504 }
12505 
12506 /*
12507  * Register a legacy cmdk-style devid for the target (disk) device.
12508  *
12509  * Note: This function is called only when the HBA devinfo node has the
12510  * property "use-cmdk-devid-format" set. This property indicates that
12511  * devid compatible with old cmdk (target) driver is to be generated
12512  * for any target device attached to this controller. This will take
12513  * precedence over the devid generated by sd (target) driver.
12514  * This function is derived from cmdk_devid_setup() function in cmdk.c.
12515  */
12516 static void
12517 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo)
12518 {
12519 	char	*hwid;
12520 	int	modlen;
12521 	int	serlen;
12522 	int	rval;
12523 	ddi_devid_t	devid;
12524 
12525 	/*
12526 	 * device ID is a concatanation of model number, "=", serial number.
12527 	 */
12528 	hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP);
12529 	bcopy(&sdinfo->satadrv_id.ai_model, hwid,
12530 	    sizeof (sdinfo->satadrv_id.ai_model));
12531 	swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model));
12532 	modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model));
12533 	if (modlen == 0)
12534 		goto err;
12535 	hwid[modlen++] = '=';
12536 	bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen],
12537 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12538 	swab(&hwid[modlen], &hwid[modlen],
12539 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12540 	serlen = sata_check_modser(&hwid[modlen],
12541 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12542 	if (serlen == 0)
12543 		goto err;
12544 	hwid[modlen + serlen] = 0; /* terminate the hwid string */
12545 
12546 	/* initialize/register devid */
12547 	if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL,
12548 	    (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS)
12549 		rval = ddi_devid_register(dip, devid);
12550 
12551 	if (rval != DDI_SUCCESS)
12552 		cmn_err(CE_WARN, "sata: failed to create devid for the disk"
12553 		    " on port %d", sdinfo->satadrv_addr.cport);
12554 err:
12555 	kmem_free(hwid, LEGACY_HWID_LEN);
12556 }
12557 
12558 /*
12559  * valid model/serial string must contain a non-zero non-space characters.
12560  * trim trailing spaces/NULLs.
12561  */
12562 static int
12563 sata_check_modser(char *buf, int buf_len)
12564 {
12565 	boolean_t ret;
12566 	char *s;
12567 	int i;
12568 	int tb;
12569 	char ch;
12570 
12571 	ret = B_FALSE;
12572 	s = buf;
12573 	for (i = 0; i < buf_len; i++) {
12574 		ch = *s++;
12575 		if (ch != ' ' && ch != '\0')
12576 			tb = i + 1;
12577 		if (ch != ' ' && ch != '\0' && ch != '0')
12578 			ret = B_TRUE;
12579 	}
12580 
12581 	if (ret == B_FALSE)
12582 		return (0); /* invalid string */
12583 
12584 	return (tb); /* return length */
12585 }
12586 
12587 /*
12588  * sata_set_drive_features function compares current device features setting
12589  * with the saved device features settings and, if there is a difference,
12590  * it restores device features setting to the previously saved state.
12591  * It also arbitrarily tries to select the highest supported DMA mode.
12592  * Device Identify or Identify Packet Device data has to be current.
12593  * At the moment read ahead and write cache are considered for all devices.
12594  * For atapi devices, Removable Media Status Notification is set in addition
12595  * to common features.
12596  *
12597  * This function cannot be called in the interrupt context (it may sleep).
12598  *
12599  * The input argument sdinfo should point to the drive info structure
12600  * to be updated after features are set. Note, that only
12601  * device (packet) identify data is updated, not the flags indicating the
12602  * supported features.
12603  *
12604  * Returns TRUE if successful or there was nothing to do. Device Identify data
12605  * in the drive info structure pointed to by the sdinfo argumens is updated
12606  * even when no features were set or changed.
12607  *
12608  * Returns FALSE if device features could not be set.
12609  *
12610  * Note: This function may fail the port, making it inaccessible.
12611  * In such case the explicit port disconnect/connect or physical device
12612  * detach/attach is required to re-evaluate port state again.
12613  */
12614 
12615 static int
12616 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst,
12617     sata_drive_info_t *sdinfo, int restore)
12618 {
12619 	int rval = SATA_SUCCESS;
12620 	sata_drive_info_t new_sdinfo;
12621 	char *finfo = "sata_set_drive_features: cannot";
12622 	char *finfox;
12623 	int cache_op;
12624 
12625 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
12626 	new_sdinfo.satadrv_addr = sdinfo->satadrv_addr;
12627 	new_sdinfo.satadrv_type = sdinfo->satadrv_type;
12628 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
12629 		/*
12630 		 * Cannot get device identification - retry later
12631 		 */
12632 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12633 		    "%s fetch device identify data\n", finfo);
12634 		return (SATA_FAILURE);
12635 	}
12636 	finfox = (restore != 0) ? " restore device features" :
12637 	    " initialize device features\n";
12638 
12639 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12640 		/* Arbitrarily set UDMA mode */
12641 		if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
12642 		    SATA_SUCCESS) {
12643 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12644 			    "%s set UDMA mode\n", finfo));
12645 			return (SATA_FAILURE);
12646 		}
12647 	} else { /* Assume SATA ATAPI CD/DVD */
12648 		/*  Set Removable Media Status Notification, if necessary */
12649 		if ((new_sdinfo.satadrv_id.ai_cmdset83 &
12650 		    SATA_RM_STATUS_NOTIFIC) != 0 && restore != 0) {
12651 			if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) &&
12652 			    (!(new_sdinfo.satadrv_id.ai_features86 &
12653 			    SATA_RM_STATUS_NOTIFIC))) ||
12654 			    ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) &&
12655 			    (new_sdinfo.satadrv_id.ai_features86 &
12656 			    SATA_RM_STATUS_NOTIFIC))) {
12657 				/* Current setting does not match saved one */
12658 				if (sata_set_rmsn(sata_hba_inst, sdinfo,
12659 				    sdinfo->satadrv_settings &
12660 				    SATA_DEV_RMSN) != SATA_SUCCESS)
12661 					rval = SATA_FAILURE;
12662 			}
12663 		}
12664 		/*
12665 		 * We have to set Multiword DMA or UDMA, if it is supported, as
12666 		 * we want to use DMA transfer mode whenever possible.
12667 		 * Some devices require explicit setting of the DMA mode.
12668 		 */
12669 		if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) {
12670 			/* Set highest supported DMA mode */
12671 			if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
12672 			    SATA_SUCCESS) {
12673 				SATA_LOG_D((sata_hba_inst, CE_WARN,
12674 				    "%s set UDMA mode\n", finfo));
12675 				rval = SATA_FAILURE;
12676 			}
12677 		}
12678 	}
12679 
12680 	if (!(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) &&
12681 	    !(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) {
12682 		/* None of the features is supported - do nothing */
12683 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12684 		    "settable features not supported\n", NULL);
12685 		goto update_sdinfo;
12686 	}
12687 
12688 	if (((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) &&
12689 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) &&
12690 	    ((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) &&
12691 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
12692 		/* Nothing to do */
12693 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12694 		    "no device features to set\n", NULL);
12695 		goto update_sdinfo;
12696 	}
12697 
12698 	if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) &&
12699 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD))) {
12700 		if (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) {
12701 			/* Enable read ahead / read cache */
12702 			cache_op = SATAC_SF_ENABLE_READ_AHEAD;
12703 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12704 			    "enabling read cache\n", NULL);
12705 		} else {
12706 			/* Disable read ahead  / read cache */
12707 			cache_op = SATAC_SF_DISABLE_READ_AHEAD;
12708 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12709 			    "disabling read cache\n", NULL);
12710 		}
12711 
12712 		/* Try to set read cache mode */
12713 		if (sata_set_cache_mode(sata_hba_inst, &new_sdinfo,
12714 		    cache_op) != SATA_SUCCESS) {
12715 			/* Pkt execution failed */
12716 			rval = SATA_FAILURE;
12717 		}
12718 	}
12719 
12720 	if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) &&
12721 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
12722 		if (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) {
12723 			/* Enable write cache */
12724 			cache_op = SATAC_SF_ENABLE_WRITE_CACHE;
12725 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12726 			    "enabling write cache\n", NULL);
12727 		} else {
12728 			/* Disable write cache */
12729 			cache_op = SATAC_SF_DISABLE_WRITE_CACHE;
12730 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12731 			    "disabling write cache\n", NULL);
12732 		}
12733 		/* Try to set write cache mode */
12734 		if (sata_set_cache_mode(sata_hba_inst, &new_sdinfo,
12735 		    cache_op) != SATA_SUCCESS) {
12736 			/* Pkt execution failed */
12737 			rval = SATA_FAILURE;
12738 		}
12739 	}
12740 
12741 	if (rval == SATA_FAILURE)
12742 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12743 		    "%s %s", finfo, finfox));
12744 update_sdinfo:
12745 	/*
12746 	 * We need to fetch Device Identify data again
12747 	 */
12748 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
12749 		/*
12750 		 * Cannot get device identification - retry later
12751 		 */
12752 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12753 		    "%s re-fetch device identify data\n", finfo));
12754 		rval = SATA_FAILURE;
12755 	}
12756 	/* Copy device sata info. */
12757 	sdinfo->satadrv_id = new_sdinfo.satadrv_id;
12758 
12759 	return (rval);
12760 }
12761 
12762 
12763 /*
12764  *
12765  * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if
12766  * unable to determine.
12767  *
12768  * Cannot be called in an interrupt context.
12769  *
12770  * Called by sata_build_lsense_page_2f()
12771  */
12772 
12773 static int
12774 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst,
12775     sata_drive_info_t *sdinfo)
12776 {
12777 	sata_pkt_t *spkt;
12778 	sata_cmd_t *scmd;
12779 	sata_pkt_txlate_t *spx;
12780 	int rval;
12781 
12782 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12783 	spx->txlt_sata_hba_inst = sata_hba_inst;
12784 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12785 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12786 	if (spkt == NULL) {
12787 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12788 		return (-1);
12789 	}
12790 	/* address is needed now */
12791 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12792 
12793 
12794 	/* Fill sata_pkt */
12795 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12796 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12797 	/* Synchronous mode, no callback */
12798 	spkt->satapkt_comp = NULL;
12799 	/* Timeout 30s */
12800 	spkt->satapkt_time = sata_default_pkt_time;
12801 
12802 	scmd = &spkt->satapkt_cmd;
12803 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
12804 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
12805 
12806 	/* Set up which registers need to be returned */
12807 	scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE;
12808 	scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE;
12809 
12810 	/* Build SMART_RETURN_STATUS cmd in the sata_pkt */
12811 	scmd->satacmd_addr_type = 0;		/* N/A */
12812 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
12813 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
12814 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
12815 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
12816 	scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS;
12817 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12818 	scmd->satacmd_cmd_reg = SATAC_SMART;
12819 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12820 	    sdinfo->satadrv_addr.cport)));
12821 
12822 
12823 	/* Send pkt to SATA HBA driver */
12824 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12825 	    SATA_TRAN_ACCEPTED ||
12826 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12827 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12828 		    sdinfo->satadrv_addr.cport)));
12829 		/*
12830 		 * Whoops, no SMART RETURN STATUS
12831 		 */
12832 		rval = -1;
12833 	} else {
12834 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12835 		    sdinfo->satadrv_addr.cport)));
12836 		if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
12837 			rval = -1;
12838 			goto fail;
12839 		}
12840 		if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
12841 			rval = -1;
12842 			goto fail;
12843 		}
12844 		if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) &&
12845 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2))
12846 			rval = 0;
12847 		else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) &&
12848 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4))
12849 			rval = 1;
12850 		else {
12851 			rval = -1;
12852 			goto fail;
12853 		}
12854 	}
12855 fail:
12856 	/* Free allocated resources */
12857 	sata_pkt_free(spx);
12858 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12859 
12860 	return (rval);
12861 }
12862 
12863 /*
12864  *
12865  * Returns 0 if succeeded, -1 otherwise
12866  *
12867  * Cannot be called in an interrupt context.
12868  *
12869  */
12870 static int
12871 sata_fetch_smart_data(
12872 	sata_hba_inst_t *sata_hba_inst,
12873 	sata_drive_info_t *sdinfo,
12874 	struct smart_data *smart_data)
12875 {
12876 	sata_pkt_t *spkt;
12877 	sata_cmd_t *scmd;
12878 	sata_pkt_txlate_t *spx;
12879 	int rval;
12880 
12881 #if ! defined(lint)
12882 	ASSERT(sizeof (struct smart_data) == 512);
12883 #endif
12884 
12885 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12886 	spx->txlt_sata_hba_inst = sata_hba_inst;
12887 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12888 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12889 	if (spkt == NULL) {
12890 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12891 		return (-1);
12892 	}
12893 	/* address is needed now */
12894 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12895 
12896 
12897 	/* Fill sata_pkt */
12898 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12899 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12900 	/* Synchronous mode, no callback */
12901 	spkt->satapkt_comp = NULL;
12902 	/* Timeout 30s */
12903 	spkt->satapkt_time = sata_default_pkt_time;
12904 
12905 	scmd = &spkt->satapkt_cmd;
12906 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
12907 
12908 	/*
12909 	 * Allocate buffer for SMART data
12910 	 */
12911 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
12912 	    sizeof (struct smart_data));
12913 	if (scmd->satacmd_bp == NULL) {
12914 		sata_pkt_free(spx);
12915 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12916 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12917 		    "sata_fetch_smart_data: "
12918 		    "cannot allocate buffer"));
12919 		return (-1);
12920 	}
12921 
12922 
12923 	/* Build SMART_READ_DATA cmd in the sata_pkt */
12924 	scmd->satacmd_addr_type = 0;		/* N/A */
12925 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
12926 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
12927 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
12928 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
12929 	scmd->satacmd_features_reg = SATA_SMART_READ_DATA;
12930 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12931 	scmd->satacmd_cmd_reg = SATAC_SMART;
12932 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12933 	    sdinfo->satadrv_addr.cport)));
12934 
12935 	/* Send pkt to SATA HBA driver */
12936 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12937 	    SATA_TRAN_ACCEPTED ||
12938 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12939 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12940 		    sdinfo->satadrv_addr.cport)));
12941 		/*
12942 		 * Whoops, no SMART DATA available
12943 		 */
12944 		rval = -1;
12945 		goto fail;
12946 	} else {
12947 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12948 		    sdinfo->satadrv_addr.cport)));
12949 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
12950 		    DDI_DMA_SYNC_FORKERNEL);
12951 		ASSERT(rval == DDI_SUCCESS);
12952 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data,
12953 		    sizeof (struct smart_data));
12954 	}
12955 
12956 fail:
12957 	/* Free allocated resources */
12958 	sata_free_local_buffer(spx);
12959 	sata_pkt_free(spx);
12960 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12961 
12962 	return (rval);
12963 }
12964 
12965 /*
12966  * Used by LOG SENSE page 0x10
12967  *
12968  * return 0 for success, -1 otherwise
12969  *
12970  */
12971 static int
12972 sata_ext_smart_selftest_read_log(
12973 	sata_hba_inst_t *sata_hba_inst,
12974 	sata_drive_info_t *sdinfo,
12975 	struct smart_ext_selftest_log *ext_selftest_log,
12976 	uint16_t block_num)
12977 {
12978 	sata_pkt_txlate_t *spx;
12979 	sata_pkt_t *spkt;
12980 	sata_cmd_t *scmd;
12981 	int rval;
12982 
12983 #if ! defined(lint)
12984 	ASSERT(sizeof (struct smart_ext_selftest_log) == 512);
12985 #endif
12986 
12987 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12988 	spx->txlt_sata_hba_inst = sata_hba_inst;
12989 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12990 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12991 	if (spkt == NULL) {
12992 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12993 		return (-1);
12994 	}
12995 	/* address is needed now */
12996 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12997 
12998 
12999 	/* Fill sata_pkt */
13000 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13001 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13002 	/* Synchronous mode, no callback */
13003 	spkt->satapkt_comp = NULL;
13004 	/* Timeout 30s */
13005 	spkt->satapkt_time = sata_default_pkt_time;
13006 
13007 	scmd = &spkt->satapkt_cmd;
13008 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13009 
13010 	/*
13011 	 * Allocate buffer for SMART extended self-test log
13012 	 */
13013 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13014 	    sizeof (struct smart_ext_selftest_log));
13015 	if (scmd->satacmd_bp == NULL) {
13016 		sata_pkt_free(spx);
13017 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13018 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13019 		    "sata_ext_smart_selftest_log: "
13020 		    "cannot allocate buffer"));
13021 		return (-1);
13022 	}
13023 
13024 	/* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */
13025 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
13026 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of selftest log */
13027 	scmd->satacmd_sec_count_msb = 0;	/* One sector of selftest log */
13028 	scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE;
13029 	scmd->satacmd_lba_low_msb = 0;
13030 	scmd->satacmd_lba_mid_lsb = block_num & 0xff;
13031 	scmd->satacmd_lba_mid_msb = block_num >> 8;
13032 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13033 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
13034 
13035 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13036 	    sdinfo->satadrv_addr.cport)));
13037 
13038 	/* Send pkt to SATA HBA driver */
13039 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13040 	    SATA_TRAN_ACCEPTED ||
13041 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13042 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13043 		    sdinfo->satadrv_addr.cport)));
13044 
13045 		/*
13046 		 * Whoops, no SMART selftest log info available
13047 		 */
13048 		rval = -1;
13049 		goto fail;
13050 	} else {
13051 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13052 		    sdinfo->satadrv_addr.cport)));
13053 
13054 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13055 		    DDI_DMA_SYNC_FORKERNEL);
13056 		ASSERT(rval == DDI_SUCCESS);
13057 		bcopy(scmd->satacmd_bp->b_un.b_addr,
13058 		    (uint8_t *)ext_selftest_log,
13059 		    sizeof (struct smart_ext_selftest_log));
13060 		rval = 0;
13061 	}
13062 
13063 fail:
13064 	/* Free allocated resources */
13065 	sata_free_local_buffer(spx);
13066 	sata_pkt_free(spx);
13067 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13068 
13069 	return (rval);
13070 }
13071 
13072 /*
13073  * Returns 0 for success, -1 otherwise
13074  *
13075  * SMART self-test log data is returned in buffer pointed to by selftest_log
13076  */
13077 static int
13078 sata_smart_selftest_log(
13079 	sata_hba_inst_t *sata_hba_inst,
13080 	sata_drive_info_t *sdinfo,
13081 	struct smart_selftest_log *selftest_log)
13082 {
13083 	sata_pkt_t *spkt;
13084 	sata_cmd_t *scmd;
13085 	sata_pkt_txlate_t *spx;
13086 	int rval;
13087 
13088 #if ! defined(lint)
13089 	ASSERT(sizeof (struct smart_selftest_log) == 512);
13090 #endif
13091 
13092 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13093 	spx->txlt_sata_hba_inst = sata_hba_inst;
13094 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13095 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13096 	if (spkt == NULL) {
13097 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13098 		return (-1);
13099 	}
13100 	/* address is needed now */
13101 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13102 
13103 
13104 	/* Fill sata_pkt */
13105 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13106 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13107 	/* Synchronous mode, no callback */
13108 	spkt->satapkt_comp = NULL;
13109 	/* Timeout 30s */
13110 	spkt->satapkt_time = sata_default_pkt_time;
13111 
13112 	scmd = &spkt->satapkt_cmd;
13113 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13114 
13115 	/*
13116 	 * Allocate buffer for SMART SELFTEST LOG
13117 	 */
13118 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13119 	    sizeof (struct smart_selftest_log));
13120 	if (scmd->satacmd_bp == NULL) {
13121 		sata_pkt_free(spx);
13122 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13123 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13124 		    "sata_smart_selftest_log: "
13125 		    "cannot allocate buffer"));
13126 		return (-1);
13127 	}
13128 
13129 	/* Build SMART_READ_LOG cmd in the sata_pkt */
13130 	scmd->satacmd_addr_type = 0;		/* N/A */
13131 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of SMART log */
13132 	scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE;
13133 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13134 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13135 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
13136 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13137 	scmd->satacmd_cmd_reg = SATAC_SMART;
13138 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13139 	    sdinfo->satadrv_addr.cport)));
13140 
13141 	/* Send pkt to SATA HBA driver */
13142 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13143 	    SATA_TRAN_ACCEPTED ||
13144 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13145 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13146 		    sdinfo->satadrv_addr.cport)));
13147 		/*
13148 		 * Whoops, no SMART DATA available
13149 		 */
13150 		rval = -1;
13151 		goto fail;
13152 	} else {
13153 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13154 		    sdinfo->satadrv_addr.cport)));
13155 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13156 		    DDI_DMA_SYNC_FORKERNEL);
13157 		ASSERT(rval == DDI_SUCCESS);
13158 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log,
13159 		    sizeof (struct smart_selftest_log));
13160 		rval = 0;
13161 	}
13162 
13163 fail:
13164 	/* Free allocated resources */
13165 	sata_free_local_buffer(spx);
13166 	sata_pkt_free(spx);
13167 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13168 
13169 	return (rval);
13170 }
13171 
13172 
13173 /*
13174  * Returns 0 for success, -1 otherwise
13175  *
13176  * SMART READ LOG data is returned in buffer pointed to by smart_log
13177  */
13178 static int
13179 sata_smart_read_log(
13180 	sata_hba_inst_t *sata_hba_inst,
13181 	sata_drive_info_t *sdinfo,
13182 	uint8_t *smart_log,		/* where the data should be returned */
13183 	uint8_t which_log,		/* which log should be returned */
13184 	uint8_t log_size)		/* # of 512 bytes in log */
13185 {
13186 	sata_pkt_t *spkt;
13187 	sata_cmd_t *scmd;
13188 	sata_pkt_txlate_t *spx;
13189 	int rval;
13190 
13191 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13192 	spx->txlt_sata_hba_inst = sata_hba_inst;
13193 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13194 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13195 	if (spkt == NULL) {
13196 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13197 		return (-1);
13198 	}
13199 	/* address is needed now */
13200 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13201 
13202 
13203 	/* Fill sata_pkt */
13204 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13205 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13206 	/* Synchronous mode, no callback */
13207 	spkt->satapkt_comp = NULL;
13208 	/* Timeout 30s */
13209 	spkt->satapkt_time = sata_default_pkt_time;
13210 
13211 	scmd = &spkt->satapkt_cmd;
13212 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13213 
13214 	/*
13215 	 * Allocate buffer for SMART READ LOG
13216 	 */
13217 	scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512);
13218 	if (scmd->satacmd_bp == NULL) {
13219 		sata_pkt_free(spx);
13220 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13221 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13222 		    "sata_smart_read_log: " "cannot allocate buffer"));
13223 		return (-1);
13224 	}
13225 
13226 	/* Build SMART_READ_LOG cmd in the sata_pkt */
13227 	scmd->satacmd_addr_type = 0;		/* N/A */
13228 	scmd->satacmd_sec_count_lsb = log_size;	/* what the caller asked for */
13229 	scmd->satacmd_lba_low_lsb = which_log;	/* which log page */
13230 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13231 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13232 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
13233 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13234 	scmd->satacmd_cmd_reg = SATAC_SMART;
13235 
13236 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13237 	    sdinfo->satadrv_addr.cport)));
13238 
13239 	/* Send pkt to SATA HBA driver */
13240 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13241 	    SATA_TRAN_ACCEPTED ||
13242 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13243 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13244 		    sdinfo->satadrv_addr.cport)));
13245 
13246 		/*
13247 		 * Whoops, no SMART DATA available
13248 		 */
13249 		rval = -1;
13250 		goto fail;
13251 	} else {
13252 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13253 		    sdinfo->satadrv_addr.cport)));
13254 
13255 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13256 		    DDI_DMA_SYNC_FORKERNEL);
13257 		ASSERT(rval == DDI_SUCCESS);
13258 		bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512);
13259 		rval = 0;
13260 	}
13261 
13262 fail:
13263 	/* Free allocated resources */
13264 	sata_free_local_buffer(spx);
13265 	sata_pkt_free(spx);
13266 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13267 
13268 	return (rval);
13269 }
13270 
13271 /*
13272  * Used by LOG SENSE page 0x10
13273  *
13274  * return 0 for success, -1 otherwise
13275  *
13276  */
13277 static int
13278 sata_read_log_ext_directory(
13279 	sata_hba_inst_t *sata_hba_inst,
13280 	sata_drive_info_t *sdinfo,
13281 	struct read_log_ext_directory *logdir)
13282 {
13283 	sata_pkt_txlate_t *spx;
13284 	sata_pkt_t *spkt;
13285 	sata_cmd_t *scmd;
13286 	int rval;
13287 
13288 #if ! defined(lint)
13289 	ASSERT(sizeof (struct read_log_ext_directory) == 512);
13290 #endif
13291 
13292 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13293 	spx->txlt_sata_hba_inst = sata_hba_inst;
13294 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13295 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13296 	if (spkt == NULL) {
13297 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13298 		return (-1);
13299 	}
13300 
13301 	/* Fill sata_pkt */
13302 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13303 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13304 	/* Synchronous mode, no callback */
13305 	spkt->satapkt_comp = NULL;
13306 	/* Timeout 30s */
13307 	spkt->satapkt_time = sata_default_pkt_time;
13308 
13309 	scmd = &spkt->satapkt_cmd;
13310 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13311 
13312 	/*
13313 	 * Allocate buffer for SMART READ LOG EXTENDED command
13314 	 */
13315 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13316 	    sizeof (struct read_log_ext_directory));
13317 	if (scmd->satacmd_bp == NULL) {
13318 		sata_pkt_free(spx);
13319 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13320 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13321 		    "sata_read_log_ext_directory: "
13322 		    "cannot allocate buffer"));
13323 		return (-1);
13324 	}
13325 
13326 	/* Build READ LOG EXT w/ log directory cmd in the  sata_pkt */
13327 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
13328 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of directory */
13329 	scmd->satacmd_sec_count_msb = 0;	/* One sector of directory */
13330 	scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY;
13331 	scmd->satacmd_lba_low_msb = 0;
13332 	scmd->satacmd_lba_mid_lsb = 0;
13333 	scmd->satacmd_lba_mid_msb = 0;
13334 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13335 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
13336 
13337 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13338 	    sdinfo->satadrv_addr.cport)));
13339 
13340 	/* Send pkt to SATA HBA driver */
13341 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13342 	    SATA_TRAN_ACCEPTED ||
13343 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13344 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13345 		    sdinfo->satadrv_addr.cport)));
13346 		/*
13347 		 * Whoops, no SMART selftest log info available
13348 		 */
13349 		rval = -1;
13350 		goto fail;
13351 	} else {
13352 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13353 		    sdinfo->satadrv_addr.cport)));
13354 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13355 		    DDI_DMA_SYNC_FORKERNEL);
13356 		ASSERT(rval == DDI_SUCCESS);
13357 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir,
13358 		    sizeof (struct read_log_ext_directory));
13359 		rval = 0;
13360 	}
13361 
13362 fail:
13363 	/* Free allocated resources */
13364 	sata_free_local_buffer(spx);
13365 	sata_pkt_free(spx);
13366 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13367 
13368 	return (rval);
13369 }
13370 
13371 /*
13372  * Set up error retrieval sata command for NCQ command error data
13373  * recovery.
13374  *
13375  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
13376  * returns SATA_FAILURE otherwise.
13377  */
13378 static int
13379 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
13380 {
13381 #ifndef __lock_lint
13382 	_NOTE(ARGUNUSED(sdinfo))
13383 #endif
13384 
13385 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
13386 	sata_cmd_t *scmd;
13387 	struct buf *bp;
13388 
13389 	/* Operation modes are up to the caller */
13390 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13391 
13392 	/* Synchronous mode, no callback - may be changed by the caller */
13393 	spkt->satapkt_comp = NULL;
13394 	spkt->satapkt_time = sata_default_pkt_time;
13395 
13396 	scmd = &spkt->satapkt_cmd;
13397 	bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t));
13398 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
13399 
13400 	/*
13401 	 * Allocate dma_able buffer error data.
13402 	 * Buffer allocation will take care of buffer alignment and other DMA
13403 	 * attributes.
13404 	 */
13405 	bp = sata_alloc_local_buffer(spx,
13406 	    sizeof (struct sata_ncq_error_recovery_page));
13407 	if (bp == NULL)
13408 		return (SATA_FAILURE);
13409 
13410 	bp_mapin(bp); /* make data buffer accessible */
13411 	scmd->satacmd_bp = bp;
13412 
13413 	/*
13414 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
13415 	 * before accessing it. Handle is in usual place in translate struct.
13416 	 */
13417 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
13418 
13419 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
13420 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
13421 
13422 	return (SATA_SUCCESS);
13423 }
13424 
13425 /*
13426  * sata_xlate_errors() is used to translate (S)ATA error
13427  * information to SCSI information returned in the SCSI
13428  * packet.
13429  */
13430 static void
13431 sata_xlate_errors(sata_pkt_txlate_t *spx)
13432 {
13433 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
13434 	struct scsi_extended_sense *sense;
13435 
13436 	scsipkt->pkt_reason = CMD_INCOMPLETE;
13437 	*scsipkt->pkt_scbp = STATUS_CHECK;
13438 	sense = sata_arq_sense(spx);
13439 
13440 	switch (spx->txlt_sata_pkt->satapkt_reason) {
13441 	case SATA_PKT_PORT_ERROR:
13442 		/*
13443 		 * We have no device data. Assume no data transfered.
13444 		 */
13445 		sense->es_key = KEY_HARDWARE_ERROR;
13446 		break;
13447 
13448 	case SATA_PKT_DEV_ERROR:
13449 		if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
13450 		    SATA_STATUS_ERR) {
13451 			/*
13452 			 * determine dev error reason from error
13453 			 * reg content
13454 			 */
13455 			sata_decode_device_error(spx, sense);
13456 			break;
13457 		}
13458 		/* No extended sense key - no info available */
13459 		break;
13460 
13461 	case SATA_PKT_TIMEOUT:
13462 		scsipkt->pkt_reason = CMD_TIMEOUT;
13463 		scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET;
13464 		/* No extended sense key */
13465 		break;
13466 
13467 	case SATA_PKT_ABORTED:
13468 		scsipkt->pkt_reason = CMD_ABORTED;
13469 		scsipkt->pkt_statistics |= STAT_ABORTED;
13470 		/* No extended sense key */
13471 		break;
13472 
13473 	case SATA_PKT_RESET:
13474 		/*
13475 		 * pkt aborted either by an explicit reset request from
13476 		 * a host, or due to error recovery
13477 		 */
13478 		scsipkt->pkt_reason = CMD_RESET;
13479 		scsipkt->pkt_statistics |= STAT_DEV_RESET;
13480 		break;
13481 
13482 	default:
13483 		scsipkt->pkt_reason = CMD_TRAN_ERR;
13484 		break;
13485 	}
13486 }
13487 
13488 
13489 
13490 
13491 /*
13492  * Log sata message
13493  * dev pathname msg line preceeds the logged message.
13494  */
13495 
13496 static	void
13497 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...)
13498 {
13499 	char pathname[128];
13500 	dev_info_t *dip;
13501 	va_list ap;
13502 
13503 	mutex_enter(&sata_log_mutex);
13504 
13505 	va_start(ap, fmt);
13506 	(void) vsprintf(sata_log_buf, fmt, ap);
13507 	va_end(ap);
13508 
13509 	if (sata_hba_inst != NULL) {
13510 		dip = SATA_DIP(sata_hba_inst);
13511 		(void) ddi_pathname(dip, pathname);
13512 	} else {
13513 		pathname[0] = 0;
13514 	}
13515 	if (level == CE_CONT) {
13516 		if (sata_debug_flags == 0)
13517 			cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf);
13518 		else
13519 			cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf);
13520 	} else {
13521 		if (level != CE_NOTE) {
13522 			cmn_err(level, "%s:\n %s", pathname, sata_log_buf);
13523 		} else if (sata_msg) {
13524 			cmn_err(level, "%s:\n %s", pathname,
13525 			    sata_log_buf);
13526 		}
13527 	}
13528 
13529 	mutex_exit(&sata_log_mutex);
13530 }
13531 
13532 
13533 /* ******** Asynchronous HBA events handling & hotplugging support ******** */
13534 
13535 /*
13536  * Start or terminate the thread, depending on flag arg and current state
13537  */
13538 static void
13539 sata_event_thread_control(int startstop)
13540 {
13541 	static 	int sata_event_thread_terminating = 0;
13542 	static 	int sata_event_thread_starting = 0;
13543 	int i;
13544 
13545 	mutex_enter(&sata_event_mutex);
13546 
13547 	if (startstop == 0 && (sata_event_thread_starting == 1 ||
13548 	    sata_event_thread_terminating == 1)) {
13549 		mutex_exit(&sata_event_mutex);
13550 		return;
13551 	}
13552 	if (startstop == 1 && sata_event_thread_starting == 1) {
13553 		mutex_exit(&sata_event_mutex);
13554 		return;
13555 	}
13556 	if (startstop == 1 && sata_event_thread_terminating == 1) {
13557 		sata_event_thread_starting = 1;
13558 		/* wait til terminate operation completes */
13559 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
13560 		while (sata_event_thread_terminating == 1) {
13561 			if (i-- <= 0) {
13562 				sata_event_thread_starting = 0;
13563 				mutex_exit(&sata_event_mutex);
13564 #ifdef SATA_DEBUG
13565 				cmn_err(CE_WARN, "sata_event_thread_control: "
13566 				    "timeout waiting for thread to terminate");
13567 #endif
13568 				return;
13569 			}
13570 			mutex_exit(&sata_event_mutex);
13571 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
13572 			mutex_enter(&sata_event_mutex);
13573 		}
13574 	}
13575 	if (startstop == 1) {
13576 		if (sata_event_thread == NULL) {
13577 			sata_event_thread = thread_create(NULL, 0,
13578 			    (void (*)())sata_event_daemon,
13579 			    &sata_hba_list, 0, &p0, TS_RUN, minclsyspri);
13580 		}
13581 		sata_event_thread_starting = 0;
13582 		mutex_exit(&sata_event_mutex);
13583 		return;
13584 	}
13585 
13586 	/*
13587 	 * If we got here, thread may need to be terminated
13588 	 */
13589 	if (sata_event_thread != NULL) {
13590 		int i;
13591 		/* Signal event thread to go away */
13592 		sata_event_thread_terminating = 1;
13593 		sata_event_thread_terminate = 1;
13594 		cv_signal(&sata_event_cv);
13595 		/*
13596 		 * Wait til daemon terminates.
13597 		 */
13598 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
13599 		while (sata_event_thread_terminate == 1) {
13600 			mutex_exit(&sata_event_mutex);
13601 			if (i-- <= 0) {
13602 				/* Daemon did not go away !!! */
13603 #ifdef SATA_DEBUG
13604 				cmn_err(CE_WARN, "sata_event_thread_control: "
13605 				    "cannot terminate event daemon thread");
13606 #endif
13607 				mutex_enter(&sata_event_mutex);
13608 				break;
13609 			}
13610 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
13611 			mutex_enter(&sata_event_mutex);
13612 		}
13613 		sata_event_thread_terminating = 0;
13614 	}
13615 	ASSERT(sata_event_thread_terminating == 0);
13616 	ASSERT(sata_event_thread_starting == 0);
13617 	mutex_exit(&sata_event_mutex);
13618 }
13619 
13620 
13621 /*
13622  * SATA HBA event notification function.
13623  * Events reported by SATA HBA drivers per HBA instance relate to a change in
13624  * a port and/or device state or a controller itself.
13625  * Events for different addresses/addr types cannot be combined.
13626  * A warning message is generated for each event type.
13627  * Events are not processed by this function, so only the
13628  * event flag(s)is set for an affected entity and the event thread is
13629  * waken up. Event daemon thread processes all events.
13630  *
13631  * NOTE: Since more than one event may be reported at the same time, one
13632  * cannot determine a sequence of events when opposite event are reported, eg.
13633  * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing
13634  * is taking precedence over reported events, i.e. may cause ignoring some
13635  * events.
13636  */
13637 #define	SATA_EVENT_MAX_MSG_LENGTH	79
13638 
13639 void
13640 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event)
13641 {
13642 	sata_hba_inst_t *sata_hba_inst = NULL;
13643 	sata_address_t *saddr;
13644 	sata_drive_info_t *sdinfo;
13645 	sata_port_stats_t *pstats;
13646 	int cport, pmport;
13647 	char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1];
13648 	char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1];
13649 	char *lcp;
13650 	static char *err_msg_evnt_1 =
13651 	    "sata_hba_event_notify: invalid port event 0x%x ";
13652 	static char *err_msg_evnt_2 =
13653 	    "sata_hba_event_notify: invalid device event 0x%x ";
13654 	int linkevent;
13655 
13656 	/*
13657 	 * There is a possibility that an event will be generated on HBA
13658 	 * that has not completed attachment or is detaching.
13659 	 * HBA driver should prevent this, but just in case it does not,
13660 	 * we need to ignore events for such HBA.
13661 	 */
13662 	mutex_enter(&sata_mutex);
13663 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13664 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13665 		if (SATA_DIP(sata_hba_inst) == dip)
13666 			if (sata_hba_inst->satahba_attached == 1)
13667 				break;
13668 	}
13669 	mutex_exit(&sata_mutex);
13670 	if (sata_hba_inst == NULL)
13671 		/* HBA not attached */
13672 		return;
13673 
13674 	ASSERT(sata_device != NULL);
13675 
13676 	/*
13677 	 * Validate address before - do not proceed with invalid address.
13678 	 */
13679 	saddr = &sata_device->satadev_addr;
13680 	if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst))
13681 		return;
13682 	if (saddr->qual == SATA_ADDR_PMPORT ||
13683 	    saddr->qual == SATA_ADDR_DPMPORT)
13684 		/* Port Multiplier not supported yet */
13685 		return;
13686 
13687 	cport = saddr->cport;
13688 	pmport = saddr->pmport;
13689 
13690 	buf1[0] = buf2[0] = '\0';
13691 
13692 	/*
13693 	 * Events refer to devices, ports and controllers - each has
13694 	 * unique address. Events for different addresses cannot be combined.
13695 	 */
13696 	if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) {
13697 
13698 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13699 
13700 		/* qualify this event(s) */
13701 		if ((event & SATA_EVNT_PORT_EVENTS) == 0) {
13702 			/* Invalid event for the device port */
13703 			(void) sprintf(buf2, err_msg_evnt_1,
13704 			    event & SATA_EVNT_PORT_EVENTS);
13705 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13706 			goto event_info;
13707 		}
13708 		if (saddr->qual == SATA_ADDR_CPORT) {
13709 			/* Controller's device port event */
13710 
13711 			(SATA_CPORT_INFO(sata_hba_inst, cport))->
13712 			    cport_event_flags |=
13713 			    event & SATA_EVNT_PORT_EVENTS;
13714 			pstats =
13715 			    &(SATA_CPORT_INFO(sata_hba_inst, cport))->
13716 			    cport_stats;
13717 		} else {
13718 			/* Port multiplier's device port event */
13719 			(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
13720 			    pmport_event_flags |=
13721 			    event & SATA_EVNT_PORT_EVENTS;
13722 			pstats =
13723 			    &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
13724 			    pmport_stats;
13725 		}
13726 
13727 		/*
13728 		 * Add to statistics and log the message. We have to do it
13729 		 * here rather than in the event daemon, because there may be
13730 		 * multiple events occuring before they are processed.
13731 		 */
13732 		linkevent = event &
13733 		    (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED);
13734 		if (linkevent) {
13735 			if (linkevent == (SATA_EVNT_LINK_LOST |
13736 			    SATA_EVNT_LINK_ESTABLISHED)) {
13737 				/* This is likely event combination */
13738 				(void) strlcat(buf1, "link lost/established, ",
13739 				    SATA_EVENT_MAX_MSG_LENGTH);
13740 
13741 				if (pstats->link_lost < 0xffffffffffffffffULL)
13742 					pstats->link_lost++;
13743 				if (pstats->link_established <
13744 				    0xffffffffffffffffULL)
13745 					pstats->link_established++;
13746 				linkevent = 0;
13747 			} else if (linkevent & SATA_EVNT_LINK_LOST) {
13748 				(void) strlcat(buf1, "link lost, ",
13749 				    SATA_EVENT_MAX_MSG_LENGTH);
13750 
13751 				if (pstats->link_lost < 0xffffffffffffffffULL)
13752 					pstats->link_lost++;
13753 			} else {
13754 				(void) strlcat(buf1, "link established, ",
13755 				    SATA_EVENT_MAX_MSG_LENGTH);
13756 				if (pstats->link_established <
13757 				    0xffffffffffffffffULL)
13758 					pstats->link_established++;
13759 			}
13760 		}
13761 		if (event & SATA_EVNT_DEVICE_ATTACHED) {
13762 			(void) strlcat(buf1, "device attached, ",
13763 			    SATA_EVENT_MAX_MSG_LENGTH);
13764 			if (pstats->device_attached < 0xffffffffffffffffULL)
13765 				pstats->device_attached++;
13766 		}
13767 		if (event & SATA_EVNT_DEVICE_DETACHED) {
13768 			(void) strlcat(buf1, "device detached, ",
13769 			    SATA_EVENT_MAX_MSG_LENGTH);
13770 			if (pstats->device_detached < 0xffffffffffffffffULL)
13771 				pstats->device_detached++;
13772 		}
13773 		if (event & SATA_EVNT_PWR_LEVEL_CHANGED) {
13774 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
13775 			    "port %d power level changed", cport);
13776 			if (pstats->port_pwr_changed < 0xffffffffffffffffULL)
13777 				pstats->port_pwr_changed++;
13778 		}
13779 
13780 		if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) {
13781 			/* There should be no other events for this address */
13782 			(void) sprintf(buf2, err_msg_evnt_1,
13783 			    event & ~SATA_EVNT_PORT_EVENTS);
13784 		}
13785 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13786 
13787 	} else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) {
13788 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13789 
13790 		/* qualify this event */
13791 		if ((event & SATA_EVNT_DEVICE_RESET) == 0) {
13792 			/* Invalid event for a device */
13793 			(void) sprintf(buf2, err_msg_evnt_2,
13794 			    event & SATA_EVNT_DEVICE_RESET);
13795 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13796 			goto event_info;
13797 		}
13798 		/* drive event */
13799 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
13800 		if (sdinfo != NULL) {
13801 			if (event & SATA_EVNT_DEVICE_RESET) {
13802 				(void) strlcat(buf1, "device reset, ",
13803 				    SATA_EVENT_MAX_MSG_LENGTH);
13804 				if (sdinfo->satadrv_stats.drive_reset <
13805 				    0xffffffffffffffffULL)
13806 					sdinfo->satadrv_stats.drive_reset++;
13807 				sdinfo->satadrv_event_flags |=
13808 				    SATA_EVNT_DEVICE_RESET;
13809 			}
13810 		}
13811 		if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) {
13812 			/* Invalid event for a device */
13813 			(void) sprintf(buf2, err_msg_evnt_2,
13814 			    event & ~SATA_EVNT_DRIVE_EVENTS);
13815 		}
13816 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13817 	} else {
13818 		if (saddr->qual != SATA_ADDR_NULL) {
13819 			/* Wrong address qualifier */
13820 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13821 			    "sata_hba_event_notify: invalid address 0x%x",
13822 			    *(uint32_t *)saddr));
13823 			return;
13824 		}
13825 		if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 ||
13826 		    (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) {
13827 			/* Invalid event for the controller */
13828 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13829 			    "sata_hba_event_notify: invalid event 0x%x for "
13830 			    "controller",
13831 			    event & SATA_EVNT_CONTROLLER_EVENTS));
13832 			return;
13833 		}
13834 		buf1[0] = '\0';
13835 		/* This may be a frequent and not interesting event */
13836 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
13837 		    "controller power level changed\n", NULL);
13838 
13839 		mutex_enter(&sata_hba_inst->satahba_mutex);
13840 		if (sata_hba_inst->satahba_stats.ctrl_pwr_change <
13841 		    0xffffffffffffffffULL)
13842 			sata_hba_inst->satahba_stats.ctrl_pwr_change++;
13843 
13844 		sata_hba_inst->satahba_event_flags |=
13845 		    SATA_EVNT_PWR_LEVEL_CHANGED;
13846 		mutex_exit(&sata_hba_inst->satahba_mutex);
13847 	}
13848 	/*
13849 	 * If we got here, there is something to do with this HBA
13850 	 * instance.
13851 	 */
13852 	mutex_enter(&sata_hba_inst->satahba_mutex);
13853 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
13854 	mutex_exit(&sata_hba_inst->satahba_mutex);
13855 	mutex_enter(&sata_mutex);
13856 	sata_event_pending |= SATA_EVNT_MAIN;	/* global event indicator */
13857 	mutex_exit(&sata_mutex);
13858 
13859 	/* Tickle event thread */
13860 	mutex_enter(&sata_event_mutex);
13861 	if (sata_event_thread_active == 0)
13862 		cv_signal(&sata_event_cv);
13863 	mutex_exit(&sata_event_mutex);
13864 
13865 event_info:
13866 	if (buf1[0] != '\0') {
13867 		lcp = strrchr(buf1, ',');
13868 		if (lcp != NULL)
13869 			*lcp = '\0';
13870 	}
13871 	if (saddr->qual == SATA_ADDR_CPORT ||
13872 	    saddr->qual == SATA_ADDR_DCPORT) {
13873 		if (buf1[0] != '\0') {
13874 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
13875 			    cport, buf1);
13876 		}
13877 		if (buf2[0] != '\0') {
13878 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
13879 			    cport, buf2);
13880 		}
13881 	} else if (saddr->qual == SATA_ADDR_PMPORT ||
13882 	    saddr->qual == SATA_ADDR_DPMPORT) {
13883 		if (buf1[0] != '\0') {
13884 			sata_log(sata_hba_inst, CE_NOTE,
13885 			    "port %d pmport %d: %s\n", cport, pmport, buf1);
13886 		}
13887 		if (buf2[0] != '\0') {
13888 			sata_log(sata_hba_inst, CE_NOTE,
13889 			    "port %d pmport %d: %s\n", cport, pmport, buf2);
13890 		}
13891 	}
13892 }
13893 
13894 
13895 /*
13896  * Event processing thread.
13897  * Arg is a pointer to the sata_hba_list pointer.
13898  * It is not really needed, because sata_hba_list is global and static
13899  */
13900 static void
13901 sata_event_daemon(void *arg)
13902 {
13903 #ifndef __lock_lint
13904 	_NOTE(ARGUNUSED(arg))
13905 #endif
13906 	sata_hba_inst_t *sata_hba_inst;
13907 	clock_t lbolt;
13908 
13909 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13910 	    "SATA event daemon started\n", NULL);
13911 loop:
13912 	/*
13913 	 * Process events here. Walk through all registered HBAs
13914 	 */
13915 	mutex_enter(&sata_mutex);
13916 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13917 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13918 		ASSERT(sata_hba_inst != NULL);
13919 		mutex_enter(&sata_hba_inst->satahba_mutex);
13920 		if (sata_hba_inst->satahba_attached != 1 ||
13921 		    (sata_hba_inst->satahba_event_flags &
13922 		    SATA_EVNT_SKIP) != 0) {
13923 			mutex_exit(&sata_hba_inst->satahba_mutex);
13924 			continue;
13925 		}
13926 		if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) {
13927 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP;
13928 			mutex_exit(&sata_hba_inst->satahba_mutex);
13929 			mutex_exit(&sata_mutex);
13930 			/* Got the controller with pending event */
13931 			sata_process_controller_events(sata_hba_inst);
13932 			/*
13933 			 * Since global mutex was released, there is a
13934 			 * possibility that HBA list has changed, so start
13935 			 * over from the top. Just processed controller
13936 			 * will be passed-over because of the SKIP flag.
13937 			 */
13938 			goto loop;
13939 		}
13940 		mutex_exit(&sata_hba_inst->satahba_mutex);
13941 	}
13942 	/* Clear SKIP flag in all controllers */
13943 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13944 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13945 		mutex_enter(&sata_hba_inst->satahba_mutex);
13946 		sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP;
13947 		mutex_exit(&sata_hba_inst->satahba_mutex);
13948 	}
13949 	mutex_exit(&sata_mutex);
13950 
13951 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13952 	    "SATA EVENT DAEMON suspending itself", NULL);
13953 
13954 #ifdef SATA_DEBUG
13955 	if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) {
13956 		sata_log(sata_hba_inst, CE_WARN,
13957 		    "SATA EVENTS PROCESSING DISABLED\n");
13958 		thread_exit(); /* Daemon will not run again */
13959 	}
13960 #endif
13961 	mutex_enter(&sata_event_mutex);
13962 	sata_event_thread_active = 0;
13963 	mutex_exit(&sata_event_mutex);
13964 	/*
13965 	 * Go to sleep/suspend itself and wake up either because new event or
13966 	 * wait timeout. Exit if there is a termination request (driver
13967 	 * unload).
13968 	 */
13969 	do {
13970 		lbolt = ddi_get_lbolt();
13971 		lbolt += drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME);
13972 		mutex_enter(&sata_event_mutex);
13973 		(void) cv_timedwait(&sata_event_cv, &sata_event_mutex, lbolt);
13974 
13975 		if (sata_event_thread_active != 0) {
13976 			mutex_exit(&sata_event_mutex);
13977 			continue;
13978 		}
13979 
13980 		/* Check if it is time to go away */
13981 		if (sata_event_thread_terminate == 1) {
13982 			/*
13983 			 * It is up to the thread setting above flag to make
13984 			 * sure that this thread is not killed prematurely.
13985 			 */
13986 			sata_event_thread_terminate = 0;
13987 			sata_event_thread = NULL;
13988 			mutex_exit(&sata_event_mutex);
13989 			SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13990 			    "SATA_EVENT_DAEMON_TERMINATING", NULL);
13991 			thread_exit();  { _NOTE(NOT_REACHED) }
13992 		}
13993 		mutex_exit(&sata_event_mutex);
13994 	} while (!(sata_event_pending & SATA_EVNT_MAIN));
13995 
13996 	mutex_enter(&sata_event_mutex);
13997 	sata_event_thread_active = 1;
13998 	mutex_exit(&sata_event_mutex);
13999 
14000 	mutex_enter(&sata_mutex);
14001 	sata_event_pending &= ~SATA_EVNT_MAIN;
14002 	mutex_exit(&sata_mutex);
14003 
14004 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
14005 	    "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL);
14006 
14007 	goto loop;
14008 }
14009 
14010 /*
14011  * Specific HBA instance event processing.
14012  *
14013  * NOTE: At the moment, device event processing is limited to hard disks
14014  * only.
14015  * cports only are supported - no pmports.
14016  */
14017 static void
14018 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst)
14019 {
14020 	int ncport;
14021 	uint32_t event_flags;
14022 	sata_address_t *saddr;
14023 
14024 	SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst,
14025 	    "Processing controller %d event(s)",
14026 	    ddi_get_instance(SATA_DIP(sata_hba_inst)));
14027 
14028 	mutex_enter(&sata_hba_inst->satahba_mutex);
14029 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN;
14030 	event_flags = sata_hba_inst->satahba_event_flags;
14031 	mutex_exit(&sata_hba_inst->satahba_mutex);
14032 	/*
14033 	 * Process controller power change first
14034 	 * HERE
14035 	 */
14036 	if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED)
14037 		sata_process_cntrl_pwr_level_change(sata_hba_inst);
14038 
14039 	/*
14040 	 * Search through ports/devices to identify affected port/device.
14041 	 * We may have to process events for more than one port/device.
14042 	 */
14043 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
14044 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14045 		event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))->
14046 		    cport_event_flags;
14047 		/* Check if port was locked by IOCTL processing */
14048 		if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) {
14049 			/*
14050 			 * We ignore port events because port is busy
14051 			 * with AP control processing. Set again
14052 			 * controller and main event flag, so that
14053 			 * events may be processed by the next daemon
14054 			 * run.
14055 			 */
14056 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14057 			mutex_enter(&sata_hba_inst->satahba_mutex);
14058 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14059 			mutex_exit(&sata_hba_inst->satahba_mutex);
14060 			mutex_enter(&sata_mutex);
14061 			sata_event_pending |= SATA_EVNT_MAIN;
14062 			mutex_exit(&sata_mutex);
14063 			SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst,
14064 			    "Event processing postponed until "
14065 			    "AP control processing completes",
14066 			    NULL);
14067 			/* Check other ports */
14068 			continue;
14069 		} else {
14070 			/*
14071 			 * Set BSY flag so that AP control would not
14072 			 * interfere with events processing for
14073 			 * this port.
14074 			 */
14075 			(SATA_CPORT_INFO(sata_hba_inst, ncport))->
14076 			    cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY;
14077 		}
14078 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14079 
14080 		saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr;
14081 
14082 		if ((event_flags &
14083 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
14084 			/*
14085 			 * Got port event.
14086 			 * We need some hierarchy of event processing as they
14087 			 * are affecting each other:
14088 			 * 1. port failed
14089 			 * 2. device detached/attached
14090 			 * 3. link events - link events may trigger device
14091 			 *    detached or device attached events in some
14092 			 *    circumstances.
14093 			 * 4. port power level changed
14094 			 */
14095 			if (event_flags & SATA_EVNT_PORT_FAILED) {
14096 				sata_process_port_failed_event(sata_hba_inst,
14097 				    saddr);
14098 			}
14099 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
14100 				sata_process_device_detached(sata_hba_inst,
14101 				    saddr);
14102 			}
14103 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
14104 				sata_process_device_attached(sata_hba_inst,
14105 				    saddr);
14106 			}
14107 			if (event_flags &
14108 			    (SATA_EVNT_LINK_ESTABLISHED |
14109 			    SATA_EVNT_LINK_LOST)) {
14110 				sata_process_port_link_events(sata_hba_inst,
14111 				    saddr);
14112 			}
14113 			if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) {
14114 				sata_process_port_pwr_change(sata_hba_inst,
14115 				    saddr);
14116 			}
14117 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
14118 				sata_process_target_node_cleanup(
14119 				    sata_hba_inst, saddr);
14120 			}
14121 			if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) {
14122 				sata_process_device_autoonline(
14123 				    sata_hba_inst, saddr);
14124 			}
14125 		}
14126 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14127 		if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) !=
14128 		    SATA_DTYPE_NONE) &&
14129 		    (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) {
14130 			if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)->
14131 			    satadrv_event_flags &
14132 			    (SATA_EVNT_DEVICE_RESET |
14133 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
14134 				/* Have device event */
14135 				sata_process_device_reset(sata_hba_inst,
14136 				    saddr);
14137 			}
14138 		}
14139 		/* Release PORT_BUSY flag */
14140 		(SATA_CPORT_INFO(sata_hba_inst, ncport))->
14141 		    cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
14142 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14143 
14144 	} /* End of loop through the controller SATA ports */
14145 }
14146 
14147 /*
14148  * Process HBA power level change reported by HBA driver.
14149  * Not implemented at this time - event is ignored.
14150  */
14151 static void
14152 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst)
14153 {
14154 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14155 	    "Processing controller power level change", NULL);
14156 
14157 	/* Ignoring it for now */
14158 	mutex_enter(&sata_hba_inst->satahba_mutex);
14159 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
14160 	mutex_exit(&sata_hba_inst->satahba_mutex);
14161 }
14162 
14163 /*
14164  * Process port power level change reported by HBA driver.
14165  * Not implemented at this time - event is ignored.
14166  */
14167 static void
14168 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst,
14169     sata_address_t *saddr)
14170 {
14171 	sata_cport_info_t *cportinfo;
14172 
14173 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14174 	    "Processing port power level change", NULL);
14175 
14176 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14177 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14178 	/* Reset event flag */
14179 	cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
14180 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14181 }
14182 
14183 /*
14184  * Process port failure reported by HBA driver.
14185  * cports support only - no pmports.
14186  */
14187 static void
14188 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst,
14189     sata_address_t *saddr)
14190 {
14191 	sata_cport_info_t *cportinfo;
14192 
14193 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14194 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14195 	/* Reset event flag first */
14196 	cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED;
14197 	/* If the port is in SHUTDOWN or FAILED state, ignore this event. */
14198 	if ((cportinfo->cport_state &
14199 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) {
14200 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14201 		    cport_mutex);
14202 		return;
14203 	}
14204 	/* Fail the port */
14205 	cportinfo->cport_state = SATA_PSTATE_FAILED;
14206 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14207 	sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport);
14208 }
14209 
14210 /*
14211  * Device Reset Event processing.
14212  * The seqeunce is managed by 3 stage flags:
14213  * - reset event reported,
14214  * - reset event being processed,
14215  * - request to clear device reset state.
14216  *
14217  * NOTE: This function has to be entered with cport mutex held. It exits with
14218  * mutex held as well, but can release mutex during the processing.
14219  */
14220 static void
14221 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst,
14222     sata_address_t *saddr)
14223 {
14224 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
14225 	sata_drive_info_t *sdinfo;
14226 	sata_cport_info_t *cportinfo;
14227 	sata_device_t sata_device;
14228 	int rval;
14229 
14230 	/* We only care about host sata cport for now */
14231 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14232 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14233 	/*
14234 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
14235 	 * state, ignore reset event.
14236 	 */
14237 	if (((cportinfo->cport_state &
14238 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
14239 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
14240 		sdinfo->satadrv_event_flags &=
14241 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
14242 		return;
14243 	}
14244 
14245 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) &
14246 	    SATA_VALID_DEV_TYPE) == 0) {
14247 		/*
14248 		 * This should not happen - coding error.
14249 		 * But we can recover, so do not panic, just clean up
14250 		 * and if in debug mode, log the message.
14251 		 */
14252 #ifdef SATA_DEBUG
14253 		sata_log(sata_hba_inst, CE_WARN,
14254 		    "sata_process_device_reset: "
14255 		    "Invalid device type with sdinfo!", NULL);
14256 #endif
14257 		sdinfo->satadrv_event_flags = 0;
14258 		return;
14259 	}
14260 
14261 #ifdef SATA_DEBUG
14262 	if ((sdinfo->satadrv_event_flags &
14263 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
14264 		/* Nothing to do */
14265 		/* Something is weird - why we are processing dev reset? */
14266 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14267 		    "No device reset event!!!!", NULL);
14268 
14269 		return;
14270 	}
14271 	if ((sdinfo->satadrv_event_flags &
14272 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
14273 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
14274 		/* Something is weird - new device reset event */
14275 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14276 		    "Overlapping device reset events!", NULL);
14277 	}
14278 #endif
14279 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14280 	    "Processing port %d device reset", saddr->cport);
14281 
14282 	/* Clear event flag */
14283 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
14284 
14285 	/* It seems that we always need to check the port state first */
14286 	sata_device.satadev_rev = SATA_DEVICE_REV;
14287 	sata_device.satadev_addr = *saddr;
14288 	/*
14289 	 * We have to exit mutex, because the HBA probe port function may
14290 	 * block on its own mutex.
14291 	 */
14292 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14293 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14294 	    (SATA_DIP(sata_hba_inst), &sata_device);
14295 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14296 	sata_update_port_info(sata_hba_inst, &sata_device);
14297 	if (rval != SATA_SUCCESS) {
14298 		/* Something went wrong? Fail the port */
14299 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14300 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14301 		if (sdinfo != NULL)
14302 			sdinfo->satadrv_event_flags = 0;
14303 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14304 		    cport_mutex);
14305 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14306 		    "SATA port %d probing failed",
14307 		    saddr->cport));
14308 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14309 		    saddr->cport)->cport_mutex);
14310 		return;
14311 	}
14312 	if ((sata_device.satadev_scr.sstatus  &
14313 	    SATA_PORT_DEVLINK_UP_MASK) !=
14314 	    SATA_PORT_DEVLINK_UP ||
14315 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
14316 		/*
14317 		 * No device to process, anymore. Some other event processing
14318 		 * would or have already performed port info cleanup.
14319 		 * To be safe (HBA may need it), request clearing device
14320 		 * reset condition.
14321 		 */
14322 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14323 		if (sdinfo != NULL) {
14324 			sdinfo->satadrv_event_flags &=
14325 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
14326 			sdinfo->satadrv_event_flags |=
14327 			    SATA_EVNT_CLEAR_DEVICE_RESET;
14328 		}
14329 		return;
14330 	}
14331 
14332 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14333 	if (sdinfo == NULL) {
14334 		return;
14335 	}
14336 	if ((sdinfo->satadrv_event_flags &
14337 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
14338 		/*
14339 		 * Start tracking time for device feature restoration and
14340 		 * identification. Save current time (lbolt value).
14341 		 */
14342 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
14343 	}
14344 	/* Mark device reset processing as active */
14345 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
14346 
14347 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
14348 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14349 
14350 	if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) ==
14351 	    SATA_FAILURE) {
14352 		/*
14353 		 * Restoring drive setting failed.
14354 		 * Probe the port first, to check if the port state has changed
14355 		 */
14356 		sata_device.satadev_rev = SATA_DEVICE_REV;
14357 		sata_device.satadev_addr = *saddr;
14358 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
14359 		/* probe port */
14360 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14361 		    (SATA_DIP(sata_hba_inst), &sata_device);
14362 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14363 		    cport_mutex);
14364 		if (rval == SATA_SUCCESS &&
14365 		    (sata_device.satadev_state &
14366 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
14367 		    (sata_device.satadev_scr.sstatus  &
14368 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
14369 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
14370 			/*
14371 			 * We may retry this a bit later - in-process reset
14372 			 * condition should be already set.
14373 			 * Track retry time for device identification.
14374 			 */
14375 			if ((cportinfo->cport_dev_type &
14376 			    SATA_VALID_DEV_TYPE) != 0 &&
14377 			    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL &&
14378 			    sdinfo->satadrv_reset_time != 0) {
14379 				clock_t cur_time = ddi_get_lbolt();
14380 				/*
14381 				 * If the retry time limit was not
14382 				 * exceeded, retry.
14383 				 */
14384 				if ((cur_time - sdinfo->satadrv_reset_time) <
14385 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
14386 					mutex_enter(
14387 					    &sata_hba_inst->satahba_mutex);
14388 					sata_hba_inst->satahba_event_flags |=
14389 					    SATA_EVNT_MAIN;
14390 					mutex_exit(
14391 					    &sata_hba_inst->satahba_mutex);
14392 					mutex_enter(&sata_mutex);
14393 					sata_event_pending |= SATA_EVNT_MAIN;
14394 					mutex_exit(&sata_mutex);
14395 					return;
14396 				}
14397 			}
14398 			/* Fail the drive */
14399 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
14400 
14401 			sata_log(sata_hba_inst, CE_WARN,
14402 			    "SATA device at port %d - device failed",
14403 			    saddr->cport);
14404 		} else {
14405 			/*
14406 			 * No point of retrying - some other event processing
14407 			 * would or already did port info cleanup.
14408 			 * To be safe (HBA may need it),
14409 			 * request clearing device reset condition.
14410 			 */
14411 			sdinfo->satadrv_event_flags |=
14412 			    SATA_EVNT_CLEAR_DEVICE_RESET;
14413 		}
14414 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
14415 		sdinfo->satadrv_reset_time = 0;
14416 		return;
14417 	}
14418 	/*
14419 	 * Raise the flag indicating that the next sata command could
14420 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
14421 	 * reset is reported.
14422 	 */
14423 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14424 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14425 		sdinfo->satadrv_reset_time = 0;
14426 		if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) {
14427 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14428 			sdinfo->satadrv_event_flags &=
14429 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
14430 			sdinfo->satadrv_event_flags |=
14431 			    SATA_EVNT_CLEAR_DEVICE_RESET;
14432 		}
14433 	}
14434 }
14435 
14436 
14437 /*
14438  * Port Link Events processing.
14439  * Every link established event may involve device reset (due to
14440  * COMRESET signal, equivalent of the hard reset) so arbitrarily
14441  * set device reset event for an attached device (if any).
14442  * If the port is in SHUTDOWN or FAILED state, ignore link events.
14443  *
14444  * The link established event processing varies, depending on the state
14445  * of the target node, HBA hotplugging capabilities, state of the port.
14446  * If the link is not active, the link established event is ignored.
14447  * If HBA cannot detect device attachment and there is no target node,
14448  * the link established event triggers device attach event processing.
14449  * Else, link established event triggers device reset event processing.
14450  *
14451  * The link lost event processing varies, depending on a HBA hotplugging
14452  * capability and the state of the port (link active or not active).
14453  * If the link is active, the lost link event is ignored.
14454  * If HBA cannot detect device removal, the lost link event triggers
14455  * device detached event processing after link lost timeout.
14456  * Else, the event is ignored.
14457  *
14458  * NOTE: Only cports are processed for now, i.e. no port multiplier ports
14459  */
14460 static void
14461 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst,
14462     sata_address_t *saddr)
14463 {
14464 	sata_device_t sata_device;
14465 	sata_cport_info_t *cportinfo;
14466 	sata_drive_info_t *sdinfo;
14467 	uint32_t event_flags;
14468 	int rval;
14469 
14470 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14471 	    "Processing port %d link event(s)", saddr->cport);
14472 
14473 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14474 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14475 	event_flags = cportinfo->cport_event_flags;
14476 
14477 	/* Reset event flags first */
14478 	cportinfo->cport_event_flags &=
14479 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
14480 
14481 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
14482 	if ((cportinfo->cport_state &
14483 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14484 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14485 		    cport_mutex);
14486 		return;
14487 	}
14488 
14489 	/*
14490 	 * For the sanity sake get current port state.
14491 	 * Set device address only. Other sata_device fields should be
14492 	 * set by HBA driver.
14493 	 */
14494 	sata_device.satadev_rev = SATA_DEVICE_REV;
14495 	sata_device.satadev_addr = *saddr;
14496 	/*
14497 	 * We have to exit mutex, because the HBA probe port function may
14498 	 * block on its own mutex.
14499 	 */
14500 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14501 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14502 	    (SATA_DIP(sata_hba_inst), &sata_device);
14503 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14504 	sata_update_port_info(sata_hba_inst, &sata_device);
14505 	if (rval != SATA_SUCCESS) {
14506 		/* Something went wrong? Fail the port */
14507 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14508 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14509 		    cport_mutex);
14510 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14511 		    "SATA port %d probing failed",
14512 		    saddr->cport));
14513 		/*
14514 		 * We may want to release device info structure, but
14515 		 * it is not necessary.
14516 		 */
14517 		return;
14518 	} else {
14519 		/* port probed successfully */
14520 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14521 	}
14522 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
14523 
14524 		if ((sata_device.satadev_scr.sstatus &
14525 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
14526 			/* Ignore event */
14527 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14528 			    "Ignoring port %d link established event - "
14529 			    "link down",
14530 			    saddr->cport);
14531 			goto linklost;
14532 		}
14533 
14534 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14535 		    "Processing port %d link established event",
14536 		    saddr->cport);
14537 
14538 		/*
14539 		 * For the sanity sake check if a device is attached - check
14540 		 * return state of a port probing.
14541 		 */
14542 		if (sata_device.satadev_type != SATA_DTYPE_NONE &&
14543 		    sata_device.satadev_type != SATA_DTYPE_PMULT) {
14544 			/*
14545 			 * HBA port probe indicated that there is a device
14546 			 * attached. Check if the framework had device info
14547 			 * structure attached for this device.
14548 			 */
14549 			if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
14550 				ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) !=
14551 				    NULL);
14552 
14553 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14554 				if ((sdinfo->satadrv_type &
14555 				    SATA_VALID_DEV_TYPE) != 0) {
14556 					/*
14557 					 * Dev info structure is present.
14558 					 * If dev_type is set to known type in
14559 					 * the framework's drive info struct
14560 					 * then the device existed before and
14561 					 * the link was probably lost
14562 					 * momentarily - in such case
14563 					 * we may want to check device
14564 					 * identity.
14565 					 * Identity check is not supported now.
14566 					 *
14567 					 * Link established event
14568 					 * triggers device reset event.
14569 					 */
14570 					(SATA_CPORTINFO_DRV_INFO(cportinfo))->
14571 					    satadrv_event_flags |=
14572 					    SATA_EVNT_DEVICE_RESET;
14573 				}
14574 			} else if (cportinfo->cport_dev_type ==
14575 			    SATA_DTYPE_NONE) {
14576 				/*
14577 				 * We got new device attached! If HBA does not
14578 				 * generate device attached events, trigger it
14579 				 * here.
14580 				 */
14581 				if (!(SATA_FEATURES(sata_hba_inst) &
14582 				    SATA_CTLF_HOTPLUG)) {
14583 					cportinfo->cport_event_flags |=
14584 					    SATA_EVNT_DEVICE_ATTACHED;
14585 				}
14586 			}
14587 			/* Reset link lost timeout */
14588 			cportinfo->cport_link_lost_time = 0;
14589 		}
14590 	}
14591 linklost:
14592 	if (event_flags & SATA_EVNT_LINK_LOST) {
14593 		if ((sata_device.satadev_scr.sstatus &
14594 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
14595 			/* Ignore event */
14596 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14597 			    "Ignoring port %d link lost event - link is up",
14598 			    saddr->cport);
14599 			goto done;
14600 		}
14601 #ifdef SATA_DEBUG
14602 		if (cportinfo->cport_link_lost_time == 0) {
14603 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14604 			    "Processing port %d link lost event",
14605 			    saddr->cport);
14606 		}
14607 #endif
14608 		/*
14609 		 * When HBA cannot generate device attached/detached events,
14610 		 * we need to track link lost time and eventually generate
14611 		 * device detach event.
14612 		 */
14613 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
14614 			/* We are tracking link lost time */
14615 			if (cportinfo->cport_link_lost_time == 0) {
14616 				/* save current time (lbolt value) */
14617 				cportinfo->cport_link_lost_time =
14618 				    ddi_get_lbolt();
14619 				/* just keep link lost event */
14620 				cportinfo->cport_event_flags |=
14621 				    SATA_EVNT_LINK_LOST;
14622 			} else {
14623 				clock_t cur_time = ddi_get_lbolt();
14624 				if ((cur_time -
14625 				    cportinfo->cport_link_lost_time) >=
14626 				    drv_usectohz(
14627 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
14628 					/* trigger device detach event */
14629 					cportinfo->cport_event_flags |=
14630 					    SATA_EVNT_DEVICE_DETACHED;
14631 					cportinfo->cport_link_lost_time = 0;
14632 					SATADBG1(SATA_DBG_EVENTS,
14633 					    sata_hba_inst,
14634 					    "Triggering port %d "
14635 					    "device detached event",
14636 					    saddr->cport);
14637 				} else {
14638 					/* keep link lost event */
14639 					cportinfo->cport_event_flags |=
14640 					    SATA_EVNT_LINK_LOST;
14641 				}
14642 			}
14643 		}
14644 		/*
14645 		 * We could change port state to disable/delay access to
14646 		 * the attached device until the link is recovered.
14647 		 */
14648 	}
14649 done:
14650 	event_flags = cportinfo->cport_event_flags;
14651 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14652 	if (event_flags != 0) {
14653 		mutex_enter(&sata_hba_inst->satahba_mutex);
14654 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14655 		mutex_exit(&sata_hba_inst->satahba_mutex);
14656 		mutex_enter(&sata_mutex);
14657 		sata_event_pending |= SATA_EVNT_MAIN;
14658 		mutex_exit(&sata_mutex);
14659 	}
14660 }
14661 
14662 /*
14663  * Device Detached Event processing.
14664  * Port is probed to find if a device is really gone. If so,
14665  * the device info structure is detached from the SATA port info structure
14666  * and released.
14667  * Port status is updated.
14668  *
14669  * NOTE: Process cports event only, no port multiplier ports.
14670  */
14671 static void
14672 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst,
14673     sata_address_t *saddr)
14674 {
14675 	sata_cport_info_t *cportinfo;
14676 	sata_drive_info_t *sdevinfo;
14677 	sata_device_t sata_device;
14678 	dev_info_t *tdip;
14679 	int rval;
14680 
14681 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14682 	    "Processing port %d device detached", saddr->cport);
14683 
14684 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14685 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14686 	/* Clear event flag */
14687 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
14688 
14689 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
14690 	if ((cportinfo->cport_state &
14691 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14692 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14693 		    cport_mutex);
14694 		return;
14695 	}
14696 	/* For sanity, re-probe the port */
14697 	sata_device.satadev_rev = SATA_DEVICE_REV;
14698 	sata_device.satadev_addr = *saddr;
14699 
14700 	/*
14701 	 * We have to exit mutex, because the HBA probe port function may
14702 	 * block on its own mutex.
14703 	 */
14704 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14705 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14706 	    (SATA_DIP(sata_hba_inst), &sata_device);
14707 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14708 	sata_update_port_info(sata_hba_inst, &sata_device);
14709 	if (rval != SATA_SUCCESS) {
14710 		/* Something went wrong? Fail the port */
14711 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14712 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14713 		    cport_mutex);
14714 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14715 		    "SATA port %d probing failed",
14716 		    saddr->cport));
14717 		/*
14718 		 * We may want to release device info structure, but
14719 		 * it is not necessary.
14720 		 */
14721 		return;
14722 	} else {
14723 		/* port probed successfully */
14724 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14725 	}
14726 	/*
14727 	 * Check if a device is still attached. For sanity, check also
14728 	 * link status - if no link, there is no device.
14729 	 */
14730 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
14731 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
14732 	    SATA_DTYPE_NONE) {
14733 		/*
14734 		 * Device is still attached - ignore detach event.
14735 		 */
14736 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14737 		    cport_mutex);
14738 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14739 		    "Ignoring detach - device still attached to port %d",
14740 		    sata_device.satadev_addr.cport);
14741 		return;
14742 	}
14743 	/*
14744 	 * We need to detach and release device info structure here
14745 	 */
14746 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14747 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14748 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14749 		(void) kmem_free((void *)sdevinfo,
14750 		    sizeof (sata_drive_info_t));
14751 	}
14752 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14753 	/*
14754 	 * Device cannot be reached anymore, even if the target node may be
14755 	 * still present.
14756 	 */
14757 
14758 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14759 	sata_log(sata_hba_inst, CE_WARN, "SATA device detached at port %d",
14760 	    sata_device.satadev_addr.cport);
14761 
14762 	/*
14763 	 * Try to offline a device and remove target node if it still exists
14764 	 */
14765 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
14766 	if (tdip != NULL) {
14767 		/*
14768 		 * Target node exists.  Unconfigure device then remove
14769 		 * the target node (one ndi operation).
14770 		 */
14771 		if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
14772 			/*
14773 			 * PROBLEM - no device, but target node remained
14774 			 * This happens when the file was open or node was
14775 			 * waiting for resources.
14776 			 */
14777 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14778 			    "sata_process_device_detached: "
14779 			    "Failed to remove target node for "
14780 			    "detached SATA device."));
14781 			/*
14782 			 * Set target node state to DEVI_DEVICE_REMOVED.
14783 			 * But re-check first that the node still exists.
14784 			 */
14785 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
14786 			    saddr->cport);
14787 			if (tdip != NULL) {
14788 				sata_set_device_removed(tdip);
14789 				/*
14790 				 * Instruct event daemon to retry the
14791 				 * cleanup later.
14792 				 */
14793 				sata_set_target_node_cleanup(sata_hba_inst,
14794 				    &sata_device.satadev_addr);
14795 			}
14796 		}
14797 	}
14798 	/*
14799 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14800 	 * with the hint: SE_HINT_REMOVE
14801 	 */
14802 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
14803 }
14804 
14805 
14806 /*
14807  * Device Attached Event processing.
14808  * Port state is checked to verify that a device is really attached. If so,
14809  * the device info structure is created and attached to the SATA port info
14810  * structure.
14811  *
14812  * If attached device cannot be identified or set-up, the retry for the
14813  * attach processing is set-up. Subsequent daemon run would try again to
14814  * identify the device, until the time limit is reached
14815  * (SATA_DEV_IDENTIFY_TIMEOUT).
14816  *
14817  * This function cannot be called in interrupt context (it may sleep).
14818  *
14819  * NOTE: Process cports event only, no port multiplier ports.
14820  */
14821 static void
14822 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst,
14823     sata_address_t *saddr)
14824 {
14825 	sata_cport_info_t *cportinfo;
14826 	sata_drive_info_t *sdevinfo;
14827 	sata_device_t sata_device;
14828 	dev_info_t *tdip;
14829 	uint32_t event_flags;
14830 	int rval;
14831 
14832 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14833 	    "Processing port %d device attached", saddr->cport);
14834 
14835 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14836 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14837 
14838 	/* Clear attach event flag first */
14839 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
14840 
14841 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
14842 	if ((cportinfo->cport_state &
14843 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14844 		cportinfo->cport_dev_attach_time = 0;
14845 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14846 		    cport_mutex);
14847 		return;
14848 	}
14849 
14850 	/*
14851 	 * If the sata_drive_info structure is found attached to the port info,
14852 	 * despite the fact the device was removed and now it is re-attached,
14853 	 * the old drive info structure was not removed.
14854 	 * Arbitrarily release device info structure.
14855 	 */
14856 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14857 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14858 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14859 		(void) kmem_free((void *)sdevinfo,
14860 		    sizeof (sata_drive_info_t));
14861 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14862 		    "Arbitrarily detaching old device info.", NULL);
14863 	}
14864 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14865 
14866 	/* For sanity, re-probe the port */
14867 	sata_device.satadev_rev = SATA_DEVICE_REV;
14868 	sata_device.satadev_addr = *saddr;
14869 
14870 	/*
14871 	 * We have to exit mutex, because the HBA probe port function may
14872 	 * block on its own mutex.
14873 	 */
14874 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14875 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14876 	    (SATA_DIP(sata_hba_inst), &sata_device);
14877 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14878 	sata_update_port_info(sata_hba_inst, &sata_device);
14879 	if (rval != SATA_SUCCESS) {
14880 		/* Something went wrong? Fail the port */
14881 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14882 		cportinfo->cport_dev_attach_time = 0;
14883 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14884 		    cport_mutex);
14885 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14886 		    "SATA port %d probing failed",
14887 		    saddr->cport));
14888 		return;
14889 	} else {
14890 		/* port probed successfully */
14891 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14892 	}
14893 	/*
14894 	 * Check if a device is still attached. For sanity, check also
14895 	 * link status - if no link, there is no device.
14896 	 */
14897 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
14898 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
14899 	    SATA_DTYPE_NONE) {
14900 		/*
14901 		 * No device - ignore attach event.
14902 		 */
14903 		cportinfo->cport_dev_attach_time = 0;
14904 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14905 		    cport_mutex);
14906 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14907 		    "Ignoring attach - no device connected to port %d",
14908 		    sata_device.satadev_addr.cport);
14909 		return;
14910 	}
14911 
14912 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14913 	/*
14914 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14915 	 * with the hint: SE_HINT_INSERT
14916 	 */
14917 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
14918 
14919 	/*
14920 	 * Port reprobing will take care of the creation of the device
14921 	 * info structure and determination of the device type.
14922 	 */
14923 	sata_device.satadev_addr = *saddr;
14924 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
14925 	    SATA_DEV_IDENTIFY_NORETRY);
14926 
14927 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14928 	    cport_mutex);
14929 	if ((cportinfo->cport_state & SATA_STATE_READY) &&
14930 	    (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) {
14931 		/* Some device is attached to the port */
14932 		if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) {
14933 			/*
14934 			 * A device was not successfully attached.
14935 			 * Track retry time for device identification.
14936 			 */
14937 			if (cportinfo->cport_dev_attach_time != 0) {
14938 				clock_t cur_time = ddi_get_lbolt();
14939 				/*
14940 				 * If the retry time limit was not exceeded,
14941 				 * reinstate attach event.
14942 				 */
14943 				if ((cur_time -
14944 				    cportinfo->cport_dev_attach_time) <
14945 				    drv_usectohz(
14946 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
14947 					/* OK, restore attach event */
14948 					cportinfo->cport_event_flags |=
14949 					    SATA_EVNT_DEVICE_ATTACHED;
14950 				} else {
14951 					/* Timeout - cannot identify device */
14952 					cportinfo->cport_dev_attach_time = 0;
14953 					sata_log(sata_hba_inst,
14954 					    CE_WARN,
14955 					    "Could not identify SATA device "
14956 					    "at port %d",
14957 					    saddr->cport);
14958 				}
14959 			} else {
14960 				/*
14961 				 * Start tracking time for device
14962 				 * identification.
14963 				 * Save current time (lbolt value).
14964 				 */
14965 				cportinfo->cport_dev_attach_time =
14966 				    ddi_get_lbolt();
14967 				/* Restore attach event */
14968 				cportinfo->cport_event_flags |=
14969 				    SATA_EVNT_DEVICE_ATTACHED;
14970 			}
14971 		} else {
14972 			/*
14973 			 * If device was successfully attached, the subsequent
14974 			 * action depends on a state of the
14975 			 * sata_auto_online variable. If it is set to zero.
14976 			 * an explicit 'configure' command will be needed to
14977 			 * configure it. If its value is non-zero, we will
14978 			 * attempt to online (configure) the device.
14979 			 * First, log the message indicating that a device
14980 			 * was attached.
14981 			 */
14982 			cportinfo->cport_dev_attach_time = 0;
14983 			sata_log(sata_hba_inst, CE_WARN,
14984 			    "SATA device detected at port %d", saddr->cport);
14985 
14986 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14987 				sata_drive_info_t new_sdinfo;
14988 
14989 				/* Log device info data */
14990 				new_sdinfo = *(SATA_CPORTINFO_DRV_INFO(
14991 				    cportinfo));
14992 				sata_show_drive_info(sata_hba_inst,
14993 				    &new_sdinfo);
14994 			}
14995 
14996 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14997 			    saddr->cport)->cport_mutex);
14998 
14999 			/*
15000 			 * Make sure that there is no target node for that
15001 			 * device. If so, release it. It should not happen,
15002 			 * unless we had problem removing the node when
15003 			 * device was detached.
15004 			 */
15005 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
15006 			    saddr->cport);
15007 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15008 			    saddr->cport)->cport_mutex);
15009 			if (tdip != NULL) {
15010 
15011 #ifdef SATA_DEBUG
15012 				if ((cportinfo->cport_event_flags &
15013 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
15014 					sata_log(sata_hba_inst, CE_WARN,
15015 					    "sata_process_device_attached: "
15016 					    "old device target node exists!");
15017 #endif
15018 				/*
15019 				 * target node exists - try to unconfigure
15020 				 * device and remove the node.
15021 				 */
15022 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15023 				    saddr->cport)->cport_mutex);
15024 				rval = ndi_devi_offline(tdip,
15025 				    NDI_DEVI_REMOVE);
15026 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15027 				    saddr->cport)->cport_mutex);
15028 
15029 				if (rval == NDI_SUCCESS) {
15030 					cportinfo->cport_event_flags &=
15031 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
15032 					cportinfo->cport_tgtnode_clean = B_TRUE;
15033 				} else {
15034 					/*
15035 					 * PROBLEM - the target node remained
15036 					 * and it belongs to a previously
15037 					 * attached device.
15038 					 * This happens when the file was open
15039 					 * or the node was waiting for
15040 					 * resources at the time the
15041 					 * associated device was removed.
15042 					 * Instruct event daemon to retry the
15043 					 * cleanup later.
15044 					 */
15045 					sata_log(sata_hba_inst,
15046 					    CE_WARN,
15047 					    "Application(s) accessing "
15048 					    "previously attached SATA "
15049 					    "device have to release "
15050 					    "it before newly inserted "
15051 					    "device can be made accessible.",
15052 					    saddr->cport);
15053 					cportinfo->cport_event_flags |=
15054 					    SATA_EVNT_TARGET_NODE_CLEANUP;
15055 					cportinfo->cport_tgtnode_clean =
15056 					    B_FALSE;
15057 				}
15058 			}
15059 			if (sata_auto_online != 0) {
15060 				cportinfo->cport_event_flags |=
15061 				    SATA_EVNT_AUTOONLINE_DEVICE;
15062 			}
15063 
15064 		}
15065 	} else {
15066 		cportinfo->cport_dev_attach_time = 0;
15067 	}
15068 
15069 	event_flags = cportinfo->cport_event_flags;
15070 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15071 	if (event_flags != 0) {
15072 		mutex_enter(&sata_hba_inst->satahba_mutex);
15073 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15074 		mutex_exit(&sata_hba_inst->satahba_mutex);
15075 		mutex_enter(&sata_mutex);
15076 		sata_event_pending |= SATA_EVNT_MAIN;
15077 		mutex_exit(&sata_mutex);
15078 	}
15079 }
15080 
15081 
15082 /*
15083  * Device Target Node Cleanup Event processing.
15084  * If the target node associated with a sata port device is in
15085  * DEVI_DEVICE_REMOVED state, an attempt is made to remove it.
15086  * If the target node cannot be removed, the event flag is left intact,
15087  * so that event daemon may re-run this function later.
15088  *
15089  * This function cannot be called in interrupt context (it may sleep).
15090  *
15091  * NOTE: Processes cport events only, not port multiplier ports.
15092  */
15093 static void
15094 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
15095     sata_address_t *saddr)
15096 {
15097 	sata_cport_info_t *cportinfo;
15098 	dev_info_t *tdip;
15099 
15100 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15101 	    "Processing port %d device target node cleanup", saddr->cport);
15102 
15103 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15104 
15105 	/*
15106 	 * Check if there is target node for that device and it is in the
15107 	 * DEVI_DEVICE_REMOVED state. If so, release it.
15108 	 */
15109 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
15110 	if (tdip != NULL) {
15111 		/*
15112 		 * target node exists - check if it is target node of
15113 		 * a removed device.
15114 		 */
15115 		if (sata_check_device_removed(tdip) == B_TRUE) {
15116 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15117 			    "sata_process_target_node_cleanup: "
15118 			    "old device target node exists!", NULL);
15119 			/*
15120 			 * Unconfigure and remove the target node
15121 			 */
15122 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) ==
15123 			    NDI_SUCCESS) {
15124 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15125 				    saddr->cport)->cport_mutex);
15126 				cportinfo->cport_event_flags &=
15127 				    ~SATA_EVNT_TARGET_NODE_CLEANUP;
15128 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15129 				    saddr->cport)->cport_mutex);
15130 				return;
15131 			}
15132 			/*
15133 			 * Event daemon will retry the cleanup later.
15134 			 */
15135 			mutex_enter(&sata_hba_inst->satahba_mutex);
15136 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15137 			mutex_exit(&sata_hba_inst->satahba_mutex);
15138 			mutex_enter(&sata_mutex);
15139 			sata_event_pending |= SATA_EVNT_MAIN;
15140 			mutex_exit(&sata_mutex);
15141 		}
15142 	} else {
15143 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15144 		    saddr->cport)->cport_mutex);
15145 		cportinfo->cport_event_flags &=
15146 		    ~SATA_EVNT_TARGET_NODE_CLEANUP;
15147 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15148 		    saddr->cport)->cport_mutex);
15149 	}
15150 }
15151 
15152 /*
15153  * Device AutoOnline Event processing.
15154  * If attached device is to be onlined, an attempt is made to online this
15155  * device, but only if there is no lingering (old) target node present.
15156  * If the device cannot be onlined, the event flag is left intact,
15157  * so that event daemon may re-run this function later.
15158  *
15159  * This function cannot be called in interrupt context (it may sleep).
15160  *
15161  * NOTE: Processes cport events only, not port multiplier ports.
15162  */
15163 static void
15164 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst,
15165     sata_address_t *saddr)
15166 {
15167 	sata_cport_info_t *cportinfo;
15168 	sata_drive_info_t *sdinfo;
15169 	sata_device_t sata_device;
15170 	dev_info_t *tdip;
15171 
15172 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15173 	    "Processing port %d attached device auto-onlining", saddr->cport);
15174 
15175 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15176 
15177 	/*
15178 	 * Check if device is present and recognized. If not, reset event.
15179 	 */
15180 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15181 	if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
15182 		/* Nothing to online */
15183 		cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
15184 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15185 		    saddr->cport)->cport_mutex);
15186 		return;
15187 	}
15188 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15189 
15190 	/*
15191 	 * Check if there is target node for this device and if it is in the
15192 	 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep
15193 	 * the event for later processing.
15194 	 */
15195 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
15196 	if (tdip != NULL) {
15197 		/*
15198 		 * target node exists - check if it is target node of
15199 		 * a removed device.
15200 		 */
15201 		if (sata_check_device_removed(tdip) == B_TRUE) {
15202 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15203 			    "sata_process_device_autoonline: "
15204 			    "old device target node exists!", NULL);
15205 			/*
15206 			 * Event daemon will retry device onlining later.
15207 			 */
15208 			mutex_enter(&sata_hba_inst->satahba_mutex);
15209 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15210 			mutex_exit(&sata_hba_inst->satahba_mutex);
15211 			mutex_enter(&sata_mutex);
15212 			sata_event_pending |= SATA_EVNT_MAIN;
15213 			mutex_exit(&sata_mutex);
15214 			return;
15215 		}
15216 		/*
15217 		 * If the target node is not in the 'removed" state, assume
15218 		 * that it belongs to this device. There is nothing more to do,
15219 		 * but reset the event.
15220 		 */
15221 	} else {
15222 
15223 		/*
15224 		 * Try to online the device
15225 		 * If there is any reset-related event, remove it. We are
15226 		 * configuring the device and no state restoring is needed.
15227 		 */
15228 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15229 		    saddr->cport)->cport_mutex);
15230 		sata_device.satadev_addr = *saddr;
15231 		if (saddr->qual == SATA_ADDR_CPORT)
15232 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
15233 		else
15234 			sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
15235 		sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
15236 		if (sdinfo != NULL) {
15237 			if (sdinfo->satadrv_event_flags &
15238 			    (SATA_EVNT_DEVICE_RESET |
15239 			    SATA_EVNT_INPROC_DEVICE_RESET))
15240 				sdinfo->satadrv_event_flags = 0;
15241 			sdinfo->satadrv_event_flags |=
15242 			    SATA_EVNT_CLEAR_DEVICE_RESET;
15243 
15244 			/* Need to create a new target node. */
15245 			cportinfo->cport_tgtnode_clean = B_TRUE;
15246 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15247 			    saddr->cport)->cport_mutex);
15248 			tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
15249 			    sata_hba_inst, &sata_device.satadev_addr);
15250 			if (tdip == NULL) {
15251 				/*
15252 				 * Configure (onlining) failed.
15253 				 * We will NOT retry
15254 				 */
15255 				SATA_LOG_D((sata_hba_inst, CE_WARN,
15256 				    "sata_process_device_autoonline: "
15257 				    "configuring SATA device at port %d failed",
15258 				    saddr->cport));
15259 			}
15260 		} else {
15261 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15262 			    saddr->cport)->cport_mutex);
15263 		}
15264 
15265 	}
15266 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15267 	cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
15268 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15269 	    saddr->cport)->cport_mutex);
15270 }
15271 
15272 
15273 static void
15274 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr,
15275     int hint)
15276 {
15277 	char ap[MAXPATHLEN];
15278 	nvlist_t *ev_attr_list = NULL;
15279 	int err;
15280 
15281 	/* Allocate and build sysevent attribute list */
15282 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP);
15283 	if (err != 0) {
15284 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15285 		    "sata_gen_sysevent: "
15286 		    "cannot allocate memory for sysevent attributes\n"));
15287 		return;
15288 	}
15289 	/* Add hint attribute */
15290 	err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint));
15291 	if (err != 0) {
15292 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15293 		    "sata_gen_sysevent: "
15294 		    "failed to add DR_HINT attr for sysevent"));
15295 		nvlist_free(ev_attr_list);
15296 		return;
15297 	}
15298 	/*
15299 	 * Add AP attribute.
15300 	 * Get controller pathname and convert it into AP pathname by adding
15301 	 * a target number.
15302 	 */
15303 	(void) snprintf(ap, MAXPATHLEN, "/devices");
15304 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap));
15305 	(void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d",
15306 	    SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual));
15307 
15308 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap);
15309 	if (err != 0) {
15310 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15311 		    "sata_gen_sysevent: "
15312 		    "failed to add DR_AP_ID attr for sysevent"));
15313 		nvlist_free(ev_attr_list);
15314 		return;
15315 	}
15316 
15317 	/* Generate/log sysevent */
15318 	err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR,
15319 	    ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP);
15320 	if (err != DDI_SUCCESS) {
15321 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15322 		    "sata_gen_sysevent: "
15323 		    "cannot log sysevent, err code %x\n", err));
15324 	}
15325 
15326 	nvlist_free(ev_attr_list);
15327 }
15328 
15329 
15330 
15331 
15332 /*
15333  * Set DEVI_DEVICE_REMOVED state in the SATA device target node.
15334  */
15335 static void
15336 sata_set_device_removed(dev_info_t *tdip)
15337 {
15338 	int circ;
15339 
15340 	ASSERT(tdip != NULL);
15341 
15342 	ndi_devi_enter(tdip, &circ);
15343 	mutex_enter(&DEVI(tdip)->devi_lock);
15344 	DEVI_SET_DEVICE_REMOVED(tdip);
15345 	mutex_exit(&DEVI(tdip)->devi_lock);
15346 	ndi_devi_exit(tdip, circ);
15347 }
15348 
15349 
15350 /*
15351  * Set internal event instructing event daemon to try
15352  * to perform the target node cleanup.
15353  */
15354 static void
15355 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
15356     sata_address_t *saddr)
15357 {
15358 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15359 	SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |=
15360 	    SATA_EVNT_TARGET_NODE_CLEANUP;
15361 	SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_tgtnode_clean =
15362 	    B_FALSE;
15363 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15364 	mutex_enter(&sata_hba_inst->satahba_mutex);
15365 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15366 	mutex_exit(&sata_hba_inst->satahba_mutex);
15367 	mutex_enter(&sata_mutex);
15368 	sata_event_pending |= SATA_EVNT_MAIN;
15369 	mutex_exit(&sata_mutex);
15370 }
15371 
15372 
15373 /*
15374  * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state,
15375  * i.e. check if the target node state indicates that it belongs to a removed
15376  * device.
15377  *
15378  * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state,
15379  * B_FALSE otherwise.
15380  *
15381  * NOTE: No port multiplier support.
15382  */
15383 static boolean_t
15384 sata_check_device_removed(dev_info_t *tdip)
15385 {
15386 	ASSERT(tdip != NULL);
15387 
15388 	if (DEVI_IS_DEVICE_REMOVED(tdip))
15389 		return (B_TRUE);
15390 	else
15391 		return (B_FALSE);
15392 }
15393 
15394 /* ************************ FAULT INJECTTION **************************** */
15395 
15396 #ifdef SATA_INJECT_FAULTS
15397 
15398 static	uint32_t sata_fault_count = 0;
15399 static	uint32_t sata_fault_suspend_count = 0;
15400 
15401 /*
15402  * Inject sata pkt fault
15403  * It modifies returned values of the sata packet.
15404  * First argument is the pointer to the executed sata packet.
15405  * The second argument specifies SATA command to be affected (not all commands
15406  * are instrumented).
15407  * Third argument is a pointer to a value returned by the HBA tran_start
15408  * function.
15409  * Fourth argument specifies injected error. Injected sata packet faults
15410  * are the satapkt_reason values.
15411  * SATA_PKT_BUSY		-1	Not completed, busy
15412  * SATA_PKT_DEV_ERROR		1	Device reported error
15413  * SATA_PKT_QUEUE_FULL		2	Not accepted, queue full
15414  * SATA_PKT_PORT_ERROR		3	Not completed, port error
15415  * SATA_PKT_CMD_UNSUPPORTED	4	Cmd unsupported
15416  * SATA_PKT_ABORTED		5	Aborted by request
15417  * SATA_PKT_TIMEOUT		6	Operation timeut
15418  * SATA_PKT_RESET		7	Aborted by reset request
15419  *
15420  * sata_inject_fault_count variable specifies number of times in row the
15421  * error is injected. Value of -1 specifies permanent fault, ie. every time
15422  * the fault injection pointnis reached, the fault is injected and anu pause
15423  * between fault injection specified by sata_inject_fault_pause_count is
15424  * ignored).
15425  *
15426  * sata_inject_fault_pause_count variable specifies number of times a fault
15427  * injection is bypassed (pause between fault injections).
15428  * If set to 0, a fault is injected only a number of times specified by
15429  * sata_inject_fault_count.
15430  *
15431  * The fault counts are static, so for periodic errors they have to be manually
15432  * reset to start repetition sequence from scratch.
15433  * If the original value returned by the HBA tran_start function is not
15434  * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error
15435  * is injected (to avoid masking real problems);
15436  *
15437  * NOTE: In its current incarnation, this function should be invoked only for
15438  * commands executed in SYNCHRONOUS mode.
15439  */
15440 
15441 
15442 static	void
15443 sata_inject_pkt_fault(sata_pkt_t *spkt, uint8_t cmd, int *rval,
15444     int fault)
15445 {
15446 	if (fault == 0)
15447 		return;
15448 	if (sata_inject_fault_count == 0)
15449 		return;
15450 
15451 	if (spkt->satapkt_cmd.satacmd_cmd_reg != cmd)
15452 		return;
15453 
15454 	if (*rval != SATA_TRAN_ACCEPTED ||
15455 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
15456 		sata_fault_count = 0;
15457 		sata_fault_suspend_count = 0;
15458 		return;
15459 	}
15460 	if (sata_fault_count == 0 && sata_fault_suspend_count != 0) {
15461 		/* Pause in the injection */
15462 		sata_fault_suspend_count -= 1;
15463 		return;
15464 	}
15465 
15466 	if (sata_fault_count == 0 && sata_fault_suspend_count == 0) {
15467 		/*
15468 		 * Init inject fault cycle. If fault count is set to -1,
15469 		 * it is a permanent fault.
15470 		 */
15471 		if (sata_inject_fault_count != -1) {
15472 			sata_fault_count = sata_inject_fault_count;
15473 			sata_fault_suspend_count =
15474 			    sata_inject_fault_pause_count;
15475 			if (sata_fault_suspend_count == 0)
15476 				sata_inject_fault_count = 0;
15477 		}
15478 	}
15479 
15480 	if (sata_fault_count != 0)
15481 		sata_fault_count -= 1;
15482 
15483 	switch (fault) {
15484 	case SATA_PKT_BUSY:
15485 		*rval = SATA_TRAN_BUSY;
15486 		spkt->satapkt_reason = SATA_PKT_BUSY;
15487 		break;
15488 
15489 	case SATA_PKT_QUEUE_FULL:
15490 		*rval = SATA_TRAN_QUEUE_FULL;
15491 		spkt->satapkt_reason = SATA_PKT_QUEUE_FULL;
15492 		break;
15493 
15494 	case SATA_PKT_CMD_UNSUPPORTED:
15495 		*rval = SATA_TRAN_CMD_UNSUPPORTED;
15496 		spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED;
15497 		break;
15498 
15499 	case SATA_PKT_PORT_ERROR:
15500 		/* This is "rejected" command */
15501 		*rval = SATA_TRAN_PORT_ERROR;
15502 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
15503 		/* Additional error setup could be done here - port state */
15504 		break;
15505 
15506 	case SATA_PKT_DEV_ERROR:
15507 		spkt->satapkt_reason = SATA_PKT_DEV_ERROR;
15508 		/*
15509 		 * Additional error setup could be done here
15510 		 */
15511 		break;
15512 
15513 	case SATA_PKT_ABORTED:
15514 		spkt->satapkt_reason = SATA_PKT_ABORTED;
15515 		break;
15516 
15517 	case SATA_PKT_TIMEOUT:
15518 		spkt->satapkt_reason = SATA_PKT_TIMEOUT;
15519 		/* Additional error setup could be done here */
15520 		break;
15521 
15522 	case SATA_PKT_RESET:
15523 		spkt->satapkt_reason = SATA_PKT_RESET;
15524 		/*
15525 		 * Additional error setup could be done here - device reset
15526 		 */
15527 		break;
15528 
15529 	default:
15530 		break;
15531 	}
15532 }
15533 
15534 #endif
15535