xref: /titanic_41/usr/src/uts/common/io/sata/impl/sata.c (revision 391647d5ee9b25dc5307abb55f583388e08b2dd7)
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_fault_cmd = 0;
120 uint32_t	sata_inject_fault_type = 0;
121 uint32_t	sata_inject_fault_count = 0;
122 uint32_t	sata_inject_fault_pause_count = 0;
123 
124 static	void sata_inject_pkt_fault(sata_pkt_t *, uint8_t, int *, int);
125 
126 #endif
127 
128 #define	LEGACY_HWID_LEN	64	/* Model (40) + Serial (20) + pad */
129 
130 static char sata_rev_tag[] = {"1.37"};
131 
132 /*
133  * SATA cb_ops functions
134  */
135 static 	int sata_hba_open(dev_t *, int, int, cred_t *);
136 static 	int sata_hba_close(dev_t, int, int, cred_t *);
137 static 	int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *,	int *);
138 
139 /*
140  * SCSA required entry points
141  */
142 static	int sata_scsi_tgt_init(dev_info_t *, dev_info_t *,
143     scsi_hba_tran_t *, struct scsi_device *);
144 static	int sata_scsi_tgt_probe(struct scsi_device *,
145     int (*callback)(void));
146 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *,
147     scsi_hba_tran_t *, struct scsi_device *);
148 static 	int sata_scsi_start(struct scsi_address *, struct scsi_pkt *);
149 static 	int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *);
150 static 	int sata_scsi_reset(struct scsi_address *, int);
151 static 	int sata_scsi_getcap(struct scsi_address *, char *, int);
152 static 	int sata_scsi_setcap(struct scsi_address *, char *, int, int);
153 static 	struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *,
154     struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t),
155     caddr_t);
156 static 	void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *);
157 static 	void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *);
158 static 	void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *);
159 
160 /*
161  * SATA HBA interface functions are defined in sata_hba.h header file
162  */
163 
164 /* Event processing functions */
165 static	void sata_event_daemon(void *);
166 static	void sata_event_thread_control(int);
167 static	void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst);
168 static	void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *);
169 static	void sata_process_port_failed_event(sata_hba_inst_t *,
170     sata_address_t *);
171 static	void sata_process_port_link_events(sata_hba_inst_t *,
172     sata_address_t *);
173 static	void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *);
174 static	void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *);
175 static	void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *);
176 static	void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *);
177 static	void sata_process_target_node_cleanup(sata_hba_inst_t *,
178     sata_address_t *);
179 static	void sata_process_device_autoonline(sata_hba_inst_t *,
180     sata_address_t *saddr);
181 
182 /*
183  * Local translation functions
184  */
185 static	int sata_txlt_inquiry(sata_pkt_txlate_t *);
186 static	int sata_txlt_test_unit_ready(sata_pkt_txlate_t *);
187 static	int sata_txlt_start_stop_unit(sata_pkt_txlate_t *);
188 static	int sata_txlt_read_capacity(sata_pkt_txlate_t *);
189 static	int sata_txlt_request_sense(sata_pkt_txlate_t *);
190 static 	int sata_txlt_read(sata_pkt_txlate_t *);
191 static 	int sata_txlt_write(sata_pkt_txlate_t *);
192 static 	int sata_txlt_log_sense(sata_pkt_txlate_t *);
193 static 	int sata_txlt_log_select(sata_pkt_txlate_t *);
194 static 	int sata_txlt_mode_sense(sata_pkt_txlate_t *);
195 static 	int sata_txlt_mode_select(sata_pkt_txlate_t *);
196 static 	int sata_txlt_synchronize_cache(sata_pkt_txlate_t *);
197 static 	int sata_txlt_write_buffer(sata_pkt_txlate_t *);
198 static 	int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *);
199 
200 static 	int sata_hba_start(sata_pkt_txlate_t *, int *);
201 static	int sata_txlt_invalid_command(sata_pkt_txlate_t *);
202 static	int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *);
203 static 	void sata_txlt_rw_completion(sata_pkt_t *);
204 static 	void sata_txlt_nodata_cmd_completion(sata_pkt_t *);
205 static 	void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *);
206 static 	int sata_emul_rw_completion(sata_pkt_txlate_t *);
207 static 	struct scsi_extended_sense *sata_immediate_error_response(
208     sata_pkt_txlate_t *, int);
209 static	struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *);
210 
211 static 	int sata_txlt_atapi(sata_pkt_txlate_t *);
212 static 	void sata_txlt_atapi_completion(sata_pkt_t *);
213 
214 /*
215  * Local functions for ioctl
216  */
217 static	int32_t sata_get_port_num(sata_hba_inst_t *,  struct devctl_iocdata *);
218 static	void sata_cfgadm_state(sata_hba_inst_t *, int32_t,
219     devctl_ap_state_t *);
220 static	dev_info_t *sata_get_target_dip(dev_info_t *, int32_t);
221 static	dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *);
222 static	dev_info_t *sata_devt_to_devinfo(dev_t);
223 static	int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *);
224 static	int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *);
225 static	int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *);
226 static	int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *);
227 static	int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *);
228 static	int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *);
229 static	int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *);
230 static	int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *);
231 static	int sata_ioctl_reset_all(sata_hba_inst_t *);
232 static	int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *);
233 static	int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *,
234     sata_ioctl_data_t *, int mode);
235 static	int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *,
236     sata_ioctl_data_t *, int mode);
237 static	int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *,
238     sata_ioctl_data_t *, int mode);
239 static	int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *,
240     sata_ioctl_data_t *, int mode);
241 static	int sata_ioctl_get_serialnumber_info(sata_hba_inst_t *,
242     sata_device_t *, sata_ioctl_data_t *, int mode);
243 
244 /*
245  * Local functions
246  */
247 static 	void sata_remove_hba_instance(dev_info_t *);
248 static 	int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *);
249 static 	void sata_probe_ports(sata_hba_inst_t *);
250 static 	int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int);
251 static 	int sata_add_device(dev_info_t *, sata_hba_inst_t *, int cport,
252     int pmport);
253 static 	dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *,
254     sata_address_t *);
255 static 	int sata_validate_scsi_address(sata_hba_inst_t *,
256     struct scsi_address *, sata_device_t *);
257 static 	int sata_validate_sata_address(sata_hba_inst_t *, int, int, int);
258 static	sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t));
259 static	void sata_pkt_free(sata_pkt_txlate_t *);
260 static	int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t),
261     caddr_t, ddi_dma_attr_t *);
262 static	void sata_common_free_dma_rsrcs(sata_pkt_txlate_t *);
263 static	int sata_probe_device(sata_hba_inst_t *, sata_device_t *);
264 static	sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *,
265     sata_device_t *);
266 static 	int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *);
267 static	void sata_reidentify_device(sata_pkt_txlate_t *);
268 static	struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int);
269 static 	void sata_free_local_buffer(sata_pkt_txlate_t *);
270 static 	uint64_t sata_check_capacity(sata_drive_info_t *);
271 void 	sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *,
272     ddi_dma_attr_t *);
273 static 	int sata_fetch_device_identify_data(sata_hba_inst_t *,
274     sata_drive_info_t *);
275 static	void sata_update_port_info(sata_hba_inst_t *, sata_device_t *);
276 static	void sata_update_port_scr(sata_port_scr_t *, sata_device_t *);
277 static	int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *);
278 static	int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int);
279 static	int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int);
280 static	int sata_set_drive_features(sata_hba_inst_t *,
281     sata_drive_info_t *, int flag);
282 static	void sata_init_write_cache_mode(sata_drive_info_t *sdinfo);
283 static	int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *);
284 static	void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *,
285     uint8_t *);
286 static	int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *,
287     struct scsi_inquiry *);
288 static	int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *);
289 static	int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *);
290 static	int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *);
291 static	int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *);
292 static	int sata_mode_select_page_8(sata_pkt_txlate_t *,
293     struct mode_cache_scsi3 *, int, int *, int *, int *);
294 static	int sata_mode_select_page_1c(sata_pkt_txlate_t *,
295     struct mode_info_excpt_page *, int, int *, int *, int *);
296 static	int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *);
297 static	int sata_mode_select_page_30(sata_pkt_txlate_t *,
298     struct mode_acoustic_management *, int, int *, int *, int *);
299 
300 static	int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *);
301 static	int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *,
302     sata_hba_inst_t *);
303 static	int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *,
304     sata_hba_inst_t *);
305 static	int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *,
306     sata_hba_inst_t *);
307 static	void sata_save_drive_settings(sata_drive_info_t *);
308 static	void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *);
309 static	void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...);
310 static	int sata_fetch_smart_return_status(sata_hba_inst_t *,
311     sata_drive_info_t *);
312 static	int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *,
313     struct smart_data *);
314 static	int sata_smart_selftest_log(sata_hba_inst_t *,
315     sata_drive_info_t *,
316     struct smart_selftest_log *);
317 static	int sata_ext_smart_selftest_read_log(sata_hba_inst_t *,
318     sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t);
319 static	int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *,
320     uint8_t *, uint8_t, uint8_t);
321 static	int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *,
322     struct read_log_ext_directory *);
323 static	void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int);
324 static	void sata_xlate_errors(sata_pkt_txlate_t *);
325 static	void sata_decode_device_error(sata_pkt_txlate_t *,
326     struct scsi_extended_sense *);
327 static	void sata_set_device_removed(dev_info_t *);
328 static	boolean_t sata_check_device_removed(dev_info_t *);
329 static	void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *);
330 static	int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *,
331     sata_drive_info_t *);
332 static	int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *,
333     sata_drive_info_t *);
334 static	void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *);
335 static	void sata_fixed_sense_data_preset(struct scsi_extended_sense *);
336 static  void sata_target_devid_register(dev_info_t *, sata_drive_info_t *);
337 static  int sata_check_modser(char *, int);
338 
339 
340 
341 /*
342  * SATA Framework will ignore SATA HBA driver cb_ops structure and
343  * register following one with SCSA framework.
344  * Open & close are provided, so scsi framework will not use its own
345  */
346 static struct cb_ops sata_cb_ops = {
347 	sata_hba_open,			/* open */
348 	sata_hba_close,			/* close */
349 	nodev,				/* strategy */
350 	nodev,				/* print */
351 	nodev,				/* dump */
352 	nodev,				/* read */
353 	nodev,				/* write */
354 	sata_hba_ioctl,			/* ioctl */
355 	nodev,				/* devmap */
356 	nodev,				/* mmap */
357 	nodev,				/* segmap */
358 	nochpoll,			/* chpoll */
359 	ddi_prop_op,			/* cb_prop_op */
360 	0,				/* streamtab */
361 	D_NEW | D_MP,			/* cb_flag */
362 	CB_REV,				/* rev */
363 	nodev,				/* aread */
364 	nodev				/* awrite */
365 };
366 
367 
368 extern struct mod_ops mod_miscops;
369 extern uchar_t	scsi_cdb_size[];
370 
371 static struct modlmisc modlmisc = {
372 	&mod_miscops,			/* Type of module */
373 	"SATA Module"			/* module name */
374 };
375 
376 
377 static struct modlinkage modlinkage = {
378 	MODREV_1,
379 	(void *)&modlmisc,
380 	NULL
381 };
382 
383 /*
384  * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero,
385  * i.e. when scsi_pkt has not timeout specified.
386  */
387 static int sata_default_pkt_time = 60;	/* 60 seconds */
388 
389 /*
390  * Intermediate buffer device access attributes - they are required,
391  * but not necessarily used.
392  */
393 static ddi_device_acc_attr_t sata_acc_attr = {
394 	DDI_DEVICE_ATTR_V0,
395 	DDI_STRUCTURE_LE_ACC,
396 	DDI_STRICTORDER_ACC
397 };
398 
399 
400 /*
401  * Mutexes protecting structures in multithreaded operations.
402  * Because events are relatively rare, a single global mutex protecting
403  * data structures should be sufficient. To increase performance, add
404  * separate mutex per each sata port and use global mutex only to protect
405  * common data structures.
406  */
407 static	kmutex_t sata_mutex;		/* protects sata_hba_list */
408 static	kmutex_t sata_log_mutex;	/* protects log */
409 
410 static 	char sata_log_buf[256];
411 
412 /* Default write cache setting for SATA hard disks */
413 int	sata_write_cache = 1;		/* enabled */
414 
415 /* Default write cache setting for SATA ATAPI CD/DVD */
416 int 	sata_atapicdvd_write_cache = 1; /* enabled */
417 
418 /*
419  * Linked list of HBA instances
420  */
421 static 	sata_hba_inst_t *sata_hba_list = NULL;
422 static 	sata_hba_inst_t *sata_hba_list_tail = NULL;
423 /*
424  * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran
425  * structure and in sata soft state.
426  */
427 
428 /*
429  * Event daemon related variables
430  */
431 static 	kmutex_t sata_event_mutex;
432 static 	kcondvar_t sata_event_cv;
433 static 	kthread_t *sata_event_thread = NULL;
434 static 	int sata_event_thread_terminate = 0;
435 static 	int sata_event_pending = 0;
436 static 	int sata_event_thread_active = 0;
437 extern 	pri_t minclsyspri;
438 
439 /*
440  * NCQ error recovery command
441  */
442 static const sata_cmd_t sata_rle_cmd = {
443 	SATA_CMD_REV,
444 	NULL,
445 	{
446 		SATA_DIR_READ
447 	},
448 	ATA_ADDR_LBA48,
449 	0,
450 	0,
451 	0,
452 	0,
453 	0,
454 	1,
455 	READ_LOG_EXT_NCQ_ERROR_RECOVERY,
456 	0,
457 	0,
458 	0,
459 	SATAC_READ_LOG_EXT,
460 	0,
461 	0,
462 	0,
463 };
464 
465 /*
466  * ATAPI error recovery CDB
467  */
468 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = {
469 	SCMD_REQUEST_SENSE,
470 	0,			/* Only fixed RQ format is supported */
471 	0,
472 	0,
473 	SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */
474 	0
475 };
476 
477 
478 /* Warlock directives */
479 
480 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran))
481 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device))
482 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops))
483 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense))
484 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status))
485 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr))
486 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t))
487 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state))
488 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state))
489 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list))
490 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list))
491 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next))
492 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev))
493 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \
494     sata_hba_inst::satahba_scsi_tran))
495 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran))
496 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip))
497 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached))
498 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port))
499 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex,
500     sata_hba_inst::satahba_event_flags))
501 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
502     sata_cport_info::cport_devp))
503 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp))
504 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr))
505 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
506     sata_cport_info::cport_dev_type))
507 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type))
508 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
509     sata_cport_info::cport_state))
510 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state))
511 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
512     sata_pmport_info::pmport_state))
513 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state))
514 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type))
515 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive))
516 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port))
517 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports))
518 #ifdef SATA_DEBUG
519 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count))
520 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count))
521 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace))
522 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index))
523 #endif
524 
525 /* End of warlock directives */
526 
527 /* ************** loadable module configuration functions ************** */
528 
529 int
530 _init()
531 {
532 	int rval;
533 
534 	mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL);
535 	mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL);
536 	mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL);
537 	cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL);
538 	if ((rval = mod_install(&modlinkage)) != 0) {
539 #ifdef SATA_DEBUG
540 		cmn_err(CE_WARN, "sata: _init: mod_install failed\n");
541 #endif
542 		mutex_destroy(&sata_log_mutex);
543 		cv_destroy(&sata_event_cv);
544 		mutex_destroy(&sata_event_mutex);
545 		mutex_destroy(&sata_mutex);
546 	}
547 	return (rval);
548 }
549 
550 int
551 _fini()
552 {
553 	int rval;
554 
555 	if ((rval = mod_remove(&modlinkage)) != 0)
556 		return (rval);
557 
558 	mutex_destroy(&sata_log_mutex);
559 	cv_destroy(&sata_event_cv);
560 	mutex_destroy(&sata_event_mutex);
561 	mutex_destroy(&sata_mutex);
562 	return (rval);
563 }
564 
565 int
566 _info(struct modinfo *modinfop)
567 {
568 	return (mod_info(&modlinkage, modinfop));
569 }
570 
571 
572 
573 /* ********************* SATA HBA entry points ********************* */
574 
575 
576 /*
577  * Called by SATA HBA from _init().
578  * Registers HBA driver instance/sata framework pair with scsi framework, by
579  * calling scsi_hba_init().
580  *
581  * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used
582  * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver
583  * cb_ops pointer in SATA HBA driver dev_ops structure.
584  * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors.
585  *
586  * Return status of the scsi_hba_init() is returned to a calling SATA HBA
587  * driver.
588  */
589 int
590 sata_hba_init(struct modlinkage *modlp)
591 {
592 	int rval;
593 	struct dev_ops *hba_ops;
594 
595 	SATADBG1(SATA_DBG_HBA_IF, NULL,
596 	    "sata_hba_init: name %s \n",
597 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
598 	/*
599 	 * Fill-up cb_ops and dev_ops when necessary
600 	 */
601 	hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
602 	/*
603 	 * Provide pointer to SATA dev_ops
604 	 */
605 	hba_ops->devo_cb_ops = &sata_cb_ops;
606 
607 	/*
608 	 * Register SATA HBA with SCSI framework
609 	 */
610 	if ((rval = scsi_hba_init(modlp)) != 0) {
611 		SATADBG1(SATA_DBG_HBA_IF, NULL,
612 		    "sata_hba_init: scsi hba init failed\n", NULL);
613 		return (rval);
614 	}
615 
616 	return (0);
617 }
618 
619 
620 /* HBA attach stages */
621 #define	HBA_ATTACH_STAGE_SATA_HBA_INST	1
622 #define	HBA_ATTACH_STAGE_SCSI_ATTACHED	2
623 #define	HBA_ATTACH_STAGE_SETUP		4
624 #define	HBA_ATTACH_STAGE_LINKED		8
625 
626 
627 /*
628  *
629  * Called from SATA HBA driver's attach routine to attach an instance of
630  * the HBA.
631  *
632  * For DDI_ATTACH command:
633  * sata_hba_inst structure is allocated here and initialized with pointers to
634  * SATA framework implementation of required scsi tran functions.
635  * The scsi_tran's tran_hba_private field is used by SATA Framework to point
636  * to the soft structure (sata_hba_inst) allocated by SATA framework for
637  * SATA HBA instance related data.
638  * The scsi_tran's tran_hba_private field is used by SATA framework to
639  * store a pointer to per-HBA-instance of sata_hba_inst structure.
640  * The sata_hba_inst structure is cross-linked to scsi tran structure.
641  * Among other info, a pointer to sata_hba_tran structure is stored in
642  * sata_hba_inst. The sata_hba_inst structures for different HBA instances are
643  * linked together into the list, pointed to by sata_hba_list.
644  * On the first HBA instance attach the sata event thread is initialized.
645  * Attachment points are created for all SATA ports of the HBA being attached.
646  * All HBA instance's SATA ports are probed and type of plugged devices is
647  * determined. For each device of a supported type, a target node is created.
648  *
649  * DDI_SUCCESS is returned when attachment process is successful,
650  * DDI_FAILURE is returned otherwise.
651  *
652  * For DDI_RESUME command:
653  * Not implemented at this time (postponed until phase 2 of the development).
654  */
655 int
656 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran,
657     ddi_attach_cmd_t cmd)
658 {
659 	sata_hba_inst_t	*sata_hba_inst;
660 	scsi_hba_tran_t *scsi_tran = NULL;
661 	int hba_attach_state = 0;
662 	char taskq_name[MAXPATHLEN];
663 
664 	SATADBG3(SATA_DBG_HBA_IF, NULL,
665 	    "sata_hba_attach: node %s (%s%d)\n",
666 	    ddi_node_name(dip), ddi_driver_name(dip),
667 	    ddi_get_instance(dip));
668 
669 	if (cmd == DDI_RESUME) {
670 		/*
671 		 * Postponed until phase 2 of the development
672 		 */
673 		return (DDI_FAILURE);
674 	}
675 
676 	if (cmd != DDI_ATTACH) {
677 		return (DDI_FAILURE);
678 	}
679 
680 	/* cmd == DDI_ATTACH */
681 
682 	if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) {
683 		SATA_LOG_D((NULL, CE_WARN,
684 		    "sata_hba_attach: invalid sata_hba_tran"));
685 		return (DDI_FAILURE);
686 	}
687 	/*
688 	 * Allocate and initialize SCSI tran structure.
689 	 * SATA copy of tran_bus_config is provided to create port nodes.
690 	 */
691 	scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
692 	if (scsi_tran == NULL)
693 		return (DDI_FAILURE);
694 	/*
695 	 * Allocate soft structure for SATA HBA instance.
696 	 * There is a separate softstate for each HBA instance.
697 	 */
698 	sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP);
699 	ASSERT(sata_hba_inst != NULL); /* this should not fail */
700 	mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL);
701 	hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST;
702 
703 	/*
704 	 * scsi_trans's tran_hba_private is used by SATA Framework to point to
705 	 * soft structure allocated by SATA framework for
706 	 * SATA HBA instance related data.
707 	 */
708 	scsi_tran->tran_hba_private	= sata_hba_inst;
709 	scsi_tran->tran_tgt_private	= NULL;
710 
711 	scsi_tran->tran_tgt_init	= sata_scsi_tgt_init;
712 	scsi_tran->tran_tgt_probe	= sata_scsi_tgt_probe;
713 	scsi_tran->tran_tgt_free	= sata_scsi_tgt_free;
714 
715 	scsi_tran->tran_start		= sata_scsi_start;
716 	scsi_tran->tran_reset		= sata_scsi_reset;
717 	scsi_tran->tran_abort		= sata_scsi_abort;
718 	scsi_tran->tran_getcap		= sata_scsi_getcap;
719 	scsi_tran->tran_setcap		= sata_scsi_setcap;
720 	scsi_tran->tran_init_pkt	= sata_scsi_init_pkt;
721 	scsi_tran->tran_destroy_pkt	= sata_scsi_destroy_pkt;
722 
723 	scsi_tran->tran_dmafree		= sata_scsi_dmafree;
724 	scsi_tran->tran_sync_pkt	= sata_scsi_sync_pkt;
725 
726 	scsi_tran->tran_reset_notify	= NULL;
727 	scsi_tran->tran_get_bus_addr	= NULL;
728 	scsi_tran->tran_quiesce		= NULL;
729 	scsi_tran->tran_unquiesce	= NULL;
730 	scsi_tran->tran_bus_reset	= NULL;
731 
732 	if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr,
733 	    scsi_tran, 0) != DDI_SUCCESS) {
734 #ifdef SATA_DEBUG
735 		cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed",
736 		    ddi_driver_name(dip), ddi_get_instance(dip));
737 #endif
738 		goto fail;
739 	}
740 	hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED;
741 
742 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) {
743 		if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
744 		    "sata", 1) != DDI_PROP_SUCCESS) {
745 			SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: "
746 			    "failed to create hba sata prop"));
747 			goto fail;
748 		}
749 	}
750 
751 	/*
752 	 * Save pointers in hba instance soft state.
753 	 */
754 	sata_hba_inst->satahba_scsi_tran = scsi_tran;
755 	sata_hba_inst->satahba_tran = sata_tran;
756 	sata_hba_inst->satahba_dip = dip;
757 
758 	/*
759 	 * Create a task queue to handle emulated commands completion
760 	 * Use node name, dash, instance number as the queue name.
761 	 */
762 	taskq_name[0] = '\0';
763 	(void) strlcat(taskq_name, DEVI(dip)->devi_node_name,
764 	    sizeof (taskq_name));
765 	(void) snprintf(taskq_name + strlen(taskq_name),
766 	    sizeof (taskq_name) - strlen(taskq_name),
767 	    "-%d", DEVI(dip)->devi_instance);
768 	sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1,
769 	    minclsyspri, 1, sata_tran->sata_tran_hba_num_cports,
770 	    TASKQ_DYNAMIC);
771 
772 	hba_attach_state |= HBA_ATTACH_STAGE_SETUP;
773 
774 	/*
775 	 * Create events thread if not created yet.
776 	 */
777 	sata_event_thread_control(1);
778 
779 	/*
780 	 * Link this hba instance into the list.
781 	 */
782 	mutex_enter(&sata_mutex);
783 
784 	if (sata_hba_list == NULL) {
785 		/*
786 		 * The first instance of HBA is attached.
787 		 * Set current/active default maximum NCQ/TCQ queue depth for
788 		 * all SATA devices. It is done here and now, to eliminate the
789 		 * possibility of the dynamic, programatic modification of the
790 		 * queue depth via global (and public) sata_max_queue_depth
791 		 * variable (this would require special handling in HBA drivers)
792 		 */
793 		sata_current_max_qdepth = sata_max_queue_depth;
794 		if (sata_current_max_qdepth > 32)
795 			sata_current_max_qdepth = 32;
796 		else if (sata_current_max_qdepth < 1)
797 			sata_current_max_qdepth = 1;
798 	}
799 
800 	sata_hba_inst->satahba_next = NULL;
801 	sata_hba_inst->satahba_prev = sata_hba_list_tail;
802 	if (sata_hba_list == NULL) {
803 		sata_hba_list = sata_hba_inst;
804 	}
805 	if (sata_hba_list_tail != NULL) {
806 		sata_hba_list_tail->satahba_next = sata_hba_inst;
807 	}
808 	sata_hba_list_tail = sata_hba_inst;
809 	mutex_exit(&sata_mutex);
810 	hba_attach_state |= HBA_ATTACH_STAGE_LINKED;
811 
812 	/*
813 	 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl
814 	 * SATA HBA driver should not use its own open/close entry points.
815 	 *
816 	 * Make sure that instance number doesn't overflow
817 	 * when forming minor numbers.
818 	 */
819 	ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT));
820 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
821 	    INST2DEVCTL(ddi_get_instance(dip)),
822 	    DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) {
823 #ifdef SATA_DEBUG
824 		cmn_err(CE_WARN, "sata_hba_attach: "
825 		    "cannot create devctl minor node");
826 #endif
827 		goto fail;
828 	}
829 
830 
831 	/*
832 	 * Set-up kstats here, if necessary.
833 	 * (postponed until phase 2 of the development).
834 	 */
835 
836 
837 	/*
838 	 * Probe controller ports. This operation will describe a current
839 	 * controller/port/multipliers/device configuration and will create
840 	 * attachment points.
841 	 * We may end-up with just a controller with no devices attached.
842 	 * For the ports with a supported device attached, device target nodes
843 	 * are created and devices are initialized.
844 	 */
845 	sata_probe_ports(sata_hba_inst);
846 
847 	sata_hba_inst->satahba_attached = 1;
848 	return (DDI_SUCCESS);
849 
850 fail:
851 	if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) {
852 		(void) sata_remove_hba_instance(dip);
853 		if (sata_hba_list == NULL)
854 			sata_event_thread_control(0);
855 	}
856 
857 	if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) {
858 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
859 		taskq_destroy(sata_hba_inst->satahba_taskq);
860 	}
861 
862 	if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED)
863 		(void) scsi_hba_detach(dip);
864 
865 	if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) {
866 		mutex_destroy(&sata_hba_inst->satahba_mutex);
867 		kmem_free((void *)sata_hba_inst,
868 		    sizeof (struct sata_hba_inst));
869 		scsi_hba_tran_free(scsi_tran);
870 	}
871 
872 	sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed",
873 	    ddi_driver_name(dip), ddi_get_instance(dip));
874 
875 	return (DDI_FAILURE);
876 }
877 
878 
879 /*
880  * Called by SATA HBA from to detach an instance of the driver.
881  *
882  * For DDI_DETACH command:
883  * Free local structures allocated for SATA HBA instance during
884  * sata_hba_attach processing.
885  *
886  * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise.
887  *
888  * For DDI_SUSPEND command:
889  * Not implemented at this time (postponed until phase 2 of the development)
890  * Returnd DDI_SUCCESS.
891  *
892  * When the last HBA instance is detached, the event daemon is terminated.
893  *
894  * NOTE: cport support only, no port multiplier support.
895  */
896 int
897 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
898 {
899 	dev_info_t	*tdip;
900 	sata_hba_inst_t	*sata_hba_inst;
901 	scsi_hba_tran_t *scsi_hba_tran;
902 	sata_cport_info_t *cportinfo;
903 	sata_drive_info_t *sdinfo;
904 	int ncport;
905 
906 	SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n",
907 	    ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip));
908 
909 	switch (cmd) {
910 	case DDI_DETACH:
911 
912 		if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
913 			return (DDI_FAILURE);
914 
915 		sata_hba_inst = scsi_hba_tran->tran_hba_private;
916 		if (sata_hba_inst == NULL)
917 			return (DDI_FAILURE);
918 
919 		if (scsi_hba_detach(dip) == DDI_FAILURE) {
920 			sata_hba_inst->satahba_attached = 1;
921 			return (DDI_FAILURE);
922 		}
923 
924 		/*
925 		 * Free all target nodes - at this point
926 		 * devices should be at least offlined
927 		 * otherwise scsi_hba_detach() should not be called.
928 		 */
929 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
930 		    ncport++) {
931 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
932 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
933 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
934 				if (sdinfo != NULL) {
935 					tdip = sata_get_target_dip(dip,
936 					    ncport);
937 					if (tdip != NULL) {
938 						if (ndi_devi_offline(tdip,
939 						    NDI_DEVI_REMOVE) !=
940 						    NDI_SUCCESS) {
941 							SATA_LOG_D((
942 							    sata_hba_inst,
943 							    CE_WARN,
944 							    "sata_hba_detach: "
945 							    "Target node not "
946 							    "removed !"));
947 							return (DDI_FAILURE);
948 						}
949 					}
950 				}
951 			}
952 		}
953 		/*
954 		 * Disable sata event daemon processing for this HBA
955 		 */
956 		sata_hba_inst->satahba_attached = 0;
957 
958 		/*
959 		 * Remove event daemon thread, if it is last HBA instance.
960 		 */
961 
962 		mutex_enter(&sata_mutex);
963 		if (sata_hba_list->satahba_next == NULL) {
964 			mutex_exit(&sata_mutex);
965 			sata_event_thread_control(0);
966 			mutex_enter(&sata_mutex);
967 		}
968 		mutex_exit(&sata_mutex);
969 
970 		/* Remove this HBA instance from the HBA list */
971 		sata_remove_hba_instance(dip);
972 
973 		/*
974 		 * At this point there should be no target nodes attached.
975 		 * Detach and destroy device and port info structures.
976 		 */
977 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
978 		    ncport++) {
979 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
980 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
981 				sdinfo =
982 				    cportinfo->cport_devp.cport_sata_drive;
983 				if (sdinfo != NULL) {
984 					/* Release device structure */
985 					kmem_free(sdinfo,
986 					    sizeof (sata_drive_info_t));
987 				}
988 				/* Release cport info */
989 				mutex_destroy(&cportinfo->cport_mutex);
990 				kmem_free(cportinfo,
991 				    sizeof (sata_cport_info_t));
992 			}
993 		}
994 
995 		scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran);
996 
997 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
998 
999 		taskq_destroy(sata_hba_inst->satahba_taskq);
1000 
1001 		mutex_destroy(&sata_hba_inst->satahba_mutex);
1002 		kmem_free((void *)sata_hba_inst,
1003 		    sizeof (struct sata_hba_inst));
1004 
1005 		return (DDI_SUCCESS);
1006 
1007 	case DDI_SUSPEND:
1008 		/*
1009 		 * Postponed until phase 2
1010 		 */
1011 		return (DDI_FAILURE);
1012 
1013 	default:
1014 		return (DDI_FAILURE);
1015 	}
1016 }
1017 
1018 
1019 /*
1020  * Called by an HBA drive from _fini() routine.
1021  * Unregisters SATA HBA instance/SATA framework pair from the scsi framework.
1022  */
1023 void
1024 sata_hba_fini(struct modlinkage *modlp)
1025 {
1026 	SATADBG1(SATA_DBG_HBA_IF, NULL,
1027 	    "sata_hba_fini: name %s\n",
1028 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
1029 
1030 	scsi_hba_fini(modlp);
1031 }
1032 
1033 
1034 /*
1035  * Default open and close routine for sata_hba framework.
1036  *
1037  */
1038 /*
1039  * Open devctl node.
1040  *
1041  * Returns:
1042  * 0 if node was open successfully, error code otherwise.
1043  *
1044  *
1045  */
1046 
1047 static int
1048 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1049 {
1050 #ifndef __lock_lint
1051 	_NOTE(ARGUNUSED(credp))
1052 #endif
1053 	int rv = 0;
1054 	dev_info_t *dip;
1055 	scsi_hba_tran_t *scsi_hba_tran;
1056 	sata_hba_inst_t	*sata_hba_inst;
1057 
1058 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL);
1059 
1060 	if (otyp != OTYP_CHR)
1061 		return (EINVAL);
1062 
1063 	dip = sata_devt_to_devinfo(*devp);
1064 	if (dip == NULL)
1065 		return (ENXIO);
1066 
1067 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1068 		return (ENXIO);
1069 
1070 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1071 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1072 		return (ENXIO);
1073 
1074 	mutex_enter(&sata_mutex);
1075 	if (flags & FEXCL) {
1076 		if (sata_hba_inst->satahba_open_flag != 0) {
1077 			rv = EBUSY;
1078 		} else {
1079 			sata_hba_inst->satahba_open_flag =
1080 			    SATA_DEVCTL_EXOPENED;
1081 		}
1082 	} else {
1083 		if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) {
1084 			rv = EBUSY;
1085 		} else {
1086 			sata_hba_inst->satahba_open_flag =
1087 			    SATA_DEVCTL_SOPENED;
1088 		}
1089 	}
1090 	mutex_exit(&sata_mutex);
1091 
1092 	return (rv);
1093 }
1094 
1095 
1096 /*
1097  * Close devctl node.
1098  * Returns:
1099  * 0 if node was closed successfully, error code otherwise.
1100  *
1101  */
1102 
1103 static int
1104 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
1105 {
1106 #ifndef __lock_lint
1107 	_NOTE(ARGUNUSED(credp))
1108 	_NOTE(ARGUNUSED(flag))
1109 #endif
1110 	dev_info_t *dip;
1111 	scsi_hba_tran_t *scsi_hba_tran;
1112 	sata_hba_inst_t	*sata_hba_inst;
1113 
1114 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL);
1115 
1116 	if (otyp != OTYP_CHR)
1117 		return (EINVAL);
1118 
1119 	dip = sata_devt_to_devinfo(dev);
1120 	if (dip == NULL)
1121 		return (ENXIO);
1122 
1123 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1124 		return (ENXIO);
1125 
1126 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1127 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1128 		return (ENXIO);
1129 
1130 	mutex_enter(&sata_mutex);
1131 	sata_hba_inst->satahba_open_flag = 0;
1132 	mutex_exit(&sata_mutex);
1133 	return (0);
1134 }
1135 
1136 
1137 
1138 /*
1139  * Standard IOCTL commands for SATA hotplugging.
1140  * Implemented DEVCTL_AP commands:
1141  * DEVCTL_AP_CONNECT
1142  * DEVCTL_AP_DISCONNECT
1143  * DEVCTL_AP_CONFIGURE
1144  * DEVCTL_UNCONFIGURE
1145  * DEVCTL_AP_CONTROL
1146  *
1147  * Commands passed to default ndi ioctl handler:
1148  * DEVCTL_DEVICE_GETSTATE
1149  * DEVCTL_DEVICE_ONLINE
1150  * DEVCTL_DEVICE_OFFLINE
1151  * DEVCTL_DEVICE_REMOVE
1152  * DEVCTL_DEVICE_INSERT
1153  * DEVCTL_BUS_GETSTATE
1154  *
1155  * All other cmds are passed to HBA if it provide ioctl handler, or failed
1156  * if not.
1157  *
1158  * Returns:
1159  * 0 if successful,
1160  * error code if operation failed.
1161  *
1162  * NOTE: Port Multiplier is not supported.
1163  *
1164  */
1165 
1166 static int
1167 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1168     int *rvalp)
1169 {
1170 #ifndef __lock_lint
1171 	_NOTE(ARGUNUSED(credp))
1172 	_NOTE(ARGUNUSED(rvalp))
1173 #endif
1174 	int rv = 0;
1175 	int32_t	comp_port = -1;
1176 	dev_info_t *dip;
1177 	devctl_ap_state_t ap_state;
1178 	struct devctl_iocdata *dcp = NULL;
1179 	scsi_hba_tran_t *scsi_hba_tran;
1180 	sata_hba_inst_t *sata_hba_inst;
1181 	sata_device_t sata_device;
1182 	sata_cport_info_t *cportinfo;
1183 	int cport, pmport, qual;
1184 	int rval = SATA_SUCCESS;
1185 
1186 	dip = sata_devt_to_devinfo(dev);
1187 	if (dip == NULL)
1188 		return (ENXIO);
1189 
1190 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1191 		return (ENXIO);
1192 
1193 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1194 	if (sata_hba_inst == NULL)
1195 		return (ENXIO);
1196 
1197 	if (sata_hba_inst->satahba_tran == NULL)
1198 		return (ENXIO);
1199 
1200 	switch (cmd) {
1201 
1202 	case DEVCTL_DEVICE_GETSTATE:
1203 	case DEVCTL_DEVICE_ONLINE:
1204 	case DEVCTL_DEVICE_OFFLINE:
1205 	case DEVCTL_DEVICE_REMOVE:
1206 	case DEVCTL_BUS_GETSTATE:
1207 		/*
1208 		 * There may be more cases that we want to pass to default
1209 		 * handler rather than fail them.
1210 		 */
1211 		return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
1212 	}
1213 
1214 	/* read devctl ioctl data */
1215 	if (cmd != DEVCTL_AP_CONTROL) {
1216 		if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1217 			return (EFAULT);
1218 
1219 		if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) ==
1220 		    -1) {
1221 			if (dcp)
1222 				ndi_dc_freehdl(dcp);
1223 			return (EINVAL);
1224 		}
1225 
1226 		cport = SCSI_TO_SATA_CPORT(comp_port);
1227 		pmport = SCSI_TO_SATA_PMPORT(comp_port);
1228 		/* Only cport is considered now, i.e. SATA_ADDR_CPORT */
1229 		qual = SATA_ADDR_CPORT;
1230 		if (sata_validate_sata_address(sata_hba_inst, cport, pmport,
1231 		    qual) != 0) {
1232 			ndi_dc_freehdl(dcp);
1233 			return (EINVAL);
1234 		}
1235 
1236 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1237 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1238 		    cport_mutex);
1239 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1240 			/*
1241 			 * Cannot process ioctl request now. Come back later.
1242 			 */
1243 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1244 			    cport_mutex);
1245 			ndi_dc_freehdl(dcp);
1246 			return (EBUSY);
1247 		}
1248 		/* Block event processing for this port */
1249 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1250 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1251 
1252 		sata_device.satadev_addr.cport = cport;
1253 		sata_device.satadev_addr.pmport = pmport;
1254 		sata_device.satadev_addr.qual = qual;
1255 		sata_device.satadev_rev = SATA_DEVICE_REV;
1256 	}
1257 
1258 	switch (cmd) {
1259 
1260 	case DEVCTL_AP_DISCONNECT:
1261 
1262 		/*
1263 		 * Normally, cfgadm sata plugin will try to offline
1264 		 * (unconfigure) device before this request. Nevertheless,
1265 		 * if a device is still configured, we need to
1266 		 * attempt to offline and unconfigure device first, and we will
1267 		 * deactivate the port regardless of the unconfigure
1268 		 * operation results.
1269 		 *
1270 		 */
1271 		rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device);
1272 
1273 		break;
1274 
1275 	case DEVCTL_AP_UNCONFIGURE:
1276 
1277 		/*
1278 		 * The unconfigure operation uses generic nexus operation to
1279 		 * offline a device. It leaves a target device node attached.
1280 		 * and obviously sata_drive_info attached as well, because
1281 		 * from the hardware point of view nothing has changed.
1282 		 */
1283 		rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device);
1284 		break;
1285 
1286 	case DEVCTL_AP_CONNECT:
1287 	{
1288 		/*
1289 		 * The sata cfgadm pluging will invoke this operation only if
1290 		 * port was found in the disconnect state (failed state
1291 		 * is also treated as the disconnected state).
1292 		 * If port activation is successful and a device is found
1293 		 * attached to the port, the initialization sequence is
1294 		 * executed to probe the port and attach
1295 		 * a device structure to a port structure. The device is not
1296 		 * set in configured state (system-wise) by this operation.
1297 		 */
1298 
1299 		rv = sata_ioctl_connect(sata_hba_inst, &sata_device);
1300 
1301 		break;
1302 	}
1303 
1304 	case DEVCTL_AP_CONFIGURE:
1305 	{
1306 		/*
1307 		 * A port may be in an active or shutdown state.
1308 		 * If port is in a failed state, operation is aborted.
1309 		 * If a port is in a shutdown state, sata_tran_port_activate()
1310 		 * is invoked prior to any other operation.
1311 		 *
1312 		 * Onlining the device involves creating a new target node.
1313 		 * If there is an old target node present (belonging to
1314 		 * previously removed device), the operation is aborted - the
1315 		 * old node has to be released and removed before configure
1316 		 * operation is attempted.
1317 		 */
1318 
1319 		rv = sata_ioctl_configure(sata_hba_inst, &sata_device);
1320 
1321 		break;
1322 	}
1323 
1324 	case DEVCTL_AP_GETSTATE:
1325 
1326 		sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state);
1327 
1328 		ap_state.ap_last_change = (time_t)-1;
1329 		ap_state.ap_error_code = 0;
1330 		ap_state.ap_in_transition = 0;
1331 
1332 		/* Copy the return AP-state information to the user space */
1333 		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
1334 			rv = EFAULT;
1335 		}
1336 		break;
1337 
1338 	case DEVCTL_AP_CONTROL:
1339 	{
1340 		/*
1341 		 * Generic devctl for hardware specific functionality
1342 		 */
1343 		sata_ioctl_data_t	ioc;
1344 
1345 		ASSERT(dcp == NULL);
1346 
1347 		/* Copy in user ioctl data first */
1348 #ifdef _MULTI_DATAMODEL
1349 		if (ddi_model_convert_from(mode & FMODELS) ==
1350 		    DDI_MODEL_ILP32) {
1351 
1352 			sata_ioctl_data_32_t	ioc32;
1353 
1354 			if (ddi_copyin((void *)arg, (void *)&ioc32,
1355 			    sizeof (ioc32), mode) != 0) {
1356 				rv = EFAULT;
1357 				break;
1358 			}
1359 			ioc.cmd 	= (uint_t)ioc32.cmd;
1360 			ioc.port	= (uint_t)ioc32.port;
1361 			ioc.get_size	= (uint_t)ioc32.get_size;
1362 			ioc.buf		= (caddr_t)(uintptr_t)ioc32.buf;
1363 			ioc.bufsiz	= (uint_t)ioc32.bufsiz;
1364 			ioc.misc_arg	= (uint_t)ioc32.misc_arg;
1365 		} else
1366 #endif /* _MULTI_DATAMODEL */
1367 		if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
1368 		    mode) != 0) {
1369 			return (EFAULT);
1370 		}
1371 
1372 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
1373 		    "sata_hba_ioctl: DEVCTL_AP_CONTROL "
1374 		    "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port);
1375 
1376 		/*
1377 		 * To avoid BE/LE and 32/64 issues, a get_size always returns
1378 		 * a 32-bit number.
1379 		 */
1380 		if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
1381 			return (EINVAL);
1382 		}
1383 		/* validate address */
1384 		cport = SCSI_TO_SATA_CPORT(ioc.port);
1385 		pmport = SCSI_TO_SATA_PMPORT(ioc.port);
1386 		qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port);
1387 
1388 		/* Override address qualifier - handle cport only for now */
1389 		qual = SATA_ADDR_CPORT;
1390 
1391 		if (sata_validate_sata_address(sata_hba_inst, cport,
1392 		    pmport, qual) != 0)
1393 			return (EINVAL);
1394 
1395 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1396 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1397 		    cport_mutex);
1398 		/* Is the port locked by event processing daemon ? */
1399 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1400 			/*
1401 			 * Cannot process ioctl request now. Come back later
1402 			 */
1403 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1404 			    cport_mutex);
1405 			return (EBUSY);
1406 		}
1407 		/* Block event processing for this port */
1408 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1409 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1410 
1411 
1412 		sata_device.satadev_addr.cport = cport;
1413 		sata_device.satadev_addr.pmport = pmport;
1414 		sata_device.satadev_addr.qual = qual;
1415 		sata_device.satadev_rev = SATA_DEVICE_REV;
1416 
1417 		switch (ioc.cmd) {
1418 
1419 		case SATA_CFGA_RESET_PORT:
1420 			/*
1421 			 * There is no protection for configured device.
1422 			 */
1423 			rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device);
1424 			break;
1425 
1426 		case SATA_CFGA_RESET_DEVICE:
1427 			/*
1428 			 * There is no protection for configured device.
1429 			 */
1430 			rv = sata_ioctl_reset_device(sata_hba_inst,
1431 			    &sata_device);
1432 			break;
1433 
1434 		case SATA_CFGA_RESET_ALL:
1435 			/*
1436 			 * There is no protection for configured devices.
1437 			 */
1438 			rv = sata_ioctl_reset_all(sata_hba_inst);
1439 			/*
1440 			 * We return here, because common return is for
1441 			 * a single port operation - we have already unlocked
1442 			 * all ports and no dc handle was allocated.
1443 			 */
1444 			return (rv);
1445 
1446 		case SATA_CFGA_PORT_DEACTIVATE:
1447 			/*
1448 			 * Arbitrarily unconfigure attached device, if any.
1449 			 * Even if the unconfigure fails, proceed with the
1450 			 * port deactivation.
1451 			 */
1452 			rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device);
1453 
1454 			break;
1455 
1456 		case SATA_CFGA_PORT_ACTIVATE:
1457 
1458 			rv = sata_ioctl_activate(sata_hba_inst, &sata_device);
1459 			break;
1460 
1461 		case SATA_CFGA_PORT_SELF_TEST:
1462 
1463 			rv = sata_ioctl_port_self_test(sata_hba_inst,
1464 			    &sata_device);
1465 			break;
1466 
1467 		case SATA_CFGA_GET_DEVICE_PATH:
1468 			if (qual == SATA_ADDR_CPORT)
1469 				sata_device.satadev_addr.qual =
1470 				    SATA_ADDR_DCPORT;
1471 			else
1472 				sata_device.satadev_addr.qual =
1473 				    SATA_ADDR_DPMPORT;
1474 			rv = sata_ioctl_get_device_path(sata_hba_inst,
1475 			    &sata_device, &ioc, mode);
1476 			break;
1477 
1478 		case SATA_CFGA_GET_AP_TYPE:
1479 
1480 			rv = sata_ioctl_get_ap_type(sata_hba_inst,
1481 			    &sata_device, &ioc, mode);
1482 			break;
1483 
1484 		case SATA_CFGA_GET_MODEL_INFO:
1485 
1486 			rv = sata_ioctl_get_model_info(sata_hba_inst,
1487 			    &sata_device, &ioc, mode);
1488 			break;
1489 
1490 		case SATA_CFGA_GET_REVFIRMWARE_INFO:
1491 
1492 			rv = sata_ioctl_get_revfirmware_info(sata_hba_inst,
1493 			    &sata_device, &ioc, mode);
1494 			break;
1495 
1496 		case SATA_CFGA_GET_SERIALNUMBER_INFO:
1497 
1498 			rv = sata_ioctl_get_serialnumber_info(sata_hba_inst,
1499 			    &sata_device, &ioc, mode);
1500 			break;
1501 
1502 		default:
1503 			rv = EINVAL;
1504 			break;
1505 
1506 		} /* End of DEVCTL_AP_CONTROL cmd switch */
1507 
1508 		break;
1509 	}
1510 
1511 	default:
1512 	{
1513 		/*
1514 		 * If we got here, we got an IOCTL that SATA HBA Framework
1515 		 * does not recognize. Pass ioctl to HBA driver, in case
1516 		 * it could process it.
1517 		 */
1518 		sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran;
1519 		dev_info_t	*mydip = SATA_DIP(sata_hba_inst);
1520 
1521 		SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1522 		    "IOCTL 0x%2x not supported in SATA framework, "
1523 		    "passthrough to HBA", cmd);
1524 
1525 		if (sata_tran->sata_tran_ioctl == NULL) {
1526 			rv = EINVAL;
1527 			break;
1528 		}
1529 		rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg);
1530 		if (rval != 0) {
1531 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1532 			    "IOCTL 0x%2x failed in HBA", cmd);
1533 			rv = rval;
1534 		}
1535 		break;
1536 	}
1537 
1538 	} /* End of main IOCTL switch */
1539 
1540 	if (dcp) {
1541 		ndi_dc_freehdl(dcp);
1542 	}
1543 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1544 	cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
1545 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1546 
1547 	return (rv);
1548 }
1549 
1550 
1551 /*
1552  * Create error retrieval sata packet
1553  *
1554  * A sata packet is allocated and set-up to contain specified error retrieval
1555  * command and appropriate dma-able data buffer.
1556  * No association with any scsi packet is made and no callback routine is
1557  * specified.
1558  *
1559  * Returns a pointer to sata packet upon successfull packet creation.
1560  * Returns NULL, if packet cannot be created.
1561  */
1562 sata_pkt_t *
1563 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device,
1564     int pkt_type)
1565 {
1566 	sata_hba_inst_t	*sata_hba_inst;
1567 	sata_pkt_txlate_t *spx;
1568 	sata_pkt_t *spkt;
1569 	sata_drive_info_t *sdinfo;
1570 
1571 	mutex_enter(&sata_mutex);
1572 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1573 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1574 		if (SATA_DIP(sata_hba_inst) == dip)
1575 			break;
1576 	}
1577 	mutex_exit(&sata_mutex);
1578 	ASSERT(sata_hba_inst != NULL);
1579 
1580 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
1581 	if (sdinfo == NULL) {
1582 		sata_log(sata_hba_inst, CE_WARN,
1583 		    "sata: error recovery request for non-attached device at "
1584 		    "cport %d", sata_device->satadev_addr.cport);
1585 		return (NULL);
1586 	}
1587 
1588 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1589 	spx->txlt_sata_hba_inst = sata_hba_inst;
1590 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
1591 	spkt = sata_pkt_alloc(spx, NULL);
1592 	if (spkt == NULL) {
1593 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1594 		return (NULL);
1595 	}
1596 	/* address is needed now */
1597 	spkt->satapkt_device.satadev_addr = sata_device->satadev_addr;
1598 
1599 	switch (pkt_type) {
1600 	case SATA_ERR_RETR_PKT_TYPE_NCQ:
1601 		if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS)
1602 			return (spkt);
1603 		break;
1604 
1605 	case SATA_ERR_RETR_PKT_TYPE_ATAPI:
1606 		if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS)
1607 			return (spkt);
1608 		break;
1609 
1610 	default:
1611 		break;
1612 	}
1613 
1614 	sata_pkt_free(spx);
1615 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1616 	return (NULL);
1617 
1618 }
1619 
1620 
1621 /*
1622  * Free error retrieval sata packet
1623  *
1624  * Free sata packet and any associated resources allocated previously by
1625  * sata_get_error_retrieval_pkt().
1626  *
1627  * Void return.
1628  */
1629 void
1630 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt)
1631 {
1632 	sata_pkt_txlate_t *spx =
1633 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1634 
1635 	ASSERT(sata_pkt != NULL);
1636 
1637 	sata_free_local_buffer(spx);
1638 	sata_pkt_free(spx);
1639 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1640 
1641 }
1642 
1643 /*
1644  * sata_name_child is for composing the name of the node
1645  * the format of the name is "target,0".
1646  */
1647 static int
1648 sata_name_child(dev_info_t *dip, char *name, int namelen)
1649 {
1650 	int target;
1651 
1652 	target = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1653 	    DDI_PROP_DONTPASS, "target", -1);
1654 	if (target == -1)
1655 		return (DDI_FAILURE);
1656 	(void) snprintf(name, namelen, "%x,0", target);
1657 	return (DDI_SUCCESS);
1658 }
1659 
1660 
1661 
1662 /* ****************** SCSA required entry points *********************** */
1663 
1664 /*
1665  * Implementation of scsi tran_tgt_init.
1666  * sata_scsi_tgt_init() initializes scsi_device structure
1667  *
1668  * If successful, DDI_SUCCESS is returned.
1669  * DDI_FAILURE is returned if addressed device does not exist
1670  */
1671 
1672 static int
1673 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1674     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1675 {
1676 #ifndef __lock_lint
1677 	_NOTE(ARGUNUSED(hba_dip))
1678 	_NOTE(ARGUNUSED(tgt_dip))
1679 #endif
1680 	sata_device_t		sata_device;
1681 	sata_drive_info_t	*sdinfo;
1682 	struct sata_id		*sid;
1683 	sata_hba_inst_t		*sata_hba_inst;
1684 	char			model[SATA_ID_MODEL_LEN + 1];
1685 	char			fw[SATA_ID_FW_LEN + 1];
1686 	char			*vid, *pid;
1687 	int			i;
1688 
1689 	/*
1690 	 * Fail tran_tgt_init for .conf stub node
1691 	 */
1692 	if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
1693 		(void) ndi_merge_node(tgt_dip, sata_name_child);
1694 		ddi_set_name_addr(tgt_dip, NULL);
1695 		return (DDI_FAILURE);
1696 	}
1697 
1698 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
1699 
1700 	/* Validate scsi device address */
1701 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
1702 	    &sata_device) != 0)
1703 		return (DDI_FAILURE);
1704 
1705 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1706 	    sata_device.satadev_addr.cport)));
1707 
1708 	/* sata_device now contains a valid sata address */
1709 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
1710 	if (sdinfo == NULL) {
1711 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1712 		    sata_device.satadev_addr.cport)));
1713 		return (DDI_FAILURE);
1714 	}
1715 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1716 	    sata_device.satadev_addr.cport)));
1717 
1718 	/*
1719 	 * Check if we need to create a legacy devid (i.e cmdk style) for
1720 	 * the target disks.
1721 	 *
1722 	 * HBA devinfo node will have the property "use-cmdk-devid-format"
1723 	 * if we need to create cmdk-style devid for all the disk devices
1724 	 * attached to this controller. This property may have been set
1725 	 * from HBA driver's .conf file or by the HBA driver in its
1726 	 * attach(9F) function.
1727 	 */
1728 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
1729 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
1730 	    "use-cmdk-devid-format", 0) == 1)) {
1731 		/* register a legacy devid for this target node */
1732 		sata_target_devid_register(tgt_dip, sdinfo);
1733 	}
1734 
1735 
1736 	/*
1737 	 * 'Identify Device Data' does not always fit in standard SCSI
1738 	 * INQUIRY data, so establish INQUIRY_* properties with full-form
1739 	 * of information.
1740 	 */
1741 	sid = &sdinfo->satadrv_id;
1742 #ifdef	_LITTLE_ENDIAN
1743 	swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
1744 	swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
1745 #else	/* _LITTLE_ENDIAN */
1746 	bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN);
1747 	bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN);
1748 #endif	/* _LITTLE_ENDIAN */
1749 	model[SATA_ID_MODEL_LEN] = 0;
1750 	fw[SATA_ID_FW_LEN] = 0;
1751 
1752 	/* split model into into vid/pid */
1753 	for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++)
1754 		if ((*pid == ' ') || (*pid == '\t'))
1755 			break;
1756 	if (i < SATA_ID_MODEL_LEN) {
1757 		vid = model;
1758 		*pid++ = 0;		/* terminate vid, establish pid */
1759 	} else {
1760 		vid = NULL;		/* vid will stay "ATA     " */
1761 		pid = model;		/* model is all pid */
1762 	}
1763 
1764 	if (vid)
1765 		(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_VENDOR_ID,
1766 		    vid, strlen(vid));
1767 	if (pid)
1768 		(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID,
1769 		    pid, strlen(pid));
1770 	(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_REVISION_ID,
1771 	    fw, strlen(fw));
1772 
1773 	return (DDI_SUCCESS);
1774 }
1775 
1776 /*
1777  * Implementation of scsi tran_tgt_probe.
1778  * Probe target, by calling default scsi routine scsi_hba_probe()
1779  */
1780 static int
1781 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void))
1782 {
1783 	sata_hba_inst_t *sata_hba_inst =
1784 	    (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private);
1785 	int rval;
1786 
1787 	rval = scsi_hba_probe(sd, callback);
1788 
1789 	if (rval == SCSIPROBE_EXISTS) {
1790 		/*
1791 		 * Set property "pm-capable" on the target device node, so that
1792 		 * the target driver will not try to fetch scsi cycle counters
1793 		 * before enabling device power-management.
1794 		 */
1795 		if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
1796 		    "pm-capable", 1)) != DDI_PROP_SUCCESS) {
1797 			sata_log(sata_hba_inst, CE_WARN,
1798 			    "SATA device at port %d: "
1799 			    "will not be power-managed ",
1800 			    SCSI_TO_SATA_CPORT(sd->sd_address.a_target));
1801 			SATA_LOG_D((sata_hba_inst, CE_WARN,
1802 			    "failure updating pm-capable property"));
1803 		}
1804 	}
1805 	return (rval);
1806 }
1807 
1808 /*
1809  * Implementation of scsi tran_tgt_free.
1810  * Release all resources allocated for scsi_device
1811  */
1812 static void
1813 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1814     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1815 {
1816 #ifndef __lock_lint
1817 	_NOTE(ARGUNUSED(hba_dip))
1818 #endif
1819 	sata_device_t		sata_device;
1820 	sata_drive_info_t	*sdinfo;
1821 	sata_hba_inst_t		*sata_hba_inst;
1822 	ddi_devid_t		devid;
1823 
1824 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
1825 
1826 	/* Validate scsi device address */
1827 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
1828 	    &sata_device) != 0)
1829 		return;
1830 
1831 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1832 	    sata_device.satadev_addr.cport)));
1833 
1834 	/* sata_device now should contain a valid sata address */
1835 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
1836 	if (sdinfo == NULL) {
1837 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1838 		    sata_device.satadev_addr.cport)));
1839 		return;
1840 	}
1841 	/*
1842 	 * We did not allocate any resources in sata_scsi_tgt_init()
1843 	 * other than few properties.
1844 	 * Free them.
1845 	 */
1846 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1847 	    sata_device.satadev_addr.cport)));
1848 	(void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable");
1849 
1850 	/*
1851 	 * If devid was previously created but not freed up from
1852 	 * sd(7D) driver (i.e during detach(9F)) then do it here.
1853 	 */
1854 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
1855 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
1856 	    "use-cmdk-devid-format", 0) == 1) &&
1857 	    (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) {
1858 		ddi_devid_unregister(tgt_dip);
1859 		ddi_devid_free(devid);
1860 	}
1861 }
1862 
1863 /*
1864  * Implementation of scsi tran_init_pkt
1865  * Upon successful return, scsi pkt buffer has DMA resources allocated.
1866  *
1867  * It seems that we should always allocate pkt, even if the address is
1868  * for non-existing device - just use some default for dma_attr.
1869  * The reason is that there is no way to communicate this to a caller here.
1870  * Subsequent call to sata_scsi_start may fail appropriately.
1871  * Simply returning NULL does not seem to discourage a target driver...
1872  *
1873  * Returns a pointer to initialized scsi_pkt, or NULL otherwise.
1874  */
1875 static struct scsi_pkt *
1876 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
1877     struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
1878     int (*callback)(caddr_t), caddr_t arg)
1879 {
1880 	sata_hba_inst_t *sata_hba_inst =
1881 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
1882 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
1883 	sata_device_t sata_device;
1884 	sata_drive_info_t *sdinfo;
1885 	sata_pkt_txlate_t *spx;
1886 	ddi_dma_attr_t cur_dma_attr;
1887 	int rval;
1888 	boolean_t new_pkt = TRUE;
1889 
1890 	ASSERT(ap->a_hba_tran->tran_hba_dip == dip);
1891 
1892 	/*
1893 	 * We need to translate the address, even if it could be
1894 	 * a bogus one, for a non-existing device
1895 	 */
1896 	sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
1897 	sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target);
1898 	sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
1899 	sata_device.satadev_rev = SATA_DEVICE_REV;
1900 
1901 	if (pkt == NULL) {
1902 		/*
1903 		 * Have to allocate a brand new scsi packet.
1904 		 * We need to operate with auto request sense enabled.
1905 		 */
1906 		pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen,
1907 		    MAX(statuslen, sizeof (struct scsi_arq_status)),
1908 		    tgtlen, sizeof (sata_pkt_txlate_t), callback, arg);
1909 
1910 		if (pkt == NULL)
1911 			return (NULL);
1912 
1913 		/* Fill scsi packet structure */
1914 		pkt->pkt_comp		= (void (*)())NULL;
1915 		pkt->pkt_time		= 0;
1916 		pkt->pkt_resid		= 0;
1917 		pkt->pkt_statistics	= 0;
1918 		pkt->pkt_reason		= 0;
1919 
1920 		/*
1921 		 * pkt_hba_private will point to sata pkt txlate structure
1922 		 */
1923 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
1924 		bzero(spx, sizeof (sata_pkt_txlate_t));
1925 
1926 		spx->txlt_scsi_pkt = pkt;
1927 		spx->txlt_sata_hba_inst = sata_hba_inst;
1928 
1929 		/* Allocate sata_pkt */
1930 		spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback);
1931 		if (spx->txlt_sata_pkt == NULL) {
1932 			/* Could not allocate sata pkt */
1933 			scsi_hba_pkt_free(ap, pkt);
1934 			return (NULL);
1935 		}
1936 		/* Set sata address */
1937 		spx->txlt_sata_pkt->satapkt_device.satadev_addr =
1938 		    sata_device.satadev_addr;
1939 		spx->txlt_sata_pkt->satapkt_device.satadev_rev =
1940 		    sata_device.satadev_rev;
1941 
1942 		if ((bp == NULL) || (bp->b_bcount == 0))
1943 			return (pkt);
1944 
1945 		spx->txlt_total_residue = bp->b_bcount;
1946 	} else {
1947 		new_pkt = FALSE;
1948 		/*
1949 		 * Packet was preallocated/initialized by previous call
1950 		 */
1951 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
1952 
1953 		if ((bp == NULL) || (bp->b_bcount == 0)) {
1954 			return (pkt);
1955 		}
1956 
1957 		/* Pkt is available already: spx->txlt_scsi_pkt == pkt; */
1958 	}
1959 
1960 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
1961 
1962 	/*
1963 	 * We use an adjusted version of the dma_attr, to account
1964 	 * for device addressing limitations.
1965 	 * sata_adjust_dma_attr() will handle sdinfo == NULL which may
1966 	 * happen when a device is not yet configured.
1967 	 */
1968 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1969 	    sata_device.satadev_addr.cport)));
1970 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
1971 	    &spx->txlt_sata_pkt->satapkt_device);
1972 	/* NULL sdinfo may be passsed to sata_adjust_dma_attr() */
1973 	sata_adjust_dma_attr(sdinfo,
1974 	    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
1975 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1976 	    sata_device.satadev_addr.cport)));
1977 	/*
1978 	 * Allocate necessary DMA resources for the packet's data buffer
1979 	 * NOTE:
1980 	 * In case of read/write commands, DMA resource allocation here is
1981 	 * based on the premise that the transfer length specified in
1982 	 * the read/write scsi cdb will match exactly DMA resources -
1983 	 * returning correct packet residue is crucial.
1984 	 */
1985 	if ((rval = sata_dma_buf_setup(spx, flags, callback, arg,
1986 	    &cur_dma_attr)) != DDI_SUCCESS) {
1987 		/*
1988 		 * If a DMA allocation request fails with
1989 		 * DDI_DMA_NOMAPPING, indicate the error by calling
1990 		 * bioerror(9F) with bp and an error code of EFAULT.
1991 		 * If a DMA allocation request fails with
1992 		 * DDI_DMA_TOOBIG, indicate the error by calling
1993 		 * bioerror(9F) with bp and an error code of EINVAL.
1994 		 * For DDI_DMA_NORESOURCES, we may have some of them allocated.
1995 		 * Request may be repeated later - there is no real error.
1996 		 */
1997 		switch (rval) {
1998 		case DDI_DMA_NORESOURCES:
1999 			bioerror(bp, 0);
2000 			break;
2001 		case DDI_DMA_NOMAPPING:
2002 		case DDI_DMA_BADATTR:
2003 			bioerror(bp, EFAULT);
2004 			break;
2005 		case DDI_DMA_TOOBIG:
2006 		default:
2007 			bioerror(bp, EINVAL);
2008 			break;
2009 		}
2010 		if (new_pkt == TRUE) {
2011 			/*
2012 			 * Since this is a new packet, we can clean-up
2013 			 * everything
2014 			 */
2015 			sata_scsi_destroy_pkt(ap, pkt);
2016 		} else {
2017 			/*
2018 			 * This is a re-used packet. It will be target driver's
2019 			 * responsibility to eventually destroy it (which
2020 			 * will free allocated resources).
2021 			 * Here, we just "complete" the request, leaving
2022 			 * allocated resources intact, so the request may
2023 			 * be retried.
2024 			 */
2025 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2026 			sata_pkt_free(spx);
2027 		}
2028 		return (NULL);
2029 	}
2030 	/* Set number of bytes that are not yet accounted for */
2031 	pkt->pkt_resid = spx->txlt_total_residue;
2032 	ASSERT(pkt->pkt_resid >= 0);
2033 
2034 	return (pkt);
2035 }
2036 
2037 /*
2038  * Implementation of scsi tran_start.
2039  * Translate scsi cmd into sata operation and return status.
2040  * ATAPI CDBs are passed to ATAPI devices - the device determines what commands
2041  * are supported.
2042  * For SATA hard disks, supported scsi commands:
2043  * SCMD_INQUIRY
2044  * SCMD_TEST_UNIT_READY
2045  * SCMD_START_STOP
2046  * SCMD_READ_CAPACITY
2047  * SCMD_REQUEST_SENSE
2048  * SCMD_LOG_SENSE_G1
2049  * SCMD_LOG_SELECT_G1
2050  * SCMD_MODE_SENSE	(specific pages)
2051  * SCMD_MODE_SENSE_G1	(specific pages)
2052  * SCMD_MODE_SELECT	(specific pages)
2053  * SCMD_MODE_SELECT_G1	(specific pages)
2054  * SCMD_SYNCHRONIZE_CACHE
2055  * SCMD_SYNCHRONIZE_CACHE_G1
2056  * SCMD_READ
2057  * SCMD_READ_G1
2058  * SCMD_READ_G4
2059  * SCMD_READ_G5
2060  * SCMD_WRITE
2061  * SCMD_WRITE_BUFFER
2062  * SCMD_WRITE_G1
2063  * SCMD_WRITE_G4
2064  * SCMD_WRITE_G5
2065  * SCMD_SEEK		(noop)
2066  * SCMD_SDIAG
2067  *
2068  * All other commands are rejected as unsupported.
2069  *
2070  * Returns:
2071  * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver
2072  * for execution. TRAN_ACCEPT may be returned also if device was removed but
2073  * a callback could be scheduled.
2074  * TRAN_BADPKT if cmd was directed to invalid address.
2075  * TRAN_FATAL_ERROR is command was rejected due to hardware error, including
2076  * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device
2077  * was removed and there was no callback specified in scsi pkt.
2078  * TRAN_BUSY if command could not be executed becasue HBA driver or SATA
2079  * framework was busy performing some other operation(s).
2080  *
2081  */
2082 static int
2083 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
2084 {
2085 	sata_hba_inst_t *sata_hba_inst =
2086 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2087 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2088 	sata_drive_info_t *sdinfo;
2089 	struct buf *bp;
2090 	int cport;
2091 	int rval;
2092 
2093 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2094 	    "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]);
2095 
2096 	ASSERT(spx != NULL &&
2097 	    spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL);
2098 
2099 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
2100 
2101 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2102 	sdinfo = sata_get_device_info(sata_hba_inst,
2103 	    &spx->txlt_sata_pkt->satapkt_device);
2104 	if (sdinfo == NULL ||
2105 	    SATA_CPORT_INFO(sata_hba_inst, cport)->cport_tgtnode_clean ==
2106 	    B_FALSE ||
2107 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2108 
2109 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2110 		pkt->pkt_reason = CMD_DEV_GONE;
2111 		/*
2112 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2113 		 * only in callback function (for normal requests) and
2114 		 * in the dump code path.
2115 		 * So, if the callback is available, we need to do
2116 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2117 		 */
2118 		if (pkt->pkt_comp != NULL) {
2119 			/* scsi callback required */
2120 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2121 			    (task_func_t *)pkt->pkt_comp,
2122 			    (void *)pkt, TQ_SLEEP) == NULL)
2123 				/* Scheduling the callback failed */
2124 				return (TRAN_BUSY);
2125 			return (TRAN_ACCEPT);
2126 		}
2127 		/* No callback available */
2128 		return (TRAN_FATAL_ERROR);
2129 	}
2130 
2131 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
2132 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2133 		rval = sata_txlt_atapi(spx);
2134 		SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2135 		    "sata_scsi_start atapi: rval %d\n", rval);
2136 		return (rval);
2137 	}
2138 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2139 
2140 	/* ATA Disk commands processing starts here */
2141 
2142 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2143 
2144 	switch (pkt->pkt_cdbp[0]) {
2145 
2146 	case SCMD_INQUIRY:
2147 		/* Mapped to identify device */
2148 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2149 			bp_mapin(bp);
2150 		rval = sata_txlt_inquiry(spx);
2151 		break;
2152 
2153 	case SCMD_TEST_UNIT_READY:
2154 		/*
2155 		 * SAT "SATA to ATA Translation" doc specifies translation
2156 		 * to ATA CHECK POWER MODE.
2157 		 */
2158 		rval = sata_txlt_test_unit_ready(spx);
2159 		break;
2160 
2161 	case SCMD_START_STOP:
2162 		/* Mapping depends on the command */
2163 		rval = sata_txlt_start_stop_unit(spx);
2164 		break;
2165 
2166 	case SCMD_READ_CAPACITY:
2167 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2168 			bp_mapin(bp);
2169 		rval = sata_txlt_read_capacity(spx);
2170 		break;
2171 
2172 	case SCMD_REQUEST_SENSE:
2173 		/*
2174 		 * Always No Sense, since we force ARQ
2175 		 */
2176 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2177 			bp_mapin(bp);
2178 		rval = sata_txlt_request_sense(spx);
2179 		break;
2180 
2181 	case SCMD_LOG_SENSE_G1:
2182 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2183 			bp_mapin(bp);
2184 		rval = sata_txlt_log_sense(spx);
2185 		break;
2186 
2187 	case SCMD_LOG_SELECT_G1:
2188 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2189 			bp_mapin(bp);
2190 		rval = sata_txlt_log_select(spx);
2191 		break;
2192 
2193 	case SCMD_MODE_SENSE:
2194 	case SCMD_MODE_SENSE_G1:
2195 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2196 			bp_mapin(bp);
2197 		rval = sata_txlt_mode_sense(spx);
2198 		break;
2199 
2200 
2201 	case SCMD_MODE_SELECT:
2202 	case SCMD_MODE_SELECT_G1:
2203 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2204 			bp_mapin(bp);
2205 		rval = sata_txlt_mode_select(spx);
2206 		break;
2207 
2208 	case SCMD_SYNCHRONIZE_CACHE:
2209 	case SCMD_SYNCHRONIZE_CACHE_G1:
2210 		rval = sata_txlt_synchronize_cache(spx);
2211 		break;
2212 
2213 	case SCMD_READ:
2214 	case SCMD_READ_G1:
2215 	case SCMD_READ_G4:
2216 	case SCMD_READ_G5:
2217 		rval = sata_txlt_read(spx);
2218 		break;
2219 	case SCMD_WRITE_BUFFER:
2220 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2221 			bp_mapin(bp);
2222 		rval = sata_txlt_write_buffer(spx);
2223 		break;
2224 
2225 	case SCMD_WRITE:
2226 	case SCMD_WRITE_G1:
2227 	case SCMD_WRITE_G4:
2228 	case SCMD_WRITE_G5:
2229 		rval = sata_txlt_write(spx);
2230 		break;
2231 
2232 	case SCMD_SEEK:
2233 		rval = sata_txlt_nodata_cmd_immediate(spx);
2234 		break;
2235 
2236 		/* Other cases will be filed later */
2237 		/* postponed until phase 2 of the development */
2238 	default:
2239 		rval = sata_txlt_invalid_command(spx);
2240 		break;
2241 	}
2242 
2243 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2244 	    "sata_scsi_start: rval %d\n", rval);
2245 
2246 	return (rval);
2247 }
2248 
2249 /*
2250  * Implementation of scsi tran_abort.
2251  * Abort specific pkt or all packets.
2252  *
2253  * Returns 1 if one or more packets were aborted, returns 0 otherwise
2254  *
2255  * May be called from an interrupt level.
2256  */
2257 static int
2258 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt)
2259 {
2260 	sata_hba_inst_t *sata_hba_inst =
2261 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2262 	sata_device_t	sata_device;
2263 	sata_pkt_t	*sata_pkt;
2264 
2265 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2266 	    "sata_scsi_abort: %s at target: 0x%x\n",
2267 	    scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target);
2268 
2269 	/* Validate address */
2270 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0)
2271 		/* Invalid address */
2272 		return (0);
2273 
2274 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2275 	    sata_device.satadev_addr.cport)));
2276 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2277 		/* invalid address */
2278 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2279 		    sata_device.satadev_addr.cport)));
2280 		return (0);
2281 	}
2282 	if (scsi_pkt == NULL) {
2283 		/*
2284 		 * Abort all packets.
2285 		 * Although we do not have specific packet, we still need
2286 		 * dummy packet structure to pass device address to HBA.
2287 		 * Allocate one, without sleeping. Fail if pkt cannot be
2288 		 * allocated.
2289 		 */
2290 		sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP);
2291 		if (sata_pkt == NULL) {
2292 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2293 			    sata_device.satadev_addr.cport)));
2294 			SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: "
2295 			    "could not allocate sata_pkt"));
2296 			return (0);
2297 		}
2298 		sata_pkt->satapkt_rev = SATA_PKT_REV;
2299 		sata_pkt->satapkt_device = sata_device;
2300 		sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
2301 	} else {
2302 		if (scsi_pkt->pkt_ha_private == NULL) {
2303 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2304 			    sata_device.satadev_addr.cport)));
2305 			return (0); /* Bad scsi pkt */
2306 		}
2307 		/* extract pointer to sata pkt */
2308 		sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)->
2309 		    txlt_sata_pkt;
2310 	}
2311 
2312 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2313 	    sata_device.satadev_addr.cport)));
2314 	/* Send abort request to HBA */
2315 	if ((*SATA_ABORT_FUNC(sata_hba_inst))
2316 	    (SATA_DIP(sata_hba_inst), sata_pkt,
2317 	    scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) ==
2318 	    SATA_SUCCESS) {
2319 		if (scsi_pkt == NULL)
2320 			kmem_free(sata_pkt, sizeof (sata_pkt_t));
2321 		/* Success */
2322 		return (1);
2323 	}
2324 	/* Else, something did not go right */
2325 	if (scsi_pkt == NULL)
2326 		kmem_free(sata_pkt, sizeof (sata_pkt_t));
2327 	/* Failure */
2328 	return (0);
2329 }
2330 
2331 
2332 /*
2333  * Implementation of scsi tran_reset.
2334  * RESET_ALL request is translated into port reset.
2335  * RESET_TARGET requests is translated into a device reset,
2336  * RESET_LUN request is accepted only for LUN 0 and translated into
2337  * device reset.
2338  * The target reset should cause all HBA active and queued packets to
2339  * be terminated and returned with pkt reason SATA_PKT_RESET prior to
2340  * the return. HBA should report reset event for the device.
2341  *
2342  * Returns 1 upon success, 0 upon failure.
2343  */
2344 static int
2345 sata_scsi_reset(struct scsi_address *ap, int level)
2346 {
2347 	sata_hba_inst_t	*sata_hba_inst =
2348 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2349 	sata_device_t	sata_device;
2350 	int		val;
2351 
2352 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2353 	    "sata_scsi_reset: level %d target: 0x%x\n",
2354 	    level, ap->a_target);
2355 
2356 	/* Validate address */
2357 	val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device);
2358 	if (val == -1)
2359 		/* Invalid address */
2360 		return (0);
2361 
2362 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2363 	    sata_device.satadev_addr.cport)));
2364 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2365 		/* invalid address */
2366 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2367 		    sata_device.satadev_addr.cport)));
2368 		return (0);
2369 	}
2370 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2371 	    sata_device.satadev_addr.cport)));
2372 	if (level == RESET_ALL) {
2373 		/* port reset - cport only */
2374 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2375 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2376 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2377 			return (1);
2378 		else
2379 			return (0);
2380 
2381 	} else if (val == 0 &&
2382 	    (level == RESET_TARGET || level == RESET_LUN)) {
2383 		/* reset device (device attached) */
2384 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2385 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2386 			return (1);
2387 		else
2388 			return (0);
2389 	}
2390 	return (0);
2391 }
2392 
2393 
2394 /*
2395  * Implementation of scsi tran_getcap (get transport/device capabilities).
2396  * Supported capabilities for SATA hard disks:
2397  * auto-rqsense		(always supported)
2398  * tagged-qing		(supported if HBA supports it)
2399  * untagged-qing	(could be supported if disk supports it, but because
2400  *			 caching behavior allowing untagged queuing actually
2401  *			 results in reduced performance.  sd tries to throttle
2402  *			 back to only 3 outstanding commands, which may
2403  *			 work for real SCSI disks, but with read ahead
2404  *			 caching, having more than 1 outstanding command
2405  *			 results in cache thrashing.)
2406  * sector_size
2407  * dma_max
2408  * interconnect-type	(INTERCONNECT_SATA)
2409  *
2410  * Supported capabilities for ATAPI devices (CD/DVD):
2411  * auto-rqsense		(always supported)
2412  * sector_size
2413  * dma_max
2414  * interconnect-type	(INTERCONNECT_SATA)
2415  *
2416  * Request for other capabilities is rejected as unsupported.
2417  *
2418  * Returns supported capability value, or -1 if capability is unsuppported or
2419  * the address is invalid - no device.
2420  */
2421 
2422 static int
2423 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
2424 {
2425 
2426 	sata_hba_inst_t 	*sata_hba_inst =
2427 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2428 	sata_device_t		sata_device;
2429 	sata_drive_info_t	*sdinfo;
2430 	ddi_dma_attr_t		adj_dma_attr;
2431 	int 			rval;
2432 
2433 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2434 	    "sata_scsi_getcap: target: 0x%x, cap: %s\n",
2435 	    ap->a_target, cap);
2436 
2437 	/*
2438 	 * We want to process the capabilities on per port granularity.
2439 	 * So, we are specifically restricting ourselves to whom != 0
2440 	 * to exclude the controller wide handling.
2441 	 */
2442 	if (cap == NULL || whom == 0)
2443 		return (-1);
2444 
2445 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2446 		/* Invalid address */
2447 		return (-1);
2448 	}
2449 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2450 	    sata_device.satadev_addr.cport)));
2451 	if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) ==
2452 	    NULL) {
2453 		/* invalid address */
2454 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2455 		    sata_device.satadev_addr.cport)));
2456 		return (-1);
2457 	}
2458 
2459 	switch (scsi_hba_lookup_capstr(cap)) {
2460 	case SCSI_CAP_ARQ:
2461 		rval = 1;		/* ARQ supported, turned on */
2462 		break;
2463 
2464 	case SCSI_CAP_SECTOR_SIZE:
2465 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK)
2466 			rval = SATA_DISK_SECTOR_SIZE;	/* fixed size */
2467 		else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD)
2468 			rval = SATA_ATAPI_SECTOR_SIZE;
2469 		else rval = -1;
2470 		break;
2471 
2472 	/*
2473 	 * untagged queuing cause a performance inversion because of
2474 	 * the way sd operates.  Because of this reason we do not
2475 	 * use it when available.
2476 	 */
2477 	case SCSI_CAP_UNTAGGED_QING:
2478 		if (sdinfo->satadrv_features_enabled &
2479 		    SATA_DEV_F_E_UNTAGGED_QING)
2480 			rval = 1;	/* Untagged queuing available */
2481 		else
2482 			rval = -1;	/* Untagged queuing not available */
2483 		break;
2484 
2485 	case SCSI_CAP_TAGGED_QING:
2486 		if ((sdinfo->satadrv_features_enabled &
2487 		    SATA_DEV_F_E_TAGGED_QING) &&
2488 		    (sdinfo->satadrv_max_queue_depth > 1))
2489 			rval = 1;	/* Tagged queuing available */
2490 		else
2491 			rval = -1;	/* Tagged queuing not available */
2492 		break;
2493 
2494 	case SCSI_CAP_DMA_MAX:
2495 		sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst),
2496 		    &adj_dma_attr);
2497 		rval = (int)adj_dma_attr.dma_attr_maxxfer;
2498 		/* We rely on the fact that dma_attr_maxxfer < 0x80000000 */
2499 		break;
2500 
2501 	case SCSI_CAP_INTERCONNECT_TYPE:
2502 		rval = INTERCONNECT_SATA;	/* SATA interconnect type */
2503 		break;
2504 
2505 	default:
2506 		rval = -1;
2507 		break;
2508 	}
2509 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2510 	    sata_device.satadev_addr.cport)));
2511 	return (rval);
2512 }
2513 
2514 /*
2515  * Implementation of scsi tran_setcap
2516  *
2517  * Only SCSI_CAP_UNTAGGED_QING and  SCSI_CAP_TAGGED_QING are changeable.
2518  *
2519  */
2520 static int
2521 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
2522 {
2523 	sata_hba_inst_t	*sata_hba_inst =
2524 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2525 	sata_device_t	sata_device;
2526 	sata_drive_info_t	*sdinfo;
2527 	int		rval;
2528 
2529 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2530 	    "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap);
2531 
2532 	/*
2533 	 * We want to process the capabilities on per port granularity.
2534 	 * So, we are specifically restricting ourselves to whom != 0
2535 	 * to exclude the controller wide handling.
2536 	 */
2537 	if (cap == NULL || whom == 0) {
2538 		return (-1);
2539 	}
2540 
2541 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2542 		/* Invalid address */
2543 		return (-1);
2544 	}
2545 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2546 	    sata_device.satadev_addr.cport)));
2547 	if ((sdinfo = sata_get_device_info(sata_hba_inst,
2548 	    &sata_device)) == NULL) {
2549 		/* invalid address */
2550 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2551 		    sata_device.satadev_addr.cport)));
2552 		return (-1);
2553 	}
2554 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2555 	    sata_device.satadev_addr.cport)));
2556 
2557 	switch (scsi_hba_lookup_capstr(cap)) {
2558 	case SCSI_CAP_ARQ:
2559 	case SCSI_CAP_SECTOR_SIZE:
2560 	case SCSI_CAP_DMA_MAX:
2561 	case SCSI_CAP_INTERCONNECT_TYPE:
2562 		rval = 0;
2563 		break;
2564 	case SCSI_CAP_UNTAGGED_QING:
2565 		if (SATA_QDEPTH(sata_hba_inst) > 1) {
2566 			rval = 1;
2567 			if (value == 1) {
2568 				sdinfo->satadrv_features_enabled |=
2569 				    SATA_DEV_F_E_UNTAGGED_QING;
2570 			} else if (value == 0) {
2571 				sdinfo->satadrv_features_enabled &=
2572 				    ~SATA_DEV_F_E_UNTAGGED_QING;
2573 			} else {
2574 				rval = -1;
2575 			}
2576 		} else {
2577 			rval = 0;
2578 		}
2579 		break;
2580 	case SCSI_CAP_TAGGED_QING:
2581 		/* This can TCQ or NCQ */
2582 		if (sata_func_enable & SATA_ENABLE_QUEUING &&
2583 		    ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ &&
2584 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) ||
2585 		    (sata_func_enable & SATA_ENABLE_NCQ &&
2586 		    sdinfo->satadrv_features_support & SATA_DEV_F_NCQ &&
2587 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) &&
2588 		    (sdinfo->satadrv_max_queue_depth > 1)) {
2589 			rval = 1;
2590 			if (value == 1) {
2591 				sdinfo->satadrv_features_enabled |=
2592 				    SATA_DEV_F_E_TAGGED_QING;
2593 			} else if (value == 0) {
2594 				sdinfo->satadrv_features_enabled &=
2595 				    ~SATA_DEV_F_E_TAGGED_QING;
2596 			} else {
2597 				rval = -1;
2598 			}
2599 		} else {
2600 			rval = 0;
2601 		}
2602 		break;
2603 	default:
2604 		rval = -1;
2605 		break;
2606 	}
2607 	return (rval);
2608 }
2609 
2610 /*
2611  * Implementations of scsi tran_destroy_pkt.
2612  * Free resources allocated by sata_scsi_init_pkt()
2613  */
2614 static void
2615 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2616 {
2617 	sata_pkt_txlate_t *spx;
2618 
2619 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2620 
2621 	sata_common_free_dma_rsrcs(spx);
2622 
2623 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2624 	sata_pkt_free(spx);
2625 
2626 	scsi_hba_pkt_free(ap, pkt);
2627 }
2628 
2629 /*
2630  * Implementation of scsi tran_dmafree.
2631  * Free DMA resources allocated by sata_scsi_init_pkt()
2632  */
2633 
2634 static void
2635 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
2636 {
2637 #ifndef __lock_lint
2638 	_NOTE(ARGUNUSED(ap))
2639 #endif
2640 	sata_pkt_txlate_t *spx;
2641 
2642 	ASSERT(pkt != NULL);
2643 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2644 
2645 	sata_common_free_dma_rsrcs(spx);
2646 }
2647 
2648 /*
2649  * Implementation of scsi tran_sync_pkt.
2650  *
2651  * The assumption below is that pkt is unique - there is no need to check ap
2652  *
2653  * Synchronize DMA buffer and, if the intermediate buffer is used, copy data
2654  * into/from the real buffer.
2655  */
2656 static void
2657 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2658 {
2659 #ifndef __lock_lint
2660 	_NOTE(ARGUNUSED(ap))
2661 #endif
2662 	int rval;
2663 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2664 	struct buf *bp;
2665 	int direction;
2666 
2667 	ASSERT(spx != NULL);
2668 	if (spx->txlt_buf_dma_handle != NULL) {
2669 		direction = spx->txlt_sata_pkt->
2670 		    satapkt_cmd.satacmd_flags.sata_data_direction;
2671 		if (spx->txlt_sata_pkt != NULL &&
2672 		    direction != SATA_DIR_NODATA_XFER) {
2673 			if (spx->txlt_tmp_buf != NULL) {
2674 				/* Intermediate DMA buffer used */
2675 				bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2676 
2677 				if (direction & SATA_DIR_WRITE) {
2678 					bcopy(bp->b_un.b_addr,
2679 					    spx->txlt_tmp_buf, bp->b_bcount);
2680 				}
2681 			}
2682 			/* Sync the buffer for device or for CPU */
2683 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle,   0, 0,
2684 			    (direction & SATA_DIR_WRITE) ?
2685 			    DDI_DMA_SYNC_FORDEV :  DDI_DMA_SYNC_FORCPU);
2686 			ASSERT(rval == DDI_SUCCESS);
2687 			if (spx->txlt_tmp_buf != NULL &&
2688 			    !(direction & SATA_DIR_WRITE)) {
2689 				/* Intermediate DMA buffer used for read */
2690 				bcopy(spx->txlt_tmp_buf,
2691 				    bp->b_un.b_addr, bp->b_bcount);
2692 			}
2693 
2694 		}
2695 	}
2696 }
2697 
2698 
2699 
2700 /* *******************  SATA - SCSI Translation functions **************** */
2701 /*
2702  * SCSI to SATA pkt and command translation and SATA to SCSI status/error
2703  * translation.
2704  */
2705 
2706 /*
2707  * Checks if a device exists and can be access and translates common
2708  * scsi_pkt data to sata_pkt data.
2709  *
2710  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and
2711  * sata_pkt was set-up.
2712  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not
2713  * exist and pkt_comp callback was scheduled.
2714  * Returns other TRAN_XXXXX values when error occured and command should be
2715  * rejected with the returned TRAN_XXXXX value.
2716  *
2717  * This function should be called with port mutex held.
2718  */
2719 static int
2720 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason)
2721 {
2722 	sata_drive_info_t *sdinfo;
2723 	sata_device_t sata_device;
2724 	const struct sata_cmd_flags sata_initial_cmd_flags = {
2725 		SATA_DIR_NODATA_XFER,
2726 		/* all other values to 0/FALSE */
2727 	};
2728 	/*
2729 	 * Pkt_reason has to be set if the pkt_comp callback is invoked,
2730 	 * and that implies TRAN_ACCEPT return value. Any other returned value
2731 	 * indicates that the scsi packet was not accepted (the reason will not
2732 	 * be checked by the scsi target driver).
2733 	 * To make debugging easier, we set pkt_reason to know value here.
2734 	 * It may be changed later when different completion reason is
2735 	 * determined.
2736 	 */
2737 	spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
2738 	*reason = CMD_TRAN_ERR;
2739 
2740 	/* Validate address */
2741 	switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst,
2742 	    &spx->txlt_scsi_pkt->pkt_address, &sata_device)) {
2743 
2744 	case -1:
2745 		/* Invalid address or invalid device type */
2746 		return (TRAN_BADPKT);
2747 	case 1:
2748 		/* valid address but no device - it has disappeared ? */
2749 		spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE;
2750 		*reason = CMD_DEV_GONE;
2751 		/*
2752 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2753 		 * only in callback function (for normal requests) and
2754 		 * in the dump code path.
2755 		 * So, if the callback is available, we need to do
2756 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2757 		 */
2758 		if (spx->txlt_scsi_pkt->pkt_comp != NULL) {
2759 			/* scsi callback required */
2760 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2761 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2762 			    (void *)spx->txlt_scsi_pkt,
2763 			    TQ_SLEEP) == NULL)
2764 				/* Scheduling the callback failed */
2765 				return (TRAN_BUSY);
2766 
2767 			return (TRAN_ACCEPT);
2768 		}
2769 		return (TRAN_FATAL_ERROR);
2770 	default:
2771 		/* all OK; pkt reason will be overwritten later */
2772 		break;
2773 	}
2774 	/*
2775 	 * If in an interrupt context, reject packet if it is to be
2776 	 * executed in polling mode
2777 	 */
2778 	if (servicing_interrupt() &&
2779 	    (spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
2780 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
2781 		    "sata_scsi_start: rejecting synchronous command because "
2782 		    "of interrupt context\n", NULL);
2783 		return (TRAN_BUSY);
2784 	}
2785 
2786 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2787 	    &spx->txlt_sata_pkt->satapkt_device);
2788 
2789 	/*
2790 	 * If device is in reset condition, reject the packet with
2791 	 * TRAN_BUSY, unless:
2792 	 * 1. system is panicking (dumping)
2793 	 * In such case only one thread is running and there is no way to
2794 	 * process reset.
2795 	 * 2. cfgadm operation is is progress (internal APCTL lock is set)
2796 	 * Some cfgadm operations involve drive commands, so reset condition
2797 	 * needs to be ignored for IOCTL operations.
2798 	 */
2799 	if ((sdinfo->satadrv_event_flags &
2800 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
2801 
2802 		if (!ddi_in_panic() &&
2803 		    ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst,
2804 		    sata_device.satadev_addr.cport) &
2805 		    SATA_APCTL_LOCK_PORT_BUSY) == 0)) {
2806 			spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
2807 			*reason = CMD_INCOMPLETE;
2808 			SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
2809 			    "sata_scsi_start: rejecting command because "
2810 			    "of device reset state\n", NULL);
2811 			return (TRAN_BUSY);
2812 		}
2813 	}
2814 
2815 	/*
2816 	 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by
2817 	 * sata_scsi_pkt_init() because pkt init had to work also with
2818 	 * non-existing devices.
2819 	 * Now we know that the packet was set-up for a real device, so its
2820 	 * type is known.
2821 	 */
2822 	spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type;
2823 
2824 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags;
2825 	if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst,
2826 	    sata_device.satadev_addr.cport)->cport_event_flags &
2827 	    SATA_APCTL_LOCK_PORT_BUSY) != 0) {
2828 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
2829 		    sata_ignore_dev_reset = B_TRUE;
2830 	}
2831 	/*
2832 	 * At this point the generic translation routine determined that the
2833 	 * scsi packet should be accepted. Packet completion reason may be
2834 	 * changed later when a different completion reason is determined.
2835 	 */
2836 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
2837 	*reason = CMD_CMPLT;
2838 
2839 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
2840 		/* Synchronous execution */
2841 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH |
2842 		    SATA_OPMODE_POLLING;
2843 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
2844 		    sata_ignore_dev_reset = ddi_in_panic();
2845 	} else {
2846 		/* Asynchronous execution */
2847 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH |
2848 		    SATA_OPMODE_INTERRUPTS;
2849 	}
2850 	/* Convert queuing information */
2851 	if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG)
2852 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag =
2853 		    B_TRUE;
2854 	else if (spx->txlt_scsi_pkt->pkt_flags &
2855 	    (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD))
2856 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag =
2857 		    B_TRUE;
2858 
2859 	/* Always limit pkt time */
2860 	if (spx->txlt_scsi_pkt->pkt_time == 0)
2861 		spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time;
2862 	else
2863 		/* Pass on scsi_pkt time */
2864 		spx->txlt_sata_pkt->satapkt_time =
2865 		    spx->txlt_scsi_pkt->pkt_time;
2866 
2867 	return (TRAN_ACCEPT);
2868 }
2869 
2870 
2871 /*
2872  * Translate ATA Identify Device data to SCSI Inquiry data.
2873  * This function may be called only for ATA devices.
2874  * This function should not be called for ATAPI devices - they
2875  * respond directly to SCSI Inquiry command.
2876  *
2877  * SATA Identify Device data has to be valid in sata_rive_info.
2878  * Buffer has to accomodate the inquiry length (36 bytes).
2879  *
2880  * This function should be called with a port mutex held.
2881  */
2882 static	void
2883 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst,
2884     sata_drive_info_t *sdinfo, uint8_t *buf)
2885 {
2886 
2887 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
2888 	struct sata_id *sid = &sdinfo->satadrv_id;
2889 
2890 	/* Start with a nice clean slate */
2891 	bzero((void *)inq, sizeof (struct scsi_inquiry));
2892 
2893 	/*
2894 	 * Rely on the dev_type for setting paripheral qualifier.
2895 	 * Assume that  DTYPE_RODIRECT applies to CD/DVD R/W devices.
2896 	 * It could be that DTYPE_OPTICAL could also qualify in the future.
2897 	 * ATAPI Inquiry may provide more data to the target driver.
2898 	 */
2899 	inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
2900 	    DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */
2901 
2902 	inq->inq_rmb = sid->ai_config & SATA_REM_MEDIA ? 1 : 0;
2903 	inq->inq_qual = 0;	/* Device type qualifier (obsolete in SCSI3? */
2904 	inq->inq_iso = 0;	/* ISO version */
2905 	inq->inq_ecma = 0;	/* ECMA version */
2906 	inq->inq_ansi = 3;	/* ANSI version - SCSI 3 */
2907 	inq->inq_aenc = 0;	/* Async event notification cap. */
2908 	inq->inq_trmiop = 0;	/* Supports TERMINATE I/O PROC msg - NO */
2909 	inq->inq_normaca = 0;	/* setting NACA bit supported - NO */
2910 	inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */
2911 	inq->inq_len = 31;	/* Additional length */
2912 	inq->inq_dualp = 0;	/* dual port device - NO */
2913 	inq->inq_reladdr = 0;	/* Supports relative addressing - NO */
2914 	inq->inq_sync = 0;	/* Supports synchronous data xfers - NO */
2915 	inq->inq_linked = 0;	/* Supports linked commands - NO */
2916 				/*
2917 				 * Queuing support - controller has to
2918 				 * support some sort of command queuing.
2919 				 */
2920 	if (SATA_QDEPTH(sata_hba_inst) > 1)
2921 		inq->inq_cmdque = 1; /* Supports command queueing - YES */
2922 	else
2923 		inq->inq_cmdque = 0; /* Supports command queueing - NO */
2924 	inq->inq_sftre = 0;	/* Supports Soft Reset option - NO ??? */
2925 	inq->inq_wbus32 = 0;	/* Supports 32 bit wide data xfers - NO */
2926 	inq->inq_wbus16 = 0;	/* Supports 16 bit wide data xfers - NO */
2927 
2928 #ifdef	_LITTLE_ENDIAN
2929 	/* Swap text fields to match SCSI format */
2930 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
2931 	swab(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
2932 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
2933 		swab(sid->ai_fw, inq->inq_revision, 4);	/* Revision level */
2934 	else
2935 		swab(&sid->ai_fw[4], inq->inq_revision, 4);	/* Rev. level */
2936 #else	/* _LITTLE_ENDIAN */
2937 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
2938 	bcopy(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
2939 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
2940 		bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
2941 	else
2942 		bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
2943 #endif	/* _LITTLE_ENDIAN */
2944 }
2945 
2946 
2947 /*
2948  * Scsi response set up for invalid command (command not supported)
2949  *
2950  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
2951  */
2952 static int
2953 sata_txlt_invalid_command(sata_pkt_txlate_t *spx)
2954 {
2955 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
2956 	struct scsi_extended_sense *sense;
2957 
2958 	scsipkt->pkt_reason = CMD_CMPLT;
2959 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
2960 	    STATE_SENT_CMD | STATE_GOT_STATUS;
2961 
2962 	*scsipkt->pkt_scbp = STATUS_CHECK;
2963 
2964 	sense = sata_arq_sense(spx);
2965 	sense->es_key = KEY_ILLEGAL_REQUEST;
2966 	sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE;
2967 
2968 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
2969 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
2970 
2971 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
2972 	    scsipkt->pkt_comp != NULL)
2973 		/* scsi callback required */
2974 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2975 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2976 		    (void *)spx->txlt_scsi_pkt,
2977 		    TQ_SLEEP) == NULL)
2978 			/* Scheduling the callback failed */
2979 			return (TRAN_BUSY);
2980 	return (TRAN_ACCEPT);
2981 }
2982 
2983 /*
2984  * Scsi response setup for
2985  * emulated non-data command that requires no action/return data
2986  *
2987  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
2988  */
2989 static 	int
2990 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx)
2991 {
2992 	int rval;
2993 	int reason;
2994 
2995 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
2996 
2997 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
2998 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
2999 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3000 		return (rval);
3001 	}
3002 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3003 
3004 	spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3005 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3006 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
3007 	*(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD;
3008 
3009 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3010 	    "Scsi_pkt completion reason %x\n",
3011 	    spx->txlt_scsi_pkt->pkt_reason);
3012 
3013 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 &&
3014 	    spx->txlt_scsi_pkt->pkt_comp != NULL)
3015 		/* scsi callback required */
3016 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3017 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3018 		    (void *)spx->txlt_scsi_pkt,
3019 		    TQ_SLEEP) == NULL)
3020 			/* Scheduling the callback failed */
3021 			return (TRAN_BUSY);
3022 	return (TRAN_ACCEPT);
3023 }
3024 
3025 
3026 /*
3027  * SATA translate command: Inquiry / Identify Device
3028  * Use cached Identify Device data for now, rather than issuing actual
3029  * Device Identify cmd request. If device is detached and re-attached,
3030  * asynchromous event processing should fetch and refresh Identify Device
3031  * data.
3032  * Two VPD pages are supported now:
3033  * Vital Product Data page
3034  * Unit Serial Number page
3035  *
3036  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3037  */
3038 
3039 #define	EVPD			1	/* Extended Vital Product Data flag */
3040 #define	CMDDT			2	/* Command Support Data - Obsolete */
3041 #define	INQUIRY_SUP_VPD_PAGE	0	/* Supported VDP Pages Page COde */
3042 #define	INQUIRY_USN_PAGE	0x80	/* Unit Serial Number Page Code */
3043 #define	INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */
3044 
3045 static int
3046 sata_txlt_inquiry(sata_pkt_txlate_t *spx)
3047 {
3048 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3049 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3050 	sata_drive_info_t *sdinfo;
3051 	struct scsi_extended_sense *sense;
3052 	int count;
3053 	uint8_t *p;
3054 	int i, j;
3055 	uint8_t page_buf[0xff]; /* Max length */
3056 	int rval, reason;
3057 
3058 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3059 
3060 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3061 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3062 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3063 		return (rval);
3064 	}
3065 
3066 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3067 	    &spx->txlt_sata_pkt->satapkt_device);
3068 
3069 	ASSERT(sdinfo != NULL);
3070 
3071 	scsipkt->pkt_reason = CMD_CMPLT;
3072 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3073 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3074 
3075 	/* Reject not supported request */
3076 	if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */
3077 		*scsipkt->pkt_scbp = STATUS_CHECK;
3078 		sense = sata_arq_sense(spx);
3079 		sense->es_key = KEY_ILLEGAL_REQUEST;
3080 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3081 		goto done;
3082 	}
3083 
3084 	/* Valid Inquiry request */
3085 	*scsipkt->pkt_scbp = STATUS_GOOD;
3086 
3087 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3088 
3089 		/*
3090 		 * Because it is fully emulated command storing data
3091 		 * programatically in the specified buffer, release
3092 		 * preallocated DMA resources before storing data in the buffer,
3093 		 * so no unwanted DMA sync would take place.
3094 		 */
3095 		sata_scsi_dmafree(NULL, scsipkt);
3096 
3097 		if (!(scsipkt->pkt_cdbp[1] & EVPD)) {
3098 			/* Standard Inquiry Data request */
3099 			struct scsi_inquiry inq;
3100 			unsigned int bufsize;
3101 
3102 			sata_identdev_to_inquiry(spx->txlt_sata_hba_inst,
3103 			    sdinfo, (uint8_t *)&inq);
3104 			/* Copy no more than requested */
3105 			count = MIN(bp->b_bcount,
3106 			    sizeof (struct scsi_inquiry));
3107 			bufsize = scsipkt->pkt_cdbp[4];
3108 			bufsize |= scsipkt->pkt_cdbp[3] << 8;
3109 			count = MIN(count, bufsize);
3110 			bcopy(&inq, bp->b_un.b_addr, count);
3111 
3112 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
3113 			scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3114 			    bufsize - count : 0;
3115 		} else {
3116 			/*
3117 			 * peripheral_qualifier = 0;
3118 			 *
3119 			 * We are dealing only with HD and will be
3120 			 * dealing with CD/DVD devices soon
3121 			 */
3122 			uint8_t peripheral_device_type =
3123 			    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3124 			    DTYPE_DIRECT : DTYPE_RODIRECT;
3125 
3126 			switch ((uint_t)scsipkt->pkt_cdbp[2]) {
3127 			case INQUIRY_SUP_VPD_PAGE:
3128 				/*
3129 				 * Request for suported Vital Product Data
3130 				 * pages - assuming only 2 page codes
3131 				 * supported
3132 				 */
3133 				page_buf[0] = peripheral_device_type;
3134 				page_buf[1] = INQUIRY_SUP_VPD_PAGE;
3135 				page_buf[2] = 0;
3136 				page_buf[3] = 2; /* page length */
3137 				page_buf[4] = INQUIRY_SUP_VPD_PAGE;
3138 				page_buf[5] = INQUIRY_USN_PAGE;
3139 				/* Copy no more than requested */
3140 				count = MIN(bp->b_bcount, 6);
3141 				bcopy(page_buf, bp->b_un.b_addr, count);
3142 				break;
3143 			case INQUIRY_USN_PAGE:
3144 				/*
3145 				 * Request for Unit Serial Number page
3146 				 */
3147 				page_buf[0] = peripheral_device_type;
3148 				page_buf[1] = INQUIRY_USN_PAGE;
3149 				page_buf[2] = 0;
3150 				page_buf[3] = 20; /* remaining page length */
3151 				p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser);
3152 #ifdef	_LITTLE_ENDIAN
3153 				swab(p, &page_buf[4], 20);
3154 #else
3155 				bcopy(p, &page_buf[4], 20);
3156 #endif
3157 				for (i = 0; i < 20; i++) {
3158 					if (page_buf[4 + i] == '\0' ||
3159 					    page_buf[4 + i] == '\040') {
3160 						break;
3161 					}
3162 				}
3163 				/*
3164 				 * 'i' contains string length.
3165 				 *
3166 				 * Least significant character of the serial
3167 				 * number shall appear as the last byte,
3168 				 * according to SBC-3 spec.
3169 				 */
3170 				p = &page_buf[20 + 4 - 1];
3171 				for (j = i; j > 0; j--, p--) {
3172 					*p = *(p - 20 + i);
3173 				}
3174 				p = &page_buf[4];
3175 				for (j = 20 - i; j > 0; j--) {
3176 					*p++ = '\040';
3177 				}
3178 				count = MIN(bp->b_bcount, 24);
3179 				bcopy(page_buf, bp->b_un.b_addr, count);
3180 				break;
3181 
3182 			case INQUIRY_DEV_IDENTIFICATION_PAGE:
3183 				/*
3184 				 * We may want to implement this page, when
3185 				 * identifiers are common for SATA devices
3186 				 * But not now.
3187 				 */
3188 				/*FALLTHROUGH*/
3189 
3190 			default:
3191 				/* Request for unsupported VPD page */
3192 				*scsipkt->pkt_scbp = STATUS_CHECK;
3193 				sense = sata_arq_sense(spx);
3194 				sense->es_key = KEY_ILLEGAL_REQUEST;
3195 				sense->es_add_code =
3196 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3197 				goto done;
3198 			}
3199 		}
3200 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3201 		scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3202 		    scsipkt->pkt_cdbp[4] - count : 0;
3203 	}
3204 done:
3205 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3206 
3207 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3208 	    "Scsi_pkt completion reason %x\n",
3209 	    scsipkt->pkt_reason);
3210 
3211 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3212 	    scsipkt->pkt_comp != NULL) {
3213 		/* scsi callback required */
3214 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3215 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3216 		    TQ_SLEEP) == NULL)
3217 			/* Scheduling the callback failed */
3218 			return (TRAN_BUSY);
3219 	}
3220 	return (TRAN_ACCEPT);
3221 }
3222 
3223 /*
3224  * SATA translate command: Request Sense.
3225  * Emulated command (ATA version for SATA hard disks)
3226  * Always NO SENSE, because any sense data should be reported by ARQ sense.
3227  *
3228  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3229  *
3230  * Note: There is a mismatch between already implemented Informational
3231  * Exception Mode Select page 0x1C and this function.
3232  * When MRIE bit is set in page 0x1C, Request Sense is supposed to return
3233  * NO SENSE and set additional sense code to the exception code - this is not
3234  * implemented here.
3235  */
3236 static int
3237 sata_txlt_request_sense(sata_pkt_txlate_t *spx)
3238 {
3239 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3240 	struct scsi_extended_sense sense;
3241 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3242 	int rval, reason;
3243 
3244 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3245 
3246 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3247 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3248 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3249 		return (rval);
3250 	}
3251 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3252 
3253 
3254 	scsipkt->pkt_reason = CMD_CMPLT;
3255 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3256 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3257 	*scsipkt->pkt_scbp = STATUS_GOOD;
3258 
3259 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3260 		/*
3261 		 * Because it is fully emulated command storing data
3262 		 * programatically in the specified buffer, release
3263 		 * preallocated DMA resources before storing data in the buffer,
3264 		 * so no unwanted DMA sync would take place.
3265 		 */
3266 		int count = MIN(bp->b_bcount,
3267 		    sizeof (struct scsi_extended_sense));
3268 		sata_scsi_dmafree(NULL, scsipkt);
3269 		bzero(&sense, sizeof (struct scsi_extended_sense));
3270 		sense.es_valid = 0;	/* Valid LBA */
3271 		sense.es_class = 7;	/* Response code 0x70 - current err */
3272 		sense.es_key = KEY_NO_SENSE;
3273 		sense.es_add_len = 6;	/* Additional length */
3274 		/* Copy no more than requested */
3275 		bcopy(&sense, bp->b_un.b_addr, count);
3276 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3277 		scsipkt->pkt_resid = 0;
3278 	}
3279 
3280 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3281 	    "Scsi_pkt completion reason %x\n",
3282 	    scsipkt->pkt_reason);
3283 
3284 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3285 	    scsipkt->pkt_comp != NULL)
3286 		/* scsi callback required */
3287 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3288 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3289 		    TQ_SLEEP) == NULL)
3290 			/* Scheduling the callback failed */
3291 			return (TRAN_BUSY);
3292 	return (TRAN_ACCEPT);
3293 }
3294 
3295 /*
3296  * SATA translate command: Test Unit Ready
3297  * At the moment this is an emulated command (ATA version for SATA hard disks).
3298  * May be translated into Check Power Mode command in the future
3299  *
3300  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3301  */
3302 static int
3303 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx)
3304 {
3305 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3306 	struct scsi_extended_sense *sense;
3307 	int power_state;
3308 	int rval, reason;
3309 
3310 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3311 
3312 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3313 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3314 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3315 		return (rval);
3316 	}
3317 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3318 
3319 	/* At this moment, emulate it rather than execute anything */
3320 	power_state = SATA_PWRMODE_ACTIVE;
3321 
3322 	scsipkt->pkt_reason = CMD_CMPLT;
3323 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3324 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3325 
3326 	switch (power_state) {
3327 	case SATA_PWRMODE_ACTIVE:
3328 	case SATA_PWRMODE_IDLE:
3329 		*scsipkt->pkt_scbp = STATUS_GOOD;
3330 		break;
3331 	default:
3332 		/* PWR mode standby */
3333 		*scsipkt->pkt_scbp = STATUS_CHECK;
3334 		sense = sata_arq_sense(spx);
3335 		sense->es_key = KEY_NOT_READY;
3336 		sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY;
3337 		break;
3338 	}
3339 
3340 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3341 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3342 
3343 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3344 	    scsipkt->pkt_comp != NULL)
3345 		/* scsi callback required */
3346 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3347 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3348 		    TQ_SLEEP) == NULL)
3349 			/* Scheduling the callback failed */
3350 			return (TRAN_BUSY);
3351 
3352 	return (TRAN_ACCEPT);
3353 }
3354 
3355 
3356 /*
3357  * SATA translate command: Start Stop Unit
3358  * Translation depends on a command:
3359  *	Start Unit translated into Idle Immediate
3360  *	Stop Unit translated into Standby Immediate
3361  *	Unload Media / NOT SUPPORTED YET
3362  *	Load Media / NOT SUPPROTED YET
3363  * Power condition bits are ignored, so is Immediate bit
3364  * Requesting synchronous execution.
3365  *
3366  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
3367  * appropriate values in scsi_pkt fields.
3368  */
3369 static int
3370 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx)
3371 {
3372 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3373 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3374 	struct scsi_extended_sense *sense;
3375 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3376 	int cport = SATA_TXLT_CPORT(spx);
3377 	int rval, reason;
3378 	int synch;
3379 
3380 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3381 	    "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1);
3382 
3383 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3384 
3385 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3386 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3387 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3388 		return (rval);
3389 	}
3390 
3391 	if (scsipkt->pkt_cdbp[4] & 2) {
3392 		/* Load/Unload Media - invalid request */
3393 		*scsipkt->pkt_scbp = STATUS_CHECK;
3394 		sense = sata_arq_sense(spx);
3395 		sense->es_key = KEY_ILLEGAL_REQUEST;
3396 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3397 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3398 
3399 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3400 		    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3401 
3402 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3403 		    scsipkt->pkt_comp != NULL)
3404 			/* scsi callback required */
3405 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3406 			    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3407 			    TQ_SLEEP) == NULL)
3408 				/* Scheduling the callback failed */
3409 				return (TRAN_BUSY);
3410 
3411 		return (TRAN_ACCEPT);
3412 	}
3413 	scmd->satacmd_addr_type = 0;
3414 	scmd->satacmd_sec_count_lsb = 0;
3415 	scmd->satacmd_lba_low_lsb = 0;
3416 	scmd->satacmd_lba_mid_lsb = 0;
3417 	scmd->satacmd_lba_high_lsb = 0;
3418 	scmd->satacmd_features_reg = 0;
3419 	scmd->satacmd_device_reg = 0;
3420 	scmd->satacmd_status_reg = 0;
3421 	if (scsipkt->pkt_cdbp[4] & 1) {
3422 		/* Start Unit */
3423 		scmd->satacmd_cmd_reg = SATAC_IDLE_IM;
3424 	} else {
3425 		/* Stop Unit */
3426 		scmd->satacmd_cmd_reg = SATAC_STANDBY_IM;
3427 	}
3428 
3429 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
3430 		/* Need to set-up a callback function */
3431 		spx->txlt_sata_pkt->satapkt_comp =
3432 		    sata_txlt_nodata_cmd_completion;
3433 		synch = FALSE;
3434 	} else {
3435 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
3436 		synch = TRUE;
3437 	}
3438 
3439 	/* Transfer command to HBA */
3440 	if (sata_hba_start(spx, &rval) != 0) {
3441 		/* Pkt not accepted for execution */
3442 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3443 		return (rval);
3444 	}
3445 
3446 	/*
3447 	 * If execution is non-synchronous,
3448 	 * a callback function will handle potential errors, translate
3449 	 * the response and will do a callback to a target driver.
3450 	 * If it was synchronous, check execution status using the same
3451 	 * framework callback.
3452 	 */
3453 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3454 	if (synch) {
3455 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3456 		    "synchronous execution status %x\n",
3457 		    spx->txlt_sata_pkt->satapkt_reason);
3458 
3459 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
3460 	}
3461 	return (TRAN_ACCEPT);
3462 
3463 }
3464 
3465 
3466 /*
3467  * SATA translate command:  Read Capacity.
3468  * Emulated command for SATA disks.
3469  * Capacity is retrieved from cached Idenifty Device data.
3470  * Identify Device data shows effective disk capacity, not the native
3471  * capacity, which may be limitted by Set Max Address command.
3472  * This is ATA version for SATA hard disks.
3473  *
3474  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3475  */
3476 static int
3477 sata_txlt_read_capacity(sata_pkt_txlate_t *spx)
3478 {
3479 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3480 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3481 	sata_drive_info_t *sdinfo;
3482 	uint64_t val;
3483 	uchar_t *rbuf;
3484 	int rval, reason;
3485 
3486 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3487 	    "sata_txlt_read_capacity: ", NULL);
3488 
3489 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3490 
3491 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3492 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3493 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3494 		return (rval);
3495 	}
3496 
3497 	scsipkt->pkt_reason = CMD_CMPLT;
3498 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3499 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3500 	*scsipkt->pkt_scbp = STATUS_GOOD;
3501 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3502 		/*
3503 		 * Because it is fully emulated command storing data
3504 		 * programatically in the specified buffer, release
3505 		 * preallocated DMA resources before storing data in the buffer,
3506 		 * so no unwanted DMA sync would take place.
3507 		 */
3508 		sata_scsi_dmafree(NULL, scsipkt);
3509 
3510 		sdinfo = sata_get_device_info(
3511 		    spx->txlt_sata_hba_inst,
3512 		    &spx->txlt_sata_pkt->satapkt_device);
3513 		/* Last logical block address */
3514 		val = sdinfo->satadrv_capacity - 1;
3515 		rbuf = (uchar_t *)bp->b_un.b_addr;
3516 		/* Need to swap endians to match scsi format */
3517 		rbuf[0] = (val >> 24) & 0xff;
3518 		rbuf[1] = (val >> 16) & 0xff;
3519 		rbuf[2] = (val >> 8) & 0xff;
3520 		rbuf[3] = val & 0xff;
3521 		/* block size - always 512 bytes, for now */
3522 		rbuf[4] = 0;
3523 		rbuf[5] = 0;
3524 		rbuf[6] = 0x02;
3525 		rbuf[7] = 0;
3526 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3527 		scsipkt->pkt_resid = 0;
3528 
3529 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n",
3530 		    sdinfo->satadrv_capacity -1);
3531 	}
3532 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3533 	/*
3534 	 * If a callback was requested, do it now.
3535 	 */
3536 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3537 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3538 
3539 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3540 	    scsipkt->pkt_comp != NULL)
3541 		/* scsi callback required */
3542 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3543 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3544 		    TQ_SLEEP) == NULL)
3545 			/* Scheduling the callback failed */
3546 			return (TRAN_BUSY);
3547 
3548 	return (TRAN_ACCEPT);
3549 }
3550 
3551 /*
3552  * SATA translate command: Mode Sense.
3553  * Translated into appropriate SATA command or emulated.
3554  * Saved Values Page Control (03) are not supported.
3555  *
3556  * NOTE: only caching mode sense page is currently implemented.
3557  *
3558  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3559  */
3560 
3561 static int
3562 sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
3563 {
3564 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
3565 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3566 	sata_drive_info_t *sdinfo;
3567 	sata_id_t *sata_id;
3568 	struct scsi_extended_sense *sense;
3569 	int 		len, bdlen, count, alc_len;
3570 	int		pc;	/* Page Control code */
3571 	uint8_t		*buf;	/* mode sense buffer */
3572 	int		rval, reason;
3573 
3574 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3575 	    "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
3576 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
3577 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
3578 
3579 	buf = kmem_zalloc(1024, KM_SLEEP);
3580 
3581 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3582 
3583 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3584 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3585 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3586 		kmem_free(buf, 1024);
3587 		return (rval);
3588 	}
3589 
3590 	scsipkt->pkt_reason = CMD_CMPLT;
3591 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3592 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3593 
3594 	pc = scsipkt->pkt_cdbp[2] >> 6;
3595 
3596 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3597 		/*
3598 		 * Because it is fully emulated command storing data
3599 		 * programatically in the specified buffer, release
3600 		 * preallocated DMA resources before storing data in the buffer,
3601 		 * so no unwanted DMA sync would take place.
3602 		 */
3603 		sata_scsi_dmafree(NULL, scsipkt);
3604 
3605 		len = 0;
3606 		bdlen = 0;
3607 		if (!(scsipkt->pkt_cdbp[1] & 8)) {
3608 			if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
3609 			    (scsipkt->pkt_cdbp[0] & 0x10))
3610 				bdlen = 16;
3611 			else
3612 				bdlen = 8;
3613 		}
3614 		/* Build mode parameter header */
3615 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3616 			/* 4-byte mode parameter header */
3617 			buf[len++] = 0;   	/* mode data length */
3618 			buf[len++] = 0;		/* medium type */
3619 			buf[len++] = 0;		/* dev-specific param */
3620 			buf[len++] = bdlen;	/* Block Descriptor length */
3621 		} else {
3622 			/* 8-byte mode parameter header */
3623 			buf[len++] = 0;		/* mode data length */
3624 			buf[len++] = 0;
3625 			buf[len++] = 0;		/* medium type */
3626 			buf[len++] = 0;		/* dev-specific param */
3627 			if (bdlen == 16)
3628 				buf[len++] = 1;	/* long lba descriptor */
3629 			else
3630 				buf[len++] = 0;
3631 			buf[len++] = 0;
3632 			buf[len++] = 0;		/* Block Descriptor length */
3633 			buf[len++] = bdlen;
3634 		}
3635 
3636 		sdinfo = sata_get_device_info(
3637 		    spx->txlt_sata_hba_inst,
3638 		    &spx->txlt_sata_pkt->satapkt_device);
3639 
3640 		/* Build block descriptor only if not disabled (DBD) */
3641 		if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
3642 			/* Block descriptor - direct-access device format */
3643 			if (bdlen == 8) {
3644 				/* build regular block descriptor */
3645 				buf[len++] =
3646 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
3647 				buf[len++] =
3648 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
3649 				buf[len++] =
3650 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
3651 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
3652 				buf[len++] = 0; /* density code */
3653 				buf[len++] = 0;
3654 				if (sdinfo->satadrv_type ==
3655 				    SATA_DTYPE_ATADISK)
3656 					buf[len++] = 2;
3657 				else
3658 					/* ATAPI */
3659 					buf[len++] = 8;
3660 				buf[len++] = 0;
3661 			} else if (bdlen == 16) {
3662 				/* Long LBA Accepted */
3663 				/* build long lba block descriptor */
3664 #ifndef __lock_lint
3665 				buf[len++] =
3666 				    (sdinfo->satadrv_capacity >> 56) & 0xff;
3667 				buf[len++] =
3668 				    (sdinfo->satadrv_capacity >> 48) & 0xff;
3669 				buf[len++] =
3670 				    (sdinfo->satadrv_capacity >> 40) & 0xff;
3671 				buf[len++] =
3672 				    (sdinfo->satadrv_capacity >> 32) & 0xff;
3673 #endif
3674 				buf[len++] =
3675 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
3676 				buf[len++] =
3677 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
3678 				buf[len++] =
3679 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
3680 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
3681 				buf[len++] = 0;
3682 				buf[len++] = 0; /* density code */
3683 				buf[len++] = 0;
3684 				buf[len++] = 0;
3685 				if (sdinfo->satadrv_type ==
3686 				    SATA_DTYPE_ATADISK)
3687 					buf[len++] = 2;
3688 				else
3689 					/* ATAPI */
3690 					buf[len++] = 8;
3691 				buf[len++] = 0;
3692 			}
3693 		}
3694 
3695 		sata_id = &sdinfo->satadrv_id;
3696 
3697 		/*
3698 		 * Add requested pages.
3699 		 * Page 3 and 4 are obsolete and we are not supporting them.
3700 		 * We deal now with:
3701 		 * caching (read/write cache control).
3702 		 * We should eventually deal with following mode pages:
3703 		 * error recovery  (0x01),
3704 		 * power condition (0x1a),
3705 		 * exception control page (enables SMART) (0x1c),
3706 		 * enclosure management (ses),
3707 		 * protocol-specific port mode (port control).
3708 		 */
3709 		switch (scsipkt->pkt_cdbp[2] & 0x3f) {
3710 		case MODEPAGE_RW_ERRRECOV:
3711 			/* DAD_MODE_ERR_RECOV */
3712 			/* R/W recovery */
3713 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
3714 			break;
3715 		case MODEPAGE_CACHING:
3716 			/* DAD_MODE_CACHE */
3717 			/* Reject not supported request for saved parameters */
3718 			if (pc == 3) {
3719 				*scsipkt->pkt_scbp = STATUS_CHECK;
3720 				sense = sata_arq_sense(spx);
3721 				sense->es_key = KEY_ILLEGAL_REQUEST;
3722 				sense->es_add_code =
3723 				    SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
3724 				goto done;
3725 			}
3726 
3727 			/* caching */
3728 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
3729 			break;
3730 		case MODEPAGE_INFO_EXCPT:
3731 			/* exception cntrl */
3732 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
3733 				len += sata_build_msense_page_1c(sdinfo, pc,
3734 				    buf+len);
3735 			}
3736 			else
3737 				goto err;
3738 			break;
3739 		case MODEPAGE_POWER_COND:
3740 			/* DAD_MODE_POWER_COND */
3741 			/* power condition */
3742 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
3743 			break;
3744 
3745 		case MODEPAGE_ACOUSTIC_MANAG:
3746 			/* acoustic management */
3747 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
3748 			break;
3749 		case MODEPAGE_ALLPAGES:
3750 			/* all pages */
3751 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
3752 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
3753 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
3754 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
3755 				len += sata_build_msense_page_1c(sdinfo, pc,
3756 				    buf+len);
3757 			}
3758 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
3759 			break;
3760 		default:
3761 		err:
3762 			/* Invalid request */
3763 			*scsipkt->pkt_scbp = STATUS_CHECK;
3764 			sense = sata_arq_sense(spx);
3765 			sense->es_key = KEY_ILLEGAL_REQUEST;
3766 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3767 			goto done;
3768 		}
3769 
3770 		/* fix total mode data length */
3771 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3772 			/* 4-byte mode parameter header */
3773 			buf[0] = len - 1;   	/* mode data length */
3774 		} else {
3775 			buf[0] = (len -2) >> 8;
3776 			buf[1] = (len -2) & 0xff;
3777 		}
3778 
3779 
3780 		/* Check allocation length */
3781 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3782 			alc_len = scsipkt->pkt_cdbp[4];
3783 		} else {
3784 			alc_len = scsipkt->pkt_cdbp[7];
3785 			alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
3786 		}
3787 		/*
3788 		 * We do not check for possible parameters truncation
3789 		 * (alc_len < len) assuming that the target driver works
3790 		 * correctly. Just avoiding overrun.
3791 		 * Copy no more than requested and possible, buffer-wise.
3792 		 */
3793 		count = MIN(alc_len, len);
3794 		count = MIN(bp->b_bcount, count);
3795 		bcopy(buf, bp->b_un.b_addr, count);
3796 
3797 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3798 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
3799 	}
3800 	*scsipkt->pkt_scbp = STATUS_GOOD;
3801 done:
3802 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3803 	(void) kmem_free(buf, 1024);
3804 
3805 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3806 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3807 
3808 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3809 	    scsipkt->pkt_comp != NULL)
3810 		/* scsi callback required */
3811 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3812 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3813 		    TQ_SLEEP) == NULL)
3814 			/* Scheduling the callback failed */
3815 			return (TRAN_BUSY);
3816 
3817 	return (TRAN_ACCEPT);
3818 }
3819 
3820 
3821 /*
3822  * SATA translate command: Mode Select.
3823  * Translated into appropriate SATA command or emulated.
3824  * Saving parameters is not supported.
3825  * Changing device capacity is not supported (although theoretically
3826  * possible by executing SET FEATURES/SET MAX ADDRESS)
3827  *
3828  * Assumption is that the target driver is working correctly.
3829  *
3830  * More than one SATA command may be executed to perform operations specified
3831  * by mode select pages. The first error terminates further execution.
3832  * Operations performed successully are not backed-up in such case.
3833  *
3834  * NOTE: Implemented pages:
3835  * - caching page
3836  * - informational exception page
3837  * - acoustic management page
3838  * Caching setup is remembered so it could be re-stored in case of
3839  * an unexpected device reset.
3840  *
3841  * Returns TRAN_XXXX.
3842  * If TRAN_ACCEPT is returned, appropriate values are set in scsi_pkt fields.
3843  */
3844 
3845 static int
3846 sata_txlt_mode_select(sata_pkt_txlate_t *spx)
3847 {
3848 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3849 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3850 	struct scsi_extended_sense *sense;
3851 	int len, pagelen, count, pllen;
3852 	uint8_t *buf;	/* mode select buffer */
3853 	int rval, stat, reason;
3854 	uint_t nointr_flag;
3855 	int dmod = 0;
3856 
3857 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3858 	    "sata_txlt_mode_select, pc %x page code 0x%02x\n",
3859 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
3860 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
3861 
3862 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3863 
3864 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3865 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3866 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3867 		return (rval);
3868 	}
3869 	/*
3870 	 * If in interrupt context, reject this packet because it may result
3871 	 * in issuing a synchronous command to HBA.
3872 	 */
3873 	if (servicing_interrupt()) {
3874 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
3875 		    "sata_txlt_mode_select: rejecting command because "
3876 		    "of interrupt context\n", NULL);
3877 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3878 		return (TRAN_BUSY);
3879 	}
3880 
3881 	rval = TRAN_ACCEPT;
3882 
3883 	scsipkt->pkt_reason = CMD_CMPLT;
3884 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3885 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3886 
3887 	/* Reject not supported request */
3888 	if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
3889 		*scsipkt->pkt_scbp = STATUS_CHECK;
3890 		sense = sata_arq_sense(spx);
3891 		sense->es_key = KEY_ILLEGAL_REQUEST;
3892 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3893 		goto done;
3894 	}
3895 
3896 	if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
3897 		pllen = scsipkt->pkt_cdbp[4];
3898 	} else {
3899 		pllen = scsipkt->pkt_cdbp[7];
3900 		pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
3901 	}
3902 
3903 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
3904 
3905 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
3906 		buf = (uint8_t *)bp->b_un.b_addr;
3907 		count = MIN(bp->b_bcount, pllen);
3908 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3909 		scsipkt->pkt_resid = 0;
3910 		pllen = count;
3911 
3912 		/*
3913 		 * Check the header to skip the block descriptor(s) - we
3914 		 * do not support setting device capacity.
3915 		 * Existing macros do not recognize long LBA dscriptor,
3916 		 * hence manual calculation.
3917 		 */
3918 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
3919 			/* 6-bytes CMD, 4 bytes header */
3920 			if (count <= 4)
3921 				goto done;		/* header only */
3922 			len = buf[3] + 4;
3923 		} else {
3924 			/* 10-bytes CMD, 8 bytes header */
3925 			if (count <= 8)
3926 				goto done;		/* header only */
3927 			len = buf[6];
3928 			len = (len << 8) + buf[7] + 8;
3929 		}
3930 		if (len >= count)
3931 			goto done;	/* header + descriptor(s) only */
3932 
3933 		pllen -= len;		/* remaining data length */
3934 
3935 		/*
3936 		 * We may be executing SATA command and want to execute it
3937 		 * in SYNCH mode, regardless of scsi_pkt setting.
3938 		 * Save scsi_pkt setting and indicate SYNCH mode
3939 		 */
3940 		nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
3941 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3942 		    scsipkt->pkt_comp != NULL) {
3943 			scsipkt->pkt_flags |= FLAG_NOINTR;
3944 		}
3945 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
3946 
3947 		/*
3948 		 * len is now the offset to a first mode select page
3949 		 * Process all pages
3950 		 */
3951 		while (pllen > 0) {
3952 			switch ((int)buf[len]) {
3953 			case MODEPAGE_CACHING:
3954 				/* No support for SP (saving) */
3955 				if (scsipkt->pkt_cdbp[1] & 0x01) {
3956 					*scsipkt->pkt_scbp = STATUS_CHECK;
3957 					sense = sata_arq_sense(spx);
3958 					sense->es_key = KEY_ILLEGAL_REQUEST;
3959 					sense->es_add_code =
3960 					    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3961 					goto done;
3962 				}
3963 				stat = sata_mode_select_page_8(spx,
3964 				    (struct mode_cache_scsi3 *)&buf[len],
3965 				    pllen, &pagelen, &rval, &dmod);
3966 				/*
3967 				 * The pagelen value indicates the number of
3968 				 * parameter bytes already processed.
3969 				 * The rval is the return value from
3970 				 * sata_tran_start().
3971 				 * The stat indicates the overall status of
3972 				 * the operation(s).
3973 				 */
3974 				if (stat != SATA_SUCCESS)
3975 					/*
3976 					 * Page processing did not succeed -
3977 					 * all error info is already set-up,
3978 					 * just return
3979 					 */
3980 					pllen = 0; /* this breaks the loop */
3981 				else {
3982 					len += pagelen;
3983 					pllen -= pagelen;
3984 				}
3985 				break;
3986 
3987 			case MODEPAGE_INFO_EXCPT:
3988 				stat = sata_mode_select_page_1c(spx,
3989 				    (struct mode_info_excpt_page *)&buf[len],
3990 				    pllen, &pagelen, &rval, &dmod);
3991 				/*
3992 				 * The pagelen value indicates the number of
3993 				 * parameter bytes already processed.
3994 				 * The rval is the return value from
3995 				 * sata_tran_start().
3996 				 * The stat indicates the overall status of
3997 				 * the operation(s).
3998 				 */
3999 				if (stat != SATA_SUCCESS)
4000 					/*
4001 					 * Page processing did not succeed -
4002 					 * all error info is already set-up,
4003 					 * just return
4004 					 */
4005 					pllen = 0; /* this breaks the loop */
4006 				else {
4007 					len += pagelen;
4008 					pllen -= pagelen;
4009 				}
4010 				break;
4011 
4012 			case MODEPAGE_ACOUSTIC_MANAG:
4013 				stat = sata_mode_select_page_30(spx,
4014 				    (struct mode_acoustic_management *)
4015 				    &buf[len], pllen, &pagelen, &rval, &dmod);
4016 				/*
4017 				 * The pagelen value indicates the number of
4018 				 * parameter bytes already processed.
4019 				 * The rval is the return value from
4020 				 * sata_tran_start().
4021 				 * The stat indicates the overall status of
4022 				 * the operation(s).
4023 				 */
4024 				if (stat != SATA_SUCCESS)
4025 					/*
4026 					 * Page processing did not succeed -
4027 					 * all error info is already set-up,
4028 					 * just return
4029 					 */
4030 					pllen = 0; /* this breaks the loop */
4031 				else {
4032 					len += pagelen;
4033 					pllen -= pagelen;
4034 				}
4035 
4036 				break;
4037 			default:
4038 				*scsipkt->pkt_scbp = STATUS_CHECK;
4039 				sense = sata_arq_sense(spx);
4040 				sense->es_key = KEY_ILLEGAL_REQUEST;
4041 				sense->es_add_code =
4042 				    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
4043 				goto done;
4044 			}
4045 		}
4046 	}
4047 done:
4048 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4049 	/*
4050 	 * If device parameters were modified, fetch and store the new
4051 	 * Identify Device data. Since port mutex could have been released
4052 	 * for accessing HBA driver, we need to re-check device existence.
4053 	 */
4054 	if (dmod != 0) {
4055 		sata_drive_info_t new_sdinfo, *sdinfo;
4056 		int rv = 0;
4057 
4058 		/*
4059 		 * Following statement has to be changed if this function is
4060 		 * used for devices other than SATA hard disks.
4061 		 */
4062 		new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
4063 
4064 		new_sdinfo.satadrv_addr =
4065 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr;
4066 		rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
4067 		    &new_sdinfo);
4068 
4069 		mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4070 		/*
4071 		 * Since port mutex could have been released when
4072 		 * accessing HBA driver, we need to re-check that the
4073 		 * framework still holds the device info structure.
4074 		 */
4075 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4076 		    &spx->txlt_sata_pkt->satapkt_device);
4077 		if (sdinfo != NULL) {
4078 			/*
4079 			 * Device still has info structure in the
4080 			 * sata framework. Copy newly fetched info
4081 			 */
4082 			if (rv == 0) {
4083 				sdinfo->satadrv_id = new_sdinfo.satadrv_id;
4084 				sata_save_drive_settings(sdinfo);
4085 			} else {
4086 				/*
4087 				 * Could not fetch new data - invalidate
4088 				 * sata_drive_info. That makes device
4089 				 * unusable.
4090 				 */
4091 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
4092 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
4093 			}
4094 		}
4095 		if (rv != 0 || sdinfo == NULL) {
4096 			/*
4097 			 * This changes the overall mode select completion
4098 			 * reason to a failed one !!!!!
4099 			 */
4100 			*scsipkt->pkt_scbp = STATUS_CHECK;
4101 			sense = sata_arq_sense(spx);
4102 			scsipkt->pkt_reason = CMD_INCOMPLETE;
4103 			rval = TRAN_ACCEPT;
4104 		}
4105 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4106 	}
4107 	/* Restore the scsi pkt flags */
4108 	scsipkt->pkt_flags &= ~FLAG_NOINTR;
4109 	scsipkt->pkt_flags |= nointr_flag;
4110 
4111 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4112 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4113 
4114 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4115 	    scsipkt->pkt_comp != NULL)
4116 		/* scsi callback required */
4117 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4118 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4119 		    TQ_SLEEP) == NULL)
4120 			/* Scheduling the callback failed */
4121 			return (TRAN_BUSY);
4122 
4123 	return (rval);
4124 }
4125 
4126 
4127 
4128 /*
4129  * Translate command: Log Sense
4130  */
4131 static 	int
4132 sata_txlt_log_sense(sata_pkt_txlate_t *spx)
4133 {
4134 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4135 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4136 	sata_drive_info_t *sdinfo;
4137 	struct scsi_extended_sense *sense;
4138 	int 		len, count, alc_len;
4139 	int		pc;	/* Page Control code */
4140 	int		page_code;	/* Page code */
4141 	uint8_t		*buf;	/* log sense buffer */
4142 	int		rval, reason;
4143 #define	MAX_LOG_SENSE_PAGE_SIZE	512
4144 
4145 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4146 	    "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
4147 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4148 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4149 
4150 	buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
4151 
4152 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4153 
4154 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4155 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4156 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4157 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4158 		return (rval);
4159 	}
4160 	/*
4161 	 * If in interrupt context, reject this packet because it may result
4162 	 * in issuing a synchronous command to HBA.
4163 	 */
4164 	if (servicing_interrupt()) {
4165 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
4166 		    "sata_log_sense: rejecting command because "
4167 		    "of interrupt context\n", NULL);
4168 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4169 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4170 		return (TRAN_BUSY);
4171 	}
4172 
4173 	scsipkt->pkt_reason = CMD_CMPLT;
4174 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4175 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4176 
4177 	pc = scsipkt->pkt_cdbp[2] >> 6;
4178 	page_code = scsipkt->pkt_cdbp[2] & 0x3f;
4179 
4180 	/* Reject not supported request for all but cumulative values */
4181 	switch (pc) {
4182 	case PC_CUMULATIVE_VALUES:
4183 		break;
4184 	default:
4185 		*scsipkt->pkt_scbp = STATUS_CHECK;
4186 		sense = sata_arq_sense(spx);
4187 		sense->es_key = KEY_ILLEGAL_REQUEST;
4188 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4189 		goto done;
4190 	}
4191 
4192 	switch (page_code) {
4193 	case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
4194 	case PAGE_CODE_SELF_TEST_RESULTS:
4195 	case PAGE_CODE_INFORMATION_EXCEPTIONS:
4196 	case PAGE_CODE_SMART_READ_DATA:
4197 		break;
4198 	default:
4199 		*scsipkt->pkt_scbp = STATUS_CHECK;
4200 		sense = sata_arq_sense(spx);
4201 		sense->es_key = KEY_ILLEGAL_REQUEST;
4202 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4203 		goto done;
4204 	}
4205 
4206 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4207 		/*
4208 		 * Because log sense uses local buffers for data retrieval from
4209 		 * the devices and sets the data programatically in the
4210 		 * original specified buffer, release preallocated DMA
4211 		 * resources before storing data in the original buffer,
4212 		 * so no unwanted DMA sync would take place.
4213 		 */
4214 		sata_id_t *sata_id;
4215 
4216 		sata_scsi_dmafree(NULL, scsipkt);
4217 
4218 		len = 0;
4219 
4220 		/* Build log parameter header */
4221 		buf[len++] = page_code;	/* page code as in the CDB */
4222 		buf[len++] = 0;		/* reserved */
4223 		buf[len++] = 0;		/* Zero out page length for now (MSB) */
4224 		buf[len++] = 0;		/* (LSB) */
4225 
4226 		sdinfo = sata_get_device_info(
4227 		    spx->txlt_sata_hba_inst,
4228 		    &spx->txlt_sata_pkt->satapkt_device);
4229 
4230 		/*
4231 		 * Add requested pages.
4232 		 */
4233 		switch (page_code) {
4234 		case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
4235 			len = sata_build_lsense_page_0(sdinfo, buf + len);
4236 			break;
4237 		case PAGE_CODE_SELF_TEST_RESULTS:
4238 			sata_id = &sdinfo->satadrv_id;
4239 			if ((! (sata_id->ai_cmdset84 &
4240 			    SATA_SMART_SELF_TEST_SUPPORTED)) ||
4241 			    (! (sata_id->ai_features87 &
4242 			    SATA_SMART_SELF_TEST_SUPPORTED))) {
4243 				*scsipkt->pkt_scbp = STATUS_CHECK;
4244 				sense = sata_arq_sense(spx);
4245 				sense->es_key = KEY_ILLEGAL_REQUEST;
4246 				sense->es_add_code =
4247 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4248 
4249 				goto done;
4250 			}
4251 			len = sata_build_lsense_page_10(sdinfo, buf + len,
4252 			    spx->txlt_sata_hba_inst);
4253 			break;
4254 		case PAGE_CODE_INFORMATION_EXCEPTIONS:
4255 			sata_id = &sdinfo->satadrv_id;
4256 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
4257 				*scsipkt->pkt_scbp = STATUS_CHECK;
4258 				sense = sata_arq_sense(spx);
4259 				sense->es_key = KEY_ILLEGAL_REQUEST;
4260 				sense->es_add_code =
4261 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4262 
4263 				goto done;
4264 			}
4265 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
4266 				*scsipkt->pkt_scbp = STATUS_CHECK;
4267 				sense = sata_arq_sense(spx);
4268 				sense->es_key = KEY_ABORTED_COMMAND;
4269 				sense->es_add_code =
4270 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
4271 				sense->es_qual_code =
4272 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
4273 
4274 				goto done;
4275 			}
4276 
4277 			len = sata_build_lsense_page_2f(sdinfo, buf + len,
4278 			    spx->txlt_sata_hba_inst);
4279 			break;
4280 		case PAGE_CODE_SMART_READ_DATA:
4281 			sata_id = &sdinfo->satadrv_id;
4282 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
4283 				*scsipkt->pkt_scbp = STATUS_CHECK;
4284 				sense = sata_arq_sense(spx);
4285 				sense->es_key = KEY_ILLEGAL_REQUEST;
4286 				sense->es_add_code =
4287 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4288 
4289 				goto done;
4290 			}
4291 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
4292 				*scsipkt->pkt_scbp = STATUS_CHECK;
4293 				sense = sata_arq_sense(spx);
4294 				sense->es_key = KEY_ABORTED_COMMAND;
4295 				sense->es_add_code =
4296 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
4297 				sense->es_qual_code =
4298 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
4299 
4300 				goto done;
4301 			}
4302 
4303 			/* This page doesn't include a page header */
4304 			len = sata_build_lsense_page_30(sdinfo, buf,
4305 			    spx->txlt_sata_hba_inst);
4306 			goto no_header;
4307 		default:
4308 			/* Invalid request */
4309 			*scsipkt->pkt_scbp = STATUS_CHECK;
4310 			sense = sata_arq_sense(spx);
4311 			sense->es_key = KEY_ILLEGAL_REQUEST;
4312 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4313 			goto done;
4314 		}
4315 
4316 		/* set parameter log sense data length */
4317 		buf[2] = len >> 8;	/* log sense length (MSB) */
4318 		buf[3] = len & 0xff;	/* log sense length (LSB) */
4319 
4320 		len += SCSI_LOG_PAGE_HDR_LEN;
4321 		ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
4322 
4323 no_header:
4324 		/* Check allocation length */
4325 		alc_len = scsipkt->pkt_cdbp[7];
4326 		alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
4327 
4328 		/*
4329 		 * We do not check for possible parameters truncation
4330 		 * (alc_len < len) assuming that the target driver works
4331 		 * correctly. Just avoiding overrun.
4332 		 * Copy no more than requested and possible, buffer-wise.
4333 		 */
4334 		count = MIN(alc_len, len);
4335 		count = MIN(bp->b_bcount, count);
4336 		bcopy(buf, bp->b_un.b_addr, count);
4337 
4338 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4339 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
4340 	}
4341 	*scsipkt->pkt_scbp = STATUS_GOOD;
4342 done:
4343 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4344 	(void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4345 
4346 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4347 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4348 
4349 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4350 	    scsipkt->pkt_comp != NULL)
4351 		/* scsi callback required */
4352 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4353 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4354 		    TQ_SLEEP) == NULL)
4355 			/* Scheduling the callback failed */
4356 			return (TRAN_BUSY);
4357 
4358 	return (TRAN_ACCEPT);
4359 }
4360 
4361 /*
4362  * Translate command: Log Select
4363  * Not implemented at this time - returns invalid command response.
4364  */
4365 static 	int
4366 sata_txlt_log_select(sata_pkt_txlate_t *spx)
4367 {
4368 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4369 	    "sata_txlt_log_select\n", NULL);
4370 
4371 	return (sata_txlt_invalid_command(spx));
4372 }
4373 
4374 
4375 /*
4376  * Translate command: Read (various types).
4377  * Translated into appropriate type of ATA READ command
4378  * for SATA hard disks.
4379  * Both the device capabilities and requested operation mode are
4380  * considered.
4381  *
4382  * Following scsi cdb fields are ignored:
4383  * rdprotect, dpo, fua, fua_nv, group_number.
4384  *
4385  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
4386  * enable variable sata_func_enable), the capability of the controller and
4387  * capability of a device are checked and if both support queueing, read
4388  * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
4389  * command rather than plain READ_XXX command.
4390  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
4391  * both the controller and device suport such functionality, the read
4392  * request will be translated to READ_FPDMA_QUEUED command.
4393  * In both cases the maximum queue depth is derived as minimum of:
4394  * HBA capability,device capability and sata_max_queue_depth variable setting.
4395  * The value passed to HBA driver is decremented by 1, because only 5 bits are
4396  * used to pass max queue depth value, and the maximum possible queue depth
4397  * is 32.
4398  *
4399  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4400  * appropriate values in scsi_pkt fields.
4401  */
4402 static int
4403 sata_txlt_read(sata_pkt_txlate_t *spx)
4404 {
4405 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4406 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4407 	sata_drive_info_t *sdinfo;
4408 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4409 	int cport = SATA_TXLT_CPORT(spx);
4410 	uint16_t sec_count;
4411 	uint64_t lba;
4412 	int rval, reason;
4413 	int synch;
4414 
4415 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4416 
4417 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4418 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4419 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4420 		return (rval);
4421 	}
4422 
4423 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4424 	    &spx->txlt_sata_pkt->satapkt_device);
4425 
4426 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
4427 	/*
4428 	 * Extract LBA and sector count from scsi CDB.
4429 	 */
4430 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
4431 	case SCMD_READ:
4432 		/* 6-byte scsi read cmd : 0x08 */
4433 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
4434 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
4435 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4436 		sec_count = scsipkt->pkt_cdbp[4];
4437 		/* sec_count 0 will be interpreted as 256 by a device */
4438 		break;
4439 	case SCMD_READ_G1:
4440 		/* 10-bytes scsi read command : 0x28 */
4441 		lba = scsipkt->pkt_cdbp[2];
4442 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4443 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4444 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4445 		sec_count = scsipkt->pkt_cdbp[7];
4446 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4447 		break;
4448 	case SCMD_READ_G5:
4449 		/* 12-bytes scsi read command : 0xA8 */
4450 		lba = scsipkt->pkt_cdbp[2];
4451 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4452 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4453 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4454 		sec_count = scsipkt->pkt_cdbp[6];
4455 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
4456 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4457 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
4458 		break;
4459 	case SCMD_READ_G4:
4460 		/* 16-bytes scsi read command : 0x88 */
4461 		lba = scsipkt->pkt_cdbp[2];
4462 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4463 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4464 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4465 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
4466 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
4467 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
4468 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
4469 		sec_count = scsipkt->pkt_cdbp[10];
4470 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
4471 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
4472 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
4473 		break;
4474 	default:
4475 		/* Unsupported command */
4476 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4477 		return (sata_txlt_invalid_command(spx));
4478 	}
4479 
4480 	/*
4481 	 * Check if specified address exceeds device capacity
4482 	 */
4483 	if ((lba >= sdinfo->satadrv_capacity) ||
4484 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
4485 		/* LBA out of range */
4486 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4487 		return (sata_txlt_lba_out_of_range(spx));
4488 	}
4489 
4490 	/*
4491 	 * For zero-length transfer, emulate good completion of the command
4492 	 * (reasons for rejecting the command were already checked).
4493 	 * No DMA resources were allocated.
4494 	 */
4495 	if (spx->txlt_dma_cookie_list == NULL) {
4496 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4497 		return (sata_emul_rw_completion(spx));
4498 	}
4499 
4500 	/*
4501 	 * Build cmd block depending on the device capability and
4502 	 * requested operation mode.
4503 	 * Do not bother with non-dma mode - we are working only with
4504 	 * devices supporting DMA.
4505 	 */
4506 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
4507 	scmd->satacmd_device_reg = SATA_ADH_LBA;
4508 	scmd->satacmd_cmd_reg = SATAC_READ_DMA;
4509 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
4510 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4511 		scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT;
4512 		scmd->satacmd_sec_count_msb = sec_count >> 8;
4513 #ifndef __lock_lint
4514 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
4515 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
4516 		scmd->satacmd_lba_high_msb = lba >> 40;
4517 #endif
4518 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
4519 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
4520 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
4521 	}
4522 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
4523 	scmd->satacmd_lba_low_lsb = lba & 0xff;
4524 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
4525 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
4526 	scmd->satacmd_features_reg = 0;
4527 	scmd->satacmd_status_reg = 0;
4528 	scmd->satacmd_error_reg = 0;
4529 
4530 	/*
4531 	 * Check if queueing commands should be used and switch
4532 	 * to appropriate command if possible
4533 	 */
4534 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
4535 		boolean_t using_queuing;
4536 
4537 		/* Queuing supported by controller and device? */
4538 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
4539 		    (sdinfo->satadrv_features_support &
4540 		    SATA_DEV_F_NCQ) &&
4541 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4542 		    SATA_CTLF_NCQ)) {
4543 			using_queuing = B_TRUE;
4544 
4545 			/* NCQ supported - use FPDMA READ */
4546 			scmd->satacmd_cmd_reg =
4547 			    SATAC_READ_FPDMA_QUEUED;
4548 			scmd->satacmd_features_reg_ext =
4549 			    scmd->satacmd_sec_count_msb;
4550 			scmd->satacmd_sec_count_msb = 0;
4551 		} else if ((sdinfo->satadrv_features_support &
4552 		    SATA_DEV_F_TCQ) &&
4553 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4554 		    SATA_CTLF_QCMD)) {
4555 			using_queuing = B_TRUE;
4556 
4557 			/* Legacy queueing */
4558 			if (sdinfo->satadrv_features_support &
4559 			    SATA_DEV_F_LBA48) {
4560 				scmd->satacmd_cmd_reg =
4561 				    SATAC_READ_DMA_QUEUED_EXT;
4562 				scmd->satacmd_features_reg_ext =
4563 				    scmd->satacmd_sec_count_msb;
4564 				scmd->satacmd_sec_count_msb = 0;
4565 			} else {
4566 				scmd->satacmd_cmd_reg =
4567 				    SATAC_READ_DMA_QUEUED;
4568 			}
4569 		} else	/* NCQ nor legacy queuing not supported */
4570 			using_queuing = B_FALSE;
4571 
4572 		/*
4573 		 * If queuing, the sector count goes in the features register
4574 		 * and the secount count will contain the tag.
4575 		 */
4576 		if (using_queuing) {
4577 			scmd->satacmd_features_reg =
4578 			    scmd->satacmd_sec_count_lsb;
4579 			scmd->satacmd_sec_count_lsb = 0;
4580 			scmd->satacmd_flags.sata_queued = B_TRUE;
4581 
4582 			/* Set-up maximum queue depth */
4583 			scmd->satacmd_flags.sata_max_queue_depth =
4584 			    sdinfo->satadrv_max_queue_depth - 1;
4585 		} else if (sdinfo->satadrv_features_enabled &
4586 		    SATA_DEV_F_E_UNTAGGED_QING) {
4587 			/*
4588 			 * Although NCQ/TCQ is not enabled, untagged queuing
4589 			 * may be still used.
4590 			 * Set-up the maximum untagged queue depth.
4591 			 * Use controller's queue depth from sata_hba_tran.
4592 			 * SATA HBA drivers may ignore this value and rely on
4593 			 * the internal limits.For drivers that do not
4594 			 * ignore untaged queue depth, limit the value to
4595 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
4596 			 * largest value that can be passed via
4597 			 * satacmd_flags.sata_max_queue_depth.
4598 			 */
4599 			scmd->satacmd_flags.sata_max_queue_depth =
4600 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
4601 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
4602 
4603 		} else {
4604 			scmd->satacmd_flags.sata_max_queue_depth = 0;
4605 		}
4606 	} else
4607 		scmd->satacmd_flags.sata_max_queue_depth = 0;
4608 
4609 	SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst,
4610 	    "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n",
4611 	    scmd->satacmd_cmd_reg, lba, sec_count);
4612 
4613 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4614 		/* Need callback function */
4615 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
4616 		synch = FALSE;
4617 	} else
4618 		synch = TRUE;
4619 
4620 	/* Transfer command to HBA */
4621 	if (sata_hba_start(spx, &rval) != 0) {
4622 		/* Pkt not accepted for execution */
4623 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4624 		return (rval);
4625 	}
4626 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4627 	/*
4628 	 * If execution is non-synchronous,
4629 	 * a callback function will handle potential errors, translate
4630 	 * the response and will do a callback to a target driver.
4631 	 * If it was synchronous, check execution status using the same
4632 	 * framework callback.
4633 	 */
4634 	if (synch) {
4635 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4636 		    "synchronous execution status %x\n",
4637 		    spx->txlt_sata_pkt->satapkt_reason);
4638 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
4639 	}
4640 	return (TRAN_ACCEPT);
4641 }
4642 
4643 
4644 /*
4645  * SATA translate command: Write (various types)
4646  * Translated into appropriate type of ATA WRITE command
4647  * for SATA hard disks.
4648  * Both the device capabilities and requested operation mode are
4649  * considered.
4650  *
4651  * Following scsi cdb fields are ignored:
4652  * rwprotect, dpo, fua, fua_nv, group_number.
4653  *
4654  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
4655  * enable variable sata_func_enable), the capability of the controller and
4656  * capability of a device are checked and if both support queueing, write
4657  * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT
4658  * command rather than plain WRITE_XXX command.
4659  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
4660  * both the controller and device suport such functionality, the write
4661  * request will be translated to WRITE_FPDMA_QUEUED command.
4662  * In both cases the maximum queue depth is derived as minimum of:
4663  * HBA capability,device capability and sata_max_queue_depth variable setting.
4664  * The value passed to HBA driver is decremented by 1, because only 5 bits are
4665  * used to pass max queue depth value, and the maximum possible queue depth
4666  * is 32.
4667  *
4668  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4669  * appropriate values in scsi_pkt fields.
4670  */
4671 static int
4672 sata_txlt_write(sata_pkt_txlate_t *spx)
4673 {
4674 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4675 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4676 	sata_drive_info_t *sdinfo;
4677 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4678 	int cport = SATA_TXLT_CPORT(spx);
4679 	uint16_t sec_count;
4680 	uint64_t lba;
4681 	int rval, reason;
4682 	int synch;
4683 
4684 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4685 
4686 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4687 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4688 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4689 		return (rval);
4690 	}
4691 
4692 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4693 	    &spx->txlt_sata_pkt->satapkt_device);
4694 
4695 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4696 	/*
4697 	 * Extract LBA and sector count from scsi CDB
4698 	 */
4699 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
4700 	case SCMD_WRITE:
4701 		/* 6-byte scsi read cmd : 0x0A */
4702 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
4703 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
4704 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4705 		sec_count = scsipkt->pkt_cdbp[4];
4706 		/* sec_count 0 will be interpreted as 256 by a device */
4707 		break;
4708 	case SCMD_WRITE_G1:
4709 		/* 10-bytes scsi write command : 0x2A */
4710 		lba = scsipkt->pkt_cdbp[2];
4711 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4712 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4713 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4714 		sec_count = scsipkt->pkt_cdbp[7];
4715 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4716 		break;
4717 	case SCMD_WRITE_G5:
4718 		/* 12-bytes scsi read command : 0xAA */
4719 		lba = scsipkt->pkt_cdbp[2];
4720 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4721 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4722 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4723 		sec_count = scsipkt->pkt_cdbp[6];
4724 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
4725 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4726 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
4727 		break;
4728 	case SCMD_WRITE_G4:
4729 		/* 16-bytes scsi write command : 0x8A */
4730 		lba = scsipkt->pkt_cdbp[2];
4731 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4732 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4733 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4734 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
4735 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
4736 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
4737 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
4738 		sec_count = scsipkt->pkt_cdbp[10];
4739 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
4740 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
4741 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
4742 		break;
4743 	default:
4744 		/* Unsupported command */
4745 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4746 		return (sata_txlt_invalid_command(spx));
4747 	}
4748 
4749 	/*
4750 	 * Check if specified address and length exceeds device capacity
4751 	 */
4752 	if ((lba >= sdinfo->satadrv_capacity) ||
4753 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
4754 		/* LBA out of range */
4755 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4756 		return (sata_txlt_lba_out_of_range(spx));
4757 	}
4758 
4759 	/*
4760 	 * For zero-length transfer, emulate good completion of the command
4761 	 * (reasons for rejecting the command were already checked).
4762 	 * No DMA resources were allocated.
4763 	 */
4764 	if (spx->txlt_dma_cookie_list == NULL) {
4765 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4766 		return (sata_emul_rw_completion(spx));
4767 	}
4768 
4769 	/*
4770 	 * Build cmd block depending on the device capability and
4771 	 * requested operation mode.
4772 	 * Do not bother with non-dma mode- we are working only with
4773 	 * devices supporting DMA.
4774 	 */
4775 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
4776 	scmd->satacmd_device_reg = SATA_ADH_LBA;
4777 	scmd->satacmd_cmd_reg = SATAC_WRITE_DMA;
4778 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
4779 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4780 		scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT;
4781 		scmd->satacmd_sec_count_msb = sec_count >> 8;
4782 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
4783 #ifndef __lock_lint
4784 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
4785 		scmd->satacmd_lba_high_msb = lba >> 40;
4786 #endif
4787 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
4788 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
4789 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
4790 	}
4791 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
4792 	scmd->satacmd_lba_low_lsb = lba & 0xff;
4793 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
4794 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
4795 	scmd->satacmd_features_reg = 0;
4796 	scmd->satacmd_status_reg = 0;
4797 	scmd->satacmd_error_reg = 0;
4798 
4799 	/*
4800 	 * Check if queueing commands should be used and switch
4801 	 * to appropriate command if possible
4802 	 */
4803 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
4804 		boolean_t using_queuing;
4805 
4806 		/* Queuing supported by controller and device? */
4807 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
4808 		    (sdinfo->satadrv_features_support &
4809 		    SATA_DEV_F_NCQ) &&
4810 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4811 		    SATA_CTLF_NCQ)) {
4812 			using_queuing = B_TRUE;
4813 
4814 			/* NCQ supported - use FPDMA WRITE */
4815 			scmd->satacmd_cmd_reg =
4816 			    SATAC_WRITE_FPDMA_QUEUED;
4817 			scmd->satacmd_features_reg_ext =
4818 			    scmd->satacmd_sec_count_msb;
4819 			scmd->satacmd_sec_count_msb = 0;
4820 		} else if ((sdinfo->satadrv_features_support &
4821 		    SATA_DEV_F_TCQ) &&
4822 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4823 		    SATA_CTLF_QCMD)) {
4824 			using_queuing = B_TRUE;
4825 
4826 			/* Legacy queueing */
4827 			if (sdinfo->satadrv_features_support &
4828 			    SATA_DEV_F_LBA48) {
4829 				scmd->satacmd_cmd_reg =
4830 				    SATAC_WRITE_DMA_QUEUED_EXT;
4831 				scmd->satacmd_features_reg_ext =
4832 				    scmd->satacmd_sec_count_msb;
4833 				scmd->satacmd_sec_count_msb = 0;
4834 			} else {
4835 				scmd->satacmd_cmd_reg =
4836 				    SATAC_WRITE_DMA_QUEUED;
4837 			}
4838 		} else	/*  NCQ nor legacy queuing not supported */
4839 			using_queuing = B_FALSE;
4840 
4841 		if (using_queuing) {
4842 			scmd->satacmd_features_reg =
4843 			    scmd->satacmd_sec_count_lsb;
4844 			scmd->satacmd_sec_count_lsb = 0;
4845 			scmd->satacmd_flags.sata_queued = B_TRUE;
4846 			/* Set-up maximum queue depth */
4847 			scmd->satacmd_flags.sata_max_queue_depth =
4848 			    sdinfo->satadrv_max_queue_depth - 1;
4849 		} else if (sdinfo->satadrv_features_enabled &
4850 		    SATA_DEV_F_E_UNTAGGED_QING) {
4851 			/*
4852 			 * Although NCQ/TCQ is not enabled, untagged queuing
4853 			 * may be still used.
4854 			 * Set-up the maximum untagged queue depth.
4855 			 * Use controller's queue depth from sata_hba_tran.
4856 			 * SATA HBA drivers may ignore this value and rely on
4857 			 * the internal limits. For drivera that do not
4858 			 * ignore untaged queue depth, limit the value to
4859 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
4860 			 * largest value that can be passed via
4861 			 * satacmd_flags.sata_max_queue_depth.
4862 			 */
4863 			scmd->satacmd_flags.sata_max_queue_depth =
4864 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
4865 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
4866 
4867 		} else {
4868 			scmd->satacmd_flags.sata_max_queue_depth = 0;
4869 		}
4870 	} else
4871 		scmd->satacmd_flags.sata_max_queue_depth = 0;
4872 
4873 	SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4874 	    "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n",
4875 	    scmd->satacmd_cmd_reg, lba, sec_count);
4876 
4877 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4878 		/* Need callback function */
4879 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
4880 		synch = FALSE;
4881 	} else
4882 		synch = TRUE;
4883 
4884 	/* Transfer command to HBA */
4885 	if (sata_hba_start(spx, &rval) != 0) {
4886 		/* Pkt not accepted for execution */
4887 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4888 		return (rval);
4889 	}
4890 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4891 
4892 	/*
4893 	 * If execution is non-synchronous,
4894 	 * a callback function will handle potential errors, translate
4895 	 * the response and will do a callback to a target driver.
4896 	 * If it was synchronous, check execution status using the same
4897 	 * framework callback.
4898 	 */
4899 	if (synch) {
4900 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4901 		    "synchronous execution status %x\n",
4902 		    spx->txlt_sata_pkt->satapkt_reason);
4903 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
4904 	}
4905 	return (TRAN_ACCEPT);
4906 }
4907 
4908 
4909 /*
4910  * Implements SCSI SBC WRITE BUFFER command download microcode option
4911  */
4912 static int
4913 sata_txlt_write_buffer(sata_pkt_txlate_t *spx)
4914 {
4915 #define	WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE			4
4916 #define	WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE			5
4917 
4918 	sata_hba_inst_t *sata_hba_inst = SATA_TXLT_HBA_INST(spx);
4919 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4920 	struct sata_pkt *sata_pkt = spx->txlt_sata_pkt;
4921 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4922 
4923 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4924 	struct scsi_extended_sense *sense;
4925 	int rval, mode, sector_count, reason;
4926 	int cport = SATA_TXLT_CPORT(spx);
4927 
4928 	mode = scsipkt->pkt_cdbp[1] & 0x1f;
4929 
4930 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4931 	    "sata_txlt_write_buffer, mode 0x%x\n", mode);
4932 
4933 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4934 
4935 	if ((rval = sata_txlt_generic_pkt_info(spx, &reason)) != TRAN_ACCEPT) {
4936 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4937 		return (rval);
4938 	}
4939 	/*
4940 	 * If in interrupt context, reject this packet because it would issue
4941 	 * a synchronous command to HBA.
4942 	 */
4943 	if (servicing_interrupt()) {
4944 		SATADBG1(SATA_DBG_INTR_CTX, spx->txlt_sata_hba_inst,
4945 		    "sata_txlt_write_buffer: rejecting command because "
4946 		    "of interrupt context\n", NULL);
4947 		return (TRAN_BUSY);
4948 	}
4949 
4950 	/* Use synchronous mode */
4951 	spx->txlt_sata_pkt->satapkt_op_mode
4952 	    |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
4953 
4954 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4955 
4956 	scsipkt->pkt_reason = CMD_CMPLT;
4957 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4958 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4959 
4960 	/*
4961 	 * The SCSI to ATA translation specification only calls
4962 	 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE.
4963 	 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but
4964 	 * ATA 8 (draft) got rid of download microcode for temp
4965 	 * and it is even optional for ATA 7, so it may be aborted.
4966 	 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as
4967 	 * it is not specified and the buffer offset for SCSI is a 16-bit
4968 	 * value in bytes, but for ATA it is a 16-bit offset in 512 byte
4969 	 * sectors.  Thus the offset really doesn't buy us anything.
4970 	 * If and when ATA 8 is stabilized and the SCSI to ATA specification
4971 	 * is revised, this can be revisisted.
4972 	 */
4973 	/* Reject not supported request */
4974 	switch (mode) {
4975 	case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE:
4976 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP;
4977 		break;
4978 	case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE:
4979 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE;
4980 		break;
4981 	default:
4982 		goto bad_param;
4983 	}
4984 
4985 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
4986 
4987 	scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE;
4988 	if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0)
4989 		goto bad_param;
4990 	sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE;
4991 	scmd->satacmd_sec_count_lsb = (uint8_t)sector_count;
4992 	scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8;
4993 	scmd->satacmd_lba_mid_lsb = 0;
4994 	scmd->satacmd_lba_high_lsb = 0;
4995 	scmd->satacmd_device_reg = 0;
4996 	spx->txlt_sata_pkt->satapkt_comp = NULL;
4997 	scmd->satacmd_addr_type = 0;
4998 
4999 	/* Transfer command to HBA */
5000 	if (sata_hba_start(spx, &rval) != 0) {
5001 		/* Pkt not accepted for execution */
5002 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
5003 		return (rval);
5004 	}
5005 
5006 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
5007 
5008 	/* Then we need synchronous check the status of the disk */
5009 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5010 	    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
5011 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5012 		scsipkt->pkt_reason = CMD_CMPLT;
5013 
5014 		/* Download commmand succeed, so probe and identify device */
5015 		sata_reidentify_device(spx);
5016 	} else {
5017 		/* Something went wrong, microcode download command failed */
5018 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5019 		*scsipkt->pkt_scbp = STATUS_CHECK;
5020 		sense = sata_arq_sense(spx);
5021 		switch (sata_pkt->satapkt_reason) {
5022 		case SATA_PKT_PORT_ERROR:
5023 			/*
5024 			 * We have no device data. Assume no data transfered.
5025 			 */
5026 			sense->es_key = KEY_HARDWARE_ERROR;
5027 			break;
5028 
5029 		case SATA_PKT_DEV_ERROR:
5030 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5031 			    SATA_STATUS_ERR) {
5032 				/*
5033 				 * determine dev error reason from error
5034 				 * reg content
5035 				 */
5036 				sata_decode_device_error(spx, sense);
5037 				break;
5038 			}
5039 			/* No extended sense key - no info available */
5040 			break;
5041 
5042 		case SATA_PKT_TIMEOUT:
5043 			scsipkt->pkt_reason = CMD_TIMEOUT;
5044 			scsipkt->pkt_statistics |=
5045 			    STAT_TIMEOUT | STAT_DEV_RESET;
5046 			/* No extended sense key ? */
5047 			break;
5048 
5049 		case SATA_PKT_ABORTED:
5050 			scsipkt->pkt_reason = CMD_ABORTED;
5051 			scsipkt->pkt_statistics |= STAT_ABORTED;
5052 			/* No extended sense key ? */
5053 			break;
5054 
5055 		case SATA_PKT_RESET:
5056 			/* pkt aborted by an explicit reset from a host */
5057 			scsipkt->pkt_reason = CMD_RESET;
5058 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5059 			break;
5060 
5061 		default:
5062 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5063 			    "sata_txlt_nodata_cmd_completion: "
5064 			    "invalid packet completion reason %d",
5065 			    sata_pkt->satapkt_reason));
5066 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5067 			break;
5068 		}
5069 
5070 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5071 		    "scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5072 
5073 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5074 		    scsipkt->pkt_comp != NULL)
5075 			/* scsi callback required */
5076 			(*scsipkt->pkt_comp)(scsipkt);
5077 	}
5078 	return (TRAN_ACCEPT);
5079 
5080 bad_param:
5081 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5082 	*scsipkt->pkt_scbp = STATUS_CHECK;
5083 	sense = sata_arq_sense(spx);
5084 	sense->es_key = KEY_ILLEGAL_REQUEST;
5085 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5086 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5087 	    scsipkt->pkt_comp != NULL) {
5088 		/* scsi callback required */
5089 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5090 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5091 		    TQ_SLEEP) == 0) {
5092 			/* Scheduling the callback failed */
5093 			rval = TRAN_BUSY;
5094 		}
5095 	}
5096 	return (rval);
5097 }
5098 
5099 /*
5100  * Re-identify device after doing a firmware download.
5101  */
5102 static void
5103 sata_reidentify_device(sata_pkt_txlate_t *spx)
5104 {
5105 #define	DOWNLOAD_WAIT_TIME_SECS	60
5106 #define	DOWNLOAD_WAIT_INTERVAL_SECS	1
5107 	int rval;
5108 	int retry_cnt;
5109 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5110 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
5111 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
5112 	sata_drive_info_t *sdinfo;
5113 
5114 	/*
5115 	 * Before returning good status, probe device.
5116 	 * Device probing will get IDENTIFY DEVICE data, if possible.
5117 	 * The assumption is that the new microcode is applied by the
5118 	 * device. It is a caller responsibility to verify this.
5119 	 */
5120 	for (retry_cnt = 0;
5121 	    retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS;
5122 	    retry_cnt++) {
5123 		rval = sata_probe_device(sata_hba_inst, &sata_device);
5124 
5125 		if (rval == SATA_SUCCESS) { /* Set default features */
5126 			sdinfo = sata_get_device_info(sata_hba_inst,
5127 			    &sata_device);
5128 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
5129 			    SATA_SUCCESS) {
5130 				/* retry */
5131 				(void) sata_initialize_device(sata_hba_inst,
5132 				    sdinfo);
5133 			}
5134 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5135 			    scsipkt->pkt_comp != NULL)
5136 				(*scsipkt->pkt_comp)(scsipkt);
5137 			return;
5138 		} else if (rval == SATA_RETRY) {
5139 			delay(drv_usectohz(1000000 *
5140 			    DOWNLOAD_WAIT_INTERVAL_SECS));
5141 			continue;
5142 		} else	/* failed - no reason to retry */
5143 			break;
5144 	}
5145 
5146 	/*
5147 	 * Something went wrong, device probing failed.
5148 	 */
5149 	SATA_LOG_D((sata_hba_inst, CE_WARN,
5150 	    "Cannot probe device after downloading microcode\n"));
5151 
5152 	/* Reset device to force retrying the probe. */
5153 	(void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst))
5154 	    (SATA_DIP(sata_hba_inst), &sata_device);
5155 
5156 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5157 	    scsipkt->pkt_comp != NULL)
5158 		(*scsipkt->pkt_comp)(scsipkt);
5159 }
5160 
5161 
5162 /*
5163  * Translate command: Synchronize Cache.
5164  * Translates into Flush Cache command for SATA hard disks.
5165  *
5166  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
5167  * appropriate values in scsi_pkt fields.
5168  */
5169 static 	int
5170 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx)
5171 {
5172 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5173 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
5174 	int cport = SATA_TXLT_CPORT(spx);
5175 	int rval, reason;
5176 	int synch;
5177 
5178 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5179 
5180 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
5181 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5182 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5183 		return (rval);
5184 	}
5185 
5186 	scmd->satacmd_addr_type = 0;
5187 	scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE;
5188 	scmd->satacmd_device_reg = 0;
5189 	scmd->satacmd_sec_count_lsb = 0;
5190 	scmd->satacmd_lba_low_lsb = 0;
5191 	scmd->satacmd_lba_mid_lsb = 0;
5192 	scmd->satacmd_lba_high_lsb = 0;
5193 	scmd->satacmd_features_reg = 0;
5194 	scmd->satacmd_status_reg = 0;
5195 	scmd->satacmd_error_reg = 0;
5196 
5197 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5198 	    "sata_txlt_synchronize_cache\n", NULL);
5199 
5200 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5201 		/* Need to set-up a callback function */
5202 		spx->txlt_sata_pkt->satapkt_comp =
5203 		    sata_txlt_nodata_cmd_completion;
5204 		synch = FALSE;
5205 	} else
5206 		synch = TRUE;
5207 
5208 	/* Transfer command to HBA */
5209 	if (sata_hba_start(spx, &rval) != 0) {
5210 		/* Pkt not accepted for execution */
5211 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5212 		return (rval);
5213 	}
5214 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5215 
5216 	/*
5217 	 * If execution non-synchronous, it had to be completed
5218 	 * a callback function will handle potential errors, translate
5219 	 * the response and will do a callback to a target driver.
5220 	 * If it was synchronous, check status, using the same
5221 	 * framework callback.
5222 	 */
5223 	if (synch) {
5224 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5225 		    "synchronous execution status %x\n",
5226 		    spx->txlt_sata_pkt->satapkt_reason);
5227 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
5228 	}
5229 	return (TRAN_ACCEPT);
5230 }
5231 
5232 
5233 /*
5234  * Send pkt to SATA HBA driver
5235  *
5236  * This function may be called only if the operation is requested by scsi_pkt,
5237  * i.e. scsi_pkt is not NULL.
5238  *
5239  * This function has to be called with cport mutex held. It does release
5240  * the mutex when it calls HBA driver sata_tran_start function and
5241  * re-acquires it afterwards.
5242  *
5243  * If return value is 0, pkt was accepted, -1 otherwise
5244  * rval is set to appropriate sata_scsi_start return value.
5245  *
5246  * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not
5247  * have called the sata_pkt callback function for this packet.
5248  *
5249  * The scsi callback has to be performed by the caller of this routine.
5250  *
5251  * Note 2: No port multiplier support for now.
5252  */
5253 static int
5254 sata_hba_start(sata_pkt_txlate_t *spx, int *rval)
5255 {
5256 	int stat, cport;
5257 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
5258 	sata_drive_info_t *sdinfo;
5259 	sata_device_t *sata_device;
5260 	uint8_t cmd;
5261 	struct sata_cmd_flags cmd_flags;
5262 
5263 	ASSERT(spx->txlt_sata_pkt != NULL);
5264 
5265 	cport = SATA_TXLT_CPORT(spx);
5266 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5267 
5268 	sdinfo = sata_get_device_info(sata_hba_inst,
5269 	    &spx->txlt_sata_pkt->satapkt_device);
5270 	ASSERT(sdinfo != NULL);
5271 
5272 	/* Clear device reset state? */
5273 	if (sdinfo->satadrv_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) {
5274 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
5275 		    sata_clear_dev_reset = B_TRUE;
5276 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_CLEAR_DEVICE_RESET;
5277 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5278 		    "sata_hba_start: clearing device reset state\n", NULL);
5279 	}
5280 	cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg;
5281 	cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags;
5282 	sata_device = &spx->txlt_sata_pkt->satapkt_device;
5283 
5284 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5285 
5286 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5287 	    "Sata cmd 0x%2x\n", cmd);
5288 
5289 	stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
5290 	    spx->txlt_sata_pkt);
5291 
5292 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5293 	/*
5294 	 * If sata pkt was accepted and executed in asynchronous mode, i.e.
5295 	 * with the sata callback, the sata_pkt could be already destroyed
5296 	 * by the time we check ther return status from the hba_start()
5297 	 * function, because sata_scsi_destroy_pkt() could have been already
5298 	 * called (perhaps in the interrupt context). So, in such case, there
5299 	 * should be no references to it. In other cases, sata_pkt still
5300 	 * exists.
5301 	 */
5302 	if (stat == SATA_TRAN_ACCEPTED) {
5303 		/*
5304 		 * pkt accepted for execution.
5305 		 * If it was executed synchronously, it is already completed
5306 		 * and pkt completion_reason indicates completion status.
5307 		 */
5308 		*rval = TRAN_ACCEPT;
5309 		return (0);
5310 	}
5311 
5312 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
5313 	switch (stat) {
5314 	case SATA_TRAN_QUEUE_FULL:
5315 		/*
5316 		 * Controller detected queue full condition.
5317 		 */
5318 		SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst,
5319 		    "sata_hba_start: queue full\n", NULL);
5320 
5321 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
5322 		*spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL;
5323 
5324 		*rval = TRAN_BUSY;
5325 		break;
5326 
5327 	case SATA_TRAN_PORT_ERROR:
5328 		/*
5329 		 * Communication/link with device or general port error
5330 		 * detected before pkt execution begun.
5331 		 */
5332 		if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
5333 		    SATA_ADDR_CPORT ||
5334 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
5335 		    SATA_ADDR_DCPORT)
5336 			sata_log(sata_hba_inst, CE_CONT,
5337 			    "SATA port %d error",
5338 			    sata_device->satadev_addr.cport);
5339 		else
5340 			sata_log(sata_hba_inst, CE_CONT,
5341 			    "SATA port %d pmport %d error\n",
5342 			    sata_device->satadev_addr.cport,
5343 			    sata_device->satadev_addr.pmport);
5344 
5345 		/*
5346 		 * Update the port/device structure.
5347 		 * sata_pkt should be still valid. Since port error is
5348 		 * returned, sata_device content should reflect port
5349 		 * state - it means, that sata address have been changed,
5350 		 * because original packet's sata address refered to a device
5351 		 * attached to some port.
5352 		 */
5353 		sata_update_port_info(sata_hba_inst, sata_device);
5354 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
5355 		*rval = TRAN_FATAL_ERROR;
5356 		break;
5357 
5358 	case SATA_TRAN_CMD_UNSUPPORTED:
5359 		/*
5360 		 * Command rejected by HBA as unsupported. It was HBA driver
5361 		 * that rejected the command, command was not sent to
5362 		 * an attached device.
5363 		 */
5364 		if ((sdinfo != NULL) &&
5365 		    (sdinfo->satadrv_state & SATA_DSTATE_RESET))
5366 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5367 			    "sat_hba_start: cmd 0x%2x rejected "
5368 			    "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd);
5369 
5370 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5371 		(void) sata_txlt_invalid_command(spx);
5372 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5373 
5374 		*rval = TRAN_ACCEPT;
5375 		break;
5376 
5377 	case SATA_TRAN_BUSY:
5378 		/*
5379 		 * Command rejected by HBA because other operation prevents
5380 		 * accepting the packet, or device is in RESET condition.
5381 		 */
5382 		if (sdinfo != NULL) {
5383 			sdinfo->satadrv_state =
5384 			    spx->txlt_sata_pkt->satapkt_device.satadev_state;
5385 
5386 			if (sdinfo->satadrv_state & SATA_DSTATE_RESET) {
5387 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5388 				    "sata_hba_start: cmd 0x%2x rejected "
5389 				    "because of device reset condition\n",
5390 				    cmd);
5391 			} else {
5392 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5393 				    "sata_hba_start: cmd 0x%2x rejected "
5394 				    "with SATA_TRAN_BUSY status\n",
5395 				    cmd);
5396 			}
5397 		}
5398 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
5399 		*rval = TRAN_BUSY;
5400 		break;
5401 
5402 	default:
5403 		/* Unrecognized HBA response */
5404 		SATA_LOG_D((sata_hba_inst, CE_WARN,
5405 		    "sata_hba_start: unrecognized HBA response "
5406 		    "to cmd : 0x%2x resp 0x%x", cmd, rval));
5407 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
5408 		*rval = TRAN_FATAL_ERROR;
5409 		break;
5410 	}
5411 
5412 	/*
5413 	 * If we got here, the packet was rejected.
5414 	 * Check if we need to remember reset state clearing request
5415 	 */
5416 	if (cmd_flags.sata_clear_dev_reset) {
5417 		/*
5418 		 * Check if device is still configured - it may have
5419 		 * disapeared from the configuration
5420 		 */
5421 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
5422 		if (sdinfo != NULL) {
5423 			/*
5424 			 * Restore the flag that requests clearing of
5425 			 * the device reset state,
5426 			 * so the next sata packet may carry it to HBA.
5427 			 */
5428 			sdinfo->satadrv_event_flags |=
5429 			    SATA_EVNT_CLEAR_DEVICE_RESET;
5430 		}
5431 	}
5432 	return (-1);
5433 }
5434 
5435 /*
5436  * Scsi response setup for invalid LBA
5437  *
5438  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
5439  */
5440 static int
5441 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx)
5442 {
5443 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5444 	struct scsi_extended_sense *sense;
5445 
5446 	scsipkt->pkt_reason = CMD_CMPLT;
5447 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5448 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5449 	*scsipkt->pkt_scbp = STATUS_CHECK;
5450 
5451 	*scsipkt->pkt_scbp = STATUS_CHECK;
5452 	sense = sata_arq_sense(spx);
5453 	sense->es_key = KEY_ILLEGAL_REQUEST;
5454 	sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
5455 
5456 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5457 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5458 
5459 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5460 	    scsipkt->pkt_comp != NULL)
5461 		/* scsi callback required */
5462 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5463 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5464 		    TQ_SLEEP) == NULL)
5465 			/* Scheduling the callback failed */
5466 			return (TRAN_BUSY);
5467 	return (TRAN_ACCEPT);
5468 }
5469 
5470 
5471 /*
5472  * Analyze device status and error registers and translate them into
5473  * appropriate scsi sense codes.
5474  * NOTE: non-packet commands only for now
5475  */
5476 static void
5477 sata_decode_device_error(sata_pkt_txlate_t *spx,
5478     struct scsi_extended_sense *sense)
5479 {
5480 	uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg;
5481 
5482 	ASSERT(sense != NULL);
5483 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
5484 	    SATA_STATUS_ERR);
5485 
5486 
5487 	if (err_reg & SATA_ERROR_ICRC) {
5488 		sense->es_key = KEY_ABORTED_COMMAND;
5489 		sense->es_add_code = 0x08; /* Communication failure */
5490 		return;
5491 	}
5492 
5493 	if (err_reg & SATA_ERROR_UNC) {
5494 		sense->es_key = KEY_MEDIUM_ERROR;
5495 		/* Information bytes (LBA) need to be set by a caller */
5496 		return;
5497 	}
5498 
5499 	/* ADD HERE: MC error bit handling for ATAPI CD/DVD */
5500 	if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) {
5501 		sense->es_key = KEY_UNIT_ATTENTION;
5502 		sense->es_add_code = 0x3a; /* No media present */
5503 		return;
5504 	}
5505 
5506 	if (err_reg & SATA_ERROR_IDNF) {
5507 		if (err_reg & SATA_ERROR_ABORT) {
5508 			sense->es_key = KEY_ABORTED_COMMAND;
5509 		} else {
5510 			sense->es_key = KEY_ILLEGAL_REQUEST;
5511 			sense->es_add_code = 0x21; /* LBA out of range */
5512 		}
5513 		return;
5514 	}
5515 
5516 	if (err_reg & SATA_ERROR_ABORT) {
5517 		ASSERT(spx->txlt_sata_pkt != NULL);
5518 		sense->es_key = KEY_ABORTED_COMMAND;
5519 		return;
5520 	}
5521 }
5522 
5523 /*
5524  * Extract error LBA from sata_pkt.satapkt_cmd register fields
5525  */
5526 static void
5527 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba)
5528 {
5529 	sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd;
5530 
5531 	*lba = 0;
5532 	if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) {
5533 		*lba = sata_cmd->satacmd_lba_high_msb;
5534 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb;
5535 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb;
5536 	} else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) {
5537 		*lba = sata_cmd->satacmd_device_reg & 0xf;
5538 	}
5539 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb;
5540 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb;
5541 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb;
5542 }
5543 
5544 /*
5545  * This is fixed sense format - if LBA exceeds the info field size,
5546  * no valid info will be returned (valid bit in extended sense will
5547  * be set to 0).
5548  */
5549 static struct scsi_extended_sense *
5550 sata_arq_sense(sata_pkt_txlate_t *spx)
5551 {
5552 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5553 	struct scsi_arq_status *arqs;
5554 	struct scsi_extended_sense *sense;
5555 
5556 	/* Fill ARQ sense data */
5557 	scsipkt->pkt_state |= STATE_ARQ_DONE;
5558 	arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp;
5559 	*(uchar_t *)&arqs->sts_status = STATUS_CHECK;
5560 	*(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD;
5561 	arqs->sts_rqpkt_reason = CMD_CMPLT;
5562 	arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5563 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
5564 	arqs->sts_rqpkt_resid = 0;
5565 	sense = &arqs->sts_sensedata;
5566 	bzero(sense, sizeof (struct scsi_extended_sense));
5567 	sata_fixed_sense_data_preset(sense);
5568 	return (sense);
5569 }
5570 
5571 
5572 /*
5573  * Emulated SATA Read/Write command completion for zero-length requests.
5574  * This request always succedes, so in synchronous mode it always returns
5575  * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the
5576  * callback cannot be scheduled.
5577  */
5578 static int
5579 sata_emul_rw_completion(sata_pkt_txlate_t *spx)
5580 {
5581 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5582 
5583 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5584 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5585 	scsipkt->pkt_reason = CMD_CMPLT;
5586 	*scsipkt->pkt_scbp = STATUS_GOOD;
5587 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5588 		/* scsi callback required - have to schedule it */
5589 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5590 		    (task_func_t *)scsipkt->pkt_comp,
5591 		    (void *)scsipkt, TQ_SLEEP) == NULL)
5592 			/* Scheduling the callback failed */
5593 			return (TRAN_BUSY);
5594 	}
5595 	return (TRAN_ACCEPT);
5596 }
5597 
5598 
5599 /*
5600  * Translate completion status of SATA read/write commands into scsi response.
5601  * pkt completion_reason is checked to determine the completion status.
5602  * Do scsi callback if necessary.
5603  *
5604  * Note: this function may be called also for synchronously executed
5605  * commands.
5606  * This function may be used only if scsi_pkt is non-NULL.
5607  */
5608 static void
5609 sata_txlt_rw_completion(sata_pkt_t *sata_pkt)
5610 {
5611 	sata_pkt_txlate_t *spx =
5612 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
5613 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
5614 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5615 	struct scsi_extended_sense *sense;
5616 	uint64_t lba;
5617 	struct buf *bp;
5618 	int rval;
5619 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5620 		/* Normal completion */
5621 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5622 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
5623 		scsipkt->pkt_reason = CMD_CMPLT;
5624 		*scsipkt->pkt_scbp = STATUS_GOOD;
5625 		if (spx->txlt_tmp_buf != NULL) {
5626 			/* Temporary buffer was used */
5627 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5628 			if (bp->b_flags & B_READ) {
5629 				rval = ddi_dma_sync(
5630 				    spx->txlt_buf_dma_handle, 0, 0,
5631 				    DDI_DMA_SYNC_FORCPU);
5632 				ASSERT(rval == DDI_SUCCESS);
5633 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
5634 				    bp->b_bcount);
5635 			}
5636 		}
5637 	} else {
5638 		/*
5639 		 * Something went wrong - analyze return
5640 		 */
5641 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5642 		    STATE_SENT_CMD | STATE_GOT_STATUS;
5643 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5644 		*scsipkt->pkt_scbp = STATUS_CHECK;
5645 		sense = sata_arq_sense(spx);
5646 		ASSERT(sense != NULL);
5647 
5648 		/*
5649 		 * SATA_PKT_DEV_ERROR is the only case where we may be able to
5650 		 * extract from device registers the failing LBA.
5651 		 */
5652 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
5653 			if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) &&
5654 			    (scmd->satacmd_lba_mid_msb != 0 ||
5655 			    scmd->satacmd_lba_high_msb != 0)) {
5656 				/*
5657 				 * We have problem reporting this cmd LBA
5658 				 * in fixed sense data format, because of
5659 				 * the size of the scsi LBA fields.
5660 				 */
5661 				sense->es_valid = 0;
5662 			} else {
5663 				sata_extract_error_lba(spx, &lba);
5664 				sense->es_info_1 = (lba & 0xFF000000) >> 24;
5665 				sense->es_info_2 = (lba & 0xFF0000) >> 16;
5666 				sense->es_info_3 = (lba & 0xFF00) >> 8;
5667 				sense->es_info_4 = lba & 0xFF;
5668 			}
5669 		} else {
5670 			/* Invalid extended sense info */
5671 			sense->es_valid = 0;
5672 		}
5673 
5674 		switch (sata_pkt->satapkt_reason) {
5675 		case SATA_PKT_PORT_ERROR:
5676 			/* We may want to handle DEV GONE state as well */
5677 			/*
5678 			 * We have no device data. Assume no data transfered.
5679 			 */
5680 			sense->es_key = KEY_HARDWARE_ERROR;
5681 			break;
5682 
5683 		case SATA_PKT_DEV_ERROR:
5684 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5685 			    SATA_STATUS_ERR) {
5686 				/*
5687 				 * determine dev error reason from error
5688 				 * reg content
5689 				 */
5690 				sata_decode_device_error(spx, sense);
5691 				if (sense->es_key == KEY_MEDIUM_ERROR) {
5692 					switch (scmd->satacmd_cmd_reg) {
5693 					case SATAC_READ_DMA:
5694 					case SATAC_READ_DMA_EXT:
5695 					case SATAC_READ_DMA_QUEUED:
5696 					case SATAC_READ_DMA_QUEUED_EXT:
5697 					case SATAC_READ_FPDMA_QUEUED:
5698 						/* Unrecovered read error */
5699 						sense->es_add_code =
5700 						    SD_SCSI_ASC_UNREC_READ_ERR;
5701 						break;
5702 					case SATAC_WRITE_DMA:
5703 					case SATAC_WRITE_DMA_EXT:
5704 					case SATAC_WRITE_DMA_QUEUED:
5705 					case SATAC_WRITE_DMA_QUEUED_EXT:
5706 					case SATAC_WRITE_FPDMA_QUEUED:
5707 						/* Write error */
5708 						sense->es_add_code =
5709 						    SD_SCSI_ASC_WRITE_ERR;
5710 						break;
5711 					default:
5712 						/* Internal error */
5713 						SATA_LOG_D((
5714 						    spx->txlt_sata_hba_inst,
5715 						    CE_WARN,
5716 						    "sata_txlt_rw_completion :"
5717 						    "internal error - invalid "
5718 						    "command 0x%2x",
5719 						    scmd->satacmd_cmd_reg));
5720 						break;
5721 					}
5722 				}
5723 				break;
5724 			}
5725 			/* No extended sense key - no info available */
5726 			scsipkt->pkt_reason = CMD_INCOMPLETE;
5727 			break;
5728 
5729 		case SATA_PKT_TIMEOUT:
5730 			scsipkt->pkt_reason = CMD_TIMEOUT;
5731 			scsipkt->pkt_statistics |=
5732 			    STAT_TIMEOUT | STAT_DEV_RESET;
5733 			sense->es_key = KEY_ABORTED_COMMAND;
5734 			break;
5735 
5736 		case SATA_PKT_ABORTED:
5737 			scsipkt->pkt_reason = CMD_ABORTED;
5738 			scsipkt->pkt_statistics |= STAT_ABORTED;
5739 			sense->es_key = KEY_ABORTED_COMMAND;
5740 			break;
5741 
5742 		case SATA_PKT_RESET:
5743 			scsipkt->pkt_reason = CMD_RESET;
5744 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5745 			sense->es_key = KEY_ABORTED_COMMAND;
5746 			break;
5747 
5748 		default:
5749 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5750 			    "sata_txlt_rw_completion: "
5751 			    "invalid packet completion reason"));
5752 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5753 			break;
5754 		}
5755 	}
5756 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5757 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5758 
5759 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5760 	    scsipkt->pkt_comp != NULL)
5761 		/* scsi callback required */
5762 		(*scsipkt->pkt_comp)(scsipkt);
5763 }
5764 
5765 
5766 /*
5767  * Translate completion status of non-data commands (i.e. commands returning
5768  * no data).
5769  * pkt completion_reason is checked to determine the completion status.
5770  * Do scsi callback if necessary (FLAG_NOINTR == 0)
5771  *
5772  * Note: this function may be called also for synchronously executed
5773  * commands.
5774  * This function may be used only if scsi_pkt is non-NULL.
5775  */
5776 
5777 static 	void
5778 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt)
5779 {
5780 	sata_pkt_txlate_t *spx =
5781 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
5782 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5783 	struct scsi_extended_sense *sense;
5784 
5785 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5786 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5787 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5788 		/* Normal completion */
5789 		scsipkt->pkt_reason = CMD_CMPLT;
5790 		*scsipkt->pkt_scbp = STATUS_GOOD;
5791 	} else {
5792 		/* Something went wrong */
5793 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5794 		*scsipkt->pkt_scbp = STATUS_CHECK;
5795 		sense = sata_arq_sense(spx);
5796 		switch (sata_pkt->satapkt_reason) {
5797 		case SATA_PKT_PORT_ERROR:
5798 			/*
5799 			 * We have no device data. Assume no data transfered.
5800 			 */
5801 			sense->es_key = KEY_HARDWARE_ERROR;
5802 			break;
5803 
5804 		case SATA_PKT_DEV_ERROR:
5805 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5806 			    SATA_STATUS_ERR) {
5807 				/*
5808 				 * determine dev error reason from error
5809 				 * reg content
5810 				 */
5811 				sata_decode_device_error(spx, sense);
5812 				break;
5813 			}
5814 			/* No extended sense key - no info available */
5815 			break;
5816 
5817 		case SATA_PKT_TIMEOUT:
5818 			scsipkt->pkt_reason = CMD_TIMEOUT;
5819 			scsipkt->pkt_statistics |=
5820 			    STAT_TIMEOUT | STAT_DEV_RESET;
5821 			/* No extended sense key ? */
5822 			break;
5823 
5824 		case SATA_PKT_ABORTED:
5825 			scsipkt->pkt_reason = CMD_ABORTED;
5826 			scsipkt->pkt_statistics |= STAT_ABORTED;
5827 			/* No extended sense key ? */
5828 			break;
5829 
5830 		case SATA_PKT_RESET:
5831 			/* pkt aborted by an explicit reset from a host */
5832 			scsipkt->pkt_reason = CMD_RESET;
5833 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5834 			break;
5835 
5836 		default:
5837 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5838 			    "sata_txlt_nodata_cmd_completion: "
5839 			    "invalid packet completion reason %d",
5840 			    sata_pkt->satapkt_reason));
5841 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5842 			break;
5843 		}
5844 
5845 	}
5846 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5847 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5848 
5849 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5850 	    scsipkt->pkt_comp != NULL)
5851 		/* scsi callback required */
5852 		(*scsipkt->pkt_comp)(scsipkt);
5853 }
5854 
5855 
5856 /*
5857  * Build Mode sense R/W recovery page
5858  * NOT IMPLEMENTED
5859  */
5860 
5861 static int
5862 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5863 {
5864 #ifndef __lock_lint
5865 	_NOTE(ARGUNUSED(sdinfo))
5866 	_NOTE(ARGUNUSED(pcntrl))
5867 	_NOTE(ARGUNUSED(buf))
5868 #endif
5869 	return (0);
5870 }
5871 
5872 /*
5873  * Build Mode sense caching page  -  scsi-3 implementation.
5874  * Page length distinguishes previous format from scsi-3 format.
5875  * buf must have space for 0x12 bytes.
5876  * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable.
5877  *
5878  */
5879 static int
5880 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5881 {
5882 	struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf;
5883 	sata_id_t *sata_id = &sdinfo->satadrv_id;
5884 
5885 	/*
5886 	 * Most of the fields are set to 0, being not supported and/or disabled
5887 	 */
5888 	bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3);
5889 
5890 	/* Saved paramters not supported */
5891 	if (pcntrl == 3)
5892 		return (0);
5893 	if (pcntrl == 0 || pcntrl == 2) {
5894 		/*
5895 		 * For now treat current and default parameters as same
5896 		 * That may have to change, if target driver will complain
5897 		 */
5898 		page->mode_page.code = MODEPAGE_CACHING;	/* PS = 0 */
5899 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
5900 
5901 		if ((sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) &&
5902 		    !(sata_id->ai_features85 & SATA_LOOK_AHEAD)) {
5903 			page->dra = 1;		/* Read Ahead disabled */
5904 			page->rcd = 1;		/* Read Cache disabled */
5905 		}
5906 		if ((sata_id->ai_cmdset82 & SATA_WRITE_CACHE) &&
5907 		    (sata_id->ai_features85 & SATA_WRITE_CACHE))
5908 			page->wce = 1;		/* Write Cache enabled */
5909 	} else {
5910 		/* Changeable parameters */
5911 		page->mode_page.code = MODEPAGE_CACHING;
5912 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
5913 		if (sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) {
5914 			page->dra = 1;
5915 			page->rcd = 1;
5916 		}
5917 		if (sata_id->ai_cmdset82 & SATA_WRITE_CACHE)
5918 			page->wce = 1;
5919 	}
5920 	return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
5921 	    sizeof (struct mode_page));
5922 }
5923 
5924 /*
5925  * Build Mode sense exception cntrl page
5926  */
5927 static int
5928 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5929 {
5930 	struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf;
5931 	sata_id_t *sata_id = &sdinfo->satadrv_id;
5932 
5933 	/*
5934 	 * Most of the fields are set to 0, being not supported and/or disabled
5935 	 */
5936 	bzero(buf, PAGELENGTH_INFO_EXCPT);
5937 
5938 	page->mode_page.code = MODEPAGE_INFO_EXCPT;
5939 	page->mode_page.length = PAGELENGTH_INFO_EXCPT;
5940 
5941 	/* Indicate that this is page is saveable */
5942 	page->mode_page.ps = 1;
5943 
5944 	/*
5945 	 * We will return the same data for default, current and saved page.
5946 	 * The only changeable bit is dexcpt and that bit is required
5947 	 * by the ATA specification to be preserved across power cycles.
5948 	 */
5949 	if (pcntrl != 1) {
5950 		page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED);
5951 		page->mrie = MRIE_ONLY_ON_REQUEST;
5952 	}
5953 	else
5954 		page->dexcpt = 1;	/* Only changeable parameter */
5955 
5956 	return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_info_excpt_page));
5957 }
5958 
5959 
5960 static int
5961 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5962 {
5963 	struct mode_acoustic_management *page =
5964 	    (struct mode_acoustic_management *)buf;
5965 	sata_id_t *sata_id = &sdinfo->satadrv_id;
5966 
5967 	/*
5968 	 * Most of the fields are set to 0, being not supported and/or disabled
5969 	 */
5970 	bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT);
5971 
5972 	switch (pcntrl) {
5973 	case P_CNTRL_DEFAULT:
5974 		/*  default paramters not supported */
5975 		return (0);
5976 
5977 	case P_CNTRL_CURRENT:
5978 	case P_CNTRL_SAVED:
5979 		/* Saved and current are supported and are identical */
5980 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
5981 		page->mode_page.length =
5982 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
5983 		page->mode_page.ps = 1;
5984 
5985 		/* Word 83 indicates if feature is supported */
5986 		/* If feature is not supported */
5987 		if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) {
5988 			page->acoustic_manag_enable =
5989 			    ACOUSTIC_DISABLED;
5990 		} else {
5991 			page->acoustic_manag_enable =
5992 			    ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT)
5993 			    != 0);
5994 			/* Word 94 inidicates the value */
5995 #ifdef	_LITTLE_ENDIAN
5996 			page->acoustic_manag_level =
5997 			    (uchar_t)sata_id->ai_acoustic;
5998 			page->vendor_recommended_value =
5999 			    sata_id->ai_acoustic >> 8;
6000 #else
6001 			page->acoustic_manag_level =
6002 			    sata_id->ai_acoustic >> 8;
6003 			page->vendor_recommended_value =
6004 			    (uchar_t)sata_id->ai_acoustic;
6005 #endif
6006 		}
6007 		break;
6008 
6009 	case P_CNTRL_CHANGEABLE:
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 the feature is supported */
6016 		if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) {
6017 			page->acoustic_manag_enable =
6018 			    ACOUSTIC_ENABLED;
6019 			page->acoustic_manag_level = 0xff;
6020 		}
6021 		break;
6022 	}
6023 	return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6024 	    sizeof (struct mode_page));
6025 }
6026 
6027 
6028 /*
6029  * Build Mode sense power condition page
6030  * NOT IMPLEMENTED.
6031  */
6032 static int
6033 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6034 {
6035 #ifndef __lock_lint
6036 	_NOTE(ARGUNUSED(sdinfo))
6037 	_NOTE(ARGUNUSED(pcntrl))
6038 	_NOTE(ARGUNUSED(buf))
6039 #endif
6040 	return (0);
6041 }
6042 
6043 
6044 /*
6045  * Process mode select caching page 8 (scsi3 format only).
6046  * Read Ahead (same as read cache) and Write Cache may be turned on and off
6047  * if these features are supported by the device. If these features are not
6048  * supported, quietly ignore them.
6049  * This function fails only if the SET FEATURE command sent to
6050  * the device fails. The page format is not varified, assuming that the
6051  * target driver operates correctly - if parameters length is too short,
6052  * we just drop the page.
6053  * Two command may be sent if both Read Cache/Read Ahead and Write Cache
6054  * setting have to be changed.
6055  * SET FEATURE command is executed synchronously, i.e. we wait here until
6056  * it is completed, regardless of the scsi pkt directives.
6057  *
6058  * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e.
6059  * changing DRA will change RCD.
6060  *
6061  * More than one SATA command may be executed to perform operations specified
6062  * by mode select pages. The first error terminates further execution.
6063  * Operations performed successully are not backed-up in such case.
6064  *
6065  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
6066  * If operation resulted in changing device setup, dmod flag should be set to
6067  * one (1). If parameters were not changed, dmod flag should be set to 0.
6068  * Upon return, if operation required sending command to the device, the rval
6069  * should be set to the value returned by sata_hba_start. If operation
6070  * did not require device access, rval should be set to TRAN_ACCEPT.
6071  * The pagelen should be set to the length of the page.
6072  *
6073  * This function has to be called with a port mutex held.
6074  *
6075  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6076  */
6077 int
6078 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page,
6079     int parmlen, int *pagelen, int *rval, int *dmod)
6080 {
6081 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6082 	sata_drive_info_t *sdinfo;
6083 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6084 	sata_id_t *sata_id;
6085 	struct scsi_extended_sense *sense;
6086 	int wce, dra;	/* Current settings */
6087 
6088 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6089 	    &spx->txlt_sata_pkt->satapkt_device);
6090 	sata_id = &sdinfo->satadrv_id;
6091 	*dmod = 0;
6092 
6093 	/* Verify parameters length. If too short, drop it */
6094 	if (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
6095 	    sizeof (struct mode_page) < parmlen) {
6096 		*scsipkt->pkt_scbp = STATUS_CHECK;
6097 		sense = sata_arq_sense(spx);
6098 		sense->es_key = KEY_ILLEGAL_REQUEST;
6099 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6100 		*pagelen = parmlen;
6101 		*rval = TRAN_ACCEPT;
6102 		return (SATA_FAILURE);
6103 	}
6104 
6105 	*pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page);
6106 
6107 	/*
6108 	 * We can manipulate only write cache and read ahead
6109 	 * (read cache) setting.
6110 	 */
6111 	if (!(sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) &&
6112 	    !(sata_id->ai_cmdset82 & SATA_WRITE_CACHE)) {
6113 		/*
6114 		 * None of the features is supported - ignore
6115 		 */
6116 		*rval = TRAN_ACCEPT;
6117 		return (SATA_SUCCESS);
6118 	}
6119 
6120 	/* Current setting of Read Ahead (and Read Cache) */
6121 	if (sata_id->ai_features85 & SATA_LOOK_AHEAD)
6122 		dra = 0;	/* 0 == not disabled */
6123 	else
6124 		dra = 1;
6125 	/* Current setting of Write Cache */
6126 	if (sata_id->ai_features85 & SATA_WRITE_CACHE)
6127 		wce = 1;
6128 	else
6129 		wce = 0;
6130 
6131 	if (page->dra == dra && page->wce == wce && page->rcd == dra) {
6132 		/* nothing to do */
6133 		*rval = TRAN_ACCEPT;
6134 		return (SATA_SUCCESS);
6135 	}
6136 
6137 	/*
6138 	 * Need to flip some setting
6139 	 * Set-up Internal SET FEATURES command(s)
6140 	 */
6141 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6142 	scmd->satacmd_addr_type = 0;
6143 	scmd->satacmd_device_reg = 0;
6144 	scmd->satacmd_status_reg = 0;
6145 	scmd->satacmd_error_reg = 0;
6146 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
6147 	if (page->dra != dra || page->rcd != dra) {
6148 		/* Need to flip read ahead setting */
6149 		if (dra == 0)
6150 			/* Disable read ahead / read cache */
6151 			scmd->satacmd_features_reg =
6152 			    SATAC_SF_DISABLE_READ_AHEAD;
6153 		else
6154 			/* Enable read ahead  / read cache */
6155 			scmd->satacmd_features_reg =
6156 			    SATAC_SF_ENABLE_READ_AHEAD;
6157 
6158 		/* Transfer command to HBA */
6159 		if (sata_hba_start(spx, rval) != 0)
6160 			/*
6161 			 * Pkt not accepted for execution.
6162 			 */
6163 			return (SATA_FAILURE);
6164 
6165 		*dmod = 1;
6166 
6167 		/* Now process return */
6168 		if (spx->txlt_sata_pkt->satapkt_reason !=
6169 		    SATA_PKT_COMPLETED) {
6170 			goto failure;	/* Terminate */
6171 		}
6172 	}
6173 
6174 	/* Note that the packet is not removed, so it could be re-used */
6175 	if (page->wce != wce) {
6176 		/* Need to flip Write Cache setting */
6177 		if (page->wce == 1)
6178 			/* Enable write cache */
6179 			scmd->satacmd_features_reg =
6180 			    SATAC_SF_ENABLE_WRITE_CACHE;
6181 		else
6182 			/* Disable write cache */
6183 			scmd->satacmd_features_reg =
6184 			    SATAC_SF_DISABLE_WRITE_CACHE;
6185 
6186 		/* Transfer command to HBA */
6187 		if (sata_hba_start(spx, rval) != 0)
6188 			/*
6189 			 * Pkt not accepted for execution.
6190 			 */
6191 			return (SATA_FAILURE);
6192 
6193 		*dmod = 1;
6194 
6195 		/* Now process return */
6196 		if (spx->txlt_sata_pkt->satapkt_reason !=
6197 		    SATA_PKT_COMPLETED) {
6198 			goto failure;
6199 		}
6200 	}
6201 	return (SATA_SUCCESS);
6202 
6203 failure:
6204 	sata_xlate_errors(spx);
6205 
6206 	return (SATA_FAILURE);
6207 }
6208 
6209 /*
6210  * Process mode select informational exceptions control page 0x1c
6211  *
6212  * The only changeable bit is dexcpt (disable exceptions).
6213  * MRIE (method of reporting informational exceptions) must be
6214  * "only on request".
6215  * This page applies to informational exceptions that report
6216  * additional sense codes with the ADDITIONAL SENSE CODE field set to 5Dh
6217  * (e.g.,FAILURE PREDICTION THRESHOLD EXCEEDED) or 0Bh (e.g., WARNING_).
6218  * Informational exception conditions occur as the result of background scan
6219  * errors, background self-test errors, or vendor specific events within a
6220  * logical unit. An informational exception condition may occur asynchronous
6221  * to any commands.
6222  *
6223  * Returns: SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
6224  * If operation resulted in changing device setup, dmod flag should be set to
6225  * one (1). If parameters were not changed, dmod flag should be set to 0.
6226  * Upon return, if operation required sending command to the device, the rval
6227  * should be set to the value returned by sata_hba_start. If operation
6228  * did not require device access, rval should be set to TRAN_ACCEPT.
6229  * The pagelen should be set to the length of the page.
6230  *
6231  * This function has to be called with a port mutex held.
6232  *
6233  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6234  *
6235  * Cannot be called in the interrupt context.
6236  */
6237 static	int
6238 sata_mode_select_page_1c(
6239 	sata_pkt_txlate_t *spx,
6240 	struct mode_info_excpt_page *page,
6241 	int parmlen,
6242 	int *pagelen,
6243 	int *rval,
6244 	int *dmod)
6245 {
6246 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6247 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6248 	sata_drive_info_t *sdinfo;
6249 	sata_id_t *sata_id;
6250 	struct scsi_extended_sense *sense;
6251 
6252 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6253 	    &spx->txlt_sata_pkt->satapkt_device);
6254 	sata_id = &sdinfo->satadrv_id;
6255 
6256 	*dmod = 0;
6257 
6258 	/* Verify parameters length. If too short, drop it */
6259 	if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) < parmlen) ||
6260 	    page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) {
6261 		*scsipkt->pkt_scbp = STATUS_CHECK;
6262 		sense = sata_arq_sense(spx);
6263 		sense->es_key = KEY_ILLEGAL_REQUEST;
6264 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6265 		*pagelen = parmlen;
6266 		*rval = TRAN_ACCEPT;
6267 		return (SATA_FAILURE);
6268 	}
6269 
6270 	*pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page);
6271 
6272 	if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
6273 		*scsipkt->pkt_scbp = STATUS_CHECK;
6274 		sense = sata_arq_sense(spx);
6275 		sense->es_key = KEY_ILLEGAL_REQUEST;
6276 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6277 		*pagelen = parmlen;
6278 		*rval = TRAN_ACCEPT;
6279 		return (SATA_FAILURE);
6280 	}
6281 
6282 	/* If already in the state requested, we are done */
6283 	if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
6284 		/* nothing to do */
6285 		*rval = TRAN_ACCEPT;
6286 		return (SATA_SUCCESS);
6287 	}
6288 
6289 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6290 
6291 	/* Build SMART_ENABLE or SMART_DISABLE command */
6292 	scmd->satacmd_addr_type = 0;		/* N/A */
6293 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
6294 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
6295 	scmd->satacmd_features_reg = page->dexcpt ?
6296 	    SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS;
6297 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
6298 	scmd->satacmd_cmd_reg = SATAC_SMART;
6299 
6300 	/* Transfer command to HBA */
6301 	if (sata_hba_start(spx, rval) != 0)
6302 		/*
6303 		 * Pkt not accepted for execution.
6304 		 */
6305 		return (SATA_FAILURE);
6306 
6307 	*dmod = 1;	/* At least may have been modified */
6308 
6309 	/* Now process return */
6310 	if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED)
6311 		return (SATA_SUCCESS);
6312 
6313 	/* Packet did not complete successfully */
6314 	sata_xlate_errors(spx);
6315 
6316 	return (SATA_FAILURE);
6317 }
6318 
6319 /*
6320  * Process mode select acoustic management control page 0x30
6321  *
6322  *
6323  * This function has to be called with a port mutex held.
6324  *
6325  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6326  *
6327  * Cannot be called in the interrupt context.
6328  */
6329 int
6330 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct
6331     mode_acoustic_management *page, int parmlen, int *pagelen,
6332     int *rval, int *dmod)
6333 {
6334 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6335 	sata_drive_info_t *sdinfo;
6336 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6337 	sata_id_t *sata_id;
6338 	struct scsi_extended_sense *sense;
6339 
6340 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6341 	    &spx->txlt_sata_pkt->satapkt_device);
6342 	sata_id = &sdinfo->satadrv_id;
6343 	*dmod = 0;
6344 
6345 	/* If parmlen is too short or the feature is not supported, drop it */
6346 	if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6347 	    sizeof (struct mode_page)) < parmlen) ||
6348 	    (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) {
6349 		*scsipkt->pkt_scbp = STATUS_CHECK;
6350 		sense = sata_arq_sense(spx);
6351 		sense->es_key = KEY_ILLEGAL_REQUEST;
6352 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6353 		*pagelen = parmlen;
6354 		*rval = TRAN_ACCEPT;
6355 		return (SATA_FAILURE);
6356 	}
6357 
6358 	*pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6359 	    sizeof (struct mode_page);
6360 
6361 	/*
6362 	 * We can enable and disable acoustice management and
6363 	 * set the acoustic management level.
6364 	 */
6365 
6366 	/*
6367 	 * Set-up Internal SET FEATURES command(s)
6368 	 */
6369 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6370 	scmd->satacmd_addr_type = 0;
6371 	scmd->satacmd_device_reg = 0;
6372 	scmd->satacmd_status_reg = 0;
6373 	scmd->satacmd_error_reg = 0;
6374 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
6375 	if (page->acoustic_manag_enable) {
6376 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC;
6377 		scmd->satacmd_sec_count_lsb = page->acoustic_manag_level;
6378 	} else {	/* disabling acoustic management */
6379 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC;
6380 	}
6381 
6382 	/* Transfer command to HBA */
6383 	if (sata_hba_start(spx, rval) != 0)
6384 		/*
6385 		 * Pkt not accepted for execution.
6386 		 */
6387 		return (SATA_FAILURE);
6388 
6389 	/* Now process return */
6390 	if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) {
6391 		sata_xlate_errors(spx);
6392 		return (SATA_FAILURE);
6393 	}
6394 
6395 	*dmod = 1;
6396 
6397 	return (SATA_SUCCESS);
6398 }
6399 
6400 
6401 
6402 
6403 /*
6404  * sata_build_lsense_page0() is used to create the
6405  * SCSI LOG SENSE page 0 (supported log pages)
6406  *
6407  * Currently supported pages are 0, 0x10, 0x2f and 0x30
6408  * (supported log pages, self-test results, informational exceptions
6409  *  and Sun vendor specific ATA SMART data).
6410  *
6411  * Takes a sata_drive_info t * and the address of a buffer
6412  * in which to create the page information.
6413  *
6414  * Returns the number of bytes valid in the buffer.
6415  */
6416 static	int
6417 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf)
6418 {
6419 	struct log_parameter *lpp = (struct log_parameter *)buf;
6420 	uint8_t *page_ptr = (uint8_t *)lpp->param_values;
6421 	int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */
6422 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6423 
6424 	lpp->param_code[0] = 0;
6425 	lpp->param_code[1] = 0;
6426 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
6427 	*page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES;
6428 
6429 	if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
6430 		if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) {
6431 			*page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS;
6432 			++num_pages_supported;
6433 		}
6434 		*page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS;
6435 		++num_pages_supported;
6436 		*page_ptr++ = PAGE_CODE_SMART_READ_DATA;
6437 		++num_pages_supported;
6438 	}
6439 
6440 	lpp->param_len = num_pages_supported;
6441 
6442 	return ((&lpp->param_values[0] - (uint8_t *)lpp) +
6443 	    num_pages_supported);
6444 }
6445 
6446 /*
6447  * sata_build_lsense_page_10() is used to create the
6448  * SCSI LOG SENSE page 0x10 (self-test results)
6449  *
6450  * Takes a sata_drive_info t * and the address of a buffer
6451  * in which to create the page information as well as a sata_hba_inst_t *.
6452  *
6453  * Returns the number of bytes valid in the buffer.
6454  *
6455  * Note: Self test and SMART data is accessible in device log pages.
6456  * The log pages can be accessed by SMART READ/WRITE LOG (up to 255 sectors
6457  * of data can be transferred by a single command), or by the General Purpose
6458  * Logging commands (GPL) READ LOG EXT and WRITE LOG EXT (up to 65,535 sectors
6459  * - approximately 33MB - can be transferred by a single command.
6460  * The SCT Command response (either error or command) is the same for both
6461  * the SMART and GPL methods of issuing commands.
6462  * This function uses READ LOG EXT command when drive supports LBA48, and
6463  * SMART READ command otherwise.
6464  *
6465  * Since above commands are executed in a synchronous mode, this function
6466  * should not be called in an interrupt context.
6467  */
6468 static	int
6469 sata_build_lsense_page_10(
6470 	sata_drive_info_t *sdinfo,
6471 	uint8_t *buf,
6472 	sata_hba_inst_t *sata_hba_inst)
6473 {
6474 	struct log_parameter *lpp = (struct log_parameter *)buf;
6475 	int rval;
6476 
6477 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6478 		struct smart_ext_selftest_log *ext_selftest_log;
6479 
6480 		ext_selftest_log = kmem_zalloc(
6481 		    sizeof (struct smart_ext_selftest_log), KM_SLEEP);
6482 
6483 		rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo,
6484 		    ext_selftest_log, 0);
6485 		if (rval == 0) {
6486 			int index, start_index;
6487 			struct smart_ext_selftest_log_entry *entry;
6488 			static const struct smart_ext_selftest_log_entry empty =
6489 			    {0};
6490 			uint16_t block_num;
6491 			int count;
6492 			boolean_t only_one_block = B_FALSE;
6493 
6494 			index = ext_selftest_log->
6495 			    smart_ext_selftest_log_index[0];
6496 			index |= ext_selftest_log->
6497 			    smart_ext_selftest_log_index[1] << 8;
6498 			if (index == 0)
6499 				goto out;
6500 
6501 			--index;	/* Correct for 0 origin */
6502 			start_index = index;	/* remember where we started */
6503 			block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6504 			if (block_num != 0) {
6505 				rval = sata_ext_smart_selftest_read_log(
6506 				    sata_hba_inst, sdinfo, ext_selftest_log,
6507 				    block_num);
6508 				if (rval != 0)
6509 					goto out;
6510 			}
6511 			index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6512 			entry =
6513 			    &ext_selftest_log->
6514 			    smart_ext_selftest_log_entries[index];
6515 
6516 			for (count = 1;
6517 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
6518 			    ++count) {
6519 				uint8_t status;
6520 				uint8_t code;
6521 				uint8_t sense_key;
6522 				uint8_t add_sense_code;
6523 				uint8_t add_sense_code_qual;
6524 
6525 				/* If this is an unused entry, we are done */
6526 				if (bcmp(entry, &empty, sizeof (empty)) == 0) {
6527 					/* Broken firmware on some disks */
6528 					if (index + 1 ==
6529 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK) {
6530 						--entry;
6531 						--index;
6532 						if (bcmp(entry, &empty,
6533 						    sizeof (empty)) == 0)
6534 							goto out;
6535 					} else
6536 						goto out;
6537 				}
6538 
6539 				if (only_one_block &&
6540 				    start_index == index)
6541 					goto out;
6542 
6543 				lpp->param_code[0] = 0;
6544 				lpp->param_code[1] = count;
6545 				lpp->param_ctrl_flags =
6546 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
6547 				lpp->param_len =
6548 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
6549 
6550 				status = entry->smart_ext_selftest_log_status;
6551 				status >>= 4;
6552 				switch (status) {
6553 				case 0:
6554 				default:
6555 					sense_key = KEY_NO_SENSE;
6556 					add_sense_code =
6557 					    SD_SCSI_ASC_NO_ADD_SENSE;
6558 					add_sense_code_qual = 0;
6559 					break;
6560 				case 1:
6561 					sense_key = KEY_ABORTED_COMMAND;
6562 					add_sense_code =
6563 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6564 					add_sense_code_qual = SCSI_COMPONENT_81;
6565 					break;
6566 				case 2:
6567 					sense_key = KEY_ABORTED_COMMAND;
6568 					add_sense_code =
6569 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6570 					add_sense_code_qual = SCSI_COMPONENT_82;
6571 					break;
6572 				case 3:
6573 					sense_key = KEY_ABORTED_COMMAND;
6574 					add_sense_code =
6575 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6576 					add_sense_code_qual = SCSI_COMPONENT_83;
6577 					break;
6578 				case 4:
6579 					sense_key = KEY_HARDWARE_ERROR;
6580 					add_sense_code =
6581 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6582 					add_sense_code_qual = SCSI_COMPONENT_84;
6583 					break;
6584 				case 5:
6585 					sense_key = KEY_HARDWARE_ERROR;
6586 					add_sense_code =
6587 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6588 					add_sense_code_qual = SCSI_COMPONENT_85;
6589 					break;
6590 				case 6:
6591 					sense_key = KEY_HARDWARE_ERROR;
6592 					add_sense_code =
6593 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6594 					add_sense_code_qual = SCSI_COMPONENT_86;
6595 					break;
6596 				case 7:
6597 					sense_key = KEY_MEDIUM_ERROR;
6598 					add_sense_code =
6599 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6600 					add_sense_code_qual = SCSI_COMPONENT_87;
6601 					break;
6602 				case 8:
6603 					sense_key = KEY_HARDWARE_ERROR;
6604 					add_sense_code =
6605 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6606 					add_sense_code_qual = SCSI_COMPONENT_88;
6607 					break;
6608 				}
6609 				code = 0;	/* unspecified */
6610 				status |= (code << 4);
6611 				lpp->param_values[0] = status;
6612 				lpp->param_values[1] = 0; /* unspecified */
6613 				lpp->param_values[2] = entry->
6614 				    smart_ext_selftest_log_timestamp[1];
6615 				lpp->param_values[3] = entry->
6616 				    smart_ext_selftest_log_timestamp[0];
6617 				if (status != 0) {
6618 					lpp->param_values[4] = 0;
6619 					lpp->param_values[5] = 0;
6620 					lpp->param_values[6] = entry->
6621 					    smart_ext_selftest_log_failing_lba
6622 					    [5];
6623 					lpp->param_values[7] = entry->
6624 					    smart_ext_selftest_log_failing_lba
6625 					    [4];
6626 					lpp->param_values[8] = entry->
6627 					    smart_ext_selftest_log_failing_lba
6628 					    [3];
6629 					lpp->param_values[9] = entry->
6630 					    smart_ext_selftest_log_failing_lba
6631 					    [2];
6632 					lpp->param_values[10] = entry->
6633 					    smart_ext_selftest_log_failing_lba
6634 					    [1];
6635 					lpp->param_values[11] = entry->
6636 					    smart_ext_selftest_log_failing_lba
6637 					    [0];
6638 				} else {	/* No bad block address */
6639 					lpp->param_values[4] = 0xff;
6640 					lpp->param_values[5] = 0xff;
6641 					lpp->param_values[6] = 0xff;
6642 					lpp->param_values[7] = 0xff;
6643 					lpp->param_values[8] = 0xff;
6644 					lpp->param_values[9] = 0xff;
6645 					lpp->param_values[10] = 0xff;
6646 					lpp->param_values[11] = 0xff;
6647 				}
6648 
6649 				lpp->param_values[12] = sense_key;
6650 				lpp->param_values[13] = add_sense_code;
6651 				lpp->param_values[14] = add_sense_code_qual;
6652 				lpp->param_values[15] = 0; /* undefined */
6653 
6654 				lpp = (struct log_parameter *)
6655 				    (((uint8_t *)lpp) +
6656 				    SCSI_LOG_PARAM_HDR_LEN +
6657 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
6658 
6659 				--index;	/* Back up to previous entry */
6660 				if (index < 0) {
6661 					if (block_num > 0) {
6662 						--block_num;
6663 					} else {
6664 						struct read_log_ext_directory
6665 						    logdir;
6666 
6667 						rval =
6668 						    sata_read_log_ext_directory(
6669 						    sata_hba_inst, sdinfo,
6670 						    &logdir);
6671 						if (rval == -1)
6672 							goto out;
6673 						if ((logdir.read_log_ext_vers
6674 						    [0] == 0) &&
6675 						    (logdir.read_log_ext_vers
6676 						    [1] == 0))
6677 							goto out;
6678 						block_num =
6679 						    logdir.read_log_ext_nblks
6680 						    [EXT_SMART_SELFTEST_LOG_PAGE
6681 						    - 1][0];
6682 						block_num |= logdir.
6683 						    read_log_ext_nblks
6684 						    [EXT_SMART_SELFTEST_LOG_PAGE
6685 						    - 1][1] << 8;
6686 						--block_num;
6687 						only_one_block =
6688 						    (block_num == 0);
6689 					}
6690 					rval = sata_ext_smart_selftest_read_log(
6691 					    sata_hba_inst, sdinfo,
6692 					    ext_selftest_log, block_num);
6693 					if (rval != 0)
6694 						goto out;
6695 
6696 					index =
6697 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK -
6698 					    1;
6699 				}
6700 				index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6701 				entry = &ext_selftest_log->
6702 				    smart_ext_selftest_log_entries[index];
6703 			}
6704 		}
6705 out:
6706 		kmem_free(ext_selftest_log,
6707 		    sizeof (struct smart_ext_selftest_log));
6708 	} else {
6709 		struct smart_selftest_log *selftest_log;
6710 
6711 		selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log),
6712 		    KM_SLEEP);
6713 
6714 		rval = sata_smart_selftest_log(sata_hba_inst, sdinfo,
6715 		    selftest_log);
6716 
6717 		if (rval == 0) {
6718 			int index;
6719 			int count;
6720 			struct smart_selftest_log_entry *entry;
6721 			static const struct smart_selftest_log_entry empty =
6722 			    { 0 };
6723 
6724 			index = selftest_log->smart_selftest_log_index;
6725 			if (index == 0)
6726 				goto done;
6727 			--index;	/* Correct for 0 origin */
6728 			entry = &selftest_log->
6729 			    smart_selftest_log_entries[index];
6730 			for (count = 1;
6731 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
6732 			    ++count) {
6733 				uint8_t status;
6734 				uint8_t code;
6735 				uint8_t sense_key;
6736 				uint8_t add_sense_code;
6737 				uint8_t add_sense_code_qual;
6738 
6739 				if (bcmp(entry, &empty, sizeof (empty)) == 0)
6740 					goto done;
6741 
6742 				lpp->param_code[0] = 0;
6743 				lpp->param_code[1] = count;
6744 				lpp->param_ctrl_flags =
6745 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
6746 				lpp->param_len =
6747 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
6748 
6749 				status = entry->smart_selftest_log_status;
6750 				status >>= 4;
6751 				switch (status) {
6752 				case 0:
6753 				default:
6754 					sense_key = KEY_NO_SENSE;
6755 					add_sense_code =
6756 					    SD_SCSI_ASC_NO_ADD_SENSE;
6757 					break;
6758 				case 1:
6759 					sense_key = KEY_ABORTED_COMMAND;
6760 					add_sense_code =
6761 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6762 					add_sense_code_qual = SCSI_COMPONENT_81;
6763 					break;
6764 				case 2:
6765 					sense_key = KEY_ABORTED_COMMAND;
6766 					add_sense_code =
6767 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6768 					add_sense_code_qual = SCSI_COMPONENT_82;
6769 					break;
6770 				case 3:
6771 					sense_key = KEY_ABORTED_COMMAND;
6772 					add_sense_code =
6773 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6774 					add_sense_code_qual = SCSI_COMPONENT_83;
6775 					break;
6776 				case 4:
6777 					sense_key = KEY_HARDWARE_ERROR;
6778 					add_sense_code =
6779 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6780 					add_sense_code_qual = SCSI_COMPONENT_84;
6781 					break;
6782 				case 5:
6783 					sense_key = KEY_HARDWARE_ERROR;
6784 					add_sense_code =
6785 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6786 					add_sense_code_qual = SCSI_COMPONENT_85;
6787 					break;
6788 				case 6:
6789 					sense_key = KEY_HARDWARE_ERROR;
6790 					add_sense_code =
6791 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6792 					add_sense_code_qual = SCSI_COMPONENT_86;
6793 					break;
6794 				case 7:
6795 					sense_key = KEY_MEDIUM_ERROR;
6796 					add_sense_code =
6797 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6798 					add_sense_code_qual = SCSI_COMPONENT_87;
6799 					break;
6800 				case 8:
6801 					sense_key = KEY_HARDWARE_ERROR;
6802 					add_sense_code =
6803 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6804 					add_sense_code_qual = SCSI_COMPONENT_88;
6805 					break;
6806 				}
6807 				code = 0;	/* unspecified */
6808 				status |= (code << 4);
6809 				lpp->param_values[0] = status;
6810 				lpp->param_values[1] = 0; /* unspecified */
6811 				lpp->param_values[2] = entry->
6812 				    smart_selftest_log_timestamp[1];
6813 				lpp->param_values[3] = entry->
6814 				    smart_selftest_log_timestamp[0];
6815 				if (status != 0) {
6816 					lpp->param_values[4] = 0;
6817 					lpp->param_values[5] = 0;
6818 					lpp->param_values[6] = 0;
6819 					lpp->param_values[7] = 0;
6820 					lpp->param_values[8] = entry->
6821 					    smart_selftest_log_failing_lba[3];
6822 					lpp->param_values[9] = entry->
6823 					    smart_selftest_log_failing_lba[2];
6824 					lpp->param_values[10] = entry->
6825 					    smart_selftest_log_failing_lba[1];
6826 					lpp->param_values[11] = entry->
6827 					    smart_selftest_log_failing_lba[0];
6828 				} else {	/* No block address */
6829 					lpp->param_values[4] = 0xff;
6830 					lpp->param_values[5] = 0xff;
6831 					lpp->param_values[6] = 0xff;
6832 					lpp->param_values[7] = 0xff;
6833 					lpp->param_values[8] = 0xff;
6834 					lpp->param_values[9] = 0xff;
6835 					lpp->param_values[10] = 0xff;
6836 					lpp->param_values[11] = 0xff;
6837 				}
6838 				lpp->param_values[12] = sense_key;
6839 				lpp->param_values[13] = add_sense_code;
6840 				lpp->param_values[14] = add_sense_code_qual;
6841 				lpp->param_values[15] = 0; /* undefined */
6842 
6843 				lpp = (struct log_parameter *)
6844 				    (((uint8_t *)lpp) +
6845 				    SCSI_LOG_PARAM_HDR_LEN +
6846 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
6847 				--index;	/* back up to previous entry */
6848 				if (index < 0) {
6849 					index =
6850 					    NUM_SMART_SELFTEST_LOG_ENTRIES - 1;
6851 				}
6852 				entry = &selftest_log->
6853 				    smart_selftest_log_entries[index];
6854 			}
6855 		}
6856 done:
6857 		kmem_free(selftest_log, sizeof (struct smart_selftest_log));
6858 	}
6859 
6860 	return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) *
6861 	    SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS);
6862 }
6863 
6864 /*
6865  * sata_build_lsense_page_2f() is used to create the
6866  * SCSI LOG SENSE page 0x2f (informational exceptions)
6867  *
6868  * Takes a sata_drive_info t * and the address of a buffer
6869  * in which to create the page information as well as a sata_hba_inst_t *.
6870  *
6871  * Returns the number of bytes valid in the buffer.
6872  *
6873  * Because it invokes function(s) that send synchronously executed command
6874  * to the HBA, it cannot be called in the interrupt context.
6875  */
6876 static	int
6877 sata_build_lsense_page_2f(
6878 	sata_drive_info_t *sdinfo,
6879 	uint8_t *buf,
6880 	sata_hba_inst_t *sata_hba_inst)
6881 {
6882 	struct log_parameter *lpp = (struct log_parameter *)buf;
6883 	int rval;
6884 	uint8_t *smart_data;
6885 	uint8_t temp;
6886 	sata_id_t *sata_id;
6887 #define	SMART_NO_TEMP	0xff
6888 
6889 	lpp->param_code[0] = 0;
6890 	lpp->param_code[1] = 0;
6891 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
6892 
6893 	/* Now get the SMART status w.r.t. threshold exceeded */
6894 	rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo);
6895 	switch (rval) {
6896 	case 1:
6897 		lpp->param_values[0] = SCSI_PREDICTED_FAILURE;
6898 		lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE;
6899 		break;
6900 	case 0:
6901 	case -1:	/* failed to get data */
6902 		lpp->param_values[0] = 0;	/* No failure predicted */
6903 		lpp->param_values[1] = 0;
6904 		break;
6905 #if defined(SATA_DEBUG)
6906 	default:
6907 		cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value");
6908 		/* NOTREACHED */
6909 #endif
6910 	}
6911 
6912 	sata_id = &sdinfo->satadrv_id;
6913 	if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP))
6914 		temp = SMART_NO_TEMP;
6915 	else {
6916 		/* Now get the temperature */
6917 		smart_data = kmem_zalloc(512, KM_SLEEP);
6918 		rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data,
6919 		    SCT_STATUS_LOG_PAGE, 1);
6920 		if (rval == -1)
6921 			temp = SMART_NO_TEMP;
6922 		else {
6923 			temp = smart_data[200];
6924 			if (temp & 0x80) {
6925 				if (temp & 0x7f)
6926 					temp = 0;
6927 				else
6928 					temp = SMART_NO_TEMP;
6929 			}
6930 		}
6931 		kmem_free(smart_data, 512);
6932 	}
6933 
6934 	lpp->param_values[2] = temp;	/* most recent temperature */
6935 	lpp->param_values[3] = 0;	/* required vendor specific byte */
6936 
6937 	lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN;
6938 
6939 
6940 	return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN);
6941 }
6942 
6943 /*
6944  * sata_build_lsense_page_30() is used to create the
6945  * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data).
6946  *
6947  * Takes a sata_drive_info t * and the address of a buffer
6948  * in which to create the page information as well as a sata_hba_inst_t *.
6949  *
6950  * Returns the number of bytes valid in the buffer.
6951  */
6952 static int
6953 sata_build_lsense_page_30(
6954 	sata_drive_info_t *sdinfo,
6955 	uint8_t *buf,
6956 	sata_hba_inst_t *sata_hba_inst)
6957 {
6958 	struct smart_data *smart_data = (struct smart_data *)buf;
6959 	int rval;
6960 
6961 	/* Now do the SMART READ DATA */
6962 	rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data);
6963 	if (rval == -1)
6964 		return (0);
6965 
6966 	return (sizeof (struct smart_data));
6967 }
6968 
6969 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */
6970 
6971 /*
6972  * Start command for ATAPI device.
6973  * This function processes scsi_pkt requests.
6974  * Only CD/DVD devices are supported.
6975  * Most commands are packet without any translation into Packet Command.
6976  * Some may be trapped and executed as SATA commands (not clear which one).
6977  *
6978  * Returns TRAN_ACCEPT if command is accepted for execution (or completed
6979  * execution).
6980  * Returns other TRAN_XXXX codes if command is not accepted or completed
6981  * (see return values for sata_hba_start()).
6982  *
6983  * Note:
6984  * Inquiry cdb format differs between transport version 2 and 3.
6985  * However, the transport version 3 devices that were checked did not adhere
6986  * to the specification (ignored MSB of the allocation length). Therefore,
6987  * the transport version is not checked, but Inquiry allocation length is
6988  * truncated to 255 bytes if the original allocation length set-up by the
6989  * target driver is greater than 255 bytes.
6990  */
6991 static int
6992 sata_txlt_atapi(sata_pkt_txlate_t *spx)
6993 {
6994 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6995 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6996 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
6997 	sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx);
6998 	sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba,
6999 	    &spx->txlt_sata_pkt->satapkt_device);
7000 	int cport = SATA_TXLT_CPORT(spx);
7001 	int cdblen;
7002 	int rval, reason;
7003 	int synch;
7004 	union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp;
7005 
7006 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
7007 
7008 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
7009 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
7010 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7011 		return (rval);
7012 	}
7013 
7014 	/*
7015 	 * ATAPI device executes some ATA commands in addition to MMC command
7016 	 * set. These ATA commands may be executed by the regular SATA
7017 	 * translation functions. None needs to be captured now.
7018 	 * Other commands belong to MMC command set and are delivered
7019 	 * to ATAPI device via Packet Command.
7020 	 */
7021 
7022 	/* Check the size of cdb */
7023 	cdblen = scsi_cdb_size[GETGROUP(cdbp)];
7024 	if (cdblen > sdinfo->satadrv_atapi_cdb_len) {
7025 		sata_log(NULL, CE_WARN,
7026 		    "sata: invalid ATAPI cdb length %d",
7027 		    scsipkt->pkt_cdblen);
7028 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7029 		return (TRAN_BADPKT);
7030 	}
7031 
7032 	SATAATAPITRACE(spx, cdblen);
7033 
7034 	/*
7035 	 * For non-read/write commands we need to
7036 	 * map buffer
7037 	 */
7038 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
7039 	case SCMD_READ:
7040 	case SCMD_READ_G1:
7041 	case SCMD_READ_G5:
7042 	case SCMD_READ_G4:
7043 	case SCMD_WRITE:
7044 	case SCMD_WRITE_G1:
7045 	case SCMD_WRITE_G5:
7046 	case SCMD_WRITE_G4:
7047 		break;
7048 	default:
7049 		if (bp != NULL) {
7050 			if (bp->b_flags & (B_PHYS | B_PAGEIO))
7051 				bp_mapin(bp);
7052 		}
7053 		break;
7054 	}
7055 	/*
7056 	 * scmd->satacmd_flags.sata_data_direction default -
7057 	 * SATA_DIR_NODATA_XFER - is set by
7058 	 * sata_txlt_generic_pkt_info().
7059 	 */
7060 	if (scmd->satacmd_bp) {
7061 		if (scmd->satacmd_bp->b_flags & B_READ) {
7062 			scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7063 		} else {
7064 			scmd->satacmd_flags.sata_data_direction =
7065 			    SATA_DIR_WRITE;
7066 		}
7067 	}
7068 
7069 	/*
7070 	 * Set up ATAPI packet command.
7071 	 */
7072 
7073 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7074 
7075 	/* Copy cdb into sata_cmd */
7076 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7077 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7078 	bcopy(cdbp, scmd->satacmd_acdb, cdblen);
7079 
7080 	/* See note in the command header */
7081 	if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) {
7082 		if (scmd->satacmd_acdb[3] != 0)
7083 			scmd->satacmd_acdb[4] = 255;
7084 	}
7085 
7086 #ifdef SATA_DEBUG
7087 	if (sata_debug_flags & SATA_DBG_ATAPI) {
7088 		uint8_t *p = scmd->satacmd_acdb;
7089 		char buf[3 * SATA_ATAPI_MAX_CDB_LEN];
7090 
7091 		(void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN,
7092 		    "%02x %02x %02x %02x %02x %02x %02x %02x "
7093 		    "%2x %02x %02x %02x %02x %02x %02x %02x",
7094 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7095 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7096 		buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0';
7097 		cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf);
7098 	}
7099 #endif
7100 
7101 	/*
7102 	 * Preset request sense data to NO SENSE.
7103 	 * If there is no way to get error information via Request Sense,
7104 	 * the packet request sense data would not have to be modified by HBA,
7105 	 * but it could be returned as is.
7106 	 */
7107 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
7108 	sata_fixed_sense_data_preset(
7109 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7110 
7111 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
7112 		/* Need callback function */
7113 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion;
7114 		synch = FALSE;
7115 	} else
7116 		synch = TRUE;
7117 
7118 	/* Transfer command to HBA */
7119 	if (sata_hba_start(spx, &rval) != 0) {
7120 		/* Pkt not accepted for execution */
7121 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
7122 		return (rval);
7123 	}
7124 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
7125 	/*
7126 	 * If execution is non-synchronous,
7127 	 * a callback function will handle potential errors, translate
7128 	 * the response and will do a callback to a target driver.
7129 	 * If it was synchronous, use the same framework callback to check
7130 	 * an execution status.
7131 	 */
7132 	if (synch) {
7133 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7134 		    "synchronous execution status %x\n",
7135 		    spx->txlt_sata_pkt->satapkt_reason);
7136 		sata_txlt_atapi_completion(spx->txlt_sata_pkt);
7137 	}
7138 	return (TRAN_ACCEPT);
7139 }
7140 
7141 
7142 /*
7143  * ATAPI Packet command completion.
7144  *
7145  * Failure of the command passed via Packet command are considered device
7146  * error. SATA HBA driver would have to retrieve error data (via Request
7147  * Sense command delivered via error retrieval sata packet) and copy it
7148  * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data.
7149  */
7150 static void
7151 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt)
7152 {
7153 	sata_pkt_txlate_t *spx =
7154 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7155 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7156 	struct scsi_extended_sense *sense;
7157 	struct buf *bp;
7158 	int rval;
7159 
7160 #ifdef SATA_DEBUG
7161 	uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense;
7162 #endif
7163 
7164 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7165 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7166 
7167 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7168 		/* Normal completion */
7169 		if (sata_pkt->satapkt_cmd.satacmd_bp != NULL)
7170 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
7171 		scsipkt->pkt_reason = CMD_CMPLT;
7172 		*scsipkt->pkt_scbp = STATUS_GOOD;
7173 		if (spx->txlt_tmp_buf != NULL) {
7174 			/* Temporary buffer was used */
7175 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7176 			if (bp->b_flags & B_READ) {
7177 				rval = ddi_dma_sync(
7178 				    spx->txlt_buf_dma_handle, 0, 0,
7179 				    DDI_DMA_SYNC_FORCPU);
7180 				ASSERT(rval == DDI_SUCCESS);
7181 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7182 				    bp->b_bcount);
7183 			}
7184 		}
7185 	} else {
7186 		/*
7187 		 * Something went wrong - analyze return
7188 		 */
7189 		*scsipkt->pkt_scbp = STATUS_CHECK;
7190 		sense = sata_arq_sense(spx);
7191 
7192 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7193 			scsipkt->pkt_reason = CMD_INCOMPLETE;
7194 			/*
7195 			 * We may not have ARQ data if there was a double
7196 			 * error. But sense data in sata packet was pre-set
7197 			 * with NO SENSE so it is valid even if HBA could
7198 			 * not retrieve a real sense data.
7199 			 * Just copy this sense data into scsi pkt sense area.
7200 			 */
7201 			bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense,
7202 			    SATA_ATAPI_MIN_RQSENSE_LEN);
7203 #ifdef SATA_DEBUG
7204 			if (sata_debug_flags & SATA_DBG_SCSI_IF) {
7205 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7206 				    "sata_txlt_atapi_completion: %02x\n"
7207 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
7208 				    "          %02x %02x %02x %02x %02x %02x "
7209 				    "          %02x %02x %02x %02x %02x %02x\n",
7210 				    scsipkt->pkt_reason,
7211 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7212 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7213 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7214 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7215 				    rqsp[16], rqsp[17]);
7216 			}
7217 #endif
7218 		} else {
7219 			switch (sata_pkt->satapkt_reason) {
7220 			case SATA_PKT_PORT_ERROR:
7221 				/*
7222 				 * We have no device data.
7223 				 */
7224 				scsipkt->pkt_reason = CMD_INCOMPLETE;
7225 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
7226 				    STATE_GOT_TARGET | STATE_SENT_CMD |
7227 				    STATE_GOT_STATUS);
7228 				sense->es_key = KEY_HARDWARE_ERROR;
7229 				break;
7230 
7231 			case SATA_PKT_TIMEOUT:
7232 				scsipkt->pkt_reason = CMD_TIMEOUT;
7233 				scsipkt->pkt_statistics |=
7234 				    STAT_TIMEOUT | STAT_DEV_RESET;
7235 				/*
7236 				 * Need to check if HARDWARE_ERROR/
7237 				 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more
7238 				 * appropriate.
7239 				 */
7240 				break;
7241 
7242 			case SATA_PKT_ABORTED:
7243 				scsipkt->pkt_reason = CMD_ABORTED;
7244 				scsipkt->pkt_statistics |= STAT_ABORTED;
7245 				/* Should we set key COMMAND_ABPRTED? */
7246 				break;
7247 
7248 			case SATA_PKT_RESET:
7249 				scsipkt->pkt_reason = CMD_RESET;
7250 				scsipkt->pkt_statistics |= STAT_DEV_RESET;
7251 				/*
7252 				 * May be we should set Unit Attention /
7253 				 * Reset. Perhaps the same should be
7254 				 * returned for disks....
7255 				 */
7256 				sense->es_key = KEY_UNIT_ATTENTION;
7257 				sense->es_add_code = SD_SCSI_ASC_RESET;
7258 				break;
7259 
7260 			default:
7261 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7262 				    "sata_txlt_atapi_completion: "
7263 				    "invalid packet completion reason"));
7264 				scsipkt->pkt_reason = CMD_TRAN_ERR;
7265 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
7266 				    STATE_GOT_TARGET | STATE_SENT_CMD |
7267 				    STATE_GOT_STATUS);
7268 				break;
7269 			}
7270 		}
7271 	}
7272 
7273 	SATAATAPITRACE(spx, 0);
7274 
7275 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7276 	    scsipkt->pkt_comp != NULL) {
7277 		/* scsi callback required */
7278 		(*scsipkt->pkt_comp)(scsipkt);
7279 	}
7280 }
7281 
7282 /*
7283  * Set up error retrieval sata command for ATAPI Packet Command error data
7284  * recovery.
7285  *
7286  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
7287  * returns SATA_FAILURE otherwise.
7288  */
7289 
7290 static int
7291 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
7292 {
7293 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
7294 	sata_cmd_t *scmd;
7295 	struct buf *bp;
7296 
7297 	/*
7298 	 * Allocate dma-able buffer error data.
7299 	 * Buffer allocation will take care of buffer alignment and other DMA
7300 	 * attributes.
7301 	 */
7302 	bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN);
7303 	if (bp == NULL) {
7304 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
7305 		    "sata_get_err_retrieval_pkt: "
7306 		    "cannot allocate buffer for error data", NULL);
7307 		return (SATA_FAILURE);
7308 	}
7309 	bp_mapin(bp); /* make data buffer accessible */
7310 
7311 	/* Operation modes are up to the caller */
7312 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7313 
7314 	/* Synchronous mode, no callback - may be changed by the caller */
7315 	spkt->satapkt_comp = NULL;
7316 	spkt->satapkt_time = sata_default_pkt_time;
7317 
7318 	scmd = &spkt->satapkt_cmd;
7319 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7320 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7321 
7322 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7323 
7324 	/*
7325 	 * Set-up acdb. Request Sense CDB (packet command content) is
7326 	 * not in DMA-able buffer. Its handling is HBA-specific (how
7327 	 * it is transfered into packet FIS).
7328 	 */
7329 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7330 	bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN);
7331 	/* Following zeroing of pad bytes may not be necessary */
7332 	bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN],
7333 	    sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN);
7334 
7335 	/*
7336 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
7337 	 * before accessing it. Handle is in usual place in translate struct.
7338 	 */
7339 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
7340 
7341 	/*
7342 	 * Preset request sense data to NO SENSE.
7343 	 * Here it is redundant, only for a symetry with scsi-originated
7344 	 * packets. It should not be used for anything but debugging.
7345 	 */
7346 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
7347 	sata_fixed_sense_data_preset(
7348 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7349 
7350 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7351 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7352 
7353 	return (SATA_SUCCESS);
7354 }
7355 
7356 /*
7357  * Set-up ATAPI packet command.
7358  * Data transfer direction has to be set-up in sata_cmd structure prior to
7359  * calling this function.
7360  *
7361  * Returns void
7362  */
7363 
7364 static void
7365 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo)
7366 {
7367 	scmd->satacmd_addr_type = 0;		/* N/A */
7368 	scmd->satacmd_sec_count_lsb = 0;	/* no tag */
7369 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
7370 	scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ;
7371 	scmd->satacmd_lba_high_lsb =
7372 	    (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8);
7373 	scmd->satacmd_cmd_reg = SATAC_PACKET;	/* Command */
7374 
7375 	/*
7376 	 * We want all data to be transfered via DMA.
7377 	 * But specify it only if drive supports DMA and DMA mode is
7378 	 * selected - some drives are sensitive about it.
7379 	 * Hopefully it wil work for all drives....
7380 	 */
7381 	if (sdinfo->satadrv_settings & SATA_DEV_DMA)
7382 		scmd->satacmd_features_reg = SATA_ATAPI_F_DMA;
7383 
7384 	/*
7385 	 * Features register requires special care for devices that use
7386 	 * Serial ATA bridge - they need an explicit specification of
7387 	 * the data transfer direction for Packet DMA commands.
7388 	 * Setting this bit is harmless if DMA is not used.
7389 	 *
7390 	 * Many drives do not implement word 80, specifying what ATA/ATAPI
7391 	 * spec they follow.
7392 	 * We are arbitrarily following the latest SerialATA 2.6 spec,
7393 	 * which uses ATA/ATAPI 6 specification for Identify Data, unless
7394 	 * ATA/ATAPI-7 support is explicitly indicated.
7395 	 */
7396 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
7397 	    sdinfo->satadrv_id.ai_majorversion != 0xffff &&
7398 	    (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) {
7399 		/*
7400 		 * Specification of major version is valid and version 7
7401 		 * is supported. It does automatically imply that all
7402 		 * spec features are supported. For now, we assume that
7403 		 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete.
7404 		 */
7405 		if ((sdinfo->satadrv_id.ai_dirdma &
7406 		    SATA_ATAPI_ID_DMADIR_REQ) != 0) {
7407 			if (scmd->satacmd_flags.sata_data_direction ==
7408 			    SATA_DIR_READ)
7409 			scmd->satacmd_features_reg |=
7410 			    SATA_ATAPI_F_DATA_DIR_READ;
7411 		}
7412 	}
7413 }
7414 
7415 
7416 #ifdef SATA_DEBUG
7417 
7418 /* Display 18 bytes of Inquiry data */
7419 static void
7420 sata_show_inqry_data(uint8_t *buf)
7421 {
7422 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
7423 	uint8_t *p;
7424 
7425 	cmn_err(CE_NOTE, "Inquiry data:");
7426 	cmn_err(CE_NOTE, "device type %x", inq->inq_dtype);
7427 	cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb);
7428 	cmn_err(CE_NOTE, "version %x", inq->inq_ansi);
7429 	cmn_err(CE_NOTE, "ATAPI transport version %d",
7430 	    SATA_ATAPI_TRANS_VERSION(inq));
7431 	cmn_err(CE_NOTE, "response data format %d, aenc %d",
7432 	    inq->inq_rdf, inq->inq_aenc);
7433 	cmn_err(CE_NOTE, " additional length %d", inq->inq_len);
7434 	cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs);
7435 	p = (uint8_t *)inq->inq_vid;
7436 	cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x "
7437 	    "%02x %02x %02x %02x",
7438 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
7439 	p = (uint8_t *)inq->inq_vid;
7440 	cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c",
7441 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
7442 
7443 	p = (uint8_t *)inq->inq_pid;
7444 	cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x "
7445 	    "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
7446 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7447 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7448 	p = (uint8_t *)inq->inq_pid;
7449 	cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c "
7450 	    "%c %c %c %c %c %c %c %c",
7451 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7452 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7453 
7454 	p = (uint8_t *)inq->inq_revision;
7455 	cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x",
7456 	    p[0], p[1], p[2], p[3]);
7457 	p = (uint8_t *)inq->inq_revision;
7458 	cmn_err(CE_NOTE, "revision: %c %c %c %c",
7459 	    p[0], p[1], p[2], p[3]);
7460 
7461 }
7462 
7463 
7464 static void
7465 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count)
7466 {
7467 	struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt;
7468 
7469 	if (scsi_pkt == NULL)
7470 		return;
7471 	if (count != 0) {
7472 		/* saving cdb */
7473 		bzero(sata_atapi_trace[sata_atapi_trace_index].acdb,
7474 		    SATA_ATAPI_MAX_CDB_LEN);
7475 		bcopy(scsi_pkt->pkt_cdbp,
7476 		    sata_atapi_trace[sata_atapi_trace_index].acdb, count);
7477 	} else {
7478 		bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)->
7479 		    sts_sensedata,
7480 		    sata_atapi_trace[sata_atapi_trace_index].arqs,
7481 		    SATA_ATAPI_MIN_RQSENSE_LEN);
7482 		sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason =
7483 		    scsi_pkt->pkt_reason;
7484 		sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason =
7485 		    spx->txlt_sata_pkt->satapkt_reason;
7486 
7487 		if (++sata_atapi_trace_index >= 64)
7488 			sata_atapi_trace_index = 0;
7489 	}
7490 }
7491 
7492 #endif
7493 
7494 /*
7495  * Fetch inquiry data from ATAPI device
7496  * Returns SATA_SUCCESS if operation was successfull, SATA_FAILURE otherwise.
7497  *
7498  * Note:
7499  * inqb pointer does not point to a DMA-able buffer. It is a local buffer
7500  * where the caller expects to see the inquiry data.
7501  *
7502  */
7503 
7504 static int
7505 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba,
7506     sata_address_t *saddr, struct scsi_inquiry *inq)
7507 {
7508 	sata_pkt_txlate_t *spx;
7509 	sata_pkt_t *spkt;
7510 	struct buf *bp;
7511 	sata_drive_info_t *sdinfo;
7512 	sata_cmd_t *scmd;
7513 	int rval;
7514 	uint8_t *rqsp;
7515 #ifdef SATA_DEBUG
7516 	char msg_buf[MAXPATHLEN];
7517 #endif
7518 
7519 	ASSERT(sata_hba != NULL);
7520 
7521 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
7522 	spx->txlt_sata_hba_inst = sata_hba;
7523 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
7524 	spkt = sata_pkt_alloc(spx, NULL);
7525 	if (spkt == NULL) {
7526 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7527 		return (SATA_FAILURE);
7528 	}
7529 	/* address is needed now */
7530 	spkt->satapkt_device.satadev_addr = *saddr;
7531 
7532 	/* scsi_inquiry size buffer */
7533 	bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry));
7534 	if (bp == NULL) {
7535 		sata_pkt_free(spx);
7536 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7537 		SATA_LOG_D((sata_hba, CE_WARN,
7538 		    "sata_get_atapi_inquiry_data: "
7539 		    "cannot allocate data buffer"));
7540 		return (SATA_FAILURE);
7541 	}
7542 	bp_mapin(bp); /* make data buffer accessible */
7543 
7544 	scmd = &spkt->satapkt_cmd;
7545 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7546 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7547 
7548 	/* Use synchronous mode */
7549 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7550 	spkt->satapkt_comp = NULL;
7551 	spkt->satapkt_time = sata_default_pkt_time;
7552 
7553 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
7554 
7555 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7556 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7557 
7558 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
7559 	sdinfo = sata_get_device_info(sata_hba,
7560 	    &spx->txlt_sata_pkt->satapkt_device);
7561 	if (sdinfo == NULL) {
7562 		/* we have to be carefull about the disapearing device */
7563 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7564 		rval = SATA_FAILURE;
7565 		goto cleanup;
7566 	}
7567 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7568 
7569 	/*
7570 	 * Set-up acdb. This works for atapi transport version 2 and later.
7571 	 */
7572 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7573 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7574 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
7575 	scmd->satacmd_acdb[1] = 0x00;
7576 	scmd->satacmd_acdb[2] = 0x00;
7577 	scmd->satacmd_acdb[3] = 0x00;
7578 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
7579 	scmd->satacmd_acdb[5] = 0x00;
7580 
7581 	sata_fixed_sense_data_preset(
7582 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7583 
7584 	/* Transfer command to HBA */
7585 	if (sata_hba_start(spx, &rval) != 0) {
7586 		/* Pkt not accepted for execution */
7587 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
7588 		    "sata_get_atapi_inquiry_data: "
7589 		    "Packet not accepted for execution - ret: %02x", rval);
7590 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7591 		rval = SATA_FAILURE;
7592 		goto cleanup;
7593 	}
7594 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7595 
7596 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
7597 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
7598 		    "sata_get_atapi_inquiry_data: "
7599 		    "Packet completed successfully - ret: %02x", rval);
7600 		if (spx->txlt_buf_dma_handle != NULL) {
7601 			/*
7602 			 * Sync buffer. Handle is in usual place in translate
7603 			 * struct.
7604 			 */
7605 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
7606 			    DDI_DMA_SYNC_FORCPU);
7607 			ASSERT(rval == DDI_SUCCESS);
7608 		}
7609 		/*
7610 		 * Normal completion - copy data into caller's buffer
7611 		 */
7612 		bcopy(bp->b_un.b_addr, (uint8_t *)inq,
7613 		    sizeof (struct scsi_inquiry));
7614 #ifdef SATA_DEBUG
7615 		if (sata_debug_flags & SATA_DBG_ATAPI) {
7616 			sata_show_inqry_data((uint8_t *)inq);
7617 		}
7618 #endif
7619 		rval = SATA_SUCCESS;
7620 	} else {
7621 		/*
7622 		 * Something went wrong - analyze return - check rqsense data
7623 		 */
7624 		rval = SATA_FAILURE;
7625 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7626 			/*
7627 			 * ARQ data hopefull show something other than NO SENSE
7628 			 */
7629 			rqsp = scmd->satacmd_rqsense;
7630 #ifdef SATA_DEBUG
7631 			if (sata_debug_flags & SATA_DBG_ATAPI) {
7632 				msg_buf[0] = '\0';
7633 				(void) snprintf(msg_buf, MAXPATHLEN,
7634 				    "ATAPI packet completion reason: %02x\n"
7635 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x\n"
7636 				    "          %02x %02x %02x %02x %02x %02x\n"
7637 				    "          %02x %02x %02x %02x %02x %02x",
7638 				    spkt->satapkt_reason,
7639 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7640 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7641 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7642 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7643 				    rqsp[16], rqsp[17]);
7644 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7645 				    "%s", msg_buf);
7646 			}
7647 #endif
7648 		} else {
7649 			switch (spkt->satapkt_reason) {
7650 			case SATA_PKT_PORT_ERROR:
7651 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7652 				    "sata_get_atapi_inquiry_data: "
7653 				    "packet reason: port error", NULL);
7654 				break;
7655 
7656 			case SATA_PKT_TIMEOUT:
7657 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7658 				    "sata_get_atapi_inquiry_data: "
7659 				    "packet reason: timeout", NULL);
7660 				break;
7661 
7662 			case SATA_PKT_ABORTED:
7663 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7664 				    "sata_get_atapi_inquiry_data: "
7665 				    "packet reason: aborted", NULL);
7666 				break;
7667 
7668 			case SATA_PKT_RESET:
7669 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7670 				    "sata_get_atapi_inquiry_data: "
7671 				    "packet reason: reset\n", NULL);
7672 				break;
7673 			default:
7674 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7675 				    "sata_get_atapi_inquiry_data: "
7676 				    "invalid packet reason: %02x\n",
7677 				    spkt->satapkt_reason);
7678 				break;
7679 			}
7680 		}
7681 	}
7682 cleanup:
7683 	sata_free_local_buffer(spx);
7684 	sata_pkt_free(spx);
7685 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
7686 	return (rval);
7687 }
7688 
7689 
7690 
7691 
7692 
7693 #if 0
7694 #ifdef SATA_DEBUG
7695 
7696 /*
7697  * Test ATAPI packet command.
7698  * Single threaded test: send packet command in synch mode, process completion
7699  *
7700  */
7701 static void
7702 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport)
7703 {
7704 	sata_pkt_txlate_t *spx;
7705 	sata_pkt_t *spkt;
7706 	struct buf *bp;
7707 	sata_device_t sata_device;
7708 	sata_drive_info_t *sdinfo;
7709 	sata_cmd_t *scmd;
7710 	int rval;
7711 	uint8_t *rqsp;
7712 
7713 	ASSERT(sata_hba_inst != NULL);
7714 	sata_device.satadev_addr.cport = cport;
7715 	sata_device.satadev_addr.pmport = 0;
7716 	sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
7717 	sata_device.satadev_rev = SATA_DEVICE_REV;
7718 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7719 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
7720 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7721 	if (sdinfo == NULL) {
7722 		sata_log(sata_hba_inst, CE_WARN,
7723 		    "sata_test_atapi_packet_command: "
7724 		    "no device info for cport %d",
7725 		    sata_device.satadev_addr.cport);
7726 		return;
7727 	}
7728 
7729 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
7730 	spx->txlt_sata_hba_inst = sata_hba_inst;
7731 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
7732 	spkt = sata_pkt_alloc(spx, NULL);
7733 	if (spkt == NULL) {
7734 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7735 		return;
7736 	}
7737 	/* address is needed now */
7738 	spkt->satapkt_device.satadev_addr = sata_device.satadev_addr;
7739 
7740 	/* 1024k buffer */
7741 	bp = sata_alloc_local_buffer(spx, 1024);
7742 	if (bp == NULL) {
7743 		sata_pkt_free(spx);
7744 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7745 		sata_log(sata_hba_inst, CE_WARN,
7746 		    "sata_test_atapi_packet_command: "
7747 		    "cannot allocate data buffer");
7748 		return;
7749 	}
7750 	bp_mapin(bp); /* make data buffer accessible */
7751 
7752 	scmd = &spkt->satapkt_cmd;
7753 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7754 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7755 
7756 	/* Use synchronous mode */
7757 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7758 
7759 	/* Synchronous mode, no callback - may be changed by the caller */
7760 	spkt->satapkt_comp = NULL;
7761 	spkt->satapkt_time = sata_default_pkt_time;
7762 
7763 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
7764 
7765 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7766 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7767 
7768 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7769 
7770 	/* Set-up acdb. */
7771 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7772 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7773 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
7774 	scmd->satacmd_acdb[1] = 0x00;
7775 	scmd->satacmd_acdb[2] = 0x00;
7776 	scmd->satacmd_acdb[3] = 0x00;
7777 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
7778 	scmd->satacmd_acdb[5] = 0x00;
7779 
7780 	sata_fixed_sense_data_preset(
7781 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7782 
7783 	/* Transfer command to HBA */
7784 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7785 	if (sata_hba_start(spx, &rval) != 0) {
7786 		/* Pkt not accepted for execution */
7787 		sata_log(sata_hba_inst, CE_WARN,
7788 		    "sata_test_atapi_packet_command: "
7789 		    "Packet not accepted for execution - ret: %02x", rval);
7790 		mutex_exit(
7791 		    &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7792 		goto cleanup;
7793 	}
7794 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7795 
7796 	if (spx->txlt_buf_dma_handle != NULL) {
7797 		/*
7798 		 * Sync buffer. Handle is in usual place in translate struct.
7799 		 */
7800 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
7801 		    DDI_DMA_SYNC_FORCPU);
7802 		ASSERT(rval == DDI_SUCCESS);
7803 	}
7804 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
7805 		sata_log(sata_hba_inst, CE_WARN,
7806 		    "sata_test_atapi_packet_command: "
7807 		    "Packet completed successfully");
7808 		/*
7809 		 * Normal completion - show inquiry data
7810 		 */
7811 		sata_show_inqry_data((uint8_t *)bp->b_un.b_addr);
7812 	} else {
7813 		/*
7814 		 * Something went wrong - analyze return - check rqsense data
7815 		 */
7816 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7817 			/*
7818 			 * ARQ data hopefull show something other than NO SENSE
7819 			 */
7820 			rqsp = scmd->satacmd_rqsense;
7821 			sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7822 			    "ATAPI packet completion reason: %02x\n"
7823 			    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
7824 			    "          %02x %02x %02x %02x %02x %02x "
7825 			    "          %02x %02x %02x %02x %02x %02x\n",
7826 			    spkt->satapkt_reason,
7827 			    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7828 			    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7829 			    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7830 			    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7831 			    rqsp[16], rqsp[17]);
7832 		} else {
7833 			switch (spkt->satapkt_reason) {
7834 			case SATA_PKT_PORT_ERROR:
7835 				sata_log(sata_hba_inst, CE_WARN,
7836 				    "sata_test_atapi_packet_command: "
7837 				    "packet reason: port error\n");
7838 				break;
7839 
7840 			case SATA_PKT_TIMEOUT:
7841 				sata_log(sata_hba_inst, CE_WARN,
7842 				    "sata_test_atapi_packet_command: "
7843 				    "packet reason: timeout\n");
7844 				break;
7845 
7846 			case SATA_PKT_ABORTED:
7847 				sata_log(sata_hba_inst, CE_WARN,
7848 				    "sata_test_atapi_packet_command: "
7849 				    "packet reason: aborted\n");
7850 				break;
7851 
7852 			case SATA_PKT_RESET:
7853 				sata_log(sata_hba_inst, CE_WARN,
7854 				    "sata_test_atapi_packet_command: "
7855 				    "packet reason: reset\n");
7856 				break;
7857 			default:
7858 				sata_log(sata_hba_inst, CE_WARN,
7859 				    "sata_test_atapi_packet_command: "
7860 				    "invalid packet reason: %02x\n",
7861 				    spkt->satapkt_reason);
7862 				break;
7863 			}
7864 		}
7865 	}
7866 cleanup:
7867 	sata_free_local_buffer(spx);
7868 	sata_pkt_free(spx);
7869 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
7870 }
7871 
7872 #endif /* SATA_DEBUG */
7873 #endif /* 1 */
7874 
7875 
7876 /* ************************** LOCAL HELPER FUNCTIONS *********************** */
7877 
7878 /*
7879  * Validate sata_tran info
7880  * SATA_FAILURE returns if structure is inconsistent or structure revision
7881  * does not match one used by the framework.
7882  *
7883  * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains
7884  * required function pointers.
7885  * Returns SATA_FAILURE otherwise.
7886  */
7887 static int
7888 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran)
7889 {
7890 	/*
7891 	 * SATA_TRAN_HBA_REV is the current (highest) revision number
7892 	 * of the SATA interface.
7893 	 */
7894 	if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) {
7895 		sata_log(NULL, CE_WARN,
7896 		    "sata: invalid sata_hba_tran version %d for driver %s",
7897 		    sata_tran->sata_tran_hba_rev, ddi_driver_name(dip));
7898 		return (SATA_FAILURE);
7899 	}
7900 
7901 	if (dip != sata_tran->sata_tran_hba_dip) {
7902 		SATA_LOG_D((NULL, CE_WARN,
7903 		    "sata: inconsistent sata_tran_hba_dip "
7904 		    "%p / %p", sata_tran->sata_tran_hba_dip, dip));
7905 		return (SATA_FAILURE);
7906 	}
7907 
7908 	if (sata_tran->sata_tran_probe_port == NULL ||
7909 	    sata_tran->sata_tran_start == NULL ||
7910 	    sata_tran->sata_tran_abort == NULL ||
7911 	    sata_tran->sata_tran_reset_dport == NULL ||
7912 	    sata_tran->sata_tran_hotplug_ops == NULL ||
7913 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL ||
7914 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate ==
7915 	    NULL) {
7916 		SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing "
7917 		    "required functions"));
7918 	}
7919 	return (SATA_SUCCESS);
7920 }
7921 
7922 /*
7923  * Remove HBA instance from sata_hba_list.
7924  */
7925 static void
7926 sata_remove_hba_instance(dev_info_t *dip)
7927 {
7928 	sata_hba_inst_t	*sata_hba_inst;
7929 
7930 	mutex_enter(&sata_mutex);
7931 	for (sata_hba_inst = sata_hba_list;
7932 	    sata_hba_inst != (struct sata_hba_inst *)NULL;
7933 	    sata_hba_inst = sata_hba_inst->satahba_next) {
7934 		if (sata_hba_inst->satahba_dip == dip)
7935 			break;
7936 	}
7937 
7938 	if (sata_hba_inst == (struct sata_hba_inst *)NULL) {
7939 #ifdef SATA_DEBUG
7940 		cmn_err(CE_WARN, "sata_remove_hba_instance: "
7941 		    "unknown HBA instance\n");
7942 #endif
7943 		ASSERT(FALSE);
7944 	}
7945 	if (sata_hba_inst == sata_hba_list) {
7946 		sata_hba_list = sata_hba_inst->satahba_next;
7947 		if (sata_hba_list) {
7948 			sata_hba_list->satahba_prev =
7949 			    (struct sata_hba_inst *)NULL;
7950 		}
7951 		if (sata_hba_inst == sata_hba_list_tail) {
7952 			sata_hba_list_tail = NULL;
7953 		}
7954 	} else if (sata_hba_inst == sata_hba_list_tail) {
7955 		sata_hba_list_tail = sata_hba_inst->satahba_prev;
7956 		if (sata_hba_list_tail) {
7957 			sata_hba_list_tail->satahba_next =
7958 			    (struct sata_hba_inst *)NULL;
7959 		}
7960 	} else {
7961 		sata_hba_inst->satahba_prev->satahba_next =
7962 		    sata_hba_inst->satahba_next;
7963 		sata_hba_inst->satahba_next->satahba_prev =
7964 		    sata_hba_inst->satahba_prev;
7965 	}
7966 	mutex_exit(&sata_mutex);
7967 }
7968 
7969 
7970 
7971 
7972 
7973 /*
7974  * Probe all SATA ports of the specified HBA instance.
7975  * The assumption is that there are no target and attachment point minor nodes
7976  * created by the boot subsystems, so we do not need to prune device tree.
7977  *
7978  * This function is called only from sata_hba_attach(). It does not have to
7979  * be protected by controller mutex, because the hba_attached flag is not set
7980  * yet and no one would be touching this HBA instance other than this thread.
7981  * Determines if port is active and what type of the device is attached
7982  * (if any). Allocates necessary structures for each port.
7983  *
7984  * An AP (Attachement Point) node is created for each SATA device port even
7985  * when there is no device attached.
7986  */
7987 
7988 static 	void
7989 sata_probe_ports(sata_hba_inst_t *sata_hba_inst)
7990 {
7991 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
7992 	int			ncport, npmport;
7993 	sata_cport_info_t 	*cportinfo;
7994 	sata_drive_info_t	*drive;
7995 	sata_pmult_info_t	*pminfo;
7996 	sata_pmport_info_t 	*pmportinfo;
7997 	sata_device_t		sata_device;
7998 	int			rval;
7999 	dev_t			minor_number;
8000 	char			name[16];
8001 	clock_t			start_time, cur_time;
8002 
8003 	/*
8004 	 * Probe controller ports first, to find port status and
8005 	 * any port multiplier attached.
8006 	 */
8007 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
8008 		/* allocate cport structure */
8009 		cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP);
8010 		ASSERT(cportinfo != NULL);
8011 		mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL);
8012 
8013 		mutex_enter(&cportinfo->cport_mutex);
8014 
8015 		cportinfo->cport_addr.cport = ncport;
8016 		cportinfo->cport_addr.pmport = 0;
8017 		cportinfo->cport_addr.qual = SATA_ADDR_CPORT;
8018 		cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
8019 		cportinfo->cport_state |= SATA_STATE_PROBING;
8020 		SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo;
8021 
8022 		/*
8023 		 * Regardless if a port is usable or not, create
8024 		 * an attachment point
8025 		 */
8026 		mutex_exit(&cportinfo->cport_mutex);
8027 		minor_number =
8028 		    SATA_MAKE_AP_MINOR(ddi_get_instance(dip), ncport, 0, 0);
8029 		(void) sprintf(name, "%d", ncport);
8030 		if (ddi_create_minor_node(dip, name, S_IFCHR,
8031 		    minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) !=
8032 		    DDI_SUCCESS) {
8033 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
8034 			    "cannot create SATA attachment point for port %d",
8035 			    ncport);
8036 		}
8037 
8038 		/* Probe port */
8039 		start_time = ddi_get_lbolt();
8040 	reprobe_cport:
8041 		sata_device.satadev_addr.cport = ncport;
8042 		sata_device.satadev_addr.pmport = 0;
8043 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
8044 		sata_device.satadev_rev = SATA_DEVICE_REV;
8045 
8046 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8047 		    (dip, &sata_device);
8048 
8049 		mutex_enter(&cportinfo->cport_mutex);
8050 		sata_update_port_scr(&cportinfo->cport_scr, &sata_device);
8051 		if (rval != SATA_SUCCESS) {
8052 			/* Something went wrong? Fail the port */
8053 			cportinfo->cport_state = SATA_PSTATE_FAILED;
8054 			mutex_exit(&cportinfo->cport_mutex);
8055 			continue;
8056 		}
8057 		cportinfo->cport_state &= ~SATA_STATE_PROBING;
8058 		cportinfo->cport_state |= SATA_STATE_PROBED;
8059 		cportinfo->cport_dev_type = sata_device.satadev_type;
8060 
8061 		cportinfo->cport_state |= SATA_STATE_READY;
8062 		if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) {
8063 			mutex_exit(&cportinfo->cport_mutex);
8064 			continue;
8065 		}
8066 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
8067 			/*
8068 			 * There is some device attached.
8069 			 * Allocate device info structure
8070 			 */
8071 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) {
8072 				mutex_exit(&cportinfo->cport_mutex);
8073 				SATA_CPORTINFO_DRV_INFO(cportinfo) =
8074 				    kmem_zalloc(sizeof (sata_drive_info_t),
8075 				    KM_SLEEP);
8076 				mutex_enter(&cportinfo->cport_mutex);
8077 			}
8078 			drive = SATA_CPORTINFO_DRV_INFO(cportinfo);
8079 			drive->satadrv_addr = cportinfo->cport_addr;
8080 			drive->satadrv_addr.qual = SATA_ADDR_DCPORT;
8081 			drive->satadrv_type = cportinfo->cport_dev_type;
8082 			drive->satadrv_state = SATA_STATE_UNKNOWN;
8083 
8084 			mutex_exit(&cportinfo->cport_mutex);
8085 			if (sata_add_device(dip, sata_hba_inst, ncport, 0) !=
8086 			    SATA_SUCCESS) {
8087 				/*
8088 				 * Plugged device was not correctly identified.
8089 				 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT
8090 				 */
8091 				cur_time = ddi_get_lbolt();
8092 				if ((cur_time - start_time) <
8093 				    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
8094 					/* sleep for a while */
8095 					delay(drv_usectohz(
8096 					    SATA_DEV_RETRY_DLY));
8097 					goto reprobe_cport;
8098 				}
8099 			}
8100 		} else {
8101 			mutex_exit(&cportinfo->cport_mutex);
8102 			ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT);
8103 			pminfo = kmem_zalloc(sizeof (sata_pmult_info_t),
8104 			    KM_SLEEP);
8105 			mutex_enter(&cportinfo->cport_mutex);
8106 			ASSERT(pminfo != NULL);
8107 			SATA_CPORTINFO_PMULT_INFO(cportinfo) = pminfo;
8108 			pminfo->pmult_addr.cport = cportinfo->cport_addr.cport;
8109 			pminfo->pmult_addr.pmport = SATA_PMULT_HOSTPORT;
8110 			pminfo->pmult_addr.qual = SATA_ADDR_PMPORT;
8111 			pminfo->pmult_num_dev_ports =
8112 			    sata_device.satadev_add_info;
8113 			mutex_init(&pminfo->pmult_mutex, NULL, MUTEX_DRIVER,
8114 			    NULL);
8115 			pminfo->pmult_state = SATA_STATE_PROBING;
8116 			mutex_exit(&cportinfo->cport_mutex);
8117 
8118 			/* Probe Port Multiplier ports */
8119 			for (npmport = 0;
8120 			    npmport < pminfo->pmult_num_dev_ports;
8121 			    npmport++) {
8122 				pmportinfo = kmem_zalloc(
8123 				    sizeof (sata_pmport_info_t), KM_SLEEP);
8124 				mutex_enter(&cportinfo->cport_mutex);
8125 				ASSERT(pmportinfo != NULL);
8126 				pmportinfo->pmport_addr.cport = ncport;
8127 				pmportinfo->pmport_addr.pmport = npmport;
8128 				pmportinfo->pmport_addr.qual =
8129 				    SATA_ADDR_PMPORT;
8130 				pminfo->pmult_dev_port[npmport] = pmportinfo;
8131 
8132 				mutex_init(&pmportinfo->pmport_mutex, NULL,
8133 				    MUTEX_DRIVER, NULL);
8134 
8135 				mutex_exit(&cportinfo->cport_mutex);
8136 
8137 				/* Create an attachment point */
8138 				minor_number = SATA_MAKE_AP_MINOR(
8139 				    ddi_get_instance(dip), ncport, npmport, 1);
8140 				(void) sprintf(name, "%d.%d", ncport, npmport);
8141 				if (ddi_create_minor_node(dip, name, S_IFCHR,
8142 				    minor_number, DDI_NT_SATA_ATTACHMENT_POINT,
8143 				    0) != DDI_SUCCESS) {
8144 					sata_log(sata_hba_inst, CE_WARN,
8145 					    "sata_hba_attach: "
8146 					    "cannot create SATA attachment "
8147 					    "point for port %d pmult port %d",
8148 					    ncport, npmport);
8149 				}
8150 
8151 				start_time = ddi_get_lbolt();
8152 			reprobe_pmport:
8153 				sata_device.satadev_addr.pmport = npmport;
8154 				sata_device.satadev_addr.qual =
8155 				    SATA_ADDR_PMPORT;
8156 
8157 				rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8158 				    (dip, &sata_device);
8159 				mutex_enter(&cportinfo->cport_mutex);
8160 
8161 				/* sata_update_port_info() */
8162 				sata_update_port_scr(&pmportinfo->pmport_scr,
8163 				    &sata_device);
8164 
8165 				if (rval != SATA_SUCCESS) {
8166 					pmportinfo->pmport_state =
8167 					    SATA_PSTATE_FAILED;
8168 					mutex_exit(&cportinfo->cport_mutex);
8169 					continue;
8170 				}
8171 				pmportinfo->pmport_state &=
8172 				    ~SATA_STATE_PROBING;
8173 				pmportinfo->pmport_state |= SATA_STATE_PROBED;
8174 				pmportinfo->pmport_dev_type =
8175 				    sata_device.satadev_type;
8176 
8177 				pmportinfo->pmport_state |= SATA_STATE_READY;
8178 				if (pmportinfo->pmport_dev_type ==
8179 				    SATA_DTYPE_NONE) {
8180 					mutex_exit(&cportinfo->cport_mutex);
8181 					continue;
8182 				}
8183 				/* Port multipliers cannot be chained */
8184 				ASSERT(pmportinfo->pmport_dev_type !=
8185 				    SATA_DTYPE_PMULT);
8186 				/*
8187 				 * There is something attached to Port
8188 				 * Multiplier device port
8189 				 * Allocate device info structure
8190 				 */
8191 				if (pmportinfo->pmport_sata_drive == NULL) {
8192 					mutex_exit(&cportinfo->cport_mutex);
8193 					pmportinfo->pmport_sata_drive =
8194 					    kmem_zalloc(
8195 					    sizeof (sata_drive_info_t),
8196 					    KM_SLEEP);
8197 					mutex_enter(&cportinfo->cport_mutex);
8198 				}
8199 				drive = pmportinfo->pmport_sata_drive;
8200 				drive->satadrv_addr.cport =
8201 				    pmportinfo->pmport_addr.cport;
8202 				drive->satadrv_addr.pmport = npmport;
8203 				drive->satadrv_addr.qual = SATA_ADDR_DPMPORT;
8204 				drive->satadrv_type = pmportinfo->
8205 				    pmport_dev_type;
8206 				drive->satadrv_state = SATA_STATE_UNKNOWN;
8207 
8208 				mutex_exit(&cportinfo->cport_mutex);
8209 				if (sata_add_device(dip, sata_hba_inst, ncport,
8210 				    npmport) != SATA_SUCCESS) {
8211 					/*
8212 					 * Plugged device was not correctly
8213 					 * identified. Retry, within the
8214 					 * SATA_DEV_IDENTIFY_TIMEOUT
8215 					 */
8216 					cur_time = ddi_get_lbolt();
8217 					if ((cur_time - start_time) <
8218 					    drv_usectohz(
8219 					    SATA_DEV_IDENTIFY_TIMEOUT)) {
8220 						/* sleep for a while */
8221 						delay(drv_usectohz(
8222 						    SATA_DEV_RETRY_DLY));
8223 						goto reprobe_pmport;
8224 					}
8225 				}
8226 			}
8227 			pmportinfo->pmport_state =
8228 			    SATA_STATE_PROBED | SATA_STATE_READY;
8229 		}
8230 	}
8231 }
8232 
8233 /*
8234  * Add SATA device for specified HBA instance & port (SCSI target
8235  * device nodes).
8236  * This function is called (indirectly) only from sata_hba_attach().
8237  * A target node is created when there is a supported type device attached,
8238  * but may be removed if it cannot be put online.
8239  *
8240  * This function cannot be called from an interrupt context.
8241  *
8242  * ONLY DISK TARGET NODES ARE CREATED NOW
8243  *
8244  * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when
8245  * device identification failed - adding a device could be retried.
8246  *
8247  */
8248 static 	int
8249 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst, int cport,
8250     int pmport)
8251 {
8252 	sata_cport_info_t 	*cportinfo;
8253 	sata_pmult_info_t	*pminfo;
8254 	sata_pmport_info_t	*pmportinfo;
8255 	dev_info_t		*cdip;		/* child dip */
8256 	sata_device_t		sata_device;
8257 	int			rval;
8258 
8259 
8260 
8261 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
8262 	ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE);
8263 	mutex_enter(&cportinfo->cport_mutex);
8264 	/*
8265 	 * Some device is attached to a controller port.
8266 	 * We rely on controllers distinquishing between no-device,
8267 	 * attached port multiplier and other kind of attached device.
8268 	 * We need to get Identify Device data and determine
8269 	 * positively the dev type before trying to attach
8270 	 * the target driver.
8271 	 */
8272 	sata_device.satadev_rev = SATA_DEVICE_REV;
8273 	if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
8274 		/*
8275 		 * Not port multiplier.
8276 		 */
8277 		sata_device.satadev_addr = cportinfo->cport_addr;
8278 		sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
8279 		mutex_exit(&cportinfo->cport_mutex);
8280 
8281 		rval = sata_probe_device(sata_hba_inst, &sata_device);
8282 		if (rval != SATA_SUCCESS ||
8283 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN)
8284 			return (SATA_FAILURE);
8285 
8286 		mutex_enter(&cportinfo->cport_mutex);
8287 		sata_show_drive_info(sata_hba_inst,
8288 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
8289 
8290 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
8291 			/*
8292 			 * Could not determine device type or
8293 			 * a device is not supported.
8294 			 * Degrade this device to unknown.
8295 			 */
8296 			cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8297 			mutex_exit(&cportinfo->cport_mutex);
8298 			return (SATA_SUCCESS);
8299 		}
8300 		cportinfo->cport_dev_type = sata_device.satadev_type;
8301 		cportinfo->cport_tgtnode_clean = B_TRUE;
8302 		mutex_exit(&cportinfo->cport_mutex);
8303 
8304 		/*
8305 		 * Initialize device to the desired state. Even if it
8306 		 * fails, the device will still attach but syslog
8307 		 * will show the warning.
8308 		 */
8309 		if (sata_initialize_device(sata_hba_inst,
8310 		    SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS)
8311 			/* Retry */
8312 			(void) sata_initialize_device(sata_hba_inst,
8313 			    SATA_CPORTINFO_DRV_INFO(cportinfo));
8314 
8315 		cdip = sata_create_target_node(pdip, sata_hba_inst,
8316 		    &sata_device.satadev_addr);
8317 		mutex_enter(&cportinfo->cport_mutex);
8318 		if (cdip == NULL) {
8319 			/*
8320 			 * Attaching target node failed.
8321 			 * We retain sata_drive_info structure...
8322 			 */
8323 			mutex_exit(&cportinfo->cport_mutex);
8324 			return (SATA_SUCCESS);
8325 		}
8326 		(SATA_CPORTINFO_DRV_INFO(cportinfo))->
8327 		    satadrv_state = SATA_STATE_READY;
8328 	} else {
8329 		/* This must be Port Multiplier type */
8330 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
8331 			SATA_LOG_D((sata_hba_inst, CE_WARN,
8332 			    "sata_add_device: "
8333 			    "unrecognized dev type %x",
8334 			    cportinfo->cport_dev_type));
8335 			mutex_exit(&cportinfo->cport_mutex);
8336 			return (SATA_SUCCESS);
8337 		}
8338 		pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
8339 		pmportinfo = pminfo->pmult_dev_port[pmport];
8340 		sata_device.satadev_addr = pmportinfo->pmport_addr;
8341 		sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
8342 		mutex_exit(&cportinfo->cport_mutex);
8343 
8344 		rval = sata_probe_device(sata_hba_inst, &sata_device);
8345 		if (rval != SATA_SUCCESS ||
8346 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN) {
8347 			return (SATA_FAILURE);
8348 		}
8349 		mutex_enter(&cportinfo->cport_mutex);
8350 		sata_show_drive_info(sata_hba_inst,
8351 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
8352 
8353 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
8354 			/*
8355 			 * Could not determine device type.
8356 			 * Degrade this device to unknown.
8357 			 */
8358 			pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
8359 			mutex_exit(&cportinfo->cport_mutex);
8360 			return (SATA_SUCCESS);
8361 		}
8362 		pmportinfo->pmport_dev_type = sata_device.satadev_type;
8363 		pmportinfo->pmport_tgtnode_clean = B_TRUE;
8364 		mutex_exit(&cportinfo->cport_mutex);
8365 
8366 		/*
8367 		 * Initialize device to the desired state.
8368 		 * Even if it fails, the device will still
8369 		 * attach but syslog will show the warning.
8370 		 */
8371 		if (sata_initialize_device(sata_hba_inst,
8372 		    pmportinfo->pmport_sata_drive) != SATA_SUCCESS)
8373 			/* Retry */
8374 			(void) sata_initialize_device(sata_hba_inst,
8375 			    pmportinfo->pmport_sata_drive);
8376 
8377 		cdip = sata_create_target_node(pdip, sata_hba_inst,
8378 		    &sata_device.satadev_addr);
8379 		mutex_enter(&cportinfo->cport_mutex);
8380 		if (cdip == NULL) {
8381 			/*
8382 			 * Attaching target node failed.
8383 			 * We retain sata_drive_info structure...
8384 			 */
8385 			mutex_exit(&cportinfo->cport_mutex);
8386 			return (SATA_SUCCESS);
8387 		}
8388 		pmportinfo->pmport_sata_drive->satadrv_state |=
8389 		    SATA_STATE_READY;
8390 	}
8391 	mutex_exit(&cportinfo->cport_mutex);
8392 	return (SATA_SUCCESS);
8393 }
8394 
8395 
8396 
8397 /*
8398  * Create scsi target node for attached device, create node properties and
8399  * attach the node.
8400  * The node could be removed if the device onlining fails.
8401  *
8402  * A dev_info_t pointer is returned if operation is successful, NULL is
8403  * returned otherwise.
8404  *
8405  * No port multiplier support.
8406  */
8407 
8408 static dev_info_t *
8409 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst,
8410 			sata_address_t *sata_addr)
8411 {
8412 	dev_info_t *cdip = NULL;
8413 	int rval;
8414 	char *nname = NULL;
8415 	char **compatible = NULL;
8416 	int ncompatible;
8417 	struct scsi_inquiry inq;
8418 	sata_device_t sata_device;
8419 	sata_drive_info_t *sdinfo;
8420 	int target;
8421 	int i;
8422 
8423 	sata_device.satadev_rev = SATA_DEVICE_REV;
8424 	sata_device.satadev_addr = *sata_addr;
8425 
8426 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport)));
8427 
8428 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
8429 
8430 	target = SATA_TO_SCSI_TARGET(sata_addr->cport,
8431 	    sata_addr->pmport, sata_addr->qual);
8432 
8433 	if (sdinfo == NULL) {
8434 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8435 		    sata_addr->cport)));
8436 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8437 		    "sata_create_target_node: no sdinfo for target %x",
8438 		    target));
8439 		return (NULL);
8440 	}
8441 
8442 	/*
8443 	 * create or get scsi inquiry data, expected by
8444 	 * scsi_hba_nodename_compatible_get()
8445 	 * SATA hard disks get Identify Data translated into Inguiry Data.
8446 	 * ATAPI devices respond directly to Inquiry request.
8447 	 */
8448 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
8449 		sata_identdev_to_inquiry(sata_hba_inst, sdinfo,
8450 		    (uint8_t *)&inq);
8451 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8452 		    sata_addr->cport)));
8453 	} else { /* Assume supported ATAPI device */
8454 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8455 		    sata_addr->cport)));
8456 		if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr,
8457 		    &inq) == SATA_FAILURE)
8458 			return (NULL);
8459 		/*
8460 		 * Save supported ATAPI transport version
8461 		 */
8462 		sdinfo->satadrv_atapi_trans_ver =
8463 		    SATA_ATAPI_TRANS_VERSION(&inq);
8464 	}
8465 
8466 	/* determine the node name and compatible */
8467 	scsi_hba_nodename_compatible_get(&inq, NULL,
8468 	    inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
8469 
8470 #ifdef SATA_DEBUG
8471 	if (sata_debug_flags & SATA_DBG_NODES) {
8472 		if (nname == NULL) {
8473 			cmn_err(CE_NOTE, "sata_create_target_node: "
8474 			    "cannot determine nodename for target %d\n",
8475 			    target);
8476 		} else {
8477 			cmn_err(CE_WARN, "sata_create_target_node: "
8478 			    "target %d nodename: %s\n", target, nname);
8479 		}
8480 		if (compatible == NULL) {
8481 			cmn_err(CE_WARN,
8482 			    "sata_create_target_node: no compatible name\n");
8483 		} else {
8484 			for (i = 0; i < ncompatible; i++) {
8485 				cmn_err(CE_WARN, "sata_create_target_node: "
8486 				    "compatible name: %s\n", compatible[i]);
8487 			}
8488 		}
8489 	}
8490 #endif
8491 
8492 	/* if nodename can't be determined, log error and exit */
8493 	if (nname == NULL) {
8494 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8495 		    "sata_create_target_node: cannot determine nodename "
8496 		    "for target %d\n", target));
8497 		scsi_hba_nodename_compatible_free(nname, compatible);
8498 		return (NULL);
8499 	}
8500 	/*
8501 	 * Create scsi target node
8502 	 */
8503 	ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip);
8504 	rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
8505 	    "device-type", "scsi");
8506 
8507 	if (rval != DDI_PROP_SUCCESS) {
8508 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8509 		    "updating device_type prop failed %d", rval));
8510 		goto fail;
8511 	}
8512 
8513 	/*
8514 	 * Create target node properties: target & lun
8515 	 */
8516 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target);
8517 	if (rval != DDI_PROP_SUCCESS) {
8518 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8519 		    "updating target prop failed %d", rval));
8520 		goto fail;
8521 	}
8522 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0);
8523 	if (rval != DDI_PROP_SUCCESS) {
8524 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8525 		    "updating target prop failed %d", rval));
8526 		goto fail;
8527 	}
8528 
8529 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
8530 		/*
8531 		 * Add "variant" property
8532 		 */
8533 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
8534 		    "variant", "atapi");
8535 		if (rval != DDI_PROP_SUCCESS) {
8536 			SATA_LOG_D((sata_hba_inst, CE_WARN,
8537 			    "sata_create_target_node: variant atapi "
8538 			    "property could not be created: %d", rval));
8539 			goto fail;
8540 		}
8541 	}
8542 	/* decorate the node with compatible */
8543 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible",
8544 	    compatible, ncompatible) != DDI_PROP_SUCCESS) {
8545 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8546 		    "sata_create_target_node: FAIL compatible props cdip 0x%p",
8547 		    (void *)cdip));
8548 		goto fail;
8549 	}
8550 
8551 
8552 	/*
8553 	 * Now, try to attach the driver. If probing of the device fails,
8554 	 * the target node may be removed
8555 	 */
8556 	rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH);
8557 
8558 	scsi_hba_nodename_compatible_free(nname, compatible);
8559 
8560 	if (rval == NDI_SUCCESS)
8561 		return (cdip);
8562 
8563 	/* target node was removed - are we sure? */
8564 	return (NULL);
8565 
8566 fail:
8567 	scsi_hba_nodename_compatible_free(nname, compatible);
8568 	ddi_prop_remove_all(cdip);
8569 	rval = ndi_devi_free(cdip);
8570 	if (rval != NDI_SUCCESS) {
8571 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8572 		    "node removal failed %d", rval));
8573 	}
8574 	sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: "
8575 	    "cannot create target node for SATA device at port %d",
8576 	    sata_addr->cport);
8577 	return (NULL);
8578 }
8579 
8580 
8581 
8582 /*
8583  * Re-probe sata port, check for a device and attach info
8584  * structures when necessary. Identify Device data is fetched, if possible.
8585  * Assumption: sata address is already validated.
8586  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
8587  * the presence of a device and its type.
8588  *
8589  * flag arg specifies that the function should try multiple times to identify
8590  * device type and to initialize it, or it should return immediately on failure.
8591  * SATA_DEV_IDENTIFY_RETRY - retry
8592  * SATA_DEV_IDENTIFY_NORETRY - no retry
8593  *
8594  * SATA_FAILURE is returned if one of the operations failed.
8595  *
8596  * This function cannot be called in interrupt context - it may sleep.
8597  *
8598  * NOte: Port multiplier is not supported yet, although there may be some
8599  * pieces of code referencing to it.
8600  */
8601 static int
8602 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
8603     int flag)
8604 {
8605 	sata_cport_info_t *cportinfo;
8606 	sata_drive_info_t *sdinfo, *osdinfo;
8607 	boolean_t init_device = B_FALSE;
8608 	int prev_device_type = SATA_DTYPE_NONE;
8609 	int prev_device_settings = 0;
8610 	int prev_device_state = 0;
8611 	clock_t start_time;
8612 	int retry = B_FALSE;
8613 	int rval;
8614 
8615 	/* We only care about host sata cport for now */
8616 	cportinfo = SATA_CPORT_INFO(sata_hba_inst,
8617 	    sata_device->satadev_addr.cport);
8618 	osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8619 	if (osdinfo != NULL) {
8620 		/*
8621 		 * We are re-probing port with a previously attached device.
8622 		 * Save previous device type and settings.
8623 		 */
8624 		prev_device_type = cportinfo->cport_dev_type;
8625 		prev_device_settings = osdinfo->satadrv_settings;
8626 		prev_device_state = osdinfo->satadrv_state;
8627 	}
8628 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
8629 		start_time = ddi_get_lbolt();
8630 		retry = B_TRUE;
8631 	}
8632 retry_probe:
8633 
8634 	/* probe port */
8635 	mutex_enter(&cportinfo->cport_mutex);
8636 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
8637 	cportinfo->cport_state |= SATA_STATE_PROBING;
8638 	mutex_exit(&cportinfo->cport_mutex);
8639 
8640 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8641 	    (SATA_DIP(sata_hba_inst), sata_device);
8642 
8643 	mutex_enter(&cportinfo->cport_mutex);
8644 	if (rval != SATA_SUCCESS) {
8645 		cportinfo->cport_state = SATA_PSTATE_FAILED;
8646 		mutex_exit(&cportinfo->cport_mutex);
8647 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: "
8648 		    "SATA port %d probing failed",
8649 		    cportinfo->cport_addr.cport));
8650 		return (SATA_FAILURE);
8651 	}
8652 
8653 	/*
8654 	 * update sata port state and set device type
8655 	 */
8656 	sata_update_port_info(sata_hba_inst, sata_device);
8657 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
8658 
8659 	/*
8660 	 * Sanity check - Port is active? Is the link active?
8661 	 * Is there any device attached?
8662 	 */
8663 	if ((cportinfo->cport_state &
8664 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
8665 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
8666 	    SATA_PORT_DEVLINK_UP) {
8667 		/*
8668 		 * Port in non-usable state or no link active/no device.
8669 		 * Free info structure if necessary (direct attached drive
8670 		 * only, for now!
8671 		 */
8672 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8673 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8674 		/* Add here differentiation for device attached or not */
8675 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8676 		mutex_exit(&cportinfo->cport_mutex);
8677 		if (sdinfo != NULL)
8678 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8679 		return (SATA_SUCCESS);
8680 	}
8681 
8682 	cportinfo->cport_state |= SATA_STATE_READY;
8683 	cportinfo->cport_dev_type = sata_device->satadev_type;
8684 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8685 
8686 	/*
8687 	 * If we are re-probing the port, there may be
8688 	 * sata_drive_info structure attached
8689 	 * (or sata_pm_info, if PMult is supported).
8690 	 */
8691 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
8692 		/*
8693 		 * There is no device, so remove device info structure,
8694 		 * if necessary.
8695 		 * Only direct attached drive is considered now, until
8696 		 * port multiplier is supported. If the previously
8697 		 * attached device was a port multiplier, we would need
8698 		 * to take care of devices attached beyond the port
8699 		 * multiplier.
8700 		 */
8701 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8702 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8703 		if (sdinfo != NULL) {
8704 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8705 			sata_log(sata_hba_inst, CE_WARN,
8706 			    "SATA device detached "
8707 			    "from port %d", cportinfo->cport_addr.cport);
8708 		}
8709 		mutex_exit(&cportinfo->cport_mutex);
8710 		return (SATA_SUCCESS);
8711 	}
8712 
8713 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
8714 		if (sdinfo == NULL) {
8715 			/*
8716 			 * There is some device attached, but there is
8717 			 * no sata_drive_info structure - allocate one
8718 			 */
8719 			mutex_exit(&cportinfo->cport_mutex);
8720 			sdinfo = kmem_zalloc(
8721 			    sizeof (sata_drive_info_t), KM_SLEEP);
8722 			mutex_enter(&cportinfo->cport_mutex);
8723 			/*
8724 			 * Recheck, that the port state did not change when we
8725 			 * released mutex.
8726 			 */
8727 			if (cportinfo->cport_state & SATA_STATE_READY) {
8728 				SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo;
8729 				sdinfo->satadrv_addr = cportinfo->cport_addr;
8730 				sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT;
8731 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
8732 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
8733 			} else {
8734 				/*
8735 				 * Port is not in ready state, we
8736 				 * cannot attach a device.
8737 				 */
8738 				mutex_exit(&cportinfo->cport_mutex);
8739 				kmem_free(sdinfo, sizeof (sata_drive_info_t));
8740 				return (SATA_SUCCESS);
8741 			}
8742 			/*
8743 			 * Since we are adding device, presumably new one,
8744 			 * indicate that it  should be initalized,
8745 			 * as well as some internal framework states).
8746 			 */
8747 			init_device = B_TRUE;
8748 		}
8749 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8750 		sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
8751 	} else {
8752 		/*
8753 		 * The device is a port multiplier - not handled now.
8754 		 */
8755 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8756 		mutex_exit(&cportinfo->cport_mutex);
8757 		return (SATA_SUCCESS);
8758 	}
8759 	mutex_exit(&cportinfo->cport_mutex);
8760 	/*
8761 	 * Figure out what kind of device we are really
8762 	 * dealing with.
8763 	 */
8764 	rval = sata_probe_device(sata_hba_inst, sata_device);
8765 
8766 	mutex_enter(&cportinfo->cport_mutex);
8767 	if (rval == SATA_SUCCESS) {
8768 		/*
8769 		 * If we are dealing with the same type of a device as before,
8770 		 * restore its settings flags.
8771 		 */
8772 		if (osdinfo != NULL &&
8773 		    sata_device->satadev_type == prev_device_type)
8774 			sdinfo->satadrv_settings = prev_device_settings;
8775 
8776 		mutex_exit(&cportinfo->cport_mutex);
8777 		/* Set initial device features, if necessary */
8778 		if (init_device == B_TRUE) {
8779 			rval = sata_initialize_device(sata_hba_inst, sdinfo);
8780 		}
8781 		if (rval == SATA_SUCCESS)
8782 			return (rval);
8783 	} else {
8784 		/*
8785 		 * If there was some device info before we probe the device,
8786 		 * restore previous device setting, so we can retry from scratch
8787 		 * later. Providing, of course, that device has not disapear
8788 		 * during probing process.
8789 		 */
8790 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
8791 			if (osdinfo != NULL) {
8792 				cportinfo->cport_dev_type = prev_device_type;
8793 				sdinfo->satadrv_type = prev_device_type;
8794 				sdinfo->satadrv_state = prev_device_state;
8795 			}
8796 		} else {
8797 			/* device is gone */
8798 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8799 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8800 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8801 			mutex_exit(&cportinfo->cport_mutex);
8802 			return (SATA_SUCCESS);
8803 		}
8804 		mutex_exit(&cportinfo->cport_mutex);
8805 	}
8806 
8807 	if (retry) {
8808 		clock_t cur_time = ddi_get_lbolt();
8809 		/*
8810 		 * A device was not successfully identified or initialized.
8811 		 * Track retry time for device identification.
8812 		 */
8813 		if ((cur_time - start_time) <
8814 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
8815 			/* sleep for a while */
8816 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
8817 			goto retry_probe;
8818 		} else {
8819 			mutex_enter(&cportinfo->cport_mutex);
8820 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL)
8821 				SATA_CPORTINFO_DRV_INFO(cportinfo)->
8822 				    satadrv_state = SATA_DSTATE_FAILED;
8823 			mutex_exit(&cportinfo->cport_mutex);
8824 		}
8825 	}
8826 	return (SATA_SUCCESS);
8827 }
8828 
8829 /*
8830  * Initialize device
8831  * Specified device is initialized to a default state.
8832  *
8833  * Returns SATA_SUCCESS if all device features are set successfully,
8834  * SATA_FAILURE otherwise
8835  */
8836 static int
8837 sata_initialize_device(sata_hba_inst_t *sata_hba_inst,
8838     sata_drive_info_t *sdinfo)
8839 {
8840 	int rval;
8841 
8842 	sata_save_drive_settings(sdinfo);
8843 
8844 	sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
8845 
8846 	sata_init_write_cache_mode(sdinfo);
8847 
8848 	rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0);
8849 
8850 	/* Determine current data transfer mode */
8851 	if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) {
8852 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
8853 	} else if ((sdinfo->satadrv_id.ai_validinfo &
8854 	    SATA_VALIDINFO_88) != 0 &&
8855 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) {
8856 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
8857 	} else if ((sdinfo->satadrv_id.ai_dworddma &
8858 	    SATA_MDMA_SEL_MASK) != 0) {
8859 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
8860 	} else
8861 		/* DMA supported, not no DMA transfer mode is selected !? */
8862 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
8863 
8864 	return (rval);
8865 }
8866 
8867 
8868 /*
8869  * Initialize write cache mode.
8870  *
8871  * The default write cache setting for SATA HDD is provided by sata_write_cache
8872  * static variable. ATAPI CD/DVDs devices have write cache default is
8873  * determined by sata_atapicdvd_write_cache static variable.
8874  * 1 - enable
8875  * 0 - disable
8876  * any other value - current drive setting
8877  *
8878  * Although there is not reason to disable write cache on CD/DVD devices,
8879  * the default setting control is provided for the maximun flexibility.
8880  *
8881  * In the future, it may be overridden by the
8882  * disk-write-cache-enable property setting, if it is defined.
8883  * Returns SATA_SUCCESS if all device features are set successfully,
8884  * SATA_FAILURE otherwise.
8885  */
8886 static void
8887 sata_init_write_cache_mode(sata_drive_info_t *sdinfo)
8888 {
8889 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
8890 		if (sata_write_cache == 1)
8891 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
8892 		else if (sata_write_cache == 0)
8893 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
8894 		/*
8895 		 * When sata_write_cache value is not 0 or 1,
8896 		 * a current setting of the drive's write cache is used.
8897 		 */
8898 	} else { /* Assume ATAPI CD/DVD device */
8899 		if (sata_atapicdvd_write_cache == 1)
8900 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
8901 		else if (sata_atapicdvd_write_cache == 0)
8902 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
8903 		/*
8904 		 * When sata_write_cache value is not 0 or 1,
8905 		 * a current setting of the drive's write cache is used.
8906 		 */
8907 	}
8908 }
8909 
8910 
8911 /*
8912  * Validate sata address.
8913  * Specified cport, pmport and qualifier has to match
8914  * passed sata_scsi configuration info.
8915  * The presence of an attached device is not verified.
8916  *
8917  * Returns 0 when address is valid, -1 otherwise.
8918  */
8919 static int
8920 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport,
8921 	int pmport, int qual)
8922 {
8923 	if (qual == SATA_ADDR_DCPORT && pmport != 0)
8924 		goto invalid_address;
8925 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
8926 		goto invalid_address;
8927 	if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) &&
8928 	    ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) ||
8929 	    (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) ||
8930 	    (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport))))
8931 		goto invalid_address;
8932 
8933 	return (0);
8934 
8935 invalid_address:
8936 	return (-1);
8937 
8938 }
8939 
8940 /*
8941  * Validate scsi address
8942  * SCSI target address is translated into SATA cport/pmport and compared
8943  * with a controller port/device configuration. LUN has to be 0.
8944  * Returns 0 if a scsi target refers to an attached device,
8945  * returns 1 if address is valid but device is not attached,
8946  * returns -1 if bad address or device is of an unsupported type.
8947  * Upon return sata_device argument is set.
8948  */
8949 static int
8950 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst,
8951 	struct scsi_address *ap, sata_device_t *sata_device)
8952 {
8953 	int cport, pmport, qual, rval;
8954 
8955 	rval = -1;	/* Invalid address */
8956 	if (ap->a_lun != 0)
8957 		goto out;
8958 
8959 	qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
8960 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
8961 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
8962 
8963 	if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT)
8964 		goto out;
8965 
8966 	if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) ==
8967 	    0) {
8968 
8969 		sata_cport_info_t *cportinfo;
8970 		sata_pmult_info_t *pmultinfo;
8971 		sata_drive_info_t *sdinfo = NULL;
8972 
8973 		rval = 1;	/* Valid sata address */
8974 
8975 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
8976 		if (qual == SATA_ADDR_DCPORT) {
8977 			if (cportinfo == NULL ||
8978 			    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
8979 				goto out;
8980 
8981 			if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT ||
8982 			    (cportinfo->cport_dev_type &
8983 			    SATA_VALID_DEV_TYPE) == 0) {
8984 				rval = -1;
8985 				goto out;
8986 			}
8987 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8988 
8989 		} else if (qual == SATA_ADDR_DPMPORT) {
8990 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
8991 			if (pmultinfo == NULL) {
8992 				rval = -1;
8993 				goto out;
8994 			}
8995 			if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) ==
8996 			    NULL ||
8997 			    SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
8998 			    pmport) == SATA_DTYPE_NONE)
8999 				goto out;
9000 
9001 			sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport,
9002 			    pmport);
9003 		} else {
9004 			rval = -1;
9005 			goto out;
9006 		}
9007 		if ((sdinfo == NULL) ||
9008 		    (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0)
9009 			goto out;
9010 
9011 		sata_device->satadev_type = sdinfo->satadrv_type;
9012 		sata_device->satadev_addr.qual = qual;
9013 		sata_device->satadev_addr.cport = cport;
9014 		sata_device->satadev_addr.pmport = pmport;
9015 		sata_device->satadev_rev = SATA_DEVICE_REV_1;
9016 		return (0);
9017 	}
9018 out:
9019 	if (rval == 1) {
9020 		SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
9021 		    "sata_validate_scsi_address: no valid target %x lun %x",
9022 		    ap->a_target, ap->a_lun);
9023 	}
9024 	return (rval);
9025 }
9026 
9027 /*
9028  * Find dip corresponding to passed device number
9029  *
9030  * Returns NULL if invalid device number is passed or device cannot be found,
9031  * Returns dip is device is found.
9032  */
9033 static dev_info_t *
9034 sata_devt_to_devinfo(dev_t dev)
9035 {
9036 	dev_info_t *dip;
9037 #ifndef __lock_lint
9038 	struct devnames *dnp;
9039 	major_t major = getmajor(dev);
9040 	int instance = SATA_MINOR2INSTANCE(getminor(dev));
9041 
9042 	if (major >= devcnt)
9043 		return (NULL);
9044 
9045 	dnp = &devnamesp[major];
9046 	LOCK_DEV_OPS(&(dnp->dn_lock));
9047 	dip = dnp->dn_head;
9048 	while (dip && (ddi_get_instance(dip) != instance)) {
9049 		dip = ddi_get_next(dip);
9050 	}
9051 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
9052 #endif
9053 
9054 	return (dip);
9055 }
9056 
9057 
9058 /*
9059  * Probe device.
9060  * This function issues Identify Device command and initializes local
9061  * sata_drive_info structure if the device can be identified.
9062  * The device type is determined by examining Identify Device
9063  * command response.
9064  * If the sata_hba_inst has linked drive info structure for this
9065  * device address, the Identify Device data is stored into sata_drive_info
9066  * structure linked to the port info structure.
9067  *
9068  * sata_device has to refer to the valid sata port(s) for HBA described
9069  * by sata_hba_inst structure.
9070  *
9071  * Returns:
9072  *	SATA_SUCCESS if device type was successfully probed and port-linked
9073  *		drive info structure was updated;
9074  * 	SATA_FAILURE if there is no device, or device was not probed
9075  *		successully;
9076  *	SATA_RETRY if device probe can be retried later.
9077  * If a device cannot be identified, sata_device's dev_state and dev_type
9078  * fields are set to unknown.
9079  * There are no retries in this function. Any retries should be managed by
9080  * the caller.
9081  */
9082 
9083 
9084 static int
9085 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
9086 {
9087 	sata_drive_info_t *sdinfo;
9088 	sata_drive_info_t new_sdinfo;	/* local drive info struct */
9089 	int rval;
9090 
9091 	ASSERT((SATA_CPORT_STATE(sata_hba_inst,
9092 	    sata_device->satadev_addr.cport) &
9093 	    (SATA_STATE_PROBED | SATA_STATE_READY)) != 0);
9094 
9095 	sata_device->satadev_type = SATA_DTYPE_NONE;
9096 
9097 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9098 	    sata_device->satadev_addr.cport)));
9099 
9100 	/* Get pointer to port-linked sata device info structure */
9101 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9102 	if (sdinfo != NULL) {
9103 		sdinfo->satadrv_state &=
9104 		    ~(SATA_STATE_PROBED | SATA_STATE_READY);
9105 		sdinfo->satadrv_state |= SATA_STATE_PROBING;
9106 	} else {
9107 		/* No device to probe */
9108 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9109 		    sata_device->satadev_addr.cport)));
9110 		sata_device->satadev_type = SATA_DTYPE_NONE;
9111 		sata_device->satadev_state = SATA_STATE_UNKNOWN;
9112 		return (SATA_FAILURE);
9113 	}
9114 	/*
9115 	 * Need to issue both types of identify device command and
9116 	 * determine device type by examining retreived data/status.
9117 	 * First, ATA Identify Device.
9118 	 */
9119 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
9120 	new_sdinfo.satadrv_addr = sata_device->satadev_addr;
9121 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9122 	    sata_device->satadev_addr.cport)));
9123 	new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
9124 	rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
9125 	if (rval == SATA_RETRY) {
9126 		/* We may try to check for ATAPI device */
9127 		if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) {
9128 			/*
9129 			 * HBA supports ATAPI - try to issue Identify Packet
9130 			 * Device command.
9131 			 */
9132 			new_sdinfo.satadrv_type = SATA_DTYPE_ATAPICD;
9133 			rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
9134 		}
9135 	}
9136 	if (rval == SATA_SUCCESS) {
9137 		/*
9138 		 * Got something responding positively to ATA Identify Device
9139 		 * or to Identify Packet Device cmd.
9140 		 * Save last used device type.
9141 		 */
9142 		sata_device->satadev_type = new_sdinfo.satadrv_type;
9143 
9144 		/* save device info, if possible */
9145 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9146 		    sata_device->satadev_addr.cport)));
9147 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9148 		if (sdinfo == NULL) {
9149 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9150 			    sata_device->satadev_addr.cport)));
9151 			return (SATA_FAILURE);
9152 		}
9153 		/*
9154 		 * Copy drive info into the port-linked drive info structure.
9155 		 */
9156 		*sdinfo = new_sdinfo;
9157 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
9158 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
9159 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
9160 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
9161 			    sata_device->satadev_addr.cport) =
9162 			    sdinfo->satadrv_type;
9163 		else /* SATA_ADDR_DPMPORT */
9164 			SATA_PMPORT_DEV_TYPE(sata_hba_inst,
9165 			    sata_device->satadev_addr.cport,
9166 			    sata_device->satadev_addr.pmport) =
9167 			    sdinfo->satadrv_type;
9168 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9169 		    sata_device->satadev_addr.cport)));
9170 		return (SATA_SUCCESS);
9171 	}
9172 
9173 	/*
9174 	 * It may be SATA_RETRY or SATA_FAILURE return.
9175 	 * Looks like we cannot determine the device type at this time.
9176 	 */
9177 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9178 	    sata_device->satadev_addr.cport)));
9179 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9180 	if (sdinfo != NULL) {
9181 		sata_device->satadev_type = SATA_DTYPE_UNKNOWN;
9182 		sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9183 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
9184 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
9185 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
9186 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
9187 			    sata_device->satadev_addr.cport) =
9188 			    SATA_DTYPE_UNKNOWN;
9189 		else {
9190 			/* SATA_ADDR_DPMPORT */
9191 			if ((SATA_PMULT_INFO(sata_hba_inst,
9192 			    sata_device->satadev_addr.cport) != NULL) &&
9193 			    (SATA_PMPORT_INFO(sata_hba_inst,
9194 			    sata_device->satadev_addr.cport,
9195 			    sata_device->satadev_addr.pmport) != NULL))
9196 				SATA_PMPORT_DEV_TYPE(sata_hba_inst,
9197 				    sata_device->satadev_addr.cport,
9198 				    sata_device->satadev_addr.pmport) =
9199 				    SATA_DTYPE_UNKNOWN;
9200 		}
9201 	}
9202 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9203 	    sata_device->satadev_addr.cport)));
9204 	return (rval);
9205 }
9206 
9207 
9208 /*
9209  * Get pointer to sata_drive_info structure.
9210  *
9211  * The sata_device has to contain address (cport, pmport and qualifier) for
9212  * specified sata_scsi structure.
9213  *
9214  * Returns NULL if device address is not valid for this HBA configuration.
9215  * Otherwise, returns a pointer to sata_drive_info structure.
9216  *
9217  * This function should be called with a port mutex held.
9218  */
9219 static sata_drive_info_t *
9220 sata_get_device_info(sata_hba_inst_t *sata_hba_inst,
9221     sata_device_t *sata_device)
9222 {
9223 	uint8_t cport = sata_device->satadev_addr.cport;
9224 	uint8_t pmport = sata_device->satadev_addr.pmport;
9225 	uint8_t qual = sata_device->satadev_addr.qual;
9226 
9227 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
9228 		return (NULL);
9229 
9230 	if (!(SATA_CPORT_STATE(sata_hba_inst, cport) &
9231 	    (SATA_STATE_PROBED | SATA_STATE_READY)))
9232 		/* Port not probed yet */
9233 		return (NULL);
9234 
9235 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE)
9236 		return (NULL);
9237 
9238 	if (qual == SATA_ADDR_DCPORT) {
9239 		/* Request for a device on a controller port */
9240 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
9241 		    SATA_DTYPE_PMULT)
9242 			/* Port multiplier attached */
9243 			return (NULL);
9244 		return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport));
9245 	}
9246 	if (qual == SATA_ADDR_DPMPORT) {
9247 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
9248 		    SATA_DTYPE_PMULT)
9249 			return (NULL);
9250 
9251 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport))
9252 			return (NULL);
9253 
9254 		return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport));
9255 	}
9256 
9257 	/* we should not get here */
9258 	return (NULL);
9259 }
9260 
9261 
9262 /*
9263  * sata_identify_device.
9264  * Send Identify Device command to SATA HBA driver.
9265  * If command executes successfully, update sata_drive_info structure pointed
9266  * to by sdinfo argument, including Identify Device data.
9267  * If command fails, invalidate data in sata_drive_info.
9268  *
9269  * Cannot be called from interrupt level.
9270  *
9271  * Returns:
9272  * SATA_SUCCESS if the device was identified as a supported device,
9273  * SATA_RETRY if the device was not identified but could be retried,
9274  * SATA_FAILURE if the device was not identified and identify attempt
9275  *	should not be retried.
9276  */
9277 static int
9278 sata_identify_device(sata_hba_inst_t *sata_hba_inst,
9279     sata_drive_info_t *sdinfo)
9280 {
9281 	uint16_t cfg_word;
9282 	int rval;
9283 
9284 	/* fetch device identify data */
9285 	if ((rval = sata_fetch_device_identify_data(sata_hba_inst,
9286 	    sdinfo)) != 0)
9287 		goto fail_unknown;
9288 
9289 	cfg_word = sdinfo->satadrv_id.ai_config;
9290 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK &&
9291 	    (cfg_word & SATA_ATA_TYPE_MASK) != SATA_ATA_TYPE) {
9292 		/* Change device type to reflect Identify Device data */
9293 		if (((cfg_word & SATA_ATAPI_TYPE_MASK) ==
9294 		    SATA_ATAPI_TYPE) &&
9295 		    ((cfg_word & SATA_ATAPI_ID_DEV_TYPE) ==
9296 		    SATA_ATAPI_CDROM_DEV)) {
9297 			sdinfo->satadrv_type = SATA_DTYPE_ATAPICD;
9298 		} else {
9299 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9300 		}
9301 	} else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD &&
9302 	    (((cfg_word & SATA_ATAPI_TYPE_MASK) != SATA_ATAPI_TYPE) ||
9303 	    ((cfg_word & SATA_ATAPI_ID_DEV_TYPE) != SATA_ATAPI_CDROM_DEV))) {
9304 		/* Change device type to reflect Identify Device data ! */
9305 		if ((sdinfo->satadrv_id.ai_config & SATA_ATA_TYPE_MASK) ==
9306 		    SATA_ATA_TYPE) {
9307 			sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
9308 		} else {
9309 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9310 		}
9311 	}
9312 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9313 		if (sdinfo->satadrv_capacity == 0) {
9314 			/* Non-LBA disk. Too bad... */
9315 			sata_log(sata_hba_inst, CE_WARN,
9316 			    "SATA disk device at port %d does not support LBA",
9317 			    sdinfo->satadrv_addr.cport);
9318 			rval = SATA_FAILURE;
9319 			goto fail_unknown;
9320 		}
9321 	}
9322 #if 0
9323 	/* Left for historical reason */
9324 	/*
9325 	 * Some initial version of SATA spec indicated that at least
9326 	 * UDMA mode 4 has to be supported. It is not metioned in
9327 	 * SerialATA 2.6, so this restriction is removed.
9328 	 */
9329 	/* Check for Ultra DMA modes 6 through 0 being supported */
9330 	for (i = 6; i >= 0; --i) {
9331 		if (sdinfo->satadrv_id.ai_ultradma & (1 << i))
9332 			break;
9333 	}
9334 
9335 	/*
9336 	 * At least UDMA 4 mode has to be supported. If mode 4 or
9337 	 * higher are not supported by the device, fail this
9338 	 * device.
9339 	 */
9340 	if (i < 4) {
9341 		/* No required Ultra DMA mode supported */
9342 		sata_log(sata_hba_inst, CE_WARN,
9343 		    "SATA disk device at port %d does not support UDMA "
9344 		    "mode 4 or higher", sdinfo->satadrv_addr.cport);
9345 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9346 		    "mode 4 or higher required, %d supported", i));
9347 		rval = SATA_FAILURE;
9348 		goto fail_unknown;
9349 	}
9350 #endif
9351 
9352 	return (SATA_SUCCESS);
9353 
9354 fail_unknown:
9355 	/* Invalidate sata_drive_info ? */
9356 	sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9357 	sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
9358 	return (rval);
9359 }
9360 
9361 /*
9362  * Log/display device information
9363  */
9364 static void
9365 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst,
9366     sata_drive_info_t *sdinfo)
9367 {
9368 	int valid_version;
9369 	char msg_buf[MAXPATHLEN];
9370 	int i;
9371 
9372 	/* Show HBA path */
9373 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf);
9374 
9375 	cmn_err(CE_CONT, "?%s :\n", msg_buf);
9376 
9377 	if (sdinfo->satadrv_type == SATA_DTYPE_UNKNOWN) {
9378 		(void) sprintf(msg_buf,
9379 		    "Unsupported SATA device type (cfg 0x%x) at ",
9380 		    sdinfo->satadrv_id.ai_config);
9381 	} else {
9382 		(void) sprintf(msg_buf, "SATA %s device at",
9383 		    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
9384 		    "disk":"CD/DVD (ATAPI)");
9385 	}
9386 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT)
9387 		cmn_err(CE_CONT, "?\t%s port %d\n",
9388 		    msg_buf, sdinfo->satadrv_addr.cport);
9389 	else
9390 		cmn_err(CE_CONT, "?\t%s port %d pmport %d\n",
9391 		    msg_buf, sdinfo->satadrv_addr.cport,
9392 		    sdinfo->satadrv_addr.pmport);
9393 
9394 	bcopy(&sdinfo->satadrv_id.ai_model, msg_buf,
9395 	    sizeof (sdinfo->satadrv_id.ai_model));
9396 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model));
9397 	msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
9398 	cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf);
9399 
9400 	bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf,
9401 	    sizeof (sdinfo->satadrv_id.ai_fw));
9402 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw));
9403 	msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
9404 	cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf);
9405 
9406 	bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf,
9407 	    sizeof (sdinfo->satadrv_id.ai_drvser));
9408 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser));
9409 	msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
9410 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9411 		cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
9412 	} else {
9413 		/* Assuming ATAPI CD/DVD */
9414 		/*
9415 		 * SOme drives do not implement serial number and may
9416 		 * violate the spec by provinding spaces rather than zeros
9417 		 * in serial number field. Scan the buffer to detect it.
9418 		 */
9419 		for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) {
9420 			if (msg_buf[i] != '\0' && msg_buf[i] != ' ')
9421 				break;
9422 		}
9423 		if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) {
9424 			cmn_err(CE_CONT, "?\tserial number - none\n");
9425 		} else {
9426 			cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
9427 		}
9428 	}
9429 
9430 #ifdef SATA_DEBUG
9431 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
9432 	    sdinfo->satadrv_id.ai_majorversion != 0xffff) {
9433 		int i;
9434 		for (i = 14; i >= 2; i--) {
9435 			if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) {
9436 				valid_version = i;
9437 				break;
9438 			}
9439 		}
9440 		cmn_err(CE_CONT,
9441 		    "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
9442 		    valid_version,
9443 		    sdinfo->satadrv_id.ai_majorversion,
9444 		    sdinfo->satadrv_id.ai_minorversion);
9445 	}
9446 #endif
9447 	/* Log some info */
9448 	cmn_err(CE_CONT, "?\tsupported features:\n");
9449 	msg_buf[0] = '\0';
9450 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9451 		if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48)
9452 			(void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN);
9453 		else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28)
9454 			(void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN);
9455 	}
9456 	if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA)
9457 		(void) strlcat(msg_buf, "DMA", MAXPATHLEN);
9458 	if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ)
9459 		(void) strlcat(msg_buf, ", Native Command Queueing",
9460 		    MAXPATHLEN);
9461 	if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)
9462 		(void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN);
9463 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) &&
9464 	    (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED))
9465 		(void) strlcat(msg_buf, ", SMART", MAXPATHLEN);
9466 	if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) &&
9467 	    (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED))
9468 		(void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN);
9469 	cmn_err(CE_CONT, "?\t %s\n", msg_buf);
9470 	if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2)
9471 		cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n");
9472 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1)
9473 		cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n");
9474 	if (sdinfo->satadrv_features_support &
9475 	    (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) {
9476 		msg_buf[0] = '\0';
9477 		(void) snprintf(msg_buf, MAXPATHLEN,
9478 		    "Supported queue depth %d",
9479 		    sdinfo->satadrv_queue_depth);
9480 		if (!(sata_func_enable &
9481 		    (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ)))
9482 			(void) strlcat(msg_buf,
9483 			    " - queueing disabled globally", MAXPATHLEN);
9484 		else if (sdinfo->satadrv_queue_depth >
9485 		    sdinfo->satadrv_max_queue_depth) {
9486 			(void) snprintf(&msg_buf[strlen(msg_buf)],
9487 			    MAXPATHLEN - strlen(msg_buf), ", limited to %d",
9488 			    (int)sdinfo->satadrv_max_queue_depth);
9489 		}
9490 		cmn_err(CE_CONT, "?\t%s\n", msg_buf);
9491 	}
9492 
9493 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9494 #ifdef __i386
9495 		(void) sprintf(msg_buf, "\tcapacity = %llu sectors\n",
9496 		    sdinfo->satadrv_capacity);
9497 #else
9498 		(void) sprintf(msg_buf, "\tcapacity = %lu sectors\n",
9499 		    sdinfo->satadrv_capacity);
9500 #endif
9501 		cmn_err(CE_CONT, "?%s", msg_buf);
9502 	}
9503 }
9504 
9505 
9506 /*
9507  * sata_save_drive_settings extracts current setting of the device and stores
9508  * it for future reference, in case the device setup would need to be restored
9509  * after the device reset.
9510  *
9511  * For all devices read ahead and write cache settings are saved, if the
9512  * device supports these features at all.
9513  * For ATAPI devices the Removable Media Status Notification setting is saved.
9514  */
9515 static void
9516 sata_save_drive_settings(sata_drive_info_t *sdinfo)
9517 {
9518 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) ||
9519 	    (sdinfo->satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) {
9520 
9521 		/* Current setting of Read Ahead (and Read Cache) */
9522 		if (sdinfo->satadrv_id.ai_features85 & SATA_LOOK_AHEAD)
9523 			sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
9524 		else
9525 			sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD;
9526 
9527 		/* Current setting of Write Cache */
9528 		if (sdinfo->satadrv_id.ai_features85 & SATA_WRITE_CACHE)
9529 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
9530 		else
9531 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
9532 	}
9533 
9534 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
9535 		if (sdinfo->satadrv_id.ai_cmdset83 & SATA_RM_STATUS_NOTIFIC)
9536 			sdinfo->satadrv_settings |= SATA_DEV_RMSN;
9537 		else
9538 			sdinfo->satadrv_settings &= ~SATA_DEV_RMSN;
9539 	}
9540 }
9541 
9542 
9543 /*
9544  * sata_check_capacity function determines a disk capacity
9545  * and addressing mode (LBA28/LBA48) by examining a disk identify device data.
9546  *
9547  * NOTE: CHS mode is not supported! If a device does not support LBA,
9548  * this function is not called.
9549  *
9550  * Returns device capacity in number of blocks, i.e. largest addressable LBA+1
9551  */
9552 static uint64_t
9553 sata_check_capacity(sata_drive_info_t *sdinfo)
9554 {
9555 	uint64_t capacity = 0;
9556 	int i;
9557 
9558 	if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK ||
9559 	    !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT)
9560 		/* Capacity valid only for LBA-addressable disk devices */
9561 		return (0);
9562 
9563 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) &&
9564 	    (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) &&
9565 	    (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) {
9566 		/* LBA48 mode supported and enabled */
9567 		sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 |
9568 		    SATA_DEV_F_LBA28;
9569 		for (i = 3;  i >= 0;  --i) {
9570 			capacity <<= 16;
9571 			capacity += sdinfo->satadrv_id.ai_addrsecxt[i];
9572 		}
9573 	} else {
9574 		capacity = sdinfo->satadrv_id.ai_addrsec[1];
9575 		capacity <<= 16;
9576 		capacity += sdinfo->satadrv_id.ai_addrsec[0];
9577 		if (capacity >= 0x1000000)
9578 			/* LBA28 mode */
9579 			sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28;
9580 	}
9581 	return (capacity);
9582 }
9583 
9584 
9585 /*
9586  * Allocate consistent buffer for DMA transfer
9587  *
9588  * Cannot be called from interrupt level or with mutex held - it may sleep.
9589  *
9590  * Returns pointer to allocated buffer structure, or NULL if allocation failed.
9591  */
9592 static struct buf *
9593 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len)
9594 {
9595 	struct scsi_address ap;
9596 	struct buf *bp;
9597 	ddi_dma_attr_t	cur_dma_attr;
9598 
9599 	ASSERT(spx->txlt_sata_pkt != NULL);
9600 	ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran;
9601 	ap.a_target = SATA_TO_SCSI_TARGET(
9602 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport,
9603 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport,
9604 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual);
9605 	ap.a_lun = 0;
9606 
9607 	bp = scsi_alloc_consistent_buf(&ap, NULL, len,
9608 	    B_READ, SLEEP_FUNC, NULL);
9609 
9610 	if (bp != NULL) {
9611 		/* Allocate DMA resources for this buffer */
9612 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
9613 		/*
9614 		 * We use a local version of the dma_attr, to account
9615 		 * for a device addressing limitations.
9616 		 * sata_adjust_dma_attr() will handle sdinfo == NULL which
9617 		 * will cause dma attributes to be adjusted to a lowest
9618 		 * acceptable level.
9619 		 */
9620 		sata_adjust_dma_attr(NULL,
9621 		    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
9622 
9623 		if (sata_dma_buf_setup(spx, PKT_CONSISTENT,
9624 		    SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) {
9625 			scsi_free_consistent_buf(bp);
9626 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
9627 			bp = NULL;
9628 		}
9629 	}
9630 	return (bp);
9631 }
9632 
9633 /*
9634  * Release local buffer (consistent buffer for DMA transfer) allocated
9635  * via sata_alloc_local_buffer().
9636  */
9637 static void
9638 sata_free_local_buffer(sata_pkt_txlate_t *spx)
9639 {
9640 	ASSERT(spx->txlt_sata_pkt != NULL);
9641 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL);
9642 
9643 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0;
9644 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL;
9645 
9646 	if (spx->txlt_buf_dma_handle != NULL) {
9647 		/* Free DMA resources */
9648 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
9649 		ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
9650 		spx->txlt_buf_dma_handle = 0;
9651 
9652 		if (spx->txlt_dma_cookie_list != &spx->txlt_dma_cookie) {
9653 			kmem_free(spx->txlt_dma_cookie_list,
9654 			    spx->txlt_dma_cookie_list_len *
9655 			    sizeof (ddi_dma_cookie_t));
9656 			spx->txlt_dma_cookie_list = NULL;
9657 			spx->txlt_dma_cookie_list_len = 0;
9658 		}
9659 	}
9660 
9661 	/* Free buffer */
9662 	scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp);
9663 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
9664 }
9665 
9666 
9667 
9668 
9669 /*
9670  * Allocate sata_pkt
9671  * Pkt structure version and embedded strcutures version are initialized.
9672  * sata_pkt and sata_pkt_txlate structures are cross-linked.
9673  *
9674  * Since this may be called in interrupt context by sata_scsi_init_pkt,
9675  * callback argument determines if it can sleep or not.
9676  * Hence, it should not be called from interrupt context.
9677  *
9678  * If successful, non-NULL pointer to a sata pkt is returned.
9679  * Upon failure, NULL pointer is returned.
9680  */
9681 static sata_pkt_t *
9682 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t))
9683 {
9684 	sata_pkt_t *spkt;
9685 	int kmsflag;
9686 
9687 	kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
9688 	spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag);
9689 	if (spkt == NULL) {
9690 		SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9691 		    "sata_pkt_alloc: failed"));
9692 		return (NULL);
9693 	}
9694 	spkt->satapkt_rev = SATA_PKT_REV;
9695 	spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV;
9696 	spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
9697 	spkt->satapkt_framework_private = spx;
9698 	spx->txlt_sata_pkt = spkt;
9699 	return (spkt);
9700 }
9701 
9702 /*
9703  * Free sata pkt allocated via sata_pkt_alloc()
9704  */
9705 static void
9706 sata_pkt_free(sata_pkt_txlate_t *spx)
9707 {
9708 	ASSERT(spx->txlt_sata_pkt != NULL);
9709 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL);
9710 	kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t));
9711 	spx->txlt_sata_pkt = NULL;
9712 }
9713 
9714 
9715 /*
9716  * Adjust DMA attributes.
9717  * SCSI cmds block count is up to 24 bits, SATA cmd block count vary
9718  * from 8 bits to 16 bits, depending on a command being used.
9719  * Limiting max block count arbitrarily to 256 for all read/write
9720  * commands may affects performance, so check both the device and
9721  * controller capability before adjusting dma attributes.
9722  */
9723 void
9724 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr,
9725     ddi_dma_attr_t *adj_dma_attr)
9726 {
9727 	uint32_t count_max;
9728 
9729 	/* Copy original attributes */
9730 	*adj_dma_attr = *dma_attr;
9731 	/*
9732 	 * Things to consider: device addressing capability,
9733 	 * "excessive" controller DMA capabilities.
9734 	 * If a device is being probed/initialized, there are
9735 	 * no device info - use default limits then.
9736 	 */
9737 	if (sdinfo == NULL) {
9738 		count_max = dma_attr->dma_attr_granular * 0x100;
9739 		if (dma_attr->dma_attr_count_max > count_max)
9740 			adj_dma_attr->dma_attr_count_max = count_max;
9741 		if (dma_attr->dma_attr_maxxfer > count_max)
9742 			adj_dma_attr->dma_attr_maxxfer = count_max;
9743 		return;
9744 	}
9745 
9746 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9747 		if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) {
9748 			/*
9749 			 * 16-bit sector count may be used - we rely on
9750 			 * the assumption that only read and write cmds
9751 			 * will request more than 256 sectors worth of data
9752 			 */
9753 			count_max = adj_dma_attr->dma_attr_granular * 0x10000;
9754 		} else {
9755 			/*
9756 			 * 8-bit sector count will be used - default limits
9757 			 * for dma attributes
9758 			 */
9759 			count_max = adj_dma_attr->dma_attr_granular * 0x100;
9760 		}
9761 		/*
9762 		 * Adjust controler dma attributes, if necessary
9763 		 */
9764 		if (dma_attr->dma_attr_count_max > count_max)
9765 			adj_dma_attr->dma_attr_count_max = count_max;
9766 		if (dma_attr->dma_attr_maxxfer > count_max)
9767 			adj_dma_attr->dma_attr_maxxfer = count_max;
9768 	}
9769 }
9770 
9771 
9772 /*
9773  * Allocate DMA resources for the buffer
9774  * This function handles initial DMA resource allocation as well as
9775  * DMA window shift and may be called repeatedly for the same DMA window
9776  * until all DMA cookies in the DMA window are processed.
9777  * To guarantee that there is always a coherent set of cookies to process
9778  * by SATA HBA driver (observing alignment, device granularity, etc.),
9779  * the number of slots for DMA cookies is equal to lesser of  a number of
9780  * cookies in a DMA window and a max number of scatter/gather entries.
9781  *
9782  * Returns DDI_SUCCESS upon successful operation.
9783  * Return failure code of a failing command or DDI_FAILURE when
9784  * internal cleanup failed.
9785  */
9786 static int
9787 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags,
9788     int (*callback)(caddr_t), caddr_t arg,
9789     ddi_dma_attr_t *cur_dma_attr)
9790 {
9791 	int	rval;
9792 	off_t	offset;
9793 	size_t	size;
9794 	int	max_sg_len, req_len, i;
9795 	uint_t	dma_flags;
9796 	struct buf	*bp;
9797 	uint64_t	cur_txfer_len;
9798 
9799 
9800 	ASSERT(spx->txlt_sata_pkt != NULL);
9801 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9802 	ASSERT(bp != NULL);
9803 
9804 
9805 	if (spx->txlt_buf_dma_handle == NULL) {
9806 		/*
9807 		 * No DMA resources allocated so far - this is a first call
9808 		 * for this sata pkt.
9809 		 */
9810 		rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst),
9811 		    cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle);
9812 
9813 		if (rval != DDI_SUCCESS) {
9814 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9815 			    "sata_dma_buf_setup: no buf DMA resources %x",
9816 			    rval));
9817 			return (rval);
9818 		}
9819 
9820 		if (bp->b_flags & B_READ)
9821 			dma_flags = DDI_DMA_READ;
9822 		else
9823 			dma_flags = DDI_DMA_WRITE;
9824 
9825 		if (flags & PKT_CONSISTENT)
9826 			dma_flags |= DDI_DMA_CONSISTENT;
9827 
9828 		if (flags & PKT_DMA_PARTIAL)
9829 			dma_flags |= DDI_DMA_PARTIAL;
9830 
9831 		/*
9832 		 * Check buffer alignment and size against dma attributes
9833 		 * Consider dma_attr_align only. There may be requests
9834 		 * with the size lower than device granularity, but they
9835 		 * will not read/write from/to the device, so no adjustment
9836 		 * is necessary. The dma_attr_minxfer theoretically should
9837 		 * be considered, but no HBA driver is checking it.
9838 		 */
9839 		if (IS_P2ALIGNED(bp->b_un.b_addr,
9840 		    cur_dma_attr->dma_attr_align)) {
9841 			rval = ddi_dma_buf_bind_handle(
9842 			    spx->txlt_buf_dma_handle,
9843 			    bp, dma_flags, callback, arg,
9844 			    &spx->txlt_dma_cookie,
9845 			    &spx->txlt_curwin_num_dma_cookies);
9846 		} else { /* Buffer is not aligned */
9847 
9848 			int	(*ddicallback)(caddr_t);
9849 			size_t	bufsz;
9850 
9851 			/* Check id sleeping is allowed */
9852 			ddicallback = (callback == NULL_FUNC) ?
9853 			    DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
9854 
9855 			SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
9856 			    "mis-aligned buffer: addr=0x%p, cnt=%lu",
9857 			    (void *)bp->b_un.b_addr, bp->b_bcount);
9858 
9859 			if (bp->b_flags & (B_PAGEIO|B_PHYS))
9860 				/*
9861 				 * CPU will need to access data in the buffer
9862 				 * (for copying) so map it.
9863 				 */
9864 				bp_mapin(bp);
9865 
9866 			ASSERT(spx->txlt_tmp_buf == NULL);
9867 
9868 			/* Buffer may be padded by ddi_dma_mem_alloc()! */
9869 			rval = ddi_dma_mem_alloc(
9870 			    spx->txlt_buf_dma_handle,
9871 			    bp->b_bcount,
9872 			    &sata_acc_attr,
9873 			    DDI_DMA_STREAMING,
9874 			    ddicallback, NULL,
9875 			    &spx->txlt_tmp_buf,
9876 			    &bufsz,
9877 			    &spx->txlt_tmp_buf_handle);
9878 
9879 			if (rval != DDI_SUCCESS) {
9880 				/* DMA mapping failed */
9881 				(void) ddi_dma_free_handle(
9882 				    &spx->txlt_buf_dma_handle);
9883 				spx->txlt_buf_dma_handle = NULL;
9884 #ifdef SATA_DEBUG
9885 				mbuffail_count++;
9886 #endif
9887 				SATADBG1(SATA_DBG_DMA_SETUP,
9888 				    spx->txlt_sata_hba_inst,
9889 				    "sata_dma_buf_setup: "
9890 				    "buf dma mem alloc failed %x\n", rval);
9891 				return (rval);
9892 			}
9893 			ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf,
9894 			    cur_dma_attr->dma_attr_align));
9895 
9896 #ifdef SATA_DEBUG
9897 			mbuf_count++;
9898 
9899 			if (bp->b_bcount != bufsz)
9900 				/*
9901 				 * This will require special handling, because
9902 				 * DMA cookies will be based on the temporary
9903 				 * buffer size, not the original buffer
9904 				 * b_bcount, so the residue may have to
9905 				 * be counted differently.
9906 				 */
9907 				SATADBG2(SATA_DBG_DMA_SETUP,
9908 				    spx->txlt_sata_hba_inst,
9909 				    "sata_dma_buf_setup: bp size %x != "
9910 				    "bufsz %x\n", bp->b_bcount, bufsz);
9911 #endif
9912 			if (dma_flags & DDI_DMA_WRITE) {
9913 				/*
9914 				 * Write operation - copy data into
9915 				 * an aligned temporary buffer. Buffer will be
9916 				 * synced for device by ddi_dma_addr_bind_handle
9917 				 */
9918 				bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf,
9919 				    bp->b_bcount);
9920 			}
9921 
9922 			rval = ddi_dma_addr_bind_handle(
9923 			    spx->txlt_buf_dma_handle,
9924 			    NULL,
9925 			    spx->txlt_tmp_buf,
9926 			    bufsz, dma_flags, ddicallback, 0,
9927 			    &spx->txlt_dma_cookie,
9928 			    &spx->txlt_curwin_num_dma_cookies);
9929 		}
9930 
9931 		switch (rval) {
9932 		case DDI_DMA_PARTIAL_MAP:
9933 			SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
9934 			    "sata_dma_buf_setup: DMA Partial Map\n", NULL);
9935 			/*
9936 			 * Partial DMA mapping.
9937 			 * Retrieve number of DMA windows for this request.
9938 			 */
9939 			if (ddi_dma_numwin(spx->txlt_buf_dma_handle,
9940 			    &spx->txlt_num_dma_win) != DDI_SUCCESS) {
9941 				if (spx->txlt_tmp_buf != NULL) {
9942 					ddi_dma_mem_free(
9943 					    &spx->txlt_tmp_buf_handle);
9944 					spx->txlt_tmp_buf = NULL;
9945 				}
9946 				(void) ddi_dma_unbind_handle(
9947 				    spx->txlt_buf_dma_handle);
9948 				(void) ddi_dma_free_handle(
9949 				    &spx->txlt_buf_dma_handle);
9950 				spx->txlt_buf_dma_handle = NULL;
9951 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9952 				    "sata_dma_buf_setup: numwin failed\n"));
9953 				return (DDI_FAILURE);
9954 			}
9955 			SATADBG2(SATA_DBG_DMA_SETUP,
9956 			    spx->txlt_sata_hba_inst,
9957 			    "sata_dma_buf_setup: windows: %d, cookies: %d\n",
9958 			    spx->txlt_num_dma_win,
9959 			    spx->txlt_curwin_num_dma_cookies);
9960 			spx->txlt_cur_dma_win = 0;
9961 			break;
9962 
9963 		case DDI_DMA_MAPPED:
9964 			/* DMA fully mapped */
9965 			spx->txlt_num_dma_win = 1;
9966 			spx->txlt_cur_dma_win = 0;
9967 			SATADBG1(SATA_DBG_DMA_SETUP,
9968 			    spx->txlt_sata_hba_inst,
9969 			    "sata_dma_buf_setup: windows: 1 "
9970 			    "cookies: %d\n", spx->txlt_curwin_num_dma_cookies);
9971 			break;
9972 
9973 		default:
9974 			/* DMA mapping failed */
9975 			if (spx->txlt_tmp_buf != NULL) {
9976 				ddi_dma_mem_free(
9977 				    &spx->txlt_tmp_buf_handle);
9978 				spx->txlt_tmp_buf = NULL;
9979 			}
9980 			(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
9981 			spx->txlt_buf_dma_handle = NULL;
9982 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9983 			    "sata_dma_buf_setup: buf dma handle binding "
9984 			    "failed %x\n", rval));
9985 			return (rval);
9986 		}
9987 		spx->txlt_curwin_processed_dma_cookies = 0;
9988 		spx->txlt_dma_cookie_list = NULL;
9989 	} else {
9990 		/*
9991 		 * DMA setup is reused. Check if we need to process more
9992 		 * cookies in current window, or to get next window, if any.
9993 		 */
9994 
9995 		ASSERT(spx->txlt_curwin_processed_dma_cookies <=
9996 		    spx->txlt_curwin_num_dma_cookies);
9997 
9998 		if (spx->txlt_curwin_processed_dma_cookies ==
9999 		    spx->txlt_curwin_num_dma_cookies) {
10000 			/*
10001 			 * All cookies from current DMA window were processed.
10002 			 * Get next DMA window.
10003 			 */
10004 			spx->txlt_cur_dma_win++;
10005 			if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) {
10006 				(void) ddi_dma_getwin(spx->txlt_buf_dma_handle,
10007 				    spx->txlt_cur_dma_win, &offset, &size,
10008 				    &spx->txlt_dma_cookie,
10009 				    &spx->txlt_curwin_num_dma_cookies);
10010 				spx->txlt_curwin_processed_dma_cookies = 0;
10011 			} else {
10012 				/* No more windows! End of request! */
10013 				/* What to do? - panic for now */
10014 				ASSERT(spx->txlt_cur_dma_win >=
10015 				    spx->txlt_num_dma_win);
10016 
10017 				spx->txlt_curwin_num_dma_cookies = 0;
10018 				spx->txlt_curwin_processed_dma_cookies = 0;
10019 				spx->txlt_sata_pkt->
10020 				    satapkt_cmd.satacmd_num_dma_cookies = 0;
10021 				return (DDI_SUCCESS);
10022 			}
10023 		}
10024 	}
10025 	/* There better be at least one DMA cookie outstanding */
10026 	ASSERT((spx->txlt_curwin_num_dma_cookies -
10027 	    spx->txlt_curwin_processed_dma_cookies) > 0);
10028 
10029 	if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) {
10030 		/* The default cookie slot was used in previous run */
10031 		ASSERT(spx->txlt_curwin_processed_dma_cookies == 0);
10032 		spx->txlt_dma_cookie_list = NULL;
10033 		spx->txlt_dma_cookie_list_len = 0;
10034 	}
10035 	if (spx->txlt_curwin_processed_dma_cookies == 0) {
10036 		/*
10037 		 * Processing a new DMA window - set-up dma cookies list.
10038 		 * We may reuse previously allocated cookie array if it is
10039 		 * possible.
10040 		 */
10041 		if (spx->txlt_dma_cookie_list != NULL &&
10042 		    spx->txlt_dma_cookie_list_len <
10043 		    spx->txlt_curwin_num_dma_cookies) {
10044 			/*
10045 			 * New DMA window contains more cookies than
10046 			 * the previous one. We need larger cookie list - free
10047 			 * the old one.
10048 			 */
10049 			(void) kmem_free(spx->txlt_dma_cookie_list,
10050 			    spx->txlt_dma_cookie_list_len *
10051 			    sizeof (ddi_dma_cookie_t));
10052 			spx->txlt_dma_cookie_list = NULL;
10053 			spx->txlt_dma_cookie_list_len = 0;
10054 		}
10055 		if (spx->txlt_dma_cookie_list == NULL) {
10056 			/*
10057 			 * Calculate lesser of number of cookies in this
10058 			 * DMA window and number of s/g entries.
10059 			 */
10060 			max_sg_len = cur_dma_attr->dma_attr_sgllen;
10061 			req_len = MIN(max_sg_len,
10062 			    spx->txlt_curwin_num_dma_cookies);
10063 
10064 			/* Allocate new dma cookie array if necessary */
10065 			if (req_len == 1) {
10066 				/* Only one cookie - no need for a list */
10067 				spx->txlt_dma_cookie_list =
10068 				    &spx->txlt_dma_cookie;
10069 				spx->txlt_dma_cookie_list_len = 1;
10070 			} else {
10071 				/*
10072 				 * More than one cookie - try to allocate space.
10073 				 */
10074 				spx->txlt_dma_cookie_list = kmem_zalloc(
10075 				    sizeof (ddi_dma_cookie_t) * req_len,
10076 				    callback == NULL_FUNC ? KM_NOSLEEP :
10077 				    KM_SLEEP);
10078 				if (spx->txlt_dma_cookie_list == NULL) {
10079 					SATADBG1(SATA_DBG_DMA_SETUP,
10080 					    spx->txlt_sata_hba_inst,
10081 					    "sata_dma_buf_setup: cookie list "
10082 					    "allocation failed\n", NULL);
10083 					/*
10084 					 * We could not allocate space for
10085 					 * neccessary number of dma cookies in
10086 					 * this window, so we fail this request.
10087 					 * Next invocation would try again to
10088 					 * allocate space for cookie list.
10089 					 * Note:Packet residue was not modified.
10090 					 */
10091 					return (DDI_DMA_NORESOURCES);
10092 				} else {
10093 					spx->txlt_dma_cookie_list_len = req_len;
10094 				}
10095 			}
10096 		}
10097 		/*
10098 		 * Fetch DMA cookies into cookie list in sata_pkt_txlate.
10099 		 * First cookie was already fetched.
10100 		 */
10101 		*(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie;
10102 		cur_txfer_len =
10103 		    (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size;
10104 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1;
10105 		spx->txlt_curwin_processed_dma_cookies++;
10106 		for (i = 1; (i < spx->txlt_dma_cookie_list_len) &&
10107 		    (i < spx->txlt_curwin_num_dma_cookies); i++) {
10108 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
10109 			    &spx->txlt_dma_cookie_list[i]);
10110 			cur_txfer_len +=
10111 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
10112 			spx->txlt_curwin_processed_dma_cookies++;
10113 			spx->txlt_sata_pkt->
10114 			    satapkt_cmd.satacmd_num_dma_cookies += 1;
10115 		}
10116 	} else {
10117 		SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
10118 		    "sata_dma_buf_setup: sliding within DMA window, "
10119 		    "cur cookie %d, total cookies %d\n",
10120 		    spx->txlt_curwin_processed_dma_cookies,
10121 		    spx->txlt_curwin_num_dma_cookies);
10122 
10123 		/*
10124 		 * Not all cookies from the current dma window were used because
10125 		 * of s/g limitation.
10126 		 * There is no need to re-size the list - it was set at
10127 		 * optimal size, or only default entry is used (s/g = 1).
10128 		 */
10129 		if (spx->txlt_dma_cookie_list == NULL) {
10130 			spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie;
10131 			spx->txlt_dma_cookie_list_len = 1;
10132 		}
10133 		/*
10134 		 * Since we are processing remaining cookies in a DMA window,
10135 		 * there may be less of them than the number of entries in the
10136 		 * current dma cookie list.
10137 		 */
10138 		req_len = MIN(spx->txlt_dma_cookie_list_len,
10139 		    (spx->txlt_curwin_num_dma_cookies -
10140 		    spx->txlt_curwin_processed_dma_cookies));
10141 
10142 		/* Fetch the next batch of cookies */
10143 		for (i = 0, cur_txfer_len = 0; i < req_len; i++) {
10144 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
10145 			    &spx->txlt_dma_cookie_list[i]);
10146 			cur_txfer_len +=
10147 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
10148 			spx->txlt_sata_pkt->
10149 			    satapkt_cmd.satacmd_num_dma_cookies++;
10150 			spx->txlt_curwin_processed_dma_cookies++;
10151 		}
10152 	}
10153 
10154 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0);
10155 
10156 	/* Point sata_cmd to the cookie list */
10157 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list =
10158 	    &spx->txlt_dma_cookie_list[0];
10159 
10160 	/* Remember number of DMA cookies passed in sata packet */
10161 	spx->txlt_num_dma_cookies =
10162 	    spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies;
10163 
10164 	ASSERT(cur_txfer_len != 0);
10165 	if (cur_txfer_len <= bp->b_bcount)
10166 		spx->txlt_total_residue -= cur_txfer_len;
10167 	else {
10168 		/*
10169 		 * Temporary DMA buffer has been padded by
10170 		 * ddi_dma_mem_alloc()!
10171 		 * This requires special handling, because DMA cookies are
10172 		 * based on the temporary buffer size, not the b_bcount,
10173 		 * and we have extra bytes to transfer - but the packet
10174 		 * residue has to stay correct because we will copy only
10175 		 * the requested number of bytes.
10176 		 */
10177 		spx->txlt_total_residue -= bp->b_bcount;
10178 	}
10179 
10180 	return (DDI_SUCCESS);
10181 }
10182 
10183 /*
10184  * Common routine for releasing DMA resources
10185  */
10186 static void
10187 sata_common_free_dma_rsrcs(sata_pkt_txlate_t *spx)
10188 {
10189 	if (spx->txlt_buf_dma_handle != NULL) {
10190 		if (spx->txlt_tmp_buf != NULL)  {
10191 			/*
10192 			 * Intermediate DMA buffer was allocated.
10193 			 * Free allocated buffer and associated access handle.
10194 			 */
10195 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
10196 			spx->txlt_tmp_buf = NULL;
10197 		}
10198 		/*
10199 		 * Free DMA resources - cookies and handles
10200 		 */
10201 		/* ASSERT(spx->txlt_dma_cookie_list != NULL); */
10202 		if (spx->txlt_dma_cookie_list != NULL) {
10203 			if (spx->txlt_dma_cookie_list !=
10204 			    &spx->txlt_dma_cookie) {
10205 				(void) kmem_free(spx->txlt_dma_cookie_list,
10206 				    spx->txlt_dma_cookie_list_len *
10207 				    sizeof (ddi_dma_cookie_t));
10208 				spx->txlt_dma_cookie_list = NULL;
10209 			}
10210 		}
10211 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
10212 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
10213 		spx->txlt_buf_dma_handle = NULL;
10214 	}
10215 }
10216 
10217 /*
10218  * Free DMA resources
10219  * Used by the HBA driver to release DMA resources that it does not use.
10220  *
10221  * Returns Void
10222  */
10223 void
10224 sata_free_dma_resources(sata_pkt_t *sata_pkt)
10225 {
10226 	sata_pkt_txlate_t *spx;
10227 
10228 	if (sata_pkt == NULL)
10229 		return;
10230 
10231 	spx = (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
10232 
10233 	sata_common_free_dma_rsrcs(spx);
10234 }
10235 
10236 /*
10237  * Fetch Device Identify data.
10238  * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type)
10239  * command to a device and get the device identify data.
10240  * The device_info structure has to be set to device type (for selecting proper
10241  * device identify command).
10242  *
10243  * Returns:
10244  * SATA_SUCCESS if cmd succeeded
10245  * SATA_RETRY if cmd was rejected and could be retried,
10246  * SATA_FAILURE if cmd failed and should not be retried (port error)
10247  *
10248  * Cannot be called in an interrupt context.
10249  */
10250 
10251 static int
10252 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst,
10253     sata_drive_info_t *sdinfo)
10254 {
10255 	struct buf *bp;
10256 	sata_pkt_t *spkt;
10257 	sata_cmd_t *scmd;
10258 	sata_pkt_txlate_t *spx;
10259 	int rval;
10260 
10261 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10262 	spx->txlt_sata_hba_inst = sata_hba_inst;
10263 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
10264 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10265 	if (spkt == NULL) {
10266 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10267 		return (SATA_RETRY); /* may retry later */
10268 	}
10269 	/* address is needed now */
10270 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10271 
10272 	/*
10273 	 * Allocate buffer for Identify Data return data
10274 	 */
10275 	bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t));
10276 	if (bp == NULL) {
10277 		sata_pkt_free(spx);
10278 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10279 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10280 		    "sata_fetch_device_identify_data: "
10281 		    "cannot allocate buffer for ID"));
10282 		return (SATA_RETRY); /* may retry later */
10283 	}
10284 
10285 	/* Fill sata_pkt */
10286 	sdinfo->satadrv_state = SATA_STATE_PROBING;
10287 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10288 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10289 	/* Synchronous mode, no callback */
10290 	spkt->satapkt_comp = NULL;
10291 	/* Timeout 30s */
10292 	spkt->satapkt_time = sata_default_pkt_time;
10293 
10294 	scmd = &spkt->satapkt_cmd;
10295 	scmd->satacmd_bp = bp;
10296 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
10297 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10298 
10299 	/* Build Identify Device cmd in the sata_pkt */
10300 	scmd->satacmd_addr_type = 0;		/* N/A */
10301 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
10302 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
10303 	scmd->satacmd_lba_mid_lsb = 0;		/* N/A */
10304 	scmd->satacmd_lba_high_lsb = 0;		/* N/A */
10305 	scmd->satacmd_features_reg = 0;		/* N/A */
10306 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
10307 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
10308 		/* Identify Packet Device cmd */
10309 		scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE;
10310 	} else {
10311 		/* Identify Device cmd - mandatory for all other devices */
10312 		scmd->satacmd_cmd_reg = SATAC_ID_DEVICE;
10313 	}
10314 
10315 	/* Send pkt to SATA HBA driver */
10316 	rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt);
10317 
10318 #ifdef SATA_INJECT_FAULTS
10319 	if (sata_inject_fault == SATA_INJECT_PKT_FAULT)
10320 		if (sata_fault_cmd == scmd->satacmd_cmd_reg)
10321 			sata_inject_pkt_fault(spkt, scmd->satacmd_cmd_reg,
10322 			    &rval, sata_inject_fault_type);
10323 #endif
10324 
10325 	if (rval == SATA_TRAN_ACCEPTED &&
10326 	    spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10327 		if (spx->txlt_buf_dma_handle != NULL) {
10328 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10329 			    DDI_DMA_SYNC_FORKERNEL);
10330 			ASSERT(rval == DDI_SUCCESS);
10331 		}
10332 		if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config &
10333 		    SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) {
10334 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10335 			    "SATA disk device at port %d - "
10336 			    "partial Identify Data",
10337 			    sdinfo->satadrv_addr.cport));
10338 			rval = SATA_RETRY; /* may retry later */
10339 			goto fail;
10340 		}
10341 		/* Update sata_drive_info */
10342 		bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id,
10343 		    sizeof (sata_id_t));
10344 
10345 		sdinfo->satadrv_features_support = 0;
10346 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10347 			/*
10348 			 * Retrieve capacity (disks only) and addressing mode
10349 			 */
10350 			sdinfo->satadrv_capacity = sata_check_capacity(sdinfo);
10351 		} else {
10352 			/*
10353 			 * For ATAPI devices one would have to issue
10354 			 * Get Capacity cmd for media capacity. Not here.
10355 			 */
10356 			sdinfo->satadrv_capacity = 0;
10357 			/*
10358 			 * Check what cdb length is supported
10359 			 */
10360 			if ((sdinfo->satadrv_id.ai_config &
10361 			    SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B)
10362 				sdinfo->satadrv_atapi_cdb_len = 16;
10363 			else
10364 				sdinfo->satadrv_atapi_cdb_len = 12;
10365 		}
10366 		/* Setup supported features flags */
10367 		if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT)
10368 			sdinfo->satadrv_features_support |= SATA_DEV_F_DMA;
10369 
10370 		/* Check for SATA GEN and NCQ support */
10371 		if (sdinfo->satadrv_id.ai_satacap != 0 &&
10372 		    sdinfo->satadrv_id.ai_satacap != 0xffff) {
10373 			/* SATA compliance */
10374 			if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ)
10375 				sdinfo->satadrv_features_support |=
10376 				    SATA_DEV_F_NCQ;
10377 			if (sdinfo->satadrv_id.ai_satacap &
10378 			    (SATA_1_SPEED | SATA_2_SPEED)) {
10379 				if (sdinfo->satadrv_id.ai_satacap &
10380 				    SATA_2_SPEED)
10381 					sdinfo->satadrv_features_support |=
10382 					    SATA_DEV_F_SATA2;
10383 				if (sdinfo->satadrv_id.ai_satacap &
10384 				    SATA_1_SPEED)
10385 					sdinfo->satadrv_features_support |=
10386 					    SATA_DEV_F_SATA1;
10387 			} else {
10388 				sdinfo->satadrv_features_support |=
10389 				    SATA_DEV_F_SATA1;
10390 			}
10391 		}
10392 		if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) &&
10393 		    (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD))
10394 			sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ;
10395 
10396 		sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth;
10397 		if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) ||
10398 		    (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) {
10399 			++sdinfo->satadrv_queue_depth;
10400 			/* Adjust according to controller capabilities */
10401 			sdinfo->satadrv_max_queue_depth = MIN(
10402 			    sdinfo->satadrv_queue_depth,
10403 			    SATA_QDEPTH(sata_hba_inst));
10404 			/* Adjust according to global queue depth limit */
10405 			sdinfo->satadrv_max_queue_depth = MIN(
10406 			    sdinfo->satadrv_max_queue_depth,
10407 			    sata_current_max_qdepth);
10408 			if (sdinfo->satadrv_max_queue_depth == 0)
10409 				sdinfo->satadrv_max_queue_depth = 1;
10410 		} else
10411 			sdinfo->satadrv_max_queue_depth = 1;
10412 
10413 		rval = SATA_SUCCESS;
10414 	} else {
10415 		/*
10416 		 * Woops, no Identify Data.
10417 		 */
10418 		if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) {
10419 			rval = SATA_RETRY; /* may retry later */
10420 		} else if (rval == SATA_TRAN_ACCEPTED) {
10421 			if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR ||
10422 			    spkt->satapkt_reason == SATA_PKT_ABORTED ||
10423 			    spkt->satapkt_reason == SATA_PKT_TIMEOUT ||
10424 			    spkt->satapkt_reason == SATA_PKT_RESET)
10425 				rval = SATA_RETRY; /* may retry later */
10426 			else
10427 				rval = SATA_FAILURE;
10428 		} else {
10429 			rval = SATA_FAILURE;
10430 		}
10431 	}
10432 fail:
10433 	/* Free allocated resources */
10434 	sata_free_local_buffer(spx);
10435 	sata_pkt_free(spx);
10436 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
10437 
10438 	return (rval);
10439 }
10440 
10441 
10442 /*
10443  * Some devices may not come-up with default DMA mode (UDMA or MWDMA).
10444  * UDMA mode is checked first, followed by MWDMA mode.
10445  * set correctly, so this function is setting it to the highest supported level.
10446  * Older SATA spec required that the device supports at least DMA 4 mode and
10447  * UDMA mode is selected.  It is not mentioned in SerialATA 2.6, so this
10448  * restriction has been removed.
10449  *
10450  * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported.
10451  * Returns SATA_FAILURE if proper DMA mode could not be selected.
10452  *
10453  * NOTE: This function should be called only if DMA mode is supported.
10454  */
10455 static int
10456 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo)
10457 {
10458 	sata_pkt_t *spkt;
10459 	sata_cmd_t *scmd;
10460 	sata_pkt_txlate_t *spx;
10461 	int i, mode;
10462 	uint8_t subcmd;
10463 	int rval = SATA_SUCCESS;
10464 
10465 	ASSERT(sdinfo != NULL);
10466 	ASSERT(sata_hba_inst != NULL);
10467 
10468 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
10469 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) {
10470 		/* Find highest Ultra DMA mode supported */
10471 		for (mode = 6; mode >= 0; --mode) {
10472 			if (sdinfo->satadrv_id.ai_ultradma & (1 << mode))
10473 				break;
10474 		}
10475 #if 0
10476 		/* Left for historical reasons */
10477 		/*
10478 		 * Some initial version of SATA spec indicated that at least
10479 		 * UDMA mode 4 has to be supported. It is not mentioned in
10480 		 * SerialATA 2.6, so this restriction is removed.
10481 		 */
10482 		if (mode < 4)
10483 			return (SATA_FAILURE);
10484 #endif
10485 		/* Find UDMA mode currently selected */
10486 		for (i = 6; i >= 0; --i) {
10487 			if (sdinfo->satadrv_id.ai_ultradma & (1 << (i + 8)))
10488 				break;
10489 		}
10490 		if (i >= mode)
10491 			/* Nothing to do */
10492 			return (SATA_SUCCESS);
10493 
10494 		subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA;
10495 
10496 	} else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) {
10497 		/* Find highest MultiWord DMA mode supported */
10498 		for (mode = 2; mode >= 0; --mode) {
10499 			if (sdinfo->satadrv_id.ai_dworddma & (1 << mode))
10500 				break;
10501 		}
10502 		/* Find highest MultiWord DMA mode selected */
10503 		for (i = 2; i >= 0; --i) {
10504 			if (sdinfo->satadrv_id.ai_dworddma & (1 << (i + 8)))
10505 				break;
10506 		}
10507 		if (i >= mode)
10508 			/* Nothing to do */
10509 			return (SATA_SUCCESS);
10510 
10511 		subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA;
10512 	} else
10513 		return (SATA_SUCCESS);
10514 
10515 	/*
10516 	 * Set DMA mode via SET FEATURES COMMAND.
10517 	 * Prepare packet for SET FEATURES COMMAND.
10518 	 */
10519 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10520 	spx->txlt_sata_hba_inst = sata_hba_inst;
10521 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10522 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10523 	if (spkt == NULL) {
10524 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10525 		    "sata_set_dma_mode: could not set DMA mode %", mode));
10526 		rval = SATA_FAILURE;
10527 		goto done;
10528 	}
10529 	/* Fill sata_pkt */
10530 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10531 	/* Timeout 30s */
10532 	spkt->satapkt_time = sata_default_pkt_time;
10533 	/* Synchronous mode, no callback, interrupts */
10534 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10535 	spkt->satapkt_comp = NULL;
10536 	scmd = &spkt->satapkt_cmd;
10537 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10538 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10539 	scmd->satacmd_addr_type = 0;
10540 	scmd->satacmd_device_reg = 0;
10541 	scmd->satacmd_status_reg = 0;
10542 	scmd->satacmd_error_reg = 0;
10543 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10544 	scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE;
10545 	scmd->satacmd_sec_count_lsb = subcmd | mode;
10546 
10547 	/* Transfer command to HBA */
10548 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
10549 	    spkt) != SATA_TRAN_ACCEPTED ||
10550 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
10551 		/* Pkt execution failed */
10552 		rval = SATA_FAILURE;
10553 	}
10554 done:
10555 
10556 	/* Free allocated resources */
10557 	if (spkt != NULL)
10558 		sata_pkt_free(spx);
10559 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10560 
10561 	return (rval);
10562 }
10563 
10564 
10565 /*
10566  * Set device caching mode.
10567  * One of the following operations should be specified:
10568  * SATAC_SF_ENABLE_READ_AHEAD
10569  * SATAC_SF_DISABLE_READ_AHEAD
10570  * SATAC_SF_ENABLE_WRITE_CACHE
10571  * SATAC_SF_DISABLE_WRITE_CACHE
10572  *
10573  * If operation fails, system log messgage is emitted.
10574  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
10575  */
10576 
10577 static int
10578 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
10579     int cache_op)
10580 {
10581 	sata_pkt_t *spkt;
10582 	sata_cmd_t *scmd;
10583 	sata_pkt_txlate_t *spx;
10584 	int rval = SATA_SUCCESS;
10585 	char *infop;
10586 
10587 	ASSERT(sdinfo != NULL);
10588 	ASSERT(sata_hba_inst != NULL);
10589 	ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD ||
10590 	    cache_op == SATAC_SF_DISABLE_READ_AHEAD ||
10591 	    cache_op == SATAC_SF_ENABLE_WRITE_CACHE ||
10592 	    cache_op == SATAC_SF_DISABLE_WRITE_CACHE);
10593 
10594 
10595 	/* Prepare packet for SET FEATURES COMMAND */
10596 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10597 	spx->txlt_sata_hba_inst = sata_hba_inst;
10598 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10599 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10600 	if (spkt == NULL) {
10601 		rval = SATA_FAILURE;
10602 		goto failure;
10603 	}
10604 	/* Fill sata_pkt */
10605 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10606 	/* Timeout 30s */
10607 	spkt->satapkt_time = sata_default_pkt_time;
10608 	/* Synchronous mode, no callback, interrupts */
10609 	spkt->satapkt_op_mode =
10610 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10611 	spkt->satapkt_comp = NULL;
10612 	scmd = &spkt->satapkt_cmd;
10613 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10614 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10615 	scmd->satacmd_addr_type = 0;
10616 	scmd->satacmd_device_reg = 0;
10617 	scmd->satacmd_status_reg = 0;
10618 	scmd->satacmd_error_reg = 0;
10619 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10620 	scmd->satacmd_features_reg = cache_op;
10621 
10622 	/* Transfer command to HBA */
10623 	if (((*SATA_START_FUNC(sata_hba_inst))(
10624 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
10625 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
10626 		/* Pkt execution failed */
10627 		switch (cache_op) {
10628 		case SATAC_SF_ENABLE_READ_AHEAD:
10629 			infop = "enabling read ahead failed";
10630 			break;
10631 		case SATAC_SF_DISABLE_READ_AHEAD:
10632 			infop = "disabling read ahead failed";
10633 			break;
10634 		case SATAC_SF_ENABLE_WRITE_CACHE:
10635 			infop = "enabling write cache failed";
10636 			break;
10637 		case SATAC_SF_DISABLE_WRITE_CACHE:
10638 			infop = "disabling write cache failed";
10639 			break;
10640 		}
10641 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
10642 		rval = SATA_FAILURE;
10643 	}
10644 failure:
10645 	/* Free allocated resources */
10646 	if (spkt != NULL)
10647 		sata_pkt_free(spx);
10648 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10649 	return (rval);
10650 }
10651 
10652 /*
10653  * Set Removable Media Status Notification (enable/disable)
10654  * state == 0 , disable
10655  * state != 0 , enable
10656  *
10657  * If operation fails, system log messgage is emitted.
10658  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
10659  */
10660 
10661 static int
10662 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
10663     int state)
10664 {
10665 	sata_pkt_t *spkt;
10666 	sata_cmd_t *scmd;
10667 	sata_pkt_txlate_t *spx;
10668 	int rval = SATA_SUCCESS;
10669 	char *infop;
10670 
10671 	ASSERT(sdinfo != NULL);
10672 	ASSERT(sata_hba_inst != NULL);
10673 
10674 	/* Prepare packet for SET FEATURES COMMAND */
10675 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10676 	spx->txlt_sata_hba_inst = sata_hba_inst;
10677 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10678 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10679 	if (spkt == NULL) {
10680 		rval = SATA_FAILURE;
10681 		goto failure;
10682 	}
10683 	/* Fill sata_pkt */
10684 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10685 	/* Timeout 30s */
10686 	spkt->satapkt_time = sata_default_pkt_time;
10687 	/* Synchronous mode, no callback, interrupts */
10688 	spkt->satapkt_op_mode =
10689 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10690 	spkt->satapkt_comp = NULL;
10691 	scmd = &spkt->satapkt_cmd;
10692 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10693 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10694 	scmd->satacmd_addr_type = 0;
10695 	scmd->satacmd_device_reg = 0;
10696 	scmd->satacmd_status_reg = 0;
10697 	scmd->satacmd_error_reg = 0;
10698 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10699 	if (state == 0)
10700 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN;
10701 	else
10702 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN;
10703 
10704 	/* Transfer command to HBA */
10705 	if (((*SATA_START_FUNC(sata_hba_inst))(
10706 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
10707 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
10708 		/* Pkt execution failed */
10709 		if (state == 0)
10710 			infop = "disabling Removable Media Status "
10711 			    "Notification failed";
10712 		else
10713 			infop = "enabling Removable Media Status "
10714 			    "Notification failed";
10715 
10716 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
10717 		rval = SATA_FAILURE;
10718 	}
10719 failure:
10720 	/* Free allocated resources */
10721 	if (spkt != NULL)
10722 		sata_pkt_free(spx);
10723 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10724 	return (rval);
10725 }
10726 
10727 
10728 /*
10729  * Update port SCR block
10730  */
10731 static void
10732 sata_update_port_scr(sata_port_scr_t *port_scr, sata_device_t *device)
10733 {
10734 	port_scr->sstatus = device->satadev_scr.sstatus;
10735 	port_scr->serror = device->satadev_scr.serror;
10736 	port_scr->scontrol = device->satadev_scr.scontrol;
10737 	port_scr->sactive = device->satadev_scr.sactive;
10738 	port_scr->snotific = device->satadev_scr.snotific;
10739 }
10740 
10741 /*
10742  * Update state and copy port ss* values from passed sata_device structure.
10743  * sata_address is validated - if not valid, nothing is changed in sata_scsi
10744  * configuration struct.
10745  *
10746  * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function
10747  * regardless of the state in device argument.
10748  *
10749  * Port mutex should be held while calling this function.
10750  */
10751 static void
10752 sata_update_port_info(sata_hba_inst_t *sata_hba_inst,
10753 	sata_device_t *sata_device)
10754 {
10755 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst,
10756 	    sata_device->satadev_addr.cport)));
10757 
10758 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT ||
10759 	    sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
10760 
10761 		sata_cport_info_t *cportinfo;
10762 
10763 		if (SATA_NUM_CPORTS(sata_hba_inst) <=
10764 		    sata_device->satadev_addr.cport)
10765 			return;
10766 
10767 		cportinfo = SATA_CPORT_INFO(sata_hba_inst,
10768 		    sata_device->satadev_addr.cport);
10769 		sata_update_port_scr(&cportinfo->cport_scr, sata_device);
10770 
10771 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
10772 		cportinfo->cport_state &= ~(SATA_PSTATE_PWRON |
10773 		    SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
10774 		cportinfo->cport_state |=
10775 		    sata_device->satadev_state & SATA_PSTATE_VALID;
10776 	} else {
10777 		sata_pmport_info_t *pmportinfo;
10778 
10779 		if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT) ||
10780 		    (sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) ||
10781 		    SATA_NUM_PMPORTS(sata_hba_inst,
10782 		    sata_device->satadev_addr.cport) <
10783 		    sata_device->satadev_addr.pmport)
10784 			return;
10785 
10786 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
10787 		    sata_device->satadev_addr.cport,
10788 		    sata_device->satadev_addr.pmport);
10789 		sata_update_port_scr(&pmportinfo->pmport_scr, sata_device);
10790 
10791 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
10792 		pmportinfo->pmport_state &=
10793 		    ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF |
10794 		    SATA_PSTATE_FAILED);
10795 		pmportinfo->pmport_state |=
10796 		    sata_device->satadev_state & SATA_PSTATE_VALID;
10797 	}
10798 }
10799 
10800 
10801 
10802 /*
10803  * Extract SATA port specification from an IOCTL argument.
10804  *
10805  * This function return the port the user land send us as is, unless it
10806  * cannot retrieve port spec, then -1 is returned.
10807  *
10808  * Note: Only cport  - no port multiplier port.
10809  */
10810 static int32_t
10811 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp)
10812 {
10813 	int32_t port;
10814 
10815 	/* Extract port number from nvpair in dca structure  */
10816 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
10817 		SATA_LOG_D((sata_hba_inst, CE_NOTE,
10818 		    "sata_get_port_num: invalid port spec 0x%x in ioctl",
10819 		    port));
10820 		port = -1;
10821 	}
10822 
10823 	return (port);
10824 }
10825 
10826 /*
10827  * Get dev_info_t pointer to the device node pointed to by port argument.
10828  * NOTE: target argument is a value used in ioctls to identify
10829  * the AP - it is not a sata_address.
10830  * It is a combination of cport, pmport and address qualifier, encodded same
10831  * way as a scsi target number.
10832  * At this moment it carries only cport number.
10833  *
10834  * No PMult hotplug support.
10835  *
10836  * Returns dev_info_t pointer if target device was found, NULL otherwise.
10837  */
10838 
10839 static dev_info_t *
10840 sata_get_target_dip(dev_info_t *dip, int32_t port)
10841 {
10842 	dev_info_t	*cdip = NULL;
10843 	int		target, tgt;
10844 	int		ncport;
10845 	int 		circ;
10846 
10847 	ncport = port & SATA_CFGA_CPORT_MASK;
10848 	target = SATA_TO_SCSI_TARGET(ncport, 0, SATA_ADDR_DCPORT);
10849 
10850 	ndi_devi_enter(dip, &circ);
10851 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
10852 		dev_info_t *next = ddi_get_next_sibling(cdip);
10853 
10854 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
10855 		    DDI_PROP_DONTPASS, "target", -1);
10856 		if (tgt == -1) {
10857 			/*
10858 			 * This is actually an error condition, but not
10859 			 * a fatal one. Just continue the search.
10860 			 */
10861 			cdip = next;
10862 			continue;
10863 		}
10864 
10865 		if (tgt == target)
10866 			break;
10867 
10868 		cdip = next;
10869 	}
10870 	ndi_devi_exit(dip, circ);
10871 
10872 	return (cdip);
10873 }
10874 
10875 /*
10876  * Get dev_info_t pointer to the device node pointed to by port argument.
10877  * NOTE: target argument is a value used in ioctls to identify
10878  * the AP - it is not a sata_address.
10879  * It is a combination of cport, pmport and address qualifier, encoded same
10880  * way as a scsi target number.
10881  * At this moment it carries only cport number.
10882  *
10883  * No PMult hotplug support.
10884  *
10885  * Returns dev_info_t pointer if target device was found, NULL otherwise.
10886  */
10887 
10888 static dev_info_t *
10889 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr)
10890 {
10891 	dev_info_t	*cdip = NULL;
10892 	int		target, tgt;
10893 	int 		circ;
10894 
10895 	target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual);
10896 
10897 	ndi_devi_enter(dip, &circ);
10898 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
10899 		dev_info_t *next = ddi_get_next_sibling(cdip);
10900 
10901 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
10902 		    DDI_PROP_DONTPASS, "target", -1);
10903 		if (tgt == -1) {
10904 			/*
10905 			 * This is actually an error condition, but not
10906 			 * a fatal one. Just continue the search.
10907 			 */
10908 			cdip = next;
10909 			continue;
10910 		}
10911 
10912 		if (tgt == target)
10913 			break;
10914 
10915 		cdip = next;
10916 	}
10917 	ndi_devi_exit(dip, circ);
10918 
10919 	return (cdip);
10920 }
10921 
10922 /*
10923  * Process sata port disconnect request.
10924  * Normally, cfgadm sata plugin will try to offline (unconfigure) the device
10925  * before this request. Nevertheless, if a device is still configured,
10926  * we need to attempt to offline and unconfigure device.
10927  * Regardless of the unconfigure operation results the port is marked as
10928  * deactivated and no access to the attached device is possible.
10929  * If the target node remains because unconfigure operation failed, its state
10930  * will be set to DEVICE_REMOVED, preventing it to be used again when a device
10931  * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure
10932  * the device and remove old target node.
10933  *
10934  * This function invokes sata_hba_inst->satahba_tran->
10935  * sata_tran_hotplug_ops->sata_tran_port_deactivate().
10936  * If successful, the device structure (if any) attached to the specified port
10937  * is removed and state of the port marked appropriately.
10938  * Failure of the port_deactivate may keep port in the physically active state,
10939  * or may fail the port.
10940  *
10941  * NOTE: Port multiplier code is not completed nor tested.
10942  */
10943 
10944 static int
10945 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst,
10946     sata_device_t *sata_device)
10947 {
10948 	sata_drive_info_t *sdinfo = NULL;
10949 	sata_cport_info_t *cportinfo = NULL;
10950 	sata_pmport_info_t *pmportinfo = NULL;
10951 	sata_pmult_info_t *pmultinfo = NULL;
10952 	dev_info_t *tdip;
10953 	int cport, pmport, qual;
10954 	int rval = SATA_SUCCESS;
10955 	int rv = 0;
10956 
10957 	cport = sata_device->satadev_addr.cport;
10958 	pmport = sata_device->satadev_addr.pmport;
10959 	qual = sata_device->satadev_addr.qual;
10960 
10961 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
10962 
10963 	/*
10964 	 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran->
10965 	 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
10966 	 * Do the sanity check.
10967 	 */
10968 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
10969 		/* No physical port deactivation supported. */
10970 		return (EINVAL);
10971 	}
10972 
10973 	/* Check the current state of the port */
10974 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10975 	    (SATA_DIP(sata_hba_inst), sata_device);
10976 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10977 	sata_update_port_info(sata_hba_inst, sata_device);
10978 	if (rval != SATA_SUCCESS ||
10979 	    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
10980 		/* Device port status is unknown or it is in failed state */
10981 		if (qual == SATA_ADDR_PMPORT) {
10982 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
10983 			    SATA_PSTATE_FAILED;
10984 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
10985 			    "sata_hba_ioctl: connect: failed to deactivate "
10986 			    "SATA port %d", cport);
10987 		} else {
10988 			SATA_CPORT_STATE(sata_hba_inst, cport) =
10989 			    SATA_PSTATE_FAILED;
10990 			SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
10991 			    "sata_hba_ioctl: connect: failed to deactivate "
10992 			    "SATA port %d:%d", cport, pmport);
10993 		}
10994 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
10995 		    cport)->cport_mutex);
10996 		return (EIO);
10997 	}
10998 	/*
10999 	 * Set port's dev_state to not ready - this will disable
11000 	 * an access to a potentially attached device.
11001 	 */
11002 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11003 	if (qual == SATA_ADDR_PMPORT) {
11004 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11005 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
11006 			sdinfo = pmportinfo->pmport_sata_drive;
11007 			ASSERT(sdinfo != NULL);
11008 		}
11009 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
11010 	} else {
11011 		/* Assuming cport */
11012 
11013 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
11014 			if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
11015 				pmultinfo =
11016 				    cportinfo->cport_devp.cport_sata_pmult;
11017 				ASSERT(pmultinfo != NULL);
11018 			} else {
11019 				sdinfo = cportinfo->cport_devp.cport_sata_drive;
11020 			}
11021 		}
11022 		cportinfo->cport_state &= ~SATA_STATE_READY;
11023 	}
11024 	if (sdinfo != NULL) {
11025 		if ((sdinfo->satadrv_type & (SATA_VALID_DEV_TYPE)) != 0) {
11026 			/*
11027 			 * If a target node exists, try to offline
11028 			 * a device and remove target node.
11029 			 */
11030 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11031 			    cport)->cport_mutex);
11032 			/* We are addressing attached device, not a port */
11033 			sata_device->satadev_addr.qual =
11034 			    sdinfo->satadrv_addr.qual;
11035 			tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11036 			    &sata_device->satadev_addr);
11037 			if (tdip != NULL && ndi_devi_offline(tdip,
11038 			    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
11039 				/*
11040 				 * Problem
11041 				 * The target node remained attached.
11042 				 * This happens when the device file was open
11043 				 * or a node was waiting for resources.
11044 				 * Cannot do anything about it.
11045 				 */
11046 				if (qual == SATA_ADDR_CPORT) {
11047 					SATA_LOG_D((sata_hba_inst, CE_WARN,
11048 					    "sata_hba_ioctl: disconnect: could "
11049 					    "not unconfigure device before "
11050 					    "disconnecting the SATA port %d",
11051 					    cport));
11052 				} else {
11053 					SATA_LOG_D((sata_hba_inst, CE_WARN,
11054 					    "sata_hba_ioctl: disconnect: could "
11055 					    "not unconfigure device before "
11056 					    "disconnecting the SATA port %d:%d",
11057 					    cport, pmport));
11058 				}
11059 				/*
11060 				 * Set DEVICE REMOVED state in the target
11061 				 * node. It will prevent access to the device
11062 				 * even when a new device is attached, until
11063 				 * the old target node is released, removed and
11064 				 * recreated for a new  device.
11065 				 */
11066 				sata_set_device_removed(tdip);
11067 
11068 				/*
11069 				 * Instruct event daemon to try the target
11070 				 * node cleanup later.
11071 				 */
11072 				sata_set_target_node_cleanup(
11073 				    sata_hba_inst, &sata_device->satadev_addr);
11074 			}
11075 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11076 			    cport)->cport_mutex);
11077 		}
11078 
11079 		/* Remove and release sata_drive info structure. */
11080 		if (pmportinfo != NULL) {
11081 			SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport) =
11082 			    NULL;
11083 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11084 		} else {
11085 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11086 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11087 		}
11088 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
11089 	}
11090 #if 0
11091 	else if (pmultinfo != NULL) {
11092 		/*
11093 		 * Port Multiplier itself needs special handling.
11094 		 * All device ports need to be processed here!
11095 		 */
11096 	}
11097 #endif
11098 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11099 	/* Just ask HBA driver to deactivate port */
11100 	/*	sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; */
11101 
11102 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
11103 	    (SATA_DIP(sata_hba_inst), sata_device);
11104 
11105 	/*
11106 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11107 	 * without the hint (to force listener to investivate the state).
11108 	 */
11109 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11110 	    SE_NO_HINT);
11111 
11112 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11113 	sata_update_port_info(sata_hba_inst, sata_device);
11114 
11115 	if (rval != SATA_SUCCESS) {
11116 		/*
11117 		 * Port deactivation failure - do not
11118 		 * change port state unless the state
11119 		 * returned by HBA indicates a port failure.
11120 		 * NOTE: device structures were released, so devices now are
11121 		 * invisible! Port reset is needed to re-enumerate devices.
11122 		 */
11123 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11124 			if (pmportinfo != NULL)
11125 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
11126 			else
11127 				cportinfo->cport_state = SATA_PSTATE_FAILED;
11128 			rv = EIO;
11129 		}
11130 	} else {
11131 		/*
11132 		 * Deactivation succeded. From now on the sata framework
11133 		 * will not care what is happening to the device, until
11134 		 * the port is activated again.
11135 		 */
11136 		cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
11137 	}
11138 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11139 	return (rv);
11140 }
11141 
11142 
11143 
11144 /*
11145  * Process sata port connect request
11146  * The sata cfgadm pluging will invoke this operation only if port was found
11147  * in the disconnect state (failed state is also treated as the disconnected
11148  * state).
11149  * DEVCTL_AP_CONNECT would invoke  sata_hba_inst->satahba_tran->
11150  * sata_tran_hotplug_ops->sata_tran_port_activate().
11151  * If successful and a device is found attached to the port,
11152  * the initialization sequence is executed to attach a device structure to
11153  * a port structure. The state of the port and a device would be set
11154  * appropriately.
11155  * The device is not set in configured state (system-wise) by this operation.
11156  *
11157  * Note, that activating the port may generate link events,
11158  * so it is important that following processing and the
11159  * event processing does not interfere with each other!
11160  *
11161  * This operation may remove port failed state and will
11162  * try to make port active and in good standing.
11163  *
11164  * NOTE: Port multiplier code is not completed nor tested.
11165  */
11166 
11167 static int
11168 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst,
11169     sata_device_t *sata_device)
11170 {
11171 	int cport, pmport, qual;
11172 	int rv = 0;
11173 
11174 	cport = sata_device->satadev_addr.cport;
11175 	pmport = sata_device->satadev_addr.pmport;
11176 	qual = sata_device->satadev_addr.qual;
11177 
11178 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
11179 
11180 	/*
11181 	 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->
11182 	 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate().
11183 	 * Perform sanity check now.
11184 	 */
11185 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) {
11186 		/* No physical port activation supported. */
11187 		return (EINVAL);
11188 	}
11189 
11190 	/* Just ask HBA driver to activate port */
11191 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11192 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11193 		/*
11194 		 * Port activation failure.
11195 		 */
11196 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11197 		    cport)->cport_mutex);
11198 		sata_update_port_info(sata_hba_inst, sata_device);
11199 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11200 			if (qual == SATA_ADDR_DCPORT) {
11201 				SATA_CPORT_STATE(sata_hba_inst, cport) =
11202 				    SATA_PSTATE_FAILED;
11203 				SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
11204 				    "sata_hba_ioctl: connect: failed to "
11205 				    "activate SATA port %d", cport);
11206 			} else { /* port multiplier device port */
11207 				SATA_PMPORT_STATE(sata_hba_inst, cport,
11208 				    pmport) = SATA_PSTATE_FAILED;
11209 				SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
11210 				    "sata_hba_ioctl: connect: failed to "
11211 				    "activate SATA port %d:%d", cport, pmport);
11212 
11213 			}
11214 		}
11215 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11216 		    cport)->cport_mutex);
11217 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
11218 		    "sata_hba_ioctl: connect: failed to activate SATA "
11219 		    "port %d:%d", cport, pmport);
11220 		return (EIO);
11221 	}
11222 
11223 	/* Virgin port state - will be updated by the port re-probe. */
11224 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11225 	if (qual == SATA_ADDR_CPORT)
11226 		SATA_CPORT_STATE(sata_hba_inst, cport) = 0;
11227 	else /* port multiplier device port */
11228 		SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0;
11229 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11230 
11231 	/*
11232 	 * Probe the port to find its state and attached device.
11233 	 */
11234 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11235 	    SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE)
11236 		rv = EIO;
11237 
11238 	/*
11239 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11240 	 * without the hint
11241 	 */
11242 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11243 	    SE_NO_HINT);
11244 
11245 	/*
11246 	 * If there is a device attached to the port, emit
11247 	 * a message.
11248 	 */
11249 	if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11250 
11251 		if (qual == SATA_ADDR_CPORT) {
11252 			sata_log(sata_hba_inst, CE_WARN,
11253 			    "SATA device detected at port %d", cport);
11254 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11255 				/*
11256 				 * A device was not successfully identified
11257 				 */
11258 				sata_log(sata_hba_inst, CE_WARN,
11259 				    "Could not identify SATA "
11260 				    "device at port %d", cport);
11261 			}
11262 		} else { /* port multiplier device port */
11263 			sata_log(sata_hba_inst, CE_WARN,
11264 			    "SATA device detected at port %d:%d",
11265 			    cport, pmport);
11266 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11267 				/*
11268 				 * A device was not successfully identified
11269 				 */
11270 				sata_log(sata_hba_inst, CE_WARN,
11271 				    "Could not identify SATA "
11272 				    "device at port %d:%d", cport, pmport);
11273 			}
11274 		}
11275 	}
11276 
11277 	return (rv);
11278 }
11279 
11280 
11281 /*
11282  * Process sata device unconfigure request.
11283  * The unconfigure operation uses generic nexus operation to
11284  * offline a device. It leaves a target device node attached.
11285  * and obviously sata_drive_info attached as well, because
11286  * from the hardware point of view nothing has changed.
11287  */
11288 static int
11289 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst,
11290     sata_device_t *sata_device)
11291 {
11292 	int rv = 0;
11293 	dev_info_t *tdip;
11294 
11295 	/* We are addressing attached device, not a port */
11296 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
11297 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11298 	else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT)
11299 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11300 
11301 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11302 	    &sata_device->satadev_addr)) != NULL) {
11303 
11304 		if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) {
11305 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11306 			    "sata_hba_ioctl: unconfigure: "
11307 			    "failed to unconfigure device at SATA port %d:%d",
11308 			    sata_device->satadev_addr.cport,
11309 			    sata_device->satadev_addr.pmport));
11310 			rv = EIO;
11311 		}
11312 		/*
11313 		 * The target node devi_state should be marked with
11314 		 * DEVI_DEVICE_OFFLINE by ndi_devi_offline().
11315 		 * This would be the indication for cfgadm that
11316 		 * the AP node occupant state is 'unconfigured'.
11317 		 */
11318 
11319 	} else {
11320 		/*
11321 		 * This would indicate a failure on the part of cfgadm
11322 		 * to detect correct state of the node prior to this
11323 		 * call - one cannot unconfigure non-existing device.
11324 		 */
11325 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11326 		    "sata_hba_ioctl: unconfigure: "
11327 		    "attempt to unconfigure non-existing device "
11328 		    "at SATA port %d:%d",
11329 		    sata_device->satadev_addr.cport,
11330 		    sata_device->satadev_addr.pmport));
11331 		rv = ENXIO;
11332 	}
11333 	return (rv);
11334 }
11335 
11336 /*
11337  * Process sata device configure request
11338  * If port is in a failed state, operation is aborted - one has to use
11339  * an explicit connect or port activate request to try to get a port into
11340  * non-failed mode. Port reset wil also work in such situation.
11341  * If the port is in disconnected (shutdown) state, the connect operation is
11342  * attempted prior to any other action.
11343  * When port is in the active state, there is a device attached and the target
11344  * node exists, a device was most likely offlined.
11345  * If target node does not exist, a new target node is created. In both cases
11346  * an attempt is made to online (configure) the device.
11347  *
11348  * NOTE: Port multiplier code is not completed nor tested.
11349  */
11350 static int
11351 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst,
11352     sata_device_t *sata_device)
11353 {
11354 	int cport, pmport, qual;
11355 	int rval;
11356 	boolean_t target = TRUE;
11357 	sata_cport_info_t *cportinfo;
11358 	sata_pmport_info_t *pmportinfo = NULL;
11359 	dev_info_t *tdip;
11360 	sata_drive_info_t *sdinfo;
11361 
11362 	cport = sata_device->satadev_addr.cport;
11363 	pmport = sata_device->satadev_addr.pmport;
11364 	qual = sata_device->satadev_addr.qual;
11365 
11366 	/* Get current port state */
11367 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11368 	    (SATA_DIP(sata_hba_inst), sata_device);
11369 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11370 	sata_update_port_info(sata_hba_inst, sata_device);
11371 
11372 	if (rval != SATA_SUCCESS ||
11373 	    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
11374 		/*
11375 		 * Obviously, device on a failed port is not visible
11376 		 */
11377 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11378 		return (ENXIO);
11379 	}
11380 
11381 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11382 	if (qual == SATA_ADDR_PMPORT)
11383 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11384 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11385 
11386 	if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) {
11387 		/* need to activate port */
11388 		target = FALSE;
11389 
11390 		/* Sanity check */
11391 		if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
11392 			return (ENXIO);
11393 
11394 		/* Just let HBA driver to activate port */
11395 		if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11396 		    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11397 			/*
11398 			 * Port activation failure - do not change port state
11399 			 * unless the state returned by HBA indicates a port
11400 			 * failure.
11401 			 */
11402 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11403 			    cport)->cport_mutex);
11404 			sata_update_port_info(sata_hba_inst, sata_device);
11405 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11406 				if (qual == SATA_ADDR_PMPORT)
11407 					pmportinfo->pmport_state =
11408 					    SATA_PSTATE_FAILED;
11409 				else
11410 					cportinfo->cport_state =
11411 					    SATA_PSTATE_FAILED;
11412 			}
11413 			mutex_exit(&SATA_CPORT_INFO(
11414 			    sata_hba_inst, cport)->cport_mutex);
11415 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11416 			    "sata_hba_ioctl: configure: "
11417 			    "failed to activate SATA port %d:%d",
11418 			    cport, pmport));
11419 			return (EIO);
11420 		}
11421 		/*
11422 		 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11423 		 * without the hint.
11424 		 */
11425 		sata_gen_sysevent(sata_hba_inst,
11426 		    &sata_device->satadev_addr, SE_NO_HINT);
11427 
11428 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11429 		    cport_mutex);
11430 		/* Virgin port state */
11431 		if (qual == SATA_ADDR_PMPORT)
11432 			pmportinfo->pmport_state = 0;
11433 		else
11434 			cportinfo->cport_state = 0;
11435 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11436 	}
11437 	/*
11438 	 * Always reprobe port, to get current device info.
11439 	 */
11440 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11441 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
11442 		return (EIO);
11443 
11444 	if (sata_device->satadev_type != SATA_DTYPE_NONE && target == FALSE) {
11445 		if (qual == SATA_ADDR_PMPORT) {
11446 			/*
11447 			 * That's the transition from "inactive" port
11448 			 * to active one with device attached.
11449 			 */
11450 			sata_log(sata_hba_inst, CE_WARN,
11451 			    "SATA device detected at port %d:%d",
11452 			    cport, pmport);
11453 		} else {
11454 			/*
11455 			 * When PM is attached to the cport and cport is
11456 			 * activated, every PM device port needs to be reprobed.
11457 			 * We need to emit message for all devices detected
11458 			 * at port multiplier's device ports.
11459 			 * Add such code here.
11460 			 * For now, just inform about device attached to
11461 			 * cport.
11462 			 */
11463 			sata_log(sata_hba_inst, CE_WARN,
11464 			    "SATA device detected at port %d", cport);
11465 		}
11466 	}
11467 
11468 	/*
11469 	 * This is where real configuration operation starts.
11470 	 *
11471 	 * When PM is attached to the cport and cport is activated,
11472 	 * devices attached PM device ports may have to be configured
11473 	 * explicitly. This may change when port multiplier is supported.
11474 	 * For now, configure only disks and other valid target devices.
11475 	 */
11476 	if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) {
11477 		if (qual == SATA_ADDR_CPORT) {
11478 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11479 				/*
11480 				 * A device was not successfully identified
11481 				 */
11482 				sata_log(sata_hba_inst, CE_WARN,
11483 				    "Could not identify SATA "
11484 				    "device at port %d", cport);
11485 			}
11486 		} else { /* port multiplier device port */
11487 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11488 				/*
11489 				 * A device was not successfully identified
11490 				 */
11491 				sata_log(sata_hba_inst, CE_WARN,
11492 				    "Could not identify SATA "
11493 				    "device at port %d:%d", cport, pmport);
11494 			}
11495 		}
11496 		return (ENXIO);		/* No device to configure */
11497 	}
11498 
11499 	/*
11500 	 * Here we may have a device in reset condition,
11501 	 * but because we are just configuring it, there is
11502 	 * no need to process the reset other than just
11503 	 * to clear device reset condition in the HBA driver.
11504 	 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will
11505 	 * cause a first command sent the HBA driver with the request
11506 	 * to clear device reset condition.
11507 	 */
11508 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11509 	if (qual == SATA_ADDR_PMPORT)
11510 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11511 	else
11512 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11513 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
11514 	if (sdinfo == NULL) {
11515 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11516 		return (ENXIO);
11517 	}
11518 	if (sdinfo->satadrv_event_flags &
11519 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
11520 		sdinfo->satadrv_event_flags = 0;
11521 	}
11522 	sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
11523 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11524 
11525 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11526 	    &sata_device->satadev_addr)) != NULL) {
11527 		/*
11528 		 * Target node exists. Verify, that it belongs
11529 		 * to existing, attached device and not to
11530 		 * a removed device.
11531 		 */
11532 		if (sata_check_device_removed(tdip) == B_TRUE) {
11533 			if (qual == SATA_ADDR_DPMPORT)
11534 				sata_log(sata_hba_inst, CE_WARN,
11535 				    "SATA device at port %d cannot be "
11536 				    "configured. "
11537 				    "Application(s) accessing "
11538 				    "previously attached device "
11539 				    "have to release it before newly "
11540 				    "inserted device can be made accessible.",
11541 				    cport);
11542 			else
11543 				sata_log(sata_hba_inst, CE_WARN,
11544 				    "SATA device at port %d:%d cannot be"
11545 				    "configured. "
11546 				    "Application(s) accessing "
11547 				    "previously attached device "
11548 				    "have to release it before newly "
11549 				    "inserted device can be made accessible.",
11550 				    cport, pmport);
11551 			return (EIO);
11552 		}
11553 		/*
11554 		 * Device was not removed and re-inserted.
11555 		 * Try to online it.
11556 		 */
11557 		if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) {
11558 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11559 			    "sata_hba_ioctl: configure: "
11560 			    "onlining device at SATA port "
11561 			    "%d:%d failed", cport, pmport));
11562 			return (EIO);
11563 		}
11564 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11565 		    cport)->cport_mutex);
11566 
11567 		if (qual == SATA_ADDR_DPMPORT)
11568 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
11569 		else
11570 			cportinfo-> cport_tgtnode_clean = B_TRUE;
11571 
11572 		mutex_exit(&SATA_CPORT_INFO(
11573 		    sata_hba_inst, cport)->cport_mutex);
11574 	} else {
11575 		/*
11576 		 * No target node - need to create a new target node.
11577 		 */
11578 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11579 		    cport_mutex);
11580 		if (qual == SATA_ADDR_DPMPORT)
11581 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
11582 		else
11583 			cportinfo-> cport_tgtnode_clean = B_TRUE;
11584 
11585 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11586 		    cport_mutex);
11587 		tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
11588 		    sata_hba_inst, &sata_device->satadev_addr);
11589 		if (tdip == NULL) {
11590 			/* Configure operation failed */
11591 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11592 			    "sata_hba_ioctl: configure: "
11593 			    "configuring SATA device at port %d:%d "
11594 			    "failed", cport, pmport));
11595 			return (EIO);
11596 		}
11597 	}
11598 	return (0);
11599 }
11600 
11601 
11602 /*
11603  * Process ioctl deactivate port request.
11604  * Arbitrarily unconfigure attached device, if any.
11605  * Even if the unconfigure fails, proceed with the
11606  * port deactivation.
11607  *
11608  * NOTE: Port Multiplier code is not completed and tested.
11609  */
11610 
11611 static int
11612 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst,
11613     sata_device_t *sata_device)
11614 {
11615 	int cport, pmport, qual;
11616 	int rval, rv = 0;
11617 	sata_cport_info_t *cportinfo;
11618 	sata_pmport_info_t *pmportinfo = NULL;
11619 	dev_info_t *tdip;
11620 	sata_drive_info_t *sdinfo = NULL;
11621 
11622 	/* Sanity check */
11623 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL)
11624 		return (ENOTSUP);
11625 
11626 	cport = sata_device->satadev_addr.cport;
11627 	pmport = sata_device->satadev_addr.pmport;
11628 	qual = sata_device->satadev_addr.qual;
11629 
11630 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11631 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11632 	if (qual == SATA_ADDR_CPORT) {
11633 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11634 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
11635 			/*
11636 			 * For now, assume that port multiplier is not
11637 			 * supported, i.e. deal only with valid devices
11638 			 */
11639 			if ((cportinfo->cport_dev_type &
11640 			    SATA_VALID_DEV_TYPE) != 0)
11641 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11642 			/*
11643 			 * If attached device is a port multiplier, we will
11644 			 * have to unconfigure all devices attached to the
11645 			 * port multiplier. Add this code here.
11646 			 */
11647 		}
11648 		cportinfo->cport_state &= ~SATA_STATE_READY;
11649 	} else {
11650 		/* Port multiplier device port */
11651 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11652 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11653 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
11654 		    (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0)
11655 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11656 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
11657 	}
11658 
11659 	if (sdinfo != NULL) {
11660 		/*
11661 		 * If a target node exists, try to offline a device and
11662 		 * to remove a target node.
11663 		 */
11664 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11665 		    cport_mutex);
11666 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11667 		    &sata_device->satadev_addr);
11668 		if (tdip != NULL) {
11669 			/* target node exist */
11670 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
11671 			    "sata_hba_ioctl: port deactivate: "
11672 			    "target node exists.", NULL);
11673 
11674 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) !=
11675 			    NDI_SUCCESS) {
11676 				SATA_LOG_D((sata_hba_inst, CE_WARN,
11677 				    "sata_hba_ioctl: port deactivate: "
11678 				    "failed to unconfigure device at port "
11679 				    "%d:%d before deactivating the port",
11680 				    cport, pmport));
11681 				/*
11682 				 * Set DEVICE REMOVED state in the target
11683 				 * node. It will prevent an access to
11684 				 * the device even when a new device is
11685 				 * attached, until the old target node is
11686 				 * released, removed and recreated for a new
11687 				 * device.
11688 				 */
11689 				sata_set_device_removed(tdip);
11690 
11691 				/*
11692 				 * Instruct the event daemon to try the
11693 				 * target node cleanup later.
11694 				 */
11695 				sata_set_target_node_cleanup(sata_hba_inst,
11696 				    &sata_device->satadev_addr);
11697 			}
11698 		}
11699 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11700 		    cport_mutex);
11701 		/*
11702 		 * In any case, remove and release sata_drive_info
11703 		 * structure.
11704 		 */
11705 		if (qual == SATA_ADDR_CPORT) {
11706 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11707 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11708 		} else { /* port multiplier device port */
11709 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11710 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11711 		}
11712 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
11713 	}
11714 	if (qual == SATA_ADDR_CPORT) {
11715 		cportinfo->cport_state &= ~(SATA_STATE_PROBED |
11716 		    SATA_STATE_PROBING);
11717 	} else { /* port multiplier device port */
11718 		pmportinfo->pmport_state &= ~(SATA_STATE_PROBED |
11719 		    SATA_STATE_PROBING);
11720 	}
11721 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11722 
11723 	/* Just let HBA driver to deactivate port */
11724 	sata_device->satadev_addr.qual = qual;
11725 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
11726 	    (SATA_DIP(sata_hba_inst), sata_device);
11727 
11728 	/*
11729 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11730 	 * without the hint
11731 	 */
11732 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11733 	    SE_NO_HINT);
11734 
11735 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11736 	sata_update_port_info(sata_hba_inst, sata_device);
11737 	if (qual == SATA_ADDR_CPORT) {
11738 		if (rval != SATA_SUCCESS) {
11739 			/*
11740 			 * Port deactivation failure - do not change port state
11741 			 * unless the state returned by HBA indicates a port
11742 			 * failure.
11743 			 */
11744 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11745 				SATA_CPORT_STATE(sata_hba_inst, cport) =
11746 				    SATA_PSTATE_FAILED;
11747 			}
11748 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11749 			    "sata_hba_ioctl: port deactivate: "
11750 			    "cannot deactivate SATA port %d", cport));
11751 			rv = EIO;
11752 		} else {
11753 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
11754 		}
11755 	} else {
11756 		if (rval != SATA_SUCCESS) {
11757 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11758 				SATA_PMPORT_STATE(sata_hba_inst, cport,
11759 				    pmport) = SATA_PSTATE_FAILED;
11760 			}
11761 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11762 			    "sata_hba_ioctl: port deactivate: "
11763 			    "cannot deactivate SATA port %d:%d",
11764 			    cport, pmport));
11765 			rv = EIO;
11766 		} else {
11767 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
11768 		}
11769 	}
11770 
11771 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11772 
11773 	return (rv);
11774 }
11775 
11776 /*
11777  * Process ioctl port activate request.
11778  *
11779  * NOTE: Port multiplier code is not completed nor tested.
11780  */
11781 static int
11782 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst,
11783     sata_device_t *sata_device)
11784 {
11785 	int cport, pmport, qual;
11786 	sata_cport_info_t *cportinfo;
11787 	sata_pmport_info_t *pmportinfo = NULL;
11788 	boolean_t dev_existed = TRUE;
11789 
11790 	/* Sanity check */
11791 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
11792 		return (ENOTSUP);
11793 
11794 	cport = sata_device->satadev_addr.cport;
11795 	pmport = sata_device->satadev_addr.pmport;
11796 	qual = sata_device->satadev_addr.qual;
11797 
11798 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11799 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11800 	if (qual == SATA_ADDR_PMPORT) {
11801 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11802 		if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN ||
11803 		    pmportinfo->pmport_dev_type == SATA_DTYPE_NONE)
11804 			dev_existed = FALSE;
11805 	} else { /* cport */
11806 		if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN ||
11807 		    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
11808 			dev_existed = FALSE;
11809 	}
11810 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11811 
11812 	/* Just let HBA driver to activate port, if necessary */
11813 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11814 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11815 		/*
11816 		 * Port activation failure - do not change port state unless
11817 		 * the state returned by HBA indicates a port failure.
11818 		 */
11819 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11820 		    cport)->cport_mutex);
11821 		sata_update_port_info(sata_hba_inst, sata_device);
11822 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11823 			if (qual == SATA_ADDR_PMPORT)
11824 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
11825 			else
11826 				cportinfo->cport_state = SATA_PSTATE_FAILED;
11827 
11828 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11829 			    cport)->cport_mutex);
11830 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11831 			    "sata_hba_ioctl: port activate: cannot activate "
11832 			    "SATA port %d:%d", cport, pmport));
11833 			return (EIO);
11834 		}
11835 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11836 	}
11837 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11838 	if (qual == SATA_ADDR_PMPORT)
11839 		pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN;
11840 	else
11841 		cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN;
11842 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11843 
11844 	/*
11845 	 * Re-probe port to find its current state and possibly attached device.
11846 	 * Port re-probing may change the cportinfo device type if device is
11847 	 * found attached.
11848 	 * If port probing failed, the device type would be set to
11849 	 * SATA_DTYPE_NONE.
11850 	 */
11851 	(void) sata_reprobe_port(sata_hba_inst, sata_device,
11852 	    SATA_DEV_IDENTIFY_RETRY);
11853 
11854 	/*
11855 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11856 	 * without the hint.
11857 	 */
11858 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11859 	    SE_NO_HINT);
11860 
11861 	if (dev_existed == FALSE) {
11862 		if (qual == SATA_ADDR_PMPORT &&
11863 		    pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
11864 			/*
11865 			 * That's the transition from the "inactive" port state
11866 			 * or the active port without a device attached to the
11867 			 * active port state with a device attached.
11868 			 */
11869 			sata_log(sata_hba_inst, CE_WARN,
11870 			    "SATA device detected at port %d:%d",
11871 			    cport, pmport);
11872 		} else if (qual == SATA_ADDR_CPORT &&
11873 		    cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
11874 			/*
11875 			 * That's the transition from the "inactive" port state
11876 			 * or the active port without a device attached to the
11877 			 * active port state with a device attached.
11878 			 */
11879 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
11880 				sata_log(sata_hba_inst, CE_WARN,
11881 				    "SATA device detected at port %d", cport);
11882 			} else {
11883 				sata_log(sata_hba_inst, CE_WARN,
11884 				    "SATA port multiplier detected at port %d",
11885 				    cport);
11886 				/*
11887 				 * Because the detected device is a port
11888 				 * multiplier, we need to reprobe every device
11889 				 * port on the port multiplier and show every
11890 				 * device found attached.
11891 				 * Add this code here.
11892 				 */
11893 			}
11894 		}
11895 	}
11896 	return (0);
11897 }
11898 
11899 
11900 
11901 /*
11902  * Process ioctl reset port request.
11903  *
11904  * NOTE: Port multiplier code is not completed nor tested.
11905  */
11906 static int
11907 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst,
11908     sata_device_t *sata_device)
11909 {
11910 	int cport, pmport, qual;
11911 	int rv = 0;
11912 
11913 	cport = sata_device->satadev_addr.cport;
11914 	pmport = sata_device->satadev_addr.pmport;
11915 	qual = sata_device->satadev_addr.qual;
11916 
11917 	/* Sanity check */
11918 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
11919 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11920 		    "sata_hba_ioctl: sata_hba_tran missing required "
11921 		    "function sata_tran_reset_dport"));
11922 		return (ENOTSUP);
11923 	}
11924 
11925 	/* Ask HBA to reset port */
11926 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
11927 	    sata_device) != SATA_SUCCESS) {
11928 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11929 		    "sata_hba_ioctl: reset port: failed %d:%d",
11930 		    cport, pmport));
11931 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11932 		    cport_mutex);
11933 		sata_update_port_info(sata_hba_inst, sata_device);
11934 		if (qual == SATA_ADDR_CPORT)
11935 			SATA_CPORT_STATE(sata_hba_inst, cport) =
11936 			    SATA_PSTATE_FAILED;
11937 		else
11938 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
11939 			    SATA_PSTATE_FAILED;
11940 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11941 		    cport_mutex);
11942 		rv = EIO;
11943 	}
11944 	/*
11945 	 * Beacuse the port was reset, it should be probed and
11946 	 * attached device reinitialized. At this point the
11947 	 * port state is unknown - it's state is HBA-specific.
11948 	 * Re-probe port to get its state.
11949 	 */
11950 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11951 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) {
11952 		rv = EIO;
11953 	}
11954 	return (rv);
11955 }
11956 
11957 /*
11958  * Process ioctl reset device request.
11959  *
11960  * NOTE: Port multiplier code is not completed nor tested.
11961  */
11962 static int
11963 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst,
11964     sata_device_t *sata_device)
11965 {
11966 	sata_drive_info_t *sdinfo;
11967 	int cport, pmport;
11968 	int rv = 0;
11969 
11970 	/* Sanity check */
11971 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
11972 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11973 		    "sata_hba_ioctl: sata_hba_tran missing required "
11974 		    "function sata_tran_reset_dport"));
11975 		return (ENOTSUP);
11976 	}
11977 
11978 	cport = sata_device->satadev_addr.cport;
11979 	pmport = sata_device->satadev_addr.pmport;
11980 
11981 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11982 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) {
11983 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11984 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
11985 		    sata_device->satadev_addr.cport);
11986 	} else { /* port multiplier */
11987 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11988 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
11989 		    sata_device->satadev_addr.cport,
11990 		    sata_device->satadev_addr.pmport);
11991 	}
11992 	if (sdinfo == NULL) {
11993 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11994 		return (EINVAL);
11995 	}
11996 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11997 
11998 	/* Ask HBA to reset device */
11999 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
12000 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
12001 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12002 		    "sata_hba_ioctl: reset device: failed at port %d:%d",
12003 		    cport, pmport));
12004 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12005 		    cport_mutex);
12006 		sata_update_port_info(sata_hba_inst, sata_device);
12007 		/*
12008 		 * Device info structure remains attached. Another device reset
12009 		 * or port disconnect/connect and re-probing is
12010 		 * needed to change it's state
12011 		 */
12012 		sdinfo->satadrv_state &= ~SATA_STATE_READY;
12013 		sdinfo->satadrv_state |= SATA_DSTATE_FAILED;
12014 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
12015 		rv = EIO;
12016 	}
12017 	/*
12018 	 * If attached device was a port multiplier, some extra processing
12019 	 * may be needed, to bring it back (if port re-probing did not handle
12020 	 * it). Add such code here.
12021 	 */
12022 	return (rv);
12023 }
12024 
12025 
12026 /*
12027  * Process ioctl reset all request.
12028  *
12029  * NOTE: Port multiplier code is not completed nor tested.
12030  */
12031 static int
12032 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst)
12033 {
12034 	sata_device_t sata_device;
12035 	int rv = 0;
12036 	int tcport;
12037 	int tpmport = 0;
12038 
12039 	sata_device.satadev_rev = SATA_DEVICE_REV;
12040 
12041 	/*
12042 	 * There is no protection here for configured devices.
12043 	 */
12044 	/* Sanity check */
12045 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
12046 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12047 		    "sata_hba_ioctl: sata_hba_tran missing required "
12048 		    "function sata_tran_reset_dport"));
12049 		return (ENOTSUP);
12050 	}
12051 
12052 	/*
12053 	 * Need to lock all ports, not just one.
12054 	 * If any port is locked by event processing, fail the whole operation.
12055 	 * One port is already locked, but for simplicity lock it again.
12056 	 */
12057 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
12058 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
12059 		    cport_mutex);
12060 		if (((SATA_CPORT_INFO(sata_hba_inst, tcport)->
12061 		    cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) {
12062 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
12063 			    cport_mutex);
12064 			rv = EBUSY;
12065 			break;
12066 		} else {
12067 			SATA_CPORT_INFO(sata_hba_inst, tcport)->
12068 			    cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
12069 			/*
12070 			 * If there is a port multiplier attached, we may need
12071 			 * to lock its port as well. If so, add such code here.
12072 			 */
12073 		}
12074 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
12075 		    cport_mutex);
12076 	}
12077 
12078 	if (rv == 0) {
12079 		/*
12080 		 * All cports were successfully locked.
12081 		 * Reset main SATA controller only for now - no PMult.
12082 		 * Set the device address to port 0, to have a valid device
12083 		 * address.
12084 		 */
12085 		sata_device.satadev_addr.qual = SATA_ADDR_CNTRL;
12086 		sata_device.satadev_addr.cport = 0;
12087 		sata_device.satadev_addr.pmport = 0;
12088 
12089 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
12090 		    (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) {
12091 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12092 			    "sata_hba_ioctl: reset controller failed"));
12093 			return (EIO);
12094 		}
12095 		/*
12096 		 * Because ports were reset, port states are unknown.
12097 		 * They should be re-probed to get their state and
12098 		 * attached devices should be reinitialized.
12099 		 * Add code here to re-probe port multiplier device ports.
12100 		 */
12101 		for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst);
12102 		    tcport++) {
12103 			sata_device.satadev_addr.cport = tcport;
12104 			sata_device.satadev_addr.pmport = tpmport;
12105 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
12106 
12107 			if (sata_reprobe_port(sata_hba_inst, &sata_device,
12108 			    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
12109 				rv = EIO;
12110 		}
12111 	}
12112 	/*
12113 	 * Unlock all ports
12114 	 */
12115 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
12116 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
12117 		    cport_mutex);
12118 		SATA_CPORT_INFO(sata_hba_inst, tcport)->
12119 		    cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
12120 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
12121 		    cport_mutex);
12122 	}
12123 
12124 	/*
12125 	 * This operation returns EFAULT if either reset
12126 	 * controller failed or a re-probing of any port failed.
12127 	 */
12128 	return (rv);
12129 }
12130 
12131 
12132 /*
12133  * Process ioctl port self test request.
12134  *
12135  * NOTE: Port multiplier code is not completed nor tested.
12136  */
12137 static int
12138 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst,
12139     sata_device_t *sata_device)
12140 {
12141 	int cport, pmport, qual;
12142 	int rv = 0;
12143 
12144 	/* Sanity check */
12145 	if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL)
12146 		return (ENOTSUP);
12147 
12148 	cport = sata_device->satadev_addr.cport;
12149 	pmport = sata_device->satadev_addr.pmport;
12150 	qual = sata_device->satadev_addr.qual;
12151 
12152 	/*
12153 	 * There is no protection here for a configured
12154 	 * device attached to this port.
12155 	 */
12156 
12157 	if ((*SATA_SELFTEST_FUNC(sata_hba_inst))
12158 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
12159 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12160 		    "sata_hba_ioctl: port selftest: "
12161 		    "failed port %d:%d", cport, pmport));
12162 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12163 		    cport_mutex);
12164 		sata_update_port_info(sata_hba_inst, sata_device);
12165 		if (qual == SATA_ADDR_CPORT)
12166 			SATA_CPORT_STATE(sata_hba_inst, cport) =
12167 			    SATA_PSTATE_FAILED;
12168 		else /* port ultiplier device port */
12169 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
12170 			    SATA_PSTATE_FAILED;
12171 
12172 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12173 		    cport_mutex);
12174 		return (EIO);
12175 	}
12176 	/*
12177 	 * Beacuse the port was reset in the course of testing, it should be
12178 	 * re-probed and attached device state should be restored. At this
12179 	 * point the port state is unknown - it's state is HBA-specific.
12180 	 * Force port re-probing to get it into a known state.
12181 	 */
12182 	if (sata_reprobe_port(sata_hba_inst, sata_device,
12183 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
12184 		rv = EIO;
12185 	return (rv);
12186 }
12187 
12188 
12189 /*
12190  * sata_cfgadm_state:
12191  * Use the sata port state and state of the target node to figure out
12192  * the cfgadm_state.
12193  *
12194  * The port argument is a value with encoded cport,
12195  * pmport and address qualifier, in the same manner as a scsi target number.
12196  * SCSI_TO_SATA_CPORT macro extracts cport number,
12197  * SCSI_TO_SATA_PMPORT extracts pmport number and
12198  * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag.
12199  *
12200  * For now, support is for cports only - no port multiplier device ports.
12201  */
12202 
12203 static void
12204 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port,
12205     devctl_ap_state_t *ap_state)
12206 {
12207 	uint16_t	cport;
12208 	int		port_state;
12209 	sata_drive_info_t *sdinfo;
12210 
12211 	/* Cport only */
12212 	cport = SCSI_TO_SATA_CPORT(port);
12213 
12214 	port_state = SATA_CPORT_STATE(sata_hba_inst, cport);
12215 	if (port_state & SATA_PSTATE_SHUTDOWN ||
12216 	    port_state & SATA_PSTATE_FAILED) {
12217 		ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
12218 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12219 		if (port_state & SATA_PSTATE_FAILED)
12220 			ap_state->ap_condition = AP_COND_FAILED;
12221 		else
12222 			ap_state->ap_condition = AP_COND_UNKNOWN;
12223 
12224 		return;
12225 	}
12226 
12227 	/* Need to check pmult device port here as well, when supported */
12228 
12229 	/* Port is enabled and ready */
12230 
12231 	switch (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport)) {
12232 	case SATA_DTYPE_NONE:
12233 	{
12234 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12235 		ap_state->ap_condition = AP_COND_OK;
12236 		/* No device attached */
12237 		ap_state->ap_rstate = AP_RSTATE_EMPTY;
12238 		break;
12239 	}
12240 	case SATA_DTYPE_UNKNOWN:
12241 	case SATA_DTYPE_ATAPINONCD:
12242 	case SATA_DTYPE_PMULT:	/* Until PMult is supported */
12243 	case SATA_DTYPE_ATADISK:
12244 	case SATA_DTYPE_ATAPICD:
12245 	{
12246 		dev_info_t *tdip = NULL;
12247 		dev_info_t *dip = NULL;
12248 		int circ;
12249 
12250 		dip = SATA_DIP(sata_hba_inst);
12251 		tdip = sata_get_target_dip(dip, port);
12252 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
12253 		if (tdip != NULL) {
12254 			ndi_devi_enter(dip, &circ);
12255 			mutex_enter(&(DEVI(tdip)->devi_lock));
12256 			if (DEVI_IS_DEVICE_REMOVED(tdip)) {
12257 				/*
12258 				 * There could be the case where previously
12259 				 * configured and opened device was removed
12260 				 * and unknown device was plugged.
12261 				 * In such case we want to show a device, and
12262 				 * its configured or unconfigured state but
12263 				 * indicate unusable condition untill the
12264 				 * old target node is released and removed.
12265 				 */
12266 				ap_state->ap_condition = AP_COND_UNUSABLE;
12267 			} else {
12268 				mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst,
12269 				    cport));
12270 				sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12271 				    cport);
12272 				if (sdinfo != NULL) {
12273 					if ((sdinfo->satadrv_state &
12274 					    SATA_DSTATE_FAILED) != 0)
12275 						ap_state->ap_condition =
12276 						    AP_COND_FAILED;
12277 					else
12278 						ap_state->ap_condition =
12279 						    AP_COND_OK;
12280 				} else {
12281 					ap_state->ap_condition =
12282 					    AP_COND_UNKNOWN;
12283 				}
12284 				mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst,
12285 				    cport));
12286 			}
12287 			if ((DEVI_IS_DEVICE_OFFLINE(tdip)) ||
12288 			    (DEVI_IS_DEVICE_DOWN(tdip))) {
12289 				ap_state->ap_ostate =
12290 				    AP_OSTATE_UNCONFIGURED;
12291 			} else {
12292 				ap_state->ap_ostate =
12293 				    AP_OSTATE_CONFIGURED;
12294 			}
12295 			mutex_exit(&(DEVI(tdip)->devi_lock));
12296 			ndi_devi_exit(dip, circ);
12297 		} else {
12298 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12299 			ap_state->ap_condition = AP_COND_UNKNOWN;
12300 		}
12301 		break;
12302 	}
12303 	default:
12304 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
12305 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12306 		ap_state->ap_condition = AP_COND_UNKNOWN;
12307 		/*
12308 		 * This is actually internal error condition (non fatal),
12309 		 * because we have already checked all defined device types.
12310 		 */
12311 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12312 		    "sata_cfgadm_state: Internal error: "
12313 		    "unknown device type"));
12314 		break;
12315 	}
12316 }
12317 
12318 
12319 /*
12320  * Process ioctl get device path request.
12321  *
12322  * NOTE: Port multiplier code is not completed nor tested.
12323  */
12324 static int
12325 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst,
12326     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12327 {
12328 	char path[MAXPATHLEN];
12329 	uint32_t size;
12330 	dev_info_t *tdip;
12331 
12332 	(void) strcpy(path, "/devices");
12333 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
12334 	    &sata_device->satadev_addr)) == NULL) {
12335 		/*
12336 		 * No such device. If this is a request for a size, do not
12337 		 * return EINVAL for non-existing target, because cfgadm
12338 		 * will then indicate a meaningless ioctl failure.
12339 		 * If this is a request for a path, indicate invalid
12340 		 * argument.
12341 		 */
12342 		if (ioc->get_size == 0)
12343 			return (EINVAL);
12344 	} else {
12345 		(void) ddi_pathname(tdip, path + strlen(path));
12346 	}
12347 	size = strlen(path) + 1;
12348 
12349 	if (ioc->get_size != 0) {
12350 		if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz,
12351 		    mode) != 0)
12352 			return (EFAULT);
12353 	} else {
12354 		if (ioc->bufsiz != size)
12355 			return (EINVAL);
12356 
12357 		else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz,
12358 		    mode) != 0)
12359 			return (EFAULT);
12360 	}
12361 	return (0);
12362 }
12363 
12364 /*
12365  * Process ioctl get attachment point type request.
12366  *
12367  * NOTE: Port multiplier code is not completed nor tested.
12368  */
12369 static	int
12370 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst,
12371     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12372 {
12373 	uint32_t	type_len;
12374 	const char	*ap_type;
12375 	int		dev_type;
12376 
12377 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12378 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst,
12379 		    sata_device->satadev_addr.cport);
12380 	else /* pmport */
12381 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12382 		    sata_device->satadev_addr.cport,
12383 		    sata_device->satadev_addr.pmport);
12384 
12385 	switch (dev_type) {
12386 	case SATA_DTYPE_NONE:
12387 		ap_type = "port";
12388 		break;
12389 
12390 	case SATA_DTYPE_ATADISK:
12391 		ap_type = "disk";
12392 		break;
12393 
12394 	case SATA_DTYPE_ATAPICD:
12395 		ap_type = "cd/dvd";
12396 		break;
12397 
12398 	case SATA_DTYPE_PMULT:
12399 		ap_type = "pmult";
12400 		break;
12401 
12402 	case SATA_DTYPE_UNKNOWN:
12403 		ap_type = "unknown";
12404 		break;
12405 
12406 	default:
12407 		ap_type = "unsupported";
12408 		break;
12409 
12410 	} /* end of dev_type switch */
12411 
12412 	type_len = strlen(ap_type) + 1;
12413 
12414 	if (ioc->get_size) {
12415 		if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz,
12416 		    mode) != 0)
12417 			return (EFAULT);
12418 	} else {
12419 		if (ioc->bufsiz != type_len)
12420 			return (EINVAL);
12421 
12422 		if (ddi_copyout((void *)ap_type, ioc->buf,
12423 		    ioc->bufsiz, mode) != 0)
12424 			return (EFAULT);
12425 	}
12426 	return (0);
12427 
12428 }
12429 
12430 /*
12431  * Process ioctl get device model info request.
12432  * This operation should return to cfgadm the device model
12433  * information string
12434  *
12435  * NOTE: Port multiplier code is not completed nor tested.
12436  */
12437 static	int
12438 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst,
12439     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12440 {
12441 	sata_drive_info_t *sdinfo;
12442 	uint32_t info_len;
12443 	char ap_info[SATA_ID_MODEL_LEN + 1];
12444 
12445 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12446 	    sata_device->satadev_addr.cport)->cport_mutex);
12447 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12448 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12449 		    sata_device->satadev_addr.cport);
12450 	else /* port multiplier */
12451 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12452 		    sata_device->satadev_addr.cport,
12453 		    sata_device->satadev_addr.pmport);
12454 	if (sdinfo == NULL) {
12455 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12456 		    sata_device->satadev_addr.cport)->cport_mutex);
12457 		return (EINVAL);
12458 	}
12459 
12460 #ifdef	_LITTLE_ENDIAN
12461 	swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
12462 #else	/* _LITTLE_ENDIAN */
12463 	bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
12464 #endif	/* _LITTLE_ENDIAN */
12465 
12466 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12467 	    sata_device->satadev_addr.cport)->cport_mutex);
12468 
12469 	ap_info[SATA_ID_MODEL_LEN] = '\0';
12470 
12471 	info_len = strlen(ap_info) + 1;
12472 
12473 	if (ioc->get_size) {
12474 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12475 		    mode) != 0)
12476 			return (EFAULT);
12477 	} else {
12478 		if (ioc->bufsiz < info_len)
12479 			return (EINVAL);
12480 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12481 		    mode) != 0)
12482 			return (EFAULT);
12483 	}
12484 	return (0);
12485 }
12486 
12487 
12488 /*
12489  * Process ioctl get device firmware revision info request.
12490  * This operation should return to cfgadm the device firmware revision
12491  * information string
12492  *
12493  * NOTE: Port multiplier code is not completed nor tested.
12494  */
12495 static	int
12496 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst,
12497     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12498 {
12499 	sata_drive_info_t *sdinfo;
12500 	uint32_t info_len;
12501 	char ap_info[SATA_ID_FW_LEN + 1];
12502 
12503 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12504 	    sata_device->satadev_addr.cport)->cport_mutex);
12505 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12506 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12507 		    sata_device->satadev_addr.cport);
12508 	else /* port multiplier */
12509 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12510 		    sata_device->satadev_addr.cport,
12511 		    sata_device->satadev_addr.pmport);
12512 	if (sdinfo == NULL) {
12513 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12514 		    sata_device->satadev_addr.cport)->cport_mutex);
12515 		return (EINVAL);
12516 	}
12517 
12518 #ifdef	_LITTLE_ENDIAN
12519 	swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
12520 #else	/* _LITTLE_ENDIAN */
12521 	bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
12522 #endif	/* _LITTLE_ENDIAN */
12523 
12524 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12525 	    sata_device->satadev_addr.cport)->cport_mutex);
12526 
12527 	ap_info[SATA_ID_FW_LEN] = '\0';
12528 
12529 	info_len = strlen(ap_info) + 1;
12530 
12531 	if (ioc->get_size) {
12532 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12533 		    mode) != 0)
12534 			return (EFAULT);
12535 	} else {
12536 		if (ioc->bufsiz < info_len)
12537 			return (EINVAL);
12538 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12539 		    mode) != 0)
12540 			return (EFAULT);
12541 	}
12542 	return (0);
12543 }
12544 
12545 
12546 /*
12547  * Process ioctl get device serial number info request.
12548  * This operation should return to cfgadm the device serial number string.
12549  *
12550  * NOTE: Port multiplier code is not completed nor tested.
12551  */
12552 static	int
12553 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst,
12554     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12555 {
12556 	sata_drive_info_t *sdinfo;
12557 	uint32_t info_len;
12558 	char ap_info[SATA_ID_SERIAL_LEN + 1];
12559 
12560 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12561 	    sata_device->satadev_addr.cport)->cport_mutex);
12562 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12563 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12564 		    sata_device->satadev_addr.cport);
12565 	else /* port multiplier */
12566 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12567 		    sata_device->satadev_addr.cport,
12568 		    sata_device->satadev_addr.pmport);
12569 	if (sdinfo == NULL) {
12570 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12571 		    sata_device->satadev_addr.cport)->cport_mutex);
12572 		return (EINVAL);
12573 	}
12574 
12575 #ifdef	_LITTLE_ENDIAN
12576 	swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
12577 #else	/* _LITTLE_ENDIAN */
12578 	bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
12579 #endif	/* _LITTLE_ENDIAN */
12580 
12581 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12582 	    sata_device->satadev_addr.cport)->cport_mutex);
12583 
12584 	ap_info[SATA_ID_SERIAL_LEN] = '\0';
12585 
12586 	info_len = strlen(ap_info) + 1;
12587 
12588 	if (ioc->get_size) {
12589 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12590 		    mode) != 0)
12591 			return (EFAULT);
12592 	} else {
12593 		if (ioc->bufsiz < info_len)
12594 			return (EINVAL);
12595 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12596 		    mode) != 0)
12597 			return (EFAULT);
12598 	}
12599 	return (0);
12600 }
12601 
12602 
12603 /*
12604  * Preset scsi extended sense data (to NO SENSE)
12605  * First 18 bytes of the sense data are preset to current valid sense
12606  * with a key NO SENSE data.
12607  *
12608  * Returns void
12609  */
12610 static void
12611 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense)
12612 {
12613 	sense->es_valid = 1;		/* Valid sense */
12614 	sense->es_class = CLASS_EXTENDED_SENSE;	/* 0x70 - current err */
12615 	sense->es_key = KEY_NO_SENSE;
12616 	sense->es_info_1 = 0;
12617 	sense->es_info_2 = 0;
12618 	sense->es_info_3 = 0;
12619 	sense->es_info_4 = 0;
12620 	sense->es_add_len = 10;	/* Additional length - replace with a def */
12621 	sense->es_cmd_info[0] = 0;
12622 	sense->es_cmd_info[1] = 0;
12623 	sense->es_cmd_info[2] = 0;
12624 	sense->es_cmd_info[3] = 0;
12625 	sense->es_add_code = 0;
12626 	sense->es_qual_code = 0;
12627 }
12628 
12629 /*
12630  * Register a legacy cmdk-style devid for the target (disk) device.
12631  *
12632  * Note: This function is called only when the HBA devinfo node has the
12633  * property "use-cmdk-devid-format" set. This property indicates that
12634  * devid compatible with old cmdk (target) driver is to be generated
12635  * for any target device attached to this controller. This will take
12636  * precedence over the devid generated by sd (target) driver.
12637  * This function is derived from cmdk_devid_setup() function in cmdk.c.
12638  */
12639 static void
12640 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo)
12641 {
12642 	char	*hwid;
12643 	int	modlen;
12644 	int	serlen;
12645 	int	rval;
12646 	ddi_devid_t	devid;
12647 
12648 	/*
12649 	 * device ID is a concatanation of model number, "=", serial number.
12650 	 */
12651 	hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP);
12652 	bcopy(&sdinfo->satadrv_id.ai_model, hwid,
12653 	    sizeof (sdinfo->satadrv_id.ai_model));
12654 	swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model));
12655 	modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model));
12656 	if (modlen == 0)
12657 		goto err;
12658 	hwid[modlen++] = '=';
12659 	bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen],
12660 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12661 	swab(&hwid[modlen], &hwid[modlen],
12662 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12663 	serlen = sata_check_modser(&hwid[modlen],
12664 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12665 	if (serlen == 0)
12666 		goto err;
12667 	hwid[modlen + serlen] = 0; /* terminate the hwid string */
12668 
12669 	/* initialize/register devid */
12670 	if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL,
12671 	    (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS)
12672 		rval = ddi_devid_register(dip, devid);
12673 
12674 	if (rval != DDI_SUCCESS)
12675 		cmn_err(CE_WARN, "sata: failed to create devid for the disk"
12676 		    " on port %d", sdinfo->satadrv_addr.cport);
12677 err:
12678 	kmem_free(hwid, LEGACY_HWID_LEN);
12679 }
12680 
12681 /*
12682  * valid model/serial string must contain a non-zero non-space characters.
12683  * trim trailing spaces/NULLs.
12684  */
12685 static int
12686 sata_check_modser(char *buf, int buf_len)
12687 {
12688 	boolean_t ret;
12689 	char *s;
12690 	int i;
12691 	int tb;
12692 	char ch;
12693 
12694 	ret = B_FALSE;
12695 	s = buf;
12696 	for (i = 0; i < buf_len; i++) {
12697 		ch = *s++;
12698 		if (ch != ' ' && ch != '\0')
12699 			tb = i + 1;
12700 		if (ch != ' ' && ch != '\0' && ch != '0')
12701 			ret = B_TRUE;
12702 	}
12703 
12704 	if (ret == B_FALSE)
12705 		return (0); /* invalid string */
12706 
12707 	return (tb); /* return length */
12708 }
12709 
12710 /*
12711  * sata_set_drive_features function compares current device features setting
12712  * with the saved device features settings and, if there is a difference,
12713  * it restores device features setting to the previously saved state.
12714  * It also arbitrarily tries to select the highest supported DMA mode.
12715  * Device Identify or Identify Packet Device data has to be current.
12716  * At the moment read ahead and write cache are considered for all devices.
12717  * For atapi devices, Removable Media Status Notification is set in addition
12718  * to common features.
12719  *
12720  * This function cannot be called in the interrupt context (it may sleep).
12721  *
12722  * The input argument sdinfo should point to the drive info structure
12723  * to be updated after features are set. Note, that only
12724  * device (packet) identify data is updated, not the flags indicating the
12725  * supported features.
12726  *
12727  * Returns TRUE if successful or there was nothing to do. Device Identify data
12728  * in the drive info structure pointed to by the sdinfo argumens is updated
12729  * even when no features were set or changed.
12730  *
12731  * Returns FALSE if device features could not be set.
12732  *
12733  * Note: This function may fail the port, making it inaccessible.
12734  * In such case the explicit port disconnect/connect or physical device
12735  * detach/attach is required to re-evaluate port state again.
12736  */
12737 
12738 static int
12739 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst,
12740     sata_drive_info_t *sdinfo, int restore)
12741 {
12742 	int rval = SATA_SUCCESS;
12743 	sata_drive_info_t new_sdinfo;
12744 	char *finfo = "sata_set_drive_features: cannot";
12745 	char *finfox;
12746 	int cache_op;
12747 
12748 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
12749 	new_sdinfo.satadrv_addr = sdinfo->satadrv_addr;
12750 	new_sdinfo.satadrv_type = sdinfo->satadrv_type;
12751 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
12752 		/*
12753 		 * Cannot get device identification - retry later
12754 		 */
12755 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12756 		    "%s fetch device identify data\n", finfo);
12757 		return (SATA_FAILURE);
12758 	}
12759 	finfox = (restore != 0) ? " restore device features" :
12760 	    " initialize device features\n";
12761 
12762 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12763 		/* Arbitrarily set UDMA mode */
12764 		if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
12765 		    SATA_SUCCESS) {
12766 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12767 			    "%s set UDMA mode\n", finfo));
12768 			return (SATA_FAILURE);
12769 		}
12770 	} else { /* Assume SATA ATAPI CD/DVD */
12771 		/*  Set Removable Media Status Notification, if necessary */
12772 		if ((new_sdinfo.satadrv_id.ai_cmdset83 &
12773 		    SATA_RM_STATUS_NOTIFIC) != 0 && restore != 0) {
12774 			if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) &&
12775 			    (!(new_sdinfo.satadrv_id.ai_features86 &
12776 			    SATA_RM_STATUS_NOTIFIC))) ||
12777 			    ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) &&
12778 			    (new_sdinfo.satadrv_id.ai_features86 &
12779 			    SATA_RM_STATUS_NOTIFIC))) {
12780 				/* Current setting does not match saved one */
12781 				if (sata_set_rmsn(sata_hba_inst, sdinfo,
12782 				    sdinfo->satadrv_settings &
12783 				    SATA_DEV_RMSN) != SATA_SUCCESS)
12784 					rval = SATA_FAILURE;
12785 			}
12786 		}
12787 		/*
12788 		 * We have to set Multiword DMA or UDMA, if it is supported, as
12789 		 * we want to use DMA transfer mode whenever possible.
12790 		 * Some devices require explicit setting of the DMA mode.
12791 		 */
12792 		if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) {
12793 			/* Set highest supported DMA mode */
12794 			if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
12795 			    SATA_SUCCESS) {
12796 				SATA_LOG_D((sata_hba_inst, CE_WARN,
12797 				    "%s set UDMA mode\n", finfo));
12798 				rval = SATA_FAILURE;
12799 			}
12800 		}
12801 	}
12802 
12803 	if (!(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) &&
12804 	    !(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) {
12805 		/* None of the features is supported - do nothing */
12806 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12807 		    "settable features not supported\n", NULL);
12808 		goto update_sdinfo;
12809 	}
12810 
12811 	if (((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) &&
12812 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) &&
12813 	    ((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) &&
12814 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
12815 		/* Nothing to do */
12816 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12817 		    "no device features to set\n", NULL);
12818 		goto update_sdinfo;
12819 	}
12820 
12821 	if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) &&
12822 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD))) {
12823 		if (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) {
12824 			/* Enable read ahead / read cache */
12825 			cache_op = SATAC_SF_ENABLE_READ_AHEAD;
12826 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12827 			    "enabling read cache\n", NULL);
12828 		} else {
12829 			/* Disable read ahead  / read cache */
12830 			cache_op = SATAC_SF_DISABLE_READ_AHEAD;
12831 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12832 			    "disabling read cache\n", NULL);
12833 		}
12834 
12835 		/* Try to set read cache mode */
12836 		if (sata_set_cache_mode(sata_hba_inst, &new_sdinfo,
12837 		    cache_op) != SATA_SUCCESS) {
12838 			/* Pkt execution failed */
12839 			rval = SATA_FAILURE;
12840 		}
12841 	}
12842 
12843 	if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) &&
12844 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
12845 		if (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) {
12846 			/* Enable write cache */
12847 			cache_op = SATAC_SF_ENABLE_WRITE_CACHE;
12848 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12849 			    "enabling write cache\n", NULL);
12850 		} else {
12851 			/* Disable write cache */
12852 			cache_op = SATAC_SF_DISABLE_WRITE_CACHE;
12853 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12854 			    "disabling write cache\n", NULL);
12855 		}
12856 		/* Try to set write cache mode */
12857 		if (sata_set_cache_mode(sata_hba_inst, &new_sdinfo,
12858 		    cache_op) != SATA_SUCCESS) {
12859 			/* Pkt execution failed */
12860 			rval = SATA_FAILURE;
12861 		}
12862 	}
12863 
12864 	if (rval == SATA_FAILURE)
12865 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12866 		    "%s %s", finfo, finfox));
12867 update_sdinfo:
12868 	/*
12869 	 * We need to fetch Device Identify data again
12870 	 */
12871 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
12872 		/*
12873 		 * Cannot get device identification - retry later
12874 		 */
12875 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12876 		    "%s re-fetch device identify data\n", finfo));
12877 		rval = SATA_FAILURE;
12878 	}
12879 	/* Copy device sata info. */
12880 	sdinfo->satadrv_id = new_sdinfo.satadrv_id;
12881 
12882 	return (rval);
12883 }
12884 
12885 
12886 /*
12887  *
12888  * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if
12889  * unable to determine.
12890  *
12891  * Cannot be called in an interrupt context.
12892  *
12893  * Called by sata_build_lsense_page_2f()
12894  */
12895 
12896 static int
12897 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst,
12898     sata_drive_info_t *sdinfo)
12899 {
12900 	sata_pkt_t *spkt;
12901 	sata_cmd_t *scmd;
12902 	sata_pkt_txlate_t *spx;
12903 	int rval;
12904 
12905 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12906 	spx->txlt_sata_hba_inst = sata_hba_inst;
12907 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12908 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12909 	if (spkt == NULL) {
12910 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12911 		return (-1);
12912 	}
12913 	/* address is needed now */
12914 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12915 
12916 
12917 	/* Fill sata_pkt */
12918 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12919 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12920 	/* Synchronous mode, no callback */
12921 	spkt->satapkt_comp = NULL;
12922 	/* Timeout 30s */
12923 	spkt->satapkt_time = sata_default_pkt_time;
12924 
12925 	scmd = &spkt->satapkt_cmd;
12926 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
12927 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
12928 
12929 	/* Set up which registers need to be returned */
12930 	scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE;
12931 	scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE;
12932 
12933 	/* Build SMART_RETURN_STATUS cmd in the sata_pkt */
12934 	scmd->satacmd_addr_type = 0;		/* N/A */
12935 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
12936 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
12937 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
12938 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
12939 	scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS;
12940 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12941 	scmd->satacmd_cmd_reg = SATAC_SMART;
12942 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12943 	    sdinfo->satadrv_addr.cport)));
12944 
12945 
12946 	/* Send pkt to SATA HBA driver */
12947 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12948 	    SATA_TRAN_ACCEPTED ||
12949 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12950 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12951 		    sdinfo->satadrv_addr.cport)));
12952 		/*
12953 		 * Whoops, no SMART RETURN STATUS
12954 		 */
12955 		rval = -1;
12956 	} else {
12957 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12958 		    sdinfo->satadrv_addr.cport)));
12959 		if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
12960 			rval = -1;
12961 			goto fail;
12962 		}
12963 		if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
12964 			rval = -1;
12965 			goto fail;
12966 		}
12967 		if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) &&
12968 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2))
12969 			rval = 0;
12970 		else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) &&
12971 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4))
12972 			rval = 1;
12973 		else {
12974 			rval = -1;
12975 			goto fail;
12976 		}
12977 	}
12978 fail:
12979 	/* Free allocated resources */
12980 	sata_pkt_free(spx);
12981 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12982 
12983 	return (rval);
12984 }
12985 
12986 /*
12987  *
12988  * Returns 0 if succeeded, -1 otherwise
12989  *
12990  * Cannot be called in an interrupt context.
12991  *
12992  */
12993 static int
12994 sata_fetch_smart_data(
12995 	sata_hba_inst_t *sata_hba_inst,
12996 	sata_drive_info_t *sdinfo,
12997 	struct smart_data *smart_data)
12998 {
12999 	sata_pkt_t *spkt;
13000 	sata_cmd_t *scmd;
13001 	sata_pkt_txlate_t *spx;
13002 	int rval;
13003 
13004 #if ! defined(lint)
13005 	ASSERT(sizeof (struct smart_data) == 512);
13006 #endif
13007 
13008 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13009 	spx->txlt_sata_hba_inst = sata_hba_inst;
13010 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13011 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13012 	if (spkt == NULL) {
13013 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13014 		return (-1);
13015 	}
13016 	/* address is needed now */
13017 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13018 
13019 
13020 	/* Fill sata_pkt */
13021 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13022 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13023 	/* Synchronous mode, no callback */
13024 	spkt->satapkt_comp = NULL;
13025 	/* Timeout 30s */
13026 	spkt->satapkt_time = sata_default_pkt_time;
13027 
13028 	scmd = &spkt->satapkt_cmd;
13029 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13030 
13031 	/*
13032 	 * Allocate buffer for SMART data
13033 	 */
13034 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13035 	    sizeof (struct smart_data));
13036 	if (scmd->satacmd_bp == NULL) {
13037 		sata_pkt_free(spx);
13038 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13039 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13040 		    "sata_fetch_smart_data: "
13041 		    "cannot allocate buffer"));
13042 		return (-1);
13043 	}
13044 
13045 
13046 	/* Build SMART_READ_DATA cmd in the sata_pkt */
13047 	scmd->satacmd_addr_type = 0;		/* N/A */
13048 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
13049 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
13050 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13051 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13052 	scmd->satacmd_features_reg = SATA_SMART_READ_DATA;
13053 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13054 	scmd->satacmd_cmd_reg = SATAC_SMART;
13055 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13056 	    sdinfo->satadrv_addr.cport)));
13057 
13058 	/* Send pkt to SATA HBA driver */
13059 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13060 	    SATA_TRAN_ACCEPTED ||
13061 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13062 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13063 		    sdinfo->satadrv_addr.cport)));
13064 		/*
13065 		 * Whoops, no SMART DATA available
13066 		 */
13067 		rval = -1;
13068 		goto fail;
13069 	} else {
13070 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13071 		    sdinfo->satadrv_addr.cport)));
13072 		if (spx->txlt_buf_dma_handle != NULL) {
13073 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13074 			    DDI_DMA_SYNC_FORKERNEL);
13075 			ASSERT(rval == DDI_SUCCESS);
13076 		}
13077 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data,
13078 		    sizeof (struct smart_data));
13079 	}
13080 
13081 fail:
13082 	/* Free allocated resources */
13083 	sata_free_local_buffer(spx);
13084 	sata_pkt_free(spx);
13085 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13086 
13087 	return (rval);
13088 }
13089 
13090 /*
13091  * Used by LOG SENSE page 0x10
13092  * Reads (in synchronous mode) the self test log data using Read Log Ext cmd.
13093  * Note: cannot be called in the interrupt context.
13094  *
13095  * return 0 for success, -1 otherwise
13096  *
13097  */
13098 static int
13099 sata_ext_smart_selftest_read_log(
13100 	sata_hba_inst_t *sata_hba_inst,
13101 	sata_drive_info_t *sdinfo,
13102 	struct smart_ext_selftest_log *ext_selftest_log,
13103 	uint16_t block_num)
13104 {
13105 	sata_pkt_txlate_t *spx;
13106 	sata_pkt_t *spkt;
13107 	sata_cmd_t *scmd;
13108 	int rval;
13109 
13110 #if ! defined(lint)
13111 	ASSERT(sizeof (struct smart_ext_selftest_log) == 512);
13112 #endif
13113 
13114 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13115 	spx->txlt_sata_hba_inst = sata_hba_inst;
13116 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13117 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13118 	if (spkt == NULL) {
13119 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13120 		return (-1);
13121 	}
13122 	/* address is needed now */
13123 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13124 
13125 
13126 	/* Fill sata_pkt */
13127 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13128 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13129 	/* Synchronous mode, no callback */
13130 	spkt->satapkt_comp = NULL;
13131 	/* Timeout 30s */
13132 	spkt->satapkt_time = sata_default_pkt_time;
13133 
13134 	scmd = &spkt->satapkt_cmd;
13135 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13136 
13137 	/*
13138 	 * Allocate buffer for SMART extended self-test log
13139 	 */
13140 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13141 	    sizeof (struct smart_ext_selftest_log));
13142 	if (scmd->satacmd_bp == NULL) {
13143 		sata_pkt_free(spx);
13144 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13145 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13146 		    "sata_ext_smart_selftest_log: "
13147 		    "cannot allocate buffer"));
13148 		return (-1);
13149 	}
13150 
13151 	/* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */
13152 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
13153 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of selftest log */
13154 	scmd->satacmd_sec_count_msb = 0;	/* One sector of selftest log */
13155 	scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE;
13156 	scmd->satacmd_lba_low_msb = 0;
13157 	scmd->satacmd_lba_mid_lsb = block_num & 0xff;
13158 	scmd->satacmd_lba_mid_msb = block_num >> 8;
13159 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13160 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
13161 
13162 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13163 	    sdinfo->satadrv_addr.cport)));
13164 
13165 	/* Send pkt to SATA HBA driver */
13166 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13167 	    SATA_TRAN_ACCEPTED ||
13168 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13169 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13170 		    sdinfo->satadrv_addr.cport)));
13171 
13172 		/*
13173 		 * Whoops, no SMART selftest log info available
13174 		 */
13175 		rval = -1;
13176 		goto fail;
13177 	} else {
13178 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13179 		    sdinfo->satadrv_addr.cport)));
13180 
13181 		if (spx->txlt_buf_dma_handle != NULL) {
13182 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13183 			    DDI_DMA_SYNC_FORKERNEL);
13184 			ASSERT(rval == DDI_SUCCESS);
13185 		}
13186 		bcopy(scmd->satacmd_bp->b_un.b_addr,
13187 		    (uint8_t *)ext_selftest_log,
13188 		    sizeof (struct smart_ext_selftest_log));
13189 		rval = 0;
13190 	}
13191 
13192 fail:
13193 	/* Free allocated resources */
13194 	sata_free_local_buffer(spx);
13195 	sata_pkt_free(spx);
13196 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13197 
13198 	return (rval);
13199 }
13200 
13201 /*
13202  * Returns 0 for success, -1 otherwise
13203  *
13204  * SMART self-test log data is returned in buffer pointed to by selftest_log
13205  */
13206 static int
13207 sata_smart_selftest_log(
13208 	sata_hba_inst_t *sata_hba_inst,
13209 	sata_drive_info_t *sdinfo,
13210 	struct smart_selftest_log *selftest_log)
13211 {
13212 	sata_pkt_t *spkt;
13213 	sata_cmd_t *scmd;
13214 	sata_pkt_txlate_t *spx;
13215 	int rval;
13216 
13217 #if ! defined(lint)
13218 	ASSERT(sizeof (struct smart_selftest_log) == 512);
13219 #endif
13220 
13221 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13222 	spx->txlt_sata_hba_inst = sata_hba_inst;
13223 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13224 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13225 	if (spkt == NULL) {
13226 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13227 		return (-1);
13228 	}
13229 	/* address is needed now */
13230 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13231 
13232 
13233 	/* Fill sata_pkt */
13234 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13235 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13236 	/* Synchronous mode, no callback */
13237 	spkt->satapkt_comp = NULL;
13238 	/* Timeout 30s */
13239 	spkt->satapkt_time = sata_default_pkt_time;
13240 
13241 	scmd = &spkt->satapkt_cmd;
13242 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13243 
13244 	/*
13245 	 * Allocate buffer for SMART SELFTEST LOG
13246 	 */
13247 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13248 	    sizeof (struct smart_selftest_log));
13249 	if (scmd->satacmd_bp == NULL) {
13250 		sata_pkt_free(spx);
13251 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13252 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13253 		    "sata_smart_selftest_log: "
13254 		    "cannot allocate buffer"));
13255 		return (-1);
13256 	}
13257 
13258 	/* Build SMART_READ_LOG cmd in the sata_pkt */
13259 	scmd->satacmd_addr_type = 0;		/* N/A */
13260 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of SMART log */
13261 	scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE;
13262 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13263 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13264 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
13265 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13266 	scmd->satacmd_cmd_reg = SATAC_SMART;
13267 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13268 	    sdinfo->satadrv_addr.cport)));
13269 
13270 	/* Send pkt to SATA HBA driver */
13271 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13272 	    SATA_TRAN_ACCEPTED ||
13273 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13274 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13275 		    sdinfo->satadrv_addr.cport)));
13276 		/*
13277 		 * Whoops, no SMART DATA available
13278 		 */
13279 		rval = -1;
13280 		goto fail;
13281 	} else {
13282 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13283 		    sdinfo->satadrv_addr.cport)));
13284 		if (spx->txlt_buf_dma_handle != NULL) {
13285 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13286 			    DDI_DMA_SYNC_FORKERNEL);
13287 			ASSERT(rval == DDI_SUCCESS);
13288 		}
13289 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log,
13290 		    sizeof (struct smart_selftest_log));
13291 		rval = 0;
13292 	}
13293 
13294 fail:
13295 	/* Free allocated resources */
13296 	sata_free_local_buffer(spx);
13297 	sata_pkt_free(spx);
13298 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13299 
13300 	return (rval);
13301 }
13302 
13303 
13304 /*
13305  * Returns 0 for success, -1 otherwise
13306  *
13307  * SMART READ LOG data is returned in buffer pointed to by smart_log
13308  */
13309 static int
13310 sata_smart_read_log(
13311 	sata_hba_inst_t *sata_hba_inst,
13312 	sata_drive_info_t *sdinfo,
13313 	uint8_t *smart_log,		/* where the data should be returned */
13314 	uint8_t which_log,		/* which log should be returned */
13315 	uint8_t log_size)		/* # of 512 bytes in log */
13316 {
13317 	sata_pkt_t *spkt;
13318 	sata_cmd_t *scmd;
13319 	sata_pkt_txlate_t *spx;
13320 	int rval;
13321 
13322 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13323 	spx->txlt_sata_hba_inst = sata_hba_inst;
13324 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13325 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13326 	if (spkt == NULL) {
13327 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13328 		return (-1);
13329 	}
13330 	/* address is needed now */
13331 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13332 
13333 
13334 	/* Fill sata_pkt */
13335 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13336 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13337 	/* Synchronous mode, no callback */
13338 	spkt->satapkt_comp = NULL;
13339 	/* Timeout 30s */
13340 	spkt->satapkt_time = sata_default_pkt_time;
13341 
13342 	scmd = &spkt->satapkt_cmd;
13343 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13344 
13345 	/*
13346 	 * Allocate buffer for SMART READ LOG
13347 	 */
13348 	scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512);
13349 	if (scmd->satacmd_bp == NULL) {
13350 		sata_pkt_free(spx);
13351 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13352 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13353 		    "sata_smart_read_log: " "cannot allocate buffer"));
13354 		return (-1);
13355 	}
13356 
13357 	/* Build SMART_READ_LOG cmd in the sata_pkt */
13358 	scmd->satacmd_addr_type = 0;		/* N/A */
13359 	scmd->satacmd_sec_count_lsb = log_size;	/* what the caller asked for */
13360 	scmd->satacmd_lba_low_lsb = which_log;	/* which log page */
13361 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13362 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13363 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
13364 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13365 	scmd->satacmd_cmd_reg = SATAC_SMART;
13366 
13367 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13368 	    sdinfo->satadrv_addr.cport)));
13369 
13370 	/* Send pkt to SATA HBA driver */
13371 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13372 	    SATA_TRAN_ACCEPTED ||
13373 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13374 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13375 		    sdinfo->satadrv_addr.cport)));
13376 
13377 		/*
13378 		 * Whoops, no SMART DATA available
13379 		 */
13380 		rval = -1;
13381 		goto fail;
13382 	} else {
13383 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13384 		    sdinfo->satadrv_addr.cport)));
13385 
13386 		if (spx->txlt_buf_dma_handle != NULL) {
13387 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13388 			    DDI_DMA_SYNC_FORKERNEL);
13389 			ASSERT(rval == DDI_SUCCESS);
13390 		}
13391 		bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512);
13392 		rval = 0;
13393 	}
13394 
13395 fail:
13396 	/* Free allocated resources */
13397 	sata_free_local_buffer(spx);
13398 	sata_pkt_free(spx);
13399 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13400 
13401 	return (rval);
13402 }
13403 
13404 /*
13405  * Used by LOG SENSE page 0x10
13406  *
13407  * return 0 for success, -1 otherwise
13408  *
13409  */
13410 static int
13411 sata_read_log_ext_directory(
13412 	sata_hba_inst_t *sata_hba_inst,
13413 	sata_drive_info_t *sdinfo,
13414 	struct read_log_ext_directory *logdir)
13415 {
13416 	sata_pkt_txlate_t *spx;
13417 	sata_pkt_t *spkt;
13418 	sata_cmd_t *scmd;
13419 	int rval;
13420 
13421 #if ! defined(lint)
13422 	ASSERT(sizeof (struct read_log_ext_directory) == 512);
13423 #endif
13424 
13425 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13426 	spx->txlt_sata_hba_inst = sata_hba_inst;
13427 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13428 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13429 	if (spkt == NULL) {
13430 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13431 		return (-1);
13432 	}
13433 
13434 	/* Fill sata_pkt */
13435 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13436 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13437 	/* Synchronous mode, no callback */
13438 	spkt->satapkt_comp = NULL;
13439 	/* Timeout 30s */
13440 	spkt->satapkt_time = sata_default_pkt_time;
13441 
13442 	scmd = &spkt->satapkt_cmd;
13443 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13444 
13445 	/*
13446 	 * Allocate buffer for SMART READ LOG EXTENDED command
13447 	 */
13448 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13449 	    sizeof (struct read_log_ext_directory));
13450 	if (scmd->satacmd_bp == NULL) {
13451 		sata_pkt_free(spx);
13452 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13453 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13454 		    "sata_read_log_ext_directory: "
13455 		    "cannot allocate buffer"));
13456 		return (-1);
13457 	}
13458 
13459 	/* Build READ LOG EXT w/ log directory cmd in the  sata_pkt */
13460 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
13461 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of directory */
13462 	scmd->satacmd_sec_count_msb = 0;	/* One sector of directory */
13463 	scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY;
13464 	scmd->satacmd_lba_low_msb = 0;
13465 	scmd->satacmd_lba_mid_lsb = 0;
13466 	scmd->satacmd_lba_mid_msb = 0;
13467 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13468 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
13469 
13470 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13471 	    sdinfo->satadrv_addr.cport)));
13472 
13473 	/* Send pkt to SATA HBA driver */
13474 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13475 	    SATA_TRAN_ACCEPTED ||
13476 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13477 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13478 		    sdinfo->satadrv_addr.cport)));
13479 		/*
13480 		 * Whoops, no SMART selftest log info available
13481 		 */
13482 		rval = -1;
13483 		goto fail;
13484 	} else {
13485 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13486 		    sdinfo->satadrv_addr.cport)));
13487 		if (spx->txlt_buf_dma_handle != NULL) {
13488 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13489 			    DDI_DMA_SYNC_FORKERNEL);
13490 			ASSERT(rval == DDI_SUCCESS);
13491 		}
13492 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir,
13493 		    sizeof (struct read_log_ext_directory));
13494 		rval = 0;
13495 	}
13496 
13497 fail:
13498 	/* Free allocated resources */
13499 	sata_free_local_buffer(spx);
13500 	sata_pkt_free(spx);
13501 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13502 
13503 	return (rval);
13504 }
13505 
13506 /*
13507  * Set up error retrieval sata command for NCQ command error data
13508  * recovery.
13509  *
13510  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
13511  * returns SATA_FAILURE otherwise.
13512  */
13513 static int
13514 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
13515 {
13516 #ifndef __lock_lint
13517 	_NOTE(ARGUNUSED(sdinfo))
13518 #endif
13519 
13520 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
13521 	sata_cmd_t *scmd;
13522 	struct buf *bp;
13523 
13524 	/* Operation modes are up to the caller */
13525 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13526 
13527 	/* Synchronous mode, no callback - may be changed by the caller */
13528 	spkt->satapkt_comp = NULL;
13529 	spkt->satapkt_time = sata_default_pkt_time;
13530 
13531 	scmd = &spkt->satapkt_cmd;
13532 	bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t));
13533 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
13534 
13535 	/*
13536 	 * Allocate dma_able buffer error data.
13537 	 * Buffer allocation will take care of buffer alignment and other DMA
13538 	 * attributes.
13539 	 */
13540 	bp = sata_alloc_local_buffer(spx,
13541 	    sizeof (struct sata_ncq_error_recovery_page));
13542 	if (bp == NULL)
13543 		return (SATA_FAILURE);
13544 
13545 	bp_mapin(bp); /* make data buffer accessible */
13546 	scmd->satacmd_bp = bp;
13547 
13548 	/*
13549 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
13550 	 * before accessing it. Handle is in usual place in translate struct.
13551 	 */
13552 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
13553 
13554 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
13555 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
13556 
13557 	return (SATA_SUCCESS);
13558 }
13559 
13560 /*
13561  * sata_xlate_errors() is used to translate (S)ATA error
13562  * information to SCSI information returned in the SCSI
13563  * packet.
13564  */
13565 static void
13566 sata_xlate_errors(sata_pkt_txlate_t *spx)
13567 {
13568 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
13569 	struct scsi_extended_sense *sense;
13570 
13571 	scsipkt->pkt_reason = CMD_INCOMPLETE;
13572 	*scsipkt->pkt_scbp = STATUS_CHECK;
13573 	sense = sata_arq_sense(spx);
13574 
13575 	switch (spx->txlt_sata_pkt->satapkt_reason) {
13576 	case SATA_PKT_PORT_ERROR:
13577 		/*
13578 		 * We have no device data. Assume no data transfered.
13579 		 */
13580 		sense->es_key = KEY_HARDWARE_ERROR;
13581 		break;
13582 
13583 	case SATA_PKT_DEV_ERROR:
13584 		if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
13585 		    SATA_STATUS_ERR) {
13586 			/*
13587 			 * determine dev error reason from error
13588 			 * reg content
13589 			 */
13590 			sata_decode_device_error(spx, sense);
13591 			break;
13592 		}
13593 		/* No extended sense key - no info available */
13594 		break;
13595 
13596 	case SATA_PKT_TIMEOUT:
13597 		scsipkt->pkt_reason = CMD_TIMEOUT;
13598 		scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET;
13599 		/* No extended sense key */
13600 		break;
13601 
13602 	case SATA_PKT_ABORTED:
13603 		scsipkt->pkt_reason = CMD_ABORTED;
13604 		scsipkt->pkt_statistics |= STAT_ABORTED;
13605 		/* No extended sense key */
13606 		break;
13607 
13608 	case SATA_PKT_RESET:
13609 		/*
13610 		 * pkt aborted either by an explicit reset request from
13611 		 * a host, or due to error recovery
13612 		 */
13613 		scsipkt->pkt_reason = CMD_RESET;
13614 		scsipkt->pkt_statistics |= STAT_DEV_RESET;
13615 		break;
13616 
13617 	default:
13618 		scsipkt->pkt_reason = CMD_TRAN_ERR;
13619 		break;
13620 	}
13621 }
13622 
13623 
13624 
13625 
13626 /*
13627  * Log sata message
13628  * dev pathname msg line preceeds the logged message.
13629  */
13630 
13631 static	void
13632 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...)
13633 {
13634 	char pathname[128];
13635 	dev_info_t *dip;
13636 	va_list ap;
13637 
13638 	mutex_enter(&sata_log_mutex);
13639 
13640 	va_start(ap, fmt);
13641 	(void) vsprintf(sata_log_buf, fmt, ap);
13642 	va_end(ap);
13643 
13644 	if (sata_hba_inst != NULL) {
13645 		dip = SATA_DIP(sata_hba_inst);
13646 		(void) ddi_pathname(dip, pathname);
13647 	} else {
13648 		pathname[0] = 0;
13649 	}
13650 	if (level == CE_CONT) {
13651 		if (sata_debug_flags == 0)
13652 			cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf);
13653 		else
13654 			cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf);
13655 	} else {
13656 		if (level != CE_NOTE) {
13657 			cmn_err(level, "%s:\n %s", pathname, sata_log_buf);
13658 		} else if (sata_msg) {
13659 			cmn_err(level, "%s:\n %s", pathname,
13660 			    sata_log_buf);
13661 		}
13662 	}
13663 
13664 	mutex_exit(&sata_log_mutex);
13665 }
13666 
13667 
13668 /* ******** Asynchronous HBA events handling & hotplugging support ******** */
13669 
13670 /*
13671  * Start or terminate the thread, depending on flag arg and current state
13672  */
13673 static void
13674 sata_event_thread_control(int startstop)
13675 {
13676 	static 	int sata_event_thread_terminating = 0;
13677 	static 	int sata_event_thread_starting = 0;
13678 	int i;
13679 
13680 	mutex_enter(&sata_event_mutex);
13681 
13682 	if (startstop == 0 && (sata_event_thread_starting == 1 ||
13683 	    sata_event_thread_terminating == 1)) {
13684 		mutex_exit(&sata_event_mutex);
13685 		return;
13686 	}
13687 	if (startstop == 1 && sata_event_thread_starting == 1) {
13688 		mutex_exit(&sata_event_mutex);
13689 		return;
13690 	}
13691 	if (startstop == 1 && sata_event_thread_terminating == 1) {
13692 		sata_event_thread_starting = 1;
13693 		/* wait til terminate operation completes */
13694 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
13695 		while (sata_event_thread_terminating == 1) {
13696 			if (i-- <= 0) {
13697 				sata_event_thread_starting = 0;
13698 				mutex_exit(&sata_event_mutex);
13699 #ifdef SATA_DEBUG
13700 				cmn_err(CE_WARN, "sata_event_thread_control: "
13701 				    "timeout waiting for thread to terminate");
13702 #endif
13703 				return;
13704 			}
13705 			mutex_exit(&sata_event_mutex);
13706 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
13707 			mutex_enter(&sata_event_mutex);
13708 		}
13709 	}
13710 	if (startstop == 1) {
13711 		if (sata_event_thread == NULL) {
13712 			sata_event_thread = thread_create(NULL, 0,
13713 			    (void (*)())sata_event_daemon,
13714 			    &sata_hba_list, 0, &p0, TS_RUN, minclsyspri);
13715 		}
13716 		sata_event_thread_starting = 0;
13717 		mutex_exit(&sata_event_mutex);
13718 		return;
13719 	}
13720 
13721 	/*
13722 	 * If we got here, thread may need to be terminated
13723 	 */
13724 	if (sata_event_thread != NULL) {
13725 		int i;
13726 		/* Signal event thread to go away */
13727 		sata_event_thread_terminating = 1;
13728 		sata_event_thread_terminate = 1;
13729 		cv_signal(&sata_event_cv);
13730 		/*
13731 		 * Wait til daemon terminates.
13732 		 */
13733 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
13734 		while (sata_event_thread_terminate == 1) {
13735 			mutex_exit(&sata_event_mutex);
13736 			if (i-- <= 0) {
13737 				/* Daemon did not go away !!! */
13738 #ifdef SATA_DEBUG
13739 				cmn_err(CE_WARN, "sata_event_thread_control: "
13740 				    "cannot terminate event daemon thread");
13741 #endif
13742 				mutex_enter(&sata_event_mutex);
13743 				break;
13744 			}
13745 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
13746 			mutex_enter(&sata_event_mutex);
13747 		}
13748 		sata_event_thread_terminating = 0;
13749 	}
13750 	ASSERT(sata_event_thread_terminating == 0);
13751 	ASSERT(sata_event_thread_starting == 0);
13752 	mutex_exit(&sata_event_mutex);
13753 }
13754 
13755 
13756 /*
13757  * SATA HBA event notification function.
13758  * Events reported by SATA HBA drivers per HBA instance relate to a change in
13759  * a port and/or device state or a controller itself.
13760  * Events for different addresses/addr types cannot be combined.
13761  * A warning message is generated for each event type.
13762  * Events are not processed by this function, so only the
13763  * event flag(s)is set for an affected entity and the event thread is
13764  * waken up. Event daemon thread processes all events.
13765  *
13766  * NOTE: Since more than one event may be reported at the same time, one
13767  * cannot determine a sequence of events when opposite event are reported, eg.
13768  * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing
13769  * is taking precedence over reported events, i.e. may cause ignoring some
13770  * events.
13771  */
13772 #define	SATA_EVENT_MAX_MSG_LENGTH	79
13773 
13774 void
13775 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event)
13776 {
13777 	sata_hba_inst_t *sata_hba_inst = NULL;
13778 	sata_address_t *saddr;
13779 	sata_drive_info_t *sdinfo;
13780 	sata_port_stats_t *pstats;
13781 	int cport, pmport;
13782 	char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1];
13783 	char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1];
13784 	char *lcp;
13785 	static char *err_msg_evnt_1 =
13786 	    "sata_hba_event_notify: invalid port event 0x%x ";
13787 	static char *err_msg_evnt_2 =
13788 	    "sata_hba_event_notify: invalid device event 0x%x ";
13789 	int linkevent;
13790 
13791 	/*
13792 	 * There is a possibility that an event will be generated on HBA
13793 	 * that has not completed attachment or is detaching.
13794 	 * HBA driver should prevent this, but just in case it does not,
13795 	 * we need to ignore events for such HBA.
13796 	 */
13797 	mutex_enter(&sata_mutex);
13798 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13799 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13800 		if (SATA_DIP(sata_hba_inst) == dip)
13801 			if (sata_hba_inst->satahba_attached == 1)
13802 				break;
13803 	}
13804 	mutex_exit(&sata_mutex);
13805 	if (sata_hba_inst == NULL)
13806 		/* HBA not attached */
13807 		return;
13808 
13809 	ASSERT(sata_device != NULL);
13810 
13811 	/*
13812 	 * Validate address before - do not proceed with invalid address.
13813 	 */
13814 	saddr = &sata_device->satadev_addr;
13815 	if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst))
13816 		return;
13817 	if (saddr->qual == SATA_ADDR_PMPORT ||
13818 	    saddr->qual == SATA_ADDR_DPMPORT)
13819 		/* Port Multiplier not supported yet */
13820 		return;
13821 
13822 	cport = saddr->cport;
13823 	pmport = saddr->pmport;
13824 
13825 	buf1[0] = buf2[0] = '\0';
13826 
13827 	/*
13828 	 * Events refer to devices, ports and controllers - each has
13829 	 * unique address. Events for different addresses cannot be combined.
13830 	 */
13831 	if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) {
13832 
13833 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13834 
13835 		/* qualify this event(s) */
13836 		if ((event & SATA_EVNT_PORT_EVENTS) == 0) {
13837 			/* Invalid event for the device port */
13838 			(void) sprintf(buf2, err_msg_evnt_1,
13839 			    event & SATA_EVNT_PORT_EVENTS);
13840 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13841 			goto event_info;
13842 		}
13843 		if (saddr->qual == SATA_ADDR_CPORT) {
13844 			/* Controller's device port event */
13845 
13846 			(SATA_CPORT_INFO(sata_hba_inst, cport))->
13847 			    cport_event_flags |=
13848 			    event & SATA_EVNT_PORT_EVENTS;
13849 			pstats =
13850 			    &(SATA_CPORT_INFO(sata_hba_inst, cport))->
13851 			    cport_stats;
13852 		} else {
13853 			/* Port multiplier's device port event */
13854 			(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
13855 			    pmport_event_flags |=
13856 			    event & SATA_EVNT_PORT_EVENTS;
13857 			pstats =
13858 			    &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
13859 			    pmport_stats;
13860 		}
13861 
13862 		/*
13863 		 * Add to statistics and log the message. We have to do it
13864 		 * here rather than in the event daemon, because there may be
13865 		 * multiple events occuring before they are processed.
13866 		 */
13867 		linkevent = event &
13868 		    (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED);
13869 		if (linkevent) {
13870 			if (linkevent == (SATA_EVNT_LINK_LOST |
13871 			    SATA_EVNT_LINK_ESTABLISHED)) {
13872 				/* This is likely event combination */
13873 				(void) strlcat(buf1, "link lost/established, ",
13874 				    SATA_EVENT_MAX_MSG_LENGTH);
13875 
13876 				if (pstats->link_lost < 0xffffffffffffffffULL)
13877 					pstats->link_lost++;
13878 				if (pstats->link_established <
13879 				    0xffffffffffffffffULL)
13880 					pstats->link_established++;
13881 				linkevent = 0;
13882 			} else if (linkevent & SATA_EVNT_LINK_LOST) {
13883 				(void) strlcat(buf1, "link lost, ",
13884 				    SATA_EVENT_MAX_MSG_LENGTH);
13885 
13886 				if (pstats->link_lost < 0xffffffffffffffffULL)
13887 					pstats->link_lost++;
13888 			} else {
13889 				(void) strlcat(buf1, "link established, ",
13890 				    SATA_EVENT_MAX_MSG_LENGTH);
13891 				if (pstats->link_established <
13892 				    0xffffffffffffffffULL)
13893 					pstats->link_established++;
13894 			}
13895 		}
13896 		if (event & SATA_EVNT_DEVICE_ATTACHED) {
13897 			(void) strlcat(buf1, "device attached, ",
13898 			    SATA_EVENT_MAX_MSG_LENGTH);
13899 			if (pstats->device_attached < 0xffffffffffffffffULL)
13900 				pstats->device_attached++;
13901 		}
13902 		if (event & SATA_EVNT_DEVICE_DETACHED) {
13903 			(void) strlcat(buf1, "device detached, ",
13904 			    SATA_EVENT_MAX_MSG_LENGTH);
13905 			if (pstats->device_detached < 0xffffffffffffffffULL)
13906 				pstats->device_detached++;
13907 		}
13908 		if (event & SATA_EVNT_PWR_LEVEL_CHANGED) {
13909 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
13910 			    "port %d power level changed", cport);
13911 			if (pstats->port_pwr_changed < 0xffffffffffffffffULL)
13912 				pstats->port_pwr_changed++;
13913 		}
13914 
13915 		if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) {
13916 			/* There should be no other events for this address */
13917 			(void) sprintf(buf2, err_msg_evnt_1,
13918 			    event & ~SATA_EVNT_PORT_EVENTS);
13919 		}
13920 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13921 
13922 	} else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) {
13923 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13924 
13925 		/* qualify this event */
13926 		if ((event & SATA_EVNT_DEVICE_RESET) == 0) {
13927 			/* Invalid event for a device */
13928 			(void) sprintf(buf2, err_msg_evnt_2,
13929 			    event & SATA_EVNT_DEVICE_RESET);
13930 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13931 			goto event_info;
13932 		}
13933 		/* drive event */
13934 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
13935 		if (sdinfo != NULL) {
13936 			if (event & SATA_EVNT_DEVICE_RESET) {
13937 				(void) strlcat(buf1, "device reset, ",
13938 				    SATA_EVENT_MAX_MSG_LENGTH);
13939 				if (sdinfo->satadrv_stats.drive_reset <
13940 				    0xffffffffffffffffULL)
13941 					sdinfo->satadrv_stats.drive_reset++;
13942 				sdinfo->satadrv_event_flags |=
13943 				    SATA_EVNT_DEVICE_RESET;
13944 			}
13945 		}
13946 		if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) {
13947 			/* Invalid event for a device */
13948 			(void) sprintf(buf2, err_msg_evnt_2,
13949 			    event & ~SATA_EVNT_DRIVE_EVENTS);
13950 		}
13951 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13952 	} else {
13953 		if (saddr->qual != SATA_ADDR_NULL) {
13954 			/* Wrong address qualifier */
13955 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13956 			    "sata_hba_event_notify: invalid address 0x%x",
13957 			    *(uint32_t *)saddr));
13958 			return;
13959 		}
13960 		if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 ||
13961 		    (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) {
13962 			/* Invalid event for the controller */
13963 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13964 			    "sata_hba_event_notify: invalid event 0x%x for "
13965 			    "controller",
13966 			    event & SATA_EVNT_CONTROLLER_EVENTS));
13967 			return;
13968 		}
13969 		buf1[0] = '\0';
13970 		/* This may be a frequent and not interesting event */
13971 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
13972 		    "controller power level changed\n", NULL);
13973 
13974 		mutex_enter(&sata_hba_inst->satahba_mutex);
13975 		if (sata_hba_inst->satahba_stats.ctrl_pwr_change <
13976 		    0xffffffffffffffffULL)
13977 			sata_hba_inst->satahba_stats.ctrl_pwr_change++;
13978 
13979 		sata_hba_inst->satahba_event_flags |=
13980 		    SATA_EVNT_PWR_LEVEL_CHANGED;
13981 		mutex_exit(&sata_hba_inst->satahba_mutex);
13982 	}
13983 	/*
13984 	 * If we got here, there is something to do with this HBA
13985 	 * instance.
13986 	 */
13987 	mutex_enter(&sata_hba_inst->satahba_mutex);
13988 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
13989 	mutex_exit(&sata_hba_inst->satahba_mutex);
13990 	mutex_enter(&sata_mutex);
13991 	sata_event_pending |= SATA_EVNT_MAIN;	/* global event indicator */
13992 	mutex_exit(&sata_mutex);
13993 
13994 	/* Tickle event thread */
13995 	mutex_enter(&sata_event_mutex);
13996 	if (sata_event_thread_active == 0)
13997 		cv_signal(&sata_event_cv);
13998 	mutex_exit(&sata_event_mutex);
13999 
14000 event_info:
14001 	if (buf1[0] != '\0') {
14002 		lcp = strrchr(buf1, ',');
14003 		if (lcp != NULL)
14004 			*lcp = '\0';
14005 	}
14006 	if (saddr->qual == SATA_ADDR_CPORT ||
14007 	    saddr->qual == SATA_ADDR_DCPORT) {
14008 		if (buf1[0] != '\0') {
14009 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
14010 			    cport, buf1);
14011 		}
14012 		if (buf2[0] != '\0') {
14013 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
14014 			    cport, buf2);
14015 		}
14016 	} else if (saddr->qual == SATA_ADDR_PMPORT ||
14017 	    saddr->qual == SATA_ADDR_DPMPORT) {
14018 		if (buf1[0] != '\0') {
14019 			sata_log(sata_hba_inst, CE_NOTE,
14020 			    "port %d pmport %d: %s\n", cport, pmport, buf1);
14021 		}
14022 		if (buf2[0] != '\0') {
14023 			sata_log(sata_hba_inst, CE_NOTE,
14024 			    "port %d pmport %d: %s\n", cport, pmport, buf2);
14025 		}
14026 	}
14027 }
14028 
14029 
14030 /*
14031  * Event processing thread.
14032  * Arg is a pointer to the sata_hba_list pointer.
14033  * It is not really needed, because sata_hba_list is global and static
14034  */
14035 static void
14036 sata_event_daemon(void *arg)
14037 {
14038 #ifndef __lock_lint
14039 	_NOTE(ARGUNUSED(arg))
14040 #endif
14041 	sata_hba_inst_t *sata_hba_inst;
14042 	clock_t lbolt;
14043 
14044 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
14045 	    "SATA event daemon started\n", NULL);
14046 loop:
14047 	/*
14048 	 * Process events here. Walk through all registered HBAs
14049 	 */
14050 	mutex_enter(&sata_mutex);
14051 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
14052 	    sata_hba_inst = sata_hba_inst->satahba_next) {
14053 		ASSERT(sata_hba_inst != NULL);
14054 		mutex_enter(&sata_hba_inst->satahba_mutex);
14055 		if (sata_hba_inst->satahba_attached != 1 ||
14056 		    (sata_hba_inst->satahba_event_flags &
14057 		    SATA_EVNT_SKIP) != 0) {
14058 			mutex_exit(&sata_hba_inst->satahba_mutex);
14059 			continue;
14060 		}
14061 		if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) {
14062 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP;
14063 			mutex_exit(&sata_hba_inst->satahba_mutex);
14064 			mutex_exit(&sata_mutex);
14065 			/* Got the controller with pending event */
14066 			sata_process_controller_events(sata_hba_inst);
14067 			/*
14068 			 * Since global mutex was released, there is a
14069 			 * possibility that HBA list has changed, so start
14070 			 * over from the top. Just processed controller
14071 			 * will be passed-over because of the SKIP flag.
14072 			 */
14073 			goto loop;
14074 		}
14075 		mutex_exit(&sata_hba_inst->satahba_mutex);
14076 	}
14077 	/* Clear SKIP flag in all controllers */
14078 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
14079 	    sata_hba_inst = sata_hba_inst->satahba_next) {
14080 		mutex_enter(&sata_hba_inst->satahba_mutex);
14081 		sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP;
14082 		mutex_exit(&sata_hba_inst->satahba_mutex);
14083 	}
14084 	mutex_exit(&sata_mutex);
14085 
14086 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
14087 	    "SATA EVENT DAEMON suspending itself", NULL);
14088 
14089 #ifdef SATA_DEBUG
14090 	if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) {
14091 		sata_log(sata_hba_inst, CE_WARN,
14092 		    "SATA EVENTS PROCESSING DISABLED\n");
14093 		thread_exit(); /* Daemon will not run again */
14094 	}
14095 #endif
14096 	mutex_enter(&sata_event_mutex);
14097 	sata_event_thread_active = 0;
14098 	mutex_exit(&sata_event_mutex);
14099 	/*
14100 	 * Go to sleep/suspend itself and wake up either because new event or
14101 	 * wait timeout. Exit if there is a termination request (driver
14102 	 * unload).
14103 	 */
14104 	do {
14105 		lbolt = ddi_get_lbolt();
14106 		lbolt += drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME);
14107 		mutex_enter(&sata_event_mutex);
14108 		(void) cv_timedwait(&sata_event_cv, &sata_event_mutex, lbolt);
14109 
14110 		if (sata_event_thread_active != 0) {
14111 			mutex_exit(&sata_event_mutex);
14112 			continue;
14113 		}
14114 
14115 		/* Check if it is time to go away */
14116 		if (sata_event_thread_terminate == 1) {
14117 			/*
14118 			 * It is up to the thread setting above flag to make
14119 			 * sure that this thread is not killed prematurely.
14120 			 */
14121 			sata_event_thread_terminate = 0;
14122 			sata_event_thread = NULL;
14123 			mutex_exit(&sata_event_mutex);
14124 			SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
14125 			    "SATA_EVENT_DAEMON_TERMINATING", NULL);
14126 			thread_exit();  { _NOTE(NOT_REACHED) }
14127 		}
14128 		mutex_exit(&sata_event_mutex);
14129 	} while (!(sata_event_pending & SATA_EVNT_MAIN));
14130 
14131 	mutex_enter(&sata_event_mutex);
14132 	sata_event_thread_active = 1;
14133 	mutex_exit(&sata_event_mutex);
14134 
14135 	mutex_enter(&sata_mutex);
14136 	sata_event_pending &= ~SATA_EVNT_MAIN;
14137 	mutex_exit(&sata_mutex);
14138 
14139 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
14140 	    "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL);
14141 
14142 	goto loop;
14143 }
14144 
14145 /*
14146  * Specific HBA instance event processing.
14147  *
14148  * NOTE: At the moment, device event processing is limited to hard disks
14149  * only.
14150  * cports only are supported - no pmports.
14151  */
14152 static void
14153 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst)
14154 {
14155 	int ncport;
14156 	uint32_t event_flags;
14157 	sata_address_t *saddr;
14158 
14159 	SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst,
14160 	    "Processing controller %d event(s)",
14161 	    ddi_get_instance(SATA_DIP(sata_hba_inst)));
14162 
14163 	mutex_enter(&sata_hba_inst->satahba_mutex);
14164 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN;
14165 	event_flags = sata_hba_inst->satahba_event_flags;
14166 	mutex_exit(&sata_hba_inst->satahba_mutex);
14167 	/*
14168 	 * Process controller power change first
14169 	 * HERE
14170 	 */
14171 	if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED)
14172 		sata_process_cntrl_pwr_level_change(sata_hba_inst);
14173 
14174 	/*
14175 	 * Search through ports/devices to identify affected port/device.
14176 	 * We may have to process events for more than one port/device.
14177 	 */
14178 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
14179 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14180 		event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))->
14181 		    cport_event_flags;
14182 		/* Check if port was locked by IOCTL processing */
14183 		if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) {
14184 			/*
14185 			 * We ignore port events because port is busy
14186 			 * with AP control processing. Set again
14187 			 * controller and main event flag, so that
14188 			 * events may be processed by the next daemon
14189 			 * run.
14190 			 */
14191 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14192 			mutex_enter(&sata_hba_inst->satahba_mutex);
14193 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14194 			mutex_exit(&sata_hba_inst->satahba_mutex);
14195 			mutex_enter(&sata_mutex);
14196 			sata_event_pending |= SATA_EVNT_MAIN;
14197 			mutex_exit(&sata_mutex);
14198 			SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst,
14199 			    "Event processing postponed until "
14200 			    "AP control processing completes",
14201 			    NULL);
14202 			/* Check other ports */
14203 			continue;
14204 		} else {
14205 			/*
14206 			 * Set BSY flag so that AP control would not
14207 			 * interfere with events processing for
14208 			 * this port.
14209 			 */
14210 			(SATA_CPORT_INFO(sata_hba_inst, ncport))->
14211 			    cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY;
14212 		}
14213 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14214 
14215 		saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr;
14216 
14217 		if ((event_flags &
14218 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
14219 			/*
14220 			 * Got port event.
14221 			 * We need some hierarchy of event processing as they
14222 			 * are affecting each other:
14223 			 * 1. port failed
14224 			 * 2. device detached/attached
14225 			 * 3. link events - link events may trigger device
14226 			 *    detached or device attached events in some
14227 			 *    circumstances.
14228 			 * 4. port power level changed
14229 			 */
14230 			if (event_flags & SATA_EVNT_PORT_FAILED) {
14231 				sata_process_port_failed_event(sata_hba_inst,
14232 				    saddr);
14233 			}
14234 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
14235 				sata_process_device_detached(sata_hba_inst,
14236 				    saddr);
14237 			}
14238 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
14239 				sata_process_device_attached(sata_hba_inst,
14240 				    saddr);
14241 			}
14242 			if (event_flags &
14243 			    (SATA_EVNT_LINK_ESTABLISHED |
14244 			    SATA_EVNT_LINK_LOST)) {
14245 				sata_process_port_link_events(sata_hba_inst,
14246 				    saddr);
14247 			}
14248 			if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) {
14249 				sata_process_port_pwr_change(sata_hba_inst,
14250 				    saddr);
14251 			}
14252 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
14253 				sata_process_target_node_cleanup(
14254 				    sata_hba_inst, saddr);
14255 			}
14256 			if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) {
14257 				sata_process_device_autoonline(
14258 				    sata_hba_inst, saddr);
14259 			}
14260 		}
14261 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14262 		if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) !=
14263 		    SATA_DTYPE_NONE) &&
14264 		    (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) {
14265 			if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)->
14266 			    satadrv_event_flags &
14267 			    (SATA_EVNT_DEVICE_RESET |
14268 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
14269 				/* Have device event */
14270 				sata_process_device_reset(sata_hba_inst,
14271 				    saddr);
14272 			}
14273 		}
14274 		/* Release PORT_BUSY flag */
14275 		(SATA_CPORT_INFO(sata_hba_inst, ncport))->
14276 		    cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
14277 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14278 
14279 	} /* End of loop through the controller SATA ports */
14280 }
14281 
14282 /*
14283  * Process HBA power level change reported by HBA driver.
14284  * Not implemented at this time - event is ignored.
14285  */
14286 static void
14287 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst)
14288 {
14289 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14290 	    "Processing controller power level change", NULL);
14291 
14292 	/* Ignoring it for now */
14293 	mutex_enter(&sata_hba_inst->satahba_mutex);
14294 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
14295 	mutex_exit(&sata_hba_inst->satahba_mutex);
14296 }
14297 
14298 /*
14299  * Process port power level change reported by HBA driver.
14300  * Not implemented at this time - event is ignored.
14301  */
14302 static void
14303 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst,
14304     sata_address_t *saddr)
14305 {
14306 	sata_cport_info_t *cportinfo;
14307 
14308 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14309 	    "Processing port power level change", NULL);
14310 
14311 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14312 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14313 	/* Reset event flag */
14314 	cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
14315 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14316 }
14317 
14318 /*
14319  * Process port failure reported by HBA driver.
14320  * cports support only - no pmports.
14321  */
14322 static void
14323 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst,
14324     sata_address_t *saddr)
14325 {
14326 	sata_cport_info_t *cportinfo;
14327 
14328 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14329 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14330 	/* Reset event flag first */
14331 	cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED;
14332 	/* If the port is in SHUTDOWN or FAILED state, ignore this event. */
14333 	if ((cportinfo->cport_state &
14334 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) {
14335 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14336 		    cport_mutex);
14337 		return;
14338 	}
14339 	/* Fail the port */
14340 	cportinfo->cport_state = SATA_PSTATE_FAILED;
14341 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14342 	sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport);
14343 }
14344 
14345 /*
14346  * Device Reset Event processing.
14347  * The seqeunce is managed by 3 stage flags:
14348  * - reset event reported,
14349  * - reset event being processed,
14350  * - request to clear device reset state.
14351  *
14352  * NOTE: This function has to be entered with cport mutex held. It exits with
14353  * mutex held as well, but can release mutex during the processing.
14354  */
14355 static void
14356 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst,
14357     sata_address_t *saddr)
14358 {
14359 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
14360 	sata_drive_info_t *sdinfo;
14361 	sata_cport_info_t *cportinfo;
14362 	sata_device_t sata_device;
14363 	int rval;
14364 
14365 	/* We only care about host sata cport for now */
14366 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14367 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14368 	/*
14369 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
14370 	 * state, ignore reset event.
14371 	 */
14372 	if (((cportinfo->cport_state &
14373 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
14374 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
14375 		sdinfo->satadrv_event_flags &=
14376 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
14377 		return;
14378 	}
14379 
14380 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) &
14381 	    SATA_VALID_DEV_TYPE) == 0) {
14382 		/*
14383 		 * This should not happen - coding error.
14384 		 * But we can recover, so do not panic, just clean up
14385 		 * and if in debug mode, log the message.
14386 		 */
14387 #ifdef SATA_DEBUG
14388 		sata_log(sata_hba_inst, CE_WARN,
14389 		    "sata_process_device_reset: "
14390 		    "Invalid device type with sdinfo!", NULL);
14391 #endif
14392 		sdinfo->satadrv_event_flags = 0;
14393 		return;
14394 	}
14395 
14396 #ifdef SATA_DEBUG
14397 	if ((sdinfo->satadrv_event_flags &
14398 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
14399 		/* Nothing to do */
14400 		/* Something is weird - why we are processing dev reset? */
14401 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14402 		    "No device reset event!!!!", NULL);
14403 
14404 		return;
14405 	}
14406 	if ((sdinfo->satadrv_event_flags &
14407 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
14408 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
14409 		/* Something is weird - new device reset event */
14410 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14411 		    "Overlapping device reset events!", NULL);
14412 	}
14413 #endif
14414 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14415 	    "Processing port %d device reset", saddr->cport);
14416 
14417 	/* Clear event flag */
14418 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
14419 
14420 	/* It seems that we always need to check the port state first */
14421 	sata_device.satadev_rev = SATA_DEVICE_REV;
14422 	sata_device.satadev_addr = *saddr;
14423 	/*
14424 	 * We have to exit mutex, because the HBA probe port function may
14425 	 * block on its own mutex.
14426 	 */
14427 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14428 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14429 	    (SATA_DIP(sata_hba_inst), &sata_device);
14430 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14431 	sata_update_port_info(sata_hba_inst, &sata_device);
14432 	if (rval != SATA_SUCCESS) {
14433 		/* Something went wrong? Fail the port */
14434 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14435 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14436 		if (sdinfo != NULL)
14437 			sdinfo->satadrv_event_flags = 0;
14438 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14439 		    cport_mutex);
14440 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14441 		    "SATA port %d probing failed",
14442 		    saddr->cport));
14443 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14444 		    saddr->cport)->cport_mutex);
14445 		return;
14446 	}
14447 	if ((sata_device.satadev_scr.sstatus  &
14448 	    SATA_PORT_DEVLINK_UP_MASK) !=
14449 	    SATA_PORT_DEVLINK_UP ||
14450 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
14451 		/*
14452 		 * No device to process, anymore. Some other event processing
14453 		 * would or have already performed port info cleanup.
14454 		 * To be safe (HBA may need it), request clearing device
14455 		 * reset condition.
14456 		 */
14457 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14458 		if (sdinfo != NULL) {
14459 			sdinfo->satadrv_event_flags &=
14460 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
14461 			sdinfo->satadrv_event_flags |=
14462 			    SATA_EVNT_CLEAR_DEVICE_RESET;
14463 		}
14464 		return;
14465 	}
14466 
14467 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14468 	if (sdinfo == NULL) {
14469 		return;
14470 	}
14471 	if ((sdinfo->satadrv_event_flags &
14472 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
14473 		/*
14474 		 * Start tracking time for device feature restoration and
14475 		 * identification. Save current time (lbolt value).
14476 		 */
14477 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
14478 	}
14479 	/* Mark device reset processing as active */
14480 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
14481 
14482 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
14483 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14484 
14485 	if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) ==
14486 	    SATA_FAILURE) {
14487 		/*
14488 		 * Restoring drive setting failed.
14489 		 * Probe the port first, to check if the port state has changed
14490 		 */
14491 		sata_device.satadev_rev = SATA_DEVICE_REV;
14492 		sata_device.satadev_addr = *saddr;
14493 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
14494 		/* probe port */
14495 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14496 		    (SATA_DIP(sata_hba_inst), &sata_device);
14497 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14498 		    cport_mutex);
14499 		if (rval == SATA_SUCCESS &&
14500 		    (sata_device.satadev_state &
14501 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
14502 		    (sata_device.satadev_scr.sstatus  &
14503 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
14504 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
14505 			/*
14506 			 * We may retry this a bit later - in-process reset
14507 			 * condition should be already set.
14508 			 * Track retry time for device identification.
14509 			 */
14510 			if ((cportinfo->cport_dev_type &
14511 			    SATA_VALID_DEV_TYPE) != 0 &&
14512 			    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL &&
14513 			    sdinfo->satadrv_reset_time != 0) {
14514 				clock_t cur_time = ddi_get_lbolt();
14515 				/*
14516 				 * If the retry time limit was not
14517 				 * exceeded, retry.
14518 				 */
14519 				if ((cur_time - sdinfo->satadrv_reset_time) <
14520 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
14521 					mutex_enter(
14522 					    &sata_hba_inst->satahba_mutex);
14523 					sata_hba_inst->satahba_event_flags |=
14524 					    SATA_EVNT_MAIN;
14525 					mutex_exit(
14526 					    &sata_hba_inst->satahba_mutex);
14527 					mutex_enter(&sata_mutex);
14528 					sata_event_pending |= SATA_EVNT_MAIN;
14529 					mutex_exit(&sata_mutex);
14530 					return;
14531 				}
14532 			}
14533 			/* Fail the drive */
14534 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
14535 
14536 			sata_log(sata_hba_inst, CE_WARN,
14537 			    "SATA device at port %d - device failed",
14538 			    saddr->cport);
14539 		} else {
14540 			/*
14541 			 * No point of retrying - some other event processing
14542 			 * would or already did port info cleanup.
14543 			 * To be safe (HBA may need it),
14544 			 * request clearing device reset condition.
14545 			 */
14546 			sdinfo->satadrv_event_flags |=
14547 			    SATA_EVNT_CLEAR_DEVICE_RESET;
14548 		}
14549 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
14550 		sdinfo->satadrv_reset_time = 0;
14551 		return;
14552 	}
14553 	/*
14554 	 * Raise the flag indicating that the next sata command could
14555 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
14556 	 * reset is reported.
14557 	 */
14558 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14559 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14560 		sdinfo->satadrv_reset_time = 0;
14561 		if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) {
14562 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14563 			sdinfo->satadrv_event_flags &=
14564 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
14565 			sdinfo->satadrv_event_flags |=
14566 			    SATA_EVNT_CLEAR_DEVICE_RESET;
14567 		}
14568 	}
14569 }
14570 
14571 
14572 /*
14573  * Port Link Events processing.
14574  * Every link established event may involve device reset (due to
14575  * COMRESET signal, equivalent of the hard reset) so arbitrarily
14576  * set device reset event for an attached device (if any).
14577  * If the port is in SHUTDOWN or FAILED state, ignore link events.
14578  *
14579  * The link established event processing varies, depending on the state
14580  * of the target node, HBA hotplugging capabilities, state of the port.
14581  * If the link is not active, the link established event is ignored.
14582  * If HBA cannot detect device attachment and there is no target node,
14583  * the link established event triggers device attach event processing.
14584  * Else, link established event triggers device reset event processing.
14585  *
14586  * The link lost event processing varies, depending on a HBA hotplugging
14587  * capability and the state of the port (link active or not active).
14588  * If the link is active, the lost link event is ignored.
14589  * If HBA cannot detect device removal, the lost link event triggers
14590  * device detached event processing after link lost timeout.
14591  * Else, the event is ignored.
14592  *
14593  * NOTE: Only cports are processed for now, i.e. no port multiplier ports
14594  */
14595 static void
14596 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst,
14597     sata_address_t *saddr)
14598 {
14599 	sata_device_t sata_device;
14600 	sata_cport_info_t *cportinfo;
14601 	sata_drive_info_t *sdinfo;
14602 	uint32_t event_flags;
14603 	int rval;
14604 
14605 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14606 	    "Processing port %d link event(s)", saddr->cport);
14607 
14608 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14609 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14610 	event_flags = cportinfo->cport_event_flags;
14611 
14612 	/* Reset event flags first */
14613 	cportinfo->cport_event_flags &=
14614 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
14615 
14616 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
14617 	if ((cportinfo->cport_state &
14618 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14619 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14620 		    cport_mutex);
14621 		return;
14622 	}
14623 
14624 	/*
14625 	 * For the sanity sake get current port state.
14626 	 * Set device address only. Other sata_device fields should be
14627 	 * set by HBA driver.
14628 	 */
14629 	sata_device.satadev_rev = SATA_DEVICE_REV;
14630 	sata_device.satadev_addr = *saddr;
14631 	/*
14632 	 * We have to exit mutex, because the HBA probe port function may
14633 	 * block on its own mutex.
14634 	 */
14635 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14636 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14637 	    (SATA_DIP(sata_hba_inst), &sata_device);
14638 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14639 	sata_update_port_info(sata_hba_inst, &sata_device);
14640 	if (rval != SATA_SUCCESS) {
14641 		/* Something went wrong? Fail the port */
14642 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14643 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14644 		    cport_mutex);
14645 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14646 		    "SATA port %d probing failed",
14647 		    saddr->cport));
14648 		/*
14649 		 * We may want to release device info structure, but
14650 		 * it is not necessary.
14651 		 */
14652 		return;
14653 	} else {
14654 		/* port probed successfully */
14655 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14656 	}
14657 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
14658 
14659 		if ((sata_device.satadev_scr.sstatus &
14660 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
14661 			/* Ignore event */
14662 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14663 			    "Ignoring port %d link established event - "
14664 			    "link down",
14665 			    saddr->cport);
14666 			goto linklost;
14667 		}
14668 
14669 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14670 		    "Processing port %d link established event",
14671 		    saddr->cport);
14672 
14673 		/*
14674 		 * For the sanity sake check if a device is attached - check
14675 		 * return state of a port probing.
14676 		 */
14677 		if (sata_device.satadev_type != SATA_DTYPE_NONE &&
14678 		    sata_device.satadev_type != SATA_DTYPE_PMULT) {
14679 			/*
14680 			 * HBA port probe indicated that there is a device
14681 			 * attached. Check if the framework had device info
14682 			 * structure attached for this device.
14683 			 */
14684 			if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
14685 				ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) !=
14686 				    NULL);
14687 
14688 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14689 				if ((sdinfo->satadrv_type &
14690 				    SATA_VALID_DEV_TYPE) != 0) {
14691 					/*
14692 					 * Dev info structure is present.
14693 					 * If dev_type is set to known type in
14694 					 * the framework's drive info struct
14695 					 * then the device existed before and
14696 					 * the link was probably lost
14697 					 * momentarily - in such case
14698 					 * we may want to check device
14699 					 * identity.
14700 					 * Identity check is not supported now.
14701 					 *
14702 					 * Link established event
14703 					 * triggers device reset event.
14704 					 */
14705 					(SATA_CPORTINFO_DRV_INFO(cportinfo))->
14706 					    satadrv_event_flags |=
14707 					    SATA_EVNT_DEVICE_RESET;
14708 				}
14709 			} else if (cportinfo->cport_dev_type ==
14710 			    SATA_DTYPE_NONE) {
14711 				/*
14712 				 * We got new device attached! If HBA does not
14713 				 * generate device attached events, trigger it
14714 				 * here.
14715 				 */
14716 				if (!(SATA_FEATURES(sata_hba_inst) &
14717 				    SATA_CTLF_HOTPLUG)) {
14718 					cportinfo->cport_event_flags |=
14719 					    SATA_EVNT_DEVICE_ATTACHED;
14720 				}
14721 			}
14722 			/* Reset link lost timeout */
14723 			cportinfo->cport_link_lost_time = 0;
14724 		}
14725 	}
14726 linklost:
14727 	if (event_flags & SATA_EVNT_LINK_LOST) {
14728 		if ((sata_device.satadev_scr.sstatus &
14729 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
14730 			/* Ignore event */
14731 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14732 			    "Ignoring port %d link lost event - link is up",
14733 			    saddr->cport);
14734 			goto done;
14735 		}
14736 #ifdef SATA_DEBUG
14737 		if (cportinfo->cport_link_lost_time == 0) {
14738 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14739 			    "Processing port %d link lost event",
14740 			    saddr->cport);
14741 		}
14742 #endif
14743 		/*
14744 		 * When HBA cannot generate device attached/detached events,
14745 		 * we need to track link lost time and eventually generate
14746 		 * device detach event.
14747 		 */
14748 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
14749 			/* We are tracking link lost time */
14750 			if (cportinfo->cport_link_lost_time == 0) {
14751 				/* save current time (lbolt value) */
14752 				cportinfo->cport_link_lost_time =
14753 				    ddi_get_lbolt();
14754 				/* just keep link lost event */
14755 				cportinfo->cport_event_flags |=
14756 				    SATA_EVNT_LINK_LOST;
14757 			} else {
14758 				clock_t cur_time = ddi_get_lbolt();
14759 				if ((cur_time -
14760 				    cportinfo->cport_link_lost_time) >=
14761 				    drv_usectohz(
14762 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
14763 					/* trigger device detach event */
14764 					cportinfo->cport_event_flags |=
14765 					    SATA_EVNT_DEVICE_DETACHED;
14766 					cportinfo->cport_link_lost_time = 0;
14767 					SATADBG1(SATA_DBG_EVENTS,
14768 					    sata_hba_inst,
14769 					    "Triggering port %d "
14770 					    "device detached event",
14771 					    saddr->cport);
14772 				} else {
14773 					/* keep link lost event */
14774 					cportinfo->cport_event_flags |=
14775 					    SATA_EVNT_LINK_LOST;
14776 				}
14777 			}
14778 		}
14779 		/*
14780 		 * We could change port state to disable/delay access to
14781 		 * the attached device until the link is recovered.
14782 		 */
14783 	}
14784 done:
14785 	event_flags = cportinfo->cport_event_flags;
14786 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14787 	if (event_flags != 0) {
14788 		mutex_enter(&sata_hba_inst->satahba_mutex);
14789 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14790 		mutex_exit(&sata_hba_inst->satahba_mutex);
14791 		mutex_enter(&sata_mutex);
14792 		sata_event_pending |= SATA_EVNT_MAIN;
14793 		mutex_exit(&sata_mutex);
14794 	}
14795 }
14796 
14797 /*
14798  * Device Detached Event processing.
14799  * Port is probed to find if a device is really gone. If so,
14800  * the device info structure is detached from the SATA port info structure
14801  * and released.
14802  * Port status is updated.
14803  *
14804  * NOTE: Process cports event only, no port multiplier ports.
14805  */
14806 static void
14807 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst,
14808     sata_address_t *saddr)
14809 {
14810 	sata_cport_info_t *cportinfo;
14811 	sata_drive_info_t *sdevinfo;
14812 	sata_device_t sata_device;
14813 	dev_info_t *tdip;
14814 	int rval;
14815 
14816 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14817 	    "Processing port %d device detached", saddr->cport);
14818 
14819 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14820 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14821 	/* Clear event flag */
14822 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
14823 
14824 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
14825 	if ((cportinfo->cport_state &
14826 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14827 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14828 		    cport_mutex);
14829 		return;
14830 	}
14831 	/* For sanity, re-probe the port */
14832 	sata_device.satadev_rev = SATA_DEVICE_REV;
14833 	sata_device.satadev_addr = *saddr;
14834 
14835 	/*
14836 	 * We have to exit mutex, because the HBA probe port function may
14837 	 * block on its own mutex.
14838 	 */
14839 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14840 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14841 	    (SATA_DIP(sata_hba_inst), &sata_device);
14842 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14843 	sata_update_port_info(sata_hba_inst, &sata_device);
14844 	if (rval != SATA_SUCCESS) {
14845 		/* Something went wrong? Fail the port */
14846 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14847 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14848 		    cport_mutex);
14849 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14850 		    "SATA port %d probing failed",
14851 		    saddr->cport));
14852 		/*
14853 		 * We may want to release device info structure, but
14854 		 * it is not necessary.
14855 		 */
14856 		return;
14857 	} else {
14858 		/* port probed successfully */
14859 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14860 	}
14861 	/*
14862 	 * Check if a device is still attached. For sanity, check also
14863 	 * link status - if no link, there is no device.
14864 	 */
14865 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
14866 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
14867 	    SATA_DTYPE_NONE) {
14868 		/*
14869 		 * Device is still attached - ignore detach event.
14870 		 */
14871 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14872 		    cport_mutex);
14873 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14874 		    "Ignoring detach - device still attached to port %d",
14875 		    sata_device.satadev_addr.cport);
14876 		return;
14877 	}
14878 	/*
14879 	 * We need to detach and release device info structure here
14880 	 */
14881 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14882 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14883 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14884 		(void) kmem_free((void *)sdevinfo,
14885 		    sizeof (sata_drive_info_t));
14886 	}
14887 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14888 	/*
14889 	 * Device cannot be reached anymore, even if the target node may be
14890 	 * still present.
14891 	 */
14892 
14893 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14894 	sata_log(sata_hba_inst, CE_WARN, "SATA device detached at port %d",
14895 	    sata_device.satadev_addr.cport);
14896 
14897 	/*
14898 	 * Try to offline a device and remove target node if it still exists
14899 	 */
14900 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
14901 	if (tdip != NULL) {
14902 		/*
14903 		 * Target node exists.  Unconfigure device then remove
14904 		 * the target node (one ndi operation).
14905 		 */
14906 		if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
14907 			/*
14908 			 * PROBLEM - no device, but target node remained
14909 			 * This happens when the file was open or node was
14910 			 * waiting for resources.
14911 			 */
14912 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14913 			    "sata_process_device_detached: "
14914 			    "Failed to remove target node for "
14915 			    "detached SATA device."));
14916 			/*
14917 			 * Set target node state to DEVI_DEVICE_REMOVED.
14918 			 * But re-check first that the node still exists.
14919 			 */
14920 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
14921 			    saddr->cport);
14922 			if (tdip != NULL) {
14923 				sata_set_device_removed(tdip);
14924 				/*
14925 				 * Instruct event daemon to retry the
14926 				 * cleanup later.
14927 				 */
14928 				sata_set_target_node_cleanup(sata_hba_inst,
14929 				    &sata_device.satadev_addr);
14930 			}
14931 		}
14932 	}
14933 	/*
14934 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14935 	 * with the hint: SE_HINT_REMOVE
14936 	 */
14937 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
14938 }
14939 
14940 
14941 /*
14942  * Device Attached Event processing.
14943  * Port state is checked to verify that a device is really attached. If so,
14944  * the device info structure is created and attached to the SATA port info
14945  * structure.
14946  *
14947  * If attached device cannot be identified or set-up, the retry for the
14948  * attach processing is set-up. Subsequent daemon run would try again to
14949  * identify the device, until the time limit is reached
14950  * (SATA_DEV_IDENTIFY_TIMEOUT).
14951  *
14952  * This function cannot be called in interrupt context (it may sleep).
14953  *
14954  * NOTE: Process cports event only, no port multiplier ports.
14955  */
14956 static void
14957 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst,
14958     sata_address_t *saddr)
14959 {
14960 	sata_cport_info_t *cportinfo;
14961 	sata_drive_info_t *sdevinfo;
14962 	sata_device_t sata_device;
14963 	dev_info_t *tdip;
14964 	uint32_t event_flags;
14965 	int rval;
14966 
14967 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14968 	    "Processing port %d device attached", saddr->cport);
14969 
14970 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14971 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14972 
14973 	/* Clear attach event flag first */
14974 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
14975 
14976 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
14977 	if ((cportinfo->cport_state &
14978 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14979 		cportinfo->cport_dev_attach_time = 0;
14980 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14981 		    cport_mutex);
14982 		return;
14983 	}
14984 
14985 	/*
14986 	 * If the sata_drive_info structure is found attached to the port info,
14987 	 * despite the fact the device was removed and now it is re-attached,
14988 	 * the old drive info structure was not removed.
14989 	 * Arbitrarily release device info structure.
14990 	 */
14991 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14992 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14993 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14994 		(void) kmem_free((void *)sdevinfo,
14995 		    sizeof (sata_drive_info_t));
14996 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14997 		    "Arbitrarily detaching old device info.", NULL);
14998 	}
14999 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
15000 
15001 	/* For sanity, re-probe the port */
15002 	sata_device.satadev_rev = SATA_DEVICE_REV;
15003 	sata_device.satadev_addr = *saddr;
15004 
15005 	/*
15006 	 * We have to exit mutex, because the HBA probe port function may
15007 	 * block on its own mutex.
15008 	 */
15009 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15010 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
15011 	    (SATA_DIP(sata_hba_inst), &sata_device);
15012 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15013 	sata_update_port_info(sata_hba_inst, &sata_device);
15014 	if (rval != SATA_SUCCESS) {
15015 		/* Something went wrong? Fail the port */
15016 		cportinfo->cport_state = SATA_PSTATE_FAILED;
15017 		cportinfo->cport_dev_attach_time = 0;
15018 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15019 		    cport_mutex);
15020 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15021 		    "SATA port %d probing failed",
15022 		    saddr->cport));
15023 		return;
15024 	} else {
15025 		/* port probed successfully */
15026 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
15027 	}
15028 	/*
15029 	 * Check if a device is still attached. For sanity, check also
15030 	 * link status - if no link, there is no device.
15031 	 */
15032 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
15033 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
15034 	    SATA_DTYPE_NONE) {
15035 		/*
15036 		 * No device - ignore attach event.
15037 		 */
15038 		cportinfo->cport_dev_attach_time = 0;
15039 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15040 		    cport_mutex);
15041 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15042 		    "Ignoring attach - no device connected to port %d",
15043 		    sata_device.satadev_addr.cport);
15044 		return;
15045 	}
15046 
15047 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15048 	/*
15049 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
15050 	 * with the hint: SE_HINT_INSERT
15051 	 */
15052 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
15053 
15054 	/*
15055 	 * Port reprobing will take care of the creation of the device
15056 	 * info structure and determination of the device type.
15057 	 */
15058 	sata_device.satadev_addr = *saddr;
15059 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
15060 	    SATA_DEV_IDENTIFY_NORETRY);
15061 
15062 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
15063 	    cport_mutex);
15064 	if ((cportinfo->cport_state & SATA_STATE_READY) &&
15065 	    (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) {
15066 		/* Some device is attached to the port */
15067 		if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) {
15068 			/*
15069 			 * A device was not successfully attached.
15070 			 * Track retry time for device identification.
15071 			 */
15072 			if (cportinfo->cport_dev_attach_time != 0) {
15073 				clock_t cur_time = ddi_get_lbolt();
15074 				/*
15075 				 * If the retry time limit was not exceeded,
15076 				 * reinstate attach event.
15077 				 */
15078 				if ((cur_time -
15079 				    cportinfo->cport_dev_attach_time) <
15080 				    drv_usectohz(
15081 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
15082 					/* OK, restore attach event */
15083 					cportinfo->cport_event_flags |=
15084 					    SATA_EVNT_DEVICE_ATTACHED;
15085 				} else {
15086 					/* Timeout - cannot identify device */
15087 					cportinfo->cport_dev_attach_time = 0;
15088 					sata_log(sata_hba_inst,
15089 					    CE_WARN,
15090 					    "Could not identify SATA device "
15091 					    "at port %d",
15092 					    saddr->cport);
15093 				}
15094 			} else {
15095 				/*
15096 				 * Start tracking time for device
15097 				 * identification.
15098 				 * Save current time (lbolt value).
15099 				 */
15100 				cportinfo->cport_dev_attach_time =
15101 				    ddi_get_lbolt();
15102 				/* Restore attach event */
15103 				cportinfo->cport_event_flags |=
15104 				    SATA_EVNT_DEVICE_ATTACHED;
15105 			}
15106 		} else {
15107 			/*
15108 			 * If device was successfully attached, the subsequent
15109 			 * action depends on a state of the
15110 			 * sata_auto_online variable. If it is set to zero.
15111 			 * an explicit 'configure' command will be needed to
15112 			 * configure it. If its value is non-zero, we will
15113 			 * attempt to online (configure) the device.
15114 			 * First, log the message indicating that a device
15115 			 * was attached.
15116 			 */
15117 			cportinfo->cport_dev_attach_time = 0;
15118 			sata_log(sata_hba_inst, CE_WARN,
15119 			    "SATA device detected at port %d", saddr->cport);
15120 
15121 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
15122 				sata_drive_info_t new_sdinfo;
15123 
15124 				/* Log device info data */
15125 				new_sdinfo = *(SATA_CPORTINFO_DRV_INFO(
15126 				    cportinfo));
15127 				sata_show_drive_info(sata_hba_inst,
15128 				    &new_sdinfo);
15129 			}
15130 
15131 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15132 			    saddr->cport)->cport_mutex);
15133 
15134 			/*
15135 			 * Make sure that there is no target node for that
15136 			 * device. If so, release it. It should not happen,
15137 			 * unless we had problem removing the node when
15138 			 * device was detached.
15139 			 */
15140 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
15141 			    saddr->cport);
15142 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15143 			    saddr->cport)->cport_mutex);
15144 			if (tdip != NULL) {
15145 
15146 #ifdef SATA_DEBUG
15147 				if ((cportinfo->cport_event_flags &
15148 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
15149 					sata_log(sata_hba_inst, CE_WARN,
15150 					    "sata_process_device_attached: "
15151 					    "old device target node exists!");
15152 #endif
15153 				/*
15154 				 * target node exists - try to unconfigure
15155 				 * device and remove the node.
15156 				 */
15157 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15158 				    saddr->cport)->cport_mutex);
15159 				rval = ndi_devi_offline(tdip,
15160 				    NDI_DEVI_REMOVE);
15161 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15162 				    saddr->cport)->cport_mutex);
15163 
15164 				if (rval == NDI_SUCCESS) {
15165 					cportinfo->cport_event_flags &=
15166 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
15167 					cportinfo->cport_tgtnode_clean = B_TRUE;
15168 				} else {
15169 					/*
15170 					 * PROBLEM - the target node remained
15171 					 * and it belongs to a previously
15172 					 * attached device.
15173 					 * This happens when the file was open
15174 					 * or the node was waiting for
15175 					 * resources at the time the
15176 					 * associated device was removed.
15177 					 * Instruct event daemon to retry the
15178 					 * cleanup later.
15179 					 */
15180 					sata_log(sata_hba_inst,
15181 					    CE_WARN,
15182 					    "Application(s) accessing "
15183 					    "previously attached SATA "
15184 					    "device have to release "
15185 					    "it before newly inserted "
15186 					    "device can be made accessible.",
15187 					    saddr->cport);
15188 					cportinfo->cport_event_flags |=
15189 					    SATA_EVNT_TARGET_NODE_CLEANUP;
15190 					cportinfo->cport_tgtnode_clean =
15191 					    B_FALSE;
15192 				}
15193 			}
15194 			if (sata_auto_online != 0) {
15195 				cportinfo->cport_event_flags |=
15196 				    SATA_EVNT_AUTOONLINE_DEVICE;
15197 			}
15198 
15199 		}
15200 	} else {
15201 		cportinfo->cport_dev_attach_time = 0;
15202 	}
15203 
15204 	event_flags = cportinfo->cport_event_flags;
15205 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15206 	if (event_flags != 0) {
15207 		mutex_enter(&sata_hba_inst->satahba_mutex);
15208 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15209 		mutex_exit(&sata_hba_inst->satahba_mutex);
15210 		mutex_enter(&sata_mutex);
15211 		sata_event_pending |= SATA_EVNT_MAIN;
15212 		mutex_exit(&sata_mutex);
15213 	}
15214 }
15215 
15216 
15217 /*
15218  * Device Target Node Cleanup Event processing.
15219  * If the target node associated with a sata port device is in
15220  * DEVI_DEVICE_REMOVED state, an attempt is made to remove it.
15221  * If the target node cannot be removed, the event flag is left intact,
15222  * so that event daemon may re-run this function later.
15223  *
15224  * This function cannot be called in interrupt context (it may sleep).
15225  *
15226  * NOTE: Processes cport events only, not port multiplier ports.
15227  */
15228 static void
15229 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
15230     sata_address_t *saddr)
15231 {
15232 	sata_cport_info_t *cportinfo;
15233 	dev_info_t *tdip;
15234 
15235 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15236 	    "Processing port %d device target node cleanup", saddr->cport);
15237 
15238 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15239 
15240 	/*
15241 	 * Check if there is target node for that device and it is in the
15242 	 * DEVI_DEVICE_REMOVED state. If so, release it.
15243 	 */
15244 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
15245 	if (tdip != NULL) {
15246 		/*
15247 		 * target node exists - check if it is target node of
15248 		 * a removed device.
15249 		 */
15250 		if (sata_check_device_removed(tdip) == B_TRUE) {
15251 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15252 			    "sata_process_target_node_cleanup: "
15253 			    "old device target node exists!", NULL);
15254 			/*
15255 			 * Unconfigure and remove the target node
15256 			 */
15257 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) ==
15258 			    NDI_SUCCESS) {
15259 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15260 				    saddr->cport)->cport_mutex);
15261 				cportinfo->cport_event_flags &=
15262 				    ~SATA_EVNT_TARGET_NODE_CLEANUP;
15263 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15264 				    saddr->cport)->cport_mutex);
15265 				return;
15266 			}
15267 			/*
15268 			 * Event daemon will retry the cleanup later.
15269 			 */
15270 			mutex_enter(&sata_hba_inst->satahba_mutex);
15271 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15272 			mutex_exit(&sata_hba_inst->satahba_mutex);
15273 			mutex_enter(&sata_mutex);
15274 			sata_event_pending |= SATA_EVNT_MAIN;
15275 			mutex_exit(&sata_mutex);
15276 		}
15277 	} else {
15278 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15279 		    saddr->cport)->cport_mutex);
15280 		cportinfo->cport_event_flags &=
15281 		    ~SATA_EVNT_TARGET_NODE_CLEANUP;
15282 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15283 		    saddr->cport)->cport_mutex);
15284 	}
15285 }
15286 
15287 /*
15288  * Device AutoOnline Event processing.
15289  * If attached device is to be onlined, an attempt is made to online this
15290  * device, but only if there is no lingering (old) target node present.
15291  * If the device cannot be onlined, the event flag is left intact,
15292  * so that event daemon may re-run this function later.
15293  *
15294  * This function cannot be called in interrupt context (it may sleep).
15295  *
15296  * NOTE: Processes cport events only, not port multiplier ports.
15297  */
15298 static void
15299 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst,
15300     sata_address_t *saddr)
15301 {
15302 	sata_cport_info_t *cportinfo;
15303 	sata_drive_info_t *sdinfo;
15304 	sata_device_t sata_device;
15305 	dev_info_t *tdip;
15306 
15307 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15308 	    "Processing port %d attached device auto-onlining", saddr->cport);
15309 
15310 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15311 
15312 	/*
15313 	 * Check if device is present and recognized. If not, reset event.
15314 	 */
15315 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15316 	if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
15317 		/* Nothing to online */
15318 		cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
15319 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15320 		    saddr->cport)->cport_mutex);
15321 		return;
15322 	}
15323 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15324 
15325 	/*
15326 	 * Check if there is target node for this device and if it is in the
15327 	 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep
15328 	 * the event for later processing.
15329 	 */
15330 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
15331 	if (tdip != NULL) {
15332 		/*
15333 		 * target node exists - check if it is target node of
15334 		 * a removed device.
15335 		 */
15336 		if (sata_check_device_removed(tdip) == B_TRUE) {
15337 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15338 			    "sata_process_device_autoonline: "
15339 			    "old device target node exists!", NULL);
15340 			/*
15341 			 * Event daemon will retry device onlining later.
15342 			 */
15343 			mutex_enter(&sata_hba_inst->satahba_mutex);
15344 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15345 			mutex_exit(&sata_hba_inst->satahba_mutex);
15346 			mutex_enter(&sata_mutex);
15347 			sata_event_pending |= SATA_EVNT_MAIN;
15348 			mutex_exit(&sata_mutex);
15349 			return;
15350 		}
15351 		/*
15352 		 * If the target node is not in the 'removed" state, assume
15353 		 * that it belongs to this device. There is nothing more to do,
15354 		 * but reset the event.
15355 		 */
15356 	} else {
15357 
15358 		/*
15359 		 * Try to online the device
15360 		 * If there is any reset-related event, remove it. We are
15361 		 * configuring the device and no state restoring is needed.
15362 		 */
15363 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15364 		    saddr->cport)->cport_mutex);
15365 		sata_device.satadev_addr = *saddr;
15366 		if (saddr->qual == SATA_ADDR_CPORT)
15367 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
15368 		else
15369 			sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
15370 		sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
15371 		if (sdinfo != NULL) {
15372 			if (sdinfo->satadrv_event_flags &
15373 			    (SATA_EVNT_DEVICE_RESET |
15374 			    SATA_EVNT_INPROC_DEVICE_RESET))
15375 				sdinfo->satadrv_event_flags = 0;
15376 			sdinfo->satadrv_event_flags |=
15377 			    SATA_EVNT_CLEAR_DEVICE_RESET;
15378 
15379 			/* Need to create a new target node. */
15380 			cportinfo->cport_tgtnode_clean = B_TRUE;
15381 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15382 			    saddr->cport)->cport_mutex);
15383 			tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
15384 			    sata_hba_inst, &sata_device.satadev_addr);
15385 			if (tdip == NULL) {
15386 				/*
15387 				 * Configure (onlining) failed.
15388 				 * We will NOT retry
15389 				 */
15390 				SATA_LOG_D((sata_hba_inst, CE_WARN,
15391 				    "sata_process_device_autoonline: "
15392 				    "configuring SATA device at port %d failed",
15393 				    saddr->cport));
15394 			}
15395 		} else {
15396 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15397 			    saddr->cport)->cport_mutex);
15398 		}
15399 
15400 	}
15401 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15402 	cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
15403 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15404 	    saddr->cport)->cport_mutex);
15405 }
15406 
15407 
15408 static void
15409 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr,
15410     int hint)
15411 {
15412 	char ap[MAXPATHLEN];
15413 	nvlist_t *ev_attr_list = NULL;
15414 	int err;
15415 
15416 	/* Allocate and build sysevent attribute list */
15417 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP);
15418 	if (err != 0) {
15419 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15420 		    "sata_gen_sysevent: "
15421 		    "cannot allocate memory for sysevent attributes\n"));
15422 		return;
15423 	}
15424 	/* Add hint attribute */
15425 	err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint));
15426 	if (err != 0) {
15427 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15428 		    "sata_gen_sysevent: "
15429 		    "failed to add DR_HINT attr for sysevent"));
15430 		nvlist_free(ev_attr_list);
15431 		return;
15432 	}
15433 	/*
15434 	 * Add AP attribute.
15435 	 * Get controller pathname and convert it into AP pathname by adding
15436 	 * a target number.
15437 	 */
15438 	(void) snprintf(ap, MAXPATHLEN, "/devices");
15439 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap));
15440 	(void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d",
15441 	    SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual));
15442 
15443 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap);
15444 	if (err != 0) {
15445 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15446 		    "sata_gen_sysevent: "
15447 		    "failed to add DR_AP_ID attr for sysevent"));
15448 		nvlist_free(ev_attr_list);
15449 		return;
15450 	}
15451 
15452 	/* Generate/log sysevent */
15453 	err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR,
15454 	    ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP);
15455 	if (err != DDI_SUCCESS) {
15456 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15457 		    "sata_gen_sysevent: "
15458 		    "cannot log sysevent, err code %x\n", err));
15459 	}
15460 
15461 	nvlist_free(ev_attr_list);
15462 }
15463 
15464 
15465 
15466 
15467 /*
15468  * Set DEVI_DEVICE_REMOVED state in the SATA device target node.
15469  */
15470 static void
15471 sata_set_device_removed(dev_info_t *tdip)
15472 {
15473 	int circ;
15474 
15475 	ASSERT(tdip != NULL);
15476 
15477 	ndi_devi_enter(tdip, &circ);
15478 	mutex_enter(&DEVI(tdip)->devi_lock);
15479 	DEVI_SET_DEVICE_REMOVED(tdip);
15480 	mutex_exit(&DEVI(tdip)->devi_lock);
15481 	ndi_devi_exit(tdip, circ);
15482 }
15483 
15484 
15485 /*
15486  * Set internal event instructing event daemon to try
15487  * to perform the target node cleanup.
15488  */
15489 static void
15490 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
15491     sata_address_t *saddr)
15492 {
15493 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15494 	SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |=
15495 	    SATA_EVNT_TARGET_NODE_CLEANUP;
15496 	SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_tgtnode_clean =
15497 	    B_FALSE;
15498 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15499 	mutex_enter(&sata_hba_inst->satahba_mutex);
15500 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15501 	mutex_exit(&sata_hba_inst->satahba_mutex);
15502 	mutex_enter(&sata_mutex);
15503 	sata_event_pending |= SATA_EVNT_MAIN;
15504 	mutex_exit(&sata_mutex);
15505 }
15506 
15507 
15508 /*
15509  * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state,
15510  * i.e. check if the target node state indicates that it belongs to a removed
15511  * device.
15512  *
15513  * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state,
15514  * B_FALSE otherwise.
15515  *
15516  * NOTE: No port multiplier support.
15517  */
15518 static boolean_t
15519 sata_check_device_removed(dev_info_t *tdip)
15520 {
15521 	ASSERT(tdip != NULL);
15522 
15523 	if (DEVI_IS_DEVICE_REMOVED(tdip))
15524 		return (B_TRUE);
15525 	else
15526 		return (B_FALSE);
15527 }
15528 
15529 /* ************************ FAULT INJECTTION **************************** */
15530 
15531 #ifdef SATA_INJECT_FAULTS
15532 
15533 static	uint32_t sata_fault_count = 0;
15534 static	uint32_t sata_fault_suspend_count = 0;
15535 
15536 /*
15537  * Inject sata pkt fault
15538  * It modifies returned values of the sata packet.
15539  * First argument is the pointer to the executed sata packet.
15540  * The second argument specifies SATA command to be affected (not all commands
15541  * are instrumented).
15542  * Third argument is a pointer to a value returned by the HBA tran_start
15543  * function.
15544  * Fourth argument specifies injected error. Injected sata packet faults
15545  * are the satapkt_reason values.
15546  * SATA_PKT_BUSY		-1	Not completed, busy
15547  * SATA_PKT_DEV_ERROR		1	Device reported error
15548  * SATA_PKT_QUEUE_FULL		2	Not accepted, queue full
15549  * SATA_PKT_PORT_ERROR		3	Not completed, port error
15550  * SATA_PKT_CMD_UNSUPPORTED	4	Cmd unsupported
15551  * SATA_PKT_ABORTED		5	Aborted by request
15552  * SATA_PKT_TIMEOUT		6	Operation timeut
15553  * SATA_PKT_RESET		7	Aborted by reset request
15554  *
15555  * sata_inject_fault_count variable specifies number of times in row the
15556  * error is injected. Value of -1 specifies permanent fault, ie. every time
15557  * the fault injection pointnis reached, the fault is injected and anu pause
15558  * between fault injection specified by sata_inject_fault_pause_count is
15559  * ignored).
15560  *
15561  * sata_inject_fault_pause_count variable specifies number of times a fault
15562  * injection is bypassed (pause between fault injections).
15563  * If set to 0, a fault is injected only a number of times specified by
15564  * sata_inject_fault_count.
15565  *
15566  * The fault counts are static, so for periodic errors they have to be manually
15567  * reset to start repetition sequence from scratch.
15568  * If the original value returned by the HBA tran_start function is not
15569  * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error
15570  * is injected (to avoid masking real problems);
15571  *
15572  * NOTE: In its current incarnation, this function should be invoked only for
15573  * commands executed in SYNCHRONOUS mode.
15574  */
15575 
15576 
15577 static	void
15578 sata_inject_pkt_fault(sata_pkt_t *spkt, uint8_t cmd, int *rval,
15579     int fault)
15580 {
15581 	if (fault == 0)
15582 		return;
15583 	if (sata_inject_fault_count == 0)
15584 		return;
15585 
15586 	if (spkt->satapkt_cmd.satacmd_cmd_reg != cmd)
15587 		return;
15588 
15589 	if (*rval != SATA_TRAN_ACCEPTED ||
15590 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
15591 		sata_fault_count = 0;
15592 		sata_fault_suspend_count = 0;
15593 		return;
15594 	}
15595 	if (sata_fault_count == 0 && sata_fault_suspend_count != 0) {
15596 		/* Pause in the injection */
15597 		sata_fault_suspend_count -= 1;
15598 		return;
15599 	}
15600 
15601 	if (sata_fault_count == 0 && sata_fault_suspend_count == 0) {
15602 		/*
15603 		 * Init inject fault cycle. If fault count is set to -1,
15604 		 * it is a permanent fault.
15605 		 */
15606 		if (sata_inject_fault_count != -1) {
15607 			sata_fault_count = sata_inject_fault_count;
15608 			sata_fault_suspend_count =
15609 			    sata_inject_fault_pause_count;
15610 			if (sata_fault_suspend_count == 0)
15611 				sata_inject_fault_count = 0;
15612 		}
15613 	}
15614 
15615 	if (sata_fault_count != 0)
15616 		sata_fault_count -= 1;
15617 
15618 	switch (fault) {
15619 	case SATA_PKT_BUSY:
15620 		*rval = SATA_TRAN_BUSY;
15621 		spkt->satapkt_reason = SATA_PKT_BUSY;
15622 		break;
15623 
15624 	case SATA_PKT_QUEUE_FULL:
15625 		*rval = SATA_TRAN_QUEUE_FULL;
15626 		spkt->satapkt_reason = SATA_PKT_QUEUE_FULL;
15627 		break;
15628 
15629 	case SATA_PKT_CMD_UNSUPPORTED:
15630 		*rval = SATA_TRAN_CMD_UNSUPPORTED;
15631 		spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED;
15632 		break;
15633 
15634 	case SATA_PKT_PORT_ERROR:
15635 		/* This is "rejected" command */
15636 		*rval = SATA_TRAN_PORT_ERROR;
15637 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
15638 		/* Additional error setup could be done here - port state */
15639 		break;
15640 
15641 	case SATA_PKT_DEV_ERROR:
15642 		spkt->satapkt_reason = SATA_PKT_DEV_ERROR;
15643 		/*
15644 		 * Additional error setup could be done here
15645 		 */
15646 		break;
15647 
15648 	case SATA_PKT_ABORTED:
15649 		spkt->satapkt_reason = SATA_PKT_ABORTED;
15650 		break;
15651 
15652 	case SATA_PKT_TIMEOUT:
15653 		spkt->satapkt_reason = SATA_PKT_TIMEOUT;
15654 		/* Additional error setup could be done here */
15655 		break;
15656 
15657 	case SATA_PKT_RESET:
15658 		spkt->satapkt_reason = SATA_PKT_RESET;
15659 		/*
15660 		 * Additional error setup could be done here - device reset
15661 		 */
15662 		break;
15663 
15664 	default:
15665 		break;
15666 	}
15667 }
15668 
15669 #endif
15670