xref: /titanic_51/usr/src/uts/common/io/sata/impl/sata.c (revision 9da57d7b0ddd8d73b676ce12c040362132cdd538)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * SATA Framework
31  * Generic SATA Host Adapter Implementation
32  */
33 
34 #include <sys/conf.h>
35 #include <sys/file.h>
36 #include <sys/ddi.h>
37 #include <sys/sunddi.h>
38 #include <sys/modctl.h>
39 #include <sys/cmn_err.h>
40 #include <sys/errno.h>
41 #include <sys/thread.h>
42 #include <sys/kstat.h>
43 #include <sys/note.h>
44 #include <sys/sysevent.h>
45 #include <sys/sysevent/eventdefs.h>
46 #include <sys/sysevent/dr.h>
47 #include <sys/taskq.h>
48 
49 #include <sys/sata/impl/sata.h>
50 #include <sys/sata/sata_hba.h>
51 #include <sys/sata/sata_defs.h>
52 #include <sys/sata/sata_cfgadm.h>
53 
54 /* Debug flags - defined in sata.h */
55 int	sata_debug_flags = 0;
56 int	sata_msg = 0;
57 
58 /*
59  * Flags enabling selected SATA HBA framework functionality
60  */
61 #define	SATA_ENABLE_QUEUING		1
62 #define	SATA_ENABLE_NCQ			2
63 #define	SATA_ENABLE_PROCESS_EVENTS	4
64 int sata_func_enable =
65 	SATA_ENABLE_PROCESS_EVENTS | SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ;
66 
67 /*
68  * Global variable setting default maximum queue depth (NCQ or TCQ)
69  * Note:minimum queue depth is 1
70  */
71 int sata_max_queue_depth = SATA_MAX_QUEUE_DEPTH; /* max NCQ/TCQ queue depth */
72 
73 /*
74  * Currently used default NCQ/TCQ queue depth. It is set-up during the driver
75  * initialization, using value from sata_max_queue_depth
76  * It is adjusted to minimum supported by the controller and by the device,
77  * if queueing is enabled.
78  */
79 static	int sata_current_max_qdepth;
80 
81 /*
82  * Global variable determining the default behavior after device hotpluggin.
83  * If non-zero, the hotplugged device is onlined (if possible) without explicit
84  * IOCTL request (AP_CONFIGURE).
85  * If zero, hotplugged device is identified, but not onlined.
86  * Enabling (AP_CONNECT) device port with an attached device does not result
87  * in device onlining regardless of the flag setting
88  */
89 int sata_auto_online = 0;
90 
91 #ifdef SATA_DEBUG
92 
93 #define	SATA_LOG_D(args)	sata_log args
94 uint64_t mbuf_count = 0;
95 uint64_t mbuffail_count = 0;
96 
97 sata_atapi_cmd_t sata_atapi_trace[64];
98 uint32_t sata_atapi_trace_index = 0;
99 int sata_atapi_trace_save = 1;
100 static	void sata_save_atapi_trace(sata_pkt_txlate_t *, int);
101 #define	SATAATAPITRACE(spx, count)	if (sata_atapi_trace_save) \
102     sata_save_atapi_trace(spx, count);
103 
104 #else
105 #define	SATA_LOG_D(arg)
106 #define	SATAATAPITRACE(spx, count)
107 #endif
108 
109 #if 0
110 static void
111 sata_test_atapi_packet_command(sata_hba_inst_t *, int);
112 #endif
113 
114 #ifdef SATA_INJECT_FAULTS
115 
116 #define		SATA_INJECT_PKT_FAULT	1
117 uint32_t	sata_inject_fault = 0;
118 
119 uint32_t	sata_fault_cmd = 0;
120 uint32_t	sata_inject_fault_type = 0;
121 uint32_t	sata_inject_fault_count = 0;
122 uint32_t	sata_inject_fault_pause_count = 0;
123 
124 static	void sata_inject_pkt_fault(sata_pkt_t *, uint8_t, int *, int);
125 
126 #endif
127 
128 #define	LEGACY_HWID_LEN	64	/* Model (40) + Serial (20) + pad */
129 
130 /*
131  * SATA cb_ops functions
132  */
133 static 	int sata_hba_open(dev_t *, int, int, cred_t *);
134 static 	int sata_hba_close(dev_t, int, int, cred_t *);
135 static 	int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *,	int *);
136 
137 /*
138  * SCSA required entry points
139  */
140 static	int sata_scsi_tgt_init(dev_info_t *, dev_info_t *,
141     scsi_hba_tran_t *, struct scsi_device *);
142 static	int sata_scsi_tgt_probe(struct scsi_device *,
143     int (*callback)(void));
144 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *,
145     scsi_hba_tran_t *, struct scsi_device *);
146 static 	int sata_scsi_start(struct scsi_address *, struct scsi_pkt *);
147 static 	int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *);
148 static 	int sata_scsi_reset(struct scsi_address *, int);
149 static 	int sata_scsi_getcap(struct scsi_address *, char *, int);
150 static 	int sata_scsi_setcap(struct scsi_address *, char *, int, int);
151 static 	struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *,
152     struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t),
153     caddr_t);
154 static 	void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *);
155 static 	void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *);
156 static 	void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *);
157 
158 /*
159  * SATA HBA interface functions are defined in sata_hba.h header file
160  */
161 
162 /* Event processing functions */
163 static	void sata_event_daemon(void *);
164 static	void sata_event_thread_control(int);
165 static	void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst);
166 static	void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *);
167 static	void sata_process_port_failed_event(sata_hba_inst_t *,
168     sata_address_t *);
169 static	void sata_process_port_link_events(sata_hba_inst_t *,
170     sata_address_t *);
171 static	void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *);
172 static	void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *);
173 static	void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *);
174 static	void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *);
175 static	void sata_process_target_node_cleanup(sata_hba_inst_t *,
176     sata_address_t *);
177 static	void sata_process_device_autoonline(sata_hba_inst_t *,
178     sata_address_t *saddr);
179 
180 /*
181  * Local translation functions
182  */
183 static	int sata_txlt_inquiry(sata_pkt_txlate_t *);
184 static	int sata_txlt_test_unit_ready(sata_pkt_txlate_t *);
185 static	int sata_txlt_start_stop_unit(sata_pkt_txlate_t *);
186 static	int sata_txlt_read_capacity(sata_pkt_txlate_t *);
187 static	int sata_txlt_request_sense(sata_pkt_txlate_t *);
188 static 	int sata_txlt_read(sata_pkt_txlate_t *);
189 static 	int sata_txlt_write(sata_pkt_txlate_t *);
190 static 	int sata_txlt_log_sense(sata_pkt_txlate_t *);
191 static 	int sata_txlt_log_select(sata_pkt_txlate_t *);
192 static 	int sata_txlt_mode_sense(sata_pkt_txlate_t *);
193 static 	int sata_txlt_mode_select(sata_pkt_txlate_t *);
194 static 	int sata_txlt_synchronize_cache(sata_pkt_txlate_t *);
195 static 	int sata_txlt_write_buffer(sata_pkt_txlate_t *);
196 static 	int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *);
197 
198 static 	int sata_hba_start(sata_pkt_txlate_t *, int *);
199 static	int sata_txlt_invalid_command(sata_pkt_txlate_t *);
200 static	int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *);
201 static 	void sata_txlt_rw_completion(sata_pkt_t *);
202 static 	void sata_txlt_nodata_cmd_completion(sata_pkt_t *);
203 static 	void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *);
204 static 	int sata_emul_rw_completion(sata_pkt_txlate_t *);
205 static 	struct scsi_extended_sense *sata_immediate_error_response(
206     sata_pkt_txlate_t *, int);
207 static	struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *);
208 
209 static 	int sata_txlt_atapi(sata_pkt_txlate_t *);
210 static 	void sata_txlt_atapi_completion(sata_pkt_t *);
211 
212 /*
213  * Local functions for ioctl
214  */
215 static	int32_t sata_get_port_num(sata_hba_inst_t *,  struct devctl_iocdata *);
216 static	void sata_cfgadm_state(sata_hba_inst_t *, int32_t,
217     devctl_ap_state_t *);
218 static	dev_info_t *sata_get_target_dip(dev_info_t *, int32_t);
219 static	dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *);
220 static	dev_info_t *sata_devt_to_devinfo(dev_t);
221 static	int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *);
222 static	int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *);
223 static	int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *);
224 static	int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *);
225 static	int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *);
226 static	int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *);
227 static	int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *);
228 static	int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *);
229 static	int sata_ioctl_reset_all(sata_hba_inst_t *);
230 static	int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *);
231 static	int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *,
232     sata_ioctl_data_t *, int mode);
233 static	int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *,
234     sata_ioctl_data_t *, int mode);
235 static	int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *,
236     sata_ioctl_data_t *, int mode);
237 static	int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *,
238     sata_ioctl_data_t *, int mode);
239 static	int sata_ioctl_get_serialnumber_info(sata_hba_inst_t *,
240     sata_device_t *, sata_ioctl_data_t *, int mode);
241 
242 /*
243  * Local functions
244  */
245 static 	void sata_remove_hba_instance(dev_info_t *);
246 static 	int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *);
247 static 	void sata_probe_ports(sata_hba_inst_t *);
248 static 	int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int);
249 static 	int sata_add_device(dev_info_t *, sata_hba_inst_t *, int cport,
250     int pmport);
251 static 	dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *,
252     sata_address_t *);
253 static 	int sata_validate_scsi_address(sata_hba_inst_t *,
254     struct scsi_address *, sata_device_t *);
255 static 	int sata_validate_sata_address(sata_hba_inst_t *, int, int, int);
256 static	sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t));
257 static	void sata_pkt_free(sata_pkt_txlate_t *);
258 static	int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t),
259     caddr_t, ddi_dma_attr_t *);
260 static	int sata_probe_device(sata_hba_inst_t *, sata_device_t *);
261 static	sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *,
262     sata_device_t *);
263 static 	int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *);
264 static	void sata_reidentify_device(sata_pkt_txlate_t *);
265 static	struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int);
266 static 	void sata_free_local_buffer(sata_pkt_txlate_t *);
267 static 	uint64_t sata_check_capacity(sata_drive_info_t *);
268 void 	sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *,
269     ddi_dma_attr_t *);
270 static 	int sata_fetch_device_identify_data(sata_hba_inst_t *,
271     sata_drive_info_t *);
272 static	void sata_update_port_info(sata_hba_inst_t *, sata_device_t *);
273 static	void sata_update_port_scr(sata_port_scr_t *, sata_device_t *);
274 static	int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *);
275 static	int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int);
276 static	int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int);
277 static	int sata_set_drive_features(sata_hba_inst_t *,
278     sata_drive_info_t *, int flag);
279 static	void sata_init_write_cache_mode(sata_drive_info_t *sdinfo);
280 static	int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *);
281 static	void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *,
282     uint8_t *);
283 static	int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *,
284     struct scsi_inquiry *);
285 static	int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *);
286 static	int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *);
287 static	int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *);
288 static	int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *);
289 static	int sata_mode_select_page_8(sata_pkt_txlate_t *,
290     struct mode_cache_scsi3 *, int, int *, int *, int *);
291 static	int sata_mode_select_page_1c(sata_pkt_txlate_t *,
292     struct mode_info_excpt_page *, int, int *, int *, int *);
293 static	int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *);
294 static	int sata_mode_select_page_30(sata_pkt_txlate_t *,
295     struct mode_acoustic_management *, int, int *, int *, int *);
296 
297 static	int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *);
298 static	int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *,
299     sata_hba_inst_t *);
300 static	int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *,
301     sata_hba_inst_t *);
302 static	int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *,
303     sata_hba_inst_t *);
304 static	void sata_save_drive_settings(sata_drive_info_t *);
305 static	void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *);
306 static	void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...);
307 static	int sata_fetch_smart_return_status(sata_hba_inst_t *,
308     sata_drive_info_t *);
309 static	int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *,
310     struct smart_data *);
311 static	int sata_smart_selftest_log(sata_hba_inst_t *,
312     sata_drive_info_t *,
313     struct smart_selftest_log *);
314 static	int sata_ext_smart_selftest_read_log(sata_hba_inst_t *,
315     sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t);
316 static	int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *,
317     uint8_t *, uint8_t, uint8_t);
318 static	int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *,
319     struct read_log_ext_directory *);
320 static	void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int);
321 static	void sata_xlate_errors(sata_pkt_txlate_t *);
322 static	void sata_decode_device_error(sata_pkt_txlate_t *,
323     struct scsi_extended_sense *);
324 static	void sata_set_device_removed(dev_info_t *);
325 static	boolean_t sata_check_device_removed(dev_info_t *);
326 static	void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *);
327 static	int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *,
328     sata_drive_info_t *);
329 static	int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *,
330     sata_drive_info_t *);
331 static	void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *);
332 static	void sata_fixed_sense_data_preset(struct scsi_extended_sense *);
333 static  void sata_target_devid_register(dev_info_t *, sata_drive_info_t *);
334 static  int sata_check_modser(char *, int);
335 
336 
337 
338 /*
339  * SATA Framework will ignore SATA HBA driver cb_ops structure and
340  * register following one with SCSA framework.
341  * Open & close are provided, so scsi framework will not use its own
342  */
343 static struct cb_ops sata_cb_ops = {
344 	sata_hba_open,			/* open */
345 	sata_hba_close,			/* close */
346 	nodev,				/* strategy */
347 	nodev,				/* print */
348 	nodev,				/* dump */
349 	nodev,				/* read */
350 	nodev,				/* write */
351 	sata_hba_ioctl,			/* ioctl */
352 	nodev,				/* devmap */
353 	nodev,				/* mmap */
354 	nodev,				/* segmap */
355 	nochpoll,			/* chpoll */
356 	ddi_prop_op,			/* cb_prop_op */
357 	0,				/* streamtab */
358 	D_NEW | D_MP,			/* cb_flag */
359 	CB_REV,				/* rev */
360 	nodev,				/* aread */
361 	nodev				/* awrite */
362 };
363 
364 
365 extern struct mod_ops mod_miscops;
366 extern uchar_t	scsi_cdb_size[];
367 
368 static struct modlmisc modlmisc = {
369 	&mod_miscops,			/* Type of module */
370 	"SATA Module v%I%"		/* module name */
371 };
372 
373 
374 static struct modlinkage modlinkage = {
375 	MODREV_1,
376 	(void *)&modlmisc,
377 	NULL
378 };
379 
380 /*
381  * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero,
382  * i.e. when scsi_pkt has not timeout specified.
383  */
384 static int sata_default_pkt_time = 60;	/* 60 seconds */
385 
386 /*
387  * Intermediate buffer device access attributes - they are required,
388  * but not necessarily used.
389  */
390 static ddi_device_acc_attr_t sata_acc_attr = {
391 	DDI_DEVICE_ATTR_V0,
392 	DDI_STRUCTURE_LE_ACC,
393 	DDI_STRICTORDER_ACC
394 };
395 
396 
397 /*
398  * Mutexes protecting structures in multithreaded operations.
399  * Because events are relatively rare, a single global mutex protecting
400  * data structures should be sufficient. To increase performance, add
401  * separate mutex per each sata port and use global mutex only to protect
402  * common data structures.
403  */
404 static	kmutex_t sata_mutex;		/* protects sata_hba_list */
405 static	kmutex_t sata_log_mutex;	/* protects log */
406 
407 static 	char sata_log_buf[256];
408 
409 /* Default write cache setting for SATA hard disks */
410 int	sata_write_cache = 1;		/* enabled */
411 
412 /* Default write cache setting for SATA ATAPI CD/DVD */
413 int 	sata_atapicdvd_write_cache = 1; /* enabled */
414 
415 /*
416  * Linked list of HBA instances
417  */
418 static 	sata_hba_inst_t *sata_hba_list = NULL;
419 static 	sata_hba_inst_t *sata_hba_list_tail = NULL;
420 /*
421  * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran
422  * structure and in sata soft state.
423  */
424 
425 /*
426  * Event daemon related variables
427  */
428 static 	kmutex_t sata_event_mutex;
429 static 	kcondvar_t sata_event_cv;
430 static 	kthread_t *sata_event_thread = NULL;
431 static 	int sata_event_thread_terminate = 0;
432 static 	int sata_event_pending = 0;
433 static 	int sata_event_thread_active = 0;
434 extern 	pri_t minclsyspri;
435 
436 /*
437  * NCQ error recovery command
438  */
439 static const sata_cmd_t sata_rle_cmd = {
440 	SATA_CMD_REV,
441 	NULL,
442 	{
443 		SATA_DIR_READ
444 	},
445 	ATA_ADDR_LBA48,
446 	0,
447 	0,
448 	0,
449 	0,
450 	0,
451 	1,
452 	READ_LOG_EXT_NCQ_ERROR_RECOVERY,
453 	0,
454 	0,
455 	0,
456 	SATAC_READ_LOG_EXT,
457 	0,
458 	0,
459 	0,
460 };
461 
462 /*
463  * ATAPI error recovery CDB
464  */
465 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = {
466 	SCMD_REQUEST_SENSE,
467 	0,			/* Only fixed RQ format is supported */
468 	0,
469 	0,
470 	SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */
471 	0
472 };
473 
474 
475 /* Warlock directives */
476 
477 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran))
478 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device))
479 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops))
480 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense))
481 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status))
482 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr))
483 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t))
484 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state))
485 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state))
486 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list))
487 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list))
488 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next))
489 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev))
490 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \
491     sata_hba_inst::satahba_scsi_tran))
492 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran))
493 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip))
494 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached))
495 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port))
496 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex,
497     sata_hba_inst::satahba_event_flags))
498 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
499     sata_cport_info::cport_devp))
500 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp))
501 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr))
502 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
503     sata_cport_info::cport_dev_type))
504 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type))
505 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
506     sata_cport_info::cport_state))
507 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state))
508 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
509     sata_pmport_info::pmport_state))
510 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state))
511 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type))
512 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive))
513 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port))
514 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports))
515 #ifdef SATA_DEBUG
516 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count))
517 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count))
518 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace))
519 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index))
520 #endif
521 
522 /* End of warlock directives */
523 
524 /* ************** loadable module configuration functions ************** */
525 
526 int
527 _init()
528 {
529 	int rval;
530 
531 	mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL);
532 	mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL);
533 	mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL);
534 	cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL);
535 	if ((rval = mod_install(&modlinkage)) != 0) {
536 #ifdef SATA_DEBUG
537 		cmn_err(CE_WARN, "sata: _init: mod_install failed\n");
538 #endif
539 		mutex_destroy(&sata_log_mutex);
540 		cv_destroy(&sata_event_cv);
541 		mutex_destroy(&sata_event_mutex);
542 		mutex_destroy(&sata_mutex);
543 	}
544 	return (rval);
545 }
546 
547 int
548 _fini()
549 {
550 	int rval;
551 
552 	if ((rval = mod_remove(&modlinkage)) != 0)
553 		return (rval);
554 
555 	mutex_destroy(&sata_log_mutex);
556 	cv_destroy(&sata_event_cv);
557 	mutex_destroy(&sata_event_mutex);
558 	mutex_destroy(&sata_mutex);
559 	return (rval);
560 }
561 
562 int
563 _info(struct modinfo *modinfop)
564 {
565 	return (mod_info(&modlinkage, modinfop));
566 }
567 
568 
569 
570 /* ********************* SATA HBA entry points ********************* */
571 
572 
573 /*
574  * Called by SATA HBA from _init().
575  * Registers HBA driver instance/sata framework pair with scsi framework, by
576  * calling scsi_hba_init().
577  *
578  * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used
579  * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver
580  * cb_ops pointer in SATA HBA driver dev_ops structure.
581  * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors.
582  *
583  * Return status of the scsi_hba_init() is returned to a calling SATA HBA
584  * driver.
585  */
586 int
587 sata_hba_init(struct modlinkage *modlp)
588 {
589 	int rval;
590 	struct dev_ops *hba_ops;
591 
592 	SATADBG1(SATA_DBG_HBA_IF, NULL,
593 	    "sata_hba_init: name %s \n",
594 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
595 	/*
596 	 * Fill-up cb_ops and dev_ops when necessary
597 	 */
598 	hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
599 	/*
600 	 * Provide pointer to SATA dev_ops
601 	 */
602 	hba_ops->devo_cb_ops = &sata_cb_ops;
603 
604 	/*
605 	 * Register SATA HBA with SCSI framework
606 	 */
607 	if ((rval = scsi_hba_init(modlp)) != 0) {
608 		SATADBG1(SATA_DBG_HBA_IF, NULL,
609 		    "sata_hba_init: scsi hba init failed\n", NULL);
610 		return (rval);
611 	}
612 
613 	return (0);
614 }
615 
616 
617 /* HBA attach stages */
618 #define	HBA_ATTACH_STAGE_SATA_HBA_INST	1
619 #define	HBA_ATTACH_STAGE_SCSI_ATTACHED	2
620 #define	HBA_ATTACH_STAGE_SETUP		4
621 #define	HBA_ATTACH_STAGE_LINKED		8
622 
623 
624 /*
625  *
626  * Called from SATA HBA driver's attach routine to attach an instance of
627  * the HBA.
628  *
629  * For DDI_ATTACH command:
630  * sata_hba_inst structure is allocated here and initialized with pointers to
631  * SATA framework implementation of required scsi tran functions.
632  * The scsi_tran's tran_hba_private field is used by SATA Framework to point
633  * to the soft structure (sata_hba_inst) allocated by SATA framework for
634  * SATA HBA instance related data.
635  * The scsi_tran's tran_hba_private field is used by SATA framework to
636  * store a pointer to per-HBA-instance of sata_hba_inst structure.
637  * The sata_hba_inst structure is cross-linked to scsi tran structure.
638  * Among other info, a pointer to sata_hba_tran structure is stored in
639  * sata_hba_inst. The sata_hba_inst structures for different HBA instances are
640  * linked together into the list, pointed to by sata_hba_list.
641  * On the first HBA instance attach the sata event thread is initialized.
642  * Attachment points are created for all SATA ports of the HBA being attached.
643  * All HBA instance's SATA ports are probed and type of plugged devices is
644  * determined. For each device of a supported type, a target node is created.
645  *
646  * DDI_SUCCESS is returned when attachment process is successful,
647  * DDI_FAILURE is returned otherwise.
648  *
649  * For DDI_RESUME command:
650  * Not implemented at this time (postponed until phase 2 of the development).
651  */
652 int
653 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran,
654     ddi_attach_cmd_t cmd)
655 {
656 	sata_hba_inst_t	*sata_hba_inst;
657 	scsi_hba_tran_t *scsi_tran = NULL;
658 	int hba_attach_state = 0;
659 	char taskq_name[MAXPATHLEN];
660 
661 	SATADBG3(SATA_DBG_HBA_IF, NULL,
662 	    "sata_hba_attach: node %s (%s%d)\n",
663 	    ddi_node_name(dip), ddi_driver_name(dip),
664 	    ddi_get_instance(dip));
665 
666 	if (cmd == DDI_RESUME) {
667 		/*
668 		 * Postponed until phase 2 of the development
669 		 */
670 		return (DDI_FAILURE);
671 	}
672 
673 	if (cmd != DDI_ATTACH) {
674 		return (DDI_FAILURE);
675 	}
676 
677 	/* cmd == DDI_ATTACH */
678 
679 	if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) {
680 		SATA_LOG_D((NULL, CE_WARN,
681 		    "sata_hba_attach: invalid sata_hba_tran"));
682 		return (DDI_FAILURE);
683 	}
684 	/*
685 	 * Allocate and initialize SCSI tran structure.
686 	 * SATA copy of tran_bus_config is provided to create port nodes.
687 	 */
688 	scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
689 	if (scsi_tran == NULL)
690 		return (DDI_FAILURE);
691 	/*
692 	 * Allocate soft structure for SATA HBA instance.
693 	 * There is a separate softstate for each HBA instance.
694 	 */
695 	sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP);
696 	ASSERT(sata_hba_inst != NULL); /* this should not fail */
697 	mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL);
698 	hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST;
699 
700 	/*
701 	 * scsi_trans's tran_hba_private is used by SATA Framework to point to
702 	 * soft structure allocated by SATA framework for
703 	 * SATA HBA instance related data.
704 	 */
705 	scsi_tran->tran_hba_private	= sata_hba_inst;
706 	scsi_tran->tran_tgt_private	= NULL;
707 
708 	scsi_tran->tran_tgt_init	= sata_scsi_tgt_init;
709 	scsi_tran->tran_tgt_probe	= sata_scsi_tgt_probe;
710 	scsi_tran->tran_tgt_free	= sata_scsi_tgt_free;
711 
712 	scsi_tran->tran_start		= sata_scsi_start;
713 	scsi_tran->tran_reset		= sata_scsi_reset;
714 	scsi_tran->tran_abort		= sata_scsi_abort;
715 	scsi_tran->tran_getcap		= sata_scsi_getcap;
716 	scsi_tran->tran_setcap		= sata_scsi_setcap;
717 	scsi_tran->tran_init_pkt	= sata_scsi_init_pkt;
718 	scsi_tran->tran_destroy_pkt	= sata_scsi_destroy_pkt;
719 
720 	scsi_tran->tran_dmafree		= sata_scsi_dmafree;
721 	scsi_tran->tran_sync_pkt	= sata_scsi_sync_pkt;
722 
723 	scsi_tran->tran_reset_notify	= NULL;
724 	scsi_tran->tran_get_bus_addr	= NULL;
725 	scsi_tran->tran_quiesce		= NULL;
726 	scsi_tran->tran_unquiesce	= NULL;
727 	scsi_tran->tran_bus_reset	= NULL;
728 
729 	if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr,
730 	    scsi_tran, 0) != DDI_SUCCESS) {
731 #ifdef SATA_DEBUG
732 		cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed",
733 		    ddi_driver_name(dip), ddi_get_instance(dip));
734 #endif
735 		goto fail;
736 	}
737 	hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED;
738 
739 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) {
740 		if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
741 		    "sata", 1) != DDI_PROP_SUCCESS) {
742 			SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: "
743 			    "failed to create hba sata prop"));
744 			goto fail;
745 		}
746 	}
747 
748 	/*
749 	 * Save pointers in hba instance soft state.
750 	 */
751 	sata_hba_inst->satahba_scsi_tran = scsi_tran;
752 	sata_hba_inst->satahba_tran = sata_tran;
753 	sata_hba_inst->satahba_dip = dip;
754 
755 	/*
756 	 * Create a task queue to handle emulated commands completion
757 	 * Use node name, dash, instance number as the queue name.
758 	 */
759 	taskq_name[0] = '\0';
760 	(void) strlcat(taskq_name, DEVI(dip)->devi_node_name,
761 	    sizeof (taskq_name));
762 	(void) snprintf(taskq_name + strlen(taskq_name),
763 	    sizeof (taskq_name) - strlen(taskq_name),
764 	    "-%d", DEVI(dip)->devi_instance);
765 	sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1,
766 	    minclsyspri, 1, sata_tran->sata_tran_hba_num_cports,
767 	    TASKQ_DYNAMIC);
768 
769 	hba_attach_state |= HBA_ATTACH_STAGE_SETUP;
770 
771 	/*
772 	 * Create events thread if not created yet.
773 	 */
774 	sata_event_thread_control(1);
775 
776 	/*
777 	 * Link this hba instance into the list.
778 	 */
779 	mutex_enter(&sata_mutex);
780 
781 	if (sata_hba_list == NULL) {
782 		/*
783 		 * The first instance of HBA is attached.
784 		 * Set current/active default maximum NCQ/TCQ queue depth for
785 		 * all SATA devices. It is done here and now, to eliminate the
786 		 * possibility of the dynamic, programatic modification of the
787 		 * queue depth via global (and public) sata_max_queue_depth
788 		 * variable (this would require special handling in HBA drivers)
789 		 */
790 		sata_current_max_qdepth = sata_max_queue_depth;
791 		if (sata_current_max_qdepth > 32)
792 			sata_current_max_qdepth = 32;
793 		else if (sata_current_max_qdepth < 1)
794 			sata_current_max_qdepth = 1;
795 	}
796 
797 	sata_hba_inst->satahba_next = NULL;
798 	sata_hba_inst->satahba_prev = sata_hba_list_tail;
799 	if (sata_hba_list == NULL) {
800 		sata_hba_list = sata_hba_inst;
801 	}
802 	if (sata_hba_list_tail != NULL) {
803 		sata_hba_list_tail->satahba_next = sata_hba_inst;
804 	}
805 	sata_hba_list_tail = sata_hba_inst;
806 	mutex_exit(&sata_mutex);
807 	hba_attach_state |= HBA_ATTACH_STAGE_LINKED;
808 
809 	/*
810 	 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl
811 	 * SATA HBA driver should not use its own open/close entry points.
812 	 *
813 	 * Make sure that instance number doesn't overflow
814 	 * when forming minor numbers.
815 	 */
816 	ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT));
817 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
818 	    INST2DEVCTL(ddi_get_instance(dip)),
819 	    DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) {
820 #ifdef SATA_DEBUG
821 		cmn_err(CE_WARN, "sata_hba_attach: "
822 		    "cannot create devctl minor node");
823 #endif
824 		goto fail;
825 	}
826 
827 
828 	/*
829 	 * Set-up kstats here, if necessary.
830 	 * (postponed until phase 2 of the development).
831 	 */
832 
833 
834 	/*
835 	 * Probe controller ports. This operation will describe a current
836 	 * controller/port/multipliers/device configuration and will create
837 	 * attachment points.
838 	 * We may end-up with just a controller with no devices attached.
839 	 * For the ports with a supported device attached, device target nodes
840 	 * are created and devices are initialized.
841 	 */
842 	sata_probe_ports(sata_hba_inst);
843 
844 	sata_hba_inst->satahba_attached = 1;
845 	return (DDI_SUCCESS);
846 
847 fail:
848 	if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) {
849 		(void) sata_remove_hba_instance(dip);
850 		if (sata_hba_list == NULL)
851 			sata_event_thread_control(0);
852 	}
853 
854 	if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) {
855 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
856 		taskq_destroy(sata_hba_inst->satahba_taskq);
857 	}
858 
859 	if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED)
860 		(void) scsi_hba_detach(dip);
861 
862 	if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) {
863 		mutex_destroy(&sata_hba_inst->satahba_mutex);
864 		kmem_free((void *)sata_hba_inst,
865 		    sizeof (struct sata_hba_inst));
866 		scsi_hba_tran_free(scsi_tran);
867 	}
868 
869 	sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed",
870 	    ddi_driver_name(dip), ddi_get_instance(dip));
871 
872 	return (DDI_FAILURE);
873 }
874 
875 
876 /*
877  * Called by SATA HBA from to detach an instance of the driver.
878  *
879  * For DDI_DETACH command:
880  * Free local structures allocated for SATA HBA instance during
881  * sata_hba_attach processing.
882  *
883  * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise.
884  *
885  * For DDI_SUSPEND command:
886  * Not implemented at this time (postponed until phase 2 of the development)
887  * Returnd DDI_SUCCESS.
888  *
889  * When the last HBA instance is detached, the event daemon is terminated.
890  *
891  * NOTE: cport support only, no port multiplier support.
892  */
893 int
894 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
895 {
896 	dev_info_t	*tdip;
897 	sata_hba_inst_t	*sata_hba_inst;
898 	scsi_hba_tran_t *scsi_hba_tran;
899 	sata_cport_info_t *cportinfo;
900 	sata_drive_info_t *sdinfo;
901 	int ncport;
902 
903 	SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n",
904 	    ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip));
905 
906 	switch (cmd) {
907 	case DDI_DETACH:
908 
909 		if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
910 			return (DDI_FAILURE);
911 
912 		sata_hba_inst = scsi_hba_tran->tran_hba_private;
913 		if (sata_hba_inst == NULL)
914 			return (DDI_FAILURE);
915 
916 		if (scsi_hba_detach(dip) == DDI_FAILURE) {
917 			sata_hba_inst->satahba_attached = 1;
918 			return (DDI_FAILURE);
919 		}
920 
921 		/*
922 		 * Free all target nodes - at this point
923 		 * devices should be at least offlined
924 		 * otherwise scsi_hba_detach() should not be called.
925 		 */
926 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
927 		    ncport++) {
928 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
929 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
930 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
931 				if (sdinfo != NULL) {
932 					tdip = sata_get_target_dip(dip,
933 					    ncport);
934 					if (tdip != NULL) {
935 						if (ndi_devi_offline(tdip,
936 						    NDI_DEVI_REMOVE) !=
937 						    NDI_SUCCESS) {
938 							SATA_LOG_D((
939 							    sata_hba_inst,
940 							    CE_WARN,
941 							    "sata_hba_detach: "
942 							    "Target node not "
943 							    "removed !"));
944 							return (DDI_FAILURE);
945 						}
946 					}
947 				}
948 			}
949 		}
950 		/*
951 		 * Disable sata event daemon processing for this HBA
952 		 */
953 		sata_hba_inst->satahba_attached = 0;
954 
955 		/*
956 		 * Remove event daemon thread, if it is last HBA instance.
957 		 */
958 
959 		mutex_enter(&sata_mutex);
960 		if (sata_hba_list->satahba_next == NULL) {
961 			mutex_exit(&sata_mutex);
962 			sata_event_thread_control(0);
963 			mutex_enter(&sata_mutex);
964 		}
965 		mutex_exit(&sata_mutex);
966 
967 		/* Remove this HBA instance from the HBA list */
968 		sata_remove_hba_instance(dip);
969 
970 		/*
971 		 * At this point there should be no target nodes attached.
972 		 * Detach and destroy device and port info structures.
973 		 */
974 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
975 		    ncport++) {
976 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
977 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
978 				sdinfo =
979 				    cportinfo->cport_devp.cport_sata_drive;
980 				if (sdinfo != NULL) {
981 					/* Release device structure */
982 					kmem_free(sdinfo,
983 					    sizeof (sata_drive_info_t));
984 				}
985 				/* Release cport info */
986 				mutex_destroy(&cportinfo->cport_mutex);
987 				kmem_free(cportinfo,
988 				    sizeof (sata_cport_info_t));
989 			}
990 		}
991 
992 		scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran);
993 
994 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
995 
996 		taskq_destroy(sata_hba_inst->satahba_taskq);
997 
998 		mutex_destroy(&sata_hba_inst->satahba_mutex);
999 		kmem_free((void *)sata_hba_inst,
1000 		    sizeof (struct sata_hba_inst));
1001 
1002 		return (DDI_SUCCESS);
1003 
1004 	case DDI_SUSPEND:
1005 		/*
1006 		 * Postponed until phase 2
1007 		 */
1008 		return (DDI_FAILURE);
1009 
1010 	default:
1011 		return (DDI_FAILURE);
1012 	}
1013 }
1014 
1015 
1016 /*
1017  * Called by an HBA drive from _fini() routine.
1018  * Unregisters SATA HBA instance/SATA framework pair from the scsi framework.
1019  */
1020 void
1021 sata_hba_fini(struct modlinkage *modlp)
1022 {
1023 	SATADBG1(SATA_DBG_HBA_IF, NULL,
1024 	    "sata_hba_fini: name %s\n",
1025 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
1026 
1027 	scsi_hba_fini(modlp);
1028 }
1029 
1030 
1031 /*
1032  * Default open and close routine for sata_hba framework.
1033  *
1034  */
1035 /*
1036  * Open devctl node.
1037  *
1038  * Returns:
1039  * 0 if node was open successfully, error code otherwise.
1040  *
1041  *
1042  */
1043 
1044 static int
1045 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1046 {
1047 #ifndef __lock_lint
1048 	_NOTE(ARGUNUSED(credp))
1049 #endif
1050 	int rv = 0;
1051 	dev_info_t *dip;
1052 	scsi_hba_tran_t *scsi_hba_tran;
1053 	sata_hba_inst_t	*sata_hba_inst;
1054 
1055 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL);
1056 
1057 	if (otyp != OTYP_CHR)
1058 		return (EINVAL);
1059 
1060 	dip = sata_devt_to_devinfo(*devp);
1061 	if (dip == NULL)
1062 		return (ENXIO);
1063 
1064 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1065 		return (ENXIO);
1066 
1067 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1068 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1069 		return (ENXIO);
1070 
1071 	mutex_enter(&sata_mutex);
1072 	if (flags & FEXCL) {
1073 		if (sata_hba_inst->satahba_open_flag != 0) {
1074 			rv = EBUSY;
1075 		} else {
1076 			sata_hba_inst->satahba_open_flag =
1077 			    SATA_DEVCTL_EXOPENED;
1078 		}
1079 	} else {
1080 		if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) {
1081 			rv = EBUSY;
1082 		} else {
1083 			sata_hba_inst->satahba_open_flag =
1084 			    SATA_DEVCTL_SOPENED;
1085 		}
1086 	}
1087 	mutex_exit(&sata_mutex);
1088 
1089 	return (rv);
1090 }
1091 
1092 
1093 /*
1094  * Close devctl node.
1095  * Returns:
1096  * 0 if node was closed successfully, error code otherwise.
1097  *
1098  */
1099 
1100 static int
1101 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
1102 {
1103 #ifndef __lock_lint
1104 	_NOTE(ARGUNUSED(credp))
1105 	_NOTE(ARGUNUSED(flag))
1106 #endif
1107 	dev_info_t *dip;
1108 	scsi_hba_tran_t *scsi_hba_tran;
1109 	sata_hba_inst_t	*sata_hba_inst;
1110 
1111 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL);
1112 
1113 	if (otyp != OTYP_CHR)
1114 		return (EINVAL);
1115 
1116 	dip = sata_devt_to_devinfo(dev);
1117 	if (dip == NULL)
1118 		return (ENXIO);
1119 
1120 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1121 		return (ENXIO);
1122 
1123 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1124 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1125 		return (ENXIO);
1126 
1127 	mutex_enter(&sata_mutex);
1128 	sata_hba_inst->satahba_open_flag = 0;
1129 	mutex_exit(&sata_mutex);
1130 	return (0);
1131 }
1132 
1133 
1134 
1135 /*
1136  * Standard IOCTL commands for SATA hotplugging.
1137  * Implemented DEVCTL_AP commands:
1138  * DEVCTL_AP_CONNECT
1139  * DEVCTL_AP_DISCONNECT
1140  * DEVCTL_AP_CONFIGURE
1141  * DEVCTL_UNCONFIGURE
1142  * DEVCTL_AP_CONTROL
1143  *
1144  * Commands passed to default ndi ioctl handler:
1145  * DEVCTL_DEVICE_GETSTATE
1146  * DEVCTL_DEVICE_ONLINE
1147  * DEVCTL_DEVICE_OFFLINE
1148  * DEVCTL_DEVICE_REMOVE
1149  * DEVCTL_DEVICE_INSERT
1150  * DEVCTL_BUS_GETSTATE
1151  *
1152  * All other cmds are passed to HBA if it provide ioctl handler, or failed
1153  * if not.
1154  *
1155  * Returns:
1156  * 0 if successful,
1157  * error code if operation failed.
1158  *
1159  * NOTE: Port Multiplier is not supported.
1160  *
1161  */
1162 
1163 static int
1164 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1165     int *rvalp)
1166 {
1167 #ifndef __lock_lint
1168 	_NOTE(ARGUNUSED(credp))
1169 	_NOTE(ARGUNUSED(rvalp))
1170 #endif
1171 	int rv = 0;
1172 	int32_t	comp_port = -1;
1173 	dev_info_t *dip;
1174 	devctl_ap_state_t ap_state;
1175 	struct devctl_iocdata *dcp = NULL;
1176 	scsi_hba_tran_t *scsi_hba_tran;
1177 	sata_hba_inst_t *sata_hba_inst;
1178 	sata_device_t sata_device;
1179 	sata_cport_info_t *cportinfo;
1180 	int cport, pmport, qual;
1181 	int rval = SATA_SUCCESS;
1182 
1183 	dip = sata_devt_to_devinfo(dev);
1184 	if (dip == NULL)
1185 		return (ENXIO);
1186 
1187 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1188 		return (ENXIO);
1189 
1190 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1191 	if (sata_hba_inst == NULL)
1192 		return (ENXIO);
1193 
1194 	if (sata_hba_inst->satahba_tran == NULL)
1195 		return (ENXIO);
1196 
1197 	switch (cmd) {
1198 
1199 	case DEVCTL_DEVICE_GETSTATE:
1200 	case DEVCTL_DEVICE_ONLINE:
1201 	case DEVCTL_DEVICE_OFFLINE:
1202 	case DEVCTL_DEVICE_REMOVE:
1203 	case DEVCTL_BUS_GETSTATE:
1204 		/*
1205 		 * There may be more cases that we want to pass to default
1206 		 * handler rather than fail them.
1207 		 */
1208 		return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
1209 	}
1210 
1211 	/* read devctl ioctl data */
1212 	if (cmd != DEVCTL_AP_CONTROL) {
1213 		if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1214 			return (EFAULT);
1215 
1216 		if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) ==
1217 		    -1) {
1218 			if (dcp)
1219 				ndi_dc_freehdl(dcp);
1220 			return (EINVAL);
1221 		}
1222 
1223 		cport = SCSI_TO_SATA_CPORT(comp_port);
1224 		pmport = SCSI_TO_SATA_PMPORT(comp_port);
1225 		/* Only cport is considered now, i.e. SATA_ADDR_CPORT */
1226 		qual = SATA_ADDR_CPORT;
1227 		if (sata_validate_sata_address(sata_hba_inst, cport, pmport,
1228 		    qual) != 0) {
1229 			ndi_dc_freehdl(dcp);
1230 			return (EINVAL);
1231 		}
1232 
1233 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1234 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1235 		    cport_mutex);
1236 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1237 			/*
1238 			 * Cannot process ioctl request now. Come back later.
1239 			 */
1240 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1241 			    cport_mutex);
1242 			ndi_dc_freehdl(dcp);
1243 			return (EBUSY);
1244 		}
1245 		/* Block event processing for this port */
1246 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1247 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1248 
1249 		sata_device.satadev_addr.cport = cport;
1250 		sata_device.satadev_addr.pmport = pmport;
1251 		sata_device.satadev_addr.qual = qual;
1252 		sata_device.satadev_rev = SATA_DEVICE_REV;
1253 	}
1254 
1255 	switch (cmd) {
1256 
1257 	case DEVCTL_AP_DISCONNECT:
1258 
1259 		/*
1260 		 * Normally, cfgadm sata plugin will try to offline
1261 		 * (unconfigure) device before this request. Nevertheless,
1262 		 * if a device is still configured, we need to
1263 		 * attempt to offline and unconfigure device first, and we will
1264 		 * deactivate the port regardless of the unconfigure
1265 		 * operation results.
1266 		 *
1267 		 */
1268 		rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device);
1269 
1270 		break;
1271 
1272 	case DEVCTL_AP_UNCONFIGURE:
1273 
1274 		/*
1275 		 * The unconfigure operation uses generic nexus operation to
1276 		 * offline a device. It leaves a target device node attached.
1277 		 * and obviously sata_drive_info attached as well, because
1278 		 * from the hardware point of view nothing has changed.
1279 		 */
1280 		rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device);
1281 		break;
1282 
1283 	case DEVCTL_AP_CONNECT:
1284 	{
1285 		/*
1286 		 * The sata cfgadm pluging will invoke this operation only if
1287 		 * port was found in the disconnect state (failed state
1288 		 * is also treated as the disconnected state).
1289 		 * If port activation is successful and a device is found
1290 		 * attached to the port, the initialization sequence is
1291 		 * executed to probe the port and attach
1292 		 * a device structure to a port structure. The device is not
1293 		 * set in configured state (system-wise) by this operation.
1294 		 */
1295 
1296 		rv = sata_ioctl_connect(sata_hba_inst, &sata_device);
1297 
1298 		break;
1299 	}
1300 
1301 	case DEVCTL_AP_CONFIGURE:
1302 	{
1303 		/*
1304 		 * A port may be in an active or shutdown state.
1305 		 * If port is in a failed state, operation is aborted.
1306 		 * If a port is in a shutdown state, sata_tran_port_activate()
1307 		 * is invoked prior to any other operation.
1308 		 *
1309 		 * Onlining the device involves creating a new target node.
1310 		 * If there is an old target node present (belonging to
1311 		 * previously removed device), the operation is aborted - the
1312 		 * old node has to be released and removed before configure
1313 		 * operation is attempted.
1314 		 */
1315 
1316 		rv = sata_ioctl_configure(sata_hba_inst, &sata_device);
1317 
1318 		break;
1319 	}
1320 
1321 	case DEVCTL_AP_GETSTATE:
1322 
1323 		sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state);
1324 
1325 		ap_state.ap_last_change = (time_t)-1;
1326 		ap_state.ap_error_code = 0;
1327 		ap_state.ap_in_transition = 0;
1328 
1329 		/* Copy the return AP-state information to the user space */
1330 		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
1331 			rv = EFAULT;
1332 		}
1333 		break;
1334 
1335 	case DEVCTL_AP_CONTROL:
1336 	{
1337 		/*
1338 		 * Generic devctl for hardware specific functionality
1339 		 */
1340 		sata_ioctl_data_t	ioc;
1341 
1342 		ASSERT(dcp == NULL);
1343 
1344 		/* Copy in user ioctl data first */
1345 #ifdef _MULTI_DATAMODEL
1346 		if (ddi_model_convert_from(mode & FMODELS) ==
1347 		    DDI_MODEL_ILP32) {
1348 
1349 			sata_ioctl_data_32_t	ioc32;
1350 
1351 			if (ddi_copyin((void *)arg, (void *)&ioc32,
1352 			    sizeof (ioc32), mode) != 0) {
1353 				rv = EFAULT;
1354 				break;
1355 			}
1356 			ioc.cmd 	= (uint_t)ioc32.cmd;
1357 			ioc.port	= (uint_t)ioc32.port;
1358 			ioc.get_size	= (uint_t)ioc32.get_size;
1359 			ioc.buf		= (caddr_t)(uintptr_t)ioc32.buf;
1360 			ioc.bufsiz	= (uint_t)ioc32.bufsiz;
1361 			ioc.misc_arg	= (uint_t)ioc32.misc_arg;
1362 		} else
1363 #endif /* _MULTI_DATAMODEL */
1364 		if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
1365 		    mode) != 0) {
1366 			return (EFAULT);
1367 		}
1368 
1369 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
1370 		    "sata_hba_ioctl: DEVCTL_AP_CONTROL "
1371 		    "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port);
1372 
1373 		/*
1374 		 * To avoid BE/LE and 32/64 issues, a get_size always returns
1375 		 * a 32-bit number.
1376 		 */
1377 		if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
1378 			return (EINVAL);
1379 		}
1380 		/* validate address */
1381 		cport = SCSI_TO_SATA_CPORT(ioc.port);
1382 		pmport = SCSI_TO_SATA_PMPORT(ioc.port);
1383 		qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port);
1384 
1385 		/* Override address qualifier - handle cport only for now */
1386 		qual = SATA_ADDR_CPORT;
1387 
1388 		if (sata_validate_sata_address(sata_hba_inst, cport,
1389 		    pmport, qual) != 0)
1390 			return (EINVAL);
1391 
1392 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1393 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1394 		    cport_mutex);
1395 		/* Is the port locked by event processing daemon ? */
1396 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1397 			/*
1398 			 * Cannot process ioctl request now. Come back later
1399 			 */
1400 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1401 			    cport_mutex);
1402 			return (EBUSY);
1403 		}
1404 		/* Block event processing for this port */
1405 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1406 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1407 
1408 
1409 		sata_device.satadev_addr.cport = cport;
1410 		sata_device.satadev_addr.pmport = pmport;
1411 		sata_device.satadev_addr.qual = qual;
1412 		sata_device.satadev_rev = SATA_DEVICE_REV;
1413 
1414 		switch (ioc.cmd) {
1415 
1416 		case SATA_CFGA_RESET_PORT:
1417 			/*
1418 			 * There is no protection for configured device.
1419 			 */
1420 			rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device);
1421 			break;
1422 
1423 		case SATA_CFGA_RESET_DEVICE:
1424 			/*
1425 			 * There is no protection for configured device.
1426 			 */
1427 			rv = sata_ioctl_reset_device(sata_hba_inst,
1428 			    &sata_device);
1429 			break;
1430 
1431 		case SATA_CFGA_RESET_ALL:
1432 			/*
1433 			 * There is no protection for configured devices.
1434 			 */
1435 			rv = sata_ioctl_reset_all(sata_hba_inst);
1436 			/*
1437 			 * We return here, because common return is for
1438 			 * a single port operation - we have already unlocked
1439 			 * all ports and no dc handle was allocated.
1440 			 */
1441 			return (rv);
1442 
1443 		case SATA_CFGA_PORT_DEACTIVATE:
1444 			/*
1445 			 * Arbitrarily unconfigure attached device, if any.
1446 			 * Even if the unconfigure fails, proceed with the
1447 			 * port deactivation.
1448 			 */
1449 			rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device);
1450 
1451 			break;
1452 
1453 		case SATA_CFGA_PORT_ACTIVATE:
1454 
1455 			rv = sata_ioctl_activate(sata_hba_inst, &sata_device);
1456 			break;
1457 
1458 		case SATA_CFGA_PORT_SELF_TEST:
1459 
1460 			rv = sata_ioctl_port_self_test(sata_hba_inst,
1461 			    &sata_device);
1462 			break;
1463 
1464 		case SATA_CFGA_GET_DEVICE_PATH:
1465 			if (qual == SATA_ADDR_CPORT)
1466 				sata_device.satadev_addr.qual =
1467 				    SATA_ADDR_DCPORT;
1468 			else
1469 				sata_device.satadev_addr.qual =
1470 				    SATA_ADDR_DPMPORT;
1471 			rv = sata_ioctl_get_device_path(sata_hba_inst,
1472 			    &sata_device, &ioc, mode);
1473 			break;
1474 
1475 		case SATA_CFGA_GET_AP_TYPE:
1476 
1477 			rv = sata_ioctl_get_ap_type(sata_hba_inst,
1478 			    &sata_device, &ioc, mode);
1479 			break;
1480 
1481 		case SATA_CFGA_GET_MODEL_INFO:
1482 
1483 			rv = sata_ioctl_get_model_info(sata_hba_inst,
1484 			    &sata_device, &ioc, mode);
1485 			break;
1486 
1487 		case SATA_CFGA_GET_REVFIRMWARE_INFO:
1488 
1489 			rv = sata_ioctl_get_revfirmware_info(sata_hba_inst,
1490 			    &sata_device, &ioc, mode);
1491 			break;
1492 
1493 		case SATA_CFGA_GET_SERIALNUMBER_INFO:
1494 
1495 			rv = sata_ioctl_get_serialnumber_info(sata_hba_inst,
1496 			    &sata_device, &ioc, mode);
1497 			break;
1498 
1499 		default:
1500 			rv = EINVAL;
1501 			break;
1502 
1503 		} /* End of DEVCTL_AP_CONTROL cmd switch */
1504 
1505 		break;
1506 	}
1507 
1508 	default:
1509 	{
1510 		/*
1511 		 * If we got here, we got an IOCTL that SATA HBA Framework
1512 		 * does not recognize. Pass ioctl to HBA driver, in case
1513 		 * it could process it.
1514 		 */
1515 		sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran;
1516 		dev_info_t	*mydip = SATA_DIP(sata_hba_inst);
1517 
1518 		SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1519 		    "IOCTL 0x%2x not supported in SATA framework, "
1520 		    "passthrough to HBA", cmd);
1521 
1522 		if (sata_tran->sata_tran_ioctl == NULL) {
1523 			rv = EINVAL;
1524 			break;
1525 		}
1526 		rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg);
1527 		if (rval != 0) {
1528 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1529 			    "IOCTL 0x%2x failed in HBA", cmd);
1530 			rv = rval;
1531 		}
1532 		break;
1533 	}
1534 
1535 	} /* End of main IOCTL switch */
1536 
1537 	if (dcp) {
1538 		ndi_dc_freehdl(dcp);
1539 	}
1540 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1541 	cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
1542 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1543 
1544 	return (rv);
1545 }
1546 
1547 
1548 /*
1549  * Create error retrieval sata packet
1550  *
1551  * A sata packet is allocated and set-up to contain specified error retrieval
1552  * command and appropriate dma-able data buffer.
1553  * No association with any scsi packet is made and no callback routine is
1554  * specified.
1555  *
1556  * Returns a pointer to sata packet upon successfull packet creation.
1557  * Returns NULL, if packet cannot be created.
1558  */
1559 sata_pkt_t *
1560 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device,
1561     int pkt_type)
1562 {
1563 	sata_hba_inst_t	*sata_hba_inst;
1564 	sata_pkt_txlate_t *spx;
1565 	sata_pkt_t *spkt;
1566 	sata_drive_info_t *sdinfo;
1567 
1568 	mutex_enter(&sata_mutex);
1569 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1570 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1571 		if (SATA_DIP(sata_hba_inst) == dip)
1572 			break;
1573 	}
1574 	mutex_exit(&sata_mutex);
1575 	ASSERT(sata_hba_inst != NULL);
1576 
1577 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
1578 	if (sdinfo == NULL) {
1579 		sata_log(sata_hba_inst, CE_WARN,
1580 		    "sata: error recovery request for non-attached device at "
1581 		    "cport %d", sata_device->satadev_addr.cport);
1582 		return (NULL);
1583 	}
1584 
1585 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1586 	spx->txlt_sata_hba_inst = sata_hba_inst;
1587 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
1588 	spkt = sata_pkt_alloc(spx, NULL);
1589 	if (spkt == NULL) {
1590 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1591 		return (NULL);
1592 	}
1593 	/* address is needed now */
1594 	spkt->satapkt_device.satadev_addr = sata_device->satadev_addr;
1595 
1596 	switch (pkt_type) {
1597 	case SATA_ERR_RETR_PKT_TYPE_NCQ:
1598 		if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS)
1599 			return (spkt);
1600 		break;
1601 
1602 	case SATA_ERR_RETR_PKT_TYPE_ATAPI:
1603 		if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS)
1604 			return (spkt);
1605 		break;
1606 
1607 	default:
1608 		break;
1609 	}
1610 
1611 	sata_pkt_free(spx);
1612 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1613 	return (NULL);
1614 
1615 }
1616 
1617 
1618 /*
1619  * Free error retrieval sata packet
1620  *
1621  * Free sata packet and any associated resources allocated previously by
1622  * sata_get_error_retrieval_pkt().
1623  *
1624  * Void return.
1625  */
1626 void
1627 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt)
1628 {
1629 	sata_pkt_txlate_t *spx =
1630 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1631 
1632 	ASSERT(sata_pkt != NULL);
1633 
1634 	sata_free_local_buffer(spx);
1635 	sata_pkt_free(spx);
1636 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1637 
1638 }
1639 
1640 
1641 /* ****************** SCSA required entry points *********************** */
1642 
1643 /*
1644  * Implementation of scsi tran_tgt_init.
1645  * sata_scsi_tgt_init() initializes scsi_device structure
1646  *
1647  * If successful, DDI_SUCCESS is returned.
1648  * DDI_FAILURE is returned if addressed device does not exist
1649  */
1650 
1651 static int
1652 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1653     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1654 {
1655 #ifndef __lock_lint
1656 	_NOTE(ARGUNUSED(hba_dip))
1657 	_NOTE(ARGUNUSED(tgt_dip))
1658 #endif
1659 	sata_device_t		sata_device;
1660 	sata_drive_info_t	*sdinfo;
1661 	struct sata_id		*sid;
1662 	sata_hba_inst_t		*sata_hba_inst;
1663 	char			model[SATA_ID_MODEL_LEN + 1];
1664 	char			fw[SATA_ID_FW_LEN + 1];
1665 	char			*vid, *pid;
1666 	int			i;
1667 
1668 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
1669 
1670 	/* Validate scsi device address */
1671 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
1672 	    &sata_device) != 0)
1673 		return (DDI_FAILURE);
1674 
1675 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1676 	    sata_device.satadev_addr.cport)));
1677 
1678 	/* sata_device now contains a valid sata address */
1679 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
1680 	if (sdinfo == NULL) {
1681 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1682 		    sata_device.satadev_addr.cport)));
1683 		return (DDI_FAILURE);
1684 	}
1685 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1686 	    sata_device.satadev_addr.cport)));
1687 
1688 	/*
1689 	 * Check if we need to create a legacy devid (i.e cmdk style) for
1690 	 * the target disks.
1691 	 *
1692 	 * HBA devinfo node will have the property "use-cmdk-devid-format"
1693 	 * if we need to create cmdk-style devid for all the disk devices
1694 	 * attached to this controller. This property may have been set
1695 	 * from HBA driver's .conf file or by the HBA driver in its
1696 	 * attach(9F) function.
1697 	 */
1698 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
1699 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
1700 	    "use-cmdk-devid-format", 0) == 1)) {
1701 		/* register a legacy devid for this target node */
1702 		sata_target_devid_register(tgt_dip, sdinfo);
1703 	}
1704 
1705 
1706 	/*
1707 	 * 'Identify Device Data' does not always fit in standard SCSI
1708 	 * INQUIRY data, so establish INQUIRY_* properties with full-form
1709 	 * of information.
1710 	 */
1711 	sid = &sdinfo->satadrv_id;
1712 #ifdef	_LITTLE_ENDIAN
1713 	swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
1714 	swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
1715 #else	/* _LITTLE_ENDIAN */
1716 	bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN);
1717 	bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN);
1718 #endif	/* _LITTLE_ENDIAN */
1719 	model[SATA_ID_MODEL_LEN] = 0;
1720 	fw[SATA_ID_FW_LEN] = 0;
1721 
1722 	/* split model into into vid/pid */
1723 	for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++)
1724 		if ((*pid == ' ') || (*pid == '\t'))
1725 			break;
1726 	if (i < SATA_ID_MODEL_LEN) {
1727 		vid = model;
1728 		*pid++ = 0;		/* terminate vid, establish pid */
1729 	} else {
1730 		vid = NULL;		/* vid will stay "ATA     " */
1731 		pid = model;		/* model is all pid */
1732 	}
1733 
1734 	if (vid)
1735 		(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_VENDOR_ID,
1736 		    vid, strlen(vid));
1737 	if (pid)
1738 		(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID,
1739 		    pid, strlen(pid));
1740 	(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_REVISION_ID,
1741 	    fw, strlen(fw));
1742 
1743 	return (DDI_SUCCESS);
1744 }
1745 
1746 /*
1747  * Implementation of scsi tran_tgt_probe.
1748  * Probe target, by calling default scsi routine scsi_hba_probe()
1749  */
1750 static int
1751 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void))
1752 {
1753 	sata_hba_inst_t *sata_hba_inst =
1754 	    (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private);
1755 	int rval;
1756 
1757 	rval = scsi_hba_probe(sd, callback);
1758 
1759 	if (rval == SCSIPROBE_EXISTS) {
1760 		/*
1761 		 * Set property "pm-capable" on the target device node, so that
1762 		 * the target driver will not try to fetch scsi cycle counters
1763 		 * before enabling device power-management.
1764 		 */
1765 		if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
1766 		    "pm-capable", 1)) != DDI_PROP_SUCCESS) {
1767 			sata_log(sata_hba_inst, CE_WARN,
1768 			    "SATA device at port %d: "
1769 			    "will not be power-managed ",
1770 			    SCSI_TO_SATA_CPORT(sd->sd_address.a_target));
1771 			SATA_LOG_D((sata_hba_inst, CE_WARN,
1772 			    "failure updating pm-capable property"));
1773 		}
1774 	}
1775 	return (rval);
1776 }
1777 
1778 /*
1779  * Implementation of scsi tran_tgt_free.
1780  * Release all resources allocated for scsi_device
1781  */
1782 static void
1783 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1784     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1785 {
1786 #ifndef __lock_lint
1787 	_NOTE(ARGUNUSED(hba_dip))
1788 #endif
1789 	sata_device_t		sata_device;
1790 	sata_drive_info_t	*sdinfo;
1791 	sata_hba_inst_t		*sata_hba_inst;
1792 	ddi_devid_t		devid;
1793 
1794 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
1795 
1796 	/* Validate scsi device address */
1797 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
1798 	    &sata_device) != 0)
1799 		return;
1800 
1801 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1802 	    sata_device.satadev_addr.cport)));
1803 
1804 	/* sata_device now should contain a valid sata address */
1805 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
1806 	if (sdinfo == NULL) {
1807 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1808 		    sata_device.satadev_addr.cport)));
1809 		return;
1810 	}
1811 	/*
1812 	 * We did not allocate any resources in sata_scsi_tgt_init()
1813 	 * other than few properties.
1814 	 * Free them.
1815 	 */
1816 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1817 	    sata_device.satadev_addr.cport)));
1818 	(void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable");
1819 
1820 	/*
1821 	 * If devid was previously created but not freed up from
1822 	 * sd(7D) driver (i.e during detach(9F)) then do it here.
1823 	 */
1824 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
1825 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
1826 	    "use-cmdk-devid-format", 0) == 1) &&
1827 	    (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) {
1828 		ddi_devid_unregister(tgt_dip);
1829 		ddi_devid_free(devid);
1830 	}
1831 }
1832 
1833 /*
1834  * Implementation of scsi tran_init_pkt
1835  * Upon successful return, scsi pkt buffer has DMA resources allocated.
1836  *
1837  * It seems that we should always allocate pkt, even if the address is
1838  * for non-existing device - just use some default for dma_attr.
1839  * The reason is that there is no way to communicate this to a caller here.
1840  * Subsequent call to sata_scsi_start may fail appropriately.
1841  * Simply returning NULL does not seem to discourage a target driver...
1842  *
1843  * Returns a pointer to initialized scsi_pkt, or NULL otherwise.
1844  */
1845 static struct scsi_pkt *
1846 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
1847     struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
1848     int (*callback)(caddr_t), caddr_t arg)
1849 {
1850 	sata_hba_inst_t *sata_hba_inst =
1851 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
1852 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
1853 	sata_device_t sata_device;
1854 	sata_drive_info_t *sdinfo;
1855 	sata_pkt_txlate_t *spx;
1856 	ddi_dma_attr_t cur_dma_attr;
1857 	int rval;
1858 	boolean_t new_pkt = TRUE;
1859 
1860 	ASSERT(ap->a_hba_tran->tran_hba_dip == dip);
1861 
1862 	/*
1863 	 * We need to translate the address, even if it could be
1864 	 * a bogus one, for a non-existing device
1865 	 */
1866 	sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
1867 	sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target);
1868 	sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
1869 	sata_device.satadev_rev = SATA_DEVICE_REV;
1870 
1871 	if (pkt == NULL) {
1872 		/*
1873 		 * Have to allocate a brand new scsi packet.
1874 		 * We need to operate with auto request sense enabled.
1875 		 */
1876 		pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen,
1877 		    MAX(statuslen, sizeof (struct scsi_arq_status)),
1878 		    tgtlen, sizeof (sata_pkt_txlate_t), callback, arg);
1879 
1880 		if (pkt == NULL)
1881 			return (NULL);
1882 
1883 		/* Fill scsi packet structure */
1884 		pkt->pkt_comp		= (void (*)())NULL;
1885 		pkt->pkt_time		= 0;
1886 		pkt->pkt_resid		= 0;
1887 		pkt->pkt_statistics	= 0;
1888 		pkt->pkt_reason		= 0;
1889 
1890 		/*
1891 		 * pkt_hba_private will point to sata pkt txlate structure
1892 		 */
1893 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
1894 		bzero(spx, sizeof (sata_pkt_txlate_t));
1895 
1896 		spx->txlt_scsi_pkt = pkt;
1897 		spx->txlt_sata_hba_inst = sata_hba_inst;
1898 
1899 		/* Allocate sata_pkt */
1900 		spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback);
1901 		if (spx->txlt_sata_pkt == NULL) {
1902 			/* Could not allocate sata pkt */
1903 			scsi_hba_pkt_free(ap, pkt);
1904 			return (NULL);
1905 		}
1906 		/* Set sata address */
1907 		spx->txlt_sata_pkt->satapkt_device.satadev_addr =
1908 		    sata_device.satadev_addr;
1909 		spx->txlt_sata_pkt->satapkt_device.satadev_rev =
1910 		    sata_device.satadev_rev;
1911 
1912 		if ((bp == NULL) || (bp->b_bcount == 0))
1913 			return (pkt);
1914 
1915 		spx->txlt_total_residue = bp->b_bcount;
1916 	} else {
1917 		new_pkt = FALSE;
1918 		/*
1919 		 * Packet was preallocated/initialized by previous call
1920 		 */
1921 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
1922 
1923 		if ((bp == NULL) || (bp->b_bcount == 0)) {
1924 			return (pkt);
1925 		}
1926 		ASSERT(spx->txlt_buf_dma_handle != NULL);
1927 
1928 		/* Pkt is available already: spx->txlt_scsi_pkt == pkt; */
1929 	}
1930 
1931 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
1932 
1933 	/*
1934 	 * We use an adjusted version of the dma_attr, to account
1935 	 * for device addressing limitations.
1936 	 * sata_adjust_dma_attr() will handle sdinfo == NULL which may
1937 	 * happen when a device is not yet configured.
1938 	 */
1939 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1940 	    sata_device.satadev_addr.cport)));
1941 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
1942 	    &spx->txlt_sata_pkt->satapkt_device);
1943 	/* NULL sdinfo may be passsed to sata_adjust_dma_attr() */
1944 	sata_adjust_dma_attr(sdinfo,
1945 	    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
1946 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1947 	    sata_device.satadev_addr.cport)));
1948 	/*
1949 	 * Allocate necessary DMA resources for the packet's data buffer
1950 	 * NOTE:
1951 	 * In case of read/write commands, DMA resource allocation here is
1952 	 * based on the premise that the transfer length specified in
1953 	 * the read/write scsi cdb will match exactly DMA resources -
1954 	 * returning correct packet residue is crucial.
1955 	 */
1956 	if ((rval = sata_dma_buf_setup(spx, flags, callback, arg,
1957 	    &cur_dma_attr)) != DDI_SUCCESS) {
1958 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
1959 		sata_pkt_free(spx);
1960 		/*
1961 		 * If a DMA allocation request fails with
1962 		 * DDI_DMA_NOMAPPING, indicate the error by calling
1963 		 * bioerror(9F) with bp and an error code of EFAULT.
1964 		 * If a DMA allocation request fails with
1965 		 * DDI_DMA_TOOBIG, indicate the error by calling
1966 		 * bioerror(9F) with bp and an error code of EINVAL.
1967 		 */
1968 		switch (rval) {
1969 		case DDI_DMA_NORESOURCES:
1970 			bioerror(bp, 0);
1971 			break;
1972 		case DDI_DMA_NOMAPPING:
1973 		case DDI_DMA_BADATTR:
1974 			bioerror(bp, EFAULT);
1975 			break;
1976 		case DDI_DMA_TOOBIG:
1977 		default:
1978 			bioerror(bp, EINVAL);
1979 			break;
1980 		}
1981 		if (new_pkt == TRUE)
1982 			scsi_hba_pkt_free(ap, pkt);
1983 		return (NULL);
1984 	}
1985 	/* Set number of bytes that are not yet accounted for */
1986 	pkt->pkt_resid = spx->txlt_total_residue;
1987 	ASSERT(pkt->pkt_resid >= 0);
1988 
1989 	return (pkt);
1990 }
1991 
1992 /*
1993  * Implementation of scsi tran_start.
1994  * Translate scsi cmd into sata operation and return status.
1995  * ATAPI CDBs are passed to ATAPI devices - the device determines what commands
1996  * are supported.
1997  * For SATA hard disks, supported scsi commands:
1998  * SCMD_INQUIRY
1999  * SCMD_TEST_UNIT_READY
2000  * SCMD_START_STOP
2001  * SCMD_READ_CAPACITY
2002  * SCMD_REQUEST_SENSE
2003  * SCMD_LOG_SENSE_G1
2004  * SCMD_LOG_SELECT_G1
2005  * SCMD_MODE_SENSE	(specific pages)
2006  * SCMD_MODE_SENSE_G1	(specific pages)
2007  * SCMD_MODE_SELECT	(specific pages)
2008  * SCMD_MODE_SELECT_G1	(specific pages)
2009  * SCMD_SYNCHRONIZE_CACHE
2010  * SCMD_SYNCHRONIZE_CACHE_G1
2011  * SCMD_READ
2012  * SCMD_READ_G1
2013  * SCMD_READ_G4
2014  * SCMD_READ_G5
2015  * SCMD_WRITE
2016  * SCMD_WRITE_BUFFER
2017  * SCMD_WRITE_G1
2018  * SCMD_WRITE_G4
2019  * SCMD_WRITE_G5
2020  * SCMD_SEEK		(noop)
2021  * SCMD_SDIAG
2022  *
2023  * All other commands are rejected as unsupported.
2024  *
2025  * Returns:
2026  * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver
2027  * for execution. TRAN_ACCEPT may be returned also if device was removed but
2028  * a callback could be scheduled.
2029  * TRAN_BADPKT if cmd was directed to invalid address.
2030  * TRAN_FATAL_ERROR is command was rejected due to hardware error, including
2031  * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device
2032  * was removed and there was no callback specified in scsi pkt.
2033  * TRAN_BUSY if command could not be executed becasue HBA driver or SATA
2034  * framework was busy performing some other operation(s).
2035  *
2036  */
2037 static int
2038 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
2039 {
2040 	sata_hba_inst_t *sata_hba_inst =
2041 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2042 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2043 	sata_drive_info_t *sdinfo;
2044 	struct buf *bp;
2045 	int cport;
2046 	int rval;
2047 
2048 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2049 	    "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]);
2050 
2051 	ASSERT(spx != NULL &&
2052 	    spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL);
2053 
2054 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
2055 
2056 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2057 	sdinfo = sata_get_device_info(sata_hba_inst,
2058 	    &spx->txlt_sata_pkt->satapkt_device);
2059 	if (sdinfo == NULL ||
2060 	    SATA_CPORT_INFO(sata_hba_inst, cport)->cport_tgtnode_clean ==
2061 	    B_FALSE ||
2062 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
2063 
2064 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2065 		pkt->pkt_reason = CMD_DEV_GONE;
2066 		/*
2067 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2068 		 * only in callback function (for normal requests) and
2069 		 * in the dump code path.
2070 		 * So, if the callback is available, we need to do
2071 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2072 		 */
2073 		if (pkt->pkt_comp != NULL) {
2074 			/* scsi callback required */
2075 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2076 			    (task_func_t *)pkt->pkt_comp,
2077 			    (void *)pkt, TQ_SLEEP) == NULL)
2078 				/* Scheduling the callback failed */
2079 				return (TRAN_BUSY);
2080 			return (TRAN_ACCEPT);
2081 		}
2082 		/* No callback available */
2083 		return (TRAN_FATAL_ERROR);
2084 	}
2085 
2086 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
2087 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2088 		rval = sata_txlt_atapi(spx);
2089 		SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2090 		    "sata_scsi_start atapi: rval %d\n", rval);
2091 		return (rval);
2092 	}
2093 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2094 
2095 	/* ATA Disk commands processing starts here */
2096 
2097 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2098 
2099 	switch (pkt->pkt_cdbp[0]) {
2100 
2101 	case SCMD_INQUIRY:
2102 		/* Mapped to identify device */
2103 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2104 			bp_mapin(bp);
2105 		rval = sata_txlt_inquiry(spx);
2106 		break;
2107 
2108 	case SCMD_TEST_UNIT_READY:
2109 		/*
2110 		 * SAT "SATA to ATA Translation" doc specifies translation
2111 		 * to ATA CHECK POWER MODE.
2112 		 */
2113 		rval = sata_txlt_test_unit_ready(spx);
2114 		break;
2115 
2116 	case SCMD_START_STOP:
2117 		/* Mapping depends on the command */
2118 		rval = sata_txlt_start_stop_unit(spx);
2119 		break;
2120 
2121 	case SCMD_READ_CAPACITY:
2122 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2123 			bp_mapin(bp);
2124 		rval = sata_txlt_read_capacity(spx);
2125 		break;
2126 
2127 	case SCMD_REQUEST_SENSE:
2128 		/*
2129 		 * Always No Sense, since we force ARQ
2130 		 */
2131 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2132 			bp_mapin(bp);
2133 		rval = sata_txlt_request_sense(spx);
2134 		break;
2135 
2136 	case SCMD_LOG_SENSE_G1:
2137 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2138 			bp_mapin(bp);
2139 		rval = sata_txlt_log_sense(spx);
2140 		break;
2141 
2142 	case SCMD_LOG_SELECT_G1:
2143 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2144 			bp_mapin(bp);
2145 		rval = sata_txlt_log_select(spx);
2146 		break;
2147 
2148 	case SCMD_MODE_SENSE:
2149 	case SCMD_MODE_SENSE_G1:
2150 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2151 			bp_mapin(bp);
2152 		rval = sata_txlt_mode_sense(spx);
2153 		break;
2154 
2155 
2156 	case SCMD_MODE_SELECT:
2157 	case SCMD_MODE_SELECT_G1:
2158 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2159 			bp_mapin(bp);
2160 		rval = sata_txlt_mode_select(spx);
2161 		break;
2162 
2163 	case SCMD_SYNCHRONIZE_CACHE:
2164 	case SCMD_SYNCHRONIZE_CACHE_G1:
2165 		rval = sata_txlt_synchronize_cache(spx);
2166 		break;
2167 
2168 	case SCMD_READ:
2169 	case SCMD_READ_G1:
2170 	case SCMD_READ_G4:
2171 	case SCMD_READ_G5:
2172 		rval = sata_txlt_read(spx);
2173 		break;
2174 	case SCMD_WRITE_BUFFER:
2175 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2176 			bp_mapin(bp);
2177 		rval = sata_txlt_write_buffer(spx);
2178 		break;
2179 
2180 	case SCMD_WRITE:
2181 	case SCMD_WRITE_G1:
2182 	case SCMD_WRITE_G4:
2183 	case SCMD_WRITE_G5:
2184 		rval = sata_txlt_write(spx);
2185 		break;
2186 
2187 	case SCMD_SEEK:
2188 		rval = sata_txlt_nodata_cmd_immediate(spx);
2189 		break;
2190 
2191 		/* Other cases will be filed later */
2192 		/* postponed until phase 2 of the development */
2193 	default:
2194 		rval = sata_txlt_invalid_command(spx);
2195 		break;
2196 	}
2197 
2198 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2199 	    "sata_scsi_start: rval %d\n", rval);
2200 
2201 	return (rval);
2202 }
2203 
2204 /*
2205  * Implementation of scsi tran_abort.
2206  * Abort specific pkt or all packets.
2207  *
2208  * Returns 1 if one or more packets were aborted, returns 0 otherwise
2209  *
2210  * May be called from an interrupt level.
2211  */
2212 static int
2213 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt)
2214 {
2215 	sata_hba_inst_t *sata_hba_inst =
2216 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2217 	sata_device_t	sata_device;
2218 	sata_pkt_t	*sata_pkt;
2219 
2220 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2221 	    "sata_scsi_abort: %s at target: 0x%x\n",
2222 	    scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target);
2223 
2224 	/* Validate address */
2225 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0)
2226 		/* Invalid address */
2227 		return (0);
2228 
2229 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2230 	    sata_device.satadev_addr.cport)));
2231 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2232 		/* invalid address */
2233 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2234 		    sata_device.satadev_addr.cport)));
2235 		return (0);
2236 	}
2237 	if (scsi_pkt == NULL) {
2238 		/*
2239 		 * Abort all packets.
2240 		 * Although we do not have specific packet, we still need
2241 		 * dummy packet structure to pass device address to HBA.
2242 		 * Allocate one, without sleeping. Fail if pkt cannot be
2243 		 * allocated.
2244 		 */
2245 		sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP);
2246 		if (sata_pkt == NULL) {
2247 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2248 			    sata_device.satadev_addr.cport)));
2249 			SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: "
2250 			    "could not allocate sata_pkt"));
2251 			return (0);
2252 		}
2253 		sata_pkt->satapkt_rev = SATA_PKT_REV;
2254 		sata_pkt->satapkt_device = sata_device;
2255 		sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
2256 	} else {
2257 		if (scsi_pkt->pkt_ha_private == NULL) {
2258 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2259 			    sata_device.satadev_addr.cport)));
2260 			return (0); /* Bad scsi pkt */
2261 		}
2262 		/* extract pointer to sata pkt */
2263 		sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)->
2264 		    txlt_sata_pkt;
2265 	}
2266 
2267 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2268 	    sata_device.satadev_addr.cport)));
2269 	/* Send abort request to HBA */
2270 	if ((*SATA_ABORT_FUNC(sata_hba_inst))
2271 	    (SATA_DIP(sata_hba_inst), sata_pkt,
2272 	    scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) ==
2273 	    SATA_SUCCESS) {
2274 		if (scsi_pkt == NULL)
2275 			kmem_free(sata_pkt, sizeof (sata_pkt_t));
2276 		/* Success */
2277 		return (1);
2278 	}
2279 	/* Else, something did not go right */
2280 	if (scsi_pkt == NULL)
2281 		kmem_free(sata_pkt, sizeof (sata_pkt_t));
2282 	/* Failure */
2283 	return (0);
2284 }
2285 
2286 
2287 /*
2288  * Implementation of scsi tran_reset.
2289  * RESET_ALL request is translated into port reset.
2290  * RESET_TARGET requests is translated into a device reset,
2291  * RESET_LUN request is accepted only for LUN 0 and translated into
2292  * device reset.
2293  * The target reset should cause all HBA active and queued packets to
2294  * be terminated and returned with pkt reason SATA_PKT_RESET prior to
2295  * the return. HBA should report reset event for the device.
2296  *
2297  * Returns 1 upon success, 0 upon failure.
2298  */
2299 static int
2300 sata_scsi_reset(struct scsi_address *ap, int level)
2301 {
2302 	sata_hba_inst_t	*sata_hba_inst =
2303 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2304 	sata_device_t	sata_device;
2305 	int		val;
2306 
2307 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2308 	    "sata_scsi_reset: level %d target: 0x%x\n",
2309 	    level, ap->a_target);
2310 
2311 	/* Validate address */
2312 	val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device);
2313 	if (val == -1)
2314 		/* Invalid address */
2315 		return (0);
2316 
2317 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2318 	    sata_device.satadev_addr.cport)));
2319 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2320 		/* invalid address */
2321 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2322 		    sata_device.satadev_addr.cport)));
2323 		return (0);
2324 	}
2325 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2326 	    sata_device.satadev_addr.cport)));
2327 	if (level == RESET_ALL) {
2328 		/* port reset - cport only */
2329 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2330 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2331 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2332 			return (1);
2333 		else
2334 			return (0);
2335 
2336 	} else if (val == 0 &&
2337 	    (level == RESET_TARGET || level == RESET_LUN)) {
2338 		/* reset device (device attached) */
2339 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2340 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2341 			return (1);
2342 		else
2343 			return (0);
2344 	}
2345 	return (0);
2346 }
2347 
2348 
2349 /*
2350  * Implementation of scsi tran_getcap (get transport/device capabilities).
2351  * Supported capabilities for SATA hard disks:
2352  * auto-rqsense		(always supported)
2353  * tagged-qing		(supported if HBA supports it)
2354  * untagged-qing	(could be supported if disk supports it, but because
2355  *			 caching behavior allowing untagged queuing actually
2356  *			 results in reduced performance.  sd tries to throttle
2357  *			 back to only 3 outstanding commands, which may
2358  *			 work for real SCSI disks, but with read ahead
2359  *			 caching, having more than 1 outstanding command
2360  *			 results in cache thrashing.)
2361  * sector_size
2362  * dma_max
2363  * interconnect-type	(INTERCONNECT_SATA)
2364  *
2365  * Supported capabilities for ATAPI devices (CD/DVD):
2366  * auto-rqsense		(always supported)
2367  * sector_size
2368  * dma_max
2369  * interconnect-type	(INTERCONNECT_SATA)
2370  *
2371  * Request for other capabilities is rejected as unsupported.
2372  *
2373  * Returns supported capability value, or -1 if capability is unsuppported or
2374  * the address is invalid - no device.
2375  */
2376 
2377 static int
2378 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
2379 {
2380 
2381 	sata_hba_inst_t 	*sata_hba_inst =
2382 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2383 	sata_device_t		sata_device;
2384 	sata_drive_info_t	*sdinfo;
2385 	ddi_dma_attr_t		adj_dma_attr;
2386 	int 			rval;
2387 
2388 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2389 	    "sata_scsi_getcap: target: 0x%x, cap: %s\n",
2390 	    ap->a_target, cap);
2391 
2392 	/*
2393 	 * We want to process the capabilities on per port granularity.
2394 	 * So, we are specifically restricting ourselves to whom != 0
2395 	 * to exclude the controller wide handling.
2396 	 */
2397 	if (cap == NULL || whom == 0)
2398 		return (-1);
2399 
2400 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2401 		/* Invalid address */
2402 		return (-1);
2403 	}
2404 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2405 	    sata_device.satadev_addr.cport)));
2406 	if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) ==
2407 	    NULL) {
2408 		/* invalid address */
2409 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2410 		    sata_device.satadev_addr.cport)));
2411 		return (-1);
2412 	}
2413 
2414 	switch (scsi_hba_lookup_capstr(cap)) {
2415 	case SCSI_CAP_ARQ:
2416 		rval = 1;		/* ARQ supported, turned on */
2417 		break;
2418 
2419 	case SCSI_CAP_SECTOR_SIZE:
2420 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK)
2421 			rval = SATA_DISK_SECTOR_SIZE;	/* fixed size */
2422 		else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD)
2423 			rval = SATA_ATAPI_SECTOR_SIZE;
2424 		else rval = -1;
2425 		break;
2426 
2427 	/*
2428 	 * untagged queuing cause a performance inversion because of
2429 	 * the way sd operates.  Because of this reason we do not
2430 	 * use it when available.
2431 	 */
2432 	case SCSI_CAP_UNTAGGED_QING:
2433 		if (sdinfo->satadrv_features_enabled &
2434 		    SATA_DEV_F_E_UNTAGGED_QING)
2435 			rval = 1;	/* Untagged queuing available */
2436 		else
2437 			rval = -1;	/* Untagged queuing not available */
2438 		break;
2439 
2440 	case SCSI_CAP_TAGGED_QING:
2441 		if ((sdinfo->satadrv_features_enabled &
2442 		    SATA_DEV_F_E_TAGGED_QING) &&
2443 		    (sdinfo->satadrv_max_queue_depth > 1))
2444 			rval = 1;	/* Tagged queuing available */
2445 		else
2446 			rval = -1;	/* Tagged queuing not available */
2447 		break;
2448 
2449 	case SCSI_CAP_DMA_MAX:
2450 		sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst),
2451 		    &adj_dma_attr);
2452 		rval = (int)adj_dma_attr.dma_attr_maxxfer;
2453 		/* We rely on the fact that dma_attr_maxxfer < 0x80000000 */
2454 		break;
2455 
2456 	case SCSI_CAP_INTERCONNECT_TYPE:
2457 		rval = INTERCONNECT_SATA;	/* SATA interconnect type */
2458 		break;
2459 
2460 	default:
2461 		rval = -1;
2462 		break;
2463 	}
2464 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2465 	    sata_device.satadev_addr.cport)));
2466 	return (rval);
2467 }
2468 
2469 /*
2470  * Implementation of scsi tran_setcap
2471  *
2472  * Only SCSI_CAP_UNTAGGED_QING and  SCSI_CAP_TAGGED_QING are changeable.
2473  *
2474  */
2475 static int
2476 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
2477 {
2478 	sata_hba_inst_t	*sata_hba_inst =
2479 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2480 	sata_device_t	sata_device;
2481 	sata_drive_info_t	*sdinfo;
2482 	int		rval;
2483 
2484 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2485 	    "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap);
2486 
2487 	/*
2488 	 * We want to process the capabilities on per port granularity.
2489 	 * So, we are specifically restricting ourselves to whom != 0
2490 	 * to exclude the controller wide handling.
2491 	 */
2492 	if (cap == NULL || whom == 0) {
2493 		return (-1);
2494 	}
2495 
2496 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2497 		/* Invalid address */
2498 		return (-1);
2499 	}
2500 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2501 	    sata_device.satadev_addr.cport)));
2502 	if ((sdinfo = sata_get_device_info(sata_hba_inst,
2503 	    &sata_device)) == NULL) {
2504 		/* invalid address */
2505 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2506 		    sata_device.satadev_addr.cport)));
2507 		return (-1);
2508 	}
2509 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2510 	    sata_device.satadev_addr.cport)));
2511 
2512 	switch (scsi_hba_lookup_capstr(cap)) {
2513 	case SCSI_CAP_ARQ:
2514 	case SCSI_CAP_SECTOR_SIZE:
2515 	case SCSI_CAP_DMA_MAX:
2516 	case SCSI_CAP_INTERCONNECT_TYPE:
2517 		rval = 0;
2518 		break;
2519 	case SCSI_CAP_UNTAGGED_QING:
2520 		if (SATA_QDEPTH(sata_hba_inst) > 1) {
2521 			rval = 1;
2522 			if (value == 1) {
2523 				sdinfo->satadrv_features_enabled |=
2524 				    SATA_DEV_F_E_UNTAGGED_QING;
2525 			} else if (value == 0) {
2526 				sdinfo->satadrv_features_enabled &=
2527 				    ~SATA_DEV_F_E_UNTAGGED_QING;
2528 			} else {
2529 				rval = -1;
2530 			}
2531 		} else {
2532 			rval = 0;
2533 		}
2534 		break;
2535 	case SCSI_CAP_TAGGED_QING:
2536 		/* This can TCQ or NCQ */
2537 		if (sata_func_enable & SATA_ENABLE_QUEUING &&
2538 		    ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ &&
2539 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) ||
2540 		    (sata_func_enable & SATA_ENABLE_NCQ &&
2541 		    sdinfo->satadrv_features_support & SATA_DEV_F_NCQ &&
2542 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) &&
2543 		    (sdinfo->satadrv_max_queue_depth > 1)) {
2544 			rval = 1;
2545 			if (value == 1) {
2546 				sdinfo->satadrv_features_enabled |=
2547 				    SATA_DEV_F_E_TAGGED_QING;
2548 			} else if (value == 0) {
2549 				sdinfo->satadrv_features_enabled &=
2550 				    ~SATA_DEV_F_E_TAGGED_QING;
2551 			} else {
2552 				rval = -1;
2553 			}
2554 		} else {
2555 			rval = 0;
2556 		}
2557 		break;
2558 	default:
2559 		rval = -1;
2560 		break;
2561 	}
2562 	return (rval);
2563 }
2564 
2565 /*
2566  * Implementations of scsi tran_destroy_pkt.
2567  * Free resources allocated by sata_scsi_init_pkt()
2568  */
2569 static void
2570 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2571 {
2572 	sata_pkt_txlate_t *spx;
2573 
2574 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2575 
2576 	if (spx->txlt_buf_dma_handle != NULL) {
2577 		if (spx->txlt_tmp_buf != NULL)  {
2578 			ASSERT(spx->txlt_tmp_buf_handle != 0);
2579 			/*
2580 			 * Intermediate DMA buffer was allocated.
2581 			 * Free allocated buffer and associated access handle.
2582 			 */
2583 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
2584 			spx->txlt_tmp_buf = NULL;
2585 		}
2586 		/*
2587 		 * Free DMA resources - cookies and handles
2588 		 */
2589 		if (spx->txlt_dma_cookie_list != NULL) {
2590 			if (spx->txlt_dma_cookie_list !=
2591 			    &spx->txlt_dma_cookie) {
2592 				(void) kmem_free(spx->txlt_dma_cookie_list,
2593 				    spx->txlt_dma_cookie_list_len *
2594 				    sizeof (ddi_dma_cookie_t));
2595 				spx->txlt_dma_cookie_list = NULL;
2596 			}
2597 		}
2598 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
2599 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
2600 	}
2601 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2602 	sata_pkt_free(spx);
2603 
2604 	scsi_hba_pkt_free(ap, pkt);
2605 }
2606 
2607 /*
2608  * Implementation of scsi tran_dmafree.
2609  * Free DMA resources allocated by sata_scsi_init_pkt()
2610  */
2611 
2612 static void
2613 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
2614 {
2615 #ifndef __lock_lint
2616 	_NOTE(ARGUNUSED(ap))
2617 #endif
2618 	sata_pkt_txlate_t *spx;
2619 
2620 	ASSERT(pkt != NULL);
2621 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2622 
2623 	if (spx->txlt_buf_dma_handle != NULL) {
2624 		if (spx->txlt_tmp_buf != NULL)  {
2625 			/*
2626 			 * Intermediate DMA buffer was allocated.
2627 			 * Free allocated buffer and associated access handle.
2628 			 */
2629 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
2630 			spx->txlt_tmp_buf = NULL;
2631 		}
2632 		/*
2633 		 * Free DMA resources - cookies and handles
2634 		 */
2635 		/* ASSERT(spx->txlt_dma_cookie_list != NULL); */
2636 		if (spx->txlt_dma_cookie_list != NULL) {
2637 			if (spx->txlt_dma_cookie_list !=
2638 			    &spx->txlt_dma_cookie) {
2639 				(void) kmem_free(spx->txlt_dma_cookie_list,
2640 				    spx->txlt_dma_cookie_list_len *
2641 				    sizeof (ddi_dma_cookie_t));
2642 				spx->txlt_dma_cookie_list = NULL;
2643 			}
2644 		}
2645 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
2646 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
2647 		spx->txlt_buf_dma_handle = NULL;
2648 	}
2649 }
2650 
2651 /*
2652  * Implementation of scsi tran_sync_pkt.
2653  *
2654  * The assumption below is that pkt is unique - there is no need to check ap
2655  *
2656  * Synchronize DMA buffer and, if the intermediate buffer is used, copy data
2657  * into/from the real buffer.
2658  */
2659 static void
2660 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2661 {
2662 #ifndef __lock_lint
2663 	_NOTE(ARGUNUSED(ap))
2664 #endif
2665 	int rval;
2666 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2667 	struct buf *bp;
2668 	int direction;
2669 
2670 	ASSERT(spx != NULL);
2671 	if (spx->txlt_buf_dma_handle != NULL) {
2672 		direction = spx->txlt_sata_pkt->
2673 		    satapkt_cmd.satacmd_flags.sata_data_direction;
2674 		if (spx->txlt_sata_pkt != NULL &&
2675 		    direction != SATA_DIR_NODATA_XFER) {
2676 			if (spx->txlt_tmp_buf != NULL) {
2677 				/* Intermediate DMA buffer used */
2678 				bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2679 
2680 				if (direction & SATA_DIR_WRITE) {
2681 					bcopy(bp->b_un.b_addr,
2682 					    spx->txlt_tmp_buf, bp->b_bcount);
2683 				}
2684 			}
2685 			/* Sync the buffer for device or for CPU */
2686 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle,   0, 0,
2687 			    (direction & SATA_DIR_WRITE) ?
2688 			    DDI_DMA_SYNC_FORDEV :  DDI_DMA_SYNC_FORCPU);
2689 			ASSERT(rval == DDI_SUCCESS);
2690 			if (spx->txlt_tmp_buf != NULL &&
2691 			    !(direction & SATA_DIR_WRITE)) {
2692 				/* Intermediate DMA buffer used for read */
2693 				bcopy(spx->txlt_tmp_buf,
2694 				    bp->b_un.b_addr, bp->b_bcount);
2695 			}
2696 
2697 		}
2698 	}
2699 }
2700 
2701 
2702 
2703 /* *******************  SATA - SCSI Translation functions **************** */
2704 /*
2705  * SCSI to SATA pkt and command translation and SATA to SCSI status/error
2706  * translation.
2707  */
2708 
2709 /*
2710  * Checks if a device exists and can be access and translates common
2711  * scsi_pkt data to sata_pkt data.
2712  *
2713  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and
2714  * sata_pkt was set-up.
2715  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not
2716  * exist and pkt_comp callback was scheduled.
2717  * Returns other TRAN_XXXXX values when error occured and command should be
2718  * rejected with the returned TRAN_XXXXX value.
2719  *
2720  * This function should be called with port mutex held.
2721  */
2722 static int
2723 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx, int *reason)
2724 {
2725 	sata_drive_info_t *sdinfo;
2726 	sata_device_t sata_device;
2727 	const struct sata_cmd_flags sata_initial_cmd_flags = {
2728 		SATA_DIR_NODATA_XFER,
2729 		/* all other values to 0/FALSE */
2730 	};
2731 	/*
2732 	 * Pkt_reason has to be set if the pkt_comp callback is invoked,
2733 	 * and that implies TRAN_ACCEPT return value. Any other returned value
2734 	 * indicates that the scsi packet was not accepted (the reason will not
2735 	 * be checked by the scsi target driver).
2736 	 * To make debugging easier, we set pkt_reason to know value here.
2737 	 * It may be changed later when different completion reason is
2738 	 * determined.
2739 	 */
2740 	spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
2741 	*reason = CMD_TRAN_ERR;
2742 
2743 	/* Validate address */
2744 	switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst,
2745 	    &spx->txlt_scsi_pkt->pkt_address, &sata_device)) {
2746 
2747 	case -1:
2748 		/* Invalid address or invalid device type */
2749 		return (TRAN_BADPKT);
2750 	case 1:
2751 		/* valid address but no device - it has disappeared ? */
2752 		spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE;
2753 		*reason = CMD_DEV_GONE;
2754 		/*
2755 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2756 		 * only in callback function (for normal requests) and
2757 		 * in the dump code path.
2758 		 * So, if the callback is available, we need to do
2759 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2760 		 */
2761 		if (spx->txlt_scsi_pkt->pkt_comp != NULL) {
2762 			/* scsi callback required */
2763 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2764 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2765 			    (void *)spx->txlt_scsi_pkt,
2766 			    TQ_SLEEP) == NULL)
2767 				/* Scheduling the callback failed */
2768 				return (TRAN_BUSY);
2769 
2770 			return (TRAN_ACCEPT);
2771 		}
2772 		return (TRAN_FATAL_ERROR);
2773 	default:
2774 		/* all OK; pkt reason will be overwritten later */
2775 		break;
2776 	}
2777 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2778 	    &spx->txlt_sata_pkt->satapkt_device);
2779 
2780 	/*
2781 	 * If device is in reset condition, reject the packet with
2782 	 * TRAN_BUSY, unless:
2783 	 * 1. system is panicking (dumping)
2784 	 * In such case only one thread is running and there is no way to
2785 	 * process reset.
2786 	 * 2. cfgadm operation is is progress (internal APCTL lock is set)
2787 	 * Some cfgadm operations involve drive commands, so reset condition
2788 	 * needs to be ignored for IOCTL operations.
2789 	 */
2790 	if ((sdinfo->satadrv_event_flags &
2791 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
2792 
2793 		if (!ddi_in_panic() &&
2794 		    ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst,
2795 		    sata_device.satadev_addr.cport) &
2796 		    SATA_APCTL_LOCK_PORT_BUSY) == 0)) {
2797 			spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
2798 			*reason = CMD_INCOMPLETE;
2799 			SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
2800 			    "sata_scsi_start: rejecting command because "
2801 			    "of device reset state\n", NULL);
2802 			return (TRAN_BUSY);
2803 		}
2804 	}
2805 
2806 	/*
2807 	 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by
2808 	 * sata_scsi_pkt_init() because pkt init had to work also with
2809 	 * non-existing devices.
2810 	 * Now we know that the packet was set-up for a real device, so its
2811 	 * type is known.
2812 	 */
2813 	spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type;
2814 
2815 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags;
2816 	if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst,
2817 	    sata_device.satadev_addr.cport)->cport_event_flags &
2818 	    SATA_APCTL_LOCK_PORT_BUSY) != 0) {
2819 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
2820 		    sata_ignore_dev_reset = B_TRUE;
2821 	}
2822 	/*
2823 	 * At this point the generic translation routine determined that the
2824 	 * scsi packet should be accepted. Packet completion reason may be
2825 	 * changed later when a different completion reason is determined.
2826 	 */
2827 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
2828 	*reason = CMD_CMPLT;
2829 
2830 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
2831 		/* Synchronous execution */
2832 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH |
2833 		    SATA_OPMODE_POLLING;
2834 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
2835 		    sata_ignore_dev_reset = ddi_in_panic();
2836 	} else {
2837 		/* Asynchronous execution */
2838 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH |
2839 		    SATA_OPMODE_INTERRUPTS;
2840 	}
2841 	/* Convert queuing information */
2842 	if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG)
2843 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag =
2844 		    B_TRUE;
2845 	else if (spx->txlt_scsi_pkt->pkt_flags &
2846 	    (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD))
2847 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag =
2848 		    B_TRUE;
2849 
2850 	/* Always limit pkt time */
2851 	if (spx->txlt_scsi_pkt->pkt_time == 0)
2852 		spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time;
2853 	else
2854 		/* Pass on scsi_pkt time */
2855 		spx->txlt_sata_pkt->satapkt_time =
2856 		    spx->txlt_scsi_pkt->pkt_time;
2857 
2858 	return (TRAN_ACCEPT);
2859 }
2860 
2861 
2862 /*
2863  * Translate ATA Identify Device data to SCSI Inquiry data.
2864  * This function may be called only for ATA devices.
2865  * This function should not be called for ATAPI devices - they
2866  * respond directly to SCSI Inquiry command.
2867  *
2868  * SATA Identify Device data has to be valid in sata_rive_info.
2869  * Buffer has to accomodate the inquiry length (36 bytes).
2870  *
2871  * This function should be called with a port mutex held.
2872  */
2873 static	void
2874 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst,
2875     sata_drive_info_t *sdinfo, uint8_t *buf)
2876 {
2877 
2878 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
2879 	struct sata_id *sid = &sdinfo->satadrv_id;
2880 
2881 	/* Start with a nice clean slate */
2882 	bzero((void *)inq, sizeof (struct scsi_inquiry));
2883 
2884 	/*
2885 	 * Rely on the dev_type for setting paripheral qualifier.
2886 	 * Assume that  DTYPE_RODIRECT applies to CD/DVD R/W devices.
2887 	 * It could be that DTYPE_OPTICAL could also qualify in the future.
2888 	 * ATAPI Inquiry may provide more data to the target driver.
2889 	 */
2890 	inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
2891 	    DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */
2892 
2893 	inq->inq_rmb = sid->ai_config & SATA_REM_MEDIA ? 1 : 0;
2894 	inq->inq_qual = 0;	/* Device type qualifier (obsolete in SCSI3? */
2895 	inq->inq_iso = 0;	/* ISO version */
2896 	inq->inq_ecma = 0;	/* ECMA version */
2897 	inq->inq_ansi = 3;	/* ANSI version - SCSI 3 */
2898 	inq->inq_aenc = 0;	/* Async event notification cap. */
2899 	inq->inq_trmiop = 0;	/* Supports TERMINATE I/O PROC msg - NO */
2900 	inq->inq_normaca = 0;	/* setting NACA bit supported - NO */
2901 	inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */
2902 	inq->inq_len = 31;	/* Additional length */
2903 	inq->inq_dualp = 0;	/* dual port device - NO */
2904 	inq->inq_reladdr = 0;	/* Supports relative addressing - NO */
2905 	inq->inq_sync = 0;	/* Supports synchronous data xfers - NO */
2906 	inq->inq_linked = 0;	/* Supports linked commands - NO */
2907 				/*
2908 				 * Queuing support - controller has to
2909 				 * support some sort of command queuing.
2910 				 */
2911 	if (SATA_QDEPTH(sata_hba_inst) > 1)
2912 		inq->inq_cmdque = 1; /* Supports command queueing - YES */
2913 	else
2914 		inq->inq_cmdque = 0; /* Supports command queueing - NO */
2915 	inq->inq_sftre = 0;	/* Supports Soft Reset option - NO ??? */
2916 	inq->inq_wbus32 = 0;	/* Supports 32 bit wide data xfers - NO */
2917 	inq->inq_wbus16 = 0;	/* Supports 16 bit wide data xfers - NO */
2918 
2919 #ifdef	_LITTLE_ENDIAN
2920 	/* Swap text fields to match SCSI format */
2921 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
2922 	swab(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
2923 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
2924 		swab(sid->ai_fw, inq->inq_revision, 4);	/* Revision level */
2925 	else
2926 		swab(&sid->ai_fw[4], inq->inq_revision, 4);	/* Rev. level */
2927 #else	/* _LITTLE_ENDIAN */
2928 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
2929 	bcopy(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
2930 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
2931 		bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
2932 	else
2933 		bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
2934 #endif	/* _LITTLE_ENDIAN */
2935 }
2936 
2937 
2938 /*
2939  * Scsi response set up for invalid command (command not supported)
2940  *
2941  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
2942  */
2943 static int
2944 sata_txlt_invalid_command(sata_pkt_txlate_t *spx)
2945 {
2946 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
2947 	struct scsi_extended_sense *sense;
2948 
2949 	scsipkt->pkt_reason = CMD_CMPLT;
2950 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
2951 	    STATE_SENT_CMD | STATE_GOT_STATUS;
2952 
2953 	*scsipkt->pkt_scbp = STATUS_CHECK;
2954 
2955 	sense = sata_arq_sense(spx);
2956 	sense->es_key = KEY_ILLEGAL_REQUEST;
2957 	sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE;
2958 
2959 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
2960 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
2961 
2962 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
2963 	    scsipkt->pkt_comp != NULL)
2964 		/* scsi callback required */
2965 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2966 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2967 		    (void *)spx->txlt_scsi_pkt,
2968 		    TQ_SLEEP) == NULL)
2969 			/* Scheduling the callback failed */
2970 			return (TRAN_BUSY);
2971 	return (TRAN_ACCEPT);
2972 }
2973 
2974 /*
2975  * Scsi response setup for
2976  * emulated non-data command that requires no action/return data
2977  *
2978  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
2979  */
2980 static 	int
2981 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx)
2982 {
2983 	int rval;
2984 	int reason;
2985 
2986 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
2987 
2988 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
2989 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
2990 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
2991 		return (rval);
2992 	}
2993 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
2994 
2995 	spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
2996 	    STATE_SENT_CMD | STATE_GOT_STATUS;
2997 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
2998 	*(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD;
2999 
3000 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3001 	    "Scsi_pkt completion reason %x\n",
3002 	    spx->txlt_scsi_pkt->pkt_reason);
3003 
3004 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 &&
3005 	    spx->txlt_scsi_pkt->pkt_comp != NULL)
3006 		/* scsi callback required */
3007 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3008 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3009 		    (void *)spx->txlt_scsi_pkt,
3010 		    TQ_SLEEP) == NULL)
3011 			/* Scheduling the callback failed */
3012 			return (TRAN_BUSY);
3013 	return (TRAN_ACCEPT);
3014 }
3015 
3016 
3017 /*
3018  * SATA translate command: Inquiry / Identify Device
3019  * Use cached Identify Device data for now, rather than issuing actual
3020  * Device Identify cmd request. If device is detached and re-attached,
3021  * asynchromous event processing should fetch and refresh Identify Device
3022  * data.
3023  * Two VPD pages are supported now:
3024  * Vital Product Data page
3025  * Unit Serial Number page
3026  *
3027  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3028  */
3029 
3030 #define	EVPD			1	/* Extended Vital Product Data flag */
3031 #define	CMDDT			2	/* Command Support Data - Obsolete */
3032 #define	INQUIRY_SUP_VPD_PAGE	0	/* Supported VDP Pages Page COde */
3033 #define	INQUIRY_USN_PAGE	0x80	/* Unit Serial Number Page Code */
3034 #define	INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */
3035 
3036 static int
3037 sata_txlt_inquiry(sata_pkt_txlate_t *spx)
3038 {
3039 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3040 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3041 	sata_drive_info_t *sdinfo;
3042 	struct scsi_extended_sense *sense;
3043 	int count;
3044 	uint8_t *p;
3045 	int i, j;
3046 	uint8_t page_buf[0xff]; /* Max length */
3047 	int rval, reason;
3048 
3049 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3050 
3051 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3052 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3053 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3054 		return (rval);
3055 	}
3056 
3057 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3058 	    &spx->txlt_sata_pkt->satapkt_device);
3059 
3060 	ASSERT(sdinfo != NULL);
3061 
3062 	scsipkt->pkt_reason = CMD_CMPLT;
3063 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3064 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3065 
3066 	/* Reject not supported request */
3067 	if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */
3068 		*scsipkt->pkt_scbp = STATUS_CHECK;
3069 		sense = sata_arq_sense(spx);
3070 		sense->es_key = KEY_ILLEGAL_REQUEST;
3071 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3072 		goto done;
3073 	}
3074 
3075 	/* Valid Inquiry request */
3076 	*scsipkt->pkt_scbp = STATUS_GOOD;
3077 
3078 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3079 
3080 		/*
3081 		 * Because it is fully emulated command storing data
3082 		 * programatically in the specified buffer, release
3083 		 * preallocated DMA resources before storing data in the buffer,
3084 		 * so no unwanted DMA sync would take place.
3085 		 */
3086 		sata_scsi_dmafree(NULL, scsipkt);
3087 
3088 		if (!(scsipkt->pkt_cdbp[1] & EVPD)) {
3089 			/* Standard Inquiry Data request */
3090 			struct scsi_inquiry inq;
3091 			unsigned int bufsize;
3092 
3093 			sata_identdev_to_inquiry(spx->txlt_sata_hba_inst,
3094 			    sdinfo, (uint8_t *)&inq);
3095 			/* Copy no more than requested */
3096 			count = MIN(bp->b_bcount,
3097 			    sizeof (struct scsi_inquiry));
3098 			bufsize = scsipkt->pkt_cdbp[4];
3099 			bufsize |= scsipkt->pkt_cdbp[3] << 8;
3100 			count = MIN(count, bufsize);
3101 			bcopy(&inq, bp->b_un.b_addr, count);
3102 
3103 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
3104 			scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3105 			    bufsize - count : 0;
3106 		} else {
3107 			/*
3108 			 * peripheral_qualifier = 0;
3109 			 *
3110 			 * We are dealing only with HD and will be
3111 			 * dealing with CD/DVD devices soon
3112 			 */
3113 			uint8_t peripheral_device_type =
3114 			    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3115 			    DTYPE_DIRECT : DTYPE_RODIRECT;
3116 
3117 			switch ((uint_t)scsipkt->pkt_cdbp[2]) {
3118 			case INQUIRY_SUP_VPD_PAGE:
3119 				/*
3120 				 * Request for suported Vital Product Data
3121 				 * pages - assuming only 2 page codes
3122 				 * supported
3123 				 */
3124 				page_buf[0] = peripheral_device_type;
3125 				page_buf[1] = INQUIRY_SUP_VPD_PAGE;
3126 				page_buf[2] = 0;
3127 				page_buf[3] = 2; /* page length */
3128 				page_buf[4] = INQUIRY_SUP_VPD_PAGE;
3129 				page_buf[5] = INQUIRY_USN_PAGE;
3130 				/* Copy no more than requested */
3131 				count = MIN(bp->b_bcount, 6);
3132 				bcopy(page_buf, bp->b_un.b_addr, count);
3133 				break;
3134 			case INQUIRY_USN_PAGE:
3135 				/*
3136 				 * Request for Unit Serial Number page
3137 				 */
3138 				page_buf[0] = peripheral_device_type;
3139 				page_buf[1] = INQUIRY_USN_PAGE;
3140 				page_buf[2] = 0;
3141 				page_buf[3] = 20; /* remaining page length */
3142 				p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser);
3143 #ifdef	_LITTLE_ENDIAN
3144 				swab(p, &page_buf[4], 20);
3145 #else
3146 				bcopy(p, &page_buf[4], 20);
3147 #endif
3148 				for (i = 0; i < 20; i++) {
3149 					if (page_buf[4 + i] == '\0' ||
3150 					    page_buf[4 + i] == '\040') {
3151 						break;
3152 					}
3153 				}
3154 				/*
3155 				 * 'i' contains string length.
3156 				 *
3157 				 * Least significant character of the serial
3158 				 * number shall appear as the last byte,
3159 				 * according to SBC-3 spec.
3160 				 */
3161 				p = &page_buf[20 + 4 - 1];
3162 				for (j = i; j > 0; j--, p--) {
3163 					*p = *(p - 20 + i);
3164 				}
3165 				p = &page_buf[4];
3166 				for (j = 20 - i; j > 0; j--) {
3167 					*p++ = '\040';
3168 				}
3169 				count = MIN(bp->b_bcount, 24);
3170 				bcopy(page_buf, bp->b_un.b_addr, count);
3171 				break;
3172 
3173 			case INQUIRY_DEV_IDENTIFICATION_PAGE:
3174 				/*
3175 				 * We may want to implement this page, when
3176 				 * identifiers are common for SATA devices
3177 				 * But not now.
3178 				 */
3179 				/*FALLTHROUGH*/
3180 
3181 			default:
3182 				/* Request for unsupported VPD page */
3183 				*scsipkt->pkt_scbp = STATUS_CHECK;
3184 				sense = sata_arq_sense(spx);
3185 				sense->es_key = KEY_ILLEGAL_REQUEST;
3186 				sense->es_add_code =
3187 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3188 				goto done;
3189 			}
3190 		}
3191 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3192 		scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3193 		    scsipkt->pkt_cdbp[4] - count : 0;
3194 	}
3195 done:
3196 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3197 
3198 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3199 	    "Scsi_pkt completion reason %x\n",
3200 	    scsipkt->pkt_reason);
3201 
3202 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3203 	    scsipkt->pkt_comp != NULL) {
3204 		/* scsi callback required */
3205 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3206 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3207 		    TQ_SLEEP) == NULL)
3208 			/* Scheduling the callback failed */
3209 			return (TRAN_BUSY);
3210 	}
3211 	return (TRAN_ACCEPT);
3212 }
3213 
3214 /*
3215  * SATA translate command: Request Sense.
3216  * Emulated command (ATA version for SATA hard disks)
3217  * Always NO SENSE, because any sense data should be reported by ARQ sense.
3218  *
3219  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3220  */
3221 static int
3222 sata_txlt_request_sense(sata_pkt_txlate_t *spx)
3223 {
3224 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3225 	struct scsi_extended_sense sense;
3226 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3227 	int rval, reason;
3228 
3229 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3230 
3231 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3232 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3233 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3234 		return (rval);
3235 	}
3236 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3237 
3238 
3239 	scsipkt->pkt_reason = CMD_CMPLT;
3240 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3241 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3242 	*scsipkt->pkt_scbp = STATUS_GOOD;
3243 
3244 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3245 		/*
3246 		 * Because it is fully emulated command storing data
3247 		 * programatically in the specified buffer, release
3248 		 * preallocated DMA resources before storing data in the buffer,
3249 		 * so no unwanted DMA sync would take place.
3250 		 */
3251 		int count = MIN(bp->b_bcount,
3252 		    sizeof (struct scsi_extended_sense));
3253 		sata_scsi_dmafree(NULL, scsipkt);
3254 		bzero(&sense, sizeof (struct scsi_extended_sense));
3255 		sense.es_valid = 0;	/* Valid LBA */
3256 		sense.es_class = 7;	/* Response code 0x70 - current err */
3257 		sense.es_key = KEY_NO_SENSE;
3258 		sense.es_add_len = 6;	/* Additional length */
3259 		/* Copy no more than requested */
3260 		bcopy(&sense, bp->b_un.b_addr, count);
3261 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3262 		scsipkt->pkt_resid = 0;
3263 	}
3264 
3265 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3266 	    "Scsi_pkt completion reason %x\n",
3267 	    scsipkt->pkt_reason);
3268 
3269 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3270 	    scsipkt->pkt_comp != NULL)
3271 		/* scsi callback required */
3272 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3273 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3274 		    TQ_SLEEP) == NULL)
3275 			/* Scheduling the callback failed */
3276 			return (TRAN_BUSY);
3277 	return (TRAN_ACCEPT);
3278 }
3279 
3280 /*
3281  * SATA translate command: Test Unit Ready
3282  * At the moment this is an emulated command (ATA version for SATA hard disks).
3283  * May be translated into Check Power Mode command in the future
3284  *
3285  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3286  */
3287 static int
3288 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx)
3289 {
3290 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3291 	struct scsi_extended_sense *sense;
3292 	int power_state;
3293 	int rval, reason;
3294 
3295 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3296 
3297 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3298 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3299 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3300 		return (rval);
3301 	}
3302 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3303 
3304 	/* At this moment, emulate it rather than execute anything */
3305 	power_state = SATA_PWRMODE_ACTIVE;
3306 
3307 	scsipkt->pkt_reason = CMD_CMPLT;
3308 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3309 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3310 
3311 	switch (power_state) {
3312 	case SATA_PWRMODE_ACTIVE:
3313 	case SATA_PWRMODE_IDLE:
3314 		*scsipkt->pkt_scbp = STATUS_GOOD;
3315 		break;
3316 	default:
3317 		/* PWR mode standby */
3318 		*scsipkt->pkt_scbp = STATUS_CHECK;
3319 		sense = sata_arq_sense(spx);
3320 		sense->es_key = KEY_NOT_READY;
3321 		sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY;
3322 		break;
3323 	}
3324 
3325 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3326 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3327 
3328 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3329 	    scsipkt->pkt_comp != NULL)
3330 		/* scsi callback required */
3331 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3332 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3333 		    TQ_SLEEP) == NULL)
3334 			/* Scheduling the callback failed */
3335 			return (TRAN_BUSY);
3336 
3337 	return (TRAN_ACCEPT);
3338 }
3339 
3340 
3341 /*
3342  * SATA translate command: Start Stop Unit
3343  * Translation depends on a command:
3344  *	Start Unit translated into Idle Immediate
3345  *	Stop Unit translated into Standby Immediate
3346  *	Unload Media / NOT SUPPORTED YET
3347  *	Load Media / NOT SUPPROTED YET
3348  * Power condition bits are ignored, so is Immediate bit
3349  * Requesting synchronous execution.
3350  *
3351  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
3352  * appropriate values in scsi_pkt fields.
3353  */
3354 static int
3355 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx)
3356 {
3357 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3358 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3359 	struct scsi_extended_sense *sense;
3360 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3361 	int cport = SATA_TXLT_CPORT(spx);
3362 	int rval, reason;
3363 	int synch;
3364 
3365 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3366 	    "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1);
3367 
3368 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3369 
3370 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3371 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3372 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3373 		return (rval);
3374 	}
3375 
3376 	if (scsipkt->pkt_cdbp[4] & 2) {
3377 		/* Load/Unload Media - invalid request */
3378 		*scsipkt->pkt_scbp = STATUS_CHECK;
3379 		sense = sata_arq_sense(spx);
3380 		sense->es_key = KEY_ILLEGAL_REQUEST;
3381 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3382 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3383 
3384 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3385 		    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3386 
3387 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3388 		    scsipkt->pkt_comp != NULL)
3389 			/* scsi callback required */
3390 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3391 			    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3392 			    TQ_SLEEP) == NULL)
3393 				/* Scheduling the callback failed */
3394 				return (TRAN_BUSY);
3395 
3396 		return (TRAN_ACCEPT);
3397 	}
3398 	scmd->satacmd_addr_type = 0;
3399 	scmd->satacmd_sec_count_lsb = 0;
3400 	scmd->satacmd_lba_low_lsb = 0;
3401 	scmd->satacmd_lba_mid_lsb = 0;
3402 	scmd->satacmd_lba_high_lsb = 0;
3403 	scmd->satacmd_features_reg = 0;
3404 	scmd->satacmd_device_reg = 0;
3405 	scmd->satacmd_status_reg = 0;
3406 	if (scsipkt->pkt_cdbp[4] & 1) {
3407 		/* Start Unit */
3408 		scmd->satacmd_cmd_reg = SATAC_IDLE_IM;
3409 	} else {
3410 		/* Stop Unit */
3411 		scmd->satacmd_cmd_reg = SATAC_STANDBY_IM;
3412 	}
3413 
3414 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
3415 		/* Need to set-up a callback function */
3416 		spx->txlt_sata_pkt->satapkt_comp =
3417 		    sata_txlt_nodata_cmd_completion;
3418 		synch = FALSE;
3419 	} else {
3420 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
3421 		synch = TRUE;
3422 	}
3423 
3424 	/* Transfer command to HBA */
3425 	if (sata_hba_start(spx, &rval) != 0) {
3426 		/* Pkt not accepted for execution */
3427 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3428 		return (rval);
3429 	}
3430 
3431 	/*
3432 	 * If execution is non-synchronous,
3433 	 * a callback function will handle potential errors, translate
3434 	 * the response and will do a callback to a target driver.
3435 	 * If it was synchronous, check execution status using the same
3436 	 * framework callback.
3437 	 */
3438 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3439 	if (synch) {
3440 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3441 		    "synchronous execution status %x\n",
3442 		    spx->txlt_sata_pkt->satapkt_reason);
3443 
3444 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
3445 	}
3446 	return (TRAN_ACCEPT);
3447 
3448 }
3449 
3450 
3451 /*
3452  * SATA translate command:  Read Capacity.
3453  * Emulated command for SATA disks.
3454  * Capacity is retrieved from cached Idenifty Device data.
3455  * Identify Device data shows effective disk capacity, not the native
3456  * capacity, which may be limitted by Set Max Address command.
3457  * This is ATA version for SATA hard disks.
3458  *
3459  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3460  */
3461 static int
3462 sata_txlt_read_capacity(sata_pkt_txlate_t *spx)
3463 {
3464 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3465 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3466 	sata_drive_info_t *sdinfo;
3467 	uint64_t val;
3468 	uchar_t *rbuf;
3469 	int rval, reason;
3470 
3471 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3472 	    "sata_txlt_read_capacity: ", NULL);
3473 
3474 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3475 
3476 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3477 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3478 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3479 		return (rval);
3480 	}
3481 
3482 	scsipkt->pkt_reason = CMD_CMPLT;
3483 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3484 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3485 	*scsipkt->pkt_scbp = STATUS_GOOD;
3486 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3487 		/*
3488 		 * Because it is fully emulated command storing data
3489 		 * programatically in the specified buffer, release
3490 		 * preallocated DMA resources before storing data in the buffer,
3491 		 * so no unwanted DMA sync would take place.
3492 		 */
3493 		sata_scsi_dmafree(NULL, scsipkt);
3494 
3495 		sdinfo = sata_get_device_info(
3496 		    spx->txlt_sata_hba_inst,
3497 		    &spx->txlt_sata_pkt->satapkt_device);
3498 		/* Last logical block address */
3499 		val = sdinfo->satadrv_capacity - 1;
3500 		rbuf = (uchar_t *)bp->b_un.b_addr;
3501 		/* Need to swap endians to match scsi format */
3502 		rbuf[0] = (val >> 24) & 0xff;
3503 		rbuf[1] = (val >> 16) & 0xff;
3504 		rbuf[2] = (val >> 8) & 0xff;
3505 		rbuf[3] = val & 0xff;
3506 		/* block size - always 512 bytes, for now */
3507 		rbuf[4] = 0;
3508 		rbuf[5] = 0;
3509 		rbuf[6] = 0x02;
3510 		rbuf[7] = 0;
3511 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3512 		scsipkt->pkt_resid = 0;
3513 
3514 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n",
3515 		    sdinfo->satadrv_capacity -1);
3516 	}
3517 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3518 	/*
3519 	 * If a callback was requested, do it now.
3520 	 */
3521 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3522 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3523 
3524 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3525 	    scsipkt->pkt_comp != NULL)
3526 		/* scsi callback required */
3527 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3528 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3529 		    TQ_SLEEP) == NULL)
3530 			/* Scheduling the callback failed */
3531 			return (TRAN_BUSY);
3532 
3533 	return (TRAN_ACCEPT);
3534 }
3535 
3536 /*
3537  * SATA translate command: Mode Sense.
3538  * Translated into appropriate SATA command or emulated.
3539  * Saved Values Page Control (03) are not supported.
3540  *
3541  * NOTE: only caching mode sense page is currently implemented.
3542  *
3543  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3544  */
3545 
3546 static int
3547 sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
3548 {
3549 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
3550 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3551 	sata_drive_info_t *sdinfo;
3552 	sata_id_t *sata_id;
3553 	struct scsi_extended_sense *sense;
3554 	int 		len, bdlen, count, alc_len;
3555 	int		pc;	/* Page Control code */
3556 	uint8_t		*buf;	/* mode sense buffer */
3557 	int		rval, reason;
3558 
3559 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3560 	    "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
3561 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
3562 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
3563 
3564 	buf = kmem_zalloc(1024, KM_SLEEP);
3565 
3566 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3567 
3568 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3569 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3570 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3571 		kmem_free(buf, 1024);
3572 		return (rval);
3573 	}
3574 
3575 	scsipkt->pkt_reason = CMD_CMPLT;
3576 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3577 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3578 
3579 	pc = scsipkt->pkt_cdbp[2] >> 6;
3580 
3581 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3582 		/*
3583 		 * Because it is fully emulated command storing data
3584 		 * programatically in the specified buffer, release
3585 		 * preallocated DMA resources before storing data in the buffer,
3586 		 * so no unwanted DMA sync would take place.
3587 		 */
3588 		sata_scsi_dmafree(NULL, scsipkt);
3589 
3590 		len = 0;
3591 		bdlen = 0;
3592 		if (!(scsipkt->pkt_cdbp[1] & 8)) {
3593 			if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
3594 			    (scsipkt->pkt_cdbp[0] & 0x10))
3595 				bdlen = 16;
3596 			else
3597 				bdlen = 8;
3598 		}
3599 		/* Build mode parameter header */
3600 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3601 			/* 4-byte mode parameter header */
3602 			buf[len++] = 0;   	/* mode data length */
3603 			buf[len++] = 0;		/* medium type */
3604 			buf[len++] = 0;		/* dev-specific param */
3605 			buf[len++] = bdlen;	/* Block Descriptor length */
3606 		} else {
3607 			/* 8-byte mode parameter header */
3608 			buf[len++] = 0;		/* mode data length */
3609 			buf[len++] = 0;
3610 			buf[len++] = 0;		/* medium type */
3611 			buf[len++] = 0;		/* dev-specific param */
3612 			if (bdlen == 16)
3613 				buf[len++] = 1;	/* long lba descriptor */
3614 			else
3615 				buf[len++] = 0;
3616 			buf[len++] = 0;
3617 			buf[len++] = 0;		/* Block Descriptor length */
3618 			buf[len++] = bdlen;
3619 		}
3620 
3621 		sdinfo = sata_get_device_info(
3622 		    spx->txlt_sata_hba_inst,
3623 		    &spx->txlt_sata_pkt->satapkt_device);
3624 
3625 		/* Build block descriptor only if not disabled (DBD) */
3626 		if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
3627 			/* Block descriptor - direct-access device format */
3628 			if (bdlen == 8) {
3629 				/* build regular block descriptor */
3630 				buf[len++] =
3631 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
3632 				buf[len++] =
3633 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
3634 				buf[len++] =
3635 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
3636 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
3637 				buf[len++] = 0; /* density code */
3638 				buf[len++] = 0;
3639 				if (sdinfo->satadrv_type ==
3640 				    SATA_DTYPE_ATADISK)
3641 					buf[len++] = 2;
3642 				else
3643 					/* ATAPI */
3644 					buf[len++] = 8;
3645 				buf[len++] = 0;
3646 			} else if (bdlen == 16) {
3647 				/* Long LBA Accepted */
3648 				/* build long lba block descriptor */
3649 #ifndef __lock_lint
3650 				buf[len++] =
3651 				    (sdinfo->satadrv_capacity >> 56) & 0xff;
3652 				buf[len++] =
3653 				    (sdinfo->satadrv_capacity >> 48) & 0xff;
3654 				buf[len++] =
3655 				    (sdinfo->satadrv_capacity >> 40) & 0xff;
3656 				buf[len++] =
3657 				    (sdinfo->satadrv_capacity >> 32) & 0xff;
3658 #endif
3659 				buf[len++] =
3660 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
3661 				buf[len++] =
3662 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
3663 				buf[len++] =
3664 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
3665 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
3666 				buf[len++] = 0;
3667 				buf[len++] = 0; /* density code */
3668 				buf[len++] = 0;
3669 				buf[len++] = 0;
3670 				if (sdinfo->satadrv_type ==
3671 				    SATA_DTYPE_ATADISK)
3672 					buf[len++] = 2;
3673 				else
3674 					/* ATAPI */
3675 					buf[len++] = 8;
3676 				buf[len++] = 0;
3677 			}
3678 		}
3679 
3680 		sata_id = &sdinfo->satadrv_id;
3681 
3682 		/*
3683 		 * Add requested pages.
3684 		 * Page 3 and 4 are obsolete and we are not supporting them.
3685 		 * We deal now with:
3686 		 * caching (read/write cache control).
3687 		 * We should eventually deal with following mode pages:
3688 		 * error recovery  (0x01),
3689 		 * power condition (0x1a),
3690 		 * exception control page (enables SMART) (0x1c),
3691 		 * enclosure management (ses),
3692 		 * protocol-specific port mode (port control).
3693 		 */
3694 		switch (scsipkt->pkt_cdbp[2] & 0x3f) {
3695 		case MODEPAGE_RW_ERRRECOV:
3696 			/* DAD_MODE_ERR_RECOV */
3697 			/* R/W recovery */
3698 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
3699 			break;
3700 		case MODEPAGE_CACHING:
3701 			/* DAD_MODE_CACHE */
3702 			/* Reject not supported request for saved parameters */
3703 			if (pc == 3) {
3704 				*scsipkt->pkt_scbp = STATUS_CHECK;
3705 				sense = sata_arq_sense(spx);
3706 				sense->es_key = KEY_ILLEGAL_REQUEST;
3707 				sense->es_add_code =
3708 				    SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
3709 				goto done;
3710 			}
3711 
3712 			/* caching */
3713 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
3714 			break;
3715 		case MODEPAGE_INFO_EXCPT:
3716 			/* exception cntrl */
3717 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
3718 				len += sata_build_msense_page_1c(sdinfo, pc,
3719 				    buf+len);
3720 			}
3721 			else
3722 				goto err;
3723 			break;
3724 		case MODEPAGE_POWER_COND:
3725 			/* DAD_MODE_POWER_COND */
3726 			/* power condition */
3727 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
3728 			break;
3729 
3730 		case MODEPAGE_ACOUSTIC_MANAG:
3731 			/* acoustic management */
3732 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
3733 			break;
3734 		case MODEPAGE_ALLPAGES:
3735 			/* all pages */
3736 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
3737 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
3738 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
3739 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
3740 				len += sata_build_msense_page_1c(sdinfo, pc,
3741 				    buf+len);
3742 			}
3743 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
3744 			break;
3745 		default:
3746 		err:
3747 			/* Invalid request */
3748 			*scsipkt->pkt_scbp = STATUS_CHECK;
3749 			sense = sata_arq_sense(spx);
3750 			sense->es_key = KEY_ILLEGAL_REQUEST;
3751 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3752 			goto done;
3753 		}
3754 
3755 		/* fix total mode data length */
3756 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3757 			/* 4-byte mode parameter header */
3758 			buf[0] = len - 1;   	/* mode data length */
3759 		} else {
3760 			buf[0] = (len -2) >> 8;
3761 			buf[1] = (len -2) & 0xff;
3762 		}
3763 
3764 
3765 		/* Check allocation length */
3766 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3767 			alc_len = scsipkt->pkt_cdbp[4];
3768 		} else {
3769 			alc_len = scsipkt->pkt_cdbp[7];
3770 			alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
3771 		}
3772 		/*
3773 		 * We do not check for possible parameters truncation
3774 		 * (alc_len < len) assuming that the target driver works
3775 		 * correctly. Just avoiding overrun.
3776 		 * Copy no more than requested and possible, buffer-wise.
3777 		 */
3778 		count = MIN(alc_len, len);
3779 		count = MIN(bp->b_bcount, count);
3780 		bcopy(buf, bp->b_un.b_addr, count);
3781 
3782 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3783 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
3784 	}
3785 	*scsipkt->pkt_scbp = STATUS_GOOD;
3786 done:
3787 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3788 	(void) kmem_free(buf, 1024);
3789 
3790 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3791 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3792 
3793 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3794 	    scsipkt->pkt_comp != NULL)
3795 		/* scsi callback required */
3796 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3797 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3798 		    TQ_SLEEP) == NULL)
3799 			/* Scheduling the callback failed */
3800 			return (TRAN_BUSY);
3801 
3802 	return (TRAN_ACCEPT);
3803 }
3804 
3805 
3806 /*
3807  * SATA translate command: Mode Select.
3808  * Translated into appropriate SATA command or emulated.
3809  * Saving parameters is not supported.
3810  * Changing device capacity is not supported (although theoretically
3811  * possible by executing SET FEATURES/SET MAX ADDRESS)
3812  *
3813  * Assumption is that the target driver is working correctly.
3814  *
3815  * More than one SATA command may be executed to perform operations specified
3816  * by mode select pages. The first error terminates further execution.
3817  * Operations performed successully are not backed-up in such case.
3818  *
3819  * NOTE: only caching mode select page is implemented.
3820  * Caching setup is remembered so it could be re-stored in case of
3821  * an unexpected device reset.
3822  *
3823  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3824  */
3825 
3826 static int
3827 sata_txlt_mode_select(sata_pkt_txlate_t *spx)
3828 {
3829 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3830 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3831 	struct scsi_extended_sense *sense;
3832 	int len, pagelen, count, pllen;
3833 	uint8_t *buf;	/* mode select buffer */
3834 	int rval, stat, reason;
3835 	uint_t nointr_flag;
3836 	int dmod = 0;
3837 
3838 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3839 	    "sata_txlt_mode_select, pc %x page code 0x%02x\n",
3840 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
3841 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
3842 
3843 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3844 
3845 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
3846 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
3847 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3848 		return (rval);
3849 	}
3850 
3851 	rval = TRAN_ACCEPT;
3852 
3853 	scsipkt->pkt_reason = CMD_CMPLT;
3854 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3855 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3856 
3857 	/* Reject not supported request */
3858 	if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
3859 		*scsipkt->pkt_scbp = STATUS_CHECK;
3860 		sense = sata_arq_sense(spx);
3861 		sense->es_key = KEY_ILLEGAL_REQUEST;
3862 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3863 		goto done;
3864 	}
3865 
3866 	if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
3867 		pllen = scsipkt->pkt_cdbp[4];
3868 	} else {
3869 		pllen = scsipkt->pkt_cdbp[7];
3870 		pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
3871 	}
3872 
3873 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
3874 
3875 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
3876 		buf = (uint8_t *)bp->b_un.b_addr;
3877 		count = MIN(bp->b_bcount, pllen);
3878 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3879 		scsipkt->pkt_resid = 0;
3880 		pllen = count;
3881 
3882 		/*
3883 		 * Check the header to skip the block descriptor(s) - we
3884 		 * do not support setting device capacity.
3885 		 * Existing macros do not recognize long LBA dscriptor,
3886 		 * hence manual calculation.
3887 		 */
3888 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
3889 			/* 6-bytes CMD, 4 bytes header */
3890 			if (count <= 4)
3891 				goto done;		/* header only */
3892 			len = buf[3] + 4;
3893 		} else {
3894 			/* 10-bytes CMD, 8 bytes header */
3895 			if (count <= 8)
3896 				goto done;		/* header only */
3897 			len = buf[6];
3898 			len = (len << 8) + buf[7] + 8;
3899 		}
3900 		if (len >= count)
3901 			goto done;	/* header + descriptor(s) only */
3902 
3903 		pllen -= len;		/* remaining data length */
3904 
3905 		/*
3906 		 * We may be executing SATA command and want to execute it
3907 		 * in SYNCH mode, regardless of scsi_pkt setting.
3908 		 * Save scsi_pkt setting and indicate SYNCH mode
3909 		 */
3910 		nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
3911 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3912 		    scsipkt->pkt_comp != NULL) {
3913 			scsipkt->pkt_flags |= FLAG_NOINTR;
3914 		}
3915 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
3916 
3917 		/*
3918 		 * len is now the offset to a first mode select page
3919 		 * Process all pages
3920 		 */
3921 		while (pllen > 0) {
3922 			switch ((int)buf[len]) {
3923 			case MODEPAGE_CACHING:
3924 				/* No support for SP (saving) */
3925 				if (scsipkt->pkt_cdbp[1] & 0x01) {
3926 					*scsipkt->pkt_scbp = STATUS_CHECK;
3927 					sense = sata_arq_sense(spx);
3928 					sense->es_key = KEY_ILLEGAL_REQUEST;
3929 					sense->es_add_code =
3930 					    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3931 					goto done;
3932 				}
3933 				stat = sata_mode_select_page_8(spx,
3934 				    (struct mode_cache_scsi3 *)&buf[len],
3935 				    pllen, &pagelen, &rval, &dmod);
3936 				/*
3937 				 * The pagelen value indicates the number of
3938 				 * parameter bytes already processed.
3939 				 * The rval is the return value from
3940 				 * sata_tran_start().
3941 				 * The stat indicates the overall status of
3942 				 * the operation(s).
3943 				 */
3944 				if (stat != SATA_SUCCESS)
3945 					/*
3946 					 * Page processing did not succeed -
3947 					 * all error info is already set-up,
3948 					 * just return
3949 					 */
3950 					pllen = 0; /* this breaks the loop */
3951 				else {
3952 					len += pagelen;
3953 					pllen -= pagelen;
3954 				}
3955 				break;
3956 
3957 			case MODEPAGE_INFO_EXCPT:
3958 				stat = sata_mode_select_page_1c(spx,
3959 				    (struct mode_info_excpt_page *)&buf[len],
3960 				    pllen, &pagelen, &rval, &dmod);
3961 				/*
3962 				 * The pagelen value indicates the number of
3963 				 * parameter bytes already processed.
3964 				 * The rval is the return value from
3965 				 * sata_tran_start().
3966 				 * The stat indicates the overall status of
3967 				 * the operation(s).
3968 				 */
3969 				if (stat != SATA_SUCCESS)
3970 					/*
3971 					 * Page processing did not succeed -
3972 					 * all error info is already set-up,
3973 					 * just return
3974 					 */
3975 					pllen = 0; /* this breaks the loop */
3976 				else {
3977 					len += pagelen;
3978 					pllen -= pagelen;
3979 				}
3980 				break;
3981 
3982 			case MODEPAGE_ACOUSTIC_MANAG:
3983 				stat = sata_mode_select_page_30(spx,
3984 				    (struct mode_acoustic_management *)
3985 				    &buf[len], pllen, &pagelen, &rval, &dmod);
3986 				/*
3987 				 * The pagelen value indicates the number of
3988 				 * parameter bytes already processed.
3989 				 * The rval is the return value from
3990 				 * sata_tran_start().
3991 				 * The stat indicates the overall status of
3992 				 * the operation(s).
3993 				 */
3994 				if (stat != SATA_SUCCESS)
3995 					/*
3996 					 * Page processing did not succeed -
3997 					 * all error info is already set-up,
3998 					 * just return
3999 					 */
4000 					pllen = 0; /* this breaks the loop */
4001 				else {
4002 					len += pagelen;
4003 					pllen -= pagelen;
4004 				}
4005 
4006 				break;
4007 			default:
4008 				*scsipkt->pkt_scbp = STATUS_CHECK;
4009 				sense = sata_arq_sense(spx);
4010 				sense->es_key = KEY_ILLEGAL_REQUEST;
4011 				sense->es_add_code =
4012 				    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
4013 				goto done;
4014 			}
4015 		}
4016 	}
4017 done:
4018 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4019 	/*
4020 	 * If device parameters were modified, fetch and store the new
4021 	 * Identify Device data. Since port mutex could have been released
4022 	 * for accessing HBA driver, we need to re-check device existence.
4023 	 */
4024 	if (dmod != 0) {
4025 		sata_drive_info_t new_sdinfo, *sdinfo;
4026 		int rv = 0;
4027 
4028 		/*
4029 		 * Following statement has to be changed if this function is
4030 		 * used for devices other than SATA hard disks.
4031 		 */
4032 		new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
4033 
4034 		new_sdinfo.satadrv_addr =
4035 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr;
4036 		rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
4037 		    &new_sdinfo);
4038 
4039 		mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4040 		/*
4041 		 * Since port mutex could have been released when
4042 		 * accessing HBA driver, we need to re-check that the
4043 		 * framework still holds the device info structure.
4044 		 */
4045 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4046 		    &spx->txlt_sata_pkt->satapkt_device);
4047 		if (sdinfo != NULL) {
4048 			/*
4049 			 * Device still has info structure in the
4050 			 * sata framework. Copy newly fetched info
4051 			 */
4052 			if (rv == 0) {
4053 				sdinfo->satadrv_id = new_sdinfo.satadrv_id;
4054 				sata_save_drive_settings(sdinfo);
4055 			} else {
4056 				/*
4057 				 * Could not fetch new data - invalidate
4058 				 * sata_drive_info. That makes device
4059 				 * unusable.
4060 				 */
4061 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
4062 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
4063 			}
4064 		}
4065 		if (rv != 0 || sdinfo == NULL) {
4066 			/*
4067 			 * This changes the overall mode select completion
4068 			 * reason to a failed one !!!!!
4069 			 */
4070 			*scsipkt->pkt_scbp = STATUS_CHECK;
4071 			sense = sata_arq_sense(spx);
4072 			scsipkt->pkt_reason = CMD_INCOMPLETE;
4073 			rval = TRAN_ACCEPT;
4074 		}
4075 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4076 	}
4077 	/* Restore the scsi pkt flags */
4078 	scsipkt->pkt_flags &= ~FLAG_NOINTR;
4079 	scsipkt->pkt_flags |= nointr_flag;
4080 
4081 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4082 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4083 
4084 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4085 	    scsipkt->pkt_comp != NULL)
4086 		/* scsi callback required */
4087 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4088 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4089 		    TQ_SLEEP) == NULL)
4090 			/* Scheduling the callback failed */
4091 			return (TRAN_BUSY);
4092 
4093 	return (rval);
4094 }
4095 
4096 
4097 
4098 /*
4099  * Translate command: Log Sense
4100  */
4101 static 	int
4102 sata_txlt_log_sense(sata_pkt_txlate_t *spx)
4103 {
4104 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4105 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4106 	sata_drive_info_t *sdinfo;
4107 	struct scsi_extended_sense *sense;
4108 	int 		len, count, alc_len;
4109 	int		pc;	/* Page Control code */
4110 	int		page_code;	/* Page code */
4111 	uint8_t		*buf;	/* log sense buffer */
4112 	int		rval, reason;
4113 #define	MAX_LOG_SENSE_PAGE_SIZE	512
4114 
4115 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4116 	    "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
4117 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4118 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4119 
4120 	buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
4121 
4122 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4123 
4124 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4125 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4126 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4127 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4128 		return (rval);
4129 	}
4130 
4131 	scsipkt->pkt_reason = CMD_CMPLT;
4132 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4133 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4134 
4135 	pc = scsipkt->pkt_cdbp[2] >> 6;
4136 	page_code = scsipkt->pkt_cdbp[2] & 0x3f;
4137 
4138 	/* Reject not supported request for all but cumulative values */
4139 	switch (pc) {
4140 	case PC_CUMULATIVE_VALUES:
4141 		break;
4142 	default:
4143 		*scsipkt->pkt_scbp = STATUS_CHECK;
4144 		sense = sata_arq_sense(spx);
4145 		sense->es_key = KEY_ILLEGAL_REQUEST;
4146 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4147 		goto done;
4148 	}
4149 
4150 	switch (page_code) {
4151 	case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
4152 	case PAGE_CODE_SELF_TEST_RESULTS:
4153 	case PAGE_CODE_INFORMATION_EXCEPTIONS:
4154 	case PAGE_CODE_SMART_READ_DATA:
4155 		break;
4156 	default:
4157 		*scsipkt->pkt_scbp = STATUS_CHECK;
4158 		sense = sata_arq_sense(spx);
4159 		sense->es_key = KEY_ILLEGAL_REQUEST;
4160 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4161 		goto done;
4162 	}
4163 
4164 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4165 		/*
4166 		 * Because log sense uses local buffers for data retrieval from
4167 		 * the devices and sets the data programatically in the
4168 		 * original specified buffer, release preallocated DMA
4169 		 * resources before storing data in the original buffer,
4170 		 * so no unwanted DMA sync would take place.
4171 		 */
4172 		sata_id_t *sata_id;
4173 
4174 		sata_scsi_dmafree(NULL, scsipkt);
4175 
4176 		len = 0;
4177 
4178 		/* Build log parameter header */
4179 		buf[len++] = page_code;	/* page code as in the CDB */
4180 		buf[len++] = 0;		/* reserved */
4181 		buf[len++] = 0;		/* Zero out page length for now (MSB) */
4182 		buf[len++] = 0;		/* (LSB) */
4183 
4184 		sdinfo = sata_get_device_info(
4185 		    spx->txlt_sata_hba_inst,
4186 		    &spx->txlt_sata_pkt->satapkt_device);
4187 
4188 
4189 		/*
4190 		 * Add requested pages.
4191 		 */
4192 		switch (page_code) {
4193 		case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
4194 			len = sata_build_lsense_page_0(sdinfo, buf + len);
4195 			break;
4196 		case PAGE_CODE_SELF_TEST_RESULTS:
4197 			sata_id = &sdinfo->satadrv_id;
4198 			if ((! (sata_id->ai_cmdset84 &
4199 			    SATA_SMART_SELF_TEST_SUPPORTED)) ||
4200 			    (! (sata_id->ai_features87 &
4201 			    SATA_SMART_SELF_TEST_SUPPORTED))) {
4202 				*scsipkt->pkt_scbp = STATUS_CHECK;
4203 				sense = sata_arq_sense(spx);
4204 				sense->es_key = KEY_ILLEGAL_REQUEST;
4205 				sense->es_add_code =
4206 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4207 
4208 				goto done;
4209 			}
4210 			len = sata_build_lsense_page_10(sdinfo, buf + len,
4211 			    spx->txlt_sata_hba_inst);
4212 			break;
4213 		case PAGE_CODE_INFORMATION_EXCEPTIONS:
4214 			sata_id = &sdinfo->satadrv_id;
4215 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
4216 				*scsipkt->pkt_scbp = STATUS_CHECK;
4217 				sense = sata_arq_sense(spx);
4218 				sense->es_key = KEY_ILLEGAL_REQUEST;
4219 				sense->es_add_code =
4220 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4221 
4222 				goto done;
4223 			}
4224 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
4225 				*scsipkt->pkt_scbp = STATUS_CHECK;
4226 				sense = sata_arq_sense(spx);
4227 				sense->es_key = KEY_ABORTED_COMMAND;
4228 				sense->es_add_code =
4229 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
4230 				sense->es_qual_code =
4231 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
4232 
4233 				goto done;
4234 			}
4235 
4236 			len = sata_build_lsense_page_2f(sdinfo, buf + len,
4237 			    spx->txlt_sata_hba_inst);
4238 			break;
4239 		case PAGE_CODE_SMART_READ_DATA:
4240 			sata_id = &sdinfo->satadrv_id;
4241 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
4242 				*scsipkt->pkt_scbp = STATUS_CHECK;
4243 				sense = sata_arq_sense(spx);
4244 				sense->es_key = KEY_ILLEGAL_REQUEST;
4245 				sense->es_add_code =
4246 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4247 
4248 				goto done;
4249 			}
4250 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
4251 				*scsipkt->pkt_scbp = STATUS_CHECK;
4252 				sense = sata_arq_sense(spx);
4253 				sense->es_key = KEY_ABORTED_COMMAND;
4254 				sense->es_add_code =
4255 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
4256 				sense->es_qual_code =
4257 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
4258 
4259 				goto done;
4260 			}
4261 
4262 			/* This page doesn't include a page header */
4263 			len = sata_build_lsense_page_30(sdinfo, buf,
4264 			    spx->txlt_sata_hba_inst);
4265 			goto no_header;
4266 		default:
4267 			/* Invalid request */
4268 			*scsipkt->pkt_scbp = STATUS_CHECK;
4269 			sense = sata_arq_sense(spx);
4270 			sense->es_key = KEY_ILLEGAL_REQUEST;
4271 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4272 			goto done;
4273 		}
4274 
4275 		/* set parameter log sense data length */
4276 		buf[2] = len >> 8;	/* log sense length (MSB) */
4277 		buf[3] = len & 0xff;	/* log sense length (LSB) */
4278 
4279 		len += SCSI_LOG_PAGE_HDR_LEN;
4280 		ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
4281 
4282 no_header:
4283 		/* Check allocation length */
4284 		alc_len = scsipkt->pkt_cdbp[7];
4285 		alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
4286 
4287 		/*
4288 		 * We do not check for possible parameters truncation
4289 		 * (alc_len < len) assuming that the target driver works
4290 		 * correctly. Just avoiding overrun.
4291 		 * Copy no more than requested and possible, buffer-wise.
4292 		 */
4293 		count = MIN(alc_len, len);
4294 		count = MIN(bp->b_bcount, count);
4295 		bcopy(buf, bp->b_un.b_addr, count);
4296 
4297 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4298 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
4299 	}
4300 	*scsipkt->pkt_scbp = STATUS_GOOD;
4301 done:
4302 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4303 	(void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4304 
4305 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4306 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4307 
4308 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4309 	    scsipkt->pkt_comp != NULL)
4310 		/* scsi callback required */
4311 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4312 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4313 		    TQ_SLEEP) == NULL)
4314 			/* Scheduling the callback failed */
4315 			return (TRAN_BUSY);
4316 
4317 	return (TRAN_ACCEPT);
4318 }
4319 
4320 /*
4321  * Translate command: Log Select
4322  * Not implemented at this time - returns invalid command response.
4323  */
4324 static 	int
4325 sata_txlt_log_select(sata_pkt_txlate_t *spx)
4326 {
4327 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4328 	    "sata_txlt_log_select\n", NULL);
4329 
4330 	return (sata_txlt_invalid_command(spx));
4331 }
4332 
4333 
4334 /*
4335  * Translate command: Read (various types).
4336  * Translated into appropriate type of ATA READ command
4337  * for SATA hard disks.
4338  * Both the device capabilities and requested operation mode are
4339  * considered.
4340  *
4341  * Following scsi cdb fields are ignored:
4342  * rdprotect, dpo, fua, fua_nv, group_number.
4343  *
4344  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
4345  * enable variable sata_func_enable), the capability of the controller and
4346  * capability of a device are checked and if both support queueing, read
4347  * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
4348  * command rather than plain READ_XXX command.
4349  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
4350  * both the controller and device suport such functionality, the read
4351  * request will be translated to READ_FPDMA_QUEUED command.
4352  * In both cases the maximum queue depth is derived as minimum of:
4353  * HBA capability,device capability and sata_max_queue_depth variable setting.
4354  * The value passed to HBA driver is decremented by 1, because only 5 bits are
4355  * used to pass max queue depth value, and the maximum possible queue depth
4356  * is 32.
4357  *
4358  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4359  * appropriate values in scsi_pkt fields.
4360  */
4361 static int
4362 sata_txlt_read(sata_pkt_txlate_t *spx)
4363 {
4364 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4365 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4366 	sata_drive_info_t *sdinfo;
4367 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4368 	int cport = SATA_TXLT_CPORT(spx);
4369 	uint16_t sec_count;
4370 	uint64_t lba;
4371 	int rval, reason;
4372 	int synch;
4373 
4374 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4375 
4376 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4377 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4378 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4379 		return (rval);
4380 	}
4381 
4382 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4383 	    &spx->txlt_sata_pkt->satapkt_device);
4384 
4385 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
4386 	/*
4387 	 * Extract LBA and sector count from scsi CDB.
4388 	 */
4389 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
4390 	case SCMD_READ:
4391 		/* 6-byte scsi read cmd : 0x08 */
4392 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
4393 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
4394 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4395 		sec_count = scsipkt->pkt_cdbp[4];
4396 		/* sec_count 0 will be interpreted as 256 by a device */
4397 		break;
4398 	case SCMD_READ_G1:
4399 		/* 10-bytes scsi read command : 0x28 */
4400 		lba = scsipkt->pkt_cdbp[2];
4401 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4402 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4403 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4404 		sec_count = scsipkt->pkt_cdbp[7];
4405 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4406 		break;
4407 	case SCMD_READ_G5:
4408 		/* 12-bytes scsi read command : 0xA8 */
4409 		lba = scsipkt->pkt_cdbp[2];
4410 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4411 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4412 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4413 		sec_count = scsipkt->pkt_cdbp[6];
4414 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
4415 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4416 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
4417 		break;
4418 	case SCMD_READ_G4:
4419 		/* 16-bytes scsi read command : 0x88 */
4420 		lba = scsipkt->pkt_cdbp[2];
4421 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4422 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4423 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4424 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
4425 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
4426 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
4427 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
4428 		sec_count = scsipkt->pkt_cdbp[10];
4429 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
4430 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
4431 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
4432 		break;
4433 	default:
4434 		/* Unsupported command */
4435 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4436 		return (sata_txlt_invalid_command(spx));
4437 	}
4438 
4439 	/*
4440 	 * Check if specified address exceeds device capacity
4441 	 */
4442 	if ((lba >= sdinfo->satadrv_capacity) ||
4443 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
4444 		/* LBA out of range */
4445 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4446 		return (sata_txlt_lba_out_of_range(spx));
4447 	}
4448 
4449 	/*
4450 	 * For zero-length transfer, emulate good completion of the command
4451 	 * (reasons for rejecting the command were already checked).
4452 	 * No DMA resources were allocated.
4453 	 */
4454 	if (spx->txlt_dma_cookie_list == NULL) {
4455 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4456 		return (sata_emul_rw_completion(spx));
4457 	}
4458 
4459 	/*
4460 	 * Build cmd block depending on the device capability and
4461 	 * requested operation mode.
4462 	 * Do not bother with non-dma mode - we are working only with
4463 	 * devices supporting DMA.
4464 	 */
4465 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
4466 	scmd->satacmd_device_reg = SATA_ADH_LBA;
4467 	scmd->satacmd_cmd_reg = SATAC_READ_DMA;
4468 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
4469 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4470 		scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT;
4471 		scmd->satacmd_sec_count_msb = sec_count >> 8;
4472 #ifndef __lock_lint
4473 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
4474 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
4475 		scmd->satacmd_lba_high_msb = lba >> 40;
4476 #endif
4477 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
4478 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
4479 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
4480 	}
4481 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
4482 	scmd->satacmd_lba_low_lsb = lba & 0xff;
4483 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
4484 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
4485 	scmd->satacmd_features_reg = 0;
4486 	scmd->satacmd_status_reg = 0;
4487 	scmd->satacmd_error_reg = 0;
4488 
4489 	/*
4490 	 * Check if queueing commands should be used and switch
4491 	 * to appropriate command if possible
4492 	 */
4493 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
4494 		boolean_t using_queuing;
4495 
4496 		/* Queuing supported by controller and device? */
4497 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
4498 		    (sdinfo->satadrv_features_support &
4499 		    SATA_DEV_F_NCQ) &&
4500 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4501 		    SATA_CTLF_NCQ)) {
4502 			using_queuing = B_TRUE;
4503 
4504 			/* NCQ supported - use FPDMA READ */
4505 			scmd->satacmd_cmd_reg =
4506 			    SATAC_READ_FPDMA_QUEUED;
4507 			scmd->satacmd_features_reg_ext =
4508 			    scmd->satacmd_sec_count_msb;
4509 			scmd->satacmd_sec_count_msb = 0;
4510 		} else if ((sdinfo->satadrv_features_support &
4511 		    SATA_DEV_F_TCQ) &&
4512 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4513 		    SATA_CTLF_QCMD)) {
4514 			using_queuing = B_TRUE;
4515 
4516 			/* Legacy queueing */
4517 			if (sdinfo->satadrv_features_support &
4518 			    SATA_DEV_F_LBA48) {
4519 				scmd->satacmd_cmd_reg =
4520 				    SATAC_READ_DMA_QUEUED_EXT;
4521 				scmd->satacmd_features_reg_ext =
4522 				    scmd->satacmd_sec_count_msb;
4523 				scmd->satacmd_sec_count_msb = 0;
4524 			} else {
4525 				scmd->satacmd_cmd_reg =
4526 				    SATAC_READ_DMA_QUEUED;
4527 			}
4528 		} else	/* NCQ nor legacy queuing not supported */
4529 			using_queuing = B_FALSE;
4530 
4531 		/*
4532 		 * If queuing, the sector count goes in the features register
4533 		 * and the secount count will contain the tag.
4534 		 */
4535 		if (using_queuing) {
4536 			scmd->satacmd_features_reg =
4537 			    scmd->satacmd_sec_count_lsb;
4538 			scmd->satacmd_sec_count_lsb = 0;
4539 			scmd->satacmd_flags.sata_queued = B_TRUE;
4540 
4541 			/* Set-up maximum queue depth */
4542 			scmd->satacmd_flags.sata_max_queue_depth =
4543 			    sdinfo->satadrv_max_queue_depth - 1;
4544 		} else if (sdinfo->satadrv_features_enabled &
4545 		    SATA_DEV_F_E_UNTAGGED_QING) {
4546 			/*
4547 			 * Although NCQ/TCQ is not enabled, untagged queuing
4548 			 * may be still used.
4549 			 * Set-up the maximum untagged queue depth.
4550 			 * Use controller's queue depth from sata_hba_tran.
4551 			 * SATA HBA drivers may ignore this value and rely on
4552 			 * the internal limits.For drivers that do not
4553 			 * ignore untaged queue depth, limit the value to
4554 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
4555 			 * largest value that can be passed via
4556 			 * satacmd_flags.sata_max_queue_depth.
4557 			 */
4558 			scmd->satacmd_flags.sata_max_queue_depth =
4559 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
4560 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
4561 
4562 		} else {
4563 			scmd->satacmd_flags.sata_max_queue_depth = 0;
4564 		}
4565 	} else
4566 		scmd->satacmd_flags.sata_max_queue_depth = 0;
4567 
4568 	SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst,
4569 	    "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n",
4570 	    scmd->satacmd_cmd_reg, lba, sec_count);
4571 
4572 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4573 		/* Need callback function */
4574 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
4575 		synch = FALSE;
4576 	} else
4577 		synch = TRUE;
4578 
4579 	/* Transfer command to HBA */
4580 	if (sata_hba_start(spx, &rval) != 0) {
4581 		/* Pkt not accepted for execution */
4582 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4583 		return (rval);
4584 	}
4585 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4586 	/*
4587 	 * If execution is non-synchronous,
4588 	 * a callback function will handle potential errors, translate
4589 	 * the response and will do a callback to a target driver.
4590 	 * If it was synchronous, check execution status using the same
4591 	 * framework callback.
4592 	 */
4593 	if (synch) {
4594 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4595 		    "synchronous execution status %x\n",
4596 		    spx->txlt_sata_pkt->satapkt_reason);
4597 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
4598 	}
4599 	return (TRAN_ACCEPT);
4600 }
4601 
4602 
4603 /*
4604  * SATA translate command: Write (various types)
4605  * Translated into appropriate type of ATA WRITE command
4606  * for SATA hard disks.
4607  * Both the device capabilities and requested operation mode are
4608  * considered.
4609  *
4610  * Following scsi cdb fields are ignored:
4611  * rwprotect, dpo, fua, fua_nv, group_number.
4612  *
4613  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
4614  * enable variable sata_func_enable), the capability of the controller and
4615  * capability of a device are checked and if both support queueing, write
4616  * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT
4617  * command rather than plain WRITE_XXX command.
4618  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
4619  * both the controller and device suport such functionality, the write
4620  * request will be translated to WRITE_FPDMA_QUEUED command.
4621  * In both cases the maximum queue depth is derived as minimum of:
4622  * HBA capability,device capability and sata_max_queue_depth variable setting.
4623  * The value passed to HBA driver is decremented by 1, because only 5 bits are
4624  * used to pass max queue depth value, and the maximum possible queue depth
4625  * is 32.
4626  *
4627  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4628  * appropriate values in scsi_pkt fields.
4629  */
4630 static int
4631 sata_txlt_write(sata_pkt_txlate_t *spx)
4632 {
4633 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4634 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4635 	sata_drive_info_t *sdinfo;
4636 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4637 	int cport = SATA_TXLT_CPORT(spx);
4638 	uint16_t sec_count;
4639 	uint64_t lba;
4640 	int rval, reason;
4641 	int synch;
4642 
4643 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4644 
4645 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
4646 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
4647 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4648 		return (rval);
4649 	}
4650 
4651 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4652 	    &spx->txlt_sata_pkt->satapkt_device);
4653 
4654 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4655 	/*
4656 	 * Extract LBA and sector count from scsi CDB
4657 	 */
4658 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
4659 	case SCMD_WRITE:
4660 		/* 6-byte scsi read cmd : 0x0A */
4661 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
4662 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
4663 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4664 		sec_count = scsipkt->pkt_cdbp[4];
4665 		/* sec_count 0 will be interpreted as 256 by a device */
4666 		break;
4667 	case SCMD_WRITE_G1:
4668 		/* 10-bytes scsi write command : 0x2A */
4669 		lba = scsipkt->pkt_cdbp[2];
4670 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4671 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4672 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4673 		sec_count = scsipkt->pkt_cdbp[7];
4674 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4675 		break;
4676 	case SCMD_WRITE_G5:
4677 		/* 12-bytes scsi read command : 0xAA */
4678 		lba = scsipkt->pkt_cdbp[2];
4679 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4680 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4681 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4682 		sec_count = scsipkt->pkt_cdbp[6];
4683 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
4684 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4685 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
4686 		break;
4687 	case SCMD_WRITE_G4:
4688 		/* 16-bytes scsi write command : 0x8A */
4689 		lba = scsipkt->pkt_cdbp[2];
4690 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4691 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4692 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4693 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
4694 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
4695 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
4696 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
4697 		sec_count = scsipkt->pkt_cdbp[10];
4698 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
4699 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
4700 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
4701 		break;
4702 	default:
4703 		/* Unsupported command */
4704 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4705 		return (sata_txlt_invalid_command(spx));
4706 	}
4707 
4708 	/*
4709 	 * Check if specified address and length exceeds device capacity
4710 	 */
4711 	if ((lba >= sdinfo->satadrv_capacity) ||
4712 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
4713 		/* LBA out of range */
4714 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4715 		return (sata_txlt_lba_out_of_range(spx));
4716 	}
4717 
4718 	/*
4719 	 * For zero-length transfer, emulate good completion of the command
4720 	 * (reasons for rejecting the command were already checked).
4721 	 * No DMA resources were allocated.
4722 	 */
4723 	if (spx->txlt_dma_cookie_list == NULL) {
4724 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4725 		return (sata_emul_rw_completion(spx));
4726 	}
4727 
4728 	/*
4729 	 * Build cmd block depending on the device capability and
4730 	 * requested operation mode.
4731 	 * Do not bother with non-dma mode- we are working only with
4732 	 * devices supporting DMA.
4733 	 */
4734 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
4735 	scmd->satacmd_device_reg = SATA_ADH_LBA;
4736 	scmd->satacmd_cmd_reg = SATAC_WRITE_DMA;
4737 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
4738 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4739 		scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT;
4740 		scmd->satacmd_sec_count_msb = sec_count >> 8;
4741 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
4742 #ifndef __lock_lint
4743 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
4744 		scmd->satacmd_lba_high_msb = lba >> 40;
4745 #endif
4746 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
4747 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
4748 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
4749 	}
4750 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
4751 	scmd->satacmd_lba_low_lsb = lba & 0xff;
4752 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
4753 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
4754 	scmd->satacmd_features_reg = 0;
4755 	scmd->satacmd_status_reg = 0;
4756 	scmd->satacmd_error_reg = 0;
4757 
4758 	/*
4759 	 * Check if queueing commands should be used and switch
4760 	 * to appropriate command if possible
4761 	 */
4762 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
4763 		boolean_t using_queuing;
4764 
4765 		/* Queuing supported by controller and device? */
4766 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
4767 		    (sdinfo->satadrv_features_support &
4768 		    SATA_DEV_F_NCQ) &&
4769 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4770 		    SATA_CTLF_NCQ)) {
4771 			using_queuing = B_TRUE;
4772 
4773 			/* NCQ supported - use FPDMA WRITE */
4774 			scmd->satacmd_cmd_reg =
4775 			    SATAC_WRITE_FPDMA_QUEUED;
4776 			scmd->satacmd_features_reg_ext =
4777 			    scmd->satacmd_sec_count_msb;
4778 			scmd->satacmd_sec_count_msb = 0;
4779 		} else if ((sdinfo->satadrv_features_support &
4780 		    SATA_DEV_F_TCQ) &&
4781 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4782 		    SATA_CTLF_QCMD)) {
4783 			using_queuing = B_TRUE;
4784 
4785 			/* Legacy queueing */
4786 			if (sdinfo->satadrv_features_support &
4787 			    SATA_DEV_F_LBA48) {
4788 				scmd->satacmd_cmd_reg =
4789 				    SATAC_WRITE_DMA_QUEUED_EXT;
4790 				scmd->satacmd_features_reg_ext =
4791 				    scmd->satacmd_sec_count_msb;
4792 				scmd->satacmd_sec_count_msb = 0;
4793 			} else {
4794 				scmd->satacmd_cmd_reg =
4795 				    SATAC_WRITE_DMA_QUEUED;
4796 			}
4797 		} else	/*  NCQ nor legacy queuing not supported */
4798 			using_queuing = B_FALSE;
4799 
4800 		if (using_queuing) {
4801 			scmd->satacmd_features_reg =
4802 			    scmd->satacmd_sec_count_lsb;
4803 			scmd->satacmd_sec_count_lsb = 0;
4804 			scmd->satacmd_flags.sata_queued = B_TRUE;
4805 			/* Set-up maximum queue depth */
4806 			scmd->satacmd_flags.sata_max_queue_depth =
4807 			    sdinfo->satadrv_max_queue_depth - 1;
4808 		} else if (sdinfo->satadrv_features_enabled &
4809 		    SATA_DEV_F_E_UNTAGGED_QING) {
4810 			/*
4811 			 * Although NCQ/TCQ is not enabled, untagged queuing
4812 			 * may be still used.
4813 			 * Set-up the maximum untagged queue depth.
4814 			 * Use controller's queue depth from sata_hba_tran.
4815 			 * SATA HBA drivers may ignore this value and rely on
4816 			 * the internal limits. For drivera that do not
4817 			 * ignore untaged queue depth, limit the value to
4818 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
4819 			 * largest value that can be passed via
4820 			 * satacmd_flags.sata_max_queue_depth.
4821 			 */
4822 			scmd->satacmd_flags.sata_max_queue_depth =
4823 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
4824 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
4825 
4826 		} else {
4827 			scmd->satacmd_flags.sata_max_queue_depth = 0;
4828 		}
4829 	} else
4830 		scmd->satacmd_flags.sata_max_queue_depth = 0;
4831 
4832 	SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4833 	    "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n",
4834 	    scmd->satacmd_cmd_reg, lba, sec_count);
4835 
4836 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4837 		/* Need callback function */
4838 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
4839 		synch = FALSE;
4840 	} else
4841 		synch = TRUE;
4842 
4843 	/* Transfer command to HBA */
4844 	if (sata_hba_start(spx, &rval) != 0) {
4845 		/* Pkt not accepted for execution */
4846 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4847 		return (rval);
4848 	}
4849 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4850 
4851 	/*
4852 	 * If execution is non-synchronous,
4853 	 * a callback function will handle potential errors, translate
4854 	 * the response and will do a callback to a target driver.
4855 	 * If it was synchronous, check execution status using the same
4856 	 * framework callback.
4857 	 */
4858 	if (synch) {
4859 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4860 		    "synchronous execution status %x\n",
4861 		    spx->txlt_sata_pkt->satapkt_reason);
4862 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
4863 	}
4864 	return (TRAN_ACCEPT);
4865 }
4866 
4867 
4868 /*
4869  * Implements SCSI SBC WRITE BUFFER command download microcode option
4870  */
4871 static int
4872 sata_txlt_write_buffer(sata_pkt_txlate_t *spx)
4873 {
4874 #define	WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE			4
4875 #define	WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE			5
4876 
4877 	sata_hba_inst_t *sata_hba_inst = SATA_TXLT_HBA_INST(spx);
4878 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4879 	struct sata_pkt *sata_pkt = spx->txlt_sata_pkt;
4880 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4881 
4882 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4883 	struct scsi_extended_sense *sense;
4884 	int rval, mode, sector_count, reason;
4885 	int cport = SATA_TXLT_CPORT(spx);
4886 
4887 	mode = scsipkt->pkt_cdbp[1] & 0x1f;
4888 
4889 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4890 	    "sata_txlt_write_buffer, mode 0x%x\n", mode);
4891 
4892 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4893 
4894 	if ((rval = sata_txlt_generic_pkt_info(spx, &reason)) != TRAN_ACCEPT) {
4895 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4896 		return (rval);
4897 	}
4898 
4899 	/* Use synchronous mode */
4900 	spx->txlt_sata_pkt->satapkt_op_mode
4901 	    |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
4902 
4903 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4904 
4905 	scsipkt->pkt_reason = CMD_CMPLT;
4906 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4907 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4908 
4909 	/*
4910 	 * The SCSI to ATA translation specification only calls
4911 	 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE.
4912 	 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but
4913 	 * ATA 8 (draft) got rid of download microcode for temp
4914 	 * and it is even optional for ATA 7, so it may be aborted.
4915 	 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as
4916 	 * it is not specified and the buffer offset for SCSI is a 16-bit
4917 	 * value in bytes, but for ATA it is a 16-bit offset in 512 byte
4918 	 * sectors.  Thus the offset really doesn't buy us anything.
4919 	 * If and when ATA 8 is stabilized and the SCSI to ATA specification
4920 	 * is revised, this can be revisisted.
4921 	 */
4922 	/* Reject not supported request */
4923 	switch (mode) {
4924 	case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE:
4925 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP;
4926 		break;
4927 	case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE:
4928 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE;
4929 		break;
4930 	default:
4931 		goto bad_param;
4932 	}
4933 
4934 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
4935 
4936 	scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE;
4937 	if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0)
4938 		goto bad_param;
4939 	sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE;
4940 	scmd->satacmd_sec_count_lsb = (uint8_t)sector_count;
4941 	scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8;
4942 	scmd->satacmd_lba_mid_lsb = 0;
4943 	scmd->satacmd_lba_high_lsb = 0;
4944 	scmd->satacmd_device_reg = 0;
4945 	spx->txlt_sata_pkt->satapkt_comp = NULL;
4946 	scmd->satacmd_addr_type = 0;
4947 
4948 	/* Transfer command to HBA */
4949 	if (sata_hba_start(spx, &rval) != 0) {
4950 		/* Pkt not accepted for execution */
4951 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
4952 		return (rval);
4953 	}
4954 
4955 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
4956 
4957 	/* Then we need synchronous check the status of the disk */
4958 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4959 	    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
4960 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
4961 		scsipkt->pkt_reason = CMD_CMPLT;
4962 
4963 		/* Download commmand succeed, so probe and identify device */
4964 		sata_reidentify_device(spx);
4965 	} else {
4966 		/* Something went wrong, microcode download command failed */
4967 		scsipkt->pkt_reason = CMD_INCOMPLETE;
4968 		*scsipkt->pkt_scbp = STATUS_CHECK;
4969 		sense = sata_arq_sense(spx);
4970 		switch (sata_pkt->satapkt_reason) {
4971 		case SATA_PKT_PORT_ERROR:
4972 			/*
4973 			 * We have no device data. Assume no data transfered.
4974 			 */
4975 			sense->es_key = KEY_HARDWARE_ERROR;
4976 			break;
4977 
4978 		case SATA_PKT_DEV_ERROR:
4979 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
4980 			    SATA_STATUS_ERR) {
4981 				/*
4982 				 * determine dev error reason from error
4983 				 * reg content
4984 				 */
4985 				sata_decode_device_error(spx, sense);
4986 				break;
4987 			}
4988 			/* No extended sense key - no info available */
4989 			break;
4990 
4991 		case SATA_PKT_TIMEOUT:
4992 			scsipkt->pkt_reason = CMD_TIMEOUT;
4993 			scsipkt->pkt_statistics |=
4994 			    STAT_TIMEOUT | STAT_DEV_RESET;
4995 			/* No extended sense key ? */
4996 			break;
4997 
4998 		case SATA_PKT_ABORTED:
4999 			scsipkt->pkt_reason = CMD_ABORTED;
5000 			scsipkt->pkt_statistics |= STAT_ABORTED;
5001 			/* No extended sense key ? */
5002 			break;
5003 
5004 		case SATA_PKT_RESET:
5005 			/* pkt aborted by an explicit reset from a host */
5006 			scsipkt->pkt_reason = CMD_RESET;
5007 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5008 			break;
5009 
5010 		default:
5011 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5012 			    "sata_txlt_nodata_cmd_completion: "
5013 			    "invalid packet completion reason %d",
5014 			    sata_pkt->satapkt_reason));
5015 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5016 			break;
5017 		}
5018 
5019 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5020 		    "scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5021 
5022 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5023 		    scsipkt->pkt_comp != NULL)
5024 			/* scsi callback required */
5025 			(*scsipkt->pkt_comp)(scsipkt);
5026 	}
5027 	return (TRAN_ACCEPT);
5028 
5029 bad_param:
5030 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5031 	*scsipkt->pkt_scbp = STATUS_CHECK;
5032 	sense = sata_arq_sense(spx);
5033 	sense->es_key = KEY_ILLEGAL_REQUEST;
5034 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5035 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5036 	    scsipkt->pkt_comp != NULL) {
5037 		/* scsi callback required */
5038 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5039 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5040 		    TQ_SLEEP) == 0) {
5041 			/* Scheduling the callback failed */
5042 			rval = TRAN_BUSY;
5043 		}
5044 	}
5045 	return (rval);
5046 }
5047 
5048 /*
5049  * Re-identify device after doing a firmware download.
5050  */
5051 static void
5052 sata_reidentify_device(sata_pkt_txlate_t *spx)
5053 {
5054 #define	DOWNLOAD_WAIT_TIME_SECS	60
5055 #define	DOWNLOAD_WAIT_INTERVAL_SECS	1
5056 	int rval;
5057 	int retry_cnt;
5058 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5059 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
5060 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
5061 	sata_drive_info_t *sdinfo;
5062 
5063 	/*
5064 	 * Before returning good status, probe device.
5065 	 * Device probing will get IDENTIFY DEVICE data, if possible.
5066 	 * The assumption is that the new microcode is applied by the
5067 	 * device. It is a caller responsibility to verify this.
5068 	 */
5069 	for (retry_cnt = 0;
5070 	    retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS;
5071 	    retry_cnt++) {
5072 		rval = sata_probe_device(sata_hba_inst, &sata_device);
5073 
5074 		if (rval == SATA_SUCCESS) { /* Set default features */
5075 			sdinfo = sata_get_device_info(sata_hba_inst,
5076 			    &sata_device);
5077 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
5078 			    SATA_SUCCESS) {
5079 				/* retry */
5080 				(void) sata_initialize_device(sata_hba_inst,
5081 				    sdinfo);
5082 			}
5083 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5084 			    scsipkt->pkt_comp != NULL)
5085 				(*scsipkt->pkt_comp)(scsipkt);
5086 			return;
5087 		} else if (rval == SATA_RETRY) {
5088 			delay(drv_usectohz(1000000 *
5089 			    DOWNLOAD_WAIT_INTERVAL_SECS));
5090 			continue;
5091 		} else	/* failed - no reason to retry */
5092 			break;
5093 	}
5094 
5095 	/*
5096 	 * Something went wrong, device probing failed.
5097 	 */
5098 	SATA_LOG_D((sata_hba_inst, CE_WARN,
5099 	    "Cannot probe device after downloading microcode\n"));
5100 
5101 	/* Reset device to force retrying the probe. */
5102 	(void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst))
5103 	    (SATA_DIP(sata_hba_inst), &sata_device);
5104 
5105 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5106 	    scsipkt->pkt_comp != NULL)
5107 		(*scsipkt->pkt_comp)(scsipkt);
5108 }
5109 
5110 
5111 /*
5112  * Translate command: Synchronize Cache.
5113  * Translates into Flush Cache command for SATA hard disks.
5114  *
5115  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
5116  * appropriate values in scsi_pkt fields.
5117  */
5118 static 	int
5119 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx)
5120 {
5121 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5122 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
5123 	int cport = SATA_TXLT_CPORT(spx);
5124 	int rval, reason;
5125 	int synch;
5126 
5127 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5128 
5129 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
5130 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
5131 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5132 		return (rval);
5133 	}
5134 
5135 	scmd->satacmd_addr_type = 0;
5136 	scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE;
5137 	scmd->satacmd_device_reg = 0;
5138 	scmd->satacmd_sec_count_lsb = 0;
5139 	scmd->satacmd_lba_low_lsb = 0;
5140 	scmd->satacmd_lba_mid_lsb = 0;
5141 	scmd->satacmd_lba_high_lsb = 0;
5142 	scmd->satacmd_features_reg = 0;
5143 	scmd->satacmd_status_reg = 0;
5144 	scmd->satacmd_error_reg = 0;
5145 
5146 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5147 	    "sata_txlt_synchronize_cache\n", NULL);
5148 
5149 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5150 		/* Need to set-up a callback function */
5151 		spx->txlt_sata_pkt->satapkt_comp =
5152 		    sata_txlt_nodata_cmd_completion;
5153 		synch = FALSE;
5154 	} else
5155 		synch = TRUE;
5156 
5157 	/* Transfer command to HBA */
5158 	if (sata_hba_start(spx, &rval) != 0) {
5159 		/* Pkt not accepted for execution */
5160 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5161 		return (rval);
5162 	}
5163 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5164 
5165 	/*
5166 	 * If execution non-synchronous, it had to be completed
5167 	 * a callback function will handle potential errors, translate
5168 	 * the response and will do a callback to a target driver.
5169 	 * If it was synchronous, check status, using the same
5170 	 * framework callback.
5171 	 */
5172 	if (synch) {
5173 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5174 		    "synchronous execution status %x\n",
5175 		    spx->txlt_sata_pkt->satapkt_reason);
5176 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
5177 	}
5178 	return (TRAN_ACCEPT);
5179 }
5180 
5181 
5182 /*
5183  * Send pkt to SATA HBA driver
5184  *
5185  * This function may be called only if the operation is requested by scsi_pkt,
5186  * i.e. scsi_pkt is not NULL.
5187  *
5188  * This function has to be called with cport mutex held. It does release
5189  * the mutex when it calls HBA driver sata_tran_start function and
5190  * re-acquires it afterwards.
5191  *
5192  * If return value is 0, pkt was accepted, -1 otherwise
5193  * rval is set to appropriate sata_scsi_start return value.
5194  *
5195  * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not
5196  * have called the sata_pkt callback function for this packet.
5197  *
5198  * The scsi callback has to be performed by the caller of this routine.
5199  *
5200  * Note 2: No port multiplier support for now.
5201  */
5202 static int
5203 sata_hba_start(sata_pkt_txlate_t *spx, int *rval)
5204 {
5205 	int stat, cport;
5206 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
5207 	sata_drive_info_t *sdinfo;
5208 	sata_device_t *sata_device;
5209 	uint8_t cmd;
5210 	struct sata_cmd_flags cmd_flags;
5211 
5212 	ASSERT(spx->txlt_sata_pkt != NULL);
5213 
5214 	cport = SATA_TXLT_CPORT(spx);
5215 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5216 
5217 	sdinfo = sata_get_device_info(sata_hba_inst,
5218 	    &spx->txlt_sata_pkt->satapkt_device);
5219 	ASSERT(sdinfo != NULL);
5220 
5221 	/* Clear device reset state? */
5222 	if (sdinfo->satadrv_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) {
5223 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
5224 		    sata_clear_dev_reset = B_TRUE;
5225 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_CLEAR_DEVICE_RESET;
5226 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5227 		    "sata_hba_start: clearing device reset state\n", NULL);
5228 	}
5229 	cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg;
5230 	cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags;
5231 	sata_device = &spx->txlt_sata_pkt->satapkt_device;
5232 
5233 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5234 
5235 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5236 	    "Sata cmd 0x%2x\n", cmd);
5237 
5238 	stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
5239 	    spx->txlt_sata_pkt);
5240 
5241 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5242 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
5243 	/*
5244 	 * If sata pkt was accepted and executed in asynchronous mode, i.e.
5245 	 * with the sata callback, the sata_pkt could be already destroyed
5246 	 * by the time we check ther return status from the hba_start()
5247 	 * function, because sata_scsi_destroy_pkt() could have been already
5248 	 * called (perhaps in the interrupt context). So, in such case, there
5249 	 * should be no references to it. In other cases, sata_pkt still
5250 	 * exists.
5251 	 */
5252 	switch (stat) {
5253 	case SATA_TRAN_ACCEPTED:
5254 		/*
5255 		 * pkt accepted for execution.
5256 		 * If it was executed synchronously, it is already completed
5257 		 * and pkt completion_reason indicates completion status.
5258 		 */
5259 		*rval = TRAN_ACCEPT;
5260 		return (0);
5261 
5262 	case SATA_TRAN_QUEUE_FULL:
5263 		/*
5264 		 * Controller detected queue full condition.
5265 		 */
5266 		SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst,
5267 		    "sata_hba_start: queue full\n", NULL);
5268 
5269 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
5270 		*spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL;
5271 
5272 		*rval = TRAN_BUSY;
5273 		break;
5274 
5275 	case SATA_TRAN_PORT_ERROR:
5276 		/*
5277 		 * Communication/link with device or general port error
5278 		 * detected before pkt execution begun.
5279 		 */
5280 		if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
5281 		    SATA_ADDR_CPORT ||
5282 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
5283 		    SATA_ADDR_DCPORT)
5284 			sata_log(sata_hba_inst, CE_CONT,
5285 			    "SATA port %d error",
5286 			    sata_device->satadev_addr.cport);
5287 		else
5288 			sata_log(sata_hba_inst, CE_CONT,
5289 			    "SATA port %d pmport %d error\n",
5290 			    sata_device->satadev_addr.cport,
5291 			    sata_device->satadev_addr.pmport);
5292 
5293 		/*
5294 		 * Update the port/device structure.
5295 		 * sata_pkt should be still valid. Since port error is
5296 		 * returned, sata_device content should reflect port
5297 		 * state - it means, that sata address have been changed,
5298 		 * because original packet's sata address refered to a device
5299 		 * attached to some port.
5300 		 */
5301 		sata_update_port_info(sata_hba_inst, sata_device);
5302 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
5303 		*rval = TRAN_FATAL_ERROR;
5304 		break;
5305 
5306 	case SATA_TRAN_CMD_UNSUPPORTED:
5307 		/*
5308 		 * Command rejected by HBA as unsupported. It was HBA driver
5309 		 * that rejected the command, command was not sent to
5310 		 * an attached device.
5311 		 */
5312 		if ((sdinfo != NULL) &&
5313 		    (sdinfo->satadrv_state & SATA_DSTATE_RESET))
5314 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5315 			    "sat_hba_start: cmd 0x%2x rejected "
5316 			    "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd);
5317 
5318 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5319 		(void) sata_txlt_invalid_command(spx);
5320 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5321 
5322 		*rval = TRAN_ACCEPT;
5323 		break;
5324 
5325 	case SATA_TRAN_BUSY:
5326 		/*
5327 		 * Command rejected by HBA because other operation prevents
5328 		 * accepting the packet, or device is in RESET condition.
5329 		 */
5330 		if (sdinfo != NULL) {
5331 			sdinfo->satadrv_state =
5332 			    spx->txlt_sata_pkt->satapkt_device.satadev_state;
5333 
5334 			if (sdinfo->satadrv_state & SATA_DSTATE_RESET) {
5335 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5336 				    "sata_hba_start: cmd 0x%2x rejected "
5337 				    "because of device reset condition\n",
5338 				    cmd);
5339 			} else {
5340 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5341 				    "sata_hba_start: cmd 0x%2x rejected "
5342 				    "with SATA_TRAN_BUSY status\n",
5343 				    cmd);
5344 			}
5345 		}
5346 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
5347 		*rval = TRAN_BUSY;
5348 		break;
5349 
5350 	default:
5351 		/* Unrecognized HBA response */
5352 		SATA_LOG_D((sata_hba_inst, CE_WARN,
5353 		    "sata_hba_start: unrecognized HBA response "
5354 		    "to cmd : 0x%2x resp 0x%x", cmd, rval));
5355 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
5356 		*rval = TRAN_FATAL_ERROR;
5357 		break;
5358 	}
5359 
5360 	/*
5361 	 * If we got here, the packet was rejected.
5362 	 * Check if we need to remember reset state clearing request
5363 	 */
5364 	if (cmd_flags.sata_clear_dev_reset) {
5365 		/*
5366 		 * Check if device is still configured - it may have
5367 		 * disapeared from the configuration
5368 		 */
5369 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
5370 		if (sdinfo != NULL) {
5371 			/*
5372 			 * Restore the flag that requests clearing of
5373 			 * the device reset state,
5374 			 * so the next sata packet may carry it to HBA.
5375 			 */
5376 			sdinfo->satadrv_event_flags |=
5377 			    SATA_EVNT_CLEAR_DEVICE_RESET;
5378 		}
5379 	}
5380 	return (-1);
5381 }
5382 
5383 /*
5384  * Scsi response setup for invalid LBA
5385  *
5386  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
5387  */
5388 static int
5389 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx)
5390 {
5391 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5392 	struct scsi_extended_sense *sense;
5393 
5394 	scsipkt->pkt_reason = CMD_CMPLT;
5395 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5396 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5397 	*scsipkt->pkt_scbp = STATUS_CHECK;
5398 
5399 	*scsipkt->pkt_scbp = STATUS_CHECK;
5400 	sense = sata_arq_sense(spx);
5401 	sense->es_key = KEY_ILLEGAL_REQUEST;
5402 	sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
5403 
5404 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5405 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5406 
5407 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5408 	    scsipkt->pkt_comp != NULL)
5409 		/* scsi callback required */
5410 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5411 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5412 		    TQ_SLEEP) == NULL)
5413 			/* Scheduling the callback failed */
5414 			return (TRAN_BUSY);
5415 	return (TRAN_ACCEPT);
5416 }
5417 
5418 
5419 /*
5420  * Analyze device status and error registers and translate them into
5421  * appropriate scsi sense codes.
5422  * NOTE: non-packet commands only for now
5423  */
5424 static void
5425 sata_decode_device_error(sata_pkt_txlate_t *spx,
5426     struct scsi_extended_sense *sense)
5427 {
5428 	uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg;
5429 
5430 	ASSERT(sense != NULL);
5431 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
5432 	    SATA_STATUS_ERR);
5433 
5434 
5435 	if (err_reg & SATA_ERROR_ICRC) {
5436 		sense->es_key = KEY_ABORTED_COMMAND;
5437 		sense->es_add_code = 0x08; /* Communication failure */
5438 		return;
5439 	}
5440 
5441 	if (err_reg & SATA_ERROR_UNC) {
5442 		sense->es_key = KEY_MEDIUM_ERROR;
5443 		/* Information bytes (LBA) need to be set by a caller */
5444 		return;
5445 	}
5446 
5447 	/* ADD HERE: MC error bit handling for ATAPI CD/DVD */
5448 	if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) {
5449 		sense->es_key = KEY_UNIT_ATTENTION;
5450 		sense->es_add_code = 0x3a; /* No media present */
5451 		return;
5452 	}
5453 
5454 	if (err_reg & SATA_ERROR_IDNF) {
5455 		if (err_reg & SATA_ERROR_ABORT) {
5456 			sense->es_key = KEY_ABORTED_COMMAND;
5457 		} else {
5458 			sense->es_key = KEY_ILLEGAL_REQUEST;
5459 			sense->es_add_code = 0x21; /* LBA out of range */
5460 		}
5461 		return;
5462 	}
5463 
5464 	if (err_reg & SATA_ERROR_ABORT) {
5465 		ASSERT(spx->txlt_sata_pkt != NULL);
5466 		sense->es_key = KEY_ABORTED_COMMAND;
5467 		return;
5468 	}
5469 }
5470 
5471 /*
5472  * Extract error LBA from sata_pkt.satapkt_cmd register fields
5473  */
5474 static void
5475 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba)
5476 {
5477 	sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd;
5478 
5479 	*lba = 0;
5480 	if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) {
5481 		*lba = sata_cmd->satacmd_lba_high_msb;
5482 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb;
5483 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb;
5484 	} else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) {
5485 		*lba = sata_cmd->satacmd_device_reg & 0xf;
5486 	}
5487 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb;
5488 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb;
5489 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb;
5490 }
5491 
5492 /*
5493  * This is fixed sense format - if LBA exceeds the info field size,
5494  * no valid info will be returned (valid bit in extended sense will
5495  * be set to 0).
5496  */
5497 static struct scsi_extended_sense *
5498 sata_arq_sense(sata_pkt_txlate_t *spx)
5499 {
5500 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5501 	struct scsi_arq_status *arqs;
5502 	struct scsi_extended_sense *sense;
5503 
5504 	/* Fill ARQ sense data */
5505 	scsipkt->pkt_state |= STATE_ARQ_DONE;
5506 	arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp;
5507 	*(uchar_t *)&arqs->sts_status = STATUS_CHECK;
5508 	*(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD;
5509 	arqs->sts_rqpkt_reason = CMD_CMPLT;
5510 	arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5511 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
5512 	arqs->sts_rqpkt_resid = 0;
5513 	sense = &arqs->sts_sensedata;
5514 	bzero(sense, sizeof (struct scsi_extended_sense));
5515 	sata_fixed_sense_data_preset(sense);
5516 	return (sense);
5517 }
5518 
5519 
5520 /*
5521  * Emulated SATA Read/Write command completion for zero-length requests.
5522  * This request always succedes, so in synchronous mode it always returns
5523  * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the
5524  * callback cannot be scheduled.
5525  */
5526 static int
5527 sata_emul_rw_completion(sata_pkt_txlate_t *spx)
5528 {
5529 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5530 
5531 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5532 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5533 	scsipkt->pkt_reason = CMD_CMPLT;
5534 	*scsipkt->pkt_scbp = STATUS_GOOD;
5535 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5536 		/* scsi callback required - have to schedule it */
5537 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5538 		    (task_func_t *)scsipkt->pkt_comp,
5539 		    (void *)scsipkt, TQ_SLEEP) == NULL)
5540 			/* Scheduling the callback failed */
5541 			return (TRAN_BUSY);
5542 	}
5543 	return (TRAN_ACCEPT);
5544 }
5545 
5546 
5547 /*
5548  * Translate completion status of SATA read/write commands into scsi response.
5549  * pkt completion_reason is checked to determine the completion status.
5550  * Do scsi callback if necessary.
5551  *
5552  * Note: this function may be called also for synchronously executed
5553  * commands.
5554  * This function may be used only if scsi_pkt is non-NULL.
5555  */
5556 static void
5557 sata_txlt_rw_completion(sata_pkt_t *sata_pkt)
5558 {
5559 	sata_pkt_txlate_t *spx =
5560 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
5561 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
5562 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5563 	struct scsi_extended_sense *sense;
5564 	uint64_t lba;
5565 	struct buf *bp;
5566 	int rval;
5567 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5568 		/* Normal completion */
5569 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5570 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
5571 		scsipkt->pkt_reason = CMD_CMPLT;
5572 		*scsipkt->pkt_scbp = STATUS_GOOD;
5573 		if (spx->txlt_tmp_buf != NULL) {
5574 			/* Temporary buffer was used */
5575 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5576 			if (bp->b_flags & B_READ) {
5577 				rval = ddi_dma_sync(
5578 				    spx->txlt_buf_dma_handle, 0, 0,
5579 				    DDI_DMA_SYNC_FORCPU);
5580 				ASSERT(rval == DDI_SUCCESS);
5581 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
5582 				    bp->b_bcount);
5583 			}
5584 		}
5585 	} else {
5586 		/*
5587 		 * Something went wrong - analyze return
5588 		 */
5589 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5590 		    STATE_SENT_CMD | STATE_GOT_STATUS;
5591 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5592 		*scsipkt->pkt_scbp = STATUS_CHECK;
5593 		sense = sata_arq_sense(spx);
5594 		ASSERT(sense != NULL);
5595 
5596 		/*
5597 		 * SATA_PKT_DEV_ERROR is the only case where we may be able to
5598 		 * extract from device registers the failing LBA.
5599 		 */
5600 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
5601 			if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) &&
5602 			    (scmd->satacmd_lba_mid_msb != 0 ||
5603 			    scmd->satacmd_lba_high_msb != 0)) {
5604 				/*
5605 				 * We have problem reporting this cmd LBA
5606 				 * in fixed sense data format, because of
5607 				 * the size of the scsi LBA fields.
5608 				 */
5609 				sense->es_valid = 0;
5610 			} else {
5611 				sata_extract_error_lba(spx, &lba);
5612 				sense->es_info_1 = (lba & 0xFF000000) >> 24;
5613 				sense->es_info_2 = (lba & 0xFF0000) >> 16;
5614 				sense->es_info_3 = (lba & 0xFF00) >> 8;
5615 				sense->es_info_4 = lba & 0xFF;
5616 			}
5617 		} else {
5618 			/* Invalid extended sense info */
5619 			sense->es_valid = 0;
5620 		}
5621 
5622 		switch (sata_pkt->satapkt_reason) {
5623 		case SATA_PKT_PORT_ERROR:
5624 			/* We may want to handle DEV GONE state as well */
5625 			/*
5626 			 * We have no device data. Assume no data transfered.
5627 			 */
5628 			sense->es_key = KEY_HARDWARE_ERROR;
5629 			break;
5630 
5631 		case SATA_PKT_DEV_ERROR:
5632 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5633 			    SATA_STATUS_ERR) {
5634 				/*
5635 				 * determine dev error reason from error
5636 				 * reg content
5637 				 */
5638 				sata_decode_device_error(spx, sense);
5639 				if (sense->es_key == KEY_MEDIUM_ERROR) {
5640 					switch (scmd->satacmd_cmd_reg) {
5641 					case SATAC_READ_DMA:
5642 					case SATAC_READ_DMA_EXT:
5643 					case SATAC_READ_DMA_QUEUED:
5644 					case SATAC_READ_DMA_QUEUED_EXT:
5645 					case SATAC_READ_FPDMA_QUEUED:
5646 						/* Unrecovered read error */
5647 						sense->es_add_code =
5648 						    SD_SCSI_ASC_UNREC_READ_ERR;
5649 						break;
5650 					case SATAC_WRITE_DMA:
5651 					case SATAC_WRITE_DMA_EXT:
5652 					case SATAC_WRITE_DMA_QUEUED:
5653 					case SATAC_WRITE_DMA_QUEUED_EXT:
5654 					case SATAC_WRITE_FPDMA_QUEUED:
5655 						/* Write error */
5656 						sense->es_add_code =
5657 						    SD_SCSI_ASC_WRITE_ERR;
5658 						break;
5659 					default:
5660 						/* Internal error */
5661 						SATA_LOG_D((
5662 						    spx->txlt_sata_hba_inst,
5663 						    CE_WARN,
5664 						    "sata_txlt_rw_completion :"
5665 						    "internal error - invalid "
5666 						    "command 0x%2x",
5667 						    scmd->satacmd_cmd_reg));
5668 						break;
5669 					}
5670 				}
5671 				break;
5672 			}
5673 			/* No extended sense key - no info available */
5674 			scsipkt->pkt_reason = CMD_INCOMPLETE;
5675 			break;
5676 
5677 		case SATA_PKT_TIMEOUT:
5678 			scsipkt->pkt_reason = CMD_TIMEOUT;
5679 			scsipkt->pkt_statistics |=
5680 			    STAT_TIMEOUT | STAT_DEV_RESET;
5681 			sense->es_key = KEY_ABORTED_COMMAND;
5682 			break;
5683 
5684 		case SATA_PKT_ABORTED:
5685 			scsipkt->pkt_reason = CMD_ABORTED;
5686 			scsipkt->pkt_statistics |= STAT_ABORTED;
5687 			sense->es_key = KEY_ABORTED_COMMAND;
5688 			break;
5689 
5690 		case SATA_PKT_RESET:
5691 			scsipkt->pkt_reason = CMD_RESET;
5692 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5693 			sense->es_key = KEY_ABORTED_COMMAND;
5694 			break;
5695 
5696 		default:
5697 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5698 			    "sata_txlt_rw_completion: "
5699 			    "invalid packet completion reason"));
5700 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5701 			break;
5702 		}
5703 	}
5704 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5705 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5706 
5707 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5708 	    scsipkt->pkt_comp != NULL)
5709 		/* scsi callback required */
5710 		(*scsipkt->pkt_comp)(scsipkt);
5711 }
5712 
5713 
5714 /*
5715  * Translate completion status of non-data commands (i.e. commands returning
5716  * no data).
5717  * pkt completion_reason is checked to determine the completion status.
5718  * Do scsi callback if necessary (FLAG_NOINTR == 0)
5719  *
5720  * Note: this function may be called also for synchronously executed
5721  * commands.
5722  * This function may be used only if scsi_pkt is non-NULL.
5723  */
5724 
5725 static 	void
5726 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt)
5727 {
5728 	sata_pkt_txlate_t *spx =
5729 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
5730 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5731 	struct scsi_extended_sense *sense;
5732 
5733 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5734 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5735 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5736 		/* Normal completion */
5737 		scsipkt->pkt_reason = CMD_CMPLT;
5738 		*scsipkt->pkt_scbp = STATUS_GOOD;
5739 	} else {
5740 		/* Something went wrong */
5741 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5742 		*scsipkt->pkt_scbp = STATUS_CHECK;
5743 		sense = sata_arq_sense(spx);
5744 		switch (sata_pkt->satapkt_reason) {
5745 		case SATA_PKT_PORT_ERROR:
5746 			/*
5747 			 * We have no device data. Assume no data transfered.
5748 			 */
5749 			sense->es_key = KEY_HARDWARE_ERROR;
5750 			break;
5751 
5752 		case SATA_PKT_DEV_ERROR:
5753 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5754 			    SATA_STATUS_ERR) {
5755 				/*
5756 				 * determine dev error reason from error
5757 				 * reg content
5758 				 */
5759 				sata_decode_device_error(spx, sense);
5760 				break;
5761 			}
5762 			/* No extended sense key - no info available */
5763 			break;
5764 
5765 		case SATA_PKT_TIMEOUT:
5766 			scsipkt->pkt_reason = CMD_TIMEOUT;
5767 			scsipkt->pkt_statistics |=
5768 			    STAT_TIMEOUT | STAT_DEV_RESET;
5769 			/* No extended sense key ? */
5770 			break;
5771 
5772 		case SATA_PKT_ABORTED:
5773 			scsipkt->pkt_reason = CMD_ABORTED;
5774 			scsipkt->pkt_statistics |= STAT_ABORTED;
5775 			/* No extended sense key ? */
5776 			break;
5777 
5778 		case SATA_PKT_RESET:
5779 			/* pkt aborted by an explicit reset from a host */
5780 			scsipkt->pkt_reason = CMD_RESET;
5781 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5782 			break;
5783 
5784 		default:
5785 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5786 			    "sata_txlt_nodata_cmd_completion: "
5787 			    "invalid packet completion reason %d",
5788 			    sata_pkt->satapkt_reason));
5789 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5790 			break;
5791 		}
5792 
5793 	}
5794 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5795 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5796 
5797 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5798 	    scsipkt->pkt_comp != NULL)
5799 		/* scsi callback required */
5800 		(*scsipkt->pkt_comp)(scsipkt);
5801 }
5802 
5803 
5804 /*
5805  * Build Mode sense R/W recovery page
5806  * NOT IMPLEMENTED
5807  */
5808 
5809 static int
5810 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5811 {
5812 #ifndef __lock_lint
5813 	_NOTE(ARGUNUSED(sdinfo))
5814 	_NOTE(ARGUNUSED(pcntrl))
5815 	_NOTE(ARGUNUSED(buf))
5816 #endif
5817 	return (0);
5818 }
5819 
5820 /*
5821  * Build Mode sense caching page  -  scsi-3 implementation.
5822  * Page length distinguishes previous format from scsi-3 format.
5823  * buf must have space for 0x12 bytes.
5824  * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable.
5825  *
5826  */
5827 static int
5828 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5829 {
5830 	struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf;
5831 	sata_id_t *sata_id = &sdinfo->satadrv_id;
5832 
5833 	/*
5834 	 * Most of the fields are set to 0, being not supported and/or disabled
5835 	 */
5836 	bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3);
5837 
5838 	/* Saved paramters not supported */
5839 	if (pcntrl == 3)
5840 		return (0);
5841 	if (pcntrl == 0 || pcntrl == 2) {
5842 		/*
5843 		 * For now treat current and default parameters as same
5844 		 * That may have to change, if target driver will complain
5845 		 */
5846 		page->mode_page.code = MODEPAGE_CACHING;	/* PS = 0 */
5847 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
5848 
5849 		if ((sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) &&
5850 		    !(sata_id->ai_features85 & SATA_LOOK_AHEAD)) {
5851 			page->dra = 1;		/* Read Ahead disabled */
5852 			page->rcd = 1;		/* Read Cache disabled */
5853 		}
5854 		if ((sata_id->ai_cmdset82 & SATA_WRITE_CACHE) &&
5855 		    (sata_id->ai_features85 & SATA_WRITE_CACHE))
5856 			page->wce = 1;		/* Write Cache enabled */
5857 	} else {
5858 		/* Changeable parameters */
5859 		page->mode_page.code = MODEPAGE_CACHING;
5860 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
5861 		if (sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) {
5862 			page->dra = 1;
5863 			page->rcd = 1;
5864 		}
5865 		if (sata_id->ai_cmdset82 & SATA_WRITE_CACHE)
5866 			page->wce = 1;
5867 	}
5868 	return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
5869 	    sizeof (struct mode_page));
5870 }
5871 
5872 /*
5873  * Build Mode sense exception cntrl page
5874  */
5875 static int
5876 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5877 {
5878 	struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf;
5879 	sata_id_t *sata_id = &sdinfo->satadrv_id;
5880 
5881 	/*
5882 	 * Most of the fields are set to 0, being not supported and/or disabled
5883 	 */
5884 	bzero(buf, PAGELENGTH_INFO_EXCPT);
5885 
5886 	page->mode_page.code = MODEPAGE_INFO_EXCPT;
5887 	page->mode_page.length = PAGELENGTH_INFO_EXCPT;
5888 
5889 	/* Indicate that this is page is saveable */
5890 	page->mode_page.ps = 1;
5891 
5892 	/*
5893 	 * We will return the same data for default, current and saved page.
5894 	 * The only changeable bit is dexcpt and that bit is required
5895 	 * by the ATA specification to be preserved across power cycles.
5896 	 */
5897 	if (pcntrl != 1) {
5898 		page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED);
5899 		page->mrie = MRIE_ONLY_ON_REQUEST;
5900 	}
5901 	else
5902 		page->dexcpt = 1;	/* Only changeable parameter */
5903 
5904 	return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_info_excpt_page));
5905 }
5906 
5907 
5908 static int
5909 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5910 {
5911 	struct mode_acoustic_management *page =
5912 	    (struct mode_acoustic_management *)buf;
5913 	sata_id_t *sata_id = &sdinfo->satadrv_id;
5914 
5915 	/*
5916 	 * Most of the fields are set to 0, being not supported and/or disabled
5917 	 */
5918 	bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT);
5919 
5920 	switch (pcntrl) {
5921 	case P_CNTRL_DEFAULT:
5922 		/*  default paramters not supported */
5923 		return (0);
5924 
5925 	case P_CNTRL_CURRENT:
5926 	case P_CNTRL_SAVED:
5927 		/* Saved and current are supported and are identical */
5928 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
5929 		page->mode_page.length =
5930 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
5931 		page->mode_page.ps = 1;
5932 
5933 		/* Word 83 indicates if feature is supported */
5934 		/* If feature is not supported */
5935 		if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) {
5936 			page->acoustic_manag_enable =
5937 			    ACOUSTIC_DISABLED;
5938 		} else {
5939 			page->acoustic_manag_enable =
5940 			    ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT)
5941 			    != 0);
5942 			/* Word 94 inidicates the value */
5943 #ifdef	_LITTLE_ENDIAN
5944 			page->acoustic_manag_level =
5945 			    (uchar_t)sata_id->ai_acoustic;
5946 			page->vendor_recommended_value =
5947 			    sata_id->ai_acoustic >> 8;
5948 #else
5949 			page->acoustic_manag_level =
5950 			    sata_id->ai_acoustic >> 8;
5951 			page->vendor_recommended_value =
5952 			    (uchar_t)sata_id->ai_acoustic;
5953 #endif
5954 		}
5955 		break;
5956 
5957 	case P_CNTRL_CHANGEABLE:
5958 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
5959 		page->mode_page.length =
5960 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
5961 		page->mode_page.ps = 1;
5962 
5963 		/* Word 83 indicates if the feature is supported */
5964 		if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) {
5965 			page->acoustic_manag_enable =
5966 			    ACOUSTIC_ENABLED;
5967 			page->acoustic_manag_level = 0xff;
5968 		}
5969 		break;
5970 	}
5971 	return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
5972 	    sizeof (struct mode_page));
5973 }
5974 
5975 
5976 /*
5977  * Build Mode sense power condition page
5978  * NOT IMPLEMENTED.
5979  */
5980 static int
5981 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5982 {
5983 #ifndef __lock_lint
5984 	_NOTE(ARGUNUSED(sdinfo))
5985 	_NOTE(ARGUNUSED(pcntrl))
5986 	_NOTE(ARGUNUSED(buf))
5987 #endif
5988 	return (0);
5989 }
5990 
5991 
5992 /*
5993  * Process mode select caching page 8 (scsi3 format only).
5994  * Read Ahead (same as read cache) and Write Cache may be turned on and off
5995  * if these features are supported by the device. If these features are not
5996  * supported, quietly ignore them.
5997  * This function fails only if the SET FEATURE command sent to
5998  * the device fails. The page format is not varified, assuming that the
5999  * target driver operates correctly - if parameters length is too short,
6000  * we just drop the page.
6001  * Two command may be sent if both Read Cache/Read Ahead and Write Cache
6002  * setting have to be changed.
6003  * SET FEATURE command is executed synchronously, i.e. we wait here until
6004  * it is completed, regardless of the scsi pkt directives.
6005  *
6006  * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e.
6007  * changing DRA will change RCD.
6008  *
6009  * More than one SATA command may be executed to perform operations specified
6010  * by mode select pages. The first error terminates further execution.
6011  * Operations performed successully are not backed-up in such case.
6012  *
6013  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
6014  * If operation resulted in changing device setup, dmod flag should be set to
6015  * one (1). If parameters were not changed, dmod flag should be set to 0.
6016  * Upon return, if operation required sending command to the device, the rval
6017  * should be set to the value returned by sata_hba_start. If operation
6018  * did not require device access, rval should be set to TRAN_ACCEPT.
6019  * The pagelen should be set to the length of the page.
6020  *
6021  * This function has to be called with a port mutex held.
6022  *
6023  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6024  */
6025 int
6026 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page,
6027     int parmlen, int *pagelen, int *rval, int *dmod)
6028 {
6029 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6030 	sata_drive_info_t *sdinfo;
6031 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6032 	sata_id_t *sata_id;
6033 	struct scsi_extended_sense *sense;
6034 	int wce, dra;	/* Current settings */
6035 
6036 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6037 	    &spx->txlt_sata_pkt->satapkt_device);
6038 	sata_id = &sdinfo->satadrv_id;
6039 	*dmod = 0;
6040 
6041 	/* Verify parameters length. If too short, drop it */
6042 	if (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
6043 	    sizeof (struct mode_page) < parmlen) {
6044 		*scsipkt->pkt_scbp = STATUS_CHECK;
6045 		sense = sata_arq_sense(spx);
6046 		sense->es_key = KEY_ILLEGAL_REQUEST;
6047 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6048 		*pagelen = parmlen;
6049 		*rval = TRAN_ACCEPT;
6050 		return (SATA_FAILURE);
6051 	}
6052 
6053 	*pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page);
6054 
6055 	/*
6056 	 * We can manipulate only write cache and read ahead
6057 	 * (read cache) setting.
6058 	 */
6059 	if (!(sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) &&
6060 	    !(sata_id->ai_cmdset82 & SATA_WRITE_CACHE)) {
6061 		/*
6062 		 * None of the features is supported - ignore
6063 		 */
6064 		*rval = TRAN_ACCEPT;
6065 		return (SATA_SUCCESS);
6066 	}
6067 
6068 	/* Current setting of Read Ahead (and Read Cache) */
6069 	if (sata_id->ai_features85 & SATA_LOOK_AHEAD)
6070 		dra = 0;	/* 0 == not disabled */
6071 	else
6072 		dra = 1;
6073 	/* Current setting of Write Cache */
6074 	if (sata_id->ai_features85 & SATA_WRITE_CACHE)
6075 		wce = 1;
6076 	else
6077 		wce = 0;
6078 
6079 	if (page->dra == dra && page->wce == wce && page->rcd == dra) {
6080 		/* nothing to do */
6081 		*rval = TRAN_ACCEPT;
6082 		return (SATA_SUCCESS);
6083 	}
6084 	/*
6085 	 * Need to flip some setting
6086 	 * Set-up Internal SET FEATURES command(s)
6087 	 */
6088 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6089 	scmd->satacmd_addr_type = 0;
6090 	scmd->satacmd_device_reg = 0;
6091 	scmd->satacmd_status_reg = 0;
6092 	scmd->satacmd_error_reg = 0;
6093 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
6094 	if (page->dra != dra || page->rcd != dra) {
6095 		/* Need to flip read ahead setting */
6096 		if (dra == 0)
6097 			/* Disable read ahead / read cache */
6098 			scmd->satacmd_features_reg =
6099 			    SATAC_SF_DISABLE_READ_AHEAD;
6100 		else
6101 			/* Enable read ahead  / read cache */
6102 			scmd->satacmd_features_reg =
6103 			    SATAC_SF_ENABLE_READ_AHEAD;
6104 
6105 		/* Transfer command to HBA */
6106 		if (sata_hba_start(spx, rval) != 0)
6107 			/*
6108 			 * Pkt not accepted for execution.
6109 			 */
6110 			return (SATA_FAILURE);
6111 
6112 		*dmod = 1;
6113 
6114 		/* Now process return */
6115 		if (spx->txlt_sata_pkt->satapkt_reason !=
6116 		    SATA_PKT_COMPLETED) {
6117 			goto failure;	/* Terminate */
6118 		}
6119 	}
6120 
6121 	/* Note that the packet is not removed, so it could be re-used */
6122 	if (page->wce != wce) {
6123 		/* Need to flip Write Cache setting */
6124 		if (page->wce == 1)
6125 			/* Enable write cache */
6126 			scmd->satacmd_features_reg =
6127 			    SATAC_SF_ENABLE_WRITE_CACHE;
6128 		else
6129 			/* Disable write cache */
6130 			scmd->satacmd_features_reg =
6131 			    SATAC_SF_DISABLE_WRITE_CACHE;
6132 
6133 		/* Transfer command to HBA */
6134 		if (sata_hba_start(spx, rval) != 0)
6135 			/*
6136 			 * Pkt not accepted for execution.
6137 			 */
6138 			return (SATA_FAILURE);
6139 
6140 		*dmod = 1;
6141 
6142 		/* Now process return */
6143 		if (spx->txlt_sata_pkt->satapkt_reason !=
6144 		    SATA_PKT_COMPLETED) {
6145 			goto failure;
6146 		}
6147 	}
6148 	return (SATA_SUCCESS);
6149 
6150 failure:
6151 	sata_xlate_errors(spx);
6152 
6153 	return (SATA_FAILURE);
6154 }
6155 
6156 /*
6157  * Process mode select informational exceptions control page 0x1c
6158  *
6159  * The only changeable bit is dexcpt (disable exceptions).
6160  * MRIE (method of reporting informational exceptions) must be
6161  * "only on request".
6162  *
6163  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
6164  * If operation resulted in changing device setup, dmod flag should be set to
6165  * one (1). If parameters were not changed, dmod flag should be set to 0.
6166  * Upon return, if operation required sending command to the device, the rval
6167  * should be set to the value returned by sata_hba_start. If operation
6168  * did not require device access, rval should be set to TRAN_ACCEPT.
6169  * The pagelen should be set to the length of the page.
6170  *
6171  * This function has to be called with a port mutex held.
6172  *
6173  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6174  */
6175 static	int
6176 sata_mode_select_page_1c(
6177 	sata_pkt_txlate_t *spx,
6178 	struct mode_info_excpt_page *page,
6179 	int parmlen,
6180 	int *pagelen,
6181 	int *rval,
6182 	int *dmod)
6183 {
6184 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6185 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6186 	sata_drive_info_t *sdinfo;
6187 	sata_id_t *sata_id;
6188 	struct scsi_extended_sense *sense;
6189 
6190 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6191 	    &spx->txlt_sata_pkt->satapkt_device);
6192 	sata_id = &sdinfo->satadrv_id;
6193 
6194 	*dmod = 0;
6195 
6196 	/* Verify parameters length. If too short, drop it */
6197 	if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) < parmlen) ||
6198 	    page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) {
6199 		*scsipkt->pkt_scbp = STATUS_CHECK;
6200 		sense = sata_arq_sense(spx);
6201 		sense->es_key = KEY_ILLEGAL_REQUEST;
6202 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6203 		*pagelen = parmlen;
6204 		*rval = TRAN_ACCEPT;
6205 		return (SATA_FAILURE);
6206 	}
6207 
6208 	*pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page);
6209 
6210 	if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
6211 		*scsipkt->pkt_scbp = STATUS_CHECK;
6212 		sense = sata_arq_sense(spx);
6213 		sense->es_key = KEY_ILLEGAL_REQUEST;
6214 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6215 		*pagelen = parmlen;
6216 		*rval = TRAN_ACCEPT;
6217 		return (SATA_FAILURE);
6218 	}
6219 
6220 	/* If already in the state requested, we are done */
6221 	if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
6222 		/* nothing to do */
6223 		*rval = TRAN_ACCEPT;
6224 		return (SATA_SUCCESS);
6225 	}
6226 
6227 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6228 
6229 	/* Build SMART_ENABLE or SMART_DISABLE command */
6230 	scmd->satacmd_addr_type = 0;		/* N/A */
6231 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
6232 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
6233 	scmd->satacmd_features_reg = page->dexcpt ?
6234 	    SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS;
6235 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
6236 	scmd->satacmd_cmd_reg = SATAC_SMART;
6237 
6238 	/* Transfer command to HBA */
6239 	if (sata_hba_start(spx, rval) != 0)
6240 		/*
6241 		 * Pkt not accepted for execution.
6242 		 */
6243 		return (SATA_FAILURE);
6244 
6245 	*dmod = 1;	/* At least may have been modified */
6246 
6247 	/* Now process return */
6248 	if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED)
6249 		return (SATA_SUCCESS);
6250 
6251 	/* Packet did not complete successfully */
6252 	sata_xlate_errors(spx);
6253 
6254 	return (SATA_FAILURE);
6255 }
6256 
6257 int
6258 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct
6259     mode_acoustic_management *page, int parmlen, int *pagelen,
6260     int *rval, int *dmod)
6261 {
6262 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6263 	sata_drive_info_t *sdinfo;
6264 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6265 	sata_id_t *sata_id;
6266 	struct scsi_extended_sense *sense;
6267 
6268 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6269 	    &spx->txlt_sata_pkt->satapkt_device);
6270 	sata_id = &sdinfo->satadrv_id;
6271 	*dmod = 0;
6272 
6273 	/* If parmlen is too short or the feature is not supported, drop it */
6274 	if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6275 	    sizeof (struct mode_page)) < parmlen) ||
6276 	    (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) {
6277 		*scsipkt->pkt_scbp = STATUS_CHECK;
6278 		sense = sata_arq_sense(spx);
6279 		sense->es_key = KEY_ILLEGAL_REQUEST;
6280 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6281 		*pagelen = parmlen;
6282 		*rval = TRAN_ACCEPT;
6283 		return (SATA_FAILURE);
6284 	}
6285 
6286 	*pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6287 	    sizeof (struct mode_page);
6288 
6289 	/*
6290 	 * We can enable and disable acoustice management and
6291 	 * set the acoustic management level.
6292 	 */
6293 
6294 	/*
6295 	 * Set-up Internal SET FEATURES command(s)
6296 	 */
6297 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6298 	scmd->satacmd_addr_type = 0;
6299 	scmd->satacmd_device_reg = 0;
6300 	scmd->satacmd_status_reg = 0;
6301 	scmd->satacmd_error_reg = 0;
6302 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
6303 	if (page->acoustic_manag_enable) {
6304 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC;
6305 		scmd->satacmd_sec_count_lsb = page->acoustic_manag_level;
6306 	} else {	/* disabling acoustic management */
6307 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC;
6308 	}
6309 
6310 	/* Transfer command to HBA */
6311 	if (sata_hba_start(spx, rval) != 0)
6312 		/*
6313 		 * Pkt not accepted for execution.
6314 		 */
6315 		return (SATA_FAILURE);
6316 
6317 	/* Now process return */
6318 	if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) {
6319 		sata_xlate_errors(spx);
6320 		return (SATA_FAILURE);
6321 	}
6322 
6323 	*dmod = 1;
6324 
6325 	return (SATA_SUCCESS);
6326 }
6327 
6328 
6329 
6330 
6331 /*
6332  * sata_build_lsense_page0() is used to create the
6333  * SCSI LOG SENSE page 0 (supported log pages)
6334  *
6335  * Currently supported pages are 0, 0x10, 0x2f and 0x30
6336  * (supported log pages, self-test results, informational exceptions
6337  *  and Sun vendor specific ATA SMART data).
6338  *
6339  * Takes a sata_drive_info t * and the address of a buffer
6340  * in which to create the page information.
6341  *
6342  * Returns the number of bytes valid in the buffer.
6343  */
6344 static	int
6345 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf)
6346 {
6347 	struct log_parameter *lpp = (struct log_parameter *)buf;
6348 	uint8_t *page_ptr = (uint8_t *)lpp->param_values;
6349 	int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */
6350 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6351 
6352 	lpp->param_code[0] = 0;
6353 	lpp->param_code[1] = 0;
6354 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
6355 	*page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES;
6356 
6357 	if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
6358 		if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) {
6359 			*page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS;
6360 			++num_pages_supported;
6361 		}
6362 		*page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS;
6363 		++num_pages_supported;
6364 		*page_ptr++ = PAGE_CODE_SMART_READ_DATA;
6365 		++num_pages_supported;
6366 	}
6367 
6368 	lpp->param_len = num_pages_supported;
6369 
6370 	return ((&lpp->param_values[0] - (uint8_t *)lpp) +
6371 	    num_pages_supported);
6372 }
6373 
6374 /*
6375  * sata_build_lsense_page_10() is used to create the
6376  * SCSI LOG SENSE page 0x10 (self-test results)
6377  *
6378  * Takes a sata_drive_info t * and the address of a buffer
6379  * in which to create the page information as well as a sata_hba_inst_t *.
6380  *
6381  * Returns the number of bytes valid in the buffer.
6382  */
6383 static	int
6384 sata_build_lsense_page_10(
6385 	sata_drive_info_t *sdinfo,
6386 	uint8_t *buf,
6387 	sata_hba_inst_t *sata_hba_inst)
6388 {
6389 	struct log_parameter *lpp = (struct log_parameter *)buf;
6390 	int rval;
6391 
6392 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6393 		struct smart_ext_selftest_log *ext_selftest_log;
6394 
6395 		ext_selftest_log = kmem_zalloc(
6396 		    sizeof (struct smart_ext_selftest_log), KM_SLEEP);
6397 
6398 		rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo,
6399 		    ext_selftest_log, 0);
6400 		if (rval == 0) {
6401 			int index, start_index;
6402 			struct smart_ext_selftest_log_entry *entry;
6403 			static const struct smart_ext_selftest_log_entry empty =
6404 			    {0};
6405 			uint16_t block_num;
6406 			int count;
6407 			boolean_t only_one_block = B_FALSE;
6408 
6409 			index = ext_selftest_log->
6410 			    smart_ext_selftest_log_index[0];
6411 			index |= ext_selftest_log->
6412 			    smart_ext_selftest_log_index[1] << 8;
6413 			if (index == 0)
6414 				goto out;
6415 
6416 			--index;	/* Correct for 0 origin */
6417 			start_index = index;	/* remember where we started */
6418 			block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6419 			if (block_num != 0) {
6420 				rval = sata_ext_smart_selftest_read_log(
6421 				    sata_hba_inst, sdinfo, ext_selftest_log,
6422 				    block_num);
6423 				if (rval != 0)
6424 					goto out;
6425 			}
6426 			index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6427 			entry =
6428 			    &ext_selftest_log->
6429 			    smart_ext_selftest_log_entries[index];
6430 
6431 			for (count = 1;
6432 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
6433 			    ++count) {
6434 				uint8_t status;
6435 				uint8_t code;
6436 				uint8_t sense_key;
6437 				uint8_t add_sense_code;
6438 				uint8_t add_sense_code_qual;
6439 
6440 				/* If this is an unused entry, we are done */
6441 				if (bcmp(entry, &empty, sizeof (empty)) == 0) {
6442 					/* Broken firmware on some disks */
6443 					if (index + 1 ==
6444 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK) {
6445 						--entry;
6446 						--index;
6447 						if (bcmp(entry, &empty,
6448 						    sizeof (empty)) == 0)
6449 							goto out;
6450 					} else
6451 						goto out;
6452 				}
6453 
6454 				if (only_one_block &&
6455 				    start_index == index)
6456 					goto out;
6457 
6458 				lpp->param_code[0] = 0;
6459 				lpp->param_code[1] = count;
6460 				lpp->param_ctrl_flags =
6461 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
6462 				lpp->param_len =
6463 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
6464 
6465 				status = entry->smart_ext_selftest_log_status;
6466 				status >>= 4;
6467 				switch (status) {
6468 				case 0:
6469 				default:
6470 					sense_key = KEY_NO_SENSE;
6471 					add_sense_code =
6472 					    SD_SCSI_ASC_NO_ADD_SENSE;
6473 					add_sense_code_qual = 0;
6474 					break;
6475 				case 1:
6476 					sense_key = KEY_ABORTED_COMMAND;
6477 					add_sense_code =
6478 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6479 					add_sense_code_qual = SCSI_COMPONENT_81;
6480 					break;
6481 				case 2:
6482 					sense_key = KEY_ABORTED_COMMAND;
6483 					add_sense_code =
6484 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6485 					add_sense_code_qual = SCSI_COMPONENT_82;
6486 					break;
6487 				case 3:
6488 					sense_key = KEY_ABORTED_COMMAND;
6489 					add_sense_code =
6490 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6491 					add_sense_code_qual = SCSI_COMPONENT_83;
6492 					break;
6493 				case 4:
6494 					sense_key = KEY_HARDWARE_ERROR;
6495 					add_sense_code =
6496 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6497 					add_sense_code_qual = SCSI_COMPONENT_84;
6498 					break;
6499 				case 5:
6500 					sense_key = KEY_HARDWARE_ERROR;
6501 					add_sense_code =
6502 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6503 					add_sense_code_qual = SCSI_COMPONENT_85;
6504 					break;
6505 				case 6:
6506 					sense_key = KEY_HARDWARE_ERROR;
6507 					add_sense_code =
6508 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6509 					add_sense_code_qual = SCSI_COMPONENT_86;
6510 					break;
6511 				case 7:
6512 					sense_key = KEY_MEDIUM_ERROR;
6513 					add_sense_code =
6514 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6515 					add_sense_code_qual = SCSI_COMPONENT_87;
6516 					break;
6517 				case 8:
6518 					sense_key = KEY_HARDWARE_ERROR;
6519 					add_sense_code =
6520 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6521 					add_sense_code_qual = SCSI_COMPONENT_88;
6522 					break;
6523 				}
6524 				code = 0;	/* unspecified */
6525 				status |= (code << 4);
6526 				lpp->param_values[0] = status;
6527 				lpp->param_values[1] = 0; /* unspecified */
6528 				lpp->param_values[2] = entry->
6529 				    smart_ext_selftest_log_timestamp[1];
6530 				lpp->param_values[3] = entry->
6531 				    smart_ext_selftest_log_timestamp[0];
6532 				if (status != 0) {
6533 					lpp->param_values[4] = 0;
6534 					lpp->param_values[5] = 0;
6535 					lpp->param_values[6] = entry->
6536 					    smart_ext_selftest_log_failing_lba
6537 					    [5];
6538 					lpp->param_values[7] = entry->
6539 					    smart_ext_selftest_log_failing_lba
6540 					    [4];
6541 					lpp->param_values[8] = entry->
6542 					    smart_ext_selftest_log_failing_lba
6543 					    [3];
6544 					lpp->param_values[9] = entry->
6545 					    smart_ext_selftest_log_failing_lba
6546 					    [2];
6547 					lpp->param_values[10] = entry->
6548 					    smart_ext_selftest_log_failing_lba
6549 					    [1];
6550 					lpp->param_values[11] = entry->
6551 					    smart_ext_selftest_log_failing_lba
6552 					    [0];
6553 				} else {	/* No bad block address */
6554 					lpp->param_values[4] = 0xff;
6555 					lpp->param_values[5] = 0xff;
6556 					lpp->param_values[6] = 0xff;
6557 					lpp->param_values[7] = 0xff;
6558 					lpp->param_values[8] = 0xff;
6559 					lpp->param_values[9] = 0xff;
6560 					lpp->param_values[10] = 0xff;
6561 					lpp->param_values[11] = 0xff;
6562 				}
6563 
6564 				lpp->param_values[12] = sense_key;
6565 				lpp->param_values[13] = add_sense_code;
6566 				lpp->param_values[14] = add_sense_code_qual;
6567 				lpp->param_values[15] = 0; /* undefined */
6568 
6569 				lpp = (struct log_parameter *)
6570 				    (((uint8_t *)lpp) +
6571 				    SCSI_LOG_PARAM_HDR_LEN +
6572 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
6573 
6574 				--index;	/* Back up to previous entry */
6575 				if (index < 0) {
6576 					if (block_num > 0) {
6577 						--block_num;
6578 					} else {
6579 						struct read_log_ext_directory
6580 						    logdir;
6581 
6582 						rval =
6583 						    sata_read_log_ext_directory(
6584 						    sata_hba_inst, sdinfo,
6585 						    &logdir);
6586 						if (rval == -1)
6587 							goto out;
6588 						if ((logdir.read_log_ext_vers
6589 						    [0] == 0) &&
6590 						    (logdir.read_log_ext_vers
6591 						    [1] == 0))
6592 							goto out;
6593 						block_num =
6594 						    logdir.read_log_ext_nblks
6595 						    [EXT_SMART_SELFTEST_LOG_PAGE
6596 						    - 1][0];
6597 						block_num |= logdir.
6598 						    read_log_ext_nblks
6599 						    [EXT_SMART_SELFTEST_LOG_PAGE
6600 						    - 1][1] << 8;
6601 						--block_num;
6602 						only_one_block =
6603 						    (block_num == 0);
6604 					}
6605 					rval = sata_ext_smart_selftest_read_log(
6606 					    sata_hba_inst, sdinfo,
6607 					    ext_selftest_log, block_num);
6608 					if (rval != 0)
6609 						goto out;
6610 
6611 					index =
6612 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK -
6613 					    1;
6614 				}
6615 				index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6616 				entry = &ext_selftest_log->
6617 				    smart_ext_selftest_log_entries[index];
6618 			}
6619 		}
6620 out:
6621 		kmem_free(ext_selftest_log,
6622 		    sizeof (struct smart_ext_selftest_log));
6623 	} else {
6624 		struct smart_selftest_log *selftest_log;
6625 
6626 		selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log),
6627 		    KM_SLEEP);
6628 
6629 		rval = sata_smart_selftest_log(sata_hba_inst, sdinfo,
6630 		    selftest_log);
6631 
6632 		if (rval == 0) {
6633 			int index;
6634 			int count;
6635 			struct smart_selftest_log_entry *entry;
6636 			static const struct smart_selftest_log_entry empty =
6637 			    { 0 };
6638 
6639 			index = selftest_log->smart_selftest_log_index;
6640 			if (index == 0)
6641 				goto done;
6642 			--index;	/* Correct for 0 origin */
6643 			entry = &selftest_log->
6644 			    smart_selftest_log_entries[index];
6645 			for (count = 1;
6646 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
6647 			    ++count) {
6648 				uint8_t status;
6649 				uint8_t code;
6650 				uint8_t sense_key;
6651 				uint8_t add_sense_code;
6652 				uint8_t add_sense_code_qual;
6653 
6654 				if (bcmp(entry, &empty, sizeof (empty)) == 0)
6655 					goto done;
6656 
6657 				lpp->param_code[0] = 0;
6658 				lpp->param_code[1] = count;
6659 				lpp->param_ctrl_flags =
6660 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
6661 				lpp->param_len =
6662 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
6663 
6664 				status = entry->smart_selftest_log_status;
6665 				status >>= 4;
6666 				switch (status) {
6667 				case 0:
6668 				default:
6669 					sense_key = KEY_NO_SENSE;
6670 					add_sense_code =
6671 					    SD_SCSI_ASC_NO_ADD_SENSE;
6672 					break;
6673 				case 1:
6674 					sense_key = KEY_ABORTED_COMMAND;
6675 					add_sense_code =
6676 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6677 					add_sense_code_qual = SCSI_COMPONENT_81;
6678 					break;
6679 				case 2:
6680 					sense_key = KEY_ABORTED_COMMAND;
6681 					add_sense_code =
6682 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6683 					add_sense_code_qual = SCSI_COMPONENT_82;
6684 					break;
6685 				case 3:
6686 					sense_key = KEY_ABORTED_COMMAND;
6687 					add_sense_code =
6688 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6689 					add_sense_code_qual = SCSI_COMPONENT_83;
6690 					break;
6691 				case 4:
6692 					sense_key = KEY_HARDWARE_ERROR;
6693 					add_sense_code =
6694 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6695 					add_sense_code_qual = SCSI_COMPONENT_84;
6696 					break;
6697 				case 5:
6698 					sense_key = KEY_HARDWARE_ERROR;
6699 					add_sense_code =
6700 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6701 					add_sense_code_qual = SCSI_COMPONENT_85;
6702 					break;
6703 				case 6:
6704 					sense_key = KEY_HARDWARE_ERROR;
6705 					add_sense_code =
6706 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6707 					add_sense_code_qual = SCSI_COMPONENT_86;
6708 					break;
6709 				case 7:
6710 					sense_key = KEY_MEDIUM_ERROR;
6711 					add_sense_code =
6712 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6713 					add_sense_code_qual = SCSI_COMPONENT_87;
6714 					break;
6715 				case 8:
6716 					sense_key = KEY_HARDWARE_ERROR;
6717 					add_sense_code =
6718 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6719 					add_sense_code_qual = SCSI_COMPONENT_88;
6720 					break;
6721 				}
6722 				code = 0;	/* unspecified */
6723 				status |= (code << 4);
6724 				lpp->param_values[0] = status;
6725 				lpp->param_values[1] = 0; /* unspecified */
6726 				lpp->param_values[2] = entry->
6727 				    smart_selftest_log_timestamp[1];
6728 				lpp->param_values[3] = entry->
6729 				    smart_selftest_log_timestamp[0];
6730 				if (status != 0) {
6731 					lpp->param_values[4] = 0;
6732 					lpp->param_values[5] = 0;
6733 					lpp->param_values[6] = 0;
6734 					lpp->param_values[7] = 0;
6735 					lpp->param_values[8] = entry->
6736 					    smart_selftest_log_failing_lba[3];
6737 					lpp->param_values[9] = entry->
6738 					    smart_selftest_log_failing_lba[2];
6739 					lpp->param_values[10] = entry->
6740 					    smart_selftest_log_failing_lba[1];
6741 					lpp->param_values[11] = entry->
6742 					    smart_selftest_log_failing_lba[0];
6743 				} else {	/* No block address */
6744 					lpp->param_values[4] = 0xff;
6745 					lpp->param_values[5] = 0xff;
6746 					lpp->param_values[6] = 0xff;
6747 					lpp->param_values[7] = 0xff;
6748 					lpp->param_values[8] = 0xff;
6749 					lpp->param_values[9] = 0xff;
6750 					lpp->param_values[10] = 0xff;
6751 					lpp->param_values[11] = 0xff;
6752 				}
6753 				lpp->param_values[12] = sense_key;
6754 				lpp->param_values[13] = add_sense_code;
6755 				lpp->param_values[14] = add_sense_code_qual;
6756 				lpp->param_values[15] = 0; /* undefined */
6757 
6758 				lpp = (struct log_parameter *)
6759 				    (((uint8_t *)lpp) +
6760 				    SCSI_LOG_PARAM_HDR_LEN +
6761 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
6762 				--index;	/* back up to previous entry */
6763 				if (index < 0) {
6764 					index =
6765 					    NUM_SMART_SELFTEST_LOG_ENTRIES - 1;
6766 				}
6767 				entry = &selftest_log->
6768 				    smart_selftest_log_entries[index];
6769 			}
6770 		}
6771 done:
6772 		kmem_free(selftest_log, sizeof (struct smart_selftest_log));
6773 	}
6774 
6775 	return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) *
6776 	    SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS);
6777 }
6778 
6779 /*
6780  * sata_build_lsense_page_2f() is used to create the
6781  * SCSI LOG SENSE page 0x10 (informational exceptions)
6782  *
6783  * Takes a sata_drive_info t * and the address of a buffer
6784  * in which to create the page information as well as a sata_hba_inst_t *.
6785  *
6786  * Returns the number of bytes valid in the buffer.
6787  */
6788 static	int
6789 sata_build_lsense_page_2f(
6790 	sata_drive_info_t *sdinfo,
6791 	uint8_t *buf,
6792 	sata_hba_inst_t *sata_hba_inst)
6793 {
6794 	struct log_parameter *lpp = (struct log_parameter *)buf;
6795 	int rval;
6796 	uint8_t *smart_data;
6797 	uint8_t temp;
6798 	sata_id_t *sata_id;
6799 #define	SMART_NO_TEMP	0xff
6800 
6801 	lpp->param_code[0] = 0;
6802 	lpp->param_code[1] = 0;
6803 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
6804 
6805 	/* Now get the SMART status w.r.t. threshold exceeded */
6806 	rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo);
6807 	switch (rval) {
6808 	case 1:
6809 		lpp->param_values[0] = SCSI_PREDICTED_FAILURE;
6810 		lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE;
6811 		break;
6812 	case 0:
6813 	case -1:	/* failed to get data */
6814 		lpp->param_values[0] = 0;	/* No failure predicted */
6815 		lpp->param_values[1] = 0;
6816 		break;
6817 #if defined(SATA_DEBUG)
6818 	default:
6819 		cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value");
6820 		/* NOTREACHED */
6821 #endif
6822 	}
6823 
6824 	sata_id = &sdinfo->satadrv_id;
6825 	if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP))
6826 		temp = SMART_NO_TEMP;
6827 	else {
6828 		/* Now get the temperature */
6829 		smart_data = kmem_zalloc(512, KM_SLEEP);
6830 		rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data,
6831 		    SCT_STATUS_LOG_PAGE, 1);
6832 		if (rval == -1)
6833 			temp = SMART_NO_TEMP;
6834 		else {
6835 			temp = smart_data[200];
6836 			if (temp & 0x80) {
6837 				if (temp & 0x7f)
6838 					temp = 0;
6839 				else
6840 					temp = SMART_NO_TEMP;
6841 			}
6842 		}
6843 		kmem_free(smart_data, 512);
6844 	}
6845 
6846 	lpp->param_values[2] = temp;	/* most recent temperature */
6847 	lpp->param_values[3] = 0;	/* required vendor specific byte */
6848 
6849 	lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN;
6850 
6851 
6852 	return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN);
6853 }
6854 
6855 /*
6856  * sata_build_lsense_page_30() is used to create the
6857  * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data).
6858  *
6859  * Takes a sata_drive_info t * and the address of a buffer
6860  * in which to create the page information as well as a sata_hba_inst_t *.
6861  *
6862  * Returns the number of bytes valid in the buffer.
6863  */
6864 static int
6865 sata_build_lsense_page_30(
6866 	sata_drive_info_t *sdinfo,
6867 	uint8_t *buf,
6868 	sata_hba_inst_t *sata_hba_inst)
6869 {
6870 	struct smart_data *smart_data = (struct smart_data *)buf;
6871 	int rval;
6872 
6873 	/* Now do the SMART READ DATA */
6874 	rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data);
6875 	if (rval == -1)
6876 		return (0);
6877 
6878 	return (sizeof (struct smart_data));
6879 }
6880 
6881 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */
6882 
6883 /*
6884  * Start command for ATAPI device.
6885  * This function processes scsi_pkt requests.
6886  * Only CD/DVD devices are supported.
6887  * Most commands are packet without any translation into Packet Command.
6888  * Some may be trapped and executed as SATA commands (not clear which one).
6889  *
6890  * Returns TRAN_ACCEPT if command is accepted for execution (or completed
6891  * execution).
6892  * Returns other TRAN_XXXX codes if command is not accepted or completed
6893  * (see return values for sata_hba_start()).
6894  *
6895  * Note:
6896  * Inquiry cdb format differs between transport version 2 and 3.
6897  * However, the transport version 3 devices that were checked did not adhere
6898  * to the specification (ignored MSB of the allocation length). Therefore,
6899  * the transport version is not checked, but Inquiry allocation length is
6900  * truncated to 255 bytes if the original allocation length set-up by the
6901  * target driver is greater than 255 bytes.
6902  */
6903 static int
6904 sata_txlt_atapi(sata_pkt_txlate_t *spx)
6905 {
6906 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6907 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6908 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
6909 	sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx);
6910 	sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba,
6911 	    &spx->txlt_sata_pkt->satapkt_device);
6912 	int cport = SATA_TXLT_CPORT(spx);
6913 	int cdblen;
6914 	int rval, reason;
6915 	int synch;
6916 	union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp;
6917 
6918 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
6919 
6920 	if (((rval = sata_txlt_generic_pkt_info(spx, &reason)) !=
6921 	    TRAN_ACCEPT) || (reason == CMD_DEV_GONE)) {
6922 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6923 		return (rval);
6924 	}
6925 
6926 	/*
6927 	 * ATAPI device executes some ATA commands in addition to MMC command
6928 	 * set. These ATA commands may be executed by the regular SATA
6929 	 * translation functions. None needs to be captured now.
6930 	 * Other commands belong to MMC command set and are delivered
6931 	 * to ATAPI device via Packet Command.
6932 	 */
6933 
6934 	/* Check the size of cdb */
6935 	cdblen = scsi_cdb_size[GETGROUP(cdbp)];
6936 	if (cdblen > sdinfo->satadrv_atapi_cdb_len) {
6937 		sata_log(NULL, CE_WARN,
6938 		    "sata: invalid ATAPI cdb length %d",
6939 		    scsipkt->pkt_cdblen);
6940 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6941 		return (TRAN_BADPKT);
6942 	}
6943 
6944 	SATAATAPITRACE(spx, cdblen);
6945 
6946 	/*
6947 	 * For non-read/write commands we need to
6948 	 * map buffer
6949 	 */
6950 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6951 	case SCMD_READ:
6952 	case SCMD_READ_G1:
6953 	case SCMD_READ_G5:
6954 	case SCMD_READ_G4:
6955 	case SCMD_WRITE:
6956 	case SCMD_WRITE_G1:
6957 	case SCMD_WRITE_G5:
6958 	case SCMD_WRITE_G4:
6959 		break;
6960 	default:
6961 		if (bp != NULL) {
6962 			if (bp->b_flags & (B_PHYS | B_PAGEIO))
6963 				bp_mapin(bp);
6964 		}
6965 		break;
6966 	}
6967 	/*
6968 	 * scmd->satacmd_flags.sata_data_direction default -
6969 	 * SATA_DIR_NODATA_XFER - is set by
6970 	 * sata_txlt_generic_pkt_info().
6971 	 */
6972 	if (scmd->satacmd_bp) {
6973 		if (scmd->satacmd_bp->b_flags & B_READ) {
6974 			scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
6975 		} else {
6976 			scmd->satacmd_flags.sata_data_direction =
6977 			    SATA_DIR_WRITE;
6978 		}
6979 	}
6980 
6981 	/*
6982 	 * Set up ATAPI packet command.
6983 	 */
6984 
6985 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
6986 
6987 	/* Copy cdb into sata_cmd */
6988 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
6989 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
6990 	bcopy(cdbp, scmd->satacmd_acdb, cdblen);
6991 
6992 	/* See note in the command header */
6993 	if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) {
6994 		if (scmd->satacmd_acdb[3] != 0)
6995 			scmd->satacmd_acdb[4] = 255;
6996 	}
6997 
6998 #ifdef SATA_DEBUG
6999 	if (sata_debug_flags & SATA_DBG_ATAPI) {
7000 		uint8_t *p = scmd->satacmd_acdb;
7001 		char buf[3 * SATA_ATAPI_MAX_CDB_LEN];
7002 
7003 		(void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN,
7004 		    "%02x %02x %02x %02x %02x %02x %02x %02x "
7005 		    "%2x %02x %02x %02x %02x %02x %02x %02x",
7006 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7007 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7008 		buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0';
7009 		cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf);
7010 	}
7011 #endif
7012 
7013 	/*
7014 	 * Preset request sense data to NO SENSE.
7015 	 * If there is no way to get error information via Request Sense,
7016 	 * the packet request sense data would not have to be modified by HBA,
7017 	 * but it could be returned as is.
7018 	 */
7019 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
7020 	sata_fixed_sense_data_preset(
7021 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7022 
7023 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
7024 		/* Need callback function */
7025 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion;
7026 		synch = FALSE;
7027 	} else
7028 		synch = TRUE;
7029 
7030 	/* Transfer command to HBA */
7031 	if (sata_hba_start(spx, &rval) != 0) {
7032 		/* Pkt not accepted for execution */
7033 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
7034 		return (rval);
7035 	}
7036 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
7037 	/*
7038 	 * If execution is non-synchronous,
7039 	 * a callback function will handle potential errors, translate
7040 	 * the response and will do a callback to a target driver.
7041 	 * If it was synchronous, use the same framework callback to check
7042 	 * an execution status.
7043 	 */
7044 	if (synch) {
7045 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7046 		    "synchronous execution status %x\n",
7047 		    spx->txlt_sata_pkt->satapkt_reason);
7048 		sata_txlt_atapi_completion(spx->txlt_sata_pkt);
7049 	}
7050 	return (TRAN_ACCEPT);
7051 }
7052 
7053 
7054 /*
7055  * ATAPI Packet command completion.
7056  *
7057  * Failure of the command passed via Packet command are considered device
7058  * error. SATA HBA driver would have to retrieve error data (via Request
7059  * Sense command delivered via error retrieval sata packet) and copy it
7060  * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data.
7061  */
7062 static void
7063 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt)
7064 {
7065 	sata_pkt_txlate_t *spx =
7066 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7067 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7068 	struct scsi_extended_sense *sense;
7069 	struct buf *bp;
7070 	int rval;
7071 
7072 #ifdef SATA_DEBUG
7073 	uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense;
7074 #endif
7075 
7076 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7077 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7078 
7079 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7080 		/* Normal completion */
7081 		if (sata_pkt->satapkt_cmd.satacmd_bp != NULL)
7082 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
7083 		scsipkt->pkt_reason = CMD_CMPLT;
7084 		*scsipkt->pkt_scbp = STATUS_GOOD;
7085 		if (spx->txlt_tmp_buf != NULL) {
7086 			/* Temporary buffer was used */
7087 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7088 			if (bp->b_flags & B_READ) {
7089 				rval = ddi_dma_sync(
7090 				    spx->txlt_buf_dma_handle, 0, 0,
7091 				    DDI_DMA_SYNC_FORCPU);
7092 				ASSERT(rval == DDI_SUCCESS);
7093 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7094 				    bp->b_bcount);
7095 			}
7096 		}
7097 	} else {
7098 		/*
7099 		 * Something went wrong - analyze return
7100 		 */
7101 		*scsipkt->pkt_scbp = STATUS_CHECK;
7102 		sense = sata_arq_sense(spx);
7103 
7104 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7105 			scsipkt->pkt_reason = CMD_INCOMPLETE;
7106 			/*
7107 			 * We may not have ARQ data if there was a double
7108 			 * error. But sense data in sata packet was pre-set
7109 			 * with NO SENSE so it is valid even if HBA could
7110 			 * not retrieve a real sense data.
7111 			 * Just copy this sense data into scsi pkt sense area.
7112 			 */
7113 			bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense,
7114 			    SATA_ATAPI_MIN_RQSENSE_LEN);
7115 #ifdef SATA_DEBUG
7116 			if (sata_debug_flags & SATA_DBG_SCSI_IF) {
7117 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7118 				    "sata_txlt_atapi_completion: %02x\n"
7119 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
7120 				    "          %02x %02x %02x %02x %02x %02x "
7121 				    "          %02x %02x %02x %02x %02x %02x\n",
7122 				    scsipkt->pkt_reason,
7123 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7124 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7125 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7126 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7127 				    rqsp[16], rqsp[17]);
7128 			}
7129 #endif
7130 		} else {
7131 			switch (sata_pkt->satapkt_reason) {
7132 			case SATA_PKT_PORT_ERROR:
7133 				/*
7134 				 * We have no device data.
7135 				 */
7136 				scsipkt->pkt_reason = CMD_INCOMPLETE;
7137 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
7138 				    STATE_GOT_TARGET | STATE_SENT_CMD |
7139 				    STATE_GOT_STATUS);
7140 				sense->es_key = KEY_HARDWARE_ERROR;
7141 				break;
7142 
7143 			case SATA_PKT_TIMEOUT:
7144 				scsipkt->pkt_reason = CMD_TIMEOUT;
7145 				scsipkt->pkt_statistics |=
7146 				    STAT_TIMEOUT | STAT_DEV_RESET;
7147 				/*
7148 				 * Need to check if HARDWARE_ERROR/
7149 				 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more
7150 				 * appropriate.
7151 				 */
7152 				break;
7153 
7154 			case SATA_PKT_ABORTED:
7155 				scsipkt->pkt_reason = CMD_ABORTED;
7156 				scsipkt->pkt_statistics |= STAT_ABORTED;
7157 				/* Should we set key COMMAND_ABPRTED? */
7158 				break;
7159 
7160 			case SATA_PKT_RESET:
7161 				scsipkt->pkt_reason = CMD_RESET;
7162 				scsipkt->pkt_statistics |= STAT_DEV_RESET;
7163 				/*
7164 				 * May be we should set Unit Attention /
7165 				 * Reset. Perhaps the same should be
7166 				 * returned for disks....
7167 				 */
7168 				sense->es_key = KEY_UNIT_ATTENTION;
7169 				sense->es_add_code = SD_SCSI_ASC_RESET;
7170 				break;
7171 
7172 			default:
7173 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7174 				    "sata_txlt_atapi_completion: "
7175 				    "invalid packet completion reason"));
7176 				scsipkt->pkt_reason = CMD_TRAN_ERR;
7177 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
7178 				    STATE_GOT_TARGET | STATE_SENT_CMD |
7179 				    STATE_GOT_STATUS);
7180 				break;
7181 			}
7182 		}
7183 	}
7184 
7185 	SATAATAPITRACE(spx, 0);
7186 
7187 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7188 	    scsipkt->pkt_comp != NULL) {
7189 		/* scsi callback required */
7190 		(*scsipkt->pkt_comp)(scsipkt);
7191 	}
7192 }
7193 
7194 /*
7195  * Set up error retrieval sata command for ATAPI Packet Command error data
7196  * recovery.
7197  *
7198  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
7199  * returns SATA_FAILURE otherwise.
7200  */
7201 
7202 static int
7203 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
7204 {
7205 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
7206 	sata_cmd_t *scmd;
7207 	struct buf *bp;
7208 
7209 	/*
7210 	 * Allocate dma-able buffer error data.
7211 	 * Buffer allocation will take care of buffer alignment and other DMA
7212 	 * attributes.
7213 	 */
7214 	bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN);
7215 	if (bp == NULL) {
7216 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
7217 		    "sata_get_err_retrieval_pkt: "
7218 		    "cannot allocate buffer for error data", NULL);
7219 		return (SATA_FAILURE);
7220 	}
7221 	bp_mapin(bp); /* make data buffer accessible */
7222 
7223 	/* Operation modes are up to the caller */
7224 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7225 
7226 	/* Synchronous mode, no callback - may be changed by the caller */
7227 	spkt->satapkt_comp = NULL;
7228 	spkt->satapkt_time = sata_default_pkt_time;
7229 
7230 	scmd = &spkt->satapkt_cmd;
7231 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7232 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7233 
7234 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7235 
7236 	/*
7237 	 * Set-up acdb. Request Sense CDB (packet command content) is
7238 	 * not in DMA-able buffer. Its handling is HBA-specific (how
7239 	 * it is transfered into packet FIS).
7240 	 */
7241 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7242 	bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN);
7243 	/* Following zeroing of pad bytes may not be necessary */
7244 	bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN],
7245 	    sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN);
7246 
7247 	/*
7248 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
7249 	 * before accessing it. Handle is in usual place in translate struct.
7250 	 */
7251 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
7252 
7253 	/*
7254 	 * Preset request sense data to NO SENSE.
7255 	 * Here it is redundant, only for a symetry with scsi-originated
7256 	 * packets. It should not be used for anything but debugging.
7257 	 */
7258 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
7259 	sata_fixed_sense_data_preset(
7260 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7261 
7262 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7263 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7264 
7265 	return (SATA_SUCCESS);
7266 }
7267 
7268 /*
7269  * Set-up ATAPI packet command.
7270  * Data transfer direction has to be set-up in sata_cmd structure prior to
7271  * calling this function.
7272  *
7273  * Returns void
7274  */
7275 
7276 static void
7277 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo)
7278 {
7279 	scmd->satacmd_addr_type = 0;		/* N/A */
7280 	scmd->satacmd_sec_count_lsb = 0;	/* no tag */
7281 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
7282 	scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ;
7283 	scmd->satacmd_lba_high_lsb =
7284 	    (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8);
7285 	scmd->satacmd_cmd_reg = SATAC_PACKET;	/* Command */
7286 
7287 	/*
7288 	 * We want all data to be transfered via DMA.
7289 	 * But specify it only if drive supports DMA and DMA mode is
7290 	 * selected - some drives are sensitive about it.
7291 	 * Hopefully it wil work for all drives....
7292 	 */
7293 	if (sdinfo->satadrv_settings & SATA_DEV_DMA)
7294 		scmd->satacmd_features_reg = SATA_ATAPI_F_DMA;
7295 
7296 	/*
7297 	 * Features register requires special care for devices that use
7298 	 * Serial ATA bridge - they need an explicit specification of
7299 	 * the data transfer direction for Packet DMA commands.
7300 	 * Setting this bit is harmless if DMA is not used.
7301 	 *
7302 	 * Many drives do not implement word 80, specifying what ATA/ATAPI
7303 	 * spec they follow.
7304 	 * We are arbitrarily following the latest SerialATA 2.6 spec,
7305 	 * which uses ATA/ATAPI 6 specification for Identify Data, unless
7306 	 * ATA/ATAPI-7 support is explicitly indicated.
7307 	 */
7308 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
7309 	    sdinfo->satadrv_id.ai_majorversion != 0xffff &&
7310 	    (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) {
7311 		/*
7312 		 * Specification of major version is valid and version 7
7313 		 * is supported. It does automatically imply that all
7314 		 * spec features are supported. For now, we assume that
7315 		 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete.
7316 		 */
7317 		if ((sdinfo->satadrv_id.ai_dirdma &
7318 		    SATA_ATAPI_ID_DMADIR_REQ) != 0) {
7319 			if (scmd->satacmd_flags.sata_data_direction ==
7320 			    SATA_DIR_READ)
7321 			scmd->satacmd_features_reg |=
7322 			    SATA_ATAPI_F_DATA_DIR_READ;
7323 		}
7324 	}
7325 }
7326 
7327 
7328 #ifdef SATA_DEBUG
7329 
7330 /* Display 18 bytes of Inquiry data */
7331 static void
7332 sata_show_inqry_data(uint8_t *buf)
7333 {
7334 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
7335 	uint8_t *p;
7336 
7337 	cmn_err(CE_NOTE, "Inquiry data:");
7338 	cmn_err(CE_NOTE, "device type %x", inq->inq_dtype);
7339 	cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb);
7340 	cmn_err(CE_NOTE, "version %x", inq->inq_ansi);
7341 	cmn_err(CE_NOTE, "ATAPI transport version %d",
7342 	    SATA_ATAPI_TRANS_VERSION(inq));
7343 	cmn_err(CE_NOTE, "response data format %d, aenc %d",
7344 	    inq->inq_rdf, inq->inq_aenc);
7345 	cmn_err(CE_NOTE, " additional length %d", inq->inq_len);
7346 	cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs);
7347 	p = (uint8_t *)inq->inq_vid;
7348 	cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x "
7349 	    "%02x %02x %02x %02x",
7350 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
7351 	p = (uint8_t *)inq->inq_vid;
7352 	cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c",
7353 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
7354 
7355 	p = (uint8_t *)inq->inq_pid;
7356 	cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x "
7357 	    "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
7358 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7359 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7360 	p = (uint8_t *)inq->inq_pid;
7361 	cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c "
7362 	    "%c %c %c %c %c %c %c %c",
7363 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7364 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7365 
7366 	p = (uint8_t *)inq->inq_revision;
7367 	cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x",
7368 	    p[0], p[1], p[2], p[3]);
7369 	p = (uint8_t *)inq->inq_revision;
7370 	cmn_err(CE_NOTE, "revision: %c %c %c %c",
7371 	    p[0], p[1], p[2], p[3]);
7372 
7373 }
7374 
7375 
7376 static void
7377 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count)
7378 {
7379 	struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt;
7380 
7381 	if (scsi_pkt == NULL)
7382 		return;
7383 	if (count != 0) {
7384 		/* saving cdb */
7385 		bzero(sata_atapi_trace[sata_atapi_trace_index].acdb,
7386 		    SATA_ATAPI_MAX_CDB_LEN);
7387 		bcopy(scsi_pkt->pkt_cdbp,
7388 		    sata_atapi_trace[sata_atapi_trace_index].acdb, count);
7389 	} else {
7390 		bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)->
7391 		    sts_sensedata,
7392 		    sata_atapi_trace[sata_atapi_trace_index].arqs,
7393 		    SATA_ATAPI_MIN_RQSENSE_LEN);
7394 		sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason =
7395 		    scsi_pkt->pkt_reason;
7396 		sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason =
7397 		    spx->txlt_sata_pkt->satapkt_reason;
7398 
7399 		if (++sata_atapi_trace_index >= 64)
7400 			sata_atapi_trace_index = 0;
7401 	}
7402 }
7403 
7404 #endif
7405 
7406 /*
7407  * Fetch inquiry data from ATAPI device
7408  * Returns SATA_SUCCESS if operation was successfull, SATA_FAILURE otherwise.
7409  *
7410  * Note:
7411  * inqb pointer does not point to a DMA-able buffer. It is a local buffer
7412  * where the caller expects to see the inquiry data.
7413  *
7414  */
7415 
7416 static int
7417 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba,
7418     sata_address_t *saddr, struct scsi_inquiry *inq)
7419 {
7420 	sata_pkt_txlate_t *spx;
7421 	sata_pkt_t *spkt;
7422 	struct buf *bp;
7423 	sata_drive_info_t *sdinfo;
7424 	sata_cmd_t *scmd;
7425 	int rval;
7426 	uint8_t *rqsp;
7427 #ifdef SATA_DEBUG
7428 	char msg_buf[MAXPATHLEN];
7429 #endif
7430 
7431 	ASSERT(sata_hba != NULL);
7432 
7433 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
7434 	spx->txlt_sata_hba_inst = sata_hba;
7435 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
7436 	spkt = sata_pkt_alloc(spx, NULL);
7437 	if (spkt == NULL) {
7438 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7439 		return (SATA_FAILURE);
7440 	}
7441 	/* address is needed now */
7442 	spkt->satapkt_device.satadev_addr = *saddr;
7443 
7444 	/* scsi_inquiry size buffer */
7445 	bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry));
7446 	if (bp == NULL) {
7447 		sata_pkt_free(spx);
7448 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7449 		SATA_LOG_D((sata_hba, CE_WARN,
7450 		    "sata_get_atapi_inquiry_data: "
7451 		    "cannot allocate data buffer"));
7452 		return (SATA_FAILURE);
7453 	}
7454 	bp_mapin(bp); /* make data buffer accessible */
7455 
7456 	scmd = &spkt->satapkt_cmd;
7457 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7458 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7459 
7460 	/* Use synchronous mode */
7461 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7462 	spkt->satapkt_comp = NULL;
7463 	spkt->satapkt_time = sata_default_pkt_time;
7464 
7465 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
7466 
7467 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7468 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7469 
7470 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
7471 	sdinfo = sata_get_device_info(sata_hba,
7472 	    &spx->txlt_sata_pkt->satapkt_device);
7473 	if (sdinfo == NULL) {
7474 		/* we have to be carefull about the disapearing device */
7475 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7476 		rval = SATA_FAILURE;
7477 		goto cleanup;
7478 	}
7479 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7480 
7481 	/*
7482 	 * Set-up acdb. This works for atapi transport version 2 and later.
7483 	 */
7484 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7485 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7486 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
7487 	scmd->satacmd_acdb[1] = 0x00;
7488 	scmd->satacmd_acdb[2] = 0x00;
7489 	scmd->satacmd_acdb[3] = 0x00;
7490 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
7491 	scmd->satacmd_acdb[5] = 0x00;
7492 
7493 	sata_fixed_sense_data_preset(
7494 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7495 
7496 	/* Transfer command to HBA */
7497 	if (sata_hba_start(spx, &rval) != 0) {
7498 		/* Pkt not accepted for execution */
7499 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
7500 		    "sata_get_atapi_inquiry_data: "
7501 		    "Packet not accepted for execution - ret: %02x", rval);
7502 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7503 		rval = SATA_FAILURE;
7504 		goto cleanup;
7505 	}
7506 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7507 
7508 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
7509 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
7510 		    "sata_get_atapi_inquiry_data: "
7511 		    "Packet completed successfully - ret: %02x", rval);
7512 		/*
7513 		 * Sync buffer. Handle is in usual place in translate struct.
7514 		 * Normal completion - copy data into caller's buffer
7515 		 */
7516 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
7517 		    DDI_DMA_SYNC_FORCPU);
7518 		ASSERT(rval == DDI_SUCCESS);
7519 		bcopy(bp->b_un.b_addr, (uint8_t *)inq,
7520 		    sizeof (struct scsi_inquiry));
7521 #ifdef SATA_DEBUG
7522 		if (sata_debug_flags & SATA_DBG_ATAPI) {
7523 			sata_show_inqry_data((uint8_t *)inq);
7524 		}
7525 #endif
7526 		rval = SATA_SUCCESS;
7527 	} else {
7528 		/*
7529 		 * Something went wrong - analyze return - check rqsense data
7530 		 */
7531 		rval = SATA_FAILURE;
7532 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7533 			/*
7534 			 * ARQ data hopefull show something other than NO SENSE
7535 			 */
7536 			rqsp = scmd->satacmd_rqsense;
7537 #ifdef SATA_DEBUG
7538 			if (sata_debug_flags & SATA_DBG_ATAPI) {
7539 				msg_buf[0] = '\0';
7540 				(void) snprintf(msg_buf, MAXPATHLEN,
7541 				    "ATAPI packet completion reason: %02x\n"
7542 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x\n"
7543 				    "          %02x %02x %02x %02x %02x %02x\n"
7544 				    "          %02x %02x %02x %02x %02x %02x",
7545 				    spkt->satapkt_reason,
7546 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7547 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7548 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7549 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7550 				    rqsp[16], rqsp[17]);
7551 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7552 				    "%s", msg_buf);
7553 			}
7554 #endif
7555 		} else {
7556 			switch (spkt->satapkt_reason) {
7557 			case SATA_PKT_PORT_ERROR:
7558 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7559 				    "sata_get_atapi_inquiry_data: "
7560 				    "packet reason: port error", NULL);
7561 				break;
7562 
7563 			case SATA_PKT_TIMEOUT:
7564 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7565 				    "sata_get_atapi_inquiry_data: "
7566 				    "packet reason: timeout", NULL);
7567 				break;
7568 
7569 			case SATA_PKT_ABORTED:
7570 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7571 				    "sata_get_atapi_inquiry_data: "
7572 				    "packet reason: aborted", NULL);
7573 				break;
7574 
7575 			case SATA_PKT_RESET:
7576 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7577 				    "sata_get_atapi_inquiry_data: "
7578 				    "packet reason: reset\n", NULL);
7579 				break;
7580 			default:
7581 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7582 				    "sata_get_atapi_inquiry_data: "
7583 				    "invalid packet reason: %02x\n",
7584 				    spkt->satapkt_reason);
7585 				break;
7586 			}
7587 		}
7588 	}
7589 cleanup:
7590 	sata_free_local_buffer(spx);
7591 	sata_pkt_free(spx);
7592 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
7593 	return (rval);
7594 }
7595 
7596 
7597 
7598 
7599 
7600 #if 0
7601 #ifdef SATA_DEBUG
7602 
7603 /*
7604  * Test ATAPI packet command.
7605  * Single threaded test: send packet command in synch mode, process completion
7606  *
7607  */
7608 static void
7609 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport)
7610 {
7611 	sata_pkt_txlate_t *spx;
7612 	sata_pkt_t *spkt;
7613 	struct buf *bp;
7614 	sata_device_t sata_device;
7615 	sata_drive_info_t *sdinfo;
7616 	sata_cmd_t *scmd;
7617 	int rval;
7618 	uint8_t *rqsp;
7619 
7620 	ASSERT(sata_hba_inst != NULL);
7621 	sata_device.satadev_addr.cport = cport;
7622 	sata_device.satadev_addr.pmport = 0;
7623 	sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
7624 	sata_device.satadev_rev = SATA_DEVICE_REV;
7625 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7626 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
7627 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7628 	if (sdinfo == NULL) {
7629 		sata_log(sata_hba_inst, CE_WARN,
7630 		    "sata_test_atapi_packet_command: "
7631 		    "no device info for cport %d",
7632 		    sata_device.satadev_addr.cport);
7633 		return;
7634 	}
7635 
7636 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
7637 	spx->txlt_sata_hba_inst = sata_hba_inst;
7638 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
7639 	spkt = sata_pkt_alloc(spx, NULL);
7640 	if (spkt == NULL) {
7641 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7642 		return;
7643 	}
7644 	/* address is needed now */
7645 	spkt->satapkt_device.satadev_addr = sata_device.satadev_addr;
7646 
7647 	/* 1024k buffer */
7648 	bp = sata_alloc_local_buffer(spx, 1024);
7649 	if (bp == NULL) {
7650 		sata_pkt_free(spx);
7651 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7652 		sata_log(sata_hba_inst, CE_WARN,
7653 		    "sata_test_atapi_packet_command: "
7654 		    "cannot allocate data buffer");
7655 		return;
7656 	}
7657 	bp_mapin(bp); /* make data buffer accessible */
7658 
7659 	scmd = &spkt->satapkt_cmd;
7660 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7661 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7662 
7663 	/* Use synchronous mode */
7664 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7665 
7666 	/* Synchronous mode, no callback - may be changed by the caller */
7667 	spkt->satapkt_comp = NULL;
7668 	spkt->satapkt_time = sata_default_pkt_time;
7669 
7670 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
7671 
7672 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7673 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7674 
7675 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7676 
7677 	/* Set-up acdb. */
7678 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7679 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7680 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
7681 	scmd->satacmd_acdb[1] = 0x00;
7682 	scmd->satacmd_acdb[2] = 0x00;
7683 	scmd->satacmd_acdb[3] = 0x00;
7684 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
7685 	scmd->satacmd_acdb[5] = 0x00;
7686 
7687 	sata_fixed_sense_data_preset(
7688 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7689 
7690 	/* Transfer command to HBA */
7691 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7692 	if (sata_hba_start(spx, &rval) != 0) {
7693 		/* Pkt not accepted for execution */
7694 		sata_log(sata_hba_inst, CE_WARN,
7695 		    "sata_test_atapi_packet_command: "
7696 		    "Packet not accepted for execution - ret: %02x", rval);
7697 		mutex_exit(
7698 		    &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7699 		goto cleanup;
7700 	}
7701 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7702 
7703 	/*
7704 	 * Sync buffer. Handle is in usual place in translate struct.
7705 	 */
7706 	rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
7707 	    DDI_DMA_SYNC_FORCPU);
7708 	ASSERT(rval == DDI_SUCCESS);
7709 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
7710 		sata_log(sata_hba_inst, CE_WARN,
7711 		    "sata_test_atapi_packet_command: "
7712 		    "Packet completed successfully");
7713 		/*
7714 		 * Normal completion - show inquiry data
7715 		 */
7716 		sata_show_inqry_data((uint8_t *)bp->b_un.b_addr);
7717 	} else {
7718 		/*
7719 		 * Something went wrong - analyze return - check rqsense data
7720 		 */
7721 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7722 			/*
7723 			 * ARQ data hopefull show something other than NO SENSE
7724 			 */
7725 			rqsp = scmd->satacmd_rqsense;
7726 			sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7727 			    "ATAPI packet completion reason: %02x\n"
7728 			    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
7729 			    "          %02x %02x %02x %02x %02x %02x "
7730 			    "          %02x %02x %02x %02x %02x %02x\n",
7731 			    spkt->satapkt_reason,
7732 			    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7733 			    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7734 			    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7735 			    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7736 			    rqsp[16], rqsp[17]);
7737 		} else {
7738 			switch (spkt->satapkt_reason) {
7739 			case SATA_PKT_PORT_ERROR:
7740 				sata_log(sata_hba_inst, CE_WARN,
7741 				    "sata_test_atapi_packet_command: "
7742 				    "packet reason: port error\n");
7743 				break;
7744 
7745 			case SATA_PKT_TIMEOUT:
7746 				sata_log(sata_hba_inst, CE_WARN,
7747 				    "sata_test_atapi_packet_command: "
7748 				    "packet reason: timeout\n");
7749 				break;
7750 
7751 			case SATA_PKT_ABORTED:
7752 				sata_log(sata_hba_inst, CE_WARN,
7753 				    "sata_test_atapi_packet_command: "
7754 				    "packet reason: aborted\n");
7755 				break;
7756 
7757 			case SATA_PKT_RESET:
7758 				sata_log(sata_hba_inst, CE_WARN,
7759 				    "sata_test_atapi_packet_command: "
7760 				    "packet reason: reset\n");
7761 				break;
7762 			default:
7763 				sata_log(sata_hba_inst, CE_WARN,
7764 				    "sata_test_atapi_packet_command: "
7765 				    "invalid packet reason: %02x\n",
7766 				    spkt->satapkt_reason);
7767 				break;
7768 			}
7769 		}
7770 	}
7771 cleanup:
7772 	sata_free_local_buffer(spx);
7773 	sata_pkt_free(spx);
7774 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
7775 }
7776 
7777 #endif /* SATA_DEBUG */
7778 #endif /* 1 */
7779 
7780 
7781 /* ************************** LOCAL HELPER FUNCTIONS *********************** */
7782 
7783 /*
7784  * Validate sata_tran info
7785  * SATA_FAILURE returns if structure is inconsistent or structure revision
7786  * does not match one used by the framework.
7787  *
7788  * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains
7789  * required function pointers.
7790  * Returns SATA_FAILURE otherwise.
7791  */
7792 static int
7793 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran)
7794 {
7795 	/*
7796 	 * SATA_TRAN_HBA_REV is the current (highest) revision number
7797 	 * of the SATA interface.
7798 	 */
7799 	if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) {
7800 		sata_log(NULL, CE_WARN,
7801 		    "sata: invalid sata_hba_tran version %d for driver %s",
7802 		    sata_tran->sata_tran_hba_rev, ddi_driver_name(dip));
7803 		return (SATA_FAILURE);
7804 	}
7805 
7806 	if (dip != sata_tran->sata_tran_hba_dip) {
7807 		SATA_LOG_D((NULL, CE_WARN,
7808 		    "sata: inconsistent sata_tran_hba_dip "
7809 		    "%p / %p", sata_tran->sata_tran_hba_dip, dip));
7810 		return (SATA_FAILURE);
7811 	}
7812 
7813 	if (sata_tran->sata_tran_probe_port == NULL ||
7814 	    sata_tran->sata_tran_start == NULL ||
7815 	    sata_tran->sata_tran_abort == NULL ||
7816 	    sata_tran->sata_tran_reset_dport == NULL ||
7817 	    sata_tran->sata_tran_hotplug_ops == NULL ||
7818 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL ||
7819 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate ==
7820 	    NULL) {
7821 		SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing "
7822 		    "required functions"));
7823 	}
7824 	return (SATA_SUCCESS);
7825 }
7826 
7827 /*
7828  * Remove HBA instance from sata_hba_list.
7829  */
7830 static void
7831 sata_remove_hba_instance(dev_info_t *dip)
7832 {
7833 	sata_hba_inst_t	*sata_hba_inst;
7834 
7835 	mutex_enter(&sata_mutex);
7836 	for (sata_hba_inst = sata_hba_list;
7837 	    sata_hba_inst != (struct sata_hba_inst *)NULL;
7838 	    sata_hba_inst = sata_hba_inst->satahba_next) {
7839 		if (sata_hba_inst->satahba_dip == dip)
7840 			break;
7841 	}
7842 
7843 	if (sata_hba_inst == (struct sata_hba_inst *)NULL) {
7844 #ifdef SATA_DEBUG
7845 		cmn_err(CE_WARN, "sata_remove_hba_instance: "
7846 		    "unknown HBA instance\n");
7847 #endif
7848 		ASSERT(FALSE);
7849 	}
7850 	if (sata_hba_inst == sata_hba_list) {
7851 		sata_hba_list = sata_hba_inst->satahba_next;
7852 		if (sata_hba_list) {
7853 			sata_hba_list->satahba_prev =
7854 			    (struct sata_hba_inst *)NULL;
7855 		}
7856 		if (sata_hba_inst == sata_hba_list_tail) {
7857 			sata_hba_list_tail = NULL;
7858 		}
7859 	} else if (sata_hba_inst == sata_hba_list_tail) {
7860 		sata_hba_list_tail = sata_hba_inst->satahba_prev;
7861 		if (sata_hba_list_tail) {
7862 			sata_hba_list_tail->satahba_next =
7863 			    (struct sata_hba_inst *)NULL;
7864 		}
7865 	} else {
7866 		sata_hba_inst->satahba_prev->satahba_next =
7867 		    sata_hba_inst->satahba_next;
7868 		sata_hba_inst->satahba_next->satahba_prev =
7869 		    sata_hba_inst->satahba_prev;
7870 	}
7871 	mutex_exit(&sata_mutex);
7872 }
7873 
7874 
7875 
7876 
7877 
7878 /*
7879  * Probe all SATA ports of the specified HBA instance.
7880  * The assumption is that there are no target and attachment point minor nodes
7881  * created by the boot subsystems, so we do not need to prune device tree.
7882  *
7883  * This function is called only from sata_hba_attach(). It does not have to
7884  * be protected by controller mutex, because the hba_attached flag is not set
7885  * yet and no one would be touching this HBA instance other than this thread.
7886  * Determines if port is active and what type of the device is attached
7887  * (if any). Allocates necessary structures for each port.
7888  *
7889  * An AP (Attachement Point) node is created for each SATA device port even
7890  * when there is no device attached.
7891  */
7892 
7893 static 	void
7894 sata_probe_ports(sata_hba_inst_t *sata_hba_inst)
7895 {
7896 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
7897 	int			ncport, npmport;
7898 	sata_cport_info_t 	*cportinfo;
7899 	sata_drive_info_t	*drive;
7900 	sata_pmult_info_t	*pminfo;
7901 	sata_pmport_info_t 	*pmportinfo;
7902 	sata_device_t		sata_device;
7903 	int			rval;
7904 	dev_t			minor_number;
7905 	char			name[16];
7906 	clock_t			start_time, cur_time;
7907 
7908 	/*
7909 	 * Probe controller ports first, to find port status and
7910 	 * any port multiplier attached.
7911 	 */
7912 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
7913 		/* allocate cport structure */
7914 		cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP);
7915 		ASSERT(cportinfo != NULL);
7916 		mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL);
7917 
7918 		mutex_enter(&cportinfo->cport_mutex);
7919 
7920 		cportinfo->cport_addr.cport = ncport;
7921 		cportinfo->cport_addr.pmport = 0;
7922 		cportinfo->cport_addr.qual = SATA_ADDR_CPORT;
7923 		cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
7924 		cportinfo->cport_state |= SATA_STATE_PROBING;
7925 		SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo;
7926 
7927 		/*
7928 		 * Regardless if a port is usable or not, create
7929 		 * an attachment point
7930 		 */
7931 		mutex_exit(&cportinfo->cport_mutex);
7932 		minor_number =
7933 		    SATA_MAKE_AP_MINOR(ddi_get_instance(dip), ncport, 0, 0);
7934 		(void) sprintf(name, "%d", ncport);
7935 		if (ddi_create_minor_node(dip, name, S_IFCHR,
7936 		    minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) !=
7937 		    DDI_SUCCESS) {
7938 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
7939 			    "cannot create SATA attachment point for port %d",
7940 			    ncport);
7941 		}
7942 
7943 		/* Probe port */
7944 		start_time = ddi_get_lbolt();
7945 	reprobe_cport:
7946 		sata_device.satadev_addr.cport = ncport;
7947 		sata_device.satadev_addr.pmport = 0;
7948 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
7949 		sata_device.satadev_rev = SATA_DEVICE_REV;
7950 
7951 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
7952 		    (dip, &sata_device);
7953 
7954 		mutex_enter(&cportinfo->cport_mutex);
7955 		sata_update_port_scr(&cportinfo->cport_scr, &sata_device);
7956 		if (rval != SATA_SUCCESS) {
7957 			/* Something went wrong? Fail the port */
7958 			cportinfo->cport_state = SATA_PSTATE_FAILED;
7959 			mutex_exit(&cportinfo->cport_mutex);
7960 			continue;
7961 		}
7962 		cportinfo->cport_state &= ~SATA_STATE_PROBING;
7963 		cportinfo->cport_state |= SATA_STATE_PROBED;
7964 		cportinfo->cport_dev_type = sata_device.satadev_type;
7965 
7966 		cportinfo->cport_state |= SATA_STATE_READY;
7967 		if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) {
7968 			mutex_exit(&cportinfo->cport_mutex);
7969 			continue;
7970 		}
7971 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
7972 			/*
7973 			 * There is some device attached.
7974 			 * Allocate device info structure
7975 			 */
7976 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) {
7977 				mutex_exit(&cportinfo->cport_mutex);
7978 				SATA_CPORTINFO_DRV_INFO(cportinfo) =
7979 				    kmem_zalloc(sizeof (sata_drive_info_t),
7980 				    KM_SLEEP);
7981 				mutex_enter(&cportinfo->cport_mutex);
7982 			}
7983 			drive = SATA_CPORTINFO_DRV_INFO(cportinfo);
7984 			drive->satadrv_addr = cportinfo->cport_addr;
7985 			drive->satadrv_addr.qual = SATA_ADDR_DCPORT;
7986 			drive->satadrv_type = cportinfo->cport_dev_type;
7987 			drive->satadrv_state = SATA_STATE_UNKNOWN;
7988 
7989 			mutex_exit(&cportinfo->cport_mutex);
7990 			if (sata_add_device(dip, sata_hba_inst, ncport, 0) !=
7991 			    SATA_SUCCESS) {
7992 				/*
7993 				 * Plugged device was not correctly identified.
7994 				 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT
7995 				 */
7996 				cur_time = ddi_get_lbolt();
7997 				if ((cur_time - start_time) <
7998 				    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
7999 					/* sleep for a while */
8000 					delay(drv_usectohz(
8001 					    SATA_DEV_RETRY_DLY));
8002 					goto reprobe_cport;
8003 				}
8004 			}
8005 		} else {
8006 			mutex_exit(&cportinfo->cport_mutex);
8007 			ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT);
8008 			pminfo = kmem_zalloc(sizeof (sata_pmult_info_t),
8009 			    KM_SLEEP);
8010 			mutex_enter(&cportinfo->cport_mutex);
8011 			ASSERT(pminfo != NULL);
8012 			SATA_CPORTINFO_PMULT_INFO(cportinfo) = pminfo;
8013 			pminfo->pmult_addr.cport = cportinfo->cport_addr.cport;
8014 			pminfo->pmult_addr.pmport = SATA_PMULT_HOSTPORT;
8015 			pminfo->pmult_addr.qual = SATA_ADDR_PMPORT;
8016 			pminfo->pmult_num_dev_ports =
8017 			    sata_device.satadev_add_info;
8018 			mutex_init(&pminfo->pmult_mutex, NULL, MUTEX_DRIVER,
8019 			    NULL);
8020 			pminfo->pmult_state = SATA_STATE_PROBING;
8021 			mutex_exit(&cportinfo->cport_mutex);
8022 
8023 			/* Probe Port Multiplier ports */
8024 			for (npmport = 0;
8025 			    npmport < pminfo->pmult_num_dev_ports;
8026 			    npmport++) {
8027 				pmportinfo = kmem_zalloc(
8028 				    sizeof (sata_pmport_info_t), KM_SLEEP);
8029 				mutex_enter(&cportinfo->cport_mutex);
8030 				ASSERT(pmportinfo != NULL);
8031 				pmportinfo->pmport_addr.cport = ncport;
8032 				pmportinfo->pmport_addr.pmport = npmport;
8033 				pmportinfo->pmport_addr.qual =
8034 				    SATA_ADDR_PMPORT;
8035 				pminfo->pmult_dev_port[npmport] = pmportinfo;
8036 
8037 				mutex_init(&pmportinfo->pmport_mutex, NULL,
8038 				    MUTEX_DRIVER, NULL);
8039 
8040 				mutex_exit(&cportinfo->cport_mutex);
8041 
8042 				/* Create an attachment point */
8043 				minor_number = SATA_MAKE_AP_MINOR(
8044 				    ddi_get_instance(dip), ncport, npmport, 1);
8045 				(void) sprintf(name, "%d.%d", ncport, npmport);
8046 				if (ddi_create_minor_node(dip, name, S_IFCHR,
8047 				    minor_number, DDI_NT_SATA_ATTACHMENT_POINT,
8048 				    0) != DDI_SUCCESS) {
8049 					sata_log(sata_hba_inst, CE_WARN,
8050 					    "sata_hba_attach: "
8051 					    "cannot create SATA attachment "
8052 					    "point for port %d pmult port %d",
8053 					    ncport, npmport);
8054 				}
8055 
8056 				start_time = ddi_get_lbolt();
8057 			reprobe_pmport:
8058 				sata_device.satadev_addr.pmport = npmport;
8059 				sata_device.satadev_addr.qual =
8060 				    SATA_ADDR_PMPORT;
8061 
8062 				rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8063 				    (dip, &sata_device);
8064 				mutex_enter(&cportinfo->cport_mutex);
8065 
8066 				/* sata_update_port_info() */
8067 				sata_update_port_scr(&pmportinfo->pmport_scr,
8068 				    &sata_device);
8069 
8070 				if (rval != SATA_SUCCESS) {
8071 					pmportinfo->pmport_state =
8072 					    SATA_PSTATE_FAILED;
8073 					mutex_exit(&cportinfo->cport_mutex);
8074 					continue;
8075 				}
8076 				pmportinfo->pmport_state &=
8077 				    ~SATA_STATE_PROBING;
8078 				pmportinfo->pmport_state |= SATA_STATE_PROBED;
8079 				pmportinfo->pmport_dev_type =
8080 				    sata_device.satadev_type;
8081 
8082 				pmportinfo->pmport_state |= SATA_STATE_READY;
8083 				if (pmportinfo->pmport_dev_type ==
8084 				    SATA_DTYPE_NONE) {
8085 					mutex_exit(&cportinfo->cport_mutex);
8086 					continue;
8087 				}
8088 				/* Port multipliers cannot be chained */
8089 				ASSERT(pmportinfo->pmport_dev_type !=
8090 				    SATA_DTYPE_PMULT);
8091 				/*
8092 				 * There is something attached to Port
8093 				 * Multiplier device port
8094 				 * Allocate device info structure
8095 				 */
8096 				if (pmportinfo->pmport_sata_drive == NULL) {
8097 					mutex_exit(&cportinfo->cport_mutex);
8098 					pmportinfo->pmport_sata_drive =
8099 					    kmem_zalloc(
8100 					    sizeof (sata_drive_info_t),
8101 					    KM_SLEEP);
8102 					mutex_enter(&cportinfo->cport_mutex);
8103 				}
8104 				drive = pmportinfo->pmport_sata_drive;
8105 				drive->satadrv_addr.cport =
8106 				    pmportinfo->pmport_addr.cport;
8107 				drive->satadrv_addr.pmport = npmport;
8108 				drive->satadrv_addr.qual = SATA_ADDR_DPMPORT;
8109 				drive->satadrv_type = pmportinfo->
8110 				    pmport_dev_type;
8111 				drive->satadrv_state = SATA_STATE_UNKNOWN;
8112 
8113 				mutex_exit(&cportinfo->cport_mutex);
8114 				if (sata_add_device(dip, sata_hba_inst, ncport,
8115 				    npmport) != SATA_SUCCESS) {
8116 					/*
8117 					 * Plugged device was not correctly
8118 					 * identified. Retry, within the
8119 					 * SATA_DEV_IDENTIFY_TIMEOUT
8120 					 */
8121 					cur_time = ddi_get_lbolt();
8122 					if ((cur_time - start_time) <
8123 					    drv_usectohz(
8124 					    SATA_DEV_IDENTIFY_TIMEOUT)) {
8125 						/* sleep for a while */
8126 						delay(drv_usectohz(
8127 						    SATA_DEV_RETRY_DLY));
8128 						goto reprobe_pmport;
8129 					}
8130 				}
8131 			}
8132 			pmportinfo->pmport_state =
8133 			    SATA_STATE_PROBED | SATA_STATE_READY;
8134 		}
8135 	}
8136 }
8137 
8138 /*
8139  * Add SATA device for specified HBA instance & port (SCSI target
8140  * device nodes).
8141  * This function is called (indirectly) only from sata_hba_attach().
8142  * A target node is created when there is a supported type device attached,
8143  * but may be removed if it cannot be put online.
8144  *
8145  * This function cannot be called from an interrupt context.
8146  *
8147  * ONLY DISK TARGET NODES ARE CREATED NOW
8148  *
8149  * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when
8150  * device identification failed - adding a device could be retried.
8151  *
8152  */
8153 static 	int
8154 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst, int cport,
8155     int pmport)
8156 {
8157 	sata_cport_info_t 	*cportinfo;
8158 	sata_pmult_info_t	*pminfo;
8159 	sata_pmport_info_t	*pmportinfo;
8160 	dev_info_t		*cdip;		/* child dip */
8161 	sata_device_t		sata_device;
8162 	int			rval;
8163 
8164 
8165 
8166 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
8167 	ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE);
8168 	mutex_enter(&cportinfo->cport_mutex);
8169 	/*
8170 	 * Some device is attached to a controller port.
8171 	 * We rely on controllers distinquishing between no-device,
8172 	 * attached port multiplier and other kind of attached device.
8173 	 * We need to get Identify Device data and determine
8174 	 * positively the dev type before trying to attach
8175 	 * the target driver.
8176 	 */
8177 	sata_device.satadev_rev = SATA_DEVICE_REV;
8178 	if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
8179 		/*
8180 		 * Not port multiplier.
8181 		 */
8182 		sata_device.satadev_addr = cportinfo->cport_addr;
8183 		sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
8184 		mutex_exit(&cportinfo->cport_mutex);
8185 
8186 		rval = sata_probe_device(sata_hba_inst, &sata_device);
8187 		if (rval != SATA_SUCCESS ||
8188 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN)
8189 			return (SATA_FAILURE);
8190 
8191 		mutex_enter(&cportinfo->cport_mutex);
8192 		sata_show_drive_info(sata_hba_inst,
8193 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
8194 
8195 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
8196 			/*
8197 			 * Could not determine device type or
8198 			 * a device is not supported.
8199 			 * Degrade this device to unknown.
8200 			 */
8201 			cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8202 			mutex_exit(&cportinfo->cport_mutex);
8203 			return (SATA_SUCCESS);
8204 		}
8205 		cportinfo->cport_dev_type = sata_device.satadev_type;
8206 		cportinfo->cport_tgtnode_clean = B_TRUE;
8207 		mutex_exit(&cportinfo->cport_mutex);
8208 
8209 		/*
8210 		 * Initialize device to the desired state. Even if it
8211 		 * fails, the device will still attach but syslog
8212 		 * will show the warning.
8213 		 */
8214 		if (sata_initialize_device(sata_hba_inst,
8215 		    SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS)
8216 			/* Retry */
8217 			(void) sata_initialize_device(sata_hba_inst,
8218 			    SATA_CPORTINFO_DRV_INFO(cportinfo));
8219 
8220 		cdip = sata_create_target_node(pdip, sata_hba_inst,
8221 		    &sata_device.satadev_addr);
8222 		mutex_enter(&cportinfo->cport_mutex);
8223 		if (cdip == NULL) {
8224 			/*
8225 			 * Attaching target node failed.
8226 			 * We retain sata_drive_info structure...
8227 			 */
8228 			mutex_exit(&cportinfo->cport_mutex);
8229 			return (SATA_SUCCESS);
8230 		}
8231 		(SATA_CPORTINFO_DRV_INFO(cportinfo))->
8232 		    satadrv_state = SATA_STATE_READY;
8233 	} else {
8234 		/* This must be Port Multiplier type */
8235 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
8236 			SATA_LOG_D((sata_hba_inst, CE_WARN,
8237 			    "sata_add_device: "
8238 			    "unrecognized dev type %x",
8239 			    cportinfo->cport_dev_type));
8240 			mutex_exit(&cportinfo->cport_mutex);
8241 			return (SATA_SUCCESS);
8242 		}
8243 		pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
8244 		pmportinfo = pminfo->pmult_dev_port[pmport];
8245 		sata_device.satadev_addr = pmportinfo->pmport_addr;
8246 		sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
8247 		mutex_exit(&cportinfo->cport_mutex);
8248 
8249 		rval = sata_probe_device(sata_hba_inst, &sata_device);
8250 		if (rval != SATA_SUCCESS ||
8251 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN) {
8252 			return (SATA_FAILURE);
8253 		}
8254 		mutex_enter(&cportinfo->cport_mutex);
8255 		sata_show_drive_info(sata_hba_inst,
8256 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
8257 
8258 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
8259 			/*
8260 			 * Could not determine device type.
8261 			 * Degrade this device to unknown.
8262 			 */
8263 			pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
8264 			mutex_exit(&cportinfo->cport_mutex);
8265 			return (SATA_SUCCESS);
8266 		}
8267 		pmportinfo->pmport_dev_type = sata_device.satadev_type;
8268 		pmportinfo->pmport_tgtnode_clean = B_TRUE;
8269 		mutex_exit(&cportinfo->cport_mutex);
8270 
8271 		/*
8272 		 * Initialize device to the desired state.
8273 		 * Even if it fails, the device will still
8274 		 * attach but syslog will show the warning.
8275 		 */
8276 		if (sata_initialize_device(sata_hba_inst,
8277 		    pmportinfo->pmport_sata_drive) != SATA_SUCCESS)
8278 			/* Retry */
8279 			(void) sata_initialize_device(sata_hba_inst,
8280 			    pmportinfo->pmport_sata_drive);
8281 
8282 		cdip = sata_create_target_node(pdip, sata_hba_inst,
8283 		    &sata_device.satadev_addr);
8284 		mutex_enter(&cportinfo->cport_mutex);
8285 		if (cdip == NULL) {
8286 			/*
8287 			 * Attaching target node failed.
8288 			 * We retain sata_drive_info structure...
8289 			 */
8290 			mutex_exit(&cportinfo->cport_mutex);
8291 			return (SATA_SUCCESS);
8292 		}
8293 		pmportinfo->pmport_sata_drive->satadrv_state |=
8294 		    SATA_STATE_READY;
8295 	}
8296 	mutex_exit(&cportinfo->cport_mutex);
8297 	return (SATA_SUCCESS);
8298 }
8299 
8300 
8301 
8302 /*
8303  * Create scsi target node for attached device, create node properties and
8304  * attach the node.
8305  * The node could be removed if the device onlining fails.
8306  *
8307  * A dev_info_t pointer is returned if operation is successful, NULL is
8308  * returned otherwise.
8309  *
8310  * No port multiplier support.
8311  */
8312 
8313 static dev_info_t *
8314 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst,
8315 			sata_address_t *sata_addr)
8316 {
8317 	dev_info_t *cdip = NULL;
8318 	int rval;
8319 	char *nname = NULL;
8320 	char **compatible = NULL;
8321 	int ncompatible;
8322 	struct scsi_inquiry inq;
8323 	sata_device_t sata_device;
8324 	sata_drive_info_t *sdinfo;
8325 	int target;
8326 	int i;
8327 
8328 	sata_device.satadev_rev = SATA_DEVICE_REV;
8329 	sata_device.satadev_addr = *sata_addr;
8330 
8331 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport)));
8332 
8333 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
8334 
8335 	target = SATA_TO_SCSI_TARGET(sata_addr->cport,
8336 	    sata_addr->pmport, sata_addr->qual);
8337 
8338 	if (sdinfo == NULL) {
8339 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8340 		    sata_addr->cport)));
8341 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8342 		    "sata_create_target_node: no sdinfo for target %x",
8343 		    target));
8344 		return (NULL);
8345 	}
8346 
8347 	/*
8348 	 * create or get scsi inquiry data, expected by
8349 	 * scsi_hba_nodename_compatible_get()
8350 	 * SATA hard disks get Identify Data translated into Inguiry Data.
8351 	 * ATAPI devices respond directly to Inquiry request.
8352 	 */
8353 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
8354 		sata_identdev_to_inquiry(sata_hba_inst, sdinfo,
8355 		    (uint8_t *)&inq);
8356 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8357 		    sata_addr->cport)));
8358 	} else { /* Assume supported ATAPI device */
8359 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8360 		    sata_addr->cport)));
8361 		if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr,
8362 		    &inq) == SATA_FAILURE)
8363 			return (NULL);
8364 		/*
8365 		 * Save supported ATAPI transport version
8366 		 */
8367 		sdinfo->satadrv_atapi_trans_ver =
8368 		    SATA_ATAPI_TRANS_VERSION(&inq);
8369 	}
8370 
8371 	/* determine the node name and compatible */
8372 	scsi_hba_nodename_compatible_get(&inq, NULL,
8373 	    inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
8374 
8375 #ifdef SATA_DEBUG
8376 	if (sata_debug_flags & SATA_DBG_NODES) {
8377 		if (nname == NULL) {
8378 			cmn_err(CE_NOTE, "sata_create_target_node: "
8379 			    "cannot determine nodename for target %d\n",
8380 			    target);
8381 		} else {
8382 			cmn_err(CE_WARN, "sata_create_target_node: "
8383 			    "target %d nodename: %s\n", target, nname);
8384 		}
8385 		if (compatible == NULL) {
8386 			cmn_err(CE_WARN,
8387 			    "sata_create_target_node: no compatible name\n");
8388 		} else {
8389 			for (i = 0; i < ncompatible; i++) {
8390 				cmn_err(CE_WARN, "sata_create_target_node: "
8391 				    "compatible name: %s\n", compatible[i]);
8392 			}
8393 		}
8394 	}
8395 #endif
8396 
8397 	/* if nodename can't be determined, log error and exit */
8398 	if (nname == NULL) {
8399 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8400 		    "sata_create_target_node: cannot determine nodename "
8401 		    "for target %d\n", target));
8402 		scsi_hba_nodename_compatible_free(nname, compatible);
8403 		return (NULL);
8404 	}
8405 	/*
8406 	 * Create scsi target node
8407 	 */
8408 	ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip);
8409 	rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
8410 	    "device-type", "scsi");
8411 
8412 	if (rval != DDI_PROP_SUCCESS) {
8413 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8414 		    "updating device_type prop failed %d", rval));
8415 		goto fail;
8416 	}
8417 
8418 	/*
8419 	 * Create target node properties: target & lun
8420 	 */
8421 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target);
8422 	if (rval != DDI_PROP_SUCCESS) {
8423 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8424 		    "updating target prop failed %d", rval));
8425 		goto fail;
8426 	}
8427 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0);
8428 	if (rval != DDI_PROP_SUCCESS) {
8429 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8430 		    "updating target prop failed %d", rval));
8431 		goto fail;
8432 	}
8433 
8434 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
8435 		/*
8436 		 * Add "variant" property
8437 		 */
8438 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
8439 		    "variant", "atapi");
8440 		if (rval != DDI_PROP_SUCCESS) {
8441 			SATA_LOG_D((sata_hba_inst, CE_WARN,
8442 			    "sata_create_target_node: variant atapi "
8443 			    "property could not be created: %d", rval));
8444 			goto fail;
8445 		}
8446 	}
8447 	/* decorate the node with compatible */
8448 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible",
8449 	    compatible, ncompatible) != DDI_PROP_SUCCESS) {
8450 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8451 		    "sata_create_target_node: FAIL compatible props cdip 0x%p",
8452 		    (void *)cdip));
8453 		goto fail;
8454 	}
8455 
8456 
8457 	/*
8458 	 * Now, try to attach the driver. If probing of the device fails,
8459 	 * the target node may be removed
8460 	 */
8461 	rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH);
8462 
8463 	scsi_hba_nodename_compatible_free(nname, compatible);
8464 
8465 	if (rval == NDI_SUCCESS)
8466 		return (cdip);
8467 
8468 	/* target node was removed - are we sure? */
8469 	return (NULL);
8470 
8471 fail:
8472 	scsi_hba_nodename_compatible_free(nname, compatible);
8473 	ddi_prop_remove_all(cdip);
8474 	rval = ndi_devi_free(cdip);
8475 	if (rval != NDI_SUCCESS) {
8476 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8477 		    "node removal failed %d", rval));
8478 	}
8479 	sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: "
8480 	    "cannot create target node for SATA device at port %d",
8481 	    sata_addr->cport);
8482 	return (NULL);
8483 }
8484 
8485 
8486 
8487 /*
8488  * Re-probe sata port, check for a device and attach info
8489  * structures when necessary. Identify Device data is fetched, if possible.
8490  * Assumption: sata address is already validated.
8491  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
8492  * the presence of a device and its type.
8493  *
8494  * flag arg specifies that the function should try multiple times to identify
8495  * device type and to initialize it, or it should return immediately on failure.
8496  * SATA_DEV_IDENTIFY_RETRY - retry
8497  * SATA_DEV_IDENTIFY_NORETRY - no retry
8498  *
8499  * SATA_FAILURE is returned if one of the operations failed.
8500  *
8501  * This function cannot be called in interrupt context - it may sleep.
8502  *
8503  * NOte: Port multiplier is not supported yet, although there may be some
8504  * pieces of code referencing to it.
8505  */
8506 static int
8507 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
8508     int flag)
8509 {
8510 	sata_cport_info_t *cportinfo;
8511 	sata_drive_info_t *sdinfo, *osdinfo;
8512 	boolean_t init_device = B_FALSE;
8513 	int prev_device_type = SATA_DTYPE_NONE;
8514 	int prev_device_settings = 0;
8515 	int prev_device_state = 0;
8516 	clock_t start_time;
8517 	int retry = B_FALSE;
8518 	int rval;
8519 
8520 	/* We only care about host sata cport for now */
8521 	cportinfo = SATA_CPORT_INFO(sata_hba_inst,
8522 	    sata_device->satadev_addr.cport);
8523 	osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8524 	if (osdinfo != NULL) {
8525 		/*
8526 		 * We are re-probing port with a previously attached device.
8527 		 * Save previous device type and settings.
8528 		 */
8529 		prev_device_type = cportinfo->cport_dev_type;
8530 		prev_device_settings = osdinfo->satadrv_settings;
8531 		prev_device_state = osdinfo->satadrv_state;
8532 	}
8533 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
8534 		start_time = ddi_get_lbolt();
8535 		retry = B_TRUE;
8536 	}
8537 retry_probe:
8538 
8539 	/* probe port */
8540 	mutex_enter(&cportinfo->cport_mutex);
8541 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
8542 	cportinfo->cport_state |= SATA_STATE_PROBING;
8543 	mutex_exit(&cportinfo->cport_mutex);
8544 
8545 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8546 	    (SATA_DIP(sata_hba_inst), sata_device);
8547 
8548 	mutex_enter(&cportinfo->cport_mutex);
8549 	if (rval != SATA_SUCCESS) {
8550 		cportinfo->cport_state = SATA_PSTATE_FAILED;
8551 		mutex_exit(&cportinfo->cport_mutex);
8552 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: "
8553 		    "SATA port %d probing failed",
8554 		    cportinfo->cport_addr.cport));
8555 		return (SATA_FAILURE);
8556 	}
8557 
8558 	/*
8559 	 * update sata port state and set device type
8560 	 */
8561 	sata_update_port_info(sata_hba_inst, sata_device);
8562 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
8563 
8564 	/*
8565 	 * Sanity check - Port is active? Is the link active?
8566 	 * Is there any device attached?
8567 	 */
8568 	if ((cportinfo->cport_state &
8569 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
8570 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
8571 	    SATA_PORT_DEVLINK_UP) {
8572 		/*
8573 		 * Port in non-usable state or no link active/no device.
8574 		 * Free info structure if necessary (direct attached drive
8575 		 * only, for now!
8576 		 */
8577 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8578 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8579 		/* Add here differentiation for device attached or not */
8580 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8581 		mutex_exit(&cportinfo->cport_mutex);
8582 		if (sdinfo != NULL)
8583 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8584 		return (SATA_SUCCESS);
8585 	}
8586 
8587 	cportinfo->cport_state |= SATA_STATE_READY;
8588 	cportinfo->cport_dev_type = sata_device->satadev_type;
8589 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8590 
8591 	/*
8592 	 * If we are re-probing the port, there may be
8593 	 * sata_drive_info structure attached
8594 	 * (or sata_pm_info, if PMult is supported).
8595 	 */
8596 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
8597 		/*
8598 		 * There is no device, so remove device info structure,
8599 		 * if necessary.
8600 		 * Only direct attached drive is considered now, until
8601 		 * port multiplier is supported. If the previously
8602 		 * attached device was a port multiplier, we would need
8603 		 * to take care of devices attached beyond the port
8604 		 * multiplier.
8605 		 */
8606 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8607 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8608 		if (sdinfo != NULL) {
8609 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8610 			sata_log(sata_hba_inst, CE_WARN,
8611 			    "SATA device detached "
8612 			    "from port %d", cportinfo->cport_addr.cport);
8613 		}
8614 		mutex_exit(&cportinfo->cport_mutex);
8615 		return (SATA_SUCCESS);
8616 	}
8617 
8618 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
8619 		if (sdinfo == NULL) {
8620 			/*
8621 			 * There is some device attached, but there is
8622 			 * no sata_drive_info structure - allocate one
8623 			 */
8624 			mutex_exit(&cportinfo->cport_mutex);
8625 			sdinfo = kmem_zalloc(
8626 			    sizeof (sata_drive_info_t), KM_SLEEP);
8627 			mutex_enter(&cportinfo->cport_mutex);
8628 			/*
8629 			 * Recheck, that the port state did not change when we
8630 			 * released mutex.
8631 			 */
8632 			if (cportinfo->cport_state & SATA_STATE_READY) {
8633 				SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo;
8634 				sdinfo->satadrv_addr = cportinfo->cport_addr;
8635 				sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT;
8636 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
8637 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
8638 			} else {
8639 				/*
8640 				 * Port is not in ready state, we
8641 				 * cannot attach a device.
8642 				 */
8643 				mutex_exit(&cportinfo->cport_mutex);
8644 				kmem_free(sdinfo, sizeof (sata_drive_info_t));
8645 				return (SATA_SUCCESS);
8646 			}
8647 			/*
8648 			 * Since we are adding device, presumably new one,
8649 			 * indicate that it  should be initalized,
8650 			 * as well as some internal framework states).
8651 			 */
8652 			init_device = B_TRUE;
8653 		}
8654 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8655 		sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
8656 	} else {
8657 		/*
8658 		 * The device is a port multiplier - not handled now.
8659 		 */
8660 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8661 		mutex_exit(&cportinfo->cport_mutex);
8662 		return (SATA_SUCCESS);
8663 	}
8664 	mutex_exit(&cportinfo->cport_mutex);
8665 	/*
8666 	 * Figure out what kind of device we are really
8667 	 * dealing with.
8668 	 */
8669 	rval = sata_probe_device(sata_hba_inst, sata_device);
8670 
8671 	mutex_enter(&cportinfo->cport_mutex);
8672 	if (rval == SATA_SUCCESS) {
8673 		/*
8674 		 * If we are dealing with the same type of a device as before,
8675 		 * restore its settings flags.
8676 		 */
8677 		if (osdinfo != NULL &&
8678 		    sata_device->satadev_type == prev_device_type)
8679 			sdinfo->satadrv_settings = prev_device_settings;
8680 
8681 		mutex_exit(&cportinfo->cport_mutex);
8682 		/* Set initial device features, if necessary */
8683 		if (init_device == B_TRUE) {
8684 			rval = sata_initialize_device(sata_hba_inst, sdinfo);
8685 		}
8686 		if (rval == SATA_SUCCESS)
8687 			return (rval);
8688 	} else {
8689 		/*
8690 		 * If there was some device info before we probe the device,
8691 		 * restore previous device setting, so we can retry from scratch
8692 		 * later. Providing, of course, that device has not disapear
8693 		 * during probing process.
8694 		 */
8695 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
8696 			if (osdinfo != NULL) {
8697 				cportinfo->cport_dev_type = prev_device_type;
8698 				sdinfo->satadrv_type = prev_device_type;
8699 				sdinfo->satadrv_state = prev_device_state;
8700 			}
8701 		} else {
8702 			/* device is gone */
8703 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8704 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8705 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8706 			mutex_exit(&cportinfo->cport_mutex);
8707 			return (SATA_SUCCESS);
8708 		}
8709 		mutex_exit(&cportinfo->cport_mutex);
8710 	}
8711 
8712 	if (retry) {
8713 		clock_t cur_time = ddi_get_lbolt();
8714 		/*
8715 		 * A device was not successfully identified or initialized.
8716 		 * Track retry time for device identification.
8717 		 */
8718 		if ((cur_time - start_time) <
8719 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
8720 			/* sleep for a while */
8721 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
8722 			goto retry_probe;
8723 		} else {
8724 			mutex_enter(&cportinfo->cport_mutex);
8725 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL)
8726 				SATA_CPORTINFO_DRV_INFO(cportinfo)->
8727 				    satadrv_state = SATA_DSTATE_FAILED;
8728 			mutex_exit(&cportinfo->cport_mutex);
8729 		}
8730 	}
8731 	return (SATA_SUCCESS);
8732 }
8733 
8734 /*
8735  * Initialize device
8736  * Specified device is initialized to a default state.
8737  *
8738  * Returns SATA_SUCCESS if all device features are set successfully,
8739  * SATA_FAILURE otherwise
8740  */
8741 static int
8742 sata_initialize_device(sata_hba_inst_t *sata_hba_inst,
8743     sata_drive_info_t *sdinfo)
8744 {
8745 	int rval;
8746 
8747 	sata_save_drive_settings(sdinfo);
8748 
8749 	sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
8750 
8751 	sata_init_write_cache_mode(sdinfo);
8752 
8753 	rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0);
8754 
8755 	/* Determine current data transfer mode */
8756 	if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) {
8757 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
8758 	} else if ((sdinfo->satadrv_id.ai_validinfo &
8759 	    SATA_VALIDINFO_88) != 0 &&
8760 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) {
8761 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
8762 	} else if ((sdinfo->satadrv_id.ai_dworddma &
8763 	    SATA_MDMA_SEL_MASK) != 0) {
8764 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
8765 	} else
8766 		/* DMA supported, not no DMA transfer mode is selected !? */
8767 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
8768 
8769 	return (rval);
8770 }
8771 
8772 
8773 /*
8774  * Initialize write cache mode.
8775  *
8776  * The default write cache setting for SATA HDD is provided by sata_write_cache
8777  * static variable. ATAPI CD/DVDs devices have write cache default is
8778  * determined by sata_atapicdvd_write_cache static variable.
8779  * 1 - enable
8780  * 0 - disable
8781  * any other value - current drive setting
8782  *
8783  * Although there is not reason to disable write cache on CD/DVD devices,
8784  * the default setting control is provided for the maximun flexibility.
8785  *
8786  * In the future, it may be overridden by the
8787  * disk-write-cache-enable property setting, if it is defined.
8788  * Returns SATA_SUCCESS if all device features are set successfully,
8789  * SATA_FAILURE otherwise.
8790  */
8791 static void
8792 sata_init_write_cache_mode(sata_drive_info_t *sdinfo)
8793 {
8794 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
8795 		if (sata_write_cache == 1)
8796 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
8797 		else if (sata_write_cache == 0)
8798 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
8799 		/*
8800 		 * When sata_write_cache value is not 0 or 1,
8801 		 * a current setting of the drive's write cache is used.
8802 		 */
8803 	} else { /* Assume ATAPI CD/DVD device */
8804 		if (sata_atapicdvd_write_cache == 1)
8805 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
8806 		else if (sata_atapicdvd_write_cache == 0)
8807 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
8808 		/*
8809 		 * When sata_write_cache value is not 0 or 1,
8810 		 * a current setting of the drive's write cache is used.
8811 		 */
8812 	}
8813 }
8814 
8815 
8816 /*
8817  * Validate sata address.
8818  * Specified cport, pmport and qualifier has to match
8819  * passed sata_scsi configuration info.
8820  * The presence of an attached device is not verified.
8821  *
8822  * Returns 0 when address is valid, -1 otherwise.
8823  */
8824 static int
8825 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport,
8826 	int pmport, int qual)
8827 {
8828 	if (qual == SATA_ADDR_DCPORT && pmport != 0)
8829 		goto invalid_address;
8830 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
8831 		goto invalid_address;
8832 	if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) &&
8833 	    ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) ||
8834 	    (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) ||
8835 	    (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport))))
8836 		goto invalid_address;
8837 
8838 	return (0);
8839 
8840 invalid_address:
8841 	return (-1);
8842 
8843 }
8844 
8845 /*
8846  * Validate scsi address
8847  * SCSI target address is translated into SATA cport/pmport and compared
8848  * with a controller port/device configuration. LUN has to be 0.
8849  * Returns 0 if a scsi target refers to an attached device,
8850  * returns 1 if address is valid but device is not attached,
8851  * returns -1 if bad address or device is of an unsupported type.
8852  * Upon return sata_device argument is set.
8853  */
8854 static int
8855 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst,
8856 	struct scsi_address *ap, sata_device_t *sata_device)
8857 {
8858 	int cport, pmport, qual, rval;
8859 
8860 	rval = -1;	/* Invalid address */
8861 	if (ap->a_lun != 0)
8862 		goto out;
8863 
8864 	qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
8865 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
8866 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
8867 
8868 	if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT)
8869 		goto out;
8870 
8871 	if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) ==
8872 	    0) {
8873 
8874 		sata_cport_info_t *cportinfo;
8875 		sata_pmult_info_t *pmultinfo;
8876 		sata_drive_info_t *sdinfo = NULL;
8877 
8878 		rval = 1;	/* Valid sata address */
8879 
8880 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
8881 		if (qual == SATA_ADDR_DCPORT) {
8882 			if (cportinfo == NULL ||
8883 			    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
8884 				goto out;
8885 
8886 			if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT ||
8887 			    (cportinfo->cport_dev_type &
8888 			    SATA_VALID_DEV_TYPE) == 0) {
8889 				rval = -1;
8890 				goto out;
8891 			}
8892 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8893 
8894 		} else if (qual == SATA_ADDR_DPMPORT) {
8895 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
8896 			if (pmultinfo == NULL) {
8897 				rval = -1;
8898 				goto out;
8899 			}
8900 			if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) ==
8901 			    NULL ||
8902 			    SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
8903 			    pmport) == SATA_DTYPE_NONE)
8904 				goto out;
8905 
8906 			sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport,
8907 			    pmport);
8908 		} else {
8909 			rval = -1;
8910 			goto out;
8911 		}
8912 		if ((sdinfo == NULL) ||
8913 		    (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0)
8914 			goto out;
8915 
8916 		sata_device->satadev_type = sdinfo->satadrv_type;
8917 		sata_device->satadev_addr.qual = qual;
8918 		sata_device->satadev_addr.cport = cport;
8919 		sata_device->satadev_addr.pmport = pmport;
8920 		sata_device->satadev_rev = SATA_DEVICE_REV_1;
8921 		return (0);
8922 	}
8923 out:
8924 	if (rval == 1) {
8925 		SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
8926 		    "sata_validate_scsi_address: no valid target %x lun %x",
8927 		    ap->a_target, ap->a_lun);
8928 	}
8929 	return (rval);
8930 }
8931 
8932 /*
8933  * Find dip corresponding to passed device number
8934  *
8935  * Returns NULL if invalid device number is passed or device cannot be found,
8936  * Returns dip is device is found.
8937  */
8938 static dev_info_t *
8939 sata_devt_to_devinfo(dev_t dev)
8940 {
8941 	dev_info_t *dip;
8942 #ifndef __lock_lint
8943 	struct devnames *dnp;
8944 	major_t major = getmajor(dev);
8945 	int instance = SATA_MINOR2INSTANCE(getminor(dev));
8946 
8947 	if (major >= devcnt)
8948 		return (NULL);
8949 
8950 	dnp = &devnamesp[major];
8951 	LOCK_DEV_OPS(&(dnp->dn_lock));
8952 	dip = dnp->dn_head;
8953 	while (dip && (ddi_get_instance(dip) != instance)) {
8954 		dip = ddi_get_next(dip);
8955 	}
8956 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
8957 #endif
8958 
8959 	return (dip);
8960 }
8961 
8962 
8963 /*
8964  * Probe device.
8965  * This function issues Identify Device command and initializes local
8966  * sata_drive_info structure if the device can be identified.
8967  * The device type is determined by examining Identify Device
8968  * command response.
8969  * If the sata_hba_inst has linked drive info structure for this
8970  * device address, the Identify Device data is stored into sata_drive_info
8971  * structure linked to the port info structure.
8972  *
8973  * sata_device has to refer to the valid sata port(s) for HBA described
8974  * by sata_hba_inst structure.
8975  *
8976  * Returns:
8977  *	SATA_SUCCESS if device type was successfully probed and port-linked
8978  *		drive info structure was updated;
8979  * 	SATA_FAILURE if there is no device, or device was not probed
8980  *		successully;
8981  *	SATA_RETRY if device probe can be retried later.
8982  * If a device cannot be identified, sata_device's dev_state and dev_type
8983  * fields are set to unknown.
8984  * There are no retries in this function. Any retries should be managed by
8985  * the caller.
8986  */
8987 
8988 
8989 static int
8990 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
8991 {
8992 	sata_drive_info_t *sdinfo;
8993 	sata_drive_info_t new_sdinfo;	/* local drive info struct */
8994 	int rval;
8995 
8996 	ASSERT((SATA_CPORT_STATE(sata_hba_inst,
8997 	    sata_device->satadev_addr.cport) &
8998 	    (SATA_STATE_PROBED | SATA_STATE_READY)) != 0);
8999 
9000 	sata_device->satadev_type = SATA_DTYPE_NONE;
9001 
9002 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9003 	    sata_device->satadev_addr.cport)));
9004 
9005 	/* Get pointer to port-linked sata device info structure */
9006 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9007 	if (sdinfo != NULL) {
9008 		sdinfo->satadrv_state &=
9009 		    ~(SATA_STATE_PROBED | SATA_STATE_READY);
9010 		sdinfo->satadrv_state |= SATA_STATE_PROBING;
9011 	} else {
9012 		/* No device to probe */
9013 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9014 		    sata_device->satadev_addr.cport)));
9015 		sata_device->satadev_type = SATA_DTYPE_NONE;
9016 		sata_device->satadev_state = SATA_STATE_UNKNOWN;
9017 		return (SATA_FAILURE);
9018 	}
9019 	/*
9020 	 * Need to issue both types of identify device command and
9021 	 * determine device type by examining retreived data/status.
9022 	 * First, ATA Identify Device.
9023 	 */
9024 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
9025 	new_sdinfo.satadrv_addr = sata_device->satadev_addr;
9026 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9027 	    sata_device->satadev_addr.cport)));
9028 	new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
9029 	rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
9030 	if (rval == SATA_RETRY) {
9031 		/* We may try to check for ATAPI device */
9032 		if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) {
9033 			/*
9034 			 * HBA supports ATAPI - try to issue Identify Packet
9035 			 * Device command.
9036 			 */
9037 			new_sdinfo.satadrv_type = SATA_DTYPE_ATAPICD;
9038 			rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
9039 		}
9040 	}
9041 	if (rval == SATA_SUCCESS) {
9042 		/*
9043 		 * Got something responding positively to ATA Identify Device
9044 		 * or to Identify Packet Device cmd.
9045 		 * Save last used device type.
9046 		 */
9047 		sata_device->satadev_type = new_sdinfo.satadrv_type;
9048 
9049 		/* save device info, if possible */
9050 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9051 		    sata_device->satadev_addr.cport)));
9052 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9053 		if (sdinfo == NULL) {
9054 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9055 			    sata_device->satadev_addr.cport)));
9056 			return (SATA_FAILURE);
9057 		}
9058 		/*
9059 		 * Copy drive info into the port-linked drive info structure.
9060 		 */
9061 		*sdinfo = new_sdinfo;
9062 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
9063 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
9064 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
9065 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
9066 			    sata_device->satadev_addr.cport) =
9067 			    sdinfo->satadrv_type;
9068 		else /* SATA_ADDR_DPMPORT */
9069 			SATA_PMPORT_DEV_TYPE(sata_hba_inst,
9070 			    sata_device->satadev_addr.cport,
9071 			    sata_device->satadev_addr.pmport) =
9072 			    sdinfo->satadrv_type;
9073 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9074 		    sata_device->satadev_addr.cport)));
9075 		return (SATA_SUCCESS);
9076 	}
9077 
9078 	/*
9079 	 * It may be SATA_RETRY or SATA_FAILURE return.
9080 	 * Looks like we cannot determine the device type at this time.
9081 	 */
9082 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9083 	    sata_device->satadev_addr.cport)));
9084 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9085 	if (sdinfo != NULL) {
9086 		sata_device->satadev_type = SATA_DTYPE_UNKNOWN;
9087 		sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9088 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
9089 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
9090 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
9091 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
9092 			    sata_device->satadev_addr.cport) =
9093 			    SATA_DTYPE_UNKNOWN;
9094 		else {
9095 			/* SATA_ADDR_DPMPORT */
9096 			if ((SATA_PMULT_INFO(sata_hba_inst,
9097 			    sata_device->satadev_addr.cport) != NULL) &&
9098 			    (SATA_PMPORT_INFO(sata_hba_inst,
9099 			    sata_device->satadev_addr.cport,
9100 			    sata_device->satadev_addr.pmport) != NULL))
9101 				SATA_PMPORT_DEV_TYPE(sata_hba_inst,
9102 				    sata_device->satadev_addr.cport,
9103 				    sata_device->satadev_addr.pmport) =
9104 				    SATA_DTYPE_UNKNOWN;
9105 		}
9106 	}
9107 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9108 	    sata_device->satadev_addr.cport)));
9109 	return (rval);
9110 }
9111 
9112 
9113 /*
9114  * Get pointer to sata_drive_info structure.
9115  *
9116  * The sata_device has to contain address (cport, pmport and qualifier) for
9117  * specified sata_scsi structure.
9118  *
9119  * Returns NULL if device address is not valid for this HBA configuration.
9120  * Otherwise, returns a pointer to sata_drive_info structure.
9121  *
9122  * This function should be called with a port mutex held.
9123  */
9124 static sata_drive_info_t *
9125 sata_get_device_info(sata_hba_inst_t *sata_hba_inst,
9126     sata_device_t *sata_device)
9127 {
9128 	uint8_t cport = sata_device->satadev_addr.cport;
9129 	uint8_t pmport = sata_device->satadev_addr.pmport;
9130 	uint8_t qual = sata_device->satadev_addr.qual;
9131 
9132 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
9133 		return (NULL);
9134 
9135 	if (!(SATA_CPORT_STATE(sata_hba_inst, cport) &
9136 	    (SATA_STATE_PROBED | SATA_STATE_READY)))
9137 		/* Port not probed yet */
9138 		return (NULL);
9139 
9140 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE)
9141 		return (NULL);
9142 
9143 	if (qual == SATA_ADDR_DCPORT) {
9144 		/* Request for a device on a controller port */
9145 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
9146 		    SATA_DTYPE_PMULT)
9147 			/* Port multiplier attached */
9148 			return (NULL);
9149 		return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport));
9150 	}
9151 	if (qual == SATA_ADDR_DPMPORT) {
9152 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
9153 		    SATA_DTYPE_PMULT)
9154 			return (NULL);
9155 
9156 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport))
9157 			return (NULL);
9158 
9159 		return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport));
9160 	}
9161 
9162 	/* we should not get here */
9163 	return (NULL);
9164 }
9165 
9166 
9167 /*
9168  * sata_identify_device.
9169  * Send Identify Device command to SATA HBA driver.
9170  * If command executes successfully, update sata_drive_info structure pointed
9171  * to by sdinfo argument, including Identify Device data.
9172  * If command fails, invalidate data in sata_drive_info.
9173  *
9174  * Cannot be called from interrupt level.
9175  *
9176  * Returns:
9177  * SATA_SUCCESS if the device was identified as a supported device,
9178  * SATA_RETRY if the device was not identified but could be retried,
9179  * SATA_FAILURE if the device was not identified and identify attempt
9180  *	should not be retried.
9181  */
9182 static int
9183 sata_identify_device(sata_hba_inst_t *sata_hba_inst,
9184     sata_drive_info_t *sdinfo)
9185 {
9186 	uint16_t cfg_word;
9187 	int rval;
9188 
9189 	/* fetch device identify data */
9190 	if ((rval = sata_fetch_device_identify_data(sata_hba_inst,
9191 	    sdinfo)) != 0)
9192 		goto fail_unknown;
9193 
9194 	cfg_word = sdinfo->satadrv_id.ai_config;
9195 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK &&
9196 	    (cfg_word & SATA_ATA_TYPE_MASK) != SATA_ATA_TYPE) {
9197 		/* Change device type to reflect Identify Device data */
9198 		if (((cfg_word & SATA_ATAPI_TYPE_MASK) ==
9199 		    SATA_ATAPI_TYPE) &&
9200 		    ((cfg_word & SATA_ATAPI_ID_DEV_TYPE) ==
9201 		    SATA_ATAPI_CDROM_DEV)) {
9202 			sdinfo->satadrv_type = SATA_DTYPE_ATAPICD;
9203 		} else {
9204 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9205 		}
9206 	} else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD &&
9207 	    (((cfg_word & SATA_ATAPI_TYPE_MASK) != SATA_ATAPI_TYPE) ||
9208 	    ((cfg_word & SATA_ATAPI_ID_DEV_TYPE) != SATA_ATAPI_CDROM_DEV))) {
9209 		/* Change device type to reflect Identify Device data ! */
9210 		if ((sdinfo->satadrv_id.ai_config & SATA_ATA_TYPE_MASK) ==
9211 		    SATA_ATA_TYPE) {
9212 			sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
9213 		} else {
9214 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9215 		}
9216 	}
9217 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9218 		if (sdinfo->satadrv_capacity == 0) {
9219 			/* Non-LBA disk. Too bad... */
9220 			sata_log(sata_hba_inst, CE_WARN,
9221 			    "SATA disk device at port %d does not support LBA",
9222 			    sdinfo->satadrv_addr.cport);
9223 			rval = SATA_FAILURE;
9224 			goto fail_unknown;
9225 		}
9226 	}
9227 #if 0
9228 	/* Left for historical reason */
9229 	/*
9230 	 * Some initial version of SATA spec indicated that at least
9231 	 * UDMA mode 4 has to be supported. It is not metioned in
9232 	 * SerialATA 2.6, so this restriction is removed.
9233 	 */
9234 	/* Check for Ultra DMA modes 6 through 0 being supported */
9235 	for (i = 6; i >= 0; --i) {
9236 		if (sdinfo->satadrv_id.ai_ultradma & (1 << i))
9237 			break;
9238 	}
9239 
9240 	/*
9241 	 * At least UDMA 4 mode has to be supported. If mode 4 or
9242 	 * higher are not supported by the device, fail this
9243 	 * device.
9244 	 */
9245 	if (i < 4) {
9246 		/* No required Ultra DMA mode supported */
9247 		sata_log(sata_hba_inst, CE_WARN,
9248 		    "SATA disk device at port %d does not support UDMA "
9249 		    "mode 4 or higher", sdinfo->satadrv_addr.cport);
9250 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9251 		    "mode 4 or higher required, %d supported", i));
9252 		rval = SATA_FAILURE;
9253 		goto fail_unknown;
9254 	}
9255 #endif
9256 
9257 	return (SATA_SUCCESS);
9258 
9259 fail_unknown:
9260 	/* Invalidate sata_drive_info ? */
9261 	sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9262 	sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
9263 	return (rval);
9264 }
9265 
9266 /*
9267  * Log/display device information
9268  */
9269 static void
9270 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst,
9271     sata_drive_info_t *sdinfo)
9272 {
9273 	int valid_version;
9274 	char msg_buf[MAXPATHLEN];
9275 	int i;
9276 
9277 	/* Show HBA path */
9278 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf);
9279 
9280 	cmn_err(CE_CONT, "?%s :\n", msg_buf);
9281 
9282 	if (sdinfo->satadrv_type == SATA_DTYPE_UNKNOWN) {
9283 		(void) sprintf(msg_buf,
9284 		    "Unsupported SATA device type (cfg 0x%x) at ",
9285 		    sdinfo->satadrv_id.ai_config);
9286 	} else {
9287 		(void) sprintf(msg_buf, "SATA %s device at",
9288 		    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
9289 		    "disk":"CD/DVD (ATAPI)");
9290 	}
9291 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT)
9292 		cmn_err(CE_CONT, "?\t%s port %d\n",
9293 		    msg_buf, sdinfo->satadrv_addr.cport);
9294 	else
9295 		cmn_err(CE_CONT, "?\t%s port %d pmport %d\n",
9296 		    msg_buf, sdinfo->satadrv_addr.cport,
9297 		    sdinfo->satadrv_addr.pmport);
9298 
9299 	bcopy(&sdinfo->satadrv_id.ai_model, msg_buf,
9300 	    sizeof (sdinfo->satadrv_id.ai_model));
9301 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model));
9302 	msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
9303 	cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf);
9304 
9305 	bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf,
9306 	    sizeof (sdinfo->satadrv_id.ai_fw));
9307 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw));
9308 	msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
9309 	cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf);
9310 
9311 	bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf,
9312 	    sizeof (sdinfo->satadrv_id.ai_drvser));
9313 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser));
9314 	msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
9315 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9316 		cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
9317 	} else {
9318 		/* Assuming ATAPI CD/DVD */
9319 		/*
9320 		 * SOme drives do not implement serial number and may
9321 		 * violate the spec by provinding spaces rather than zeros
9322 		 * in serial number field. Scan the buffer to detect it.
9323 		 */
9324 		for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) {
9325 			if (msg_buf[i] != '\0' && msg_buf[i] != ' ')
9326 				break;
9327 		}
9328 		if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) {
9329 			cmn_err(CE_CONT, "?\tserial number - none\n");
9330 		} else {
9331 			cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
9332 		}
9333 	}
9334 
9335 #ifdef SATA_DEBUG
9336 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
9337 	    sdinfo->satadrv_id.ai_majorversion != 0xffff) {
9338 		int i;
9339 		for (i = 14; i >= 2; i--) {
9340 			if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) {
9341 				valid_version = i;
9342 				break;
9343 			}
9344 		}
9345 		cmn_err(CE_CONT,
9346 		    "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
9347 		    valid_version,
9348 		    sdinfo->satadrv_id.ai_majorversion,
9349 		    sdinfo->satadrv_id.ai_minorversion);
9350 	}
9351 #endif
9352 	/* Log some info */
9353 	cmn_err(CE_CONT, "?\tsupported features:\n");
9354 	msg_buf[0] = '\0';
9355 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9356 		if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48)
9357 			(void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN);
9358 		else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28)
9359 			(void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN);
9360 	}
9361 	if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA)
9362 		(void) strlcat(msg_buf, "DMA", MAXPATHLEN);
9363 	if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ)
9364 		(void) strlcat(msg_buf, ", Native Command Queueing",
9365 		    MAXPATHLEN);
9366 	if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)
9367 		(void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN);
9368 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) &&
9369 	    (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED))
9370 		(void) strlcat(msg_buf, ", SMART", MAXPATHLEN);
9371 	if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) &&
9372 	    (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED))
9373 		(void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN);
9374 	cmn_err(CE_CONT, "?\t %s\n", msg_buf);
9375 	if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2)
9376 		cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n");
9377 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1)
9378 		cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n");
9379 	if (sdinfo->satadrv_features_support &
9380 	    (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) {
9381 		msg_buf[0] = '\0';
9382 		(void) snprintf(msg_buf, MAXPATHLEN,
9383 		    "Supported queue depth %d",
9384 		    sdinfo->satadrv_queue_depth);
9385 		if (!(sata_func_enable &
9386 		    (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ)))
9387 			(void) strlcat(msg_buf,
9388 			    " - queueing disabled globally", MAXPATHLEN);
9389 		else if (sdinfo->satadrv_queue_depth >
9390 		    sdinfo->satadrv_max_queue_depth) {
9391 			(void) snprintf(&msg_buf[strlen(msg_buf)],
9392 			    MAXPATHLEN - strlen(msg_buf), ", limited to %d",
9393 			    (int)sdinfo->satadrv_max_queue_depth);
9394 		}
9395 		cmn_err(CE_CONT, "?\t%s\n", msg_buf);
9396 	}
9397 
9398 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9399 #ifdef __i386
9400 		(void) sprintf(msg_buf, "\tcapacity = %llu sectors\n",
9401 		    sdinfo->satadrv_capacity);
9402 #else
9403 		(void) sprintf(msg_buf, "\tcapacity = %lu sectors\n",
9404 		    sdinfo->satadrv_capacity);
9405 #endif
9406 		cmn_err(CE_CONT, "?%s", msg_buf);
9407 	}
9408 }
9409 
9410 
9411 /*
9412  * sata_save_drive_settings extracts current setting of the device and stores
9413  * it for future reference, in case the device setup would need to be restored
9414  * after the device reset.
9415  *
9416  * For all devices read ahead and write cache settings are saved, if the
9417  * device supports these features at all.
9418  * For ATAPI devices the Removable Media Status Notification setting is saved.
9419  */
9420 static void
9421 sata_save_drive_settings(sata_drive_info_t *sdinfo)
9422 {
9423 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) ||
9424 	    (sdinfo->satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) {
9425 
9426 		/* Current setting of Read Ahead (and Read Cache) */
9427 		if (sdinfo->satadrv_id.ai_features85 & SATA_LOOK_AHEAD)
9428 			sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
9429 		else
9430 			sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD;
9431 
9432 		/* Current setting of Write Cache */
9433 		if (sdinfo->satadrv_id.ai_features85 & SATA_WRITE_CACHE)
9434 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
9435 		else
9436 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
9437 	}
9438 
9439 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
9440 		if (sdinfo->satadrv_id.ai_cmdset83 & SATA_RM_STATUS_NOTIFIC)
9441 			sdinfo->satadrv_settings |= SATA_DEV_RMSN;
9442 		else
9443 			sdinfo->satadrv_settings &= ~SATA_DEV_RMSN;
9444 	}
9445 }
9446 
9447 
9448 /*
9449  * sata_check_capacity function determines a disk capacity
9450  * and addressing mode (LBA28/LBA48) by examining a disk identify device data.
9451  *
9452  * NOTE: CHS mode is not supported! If a device does not support LBA,
9453  * this function is not called.
9454  *
9455  * Returns device capacity in number of blocks, i.e. largest addressable LBA+1
9456  */
9457 static uint64_t
9458 sata_check_capacity(sata_drive_info_t *sdinfo)
9459 {
9460 	uint64_t capacity = 0;
9461 	int i;
9462 
9463 	if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK ||
9464 	    !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT)
9465 		/* Capacity valid only for LBA-addressable disk devices */
9466 		return (0);
9467 
9468 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) &&
9469 	    (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) &&
9470 	    (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) {
9471 		/* LBA48 mode supported and enabled */
9472 		sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 |
9473 		    SATA_DEV_F_LBA28;
9474 		for (i = 3;  i >= 0;  --i) {
9475 			capacity <<= 16;
9476 			capacity += sdinfo->satadrv_id.ai_addrsecxt[i];
9477 		}
9478 	} else {
9479 		capacity = sdinfo->satadrv_id.ai_addrsec[1];
9480 		capacity <<= 16;
9481 		capacity += sdinfo->satadrv_id.ai_addrsec[0];
9482 		if (capacity >= 0x1000000)
9483 			/* LBA28 mode */
9484 			sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28;
9485 	}
9486 	return (capacity);
9487 }
9488 
9489 
9490 /*
9491  * Allocate consistent buffer for DMA transfer
9492  *
9493  * Cannot be called from interrupt level or with mutex held - it may sleep.
9494  *
9495  * Returns pointer to allocated buffer structure, or NULL if allocation failed.
9496  */
9497 static struct buf *
9498 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len)
9499 {
9500 	struct scsi_address ap;
9501 	struct buf *bp;
9502 	ddi_dma_attr_t	cur_dma_attr;
9503 
9504 	ASSERT(spx->txlt_sata_pkt != NULL);
9505 	ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran;
9506 	ap.a_target = SATA_TO_SCSI_TARGET(
9507 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport,
9508 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport,
9509 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual);
9510 	ap.a_lun = 0;
9511 
9512 	bp = scsi_alloc_consistent_buf(&ap, NULL, len,
9513 	    B_READ, SLEEP_FUNC, NULL);
9514 
9515 	if (bp != NULL) {
9516 		/* Allocate DMA resources for this buffer */
9517 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
9518 		/*
9519 		 * We use a local version of the dma_attr, to account
9520 		 * for a device addressing limitations.
9521 		 * sata_adjust_dma_attr() will handle sdinfo == NULL which
9522 		 * will cause dma attributes to be adjusted to a lowest
9523 		 * acceptable level.
9524 		 */
9525 		sata_adjust_dma_attr(NULL,
9526 		    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
9527 
9528 		if (sata_dma_buf_setup(spx, PKT_CONSISTENT,
9529 		    SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) {
9530 			scsi_free_consistent_buf(bp);
9531 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
9532 			bp = NULL;
9533 		}
9534 	}
9535 	return (bp);
9536 }
9537 
9538 /*
9539  * Release local buffer (consistent buffer for DMA transfer) allocated
9540  * via sata_alloc_local_buffer().
9541  */
9542 static void
9543 sata_free_local_buffer(sata_pkt_txlate_t *spx)
9544 {
9545 	ASSERT(spx->txlt_sata_pkt != NULL);
9546 	ASSERT(spx->txlt_dma_cookie_list != NULL);
9547 	ASSERT(spx->txlt_dma_cookie_list_len != 0);
9548 	ASSERT(spx->txlt_buf_dma_handle != NULL);
9549 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL);
9550 
9551 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0;
9552 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL;
9553 
9554 	/* Free DMA resources */
9555 	(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
9556 	ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
9557 	spx->txlt_buf_dma_handle = 0;
9558 
9559 	if (spx->txlt_dma_cookie_list != &spx->txlt_dma_cookie) {
9560 		kmem_free(spx->txlt_dma_cookie_list,
9561 		    spx->txlt_dma_cookie_list_len * sizeof (ddi_dma_cookie_t));
9562 		spx->txlt_dma_cookie_list = NULL;
9563 		spx->txlt_dma_cookie_list_len = 0;
9564 	}
9565 	/* Free buffer */
9566 	scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp);
9567 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
9568 }
9569 
9570 
9571 
9572 
9573 /*
9574  * Allocate sata_pkt
9575  * Pkt structure version and embedded strcutures version are initialized.
9576  * sata_pkt and sata_pkt_txlate structures are cross-linked.
9577  *
9578  * Since this may be called in interrupt context by sata_scsi_init_pkt,
9579  * callback argument determines if it can sleep or not.
9580  * Hence, it should not be called from interrupt context.
9581  *
9582  * If successful, non-NULL pointer to a sata pkt is returned.
9583  * Upon failure, NULL pointer is returned.
9584  */
9585 static sata_pkt_t *
9586 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t))
9587 {
9588 	sata_pkt_t *spkt;
9589 	int kmsflag;
9590 
9591 	kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
9592 	spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag);
9593 	if (spkt == NULL) {
9594 		SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9595 		    "sata_pkt_alloc: failed"));
9596 		return (NULL);
9597 	}
9598 	spkt->satapkt_rev = SATA_PKT_REV;
9599 	spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV;
9600 	spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
9601 	spkt->satapkt_framework_private = spx;
9602 	spx->txlt_sata_pkt = spkt;
9603 	return (spkt);
9604 }
9605 
9606 /*
9607  * Free sata pkt allocated via sata_pkt_alloc()
9608  */
9609 static void
9610 sata_pkt_free(sata_pkt_txlate_t *spx)
9611 {
9612 	ASSERT(spx->txlt_sata_pkt != NULL);
9613 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL);
9614 	kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t));
9615 	spx->txlt_sata_pkt = NULL;
9616 }
9617 
9618 
9619 /*
9620  * Adjust DMA attributes.
9621  * SCSI cmds block count is up to 24 bits, SATA cmd block count vary
9622  * from 8 bits to 16 bits, depending on a command being used.
9623  * Limiting max block count arbitrarily to 256 for all read/write
9624  * commands may affects performance, so check both the device and
9625  * controller capability before adjusting dma attributes.
9626  */
9627 void
9628 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr,
9629     ddi_dma_attr_t *adj_dma_attr)
9630 {
9631 	uint32_t count_max;
9632 
9633 	/* Copy original attributes */
9634 	*adj_dma_attr = *dma_attr;
9635 	/*
9636 	 * Things to consider: device addressing capability,
9637 	 * "excessive" controller DMA capabilities.
9638 	 * If a device is being probed/initialized, there are
9639 	 * no device info - use default limits then.
9640 	 */
9641 	if (sdinfo == NULL) {
9642 		count_max = dma_attr->dma_attr_granular * 0x100;
9643 		if (dma_attr->dma_attr_count_max > count_max)
9644 			adj_dma_attr->dma_attr_count_max = count_max;
9645 		if (dma_attr->dma_attr_maxxfer > count_max)
9646 			adj_dma_attr->dma_attr_maxxfer = count_max;
9647 		return;
9648 	}
9649 
9650 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9651 		if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) {
9652 			/*
9653 			 * 16-bit sector count may be used - we rely on
9654 			 * the assumption that only read and write cmds
9655 			 * will request more than 256 sectors worth of data
9656 			 */
9657 			count_max = adj_dma_attr->dma_attr_granular * 0x10000;
9658 		} else {
9659 			/*
9660 			 * 8-bit sector count will be used - default limits
9661 			 * for dma attributes
9662 			 */
9663 			count_max = adj_dma_attr->dma_attr_granular * 0x100;
9664 		}
9665 		/*
9666 		 * Adjust controler dma attributes, if necessary
9667 		 */
9668 		if (dma_attr->dma_attr_count_max > count_max)
9669 			adj_dma_attr->dma_attr_count_max = count_max;
9670 		if (dma_attr->dma_attr_maxxfer > count_max)
9671 			adj_dma_attr->dma_attr_maxxfer = count_max;
9672 	}
9673 }
9674 
9675 
9676 /*
9677  * Allocate DMA resources for the buffer
9678  * This function handles initial DMA resource allocation as well as
9679  * DMA window shift and may be called repeatedly for the same DMA window
9680  * until all DMA cookies in the DMA window are processed.
9681  * To guarantee that there is always a coherent set of cookies to process
9682  * by SATA HBA driver (observing alignment, device granularity, etc.),
9683  * the number of slots for DMA cookies is equal to lesser of  a number of
9684  * cookies in a DMA window and a max number of scatter/gather entries.
9685  *
9686  * Returns DDI_SUCCESS upon successful operation.
9687  * Return failure code of a failing command or DDI_FAILURE when
9688  * internal cleanup failed.
9689  */
9690 static int
9691 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags,
9692     int (*callback)(caddr_t), caddr_t arg,
9693     ddi_dma_attr_t *cur_dma_attr)
9694 {
9695 	int	rval;
9696 	off_t	offset;
9697 	size_t	size;
9698 	int	max_sg_len, req_len, i;
9699 	uint_t	dma_flags;
9700 	struct buf	*bp;
9701 	uint64_t	cur_txfer_len;
9702 
9703 
9704 	ASSERT(spx->txlt_sata_pkt != NULL);
9705 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9706 	ASSERT(bp != NULL);
9707 
9708 
9709 	if (spx->txlt_buf_dma_handle == NULL) {
9710 		/*
9711 		 * No DMA resources allocated so far - this is a first call
9712 		 * for this sata pkt.
9713 		 */
9714 		rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst),
9715 		    cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle);
9716 
9717 		if (rval != DDI_SUCCESS) {
9718 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9719 			    "sata_dma_buf_setup: no buf DMA resources %x",
9720 			    rval));
9721 			return (rval);
9722 		}
9723 
9724 		if (bp->b_flags & B_READ)
9725 			dma_flags = DDI_DMA_READ;
9726 		else
9727 			dma_flags = DDI_DMA_WRITE;
9728 
9729 		if (flags & PKT_CONSISTENT)
9730 			dma_flags |= DDI_DMA_CONSISTENT;
9731 
9732 		if (flags & PKT_DMA_PARTIAL)
9733 			dma_flags |= DDI_DMA_PARTIAL;
9734 
9735 		/*
9736 		 * Check buffer alignment and size against dma attributes
9737 		 * Consider dma_attr_align only. There may be requests
9738 		 * with the size lower than device granularity, but they
9739 		 * will not read/write from/to the device, so no adjustment
9740 		 * is necessary. The dma_attr_minxfer theoretically should
9741 		 * be considered, but no HBA driver is checking it.
9742 		 */
9743 		if (IS_P2ALIGNED(bp->b_un.b_addr,
9744 		    cur_dma_attr->dma_attr_align)) {
9745 			rval = ddi_dma_buf_bind_handle(
9746 			    spx->txlt_buf_dma_handle,
9747 			    bp, dma_flags, callback, arg,
9748 			    &spx->txlt_dma_cookie,
9749 			    &spx->txlt_curwin_num_dma_cookies);
9750 		} else { /* Buffer is not aligned */
9751 
9752 			int	(*ddicallback)(caddr_t);
9753 			size_t	bufsz;
9754 
9755 			/* Check id sleeping is allowed */
9756 			ddicallback = (callback == NULL_FUNC) ?
9757 			    DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
9758 
9759 			SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
9760 			    "mis-aligned buffer: addr=0x%p, cnt=%lu",
9761 			    (void *)bp->b_un.b_addr, bp->b_bcount);
9762 
9763 			if (bp->b_flags & (B_PAGEIO|B_PHYS))
9764 				/*
9765 				 * CPU will need to access data in the buffer
9766 				 * (for copying) so map it.
9767 				 */
9768 				bp_mapin(bp);
9769 
9770 			ASSERT(spx->txlt_tmp_buf == NULL);
9771 
9772 			/* Buffer may be padded by ddi_dma_mem_alloc()! */
9773 			rval = ddi_dma_mem_alloc(
9774 			    spx->txlt_buf_dma_handle,
9775 			    bp->b_bcount,
9776 			    &sata_acc_attr,
9777 			    DDI_DMA_STREAMING,
9778 			    ddicallback, NULL,
9779 			    &spx->txlt_tmp_buf,
9780 			    &bufsz,
9781 			    &spx->txlt_tmp_buf_handle);
9782 
9783 			if (rval != DDI_SUCCESS) {
9784 				/* DMA mapping failed */
9785 				(void) ddi_dma_free_handle(
9786 				    &spx->txlt_buf_dma_handle);
9787 				spx->txlt_buf_dma_handle = NULL;
9788 #ifdef SATA_DEBUG
9789 				mbuffail_count++;
9790 #endif
9791 				SATADBG1(SATA_DBG_DMA_SETUP,
9792 				    spx->txlt_sata_hba_inst,
9793 				    "sata_dma_buf_setup: "
9794 				    "buf dma mem alloc failed %x\n", rval);
9795 				return (rval);
9796 			}
9797 			ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf,
9798 			    cur_dma_attr->dma_attr_align));
9799 
9800 #ifdef SATA_DEBUG
9801 			mbuf_count++;
9802 
9803 			if (bp->b_bcount != bufsz)
9804 				/*
9805 				 * This will require special handling, because
9806 				 * DMA cookies will be based on the temporary
9807 				 * buffer size, not the original buffer
9808 				 * b_bcount, so the residue may have to
9809 				 * be counted differently.
9810 				 */
9811 				SATADBG2(SATA_DBG_DMA_SETUP,
9812 				    spx->txlt_sata_hba_inst,
9813 				    "sata_dma_buf_setup: bp size %x != "
9814 				    "bufsz %x\n", bp->b_bcount, bufsz);
9815 #endif
9816 			if (dma_flags & DDI_DMA_WRITE) {
9817 				/*
9818 				 * Write operation - copy data into
9819 				 * an aligned temporary buffer. Buffer will be
9820 				 * synced for device by ddi_dma_addr_bind_handle
9821 				 */
9822 				bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf,
9823 				    bp->b_bcount);
9824 			}
9825 
9826 			rval = ddi_dma_addr_bind_handle(
9827 			    spx->txlt_buf_dma_handle,
9828 			    NULL,
9829 			    spx->txlt_tmp_buf,
9830 			    bufsz, dma_flags, ddicallback, 0,
9831 			    &spx->txlt_dma_cookie,
9832 			    &spx->txlt_curwin_num_dma_cookies);
9833 		}
9834 
9835 		switch (rval) {
9836 		case DDI_DMA_PARTIAL_MAP:
9837 			SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
9838 			    "sata_dma_buf_setup: DMA Partial Map\n", NULL);
9839 			/*
9840 			 * Partial DMA mapping.
9841 			 * Retrieve number of DMA windows for this request.
9842 			 */
9843 			if (ddi_dma_numwin(spx->txlt_buf_dma_handle,
9844 			    &spx->txlt_num_dma_win) != DDI_SUCCESS) {
9845 				if (spx->txlt_tmp_buf != NULL) {
9846 					ddi_dma_mem_free(
9847 					    &spx->txlt_tmp_buf_handle);
9848 					spx->txlt_tmp_buf = NULL;
9849 				}
9850 				(void) ddi_dma_unbind_handle(
9851 				    spx->txlt_buf_dma_handle);
9852 				(void) ddi_dma_free_handle(
9853 				    &spx->txlt_buf_dma_handle);
9854 				spx->txlt_buf_dma_handle = NULL;
9855 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9856 				    "sata_dma_buf_setup: numwin failed\n"));
9857 				return (DDI_FAILURE);
9858 			}
9859 			SATADBG2(SATA_DBG_DMA_SETUP,
9860 			    spx->txlt_sata_hba_inst,
9861 			    "sata_dma_buf_setup: windows: %d, cookies: %d\n",
9862 			    spx->txlt_num_dma_win,
9863 			    spx->txlt_curwin_num_dma_cookies);
9864 			spx->txlt_cur_dma_win = 0;
9865 			break;
9866 
9867 		case DDI_DMA_MAPPED:
9868 			/* DMA fully mapped */
9869 			spx->txlt_num_dma_win = 1;
9870 			spx->txlt_cur_dma_win = 0;
9871 			SATADBG1(SATA_DBG_DMA_SETUP,
9872 			    spx->txlt_sata_hba_inst,
9873 			    "sata_dma_buf_setup: windows: 1 "
9874 			    "cookies: %d\n", spx->txlt_curwin_num_dma_cookies);
9875 			break;
9876 
9877 		default:
9878 			/* DMA mapping failed */
9879 			if (spx->txlt_tmp_buf != NULL) {
9880 				ddi_dma_mem_free(
9881 				    &spx->txlt_tmp_buf_handle);
9882 				spx->txlt_tmp_buf = NULL;
9883 			}
9884 			(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
9885 			spx->txlt_buf_dma_handle = NULL;
9886 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9887 			    "sata_dma_buf_setup: buf dma handle binding "
9888 			    "failed %x\n", rval));
9889 			return (rval);
9890 		}
9891 		spx->txlt_curwin_processed_dma_cookies = 0;
9892 		spx->txlt_dma_cookie_list = NULL;
9893 	} else {
9894 		/*
9895 		 * DMA setup is reused. Check if we need to process more
9896 		 * cookies in current window, or to get next window, if any.
9897 		 */
9898 
9899 		ASSERT(spx->txlt_curwin_processed_dma_cookies <=
9900 		    spx->txlt_curwin_num_dma_cookies);
9901 
9902 		if (spx->txlt_curwin_processed_dma_cookies ==
9903 		    spx->txlt_curwin_num_dma_cookies) {
9904 			/*
9905 			 * All cookies from current DMA window were processed.
9906 			 * Get next DMA window.
9907 			 */
9908 			spx->txlt_cur_dma_win++;
9909 			if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) {
9910 				(void) ddi_dma_getwin(spx->txlt_buf_dma_handle,
9911 				    spx->txlt_cur_dma_win, &offset, &size,
9912 				    &spx->txlt_dma_cookie,
9913 				    &spx->txlt_curwin_num_dma_cookies);
9914 				spx->txlt_curwin_processed_dma_cookies = 0;
9915 			} else {
9916 				/* No more windows! End of request! */
9917 				/* What to do? - panic for now */
9918 				ASSERT(spx->txlt_cur_dma_win >=
9919 				    spx->txlt_num_dma_win);
9920 
9921 				spx->txlt_curwin_num_dma_cookies = 0;
9922 				spx->txlt_curwin_processed_dma_cookies = 0;
9923 				spx->txlt_sata_pkt->
9924 				    satapkt_cmd.satacmd_num_dma_cookies = 0;
9925 				return (DDI_SUCCESS);
9926 			}
9927 		}
9928 	}
9929 	/* There better be at least one DMA cookie outstanding */
9930 	ASSERT((spx->txlt_curwin_num_dma_cookies -
9931 	    spx->txlt_curwin_processed_dma_cookies) > 0);
9932 
9933 	if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) {
9934 		/* The default cookie slot was used in previous run */
9935 		ASSERT(spx->txlt_curwin_processed_dma_cookies == 0);
9936 		spx->txlt_dma_cookie_list = NULL;
9937 		spx->txlt_dma_cookie_list_len = 0;
9938 	}
9939 	if (spx->txlt_curwin_processed_dma_cookies == 0) {
9940 		/*
9941 		 * Processing a new DMA window - set-up dma cookies list.
9942 		 * We may reuse previously allocated cookie array if it is
9943 		 * possible.
9944 		 */
9945 		if (spx->txlt_dma_cookie_list != NULL &&
9946 		    spx->txlt_dma_cookie_list_len <
9947 		    spx->txlt_curwin_num_dma_cookies) {
9948 			/*
9949 			 * New DMA window contains more cookies than
9950 			 * the previous one. We need larger cookie list - free
9951 			 * the old one.
9952 			 */
9953 			(void) kmem_free(spx->txlt_dma_cookie_list,
9954 			    spx->txlt_dma_cookie_list_len *
9955 			    sizeof (ddi_dma_cookie_t));
9956 			spx->txlt_dma_cookie_list = NULL;
9957 			spx->txlt_dma_cookie_list_len = 0;
9958 		}
9959 		if (spx->txlt_dma_cookie_list == NULL) {
9960 			/*
9961 			 * Calculate lesser of number of cookies in this
9962 			 * DMA window and number of s/g entries.
9963 			 */
9964 			max_sg_len = cur_dma_attr->dma_attr_sgllen;
9965 			req_len = MIN(max_sg_len,
9966 			    spx->txlt_curwin_num_dma_cookies);
9967 
9968 			/* Allocate new dma cookie array if necessary */
9969 			if (req_len == 1) {
9970 				/* Only one cookie - no need for a list */
9971 				spx->txlt_dma_cookie_list =
9972 				    &spx->txlt_dma_cookie;
9973 				spx->txlt_dma_cookie_list_len = 1;
9974 			} else {
9975 				/*
9976 				 * More than one cookie - try to allocate space.
9977 				 */
9978 				spx->txlt_dma_cookie_list = kmem_zalloc(
9979 				    sizeof (ddi_dma_cookie_t) * req_len,
9980 				    callback == NULL_FUNC ? KM_NOSLEEP :
9981 				    KM_SLEEP);
9982 				if (spx->txlt_dma_cookie_list == NULL) {
9983 					SATADBG1(SATA_DBG_DMA_SETUP,
9984 					    spx->txlt_sata_hba_inst,
9985 					    "sata_dma_buf_setup: cookie list "
9986 					    "allocation failed\n", NULL);
9987 					/*
9988 					 * We could not allocate space for
9989 					 * neccessary number of dma cookies in
9990 					 * this window, so we fail this request.
9991 					 * Next invocation would try again to
9992 					 * allocate space for cookie list.
9993 					 * Note:Packet residue was not modified.
9994 					 */
9995 					return (DDI_DMA_NORESOURCES);
9996 				} else {
9997 					spx->txlt_dma_cookie_list_len = req_len;
9998 				}
9999 			}
10000 		}
10001 		/*
10002 		 * Fetch DMA cookies into cookie list in sata_pkt_txlate.
10003 		 * First cookie was already fetched.
10004 		 */
10005 		*(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie;
10006 		cur_txfer_len =
10007 		    (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size;
10008 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1;
10009 		spx->txlt_curwin_processed_dma_cookies++;
10010 		for (i = 1; (i < spx->txlt_dma_cookie_list_len) &&
10011 		    (i < spx->txlt_curwin_num_dma_cookies); i++) {
10012 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
10013 			    &spx->txlt_dma_cookie_list[i]);
10014 			cur_txfer_len +=
10015 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
10016 			spx->txlt_curwin_processed_dma_cookies++;
10017 			spx->txlt_sata_pkt->
10018 			    satapkt_cmd.satacmd_num_dma_cookies += 1;
10019 		}
10020 	} else {
10021 		SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
10022 		    "sata_dma_buf_setup: sliding within DMA window, "
10023 		    "cur cookie %d, total cookies %d\n",
10024 		    spx->txlt_curwin_processed_dma_cookies,
10025 		    spx->txlt_curwin_num_dma_cookies);
10026 
10027 		/*
10028 		 * Not all cookies from the current dma window were used because
10029 		 * of s/g limitation.
10030 		 * There is no need to re-size the list - it was set at
10031 		 * optimal size, or only default entry is used (s/g = 1).
10032 		 */
10033 		if (spx->txlt_dma_cookie_list == NULL) {
10034 			spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie;
10035 			spx->txlt_dma_cookie_list_len = 1;
10036 		}
10037 		/*
10038 		 * Since we are processing remaining cookies in a DMA window,
10039 		 * there may be less of them than the number of entries in the
10040 		 * current dma cookie list.
10041 		 */
10042 		req_len = MIN(spx->txlt_dma_cookie_list_len,
10043 		    (spx->txlt_curwin_num_dma_cookies -
10044 		    spx->txlt_curwin_processed_dma_cookies));
10045 
10046 		/* Fetch the next batch of cookies */
10047 		for (i = 0, cur_txfer_len = 0; i < req_len; i++) {
10048 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
10049 			    &spx->txlt_dma_cookie_list[i]);
10050 			cur_txfer_len +=
10051 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
10052 			spx->txlt_sata_pkt->
10053 			    satapkt_cmd.satacmd_num_dma_cookies++;
10054 			spx->txlt_curwin_processed_dma_cookies++;
10055 		}
10056 	}
10057 
10058 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0);
10059 
10060 	/* Point sata_cmd to the cookie list */
10061 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list =
10062 	    &spx->txlt_dma_cookie_list[0];
10063 
10064 	/* Remember number of DMA cookies passed in sata packet */
10065 	spx->txlt_num_dma_cookies =
10066 	    spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies;
10067 
10068 	ASSERT(cur_txfer_len != 0);
10069 	if (cur_txfer_len <= bp->b_bcount)
10070 		spx->txlt_total_residue -= cur_txfer_len;
10071 	else {
10072 		/*
10073 		 * Temporary DMA buffer has been padded by
10074 		 * ddi_dma_mem_alloc()!
10075 		 * This requires special handling, because DMA cookies are
10076 		 * based on the temporary buffer size, not the b_bcount,
10077 		 * and we have extra bytes to transfer - but the packet
10078 		 * residue has to stay correct because we will copy only
10079 		 * the requested number of bytes.
10080 		 */
10081 		spx->txlt_total_residue -= bp->b_bcount;
10082 	}
10083 
10084 	return (DDI_SUCCESS);
10085 }
10086 
10087 
10088 /*
10089  * Fetch Device Identify data.
10090  * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type)
10091  * command to a device and get the device identify data.
10092  * The device_info structure has to be set to device type (for selecting proper
10093  * device identify command).
10094  *
10095  * Returns:
10096  * SATA_SUCCESS if cmd succeeded
10097  * SATA_RETRY if cmd was rejected and could be retried,
10098  * SATA_FAILURE if cmd failed and should not be retried (port error)
10099  *
10100  * Cannot be called in an interrupt context.
10101  */
10102 
10103 static int
10104 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst,
10105     sata_drive_info_t *sdinfo)
10106 {
10107 	struct buf *bp;
10108 	sata_pkt_t *spkt;
10109 	sata_cmd_t *scmd;
10110 	sata_pkt_txlate_t *spx;
10111 	int rval;
10112 
10113 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10114 	spx->txlt_sata_hba_inst = sata_hba_inst;
10115 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
10116 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10117 	if (spkt == NULL) {
10118 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10119 		return (SATA_RETRY); /* may retry later */
10120 	}
10121 	/* address is needed now */
10122 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10123 
10124 	/*
10125 	 * Allocate buffer for Identify Data return data
10126 	 */
10127 	bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t));
10128 	if (bp == NULL) {
10129 		sata_pkt_free(spx);
10130 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10131 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10132 		    "sata_fetch_device_identify_data: "
10133 		    "cannot allocate buffer for ID"));
10134 		return (SATA_RETRY); /* may retry later */
10135 	}
10136 
10137 	/* Fill sata_pkt */
10138 	sdinfo->satadrv_state = SATA_STATE_PROBING;
10139 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10140 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10141 	/* Synchronous mode, no callback */
10142 	spkt->satapkt_comp = NULL;
10143 	/* Timeout 30s */
10144 	spkt->satapkt_time = sata_default_pkt_time;
10145 
10146 	scmd = &spkt->satapkt_cmd;
10147 	scmd->satacmd_bp = bp;
10148 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
10149 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10150 
10151 	/* Build Identify Device cmd in the sata_pkt */
10152 	scmd->satacmd_addr_type = 0;		/* N/A */
10153 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
10154 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
10155 	scmd->satacmd_lba_mid_lsb = 0;		/* N/A */
10156 	scmd->satacmd_lba_high_lsb = 0;		/* N/A */
10157 	scmd->satacmd_features_reg = 0;		/* N/A */
10158 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
10159 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
10160 		/* Identify Packet Device cmd */
10161 		scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE;
10162 	} else {
10163 		/* Identify Device cmd - mandatory for all other devices */
10164 		scmd->satacmd_cmd_reg = SATAC_ID_DEVICE;
10165 	}
10166 
10167 	/* Send pkt to SATA HBA driver */
10168 	rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt);
10169 
10170 #ifdef SATA_INJECT_FAULTS
10171 	if (sata_inject_fault == SATA_INJECT_PKT_FAULT)
10172 		if (sata_fault_cmd == scmd->satacmd_cmd_reg)
10173 			sata_inject_pkt_fault(spkt, scmd->satacmd_cmd_reg,
10174 			    &rval, sata_inject_fault_type);
10175 #endif
10176 
10177 	if (rval == SATA_TRAN_ACCEPTED &&
10178 	    spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10179 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10180 		    DDI_DMA_SYNC_FORKERNEL);
10181 		ASSERT(rval == DDI_SUCCESS);
10182 		if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config &
10183 		    SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) {
10184 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10185 			    "SATA disk device at port %d - "
10186 			    "partial Identify Data",
10187 			    sdinfo->satadrv_addr.cport));
10188 			rval = SATA_RETRY; /* may retry later */
10189 			goto fail;
10190 		}
10191 		/* Update sata_drive_info */
10192 		bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id,
10193 		    sizeof (sata_id_t));
10194 
10195 		sdinfo->satadrv_features_support = 0;
10196 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10197 			/*
10198 			 * Retrieve capacity (disks only) and addressing mode
10199 			 */
10200 			sdinfo->satadrv_capacity = sata_check_capacity(sdinfo);
10201 		} else {
10202 			/*
10203 			 * For ATAPI devices one would have to issue
10204 			 * Get Capacity cmd for media capacity. Not here.
10205 			 */
10206 			sdinfo->satadrv_capacity = 0;
10207 			/*
10208 			 * Check what cdb length is supported
10209 			 */
10210 			if ((sdinfo->satadrv_id.ai_config &
10211 			    SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B)
10212 				sdinfo->satadrv_atapi_cdb_len = 16;
10213 			else
10214 				sdinfo->satadrv_atapi_cdb_len = 12;
10215 		}
10216 		/* Setup supported features flags */
10217 		if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT)
10218 			sdinfo->satadrv_features_support |= SATA_DEV_F_DMA;
10219 
10220 		/* Check for SATA GEN and NCQ support */
10221 		if (sdinfo->satadrv_id.ai_satacap != 0 &&
10222 		    sdinfo->satadrv_id.ai_satacap != 0xffff) {
10223 			/* SATA compliance */
10224 			if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ)
10225 				sdinfo->satadrv_features_support |=
10226 				    SATA_DEV_F_NCQ;
10227 			if (sdinfo->satadrv_id.ai_satacap &
10228 			    (SATA_1_SPEED | SATA_2_SPEED)) {
10229 				if (sdinfo->satadrv_id.ai_satacap &
10230 				    SATA_2_SPEED)
10231 					sdinfo->satadrv_features_support |=
10232 					    SATA_DEV_F_SATA2;
10233 				if (sdinfo->satadrv_id.ai_satacap &
10234 				    SATA_1_SPEED)
10235 					sdinfo->satadrv_features_support |=
10236 					    SATA_DEV_F_SATA1;
10237 			} else {
10238 				sdinfo->satadrv_features_support |=
10239 				    SATA_DEV_F_SATA1;
10240 			}
10241 		}
10242 		if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) &&
10243 		    (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD))
10244 			sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ;
10245 
10246 		sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth;
10247 		if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) ||
10248 		    (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) {
10249 			++sdinfo->satadrv_queue_depth;
10250 			/* Adjust according to controller capabilities */
10251 			sdinfo->satadrv_max_queue_depth = MIN(
10252 			    sdinfo->satadrv_queue_depth,
10253 			    SATA_QDEPTH(sata_hba_inst));
10254 			/* Adjust according to global queue depth limit */
10255 			sdinfo->satadrv_max_queue_depth = MIN(
10256 			    sdinfo->satadrv_max_queue_depth,
10257 			    sata_current_max_qdepth);
10258 			if (sdinfo->satadrv_max_queue_depth == 0)
10259 				sdinfo->satadrv_max_queue_depth = 1;
10260 		} else
10261 			sdinfo->satadrv_max_queue_depth = 1;
10262 
10263 		rval = SATA_SUCCESS;
10264 	} else {
10265 		/*
10266 		 * Woops, no Identify Data.
10267 		 */
10268 		if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) {
10269 			rval = SATA_RETRY; /* may retry later */
10270 		} else if (rval == SATA_TRAN_ACCEPTED) {
10271 			if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR ||
10272 			    spkt->satapkt_reason == SATA_PKT_ABORTED ||
10273 			    spkt->satapkt_reason == SATA_PKT_TIMEOUT ||
10274 			    spkt->satapkt_reason == SATA_PKT_RESET)
10275 				rval = SATA_RETRY; /* may retry later */
10276 			else
10277 				rval = SATA_FAILURE;
10278 		} else {
10279 			rval = SATA_FAILURE;
10280 		}
10281 	}
10282 fail:
10283 	/* Free allocated resources */
10284 	sata_free_local_buffer(spx);
10285 	sata_pkt_free(spx);
10286 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
10287 
10288 	return (rval);
10289 }
10290 
10291 
10292 /*
10293  * Some devices may not come-up with default DMA mode (UDMA or MWDMA).
10294  * UDMA mode is checked first, followed by MWDMA mode.
10295  * set correctly, so this function is setting it to the highest supported level.
10296  * Older SATA spec required that the device supports at least DMA 4 mode and
10297  * UDMA mode is selected.  It is not mentioned in SerialATA 2.6, so this
10298  * restriction has been removed.
10299  *
10300  * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported.
10301  * Returns SATA_FAILURE if proper DMA mode could not be selected.
10302  *
10303  * NOTE: This function should be called only if DMA mode is supported.
10304  */
10305 static int
10306 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo)
10307 {
10308 	sata_pkt_t *spkt;
10309 	sata_cmd_t *scmd;
10310 	sata_pkt_txlate_t *spx;
10311 	int i, mode;
10312 	uint8_t subcmd;
10313 	int rval = SATA_SUCCESS;
10314 
10315 	ASSERT(sdinfo != NULL);
10316 	ASSERT(sata_hba_inst != NULL);
10317 
10318 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
10319 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) {
10320 		/* Find highest Ultra DMA mode supported */
10321 		for (mode = 6; mode >= 0; --mode) {
10322 			if (sdinfo->satadrv_id.ai_ultradma & (1 << mode))
10323 				break;
10324 		}
10325 #if 0
10326 		/* Left for historical reasons */
10327 		/*
10328 		 * Some initial version of SATA spec indicated that at least
10329 		 * UDMA mode 4 has to be supported. It is not mentioned in
10330 		 * SerialATA 2.6, so this restriction is removed.
10331 		 */
10332 		if (mode < 4)
10333 			return (SATA_FAILURE);
10334 #endif
10335 		/* Find UDMA mode currently selected */
10336 		for (i = 6; i >= 0; --i) {
10337 			if (sdinfo->satadrv_id.ai_ultradma & (1 << (i + 8)))
10338 				break;
10339 		}
10340 		if (i >= mode)
10341 			/* Nothing to do */
10342 			return (SATA_SUCCESS);
10343 
10344 		subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA;
10345 
10346 	} else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) {
10347 		/* Find highest MultiWord DMA mode supported */
10348 		for (mode = 2; mode >= 0; --mode) {
10349 			if (sdinfo->satadrv_id.ai_dworddma & (1 << mode))
10350 				break;
10351 		}
10352 		/* Find highest MultiWord DMA mode selected */
10353 		for (i = 2; i >= 0; --i) {
10354 			if (sdinfo->satadrv_id.ai_dworddma & (1 << (i + 8)))
10355 				break;
10356 		}
10357 		if (i >= mode)
10358 			/* Nothing to do */
10359 			return (SATA_SUCCESS);
10360 
10361 		subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA;
10362 	} else
10363 		return (SATA_SUCCESS);
10364 
10365 	/*
10366 	 * Set DMA mode via SET FEATURES COMMAND.
10367 	 * Prepare packet for SET FEATURES COMMAND.
10368 	 */
10369 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10370 	spx->txlt_sata_hba_inst = sata_hba_inst;
10371 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10372 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10373 	if (spkt == NULL) {
10374 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10375 		    "sata_set_dma_mode: could not set DMA mode %", mode));
10376 		rval = SATA_FAILURE;
10377 		goto done;
10378 	}
10379 	/* Fill sata_pkt */
10380 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10381 	/* Timeout 30s */
10382 	spkt->satapkt_time = sata_default_pkt_time;
10383 	/* Synchronous mode, no callback, interrupts */
10384 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10385 	spkt->satapkt_comp = NULL;
10386 	scmd = &spkt->satapkt_cmd;
10387 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10388 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10389 	scmd->satacmd_addr_type = 0;
10390 	scmd->satacmd_device_reg = 0;
10391 	scmd->satacmd_status_reg = 0;
10392 	scmd->satacmd_error_reg = 0;
10393 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10394 	scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE;
10395 	scmd->satacmd_sec_count_lsb = subcmd | mode;
10396 
10397 	/* Transfer command to HBA */
10398 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
10399 	    spkt) != SATA_TRAN_ACCEPTED ||
10400 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
10401 		/* Pkt execution failed */
10402 		rval = SATA_FAILURE;
10403 	}
10404 done:
10405 
10406 	/* Free allocated resources */
10407 	if (spkt != NULL)
10408 		sata_pkt_free(spx);
10409 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10410 
10411 	return (rval);
10412 }
10413 
10414 
10415 /*
10416  * Set device caching mode.
10417  * One of the following operations should be specified:
10418  * SATAC_SF_ENABLE_READ_AHEAD
10419  * SATAC_SF_DISABLE_READ_AHEAD
10420  * SATAC_SF_ENABLE_WRITE_CACHE
10421  * SATAC_SF_DISABLE_WRITE_CACHE
10422  *
10423  * If operation fails, system log messgage is emitted.
10424  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
10425  */
10426 
10427 static int
10428 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
10429     int cache_op)
10430 {
10431 	sata_pkt_t *spkt;
10432 	sata_cmd_t *scmd;
10433 	sata_pkt_txlate_t *spx;
10434 	int rval = SATA_SUCCESS;
10435 	char *infop;
10436 
10437 	ASSERT(sdinfo != NULL);
10438 	ASSERT(sata_hba_inst != NULL);
10439 	ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD ||
10440 	    cache_op == SATAC_SF_DISABLE_READ_AHEAD ||
10441 	    cache_op == SATAC_SF_ENABLE_WRITE_CACHE ||
10442 	    cache_op == SATAC_SF_DISABLE_WRITE_CACHE);
10443 
10444 
10445 	/* Prepare packet for SET FEATURES COMMAND */
10446 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10447 	spx->txlt_sata_hba_inst = sata_hba_inst;
10448 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10449 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10450 	if (spkt == NULL) {
10451 		rval = SATA_FAILURE;
10452 		goto failure;
10453 	}
10454 	/* Fill sata_pkt */
10455 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10456 	/* Timeout 30s */
10457 	spkt->satapkt_time = sata_default_pkt_time;
10458 	/* Synchronous mode, no callback, interrupts */
10459 	spkt->satapkt_op_mode =
10460 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10461 	spkt->satapkt_comp = NULL;
10462 	scmd = &spkt->satapkt_cmd;
10463 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10464 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10465 	scmd->satacmd_addr_type = 0;
10466 	scmd->satacmd_device_reg = 0;
10467 	scmd->satacmd_status_reg = 0;
10468 	scmd->satacmd_error_reg = 0;
10469 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10470 	scmd->satacmd_features_reg = cache_op;
10471 
10472 	/* Transfer command to HBA */
10473 	if (((*SATA_START_FUNC(sata_hba_inst))(
10474 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
10475 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
10476 		/* Pkt execution failed */
10477 		switch (cache_op) {
10478 		case SATAC_SF_ENABLE_READ_AHEAD:
10479 			infop = "enabling read ahead failed";
10480 			break;
10481 		case SATAC_SF_DISABLE_READ_AHEAD:
10482 			infop = "disabling read ahead failed";
10483 			break;
10484 		case SATAC_SF_ENABLE_WRITE_CACHE:
10485 			infop = "enabling write cache failed";
10486 			break;
10487 		case SATAC_SF_DISABLE_WRITE_CACHE:
10488 			infop = "disabling write cache failed";
10489 			break;
10490 		}
10491 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
10492 		rval = SATA_FAILURE;
10493 	}
10494 failure:
10495 	/* Free allocated resources */
10496 	if (spkt != NULL)
10497 		sata_pkt_free(spx);
10498 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10499 	return (rval);
10500 }
10501 
10502 /*
10503  * Set Removable Media Status Notification (enable/disable)
10504  * state == 0 , disable
10505  * state != 0 , enable
10506  *
10507  * If operation fails, system log messgage is emitted.
10508  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
10509  */
10510 
10511 static int
10512 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
10513     int state)
10514 {
10515 	sata_pkt_t *spkt;
10516 	sata_cmd_t *scmd;
10517 	sata_pkt_txlate_t *spx;
10518 	int rval = SATA_SUCCESS;
10519 	char *infop;
10520 
10521 	ASSERT(sdinfo != NULL);
10522 	ASSERT(sata_hba_inst != NULL);
10523 
10524 	/* Prepare packet for SET FEATURES COMMAND */
10525 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10526 	spx->txlt_sata_hba_inst = sata_hba_inst;
10527 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10528 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10529 	if (spkt == NULL) {
10530 		rval = SATA_FAILURE;
10531 		goto failure;
10532 	}
10533 	/* Fill sata_pkt */
10534 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10535 	/* Timeout 30s */
10536 	spkt->satapkt_time = sata_default_pkt_time;
10537 	/* Synchronous mode, no callback, interrupts */
10538 	spkt->satapkt_op_mode =
10539 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10540 	spkt->satapkt_comp = NULL;
10541 	scmd = &spkt->satapkt_cmd;
10542 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10543 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10544 	scmd->satacmd_addr_type = 0;
10545 	scmd->satacmd_device_reg = 0;
10546 	scmd->satacmd_status_reg = 0;
10547 	scmd->satacmd_error_reg = 0;
10548 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10549 	if (state == 0)
10550 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN;
10551 	else
10552 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN;
10553 
10554 	/* Transfer command to HBA */
10555 	if (((*SATA_START_FUNC(sata_hba_inst))(
10556 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
10557 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
10558 		/* Pkt execution failed */
10559 		if (state == 0)
10560 			infop = "disabling Removable Media Status "
10561 			    "Notification failed";
10562 		else
10563 			infop = "enabling Removable Media Status "
10564 			    "Notification failed";
10565 
10566 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
10567 		rval = SATA_FAILURE;
10568 	}
10569 failure:
10570 	/* Free allocated resources */
10571 	if (spkt != NULL)
10572 		sata_pkt_free(spx);
10573 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10574 	return (rval);
10575 }
10576 
10577 
10578 /*
10579  * Update port SCR block
10580  */
10581 static void
10582 sata_update_port_scr(sata_port_scr_t *port_scr, sata_device_t *device)
10583 {
10584 	port_scr->sstatus = device->satadev_scr.sstatus;
10585 	port_scr->serror = device->satadev_scr.serror;
10586 	port_scr->scontrol = device->satadev_scr.scontrol;
10587 	port_scr->sactive = device->satadev_scr.sactive;
10588 	port_scr->snotific = device->satadev_scr.snotific;
10589 }
10590 
10591 /*
10592  * Update state and copy port ss* values from passed sata_device structure.
10593  * sata_address is validated - if not valid, nothing is changed in sata_scsi
10594  * configuration struct.
10595  *
10596  * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function
10597  * regardless of the state in device argument.
10598  *
10599  * Port mutex should be held while calling this function.
10600  */
10601 static void
10602 sata_update_port_info(sata_hba_inst_t *sata_hba_inst,
10603 	sata_device_t *sata_device)
10604 {
10605 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst,
10606 	    sata_device->satadev_addr.cport)));
10607 
10608 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT ||
10609 	    sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
10610 
10611 		sata_cport_info_t *cportinfo;
10612 
10613 		if (SATA_NUM_CPORTS(sata_hba_inst) <=
10614 		    sata_device->satadev_addr.cport)
10615 			return;
10616 
10617 		cportinfo = SATA_CPORT_INFO(sata_hba_inst,
10618 		    sata_device->satadev_addr.cport);
10619 		sata_update_port_scr(&cportinfo->cport_scr, sata_device);
10620 
10621 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
10622 		cportinfo->cport_state &= ~(SATA_PSTATE_PWRON |
10623 		    SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
10624 		cportinfo->cport_state |=
10625 		    sata_device->satadev_state & SATA_PSTATE_VALID;
10626 	} else {
10627 		sata_pmport_info_t *pmportinfo;
10628 
10629 		if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT) ||
10630 		    (sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) ||
10631 		    SATA_NUM_PMPORTS(sata_hba_inst,
10632 		    sata_device->satadev_addr.cport) <
10633 		    sata_device->satadev_addr.pmport)
10634 			return;
10635 
10636 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
10637 		    sata_device->satadev_addr.cport,
10638 		    sata_device->satadev_addr.pmport);
10639 		sata_update_port_scr(&pmportinfo->pmport_scr, sata_device);
10640 
10641 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
10642 		pmportinfo->pmport_state &=
10643 		    ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF |
10644 		    SATA_PSTATE_FAILED);
10645 		pmportinfo->pmport_state |=
10646 		    sata_device->satadev_state & SATA_PSTATE_VALID;
10647 	}
10648 }
10649 
10650 
10651 
10652 /*
10653  * Extract SATA port specification from an IOCTL argument.
10654  *
10655  * This function return the port the user land send us as is, unless it
10656  * cannot retrieve port spec, then -1 is returned.
10657  *
10658  * Note: Only cport  - no port multiplier port.
10659  */
10660 static int32_t
10661 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp)
10662 {
10663 	int32_t port;
10664 
10665 	/* Extract port number from nvpair in dca structure  */
10666 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
10667 		SATA_LOG_D((sata_hba_inst, CE_NOTE,
10668 		    "sata_get_port_num: invalid port spec 0x%x in ioctl",
10669 		    port));
10670 		port = -1;
10671 	}
10672 
10673 	return (port);
10674 }
10675 
10676 /*
10677  * Get dev_info_t pointer to the device node pointed to by port argument.
10678  * NOTE: target argument is a value used in ioctls to identify
10679  * the AP - it is not a sata_address.
10680  * It is a combination of cport, pmport and address qualifier, encodded same
10681  * way as a scsi target number.
10682  * At this moment it carries only cport number.
10683  *
10684  * No PMult hotplug support.
10685  *
10686  * Returns dev_info_t pointer if target device was found, NULL otherwise.
10687  */
10688 
10689 static dev_info_t *
10690 sata_get_target_dip(dev_info_t *dip, int32_t port)
10691 {
10692 	dev_info_t	*cdip = NULL;
10693 	int		target, tgt;
10694 	int		ncport;
10695 	int 		circ;
10696 
10697 	ncport = port & SATA_CFGA_CPORT_MASK;
10698 	target = SATA_TO_SCSI_TARGET(ncport, 0, SATA_ADDR_DCPORT);
10699 
10700 	ndi_devi_enter(dip, &circ);
10701 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
10702 		dev_info_t *next = ddi_get_next_sibling(cdip);
10703 
10704 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
10705 		    DDI_PROP_DONTPASS, "target", -1);
10706 		if (tgt == -1) {
10707 			/*
10708 			 * This is actually an error condition, but not
10709 			 * a fatal one. Just continue the search.
10710 			 */
10711 			cdip = next;
10712 			continue;
10713 		}
10714 
10715 		if (tgt == target)
10716 			break;
10717 
10718 		cdip = next;
10719 	}
10720 	ndi_devi_exit(dip, circ);
10721 
10722 	return (cdip);
10723 }
10724 
10725 /*
10726  * Get dev_info_t pointer to the device node pointed to by port argument.
10727  * NOTE: target argument is a value used in ioctls to identify
10728  * the AP - it is not a sata_address.
10729  * It is a combination of cport, pmport and address qualifier, encoded same
10730  * way as a scsi target number.
10731  * At this moment it carries only cport number.
10732  *
10733  * No PMult hotplug support.
10734  *
10735  * Returns dev_info_t pointer if target device was found, NULL otherwise.
10736  */
10737 
10738 static dev_info_t *
10739 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr)
10740 {
10741 	dev_info_t	*cdip = NULL;
10742 	int		target, tgt;
10743 	int 		circ;
10744 
10745 	target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual);
10746 
10747 	ndi_devi_enter(dip, &circ);
10748 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
10749 		dev_info_t *next = ddi_get_next_sibling(cdip);
10750 
10751 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
10752 		    DDI_PROP_DONTPASS, "target", -1);
10753 		if (tgt == -1) {
10754 			/*
10755 			 * This is actually an error condition, but not
10756 			 * a fatal one. Just continue the search.
10757 			 */
10758 			cdip = next;
10759 			continue;
10760 		}
10761 
10762 		if (tgt == target)
10763 			break;
10764 
10765 		cdip = next;
10766 	}
10767 	ndi_devi_exit(dip, circ);
10768 
10769 	return (cdip);
10770 }
10771 
10772 /*
10773  * Process sata port disconnect request.
10774  * Normally, cfgadm sata plugin will try to offline (unconfigure) the device
10775  * before this request. Nevertheless, if a device is still configured,
10776  * we need to attempt to offline and unconfigure device.
10777  * Regardless of the unconfigure operation results the port is marked as
10778  * deactivated and no access to the attached device is possible.
10779  * If the target node remains because unconfigure operation failed, its state
10780  * will be set to DEVICE_REMOVED, preventing it to be used again when a device
10781  * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure
10782  * the device and remove old target node.
10783  *
10784  * This function invokes sata_hba_inst->satahba_tran->
10785  * sata_tran_hotplug_ops->sata_tran_port_deactivate().
10786  * If successful, the device structure (if any) attached to the specified port
10787  * is removed and state of the port marked appropriately.
10788  * Failure of the port_deactivate may keep port in the physically active state,
10789  * or may fail the port.
10790  *
10791  * NOTE: Port multiplier code is not completed nor tested.
10792  */
10793 
10794 static int
10795 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst,
10796     sata_device_t *sata_device)
10797 {
10798 	sata_drive_info_t *sdinfo = NULL;
10799 	sata_cport_info_t *cportinfo = NULL;
10800 	sata_pmport_info_t *pmportinfo = NULL;
10801 	sata_pmult_info_t *pmultinfo = NULL;
10802 	dev_info_t *tdip;
10803 	int cport, pmport, qual;
10804 	int rval = SATA_SUCCESS;
10805 	int rv = 0;
10806 
10807 	cport = sata_device->satadev_addr.cport;
10808 	pmport = sata_device->satadev_addr.pmport;
10809 	qual = sata_device->satadev_addr.qual;
10810 
10811 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
10812 
10813 	/*
10814 	 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran->
10815 	 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
10816 	 * Do the sanity check.
10817 	 */
10818 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
10819 		/* No physical port deactivation supported. */
10820 		return (EINVAL);
10821 	}
10822 
10823 	/* Check the current state of the port */
10824 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10825 	    (SATA_DIP(sata_hba_inst), sata_device);
10826 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10827 	sata_update_port_info(sata_hba_inst, sata_device);
10828 	if (rval != SATA_SUCCESS ||
10829 	    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
10830 		/* Device port status is unknown or it is in failed state */
10831 		if (qual == SATA_ADDR_PMPORT) {
10832 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
10833 			    SATA_PSTATE_FAILED;
10834 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
10835 			    "sata_hba_ioctl: connect: failed to deactivate "
10836 			    "SATA port %d", cport);
10837 		} else {
10838 			SATA_CPORT_STATE(sata_hba_inst, cport) =
10839 			    SATA_PSTATE_FAILED;
10840 			SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
10841 			    "sata_hba_ioctl: connect: failed to deactivate "
10842 			    "SATA port %d:%d", cport, pmport);
10843 		}
10844 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
10845 		    cport)->cport_mutex);
10846 		return (EIO);
10847 	}
10848 	/*
10849 	 * Set port's dev_state to not ready - this will disable
10850 	 * an access to a potentially attached device.
10851 	 */
10852 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
10853 	if (qual == SATA_ADDR_PMPORT) {
10854 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
10855 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
10856 			sdinfo = pmportinfo->pmport_sata_drive;
10857 			ASSERT(sdinfo != NULL);
10858 		}
10859 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
10860 	} else {
10861 		/* Assuming cport */
10862 
10863 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
10864 			if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
10865 				pmultinfo =
10866 				    cportinfo->cport_devp.cport_sata_pmult;
10867 				ASSERT(pmultinfo != NULL);
10868 			} else {
10869 				sdinfo = cportinfo->cport_devp.cport_sata_drive;
10870 			}
10871 		}
10872 		cportinfo->cport_state &= ~SATA_STATE_READY;
10873 	}
10874 	if (sdinfo != NULL) {
10875 		if ((sdinfo->satadrv_type & (SATA_VALID_DEV_TYPE)) != 0) {
10876 			/*
10877 			 * If a target node exists, try to offline
10878 			 * a device and remove target node.
10879 			 */
10880 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
10881 			    cport)->cport_mutex);
10882 			/* We are addressing attached device, not a port */
10883 			sata_device->satadev_addr.qual =
10884 			    sdinfo->satadrv_addr.qual;
10885 			tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
10886 			    &sata_device->satadev_addr);
10887 			if (tdip != NULL && ndi_devi_offline(tdip,
10888 			    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
10889 				/*
10890 				 * Problem
10891 				 * The target node remained attached.
10892 				 * This happens when the device file was open
10893 				 * or a node was waiting for resources.
10894 				 * Cannot do anything about it.
10895 				 */
10896 				if (qual == SATA_ADDR_CPORT) {
10897 					SATA_LOG_D((sata_hba_inst, CE_WARN,
10898 					    "sata_hba_ioctl: disconnect: could "
10899 					    "not unconfigure device before "
10900 					    "disconnecting the SATA port %d",
10901 					    cport));
10902 				} else {
10903 					SATA_LOG_D((sata_hba_inst, CE_WARN,
10904 					    "sata_hba_ioctl: disconnect: could "
10905 					    "not unconfigure device before "
10906 					    "disconnecting the SATA port %d:%d",
10907 					    cport, pmport));
10908 				}
10909 				/*
10910 				 * Set DEVICE REMOVED state in the target
10911 				 * node. It will prevent access to the device
10912 				 * even when a new device is attached, until
10913 				 * the old target node is released, removed and
10914 				 * recreated for a new  device.
10915 				 */
10916 				sata_set_device_removed(tdip);
10917 
10918 				/*
10919 				 * Instruct event daemon to try the target
10920 				 * node cleanup later.
10921 				 */
10922 				sata_set_target_node_cleanup(
10923 				    sata_hba_inst, &sata_device->satadev_addr);
10924 			}
10925 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
10926 			    cport)->cport_mutex);
10927 		}
10928 
10929 		/* Remove and release sata_drive info structure. */
10930 		if (pmportinfo != NULL) {
10931 			SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport) =
10932 			    NULL;
10933 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
10934 		} else {
10935 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
10936 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
10937 		}
10938 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
10939 	}
10940 #if 0
10941 	else if (pmultinfo != NULL) {
10942 		/*
10943 		 * Port Multiplier itself needs special handling.
10944 		 * All device ports need to be processed here!
10945 		 */
10946 	}
10947 #endif
10948 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10949 	/* Just ask HBA driver to deactivate port */
10950 	/*	sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; */
10951 
10952 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
10953 	    (SATA_DIP(sata_hba_inst), sata_device);
10954 
10955 	/*
10956 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
10957 	 * without the hint (to force listener to investivate the state).
10958 	 */
10959 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
10960 	    SE_NO_HINT);
10961 
10962 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10963 	sata_update_port_info(sata_hba_inst, sata_device);
10964 
10965 	if (rval != SATA_SUCCESS) {
10966 		/*
10967 		 * Port deactivation failure - do not
10968 		 * change port state unless the state
10969 		 * returned by HBA indicates a port failure.
10970 		 * NOTE: device structures were released, so devices now are
10971 		 * invisible! Port reset is needed to re-enumerate devices.
10972 		 */
10973 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
10974 			if (pmportinfo != NULL)
10975 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
10976 			else
10977 				cportinfo->cport_state = SATA_PSTATE_FAILED;
10978 			rv = EIO;
10979 		}
10980 	} else {
10981 		/*
10982 		 * Deactivation succeded. From now on the sata framework
10983 		 * will not care what is happening to the device, until
10984 		 * the port is activated again.
10985 		 */
10986 		cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
10987 	}
10988 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10989 	return (rv);
10990 }
10991 
10992 
10993 
10994 /*
10995  * Process sata port connect request
10996  * The sata cfgadm pluging will invoke this operation only if port was found
10997  * in the disconnect state (failed state is also treated as the disconnected
10998  * state).
10999  * DEVCTL_AP_CONNECT would invoke  sata_hba_inst->satahba_tran->
11000  * sata_tran_hotplug_ops->sata_tran_port_activate().
11001  * If successful and a device is found attached to the port,
11002  * the initialization sequence is executed to attach a device structure to
11003  * a port structure. The state of the port and a device would be set
11004  * appropriately.
11005  * The device is not set in configured state (system-wise) by this operation.
11006  *
11007  * Note, that activating the port may generate link events,
11008  * so it is important that following processing and the
11009  * event processing does not interfere with each other!
11010  *
11011  * This operation may remove port failed state and will
11012  * try to make port active and in good standing.
11013  *
11014  * NOTE: Port multiplier code is not completed nor tested.
11015  */
11016 
11017 static int
11018 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst,
11019     sata_device_t *sata_device)
11020 {
11021 	int cport, pmport, qual;
11022 	int rv = 0;
11023 
11024 	cport = sata_device->satadev_addr.cport;
11025 	pmport = sata_device->satadev_addr.pmport;
11026 	qual = sata_device->satadev_addr.qual;
11027 
11028 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
11029 
11030 	/*
11031 	 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->
11032 	 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate().
11033 	 * Perform sanity check now.
11034 	 */
11035 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) {
11036 		/* No physical port activation supported. */
11037 		return (EINVAL);
11038 	}
11039 
11040 	/* Just ask HBA driver to activate port */
11041 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11042 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11043 		/*
11044 		 * Port activation failure.
11045 		 */
11046 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11047 		    cport)->cport_mutex);
11048 		sata_update_port_info(sata_hba_inst, sata_device);
11049 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11050 			if (qual == SATA_ADDR_DCPORT) {
11051 				SATA_CPORT_STATE(sata_hba_inst, cport) =
11052 				    SATA_PSTATE_FAILED;
11053 				SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
11054 				    "sata_hba_ioctl: connect: failed to "
11055 				    "activate SATA port %d", cport);
11056 			} else { /* port multiplier device port */
11057 				SATA_PMPORT_STATE(sata_hba_inst, cport,
11058 				    pmport) = SATA_PSTATE_FAILED;
11059 				SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
11060 				    "sata_hba_ioctl: connect: failed to "
11061 				    "activate SATA port %d:%d", cport, pmport);
11062 
11063 			}
11064 		}
11065 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11066 		    cport)->cport_mutex);
11067 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
11068 		    "sata_hba_ioctl: connect: failed to activate SATA "
11069 		    "port %d:%d", cport, pmport);
11070 		return (EIO);
11071 	}
11072 
11073 	/* Virgin port state - will be updated by the port re-probe. */
11074 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11075 	if (qual == SATA_ADDR_CPORT)
11076 		SATA_CPORT_STATE(sata_hba_inst, cport) = 0;
11077 	else /* port multiplier device port */
11078 		SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0;
11079 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11080 
11081 	/*
11082 	 * Probe the port to find its state and attached device.
11083 	 */
11084 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11085 	    SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE)
11086 		rv = EIO;
11087 
11088 	/*
11089 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11090 	 * without the hint
11091 	 */
11092 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11093 	    SE_NO_HINT);
11094 
11095 	/*
11096 	 * If there is a device attached to the port, emit
11097 	 * a message.
11098 	 */
11099 	if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11100 
11101 		if (qual == SATA_ADDR_CPORT) {
11102 			sata_log(sata_hba_inst, CE_WARN,
11103 			    "SATA device detected at port %d", cport);
11104 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11105 				/*
11106 				 * A device was not successfully identified
11107 				 */
11108 				sata_log(sata_hba_inst, CE_WARN,
11109 				    "Could not identify SATA "
11110 				    "device at port %d", cport);
11111 			}
11112 		} else { /* port multiplier device port */
11113 			sata_log(sata_hba_inst, CE_WARN,
11114 			    "SATA device detected at port %d:%d",
11115 			    cport, pmport);
11116 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11117 				/*
11118 				 * A device was not successfully identified
11119 				 */
11120 				sata_log(sata_hba_inst, CE_WARN,
11121 				    "Could not identify SATA "
11122 				    "device at port %d:%d", cport, pmport);
11123 			}
11124 		}
11125 	}
11126 
11127 	return (rv);
11128 }
11129 
11130 
11131 /*
11132  * Process sata device unconfigure request.
11133  * The unconfigure operation uses generic nexus operation to
11134  * offline a device. It leaves a target device node attached.
11135  * and obviously sata_drive_info attached as well, because
11136  * from the hardware point of view nothing has changed.
11137  */
11138 static int
11139 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst,
11140     sata_device_t *sata_device)
11141 {
11142 	int rv = 0;
11143 	dev_info_t *tdip;
11144 
11145 	/* We are addressing attached device, not a port */
11146 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
11147 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11148 	else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT)
11149 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11150 
11151 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11152 	    &sata_device->satadev_addr)) != NULL) {
11153 
11154 		if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) {
11155 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11156 			    "sata_hba_ioctl: unconfigure: "
11157 			    "failed to unconfigure device at SATA port %d:%d",
11158 			    sata_device->satadev_addr.cport,
11159 			    sata_device->satadev_addr.pmport));
11160 			rv = EIO;
11161 		}
11162 		/*
11163 		 * The target node devi_state should be marked with
11164 		 * DEVI_DEVICE_OFFLINE by ndi_devi_offline().
11165 		 * This would be the indication for cfgadm that
11166 		 * the AP node occupant state is 'unconfigured'.
11167 		 */
11168 
11169 	} else {
11170 		/*
11171 		 * This would indicate a failure on the part of cfgadm
11172 		 * to detect correct state of the node prior to this
11173 		 * call - one cannot unconfigure non-existing device.
11174 		 */
11175 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11176 		    "sata_hba_ioctl: unconfigure: "
11177 		    "attempt to unconfigure non-existing device "
11178 		    "at SATA port %d:%d",
11179 		    sata_device->satadev_addr.cport,
11180 		    sata_device->satadev_addr.pmport));
11181 		rv = ENXIO;
11182 	}
11183 	return (rv);
11184 }
11185 
11186 /*
11187  * Process sata device configure request
11188  * If port is in a failed state, operation is aborted - one has to use
11189  * an explicit connect or port activate request to try to get a port into
11190  * non-failed mode. Port reset wil also work in such situation.
11191  * If the port is in disconnected (shutdown) state, the connect operation is
11192  * attempted prior to any other action.
11193  * When port is in the active state, there is a device attached and the target
11194  * node exists, a device was most likely offlined.
11195  * If target node does not exist, a new target node is created. In both cases
11196  * an attempt is made to online (configure) the device.
11197  *
11198  * NOTE: Port multiplier code is not completed nor tested.
11199  */
11200 static int
11201 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst,
11202     sata_device_t *sata_device)
11203 {
11204 	int cport, pmport, qual;
11205 	int rval;
11206 	boolean_t target = TRUE;
11207 	sata_cport_info_t *cportinfo;
11208 	sata_pmport_info_t *pmportinfo = NULL;
11209 	dev_info_t *tdip;
11210 	sata_drive_info_t *sdinfo;
11211 
11212 	cport = sata_device->satadev_addr.cport;
11213 	pmport = sata_device->satadev_addr.pmport;
11214 	qual = sata_device->satadev_addr.qual;
11215 
11216 	/* Get current port state */
11217 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11218 	    (SATA_DIP(sata_hba_inst), sata_device);
11219 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11220 	sata_update_port_info(sata_hba_inst, sata_device);
11221 
11222 	if (rval != SATA_SUCCESS ||
11223 	    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
11224 		/*
11225 		 * Obviously, device on a failed port is not visible
11226 		 */
11227 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11228 		return (ENXIO);
11229 	}
11230 
11231 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11232 	if (qual == SATA_ADDR_PMPORT)
11233 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11234 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11235 
11236 	if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) {
11237 		/* need to activate port */
11238 		target = FALSE;
11239 
11240 		/* Sanity check */
11241 		if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
11242 			return (ENXIO);
11243 
11244 		/* Just let HBA driver to activate port */
11245 		if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11246 		    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11247 			/*
11248 			 * Port activation failure - do not change port state
11249 			 * unless the state returned by HBA indicates a port
11250 			 * failure.
11251 			 */
11252 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11253 			    cport)->cport_mutex);
11254 			sata_update_port_info(sata_hba_inst, sata_device);
11255 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11256 				if (qual == SATA_ADDR_PMPORT)
11257 					pmportinfo->pmport_state =
11258 					    SATA_PSTATE_FAILED;
11259 				else
11260 					cportinfo->cport_state =
11261 					    SATA_PSTATE_FAILED;
11262 			}
11263 			mutex_exit(&SATA_CPORT_INFO(
11264 			    sata_hba_inst, cport)->cport_mutex);
11265 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11266 			    "sata_hba_ioctl: configure: "
11267 			    "failed to activate SATA port %d:%d",
11268 			    cport, pmport));
11269 			return (EIO);
11270 		}
11271 		/*
11272 		 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11273 		 * without the hint.
11274 		 */
11275 		sata_gen_sysevent(sata_hba_inst,
11276 		    &sata_device->satadev_addr, SE_NO_HINT);
11277 
11278 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11279 		    cport_mutex);
11280 		/* Virgin port state */
11281 		if (qual == SATA_ADDR_PMPORT)
11282 			pmportinfo->pmport_state = 0;
11283 		else
11284 			cportinfo->cport_state = 0;
11285 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11286 	}
11287 	/*
11288 	 * Always reprobe port, to get current device info.
11289 	 */
11290 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11291 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
11292 		return (EIO);
11293 
11294 	if (sata_device->satadev_type != SATA_DTYPE_NONE && target == FALSE) {
11295 		if (qual == SATA_ADDR_PMPORT) {
11296 			/*
11297 			 * That's the transition from "inactive" port
11298 			 * to active one with device attached.
11299 			 */
11300 			sata_log(sata_hba_inst, CE_WARN,
11301 			    "SATA device detected at port %d:%d",
11302 			    cport, pmport);
11303 		} else {
11304 			/*
11305 			 * When PM is attached to the cport and cport is
11306 			 * activated, every PM device port needs to be reprobed.
11307 			 * We need to emit message for all devices detected
11308 			 * at port multiplier's device ports.
11309 			 * Add such code here.
11310 			 * For now, just inform about device attached to
11311 			 * cport.
11312 			 */
11313 			sata_log(sata_hba_inst, CE_WARN,
11314 			    "SATA device detected at port %d", cport);
11315 		}
11316 	}
11317 
11318 	/*
11319 	 * This is where real configuration operation starts.
11320 	 *
11321 	 * When PM is attached to the cport and cport is activated,
11322 	 * devices attached PM device ports may have to be configured
11323 	 * explicitly. This may change when port multiplier is supported.
11324 	 * For now, configure only disks and other valid target devices.
11325 	 */
11326 	if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) {
11327 		if (qual == SATA_ADDR_CPORT) {
11328 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11329 				/*
11330 				 * A device was not successfully identified
11331 				 */
11332 				sata_log(sata_hba_inst, CE_WARN,
11333 				    "Could not identify SATA "
11334 				    "device at port %d", cport);
11335 			}
11336 		} else { /* port multiplier device port */
11337 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11338 				/*
11339 				 * A device was not successfully identified
11340 				 */
11341 				sata_log(sata_hba_inst, CE_WARN,
11342 				    "Could not identify SATA "
11343 				    "device at port %d:%d", cport, pmport);
11344 			}
11345 		}
11346 		return (ENXIO);		/* No device to configure */
11347 	}
11348 
11349 	/*
11350 	 * Here we may have a device in reset condition,
11351 	 * but because we are just configuring it, there is
11352 	 * no need to process the reset other than just
11353 	 * to clear device reset condition in the HBA driver.
11354 	 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will
11355 	 * cause a first command sent the HBA driver with the request
11356 	 * to clear device reset condition.
11357 	 */
11358 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11359 	if (qual == SATA_ADDR_PMPORT)
11360 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11361 	else
11362 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11363 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
11364 	if (sdinfo == NULL) {
11365 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11366 		return (ENXIO);
11367 	}
11368 	if (sdinfo->satadrv_event_flags &
11369 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
11370 		sdinfo->satadrv_event_flags = 0;
11371 	}
11372 	sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
11373 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11374 
11375 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11376 	    &sata_device->satadev_addr)) != NULL) {
11377 		/*
11378 		 * Target node exists. Verify, that it belongs
11379 		 * to existing, attached device and not to
11380 		 * a removed device.
11381 		 */
11382 		if (sata_check_device_removed(tdip) == B_TRUE) {
11383 			if (qual == SATA_ADDR_DPMPORT)
11384 				sata_log(sata_hba_inst, CE_WARN,
11385 				    "SATA device at port %d cannot be "
11386 				    "configured. "
11387 				    "Application(s) accessing "
11388 				    "previously attached device "
11389 				    "have to release it before newly "
11390 				    "inserted device can be made accessible.",
11391 				    cport);
11392 			else
11393 				sata_log(sata_hba_inst, CE_WARN,
11394 				    "SATA device at port %d:%d cannot be"
11395 				    "configured. "
11396 				    "Application(s) accessing "
11397 				    "previously attached device "
11398 				    "have to release it before newly "
11399 				    "inserted device can be made accessible.",
11400 				    cport, pmport);
11401 			return (EIO);
11402 		}
11403 		/*
11404 		 * Device was not removed and re-inserted.
11405 		 * Try to online it.
11406 		 */
11407 		if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) {
11408 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11409 			    "sata_hba_ioctl: configure: "
11410 			    "onlining device at SATA port "
11411 			    "%d:%d failed", cport, pmport));
11412 			return (EIO);
11413 		}
11414 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11415 		    cport)->cport_mutex);
11416 
11417 		if (qual == SATA_ADDR_DPMPORT)
11418 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
11419 		else
11420 			cportinfo-> cport_tgtnode_clean = B_TRUE;
11421 
11422 		mutex_exit(&SATA_CPORT_INFO(
11423 		    sata_hba_inst, cport)->cport_mutex);
11424 	} else {
11425 		/*
11426 		 * No target node - need to create a new target node.
11427 		 */
11428 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11429 		    cport_mutex);
11430 		if (qual == SATA_ADDR_DPMPORT)
11431 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
11432 		else
11433 			cportinfo-> cport_tgtnode_clean = B_TRUE;
11434 
11435 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11436 		    cport_mutex);
11437 		tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
11438 		    sata_hba_inst, &sata_device->satadev_addr);
11439 		if (tdip == NULL) {
11440 			/* Configure operation failed */
11441 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11442 			    "sata_hba_ioctl: configure: "
11443 			    "configuring SATA device at port %d:%d "
11444 			    "failed", cport, pmport));
11445 			return (EIO);
11446 		}
11447 	}
11448 	return (0);
11449 }
11450 
11451 
11452 /*
11453  * Process ioctl deactivate port request.
11454  * Arbitrarily unconfigure attached device, if any.
11455  * Even if the unconfigure fails, proceed with the
11456  * port deactivation.
11457  *
11458  * NOTE: Port Multiplier code is not completed and tested.
11459  */
11460 
11461 static int
11462 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst,
11463     sata_device_t *sata_device)
11464 {
11465 	int cport, pmport, qual;
11466 	int rval, rv = 0;
11467 	sata_cport_info_t *cportinfo;
11468 	sata_pmport_info_t *pmportinfo = NULL;
11469 	dev_info_t *tdip;
11470 	sata_drive_info_t *sdinfo = NULL;
11471 
11472 	/* Sanity check */
11473 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL)
11474 		return (ENOTSUP);
11475 
11476 	cport = sata_device->satadev_addr.cport;
11477 	pmport = sata_device->satadev_addr.pmport;
11478 	qual = sata_device->satadev_addr.qual;
11479 
11480 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11481 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11482 	if (qual == SATA_ADDR_CPORT) {
11483 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11484 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
11485 			/*
11486 			 * For now, assume that port multiplier is not
11487 			 * supported, i.e. deal only with valid devices
11488 			 */
11489 			if ((cportinfo->cport_dev_type &
11490 			    SATA_VALID_DEV_TYPE) != 0)
11491 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11492 			/*
11493 			 * If attached device is a port multiplier, we will
11494 			 * have to unconfigure all devices attached to the
11495 			 * port multiplier. Add this code here.
11496 			 */
11497 		}
11498 		cportinfo->cport_state &= ~SATA_STATE_READY;
11499 	} else {
11500 		/* Port multiplier device port */
11501 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11502 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11503 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
11504 		    (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0)
11505 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11506 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
11507 	}
11508 
11509 	if (sdinfo != NULL) {
11510 		/*
11511 		 * If a target node exists, try to offline a device and
11512 		 * to remove a target node.
11513 		 */
11514 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11515 		    cport_mutex);
11516 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11517 		    &sata_device->satadev_addr);
11518 		if (tdip != NULL) {
11519 			/* target node exist */
11520 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
11521 			    "sata_hba_ioctl: port deactivate: "
11522 			    "target node exists.", NULL);
11523 
11524 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) !=
11525 			    NDI_SUCCESS) {
11526 				SATA_LOG_D((sata_hba_inst, CE_WARN,
11527 				    "sata_hba_ioctl: port deactivate: "
11528 				    "failed to unconfigure device at port "
11529 				    "%d:%d before deactivating the port",
11530 				    cport, pmport));
11531 				/*
11532 				 * Set DEVICE REMOVED state in the target
11533 				 * node. It will prevent an access to
11534 				 * the device even when a new device is
11535 				 * attached, until the old target node is
11536 				 * released, removed and recreated for a new
11537 				 * device.
11538 				 */
11539 				sata_set_device_removed(tdip);
11540 
11541 				/*
11542 				 * Instruct the event daemon to try the
11543 				 * target node cleanup later.
11544 				 */
11545 				sata_set_target_node_cleanup(sata_hba_inst,
11546 				    &sata_device->satadev_addr);
11547 			}
11548 		}
11549 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11550 		    cport_mutex);
11551 		/*
11552 		 * In any case, remove and release sata_drive_info
11553 		 * structure.
11554 		 */
11555 		if (qual == SATA_ADDR_CPORT) {
11556 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11557 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11558 		} else { /* port multiplier device port */
11559 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11560 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11561 		}
11562 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
11563 	}
11564 	if (qual == SATA_ADDR_CPORT) {
11565 		cportinfo->cport_state &= ~(SATA_STATE_PROBED |
11566 		    SATA_STATE_PROBING);
11567 	} else { /* port multiplier device port */
11568 		pmportinfo->pmport_state &= ~(SATA_STATE_PROBED |
11569 		    SATA_STATE_PROBING);
11570 	}
11571 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11572 
11573 	/* Just let HBA driver to deactivate port */
11574 	sata_device->satadev_addr.qual = qual;
11575 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
11576 	    (SATA_DIP(sata_hba_inst), sata_device);
11577 
11578 	/*
11579 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11580 	 * without the hint
11581 	 */
11582 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11583 	    SE_NO_HINT);
11584 
11585 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11586 	sata_update_port_info(sata_hba_inst, sata_device);
11587 	if (qual == SATA_ADDR_CPORT) {
11588 		if (rval != SATA_SUCCESS) {
11589 			/*
11590 			 * Port deactivation failure - do not change port state
11591 			 * unless the state returned by HBA indicates a port
11592 			 * failure.
11593 			 */
11594 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11595 				SATA_CPORT_STATE(sata_hba_inst, cport) =
11596 				    SATA_PSTATE_FAILED;
11597 			}
11598 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11599 			    "sata_hba_ioctl: port deactivate: "
11600 			    "cannot deactivate SATA port %d", cport));
11601 			rv = EIO;
11602 		} else {
11603 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
11604 		}
11605 	} else {
11606 		if (rval != SATA_SUCCESS) {
11607 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11608 				SATA_PMPORT_STATE(sata_hba_inst, cport,
11609 				    pmport) = SATA_PSTATE_FAILED;
11610 			}
11611 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11612 			    "sata_hba_ioctl: port deactivate: "
11613 			    "cannot deactivate SATA port %d:%d",
11614 			    cport, pmport));
11615 			rv = EIO;
11616 		} else {
11617 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
11618 		}
11619 	}
11620 
11621 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11622 
11623 	return (rv);
11624 }
11625 
11626 /*
11627  * Process ioctl port activate request.
11628  *
11629  * NOTE: Port multiplier code is not completed nor tested.
11630  */
11631 static int
11632 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst,
11633     sata_device_t *sata_device)
11634 {
11635 	int cport, pmport, qual;
11636 	sata_cport_info_t *cportinfo;
11637 	sata_pmport_info_t *pmportinfo = NULL;
11638 	boolean_t dev_existed = TRUE;
11639 
11640 	/* Sanity check */
11641 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
11642 		return (ENOTSUP);
11643 
11644 	cport = sata_device->satadev_addr.cport;
11645 	pmport = sata_device->satadev_addr.pmport;
11646 	qual = sata_device->satadev_addr.qual;
11647 
11648 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11649 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11650 	if (qual == SATA_ADDR_PMPORT) {
11651 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11652 		if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN ||
11653 		    pmportinfo->pmport_dev_type == SATA_DTYPE_NONE)
11654 			dev_existed = FALSE;
11655 	} else { /* cport */
11656 		if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN ||
11657 		    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
11658 			dev_existed = FALSE;
11659 	}
11660 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11661 
11662 	/* Just let HBA driver to activate port, if necessary */
11663 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11664 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11665 		/*
11666 		 * Port activation failure - do not change port state unless
11667 		 * the state returned by HBA indicates a port failure.
11668 		 */
11669 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11670 		    cport)->cport_mutex);
11671 		sata_update_port_info(sata_hba_inst, sata_device);
11672 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11673 			if (qual == SATA_ADDR_PMPORT)
11674 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
11675 			else
11676 				cportinfo->cport_state = SATA_PSTATE_FAILED;
11677 
11678 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11679 			    cport)->cport_mutex);
11680 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11681 			    "sata_hba_ioctl: port activate: cannot activate "
11682 			    "SATA port %d:%d", cport, pmport));
11683 			return (EIO);
11684 		}
11685 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11686 	}
11687 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11688 	if (qual == SATA_ADDR_PMPORT)
11689 		pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN;
11690 	else
11691 		cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN;
11692 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11693 
11694 	/*
11695 	 * Re-probe port to find its current state and possibly attached device.
11696 	 * Port re-probing may change the cportinfo device type if device is
11697 	 * found attached.
11698 	 * If port probing failed, the device type would be set to
11699 	 * SATA_DTYPE_NONE.
11700 	 */
11701 	(void) sata_reprobe_port(sata_hba_inst, sata_device,
11702 	    SATA_DEV_IDENTIFY_RETRY);
11703 
11704 	/*
11705 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11706 	 * without the hint.
11707 	 */
11708 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11709 	    SE_NO_HINT);
11710 
11711 	if (dev_existed == FALSE) {
11712 		if (qual == SATA_ADDR_PMPORT &&
11713 		    pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
11714 			/*
11715 			 * That's the transition from the "inactive" port state
11716 			 * or the active port without a device attached to the
11717 			 * active port state with a device attached.
11718 			 */
11719 			sata_log(sata_hba_inst, CE_WARN,
11720 			    "SATA device detected at port %d:%d",
11721 			    cport, pmport);
11722 		} else if (qual == SATA_ADDR_CPORT &&
11723 		    cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
11724 			/*
11725 			 * That's the transition from the "inactive" port state
11726 			 * or the active port without a device attached to the
11727 			 * active port state with a device attached.
11728 			 */
11729 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
11730 				sata_log(sata_hba_inst, CE_WARN,
11731 				    "SATA device detected at port %d", cport);
11732 			} else {
11733 				sata_log(sata_hba_inst, CE_WARN,
11734 				    "SATA port multiplier detected at port %d",
11735 				    cport);
11736 				/*
11737 				 * Because the detected device is a port
11738 				 * multiplier, we need to reprobe every device
11739 				 * port on the port multiplier and show every
11740 				 * device found attached.
11741 				 * Add this code here.
11742 				 */
11743 			}
11744 		}
11745 	}
11746 	return (0);
11747 }
11748 
11749 
11750 
11751 /*
11752  * Process ioctl reset port request.
11753  *
11754  * NOTE: Port multiplier code is not completed nor tested.
11755  */
11756 static int
11757 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst,
11758     sata_device_t *sata_device)
11759 {
11760 	int cport, pmport, qual;
11761 	int rv = 0;
11762 
11763 	cport = sata_device->satadev_addr.cport;
11764 	pmport = sata_device->satadev_addr.pmport;
11765 	qual = sata_device->satadev_addr.qual;
11766 
11767 	/* Sanity check */
11768 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
11769 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11770 		    "sata_hba_ioctl: sata_hba_tran missing required "
11771 		    "function sata_tran_reset_dport"));
11772 		return (ENOTSUP);
11773 	}
11774 
11775 	/* Ask HBA to reset port */
11776 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
11777 	    sata_device) != SATA_SUCCESS) {
11778 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11779 		    "sata_hba_ioctl: reset port: failed %d:%d",
11780 		    cport, pmport));
11781 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11782 		    cport_mutex);
11783 		sata_update_port_info(sata_hba_inst, sata_device);
11784 		if (qual == SATA_ADDR_CPORT)
11785 			SATA_CPORT_STATE(sata_hba_inst, cport) =
11786 			    SATA_PSTATE_FAILED;
11787 		else
11788 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
11789 			    SATA_PSTATE_FAILED;
11790 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11791 		    cport_mutex);
11792 		rv = EIO;
11793 	}
11794 	/*
11795 	 * Beacuse the port was reset, it should be probed and
11796 	 * attached device reinitialized. At this point the
11797 	 * port state is unknown - it's state is HBA-specific.
11798 	 * Re-probe port to get its state.
11799 	 */
11800 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11801 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) {
11802 		rv = EIO;
11803 	}
11804 	return (rv);
11805 }
11806 
11807 /*
11808  * Process ioctl reset device request.
11809  *
11810  * NOTE: Port multiplier code is not completed nor tested.
11811  */
11812 static int
11813 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst,
11814     sata_device_t *sata_device)
11815 {
11816 	sata_drive_info_t *sdinfo;
11817 	int cport, pmport;
11818 	int rv = 0;
11819 
11820 	/* Sanity check */
11821 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
11822 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11823 		    "sata_hba_ioctl: sata_hba_tran missing required "
11824 		    "function sata_tran_reset_dport"));
11825 		return (ENOTSUP);
11826 	}
11827 
11828 	cport = sata_device->satadev_addr.cport;
11829 	pmport = sata_device->satadev_addr.pmport;
11830 
11831 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11832 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) {
11833 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11834 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
11835 		    sata_device->satadev_addr.cport);
11836 	} else { /* port multiplier */
11837 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11838 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
11839 		    sata_device->satadev_addr.cport,
11840 		    sata_device->satadev_addr.pmport);
11841 	}
11842 	if (sdinfo == NULL) {
11843 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11844 		return (EINVAL);
11845 	}
11846 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11847 
11848 	/* Ask HBA to reset device */
11849 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
11850 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11851 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11852 		    "sata_hba_ioctl: reset device: failed at port %d:%d",
11853 		    cport, pmport));
11854 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11855 		    cport_mutex);
11856 		sata_update_port_info(sata_hba_inst, sata_device);
11857 		/*
11858 		 * Device info structure remains attached. Another device reset
11859 		 * or port disconnect/connect and re-probing is
11860 		 * needed to change it's state
11861 		 */
11862 		sdinfo->satadrv_state &= ~SATA_STATE_READY;
11863 		sdinfo->satadrv_state |= SATA_DSTATE_FAILED;
11864 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11865 		rv = EIO;
11866 	}
11867 	/*
11868 	 * If attached device was a port multiplier, some extra processing
11869 	 * may be needed, to bring it back (if port re-probing did not handle
11870 	 * it). Add such code here.
11871 	 */
11872 	return (rv);
11873 }
11874 
11875 
11876 /*
11877  * Process ioctl reset all request.
11878  *
11879  * NOTE: Port multiplier code is not completed nor tested.
11880  */
11881 static int
11882 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst)
11883 {
11884 	sata_device_t sata_device;
11885 	int rv = 0;
11886 	int tcport;
11887 	int tpmport = 0;
11888 
11889 	sata_device.satadev_rev = SATA_DEVICE_REV;
11890 
11891 	/*
11892 	 * There is no protection here for configured devices.
11893 	 */
11894 	/* Sanity check */
11895 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
11896 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11897 		    "sata_hba_ioctl: sata_hba_tran missing required "
11898 		    "function sata_tran_reset_dport"));
11899 		return (ENOTSUP);
11900 	}
11901 
11902 	/*
11903 	 * Need to lock all ports, not just one.
11904 	 * If any port is locked by event processing, fail the whole operation.
11905 	 * One port is already locked, but for simplicity lock it again.
11906 	 */
11907 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
11908 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11909 		    cport_mutex);
11910 		if (((SATA_CPORT_INFO(sata_hba_inst, tcport)->
11911 		    cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) {
11912 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11913 			    cport_mutex);
11914 			rv = EBUSY;
11915 			break;
11916 		} else {
11917 			SATA_CPORT_INFO(sata_hba_inst, tcport)->
11918 			    cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
11919 			/*
11920 			 * If there is a port multiplier attached, we may need
11921 			 * to lock its port as well. If so, add such code here.
11922 			 */
11923 		}
11924 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11925 		    cport_mutex);
11926 	}
11927 
11928 	if (rv == 0) {
11929 		/*
11930 		 * All cports were successfully locked.
11931 		 * Reset main SATA controller only for now - no PMult.
11932 		 * Set the device address to port 0, to have a valid device
11933 		 * address.
11934 		 */
11935 		sata_device.satadev_addr.qual = SATA_ADDR_CNTRL;
11936 		sata_device.satadev_addr.cport = 0;
11937 		sata_device.satadev_addr.pmport = 0;
11938 
11939 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
11940 		    (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) {
11941 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11942 			    "sata_hba_ioctl: reset controller failed"));
11943 			return (EIO);
11944 		}
11945 		/*
11946 		 * Because ports were reset, port states are unknown.
11947 		 * They should be re-probed to get their state and
11948 		 * attached devices should be reinitialized.
11949 		 * Add code here to re-probe port multiplier device ports.
11950 		 */
11951 		for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst);
11952 		    tcport++) {
11953 			sata_device.satadev_addr.cport = tcport;
11954 			sata_device.satadev_addr.pmport = tpmport;
11955 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
11956 
11957 			if (sata_reprobe_port(sata_hba_inst, &sata_device,
11958 			    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
11959 				rv = EIO;
11960 		}
11961 	}
11962 	/*
11963 	 * Unlock all ports
11964 	 */
11965 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
11966 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11967 		    cport_mutex);
11968 		SATA_CPORT_INFO(sata_hba_inst, tcport)->
11969 		    cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
11970 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11971 		    cport_mutex);
11972 	}
11973 
11974 	/*
11975 	 * This operation returns EFAULT if either reset
11976 	 * controller failed or a re-probing of any port failed.
11977 	 */
11978 	return (rv);
11979 }
11980 
11981 
11982 /*
11983  * Process ioctl port self test request.
11984  *
11985  * NOTE: Port multiplier code is not completed nor tested.
11986  */
11987 static int
11988 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst,
11989     sata_device_t *sata_device)
11990 {
11991 	int cport, pmport, qual;
11992 	int rv = 0;
11993 
11994 	/* Sanity check */
11995 	if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL)
11996 		return (ENOTSUP);
11997 
11998 	cport = sata_device->satadev_addr.cport;
11999 	pmport = sata_device->satadev_addr.pmport;
12000 	qual = sata_device->satadev_addr.qual;
12001 
12002 	/*
12003 	 * There is no protection here for a configured
12004 	 * device attached to this port.
12005 	 */
12006 
12007 	if ((*SATA_SELFTEST_FUNC(sata_hba_inst))
12008 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
12009 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12010 		    "sata_hba_ioctl: port selftest: "
12011 		    "failed port %d:%d", cport, pmport));
12012 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12013 		    cport_mutex);
12014 		sata_update_port_info(sata_hba_inst, sata_device);
12015 		if (qual == SATA_ADDR_CPORT)
12016 			SATA_CPORT_STATE(sata_hba_inst, cport) =
12017 			    SATA_PSTATE_FAILED;
12018 		else /* port ultiplier device port */
12019 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
12020 			    SATA_PSTATE_FAILED;
12021 
12022 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12023 		    cport_mutex);
12024 		return (EIO);
12025 	}
12026 	/*
12027 	 * Beacuse the port was reset in the course of testing, it should be
12028 	 * re-probed and attached device state should be restored. At this
12029 	 * point the port state is unknown - it's state is HBA-specific.
12030 	 * Force port re-probing to get it into a known state.
12031 	 */
12032 	if (sata_reprobe_port(sata_hba_inst, sata_device,
12033 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
12034 		rv = EIO;
12035 	return (rv);
12036 }
12037 
12038 
12039 /*
12040  * sata_cfgadm_state:
12041  * Use the sata port state and state of the target node to figure out
12042  * the cfgadm_state.
12043  *
12044  * The port argument is a value with encoded cport,
12045  * pmport and address qualifier, in the same manner as a scsi target number.
12046  * SCSI_TO_SATA_CPORT macro extracts cport number,
12047  * SCSI_TO_SATA_PMPORT extracts pmport number and
12048  * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag.
12049  *
12050  * For now, support is for cports only - no port multiplier device ports.
12051  */
12052 
12053 static void
12054 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port,
12055     devctl_ap_state_t *ap_state)
12056 {
12057 	uint16_t	cport;
12058 	int		port_state;
12059 	sata_drive_info_t *sdinfo;
12060 
12061 	/* Cport only */
12062 	cport = SCSI_TO_SATA_CPORT(port);
12063 
12064 	port_state = SATA_CPORT_STATE(sata_hba_inst, cport);
12065 	if (port_state & SATA_PSTATE_SHUTDOWN ||
12066 	    port_state & SATA_PSTATE_FAILED) {
12067 		ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
12068 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12069 		if (port_state & SATA_PSTATE_FAILED)
12070 			ap_state->ap_condition = AP_COND_FAILED;
12071 		else
12072 			ap_state->ap_condition = AP_COND_UNKNOWN;
12073 
12074 		return;
12075 	}
12076 
12077 	/* Need to check pmult device port here as well, when supported */
12078 
12079 	/* Port is enabled and ready */
12080 
12081 	switch (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport)) {
12082 	case SATA_DTYPE_NONE:
12083 	{
12084 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12085 		ap_state->ap_condition = AP_COND_OK;
12086 		/* No device attached */
12087 		ap_state->ap_rstate = AP_RSTATE_EMPTY;
12088 		break;
12089 	}
12090 	case SATA_DTYPE_UNKNOWN:
12091 	case SATA_DTYPE_ATAPINONCD:
12092 	case SATA_DTYPE_PMULT:	/* Until PMult is supported */
12093 	case SATA_DTYPE_ATADISK:
12094 	case SATA_DTYPE_ATAPICD:
12095 	{
12096 		dev_info_t *tdip = NULL;
12097 		dev_info_t *dip = NULL;
12098 		int circ;
12099 
12100 		dip = SATA_DIP(sata_hba_inst);
12101 		tdip = sata_get_target_dip(dip, port);
12102 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
12103 		if (tdip != NULL) {
12104 			ndi_devi_enter(dip, &circ);
12105 			mutex_enter(&(DEVI(tdip)->devi_lock));
12106 			if (DEVI_IS_DEVICE_REMOVED(tdip)) {
12107 				/*
12108 				 * There could be the case where previously
12109 				 * configured and opened device was removed
12110 				 * and unknown device was plugged.
12111 				 * In such case we want to show a device, and
12112 				 * its configured or unconfigured state but
12113 				 * indicate unusable condition untill the
12114 				 * old target node is released and removed.
12115 				 */
12116 				ap_state->ap_condition = AP_COND_UNUSABLE;
12117 			} else {
12118 				mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst,
12119 				    cport));
12120 				sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12121 				    cport);
12122 				if (sdinfo != NULL) {
12123 					if ((sdinfo->satadrv_state &
12124 					    SATA_DSTATE_FAILED) != 0)
12125 						ap_state->ap_condition =
12126 						    AP_COND_FAILED;
12127 					else
12128 						ap_state->ap_condition =
12129 						    AP_COND_OK;
12130 				} else {
12131 					ap_state->ap_condition =
12132 					    AP_COND_UNKNOWN;
12133 				}
12134 				mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst,
12135 				    cport));
12136 			}
12137 			if ((DEVI_IS_DEVICE_OFFLINE(tdip)) ||
12138 			    (DEVI_IS_DEVICE_DOWN(tdip))) {
12139 				ap_state->ap_ostate =
12140 				    AP_OSTATE_UNCONFIGURED;
12141 			} else {
12142 				ap_state->ap_ostate =
12143 				    AP_OSTATE_CONFIGURED;
12144 			}
12145 			mutex_exit(&(DEVI(tdip)->devi_lock));
12146 			ndi_devi_exit(dip, circ);
12147 		} else {
12148 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12149 			ap_state->ap_condition = AP_COND_UNKNOWN;
12150 		}
12151 		break;
12152 	}
12153 	default:
12154 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
12155 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12156 		ap_state->ap_condition = AP_COND_UNKNOWN;
12157 		/*
12158 		 * This is actually internal error condition (non fatal),
12159 		 * because we have already checked all defined device types.
12160 		 */
12161 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12162 		    "sata_cfgadm_state: Internal error: "
12163 		    "unknown device type"));
12164 		break;
12165 	}
12166 }
12167 
12168 
12169 /*
12170  * Process ioctl get device path request.
12171  *
12172  * NOTE: Port multiplier code is not completed nor tested.
12173  */
12174 static int
12175 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst,
12176     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12177 {
12178 	char path[MAXPATHLEN];
12179 	uint32_t size;
12180 	dev_info_t *tdip;
12181 
12182 	(void) strcpy(path, "/devices");
12183 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
12184 	    &sata_device->satadev_addr)) == NULL) {
12185 		/*
12186 		 * No such device. If this is a request for a size, do not
12187 		 * return EINVAL for non-existing target, because cfgadm
12188 		 * will then indicate a meaningless ioctl failure.
12189 		 * If this is a request for a path, indicate invalid
12190 		 * argument.
12191 		 */
12192 		if (ioc->get_size == 0)
12193 			return (EINVAL);
12194 	} else {
12195 		(void) ddi_pathname(tdip, path + strlen(path));
12196 	}
12197 	size = strlen(path) + 1;
12198 
12199 	if (ioc->get_size != 0) {
12200 		if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz,
12201 		    mode) != 0)
12202 			return (EFAULT);
12203 	} else {
12204 		if (ioc->bufsiz != size)
12205 			return (EINVAL);
12206 
12207 		else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz,
12208 		    mode) != 0)
12209 			return (EFAULT);
12210 	}
12211 	return (0);
12212 }
12213 
12214 /*
12215  * Process ioctl get attachment point type request.
12216  *
12217  * NOTE: Port multiplier code is not completed nor tested.
12218  */
12219 static	int
12220 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst,
12221     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12222 {
12223 	uint32_t	type_len;
12224 	const char	*ap_type;
12225 	int		dev_type;
12226 
12227 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12228 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst,
12229 		    sata_device->satadev_addr.cport);
12230 	else /* pmport */
12231 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12232 		    sata_device->satadev_addr.cport,
12233 		    sata_device->satadev_addr.pmport);
12234 
12235 	switch (dev_type) {
12236 	case SATA_DTYPE_NONE:
12237 		ap_type = "port";
12238 		break;
12239 
12240 	case SATA_DTYPE_ATADISK:
12241 		ap_type = "disk";
12242 		break;
12243 
12244 	case SATA_DTYPE_ATAPICD:
12245 		ap_type = "cd/dvd";
12246 		break;
12247 
12248 	case SATA_DTYPE_PMULT:
12249 		ap_type = "pmult";
12250 		break;
12251 
12252 	case SATA_DTYPE_UNKNOWN:
12253 		ap_type = "unknown";
12254 		break;
12255 
12256 	default:
12257 		ap_type = "unsupported";
12258 		break;
12259 
12260 	} /* end of dev_type switch */
12261 
12262 	type_len = strlen(ap_type) + 1;
12263 
12264 	if (ioc->get_size) {
12265 		if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz,
12266 		    mode) != 0)
12267 			return (EFAULT);
12268 	} else {
12269 		if (ioc->bufsiz != type_len)
12270 			return (EINVAL);
12271 
12272 		if (ddi_copyout((void *)ap_type, ioc->buf,
12273 		    ioc->bufsiz, mode) != 0)
12274 			return (EFAULT);
12275 	}
12276 	return (0);
12277 
12278 }
12279 
12280 /*
12281  * Process ioctl get device model info request.
12282  * This operation should return to cfgadm the device model
12283  * information string
12284  *
12285  * NOTE: Port multiplier code is not completed nor tested.
12286  */
12287 static	int
12288 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst,
12289     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12290 {
12291 	sata_drive_info_t *sdinfo;
12292 	uint32_t info_len;
12293 	char ap_info[SATA_ID_MODEL_LEN + 1];
12294 
12295 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12296 	    sata_device->satadev_addr.cport)->cport_mutex);
12297 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12298 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12299 		    sata_device->satadev_addr.cport);
12300 	else /* port multiplier */
12301 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12302 		    sata_device->satadev_addr.cport,
12303 		    sata_device->satadev_addr.pmport);
12304 	if (sdinfo == NULL) {
12305 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12306 		    sata_device->satadev_addr.cport)->cport_mutex);
12307 		return (EINVAL);
12308 	}
12309 
12310 #ifdef	_LITTLE_ENDIAN
12311 	swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
12312 #else	/* _LITTLE_ENDIAN */
12313 	bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
12314 #endif	/* _LITTLE_ENDIAN */
12315 
12316 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12317 	    sata_device->satadev_addr.cport)->cport_mutex);
12318 
12319 	ap_info[SATA_ID_MODEL_LEN] = '\0';
12320 
12321 	info_len = strlen(ap_info) + 1;
12322 
12323 	if (ioc->get_size) {
12324 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12325 		    mode) != 0)
12326 			return (EFAULT);
12327 	} else {
12328 		if (ioc->bufsiz < info_len)
12329 			return (EINVAL);
12330 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12331 		    mode) != 0)
12332 			return (EFAULT);
12333 	}
12334 	return (0);
12335 }
12336 
12337 
12338 /*
12339  * Process ioctl get device firmware revision info request.
12340  * This operation should return to cfgadm the device firmware revision
12341  * information string
12342  *
12343  * NOTE: Port multiplier code is not completed nor tested.
12344  */
12345 static	int
12346 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst,
12347     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12348 {
12349 	sata_drive_info_t *sdinfo;
12350 	uint32_t info_len;
12351 	char ap_info[SATA_ID_FW_LEN + 1];
12352 
12353 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12354 	    sata_device->satadev_addr.cport)->cport_mutex);
12355 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12356 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12357 		    sata_device->satadev_addr.cport);
12358 	else /* port multiplier */
12359 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12360 		    sata_device->satadev_addr.cport,
12361 		    sata_device->satadev_addr.pmport);
12362 	if (sdinfo == NULL) {
12363 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12364 		    sata_device->satadev_addr.cport)->cport_mutex);
12365 		return (EINVAL);
12366 	}
12367 
12368 #ifdef	_LITTLE_ENDIAN
12369 	swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
12370 #else	/* _LITTLE_ENDIAN */
12371 	bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
12372 #endif	/* _LITTLE_ENDIAN */
12373 
12374 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12375 	    sata_device->satadev_addr.cport)->cport_mutex);
12376 
12377 	ap_info[SATA_ID_FW_LEN] = '\0';
12378 
12379 	info_len = strlen(ap_info) + 1;
12380 
12381 	if (ioc->get_size) {
12382 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12383 		    mode) != 0)
12384 			return (EFAULT);
12385 	} else {
12386 		if (ioc->bufsiz < info_len)
12387 			return (EINVAL);
12388 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12389 		    mode) != 0)
12390 			return (EFAULT);
12391 	}
12392 	return (0);
12393 }
12394 
12395 
12396 /*
12397  * Process ioctl get device serial number info request.
12398  * This operation should return to cfgadm the device serial number string.
12399  *
12400  * NOTE: Port multiplier code is not completed nor tested.
12401  */
12402 static	int
12403 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst,
12404     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12405 {
12406 	sata_drive_info_t *sdinfo;
12407 	uint32_t info_len;
12408 	char ap_info[SATA_ID_SERIAL_LEN + 1];
12409 
12410 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12411 	    sata_device->satadev_addr.cport)->cport_mutex);
12412 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12413 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12414 		    sata_device->satadev_addr.cport);
12415 	else /* port multiplier */
12416 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12417 		    sata_device->satadev_addr.cport,
12418 		    sata_device->satadev_addr.pmport);
12419 	if (sdinfo == NULL) {
12420 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12421 		    sata_device->satadev_addr.cport)->cport_mutex);
12422 		return (EINVAL);
12423 	}
12424 
12425 #ifdef	_LITTLE_ENDIAN
12426 	swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
12427 #else	/* _LITTLE_ENDIAN */
12428 	bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
12429 #endif	/* _LITTLE_ENDIAN */
12430 
12431 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12432 	    sata_device->satadev_addr.cport)->cport_mutex);
12433 
12434 	ap_info[SATA_ID_SERIAL_LEN] = '\0';
12435 
12436 	info_len = strlen(ap_info) + 1;
12437 
12438 	if (ioc->get_size) {
12439 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12440 		    mode) != 0)
12441 			return (EFAULT);
12442 	} else {
12443 		if (ioc->bufsiz < info_len)
12444 			return (EINVAL);
12445 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12446 		    mode) != 0)
12447 			return (EFAULT);
12448 	}
12449 	return (0);
12450 }
12451 
12452 
12453 /*
12454  * Preset scsi extended sense data (to NO SENSE)
12455  * First 18 bytes of the sense data are preset to current valid sense
12456  * with a key NO SENSE data.
12457  *
12458  * Returns void
12459  */
12460 static void
12461 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense)
12462 {
12463 	sense->es_valid = 1;		/* Valid sense */
12464 	sense->es_class = CLASS_EXTENDED_SENSE;	/* 0x70 - current err */
12465 	sense->es_key = KEY_NO_SENSE;
12466 	sense->es_info_1 = 0;
12467 	sense->es_info_2 = 0;
12468 	sense->es_info_3 = 0;
12469 	sense->es_info_4 = 0;
12470 	sense->es_add_len = 10;	/* Additional length - replace with a def */
12471 	sense->es_cmd_info[0] = 0;
12472 	sense->es_cmd_info[1] = 0;
12473 	sense->es_cmd_info[2] = 0;
12474 	sense->es_cmd_info[3] = 0;
12475 	sense->es_add_code = 0;
12476 	sense->es_qual_code = 0;
12477 }
12478 
12479 /*
12480  * Register a legacy cmdk-style devid for the target (disk) device.
12481  *
12482  * Note: This function is called only when the HBA devinfo node has the
12483  * property "use-cmdk-devid-format" set. This property indicates that
12484  * devid compatible with old cmdk (target) driver is to be generated
12485  * for any target device attached to this controller. This will take
12486  * precedence over the devid generated by sd (target) driver.
12487  * This function is derived from cmdk_devid_setup() function in cmdk.c.
12488  */
12489 static void
12490 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo)
12491 {
12492 	char	*hwid;
12493 	int	modlen;
12494 	int	serlen;
12495 	int	rval;
12496 	ddi_devid_t	devid;
12497 
12498 	/*
12499 	 * device ID is a concatanation of model number, "=", serial number.
12500 	 */
12501 	hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP);
12502 	bcopy(&sdinfo->satadrv_id.ai_model, hwid,
12503 	    sizeof (sdinfo->satadrv_id.ai_model));
12504 	swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model));
12505 	modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model));
12506 	if (modlen == 0)
12507 		goto err;
12508 	hwid[modlen++] = '=';
12509 	bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen],
12510 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12511 	swab(&hwid[modlen], &hwid[modlen],
12512 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12513 	serlen = sata_check_modser(&hwid[modlen],
12514 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12515 	if (serlen == 0)
12516 		goto err;
12517 	hwid[modlen + serlen] = 0; /* terminate the hwid string */
12518 
12519 	/* initialize/register devid */
12520 	if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL,
12521 	    (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS)
12522 		rval = ddi_devid_register(dip, devid);
12523 
12524 	if (rval != DDI_SUCCESS)
12525 		cmn_err(CE_WARN, "sata: failed to create devid for the disk"
12526 		    " on port %d", sdinfo->satadrv_addr.cport);
12527 err:
12528 	kmem_free(hwid, LEGACY_HWID_LEN);
12529 }
12530 
12531 /*
12532  * valid model/serial string must contain a non-zero non-space characters.
12533  * trim trailing spaces/NULLs.
12534  */
12535 static int
12536 sata_check_modser(char *buf, int buf_len)
12537 {
12538 	boolean_t ret;
12539 	char *s;
12540 	int i;
12541 	int tb;
12542 	char ch;
12543 
12544 	ret = B_FALSE;
12545 	s = buf;
12546 	for (i = 0; i < buf_len; i++) {
12547 		ch = *s++;
12548 		if (ch != ' ' && ch != '\0')
12549 			tb = i + 1;
12550 		if (ch != ' ' && ch != '\0' && ch != '0')
12551 			ret = B_TRUE;
12552 	}
12553 
12554 	if (ret == B_FALSE)
12555 		return (0); /* invalid string */
12556 
12557 	return (tb); /* return length */
12558 }
12559 
12560 /*
12561  * sata_set_drive_features function compares current device features setting
12562  * with the saved device features settings and, if there is a difference,
12563  * it restores device features setting to the previously saved state.
12564  * It also arbitrarily tries to select the highest supported DMA mode.
12565  * Device Identify or Identify Packet Device data has to be current.
12566  * At the moment read ahead and write cache are considered for all devices.
12567  * For atapi devices, Removable Media Status Notification is set in addition
12568  * to common features.
12569  *
12570  * This function cannot be called in the interrupt context (it may sleep).
12571  *
12572  * The input argument sdinfo should point to the drive info structure
12573  * to be updated after features are set. Note, that only
12574  * device (packet) identify data is updated, not the flags indicating the
12575  * supported features.
12576  *
12577  * Returns TRUE if successful or there was nothing to do. Device Identify data
12578  * in the drive info structure pointed to by the sdinfo argumens is updated
12579  * even when no features were set or changed.
12580  *
12581  * Returns FALSE if device features could not be set.
12582  *
12583  * Note: This function may fail the port, making it inaccessible.
12584  * In such case the explicit port disconnect/connect or physical device
12585  * detach/attach is required to re-evaluate port state again.
12586  */
12587 
12588 static int
12589 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst,
12590     sata_drive_info_t *sdinfo, int restore)
12591 {
12592 	int rval = SATA_SUCCESS;
12593 	sata_drive_info_t new_sdinfo;
12594 	char *finfo = "sata_set_drive_features: cannot";
12595 	char *finfox;
12596 	int cache_op;
12597 
12598 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
12599 	new_sdinfo.satadrv_addr = sdinfo->satadrv_addr;
12600 	new_sdinfo.satadrv_type = sdinfo->satadrv_type;
12601 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
12602 		/*
12603 		 * Cannot get device identification - retry later
12604 		 */
12605 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12606 		    "%s fetch device identify data\n", finfo);
12607 		return (SATA_FAILURE);
12608 	}
12609 	finfox = (restore != 0) ? " restore device features" :
12610 	    " initialize device features\n";
12611 
12612 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12613 		/* Arbitrarily set UDMA mode */
12614 		if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
12615 		    SATA_SUCCESS) {
12616 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12617 			    "%s set UDMA mode\n", finfo));
12618 			return (SATA_FAILURE);
12619 		}
12620 	} else { /* Assume SATA ATAPI CD/DVD */
12621 		/*  Set Removable Media Status Notification, if necessary */
12622 		if ((new_sdinfo.satadrv_id.ai_cmdset83 &
12623 		    SATA_RM_STATUS_NOTIFIC) != 0 && restore != 0) {
12624 			if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) &&
12625 			    (!(new_sdinfo.satadrv_id.ai_features86 &
12626 			    SATA_RM_STATUS_NOTIFIC))) ||
12627 			    ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) &&
12628 			    (new_sdinfo.satadrv_id.ai_features86 &
12629 			    SATA_RM_STATUS_NOTIFIC))) {
12630 				/* Current setting does not match saved one */
12631 				if (sata_set_rmsn(sata_hba_inst, sdinfo,
12632 				    sdinfo->satadrv_settings &
12633 				    SATA_DEV_RMSN) != SATA_SUCCESS)
12634 					rval = SATA_FAILURE;
12635 			}
12636 		}
12637 		/*
12638 		 * We have to set Multiword DMA or UDMA, if it is supported, as
12639 		 * we want to use DMA transfer mode whenever possible.
12640 		 * Some devices require explicit setting of the DMA mode.
12641 		 */
12642 		if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) {
12643 			/* Set highest supported DMA mode */
12644 			if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
12645 			    SATA_SUCCESS) {
12646 				SATA_LOG_D((sata_hba_inst, CE_WARN,
12647 				    "%s set UDMA mode\n", finfo));
12648 				rval = SATA_FAILURE;
12649 			}
12650 		}
12651 	}
12652 
12653 	if (!(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) &&
12654 	    !(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) {
12655 		/* None of the features is supported - do nothing */
12656 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12657 		    "settable features not supported\n", NULL);
12658 		goto update_sdinfo;
12659 	}
12660 
12661 	if (((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) &&
12662 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) &&
12663 	    ((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) &&
12664 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
12665 		/* Nothing to do */
12666 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12667 		    "no device features to set\n", NULL);
12668 		goto update_sdinfo;
12669 	}
12670 
12671 	if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) &&
12672 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD))) {
12673 		if (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) {
12674 			/* Enable read ahead / read cache */
12675 			cache_op = SATAC_SF_ENABLE_READ_AHEAD;
12676 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12677 			    "enabling read cache\n", NULL);
12678 		} else {
12679 			/* Disable read ahead  / read cache */
12680 			cache_op = SATAC_SF_DISABLE_READ_AHEAD;
12681 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12682 			    "disabling read cache\n", NULL);
12683 		}
12684 
12685 		/* Try to set read cache mode */
12686 		if (sata_set_cache_mode(sata_hba_inst, &new_sdinfo,
12687 		    cache_op) != SATA_SUCCESS) {
12688 			/* Pkt execution failed */
12689 			rval = SATA_FAILURE;
12690 		}
12691 	}
12692 
12693 	if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) &&
12694 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
12695 		if (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) {
12696 			/* Enable write cache */
12697 			cache_op = SATAC_SF_ENABLE_WRITE_CACHE;
12698 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12699 			    "enabling write cache\n", NULL);
12700 		} else {
12701 			/* Disable write cache */
12702 			cache_op = SATAC_SF_DISABLE_WRITE_CACHE;
12703 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12704 			    "disabling write cache\n", NULL);
12705 		}
12706 		/* Try to set write cache mode */
12707 		if (sata_set_cache_mode(sata_hba_inst, &new_sdinfo,
12708 		    cache_op) != SATA_SUCCESS) {
12709 			/* Pkt execution failed */
12710 			rval = SATA_FAILURE;
12711 		}
12712 	}
12713 
12714 	if (rval == SATA_FAILURE)
12715 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12716 		    "%s %s", finfo, finfox));
12717 update_sdinfo:
12718 	/*
12719 	 * We need to fetch Device Identify data again
12720 	 */
12721 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
12722 		/*
12723 		 * Cannot get device identification - retry later
12724 		 */
12725 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12726 		    "%s re-fetch device identify data\n", finfo));
12727 		rval = SATA_FAILURE;
12728 	}
12729 	/* Copy device sata info. */
12730 	sdinfo->satadrv_id = new_sdinfo.satadrv_id;
12731 
12732 	return (rval);
12733 }
12734 
12735 
12736 /*
12737  *
12738  * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if
12739  * unable to determine.
12740  *
12741  * Cannot be called in an interrupt context.
12742  *
12743  * Called by sata_build_lsense_page_2f()
12744  */
12745 
12746 static int
12747 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst,
12748     sata_drive_info_t *sdinfo)
12749 {
12750 	sata_pkt_t *spkt;
12751 	sata_cmd_t *scmd;
12752 	sata_pkt_txlate_t *spx;
12753 	int rval;
12754 
12755 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12756 	spx->txlt_sata_hba_inst = sata_hba_inst;
12757 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12758 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12759 	if (spkt == NULL) {
12760 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12761 		return (-1);
12762 	}
12763 	/* address is needed now */
12764 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12765 
12766 
12767 	/* Fill sata_pkt */
12768 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12769 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12770 	/* Synchronous mode, no callback */
12771 	spkt->satapkt_comp = NULL;
12772 	/* Timeout 30s */
12773 	spkt->satapkt_time = sata_default_pkt_time;
12774 
12775 	scmd = &spkt->satapkt_cmd;
12776 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
12777 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
12778 
12779 	/* Set up which registers need to be returned */
12780 	scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE;
12781 	scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE;
12782 
12783 	/* Build SMART_RETURN_STATUS cmd in the sata_pkt */
12784 	scmd->satacmd_addr_type = 0;		/* N/A */
12785 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
12786 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
12787 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
12788 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
12789 	scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS;
12790 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12791 	scmd->satacmd_cmd_reg = SATAC_SMART;
12792 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12793 	    sdinfo->satadrv_addr.cport)));
12794 
12795 
12796 	/* Send pkt to SATA HBA driver */
12797 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12798 	    SATA_TRAN_ACCEPTED ||
12799 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12800 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12801 		    sdinfo->satadrv_addr.cport)));
12802 		/*
12803 		 * Whoops, no SMART RETURN STATUS
12804 		 */
12805 		rval = -1;
12806 	} else {
12807 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12808 		    sdinfo->satadrv_addr.cport)));
12809 		if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
12810 			rval = -1;
12811 			goto fail;
12812 		}
12813 		if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
12814 			rval = -1;
12815 			goto fail;
12816 		}
12817 		if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) &&
12818 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2))
12819 			rval = 0;
12820 		else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) &&
12821 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4))
12822 			rval = 1;
12823 		else {
12824 			rval = -1;
12825 			goto fail;
12826 		}
12827 	}
12828 fail:
12829 	/* Free allocated resources */
12830 	sata_pkt_free(spx);
12831 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12832 
12833 	return (rval);
12834 }
12835 
12836 /*
12837  *
12838  * Returns 0 if succeeded, -1 otherwise
12839  *
12840  * Cannot be called in an interrupt context.
12841  *
12842  */
12843 static int
12844 sata_fetch_smart_data(
12845 	sata_hba_inst_t *sata_hba_inst,
12846 	sata_drive_info_t *sdinfo,
12847 	struct smart_data *smart_data)
12848 {
12849 	sata_pkt_t *spkt;
12850 	sata_cmd_t *scmd;
12851 	sata_pkt_txlate_t *spx;
12852 	int rval;
12853 
12854 #if ! defined(lint)
12855 	ASSERT(sizeof (struct smart_data) == 512);
12856 #endif
12857 
12858 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12859 	spx->txlt_sata_hba_inst = sata_hba_inst;
12860 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12861 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12862 	if (spkt == NULL) {
12863 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12864 		return (-1);
12865 	}
12866 	/* address is needed now */
12867 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12868 
12869 
12870 	/* Fill sata_pkt */
12871 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12872 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12873 	/* Synchronous mode, no callback */
12874 	spkt->satapkt_comp = NULL;
12875 	/* Timeout 30s */
12876 	spkt->satapkt_time = sata_default_pkt_time;
12877 
12878 	scmd = &spkt->satapkt_cmd;
12879 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
12880 
12881 	/*
12882 	 * Allocate buffer for SMART data
12883 	 */
12884 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
12885 	    sizeof (struct smart_data));
12886 	if (scmd->satacmd_bp == NULL) {
12887 		sata_pkt_free(spx);
12888 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12889 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12890 		    "sata_fetch_smart_data: "
12891 		    "cannot allocate buffer"));
12892 		return (-1);
12893 	}
12894 
12895 
12896 	/* Build SMART_READ_DATA cmd in the sata_pkt */
12897 	scmd->satacmd_addr_type = 0;		/* N/A */
12898 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
12899 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
12900 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
12901 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
12902 	scmd->satacmd_features_reg = SATA_SMART_READ_DATA;
12903 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12904 	scmd->satacmd_cmd_reg = SATAC_SMART;
12905 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12906 	    sdinfo->satadrv_addr.cport)));
12907 
12908 	/* Send pkt to SATA HBA driver */
12909 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12910 	    SATA_TRAN_ACCEPTED ||
12911 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12912 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12913 		    sdinfo->satadrv_addr.cport)));
12914 		/*
12915 		 * Whoops, no SMART DATA available
12916 		 */
12917 		rval = -1;
12918 		goto fail;
12919 	} else {
12920 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12921 		    sdinfo->satadrv_addr.cport)));
12922 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
12923 		    DDI_DMA_SYNC_FORKERNEL);
12924 		ASSERT(rval == DDI_SUCCESS);
12925 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data,
12926 		    sizeof (struct smart_data));
12927 	}
12928 
12929 fail:
12930 	/* Free allocated resources */
12931 	sata_free_local_buffer(spx);
12932 	sata_pkt_free(spx);
12933 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12934 
12935 	return (rval);
12936 }
12937 
12938 /*
12939  * Used by LOG SENSE page 0x10
12940  *
12941  * return 0 for success, -1 otherwise
12942  *
12943  */
12944 static int
12945 sata_ext_smart_selftest_read_log(
12946 	sata_hba_inst_t *sata_hba_inst,
12947 	sata_drive_info_t *sdinfo,
12948 	struct smart_ext_selftest_log *ext_selftest_log,
12949 	uint16_t block_num)
12950 {
12951 	sata_pkt_txlate_t *spx;
12952 	sata_pkt_t *spkt;
12953 	sata_cmd_t *scmd;
12954 	int rval;
12955 
12956 #if ! defined(lint)
12957 	ASSERT(sizeof (struct smart_ext_selftest_log) == 512);
12958 #endif
12959 
12960 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12961 	spx->txlt_sata_hba_inst = sata_hba_inst;
12962 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12963 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12964 	if (spkt == NULL) {
12965 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12966 		return (-1);
12967 	}
12968 	/* address is needed now */
12969 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12970 
12971 
12972 	/* Fill sata_pkt */
12973 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12974 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12975 	/* Synchronous mode, no callback */
12976 	spkt->satapkt_comp = NULL;
12977 	/* Timeout 30s */
12978 	spkt->satapkt_time = sata_default_pkt_time;
12979 
12980 	scmd = &spkt->satapkt_cmd;
12981 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
12982 
12983 	/*
12984 	 * Allocate buffer for SMART extended self-test log
12985 	 */
12986 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
12987 	    sizeof (struct smart_ext_selftest_log));
12988 	if (scmd->satacmd_bp == NULL) {
12989 		sata_pkt_free(spx);
12990 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12991 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12992 		    "sata_ext_smart_selftest_log: "
12993 		    "cannot allocate buffer"));
12994 		return (-1);
12995 	}
12996 
12997 	/* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */
12998 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
12999 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of selftest log */
13000 	scmd->satacmd_sec_count_msb = 0;	/* One sector of selftest log */
13001 	scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE;
13002 	scmd->satacmd_lba_low_msb = 0;
13003 	scmd->satacmd_lba_mid_lsb = block_num & 0xff;
13004 	scmd->satacmd_lba_mid_msb = block_num >> 8;
13005 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13006 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
13007 
13008 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13009 	    sdinfo->satadrv_addr.cport)));
13010 
13011 	/* Send pkt to SATA HBA driver */
13012 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13013 	    SATA_TRAN_ACCEPTED ||
13014 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13015 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13016 		    sdinfo->satadrv_addr.cport)));
13017 
13018 		/*
13019 		 * Whoops, no SMART selftest log info available
13020 		 */
13021 		rval = -1;
13022 		goto fail;
13023 	} else {
13024 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13025 		    sdinfo->satadrv_addr.cport)));
13026 
13027 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13028 		    DDI_DMA_SYNC_FORKERNEL);
13029 		ASSERT(rval == DDI_SUCCESS);
13030 		bcopy(scmd->satacmd_bp->b_un.b_addr,
13031 		    (uint8_t *)ext_selftest_log,
13032 		    sizeof (struct smart_ext_selftest_log));
13033 		rval = 0;
13034 	}
13035 
13036 fail:
13037 	/* Free allocated resources */
13038 	sata_free_local_buffer(spx);
13039 	sata_pkt_free(spx);
13040 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13041 
13042 	return (rval);
13043 }
13044 
13045 /*
13046  * Returns 0 for success, -1 otherwise
13047  *
13048  * SMART self-test log data is returned in buffer pointed to by selftest_log
13049  */
13050 static int
13051 sata_smart_selftest_log(
13052 	sata_hba_inst_t *sata_hba_inst,
13053 	sata_drive_info_t *sdinfo,
13054 	struct smart_selftest_log *selftest_log)
13055 {
13056 	sata_pkt_t *spkt;
13057 	sata_cmd_t *scmd;
13058 	sata_pkt_txlate_t *spx;
13059 	int rval;
13060 
13061 #if ! defined(lint)
13062 	ASSERT(sizeof (struct smart_selftest_log) == 512);
13063 #endif
13064 
13065 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13066 	spx->txlt_sata_hba_inst = sata_hba_inst;
13067 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13068 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13069 	if (spkt == NULL) {
13070 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13071 		return (-1);
13072 	}
13073 	/* address is needed now */
13074 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13075 
13076 
13077 	/* Fill sata_pkt */
13078 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13079 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13080 	/* Synchronous mode, no callback */
13081 	spkt->satapkt_comp = NULL;
13082 	/* Timeout 30s */
13083 	spkt->satapkt_time = sata_default_pkt_time;
13084 
13085 	scmd = &spkt->satapkt_cmd;
13086 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13087 
13088 	/*
13089 	 * Allocate buffer for SMART SELFTEST LOG
13090 	 */
13091 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13092 	    sizeof (struct smart_selftest_log));
13093 	if (scmd->satacmd_bp == NULL) {
13094 		sata_pkt_free(spx);
13095 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13096 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13097 		    "sata_smart_selftest_log: "
13098 		    "cannot allocate buffer"));
13099 		return (-1);
13100 	}
13101 
13102 	/* Build SMART_READ_LOG cmd in the sata_pkt */
13103 	scmd->satacmd_addr_type = 0;		/* N/A */
13104 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of SMART log */
13105 	scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE;
13106 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13107 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13108 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
13109 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13110 	scmd->satacmd_cmd_reg = SATAC_SMART;
13111 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13112 	    sdinfo->satadrv_addr.cport)));
13113 
13114 	/* Send pkt to SATA HBA driver */
13115 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13116 	    SATA_TRAN_ACCEPTED ||
13117 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13118 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13119 		    sdinfo->satadrv_addr.cport)));
13120 		/*
13121 		 * Whoops, no SMART DATA available
13122 		 */
13123 		rval = -1;
13124 		goto fail;
13125 	} else {
13126 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13127 		    sdinfo->satadrv_addr.cport)));
13128 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13129 		    DDI_DMA_SYNC_FORKERNEL);
13130 		ASSERT(rval == DDI_SUCCESS);
13131 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log,
13132 		    sizeof (struct smart_selftest_log));
13133 		rval = 0;
13134 	}
13135 
13136 fail:
13137 	/* Free allocated resources */
13138 	sata_free_local_buffer(spx);
13139 	sata_pkt_free(spx);
13140 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13141 
13142 	return (rval);
13143 }
13144 
13145 
13146 /*
13147  * Returns 0 for success, -1 otherwise
13148  *
13149  * SMART READ LOG data is returned in buffer pointed to by smart_log
13150  */
13151 static int
13152 sata_smart_read_log(
13153 	sata_hba_inst_t *sata_hba_inst,
13154 	sata_drive_info_t *sdinfo,
13155 	uint8_t *smart_log,		/* where the data should be returned */
13156 	uint8_t which_log,		/* which log should be returned */
13157 	uint8_t log_size)		/* # of 512 bytes in log */
13158 {
13159 	sata_pkt_t *spkt;
13160 	sata_cmd_t *scmd;
13161 	sata_pkt_txlate_t *spx;
13162 	int rval;
13163 
13164 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13165 	spx->txlt_sata_hba_inst = sata_hba_inst;
13166 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13167 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13168 	if (spkt == NULL) {
13169 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13170 		return (-1);
13171 	}
13172 	/* address is needed now */
13173 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13174 
13175 
13176 	/* Fill sata_pkt */
13177 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13178 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13179 	/* Synchronous mode, no callback */
13180 	spkt->satapkt_comp = NULL;
13181 	/* Timeout 30s */
13182 	spkt->satapkt_time = sata_default_pkt_time;
13183 
13184 	scmd = &spkt->satapkt_cmd;
13185 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13186 
13187 	/*
13188 	 * Allocate buffer for SMART READ LOG
13189 	 */
13190 	scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512);
13191 	if (scmd->satacmd_bp == NULL) {
13192 		sata_pkt_free(spx);
13193 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13194 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13195 		    "sata_smart_read_log: " "cannot allocate buffer"));
13196 		return (-1);
13197 	}
13198 
13199 	/* Build SMART_READ_LOG cmd in the sata_pkt */
13200 	scmd->satacmd_addr_type = 0;		/* N/A */
13201 	scmd->satacmd_sec_count_lsb = log_size;	/* what the caller asked for */
13202 	scmd->satacmd_lba_low_lsb = which_log;	/* which log page */
13203 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13204 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13205 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
13206 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13207 	scmd->satacmd_cmd_reg = SATAC_SMART;
13208 
13209 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13210 	    sdinfo->satadrv_addr.cport)));
13211 
13212 	/* Send pkt to SATA HBA driver */
13213 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13214 	    SATA_TRAN_ACCEPTED ||
13215 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13216 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13217 		    sdinfo->satadrv_addr.cport)));
13218 
13219 		/*
13220 		 * Whoops, no SMART DATA available
13221 		 */
13222 		rval = -1;
13223 		goto fail;
13224 	} else {
13225 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13226 		    sdinfo->satadrv_addr.cport)));
13227 
13228 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13229 		    DDI_DMA_SYNC_FORKERNEL);
13230 		ASSERT(rval == DDI_SUCCESS);
13231 		bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512);
13232 		rval = 0;
13233 	}
13234 
13235 fail:
13236 	/* Free allocated resources */
13237 	sata_free_local_buffer(spx);
13238 	sata_pkt_free(spx);
13239 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13240 
13241 	return (rval);
13242 }
13243 
13244 /*
13245  * Used by LOG SENSE page 0x10
13246  *
13247  * return 0 for success, -1 otherwise
13248  *
13249  */
13250 static int
13251 sata_read_log_ext_directory(
13252 	sata_hba_inst_t *sata_hba_inst,
13253 	sata_drive_info_t *sdinfo,
13254 	struct read_log_ext_directory *logdir)
13255 {
13256 	sata_pkt_txlate_t *spx;
13257 	sata_pkt_t *spkt;
13258 	sata_cmd_t *scmd;
13259 	int rval;
13260 
13261 #if ! defined(lint)
13262 	ASSERT(sizeof (struct read_log_ext_directory) == 512);
13263 #endif
13264 
13265 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13266 	spx->txlt_sata_hba_inst = sata_hba_inst;
13267 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13268 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13269 	if (spkt == NULL) {
13270 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13271 		return (-1);
13272 	}
13273 
13274 	/* Fill sata_pkt */
13275 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13276 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13277 	/* Synchronous mode, no callback */
13278 	spkt->satapkt_comp = NULL;
13279 	/* Timeout 30s */
13280 	spkt->satapkt_time = sata_default_pkt_time;
13281 
13282 	scmd = &spkt->satapkt_cmd;
13283 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13284 
13285 	/*
13286 	 * Allocate buffer for SMART READ LOG EXTENDED command
13287 	 */
13288 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13289 	    sizeof (struct read_log_ext_directory));
13290 	if (scmd->satacmd_bp == NULL) {
13291 		sata_pkt_free(spx);
13292 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13293 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13294 		    "sata_read_log_ext_directory: "
13295 		    "cannot allocate buffer"));
13296 		return (-1);
13297 	}
13298 
13299 	/* Build READ LOG EXT w/ log directory cmd in the  sata_pkt */
13300 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
13301 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of directory */
13302 	scmd->satacmd_sec_count_msb = 0;	/* One sector of directory */
13303 	scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY;
13304 	scmd->satacmd_lba_low_msb = 0;
13305 	scmd->satacmd_lba_mid_lsb = 0;
13306 	scmd->satacmd_lba_mid_msb = 0;
13307 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13308 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
13309 
13310 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13311 	    sdinfo->satadrv_addr.cport)));
13312 
13313 	/* Send pkt to SATA HBA driver */
13314 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13315 	    SATA_TRAN_ACCEPTED ||
13316 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13317 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13318 		    sdinfo->satadrv_addr.cport)));
13319 		/*
13320 		 * Whoops, no SMART selftest log info available
13321 		 */
13322 		rval = -1;
13323 		goto fail;
13324 	} else {
13325 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13326 		    sdinfo->satadrv_addr.cport)));
13327 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13328 		    DDI_DMA_SYNC_FORKERNEL);
13329 		ASSERT(rval == DDI_SUCCESS);
13330 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir,
13331 		    sizeof (struct read_log_ext_directory));
13332 		rval = 0;
13333 	}
13334 
13335 fail:
13336 	/* Free allocated resources */
13337 	sata_free_local_buffer(spx);
13338 	sata_pkt_free(spx);
13339 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13340 
13341 	return (rval);
13342 }
13343 
13344 /*
13345  * Set up error retrieval sata command for NCQ command error data
13346  * recovery.
13347  *
13348  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
13349  * returns SATA_FAILURE otherwise.
13350  */
13351 static int
13352 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
13353 {
13354 #ifndef __lock_lint
13355 	_NOTE(ARGUNUSED(sdinfo))
13356 #endif
13357 
13358 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
13359 	sata_cmd_t *scmd;
13360 	struct buf *bp;
13361 
13362 	/* Operation modes are up to the caller */
13363 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13364 
13365 	/* Synchronous mode, no callback - may be changed by the caller */
13366 	spkt->satapkt_comp = NULL;
13367 	spkt->satapkt_time = sata_default_pkt_time;
13368 
13369 	scmd = &spkt->satapkt_cmd;
13370 	bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t));
13371 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
13372 
13373 	/*
13374 	 * Allocate dma_able buffer error data.
13375 	 * Buffer allocation will take care of buffer alignment and other DMA
13376 	 * attributes.
13377 	 */
13378 	bp = sata_alloc_local_buffer(spx,
13379 	    sizeof (struct sata_ncq_error_recovery_page));
13380 	if (bp == NULL)
13381 		return (SATA_FAILURE);
13382 
13383 	bp_mapin(bp); /* make data buffer accessible */
13384 	scmd->satacmd_bp = bp;
13385 
13386 	/*
13387 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
13388 	 * before accessing it. Handle is in usual place in translate struct.
13389 	 */
13390 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
13391 
13392 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
13393 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
13394 
13395 	return (SATA_SUCCESS);
13396 }
13397 
13398 /*
13399  * sata_xlate_errors() is used to translate (S)ATA error
13400  * information to SCSI information returned in the SCSI
13401  * packet.
13402  */
13403 static void
13404 sata_xlate_errors(sata_pkt_txlate_t *spx)
13405 {
13406 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
13407 	struct scsi_extended_sense *sense;
13408 
13409 	scsipkt->pkt_reason = CMD_INCOMPLETE;
13410 	*scsipkt->pkt_scbp = STATUS_CHECK;
13411 	sense = sata_arq_sense(spx);
13412 
13413 	switch (spx->txlt_sata_pkt->satapkt_reason) {
13414 	case SATA_PKT_PORT_ERROR:
13415 		/*
13416 		 * We have no device data. Assume no data transfered.
13417 		 */
13418 		sense->es_key = KEY_HARDWARE_ERROR;
13419 		break;
13420 
13421 	case SATA_PKT_DEV_ERROR:
13422 		if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
13423 		    SATA_STATUS_ERR) {
13424 			/*
13425 			 * determine dev error reason from error
13426 			 * reg content
13427 			 */
13428 			sata_decode_device_error(spx, sense);
13429 			break;
13430 		}
13431 		/* No extended sense key - no info available */
13432 		break;
13433 
13434 	case SATA_PKT_TIMEOUT:
13435 		scsipkt->pkt_reason = CMD_TIMEOUT;
13436 		scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET;
13437 		/* No extended sense key */
13438 		break;
13439 
13440 	case SATA_PKT_ABORTED:
13441 		scsipkt->pkt_reason = CMD_ABORTED;
13442 		scsipkt->pkt_statistics |= STAT_ABORTED;
13443 		/* No extended sense key */
13444 		break;
13445 
13446 	case SATA_PKT_RESET:
13447 		/*
13448 		 * pkt aborted either by an explicit reset request from
13449 		 * a host, or due to error recovery
13450 		 */
13451 		scsipkt->pkt_reason = CMD_RESET;
13452 		scsipkt->pkt_statistics |= STAT_DEV_RESET;
13453 		break;
13454 
13455 	default:
13456 		scsipkt->pkt_reason = CMD_TRAN_ERR;
13457 		break;
13458 	}
13459 }
13460 
13461 
13462 
13463 
13464 /*
13465  * Log sata message
13466  * dev pathname msg line preceeds the logged message.
13467  */
13468 
13469 static	void
13470 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...)
13471 {
13472 	char pathname[128];
13473 	dev_info_t *dip;
13474 	va_list ap;
13475 
13476 	mutex_enter(&sata_log_mutex);
13477 
13478 	va_start(ap, fmt);
13479 	(void) vsprintf(sata_log_buf, fmt, ap);
13480 	va_end(ap);
13481 
13482 	if (sata_hba_inst != NULL) {
13483 		dip = SATA_DIP(sata_hba_inst);
13484 		(void) ddi_pathname(dip, pathname);
13485 	} else {
13486 		pathname[0] = 0;
13487 	}
13488 	if (level == CE_CONT) {
13489 		if (sata_debug_flags == 0)
13490 			cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf);
13491 		else
13492 			cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf);
13493 	} else {
13494 		if (level != CE_NOTE) {
13495 			cmn_err(level, "%s:\n %s", pathname, sata_log_buf);
13496 		} else if (sata_msg) {
13497 			cmn_err(level, "%s:\n %s", pathname,
13498 			    sata_log_buf);
13499 		}
13500 	}
13501 
13502 	mutex_exit(&sata_log_mutex);
13503 }
13504 
13505 
13506 /* ******** Asynchronous HBA events handling & hotplugging support ******** */
13507 
13508 /*
13509  * Start or terminate the thread, depending on flag arg and current state
13510  */
13511 static void
13512 sata_event_thread_control(int startstop)
13513 {
13514 	static 	int sata_event_thread_terminating = 0;
13515 	static 	int sata_event_thread_starting = 0;
13516 	int i;
13517 
13518 	mutex_enter(&sata_event_mutex);
13519 
13520 	if (startstop == 0 && (sata_event_thread_starting == 1 ||
13521 	    sata_event_thread_terminating == 1)) {
13522 		mutex_exit(&sata_event_mutex);
13523 		return;
13524 	}
13525 	if (startstop == 1 && sata_event_thread_starting == 1) {
13526 		mutex_exit(&sata_event_mutex);
13527 		return;
13528 	}
13529 	if (startstop == 1 && sata_event_thread_terminating == 1) {
13530 		sata_event_thread_starting = 1;
13531 		/* wait til terminate operation completes */
13532 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
13533 		while (sata_event_thread_terminating == 1) {
13534 			if (i-- <= 0) {
13535 				sata_event_thread_starting = 0;
13536 				mutex_exit(&sata_event_mutex);
13537 #ifdef SATA_DEBUG
13538 				cmn_err(CE_WARN, "sata_event_thread_control: "
13539 				    "timeout waiting for thread to terminate");
13540 #endif
13541 				return;
13542 			}
13543 			mutex_exit(&sata_event_mutex);
13544 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
13545 			mutex_enter(&sata_event_mutex);
13546 		}
13547 	}
13548 	if (startstop == 1) {
13549 		if (sata_event_thread == NULL) {
13550 			sata_event_thread = thread_create(NULL, 0,
13551 			    (void (*)())sata_event_daemon,
13552 			    &sata_hba_list, 0, &p0, TS_RUN, minclsyspri);
13553 		}
13554 		sata_event_thread_starting = 0;
13555 		mutex_exit(&sata_event_mutex);
13556 		return;
13557 	}
13558 
13559 	/*
13560 	 * If we got here, thread may need to be terminated
13561 	 */
13562 	if (sata_event_thread != NULL) {
13563 		int i;
13564 		/* Signal event thread to go away */
13565 		sata_event_thread_terminating = 1;
13566 		sata_event_thread_terminate = 1;
13567 		cv_signal(&sata_event_cv);
13568 		/*
13569 		 * Wait til daemon terminates.
13570 		 */
13571 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
13572 		while (sata_event_thread_terminate == 1) {
13573 			mutex_exit(&sata_event_mutex);
13574 			if (i-- <= 0) {
13575 				/* Daemon did not go away !!! */
13576 #ifdef SATA_DEBUG
13577 				cmn_err(CE_WARN, "sata_event_thread_control: "
13578 				    "cannot terminate event daemon thread");
13579 #endif
13580 				mutex_enter(&sata_event_mutex);
13581 				break;
13582 			}
13583 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
13584 			mutex_enter(&sata_event_mutex);
13585 		}
13586 		sata_event_thread_terminating = 0;
13587 	}
13588 	ASSERT(sata_event_thread_terminating == 0);
13589 	ASSERT(sata_event_thread_starting == 0);
13590 	mutex_exit(&sata_event_mutex);
13591 }
13592 
13593 
13594 /*
13595  * SATA HBA event notification function.
13596  * Events reported by SATA HBA drivers per HBA instance relate to a change in
13597  * a port and/or device state or a controller itself.
13598  * Events for different addresses/addr types cannot be combined.
13599  * A warning message is generated for each event type.
13600  * Events are not processed by this function, so only the
13601  * event flag(s)is set for an affected entity and the event thread is
13602  * waken up. Event daemon thread processes all events.
13603  *
13604  * NOTE: Since more than one event may be reported at the same time, one
13605  * cannot determine a sequence of events when opposite event are reported, eg.
13606  * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing
13607  * is taking precedence over reported events, i.e. may cause ignoring some
13608  * events.
13609  */
13610 #define	SATA_EVENT_MAX_MSG_LENGTH	79
13611 
13612 void
13613 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event)
13614 {
13615 	sata_hba_inst_t *sata_hba_inst = NULL;
13616 	sata_address_t *saddr;
13617 	sata_drive_info_t *sdinfo;
13618 	sata_port_stats_t *pstats;
13619 	int cport, pmport;
13620 	char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1];
13621 	char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1];
13622 	char *lcp;
13623 	static char *err_msg_evnt_1 =
13624 	    "sata_hba_event_notify: invalid port event 0x%x ";
13625 	static char *err_msg_evnt_2 =
13626 	    "sata_hba_event_notify: invalid device event 0x%x ";
13627 	int linkevent;
13628 
13629 	/*
13630 	 * There is a possibility that an event will be generated on HBA
13631 	 * that has not completed attachment or is detaching.
13632 	 * HBA driver should prevent this, but just in case it does not,
13633 	 * we need to ignore events for such HBA.
13634 	 */
13635 	mutex_enter(&sata_mutex);
13636 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13637 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13638 		if (SATA_DIP(sata_hba_inst) == dip)
13639 			if (sata_hba_inst->satahba_attached == 1)
13640 				break;
13641 	}
13642 	mutex_exit(&sata_mutex);
13643 	if (sata_hba_inst == NULL)
13644 		/* HBA not attached */
13645 		return;
13646 
13647 	ASSERT(sata_device != NULL);
13648 
13649 	/*
13650 	 * Validate address before - do not proceed with invalid address.
13651 	 */
13652 	saddr = &sata_device->satadev_addr;
13653 	if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst))
13654 		return;
13655 	if (saddr->qual == SATA_ADDR_PMPORT ||
13656 	    saddr->qual == SATA_ADDR_DPMPORT)
13657 		/* Port Multiplier not supported yet */
13658 		return;
13659 
13660 	cport = saddr->cport;
13661 	pmport = saddr->pmport;
13662 
13663 	buf1[0] = buf2[0] = '\0';
13664 
13665 	/*
13666 	 * Events refer to devices, ports and controllers - each has
13667 	 * unique address. Events for different addresses cannot be combined.
13668 	 */
13669 	if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) {
13670 
13671 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13672 
13673 		/* qualify this event(s) */
13674 		if ((event & SATA_EVNT_PORT_EVENTS) == 0) {
13675 			/* Invalid event for the device port */
13676 			(void) sprintf(buf2, err_msg_evnt_1,
13677 			    event & SATA_EVNT_PORT_EVENTS);
13678 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13679 			goto event_info;
13680 		}
13681 		if (saddr->qual == SATA_ADDR_CPORT) {
13682 			/* Controller's device port event */
13683 
13684 			(SATA_CPORT_INFO(sata_hba_inst, cport))->
13685 			    cport_event_flags |=
13686 			    event & SATA_EVNT_PORT_EVENTS;
13687 			pstats =
13688 			    &(SATA_CPORT_INFO(sata_hba_inst, cport))->
13689 			    cport_stats;
13690 		} else {
13691 			/* Port multiplier's device port event */
13692 			(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
13693 			    pmport_event_flags |=
13694 			    event & SATA_EVNT_PORT_EVENTS;
13695 			pstats =
13696 			    &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
13697 			    pmport_stats;
13698 		}
13699 
13700 		/*
13701 		 * Add to statistics and log the message. We have to do it
13702 		 * here rather than in the event daemon, because there may be
13703 		 * multiple events occuring before they are processed.
13704 		 */
13705 		linkevent = event &
13706 		    (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED);
13707 		if (linkevent) {
13708 			if (linkevent == (SATA_EVNT_LINK_LOST |
13709 			    SATA_EVNT_LINK_ESTABLISHED)) {
13710 				/* This is likely event combination */
13711 				(void) strlcat(buf1, "link lost/established, ",
13712 				    SATA_EVENT_MAX_MSG_LENGTH);
13713 
13714 				if (pstats->link_lost < 0xffffffffffffffffULL)
13715 					pstats->link_lost++;
13716 				if (pstats->link_established <
13717 				    0xffffffffffffffffULL)
13718 					pstats->link_established++;
13719 				linkevent = 0;
13720 			} else if (linkevent & SATA_EVNT_LINK_LOST) {
13721 				(void) strlcat(buf1, "link lost, ",
13722 				    SATA_EVENT_MAX_MSG_LENGTH);
13723 
13724 				if (pstats->link_lost < 0xffffffffffffffffULL)
13725 					pstats->link_lost++;
13726 			} else {
13727 				(void) strlcat(buf1, "link established, ",
13728 				    SATA_EVENT_MAX_MSG_LENGTH);
13729 				if (pstats->link_established <
13730 				    0xffffffffffffffffULL)
13731 					pstats->link_established++;
13732 			}
13733 		}
13734 		if (event & SATA_EVNT_DEVICE_ATTACHED) {
13735 			(void) strlcat(buf1, "device attached, ",
13736 			    SATA_EVENT_MAX_MSG_LENGTH);
13737 			if (pstats->device_attached < 0xffffffffffffffffULL)
13738 				pstats->device_attached++;
13739 		}
13740 		if (event & SATA_EVNT_DEVICE_DETACHED) {
13741 			(void) strlcat(buf1, "device detached, ",
13742 			    SATA_EVENT_MAX_MSG_LENGTH);
13743 			if (pstats->device_detached < 0xffffffffffffffffULL)
13744 				pstats->device_detached++;
13745 		}
13746 		if (event & SATA_EVNT_PWR_LEVEL_CHANGED) {
13747 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
13748 			    "port %d power level changed", cport);
13749 			if (pstats->port_pwr_changed < 0xffffffffffffffffULL)
13750 				pstats->port_pwr_changed++;
13751 		}
13752 
13753 		if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) {
13754 			/* There should be no other events for this address */
13755 			(void) sprintf(buf2, err_msg_evnt_1,
13756 			    event & ~SATA_EVNT_PORT_EVENTS);
13757 		}
13758 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13759 
13760 	} else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) {
13761 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13762 
13763 		/* qualify this event */
13764 		if ((event & SATA_EVNT_DEVICE_RESET) == 0) {
13765 			/* Invalid event for a device */
13766 			(void) sprintf(buf2, err_msg_evnt_2,
13767 			    event & SATA_EVNT_DEVICE_RESET);
13768 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13769 			goto event_info;
13770 		}
13771 		/* drive event */
13772 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
13773 		if (sdinfo != NULL) {
13774 			if (event & SATA_EVNT_DEVICE_RESET) {
13775 				(void) strlcat(buf1, "device reset, ",
13776 				    SATA_EVENT_MAX_MSG_LENGTH);
13777 				if (sdinfo->satadrv_stats.drive_reset <
13778 				    0xffffffffffffffffULL)
13779 					sdinfo->satadrv_stats.drive_reset++;
13780 				sdinfo->satadrv_event_flags |=
13781 				    SATA_EVNT_DEVICE_RESET;
13782 			}
13783 		}
13784 		if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) {
13785 			/* Invalid event for a device */
13786 			(void) sprintf(buf2, err_msg_evnt_2,
13787 			    event & ~SATA_EVNT_DRIVE_EVENTS);
13788 		}
13789 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13790 	} else {
13791 		if (saddr->qual != SATA_ADDR_NULL) {
13792 			/* Wrong address qualifier */
13793 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13794 			    "sata_hba_event_notify: invalid address 0x%x",
13795 			    *(uint32_t *)saddr));
13796 			return;
13797 		}
13798 		if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 ||
13799 		    (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) {
13800 			/* Invalid event for the controller */
13801 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13802 			    "sata_hba_event_notify: invalid event 0x%x for "
13803 			    "controller",
13804 			    event & SATA_EVNT_CONTROLLER_EVENTS));
13805 			return;
13806 		}
13807 		buf1[0] = '\0';
13808 		/* This may be a frequent and not interesting event */
13809 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
13810 		    "controller power level changed\n", NULL);
13811 
13812 		mutex_enter(&sata_hba_inst->satahba_mutex);
13813 		if (sata_hba_inst->satahba_stats.ctrl_pwr_change <
13814 		    0xffffffffffffffffULL)
13815 			sata_hba_inst->satahba_stats.ctrl_pwr_change++;
13816 
13817 		sata_hba_inst->satahba_event_flags |=
13818 		    SATA_EVNT_PWR_LEVEL_CHANGED;
13819 		mutex_exit(&sata_hba_inst->satahba_mutex);
13820 	}
13821 	/*
13822 	 * If we got here, there is something to do with this HBA
13823 	 * instance.
13824 	 */
13825 	mutex_enter(&sata_hba_inst->satahba_mutex);
13826 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
13827 	mutex_exit(&sata_hba_inst->satahba_mutex);
13828 	mutex_enter(&sata_mutex);
13829 	sata_event_pending |= SATA_EVNT_MAIN;	/* global event indicator */
13830 	mutex_exit(&sata_mutex);
13831 
13832 	/* Tickle event thread */
13833 	mutex_enter(&sata_event_mutex);
13834 	if (sata_event_thread_active == 0)
13835 		cv_signal(&sata_event_cv);
13836 	mutex_exit(&sata_event_mutex);
13837 
13838 event_info:
13839 	if (buf1[0] != '\0') {
13840 		lcp = strrchr(buf1, ',');
13841 		if (lcp != NULL)
13842 			*lcp = '\0';
13843 	}
13844 	if (saddr->qual == SATA_ADDR_CPORT ||
13845 	    saddr->qual == SATA_ADDR_DCPORT) {
13846 		if (buf1[0] != '\0') {
13847 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
13848 			    cport, buf1);
13849 		}
13850 		if (buf2[0] != '\0') {
13851 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
13852 			    cport, buf2);
13853 		}
13854 	} else if (saddr->qual == SATA_ADDR_PMPORT ||
13855 	    saddr->qual == SATA_ADDR_DPMPORT) {
13856 		if (buf1[0] != '\0') {
13857 			sata_log(sata_hba_inst, CE_NOTE,
13858 			    "port %d pmport %d: %s\n", cport, pmport, buf1);
13859 		}
13860 		if (buf2[0] != '\0') {
13861 			sata_log(sata_hba_inst, CE_NOTE,
13862 			    "port %d pmport %d: %s\n", cport, pmport, buf2);
13863 		}
13864 	}
13865 }
13866 
13867 
13868 /*
13869  * Event processing thread.
13870  * Arg is a pointer to the sata_hba_list pointer.
13871  * It is not really needed, because sata_hba_list is global and static
13872  */
13873 static void
13874 sata_event_daemon(void *arg)
13875 {
13876 #ifndef __lock_lint
13877 	_NOTE(ARGUNUSED(arg))
13878 #endif
13879 	sata_hba_inst_t *sata_hba_inst;
13880 	clock_t lbolt;
13881 
13882 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13883 	    "SATA event daemon started\n", NULL);
13884 loop:
13885 	/*
13886 	 * Process events here. Walk through all registered HBAs
13887 	 */
13888 	mutex_enter(&sata_mutex);
13889 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13890 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13891 		ASSERT(sata_hba_inst != NULL);
13892 		mutex_enter(&sata_hba_inst->satahba_mutex);
13893 		if (sata_hba_inst->satahba_attached != 1 ||
13894 		    (sata_hba_inst->satahba_event_flags &
13895 		    SATA_EVNT_SKIP) != 0) {
13896 			mutex_exit(&sata_hba_inst->satahba_mutex);
13897 			continue;
13898 		}
13899 		if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) {
13900 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP;
13901 			mutex_exit(&sata_hba_inst->satahba_mutex);
13902 			mutex_exit(&sata_mutex);
13903 			/* Got the controller with pending event */
13904 			sata_process_controller_events(sata_hba_inst);
13905 			/*
13906 			 * Since global mutex was released, there is a
13907 			 * possibility that HBA list has changed, so start
13908 			 * over from the top. Just processed controller
13909 			 * will be passed-over because of the SKIP flag.
13910 			 */
13911 			goto loop;
13912 		}
13913 		mutex_exit(&sata_hba_inst->satahba_mutex);
13914 	}
13915 	/* Clear SKIP flag in all controllers */
13916 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13917 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13918 		mutex_enter(&sata_hba_inst->satahba_mutex);
13919 		sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP;
13920 		mutex_exit(&sata_hba_inst->satahba_mutex);
13921 	}
13922 	mutex_exit(&sata_mutex);
13923 
13924 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13925 	    "SATA EVENT DAEMON suspending itself", NULL);
13926 
13927 #ifdef SATA_DEBUG
13928 	if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) {
13929 		sata_log(sata_hba_inst, CE_WARN,
13930 		    "SATA EVENTS PROCESSING DISABLED\n");
13931 		thread_exit(); /* Daemon will not run again */
13932 	}
13933 #endif
13934 	mutex_enter(&sata_event_mutex);
13935 	sata_event_thread_active = 0;
13936 	mutex_exit(&sata_event_mutex);
13937 	/*
13938 	 * Go to sleep/suspend itself and wake up either because new event or
13939 	 * wait timeout. Exit if there is a termination request (driver
13940 	 * unload).
13941 	 */
13942 	do {
13943 		lbolt = ddi_get_lbolt();
13944 		lbolt += drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME);
13945 		mutex_enter(&sata_event_mutex);
13946 		(void) cv_timedwait(&sata_event_cv, &sata_event_mutex, lbolt);
13947 
13948 		if (sata_event_thread_active != 0) {
13949 			mutex_exit(&sata_event_mutex);
13950 			continue;
13951 		}
13952 
13953 		/* Check if it is time to go away */
13954 		if (sata_event_thread_terminate == 1) {
13955 			/*
13956 			 * It is up to the thread setting above flag to make
13957 			 * sure that this thread is not killed prematurely.
13958 			 */
13959 			sata_event_thread_terminate = 0;
13960 			sata_event_thread = NULL;
13961 			mutex_exit(&sata_event_mutex);
13962 			SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13963 			    "SATA_EVENT_DAEMON_TERMINATING", NULL);
13964 			thread_exit();  { _NOTE(NOT_REACHED) }
13965 		}
13966 		mutex_exit(&sata_event_mutex);
13967 	} while (!(sata_event_pending & SATA_EVNT_MAIN));
13968 
13969 	mutex_enter(&sata_event_mutex);
13970 	sata_event_thread_active = 1;
13971 	mutex_exit(&sata_event_mutex);
13972 
13973 	mutex_enter(&sata_mutex);
13974 	sata_event_pending &= ~SATA_EVNT_MAIN;
13975 	mutex_exit(&sata_mutex);
13976 
13977 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13978 	    "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL);
13979 
13980 	goto loop;
13981 }
13982 
13983 /*
13984  * Specific HBA instance event processing.
13985  *
13986  * NOTE: At the moment, device event processing is limited to hard disks
13987  * only.
13988  * cports only are supported - no pmports.
13989  */
13990 static void
13991 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst)
13992 {
13993 	int ncport;
13994 	uint32_t event_flags;
13995 	sata_address_t *saddr;
13996 
13997 	SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst,
13998 	    "Processing controller %d event(s)",
13999 	    ddi_get_instance(SATA_DIP(sata_hba_inst)));
14000 
14001 	mutex_enter(&sata_hba_inst->satahba_mutex);
14002 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN;
14003 	event_flags = sata_hba_inst->satahba_event_flags;
14004 	mutex_exit(&sata_hba_inst->satahba_mutex);
14005 	/*
14006 	 * Process controller power change first
14007 	 * HERE
14008 	 */
14009 	if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED)
14010 		sata_process_cntrl_pwr_level_change(sata_hba_inst);
14011 
14012 	/*
14013 	 * Search through ports/devices to identify affected port/device.
14014 	 * We may have to process events for more than one port/device.
14015 	 */
14016 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
14017 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14018 		event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))->
14019 		    cport_event_flags;
14020 		/* Check if port was locked by IOCTL processing */
14021 		if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) {
14022 			/*
14023 			 * We ignore port events because port is busy
14024 			 * with AP control processing. Set again
14025 			 * controller and main event flag, so that
14026 			 * events may be processed by the next daemon
14027 			 * run.
14028 			 */
14029 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14030 			mutex_enter(&sata_hba_inst->satahba_mutex);
14031 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14032 			mutex_exit(&sata_hba_inst->satahba_mutex);
14033 			mutex_enter(&sata_mutex);
14034 			sata_event_pending |= SATA_EVNT_MAIN;
14035 			mutex_exit(&sata_mutex);
14036 			SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst,
14037 			    "Event processing postponed until "
14038 			    "AP control processing completes",
14039 			    NULL);
14040 			/* Check other ports */
14041 			continue;
14042 		} else {
14043 			/*
14044 			 * Set BSY flag so that AP control would not
14045 			 * interfere with events processing for
14046 			 * this port.
14047 			 */
14048 			(SATA_CPORT_INFO(sata_hba_inst, ncport))->
14049 			    cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY;
14050 		}
14051 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14052 
14053 		saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr;
14054 
14055 		if ((event_flags &
14056 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
14057 			/*
14058 			 * Got port event.
14059 			 * We need some hierarchy of event processing as they
14060 			 * are affecting each other:
14061 			 * 1. port failed
14062 			 * 2. device detached/attached
14063 			 * 3. link events - link events may trigger device
14064 			 *    detached or device attached events in some
14065 			 *    circumstances.
14066 			 * 4. port power level changed
14067 			 */
14068 			if (event_flags & SATA_EVNT_PORT_FAILED) {
14069 				sata_process_port_failed_event(sata_hba_inst,
14070 				    saddr);
14071 			}
14072 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
14073 				sata_process_device_detached(sata_hba_inst,
14074 				    saddr);
14075 			}
14076 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
14077 				sata_process_device_attached(sata_hba_inst,
14078 				    saddr);
14079 			}
14080 			if (event_flags &
14081 			    (SATA_EVNT_LINK_ESTABLISHED |
14082 			    SATA_EVNT_LINK_LOST)) {
14083 				sata_process_port_link_events(sata_hba_inst,
14084 				    saddr);
14085 			}
14086 			if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) {
14087 				sata_process_port_pwr_change(sata_hba_inst,
14088 				    saddr);
14089 			}
14090 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
14091 				sata_process_target_node_cleanup(
14092 				    sata_hba_inst, saddr);
14093 			}
14094 			if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) {
14095 				sata_process_device_autoonline(
14096 				    sata_hba_inst, saddr);
14097 			}
14098 		}
14099 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14100 		if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) !=
14101 		    SATA_DTYPE_NONE) &&
14102 		    (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) {
14103 			if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)->
14104 			    satadrv_event_flags &
14105 			    (SATA_EVNT_DEVICE_RESET |
14106 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
14107 				/* Have device event */
14108 				sata_process_device_reset(sata_hba_inst,
14109 				    saddr);
14110 			}
14111 		}
14112 		/* Release PORT_BUSY flag */
14113 		(SATA_CPORT_INFO(sata_hba_inst, ncport))->
14114 		    cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
14115 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14116 
14117 	} /* End of loop through the controller SATA ports */
14118 }
14119 
14120 /*
14121  * Process HBA power level change reported by HBA driver.
14122  * Not implemented at this time - event is ignored.
14123  */
14124 static void
14125 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst)
14126 {
14127 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14128 	    "Processing controller power level change", NULL);
14129 
14130 	/* Ignoring it for now */
14131 	mutex_enter(&sata_hba_inst->satahba_mutex);
14132 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
14133 	mutex_exit(&sata_hba_inst->satahba_mutex);
14134 }
14135 
14136 /*
14137  * Process port power level change reported by HBA driver.
14138  * Not implemented at this time - event is ignored.
14139  */
14140 static void
14141 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst,
14142     sata_address_t *saddr)
14143 {
14144 	sata_cport_info_t *cportinfo;
14145 
14146 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14147 	    "Processing port power level change", NULL);
14148 
14149 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14150 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14151 	/* Reset event flag */
14152 	cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
14153 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14154 }
14155 
14156 /*
14157  * Process port failure reported by HBA driver.
14158  * cports support only - no pmports.
14159  */
14160 static void
14161 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst,
14162     sata_address_t *saddr)
14163 {
14164 	sata_cport_info_t *cportinfo;
14165 
14166 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14167 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14168 	/* Reset event flag first */
14169 	cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED;
14170 	/* If the port is in SHUTDOWN or FAILED state, ignore this event. */
14171 	if ((cportinfo->cport_state &
14172 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) {
14173 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14174 		    cport_mutex);
14175 		return;
14176 	}
14177 	/* Fail the port */
14178 	cportinfo->cport_state = SATA_PSTATE_FAILED;
14179 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14180 	sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport);
14181 }
14182 
14183 /*
14184  * Device Reset Event processing.
14185  * The seqeunce is managed by 3 stage flags:
14186  * - reset event reported,
14187  * - reset event being processed,
14188  * - request to clear device reset state.
14189  *
14190  * NOTE: This function has to be entered with cport mutex held. It exits with
14191  * mutex held as well, but can release mutex during the processing.
14192  */
14193 static void
14194 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst,
14195     sata_address_t *saddr)
14196 {
14197 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
14198 	sata_drive_info_t *sdinfo;
14199 	sata_cport_info_t *cportinfo;
14200 	sata_device_t sata_device;
14201 	int rval;
14202 
14203 	/* We only care about host sata cport for now */
14204 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14205 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14206 	/*
14207 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
14208 	 * state, ignore reset event.
14209 	 */
14210 	if (((cportinfo->cport_state &
14211 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
14212 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
14213 		sdinfo->satadrv_event_flags &=
14214 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
14215 		return;
14216 	}
14217 
14218 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) &
14219 	    SATA_VALID_DEV_TYPE) == 0) {
14220 		/*
14221 		 * This should not happen - coding error.
14222 		 * But we can recover, so do not panic, just clean up
14223 		 * and if in debug mode, log the message.
14224 		 */
14225 #ifdef SATA_DEBUG
14226 		sata_log(sata_hba_inst, CE_WARN,
14227 		    "sata_process_device_reset: "
14228 		    "Invalid device type with sdinfo!", NULL);
14229 #endif
14230 		sdinfo->satadrv_event_flags = 0;
14231 		return;
14232 	}
14233 
14234 #ifdef SATA_DEBUG
14235 	if ((sdinfo->satadrv_event_flags &
14236 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
14237 		/* Nothing to do */
14238 		/* Something is weird - why we are processing dev reset? */
14239 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14240 		    "No device reset event!!!!", NULL);
14241 
14242 		return;
14243 	}
14244 	if ((sdinfo->satadrv_event_flags &
14245 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
14246 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
14247 		/* Something is weird - new device reset event */
14248 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14249 		    "Overlapping device reset events!", NULL);
14250 	}
14251 #endif
14252 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14253 	    "Processing port %d device reset", saddr->cport);
14254 
14255 	/* Clear event flag */
14256 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
14257 
14258 	/* It seems that we always need to check the port state first */
14259 	sata_device.satadev_rev = SATA_DEVICE_REV;
14260 	sata_device.satadev_addr = *saddr;
14261 	/*
14262 	 * We have to exit mutex, because the HBA probe port function may
14263 	 * block on its own mutex.
14264 	 */
14265 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14266 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14267 	    (SATA_DIP(sata_hba_inst), &sata_device);
14268 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14269 	sata_update_port_info(sata_hba_inst, &sata_device);
14270 	if (rval != SATA_SUCCESS) {
14271 		/* Something went wrong? Fail the port */
14272 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14273 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14274 		if (sdinfo != NULL)
14275 			sdinfo->satadrv_event_flags = 0;
14276 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14277 		    cport_mutex);
14278 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14279 		    "SATA port %d probing failed",
14280 		    saddr->cport));
14281 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14282 		    saddr->cport)->cport_mutex);
14283 		return;
14284 	}
14285 	if ((sata_device.satadev_scr.sstatus  &
14286 	    SATA_PORT_DEVLINK_UP_MASK) !=
14287 	    SATA_PORT_DEVLINK_UP ||
14288 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
14289 		/*
14290 		 * No device to process, anymore. Some other event processing
14291 		 * would or have already performed port info cleanup.
14292 		 * To be safe (HBA may need it), request clearing device
14293 		 * reset condition.
14294 		 */
14295 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14296 		if (sdinfo != NULL) {
14297 			sdinfo->satadrv_event_flags &=
14298 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
14299 			sdinfo->satadrv_event_flags |=
14300 			    SATA_EVNT_CLEAR_DEVICE_RESET;
14301 		}
14302 		return;
14303 	}
14304 
14305 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14306 	if (sdinfo == NULL) {
14307 		return;
14308 	}
14309 	if ((sdinfo->satadrv_event_flags &
14310 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
14311 		/*
14312 		 * Start tracking time for device feature restoration and
14313 		 * identification. Save current time (lbolt value).
14314 		 */
14315 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
14316 	}
14317 	/* Mark device reset processing as active */
14318 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
14319 
14320 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
14321 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14322 
14323 	if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) ==
14324 	    SATA_FAILURE) {
14325 		/*
14326 		 * Restoring drive setting failed.
14327 		 * Probe the port first, to check if the port state has changed
14328 		 */
14329 		sata_device.satadev_rev = SATA_DEVICE_REV;
14330 		sata_device.satadev_addr = *saddr;
14331 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
14332 		/* probe port */
14333 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14334 		    (SATA_DIP(sata_hba_inst), &sata_device);
14335 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14336 		    cport_mutex);
14337 		if (rval == SATA_SUCCESS &&
14338 		    (sata_device.satadev_state &
14339 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
14340 		    (sata_device.satadev_scr.sstatus  &
14341 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
14342 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
14343 			/*
14344 			 * We may retry this a bit later - in-process reset
14345 			 * condition should be already set.
14346 			 * Track retry time for device identification.
14347 			 */
14348 			if ((cportinfo->cport_dev_type &
14349 			    SATA_VALID_DEV_TYPE) != 0 &&
14350 			    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL &&
14351 			    sdinfo->satadrv_reset_time != 0) {
14352 				clock_t cur_time = ddi_get_lbolt();
14353 				/*
14354 				 * If the retry time limit was not
14355 				 * exceeded, retry.
14356 				 */
14357 				if ((cur_time - sdinfo->satadrv_reset_time) <
14358 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
14359 					mutex_enter(
14360 					    &sata_hba_inst->satahba_mutex);
14361 					sata_hba_inst->satahba_event_flags |=
14362 					    SATA_EVNT_MAIN;
14363 					mutex_exit(
14364 					    &sata_hba_inst->satahba_mutex);
14365 					mutex_enter(&sata_mutex);
14366 					sata_event_pending |= SATA_EVNT_MAIN;
14367 					mutex_exit(&sata_mutex);
14368 					return;
14369 				}
14370 			}
14371 			/* Fail the drive */
14372 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
14373 
14374 			sata_log(sata_hba_inst, CE_WARN,
14375 			    "SATA device at port %d - device failed",
14376 			    saddr->cport);
14377 		} else {
14378 			/*
14379 			 * No point of retrying - some other event processing
14380 			 * would or already did port info cleanup.
14381 			 * To be safe (HBA may need it),
14382 			 * request clearing device reset condition.
14383 			 */
14384 			sdinfo->satadrv_event_flags |=
14385 			    SATA_EVNT_CLEAR_DEVICE_RESET;
14386 		}
14387 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
14388 		sdinfo->satadrv_reset_time = 0;
14389 		return;
14390 	}
14391 	/*
14392 	 * Raise the flag indicating that the next sata command could
14393 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
14394 	 * reset is reported.
14395 	 */
14396 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14397 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14398 		sdinfo->satadrv_reset_time = 0;
14399 		if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) {
14400 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14401 			sdinfo->satadrv_event_flags &=
14402 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
14403 			sdinfo->satadrv_event_flags |=
14404 			    SATA_EVNT_CLEAR_DEVICE_RESET;
14405 		}
14406 	}
14407 }
14408 
14409 
14410 /*
14411  * Port Link Events processing.
14412  * Every link established event may involve device reset (due to
14413  * COMRESET signal, equivalent of the hard reset) so arbitrarily
14414  * set device reset event for an attached device (if any).
14415  * If the port is in SHUTDOWN or FAILED state, ignore link events.
14416  *
14417  * The link established event processing varies, depending on the state
14418  * of the target node, HBA hotplugging capabilities, state of the port.
14419  * If the link is not active, the link established event is ignored.
14420  * If HBA cannot detect device attachment and there is no target node,
14421  * the link established event triggers device attach event processing.
14422  * Else, link established event triggers device reset event processing.
14423  *
14424  * The link lost event processing varies, depending on a HBA hotplugging
14425  * capability and the state of the port (link active or not active).
14426  * If the link is active, the lost link event is ignored.
14427  * If HBA cannot detect device removal, the lost link event triggers
14428  * device detached event processing after link lost timeout.
14429  * Else, the event is ignored.
14430  *
14431  * NOTE: Only cports are processed for now, i.e. no port multiplier ports
14432  */
14433 static void
14434 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst,
14435     sata_address_t *saddr)
14436 {
14437 	sata_device_t sata_device;
14438 	sata_cport_info_t *cportinfo;
14439 	sata_drive_info_t *sdinfo;
14440 	uint32_t event_flags;
14441 	int rval;
14442 
14443 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14444 	    "Processing port %d link event(s)", saddr->cport);
14445 
14446 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14447 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14448 	event_flags = cportinfo->cport_event_flags;
14449 
14450 	/* Reset event flags first */
14451 	cportinfo->cport_event_flags &=
14452 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
14453 
14454 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
14455 	if ((cportinfo->cport_state &
14456 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14457 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14458 		    cport_mutex);
14459 		return;
14460 	}
14461 
14462 	/*
14463 	 * For the sanity sake get current port state.
14464 	 * Set device address only. Other sata_device fields should be
14465 	 * set by HBA driver.
14466 	 */
14467 	sata_device.satadev_rev = SATA_DEVICE_REV;
14468 	sata_device.satadev_addr = *saddr;
14469 	/*
14470 	 * We have to exit mutex, because the HBA probe port function may
14471 	 * block on its own mutex.
14472 	 */
14473 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14474 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14475 	    (SATA_DIP(sata_hba_inst), &sata_device);
14476 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14477 	sata_update_port_info(sata_hba_inst, &sata_device);
14478 	if (rval != SATA_SUCCESS) {
14479 		/* Something went wrong? Fail the port */
14480 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14481 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14482 		    cport_mutex);
14483 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14484 		    "SATA port %d probing failed",
14485 		    saddr->cport));
14486 		/*
14487 		 * We may want to release device info structure, but
14488 		 * it is not necessary.
14489 		 */
14490 		return;
14491 	} else {
14492 		/* port probed successfully */
14493 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14494 	}
14495 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
14496 
14497 		if ((sata_device.satadev_scr.sstatus &
14498 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
14499 			/* Ignore event */
14500 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14501 			    "Ignoring port %d link established event - "
14502 			    "link down",
14503 			    saddr->cport);
14504 			goto linklost;
14505 		}
14506 
14507 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14508 		    "Processing port %d link established event",
14509 		    saddr->cport);
14510 
14511 		/*
14512 		 * For the sanity sake check if a device is attached - check
14513 		 * return state of a port probing.
14514 		 */
14515 		if (sata_device.satadev_type != SATA_DTYPE_NONE &&
14516 		    sata_device.satadev_type != SATA_DTYPE_PMULT) {
14517 			/*
14518 			 * HBA port probe indicated that there is a device
14519 			 * attached. Check if the framework had device info
14520 			 * structure attached for this device.
14521 			 */
14522 			if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
14523 				ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) !=
14524 				    NULL);
14525 
14526 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14527 				if ((sdinfo->satadrv_type &
14528 				    SATA_VALID_DEV_TYPE) != 0) {
14529 					/*
14530 					 * Dev info structure is present.
14531 					 * If dev_type is set to known type in
14532 					 * the framework's drive info struct
14533 					 * then the device existed before and
14534 					 * the link was probably lost
14535 					 * momentarily - in such case
14536 					 * we may want to check device
14537 					 * identity.
14538 					 * Identity check is not supported now.
14539 					 *
14540 					 * Link established event
14541 					 * triggers device reset event.
14542 					 */
14543 					(SATA_CPORTINFO_DRV_INFO(cportinfo))->
14544 					    satadrv_event_flags |=
14545 					    SATA_EVNT_DEVICE_RESET;
14546 				}
14547 			} else if (cportinfo->cport_dev_type ==
14548 			    SATA_DTYPE_NONE) {
14549 				/*
14550 				 * We got new device attached! If HBA does not
14551 				 * generate device attached events, trigger it
14552 				 * here.
14553 				 */
14554 				if (!(SATA_FEATURES(sata_hba_inst) &
14555 				    SATA_CTLF_HOTPLUG)) {
14556 					cportinfo->cport_event_flags |=
14557 					    SATA_EVNT_DEVICE_ATTACHED;
14558 				}
14559 			}
14560 			/* Reset link lost timeout */
14561 			cportinfo->cport_link_lost_time = 0;
14562 		}
14563 	}
14564 linklost:
14565 	if (event_flags & SATA_EVNT_LINK_LOST) {
14566 		if ((sata_device.satadev_scr.sstatus &
14567 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
14568 			/* Ignore event */
14569 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14570 			    "Ignoring port %d link lost event - link is up",
14571 			    saddr->cport);
14572 			goto done;
14573 		}
14574 #ifdef SATA_DEBUG
14575 		if (cportinfo->cport_link_lost_time == 0) {
14576 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14577 			    "Processing port %d link lost event",
14578 			    saddr->cport);
14579 		}
14580 #endif
14581 		/*
14582 		 * When HBA cannot generate device attached/detached events,
14583 		 * we need to track link lost time and eventually generate
14584 		 * device detach event.
14585 		 */
14586 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
14587 			/* We are tracking link lost time */
14588 			if (cportinfo->cport_link_lost_time == 0) {
14589 				/* save current time (lbolt value) */
14590 				cportinfo->cport_link_lost_time =
14591 				    ddi_get_lbolt();
14592 				/* just keep link lost event */
14593 				cportinfo->cport_event_flags |=
14594 				    SATA_EVNT_LINK_LOST;
14595 			} else {
14596 				clock_t cur_time = ddi_get_lbolt();
14597 				if ((cur_time -
14598 				    cportinfo->cport_link_lost_time) >=
14599 				    drv_usectohz(
14600 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
14601 					/* trigger device detach event */
14602 					cportinfo->cport_event_flags |=
14603 					    SATA_EVNT_DEVICE_DETACHED;
14604 					cportinfo->cport_link_lost_time = 0;
14605 					SATADBG1(SATA_DBG_EVENTS,
14606 					    sata_hba_inst,
14607 					    "Triggering port %d "
14608 					    "device detached event",
14609 					    saddr->cport);
14610 				} else {
14611 					/* keep link lost event */
14612 					cportinfo->cport_event_flags |=
14613 					    SATA_EVNT_LINK_LOST;
14614 				}
14615 			}
14616 		}
14617 		/*
14618 		 * We could change port state to disable/delay access to
14619 		 * the attached device until the link is recovered.
14620 		 */
14621 	}
14622 done:
14623 	event_flags = cportinfo->cport_event_flags;
14624 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14625 	if (event_flags != 0) {
14626 		mutex_enter(&sata_hba_inst->satahba_mutex);
14627 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14628 		mutex_exit(&sata_hba_inst->satahba_mutex);
14629 		mutex_enter(&sata_mutex);
14630 		sata_event_pending |= SATA_EVNT_MAIN;
14631 		mutex_exit(&sata_mutex);
14632 	}
14633 }
14634 
14635 /*
14636  * Device Detached Event processing.
14637  * Port is probed to find if a device is really gone. If so,
14638  * the device info structure is detached from the SATA port info structure
14639  * and released.
14640  * Port status is updated.
14641  *
14642  * NOTE: Process cports event only, no port multiplier ports.
14643  */
14644 static void
14645 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst,
14646     sata_address_t *saddr)
14647 {
14648 	sata_cport_info_t *cportinfo;
14649 	sata_drive_info_t *sdevinfo;
14650 	sata_device_t sata_device;
14651 	dev_info_t *tdip;
14652 	int rval;
14653 
14654 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14655 	    "Processing port %d device detached", saddr->cport);
14656 
14657 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14658 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14659 	/* Clear event flag */
14660 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
14661 
14662 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
14663 	if ((cportinfo->cport_state &
14664 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14665 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14666 		    cport_mutex);
14667 		return;
14668 	}
14669 	/* For sanity, re-probe the port */
14670 	sata_device.satadev_rev = SATA_DEVICE_REV;
14671 	sata_device.satadev_addr = *saddr;
14672 
14673 	/*
14674 	 * We have to exit mutex, because the HBA probe port function may
14675 	 * block on its own mutex.
14676 	 */
14677 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14678 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14679 	    (SATA_DIP(sata_hba_inst), &sata_device);
14680 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14681 	sata_update_port_info(sata_hba_inst, &sata_device);
14682 	if (rval != SATA_SUCCESS) {
14683 		/* Something went wrong? Fail the port */
14684 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14685 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14686 		    cport_mutex);
14687 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14688 		    "SATA port %d probing failed",
14689 		    saddr->cport));
14690 		/*
14691 		 * We may want to release device info structure, but
14692 		 * it is not necessary.
14693 		 */
14694 		return;
14695 	} else {
14696 		/* port probed successfully */
14697 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14698 	}
14699 	/*
14700 	 * Check if a device is still attached. For sanity, check also
14701 	 * link status - if no link, there is no device.
14702 	 */
14703 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
14704 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
14705 	    SATA_DTYPE_NONE) {
14706 		/*
14707 		 * Device is still attached - ignore detach event.
14708 		 */
14709 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14710 		    cport_mutex);
14711 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14712 		    "Ignoring detach - device still attached to port %d",
14713 		    sata_device.satadev_addr.cport);
14714 		return;
14715 	}
14716 	/*
14717 	 * We need to detach and release device info structure here
14718 	 */
14719 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14720 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14721 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14722 		(void) kmem_free((void *)sdevinfo,
14723 		    sizeof (sata_drive_info_t));
14724 	}
14725 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14726 	/*
14727 	 * Device cannot be reached anymore, even if the target node may be
14728 	 * still present.
14729 	 */
14730 
14731 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14732 	sata_log(sata_hba_inst, CE_WARN, "SATA device detached at port %d",
14733 	    sata_device.satadev_addr.cport);
14734 
14735 	/*
14736 	 * Try to offline a device and remove target node if it still exists
14737 	 */
14738 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
14739 	if (tdip != NULL) {
14740 		/*
14741 		 * Target node exists.  Unconfigure device then remove
14742 		 * the target node (one ndi operation).
14743 		 */
14744 		if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
14745 			/*
14746 			 * PROBLEM - no device, but target node remained
14747 			 * This happens when the file was open or node was
14748 			 * waiting for resources.
14749 			 */
14750 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14751 			    "sata_process_device_detached: "
14752 			    "Failed to remove target node for "
14753 			    "detached SATA device."));
14754 			/*
14755 			 * Set target node state to DEVI_DEVICE_REMOVED.
14756 			 * But re-check first that the node still exists.
14757 			 */
14758 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
14759 			    saddr->cport);
14760 			if (tdip != NULL) {
14761 				sata_set_device_removed(tdip);
14762 				/*
14763 				 * Instruct event daemon to retry the
14764 				 * cleanup later.
14765 				 */
14766 				sata_set_target_node_cleanup(sata_hba_inst,
14767 				    &sata_device.satadev_addr);
14768 			}
14769 		}
14770 	}
14771 	/*
14772 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14773 	 * with the hint: SE_HINT_REMOVE
14774 	 */
14775 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
14776 }
14777 
14778 
14779 /*
14780  * Device Attached Event processing.
14781  * Port state is checked to verify that a device is really attached. If so,
14782  * the device info structure is created and attached to the SATA port info
14783  * structure.
14784  *
14785  * If attached device cannot be identified or set-up, the retry for the
14786  * attach processing is set-up. Subsequent daemon run would try again to
14787  * identify the device, until the time limit is reached
14788  * (SATA_DEV_IDENTIFY_TIMEOUT).
14789  *
14790  * This function cannot be called in interrupt context (it may sleep).
14791  *
14792  * NOTE: Process cports event only, no port multiplier ports.
14793  */
14794 static void
14795 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst,
14796     sata_address_t *saddr)
14797 {
14798 	sata_cport_info_t *cportinfo;
14799 	sata_drive_info_t *sdevinfo;
14800 	sata_device_t sata_device;
14801 	dev_info_t *tdip;
14802 	uint32_t event_flags;
14803 	int rval;
14804 
14805 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14806 	    "Processing port %d device attached", saddr->cport);
14807 
14808 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14809 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14810 
14811 	/* Clear attach event flag first */
14812 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
14813 
14814 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
14815 	if ((cportinfo->cport_state &
14816 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14817 		cportinfo->cport_dev_attach_time = 0;
14818 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14819 		    cport_mutex);
14820 		return;
14821 	}
14822 
14823 	/*
14824 	 * If the sata_drive_info structure is found attached to the port info,
14825 	 * despite the fact the device was removed and now it is re-attached,
14826 	 * the old drive info structure was not removed.
14827 	 * Arbitrarily release device info structure.
14828 	 */
14829 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14830 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14831 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14832 		(void) kmem_free((void *)sdevinfo,
14833 		    sizeof (sata_drive_info_t));
14834 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14835 		    "Arbitrarily detaching old device info.", NULL);
14836 	}
14837 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14838 
14839 	/* For sanity, re-probe the port */
14840 	sata_device.satadev_rev = SATA_DEVICE_REV;
14841 	sata_device.satadev_addr = *saddr;
14842 
14843 	/*
14844 	 * We have to exit mutex, because the HBA probe port function may
14845 	 * block on its own mutex.
14846 	 */
14847 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14848 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14849 	    (SATA_DIP(sata_hba_inst), &sata_device);
14850 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14851 	sata_update_port_info(sata_hba_inst, &sata_device);
14852 	if (rval != SATA_SUCCESS) {
14853 		/* Something went wrong? Fail the port */
14854 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14855 		cportinfo->cport_dev_attach_time = 0;
14856 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14857 		    cport_mutex);
14858 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14859 		    "SATA port %d probing failed",
14860 		    saddr->cport));
14861 		return;
14862 	} else {
14863 		/* port probed successfully */
14864 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14865 	}
14866 	/*
14867 	 * Check if a device is still attached. For sanity, check also
14868 	 * link status - if no link, there is no device.
14869 	 */
14870 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
14871 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
14872 	    SATA_DTYPE_NONE) {
14873 		/*
14874 		 * No device - ignore attach event.
14875 		 */
14876 		cportinfo->cport_dev_attach_time = 0;
14877 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14878 		    cport_mutex);
14879 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14880 		    "Ignoring attach - no device connected to port %d",
14881 		    sata_device.satadev_addr.cport);
14882 		return;
14883 	}
14884 
14885 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14886 	/*
14887 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14888 	 * with the hint: SE_HINT_INSERT
14889 	 */
14890 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
14891 
14892 	/*
14893 	 * Port reprobing will take care of the creation of the device
14894 	 * info structure and determination of the device type.
14895 	 */
14896 	sata_device.satadev_addr = *saddr;
14897 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
14898 	    SATA_DEV_IDENTIFY_NORETRY);
14899 
14900 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14901 	    cport_mutex);
14902 	if ((cportinfo->cport_state & SATA_STATE_READY) &&
14903 	    (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) {
14904 		/* Some device is attached to the port */
14905 		if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) {
14906 			/*
14907 			 * A device was not successfully attached.
14908 			 * Track retry time for device identification.
14909 			 */
14910 			if (cportinfo->cport_dev_attach_time != 0) {
14911 				clock_t cur_time = ddi_get_lbolt();
14912 				/*
14913 				 * If the retry time limit was not exceeded,
14914 				 * reinstate attach event.
14915 				 */
14916 				if ((cur_time -
14917 				    cportinfo->cport_dev_attach_time) <
14918 				    drv_usectohz(
14919 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
14920 					/* OK, restore attach event */
14921 					cportinfo->cport_event_flags |=
14922 					    SATA_EVNT_DEVICE_ATTACHED;
14923 				} else {
14924 					/* Timeout - cannot identify device */
14925 					cportinfo->cport_dev_attach_time = 0;
14926 					sata_log(sata_hba_inst,
14927 					    CE_WARN,
14928 					    "Could not identify SATA device "
14929 					    "at port %d",
14930 					    saddr->cport);
14931 				}
14932 			} else {
14933 				/*
14934 				 * Start tracking time for device
14935 				 * identification.
14936 				 * Save current time (lbolt value).
14937 				 */
14938 				cportinfo->cport_dev_attach_time =
14939 				    ddi_get_lbolt();
14940 				/* Restore attach event */
14941 				cportinfo->cport_event_flags |=
14942 				    SATA_EVNT_DEVICE_ATTACHED;
14943 			}
14944 		} else {
14945 			/*
14946 			 * If device was successfully attached, the subsequent
14947 			 * action depends on a state of the
14948 			 * sata_auto_online variable. If it is set to zero.
14949 			 * an explicit 'configure' command will be needed to
14950 			 * configure it. If its value is non-zero, we will
14951 			 * attempt to online (configure) the device.
14952 			 * First, log the message indicating that a device
14953 			 * was attached.
14954 			 */
14955 			cportinfo->cport_dev_attach_time = 0;
14956 			sata_log(sata_hba_inst, CE_WARN,
14957 			    "SATA device detected at port %d", saddr->cport);
14958 
14959 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14960 				sata_drive_info_t new_sdinfo;
14961 
14962 				/* Log device info data */
14963 				new_sdinfo = *(SATA_CPORTINFO_DRV_INFO(
14964 				    cportinfo));
14965 				sata_show_drive_info(sata_hba_inst,
14966 				    &new_sdinfo);
14967 			}
14968 
14969 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14970 			    saddr->cport)->cport_mutex);
14971 
14972 			/*
14973 			 * Make sure that there is no target node for that
14974 			 * device. If so, release it. It should not happen,
14975 			 * unless we had problem removing the node when
14976 			 * device was detached.
14977 			 */
14978 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
14979 			    saddr->cport);
14980 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14981 			    saddr->cport)->cport_mutex);
14982 			if (tdip != NULL) {
14983 
14984 #ifdef SATA_DEBUG
14985 				if ((cportinfo->cport_event_flags &
14986 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
14987 					sata_log(sata_hba_inst, CE_WARN,
14988 					    "sata_process_device_attached: "
14989 					    "old device target node exists!");
14990 #endif
14991 				/*
14992 				 * target node exists - try to unconfigure
14993 				 * device and remove the node.
14994 				 */
14995 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14996 				    saddr->cport)->cport_mutex);
14997 				rval = ndi_devi_offline(tdip,
14998 				    NDI_DEVI_REMOVE);
14999 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15000 				    saddr->cport)->cport_mutex);
15001 
15002 				if (rval == NDI_SUCCESS) {
15003 					cportinfo->cport_event_flags &=
15004 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
15005 					cportinfo->cport_tgtnode_clean = B_TRUE;
15006 				} else {
15007 					/*
15008 					 * PROBLEM - the target node remained
15009 					 * and it belongs to a previously
15010 					 * attached device.
15011 					 * This happens when the file was open
15012 					 * or the node was waiting for
15013 					 * resources at the time the
15014 					 * associated device was removed.
15015 					 * Instruct event daemon to retry the
15016 					 * cleanup later.
15017 					 */
15018 					sata_log(sata_hba_inst,
15019 					    CE_WARN,
15020 					    "Application(s) accessing "
15021 					    "previously attached SATA "
15022 					    "device have to release "
15023 					    "it before newly inserted "
15024 					    "device can be made accessible.",
15025 					    saddr->cport);
15026 					cportinfo->cport_event_flags |=
15027 					    SATA_EVNT_TARGET_NODE_CLEANUP;
15028 					cportinfo->cport_tgtnode_clean =
15029 					    B_FALSE;
15030 				}
15031 			}
15032 			if (sata_auto_online != 0) {
15033 				cportinfo->cport_event_flags |=
15034 				    SATA_EVNT_AUTOONLINE_DEVICE;
15035 			}
15036 
15037 		}
15038 	} else {
15039 		cportinfo->cport_dev_attach_time = 0;
15040 	}
15041 
15042 	event_flags = cportinfo->cport_event_flags;
15043 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15044 	if (event_flags != 0) {
15045 		mutex_enter(&sata_hba_inst->satahba_mutex);
15046 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15047 		mutex_exit(&sata_hba_inst->satahba_mutex);
15048 		mutex_enter(&sata_mutex);
15049 		sata_event_pending |= SATA_EVNT_MAIN;
15050 		mutex_exit(&sata_mutex);
15051 	}
15052 }
15053 
15054 
15055 /*
15056  * Device Target Node Cleanup Event processing.
15057  * If the target node associated with a sata port device is in
15058  * DEVI_DEVICE_REMOVED state, an attempt is made to remove it.
15059  * If the target node cannot be removed, the event flag is left intact,
15060  * so that event daemon may re-run this function later.
15061  *
15062  * This function cannot be called in interrupt context (it may sleep).
15063  *
15064  * NOTE: Processes cport events only, not port multiplier ports.
15065  */
15066 static void
15067 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
15068     sata_address_t *saddr)
15069 {
15070 	sata_cport_info_t *cportinfo;
15071 	dev_info_t *tdip;
15072 
15073 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15074 	    "Processing port %d device target node cleanup", saddr->cport);
15075 
15076 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15077 
15078 	/*
15079 	 * Check if there is target node for that device and it is in the
15080 	 * DEVI_DEVICE_REMOVED state. If so, release it.
15081 	 */
15082 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
15083 	if (tdip != NULL) {
15084 		/*
15085 		 * target node exists - check if it is target node of
15086 		 * a removed device.
15087 		 */
15088 		if (sata_check_device_removed(tdip) == B_TRUE) {
15089 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15090 			    "sata_process_target_node_cleanup: "
15091 			    "old device target node exists!", NULL);
15092 			/*
15093 			 * Unconfigure and remove the target node
15094 			 */
15095 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) ==
15096 			    NDI_SUCCESS) {
15097 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15098 				    saddr->cport)->cport_mutex);
15099 				cportinfo->cport_event_flags &=
15100 				    ~SATA_EVNT_TARGET_NODE_CLEANUP;
15101 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15102 				    saddr->cport)->cport_mutex);
15103 				return;
15104 			}
15105 			/*
15106 			 * Event daemon will retry the cleanup later.
15107 			 */
15108 			mutex_enter(&sata_hba_inst->satahba_mutex);
15109 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15110 			mutex_exit(&sata_hba_inst->satahba_mutex);
15111 			mutex_enter(&sata_mutex);
15112 			sata_event_pending |= SATA_EVNT_MAIN;
15113 			mutex_exit(&sata_mutex);
15114 		}
15115 	} else {
15116 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15117 		    saddr->cport)->cport_mutex);
15118 		cportinfo->cport_event_flags &=
15119 		    ~SATA_EVNT_TARGET_NODE_CLEANUP;
15120 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15121 		    saddr->cport)->cport_mutex);
15122 	}
15123 }
15124 
15125 /*
15126  * Device AutoOnline Event processing.
15127  * If attached device is to be onlined, an attempt is made to online this
15128  * device, but only if there is no lingering (old) target node present.
15129  * If the device cannot be onlined, the event flag is left intact,
15130  * so that event daemon may re-run this function later.
15131  *
15132  * This function cannot be called in interrupt context (it may sleep).
15133  *
15134  * NOTE: Processes cport events only, not port multiplier ports.
15135  */
15136 static void
15137 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst,
15138     sata_address_t *saddr)
15139 {
15140 	sata_cport_info_t *cportinfo;
15141 	sata_drive_info_t *sdinfo;
15142 	sata_device_t sata_device;
15143 	dev_info_t *tdip;
15144 
15145 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15146 	    "Processing port %d attached device auto-onlining", saddr->cport);
15147 
15148 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15149 
15150 	/*
15151 	 * Check if device is present and recognized. If not, reset event.
15152 	 */
15153 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15154 	if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
15155 		/* Nothing to online */
15156 		cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
15157 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15158 		    saddr->cport)->cport_mutex);
15159 		return;
15160 	}
15161 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15162 
15163 	/*
15164 	 * Check if there is target node for this device and if it is in the
15165 	 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep
15166 	 * the event for later processing.
15167 	 */
15168 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
15169 	if (tdip != NULL) {
15170 		/*
15171 		 * target node exists - check if it is target node of
15172 		 * a removed device.
15173 		 */
15174 		if (sata_check_device_removed(tdip) == B_TRUE) {
15175 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15176 			    "sata_process_device_autoonline: "
15177 			    "old device target node exists!", NULL);
15178 			/*
15179 			 * Event daemon will retry device onlining later.
15180 			 */
15181 			mutex_enter(&sata_hba_inst->satahba_mutex);
15182 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15183 			mutex_exit(&sata_hba_inst->satahba_mutex);
15184 			mutex_enter(&sata_mutex);
15185 			sata_event_pending |= SATA_EVNT_MAIN;
15186 			mutex_exit(&sata_mutex);
15187 			return;
15188 		}
15189 		/*
15190 		 * If the target node is not in the 'removed" state, assume
15191 		 * that it belongs to this device. There is nothing more to do,
15192 		 * but reset the event.
15193 		 */
15194 	} else {
15195 
15196 		/*
15197 		 * Try to online the device
15198 		 * If there is any reset-related event, remove it. We are
15199 		 * configuring the device and no state restoring is needed.
15200 		 */
15201 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15202 		    saddr->cport)->cport_mutex);
15203 		sata_device.satadev_addr = *saddr;
15204 		if (saddr->qual == SATA_ADDR_CPORT)
15205 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
15206 		else
15207 			sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
15208 		sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
15209 		if (sdinfo != NULL) {
15210 			if (sdinfo->satadrv_event_flags &
15211 			    (SATA_EVNT_DEVICE_RESET |
15212 			    SATA_EVNT_INPROC_DEVICE_RESET))
15213 				sdinfo->satadrv_event_flags = 0;
15214 			sdinfo->satadrv_event_flags |=
15215 			    SATA_EVNT_CLEAR_DEVICE_RESET;
15216 
15217 			/* Need to create a new target node. */
15218 			cportinfo->cport_tgtnode_clean = B_TRUE;
15219 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15220 			    saddr->cport)->cport_mutex);
15221 			tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
15222 			    sata_hba_inst, &sata_device.satadev_addr);
15223 			if (tdip == NULL) {
15224 				/*
15225 				 * Configure (onlining) failed.
15226 				 * We will NOT retry
15227 				 */
15228 				SATA_LOG_D((sata_hba_inst, CE_WARN,
15229 				    "sata_process_device_autoonline: "
15230 				    "configuring SATA device at port %d failed",
15231 				    saddr->cport));
15232 			}
15233 		} else {
15234 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15235 			    saddr->cport)->cport_mutex);
15236 		}
15237 
15238 	}
15239 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15240 	cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
15241 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15242 	    saddr->cport)->cport_mutex);
15243 }
15244 
15245 
15246 static void
15247 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr,
15248     int hint)
15249 {
15250 	char ap[MAXPATHLEN];
15251 	nvlist_t *ev_attr_list = NULL;
15252 	int err;
15253 
15254 	/* Allocate and build sysevent attribute list */
15255 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP);
15256 	if (err != 0) {
15257 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15258 		    "sata_gen_sysevent: "
15259 		    "cannot allocate memory for sysevent attributes\n"));
15260 		return;
15261 	}
15262 	/* Add hint attribute */
15263 	err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint));
15264 	if (err != 0) {
15265 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15266 		    "sata_gen_sysevent: "
15267 		    "failed to add DR_HINT attr for sysevent"));
15268 		nvlist_free(ev_attr_list);
15269 		return;
15270 	}
15271 	/*
15272 	 * Add AP attribute.
15273 	 * Get controller pathname and convert it into AP pathname by adding
15274 	 * a target number.
15275 	 */
15276 	(void) snprintf(ap, MAXPATHLEN, "/devices");
15277 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap));
15278 	(void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d",
15279 	    SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual));
15280 
15281 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap);
15282 	if (err != 0) {
15283 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15284 		    "sata_gen_sysevent: "
15285 		    "failed to add DR_AP_ID attr for sysevent"));
15286 		nvlist_free(ev_attr_list);
15287 		return;
15288 	}
15289 
15290 	/* Generate/log sysevent */
15291 	err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR,
15292 	    ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP);
15293 	if (err != DDI_SUCCESS) {
15294 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15295 		    "sata_gen_sysevent: "
15296 		    "cannot log sysevent, err code %x\n", err));
15297 	}
15298 
15299 	nvlist_free(ev_attr_list);
15300 }
15301 
15302 
15303 
15304 
15305 /*
15306  * Set DEVI_DEVICE_REMOVED state in the SATA device target node.
15307  */
15308 static void
15309 sata_set_device_removed(dev_info_t *tdip)
15310 {
15311 	int circ;
15312 
15313 	ASSERT(tdip != NULL);
15314 
15315 	ndi_devi_enter(tdip, &circ);
15316 	mutex_enter(&DEVI(tdip)->devi_lock);
15317 	DEVI_SET_DEVICE_REMOVED(tdip);
15318 	mutex_exit(&DEVI(tdip)->devi_lock);
15319 	ndi_devi_exit(tdip, circ);
15320 }
15321 
15322 
15323 /*
15324  * Set internal event instructing event daemon to try
15325  * to perform the target node cleanup.
15326  */
15327 static void
15328 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
15329     sata_address_t *saddr)
15330 {
15331 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15332 	SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |=
15333 	    SATA_EVNT_TARGET_NODE_CLEANUP;
15334 	SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_tgtnode_clean =
15335 	    B_FALSE;
15336 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15337 	mutex_enter(&sata_hba_inst->satahba_mutex);
15338 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15339 	mutex_exit(&sata_hba_inst->satahba_mutex);
15340 	mutex_enter(&sata_mutex);
15341 	sata_event_pending |= SATA_EVNT_MAIN;
15342 	mutex_exit(&sata_mutex);
15343 }
15344 
15345 
15346 /*
15347  * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state,
15348  * i.e. check if the target node state indicates that it belongs to a removed
15349  * device.
15350  *
15351  * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state,
15352  * B_FALSE otherwise.
15353  *
15354  * NOTE: No port multiplier support.
15355  */
15356 static boolean_t
15357 sata_check_device_removed(dev_info_t *tdip)
15358 {
15359 	ASSERT(tdip != NULL);
15360 
15361 	if (DEVI_IS_DEVICE_REMOVED(tdip))
15362 		return (B_TRUE);
15363 	else
15364 		return (B_FALSE);
15365 }
15366 
15367 /* ************************ FAULT INJECTTION **************************** */
15368 
15369 #ifdef SATA_INJECT_FAULTS
15370 
15371 static	uint32_t sata_fault_count = 0;
15372 static	uint32_t sata_fault_suspend_count = 0;
15373 
15374 /*
15375  * Inject sata pkt fault
15376  * It modifies returned values of the sata packet.
15377  * First argument is the pointer to the executed sata packet.
15378  * The second argument specifies SATA command to be affected (not all commands
15379  * are instrumented).
15380  * Third argument is a pointer to a value returned by the HBA tran_start
15381  * function.
15382  * Fourth argument specifies injected error. Injected sata packet faults
15383  * are the satapkt_reason values.
15384  * SATA_PKT_BUSY		-1	Not completed, busy
15385  * SATA_PKT_DEV_ERROR		1	Device reported error
15386  * SATA_PKT_QUEUE_FULL		2	Not accepted, queue full
15387  * SATA_PKT_PORT_ERROR		3	Not completed, port error
15388  * SATA_PKT_CMD_UNSUPPORTED	4	Cmd unsupported
15389  * SATA_PKT_ABORTED		5	Aborted by request
15390  * SATA_PKT_TIMEOUT		6	Operation timeut
15391  * SATA_PKT_RESET		7	Aborted by reset request
15392  *
15393  * sata_inject_fault_count variable specifies number of times in row the
15394  * error is injected. Value of -1 specifies permanent fault, ie. every time
15395  * the fault injection pointnis reached, the fault is injected and anu pause
15396  * between fault injection specified by sata_inject_fault_pause_count is
15397  * ignored).
15398  *
15399  * sata_inject_fault_pause_count variable specifies number of times a fault
15400  * injection is bypassed (pause between fault injections).
15401  * If set to 0, a fault is injected only a number of times specified by
15402  * sata_inject_fault_count.
15403  *
15404  * The fault counts are static, so for periodic errors they have to be manually
15405  * reset to start repetition sequence from scratch.
15406  * If the original value returned by the HBA tran_start function is not
15407  * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error
15408  * is injected (to avoid masking real problems);
15409  *
15410  * NOTE: In its current incarnation, this function should be invoked only for
15411  * commands executed in SYNCHRONOUS mode.
15412  */
15413 
15414 
15415 static	void
15416 sata_inject_pkt_fault(sata_pkt_t *spkt, uint8_t cmd, int *rval,
15417     int fault)
15418 {
15419 	if (fault == 0)
15420 		return;
15421 	if (sata_inject_fault_count == 0)
15422 		return;
15423 
15424 	if (spkt->satapkt_cmd.satacmd_cmd_reg != cmd)
15425 		return;
15426 
15427 	if (*rval != SATA_TRAN_ACCEPTED ||
15428 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
15429 		sata_fault_count = 0;
15430 		sata_fault_suspend_count = 0;
15431 		return;
15432 	}
15433 	if (sata_fault_count == 0 && sata_fault_suspend_count != 0) {
15434 		/* Pause in the injection */
15435 		sata_fault_suspend_count -= 1;
15436 		return;
15437 	}
15438 
15439 	if (sata_fault_count == 0 && sata_fault_suspend_count == 0) {
15440 		/*
15441 		 * Init inject fault cycle. If fault count is set to -1,
15442 		 * it is a permanent fault.
15443 		 */
15444 		if (sata_inject_fault_count != -1) {
15445 			sata_fault_count = sata_inject_fault_count;
15446 			sata_fault_suspend_count =
15447 			    sata_inject_fault_pause_count;
15448 			if (sata_fault_suspend_count == 0)
15449 				sata_inject_fault_count = 0;
15450 		}
15451 	}
15452 
15453 	if (sata_fault_count != 0)
15454 		sata_fault_count -= 1;
15455 
15456 	switch (fault) {
15457 	case SATA_PKT_BUSY:
15458 		*rval = SATA_TRAN_BUSY;
15459 		spkt->satapkt_reason = SATA_PKT_BUSY;
15460 		break;
15461 
15462 	case SATA_PKT_QUEUE_FULL:
15463 		*rval = SATA_TRAN_QUEUE_FULL;
15464 		spkt->satapkt_reason = SATA_PKT_QUEUE_FULL;
15465 		break;
15466 
15467 	case SATA_PKT_CMD_UNSUPPORTED:
15468 		*rval = SATA_TRAN_CMD_UNSUPPORTED;
15469 		spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED;
15470 		break;
15471 
15472 	case SATA_PKT_PORT_ERROR:
15473 		/* This is "rejected" command */
15474 		*rval = SATA_TRAN_PORT_ERROR;
15475 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
15476 		/* Additional error setup could be done here - port state */
15477 		break;
15478 
15479 	case SATA_PKT_DEV_ERROR:
15480 		spkt->satapkt_reason = SATA_PKT_DEV_ERROR;
15481 		/*
15482 		 * Additional error setup could be done here
15483 		 */
15484 		break;
15485 
15486 	case SATA_PKT_ABORTED:
15487 		spkt->satapkt_reason = SATA_PKT_ABORTED;
15488 		break;
15489 
15490 	case SATA_PKT_TIMEOUT:
15491 		spkt->satapkt_reason = SATA_PKT_TIMEOUT;
15492 		/* Additional error setup could be done here */
15493 		break;
15494 
15495 	case SATA_PKT_RESET:
15496 		spkt->satapkt_reason = SATA_PKT_RESET;
15497 		/*
15498 		 * Additional error setup could be done here - device reset
15499 		 */
15500 		break;
15501 
15502 	default:
15503 		break;
15504 	}
15505 }
15506 
15507 #endif
15508