xref: /titanic_44/usr/src/uts/common/io/sata/impl/sata.c (revision 71269a2275bf5a143dad6461eee2710a344e7261)
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 
28 /*
29  * SATA Framework
30  * Generic SATA Host Adapter Implementation
31  */
32 
33 #include <sys/conf.h>
34 #include <sys/file.h>
35 #include <sys/ddi.h>
36 #include <sys/sunddi.h>
37 #include <sys/modctl.h>
38 #include <sys/cmn_err.h>
39 #include <sys/errno.h>
40 #include <sys/thread.h>
41 #include <sys/kstat.h>
42 #include <sys/note.h>
43 #include <sys/sysevent.h>
44 #include <sys/sysevent/eventdefs.h>
45 #include <sys/sysevent/dr.h>
46 #include <sys/taskq.h>
47 #include <sys/disp.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_inject_fault_count = 0;
120 uint32_t	sata_inject_fault_pause_count = 0;
121 uint32_t	sata_fault_type = 0;
122 uint32_t	sata_fault_cmd = 0;
123 dev_info_t	*sata_fault_ctrl = NULL;
124 sata_device_t	sata_fault_device;
125 
126 static	void sata_inject_pkt_fault(sata_pkt_t *, int *, int);
127 
128 #endif
129 
130 #define	LEGACY_HWID_LEN	64	/* Model (40) + Serial (20) + pad */
131 
132 static char sata_rev_tag[] = {"1.40"};
133 
134 /*
135  * SATA cb_ops functions
136  */
137 static 	int sata_hba_open(dev_t *, int, int, cred_t *);
138 static 	int sata_hba_close(dev_t, int, int, cred_t *);
139 static 	int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *,	int *);
140 
141 /*
142  * SCSA required entry points
143  */
144 static	int sata_scsi_tgt_init(dev_info_t *, dev_info_t *,
145     scsi_hba_tran_t *, struct scsi_device *);
146 static	int sata_scsi_tgt_probe(struct scsi_device *,
147     int (*callback)(void));
148 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *,
149     scsi_hba_tran_t *, struct scsi_device *);
150 static 	int sata_scsi_start(struct scsi_address *, struct scsi_pkt *);
151 static 	int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *);
152 static 	int sata_scsi_reset(struct scsi_address *, int);
153 static 	int sata_scsi_getcap(struct scsi_address *, char *, int);
154 static 	int sata_scsi_setcap(struct scsi_address *, char *, int, int);
155 static 	struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *,
156     struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t),
157     caddr_t);
158 static 	void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *);
159 static 	void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *);
160 static 	void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *);
161 
162 /*
163  * SATA HBA interface functions are defined in sata_hba.h header file
164  */
165 
166 /* Event processing functions */
167 static	void sata_event_daemon(void *);
168 static	void sata_event_thread_control(int);
169 static	void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst);
170 static	void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *);
171 static	void sata_process_port_failed_event(sata_hba_inst_t *,
172     sata_address_t *);
173 static	void sata_process_port_link_events(sata_hba_inst_t *,
174     sata_address_t *);
175 static	void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *);
176 static	void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *);
177 static	void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *);
178 static	void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *);
179 static	void sata_process_target_node_cleanup(sata_hba_inst_t *,
180     sata_address_t *);
181 static	void sata_process_device_autoonline(sata_hba_inst_t *,
182     sata_address_t *saddr);
183 
184 /*
185  * Local translation functions
186  */
187 static	int sata_txlt_inquiry(sata_pkt_txlate_t *);
188 static	int sata_txlt_test_unit_ready(sata_pkt_txlate_t *);
189 static	int sata_txlt_start_stop_unit(sata_pkt_txlate_t *);
190 static	int sata_txlt_read_capacity(sata_pkt_txlate_t *);
191 static	int sata_txlt_request_sense(sata_pkt_txlate_t *);
192 static 	int sata_txlt_read(sata_pkt_txlate_t *);
193 static 	int sata_txlt_write(sata_pkt_txlate_t *);
194 static 	int sata_txlt_log_sense(sata_pkt_txlate_t *);
195 static 	int sata_txlt_log_select(sata_pkt_txlate_t *);
196 static 	int sata_txlt_mode_sense(sata_pkt_txlate_t *);
197 static 	int sata_txlt_mode_select(sata_pkt_txlate_t *);
198 static 	int sata_txlt_synchronize_cache(sata_pkt_txlate_t *);
199 static 	int sata_txlt_write_buffer(sata_pkt_txlate_t *);
200 static 	int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *);
201 
202 static 	int sata_hba_start(sata_pkt_txlate_t *, int *);
203 static	int sata_txlt_invalid_command(sata_pkt_txlate_t *);
204 static	int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *);
205 static 	void sata_txlt_rw_completion(sata_pkt_t *);
206 static 	void sata_txlt_nodata_cmd_completion(sata_pkt_t *);
207 static 	void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *);
208 static 	int sata_emul_rw_completion(sata_pkt_txlate_t *);
209 static 	struct scsi_extended_sense *sata_immediate_error_response(
210     sata_pkt_txlate_t *, int);
211 static	struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *);
212 
213 static 	int sata_txlt_atapi(sata_pkt_txlate_t *);
214 static 	void sata_txlt_atapi_completion(sata_pkt_t *);
215 
216 /*
217  * Local functions for ioctl
218  */
219 static	int32_t sata_get_port_num(sata_hba_inst_t *,  struct devctl_iocdata *);
220 static	void sata_cfgadm_state(sata_hba_inst_t *, int32_t,
221     devctl_ap_state_t *);
222 static	dev_info_t *sata_get_target_dip(dev_info_t *, int32_t);
223 static	dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *);
224 static	dev_info_t *sata_devt_to_devinfo(dev_t);
225 static	int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *);
226 static	int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *);
227 static	int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *);
228 static	int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *);
229 static	int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *);
230 static	int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *);
231 static	int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *);
232 static	int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *);
233 static	int sata_ioctl_reset_all(sata_hba_inst_t *);
234 static	int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *);
235 static	int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *,
236     sata_ioctl_data_t *, int mode);
237 static	int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *,
238     sata_ioctl_data_t *, int mode);
239 static	int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *,
240     sata_ioctl_data_t *, int mode);
241 static	int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *,
242     sata_ioctl_data_t *, int mode);
243 static	int sata_ioctl_get_serialnumber_info(sata_hba_inst_t *,
244     sata_device_t *, sata_ioctl_data_t *, int mode);
245 
246 /*
247  * Local functions
248  */
249 static 	void sata_remove_hba_instance(dev_info_t *);
250 static 	int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *);
251 static 	void sata_probe_ports(sata_hba_inst_t *);
252 static 	int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int);
253 static 	int sata_add_device(dev_info_t *, sata_hba_inst_t *, int cport,
254     int pmport);
255 static 	dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *,
256     sata_address_t *);
257 static 	int sata_validate_scsi_address(sata_hba_inst_t *,
258     struct scsi_address *, sata_device_t *);
259 static 	int sata_validate_sata_address(sata_hba_inst_t *, int, int, int);
260 static	sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t));
261 static	void sata_pkt_free(sata_pkt_txlate_t *);
262 static	int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t),
263     caddr_t, ddi_dma_attr_t *);
264 static	void sata_common_free_dma_rsrcs(sata_pkt_txlate_t *);
265 static	int sata_probe_device(sata_hba_inst_t *, sata_device_t *);
266 static	sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *,
267     sata_device_t *);
268 static 	int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *);
269 static	void sata_reidentify_device(sata_pkt_txlate_t *);
270 static	struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int);
271 static 	void sata_free_local_buffer(sata_pkt_txlate_t *);
272 static 	uint64_t sata_check_capacity(sata_drive_info_t *);
273 void 	sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *,
274     ddi_dma_attr_t *);
275 static 	int sata_fetch_device_identify_data(sata_hba_inst_t *,
276     sata_drive_info_t *);
277 static	void sata_update_port_info(sata_hba_inst_t *, sata_device_t *);
278 static	void sata_update_port_scr(sata_port_scr_t *, sata_device_t *);
279 static	int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *);
280 static	int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int);
281 static	int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int);
282 static	int sata_set_drive_features(sata_hba_inst_t *,
283     sata_drive_info_t *, int flag);
284 static	void sata_init_write_cache_mode(sata_drive_info_t *sdinfo);
285 static	int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *);
286 static	void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *,
287     uint8_t *);
288 static	int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *,
289     struct scsi_inquiry *);
290 static	int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *);
291 static	int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *);
292 static	int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *);
293 static	int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *);
294 static	int sata_mode_select_page_8(sata_pkt_txlate_t *,
295     struct mode_cache_scsi3 *, int, int *, int *, int *);
296 static	int sata_mode_select_page_1c(sata_pkt_txlate_t *,
297     struct mode_info_excpt_page *, int, int *, int *, int *);
298 static	int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *);
299 static	int sata_mode_select_page_30(sata_pkt_txlate_t *,
300     struct mode_acoustic_management *, int, int *, int *, int *);
301 
302 static	int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *);
303 static	int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *,
304     sata_hba_inst_t *);
305 static	int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *,
306     sata_hba_inst_t *);
307 static	int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *,
308     sata_hba_inst_t *);
309 static	void sata_save_drive_settings(sata_drive_info_t *);
310 static	void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *);
311 static	void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...);
312 static	int sata_fetch_smart_return_status(sata_hba_inst_t *,
313     sata_drive_info_t *);
314 static	int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *,
315     struct smart_data *);
316 static	int sata_smart_selftest_log(sata_hba_inst_t *,
317     sata_drive_info_t *,
318     struct smart_selftest_log *);
319 static	int sata_ext_smart_selftest_read_log(sata_hba_inst_t *,
320     sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t);
321 static	int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *,
322     uint8_t *, uint8_t, uint8_t);
323 static	int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *,
324     struct read_log_ext_directory *);
325 static	void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int);
326 static	void sata_xlate_errors(sata_pkt_txlate_t *);
327 static	void sata_decode_device_error(sata_pkt_txlate_t *,
328     struct scsi_extended_sense *);
329 static	void sata_set_device_removed(dev_info_t *);
330 static	boolean_t sata_check_device_removed(dev_info_t *);
331 static	void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *);
332 static	int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *,
333     sata_drive_info_t *);
334 static	int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *,
335     sata_drive_info_t *);
336 static	void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *);
337 static	void sata_fixed_sense_data_preset(struct scsi_extended_sense *);
338 static  void sata_target_devid_register(dev_info_t *, sata_drive_info_t *);
339 static  int sata_check_modser(char *, int);
340 
341 
342 
343 /*
344  * SATA Framework will ignore SATA HBA driver cb_ops structure and
345  * register following one with SCSA framework.
346  * Open & close are provided, so scsi framework will not use its own
347  */
348 static struct cb_ops sata_cb_ops = {
349 	sata_hba_open,			/* open */
350 	sata_hba_close,			/* close */
351 	nodev,				/* strategy */
352 	nodev,				/* print */
353 	nodev,				/* dump */
354 	nodev,				/* read */
355 	nodev,				/* write */
356 	sata_hba_ioctl,			/* ioctl */
357 	nodev,				/* devmap */
358 	nodev,				/* mmap */
359 	nodev,				/* segmap */
360 	nochpoll,			/* chpoll */
361 	ddi_prop_op,			/* cb_prop_op */
362 	0,				/* streamtab */
363 	D_NEW | D_MP,			/* cb_flag */
364 	CB_REV,				/* rev */
365 	nodev,				/* aread */
366 	nodev				/* awrite */
367 };
368 
369 
370 extern struct mod_ops mod_miscops;
371 extern uchar_t	scsi_cdb_size[];
372 
373 static struct modlmisc modlmisc = {
374 	&mod_miscops,			/* Type of module */
375 	"SATA Module"			/* module name */
376 };
377 
378 
379 static struct modlinkage modlinkage = {
380 	MODREV_1,
381 	(void *)&modlmisc,
382 	NULL
383 };
384 
385 /*
386  * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero,
387  * i.e. when scsi_pkt has not timeout specified.
388  */
389 static int sata_default_pkt_time = 60;	/* 60 seconds */
390 
391 /*
392  * Intermediate buffer device access attributes - they are required,
393  * but not necessarily used.
394  */
395 static ddi_device_acc_attr_t sata_acc_attr = {
396 	DDI_DEVICE_ATTR_V0,
397 	DDI_STRUCTURE_LE_ACC,
398 	DDI_STRICTORDER_ACC
399 };
400 
401 
402 /*
403  * Mutexes protecting structures in multithreaded operations.
404  * Because events are relatively rare, a single global mutex protecting
405  * data structures should be sufficient. To increase performance, add
406  * separate mutex per each sata port and use global mutex only to protect
407  * common data structures.
408  */
409 static	kmutex_t sata_mutex;		/* protects sata_hba_list */
410 static	kmutex_t sata_log_mutex;	/* protects log */
411 
412 static 	char sata_log_buf[256];
413 
414 /* Default write cache setting for SATA hard disks */
415 int	sata_write_cache = 1;		/* enabled */
416 
417 /* Default write cache setting for SATA ATAPI CD/DVD */
418 int 	sata_atapicdvd_write_cache = 1; /* enabled */
419 
420 /* Default write cache setting for SATA ATAPI tape */
421 int	sata_atapitape_write_cache = 1; /* enabled */
422 
423 /*
424  * Linked list of HBA instances
425  */
426 static 	sata_hba_inst_t *sata_hba_list = NULL;
427 static 	sata_hba_inst_t *sata_hba_list_tail = NULL;
428 /*
429  * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran
430  * structure and in sata soft state.
431  */
432 
433 /*
434  * Event daemon related variables
435  */
436 static 	kmutex_t sata_event_mutex;
437 static 	kcondvar_t sata_event_cv;
438 static 	kthread_t *sata_event_thread = NULL;
439 static 	int sata_event_thread_terminate = 0;
440 static 	int sata_event_pending = 0;
441 static 	int sata_event_thread_active = 0;
442 extern 	pri_t minclsyspri;
443 
444 /*
445  * NCQ error recovery command
446  */
447 static const sata_cmd_t sata_rle_cmd = {
448 	SATA_CMD_REV,
449 	NULL,
450 	{
451 		SATA_DIR_READ
452 	},
453 	ATA_ADDR_LBA48,
454 	0,
455 	0,
456 	0,
457 	0,
458 	0,
459 	1,
460 	READ_LOG_EXT_NCQ_ERROR_RECOVERY,
461 	0,
462 	0,
463 	0,
464 	SATAC_READ_LOG_EXT,
465 	0,
466 	0,
467 	0,
468 };
469 
470 /*
471  * ATAPI error recovery CDB
472  */
473 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = {
474 	SCMD_REQUEST_SENSE,
475 	0,			/* Only fixed RQ format is supported */
476 	0,
477 	0,
478 	SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */
479 	0
480 };
481 
482 
483 /* Warlock directives */
484 
485 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran))
486 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device))
487 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops))
488 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense))
489 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status))
490 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr))
491 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t))
492 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state))
493 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state))
494 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list))
495 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list))
496 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next))
497 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev))
498 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \
499     sata_hba_inst::satahba_scsi_tran))
500 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran))
501 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip))
502 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached))
503 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port))
504 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex,
505     sata_hba_inst::satahba_event_flags))
506 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
507     sata_cport_info::cport_devp))
508 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp))
509 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr))
510 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
511     sata_cport_info::cport_dev_type))
512 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type))
513 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
514     sata_cport_info::cport_state))
515 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state))
516 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
517     sata_pmport_info::pmport_state))
518 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state))
519 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type))
520 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive))
521 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port))
522 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports))
523 #ifdef SATA_DEBUG
524 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count))
525 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count))
526 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace))
527 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index))
528 #endif
529 
530 /* End of warlock directives */
531 
532 /* ************** loadable module configuration functions ************** */
533 
534 int
535 _init()
536 {
537 	int rval;
538 
539 	mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL);
540 	mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL);
541 	mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL);
542 	cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL);
543 	if ((rval = mod_install(&modlinkage)) != 0) {
544 #ifdef SATA_DEBUG
545 		cmn_err(CE_WARN, "sata: _init: mod_install failed\n");
546 #endif
547 		mutex_destroy(&sata_log_mutex);
548 		cv_destroy(&sata_event_cv);
549 		mutex_destroy(&sata_event_mutex);
550 		mutex_destroy(&sata_mutex);
551 	}
552 	return (rval);
553 }
554 
555 int
556 _fini()
557 {
558 	int rval;
559 
560 	if ((rval = mod_remove(&modlinkage)) != 0)
561 		return (rval);
562 
563 	mutex_destroy(&sata_log_mutex);
564 	cv_destroy(&sata_event_cv);
565 	mutex_destroy(&sata_event_mutex);
566 	mutex_destroy(&sata_mutex);
567 	return (rval);
568 }
569 
570 int
571 _info(struct modinfo *modinfop)
572 {
573 	return (mod_info(&modlinkage, modinfop));
574 }
575 
576 
577 
578 /* ********************* SATA HBA entry points ********************* */
579 
580 
581 /*
582  * Called by SATA HBA from _init().
583  * Registers HBA driver instance/sata framework pair with scsi framework, by
584  * calling scsi_hba_init().
585  *
586  * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used
587  * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver
588  * cb_ops pointer in SATA HBA driver dev_ops structure.
589  * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors.
590  *
591  * Return status of the scsi_hba_init() is returned to a calling SATA HBA
592  * driver.
593  */
594 int
595 sata_hba_init(struct modlinkage *modlp)
596 {
597 	int rval;
598 	struct dev_ops *hba_ops;
599 
600 	SATADBG1(SATA_DBG_HBA_IF, NULL,
601 	    "sata_hba_init: name %s \n",
602 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
603 	/*
604 	 * Fill-up cb_ops and dev_ops when necessary
605 	 */
606 	hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
607 	/*
608 	 * Provide pointer to SATA dev_ops
609 	 */
610 	hba_ops->devo_cb_ops = &sata_cb_ops;
611 
612 	/*
613 	 * Register SATA HBA with SCSI framework
614 	 */
615 	if ((rval = scsi_hba_init(modlp)) != 0) {
616 		SATADBG1(SATA_DBG_HBA_IF, NULL,
617 		    "sata_hba_init: scsi hba init failed\n", NULL);
618 		return (rval);
619 	}
620 
621 	return (0);
622 }
623 
624 
625 /* HBA attach stages */
626 #define	HBA_ATTACH_STAGE_SATA_HBA_INST	1
627 #define	HBA_ATTACH_STAGE_SCSI_ATTACHED	2
628 #define	HBA_ATTACH_STAGE_SETUP		4
629 #define	HBA_ATTACH_STAGE_LINKED		8
630 
631 
632 /*
633  *
634  * Called from SATA HBA driver's attach routine to attach an instance of
635  * the HBA.
636  *
637  * For DDI_ATTACH command:
638  * sata_hba_inst structure is allocated here and initialized with pointers to
639  * SATA framework implementation of required scsi tran functions.
640  * The scsi_tran's tran_hba_private field is used by SATA Framework to point
641  * to the soft structure (sata_hba_inst) allocated by SATA framework for
642  * SATA HBA instance related data.
643  * The scsi_tran's tran_hba_private field is used by SATA framework to
644  * store a pointer to per-HBA-instance of sata_hba_inst structure.
645  * The sata_hba_inst structure is cross-linked to scsi tran structure.
646  * Among other info, a pointer to sata_hba_tran structure is stored in
647  * sata_hba_inst. The sata_hba_inst structures for different HBA instances are
648  * linked together into the list, pointed to by sata_hba_list.
649  * On the first HBA instance attach the sata event thread is initialized.
650  * Attachment points are created for all SATA ports of the HBA being attached.
651  * All HBA instance's SATA ports are probed and type of plugged devices is
652  * determined. For each device of a supported type, a target node is created.
653  *
654  * DDI_SUCCESS is returned when attachment process is successful,
655  * DDI_FAILURE is returned otherwise.
656  *
657  * For DDI_RESUME command:
658  * Not implemented at this time (postponed until phase 2 of the development).
659  */
660 int
661 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran,
662     ddi_attach_cmd_t cmd)
663 {
664 	sata_hba_inst_t	*sata_hba_inst;
665 	scsi_hba_tran_t *scsi_tran = NULL;
666 	int hba_attach_state = 0;
667 	char taskq_name[MAXPATHLEN];
668 
669 	SATADBG3(SATA_DBG_HBA_IF, NULL,
670 	    "sata_hba_attach: node %s (%s%d)\n",
671 	    ddi_node_name(dip), ddi_driver_name(dip),
672 	    ddi_get_instance(dip));
673 
674 	if (cmd == DDI_RESUME) {
675 		/*
676 		 * Postponed until phase 2 of the development
677 		 */
678 		return (DDI_FAILURE);
679 	}
680 
681 	if (cmd != DDI_ATTACH) {
682 		return (DDI_FAILURE);
683 	}
684 
685 	/* cmd == DDI_ATTACH */
686 
687 	if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) {
688 		SATA_LOG_D((NULL, CE_WARN,
689 		    "sata_hba_attach: invalid sata_hba_tran"));
690 		return (DDI_FAILURE);
691 	}
692 	/*
693 	 * Allocate and initialize SCSI tran structure.
694 	 * SATA copy of tran_bus_config is provided to create port nodes.
695 	 */
696 	scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
697 	if (scsi_tran == NULL)
698 		return (DDI_FAILURE);
699 	/*
700 	 * Allocate soft structure for SATA HBA instance.
701 	 * There is a separate softstate for each HBA instance.
702 	 */
703 	sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP);
704 	ASSERT(sata_hba_inst != NULL); /* this should not fail */
705 	mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL);
706 	hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST;
707 
708 	/*
709 	 * scsi_trans's tran_hba_private is used by SATA Framework to point to
710 	 * soft structure allocated by SATA framework for
711 	 * SATA HBA instance related data.
712 	 */
713 	scsi_tran->tran_hba_private	= sata_hba_inst;
714 	scsi_tran->tran_tgt_private	= NULL;
715 
716 	scsi_tran->tran_tgt_init	= sata_scsi_tgt_init;
717 	scsi_tran->tran_tgt_probe	= sata_scsi_tgt_probe;
718 	scsi_tran->tran_tgt_free	= sata_scsi_tgt_free;
719 
720 	scsi_tran->tran_start		= sata_scsi_start;
721 	scsi_tran->tran_reset		= sata_scsi_reset;
722 	scsi_tran->tran_abort		= sata_scsi_abort;
723 	scsi_tran->tran_getcap		= sata_scsi_getcap;
724 	scsi_tran->tran_setcap		= sata_scsi_setcap;
725 	scsi_tran->tran_init_pkt	= sata_scsi_init_pkt;
726 	scsi_tran->tran_destroy_pkt	= sata_scsi_destroy_pkt;
727 
728 	scsi_tran->tran_dmafree		= sata_scsi_dmafree;
729 	scsi_tran->tran_sync_pkt	= sata_scsi_sync_pkt;
730 
731 	scsi_tran->tran_reset_notify	= NULL;
732 	scsi_tran->tran_get_bus_addr	= NULL;
733 	scsi_tran->tran_quiesce		= NULL;
734 	scsi_tran->tran_unquiesce	= NULL;
735 	scsi_tran->tran_bus_reset	= NULL;
736 
737 	if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr,
738 	    scsi_tran, 0) != DDI_SUCCESS) {
739 #ifdef SATA_DEBUG
740 		cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed",
741 		    ddi_driver_name(dip), ddi_get_instance(dip));
742 #endif
743 		goto fail;
744 	}
745 	hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED;
746 
747 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) {
748 		if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
749 		    "sata", 1) != DDI_PROP_SUCCESS) {
750 			SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: "
751 			    "failed to create hba sata prop"));
752 			goto fail;
753 		}
754 	}
755 
756 	/*
757 	 * Save pointers in hba instance soft state.
758 	 */
759 	sata_hba_inst->satahba_scsi_tran = scsi_tran;
760 	sata_hba_inst->satahba_tran = sata_tran;
761 	sata_hba_inst->satahba_dip = dip;
762 
763 	/*
764 	 * Create a task queue to handle emulated commands completion
765 	 * Use node name, dash, instance number as the queue name.
766 	 */
767 	taskq_name[0] = '\0';
768 	(void) strlcat(taskq_name, DEVI(dip)->devi_node_name,
769 	    sizeof (taskq_name));
770 	(void) snprintf(taskq_name + strlen(taskq_name),
771 	    sizeof (taskq_name) - strlen(taskq_name),
772 	    "-%d", DEVI(dip)->devi_instance);
773 	sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1,
774 	    minclsyspri, 1, sata_tran->sata_tran_hba_num_cports,
775 	    TASKQ_DYNAMIC);
776 
777 	hba_attach_state |= HBA_ATTACH_STAGE_SETUP;
778 
779 	/*
780 	 * Create events thread if not created yet.
781 	 */
782 	sata_event_thread_control(1);
783 
784 	/*
785 	 * Link this hba instance into the list.
786 	 */
787 	mutex_enter(&sata_mutex);
788 
789 	if (sata_hba_list == NULL) {
790 		/*
791 		 * The first instance of HBA is attached.
792 		 * Set current/active default maximum NCQ/TCQ queue depth for
793 		 * all SATA devices. It is done here and now, to eliminate the
794 		 * possibility of the dynamic, programatic modification of the
795 		 * queue depth via global (and public) sata_max_queue_depth
796 		 * variable (this would require special handling in HBA drivers)
797 		 */
798 		sata_current_max_qdepth = sata_max_queue_depth;
799 		if (sata_current_max_qdepth > 32)
800 			sata_current_max_qdepth = 32;
801 		else if (sata_current_max_qdepth < 1)
802 			sata_current_max_qdepth = 1;
803 	}
804 
805 	sata_hba_inst->satahba_next = NULL;
806 	sata_hba_inst->satahba_prev = sata_hba_list_tail;
807 	if (sata_hba_list == NULL) {
808 		sata_hba_list = sata_hba_inst;
809 	}
810 	if (sata_hba_list_tail != NULL) {
811 		sata_hba_list_tail->satahba_next = sata_hba_inst;
812 	}
813 	sata_hba_list_tail = sata_hba_inst;
814 	mutex_exit(&sata_mutex);
815 	hba_attach_state |= HBA_ATTACH_STAGE_LINKED;
816 
817 	/*
818 	 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl
819 	 * SATA HBA driver should not use its own open/close entry points.
820 	 *
821 	 * Make sure that instance number doesn't overflow
822 	 * when forming minor numbers.
823 	 */
824 	ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT));
825 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
826 	    INST2DEVCTL(ddi_get_instance(dip)),
827 	    DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) {
828 #ifdef SATA_DEBUG
829 		cmn_err(CE_WARN, "sata_hba_attach: "
830 		    "cannot create devctl minor node");
831 #endif
832 		goto fail;
833 	}
834 
835 
836 	/*
837 	 * Set-up kstats here, if necessary.
838 	 * (postponed until future phase of the development).
839 	 */
840 
841 	/*
842 	 * Indicate that HBA is attached. This will enable events processing
843 	 * for this HBA.
844 	 */
845 	sata_hba_inst->satahba_attached = 1;
846 	/*
847 	 * Probe controller ports. This operation will describe a current
848 	 * controller/port/multipliers/device configuration and will create
849 	 * attachment points.
850 	 * We may end-up with just a controller with no devices attached.
851 	 * For the ports with a supported device attached, device target nodes
852 	 * are created and devices are initialized.
853 	 */
854 	sata_probe_ports(sata_hba_inst);
855 
856 	return (DDI_SUCCESS);
857 
858 fail:
859 	if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) {
860 		(void) sata_remove_hba_instance(dip);
861 		if (sata_hba_list == NULL)
862 			sata_event_thread_control(0);
863 	}
864 
865 	if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) {
866 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
867 		taskq_destroy(sata_hba_inst->satahba_taskq);
868 	}
869 
870 	if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED)
871 		(void) scsi_hba_detach(dip);
872 
873 	if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) {
874 		mutex_destroy(&sata_hba_inst->satahba_mutex);
875 		kmem_free((void *)sata_hba_inst,
876 		    sizeof (struct sata_hba_inst));
877 		scsi_hba_tran_free(scsi_tran);
878 	}
879 
880 	sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed",
881 	    ddi_driver_name(dip), ddi_get_instance(dip));
882 
883 	return (DDI_FAILURE);
884 }
885 
886 
887 /*
888  * Called by SATA HBA from to detach an instance of the driver.
889  *
890  * For DDI_DETACH command:
891  * Free local structures allocated for SATA HBA instance during
892  * sata_hba_attach processing.
893  *
894  * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise.
895  *
896  * For DDI_SUSPEND command:
897  * Not implemented at this time (postponed until phase 2 of the development)
898  * Returnd DDI_SUCCESS.
899  *
900  * When the last HBA instance is detached, the event daemon is terminated.
901  *
902  * NOTE: cport support only, no port multiplier support.
903  */
904 int
905 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
906 {
907 	dev_info_t	*tdip;
908 	sata_hba_inst_t	*sata_hba_inst;
909 	scsi_hba_tran_t *scsi_hba_tran;
910 	sata_cport_info_t *cportinfo;
911 	sata_drive_info_t *sdinfo;
912 	int ncport;
913 
914 	SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n",
915 	    ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip));
916 
917 	switch (cmd) {
918 	case DDI_DETACH:
919 
920 		if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
921 			return (DDI_FAILURE);
922 
923 		sata_hba_inst = scsi_hba_tran->tran_hba_private;
924 		if (sata_hba_inst == NULL)
925 			return (DDI_FAILURE);
926 
927 		if (scsi_hba_detach(dip) == DDI_FAILURE) {
928 			sata_hba_inst->satahba_attached = 1;
929 			return (DDI_FAILURE);
930 		}
931 
932 		/*
933 		 * Free all target nodes - at this point
934 		 * devices should be at least offlined
935 		 * otherwise scsi_hba_detach() should not be called.
936 		 */
937 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
938 		    ncport++) {
939 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
940 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
941 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
942 				if (sdinfo != NULL) {
943 					tdip = sata_get_target_dip(dip,
944 					    ncport);
945 					if (tdip != NULL) {
946 						if (ndi_devi_offline(tdip,
947 						    NDI_DEVI_REMOVE) !=
948 						    NDI_SUCCESS) {
949 							SATA_LOG_D((
950 							    sata_hba_inst,
951 							    CE_WARN,
952 							    "sata_hba_detach: "
953 							    "Target node not "
954 							    "removed !"));
955 							return (DDI_FAILURE);
956 						}
957 					}
958 				}
959 			}
960 		}
961 		/*
962 		 * Disable sata event daemon processing for this HBA
963 		 */
964 		sata_hba_inst->satahba_attached = 0;
965 
966 		/*
967 		 * Remove event daemon thread, if it is last HBA instance.
968 		 */
969 
970 		mutex_enter(&sata_mutex);
971 		if (sata_hba_list->satahba_next == NULL) {
972 			mutex_exit(&sata_mutex);
973 			sata_event_thread_control(0);
974 			mutex_enter(&sata_mutex);
975 		}
976 		mutex_exit(&sata_mutex);
977 
978 		/* Remove this HBA instance from the HBA list */
979 		sata_remove_hba_instance(dip);
980 
981 		/*
982 		 * At this point there should be no target nodes attached.
983 		 * Detach and destroy device and port info structures.
984 		 */
985 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
986 		    ncport++) {
987 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
988 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
989 				sdinfo =
990 				    cportinfo->cport_devp.cport_sata_drive;
991 				if (sdinfo != NULL) {
992 					/* Release device structure */
993 					kmem_free(sdinfo,
994 					    sizeof (sata_drive_info_t));
995 				}
996 				/* Release cport info */
997 				mutex_destroy(&cportinfo->cport_mutex);
998 				kmem_free(cportinfo,
999 				    sizeof (sata_cport_info_t));
1000 			}
1001 		}
1002 
1003 		scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran);
1004 
1005 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
1006 
1007 		taskq_destroy(sata_hba_inst->satahba_taskq);
1008 
1009 		mutex_destroy(&sata_hba_inst->satahba_mutex);
1010 		kmem_free((void *)sata_hba_inst,
1011 		    sizeof (struct sata_hba_inst));
1012 
1013 		return (DDI_SUCCESS);
1014 
1015 	case DDI_SUSPEND:
1016 		/*
1017 		 * Postponed until phase 2
1018 		 */
1019 		return (DDI_FAILURE);
1020 
1021 	default:
1022 		return (DDI_FAILURE);
1023 	}
1024 }
1025 
1026 
1027 /*
1028  * Called by an HBA drive from _fini() routine.
1029  * Unregisters SATA HBA instance/SATA framework pair from the scsi framework.
1030  */
1031 void
1032 sata_hba_fini(struct modlinkage *modlp)
1033 {
1034 	SATADBG1(SATA_DBG_HBA_IF, NULL,
1035 	    "sata_hba_fini: name %s\n",
1036 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
1037 
1038 	scsi_hba_fini(modlp);
1039 }
1040 
1041 
1042 /*
1043  * Default open and close routine for sata_hba framework.
1044  *
1045  */
1046 /*
1047  * Open devctl node.
1048  *
1049  * Returns:
1050  * 0 if node was open successfully, error code otherwise.
1051  *
1052  *
1053  */
1054 
1055 static int
1056 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1057 {
1058 #ifndef __lock_lint
1059 	_NOTE(ARGUNUSED(credp))
1060 #endif
1061 	int rv = 0;
1062 	dev_info_t *dip;
1063 	scsi_hba_tran_t *scsi_hba_tran;
1064 	sata_hba_inst_t	*sata_hba_inst;
1065 
1066 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL);
1067 
1068 	if (otyp != OTYP_CHR)
1069 		return (EINVAL);
1070 
1071 	dip = sata_devt_to_devinfo(*devp);
1072 	if (dip == NULL)
1073 		return (ENXIO);
1074 
1075 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1076 		return (ENXIO);
1077 
1078 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1079 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1080 		return (ENXIO);
1081 
1082 	mutex_enter(&sata_mutex);
1083 	if (flags & FEXCL) {
1084 		if (sata_hba_inst->satahba_open_flag != 0) {
1085 			rv = EBUSY;
1086 		} else {
1087 			sata_hba_inst->satahba_open_flag =
1088 			    SATA_DEVCTL_EXOPENED;
1089 		}
1090 	} else {
1091 		if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) {
1092 			rv = EBUSY;
1093 		} else {
1094 			sata_hba_inst->satahba_open_flag =
1095 			    SATA_DEVCTL_SOPENED;
1096 		}
1097 	}
1098 	mutex_exit(&sata_mutex);
1099 
1100 	return (rv);
1101 }
1102 
1103 
1104 /*
1105  * Close devctl node.
1106  * Returns:
1107  * 0 if node was closed successfully, error code otherwise.
1108  *
1109  */
1110 
1111 static int
1112 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
1113 {
1114 #ifndef __lock_lint
1115 	_NOTE(ARGUNUSED(credp))
1116 	_NOTE(ARGUNUSED(flag))
1117 #endif
1118 	dev_info_t *dip;
1119 	scsi_hba_tran_t *scsi_hba_tran;
1120 	sata_hba_inst_t	*sata_hba_inst;
1121 
1122 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL);
1123 
1124 	if (otyp != OTYP_CHR)
1125 		return (EINVAL);
1126 
1127 	dip = sata_devt_to_devinfo(dev);
1128 	if (dip == NULL)
1129 		return (ENXIO);
1130 
1131 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1132 		return (ENXIO);
1133 
1134 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1135 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1136 		return (ENXIO);
1137 
1138 	mutex_enter(&sata_mutex);
1139 	sata_hba_inst->satahba_open_flag = 0;
1140 	mutex_exit(&sata_mutex);
1141 	return (0);
1142 }
1143 
1144 
1145 
1146 /*
1147  * Standard IOCTL commands for SATA hotplugging.
1148  * Implemented DEVCTL_AP commands:
1149  * DEVCTL_AP_CONNECT
1150  * DEVCTL_AP_DISCONNECT
1151  * DEVCTL_AP_CONFIGURE
1152  * DEVCTL_UNCONFIGURE
1153  * DEVCTL_AP_CONTROL
1154  *
1155  * Commands passed to default ndi ioctl handler:
1156  * DEVCTL_DEVICE_GETSTATE
1157  * DEVCTL_DEVICE_ONLINE
1158  * DEVCTL_DEVICE_OFFLINE
1159  * DEVCTL_DEVICE_REMOVE
1160  * DEVCTL_DEVICE_INSERT
1161  * DEVCTL_BUS_GETSTATE
1162  *
1163  * All other cmds are passed to HBA if it provide ioctl handler, or failed
1164  * if not.
1165  *
1166  * Returns:
1167  * 0 if successful,
1168  * error code if operation failed.
1169  *
1170  * NOTE: Port Multiplier is not supported.
1171  *
1172  */
1173 
1174 static int
1175 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1176     int *rvalp)
1177 {
1178 #ifndef __lock_lint
1179 	_NOTE(ARGUNUSED(credp))
1180 	_NOTE(ARGUNUSED(rvalp))
1181 #endif
1182 	int rv = 0;
1183 	int32_t	comp_port = -1;
1184 	dev_info_t *dip;
1185 	devctl_ap_state_t ap_state;
1186 	struct devctl_iocdata *dcp = NULL;
1187 	scsi_hba_tran_t *scsi_hba_tran;
1188 	sata_hba_inst_t *sata_hba_inst;
1189 	sata_device_t sata_device;
1190 	sata_cport_info_t *cportinfo;
1191 	int cport, pmport, qual;
1192 	int rval = SATA_SUCCESS;
1193 
1194 	dip = sata_devt_to_devinfo(dev);
1195 	if (dip == NULL)
1196 		return (ENXIO);
1197 
1198 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1199 		return (ENXIO);
1200 
1201 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1202 	if (sata_hba_inst == NULL)
1203 		return (ENXIO);
1204 
1205 	if (sata_hba_inst->satahba_tran == NULL)
1206 		return (ENXIO);
1207 
1208 	switch (cmd) {
1209 
1210 	case DEVCTL_DEVICE_GETSTATE:
1211 	case DEVCTL_DEVICE_ONLINE:
1212 	case DEVCTL_DEVICE_OFFLINE:
1213 	case DEVCTL_DEVICE_REMOVE:
1214 	case DEVCTL_BUS_GETSTATE:
1215 		/*
1216 		 * There may be more cases that we want to pass to default
1217 		 * handler rather than fail them.
1218 		 */
1219 		return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
1220 	}
1221 
1222 	/* read devctl ioctl data */
1223 	if (cmd != DEVCTL_AP_CONTROL) {
1224 		if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1225 			return (EFAULT);
1226 
1227 		if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) ==
1228 		    -1) {
1229 			if (dcp)
1230 				ndi_dc_freehdl(dcp);
1231 			return (EINVAL);
1232 		}
1233 
1234 		cport = SCSI_TO_SATA_CPORT(comp_port);
1235 		pmport = SCSI_TO_SATA_PMPORT(comp_port);
1236 		/* Only cport is considered now, i.e. SATA_ADDR_CPORT */
1237 		qual = SATA_ADDR_CPORT;
1238 		if (sata_validate_sata_address(sata_hba_inst, cport, pmport,
1239 		    qual) != 0) {
1240 			ndi_dc_freehdl(dcp);
1241 			return (EINVAL);
1242 		}
1243 
1244 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1245 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1246 		    cport_mutex);
1247 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1248 			/*
1249 			 * Cannot process ioctl request now. Come back later.
1250 			 */
1251 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1252 			    cport_mutex);
1253 			ndi_dc_freehdl(dcp);
1254 			return (EBUSY);
1255 		}
1256 		/* Block event processing for this port */
1257 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1258 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1259 
1260 		sata_device.satadev_addr.cport = cport;
1261 		sata_device.satadev_addr.pmport = pmport;
1262 		sata_device.satadev_addr.qual = qual;
1263 		sata_device.satadev_rev = SATA_DEVICE_REV;
1264 	}
1265 
1266 	switch (cmd) {
1267 
1268 	case DEVCTL_AP_DISCONNECT:
1269 
1270 		/*
1271 		 * Normally, cfgadm sata plugin will try to offline
1272 		 * (unconfigure) device before this request. Nevertheless,
1273 		 * if a device is still configured, we need to
1274 		 * attempt to offline and unconfigure device first, and we will
1275 		 * deactivate the port regardless of the unconfigure
1276 		 * operation results.
1277 		 *
1278 		 */
1279 		rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device);
1280 
1281 		break;
1282 
1283 	case DEVCTL_AP_UNCONFIGURE:
1284 
1285 		/*
1286 		 * The unconfigure operation uses generic nexus operation to
1287 		 * offline a device. It leaves a target device node attached.
1288 		 * and obviously sata_drive_info attached as well, because
1289 		 * from the hardware point of view nothing has changed.
1290 		 */
1291 		rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device);
1292 		break;
1293 
1294 	case DEVCTL_AP_CONNECT:
1295 	{
1296 		/*
1297 		 * The sata cfgadm pluging will invoke this operation only if
1298 		 * port was found in the disconnect state (failed state
1299 		 * is also treated as the disconnected state).
1300 		 * If port activation is successful and a device is found
1301 		 * attached to the port, the initialization sequence is
1302 		 * executed to probe the port and attach
1303 		 * a device structure to a port structure. The device is not
1304 		 * set in configured state (system-wise) by this operation.
1305 		 */
1306 
1307 		rv = sata_ioctl_connect(sata_hba_inst, &sata_device);
1308 
1309 		break;
1310 	}
1311 
1312 	case DEVCTL_AP_CONFIGURE:
1313 	{
1314 		/*
1315 		 * A port may be in an active or shutdown state.
1316 		 * If port is in a failed state, operation is aborted.
1317 		 * If a port is in a shutdown state, sata_tran_port_activate()
1318 		 * is invoked prior to any other operation.
1319 		 *
1320 		 * Onlining the device involves creating a new target node.
1321 		 * If there is an old target node present (belonging to
1322 		 * previously removed device), the operation is aborted - the
1323 		 * old node has to be released and removed before configure
1324 		 * operation is attempted.
1325 		 */
1326 
1327 		rv = sata_ioctl_configure(sata_hba_inst, &sata_device);
1328 
1329 		break;
1330 	}
1331 
1332 	case DEVCTL_AP_GETSTATE:
1333 
1334 		sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state);
1335 
1336 		ap_state.ap_last_change = (time_t)-1;
1337 		ap_state.ap_error_code = 0;
1338 		ap_state.ap_in_transition = 0;
1339 
1340 		/* Copy the return AP-state information to the user space */
1341 		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
1342 			rv = EFAULT;
1343 		}
1344 		break;
1345 
1346 	case DEVCTL_AP_CONTROL:
1347 	{
1348 		/*
1349 		 * Generic devctl for hardware specific functionality
1350 		 */
1351 		sata_ioctl_data_t	ioc;
1352 
1353 		ASSERT(dcp == NULL);
1354 
1355 		/* Copy in user ioctl data first */
1356 #ifdef _MULTI_DATAMODEL
1357 		if (ddi_model_convert_from(mode & FMODELS) ==
1358 		    DDI_MODEL_ILP32) {
1359 
1360 			sata_ioctl_data_32_t	ioc32;
1361 
1362 			if (ddi_copyin((void *)arg, (void *)&ioc32,
1363 			    sizeof (ioc32), mode) != 0) {
1364 				rv = EFAULT;
1365 				break;
1366 			}
1367 			ioc.cmd 	= (uint_t)ioc32.cmd;
1368 			ioc.port	= (uint_t)ioc32.port;
1369 			ioc.get_size	= (uint_t)ioc32.get_size;
1370 			ioc.buf		= (caddr_t)(uintptr_t)ioc32.buf;
1371 			ioc.bufsiz	= (uint_t)ioc32.bufsiz;
1372 			ioc.misc_arg	= (uint_t)ioc32.misc_arg;
1373 		} else
1374 #endif /* _MULTI_DATAMODEL */
1375 		if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
1376 		    mode) != 0) {
1377 			return (EFAULT);
1378 		}
1379 
1380 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
1381 		    "sata_hba_ioctl: DEVCTL_AP_CONTROL "
1382 		    "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port);
1383 
1384 		/*
1385 		 * To avoid BE/LE and 32/64 issues, a get_size always returns
1386 		 * a 32-bit number.
1387 		 */
1388 		if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
1389 			return (EINVAL);
1390 		}
1391 		/* validate address */
1392 		cport = SCSI_TO_SATA_CPORT(ioc.port);
1393 		pmport = SCSI_TO_SATA_PMPORT(ioc.port);
1394 		qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port);
1395 
1396 		/* Override address qualifier - handle cport only for now */
1397 		qual = SATA_ADDR_CPORT;
1398 
1399 		if (sata_validate_sata_address(sata_hba_inst, cport,
1400 		    pmport, qual) != 0)
1401 			return (EINVAL);
1402 
1403 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1404 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1405 		    cport_mutex);
1406 		/* Is the port locked by event processing daemon ? */
1407 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1408 			/*
1409 			 * Cannot process ioctl request now. Come back later
1410 			 */
1411 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1412 			    cport_mutex);
1413 			return (EBUSY);
1414 		}
1415 		/* Block event processing for this port */
1416 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1417 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1418 
1419 
1420 		sata_device.satadev_addr.cport = cport;
1421 		sata_device.satadev_addr.pmport = pmport;
1422 		sata_device.satadev_addr.qual = qual;
1423 		sata_device.satadev_rev = SATA_DEVICE_REV;
1424 
1425 		switch (ioc.cmd) {
1426 
1427 		case SATA_CFGA_RESET_PORT:
1428 			/*
1429 			 * There is no protection for configured device.
1430 			 */
1431 			rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device);
1432 			break;
1433 
1434 		case SATA_CFGA_RESET_DEVICE:
1435 			/*
1436 			 * There is no protection for configured device.
1437 			 */
1438 			rv = sata_ioctl_reset_device(sata_hba_inst,
1439 			    &sata_device);
1440 			break;
1441 
1442 		case SATA_CFGA_RESET_ALL:
1443 			/*
1444 			 * There is no protection for configured devices.
1445 			 */
1446 			rv = sata_ioctl_reset_all(sata_hba_inst);
1447 			/*
1448 			 * We return here, because common return is for
1449 			 * a single port operation - we have already unlocked
1450 			 * all ports and no dc handle was allocated.
1451 			 */
1452 			return (rv);
1453 
1454 		case SATA_CFGA_PORT_DEACTIVATE:
1455 			/*
1456 			 * Arbitrarily unconfigure attached device, if any.
1457 			 * Even if the unconfigure fails, proceed with the
1458 			 * port deactivation.
1459 			 */
1460 			rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device);
1461 
1462 			break;
1463 
1464 		case SATA_CFGA_PORT_ACTIVATE:
1465 
1466 			rv = sata_ioctl_activate(sata_hba_inst, &sata_device);
1467 			break;
1468 
1469 		case SATA_CFGA_PORT_SELF_TEST:
1470 
1471 			rv = sata_ioctl_port_self_test(sata_hba_inst,
1472 			    &sata_device);
1473 			break;
1474 
1475 		case SATA_CFGA_GET_DEVICE_PATH:
1476 			if (qual == SATA_ADDR_CPORT)
1477 				sata_device.satadev_addr.qual =
1478 				    SATA_ADDR_DCPORT;
1479 			else
1480 				sata_device.satadev_addr.qual =
1481 				    SATA_ADDR_DPMPORT;
1482 			rv = sata_ioctl_get_device_path(sata_hba_inst,
1483 			    &sata_device, &ioc, mode);
1484 			break;
1485 
1486 		case SATA_CFGA_GET_AP_TYPE:
1487 
1488 			rv = sata_ioctl_get_ap_type(sata_hba_inst,
1489 			    &sata_device, &ioc, mode);
1490 			break;
1491 
1492 		case SATA_CFGA_GET_MODEL_INFO:
1493 
1494 			rv = sata_ioctl_get_model_info(sata_hba_inst,
1495 			    &sata_device, &ioc, mode);
1496 			break;
1497 
1498 		case SATA_CFGA_GET_REVFIRMWARE_INFO:
1499 
1500 			rv = sata_ioctl_get_revfirmware_info(sata_hba_inst,
1501 			    &sata_device, &ioc, mode);
1502 			break;
1503 
1504 		case SATA_CFGA_GET_SERIALNUMBER_INFO:
1505 
1506 			rv = sata_ioctl_get_serialnumber_info(sata_hba_inst,
1507 			    &sata_device, &ioc, mode);
1508 			break;
1509 
1510 		default:
1511 			rv = EINVAL;
1512 			break;
1513 
1514 		} /* End of DEVCTL_AP_CONTROL cmd switch */
1515 
1516 		break;
1517 	}
1518 
1519 	default:
1520 	{
1521 		/*
1522 		 * If we got here, we got an IOCTL that SATA HBA Framework
1523 		 * does not recognize. Pass ioctl to HBA driver, in case
1524 		 * it could process it.
1525 		 */
1526 		sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran;
1527 		dev_info_t	*mydip = SATA_DIP(sata_hba_inst);
1528 
1529 		SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1530 		    "IOCTL 0x%2x not supported in SATA framework, "
1531 		    "passthrough to HBA", cmd);
1532 
1533 		if (sata_tran->sata_tran_ioctl == NULL) {
1534 			rv = EINVAL;
1535 			break;
1536 		}
1537 		rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg);
1538 		if (rval != 0) {
1539 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1540 			    "IOCTL 0x%2x failed in HBA", cmd);
1541 			rv = rval;
1542 		}
1543 		break;
1544 	}
1545 
1546 	} /* End of main IOCTL switch */
1547 
1548 	if (dcp) {
1549 		ndi_dc_freehdl(dcp);
1550 	}
1551 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1552 	cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
1553 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1554 
1555 	return (rv);
1556 }
1557 
1558 
1559 /*
1560  * Create error retrieval sata packet
1561  *
1562  * A sata packet is allocated and set-up to contain specified error retrieval
1563  * command and appropriate dma-able data buffer.
1564  * No association with any scsi packet is made and no callback routine is
1565  * specified.
1566  *
1567  * Returns a pointer to sata packet upon successfull packet creation.
1568  * Returns NULL, if packet cannot be created.
1569  */
1570 sata_pkt_t *
1571 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device,
1572     int pkt_type)
1573 {
1574 	sata_hba_inst_t	*sata_hba_inst;
1575 	sata_pkt_txlate_t *spx;
1576 	sata_pkt_t *spkt;
1577 	sata_drive_info_t *sdinfo;
1578 
1579 	mutex_enter(&sata_mutex);
1580 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1581 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1582 		if (SATA_DIP(sata_hba_inst) == dip)
1583 			break;
1584 	}
1585 	mutex_exit(&sata_mutex);
1586 	ASSERT(sata_hba_inst != NULL);
1587 
1588 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
1589 	if (sdinfo == NULL) {
1590 		sata_log(sata_hba_inst, CE_WARN,
1591 		    "sata: error recovery request for non-attached device at "
1592 		    "cport %d", sata_device->satadev_addr.cport);
1593 		return (NULL);
1594 	}
1595 
1596 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1597 	spx->txlt_sata_hba_inst = sata_hba_inst;
1598 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
1599 	spkt = sata_pkt_alloc(spx, NULL);
1600 	if (spkt == NULL) {
1601 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1602 		return (NULL);
1603 	}
1604 	/* address is needed now */
1605 	spkt->satapkt_device.satadev_addr = sata_device->satadev_addr;
1606 
1607 	switch (pkt_type) {
1608 	case SATA_ERR_RETR_PKT_TYPE_NCQ:
1609 		if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS)
1610 			return (spkt);
1611 		break;
1612 
1613 	case SATA_ERR_RETR_PKT_TYPE_ATAPI:
1614 		if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS)
1615 			return (spkt);
1616 		break;
1617 
1618 	default:
1619 		break;
1620 	}
1621 
1622 	sata_pkt_free(spx);
1623 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1624 	return (NULL);
1625 
1626 }
1627 
1628 
1629 /*
1630  * Free error retrieval sata packet
1631  *
1632  * Free sata packet and any associated resources allocated previously by
1633  * sata_get_error_retrieval_pkt().
1634  *
1635  * Void return.
1636  */
1637 void
1638 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt)
1639 {
1640 	sata_pkt_txlate_t *spx =
1641 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1642 
1643 	ASSERT(sata_pkt != NULL);
1644 
1645 	sata_free_local_buffer(spx);
1646 	sata_pkt_free(spx);
1647 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1648 
1649 }
1650 
1651 /*
1652  * sata_name_child is for composing the name of the node
1653  * the format of the name is "target,0".
1654  */
1655 static int
1656 sata_name_child(dev_info_t *dip, char *name, int namelen)
1657 {
1658 	int target;
1659 
1660 	target = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1661 	    DDI_PROP_DONTPASS, "target", -1);
1662 	if (target == -1)
1663 		return (DDI_FAILURE);
1664 	(void) snprintf(name, namelen, "%x,0", target);
1665 	return (DDI_SUCCESS);
1666 }
1667 
1668 
1669 
1670 /* ****************** SCSA required entry points *********************** */
1671 
1672 /*
1673  * Implementation of scsi tran_tgt_init.
1674  * sata_scsi_tgt_init() initializes scsi_device structure
1675  *
1676  * If successful, DDI_SUCCESS is returned.
1677  * DDI_FAILURE is returned if addressed device does not exist
1678  */
1679 
1680 static int
1681 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1682     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1683 {
1684 #ifndef __lock_lint
1685 	_NOTE(ARGUNUSED(hba_dip))
1686 	_NOTE(ARGUNUSED(tgt_dip))
1687 #endif
1688 	sata_device_t		sata_device;
1689 	sata_drive_info_t	*sdinfo;
1690 	struct sata_id		*sid;
1691 	sata_hba_inst_t		*sata_hba_inst;
1692 	char			model[SATA_ID_MODEL_LEN + 1];
1693 	char			fw[SATA_ID_FW_LEN + 1];
1694 	char			*vid, *pid;
1695 	int			i;
1696 
1697 	/*
1698 	 * Fail tran_tgt_init for .conf stub node
1699 	 */
1700 	if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
1701 		(void) ndi_merge_node(tgt_dip, sata_name_child);
1702 		ddi_set_name_addr(tgt_dip, NULL);
1703 		return (DDI_FAILURE);
1704 	}
1705 
1706 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
1707 
1708 	/* Validate scsi device address */
1709 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
1710 	    &sata_device) != 0)
1711 		return (DDI_FAILURE);
1712 
1713 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1714 	    sata_device.satadev_addr.cport)));
1715 
1716 	/* sata_device now contains a valid sata address */
1717 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
1718 	if (sdinfo == NULL) {
1719 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1720 		    sata_device.satadev_addr.cport)));
1721 		return (DDI_FAILURE);
1722 	}
1723 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1724 	    sata_device.satadev_addr.cport)));
1725 
1726 	/*
1727 	 * Check if we need to create a legacy devid (i.e cmdk style) for
1728 	 * the target disks.
1729 	 *
1730 	 * HBA devinfo node will have the property "use-cmdk-devid-format"
1731 	 * if we need to create cmdk-style devid for all the disk devices
1732 	 * attached to this controller. This property may have been set
1733 	 * from HBA driver's .conf file or by the HBA driver in its
1734 	 * attach(9F) function.
1735 	 */
1736 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
1737 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
1738 	    "use-cmdk-devid-format", 0) == 1)) {
1739 		/* register a legacy devid for this target node */
1740 		sata_target_devid_register(tgt_dip, sdinfo);
1741 	}
1742 
1743 
1744 	/*
1745 	 * 'Identify Device Data' does not always fit in standard SCSI
1746 	 * INQUIRY data, so establish INQUIRY_* properties with full-form
1747 	 * of information.
1748 	 */
1749 	sid = &sdinfo->satadrv_id;
1750 #ifdef	_LITTLE_ENDIAN
1751 	swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
1752 	swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
1753 #else	/* _LITTLE_ENDIAN */
1754 	bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN);
1755 	bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN);
1756 #endif	/* _LITTLE_ENDIAN */
1757 	model[SATA_ID_MODEL_LEN] = 0;
1758 	fw[SATA_ID_FW_LEN] = 0;
1759 
1760 	/* split model into into vid/pid */
1761 	for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++)
1762 		if ((*pid == ' ') || (*pid == '\t'))
1763 			break;
1764 	if (i < SATA_ID_MODEL_LEN) {
1765 		vid = model;
1766 		*pid++ = 0;		/* terminate vid, establish pid */
1767 	} else {
1768 		vid = NULL;		/* vid will stay "ATA     " */
1769 		pid = model;		/* model is all pid */
1770 	}
1771 
1772 	if (vid)
1773 		(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_VENDOR_ID,
1774 		    vid, strlen(vid));
1775 	if (pid)
1776 		(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID,
1777 		    pid, strlen(pid));
1778 	(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_REVISION_ID,
1779 	    fw, strlen(fw));
1780 
1781 	return (DDI_SUCCESS);
1782 }
1783 
1784 /*
1785  * Implementation of scsi tran_tgt_probe.
1786  * Probe target, by calling default scsi routine scsi_hba_probe()
1787  */
1788 static int
1789 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void))
1790 {
1791 	sata_hba_inst_t *sata_hba_inst =
1792 	    (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private);
1793 	int rval;
1794 
1795 	rval = scsi_hba_probe(sd, callback);
1796 
1797 	if (rval == SCSIPROBE_EXISTS) {
1798 		/*
1799 		 * Set property "pm-capable" on the target device node, so that
1800 		 * the target driver will not try to fetch scsi cycle counters
1801 		 * before enabling device power-management.
1802 		 */
1803 		if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
1804 		    "pm-capable", 1)) != DDI_PROP_SUCCESS) {
1805 			sata_log(sata_hba_inst, CE_WARN,
1806 			    "SATA device at port %d: "
1807 			    "will not be power-managed ",
1808 			    SCSI_TO_SATA_CPORT(sd->sd_address.a_target));
1809 			SATA_LOG_D((sata_hba_inst, CE_WARN,
1810 			    "failure updating pm-capable property"));
1811 		}
1812 	}
1813 	return (rval);
1814 }
1815 
1816 /*
1817  * Implementation of scsi tran_tgt_free.
1818  * Release all resources allocated for scsi_device
1819  */
1820 static void
1821 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1822     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1823 {
1824 #ifndef __lock_lint
1825 	_NOTE(ARGUNUSED(hba_dip))
1826 #endif
1827 	sata_device_t		sata_device;
1828 	sata_drive_info_t	*sdinfo;
1829 	sata_hba_inst_t		*sata_hba_inst;
1830 	ddi_devid_t		devid;
1831 
1832 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
1833 
1834 	/* Validate scsi device address */
1835 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
1836 	    &sata_device) != 0)
1837 		return;
1838 
1839 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1840 	    sata_device.satadev_addr.cport)));
1841 
1842 	/* sata_device now should contain a valid sata address */
1843 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
1844 	if (sdinfo == NULL) {
1845 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1846 		    sata_device.satadev_addr.cport)));
1847 		return;
1848 	}
1849 	/*
1850 	 * We did not allocate any resources in sata_scsi_tgt_init()
1851 	 * other than few properties.
1852 	 * Free them.
1853 	 */
1854 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1855 	    sata_device.satadev_addr.cport)));
1856 	(void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable");
1857 
1858 	/*
1859 	 * If devid was previously created but not freed up from
1860 	 * sd(7D) driver (i.e during detach(9F)) then do it here.
1861 	 */
1862 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
1863 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
1864 	    "use-cmdk-devid-format", 0) == 1) &&
1865 	    (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) {
1866 		ddi_devid_unregister(tgt_dip);
1867 		ddi_devid_free(devid);
1868 	}
1869 }
1870 
1871 /*
1872  * Implementation of scsi tran_init_pkt
1873  * Upon successful return, scsi pkt buffer has DMA resources allocated.
1874  *
1875  * It seems that we should always allocate pkt, even if the address is
1876  * for non-existing device - just use some default for dma_attr.
1877  * The reason is that there is no way to communicate this to a caller here.
1878  * Subsequent call to sata_scsi_start may fail appropriately.
1879  * Simply returning NULL does not seem to discourage a target driver...
1880  *
1881  * Returns a pointer to initialized scsi_pkt, or NULL otherwise.
1882  */
1883 static struct scsi_pkt *
1884 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
1885     struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
1886     int (*callback)(caddr_t), caddr_t arg)
1887 {
1888 	sata_hba_inst_t *sata_hba_inst =
1889 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
1890 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
1891 	sata_device_t sata_device;
1892 	sata_drive_info_t *sdinfo;
1893 	sata_pkt_txlate_t *spx;
1894 	ddi_dma_attr_t cur_dma_attr;
1895 	int rval;
1896 	boolean_t new_pkt = TRUE;
1897 
1898 	ASSERT(ap->a_hba_tran->tran_hba_dip == dip);
1899 
1900 	/*
1901 	 * We need to translate the address, even if it could be
1902 	 * a bogus one, for a non-existing device
1903 	 */
1904 	sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
1905 	sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target);
1906 	sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
1907 	sata_device.satadev_rev = SATA_DEVICE_REV;
1908 
1909 	if (pkt == NULL) {
1910 		/*
1911 		 * Have to allocate a brand new scsi packet.
1912 		 * We need to operate with auto request sense enabled.
1913 		 */
1914 		pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen,
1915 		    MAX(statuslen, sizeof (struct scsi_arq_status)),
1916 		    tgtlen, sizeof (sata_pkt_txlate_t), callback, arg);
1917 
1918 		if (pkt == NULL)
1919 			return (NULL);
1920 
1921 		/* Fill scsi packet structure */
1922 		pkt->pkt_comp		= (void (*)())NULL;
1923 		pkt->pkt_time		= 0;
1924 		pkt->pkt_resid		= 0;
1925 		pkt->pkt_statistics	= 0;
1926 		pkt->pkt_reason		= 0;
1927 
1928 		/*
1929 		 * pkt_hba_private will point to sata pkt txlate structure
1930 		 */
1931 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
1932 		bzero(spx, sizeof (sata_pkt_txlate_t));
1933 
1934 		spx->txlt_scsi_pkt = pkt;
1935 		spx->txlt_sata_hba_inst = sata_hba_inst;
1936 
1937 		/* Allocate sata_pkt */
1938 		spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback);
1939 		if (spx->txlt_sata_pkt == NULL) {
1940 			/* Could not allocate sata pkt */
1941 			scsi_hba_pkt_free(ap, pkt);
1942 			return (NULL);
1943 		}
1944 		/* Set sata address */
1945 		spx->txlt_sata_pkt->satapkt_device.satadev_addr =
1946 		    sata_device.satadev_addr;
1947 		spx->txlt_sata_pkt->satapkt_device.satadev_rev =
1948 		    sata_device.satadev_rev;
1949 
1950 		if ((bp == NULL) || (bp->b_bcount == 0))
1951 			return (pkt);
1952 
1953 		spx->txlt_total_residue = bp->b_bcount;
1954 	} else {
1955 		new_pkt = FALSE;
1956 		/*
1957 		 * Packet was preallocated/initialized by previous call
1958 		 */
1959 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
1960 
1961 		if ((bp == NULL) || (bp->b_bcount == 0)) {
1962 			return (pkt);
1963 		}
1964 
1965 		/* Pkt is available already: spx->txlt_scsi_pkt == pkt; */
1966 	}
1967 
1968 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
1969 
1970 	/*
1971 	 * We use an adjusted version of the dma_attr, to account
1972 	 * for device addressing limitations.
1973 	 * sata_adjust_dma_attr() will handle sdinfo == NULL which may
1974 	 * happen when a device is not yet configured.
1975 	 */
1976 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1977 	    sata_device.satadev_addr.cport)));
1978 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
1979 	    &spx->txlt_sata_pkt->satapkt_device);
1980 	/* NULL sdinfo may be passsed to sata_adjust_dma_attr() */
1981 	sata_adjust_dma_attr(sdinfo,
1982 	    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
1983 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1984 	    sata_device.satadev_addr.cport)));
1985 	/*
1986 	 * Allocate necessary DMA resources for the packet's data buffer
1987 	 * NOTE:
1988 	 * In case of read/write commands, DMA resource allocation here is
1989 	 * based on the premise that the transfer length specified in
1990 	 * the read/write scsi cdb will match exactly DMA resources -
1991 	 * returning correct packet residue is crucial.
1992 	 */
1993 	if ((rval = sata_dma_buf_setup(spx, flags, callback, arg,
1994 	    &cur_dma_attr)) != DDI_SUCCESS) {
1995 		/*
1996 		 * If a DMA allocation request fails with
1997 		 * DDI_DMA_NOMAPPING, indicate the error by calling
1998 		 * bioerror(9F) with bp and an error code of EFAULT.
1999 		 * If a DMA allocation request fails with
2000 		 * DDI_DMA_TOOBIG, indicate the error by calling
2001 		 * bioerror(9F) with bp and an error code of EINVAL.
2002 		 * For DDI_DMA_NORESOURCES, we may have some of them allocated.
2003 		 * Request may be repeated later - there is no real error.
2004 		 */
2005 		switch (rval) {
2006 		case DDI_DMA_NORESOURCES:
2007 			bioerror(bp, 0);
2008 			break;
2009 		case DDI_DMA_NOMAPPING:
2010 		case DDI_DMA_BADATTR:
2011 			bioerror(bp, EFAULT);
2012 			break;
2013 		case DDI_DMA_TOOBIG:
2014 		default:
2015 			bioerror(bp, EINVAL);
2016 			break;
2017 		}
2018 		if (new_pkt == TRUE) {
2019 			/*
2020 			 * Since this is a new packet, we can clean-up
2021 			 * everything
2022 			 */
2023 			sata_scsi_destroy_pkt(ap, pkt);
2024 		} else {
2025 			/*
2026 			 * This is a re-used packet. It will be target driver's
2027 			 * responsibility to eventually destroy it (which
2028 			 * will free allocated resources).
2029 			 * Here, we just "complete" the request, leaving
2030 			 * allocated resources intact, so the request may
2031 			 * be retried.
2032 			 */
2033 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2034 			sata_pkt_free(spx);
2035 		}
2036 		return (NULL);
2037 	}
2038 	/* Set number of bytes that are not yet accounted for */
2039 	pkt->pkt_resid = spx->txlt_total_residue;
2040 	ASSERT(pkt->pkt_resid >= 0);
2041 
2042 	return (pkt);
2043 }
2044 
2045 /*
2046  * Implementation of scsi tran_start.
2047  * Translate scsi cmd into sata operation and return status.
2048  * ATAPI CDBs are passed to ATAPI devices - the device determines what commands
2049  * are supported.
2050  * For SATA hard disks, supported scsi commands:
2051  * SCMD_INQUIRY
2052  * SCMD_TEST_UNIT_READY
2053  * SCMD_START_STOP
2054  * SCMD_READ_CAPACITY
2055  * SCMD_REQUEST_SENSE
2056  * SCMD_LOG_SENSE_G1
2057  * SCMD_LOG_SELECT_G1
2058  * SCMD_MODE_SENSE	(specific pages)
2059  * SCMD_MODE_SENSE_G1	(specific pages)
2060  * SCMD_MODE_SELECT	(specific pages)
2061  * SCMD_MODE_SELECT_G1	(specific pages)
2062  * SCMD_SYNCHRONIZE_CACHE
2063  * SCMD_SYNCHRONIZE_CACHE_G1
2064  * SCMD_READ
2065  * SCMD_READ_G1
2066  * SCMD_READ_G4
2067  * SCMD_READ_G5
2068  * SCMD_WRITE
2069  * SCMD_WRITE_BUFFER
2070  * SCMD_WRITE_G1
2071  * SCMD_WRITE_G4
2072  * SCMD_WRITE_G5
2073  * SCMD_SEEK		(noop)
2074  * SCMD_SDIAG
2075  *
2076  * All other commands are rejected as unsupported.
2077  *
2078  * Returns:
2079  * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver
2080  * for execution. TRAN_ACCEPT may be returned also if device was removed but
2081  * a callback could be scheduled.
2082  * TRAN_BADPKT if cmd was directed to invalid address.
2083  * TRAN_FATAL_ERROR is command was rejected due to hardware error, including
2084  * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device
2085  * was removed and there was no callback specified in scsi pkt.
2086  * TRAN_BUSY if command could not be executed becasue HBA driver or SATA
2087  * framework was busy performing some other operation(s).
2088  *
2089  */
2090 static int
2091 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
2092 {
2093 	sata_hba_inst_t *sata_hba_inst =
2094 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2095 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2096 	sata_drive_info_t *sdinfo;
2097 	struct buf *bp;
2098 	int cport;
2099 	int rval;
2100 
2101 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2102 	    "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]);
2103 
2104 	ASSERT(spx != NULL &&
2105 	    spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL);
2106 
2107 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
2108 
2109 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2110 	sdinfo = sata_get_device_info(sata_hba_inst,
2111 	    &spx->txlt_sata_pkt->satapkt_device);
2112 	if (sdinfo == NULL ||
2113 	    SATA_CPORT_INFO(sata_hba_inst, cport)->cport_tgtnode_clean ==
2114 	    B_FALSE ||
2115 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2116 
2117 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2118 		pkt->pkt_reason = CMD_DEV_GONE;
2119 		/*
2120 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2121 		 * only in callback function (for normal requests) and
2122 		 * in the dump code path.
2123 		 * So, if the callback is available, we need to do
2124 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2125 		 */
2126 		if (pkt->pkt_comp != NULL) {
2127 			/* scsi callback required */
2128 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2129 			    (task_func_t *)pkt->pkt_comp,
2130 			    (void *)pkt, TQ_SLEEP) == NULL)
2131 				/* Scheduling the callback failed */
2132 				return (TRAN_BUSY);
2133 			return (TRAN_ACCEPT);
2134 		}
2135 		/* No callback available */
2136 		return (TRAN_FATAL_ERROR);
2137 	}
2138 
2139 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
2140 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2141 		rval = sata_txlt_atapi(spx);
2142 		SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2143 		    "sata_scsi_start atapi: rval %d\n", rval);
2144 		return (rval);
2145 	}
2146 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2147 
2148 	/* ATA Disk commands processing starts here */
2149 
2150 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2151 
2152 	switch (pkt->pkt_cdbp[0]) {
2153 
2154 	case SCMD_INQUIRY:
2155 		/* Mapped to identify device */
2156 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2157 			bp_mapin(bp);
2158 		rval = sata_txlt_inquiry(spx);
2159 		break;
2160 
2161 	case SCMD_TEST_UNIT_READY:
2162 		/*
2163 		 * SAT "SATA to ATA Translation" doc specifies translation
2164 		 * to ATA CHECK POWER MODE.
2165 		 */
2166 		rval = sata_txlt_test_unit_ready(spx);
2167 		break;
2168 
2169 	case SCMD_START_STOP:
2170 		/* Mapping depends on the command */
2171 		rval = sata_txlt_start_stop_unit(spx);
2172 		break;
2173 
2174 	case SCMD_READ_CAPACITY:
2175 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2176 			bp_mapin(bp);
2177 		rval = sata_txlt_read_capacity(spx);
2178 		break;
2179 
2180 	case SCMD_REQUEST_SENSE:
2181 		/*
2182 		 * Always No Sense, since we force ARQ
2183 		 */
2184 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2185 			bp_mapin(bp);
2186 		rval = sata_txlt_request_sense(spx);
2187 		break;
2188 
2189 	case SCMD_LOG_SENSE_G1:
2190 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2191 			bp_mapin(bp);
2192 		rval = sata_txlt_log_sense(spx);
2193 		break;
2194 
2195 	case SCMD_LOG_SELECT_G1:
2196 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2197 			bp_mapin(bp);
2198 		rval = sata_txlt_log_select(spx);
2199 		break;
2200 
2201 	case SCMD_MODE_SENSE:
2202 	case SCMD_MODE_SENSE_G1:
2203 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2204 			bp_mapin(bp);
2205 		rval = sata_txlt_mode_sense(spx);
2206 		break;
2207 
2208 
2209 	case SCMD_MODE_SELECT:
2210 	case SCMD_MODE_SELECT_G1:
2211 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2212 			bp_mapin(bp);
2213 		rval = sata_txlt_mode_select(spx);
2214 		break;
2215 
2216 	case SCMD_SYNCHRONIZE_CACHE:
2217 	case SCMD_SYNCHRONIZE_CACHE_G1:
2218 		rval = sata_txlt_synchronize_cache(spx);
2219 		break;
2220 
2221 	case SCMD_READ:
2222 	case SCMD_READ_G1:
2223 	case SCMD_READ_G4:
2224 	case SCMD_READ_G5:
2225 		rval = sata_txlt_read(spx);
2226 		break;
2227 	case SCMD_WRITE_BUFFER:
2228 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2229 			bp_mapin(bp);
2230 		rval = sata_txlt_write_buffer(spx);
2231 		break;
2232 
2233 	case SCMD_WRITE:
2234 	case SCMD_WRITE_G1:
2235 	case SCMD_WRITE_G4:
2236 	case SCMD_WRITE_G5:
2237 		rval = sata_txlt_write(spx);
2238 		break;
2239 
2240 	case SCMD_SEEK:
2241 		rval = sata_txlt_nodata_cmd_immediate(spx);
2242 		break;
2243 
2244 		/* Other cases will be filed later */
2245 		/* postponed until phase 2 of the development */
2246 	default:
2247 		rval = sata_txlt_invalid_command(spx);
2248 		break;
2249 	}
2250 
2251 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2252 	    "sata_scsi_start: rval %d\n", rval);
2253 
2254 	return (rval);
2255 }
2256 
2257 /*
2258  * Implementation of scsi tran_abort.
2259  * Abort specific pkt or all packets.
2260  *
2261  * Returns 1 if one or more packets were aborted, returns 0 otherwise
2262  *
2263  * May be called from an interrupt level.
2264  */
2265 static int
2266 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt)
2267 {
2268 	sata_hba_inst_t *sata_hba_inst =
2269 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2270 	sata_device_t	sata_device;
2271 	sata_pkt_t	*sata_pkt;
2272 
2273 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2274 	    "sata_scsi_abort: %s at target: 0x%x\n",
2275 	    scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target);
2276 
2277 	/* Validate address */
2278 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0)
2279 		/* Invalid address */
2280 		return (0);
2281 
2282 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2283 	    sata_device.satadev_addr.cport)));
2284 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2285 		/* invalid address */
2286 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2287 		    sata_device.satadev_addr.cport)));
2288 		return (0);
2289 	}
2290 	if (scsi_pkt == NULL) {
2291 		/*
2292 		 * Abort all packets.
2293 		 * Although we do not have specific packet, we still need
2294 		 * dummy packet structure to pass device address to HBA.
2295 		 * Allocate one, without sleeping. Fail if pkt cannot be
2296 		 * allocated.
2297 		 */
2298 		sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP);
2299 		if (sata_pkt == NULL) {
2300 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2301 			    sata_device.satadev_addr.cport)));
2302 			SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: "
2303 			    "could not allocate sata_pkt"));
2304 			return (0);
2305 		}
2306 		sata_pkt->satapkt_rev = SATA_PKT_REV;
2307 		sata_pkt->satapkt_device = sata_device;
2308 		sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
2309 	} else {
2310 		if (scsi_pkt->pkt_ha_private == NULL) {
2311 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2312 			    sata_device.satadev_addr.cport)));
2313 			return (0); /* Bad scsi pkt */
2314 		}
2315 		/* extract pointer to sata pkt */
2316 		sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)->
2317 		    txlt_sata_pkt;
2318 	}
2319 
2320 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2321 	    sata_device.satadev_addr.cport)));
2322 	/* Send abort request to HBA */
2323 	if ((*SATA_ABORT_FUNC(sata_hba_inst))
2324 	    (SATA_DIP(sata_hba_inst), sata_pkt,
2325 	    scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) ==
2326 	    SATA_SUCCESS) {
2327 		if (scsi_pkt == NULL)
2328 			kmem_free(sata_pkt, sizeof (sata_pkt_t));
2329 		/* Success */
2330 		return (1);
2331 	}
2332 	/* Else, something did not go right */
2333 	if (scsi_pkt == NULL)
2334 		kmem_free(sata_pkt, sizeof (sata_pkt_t));
2335 	/* Failure */
2336 	return (0);
2337 }
2338 
2339 
2340 /*
2341  * Implementation of scsi tran_reset.
2342  * RESET_ALL request is translated into port reset.
2343  * RESET_TARGET requests is translated into a device reset,
2344  * RESET_LUN request is accepted only for LUN 0 and translated into
2345  * device reset.
2346  * The target reset should cause all HBA active and queued packets to
2347  * be terminated and returned with pkt reason SATA_PKT_RESET prior to
2348  * the return. HBA should report reset event for the device.
2349  *
2350  * Returns 1 upon success, 0 upon failure.
2351  */
2352 static int
2353 sata_scsi_reset(struct scsi_address *ap, int level)
2354 {
2355 	sata_hba_inst_t	*sata_hba_inst =
2356 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2357 	sata_device_t	sata_device;
2358 	int		val;
2359 
2360 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2361 	    "sata_scsi_reset: level %d target: 0x%x\n",
2362 	    level, ap->a_target);
2363 
2364 	/* Validate address */
2365 	val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device);
2366 	if (val == -1)
2367 		/* Invalid address */
2368 		return (0);
2369 
2370 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2371 	    sata_device.satadev_addr.cport)));
2372 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2373 		/* invalid address */
2374 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2375 		    sata_device.satadev_addr.cport)));
2376 		return (0);
2377 	}
2378 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2379 	    sata_device.satadev_addr.cport)));
2380 	if (level == RESET_ALL) {
2381 		/* port reset - cport only */
2382 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2383 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2384 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2385 			return (1);
2386 		else
2387 			return (0);
2388 
2389 	} else if (val == 0 &&
2390 	    (level == RESET_TARGET || level == RESET_LUN)) {
2391 		/* reset device (device attached) */
2392 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2393 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2394 			return (1);
2395 		else
2396 			return (0);
2397 	}
2398 	return (0);
2399 }
2400 
2401 
2402 /*
2403  * Implementation of scsi tran_getcap (get transport/device capabilities).
2404  * Supported capabilities for SATA hard disks:
2405  * auto-rqsense		(always supported)
2406  * tagged-qing		(supported if HBA supports it)
2407  * untagged-qing	(could be supported if disk supports it, but because
2408  *			 caching behavior allowing untagged queuing actually
2409  *			 results in reduced performance.  sd tries to throttle
2410  *			 back to only 3 outstanding commands, which may
2411  *			 work for real SCSI disks, but with read ahead
2412  *			 caching, having more than 1 outstanding command
2413  *			 results in cache thrashing.)
2414  * sector_size
2415  * dma_max
2416  * interconnect-type	(INTERCONNECT_SATA)
2417  *
2418  * Supported capabilities for ATAPI CD/DVD devices:
2419  * auto-rqsense		(always supported)
2420  * sector_size
2421  * dma_max
2422  * max-cdb-length
2423  * interconnect-type	(INTERCONNECT_SATA)
2424  *
2425  * Supported capabilities for ATAPI TAPE devices:
2426  * auto-rqsense		(always supported)
2427  * dma_max
2428  * max-cdb-length
2429  *
2430  * Request for other capabilities is rejected as unsupported.
2431  *
2432  * Returns supported capability value, or -1 if capability is unsuppported or
2433  * the address is invalid - no device.
2434  */
2435 
2436 static int
2437 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
2438 {
2439 
2440 	sata_hba_inst_t 	*sata_hba_inst =
2441 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2442 	sata_device_t		sata_device;
2443 	sata_drive_info_t	*sdinfo;
2444 	ddi_dma_attr_t		adj_dma_attr;
2445 	int 			rval;
2446 
2447 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2448 	    "sata_scsi_getcap: target: 0x%x, cap: %s\n",
2449 	    ap->a_target, cap);
2450 
2451 	/*
2452 	 * We want to process the capabilities on per port granularity.
2453 	 * So, we are specifically restricting ourselves to whom != 0
2454 	 * to exclude the controller wide handling.
2455 	 */
2456 	if (cap == NULL || whom == 0)
2457 		return (-1);
2458 
2459 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2460 		/* Invalid address */
2461 		return (-1);
2462 	}
2463 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2464 	    sata_device.satadev_addr.cport)));
2465 	if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) ==
2466 	    NULL) {
2467 		/* invalid address */
2468 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2469 		    sata_device.satadev_addr.cport)));
2470 		return (-1);
2471 	}
2472 
2473 	switch (scsi_hba_lookup_capstr(cap)) {
2474 	case SCSI_CAP_ARQ:
2475 		rval = 1;		/* ARQ supported, turned on */
2476 		break;
2477 
2478 	case SCSI_CAP_SECTOR_SIZE:
2479 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK)
2480 			rval = SATA_DISK_SECTOR_SIZE;	/* fixed size */
2481 		else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD)
2482 			rval = SATA_ATAPI_SECTOR_SIZE;
2483 		else rval = -1;
2484 		break;
2485 
2486 	/*
2487 	 * untagged queuing cause a performance inversion because of
2488 	 * the way sd operates.  Because of this reason we do not
2489 	 * use it when available.
2490 	 */
2491 	case SCSI_CAP_UNTAGGED_QING:
2492 		if (sdinfo->satadrv_features_enabled &
2493 		    SATA_DEV_F_E_UNTAGGED_QING)
2494 			rval = 1;	/* Untagged queuing available */
2495 		else
2496 			rval = -1;	/* Untagged queuing not available */
2497 		break;
2498 
2499 	case SCSI_CAP_TAGGED_QING:
2500 		if ((sdinfo->satadrv_features_enabled &
2501 		    SATA_DEV_F_E_TAGGED_QING) &&
2502 		    (sdinfo->satadrv_max_queue_depth > 1))
2503 			rval = 1;	/* Tagged queuing available */
2504 		else
2505 			rval = -1;	/* Tagged queuing not available */
2506 		break;
2507 
2508 	case SCSI_CAP_DMA_MAX:
2509 		sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst),
2510 		    &adj_dma_attr);
2511 		rval = (int)adj_dma_attr.dma_attr_maxxfer;
2512 		/* We rely on the fact that dma_attr_maxxfer < 0x80000000 */
2513 		break;
2514 
2515 	case SCSI_CAP_INTERCONNECT_TYPE:
2516 		rval = INTERCONNECT_SATA;	/* SATA interconnect type */
2517 		break;
2518 
2519 	case SCSI_CAP_CDB_LEN:
2520 		if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI)
2521 			rval = sdinfo->satadrv_atapi_cdb_len;
2522 		else
2523 			rval = -1;
2524 		break;
2525 
2526 	default:
2527 		rval = -1;
2528 		break;
2529 	}
2530 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2531 	    sata_device.satadev_addr.cport)));
2532 	return (rval);
2533 }
2534 
2535 /*
2536  * Implementation of scsi tran_setcap
2537  *
2538  * Only SCSI_CAP_UNTAGGED_QING and  SCSI_CAP_TAGGED_QING are changeable.
2539  *
2540  */
2541 static int
2542 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
2543 {
2544 	sata_hba_inst_t	*sata_hba_inst =
2545 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2546 	sata_device_t	sata_device;
2547 	sata_drive_info_t	*sdinfo;
2548 	int		rval;
2549 
2550 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2551 	    "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap);
2552 
2553 	/*
2554 	 * We want to process the capabilities on per port granularity.
2555 	 * So, we are specifically restricting ourselves to whom != 0
2556 	 * to exclude the controller wide handling.
2557 	 */
2558 	if (cap == NULL || whom == 0) {
2559 		return (-1);
2560 	}
2561 
2562 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2563 		/* Invalid address */
2564 		return (-1);
2565 	}
2566 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2567 	    sata_device.satadev_addr.cport)));
2568 	if ((sdinfo = sata_get_device_info(sata_hba_inst,
2569 	    &sata_device)) == NULL) {
2570 		/* invalid address */
2571 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2572 		    sata_device.satadev_addr.cport)));
2573 		return (-1);
2574 	}
2575 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2576 	    sata_device.satadev_addr.cport)));
2577 
2578 	switch (scsi_hba_lookup_capstr(cap)) {
2579 	case SCSI_CAP_ARQ:
2580 	case SCSI_CAP_SECTOR_SIZE:
2581 	case SCSI_CAP_DMA_MAX:
2582 	case SCSI_CAP_INTERCONNECT_TYPE:
2583 		rval = 0;
2584 		break;
2585 	case SCSI_CAP_UNTAGGED_QING:
2586 		if (SATA_QDEPTH(sata_hba_inst) > 1) {
2587 			rval = 1;
2588 			if (value == 1) {
2589 				sdinfo->satadrv_features_enabled |=
2590 				    SATA_DEV_F_E_UNTAGGED_QING;
2591 			} else if (value == 0) {
2592 				sdinfo->satadrv_features_enabled &=
2593 				    ~SATA_DEV_F_E_UNTAGGED_QING;
2594 			} else {
2595 				rval = -1;
2596 			}
2597 		} else {
2598 			rval = 0;
2599 		}
2600 		break;
2601 	case SCSI_CAP_TAGGED_QING:
2602 		/* This can TCQ or NCQ */
2603 		if (sata_func_enable & SATA_ENABLE_QUEUING &&
2604 		    ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ &&
2605 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) ||
2606 		    (sata_func_enable & SATA_ENABLE_NCQ &&
2607 		    sdinfo->satadrv_features_support & SATA_DEV_F_NCQ &&
2608 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) &&
2609 		    (sdinfo->satadrv_max_queue_depth > 1)) {
2610 			rval = 1;
2611 			if (value == 1) {
2612 				sdinfo->satadrv_features_enabled |=
2613 				    SATA_DEV_F_E_TAGGED_QING;
2614 			} else if (value == 0) {
2615 				sdinfo->satadrv_features_enabled &=
2616 				    ~SATA_DEV_F_E_TAGGED_QING;
2617 			} else {
2618 				rval = -1;
2619 			}
2620 		} else {
2621 			rval = 0;
2622 		}
2623 		break;
2624 	default:
2625 		rval = -1;
2626 		break;
2627 	}
2628 	return (rval);
2629 }
2630 
2631 /*
2632  * Implementations of scsi tran_destroy_pkt.
2633  * Free resources allocated by sata_scsi_init_pkt()
2634  */
2635 static void
2636 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2637 {
2638 	sata_pkt_txlate_t *spx;
2639 
2640 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2641 
2642 	sata_common_free_dma_rsrcs(spx);
2643 
2644 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2645 	sata_pkt_free(spx);
2646 
2647 	scsi_hba_pkt_free(ap, pkt);
2648 }
2649 
2650 /*
2651  * Implementation of scsi tran_dmafree.
2652  * Free DMA resources allocated by sata_scsi_init_pkt()
2653  */
2654 
2655 static void
2656 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
2657 {
2658 #ifndef __lock_lint
2659 	_NOTE(ARGUNUSED(ap))
2660 #endif
2661 	sata_pkt_txlate_t *spx;
2662 
2663 	ASSERT(pkt != NULL);
2664 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2665 
2666 	sata_common_free_dma_rsrcs(spx);
2667 }
2668 
2669 /*
2670  * Implementation of scsi tran_sync_pkt.
2671  *
2672  * The assumption below is that pkt is unique - there is no need to check ap
2673  *
2674  * Synchronize DMA buffer and, if the intermediate buffer is used, copy data
2675  * into/from the real buffer.
2676  */
2677 static void
2678 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2679 {
2680 #ifndef __lock_lint
2681 	_NOTE(ARGUNUSED(ap))
2682 #endif
2683 	int rval;
2684 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2685 	struct buf *bp;
2686 	int direction;
2687 
2688 	ASSERT(spx != NULL);
2689 	if (spx->txlt_buf_dma_handle != NULL) {
2690 		direction = spx->txlt_sata_pkt->
2691 		    satapkt_cmd.satacmd_flags.sata_data_direction;
2692 		if (spx->txlt_sata_pkt != NULL &&
2693 		    direction != SATA_DIR_NODATA_XFER) {
2694 			if (spx->txlt_tmp_buf != NULL) {
2695 				/* Intermediate DMA buffer used */
2696 				bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2697 
2698 				if (direction & SATA_DIR_WRITE) {
2699 					bcopy(bp->b_un.b_addr,
2700 					    spx->txlt_tmp_buf, bp->b_bcount);
2701 				}
2702 			}
2703 			/* Sync the buffer for device or for CPU */
2704 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle,   0, 0,
2705 			    (direction & SATA_DIR_WRITE) ?
2706 			    DDI_DMA_SYNC_FORDEV :  DDI_DMA_SYNC_FORCPU);
2707 			ASSERT(rval == DDI_SUCCESS);
2708 			if (spx->txlt_tmp_buf != NULL &&
2709 			    !(direction & SATA_DIR_WRITE)) {
2710 				/* Intermediate DMA buffer used for read */
2711 				bcopy(spx->txlt_tmp_buf,
2712 				    bp->b_un.b_addr, bp->b_bcount);
2713 			}
2714 
2715 		}
2716 	}
2717 }
2718 
2719 
2720 
2721 /* *******************  SATA - SCSI Translation functions **************** */
2722 /*
2723  * SCSI to SATA pkt and command translation and SATA to SCSI status/error
2724  * translation.
2725  */
2726 
2727 /*
2728  * Checks if a device exists and can be access and translates common
2729  * scsi_pkt data to sata_pkt data.
2730  *
2731  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and
2732  * sata_pkt was set-up.
2733  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not
2734  * exist and pkt_comp callback was scheduled.
2735  * Returns other TRAN_XXXXX values when error occured and command should be
2736  * rejected with the returned TRAN_XXXXX value.
2737  *
2738  * This function should be called with port mutex held.
2739  */
2740 static int
2741 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason)
2742 {
2743 	sata_drive_info_t *sdinfo;
2744 	sata_device_t sata_device;
2745 	const struct sata_cmd_flags sata_initial_cmd_flags = {
2746 		SATA_DIR_NODATA_XFER,
2747 		/* all other values to 0/FALSE */
2748 	};
2749 	/*
2750 	 * Pkt_reason has to be set if the pkt_comp callback is invoked,
2751 	 * and that implies TRAN_ACCEPT return value. Any other returned value
2752 	 * indicates that the scsi packet was not accepted (the reason will not
2753 	 * be checked by the scsi target driver).
2754 	 * To make debugging easier, we set pkt_reason to know value here.
2755 	 * It may be changed later when different completion reason is
2756 	 * determined.
2757 	 */
2758 	spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
2759 	*reason = CMD_TRAN_ERR;
2760 
2761 	/* Validate address */
2762 	switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst,
2763 	    &spx->txlt_scsi_pkt->pkt_address, &sata_device)) {
2764 
2765 	case -1:
2766 		/* Invalid address or invalid device type */
2767 		return (TRAN_BADPKT);
2768 	case 1:
2769 		/* valid address but no device - it has disappeared ? */
2770 		spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE;
2771 		*reason = CMD_DEV_GONE;
2772 		/*
2773 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2774 		 * only in callback function (for normal requests) and
2775 		 * in the dump code path.
2776 		 * So, if the callback is available, we need to do
2777 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2778 		 */
2779 		if (spx->txlt_scsi_pkt->pkt_comp != NULL) {
2780 			/* scsi callback required */
2781 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2782 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2783 			    (void *)spx->txlt_scsi_pkt,
2784 			    TQ_SLEEP) == NULL)
2785 				/* Scheduling the callback failed */
2786 				return (TRAN_BUSY);
2787 
2788 			return (TRAN_ACCEPT);
2789 		}
2790 		return (TRAN_FATAL_ERROR);
2791 	default:
2792 		/* all OK; pkt reason will be overwritten later */
2793 		break;
2794 	}
2795 	/*
2796 	 * If in an interrupt context, reject packet if it is to be
2797 	 * executed in polling mode
2798 	 */
2799 	if (servicing_interrupt() &&
2800 	    (spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
2801 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
2802 		    "sata_scsi_start: rejecting synchronous command because "
2803 		    "of interrupt context\n", NULL);
2804 		return (TRAN_BUSY);
2805 	}
2806 
2807 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2808 	    &spx->txlt_sata_pkt->satapkt_device);
2809 
2810 	/*
2811 	 * If device is in reset condition, reject the packet with
2812 	 * TRAN_BUSY, unless:
2813 	 * 1. system is panicking (dumping)
2814 	 * In such case only one thread is running and there is no way to
2815 	 * process reset.
2816 	 * 2. cfgadm operation is is progress (internal APCTL lock is set)
2817 	 * Some cfgadm operations involve drive commands, so reset condition
2818 	 * needs to be ignored for IOCTL operations.
2819 	 */
2820 	if ((sdinfo->satadrv_event_flags &
2821 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
2822 
2823 		if (!ddi_in_panic() &&
2824 		    ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst,
2825 		    sata_device.satadev_addr.cport) &
2826 		    SATA_APCTL_LOCK_PORT_BUSY) == 0)) {
2827 			spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
2828 			*reason = CMD_INCOMPLETE;
2829 			SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
2830 			    "sata_scsi_start: rejecting command because "
2831 			    "of device reset state\n", NULL);
2832 			return (TRAN_BUSY);
2833 		}
2834 	}
2835 
2836 	/*
2837 	 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by
2838 	 * sata_scsi_pkt_init() because pkt init had to work also with
2839 	 * non-existing devices.
2840 	 * Now we know that the packet was set-up for a real device, so its
2841 	 * type is known.
2842 	 */
2843 	spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type;
2844 
2845 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags;
2846 	if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst,
2847 	    sata_device.satadev_addr.cport)->cport_event_flags &
2848 	    SATA_APCTL_LOCK_PORT_BUSY) != 0) {
2849 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
2850 		    sata_ignore_dev_reset = B_TRUE;
2851 	}
2852 	/*
2853 	 * At this point the generic translation routine determined that the
2854 	 * scsi packet should be accepted. Packet completion reason may be
2855 	 * changed later when a different completion reason is determined.
2856 	 */
2857 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
2858 	*reason = CMD_CMPLT;
2859 
2860 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
2861 		/* Synchronous execution */
2862 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH |
2863 		    SATA_OPMODE_POLLING;
2864 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
2865 		    sata_ignore_dev_reset = ddi_in_panic();
2866 	} else {
2867 		/* Asynchronous execution */
2868 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH |
2869 		    SATA_OPMODE_INTERRUPTS;
2870 	}
2871 	/* Convert queuing information */
2872 	if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG)
2873 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag =
2874 		    B_TRUE;
2875 	else if (spx->txlt_scsi_pkt->pkt_flags &
2876 	    (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD))
2877 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag =
2878 		    B_TRUE;
2879 
2880 	/* Always limit pkt time */
2881 	if (spx->txlt_scsi_pkt->pkt_time == 0)
2882 		spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time;
2883 	else
2884 		/* Pass on scsi_pkt time */
2885 		spx->txlt_sata_pkt->satapkt_time =
2886 		    spx->txlt_scsi_pkt->pkt_time;
2887 
2888 	return (TRAN_ACCEPT);
2889 }
2890 
2891 
2892 /*
2893  * Translate ATA Identify Device data to SCSI Inquiry data.
2894  * This function may be called only for ATA devices.
2895  * This function should not be called for ATAPI devices - they
2896  * respond directly to SCSI Inquiry command.
2897  *
2898  * SATA Identify Device data has to be valid in sata_rive_info.
2899  * Buffer has to accomodate the inquiry length (36 bytes).
2900  *
2901  * This function should be called with a port mutex held.
2902  */
2903 static	void
2904 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst,
2905     sata_drive_info_t *sdinfo, uint8_t *buf)
2906 {
2907 
2908 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
2909 	struct sata_id *sid = &sdinfo->satadrv_id;
2910 
2911 	/* Start with a nice clean slate */
2912 	bzero((void *)inq, sizeof (struct scsi_inquiry));
2913 
2914 	/*
2915 	 * Rely on the dev_type for setting paripheral qualifier.
2916 	 * Assume that  DTYPE_RODIRECT applies to CD/DVD R/W devices.
2917 	 * It could be that DTYPE_OPTICAL could also qualify in the future.
2918 	 * ATAPI Inquiry may provide more data to the target driver.
2919 	 */
2920 	inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
2921 	    DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */
2922 
2923 	inq->inq_rmb = sid->ai_config & SATA_REM_MEDIA ? 1 : 0;
2924 	inq->inq_qual = 0;	/* Device type qualifier (obsolete in SCSI3? */
2925 	inq->inq_iso = 0;	/* ISO version */
2926 	inq->inq_ecma = 0;	/* ECMA version */
2927 	inq->inq_ansi = 3;	/* ANSI version - SCSI 3 */
2928 	inq->inq_aenc = 0;	/* Async event notification cap. */
2929 	inq->inq_trmiop = 0;	/* Supports TERMINATE I/O PROC msg - NO */
2930 	inq->inq_normaca = 0;	/* setting NACA bit supported - NO */
2931 	inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */
2932 	inq->inq_len = 31;	/* Additional length */
2933 	inq->inq_dualp = 0;	/* dual port device - NO */
2934 	inq->inq_reladdr = 0;	/* Supports relative addressing - NO */
2935 	inq->inq_sync = 0;	/* Supports synchronous data xfers - NO */
2936 	inq->inq_linked = 0;	/* Supports linked commands - NO */
2937 				/*
2938 				 * Queuing support - controller has to
2939 				 * support some sort of command queuing.
2940 				 */
2941 	if (SATA_QDEPTH(sata_hba_inst) > 1)
2942 		inq->inq_cmdque = 1; /* Supports command queueing - YES */
2943 	else
2944 		inq->inq_cmdque = 0; /* Supports command queueing - NO */
2945 	inq->inq_sftre = 0;	/* Supports Soft Reset option - NO ??? */
2946 	inq->inq_wbus32 = 0;	/* Supports 32 bit wide data xfers - NO */
2947 	inq->inq_wbus16 = 0;	/* Supports 16 bit wide data xfers - NO */
2948 
2949 #ifdef	_LITTLE_ENDIAN
2950 	/* Swap text fields to match SCSI format */
2951 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
2952 	swab(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
2953 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
2954 		swab(sid->ai_fw, inq->inq_revision, 4);	/* Revision level */
2955 	else
2956 		swab(&sid->ai_fw[4], inq->inq_revision, 4);	/* Rev. level */
2957 #else	/* _LITTLE_ENDIAN */
2958 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
2959 	bcopy(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
2960 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
2961 		bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
2962 	else
2963 		bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
2964 #endif	/* _LITTLE_ENDIAN */
2965 }
2966 
2967 
2968 /*
2969  * Scsi response set up for invalid command (command not supported)
2970  *
2971  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
2972  */
2973 static int
2974 sata_txlt_invalid_command(sata_pkt_txlate_t *spx)
2975 {
2976 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
2977 	struct scsi_extended_sense *sense;
2978 
2979 	scsipkt->pkt_reason = CMD_CMPLT;
2980 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
2981 	    STATE_SENT_CMD | STATE_GOT_STATUS;
2982 
2983 	*scsipkt->pkt_scbp = STATUS_CHECK;
2984 
2985 	sense = sata_arq_sense(spx);
2986 	sense->es_key = KEY_ILLEGAL_REQUEST;
2987 	sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE;
2988 
2989 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
2990 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
2991 
2992 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
2993 	    scsipkt->pkt_comp != NULL)
2994 		/* scsi callback required */
2995 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2996 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2997 		    (void *)spx->txlt_scsi_pkt,
2998 		    TQ_SLEEP) == NULL)
2999 			/* Scheduling the callback failed */
3000 			return (TRAN_BUSY);
3001 	return (TRAN_ACCEPT);
3002 }
3003 
3004 /*
3005  * Scsi response setup for
3006  * emulated non-data command that requires no action/return data
3007  *
3008  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3009  */
3010 static 	int
3011 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx)
3012 {
3013 	int rval;
3014 	int reason;
3015 
3016 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3017 
3018 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3019 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3020 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3021 		return (rval);
3022 	}
3023 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3024 
3025 	spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3026 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3027 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3028 	*(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD;
3029 
3030 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3031 	    "Scsi_pkt completion reason %x\n",
3032 	    spx->txlt_scsi_pkt->pkt_reason);
3033 
3034 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 &&
3035 	    spx->txlt_scsi_pkt->pkt_comp != NULL)
3036 		/* scsi callback required */
3037 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3038 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3039 		    (void *)spx->txlt_scsi_pkt,
3040 		    TQ_SLEEP) == NULL)
3041 			/* Scheduling the callback failed */
3042 			return (TRAN_BUSY);
3043 	return (TRAN_ACCEPT);
3044 }
3045 
3046 
3047 /*
3048  * SATA translate command: Inquiry / Identify Device
3049  * Use cached Identify Device data for now, rather than issuing actual
3050  * Device Identify cmd request. If device is detached and re-attached,
3051  * asynchromous event processing should fetch and refresh Identify Device
3052  * data.
3053  * Two VPD pages are supported now:
3054  * Vital Product Data page
3055  * Unit Serial Number page
3056  *
3057  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3058  */
3059 
3060 #define	EVPD			1	/* Extended Vital Product Data flag */
3061 #define	CMDDT			2	/* Command Support Data - Obsolete */
3062 #define	INQUIRY_SUP_VPD_PAGE	0	/* Supported VDP Pages Page COde */
3063 #define	INQUIRY_USN_PAGE	0x80	/* Unit Serial Number Page Code */
3064 #define	INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */
3065 
3066 static int
3067 sata_txlt_inquiry(sata_pkt_txlate_t *spx)
3068 {
3069 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3070 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3071 	sata_drive_info_t *sdinfo;
3072 	struct scsi_extended_sense *sense;
3073 	int count;
3074 	uint8_t *p;
3075 	int i, j;
3076 	uint8_t page_buf[0xff]; /* Max length */
3077 	int rval, reason;
3078 
3079 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3080 
3081 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3082 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3083 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3084 		return (rval);
3085 	}
3086 
3087 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3088 	    &spx->txlt_sata_pkt->satapkt_device);
3089 
3090 	ASSERT(sdinfo != NULL);
3091 
3092 	scsipkt->pkt_reason = CMD_CMPLT;
3093 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3094 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3095 
3096 	/* Reject not supported request */
3097 	if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */
3098 		*scsipkt->pkt_scbp = STATUS_CHECK;
3099 		sense = sata_arq_sense(spx);
3100 		sense->es_key = KEY_ILLEGAL_REQUEST;
3101 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3102 		goto done;
3103 	}
3104 
3105 	/* Valid Inquiry request */
3106 	*scsipkt->pkt_scbp = STATUS_GOOD;
3107 
3108 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3109 
3110 		/*
3111 		 * Because it is fully emulated command storing data
3112 		 * programatically in the specified buffer, release
3113 		 * preallocated DMA resources before storing data in the buffer,
3114 		 * so no unwanted DMA sync would take place.
3115 		 */
3116 		sata_scsi_dmafree(NULL, scsipkt);
3117 
3118 		if (!(scsipkt->pkt_cdbp[1] & EVPD)) {
3119 			/* Standard Inquiry Data request */
3120 			struct scsi_inquiry inq;
3121 			unsigned int bufsize;
3122 
3123 			sata_identdev_to_inquiry(spx->txlt_sata_hba_inst,
3124 			    sdinfo, (uint8_t *)&inq);
3125 			/* Copy no more than requested */
3126 			count = MIN(bp->b_bcount,
3127 			    sizeof (struct scsi_inquiry));
3128 			bufsize = scsipkt->pkt_cdbp[4];
3129 			bufsize |= scsipkt->pkt_cdbp[3] << 8;
3130 			count = MIN(count, bufsize);
3131 			bcopy(&inq, bp->b_un.b_addr, count);
3132 
3133 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
3134 			scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3135 			    bufsize - count : 0;
3136 		} else {
3137 			/*
3138 			 * peripheral_qualifier = 0;
3139 			 *
3140 			 * We are dealing only with HD and will be
3141 			 * dealing with CD/DVD devices soon
3142 			 */
3143 			uint8_t peripheral_device_type =
3144 			    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3145 			    DTYPE_DIRECT : DTYPE_RODIRECT;
3146 
3147 			switch ((uint_t)scsipkt->pkt_cdbp[2]) {
3148 			case INQUIRY_SUP_VPD_PAGE:
3149 				/*
3150 				 * Request for suported Vital Product Data
3151 				 * pages - assuming only 2 page codes
3152 				 * supported
3153 				 */
3154 				page_buf[0] = peripheral_device_type;
3155 				page_buf[1] = INQUIRY_SUP_VPD_PAGE;
3156 				page_buf[2] = 0;
3157 				page_buf[3] = 2; /* page length */
3158 				page_buf[4] = INQUIRY_SUP_VPD_PAGE;
3159 				page_buf[5] = INQUIRY_USN_PAGE;
3160 				/* Copy no more than requested */
3161 				count = MIN(bp->b_bcount, 6);
3162 				bcopy(page_buf, bp->b_un.b_addr, count);
3163 				break;
3164 			case INQUIRY_USN_PAGE:
3165 				/*
3166 				 * Request for Unit Serial Number page
3167 				 */
3168 				page_buf[0] = peripheral_device_type;
3169 				page_buf[1] = INQUIRY_USN_PAGE;
3170 				page_buf[2] = 0;
3171 				page_buf[3] = 20; /* remaining page length */
3172 				p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser);
3173 #ifdef	_LITTLE_ENDIAN
3174 				swab(p, &page_buf[4], 20);
3175 #else
3176 				bcopy(p, &page_buf[4], 20);
3177 #endif
3178 				for (i = 0; i < 20; i++) {
3179 					if (page_buf[4 + i] == '\0' ||
3180 					    page_buf[4 + i] == '\040') {
3181 						break;
3182 					}
3183 				}
3184 				/*
3185 				 * 'i' contains string length.
3186 				 *
3187 				 * Least significant character of the serial
3188 				 * number shall appear as the last byte,
3189 				 * according to SBC-3 spec.
3190 				 */
3191 				p = &page_buf[20 + 4 - 1];
3192 				for (j = i; j > 0; j--, p--) {
3193 					*p = *(p - 20 + i);
3194 				}
3195 				p = &page_buf[4];
3196 				for (j = 20 - i; j > 0; j--) {
3197 					*p++ = '\040';
3198 				}
3199 				count = MIN(bp->b_bcount, 24);
3200 				bcopy(page_buf, bp->b_un.b_addr, count);
3201 				break;
3202 
3203 			case INQUIRY_DEV_IDENTIFICATION_PAGE:
3204 				/*
3205 				 * We may want to implement this page, when
3206 				 * identifiers are common for SATA devices
3207 				 * But not now.
3208 				 */
3209 				/*FALLTHROUGH*/
3210 
3211 			default:
3212 				/* Request for unsupported VPD page */
3213 				*scsipkt->pkt_scbp = STATUS_CHECK;
3214 				sense = sata_arq_sense(spx);
3215 				sense->es_key = KEY_ILLEGAL_REQUEST;
3216 				sense->es_add_code =
3217 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3218 				goto done;
3219 			}
3220 		}
3221 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3222 		scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3223 		    scsipkt->pkt_cdbp[4] - count : 0;
3224 	}
3225 done:
3226 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3227 
3228 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3229 	    "Scsi_pkt completion reason %x\n",
3230 	    scsipkt->pkt_reason);
3231 
3232 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3233 	    scsipkt->pkt_comp != NULL) {
3234 		/* scsi callback required */
3235 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3236 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3237 		    TQ_SLEEP) == NULL)
3238 			/* Scheduling the callback failed */
3239 			return (TRAN_BUSY);
3240 	}
3241 	return (TRAN_ACCEPT);
3242 }
3243 
3244 /*
3245  * SATA translate command: Request Sense.
3246  * Emulated command (ATA version for SATA hard disks)
3247  * Always NO SENSE, because any sense data should be reported by ARQ sense.
3248  *
3249  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3250  *
3251  * Note: There is a mismatch between already implemented Informational
3252  * Exception Mode Select page 0x1C and this function.
3253  * When MRIE bit is set in page 0x1C, Request Sense is supposed to return
3254  * NO SENSE and set additional sense code to the exception code - this is not
3255  * implemented here.
3256  */
3257 static int
3258 sata_txlt_request_sense(sata_pkt_txlate_t *spx)
3259 {
3260 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3261 	struct scsi_extended_sense sense;
3262 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3263 	int rval, reason;
3264 
3265 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3266 
3267 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3268 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3269 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3270 		return (rval);
3271 	}
3272 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3273 
3274 
3275 	scsipkt->pkt_reason = CMD_CMPLT;
3276 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3277 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3278 	*scsipkt->pkt_scbp = STATUS_GOOD;
3279 
3280 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3281 		/*
3282 		 * Because it is fully emulated command storing data
3283 		 * programatically in the specified buffer, release
3284 		 * preallocated DMA resources before storing data in the buffer,
3285 		 * so no unwanted DMA sync would take place.
3286 		 */
3287 		int count = MIN(bp->b_bcount,
3288 		    sizeof (struct scsi_extended_sense));
3289 		sata_scsi_dmafree(NULL, scsipkt);
3290 		bzero(&sense, sizeof (struct scsi_extended_sense));
3291 		sense.es_valid = 0;	/* Valid LBA */
3292 		sense.es_class = 7;	/* Response code 0x70 - current err */
3293 		sense.es_key = KEY_NO_SENSE;
3294 		sense.es_add_len = 6;	/* Additional length */
3295 		/* Copy no more than requested */
3296 		bcopy(&sense, bp->b_un.b_addr, count);
3297 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3298 		scsipkt->pkt_resid = 0;
3299 	}
3300 
3301 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3302 	    "Scsi_pkt completion reason %x\n",
3303 	    scsipkt->pkt_reason);
3304 
3305 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3306 	    scsipkt->pkt_comp != NULL)
3307 		/* scsi callback required */
3308 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3309 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3310 		    TQ_SLEEP) == NULL)
3311 			/* Scheduling the callback failed */
3312 			return (TRAN_BUSY);
3313 	return (TRAN_ACCEPT);
3314 }
3315 
3316 /*
3317  * SATA translate command: Test Unit Ready
3318  * At the moment this is an emulated command (ATA version for SATA hard disks).
3319  * May be translated into Check Power Mode command in the future
3320  *
3321  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3322  */
3323 static int
3324 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx)
3325 {
3326 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3327 	struct scsi_extended_sense *sense;
3328 	int power_state;
3329 	int rval, reason;
3330 
3331 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3332 
3333 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3334 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3335 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3336 		return (rval);
3337 	}
3338 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3339 
3340 	/* At this moment, emulate it rather than execute anything */
3341 	power_state = SATA_PWRMODE_ACTIVE;
3342 
3343 	scsipkt->pkt_reason = CMD_CMPLT;
3344 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3345 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3346 
3347 	switch (power_state) {
3348 	case SATA_PWRMODE_ACTIVE:
3349 	case SATA_PWRMODE_IDLE:
3350 		*scsipkt->pkt_scbp = STATUS_GOOD;
3351 		break;
3352 	default:
3353 		/* PWR mode standby */
3354 		*scsipkt->pkt_scbp = STATUS_CHECK;
3355 		sense = sata_arq_sense(spx);
3356 		sense->es_key = KEY_NOT_READY;
3357 		sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY;
3358 		break;
3359 	}
3360 
3361 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3362 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3363 
3364 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3365 	    scsipkt->pkt_comp != NULL)
3366 		/* scsi callback required */
3367 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3368 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3369 		    TQ_SLEEP) == NULL)
3370 			/* Scheduling the callback failed */
3371 			return (TRAN_BUSY);
3372 
3373 	return (TRAN_ACCEPT);
3374 }
3375 
3376 
3377 /*
3378  * SATA translate command: Start Stop Unit
3379  * Translation depends on a command:
3380  *	Start Unit translated into Idle Immediate
3381  *	Stop Unit translated into Standby Immediate
3382  *	Unload Media / NOT SUPPORTED YET
3383  *	Load Media / NOT SUPPROTED YET
3384  * Power condition bits are ignored, so is Immediate bit
3385  * Requesting synchronous execution.
3386  *
3387  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
3388  * appropriate values in scsi_pkt fields.
3389  */
3390 static int
3391 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx)
3392 {
3393 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3394 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3395 	struct scsi_extended_sense *sense;
3396 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3397 	int cport = SATA_TXLT_CPORT(spx);
3398 	int rval, reason;
3399 	int synch;
3400 
3401 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3402 	    "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1);
3403 
3404 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3405 
3406 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3407 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3408 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3409 		return (rval);
3410 	}
3411 
3412 	if (scsipkt->pkt_cdbp[4] & 2) {
3413 		/* Load/Unload Media - invalid request */
3414 		*scsipkt->pkt_scbp = STATUS_CHECK;
3415 		sense = sata_arq_sense(spx);
3416 		sense->es_key = KEY_ILLEGAL_REQUEST;
3417 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3418 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3419 
3420 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3421 		    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3422 
3423 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3424 		    scsipkt->pkt_comp != NULL)
3425 			/* scsi callback required */
3426 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3427 			    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3428 			    TQ_SLEEP) == NULL)
3429 				/* Scheduling the callback failed */
3430 				return (TRAN_BUSY);
3431 
3432 		return (TRAN_ACCEPT);
3433 	}
3434 	scmd->satacmd_addr_type = 0;
3435 	scmd->satacmd_sec_count_lsb = 0;
3436 	scmd->satacmd_lba_low_lsb = 0;
3437 	scmd->satacmd_lba_mid_lsb = 0;
3438 	scmd->satacmd_lba_high_lsb = 0;
3439 	scmd->satacmd_features_reg = 0;
3440 	scmd->satacmd_device_reg = 0;
3441 	scmd->satacmd_status_reg = 0;
3442 	if (scsipkt->pkt_cdbp[4] & 1) {
3443 		/* Start Unit */
3444 		scmd->satacmd_cmd_reg = SATAC_IDLE_IM;
3445 	} else {
3446 		/* Stop Unit */
3447 		scmd->satacmd_cmd_reg = SATAC_STANDBY_IM;
3448 	}
3449 
3450 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
3451 		/* Need to set-up a callback function */
3452 		spx->txlt_sata_pkt->satapkt_comp =
3453 		    sata_txlt_nodata_cmd_completion;
3454 		synch = FALSE;
3455 	} else {
3456 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
3457 		synch = TRUE;
3458 	}
3459 
3460 	/* Transfer command to HBA */
3461 	if (sata_hba_start(spx, &rval) != 0) {
3462 		/* Pkt not accepted for execution */
3463 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3464 		return (rval);
3465 	}
3466 
3467 	/*
3468 	 * If execution is non-synchronous,
3469 	 * a callback function will handle potential errors, translate
3470 	 * the response and will do a callback to a target driver.
3471 	 * If it was synchronous, check execution status using the same
3472 	 * framework callback.
3473 	 */
3474 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3475 	if (synch) {
3476 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3477 		    "synchronous execution status %x\n",
3478 		    spx->txlt_sata_pkt->satapkt_reason);
3479 
3480 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
3481 	}
3482 	return (TRAN_ACCEPT);
3483 
3484 }
3485 
3486 
3487 /*
3488  * SATA translate command:  Read Capacity.
3489  * Emulated command for SATA disks.
3490  * Capacity is retrieved from cached Idenifty Device data.
3491  * Identify Device data shows effective disk capacity, not the native
3492  * capacity, which may be limitted by Set Max Address command.
3493  * This is ATA version for SATA hard disks.
3494  *
3495  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3496  */
3497 static int
3498 sata_txlt_read_capacity(sata_pkt_txlate_t *spx)
3499 {
3500 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3501 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3502 	sata_drive_info_t *sdinfo;
3503 	uint64_t val;
3504 	uchar_t *rbuf;
3505 	int rval, reason;
3506 
3507 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3508 	    "sata_txlt_read_capacity: ", NULL);
3509 
3510 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3511 
3512 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3513 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3514 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3515 		return (rval);
3516 	}
3517 
3518 	scsipkt->pkt_reason = CMD_CMPLT;
3519 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3520 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3521 	*scsipkt->pkt_scbp = STATUS_GOOD;
3522 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3523 		/*
3524 		 * Because it is fully emulated command storing data
3525 		 * programatically in the specified buffer, release
3526 		 * preallocated DMA resources before storing data in the buffer,
3527 		 * so no unwanted DMA sync would take place.
3528 		 */
3529 		sata_scsi_dmafree(NULL, scsipkt);
3530 
3531 		sdinfo = sata_get_device_info(
3532 		    spx->txlt_sata_hba_inst,
3533 		    &spx->txlt_sata_pkt->satapkt_device);
3534 		/* Last logical block address */
3535 		val = sdinfo->satadrv_capacity - 1;
3536 		rbuf = (uchar_t *)bp->b_un.b_addr;
3537 		/* Need to swap endians to match scsi format */
3538 		rbuf[0] = (val >> 24) & 0xff;
3539 		rbuf[1] = (val >> 16) & 0xff;
3540 		rbuf[2] = (val >> 8) & 0xff;
3541 		rbuf[3] = val & 0xff;
3542 		/* block size - always 512 bytes, for now */
3543 		rbuf[4] = 0;
3544 		rbuf[5] = 0;
3545 		rbuf[6] = 0x02;
3546 		rbuf[7] = 0;
3547 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3548 		scsipkt->pkt_resid = 0;
3549 
3550 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n",
3551 		    sdinfo->satadrv_capacity -1);
3552 	}
3553 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3554 	/*
3555 	 * If a callback was requested, do it now.
3556 	 */
3557 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3558 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3559 
3560 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3561 	    scsipkt->pkt_comp != NULL)
3562 		/* scsi callback required */
3563 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3564 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3565 		    TQ_SLEEP) == NULL)
3566 			/* Scheduling the callback failed */
3567 			return (TRAN_BUSY);
3568 
3569 	return (TRAN_ACCEPT);
3570 }
3571 
3572 /*
3573  * SATA translate command: Mode Sense.
3574  * Translated into appropriate SATA command or emulated.
3575  * Saved Values Page Control (03) are not supported.
3576  *
3577  * NOTE: only caching mode sense page is currently implemented.
3578  *
3579  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3580  */
3581 
3582 static int
3583 sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
3584 {
3585 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
3586 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3587 	sata_drive_info_t *sdinfo;
3588 	sata_id_t *sata_id;
3589 	struct scsi_extended_sense *sense;
3590 	int 		len, bdlen, count, alc_len;
3591 	int		pc;	/* Page Control code */
3592 	uint8_t		*buf;	/* mode sense buffer */
3593 	int		rval, reason;
3594 
3595 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3596 	    "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
3597 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
3598 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
3599 
3600 	buf = kmem_zalloc(1024, KM_SLEEP);
3601 
3602 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3603 
3604 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3605 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3606 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3607 		kmem_free(buf, 1024);
3608 		return (rval);
3609 	}
3610 
3611 	scsipkt->pkt_reason = CMD_CMPLT;
3612 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3613 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3614 
3615 	pc = scsipkt->pkt_cdbp[2] >> 6;
3616 
3617 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3618 		/*
3619 		 * Because it is fully emulated command storing data
3620 		 * programatically in the specified buffer, release
3621 		 * preallocated DMA resources before storing data in the buffer,
3622 		 * so no unwanted DMA sync would take place.
3623 		 */
3624 		sata_scsi_dmafree(NULL, scsipkt);
3625 
3626 		len = 0;
3627 		bdlen = 0;
3628 		if (!(scsipkt->pkt_cdbp[1] & 8)) {
3629 			if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
3630 			    (scsipkt->pkt_cdbp[0] & 0x10))
3631 				bdlen = 16;
3632 			else
3633 				bdlen = 8;
3634 		}
3635 		/* Build mode parameter header */
3636 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3637 			/* 4-byte mode parameter header */
3638 			buf[len++] = 0;   	/* mode data length */
3639 			buf[len++] = 0;		/* medium type */
3640 			buf[len++] = 0;		/* dev-specific param */
3641 			buf[len++] = bdlen;	/* Block Descriptor length */
3642 		} else {
3643 			/* 8-byte mode parameter header */
3644 			buf[len++] = 0;		/* mode data length */
3645 			buf[len++] = 0;
3646 			buf[len++] = 0;		/* medium type */
3647 			buf[len++] = 0;		/* dev-specific param */
3648 			if (bdlen == 16)
3649 				buf[len++] = 1;	/* long lba descriptor */
3650 			else
3651 				buf[len++] = 0;
3652 			buf[len++] = 0;
3653 			buf[len++] = 0;		/* Block Descriptor length */
3654 			buf[len++] = bdlen;
3655 		}
3656 
3657 		sdinfo = sata_get_device_info(
3658 		    spx->txlt_sata_hba_inst,
3659 		    &spx->txlt_sata_pkt->satapkt_device);
3660 
3661 		/* Build block descriptor only if not disabled (DBD) */
3662 		if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
3663 			/* Block descriptor - direct-access device format */
3664 			if (bdlen == 8) {
3665 				/* build regular block descriptor */
3666 				buf[len++] =
3667 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
3668 				buf[len++] =
3669 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
3670 				buf[len++] =
3671 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
3672 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
3673 				buf[len++] = 0; /* density code */
3674 				buf[len++] = 0;
3675 				if (sdinfo->satadrv_type ==
3676 				    SATA_DTYPE_ATADISK)
3677 					buf[len++] = 2;
3678 				else
3679 					/* ATAPI */
3680 					buf[len++] = 8;
3681 				buf[len++] = 0;
3682 			} else if (bdlen == 16) {
3683 				/* Long LBA Accepted */
3684 				/* build long lba block descriptor */
3685 #ifndef __lock_lint
3686 				buf[len++] =
3687 				    (sdinfo->satadrv_capacity >> 56) & 0xff;
3688 				buf[len++] =
3689 				    (sdinfo->satadrv_capacity >> 48) & 0xff;
3690 				buf[len++] =
3691 				    (sdinfo->satadrv_capacity >> 40) & 0xff;
3692 				buf[len++] =
3693 				    (sdinfo->satadrv_capacity >> 32) & 0xff;
3694 #endif
3695 				buf[len++] =
3696 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
3697 				buf[len++] =
3698 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
3699 				buf[len++] =
3700 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
3701 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
3702 				buf[len++] = 0;
3703 				buf[len++] = 0; /* density code */
3704 				buf[len++] = 0;
3705 				buf[len++] = 0;
3706 				if (sdinfo->satadrv_type ==
3707 				    SATA_DTYPE_ATADISK)
3708 					buf[len++] = 2;
3709 				else
3710 					/* ATAPI */
3711 					buf[len++] = 8;
3712 				buf[len++] = 0;
3713 			}
3714 		}
3715 
3716 		sata_id = &sdinfo->satadrv_id;
3717 
3718 		/*
3719 		 * Add requested pages.
3720 		 * Page 3 and 4 are obsolete and we are not supporting them.
3721 		 * We deal now with:
3722 		 * caching (read/write cache control).
3723 		 * We should eventually deal with following mode pages:
3724 		 * error recovery  (0x01),
3725 		 * power condition (0x1a),
3726 		 * exception control page (enables SMART) (0x1c),
3727 		 * enclosure management (ses),
3728 		 * protocol-specific port mode (port control).
3729 		 */
3730 		switch (scsipkt->pkt_cdbp[2] & 0x3f) {
3731 		case MODEPAGE_RW_ERRRECOV:
3732 			/* DAD_MODE_ERR_RECOV */
3733 			/* R/W recovery */
3734 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
3735 			break;
3736 		case MODEPAGE_CACHING:
3737 			/* DAD_MODE_CACHE */
3738 			/* Reject not supported request for saved parameters */
3739 			if (pc == 3) {
3740 				*scsipkt->pkt_scbp = STATUS_CHECK;
3741 				sense = sata_arq_sense(spx);
3742 				sense->es_key = KEY_ILLEGAL_REQUEST;
3743 				sense->es_add_code =
3744 				    SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
3745 				goto done;
3746 			}
3747 
3748 			/* caching */
3749 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
3750 			break;
3751 		case MODEPAGE_INFO_EXCPT:
3752 			/* exception cntrl */
3753 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
3754 				len += sata_build_msense_page_1c(sdinfo, pc,
3755 				    buf+len);
3756 			}
3757 			else
3758 				goto err;
3759 			break;
3760 		case MODEPAGE_POWER_COND:
3761 			/* DAD_MODE_POWER_COND */
3762 			/* power condition */
3763 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
3764 			break;
3765 
3766 		case MODEPAGE_ACOUSTIC_MANAG:
3767 			/* acoustic management */
3768 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
3769 			break;
3770 		case MODEPAGE_ALLPAGES:
3771 			/* all pages */
3772 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
3773 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
3774 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
3775 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
3776 				len += sata_build_msense_page_1c(sdinfo, pc,
3777 				    buf+len);
3778 			}
3779 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
3780 			break;
3781 		default:
3782 		err:
3783 			/* Invalid request */
3784 			*scsipkt->pkt_scbp = STATUS_CHECK;
3785 			sense = sata_arq_sense(spx);
3786 			sense->es_key = KEY_ILLEGAL_REQUEST;
3787 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3788 			goto done;
3789 		}
3790 
3791 		/* fix total mode data length */
3792 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3793 			/* 4-byte mode parameter header */
3794 			buf[0] = len - 1;   	/* mode data length */
3795 		} else {
3796 			buf[0] = (len -2) >> 8;
3797 			buf[1] = (len -2) & 0xff;
3798 		}
3799 
3800 
3801 		/* Check allocation length */
3802 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3803 			alc_len = scsipkt->pkt_cdbp[4];
3804 		} else {
3805 			alc_len = scsipkt->pkt_cdbp[7];
3806 			alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
3807 		}
3808 		/*
3809 		 * We do not check for possible parameters truncation
3810 		 * (alc_len < len) assuming that the target driver works
3811 		 * correctly. Just avoiding overrun.
3812 		 * Copy no more than requested and possible, buffer-wise.
3813 		 */
3814 		count = MIN(alc_len, len);
3815 		count = MIN(bp->b_bcount, count);
3816 		bcopy(buf, bp->b_un.b_addr, count);
3817 
3818 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3819 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
3820 	}
3821 	*scsipkt->pkt_scbp = STATUS_GOOD;
3822 done:
3823 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3824 	(void) kmem_free(buf, 1024);
3825 
3826 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3827 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3828 
3829 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3830 	    scsipkt->pkt_comp != NULL)
3831 		/* scsi callback required */
3832 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3833 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3834 		    TQ_SLEEP) == NULL)
3835 			/* Scheduling the callback failed */
3836 			return (TRAN_BUSY);
3837 
3838 	return (TRAN_ACCEPT);
3839 }
3840 
3841 
3842 /*
3843  * SATA translate command: Mode Select.
3844  * Translated into appropriate SATA command or emulated.
3845  * Saving parameters is not supported.
3846  * Changing device capacity is not supported (although theoretically
3847  * possible by executing SET FEATURES/SET MAX ADDRESS)
3848  *
3849  * Assumption is that the target driver is working correctly.
3850  *
3851  * More than one SATA command may be executed to perform operations specified
3852  * by mode select pages. The first error terminates further execution.
3853  * Operations performed successully are not backed-up in such case.
3854  *
3855  * NOTE: Implemented pages:
3856  * - caching page
3857  * - informational exception page
3858  * - acoustic management page
3859  * Caching setup is remembered so it could be re-stored in case of
3860  * an unexpected device reset.
3861  *
3862  * Returns TRAN_XXXX.
3863  * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields.
3864  */
3865 
3866 static int
3867 sata_txlt_mode_select(sata_pkt_txlate_t *spx)
3868 {
3869 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3870 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3871 	struct scsi_extended_sense *sense;
3872 	int len, pagelen, count, pllen;
3873 	uint8_t *buf;	/* mode select buffer */
3874 	int rval, stat, reason;
3875 	uint_t nointr_flag;
3876 	int dmod = 0;
3877 
3878 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3879 	    "sata_txlt_mode_select, pc %x page code 0x%02x\n",
3880 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
3881 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
3882 
3883 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3884 
3885 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3886 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3887 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3888 		return (rval);
3889 	}
3890 	/*
3891 	 * If in interrupt context, reject this packet because it may result
3892 	 * in issuing a synchronous command to HBA.
3893 	 */
3894 	if (servicing_interrupt()) {
3895 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
3896 		    "sata_txlt_mode_select: rejecting command because "
3897 		    "of interrupt context\n", NULL);
3898 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3899 		return (TRAN_BUSY);
3900 	}
3901 
3902 	rval = TRAN_ACCEPT;
3903 
3904 	scsipkt->pkt_reason = CMD_CMPLT;
3905 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3906 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3907 	nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
3908 
3909 	/* Reject not supported request */
3910 	if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
3911 		*scsipkt->pkt_scbp = STATUS_CHECK;
3912 		sense = sata_arq_sense(spx);
3913 		sense->es_key = KEY_ILLEGAL_REQUEST;
3914 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3915 		goto done;
3916 	}
3917 
3918 	if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
3919 		pllen = scsipkt->pkt_cdbp[4];
3920 	} else {
3921 		pllen = scsipkt->pkt_cdbp[7];
3922 		pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
3923 	}
3924 
3925 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
3926 
3927 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
3928 		buf = (uint8_t *)bp->b_un.b_addr;
3929 		count = MIN(bp->b_bcount, pllen);
3930 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3931 		scsipkt->pkt_resid = 0;
3932 		pllen = count;
3933 
3934 		/*
3935 		 * Check the header to skip the block descriptor(s) - we
3936 		 * do not support setting device capacity.
3937 		 * Existing macros do not recognize long LBA dscriptor,
3938 		 * hence manual calculation.
3939 		 */
3940 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
3941 			/* 6-bytes CMD, 4 bytes header */
3942 			if (count <= 4)
3943 				goto done;		/* header only */
3944 			len = buf[3] + 4;
3945 		} else {
3946 			/* 10-bytes CMD, 8 bytes header */
3947 			if (count <= 8)
3948 				goto done;		/* header only */
3949 			len = buf[6];
3950 			len = (len << 8) + buf[7] + 8;
3951 		}
3952 		if (len >= count)
3953 			goto done;	/* header + descriptor(s) only */
3954 
3955 		pllen -= len;		/* remaining data length */
3956 
3957 		/*
3958 		 * We may be executing SATA command and want to execute it
3959 		 * in SYNCH mode, regardless of scsi_pkt setting.
3960 		 * Save scsi_pkt setting and indicate SYNCH mode
3961 		 */
3962 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3963 		    scsipkt->pkt_comp != NULL) {
3964 			scsipkt->pkt_flags |= FLAG_NOINTR;
3965 		}
3966 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
3967 
3968 		/*
3969 		 * len is now the offset to a first mode select page
3970 		 * Process all pages
3971 		 */
3972 		while (pllen > 0) {
3973 			switch ((int)buf[len]) {
3974 			case MODEPAGE_CACHING:
3975 				/* No support for SP (saving) */
3976 				if (scsipkt->pkt_cdbp[1] & 0x01) {
3977 					*scsipkt->pkt_scbp = STATUS_CHECK;
3978 					sense = sata_arq_sense(spx);
3979 					sense->es_key = KEY_ILLEGAL_REQUEST;
3980 					sense->es_add_code =
3981 					    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3982 					goto done;
3983 				}
3984 				stat = sata_mode_select_page_8(spx,
3985 				    (struct mode_cache_scsi3 *)&buf[len],
3986 				    pllen, &pagelen, &rval, &dmod);
3987 				/*
3988 				 * The pagelen value indicates the number of
3989 				 * parameter bytes already processed.
3990 				 * The rval is the return value from
3991 				 * sata_tran_start().
3992 				 * The stat indicates the overall status of
3993 				 * the operation(s).
3994 				 */
3995 				if (stat != SATA_SUCCESS)
3996 					/*
3997 					 * Page processing did not succeed -
3998 					 * all error info is already set-up,
3999 					 * just return
4000 					 */
4001 					pllen = 0; /* this breaks the loop */
4002 				else {
4003 					len += pagelen;
4004 					pllen -= pagelen;
4005 				}
4006 				break;
4007 
4008 			case MODEPAGE_INFO_EXCPT:
4009 				stat = sata_mode_select_page_1c(spx,
4010 				    (struct mode_info_excpt_page *)&buf[len],
4011 				    pllen, &pagelen, &rval, &dmod);
4012 				/*
4013 				 * The pagelen value indicates the number of
4014 				 * parameter bytes already processed.
4015 				 * The rval is the return value from
4016 				 * sata_tran_start().
4017 				 * The stat indicates the overall status of
4018 				 * the operation(s).
4019 				 */
4020 				if (stat != SATA_SUCCESS)
4021 					/*
4022 					 * Page processing did not succeed -
4023 					 * all error info is already set-up,
4024 					 * just return
4025 					 */
4026 					pllen = 0; /* this breaks the loop */
4027 				else {
4028 					len += pagelen;
4029 					pllen -= pagelen;
4030 				}
4031 				break;
4032 
4033 			case MODEPAGE_ACOUSTIC_MANAG:
4034 				stat = sata_mode_select_page_30(spx,
4035 				    (struct mode_acoustic_management *)
4036 				    &buf[len], pllen, &pagelen, &rval, &dmod);
4037 				/*
4038 				 * The pagelen value indicates the number of
4039 				 * parameter bytes already processed.
4040 				 * The rval is the return value from
4041 				 * sata_tran_start().
4042 				 * The stat indicates the overall status of
4043 				 * the operation(s).
4044 				 */
4045 				if (stat != SATA_SUCCESS)
4046 					/*
4047 					 * Page processing did not succeed -
4048 					 * all error info is already set-up,
4049 					 * just return
4050 					 */
4051 					pllen = 0; /* this breaks the loop */
4052 				else {
4053 					len += pagelen;
4054 					pllen -= pagelen;
4055 				}
4056 
4057 				break;
4058 			default:
4059 				*scsipkt->pkt_scbp = STATUS_CHECK;
4060 				sense = sata_arq_sense(spx);
4061 				sense->es_key = KEY_ILLEGAL_REQUEST;
4062 				sense->es_add_code =
4063 				    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
4064 				goto done;
4065 			}
4066 		}
4067 	}
4068 done:
4069 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4070 	/*
4071 	 * If device parameters were modified, fetch and store the new
4072 	 * Identify Device data. Since port mutex could have been released
4073 	 * for accessing HBA driver, we need to re-check device existence.
4074 	 */
4075 	if (dmod != 0) {
4076 		sata_drive_info_t new_sdinfo, *sdinfo;
4077 		int rv = 0;
4078 
4079 		/*
4080 		 * Following statement has to be changed if this function is
4081 		 * used for devices other than SATA hard disks.
4082 		 */
4083 		new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
4084 
4085 		new_sdinfo.satadrv_addr =
4086 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr;
4087 		rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
4088 		    &new_sdinfo);
4089 
4090 		mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4091 		/*
4092 		 * Since port mutex could have been released when
4093 		 * accessing HBA driver, we need to re-check that the
4094 		 * framework still holds the device info structure.
4095 		 */
4096 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4097 		    &spx->txlt_sata_pkt->satapkt_device);
4098 		if (sdinfo != NULL) {
4099 			/*
4100 			 * Device still has info structure in the
4101 			 * sata framework. Copy newly fetched info
4102 			 */
4103 			if (rv == 0) {
4104 				sdinfo->satadrv_id = new_sdinfo.satadrv_id;
4105 				sata_save_drive_settings(sdinfo);
4106 			} else {
4107 				/*
4108 				 * Could not fetch new data - invalidate
4109 				 * sata_drive_info. That makes device
4110 				 * unusable.
4111 				 */
4112 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
4113 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
4114 			}
4115 		}
4116 		if (rv != 0 || sdinfo == NULL) {
4117 			/*
4118 			 * This changes the overall mode select completion
4119 			 * reason to a failed one !!!!!
4120 			 */
4121 			*scsipkt->pkt_scbp = STATUS_CHECK;
4122 			sense = sata_arq_sense(spx);
4123 			scsipkt->pkt_reason = CMD_INCOMPLETE;
4124 			rval = TRAN_ACCEPT;
4125 		}
4126 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4127 	}
4128 	/* Restore the scsi pkt flags */
4129 	scsipkt->pkt_flags &= ~FLAG_NOINTR;
4130 	scsipkt->pkt_flags |= nointr_flag;
4131 
4132 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4133 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4134 
4135 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4136 	    scsipkt->pkt_comp != NULL)
4137 		/* scsi callback required */
4138 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4139 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4140 		    TQ_SLEEP) == NULL)
4141 			/* Scheduling the callback failed */
4142 			return (TRAN_BUSY);
4143 
4144 	return (rval);
4145 }
4146 
4147 
4148 
4149 /*
4150  * Translate command: Log Sense
4151  */
4152 static 	int
4153 sata_txlt_log_sense(sata_pkt_txlate_t *spx)
4154 {
4155 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4156 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4157 	sata_drive_info_t *sdinfo;
4158 	struct scsi_extended_sense *sense;
4159 	int 		len, count, alc_len;
4160 	int		pc;	/* Page Control code */
4161 	int		page_code;	/* Page code */
4162 	uint8_t		*buf;	/* log sense buffer */
4163 	int		rval, reason;
4164 #define	MAX_LOG_SENSE_PAGE_SIZE	512
4165 
4166 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4167 	    "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
4168 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4169 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4170 
4171 	buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
4172 
4173 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4174 
4175 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4176 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4177 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4178 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4179 		return (rval);
4180 	}
4181 	/*
4182 	 * If in interrupt context, reject this packet because it may result
4183 	 * in issuing a synchronous command to HBA.
4184 	 */
4185 	if (servicing_interrupt()) {
4186 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
4187 		    "sata_log_sense: rejecting command because "
4188 		    "of interrupt context\n", NULL);
4189 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4190 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4191 		return (TRAN_BUSY);
4192 	}
4193 
4194 	scsipkt->pkt_reason = CMD_CMPLT;
4195 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4196 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4197 
4198 	pc = scsipkt->pkt_cdbp[2] >> 6;
4199 	page_code = scsipkt->pkt_cdbp[2] & 0x3f;
4200 
4201 	/* Reject not supported request for all but cumulative values */
4202 	switch (pc) {
4203 	case PC_CUMULATIVE_VALUES:
4204 		break;
4205 	default:
4206 		*scsipkt->pkt_scbp = STATUS_CHECK;
4207 		sense = sata_arq_sense(spx);
4208 		sense->es_key = KEY_ILLEGAL_REQUEST;
4209 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4210 		goto done;
4211 	}
4212 
4213 	switch (page_code) {
4214 	case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
4215 	case PAGE_CODE_SELF_TEST_RESULTS:
4216 	case PAGE_CODE_INFORMATION_EXCEPTIONS:
4217 	case PAGE_CODE_SMART_READ_DATA:
4218 		break;
4219 	default:
4220 		*scsipkt->pkt_scbp = STATUS_CHECK;
4221 		sense = sata_arq_sense(spx);
4222 		sense->es_key = KEY_ILLEGAL_REQUEST;
4223 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4224 		goto done;
4225 	}
4226 
4227 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4228 		/*
4229 		 * Because log sense uses local buffers for data retrieval from
4230 		 * the devices and sets the data programatically in the
4231 		 * original specified buffer, release preallocated DMA
4232 		 * resources before storing data in the original buffer,
4233 		 * so no unwanted DMA sync would take place.
4234 		 */
4235 		sata_id_t *sata_id;
4236 
4237 		sata_scsi_dmafree(NULL, scsipkt);
4238 
4239 		len = 0;
4240 
4241 		/* Build log parameter header */
4242 		buf[len++] = page_code;	/* page code as in the CDB */
4243 		buf[len++] = 0;		/* reserved */
4244 		buf[len++] = 0;		/* Zero out page length for now (MSB) */
4245 		buf[len++] = 0;		/* (LSB) */
4246 
4247 		sdinfo = sata_get_device_info(
4248 		    spx->txlt_sata_hba_inst,
4249 		    &spx->txlt_sata_pkt->satapkt_device);
4250 
4251 		/*
4252 		 * Add requested pages.
4253 		 */
4254 		switch (page_code) {
4255 		case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
4256 			len = sata_build_lsense_page_0(sdinfo, buf + len);
4257 			break;
4258 		case PAGE_CODE_SELF_TEST_RESULTS:
4259 			sata_id = &sdinfo->satadrv_id;
4260 			if ((! (sata_id->ai_cmdset84 &
4261 			    SATA_SMART_SELF_TEST_SUPPORTED)) ||
4262 			    (! (sata_id->ai_features87 &
4263 			    SATA_SMART_SELF_TEST_SUPPORTED))) {
4264 				*scsipkt->pkt_scbp = STATUS_CHECK;
4265 				sense = sata_arq_sense(spx);
4266 				sense->es_key = KEY_ILLEGAL_REQUEST;
4267 				sense->es_add_code =
4268 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4269 
4270 				goto done;
4271 			}
4272 			len = sata_build_lsense_page_10(sdinfo, buf + len,
4273 			    spx->txlt_sata_hba_inst);
4274 			break;
4275 		case PAGE_CODE_INFORMATION_EXCEPTIONS:
4276 			sata_id = &sdinfo->satadrv_id;
4277 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
4278 				*scsipkt->pkt_scbp = STATUS_CHECK;
4279 				sense = sata_arq_sense(spx);
4280 				sense->es_key = KEY_ILLEGAL_REQUEST;
4281 				sense->es_add_code =
4282 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4283 
4284 				goto done;
4285 			}
4286 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
4287 				*scsipkt->pkt_scbp = STATUS_CHECK;
4288 				sense = sata_arq_sense(spx);
4289 				sense->es_key = KEY_ABORTED_COMMAND;
4290 				sense->es_add_code =
4291 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
4292 				sense->es_qual_code =
4293 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
4294 
4295 				goto done;
4296 			}
4297 
4298 			len = sata_build_lsense_page_2f(sdinfo, buf + len,
4299 			    spx->txlt_sata_hba_inst);
4300 			break;
4301 		case PAGE_CODE_SMART_READ_DATA:
4302 			sata_id = &sdinfo->satadrv_id;
4303 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
4304 				*scsipkt->pkt_scbp = STATUS_CHECK;
4305 				sense = sata_arq_sense(spx);
4306 				sense->es_key = KEY_ILLEGAL_REQUEST;
4307 				sense->es_add_code =
4308 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4309 
4310 				goto done;
4311 			}
4312 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
4313 				*scsipkt->pkt_scbp = STATUS_CHECK;
4314 				sense = sata_arq_sense(spx);
4315 				sense->es_key = KEY_ABORTED_COMMAND;
4316 				sense->es_add_code =
4317 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
4318 				sense->es_qual_code =
4319 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
4320 
4321 				goto done;
4322 			}
4323 
4324 			/* This page doesn't include a page header */
4325 			len = sata_build_lsense_page_30(sdinfo, buf,
4326 			    spx->txlt_sata_hba_inst);
4327 			goto no_header;
4328 		default:
4329 			/* Invalid request */
4330 			*scsipkt->pkt_scbp = STATUS_CHECK;
4331 			sense = sata_arq_sense(spx);
4332 			sense->es_key = KEY_ILLEGAL_REQUEST;
4333 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4334 			goto done;
4335 		}
4336 
4337 		/* set parameter log sense data length */
4338 		buf[2] = len >> 8;	/* log sense length (MSB) */
4339 		buf[3] = len & 0xff;	/* log sense length (LSB) */
4340 
4341 		len += SCSI_LOG_PAGE_HDR_LEN;
4342 		ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
4343 
4344 no_header:
4345 		/* Check allocation length */
4346 		alc_len = scsipkt->pkt_cdbp[7];
4347 		alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
4348 
4349 		/*
4350 		 * We do not check for possible parameters truncation
4351 		 * (alc_len < len) assuming that the target driver works
4352 		 * correctly. Just avoiding overrun.
4353 		 * Copy no more than requested and possible, buffer-wise.
4354 		 */
4355 		count = MIN(alc_len, len);
4356 		count = MIN(bp->b_bcount, count);
4357 		bcopy(buf, bp->b_un.b_addr, count);
4358 
4359 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4360 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
4361 	}
4362 	*scsipkt->pkt_scbp = STATUS_GOOD;
4363 done:
4364 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4365 	(void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4366 
4367 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4368 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4369 
4370 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4371 	    scsipkt->pkt_comp != NULL)
4372 		/* scsi callback required */
4373 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4374 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4375 		    TQ_SLEEP) == NULL)
4376 			/* Scheduling the callback failed */
4377 			return (TRAN_BUSY);
4378 
4379 	return (TRAN_ACCEPT);
4380 }
4381 
4382 /*
4383  * Translate command: Log Select
4384  * Not implemented at this time - returns invalid command response.
4385  */
4386 static 	int
4387 sata_txlt_log_select(sata_pkt_txlate_t *spx)
4388 {
4389 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4390 	    "sata_txlt_log_select\n", NULL);
4391 
4392 	return (sata_txlt_invalid_command(spx));
4393 }
4394 
4395 
4396 /*
4397  * Translate command: Read (various types).
4398  * Translated into appropriate type of ATA READ command
4399  * for SATA hard disks.
4400  * Both the device capabilities and requested operation mode are
4401  * considered.
4402  *
4403  * Following scsi cdb fields are ignored:
4404  * rdprotect, dpo, fua, fua_nv, group_number.
4405  *
4406  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
4407  * enable variable sata_func_enable), the capability of the controller and
4408  * capability of a device are checked and if both support queueing, read
4409  * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
4410  * command rather than plain READ_XXX command.
4411  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
4412  * both the controller and device suport such functionality, the read
4413  * request will be translated to READ_FPDMA_QUEUED command.
4414  * In both cases the maximum queue depth is derived as minimum of:
4415  * HBA capability,device capability and sata_max_queue_depth variable setting.
4416  * The value passed to HBA driver is decremented by 1, because only 5 bits are
4417  * used to pass max queue depth value, and the maximum possible queue depth
4418  * is 32.
4419  *
4420  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4421  * appropriate values in scsi_pkt fields.
4422  */
4423 static int
4424 sata_txlt_read(sata_pkt_txlate_t *spx)
4425 {
4426 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4427 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4428 	sata_drive_info_t *sdinfo;
4429 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4430 	int cport = SATA_TXLT_CPORT(spx);
4431 	uint16_t sec_count;
4432 	uint64_t lba;
4433 	int rval, reason;
4434 	int synch;
4435 
4436 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4437 
4438 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4439 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4440 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4441 		return (rval);
4442 	}
4443 
4444 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4445 	    &spx->txlt_sata_pkt->satapkt_device);
4446 
4447 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
4448 	/*
4449 	 * Extract LBA and sector count from scsi CDB.
4450 	 */
4451 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
4452 	case SCMD_READ:
4453 		/* 6-byte scsi read cmd : 0x08 */
4454 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
4455 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
4456 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4457 		sec_count = scsipkt->pkt_cdbp[4];
4458 		/* sec_count 0 will be interpreted as 256 by a device */
4459 		break;
4460 	case SCMD_READ_G1:
4461 		/* 10-bytes scsi read command : 0x28 */
4462 		lba = scsipkt->pkt_cdbp[2];
4463 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4464 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4465 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4466 		sec_count = scsipkt->pkt_cdbp[7];
4467 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4468 		break;
4469 	case SCMD_READ_G5:
4470 		/* 12-bytes scsi read command : 0xA8 */
4471 		lba = scsipkt->pkt_cdbp[2];
4472 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4473 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4474 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4475 		sec_count = scsipkt->pkt_cdbp[6];
4476 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
4477 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4478 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
4479 		break;
4480 	case SCMD_READ_G4:
4481 		/* 16-bytes scsi read command : 0x88 */
4482 		lba = scsipkt->pkt_cdbp[2];
4483 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4484 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4485 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4486 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
4487 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
4488 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
4489 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
4490 		sec_count = scsipkt->pkt_cdbp[10];
4491 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
4492 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
4493 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
4494 		break;
4495 	default:
4496 		/* Unsupported command */
4497 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4498 		return (sata_txlt_invalid_command(spx));
4499 	}
4500 
4501 	/*
4502 	 * Check if specified address exceeds device capacity
4503 	 */
4504 	if ((lba >= sdinfo->satadrv_capacity) ||
4505 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
4506 		/* LBA out of range */
4507 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4508 		return (sata_txlt_lba_out_of_range(spx));
4509 	}
4510 
4511 	/*
4512 	 * For zero-length transfer, emulate good completion of the command
4513 	 * (reasons for rejecting the command were already checked).
4514 	 * No DMA resources were allocated.
4515 	 */
4516 	if (spx->txlt_dma_cookie_list == NULL) {
4517 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4518 		return (sata_emul_rw_completion(spx));
4519 	}
4520 
4521 	/*
4522 	 * Build cmd block depending on the device capability and
4523 	 * requested operation mode.
4524 	 * Do not bother with non-dma mode - we are working only with
4525 	 * devices supporting DMA.
4526 	 */
4527 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
4528 	scmd->satacmd_device_reg = SATA_ADH_LBA;
4529 	scmd->satacmd_cmd_reg = SATAC_READ_DMA;
4530 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
4531 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4532 		scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT;
4533 		scmd->satacmd_sec_count_msb = sec_count >> 8;
4534 #ifndef __lock_lint
4535 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
4536 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
4537 		scmd->satacmd_lba_high_msb = lba >> 40;
4538 #endif
4539 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
4540 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
4541 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
4542 	}
4543 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
4544 	scmd->satacmd_lba_low_lsb = lba & 0xff;
4545 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
4546 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
4547 	scmd->satacmd_features_reg = 0;
4548 	scmd->satacmd_status_reg = 0;
4549 	scmd->satacmd_error_reg = 0;
4550 
4551 	/*
4552 	 * Check if queueing commands should be used and switch
4553 	 * to appropriate command if possible
4554 	 */
4555 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
4556 		boolean_t using_queuing;
4557 
4558 		/* Queuing supported by controller and device? */
4559 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
4560 		    (sdinfo->satadrv_features_support &
4561 		    SATA_DEV_F_NCQ) &&
4562 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4563 		    SATA_CTLF_NCQ)) {
4564 			using_queuing = B_TRUE;
4565 
4566 			/* NCQ supported - use FPDMA READ */
4567 			scmd->satacmd_cmd_reg =
4568 			    SATAC_READ_FPDMA_QUEUED;
4569 			scmd->satacmd_features_reg_ext =
4570 			    scmd->satacmd_sec_count_msb;
4571 			scmd->satacmd_sec_count_msb = 0;
4572 		} else if ((sdinfo->satadrv_features_support &
4573 		    SATA_DEV_F_TCQ) &&
4574 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4575 		    SATA_CTLF_QCMD)) {
4576 			using_queuing = B_TRUE;
4577 
4578 			/* Legacy queueing */
4579 			if (sdinfo->satadrv_features_support &
4580 			    SATA_DEV_F_LBA48) {
4581 				scmd->satacmd_cmd_reg =
4582 				    SATAC_READ_DMA_QUEUED_EXT;
4583 				scmd->satacmd_features_reg_ext =
4584 				    scmd->satacmd_sec_count_msb;
4585 				scmd->satacmd_sec_count_msb = 0;
4586 			} else {
4587 				scmd->satacmd_cmd_reg =
4588 				    SATAC_READ_DMA_QUEUED;
4589 			}
4590 		} else	/* NCQ nor legacy queuing not supported */
4591 			using_queuing = B_FALSE;
4592 
4593 		/*
4594 		 * If queuing, the sector count goes in the features register
4595 		 * and the secount count will contain the tag.
4596 		 */
4597 		if (using_queuing) {
4598 			scmd->satacmd_features_reg =
4599 			    scmd->satacmd_sec_count_lsb;
4600 			scmd->satacmd_sec_count_lsb = 0;
4601 			scmd->satacmd_flags.sata_queued = B_TRUE;
4602 
4603 			/* Set-up maximum queue depth */
4604 			scmd->satacmd_flags.sata_max_queue_depth =
4605 			    sdinfo->satadrv_max_queue_depth - 1;
4606 		} else if (sdinfo->satadrv_features_enabled &
4607 		    SATA_DEV_F_E_UNTAGGED_QING) {
4608 			/*
4609 			 * Although NCQ/TCQ is not enabled, untagged queuing
4610 			 * may be still used.
4611 			 * Set-up the maximum untagged queue depth.
4612 			 * Use controller's queue depth from sata_hba_tran.
4613 			 * SATA HBA drivers may ignore this value and rely on
4614 			 * the internal limits.For drivers that do not
4615 			 * ignore untaged queue depth, limit the value to
4616 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
4617 			 * largest value that can be passed via
4618 			 * satacmd_flags.sata_max_queue_depth.
4619 			 */
4620 			scmd->satacmd_flags.sata_max_queue_depth =
4621 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
4622 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
4623 
4624 		} else {
4625 			scmd->satacmd_flags.sata_max_queue_depth = 0;
4626 		}
4627 	} else
4628 		scmd->satacmd_flags.sata_max_queue_depth = 0;
4629 
4630 	SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst,
4631 	    "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n",
4632 	    scmd->satacmd_cmd_reg, lba, sec_count);
4633 
4634 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4635 		/* Need callback function */
4636 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
4637 		synch = FALSE;
4638 	} else
4639 		synch = TRUE;
4640 
4641 	/* Transfer command to HBA */
4642 	if (sata_hba_start(spx, &rval) != 0) {
4643 		/* Pkt not accepted for execution */
4644 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4645 		return (rval);
4646 	}
4647 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4648 	/*
4649 	 * If execution is non-synchronous,
4650 	 * a callback function will handle potential errors, translate
4651 	 * the response and will do a callback to a target driver.
4652 	 * If it was synchronous, check execution status using the same
4653 	 * framework callback.
4654 	 */
4655 	if (synch) {
4656 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4657 		    "synchronous execution status %x\n",
4658 		    spx->txlt_sata_pkt->satapkt_reason);
4659 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
4660 	}
4661 	return (TRAN_ACCEPT);
4662 }
4663 
4664 
4665 /*
4666  * SATA translate command: Write (various types)
4667  * Translated into appropriate type of ATA WRITE command
4668  * for SATA hard disks.
4669  * Both the device capabilities and requested operation mode are
4670  * considered.
4671  *
4672  * Following scsi cdb fields are ignored:
4673  * rwprotect, dpo, fua, fua_nv, group_number.
4674  *
4675  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
4676  * enable variable sata_func_enable), the capability of the controller and
4677  * capability of a device are checked and if both support queueing, write
4678  * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT
4679  * command rather than plain WRITE_XXX command.
4680  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
4681  * both the controller and device suport such functionality, the write
4682  * request will be translated to WRITE_FPDMA_QUEUED command.
4683  * In both cases the maximum queue depth is derived as minimum of:
4684  * HBA capability,device capability and sata_max_queue_depth variable setting.
4685  * The value passed to HBA driver is decremented by 1, because only 5 bits are
4686  * used to pass max queue depth value, and the maximum possible queue depth
4687  * is 32.
4688  *
4689  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4690  * appropriate values in scsi_pkt fields.
4691  */
4692 static int
4693 sata_txlt_write(sata_pkt_txlate_t *spx)
4694 {
4695 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4696 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4697 	sata_drive_info_t *sdinfo;
4698 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4699 	int cport = SATA_TXLT_CPORT(spx);
4700 	uint16_t sec_count;
4701 	uint64_t lba;
4702 	int rval, reason;
4703 	int synch;
4704 
4705 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4706 
4707 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4708 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4709 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4710 		return (rval);
4711 	}
4712 
4713 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4714 	    &spx->txlt_sata_pkt->satapkt_device);
4715 
4716 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4717 	/*
4718 	 * Extract LBA and sector count from scsi CDB
4719 	 */
4720 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
4721 	case SCMD_WRITE:
4722 		/* 6-byte scsi read cmd : 0x0A */
4723 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
4724 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
4725 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4726 		sec_count = scsipkt->pkt_cdbp[4];
4727 		/* sec_count 0 will be interpreted as 256 by a device */
4728 		break;
4729 	case SCMD_WRITE_G1:
4730 		/* 10-bytes scsi write command : 0x2A */
4731 		lba = scsipkt->pkt_cdbp[2];
4732 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4733 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4734 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4735 		sec_count = scsipkt->pkt_cdbp[7];
4736 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4737 		break;
4738 	case SCMD_WRITE_G5:
4739 		/* 12-bytes scsi read command : 0xAA */
4740 		lba = scsipkt->pkt_cdbp[2];
4741 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4742 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4743 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4744 		sec_count = scsipkt->pkt_cdbp[6];
4745 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
4746 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4747 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
4748 		break;
4749 	case SCMD_WRITE_G4:
4750 		/* 16-bytes scsi write command : 0x8A */
4751 		lba = scsipkt->pkt_cdbp[2];
4752 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4753 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4754 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4755 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
4756 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
4757 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
4758 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
4759 		sec_count = scsipkt->pkt_cdbp[10];
4760 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
4761 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
4762 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
4763 		break;
4764 	default:
4765 		/* Unsupported command */
4766 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4767 		return (sata_txlt_invalid_command(spx));
4768 	}
4769 
4770 	/*
4771 	 * Check if specified address and length exceeds device capacity
4772 	 */
4773 	if ((lba >= sdinfo->satadrv_capacity) ||
4774 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
4775 		/* LBA out of range */
4776 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4777 		return (sata_txlt_lba_out_of_range(spx));
4778 	}
4779 
4780 	/*
4781 	 * For zero-length transfer, emulate good completion of the command
4782 	 * (reasons for rejecting the command were already checked).
4783 	 * No DMA resources were allocated.
4784 	 */
4785 	if (spx->txlt_dma_cookie_list == NULL) {
4786 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4787 		return (sata_emul_rw_completion(spx));
4788 	}
4789 
4790 	/*
4791 	 * Build cmd block depending on the device capability and
4792 	 * requested operation mode.
4793 	 * Do not bother with non-dma mode- we are working only with
4794 	 * devices supporting DMA.
4795 	 */
4796 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
4797 	scmd->satacmd_device_reg = SATA_ADH_LBA;
4798 	scmd->satacmd_cmd_reg = SATAC_WRITE_DMA;
4799 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
4800 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4801 		scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT;
4802 		scmd->satacmd_sec_count_msb = sec_count >> 8;
4803 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
4804 #ifndef __lock_lint
4805 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
4806 		scmd->satacmd_lba_high_msb = lba >> 40;
4807 #endif
4808 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
4809 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
4810 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
4811 	}
4812 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
4813 	scmd->satacmd_lba_low_lsb = lba & 0xff;
4814 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
4815 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
4816 	scmd->satacmd_features_reg = 0;
4817 	scmd->satacmd_status_reg = 0;
4818 	scmd->satacmd_error_reg = 0;
4819 
4820 	/*
4821 	 * Check if queueing commands should be used and switch
4822 	 * to appropriate command if possible
4823 	 */
4824 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
4825 		boolean_t using_queuing;
4826 
4827 		/* Queuing supported by controller and device? */
4828 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
4829 		    (sdinfo->satadrv_features_support &
4830 		    SATA_DEV_F_NCQ) &&
4831 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4832 		    SATA_CTLF_NCQ)) {
4833 			using_queuing = B_TRUE;
4834 
4835 			/* NCQ supported - use FPDMA WRITE */
4836 			scmd->satacmd_cmd_reg =
4837 			    SATAC_WRITE_FPDMA_QUEUED;
4838 			scmd->satacmd_features_reg_ext =
4839 			    scmd->satacmd_sec_count_msb;
4840 			scmd->satacmd_sec_count_msb = 0;
4841 		} else if ((sdinfo->satadrv_features_support &
4842 		    SATA_DEV_F_TCQ) &&
4843 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4844 		    SATA_CTLF_QCMD)) {
4845 			using_queuing = B_TRUE;
4846 
4847 			/* Legacy queueing */
4848 			if (sdinfo->satadrv_features_support &
4849 			    SATA_DEV_F_LBA48) {
4850 				scmd->satacmd_cmd_reg =
4851 				    SATAC_WRITE_DMA_QUEUED_EXT;
4852 				scmd->satacmd_features_reg_ext =
4853 				    scmd->satacmd_sec_count_msb;
4854 				scmd->satacmd_sec_count_msb = 0;
4855 			} else {
4856 				scmd->satacmd_cmd_reg =
4857 				    SATAC_WRITE_DMA_QUEUED;
4858 			}
4859 		} else	/*  NCQ nor legacy queuing not supported */
4860 			using_queuing = B_FALSE;
4861 
4862 		if (using_queuing) {
4863 			scmd->satacmd_features_reg =
4864 			    scmd->satacmd_sec_count_lsb;
4865 			scmd->satacmd_sec_count_lsb = 0;
4866 			scmd->satacmd_flags.sata_queued = B_TRUE;
4867 			/* Set-up maximum queue depth */
4868 			scmd->satacmd_flags.sata_max_queue_depth =
4869 			    sdinfo->satadrv_max_queue_depth - 1;
4870 		} else if (sdinfo->satadrv_features_enabled &
4871 		    SATA_DEV_F_E_UNTAGGED_QING) {
4872 			/*
4873 			 * Although NCQ/TCQ is not enabled, untagged queuing
4874 			 * may be still used.
4875 			 * Set-up the maximum untagged queue depth.
4876 			 * Use controller's queue depth from sata_hba_tran.
4877 			 * SATA HBA drivers may ignore this value and rely on
4878 			 * the internal limits. For drivera that do not
4879 			 * ignore untaged queue depth, limit the value to
4880 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
4881 			 * largest value that can be passed via
4882 			 * satacmd_flags.sata_max_queue_depth.
4883 			 */
4884 			scmd->satacmd_flags.sata_max_queue_depth =
4885 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
4886 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
4887 
4888 		} else {
4889 			scmd->satacmd_flags.sata_max_queue_depth = 0;
4890 		}
4891 	} else
4892 		scmd->satacmd_flags.sata_max_queue_depth = 0;
4893 
4894 	SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4895 	    "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n",
4896 	    scmd->satacmd_cmd_reg, lba, sec_count);
4897 
4898 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4899 		/* Need callback function */
4900 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
4901 		synch = FALSE;
4902 	} else
4903 		synch = TRUE;
4904 
4905 	/* Transfer command to HBA */
4906 	if (sata_hba_start(spx, &rval) != 0) {
4907 		/* Pkt not accepted for execution */
4908 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4909 		return (rval);
4910 	}
4911 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4912 
4913 	/*
4914 	 * If execution is non-synchronous,
4915 	 * a callback function will handle potential errors, translate
4916 	 * the response and will do a callback to a target driver.
4917 	 * If it was synchronous, check execution status using the same
4918 	 * framework callback.
4919 	 */
4920 	if (synch) {
4921 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4922 		    "synchronous execution status %x\n",
4923 		    spx->txlt_sata_pkt->satapkt_reason);
4924 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
4925 	}
4926 	return (TRAN_ACCEPT);
4927 }
4928 
4929 
4930 /*
4931  * Implements SCSI SBC WRITE BUFFER command download microcode option
4932  */
4933 static int
4934 sata_txlt_write_buffer(sata_pkt_txlate_t *spx)
4935 {
4936 #define	WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE			4
4937 #define	WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE			5
4938 
4939 	sata_hba_inst_t *sata_hba_inst = SATA_TXLT_HBA_INST(spx);
4940 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4941 	struct sata_pkt *sata_pkt = spx->txlt_sata_pkt;
4942 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4943 
4944 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4945 	struct scsi_extended_sense *sense;
4946 	int rval, mode, sector_count, reason;
4947 	int cport = SATA_TXLT_CPORT(spx);
4948 
4949 	mode = scsipkt->pkt_cdbp[1] & 0x1f;
4950 
4951 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4952 	    "sata_txlt_write_buffer, mode 0x%x\n", mode);
4953 
4954 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4955 
4956 	if ((rval = sata_txlt_generic_pkt_info(spx, &reason)) != TRAN_ACCEPT) {
4957 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4958 		return (rval);
4959 	}
4960 	/*
4961 	 * If in interrupt context, reject this packet because it would issue
4962 	 * a synchronous command to HBA.
4963 	 */
4964 	if (servicing_interrupt()) {
4965 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
4966 		    "sata_txlt_write_buffer: rejecting command because "
4967 		    "of interrupt context\n", NULL);
4968 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4969 		return (TRAN_BUSY);
4970 	}
4971 
4972 	/* Use synchronous mode */
4973 	spx->txlt_sata_pkt->satapkt_op_mode
4974 	    |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
4975 
4976 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4977 
4978 	scsipkt->pkt_reason = CMD_CMPLT;
4979 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4980 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4981 
4982 	/*
4983 	 * The SCSI to ATA translation specification only calls
4984 	 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE.
4985 	 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but
4986 	 * ATA 8 (draft) got rid of download microcode for temp
4987 	 * and it is even optional for ATA 7, so it may be aborted.
4988 	 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as
4989 	 * it is not specified and the buffer offset for SCSI is a 16-bit
4990 	 * value in bytes, but for ATA it is a 16-bit offset in 512 byte
4991 	 * sectors.  Thus the offset really doesn't buy us anything.
4992 	 * If and when ATA 8 is stabilized and the SCSI to ATA specification
4993 	 * is revised, this can be revisisted.
4994 	 */
4995 	/* Reject not supported request */
4996 	switch (mode) {
4997 	case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE:
4998 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP;
4999 		break;
5000 	case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE:
5001 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE;
5002 		break;
5003 	default:
5004 		goto bad_param;
5005 	}
5006 
5007 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
5008 
5009 	scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE;
5010 	if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0)
5011 		goto bad_param;
5012 	sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE;
5013 	scmd->satacmd_sec_count_lsb = (uint8_t)sector_count;
5014 	scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8;
5015 	scmd->satacmd_lba_mid_lsb = 0;
5016 	scmd->satacmd_lba_high_lsb = 0;
5017 	scmd->satacmd_device_reg = 0;
5018 	spx->txlt_sata_pkt->satapkt_comp = NULL;
5019 	scmd->satacmd_addr_type = 0;
5020 
5021 	/* Transfer command to HBA */
5022 	if (sata_hba_start(spx, &rval) != 0) {
5023 		/* Pkt not accepted for execution */
5024 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
5025 		return (rval);
5026 	}
5027 
5028 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
5029 
5030 	/* Then we need synchronous check the status of the disk */
5031 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5032 	    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
5033 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5034 		scsipkt->pkt_reason = CMD_CMPLT;
5035 
5036 		/* Download commmand succeed, so probe and identify device */
5037 		sata_reidentify_device(spx);
5038 	} else {
5039 		/* Something went wrong, microcode download command failed */
5040 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5041 		*scsipkt->pkt_scbp = STATUS_CHECK;
5042 		sense = sata_arq_sense(spx);
5043 		switch (sata_pkt->satapkt_reason) {
5044 		case SATA_PKT_PORT_ERROR:
5045 			/*
5046 			 * We have no device data. Assume no data transfered.
5047 			 */
5048 			sense->es_key = KEY_HARDWARE_ERROR;
5049 			break;
5050 
5051 		case SATA_PKT_DEV_ERROR:
5052 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5053 			    SATA_STATUS_ERR) {
5054 				/*
5055 				 * determine dev error reason from error
5056 				 * reg content
5057 				 */
5058 				sata_decode_device_error(spx, sense);
5059 				break;
5060 			}
5061 			/* No extended sense key - no info available */
5062 			break;
5063 
5064 		case SATA_PKT_TIMEOUT:
5065 			scsipkt->pkt_reason = CMD_TIMEOUT;
5066 			scsipkt->pkt_statistics |=
5067 			    STAT_TIMEOUT | STAT_DEV_RESET;
5068 			/* No extended sense key ? */
5069 			break;
5070 
5071 		case SATA_PKT_ABORTED:
5072 			scsipkt->pkt_reason = CMD_ABORTED;
5073 			scsipkt->pkt_statistics |= STAT_ABORTED;
5074 			/* No extended sense key ? */
5075 			break;
5076 
5077 		case SATA_PKT_RESET:
5078 			/* pkt aborted by an explicit reset from a host */
5079 			scsipkt->pkt_reason = CMD_RESET;
5080 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5081 			break;
5082 
5083 		default:
5084 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5085 			    "sata_txlt_nodata_cmd_completion: "
5086 			    "invalid packet completion reason %d",
5087 			    sata_pkt->satapkt_reason));
5088 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5089 			break;
5090 		}
5091 
5092 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5093 		    "scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5094 
5095 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5096 		    scsipkt->pkt_comp != NULL)
5097 			/* scsi callback required */
5098 			(*scsipkt->pkt_comp)(scsipkt);
5099 	}
5100 	return (TRAN_ACCEPT);
5101 
5102 bad_param:
5103 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5104 	*scsipkt->pkt_scbp = STATUS_CHECK;
5105 	sense = sata_arq_sense(spx);
5106 	sense->es_key = KEY_ILLEGAL_REQUEST;
5107 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5108 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5109 	    scsipkt->pkt_comp != NULL) {
5110 		/* scsi callback required */
5111 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5112 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5113 		    TQ_SLEEP) == 0) {
5114 			/* Scheduling the callback failed */
5115 			rval = TRAN_BUSY;
5116 		}
5117 	}
5118 	return (rval);
5119 }
5120 
5121 /*
5122  * Re-identify device after doing a firmware download.
5123  */
5124 static void
5125 sata_reidentify_device(sata_pkt_txlate_t *spx)
5126 {
5127 #define	DOWNLOAD_WAIT_TIME_SECS	60
5128 #define	DOWNLOAD_WAIT_INTERVAL_SECS	1
5129 	int rval;
5130 	int retry_cnt;
5131 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5132 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
5133 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
5134 	sata_drive_info_t *sdinfo;
5135 
5136 	/*
5137 	 * Before returning good status, probe device.
5138 	 * Device probing will get IDENTIFY DEVICE data, if possible.
5139 	 * The assumption is that the new microcode is applied by the
5140 	 * device. It is a caller responsibility to verify this.
5141 	 */
5142 	for (retry_cnt = 0;
5143 	    retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS;
5144 	    retry_cnt++) {
5145 		rval = sata_probe_device(sata_hba_inst, &sata_device);
5146 
5147 		if (rval == SATA_SUCCESS) { /* Set default features */
5148 			sdinfo = sata_get_device_info(sata_hba_inst,
5149 			    &sata_device);
5150 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
5151 			    SATA_SUCCESS) {
5152 				/* retry */
5153 				rval = sata_initialize_device(sata_hba_inst,
5154 				    sdinfo);
5155 				if (rval == SATA_RETRY)
5156 					sata_log(sata_hba_inst, CE_WARN,
5157 					    "SATA device at port %d pmport %d -"
5158 					    " default device features could not"
5159 					    " be set. Device may not operate "
5160 					    "as expected.",
5161 					    sata_device.satadev_addr.cport,
5162 					    sata_device.satadev_addr.pmport);
5163 			}
5164 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5165 			    scsipkt->pkt_comp != NULL)
5166 				(*scsipkt->pkt_comp)(scsipkt);
5167 			return;
5168 		} else if (rval == SATA_RETRY) {
5169 			delay(drv_usectohz(1000000 *
5170 			    DOWNLOAD_WAIT_INTERVAL_SECS));
5171 			continue;
5172 		} else	/* failed - no reason to retry */
5173 			break;
5174 	}
5175 
5176 	/*
5177 	 * Something went wrong, device probing failed.
5178 	 */
5179 	SATA_LOG_D((sata_hba_inst, CE_WARN,
5180 	    "Cannot probe device after downloading microcode\n"));
5181 
5182 	/* Reset device to force retrying the probe. */
5183 	(void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst))
5184 	    (SATA_DIP(sata_hba_inst), &sata_device);
5185 
5186 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5187 	    scsipkt->pkt_comp != NULL)
5188 		(*scsipkt->pkt_comp)(scsipkt);
5189 }
5190 
5191 
5192 /*
5193  * Translate command: Synchronize Cache.
5194  * Translates into Flush Cache command for SATA hard disks.
5195  *
5196  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
5197  * appropriate values in scsi_pkt fields.
5198  */
5199 static 	int
5200 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx)
5201 {
5202 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5203 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
5204 	int cport = SATA_TXLT_CPORT(spx);
5205 	int rval, reason;
5206 	int synch;
5207 
5208 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5209 
5210 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
5211 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5212 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5213 		return (rval);
5214 	}
5215 
5216 	scmd->satacmd_addr_type = 0;
5217 	scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE;
5218 	scmd->satacmd_device_reg = 0;
5219 	scmd->satacmd_sec_count_lsb = 0;
5220 	scmd->satacmd_lba_low_lsb = 0;
5221 	scmd->satacmd_lba_mid_lsb = 0;
5222 	scmd->satacmd_lba_high_lsb = 0;
5223 	scmd->satacmd_features_reg = 0;
5224 	scmd->satacmd_status_reg = 0;
5225 	scmd->satacmd_error_reg = 0;
5226 
5227 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5228 	    "sata_txlt_synchronize_cache\n", NULL);
5229 
5230 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5231 		/* Need to set-up a callback function */
5232 		spx->txlt_sata_pkt->satapkt_comp =
5233 		    sata_txlt_nodata_cmd_completion;
5234 		synch = FALSE;
5235 	} else
5236 		synch = TRUE;
5237 
5238 	/* Transfer command to HBA */
5239 	if (sata_hba_start(spx, &rval) != 0) {
5240 		/* Pkt not accepted for execution */
5241 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5242 		return (rval);
5243 	}
5244 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5245 
5246 	/*
5247 	 * If execution non-synchronous, it had to be completed
5248 	 * a callback function will handle potential errors, translate
5249 	 * the response and will do a callback to a target driver.
5250 	 * If it was synchronous, check status, using the same
5251 	 * framework callback.
5252 	 */
5253 	if (synch) {
5254 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5255 		    "synchronous execution status %x\n",
5256 		    spx->txlt_sata_pkt->satapkt_reason);
5257 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
5258 	}
5259 	return (TRAN_ACCEPT);
5260 }
5261 
5262 
5263 /*
5264  * Send pkt to SATA HBA driver
5265  *
5266  * This function may be called only if the operation is requested by scsi_pkt,
5267  * i.e. scsi_pkt is not NULL.
5268  *
5269  * This function has to be called with cport mutex held. It does release
5270  * the mutex when it calls HBA driver sata_tran_start function and
5271  * re-acquires it afterwards.
5272  *
5273  * If return value is 0, pkt was accepted, -1 otherwise
5274  * rval is set to appropriate sata_scsi_start return value.
5275  *
5276  * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not
5277  * have called the sata_pkt callback function for this packet.
5278  *
5279  * The scsi callback has to be performed by the caller of this routine.
5280  *
5281  * Note 2: No port multiplier support for now.
5282  */
5283 static int
5284 sata_hba_start(sata_pkt_txlate_t *spx, int *rval)
5285 {
5286 	int stat, cport;
5287 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
5288 	sata_drive_info_t *sdinfo;
5289 	sata_device_t *sata_device;
5290 	uint8_t cmd;
5291 	struct sata_cmd_flags cmd_flags;
5292 
5293 	ASSERT(spx->txlt_sata_pkt != NULL);
5294 
5295 	cport = SATA_TXLT_CPORT(spx);
5296 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5297 
5298 	sdinfo = sata_get_device_info(sata_hba_inst,
5299 	    &spx->txlt_sata_pkt->satapkt_device);
5300 	ASSERT(sdinfo != NULL);
5301 
5302 	/* Clear device reset state? */
5303 	if (sdinfo->satadrv_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) {
5304 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
5305 		    sata_clear_dev_reset = B_TRUE;
5306 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_CLEAR_DEVICE_RESET;
5307 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5308 		    "sata_hba_start: clearing device reset state\n", NULL);
5309 	}
5310 	cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg;
5311 	cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags;
5312 	sata_device = &spx->txlt_sata_pkt->satapkt_device;
5313 
5314 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5315 
5316 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5317 	    "Sata cmd 0x%2x\n", cmd);
5318 
5319 	stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
5320 	    spx->txlt_sata_pkt);
5321 
5322 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5323 	/*
5324 	 * If sata pkt was accepted and executed in asynchronous mode, i.e.
5325 	 * with the sata callback, the sata_pkt could be already destroyed
5326 	 * by the time we check ther return status from the hba_start()
5327 	 * function, because sata_scsi_destroy_pkt() could have been already
5328 	 * called (perhaps in the interrupt context). So, in such case, there
5329 	 * should be no references to it. In other cases, sata_pkt still
5330 	 * exists.
5331 	 */
5332 	if (stat == SATA_TRAN_ACCEPTED) {
5333 		/*
5334 		 * pkt accepted for execution.
5335 		 * If it was executed synchronously, it is already completed
5336 		 * and pkt completion_reason indicates completion status.
5337 		 */
5338 		*rval = TRAN_ACCEPT;
5339 		return (0);
5340 	}
5341 
5342 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
5343 	switch (stat) {
5344 	case SATA_TRAN_QUEUE_FULL:
5345 		/*
5346 		 * Controller detected queue full condition.
5347 		 */
5348 		SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst,
5349 		    "sata_hba_start: queue full\n", NULL);
5350 
5351 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
5352 		*spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL;
5353 
5354 		*rval = TRAN_BUSY;
5355 		break;
5356 
5357 	case SATA_TRAN_PORT_ERROR:
5358 		/*
5359 		 * Communication/link with device or general port error
5360 		 * detected before pkt execution begun.
5361 		 */
5362 		if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
5363 		    SATA_ADDR_CPORT ||
5364 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
5365 		    SATA_ADDR_DCPORT)
5366 			sata_log(sata_hba_inst, CE_CONT,
5367 			    "SATA port %d error",
5368 			    sata_device->satadev_addr.cport);
5369 		else
5370 			sata_log(sata_hba_inst, CE_CONT,
5371 			    "SATA port %d pmport %d error\n",
5372 			    sata_device->satadev_addr.cport,
5373 			    sata_device->satadev_addr.pmport);
5374 
5375 		/*
5376 		 * Update the port/device structure.
5377 		 * sata_pkt should be still valid. Since port error is
5378 		 * returned, sata_device content should reflect port
5379 		 * state - it means, that sata address have been changed,
5380 		 * because original packet's sata address refered to a device
5381 		 * attached to some port.
5382 		 */
5383 		sata_update_port_info(sata_hba_inst, sata_device);
5384 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
5385 		*rval = TRAN_FATAL_ERROR;
5386 		break;
5387 
5388 	case SATA_TRAN_CMD_UNSUPPORTED:
5389 		/*
5390 		 * Command rejected by HBA as unsupported. It was HBA driver
5391 		 * that rejected the command, command was not sent to
5392 		 * an attached device.
5393 		 */
5394 		if ((sdinfo != NULL) &&
5395 		    (sdinfo->satadrv_state & SATA_DSTATE_RESET))
5396 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5397 			    "sat_hba_start: cmd 0x%2x rejected "
5398 			    "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd);
5399 
5400 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5401 		(void) sata_txlt_invalid_command(spx);
5402 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5403 
5404 		*rval = TRAN_ACCEPT;
5405 		break;
5406 
5407 	case SATA_TRAN_BUSY:
5408 		/*
5409 		 * Command rejected by HBA because other operation prevents
5410 		 * accepting the packet, or device is in RESET condition.
5411 		 */
5412 		if (sdinfo != NULL) {
5413 			sdinfo->satadrv_state =
5414 			    spx->txlt_sata_pkt->satapkt_device.satadev_state;
5415 
5416 			if (sdinfo->satadrv_state & SATA_DSTATE_RESET) {
5417 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5418 				    "sata_hba_start: cmd 0x%2x rejected "
5419 				    "because of device reset condition\n",
5420 				    cmd);
5421 			} else {
5422 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5423 				    "sata_hba_start: cmd 0x%2x rejected "
5424 				    "with SATA_TRAN_BUSY status\n",
5425 				    cmd);
5426 			}
5427 		}
5428 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
5429 		*rval = TRAN_BUSY;
5430 		break;
5431 
5432 	default:
5433 		/* Unrecognized HBA response */
5434 		SATA_LOG_D((sata_hba_inst, CE_WARN,
5435 		    "sata_hba_start: unrecognized HBA response "
5436 		    "to cmd : 0x%2x resp 0x%x", cmd, rval));
5437 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
5438 		*rval = TRAN_FATAL_ERROR;
5439 		break;
5440 	}
5441 
5442 	/*
5443 	 * If we got here, the packet was rejected.
5444 	 * Check if we need to remember reset state clearing request
5445 	 */
5446 	if (cmd_flags.sata_clear_dev_reset) {
5447 		/*
5448 		 * Check if device is still configured - it may have
5449 		 * disapeared from the configuration
5450 		 */
5451 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
5452 		if (sdinfo != NULL) {
5453 			/*
5454 			 * Restore the flag that requests clearing of
5455 			 * the device reset state,
5456 			 * so the next sata packet may carry it to HBA.
5457 			 */
5458 			sdinfo->satadrv_event_flags |=
5459 			    SATA_EVNT_CLEAR_DEVICE_RESET;
5460 		}
5461 	}
5462 	return (-1);
5463 }
5464 
5465 /*
5466  * Scsi response setup for invalid LBA
5467  *
5468  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
5469  */
5470 static int
5471 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx)
5472 {
5473 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5474 	struct scsi_extended_sense *sense;
5475 
5476 	scsipkt->pkt_reason = CMD_CMPLT;
5477 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5478 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5479 	*scsipkt->pkt_scbp = STATUS_CHECK;
5480 
5481 	*scsipkt->pkt_scbp = STATUS_CHECK;
5482 	sense = sata_arq_sense(spx);
5483 	sense->es_key = KEY_ILLEGAL_REQUEST;
5484 	sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
5485 
5486 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5487 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5488 
5489 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5490 	    scsipkt->pkt_comp != NULL)
5491 		/* scsi callback required */
5492 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5493 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5494 		    TQ_SLEEP) == NULL)
5495 			/* Scheduling the callback failed */
5496 			return (TRAN_BUSY);
5497 	return (TRAN_ACCEPT);
5498 }
5499 
5500 
5501 /*
5502  * Analyze device status and error registers and translate them into
5503  * appropriate scsi sense codes.
5504  * NOTE: non-packet commands only for now
5505  */
5506 static void
5507 sata_decode_device_error(sata_pkt_txlate_t *spx,
5508     struct scsi_extended_sense *sense)
5509 {
5510 	uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg;
5511 
5512 	ASSERT(sense != NULL);
5513 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
5514 	    SATA_STATUS_ERR);
5515 
5516 
5517 	if (err_reg & SATA_ERROR_ICRC) {
5518 		sense->es_key = KEY_ABORTED_COMMAND;
5519 		sense->es_add_code = 0x08; /* Communication failure */
5520 		return;
5521 	}
5522 
5523 	if (err_reg & SATA_ERROR_UNC) {
5524 		sense->es_key = KEY_MEDIUM_ERROR;
5525 		/* Information bytes (LBA) need to be set by a caller */
5526 		return;
5527 	}
5528 
5529 	/* ADD HERE: MC error bit handling for ATAPI CD/DVD */
5530 	if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) {
5531 		sense->es_key = KEY_UNIT_ATTENTION;
5532 		sense->es_add_code = 0x3a; /* No media present */
5533 		return;
5534 	}
5535 
5536 	if (err_reg & SATA_ERROR_IDNF) {
5537 		if (err_reg & SATA_ERROR_ABORT) {
5538 			sense->es_key = KEY_ABORTED_COMMAND;
5539 		} else {
5540 			sense->es_key = KEY_ILLEGAL_REQUEST;
5541 			sense->es_add_code = 0x21; /* LBA out of range */
5542 		}
5543 		return;
5544 	}
5545 
5546 	if (err_reg & SATA_ERROR_ABORT) {
5547 		ASSERT(spx->txlt_sata_pkt != NULL);
5548 		sense->es_key = KEY_ABORTED_COMMAND;
5549 		return;
5550 	}
5551 }
5552 
5553 /*
5554  * Extract error LBA from sata_pkt.satapkt_cmd register fields
5555  */
5556 static void
5557 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba)
5558 {
5559 	sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd;
5560 
5561 	*lba = 0;
5562 	if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) {
5563 		*lba = sata_cmd->satacmd_lba_high_msb;
5564 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb;
5565 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb;
5566 	} else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) {
5567 		*lba = sata_cmd->satacmd_device_reg & 0xf;
5568 	}
5569 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb;
5570 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb;
5571 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb;
5572 }
5573 
5574 /*
5575  * This is fixed sense format - if LBA exceeds the info field size,
5576  * no valid info will be returned (valid bit in extended sense will
5577  * be set to 0).
5578  */
5579 static struct scsi_extended_sense *
5580 sata_arq_sense(sata_pkt_txlate_t *spx)
5581 {
5582 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5583 	struct scsi_arq_status *arqs;
5584 	struct scsi_extended_sense *sense;
5585 
5586 	/* Fill ARQ sense data */
5587 	scsipkt->pkt_state |= STATE_ARQ_DONE;
5588 	arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp;
5589 	*(uchar_t *)&arqs->sts_status = STATUS_CHECK;
5590 	*(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD;
5591 	arqs->sts_rqpkt_reason = CMD_CMPLT;
5592 	arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5593 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
5594 	arqs->sts_rqpkt_resid = 0;
5595 	sense = &arqs->sts_sensedata;
5596 	bzero(sense, sizeof (struct scsi_extended_sense));
5597 	sata_fixed_sense_data_preset(sense);
5598 	return (sense);
5599 }
5600 
5601 
5602 /*
5603  * Emulated SATA Read/Write command completion for zero-length requests.
5604  * This request always succedes, so in synchronous mode it always returns
5605  * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the
5606  * callback cannot be scheduled.
5607  */
5608 static int
5609 sata_emul_rw_completion(sata_pkt_txlate_t *spx)
5610 {
5611 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5612 
5613 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5614 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5615 	scsipkt->pkt_reason = CMD_CMPLT;
5616 	*scsipkt->pkt_scbp = STATUS_GOOD;
5617 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5618 		/* scsi callback required - have to schedule it */
5619 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5620 		    (task_func_t *)scsipkt->pkt_comp,
5621 		    (void *)scsipkt, TQ_SLEEP) == NULL)
5622 			/* Scheduling the callback failed */
5623 			return (TRAN_BUSY);
5624 	}
5625 	return (TRAN_ACCEPT);
5626 }
5627 
5628 
5629 /*
5630  * Translate completion status of SATA read/write commands into scsi response.
5631  * pkt completion_reason is checked to determine the completion status.
5632  * Do scsi callback if necessary.
5633  *
5634  * Note: this function may be called also for synchronously executed
5635  * commands.
5636  * This function may be used only if scsi_pkt is non-NULL.
5637  */
5638 static void
5639 sata_txlt_rw_completion(sata_pkt_t *sata_pkt)
5640 {
5641 	sata_pkt_txlate_t *spx =
5642 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
5643 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
5644 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5645 	struct scsi_extended_sense *sense;
5646 	uint64_t lba;
5647 	struct buf *bp;
5648 	int rval;
5649 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5650 		/* Normal completion */
5651 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5652 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
5653 		scsipkt->pkt_reason = CMD_CMPLT;
5654 		*scsipkt->pkt_scbp = STATUS_GOOD;
5655 		if (spx->txlt_tmp_buf != NULL) {
5656 			/* Temporary buffer was used */
5657 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5658 			if (bp->b_flags & B_READ) {
5659 				rval = ddi_dma_sync(
5660 				    spx->txlt_buf_dma_handle, 0, 0,
5661 				    DDI_DMA_SYNC_FORCPU);
5662 				ASSERT(rval == DDI_SUCCESS);
5663 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
5664 				    bp->b_bcount);
5665 			}
5666 		}
5667 	} else {
5668 		/*
5669 		 * Something went wrong - analyze return
5670 		 */
5671 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5672 		    STATE_SENT_CMD | STATE_GOT_STATUS;
5673 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5674 		*scsipkt->pkt_scbp = STATUS_CHECK;
5675 		sense = sata_arq_sense(spx);
5676 		ASSERT(sense != NULL);
5677 
5678 		/*
5679 		 * SATA_PKT_DEV_ERROR is the only case where we may be able to
5680 		 * extract from device registers the failing LBA.
5681 		 */
5682 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
5683 			if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) &&
5684 			    (scmd->satacmd_lba_mid_msb != 0 ||
5685 			    scmd->satacmd_lba_high_msb != 0)) {
5686 				/*
5687 				 * We have problem reporting this cmd LBA
5688 				 * in fixed sense data format, because of
5689 				 * the size of the scsi LBA fields.
5690 				 */
5691 				sense->es_valid = 0;
5692 			} else {
5693 				sata_extract_error_lba(spx, &lba);
5694 				sense->es_info_1 = (lba & 0xFF000000) >> 24;
5695 				sense->es_info_2 = (lba & 0xFF0000) >> 16;
5696 				sense->es_info_3 = (lba & 0xFF00) >> 8;
5697 				sense->es_info_4 = lba & 0xFF;
5698 			}
5699 		} else {
5700 			/* Invalid extended sense info */
5701 			sense->es_valid = 0;
5702 		}
5703 
5704 		switch (sata_pkt->satapkt_reason) {
5705 		case SATA_PKT_PORT_ERROR:
5706 			/* We may want to handle DEV GONE state as well */
5707 			/*
5708 			 * We have no device data. Assume no data transfered.
5709 			 */
5710 			sense->es_key = KEY_HARDWARE_ERROR;
5711 			break;
5712 
5713 		case SATA_PKT_DEV_ERROR:
5714 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5715 			    SATA_STATUS_ERR) {
5716 				/*
5717 				 * determine dev error reason from error
5718 				 * reg content
5719 				 */
5720 				sata_decode_device_error(spx, sense);
5721 				if (sense->es_key == KEY_MEDIUM_ERROR) {
5722 					switch (scmd->satacmd_cmd_reg) {
5723 					case SATAC_READ_DMA:
5724 					case SATAC_READ_DMA_EXT:
5725 					case SATAC_READ_DMA_QUEUED:
5726 					case SATAC_READ_DMA_QUEUED_EXT:
5727 					case SATAC_READ_FPDMA_QUEUED:
5728 						/* Unrecovered read error */
5729 						sense->es_add_code =
5730 						    SD_SCSI_ASC_UNREC_READ_ERR;
5731 						break;
5732 					case SATAC_WRITE_DMA:
5733 					case SATAC_WRITE_DMA_EXT:
5734 					case SATAC_WRITE_DMA_QUEUED:
5735 					case SATAC_WRITE_DMA_QUEUED_EXT:
5736 					case SATAC_WRITE_FPDMA_QUEUED:
5737 						/* Write error */
5738 						sense->es_add_code =
5739 						    SD_SCSI_ASC_WRITE_ERR;
5740 						break;
5741 					default:
5742 						/* Internal error */
5743 						SATA_LOG_D((
5744 						    spx->txlt_sata_hba_inst,
5745 						    CE_WARN,
5746 						    "sata_txlt_rw_completion :"
5747 						    "internal error - invalid "
5748 						    "command 0x%2x",
5749 						    scmd->satacmd_cmd_reg));
5750 						break;
5751 					}
5752 				}
5753 				break;
5754 			}
5755 			/* No extended sense key - no info available */
5756 			scsipkt->pkt_reason = CMD_INCOMPLETE;
5757 			break;
5758 
5759 		case SATA_PKT_TIMEOUT:
5760 			scsipkt->pkt_reason = CMD_TIMEOUT;
5761 			scsipkt->pkt_statistics |=
5762 			    STAT_TIMEOUT | STAT_DEV_RESET;
5763 			sense->es_key = KEY_ABORTED_COMMAND;
5764 			break;
5765 
5766 		case SATA_PKT_ABORTED:
5767 			scsipkt->pkt_reason = CMD_ABORTED;
5768 			scsipkt->pkt_statistics |= STAT_ABORTED;
5769 			sense->es_key = KEY_ABORTED_COMMAND;
5770 			break;
5771 
5772 		case SATA_PKT_RESET:
5773 			scsipkt->pkt_reason = CMD_RESET;
5774 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5775 			sense->es_key = KEY_ABORTED_COMMAND;
5776 			break;
5777 
5778 		default:
5779 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5780 			    "sata_txlt_rw_completion: "
5781 			    "invalid packet completion reason"));
5782 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5783 			break;
5784 		}
5785 	}
5786 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5787 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5788 
5789 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5790 	    scsipkt->pkt_comp != NULL)
5791 		/* scsi callback required */
5792 		(*scsipkt->pkt_comp)(scsipkt);
5793 }
5794 
5795 
5796 /*
5797  * Translate completion status of non-data commands (i.e. commands returning
5798  * no data).
5799  * pkt completion_reason is checked to determine the completion status.
5800  * Do scsi callback if necessary (FLAG_NOINTR == 0)
5801  *
5802  * Note: this function may be called also for synchronously executed
5803  * commands.
5804  * This function may be used only if scsi_pkt is non-NULL.
5805  */
5806 
5807 static 	void
5808 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt)
5809 {
5810 	sata_pkt_txlate_t *spx =
5811 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
5812 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5813 	struct scsi_extended_sense *sense;
5814 
5815 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5816 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5817 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5818 		/* Normal completion */
5819 		scsipkt->pkt_reason = CMD_CMPLT;
5820 		*scsipkt->pkt_scbp = STATUS_GOOD;
5821 	} else {
5822 		/* Something went wrong */
5823 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5824 		*scsipkt->pkt_scbp = STATUS_CHECK;
5825 		sense = sata_arq_sense(spx);
5826 		switch (sata_pkt->satapkt_reason) {
5827 		case SATA_PKT_PORT_ERROR:
5828 			/*
5829 			 * We have no device data. Assume no data transfered.
5830 			 */
5831 			sense->es_key = KEY_HARDWARE_ERROR;
5832 			break;
5833 
5834 		case SATA_PKT_DEV_ERROR:
5835 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5836 			    SATA_STATUS_ERR) {
5837 				/*
5838 				 * determine dev error reason from error
5839 				 * reg content
5840 				 */
5841 				sata_decode_device_error(spx, sense);
5842 				break;
5843 			}
5844 			/* No extended sense key - no info available */
5845 			break;
5846 
5847 		case SATA_PKT_TIMEOUT:
5848 			scsipkt->pkt_reason = CMD_TIMEOUT;
5849 			scsipkt->pkt_statistics |=
5850 			    STAT_TIMEOUT | STAT_DEV_RESET;
5851 			/* No extended sense key ? */
5852 			break;
5853 
5854 		case SATA_PKT_ABORTED:
5855 			scsipkt->pkt_reason = CMD_ABORTED;
5856 			scsipkt->pkt_statistics |= STAT_ABORTED;
5857 			/* No extended sense key ? */
5858 			break;
5859 
5860 		case SATA_PKT_RESET:
5861 			/* pkt aborted by an explicit reset from a host */
5862 			scsipkt->pkt_reason = CMD_RESET;
5863 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5864 			break;
5865 
5866 		default:
5867 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5868 			    "sata_txlt_nodata_cmd_completion: "
5869 			    "invalid packet completion reason %d",
5870 			    sata_pkt->satapkt_reason));
5871 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5872 			break;
5873 		}
5874 
5875 	}
5876 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5877 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5878 
5879 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5880 	    scsipkt->pkt_comp != NULL)
5881 		/* scsi callback required */
5882 		(*scsipkt->pkt_comp)(scsipkt);
5883 }
5884 
5885 
5886 /*
5887  * Build Mode sense R/W recovery page
5888  * NOT IMPLEMENTED
5889  */
5890 
5891 static int
5892 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5893 {
5894 #ifndef __lock_lint
5895 	_NOTE(ARGUNUSED(sdinfo))
5896 	_NOTE(ARGUNUSED(pcntrl))
5897 	_NOTE(ARGUNUSED(buf))
5898 #endif
5899 	return (0);
5900 }
5901 
5902 /*
5903  * Build Mode sense caching page  -  scsi-3 implementation.
5904  * Page length distinguishes previous format from scsi-3 format.
5905  * buf must have space for 0x12 bytes.
5906  * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable.
5907  *
5908  */
5909 static int
5910 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5911 {
5912 	struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf;
5913 	sata_id_t *sata_id = &sdinfo->satadrv_id;
5914 
5915 	/*
5916 	 * Most of the fields are set to 0, being not supported and/or disabled
5917 	 */
5918 	bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3);
5919 
5920 	/* Saved paramters not supported */
5921 	if (pcntrl == 3)
5922 		return (0);
5923 	if (pcntrl == 0 || pcntrl == 2) {
5924 		/*
5925 		 * For now treat current and default parameters as same
5926 		 * That may have to change, if target driver will complain
5927 		 */
5928 		page->mode_page.code = MODEPAGE_CACHING;	/* PS = 0 */
5929 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
5930 
5931 		if ((sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) &&
5932 		    !(sata_id->ai_features85 & SATA_LOOK_AHEAD)) {
5933 			page->dra = 1;		/* Read Ahead disabled */
5934 			page->rcd = 1;		/* Read Cache disabled */
5935 		}
5936 		if ((sata_id->ai_cmdset82 & SATA_WRITE_CACHE) &&
5937 		    (sata_id->ai_features85 & SATA_WRITE_CACHE))
5938 			page->wce = 1;		/* Write Cache enabled */
5939 	} else {
5940 		/* Changeable parameters */
5941 		page->mode_page.code = MODEPAGE_CACHING;
5942 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
5943 		if (sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) {
5944 			page->dra = 1;
5945 			page->rcd = 1;
5946 		}
5947 		if (sata_id->ai_cmdset82 & SATA_WRITE_CACHE)
5948 			page->wce = 1;
5949 	}
5950 	return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
5951 	    sizeof (struct mode_page));
5952 }
5953 
5954 /*
5955  * Build Mode sense exception cntrl page
5956  */
5957 static int
5958 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5959 {
5960 	struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf;
5961 	sata_id_t *sata_id = &sdinfo->satadrv_id;
5962 
5963 	/*
5964 	 * Most of the fields are set to 0, being not supported and/or disabled
5965 	 */
5966 	bzero(buf, PAGELENGTH_INFO_EXCPT);
5967 
5968 	page->mode_page.code = MODEPAGE_INFO_EXCPT;
5969 	page->mode_page.length = PAGELENGTH_INFO_EXCPT;
5970 
5971 	/* Indicate that this is page is saveable */
5972 	page->mode_page.ps = 1;
5973 
5974 	/*
5975 	 * We will return the same data for default, current and saved page.
5976 	 * The only changeable bit is dexcpt and that bit is required
5977 	 * by the ATA specification to be preserved across power cycles.
5978 	 */
5979 	if (pcntrl != 1) {
5980 		page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED);
5981 		page->mrie = MRIE_ONLY_ON_REQUEST;
5982 	}
5983 	else
5984 		page->dexcpt = 1;	/* Only changeable parameter */
5985 
5986 	return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_info_excpt_page));
5987 }
5988 
5989 
5990 static int
5991 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5992 {
5993 	struct mode_acoustic_management *page =
5994 	    (struct mode_acoustic_management *)buf;
5995 	sata_id_t *sata_id = &sdinfo->satadrv_id;
5996 
5997 	/*
5998 	 * Most of the fields are set to 0, being not supported and/or disabled
5999 	 */
6000 	bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT);
6001 
6002 	switch (pcntrl) {
6003 	case P_CNTRL_DEFAULT:
6004 		/*  default paramters not supported */
6005 		return (0);
6006 
6007 	case P_CNTRL_CURRENT:
6008 	case P_CNTRL_SAVED:
6009 		/* Saved and current are supported and are identical */
6010 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
6011 		page->mode_page.length =
6012 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
6013 		page->mode_page.ps = 1;
6014 
6015 		/* Word 83 indicates if feature is supported */
6016 		/* If feature is not supported */
6017 		if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) {
6018 			page->acoustic_manag_enable =
6019 			    ACOUSTIC_DISABLED;
6020 		} else {
6021 			page->acoustic_manag_enable =
6022 			    ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT)
6023 			    != 0);
6024 			/* Word 94 inidicates the value */
6025 #ifdef	_LITTLE_ENDIAN
6026 			page->acoustic_manag_level =
6027 			    (uchar_t)sata_id->ai_acoustic;
6028 			page->vendor_recommended_value =
6029 			    sata_id->ai_acoustic >> 8;
6030 #else
6031 			page->acoustic_manag_level =
6032 			    sata_id->ai_acoustic >> 8;
6033 			page->vendor_recommended_value =
6034 			    (uchar_t)sata_id->ai_acoustic;
6035 #endif
6036 		}
6037 		break;
6038 
6039 	case P_CNTRL_CHANGEABLE:
6040 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
6041 		page->mode_page.length =
6042 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
6043 		page->mode_page.ps = 1;
6044 
6045 		/* Word 83 indicates if the feature is supported */
6046 		if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) {
6047 			page->acoustic_manag_enable =
6048 			    ACOUSTIC_ENABLED;
6049 			page->acoustic_manag_level = 0xff;
6050 		}
6051 		break;
6052 	}
6053 	return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6054 	    sizeof (struct mode_page));
6055 }
6056 
6057 
6058 /*
6059  * Build Mode sense power condition page
6060  * NOT IMPLEMENTED.
6061  */
6062 static int
6063 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6064 {
6065 #ifndef __lock_lint
6066 	_NOTE(ARGUNUSED(sdinfo))
6067 	_NOTE(ARGUNUSED(pcntrl))
6068 	_NOTE(ARGUNUSED(buf))
6069 #endif
6070 	return (0);
6071 }
6072 
6073 
6074 /*
6075  * Process mode select caching page 8 (scsi3 format only).
6076  * Read Ahead (same as read cache) and Write Cache may be turned on and off
6077  * if these features are supported by the device. If these features are not
6078  * supported, quietly ignore them.
6079  * This function fails only if the SET FEATURE command sent to
6080  * the device fails. The page format is not varified, assuming that the
6081  * target driver operates correctly - if parameters length is too short,
6082  * we just drop the page.
6083  * Two command may be sent if both Read Cache/Read Ahead and Write Cache
6084  * setting have to be changed.
6085  * SET FEATURE command is executed synchronously, i.e. we wait here until
6086  * it is completed, regardless of the scsi pkt directives.
6087  *
6088  * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e.
6089  * changing DRA will change RCD.
6090  *
6091  * More than one SATA command may be executed to perform operations specified
6092  * by mode select pages. The first error terminates further execution.
6093  * Operations performed successully are not backed-up in such case.
6094  *
6095  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
6096  * If operation resulted in changing device setup, dmod flag should be set to
6097  * one (1). If parameters were not changed, dmod flag should be set to 0.
6098  * Upon return, if operation required sending command to the device, the rval
6099  * should be set to the value returned by sata_hba_start. If operation
6100  * did not require device access, rval should be set to TRAN_ACCEPT.
6101  * The pagelen should be set to the length of the page.
6102  *
6103  * This function has to be called with a port mutex held.
6104  *
6105  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6106  */
6107 int
6108 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page,
6109     int parmlen, int *pagelen, int *rval, int *dmod)
6110 {
6111 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6112 	sata_drive_info_t *sdinfo;
6113 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6114 	sata_id_t *sata_id;
6115 	struct scsi_extended_sense *sense;
6116 	int wce, dra;	/* Current settings */
6117 
6118 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6119 	    &spx->txlt_sata_pkt->satapkt_device);
6120 	sata_id = &sdinfo->satadrv_id;
6121 	*dmod = 0;
6122 
6123 	/* Verify parameters length. If too short, drop it */
6124 	if (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
6125 	    sizeof (struct mode_page) < parmlen) {
6126 		*scsipkt->pkt_scbp = STATUS_CHECK;
6127 		sense = sata_arq_sense(spx);
6128 		sense->es_key = KEY_ILLEGAL_REQUEST;
6129 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6130 		*pagelen = parmlen;
6131 		*rval = TRAN_ACCEPT;
6132 		return (SATA_FAILURE);
6133 	}
6134 
6135 	*pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page);
6136 
6137 	/*
6138 	 * We can manipulate only write cache and read ahead
6139 	 * (read cache) setting.
6140 	 */
6141 	if (!(sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) &&
6142 	    !(sata_id->ai_cmdset82 & SATA_WRITE_CACHE)) {
6143 		/*
6144 		 * None of the features is supported - ignore
6145 		 */
6146 		*rval = TRAN_ACCEPT;
6147 		return (SATA_SUCCESS);
6148 	}
6149 
6150 	/* Current setting of Read Ahead (and Read Cache) */
6151 	if (sata_id->ai_features85 & SATA_LOOK_AHEAD)
6152 		dra = 0;	/* 0 == not disabled */
6153 	else
6154 		dra = 1;
6155 	/* Current setting of Write Cache */
6156 	if (sata_id->ai_features85 & SATA_WRITE_CACHE)
6157 		wce = 1;
6158 	else
6159 		wce = 0;
6160 
6161 	if (page->dra == dra && page->wce == wce && page->rcd == dra) {
6162 		/* nothing to do */
6163 		*rval = TRAN_ACCEPT;
6164 		return (SATA_SUCCESS);
6165 	}
6166 
6167 	/*
6168 	 * Need to flip some setting
6169 	 * Set-up Internal SET FEATURES command(s)
6170 	 */
6171 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6172 	scmd->satacmd_addr_type = 0;
6173 	scmd->satacmd_device_reg = 0;
6174 	scmd->satacmd_status_reg = 0;
6175 	scmd->satacmd_error_reg = 0;
6176 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
6177 	if (page->dra != dra || page->rcd != dra) {
6178 		/* Need to flip read ahead setting */
6179 		if (dra == 0)
6180 			/* Disable read ahead / read cache */
6181 			scmd->satacmd_features_reg =
6182 			    SATAC_SF_DISABLE_READ_AHEAD;
6183 		else
6184 			/* Enable read ahead  / read cache */
6185 			scmd->satacmd_features_reg =
6186 			    SATAC_SF_ENABLE_READ_AHEAD;
6187 
6188 		/* Transfer command to HBA */
6189 		if (sata_hba_start(spx, rval) != 0)
6190 			/*
6191 			 * Pkt not accepted for execution.
6192 			 */
6193 			return (SATA_FAILURE);
6194 
6195 		*dmod = 1;
6196 
6197 		/* Now process return */
6198 		if (spx->txlt_sata_pkt->satapkt_reason !=
6199 		    SATA_PKT_COMPLETED) {
6200 			goto failure;	/* Terminate */
6201 		}
6202 	}
6203 
6204 	/* Note that the packet is not removed, so it could be re-used */
6205 	if (page->wce != wce) {
6206 		/* Need to flip Write Cache setting */
6207 		if (page->wce == 1)
6208 			/* Enable write cache */
6209 			scmd->satacmd_features_reg =
6210 			    SATAC_SF_ENABLE_WRITE_CACHE;
6211 		else
6212 			/* Disable write cache */
6213 			scmd->satacmd_features_reg =
6214 			    SATAC_SF_DISABLE_WRITE_CACHE;
6215 
6216 		/* Transfer command to HBA */
6217 		if (sata_hba_start(spx, rval) != 0)
6218 			/*
6219 			 * Pkt not accepted for execution.
6220 			 */
6221 			return (SATA_FAILURE);
6222 
6223 		*dmod = 1;
6224 
6225 		/* Now process return */
6226 		if (spx->txlt_sata_pkt->satapkt_reason !=
6227 		    SATA_PKT_COMPLETED) {
6228 			goto failure;
6229 		}
6230 	}
6231 	return (SATA_SUCCESS);
6232 
6233 failure:
6234 	sata_xlate_errors(spx);
6235 
6236 	return (SATA_FAILURE);
6237 }
6238 
6239 /*
6240  * Process mode select informational exceptions control page 0x1c
6241  *
6242  * The only changeable bit is dexcpt (disable exceptions).
6243  * MRIE (method of reporting informational exceptions) must be
6244  * "only on request".
6245  * This page applies to informational exceptions that report
6246  * additional sense codes with the ADDITIONAL SENSE CODE field set to 5Dh
6247  * (e.g.,FAILURE PREDICTION THRESHOLD EXCEEDED) or 0Bh (e.g., WARNING_).
6248  * Informational exception conditions occur as the result of background scan
6249  * errors, background self-test errors, or vendor specific events within a
6250  * logical unit. An informational exception condition may occur asynchronous
6251  * to any commands.
6252  *
6253  * Returns: SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
6254  * If operation resulted in changing device setup, dmod flag should be set to
6255  * one (1). If parameters were not changed, dmod flag should be set to 0.
6256  * Upon return, if operation required sending command to the device, the rval
6257  * should be set to the value returned by sata_hba_start. If operation
6258  * did not require device access, rval should be set to TRAN_ACCEPT.
6259  * The pagelen should be set to the length of the page.
6260  *
6261  * This function has to be called with a port mutex held.
6262  *
6263  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6264  *
6265  * Cannot be called in the interrupt context.
6266  */
6267 static	int
6268 sata_mode_select_page_1c(
6269 	sata_pkt_txlate_t *spx,
6270 	struct mode_info_excpt_page *page,
6271 	int parmlen,
6272 	int *pagelen,
6273 	int *rval,
6274 	int *dmod)
6275 {
6276 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6277 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6278 	sata_drive_info_t *sdinfo;
6279 	sata_id_t *sata_id;
6280 	struct scsi_extended_sense *sense;
6281 
6282 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6283 	    &spx->txlt_sata_pkt->satapkt_device);
6284 	sata_id = &sdinfo->satadrv_id;
6285 
6286 	*dmod = 0;
6287 
6288 	/* Verify parameters length. If too short, drop it */
6289 	if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) < parmlen) ||
6290 	    page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) {
6291 		*scsipkt->pkt_scbp = STATUS_CHECK;
6292 		sense = sata_arq_sense(spx);
6293 		sense->es_key = KEY_ILLEGAL_REQUEST;
6294 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6295 		*pagelen = parmlen;
6296 		*rval = TRAN_ACCEPT;
6297 		return (SATA_FAILURE);
6298 	}
6299 
6300 	*pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page);
6301 
6302 	if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
6303 		*scsipkt->pkt_scbp = STATUS_CHECK;
6304 		sense = sata_arq_sense(spx);
6305 		sense->es_key = KEY_ILLEGAL_REQUEST;
6306 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6307 		*pagelen = parmlen;
6308 		*rval = TRAN_ACCEPT;
6309 		return (SATA_FAILURE);
6310 	}
6311 
6312 	/* If already in the state requested, we are done */
6313 	if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
6314 		/* nothing to do */
6315 		*rval = TRAN_ACCEPT;
6316 		return (SATA_SUCCESS);
6317 	}
6318 
6319 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6320 
6321 	/* Build SMART_ENABLE or SMART_DISABLE command */
6322 	scmd->satacmd_addr_type = 0;		/* N/A */
6323 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
6324 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
6325 	scmd->satacmd_features_reg = page->dexcpt ?
6326 	    SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS;
6327 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
6328 	scmd->satacmd_cmd_reg = SATAC_SMART;
6329 
6330 	/* Transfer command to HBA */
6331 	if (sata_hba_start(spx, rval) != 0)
6332 		/*
6333 		 * Pkt not accepted for execution.
6334 		 */
6335 		return (SATA_FAILURE);
6336 
6337 	*dmod = 1;	/* At least may have been modified */
6338 
6339 	/* Now process return */
6340 	if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED)
6341 		return (SATA_SUCCESS);
6342 
6343 	/* Packet did not complete successfully */
6344 	sata_xlate_errors(spx);
6345 
6346 	return (SATA_FAILURE);
6347 }
6348 
6349 /*
6350  * Process mode select acoustic management control page 0x30
6351  *
6352  *
6353  * This function has to be called with a port mutex held.
6354  *
6355  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6356  *
6357  * Cannot be called in the interrupt context.
6358  */
6359 int
6360 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct
6361     mode_acoustic_management *page, int parmlen, int *pagelen,
6362     int *rval, int *dmod)
6363 {
6364 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6365 	sata_drive_info_t *sdinfo;
6366 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6367 	sata_id_t *sata_id;
6368 	struct scsi_extended_sense *sense;
6369 
6370 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6371 	    &spx->txlt_sata_pkt->satapkt_device);
6372 	sata_id = &sdinfo->satadrv_id;
6373 	*dmod = 0;
6374 
6375 	/* If parmlen is too short or the feature is not supported, drop it */
6376 	if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6377 	    sizeof (struct mode_page)) < parmlen) ||
6378 	    (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) {
6379 		*scsipkt->pkt_scbp = STATUS_CHECK;
6380 		sense = sata_arq_sense(spx);
6381 		sense->es_key = KEY_ILLEGAL_REQUEST;
6382 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6383 		*pagelen = parmlen;
6384 		*rval = TRAN_ACCEPT;
6385 		return (SATA_FAILURE);
6386 	}
6387 
6388 	*pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6389 	    sizeof (struct mode_page);
6390 
6391 	/*
6392 	 * We can enable and disable acoustice management and
6393 	 * set the acoustic management level.
6394 	 */
6395 
6396 	/*
6397 	 * Set-up Internal SET FEATURES command(s)
6398 	 */
6399 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6400 	scmd->satacmd_addr_type = 0;
6401 	scmd->satacmd_device_reg = 0;
6402 	scmd->satacmd_status_reg = 0;
6403 	scmd->satacmd_error_reg = 0;
6404 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
6405 	if (page->acoustic_manag_enable) {
6406 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC;
6407 		scmd->satacmd_sec_count_lsb = page->acoustic_manag_level;
6408 	} else {	/* disabling acoustic management */
6409 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC;
6410 	}
6411 
6412 	/* Transfer command to HBA */
6413 	if (sata_hba_start(spx, rval) != 0)
6414 		/*
6415 		 * Pkt not accepted for execution.
6416 		 */
6417 		return (SATA_FAILURE);
6418 
6419 	/* Now process return */
6420 	if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) {
6421 		sata_xlate_errors(spx);
6422 		return (SATA_FAILURE);
6423 	}
6424 
6425 	*dmod = 1;
6426 
6427 	return (SATA_SUCCESS);
6428 }
6429 
6430 
6431 
6432 
6433 /*
6434  * sata_build_lsense_page0() is used to create the
6435  * SCSI LOG SENSE page 0 (supported log pages)
6436  *
6437  * Currently supported pages are 0, 0x10, 0x2f and 0x30
6438  * (supported log pages, self-test results, informational exceptions
6439  *  and Sun vendor specific ATA SMART data).
6440  *
6441  * Takes a sata_drive_info t * and the address of a buffer
6442  * in which to create the page information.
6443  *
6444  * Returns the number of bytes valid in the buffer.
6445  */
6446 static	int
6447 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf)
6448 {
6449 	struct log_parameter *lpp = (struct log_parameter *)buf;
6450 	uint8_t *page_ptr = (uint8_t *)lpp->param_values;
6451 	int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */
6452 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6453 
6454 	lpp->param_code[0] = 0;
6455 	lpp->param_code[1] = 0;
6456 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
6457 	*page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES;
6458 
6459 	if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
6460 		if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) {
6461 			*page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS;
6462 			++num_pages_supported;
6463 		}
6464 		*page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS;
6465 		++num_pages_supported;
6466 		*page_ptr++ = PAGE_CODE_SMART_READ_DATA;
6467 		++num_pages_supported;
6468 	}
6469 
6470 	lpp->param_len = num_pages_supported;
6471 
6472 	return ((&lpp->param_values[0] - (uint8_t *)lpp) +
6473 	    num_pages_supported);
6474 }
6475 
6476 /*
6477  * sata_build_lsense_page_10() is used to create the
6478  * SCSI LOG SENSE page 0x10 (self-test results)
6479  *
6480  * Takes a sata_drive_info t * and the address of a buffer
6481  * in which to create the page information as well as a sata_hba_inst_t *.
6482  *
6483  * Returns the number of bytes valid in the buffer.
6484  *
6485  * Note: Self test and SMART data is accessible in device log pages.
6486  * The log pages can be accessed by SMART READ/WRITE LOG (up to 255 sectors
6487  * of data can be transferred by a single command), or by the General Purpose
6488  * Logging commands (GPL) READ LOG EXT and WRITE LOG EXT (up to 65,535 sectors
6489  * - approximately 33MB - can be transferred by a single command.
6490  * The SCT Command response (either error or command) is the same for both
6491  * the SMART and GPL methods of issuing commands.
6492  * This function uses READ LOG EXT command when drive supports LBA48, and
6493  * SMART READ command otherwise.
6494  *
6495  * Since above commands are executed in a synchronous mode, this function
6496  * should not be called in an interrupt context.
6497  */
6498 static	int
6499 sata_build_lsense_page_10(
6500 	sata_drive_info_t *sdinfo,
6501 	uint8_t *buf,
6502 	sata_hba_inst_t *sata_hba_inst)
6503 {
6504 	struct log_parameter *lpp = (struct log_parameter *)buf;
6505 	int rval;
6506 
6507 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6508 		struct smart_ext_selftest_log *ext_selftest_log;
6509 
6510 		ext_selftest_log = kmem_zalloc(
6511 		    sizeof (struct smart_ext_selftest_log), KM_SLEEP);
6512 
6513 		rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo,
6514 		    ext_selftest_log, 0);
6515 		if (rval == 0) {
6516 			int index, start_index;
6517 			struct smart_ext_selftest_log_entry *entry;
6518 			static const struct smart_ext_selftest_log_entry empty =
6519 			    {0};
6520 			uint16_t block_num;
6521 			int count;
6522 			boolean_t only_one_block = B_FALSE;
6523 
6524 			index = ext_selftest_log->
6525 			    smart_ext_selftest_log_index[0];
6526 			index |= ext_selftest_log->
6527 			    smart_ext_selftest_log_index[1] << 8;
6528 			if (index == 0)
6529 				goto out;
6530 
6531 			--index;	/* Correct for 0 origin */
6532 			start_index = index;	/* remember where we started */
6533 			block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6534 			if (block_num != 0) {
6535 				rval = sata_ext_smart_selftest_read_log(
6536 				    sata_hba_inst, sdinfo, ext_selftest_log,
6537 				    block_num);
6538 				if (rval != 0)
6539 					goto out;
6540 			}
6541 			index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6542 			entry =
6543 			    &ext_selftest_log->
6544 			    smart_ext_selftest_log_entries[index];
6545 
6546 			for (count = 1;
6547 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
6548 			    ++count) {
6549 				uint8_t status;
6550 				uint8_t code;
6551 				uint8_t sense_key;
6552 				uint8_t add_sense_code;
6553 				uint8_t add_sense_code_qual;
6554 
6555 				/* If this is an unused entry, we are done */
6556 				if (bcmp(entry, &empty, sizeof (empty)) == 0) {
6557 					/* Broken firmware on some disks */
6558 					if (index + 1 ==
6559 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK) {
6560 						--entry;
6561 						--index;
6562 						if (bcmp(entry, &empty,
6563 						    sizeof (empty)) == 0)
6564 							goto out;
6565 					} else
6566 						goto out;
6567 				}
6568 
6569 				if (only_one_block &&
6570 				    start_index == index)
6571 					goto out;
6572 
6573 				lpp->param_code[0] = 0;
6574 				lpp->param_code[1] = count;
6575 				lpp->param_ctrl_flags =
6576 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
6577 				lpp->param_len =
6578 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
6579 
6580 				status = entry->smart_ext_selftest_log_status;
6581 				status >>= 4;
6582 				switch (status) {
6583 				case 0:
6584 				default:
6585 					sense_key = KEY_NO_SENSE;
6586 					add_sense_code =
6587 					    SD_SCSI_ASC_NO_ADD_SENSE;
6588 					add_sense_code_qual = 0;
6589 					break;
6590 				case 1:
6591 					sense_key = KEY_ABORTED_COMMAND;
6592 					add_sense_code =
6593 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6594 					add_sense_code_qual = SCSI_COMPONENT_81;
6595 					break;
6596 				case 2:
6597 					sense_key = KEY_ABORTED_COMMAND;
6598 					add_sense_code =
6599 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6600 					add_sense_code_qual = SCSI_COMPONENT_82;
6601 					break;
6602 				case 3:
6603 					sense_key = KEY_ABORTED_COMMAND;
6604 					add_sense_code =
6605 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6606 					add_sense_code_qual = SCSI_COMPONENT_83;
6607 					break;
6608 				case 4:
6609 					sense_key = KEY_HARDWARE_ERROR;
6610 					add_sense_code =
6611 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6612 					add_sense_code_qual = SCSI_COMPONENT_84;
6613 					break;
6614 				case 5:
6615 					sense_key = KEY_HARDWARE_ERROR;
6616 					add_sense_code =
6617 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6618 					add_sense_code_qual = SCSI_COMPONENT_85;
6619 					break;
6620 				case 6:
6621 					sense_key = KEY_HARDWARE_ERROR;
6622 					add_sense_code =
6623 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6624 					add_sense_code_qual = SCSI_COMPONENT_86;
6625 					break;
6626 				case 7:
6627 					sense_key = KEY_MEDIUM_ERROR;
6628 					add_sense_code =
6629 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6630 					add_sense_code_qual = SCSI_COMPONENT_87;
6631 					break;
6632 				case 8:
6633 					sense_key = KEY_HARDWARE_ERROR;
6634 					add_sense_code =
6635 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6636 					add_sense_code_qual = SCSI_COMPONENT_88;
6637 					break;
6638 				}
6639 				code = 0;	/* unspecified */
6640 				status |= (code << 4);
6641 				lpp->param_values[0] = status;
6642 				lpp->param_values[1] = 0; /* unspecified */
6643 				lpp->param_values[2] = entry->
6644 				    smart_ext_selftest_log_timestamp[1];
6645 				lpp->param_values[3] = entry->
6646 				    smart_ext_selftest_log_timestamp[0];
6647 				if (status != 0) {
6648 					lpp->param_values[4] = 0;
6649 					lpp->param_values[5] = 0;
6650 					lpp->param_values[6] = entry->
6651 					    smart_ext_selftest_log_failing_lba
6652 					    [5];
6653 					lpp->param_values[7] = entry->
6654 					    smart_ext_selftest_log_failing_lba
6655 					    [4];
6656 					lpp->param_values[8] = entry->
6657 					    smart_ext_selftest_log_failing_lba
6658 					    [3];
6659 					lpp->param_values[9] = entry->
6660 					    smart_ext_selftest_log_failing_lba
6661 					    [2];
6662 					lpp->param_values[10] = entry->
6663 					    smart_ext_selftest_log_failing_lba
6664 					    [1];
6665 					lpp->param_values[11] = entry->
6666 					    smart_ext_selftest_log_failing_lba
6667 					    [0];
6668 				} else {	/* No bad block address */
6669 					lpp->param_values[4] = 0xff;
6670 					lpp->param_values[5] = 0xff;
6671 					lpp->param_values[6] = 0xff;
6672 					lpp->param_values[7] = 0xff;
6673 					lpp->param_values[8] = 0xff;
6674 					lpp->param_values[9] = 0xff;
6675 					lpp->param_values[10] = 0xff;
6676 					lpp->param_values[11] = 0xff;
6677 				}
6678 
6679 				lpp->param_values[12] = sense_key;
6680 				lpp->param_values[13] = add_sense_code;
6681 				lpp->param_values[14] = add_sense_code_qual;
6682 				lpp->param_values[15] = 0; /* undefined */
6683 
6684 				lpp = (struct log_parameter *)
6685 				    (((uint8_t *)lpp) +
6686 				    SCSI_LOG_PARAM_HDR_LEN +
6687 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
6688 
6689 				--index;	/* Back up to previous entry */
6690 				if (index < 0) {
6691 					if (block_num > 0) {
6692 						--block_num;
6693 					} else {
6694 						struct read_log_ext_directory
6695 						    logdir;
6696 
6697 						rval =
6698 						    sata_read_log_ext_directory(
6699 						    sata_hba_inst, sdinfo,
6700 						    &logdir);
6701 						if (rval == -1)
6702 							goto out;
6703 						if ((logdir.read_log_ext_vers
6704 						    [0] == 0) &&
6705 						    (logdir.read_log_ext_vers
6706 						    [1] == 0))
6707 							goto out;
6708 						block_num =
6709 						    logdir.read_log_ext_nblks
6710 						    [EXT_SMART_SELFTEST_LOG_PAGE
6711 						    - 1][0];
6712 						block_num |= logdir.
6713 						    read_log_ext_nblks
6714 						    [EXT_SMART_SELFTEST_LOG_PAGE
6715 						    - 1][1] << 8;
6716 						--block_num;
6717 						only_one_block =
6718 						    (block_num == 0);
6719 					}
6720 					rval = sata_ext_smart_selftest_read_log(
6721 					    sata_hba_inst, sdinfo,
6722 					    ext_selftest_log, block_num);
6723 					if (rval != 0)
6724 						goto out;
6725 
6726 					index =
6727 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK -
6728 					    1;
6729 				}
6730 				index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6731 				entry = &ext_selftest_log->
6732 				    smart_ext_selftest_log_entries[index];
6733 			}
6734 		}
6735 out:
6736 		kmem_free(ext_selftest_log,
6737 		    sizeof (struct smart_ext_selftest_log));
6738 	} else {
6739 		struct smart_selftest_log *selftest_log;
6740 
6741 		selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log),
6742 		    KM_SLEEP);
6743 
6744 		rval = sata_smart_selftest_log(sata_hba_inst, sdinfo,
6745 		    selftest_log);
6746 
6747 		if (rval == 0) {
6748 			int index;
6749 			int count;
6750 			struct smart_selftest_log_entry *entry;
6751 			static const struct smart_selftest_log_entry empty =
6752 			    { 0 };
6753 
6754 			index = selftest_log->smart_selftest_log_index;
6755 			if (index == 0)
6756 				goto done;
6757 			--index;	/* Correct for 0 origin */
6758 			entry = &selftest_log->
6759 			    smart_selftest_log_entries[index];
6760 			for (count = 1;
6761 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
6762 			    ++count) {
6763 				uint8_t status;
6764 				uint8_t code;
6765 				uint8_t sense_key;
6766 				uint8_t add_sense_code;
6767 				uint8_t add_sense_code_qual;
6768 
6769 				if (bcmp(entry, &empty, sizeof (empty)) == 0)
6770 					goto done;
6771 
6772 				lpp->param_code[0] = 0;
6773 				lpp->param_code[1] = count;
6774 				lpp->param_ctrl_flags =
6775 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
6776 				lpp->param_len =
6777 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
6778 
6779 				status = entry->smart_selftest_log_status;
6780 				status >>= 4;
6781 				switch (status) {
6782 				case 0:
6783 				default:
6784 					sense_key = KEY_NO_SENSE;
6785 					add_sense_code =
6786 					    SD_SCSI_ASC_NO_ADD_SENSE;
6787 					break;
6788 				case 1:
6789 					sense_key = KEY_ABORTED_COMMAND;
6790 					add_sense_code =
6791 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6792 					add_sense_code_qual = SCSI_COMPONENT_81;
6793 					break;
6794 				case 2:
6795 					sense_key = KEY_ABORTED_COMMAND;
6796 					add_sense_code =
6797 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6798 					add_sense_code_qual = SCSI_COMPONENT_82;
6799 					break;
6800 				case 3:
6801 					sense_key = KEY_ABORTED_COMMAND;
6802 					add_sense_code =
6803 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6804 					add_sense_code_qual = SCSI_COMPONENT_83;
6805 					break;
6806 				case 4:
6807 					sense_key = KEY_HARDWARE_ERROR;
6808 					add_sense_code =
6809 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6810 					add_sense_code_qual = SCSI_COMPONENT_84;
6811 					break;
6812 				case 5:
6813 					sense_key = KEY_HARDWARE_ERROR;
6814 					add_sense_code =
6815 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6816 					add_sense_code_qual = SCSI_COMPONENT_85;
6817 					break;
6818 				case 6:
6819 					sense_key = KEY_HARDWARE_ERROR;
6820 					add_sense_code =
6821 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6822 					add_sense_code_qual = SCSI_COMPONENT_86;
6823 					break;
6824 				case 7:
6825 					sense_key = KEY_MEDIUM_ERROR;
6826 					add_sense_code =
6827 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6828 					add_sense_code_qual = SCSI_COMPONENT_87;
6829 					break;
6830 				case 8:
6831 					sense_key = KEY_HARDWARE_ERROR;
6832 					add_sense_code =
6833 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6834 					add_sense_code_qual = SCSI_COMPONENT_88;
6835 					break;
6836 				}
6837 				code = 0;	/* unspecified */
6838 				status |= (code << 4);
6839 				lpp->param_values[0] = status;
6840 				lpp->param_values[1] = 0; /* unspecified */
6841 				lpp->param_values[2] = entry->
6842 				    smart_selftest_log_timestamp[1];
6843 				lpp->param_values[3] = entry->
6844 				    smart_selftest_log_timestamp[0];
6845 				if (status != 0) {
6846 					lpp->param_values[4] = 0;
6847 					lpp->param_values[5] = 0;
6848 					lpp->param_values[6] = 0;
6849 					lpp->param_values[7] = 0;
6850 					lpp->param_values[8] = entry->
6851 					    smart_selftest_log_failing_lba[3];
6852 					lpp->param_values[9] = entry->
6853 					    smart_selftest_log_failing_lba[2];
6854 					lpp->param_values[10] = entry->
6855 					    smart_selftest_log_failing_lba[1];
6856 					lpp->param_values[11] = entry->
6857 					    smart_selftest_log_failing_lba[0];
6858 				} else {	/* No block address */
6859 					lpp->param_values[4] = 0xff;
6860 					lpp->param_values[5] = 0xff;
6861 					lpp->param_values[6] = 0xff;
6862 					lpp->param_values[7] = 0xff;
6863 					lpp->param_values[8] = 0xff;
6864 					lpp->param_values[9] = 0xff;
6865 					lpp->param_values[10] = 0xff;
6866 					lpp->param_values[11] = 0xff;
6867 				}
6868 				lpp->param_values[12] = sense_key;
6869 				lpp->param_values[13] = add_sense_code;
6870 				lpp->param_values[14] = add_sense_code_qual;
6871 				lpp->param_values[15] = 0; /* undefined */
6872 
6873 				lpp = (struct log_parameter *)
6874 				    (((uint8_t *)lpp) +
6875 				    SCSI_LOG_PARAM_HDR_LEN +
6876 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
6877 				--index;	/* back up to previous entry */
6878 				if (index < 0) {
6879 					index =
6880 					    NUM_SMART_SELFTEST_LOG_ENTRIES - 1;
6881 				}
6882 				entry = &selftest_log->
6883 				    smart_selftest_log_entries[index];
6884 			}
6885 		}
6886 done:
6887 		kmem_free(selftest_log, sizeof (struct smart_selftest_log));
6888 	}
6889 
6890 	return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) *
6891 	    SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS);
6892 }
6893 
6894 /*
6895  * sata_build_lsense_page_2f() is used to create the
6896  * SCSI LOG SENSE page 0x2f (informational exceptions)
6897  *
6898  * Takes a sata_drive_info t * and the address of a buffer
6899  * in which to create the page information as well as a sata_hba_inst_t *.
6900  *
6901  * Returns the number of bytes valid in the buffer.
6902  *
6903  * Because it invokes function(s) that send synchronously executed command
6904  * to the HBA, it cannot be called in the interrupt context.
6905  */
6906 static	int
6907 sata_build_lsense_page_2f(
6908 	sata_drive_info_t *sdinfo,
6909 	uint8_t *buf,
6910 	sata_hba_inst_t *sata_hba_inst)
6911 {
6912 	struct log_parameter *lpp = (struct log_parameter *)buf;
6913 	int rval;
6914 	uint8_t *smart_data;
6915 	uint8_t temp;
6916 	sata_id_t *sata_id;
6917 #define	SMART_NO_TEMP	0xff
6918 
6919 	lpp->param_code[0] = 0;
6920 	lpp->param_code[1] = 0;
6921 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
6922 
6923 	/* Now get the SMART status w.r.t. threshold exceeded */
6924 	rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo);
6925 	switch (rval) {
6926 	case 1:
6927 		lpp->param_values[0] = SCSI_PREDICTED_FAILURE;
6928 		lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE;
6929 		break;
6930 	case 0:
6931 	case -1:	/* failed to get data */
6932 		lpp->param_values[0] = 0;	/* No failure predicted */
6933 		lpp->param_values[1] = 0;
6934 		break;
6935 #if defined(SATA_DEBUG)
6936 	default:
6937 		cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value");
6938 		/* NOTREACHED */
6939 #endif
6940 	}
6941 
6942 	sata_id = &sdinfo->satadrv_id;
6943 	if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP))
6944 		temp = SMART_NO_TEMP;
6945 	else {
6946 		/* Now get the temperature */
6947 		smart_data = kmem_zalloc(512, KM_SLEEP);
6948 		rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data,
6949 		    SCT_STATUS_LOG_PAGE, 1);
6950 		if (rval == -1)
6951 			temp = SMART_NO_TEMP;
6952 		else {
6953 			temp = smart_data[200];
6954 			if (temp & 0x80) {
6955 				if (temp & 0x7f)
6956 					temp = 0;
6957 				else
6958 					temp = SMART_NO_TEMP;
6959 			}
6960 		}
6961 		kmem_free(smart_data, 512);
6962 	}
6963 
6964 	lpp->param_values[2] = temp;	/* most recent temperature */
6965 	lpp->param_values[3] = 0;	/* required vendor specific byte */
6966 
6967 	lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN;
6968 
6969 
6970 	return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN);
6971 }
6972 
6973 /*
6974  * sata_build_lsense_page_30() is used to create the
6975  * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data).
6976  *
6977  * Takes a sata_drive_info t * and the address of a buffer
6978  * in which to create the page information as well as a sata_hba_inst_t *.
6979  *
6980  * Returns the number of bytes valid in the buffer.
6981  */
6982 static int
6983 sata_build_lsense_page_30(
6984 	sata_drive_info_t *sdinfo,
6985 	uint8_t *buf,
6986 	sata_hba_inst_t *sata_hba_inst)
6987 {
6988 	struct smart_data *smart_data = (struct smart_data *)buf;
6989 	int rval;
6990 
6991 	/* Now do the SMART READ DATA */
6992 	rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data);
6993 	if (rval == -1)
6994 		return (0);
6995 
6996 	return (sizeof (struct smart_data));
6997 }
6998 
6999 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */
7000 
7001 /*
7002  * Start command for ATAPI device.
7003  * This function processes scsi_pkt requests.
7004  * Now only CD/DVD and tape devices are supported.
7005  * Most commands are packet without any translation into Packet Command.
7006  * Some may be trapped and executed as SATA commands (not clear which one).
7007  *
7008  * Returns TRAN_ACCEPT if command is accepted for execution (or completed
7009  * execution).
7010  * Returns other TRAN_XXXX codes if command is not accepted or completed
7011  * (see return values for sata_hba_start()).
7012  *
7013  * Note:
7014  * Inquiry cdb format differs between transport version 2 and 3.
7015  * However, the transport version 3 devices that were checked did not adhere
7016  * to the specification (ignored MSB of the allocation length). Therefore,
7017  * the transport version is not checked, but Inquiry allocation length is
7018  * truncated to 255 bytes if the original allocation length set-up by the
7019  * target driver is greater than 255 bytes.
7020  */
7021 static int
7022 sata_txlt_atapi(sata_pkt_txlate_t *spx)
7023 {
7024 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7025 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
7026 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7027 	sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx);
7028 	sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba,
7029 	    &spx->txlt_sata_pkt->satapkt_device);
7030 	int cport = SATA_TXLT_CPORT(spx);
7031 	int cdblen;
7032 	int rval, reason;
7033 	int synch;
7034 	union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp;
7035 
7036 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
7037 
7038 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
7039 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
7040 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7041 		return (rval);
7042 	}
7043 
7044 	/*
7045 	 * ATAPI device executes some ATA commands in addition to those
7046 	 * commands sent via PACKET command. These ATA commands may be
7047 	 * executed by the regular SATA translation functions. None needs
7048 	 * to be captured now.
7049 	 *
7050 	 * Commands sent via PACKET command include:
7051 	 *	MMC command set for ATAPI CD/DVD device
7052 	 *	SSC command set for ATAPI TAPE device
7053 	 *
7054 	 */
7055 
7056 	/* Check the size of cdb */
7057 	cdblen = scsi_cdb_size[GETGROUP(cdbp)];
7058 	if (cdblen > sdinfo->satadrv_atapi_cdb_len) {
7059 		sata_log(NULL, CE_WARN,
7060 		    "sata: invalid ATAPI cdb length %d",
7061 		    scsipkt->pkt_cdblen);
7062 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7063 		return (TRAN_BADPKT);
7064 	}
7065 
7066 	SATAATAPITRACE(spx, cdblen);
7067 
7068 	/*
7069 	 * For non-read/write commands we need to
7070 	 * map buffer
7071 	 */
7072 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
7073 	case SCMD_READ:
7074 	case SCMD_READ_G1:
7075 	case SCMD_READ_G5:
7076 	case SCMD_READ_G4:
7077 	case SCMD_WRITE:
7078 	case SCMD_WRITE_G1:
7079 	case SCMD_WRITE_G5:
7080 	case SCMD_WRITE_G4:
7081 		break;
7082 	default:
7083 		if (bp != NULL) {
7084 			if (bp->b_flags & (B_PHYS | B_PAGEIO))
7085 				bp_mapin(bp);
7086 		}
7087 		break;
7088 	}
7089 	/*
7090 	 * scmd->satacmd_flags.sata_data_direction default -
7091 	 * SATA_DIR_NODATA_XFER - is set by
7092 	 * sata_txlt_generic_pkt_info().
7093 	 */
7094 	if (scmd->satacmd_bp) {
7095 		if (scmd->satacmd_bp->b_flags & B_READ) {
7096 			scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7097 		} else {
7098 			scmd->satacmd_flags.sata_data_direction =
7099 			    SATA_DIR_WRITE;
7100 		}
7101 	}
7102 
7103 	/*
7104 	 * Set up ATAPI packet command.
7105 	 */
7106 
7107 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7108 
7109 	/* Copy cdb into sata_cmd */
7110 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7111 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7112 	bcopy(cdbp, scmd->satacmd_acdb, cdblen);
7113 
7114 	/* See note in the command header */
7115 	if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) {
7116 		if (scmd->satacmd_acdb[3] != 0)
7117 			scmd->satacmd_acdb[4] = 255;
7118 	}
7119 
7120 #ifdef SATA_DEBUG
7121 	if (sata_debug_flags & SATA_DBG_ATAPI) {
7122 		uint8_t *p = scmd->satacmd_acdb;
7123 		char buf[3 * SATA_ATAPI_MAX_CDB_LEN];
7124 
7125 		(void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN,
7126 		    "%02x %02x %02x %02x %02x %02x %02x %02x "
7127 		    "%2x %02x %02x %02x %02x %02x %02x %02x",
7128 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7129 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7130 		buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0';
7131 		cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf);
7132 	}
7133 #endif
7134 
7135 	/*
7136 	 * Preset request sense data to NO SENSE.
7137 	 * If there is no way to get error information via Request Sense,
7138 	 * the packet request sense data would not have to be modified by HBA,
7139 	 * but it could be returned as is.
7140 	 */
7141 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
7142 	sata_fixed_sense_data_preset(
7143 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7144 
7145 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
7146 		/* Need callback function */
7147 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion;
7148 		synch = FALSE;
7149 	} else
7150 		synch = TRUE;
7151 
7152 	/* Transfer command to HBA */
7153 	if (sata_hba_start(spx, &rval) != 0) {
7154 		/* Pkt not accepted for execution */
7155 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
7156 		return (rval);
7157 	}
7158 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
7159 	/*
7160 	 * If execution is non-synchronous,
7161 	 * a callback function will handle potential errors, translate
7162 	 * the response and will do a callback to a target driver.
7163 	 * If it was synchronous, use the same framework callback to check
7164 	 * an execution status.
7165 	 */
7166 	if (synch) {
7167 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7168 		    "synchronous execution status %x\n",
7169 		    spx->txlt_sata_pkt->satapkt_reason);
7170 		sata_txlt_atapi_completion(spx->txlt_sata_pkt);
7171 	}
7172 	return (TRAN_ACCEPT);
7173 }
7174 
7175 
7176 /*
7177  * ATAPI Packet command completion.
7178  *
7179  * Failure of the command passed via Packet command are considered device
7180  * error. SATA HBA driver would have to retrieve error data (via Request
7181  * Sense command delivered via error retrieval sata packet) and copy it
7182  * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data.
7183  */
7184 static void
7185 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt)
7186 {
7187 	sata_pkt_txlate_t *spx =
7188 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7189 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7190 	struct scsi_extended_sense *sense;
7191 	struct buf *bp;
7192 	int rval;
7193 
7194 #ifdef SATA_DEBUG
7195 	uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense;
7196 #endif
7197 
7198 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7199 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7200 
7201 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7202 		/* Normal completion */
7203 		if (sata_pkt->satapkt_cmd.satacmd_bp != NULL)
7204 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
7205 		scsipkt->pkt_reason = CMD_CMPLT;
7206 		*scsipkt->pkt_scbp = STATUS_GOOD;
7207 		if (spx->txlt_tmp_buf != NULL) {
7208 			/* Temporary buffer was used */
7209 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7210 			if (bp->b_flags & B_READ) {
7211 				rval = ddi_dma_sync(
7212 				    spx->txlt_buf_dma_handle, 0, 0,
7213 				    DDI_DMA_SYNC_FORCPU);
7214 				ASSERT(rval == DDI_SUCCESS);
7215 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7216 				    bp->b_bcount);
7217 			}
7218 		}
7219 	} else {
7220 		/*
7221 		 * Something went wrong - analyze return
7222 		 */
7223 		*scsipkt->pkt_scbp = STATUS_CHECK;
7224 		sense = sata_arq_sense(spx);
7225 
7226 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7227 			/*
7228 			 * pkt_reason should be CMD_CMPLT for DEVICE ERROR.
7229 			 * Under this condition ERR bit is set for ATA command,
7230 			 * and CHK bit set for ATAPI command.
7231 			 *
7232 			 * Please check st_intr & sdintr about how pkt_reason
7233 			 * is used.
7234 			 */
7235 			scsipkt->pkt_reason = CMD_CMPLT;
7236 
7237 			/*
7238 			 * We may not have ARQ data if there was a double
7239 			 * error. But sense data in sata packet was pre-set
7240 			 * with NO SENSE so it is valid even if HBA could
7241 			 * not retrieve a real sense data.
7242 			 * Just copy this sense data into scsi pkt sense area.
7243 			 */
7244 			bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense,
7245 			    SATA_ATAPI_MIN_RQSENSE_LEN);
7246 #ifdef SATA_DEBUG
7247 			if (sata_debug_flags & SATA_DBG_SCSI_IF) {
7248 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7249 				    "sata_txlt_atapi_completion: %02x\n"
7250 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
7251 				    "          %02x %02x %02x %02x %02x %02x "
7252 				    "          %02x %02x %02x %02x %02x %02x\n",
7253 				    scsipkt->pkt_reason,
7254 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7255 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7256 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7257 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7258 				    rqsp[16], rqsp[17]);
7259 			}
7260 #endif
7261 		} else {
7262 			switch (sata_pkt->satapkt_reason) {
7263 			case SATA_PKT_PORT_ERROR:
7264 				/*
7265 				 * We have no device data.
7266 				 */
7267 				scsipkt->pkt_reason = CMD_INCOMPLETE;
7268 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
7269 				    STATE_GOT_TARGET | STATE_SENT_CMD |
7270 				    STATE_GOT_STATUS);
7271 				sense->es_key = KEY_HARDWARE_ERROR;
7272 				break;
7273 
7274 			case SATA_PKT_TIMEOUT:
7275 				scsipkt->pkt_reason = CMD_TIMEOUT;
7276 				scsipkt->pkt_statistics |=
7277 				    STAT_TIMEOUT | STAT_DEV_RESET;
7278 				/*
7279 				 * Need to check if HARDWARE_ERROR/
7280 				 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more
7281 				 * appropriate.
7282 				 */
7283 				break;
7284 
7285 			case SATA_PKT_ABORTED:
7286 				scsipkt->pkt_reason = CMD_ABORTED;
7287 				scsipkt->pkt_statistics |= STAT_ABORTED;
7288 				/* Should we set key COMMAND_ABPRTED? */
7289 				break;
7290 
7291 			case SATA_PKT_RESET:
7292 				scsipkt->pkt_reason = CMD_RESET;
7293 				scsipkt->pkt_statistics |= STAT_DEV_RESET;
7294 				/*
7295 				 * May be we should set Unit Attention /
7296 				 * Reset. Perhaps the same should be
7297 				 * returned for disks....
7298 				 */
7299 				sense->es_key = KEY_UNIT_ATTENTION;
7300 				sense->es_add_code = SD_SCSI_ASC_RESET;
7301 				break;
7302 
7303 			default:
7304 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7305 				    "sata_txlt_atapi_completion: "
7306 				    "invalid packet completion reason"));
7307 				scsipkt->pkt_reason = CMD_TRAN_ERR;
7308 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
7309 				    STATE_GOT_TARGET | STATE_SENT_CMD |
7310 				    STATE_GOT_STATUS);
7311 				break;
7312 			}
7313 		}
7314 	}
7315 
7316 	SATAATAPITRACE(spx, 0);
7317 
7318 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7319 	    scsipkt->pkt_comp != NULL) {
7320 		/* scsi callback required */
7321 		(*scsipkt->pkt_comp)(scsipkt);
7322 	}
7323 }
7324 
7325 /*
7326  * Set up error retrieval sata command for ATAPI Packet Command error data
7327  * recovery.
7328  *
7329  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
7330  * returns SATA_FAILURE otherwise.
7331  */
7332 
7333 static int
7334 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
7335 {
7336 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
7337 	sata_cmd_t *scmd;
7338 	struct buf *bp;
7339 
7340 	/*
7341 	 * Allocate dma-able buffer error data.
7342 	 * Buffer allocation will take care of buffer alignment and other DMA
7343 	 * attributes.
7344 	 */
7345 	bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN);
7346 	if (bp == NULL) {
7347 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
7348 		    "sata_get_err_retrieval_pkt: "
7349 		    "cannot allocate buffer for error data", NULL);
7350 		return (SATA_FAILURE);
7351 	}
7352 	bp_mapin(bp); /* make data buffer accessible */
7353 
7354 	/* Operation modes are up to the caller */
7355 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7356 
7357 	/* Synchronous mode, no callback - may be changed by the caller */
7358 	spkt->satapkt_comp = NULL;
7359 	spkt->satapkt_time = sata_default_pkt_time;
7360 
7361 	scmd = &spkt->satapkt_cmd;
7362 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7363 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7364 
7365 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7366 
7367 	/*
7368 	 * Set-up acdb. Request Sense CDB (packet command content) is
7369 	 * not in DMA-able buffer. Its handling is HBA-specific (how
7370 	 * it is transfered into packet FIS).
7371 	 */
7372 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7373 	bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN);
7374 	/* Following zeroing of pad bytes may not be necessary */
7375 	bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN],
7376 	    sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN);
7377 
7378 	/*
7379 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
7380 	 * before accessing it. Handle is in usual place in translate struct.
7381 	 */
7382 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
7383 
7384 	/*
7385 	 * Preset request sense data to NO SENSE.
7386 	 * Here it is redundant, only for a symetry with scsi-originated
7387 	 * packets. It should not be used for anything but debugging.
7388 	 */
7389 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
7390 	sata_fixed_sense_data_preset(
7391 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7392 
7393 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7394 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7395 
7396 	return (SATA_SUCCESS);
7397 }
7398 
7399 /*
7400  * Set-up ATAPI packet command.
7401  * Data transfer direction has to be set-up in sata_cmd structure prior to
7402  * calling this function.
7403  *
7404  * Returns void
7405  */
7406 
7407 static void
7408 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo)
7409 {
7410 	scmd->satacmd_addr_type = 0;		/* N/A */
7411 	scmd->satacmd_sec_count_lsb = 0;	/* no tag */
7412 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
7413 	scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ;
7414 	scmd->satacmd_lba_high_lsb =
7415 	    (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8);
7416 	scmd->satacmd_cmd_reg = SATAC_PACKET;	/* Command */
7417 
7418 	/*
7419 	 * We want all data to be transfered via DMA.
7420 	 * But specify it only if drive supports DMA and DMA mode is
7421 	 * selected - some drives are sensitive about it.
7422 	 * Hopefully it wil work for all drives....
7423 	 */
7424 	if (sdinfo->satadrv_settings & SATA_DEV_DMA)
7425 		scmd->satacmd_features_reg = SATA_ATAPI_F_DMA;
7426 
7427 	/*
7428 	 * Features register requires special care for devices that use
7429 	 * Serial ATA bridge - they need an explicit specification of
7430 	 * the data transfer direction for Packet DMA commands.
7431 	 * Setting this bit is harmless if DMA is not used.
7432 	 *
7433 	 * Many drives do not implement word 80, specifying what ATA/ATAPI
7434 	 * spec they follow.
7435 	 * We are arbitrarily following the latest SerialATA 2.6 spec,
7436 	 * which uses ATA/ATAPI 6 specification for Identify Data, unless
7437 	 * ATA/ATAPI-7 support is explicitly indicated.
7438 	 */
7439 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
7440 	    sdinfo->satadrv_id.ai_majorversion != 0xffff &&
7441 	    (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) {
7442 		/*
7443 		 * Specification of major version is valid and version 7
7444 		 * is supported. It does automatically imply that all
7445 		 * spec features are supported. For now, we assume that
7446 		 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete.
7447 		 */
7448 		if ((sdinfo->satadrv_id.ai_dirdma &
7449 		    SATA_ATAPI_ID_DMADIR_REQ) != 0) {
7450 			if (scmd->satacmd_flags.sata_data_direction ==
7451 			    SATA_DIR_READ)
7452 			scmd->satacmd_features_reg |=
7453 			    SATA_ATAPI_F_DATA_DIR_READ;
7454 		}
7455 	}
7456 }
7457 
7458 
7459 #ifdef SATA_DEBUG
7460 
7461 /* Display 18 bytes of Inquiry data */
7462 static void
7463 sata_show_inqry_data(uint8_t *buf)
7464 {
7465 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
7466 	uint8_t *p;
7467 
7468 	cmn_err(CE_NOTE, "Inquiry data:");
7469 	cmn_err(CE_NOTE, "device type %x", inq->inq_dtype);
7470 	cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb);
7471 	cmn_err(CE_NOTE, "version %x", inq->inq_ansi);
7472 	cmn_err(CE_NOTE, "ATAPI transport version %d",
7473 	    SATA_ATAPI_TRANS_VERSION(inq));
7474 	cmn_err(CE_NOTE, "response data format %d, aenc %d",
7475 	    inq->inq_rdf, inq->inq_aenc);
7476 	cmn_err(CE_NOTE, " additional length %d", inq->inq_len);
7477 	cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs);
7478 	p = (uint8_t *)inq->inq_vid;
7479 	cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x "
7480 	    "%02x %02x %02x %02x",
7481 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
7482 	p = (uint8_t *)inq->inq_vid;
7483 	cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c",
7484 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
7485 
7486 	p = (uint8_t *)inq->inq_pid;
7487 	cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x "
7488 	    "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
7489 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7490 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7491 	p = (uint8_t *)inq->inq_pid;
7492 	cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c "
7493 	    "%c %c %c %c %c %c %c %c",
7494 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7495 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7496 
7497 	p = (uint8_t *)inq->inq_revision;
7498 	cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x",
7499 	    p[0], p[1], p[2], p[3]);
7500 	p = (uint8_t *)inq->inq_revision;
7501 	cmn_err(CE_NOTE, "revision: %c %c %c %c",
7502 	    p[0], p[1], p[2], p[3]);
7503 
7504 }
7505 
7506 
7507 static void
7508 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count)
7509 {
7510 	struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt;
7511 
7512 	if (scsi_pkt == NULL)
7513 		return;
7514 	if (count != 0) {
7515 		/* saving cdb */
7516 		bzero(sata_atapi_trace[sata_atapi_trace_index].acdb,
7517 		    SATA_ATAPI_MAX_CDB_LEN);
7518 		bcopy(scsi_pkt->pkt_cdbp,
7519 		    sata_atapi_trace[sata_atapi_trace_index].acdb, count);
7520 	} else {
7521 		bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)->
7522 		    sts_sensedata,
7523 		    sata_atapi_trace[sata_atapi_trace_index].arqs,
7524 		    SATA_ATAPI_MIN_RQSENSE_LEN);
7525 		sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason =
7526 		    scsi_pkt->pkt_reason;
7527 		sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason =
7528 		    spx->txlt_sata_pkt->satapkt_reason;
7529 
7530 		if (++sata_atapi_trace_index >= 64)
7531 			sata_atapi_trace_index = 0;
7532 	}
7533 }
7534 
7535 #endif
7536 
7537 /*
7538  * Fetch inquiry data from ATAPI device
7539  * Returns SATA_SUCCESS if operation was successfull, SATA_FAILURE otherwise.
7540  *
7541  * Note:
7542  * inqb pointer does not point to a DMA-able buffer. It is a local buffer
7543  * where the caller expects to see the inquiry data.
7544  *
7545  */
7546 
7547 static int
7548 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba,
7549     sata_address_t *saddr, struct scsi_inquiry *inq)
7550 {
7551 	sata_pkt_txlate_t *spx;
7552 	sata_pkt_t *spkt;
7553 	struct buf *bp;
7554 	sata_drive_info_t *sdinfo;
7555 	sata_cmd_t *scmd;
7556 	int rval;
7557 	uint8_t *rqsp;
7558 #ifdef SATA_DEBUG
7559 	char msg_buf[MAXPATHLEN];
7560 #endif
7561 
7562 	ASSERT(sata_hba != NULL);
7563 
7564 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
7565 	spx->txlt_sata_hba_inst = sata_hba;
7566 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
7567 	spkt = sata_pkt_alloc(spx, NULL);
7568 	if (spkt == NULL) {
7569 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7570 		return (SATA_FAILURE);
7571 	}
7572 	/* address is needed now */
7573 	spkt->satapkt_device.satadev_addr = *saddr;
7574 
7575 	/* scsi_inquiry size buffer */
7576 	bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry));
7577 	if (bp == NULL) {
7578 		sata_pkt_free(spx);
7579 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7580 		SATA_LOG_D((sata_hba, CE_WARN,
7581 		    "sata_get_atapi_inquiry_data: "
7582 		    "cannot allocate data buffer"));
7583 		return (SATA_FAILURE);
7584 	}
7585 	bp_mapin(bp); /* make data buffer accessible */
7586 
7587 	scmd = &spkt->satapkt_cmd;
7588 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7589 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7590 
7591 	/* Use synchronous mode */
7592 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7593 	spkt->satapkt_comp = NULL;
7594 	spkt->satapkt_time = sata_default_pkt_time;
7595 
7596 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
7597 
7598 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7599 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7600 
7601 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
7602 	sdinfo = sata_get_device_info(sata_hba,
7603 	    &spx->txlt_sata_pkt->satapkt_device);
7604 	if (sdinfo == NULL) {
7605 		/* we have to be carefull about the disapearing device */
7606 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7607 		rval = SATA_FAILURE;
7608 		goto cleanup;
7609 	}
7610 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7611 
7612 	/*
7613 	 * Set-up acdb. This works for atapi transport version 2 and later.
7614 	 */
7615 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7616 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7617 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
7618 	scmd->satacmd_acdb[1] = 0x00;
7619 	scmd->satacmd_acdb[2] = 0x00;
7620 	scmd->satacmd_acdb[3] = 0x00;
7621 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
7622 	scmd->satacmd_acdb[5] = 0x00;
7623 
7624 	sata_fixed_sense_data_preset(
7625 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7626 
7627 	/* Transfer command to HBA */
7628 	if (sata_hba_start(spx, &rval) != 0) {
7629 		/* Pkt not accepted for execution */
7630 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
7631 		    "sata_get_atapi_inquiry_data: "
7632 		    "Packet not accepted for execution - ret: %02x", rval);
7633 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7634 		rval = SATA_FAILURE;
7635 		goto cleanup;
7636 	}
7637 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7638 
7639 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
7640 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
7641 		    "sata_get_atapi_inquiry_data: "
7642 		    "Packet completed successfully - ret: %02x", rval);
7643 		if (spx->txlt_buf_dma_handle != NULL) {
7644 			/*
7645 			 * Sync buffer. Handle is in usual place in translate
7646 			 * struct.
7647 			 */
7648 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
7649 			    DDI_DMA_SYNC_FORCPU);
7650 			ASSERT(rval == DDI_SUCCESS);
7651 		}
7652 		/*
7653 		 * Normal completion - copy data into caller's buffer
7654 		 */
7655 		bcopy(bp->b_un.b_addr, (uint8_t *)inq,
7656 		    sizeof (struct scsi_inquiry));
7657 #ifdef SATA_DEBUG
7658 		if (sata_debug_flags & SATA_DBG_ATAPI) {
7659 			sata_show_inqry_data((uint8_t *)inq);
7660 		}
7661 #endif
7662 		rval = SATA_SUCCESS;
7663 	} else {
7664 		/*
7665 		 * Something went wrong - analyze return - check rqsense data
7666 		 */
7667 		rval = SATA_FAILURE;
7668 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7669 			/*
7670 			 * ARQ data hopefull show something other than NO SENSE
7671 			 */
7672 			rqsp = scmd->satacmd_rqsense;
7673 #ifdef SATA_DEBUG
7674 			if (sata_debug_flags & SATA_DBG_ATAPI) {
7675 				msg_buf[0] = '\0';
7676 				(void) snprintf(msg_buf, MAXPATHLEN,
7677 				    "ATAPI packet completion reason: %02x\n"
7678 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x\n"
7679 				    "          %02x %02x %02x %02x %02x %02x\n"
7680 				    "          %02x %02x %02x %02x %02x %02x",
7681 				    spkt->satapkt_reason,
7682 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7683 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7684 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7685 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7686 				    rqsp[16], rqsp[17]);
7687 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7688 				    "%s", msg_buf);
7689 			}
7690 #endif
7691 		} else {
7692 			switch (spkt->satapkt_reason) {
7693 			case SATA_PKT_PORT_ERROR:
7694 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7695 				    "sata_get_atapi_inquiry_data: "
7696 				    "packet reason: port error", NULL);
7697 				break;
7698 
7699 			case SATA_PKT_TIMEOUT:
7700 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7701 				    "sata_get_atapi_inquiry_data: "
7702 				    "packet reason: timeout", NULL);
7703 				break;
7704 
7705 			case SATA_PKT_ABORTED:
7706 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7707 				    "sata_get_atapi_inquiry_data: "
7708 				    "packet reason: aborted", NULL);
7709 				break;
7710 
7711 			case SATA_PKT_RESET:
7712 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7713 				    "sata_get_atapi_inquiry_data: "
7714 				    "packet reason: reset\n", NULL);
7715 				break;
7716 			default:
7717 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7718 				    "sata_get_atapi_inquiry_data: "
7719 				    "invalid packet reason: %02x\n",
7720 				    spkt->satapkt_reason);
7721 				break;
7722 			}
7723 		}
7724 	}
7725 cleanup:
7726 	sata_free_local_buffer(spx);
7727 	sata_pkt_free(spx);
7728 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
7729 	return (rval);
7730 }
7731 
7732 
7733 
7734 
7735 
7736 #if 0
7737 #ifdef SATA_DEBUG
7738 
7739 /*
7740  * Test ATAPI packet command.
7741  * Single threaded test: send packet command in synch mode, process completion
7742  *
7743  */
7744 static void
7745 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport)
7746 {
7747 	sata_pkt_txlate_t *spx;
7748 	sata_pkt_t *spkt;
7749 	struct buf *bp;
7750 	sata_device_t sata_device;
7751 	sata_drive_info_t *sdinfo;
7752 	sata_cmd_t *scmd;
7753 	int rval;
7754 	uint8_t *rqsp;
7755 
7756 	ASSERT(sata_hba_inst != NULL);
7757 	sata_device.satadev_addr.cport = cport;
7758 	sata_device.satadev_addr.pmport = 0;
7759 	sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
7760 	sata_device.satadev_rev = SATA_DEVICE_REV;
7761 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7762 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
7763 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7764 	if (sdinfo == NULL) {
7765 		sata_log(sata_hba_inst, CE_WARN,
7766 		    "sata_test_atapi_packet_command: "
7767 		    "no device info for cport %d",
7768 		    sata_device.satadev_addr.cport);
7769 		return;
7770 	}
7771 
7772 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
7773 	spx->txlt_sata_hba_inst = sata_hba_inst;
7774 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
7775 	spkt = sata_pkt_alloc(spx, NULL);
7776 	if (spkt == NULL) {
7777 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7778 		return;
7779 	}
7780 	/* address is needed now */
7781 	spkt->satapkt_device.satadev_addr = sata_device.satadev_addr;
7782 
7783 	/* 1024k buffer */
7784 	bp = sata_alloc_local_buffer(spx, 1024);
7785 	if (bp == NULL) {
7786 		sata_pkt_free(spx);
7787 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7788 		sata_log(sata_hba_inst, CE_WARN,
7789 		    "sata_test_atapi_packet_command: "
7790 		    "cannot allocate data buffer");
7791 		return;
7792 	}
7793 	bp_mapin(bp); /* make data buffer accessible */
7794 
7795 	scmd = &spkt->satapkt_cmd;
7796 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7797 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7798 
7799 	/* Use synchronous mode */
7800 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7801 
7802 	/* Synchronous mode, no callback - may be changed by the caller */
7803 	spkt->satapkt_comp = NULL;
7804 	spkt->satapkt_time = sata_default_pkt_time;
7805 
7806 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
7807 
7808 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7809 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7810 
7811 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7812 
7813 	/* Set-up acdb. */
7814 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7815 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7816 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
7817 	scmd->satacmd_acdb[1] = 0x00;
7818 	scmd->satacmd_acdb[2] = 0x00;
7819 	scmd->satacmd_acdb[3] = 0x00;
7820 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
7821 	scmd->satacmd_acdb[5] = 0x00;
7822 
7823 	sata_fixed_sense_data_preset(
7824 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7825 
7826 	/* Transfer command to HBA */
7827 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7828 	if (sata_hba_start(spx, &rval) != 0) {
7829 		/* Pkt not accepted for execution */
7830 		sata_log(sata_hba_inst, CE_WARN,
7831 		    "sata_test_atapi_packet_command: "
7832 		    "Packet not accepted for execution - ret: %02x", rval);
7833 		mutex_exit(
7834 		    &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7835 		goto cleanup;
7836 	}
7837 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7838 
7839 	if (spx->txlt_buf_dma_handle != NULL) {
7840 		/*
7841 		 * Sync buffer. Handle is in usual place in translate struct.
7842 		 */
7843 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
7844 		    DDI_DMA_SYNC_FORCPU);
7845 		ASSERT(rval == DDI_SUCCESS);
7846 	}
7847 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
7848 		sata_log(sata_hba_inst, CE_WARN,
7849 		    "sata_test_atapi_packet_command: "
7850 		    "Packet completed successfully");
7851 		/*
7852 		 * Normal completion - show inquiry data
7853 		 */
7854 		sata_show_inqry_data((uint8_t *)bp->b_un.b_addr);
7855 	} else {
7856 		/*
7857 		 * Something went wrong - analyze return - check rqsense data
7858 		 */
7859 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7860 			/*
7861 			 * ARQ data hopefull show something other than NO SENSE
7862 			 */
7863 			rqsp = scmd->satacmd_rqsense;
7864 			sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7865 			    "ATAPI packet completion reason: %02x\n"
7866 			    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
7867 			    "          %02x %02x %02x %02x %02x %02x "
7868 			    "          %02x %02x %02x %02x %02x %02x\n",
7869 			    spkt->satapkt_reason,
7870 			    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7871 			    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7872 			    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7873 			    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7874 			    rqsp[16], rqsp[17]);
7875 		} else {
7876 			switch (spkt->satapkt_reason) {
7877 			case SATA_PKT_PORT_ERROR:
7878 				sata_log(sata_hba_inst, CE_WARN,
7879 				    "sata_test_atapi_packet_command: "
7880 				    "packet reason: port error\n");
7881 				break;
7882 
7883 			case SATA_PKT_TIMEOUT:
7884 				sata_log(sata_hba_inst, CE_WARN,
7885 				    "sata_test_atapi_packet_command: "
7886 				    "packet reason: timeout\n");
7887 				break;
7888 
7889 			case SATA_PKT_ABORTED:
7890 				sata_log(sata_hba_inst, CE_WARN,
7891 				    "sata_test_atapi_packet_command: "
7892 				    "packet reason: aborted\n");
7893 				break;
7894 
7895 			case SATA_PKT_RESET:
7896 				sata_log(sata_hba_inst, CE_WARN,
7897 				    "sata_test_atapi_packet_command: "
7898 				    "packet reason: reset\n");
7899 				break;
7900 			default:
7901 				sata_log(sata_hba_inst, CE_WARN,
7902 				    "sata_test_atapi_packet_command: "
7903 				    "invalid packet reason: %02x\n",
7904 				    spkt->satapkt_reason);
7905 				break;
7906 			}
7907 		}
7908 	}
7909 cleanup:
7910 	sata_free_local_buffer(spx);
7911 	sata_pkt_free(spx);
7912 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
7913 }
7914 
7915 #endif /* SATA_DEBUG */
7916 #endif /* 1 */
7917 
7918 
7919 /* ************************** LOCAL HELPER FUNCTIONS *********************** */
7920 
7921 /*
7922  * Validate sata_tran info
7923  * SATA_FAILURE returns if structure is inconsistent or structure revision
7924  * does not match one used by the framework.
7925  *
7926  * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains
7927  * required function pointers.
7928  * Returns SATA_FAILURE otherwise.
7929  */
7930 static int
7931 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran)
7932 {
7933 	/*
7934 	 * SATA_TRAN_HBA_REV is the current (highest) revision number
7935 	 * of the SATA interface.
7936 	 */
7937 	if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) {
7938 		sata_log(NULL, CE_WARN,
7939 		    "sata: invalid sata_hba_tran version %d for driver %s",
7940 		    sata_tran->sata_tran_hba_rev, ddi_driver_name(dip));
7941 		return (SATA_FAILURE);
7942 	}
7943 
7944 	if (dip != sata_tran->sata_tran_hba_dip) {
7945 		SATA_LOG_D((NULL, CE_WARN,
7946 		    "sata: inconsistent sata_tran_hba_dip "
7947 		    "%p / %p", sata_tran->sata_tran_hba_dip, dip));
7948 		return (SATA_FAILURE);
7949 	}
7950 
7951 	if (sata_tran->sata_tran_probe_port == NULL ||
7952 	    sata_tran->sata_tran_start == NULL ||
7953 	    sata_tran->sata_tran_abort == NULL ||
7954 	    sata_tran->sata_tran_reset_dport == NULL ||
7955 	    sata_tran->sata_tran_hotplug_ops == NULL ||
7956 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL ||
7957 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate ==
7958 	    NULL) {
7959 		SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing "
7960 		    "required functions"));
7961 	}
7962 	return (SATA_SUCCESS);
7963 }
7964 
7965 /*
7966  * Remove HBA instance from sata_hba_list.
7967  */
7968 static void
7969 sata_remove_hba_instance(dev_info_t *dip)
7970 {
7971 	sata_hba_inst_t	*sata_hba_inst;
7972 
7973 	mutex_enter(&sata_mutex);
7974 	for (sata_hba_inst = sata_hba_list;
7975 	    sata_hba_inst != (struct sata_hba_inst *)NULL;
7976 	    sata_hba_inst = sata_hba_inst->satahba_next) {
7977 		if (sata_hba_inst->satahba_dip == dip)
7978 			break;
7979 	}
7980 
7981 	if (sata_hba_inst == (struct sata_hba_inst *)NULL) {
7982 #ifdef SATA_DEBUG
7983 		cmn_err(CE_WARN, "sata_remove_hba_instance: "
7984 		    "unknown HBA instance\n");
7985 #endif
7986 		ASSERT(FALSE);
7987 	}
7988 	if (sata_hba_inst == sata_hba_list) {
7989 		sata_hba_list = sata_hba_inst->satahba_next;
7990 		if (sata_hba_list) {
7991 			sata_hba_list->satahba_prev =
7992 			    (struct sata_hba_inst *)NULL;
7993 		}
7994 		if (sata_hba_inst == sata_hba_list_tail) {
7995 			sata_hba_list_tail = NULL;
7996 		}
7997 	} else if (sata_hba_inst == sata_hba_list_tail) {
7998 		sata_hba_list_tail = sata_hba_inst->satahba_prev;
7999 		if (sata_hba_list_tail) {
8000 			sata_hba_list_tail->satahba_next =
8001 			    (struct sata_hba_inst *)NULL;
8002 		}
8003 	} else {
8004 		sata_hba_inst->satahba_prev->satahba_next =
8005 		    sata_hba_inst->satahba_next;
8006 		sata_hba_inst->satahba_next->satahba_prev =
8007 		    sata_hba_inst->satahba_prev;
8008 	}
8009 	mutex_exit(&sata_mutex);
8010 }
8011 
8012 
8013 
8014 
8015 
8016 /*
8017  * Probe all SATA ports of the specified HBA instance.
8018  * The assumption is that there are no target and attachment point minor nodes
8019  * created by the boot subsystems, so we do not need to prune device tree.
8020  *
8021  * This function is called only from sata_hba_attach(). It does not have to
8022  * be protected by controller mutex, because the hba_attached flag is not set
8023  * yet and no one would be touching this HBA instance other than this thread.
8024  * Determines if port is active and what type of the device is attached
8025  * (if any). Allocates necessary structures for each port.
8026  *
8027  * An AP (Attachement Point) node is created for each SATA device port even
8028  * when there is no device attached.
8029  */
8030 
8031 static 	void
8032 sata_probe_ports(sata_hba_inst_t *sata_hba_inst)
8033 {
8034 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
8035 	int			ncport, npmport;
8036 	sata_cport_info_t 	*cportinfo;
8037 	sata_drive_info_t	*drive;
8038 	sata_pmult_info_t	*pminfo;
8039 	sata_pmport_info_t 	*pmportinfo;
8040 	sata_device_t		sata_device;
8041 	int			rval;
8042 	dev_t			minor_number;
8043 	char			name[16];
8044 	clock_t			start_time, cur_time;
8045 
8046 	/*
8047 	 * Probe controller ports first, to find port status and
8048 	 * any port multiplier attached.
8049 	 */
8050 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
8051 		/* allocate cport structure */
8052 		cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP);
8053 		ASSERT(cportinfo != NULL);
8054 		mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL);
8055 
8056 		mutex_enter(&cportinfo->cport_mutex);
8057 
8058 		cportinfo->cport_addr.cport = ncport;
8059 		cportinfo->cport_addr.pmport = 0;
8060 		cportinfo->cport_addr.qual = SATA_ADDR_CPORT;
8061 		cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
8062 		cportinfo->cport_state |= SATA_STATE_PROBING;
8063 		SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo;
8064 
8065 		/*
8066 		 * Regardless if a port is usable or not, create
8067 		 * an attachment point
8068 		 */
8069 		mutex_exit(&cportinfo->cport_mutex);
8070 		minor_number =
8071 		    SATA_MAKE_AP_MINOR(ddi_get_instance(dip), ncport, 0, 0);
8072 		(void) sprintf(name, "%d", ncport);
8073 		if (ddi_create_minor_node(dip, name, S_IFCHR,
8074 		    minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) !=
8075 		    DDI_SUCCESS) {
8076 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
8077 			    "cannot create SATA attachment point for port %d",
8078 			    ncport);
8079 		}
8080 
8081 		/* Probe port */
8082 		start_time = ddi_get_lbolt();
8083 	reprobe_cport:
8084 		sata_device.satadev_addr.cport = ncport;
8085 		sata_device.satadev_addr.pmport = 0;
8086 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
8087 		sata_device.satadev_rev = SATA_DEVICE_REV;
8088 
8089 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8090 		    (dip, &sata_device);
8091 
8092 		mutex_enter(&cportinfo->cport_mutex);
8093 		sata_update_port_scr(&cportinfo->cport_scr, &sata_device);
8094 		if (rval != SATA_SUCCESS) {
8095 			/* Something went wrong? Fail the port */
8096 			cportinfo->cport_state = SATA_PSTATE_FAILED;
8097 			mutex_exit(&cportinfo->cport_mutex);
8098 			continue;
8099 		}
8100 		cportinfo->cport_state &= ~SATA_STATE_PROBING;
8101 		cportinfo->cport_state |= SATA_STATE_PROBED;
8102 		cportinfo->cport_dev_type = sata_device.satadev_type;
8103 
8104 		cportinfo->cport_state |= SATA_STATE_READY;
8105 		if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) {
8106 			mutex_exit(&cportinfo->cport_mutex);
8107 			continue;
8108 		}
8109 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
8110 			/*
8111 			 * There is some device attached.
8112 			 * Allocate device info structure
8113 			 */
8114 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) {
8115 				mutex_exit(&cportinfo->cport_mutex);
8116 				SATA_CPORTINFO_DRV_INFO(cportinfo) =
8117 				    kmem_zalloc(sizeof (sata_drive_info_t),
8118 				    KM_SLEEP);
8119 				mutex_enter(&cportinfo->cport_mutex);
8120 			}
8121 			drive = SATA_CPORTINFO_DRV_INFO(cportinfo);
8122 			drive->satadrv_addr = cportinfo->cport_addr;
8123 			drive->satadrv_addr.qual = SATA_ADDR_DCPORT;
8124 			drive->satadrv_type = cportinfo->cport_dev_type;
8125 			drive->satadrv_state = SATA_STATE_UNKNOWN;
8126 
8127 			mutex_exit(&cportinfo->cport_mutex);
8128 			if (sata_add_device(dip, sata_hba_inst, ncport, 0) !=
8129 			    SATA_SUCCESS) {
8130 				/*
8131 				 * Plugged device was not correctly identified.
8132 				 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT
8133 				 */
8134 				cur_time = ddi_get_lbolt();
8135 				if ((cur_time - start_time) <
8136 				    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
8137 					/* sleep for a while */
8138 					delay(drv_usectohz(
8139 					    SATA_DEV_RETRY_DLY));
8140 					goto reprobe_cport;
8141 				}
8142 			}
8143 		} else {
8144 			mutex_exit(&cportinfo->cport_mutex);
8145 			ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT);
8146 			pminfo = kmem_zalloc(sizeof (sata_pmult_info_t),
8147 			    KM_SLEEP);
8148 			mutex_enter(&cportinfo->cport_mutex);
8149 			ASSERT(pminfo != NULL);
8150 			SATA_CPORTINFO_PMULT_INFO(cportinfo) = pminfo;
8151 			pminfo->pmult_addr.cport = cportinfo->cport_addr.cport;
8152 			pminfo->pmult_addr.pmport = SATA_PMULT_HOSTPORT;
8153 			pminfo->pmult_addr.qual = SATA_ADDR_PMPORT;
8154 			pminfo->pmult_num_dev_ports =
8155 			    sata_device.satadev_add_info;
8156 			mutex_init(&pminfo->pmult_mutex, NULL, MUTEX_DRIVER,
8157 			    NULL);
8158 			pminfo->pmult_state = SATA_STATE_PROBING;
8159 			mutex_exit(&cportinfo->cport_mutex);
8160 
8161 			/* Probe Port Multiplier ports */
8162 			for (npmport = 0;
8163 			    npmport < pminfo->pmult_num_dev_ports;
8164 			    npmport++) {
8165 				pmportinfo = kmem_zalloc(
8166 				    sizeof (sata_pmport_info_t), KM_SLEEP);
8167 				mutex_enter(&cportinfo->cport_mutex);
8168 				ASSERT(pmportinfo != NULL);
8169 				pmportinfo->pmport_addr.cport = ncport;
8170 				pmportinfo->pmport_addr.pmport = npmport;
8171 				pmportinfo->pmport_addr.qual =
8172 				    SATA_ADDR_PMPORT;
8173 				pminfo->pmult_dev_port[npmport] = pmportinfo;
8174 
8175 				mutex_init(&pmportinfo->pmport_mutex, NULL,
8176 				    MUTEX_DRIVER, NULL);
8177 
8178 				mutex_exit(&cportinfo->cport_mutex);
8179 
8180 				/* Create an attachment point */
8181 				minor_number = SATA_MAKE_AP_MINOR(
8182 				    ddi_get_instance(dip), ncport, npmport, 1);
8183 				(void) sprintf(name, "%d.%d", ncport, npmport);
8184 				if (ddi_create_minor_node(dip, name, S_IFCHR,
8185 				    minor_number, DDI_NT_SATA_ATTACHMENT_POINT,
8186 				    0) != DDI_SUCCESS) {
8187 					sata_log(sata_hba_inst, CE_WARN,
8188 					    "sata_hba_attach: "
8189 					    "cannot create SATA attachment "
8190 					    "point for port %d pmult port %d",
8191 					    ncport, npmport);
8192 				}
8193 
8194 				start_time = ddi_get_lbolt();
8195 			reprobe_pmport:
8196 				sata_device.satadev_addr.pmport = npmport;
8197 				sata_device.satadev_addr.qual =
8198 				    SATA_ADDR_PMPORT;
8199 
8200 				rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8201 				    (dip, &sata_device);
8202 				mutex_enter(&cportinfo->cport_mutex);
8203 
8204 				/* sata_update_port_info() */
8205 				sata_update_port_scr(&pmportinfo->pmport_scr,
8206 				    &sata_device);
8207 
8208 				if (rval != SATA_SUCCESS) {
8209 					pmportinfo->pmport_state =
8210 					    SATA_PSTATE_FAILED;
8211 					mutex_exit(&cportinfo->cport_mutex);
8212 					continue;
8213 				}
8214 				pmportinfo->pmport_state &=
8215 				    ~SATA_STATE_PROBING;
8216 				pmportinfo->pmport_state |= SATA_STATE_PROBED;
8217 				pmportinfo->pmport_dev_type =
8218 				    sata_device.satadev_type;
8219 
8220 				pmportinfo->pmport_state |= SATA_STATE_READY;
8221 				if (pmportinfo->pmport_dev_type ==
8222 				    SATA_DTYPE_NONE) {
8223 					mutex_exit(&cportinfo->cport_mutex);
8224 					continue;
8225 				}
8226 				/* Port multipliers cannot be chained */
8227 				ASSERT(pmportinfo->pmport_dev_type !=
8228 				    SATA_DTYPE_PMULT);
8229 				/*
8230 				 * There is something attached to Port
8231 				 * Multiplier device port
8232 				 * Allocate device info structure
8233 				 */
8234 				if (pmportinfo->pmport_sata_drive == NULL) {
8235 					mutex_exit(&cportinfo->cport_mutex);
8236 					pmportinfo->pmport_sata_drive =
8237 					    kmem_zalloc(
8238 					    sizeof (sata_drive_info_t),
8239 					    KM_SLEEP);
8240 					mutex_enter(&cportinfo->cport_mutex);
8241 				}
8242 				drive = pmportinfo->pmport_sata_drive;
8243 				drive->satadrv_addr.cport =
8244 				    pmportinfo->pmport_addr.cport;
8245 				drive->satadrv_addr.pmport = npmport;
8246 				drive->satadrv_addr.qual = SATA_ADDR_DPMPORT;
8247 				drive->satadrv_type = pmportinfo->
8248 				    pmport_dev_type;
8249 				drive->satadrv_state = SATA_STATE_UNKNOWN;
8250 
8251 				mutex_exit(&cportinfo->cport_mutex);
8252 				if (sata_add_device(dip, sata_hba_inst, ncport,
8253 				    npmport) != SATA_SUCCESS) {
8254 					/*
8255 					 * Plugged device was not correctly
8256 					 * identified. Retry, within the
8257 					 * SATA_DEV_IDENTIFY_TIMEOUT
8258 					 */
8259 					cur_time = ddi_get_lbolt();
8260 					if ((cur_time - start_time) <
8261 					    drv_usectohz(
8262 					    SATA_DEV_IDENTIFY_TIMEOUT)) {
8263 						/* sleep for a while */
8264 						delay(drv_usectohz(
8265 						    SATA_DEV_RETRY_DLY));
8266 						goto reprobe_pmport;
8267 					}
8268 				}
8269 			}
8270 			pmportinfo->pmport_state =
8271 			    SATA_STATE_PROBED | SATA_STATE_READY;
8272 		}
8273 	}
8274 }
8275 
8276 /*
8277  * Add SATA device for specified HBA instance & port (SCSI target
8278  * device nodes).
8279  * This function is called (indirectly) only from sata_hba_attach().
8280  * A target node is created when there is a supported type device attached,
8281  * but may be removed if it cannot be put online.
8282  *
8283  * This function cannot be called from an interrupt context.
8284  *
8285  * ONLY DISK TARGET NODES ARE CREATED NOW
8286  *
8287  * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when
8288  * device identification failed - adding a device could be retried.
8289  *
8290  */
8291 static 	int
8292 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst, int cport,
8293     int pmport)
8294 {
8295 	sata_cport_info_t 	*cportinfo;
8296 	sata_pmult_info_t	*pminfo;
8297 	sata_pmport_info_t	*pmportinfo;
8298 	dev_info_t		*cdip;		/* child dip */
8299 	sata_device_t		sata_device;
8300 	int			rval;
8301 
8302 
8303 
8304 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
8305 	ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE);
8306 	mutex_enter(&cportinfo->cport_mutex);
8307 	/*
8308 	 * Some device is attached to a controller port.
8309 	 * We rely on controllers distinquishing between no-device,
8310 	 * attached port multiplier and other kind of attached device.
8311 	 * We need to get Identify Device data and determine
8312 	 * positively the dev type before trying to attach
8313 	 * the target driver.
8314 	 */
8315 	sata_device.satadev_rev = SATA_DEVICE_REV;
8316 	if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
8317 		/*
8318 		 * Not port multiplier.
8319 		 */
8320 		sata_device.satadev_addr = cportinfo->cport_addr;
8321 		sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
8322 		mutex_exit(&cportinfo->cport_mutex);
8323 
8324 		rval = sata_probe_device(sata_hba_inst, &sata_device);
8325 		if (rval != SATA_SUCCESS ||
8326 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN)
8327 			return (SATA_FAILURE);
8328 
8329 		mutex_enter(&cportinfo->cport_mutex);
8330 		sata_show_drive_info(sata_hba_inst,
8331 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
8332 
8333 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
8334 			/*
8335 			 * Could not determine device type or
8336 			 * a device is not supported.
8337 			 * Degrade this device to unknown.
8338 			 */
8339 			cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8340 			mutex_exit(&cportinfo->cport_mutex);
8341 			return (SATA_SUCCESS);
8342 		}
8343 		cportinfo->cport_dev_type = sata_device.satadev_type;
8344 		cportinfo->cport_tgtnode_clean = B_TRUE;
8345 		mutex_exit(&cportinfo->cport_mutex);
8346 
8347 		/*
8348 		 * Initialize device to the desired state. Even if it
8349 		 * fails, the device will still attach but syslog
8350 		 * will show the warning.
8351 		 */
8352 		if (sata_initialize_device(sata_hba_inst,
8353 		    SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS) {
8354 			/* Retry */
8355 			rval = sata_initialize_device(sata_hba_inst,
8356 			    SATA_CPORTINFO_DRV_INFO(cportinfo));
8357 
8358 			if (rval == SATA_RETRY)
8359 				sata_log(sata_hba_inst, CE_WARN,
8360 				    "SATA device at port %d - "
8361 				    "default device features could not be set."
8362 				    " Device may not operate as expected.",
8363 				    cportinfo->cport_addr.cport);
8364 		}
8365 
8366 		cdip = sata_create_target_node(pdip, sata_hba_inst,
8367 		    &sata_device.satadev_addr);
8368 		mutex_enter(&cportinfo->cport_mutex);
8369 		if (cdip == NULL) {
8370 			/*
8371 			 * Attaching target node failed.
8372 			 * We retain sata_drive_info structure...
8373 			 */
8374 			mutex_exit(&cportinfo->cport_mutex);
8375 			return (SATA_SUCCESS);
8376 		}
8377 		(SATA_CPORTINFO_DRV_INFO(cportinfo))->
8378 		    satadrv_state = SATA_STATE_READY;
8379 	} else {
8380 		/* This must be Port Multiplier type */
8381 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
8382 			SATA_LOG_D((sata_hba_inst, CE_WARN,
8383 			    "sata_add_device: "
8384 			    "unrecognized dev type %x",
8385 			    cportinfo->cport_dev_type));
8386 			mutex_exit(&cportinfo->cport_mutex);
8387 			return (SATA_SUCCESS);
8388 		}
8389 		pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
8390 		pmportinfo = pminfo->pmult_dev_port[pmport];
8391 		sata_device.satadev_addr = pmportinfo->pmport_addr;
8392 		sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
8393 		mutex_exit(&cportinfo->cport_mutex);
8394 
8395 		rval = sata_probe_device(sata_hba_inst, &sata_device);
8396 		if (rval != SATA_SUCCESS ||
8397 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN) {
8398 			return (SATA_FAILURE);
8399 		}
8400 		mutex_enter(&cportinfo->cport_mutex);
8401 		sata_show_drive_info(sata_hba_inst,
8402 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
8403 
8404 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
8405 			/*
8406 			 * Could not determine device type.
8407 			 * Degrade this device to unknown.
8408 			 */
8409 			pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
8410 			mutex_exit(&cportinfo->cport_mutex);
8411 			return (SATA_SUCCESS);
8412 		}
8413 		pmportinfo->pmport_dev_type = sata_device.satadev_type;
8414 		pmportinfo->pmport_tgtnode_clean = B_TRUE;
8415 		mutex_exit(&cportinfo->cport_mutex);
8416 
8417 		/*
8418 		 * Initialize device to the desired state.
8419 		 * Even if it fails, the device will still
8420 		 * attach but syslog will show the warning.
8421 		 */
8422 		if (sata_initialize_device(sata_hba_inst,
8423 		    pmportinfo->pmport_sata_drive) != SATA_SUCCESS) {
8424 			/* Retry */
8425 			rval = sata_initialize_device(sata_hba_inst,
8426 			    pmportinfo->pmport_sata_drive);
8427 
8428 			if (rval == SATA_RETRY)
8429 				sata_log(sata_hba_inst, CE_WARN,
8430 				    "SATA device at port %d pmport %d - "
8431 				    "default device features could not be set."
8432 				    " Device may not operate as expected.",
8433 				    pmportinfo->pmport_addr.cport,
8434 				    pmportinfo->pmport_addr.pmport);
8435 		}
8436 		cdip = sata_create_target_node(pdip, sata_hba_inst,
8437 		    &sata_device.satadev_addr);
8438 		mutex_enter(&cportinfo->cport_mutex);
8439 		if (cdip == NULL) {
8440 			/*
8441 			 * Attaching target node failed.
8442 			 * We retain sata_drive_info structure...
8443 			 */
8444 			mutex_exit(&cportinfo->cport_mutex);
8445 			return (SATA_SUCCESS);
8446 		}
8447 		pmportinfo->pmport_sata_drive->satadrv_state |=
8448 		    SATA_STATE_READY;
8449 	}
8450 	mutex_exit(&cportinfo->cport_mutex);
8451 	return (SATA_SUCCESS);
8452 }
8453 
8454 
8455 
8456 /*
8457  * Create scsi target node for attached device, create node properties and
8458  * attach the node.
8459  * The node could be removed if the device onlining fails.
8460  *
8461  * A dev_info_t pointer is returned if operation is successful, NULL is
8462  * returned otherwise.
8463  *
8464  * No port multiplier support.
8465  */
8466 
8467 static dev_info_t *
8468 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst,
8469 			sata_address_t *sata_addr)
8470 {
8471 	dev_info_t *cdip = NULL;
8472 	int rval;
8473 	char *nname = NULL;
8474 	char **compatible = NULL;
8475 	int ncompatible;
8476 	struct scsi_inquiry inq;
8477 	sata_device_t sata_device;
8478 	sata_drive_info_t *sdinfo;
8479 	int target;
8480 	int i;
8481 
8482 	sata_device.satadev_rev = SATA_DEVICE_REV;
8483 	sata_device.satadev_addr = *sata_addr;
8484 
8485 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport)));
8486 
8487 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
8488 
8489 	target = SATA_TO_SCSI_TARGET(sata_addr->cport,
8490 	    sata_addr->pmport, sata_addr->qual);
8491 
8492 	if (sdinfo == NULL) {
8493 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8494 		    sata_addr->cport)));
8495 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8496 		    "sata_create_target_node: no sdinfo for target %x",
8497 		    target));
8498 		return (NULL);
8499 	}
8500 
8501 	/*
8502 	 * create or get scsi inquiry data, expected by
8503 	 * scsi_hba_nodename_compatible_get()
8504 	 * SATA hard disks get Identify Data translated into Inguiry Data.
8505 	 * ATAPI devices respond directly to Inquiry request.
8506 	 */
8507 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
8508 		sata_identdev_to_inquiry(sata_hba_inst, sdinfo,
8509 		    (uint8_t *)&inq);
8510 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8511 		    sata_addr->cport)));
8512 	} else { /* Assume supported ATAPI device */
8513 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8514 		    sata_addr->cport)));
8515 		if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr,
8516 		    &inq) == SATA_FAILURE)
8517 			return (NULL);
8518 		/*
8519 		 * Save supported ATAPI transport version
8520 		 */
8521 		sdinfo->satadrv_atapi_trans_ver =
8522 		    SATA_ATAPI_TRANS_VERSION(&inq);
8523 	}
8524 
8525 	/* determine the node name and compatible */
8526 	scsi_hba_nodename_compatible_get(&inq, NULL,
8527 	    inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
8528 
8529 #ifdef SATA_DEBUG
8530 	if (sata_debug_flags & SATA_DBG_NODES) {
8531 		if (nname == NULL) {
8532 			cmn_err(CE_NOTE, "sata_create_target_node: "
8533 			    "cannot determine nodename for target %d\n",
8534 			    target);
8535 		} else {
8536 			cmn_err(CE_WARN, "sata_create_target_node: "
8537 			    "target %d nodename: %s\n", target, nname);
8538 		}
8539 		if (compatible == NULL) {
8540 			cmn_err(CE_WARN,
8541 			    "sata_create_target_node: no compatible name\n");
8542 		} else {
8543 			for (i = 0; i < ncompatible; i++) {
8544 				cmn_err(CE_WARN, "sata_create_target_node: "
8545 				    "compatible name: %s\n", compatible[i]);
8546 			}
8547 		}
8548 	}
8549 #endif
8550 
8551 	/* if nodename can't be determined, log error and exit */
8552 	if (nname == NULL) {
8553 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8554 		    "sata_create_target_node: cannot determine nodename "
8555 		    "for target %d\n", target));
8556 		scsi_hba_nodename_compatible_free(nname, compatible);
8557 		return (NULL);
8558 	}
8559 	/*
8560 	 * Create scsi target node
8561 	 */
8562 	ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip);
8563 	rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
8564 	    "device-type", "scsi");
8565 
8566 	if (rval != DDI_PROP_SUCCESS) {
8567 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8568 		    "updating device_type prop failed %d", rval));
8569 		goto fail;
8570 	}
8571 
8572 	/*
8573 	 * Create target node properties: target & lun
8574 	 */
8575 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target);
8576 	if (rval != DDI_PROP_SUCCESS) {
8577 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8578 		    "updating target prop failed %d", rval));
8579 		goto fail;
8580 	}
8581 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0);
8582 	if (rval != DDI_PROP_SUCCESS) {
8583 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8584 		    "updating target prop failed %d", rval));
8585 		goto fail;
8586 	}
8587 
8588 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
8589 		/*
8590 		 * Add "variant" property
8591 		 */
8592 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
8593 		    "variant", "atapi");
8594 		if (rval != DDI_PROP_SUCCESS) {
8595 			SATA_LOG_D((sata_hba_inst, CE_WARN,
8596 			    "sata_create_target_node: variant atapi "
8597 			    "property could not be created: %d", rval));
8598 			goto fail;
8599 		}
8600 	}
8601 	/* decorate the node with compatible */
8602 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible",
8603 	    compatible, ncompatible) != DDI_PROP_SUCCESS) {
8604 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8605 		    "sata_create_target_node: FAIL compatible props cdip 0x%p",
8606 		    (void *)cdip));
8607 		goto fail;
8608 	}
8609 
8610 
8611 	/*
8612 	 * Now, try to attach the driver. If probing of the device fails,
8613 	 * the target node may be removed
8614 	 */
8615 	rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH);
8616 
8617 	scsi_hba_nodename_compatible_free(nname, compatible);
8618 
8619 	if (rval == NDI_SUCCESS)
8620 		return (cdip);
8621 
8622 	/* target node was removed - are we sure? */
8623 	return (NULL);
8624 
8625 fail:
8626 	scsi_hba_nodename_compatible_free(nname, compatible);
8627 	ddi_prop_remove_all(cdip);
8628 	rval = ndi_devi_free(cdip);
8629 	if (rval != NDI_SUCCESS) {
8630 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8631 		    "node removal failed %d", rval));
8632 	}
8633 	sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: "
8634 	    "cannot create target node for SATA device at port %d",
8635 	    sata_addr->cport);
8636 	return (NULL);
8637 }
8638 
8639 
8640 
8641 /*
8642  * Re-probe sata port, check for a device and attach info
8643  * structures when necessary. Identify Device data is fetched, if possible.
8644  * Assumption: sata address is already validated.
8645  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
8646  * the presence of a device and its type.
8647  *
8648  * flag arg specifies that the function should try multiple times to identify
8649  * device type and to initialize it, or it should return immediately on failure.
8650  * SATA_DEV_IDENTIFY_RETRY - retry
8651  * SATA_DEV_IDENTIFY_NORETRY - no retry
8652  *
8653  * SATA_FAILURE is returned if one of the operations failed.
8654  *
8655  * This function cannot be called in interrupt context - it may sleep.
8656  *
8657  * NOte: Port multiplier is not supported yet, although there may be some
8658  * pieces of code referencing to it.
8659  */
8660 static int
8661 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
8662     int flag)
8663 {
8664 	sata_cport_info_t *cportinfo;
8665 	sata_drive_info_t *sdinfo, *osdinfo;
8666 	boolean_t init_device = B_FALSE;
8667 	int prev_device_type = SATA_DTYPE_NONE;
8668 	int prev_device_settings = 0;
8669 	int prev_device_state = 0;
8670 	clock_t start_time;
8671 	int retry = B_FALSE;
8672 	int rval_probe, rval_init;
8673 
8674 	/* We only care about host sata cport for now */
8675 	cportinfo = SATA_CPORT_INFO(sata_hba_inst,
8676 	    sata_device->satadev_addr.cport);
8677 	osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8678 	if (osdinfo != NULL) {
8679 		/*
8680 		 * We are re-probing port with a previously attached device.
8681 		 * Save previous device type and settings.
8682 		 */
8683 		prev_device_type = cportinfo->cport_dev_type;
8684 		prev_device_settings = osdinfo->satadrv_settings;
8685 		prev_device_state = osdinfo->satadrv_state;
8686 	}
8687 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
8688 		start_time = ddi_get_lbolt();
8689 		retry = B_TRUE;
8690 	}
8691 retry_probe:
8692 
8693 	/* probe port */
8694 	mutex_enter(&cportinfo->cport_mutex);
8695 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
8696 	cportinfo->cport_state |= SATA_STATE_PROBING;
8697 	mutex_exit(&cportinfo->cport_mutex);
8698 
8699 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8700 	    (SATA_DIP(sata_hba_inst), sata_device);
8701 
8702 	mutex_enter(&cportinfo->cport_mutex);
8703 	if (rval_probe != SATA_SUCCESS) {
8704 		cportinfo->cport_state = SATA_PSTATE_FAILED;
8705 		mutex_exit(&cportinfo->cport_mutex);
8706 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: "
8707 		    "SATA port %d probing failed",
8708 		    cportinfo->cport_addr.cport));
8709 		return (SATA_FAILURE);
8710 	}
8711 
8712 	/*
8713 	 * update sata port state and set device type
8714 	 */
8715 	sata_update_port_info(sata_hba_inst, sata_device);
8716 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
8717 
8718 	/*
8719 	 * Sanity check - Port is active? Is the link active?
8720 	 * Is there any device attached?
8721 	 */
8722 	if ((cportinfo->cport_state &
8723 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
8724 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
8725 	    SATA_PORT_DEVLINK_UP) {
8726 		/*
8727 		 * Port in non-usable state or no link active/no device.
8728 		 * Free info structure if necessary (direct attached drive
8729 		 * only, for now!
8730 		 */
8731 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8732 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8733 		/* Add here differentiation for device attached or not */
8734 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8735 		mutex_exit(&cportinfo->cport_mutex);
8736 		if (sdinfo != NULL)
8737 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8738 		return (SATA_SUCCESS);
8739 	}
8740 
8741 	cportinfo->cport_state |= SATA_STATE_READY;
8742 	cportinfo->cport_dev_type = sata_device->satadev_type;
8743 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8744 
8745 	/*
8746 	 * If we are re-probing the port, there may be
8747 	 * sata_drive_info structure attached
8748 	 * (or sata_pm_info, if PMult is supported).
8749 	 */
8750 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
8751 		/*
8752 		 * There is no device, so remove device info structure,
8753 		 * if necessary.
8754 		 * Only direct attached drive is considered now, until
8755 		 * port multiplier is supported. If the previously
8756 		 * attached device was a port multiplier, we would need
8757 		 * to take care of devices attached beyond the port
8758 		 * multiplier.
8759 		 */
8760 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8761 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8762 		if (sdinfo != NULL) {
8763 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8764 			sata_log(sata_hba_inst, CE_WARN,
8765 			    "SATA device detached "
8766 			    "from port %d", cportinfo->cport_addr.cport);
8767 		}
8768 		mutex_exit(&cportinfo->cport_mutex);
8769 		return (SATA_SUCCESS);
8770 	}
8771 
8772 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
8773 		if (sdinfo == NULL) {
8774 			/*
8775 			 * There is some device attached, but there is
8776 			 * no sata_drive_info structure - allocate one
8777 			 */
8778 			mutex_exit(&cportinfo->cport_mutex);
8779 			sdinfo = kmem_zalloc(
8780 			    sizeof (sata_drive_info_t), KM_SLEEP);
8781 			mutex_enter(&cportinfo->cport_mutex);
8782 			/*
8783 			 * Recheck, that the port state did not change when we
8784 			 * released mutex.
8785 			 */
8786 			if (cportinfo->cport_state & SATA_STATE_READY) {
8787 				SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo;
8788 				sdinfo->satadrv_addr = cportinfo->cport_addr;
8789 				sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT;
8790 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
8791 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
8792 			} else {
8793 				/*
8794 				 * Port is not in ready state, we
8795 				 * cannot attach a device.
8796 				 */
8797 				mutex_exit(&cportinfo->cport_mutex);
8798 				kmem_free(sdinfo, sizeof (sata_drive_info_t));
8799 				return (SATA_SUCCESS);
8800 			}
8801 			/*
8802 			 * Since we are adding device, presumably new one,
8803 			 * indicate that it  should be initalized,
8804 			 * as well as some internal framework states).
8805 			 */
8806 			init_device = B_TRUE;
8807 		}
8808 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8809 		sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
8810 	} else {
8811 		/*
8812 		 * The device is a port multiplier - not handled now.
8813 		 */
8814 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8815 		mutex_exit(&cportinfo->cport_mutex);
8816 		return (SATA_SUCCESS);
8817 	}
8818 	mutex_exit(&cportinfo->cport_mutex);
8819 	/*
8820 	 * Figure out what kind of device we are really
8821 	 * dealing with. Failure of identifying device does not fail this
8822 	 * function.
8823 	 */
8824 	rval_probe = sata_probe_device(sata_hba_inst, sata_device);
8825 	rval_init = SATA_FAILURE;
8826 	mutex_enter(&cportinfo->cport_mutex);
8827 	if (rval_probe == SATA_SUCCESS) {
8828 		/*
8829 		 * If we are dealing with the same type of a device as before,
8830 		 * restore its settings flags.
8831 		 */
8832 		if (osdinfo != NULL &&
8833 		    sata_device->satadev_type == prev_device_type)
8834 			sdinfo->satadrv_settings = prev_device_settings;
8835 
8836 		mutex_exit(&cportinfo->cport_mutex);
8837 		rval_init = SATA_SUCCESS;
8838 		/* Set initial device features, if necessary */
8839 		if (init_device == B_TRUE) {
8840 			rval_init = sata_initialize_device(sata_hba_inst,
8841 			    sdinfo);
8842 		}
8843 		if (rval_init == SATA_SUCCESS)
8844 			return (rval_init);
8845 		/* else we will retry if retry was asked for */
8846 
8847 	} else {
8848 		/*
8849 		 * If there was some device info before we probe the device,
8850 		 * restore previous device setting, so we can retry from scratch
8851 		 * later. Providing, of course, that device has not disapear
8852 		 * during probing process.
8853 		 */
8854 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
8855 			if (osdinfo != NULL) {
8856 				cportinfo->cport_dev_type = prev_device_type;
8857 				sdinfo->satadrv_type = prev_device_type;
8858 				sdinfo->satadrv_state = prev_device_state;
8859 			}
8860 		} else {
8861 			/* device is gone */
8862 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8863 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8864 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8865 			mutex_exit(&cportinfo->cport_mutex);
8866 			return (SATA_SUCCESS);
8867 		}
8868 		mutex_exit(&cportinfo->cport_mutex);
8869 	}
8870 
8871 	if (retry) {
8872 		clock_t cur_time = ddi_get_lbolt();
8873 		/*
8874 		 * A device was not successfully identified or initialized.
8875 		 * Track retry time for device identification.
8876 		 */
8877 		if ((cur_time - start_time) <
8878 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
8879 			/* sleep for a while */
8880 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
8881 			goto retry_probe;
8882 		}
8883 		/* else no more retries */
8884 		mutex_enter(&cportinfo->cport_mutex);
8885 		if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
8886 			if (rval_init == SATA_RETRY) {
8887 				/*
8888 				 * Setting drive features have failed, but
8889 				 * because the drive is still accessible,
8890 				 * keep it and emit a warning message.
8891 				 */
8892 				sata_log(sata_hba_inst, CE_WARN,
8893 				    "SATA device at port %d - desired "
8894 				    "drive features could not be set. "
8895 				    "Device may not operate as expected.",
8896 				    cportinfo->cport_addr.cport);
8897 			} else {
8898 				SATA_CPORTINFO_DRV_INFO(cportinfo)->
8899 				    satadrv_state = SATA_DSTATE_FAILED;
8900 			}
8901 		}
8902 		mutex_exit(&cportinfo->cport_mutex);
8903 	}
8904 	return (SATA_SUCCESS);
8905 }
8906 
8907 /*
8908  * Initialize device
8909  * Specified device is initialized to a default state.
8910  *
8911  * Returns SATA_SUCCESS if all device features are set successfully,
8912  * SATA_RETRY if device is accessible but device features were not set
8913  * successfully, and SATA_FAILURE otherwise.
8914  */
8915 static int
8916 sata_initialize_device(sata_hba_inst_t *sata_hba_inst,
8917     sata_drive_info_t *sdinfo)
8918 {
8919 	int rval;
8920 
8921 	sata_save_drive_settings(sdinfo);
8922 
8923 	sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
8924 
8925 	sata_init_write_cache_mode(sdinfo);
8926 
8927 	rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0);
8928 
8929 	/* Determine current data transfer mode */
8930 	if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) {
8931 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
8932 	} else if ((sdinfo->satadrv_id.ai_validinfo &
8933 	    SATA_VALIDINFO_88) != 0 &&
8934 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) {
8935 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
8936 	} else if ((sdinfo->satadrv_id.ai_dworddma &
8937 	    SATA_MDMA_SEL_MASK) != 0) {
8938 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
8939 	} else
8940 		/* DMA supported, not no DMA transfer mode is selected !? */
8941 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
8942 
8943 	return (rval);
8944 }
8945 
8946 
8947 /*
8948  * Initialize write cache mode.
8949  *
8950  * The default write cache setting for SATA HDD is provided by sata_write_cache
8951  * static variable. ATAPI CD/DVDs devices have write cache default is
8952  * determined by sata_atapicdvd_write_cache static variable.
8953  * ATAPI tape devices have write cache default is determined by
8954  * sata_atapitape_write_cache static variable.
8955  * 1 - enable
8956  * 0 - disable
8957  * any other value - current drive setting
8958  *
8959  * Although there is not reason to disable write cache on CD/DVD devices
8960  * and tape devices, the default setting control is provided for the maximun
8961  * flexibility.
8962  *
8963  * In the future, it may be overridden by the
8964  * disk-write-cache-enable property setting, if it is defined.
8965  * Returns SATA_SUCCESS if all device features are set successfully,
8966  * SATA_FAILURE otherwise.
8967  */
8968 static void
8969 sata_init_write_cache_mode(sata_drive_info_t *sdinfo)
8970 {
8971 	switch (sdinfo->satadrv_type) {
8972 	case SATA_DTYPE_ATADISK:
8973 		if (sata_write_cache == 1)
8974 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
8975 		else if (sata_write_cache == 0)
8976 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
8977 		/*
8978 		 * When sata_write_cache value is not 0 or 1,
8979 		 * a current setting of the drive's write cache is used.
8980 		 */
8981 		break;
8982 	case SATA_DTYPE_ATAPICD:
8983 		if (sata_atapicdvd_write_cache == 1)
8984 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
8985 		else if (sata_atapicdvd_write_cache == 0)
8986 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
8987 		/*
8988 		 * When sata_write_cache value is not 0 or 1,
8989 		 * a current setting of the drive's write cache is used.
8990 		 */
8991 		break;
8992 	case SATA_DTYPE_ATAPITAPE:
8993 		if (sata_atapitape_write_cache == 1)
8994 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
8995 		else if (sata_atapitape_write_cache == 0)
8996 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
8997 		/*
8998 		 * When sata_write_cache value is not 0 or 1,
8999 		 * a current setting of the drive's write cache is used.
9000 		 */
9001 		break;
9002 	}
9003 }
9004 
9005 
9006 /*
9007  * Validate sata address.
9008  * Specified cport, pmport and qualifier has to match
9009  * passed sata_scsi configuration info.
9010  * The presence of an attached device is not verified.
9011  *
9012  * Returns 0 when address is valid, -1 otherwise.
9013  */
9014 static int
9015 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport,
9016 	int pmport, int qual)
9017 {
9018 	if (qual == SATA_ADDR_DCPORT && pmport != 0)
9019 		goto invalid_address;
9020 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
9021 		goto invalid_address;
9022 	if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) &&
9023 	    ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) ||
9024 	    (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) ||
9025 	    (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport))))
9026 		goto invalid_address;
9027 
9028 	return (0);
9029 
9030 invalid_address:
9031 	return (-1);
9032 
9033 }
9034 
9035 /*
9036  * Validate scsi address
9037  * SCSI target address is translated into SATA cport/pmport and compared
9038  * with a controller port/device configuration. LUN has to be 0.
9039  * Returns 0 if a scsi target refers to an attached device,
9040  * returns 1 if address is valid but device is not attached,
9041  * returns -1 if bad address or device is of an unsupported type.
9042  * Upon return sata_device argument is set.
9043  */
9044 static int
9045 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst,
9046 	struct scsi_address *ap, sata_device_t *sata_device)
9047 {
9048 	int cport, pmport, qual, rval;
9049 
9050 	rval = -1;	/* Invalid address */
9051 	if (ap->a_lun != 0)
9052 		goto out;
9053 
9054 	qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
9055 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
9056 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
9057 
9058 	if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT)
9059 		goto out;
9060 
9061 	if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) ==
9062 	    0) {
9063 
9064 		sata_cport_info_t *cportinfo;
9065 		sata_pmult_info_t *pmultinfo;
9066 		sata_drive_info_t *sdinfo = NULL;
9067 
9068 		rval = 1;	/* Valid sata address */
9069 
9070 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
9071 		if (qual == SATA_ADDR_DCPORT) {
9072 			if (cportinfo == NULL ||
9073 			    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
9074 				goto out;
9075 
9076 			if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT ||
9077 			    (cportinfo->cport_dev_type &
9078 			    SATA_VALID_DEV_TYPE) == 0) {
9079 				rval = -1;
9080 				goto out;
9081 			}
9082 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
9083 
9084 		} else if (qual == SATA_ADDR_DPMPORT) {
9085 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
9086 			if (pmultinfo == NULL) {
9087 				rval = -1;
9088 				goto out;
9089 			}
9090 			if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) ==
9091 			    NULL ||
9092 			    SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
9093 			    pmport) == SATA_DTYPE_NONE)
9094 				goto out;
9095 
9096 			sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport,
9097 			    pmport);
9098 		} else {
9099 			rval = -1;
9100 			goto out;
9101 		}
9102 		if ((sdinfo == NULL) ||
9103 		    (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0)
9104 			goto out;
9105 
9106 		sata_device->satadev_type = sdinfo->satadrv_type;
9107 		sata_device->satadev_addr.qual = qual;
9108 		sata_device->satadev_addr.cport = cport;
9109 		sata_device->satadev_addr.pmport = pmport;
9110 		sata_device->satadev_rev = SATA_DEVICE_REV_1;
9111 		return (0);
9112 	}
9113 out:
9114 	if (rval == 1) {
9115 		SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
9116 		    "sata_validate_scsi_address: no valid target %x lun %x",
9117 		    ap->a_target, ap->a_lun);
9118 	}
9119 	return (rval);
9120 }
9121 
9122 /*
9123  * Find dip corresponding to passed device number
9124  *
9125  * Returns NULL if invalid device number is passed or device cannot be found,
9126  * Returns dip is device is found.
9127  */
9128 static dev_info_t *
9129 sata_devt_to_devinfo(dev_t dev)
9130 {
9131 	dev_info_t *dip;
9132 #ifndef __lock_lint
9133 	struct devnames *dnp;
9134 	major_t major = getmajor(dev);
9135 	int instance = SATA_MINOR2INSTANCE(getminor(dev));
9136 
9137 	if (major >= devcnt)
9138 		return (NULL);
9139 
9140 	dnp = &devnamesp[major];
9141 	LOCK_DEV_OPS(&(dnp->dn_lock));
9142 	dip = dnp->dn_head;
9143 	while (dip && (ddi_get_instance(dip) != instance)) {
9144 		dip = ddi_get_next(dip);
9145 	}
9146 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
9147 #endif
9148 
9149 	return (dip);
9150 }
9151 
9152 
9153 /*
9154  * Probe device.
9155  * This function issues Identify Device command and initializes local
9156  * sata_drive_info structure if the device can be identified.
9157  * The device type is determined by examining Identify Device
9158  * command response.
9159  * If the sata_hba_inst has linked drive info structure for this
9160  * device address, the Identify Device data is stored into sata_drive_info
9161  * structure linked to the port info structure.
9162  *
9163  * sata_device has to refer to the valid sata port(s) for HBA described
9164  * by sata_hba_inst structure.
9165  *
9166  * Returns:
9167  *	SATA_SUCCESS if device type was successfully probed and port-linked
9168  *		drive info structure was updated;
9169  * 	SATA_FAILURE if there is no device, or device was not probed
9170  *		successully;
9171  *	SATA_RETRY if device probe can be retried later.
9172  * If a device cannot be identified, sata_device's dev_state and dev_type
9173  * fields are set to unknown.
9174  * There are no retries in this function. Any retries should be managed by
9175  * the caller.
9176  */
9177 
9178 
9179 static int
9180 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
9181 {
9182 	sata_drive_info_t *sdinfo;
9183 	sata_drive_info_t new_sdinfo;	/* local drive info struct */
9184 	int rval;
9185 
9186 	ASSERT((SATA_CPORT_STATE(sata_hba_inst,
9187 	    sata_device->satadev_addr.cport) &
9188 	    (SATA_STATE_PROBED | SATA_STATE_READY)) != 0);
9189 
9190 	sata_device->satadev_type = SATA_DTYPE_NONE;
9191 
9192 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9193 	    sata_device->satadev_addr.cport)));
9194 
9195 	/* Get pointer to port-linked sata device info structure */
9196 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9197 	if (sdinfo != NULL) {
9198 		sdinfo->satadrv_state &=
9199 		    ~(SATA_STATE_PROBED | SATA_STATE_READY);
9200 		sdinfo->satadrv_state |= SATA_STATE_PROBING;
9201 	} else {
9202 		/* No device to probe */
9203 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9204 		    sata_device->satadev_addr.cport)));
9205 		sata_device->satadev_type = SATA_DTYPE_NONE;
9206 		sata_device->satadev_state = SATA_STATE_UNKNOWN;
9207 		return (SATA_FAILURE);
9208 	}
9209 	/*
9210 	 * Need to issue both types of identify device command and
9211 	 * determine device type by examining retreived data/status.
9212 	 * First, ATA Identify Device.
9213 	 */
9214 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
9215 	new_sdinfo.satadrv_addr = sata_device->satadev_addr;
9216 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9217 	    sata_device->satadev_addr.cport)));
9218 	new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
9219 	rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
9220 	if (rval == SATA_RETRY) {
9221 		/* We may try to check for ATAPI device */
9222 		if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) {
9223 			/*
9224 			 * HBA supports ATAPI - try to issue Identify Packet
9225 			 * Device command.
9226 			 */
9227 			new_sdinfo.satadrv_type = SATA_DTYPE_ATAPI;
9228 			rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
9229 		}
9230 	}
9231 	if (rval == SATA_SUCCESS) {
9232 		/*
9233 		 * Got something responding positively to ATA Identify Device
9234 		 * or to Identify Packet Device cmd.
9235 		 * Save last used device type.
9236 		 */
9237 		sata_device->satadev_type = new_sdinfo.satadrv_type;
9238 
9239 		/* save device info, if possible */
9240 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9241 		    sata_device->satadev_addr.cport)));
9242 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9243 		if (sdinfo == NULL) {
9244 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9245 			    sata_device->satadev_addr.cport)));
9246 			return (SATA_FAILURE);
9247 		}
9248 		/*
9249 		 * Copy drive info into the port-linked drive info structure.
9250 		 */
9251 		*sdinfo = new_sdinfo;
9252 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
9253 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
9254 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
9255 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
9256 			    sata_device->satadev_addr.cport) =
9257 			    sdinfo->satadrv_type;
9258 		else /* SATA_ADDR_DPMPORT */
9259 			SATA_PMPORT_DEV_TYPE(sata_hba_inst,
9260 			    sata_device->satadev_addr.cport,
9261 			    sata_device->satadev_addr.pmport) =
9262 			    sdinfo->satadrv_type;
9263 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9264 		    sata_device->satadev_addr.cport)));
9265 		return (SATA_SUCCESS);
9266 	}
9267 
9268 	/*
9269 	 * It may be SATA_RETRY or SATA_FAILURE return.
9270 	 * Looks like we cannot determine the device type at this time.
9271 	 */
9272 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9273 	    sata_device->satadev_addr.cport)));
9274 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9275 	if (sdinfo != NULL) {
9276 		sata_device->satadev_type = SATA_DTYPE_UNKNOWN;
9277 		sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9278 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
9279 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
9280 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
9281 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
9282 			    sata_device->satadev_addr.cport) =
9283 			    SATA_DTYPE_UNKNOWN;
9284 		else {
9285 			/* SATA_ADDR_DPMPORT */
9286 			if ((SATA_PMULT_INFO(sata_hba_inst,
9287 			    sata_device->satadev_addr.cport) != NULL) &&
9288 			    (SATA_PMPORT_INFO(sata_hba_inst,
9289 			    sata_device->satadev_addr.cport,
9290 			    sata_device->satadev_addr.pmport) != NULL))
9291 				SATA_PMPORT_DEV_TYPE(sata_hba_inst,
9292 				    sata_device->satadev_addr.cport,
9293 				    sata_device->satadev_addr.pmport) =
9294 				    SATA_DTYPE_UNKNOWN;
9295 		}
9296 	}
9297 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9298 	    sata_device->satadev_addr.cport)));
9299 	return (rval);
9300 }
9301 
9302 
9303 /*
9304  * Get pointer to sata_drive_info structure.
9305  *
9306  * The sata_device has to contain address (cport, pmport and qualifier) for
9307  * specified sata_scsi structure.
9308  *
9309  * Returns NULL if device address is not valid for this HBA configuration.
9310  * Otherwise, returns a pointer to sata_drive_info structure.
9311  *
9312  * This function should be called with a port mutex held.
9313  */
9314 static sata_drive_info_t *
9315 sata_get_device_info(sata_hba_inst_t *sata_hba_inst,
9316     sata_device_t *sata_device)
9317 {
9318 	uint8_t cport = sata_device->satadev_addr.cport;
9319 	uint8_t pmport = sata_device->satadev_addr.pmport;
9320 	uint8_t qual = sata_device->satadev_addr.qual;
9321 
9322 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
9323 		return (NULL);
9324 
9325 	if (!(SATA_CPORT_STATE(sata_hba_inst, cport) &
9326 	    (SATA_STATE_PROBED | SATA_STATE_READY)))
9327 		/* Port not probed yet */
9328 		return (NULL);
9329 
9330 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE)
9331 		return (NULL);
9332 
9333 	if (qual == SATA_ADDR_DCPORT) {
9334 		/* Request for a device on a controller port */
9335 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
9336 		    SATA_DTYPE_PMULT)
9337 			/* Port multiplier attached */
9338 			return (NULL);
9339 		return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport));
9340 	}
9341 	if (qual == SATA_ADDR_DPMPORT) {
9342 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
9343 		    SATA_DTYPE_PMULT)
9344 			return (NULL);
9345 
9346 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport))
9347 			return (NULL);
9348 
9349 		return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport));
9350 	}
9351 
9352 	/* we should not get here */
9353 	return (NULL);
9354 }
9355 
9356 
9357 /*
9358  * sata_identify_device.
9359  * Send Identify Device command to SATA HBA driver.
9360  * If command executes successfully, update sata_drive_info structure pointed
9361  * to by sdinfo argument, including Identify Device data.
9362  * If command fails, invalidate data in sata_drive_info.
9363  *
9364  * Cannot be called from interrupt level.
9365  *
9366  * Returns:
9367  * SATA_SUCCESS if the device was identified as a supported device,
9368  * SATA_RETRY if the device was not identified but could be retried,
9369  * SATA_FAILURE if the device was not identified and identify attempt
9370  *	should not be retried.
9371  */
9372 static int
9373 sata_identify_device(sata_hba_inst_t *sata_hba_inst,
9374     sata_drive_info_t *sdinfo)
9375 {
9376 	uint16_t cfg_word;
9377 	int rval;
9378 
9379 	/* fetch device identify data */
9380 	if ((rval = sata_fetch_device_identify_data(sata_hba_inst,
9381 	    sdinfo)) != SATA_SUCCESS)
9382 		goto fail_unknown;
9383 
9384 	cfg_word = sdinfo->satadrv_id.ai_config;
9385 
9386 	/* Set the correct device type */
9387 	if ((cfg_word & SATA_ATA_TYPE_MASK) == SATA_ATA_TYPE) {
9388 		sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
9389 	} else if ((cfg_word & SATA_ATAPI_TYPE_MASK) == SATA_ATAPI_TYPE) {
9390 		switch (cfg_word & SATA_ATAPI_ID_DEV_TYPE) {
9391 		case SATA_ATAPI_CDROM_DEV:
9392 			sdinfo->satadrv_type = SATA_DTYPE_ATAPICD;
9393 			break;
9394 		case SATA_ATAPI_SQACC_DEV:
9395 			sdinfo->satadrv_type = SATA_DTYPE_ATAPITAPE;
9396 			break;
9397 		default:
9398 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9399 		}
9400 	} else {
9401 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9402 	}
9403 
9404 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9405 		if (sdinfo->satadrv_capacity == 0) {
9406 			/* Non-LBA disk. Too bad... */
9407 			sata_log(sata_hba_inst, CE_WARN,
9408 			    "SATA disk device at port %d does not support LBA",
9409 			    sdinfo->satadrv_addr.cport);
9410 			rval = SATA_FAILURE;
9411 			goto fail_unknown;
9412 		}
9413 	}
9414 #if 0
9415 	/* Left for historical reason */
9416 	/*
9417 	 * Some initial version of SATA spec indicated that at least
9418 	 * UDMA mode 4 has to be supported. It is not metioned in
9419 	 * SerialATA 2.6, so this restriction is removed.
9420 	 */
9421 	/* Check for Ultra DMA modes 6 through 0 being supported */
9422 	for (i = 6; i >= 0; --i) {
9423 		if (sdinfo->satadrv_id.ai_ultradma & (1 << i))
9424 			break;
9425 	}
9426 
9427 	/*
9428 	 * At least UDMA 4 mode has to be supported. If mode 4 or
9429 	 * higher are not supported by the device, fail this
9430 	 * device.
9431 	 */
9432 	if (i < 4) {
9433 		/* No required Ultra DMA mode supported */
9434 		sata_log(sata_hba_inst, CE_WARN,
9435 		    "SATA disk device at port %d does not support UDMA "
9436 		    "mode 4 or higher", sdinfo->satadrv_addr.cport);
9437 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9438 		    "mode 4 or higher required, %d supported", i));
9439 		rval = SATA_FAILURE;
9440 		goto fail_unknown;
9441 	}
9442 #endif
9443 
9444 	return (SATA_SUCCESS);
9445 
9446 fail_unknown:
9447 	/* Invalidate sata_drive_info ? */
9448 	sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9449 	sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
9450 	return (rval);
9451 }
9452 
9453 /*
9454  * Log/display device information
9455  */
9456 static void
9457 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst,
9458     sata_drive_info_t *sdinfo)
9459 {
9460 	int valid_version;
9461 	char msg_buf[MAXPATHLEN];
9462 	int i;
9463 
9464 	/* Show HBA path */
9465 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf);
9466 
9467 	cmn_err(CE_CONT, "?%s :\n", msg_buf);
9468 
9469 	switch (sdinfo->satadrv_type) {
9470 	case SATA_DTYPE_ATADISK:
9471 		(void) sprintf(msg_buf, "SATA disk device at");
9472 		break;
9473 
9474 	case SATA_DTYPE_ATAPICD:
9475 		(void) sprintf(msg_buf, "SATA CD/DVD (ATAPI) device at");
9476 		break;
9477 
9478 	case SATA_DTYPE_ATAPITAPE:
9479 		(void) sprintf(msg_buf, "SATA tape (ATAPI) device at");
9480 		break;
9481 
9482 	case SATA_DTYPE_UNKNOWN:
9483 		(void) sprintf(msg_buf,
9484 		    "Unsupported SATA device type (cfg 0x%x) at ",
9485 		    sdinfo->satadrv_id.ai_config);
9486 		break;
9487 	}
9488 
9489 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT)
9490 		cmn_err(CE_CONT, "?\t%s port %d\n",
9491 		    msg_buf, sdinfo->satadrv_addr.cport);
9492 	else
9493 		cmn_err(CE_CONT, "?\t%s port %d pmport %d\n",
9494 		    msg_buf, sdinfo->satadrv_addr.cport,
9495 		    sdinfo->satadrv_addr.pmport);
9496 
9497 	bcopy(&sdinfo->satadrv_id.ai_model, msg_buf,
9498 	    sizeof (sdinfo->satadrv_id.ai_model));
9499 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model));
9500 	msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
9501 	cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf);
9502 
9503 	bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf,
9504 	    sizeof (sdinfo->satadrv_id.ai_fw));
9505 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw));
9506 	msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
9507 	cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf);
9508 
9509 	bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf,
9510 	    sizeof (sdinfo->satadrv_id.ai_drvser));
9511 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser));
9512 	msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
9513 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9514 		cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
9515 	} else {
9516 		/*
9517 		 * Some drives do not implement serial number and may
9518 		 * violate the spec by providing spaces rather than zeros
9519 		 * in serial number field. Scan the buffer to detect it.
9520 		 */
9521 		for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) {
9522 			if (msg_buf[i] != '\0' && msg_buf[i] != ' ')
9523 				break;
9524 		}
9525 		if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) {
9526 			cmn_err(CE_CONT, "?\tserial number - none\n");
9527 		} else {
9528 			cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
9529 		}
9530 	}
9531 
9532 #ifdef SATA_DEBUG
9533 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
9534 	    sdinfo->satadrv_id.ai_majorversion != 0xffff) {
9535 		int i;
9536 		for (i = 14; i >= 2; i--) {
9537 			if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) {
9538 				valid_version = i;
9539 				break;
9540 			}
9541 		}
9542 		cmn_err(CE_CONT,
9543 		    "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
9544 		    valid_version,
9545 		    sdinfo->satadrv_id.ai_majorversion,
9546 		    sdinfo->satadrv_id.ai_minorversion);
9547 	}
9548 #endif
9549 	/* Log some info */
9550 	cmn_err(CE_CONT, "?\tsupported features:\n");
9551 	msg_buf[0] = '\0';
9552 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9553 		if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48)
9554 			(void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN);
9555 		else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28)
9556 			(void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN);
9557 	}
9558 	if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA)
9559 		(void) strlcat(msg_buf, "DMA", MAXPATHLEN);
9560 	if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ)
9561 		(void) strlcat(msg_buf, ", Native Command Queueing",
9562 		    MAXPATHLEN);
9563 	if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)
9564 		(void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN);
9565 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) &&
9566 	    (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED))
9567 		(void) strlcat(msg_buf, ", SMART", MAXPATHLEN);
9568 	if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) &&
9569 	    (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED))
9570 		(void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN);
9571 	cmn_err(CE_CONT, "?\t %s\n", msg_buf);
9572 	if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2)
9573 		cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n");
9574 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1)
9575 		cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n");
9576 	if (sdinfo->satadrv_features_support &
9577 	    (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) {
9578 		msg_buf[0] = '\0';
9579 		(void) snprintf(msg_buf, MAXPATHLEN,
9580 		    "Supported queue depth %d",
9581 		    sdinfo->satadrv_queue_depth);
9582 		if (!(sata_func_enable &
9583 		    (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ)))
9584 			(void) strlcat(msg_buf,
9585 			    " - queueing disabled globally", MAXPATHLEN);
9586 		else if (sdinfo->satadrv_queue_depth >
9587 		    sdinfo->satadrv_max_queue_depth) {
9588 			(void) snprintf(&msg_buf[strlen(msg_buf)],
9589 			    MAXPATHLEN - strlen(msg_buf), ", limited to %d",
9590 			    (int)sdinfo->satadrv_max_queue_depth);
9591 		}
9592 		cmn_err(CE_CONT, "?\t%s\n", msg_buf);
9593 	}
9594 
9595 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9596 #ifdef __i386
9597 		(void) sprintf(msg_buf, "\tcapacity = %llu sectors\n",
9598 		    sdinfo->satadrv_capacity);
9599 #else
9600 		(void) sprintf(msg_buf, "\tcapacity = %lu sectors\n",
9601 		    sdinfo->satadrv_capacity);
9602 #endif
9603 		cmn_err(CE_CONT, "?%s", msg_buf);
9604 	}
9605 }
9606 
9607 
9608 /*
9609  * sata_save_drive_settings extracts current setting of the device and stores
9610  * it for future reference, in case the device setup would need to be restored
9611  * after the device reset.
9612  *
9613  * For all devices read ahead and write cache settings are saved, if the
9614  * device supports these features at all.
9615  * For ATAPI devices the Removable Media Status Notification setting is saved.
9616  */
9617 static void
9618 sata_save_drive_settings(sata_drive_info_t *sdinfo)
9619 {
9620 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) ||
9621 	    (sdinfo->satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) {
9622 
9623 		/* Current setting of Read Ahead (and Read Cache) */
9624 		if (sdinfo->satadrv_id.ai_features85 & SATA_LOOK_AHEAD)
9625 			sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
9626 		else
9627 			sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD;
9628 
9629 		/* Current setting of Write Cache */
9630 		if (sdinfo->satadrv_id.ai_features85 & SATA_WRITE_CACHE)
9631 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
9632 		else
9633 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
9634 	}
9635 
9636 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
9637 		if (sdinfo->satadrv_id.ai_cmdset83 & SATA_RM_STATUS_NOTIFIC)
9638 			sdinfo->satadrv_settings |= SATA_DEV_RMSN;
9639 		else
9640 			sdinfo->satadrv_settings &= ~SATA_DEV_RMSN;
9641 	}
9642 }
9643 
9644 
9645 /*
9646  * sata_check_capacity function determines a disk capacity
9647  * and addressing mode (LBA28/LBA48) by examining a disk identify device data.
9648  *
9649  * NOTE: CHS mode is not supported! If a device does not support LBA,
9650  * this function is not called.
9651  *
9652  * Returns device capacity in number of blocks, i.e. largest addressable LBA+1
9653  */
9654 static uint64_t
9655 sata_check_capacity(sata_drive_info_t *sdinfo)
9656 {
9657 	uint64_t capacity = 0;
9658 	int i;
9659 
9660 	if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK ||
9661 	    !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT)
9662 		/* Capacity valid only for LBA-addressable disk devices */
9663 		return (0);
9664 
9665 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) &&
9666 	    (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) &&
9667 	    (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) {
9668 		/* LBA48 mode supported and enabled */
9669 		sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 |
9670 		    SATA_DEV_F_LBA28;
9671 		for (i = 3;  i >= 0;  --i) {
9672 			capacity <<= 16;
9673 			capacity += sdinfo->satadrv_id.ai_addrsecxt[i];
9674 		}
9675 	} else {
9676 		capacity = sdinfo->satadrv_id.ai_addrsec[1];
9677 		capacity <<= 16;
9678 		capacity += sdinfo->satadrv_id.ai_addrsec[0];
9679 		if (capacity >= 0x1000000)
9680 			/* LBA28 mode */
9681 			sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28;
9682 	}
9683 	return (capacity);
9684 }
9685 
9686 
9687 /*
9688  * Allocate consistent buffer for DMA transfer
9689  *
9690  * Cannot be called from interrupt level or with mutex held - it may sleep.
9691  *
9692  * Returns pointer to allocated buffer structure, or NULL if allocation failed.
9693  */
9694 static struct buf *
9695 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len)
9696 {
9697 	struct scsi_address ap;
9698 	struct buf *bp;
9699 	ddi_dma_attr_t	cur_dma_attr;
9700 
9701 	ASSERT(spx->txlt_sata_pkt != NULL);
9702 	ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran;
9703 	ap.a_target = SATA_TO_SCSI_TARGET(
9704 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport,
9705 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport,
9706 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual);
9707 	ap.a_lun = 0;
9708 
9709 	bp = scsi_alloc_consistent_buf(&ap, NULL, len,
9710 	    B_READ, SLEEP_FUNC, NULL);
9711 
9712 	if (bp != NULL) {
9713 		/* Allocate DMA resources for this buffer */
9714 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
9715 		/*
9716 		 * We use a local version of the dma_attr, to account
9717 		 * for a device addressing limitations.
9718 		 * sata_adjust_dma_attr() will handle sdinfo == NULL which
9719 		 * will cause dma attributes to be adjusted to a lowest
9720 		 * acceptable level.
9721 		 */
9722 		sata_adjust_dma_attr(NULL,
9723 		    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
9724 
9725 		if (sata_dma_buf_setup(spx, PKT_CONSISTENT,
9726 		    SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) {
9727 			scsi_free_consistent_buf(bp);
9728 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
9729 			bp = NULL;
9730 		}
9731 	}
9732 	return (bp);
9733 }
9734 
9735 /*
9736  * Release local buffer (consistent buffer for DMA transfer) allocated
9737  * via sata_alloc_local_buffer().
9738  */
9739 static void
9740 sata_free_local_buffer(sata_pkt_txlate_t *spx)
9741 {
9742 	ASSERT(spx->txlt_sata_pkt != NULL);
9743 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL);
9744 
9745 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0;
9746 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL;
9747 
9748 	if (spx->txlt_buf_dma_handle != NULL) {
9749 		/* Free DMA resources */
9750 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
9751 		ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
9752 		spx->txlt_buf_dma_handle = 0;
9753 
9754 		if (spx->txlt_dma_cookie_list != &spx->txlt_dma_cookie) {
9755 			kmem_free(spx->txlt_dma_cookie_list,
9756 			    spx->txlt_dma_cookie_list_len *
9757 			    sizeof (ddi_dma_cookie_t));
9758 			spx->txlt_dma_cookie_list = NULL;
9759 			spx->txlt_dma_cookie_list_len = 0;
9760 		}
9761 	}
9762 
9763 	/* Free buffer */
9764 	scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp);
9765 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
9766 }
9767 
9768 
9769 
9770 
9771 /*
9772  * Allocate sata_pkt
9773  * Pkt structure version and embedded strcutures version are initialized.
9774  * sata_pkt and sata_pkt_txlate structures are cross-linked.
9775  *
9776  * Since this may be called in interrupt context by sata_scsi_init_pkt,
9777  * callback argument determines if it can sleep or not.
9778  * Hence, it should not be called from interrupt context.
9779  *
9780  * If successful, non-NULL pointer to a sata pkt is returned.
9781  * Upon failure, NULL pointer is returned.
9782  */
9783 static sata_pkt_t *
9784 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t))
9785 {
9786 	sata_pkt_t *spkt;
9787 	int kmsflag;
9788 
9789 	kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
9790 	spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag);
9791 	if (spkt == NULL) {
9792 		SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9793 		    "sata_pkt_alloc: failed"));
9794 		return (NULL);
9795 	}
9796 	spkt->satapkt_rev = SATA_PKT_REV;
9797 	spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV;
9798 	spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
9799 	spkt->satapkt_framework_private = spx;
9800 	spx->txlt_sata_pkt = spkt;
9801 	return (spkt);
9802 }
9803 
9804 /*
9805  * Free sata pkt allocated via sata_pkt_alloc()
9806  */
9807 static void
9808 sata_pkt_free(sata_pkt_txlate_t *spx)
9809 {
9810 	ASSERT(spx->txlt_sata_pkt != NULL);
9811 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL);
9812 	kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t));
9813 	spx->txlt_sata_pkt = NULL;
9814 }
9815 
9816 
9817 /*
9818  * Adjust DMA attributes.
9819  * SCSI cmds block count is up to 24 bits, SATA cmd block count vary
9820  * from 8 bits to 16 bits, depending on a command being used.
9821  * Limiting max block count arbitrarily to 256 for all read/write
9822  * commands may affects performance, so check both the device and
9823  * controller capability before adjusting dma attributes.
9824  */
9825 void
9826 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr,
9827     ddi_dma_attr_t *adj_dma_attr)
9828 {
9829 	uint32_t count_max;
9830 
9831 	/* Copy original attributes */
9832 	*adj_dma_attr = *dma_attr;
9833 	/*
9834 	 * Things to consider: device addressing capability,
9835 	 * "excessive" controller DMA capabilities.
9836 	 * If a device is being probed/initialized, there are
9837 	 * no device info - use default limits then.
9838 	 */
9839 	if (sdinfo == NULL) {
9840 		count_max = dma_attr->dma_attr_granular * 0x100;
9841 		if (dma_attr->dma_attr_count_max > count_max)
9842 			adj_dma_attr->dma_attr_count_max = count_max;
9843 		if (dma_attr->dma_attr_maxxfer > count_max)
9844 			adj_dma_attr->dma_attr_maxxfer = count_max;
9845 		return;
9846 	}
9847 
9848 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9849 		if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) {
9850 			/*
9851 			 * 16-bit sector count may be used - we rely on
9852 			 * the assumption that only read and write cmds
9853 			 * will request more than 256 sectors worth of data
9854 			 */
9855 			count_max = adj_dma_attr->dma_attr_granular * 0x10000;
9856 		} else {
9857 			/*
9858 			 * 8-bit sector count will be used - default limits
9859 			 * for dma attributes
9860 			 */
9861 			count_max = adj_dma_attr->dma_attr_granular * 0x100;
9862 		}
9863 		/*
9864 		 * Adjust controler dma attributes, if necessary
9865 		 */
9866 		if (dma_attr->dma_attr_count_max > count_max)
9867 			adj_dma_attr->dma_attr_count_max = count_max;
9868 		if (dma_attr->dma_attr_maxxfer > count_max)
9869 			adj_dma_attr->dma_attr_maxxfer = count_max;
9870 	}
9871 }
9872 
9873 
9874 /*
9875  * Allocate DMA resources for the buffer
9876  * This function handles initial DMA resource allocation as well as
9877  * DMA window shift and may be called repeatedly for the same DMA window
9878  * until all DMA cookies in the DMA window are processed.
9879  * To guarantee that there is always a coherent set of cookies to process
9880  * by SATA HBA driver (observing alignment, device granularity, etc.),
9881  * the number of slots for DMA cookies is equal to lesser of  a number of
9882  * cookies in a DMA window and a max number of scatter/gather entries.
9883  *
9884  * Returns DDI_SUCCESS upon successful operation.
9885  * Return failure code of a failing command or DDI_FAILURE when
9886  * internal cleanup failed.
9887  */
9888 static int
9889 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags,
9890     int (*callback)(caddr_t), caddr_t arg,
9891     ddi_dma_attr_t *cur_dma_attr)
9892 {
9893 	int	rval;
9894 	off_t	offset;
9895 	size_t	size;
9896 	int	max_sg_len, req_len, i;
9897 	uint_t	dma_flags;
9898 	struct buf	*bp;
9899 	uint64_t	cur_txfer_len;
9900 
9901 
9902 	ASSERT(spx->txlt_sata_pkt != NULL);
9903 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9904 	ASSERT(bp != NULL);
9905 
9906 
9907 	if (spx->txlt_buf_dma_handle == NULL) {
9908 		/*
9909 		 * No DMA resources allocated so far - this is a first call
9910 		 * for this sata pkt.
9911 		 */
9912 		rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst),
9913 		    cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle);
9914 
9915 		if (rval != DDI_SUCCESS) {
9916 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9917 			    "sata_dma_buf_setup: no buf DMA resources %x",
9918 			    rval));
9919 			return (rval);
9920 		}
9921 
9922 		if (bp->b_flags & B_READ)
9923 			dma_flags = DDI_DMA_READ;
9924 		else
9925 			dma_flags = DDI_DMA_WRITE;
9926 
9927 		if (flags & PKT_CONSISTENT)
9928 			dma_flags |= DDI_DMA_CONSISTENT;
9929 
9930 		if (flags & PKT_DMA_PARTIAL)
9931 			dma_flags |= DDI_DMA_PARTIAL;
9932 
9933 		/*
9934 		 * Check buffer alignment and size against dma attributes
9935 		 * Consider dma_attr_align only. There may be requests
9936 		 * with the size lower than device granularity, but they
9937 		 * will not read/write from/to the device, so no adjustment
9938 		 * is necessary. The dma_attr_minxfer theoretically should
9939 		 * be considered, but no HBA driver is checking it.
9940 		 */
9941 		if (IS_P2ALIGNED(bp->b_un.b_addr,
9942 		    cur_dma_attr->dma_attr_align)) {
9943 			rval = ddi_dma_buf_bind_handle(
9944 			    spx->txlt_buf_dma_handle,
9945 			    bp, dma_flags, callback, arg,
9946 			    &spx->txlt_dma_cookie,
9947 			    &spx->txlt_curwin_num_dma_cookies);
9948 		} else { /* Buffer is not aligned */
9949 
9950 			int	(*ddicallback)(caddr_t);
9951 			size_t	bufsz;
9952 
9953 			/* Check id sleeping is allowed */
9954 			ddicallback = (callback == NULL_FUNC) ?
9955 			    DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
9956 
9957 			SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
9958 			    "mis-aligned buffer: addr=0x%p, cnt=%lu",
9959 			    (void *)bp->b_un.b_addr, bp->b_bcount);
9960 
9961 			if (bp->b_flags & (B_PAGEIO|B_PHYS))
9962 				/*
9963 				 * CPU will need to access data in the buffer
9964 				 * (for copying) so map it.
9965 				 */
9966 				bp_mapin(bp);
9967 
9968 			ASSERT(spx->txlt_tmp_buf == NULL);
9969 
9970 			/* Buffer may be padded by ddi_dma_mem_alloc()! */
9971 			rval = ddi_dma_mem_alloc(
9972 			    spx->txlt_buf_dma_handle,
9973 			    bp->b_bcount,
9974 			    &sata_acc_attr,
9975 			    DDI_DMA_STREAMING,
9976 			    ddicallback, NULL,
9977 			    &spx->txlt_tmp_buf,
9978 			    &bufsz,
9979 			    &spx->txlt_tmp_buf_handle);
9980 
9981 			if (rval != DDI_SUCCESS) {
9982 				/* DMA mapping failed */
9983 				(void) ddi_dma_free_handle(
9984 				    &spx->txlt_buf_dma_handle);
9985 				spx->txlt_buf_dma_handle = NULL;
9986 #ifdef SATA_DEBUG
9987 				mbuffail_count++;
9988 #endif
9989 				SATADBG1(SATA_DBG_DMA_SETUP,
9990 				    spx->txlt_sata_hba_inst,
9991 				    "sata_dma_buf_setup: "
9992 				    "buf dma mem alloc failed %x\n", rval);
9993 				return (rval);
9994 			}
9995 			ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf,
9996 			    cur_dma_attr->dma_attr_align));
9997 
9998 #ifdef SATA_DEBUG
9999 			mbuf_count++;
10000 
10001 			if (bp->b_bcount != bufsz)
10002 				/*
10003 				 * This will require special handling, because
10004 				 * DMA cookies will be based on the temporary
10005 				 * buffer size, not the original buffer
10006 				 * b_bcount, so the residue may have to
10007 				 * be counted differently.
10008 				 */
10009 				SATADBG2(SATA_DBG_DMA_SETUP,
10010 				    spx->txlt_sata_hba_inst,
10011 				    "sata_dma_buf_setup: bp size %x != "
10012 				    "bufsz %x\n", bp->b_bcount, bufsz);
10013 #endif
10014 			if (dma_flags & DDI_DMA_WRITE) {
10015 				/*
10016 				 * Write operation - copy data into
10017 				 * an aligned temporary buffer. Buffer will be
10018 				 * synced for device by ddi_dma_addr_bind_handle
10019 				 */
10020 				bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf,
10021 				    bp->b_bcount);
10022 			}
10023 
10024 			rval = ddi_dma_addr_bind_handle(
10025 			    spx->txlt_buf_dma_handle,
10026 			    NULL,
10027 			    spx->txlt_tmp_buf,
10028 			    bufsz, dma_flags, ddicallback, 0,
10029 			    &spx->txlt_dma_cookie,
10030 			    &spx->txlt_curwin_num_dma_cookies);
10031 		}
10032 
10033 		switch (rval) {
10034 		case DDI_DMA_PARTIAL_MAP:
10035 			SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
10036 			    "sata_dma_buf_setup: DMA Partial Map\n", NULL);
10037 			/*
10038 			 * Partial DMA mapping.
10039 			 * Retrieve number of DMA windows for this request.
10040 			 */
10041 			if (ddi_dma_numwin(spx->txlt_buf_dma_handle,
10042 			    &spx->txlt_num_dma_win) != DDI_SUCCESS) {
10043 				if (spx->txlt_tmp_buf != NULL) {
10044 					ddi_dma_mem_free(
10045 					    &spx->txlt_tmp_buf_handle);
10046 					spx->txlt_tmp_buf = NULL;
10047 				}
10048 				(void) ddi_dma_unbind_handle(
10049 				    spx->txlt_buf_dma_handle);
10050 				(void) ddi_dma_free_handle(
10051 				    &spx->txlt_buf_dma_handle);
10052 				spx->txlt_buf_dma_handle = NULL;
10053 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
10054 				    "sata_dma_buf_setup: numwin failed\n"));
10055 				return (DDI_FAILURE);
10056 			}
10057 			SATADBG2(SATA_DBG_DMA_SETUP,
10058 			    spx->txlt_sata_hba_inst,
10059 			    "sata_dma_buf_setup: windows: %d, cookies: %d\n",
10060 			    spx->txlt_num_dma_win,
10061 			    spx->txlt_curwin_num_dma_cookies);
10062 			spx->txlt_cur_dma_win = 0;
10063 			break;
10064 
10065 		case DDI_DMA_MAPPED:
10066 			/* DMA fully mapped */
10067 			spx->txlt_num_dma_win = 1;
10068 			spx->txlt_cur_dma_win = 0;
10069 			SATADBG1(SATA_DBG_DMA_SETUP,
10070 			    spx->txlt_sata_hba_inst,
10071 			    "sata_dma_buf_setup: windows: 1 "
10072 			    "cookies: %d\n", spx->txlt_curwin_num_dma_cookies);
10073 			break;
10074 
10075 		default:
10076 			/* DMA mapping failed */
10077 			if (spx->txlt_tmp_buf != NULL) {
10078 				ddi_dma_mem_free(
10079 				    &spx->txlt_tmp_buf_handle);
10080 				spx->txlt_tmp_buf = NULL;
10081 			}
10082 			(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
10083 			spx->txlt_buf_dma_handle = NULL;
10084 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
10085 			    "sata_dma_buf_setup: buf dma handle binding "
10086 			    "failed %x\n", rval));
10087 			return (rval);
10088 		}
10089 		spx->txlt_curwin_processed_dma_cookies = 0;
10090 		spx->txlt_dma_cookie_list = NULL;
10091 	} else {
10092 		/*
10093 		 * DMA setup is reused. Check if we need to process more
10094 		 * cookies in current window, or to get next window, if any.
10095 		 */
10096 
10097 		ASSERT(spx->txlt_curwin_processed_dma_cookies <=
10098 		    spx->txlt_curwin_num_dma_cookies);
10099 
10100 		if (spx->txlt_curwin_processed_dma_cookies ==
10101 		    spx->txlt_curwin_num_dma_cookies) {
10102 			/*
10103 			 * All cookies from current DMA window were processed.
10104 			 * Get next DMA window.
10105 			 */
10106 			spx->txlt_cur_dma_win++;
10107 			if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) {
10108 				(void) ddi_dma_getwin(spx->txlt_buf_dma_handle,
10109 				    spx->txlt_cur_dma_win, &offset, &size,
10110 				    &spx->txlt_dma_cookie,
10111 				    &spx->txlt_curwin_num_dma_cookies);
10112 				spx->txlt_curwin_processed_dma_cookies = 0;
10113 			} else {
10114 				/* No more windows! End of request! */
10115 				/* What to do? - panic for now */
10116 				ASSERT(spx->txlt_cur_dma_win >=
10117 				    spx->txlt_num_dma_win);
10118 
10119 				spx->txlt_curwin_num_dma_cookies = 0;
10120 				spx->txlt_curwin_processed_dma_cookies = 0;
10121 				spx->txlt_sata_pkt->
10122 				    satapkt_cmd.satacmd_num_dma_cookies = 0;
10123 				return (DDI_SUCCESS);
10124 			}
10125 		}
10126 	}
10127 	/* There better be at least one DMA cookie outstanding */
10128 	ASSERT((spx->txlt_curwin_num_dma_cookies -
10129 	    spx->txlt_curwin_processed_dma_cookies) > 0);
10130 
10131 	if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) {
10132 		/* The default cookie slot was used in previous run */
10133 		ASSERT(spx->txlt_curwin_processed_dma_cookies == 0);
10134 		spx->txlt_dma_cookie_list = NULL;
10135 		spx->txlt_dma_cookie_list_len = 0;
10136 	}
10137 	if (spx->txlt_curwin_processed_dma_cookies == 0) {
10138 		/*
10139 		 * Processing a new DMA window - set-up dma cookies list.
10140 		 * We may reuse previously allocated cookie array if it is
10141 		 * possible.
10142 		 */
10143 		if (spx->txlt_dma_cookie_list != NULL &&
10144 		    spx->txlt_dma_cookie_list_len <
10145 		    spx->txlt_curwin_num_dma_cookies) {
10146 			/*
10147 			 * New DMA window contains more cookies than
10148 			 * the previous one. We need larger cookie list - free
10149 			 * the old one.
10150 			 */
10151 			(void) kmem_free(spx->txlt_dma_cookie_list,
10152 			    spx->txlt_dma_cookie_list_len *
10153 			    sizeof (ddi_dma_cookie_t));
10154 			spx->txlt_dma_cookie_list = NULL;
10155 			spx->txlt_dma_cookie_list_len = 0;
10156 		}
10157 		if (spx->txlt_dma_cookie_list == NULL) {
10158 			/*
10159 			 * Calculate lesser of number of cookies in this
10160 			 * DMA window and number of s/g entries.
10161 			 */
10162 			max_sg_len = cur_dma_attr->dma_attr_sgllen;
10163 			req_len = MIN(max_sg_len,
10164 			    spx->txlt_curwin_num_dma_cookies);
10165 
10166 			/* Allocate new dma cookie array if necessary */
10167 			if (req_len == 1) {
10168 				/* Only one cookie - no need for a list */
10169 				spx->txlt_dma_cookie_list =
10170 				    &spx->txlt_dma_cookie;
10171 				spx->txlt_dma_cookie_list_len = 1;
10172 			} else {
10173 				/*
10174 				 * More than one cookie - try to allocate space.
10175 				 */
10176 				spx->txlt_dma_cookie_list = kmem_zalloc(
10177 				    sizeof (ddi_dma_cookie_t) * req_len,
10178 				    callback == NULL_FUNC ? KM_NOSLEEP :
10179 				    KM_SLEEP);
10180 				if (spx->txlt_dma_cookie_list == NULL) {
10181 					SATADBG1(SATA_DBG_DMA_SETUP,
10182 					    spx->txlt_sata_hba_inst,
10183 					    "sata_dma_buf_setup: cookie list "
10184 					    "allocation failed\n", NULL);
10185 					/*
10186 					 * We could not allocate space for
10187 					 * neccessary number of dma cookies in
10188 					 * this window, so we fail this request.
10189 					 * Next invocation would try again to
10190 					 * allocate space for cookie list.
10191 					 * Note:Packet residue was not modified.
10192 					 */
10193 					return (DDI_DMA_NORESOURCES);
10194 				} else {
10195 					spx->txlt_dma_cookie_list_len = req_len;
10196 				}
10197 			}
10198 		}
10199 		/*
10200 		 * Fetch DMA cookies into cookie list in sata_pkt_txlate.
10201 		 * First cookie was already fetched.
10202 		 */
10203 		*(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie;
10204 		cur_txfer_len =
10205 		    (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size;
10206 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1;
10207 		spx->txlt_curwin_processed_dma_cookies++;
10208 		for (i = 1; (i < spx->txlt_dma_cookie_list_len) &&
10209 		    (i < spx->txlt_curwin_num_dma_cookies); i++) {
10210 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
10211 			    &spx->txlt_dma_cookie_list[i]);
10212 			cur_txfer_len +=
10213 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
10214 			spx->txlt_curwin_processed_dma_cookies++;
10215 			spx->txlt_sata_pkt->
10216 			    satapkt_cmd.satacmd_num_dma_cookies += 1;
10217 		}
10218 	} else {
10219 		SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
10220 		    "sata_dma_buf_setup: sliding within DMA window, "
10221 		    "cur cookie %d, total cookies %d\n",
10222 		    spx->txlt_curwin_processed_dma_cookies,
10223 		    spx->txlt_curwin_num_dma_cookies);
10224 
10225 		/*
10226 		 * Not all cookies from the current dma window were used because
10227 		 * of s/g limitation.
10228 		 * There is no need to re-size the list - it was set at
10229 		 * optimal size, or only default entry is used (s/g = 1).
10230 		 */
10231 		if (spx->txlt_dma_cookie_list == NULL) {
10232 			spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie;
10233 			spx->txlt_dma_cookie_list_len = 1;
10234 		}
10235 		/*
10236 		 * Since we are processing remaining cookies in a DMA window,
10237 		 * there may be less of them than the number of entries in the
10238 		 * current dma cookie list.
10239 		 */
10240 		req_len = MIN(spx->txlt_dma_cookie_list_len,
10241 		    (spx->txlt_curwin_num_dma_cookies -
10242 		    spx->txlt_curwin_processed_dma_cookies));
10243 
10244 		/* Fetch the next batch of cookies */
10245 		for (i = 0, cur_txfer_len = 0; i < req_len; i++) {
10246 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
10247 			    &spx->txlt_dma_cookie_list[i]);
10248 			cur_txfer_len +=
10249 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
10250 			spx->txlt_sata_pkt->
10251 			    satapkt_cmd.satacmd_num_dma_cookies++;
10252 			spx->txlt_curwin_processed_dma_cookies++;
10253 		}
10254 	}
10255 
10256 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0);
10257 
10258 	/* Point sata_cmd to the cookie list */
10259 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list =
10260 	    &spx->txlt_dma_cookie_list[0];
10261 
10262 	/* Remember number of DMA cookies passed in sata packet */
10263 	spx->txlt_num_dma_cookies =
10264 	    spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies;
10265 
10266 	ASSERT(cur_txfer_len != 0);
10267 	if (cur_txfer_len <= bp->b_bcount)
10268 		spx->txlt_total_residue -= cur_txfer_len;
10269 	else {
10270 		/*
10271 		 * Temporary DMA buffer has been padded by
10272 		 * ddi_dma_mem_alloc()!
10273 		 * This requires special handling, because DMA cookies are
10274 		 * based on the temporary buffer size, not the b_bcount,
10275 		 * and we have extra bytes to transfer - but the packet
10276 		 * residue has to stay correct because we will copy only
10277 		 * the requested number of bytes.
10278 		 */
10279 		spx->txlt_total_residue -= bp->b_bcount;
10280 	}
10281 
10282 	return (DDI_SUCCESS);
10283 }
10284 
10285 /*
10286  * Common routine for releasing DMA resources
10287  */
10288 static void
10289 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx)
10290 {
10291 	if (spx->txlt_buf_dma_handle != NULL) {
10292 		if (spx->txlt_tmp_buf != NULL)  {
10293 			/*
10294 			 * Intermediate DMA buffer was allocated.
10295 			 * Free allocated buffer and associated access handle.
10296 			 */
10297 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
10298 			spx->txlt_tmp_buf = NULL;
10299 		}
10300 		/*
10301 		 * Free DMA resources - cookies and handles
10302 		 */
10303 		/* ASSERT(spx->txlt_dma_cookie_list != NULL); */
10304 		if (spx->txlt_dma_cookie_list != NULL) {
10305 			if (spx->txlt_dma_cookie_list !=
10306 			    &spx->txlt_dma_cookie) {
10307 				(void) kmem_free(spx->txlt_dma_cookie_list,
10308 				    spx->txlt_dma_cookie_list_len *
10309 				    sizeof (ddi_dma_cookie_t));
10310 				spx->txlt_dma_cookie_list = NULL;
10311 			}
10312 		}
10313 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
10314 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
10315 		spx->txlt_buf_dma_handle = NULL;
10316 	}
10317 }
10318 
10319 /*
10320  * Free DMA resources
10321  * Used by the HBA driver to release DMA resources that it does not use.
10322  *
10323  * Returns Void
10324  */
10325 void
10326 sata_free_dma_resources(sata_pkt_t *sata_pkt)
10327 {
10328 	sata_pkt_txlate_t *spx;
10329 
10330 	if (sata_pkt == NULL)
10331 		return;
10332 
10333 	spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
10334 
10335 	sata_common_free_dma_rsrcs(spx);
10336 }
10337 
10338 /*
10339  * Fetch Device Identify data.
10340  * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type)
10341  * command to a device and get the device identify data.
10342  * The device_info structure has to be set to device type (for selecting proper
10343  * device identify command).
10344  *
10345  * Returns:
10346  * SATA_SUCCESS if cmd succeeded
10347  * SATA_RETRY if cmd was rejected and could be retried,
10348  * SATA_FAILURE if cmd failed and should not be retried (port error)
10349  *
10350  * Cannot be called in an interrupt context.
10351  */
10352 
10353 static int
10354 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst,
10355     sata_drive_info_t *sdinfo)
10356 {
10357 	struct buf *bp;
10358 	sata_pkt_t *spkt;
10359 	sata_cmd_t *scmd;
10360 	sata_pkt_txlate_t *spx;
10361 	int rval;
10362 
10363 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10364 	spx->txlt_sata_hba_inst = sata_hba_inst;
10365 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
10366 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10367 	if (spkt == NULL) {
10368 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10369 		return (SATA_RETRY); /* may retry later */
10370 	}
10371 	/* address is needed now */
10372 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10373 
10374 	/*
10375 	 * Allocate buffer for Identify Data return data
10376 	 */
10377 	bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t));
10378 	if (bp == NULL) {
10379 		sata_pkt_free(spx);
10380 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10381 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10382 		    "sata_fetch_device_identify_data: "
10383 		    "cannot allocate buffer for ID"));
10384 		return (SATA_RETRY); /* may retry later */
10385 	}
10386 
10387 	/* Fill sata_pkt */
10388 	sdinfo->satadrv_state = SATA_STATE_PROBING;
10389 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10390 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10391 	/* Synchronous mode, no callback */
10392 	spkt->satapkt_comp = NULL;
10393 	/* Timeout 30s */
10394 	spkt->satapkt_time = sata_default_pkt_time;
10395 
10396 	scmd = &spkt->satapkt_cmd;
10397 	scmd->satacmd_bp = bp;
10398 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
10399 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10400 
10401 	/* Build Identify Device cmd in the sata_pkt */
10402 	scmd->satacmd_addr_type = 0;		/* N/A */
10403 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
10404 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
10405 	scmd->satacmd_lba_mid_lsb = 0;		/* N/A */
10406 	scmd->satacmd_lba_high_lsb = 0;		/* N/A */
10407 	scmd->satacmd_features_reg = 0;		/* N/A */
10408 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
10409 	if (sdinfo->satadrv_type & SATA_DTYPE_ATAPI) {
10410 		/* Identify Packet Device cmd */
10411 		scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE;
10412 	} else {
10413 		/* Identify Device cmd - mandatory for all other devices */
10414 		scmd->satacmd_cmd_reg = SATAC_ID_DEVICE;
10415 	}
10416 
10417 	/* Send pkt to SATA HBA driver */
10418 	rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt);
10419 
10420 #ifdef SATA_INJECT_FAULTS
10421 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
10422 #endif
10423 
10424 	if (rval == SATA_TRAN_ACCEPTED &&
10425 	    spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10426 		if (spx->txlt_buf_dma_handle != NULL) {
10427 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10428 			    DDI_DMA_SYNC_FORKERNEL);
10429 			ASSERT(rval == DDI_SUCCESS);
10430 		}
10431 		if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config &
10432 		    SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) {
10433 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10434 			    "SATA disk device at port %d - "
10435 			    "partial Identify Data",
10436 			    sdinfo->satadrv_addr.cport));
10437 			rval = SATA_RETRY; /* may retry later */
10438 			goto fail;
10439 		}
10440 		/* Update sata_drive_info */
10441 		bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id,
10442 		    sizeof (sata_id_t));
10443 
10444 		sdinfo->satadrv_features_support = 0;
10445 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10446 			/*
10447 			 * Retrieve capacity (disks only) and addressing mode
10448 			 */
10449 			sdinfo->satadrv_capacity = sata_check_capacity(sdinfo);
10450 		} else {
10451 			/*
10452 			 * For ATAPI devices one would have to issue
10453 			 * Get Capacity cmd for media capacity. Not here.
10454 			 */
10455 			sdinfo->satadrv_capacity = 0;
10456 			/*
10457 			 * Check what cdb length is supported
10458 			 */
10459 			if ((sdinfo->satadrv_id.ai_config &
10460 			    SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B)
10461 				sdinfo->satadrv_atapi_cdb_len = 16;
10462 			else
10463 				sdinfo->satadrv_atapi_cdb_len = 12;
10464 		}
10465 		/* Setup supported features flags */
10466 		if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT)
10467 			sdinfo->satadrv_features_support |= SATA_DEV_F_DMA;
10468 
10469 		/* Check for SATA GEN and NCQ support */
10470 		if (sdinfo->satadrv_id.ai_satacap != 0 &&
10471 		    sdinfo->satadrv_id.ai_satacap != 0xffff) {
10472 			/* SATA compliance */
10473 			if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ)
10474 				sdinfo->satadrv_features_support |=
10475 				    SATA_DEV_F_NCQ;
10476 			if (sdinfo->satadrv_id.ai_satacap &
10477 			    (SATA_1_SPEED | SATA_2_SPEED)) {
10478 				if (sdinfo->satadrv_id.ai_satacap &
10479 				    SATA_2_SPEED)
10480 					sdinfo->satadrv_features_support |=
10481 					    SATA_DEV_F_SATA2;
10482 				if (sdinfo->satadrv_id.ai_satacap &
10483 				    SATA_1_SPEED)
10484 					sdinfo->satadrv_features_support |=
10485 					    SATA_DEV_F_SATA1;
10486 			} else {
10487 				sdinfo->satadrv_features_support |=
10488 				    SATA_DEV_F_SATA1;
10489 			}
10490 		}
10491 		if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) &&
10492 		    (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD))
10493 			sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ;
10494 
10495 		sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth;
10496 		if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) ||
10497 		    (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) {
10498 			++sdinfo->satadrv_queue_depth;
10499 			/* Adjust according to controller capabilities */
10500 			sdinfo->satadrv_max_queue_depth = MIN(
10501 			    sdinfo->satadrv_queue_depth,
10502 			    SATA_QDEPTH(sata_hba_inst));
10503 			/* Adjust according to global queue depth limit */
10504 			sdinfo->satadrv_max_queue_depth = MIN(
10505 			    sdinfo->satadrv_max_queue_depth,
10506 			    sata_current_max_qdepth);
10507 			if (sdinfo->satadrv_max_queue_depth == 0)
10508 				sdinfo->satadrv_max_queue_depth = 1;
10509 		} else
10510 			sdinfo->satadrv_max_queue_depth = 1;
10511 
10512 		rval = SATA_SUCCESS;
10513 	} else {
10514 		/*
10515 		 * Woops, no Identify Data.
10516 		 */
10517 		if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) {
10518 			rval = SATA_RETRY; /* may retry later */
10519 		} else if (rval == SATA_TRAN_ACCEPTED) {
10520 			if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR ||
10521 			    spkt->satapkt_reason == SATA_PKT_ABORTED ||
10522 			    spkt->satapkt_reason == SATA_PKT_TIMEOUT ||
10523 			    spkt->satapkt_reason == SATA_PKT_RESET)
10524 				rval = SATA_RETRY; /* may retry later */
10525 			else
10526 				rval = SATA_FAILURE;
10527 		} else {
10528 			rval = SATA_FAILURE;
10529 		}
10530 	}
10531 fail:
10532 	/* Free allocated resources */
10533 	sata_free_local_buffer(spx);
10534 	sata_pkt_free(spx);
10535 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
10536 
10537 	return (rval);
10538 }
10539 
10540 
10541 /*
10542  * Some devices may not come-up with default DMA mode (UDMA or MWDMA).
10543  * UDMA mode is checked first, followed by MWDMA mode.
10544  * set correctly, so this function is setting it to the highest supported level.
10545  * Older SATA spec required that the device supports at least DMA 4 mode and
10546  * UDMA mode is selected.  It is not mentioned in SerialATA 2.6, so this
10547  * restriction has been removed.
10548  *
10549  * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported.
10550  * Returns SATA_FAILURE if proper DMA mode could not be selected.
10551  *
10552  * NOTE: This function should be called only if DMA mode is supported.
10553  */
10554 static int
10555 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo)
10556 {
10557 	sata_pkt_t *spkt;
10558 	sata_cmd_t *scmd;
10559 	sata_pkt_txlate_t *spx;
10560 	int i, mode;
10561 	uint8_t subcmd;
10562 	int rval = SATA_SUCCESS;
10563 
10564 	ASSERT(sdinfo != NULL);
10565 	ASSERT(sata_hba_inst != NULL);
10566 
10567 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
10568 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) {
10569 		/* Find highest Ultra DMA mode supported */
10570 		for (mode = 6; mode >= 0; --mode) {
10571 			if (sdinfo->satadrv_id.ai_ultradma & (1 << mode))
10572 				break;
10573 		}
10574 #if 0
10575 		/* Left for historical reasons */
10576 		/*
10577 		 * Some initial version of SATA spec indicated that at least
10578 		 * UDMA mode 4 has to be supported. It is not mentioned in
10579 		 * SerialATA 2.6, so this restriction is removed.
10580 		 */
10581 		if (mode < 4)
10582 			return (SATA_FAILURE);
10583 #endif
10584 		/* Find UDMA mode currently selected */
10585 		for (i = 6; i >= 0; --i) {
10586 			if (sdinfo->satadrv_id.ai_ultradma & (1 << (i + 8)))
10587 				break;
10588 		}
10589 		if (i >= mode)
10590 			/* Nothing to do */
10591 			return (SATA_SUCCESS);
10592 
10593 		subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA;
10594 
10595 	} else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) {
10596 		/* Find highest MultiWord DMA mode supported */
10597 		for (mode = 2; mode >= 0; --mode) {
10598 			if (sdinfo->satadrv_id.ai_dworddma & (1 << mode))
10599 				break;
10600 		}
10601 		/* Find highest MultiWord DMA mode selected */
10602 		for (i = 2; i >= 0; --i) {
10603 			if (sdinfo->satadrv_id.ai_dworddma & (1 << (i + 8)))
10604 				break;
10605 		}
10606 		if (i >= mode)
10607 			/* Nothing to do */
10608 			return (SATA_SUCCESS);
10609 
10610 		subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA;
10611 	} else
10612 		return (SATA_SUCCESS);
10613 
10614 	/*
10615 	 * Set DMA mode via SET FEATURES COMMAND.
10616 	 * Prepare packet for SET FEATURES COMMAND.
10617 	 */
10618 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10619 	spx->txlt_sata_hba_inst = sata_hba_inst;
10620 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10621 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10622 	if (spkt == NULL) {
10623 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10624 		    "sata_set_dma_mode: could not set DMA mode %", mode));
10625 		rval = SATA_FAILURE;
10626 		goto done;
10627 	}
10628 	/* Fill sata_pkt */
10629 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10630 	/* Timeout 30s */
10631 	spkt->satapkt_time = sata_default_pkt_time;
10632 	/* Synchronous mode, no callback, interrupts */
10633 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10634 	spkt->satapkt_comp = NULL;
10635 	scmd = &spkt->satapkt_cmd;
10636 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10637 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10638 	scmd->satacmd_addr_type = 0;
10639 	scmd->satacmd_device_reg = 0;
10640 	scmd->satacmd_status_reg = 0;
10641 	scmd->satacmd_error_reg = 0;
10642 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10643 	scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE;
10644 	scmd->satacmd_sec_count_lsb = subcmd | mode;
10645 
10646 	/* Transfer command to HBA */
10647 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
10648 	    spkt) != SATA_TRAN_ACCEPTED ||
10649 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
10650 		/* Pkt execution failed */
10651 		rval = SATA_FAILURE;
10652 	}
10653 done:
10654 
10655 	/* Free allocated resources */
10656 	if (spkt != NULL)
10657 		sata_pkt_free(spx);
10658 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10659 
10660 	return (rval);
10661 }
10662 
10663 
10664 /*
10665  * Set device caching mode.
10666  * One of the following operations should be specified:
10667  * SATAC_SF_ENABLE_READ_AHEAD
10668  * SATAC_SF_DISABLE_READ_AHEAD
10669  * SATAC_SF_ENABLE_WRITE_CACHE
10670  * SATAC_SF_DISABLE_WRITE_CACHE
10671  *
10672  * If operation fails, system log messgage is emitted.
10673  * Returns SATA_SUCCESS when the operation succeeds, SATA_RETRY if
10674  * command was sent but did not succeed, and SATA_FAILURE otherwise.
10675  */
10676 
10677 static int
10678 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
10679     int cache_op)
10680 {
10681 	sata_pkt_t *spkt;
10682 	sata_cmd_t *scmd;
10683 	sata_pkt_txlate_t *spx;
10684 	int rval = SATA_SUCCESS;
10685 	int hba_rval;
10686 	char *infop;
10687 
10688 	ASSERT(sdinfo != NULL);
10689 	ASSERT(sata_hba_inst != NULL);
10690 	ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD ||
10691 	    cache_op == SATAC_SF_DISABLE_READ_AHEAD ||
10692 	    cache_op == SATAC_SF_ENABLE_WRITE_CACHE ||
10693 	    cache_op == SATAC_SF_DISABLE_WRITE_CACHE);
10694 
10695 
10696 	/* Prepare packet for SET FEATURES COMMAND */
10697 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10698 	spx->txlt_sata_hba_inst = sata_hba_inst;
10699 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10700 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10701 	if (spkt == NULL) {
10702 		rval = SATA_FAILURE;
10703 		goto failure;
10704 	}
10705 	/* Fill sata_pkt */
10706 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10707 	/* Timeout 30s */
10708 	spkt->satapkt_time = sata_default_pkt_time;
10709 	/* Synchronous mode, no callback, interrupts */
10710 	spkt->satapkt_op_mode =
10711 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10712 	spkt->satapkt_comp = NULL;
10713 	scmd = &spkt->satapkt_cmd;
10714 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10715 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10716 	scmd->satacmd_addr_type = 0;
10717 	scmd->satacmd_device_reg = 0;
10718 	scmd->satacmd_status_reg = 0;
10719 	scmd->satacmd_error_reg = 0;
10720 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10721 	scmd->satacmd_features_reg = cache_op;
10722 
10723 	/* Transfer command to HBA */
10724 	hba_rval = (*SATA_START_FUNC(sata_hba_inst))(
10725 	    SATA_DIP(sata_hba_inst), spkt);
10726 
10727 #ifdef SATA_INJECT_FAULTS
10728 	sata_inject_pkt_fault(spkt, &rval, sata_fault_type);
10729 #endif
10730 
10731 	if ((hba_rval != SATA_TRAN_ACCEPTED) ||
10732 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
10733 		/* Pkt execution failed */
10734 		switch (cache_op) {
10735 		case SATAC_SF_ENABLE_READ_AHEAD:
10736 			infop = "enabling read ahead failed";
10737 			break;
10738 		case SATAC_SF_DISABLE_READ_AHEAD:
10739 			infop = "disabling read ahead failed";
10740 			break;
10741 		case SATAC_SF_ENABLE_WRITE_CACHE:
10742 			infop = "enabling write cache failed";
10743 			break;
10744 		case SATAC_SF_DISABLE_WRITE_CACHE:
10745 			infop = "disabling write cache failed";
10746 			break;
10747 		}
10748 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
10749 		rval = SATA_RETRY;
10750 	}
10751 failure:
10752 	/* Free allocated resources */
10753 	if (spkt != NULL)
10754 		sata_pkt_free(spx);
10755 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10756 	return (rval);
10757 }
10758 
10759 /*
10760  * Set Removable Media Status Notification (enable/disable)
10761  * state == 0 , disable
10762  * state != 0 , enable
10763  *
10764  * If operation fails, system log messgage is emitted.
10765  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
10766  */
10767 
10768 static int
10769 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
10770     int state)
10771 {
10772 	sata_pkt_t *spkt;
10773 	sata_cmd_t *scmd;
10774 	sata_pkt_txlate_t *spx;
10775 	int rval = SATA_SUCCESS;
10776 	char *infop;
10777 
10778 	ASSERT(sdinfo != NULL);
10779 	ASSERT(sata_hba_inst != NULL);
10780 
10781 	/* Prepare packet for SET FEATURES COMMAND */
10782 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10783 	spx->txlt_sata_hba_inst = sata_hba_inst;
10784 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10785 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10786 	if (spkt == NULL) {
10787 		rval = SATA_FAILURE;
10788 		goto failure;
10789 	}
10790 	/* Fill sata_pkt */
10791 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10792 	/* Timeout 30s */
10793 	spkt->satapkt_time = sata_default_pkt_time;
10794 	/* Synchronous mode, no callback, interrupts */
10795 	spkt->satapkt_op_mode =
10796 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10797 	spkt->satapkt_comp = NULL;
10798 	scmd = &spkt->satapkt_cmd;
10799 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10800 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10801 	scmd->satacmd_addr_type = 0;
10802 	scmd->satacmd_device_reg = 0;
10803 	scmd->satacmd_status_reg = 0;
10804 	scmd->satacmd_error_reg = 0;
10805 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10806 	if (state == 0)
10807 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN;
10808 	else
10809 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN;
10810 
10811 	/* Transfer command to HBA */
10812 	if (((*SATA_START_FUNC(sata_hba_inst))(
10813 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
10814 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
10815 		/* Pkt execution failed */
10816 		if (state == 0)
10817 			infop = "disabling Removable Media Status "
10818 			    "Notification failed";
10819 		else
10820 			infop = "enabling Removable Media Status "
10821 			    "Notification failed";
10822 
10823 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
10824 		rval = SATA_FAILURE;
10825 	}
10826 failure:
10827 	/* Free allocated resources */
10828 	if (spkt != NULL)
10829 		sata_pkt_free(spx);
10830 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10831 	return (rval);
10832 }
10833 
10834 
10835 /*
10836  * Update port SCR block
10837  */
10838 static void
10839 sata_update_port_scr(sata_port_scr_t *port_scr, sata_device_t *device)
10840 {
10841 	port_scr->sstatus = device->satadev_scr.sstatus;
10842 	port_scr->serror = device->satadev_scr.serror;
10843 	port_scr->scontrol = device->satadev_scr.scontrol;
10844 	port_scr->sactive = device->satadev_scr.sactive;
10845 	port_scr->snotific = device->satadev_scr.snotific;
10846 }
10847 
10848 /*
10849  * Update state and copy port ss* values from passed sata_device structure.
10850  * sata_address is validated - if not valid, nothing is changed in sata_scsi
10851  * configuration struct.
10852  *
10853  * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function
10854  * regardless of the state in device argument.
10855  *
10856  * Port mutex should be held while calling this function.
10857  */
10858 static void
10859 sata_update_port_info(sata_hba_inst_t *sata_hba_inst,
10860 	sata_device_t *sata_device)
10861 {
10862 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst,
10863 	    sata_device->satadev_addr.cport)));
10864 
10865 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT ||
10866 	    sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
10867 
10868 		sata_cport_info_t *cportinfo;
10869 
10870 		if (SATA_NUM_CPORTS(sata_hba_inst) <=
10871 		    sata_device->satadev_addr.cport)
10872 			return;
10873 
10874 		cportinfo = SATA_CPORT_INFO(sata_hba_inst,
10875 		    sata_device->satadev_addr.cport);
10876 		sata_update_port_scr(&cportinfo->cport_scr, sata_device);
10877 
10878 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
10879 		cportinfo->cport_state &= ~(SATA_PSTATE_PWRON |
10880 		    SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
10881 		cportinfo->cport_state |=
10882 		    sata_device->satadev_state & SATA_PSTATE_VALID;
10883 	} else {
10884 		sata_pmport_info_t *pmportinfo;
10885 
10886 		if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT) ||
10887 		    (sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) ||
10888 		    SATA_NUM_PMPORTS(sata_hba_inst,
10889 		    sata_device->satadev_addr.cport) <
10890 		    sata_device->satadev_addr.pmport)
10891 			return;
10892 
10893 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
10894 		    sata_device->satadev_addr.cport,
10895 		    sata_device->satadev_addr.pmport);
10896 		sata_update_port_scr(&pmportinfo->pmport_scr, sata_device);
10897 
10898 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
10899 		pmportinfo->pmport_state &=
10900 		    ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF |
10901 		    SATA_PSTATE_FAILED);
10902 		pmportinfo->pmport_state |=
10903 		    sata_device->satadev_state & SATA_PSTATE_VALID;
10904 	}
10905 }
10906 
10907 
10908 
10909 /*
10910  * Extract SATA port specification from an IOCTL argument.
10911  *
10912  * This function return the port the user land send us as is, unless it
10913  * cannot retrieve port spec, then -1 is returned.
10914  *
10915  * Note: Only cport  - no port multiplier port.
10916  */
10917 static int32_t
10918 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp)
10919 {
10920 	int32_t port;
10921 
10922 	/* Extract port number from nvpair in dca structure  */
10923 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
10924 		SATA_LOG_D((sata_hba_inst, CE_NOTE,
10925 		    "sata_get_port_num: invalid port spec 0x%x in ioctl",
10926 		    port));
10927 		port = -1;
10928 	}
10929 
10930 	return (port);
10931 }
10932 
10933 /*
10934  * Get dev_info_t pointer to the device node pointed to by port argument.
10935  * NOTE: target argument is a value used in ioctls to identify
10936  * the AP - it is not a sata_address.
10937  * It is a combination of cport, pmport and address qualifier, encodded same
10938  * way as a scsi target number.
10939  * At this moment it carries only cport number.
10940  *
10941  * No PMult hotplug support.
10942  *
10943  * Returns dev_info_t pointer if target device was found, NULL otherwise.
10944  */
10945 
10946 static dev_info_t *
10947 sata_get_target_dip(dev_info_t *dip, int32_t port)
10948 {
10949 	dev_info_t	*cdip = NULL;
10950 	int		target, tgt;
10951 	int		ncport;
10952 	int 		circ;
10953 
10954 	ncport = port & SATA_CFGA_CPORT_MASK;
10955 	target = SATA_TO_SCSI_TARGET(ncport, 0, SATA_ADDR_DCPORT);
10956 
10957 	ndi_devi_enter(dip, &circ);
10958 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
10959 		dev_info_t *next = ddi_get_next_sibling(cdip);
10960 
10961 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
10962 		    DDI_PROP_DONTPASS, "target", -1);
10963 		if (tgt == -1) {
10964 			/*
10965 			 * This is actually an error condition, but not
10966 			 * a fatal one. Just continue the search.
10967 			 */
10968 			cdip = next;
10969 			continue;
10970 		}
10971 
10972 		if (tgt == target)
10973 			break;
10974 
10975 		cdip = next;
10976 	}
10977 	ndi_devi_exit(dip, circ);
10978 
10979 	return (cdip);
10980 }
10981 
10982 /*
10983  * Get dev_info_t pointer to the device node pointed to by port argument.
10984  * NOTE: target argument is a value used in ioctls to identify
10985  * the AP - it is not a sata_address.
10986  * It is a combination of cport, pmport and address qualifier, encoded same
10987  * way as a scsi target number.
10988  * At this moment it carries only cport number.
10989  *
10990  * No PMult hotplug support.
10991  *
10992  * Returns dev_info_t pointer if target device was found, NULL otherwise.
10993  */
10994 
10995 static dev_info_t *
10996 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr)
10997 {
10998 	dev_info_t	*cdip = NULL;
10999 	int		target, tgt;
11000 	int 		circ;
11001 
11002 	target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual);
11003 
11004 	ndi_devi_enter(dip, &circ);
11005 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
11006 		dev_info_t *next = ddi_get_next_sibling(cdip);
11007 
11008 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
11009 		    DDI_PROP_DONTPASS, "target", -1);
11010 		if (tgt == -1) {
11011 			/*
11012 			 * This is actually an error condition, but not
11013 			 * a fatal one. Just continue the search.
11014 			 */
11015 			cdip = next;
11016 			continue;
11017 		}
11018 
11019 		if (tgt == target)
11020 			break;
11021 
11022 		cdip = next;
11023 	}
11024 	ndi_devi_exit(dip, circ);
11025 
11026 	return (cdip);
11027 }
11028 
11029 /*
11030  * Process sata port disconnect request.
11031  * Normally, cfgadm sata plugin will try to offline (unconfigure) the device
11032  * before this request. Nevertheless, if a device is still configured,
11033  * we need to attempt to offline and unconfigure device.
11034  * Regardless of the unconfigure operation results the port is marked as
11035  * deactivated and no access to the attached device is possible.
11036  * If the target node remains because unconfigure operation failed, its state
11037  * will be set to DEVICE_REMOVED, preventing it to be used again when a device
11038  * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure
11039  * the device and remove old target node.
11040  *
11041  * This function invokes sata_hba_inst->satahba_tran->
11042  * sata_tran_hotplug_ops->sata_tran_port_deactivate().
11043  * If successful, the device structure (if any) attached to the specified port
11044  * is removed and state of the port marked appropriately.
11045  * Failure of the port_deactivate may keep port in the physically active state,
11046  * or may fail the port.
11047  *
11048  * NOTE: Port multiplier code is not completed nor tested.
11049  */
11050 
11051 static int
11052 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst,
11053     sata_device_t *sata_device)
11054 {
11055 	sata_drive_info_t *sdinfo = NULL;
11056 	sata_cport_info_t *cportinfo = NULL;
11057 	sata_pmport_info_t *pmportinfo = NULL;
11058 	sata_pmult_info_t *pmultinfo = NULL;
11059 	dev_info_t *tdip;
11060 	int cport, pmport, qual;
11061 	int rval = SATA_SUCCESS;
11062 	int rv = 0;
11063 
11064 	cport = sata_device->satadev_addr.cport;
11065 	pmport = sata_device->satadev_addr.pmport;
11066 	qual = sata_device->satadev_addr.qual;
11067 
11068 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
11069 
11070 	/*
11071 	 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran->
11072 	 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
11073 	 * Do the sanity check.
11074 	 */
11075 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
11076 		/* No physical port deactivation supported. */
11077 		return (EINVAL);
11078 	}
11079 
11080 	/* Check the current state of the port */
11081 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11082 	    (SATA_DIP(sata_hba_inst), sata_device);
11083 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11084 	sata_update_port_info(sata_hba_inst, sata_device);
11085 	if (rval != SATA_SUCCESS ||
11086 	    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
11087 		/* Device port status is unknown or it is in failed state */
11088 		if (qual == SATA_ADDR_PMPORT) {
11089 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
11090 			    SATA_PSTATE_FAILED;
11091 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
11092 			    "sata_hba_ioctl: connect: failed to deactivate "
11093 			    "SATA port %d", cport);
11094 		} else {
11095 			SATA_CPORT_STATE(sata_hba_inst, cport) =
11096 			    SATA_PSTATE_FAILED;
11097 			SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
11098 			    "sata_hba_ioctl: connect: failed to deactivate "
11099 			    "SATA port %d:%d", cport, pmport);
11100 		}
11101 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11102 		    cport)->cport_mutex);
11103 		return (EIO);
11104 	}
11105 	/*
11106 	 * Set port's dev_state to not ready - this will disable
11107 	 * an access to a potentially attached device.
11108 	 */
11109 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11110 	if (qual == SATA_ADDR_PMPORT) {
11111 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11112 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
11113 			sdinfo = pmportinfo->pmport_sata_drive;
11114 			ASSERT(sdinfo != NULL);
11115 		}
11116 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
11117 	} else {
11118 		/* Assuming cport */
11119 
11120 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
11121 			if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
11122 				pmultinfo =
11123 				    cportinfo->cport_devp.cport_sata_pmult;
11124 				ASSERT(pmultinfo != NULL);
11125 			} else {
11126 				sdinfo = cportinfo->cport_devp.cport_sata_drive;
11127 			}
11128 		}
11129 		cportinfo->cport_state &= ~SATA_STATE_READY;
11130 	}
11131 	if (sdinfo != NULL) {
11132 		if ((sdinfo->satadrv_type & (SATA_VALID_DEV_TYPE)) != 0) {
11133 			/*
11134 			 * If a target node exists, try to offline
11135 			 * a device and remove target node.
11136 			 */
11137 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11138 			    cport)->cport_mutex);
11139 			/* We are addressing attached device, not a port */
11140 			sata_device->satadev_addr.qual =
11141 			    sdinfo->satadrv_addr.qual;
11142 			tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11143 			    &sata_device->satadev_addr);
11144 			if (tdip != NULL && ndi_devi_offline(tdip,
11145 			    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
11146 				/*
11147 				 * Problem
11148 				 * The target node remained attached.
11149 				 * This happens when the device file was open
11150 				 * or a node was waiting for resources.
11151 				 * Cannot do anything about it.
11152 				 */
11153 				if (qual == SATA_ADDR_CPORT) {
11154 					SATA_LOG_D((sata_hba_inst, CE_WARN,
11155 					    "sata_hba_ioctl: disconnect: could "
11156 					    "not unconfigure device before "
11157 					    "disconnecting the SATA port %d",
11158 					    cport));
11159 				} else {
11160 					SATA_LOG_D((sata_hba_inst, CE_WARN,
11161 					    "sata_hba_ioctl: disconnect: could "
11162 					    "not unconfigure device before "
11163 					    "disconnecting the SATA port %d:%d",
11164 					    cport, pmport));
11165 				}
11166 				/*
11167 				 * Set DEVICE REMOVED state in the target
11168 				 * node. It will prevent access to the device
11169 				 * even when a new device is attached, until
11170 				 * the old target node is released, removed and
11171 				 * recreated for a new  device.
11172 				 */
11173 				sata_set_device_removed(tdip);
11174 
11175 				/*
11176 				 * Instruct event daemon to try the target
11177 				 * node cleanup later.
11178 				 */
11179 				sata_set_target_node_cleanup(
11180 				    sata_hba_inst, &sata_device->satadev_addr);
11181 			}
11182 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11183 			    cport)->cport_mutex);
11184 		}
11185 
11186 		/* Remove and release sata_drive info structure. */
11187 		if (pmportinfo != NULL) {
11188 			SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport) =
11189 			    NULL;
11190 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11191 		} else {
11192 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11193 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11194 		}
11195 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
11196 	}
11197 #if 0
11198 	else if (pmultinfo != NULL) {
11199 		/*
11200 		 * Port Multiplier itself needs special handling.
11201 		 * All device ports need to be processed here!
11202 		 */
11203 	}
11204 #endif
11205 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11206 	/* Just ask HBA driver to deactivate port */
11207 	/*	sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; */
11208 
11209 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
11210 	    (SATA_DIP(sata_hba_inst), sata_device);
11211 
11212 	/*
11213 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11214 	 * without the hint (to force listener to investivate the state).
11215 	 */
11216 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11217 	    SE_NO_HINT);
11218 
11219 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11220 	sata_update_port_info(sata_hba_inst, sata_device);
11221 
11222 	if (rval != SATA_SUCCESS) {
11223 		/*
11224 		 * Port deactivation failure - do not
11225 		 * change port state unless the state
11226 		 * returned by HBA indicates a port failure.
11227 		 * NOTE: device structures were released, so devices now are
11228 		 * invisible! Port reset is needed to re-enumerate devices.
11229 		 */
11230 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11231 			if (pmportinfo != NULL)
11232 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
11233 			else
11234 				cportinfo->cport_state = SATA_PSTATE_FAILED;
11235 			rv = EIO;
11236 		}
11237 	} else {
11238 		/*
11239 		 * Deactivation succeded. From now on the sata framework
11240 		 * will not care what is happening to the device, until
11241 		 * the port is activated again.
11242 		 */
11243 		cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
11244 	}
11245 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11246 	return (rv);
11247 }
11248 
11249 
11250 
11251 /*
11252  * Process sata port connect request
11253  * The sata cfgadm pluging will invoke this operation only if port was found
11254  * in the disconnect state (failed state is also treated as the disconnected
11255  * state).
11256  * DEVCTL_AP_CONNECT would invoke  sata_hba_inst->satahba_tran->
11257  * sata_tran_hotplug_ops->sata_tran_port_activate().
11258  * If successful and a device is found attached to the port,
11259  * the initialization sequence is executed to attach a device structure to
11260  * a port structure. The state of the port and a device would be set
11261  * appropriately.
11262  * The device is not set in configured state (system-wise) by this operation.
11263  *
11264  * Note, that activating the port may generate link events,
11265  * so it is important that following processing and the
11266  * event processing does not interfere with each other!
11267  *
11268  * This operation may remove port failed state and will
11269  * try to make port active and in good standing.
11270  *
11271  * NOTE: Port multiplier code is not completed nor tested.
11272  */
11273 
11274 static int
11275 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst,
11276     sata_device_t *sata_device)
11277 {
11278 	int cport, pmport, qual;
11279 	int rv = 0;
11280 
11281 	cport = sata_device->satadev_addr.cport;
11282 	pmport = sata_device->satadev_addr.pmport;
11283 	qual = sata_device->satadev_addr.qual;
11284 
11285 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
11286 
11287 	/*
11288 	 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->
11289 	 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate().
11290 	 * Perform sanity check now.
11291 	 */
11292 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) {
11293 		/* No physical port activation supported. */
11294 		return (EINVAL);
11295 	}
11296 
11297 	/* Just ask HBA driver to activate port */
11298 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11299 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11300 		/*
11301 		 * Port activation failure.
11302 		 */
11303 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11304 		    cport)->cport_mutex);
11305 		sata_update_port_info(sata_hba_inst, sata_device);
11306 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11307 			if (qual == SATA_ADDR_DCPORT) {
11308 				SATA_CPORT_STATE(sata_hba_inst, cport) =
11309 				    SATA_PSTATE_FAILED;
11310 				SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
11311 				    "sata_hba_ioctl: connect: failed to "
11312 				    "activate SATA port %d", cport);
11313 			} else { /* port multiplier device port */
11314 				SATA_PMPORT_STATE(sata_hba_inst, cport,
11315 				    pmport) = SATA_PSTATE_FAILED;
11316 				SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
11317 				    "sata_hba_ioctl: connect: failed to "
11318 				    "activate SATA port %d:%d", cport, pmport);
11319 
11320 			}
11321 		}
11322 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11323 		    cport)->cport_mutex);
11324 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
11325 		    "sata_hba_ioctl: connect: failed to activate SATA "
11326 		    "port %d:%d", cport, pmport);
11327 		return (EIO);
11328 	}
11329 
11330 	/* Virgin port state - will be updated by the port re-probe. */
11331 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11332 	if (qual == SATA_ADDR_CPORT)
11333 		SATA_CPORT_STATE(sata_hba_inst, cport) = 0;
11334 	else /* port multiplier device port */
11335 		SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0;
11336 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11337 
11338 	/*
11339 	 * Probe the port to find its state and attached device.
11340 	 */
11341 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11342 	    SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE)
11343 		rv = EIO;
11344 
11345 	/*
11346 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11347 	 * without the hint
11348 	 */
11349 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11350 	    SE_NO_HINT);
11351 
11352 	/*
11353 	 * If there is a device attached to the port, emit
11354 	 * a message.
11355 	 */
11356 	if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11357 
11358 		if (qual == SATA_ADDR_CPORT) {
11359 			sata_log(sata_hba_inst, CE_WARN,
11360 			    "SATA device detected at port %d", cport);
11361 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11362 				/*
11363 				 * A device was not successfully identified
11364 				 */
11365 				sata_log(sata_hba_inst, CE_WARN,
11366 				    "Could not identify SATA "
11367 				    "device at port %d", cport);
11368 			}
11369 		} else { /* port multiplier device port */
11370 			sata_log(sata_hba_inst, CE_WARN,
11371 			    "SATA device detected at port %d:%d",
11372 			    cport, pmport);
11373 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11374 				/*
11375 				 * A device was not successfully identified
11376 				 */
11377 				sata_log(sata_hba_inst, CE_WARN,
11378 				    "Could not identify SATA "
11379 				    "device at port %d:%d", cport, pmport);
11380 			}
11381 		}
11382 	}
11383 
11384 	return (rv);
11385 }
11386 
11387 
11388 /*
11389  * Process sata device unconfigure request.
11390  * The unconfigure operation uses generic nexus operation to
11391  * offline a device. It leaves a target device node attached.
11392  * and obviously sata_drive_info attached as well, because
11393  * from the hardware point of view nothing has changed.
11394  */
11395 static int
11396 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst,
11397     sata_device_t *sata_device)
11398 {
11399 	int rv = 0;
11400 	dev_info_t *tdip;
11401 
11402 	/* We are addressing attached device, not a port */
11403 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
11404 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11405 	else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT)
11406 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11407 
11408 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11409 	    &sata_device->satadev_addr)) != NULL) {
11410 
11411 		if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) {
11412 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11413 			    "sata_hba_ioctl: unconfigure: "
11414 			    "failed to unconfigure device at SATA port %d:%d",
11415 			    sata_device->satadev_addr.cport,
11416 			    sata_device->satadev_addr.pmport));
11417 			rv = EIO;
11418 		}
11419 		/*
11420 		 * The target node devi_state should be marked with
11421 		 * DEVI_DEVICE_OFFLINE by ndi_devi_offline().
11422 		 * This would be the indication for cfgadm that
11423 		 * the AP node occupant state is 'unconfigured'.
11424 		 */
11425 
11426 	} else {
11427 		/*
11428 		 * This would indicate a failure on the part of cfgadm
11429 		 * to detect correct state of the node prior to this
11430 		 * call - one cannot unconfigure non-existing device.
11431 		 */
11432 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11433 		    "sata_hba_ioctl: unconfigure: "
11434 		    "attempt to unconfigure non-existing device "
11435 		    "at SATA port %d:%d",
11436 		    sata_device->satadev_addr.cport,
11437 		    sata_device->satadev_addr.pmport));
11438 		rv = ENXIO;
11439 	}
11440 	return (rv);
11441 }
11442 
11443 /*
11444  * Process sata device configure request
11445  * If port is in a failed state, operation is aborted - one has to use
11446  * an explicit connect or port activate request to try to get a port into
11447  * non-failed mode. Port reset wil also work in such situation.
11448  * If the port is in disconnected (shutdown) state, the connect operation is
11449  * attempted prior to any other action.
11450  * When port is in the active state, there is a device attached and the target
11451  * node exists, a device was most likely offlined.
11452  * If target node does not exist, a new target node is created. In both cases
11453  * an attempt is made to online (configure) the device.
11454  *
11455  * NOTE: Port multiplier code is not completed nor tested.
11456  */
11457 static int
11458 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst,
11459     sata_device_t *sata_device)
11460 {
11461 	int cport, pmport, qual;
11462 	int rval;
11463 	boolean_t target = TRUE;
11464 	sata_cport_info_t *cportinfo;
11465 	sata_pmport_info_t *pmportinfo = NULL;
11466 	dev_info_t *tdip;
11467 	sata_drive_info_t *sdinfo;
11468 
11469 	cport = sata_device->satadev_addr.cport;
11470 	pmport = sata_device->satadev_addr.pmport;
11471 	qual = sata_device->satadev_addr.qual;
11472 
11473 	/* Get current port state */
11474 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11475 	    (SATA_DIP(sata_hba_inst), sata_device);
11476 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11477 	sata_update_port_info(sata_hba_inst, sata_device);
11478 
11479 	if (rval != SATA_SUCCESS ||
11480 	    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
11481 		/*
11482 		 * Obviously, device on a failed port is not visible
11483 		 */
11484 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11485 		return (ENXIO);
11486 	}
11487 
11488 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11489 	if (qual == SATA_ADDR_PMPORT)
11490 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11491 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11492 
11493 	if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) {
11494 		/* need to activate port */
11495 		target = FALSE;
11496 
11497 		/* Sanity check */
11498 		if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
11499 			return (ENXIO);
11500 
11501 		/* Just let HBA driver to activate port */
11502 		if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11503 		    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11504 			/*
11505 			 * Port activation failure - do not change port state
11506 			 * unless the state returned by HBA indicates a port
11507 			 * failure.
11508 			 */
11509 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11510 			    cport)->cport_mutex);
11511 			sata_update_port_info(sata_hba_inst, sata_device);
11512 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11513 				if (qual == SATA_ADDR_PMPORT)
11514 					pmportinfo->pmport_state =
11515 					    SATA_PSTATE_FAILED;
11516 				else
11517 					cportinfo->cport_state =
11518 					    SATA_PSTATE_FAILED;
11519 			}
11520 			mutex_exit(&SATA_CPORT_INFO(
11521 			    sata_hba_inst, cport)->cport_mutex);
11522 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11523 			    "sata_hba_ioctl: configure: "
11524 			    "failed to activate SATA port %d:%d",
11525 			    cport, pmport));
11526 			return (EIO);
11527 		}
11528 		/*
11529 		 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11530 		 * without the hint.
11531 		 */
11532 		sata_gen_sysevent(sata_hba_inst,
11533 		    &sata_device->satadev_addr, SE_NO_HINT);
11534 
11535 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11536 		    cport_mutex);
11537 		/* Virgin port state */
11538 		if (qual == SATA_ADDR_PMPORT)
11539 			pmportinfo->pmport_state = 0;
11540 		else
11541 			cportinfo->cport_state = 0;
11542 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11543 	}
11544 	/*
11545 	 * Always reprobe port, to get current device info.
11546 	 */
11547 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11548 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
11549 		return (EIO);
11550 
11551 	if (sata_device->satadev_type != SATA_DTYPE_NONE && target == FALSE) {
11552 		if (qual == SATA_ADDR_PMPORT) {
11553 			/*
11554 			 * That's the transition from "inactive" port
11555 			 * to active one with device attached.
11556 			 */
11557 			sata_log(sata_hba_inst, CE_WARN,
11558 			    "SATA device detected at port %d:%d",
11559 			    cport, pmport);
11560 		} else {
11561 			/*
11562 			 * When PM is attached to the cport and cport is
11563 			 * activated, every PM device port needs to be reprobed.
11564 			 * We need to emit message for all devices detected
11565 			 * at port multiplier's device ports.
11566 			 * Add such code here.
11567 			 * For now, just inform about device attached to
11568 			 * cport.
11569 			 */
11570 			sata_log(sata_hba_inst, CE_WARN,
11571 			    "SATA device detected at port %d", cport);
11572 		}
11573 	}
11574 
11575 	/*
11576 	 * This is where real configuration operation starts.
11577 	 *
11578 	 * When PM is attached to the cport and cport is activated,
11579 	 * devices attached PM device ports may have to be configured
11580 	 * explicitly. This may change when port multiplier is supported.
11581 	 * For now, configure only disks and other valid target devices.
11582 	 */
11583 	if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) {
11584 		if (qual == SATA_ADDR_CPORT) {
11585 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11586 				/*
11587 				 * A device was not successfully identified
11588 				 */
11589 				sata_log(sata_hba_inst, CE_WARN,
11590 				    "Could not identify SATA "
11591 				    "device at port %d", cport);
11592 			}
11593 		} else { /* port multiplier device port */
11594 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11595 				/*
11596 				 * A device was not successfully identified
11597 				 */
11598 				sata_log(sata_hba_inst, CE_WARN,
11599 				    "Could not identify SATA "
11600 				    "device at port %d:%d", cport, pmport);
11601 			}
11602 		}
11603 		return (ENXIO);		/* No device to configure */
11604 	}
11605 
11606 	/*
11607 	 * Here we may have a device in reset condition,
11608 	 * but because we are just configuring it, there is
11609 	 * no need to process the reset other than just
11610 	 * to clear device reset condition in the HBA driver.
11611 	 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will
11612 	 * cause a first command sent the HBA driver with the request
11613 	 * to clear device reset condition.
11614 	 */
11615 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11616 	if (qual == SATA_ADDR_PMPORT)
11617 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11618 	else
11619 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11620 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
11621 	if (sdinfo == NULL) {
11622 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11623 		return (ENXIO);
11624 	}
11625 	if (sdinfo->satadrv_event_flags &
11626 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
11627 		sdinfo->satadrv_event_flags = 0;
11628 	}
11629 	sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
11630 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11631 
11632 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11633 	    &sata_device->satadev_addr)) != NULL) {
11634 		/*
11635 		 * Target node exists. Verify, that it belongs
11636 		 * to existing, attached device and not to
11637 		 * a removed device.
11638 		 */
11639 		if (sata_check_device_removed(tdip) == B_TRUE) {
11640 			if (qual == SATA_ADDR_DPMPORT)
11641 				sata_log(sata_hba_inst, CE_WARN,
11642 				    "SATA device at port %d cannot be "
11643 				    "configured. "
11644 				    "Application(s) accessing "
11645 				    "previously attached device "
11646 				    "have to release it before newly "
11647 				    "inserted device can be made accessible.",
11648 				    cport);
11649 			else
11650 				sata_log(sata_hba_inst, CE_WARN,
11651 				    "SATA device at port %d:%d cannot be"
11652 				    "configured. "
11653 				    "Application(s) accessing "
11654 				    "previously attached device "
11655 				    "have to release it before newly "
11656 				    "inserted device can be made accessible.",
11657 				    cport, pmport);
11658 			return (EIO);
11659 		}
11660 		/*
11661 		 * Device was not removed and re-inserted.
11662 		 * Try to online it.
11663 		 */
11664 		if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) {
11665 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11666 			    "sata_hba_ioctl: configure: "
11667 			    "onlining device at SATA port "
11668 			    "%d:%d failed", cport, pmport));
11669 			return (EIO);
11670 		}
11671 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11672 		    cport)->cport_mutex);
11673 
11674 		if (qual == SATA_ADDR_DPMPORT)
11675 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
11676 		else
11677 			cportinfo-> cport_tgtnode_clean = B_TRUE;
11678 
11679 		mutex_exit(&SATA_CPORT_INFO(
11680 		    sata_hba_inst, cport)->cport_mutex);
11681 	} else {
11682 		/*
11683 		 * No target node - need to create a new target node.
11684 		 */
11685 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11686 		    cport_mutex);
11687 		if (qual == SATA_ADDR_DPMPORT)
11688 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
11689 		else
11690 			cportinfo-> cport_tgtnode_clean = B_TRUE;
11691 
11692 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11693 		    cport_mutex);
11694 		tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
11695 		    sata_hba_inst, &sata_device->satadev_addr);
11696 		if (tdip == NULL) {
11697 			/* Configure operation failed */
11698 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11699 			    "sata_hba_ioctl: configure: "
11700 			    "configuring SATA device at port %d:%d "
11701 			    "failed", cport, pmport));
11702 			return (EIO);
11703 		}
11704 	}
11705 	return (0);
11706 }
11707 
11708 
11709 /*
11710  * Process ioctl deactivate port request.
11711  * Arbitrarily unconfigure attached device, if any.
11712  * Even if the unconfigure fails, proceed with the
11713  * port deactivation.
11714  *
11715  * NOTE: Port Multiplier code is not completed and tested.
11716  */
11717 
11718 static int
11719 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst,
11720     sata_device_t *sata_device)
11721 {
11722 	int cport, pmport, qual;
11723 	int rval, rv = 0;
11724 	sata_cport_info_t *cportinfo;
11725 	sata_pmport_info_t *pmportinfo = NULL;
11726 	dev_info_t *tdip;
11727 	sata_drive_info_t *sdinfo = NULL;
11728 
11729 	/* Sanity check */
11730 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL)
11731 		return (ENOTSUP);
11732 
11733 	cport = sata_device->satadev_addr.cport;
11734 	pmport = sata_device->satadev_addr.pmport;
11735 	qual = sata_device->satadev_addr.qual;
11736 
11737 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11738 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11739 	if (qual == SATA_ADDR_CPORT) {
11740 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11741 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
11742 			/*
11743 			 * For now, assume that port multiplier is not
11744 			 * supported, i.e. deal only with valid devices
11745 			 */
11746 			if ((cportinfo->cport_dev_type &
11747 			    SATA_VALID_DEV_TYPE) != 0)
11748 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11749 			/*
11750 			 * If attached device is a port multiplier, we will
11751 			 * have to unconfigure all devices attached to the
11752 			 * port multiplier. Add this code here.
11753 			 */
11754 		}
11755 		cportinfo->cport_state &= ~SATA_STATE_READY;
11756 	} else {
11757 		/* Port multiplier device port */
11758 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11759 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11760 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
11761 		    (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0)
11762 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11763 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
11764 	}
11765 
11766 	if (sdinfo != NULL) {
11767 		/*
11768 		 * If a target node exists, try to offline a device and
11769 		 * to remove a target node.
11770 		 */
11771 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11772 		    cport_mutex);
11773 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11774 		    &sata_device->satadev_addr);
11775 		if (tdip != NULL) {
11776 			/* target node exist */
11777 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
11778 			    "sata_hba_ioctl: port deactivate: "
11779 			    "target node exists.", NULL);
11780 
11781 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) !=
11782 			    NDI_SUCCESS) {
11783 				SATA_LOG_D((sata_hba_inst, CE_WARN,
11784 				    "sata_hba_ioctl: port deactivate: "
11785 				    "failed to unconfigure device at port "
11786 				    "%d:%d before deactivating the port",
11787 				    cport, pmport));
11788 				/*
11789 				 * Set DEVICE REMOVED state in the target
11790 				 * node. It will prevent an access to
11791 				 * the device even when a new device is
11792 				 * attached, until the old target node is
11793 				 * released, removed and recreated for a new
11794 				 * device.
11795 				 */
11796 				sata_set_device_removed(tdip);
11797 
11798 				/*
11799 				 * Instruct the event daemon to try the
11800 				 * target node cleanup later.
11801 				 */
11802 				sata_set_target_node_cleanup(sata_hba_inst,
11803 				    &sata_device->satadev_addr);
11804 			}
11805 		}
11806 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11807 		    cport_mutex);
11808 		/*
11809 		 * In any case, remove and release sata_drive_info
11810 		 * structure.
11811 		 */
11812 		if (qual == SATA_ADDR_CPORT) {
11813 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11814 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11815 		} else { /* port multiplier device port */
11816 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11817 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11818 		}
11819 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
11820 	}
11821 	if (qual == SATA_ADDR_CPORT) {
11822 		cportinfo->cport_state &= ~(SATA_STATE_PROBED |
11823 		    SATA_STATE_PROBING);
11824 	} else { /* port multiplier device port */
11825 		pmportinfo->pmport_state &= ~(SATA_STATE_PROBED |
11826 		    SATA_STATE_PROBING);
11827 	}
11828 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11829 
11830 	/* Just let HBA driver to deactivate port */
11831 	sata_device->satadev_addr.qual = qual;
11832 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
11833 	    (SATA_DIP(sata_hba_inst), sata_device);
11834 
11835 	/*
11836 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11837 	 * without the hint
11838 	 */
11839 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11840 	    SE_NO_HINT);
11841 
11842 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11843 	sata_update_port_info(sata_hba_inst, sata_device);
11844 	if (qual == SATA_ADDR_CPORT) {
11845 		if (rval != SATA_SUCCESS) {
11846 			/*
11847 			 * Port deactivation failure - do not change port state
11848 			 * unless the state returned by HBA indicates a port
11849 			 * failure.
11850 			 */
11851 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11852 				SATA_CPORT_STATE(sata_hba_inst, cport) =
11853 				    SATA_PSTATE_FAILED;
11854 			}
11855 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11856 			    "sata_hba_ioctl: port deactivate: "
11857 			    "cannot deactivate SATA port %d", cport));
11858 			rv = EIO;
11859 		} else {
11860 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
11861 		}
11862 	} else {
11863 		if (rval != SATA_SUCCESS) {
11864 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11865 				SATA_PMPORT_STATE(sata_hba_inst, cport,
11866 				    pmport) = SATA_PSTATE_FAILED;
11867 			}
11868 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11869 			    "sata_hba_ioctl: port deactivate: "
11870 			    "cannot deactivate SATA port %d:%d",
11871 			    cport, pmport));
11872 			rv = EIO;
11873 		} else {
11874 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
11875 		}
11876 	}
11877 
11878 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11879 
11880 	return (rv);
11881 }
11882 
11883 /*
11884  * Process ioctl port activate request.
11885  *
11886  * NOTE: Port multiplier code is not completed nor tested.
11887  */
11888 static int
11889 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst,
11890     sata_device_t *sata_device)
11891 {
11892 	int cport, pmport, qual;
11893 	sata_cport_info_t *cportinfo;
11894 	sata_pmport_info_t *pmportinfo = NULL;
11895 	boolean_t dev_existed = TRUE;
11896 
11897 	/* Sanity check */
11898 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
11899 		return (ENOTSUP);
11900 
11901 	cport = sata_device->satadev_addr.cport;
11902 	pmport = sata_device->satadev_addr.pmport;
11903 	qual = sata_device->satadev_addr.qual;
11904 
11905 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11906 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11907 	if (qual == SATA_ADDR_PMPORT) {
11908 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11909 		if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN ||
11910 		    pmportinfo->pmport_dev_type == SATA_DTYPE_NONE)
11911 			dev_existed = FALSE;
11912 	} else { /* cport */
11913 		if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN ||
11914 		    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
11915 			dev_existed = FALSE;
11916 	}
11917 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11918 
11919 	/* Just let HBA driver to activate port, if necessary */
11920 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11921 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11922 		/*
11923 		 * Port activation failure - do not change port state unless
11924 		 * the state returned by HBA indicates a port failure.
11925 		 */
11926 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11927 		    cport)->cport_mutex);
11928 		sata_update_port_info(sata_hba_inst, sata_device);
11929 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11930 			if (qual == SATA_ADDR_PMPORT)
11931 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
11932 			else
11933 				cportinfo->cport_state = SATA_PSTATE_FAILED;
11934 
11935 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11936 			    cport)->cport_mutex);
11937 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11938 			    "sata_hba_ioctl: port activate: cannot activate "
11939 			    "SATA port %d:%d", cport, pmport));
11940 			return (EIO);
11941 		}
11942 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11943 	}
11944 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11945 	if (qual == SATA_ADDR_PMPORT)
11946 		pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN;
11947 	else
11948 		cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN;
11949 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11950 
11951 	/*
11952 	 * Re-probe port to find its current state and possibly attached device.
11953 	 * Port re-probing may change the cportinfo device type if device is
11954 	 * found attached.
11955 	 * If port probing failed, the device type would be set to
11956 	 * SATA_DTYPE_NONE.
11957 	 */
11958 	(void) sata_reprobe_port(sata_hba_inst, sata_device,
11959 	    SATA_DEV_IDENTIFY_RETRY);
11960 
11961 	/*
11962 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11963 	 * without the hint.
11964 	 */
11965 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11966 	    SE_NO_HINT);
11967 
11968 	if (dev_existed == FALSE) {
11969 		if (qual == SATA_ADDR_PMPORT &&
11970 		    pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
11971 			/*
11972 			 * That's the transition from the "inactive" port state
11973 			 * or the active port without a device attached to the
11974 			 * active port state with a device attached.
11975 			 */
11976 			sata_log(sata_hba_inst, CE_WARN,
11977 			    "SATA device detected at port %d:%d",
11978 			    cport, pmport);
11979 		} else if (qual == SATA_ADDR_CPORT &&
11980 		    cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
11981 			/*
11982 			 * That's the transition from the "inactive" port state
11983 			 * or the active port without a device attached to the
11984 			 * active port state with a device attached.
11985 			 */
11986 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
11987 				sata_log(sata_hba_inst, CE_WARN,
11988 				    "SATA device detected at port %d", cport);
11989 			} else {
11990 				sata_log(sata_hba_inst, CE_WARN,
11991 				    "SATA port multiplier detected at port %d",
11992 				    cport);
11993 				/*
11994 				 * Because the detected device is a port
11995 				 * multiplier, we need to reprobe every device
11996 				 * port on the port multiplier and show every
11997 				 * device found attached.
11998 				 * Add this code here.
11999 				 */
12000 			}
12001 		}
12002 	}
12003 	return (0);
12004 }
12005 
12006 
12007 
12008 /*
12009  * Process ioctl reset port request.
12010  *
12011  * NOTE: Port multiplier code is not completed nor tested.
12012  */
12013 static int
12014 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst,
12015     sata_device_t *sata_device)
12016 {
12017 	int cport, pmport, qual;
12018 	int rv = 0;
12019 
12020 	cport = sata_device->satadev_addr.cport;
12021 	pmport = sata_device->satadev_addr.pmport;
12022 	qual = sata_device->satadev_addr.qual;
12023 
12024 	/* Sanity check */
12025 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
12026 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12027 		    "sata_hba_ioctl: sata_hba_tran missing required "
12028 		    "function sata_tran_reset_dport"));
12029 		return (ENOTSUP);
12030 	}
12031 
12032 	/* Ask HBA to reset port */
12033 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
12034 	    sata_device) != SATA_SUCCESS) {
12035 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12036 		    "sata_hba_ioctl: reset port: failed %d:%d",
12037 		    cport, pmport));
12038 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12039 		    cport_mutex);
12040 		sata_update_port_info(sata_hba_inst, sata_device);
12041 		if (qual == SATA_ADDR_CPORT)
12042 			SATA_CPORT_STATE(sata_hba_inst, cport) =
12043 			    SATA_PSTATE_FAILED;
12044 		else
12045 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
12046 			    SATA_PSTATE_FAILED;
12047 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12048 		    cport_mutex);
12049 		rv = EIO;
12050 	}
12051 	/*
12052 	 * Beacuse the port was reset, it should be probed and
12053 	 * attached device reinitialized. At this point the
12054 	 * port state is unknown - it's state is HBA-specific.
12055 	 * Re-probe port to get its state.
12056 	 */
12057 	if (sata_reprobe_port(sata_hba_inst, sata_device,
12058 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) {
12059 		rv = EIO;
12060 	}
12061 	return (rv);
12062 }
12063 
12064 /*
12065  * Process ioctl reset device request.
12066  *
12067  * NOTE: Port multiplier code is not completed nor tested.
12068  */
12069 static int
12070 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst,
12071     sata_device_t *sata_device)
12072 {
12073 	sata_drive_info_t *sdinfo;
12074 	int cport, pmport;
12075 	int rv = 0;
12076 
12077 	/* Sanity check */
12078 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
12079 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12080 		    "sata_hba_ioctl: sata_hba_tran missing required "
12081 		    "function sata_tran_reset_dport"));
12082 		return (ENOTSUP);
12083 	}
12084 
12085 	cport = sata_device->satadev_addr.cport;
12086 	pmport = sata_device->satadev_addr.pmport;
12087 
12088 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12089 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) {
12090 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
12091 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12092 		    sata_device->satadev_addr.cport);
12093 	} else { /* port multiplier */
12094 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
12095 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12096 		    sata_device->satadev_addr.cport,
12097 		    sata_device->satadev_addr.pmport);
12098 	}
12099 	if (sdinfo == NULL) {
12100 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12101 		return (EINVAL);
12102 	}
12103 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12104 
12105 	/* Ask HBA to reset device */
12106 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
12107 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
12108 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12109 		    "sata_hba_ioctl: reset device: failed at port %d:%d",
12110 		    cport, pmport));
12111 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12112 		    cport_mutex);
12113 		sata_update_port_info(sata_hba_inst, sata_device);
12114 		/*
12115 		 * Device info structure remains attached. Another device reset
12116 		 * or port disconnect/connect and re-probing is
12117 		 * needed to change it's state
12118 		 */
12119 		sdinfo->satadrv_state &= ~SATA_STATE_READY;
12120 		sdinfo->satadrv_state |= SATA_DSTATE_FAILED;
12121 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12122 		rv = EIO;
12123 	}
12124 	/*
12125 	 * If attached device was a port multiplier, some extra processing
12126 	 * may be needed, to bring it back (if port re-probing did not handle
12127 	 * it). Add such code here.
12128 	 */
12129 	return (rv);
12130 }
12131 
12132 
12133 /*
12134  * Process ioctl reset all request.
12135  *
12136  * NOTE: Port multiplier code is not completed nor tested.
12137  */
12138 static int
12139 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst)
12140 {
12141 	sata_device_t sata_device;
12142 	int rv = 0;
12143 	int tcport;
12144 	int tpmport = 0;
12145 
12146 	sata_device.satadev_rev = SATA_DEVICE_REV;
12147 
12148 	/*
12149 	 * There is no protection here for configured devices.
12150 	 */
12151 	/* Sanity check */
12152 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
12153 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12154 		    "sata_hba_ioctl: sata_hba_tran missing required "
12155 		    "function sata_tran_reset_dport"));
12156 		return (ENOTSUP);
12157 	}
12158 
12159 	/*
12160 	 * Need to lock all ports, not just one.
12161 	 * If any port is locked by event processing, fail the whole operation.
12162 	 * One port is already locked, but for simplicity lock it again.
12163 	 */
12164 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
12165 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
12166 		    cport_mutex);
12167 		if (((SATA_CPORT_INFO(sata_hba_inst, tcport)->
12168 		    cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) {
12169 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
12170 			    cport_mutex);
12171 			rv = EBUSY;
12172 			break;
12173 		} else {
12174 			SATA_CPORT_INFO(sata_hba_inst, tcport)->
12175 			    cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
12176 			/*
12177 			 * If there is a port multiplier attached, we may need
12178 			 * to lock its port as well. If so, add such code here.
12179 			 */
12180 		}
12181 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
12182 		    cport_mutex);
12183 	}
12184 
12185 	if (rv == 0) {
12186 		/*
12187 		 * All cports were successfully locked.
12188 		 * Reset main SATA controller only for now - no PMult.
12189 		 * Set the device address to port 0, to have a valid device
12190 		 * address.
12191 		 */
12192 		sata_device.satadev_addr.qual = SATA_ADDR_CNTRL;
12193 		sata_device.satadev_addr.cport = 0;
12194 		sata_device.satadev_addr.pmport = 0;
12195 
12196 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
12197 		    (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) {
12198 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12199 			    "sata_hba_ioctl: reset controller failed"));
12200 			return (EIO);
12201 		}
12202 		/*
12203 		 * Because ports were reset, port states are unknown.
12204 		 * They should be re-probed to get their state and
12205 		 * attached devices should be reinitialized.
12206 		 * Add code here to re-probe port multiplier device ports.
12207 		 */
12208 		for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst);
12209 		    tcport++) {
12210 			sata_device.satadev_addr.cport = tcport;
12211 			sata_device.satadev_addr.pmport = tpmport;
12212 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
12213 
12214 			if (sata_reprobe_port(sata_hba_inst, &sata_device,
12215 			    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
12216 				rv = EIO;
12217 		}
12218 	}
12219 	/*
12220 	 * Unlock all ports
12221 	 */
12222 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
12223 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
12224 		    cport_mutex);
12225 		SATA_CPORT_INFO(sata_hba_inst, tcport)->
12226 		    cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
12227 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
12228 		    cport_mutex);
12229 	}
12230 
12231 	/*
12232 	 * This operation returns EFAULT if either reset
12233 	 * controller failed or a re-probing of any port failed.
12234 	 */
12235 	return (rv);
12236 }
12237 
12238 
12239 /*
12240  * Process ioctl port self test request.
12241  *
12242  * NOTE: Port multiplier code is not completed nor tested.
12243  */
12244 static int
12245 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst,
12246     sata_device_t *sata_device)
12247 {
12248 	int cport, pmport, qual;
12249 	int rv = 0;
12250 
12251 	/* Sanity check */
12252 	if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL)
12253 		return (ENOTSUP);
12254 
12255 	cport = sata_device->satadev_addr.cport;
12256 	pmport = sata_device->satadev_addr.pmport;
12257 	qual = sata_device->satadev_addr.qual;
12258 
12259 	/*
12260 	 * There is no protection here for a configured
12261 	 * device attached to this port.
12262 	 */
12263 
12264 	if ((*SATA_SELFTEST_FUNC(sata_hba_inst))
12265 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
12266 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12267 		    "sata_hba_ioctl: port selftest: "
12268 		    "failed port %d:%d", cport, pmport));
12269 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12270 		    cport_mutex);
12271 		sata_update_port_info(sata_hba_inst, sata_device);
12272 		if (qual == SATA_ADDR_CPORT)
12273 			SATA_CPORT_STATE(sata_hba_inst, cport) =
12274 			    SATA_PSTATE_FAILED;
12275 		else /* port ultiplier device port */
12276 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
12277 			    SATA_PSTATE_FAILED;
12278 
12279 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12280 		    cport_mutex);
12281 		return (EIO);
12282 	}
12283 	/*
12284 	 * Beacuse the port was reset in the course of testing, it should be
12285 	 * re-probed and attached device state should be restored. At this
12286 	 * point the port state is unknown - it's state is HBA-specific.
12287 	 * Force port re-probing to get it into a known state.
12288 	 */
12289 	if (sata_reprobe_port(sata_hba_inst, sata_device,
12290 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
12291 		rv = EIO;
12292 	return (rv);
12293 }
12294 
12295 
12296 /*
12297  * sata_cfgadm_state:
12298  * Use the sata port state and state of the target node to figure out
12299  * the cfgadm_state.
12300  *
12301  * The port argument is a value with encoded cport,
12302  * pmport and address qualifier, in the same manner as a scsi target number.
12303  * SCSI_TO_SATA_CPORT macro extracts cport number,
12304  * SCSI_TO_SATA_PMPORT extracts pmport number and
12305  * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag.
12306  *
12307  * For now, support is for cports only - no port multiplier device ports.
12308  */
12309 
12310 static void
12311 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port,
12312     devctl_ap_state_t *ap_state)
12313 {
12314 	uint16_t	cport;
12315 	int		port_state;
12316 	sata_drive_info_t *sdinfo;
12317 
12318 	/* Cport only */
12319 	cport = SCSI_TO_SATA_CPORT(port);
12320 
12321 	port_state = SATA_CPORT_STATE(sata_hba_inst, cport);
12322 	if (port_state & SATA_PSTATE_SHUTDOWN ||
12323 	    port_state & SATA_PSTATE_FAILED) {
12324 		ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
12325 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12326 		if (port_state & SATA_PSTATE_FAILED)
12327 			ap_state->ap_condition = AP_COND_FAILED;
12328 		else
12329 			ap_state->ap_condition = AP_COND_UNKNOWN;
12330 
12331 		return;
12332 	}
12333 
12334 	/* Need to check pmult device port here as well, when supported */
12335 
12336 	/* Port is enabled and ready */
12337 
12338 	switch (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport)) {
12339 	case SATA_DTYPE_NONE:
12340 	{
12341 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12342 		ap_state->ap_condition = AP_COND_OK;
12343 		/* No device attached */
12344 		ap_state->ap_rstate = AP_RSTATE_EMPTY;
12345 		break;
12346 	}
12347 	case SATA_DTYPE_UNKNOWN:
12348 	case SATA_DTYPE_PMULT:	/* Until PMult is supported */
12349 	case SATA_DTYPE_ATADISK:
12350 	case SATA_DTYPE_ATAPICD:
12351 	case SATA_DTYPE_ATAPITAPE:
12352 	{
12353 		dev_info_t *tdip = NULL;
12354 		dev_info_t *dip = NULL;
12355 		int circ;
12356 
12357 		dip = SATA_DIP(sata_hba_inst);
12358 		tdip = sata_get_target_dip(dip, port);
12359 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
12360 		if (tdip != NULL) {
12361 			ndi_devi_enter(dip, &circ);
12362 			mutex_enter(&(DEVI(tdip)->devi_lock));
12363 			if (DEVI_IS_DEVICE_REMOVED(tdip)) {
12364 				/*
12365 				 * There could be the case where previously
12366 				 * configured and opened device was removed
12367 				 * and unknown device was plugged.
12368 				 * In such case we want to show a device, and
12369 				 * its configured or unconfigured state but
12370 				 * indicate unusable condition untill the
12371 				 * old target node is released and removed.
12372 				 */
12373 				ap_state->ap_condition = AP_COND_UNUSABLE;
12374 			} else {
12375 				mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst,
12376 				    cport));
12377 				sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12378 				    cport);
12379 				if (sdinfo != NULL) {
12380 					if ((sdinfo->satadrv_state &
12381 					    SATA_DSTATE_FAILED) != 0)
12382 						ap_state->ap_condition =
12383 						    AP_COND_FAILED;
12384 					else
12385 						ap_state->ap_condition =
12386 						    AP_COND_OK;
12387 				} else {
12388 					ap_state->ap_condition =
12389 					    AP_COND_UNKNOWN;
12390 				}
12391 				mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst,
12392 				    cport));
12393 			}
12394 			if ((DEVI_IS_DEVICE_OFFLINE(tdip)) ||
12395 			    (DEVI_IS_DEVICE_DOWN(tdip))) {
12396 				ap_state->ap_ostate =
12397 				    AP_OSTATE_UNCONFIGURED;
12398 			} else {
12399 				ap_state->ap_ostate =
12400 				    AP_OSTATE_CONFIGURED;
12401 			}
12402 			mutex_exit(&(DEVI(tdip)->devi_lock));
12403 			ndi_devi_exit(dip, circ);
12404 		} else {
12405 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12406 			ap_state->ap_condition = AP_COND_UNKNOWN;
12407 		}
12408 		break;
12409 	}
12410 	default:
12411 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
12412 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12413 		ap_state->ap_condition = AP_COND_UNKNOWN;
12414 		/*
12415 		 * This is actually internal error condition (non fatal),
12416 		 * because we have already checked all defined device types.
12417 		 */
12418 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12419 		    "sata_cfgadm_state: Internal error: "
12420 		    "unknown device type"));
12421 		break;
12422 	}
12423 }
12424 
12425 
12426 /*
12427  * Process ioctl get device path request.
12428  *
12429  * NOTE: Port multiplier code is not completed nor tested.
12430  */
12431 static int
12432 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst,
12433     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12434 {
12435 	char path[MAXPATHLEN];
12436 	uint32_t size;
12437 	dev_info_t *tdip;
12438 
12439 	(void) strcpy(path, "/devices");
12440 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
12441 	    &sata_device->satadev_addr)) == NULL) {
12442 		/*
12443 		 * No such device. If this is a request for a size, do not
12444 		 * return EINVAL for non-existing target, because cfgadm
12445 		 * will then indicate a meaningless ioctl failure.
12446 		 * If this is a request for a path, indicate invalid
12447 		 * argument.
12448 		 */
12449 		if (ioc->get_size == 0)
12450 			return (EINVAL);
12451 	} else {
12452 		(void) ddi_pathname(tdip, path + strlen(path));
12453 	}
12454 	size = strlen(path) + 1;
12455 
12456 	if (ioc->get_size != 0) {
12457 		if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz,
12458 		    mode) != 0)
12459 			return (EFAULT);
12460 	} else {
12461 		if (ioc->bufsiz != size)
12462 			return (EINVAL);
12463 
12464 		else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz,
12465 		    mode) != 0)
12466 			return (EFAULT);
12467 	}
12468 	return (0);
12469 }
12470 
12471 /*
12472  * Process ioctl get attachment point type request.
12473  *
12474  * NOTE: Port multiplier code is not completed nor tested.
12475  */
12476 static	int
12477 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst,
12478     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12479 {
12480 	uint32_t	type_len;
12481 	const char	*ap_type;
12482 	int		dev_type;
12483 
12484 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12485 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst,
12486 		    sata_device->satadev_addr.cport);
12487 	else /* pmport */
12488 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12489 		    sata_device->satadev_addr.cport,
12490 		    sata_device->satadev_addr.pmport);
12491 
12492 	switch (dev_type) {
12493 	case SATA_DTYPE_NONE:
12494 		ap_type = "port";
12495 		break;
12496 
12497 	case SATA_DTYPE_ATADISK:
12498 		ap_type = "disk";
12499 		break;
12500 
12501 	case SATA_DTYPE_ATAPICD:
12502 		ap_type = "cd/dvd";
12503 		break;
12504 
12505 	case SATA_DTYPE_ATAPITAPE:
12506 		ap_type = "tape";
12507 		break;
12508 
12509 	case SATA_DTYPE_PMULT:
12510 		ap_type = "pmult";
12511 		break;
12512 
12513 	case SATA_DTYPE_UNKNOWN:
12514 		ap_type = "unknown";
12515 		break;
12516 
12517 	default:
12518 		ap_type = "unsupported";
12519 		break;
12520 
12521 	} /* end of dev_type switch */
12522 
12523 	type_len = strlen(ap_type) + 1;
12524 
12525 	if (ioc->get_size) {
12526 		if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz,
12527 		    mode) != 0)
12528 			return (EFAULT);
12529 	} else {
12530 		if (ioc->bufsiz != type_len)
12531 			return (EINVAL);
12532 
12533 		if (ddi_copyout((void *)ap_type, ioc->buf,
12534 		    ioc->bufsiz, mode) != 0)
12535 			return (EFAULT);
12536 	}
12537 	return (0);
12538 
12539 }
12540 
12541 /*
12542  * Process ioctl get device model info request.
12543  * This operation should return to cfgadm the device model
12544  * information string
12545  *
12546  * NOTE: Port multiplier code is not completed nor tested.
12547  */
12548 static	int
12549 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst,
12550     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12551 {
12552 	sata_drive_info_t *sdinfo;
12553 	uint32_t info_len;
12554 	char ap_info[SATA_ID_MODEL_LEN + 1];
12555 
12556 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12557 	    sata_device->satadev_addr.cport)->cport_mutex);
12558 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12559 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12560 		    sata_device->satadev_addr.cport);
12561 	else /* port multiplier */
12562 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12563 		    sata_device->satadev_addr.cport,
12564 		    sata_device->satadev_addr.pmport);
12565 	if (sdinfo == NULL) {
12566 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12567 		    sata_device->satadev_addr.cport)->cport_mutex);
12568 		return (EINVAL);
12569 	}
12570 
12571 #ifdef	_LITTLE_ENDIAN
12572 	swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
12573 #else	/* _LITTLE_ENDIAN */
12574 	bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
12575 #endif	/* _LITTLE_ENDIAN */
12576 
12577 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12578 	    sata_device->satadev_addr.cport)->cport_mutex);
12579 
12580 	ap_info[SATA_ID_MODEL_LEN] = '\0';
12581 
12582 	info_len = strlen(ap_info) + 1;
12583 
12584 	if (ioc->get_size) {
12585 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12586 		    mode) != 0)
12587 			return (EFAULT);
12588 	} else {
12589 		if (ioc->bufsiz < info_len)
12590 			return (EINVAL);
12591 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12592 		    mode) != 0)
12593 			return (EFAULT);
12594 	}
12595 	return (0);
12596 }
12597 
12598 
12599 /*
12600  * Process ioctl get device firmware revision info request.
12601  * This operation should return to cfgadm the device firmware revision
12602  * information string
12603  *
12604  * NOTE: Port multiplier code is not completed nor tested.
12605  */
12606 static	int
12607 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst,
12608     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12609 {
12610 	sata_drive_info_t *sdinfo;
12611 	uint32_t info_len;
12612 	char ap_info[SATA_ID_FW_LEN + 1];
12613 
12614 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12615 	    sata_device->satadev_addr.cport)->cport_mutex);
12616 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12617 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12618 		    sata_device->satadev_addr.cport);
12619 	else /* port multiplier */
12620 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12621 		    sata_device->satadev_addr.cport,
12622 		    sata_device->satadev_addr.pmport);
12623 	if (sdinfo == NULL) {
12624 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12625 		    sata_device->satadev_addr.cport)->cport_mutex);
12626 		return (EINVAL);
12627 	}
12628 
12629 #ifdef	_LITTLE_ENDIAN
12630 	swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
12631 #else	/* _LITTLE_ENDIAN */
12632 	bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
12633 #endif	/* _LITTLE_ENDIAN */
12634 
12635 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12636 	    sata_device->satadev_addr.cport)->cport_mutex);
12637 
12638 	ap_info[SATA_ID_FW_LEN] = '\0';
12639 
12640 	info_len = strlen(ap_info) + 1;
12641 
12642 	if (ioc->get_size) {
12643 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12644 		    mode) != 0)
12645 			return (EFAULT);
12646 	} else {
12647 		if (ioc->bufsiz < info_len)
12648 			return (EINVAL);
12649 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12650 		    mode) != 0)
12651 			return (EFAULT);
12652 	}
12653 	return (0);
12654 }
12655 
12656 
12657 /*
12658  * Process ioctl get device serial number info request.
12659  * This operation should return to cfgadm the device serial number string.
12660  *
12661  * NOTE: Port multiplier code is not completed nor tested.
12662  */
12663 static	int
12664 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst,
12665     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12666 {
12667 	sata_drive_info_t *sdinfo;
12668 	uint32_t info_len;
12669 	char ap_info[SATA_ID_SERIAL_LEN + 1];
12670 
12671 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12672 	    sata_device->satadev_addr.cport)->cport_mutex);
12673 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12674 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12675 		    sata_device->satadev_addr.cport);
12676 	else /* port multiplier */
12677 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12678 		    sata_device->satadev_addr.cport,
12679 		    sata_device->satadev_addr.pmport);
12680 	if (sdinfo == NULL) {
12681 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12682 		    sata_device->satadev_addr.cport)->cport_mutex);
12683 		return (EINVAL);
12684 	}
12685 
12686 #ifdef	_LITTLE_ENDIAN
12687 	swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
12688 #else	/* _LITTLE_ENDIAN */
12689 	bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
12690 #endif	/* _LITTLE_ENDIAN */
12691 
12692 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12693 	    sata_device->satadev_addr.cport)->cport_mutex);
12694 
12695 	ap_info[SATA_ID_SERIAL_LEN] = '\0';
12696 
12697 	info_len = strlen(ap_info) + 1;
12698 
12699 	if (ioc->get_size) {
12700 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12701 		    mode) != 0)
12702 			return (EFAULT);
12703 	} else {
12704 		if (ioc->bufsiz < info_len)
12705 			return (EINVAL);
12706 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12707 		    mode) != 0)
12708 			return (EFAULT);
12709 	}
12710 	return (0);
12711 }
12712 
12713 
12714 /*
12715  * Preset scsi extended sense data (to NO SENSE)
12716  * First 18 bytes of the sense data are preset to current valid sense
12717  * with a key NO SENSE data.
12718  *
12719  * Returns void
12720  */
12721 static void
12722 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense)
12723 {
12724 	sense->es_valid = 1;		/* Valid sense */
12725 	sense->es_class = CLASS_EXTENDED_SENSE;	/* 0x70 - current err */
12726 	sense->es_key = KEY_NO_SENSE;
12727 	sense->es_info_1 = 0;
12728 	sense->es_info_2 = 0;
12729 	sense->es_info_3 = 0;
12730 	sense->es_info_4 = 0;
12731 	sense->es_add_len = 10;	/* Additional length - replace with a def */
12732 	sense->es_cmd_info[0] = 0;
12733 	sense->es_cmd_info[1] = 0;
12734 	sense->es_cmd_info[2] = 0;
12735 	sense->es_cmd_info[3] = 0;
12736 	sense->es_add_code = 0;
12737 	sense->es_qual_code = 0;
12738 }
12739 
12740 /*
12741  * Register a legacy cmdk-style devid for the target (disk) device.
12742  *
12743  * Note: This function is called only when the HBA devinfo node has the
12744  * property "use-cmdk-devid-format" set. This property indicates that
12745  * devid compatible with old cmdk (target) driver is to be generated
12746  * for any target device attached to this controller. This will take
12747  * precedence over the devid generated by sd (target) driver.
12748  * This function is derived from cmdk_devid_setup() function in cmdk.c.
12749  */
12750 static void
12751 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo)
12752 {
12753 	char	*hwid;
12754 	int	modlen;
12755 	int	serlen;
12756 	int	rval;
12757 	ddi_devid_t	devid;
12758 
12759 	/*
12760 	 * device ID is a concatanation of model number, "=", serial number.
12761 	 */
12762 	hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP);
12763 	bcopy(&sdinfo->satadrv_id.ai_model, hwid,
12764 	    sizeof (sdinfo->satadrv_id.ai_model));
12765 	swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model));
12766 	modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model));
12767 	if (modlen == 0)
12768 		goto err;
12769 	hwid[modlen++] = '=';
12770 	bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen],
12771 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12772 	swab(&hwid[modlen], &hwid[modlen],
12773 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12774 	serlen = sata_check_modser(&hwid[modlen],
12775 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12776 	if (serlen == 0)
12777 		goto err;
12778 	hwid[modlen + serlen] = 0; /* terminate the hwid string */
12779 
12780 	/* initialize/register devid */
12781 	if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL,
12782 	    (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS)
12783 		rval = ddi_devid_register(dip, devid);
12784 
12785 	if (rval != DDI_SUCCESS)
12786 		cmn_err(CE_WARN, "sata: failed to create devid for the disk"
12787 		    " on port %d", sdinfo->satadrv_addr.cport);
12788 err:
12789 	kmem_free(hwid, LEGACY_HWID_LEN);
12790 }
12791 
12792 /*
12793  * valid model/serial string must contain a non-zero non-space characters.
12794  * trim trailing spaces/NULLs.
12795  */
12796 static int
12797 sata_check_modser(char *buf, int buf_len)
12798 {
12799 	boolean_t ret;
12800 	char *s;
12801 	int i;
12802 	int tb;
12803 	char ch;
12804 
12805 	ret = B_FALSE;
12806 	s = buf;
12807 	for (i = 0; i < buf_len; i++) {
12808 		ch = *s++;
12809 		if (ch != ' ' && ch != '\0')
12810 			tb = i + 1;
12811 		if (ch != ' ' && ch != '\0' && ch != '0')
12812 			ret = B_TRUE;
12813 	}
12814 
12815 	if (ret == B_FALSE)
12816 		return (0); /* invalid string */
12817 
12818 	return (tb); /* return length */
12819 }
12820 
12821 /*
12822  * sata_set_drive_features function compares current device features setting
12823  * with the saved device features settings and, if there is a difference,
12824  * it restores device features setting to the previously saved state.
12825  * It also arbitrarily tries to select the highest supported DMA mode.
12826  * Device Identify or Identify Packet Device data has to be current.
12827  * At the moment read ahead and write cache are considered for all devices.
12828  * For atapi devices, Removable Media Status Notification is set in addition
12829  * to common features.
12830  *
12831  * This function cannot be called in the interrupt context (it may sleep).
12832  *
12833  * The input argument sdinfo should point to the drive info structure
12834  * to be updated after features are set. Note, that only
12835  * device (packet) identify data is updated, not the flags indicating the
12836  * supported features.
12837  *
12838  * Returns SATA_SUCCESS if successful or there was nothing to do.
12839  * Device Identify data in the drive info structure pointed to by the sdinfo
12840  * arguments is updated even when no features were set or changed.
12841  *
12842  * Returns SATA_FAILURE if device features could not be set or DMA mode
12843  * for a disk cannot be set and device identify data cannot be fetched.
12844  *
12845  * Returns SATA_RETRY if device features could not be set (other than disk
12846  * DMA mode) but the device identify data was fetched successfully.
12847  *
12848  * Note: This function may fail the port, making it inaccessible.
12849  * In such case the explicit port disconnect/connect or physical device
12850  * detach/attach is required to re-evaluate port state again.
12851  */
12852 
12853 static int
12854 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst,
12855     sata_drive_info_t *sdinfo, int restore)
12856 {
12857 	int rval = SATA_SUCCESS;
12858 	int rval_set;
12859 	sata_drive_info_t new_sdinfo;
12860 	char *finfo = "sata_set_drive_features: cannot";
12861 	char *finfox;
12862 	int cache_op;
12863 
12864 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
12865 	new_sdinfo.satadrv_addr = sdinfo->satadrv_addr;
12866 	new_sdinfo.satadrv_type = sdinfo->satadrv_type;
12867 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
12868 		/*
12869 		 * Cannot get device identification - caller may retry later
12870 		 */
12871 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12872 		    "%s fetch device identify data\n", finfo);
12873 		return (SATA_FAILURE);
12874 	}
12875 	finfox = (restore != 0) ? " restore device features" :
12876 	    " initialize device features\n";
12877 
12878 	switch (sdinfo->satadrv_type) {
12879 	case SATA_DTYPE_ATADISK:
12880 		/* Arbitrarily set UDMA mode */
12881 		if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
12882 		    SATA_SUCCESS) {
12883 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12884 			    "%s set UDMA mode\n", finfo));
12885 			return (SATA_FAILURE);
12886 		}
12887 		break;
12888 	case SATA_DTYPE_ATAPICD:
12889 	case SATA_DTYPE_ATAPITAPE:
12890 		/*  Set Removable Media Status Notification, if necessary */
12891 		if ((new_sdinfo.satadrv_id.ai_cmdset83 &
12892 		    SATA_RM_STATUS_NOTIFIC) != 0 && restore != 0) {
12893 			if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) &&
12894 			    (!(new_sdinfo.satadrv_id.ai_features86 &
12895 			    SATA_RM_STATUS_NOTIFIC))) ||
12896 			    ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) &&
12897 			    (new_sdinfo.satadrv_id.ai_features86 &
12898 			    SATA_RM_STATUS_NOTIFIC))) {
12899 				/* Current setting does not match saved one */
12900 				if (sata_set_rmsn(sata_hba_inst, sdinfo,
12901 				    sdinfo->satadrv_settings &
12902 				    SATA_DEV_RMSN) != SATA_SUCCESS)
12903 					rval = SATA_FAILURE;
12904 			}
12905 		}
12906 		/*
12907 		 * We have to set Multiword DMA or UDMA, if it is supported, as
12908 		 * we want to use DMA transfer mode whenever possible.
12909 		 * Some devices require explicit setting of the DMA mode.
12910 		 */
12911 		if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) {
12912 			/* Set highest supported DMA mode */
12913 			if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
12914 			    SATA_SUCCESS) {
12915 				SATA_LOG_D((sata_hba_inst, CE_WARN,
12916 				    "%s set UDMA mode\n", finfo));
12917 				rval = SATA_FAILURE;
12918 			}
12919 		}
12920 		break;
12921 	}
12922 
12923 	if (!(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) &&
12924 	    !(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) {
12925 		/* None of the features is supported - do nothing */
12926 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12927 		    "settable features not supported\n", NULL);
12928 		goto update_sdinfo;
12929 	}
12930 
12931 	if (((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) &&
12932 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) &&
12933 	    ((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) &&
12934 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
12935 		/* Nothing to do */
12936 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12937 		    "no device features to set\n", NULL);
12938 		goto update_sdinfo;
12939 	}
12940 
12941 	if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) &&
12942 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD))) {
12943 		if (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) {
12944 			/* Enable read ahead / read cache */
12945 			cache_op = SATAC_SF_ENABLE_READ_AHEAD;
12946 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12947 			    "enabling read cache\n", NULL);
12948 		} else {
12949 			/* Disable read ahead  / read cache */
12950 			cache_op = SATAC_SF_DISABLE_READ_AHEAD;
12951 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12952 			    "disabling read cache\n", NULL);
12953 		}
12954 
12955 		/* Try to set read cache mode */
12956 		rval_set = sata_set_cache_mode(sata_hba_inst, &new_sdinfo,
12957 		    cache_op);
12958 		if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
12959 			rval = rval_set;
12960 	}
12961 
12962 	if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) &&
12963 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
12964 		if (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) {
12965 			/* Enable write cache */
12966 			cache_op = SATAC_SF_ENABLE_WRITE_CACHE;
12967 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12968 			    "enabling write cache\n", NULL);
12969 		} else {
12970 			/* Disable write cache */
12971 			cache_op = SATAC_SF_DISABLE_WRITE_CACHE;
12972 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12973 			    "disabling write cache\n", NULL);
12974 		}
12975 		/* Try to set write cache mode */
12976 		rval_set = sata_set_cache_mode(sata_hba_inst, &new_sdinfo,
12977 		    cache_op);
12978 		if (rval != SATA_FAILURE && rval_set != SATA_SUCCESS)
12979 			rval = rval_set;
12980 	}
12981 	if (rval != SATA_SUCCESS)
12982 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12983 		    "%s %s", finfo, finfox));
12984 
12985 update_sdinfo:
12986 	/*
12987 	 * We need to fetch Device Identify data again
12988 	 */
12989 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
12990 		/*
12991 		 * Cannot get device identification - retry later
12992 		 */
12993 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12994 		    "%s re-fetch device identify data\n", finfo));
12995 		rval = SATA_FAILURE;
12996 	}
12997 	/* Copy device sata info. */
12998 	sdinfo->satadrv_id = new_sdinfo.satadrv_id;
12999 
13000 	return (rval);
13001 }
13002 
13003 
13004 /*
13005  *
13006  * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if
13007  * unable to determine.
13008  *
13009  * Cannot be called in an interrupt context.
13010  *
13011  * Called by sata_build_lsense_page_2f()
13012  */
13013 
13014 static int
13015 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst,
13016     sata_drive_info_t *sdinfo)
13017 {
13018 	sata_pkt_t *spkt;
13019 	sata_cmd_t *scmd;
13020 	sata_pkt_txlate_t *spx;
13021 	int rval;
13022 
13023 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13024 	spx->txlt_sata_hba_inst = sata_hba_inst;
13025 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13026 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13027 	if (spkt == NULL) {
13028 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13029 		return (-1);
13030 	}
13031 	/* address is needed now */
13032 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13033 
13034 
13035 	/* Fill sata_pkt */
13036 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13037 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13038 	/* Synchronous mode, no callback */
13039 	spkt->satapkt_comp = NULL;
13040 	/* Timeout 30s */
13041 	spkt->satapkt_time = sata_default_pkt_time;
13042 
13043 	scmd = &spkt->satapkt_cmd;
13044 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
13045 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
13046 
13047 	/* Set up which registers need to be returned */
13048 	scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE;
13049 	scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE;
13050 
13051 	/* Build SMART_RETURN_STATUS cmd in the sata_pkt */
13052 	scmd->satacmd_addr_type = 0;		/* N/A */
13053 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
13054 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
13055 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13056 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13057 	scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS;
13058 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13059 	scmd->satacmd_cmd_reg = SATAC_SMART;
13060 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13061 	    sdinfo->satadrv_addr.cport)));
13062 
13063 
13064 	/* Send pkt to SATA HBA driver */
13065 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13066 	    SATA_TRAN_ACCEPTED ||
13067 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13068 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13069 		    sdinfo->satadrv_addr.cport)));
13070 		/*
13071 		 * Whoops, no SMART RETURN STATUS
13072 		 */
13073 		rval = -1;
13074 	} else {
13075 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13076 		    sdinfo->satadrv_addr.cport)));
13077 		if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
13078 			rval = -1;
13079 			goto fail;
13080 		}
13081 		if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
13082 			rval = -1;
13083 			goto fail;
13084 		}
13085 		if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) &&
13086 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2))
13087 			rval = 0;
13088 		else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) &&
13089 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4))
13090 			rval = 1;
13091 		else {
13092 			rval = -1;
13093 			goto fail;
13094 		}
13095 	}
13096 fail:
13097 	/* Free allocated resources */
13098 	sata_pkt_free(spx);
13099 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13100 
13101 	return (rval);
13102 }
13103 
13104 /*
13105  *
13106  * Returns 0 if succeeded, -1 otherwise
13107  *
13108  * Cannot be called in an interrupt context.
13109  *
13110  */
13111 static int
13112 sata_fetch_smart_data(
13113 	sata_hba_inst_t *sata_hba_inst,
13114 	sata_drive_info_t *sdinfo,
13115 	struct smart_data *smart_data)
13116 {
13117 	sata_pkt_t *spkt;
13118 	sata_cmd_t *scmd;
13119 	sata_pkt_txlate_t *spx;
13120 	int rval;
13121 
13122 #if ! defined(lint)
13123 	ASSERT(sizeof (struct smart_data) == 512);
13124 #endif
13125 
13126 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13127 	spx->txlt_sata_hba_inst = sata_hba_inst;
13128 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13129 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13130 	if (spkt == NULL) {
13131 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13132 		return (-1);
13133 	}
13134 	/* address is needed now */
13135 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13136 
13137 
13138 	/* Fill sata_pkt */
13139 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13140 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13141 	/* Synchronous mode, no callback */
13142 	spkt->satapkt_comp = NULL;
13143 	/* Timeout 30s */
13144 	spkt->satapkt_time = sata_default_pkt_time;
13145 
13146 	scmd = &spkt->satapkt_cmd;
13147 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13148 
13149 	/*
13150 	 * Allocate buffer for SMART data
13151 	 */
13152 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13153 	    sizeof (struct smart_data));
13154 	if (scmd->satacmd_bp == NULL) {
13155 		sata_pkt_free(spx);
13156 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13157 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13158 		    "sata_fetch_smart_data: "
13159 		    "cannot allocate buffer"));
13160 		return (-1);
13161 	}
13162 
13163 
13164 	/* Build SMART_READ_DATA cmd in the sata_pkt */
13165 	scmd->satacmd_addr_type = 0;		/* N/A */
13166 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
13167 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
13168 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13169 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13170 	scmd->satacmd_features_reg = SATA_SMART_READ_DATA;
13171 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13172 	scmd->satacmd_cmd_reg = SATAC_SMART;
13173 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13174 	    sdinfo->satadrv_addr.cport)));
13175 
13176 	/* Send pkt to SATA HBA driver */
13177 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13178 	    SATA_TRAN_ACCEPTED ||
13179 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13180 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13181 		    sdinfo->satadrv_addr.cport)));
13182 		/*
13183 		 * Whoops, no SMART DATA available
13184 		 */
13185 		rval = -1;
13186 		goto fail;
13187 	} else {
13188 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13189 		    sdinfo->satadrv_addr.cport)));
13190 		if (spx->txlt_buf_dma_handle != NULL) {
13191 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13192 			    DDI_DMA_SYNC_FORKERNEL);
13193 			ASSERT(rval == DDI_SUCCESS);
13194 		}
13195 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data,
13196 		    sizeof (struct smart_data));
13197 	}
13198 
13199 fail:
13200 	/* Free allocated resources */
13201 	sata_free_local_buffer(spx);
13202 	sata_pkt_free(spx);
13203 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13204 
13205 	return (rval);
13206 }
13207 
13208 /*
13209  * Used by LOG SENSE page 0x10
13210  * Reads (in synchronous mode) the self test log data using Read Log Ext cmd.
13211  * Note: cannot be called in the interrupt context.
13212  *
13213  * return 0 for success, -1 otherwise
13214  *
13215  */
13216 static int
13217 sata_ext_smart_selftest_read_log(
13218 	sata_hba_inst_t *sata_hba_inst,
13219 	sata_drive_info_t *sdinfo,
13220 	struct smart_ext_selftest_log *ext_selftest_log,
13221 	uint16_t block_num)
13222 {
13223 	sata_pkt_txlate_t *spx;
13224 	sata_pkt_t *spkt;
13225 	sata_cmd_t *scmd;
13226 	int rval;
13227 
13228 #if ! defined(lint)
13229 	ASSERT(sizeof (struct smart_ext_selftest_log) == 512);
13230 #endif
13231 
13232 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13233 	spx->txlt_sata_hba_inst = sata_hba_inst;
13234 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13235 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13236 	if (spkt == NULL) {
13237 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13238 		return (-1);
13239 	}
13240 	/* address is needed now */
13241 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13242 
13243 
13244 	/* Fill sata_pkt */
13245 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13246 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13247 	/* Synchronous mode, no callback */
13248 	spkt->satapkt_comp = NULL;
13249 	/* Timeout 30s */
13250 	spkt->satapkt_time = sata_default_pkt_time;
13251 
13252 	scmd = &spkt->satapkt_cmd;
13253 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13254 
13255 	/*
13256 	 * Allocate buffer for SMART extended self-test log
13257 	 */
13258 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13259 	    sizeof (struct smart_ext_selftest_log));
13260 	if (scmd->satacmd_bp == NULL) {
13261 		sata_pkt_free(spx);
13262 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13263 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13264 		    "sata_ext_smart_selftest_log: "
13265 		    "cannot allocate buffer"));
13266 		return (-1);
13267 	}
13268 
13269 	/* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */
13270 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
13271 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of selftest log */
13272 	scmd->satacmd_sec_count_msb = 0;	/* One sector of selftest log */
13273 	scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE;
13274 	scmd->satacmd_lba_low_msb = 0;
13275 	scmd->satacmd_lba_mid_lsb = block_num & 0xff;
13276 	scmd->satacmd_lba_mid_msb = block_num >> 8;
13277 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13278 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
13279 
13280 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13281 	    sdinfo->satadrv_addr.cport)));
13282 
13283 	/* Send pkt to SATA HBA driver */
13284 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13285 	    SATA_TRAN_ACCEPTED ||
13286 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13287 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13288 		    sdinfo->satadrv_addr.cport)));
13289 
13290 		/*
13291 		 * Whoops, no SMART selftest log info available
13292 		 */
13293 		rval = -1;
13294 		goto fail;
13295 	} else {
13296 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13297 		    sdinfo->satadrv_addr.cport)));
13298 
13299 		if (spx->txlt_buf_dma_handle != NULL) {
13300 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13301 			    DDI_DMA_SYNC_FORKERNEL);
13302 			ASSERT(rval == DDI_SUCCESS);
13303 		}
13304 		bcopy(scmd->satacmd_bp->b_un.b_addr,
13305 		    (uint8_t *)ext_selftest_log,
13306 		    sizeof (struct smart_ext_selftest_log));
13307 		rval = 0;
13308 	}
13309 
13310 fail:
13311 	/* Free allocated resources */
13312 	sata_free_local_buffer(spx);
13313 	sata_pkt_free(spx);
13314 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13315 
13316 	return (rval);
13317 }
13318 
13319 /*
13320  * Returns 0 for success, -1 otherwise
13321  *
13322  * SMART self-test log data is returned in buffer pointed to by selftest_log
13323  */
13324 static int
13325 sata_smart_selftest_log(
13326 	sata_hba_inst_t *sata_hba_inst,
13327 	sata_drive_info_t *sdinfo,
13328 	struct smart_selftest_log *selftest_log)
13329 {
13330 	sata_pkt_t *spkt;
13331 	sata_cmd_t *scmd;
13332 	sata_pkt_txlate_t *spx;
13333 	int rval;
13334 
13335 #if ! defined(lint)
13336 	ASSERT(sizeof (struct smart_selftest_log) == 512);
13337 #endif
13338 
13339 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13340 	spx->txlt_sata_hba_inst = sata_hba_inst;
13341 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13342 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13343 	if (spkt == NULL) {
13344 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13345 		return (-1);
13346 	}
13347 	/* address is needed now */
13348 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13349 
13350 
13351 	/* Fill sata_pkt */
13352 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13353 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13354 	/* Synchronous mode, no callback */
13355 	spkt->satapkt_comp = NULL;
13356 	/* Timeout 30s */
13357 	spkt->satapkt_time = sata_default_pkt_time;
13358 
13359 	scmd = &spkt->satapkt_cmd;
13360 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13361 
13362 	/*
13363 	 * Allocate buffer for SMART SELFTEST LOG
13364 	 */
13365 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13366 	    sizeof (struct smart_selftest_log));
13367 	if (scmd->satacmd_bp == NULL) {
13368 		sata_pkt_free(spx);
13369 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13370 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13371 		    "sata_smart_selftest_log: "
13372 		    "cannot allocate buffer"));
13373 		return (-1);
13374 	}
13375 
13376 	/* Build SMART_READ_LOG cmd in the sata_pkt */
13377 	scmd->satacmd_addr_type = 0;		/* N/A */
13378 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of SMART log */
13379 	scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE;
13380 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13381 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13382 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
13383 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13384 	scmd->satacmd_cmd_reg = SATAC_SMART;
13385 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13386 	    sdinfo->satadrv_addr.cport)));
13387 
13388 	/* Send pkt to SATA HBA driver */
13389 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13390 	    SATA_TRAN_ACCEPTED ||
13391 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13392 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13393 		    sdinfo->satadrv_addr.cport)));
13394 		/*
13395 		 * Whoops, no SMART DATA available
13396 		 */
13397 		rval = -1;
13398 		goto fail;
13399 	} else {
13400 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13401 		    sdinfo->satadrv_addr.cport)));
13402 		if (spx->txlt_buf_dma_handle != NULL) {
13403 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13404 			    DDI_DMA_SYNC_FORKERNEL);
13405 			ASSERT(rval == DDI_SUCCESS);
13406 		}
13407 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log,
13408 		    sizeof (struct smart_selftest_log));
13409 		rval = 0;
13410 	}
13411 
13412 fail:
13413 	/* Free allocated resources */
13414 	sata_free_local_buffer(spx);
13415 	sata_pkt_free(spx);
13416 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13417 
13418 	return (rval);
13419 }
13420 
13421 
13422 /*
13423  * Returns 0 for success, -1 otherwise
13424  *
13425  * SMART READ LOG data is returned in buffer pointed to by smart_log
13426  */
13427 static int
13428 sata_smart_read_log(
13429 	sata_hba_inst_t *sata_hba_inst,
13430 	sata_drive_info_t *sdinfo,
13431 	uint8_t *smart_log,		/* where the data should be returned */
13432 	uint8_t which_log,		/* which log should be returned */
13433 	uint8_t log_size)		/* # of 512 bytes in log */
13434 {
13435 	sata_pkt_t *spkt;
13436 	sata_cmd_t *scmd;
13437 	sata_pkt_txlate_t *spx;
13438 	int rval;
13439 
13440 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13441 	spx->txlt_sata_hba_inst = sata_hba_inst;
13442 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13443 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13444 	if (spkt == NULL) {
13445 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13446 		return (-1);
13447 	}
13448 	/* address is needed now */
13449 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13450 
13451 
13452 	/* Fill sata_pkt */
13453 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13454 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13455 	/* Synchronous mode, no callback */
13456 	spkt->satapkt_comp = NULL;
13457 	/* Timeout 30s */
13458 	spkt->satapkt_time = sata_default_pkt_time;
13459 
13460 	scmd = &spkt->satapkt_cmd;
13461 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13462 
13463 	/*
13464 	 * Allocate buffer for SMART READ LOG
13465 	 */
13466 	scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512);
13467 	if (scmd->satacmd_bp == NULL) {
13468 		sata_pkt_free(spx);
13469 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13470 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13471 		    "sata_smart_read_log: " "cannot allocate buffer"));
13472 		return (-1);
13473 	}
13474 
13475 	/* Build SMART_READ_LOG cmd in the sata_pkt */
13476 	scmd->satacmd_addr_type = 0;		/* N/A */
13477 	scmd->satacmd_sec_count_lsb = log_size;	/* what the caller asked for */
13478 	scmd->satacmd_lba_low_lsb = which_log;	/* which log page */
13479 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13480 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13481 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
13482 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13483 	scmd->satacmd_cmd_reg = SATAC_SMART;
13484 
13485 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13486 	    sdinfo->satadrv_addr.cport)));
13487 
13488 	/* Send pkt to SATA HBA driver */
13489 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13490 	    SATA_TRAN_ACCEPTED ||
13491 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13492 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13493 		    sdinfo->satadrv_addr.cport)));
13494 
13495 		/*
13496 		 * Whoops, no SMART DATA available
13497 		 */
13498 		rval = -1;
13499 		goto fail;
13500 	} else {
13501 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13502 		    sdinfo->satadrv_addr.cport)));
13503 
13504 		if (spx->txlt_buf_dma_handle != NULL) {
13505 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13506 			    DDI_DMA_SYNC_FORKERNEL);
13507 			ASSERT(rval == DDI_SUCCESS);
13508 		}
13509 		bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512);
13510 		rval = 0;
13511 	}
13512 
13513 fail:
13514 	/* Free allocated resources */
13515 	sata_free_local_buffer(spx);
13516 	sata_pkt_free(spx);
13517 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13518 
13519 	return (rval);
13520 }
13521 
13522 /*
13523  * Used by LOG SENSE page 0x10
13524  *
13525  * return 0 for success, -1 otherwise
13526  *
13527  */
13528 static int
13529 sata_read_log_ext_directory(
13530 	sata_hba_inst_t *sata_hba_inst,
13531 	sata_drive_info_t *sdinfo,
13532 	struct read_log_ext_directory *logdir)
13533 {
13534 	sata_pkt_txlate_t *spx;
13535 	sata_pkt_t *spkt;
13536 	sata_cmd_t *scmd;
13537 	int rval;
13538 
13539 #if ! defined(lint)
13540 	ASSERT(sizeof (struct read_log_ext_directory) == 512);
13541 #endif
13542 
13543 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13544 	spx->txlt_sata_hba_inst = sata_hba_inst;
13545 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13546 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13547 	if (spkt == NULL) {
13548 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13549 		return (-1);
13550 	}
13551 
13552 	/* Fill sata_pkt */
13553 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13554 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13555 	/* Synchronous mode, no callback */
13556 	spkt->satapkt_comp = NULL;
13557 	/* Timeout 30s */
13558 	spkt->satapkt_time = sata_default_pkt_time;
13559 
13560 	scmd = &spkt->satapkt_cmd;
13561 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13562 
13563 	/*
13564 	 * Allocate buffer for SMART READ LOG EXTENDED command
13565 	 */
13566 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13567 	    sizeof (struct read_log_ext_directory));
13568 	if (scmd->satacmd_bp == NULL) {
13569 		sata_pkt_free(spx);
13570 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13571 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13572 		    "sata_read_log_ext_directory: "
13573 		    "cannot allocate buffer"));
13574 		return (-1);
13575 	}
13576 
13577 	/* Build READ LOG EXT w/ log directory cmd in the  sata_pkt */
13578 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
13579 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of directory */
13580 	scmd->satacmd_sec_count_msb = 0;	/* One sector of directory */
13581 	scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY;
13582 	scmd->satacmd_lba_low_msb = 0;
13583 	scmd->satacmd_lba_mid_lsb = 0;
13584 	scmd->satacmd_lba_mid_msb = 0;
13585 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13586 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
13587 
13588 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13589 	    sdinfo->satadrv_addr.cport)));
13590 
13591 	/* Send pkt to SATA HBA driver */
13592 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13593 	    SATA_TRAN_ACCEPTED ||
13594 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13595 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13596 		    sdinfo->satadrv_addr.cport)));
13597 		/*
13598 		 * Whoops, no SMART selftest log info available
13599 		 */
13600 		rval = -1;
13601 		goto fail;
13602 	} else {
13603 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13604 		    sdinfo->satadrv_addr.cport)));
13605 		if (spx->txlt_buf_dma_handle != NULL) {
13606 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13607 			    DDI_DMA_SYNC_FORKERNEL);
13608 			ASSERT(rval == DDI_SUCCESS);
13609 		}
13610 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir,
13611 		    sizeof (struct read_log_ext_directory));
13612 		rval = 0;
13613 	}
13614 
13615 fail:
13616 	/* Free allocated resources */
13617 	sata_free_local_buffer(spx);
13618 	sata_pkt_free(spx);
13619 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13620 
13621 	return (rval);
13622 }
13623 
13624 /*
13625  * Set up error retrieval sata command for NCQ command error data
13626  * recovery.
13627  *
13628  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
13629  * returns SATA_FAILURE otherwise.
13630  */
13631 static int
13632 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
13633 {
13634 #ifndef __lock_lint
13635 	_NOTE(ARGUNUSED(sdinfo))
13636 #endif
13637 
13638 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
13639 	sata_cmd_t *scmd;
13640 	struct buf *bp;
13641 
13642 	/* Operation modes are up to the caller */
13643 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13644 
13645 	/* Synchronous mode, no callback - may be changed by the caller */
13646 	spkt->satapkt_comp = NULL;
13647 	spkt->satapkt_time = sata_default_pkt_time;
13648 
13649 	scmd = &spkt->satapkt_cmd;
13650 	bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t));
13651 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
13652 
13653 	/*
13654 	 * Allocate dma_able buffer error data.
13655 	 * Buffer allocation will take care of buffer alignment and other DMA
13656 	 * attributes.
13657 	 */
13658 	bp = sata_alloc_local_buffer(spx,
13659 	    sizeof (struct sata_ncq_error_recovery_page));
13660 	if (bp == NULL)
13661 		return (SATA_FAILURE);
13662 
13663 	bp_mapin(bp); /* make data buffer accessible */
13664 	scmd->satacmd_bp = bp;
13665 
13666 	/*
13667 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
13668 	 * before accessing it. Handle is in usual place in translate struct.
13669 	 */
13670 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
13671 
13672 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
13673 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
13674 
13675 	return (SATA_SUCCESS);
13676 }
13677 
13678 /*
13679  * sata_xlate_errors() is used to translate (S)ATA error
13680  * information to SCSI information returned in the SCSI
13681  * packet.
13682  */
13683 static void
13684 sata_xlate_errors(sata_pkt_txlate_t *spx)
13685 {
13686 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
13687 	struct scsi_extended_sense *sense;
13688 
13689 	scsipkt->pkt_reason = CMD_INCOMPLETE;
13690 	*scsipkt->pkt_scbp = STATUS_CHECK;
13691 	sense = sata_arq_sense(spx);
13692 
13693 	switch (spx->txlt_sata_pkt->satapkt_reason) {
13694 	case SATA_PKT_PORT_ERROR:
13695 		/*
13696 		 * We have no device data. Assume no data transfered.
13697 		 */
13698 		sense->es_key = KEY_HARDWARE_ERROR;
13699 		break;
13700 
13701 	case SATA_PKT_DEV_ERROR:
13702 		if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
13703 		    SATA_STATUS_ERR) {
13704 			/*
13705 			 * determine dev error reason from error
13706 			 * reg content
13707 			 */
13708 			sata_decode_device_error(spx, sense);
13709 			break;
13710 		}
13711 		/* No extended sense key - no info available */
13712 		break;
13713 
13714 	case SATA_PKT_TIMEOUT:
13715 		scsipkt->pkt_reason = CMD_TIMEOUT;
13716 		scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET;
13717 		/* No extended sense key */
13718 		break;
13719 
13720 	case SATA_PKT_ABORTED:
13721 		scsipkt->pkt_reason = CMD_ABORTED;
13722 		scsipkt->pkt_statistics |= STAT_ABORTED;
13723 		/* No extended sense key */
13724 		break;
13725 
13726 	case SATA_PKT_RESET:
13727 		/*
13728 		 * pkt aborted either by an explicit reset request from
13729 		 * a host, or due to error recovery
13730 		 */
13731 		scsipkt->pkt_reason = CMD_RESET;
13732 		scsipkt->pkt_statistics |= STAT_DEV_RESET;
13733 		break;
13734 
13735 	default:
13736 		scsipkt->pkt_reason = CMD_TRAN_ERR;
13737 		break;
13738 	}
13739 }
13740 
13741 
13742 
13743 
13744 /*
13745  * Log sata message
13746  * dev pathname msg line preceeds the logged message.
13747  */
13748 
13749 static	void
13750 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...)
13751 {
13752 	char pathname[128];
13753 	dev_info_t *dip;
13754 	va_list ap;
13755 
13756 	mutex_enter(&sata_log_mutex);
13757 
13758 	va_start(ap, fmt);
13759 	(void) vsprintf(sata_log_buf, fmt, ap);
13760 	va_end(ap);
13761 
13762 	if (sata_hba_inst != NULL) {
13763 		dip = SATA_DIP(sata_hba_inst);
13764 		(void) ddi_pathname(dip, pathname);
13765 	} else {
13766 		pathname[0] = 0;
13767 	}
13768 	if (level == CE_CONT) {
13769 		if (sata_debug_flags == 0)
13770 			cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf);
13771 		else
13772 			cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf);
13773 	} else {
13774 		if (level != CE_NOTE) {
13775 			cmn_err(level, "%s:\n %s", pathname, sata_log_buf);
13776 		} else if (sata_msg) {
13777 			cmn_err(level, "%s:\n %s", pathname,
13778 			    sata_log_buf);
13779 		}
13780 	}
13781 
13782 	mutex_exit(&sata_log_mutex);
13783 }
13784 
13785 
13786 /* ******** Asynchronous HBA events handling & hotplugging support ******** */
13787 
13788 /*
13789  * Start or terminate the thread, depending on flag arg and current state
13790  */
13791 static void
13792 sata_event_thread_control(int startstop)
13793 {
13794 	static 	int sata_event_thread_terminating = 0;
13795 	static 	int sata_event_thread_starting = 0;
13796 	int i;
13797 
13798 	mutex_enter(&sata_event_mutex);
13799 
13800 	if (startstop == 0 && (sata_event_thread_starting == 1 ||
13801 	    sata_event_thread_terminating == 1)) {
13802 		mutex_exit(&sata_event_mutex);
13803 		return;
13804 	}
13805 	if (startstop == 1 && sata_event_thread_starting == 1) {
13806 		mutex_exit(&sata_event_mutex);
13807 		return;
13808 	}
13809 	if (startstop == 1 && sata_event_thread_terminating == 1) {
13810 		sata_event_thread_starting = 1;
13811 		/* wait til terminate operation completes */
13812 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
13813 		while (sata_event_thread_terminating == 1) {
13814 			if (i-- <= 0) {
13815 				sata_event_thread_starting = 0;
13816 				mutex_exit(&sata_event_mutex);
13817 #ifdef SATA_DEBUG
13818 				cmn_err(CE_WARN, "sata_event_thread_control: "
13819 				    "timeout waiting for thread to terminate");
13820 #endif
13821 				return;
13822 			}
13823 			mutex_exit(&sata_event_mutex);
13824 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
13825 			mutex_enter(&sata_event_mutex);
13826 		}
13827 	}
13828 	if (startstop == 1) {
13829 		if (sata_event_thread == NULL) {
13830 			sata_event_thread = thread_create(NULL, 0,
13831 			    (void (*)())sata_event_daemon,
13832 			    &sata_hba_list, 0, &p0, TS_RUN, minclsyspri);
13833 		}
13834 		sata_event_thread_starting = 0;
13835 		mutex_exit(&sata_event_mutex);
13836 		return;
13837 	}
13838 
13839 	/*
13840 	 * If we got here, thread may need to be terminated
13841 	 */
13842 	if (sata_event_thread != NULL) {
13843 		int i;
13844 		/* Signal event thread to go away */
13845 		sata_event_thread_terminating = 1;
13846 		sata_event_thread_terminate = 1;
13847 		cv_signal(&sata_event_cv);
13848 		/*
13849 		 * Wait til daemon terminates.
13850 		 */
13851 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
13852 		while (sata_event_thread_terminate == 1) {
13853 			mutex_exit(&sata_event_mutex);
13854 			if (i-- <= 0) {
13855 				/* Daemon did not go away !!! */
13856 #ifdef SATA_DEBUG
13857 				cmn_err(CE_WARN, "sata_event_thread_control: "
13858 				    "cannot terminate event daemon thread");
13859 #endif
13860 				mutex_enter(&sata_event_mutex);
13861 				break;
13862 			}
13863 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
13864 			mutex_enter(&sata_event_mutex);
13865 		}
13866 		sata_event_thread_terminating = 0;
13867 	}
13868 	ASSERT(sata_event_thread_terminating == 0);
13869 	ASSERT(sata_event_thread_starting == 0);
13870 	mutex_exit(&sata_event_mutex);
13871 }
13872 
13873 
13874 /*
13875  * SATA HBA event notification function.
13876  * Events reported by SATA HBA drivers per HBA instance relate to a change in
13877  * a port and/or device state or a controller itself.
13878  * Events for different addresses/addr types cannot be combined.
13879  * A warning message is generated for each event type.
13880  * Events are not processed by this function, so only the
13881  * event flag(s)is set for an affected entity and the event thread is
13882  * waken up. Event daemon thread processes all events.
13883  *
13884  * NOTE: Since more than one event may be reported at the same time, one
13885  * cannot determine a sequence of events when opposite event are reported, eg.
13886  * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing
13887  * is taking precedence over reported events, i.e. may cause ignoring some
13888  * events.
13889  */
13890 #define	SATA_EVENT_MAX_MSG_LENGTH	79
13891 
13892 void
13893 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event)
13894 {
13895 	sata_hba_inst_t *sata_hba_inst = NULL;
13896 	sata_address_t *saddr;
13897 	sata_drive_info_t *sdinfo;
13898 	sata_port_stats_t *pstats;
13899 	sata_cport_info_t *cportinfo;
13900 	sata_pmport_info_t *pmportinfo;
13901 	int cport, pmport;
13902 	char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1];
13903 	char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1];
13904 	char *lcp;
13905 	static char *err_msg_evnt_1 =
13906 	    "sata_hba_event_notify: invalid port event 0x%x ";
13907 	static char *err_msg_evnt_2 =
13908 	    "sata_hba_event_notify: invalid device event 0x%x ";
13909 	int linkevent;
13910 
13911 	/*
13912 	 * There is a possibility that an event will be generated on HBA
13913 	 * that has not completed attachment or is detaching. We still want
13914 	 * to process events until HBA is detached.
13915 	 */
13916 	mutex_enter(&sata_mutex);
13917 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13918 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13919 		if (SATA_DIP(sata_hba_inst) == dip)
13920 			if (sata_hba_inst->satahba_attached == 1)
13921 				break;
13922 	}
13923 	mutex_exit(&sata_mutex);
13924 	if (sata_hba_inst == NULL)
13925 		/* HBA not attached */
13926 		return;
13927 
13928 	ASSERT(sata_device != NULL);
13929 
13930 	/*
13931 	 * Validate address before - do not proceed with invalid address.
13932 	 */
13933 	saddr = &sata_device->satadev_addr;
13934 	if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst))
13935 		return;
13936 	if (saddr->qual == SATA_ADDR_PMPORT ||
13937 	    saddr->qual == SATA_ADDR_DPMPORT)
13938 		/* Port Multiplier not supported yet */
13939 		return;
13940 
13941 	cport = saddr->cport;
13942 	pmport = saddr->pmport;
13943 
13944 	buf1[0] = buf2[0] = '\0';
13945 
13946 	/*
13947 	 * If event relates to port or device, check port state.
13948 	 * Port has to be initialized, or we cannot accept an event.
13949 	 */
13950 	if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT |
13951 	    SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) != 0) {
13952 		if ((saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_DCPORT)) != 0) {
13953 			mutex_enter(&sata_hba_inst->satahba_mutex);
13954 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
13955 			mutex_exit(&sata_hba_inst->satahba_mutex);
13956 			if (cportinfo == NULL || cportinfo->cport_state == 0)
13957 				return;
13958 		} else {
13959 			mutex_enter(&sata_hba_inst->satahba_mutex);
13960 			pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
13961 			    cport, pmport);
13962 			mutex_exit(&sata_hba_inst->satahba_mutex);
13963 			if (pmportinfo == NULL || pmportinfo->pmport_state == 0)
13964 				return;
13965 		}
13966 	}
13967 
13968 	/*
13969 	 * Events refer to devices, ports and controllers - each has
13970 	 * unique address. Events for different addresses cannot be combined.
13971 	 */
13972 	if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) {
13973 
13974 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13975 
13976 		/* qualify this event(s) */
13977 		if ((event & SATA_EVNT_PORT_EVENTS) == 0) {
13978 			/* Invalid event for the device port */
13979 			(void) sprintf(buf2, err_msg_evnt_1,
13980 			    event & SATA_EVNT_PORT_EVENTS);
13981 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13982 			goto event_info;
13983 		}
13984 		if (saddr->qual == SATA_ADDR_CPORT) {
13985 			/* Controller's device port event */
13986 
13987 			(SATA_CPORT_INFO(sata_hba_inst, cport))->
13988 			    cport_event_flags |=
13989 			    event & SATA_EVNT_PORT_EVENTS;
13990 			pstats =
13991 			    &(SATA_CPORT_INFO(sata_hba_inst, cport))->
13992 			    cport_stats;
13993 		} else {
13994 			/* Port multiplier's device port event */
13995 			(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
13996 			    pmport_event_flags |=
13997 			    event & SATA_EVNT_PORT_EVENTS;
13998 			pstats =
13999 			    &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
14000 			    pmport_stats;
14001 		}
14002 
14003 		/*
14004 		 * Add to statistics and log the message. We have to do it
14005 		 * here rather than in the event daemon, because there may be
14006 		 * multiple events occuring before they are processed.
14007 		 */
14008 		linkevent = event &
14009 		    (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED);
14010 		if (linkevent) {
14011 			if (linkevent == (SATA_EVNT_LINK_LOST |
14012 			    SATA_EVNT_LINK_ESTABLISHED)) {
14013 				/* This is likely event combination */
14014 				(void) strlcat(buf1, "link lost/established, ",
14015 				    SATA_EVENT_MAX_MSG_LENGTH);
14016 
14017 				if (pstats->link_lost < 0xffffffffffffffffULL)
14018 					pstats->link_lost++;
14019 				if (pstats->link_established <
14020 				    0xffffffffffffffffULL)
14021 					pstats->link_established++;
14022 				linkevent = 0;
14023 			} else if (linkevent & SATA_EVNT_LINK_LOST) {
14024 				(void) strlcat(buf1, "link lost, ",
14025 				    SATA_EVENT_MAX_MSG_LENGTH);
14026 
14027 				if (pstats->link_lost < 0xffffffffffffffffULL)
14028 					pstats->link_lost++;
14029 			} else {
14030 				(void) strlcat(buf1, "link established, ",
14031 				    SATA_EVENT_MAX_MSG_LENGTH);
14032 				if (pstats->link_established <
14033 				    0xffffffffffffffffULL)
14034 					pstats->link_established++;
14035 			}
14036 		}
14037 		if (event & SATA_EVNT_DEVICE_ATTACHED) {
14038 			(void) strlcat(buf1, "device attached, ",
14039 			    SATA_EVENT_MAX_MSG_LENGTH);
14040 			if (pstats->device_attached < 0xffffffffffffffffULL)
14041 				pstats->device_attached++;
14042 		}
14043 		if (event & SATA_EVNT_DEVICE_DETACHED) {
14044 			(void) strlcat(buf1, "device detached, ",
14045 			    SATA_EVENT_MAX_MSG_LENGTH);
14046 			if (pstats->device_detached < 0xffffffffffffffffULL)
14047 				pstats->device_detached++;
14048 		}
14049 		if (event & SATA_EVNT_PWR_LEVEL_CHANGED) {
14050 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
14051 			    "port %d power level changed", cport);
14052 			if (pstats->port_pwr_changed < 0xffffffffffffffffULL)
14053 				pstats->port_pwr_changed++;
14054 		}
14055 
14056 		if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) {
14057 			/* There should be no other events for this address */
14058 			(void) sprintf(buf2, err_msg_evnt_1,
14059 			    event & ~SATA_EVNT_PORT_EVENTS);
14060 		}
14061 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
14062 
14063 	} else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) {
14064 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
14065 
14066 		/* qualify this event */
14067 		if ((event & SATA_EVNT_DEVICE_RESET) == 0) {
14068 			/* Invalid event for a device */
14069 			(void) sprintf(buf2, err_msg_evnt_2,
14070 			    event & SATA_EVNT_DEVICE_RESET);
14071 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
14072 			goto event_info;
14073 		}
14074 		/* drive event */
14075 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
14076 		if (sdinfo != NULL) {
14077 			if (event & SATA_EVNT_DEVICE_RESET) {
14078 				(void) strlcat(buf1, "device reset, ",
14079 				    SATA_EVENT_MAX_MSG_LENGTH);
14080 				if (sdinfo->satadrv_stats.drive_reset <
14081 				    0xffffffffffffffffULL)
14082 					sdinfo->satadrv_stats.drive_reset++;
14083 				sdinfo->satadrv_event_flags |=
14084 				    SATA_EVNT_DEVICE_RESET;
14085 			}
14086 		}
14087 		if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) {
14088 			/* Invalid event for a device */
14089 			(void) sprintf(buf2, err_msg_evnt_2,
14090 			    event & ~SATA_EVNT_DRIVE_EVENTS);
14091 		}
14092 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
14093 	} else {
14094 		if (saddr->qual != SATA_ADDR_NULL) {
14095 			/* Wrong address qualifier */
14096 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14097 			    "sata_hba_event_notify: invalid address 0x%x",
14098 			    *(uint32_t *)saddr));
14099 			return;
14100 		}
14101 		if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 ||
14102 		    (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) {
14103 			/* Invalid event for the controller */
14104 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14105 			    "sata_hba_event_notify: invalid event 0x%x for "
14106 			    "controller",
14107 			    event & SATA_EVNT_CONTROLLER_EVENTS));
14108 			return;
14109 		}
14110 		buf1[0] = '\0';
14111 		/* This may be a frequent and not interesting event */
14112 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
14113 		    "controller power level changed\n", NULL);
14114 
14115 		mutex_enter(&sata_hba_inst->satahba_mutex);
14116 		if (sata_hba_inst->satahba_stats.ctrl_pwr_change <
14117 		    0xffffffffffffffffULL)
14118 			sata_hba_inst->satahba_stats.ctrl_pwr_change++;
14119 
14120 		sata_hba_inst->satahba_event_flags |=
14121 		    SATA_EVNT_PWR_LEVEL_CHANGED;
14122 		mutex_exit(&sata_hba_inst->satahba_mutex);
14123 	}
14124 	/*
14125 	 * If we got here, there is something to do with this HBA
14126 	 * instance.
14127 	 */
14128 	mutex_enter(&sata_hba_inst->satahba_mutex);
14129 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14130 	mutex_exit(&sata_hba_inst->satahba_mutex);
14131 	mutex_enter(&sata_mutex);
14132 	sata_event_pending |= SATA_EVNT_MAIN;	/* global event indicator */
14133 	mutex_exit(&sata_mutex);
14134 
14135 	/* Tickle event thread */
14136 	mutex_enter(&sata_event_mutex);
14137 	if (sata_event_thread_active == 0)
14138 		cv_signal(&sata_event_cv);
14139 	mutex_exit(&sata_event_mutex);
14140 
14141 event_info:
14142 	if (buf1[0] != '\0') {
14143 		lcp = strrchr(buf1, ',');
14144 		if (lcp != NULL)
14145 			*lcp = '\0';
14146 	}
14147 	if (saddr->qual == SATA_ADDR_CPORT ||
14148 	    saddr->qual == SATA_ADDR_DCPORT) {
14149 		if (buf1[0] != '\0') {
14150 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
14151 			    cport, buf1);
14152 		}
14153 		if (buf2[0] != '\0') {
14154 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
14155 			    cport, buf2);
14156 		}
14157 	} else if (saddr->qual == SATA_ADDR_PMPORT ||
14158 	    saddr->qual == SATA_ADDR_DPMPORT) {
14159 		if (buf1[0] != '\0') {
14160 			sata_log(sata_hba_inst, CE_NOTE,
14161 			    "port %d pmport %d: %s\n", cport, pmport, buf1);
14162 		}
14163 		if (buf2[0] != '\0') {
14164 			sata_log(sata_hba_inst, CE_NOTE,
14165 			    "port %d pmport %d: %s\n", cport, pmport, buf2);
14166 		}
14167 	}
14168 }
14169 
14170 
14171 /*
14172  * Event processing thread.
14173  * Arg is a pointer to the sata_hba_list pointer.
14174  * It is not really needed, because sata_hba_list is global and static
14175  */
14176 static void
14177 sata_event_daemon(void *arg)
14178 {
14179 #ifndef __lock_lint
14180 	_NOTE(ARGUNUSED(arg))
14181 #endif
14182 	sata_hba_inst_t *sata_hba_inst;
14183 	clock_t lbolt;
14184 
14185 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
14186 	    "SATA event daemon started\n", NULL);
14187 loop:
14188 	/*
14189 	 * Process events here. Walk through all registered HBAs
14190 	 */
14191 	mutex_enter(&sata_mutex);
14192 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
14193 	    sata_hba_inst = sata_hba_inst->satahba_next) {
14194 		ASSERT(sata_hba_inst != NULL);
14195 		mutex_enter(&sata_hba_inst->satahba_mutex);
14196 		if (sata_hba_inst->satahba_attached == 0 ||
14197 		    (sata_hba_inst->satahba_event_flags &
14198 		    SATA_EVNT_SKIP) != 0) {
14199 			mutex_exit(&sata_hba_inst->satahba_mutex);
14200 			continue;
14201 		}
14202 		if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) {
14203 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP;
14204 			mutex_exit(&sata_hba_inst->satahba_mutex);
14205 			mutex_exit(&sata_mutex);
14206 			/* Got the controller with pending event */
14207 			sata_process_controller_events(sata_hba_inst);
14208 			/*
14209 			 * Since global mutex was released, there is a
14210 			 * possibility that HBA list has changed, so start
14211 			 * over from the top. Just processed controller
14212 			 * will be passed-over because of the SKIP flag.
14213 			 */
14214 			goto loop;
14215 		}
14216 		mutex_exit(&sata_hba_inst->satahba_mutex);
14217 	}
14218 	/* Clear SKIP flag in all controllers */
14219 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
14220 	    sata_hba_inst = sata_hba_inst->satahba_next) {
14221 		mutex_enter(&sata_hba_inst->satahba_mutex);
14222 		sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP;
14223 		mutex_exit(&sata_hba_inst->satahba_mutex);
14224 	}
14225 	mutex_exit(&sata_mutex);
14226 
14227 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
14228 	    "SATA EVENT DAEMON suspending itself", NULL);
14229 
14230 #ifdef SATA_DEBUG
14231 	if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) {
14232 		sata_log(sata_hba_inst, CE_WARN,
14233 		    "SATA EVENTS PROCESSING DISABLED\n");
14234 		thread_exit(); /* Daemon will not run again */
14235 	}
14236 #endif
14237 	mutex_enter(&sata_event_mutex);
14238 	sata_event_thread_active = 0;
14239 	mutex_exit(&sata_event_mutex);
14240 	/*
14241 	 * Go to sleep/suspend itself and wake up either because new event or
14242 	 * wait timeout. Exit if there is a termination request (driver
14243 	 * unload).
14244 	 */
14245 	do {
14246 		lbolt = ddi_get_lbolt();
14247 		lbolt += drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME);
14248 		mutex_enter(&sata_event_mutex);
14249 		(void) cv_timedwait(&sata_event_cv, &sata_event_mutex, lbolt);
14250 
14251 		if (sata_event_thread_active != 0) {
14252 			mutex_exit(&sata_event_mutex);
14253 			continue;
14254 		}
14255 
14256 		/* Check if it is time to go away */
14257 		if (sata_event_thread_terminate == 1) {
14258 			/*
14259 			 * It is up to the thread setting above flag to make
14260 			 * sure that this thread is not killed prematurely.
14261 			 */
14262 			sata_event_thread_terminate = 0;
14263 			sata_event_thread = NULL;
14264 			mutex_exit(&sata_event_mutex);
14265 			SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
14266 			    "SATA_EVENT_DAEMON_TERMINATING", NULL);
14267 			thread_exit();  { _NOTE(NOT_REACHED) }
14268 		}
14269 		mutex_exit(&sata_event_mutex);
14270 	} while (!(sata_event_pending & SATA_EVNT_MAIN));
14271 
14272 	mutex_enter(&sata_event_mutex);
14273 	sata_event_thread_active = 1;
14274 	mutex_exit(&sata_event_mutex);
14275 
14276 	mutex_enter(&sata_mutex);
14277 	sata_event_pending &= ~SATA_EVNT_MAIN;
14278 	mutex_exit(&sata_mutex);
14279 
14280 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
14281 	    "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL);
14282 
14283 	goto loop;
14284 }
14285 
14286 /*
14287  * Specific HBA instance event processing.
14288  *
14289  * NOTE: At the moment, device event processing is limited to hard disks
14290  * only.
14291  * cports only are supported - no pmports.
14292  */
14293 static void
14294 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst)
14295 {
14296 	int ncport;
14297 	uint32_t event_flags;
14298 	sata_address_t *saddr;
14299 	sata_cport_info_t *cportinfo;
14300 
14301 	SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst,
14302 	    "Processing controller %d event(s)",
14303 	    ddi_get_instance(SATA_DIP(sata_hba_inst)));
14304 
14305 	mutex_enter(&sata_hba_inst->satahba_mutex);
14306 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN;
14307 	event_flags = sata_hba_inst->satahba_event_flags;
14308 	mutex_exit(&sata_hba_inst->satahba_mutex);
14309 	/*
14310 	 * Process controller power change first
14311 	 * HERE
14312 	 */
14313 	if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED)
14314 		sata_process_cntrl_pwr_level_change(sata_hba_inst);
14315 
14316 	/*
14317 	 * Search through ports/devices to identify affected port/device.
14318 	 * We may have to process events for more than one port/device.
14319 	 */
14320 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
14321 		/*
14322 		 * Not all ports may be processed in attach by the time we
14323 		 * get an event. Check if port info is initialized.
14324 		 */
14325 		mutex_enter(&sata_hba_inst->satahba_mutex);
14326 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
14327 		mutex_exit(&sata_hba_inst->satahba_mutex);
14328 		if (cportinfo == NULL || cportinfo->cport_state == NULL)
14329 			continue;
14330 
14331 		/* We have initialized controller port info */
14332 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14333 		event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))->
14334 		    cport_event_flags;
14335 		/* Check if port was locked by IOCTL processing */
14336 		if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) {
14337 			/*
14338 			 * We ignore port events because port is busy
14339 			 * with AP control processing. Set again
14340 			 * controller and main event flag, so that
14341 			 * events may be processed by the next daemon
14342 			 * run.
14343 			 */
14344 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14345 			mutex_enter(&sata_hba_inst->satahba_mutex);
14346 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14347 			mutex_exit(&sata_hba_inst->satahba_mutex);
14348 			mutex_enter(&sata_mutex);
14349 			sata_event_pending |= SATA_EVNT_MAIN;
14350 			mutex_exit(&sata_mutex);
14351 			SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst,
14352 			    "Event processing postponed until "
14353 			    "AP control processing completes",
14354 			    NULL);
14355 			/* Check other ports */
14356 			continue;
14357 		} else {
14358 			/*
14359 			 * Set BSY flag so that AP control would not
14360 			 * interfere with events processing for
14361 			 * this port.
14362 			 */
14363 			(SATA_CPORT_INFO(sata_hba_inst, ncport))->
14364 			    cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY;
14365 		}
14366 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14367 
14368 		saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr;
14369 
14370 		if ((event_flags &
14371 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
14372 			/*
14373 			 * Got port event.
14374 			 * We need some hierarchy of event processing as they
14375 			 * are affecting each other:
14376 			 * 1. port failed
14377 			 * 2. device detached/attached
14378 			 * 3. link events - link events may trigger device
14379 			 *    detached or device attached events in some
14380 			 *    circumstances.
14381 			 * 4. port power level changed
14382 			 */
14383 			if (event_flags & SATA_EVNT_PORT_FAILED) {
14384 				sata_process_port_failed_event(sata_hba_inst,
14385 				    saddr);
14386 			}
14387 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
14388 				sata_process_device_detached(sata_hba_inst,
14389 				    saddr);
14390 			}
14391 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
14392 				sata_process_device_attached(sata_hba_inst,
14393 				    saddr);
14394 			}
14395 			if (event_flags &
14396 			    (SATA_EVNT_LINK_ESTABLISHED |
14397 			    SATA_EVNT_LINK_LOST)) {
14398 				sata_process_port_link_events(sata_hba_inst,
14399 				    saddr);
14400 			}
14401 			if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) {
14402 				sata_process_port_pwr_change(sata_hba_inst,
14403 				    saddr);
14404 			}
14405 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
14406 				sata_process_target_node_cleanup(
14407 				    sata_hba_inst, saddr);
14408 			}
14409 			if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) {
14410 				sata_process_device_autoonline(
14411 				    sata_hba_inst, saddr);
14412 			}
14413 		}
14414 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14415 		if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) !=
14416 		    SATA_DTYPE_NONE) &&
14417 		    (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) {
14418 			if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)->
14419 			    satadrv_event_flags &
14420 			    (SATA_EVNT_DEVICE_RESET |
14421 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
14422 				/* Have device event */
14423 				sata_process_device_reset(sata_hba_inst,
14424 				    saddr);
14425 			}
14426 		}
14427 		/* Release PORT_BUSY flag */
14428 		(SATA_CPORT_INFO(sata_hba_inst, ncport))->
14429 		    cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
14430 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14431 
14432 	} /* End of loop through the controller SATA ports */
14433 }
14434 
14435 /*
14436  * Process HBA power level change reported by HBA driver.
14437  * Not implemented at this time - event is ignored.
14438  */
14439 static void
14440 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst)
14441 {
14442 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14443 	    "Processing controller power level change", NULL);
14444 
14445 	/* Ignoring it for now */
14446 	mutex_enter(&sata_hba_inst->satahba_mutex);
14447 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
14448 	mutex_exit(&sata_hba_inst->satahba_mutex);
14449 }
14450 
14451 /*
14452  * Process port power level change reported by HBA driver.
14453  * Not implemented at this time - event is ignored.
14454  */
14455 static void
14456 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst,
14457     sata_address_t *saddr)
14458 {
14459 	sata_cport_info_t *cportinfo;
14460 
14461 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14462 	    "Processing port power level change", NULL);
14463 
14464 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14465 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14466 	/* Reset event flag */
14467 	cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
14468 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14469 }
14470 
14471 /*
14472  * Process port failure reported by HBA driver.
14473  * cports support only - no pmports.
14474  */
14475 static void
14476 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst,
14477     sata_address_t *saddr)
14478 {
14479 	sata_cport_info_t *cportinfo;
14480 
14481 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14482 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14483 	/* Reset event flag first */
14484 	cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED;
14485 	/* If the port is in SHUTDOWN or FAILED state, ignore this event. */
14486 	if ((cportinfo->cport_state &
14487 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) {
14488 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14489 		    cport_mutex);
14490 		return;
14491 	}
14492 	/* Fail the port */
14493 	cportinfo->cport_state = SATA_PSTATE_FAILED;
14494 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14495 	sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport);
14496 }
14497 
14498 /*
14499  * Device Reset Event processing.
14500  * The seqeunce is managed by 3 stage flags:
14501  * - reset event reported,
14502  * - reset event being processed,
14503  * - request to clear device reset state.
14504  *
14505  * NOTE: This function has to be entered with cport mutex held. It exits with
14506  * mutex held as well, but can release mutex during the processing.
14507  */
14508 static void
14509 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst,
14510     sata_address_t *saddr)
14511 {
14512 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
14513 	sata_drive_info_t *sdinfo;
14514 	sata_cport_info_t *cportinfo;
14515 	sata_device_t sata_device;
14516 	int rval_probe, rval_set;
14517 
14518 	/* We only care about host sata cport for now */
14519 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14520 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14521 	/*
14522 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
14523 	 * state, ignore reset event.
14524 	 */
14525 	if (((cportinfo->cport_state &
14526 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
14527 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
14528 		sdinfo->satadrv_event_flags &=
14529 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
14530 		return;
14531 	}
14532 
14533 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) &
14534 	    SATA_VALID_DEV_TYPE) == 0) {
14535 		/*
14536 		 * This should not happen - coding error.
14537 		 * But we can recover, so do not panic, just clean up
14538 		 * and if in debug mode, log the message.
14539 		 */
14540 #ifdef SATA_DEBUG
14541 		sata_log(sata_hba_inst, CE_WARN,
14542 		    "sata_process_device_reset: "
14543 		    "Invalid device type with sdinfo!", NULL);
14544 #endif
14545 		sdinfo->satadrv_event_flags = 0;
14546 		return;
14547 	}
14548 
14549 #ifdef SATA_DEBUG
14550 	if ((sdinfo->satadrv_event_flags &
14551 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
14552 		/* Nothing to do */
14553 		/* Something is weird - why we are processing dev reset? */
14554 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14555 		    "No device reset event!!!!", NULL);
14556 
14557 		return;
14558 	}
14559 	if ((sdinfo->satadrv_event_flags &
14560 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
14561 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
14562 		/* Something is weird - new device reset event */
14563 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14564 		    "Overlapping device reset events!", NULL);
14565 	}
14566 #endif
14567 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14568 	    "Processing port %d device reset", saddr->cport);
14569 
14570 	/* Clear event flag */
14571 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
14572 
14573 	/* It seems that we always need to check the port state first */
14574 	sata_device.satadev_rev = SATA_DEVICE_REV;
14575 	sata_device.satadev_addr = *saddr;
14576 	/*
14577 	 * We have to exit mutex, because the HBA probe port function may
14578 	 * block on its own mutex.
14579 	 */
14580 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14581 	rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14582 	    (SATA_DIP(sata_hba_inst), &sata_device);
14583 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14584 	sata_update_port_info(sata_hba_inst, &sata_device);
14585 	if (rval_probe != SATA_SUCCESS) {
14586 		/* Something went wrong? Fail the port */
14587 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14588 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14589 		if (sdinfo != NULL)
14590 			sdinfo->satadrv_event_flags = 0;
14591 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14592 		    cport_mutex);
14593 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14594 		    "SATA port %d probing failed",
14595 		    saddr->cport));
14596 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14597 		    saddr->cport)->cport_mutex);
14598 		return;
14599 	}
14600 	if ((sata_device.satadev_scr.sstatus  &
14601 	    SATA_PORT_DEVLINK_UP_MASK) !=
14602 	    SATA_PORT_DEVLINK_UP ||
14603 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
14604 		/*
14605 		 * No device to process, anymore. Some other event processing
14606 		 * would or have already performed port info cleanup.
14607 		 * To be safe (HBA may need it), request clearing device
14608 		 * reset condition.
14609 		 */
14610 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14611 		if (sdinfo != NULL) {
14612 			sdinfo->satadrv_event_flags &=
14613 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
14614 			sdinfo->satadrv_event_flags |=
14615 			    SATA_EVNT_CLEAR_DEVICE_RESET;
14616 		}
14617 		return;
14618 	}
14619 
14620 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14621 	if (sdinfo == NULL) {
14622 		return;
14623 	}
14624 	if ((sdinfo->satadrv_event_flags &
14625 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
14626 		/*
14627 		 * Start tracking time for device feature restoration and
14628 		 * identification. Save current time (lbolt value).
14629 		 */
14630 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
14631 	}
14632 	/* Mark device reset processing as active */
14633 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
14634 
14635 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
14636 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14637 
14638 	rval_set = sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1);
14639 
14640 	if (rval_set  != SATA_SUCCESS) {
14641 		/*
14642 		 * Restoring drive setting failed.
14643 		 * Probe the port first, to check if the port state has changed
14644 		 */
14645 		sata_device.satadev_rev = SATA_DEVICE_REV;
14646 		sata_device.satadev_addr = *saddr;
14647 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
14648 		/* probe port */
14649 		rval_probe = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14650 		    (SATA_DIP(sata_hba_inst), &sata_device);
14651 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14652 		    cport_mutex);
14653 		if (rval_probe == SATA_SUCCESS &&
14654 		    (sata_device.satadev_state &
14655 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
14656 		    (sata_device.satadev_scr.sstatus  &
14657 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
14658 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
14659 			/*
14660 			 * We may retry this a bit later - in-process reset
14661 			 * condition should be already set.
14662 			 * Track retry time for device identification.
14663 			 */
14664 			if ((cportinfo->cport_dev_type &
14665 			    SATA_VALID_DEV_TYPE) != 0 &&
14666 			    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL &&
14667 			    sdinfo->satadrv_reset_time != 0) {
14668 				clock_t cur_time = ddi_get_lbolt();
14669 				/*
14670 				 * If the retry time limit was not
14671 				 * exceeded, retry.
14672 				 */
14673 				if ((cur_time - sdinfo->satadrv_reset_time) <
14674 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
14675 					mutex_enter(
14676 					    &sata_hba_inst->satahba_mutex);
14677 					sata_hba_inst->satahba_event_flags |=
14678 					    SATA_EVNT_MAIN;
14679 					mutex_exit(
14680 					    &sata_hba_inst->satahba_mutex);
14681 					mutex_enter(&sata_mutex);
14682 					sata_event_pending |= SATA_EVNT_MAIN;
14683 					mutex_exit(&sata_mutex);
14684 					return;
14685 				}
14686 				if (rval_set == SATA_RETRY) {
14687 					/*
14688 					 * Setting drive features failed, but
14689 					 * the drive is still accessible,
14690 					 * so emit a warning message before
14691 					 * return.
14692 					 */
14693 					mutex_exit(&SATA_CPORT_INFO(
14694 					    sata_hba_inst,
14695 					    saddr->cport)->cport_mutex);
14696 					goto done;
14697 				}
14698 			}
14699 			/* Fail the drive */
14700 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
14701 
14702 			sata_log(sata_hba_inst, CE_WARN,
14703 			    "SATA device at port %d - device failed",
14704 			    saddr->cport);
14705 		}
14706 		/*
14707 		 * No point of retrying - device failed or some other event
14708 		 * processing or already did or will do port info cleanup.
14709 		 * To be safe (HBA may need it),
14710 		 * request clearing device reset condition.
14711 		 */
14712 		sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
14713 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
14714 		sdinfo->satadrv_reset_time = 0;
14715 		return;
14716 	}
14717 done:
14718 	/*
14719 	 * If setting of drive features failed, but the drive is still
14720 	 * accessible, emit a warning message.
14721 	 */
14722 	if (rval_set == SATA_RETRY) {
14723 		sata_log(sata_hba_inst, CE_WARN,
14724 		    "SATA device at port %d - desired setting could not be "
14725 		    "restored after reset. Device may not operate as expected.",
14726 		    saddr->cport);
14727 	}
14728 	/*
14729 	 * Raise the flag indicating that the next sata command could
14730 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
14731 	 * reset is reported.
14732 	 */
14733 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14734 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14735 		sdinfo->satadrv_reset_time = 0;
14736 		if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) {
14737 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14738 			sdinfo->satadrv_event_flags &=
14739 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
14740 			sdinfo->satadrv_event_flags |=
14741 			    SATA_EVNT_CLEAR_DEVICE_RESET;
14742 		}
14743 	}
14744 }
14745 
14746 
14747 /*
14748  * Port Link Events processing.
14749  * Every link established event may involve device reset (due to
14750  * COMRESET signal, equivalent of the hard reset) so arbitrarily
14751  * set device reset event for an attached device (if any).
14752  * If the port is in SHUTDOWN or FAILED state, ignore link events.
14753  *
14754  * The link established event processing varies, depending on the state
14755  * of the target node, HBA hotplugging capabilities, state of the port.
14756  * If the link is not active, the link established event is ignored.
14757  * If HBA cannot detect device attachment and there is no target node,
14758  * the link established event triggers device attach event processing.
14759  * Else, link established event triggers device reset event processing.
14760  *
14761  * The link lost event processing varies, depending on a HBA hotplugging
14762  * capability and the state of the port (link active or not active).
14763  * If the link is active, the lost link event is ignored.
14764  * If HBA cannot detect device removal, the lost link event triggers
14765  * device detached event processing after link lost timeout.
14766  * Else, the event is ignored.
14767  *
14768  * NOTE: Only cports are processed for now, i.e. no port multiplier ports
14769  */
14770 static void
14771 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst,
14772     sata_address_t *saddr)
14773 {
14774 	sata_device_t sata_device;
14775 	sata_cport_info_t *cportinfo;
14776 	sata_drive_info_t *sdinfo;
14777 	uint32_t event_flags;
14778 	int rval;
14779 
14780 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14781 	    "Processing port %d link event(s)", saddr->cport);
14782 
14783 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14784 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14785 	event_flags = cportinfo->cport_event_flags;
14786 
14787 	/* Reset event flags first */
14788 	cportinfo->cport_event_flags &=
14789 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
14790 
14791 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
14792 	if ((cportinfo->cport_state &
14793 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14794 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14795 		    cport_mutex);
14796 		return;
14797 	}
14798 
14799 	/*
14800 	 * For the sanity sake get current port state.
14801 	 * Set device address only. Other sata_device fields should be
14802 	 * set by HBA driver.
14803 	 */
14804 	sata_device.satadev_rev = SATA_DEVICE_REV;
14805 	sata_device.satadev_addr = *saddr;
14806 	/*
14807 	 * We have to exit mutex, because the HBA probe port function may
14808 	 * block on its own mutex.
14809 	 */
14810 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14811 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14812 	    (SATA_DIP(sata_hba_inst), &sata_device);
14813 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14814 	sata_update_port_info(sata_hba_inst, &sata_device);
14815 	if (rval != SATA_SUCCESS) {
14816 		/* Something went wrong? Fail the port */
14817 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14818 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14819 		    cport_mutex);
14820 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14821 		    "SATA port %d probing failed",
14822 		    saddr->cport));
14823 		/*
14824 		 * We may want to release device info structure, but
14825 		 * it is not necessary.
14826 		 */
14827 		return;
14828 	} else {
14829 		/* port probed successfully */
14830 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14831 	}
14832 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
14833 
14834 		if ((sata_device.satadev_scr.sstatus &
14835 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
14836 			/* Ignore event */
14837 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14838 			    "Ignoring port %d link established event - "
14839 			    "link down",
14840 			    saddr->cport);
14841 			goto linklost;
14842 		}
14843 
14844 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14845 		    "Processing port %d link established event",
14846 		    saddr->cport);
14847 
14848 		/*
14849 		 * For the sanity sake check if a device is attached - check
14850 		 * return state of a port probing.
14851 		 */
14852 		if (sata_device.satadev_type != SATA_DTYPE_NONE &&
14853 		    sata_device.satadev_type != SATA_DTYPE_PMULT) {
14854 			/*
14855 			 * HBA port probe indicated that there is a device
14856 			 * attached. Check if the framework had device info
14857 			 * structure attached for this device.
14858 			 */
14859 			if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
14860 				ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) !=
14861 				    NULL);
14862 
14863 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14864 				if ((sdinfo->satadrv_type &
14865 				    SATA_VALID_DEV_TYPE) != 0) {
14866 					/*
14867 					 * Dev info structure is present.
14868 					 * If dev_type is set to known type in
14869 					 * the framework's drive info struct
14870 					 * then the device existed before and
14871 					 * the link was probably lost
14872 					 * momentarily - in such case
14873 					 * we may want to check device
14874 					 * identity.
14875 					 * Identity check is not supported now.
14876 					 *
14877 					 * Link established event
14878 					 * triggers device reset event.
14879 					 */
14880 					(SATA_CPORTINFO_DRV_INFO(cportinfo))->
14881 					    satadrv_event_flags |=
14882 					    SATA_EVNT_DEVICE_RESET;
14883 				}
14884 			} else if (cportinfo->cport_dev_type ==
14885 			    SATA_DTYPE_NONE) {
14886 				/*
14887 				 * We got new device attached! If HBA does not
14888 				 * generate device attached events, trigger it
14889 				 * here.
14890 				 */
14891 				if (!(SATA_FEATURES(sata_hba_inst) &
14892 				    SATA_CTLF_HOTPLUG)) {
14893 					cportinfo->cport_event_flags |=
14894 					    SATA_EVNT_DEVICE_ATTACHED;
14895 				}
14896 			}
14897 			/* Reset link lost timeout */
14898 			cportinfo->cport_link_lost_time = 0;
14899 		}
14900 	}
14901 linklost:
14902 	if (event_flags & SATA_EVNT_LINK_LOST) {
14903 		if ((sata_device.satadev_scr.sstatus &
14904 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
14905 			/* Ignore event */
14906 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14907 			    "Ignoring port %d link lost event - link is up",
14908 			    saddr->cport);
14909 			goto done;
14910 		}
14911 #ifdef SATA_DEBUG
14912 		if (cportinfo->cport_link_lost_time == 0) {
14913 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14914 			    "Processing port %d link lost event",
14915 			    saddr->cport);
14916 		}
14917 #endif
14918 		/*
14919 		 * When HBA cannot generate device attached/detached events,
14920 		 * we need to track link lost time and eventually generate
14921 		 * device detach event.
14922 		 */
14923 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
14924 			/* We are tracking link lost time */
14925 			if (cportinfo->cport_link_lost_time == 0) {
14926 				/* save current time (lbolt value) */
14927 				cportinfo->cport_link_lost_time =
14928 				    ddi_get_lbolt();
14929 				/* just keep link lost event */
14930 				cportinfo->cport_event_flags |=
14931 				    SATA_EVNT_LINK_LOST;
14932 			} else {
14933 				clock_t cur_time = ddi_get_lbolt();
14934 				if ((cur_time -
14935 				    cportinfo->cport_link_lost_time) >=
14936 				    drv_usectohz(
14937 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
14938 					/* trigger device detach event */
14939 					cportinfo->cport_event_flags |=
14940 					    SATA_EVNT_DEVICE_DETACHED;
14941 					cportinfo->cport_link_lost_time = 0;
14942 					SATADBG1(SATA_DBG_EVENTS,
14943 					    sata_hba_inst,
14944 					    "Triggering port %d "
14945 					    "device detached event",
14946 					    saddr->cport);
14947 				} else {
14948 					/* keep link lost event */
14949 					cportinfo->cport_event_flags |=
14950 					    SATA_EVNT_LINK_LOST;
14951 				}
14952 			}
14953 		}
14954 		/*
14955 		 * We could change port state to disable/delay access to
14956 		 * the attached device until the link is recovered.
14957 		 */
14958 	}
14959 done:
14960 	event_flags = cportinfo->cport_event_flags;
14961 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14962 	if (event_flags != 0) {
14963 		mutex_enter(&sata_hba_inst->satahba_mutex);
14964 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14965 		mutex_exit(&sata_hba_inst->satahba_mutex);
14966 		mutex_enter(&sata_mutex);
14967 		sata_event_pending |= SATA_EVNT_MAIN;
14968 		mutex_exit(&sata_mutex);
14969 	}
14970 }
14971 
14972 /*
14973  * Device Detached Event processing.
14974  * Port is probed to find if a device is really gone. If so,
14975  * the device info structure is detached from the SATA port info structure
14976  * and released.
14977  * Port status is updated.
14978  *
14979  * NOTE: Process cports event only, no port multiplier ports.
14980  */
14981 static void
14982 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst,
14983     sata_address_t *saddr)
14984 {
14985 	sata_cport_info_t *cportinfo;
14986 	sata_drive_info_t *sdevinfo;
14987 	sata_device_t sata_device;
14988 	dev_info_t *tdip;
14989 	int rval;
14990 
14991 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14992 	    "Processing port %d device detached", saddr->cport);
14993 
14994 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14995 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14996 	/* Clear event flag */
14997 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
14998 
14999 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
15000 	if ((cportinfo->cport_state &
15001 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
15002 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15003 		    cport_mutex);
15004 		return;
15005 	}
15006 	/* For sanity, re-probe the port */
15007 	sata_device.satadev_rev = SATA_DEVICE_REV;
15008 	sata_device.satadev_addr = *saddr;
15009 
15010 	/*
15011 	 * We have to exit mutex, because the HBA probe port function may
15012 	 * block on its own mutex.
15013 	 */
15014 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15015 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
15016 	    (SATA_DIP(sata_hba_inst), &sata_device);
15017 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15018 	sata_update_port_info(sata_hba_inst, &sata_device);
15019 	if (rval != SATA_SUCCESS) {
15020 		/* Something went wrong? Fail the port */
15021 		cportinfo->cport_state = SATA_PSTATE_FAILED;
15022 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15023 		    cport_mutex);
15024 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15025 		    "SATA port %d probing failed",
15026 		    saddr->cport));
15027 		/*
15028 		 * We may want to release device info structure, but
15029 		 * it is not necessary.
15030 		 */
15031 		return;
15032 	} else {
15033 		/* port probed successfully */
15034 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
15035 	}
15036 	/*
15037 	 * Check if a device is still attached. For sanity, check also
15038 	 * link status - if no link, there is no device.
15039 	 */
15040 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
15041 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
15042 	    SATA_DTYPE_NONE) {
15043 		/*
15044 		 * Device is still attached - ignore detach event.
15045 		 */
15046 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15047 		    cport_mutex);
15048 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15049 		    "Ignoring detach - device still attached to port %d",
15050 		    sata_device.satadev_addr.cport);
15051 		return;
15052 	}
15053 	/*
15054 	 * We need to detach and release device info structure here
15055 	 */
15056 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
15057 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
15058 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
15059 		(void) kmem_free((void *)sdevinfo,
15060 		    sizeof (sata_drive_info_t));
15061 	}
15062 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
15063 	/*
15064 	 * Device cannot be reached anymore, even if the target node may be
15065 	 * still present.
15066 	 */
15067 
15068 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15069 	sata_log(sata_hba_inst, CE_WARN, "SATA device detached at port %d",
15070 	    sata_device.satadev_addr.cport);
15071 
15072 	/*
15073 	 * Try to offline a device and remove target node if it still exists
15074 	 */
15075 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
15076 	if (tdip != NULL) {
15077 		/*
15078 		 * Target node exists.  Unconfigure device then remove
15079 		 * the target node (one ndi operation).
15080 		 */
15081 		if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
15082 			/*
15083 			 * PROBLEM - no device, but target node remained
15084 			 * This happens when the file was open or node was
15085 			 * waiting for resources.
15086 			 */
15087 			SATA_LOG_D((sata_hba_inst, CE_WARN,
15088 			    "sata_process_device_detached: "
15089 			    "Failed to remove target node for "
15090 			    "detached SATA device."));
15091 			/*
15092 			 * Set target node state to DEVI_DEVICE_REMOVED.
15093 			 * But re-check first that the node still exists.
15094 			 */
15095 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
15096 			    saddr->cport);
15097 			if (tdip != NULL) {
15098 				sata_set_device_removed(tdip);
15099 				/*
15100 				 * Instruct event daemon to retry the
15101 				 * cleanup later.
15102 				 */
15103 				sata_set_target_node_cleanup(sata_hba_inst,
15104 				    &sata_device.satadev_addr);
15105 			}
15106 		}
15107 	}
15108 	/*
15109 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15110 	 * with the hint: SE_HINT_REMOVE
15111 	 */
15112 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
15113 }
15114 
15115 
15116 /*
15117  * Device Attached Event processing.
15118  * Port state is checked to verify that a device is really attached. If so,
15119  * the device info structure is created and attached to the SATA port info
15120  * structure.
15121  *
15122  * If attached device cannot be identified or set-up, the retry for the
15123  * attach processing is set-up. Subsequent daemon run would try again to
15124  * identify the device, until the time limit is reached
15125  * (SATA_DEV_IDENTIFY_TIMEOUT).
15126  *
15127  * This function cannot be called in interrupt context (it may sleep).
15128  *
15129  * NOTE: Process cports event only, no port multiplier ports.
15130  */
15131 static void
15132 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst,
15133     sata_address_t *saddr)
15134 {
15135 	sata_cport_info_t *cportinfo;
15136 	sata_drive_info_t *sdevinfo;
15137 	sata_device_t sata_device;
15138 	dev_info_t *tdip;
15139 	uint32_t event_flags;
15140 	int rval;
15141 
15142 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15143 	    "Processing port %d device attached", saddr->cport);
15144 
15145 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15146 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15147 
15148 	/* Clear attach event flag first */
15149 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
15150 
15151 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
15152 	if ((cportinfo->cport_state &
15153 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
15154 		cportinfo->cport_dev_attach_time = 0;
15155 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15156 		    cport_mutex);
15157 		return;
15158 	}
15159 
15160 	/*
15161 	 * If the sata_drive_info structure is found attached to the port info,
15162 	 * despite the fact the device was removed and now it is re-attached,
15163 	 * the old drive info structure was not removed.
15164 	 * Arbitrarily release device info structure.
15165 	 */
15166 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
15167 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
15168 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
15169 		(void) kmem_free((void *)sdevinfo,
15170 		    sizeof (sata_drive_info_t));
15171 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15172 		    "Arbitrarily detaching old device info.", NULL);
15173 	}
15174 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
15175 
15176 	/* For sanity, re-probe the port */
15177 	sata_device.satadev_rev = SATA_DEVICE_REV;
15178 	sata_device.satadev_addr = *saddr;
15179 
15180 	/*
15181 	 * We have to exit mutex, because the HBA probe port function may
15182 	 * block on its own mutex.
15183 	 */
15184 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15185 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
15186 	    (SATA_DIP(sata_hba_inst), &sata_device);
15187 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15188 	sata_update_port_info(sata_hba_inst, &sata_device);
15189 	if (rval != SATA_SUCCESS) {
15190 		/* Something went wrong? Fail the port */
15191 		cportinfo->cport_state = SATA_PSTATE_FAILED;
15192 		cportinfo->cport_dev_attach_time = 0;
15193 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15194 		    cport_mutex);
15195 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15196 		    "SATA port %d probing failed",
15197 		    saddr->cport));
15198 		return;
15199 	} else {
15200 		/* port probed successfully */
15201 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
15202 	}
15203 	/*
15204 	 * Check if a device is still attached. For sanity, check also
15205 	 * link status - if no link, there is no device.
15206 	 */
15207 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
15208 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
15209 	    SATA_DTYPE_NONE) {
15210 		/*
15211 		 * No device - ignore attach event.
15212 		 */
15213 		cportinfo->cport_dev_attach_time = 0;
15214 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15215 		    cport_mutex);
15216 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15217 		    "Ignoring attach - no device connected to port %d",
15218 		    sata_device.satadev_addr.cport);
15219 		return;
15220 	}
15221 
15222 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15223 	/*
15224 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15225 	 * with the hint: SE_HINT_INSERT
15226 	 */
15227 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
15228 
15229 	/*
15230 	 * Port reprobing will take care of the creation of the device
15231 	 * info structure and determination of the device type.
15232 	 */
15233 	sata_device.satadev_addr = *saddr;
15234 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
15235 	    SATA_DEV_IDENTIFY_NORETRY);
15236 
15237 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15238 	    cport_mutex);
15239 	if ((cportinfo->cport_state & SATA_STATE_READY) &&
15240 	    (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) {
15241 		/* Some device is attached to the port */
15242 		if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) {
15243 			/*
15244 			 * A device was not successfully attached.
15245 			 * Track retry time for device identification.
15246 			 */
15247 			if (cportinfo->cport_dev_attach_time != 0) {
15248 				clock_t cur_time = ddi_get_lbolt();
15249 				/*
15250 				 * If the retry time limit was not exceeded,
15251 				 * reinstate attach event.
15252 				 */
15253 				if ((cur_time -
15254 				    cportinfo->cport_dev_attach_time) <
15255 				    drv_usectohz(
15256 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
15257 					/* OK, restore attach event */
15258 					cportinfo->cport_event_flags |=
15259 					    SATA_EVNT_DEVICE_ATTACHED;
15260 				} else {
15261 					/* Timeout - cannot identify device */
15262 					cportinfo->cport_dev_attach_time = 0;
15263 					sata_log(sata_hba_inst,
15264 					    CE_WARN,
15265 					    "Could not identify SATA device "
15266 					    "at port %d",
15267 					    saddr->cport);
15268 				}
15269 			} else {
15270 				/*
15271 				 * Start tracking time for device
15272 				 * identification.
15273 				 * Save current time (lbolt value).
15274 				 */
15275 				cportinfo->cport_dev_attach_time =
15276 				    ddi_get_lbolt();
15277 				/* Restore attach event */
15278 				cportinfo->cport_event_flags |=
15279 				    SATA_EVNT_DEVICE_ATTACHED;
15280 			}
15281 		} else {
15282 			/*
15283 			 * If device was successfully attached, the subsequent
15284 			 * action depends on a state of the
15285 			 * sata_auto_online variable. If it is set to zero.
15286 			 * an explicit 'configure' command will be needed to
15287 			 * configure it. If its value is non-zero, we will
15288 			 * attempt to online (configure) the device.
15289 			 * First, log the message indicating that a device
15290 			 * was attached.
15291 			 */
15292 			cportinfo->cport_dev_attach_time = 0;
15293 			sata_log(sata_hba_inst, CE_WARN,
15294 			    "SATA device detected at port %d", saddr->cport);
15295 
15296 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
15297 				sata_drive_info_t new_sdinfo;
15298 
15299 				/* Log device info data */
15300 				new_sdinfo = *(SATA_CPORTINFO_DRV_INFO(
15301 				    cportinfo));
15302 				sata_show_drive_info(sata_hba_inst,
15303 				    &new_sdinfo);
15304 			}
15305 
15306 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15307 			    saddr->cport)->cport_mutex);
15308 
15309 			/*
15310 			 * Make sure that there is no target node for that
15311 			 * device. If so, release it. It should not happen,
15312 			 * unless we had problem removing the node when
15313 			 * device was detached.
15314 			 */
15315 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
15316 			    saddr->cport);
15317 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15318 			    saddr->cport)->cport_mutex);
15319 			if (tdip != NULL) {
15320 
15321 #ifdef SATA_DEBUG
15322 				if ((cportinfo->cport_event_flags &
15323 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
15324 					sata_log(sata_hba_inst, CE_WARN,
15325 					    "sata_process_device_attached: "
15326 					    "old device target node exists!");
15327 #endif
15328 				/*
15329 				 * target node exists - try to unconfigure
15330 				 * device and remove the node.
15331 				 */
15332 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15333 				    saddr->cport)->cport_mutex);
15334 				rval = ndi_devi_offline(tdip,
15335 				    NDI_DEVI_REMOVE);
15336 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15337 				    saddr->cport)->cport_mutex);
15338 
15339 				if (rval == NDI_SUCCESS) {
15340 					cportinfo->cport_event_flags &=
15341 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
15342 					cportinfo->cport_tgtnode_clean = B_TRUE;
15343 				} else {
15344 					/*
15345 					 * PROBLEM - the target node remained
15346 					 * and it belongs to a previously
15347 					 * attached device.
15348 					 * This happens when the file was open
15349 					 * or the node was waiting for
15350 					 * resources at the time the
15351 					 * associated device was removed.
15352 					 * Instruct event daemon to retry the
15353 					 * cleanup later.
15354 					 */
15355 					sata_log(sata_hba_inst,
15356 					    CE_WARN,
15357 					    "Application(s) accessing "
15358 					    "previously attached SATA "
15359 					    "device have to release "
15360 					    "it before newly inserted "
15361 					    "device can be made accessible.",
15362 					    saddr->cport);
15363 					cportinfo->cport_event_flags |=
15364 					    SATA_EVNT_TARGET_NODE_CLEANUP;
15365 					cportinfo->cport_tgtnode_clean =
15366 					    B_FALSE;
15367 				}
15368 			}
15369 			if (sata_auto_online != 0) {
15370 				cportinfo->cport_event_flags |=
15371 				    SATA_EVNT_AUTOONLINE_DEVICE;
15372 			}
15373 
15374 		}
15375 	} else {
15376 		cportinfo->cport_dev_attach_time = 0;
15377 	}
15378 
15379 	event_flags = cportinfo->cport_event_flags;
15380 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15381 	if (event_flags != 0) {
15382 		mutex_enter(&sata_hba_inst->satahba_mutex);
15383 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15384 		mutex_exit(&sata_hba_inst->satahba_mutex);
15385 		mutex_enter(&sata_mutex);
15386 		sata_event_pending |= SATA_EVNT_MAIN;
15387 		mutex_exit(&sata_mutex);
15388 	}
15389 }
15390 
15391 
15392 /*
15393  * Device Target Node Cleanup Event processing.
15394  * If the target node associated with a sata port device is in
15395  * DEVI_DEVICE_REMOVED state, an attempt is made to remove it.
15396  * If the target node cannot be removed, the event flag is left intact,
15397  * so that event daemon may re-run this function later.
15398  *
15399  * This function cannot be called in interrupt context (it may sleep).
15400  *
15401  * NOTE: Processes cport events only, not port multiplier ports.
15402  */
15403 static void
15404 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
15405     sata_address_t *saddr)
15406 {
15407 	sata_cport_info_t *cportinfo;
15408 	dev_info_t *tdip;
15409 
15410 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15411 	    "Processing port %d device target node cleanup", saddr->cport);
15412 
15413 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15414 
15415 	/*
15416 	 * Check if there is target node for that device and it is in the
15417 	 * DEVI_DEVICE_REMOVED state. If so, release it.
15418 	 */
15419 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
15420 	if (tdip != NULL) {
15421 		/*
15422 		 * target node exists - check if it is target node of
15423 		 * a removed device.
15424 		 */
15425 		if (sata_check_device_removed(tdip) == B_TRUE) {
15426 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15427 			    "sata_process_target_node_cleanup: "
15428 			    "old device target node exists!", NULL);
15429 			/*
15430 			 * Unconfigure and remove the target node
15431 			 */
15432 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) ==
15433 			    NDI_SUCCESS) {
15434 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15435 				    saddr->cport)->cport_mutex);
15436 				cportinfo->cport_event_flags &=
15437 				    ~SATA_EVNT_TARGET_NODE_CLEANUP;
15438 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15439 				    saddr->cport)->cport_mutex);
15440 				return;
15441 			}
15442 			/*
15443 			 * Event daemon will retry the cleanup later.
15444 			 */
15445 			mutex_enter(&sata_hba_inst->satahba_mutex);
15446 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15447 			mutex_exit(&sata_hba_inst->satahba_mutex);
15448 			mutex_enter(&sata_mutex);
15449 			sata_event_pending |= SATA_EVNT_MAIN;
15450 			mutex_exit(&sata_mutex);
15451 		}
15452 	} else {
15453 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15454 		    saddr->cport)->cport_mutex);
15455 		cportinfo->cport_event_flags &=
15456 		    ~SATA_EVNT_TARGET_NODE_CLEANUP;
15457 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15458 		    saddr->cport)->cport_mutex);
15459 	}
15460 }
15461 
15462 /*
15463  * Device AutoOnline Event processing.
15464  * If attached device is to be onlined, an attempt is made to online this
15465  * device, but only if there is no lingering (old) target node present.
15466  * If the device cannot be onlined, the event flag is left intact,
15467  * so that event daemon may re-run this function later.
15468  *
15469  * This function cannot be called in interrupt context (it may sleep).
15470  *
15471  * NOTE: Processes cport events only, not port multiplier ports.
15472  */
15473 static void
15474 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst,
15475     sata_address_t *saddr)
15476 {
15477 	sata_cport_info_t *cportinfo;
15478 	sata_drive_info_t *sdinfo;
15479 	sata_device_t sata_device;
15480 	dev_info_t *tdip;
15481 
15482 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15483 	    "Processing port %d attached device auto-onlining", saddr->cport);
15484 
15485 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15486 
15487 	/*
15488 	 * Check if device is present and recognized. If not, reset event.
15489 	 */
15490 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15491 	if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
15492 		/* Nothing to online */
15493 		cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
15494 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15495 		    saddr->cport)->cport_mutex);
15496 		return;
15497 	}
15498 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15499 
15500 	/*
15501 	 * Check if there is target node for this device and if it is in the
15502 	 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep
15503 	 * the event for later processing.
15504 	 */
15505 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
15506 	if (tdip != NULL) {
15507 		/*
15508 		 * target node exists - check if it is target node of
15509 		 * a removed device.
15510 		 */
15511 		if (sata_check_device_removed(tdip) == B_TRUE) {
15512 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15513 			    "sata_process_device_autoonline: "
15514 			    "old device target node exists!", NULL);
15515 			/*
15516 			 * Event daemon will retry device onlining later.
15517 			 */
15518 			mutex_enter(&sata_hba_inst->satahba_mutex);
15519 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15520 			mutex_exit(&sata_hba_inst->satahba_mutex);
15521 			mutex_enter(&sata_mutex);
15522 			sata_event_pending |= SATA_EVNT_MAIN;
15523 			mutex_exit(&sata_mutex);
15524 			return;
15525 		}
15526 		/*
15527 		 * If the target node is not in the 'removed" state, assume
15528 		 * that it belongs to this device. There is nothing more to do,
15529 		 * but reset the event.
15530 		 */
15531 	} else {
15532 
15533 		/*
15534 		 * Try to online the device
15535 		 * If there is any reset-related event, remove it. We are
15536 		 * configuring the device and no state restoring is needed.
15537 		 */
15538 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15539 		    saddr->cport)->cport_mutex);
15540 		sata_device.satadev_addr = *saddr;
15541 		if (saddr->qual == SATA_ADDR_CPORT)
15542 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
15543 		else
15544 			sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
15545 		sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
15546 		if (sdinfo != NULL) {
15547 			if (sdinfo->satadrv_event_flags &
15548 			    (SATA_EVNT_DEVICE_RESET |
15549 			    SATA_EVNT_INPROC_DEVICE_RESET))
15550 				sdinfo->satadrv_event_flags = 0;
15551 			sdinfo->satadrv_event_flags |=
15552 			    SATA_EVNT_CLEAR_DEVICE_RESET;
15553 
15554 			/* Need to create a new target node. */
15555 			cportinfo->cport_tgtnode_clean = B_TRUE;
15556 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15557 			    saddr->cport)->cport_mutex);
15558 			tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
15559 			    sata_hba_inst, &sata_device.satadev_addr);
15560 			if (tdip == NULL) {
15561 				/*
15562 				 * Configure (onlining) failed.
15563 				 * We will NOT retry
15564 				 */
15565 				SATA_LOG_D((sata_hba_inst, CE_WARN,
15566 				    "sata_process_device_autoonline: "
15567 				    "configuring SATA device at port %d failed",
15568 				    saddr->cport));
15569 			}
15570 		} else {
15571 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15572 			    saddr->cport)->cport_mutex);
15573 		}
15574 
15575 	}
15576 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15577 	cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
15578 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15579 	    saddr->cport)->cport_mutex);
15580 }
15581 
15582 
15583 static void
15584 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr,
15585     int hint)
15586 {
15587 	char ap[MAXPATHLEN];
15588 	nvlist_t *ev_attr_list = NULL;
15589 	int err;
15590 
15591 	/* Allocate and build sysevent attribute list */
15592 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP);
15593 	if (err != 0) {
15594 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15595 		    "sata_gen_sysevent: "
15596 		    "cannot allocate memory for sysevent attributes\n"));
15597 		return;
15598 	}
15599 	/* Add hint attribute */
15600 	err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint));
15601 	if (err != 0) {
15602 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15603 		    "sata_gen_sysevent: "
15604 		    "failed to add DR_HINT attr for sysevent"));
15605 		nvlist_free(ev_attr_list);
15606 		return;
15607 	}
15608 	/*
15609 	 * Add AP attribute.
15610 	 * Get controller pathname and convert it into AP pathname by adding
15611 	 * a target number.
15612 	 */
15613 	(void) snprintf(ap, MAXPATHLEN, "/devices");
15614 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap));
15615 	(void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d",
15616 	    SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual));
15617 
15618 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap);
15619 	if (err != 0) {
15620 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15621 		    "sata_gen_sysevent: "
15622 		    "failed to add DR_AP_ID attr for sysevent"));
15623 		nvlist_free(ev_attr_list);
15624 		return;
15625 	}
15626 
15627 	/* Generate/log sysevent */
15628 	err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR,
15629 	    ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP);
15630 	if (err != DDI_SUCCESS) {
15631 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15632 		    "sata_gen_sysevent: "
15633 		    "cannot log sysevent, err code %x\n", err));
15634 	}
15635 
15636 	nvlist_free(ev_attr_list);
15637 }
15638 
15639 
15640 
15641 
15642 /*
15643  * Set DEVI_DEVICE_REMOVED state in the SATA device target node.
15644  */
15645 static void
15646 sata_set_device_removed(dev_info_t *tdip)
15647 {
15648 	int circ;
15649 
15650 	ASSERT(tdip != NULL);
15651 
15652 	ndi_devi_enter(tdip, &circ);
15653 	mutex_enter(&DEVI(tdip)->devi_lock);
15654 	DEVI_SET_DEVICE_REMOVED(tdip);
15655 	mutex_exit(&DEVI(tdip)->devi_lock);
15656 	ndi_devi_exit(tdip, circ);
15657 }
15658 
15659 
15660 /*
15661  * Set internal event instructing event daemon to try
15662  * to perform the target node cleanup.
15663  */
15664 static void
15665 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
15666     sata_address_t *saddr)
15667 {
15668 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15669 	SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |=
15670 	    SATA_EVNT_TARGET_NODE_CLEANUP;
15671 	SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_tgtnode_clean =
15672 	    B_FALSE;
15673 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15674 	mutex_enter(&sata_hba_inst->satahba_mutex);
15675 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15676 	mutex_exit(&sata_hba_inst->satahba_mutex);
15677 	mutex_enter(&sata_mutex);
15678 	sata_event_pending |= SATA_EVNT_MAIN;
15679 	mutex_exit(&sata_mutex);
15680 }
15681 
15682 
15683 /*
15684  * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state,
15685  * i.e. check if the target node state indicates that it belongs to a removed
15686  * device.
15687  *
15688  * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state,
15689  * B_FALSE otherwise.
15690  *
15691  * NOTE: No port multiplier support.
15692  */
15693 static boolean_t
15694 sata_check_device_removed(dev_info_t *tdip)
15695 {
15696 	ASSERT(tdip != NULL);
15697 
15698 	if (DEVI_IS_DEVICE_REMOVED(tdip))
15699 		return (B_TRUE);
15700 	else
15701 		return (B_FALSE);
15702 }
15703 
15704 /* ************************ FAULT INJECTTION **************************** */
15705 
15706 #ifdef SATA_INJECT_FAULTS
15707 
15708 static	uint32_t sata_fault_count = 0;
15709 static	uint32_t sata_fault_suspend_count = 0;
15710 
15711 /*
15712  * Inject sata pkt fault
15713  * It modifies returned values of the sata packet.
15714  * It returns immediately if:
15715  * pkt fault injection is not enabled (via sata_inject_fault,
15716  * sata_inject_fault_count), or invalid fault is specified (sata_fault_type),
15717  * or pkt does not contain command to be faulted (set in sata_fault_cmd), or
15718  * pkt is not directed to specified fault controller/device
15719  * (sata_fault_ctrl_dev and sata_fault_device).
15720  * If fault controller is not specified, fault injection applies to all
15721  * controllers and devices.
15722  *
15723  * First argument is the pointer to the executed sata packet.
15724  * Second argument is a pointer to a value returned by the HBA tran_start
15725  * function.
15726  * Third argument specifies injected error. Injected sata packet faults
15727  * are the satapkt_reason values.
15728  * SATA_PKT_BUSY		-1	Not completed, busy
15729  * SATA_PKT_DEV_ERROR		1	Device reported error
15730  * SATA_PKT_QUEUE_FULL		2	Not accepted, queue full
15731  * SATA_PKT_PORT_ERROR		3	Not completed, port error
15732  * SATA_PKT_CMD_UNSUPPORTED	4	Cmd unsupported
15733  * SATA_PKT_ABORTED		5	Aborted by request
15734  * SATA_PKT_TIMEOUT		6	Operation timeut
15735  * SATA_PKT_RESET		7	Aborted by reset request
15736  *
15737  * Additional global variables affecting the execution:
15738  *
15739  * sata_inject_fault_count variable specifies number of times in row the
15740  * error is injected. Value of -1 specifies permanent fault, ie. every time
15741  * the fault injection point is reached, the fault is injected and a pause
15742  * between fault injection specified by sata_inject_fault_pause_count is
15743  * ignored). Fault injection routine decrements sata_inject_fault_count
15744  * (if greater than zero) until it reaches 0. No fault is injected when
15745  * sata_inject_fault_count is 0 (zero).
15746  *
15747  * sata_inject_fault_pause_count variable specifies number of times a fault
15748  * injection is bypassed (pause between fault injections).
15749  * If set to 0, a fault is injected only a number of times specified by
15750  * sata_inject_fault_count.
15751  *
15752  * The fault counts are static, so for periodic errors they have to be manually
15753  * reset to start repetition sequence from scratch.
15754  * If the original value returned by the HBA tran_start function is not
15755  * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error
15756  * is injected (to avoid masking real problems);
15757  *
15758  * NOTE: In its current incarnation, this function should be invoked only for
15759  * commands executed in SYNCHRONOUS mode.
15760  */
15761 
15762 
15763 static	void
15764 sata_inject_pkt_fault(sata_pkt_t *spkt, int *rval, int fault)
15765 {
15766 
15767 	if (sata_inject_fault != SATA_INJECT_PKT_FAULT)
15768 		return;
15769 
15770 	if (sata_inject_fault_count == 0)
15771 		return;
15772 
15773 	if (fault == 0)
15774 		return;
15775 
15776 	if (sata_fault_cmd != spkt->satapkt_cmd.satacmd_cmd_reg)
15777 		return;
15778 
15779 	if (sata_fault_ctrl != NULL) {
15780 		sata_pkt_txlate_t *spx =
15781 		    (sata_pkt_txlate_t *)spkt->satapkt_framework_private;
15782 
15783 		if (sata_fault_ctrl != NULL && sata_fault_ctrl !=
15784 		    spx->txlt_sata_hba_inst->satahba_dip)
15785 			return;
15786 
15787 		if (sata_fault_device.satadev_addr.cport !=
15788 		    spkt->satapkt_device.satadev_addr.cport ||
15789 		    sata_fault_device.satadev_addr.pmport !=
15790 		    spkt->satapkt_device.satadev_addr.pmport ||
15791 		    sata_fault_device.satadev_addr.qual !=
15792 		    spkt->satapkt_device.satadev_addr.qual)
15793 			return;
15794 	}
15795 
15796 	/* Modify pkt return parameters */
15797 	if (*rval != SATA_TRAN_ACCEPTED ||
15798 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
15799 		sata_fault_count = 0;
15800 		sata_fault_suspend_count = 0;
15801 		return;
15802 	}
15803 	if (sata_fault_count == 0 && sata_fault_suspend_count != 0) {
15804 		/* Pause in the injection */
15805 		sata_fault_suspend_count -= 1;
15806 		return;
15807 	}
15808 
15809 	if (sata_fault_count == 0 && sata_fault_suspend_count == 0) {
15810 		/*
15811 		 * Init inject fault cycle. If fault count is set to -1,
15812 		 * it is a permanent fault.
15813 		 */
15814 		if (sata_inject_fault_count != -1) {
15815 			sata_fault_count = sata_inject_fault_count;
15816 			sata_fault_suspend_count =
15817 			    sata_inject_fault_pause_count;
15818 			if (sata_fault_suspend_count == 0)
15819 				sata_inject_fault_count = 0;
15820 		}
15821 	}
15822 
15823 	if (sata_fault_count != 0)
15824 		sata_fault_count -= 1;
15825 
15826 	switch (fault) {
15827 	case SATA_PKT_BUSY:
15828 		*rval = SATA_TRAN_BUSY;
15829 		spkt->satapkt_reason = SATA_PKT_BUSY;
15830 		break;
15831 
15832 	case SATA_PKT_QUEUE_FULL:
15833 		*rval = SATA_TRAN_QUEUE_FULL;
15834 		spkt->satapkt_reason = SATA_PKT_QUEUE_FULL;
15835 		break;
15836 
15837 	case SATA_PKT_CMD_UNSUPPORTED:
15838 		*rval = SATA_TRAN_CMD_UNSUPPORTED;
15839 		spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED;
15840 		break;
15841 
15842 	case SATA_PKT_PORT_ERROR:
15843 		/* This is "rejected" command */
15844 		*rval = SATA_TRAN_PORT_ERROR;
15845 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
15846 		/* Additional error setup could be done here - port state */
15847 		break;
15848 
15849 	case SATA_PKT_DEV_ERROR:
15850 		spkt->satapkt_reason = SATA_PKT_DEV_ERROR;
15851 		/*
15852 		 * Additional error setup could be done here
15853 		 */
15854 		break;
15855 
15856 	case SATA_PKT_ABORTED:
15857 		spkt->satapkt_reason = SATA_PKT_ABORTED;
15858 		break;
15859 
15860 	case SATA_PKT_TIMEOUT:
15861 		spkt->satapkt_reason = SATA_PKT_TIMEOUT;
15862 		/* Additional error setup could be done here */
15863 		break;
15864 
15865 	case SATA_PKT_RESET:
15866 		spkt->satapkt_reason = SATA_PKT_RESET;
15867 		/*
15868 		 * Additional error setup could be done here - device reset
15869 		 */
15870 		break;
15871 
15872 	default:
15873 		break;
15874 	}
15875 }
15876 
15877 #endif
15878