xref: /titanic_50/usr/src/uts/common/io/sata/impl/sata.c (revision 45462bf898cce4257293567d9170f6cf79d0ea1d)
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)
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 traget 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 
2742 	/* Validate address */
2743 	switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst,
2744 	    &spx->txlt_scsi_pkt->pkt_address, &sata_device)) {
2745 
2746 	case -1:
2747 		/* Invalid address or invalid device type */
2748 		return (TRAN_BADPKT);
2749 	case 1:
2750 		/* valid address but no device - it has disappeared ? */
2751 		spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE;
2752 		/*
2753 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2754 		 * only in callback function (for normal requests) and
2755 		 * in the dump code path.
2756 		 * So, if the callback is available, we need to do
2757 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2758 		 */
2759 		if (spx->txlt_scsi_pkt->pkt_comp != NULL) {
2760 			/* scsi callback required */
2761 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2762 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2763 			    (void *)spx->txlt_scsi_pkt,
2764 			    TQ_SLEEP) == NULL)
2765 				/* Scheduling the callback failed */
2766 				return (TRAN_BUSY);
2767 
2768 			return (TRAN_ACCEPT);
2769 		}
2770 		return (TRAN_FATAL_ERROR);
2771 	default:
2772 		/* all OK */
2773 		break;
2774 	}
2775 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2776 	    &spx->txlt_sata_pkt->satapkt_device);
2777 
2778 	/*
2779 	 * If device is in reset condition, reject the packet with
2780 	 * TRAN_BUSY, unless:
2781 	 * 1. system is panicking (dumping)
2782 	 * In such case only one thread is running and there is no way to
2783 	 * process reset.
2784 	 * 2. cfgadm operation is is progress (internal APCTL lock is set)
2785 	 * Some cfgadm operations involve drive commands, so reset condition
2786 	 * needs to be ignored for IOCTL operations.
2787 	 */
2788 	if ((sdinfo->satadrv_event_flags &
2789 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
2790 
2791 		if (!ddi_in_panic() &&
2792 		    ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst,
2793 		    sata_device.satadev_addr.cport) &
2794 		    SATA_APCTL_LOCK_PORT_BUSY) == 0)) {
2795 			spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
2796 			SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
2797 			    "sata_scsi_start: rejecting command because "
2798 			    "of device reset state\n", NULL);
2799 			return (TRAN_BUSY);
2800 		}
2801 	}
2802 
2803 	/*
2804 	 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by
2805 	 * sata_scsi_pkt_init() because pkt init had to work also with
2806 	 * non-existing devices.
2807 	 * Now we know that the packet was set-up for a real device, so its
2808 	 * type is known.
2809 	 */
2810 	spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type;
2811 
2812 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags;
2813 	if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst,
2814 	    sata_device.satadev_addr.cport)->cport_event_flags &
2815 	    SATA_APCTL_LOCK_PORT_BUSY) != 0) {
2816 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
2817 		    sata_ignore_dev_reset = B_TRUE;
2818 	}
2819 	/*
2820 	 * At this point the generic translation routine determined that the
2821 	 * scsi packet should be accepted. Packet completion reason may be
2822 	 * changed later when a different completion reason is determined.
2823 	 */
2824 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
2825 
2826 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
2827 		/* Synchronous execution */
2828 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH |
2829 		    SATA_OPMODE_POLLING;
2830 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
2831 		    sata_ignore_dev_reset = ddi_in_panic();
2832 	} else {
2833 		/* Asynchronous execution */
2834 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH |
2835 		    SATA_OPMODE_INTERRUPTS;
2836 	}
2837 	/* Convert queuing information */
2838 	if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG)
2839 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag =
2840 		    B_TRUE;
2841 	else if (spx->txlt_scsi_pkt->pkt_flags &
2842 	    (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD))
2843 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag =
2844 		    B_TRUE;
2845 
2846 	/* Always limit pkt time */
2847 	if (spx->txlt_scsi_pkt->pkt_time == 0)
2848 		spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time;
2849 	else
2850 		/* Pass on scsi_pkt time */
2851 		spx->txlt_sata_pkt->satapkt_time =
2852 		    spx->txlt_scsi_pkt->pkt_time;
2853 
2854 	return (TRAN_ACCEPT);
2855 }
2856 
2857 
2858 /*
2859  * Translate ATA Identify Device data to SCSI Inquiry data.
2860  * This function may be called only for ATA devices.
2861  * This function should not be called for ATAPI devices - they
2862  * respond directly to SCSI Inquiry command.
2863  *
2864  * SATA Identify Device data has to be valid in sata_rive_info.
2865  * Buffer has to accomodate the inquiry length (36 bytes).
2866  *
2867  * This function should be called with a port mutex held.
2868  */
2869 static	void
2870 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst,
2871     sata_drive_info_t *sdinfo, uint8_t *buf)
2872 {
2873 
2874 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
2875 	struct sata_id *sid = &sdinfo->satadrv_id;
2876 
2877 	/* Start with a nice clean slate */
2878 	bzero((void *)inq, sizeof (struct scsi_inquiry));
2879 
2880 	/*
2881 	 * Rely on the dev_type for setting paripheral qualifier.
2882 	 * Assume that  DTYPE_RODIRECT applies to CD/DVD R/W devices.
2883 	 * It could be that DTYPE_OPTICAL could also qualify in the future.
2884 	 * ATAPI Inquiry may provide more data to the target driver.
2885 	 */
2886 	inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
2887 	    DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */
2888 
2889 	inq->inq_rmb = sid->ai_config & SATA_REM_MEDIA ? 1 : 0;
2890 	inq->inq_qual = 0;	/* Device type qualifier (obsolete in SCSI3? */
2891 	inq->inq_iso = 0;	/* ISO version */
2892 	inq->inq_ecma = 0;	/* ECMA version */
2893 	inq->inq_ansi = 3;	/* ANSI version - SCSI 3 */
2894 	inq->inq_aenc = 0;	/* Async event notification cap. */
2895 	inq->inq_trmiop = 0;	/* Supports TERMINATE I/O PROC msg - NO */
2896 	inq->inq_normaca = 0;	/* setting NACA bit supported - NO */
2897 	inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */
2898 	inq->inq_len = 31;	/* Additional length */
2899 	inq->inq_dualp = 0;	/* dual port device - NO */
2900 	inq->inq_reladdr = 0;	/* Supports relative addressing - NO */
2901 	inq->inq_sync = 0;	/* Supports synchronous data xfers - NO */
2902 	inq->inq_linked = 0;	/* Supports linked commands - NO */
2903 				/*
2904 				 * Queuing support - controller has to
2905 				 * support some sort of command queuing.
2906 				 */
2907 	if (SATA_QDEPTH(sata_hba_inst) > 1)
2908 		inq->inq_cmdque = 1; /* Supports command queueing - YES */
2909 	else
2910 		inq->inq_cmdque = 0; /* Supports command queueing - NO */
2911 	inq->inq_sftre = 0;	/* Supports Soft Reset option - NO ??? */
2912 	inq->inq_wbus32 = 0;	/* Supports 32 bit wide data xfers - NO */
2913 	inq->inq_wbus16 = 0;	/* Supports 16 bit wide data xfers - NO */
2914 
2915 #ifdef	_LITTLE_ENDIAN
2916 	/* Swap text fields to match SCSI format */
2917 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
2918 	swab(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
2919 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
2920 		swab(sid->ai_fw, inq->inq_revision, 4);	/* Revision level */
2921 	else
2922 		swab(&sid->ai_fw[4], inq->inq_revision, 4);	/* Rev. level */
2923 #else	/* _LITTLE_ENDIAN */
2924 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
2925 	bcopy(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
2926 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
2927 		bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
2928 	else
2929 		bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
2930 #endif	/* _LITTLE_ENDIAN */
2931 }
2932 
2933 
2934 /*
2935  * Scsi response set up for invalid command (command not supported)
2936  *
2937  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
2938  */
2939 static int
2940 sata_txlt_invalid_command(sata_pkt_txlate_t *spx)
2941 {
2942 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
2943 	struct scsi_extended_sense *sense;
2944 
2945 	scsipkt->pkt_reason = CMD_CMPLT;
2946 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
2947 	    STATE_SENT_CMD | STATE_GOT_STATUS;
2948 
2949 	*scsipkt->pkt_scbp = STATUS_CHECK;
2950 
2951 	sense = sata_arq_sense(spx);
2952 	sense->es_key = KEY_ILLEGAL_REQUEST;
2953 	sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE;
2954 
2955 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
2956 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
2957 
2958 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
2959 	    scsipkt->pkt_comp != NULL)
2960 		/* scsi callback required */
2961 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2962 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2963 		    (void *)spx->txlt_scsi_pkt,
2964 		    TQ_SLEEP) == NULL)
2965 			/* Scheduling the callback failed */
2966 			return (TRAN_BUSY);
2967 	return (TRAN_ACCEPT);
2968 }
2969 
2970 /*
2971  * Scsi response setup for
2972  * emulated non-data command that requires no action/return data
2973  *
2974  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
2975  */
2976 static 	int
2977 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx)
2978 {
2979 	int rval;
2980 
2981 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
2982 
2983 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
2984 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
2985 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
2986 		return (rval);
2987 	}
2988 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
2989 
2990 	spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
2991 	    STATE_SENT_CMD | STATE_GOT_STATUS;
2992 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
2993 	*(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD;
2994 
2995 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
2996 	    "Scsi_pkt completion reason %x\n",
2997 	    spx->txlt_scsi_pkt->pkt_reason);
2998 
2999 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 &&
3000 	    spx->txlt_scsi_pkt->pkt_comp != NULL)
3001 		/* scsi callback required */
3002 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3003 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
3004 		    (void *)spx->txlt_scsi_pkt,
3005 		    TQ_SLEEP) == NULL)
3006 			/* Scheduling the callback failed */
3007 			return (TRAN_BUSY);
3008 	return (TRAN_ACCEPT);
3009 }
3010 
3011 
3012 /*
3013  * SATA translate command: Inquiry / Identify Device
3014  * Use cached Identify Device data for now, rather than issuing actual
3015  * Device Identify cmd request. If device is detached and re-attached,
3016  * asynchromous event processing should fetch and refresh Identify Device
3017  * data.
3018  * Two VPD pages are supported now:
3019  * Vital Product Data page
3020  * Unit Serial Number page
3021  *
3022  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3023  */
3024 
3025 #define	EVPD			1	/* Extended Vital Product Data flag */
3026 #define	CMDDT			2	/* Command Support Data - Obsolete */
3027 #define	INQUIRY_SUP_VPD_PAGE	0	/* Supported VDP Pages Page COde */
3028 #define	INQUIRY_USN_PAGE	0x80	/* Unit Serial Number Page Code */
3029 #define	INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */
3030 
3031 static int
3032 sata_txlt_inquiry(sata_pkt_txlate_t *spx)
3033 {
3034 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3035 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3036 	sata_drive_info_t *sdinfo;
3037 	struct scsi_extended_sense *sense;
3038 	int count;
3039 	uint8_t *p;
3040 	int i, j;
3041 	uint8_t page_buf[0xff]; /* Max length */
3042 	int rval;
3043 
3044 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3045 
3046 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
3047 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
3048 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3049 		return (rval);
3050 	}
3051 
3052 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3053 	    &spx->txlt_sata_pkt->satapkt_device);
3054 
3055 	ASSERT(sdinfo != NULL);
3056 
3057 	scsipkt->pkt_reason = CMD_CMPLT;
3058 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3059 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3060 
3061 	/* Reject not supported request */
3062 	if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */
3063 		*scsipkt->pkt_scbp = STATUS_CHECK;
3064 		sense = sata_arq_sense(spx);
3065 		sense->es_key = KEY_ILLEGAL_REQUEST;
3066 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3067 		goto done;
3068 	}
3069 
3070 	/* Valid Inquiry request */
3071 	*scsipkt->pkt_scbp = STATUS_GOOD;
3072 
3073 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3074 
3075 		/*
3076 		 * Because it is fully emulated command storing data
3077 		 * programatically in the specified buffer, release
3078 		 * preallocated DMA resources before storing data in the buffer,
3079 		 * so no unwanted DMA sync would take place.
3080 		 */
3081 		sata_scsi_dmafree(NULL, scsipkt);
3082 
3083 		if (!(scsipkt->pkt_cdbp[1] & EVPD)) {
3084 			/* Standard Inquiry Data request */
3085 			struct scsi_inquiry inq;
3086 			unsigned int bufsize;
3087 
3088 			sata_identdev_to_inquiry(spx->txlt_sata_hba_inst,
3089 			    sdinfo, (uint8_t *)&inq);
3090 			/* Copy no more than requested */
3091 			count = MIN(bp->b_bcount,
3092 			    sizeof (struct scsi_inquiry));
3093 			bufsize = scsipkt->pkt_cdbp[4];
3094 			bufsize |= scsipkt->pkt_cdbp[3] << 8;
3095 			count = MIN(count, bufsize);
3096 			bcopy(&inq, bp->b_un.b_addr, count);
3097 
3098 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
3099 			scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3100 			    bufsize - count : 0;
3101 		} else {
3102 			/*
3103 			 * peripheral_qualifier = 0;
3104 			 *
3105 			 * We are dealing only with HD and will be
3106 			 * dealing with CD/DVD devices soon
3107 			 */
3108 			uint8_t peripheral_device_type =
3109 			    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3110 			    DTYPE_DIRECT : DTYPE_RODIRECT;
3111 
3112 			switch ((uint_t)scsipkt->pkt_cdbp[2]) {
3113 			case INQUIRY_SUP_VPD_PAGE:
3114 				/*
3115 				 * Request for suported Vital Product Data
3116 				 * pages - assuming only 2 page codes
3117 				 * supported
3118 				 */
3119 				page_buf[0] = peripheral_device_type;
3120 				page_buf[1] = INQUIRY_SUP_VPD_PAGE;
3121 				page_buf[2] = 0;
3122 				page_buf[3] = 2; /* page length */
3123 				page_buf[4] = INQUIRY_SUP_VPD_PAGE;
3124 				page_buf[5] = INQUIRY_USN_PAGE;
3125 				/* Copy no more than requested */
3126 				count = MIN(bp->b_bcount, 6);
3127 				bcopy(page_buf, bp->b_un.b_addr, count);
3128 				break;
3129 			case INQUIRY_USN_PAGE:
3130 				/*
3131 				 * Request for Unit Serial Number page
3132 				 */
3133 				page_buf[0] = peripheral_device_type;
3134 				page_buf[1] = INQUIRY_USN_PAGE;
3135 				page_buf[2] = 0;
3136 				page_buf[3] = 20; /* remaining page length */
3137 				p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser);
3138 #ifdef	_LITTLE_ENDIAN
3139 				swab(p, &page_buf[4], 20);
3140 #else
3141 				bcopy(p, &page_buf[4], 20);
3142 #endif
3143 				for (i = 0; i < 20; i++) {
3144 					if (page_buf[4 + i] == '\0' ||
3145 					    page_buf[4 + i] == '\040') {
3146 						break;
3147 					}
3148 				}
3149 				/*
3150 				 * 'i' contains string length.
3151 				 *
3152 				 * Least significant character of the serial
3153 				 * number shall appear as the last byte,
3154 				 * according to SBC-3 spec.
3155 				 */
3156 				p = &page_buf[20 + 4 - 1];
3157 				for (j = i; j > 0; j--, p--) {
3158 					*p = *(p - 20 + i);
3159 				}
3160 				p = &page_buf[4];
3161 				for (j = 20 - i; j > 0; j--) {
3162 					*p++ = '\040';
3163 				}
3164 				count = MIN(bp->b_bcount, 24);
3165 				bcopy(page_buf, bp->b_un.b_addr, count);
3166 				break;
3167 
3168 			case INQUIRY_DEV_IDENTIFICATION_PAGE:
3169 				/*
3170 				 * We may want to implement this page, when
3171 				 * identifiers are common for SATA devices
3172 				 * But not now.
3173 				 */
3174 				/*FALLTHROUGH*/
3175 
3176 			default:
3177 				/* Request for unsupported VPD page */
3178 				*scsipkt->pkt_scbp = STATUS_CHECK;
3179 				sense = sata_arq_sense(spx);
3180 				sense->es_key = KEY_ILLEGAL_REQUEST;
3181 				sense->es_add_code =
3182 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3183 				goto done;
3184 			}
3185 		}
3186 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3187 		scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3188 		    scsipkt->pkt_cdbp[4] - count : 0;
3189 	}
3190 done:
3191 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3192 
3193 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3194 	    "Scsi_pkt completion reason %x\n",
3195 	    scsipkt->pkt_reason);
3196 
3197 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3198 	    scsipkt->pkt_comp != NULL) {
3199 		/* scsi callback required */
3200 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3201 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3202 		    TQ_SLEEP) == NULL)
3203 			/* Scheduling the callback failed */
3204 			return (TRAN_BUSY);
3205 	}
3206 	return (TRAN_ACCEPT);
3207 }
3208 
3209 /*
3210  * SATA translate command: Request Sense.
3211  * Emulated command (ATA version for SATA hard disks)
3212  * Always NO SENSE, because any sense data should be reported by ARQ sense.
3213  *
3214  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3215  */
3216 static int
3217 sata_txlt_request_sense(sata_pkt_txlate_t *spx)
3218 {
3219 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3220 	struct scsi_extended_sense sense;
3221 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3222 	int rval;
3223 
3224 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3225 
3226 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
3227 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
3228 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3229 		return (rval);
3230 	}
3231 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3232 
3233 
3234 	scsipkt->pkt_reason = CMD_CMPLT;
3235 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3236 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3237 	*scsipkt->pkt_scbp = STATUS_GOOD;
3238 
3239 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3240 		/*
3241 		 * Because it is fully emulated command storing data
3242 		 * programatically in the specified buffer, release
3243 		 * preallocated DMA resources before storing data in the buffer,
3244 		 * so no unwanted DMA sync would take place.
3245 		 */
3246 		int count = MIN(bp->b_bcount,
3247 		    sizeof (struct scsi_extended_sense));
3248 		sata_scsi_dmafree(NULL, scsipkt);
3249 		bzero(&sense, sizeof (struct scsi_extended_sense));
3250 		sense.es_valid = 0;	/* Valid LBA */
3251 		sense.es_class = 7;	/* Response code 0x70 - current err */
3252 		sense.es_key = KEY_NO_SENSE;
3253 		sense.es_add_len = 6;	/* Additional length */
3254 		/* Copy no more than requested */
3255 		bcopy(&sense, bp->b_un.b_addr, count);
3256 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3257 		scsipkt->pkt_resid = 0;
3258 	}
3259 
3260 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3261 	    "Scsi_pkt completion reason %x\n",
3262 	    scsipkt->pkt_reason);
3263 
3264 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3265 	    scsipkt->pkt_comp != NULL)
3266 		/* scsi callback required */
3267 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3268 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3269 		    TQ_SLEEP) == NULL)
3270 			/* Scheduling the callback failed */
3271 			return (TRAN_BUSY);
3272 	return (TRAN_ACCEPT);
3273 }
3274 
3275 /*
3276  * SATA translate command: Test Unit Ready
3277  * At the moment this is an emulated command (ATA version for SATA hard disks).
3278  * May be translated into Check Power Mode command in the future
3279  *
3280  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3281  */
3282 static int
3283 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx)
3284 {
3285 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3286 	struct scsi_extended_sense *sense;
3287 	int power_state;
3288 	int rval;
3289 
3290 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3291 
3292 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
3293 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
3294 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3295 		return (rval);
3296 	}
3297 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3298 
3299 	/* At this moment, emulate it rather than execute anything */
3300 	power_state = SATA_PWRMODE_ACTIVE;
3301 
3302 	scsipkt->pkt_reason = CMD_CMPLT;
3303 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3304 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3305 
3306 	switch (power_state) {
3307 	case SATA_PWRMODE_ACTIVE:
3308 	case SATA_PWRMODE_IDLE:
3309 		*scsipkt->pkt_scbp = STATUS_GOOD;
3310 		break;
3311 	default:
3312 		/* PWR mode standby */
3313 		*scsipkt->pkt_scbp = STATUS_CHECK;
3314 		sense = sata_arq_sense(spx);
3315 		sense->es_key = KEY_NOT_READY;
3316 		sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY;
3317 		break;
3318 	}
3319 
3320 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3321 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3322 
3323 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3324 	    scsipkt->pkt_comp != NULL)
3325 		/* scsi callback required */
3326 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3327 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3328 		    TQ_SLEEP) == NULL)
3329 			/* Scheduling the callback failed */
3330 			return (TRAN_BUSY);
3331 
3332 	return (TRAN_ACCEPT);
3333 }
3334 
3335 
3336 /*
3337  * SATA translate command: Start Stop Unit
3338  * Translation depends on a command:
3339  *	Start Unit translated into Idle Immediate
3340  *	Stop Unit translated into Standby Immediate
3341  *	Unload Media / NOT SUPPORTED YET
3342  *	Load Media / NOT SUPPROTED YET
3343  * Power condition bits are ignored, so is Immediate bit
3344  * Requesting synchronous execution.
3345  *
3346  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
3347  * appropriate values in scsi_pkt fields.
3348  */
3349 static int
3350 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx)
3351 {
3352 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3353 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3354 	struct scsi_extended_sense *sense;
3355 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3356 	int cport = SATA_TXLT_CPORT(spx);
3357 	int rval;
3358 	int synch;
3359 
3360 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3361 	    "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1);
3362 
3363 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3364 
3365 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
3366 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
3367 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3368 		return (rval);
3369 	}
3370 
3371 	if (scsipkt->pkt_cdbp[4] & 2) {
3372 		/* Load/Unload Media - invalid request */
3373 		*scsipkt->pkt_scbp = STATUS_CHECK;
3374 		sense = sata_arq_sense(spx);
3375 		sense->es_key = KEY_ILLEGAL_REQUEST;
3376 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3377 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3378 
3379 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3380 		    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3381 
3382 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3383 		    scsipkt->pkt_comp != NULL)
3384 			/* scsi callback required */
3385 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3386 			    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3387 			    TQ_SLEEP) == NULL)
3388 				/* Scheduling the callback failed */
3389 				return (TRAN_BUSY);
3390 
3391 		return (TRAN_ACCEPT);
3392 	}
3393 	scmd->satacmd_addr_type = 0;
3394 	scmd->satacmd_sec_count_lsb = 0;
3395 	scmd->satacmd_lba_low_lsb = 0;
3396 	scmd->satacmd_lba_mid_lsb = 0;
3397 	scmd->satacmd_lba_high_lsb = 0;
3398 	scmd->satacmd_features_reg = 0;
3399 	scmd->satacmd_device_reg = 0;
3400 	scmd->satacmd_status_reg = 0;
3401 	if (scsipkt->pkt_cdbp[4] & 1) {
3402 		/* Start Unit */
3403 		scmd->satacmd_cmd_reg = SATAC_IDLE_IM;
3404 	} else {
3405 		/* Stop Unit */
3406 		scmd->satacmd_cmd_reg = SATAC_STANDBY_IM;
3407 	}
3408 
3409 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
3410 		/* Need to set-up a callback function */
3411 		spx->txlt_sata_pkt->satapkt_comp =
3412 		    sata_txlt_nodata_cmd_completion;
3413 		synch = FALSE;
3414 	} else {
3415 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
3416 		synch = TRUE;
3417 	}
3418 
3419 	/* Transfer command to HBA */
3420 	if (sata_hba_start(spx, &rval) != 0) {
3421 		/* Pkt not accepted for execution */
3422 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3423 		return (rval);
3424 	}
3425 
3426 	/*
3427 	 * If execution is non-synchronous,
3428 	 * a callback function will handle potential errors, translate
3429 	 * the response and will do a callback to a target driver.
3430 	 * If it was synchronous, check execution status using the same
3431 	 * framework callback.
3432 	 */
3433 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3434 	if (synch) {
3435 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3436 		    "synchronous execution status %x\n",
3437 		    spx->txlt_sata_pkt->satapkt_reason);
3438 
3439 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
3440 	}
3441 	return (TRAN_ACCEPT);
3442 
3443 }
3444 
3445 
3446 /*
3447  * SATA translate command:  Read Capacity.
3448  * Emulated command for SATA disks.
3449  * Capacity is retrieved from cached Idenifty Device data.
3450  * Identify Device data shows effective disk capacity, not the native
3451  * capacity, which may be limitted by Set Max Address command.
3452  * This is ATA version for SATA hard disks.
3453  *
3454  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3455  */
3456 static int
3457 sata_txlt_read_capacity(sata_pkt_txlate_t *spx)
3458 {
3459 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3460 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3461 	sata_drive_info_t *sdinfo;
3462 	uint64_t val;
3463 	uchar_t *rbuf;
3464 	int rval;
3465 
3466 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3467 	    "sata_txlt_read_capacity: ", NULL);
3468 
3469 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3470 
3471 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
3472 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
3473 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3474 		return (rval);
3475 	}
3476 
3477 	scsipkt->pkt_reason = CMD_CMPLT;
3478 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3479 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3480 	*scsipkt->pkt_scbp = STATUS_GOOD;
3481 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3482 		/*
3483 		 * Because it is fully emulated command storing data
3484 		 * programatically in the specified buffer, release
3485 		 * preallocated DMA resources before storing data in the buffer,
3486 		 * so no unwanted DMA sync would take place.
3487 		 */
3488 		sata_scsi_dmafree(NULL, scsipkt);
3489 
3490 		sdinfo = sata_get_device_info(
3491 		    spx->txlt_sata_hba_inst,
3492 		    &spx->txlt_sata_pkt->satapkt_device);
3493 		/* Last logical block address */
3494 		val = sdinfo->satadrv_capacity - 1;
3495 		rbuf = (uchar_t *)bp->b_un.b_addr;
3496 		/* Need to swap endians to match scsi format */
3497 		rbuf[0] = (val >> 24) & 0xff;
3498 		rbuf[1] = (val >> 16) & 0xff;
3499 		rbuf[2] = (val >> 8) & 0xff;
3500 		rbuf[3] = val & 0xff;
3501 		/* block size - always 512 bytes, for now */
3502 		rbuf[4] = 0;
3503 		rbuf[5] = 0;
3504 		rbuf[6] = 0x02;
3505 		rbuf[7] = 0;
3506 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3507 		scsipkt->pkt_resid = 0;
3508 
3509 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n",
3510 		    sdinfo->satadrv_capacity -1);
3511 	}
3512 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3513 	/*
3514 	 * If a callback was requested, do it now.
3515 	 */
3516 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3517 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3518 
3519 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3520 	    scsipkt->pkt_comp != NULL)
3521 		/* scsi callback required */
3522 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3523 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3524 		    TQ_SLEEP) == NULL)
3525 			/* Scheduling the callback failed */
3526 			return (TRAN_BUSY);
3527 
3528 	return (TRAN_ACCEPT);
3529 }
3530 
3531 /*
3532  * SATA translate command: Mode Sense.
3533  * Translated into appropriate SATA command or emulated.
3534  * Saved Values Page Control (03) are not supported.
3535  *
3536  * NOTE: only caching mode sense page is currently implemented.
3537  *
3538  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3539  */
3540 
3541 static int
3542 sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
3543 {
3544 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
3545 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3546 	sata_drive_info_t *sdinfo;
3547 	sata_id_t *sata_id;
3548 	struct scsi_extended_sense *sense;
3549 	int 		len, bdlen, count, alc_len;
3550 	int		pc;	/* Page Control code */
3551 	uint8_t		*buf;	/* mode sense buffer */
3552 	int		rval;
3553 
3554 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3555 	    "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
3556 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
3557 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
3558 
3559 	buf = kmem_zalloc(1024, KM_SLEEP);
3560 
3561 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3562 
3563 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
3564 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
3565 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3566 		kmem_free(buf, 1024);
3567 		return (rval);
3568 	}
3569 
3570 	scsipkt->pkt_reason = CMD_CMPLT;
3571 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3572 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3573 
3574 	pc = scsipkt->pkt_cdbp[2] >> 6;
3575 
3576 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3577 		/*
3578 		 * Because it is fully emulated command storing data
3579 		 * programatically in the specified buffer, release
3580 		 * preallocated DMA resources before storing data in the buffer,
3581 		 * so no unwanted DMA sync would take place.
3582 		 */
3583 		sata_scsi_dmafree(NULL, scsipkt);
3584 
3585 		len = 0;
3586 		bdlen = 0;
3587 		if (!(scsipkt->pkt_cdbp[1] & 8)) {
3588 			if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
3589 			    (scsipkt->pkt_cdbp[0] & 0x10))
3590 				bdlen = 16;
3591 			else
3592 				bdlen = 8;
3593 		}
3594 		/* Build mode parameter header */
3595 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3596 			/* 4-byte mode parameter header */
3597 			buf[len++] = 0;   	/* mode data length */
3598 			buf[len++] = 0;		/* medium type */
3599 			buf[len++] = 0;		/* dev-specific param */
3600 			buf[len++] = bdlen;	/* Block Descriptor length */
3601 		} else {
3602 			/* 8-byte mode parameter header */
3603 			buf[len++] = 0;		/* mode data length */
3604 			buf[len++] = 0;
3605 			buf[len++] = 0;		/* medium type */
3606 			buf[len++] = 0;		/* dev-specific param */
3607 			if (bdlen == 16)
3608 				buf[len++] = 1;	/* long lba descriptor */
3609 			else
3610 				buf[len++] = 0;
3611 			buf[len++] = 0;
3612 			buf[len++] = 0;		/* Block Descriptor length */
3613 			buf[len++] = bdlen;
3614 		}
3615 
3616 		sdinfo = sata_get_device_info(
3617 		    spx->txlt_sata_hba_inst,
3618 		    &spx->txlt_sata_pkt->satapkt_device);
3619 
3620 		/* Build block descriptor only if not disabled (DBD) */
3621 		if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
3622 			/* Block descriptor - direct-access device format */
3623 			if (bdlen == 8) {
3624 				/* build regular block descriptor */
3625 				buf[len++] =
3626 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
3627 				buf[len++] =
3628 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
3629 				buf[len++] =
3630 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
3631 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
3632 				buf[len++] = 0; /* density code */
3633 				buf[len++] = 0;
3634 				if (sdinfo->satadrv_type ==
3635 				    SATA_DTYPE_ATADISK)
3636 					buf[len++] = 2;
3637 				else
3638 					/* ATAPI */
3639 					buf[len++] = 8;
3640 				buf[len++] = 0;
3641 			} else if (bdlen == 16) {
3642 				/* Long LBA Accepted */
3643 				/* build long lba block descriptor */
3644 #ifndef __lock_lint
3645 				buf[len++] =
3646 				    (sdinfo->satadrv_capacity >> 56) & 0xff;
3647 				buf[len++] =
3648 				    (sdinfo->satadrv_capacity >> 48) & 0xff;
3649 				buf[len++] =
3650 				    (sdinfo->satadrv_capacity >> 40) & 0xff;
3651 				buf[len++] =
3652 				    (sdinfo->satadrv_capacity >> 32) & 0xff;
3653 #endif
3654 				buf[len++] =
3655 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
3656 				buf[len++] =
3657 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
3658 				buf[len++] =
3659 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
3660 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
3661 				buf[len++] = 0;
3662 				buf[len++] = 0; /* density code */
3663 				buf[len++] = 0;
3664 				buf[len++] = 0;
3665 				if (sdinfo->satadrv_type ==
3666 				    SATA_DTYPE_ATADISK)
3667 					buf[len++] = 2;
3668 				else
3669 					/* ATAPI */
3670 					buf[len++] = 8;
3671 				buf[len++] = 0;
3672 			}
3673 		}
3674 
3675 		sata_id = &sdinfo->satadrv_id;
3676 
3677 		/*
3678 		 * Add requested pages.
3679 		 * Page 3 and 4 are obsolete and we are not supporting them.
3680 		 * We deal now with:
3681 		 * caching (read/write cache control).
3682 		 * We should eventually deal with following mode pages:
3683 		 * error recovery  (0x01),
3684 		 * power condition (0x1a),
3685 		 * exception control page (enables SMART) (0x1c),
3686 		 * enclosure management (ses),
3687 		 * protocol-specific port mode (port control).
3688 		 */
3689 		switch (scsipkt->pkt_cdbp[2] & 0x3f) {
3690 		case MODEPAGE_RW_ERRRECOV:
3691 			/* DAD_MODE_ERR_RECOV */
3692 			/* R/W recovery */
3693 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
3694 			break;
3695 		case MODEPAGE_CACHING:
3696 			/* DAD_MODE_CACHE */
3697 			/* Reject not supported request for saved parameters */
3698 			if (pc == 3) {
3699 				*scsipkt->pkt_scbp = STATUS_CHECK;
3700 				sense = sata_arq_sense(spx);
3701 				sense->es_key = KEY_ILLEGAL_REQUEST;
3702 				sense->es_add_code =
3703 				    SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
3704 				goto done;
3705 			}
3706 
3707 			/* caching */
3708 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
3709 			break;
3710 		case MODEPAGE_INFO_EXCPT:
3711 			/* exception cntrl */
3712 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
3713 				len += sata_build_msense_page_1c(sdinfo, pc,
3714 				    buf+len);
3715 			}
3716 			else
3717 				goto err;
3718 			break;
3719 		case MODEPAGE_POWER_COND:
3720 			/* DAD_MODE_POWER_COND */
3721 			/* power condition */
3722 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
3723 			break;
3724 
3725 		case MODEPAGE_ACOUSTIC_MANAG:
3726 			/* acoustic management */
3727 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
3728 			break;
3729 		case MODEPAGE_ALLPAGES:
3730 			/* all pages */
3731 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
3732 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
3733 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
3734 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
3735 				len += sata_build_msense_page_1c(sdinfo, pc,
3736 				    buf+len);
3737 			}
3738 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
3739 			break;
3740 		default:
3741 		err:
3742 			/* Invalid request */
3743 			*scsipkt->pkt_scbp = STATUS_CHECK;
3744 			sense = sata_arq_sense(spx);
3745 			sense->es_key = KEY_ILLEGAL_REQUEST;
3746 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3747 			goto done;
3748 		}
3749 
3750 		/* fix total mode data length */
3751 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3752 			/* 4-byte mode parameter header */
3753 			buf[0] = len - 1;   	/* mode data length */
3754 		} else {
3755 			buf[0] = (len -2) >> 8;
3756 			buf[1] = (len -2) & 0xff;
3757 		}
3758 
3759 
3760 		/* Check allocation length */
3761 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3762 			alc_len = scsipkt->pkt_cdbp[4];
3763 		} else {
3764 			alc_len = scsipkt->pkt_cdbp[7];
3765 			alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
3766 		}
3767 		/*
3768 		 * We do not check for possible parameters truncation
3769 		 * (alc_len < len) assuming that the target driver works
3770 		 * correctly. Just avoiding overrun.
3771 		 * Copy no more than requested and possible, buffer-wise.
3772 		 */
3773 		count = MIN(alc_len, len);
3774 		count = MIN(bp->b_bcount, count);
3775 		bcopy(buf, bp->b_un.b_addr, count);
3776 
3777 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3778 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
3779 	}
3780 	*scsipkt->pkt_scbp = STATUS_GOOD;
3781 done:
3782 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3783 	(void) kmem_free(buf, 1024);
3784 
3785 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3786 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3787 
3788 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3789 	    scsipkt->pkt_comp != NULL)
3790 		/* scsi callback required */
3791 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3792 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3793 		    TQ_SLEEP) == NULL)
3794 			/* Scheduling the callback failed */
3795 			return (TRAN_BUSY);
3796 
3797 	return (TRAN_ACCEPT);
3798 }
3799 
3800 
3801 /*
3802  * SATA translate command: Mode Select.
3803  * Translated into appropriate SATA command or emulated.
3804  * Saving parameters is not supported.
3805  * Changing device capacity is not supported (although theoretically
3806  * possible by executing SET FEATURES/SET MAX ADDRESS)
3807  *
3808  * Assumption is that the target driver is working correctly.
3809  *
3810  * More than one SATA command may be executed to perform operations specified
3811  * by mode select pages. The first error terminates further execution.
3812  * Operations performed successully are not backed-up in such case.
3813  *
3814  * NOTE: only caching mode select page is implemented.
3815  * Caching setup is remembered so it could be re-stored in case of
3816  * an unexpected device reset.
3817  *
3818  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3819  */
3820 
3821 static int
3822 sata_txlt_mode_select(sata_pkt_txlate_t *spx)
3823 {
3824 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3825 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3826 	struct scsi_extended_sense *sense;
3827 	int len, pagelen, count, pllen;
3828 	uint8_t *buf;	/* mode select buffer */
3829 	int rval, stat;
3830 	uint_t nointr_flag;
3831 	int dmod = 0;
3832 
3833 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3834 	    "sata_txlt_mode_select, pc %x page code 0x%02x\n",
3835 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
3836 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
3837 
3838 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3839 
3840 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
3841 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
3842 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3843 		return (rval);
3844 	}
3845 
3846 	rval = TRAN_ACCEPT;
3847 
3848 	scsipkt->pkt_reason = CMD_CMPLT;
3849 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3850 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3851 
3852 	/* Reject not supported request */
3853 	if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
3854 		*scsipkt->pkt_scbp = STATUS_CHECK;
3855 		sense = sata_arq_sense(spx);
3856 		sense->es_key = KEY_ILLEGAL_REQUEST;
3857 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3858 		goto done;
3859 	}
3860 
3861 	if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
3862 		pllen = scsipkt->pkt_cdbp[4];
3863 	} else {
3864 		pllen = scsipkt->pkt_cdbp[7];
3865 		pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
3866 	}
3867 
3868 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
3869 
3870 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
3871 		buf = (uint8_t *)bp->b_un.b_addr;
3872 		count = MIN(bp->b_bcount, pllen);
3873 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3874 		scsipkt->pkt_resid = 0;
3875 		pllen = count;
3876 
3877 		/*
3878 		 * Check the header to skip the block descriptor(s) - we
3879 		 * do not support setting device capacity.
3880 		 * Existing macros do not recognize long LBA dscriptor,
3881 		 * hence manual calculation.
3882 		 */
3883 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
3884 			/* 6-bytes CMD, 4 bytes header */
3885 			if (count <= 4)
3886 				goto done;		/* header only */
3887 			len = buf[3] + 4;
3888 		} else {
3889 			/* 10-bytes CMD, 8 bytes header */
3890 			if (count <= 8)
3891 				goto done;		/* header only */
3892 			len = buf[6];
3893 			len = (len << 8) + buf[7] + 8;
3894 		}
3895 		if (len >= count)
3896 			goto done;	/* header + descriptor(s) only */
3897 
3898 		pllen -= len;		/* remaining data length */
3899 
3900 		/*
3901 		 * We may be executing SATA command and want to execute it
3902 		 * in SYNCH mode, regardless of scsi_pkt setting.
3903 		 * Save scsi_pkt setting and indicate SYNCH mode
3904 		 */
3905 		nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
3906 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3907 		    scsipkt->pkt_comp != NULL) {
3908 			scsipkt->pkt_flags |= FLAG_NOINTR;
3909 		}
3910 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
3911 
3912 		/*
3913 		 * len is now the offset to a first mode select page
3914 		 * Process all pages
3915 		 */
3916 		while (pllen > 0) {
3917 			switch ((int)buf[len]) {
3918 			case MODEPAGE_CACHING:
3919 				/* No support for SP (saving) */
3920 				if (scsipkt->pkt_cdbp[1] & 0x01) {
3921 					*scsipkt->pkt_scbp = STATUS_CHECK;
3922 					sense = sata_arq_sense(spx);
3923 					sense->es_key = KEY_ILLEGAL_REQUEST;
3924 					sense->es_add_code =
3925 					    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3926 					goto done;
3927 				}
3928 				stat = sata_mode_select_page_8(spx,
3929 				    (struct mode_cache_scsi3 *)&buf[len],
3930 				    pllen, &pagelen, &rval, &dmod);
3931 				/*
3932 				 * The pagelen value indicates the number of
3933 				 * parameter bytes already processed.
3934 				 * The rval is the return value from
3935 				 * sata_tran_start().
3936 				 * The stat indicates the overall status of
3937 				 * the operation(s).
3938 				 */
3939 				if (stat != SATA_SUCCESS)
3940 					/*
3941 					 * Page processing did not succeed -
3942 					 * all error info is already set-up,
3943 					 * just return
3944 					 */
3945 					pllen = 0; /* this breaks the loop */
3946 				else {
3947 					len += pagelen;
3948 					pllen -= pagelen;
3949 				}
3950 				break;
3951 
3952 			case MODEPAGE_INFO_EXCPT:
3953 				stat = sata_mode_select_page_1c(spx,
3954 				    (struct mode_info_excpt_page *)&buf[len],
3955 				    pllen, &pagelen, &rval, &dmod);
3956 				/*
3957 				 * The pagelen value indicates the number of
3958 				 * parameter bytes already processed.
3959 				 * The rval is the return value from
3960 				 * sata_tran_start().
3961 				 * The stat indicates the overall status of
3962 				 * the operation(s).
3963 				 */
3964 				if (stat != SATA_SUCCESS)
3965 					/*
3966 					 * Page processing did not succeed -
3967 					 * all error info is already set-up,
3968 					 * just return
3969 					 */
3970 					pllen = 0; /* this breaks the loop */
3971 				else {
3972 					len += pagelen;
3973 					pllen -= pagelen;
3974 				}
3975 				break;
3976 
3977 			case MODEPAGE_ACOUSTIC_MANAG:
3978 				stat = sata_mode_select_page_30(spx,
3979 				    (struct mode_acoustic_management *)
3980 				    &buf[len], pllen, &pagelen, &rval, &dmod);
3981 				/*
3982 				 * The pagelen value indicates the number of
3983 				 * parameter bytes already processed.
3984 				 * The rval is the return value from
3985 				 * sata_tran_start().
3986 				 * The stat indicates the overall status of
3987 				 * the operation(s).
3988 				 */
3989 				if (stat != SATA_SUCCESS)
3990 					/*
3991 					 * Page processing did not succeed -
3992 					 * all error info is already set-up,
3993 					 * just return
3994 					 */
3995 					pllen = 0; /* this breaks the loop */
3996 				else {
3997 					len += pagelen;
3998 					pllen -= pagelen;
3999 				}
4000 
4001 				break;
4002 			default:
4003 				*scsipkt->pkt_scbp = STATUS_CHECK;
4004 				sense = sata_arq_sense(spx);
4005 				sense->es_key = KEY_ILLEGAL_REQUEST;
4006 				sense->es_add_code =
4007 				    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
4008 				goto done;
4009 			}
4010 		}
4011 	}
4012 done:
4013 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4014 	/*
4015 	 * If device parameters were modified, fetch and store the new
4016 	 * Identify Device data. Since port mutex could have been released
4017 	 * for accessing HBA driver, we need to re-check device existence.
4018 	 */
4019 	if (dmod != 0) {
4020 		sata_drive_info_t new_sdinfo, *sdinfo;
4021 		int rv = 0;
4022 
4023 		/*
4024 		 * Following statement has to be changed if this function is
4025 		 * used for devices other than SATA hard disks.
4026 		 */
4027 		new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
4028 
4029 		new_sdinfo.satadrv_addr =
4030 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr;
4031 		rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
4032 		    &new_sdinfo);
4033 
4034 		mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4035 		/*
4036 		 * Since port mutex could have been released when
4037 		 * accessing HBA driver, we need to re-check that the
4038 		 * framework still holds the device info structure.
4039 		 */
4040 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4041 		    &spx->txlt_sata_pkt->satapkt_device);
4042 		if (sdinfo != NULL) {
4043 			/*
4044 			 * Device still has info structure in the
4045 			 * sata framework. Copy newly fetched info
4046 			 */
4047 			if (rv == 0) {
4048 				sdinfo->satadrv_id = new_sdinfo.satadrv_id;
4049 				sata_save_drive_settings(sdinfo);
4050 			} else {
4051 				/*
4052 				 * Could not fetch new data - invalidate
4053 				 * sata_drive_info. That makes device
4054 				 * unusable.
4055 				 */
4056 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
4057 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
4058 			}
4059 		}
4060 		if (rv != 0 || sdinfo == NULL) {
4061 			/*
4062 			 * This changes the overall mode select completion
4063 			 * reason to a failed one !!!!!
4064 			 */
4065 			*scsipkt->pkt_scbp = STATUS_CHECK;
4066 			sense = sata_arq_sense(spx);
4067 			scsipkt->pkt_reason = CMD_INCOMPLETE;
4068 			rval = TRAN_ACCEPT;
4069 		}
4070 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4071 	}
4072 	/* Restore the scsi pkt flags */
4073 	scsipkt->pkt_flags &= ~FLAG_NOINTR;
4074 	scsipkt->pkt_flags |= nointr_flag;
4075 
4076 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4077 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4078 
4079 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4080 	    scsipkt->pkt_comp != NULL)
4081 		/* scsi callback required */
4082 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4083 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4084 		    TQ_SLEEP) == NULL)
4085 			/* Scheduling the callback failed */
4086 			return (TRAN_BUSY);
4087 
4088 	return (rval);
4089 }
4090 
4091 
4092 
4093 /*
4094  * Translate command: Log Sense
4095  */
4096 static 	int
4097 sata_txlt_log_sense(sata_pkt_txlate_t *spx)
4098 {
4099 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4100 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4101 	sata_drive_info_t *sdinfo;
4102 	struct scsi_extended_sense *sense;
4103 	int 		len, count, alc_len;
4104 	int		pc;	/* Page Control code */
4105 	int		page_code;	/* Page code */
4106 	uint8_t		*buf;	/* log sense buffer */
4107 	int		rval;
4108 #define	MAX_LOG_SENSE_PAGE_SIZE	512
4109 
4110 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4111 	    "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
4112 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4113 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4114 
4115 	buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
4116 
4117 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4118 
4119 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
4120 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
4121 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4122 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4123 		return (rval);
4124 	}
4125 
4126 	scsipkt->pkt_reason = CMD_CMPLT;
4127 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4128 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4129 
4130 	pc = scsipkt->pkt_cdbp[2] >> 6;
4131 	page_code = scsipkt->pkt_cdbp[2] & 0x3f;
4132 
4133 	/* Reject not supported request for all but cumulative values */
4134 	switch (pc) {
4135 	case PC_CUMULATIVE_VALUES:
4136 		break;
4137 	default:
4138 		*scsipkt->pkt_scbp = STATUS_CHECK;
4139 		sense = sata_arq_sense(spx);
4140 		sense->es_key = KEY_ILLEGAL_REQUEST;
4141 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4142 		goto done;
4143 	}
4144 
4145 	switch (page_code) {
4146 	case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
4147 	case PAGE_CODE_SELF_TEST_RESULTS:
4148 	case PAGE_CODE_INFORMATION_EXCEPTIONS:
4149 	case PAGE_CODE_SMART_READ_DATA:
4150 		break;
4151 	default:
4152 		*scsipkt->pkt_scbp = STATUS_CHECK;
4153 		sense = sata_arq_sense(spx);
4154 		sense->es_key = KEY_ILLEGAL_REQUEST;
4155 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4156 		goto done;
4157 	}
4158 
4159 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4160 		/*
4161 		 * Because log sense uses local buffers for data retrieval from
4162 		 * the devices and sets the data programatically in the
4163 		 * original specified buffer, release preallocated DMA
4164 		 * resources before storing data in the original buffer,
4165 		 * so no unwanted DMA sync would take place.
4166 		 */
4167 		sata_id_t *sata_id;
4168 
4169 		sata_scsi_dmafree(NULL, scsipkt);
4170 
4171 		len = 0;
4172 
4173 		/* Build log parameter header */
4174 		buf[len++] = page_code;	/* page code as in the CDB */
4175 		buf[len++] = 0;		/* reserved */
4176 		buf[len++] = 0;		/* Zero out page length for now (MSB) */
4177 		buf[len++] = 0;		/* (LSB) */
4178 
4179 		sdinfo = sata_get_device_info(
4180 		    spx->txlt_sata_hba_inst,
4181 		    &spx->txlt_sata_pkt->satapkt_device);
4182 
4183 
4184 		/*
4185 		 * Add requested pages.
4186 		 */
4187 		switch (page_code) {
4188 		case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
4189 			len = sata_build_lsense_page_0(sdinfo, buf + len);
4190 			break;
4191 		case PAGE_CODE_SELF_TEST_RESULTS:
4192 			sata_id = &sdinfo->satadrv_id;
4193 			if ((! (sata_id->ai_cmdset84 &
4194 			    SATA_SMART_SELF_TEST_SUPPORTED)) ||
4195 			    (! (sata_id->ai_features87 &
4196 			    SATA_SMART_SELF_TEST_SUPPORTED))) {
4197 				*scsipkt->pkt_scbp = STATUS_CHECK;
4198 				sense = sata_arq_sense(spx);
4199 				sense->es_key = KEY_ILLEGAL_REQUEST;
4200 				sense->es_add_code =
4201 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4202 
4203 				goto done;
4204 			}
4205 			len = sata_build_lsense_page_10(sdinfo, buf + len,
4206 			    spx->txlt_sata_hba_inst);
4207 			break;
4208 		case PAGE_CODE_INFORMATION_EXCEPTIONS:
4209 			sata_id = &sdinfo->satadrv_id;
4210 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
4211 				*scsipkt->pkt_scbp = STATUS_CHECK;
4212 				sense = sata_arq_sense(spx);
4213 				sense->es_key = KEY_ILLEGAL_REQUEST;
4214 				sense->es_add_code =
4215 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4216 
4217 				goto done;
4218 			}
4219 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
4220 				*scsipkt->pkt_scbp = STATUS_CHECK;
4221 				sense = sata_arq_sense(spx);
4222 				sense->es_key = KEY_ABORTED_COMMAND;
4223 				sense->es_add_code =
4224 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
4225 				sense->es_qual_code =
4226 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
4227 
4228 				goto done;
4229 			}
4230 
4231 			len = sata_build_lsense_page_2f(sdinfo, buf + len,
4232 			    spx->txlt_sata_hba_inst);
4233 			break;
4234 		case PAGE_CODE_SMART_READ_DATA:
4235 			sata_id = &sdinfo->satadrv_id;
4236 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
4237 				*scsipkt->pkt_scbp = STATUS_CHECK;
4238 				sense = sata_arq_sense(spx);
4239 				sense->es_key = KEY_ILLEGAL_REQUEST;
4240 				sense->es_add_code =
4241 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4242 
4243 				goto done;
4244 			}
4245 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
4246 				*scsipkt->pkt_scbp = STATUS_CHECK;
4247 				sense = sata_arq_sense(spx);
4248 				sense->es_key = KEY_ABORTED_COMMAND;
4249 				sense->es_add_code =
4250 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
4251 				sense->es_qual_code =
4252 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
4253 
4254 				goto done;
4255 			}
4256 
4257 			/* This page doesn't include a page header */
4258 			len = sata_build_lsense_page_30(sdinfo, buf,
4259 			    spx->txlt_sata_hba_inst);
4260 			goto no_header;
4261 		default:
4262 			/* Invalid request */
4263 			*scsipkt->pkt_scbp = STATUS_CHECK;
4264 			sense = sata_arq_sense(spx);
4265 			sense->es_key = KEY_ILLEGAL_REQUEST;
4266 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4267 			goto done;
4268 		}
4269 
4270 		/* set parameter log sense data length */
4271 		buf[2] = len >> 8;	/* log sense length (MSB) */
4272 		buf[3] = len & 0xff;	/* log sense length (LSB) */
4273 
4274 		len += SCSI_LOG_PAGE_HDR_LEN;
4275 		ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
4276 
4277 no_header:
4278 		/* Check allocation length */
4279 		alc_len = scsipkt->pkt_cdbp[7];
4280 		alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
4281 
4282 		/*
4283 		 * We do not check for possible parameters truncation
4284 		 * (alc_len < len) assuming that the target driver works
4285 		 * correctly. Just avoiding overrun.
4286 		 * Copy no more than requested and possible, buffer-wise.
4287 		 */
4288 		count = MIN(alc_len, len);
4289 		count = MIN(bp->b_bcount, count);
4290 		bcopy(buf, bp->b_un.b_addr, count);
4291 
4292 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4293 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
4294 	}
4295 	*scsipkt->pkt_scbp = STATUS_GOOD;
4296 done:
4297 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4298 	(void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4299 
4300 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4301 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4302 
4303 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4304 	    scsipkt->pkt_comp != NULL)
4305 		/* scsi callback required */
4306 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4307 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4308 		    TQ_SLEEP) == NULL)
4309 			/* Scheduling the callback failed */
4310 			return (TRAN_BUSY);
4311 
4312 	return (TRAN_ACCEPT);
4313 }
4314 
4315 /*
4316  * Translate command: Log Select
4317  * Not implemented at this time - returns invalid command response.
4318  */
4319 static 	int
4320 sata_txlt_log_select(sata_pkt_txlate_t *spx)
4321 {
4322 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4323 	    "sata_txlt_log_select\n", NULL);
4324 
4325 	return (sata_txlt_invalid_command(spx));
4326 }
4327 
4328 
4329 /*
4330  * Translate command: Read (various types).
4331  * Translated into appropriate type of ATA READ command
4332  * for SATA hard disks.
4333  * Both the device capabilities and requested operation mode are
4334  * considered.
4335  *
4336  * Following scsi cdb fields are ignored:
4337  * rdprotect, dpo, fua, fua_nv, group_number.
4338  *
4339  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
4340  * enable variable sata_func_enable), the capability of the controller and
4341  * capability of a device are checked and if both support queueing, read
4342  * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
4343  * command rather than plain READ_XXX command.
4344  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
4345  * both the controller and device suport such functionality, the read
4346  * request will be translated to READ_FPDMA_QUEUED command.
4347  * In both cases the maximum queue depth is derived as minimum of:
4348  * HBA capability,device capability and sata_max_queue_depth variable setting.
4349  * The value passed to HBA driver is decremented by 1, because only 5 bits are
4350  * used to pass max queue depth value, and the maximum possible queue depth
4351  * is 32.
4352  *
4353  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4354  * appropriate values in scsi_pkt fields.
4355  */
4356 static int
4357 sata_txlt_read(sata_pkt_txlate_t *spx)
4358 {
4359 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4360 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4361 	sata_drive_info_t *sdinfo;
4362 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4363 	int cport = SATA_TXLT_CPORT(spx);
4364 	uint16_t sec_count;
4365 	uint64_t lba;
4366 	int rval;
4367 	int synch;
4368 
4369 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4370 
4371 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
4372 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
4373 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4374 		return (rval);
4375 	}
4376 
4377 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4378 	    &spx->txlt_sata_pkt->satapkt_device);
4379 
4380 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
4381 	/*
4382 	 * Extract LBA and sector count from scsi CDB.
4383 	 */
4384 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
4385 	case SCMD_READ:
4386 		/* 6-byte scsi read cmd : 0x08 */
4387 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
4388 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
4389 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4390 		sec_count = scsipkt->pkt_cdbp[4];
4391 		/* sec_count 0 will be interpreted as 256 by a device */
4392 		break;
4393 	case SCMD_READ_G1:
4394 		/* 10-bytes scsi read command : 0x28 */
4395 		lba = scsipkt->pkt_cdbp[2];
4396 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4397 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4398 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4399 		sec_count = scsipkt->pkt_cdbp[7];
4400 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4401 		break;
4402 	case SCMD_READ_G5:
4403 		/* 12-bytes scsi read command : 0xA8 */
4404 		lba = scsipkt->pkt_cdbp[2];
4405 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4406 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4407 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4408 		sec_count = scsipkt->pkt_cdbp[6];
4409 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
4410 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4411 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
4412 		break;
4413 	case SCMD_READ_G4:
4414 		/* 16-bytes scsi read command : 0x88 */
4415 		lba = scsipkt->pkt_cdbp[2];
4416 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4417 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4418 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4419 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
4420 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
4421 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
4422 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
4423 		sec_count = scsipkt->pkt_cdbp[10];
4424 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
4425 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
4426 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
4427 		break;
4428 	default:
4429 		/* Unsupported command */
4430 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4431 		return (sata_txlt_invalid_command(spx));
4432 	}
4433 
4434 	/*
4435 	 * Check if specified address exceeds device capacity
4436 	 */
4437 	if ((lba >= sdinfo->satadrv_capacity) ||
4438 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
4439 		/* LBA out of range */
4440 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4441 		return (sata_txlt_lba_out_of_range(spx));
4442 	}
4443 
4444 	/*
4445 	 * For zero-length transfer, emulate good completion of the command
4446 	 * (reasons for rejecting the command were already checked).
4447 	 * No DMA resources were allocated.
4448 	 */
4449 	if (spx->txlt_dma_cookie_list == NULL) {
4450 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4451 		return (sata_emul_rw_completion(spx));
4452 	}
4453 
4454 	/*
4455 	 * Build cmd block depending on the device capability and
4456 	 * requested operation mode.
4457 	 * Do not bother with non-dma mode - we are working only with
4458 	 * devices supporting DMA.
4459 	 */
4460 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
4461 	scmd->satacmd_device_reg = SATA_ADH_LBA;
4462 	scmd->satacmd_cmd_reg = SATAC_READ_DMA;
4463 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
4464 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4465 		scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT;
4466 		scmd->satacmd_sec_count_msb = sec_count >> 8;
4467 #ifndef __lock_lint
4468 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
4469 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
4470 		scmd->satacmd_lba_high_msb = lba >> 40;
4471 #endif
4472 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
4473 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
4474 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
4475 	}
4476 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
4477 	scmd->satacmd_lba_low_lsb = lba & 0xff;
4478 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
4479 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
4480 	scmd->satacmd_features_reg = 0;
4481 	scmd->satacmd_status_reg = 0;
4482 	scmd->satacmd_error_reg = 0;
4483 
4484 	/*
4485 	 * Check if queueing commands should be used and switch
4486 	 * to appropriate command if possible
4487 	 */
4488 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
4489 		boolean_t using_queuing;
4490 
4491 		/* Queuing supported by controller and device? */
4492 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
4493 		    (sdinfo->satadrv_features_support &
4494 		    SATA_DEV_F_NCQ) &&
4495 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4496 		    SATA_CTLF_NCQ)) {
4497 			using_queuing = B_TRUE;
4498 
4499 			/* NCQ supported - use FPDMA READ */
4500 			scmd->satacmd_cmd_reg =
4501 			    SATAC_READ_FPDMA_QUEUED;
4502 			scmd->satacmd_features_reg_ext =
4503 			    scmd->satacmd_sec_count_msb;
4504 			scmd->satacmd_sec_count_msb = 0;
4505 		} else if ((sdinfo->satadrv_features_support &
4506 		    SATA_DEV_F_TCQ) &&
4507 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4508 		    SATA_CTLF_QCMD)) {
4509 			using_queuing = B_TRUE;
4510 
4511 			/* Legacy queueing */
4512 			if (sdinfo->satadrv_features_support &
4513 			    SATA_DEV_F_LBA48) {
4514 				scmd->satacmd_cmd_reg =
4515 				    SATAC_READ_DMA_QUEUED_EXT;
4516 				scmd->satacmd_features_reg_ext =
4517 				    scmd->satacmd_sec_count_msb;
4518 				scmd->satacmd_sec_count_msb = 0;
4519 			} else {
4520 				scmd->satacmd_cmd_reg =
4521 				    SATAC_READ_DMA_QUEUED;
4522 			}
4523 		} else	/* NCQ nor legacy queuing not supported */
4524 			using_queuing = B_FALSE;
4525 
4526 		/*
4527 		 * If queuing, the sector count goes in the features register
4528 		 * and the secount count will contain the tag.
4529 		 */
4530 		if (using_queuing) {
4531 			scmd->satacmd_features_reg =
4532 			    scmd->satacmd_sec_count_lsb;
4533 			scmd->satacmd_sec_count_lsb = 0;
4534 			scmd->satacmd_flags.sata_queued = B_TRUE;
4535 
4536 			/* Set-up maximum queue depth */
4537 			scmd->satacmd_flags.sata_max_queue_depth =
4538 			    sdinfo->satadrv_max_queue_depth - 1;
4539 		} else if (sdinfo->satadrv_features_enabled &
4540 		    SATA_DEV_F_E_UNTAGGED_QING) {
4541 			/*
4542 			 * Although NCQ/TCQ is not enabled, untagged queuing
4543 			 * may be still used.
4544 			 * Set-up the maximum untagged queue depth.
4545 			 * Use controller's queue depth from sata_hba_tran.
4546 			 * SATA HBA drivers may ignore this value and rely on
4547 			 * the internal limits.For drivers that do not
4548 			 * ignore untaged queue depth, limit the value to
4549 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
4550 			 * largest value that can be passed via
4551 			 * satacmd_flags.sata_max_queue_depth.
4552 			 */
4553 			scmd->satacmd_flags.sata_max_queue_depth =
4554 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
4555 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
4556 
4557 		} else {
4558 			scmd->satacmd_flags.sata_max_queue_depth = 0;
4559 		}
4560 	} else
4561 		scmd->satacmd_flags.sata_max_queue_depth = 0;
4562 
4563 	SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst,
4564 	    "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n",
4565 	    scmd->satacmd_cmd_reg, lba, sec_count);
4566 
4567 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4568 		/* Need callback function */
4569 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
4570 		synch = FALSE;
4571 	} else
4572 		synch = TRUE;
4573 
4574 	/* Transfer command to HBA */
4575 	if (sata_hba_start(spx, &rval) != 0) {
4576 		/* Pkt not accepted for execution */
4577 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4578 		return (rval);
4579 	}
4580 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4581 	/*
4582 	 * If execution is non-synchronous,
4583 	 * a callback function will handle potential errors, translate
4584 	 * the response and will do a callback to a target driver.
4585 	 * If it was synchronous, check execution status using the same
4586 	 * framework callback.
4587 	 */
4588 	if (synch) {
4589 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4590 		    "synchronous execution status %x\n",
4591 		    spx->txlt_sata_pkt->satapkt_reason);
4592 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
4593 	}
4594 	return (TRAN_ACCEPT);
4595 }
4596 
4597 
4598 /*
4599  * SATA translate command: Write (various types)
4600  * Translated into appropriate type of ATA WRITE command
4601  * for SATA hard disks.
4602  * Both the device capabilities and requested operation mode are
4603  * considered.
4604  *
4605  * Following scsi cdb fields are ignored:
4606  * rwprotect, dpo, fua, fua_nv, group_number.
4607  *
4608  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
4609  * enable variable sata_func_enable), the capability of the controller and
4610  * capability of a device are checked and if both support queueing, write
4611  * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT
4612  * command rather than plain WRITE_XXX command.
4613  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
4614  * both the controller and device suport such functionality, the write
4615  * request will be translated to WRITE_FPDMA_QUEUED command.
4616  * In both cases the maximum queue depth is derived as minimum of:
4617  * HBA capability,device capability and sata_max_queue_depth variable setting.
4618  * The value passed to HBA driver is decremented by 1, because only 5 bits are
4619  * used to pass max queue depth value, and the maximum possible queue depth
4620  * is 32.
4621  *
4622  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4623  * appropriate values in scsi_pkt fields.
4624  */
4625 static int
4626 sata_txlt_write(sata_pkt_txlate_t *spx)
4627 {
4628 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4629 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4630 	sata_drive_info_t *sdinfo;
4631 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4632 	int cport = SATA_TXLT_CPORT(spx);
4633 	uint16_t sec_count;
4634 	uint64_t lba;
4635 	int rval;
4636 	int synch;
4637 
4638 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4639 
4640 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
4641 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
4642 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4643 		return (rval);
4644 	}
4645 
4646 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4647 	    &spx->txlt_sata_pkt->satapkt_device);
4648 
4649 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4650 	/*
4651 	 * Extract LBA and sector count from scsi CDB
4652 	 */
4653 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
4654 	case SCMD_WRITE:
4655 		/* 6-byte scsi read cmd : 0x0A */
4656 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
4657 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
4658 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4659 		sec_count = scsipkt->pkt_cdbp[4];
4660 		/* sec_count 0 will be interpreted as 256 by a device */
4661 		break;
4662 	case SCMD_WRITE_G1:
4663 		/* 10-bytes scsi write command : 0x2A */
4664 		lba = scsipkt->pkt_cdbp[2];
4665 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4666 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4667 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4668 		sec_count = scsipkt->pkt_cdbp[7];
4669 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4670 		break;
4671 	case SCMD_WRITE_G5:
4672 		/* 12-bytes scsi read command : 0xAA */
4673 		lba = scsipkt->pkt_cdbp[2];
4674 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4675 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4676 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4677 		sec_count = scsipkt->pkt_cdbp[6];
4678 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
4679 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4680 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
4681 		break;
4682 	case SCMD_WRITE_G4:
4683 		/* 16-bytes scsi write command : 0x8A */
4684 		lba = scsipkt->pkt_cdbp[2];
4685 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4686 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4687 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4688 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
4689 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
4690 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
4691 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
4692 		sec_count = scsipkt->pkt_cdbp[10];
4693 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
4694 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
4695 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
4696 		break;
4697 	default:
4698 		/* Unsupported command */
4699 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4700 		return (sata_txlt_invalid_command(spx));
4701 	}
4702 
4703 	/*
4704 	 * Check if specified address and length exceeds device capacity
4705 	 */
4706 	if ((lba >= sdinfo->satadrv_capacity) ||
4707 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
4708 		/* LBA out of range */
4709 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4710 		return (sata_txlt_lba_out_of_range(spx));
4711 	}
4712 
4713 	/*
4714 	 * For zero-length transfer, emulate good completion of the command
4715 	 * (reasons for rejecting the command were already checked).
4716 	 * No DMA resources were allocated.
4717 	 */
4718 	if (spx->txlt_dma_cookie_list == NULL) {
4719 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4720 		return (sata_emul_rw_completion(spx));
4721 	}
4722 
4723 	/*
4724 	 * Build cmd block depending on the device capability and
4725 	 * requested operation mode.
4726 	 * Do not bother with non-dma mode- we are working only with
4727 	 * devices supporting DMA.
4728 	 */
4729 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
4730 	scmd->satacmd_device_reg = SATA_ADH_LBA;
4731 	scmd->satacmd_cmd_reg = SATAC_WRITE_DMA;
4732 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
4733 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4734 		scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT;
4735 		scmd->satacmd_sec_count_msb = sec_count >> 8;
4736 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
4737 #ifndef __lock_lint
4738 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
4739 		scmd->satacmd_lba_high_msb = lba >> 40;
4740 #endif
4741 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
4742 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
4743 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
4744 	}
4745 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
4746 	scmd->satacmd_lba_low_lsb = lba & 0xff;
4747 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
4748 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
4749 	scmd->satacmd_features_reg = 0;
4750 	scmd->satacmd_status_reg = 0;
4751 	scmd->satacmd_error_reg = 0;
4752 
4753 	/*
4754 	 * Check if queueing commands should be used and switch
4755 	 * to appropriate command if possible
4756 	 */
4757 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
4758 		boolean_t using_queuing;
4759 
4760 		/* Queuing supported by controller and device? */
4761 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
4762 		    (sdinfo->satadrv_features_support &
4763 		    SATA_DEV_F_NCQ) &&
4764 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4765 		    SATA_CTLF_NCQ)) {
4766 			using_queuing = B_TRUE;
4767 
4768 			/* NCQ supported - use FPDMA WRITE */
4769 			scmd->satacmd_cmd_reg =
4770 			    SATAC_WRITE_FPDMA_QUEUED;
4771 			scmd->satacmd_features_reg_ext =
4772 			    scmd->satacmd_sec_count_msb;
4773 			scmd->satacmd_sec_count_msb = 0;
4774 		} else if ((sdinfo->satadrv_features_support &
4775 		    SATA_DEV_F_TCQ) &&
4776 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4777 		    SATA_CTLF_QCMD)) {
4778 			using_queuing = B_TRUE;
4779 
4780 			/* Legacy queueing */
4781 			if (sdinfo->satadrv_features_support &
4782 			    SATA_DEV_F_LBA48) {
4783 				scmd->satacmd_cmd_reg =
4784 				    SATAC_WRITE_DMA_QUEUED_EXT;
4785 				scmd->satacmd_features_reg_ext =
4786 				    scmd->satacmd_sec_count_msb;
4787 				scmd->satacmd_sec_count_msb = 0;
4788 			} else {
4789 				scmd->satacmd_cmd_reg =
4790 				    SATAC_WRITE_DMA_QUEUED;
4791 			}
4792 		} else	/*  NCQ nor legacy queuing not supported */
4793 			using_queuing = B_FALSE;
4794 
4795 		if (using_queuing) {
4796 			scmd->satacmd_features_reg =
4797 			    scmd->satacmd_sec_count_lsb;
4798 			scmd->satacmd_sec_count_lsb = 0;
4799 			scmd->satacmd_flags.sata_queued = B_TRUE;
4800 			/* Set-up maximum queue depth */
4801 			scmd->satacmd_flags.sata_max_queue_depth =
4802 			    sdinfo->satadrv_max_queue_depth - 1;
4803 		} else if (sdinfo->satadrv_features_enabled &
4804 		    SATA_DEV_F_E_UNTAGGED_QING) {
4805 			/*
4806 			 * Although NCQ/TCQ is not enabled, untagged queuing
4807 			 * may be still used.
4808 			 * Set-up the maximum untagged queue depth.
4809 			 * Use controller's queue depth from sata_hba_tran.
4810 			 * SATA HBA drivers may ignore this value and rely on
4811 			 * the internal limits. For drivera that do not
4812 			 * ignore untaged queue depth, limit the value to
4813 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
4814 			 * largest value that can be passed via
4815 			 * satacmd_flags.sata_max_queue_depth.
4816 			 */
4817 			scmd->satacmd_flags.sata_max_queue_depth =
4818 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
4819 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
4820 
4821 		} else {
4822 			scmd->satacmd_flags.sata_max_queue_depth = 0;
4823 		}
4824 	} else
4825 		scmd->satacmd_flags.sata_max_queue_depth = 0;
4826 
4827 	SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4828 	    "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n",
4829 	    scmd->satacmd_cmd_reg, lba, sec_count);
4830 
4831 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4832 		/* Need callback function */
4833 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
4834 		synch = FALSE;
4835 	} else
4836 		synch = TRUE;
4837 
4838 	/* Transfer command to HBA */
4839 	if (sata_hba_start(spx, &rval) != 0) {
4840 		/* Pkt not accepted for execution */
4841 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4842 		return (rval);
4843 	}
4844 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4845 
4846 	/*
4847 	 * If execution is non-synchronous,
4848 	 * a callback function will handle potential errors, translate
4849 	 * the response and will do a callback to a target driver.
4850 	 * If it was synchronous, check execution status using the same
4851 	 * framework callback.
4852 	 */
4853 	if (synch) {
4854 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4855 		    "synchronous execution status %x\n",
4856 		    spx->txlt_sata_pkt->satapkt_reason);
4857 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
4858 	}
4859 	return (TRAN_ACCEPT);
4860 }
4861 
4862 
4863 /*
4864  * Implements SCSI SBC WRITE BUFFER command download microcode option
4865  */
4866 static int
4867 sata_txlt_write_buffer(sata_pkt_txlate_t *spx)
4868 {
4869 #define	WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE			4
4870 #define	WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE			5
4871 
4872 	sata_hba_inst_t *sata_hba_inst = SATA_TXLT_HBA_INST(spx);
4873 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4874 	struct sata_pkt *sata_pkt = spx->txlt_sata_pkt;
4875 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4876 
4877 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4878 	struct scsi_extended_sense *sense;
4879 	int rval, mode, sector_count;
4880 	int cport = SATA_TXLT_CPORT(spx);
4881 
4882 	mode = scsipkt->pkt_cdbp[1] & 0x1f;
4883 
4884 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4885 	    "sata_txlt_write_buffer, mode 0x%x\n", mode);
4886 
4887 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4888 
4889 	if ((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) {
4890 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4891 		return (rval);
4892 	}
4893 
4894 	/* Use synchronous mode */
4895 	spx->txlt_sata_pkt->satapkt_op_mode
4896 	    |= SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
4897 
4898 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4899 
4900 	scsipkt->pkt_reason = CMD_CMPLT;
4901 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4902 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4903 
4904 	/*
4905 	 * The SCSI to ATA translation specification only calls
4906 	 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE.
4907 	 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but
4908 	 * ATA 8 (draft) got rid of download microcode for temp
4909 	 * and it is even optional for ATA 7, so it may be aborted.
4910 	 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as
4911 	 * it is not specified and the buffer offset for SCSI is a 16-bit
4912 	 * value in bytes, but for ATA it is a 16-bit offset in 512 byte
4913 	 * sectors.  Thus the offset really doesn't buy us anything.
4914 	 * If and when ATA 8 is stabilized and the SCSI to ATA specification
4915 	 * is revised, this can be revisisted.
4916 	 */
4917 	/* Reject not supported request */
4918 	switch (mode) {
4919 	case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE:
4920 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP;
4921 		break;
4922 	case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE:
4923 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE;
4924 		break;
4925 	default:
4926 		goto bad_param;
4927 	}
4928 
4929 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
4930 
4931 	scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE;
4932 	if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0)
4933 		goto bad_param;
4934 	sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE;
4935 	scmd->satacmd_sec_count_lsb = (uint8_t)sector_count;
4936 	scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8;
4937 	scmd->satacmd_lba_mid_lsb = 0;
4938 	scmd->satacmd_lba_high_lsb = 0;
4939 	scmd->satacmd_device_reg = 0;
4940 	spx->txlt_sata_pkt->satapkt_comp = NULL;
4941 	scmd->satacmd_addr_type = 0;
4942 
4943 	/* Transfer command to HBA */
4944 	if (sata_hba_start(spx, &rval) != 0) {
4945 		/* Pkt not accepted for execution */
4946 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
4947 		return (rval);
4948 	}
4949 
4950 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst, cport));
4951 
4952 	/* Then we need synchronous check the status of the disk */
4953 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4954 	    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
4955 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
4956 		scsipkt->pkt_reason = CMD_CMPLT;
4957 
4958 		/* Download commmand succeed, so probe and identify device */
4959 		sata_reidentify_device(spx);
4960 	} else {
4961 		/* Something went wrong, microcode download command failed */
4962 		scsipkt->pkt_reason = CMD_INCOMPLETE;
4963 		*scsipkt->pkt_scbp = STATUS_CHECK;
4964 		sense = sata_arq_sense(spx);
4965 		switch (sata_pkt->satapkt_reason) {
4966 		case SATA_PKT_PORT_ERROR:
4967 			/*
4968 			 * We have no device data. Assume no data transfered.
4969 			 */
4970 			sense->es_key = KEY_HARDWARE_ERROR;
4971 			break;
4972 
4973 		case SATA_PKT_DEV_ERROR:
4974 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
4975 			    SATA_STATUS_ERR) {
4976 				/*
4977 				 * determine dev error reason from error
4978 				 * reg content
4979 				 */
4980 				sata_decode_device_error(spx, sense);
4981 				break;
4982 			}
4983 			/* No extended sense key - no info available */
4984 			break;
4985 
4986 		case SATA_PKT_TIMEOUT:
4987 			scsipkt->pkt_reason = CMD_TIMEOUT;
4988 			scsipkt->pkt_statistics |=
4989 			    STAT_TIMEOUT | STAT_DEV_RESET;
4990 			/* No extended sense key ? */
4991 			break;
4992 
4993 		case SATA_PKT_ABORTED:
4994 			scsipkt->pkt_reason = CMD_ABORTED;
4995 			scsipkt->pkt_statistics |= STAT_ABORTED;
4996 			/* No extended sense key ? */
4997 			break;
4998 
4999 		case SATA_PKT_RESET:
5000 			/* pkt aborted by an explicit reset from a host */
5001 			scsipkt->pkt_reason = CMD_RESET;
5002 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5003 			break;
5004 
5005 		default:
5006 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5007 			    "sata_txlt_nodata_cmd_completion: "
5008 			    "invalid packet completion reason %d",
5009 			    sata_pkt->satapkt_reason));
5010 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5011 			break;
5012 		}
5013 
5014 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5015 		    "scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5016 
5017 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5018 		    scsipkt->pkt_comp != NULL)
5019 			/* scsi callback required */
5020 			(*scsipkt->pkt_comp)(scsipkt);
5021 	}
5022 	return (TRAN_ACCEPT);
5023 
5024 bad_param:
5025 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5026 	*scsipkt->pkt_scbp = STATUS_CHECK;
5027 	sense = sata_arq_sense(spx);
5028 	sense->es_key = KEY_ILLEGAL_REQUEST;
5029 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
5030 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5031 	    scsipkt->pkt_comp != NULL) {
5032 		/* scsi callback required */
5033 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5034 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5035 		    TQ_SLEEP) == 0) {
5036 			/* Scheduling the callback failed */
5037 			rval = TRAN_BUSY;
5038 		}
5039 	}
5040 	return (rval);
5041 }
5042 
5043 /*
5044  * Re-identify device after doing a firmware download.
5045  */
5046 static void
5047 sata_reidentify_device(sata_pkt_txlate_t *spx)
5048 {
5049 #define	DOWNLOAD_WAIT_TIME_SECS	60
5050 #define	DOWNLOAD_WAIT_INTERVAL_SECS	1
5051 	int rval;
5052 	int retry_cnt;
5053 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5054 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
5055 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
5056 	sata_drive_info_t *sdinfo;
5057 
5058 	/*
5059 	 * Before returning good status, probe device.
5060 	 * Device probing will get IDENTIFY DEVICE data, if possible.
5061 	 * The assumption is that the new microcode is applied by the
5062 	 * device. It is a caller responsibility to verify this.
5063 	 */
5064 	for (retry_cnt = 0;
5065 	    retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS;
5066 	    retry_cnt++) {
5067 		rval = sata_probe_device(sata_hba_inst, &sata_device);
5068 
5069 		if (rval == SATA_SUCCESS) { /* Set default features */
5070 			sdinfo = sata_get_device_info(sata_hba_inst,
5071 			    &sata_device);
5072 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
5073 			    SATA_SUCCESS) {
5074 				/* retry */
5075 				(void) sata_initialize_device(sata_hba_inst,
5076 				    sdinfo);
5077 			}
5078 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5079 			    scsipkt->pkt_comp != NULL)
5080 				(*scsipkt->pkt_comp)(scsipkt);
5081 			return;
5082 		} else if (rval == SATA_RETRY) {
5083 			delay(drv_usectohz(1000000 *
5084 			    DOWNLOAD_WAIT_INTERVAL_SECS));
5085 			continue;
5086 		} else	/* failed - no reason to retry */
5087 			break;
5088 	}
5089 
5090 	/*
5091 	 * Something went wrong, device probing failed.
5092 	 */
5093 	SATA_LOG_D((sata_hba_inst, CE_WARN,
5094 	    "Cannot probe device after downloading microcode\n"));
5095 
5096 	/* Reset device to force retrying the probe. */
5097 	(void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst))
5098 	    (SATA_DIP(sata_hba_inst), &sata_device);
5099 
5100 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5101 	    scsipkt->pkt_comp != NULL)
5102 		(*scsipkt->pkt_comp)(scsipkt);
5103 }
5104 
5105 
5106 /*
5107  * Translate command: Synchronize Cache.
5108  * Translates into Flush Cache command for SATA hard disks.
5109  *
5110  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
5111  * appropriate values in scsi_pkt fields.
5112  */
5113 static 	int
5114 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx)
5115 {
5116 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5117 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
5118 	int cport = SATA_TXLT_CPORT(spx);
5119 	int rval;
5120 	int synch;
5121 
5122 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5123 
5124 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
5125 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
5126 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5127 		return (rval);
5128 	}
5129 
5130 	scmd->satacmd_addr_type = 0;
5131 	scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE;
5132 	scmd->satacmd_device_reg = 0;
5133 	scmd->satacmd_sec_count_lsb = 0;
5134 	scmd->satacmd_lba_low_lsb = 0;
5135 	scmd->satacmd_lba_mid_lsb = 0;
5136 	scmd->satacmd_lba_high_lsb = 0;
5137 	scmd->satacmd_features_reg = 0;
5138 	scmd->satacmd_status_reg = 0;
5139 	scmd->satacmd_error_reg = 0;
5140 
5141 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5142 	    "sata_txlt_synchronize_cache\n", NULL);
5143 
5144 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5145 		/* Need to set-up a callback function */
5146 		spx->txlt_sata_pkt->satapkt_comp =
5147 		    sata_txlt_nodata_cmd_completion;
5148 		synch = FALSE;
5149 	} else
5150 		synch = TRUE;
5151 
5152 	/* Transfer command to HBA */
5153 	if (sata_hba_start(spx, &rval) != 0) {
5154 		/* Pkt not accepted for execution */
5155 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5156 		return (rval);
5157 	}
5158 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5159 
5160 	/*
5161 	 * If execution non-synchronous, it had to be completed
5162 	 * a callback function will handle potential errors, translate
5163 	 * the response and will do a callback to a target driver.
5164 	 * If it was synchronous, check status, using the same
5165 	 * framework callback.
5166 	 */
5167 	if (synch) {
5168 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5169 		    "synchronous execution status %x\n",
5170 		    spx->txlt_sata_pkt->satapkt_reason);
5171 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
5172 	}
5173 	return (TRAN_ACCEPT);
5174 }
5175 
5176 
5177 /*
5178  * Send pkt to SATA HBA driver
5179  *
5180  * This function may be called only if the operation is requested by scsi_pkt,
5181  * i.e. scsi_pkt is not NULL.
5182  *
5183  * This function has to be called with cport mutex held. It does release
5184  * the mutex when it calls HBA driver sata_tran_start function and
5185  * re-acquires it afterwards.
5186  *
5187  * If return value is 0, pkt was accepted, -1 otherwise
5188  * rval is set to appropriate sata_scsi_start return value.
5189  *
5190  * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not
5191  * have called the sata_pkt callback function for this packet.
5192  *
5193  * The scsi callback has to be performed by the caller of this routine.
5194  *
5195  * Note 2: No port multiplier support for now.
5196  */
5197 static int
5198 sata_hba_start(sata_pkt_txlate_t *spx, int *rval)
5199 {
5200 	int stat, cport;
5201 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
5202 	sata_drive_info_t *sdinfo;
5203 	sata_device_t *sata_device;
5204 	uint8_t cmd;
5205 	struct sata_cmd_flags cmd_flags;
5206 
5207 	ASSERT(spx->txlt_sata_pkt != NULL);
5208 
5209 	cport = SATA_TXLT_CPORT(spx);
5210 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5211 
5212 	sdinfo = sata_get_device_info(sata_hba_inst,
5213 	    &spx->txlt_sata_pkt->satapkt_device);
5214 	ASSERT(sdinfo != NULL);
5215 
5216 	/* Clear device reset state? */
5217 	if (sdinfo->satadrv_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) {
5218 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
5219 		    sata_clear_dev_reset = B_TRUE;
5220 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_CLEAR_DEVICE_RESET;
5221 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5222 		    "sata_hba_start: clearing device reset state\n", NULL);
5223 	}
5224 	cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg;
5225 	cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags;
5226 	sata_device = &spx->txlt_sata_pkt->satapkt_device;
5227 
5228 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5229 
5230 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5231 	    "Sata cmd 0x%2x\n", cmd);
5232 
5233 	stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
5234 	    spx->txlt_sata_pkt);
5235 
5236 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5237 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
5238 	/*
5239 	 * If sata pkt was accepted and executed in asynchronous mode, i.e.
5240 	 * with the sata callback, the sata_pkt could be already destroyed
5241 	 * by the time we check ther return status from the hba_start()
5242 	 * function, because sata_scsi_destroy_pkt() could have been already
5243 	 * called (perhaps in the interrupt context). So, in such case, there
5244 	 * should be no references to it. In other cases, sata_pkt still
5245 	 * exists.
5246 	 */
5247 	switch (stat) {
5248 	case SATA_TRAN_ACCEPTED:
5249 		/*
5250 		 * pkt accepted for execution.
5251 		 * If it was executed synchronously, it is already completed
5252 		 * and pkt completion_reason indicates completion status.
5253 		 */
5254 		*rval = TRAN_ACCEPT;
5255 		return (0);
5256 
5257 	case SATA_TRAN_QUEUE_FULL:
5258 		/*
5259 		 * Controller detected queue full condition.
5260 		 */
5261 		SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst,
5262 		    "sata_hba_start: queue full\n", NULL);
5263 
5264 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
5265 		*spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL;
5266 
5267 		*rval = TRAN_BUSY;
5268 		break;
5269 
5270 	case SATA_TRAN_PORT_ERROR:
5271 		/*
5272 		 * Communication/link with device or general port error
5273 		 * detected before pkt execution begun.
5274 		 */
5275 		if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
5276 		    SATA_ADDR_CPORT ||
5277 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
5278 		    SATA_ADDR_DCPORT)
5279 			sata_log(sata_hba_inst, CE_CONT,
5280 			    "SATA port %d error",
5281 			    sata_device->satadev_addr.cport);
5282 		else
5283 			sata_log(sata_hba_inst, CE_CONT,
5284 			    "SATA port %d pmport %d error\n",
5285 			    sata_device->satadev_addr.cport,
5286 			    sata_device->satadev_addr.pmport);
5287 
5288 		/*
5289 		 * Update the port/device structure.
5290 		 * sata_pkt should be still valid. Since port error is
5291 		 * returned, sata_device content should reflect port
5292 		 * state - it means, that sata address have been changed,
5293 		 * because original packet's sata address refered to a device
5294 		 * attached to some port.
5295 		 */
5296 		sata_update_port_info(sata_hba_inst, sata_device);
5297 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
5298 		*rval = TRAN_FATAL_ERROR;
5299 		break;
5300 
5301 	case SATA_TRAN_CMD_UNSUPPORTED:
5302 		/*
5303 		 * Command rejected by HBA as unsupported. It was HBA driver
5304 		 * that rejected the command, command was not sent to
5305 		 * an attached device.
5306 		 */
5307 		if ((sdinfo != NULL) &&
5308 		    (sdinfo->satadrv_state & SATA_DSTATE_RESET))
5309 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5310 			    "sat_hba_start: cmd 0x%2x rejected "
5311 			    "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd);
5312 
5313 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5314 		(void) sata_txlt_invalid_command(spx);
5315 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5316 
5317 		*rval = TRAN_ACCEPT;
5318 		break;
5319 
5320 	case SATA_TRAN_BUSY:
5321 		/*
5322 		 * Command rejected by HBA because other operation prevents
5323 		 * accepting the packet, or device is in RESET condition.
5324 		 */
5325 		if (sdinfo != NULL) {
5326 			sdinfo->satadrv_state =
5327 			    spx->txlt_sata_pkt->satapkt_device.satadev_state;
5328 
5329 			if (sdinfo->satadrv_state & SATA_DSTATE_RESET) {
5330 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5331 				    "sata_hba_start: cmd 0x%2x rejected "
5332 				    "because of device reset condition\n",
5333 				    cmd);
5334 			} else {
5335 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5336 				    "sata_hba_start: cmd 0x%2x rejected "
5337 				    "with SATA_TRAN_BUSY status\n",
5338 				    cmd);
5339 			}
5340 		}
5341 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
5342 		*rval = TRAN_BUSY;
5343 		break;
5344 
5345 	default:
5346 		/* Unrecognized HBA response */
5347 		SATA_LOG_D((sata_hba_inst, CE_WARN,
5348 		    "sata_hba_start: unrecognized HBA response "
5349 		    "to cmd : 0x%2x resp 0x%x", cmd, rval));
5350 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
5351 		*rval = TRAN_FATAL_ERROR;
5352 		break;
5353 	}
5354 
5355 	/*
5356 	 * If we got here, the packet was rejected.
5357 	 * Check if we need to remember reset state clearing request
5358 	 */
5359 	if (cmd_flags.sata_clear_dev_reset) {
5360 		/*
5361 		 * Check if device is still configured - it may have
5362 		 * disapeared from the configuration
5363 		 */
5364 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
5365 		if (sdinfo != NULL) {
5366 			/*
5367 			 * Restore the flag that requests clearing of
5368 			 * the device reset state,
5369 			 * so the next sata packet may carry it to HBA.
5370 			 */
5371 			sdinfo->satadrv_event_flags |=
5372 			    SATA_EVNT_CLEAR_DEVICE_RESET;
5373 		}
5374 	}
5375 	return (-1);
5376 }
5377 
5378 /*
5379  * Scsi response setup for invalid LBA
5380  *
5381  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
5382  */
5383 static int
5384 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx)
5385 {
5386 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5387 	struct scsi_extended_sense *sense;
5388 
5389 	scsipkt->pkt_reason = CMD_CMPLT;
5390 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5391 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5392 	*scsipkt->pkt_scbp = STATUS_CHECK;
5393 
5394 	*scsipkt->pkt_scbp = STATUS_CHECK;
5395 	sense = sata_arq_sense(spx);
5396 	sense->es_key = KEY_ILLEGAL_REQUEST;
5397 	sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
5398 
5399 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5400 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5401 
5402 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5403 	    scsipkt->pkt_comp != NULL)
5404 		/* scsi callback required */
5405 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5406 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5407 		    TQ_SLEEP) == NULL)
5408 			/* Scheduling the callback failed */
5409 			return (TRAN_BUSY);
5410 	return (TRAN_ACCEPT);
5411 }
5412 
5413 
5414 /*
5415  * Analyze device status and error registers and translate them into
5416  * appropriate scsi sense codes.
5417  * NOTE: non-packet commands only for now
5418  */
5419 static void
5420 sata_decode_device_error(sata_pkt_txlate_t *spx,
5421     struct scsi_extended_sense *sense)
5422 {
5423 	uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg;
5424 
5425 	ASSERT(sense != NULL);
5426 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
5427 	    SATA_STATUS_ERR);
5428 
5429 
5430 	if (err_reg & SATA_ERROR_ICRC) {
5431 		sense->es_key = KEY_ABORTED_COMMAND;
5432 		sense->es_add_code = 0x08; /* Communication failure */
5433 		return;
5434 	}
5435 
5436 	if (err_reg & SATA_ERROR_UNC) {
5437 		sense->es_key = KEY_MEDIUM_ERROR;
5438 		/* Information bytes (LBA) need to be set by a caller */
5439 		return;
5440 	}
5441 
5442 	/* ADD HERE: MC error bit handling for ATAPI CD/DVD */
5443 	if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) {
5444 		sense->es_key = KEY_UNIT_ATTENTION;
5445 		sense->es_add_code = 0x3a; /* No media present */
5446 		return;
5447 	}
5448 
5449 	if (err_reg & SATA_ERROR_IDNF) {
5450 		if (err_reg & SATA_ERROR_ABORT) {
5451 			sense->es_key = KEY_ABORTED_COMMAND;
5452 		} else {
5453 			sense->es_key = KEY_ILLEGAL_REQUEST;
5454 			sense->es_add_code = 0x21; /* LBA out of range */
5455 		}
5456 		return;
5457 	}
5458 
5459 	if (err_reg & SATA_ERROR_ABORT) {
5460 		ASSERT(spx->txlt_sata_pkt != NULL);
5461 		sense->es_key = KEY_ABORTED_COMMAND;
5462 		return;
5463 	}
5464 }
5465 
5466 /*
5467  * Extract error LBA from sata_pkt.satapkt_cmd register fields
5468  */
5469 static void
5470 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba)
5471 {
5472 	sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd;
5473 
5474 	*lba = 0;
5475 	if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) {
5476 		*lba = sata_cmd->satacmd_lba_high_msb;
5477 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb;
5478 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb;
5479 	} else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) {
5480 		*lba = sata_cmd->satacmd_device_reg & 0xf;
5481 	}
5482 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb;
5483 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb;
5484 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb;
5485 }
5486 
5487 /*
5488  * This is fixed sense format - if LBA exceeds the info field size,
5489  * no valid info will be returned (valid bit in extended sense will
5490  * be set to 0).
5491  */
5492 static struct scsi_extended_sense *
5493 sata_arq_sense(sata_pkt_txlate_t *spx)
5494 {
5495 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5496 	struct scsi_arq_status *arqs;
5497 	struct scsi_extended_sense *sense;
5498 
5499 	/* Fill ARQ sense data */
5500 	scsipkt->pkt_state |= STATE_ARQ_DONE;
5501 	arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp;
5502 	*(uchar_t *)&arqs->sts_status = STATUS_CHECK;
5503 	*(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD;
5504 	arqs->sts_rqpkt_reason = CMD_CMPLT;
5505 	arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5506 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
5507 	arqs->sts_rqpkt_resid = 0;
5508 	sense = &arqs->sts_sensedata;
5509 	bzero(sense, sizeof (struct scsi_extended_sense));
5510 	sata_fixed_sense_data_preset(sense);
5511 	return (sense);
5512 }
5513 
5514 
5515 /*
5516  * Emulated SATA Read/Write command completion for zero-length requests.
5517  * This request always succedes, so in synchronous mode it always returns
5518  * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the
5519  * callback cannot be scheduled.
5520  */
5521 static int
5522 sata_emul_rw_completion(sata_pkt_txlate_t *spx)
5523 {
5524 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5525 
5526 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5527 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5528 	scsipkt->pkt_reason = CMD_CMPLT;
5529 	*scsipkt->pkt_scbp = STATUS_GOOD;
5530 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5531 		/* scsi callback required - have to schedule it */
5532 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5533 		    (task_func_t *)scsipkt->pkt_comp,
5534 		    (void *)scsipkt, TQ_SLEEP) == NULL)
5535 			/* Scheduling the callback failed */
5536 			return (TRAN_BUSY);
5537 	}
5538 	return (TRAN_ACCEPT);
5539 }
5540 
5541 
5542 /*
5543  * Translate completion status of SATA read/write commands into scsi response.
5544  * pkt completion_reason is checked to determine the completion status.
5545  * Do scsi callback if necessary.
5546  *
5547  * Note: this function may be called also for synchronously executed
5548  * commands.
5549  * This function may be used only if scsi_pkt is non-NULL.
5550  */
5551 static void
5552 sata_txlt_rw_completion(sata_pkt_t *sata_pkt)
5553 {
5554 	sata_pkt_txlate_t *spx =
5555 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
5556 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
5557 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5558 	struct scsi_extended_sense *sense;
5559 	uint64_t lba;
5560 	struct buf *bp;
5561 	int rval;
5562 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5563 		/* Normal completion */
5564 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5565 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
5566 		scsipkt->pkt_reason = CMD_CMPLT;
5567 		*scsipkt->pkt_scbp = STATUS_GOOD;
5568 		if (spx->txlt_tmp_buf != NULL) {
5569 			/* Temporary buffer was used */
5570 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5571 			if (bp->b_flags & B_READ) {
5572 				rval = ddi_dma_sync(
5573 				    spx->txlt_buf_dma_handle, 0, 0,
5574 				    DDI_DMA_SYNC_FORCPU);
5575 				ASSERT(rval == DDI_SUCCESS);
5576 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
5577 				    bp->b_bcount);
5578 			}
5579 		}
5580 	} else {
5581 		/*
5582 		 * Something went wrong - analyze return
5583 		 */
5584 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5585 		    STATE_SENT_CMD | STATE_GOT_STATUS;
5586 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5587 		*scsipkt->pkt_scbp = STATUS_CHECK;
5588 		sense = sata_arq_sense(spx);
5589 		ASSERT(sense != NULL);
5590 
5591 		/*
5592 		 * SATA_PKT_DEV_ERROR is the only case where we may be able to
5593 		 * extract from device registers the failing LBA.
5594 		 */
5595 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
5596 			if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) &&
5597 			    (scmd->satacmd_lba_mid_msb != 0 ||
5598 			    scmd->satacmd_lba_high_msb != 0)) {
5599 				/*
5600 				 * We have problem reporting this cmd LBA
5601 				 * in fixed sense data format, because of
5602 				 * the size of the scsi LBA fields.
5603 				 */
5604 				sense->es_valid = 0;
5605 			} else {
5606 				sata_extract_error_lba(spx, &lba);
5607 				sense->es_info_1 = (lba & 0xFF000000) >> 24;
5608 				sense->es_info_2 = (lba & 0xFF0000) >> 16;
5609 				sense->es_info_3 = (lba & 0xFF00) >> 8;
5610 				sense->es_info_4 = lba & 0xFF;
5611 			}
5612 		} else {
5613 			/* Invalid extended sense info */
5614 			sense->es_valid = 0;
5615 		}
5616 
5617 		switch (sata_pkt->satapkt_reason) {
5618 		case SATA_PKT_PORT_ERROR:
5619 			/* We may want to handle DEV GONE state as well */
5620 			/*
5621 			 * We have no device data. Assume no data transfered.
5622 			 */
5623 			sense->es_key = KEY_HARDWARE_ERROR;
5624 			break;
5625 
5626 		case SATA_PKT_DEV_ERROR:
5627 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5628 			    SATA_STATUS_ERR) {
5629 				/*
5630 				 * determine dev error reason from error
5631 				 * reg content
5632 				 */
5633 				sata_decode_device_error(spx, sense);
5634 				if (sense->es_key == KEY_MEDIUM_ERROR) {
5635 					switch (scmd->satacmd_cmd_reg) {
5636 					case SATAC_READ_DMA:
5637 					case SATAC_READ_DMA_EXT:
5638 					case SATAC_READ_DMA_QUEUED:
5639 					case SATAC_READ_DMA_QUEUED_EXT:
5640 					case SATAC_READ_FPDMA_QUEUED:
5641 						/* Unrecovered read error */
5642 						sense->es_add_code =
5643 						    SD_SCSI_ASC_UNREC_READ_ERR;
5644 						break;
5645 					case SATAC_WRITE_DMA:
5646 					case SATAC_WRITE_DMA_EXT:
5647 					case SATAC_WRITE_DMA_QUEUED:
5648 					case SATAC_WRITE_DMA_QUEUED_EXT:
5649 					case SATAC_WRITE_FPDMA_QUEUED:
5650 						/* Write error */
5651 						sense->es_add_code =
5652 						    SD_SCSI_ASC_WRITE_ERR;
5653 						break;
5654 					default:
5655 						/* Internal error */
5656 						SATA_LOG_D((
5657 						    spx->txlt_sata_hba_inst,
5658 						    CE_WARN,
5659 						    "sata_txlt_rw_completion :"
5660 						    "internal error - invalid "
5661 						    "command 0x%2x",
5662 						    scmd->satacmd_cmd_reg));
5663 						break;
5664 					}
5665 				}
5666 				break;
5667 			}
5668 			/* No extended sense key - no info available */
5669 			scsipkt->pkt_reason = CMD_INCOMPLETE;
5670 			break;
5671 
5672 		case SATA_PKT_TIMEOUT:
5673 			scsipkt->pkt_reason = CMD_TIMEOUT;
5674 			scsipkt->pkt_statistics |=
5675 			    STAT_TIMEOUT | STAT_DEV_RESET;
5676 			sense->es_key = KEY_ABORTED_COMMAND;
5677 			break;
5678 
5679 		case SATA_PKT_ABORTED:
5680 			scsipkt->pkt_reason = CMD_ABORTED;
5681 			scsipkt->pkt_statistics |= STAT_ABORTED;
5682 			sense->es_key = KEY_ABORTED_COMMAND;
5683 			break;
5684 
5685 		case SATA_PKT_RESET:
5686 			scsipkt->pkt_reason = CMD_RESET;
5687 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5688 			sense->es_key = KEY_ABORTED_COMMAND;
5689 			break;
5690 
5691 		default:
5692 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5693 			    "sata_txlt_rw_completion: "
5694 			    "invalid packet completion reason"));
5695 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5696 			break;
5697 		}
5698 	}
5699 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5700 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5701 
5702 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5703 	    scsipkt->pkt_comp != NULL)
5704 		/* scsi callback required */
5705 		(*scsipkt->pkt_comp)(scsipkt);
5706 }
5707 
5708 
5709 /*
5710  * Translate completion status of non-data commands (i.e. commands returning
5711  * no data).
5712  * pkt completion_reason is checked to determine the completion status.
5713  * Do scsi callback if necessary (FLAG_NOINTR == 0)
5714  *
5715  * Note: this function may be called also for synchronously executed
5716  * commands.
5717  * This function may be used only if scsi_pkt is non-NULL.
5718  */
5719 
5720 static 	void
5721 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt)
5722 {
5723 	sata_pkt_txlate_t *spx =
5724 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
5725 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5726 	struct scsi_extended_sense *sense;
5727 
5728 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5729 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5730 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5731 		/* Normal completion */
5732 		scsipkt->pkt_reason = CMD_CMPLT;
5733 		*scsipkt->pkt_scbp = STATUS_GOOD;
5734 	} else {
5735 		/* Something went wrong */
5736 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5737 		*scsipkt->pkt_scbp = STATUS_CHECK;
5738 		sense = sata_arq_sense(spx);
5739 		switch (sata_pkt->satapkt_reason) {
5740 		case SATA_PKT_PORT_ERROR:
5741 			/*
5742 			 * We have no device data. Assume no data transfered.
5743 			 */
5744 			sense->es_key = KEY_HARDWARE_ERROR;
5745 			break;
5746 
5747 		case SATA_PKT_DEV_ERROR:
5748 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5749 			    SATA_STATUS_ERR) {
5750 				/*
5751 				 * determine dev error reason from error
5752 				 * reg content
5753 				 */
5754 				sata_decode_device_error(spx, sense);
5755 				break;
5756 			}
5757 			/* No extended sense key - no info available */
5758 			break;
5759 
5760 		case SATA_PKT_TIMEOUT:
5761 			scsipkt->pkt_reason = CMD_TIMEOUT;
5762 			scsipkt->pkt_statistics |=
5763 			    STAT_TIMEOUT | STAT_DEV_RESET;
5764 			/* No extended sense key ? */
5765 			break;
5766 
5767 		case SATA_PKT_ABORTED:
5768 			scsipkt->pkt_reason = CMD_ABORTED;
5769 			scsipkt->pkt_statistics |= STAT_ABORTED;
5770 			/* No extended sense key ? */
5771 			break;
5772 
5773 		case SATA_PKT_RESET:
5774 			/* pkt aborted by an explicit reset from a host */
5775 			scsipkt->pkt_reason = CMD_RESET;
5776 			scsipkt->pkt_statistics |= STAT_DEV_RESET;
5777 			break;
5778 
5779 		default:
5780 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5781 			    "sata_txlt_nodata_cmd_completion: "
5782 			    "invalid packet completion reason %d",
5783 			    sata_pkt->satapkt_reason));
5784 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5785 			break;
5786 		}
5787 
5788 	}
5789 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5790 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5791 
5792 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5793 	    scsipkt->pkt_comp != NULL)
5794 		/* scsi callback required */
5795 		(*scsipkt->pkt_comp)(scsipkt);
5796 }
5797 
5798 
5799 /*
5800  * Build Mode sense R/W recovery page
5801  * NOT IMPLEMENTED
5802  */
5803 
5804 static int
5805 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5806 {
5807 #ifndef __lock_lint
5808 	_NOTE(ARGUNUSED(sdinfo))
5809 	_NOTE(ARGUNUSED(pcntrl))
5810 	_NOTE(ARGUNUSED(buf))
5811 #endif
5812 	return (0);
5813 }
5814 
5815 /*
5816  * Build Mode sense caching page  -  scsi-3 implementation.
5817  * Page length distinguishes previous format from scsi-3 format.
5818  * buf must have space for 0x12 bytes.
5819  * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable.
5820  *
5821  */
5822 static int
5823 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5824 {
5825 	struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf;
5826 	sata_id_t *sata_id = &sdinfo->satadrv_id;
5827 
5828 	/*
5829 	 * Most of the fields are set to 0, being not supported and/or disabled
5830 	 */
5831 	bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3);
5832 
5833 	/* Saved paramters not supported */
5834 	if (pcntrl == 3)
5835 		return (0);
5836 	if (pcntrl == 0 || pcntrl == 2) {
5837 		/*
5838 		 * For now treat current and default parameters as same
5839 		 * That may have to change, if target driver will complain
5840 		 */
5841 		page->mode_page.code = MODEPAGE_CACHING;	/* PS = 0 */
5842 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
5843 
5844 		if ((sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) &&
5845 		    !(sata_id->ai_features85 & SATA_LOOK_AHEAD)) {
5846 			page->dra = 1;		/* Read Ahead disabled */
5847 			page->rcd = 1;		/* Read Cache disabled */
5848 		}
5849 		if ((sata_id->ai_cmdset82 & SATA_WRITE_CACHE) &&
5850 		    (sata_id->ai_features85 & SATA_WRITE_CACHE))
5851 			page->wce = 1;		/* Write Cache enabled */
5852 	} else {
5853 		/* Changeable parameters */
5854 		page->mode_page.code = MODEPAGE_CACHING;
5855 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
5856 		if (sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) {
5857 			page->dra = 1;
5858 			page->rcd = 1;
5859 		}
5860 		if (sata_id->ai_cmdset82 & SATA_WRITE_CACHE)
5861 			page->wce = 1;
5862 	}
5863 	return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
5864 	    sizeof (struct mode_page));
5865 }
5866 
5867 /*
5868  * Build Mode sense exception cntrl page
5869  */
5870 static int
5871 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5872 {
5873 	struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf;
5874 	sata_id_t *sata_id = &sdinfo->satadrv_id;
5875 
5876 	/*
5877 	 * Most of the fields are set to 0, being not supported and/or disabled
5878 	 */
5879 	bzero(buf, PAGELENGTH_INFO_EXCPT);
5880 
5881 	page->mode_page.code = MODEPAGE_INFO_EXCPT;
5882 	page->mode_page.length = PAGELENGTH_INFO_EXCPT;
5883 
5884 	/* Indicate that this is page is saveable */
5885 	page->mode_page.ps = 1;
5886 
5887 	/*
5888 	 * We will return the same data for default, current and saved page.
5889 	 * The only changeable bit is dexcpt and that bit is required
5890 	 * by the ATA specification to be preserved across power cycles.
5891 	 */
5892 	if (pcntrl != 1) {
5893 		page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED);
5894 		page->mrie = MRIE_ONLY_ON_REQUEST;
5895 	}
5896 	else
5897 		page->dexcpt = 1;	/* Only changeable parameter */
5898 
5899 	return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_info_excpt_page));
5900 }
5901 
5902 
5903 static int
5904 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5905 {
5906 	struct mode_acoustic_management *page =
5907 	    (struct mode_acoustic_management *)buf;
5908 	sata_id_t *sata_id = &sdinfo->satadrv_id;
5909 
5910 	/*
5911 	 * Most of the fields are set to 0, being not supported and/or disabled
5912 	 */
5913 	bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT);
5914 
5915 	switch (pcntrl) {
5916 	case P_CNTRL_DEFAULT:
5917 		/*  default paramters not supported */
5918 		return (0);
5919 
5920 	case P_CNTRL_CURRENT:
5921 	case P_CNTRL_SAVED:
5922 		/* Saved and current are supported and are identical */
5923 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
5924 		page->mode_page.length =
5925 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
5926 		page->mode_page.ps = 1;
5927 
5928 		/* Word 83 indicates if feature is supported */
5929 		/* If feature is not supported */
5930 		if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) {
5931 			page->acoustic_manag_enable =
5932 			    ACOUSTIC_DISABLED;
5933 		} else {
5934 			page->acoustic_manag_enable =
5935 			    ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT)
5936 			    != 0);
5937 			/* Word 94 inidicates the value */
5938 #ifdef	_LITTLE_ENDIAN
5939 			page->acoustic_manag_level =
5940 			    (uchar_t)sata_id->ai_acoustic;
5941 			page->vendor_recommended_value =
5942 			    sata_id->ai_acoustic >> 8;
5943 #else
5944 			page->acoustic_manag_level =
5945 			    sata_id->ai_acoustic >> 8;
5946 			page->vendor_recommended_value =
5947 			    (uchar_t)sata_id->ai_acoustic;
5948 #endif
5949 		}
5950 		break;
5951 
5952 	case P_CNTRL_CHANGEABLE:
5953 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
5954 		page->mode_page.length =
5955 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
5956 		page->mode_page.ps = 1;
5957 
5958 		/* Word 83 indicates if the feature is supported */
5959 		if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) {
5960 			page->acoustic_manag_enable =
5961 			    ACOUSTIC_ENABLED;
5962 			page->acoustic_manag_level = 0xff;
5963 		}
5964 		break;
5965 	}
5966 	return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
5967 	    sizeof (struct mode_page));
5968 }
5969 
5970 
5971 /*
5972  * Build Mode sense power condition page
5973  * NOT IMPLEMENTED.
5974  */
5975 static int
5976 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5977 {
5978 #ifndef __lock_lint
5979 	_NOTE(ARGUNUSED(sdinfo))
5980 	_NOTE(ARGUNUSED(pcntrl))
5981 	_NOTE(ARGUNUSED(buf))
5982 #endif
5983 	return (0);
5984 }
5985 
5986 
5987 /*
5988  * Process mode select caching page 8 (scsi3 format only).
5989  * Read Ahead (same as read cache) and Write Cache may be turned on and off
5990  * if these features are supported by the device. If these features are not
5991  * supported, quietly ignore them.
5992  * This function fails only if the SET FEATURE command sent to
5993  * the device fails. The page format is not varified, assuming that the
5994  * target driver operates correctly - if parameters length is too short,
5995  * we just drop the page.
5996  * Two command may be sent if both Read Cache/Read Ahead and Write Cache
5997  * setting have to be changed.
5998  * SET FEATURE command is executed synchronously, i.e. we wait here until
5999  * it is completed, regardless of the scsi pkt directives.
6000  *
6001  * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e.
6002  * changing DRA will change RCD.
6003  *
6004  * More than one SATA command may be executed to perform operations specified
6005  * by mode select pages. The first error terminates further execution.
6006  * Operations performed successully are not backed-up in such case.
6007  *
6008  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
6009  * If operation resulted in changing device setup, dmod flag should be set to
6010  * one (1). If parameters were not changed, dmod flag should be set to 0.
6011  * Upon return, if operation required sending command to the device, the rval
6012  * should be set to the value returned by sata_hba_start. If operation
6013  * did not require device access, rval should be set to TRAN_ACCEPT.
6014  * The pagelen should be set to the length of the page.
6015  *
6016  * This function has to be called with a port mutex held.
6017  *
6018  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6019  */
6020 int
6021 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page,
6022     int parmlen, int *pagelen, int *rval, int *dmod)
6023 {
6024 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6025 	sata_drive_info_t *sdinfo;
6026 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6027 	sata_id_t *sata_id;
6028 	struct scsi_extended_sense *sense;
6029 	int wce, dra;	/* Current settings */
6030 
6031 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6032 	    &spx->txlt_sata_pkt->satapkt_device);
6033 	sata_id = &sdinfo->satadrv_id;
6034 	*dmod = 0;
6035 
6036 	/* Verify parameters length. If too short, drop it */
6037 	if (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
6038 	    sizeof (struct mode_page) < parmlen) {
6039 		*scsipkt->pkt_scbp = STATUS_CHECK;
6040 		sense = sata_arq_sense(spx);
6041 		sense->es_key = KEY_ILLEGAL_REQUEST;
6042 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6043 		*pagelen = parmlen;
6044 		*rval = TRAN_ACCEPT;
6045 		return (SATA_FAILURE);
6046 	}
6047 
6048 	*pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page);
6049 
6050 	/*
6051 	 * We can manipulate only write cache and read ahead
6052 	 * (read cache) setting.
6053 	 */
6054 	if (!(sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) &&
6055 	    !(sata_id->ai_cmdset82 & SATA_WRITE_CACHE)) {
6056 		/*
6057 		 * None of the features is supported - ignore
6058 		 */
6059 		*rval = TRAN_ACCEPT;
6060 		return (SATA_SUCCESS);
6061 	}
6062 
6063 	/* Current setting of Read Ahead (and Read Cache) */
6064 	if (sata_id->ai_features85 & SATA_LOOK_AHEAD)
6065 		dra = 0;	/* 0 == not disabled */
6066 	else
6067 		dra = 1;
6068 	/* Current setting of Write Cache */
6069 	if (sata_id->ai_features85 & SATA_WRITE_CACHE)
6070 		wce = 1;
6071 	else
6072 		wce = 0;
6073 
6074 	if (page->dra == dra && page->wce == wce && page->rcd == dra) {
6075 		/* nothing to do */
6076 		*rval = TRAN_ACCEPT;
6077 		return (SATA_SUCCESS);
6078 	}
6079 	/*
6080 	 * Need to flip some setting
6081 	 * Set-up Internal SET FEATURES command(s)
6082 	 */
6083 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6084 	scmd->satacmd_addr_type = 0;
6085 	scmd->satacmd_device_reg = 0;
6086 	scmd->satacmd_status_reg = 0;
6087 	scmd->satacmd_error_reg = 0;
6088 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
6089 	if (page->dra != dra || page->rcd != dra) {
6090 		/* Need to flip read ahead setting */
6091 		if (dra == 0)
6092 			/* Disable read ahead / read cache */
6093 			scmd->satacmd_features_reg =
6094 			    SATAC_SF_DISABLE_READ_AHEAD;
6095 		else
6096 			/* Enable read ahead  / read cache */
6097 			scmd->satacmd_features_reg =
6098 			    SATAC_SF_ENABLE_READ_AHEAD;
6099 
6100 		/* Transfer command to HBA */
6101 		if (sata_hba_start(spx, rval) != 0)
6102 			/*
6103 			 * Pkt not accepted for execution.
6104 			 */
6105 			return (SATA_FAILURE);
6106 
6107 		*dmod = 1;
6108 
6109 		/* Now process return */
6110 		if (spx->txlt_sata_pkt->satapkt_reason !=
6111 		    SATA_PKT_COMPLETED) {
6112 			goto failure;	/* Terminate */
6113 		}
6114 	}
6115 
6116 	/* Note that the packet is not removed, so it could be re-used */
6117 	if (page->wce != wce) {
6118 		/* Need to flip Write Cache setting */
6119 		if (page->wce == 1)
6120 			/* Enable write cache */
6121 			scmd->satacmd_features_reg =
6122 			    SATAC_SF_ENABLE_WRITE_CACHE;
6123 		else
6124 			/* Disable write cache */
6125 			scmd->satacmd_features_reg =
6126 			    SATAC_SF_DISABLE_WRITE_CACHE;
6127 
6128 		/* Transfer command to HBA */
6129 		if (sata_hba_start(spx, rval) != 0)
6130 			/*
6131 			 * Pkt not accepted for execution.
6132 			 */
6133 			return (SATA_FAILURE);
6134 
6135 		*dmod = 1;
6136 
6137 		/* Now process return */
6138 		if (spx->txlt_sata_pkt->satapkt_reason !=
6139 		    SATA_PKT_COMPLETED) {
6140 			goto failure;
6141 		}
6142 	}
6143 	return (SATA_SUCCESS);
6144 
6145 failure:
6146 	sata_xlate_errors(spx);
6147 
6148 	return (SATA_FAILURE);
6149 }
6150 
6151 /*
6152  * Process mode select informational exceptions control page 0x1c
6153  *
6154  * The only changeable bit is dexcpt (disable exceptions).
6155  * MRIE (method of reporting informational exceptions) must be
6156  * "only on request".
6157  *
6158  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
6159  * If operation resulted in changing device setup, dmod flag should be set to
6160  * one (1). If parameters were not changed, dmod flag should be set to 0.
6161  * Upon return, if operation required sending command to the device, the rval
6162  * should be set to the value returned by sata_hba_start. If operation
6163  * did not require device access, rval should be set to TRAN_ACCEPT.
6164  * The pagelen should be set to the length of the page.
6165  *
6166  * This function has to be called with a port mutex held.
6167  *
6168  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6169  */
6170 static	int
6171 sata_mode_select_page_1c(
6172 	sata_pkt_txlate_t *spx,
6173 	struct mode_info_excpt_page *page,
6174 	int parmlen,
6175 	int *pagelen,
6176 	int *rval,
6177 	int *dmod)
6178 {
6179 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6180 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6181 	sata_drive_info_t *sdinfo;
6182 	sata_id_t *sata_id;
6183 	struct scsi_extended_sense *sense;
6184 
6185 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6186 	    &spx->txlt_sata_pkt->satapkt_device);
6187 	sata_id = &sdinfo->satadrv_id;
6188 
6189 	*dmod = 0;
6190 
6191 	/* Verify parameters length. If too short, drop it */
6192 	if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) < parmlen) ||
6193 	    page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) {
6194 		*scsipkt->pkt_scbp = STATUS_CHECK;
6195 		sense = sata_arq_sense(spx);
6196 		sense->es_key = KEY_ILLEGAL_REQUEST;
6197 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6198 		*pagelen = parmlen;
6199 		*rval = TRAN_ACCEPT;
6200 		return (SATA_FAILURE);
6201 	}
6202 
6203 	*pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page);
6204 
6205 	if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
6206 		*scsipkt->pkt_scbp = STATUS_CHECK;
6207 		sense = sata_arq_sense(spx);
6208 		sense->es_key = KEY_ILLEGAL_REQUEST;
6209 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6210 		*pagelen = parmlen;
6211 		*rval = TRAN_ACCEPT;
6212 		return (SATA_FAILURE);
6213 	}
6214 
6215 	/* If already in the state requested, we are done */
6216 	if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
6217 		/* nothing to do */
6218 		*rval = TRAN_ACCEPT;
6219 		return (SATA_SUCCESS);
6220 	}
6221 
6222 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6223 
6224 	/* Build SMART_ENABLE or SMART_DISABLE command */
6225 	scmd->satacmd_addr_type = 0;		/* N/A */
6226 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
6227 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
6228 	scmd->satacmd_features_reg = page->dexcpt ?
6229 	    SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS;
6230 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
6231 	scmd->satacmd_cmd_reg = SATAC_SMART;
6232 
6233 	/* Transfer command to HBA */
6234 	if (sata_hba_start(spx, rval) != 0)
6235 		/*
6236 		 * Pkt not accepted for execution.
6237 		 */
6238 		return (SATA_FAILURE);
6239 
6240 	*dmod = 1;	/* At least may have been modified */
6241 
6242 	/* Now process return */
6243 	if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED)
6244 		return (SATA_SUCCESS);
6245 
6246 	/* Packet did not complete successfully */
6247 	sata_xlate_errors(spx);
6248 
6249 	return (SATA_FAILURE);
6250 }
6251 
6252 int
6253 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct
6254     mode_acoustic_management *page, int parmlen, int *pagelen,
6255     int *rval, int *dmod)
6256 {
6257 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6258 	sata_drive_info_t *sdinfo;
6259 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6260 	sata_id_t *sata_id;
6261 	struct scsi_extended_sense *sense;
6262 
6263 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6264 	    &spx->txlt_sata_pkt->satapkt_device);
6265 	sata_id = &sdinfo->satadrv_id;
6266 	*dmod = 0;
6267 
6268 	/* If parmlen is too short or the feature is not supported, drop it */
6269 	if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6270 	    sizeof (struct mode_page)) < parmlen) ||
6271 	    (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) {
6272 		*scsipkt->pkt_scbp = STATUS_CHECK;
6273 		sense = sata_arq_sense(spx);
6274 		sense->es_key = KEY_ILLEGAL_REQUEST;
6275 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6276 		*pagelen = parmlen;
6277 		*rval = TRAN_ACCEPT;
6278 		return (SATA_FAILURE);
6279 	}
6280 
6281 	*pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6282 	    sizeof (struct mode_page);
6283 
6284 	/*
6285 	 * We can enable and disable acoustice management and
6286 	 * set the acoustic management level.
6287 	 */
6288 
6289 	/*
6290 	 * Set-up Internal SET FEATURES command(s)
6291 	 */
6292 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6293 	scmd->satacmd_addr_type = 0;
6294 	scmd->satacmd_device_reg = 0;
6295 	scmd->satacmd_status_reg = 0;
6296 	scmd->satacmd_error_reg = 0;
6297 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
6298 	if (page->acoustic_manag_enable) {
6299 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC;
6300 		scmd->satacmd_sec_count_lsb = page->acoustic_manag_level;
6301 	} else {	/* disabling acoustic management */
6302 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC;
6303 	}
6304 
6305 	/* Transfer command to HBA */
6306 	if (sata_hba_start(spx, rval) != 0)
6307 		/*
6308 		 * Pkt not accepted for execution.
6309 		 */
6310 		return (SATA_FAILURE);
6311 
6312 	/* Now process return */
6313 	if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) {
6314 		sata_xlate_errors(spx);
6315 		return (SATA_FAILURE);
6316 	}
6317 
6318 	*dmod = 1;
6319 
6320 	return (SATA_SUCCESS);
6321 }
6322 
6323 
6324 
6325 
6326 /*
6327  * sata_build_lsense_page0() is used to create the
6328  * SCSI LOG SENSE page 0 (supported log pages)
6329  *
6330  * Currently supported pages are 0, 0x10, 0x2f and 0x30
6331  * (supported log pages, self-test results, informational exceptions
6332  *  and Sun vendor specific ATA SMART data).
6333  *
6334  * Takes a sata_drive_info t * and the address of a buffer
6335  * in which to create the page information.
6336  *
6337  * Returns the number of bytes valid in the buffer.
6338  */
6339 static	int
6340 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf)
6341 {
6342 	struct log_parameter *lpp = (struct log_parameter *)buf;
6343 	uint8_t *page_ptr = (uint8_t *)lpp->param_values;
6344 	int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */
6345 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6346 
6347 	lpp->param_code[0] = 0;
6348 	lpp->param_code[1] = 0;
6349 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
6350 	*page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES;
6351 
6352 	if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
6353 		if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) {
6354 			*page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS;
6355 			++num_pages_supported;
6356 		}
6357 		*page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS;
6358 		++num_pages_supported;
6359 		*page_ptr++ = PAGE_CODE_SMART_READ_DATA;
6360 		++num_pages_supported;
6361 	}
6362 
6363 	lpp->param_len = num_pages_supported;
6364 
6365 	return ((&lpp->param_values[0] - (uint8_t *)lpp) +
6366 	    num_pages_supported);
6367 }
6368 
6369 /*
6370  * sata_build_lsense_page_10() is used to create the
6371  * SCSI LOG SENSE page 0x10 (self-test results)
6372  *
6373  * Takes a sata_drive_info t * and the address of a buffer
6374  * in which to create the page information as well as a sata_hba_inst_t *.
6375  *
6376  * Returns the number of bytes valid in the buffer.
6377  */
6378 static	int
6379 sata_build_lsense_page_10(
6380 	sata_drive_info_t *sdinfo,
6381 	uint8_t *buf,
6382 	sata_hba_inst_t *sata_hba_inst)
6383 {
6384 	struct log_parameter *lpp = (struct log_parameter *)buf;
6385 	int rval;
6386 
6387 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6388 		struct smart_ext_selftest_log *ext_selftest_log;
6389 
6390 		ext_selftest_log = kmem_zalloc(
6391 		    sizeof (struct smart_ext_selftest_log), KM_SLEEP);
6392 
6393 		rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo,
6394 		    ext_selftest_log, 0);
6395 		if (rval == 0) {
6396 			int index, start_index;
6397 			struct smart_ext_selftest_log_entry *entry;
6398 			static const struct smart_ext_selftest_log_entry empty =
6399 			    {0};
6400 			uint16_t block_num;
6401 			int count;
6402 			boolean_t only_one_block = B_FALSE;
6403 
6404 			index = ext_selftest_log->
6405 			    smart_ext_selftest_log_index[0];
6406 			index |= ext_selftest_log->
6407 			    smart_ext_selftest_log_index[1] << 8;
6408 			if (index == 0)
6409 				goto out;
6410 
6411 			--index;	/* Correct for 0 origin */
6412 			start_index = index;	/* remember where we started */
6413 			block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6414 			if (block_num != 0) {
6415 				rval = sata_ext_smart_selftest_read_log(
6416 				    sata_hba_inst, sdinfo, ext_selftest_log,
6417 				    block_num);
6418 				if (rval != 0)
6419 					goto out;
6420 			}
6421 			index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6422 			entry =
6423 			    &ext_selftest_log->
6424 			    smart_ext_selftest_log_entries[index];
6425 
6426 			for (count = 1;
6427 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
6428 			    ++count) {
6429 				uint8_t status;
6430 				uint8_t code;
6431 				uint8_t sense_key;
6432 				uint8_t add_sense_code;
6433 				uint8_t add_sense_code_qual;
6434 
6435 				/* If this is an unused entry, we are done */
6436 				if (bcmp(entry, &empty, sizeof (empty)) == 0) {
6437 					/* Broken firmware on some disks */
6438 					if (index + 1 ==
6439 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK) {
6440 						--entry;
6441 						--index;
6442 						if (bcmp(entry, &empty,
6443 						    sizeof (empty)) == 0)
6444 							goto out;
6445 					} else
6446 						goto out;
6447 				}
6448 
6449 				if (only_one_block &&
6450 				    start_index == index)
6451 					goto out;
6452 
6453 				lpp->param_code[0] = 0;
6454 				lpp->param_code[1] = count;
6455 				lpp->param_ctrl_flags =
6456 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
6457 				lpp->param_len =
6458 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
6459 
6460 				status = entry->smart_ext_selftest_log_status;
6461 				status >>= 4;
6462 				switch (status) {
6463 				case 0:
6464 				default:
6465 					sense_key = KEY_NO_SENSE;
6466 					add_sense_code =
6467 					    SD_SCSI_ASC_NO_ADD_SENSE;
6468 					add_sense_code_qual = 0;
6469 					break;
6470 				case 1:
6471 					sense_key = KEY_ABORTED_COMMAND;
6472 					add_sense_code =
6473 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6474 					add_sense_code_qual = SCSI_COMPONENT_81;
6475 					break;
6476 				case 2:
6477 					sense_key = KEY_ABORTED_COMMAND;
6478 					add_sense_code =
6479 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6480 					add_sense_code_qual = SCSI_COMPONENT_82;
6481 					break;
6482 				case 3:
6483 					sense_key = KEY_ABORTED_COMMAND;
6484 					add_sense_code =
6485 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6486 					add_sense_code_qual = SCSI_COMPONENT_83;
6487 					break;
6488 				case 4:
6489 					sense_key = KEY_HARDWARE_ERROR;
6490 					add_sense_code =
6491 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6492 					add_sense_code_qual = SCSI_COMPONENT_84;
6493 					break;
6494 				case 5:
6495 					sense_key = KEY_HARDWARE_ERROR;
6496 					add_sense_code =
6497 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6498 					add_sense_code_qual = SCSI_COMPONENT_85;
6499 					break;
6500 				case 6:
6501 					sense_key = KEY_HARDWARE_ERROR;
6502 					add_sense_code =
6503 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6504 					add_sense_code_qual = SCSI_COMPONENT_86;
6505 					break;
6506 				case 7:
6507 					sense_key = KEY_MEDIUM_ERROR;
6508 					add_sense_code =
6509 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6510 					add_sense_code_qual = SCSI_COMPONENT_87;
6511 					break;
6512 				case 8:
6513 					sense_key = KEY_HARDWARE_ERROR;
6514 					add_sense_code =
6515 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6516 					add_sense_code_qual = SCSI_COMPONENT_88;
6517 					break;
6518 				}
6519 				code = 0;	/* unspecified */
6520 				status |= (code << 4);
6521 				lpp->param_values[0] = status;
6522 				lpp->param_values[1] = 0; /* unspecified */
6523 				lpp->param_values[2] = entry->
6524 				    smart_ext_selftest_log_timestamp[1];
6525 				lpp->param_values[3] = entry->
6526 				    smart_ext_selftest_log_timestamp[0];
6527 				if (status != 0) {
6528 					lpp->param_values[4] = 0;
6529 					lpp->param_values[5] = 0;
6530 					lpp->param_values[6] = entry->
6531 					    smart_ext_selftest_log_failing_lba
6532 					    [5];
6533 					lpp->param_values[7] = entry->
6534 					    smart_ext_selftest_log_failing_lba
6535 					    [4];
6536 					lpp->param_values[8] = entry->
6537 					    smart_ext_selftest_log_failing_lba
6538 					    [3];
6539 					lpp->param_values[9] = entry->
6540 					    smart_ext_selftest_log_failing_lba
6541 					    [2];
6542 					lpp->param_values[10] = entry->
6543 					    smart_ext_selftest_log_failing_lba
6544 					    [1];
6545 					lpp->param_values[11] = entry->
6546 					    smart_ext_selftest_log_failing_lba
6547 					    [0];
6548 				} else {	/* No bad block address */
6549 					lpp->param_values[4] = 0xff;
6550 					lpp->param_values[5] = 0xff;
6551 					lpp->param_values[6] = 0xff;
6552 					lpp->param_values[7] = 0xff;
6553 					lpp->param_values[8] = 0xff;
6554 					lpp->param_values[9] = 0xff;
6555 					lpp->param_values[10] = 0xff;
6556 					lpp->param_values[11] = 0xff;
6557 				}
6558 
6559 				lpp->param_values[12] = sense_key;
6560 				lpp->param_values[13] = add_sense_code;
6561 				lpp->param_values[14] = add_sense_code_qual;
6562 				lpp->param_values[15] = 0; /* undefined */
6563 
6564 				lpp = (struct log_parameter *)
6565 				    (((uint8_t *)lpp) +
6566 				    SCSI_LOG_PARAM_HDR_LEN +
6567 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
6568 
6569 				--index;	/* Back up to previous entry */
6570 				if (index < 0) {
6571 					if (block_num > 0) {
6572 						--block_num;
6573 					} else {
6574 						struct read_log_ext_directory
6575 						    logdir;
6576 
6577 						rval =
6578 						    sata_read_log_ext_directory(
6579 						    sata_hba_inst, sdinfo,
6580 						    &logdir);
6581 						if (rval == -1)
6582 							goto out;
6583 						if ((logdir.read_log_ext_vers
6584 						    [0] == 0) &&
6585 						    (logdir.read_log_ext_vers
6586 						    [1] == 0))
6587 							goto out;
6588 						block_num =
6589 						    logdir.read_log_ext_nblks
6590 						    [EXT_SMART_SELFTEST_LOG_PAGE
6591 						    - 1][0];
6592 						block_num |= logdir.
6593 						    read_log_ext_nblks
6594 						    [EXT_SMART_SELFTEST_LOG_PAGE
6595 						    - 1][1] << 8;
6596 						--block_num;
6597 						only_one_block =
6598 						    (block_num == 0);
6599 					}
6600 					rval = sata_ext_smart_selftest_read_log(
6601 					    sata_hba_inst, sdinfo,
6602 					    ext_selftest_log, block_num);
6603 					if (rval != 0)
6604 						goto out;
6605 
6606 					index =
6607 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK -
6608 					    1;
6609 				}
6610 				index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6611 				entry = &ext_selftest_log->
6612 				    smart_ext_selftest_log_entries[index];
6613 			}
6614 		}
6615 out:
6616 		kmem_free(ext_selftest_log,
6617 		    sizeof (struct smart_ext_selftest_log));
6618 	} else {
6619 		struct smart_selftest_log *selftest_log;
6620 
6621 		selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log),
6622 		    KM_SLEEP);
6623 
6624 		rval = sata_smart_selftest_log(sata_hba_inst, sdinfo,
6625 		    selftest_log);
6626 
6627 		if (rval == 0) {
6628 			int index;
6629 			int count;
6630 			struct smart_selftest_log_entry *entry;
6631 			static const struct smart_selftest_log_entry empty =
6632 			    { 0 };
6633 
6634 			index = selftest_log->smart_selftest_log_index;
6635 			if (index == 0)
6636 				goto done;
6637 			--index;	/* Correct for 0 origin */
6638 			entry = &selftest_log->
6639 			    smart_selftest_log_entries[index];
6640 			for (count = 1;
6641 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
6642 			    ++count) {
6643 				uint8_t status;
6644 				uint8_t code;
6645 				uint8_t sense_key;
6646 				uint8_t add_sense_code;
6647 				uint8_t add_sense_code_qual;
6648 
6649 				if (bcmp(entry, &empty, sizeof (empty)) == 0)
6650 					goto done;
6651 
6652 				lpp->param_code[0] = 0;
6653 				lpp->param_code[1] = count;
6654 				lpp->param_ctrl_flags =
6655 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
6656 				lpp->param_len =
6657 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
6658 
6659 				status = entry->smart_selftest_log_status;
6660 				status >>= 4;
6661 				switch (status) {
6662 				case 0:
6663 				default:
6664 					sense_key = KEY_NO_SENSE;
6665 					add_sense_code =
6666 					    SD_SCSI_ASC_NO_ADD_SENSE;
6667 					break;
6668 				case 1:
6669 					sense_key = KEY_ABORTED_COMMAND;
6670 					add_sense_code =
6671 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6672 					add_sense_code_qual = SCSI_COMPONENT_81;
6673 					break;
6674 				case 2:
6675 					sense_key = KEY_ABORTED_COMMAND;
6676 					add_sense_code =
6677 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6678 					add_sense_code_qual = SCSI_COMPONENT_82;
6679 					break;
6680 				case 3:
6681 					sense_key = KEY_ABORTED_COMMAND;
6682 					add_sense_code =
6683 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6684 					add_sense_code_qual = SCSI_COMPONENT_83;
6685 					break;
6686 				case 4:
6687 					sense_key = KEY_HARDWARE_ERROR;
6688 					add_sense_code =
6689 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6690 					add_sense_code_qual = SCSI_COMPONENT_84;
6691 					break;
6692 				case 5:
6693 					sense_key = KEY_HARDWARE_ERROR;
6694 					add_sense_code =
6695 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6696 					add_sense_code_qual = SCSI_COMPONENT_85;
6697 					break;
6698 				case 6:
6699 					sense_key = KEY_HARDWARE_ERROR;
6700 					add_sense_code =
6701 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6702 					add_sense_code_qual = SCSI_COMPONENT_86;
6703 					break;
6704 				case 7:
6705 					sense_key = KEY_MEDIUM_ERROR;
6706 					add_sense_code =
6707 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6708 					add_sense_code_qual = SCSI_COMPONENT_87;
6709 					break;
6710 				case 8:
6711 					sense_key = KEY_HARDWARE_ERROR;
6712 					add_sense_code =
6713 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6714 					add_sense_code_qual = SCSI_COMPONENT_88;
6715 					break;
6716 				}
6717 				code = 0;	/* unspecified */
6718 				status |= (code << 4);
6719 				lpp->param_values[0] = status;
6720 				lpp->param_values[1] = 0; /* unspecified */
6721 				lpp->param_values[2] = entry->
6722 				    smart_selftest_log_timestamp[1];
6723 				lpp->param_values[3] = entry->
6724 				    smart_selftest_log_timestamp[0];
6725 				if (status != 0) {
6726 					lpp->param_values[4] = 0;
6727 					lpp->param_values[5] = 0;
6728 					lpp->param_values[6] = 0;
6729 					lpp->param_values[7] = 0;
6730 					lpp->param_values[8] = entry->
6731 					    smart_selftest_log_failing_lba[3];
6732 					lpp->param_values[9] = entry->
6733 					    smart_selftest_log_failing_lba[2];
6734 					lpp->param_values[10] = entry->
6735 					    smart_selftest_log_failing_lba[1];
6736 					lpp->param_values[11] = entry->
6737 					    smart_selftest_log_failing_lba[0];
6738 				} else {	/* No block address */
6739 					lpp->param_values[4] = 0xff;
6740 					lpp->param_values[5] = 0xff;
6741 					lpp->param_values[6] = 0xff;
6742 					lpp->param_values[7] = 0xff;
6743 					lpp->param_values[8] = 0xff;
6744 					lpp->param_values[9] = 0xff;
6745 					lpp->param_values[10] = 0xff;
6746 					lpp->param_values[11] = 0xff;
6747 				}
6748 				lpp->param_values[12] = sense_key;
6749 				lpp->param_values[13] = add_sense_code;
6750 				lpp->param_values[14] = add_sense_code_qual;
6751 				lpp->param_values[15] = 0; /* undefined */
6752 
6753 				lpp = (struct log_parameter *)
6754 				    (((uint8_t *)lpp) +
6755 				    SCSI_LOG_PARAM_HDR_LEN +
6756 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
6757 				--index;	/* back up to previous entry */
6758 				if (index < 0) {
6759 					index =
6760 					    NUM_SMART_SELFTEST_LOG_ENTRIES - 1;
6761 				}
6762 				entry = &selftest_log->
6763 				    smart_selftest_log_entries[index];
6764 			}
6765 		}
6766 done:
6767 		kmem_free(selftest_log, sizeof (struct smart_selftest_log));
6768 	}
6769 
6770 	return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) *
6771 	    SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS);
6772 }
6773 
6774 /*
6775  * sata_build_lsense_page_2f() is used to create the
6776  * SCSI LOG SENSE page 0x10 (informational exceptions)
6777  *
6778  * Takes a sata_drive_info t * and the address of a buffer
6779  * in which to create the page information as well as a sata_hba_inst_t *.
6780  *
6781  * Returns the number of bytes valid in the buffer.
6782  */
6783 static	int
6784 sata_build_lsense_page_2f(
6785 	sata_drive_info_t *sdinfo,
6786 	uint8_t *buf,
6787 	sata_hba_inst_t *sata_hba_inst)
6788 {
6789 	struct log_parameter *lpp = (struct log_parameter *)buf;
6790 	int rval;
6791 	uint8_t *smart_data;
6792 	uint8_t temp;
6793 	sata_id_t *sata_id;
6794 #define	SMART_NO_TEMP	0xff
6795 
6796 	lpp->param_code[0] = 0;
6797 	lpp->param_code[1] = 0;
6798 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
6799 
6800 	/* Now get the SMART status w.r.t. threshold exceeded */
6801 	rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo);
6802 	switch (rval) {
6803 	case 1:
6804 		lpp->param_values[0] = SCSI_PREDICTED_FAILURE;
6805 		lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE;
6806 		break;
6807 	case 0:
6808 	case -1:	/* failed to get data */
6809 		lpp->param_values[0] = 0;	/* No failure predicted */
6810 		lpp->param_values[1] = 0;
6811 		break;
6812 #if defined(SATA_DEBUG)
6813 	default:
6814 		cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value");
6815 		/* NOTREACHED */
6816 #endif
6817 	}
6818 
6819 	sata_id = &sdinfo->satadrv_id;
6820 	if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP))
6821 		temp = SMART_NO_TEMP;
6822 	else {
6823 		/* Now get the temperature */
6824 		smart_data = kmem_zalloc(512, KM_SLEEP);
6825 		rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data,
6826 		    SCT_STATUS_LOG_PAGE, 1);
6827 		if (rval == -1)
6828 			temp = SMART_NO_TEMP;
6829 		else {
6830 			temp = smart_data[200];
6831 			if (temp & 0x80) {
6832 				if (temp & 0x7f)
6833 					temp = 0;
6834 				else
6835 					temp = SMART_NO_TEMP;
6836 			}
6837 		}
6838 		kmem_free(smart_data, 512);
6839 	}
6840 
6841 	lpp->param_values[2] = temp;	/* most recent temperature */
6842 	lpp->param_values[3] = 0;	/* required vendor specific byte */
6843 
6844 	lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN;
6845 
6846 
6847 	return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN);
6848 }
6849 
6850 /*
6851  * sata_build_lsense_page_30() is used to create the
6852  * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data).
6853  *
6854  * Takes a sata_drive_info t * and the address of a buffer
6855  * in which to create the page information as well as a sata_hba_inst_t *.
6856  *
6857  * Returns the number of bytes valid in the buffer.
6858  */
6859 static int
6860 sata_build_lsense_page_30(
6861 	sata_drive_info_t *sdinfo,
6862 	uint8_t *buf,
6863 	sata_hba_inst_t *sata_hba_inst)
6864 {
6865 	struct smart_data *smart_data = (struct smart_data *)buf;
6866 	int rval;
6867 
6868 	/* Now do the SMART READ DATA */
6869 	rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data);
6870 	if (rval == -1)
6871 		return (0);
6872 
6873 	return (sizeof (struct smart_data));
6874 }
6875 
6876 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */
6877 
6878 /*
6879  * Start command for ATAPI device.
6880  * This function processes scsi_pkt requests.
6881  * Only CD/DVD devices are supported.
6882  * Most commands are packet without any translation into Packet Command.
6883  * Some may be trapped and executed as SATA commands (not clear which one).
6884  *
6885  * Returns TRAN_ACCEPT if command is accepted for execution (or completed
6886  * execution).
6887  * Returns other TRAN_XXXX codes if command is not accepted or completed
6888  * (see return values for sata_hba_start()).
6889  *
6890  * Note:
6891  * Inquiry cdb format differs between transport version 2 and 3.
6892  * However, the transport version 3 devices that were checked did not adhere
6893  * to the specification (ignored MSB of the allocation length). Therefore,
6894  * the transport version is not checked, but Inquiry allocation length is
6895  * truncated to 255 bytes if the original allocation length set-up by the
6896  * target driver is greater than 255 bytes.
6897  */
6898 static int
6899 sata_txlt_atapi(sata_pkt_txlate_t *spx)
6900 {
6901 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6902 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6903 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
6904 	sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx);
6905 	sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba,
6906 	    &spx->txlt_sata_pkt->satapkt_device);
6907 	int cport = SATA_TXLT_CPORT(spx);
6908 	int cdblen;
6909 	int rval;
6910 	int synch;
6911 	union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp;
6912 
6913 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
6914 
6915 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
6916 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
6917 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6918 		return (rval);
6919 	}
6920 
6921 	/*
6922 	 * ATAPI device executes some ATA commands in addition to MMC command
6923 	 * set. These ATA commands may be executed by the regular SATA
6924 	 * translation functions. None needs to be captured now.
6925 	 * Other commands belong to MMC command set and are delivered
6926 	 * to ATAPI device via Packet Command.
6927 	 */
6928 
6929 	/* Check the size of cdb */
6930 	cdblen = scsi_cdb_size[GETGROUP(cdbp)];
6931 	if (cdblen > sdinfo->satadrv_atapi_cdb_len) {
6932 		sata_log(NULL, CE_WARN,
6933 		    "sata: invalid ATAPI cdb length %d",
6934 		    scsipkt->pkt_cdblen);
6935 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6936 		return (TRAN_BADPKT);
6937 	}
6938 
6939 	SATAATAPITRACE(spx, cdblen);
6940 
6941 	/*
6942 	 * For non-read/write commands we need to
6943 	 * map buffer
6944 	 */
6945 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6946 	case SCMD_READ:
6947 	case SCMD_READ_G1:
6948 	case SCMD_READ_G5:
6949 	case SCMD_READ_G4:
6950 	case SCMD_WRITE:
6951 	case SCMD_WRITE_G1:
6952 	case SCMD_WRITE_G5:
6953 	case SCMD_WRITE_G4:
6954 		break;
6955 	default:
6956 		if (bp != NULL) {
6957 			if (bp->b_flags & (B_PHYS | B_PAGEIO))
6958 				bp_mapin(bp);
6959 		}
6960 		break;
6961 	}
6962 	/*
6963 	 * scmd->satacmd_flags.sata_data_direction default -
6964 	 * SATA_DIR_NODATA_XFER - is set by
6965 	 * sata_txlt_generic_pkt_info().
6966 	 */
6967 	if (scmd->satacmd_bp) {
6968 		if (scmd->satacmd_bp->b_flags & B_READ) {
6969 			scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
6970 		} else {
6971 			scmd->satacmd_flags.sata_data_direction =
6972 			    SATA_DIR_WRITE;
6973 		}
6974 	}
6975 
6976 	/*
6977 	 * Set up ATAPI packet command.
6978 	 */
6979 
6980 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
6981 
6982 	/* Copy cdb into sata_cmd */
6983 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
6984 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
6985 	bcopy(cdbp, scmd->satacmd_acdb, cdblen);
6986 
6987 	/* See note in the command header */
6988 	if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) {
6989 		if (scmd->satacmd_acdb[3] != 0)
6990 			scmd->satacmd_acdb[4] = 255;
6991 	}
6992 
6993 #ifdef SATA_DEBUG
6994 	if (sata_debug_flags & SATA_DBG_ATAPI) {
6995 		uint8_t *p = scmd->satacmd_acdb;
6996 		char buf[3 * SATA_ATAPI_MAX_CDB_LEN];
6997 
6998 		(void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN,
6999 		    "%02x %02x %02x %02x %02x %02x %02x %02x "
7000 		    "%2x %02x %02x %02x %02x %02x %02x %02x",
7001 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7002 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7003 		buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0';
7004 		cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf);
7005 	}
7006 #endif
7007 
7008 	/*
7009 	 * Preset request sense data to NO SENSE.
7010 	 * If there is no way to get error information via Request Sense,
7011 	 * the packet request sense data would not have to be modified by HBA,
7012 	 * but it could be returned as is.
7013 	 */
7014 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
7015 	sata_fixed_sense_data_preset(
7016 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7017 
7018 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
7019 		/* Need callback function */
7020 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion;
7021 		synch = FALSE;
7022 	} else
7023 		synch = TRUE;
7024 
7025 	/* Transfer command to HBA */
7026 	if (sata_hba_start(spx, &rval) != 0) {
7027 		/* Pkt not accepted for execution */
7028 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
7029 		return (rval);
7030 	}
7031 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
7032 	/*
7033 	 * If execution is non-synchronous,
7034 	 * a callback function will handle potential errors, translate
7035 	 * the response and will do a callback to a target driver.
7036 	 * If it was synchronous, use the same framework callback to check
7037 	 * an execution status.
7038 	 */
7039 	if (synch) {
7040 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7041 		    "synchronous execution status %x\n",
7042 		    spx->txlt_sata_pkt->satapkt_reason);
7043 		sata_txlt_atapi_completion(spx->txlt_sata_pkt);
7044 	}
7045 	return (TRAN_ACCEPT);
7046 }
7047 
7048 
7049 /*
7050  * ATAPI Packet command completion.
7051  *
7052  * Failure of the command passed via Packet command are considered device
7053  * error. SATA HBA driver would have to retrieve error data (via Request
7054  * Sense command delivered via error retrieval sata packet) and copy it
7055  * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data.
7056  */
7057 static void
7058 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt)
7059 {
7060 	sata_pkt_txlate_t *spx =
7061 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7062 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7063 	struct scsi_extended_sense *sense;
7064 	struct buf *bp;
7065 	int rval;
7066 
7067 #ifdef SATA_DEBUG
7068 	uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense;
7069 #endif
7070 
7071 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7072 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7073 
7074 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7075 		/* Normal completion */
7076 		if (sata_pkt->satapkt_cmd.satacmd_bp != NULL)
7077 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
7078 		scsipkt->pkt_reason = CMD_CMPLT;
7079 		*scsipkt->pkt_scbp = STATUS_GOOD;
7080 		if (spx->txlt_tmp_buf != NULL) {
7081 			/* Temporary buffer was used */
7082 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7083 			if (bp->b_flags & B_READ) {
7084 				rval = ddi_dma_sync(
7085 				    spx->txlt_buf_dma_handle, 0, 0,
7086 				    DDI_DMA_SYNC_FORCPU);
7087 				ASSERT(rval == DDI_SUCCESS);
7088 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7089 				    bp->b_bcount);
7090 			}
7091 		}
7092 	} else {
7093 		/*
7094 		 * Something went wrong - analyze return
7095 		 */
7096 		*scsipkt->pkt_scbp = STATUS_CHECK;
7097 		sense = sata_arq_sense(spx);
7098 
7099 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7100 			scsipkt->pkt_reason = CMD_INCOMPLETE;
7101 			/*
7102 			 * We may not have ARQ data if there was a double
7103 			 * error. But sense data in sata packet was pre-set
7104 			 * with NO SENSE so it is valid even if HBA could
7105 			 * not retrieve a real sense data.
7106 			 * Just copy this sense data into scsi pkt sense area.
7107 			 */
7108 			bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense,
7109 			    SATA_ATAPI_MIN_RQSENSE_LEN);
7110 #ifdef SATA_DEBUG
7111 			if (sata_debug_flags & SATA_DBG_SCSI_IF) {
7112 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7113 				    "sata_txlt_atapi_completion: %02x\n"
7114 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
7115 				    "          %02x %02x %02x %02x %02x %02x "
7116 				    "          %02x %02x %02x %02x %02x %02x\n",
7117 				    scsipkt->pkt_reason,
7118 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7119 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7120 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7121 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7122 				    rqsp[16], rqsp[17]);
7123 			}
7124 #endif
7125 		} else {
7126 			switch (sata_pkt->satapkt_reason) {
7127 			case SATA_PKT_PORT_ERROR:
7128 				/*
7129 				 * We have no device data.
7130 				 */
7131 				scsipkt->pkt_reason = CMD_INCOMPLETE;
7132 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
7133 				    STATE_GOT_TARGET | STATE_SENT_CMD |
7134 				    STATE_GOT_STATUS);
7135 				sense->es_key = KEY_HARDWARE_ERROR;
7136 				break;
7137 
7138 			case SATA_PKT_TIMEOUT:
7139 				scsipkt->pkt_reason = CMD_TIMEOUT;
7140 				scsipkt->pkt_statistics |=
7141 				    STAT_TIMEOUT | STAT_DEV_RESET;
7142 				/*
7143 				 * Need to check if HARDWARE_ERROR/
7144 				 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more
7145 				 * appropriate.
7146 				 */
7147 				break;
7148 
7149 			case SATA_PKT_ABORTED:
7150 				scsipkt->pkt_reason = CMD_ABORTED;
7151 				scsipkt->pkt_statistics |= STAT_ABORTED;
7152 				/* Should we set key COMMAND_ABPRTED? */
7153 				break;
7154 
7155 			case SATA_PKT_RESET:
7156 				scsipkt->pkt_reason = CMD_RESET;
7157 				scsipkt->pkt_statistics |= STAT_DEV_RESET;
7158 				/*
7159 				 * May be we should set Unit Attention /
7160 				 * Reset. Perhaps the same should be
7161 				 * returned for disks....
7162 				 */
7163 				sense->es_key = KEY_UNIT_ATTENTION;
7164 				sense->es_add_code = SD_SCSI_ASC_RESET;
7165 				break;
7166 
7167 			default:
7168 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7169 				    "sata_txlt_atapi_completion: "
7170 				    "invalid packet completion reason"));
7171 				scsipkt->pkt_reason = CMD_TRAN_ERR;
7172 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
7173 				    STATE_GOT_TARGET | STATE_SENT_CMD |
7174 				    STATE_GOT_STATUS);
7175 				break;
7176 			}
7177 		}
7178 	}
7179 
7180 	SATAATAPITRACE(spx, 0);
7181 
7182 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7183 	    scsipkt->pkt_comp != NULL) {
7184 		/* scsi callback required */
7185 		(*scsipkt->pkt_comp)(scsipkt);
7186 	}
7187 }
7188 
7189 /*
7190  * Set up error retrieval sata command for ATAPI Packet Command error data
7191  * recovery.
7192  *
7193  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
7194  * returns SATA_FAILURE otherwise.
7195  */
7196 
7197 static int
7198 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
7199 {
7200 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
7201 	sata_cmd_t *scmd;
7202 	struct buf *bp;
7203 
7204 	/*
7205 	 * Allocate dma-able buffer error data.
7206 	 * Buffer allocation will take care of buffer alignment and other DMA
7207 	 * attributes.
7208 	 */
7209 	bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN);
7210 	if (bp == NULL) {
7211 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
7212 		    "sata_get_err_retrieval_pkt: "
7213 		    "cannot allocate buffer for error data", NULL);
7214 		return (SATA_FAILURE);
7215 	}
7216 	bp_mapin(bp); /* make data buffer accessible */
7217 
7218 	/* Operation modes are up to the caller */
7219 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7220 
7221 	/* Synchronous mode, no callback - may be changed by the caller */
7222 	spkt->satapkt_comp = NULL;
7223 	spkt->satapkt_time = sata_default_pkt_time;
7224 
7225 	scmd = &spkt->satapkt_cmd;
7226 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7227 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7228 
7229 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7230 
7231 	/*
7232 	 * Set-up acdb. Request Sense CDB (packet command content) is
7233 	 * not in DMA-able buffer. Its handling is HBA-specific (how
7234 	 * it is transfered into packet FIS).
7235 	 */
7236 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7237 	bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN);
7238 	/* Following zeroing of pad bytes may not be necessary */
7239 	bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN],
7240 	    sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN);
7241 
7242 	/*
7243 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
7244 	 * before accessing it. Handle is in usual place in translate struct.
7245 	 */
7246 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
7247 
7248 	/*
7249 	 * Preset request sense data to NO SENSE.
7250 	 * Here it is redundant, only for a symetry with scsi-originated
7251 	 * packets. It should not be used for anything but debugging.
7252 	 */
7253 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
7254 	sata_fixed_sense_data_preset(
7255 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7256 
7257 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7258 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7259 
7260 	return (SATA_SUCCESS);
7261 }
7262 
7263 /*
7264  * Set-up ATAPI packet command.
7265  * Data transfer direction has to be set-up in sata_cmd structure prior to
7266  * calling this function.
7267  *
7268  * Returns void
7269  */
7270 
7271 static void
7272 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo)
7273 {
7274 	scmd->satacmd_addr_type = 0;		/* N/A */
7275 	scmd->satacmd_sec_count_lsb = 0;	/* no tag */
7276 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
7277 	scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ;
7278 	scmd->satacmd_lba_high_lsb =
7279 	    (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8);
7280 	scmd->satacmd_cmd_reg = SATAC_PACKET;	/* Command */
7281 
7282 	/*
7283 	 * We want all data to be transfered via DMA.
7284 	 * But specify it only if drive supports DMA and DMA mode is
7285 	 * selected - some drives are sensitive about it.
7286 	 * Hopefully it wil work for all drives....
7287 	 */
7288 	if (sdinfo->satadrv_settings & SATA_DEV_DMA)
7289 		scmd->satacmd_features_reg = SATA_ATAPI_F_DMA;
7290 
7291 	/*
7292 	 * Features register requires special care for devices that use
7293 	 * Serial ATA bridge - they need an explicit specification of
7294 	 * the data transfer direction for Packet DMA commands.
7295 	 * Setting this bit is harmless if DMA is not used.
7296 	 *
7297 	 * Many drives do not implement word 80, specifying what ATA/ATAPI
7298 	 * spec they follow.
7299 	 * We are arbitrarily following the latest SerialATA 2.6 spec,
7300 	 * which uses ATA/ATAPI 6 specification for Identify Data, unless
7301 	 * ATA/ATAPI-7 support is explicitly indicated.
7302 	 */
7303 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
7304 	    sdinfo->satadrv_id.ai_majorversion != 0xffff &&
7305 	    (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) {
7306 		/*
7307 		 * Specification of major version is valid and version 7
7308 		 * is supported. It does automatically imply that all
7309 		 * spec features are supported. For now, we assume that
7310 		 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete.
7311 		 */
7312 		if ((sdinfo->satadrv_id.ai_dirdma &
7313 		    SATA_ATAPI_ID_DMADIR_REQ) != 0) {
7314 			if (scmd->satacmd_flags.sata_data_direction ==
7315 			    SATA_DIR_READ)
7316 			scmd->satacmd_features_reg |=
7317 			    SATA_ATAPI_F_DATA_DIR_READ;
7318 		}
7319 	}
7320 }
7321 
7322 
7323 #ifdef SATA_DEBUG
7324 
7325 /* Display 18 bytes of Inquiry data */
7326 static void
7327 sata_show_inqry_data(uint8_t *buf)
7328 {
7329 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
7330 	uint8_t *p;
7331 
7332 	cmn_err(CE_NOTE, "Inquiry data:");
7333 	cmn_err(CE_NOTE, "device type %x", inq->inq_dtype);
7334 	cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb);
7335 	cmn_err(CE_NOTE, "version %x", inq->inq_ansi);
7336 	cmn_err(CE_NOTE, "ATAPI transport version %d",
7337 	    SATA_ATAPI_TRANS_VERSION(inq));
7338 	cmn_err(CE_NOTE, "response data format %d, aenc %d",
7339 	    inq->inq_rdf, inq->inq_aenc);
7340 	cmn_err(CE_NOTE, " additional length %d", inq->inq_len);
7341 	cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs);
7342 	p = (uint8_t *)inq->inq_vid;
7343 	cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x "
7344 	    "%02x %02x %02x %02x",
7345 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
7346 	p = (uint8_t *)inq->inq_vid;
7347 	cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c",
7348 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
7349 
7350 	p = (uint8_t *)inq->inq_pid;
7351 	cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x "
7352 	    "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
7353 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7354 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7355 	p = (uint8_t *)inq->inq_pid;
7356 	cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c "
7357 	    "%c %c %c %c %c %c %c %c",
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 
7361 	p = (uint8_t *)inq->inq_revision;
7362 	cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x",
7363 	    p[0], p[1], p[2], p[3]);
7364 	p = (uint8_t *)inq->inq_revision;
7365 	cmn_err(CE_NOTE, "revision: %c %c %c %c",
7366 	    p[0], p[1], p[2], p[3]);
7367 
7368 }
7369 
7370 
7371 static void
7372 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count)
7373 {
7374 	struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt;
7375 
7376 	if (scsi_pkt == NULL)
7377 		return;
7378 	if (count != 0) {
7379 		/* saving cdb */
7380 		bzero(sata_atapi_trace[sata_atapi_trace_index].acdb,
7381 		    SATA_ATAPI_MAX_CDB_LEN);
7382 		bcopy(scsi_pkt->pkt_cdbp,
7383 		    sata_atapi_trace[sata_atapi_trace_index].acdb, count);
7384 	} else {
7385 		bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)->
7386 		    sts_sensedata,
7387 		    sata_atapi_trace[sata_atapi_trace_index].arqs,
7388 		    SATA_ATAPI_MIN_RQSENSE_LEN);
7389 		sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason =
7390 		    scsi_pkt->pkt_reason;
7391 		sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason =
7392 		    spx->txlt_sata_pkt->satapkt_reason;
7393 
7394 		if (++sata_atapi_trace_index >= 64)
7395 			sata_atapi_trace_index = 0;
7396 	}
7397 }
7398 
7399 #endif
7400 
7401 /*
7402  * Fetch inquiry data from ATAPI device
7403  * Returns SATA_SUCCESS if operation was successfull, SATA_FAILURE otherwise.
7404  *
7405  * Note:
7406  * inqb pointer does not point to a DMA-able buffer. It is a local buffer
7407  * where the caller expects to see the inquiry data.
7408  *
7409  */
7410 
7411 static int
7412 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba,
7413     sata_address_t *saddr, struct scsi_inquiry *inq)
7414 {
7415 	sata_pkt_txlate_t *spx;
7416 	sata_pkt_t *spkt;
7417 	struct buf *bp;
7418 	sata_drive_info_t *sdinfo;
7419 	sata_cmd_t *scmd;
7420 	int rval;
7421 	uint8_t *rqsp;
7422 #ifdef SATA_DEBUG
7423 	char msg_buf[MAXPATHLEN];
7424 #endif
7425 
7426 	ASSERT(sata_hba != NULL);
7427 
7428 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
7429 	spx->txlt_sata_hba_inst = sata_hba;
7430 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
7431 	spkt = sata_pkt_alloc(spx, NULL);
7432 	if (spkt == NULL) {
7433 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7434 		return (SATA_FAILURE);
7435 	}
7436 	/* address is needed now */
7437 	spkt->satapkt_device.satadev_addr = *saddr;
7438 
7439 	/* scsi_inquiry size buffer */
7440 	bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry));
7441 	if (bp == NULL) {
7442 		sata_pkt_free(spx);
7443 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7444 		SATA_LOG_D((sata_hba, CE_WARN,
7445 		    "sata_get_atapi_inquiry_data: "
7446 		    "cannot allocate data buffer"));
7447 		return (SATA_FAILURE);
7448 	}
7449 	bp_mapin(bp); /* make data buffer accessible */
7450 
7451 	scmd = &spkt->satapkt_cmd;
7452 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7453 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7454 
7455 	/* Use synchronous mode */
7456 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7457 	spkt->satapkt_comp = NULL;
7458 	spkt->satapkt_time = sata_default_pkt_time;
7459 
7460 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
7461 
7462 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7463 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7464 
7465 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
7466 	sdinfo = sata_get_device_info(sata_hba,
7467 	    &spx->txlt_sata_pkt->satapkt_device);
7468 	if (sdinfo == NULL) {
7469 		/* we have to be carefull about the disapearing device */
7470 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7471 		rval = SATA_FAILURE;
7472 		goto cleanup;
7473 	}
7474 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7475 
7476 	/*
7477 	 * Set-up acdb. This works for atapi transport version 2 and later.
7478 	 */
7479 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7480 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7481 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
7482 	scmd->satacmd_acdb[1] = 0x00;
7483 	scmd->satacmd_acdb[2] = 0x00;
7484 	scmd->satacmd_acdb[3] = 0x00;
7485 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
7486 	scmd->satacmd_acdb[5] = 0x00;
7487 
7488 	sata_fixed_sense_data_preset(
7489 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7490 
7491 	/* Transfer command to HBA */
7492 	if (sata_hba_start(spx, &rval) != 0) {
7493 		/* Pkt not accepted for execution */
7494 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
7495 		    "sata_get_atapi_inquiry_data: "
7496 		    "Packet not accepted for execution - ret: %02x", rval);
7497 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7498 		rval = SATA_FAILURE;
7499 		goto cleanup;
7500 	}
7501 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7502 
7503 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
7504 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
7505 		    "sata_get_atapi_inquiry_data: "
7506 		    "Packet completed successfully - ret: %02x", rval);
7507 		/*
7508 		 * Sync buffer. Handle is in usual place in translate struct.
7509 		 * Normal completion - copy data into caller's buffer
7510 		 */
7511 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
7512 		    DDI_DMA_SYNC_FORCPU);
7513 		ASSERT(rval == DDI_SUCCESS);
7514 		bcopy(bp->b_un.b_addr, (uint8_t *)inq,
7515 		    sizeof (struct scsi_inquiry));
7516 #ifdef SATA_DEBUG
7517 		if (sata_debug_flags & SATA_DBG_ATAPI) {
7518 			sata_show_inqry_data((uint8_t *)inq);
7519 		}
7520 #endif
7521 		rval = SATA_SUCCESS;
7522 	} else {
7523 		/*
7524 		 * Something went wrong - analyze return - check rqsense data
7525 		 */
7526 		rval = SATA_FAILURE;
7527 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7528 			/*
7529 			 * ARQ data hopefull show something other than NO SENSE
7530 			 */
7531 			rqsp = scmd->satacmd_rqsense;
7532 #ifdef SATA_DEBUG
7533 			if (sata_debug_flags & SATA_DBG_ATAPI) {
7534 				msg_buf[0] = '\0';
7535 				(void) snprintf(msg_buf, MAXPATHLEN,
7536 				    "ATAPI packet completion reason: %02x\n"
7537 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x\n"
7538 				    "          %02x %02x %02x %02x %02x %02x\n"
7539 				    "          %02x %02x %02x %02x %02x %02x",
7540 				    spkt->satapkt_reason,
7541 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7542 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7543 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7544 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7545 				    rqsp[16], rqsp[17]);
7546 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7547 				    "%s", msg_buf);
7548 			}
7549 #endif
7550 		} else {
7551 			switch (spkt->satapkt_reason) {
7552 			case SATA_PKT_PORT_ERROR:
7553 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7554 				    "sata_get_atapi_inquiry_data: "
7555 				    "packet reason: port error", NULL);
7556 				break;
7557 
7558 			case SATA_PKT_TIMEOUT:
7559 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7560 				    "sata_get_atapi_inquiry_data: "
7561 				    "packet reason: timeout", NULL);
7562 				break;
7563 
7564 			case SATA_PKT_ABORTED:
7565 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7566 				    "sata_get_atapi_inquiry_data: "
7567 				    "packet reason: aborted", NULL);
7568 				break;
7569 
7570 			case SATA_PKT_RESET:
7571 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7572 				    "sata_get_atapi_inquiry_data: "
7573 				    "packet reason: reset\n", NULL);
7574 				break;
7575 			default:
7576 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7577 				    "sata_get_atapi_inquiry_data: "
7578 				    "invalid packet reason: %02x\n",
7579 				    spkt->satapkt_reason);
7580 				break;
7581 			}
7582 		}
7583 	}
7584 cleanup:
7585 	sata_free_local_buffer(spx);
7586 	sata_pkt_free(spx);
7587 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
7588 	return (rval);
7589 }
7590 
7591 
7592 
7593 
7594 
7595 #if 0
7596 #ifdef SATA_DEBUG
7597 
7598 /*
7599  * Test ATAPI packet command.
7600  * Single threaded test: send packet command in synch mode, process completion
7601  *
7602  */
7603 static void
7604 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport)
7605 {
7606 	sata_pkt_txlate_t *spx;
7607 	sata_pkt_t *spkt;
7608 	struct buf *bp;
7609 	sata_device_t sata_device;
7610 	sata_drive_info_t *sdinfo;
7611 	sata_cmd_t *scmd;
7612 	int rval;
7613 	uint8_t *rqsp;
7614 
7615 	ASSERT(sata_hba_inst != NULL);
7616 	sata_device.satadev_addr.cport = cport;
7617 	sata_device.satadev_addr.pmport = 0;
7618 	sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
7619 	sata_device.satadev_rev = SATA_DEVICE_REV;
7620 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7621 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
7622 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7623 	if (sdinfo == NULL) {
7624 		sata_log(sata_hba_inst, CE_WARN,
7625 		    "sata_test_atapi_packet_command: "
7626 		    "no device info for cport %d",
7627 		    sata_device.satadev_addr.cport);
7628 		return;
7629 	}
7630 
7631 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
7632 	spx->txlt_sata_hba_inst = sata_hba_inst;
7633 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
7634 	spkt = sata_pkt_alloc(spx, NULL);
7635 	if (spkt == NULL) {
7636 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7637 		return;
7638 	}
7639 	/* address is needed now */
7640 	spkt->satapkt_device.satadev_addr = sata_device.satadev_addr;
7641 
7642 	/* 1024k buffer */
7643 	bp = sata_alloc_local_buffer(spx, 1024);
7644 	if (bp == NULL) {
7645 		sata_pkt_free(spx);
7646 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7647 		sata_log(sata_hba_inst, CE_WARN,
7648 		    "sata_test_atapi_packet_command: "
7649 		    "cannot allocate data buffer");
7650 		return;
7651 	}
7652 	bp_mapin(bp); /* make data buffer accessible */
7653 
7654 	scmd = &spkt->satapkt_cmd;
7655 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7656 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7657 
7658 	/* Use synchronous mode */
7659 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7660 
7661 	/* Synchronous mode, no callback - may be changed by the caller */
7662 	spkt->satapkt_comp = NULL;
7663 	spkt->satapkt_time = sata_default_pkt_time;
7664 
7665 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
7666 
7667 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7668 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7669 
7670 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7671 
7672 	/* Set-up acdb. */
7673 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7674 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7675 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
7676 	scmd->satacmd_acdb[1] = 0x00;
7677 	scmd->satacmd_acdb[2] = 0x00;
7678 	scmd->satacmd_acdb[3] = 0x00;
7679 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
7680 	scmd->satacmd_acdb[5] = 0x00;
7681 
7682 	sata_fixed_sense_data_preset(
7683 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7684 
7685 	/* Transfer command to HBA */
7686 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7687 	if (sata_hba_start(spx, &rval) != 0) {
7688 		/* Pkt not accepted for execution */
7689 		sata_log(sata_hba_inst, CE_WARN,
7690 		    "sata_test_atapi_packet_command: "
7691 		    "Packet not accepted for execution - ret: %02x", rval);
7692 		mutex_exit(
7693 		    &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7694 		goto cleanup;
7695 	}
7696 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7697 
7698 	/*
7699 	 * Sync buffer. Handle is in usual place in translate struct.
7700 	 */
7701 	rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
7702 	    DDI_DMA_SYNC_FORCPU);
7703 	ASSERT(rval == DDI_SUCCESS);
7704 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
7705 		sata_log(sata_hba_inst, CE_WARN,
7706 		    "sata_test_atapi_packet_command: "
7707 		    "Packet completed successfully");
7708 		/*
7709 		 * Normal completion - show inquiry data
7710 		 */
7711 		sata_show_inqry_data((uint8_t *)bp->b_un.b_addr);
7712 	} else {
7713 		/*
7714 		 * Something went wrong - analyze return - check rqsense data
7715 		 */
7716 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7717 			/*
7718 			 * ARQ data hopefull show something other than NO SENSE
7719 			 */
7720 			rqsp = scmd->satacmd_rqsense;
7721 			sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7722 			    "ATAPI packet completion reason: %02x\n"
7723 			    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
7724 			    "          %02x %02x %02x %02x %02x %02x "
7725 			    "          %02x %02x %02x %02x %02x %02x\n",
7726 			    spkt->satapkt_reason,
7727 			    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7728 			    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7729 			    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7730 			    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7731 			    rqsp[16], rqsp[17]);
7732 		} else {
7733 			switch (spkt->satapkt_reason) {
7734 			case SATA_PKT_PORT_ERROR:
7735 				sata_log(sata_hba_inst, CE_WARN,
7736 				    "sata_test_atapi_packet_command: "
7737 				    "packet reason: port error\n");
7738 				break;
7739 
7740 			case SATA_PKT_TIMEOUT:
7741 				sata_log(sata_hba_inst, CE_WARN,
7742 				    "sata_test_atapi_packet_command: "
7743 				    "packet reason: timeout\n");
7744 				break;
7745 
7746 			case SATA_PKT_ABORTED:
7747 				sata_log(sata_hba_inst, CE_WARN,
7748 				    "sata_test_atapi_packet_command: "
7749 				    "packet reason: aborted\n");
7750 				break;
7751 
7752 			case SATA_PKT_RESET:
7753 				sata_log(sata_hba_inst, CE_WARN,
7754 				    "sata_test_atapi_packet_command: "
7755 				    "packet reason: reset\n");
7756 				break;
7757 			default:
7758 				sata_log(sata_hba_inst, CE_WARN,
7759 				    "sata_test_atapi_packet_command: "
7760 				    "invalid packet reason: %02x\n",
7761 				    spkt->satapkt_reason);
7762 				break;
7763 			}
7764 		}
7765 	}
7766 cleanup:
7767 	sata_free_local_buffer(spx);
7768 	sata_pkt_free(spx);
7769 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
7770 }
7771 
7772 #endif /* SATA_DEBUG */
7773 #endif /* 1 */
7774 
7775 
7776 /* ************************** LOCAL HELPER FUNCTIONS *********************** */
7777 
7778 /*
7779  * Validate sata_tran info
7780  * SATA_FAILURE returns if structure is inconsistent or structure revision
7781  * does not match one used by the framework.
7782  *
7783  * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains
7784  * required function pointers.
7785  * Returns SATA_FAILURE otherwise.
7786  */
7787 static int
7788 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran)
7789 {
7790 	/*
7791 	 * SATA_TRAN_HBA_REV is the current (highest) revision number
7792 	 * of the SATA interface.
7793 	 */
7794 	if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) {
7795 		sata_log(NULL, CE_WARN,
7796 		    "sata: invalid sata_hba_tran version %d for driver %s",
7797 		    sata_tran->sata_tran_hba_rev, ddi_driver_name(dip));
7798 		return (SATA_FAILURE);
7799 	}
7800 
7801 	if (dip != sata_tran->sata_tran_hba_dip) {
7802 		SATA_LOG_D((NULL, CE_WARN,
7803 		    "sata: inconsistent sata_tran_hba_dip "
7804 		    "%p / %p", sata_tran->sata_tran_hba_dip, dip));
7805 		return (SATA_FAILURE);
7806 	}
7807 
7808 	if (sata_tran->sata_tran_probe_port == NULL ||
7809 	    sata_tran->sata_tran_start == NULL ||
7810 	    sata_tran->sata_tran_abort == NULL ||
7811 	    sata_tran->sata_tran_reset_dport == NULL ||
7812 	    sata_tran->sata_tran_hotplug_ops == NULL ||
7813 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL ||
7814 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate ==
7815 	    NULL) {
7816 		SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing "
7817 		    "required functions"));
7818 	}
7819 	return (SATA_SUCCESS);
7820 }
7821 
7822 /*
7823  * Remove HBA instance from sata_hba_list.
7824  */
7825 static void
7826 sata_remove_hba_instance(dev_info_t *dip)
7827 {
7828 	sata_hba_inst_t	*sata_hba_inst;
7829 
7830 	mutex_enter(&sata_mutex);
7831 	for (sata_hba_inst = sata_hba_list;
7832 	    sata_hba_inst != (struct sata_hba_inst *)NULL;
7833 	    sata_hba_inst = sata_hba_inst->satahba_next) {
7834 		if (sata_hba_inst->satahba_dip == dip)
7835 			break;
7836 	}
7837 
7838 	if (sata_hba_inst == (struct sata_hba_inst *)NULL) {
7839 #ifdef SATA_DEBUG
7840 		cmn_err(CE_WARN, "sata_remove_hba_instance: "
7841 		    "unknown HBA instance\n");
7842 #endif
7843 		ASSERT(FALSE);
7844 	}
7845 	if (sata_hba_inst == sata_hba_list) {
7846 		sata_hba_list = sata_hba_inst->satahba_next;
7847 		if (sata_hba_list) {
7848 			sata_hba_list->satahba_prev =
7849 			    (struct sata_hba_inst *)NULL;
7850 		}
7851 		if (sata_hba_inst == sata_hba_list_tail) {
7852 			sata_hba_list_tail = NULL;
7853 		}
7854 	} else if (sata_hba_inst == sata_hba_list_tail) {
7855 		sata_hba_list_tail = sata_hba_inst->satahba_prev;
7856 		if (sata_hba_list_tail) {
7857 			sata_hba_list_tail->satahba_next =
7858 			    (struct sata_hba_inst *)NULL;
7859 		}
7860 	} else {
7861 		sata_hba_inst->satahba_prev->satahba_next =
7862 		    sata_hba_inst->satahba_next;
7863 		sata_hba_inst->satahba_next->satahba_prev =
7864 		    sata_hba_inst->satahba_prev;
7865 	}
7866 	mutex_exit(&sata_mutex);
7867 }
7868 
7869 
7870 
7871 
7872 
7873 /*
7874  * Probe all SATA ports of the specified HBA instance.
7875  * The assumption is that there are no target and attachment point minor nodes
7876  * created by the boot subsystems, so we do not need to prune device tree.
7877  *
7878  * This function is called only from sata_hba_attach(). It does not have to
7879  * be protected by controller mutex, because the hba_attached flag is not set
7880  * yet and no one would be touching this HBA instance other than this thread.
7881  * Determines if port is active and what type of the device is attached
7882  * (if any). Allocates necessary structures for each port.
7883  *
7884  * An AP (Attachement Point) node is created for each SATA device port even
7885  * when there is no device attached.
7886  */
7887 
7888 static 	void
7889 sata_probe_ports(sata_hba_inst_t *sata_hba_inst)
7890 {
7891 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
7892 	int			ncport, npmport;
7893 	sata_cport_info_t 	*cportinfo;
7894 	sata_drive_info_t	*drive;
7895 	sata_pmult_info_t	*pminfo;
7896 	sata_pmport_info_t 	*pmportinfo;
7897 	sata_device_t		sata_device;
7898 	int			rval;
7899 	dev_t			minor_number;
7900 	char			name[16];
7901 	clock_t			start_time, cur_time;
7902 
7903 	/*
7904 	 * Probe controller ports first, to find port status and
7905 	 * any port multiplier attached.
7906 	 */
7907 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
7908 		/* allocate cport structure */
7909 		cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP);
7910 		ASSERT(cportinfo != NULL);
7911 		mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL);
7912 
7913 		mutex_enter(&cportinfo->cport_mutex);
7914 
7915 		cportinfo->cport_addr.cport = ncport;
7916 		cportinfo->cport_addr.pmport = 0;
7917 		cportinfo->cport_addr.qual = SATA_ADDR_CPORT;
7918 		cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
7919 		cportinfo->cport_state |= SATA_STATE_PROBING;
7920 		SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo;
7921 
7922 		/*
7923 		 * Regardless if a port is usable or not, create
7924 		 * an attachment point
7925 		 */
7926 		mutex_exit(&cportinfo->cport_mutex);
7927 		minor_number =
7928 		    SATA_MAKE_AP_MINOR(ddi_get_instance(dip), ncport, 0, 0);
7929 		(void) sprintf(name, "%d", ncport);
7930 		if (ddi_create_minor_node(dip, name, S_IFCHR,
7931 		    minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) !=
7932 		    DDI_SUCCESS) {
7933 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
7934 			    "cannot create SATA attachment point for port %d",
7935 			    ncport);
7936 		}
7937 
7938 		/* Probe port */
7939 		start_time = ddi_get_lbolt();
7940 	reprobe_cport:
7941 		sata_device.satadev_addr.cport = ncport;
7942 		sata_device.satadev_addr.pmport = 0;
7943 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
7944 		sata_device.satadev_rev = SATA_DEVICE_REV;
7945 
7946 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
7947 		    (dip, &sata_device);
7948 
7949 		mutex_enter(&cportinfo->cport_mutex);
7950 		sata_update_port_scr(&cportinfo->cport_scr, &sata_device);
7951 		if (rval != SATA_SUCCESS) {
7952 			/* Something went wrong? Fail the port */
7953 			cportinfo->cport_state = SATA_PSTATE_FAILED;
7954 			mutex_exit(&cportinfo->cport_mutex);
7955 			continue;
7956 		}
7957 		cportinfo->cport_state &= ~SATA_STATE_PROBING;
7958 		cportinfo->cport_state |= SATA_STATE_PROBED;
7959 		cportinfo->cport_dev_type = sata_device.satadev_type;
7960 
7961 		cportinfo->cport_state |= SATA_STATE_READY;
7962 		if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) {
7963 			mutex_exit(&cportinfo->cport_mutex);
7964 			continue;
7965 		}
7966 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
7967 			/*
7968 			 * There is some device attached.
7969 			 * Allocate device info structure
7970 			 */
7971 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) {
7972 				mutex_exit(&cportinfo->cport_mutex);
7973 				SATA_CPORTINFO_DRV_INFO(cportinfo) =
7974 				    kmem_zalloc(sizeof (sata_drive_info_t),
7975 				    KM_SLEEP);
7976 				mutex_enter(&cportinfo->cport_mutex);
7977 			}
7978 			drive = SATA_CPORTINFO_DRV_INFO(cportinfo);
7979 			drive->satadrv_addr = cportinfo->cport_addr;
7980 			drive->satadrv_addr.qual = SATA_ADDR_DCPORT;
7981 			drive->satadrv_type = cportinfo->cport_dev_type;
7982 			drive->satadrv_state = SATA_STATE_UNKNOWN;
7983 
7984 			mutex_exit(&cportinfo->cport_mutex);
7985 			if (sata_add_device(dip, sata_hba_inst, ncport, 0) !=
7986 			    SATA_SUCCESS) {
7987 				/*
7988 				 * Plugged device was not correctly identified.
7989 				 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT
7990 				 */
7991 				cur_time = ddi_get_lbolt();
7992 				if ((cur_time - start_time) <
7993 				    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
7994 					/* sleep for a while */
7995 					delay(drv_usectohz(
7996 					    SATA_DEV_RETRY_DLY));
7997 					goto reprobe_cport;
7998 				}
7999 			}
8000 		} else {
8001 			mutex_exit(&cportinfo->cport_mutex);
8002 			ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT);
8003 			pminfo = kmem_zalloc(sizeof (sata_pmult_info_t),
8004 			    KM_SLEEP);
8005 			mutex_enter(&cportinfo->cport_mutex);
8006 			ASSERT(pminfo != NULL);
8007 			SATA_CPORTINFO_PMULT_INFO(cportinfo) = pminfo;
8008 			pminfo->pmult_addr.cport = cportinfo->cport_addr.cport;
8009 			pminfo->pmult_addr.pmport = SATA_PMULT_HOSTPORT;
8010 			pminfo->pmult_addr.qual = SATA_ADDR_PMPORT;
8011 			pminfo->pmult_num_dev_ports =
8012 			    sata_device.satadev_add_info;
8013 			mutex_init(&pminfo->pmult_mutex, NULL, MUTEX_DRIVER,
8014 			    NULL);
8015 			pminfo->pmult_state = SATA_STATE_PROBING;
8016 			mutex_exit(&cportinfo->cport_mutex);
8017 
8018 			/* Probe Port Multiplier ports */
8019 			for (npmport = 0;
8020 			    npmport < pminfo->pmult_num_dev_ports;
8021 			    npmport++) {
8022 				pmportinfo = kmem_zalloc(
8023 				    sizeof (sata_pmport_info_t), KM_SLEEP);
8024 				mutex_enter(&cportinfo->cport_mutex);
8025 				ASSERT(pmportinfo != NULL);
8026 				pmportinfo->pmport_addr.cport = ncport;
8027 				pmportinfo->pmport_addr.pmport = npmport;
8028 				pmportinfo->pmport_addr.qual =
8029 				    SATA_ADDR_PMPORT;
8030 				pminfo->pmult_dev_port[npmport] = pmportinfo;
8031 
8032 				mutex_init(&pmportinfo->pmport_mutex, NULL,
8033 				    MUTEX_DRIVER, NULL);
8034 
8035 				mutex_exit(&cportinfo->cport_mutex);
8036 
8037 				/* Create an attachment point */
8038 				minor_number = SATA_MAKE_AP_MINOR(
8039 				    ddi_get_instance(dip), ncport, npmport, 1);
8040 				(void) sprintf(name, "%d.%d", ncport, npmport);
8041 				if (ddi_create_minor_node(dip, name, S_IFCHR,
8042 				    minor_number, DDI_NT_SATA_ATTACHMENT_POINT,
8043 				    0) != DDI_SUCCESS) {
8044 					sata_log(sata_hba_inst, CE_WARN,
8045 					    "sata_hba_attach: "
8046 					    "cannot create SATA attachment "
8047 					    "point for port %d pmult port %d",
8048 					    ncport, npmport);
8049 				}
8050 
8051 				start_time = ddi_get_lbolt();
8052 			reprobe_pmport:
8053 				sata_device.satadev_addr.pmport = npmport;
8054 				sata_device.satadev_addr.qual =
8055 				    SATA_ADDR_PMPORT;
8056 
8057 				rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8058 				    (dip, &sata_device);
8059 				mutex_enter(&cportinfo->cport_mutex);
8060 
8061 				/* sata_update_port_info() */
8062 				sata_update_port_scr(&pmportinfo->pmport_scr,
8063 				    &sata_device);
8064 
8065 				if (rval != SATA_SUCCESS) {
8066 					pmportinfo->pmport_state =
8067 					    SATA_PSTATE_FAILED;
8068 					mutex_exit(&cportinfo->cport_mutex);
8069 					continue;
8070 				}
8071 				pmportinfo->pmport_state &=
8072 				    ~SATA_STATE_PROBING;
8073 				pmportinfo->pmport_state |= SATA_STATE_PROBED;
8074 				pmportinfo->pmport_dev_type =
8075 				    sata_device.satadev_type;
8076 
8077 				pmportinfo->pmport_state |= SATA_STATE_READY;
8078 				if (pmportinfo->pmport_dev_type ==
8079 				    SATA_DTYPE_NONE) {
8080 					mutex_exit(&cportinfo->cport_mutex);
8081 					continue;
8082 				}
8083 				/* Port multipliers cannot be chained */
8084 				ASSERT(pmportinfo->pmport_dev_type !=
8085 				    SATA_DTYPE_PMULT);
8086 				/*
8087 				 * There is something attached to Port
8088 				 * Multiplier device port
8089 				 * Allocate device info structure
8090 				 */
8091 				if (pmportinfo->pmport_sata_drive == NULL) {
8092 					mutex_exit(&cportinfo->cport_mutex);
8093 					pmportinfo->pmport_sata_drive =
8094 					    kmem_zalloc(
8095 					    sizeof (sata_drive_info_t),
8096 					    KM_SLEEP);
8097 					mutex_enter(&cportinfo->cport_mutex);
8098 				}
8099 				drive = pmportinfo->pmport_sata_drive;
8100 				drive->satadrv_addr.cport =
8101 				    pmportinfo->pmport_addr.cport;
8102 				drive->satadrv_addr.pmport = npmport;
8103 				drive->satadrv_addr.qual = SATA_ADDR_DPMPORT;
8104 				drive->satadrv_type = pmportinfo->
8105 				    pmport_dev_type;
8106 				drive->satadrv_state = SATA_STATE_UNKNOWN;
8107 
8108 				mutex_exit(&cportinfo->cport_mutex);
8109 				if (sata_add_device(dip, sata_hba_inst, ncport,
8110 				    npmport) != SATA_SUCCESS) {
8111 					/*
8112 					 * Plugged device was not correctly
8113 					 * identified. Retry, within the
8114 					 * SATA_DEV_IDENTIFY_TIMEOUT
8115 					 */
8116 					cur_time = ddi_get_lbolt();
8117 					if ((cur_time - start_time) <
8118 					    drv_usectohz(
8119 					    SATA_DEV_IDENTIFY_TIMEOUT)) {
8120 						/* sleep for a while */
8121 						delay(drv_usectohz(
8122 						    SATA_DEV_RETRY_DLY));
8123 						goto reprobe_pmport;
8124 					}
8125 				}
8126 			}
8127 			pmportinfo->pmport_state =
8128 			    SATA_STATE_PROBED | SATA_STATE_READY;
8129 		}
8130 	}
8131 }
8132 
8133 /*
8134  * Add SATA device for specified HBA instance & port (SCSI target
8135  * device nodes).
8136  * This function is called (indirectly) only from sata_hba_attach().
8137  * A target node is created when there is a supported type device attached,
8138  * but may be removed if it cannot be put online.
8139  *
8140  * This function cannot be called from an interrupt context.
8141  *
8142  * ONLY DISK TARGET NODES ARE CREATED NOW
8143  *
8144  * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when
8145  * device identification failed - adding a device could be retried.
8146  *
8147  */
8148 static 	int
8149 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst, int cport,
8150     int pmport)
8151 {
8152 	sata_cport_info_t 	*cportinfo;
8153 	sata_pmult_info_t	*pminfo;
8154 	sata_pmport_info_t	*pmportinfo;
8155 	dev_info_t		*cdip;		/* child dip */
8156 	sata_device_t		sata_device;
8157 	int			rval;
8158 
8159 
8160 
8161 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
8162 	ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE);
8163 	mutex_enter(&cportinfo->cport_mutex);
8164 	/*
8165 	 * Some device is attached to a controller port.
8166 	 * We rely on controllers distinquishing between no-device,
8167 	 * attached port multiplier and other kind of attached device.
8168 	 * We need to get Identify Device data and determine
8169 	 * positively the dev type before trying to attach
8170 	 * the target driver.
8171 	 */
8172 	sata_device.satadev_rev = SATA_DEVICE_REV;
8173 	if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
8174 		/*
8175 		 * Not port multiplier.
8176 		 */
8177 		sata_device.satadev_addr = cportinfo->cport_addr;
8178 		sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
8179 		mutex_exit(&cportinfo->cport_mutex);
8180 
8181 		rval = sata_probe_device(sata_hba_inst, &sata_device);
8182 		if (rval != SATA_SUCCESS ||
8183 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN)
8184 			return (SATA_FAILURE);
8185 
8186 		mutex_enter(&cportinfo->cport_mutex);
8187 		sata_show_drive_info(sata_hba_inst,
8188 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
8189 
8190 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
8191 			/*
8192 			 * Could not determine device type or
8193 			 * a device is not supported.
8194 			 * Degrade this device to unknown.
8195 			 */
8196 			cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8197 			mutex_exit(&cportinfo->cport_mutex);
8198 			return (SATA_SUCCESS);
8199 		}
8200 		cportinfo->cport_dev_type = sata_device.satadev_type;
8201 		cportinfo->cport_tgtnode_clean = B_TRUE;
8202 		mutex_exit(&cportinfo->cport_mutex);
8203 
8204 		/*
8205 		 * Initialize device to the desired state. Even if it
8206 		 * fails, the device will still attach but syslog
8207 		 * will show the warning.
8208 		 */
8209 		if (sata_initialize_device(sata_hba_inst,
8210 		    SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS)
8211 			/* Retry */
8212 			(void) sata_initialize_device(sata_hba_inst,
8213 			    SATA_CPORTINFO_DRV_INFO(cportinfo));
8214 
8215 		cdip = sata_create_target_node(pdip, sata_hba_inst,
8216 		    &sata_device.satadev_addr);
8217 		mutex_enter(&cportinfo->cport_mutex);
8218 		if (cdip == NULL) {
8219 			/*
8220 			 * Attaching target node failed.
8221 			 * We retain sata_drive_info structure...
8222 			 */
8223 			mutex_exit(&cportinfo->cport_mutex);
8224 			return (SATA_SUCCESS);
8225 		}
8226 		(SATA_CPORTINFO_DRV_INFO(cportinfo))->
8227 		    satadrv_state = SATA_STATE_READY;
8228 	} else {
8229 		/* This must be Port Multiplier type */
8230 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
8231 			SATA_LOG_D((sata_hba_inst, CE_WARN,
8232 			    "sata_add_device: "
8233 			    "unrecognized dev type %x",
8234 			    cportinfo->cport_dev_type));
8235 			mutex_exit(&cportinfo->cport_mutex);
8236 			return (SATA_SUCCESS);
8237 		}
8238 		pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
8239 		pmportinfo = pminfo->pmult_dev_port[pmport];
8240 		sata_device.satadev_addr = pmportinfo->pmport_addr;
8241 		sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
8242 		mutex_exit(&cportinfo->cport_mutex);
8243 
8244 		rval = sata_probe_device(sata_hba_inst, &sata_device);
8245 		if (rval != SATA_SUCCESS ||
8246 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN) {
8247 			return (SATA_FAILURE);
8248 		}
8249 		mutex_enter(&cportinfo->cport_mutex);
8250 		sata_show_drive_info(sata_hba_inst,
8251 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
8252 
8253 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
8254 			/*
8255 			 * Could not determine device type.
8256 			 * Degrade this device to unknown.
8257 			 */
8258 			pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
8259 			mutex_exit(&cportinfo->cport_mutex);
8260 			return (SATA_SUCCESS);
8261 		}
8262 		pmportinfo->pmport_dev_type = sata_device.satadev_type;
8263 		pmportinfo->pmport_tgtnode_clean = B_TRUE;
8264 		mutex_exit(&cportinfo->cport_mutex);
8265 
8266 		/*
8267 		 * Initialize device to the desired state.
8268 		 * Even if it fails, the device will still
8269 		 * attach but syslog will show the warning.
8270 		 */
8271 		if (sata_initialize_device(sata_hba_inst,
8272 		    pmportinfo->pmport_sata_drive) != SATA_SUCCESS)
8273 			/* Retry */
8274 			(void) sata_initialize_device(sata_hba_inst,
8275 			    pmportinfo->pmport_sata_drive);
8276 
8277 		cdip = sata_create_target_node(pdip, sata_hba_inst,
8278 		    &sata_device.satadev_addr);
8279 		mutex_enter(&cportinfo->cport_mutex);
8280 		if (cdip == NULL) {
8281 			/*
8282 			 * Attaching target node failed.
8283 			 * We retain sata_drive_info structure...
8284 			 */
8285 			mutex_exit(&cportinfo->cport_mutex);
8286 			return (SATA_SUCCESS);
8287 		}
8288 		pmportinfo->pmport_sata_drive->satadrv_state |=
8289 		    SATA_STATE_READY;
8290 	}
8291 	mutex_exit(&cportinfo->cport_mutex);
8292 	return (SATA_SUCCESS);
8293 }
8294 
8295 
8296 
8297 /*
8298  * Create scsi target node for attached device, create node properties and
8299  * attach the node.
8300  * The node could be removed if the device onlining fails.
8301  *
8302  * A dev_info_t pointer is returned if operation is successful, NULL is
8303  * returned otherwise.
8304  *
8305  * No port multiplier support.
8306  */
8307 
8308 static dev_info_t *
8309 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst,
8310 			sata_address_t *sata_addr)
8311 {
8312 	dev_info_t *cdip = NULL;
8313 	int rval;
8314 	char *nname = NULL;
8315 	char **compatible = NULL;
8316 	int ncompatible;
8317 	struct scsi_inquiry inq;
8318 	sata_device_t sata_device;
8319 	sata_drive_info_t *sdinfo;
8320 	int target;
8321 	int i;
8322 
8323 	sata_device.satadev_rev = SATA_DEVICE_REV;
8324 	sata_device.satadev_addr = *sata_addr;
8325 
8326 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport)));
8327 
8328 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
8329 
8330 	target = SATA_TO_SCSI_TARGET(sata_addr->cport,
8331 	    sata_addr->pmport, sata_addr->qual);
8332 
8333 	if (sdinfo == NULL) {
8334 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8335 		    sata_addr->cport)));
8336 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8337 		    "sata_create_target_node: no sdinfo for target %x",
8338 		    target));
8339 		return (NULL);
8340 	}
8341 
8342 	/*
8343 	 * create or get scsi inquiry data, expected by
8344 	 * scsi_hba_nodename_compatible_get()
8345 	 * SATA hard disks get Identify Data translated into Inguiry Data.
8346 	 * ATAPI devices respond directly to Inquiry request.
8347 	 */
8348 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
8349 		sata_identdev_to_inquiry(sata_hba_inst, sdinfo,
8350 		    (uint8_t *)&inq);
8351 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8352 		    sata_addr->cport)));
8353 	} else { /* Assume supported ATAPI device */
8354 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8355 		    sata_addr->cport)));
8356 		if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr,
8357 		    &inq) == SATA_FAILURE)
8358 			return (NULL);
8359 		/*
8360 		 * Save supported ATAPI transport version
8361 		 */
8362 		sdinfo->satadrv_atapi_trans_ver =
8363 		    SATA_ATAPI_TRANS_VERSION(&inq);
8364 	}
8365 
8366 	/* determine the node name and compatible */
8367 	scsi_hba_nodename_compatible_get(&inq, NULL,
8368 	    inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
8369 
8370 #ifdef SATA_DEBUG
8371 	if (sata_debug_flags & SATA_DBG_NODES) {
8372 		if (nname == NULL) {
8373 			cmn_err(CE_NOTE, "sata_create_target_node: "
8374 			    "cannot determine nodename for target %d\n",
8375 			    target);
8376 		} else {
8377 			cmn_err(CE_WARN, "sata_create_target_node: "
8378 			    "target %d nodename: %s\n", target, nname);
8379 		}
8380 		if (compatible == NULL) {
8381 			cmn_err(CE_WARN,
8382 			    "sata_create_target_node: no compatible name\n");
8383 		} else {
8384 			for (i = 0; i < ncompatible; i++) {
8385 				cmn_err(CE_WARN, "sata_create_target_node: "
8386 				    "compatible name: %s\n", compatible[i]);
8387 			}
8388 		}
8389 	}
8390 #endif
8391 
8392 	/* if nodename can't be determined, log error and exit */
8393 	if (nname == NULL) {
8394 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8395 		    "sata_create_target_node: cannot determine nodename "
8396 		    "for target %d\n", target));
8397 		scsi_hba_nodename_compatible_free(nname, compatible);
8398 		return (NULL);
8399 	}
8400 	/*
8401 	 * Create scsi target node
8402 	 */
8403 	ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip);
8404 	rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
8405 	    "device-type", "scsi");
8406 
8407 	if (rval != DDI_PROP_SUCCESS) {
8408 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8409 		    "updating device_type prop failed %d", rval));
8410 		goto fail;
8411 	}
8412 
8413 	/*
8414 	 * Create target node properties: target & lun
8415 	 */
8416 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target);
8417 	if (rval != DDI_PROP_SUCCESS) {
8418 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8419 		    "updating target prop failed %d", rval));
8420 		goto fail;
8421 	}
8422 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0);
8423 	if (rval != DDI_PROP_SUCCESS) {
8424 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8425 		    "updating target prop failed %d", rval));
8426 		goto fail;
8427 	}
8428 
8429 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
8430 		/*
8431 		 * Add "variant" property
8432 		 */
8433 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
8434 		    "variant", "atapi");
8435 		if (rval != DDI_PROP_SUCCESS) {
8436 			SATA_LOG_D((sata_hba_inst, CE_WARN,
8437 			    "sata_create_target_node: variant atapi "
8438 			    "property could not be created: %d", rval));
8439 			goto fail;
8440 		}
8441 	}
8442 	/* decorate the node with compatible */
8443 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible",
8444 	    compatible, ncompatible) != DDI_PROP_SUCCESS) {
8445 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8446 		    "sata_create_target_node: FAIL compatible props cdip 0x%p",
8447 		    (void *)cdip));
8448 		goto fail;
8449 	}
8450 
8451 
8452 	/*
8453 	 * Now, try to attach the driver. If probing of the device fails,
8454 	 * the target node may be removed
8455 	 */
8456 	rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH);
8457 
8458 	scsi_hba_nodename_compatible_free(nname, compatible);
8459 
8460 	if (rval == NDI_SUCCESS)
8461 		return (cdip);
8462 
8463 	/* target node was removed - are we sure? */
8464 	return (NULL);
8465 
8466 fail:
8467 	scsi_hba_nodename_compatible_free(nname, compatible);
8468 	ddi_prop_remove_all(cdip);
8469 	rval = ndi_devi_free(cdip);
8470 	if (rval != NDI_SUCCESS) {
8471 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8472 		    "node removal failed %d", rval));
8473 	}
8474 	sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: "
8475 	    "cannot create target node for SATA device at port %d",
8476 	    sata_addr->cport);
8477 	return (NULL);
8478 }
8479 
8480 
8481 
8482 /*
8483  * Re-probe sata port, check for a device and attach info
8484  * structures when necessary. Identify Device data is fetched, if possible.
8485  * Assumption: sata address is already validated.
8486  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
8487  * the presence of a device and its type.
8488  *
8489  * flag arg specifies that the function should try multiple times to identify
8490  * device type and to initialize it, or it should return immediately on failure.
8491  * SATA_DEV_IDENTIFY_RETRY - retry
8492  * SATA_DEV_IDENTIFY_NORETRY - no retry
8493  *
8494  * SATA_FAILURE is returned if one of the operations failed.
8495  *
8496  * This function cannot be called in interrupt context - it may sleep.
8497  *
8498  * NOte: Port multiplier is not supported yet, although there may be some
8499  * pieces of code referencing to it.
8500  */
8501 static int
8502 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
8503     int flag)
8504 {
8505 	sata_cport_info_t *cportinfo;
8506 	sata_drive_info_t *sdinfo, *osdinfo;
8507 	boolean_t init_device = B_FALSE;
8508 	int prev_device_type = SATA_DTYPE_NONE;
8509 	int prev_device_settings = 0;
8510 	int prev_device_state = 0;
8511 	clock_t start_time;
8512 	int retry = B_FALSE;
8513 	int rval;
8514 
8515 	/* We only care about host sata cport for now */
8516 	cportinfo = SATA_CPORT_INFO(sata_hba_inst,
8517 	    sata_device->satadev_addr.cport);
8518 	osdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8519 	if (osdinfo != NULL) {
8520 		/*
8521 		 * We are re-probing port with a previously attached device.
8522 		 * Save previous device type and settings.
8523 		 */
8524 		prev_device_type = cportinfo->cport_dev_type;
8525 		prev_device_settings = osdinfo->satadrv_settings;
8526 		prev_device_state = osdinfo->satadrv_state;
8527 	}
8528 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
8529 		start_time = ddi_get_lbolt();
8530 		retry = B_TRUE;
8531 	}
8532 retry_probe:
8533 
8534 	/* probe port */
8535 	mutex_enter(&cportinfo->cport_mutex);
8536 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
8537 	cportinfo->cport_state |= SATA_STATE_PROBING;
8538 	mutex_exit(&cportinfo->cport_mutex);
8539 
8540 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8541 	    (SATA_DIP(sata_hba_inst), sata_device);
8542 
8543 	mutex_enter(&cportinfo->cport_mutex);
8544 	if (rval != SATA_SUCCESS) {
8545 		cportinfo->cport_state = SATA_PSTATE_FAILED;
8546 		mutex_exit(&cportinfo->cport_mutex);
8547 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: "
8548 		    "SATA port %d probing failed",
8549 		    cportinfo->cport_addr.cport));
8550 		return (SATA_FAILURE);
8551 	}
8552 
8553 	/*
8554 	 * update sata port state and set device type
8555 	 */
8556 	sata_update_port_info(sata_hba_inst, sata_device);
8557 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
8558 
8559 	/*
8560 	 * Sanity check - Port is active? Is the link active?
8561 	 * Is there any device attached?
8562 	 */
8563 	if ((cportinfo->cport_state &
8564 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
8565 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
8566 	    SATA_PORT_DEVLINK_UP) {
8567 		/*
8568 		 * Port in non-usable state or no link active/no device.
8569 		 * Free info structure if necessary (direct attached drive
8570 		 * only, for now!
8571 		 */
8572 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8573 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8574 		/* Add here differentiation for device attached or not */
8575 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8576 		mutex_exit(&cportinfo->cport_mutex);
8577 		if (sdinfo != NULL)
8578 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8579 		return (SATA_SUCCESS);
8580 	}
8581 
8582 	cportinfo->cport_state |= SATA_STATE_READY;
8583 	cportinfo->cport_dev_type = sata_device->satadev_type;
8584 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8585 
8586 	/*
8587 	 * If we are re-probing the port, there may be
8588 	 * sata_drive_info structure attached
8589 	 * (or sata_pm_info, if PMult is supported).
8590 	 */
8591 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
8592 		/*
8593 		 * There is no device, so remove device info structure,
8594 		 * if necessary.
8595 		 * Only direct attached drive is considered now, until
8596 		 * port multiplier is supported. If the previously
8597 		 * attached device was a port multiplier, we would need
8598 		 * to take care of devices attached beyond the port
8599 		 * multiplier.
8600 		 */
8601 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8602 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8603 		if (sdinfo != NULL) {
8604 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8605 			sata_log(sata_hba_inst, CE_WARN,
8606 			    "SATA device detached "
8607 			    "from port %d", cportinfo->cport_addr.cport);
8608 		}
8609 		mutex_exit(&cportinfo->cport_mutex);
8610 		return (SATA_SUCCESS);
8611 	}
8612 
8613 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
8614 		if (sdinfo == NULL) {
8615 			/*
8616 			 * There is some device attached, but there is
8617 			 * no sata_drive_info structure - allocate one
8618 			 */
8619 			mutex_exit(&cportinfo->cport_mutex);
8620 			sdinfo = kmem_zalloc(
8621 			    sizeof (sata_drive_info_t), KM_SLEEP);
8622 			mutex_enter(&cportinfo->cport_mutex);
8623 			/*
8624 			 * Recheck, that the port state did not change when we
8625 			 * released mutex.
8626 			 */
8627 			if (cportinfo->cport_state & SATA_STATE_READY) {
8628 				SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo;
8629 				sdinfo->satadrv_addr = cportinfo->cport_addr;
8630 				sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT;
8631 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
8632 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
8633 			} else {
8634 				/*
8635 				 * Port is not in ready state, we
8636 				 * cannot attach a device.
8637 				 */
8638 				mutex_exit(&cportinfo->cport_mutex);
8639 				kmem_free(sdinfo, sizeof (sata_drive_info_t));
8640 				return (SATA_SUCCESS);
8641 			}
8642 			/*
8643 			 * Since we are adding device, presumably new one,
8644 			 * indicate that it  should be initalized,
8645 			 * as well as some internal framework states).
8646 			 */
8647 			init_device = B_TRUE;
8648 		}
8649 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8650 		sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
8651 	} else {
8652 		/*
8653 		 * The device is a port multiplier - not handled now.
8654 		 */
8655 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8656 		mutex_exit(&cportinfo->cport_mutex);
8657 		return (SATA_SUCCESS);
8658 	}
8659 	mutex_exit(&cportinfo->cport_mutex);
8660 	/*
8661 	 * Figure out what kind of device we are really
8662 	 * dealing with.
8663 	 */
8664 	rval = sata_probe_device(sata_hba_inst, sata_device);
8665 
8666 	mutex_enter(&cportinfo->cport_mutex);
8667 	if (rval == SATA_SUCCESS) {
8668 		/*
8669 		 * If we are dealing with the same type of a device as before,
8670 		 * restore its settings flags.
8671 		 */
8672 		if (osdinfo != NULL &&
8673 		    sata_device->satadev_type == prev_device_type)
8674 			sdinfo->satadrv_settings = prev_device_settings;
8675 
8676 		mutex_exit(&cportinfo->cport_mutex);
8677 		/* Set initial device features, if necessary */
8678 		if (init_device == B_TRUE) {
8679 			rval = sata_initialize_device(sata_hba_inst, sdinfo);
8680 		}
8681 		if (rval == SATA_SUCCESS)
8682 			return (rval);
8683 	} else {
8684 		/*
8685 		 * If there was some device info before we probe the device,
8686 		 * restore previous device setting, so we can retry from scratch
8687 		 * later. Providing, of course, that device has not disapear
8688 		 * during probing process.
8689 		 */
8690 		if (sata_device->satadev_type != SATA_DTYPE_NONE) {
8691 			if (osdinfo != NULL) {
8692 				cportinfo->cport_dev_type = prev_device_type;
8693 				sdinfo->satadrv_type = prev_device_type;
8694 				sdinfo->satadrv_state = prev_device_state;
8695 			}
8696 		} else {
8697 			/* device is gone */
8698 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8699 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8700 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8701 			mutex_exit(&cportinfo->cport_mutex);
8702 			return (SATA_SUCCESS);
8703 		}
8704 		mutex_exit(&cportinfo->cport_mutex);
8705 	}
8706 
8707 	if (retry) {
8708 		clock_t cur_time = ddi_get_lbolt();
8709 		/*
8710 		 * A device was not successfully identified or initialized.
8711 		 * Track retry time for device identification.
8712 		 */
8713 		if ((cur_time - start_time) <
8714 		    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
8715 			/* sleep for a while */
8716 			delay(drv_usectohz(SATA_DEV_RETRY_DLY));
8717 			goto retry_probe;
8718 		} else {
8719 			mutex_enter(&cportinfo->cport_mutex);
8720 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL)
8721 				SATA_CPORTINFO_DRV_INFO(cportinfo)->
8722 				    satadrv_state = SATA_DSTATE_FAILED;
8723 			mutex_exit(&cportinfo->cport_mutex);
8724 		}
8725 	}
8726 	return (SATA_SUCCESS);
8727 }
8728 
8729 /*
8730  * Initialize device
8731  * Specified device is initialized to a default state.
8732  *
8733  * Returns SATA_SUCCESS if all device features are set successfully,
8734  * SATA_FAILURE otherwise
8735  */
8736 static int
8737 sata_initialize_device(sata_hba_inst_t *sata_hba_inst,
8738     sata_drive_info_t *sdinfo)
8739 {
8740 	int rval;
8741 
8742 	sata_save_drive_settings(sdinfo);
8743 
8744 	sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
8745 
8746 	sata_init_write_cache_mode(sdinfo);
8747 
8748 	rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0);
8749 
8750 	/* Determine current data transfer mode */
8751 	if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) {
8752 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
8753 	} else if ((sdinfo->satadrv_id.ai_validinfo &
8754 	    SATA_VALIDINFO_88) != 0 &&
8755 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) {
8756 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
8757 	} else if ((sdinfo->satadrv_id.ai_dworddma &
8758 	    SATA_MDMA_SEL_MASK) != 0) {
8759 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
8760 	} else
8761 		/* DMA supported, not no DMA transfer mode is selected !? */
8762 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
8763 
8764 	return (rval);
8765 }
8766 
8767 
8768 /*
8769  * Initialize write cache mode.
8770  *
8771  * The default write cache setting for SATA HDD is provided by sata_write_cache
8772  * static variable. ATAPI CD/DVDs devices have write cache default is
8773  * determined by sata_atapicdvd_write_cache static variable.
8774  * 1 - enable
8775  * 0 - disable
8776  * any other value - current drive setting
8777  *
8778  * Although there is not reason to disable write cache on CD/DVD devices,
8779  * the default setting control is provided for the maximun flexibility.
8780  *
8781  * In the future, it may be overridden by the
8782  * disk-write-cache-enable property setting, if it is defined.
8783  * Returns SATA_SUCCESS if all device features are set successfully,
8784  * SATA_FAILURE otherwise.
8785  */
8786 static void
8787 sata_init_write_cache_mode(sata_drive_info_t *sdinfo)
8788 {
8789 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
8790 		if (sata_write_cache == 1)
8791 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
8792 		else if (sata_write_cache == 0)
8793 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
8794 		/*
8795 		 * When sata_write_cache value is not 0 or 1,
8796 		 * a current setting of the drive's write cache is used.
8797 		 */
8798 	} else { /* Assume ATAPI CD/DVD device */
8799 		if (sata_atapicdvd_write_cache == 1)
8800 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
8801 		else if (sata_atapicdvd_write_cache == 0)
8802 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
8803 		/*
8804 		 * When sata_write_cache value is not 0 or 1,
8805 		 * a current setting of the drive's write cache is used.
8806 		 */
8807 	}
8808 }
8809 
8810 
8811 /*
8812  * Validate sata address.
8813  * Specified cport, pmport and qualifier has to match
8814  * passed sata_scsi configuration info.
8815  * The presence of an attached device is not verified.
8816  *
8817  * Returns 0 when address is valid, -1 otherwise.
8818  */
8819 static int
8820 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport,
8821 	int pmport, int qual)
8822 {
8823 	if (qual == SATA_ADDR_DCPORT && pmport != 0)
8824 		goto invalid_address;
8825 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
8826 		goto invalid_address;
8827 	if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) &&
8828 	    ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) ||
8829 	    (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) ||
8830 	    (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport))))
8831 		goto invalid_address;
8832 
8833 	return (0);
8834 
8835 invalid_address:
8836 	return (-1);
8837 
8838 }
8839 
8840 /*
8841  * Validate scsi address
8842  * SCSI target address is translated into SATA cport/pmport and compared
8843  * with a controller port/device configuration. LUN has to be 0.
8844  * Returns 0 if a scsi target refers to an attached device,
8845  * returns 1 if address is valid but device is not attached,
8846  * returns -1 if bad address or device is of an unsupported type.
8847  * Upon return sata_device argument is set.
8848  */
8849 static int
8850 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst,
8851 	struct scsi_address *ap, sata_device_t *sata_device)
8852 {
8853 	int cport, pmport, qual, rval;
8854 
8855 	rval = -1;	/* Invalid address */
8856 	if (ap->a_lun != 0)
8857 		goto out;
8858 
8859 	qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
8860 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
8861 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
8862 
8863 	if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT)
8864 		goto out;
8865 
8866 	if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) ==
8867 	    0) {
8868 
8869 		sata_cport_info_t *cportinfo;
8870 		sata_pmult_info_t *pmultinfo;
8871 		sata_drive_info_t *sdinfo = NULL;
8872 
8873 		rval = 1;	/* Valid sata address */
8874 
8875 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
8876 		if (qual == SATA_ADDR_DCPORT) {
8877 			if (cportinfo == NULL ||
8878 			    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
8879 				goto out;
8880 
8881 			if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT ||
8882 			    (cportinfo->cport_dev_type &
8883 			    SATA_VALID_DEV_TYPE) == 0) {
8884 				rval = -1;
8885 				goto out;
8886 			}
8887 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8888 
8889 		} else if (qual == SATA_ADDR_DPMPORT) {
8890 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
8891 			if (pmultinfo == NULL) {
8892 				rval = -1;
8893 				goto out;
8894 			}
8895 			if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) ==
8896 			    NULL ||
8897 			    SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
8898 			    pmport) == SATA_DTYPE_NONE)
8899 				goto out;
8900 
8901 			sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport,
8902 			    pmport);
8903 		} else {
8904 			rval = -1;
8905 			goto out;
8906 		}
8907 		if ((sdinfo == NULL) ||
8908 		    (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0)
8909 			goto out;
8910 
8911 		sata_device->satadev_type = sdinfo->satadrv_type;
8912 		sata_device->satadev_addr.qual = qual;
8913 		sata_device->satadev_addr.cport = cport;
8914 		sata_device->satadev_addr.pmport = pmport;
8915 		sata_device->satadev_rev = SATA_DEVICE_REV_1;
8916 		return (0);
8917 	}
8918 out:
8919 	if (rval == 1) {
8920 		SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
8921 		    "sata_validate_scsi_address: no valid target %x lun %x",
8922 		    ap->a_target, ap->a_lun);
8923 	}
8924 	return (rval);
8925 }
8926 
8927 /*
8928  * Find dip corresponding to passed device number
8929  *
8930  * Returns NULL if invalid device number is passed or device cannot be found,
8931  * Returns dip is device is found.
8932  */
8933 static dev_info_t *
8934 sata_devt_to_devinfo(dev_t dev)
8935 {
8936 	dev_info_t *dip;
8937 #ifndef __lock_lint
8938 	struct devnames *dnp;
8939 	major_t major = getmajor(dev);
8940 	int instance = SATA_MINOR2INSTANCE(getminor(dev));
8941 
8942 	if (major >= devcnt)
8943 		return (NULL);
8944 
8945 	dnp = &devnamesp[major];
8946 	LOCK_DEV_OPS(&(dnp->dn_lock));
8947 	dip = dnp->dn_head;
8948 	while (dip && (ddi_get_instance(dip) != instance)) {
8949 		dip = ddi_get_next(dip);
8950 	}
8951 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
8952 #endif
8953 
8954 	return (dip);
8955 }
8956 
8957 
8958 /*
8959  * Probe device.
8960  * This function issues Identify Device command and initializes local
8961  * sata_drive_info structure if the device can be identified.
8962  * The device type is determined by examining Identify Device
8963  * command response.
8964  * If the sata_hba_inst has linked drive info structure for this
8965  * device address, the Identify Device data is stored into sata_drive_info
8966  * structure linked to the port info structure.
8967  *
8968  * sata_device has to refer to the valid sata port(s) for HBA described
8969  * by sata_hba_inst structure.
8970  *
8971  * Returns:
8972  *	SATA_SUCCESS if device type was successfully probed and port-linked
8973  *		drive info structure was updated;
8974  * 	SATA_FAILURE if there is no device, or device was not probed
8975  *		successully;
8976  *	SATA_RETRY if device probe can be retried later.
8977  * If a device cannot be identified, sata_device's dev_state and dev_type
8978  * fields are set to unknown.
8979  * There are no retries in this function. Any retries should be managed by
8980  * the caller.
8981  */
8982 
8983 
8984 static int
8985 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
8986 {
8987 	sata_drive_info_t *sdinfo;
8988 	sata_drive_info_t new_sdinfo;	/* local drive info struct */
8989 	int rval;
8990 
8991 	ASSERT((SATA_CPORT_STATE(sata_hba_inst,
8992 	    sata_device->satadev_addr.cport) &
8993 	    (SATA_STATE_PROBED | SATA_STATE_READY)) != 0);
8994 
8995 	sata_device->satadev_type = SATA_DTYPE_NONE;
8996 
8997 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
8998 	    sata_device->satadev_addr.cport)));
8999 
9000 	/* Get pointer to port-linked sata device info structure */
9001 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9002 	if (sdinfo != NULL) {
9003 		sdinfo->satadrv_state &=
9004 		    ~(SATA_STATE_PROBED | SATA_STATE_READY);
9005 		sdinfo->satadrv_state |= SATA_STATE_PROBING;
9006 	} else {
9007 		/* No device to probe */
9008 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9009 		    sata_device->satadev_addr.cport)));
9010 		sata_device->satadev_type = SATA_DTYPE_NONE;
9011 		sata_device->satadev_state = SATA_STATE_UNKNOWN;
9012 		return (SATA_FAILURE);
9013 	}
9014 	/*
9015 	 * Need to issue both types of identify device command and
9016 	 * determine device type by examining retreived data/status.
9017 	 * First, ATA Identify Device.
9018 	 */
9019 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
9020 	new_sdinfo.satadrv_addr = sata_device->satadev_addr;
9021 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9022 	    sata_device->satadev_addr.cport)));
9023 	new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
9024 	rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
9025 	if (rval == SATA_RETRY) {
9026 		/* We may try to check for ATAPI device */
9027 		if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) {
9028 			/*
9029 			 * HBA supports ATAPI - try to issue Identify Packet
9030 			 * Device command.
9031 			 */
9032 			new_sdinfo.satadrv_type = SATA_DTYPE_ATAPICD;
9033 			rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
9034 		}
9035 	}
9036 	if (rval == SATA_SUCCESS) {
9037 		/*
9038 		 * Got something responding positively to ATA Identify Device
9039 		 * or to Identify Packet Device cmd.
9040 		 * Save last used device type.
9041 		 */
9042 		sata_device->satadev_type = new_sdinfo.satadrv_type;
9043 
9044 		/* save device info, if possible */
9045 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9046 		    sata_device->satadev_addr.cport)));
9047 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9048 		if (sdinfo == NULL) {
9049 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9050 			    sata_device->satadev_addr.cport)));
9051 			return (SATA_FAILURE);
9052 		}
9053 		/*
9054 		 * Copy drive info into the port-linked drive info structure.
9055 		 */
9056 		*sdinfo = new_sdinfo;
9057 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
9058 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
9059 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
9060 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
9061 			    sata_device->satadev_addr.cport) =
9062 			    sdinfo->satadrv_type;
9063 		else /* SATA_ADDR_DPMPORT */
9064 			SATA_PMPORT_DEV_TYPE(sata_hba_inst,
9065 			    sata_device->satadev_addr.cport,
9066 			    sata_device->satadev_addr.pmport) =
9067 			    sdinfo->satadrv_type;
9068 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9069 		    sata_device->satadev_addr.cport)));
9070 		return (SATA_SUCCESS);
9071 	}
9072 
9073 	/*
9074 	 * It may be SATA_RETRY or SATA_FAILURE return.
9075 	 * Looks like we cannot determine the device type at this time.
9076 	 */
9077 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9078 	    sata_device->satadev_addr.cport)));
9079 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9080 	if (sdinfo != NULL) {
9081 		sata_device->satadev_type = SATA_DTYPE_UNKNOWN;
9082 		sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9083 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
9084 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
9085 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
9086 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
9087 			    sata_device->satadev_addr.cport) =
9088 			    SATA_DTYPE_UNKNOWN;
9089 		else {
9090 			/* SATA_ADDR_DPMPORT */
9091 			if ((SATA_PMULT_INFO(sata_hba_inst,
9092 			    sata_device->satadev_addr.cport) != NULL) &&
9093 			    (SATA_PMPORT_INFO(sata_hba_inst,
9094 			    sata_device->satadev_addr.cport,
9095 			    sata_device->satadev_addr.pmport) != NULL))
9096 				SATA_PMPORT_DEV_TYPE(sata_hba_inst,
9097 				    sata_device->satadev_addr.cport,
9098 				    sata_device->satadev_addr.pmport) =
9099 				    SATA_DTYPE_UNKNOWN;
9100 		}
9101 	}
9102 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9103 	    sata_device->satadev_addr.cport)));
9104 	return (rval);
9105 }
9106 
9107 
9108 /*
9109  * Get pointer to sata_drive_info structure.
9110  *
9111  * The sata_device has to contain address (cport, pmport and qualifier) for
9112  * specified sata_scsi structure.
9113  *
9114  * Returns NULL if device address is not valid for this HBA configuration.
9115  * Otherwise, returns a pointer to sata_drive_info structure.
9116  *
9117  * This function should be called with a port mutex held.
9118  */
9119 static sata_drive_info_t *
9120 sata_get_device_info(sata_hba_inst_t *sata_hba_inst,
9121     sata_device_t *sata_device)
9122 {
9123 	uint8_t cport = sata_device->satadev_addr.cport;
9124 	uint8_t pmport = sata_device->satadev_addr.pmport;
9125 	uint8_t qual = sata_device->satadev_addr.qual;
9126 
9127 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
9128 		return (NULL);
9129 
9130 	if (!(SATA_CPORT_STATE(sata_hba_inst, cport) &
9131 	    (SATA_STATE_PROBED | SATA_STATE_READY)))
9132 		/* Port not probed yet */
9133 		return (NULL);
9134 
9135 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE)
9136 		return (NULL);
9137 
9138 	if (qual == SATA_ADDR_DCPORT) {
9139 		/* Request for a device on a controller port */
9140 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
9141 		    SATA_DTYPE_PMULT)
9142 			/* Port multiplier attached */
9143 			return (NULL);
9144 		return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport));
9145 	}
9146 	if (qual == SATA_ADDR_DPMPORT) {
9147 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
9148 		    SATA_DTYPE_PMULT)
9149 			return (NULL);
9150 
9151 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport))
9152 			return (NULL);
9153 
9154 		return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport));
9155 	}
9156 
9157 	/* we should not get here */
9158 	return (NULL);
9159 }
9160 
9161 
9162 /*
9163  * sata_identify_device.
9164  * Send Identify Device command to SATA HBA driver.
9165  * If command executes successfully, update sata_drive_info structure pointed
9166  * to by sdinfo argument, including Identify Device data.
9167  * If command fails, invalidate data in sata_drive_info.
9168  *
9169  * Cannot be called from interrupt level.
9170  *
9171  * Returns:
9172  * SATA_SUCCESS if the device was identified as a supported device,
9173  * SATA_RETRY if the device was not identified but could be retried,
9174  * SATA_FAILURE if the device was not identified and identify attempt
9175  *	should not be retried.
9176  */
9177 static int
9178 sata_identify_device(sata_hba_inst_t *sata_hba_inst,
9179     sata_drive_info_t *sdinfo)
9180 {
9181 	uint16_t cfg_word;
9182 	int rval;
9183 
9184 	/* fetch device identify data */
9185 	if ((rval = sata_fetch_device_identify_data(sata_hba_inst,
9186 	    sdinfo)) != 0)
9187 		goto fail_unknown;
9188 
9189 	cfg_word = sdinfo->satadrv_id.ai_config;
9190 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK &&
9191 	    (cfg_word & SATA_ATA_TYPE_MASK) != SATA_ATA_TYPE) {
9192 		/* Change device type to reflect Identify Device data */
9193 		if (((cfg_word & SATA_ATAPI_TYPE_MASK) ==
9194 		    SATA_ATAPI_TYPE) &&
9195 		    ((cfg_word & SATA_ATAPI_ID_DEV_TYPE) ==
9196 		    SATA_ATAPI_CDROM_DEV)) {
9197 			sdinfo->satadrv_type = SATA_DTYPE_ATAPICD;
9198 		} else {
9199 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9200 		}
9201 	} else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD &&
9202 	    (((cfg_word & SATA_ATAPI_TYPE_MASK) != SATA_ATAPI_TYPE) ||
9203 	    ((cfg_word & SATA_ATAPI_ID_DEV_TYPE) != SATA_ATAPI_CDROM_DEV))) {
9204 		/* Change device type to reflect Identify Device data ! */
9205 		if ((sdinfo->satadrv_id.ai_config & SATA_ATA_TYPE_MASK) ==
9206 		    SATA_ATA_TYPE) {
9207 			sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
9208 		} else {
9209 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9210 		}
9211 	}
9212 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9213 		if (sdinfo->satadrv_capacity == 0) {
9214 			/* Non-LBA disk. Too bad... */
9215 			sata_log(sata_hba_inst, CE_WARN,
9216 			    "SATA disk device at port %d does not support LBA",
9217 			    sdinfo->satadrv_addr.cport);
9218 			rval = SATA_FAILURE;
9219 			goto fail_unknown;
9220 		}
9221 	}
9222 #if 0
9223 	/* Left for historical reason */
9224 	/*
9225 	 * Some initial version of SATA spec indicated that at least
9226 	 * UDMA mode 4 has to be supported. It is not metioned in
9227 	 * SerialATA 2.6, so this restriction is removed.
9228 	 */
9229 	/* Check for Ultra DMA modes 6 through 0 being supported */
9230 	for (i = 6; i >= 0; --i) {
9231 		if (sdinfo->satadrv_id.ai_ultradma & (1 << i))
9232 			break;
9233 	}
9234 
9235 	/*
9236 	 * At least UDMA 4 mode has to be supported. If mode 4 or
9237 	 * higher are not supported by the device, fail this
9238 	 * device.
9239 	 */
9240 	if (i < 4) {
9241 		/* No required Ultra DMA mode supported */
9242 		sata_log(sata_hba_inst, CE_WARN,
9243 		    "SATA disk device at port %d does not support UDMA "
9244 		    "mode 4 or higher", sdinfo->satadrv_addr.cport);
9245 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9246 		    "mode 4 or higher required, %d supported", i));
9247 		rval = SATA_FAILURE;
9248 		goto fail_unknown;
9249 	}
9250 #endif
9251 
9252 	return (SATA_SUCCESS);
9253 
9254 fail_unknown:
9255 	/* Invalidate sata_drive_info ? */
9256 	sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9257 	sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
9258 	return (rval);
9259 }
9260 
9261 /*
9262  * Log/display device information
9263  */
9264 static void
9265 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst,
9266     sata_drive_info_t *sdinfo)
9267 {
9268 	int valid_version;
9269 	char msg_buf[MAXPATHLEN];
9270 	int i;
9271 
9272 	/* Show HBA path */
9273 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf);
9274 
9275 	cmn_err(CE_CONT, "?%s :\n", msg_buf);
9276 
9277 	if (sdinfo->satadrv_type == SATA_DTYPE_UNKNOWN) {
9278 		(void) sprintf(msg_buf,
9279 		    "Unsupported SATA device type (cfg 0x%x) at ",
9280 		    sdinfo->satadrv_id.ai_config);
9281 	} else {
9282 		(void) sprintf(msg_buf, "SATA %s device at",
9283 		    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
9284 		    "disk":"CD/DVD (ATAPI)");
9285 	}
9286 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT)
9287 		cmn_err(CE_CONT, "?\t%s port %d\n",
9288 		    msg_buf, sdinfo->satadrv_addr.cport);
9289 	else
9290 		cmn_err(CE_CONT, "?\t%s port %d pmport %d\n",
9291 		    msg_buf, sdinfo->satadrv_addr.cport,
9292 		    sdinfo->satadrv_addr.pmport);
9293 
9294 	bcopy(&sdinfo->satadrv_id.ai_model, msg_buf,
9295 	    sizeof (sdinfo->satadrv_id.ai_model));
9296 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model));
9297 	msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
9298 	cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf);
9299 
9300 	bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf,
9301 	    sizeof (sdinfo->satadrv_id.ai_fw));
9302 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw));
9303 	msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
9304 	cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf);
9305 
9306 	bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf,
9307 	    sizeof (sdinfo->satadrv_id.ai_drvser));
9308 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser));
9309 	msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
9310 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9311 		cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
9312 	} else {
9313 		/* Assuming ATAPI CD/DVD */
9314 		/*
9315 		 * SOme drives do not implement serial number and may
9316 		 * violate the spec by provinding spaces rather than zeros
9317 		 * in serial number field. Scan the buffer to detect it.
9318 		 */
9319 		for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) {
9320 			if (msg_buf[i] != '\0' && msg_buf[i] != ' ')
9321 				break;
9322 		}
9323 		if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) {
9324 			cmn_err(CE_CONT, "?\tserial number - none\n");
9325 		} else {
9326 			cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
9327 		}
9328 	}
9329 
9330 #ifdef SATA_DEBUG
9331 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
9332 	    sdinfo->satadrv_id.ai_majorversion != 0xffff) {
9333 		int i;
9334 		for (i = 14; i >= 2; i--) {
9335 			if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) {
9336 				valid_version = i;
9337 				break;
9338 			}
9339 		}
9340 		cmn_err(CE_CONT,
9341 		    "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
9342 		    valid_version,
9343 		    sdinfo->satadrv_id.ai_majorversion,
9344 		    sdinfo->satadrv_id.ai_minorversion);
9345 	}
9346 #endif
9347 	/* Log some info */
9348 	cmn_err(CE_CONT, "?\tsupported features:\n");
9349 	msg_buf[0] = '\0';
9350 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9351 		if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48)
9352 			(void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN);
9353 		else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28)
9354 			(void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN);
9355 	}
9356 	if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA)
9357 		(void) strlcat(msg_buf, "DMA", MAXPATHLEN);
9358 	if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ)
9359 		(void) strlcat(msg_buf, ", Native Command Queueing",
9360 		    MAXPATHLEN);
9361 	if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)
9362 		(void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN);
9363 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) &&
9364 	    (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED))
9365 		(void) strlcat(msg_buf, ", SMART", MAXPATHLEN);
9366 	if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) &&
9367 	    (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED))
9368 		(void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN);
9369 	cmn_err(CE_CONT, "?\t %s\n", msg_buf);
9370 	if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2)
9371 		cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n");
9372 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1)
9373 		cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n");
9374 	if (sdinfo->satadrv_features_support &
9375 	    (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) {
9376 		msg_buf[0] = '\0';
9377 		(void) snprintf(msg_buf, MAXPATHLEN,
9378 		    "Supported queue depth %d",
9379 		    sdinfo->satadrv_queue_depth);
9380 		if (!(sata_func_enable &
9381 		    (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ)))
9382 			(void) strlcat(msg_buf,
9383 			    " - queueing disabled globally", MAXPATHLEN);
9384 		else if (sdinfo->satadrv_queue_depth >
9385 		    sdinfo->satadrv_max_queue_depth) {
9386 			(void) snprintf(&msg_buf[strlen(msg_buf)],
9387 			    MAXPATHLEN - strlen(msg_buf), ", limited to %d",
9388 			    (int)sdinfo->satadrv_max_queue_depth);
9389 		}
9390 		cmn_err(CE_CONT, "?\t%s\n", msg_buf);
9391 	}
9392 
9393 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9394 #ifdef __i386
9395 		(void) sprintf(msg_buf, "\tcapacity = %llu sectors\n",
9396 		    sdinfo->satadrv_capacity);
9397 #else
9398 		(void) sprintf(msg_buf, "\tcapacity = %lu sectors\n",
9399 		    sdinfo->satadrv_capacity);
9400 #endif
9401 		cmn_err(CE_CONT, "?%s", msg_buf);
9402 	}
9403 }
9404 
9405 
9406 /*
9407  * sata_save_drive_settings extracts current setting of the device and stores
9408  * it for future reference, in case the device setup would need to be restored
9409  * after the device reset.
9410  *
9411  * For all devices read ahead and write cache settings are saved, if the
9412  * device supports these features at all.
9413  * For ATAPI devices the Removable Media Status Notification setting is saved.
9414  */
9415 static void
9416 sata_save_drive_settings(sata_drive_info_t *sdinfo)
9417 {
9418 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) ||
9419 	    (sdinfo->satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) {
9420 
9421 		/* Current setting of Read Ahead (and Read Cache) */
9422 		if (sdinfo->satadrv_id.ai_features85 & SATA_LOOK_AHEAD)
9423 			sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
9424 		else
9425 			sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD;
9426 
9427 		/* Current setting of Write Cache */
9428 		if (sdinfo->satadrv_id.ai_features85 & SATA_WRITE_CACHE)
9429 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
9430 		else
9431 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
9432 	}
9433 
9434 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
9435 		if (sdinfo->satadrv_id.ai_cmdset83 & SATA_RM_STATUS_NOTIFIC)
9436 			sdinfo->satadrv_settings |= SATA_DEV_RMSN;
9437 		else
9438 			sdinfo->satadrv_settings &= ~SATA_DEV_RMSN;
9439 	}
9440 }
9441 
9442 
9443 /*
9444  * sata_check_capacity function determines a disk capacity
9445  * and addressing mode (LBA28/LBA48) by examining a disk identify device data.
9446  *
9447  * NOTE: CHS mode is not supported! If a device does not support LBA,
9448  * this function is not called.
9449  *
9450  * Returns device capacity in number of blocks, i.e. largest addressable LBA+1
9451  */
9452 static uint64_t
9453 sata_check_capacity(sata_drive_info_t *sdinfo)
9454 {
9455 	uint64_t capacity = 0;
9456 	int i;
9457 
9458 	if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK ||
9459 	    !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT)
9460 		/* Capacity valid only for LBA-addressable disk devices */
9461 		return (0);
9462 
9463 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) &&
9464 	    (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) &&
9465 	    (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) {
9466 		/* LBA48 mode supported and enabled */
9467 		sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 |
9468 		    SATA_DEV_F_LBA28;
9469 		for (i = 3;  i >= 0;  --i) {
9470 			capacity <<= 16;
9471 			capacity += sdinfo->satadrv_id.ai_addrsecxt[i];
9472 		}
9473 	} else {
9474 		capacity = sdinfo->satadrv_id.ai_addrsec[1];
9475 		capacity <<= 16;
9476 		capacity += sdinfo->satadrv_id.ai_addrsec[0];
9477 		if (capacity >= 0x1000000)
9478 			/* LBA28 mode */
9479 			sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28;
9480 	}
9481 	return (capacity);
9482 }
9483 
9484 
9485 /*
9486  * Allocate consistent buffer for DMA transfer
9487  *
9488  * Cannot be called from interrupt level or with mutex held - it may sleep.
9489  *
9490  * Returns pointer to allocated buffer structure, or NULL if allocation failed.
9491  */
9492 static struct buf *
9493 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len)
9494 {
9495 	struct scsi_address ap;
9496 	struct buf *bp;
9497 	ddi_dma_attr_t	cur_dma_attr;
9498 
9499 	ASSERT(spx->txlt_sata_pkt != NULL);
9500 	ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran;
9501 	ap.a_target = SATA_TO_SCSI_TARGET(
9502 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport,
9503 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport,
9504 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual);
9505 	ap.a_lun = 0;
9506 
9507 	bp = scsi_alloc_consistent_buf(&ap, NULL, len,
9508 	    B_READ, SLEEP_FUNC, NULL);
9509 
9510 	if (bp != NULL) {
9511 		/* Allocate DMA resources for this buffer */
9512 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
9513 		/*
9514 		 * We use a local version of the dma_attr, to account
9515 		 * for a device addressing limitations.
9516 		 * sata_adjust_dma_attr() will handle sdinfo == NULL which
9517 		 * will cause dma attributes to be adjusted to a lowest
9518 		 * acceptable level.
9519 		 */
9520 		sata_adjust_dma_attr(NULL,
9521 		    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
9522 
9523 		if (sata_dma_buf_setup(spx, PKT_CONSISTENT,
9524 		    SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) {
9525 			scsi_free_consistent_buf(bp);
9526 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
9527 			bp = NULL;
9528 		}
9529 	}
9530 	return (bp);
9531 }
9532 
9533 /*
9534  * Release local buffer (consistent buffer for DMA transfer) allocated
9535  * via sata_alloc_local_buffer().
9536  */
9537 static void
9538 sata_free_local_buffer(sata_pkt_txlate_t *spx)
9539 {
9540 	ASSERT(spx->txlt_sata_pkt != NULL);
9541 	ASSERT(spx->txlt_dma_cookie_list != NULL);
9542 	ASSERT(spx->txlt_dma_cookie_list_len != 0);
9543 	ASSERT(spx->txlt_buf_dma_handle != NULL);
9544 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL);
9545 
9546 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0;
9547 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL;
9548 
9549 	/* Free DMA resources */
9550 	(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
9551 	ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
9552 	spx->txlt_buf_dma_handle = 0;
9553 
9554 	if (spx->txlt_dma_cookie_list != &spx->txlt_dma_cookie) {
9555 		kmem_free(spx->txlt_dma_cookie_list,
9556 		    spx->txlt_dma_cookie_list_len * sizeof (ddi_dma_cookie_t));
9557 		spx->txlt_dma_cookie_list = NULL;
9558 		spx->txlt_dma_cookie_list_len = 0;
9559 	}
9560 	/* Free buffer */
9561 	scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp);
9562 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
9563 }
9564 
9565 
9566 
9567 
9568 /*
9569  * Allocate sata_pkt
9570  * Pkt structure version and embedded strcutures version are initialized.
9571  * sata_pkt and sata_pkt_txlate structures are cross-linked.
9572  *
9573  * Since this may be called in interrupt context by sata_scsi_init_pkt,
9574  * callback argument determines if it can sleep or not.
9575  * Hence, it should not be called from interrupt context.
9576  *
9577  * If successful, non-NULL pointer to a sata pkt is returned.
9578  * Upon failure, NULL pointer is returned.
9579  */
9580 static sata_pkt_t *
9581 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t))
9582 {
9583 	sata_pkt_t *spkt;
9584 	int kmsflag;
9585 
9586 	kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
9587 	spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag);
9588 	if (spkt == NULL) {
9589 		SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9590 		    "sata_pkt_alloc: failed"));
9591 		return (NULL);
9592 	}
9593 	spkt->satapkt_rev = SATA_PKT_REV;
9594 	spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV;
9595 	spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
9596 	spkt->satapkt_framework_private = spx;
9597 	spx->txlt_sata_pkt = spkt;
9598 	return (spkt);
9599 }
9600 
9601 /*
9602  * Free sata pkt allocated via sata_pkt_alloc()
9603  */
9604 static void
9605 sata_pkt_free(sata_pkt_txlate_t *spx)
9606 {
9607 	ASSERT(spx->txlt_sata_pkt != NULL);
9608 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL);
9609 	kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t));
9610 	spx->txlt_sata_pkt = NULL;
9611 }
9612 
9613 
9614 /*
9615  * Adjust DMA attributes.
9616  * SCSI cmds block count is up to 24 bits, SATA cmd block count vary
9617  * from 8 bits to 16 bits, depending on a command being used.
9618  * Limiting max block count arbitrarily to 256 for all read/write
9619  * commands may affects performance, so check both the device and
9620  * controller capability before adjusting dma attributes.
9621  */
9622 void
9623 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr,
9624     ddi_dma_attr_t *adj_dma_attr)
9625 {
9626 	uint32_t count_max;
9627 
9628 	/* Copy original attributes */
9629 	*adj_dma_attr = *dma_attr;
9630 	/*
9631 	 * Things to consider: device addressing capability,
9632 	 * "excessive" controller DMA capabilities.
9633 	 * If a device is being probed/initialized, there are
9634 	 * no device info - use default limits then.
9635 	 */
9636 	if (sdinfo == NULL) {
9637 		count_max = dma_attr->dma_attr_granular * 0x100;
9638 		if (dma_attr->dma_attr_count_max > count_max)
9639 			adj_dma_attr->dma_attr_count_max = count_max;
9640 		if (dma_attr->dma_attr_maxxfer > count_max)
9641 			adj_dma_attr->dma_attr_maxxfer = count_max;
9642 		return;
9643 	}
9644 
9645 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9646 		if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) {
9647 			/*
9648 			 * 16-bit sector count may be used - we rely on
9649 			 * the assumption that only read and write cmds
9650 			 * will request more than 256 sectors worth of data
9651 			 */
9652 			count_max = adj_dma_attr->dma_attr_granular * 0x10000;
9653 		} else {
9654 			/*
9655 			 * 8-bit sector count will be used - default limits
9656 			 * for dma attributes
9657 			 */
9658 			count_max = adj_dma_attr->dma_attr_granular * 0x100;
9659 		}
9660 		/*
9661 		 * Adjust controler dma attributes, if necessary
9662 		 */
9663 		if (dma_attr->dma_attr_count_max > count_max)
9664 			adj_dma_attr->dma_attr_count_max = count_max;
9665 		if (dma_attr->dma_attr_maxxfer > count_max)
9666 			adj_dma_attr->dma_attr_maxxfer = count_max;
9667 	}
9668 }
9669 
9670 
9671 /*
9672  * Allocate DMA resources for the buffer
9673  * This function handles initial DMA resource allocation as well as
9674  * DMA window shift and may be called repeatedly for the same DMA window
9675  * until all DMA cookies in the DMA window are processed.
9676  * To guarantee that there is always a coherent set of cookies to process
9677  * by SATA HBA driver (observing alignment, device granularity, etc.),
9678  * the number of slots for DMA cookies is equal to lesser of  a number of
9679  * cookies in a DMA window and a max number of scatter/gather entries.
9680  *
9681  * Returns DDI_SUCCESS upon successful operation.
9682  * Return failure code of a failing command or DDI_FAILURE when
9683  * internal cleanup failed.
9684  */
9685 static int
9686 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags,
9687     int (*callback)(caddr_t), caddr_t arg,
9688     ddi_dma_attr_t *cur_dma_attr)
9689 {
9690 	int	rval;
9691 	off_t	offset;
9692 	size_t	size;
9693 	int	max_sg_len, req_len, i;
9694 	uint_t	dma_flags;
9695 	struct buf	*bp;
9696 	uint64_t	cur_txfer_len;
9697 
9698 
9699 	ASSERT(spx->txlt_sata_pkt != NULL);
9700 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9701 	ASSERT(bp != NULL);
9702 
9703 
9704 	if (spx->txlt_buf_dma_handle == NULL) {
9705 		/*
9706 		 * No DMA resources allocated so far - this is a first call
9707 		 * for this sata pkt.
9708 		 */
9709 		rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst),
9710 		    cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle);
9711 
9712 		if (rval != DDI_SUCCESS) {
9713 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9714 			    "sata_dma_buf_setup: no buf DMA resources %x",
9715 			    rval));
9716 			return (rval);
9717 		}
9718 
9719 		if (bp->b_flags & B_READ)
9720 			dma_flags = DDI_DMA_READ;
9721 		else
9722 			dma_flags = DDI_DMA_WRITE;
9723 
9724 		if (flags & PKT_CONSISTENT)
9725 			dma_flags |= DDI_DMA_CONSISTENT;
9726 
9727 		if (flags & PKT_DMA_PARTIAL)
9728 			dma_flags |= DDI_DMA_PARTIAL;
9729 
9730 		/*
9731 		 * Check buffer alignment and size against dma attributes
9732 		 * Consider dma_attr_align only. There may be requests
9733 		 * with the size lower than device granularity, but they
9734 		 * will not read/write from/to the device, so no adjustment
9735 		 * is necessary. The dma_attr_minxfer theoretically should
9736 		 * be considered, but no HBA driver is checking it.
9737 		 */
9738 		if (IS_P2ALIGNED(bp->b_un.b_addr,
9739 		    cur_dma_attr->dma_attr_align)) {
9740 			rval = ddi_dma_buf_bind_handle(
9741 			    spx->txlt_buf_dma_handle,
9742 			    bp, dma_flags, callback, arg,
9743 			    &spx->txlt_dma_cookie,
9744 			    &spx->txlt_curwin_num_dma_cookies);
9745 		} else { /* Buffer is not aligned */
9746 
9747 			int	(*ddicallback)(caddr_t);
9748 			size_t	bufsz;
9749 
9750 			/* Check id sleeping is allowed */
9751 			ddicallback = (callback == NULL_FUNC) ?
9752 			    DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
9753 
9754 			SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
9755 			    "mis-aligned buffer: addr=0x%p, cnt=%lu",
9756 			    (void *)bp->b_un.b_addr, bp->b_bcount);
9757 
9758 			if (bp->b_flags & (B_PAGEIO|B_PHYS))
9759 				/*
9760 				 * CPU will need to access data in the buffer
9761 				 * (for copying) so map it.
9762 				 */
9763 				bp_mapin(bp);
9764 
9765 			ASSERT(spx->txlt_tmp_buf == NULL);
9766 
9767 			/* Buffer may be padded by ddi_dma_mem_alloc()! */
9768 			rval = ddi_dma_mem_alloc(
9769 			    spx->txlt_buf_dma_handle,
9770 			    bp->b_bcount,
9771 			    &sata_acc_attr,
9772 			    DDI_DMA_STREAMING,
9773 			    ddicallback, NULL,
9774 			    &spx->txlt_tmp_buf,
9775 			    &bufsz,
9776 			    &spx->txlt_tmp_buf_handle);
9777 
9778 			if (rval != DDI_SUCCESS) {
9779 				/* DMA mapping failed */
9780 				(void) ddi_dma_free_handle(
9781 				    &spx->txlt_buf_dma_handle);
9782 				spx->txlt_buf_dma_handle = NULL;
9783 #ifdef SATA_DEBUG
9784 				mbuffail_count++;
9785 #endif
9786 				SATADBG1(SATA_DBG_DMA_SETUP,
9787 				    spx->txlt_sata_hba_inst,
9788 				    "sata_dma_buf_setup: "
9789 				    "buf dma mem alloc failed %x\n", rval);
9790 				return (rval);
9791 			}
9792 			ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf,
9793 			    cur_dma_attr->dma_attr_align));
9794 
9795 #ifdef SATA_DEBUG
9796 			mbuf_count++;
9797 
9798 			if (bp->b_bcount != bufsz)
9799 				/*
9800 				 * This will require special handling, because
9801 				 * DMA cookies will be based on the temporary
9802 				 * buffer size, not the original buffer
9803 				 * b_bcount, so the residue may have to
9804 				 * be counted differently.
9805 				 */
9806 				SATADBG2(SATA_DBG_DMA_SETUP,
9807 				    spx->txlt_sata_hba_inst,
9808 				    "sata_dma_buf_setup: bp size %x != "
9809 				    "bufsz %x\n", bp->b_bcount, bufsz);
9810 #endif
9811 			if (dma_flags & DDI_DMA_WRITE) {
9812 				/*
9813 				 * Write operation - copy data into
9814 				 * an aligned temporary buffer. Buffer will be
9815 				 * synced for device by ddi_dma_addr_bind_handle
9816 				 */
9817 				bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf,
9818 				    bp->b_bcount);
9819 			}
9820 
9821 			rval = ddi_dma_addr_bind_handle(
9822 			    spx->txlt_buf_dma_handle,
9823 			    NULL,
9824 			    spx->txlt_tmp_buf,
9825 			    bufsz, dma_flags, ddicallback, 0,
9826 			    &spx->txlt_dma_cookie,
9827 			    &spx->txlt_curwin_num_dma_cookies);
9828 		}
9829 
9830 		switch (rval) {
9831 		case DDI_DMA_PARTIAL_MAP:
9832 			SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
9833 			    "sata_dma_buf_setup: DMA Partial Map\n", NULL);
9834 			/*
9835 			 * Partial DMA mapping.
9836 			 * Retrieve number of DMA windows for this request.
9837 			 */
9838 			if (ddi_dma_numwin(spx->txlt_buf_dma_handle,
9839 			    &spx->txlt_num_dma_win) != DDI_SUCCESS) {
9840 				if (spx->txlt_tmp_buf != NULL) {
9841 					ddi_dma_mem_free(
9842 					    &spx->txlt_tmp_buf_handle);
9843 					spx->txlt_tmp_buf = NULL;
9844 				}
9845 				(void) ddi_dma_unbind_handle(
9846 				    spx->txlt_buf_dma_handle);
9847 				(void) ddi_dma_free_handle(
9848 				    &spx->txlt_buf_dma_handle);
9849 				spx->txlt_buf_dma_handle = NULL;
9850 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9851 				    "sata_dma_buf_setup: numwin failed\n"));
9852 				return (DDI_FAILURE);
9853 			}
9854 			SATADBG2(SATA_DBG_DMA_SETUP,
9855 			    spx->txlt_sata_hba_inst,
9856 			    "sata_dma_buf_setup: windows: %d, cookies: %d\n",
9857 			    spx->txlt_num_dma_win,
9858 			    spx->txlt_curwin_num_dma_cookies);
9859 			spx->txlt_cur_dma_win = 0;
9860 			break;
9861 
9862 		case DDI_DMA_MAPPED:
9863 			/* DMA fully mapped */
9864 			spx->txlt_num_dma_win = 1;
9865 			spx->txlt_cur_dma_win = 0;
9866 			SATADBG1(SATA_DBG_DMA_SETUP,
9867 			    spx->txlt_sata_hba_inst,
9868 			    "sata_dma_buf_setup: windows: 1 "
9869 			    "cookies: %d\n", spx->txlt_curwin_num_dma_cookies);
9870 			break;
9871 
9872 		default:
9873 			/* DMA mapping failed */
9874 			if (spx->txlt_tmp_buf != NULL) {
9875 				ddi_dma_mem_free(
9876 				    &spx->txlt_tmp_buf_handle);
9877 				spx->txlt_tmp_buf = NULL;
9878 			}
9879 			(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
9880 			spx->txlt_buf_dma_handle = NULL;
9881 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9882 			    "sata_dma_buf_setup: buf dma handle binding "
9883 			    "failed %x\n", rval));
9884 			return (rval);
9885 		}
9886 		spx->txlt_curwin_processed_dma_cookies = 0;
9887 		spx->txlt_dma_cookie_list = NULL;
9888 	} else {
9889 		/*
9890 		 * DMA setup is reused. Check if we need to process more
9891 		 * cookies in current window, or to get next window, if any.
9892 		 */
9893 
9894 		ASSERT(spx->txlt_curwin_processed_dma_cookies <=
9895 		    spx->txlt_curwin_num_dma_cookies);
9896 
9897 		if (spx->txlt_curwin_processed_dma_cookies ==
9898 		    spx->txlt_curwin_num_dma_cookies) {
9899 			/*
9900 			 * All cookies from current DMA window were processed.
9901 			 * Get next DMA window.
9902 			 */
9903 			spx->txlt_cur_dma_win++;
9904 			if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) {
9905 				(void) ddi_dma_getwin(spx->txlt_buf_dma_handle,
9906 				    spx->txlt_cur_dma_win, &offset, &size,
9907 				    &spx->txlt_dma_cookie,
9908 				    &spx->txlt_curwin_num_dma_cookies);
9909 				spx->txlt_curwin_processed_dma_cookies = 0;
9910 			} else {
9911 				/* No more windows! End of request! */
9912 				/* What to do? - panic for now */
9913 				ASSERT(spx->txlt_cur_dma_win >=
9914 				    spx->txlt_num_dma_win);
9915 
9916 				spx->txlt_curwin_num_dma_cookies = 0;
9917 				spx->txlt_curwin_processed_dma_cookies = 0;
9918 				spx->txlt_sata_pkt->
9919 				    satapkt_cmd.satacmd_num_dma_cookies = 0;
9920 				return (DDI_SUCCESS);
9921 			}
9922 		}
9923 	}
9924 	/* There better be at least one DMA cookie outstanding */
9925 	ASSERT((spx->txlt_curwin_num_dma_cookies -
9926 	    spx->txlt_curwin_processed_dma_cookies) > 0);
9927 
9928 	if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) {
9929 		/* The default cookie slot was used in previous run */
9930 		ASSERT(spx->txlt_curwin_processed_dma_cookies == 0);
9931 		spx->txlt_dma_cookie_list = NULL;
9932 		spx->txlt_dma_cookie_list_len = 0;
9933 	}
9934 	if (spx->txlt_curwin_processed_dma_cookies == 0) {
9935 		/*
9936 		 * Processing a new DMA window - set-up dma cookies list.
9937 		 * We may reuse previously allocated cookie array if it is
9938 		 * possible.
9939 		 */
9940 		if (spx->txlt_dma_cookie_list != NULL &&
9941 		    spx->txlt_dma_cookie_list_len <
9942 		    spx->txlt_curwin_num_dma_cookies) {
9943 			/*
9944 			 * New DMA window contains more cookies than
9945 			 * the previous one. We need larger cookie list - free
9946 			 * the old one.
9947 			 */
9948 			(void) kmem_free(spx->txlt_dma_cookie_list,
9949 			    spx->txlt_dma_cookie_list_len *
9950 			    sizeof (ddi_dma_cookie_t));
9951 			spx->txlt_dma_cookie_list = NULL;
9952 			spx->txlt_dma_cookie_list_len = 0;
9953 		}
9954 		if (spx->txlt_dma_cookie_list == NULL) {
9955 			/*
9956 			 * Calculate lesser of number of cookies in this
9957 			 * DMA window and number of s/g entries.
9958 			 */
9959 			max_sg_len = cur_dma_attr->dma_attr_sgllen;
9960 			req_len = MIN(max_sg_len,
9961 			    spx->txlt_curwin_num_dma_cookies);
9962 
9963 			/* Allocate new dma cookie array if necessary */
9964 			if (req_len == 1) {
9965 				/* Only one cookie - no need for a list */
9966 				spx->txlt_dma_cookie_list =
9967 				    &spx->txlt_dma_cookie;
9968 				spx->txlt_dma_cookie_list_len = 1;
9969 			} else {
9970 				/*
9971 				 * More than one cookie - try to allocate space.
9972 				 */
9973 				spx->txlt_dma_cookie_list = kmem_zalloc(
9974 				    sizeof (ddi_dma_cookie_t) * req_len,
9975 				    callback == NULL_FUNC ? KM_NOSLEEP :
9976 				    KM_SLEEP);
9977 				if (spx->txlt_dma_cookie_list == NULL) {
9978 					SATADBG1(SATA_DBG_DMA_SETUP,
9979 					    spx->txlt_sata_hba_inst,
9980 					    "sata_dma_buf_setup: cookie list "
9981 					    "allocation failed\n", NULL);
9982 					/*
9983 					 * We could not allocate space for
9984 					 * neccessary number of dma cookies in
9985 					 * this window, so we fail this request.
9986 					 * Next invocation would try again to
9987 					 * allocate space for cookie list.
9988 					 * Note:Packet residue was not modified.
9989 					 */
9990 					return (DDI_DMA_NORESOURCES);
9991 				} else {
9992 					spx->txlt_dma_cookie_list_len = req_len;
9993 				}
9994 			}
9995 		}
9996 		/*
9997 		 * Fetch DMA cookies into cookie list in sata_pkt_txlate.
9998 		 * First cookie was already fetched.
9999 		 */
10000 		*(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie;
10001 		cur_txfer_len =
10002 		    (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size;
10003 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1;
10004 		spx->txlt_curwin_processed_dma_cookies++;
10005 		for (i = 1; (i < spx->txlt_dma_cookie_list_len) &&
10006 		    (i < spx->txlt_curwin_num_dma_cookies); i++) {
10007 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
10008 			    &spx->txlt_dma_cookie_list[i]);
10009 			cur_txfer_len +=
10010 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
10011 			spx->txlt_curwin_processed_dma_cookies++;
10012 			spx->txlt_sata_pkt->
10013 			    satapkt_cmd.satacmd_num_dma_cookies += 1;
10014 		}
10015 	} else {
10016 		SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
10017 		    "sata_dma_buf_setup: sliding within DMA window, "
10018 		    "cur cookie %d, total cookies %d\n",
10019 		    spx->txlt_curwin_processed_dma_cookies,
10020 		    spx->txlt_curwin_num_dma_cookies);
10021 
10022 		/*
10023 		 * Not all cookies from the current dma window were used because
10024 		 * of s/g limitation.
10025 		 * There is no need to re-size the list - it was set at
10026 		 * optimal size, or only default entry is used (s/g = 1).
10027 		 */
10028 		if (spx->txlt_dma_cookie_list == NULL) {
10029 			spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie;
10030 			spx->txlt_dma_cookie_list_len = 1;
10031 		}
10032 		/*
10033 		 * Since we are processing remaining cookies in a DMA window,
10034 		 * there may be less of them than the number of entries in the
10035 		 * current dma cookie list.
10036 		 */
10037 		req_len = MIN(spx->txlt_dma_cookie_list_len,
10038 		    (spx->txlt_curwin_num_dma_cookies -
10039 		    spx->txlt_curwin_processed_dma_cookies));
10040 
10041 		/* Fetch the next batch of cookies */
10042 		for (i = 0, cur_txfer_len = 0; i < req_len; i++) {
10043 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
10044 			    &spx->txlt_dma_cookie_list[i]);
10045 			cur_txfer_len +=
10046 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
10047 			spx->txlt_sata_pkt->
10048 			    satapkt_cmd.satacmd_num_dma_cookies++;
10049 			spx->txlt_curwin_processed_dma_cookies++;
10050 		}
10051 	}
10052 
10053 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0);
10054 
10055 	/* Point sata_cmd to the cookie list */
10056 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list =
10057 	    &spx->txlt_dma_cookie_list[0];
10058 
10059 	/* Remember number of DMA cookies passed in sata packet */
10060 	spx->txlt_num_dma_cookies =
10061 	    spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies;
10062 
10063 	ASSERT(cur_txfer_len != 0);
10064 	if (cur_txfer_len <= bp->b_bcount)
10065 		spx->txlt_total_residue -= cur_txfer_len;
10066 	else {
10067 		/*
10068 		 * Temporary DMA buffer has been padded by
10069 		 * ddi_dma_mem_alloc()!
10070 		 * This requires special handling, because DMA cookies are
10071 		 * based on the temporary buffer size, not the b_bcount,
10072 		 * and we have extra bytes to transfer - but the packet
10073 		 * residue has to stay correct because we will copy only
10074 		 * the requested number of bytes.
10075 		 */
10076 		spx->txlt_total_residue -= bp->b_bcount;
10077 	}
10078 
10079 	return (DDI_SUCCESS);
10080 }
10081 
10082 
10083 /*
10084  * Fetch Device Identify data.
10085  * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type)
10086  * command to a device and get the device identify data.
10087  * The device_info structure has to be set to device type (for selecting proper
10088  * device identify command).
10089  *
10090  * Returns:
10091  * SATA_SUCCESS if cmd succeeded
10092  * SATA_RETRY if cmd was rejected and could be retried,
10093  * SATA_FAILURE if cmd failed and should not be retried (port error)
10094  *
10095  * Cannot be called in an interrupt context.
10096  */
10097 
10098 static int
10099 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst,
10100     sata_drive_info_t *sdinfo)
10101 {
10102 	struct buf *bp;
10103 	sata_pkt_t *spkt;
10104 	sata_cmd_t *scmd;
10105 	sata_pkt_txlate_t *spx;
10106 	int rval;
10107 
10108 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10109 	spx->txlt_sata_hba_inst = sata_hba_inst;
10110 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
10111 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10112 	if (spkt == NULL) {
10113 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10114 		return (SATA_RETRY); /* may retry later */
10115 	}
10116 	/* address is needed now */
10117 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10118 
10119 	/*
10120 	 * Allocate buffer for Identify Data return data
10121 	 */
10122 	bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t));
10123 	if (bp == NULL) {
10124 		sata_pkt_free(spx);
10125 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10126 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10127 		    "sata_fetch_device_identify_data: "
10128 		    "cannot allocate buffer for ID"));
10129 		return (SATA_RETRY); /* may retry later */
10130 	}
10131 
10132 	/* Fill sata_pkt */
10133 	sdinfo->satadrv_state = SATA_STATE_PROBING;
10134 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10135 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10136 	/* Synchronous mode, no callback */
10137 	spkt->satapkt_comp = NULL;
10138 	/* Timeout 30s */
10139 	spkt->satapkt_time = sata_default_pkt_time;
10140 
10141 	scmd = &spkt->satapkt_cmd;
10142 	scmd->satacmd_bp = bp;
10143 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
10144 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10145 
10146 	/* Build Identify Device cmd in the sata_pkt */
10147 	scmd->satacmd_addr_type = 0;		/* N/A */
10148 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
10149 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
10150 	scmd->satacmd_lba_mid_lsb = 0;		/* N/A */
10151 	scmd->satacmd_lba_high_lsb = 0;		/* N/A */
10152 	scmd->satacmd_features_reg = 0;		/* N/A */
10153 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
10154 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
10155 		/* Identify Packet Device cmd */
10156 		scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE;
10157 	} else {
10158 		/* Identify Device cmd - mandatory for all other devices */
10159 		scmd->satacmd_cmd_reg = SATAC_ID_DEVICE;
10160 	}
10161 
10162 	/* Send pkt to SATA HBA driver */
10163 	rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt);
10164 
10165 #ifdef SATA_INJECT_FAULTS
10166 	if (sata_inject_fault == SATA_INJECT_PKT_FAULT)
10167 		if (sata_fault_cmd == scmd->satacmd_cmd_reg)
10168 			sata_inject_pkt_fault(spkt, scmd->satacmd_cmd_reg,
10169 			    &rval, sata_inject_fault_type);
10170 #endif
10171 
10172 	if (rval == SATA_TRAN_ACCEPTED &&
10173 	    spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10174 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10175 		    DDI_DMA_SYNC_FORKERNEL);
10176 		ASSERT(rval == DDI_SUCCESS);
10177 		if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config &
10178 		    SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) {
10179 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10180 			    "SATA disk device at port %d - "
10181 			    "partial Identify Data",
10182 			    sdinfo->satadrv_addr.cport));
10183 			rval = SATA_RETRY; /* may retry later */
10184 			goto fail;
10185 		}
10186 		/* Update sata_drive_info */
10187 		bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id,
10188 		    sizeof (sata_id_t));
10189 
10190 		sdinfo->satadrv_features_support = 0;
10191 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10192 			/*
10193 			 * Retrieve capacity (disks only) and addressing mode
10194 			 */
10195 			sdinfo->satadrv_capacity = sata_check_capacity(sdinfo);
10196 		} else {
10197 			/*
10198 			 * For ATAPI devices one would have to issue
10199 			 * Get Capacity cmd for media capacity. Not here.
10200 			 */
10201 			sdinfo->satadrv_capacity = 0;
10202 			/*
10203 			 * Check what cdb length is supported
10204 			 */
10205 			if ((sdinfo->satadrv_id.ai_config &
10206 			    SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B)
10207 				sdinfo->satadrv_atapi_cdb_len = 16;
10208 			else
10209 				sdinfo->satadrv_atapi_cdb_len = 12;
10210 		}
10211 		/* Setup supported features flags */
10212 		if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT)
10213 			sdinfo->satadrv_features_support |= SATA_DEV_F_DMA;
10214 
10215 		/* Check for SATA GEN and NCQ support */
10216 		if (sdinfo->satadrv_id.ai_satacap != 0 &&
10217 		    sdinfo->satadrv_id.ai_satacap != 0xffff) {
10218 			/* SATA compliance */
10219 			if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ)
10220 				sdinfo->satadrv_features_support |=
10221 				    SATA_DEV_F_NCQ;
10222 			if (sdinfo->satadrv_id.ai_satacap &
10223 			    (SATA_1_SPEED | SATA_2_SPEED)) {
10224 				if (sdinfo->satadrv_id.ai_satacap &
10225 				    SATA_2_SPEED)
10226 					sdinfo->satadrv_features_support |=
10227 					    SATA_DEV_F_SATA2;
10228 				if (sdinfo->satadrv_id.ai_satacap &
10229 				    SATA_1_SPEED)
10230 					sdinfo->satadrv_features_support |=
10231 					    SATA_DEV_F_SATA1;
10232 			} else {
10233 				sdinfo->satadrv_features_support |=
10234 				    SATA_DEV_F_SATA1;
10235 			}
10236 		}
10237 		if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) &&
10238 		    (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD))
10239 			sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ;
10240 
10241 		sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth;
10242 		if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) ||
10243 		    (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) {
10244 			++sdinfo->satadrv_queue_depth;
10245 			/* Adjust according to controller capabilities */
10246 			sdinfo->satadrv_max_queue_depth = MIN(
10247 			    sdinfo->satadrv_queue_depth,
10248 			    SATA_QDEPTH(sata_hba_inst));
10249 			/* Adjust according to global queue depth limit */
10250 			sdinfo->satadrv_max_queue_depth = MIN(
10251 			    sdinfo->satadrv_max_queue_depth,
10252 			    sata_current_max_qdepth);
10253 			if (sdinfo->satadrv_max_queue_depth == 0)
10254 				sdinfo->satadrv_max_queue_depth = 1;
10255 		} else
10256 			sdinfo->satadrv_max_queue_depth = 1;
10257 
10258 		rval = SATA_SUCCESS;
10259 	} else {
10260 		/*
10261 		 * Woops, no Identify Data.
10262 		 */
10263 		if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) {
10264 			rval = SATA_RETRY; /* may retry later */
10265 		} else if (rval == SATA_TRAN_ACCEPTED) {
10266 			if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR ||
10267 			    spkt->satapkt_reason == SATA_PKT_ABORTED ||
10268 			    spkt->satapkt_reason == SATA_PKT_TIMEOUT ||
10269 			    spkt->satapkt_reason == SATA_PKT_RESET)
10270 				rval = SATA_RETRY; /* may retry later */
10271 			else
10272 				rval = SATA_FAILURE;
10273 		} else {
10274 			rval = SATA_FAILURE;
10275 		}
10276 	}
10277 fail:
10278 	/* Free allocated resources */
10279 	sata_free_local_buffer(spx);
10280 	sata_pkt_free(spx);
10281 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
10282 
10283 	return (rval);
10284 }
10285 
10286 
10287 /*
10288  * Some devices may not come-up with default DMA mode (UDMA or MWDMA).
10289  * UDMA mode is checked first, followed by MWDMA mode.
10290  * set correctly, so this function is setting it to the highest supported level.
10291  * Older SATA spec required that the device supports at least DMA 4 mode and
10292  * UDMA mode is selected.  It is not mentioned in SerialATA 2.6, so this
10293  * restriction has been removed.
10294  *
10295  * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported.
10296  * Returns SATA_FAILURE if proper DMA mode could not be selected.
10297  *
10298  * NOTE: This function should be called only if DMA mode is supported.
10299  */
10300 static int
10301 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo)
10302 {
10303 	sata_pkt_t *spkt;
10304 	sata_cmd_t *scmd;
10305 	sata_pkt_txlate_t *spx;
10306 	int i, mode;
10307 	uint8_t subcmd;
10308 	int rval = SATA_SUCCESS;
10309 
10310 	ASSERT(sdinfo != NULL);
10311 	ASSERT(sata_hba_inst != NULL);
10312 
10313 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
10314 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) {
10315 		/* Find highest Ultra DMA mode supported */
10316 		for (mode = 6; mode >= 0; --mode) {
10317 			if (sdinfo->satadrv_id.ai_ultradma & (1 << mode))
10318 				break;
10319 		}
10320 #if 0
10321 		/* Left for historical reasons */
10322 		/*
10323 		 * Some initial version of SATA spec indicated that at least
10324 		 * UDMA mode 4 has to be supported. It is not mentioned in
10325 		 * SerialATA 2.6, so this restriction is removed.
10326 		 */
10327 		if (mode < 4)
10328 			return (SATA_FAILURE);
10329 #endif
10330 		/* Find UDMA mode currently selected */
10331 		for (i = 6; i >= 0; --i) {
10332 			if (sdinfo->satadrv_id.ai_ultradma & (1 << (i + 8)))
10333 				break;
10334 		}
10335 		if (i >= mode)
10336 			/* Nothing to do */
10337 			return (SATA_SUCCESS);
10338 
10339 		subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA;
10340 
10341 	} else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) {
10342 		/* Find highest MultiWord DMA mode supported */
10343 		for (mode = 2; mode >= 0; --mode) {
10344 			if (sdinfo->satadrv_id.ai_dworddma & (1 << mode))
10345 				break;
10346 		}
10347 		/* Find highest MultiWord DMA mode selected */
10348 		for (i = 2; i >= 0; --i) {
10349 			if (sdinfo->satadrv_id.ai_dworddma & (1 << (i + 8)))
10350 				break;
10351 		}
10352 		if (i >= mode)
10353 			/* Nothing to do */
10354 			return (SATA_SUCCESS);
10355 
10356 		subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA;
10357 	} else
10358 		return (SATA_SUCCESS);
10359 
10360 	/*
10361 	 * Set DMA mode via SET FEATURES COMMAND.
10362 	 * Prepare packet for SET FEATURES COMMAND.
10363 	 */
10364 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10365 	spx->txlt_sata_hba_inst = sata_hba_inst;
10366 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10367 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10368 	if (spkt == NULL) {
10369 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10370 		    "sata_set_dma_mode: could not set DMA mode %", mode));
10371 		rval = SATA_FAILURE;
10372 		goto done;
10373 	}
10374 	/* Fill sata_pkt */
10375 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10376 	/* Timeout 30s */
10377 	spkt->satapkt_time = sata_default_pkt_time;
10378 	/* Synchronous mode, no callback, interrupts */
10379 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10380 	spkt->satapkt_comp = NULL;
10381 	scmd = &spkt->satapkt_cmd;
10382 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10383 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10384 	scmd->satacmd_addr_type = 0;
10385 	scmd->satacmd_device_reg = 0;
10386 	scmd->satacmd_status_reg = 0;
10387 	scmd->satacmd_error_reg = 0;
10388 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10389 	scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE;
10390 	scmd->satacmd_sec_count_lsb = subcmd | mode;
10391 
10392 	/* Transfer command to HBA */
10393 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
10394 	    spkt) != SATA_TRAN_ACCEPTED ||
10395 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
10396 		/* Pkt execution failed */
10397 		rval = SATA_FAILURE;
10398 	}
10399 done:
10400 
10401 	/* Free allocated resources */
10402 	if (spkt != NULL)
10403 		sata_pkt_free(spx);
10404 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10405 
10406 	return (rval);
10407 }
10408 
10409 
10410 /*
10411  * Set device caching mode.
10412  * One of the following operations should be specified:
10413  * SATAC_SF_ENABLE_READ_AHEAD
10414  * SATAC_SF_DISABLE_READ_AHEAD
10415  * SATAC_SF_ENABLE_WRITE_CACHE
10416  * SATAC_SF_DISABLE_WRITE_CACHE
10417  *
10418  * If operation fails, system log messgage is emitted.
10419  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
10420  */
10421 
10422 static int
10423 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
10424     int cache_op)
10425 {
10426 	sata_pkt_t *spkt;
10427 	sata_cmd_t *scmd;
10428 	sata_pkt_txlate_t *spx;
10429 	int rval = SATA_SUCCESS;
10430 	char *infop;
10431 
10432 	ASSERT(sdinfo != NULL);
10433 	ASSERT(sata_hba_inst != NULL);
10434 	ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD ||
10435 	    cache_op == SATAC_SF_DISABLE_READ_AHEAD ||
10436 	    cache_op == SATAC_SF_ENABLE_WRITE_CACHE ||
10437 	    cache_op == SATAC_SF_DISABLE_WRITE_CACHE);
10438 
10439 
10440 	/* Prepare packet for SET FEATURES COMMAND */
10441 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10442 	spx->txlt_sata_hba_inst = sata_hba_inst;
10443 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10444 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10445 	if (spkt == NULL) {
10446 		rval = SATA_FAILURE;
10447 		goto failure;
10448 	}
10449 	/* Fill sata_pkt */
10450 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10451 	/* Timeout 30s */
10452 	spkt->satapkt_time = sata_default_pkt_time;
10453 	/* Synchronous mode, no callback, interrupts */
10454 	spkt->satapkt_op_mode =
10455 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10456 	spkt->satapkt_comp = NULL;
10457 	scmd = &spkt->satapkt_cmd;
10458 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10459 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10460 	scmd->satacmd_addr_type = 0;
10461 	scmd->satacmd_device_reg = 0;
10462 	scmd->satacmd_status_reg = 0;
10463 	scmd->satacmd_error_reg = 0;
10464 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10465 	scmd->satacmd_features_reg = cache_op;
10466 
10467 	/* Transfer command to HBA */
10468 	if (((*SATA_START_FUNC(sata_hba_inst))(
10469 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
10470 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
10471 		/* Pkt execution failed */
10472 		switch (cache_op) {
10473 		case SATAC_SF_ENABLE_READ_AHEAD:
10474 			infop = "enabling read ahead failed";
10475 			break;
10476 		case SATAC_SF_DISABLE_READ_AHEAD:
10477 			infop = "disabling read ahead failed";
10478 			break;
10479 		case SATAC_SF_ENABLE_WRITE_CACHE:
10480 			infop = "enabling write cache failed";
10481 			break;
10482 		case SATAC_SF_DISABLE_WRITE_CACHE:
10483 			infop = "disabling write cache failed";
10484 			break;
10485 		}
10486 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
10487 		rval = SATA_FAILURE;
10488 	}
10489 failure:
10490 	/* Free allocated resources */
10491 	if (spkt != NULL)
10492 		sata_pkt_free(spx);
10493 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10494 	return (rval);
10495 }
10496 
10497 /*
10498  * Set Removable Media Status Notification (enable/disable)
10499  * state == 0 , disable
10500  * state != 0 , enable
10501  *
10502  * If operation fails, system log messgage is emitted.
10503  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
10504  */
10505 
10506 static int
10507 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
10508     int state)
10509 {
10510 	sata_pkt_t *spkt;
10511 	sata_cmd_t *scmd;
10512 	sata_pkt_txlate_t *spx;
10513 	int rval = SATA_SUCCESS;
10514 	char *infop;
10515 
10516 	ASSERT(sdinfo != NULL);
10517 	ASSERT(sata_hba_inst != NULL);
10518 
10519 	/* Prepare packet for SET FEATURES COMMAND */
10520 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10521 	spx->txlt_sata_hba_inst = sata_hba_inst;
10522 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10523 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10524 	if (spkt == NULL) {
10525 		rval = SATA_FAILURE;
10526 		goto failure;
10527 	}
10528 	/* Fill sata_pkt */
10529 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10530 	/* Timeout 30s */
10531 	spkt->satapkt_time = sata_default_pkt_time;
10532 	/* Synchronous mode, no callback, interrupts */
10533 	spkt->satapkt_op_mode =
10534 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10535 	spkt->satapkt_comp = NULL;
10536 	scmd = &spkt->satapkt_cmd;
10537 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10538 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10539 	scmd->satacmd_addr_type = 0;
10540 	scmd->satacmd_device_reg = 0;
10541 	scmd->satacmd_status_reg = 0;
10542 	scmd->satacmd_error_reg = 0;
10543 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10544 	if (state == 0)
10545 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN;
10546 	else
10547 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN;
10548 
10549 	/* Transfer command to HBA */
10550 	if (((*SATA_START_FUNC(sata_hba_inst))(
10551 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
10552 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
10553 		/* Pkt execution failed */
10554 		if (state == 0)
10555 			infop = "disabling Removable Media Status "
10556 			    "Notification failed";
10557 		else
10558 			infop = "enabling Removable Media Status "
10559 			    "Notification failed";
10560 
10561 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
10562 		rval = SATA_FAILURE;
10563 	}
10564 failure:
10565 	/* Free allocated resources */
10566 	if (spkt != NULL)
10567 		sata_pkt_free(spx);
10568 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10569 	return (rval);
10570 }
10571 
10572 
10573 /*
10574  * Update port SCR block
10575  */
10576 static void
10577 sata_update_port_scr(sata_port_scr_t *port_scr, sata_device_t *device)
10578 {
10579 	port_scr->sstatus = device->satadev_scr.sstatus;
10580 	port_scr->serror = device->satadev_scr.serror;
10581 	port_scr->scontrol = device->satadev_scr.scontrol;
10582 	port_scr->sactive = device->satadev_scr.sactive;
10583 	port_scr->snotific = device->satadev_scr.snotific;
10584 }
10585 
10586 /*
10587  * Update state and copy port ss* values from passed sata_device structure.
10588  * sata_address is validated - if not valid, nothing is changed in sata_scsi
10589  * configuration struct.
10590  *
10591  * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function
10592  * regardless of the state in device argument.
10593  *
10594  * Port mutex should be held while calling this function.
10595  */
10596 static void
10597 sata_update_port_info(sata_hba_inst_t *sata_hba_inst,
10598 	sata_device_t *sata_device)
10599 {
10600 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst,
10601 	    sata_device->satadev_addr.cport)));
10602 
10603 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT ||
10604 	    sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
10605 
10606 		sata_cport_info_t *cportinfo;
10607 
10608 		if (SATA_NUM_CPORTS(sata_hba_inst) <=
10609 		    sata_device->satadev_addr.cport)
10610 			return;
10611 
10612 		cportinfo = SATA_CPORT_INFO(sata_hba_inst,
10613 		    sata_device->satadev_addr.cport);
10614 		sata_update_port_scr(&cportinfo->cport_scr, sata_device);
10615 
10616 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
10617 		cportinfo->cport_state &= ~(SATA_PSTATE_PWRON |
10618 		    SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
10619 		cportinfo->cport_state |=
10620 		    sata_device->satadev_state & SATA_PSTATE_VALID;
10621 	} else {
10622 		sata_pmport_info_t *pmportinfo;
10623 
10624 		if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT) ||
10625 		    (sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) ||
10626 		    SATA_NUM_PMPORTS(sata_hba_inst,
10627 		    sata_device->satadev_addr.cport) <
10628 		    sata_device->satadev_addr.pmport)
10629 			return;
10630 
10631 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
10632 		    sata_device->satadev_addr.cport,
10633 		    sata_device->satadev_addr.pmport);
10634 		sata_update_port_scr(&pmportinfo->pmport_scr, sata_device);
10635 
10636 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
10637 		pmportinfo->pmport_state &=
10638 		    ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF |
10639 		    SATA_PSTATE_FAILED);
10640 		pmportinfo->pmport_state |=
10641 		    sata_device->satadev_state & SATA_PSTATE_VALID;
10642 	}
10643 }
10644 
10645 
10646 
10647 /*
10648  * Extract SATA port specification from an IOCTL argument.
10649  *
10650  * This function return the port the user land send us as is, unless it
10651  * cannot retrieve port spec, then -1 is returned.
10652  *
10653  * Note: Only cport  - no port multiplier port.
10654  */
10655 static int32_t
10656 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp)
10657 {
10658 	int32_t port;
10659 
10660 	/* Extract port number from nvpair in dca structure  */
10661 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
10662 		SATA_LOG_D((sata_hba_inst, CE_NOTE,
10663 		    "sata_get_port_num: invalid port spec 0x%x in ioctl",
10664 		    port));
10665 		port = -1;
10666 	}
10667 
10668 	return (port);
10669 }
10670 
10671 /*
10672  * Get dev_info_t pointer to the device node pointed to by port argument.
10673  * NOTE: target argument is a value used in ioctls to identify
10674  * the AP - it is not a sata_address.
10675  * It is a combination of cport, pmport and address qualifier, encodded same
10676  * way as a scsi target number.
10677  * At this moment it carries only cport number.
10678  *
10679  * No PMult hotplug support.
10680  *
10681  * Returns dev_info_t pointer if target device was found, NULL otherwise.
10682  */
10683 
10684 static dev_info_t *
10685 sata_get_target_dip(dev_info_t *dip, int32_t port)
10686 {
10687 	dev_info_t	*cdip = NULL;
10688 	int		target, tgt;
10689 	int		ncport;
10690 	int 		circ;
10691 
10692 	ncport = port & SATA_CFGA_CPORT_MASK;
10693 	target = SATA_TO_SCSI_TARGET(ncport, 0, SATA_ADDR_DCPORT);
10694 
10695 	ndi_devi_enter(dip, &circ);
10696 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
10697 		dev_info_t *next = ddi_get_next_sibling(cdip);
10698 
10699 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
10700 		    DDI_PROP_DONTPASS, "target", -1);
10701 		if (tgt == -1) {
10702 			/*
10703 			 * This is actually an error condition, but not
10704 			 * a fatal one. Just continue the search.
10705 			 */
10706 			cdip = next;
10707 			continue;
10708 		}
10709 
10710 		if (tgt == target)
10711 			break;
10712 
10713 		cdip = next;
10714 	}
10715 	ndi_devi_exit(dip, circ);
10716 
10717 	return (cdip);
10718 }
10719 
10720 /*
10721  * Get dev_info_t pointer to the device node pointed to by port argument.
10722  * NOTE: target argument is a value used in ioctls to identify
10723  * the AP - it is not a sata_address.
10724  * It is a combination of cport, pmport and address qualifier, encoded same
10725  * way as a scsi target number.
10726  * At this moment it carries only cport number.
10727  *
10728  * No PMult hotplug support.
10729  *
10730  * Returns dev_info_t pointer if target device was found, NULL otherwise.
10731  */
10732 
10733 static dev_info_t *
10734 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr)
10735 {
10736 	dev_info_t	*cdip = NULL;
10737 	int		target, tgt;
10738 	int 		circ;
10739 
10740 	target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual);
10741 
10742 	ndi_devi_enter(dip, &circ);
10743 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
10744 		dev_info_t *next = ddi_get_next_sibling(cdip);
10745 
10746 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
10747 		    DDI_PROP_DONTPASS, "target", -1);
10748 		if (tgt == -1) {
10749 			/*
10750 			 * This is actually an error condition, but not
10751 			 * a fatal one. Just continue the search.
10752 			 */
10753 			cdip = next;
10754 			continue;
10755 		}
10756 
10757 		if (tgt == target)
10758 			break;
10759 
10760 		cdip = next;
10761 	}
10762 	ndi_devi_exit(dip, circ);
10763 
10764 	return (cdip);
10765 }
10766 
10767 /*
10768  * Process sata port disconnect request.
10769  * Normally, cfgadm sata plugin will try to offline (unconfigure) the device
10770  * before this request. Nevertheless, if a device is still configured,
10771  * we need to attempt to offline and unconfigure device.
10772  * Regardless of the unconfigure operation results the port is marked as
10773  * deactivated and no access to the attached device is possible.
10774  * If the target node remains because unconfigure operation failed, its state
10775  * will be set to DEVICE_REMOVED, preventing it to be used again when a device
10776  * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure
10777  * the device and remove old target node.
10778  *
10779  * This function invokes sata_hba_inst->satahba_tran->
10780  * sata_tran_hotplug_ops->sata_tran_port_deactivate().
10781  * If successful, the device structure (if any) attached to the specified port
10782  * is removed and state of the port marked appropriately.
10783  * Failure of the port_deactivate may keep port in the physically active state,
10784  * or may fail the port.
10785  *
10786  * NOTE: Port multiplier code is not completed nor tested.
10787  */
10788 
10789 static int
10790 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst,
10791     sata_device_t *sata_device)
10792 {
10793 	sata_drive_info_t *sdinfo = NULL;
10794 	sata_cport_info_t *cportinfo = NULL;
10795 	sata_pmport_info_t *pmportinfo = NULL;
10796 	sata_pmult_info_t *pmultinfo = NULL;
10797 	dev_info_t *tdip;
10798 	int cport, pmport, qual;
10799 	int rval = SATA_SUCCESS;
10800 	int rv = 0;
10801 
10802 	cport = sata_device->satadev_addr.cport;
10803 	pmport = sata_device->satadev_addr.pmport;
10804 	qual = sata_device->satadev_addr.qual;
10805 
10806 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
10807 
10808 	/*
10809 	 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran->
10810 	 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
10811 	 * Do the sanity check.
10812 	 */
10813 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
10814 		/* No physical port deactivation supported. */
10815 		return (EINVAL);
10816 	}
10817 
10818 	/* Check the current state of the port */
10819 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10820 	    (SATA_DIP(sata_hba_inst), sata_device);
10821 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10822 	sata_update_port_info(sata_hba_inst, sata_device);
10823 	if (rval != SATA_SUCCESS ||
10824 	    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
10825 		/* Device port status is unknown or it is in failed state */
10826 		if (qual == SATA_ADDR_PMPORT) {
10827 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
10828 			    SATA_PSTATE_FAILED;
10829 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
10830 			    "sata_hba_ioctl: connect: failed to deactivate "
10831 			    "SATA port %d", cport);
10832 		} else {
10833 			SATA_CPORT_STATE(sata_hba_inst, cport) =
10834 			    SATA_PSTATE_FAILED;
10835 			SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
10836 			    "sata_hba_ioctl: connect: failed to deactivate "
10837 			    "SATA port %d:%d", cport, pmport);
10838 		}
10839 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
10840 		    cport)->cport_mutex);
10841 		return (EIO);
10842 	}
10843 	/*
10844 	 * Set port's dev_state to not ready - this will disable
10845 	 * an access to a potentially attached device.
10846 	 */
10847 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
10848 	if (qual == SATA_ADDR_PMPORT) {
10849 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
10850 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
10851 			sdinfo = pmportinfo->pmport_sata_drive;
10852 			ASSERT(sdinfo != NULL);
10853 		}
10854 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
10855 	} else {
10856 		/* Assuming cport */
10857 
10858 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
10859 			if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
10860 				pmultinfo =
10861 				    cportinfo->cport_devp.cport_sata_pmult;
10862 				ASSERT(pmultinfo != NULL);
10863 			} else {
10864 				sdinfo = cportinfo->cport_devp.cport_sata_drive;
10865 			}
10866 		}
10867 		cportinfo->cport_state &= ~SATA_STATE_READY;
10868 	}
10869 	if (sdinfo != NULL) {
10870 		if ((sdinfo->satadrv_type & (SATA_VALID_DEV_TYPE)) != 0) {
10871 			/*
10872 			 * If a target node exists, try to offline
10873 			 * a device and remove target node.
10874 			 */
10875 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
10876 			    cport)->cport_mutex);
10877 			/* We are addressing attached device, not a port */
10878 			sata_device->satadev_addr.qual =
10879 			    sdinfo->satadrv_addr.qual;
10880 			tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
10881 			    &sata_device->satadev_addr);
10882 			if (tdip != NULL && ndi_devi_offline(tdip,
10883 			    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
10884 				/*
10885 				 * Problem
10886 				 * The target node remained attached.
10887 				 * This happens when the device file was open
10888 				 * or a node was waiting for resources.
10889 				 * Cannot do anything about it.
10890 				 */
10891 				if (qual == SATA_ADDR_CPORT) {
10892 					SATA_LOG_D((sata_hba_inst, CE_WARN,
10893 					    "sata_hba_ioctl: disconnect: could "
10894 					    "not unconfigure device before "
10895 					    "disconnecting the SATA port %d",
10896 					    cport));
10897 				} else {
10898 					SATA_LOG_D((sata_hba_inst, CE_WARN,
10899 					    "sata_hba_ioctl: disconnect: could "
10900 					    "not unconfigure device before "
10901 					    "disconnecting the SATA port %d:%d",
10902 					    cport, pmport));
10903 				}
10904 				/*
10905 				 * Set DEVICE REMOVED state in the target
10906 				 * node. It will prevent access to the device
10907 				 * even when a new device is attached, until
10908 				 * the old target node is released, removed and
10909 				 * recreated for a new  device.
10910 				 */
10911 				sata_set_device_removed(tdip);
10912 
10913 				/*
10914 				 * Instruct event daemon to try the target
10915 				 * node cleanup later.
10916 				 */
10917 				sata_set_target_node_cleanup(
10918 				    sata_hba_inst, &sata_device->satadev_addr);
10919 			}
10920 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
10921 			    cport)->cport_mutex);
10922 		}
10923 
10924 		/* Remove and release sata_drive info structure. */
10925 		if (pmportinfo != NULL) {
10926 			SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport) =
10927 			    NULL;
10928 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
10929 		} else {
10930 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
10931 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
10932 		}
10933 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
10934 	}
10935 #if 0
10936 	else if (pmultinfo != NULL) {
10937 		/*
10938 		 * Port Multiplier itself needs special handling.
10939 		 * All device ports need to be processed here!
10940 		 */
10941 	}
10942 #endif
10943 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10944 	/* Just ask HBA driver to deactivate port */
10945 	/*	sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; */
10946 
10947 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
10948 	    (SATA_DIP(sata_hba_inst), sata_device);
10949 
10950 	/*
10951 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
10952 	 * without the hint (to force listener to investivate the state).
10953 	 */
10954 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
10955 	    SE_NO_HINT);
10956 
10957 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10958 	sata_update_port_info(sata_hba_inst, sata_device);
10959 
10960 	if (rval != SATA_SUCCESS) {
10961 		/*
10962 		 * Port deactivation failure - do not
10963 		 * change port state unless the state
10964 		 * returned by HBA indicates a port failure.
10965 		 * NOTE: device structures were released, so devices now are
10966 		 * invisible! Port reset is needed to re-enumerate devices.
10967 		 */
10968 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
10969 			if (pmportinfo != NULL)
10970 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
10971 			else
10972 				cportinfo->cport_state = SATA_PSTATE_FAILED;
10973 			rv = EIO;
10974 		}
10975 	} else {
10976 		/*
10977 		 * Deactivation succeded. From now on the sata framework
10978 		 * will not care what is happening to the device, until
10979 		 * the port is activated again.
10980 		 */
10981 		cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
10982 	}
10983 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10984 	return (rv);
10985 }
10986 
10987 
10988 
10989 /*
10990  * Process sata port connect request
10991  * The sata cfgadm pluging will invoke this operation only if port was found
10992  * in the disconnect state (failed state is also treated as the disconnected
10993  * state).
10994  * DEVCTL_AP_CONNECT would invoke  sata_hba_inst->satahba_tran->
10995  * sata_tran_hotplug_ops->sata_tran_port_activate().
10996  * If successful and a device is found attached to the port,
10997  * the initialization sequence is executed to attach a device structure to
10998  * a port structure. The state of the port and a device would be set
10999  * appropriately.
11000  * The device is not set in configured state (system-wise) by this operation.
11001  *
11002  * Note, that activating the port may generate link events,
11003  * so it is important that following processing and the
11004  * event processing does not interfere with each other!
11005  *
11006  * This operation may remove port failed state and will
11007  * try to make port active and in good standing.
11008  *
11009  * NOTE: Port multiplier code is not completed nor tested.
11010  */
11011 
11012 static int
11013 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst,
11014     sata_device_t *sata_device)
11015 {
11016 	int cport, pmport, qual;
11017 	int rv = 0;
11018 
11019 	cport = sata_device->satadev_addr.cport;
11020 	pmport = sata_device->satadev_addr.pmport;
11021 	qual = sata_device->satadev_addr.qual;
11022 
11023 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
11024 
11025 	/*
11026 	 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->
11027 	 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate().
11028 	 * Perform sanity check now.
11029 	 */
11030 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) {
11031 		/* No physical port activation supported. */
11032 		return (EINVAL);
11033 	}
11034 
11035 	/* Just ask HBA driver to activate port */
11036 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11037 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11038 		/*
11039 		 * Port activation failure.
11040 		 */
11041 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11042 		    cport)->cport_mutex);
11043 		sata_update_port_info(sata_hba_inst, sata_device);
11044 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11045 			if (qual == SATA_ADDR_DCPORT) {
11046 				SATA_CPORT_STATE(sata_hba_inst, cport) =
11047 				    SATA_PSTATE_FAILED;
11048 				SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
11049 				    "sata_hba_ioctl: connect: failed to "
11050 				    "activate SATA port %d", cport);
11051 			} else { /* port multiplier device port */
11052 				SATA_PMPORT_STATE(sata_hba_inst, cport,
11053 				    pmport) = SATA_PSTATE_FAILED;
11054 				SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
11055 				    "sata_hba_ioctl: connect: failed to "
11056 				    "activate SATA port %d:%d", cport, pmport);
11057 
11058 			}
11059 		}
11060 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11061 		    cport)->cport_mutex);
11062 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
11063 		    "sata_hba_ioctl: connect: failed to activate SATA "
11064 		    "port %d:%d", cport, pmport);
11065 		return (EIO);
11066 	}
11067 
11068 	/* Virgin port state - will be updated by the port re-probe. */
11069 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11070 	if (qual == SATA_ADDR_CPORT)
11071 		SATA_CPORT_STATE(sata_hba_inst, cport) = 0;
11072 	else /* port multiplier device port */
11073 		SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0;
11074 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11075 
11076 	/*
11077 	 * Probe the port to find its state and attached device.
11078 	 */
11079 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11080 	    SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE)
11081 		rv = EIO;
11082 
11083 	/*
11084 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11085 	 * without the hint
11086 	 */
11087 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11088 	    SE_NO_HINT);
11089 
11090 	/*
11091 	 * If there is a device attached to the port, emit
11092 	 * a message.
11093 	 */
11094 	if (sata_device->satadev_type != SATA_DTYPE_NONE) {
11095 
11096 		if (qual == SATA_ADDR_CPORT) {
11097 			sata_log(sata_hba_inst, CE_WARN,
11098 			    "SATA device detected at port %d", cport);
11099 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11100 				/*
11101 				 * A device was not successfully identified
11102 				 */
11103 				sata_log(sata_hba_inst, CE_WARN,
11104 				    "Could not identify SATA "
11105 				    "device at port %d", cport);
11106 			}
11107 		} else { /* port multiplier device port */
11108 			sata_log(sata_hba_inst, CE_WARN,
11109 			    "SATA device detected at port %d:%d",
11110 			    cport, pmport);
11111 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11112 				/*
11113 				 * A device was not successfully identified
11114 				 */
11115 				sata_log(sata_hba_inst, CE_WARN,
11116 				    "Could not identify SATA "
11117 				    "device at port %d:%d", cport, pmport);
11118 			}
11119 		}
11120 	}
11121 
11122 	return (rv);
11123 }
11124 
11125 
11126 /*
11127  * Process sata device unconfigure request.
11128  * The unconfigure operation uses generic nexus operation to
11129  * offline a device. It leaves a target device node attached.
11130  * and obviously sata_drive_info attached as well, because
11131  * from the hardware point of view nothing has changed.
11132  */
11133 static int
11134 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst,
11135     sata_device_t *sata_device)
11136 {
11137 	int rv = 0;
11138 	dev_info_t *tdip;
11139 
11140 	/* We are addressing attached device, not a port */
11141 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
11142 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11143 	else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT)
11144 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11145 
11146 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11147 	    &sata_device->satadev_addr)) != NULL) {
11148 
11149 		if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) {
11150 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11151 			    "sata_hba_ioctl: unconfigure: "
11152 			    "failed to unconfigure device at SATA port %d:%d",
11153 			    sata_device->satadev_addr.cport,
11154 			    sata_device->satadev_addr.pmport));
11155 			rv = EIO;
11156 		}
11157 		/*
11158 		 * The target node devi_state should be marked with
11159 		 * DEVI_DEVICE_OFFLINE by ndi_devi_offline().
11160 		 * This would be the indication for cfgadm that
11161 		 * the AP node occupant state is 'unconfigured'.
11162 		 */
11163 
11164 	} else {
11165 		/*
11166 		 * This would indicate a failure on the part of cfgadm
11167 		 * to detect correct state of the node prior to this
11168 		 * call - one cannot unconfigure non-existing device.
11169 		 */
11170 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11171 		    "sata_hba_ioctl: unconfigure: "
11172 		    "attempt to unconfigure non-existing device "
11173 		    "at SATA port %d:%d",
11174 		    sata_device->satadev_addr.cport,
11175 		    sata_device->satadev_addr.pmport));
11176 		rv = ENXIO;
11177 	}
11178 	return (rv);
11179 }
11180 
11181 /*
11182  * Process sata device configure request
11183  * If port is in a failed state, operation is aborted - one has to use
11184  * an explicit connect or port activate request to try to get a port into
11185  * non-failed mode. Port reset wil also work in such situation.
11186  * If the port is in disconnected (shutdown) state, the connect operation is
11187  * attempted prior to any other action.
11188  * When port is in the active state, there is a device attached and the target
11189  * node exists, a device was most likely offlined.
11190  * If target node does not exist, a new target node is created. In both cases
11191  * an attempt is made to online (configure) the device.
11192  *
11193  * NOTE: Port multiplier code is not completed nor tested.
11194  */
11195 static int
11196 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst,
11197     sata_device_t *sata_device)
11198 {
11199 	int cport, pmport, qual;
11200 	int rval;
11201 	boolean_t target = TRUE;
11202 	sata_cport_info_t *cportinfo;
11203 	sata_pmport_info_t *pmportinfo = NULL;
11204 	dev_info_t *tdip;
11205 	sata_drive_info_t *sdinfo;
11206 
11207 	cport = sata_device->satadev_addr.cport;
11208 	pmport = sata_device->satadev_addr.pmport;
11209 	qual = sata_device->satadev_addr.qual;
11210 
11211 	/* Get current port state */
11212 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11213 	    (SATA_DIP(sata_hba_inst), sata_device);
11214 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11215 	sata_update_port_info(sata_hba_inst, sata_device);
11216 
11217 	if (rval != SATA_SUCCESS ||
11218 	    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
11219 		/*
11220 		 * Obviously, device on a failed port is not visible
11221 		 */
11222 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11223 		return (ENXIO);
11224 	}
11225 
11226 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11227 	if (qual == SATA_ADDR_PMPORT)
11228 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11229 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11230 
11231 	if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) {
11232 		/* need to activate port */
11233 		target = FALSE;
11234 
11235 		/* Sanity check */
11236 		if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
11237 			return (ENXIO);
11238 
11239 		/* Just let HBA driver to activate port */
11240 		if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11241 		    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11242 			/*
11243 			 * Port activation failure - do not change port state
11244 			 * unless the state returned by HBA indicates a port
11245 			 * failure.
11246 			 */
11247 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11248 			    cport)->cport_mutex);
11249 			sata_update_port_info(sata_hba_inst, sata_device);
11250 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11251 				if (qual == SATA_ADDR_PMPORT)
11252 					pmportinfo->pmport_state =
11253 					    SATA_PSTATE_FAILED;
11254 				else
11255 					cportinfo->cport_state =
11256 					    SATA_PSTATE_FAILED;
11257 			}
11258 			mutex_exit(&SATA_CPORT_INFO(
11259 			    sata_hba_inst, cport)->cport_mutex);
11260 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11261 			    "sata_hba_ioctl: configure: "
11262 			    "failed to activate SATA port %d:%d",
11263 			    cport, pmport));
11264 			return (EIO);
11265 		}
11266 		/*
11267 		 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11268 		 * without the hint.
11269 		 */
11270 		sata_gen_sysevent(sata_hba_inst,
11271 		    &sata_device->satadev_addr, SE_NO_HINT);
11272 
11273 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11274 		    cport_mutex);
11275 		/* Virgin port state */
11276 		if (qual == SATA_ADDR_PMPORT)
11277 			pmportinfo->pmport_state = 0;
11278 		else
11279 			cportinfo->cport_state = 0;
11280 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11281 	}
11282 	/*
11283 	 * Always reprobe port, to get current device info.
11284 	 */
11285 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11286 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
11287 		return (EIO);
11288 
11289 	if (sata_device->satadev_type != SATA_DTYPE_NONE && target == FALSE) {
11290 		if (qual == SATA_ADDR_PMPORT) {
11291 			/*
11292 			 * That's the transition from "inactive" port
11293 			 * to active one with device attached.
11294 			 */
11295 			sata_log(sata_hba_inst, CE_WARN,
11296 			    "SATA device detected at port %d:%d",
11297 			    cport, pmport);
11298 		} else {
11299 			/*
11300 			 * When PM is attached to the cport and cport is
11301 			 * activated, every PM device port needs to be reprobed.
11302 			 * We need to emit message for all devices detected
11303 			 * at port multiplier's device ports.
11304 			 * Add such code here.
11305 			 * For now, just inform about device attached to
11306 			 * cport.
11307 			 */
11308 			sata_log(sata_hba_inst, CE_WARN,
11309 			    "SATA device detected at port %d", cport);
11310 		}
11311 	}
11312 
11313 	/*
11314 	 * This is where real configuration operation starts.
11315 	 *
11316 	 * When PM is attached to the cport and cport is activated,
11317 	 * devices attached PM device ports may have to be configured
11318 	 * explicitly. This may change when port multiplier is supported.
11319 	 * For now, configure only disks and other valid target devices.
11320 	 */
11321 	if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) {
11322 		if (qual == SATA_ADDR_CPORT) {
11323 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11324 				/*
11325 				 * A device was not successfully identified
11326 				 */
11327 				sata_log(sata_hba_inst, CE_WARN,
11328 				    "Could not identify SATA "
11329 				    "device at port %d", cport);
11330 			}
11331 		} else { /* port multiplier device port */
11332 			if (sata_device->satadev_type == SATA_DTYPE_UNKNOWN) {
11333 				/*
11334 				 * A device was not successfully identified
11335 				 */
11336 				sata_log(sata_hba_inst, CE_WARN,
11337 				    "Could not identify SATA "
11338 				    "device at port %d:%d", cport, pmport);
11339 			}
11340 		}
11341 		return (ENXIO);		/* No device to configure */
11342 	}
11343 
11344 	/*
11345 	 * Here we may have a device in reset condition,
11346 	 * but because we are just configuring it, there is
11347 	 * no need to process the reset other than just
11348 	 * to clear device reset condition in the HBA driver.
11349 	 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will
11350 	 * cause a first command sent the HBA driver with the request
11351 	 * to clear device reset condition.
11352 	 */
11353 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11354 	if (qual == SATA_ADDR_PMPORT)
11355 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11356 	else
11357 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11358 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
11359 	if (sdinfo == NULL) {
11360 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11361 		return (ENXIO);
11362 	}
11363 	if (sdinfo->satadrv_event_flags &
11364 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
11365 		sdinfo->satadrv_event_flags = 0;
11366 	}
11367 	sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
11368 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11369 
11370 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11371 	    &sata_device->satadev_addr)) != NULL) {
11372 		/*
11373 		 * Target node exists. Verify, that it belongs
11374 		 * to existing, attached device and not to
11375 		 * a removed device.
11376 		 */
11377 		if (sata_check_device_removed(tdip) == B_TRUE) {
11378 			if (qual == SATA_ADDR_DPMPORT)
11379 				sata_log(sata_hba_inst, CE_WARN,
11380 				    "SATA device at port %d cannot be "
11381 				    "configured. "
11382 				    "Application(s) accessing "
11383 				    "previously attached device "
11384 				    "have to release it before newly "
11385 				    "inserted device can be made accessible.",
11386 				    cport);
11387 			else
11388 				sata_log(sata_hba_inst, CE_WARN,
11389 				    "SATA device at port %d:%d cannot be"
11390 				    "configured. "
11391 				    "Application(s) accessing "
11392 				    "previously attached device "
11393 				    "have to release it before newly "
11394 				    "inserted device can be made accessible.",
11395 				    cport, pmport);
11396 			return (EIO);
11397 		}
11398 		/*
11399 		 * Device was not removed and re-inserted.
11400 		 * Try to online it.
11401 		 */
11402 		if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) {
11403 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11404 			    "sata_hba_ioctl: configure: "
11405 			    "onlining device at SATA port "
11406 			    "%d:%d failed", cport, pmport));
11407 			return (EIO);
11408 		}
11409 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11410 		    cport)->cport_mutex);
11411 
11412 		if (qual == SATA_ADDR_DPMPORT)
11413 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
11414 		else
11415 			cportinfo-> cport_tgtnode_clean = B_TRUE;
11416 
11417 		mutex_exit(&SATA_CPORT_INFO(
11418 		    sata_hba_inst, cport)->cport_mutex);
11419 	} else {
11420 		/*
11421 		 * No target node - need to create a new target node.
11422 		 */
11423 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11424 		    cport_mutex);
11425 		if (qual == SATA_ADDR_DPMPORT)
11426 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
11427 		else
11428 			cportinfo-> cport_tgtnode_clean = B_TRUE;
11429 
11430 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11431 		    cport_mutex);
11432 		tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
11433 		    sata_hba_inst, &sata_device->satadev_addr);
11434 		if (tdip == NULL) {
11435 			/* Configure operation failed */
11436 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11437 			    "sata_hba_ioctl: configure: "
11438 			    "configuring SATA device at port %d:%d "
11439 			    "failed", cport, pmport));
11440 			return (EIO);
11441 		}
11442 	}
11443 	return (0);
11444 }
11445 
11446 
11447 /*
11448  * Process ioctl deactivate port request.
11449  * Arbitrarily unconfigure attached device, if any.
11450  * Even if the unconfigure fails, proceed with the
11451  * port deactivation.
11452  *
11453  * NOTE: Port Multiplier code is not completed and tested.
11454  */
11455 
11456 static int
11457 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst,
11458     sata_device_t *sata_device)
11459 {
11460 	int cport, pmport, qual;
11461 	int rval, rv = 0;
11462 	sata_cport_info_t *cportinfo;
11463 	sata_pmport_info_t *pmportinfo = NULL;
11464 	dev_info_t *tdip;
11465 	sata_drive_info_t *sdinfo = NULL;
11466 
11467 	/* Sanity check */
11468 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL)
11469 		return (ENOTSUP);
11470 
11471 	cport = sata_device->satadev_addr.cport;
11472 	pmport = sata_device->satadev_addr.pmport;
11473 	qual = sata_device->satadev_addr.qual;
11474 
11475 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11476 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11477 	if (qual == SATA_ADDR_CPORT) {
11478 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11479 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
11480 			/*
11481 			 * For now, assume that port multiplier is not
11482 			 * supported, i.e. deal only with valid devices
11483 			 */
11484 			if ((cportinfo->cport_dev_type &
11485 			    SATA_VALID_DEV_TYPE) != 0)
11486 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
11487 			/*
11488 			 * If attached device is a port multiplier, we will
11489 			 * have to unconfigure all devices attached to the
11490 			 * port multiplier. Add this code here.
11491 			 */
11492 		}
11493 		cportinfo->cport_state &= ~SATA_STATE_READY;
11494 	} else {
11495 		/* Port multiplier device port */
11496 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11497 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11498 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
11499 		    (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0)
11500 			sdinfo = SATA_PMPORTINFO_DRV_INFO(pmportinfo);
11501 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
11502 	}
11503 
11504 	if (sdinfo != NULL) {
11505 		/*
11506 		 * If a target node exists, try to offline a device and
11507 		 * to remove a target node.
11508 		 */
11509 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11510 		    cport_mutex);
11511 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11512 		    &sata_device->satadev_addr);
11513 		if (tdip != NULL) {
11514 			/* target node exist */
11515 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
11516 			    "sata_hba_ioctl: port deactivate: "
11517 			    "target node exists.", NULL);
11518 
11519 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) !=
11520 			    NDI_SUCCESS) {
11521 				SATA_LOG_D((sata_hba_inst, CE_WARN,
11522 				    "sata_hba_ioctl: port deactivate: "
11523 				    "failed to unconfigure device at port "
11524 				    "%d:%d before deactivating the port",
11525 				    cport, pmport));
11526 				/*
11527 				 * Set DEVICE REMOVED state in the target
11528 				 * node. It will prevent an access to
11529 				 * the device even when a new device is
11530 				 * attached, until the old target node is
11531 				 * released, removed and recreated for a new
11532 				 * device.
11533 				 */
11534 				sata_set_device_removed(tdip);
11535 
11536 				/*
11537 				 * Instruct the event daemon to try the
11538 				 * target node cleanup later.
11539 				 */
11540 				sata_set_target_node_cleanup(sata_hba_inst,
11541 				    &sata_device->satadev_addr);
11542 			}
11543 		}
11544 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11545 		    cport_mutex);
11546 		/*
11547 		 * In any case, remove and release sata_drive_info
11548 		 * structure.
11549 		 */
11550 		if (qual == SATA_ADDR_CPORT) {
11551 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11552 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11553 		} else { /* port multiplier device port */
11554 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11555 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11556 		}
11557 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
11558 	}
11559 	if (qual == SATA_ADDR_CPORT) {
11560 		cportinfo->cport_state &= ~(SATA_STATE_PROBED |
11561 		    SATA_STATE_PROBING);
11562 	} else { /* port multiplier device port */
11563 		pmportinfo->pmport_state &= ~(SATA_STATE_PROBED |
11564 		    SATA_STATE_PROBING);
11565 	}
11566 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11567 
11568 	/* Just let HBA driver to deactivate port */
11569 	sata_device->satadev_addr.qual = qual;
11570 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
11571 	    (SATA_DIP(sata_hba_inst), sata_device);
11572 
11573 	/*
11574 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11575 	 * without the hint
11576 	 */
11577 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11578 	    SE_NO_HINT);
11579 
11580 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11581 	sata_update_port_info(sata_hba_inst, sata_device);
11582 	if (qual == SATA_ADDR_CPORT) {
11583 		if (rval != SATA_SUCCESS) {
11584 			/*
11585 			 * Port deactivation failure - do not change port state
11586 			 * unless the state returned by HBA indicates a port
11587 			 * failure.
11588 			 */
11589 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11590 				SATA_CPORT_STATE(sata_hba_inst, cport) =
11591 				    SATA_PSTATE_FAILED;
11592 			}
11593 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11594 			    "sata_hba_ioctl: port deactivate: "
11595 			    "cannot deactivate SATA port %d", cport));
11596 			rv = EIO;
11597 		} else {
11598 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
11599 		}
11600 	} else {
11601 		if (rval != SATA_SUCCESS) {
11602 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11603 				SATA_PMPORT_STATE(sata_hba_inst, cport,
11604 				    pmport) = SATA_PSTATE_FAILED;
11605 			}
11606 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11607 			    "sata_hba_ioctl: port deactivate: "
11608 			    "cannot deactivate SATA port %d:%d",
11609 			    cport, pmport));
11610 			rv = EIO;
11611 		} else {
11612 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
11613 		}
11614 	}
11615 
11616 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11617 
11618 	return (rv);
11619 }
11620 
11621 /*
11622  * Process ioctl port activate request.
11623  *
11624  * NOTE: Port multiplier code is not completed nor tested.
11625  */
11626 static int
11627 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst,
11628     sata_device_t *sata_device)
11629 {
11630 	int cport, pmport, qual;
11631 	sata_cport_info_t *cportinfo;
11632 	sata_pmport_info_t *pmportinfo = NULL;
11633 	boolean_t dev_existed = TRUE;
11634 
11635 	/* Sanity check */
11636 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
11637 		return (ENOTSUP);
11638 
11639 	cport = sata_device->satadev_addr.cport;
11640 	pmport = sata_device->satadev_addr.pmport;
11641 	qual = sata_device->satadev_addr.qual;
11642 
11643 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11644 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11645 	if (qual == SATA_ADDR_PMPORT) {
11646 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11647 		if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN ||
11648 		    pmportinfo->pmport_dev_type == SATA_DTYPE_NONE)
11649 			dev_existed = FALSE;
11650 	} else { /* cport */
11651 		if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN ||
11652 		    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
11653 			dev_existed = FALSE;
11654 	}
11655 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11656 
11657 	/* Just let HBA driver to activate port, if necessary */
11658 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11659 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11660 		/*
11661 		 * Port activation failure - do not change port state unless
11662 		 * the state returned by HBA indicates a port failure.
11663 		 */
11664 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11665 		    cport)->cport_mutex);
11666 		sata_update_port_info(sata_hba_inst, sata_device);
11667 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11668 			if (qual == SATA_ADDR_PMPORT)
11669 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
11670 			else
11671 				cportinfo->cport_state = SATA_PSTATE_FAILED;
11672 
11673 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11674 			    cport)->cport_mutex);
11675 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11676 			    "sata_hba_ioctl: port activate: cannot activate "
11677 			    "SATA port %d:%d", cport, pmport));
11678 			return (EIO);
11679 		}
11680 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11681 	}
11682 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11683 	if (qual == SATA_ADDR_PMPORT)
11684 		pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN;
11685 	else
11686 		cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN;
11687 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11688 
11689 	/*
11690 	 * Re-probe port to find its current state and possibly attached device.
11691 	 * Port re-probing may change the cportinfo device type if device is
11692 	 * found attached.
11693 	 * If port probing failed, the device type would be set to
11694 	 * SATA_DTYPE_NONE.
11695 	 */
11696 	(void) sata_reprobe_port(sata_hba_inst, sata_device,
11697 	    SATA_DEV_IDENTIFY_RETRY);
11698 
11699 	/*
11700 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11701 	 * without the hint.
11702 	 */
11703 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11704 	    SE_NO_HINT);
11705 
11706 	if (dev_existed == FALSE) {
11707 		if (qual == SATA_ADDR_PMPORT &&
11708 		    pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
11709 			/*
11710 			 * That's the transition from the "inactive" port state
11711 			 * or the active port without a device attached to the
11712 			 * active port state with a device attached.
11713 			 */
11714 			sata_log(sata_hba_inst, CE_WARN,
11715 			    "SATA device detected at port %d:%d",
11716 			    cport, pmport);
11717 		} else if (qual == SATA_ADDR_CPORT &&
11718 		    cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
11719 			/*
11720 			 * That's the transition from the "inactive" port state
11721 			 * or the active port without a device attached to the
11722 			 * active port state with a device attached.
11723 			 */
11724 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
11725 				sata_log(sata_hba_inst, CE_WARN,
11726 				    "SATA device detected at port %d", cport);
11727 			} else {
11728 				sata_log(sata_hba_inst, CE_WARN,
11729 				    "SATA port multiplier detected at port %d",
11730 				    cport);
11731 				/*
11732 				 * Because the detected device is a port
11733 				 * multiplier, we need to reprobe every device
11734 				 * port on the port multiplier and show every
11735 				 * device found attached.
11736 				 * Add this code here.
11737 				 */
11738 			}
11739 		}
11740 	}
11741 	return (0);
11742 }
11743 
11744 
11745 
11746 /*
11747  * Process ioctl reset port request.
11748  *
11749  * NOTE: Port multiplier code is not completed nor tested.
11750  */
11751 static int
11752 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst,
11753     sata_device_t *sata_device)
11754 {
11755 	int cport, pmport, qual;
11756 	int rv = 0;
11757 
11758 	cport = sata_device->satadev_addr.cport;
11759 	pmport = sata_device->satadev_addr.pmport;
11760 	qual = sata_device->satadev_addr.qual;
11761 
11762 	/* Sanity check */
11763 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
11764 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11765 		    "sata_hba_ioctl: sata_hba_tran missing required "
11766 		    "function sata_tran_reset_dport"));
11767 		return (ENOTSUP);
11768 	}
11769 
11770 	/* Ask HBA to reset port */
11771 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
11772 	    sata_device) != SATA_SUCCESS) {
11773 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11774 		    "sata_hba_ioctl: reset port: failed %d:%d",
11775 		    cport, pmport));
11776 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11777 		    cport_mutex);
11778 		sata_update_port_info(sata_hba_inst, sata_device);
11779 		if (qual == SATA_ADDR_CPORT)
11780 			SATA_CPORT_STATE(sata_hba_inst, cport) =
11781 			    SATA_PSTATE_FAILED;
11782 		else
11783 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
11784 			    SATA_PSTATE_FAILED;
11785 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11786 		    cport_mutex);
11787 		rv = EIO;
11788 	}
11789 	/*
11790 	 * Beacuse the port was reset, it should be probed and
11791 	 * attached device reinitialized. At this point the
11792 	 * port state is unknown - it's state is HBA-specific.
11793 	 * Re-probe port to get its state.
11794 	 */
11795 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11796 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) {
11797 		rv = EIO;
11798 	}
11799 	return (rv);
11800 }
11801 
11802 /*
11803  * Process ioctl reset device request.
11804  *
11805  * NOTE: Port multiplier code is not completed nor tested.
11806  */
11807 static int
11808 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst,
11809     sata_device_t *sata_device)
11810 {
11811 	sata_drive_info_t *sdinfo;
11812 	int cport, pmport;
11813 	int rv = 0;
11814 
11815 	/* Sanity check */
11816 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
11817 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11818 		    "sata_hba_ioctl: sata_hba_tran missing required "
11819 		    "function sata_tran_reset_dport"));
11820 		return (ENOTSUP);
11821 	}
11822 
11823 	cport = sata_device->satadev_addr.cport;
11824 	pmport = sata_device->satadev_addr.pmport;
11825 
11826 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11827 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT) {
11828 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11829 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
11830 		    sata_device->satadev_addr.cport);
11831 	} else { /* port multiplier */
11832 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11833 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
11834 		    sata_device->satadev_addr.cport,
11835 		    sata_device->satadev_addr.pmport);
11836 	}
11837 	if (sdinfo == NULL) {
11838 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11839 		return (EINVAL);
11840 	}
11841 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11842 
11843 	/* Ask HBA to reset device */
11844 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
11845 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11846 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11847 		    "sata_hba_ioctl: reset device: failed at port %d:%d",
11848 		    cport, pmport));
11849 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11850 		    cport_mutex);
11851 		sata_update_port_info(sata_hba_inst, sata_device);
11852 		/*
11853 		 * Device info structure remains attached. Another device reset
11854 		 * or port disconnect/connect and re-probing is
11855 		 * needed to change it's state
11856 		 */
11857 		sdinfo->satadrv_state &= ~SATA_STATE_READY;
11858 		sdinfo->satadrv_state |= SATA_DSTATE_FAILED;
11859 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11860 		rv = EIO;
11861 	}
11862 	/*
11863 	 * If attached device was a port multiplier, some extra processing
11864 	 * may be needed, to bring it back (if port re-probing did not handle
11865 	 * it). Add such code here.
11866 	 */
11867 	return (rv);
11868 }
11869 
11870 
11871 /*
11872  * Process ioctl reset all request.
11873  *
11874  * NOTE: Port multiplier code is not completed nor tested.
11875  */
11876 static int
11877 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst)
11878 {
11879 	sata_device_t sata_device;
11880 	int rv = 0;
11881 	int tcport;
11882 	int tpmport = 0;
11883 
11884 	sata_device.satadev_rev = SATA_DEVICE_REV;
11885 
11886 	/*
11887 	 * There is no protection here for configured devices.
11888 	 */
11889 	/* Sanity check */
11890 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
11891 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11892 		    "sata_hba_ioctl: sata_hba_tran missing required "
11893 		    "function sata_tran_reset_dport"));
11894 		return (ENOTSUP);
11895 	}
11896 
11897 	/*
11898 	 * Need to lock all ports, not just one.
11899 	 * If any port is locked by event processing, fail the whole operation.
11900 	 * One port is already locked, but for simplicity lock it again.
11901 	 */
11902 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
11903 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11904 		    cport_mutex);
11905 		if (((SATA_CPORT_INFO(sata_hba_inst, tcport)->
11906 		    cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) {
11907 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11908 			    cport_mutex);
11909 			rv = EBUSY;
11910 			break;
11911 		} else {
11912 			SATA_CPORT_INFO(sata_hba_inst, tcport)->
11913 			    cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
11914 			/*
11915 			 * If there is a port multiplier attached, we may need
11916 			 * to lock its port as well. If so, add such code here.
11917 			 */
11918 		}
11919 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11920 		    cport_mutex);
11921 	}
11922 
11923 	if (rv == 0) {
11924 		/*
11925 		 * All cports were successfully locked.
11926 		 * Reset main SATA controller only for now - no PMult.
11927 		 * Set the device address to port 0, to have a valid device
11928 		 * address.
11929 		 */
11930 		sata_device.satadev_addr.qual = SATA_ADDR_CNTRL;
11931 		sata_device.satadev_addr.cport = 0;
11932 		sata_device.satadev_addr.pmport = 0;
11933 
11934 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
11935 		    (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) {
11936 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11937 			    "sata_hba_ioctl: reset controller failed"));
11938 			return (EIO);
11939 		}
11940 		/*
11941 		 * Because ports were reset, port states are unknown.
11942 		 * They should be re-probed to get their state and
11943 		 * attached devices should be reinitialized.
11944 		 * Add code here to re-probe port multiplier device ports.
11945 		 */
11946 		for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst);
11947 		    tcport++) {
11948 			sata_device.satadev_addr.cport = tcport;
11949 			sata_device.satadev_addr.pmport = tpmport;
11950 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
11951 
11952 			if (sata_reprobe_port(sata_hba_inst, &sata_device,
11953 			    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
11954 				rv = EIO;
11955 		}
11956 	}
11957 	/*
11958 	 * Unlock all ports
11959 	 */
11960 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
11961 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11962 		    cport_mutex);
11963 		SATA_CPORT_INFO(sata_hba_inst, tcport)->
11964 		    cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
11965 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11966 		    cport_mutex);
11967 	}
11968 
11969 	/*
11970 	 * This operation returns EFAULT if either reset
11971 	 * controller failed or a re-probing of any port failed.
11972 	 */
11973 	return (rv);
11974 }
11975 
11976 
11977 /*
11978  * Process ioctl port self test request.
11979  *
11980  * NOTE: Port multiplier code is not completed nor tested.
11981  */
11982 static int
11983 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst,
11984     sata_device_t *sata_device)
11985 {
11986 	int cport, pmport, qual;
11987 	int rv = 0;
11988 
11989 	/* Sanity check */
11990 	if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL)
11991 		return (ENOTSUP);
11992 
11993 	cport = sata_device->satadev_addr.cport;
11994 	pmport = sata_device->satadev_addr.pmport;
11995 	qual = sata_device->satadev_addr.qual;
11996 
11997 	/*
11998 	 * There is no protection here for a configured
11999 	 * device attached to this port.
12000 	 */
12001 
12002 	if ((*SATA_SELFTEST_FUNC(sata_hba_inst))
12003 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
12004 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12005 		    "sata_hba_ioctl: port selftest: "
12006 		    "failed port %d:%d", cport, pmport));
12007 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12008 		    cport_mutex);
12009 		sata_update_port_info(sata_hba_inst, sata_device);
12010 		if (qual == SATA_ADDR_CPORT)
12011 			SATA_CPORT_STATE(sata_hba_inst, cport) =
12012 			    SATA_PSTATE_FAILED;
12013 		else /* port ultiplier device port */
12014 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
12015 			    SATA_PSTATE_FAILED;
12016 
12017 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
12018 		    cport_mutex);
12019 		return (EIO);
12020 	}
12021 	/*
12022 	 * Beacuse the port was reset in the course of testing, it should be
12023 	 * re-probed and attached device state should be restored. At this
12024 	 * point the port state is unknown - it's state is HBA-specific.
12025 	 * Force port re-probing to get it into a known state.
12026 	 */
12027 	if (sata_reprobe_port(sata_hba_inst, sata_device,
12028 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
12029 		rv = EIO;
12030 	return (rv);
12031 }
12032 
12033 
12034 /*
12035  * sata_cfgadm_state:
12036  * Use the sata port state and state of the target node to figure out
12037  * the cfgadm_state.
12038  *
12039  * The port argument is a value with encoded cport,
12040  * pmport and address qualifier, in the same manner as a scsi target number.
12041  * SCSI_TO_SATA_CPORT macro extracts cport number,
12042  * SCSI_TO_SATA_PMPORT extracts pmport number and
12043  * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag.
12044  *
12045  * For now, support is for cports only - no port multiplier device ports.
12046  */
12047 
12048 static void
12049 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port,
12050     devctl_ap_state_t *ap_state)
12051 {
12052 	uint16_t	cport;
12053 	int		port_state;
12054 	sata_drive_info_t *sdinfo;
12055 
12056 	/* Cport only */
12057 	cport = SCSI_TO_SATA_CPORT(port);
12058 
12059 	port_state = SATA_CPORT_STATE(sata_hba_inst, cport);
12060 	if (port_state & SATA_PSTATE_SHUTDOWN ||
12061 	    port_state & SATA_PSTATE_FAILED) {
12062 		ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
12063 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12064 		if (port_state & SATA_PSTATE_FAILED)
12065 			ap_state->ap_condition = AP_COND_FAILED;
12066 		else
12067 			ap_state->ap_condition = AP_COND_UNKNOWN;
12068 
12069 		return;
12070 	}
12071 
12072 	/* Need to check pmult device port here as well, when supported */
12073 
12074 	/* Port is enabled and ready */
12075 
12076 	switch (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport)) {
12077 	case SATA_DTYPE_NONE:
12078 	{
12079 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12080 		ap_state->ap_condition = AP_COND_OK;
12081 		/* No device attached */
12082 		ap_state->ap_rstate = AP_RSTATE_EMPTY;
12083 		break;
12084 	}
12085 	case SATA_DTYPE_UNKNOWN:
12086 	case SATA_DTYPE_ATAPINONCD:
12087 	case SATA_DTYPE_PMULT:	/* Until PMult is supported */
12088 	case SATA_DTYPE_ATADISK:
12089 	case SATA_DTYPE_ATAPICD:
12090 	{
12091 		dev_info_t *tdip = NULL;
12092 		dev_info_t *dip = NULL;
12093 		int circ;
12094 
12095 		dip = SATA_DIP(sata_hba_inst);
12096 		tdip = sata_get_target_dip(dip, port);
12097 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
12098 		if (tdip != NULL) {
12099 			ndi_devi_enter(dip, &circ);
12100 			mutex_enter(&(DEVI(tdip)->devi_lock));
12101 			if (DEVI_IS_DEVICE_REMOVED(tdip)) {
12102 				/*
12103 				 * There could be the case where previously
12104 				 * configured and opened device was removed
12105 				 * and unknown device was plugged.
12106 				 * In such case we want to show a device, and
12107 				 * its configured or unconfigured state but
12108 				 * indicate unusable condition untill the
12109 				 * old target node is released and removed.
12110 				 */
12111 				ap_state->ap_condition = AP_COND_UNUSABLE;
12112 			} else {
12113 				mutex_enter(&SATA_CPORT_MUTEX(sata_hba_inst,
12114 				    cport));
12115 				sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12116 				    cport);
12117 				if (sdinfo != NULL) {
12118 					if ((sdinfo->satadrv_state &
12119 					    SATA_DSTATE_FAILED) != 0)
12120 						ap_state->ap_condition =
12121 						    AP_COND_FAILED;
12122 					else
12123 						ap_state->ap_condition =
12124 						    AP_COND_OK;
12125 				} else {
12126 					ap_state->ap_condition =
12127 					    AP_COND_UNKNOWN;
12128 				}
12129 				mutex_exit(&SATA_CPORT_MUTEX(sata_hba_inst,
12130 				    cport));
12131 			}
12132 			if ((DEVI_IS_DEVICE_OFFLINE(tdip)) ||
12133 			    (DEVI_IS_DEVICE_DOWN(tdip))) {
12134 				ap_state->ap_ostate =
12135 				    AP_OSTATE_UNCONFIGURED;
12136 			} else {
12137 				ap_state->ap_ostate =
12138 				    AP_OSTATE_CONFIGURED;
12139 			}
12140 			mutex_exit(&(DEVI(tdip)->devi_lock));
12141 			ndi_devi_exit(dip, circ);
12142 		} else {
12143 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12144 			ap_state->ap_condition = AP_COND_UNKNOWN;
12145 		}
12146 		break;
12147 	}
12148 	default:
12149 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
12150 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12151 		ap_state->ap_condition = AP_COND_UNKNOWN;
12152 		/*
12153 		 * This is actually internal error condition (non fatal),
12154 		 * because we have already checked all defined device types.
12155 		 */
12156 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12157 		    "sata_cfgadm_state: Internal error: "
12158 		    "unknown device type"));
12159 		break;
12160 	}
12161 }
12162 
12163 
12164 /*
12165  * Process ioctl get device path request.
12166  *
12167  * NOTE: Port multiplier code is not completed nor tested.
12168  */
12169 static int
12170 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst,
12171     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12172 {
12173 	char path[MAXPATHLEN];
12174 	uint32_t size;
12175 	dev_info_t *tdip;
12176 
12177 	(void) strcpy(path, "/devices");
12178 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
12179 	    &sata_device->satadev_addr)) == NULL) {
12180 		/*
12181 		 * No such device. If this is a request for a size, do not
12182 		 * return EINVAL for non-existing target, because cfgadm
12183 		 * will then indicate a meaningless ioctl failure.
12184 		 * If this is a request for a path, indicate invalid
12185 		 * argument.
12186 		 */
12187 		if (ioc->get_size == 0)
12188 			return (EINVAL);
12189 	} else {
12190 		(void) ddi_pathname(tdip, path + strlen(path));
12191 	}
12192 	size = strlen(path) + 1;
12193 
12194 	if (ioc->get_size != 0) {
12195 		if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz,
12196 		    mode) != 0)
12197 			return (EFAULT);
12198 	} else {
12199 		if (ioc->bufsiz != size)
12200 			return (EINVAL);
12201 
12202 		else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz,
12203 		    mode) != 0)
12204 			return (EFAULT);
12205 	}
12206 	return (0);
12207 }
12208 
12209 /*
12210  * Process ioctl get attachment point type request.
12211  *
12212  * NOTE: Port multiplier code is not completed nor tested.
12213  */
12214 static	int
12215 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst,
12216     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12217 {
12218 	uint32_t	type_len;
12219 	const char	*ap_type;
12220 	int		dev_type;
12221 
12222 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12223 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst,
12224 		    sata_device->satadev_addr.cport);
12225 	else /* pmport */
12226 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12227 		    sata_device->satadev_addr.cport,
12228 		    sata_device->satadev_addr.pmport);
12229 
12230 	switch (dev_type) {
12231 	case SATA_DTYPE_NONE:
12232 		ap_type = "port";
12233 		break;
12234 
12235 	case SATA_DTYPE_ATADISK:
12236 		ap_type = "disk";
12237 		break;
12238 
12239 	case SATA_DTYPE_ATAPICD:
12240 		ap_type = "cd/dvd";
12241 		break;
12242 
12243 	case SATA_DTYPE_PMULT:
12244 		ap_type = "pmult";
12245 		break;
12246 
12247 	case SATA_DTYPE_UNKNOWN:
12248 		ap_type = "unknown";
12249 		break;
12250 
12251 	default:
12252 		ap_type = "unsupported";
12253 		break;
12254 
12255 	} /* end of dev_type switch */
12256 
12257 	type_len = strlen(ap_type) + 1;
12258 
12259 	if (ioc->get_size) {
12260 		if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz,
12261 		    mode) != 0)
12262 			return (EFAULT);
12263 	} else {
12264 		if (ioc->bufsiz != type_len)
12265 			return (EINVAL);
12266 
12267 		if (ddi_copyout((void *)ap_type, ioc->buf,
12268 		    ioc->bufsiz, mode) != 0)
12269 			return (EFAULT);
12270 	}
12271 	return (0);
12272 
12273 }
12274 
12275 /*
12276  * Process ioctl get device model info request.
12277  * This operation should return to cfgadm the device model
12278  * information string
12279  *
12280  * NOTE: Port multiplier code is not completed nor tested.
12281  */
12282 static	int
12283 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst,
12284     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12285 {
12286 	sata_drive_info_t *sdinfo;
12287 	uint32_t info_len;
12288 	char ap_info[SATA_ID_MODEL_LEN + 1];
12289 
12290 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12291 	    sata_device->satadev_addr.cport)->cport_mutex);
12292 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12293 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12294 		    sata_device->satadev_addr.cport);
12295 	else /* port multiplier */
12296 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12297 		    sata_device->satadev_addr.cport,
12298 		    sata_device->satadev_addr.pmport);
12299 	if (sdinfo == NULL) {
12300 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12301 		    sata_device->satadev_addr.cport)->cport_mutex);
12302 		return (EINVAL);
12303 	}
12304 
12305 #ifdef	_LITTLE_ENDIAN
12306 	swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
12307 #else	/* _LITTLE_ENDIAN */
12308 	bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
12309 #endif	/* _LITTLE_ENDIAN */
12310 
12311 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12312 	    sata_device->satadev_addr.cport)->cport_mutex);
12313 
12314 	ap_info[SATA_ID_MODEL_LEN] = '\0';
12315 
12316 	info_len = strlen(ap_info) + 1;
12317 
12318 	if (ioc->get_size) {
12319 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12320 		    mode) != 0)
12321 			return (EFAULT);
12322 	} else {
12323 		if (ioc->bufsiz < info_len)
12324 			return (EINVAL);
12325 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12326 		    mode) != 0)
12327 			return (EFAULT);
12328 	}
12329 	return (0);
12330 }
12331 
12332 
12333 /*
12334  * Process ioctl get device firmware revision info request.
12335  * This operation should return to cfgadm the device firmware revision
12336  * information string
12337  *
12338  * NOTE: Port multiplier code is not completed nor tested.
12339  */
12340 static	int
12341 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst,
12342     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12343 {
12344 	sata_drive_info_t *sdinfo;
12345 	uint32_t info_len;
12346 	char ap_info[SATA_ID_FW_LEN + 1];
12347 
12348 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12349 	    sata_device->satadev_addr.cport)->cport_mutex);
12350 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12351 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12352 		    sata_device->satadev_addr.cport);
12353 	else /* port multiplier */
12354 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12355 		    sata_device->satadev_addr.cport,
12356 		    sata_device->satadev_addr.pmport);
12357 	if (sdinfo == NULL) {
12358 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12359 		    sata_device->satadev_addr.cport)->cport_mutex);
12360 		return (EINVAL);
12361 	}
12362 
12363 #ifdef	_LITTLE_ENDIAN
12364 	swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
12365 #else	/* _LITTLE_ENDIAN */
12366 	bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
12367 #endif	/* _LITTLE_ENDIAN */
12368 
12369 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12370 	    sata_device->satadev_addr.cport)->cport_mutex);
12371 
12372 	ap_info[SATA_ID_FW_LEN] = '\0';
12373 
12374 	info_len = strlen(ap_info) + 1;
12375 
12376 	if (ioc->get_size) {
12377 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12378 		    mode) != 0)
12379 			return (EFAULT);
12380 	} else {
12381 		if (ioc->bufsiz < info_len)
12382 			return (EINVAL);
12383 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12384 		    mode) != 0)
12385 			return (EFAULT);
12386 	}
12387 	return (0);
12388 }
12389 
12390 
12391 /*
12392  * Process ioctl get device serial number info request.
12393  * This operation should return to cfgadm the device serial number string.
12394  *
12395  * NOTE: Port multiplier code is not completed nor tested.
12396  */
12397 static	int
12398 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst,
12399     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12400 {
12401 	sata_drive_info_t *sdinfo;
12402 	uint32_t info_len;
12403 	char ap_info[SATA_ID_SERIAL_LEN + 1];
12404 
12405 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12406 	    sata_device->satadev_addr.cport)->cport_mutex);
12407 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12408 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst,
12409 		    sata_device->satadev_addr.cport);
12410 	else /* port multiplier */
12411 		sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst,
12412 		    sata_device->satadev_addr.cport,
12413 		    sata_device->satadev_addr.pmport);
12414 	if (sdinfo == NULL) {
12415 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12416 		    sata_device->satadev_addr.cport)->cport_mutex);
12417 		return (EINVAL);
12418 	}
12419 
12420 #ifdef	_LITTLE_ENDIAN
12421 	swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
12422 #else	/* _LITTLE_ENDIAN */
12423 	bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
12424 #endif	/* _LITTLE_ENDIAN */
12425 
12426 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12427 	    sata_device->satadev_addr.cport)->cport_mutex);
12428 
12429 	ap_info[SATA_ID_SERIAL_LEN] = '\0';
12430 
12431 	info_len = strlen(ap_info) + 1;
12432 
12433 	if (ioc->get_size) {
12434 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12435 		    mode) != 0)
12436 			return (EFAULT);
12437 	} else {
12438 		if (ioc->bufsiz < info_len)
12439 			return (EINVAL);
12440 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12441 		    mode) != 0)
12442 			return (EFAULT);
12443 	}
12444 	return (0);
12445 }
12446 
12447 
12448 /*
12449  * Preset scsi extended sense data (to NO SENSE)
12450  * First 18 bytes of the sense data are preset to current valid sense
12451  * with a key NO SENSE data.
12452  *
12453  * Returns void
12454  */
12455 static void
12456 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense)
12457 {
12458 	sense->es_valid = 1;		/* Valid sense */
12459 	sense->es_class = CLASS_EXTENDED_SENSE;	/* 0x70 - current err */
12460 	sense->es_key = KEY_NO_SENSE;
12461 	sense->es_info_1 = 0;
12462 	sense->es_info_2 = 0;
12463 	sense->es_info_3 = 0;
12464 	sense->es_info_4 = 0;
12465 	sense->es_add_len = 10;	/* Additional length - replace with a def */
12466 	sense->es_cmd_info[0] = 0;
12467 	sense->es_cmd_info[1] = 0;
12468 	sense->es_cmd_info[2] = 0;
12469 	sense->es_cmd_info[3] = 0;
12470 	sense->es_add_code = 0;
12471 	sense->es_qual_code = 0;
12472 }
12473 
12474 /*
12475  * Register a legacy cmdk-style devid for the target (disk) device.
12476  *
12477  * Note: This function is called only when the HBA devinfo node has the
12478  * property "use-cmdk-devid-format" set. This property indicates that
12479  * devid compatible with old cmdk (target) driver is to be generated
12480  * for any target device attached to this controller. This will take
12481  * precedence over the devid generated by sd (target) driver.
12482  * This function is derived from cmdk_devid_setup() function in cmdk.c.
12483  */
12484 static void
12485 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo)
12486 {
12487 	char	*hwid;
12488 	int	modlen;
12489 	int	serlen;
12490 	int	rval;
12491 	ddi_devid_t	devid;
12492 
12493 	/*
12494 	 * device ID is a concatanation of model number, "=", serial number.
12495 	 */
12496 	hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP);
12497 	bcopy(&sdinfo->satadrv_id.ai_model, hwid,
12498 	    sizeof (sdinfo->satadrv_id.ai_model));
12499 	swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model));
12500 	modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model));
12501 	if (modlen == 0)
12502 		goto err;
12503 	hwid[modlen++] = '=';
12504 	bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen],
12505 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12506 	swab(&hwid[modlen], &hwid[modlen],
12507 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12508 	serlen = sata_check_modser(&hwid[modlen],
12509 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12510 	if (serlen == 0)
12511 		goto err;
12512 	hwid[modlen + serlen] = 0; /* terminate the hwid string */
12513 
12514 	/* initialize/register devid */
12515 	if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL,
12516 	    (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS)
12517 		rval = ddi_devid_register(dip, devid);
12518 
12519 	if (rval != DDI_SUCCESS)
12520 		cmn_err(CE_WARN, "sata: failed to create devid for the disk"
12521 		    " on port %d", sdinfo->satadrv_addr.cport);
12522 err:
12523 	kmem_free(hwid, LEGACY_HWID_LEN);
12524 }
12525 
12526 /*
12527  * valid model/serial string must contain a non-zero non-space characters.
12528  * trim trailing spaces/NULLs.
12529  */
12530 static int
12531 sata_check_modser(char *buf, int buf_len)
12532 {
12533 	boolean_t ret;
12534 	char *s;
12535 	int i;
12536 	int tb;
12537 	char ch;
12538 
12539 	ret = B_FALSE;
12540 	s = buf;
12541 	for (i = 0; i < buf_len; i++) {
12542 		ch = *s++;
12543 		if (ch != ' ' && ch != '\0')
12544 			tb = i + 1;
12545 		if (ch != ' ' && ch != '\0' && ch != '0')
12546 			ret = B_TRUE;
12547 	}
12548 
12549 	if (ret == B_FALSE)
12550 		return (0); /* invalid string */
12551 
12552 	return (tb); /* return length */
12553 }
12554 
12555 /*
12556  * sata_set_drive_features function compares current device features setting
12557  * with the saved device features settings and, if there is a difference,
12558  * it restores device features setting to the previously saved state.
12559  * It also arbitrarily tries to select the highest supported DMA mode.
12560  * Device Identify or Identify Packet Device data has to be current.
12561  * At the moment read ahead and write cache are considered for all devices.
12562  * For atapi devices, Removable Media Status Notification is set in addition
12563  * to common features.
12564  *
12565  * This function cannot be called in the interrupt context (it may sleep).
12566  *
12567  * The input argument sdinfo should point to the drive info structure
12568  * to be updated after features are set. Note, that only
12569  * device (packet) identify data is updated, not the flags indicating the
12570  * supported features.
12571  *
12572  * Returns TRUE if successful or there was nothing to do. Device Identify data
12573  * in the drive info structure pointed to by the sdinfo argumens is updated
12574  * even when no features were set or changed.
12575  *
12576  * Returns FALSE if device features could not be set.
12577  *
12578  * Note: This function may fail the port, making it inaccessible.
12579  * In such case the explicit port disconnect/connect or physical device
12580  * detach/attach is required to re-evaluate port state again.
12581  */
12582 
12583 static int
12584 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst,
12585     sata_drive_info_t *sdinfo, int restore)
12586 {
12587 	int rval = SATA_SUCCESS;
12588 	sata_drive_info_t new_sdinfo;
12589 	char *finfo = "sata_set_drive_features: cannot";
12590 	char *finfox;
12591 	int cache_op;
12592 
12593 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
12594 	new_sdinfo.satadrv_addr = sdinfo->satadrv_addr;
12595 	new_sdinfo.satadrv_type = sdinfo->satadrv_type;
12596 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
12597 		/*
12598 		 * Cannot get device identification - retry later
12599 		 */
12600 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12601 		    "%s fetch device identify data\n", finfo);
12602 		return (SATA_FAILURE);
12603 	}
12604 	finfox = (restore != 0) ? " restore device features" :
12605 	    " initialize device features\n";
12606 
12607 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12608 		/* Arbitrarily set UDMA mode */
12609 		if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
12610 		    SATA_SUCCESS) {
12611 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12612 			    "%s set UDMA mode\n", finfo));
12613 			return (SATA_FAILURE);
12614 		}
12615 	} else { /* Assume SATA ATAPI CD/DVD */
12616 		/*  Set Removable Media Status Notification, if necessary */
12617 		if ((new_sdinfo.satadrv_id.ai_cmdset83 &
12618 		    SATA_RM_STATUS_NOTIFIC) != 0 && restore != 0) {
12619 			if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) &&
12620 			    (!(new_sdinfo.satadrv_id.ai_features86 &
12621 			    SATA_RM_STATUS_NOTIFIC))) ||
12622 			    ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) &&
12623 			    (new_sdinfo.satadrv_id.ai_features86 &
12624 			    SATA_RM_STATUS_NOTIFIC))) {
12625 				/* Current setting does not match saved one */
12626 				if (sata_set_rmsn(sata_hba_inst, sdinfo,
12627 				    sdinfo->satadrv_settings &
12628 				    SATA_DEV_RMSN) != SATA_SUCCESS)
12629 					rval = SATA_FAILURE;
12630 			}
12631 		}
12632 		/*
12633 		 * We have to set Multiword DMA or UDMA, if it is supported, as
12634 		 * we want to use DMA transfer mode whenever possible.
12635 		 * Some devices require explicit setting of the DMA mode.
12636 		 */
12637 		if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) {
12638 			/* Set highest supported DMA mode */
12639 			if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
12640 			    SATA_SUCCESS) {
12641 				SATA_LOG_D((sata_hba_inst, CE_WARN,
12642 				    "%s set UDMA mode\n", finfo));
12643 				rval = SATA_FAILURE;
12644 			}
12645 		}
12646 	}
12647 
12648 	if (!(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) &&
12649 	    !(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) {
12650 		/* None of the features is supported - do nothing */
12651 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12652 		    "settable features not supported\n", NULL);
12653 		goto update_sdinfo;
12654 	}
12655 
12656 	if (((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) &&
12657 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) &&
12658 	    ((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) &&
12659 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
12660 		/* Nothing to do */
12661 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12662 		    "no device features to set\n", NULL);
12663 		goto update_sdinfo;
12664 	}
12665 
12666 	if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) &&
12667 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD))) {
12668 		if (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) {
12669 			/* Enable read ahead / read cache */
12670 			cache_op = SATAC_SF_ENABLE_READ_AHEAD;
12671 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12672 			    "enabling read cache\n", NULL);
12673 		} else {
12674 			/* Disable read ahead  / read cache */
12675 			cache_op = SATAC_SF_DISABLE_READ_AHEAD;
12676 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12677 			    "disabling read cache\n", NULL);
12678 		}
12679 
12680 		/* Try to set read cache mode */
12681 		if (sata_set_cache_mode(sata_hba_inst, &new_sdinfo,
12682 		    cache_op) != SATA_SUCCESS) {
12683 			/* Pkt execution failed */
12684 			rval = SATA_FAILURE;
12685 		}
12686 	}
12687 
12688 	if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) &&
12689 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
12690 		if (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) {
12691 			/* Enable write cache */
12692 			cache_op = SATAC_SF_ENABLE_WRITE_CACHE;
12693 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12694 			    "enabling write cache\n", NULL);
12695 		} else {
12696 			/* Disable write cache */
12697 			cache_op = SATAC_SF_DISABLE_WRITE_CACHE;
12698 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12699 			    "disabling write cache\n", NULL);
12700 		}
12701 		/* Try to set write cache mode */
12702 		if (sata_set_cache_mode(sata_hba_inst, &new_sdinfo,
12703 		    cache_op) != SATA_SUCCESS) {
12704 			/* Pkt execution failed */
12705 			rval = SATA_FAILURE;
12706 		}
12707 	}
12708 
12709 	if (rval == SATA_FAILURE)
12710 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12711 		    "%s %s", finfo, finfox));
12712 update_sdinfo:
12713 	/*
12714 	 * We need to fetch Device Identify data again
12715 	 */
12716 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
12717 		/*
12718 		 * Cannot get device identification - retry later
12719 		 */
12720 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12721 		    "%s re-fetch device identify data\n", finfo));
12722 		rval = SATA_FAILURE;
12723 	}
12724 	/* Copy device sata info. */
12725 	sdinfo->satadrv_id = new_sdinfo.satadrv_id;
12726 
12727 	return (rval);
12728 }
12729 
12730 
12731 /*
12732  *
12733  * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if
12734  * unable to determine.
12735  *
12736  * Cannot be called in an interrupt context.
12737  *
12738  * Called by sata_build_lsense_page_2f()
12739  */
12740 
12741 static int
12742 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst,
12743     sata_drive_info_t *sdinfo)
12744 {
12745 	sata_pkt_t *spkt;
12746 	sata_cmd_t *scmd;
12747 	sata_pkt_txlate_t *spx;
12748 	int rval;
12749 
12750 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12751 	spx->txlt_sata_hba_inst = sata_hba_inst;
12752 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12753 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12754 	if (spkt == NULL) {
12755 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12756 		return (-1);
12757 	}
12758 	/* address is needed now */
12759 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12760 
12761 
12762 	/* Fill sata_pkt */
12763 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12764 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12765 	/* Synchronous mode, no callback */
12766 	spkt->satapkt_comp = NULL;
12767 	/* Timeout 30s */
12768 	spkt->satapkt_time = sata_default_pkt_time;
12769 
12770 	scmd = &spkt->satapkt_cmd;
12771 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
12772 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
12773 
12774 	/* Set up which registers need to be returned */
12775 	scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE;
12776 	scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE;
12777 
12778 	/* Build SMART_RETURN_STATUS cmd in the sata_pkt */
12779 	scmd->satacmd_addr_type = 0;		/* N/A */
12780 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
12781 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
12782 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
12783 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
12784 	scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS;
12785 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12786 	scmd->satacmd_cmd_reg = SATAC_SMART;
12787 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12788 	    sdinfo->satadrv_addr.cport)));
12789 
12790 
12791 	/* Send pkt to SATA HBA driver */
12792 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12793 	    SATA_TRAN_ACCEPTED ||
12794 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12795 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12796 		    sdinfo->satadrv_addr.cport)));
12797 		/*
12798 		 * Whoops, no SMART RETURN STATUS
12799 		 */
12800 		rval = -1;
12801 	} else {
12802 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12803 		    sdinfo->satadrv_addr.cport)));
12804 		if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
12805 			rval = -1;
12806 			goto fail;
12807 		}
12808 		if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
12809 			rval = -1;
12810 			goto fail;
12811 		}
12812 		if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) &&
12813 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2))
12814 			rval = 0;
12815 		else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) &&
12816 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4))
12817 			rval = 1;
12818 		else {
12819 			rval = -1;
12820 			goto fail;
12821 		}
12822 	}
12823 fail:
12824 	/* Free allocated resources */
12825 	sata_pkt_free(spx);
12826 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12827 
12828 	return (rval);
12829 }
12830 
12831 /*
12832  *
12833  * Returns 0 if succeeded, -1 otherwise
12834  *
12835  * Cannot be called in an interrupt context.
12836  *
12837  */
12838 static int
12839 sata_fetch_smart_data(
12840 	sata_hba_inst_t *sata_hba_inst,
12841 	sata_drive_info_t *sdinfo,
12842 	struct smart_data *smart_data)
12843 {
12844 	sata_pkt_t *spkt;
12845 	sata_cmd_t *scmd;
12846 	sata_pkt_txlate_t *spx;
12847 	int rval;
12848 
12849 #if ! defined(lint)
12850 	ASSERT(sizeof (struct smart_data) == 512);
12851 #endif
12852 
12853 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12854 	spx->txlt_sata_hba_inst = sata_hba_inst;
12855 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12856 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12857 	if (spkt == NULL) {
12858 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12859 		return (-1);
12860 	}
12861 	/* address is needed now */
12862 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12863 
12864 
12865 	/* Fill sata_pkt */
12866 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12867 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12868 	/* Synchronous mode, no callback */
12869 	spkt->satapkt_comp = NULL;
12870 	/* Timeout 30s */
12871 	spkt->satapkt_time = sata_default_pkt_time;
12872 
12873 	scmd = &spkt->satapkt_cmd;
12874 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
12875 
12876 	/*
12877 	 * Allocate buffer for SMART data
12878 	 */
12879 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
12880 	    sizeof (struct smart_data));
12881 	if (scmd->satacmd_bp == NULL) {
12882 		sata_pkt_free(spx);
12883 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12884 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12885 		    "sata_fetch_smart_data: "
12886 		    "cannot allocate buffer"));
12887 		return (-1);
12888 	}
12889 
12890 
12891 	/* Build SMART_READ_DATA cmd in the sata_pkt */
12892 	scmd->satacmd_addr_type = 0;		/* N/A */
12893 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
12894 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
12895 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
12896 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
12897 	scmd->satacmd_features_reg = SATA_SMART_READ_DATA;
12898 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12899 	scmd->satacmd_cmd_reg = SATAC_SMART;
12900 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12901 	    sdinfo->satadrv_addr.cport)));
12902 
12903 	/* Send pkt to SATA HBA driver */
12904 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12905 	    SATA_TRAN_ACCEPTED ||
12906 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12907 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12908 		    sdinfo->satadrv_addr.cport)));
12909 		/*
12910 		 * Whoops, no SMART DATA available
12911 		 */
12912 		rval = -1;
12913 		goto fail;
12914 	} else {
12915 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12916 		    sdinfo->satadrv_addr.cport)));
12917 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
12918 		    DDI_DMA_SYNC_FORKERNEL);
12919 		ASSERT(rval == DDI_SUCCESS);
12920 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data,
12921 		    sizeof (struct smart_data));
12922 	}
12923 
12924 fail:
12925 	/* Free allocated resources */
12926 	sata_free_local_buffer(spx);
12927 	sata_pkt_free(spx);
12928 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12929 
12930 	return (rval);
12931 }
12932 
12933 /*
12934  * Used by LOG SENSE page 0x10
12935  *
12936  * return 0 for success, -1 otherwise
12937  *
12938  */
12939 static int
12940 sata_ext_smart_selftest_read_log(
12941 	sata_hba_inst_t *sata_hba_inst,
12942 	sata_drive_info_t *sdinfo,
12943 	struct smart_ext_selftest_log *ext_selftest_log,
12944 	uint16_t block_num)
12945 {
12946 	sata_pkt_txlate_t *spx;
12947 	sata_pkt_t *spkt;
12948 	sata_cmd_t *scmd;
12949 	int rval;
12950 
12951 #if ! defined(lint)
12952 	ASSERT(sizeof (struct smart_ext_selftest_log) == 512);
12953 #endif
12954 
12955 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12956 	spx->txlt_sata_hba_inst = sata_hba_inst;
12957 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12958 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12959 	if (spkt == NULL) {
12960 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12961 		return (-1);
12962 	}
12963 	/* address is needed now */
12964 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12965 
12966 
12967 	/* Fill sata_pkt */
12968 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12969 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12970 	/* Synchronous mode, no callback */
12971 	spkt->satapkt_comp = NULL;
12972 	/* Timeout 30s */
12973 	spkt->satapkt_time = sata_default_pkt_time;
12974 
12975 	scmd = &spkt->satapkt_cmd;
12976 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
12977 
12978 	/*
12979 	 * Allocate buffer for SMART extended self-test log
12980 	 */
12981 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
12982 	    sizeof (struct smart_ext_selftest_log));
12983 	if (scmd->satacmd_bp == NULL) {
12984 		sata_pkt_free(spx);
12985 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12986 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12987 		    "sata_ext_smart_selftest_log: "
12988 		    "cannot allocate buffer"));
12989 		return (-1);
12990 	}
12991 
12992 	/* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */
12993 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
12994 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of selftest log */
12995 	scmd->satacmd_sec_count_msb = 0;	/* One sector of selftest log */
12996 	scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE;
12997 	scmd->satacmd_lba_low_msb = 0;
12998 	scmd->satacmd_lba_mid_lsb = block_num & 0xff;
12999 	scmd->satacmd_lba_mid_msb = block_num >> 8;
13000 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13001 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
13002 
13003 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13004 	    sdinfo->satadrv_addr.cport)));
13005 
13006 	/* Send pkt to SATA HBA driver */
13007 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13008 	    SATA_TRAN_ACCEPTED ||
13009 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13010 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13011 		    sdinfo->satadrv_addr.cport)));
13012 
13013 		/*
13014 		 * Whoops, no SMART selftest log info available
13015 		 */
13016 		rval = -1;
13017 		goto fail;
13018 	} else {
13019 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13020 		    sdinfo->satadrv_addr.cport)));
13021 
13022 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13023 		    DDI_DMA_SYNC_FORKERNEL);
13024 		ASSERT(rval == DDI_SUCCESS);
13025 		bcopy(scmd->satacmd_bp->b_un.b_addr,
13026 		    (uint8_t *)ext_selftest_log,
13027 		    sizeof (struct smart_ext_selftest_log));
13028 		rval = 0;
13029 	}
13030 
13031 fail:
13032 	/* Free allocated resources */
13033 	sata_free_local_buffer(spx);
13034 	sata_pkt_free(spx);
13035 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13036 
13037 	return (rval);
13038 }
13039 
13040 /*
13041  * Returns 0 for success, -1 otherwise
13042  *
13043  * SMART self-test log data is returned in buffer pointed to by selftest_log
13044  */
13045 static int
13046 sata_smart_selftest_log(
13047 	sata_hba_inst_t *sata_hba_inst,
13048 	sata_drive_info_t *sdinfo,
13049 	struct smart_selftest_log *selftest_log)
13050 {
13051 	sata_pkt_t *spkt;
13052 	sata_cmd_t *scmd;
13053 	sata_pkt_txlate_t *spx;
13054 	int rval;
13055 
13056 #if ! defined(lint)
13057 	ASSERT(sizeof (struct smart_selftest_log) == 512);
13058 #endif
13059 
13060 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13061 	spx->txlt_sata_hba_inst = sata_hba_inst;
13062 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13063 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13064 	if (spkt == NULL) {
13065 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13066 		return (-1);
13067 	}
13068 	/* address is needed now */
13069 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13070 
13071 
13072 	/* Fill sata_pkt */
13073 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13074 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13075 	/* Synchronous mode, no callback */
13076 	spkt->satapkt_comp = NULL;
13077 	/* Timeout 30s */
13078 	spkt->satapkt_time = sata_default_pkt_time;
13079 
13080 	scmd = &spkt->satapkt_cmd;
13081 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13082 
13083 	/*
13084 	 * Allocate buffer for SMART SELFTEST LOG
13085 	 */
13086 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13087 	    sizeof (struct smart_selftest_log));
13088 	if (scmd->satacmd_bp == NULL) {
13089 		sata_pkt_free(spx);
13090 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13091 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13092 		    "sata_smart_selftest_log: "
13093 		    "cannot allocate buffer"));
13094 		return (-1);
13095 	}
13096 
13097 	/* Build SMART_READ_LOG cmd in the sata_pkt */
13098 	scmd->satacmd_addr_type = 0;		/* N/A */
13099 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of SMART log */
13100 	scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE;
13101 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13102 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13103 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
13104 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13105 	scmd->satacmd_cmd_reg = SATAC_SMART;
13106 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13107 	    sdinfo->satadrv_addr.cport)));
13108 
13109 	/* Send pkt to SATA HBA driver */
13110 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13111 	    SATA_TRAN_ACCEPTED ||
13112 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13113 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13114 		    sdinfo->satadrv_addr.cport)));
13115 		/*
13116 		 * Whoops, no SMART DATA available
13117 		 */
13118 		rval = -1;
13119 		goto fail;
13120 	} else {
13121 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13122 		    sdinfo->satadrv_addr.cport)));
13123 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13124 		    DDI_DMA_SYNC_FORKERNEL);
13125 		ASSERT(rval == DDI_SUCCESS);
13126 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log,
13127 		    sizeof (struct smart_selftest_log));
13128 		rval = 0;
13129 	}
13130 
13131 fail:
13132 	/* Free allocated resources */
13133 	sata_free_local_buffer(spx);
13134 	sata_pkt_free(spx);
13135 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13136 
13137 	return (rval);
13138 }
13139 
13140 
13141 /*
13142  * Returns 0 for success, -1 otherwise
13143  *
13144  * SMART READ LOG data is returned in buffer pointed to by smart_log
13145  */
13146 static int
13147 sata_smart_read_log(
13148 	sata_hba_inst_t *sata_hba_inst,
13149 	sata_drive_info_t *sdinfo,
13150 	uint8_t *smart_log,		/* where the data should be returned */
13151 	uint8_t which_log,		/* which log should be returned */
13152 	uint8_t log_size)		/* # of 512 bytes in log */
13153 {
13154 	sata_pkt_t *spkt;
13155 	sata_cmd_t *scmd;
13156 	sata_pkt_txlate_t *spx;
13157 	int rval;
13158 
13159 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13160 	spx->txlt_sata_hba_inst = sata_hba_inst;
13161 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13162 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13163 	if (spkt == NULL) {
13164 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13165 		return (-1);
13166 	}
13167 	/* address is needed now */
13168 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13169 
13170 
13171 	/* Fill sata_pkt */
13172 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13173 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13174 	/* Synchronous mode, no callback */
13175 	spkt->satapkt_comp = NULL;
13176 	/* Timeout 30s */
13177 	spkt->satapkt_time = sata_default_pkt_time;
13178 
13179 	scmd = &spkt->satapkt_cmd;
13180 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13181 
13182 	/*
13183 	 * Allocate buffer for SMART READ LOG
13184 	 */
13185 	scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512);
13186 	if (scmd->satacmd_bp == NULL) {
13187 		sata_pkt_free(spx);
13188 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13189 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13190 		    "sata_smart_read_log: " "cannot allocate buffer"));
13191 		return (-1);
13192 	}
13193 
13194 	/* Build SMART_READ_LOG cmd in the sata_pkt */
13195 	scmd->satacmd_addr_type = 0;		/* N/A */
13196 	scmd->satacmd_sec_count_lsb = log_size;	/* what the caller asked for */
13197 	scmd->satacmd_lba_low_lsb = which_log;	/* which log page */
13198 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13199 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13200 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
13201 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13202 	scmd->satacmd_cmd_reg = SATAC_SMART;
13203 
13204 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13205 	    sdinfo->satadrv_addr.cport)));
13206 
13207 	/* Send pkt to SATA HBA driver */
13208 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13209 	    SATA_TRAN_ACCEPTED ||
13210 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13211 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13212 		    sdinfo->satadrv_addr.cport)));
13213 
13214 		/*
13215 		 * Whoops, no SMART DATA available
13216 		 */
13217 		rval = -1;
13218 		goto fail;
13219 	} else {
13220 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13221 		    sdinfo->satadrv_addr.cport)));
13222 
13223 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13224 		    DDI_DMA_SYNC_FORKERNEL);
13225 		ASSERT(rval == DDI_SUCCESS);
13226 		bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512);
13227 		rval = 0;
13228 	}
13229 
13230 fail:
13231 	/* Free allocated resources */
13232 	sata_free_local_buffer(spx);
13233 	sata_pkt_free(spx);
13234 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13235 
13236 	return (rval);
13237 }
13238 
13239 /*
13240  * Used by LOG SENSE page 0x10
13241  *
13242  * return 0 for success, -1 otherwise
13243  *
13244  */
13245 static int
13246 sata_read_log_ext_directory(
13247 	sata_hba_inst_t *sata_hba_inst,
13248 	sata_drive_info_t *sdinfo,
13249 	struct read_log_ext_directory *logdir)
13250 {
13251 	sata_pkt_txlate_t *spx;
13252 	sata_pkt_t *spkt;
13253 	sata_cmd_t *scmd;
13254 	int rval;
13255 
13256 #if ! defined(lint)
13257 	ASSERT(sizeof (struct read_log_ext_directory) == 512);
13258 #endif
13259 
13260 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13261 	spx->txlt_sata_hba_inst = sata_hba_inst;
13262 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13263 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13264 	if (spkt == NULL) {
13265 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13266 		return (-1);
13267 	}
13268 
13269 	/* Fill sata_pkt */
13270 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13271 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13272 	/* Synchronous mode, no callback */
13273 	spkt->satapkt_comp = NULL;
13274 	/* Timeout 30s */
13275 	spkt->satapkt_time = sata_default_pkt_time;
13276 
13277 	scmd = &spkt->satapkt_cmd;
13278 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13279 
13280 	/*
13281 	 * Allocate buffer for SMART READ LOG EXTENDED command
13282 	 */
13283 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13284 	    sizeof (struct read_log_ext_directory));
13285 	if (scmd->satacmd_bp == NULL) {
13286 		sata_pkt_free(spx);
13287 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13288 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13289 		    "sata_read_log_ext_directory: "
13290 		    "cannot allocate buffer"));
13291 		return (-1);
13292 	}
13293 
13294 	/* Build READ LOG EXT w/ log directory cmd in the  sata_pkt */
13295 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
13296 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of directory */
13297 	scmd->satacmd_sec_count_msb = 0;	/* One sector of directory */
13298 	scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY;
13299 	scmd->satacmd_lba_low_msb = 0;
13300 	scmd->satacmd_lba_mid_lsb = 0;
13301 	scmd->satacmd_lba_mid_msb = 0;
13302 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13303 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
13304 
13305 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13306 	    sdinfo->satadrv_addr.cport)));
13307 
13308 	/* Send pkt to SATA HBA driver */
13309 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13310 	    SATA_TRAN_ACCEPTED ||
13311 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13312 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13313 		    sdinfo->satadrv_addr.cport)));
13314 		/*
13315 		 * Whoops, no SMART selftest log info available
13316 		 */
13317 		rval = -1;
13318 		goto fail;
13319 	} else {
13320 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13321 		    sdinfo->satadrv_addr.cport)));
13322 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13323 		    DDI_DMA_SYNC_FORKERNEL);
13324 		ASSERT(rval == DDI_SUCCESS);
13325 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir,
13326 		    sizeof (struct read_log_ext_directory));
13327 		rval = 0;
13328 	}
13329 
13330 fail:
13331 	/* Free allocated resources */
13332 	sata_free_local_buffer(spx);
13333 	sata_pkt_free(spx);
13334 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13335 
13336 	return (rval);
13337 }
13338 
13339 /*
13340  * Set up error retrieval sata command for NCQ command error data
13341  * recovery.
13342  *
13343  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
13344  * returns SATA_FAILURE otherwise.
13345  */
13346 static int
13347 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
13348 {
13349 #ifndef __lock_lint
13350 	_NOTE(ARGUNUSED(sdinfo))
13351 #endif
13352 
13353 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
13354 	sata_cmd_t *scmd;
13355 	struct buf *bp;
13356 
13357 	/* Operation modes are up to the caller */
13358 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13359 
13360 	/* Synchronous mode, no callback - may be changed by the caller */
13361 	spkt->satapkt_comp = NULL;
13362 	spkt->satapkt_time = sata_default_pkt_time;
13363 
13364 	scmd = &spkt->satapkt_cmd;
13365 	bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t));
13366 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
13367 
13368 	/*
13369 	 * Allocate dma_able buffer error data.
13370 	 * Buffer allocation will take care of buffer alignment and other DMA
13371 	 * attributes.
13372 	 */
13373 	bp = sata_alloc_local_buffer(spx,
13374 	    sizeof (struct sata_ncq_error_recovery_page));
13375 	if (bp == NULL)
13376 		return (SATA_FAILURE);
13377 
13378 	bp_mapin(bp); /* make data buffer accessible */
13379 	scmd->satacmd_bp = bp;
13380 
13381 	/*
13382 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
13383 	 * before accessing it. Handle is in usual place in translate struct.
13384 	 */
13385 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
13386 
13387 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
13388 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
13389 
13390 	return (SATA_SUCCESS);
13391 }
13392 
13393 /*
13394  * sata_xlate_errors() is used to translate (S)ATA error
13395  * information to SCSI information returned in the SCSI
13396  * packet.
13397  */
13398 static void
13399 sata_xlate_errors(sata_pkt_txlate_t *spx)
13400 {
13401 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
13402 	struct scsi_extended_sense *sense;
13403 
13404 	scsipkt->pkt_reason = CMD_INCOMPLETE;
13405 	*scsipkt->pkt_scbp = STATUS_CHECK;
13406 	sense = sata_arq_sense(spx);
13407 
13408 	switch (spx->txlt_sata_pkt->satapkt_reason) {
13409 	case SATA_PKT_PORT_ERROR:
13410 		/*
13411 		 * We have no device data. Assume no data transfered.
13412 		 */
13413 		sense->es_key = KEY_HARDWARE_ERROR;
13414 		break;
13415 
13416 	case SATA_PKT_DEV_ERROR:
13417 		if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
13418 		    SATA_STATUS_ERR) {
13419 			/*
13420 			 * determine dev error reason from error
13421 			 * reg content
13422 			 */
13423 			sata_decode_device_error(spx, sense);
13424 			break;
13425 		}
13426 		/* No extended sense key - no info available */
13427 		break;
13428 
13429 	case SATA_PKT_TIMEOUT:
13430 		scsipkt->pkt_reason = CMD_TIMEOUT;
13431 		scsipkt->pkt_statistics |= STAT_TIMEOUT | STAT_DEV_RESET;
13432 		/* No extended sense key */
13433 		break;
13434 
13435 	case SATA_PKT_ABORTED:
13436 		scsipkt->pkt_reason = CMD_ABORTED;
13437 		scsipkt->pkt_statistics |= STAT_ABORTED;
13438 		/* No extended sense key */
13439 		break;
13440 
13441 	case SATA_PKT_RESET:
13442 		/*
13443 		 * pkt aborted either by an explicit reset request from
13444 		 * a host, or due to error recovery
13445 		 */
13446 		scsipkt->pkt_reason = CMD_RESET;
13447 		scsipkt->pkt_statistics |= STAT_DEV_RESET;
13448 		break;
13449 
13450 	default:
13451 		scsipkt->pkt_reason = CMD_TRAN_ERR;
13452 		break;
13453 	}
13454 }
13455 
13456 
13457 
13458 
13459 /*
13460  * Log sata message
13461  * dev pathname msg line preceeds the logged message.
13462  */
13463 
13464 static	void
13465 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...)
13466 {
13467 	char pathname[128];
13468 	dev_info_t *dip;
13469 	va_list ap;
13470 
13471 	mutex_enter(&sata_log_mutex);
13472 
13473 	va_start(ap, fmt);
13474 	(void) vsprintf(sata_log_buf, fmt, ap);
13475 	va_end(ap);
13476 
13477 	if (sata_hba_inst != NULL) {
13478 		dip = SATA_DIP(sata_hba_inst);
13479 		(void) ddi_pathname(dip, pathname);
13480 	} else {
13481 		pathname[0] = 0;
13482 	}
13483 	if (level == CE_CONT) {
13484 		if (sata_debug_flags == 0)
13485 			cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf);
13486 		else
13487 			cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf);
13488 	} else {
13489 		if (level != CE_NOTE) {
13490 			cmn_err(level, "%s:\n %s", pathname, sata_log_buf);
13491 		} else if (sata_msg) {
13492 			cmn_err(level, "%s:\n %s", pathname,
13493 			    sata_log_buf);
13494 		}
13495 	}
13496 
13497 	mutex_exit(&sata_log_mutex);
13498 }
13499 
13500 
13501 /* ******** Asynchronous HBA events handling & hotplugging support ******** */
13502 
13503 /*
13504  * Start or terminate the thread, depending on flag arg and current state
13505  */
13506 static void
13507 sata_event_thread_control(int startstop)
13508 {
13509 	static 	int sata_event_thread_terminating = 0;
13510 	static 	int sata_event_thread_starting = 0;
13511 	int i;
13512 
13513 	mutex_enter(&sata_event_mutex);
13514 
13515 	if (startstop == 0 && (sata_event_thread_starting == 1 ||
13516 	    sata_event_thread_terminating == 1)) {
13517 		mutex_exit(&sata_event_mutex);
13518 		return;
13519 	}
13520 	if (startstop == 1 && sata_event_thread_starting == 1) {
13521 		mutex_exit(&sata_event_mutex);
13522 		return;
13523 	}
13524 	if (startstop == 1 && sata_event_thread_terminating == 1) {
13525 		sata_event_thread_starting = 1;
13526 		/* wait til terminate operation completes */
13527 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
13528 		while (sata_event_thread_terminating == 1) {
13529 			if (i-- <= 0) {
13530 				sata_event_thread_starting = 0;
13531 				mutex_exit(&sata_event_mutex);
13532 #ifdef SATA_DEBUG
13533 				cmn_err(CE_WARN, "sata_event_thread_control: "
13534 				    "timeout waiting for thread to terminate");
13535 #endif
13536 				return;
13537 			}
13538 			mutex_exit(&sata_event_mutex);
13539 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
13540 			mutex_enter(&sata_event_mutex);
13541 		}
13542 	}
13543 	if (startstop == 1) {
13544 		if (sata_event_thread == NULL) {
13545 			sata_event_thread = thread_create(NULL, 0,
13546 			    (void (*)())sata_event_daemon,
13547 			    &sata_hba_list, 0, &p0, TS_RUN, minclsyspri);
13548 		}
13549 		sata_event_thread_starting = 0;
13550 		mutex_exit(&sata_event_mutex);
13551 		return;
13552 	}
13553 
13554 	/*
13555 	 * If we got here, thread may need to be terminated
13556 	 */
13557 	if (sata_event_thread != NULL) {
13558 		int i;
13559 		/* Signal event thread to go away */
13560 		sata_event_thread_terminating = 1;
13561 		sata_event_thread_terminate = 1;
13562 		cv_signal(&sata_event_cv);
13563 		/*
13564 		 * Wait til daemon terminates.
13565 		 */
13566 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
13567 		while (sata_event_thread_terminate == 1) {
13568 			mutex_exit(&sata_event_mutex);
13569 			if (i-- <= 0) {
13570 				/* Daemon did not go away !!! */
13571 #ifdef SATA_DEBUG
13572 				cmn_err(CE_WARN, "sata_event_thread_control: "
13573 				    "cannot terminate event daemon thread");
13574 #endif
13575 				mutex_enter(&sata_event_mutex);
13576 				break;
13577 			}
13578 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
13579 			mutex_enter(&sata_event_mutex);
13580 		}
13581 		sata_event_thread_terminating = 0;
13582 	}
13583 	ASSERT(sata_event_thread_terminating == 0);
13584 	ASSERT(sata_event_thread_starting == 0);
13585 	mutex_exit(&sata_event_mutex);
13586 }
13587 
13588 
13589 /*
13590  * SATA HBA event notification function.
13591  * Events reported by SATA HBA drivers per HBA instance relate to a change in
13592  * a port and/or device state or a controller itself.
13593  * Events for different addresses/addr types cannot be combined.
13594  * A warning message is generated for each event type.
13595  * Events are not processed by this function, so only the
13596  * event flag(s)is set for an affected entity and the event thread is
13597  * waken up. Event daemon thread processes all events.
13598  *
13599  * NOTE: Since more than one event may be reported at the same time, one
13600  * cannot determine a sequence of events when opposite event are reported, eg.
13601  * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing
13602  * is taking precedence over reported events, i.e. may cause ignoring some
13603  * events.
13604  */
13605 #define	SATA_EVENT_MAX_MSG_LENGTH	79
13606 
13607 void
13608 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event)
13609 {
13610 	sata_hba_inst_t *sata_hba_inst = NULL;
13611 	sata_address_t *saddr;
13612 	sata_drive_info_t *sdinfo;
13613 	sata_port_stats_t *pstats;
13614 	int cport, pmport;
13615 	char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1];
13616 	char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1];
13617 	char *lcp;
13618 	static char *err_msg_evnt_1 =
13619 	    "sata_hba_event_notify: invalid port event 0x%x ";
13620 	static char *err_msg_evnt_2 =
13621 	    "sata_hba_event_notify: invalid device event 0x%x ";
13622 	int linkevent;
13623 
13624 	/*
13625 	 * There is a possibility that an event will be generated on HBA
13626 	 * that has not completed attachment or is detaching.
13627 	 * HBA driver should prevent this, but just in case it does not,
13628 	 * we need to ignore events for such HBA.
13629 	 */
13630 	mutex_enter(&sata_mutex);
13631 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13632 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13633 		if (SATA_DIP(sata_hba_inst) == dip)
13634 			if (sata_hba_inst->satahba_attached == 1)
13635 				break;
13636 	}
13637 	mutex_exit(&sata_mutex);
13638 	if (sata_hba_inst == NULL)
13639 		/* HBA not attached */
13640 		return;
13641 
13642 	ASSERT(sata_device != NULL);
13643 
13644 	/*
13645 	 * Validate address before - do not proceed with invalid address.
13646 	 */
13647 	saddr = &sata_device->satadev_addr;
13648 	if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst))
13649 		return;
13650 	if (saddr->qual == SATA_ADDR_PMPORT ||
13651 	    saddr->qual == SATA_ADDR_DPMPORT)
13652 		/* Port Multiplier not supported yet */
13653 		return;
13654 
13655 	cport = saddr->cport;
13656 	pmport = saddr->pmport;
13657 
13658 	buf1[0] = buf2[0] = '\0';
13659 
13660 	/*
13661 	 * Events refer to devices, ports and controllers - each has
13662 	 * unique address. Events for different addresses cannot be combined.
13663 	 */
13664 	if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) {
13665 
13666 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13667 
13668 		/* qualify this event(s) */
13669 		if ((event & SATA_EVNT_PORT_EVENTS) == 0) {
13670 			/* Invalid event for the device port */
13671 			(void) sprintf(buf2, err_msg_evnt_1,
13672 			    event & SATA_EVNT_PORT_EVENTS);
13673 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13674 			goto event_info;
13675 		}
13676 		if (saddr->qual == SATA_ADDR_CPORT) {
13677 			/* Controller's device port event */
13678 
13679 			(SATA_CPORT_INFO(sata_hba_inst, cport))->
13680 			    cport_event_flags |=
13681 			    event & SATA_EVNT_PORT_EVENTS;
13682 			pstats =
13683 			    &(SATA_CPORT_INFO(sata_hba_inst, cport))->
13684 			    cport_stats;
13685 		} else {
13686 			/* Port multiplier's device port event */
13687 			(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
13688 			    pmport_event_flags |=
13689 			    event & SATA_EVNT_PORT_EVENTS;
13690 			pstats =
13691 			    &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
13692 			    pmport_stats;
13693 		}
13694 
13695 		/*
13696 		 * Add to statistics and log the message. We have to do it
13697 		 * here rather than in the event daemon, because there may be
13698 		 * multiple events occuring before they are processed.
13699 		 */
13700 		linkevent = event &
13701 		    (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED);
13702 		if (linkevent) {
13703 			if (linkevent == (SATA_EVNT_LINK_LOST |
13704 			    SATA_EVNT_LINK_ESTABLISHED)) {
13705 				/* This is likely event combination */
13706 				(void) strlcat(buf1, "link lost/established, ",
13707 				    SATA_EVENT_MAX_MSG_LENGTH);
13708 
13709 				if (pstats->link_lost < 0xffffffffffffffffULL)
13710 					pstats->link_lost++;
13711 				if (pstats->link_established <
13712 				    0xffffffffffffffffULL)
13713 					pstats->link_established++;
13714 				linkevent = 0;
13715 			} else if (linkevent & SATA_EVNT_LINK_LOST) {
13716 				(void) strlcat(buf1, "link lost, ",
13717 				    SATA_EVENT_MAX_MSG_LENGTH);
13718 
13719 				if (pstats->link_lost < 0xffffffffffffffffULL)
13720 					pstats->link_lost++;
13721 			} else {
13722 				(void) strlcat(buf1, "link established, ",
13723 				    SATA_EVENT_MAX_MSG_LENGTH);
13724 				if (pstats->link_established <
13725 				    0xffffffffffffffffULL)
13726 					pstats->link_established++;
13727 			}
13728 		}
13729 		if (event & SATA_EVNT_DEVICE_ATTACHED) {
13730 			(void) strlcat(buf1, "device attached, ",
13731 			    SATA_EVENT_MAX_MSG_LENGTH);
13732 			if (pstats->device_attached < 0xffffffffffffffffULL)
13733 				pstats->device_attached++;
13734 		}
13735 		if (event & SATA_EVNT_DEVICE_DETACHED) {
13736 			(void) strlcat(buf1, "device detached, ",
13737 			    SATA_EVENT_MAX_MSG_LENGTH);
13738 			if (pstats->device_detached < 0xffffffffffffffffULL)
13739 				pstats->device_detached++;
13740 		}
13741 		if (event & SATA_EVNT_PWR_LEVEL_CHANGED) {
13742 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
13743 			    "port %d power level changed", cport);
13744 			if (pstats->port_pwr_changed < 0xffffffffffffffffULL)
13745 				pstats->port_pwr_changed++;
13746 		}
13747 
13748 		if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) {
13749 			/* There should be no other events for this address */
13750 			(void) sprintf(buf2, err_msg_evnt_1,
13751 			    event & ~SATA_EVNT_PORT_EVENTS);
13752 		}
13753 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13754 
13755 	} else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) {
13756 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13757 
13758 		/* qualify this event */
13759 		if ((event & SATA_EVNT_DEVICE_RESET) == 0) {
13760 			/* Invalid event for a device */
13761 			(void) sprintf(buf2, err_msg_evnt_2,
13762 			    event & SATA_EVNT_DEVICE_RESET);
13763 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13764 			goto event_info;
13765 		}
13766 		/* drive event */
13767 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
13768 		if (sdinfo != NULL) {
13769 			if (event & SATA_EVNT_DEVICE_RESET) {
13770 				(void) strlcat(buf1, "device reset, ",
13771 				    SATA_EVENT_MAX_MSG_LENGTH);
13772 				if (sdinfo->satadrv_stats.drive_reset <
13773 				    0xffffffffffffffffULL)
13774 					sdinfo->satadrv_stats.drive_reset++;
13775 				sdinfo->satadrv_event_flags |=
13776 				    SATA_EVNT_DEVICE_RESET;
13777 			}
13778 		}
13779 		if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) {
13780 			/* Invalid event for a device */
13781 			(void) sprintf(buf2, err_msg_evnt_2,
13782 			    event & ~SATA_EVNT_DRIVE_EVENTS);
13783 		}
13784 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13785 	} else {
13786 		if (saddr->qual != SATA_ADDR_NULL) {
13787 			/* Wrong address qualifier */
13788 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13789 			    "sata_hba_event_notify: invalid address 0x%x",
13790 			    *(uint32_t *)saddr));
13791 			return;
13792 		}
13793 		if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 ||
13794 		    (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) {
13795 			/* Invalid event for the controller */
13796 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13797 			    "sata_hba_event_notify: invalid event 0x%x for "
13798 			    "controller",
13799 			    event & SATA_EVNT_CONTROLLER_EVENTS));
13800 			return;
13801 		}
13802 		buf1[0] = '\0';
13803 		/* This may be a frequent and not interesting event */
13804 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
13805 		    "controller power level changed\n", NULL);
13806 
13807 		mutex_enter(&sata_hba_inst->satahba_mutex);
13808 		if (sata_hba_inst->satahba_stats.ctrl_pwr_change <
13809 		    0xffffffffffffffffULL)
13810 			sata_hba_inst->satahba_stats.ctrl_pwr_change++;
13811 
13812 		sata_hba_inst->satahba_event_flags |=
13813 		    SATA_EVNT_PWR_LEVEL_CHANGED;
13814 		mutex_exit(&sata_hba_inst->satahba_mutex);
13815 	}
13816 	/*
13817 	 * If we got here, there is something to do with this HBA
13818 	 * instance.
13819 	 */
13820 	mutex_enter(&sata_hba_inst->satahba_mutex);
13821 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
13822 	mutex_exit(&sata_hba_inst->satahba_mutex);
13823 	mutex_enter(&sata_mutex);
13824 	sata_event_pending |= SATA_EVNT_MAIN;	/* global event indicator */
13825 	mutex_exit(&sata_mutex);
13826 
13827 	/* Tickle event thread */
13828 	mutex_enter(&sata_event_mutex);
13829 	if (sata_event_thread_active == 0)
13830 		cv_signal(&sata_event_cv);
13831 	mutex_exit(&sata_event_mutex);
13832 
13833 event_info:
13834 	if (buf1[0] != '\0') {
13835 		lcp = strrchr(buf1, ',');
13836 		if (lcp != NULL)
13837 			*lcp = '\0';
13838 	}
13839 	if (saddr->qual == SATA_ADDR_CPORT ||
13840 	    saddr->qual == SATA_ADDR_DCPORT) {
13841 		if (buf1[0] != '\0') {
13842 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
13843 			    cport, buf1);
13844 		}
13845 		if (buf2[0] != '\0') {
13846 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
13847 			    cport, buf2);
13848 		}
13849 	} else if (saddr->qual == SATA_ADDR_PMPORT ||
13850 	    saddr->qual == SATA_ADDR_DPMPORT) {
13851 		if (buf1[0] != '\0') {
13852 			sata_log(sata_hba_inst, CE_NOTE,
13853 			    "port %d pmport %d: %s\n", cport, pmport, buf1);
13854 		}
13855 		if (buf2[0] != '\0') {
13856 			sata_log(sata_hba_inst, CE_NOTE,
13857 			    "port %d pmport %d: %s\n", cport, pmport, buf2);
13858 		}
13859 	}
13860 }
13861 
13862 
13863 /*
13864  * Event processing thread.
13865  * Arg is a pointer to the sata_hba_list pointer.
13866  * It is not really needed, because sata_hba_list is global and static
13867  */
13868 static void
13869 sata_event_daemon(void *arg)
13870 {
13871 #ifndef __lock_lint
13872 	_NOTE(ARGUNUSED(arg))
13873 #endif
13874 	sata_hba_inst_t *sata_hba_inst;
13875 	clock_t lbolt;
13876 
13877 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13878 	    "SATA event daemon started\n", NULL);
13879 loop:
13880 	/*
13881 	 * Process events here. Walk through all registered HBAs
13882 	 */
13883 	mutex_enter(&sata_mutex);
13884 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13885 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13886 		ASSERT(sata_hba_inst != NULL);
13887 		mutex_enter(&sata_hba_inst->satahba_mutex);
13888 		if (sata_hba_inst->satahba_attached != 1 ||
13889 		    (sata_hba_inst->satahba_event_flags &
13890 		    SATA_EVNT_SKIP) != 0) {
13891 			mutex_exit(&sata_hba_inst->satahba_mutex);
13892 			continue;
13893 		}
13894 		if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) {
13895 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP;
13896 			mutex_exit(&sata_hba_inst->satahba_mutex);
13897 			mutex_exit(&sata_mutex);
13898 			/* Got the controller with pending event */
13899 			sata_process_controller_events(sata_hba_inst);
13900 			/*
13901 			 * Since global mutex was released, there is a
13902 			 * possibility that HBA list has changed, so start
13903 			 * over from the top. Just processed controller
13904 			 * will be passed-over because of the SKIP flag.
13905 			 */
13906 			goto loop;
13907 		}
13908 		mutex_exit(&sata_hba_inst->satahba_mutex);
13909 	}
13910 	/* Clear SKIP flag in all controllers */
13911 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13912 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13913 		mutex_enter(&sata_hba_inst->satahba_mutex);
13914 		sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP;
13915 		mutex_exit(&sata_hba_inst->satahba_mutex);
13916 	}
13917 	mutex_exit(&sata_mutex);
13918 
13919 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13920 	    "SATA EVENT DAEMON suspending itself", NULL);
13921 
13922 #ifdef SATA_DEBUG
13923 	if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) {
13924 		sata_log(sata_hba_inst, CE_WARN,
13925 		    "SATA EVENTS PROCESSING DISABLED\n");
13926 		thread_exit(); /* Daemon will not run again */
13927 	}
13928 #endif
13929 	mutex_enter(&sata_event_mutex);
13930 	sata_event_thread_active = 0;
13931 	mutex_exit(&sata_event_mutex);
13932 	/*
13933 	 * Go to sleep/suspend itself and wake up either because new event or
13934 	 * wait timeout. Exit if there is a termination request (driver
13935 	 * unload).
13936 	 */
13937 	do {
13938 		lbolt = ddi_get_lbolt();
13939 		lbolt += drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME);
13940 		mutex_enter(&sata_event_mutex);
13941 		(void) cv_timedwait(&sata_event_cv, &sata_event_mutex, lbolt);
13942 
13943 		if (sata_event_thread_active != 0) {
13944 			mutex_exit(&sata_event_mutex);
13945 			continue;
13946 		}
13947 
13948 		/* Check if it is time to go away */
13949 		if (sata_event_thread_terminate == 1) {
13950 			/*
13951 			 * It is up to the thread setting above flag to make
13952 			 * sure that this thread is not killed prematurely.
13953 			 */
13954 			sata_event_thread_terminate = 0;
13955 			sata_event_thread = NULL;
13956 			mutex_exit(&sata_event_mutex);
13957 			SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13958 			    "SATA_EVENT_DAEMON_TERMINATING", NULL);
13959 			thread_exit();  { _NOTE(NOT_REACHED) }
13960 		}
13961 		mutex_exit(&sata_event_mutex);
13962 	} while (!(sata_event_pending & SATA_EVNT_MAIN));
13963 
13964 	mutex_enter(&sata_event_mutex);
13965 	sata_event_thread_active = 1;
13966 	mutex_exit(&sata_event_mutex);
13967 
13968 	mutex_enter(&sata_mutex);
13969 	sata_event_pending &= ~SATA_EVNT_MAIN;
13970 	mutex_exit(&sata_mutex);
13971 
13972 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13973 	    "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL);
13974 
13975 	goto loop;
13976 }
13977 
13978 /*
13979  * Specific HBA instance event processing.
13980  *
13981  * NOTE: At the moment, device event processing is limited to hard disks
13982  * only.
13983  * cports only are supported - no pmports.
13984  */
13985 static void
13986 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst)
13987 {
13988 	int ncport;
13989 	uint32_t event_flags;
13990 	sata_address_t *saddr;
13991 
13992 	SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst,
13993 	    "Processing controller %d event(s)",
13994 	    ddi_get_instance(SATA_DIP(sata_hba_inst)));
13995 
13996 	mutex_enter(&sata_hba_inst->satahba_mutex);
13997 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN;
13998 	event_flags = sata_hba_inst->satahba_event_flags;
13999 	mutex_exit(&sata_hba_inst->satahba_mutex);
14000 	/*
14001 	 * Process controller power change first
14002 	 * HERE
14003 	 */
14004 	if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED)
14005 		sata_process_cntrl_pwr_level_change(sata_hba_inst);
14006 
14007 	/*
14008 	 * Search through ports/devices to identify affected port/device.
14009 	 * We may have to process events for more than one port/device.
14010 	 */
14011 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
14012 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14013 		event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))->
14014 		    cport_event_flags;
14015 		/* Check if port was locked by IOCTL processing */
14016 		if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) {
14017 			/*
14018 			 * We ignore port events because port is busy
14019 			 * with AP control processing. Set again
14020 			 * controller and main event flag, so that
14021 			 * events may be processed by the next daemon
14022 			 * run.
14023 			 */
14024 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14025 			mutex_enter(&sata_hba_inst->satahba_mutex);
14026 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14027 			mutex_exit(&sata_hba_inst->satahba_mutex);
14028 			mutex_enter(&sata_mutex);
14029 			sata_event_pending |= SATA_EVNT_MAIN;
14030 			mutex_exit(&sata_mutex);
14031 			SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst,
14032 			    "Event processing postponed until "
14033 			    "AP control processing completes",
14034 			    NULL);
14035 			/* Check other ports */
14036 			continue;
14037 		} else {
14038 			/*
14039 			 * Set BSY flag so that AP control would not
14040 			 * interfere with events processing for
14041 			 * this port.
14042 			 */
14043 			(SATA_CPORT_INFO(sata_hba_inst, ncport))->
14044 			    cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY;
14045 		}
14046 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14047 
14048 		saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr;
14049 
14050 		if ((event_flags &
14051 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
14052 			/*
14053 			 * Got port event.
14054 			 * We need some hierarchy of event processing as they
14055 			 * are affecting each other:
14056 			 * 1. port failed
14057 			 * 2. device detached/attached
14058 			 * 3. link events - link events may trigger device
14059 			 *    detached or device attached events in some
14060 			 *    circumstances.
14061 			 * 4. port power level changed
14062 			 */
14063 			if (event_flags & SATA_EVNT_PORT_FAILED) {
14064 				sata_process_port_failed_event(sata_hba_inst,
14065 				    saddr);
14066 			}
14067 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
14068 				sata_process_device_detached(sata_hba_inst,
14069 				    saddr);
14070 			}
14071 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
14072 				sata_process_device_attached(sata_hba_inst,
14073 				    saddr);
14074 			}
14075 			if (event_flags &
14076 			    (SATA_EVNT_LINK_ESTABLISHED |
14077 			    SATA_EVNT_LINK_LOST)) {
14078 				sata_process_port_link_events(sata_hba_inst,
14079 				    saddr);
14080 			}
14081 			if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) {
14082 				sata_process_port_pwr_change(sata_hba_inst,
14083 				    saddr);
14084 			}
14085 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
14086 				sata_process_target_node_cleanup(
14087 				    sata_hba_inst, saddr);
14088 			}
14089 			if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) {
14090 				sata_process_device_autoonline(
14091 				    sata_hba_inst, saddr);
14092 			}
14093 		}
14094 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14095 		if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) !=
14096 		    SATA_DTYPE_NONE) &&
14097 		    (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport) != NULL)) {
14098 			if (SATA_CPORT_DRV_INFO(sata_hba_inst, ncport)->
14099 			    satadrv_event_flags &
14100 			    (SATA_EVNT_DEVICE_RESET |
14101 			    SATA_EVNT_INPROC_DEVICE_RESET)) {
14102 				/* Have device event */
14103 				sata_process_device_reset(sata_hba_inst,
14104 				    saddr);
14105 			}
14106 		}
14107 		/* Release PORT_BUSY flag */
14108 		(SATA_CPORT_INFO(sata_hba_inst, ncport))->
14109 		    cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
14110 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14111 
14112 	} /* End of loop through the controller SATA ports */
14113 }
14114 
14115 /*
14116  * Process HBA power level change reported by HBA driver.
14117  * Not implemented at this time - event is ignored.
14118  */
14119 static void
14120 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst)
14121 {
14122 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14123 	    "Processing controller power level change", NULL);
14124 
14125 	/* Ignoring it for now */
14126 	mutex_enter(&sata_hba_inst->satahba_mutex);
14127 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
14128 	mutex_exit(&sata_hba_inst->satahba_mutex);
14129 }
14130 
14131 /*
14132  * Process port power level change reported by HBA driver.
14133  * Not implemented at this time - event is ignored.
14134  */
14135 static void
14136 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst,
14137     sata_address_t *saddr)
14138 {
14139 	sata_cport_info_t *cportinfo;
14140 
14141 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14142 	    "Processing port power level change", NULL);
14143 
14144 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14145 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14146 	/* Reset event flag */
14147 	cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
14148 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14149 }
14150 
14151 /*
14152  * Process port failure reported by HBA driver.
14153  * cports support only - no pmports.
14154  */
14155 static void
14156 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst,
14157     sata_address_t *saddr)
14158 {
14159 	sata_cport_info_t *cportinfo;
14160 
14161 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14162 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14163 	/* Reset event flag first */
14164 	cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED;
14165 	/* If the port is in SHUTDOWN or FAILED state, ignore this event. */
14166 	if ((cportinfo->cport_state &
14167 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) {
14168 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14169 		    cport_mutex);
14170 		return;
14171 	}
14172 	/* Fail the port */
14173 	cportinfo->cport_state = SATA_PSTATE_FAILED;
14174 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14175 	sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport);
14176 }
14177 
14178 /*
14179  * Device Reset Event processing.
14180  * The seqeunce is managed by 3 stage flags:
14181  * - reset event reported,
14182  * - reset event being processed,
14183  * - request to clear device reset state.
14184  *
14185  * NOTE: This function has to be entered with cport mutex held. It exits with
14186  * mutex held as well, but can release mutex during the processing.
14187  */
14188 static void
14189 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst,
14190     sata_address_t *saddr)
14191 {
14192 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
14193 	sata_drive_info_t *sdinfo;
14194 	sata_cport_info_t *cportinfo;
14195 	sata_device_t sata_device;
14196 	int rval;
14197 
14198 	/* We only care about host sata cport for now */
14199 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14200 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14201 	/*
14202 	 * If the port is in SHUTDOWN or FAILED state, or device is in FAILED
14203 	 * state, ignore reset event.
14204 	 */
14205 	if (((cportinfo->cport_state &
14206 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) ||
14207 	    (sdinfo->satadrv_state & SATA_DSTATE_FAILED) != 0) {
14208 		sdinfo->satadrv_event_flags &=
14209 		    ~(SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET);
14210 		return;
14211 	}
14212 
14213 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) &
14214 	    SATA_VALID_DEV_TYPE) == 0) {
14215 		/*
14216 		 * This should not happen - coding error.
14217 		 * But we can recover, so do not panic, just clean up
14218 		 * and if in debug mode, log the message.
14219 		 */
14220 #ifdef SATA_DEBUG
14221 		sata_log(sata_hba_inst, CE_WARN,
14222 		    "sata_process_device_reset: "
14223 		    "Invalid device type with sdinfo!", NULL);
14224 #endif
14225 		sdinfo->satadrv_event_flags = 0;
14226 		return;
14227 	}
14228 
14229 #ifdef SATA_DEBUG
14230 	if ((sdinfo->satadrv_event_flags &
14231 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
14232 		/* Nothing to do */
14233 		/* Something is weird - why we are processing dev reset? */
14234 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14235 		    "No device reset event!!!!", NULL);
14236 
14237 		return;
14238 	}
14239 	if ((sdinfo->satadrv_event_flags &
14240 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
14241 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
14242 		/* Something is weird - new device reset event */
14243 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14244 		    "Overlapping device reset events!", NULL);
14245 	}
14246 #endif
14247 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14248 	    "Processing port %d device reset", saddr->cport);
14249 
14250 	/* Clear event flag */
14251 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
14252 
14253 	/* It seems that we always need to check the port state first */
14254 	sata_device.satadev_rev = SATA_DEVICE_REV;
14255 	sata_device.satadev_addr = *saddr;
14256 	/*
14257 	 * We have to exit mutex, because the HBA probe port function may
14258 	 * block on its own mutex.
14259 	 */
14260 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14261 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14262 	    (SATA_DIP(sata_hba_inst), &sata_device);
14263 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14264 	sata_update_port_info(sata_hba_inst, &sata_device);
14265 	if (rval != SATA_SUCCESS) {
14266 		/* Something went wrong? Fail the port */
14267 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14268 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14269 		if (sdinfo != NULL)
14270 			sdinfo->satadrv_event_flags = 0;
14271 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14272 		    cport_mutex);
14273 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14274 		    "SATA port %d probing failed",
14275 		    saddr->cport));
14276 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14277 		    saddr->cport)->cport_mutex);
14278 		return;
14279 	}
14280 	if ((sata_device.satadev_scr.sstatus  &
14281 	    SATA_PORT_DEVLINK_UP_MASK) !=
14282 	    SATA_PORT_DEVLINK_UP ||
14283 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
14284 		/*
14285 		 * No device to process, anymore. Some other event processing
14286 		 * would or have already performed port info cleanup.
14287 		 * To be safe (HBA may need it), request clearing device
14288 		 * reset condition.
14289 		 */
14290 		sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14291 		if (sdinfo != NULL) {
14292 			sdinfo->satadrv_event_flags &=
14293 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
14294 			sdinfo->satadrv_event_flags |=
14295 			    SATA_EVNT_CLEAR_DEVICE_RESET;
14296 		}
14297 		return;
14298 	}
14299 
14300 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14301 	if (sdinfo == NULL) {
14302 		return;
14303 	}
14304 	if ((sdinfo->satadrv_event_flags &
14305 	    SATA_EVNT_INPROC_DEVICE_RESET) == 0) {
14306 		/*
14307 		 * Start tracking time for device feature restoration and
14308 		 * identification. Save current time (lbolt value).
14309 		 */
14310 		sdinfo->satadrv_reset_time = ddi_get_lbolt();
14311 	}
14312 	/* Mark device reset processing as active */
14313 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
14314 
14315 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
14316 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14317 
14318 	if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) ==
14319 	    SATA_FAILURE) {
14320 		/*
14321 		 * Restoring drive setting failed.
14322 		 * Probe the port first, to check if the port state has changed
14323 		 */
14324 		sata_device.satadev_rev = SATA_DEVICE_REV;
14325 		sata_device.satadev_addr = *saddr;
14326 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
14327 		/* probe port */
14328 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14329 		    (SATA_DIP(sata_hba_inst), &sata_device);
14330 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14331 		    cport_mutex);
14332 		if (rval == SATA_SUCCESS &&
14333 		    (sata_device.satadev_state &
14334 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
14335 		    (sata_device.satadev_scr.sstatus  &
14336 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
14337 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
14338 			/*
14339 			 * We may retry this a bit later - in-process reset
14340 			 * condition should be already set.
14341 			 * Track retry time for device identification.
14342 			 */
14343 			if ((cportinfo->cport_dev_type &
14344 			    SATA_VALID_DEV_TYPE) != 0 &&
14345 			    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL &&
14346 			    sdinfo->satadrv_reset_time != 0) {
14347 				clock_t cur_time = ddi_get_lbolt();
14348 				/*
14349 				 * If the retry time limit was not
14350 				 * exceeded, retry.
14351 				 */
14352 				if ((cur_time - sdinfo->satadrv_reset_time) <
14353 				    drv_usectohz(SATA_DEV_REPROBE_TIMEOUT)) {
14354 					mutex_enter(
14355 					    &sata_hba_inst->satahba_mutex);
14356 					sata_hba_inst->satahba_event_flags |=
14357 					    SATA_EVNT_MAIN;
14358 					mutex_exit(
14359 					    &sata_hba_inst->satahba_mutex);
14360 					mutex_enter(&sata_mutex);
14361 					sata_event_pending |= SATA_EVNT_MAIN;
14362 					mutex_exit(&sata_mutex);
14363 					return;
14364 				}
14365 			}
14366 			/* Fail the drive */
14367 			sdinfo->satadrv_state = SATA_DSTATE_FAILED;
14368 
14369 			sata_log(sata_hba_inst, CE_WARN,
14370 			    "SATA device at port %d - device failed",
14371 			    saddr->cport);
14372 		} else {
14373 			/*
14374 			 * No point of retrying - some other event processing
14375 			 * would or already did port info cleanup.
14376 			 * To be safe (HBA may need it),
14377 			 * request clearing device reset condition.
14378 			 */
14379 			sdinfo->satadrv_event_flags |=
14380 			    SATA_EVNT_CLEAR_DEVICE_RESET;
14381 		}
14382 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
14383 		sdinfo->satadrv_reset_time = 0;
14384 		return;
14385 	}
14386 	/*
14387 	 * Raise the flag indicating that the next sata command could
14388 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
14389 	 * reset is reported.
14390 	 */
14391 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14392 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14393 		sdinfo->satadrv_reset_time = 0;
14394 		if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0) {
14395 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14396 			sdinfo->satadrv_event_flags &=
14397 			    ~SATA_EVNT_INPROC_DEVICE_RESET;
14398 			sdinfo->satadrv_event_flags |=
14399 			    SATA_EVNT_CLEAR_DEVICE_RESET;
14400 		}
14401 	}
14402 }
14403 
14404 
14405 /*
14406  * Port Link Events processing.
14407  * Every link established event may involve device reset (due to
14408  * COMRESET signal, equivalent of the hard reset) so arbitrarily
14409  * set device reset event for an attached device (if any).
14410  * If the port is in SHUTDOWN or FAILED state, ignore link events.
14411  *
14412  * The link established event processing varies, depending on the state
14413  * of the target node, HBA hotplugging capabilities, state of the port.
14414  * If the link is not active, the link established event is ignored.
14415  * If HBA cannot detect device attachment and there is no target node,
14416  * the link established event triggers device attach event processing.
14417  * Else, link established event triggers device reset event processing.
14418  *
14419  * The link lost event processing varies, depending on a HBA hotplugging
14420  * capability and the state of the port (link active or not active).
14421  * If the link is active, the lost link event is ignored.
14422  * If HBA cannot detect device removal, the lost link event triggers
14423  * device detached event processing after link lost timeout.
14424  * Else, the event is ignored.
14425  *
14426  * NOTE: Only cports are processed for now, i.e. no port multiplier ports
14427  */
14428 static void
14429 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst,
14430     sata_address_t *saddr)
14431 {
14432 	sata_device_t sata_device;
14433 	sata_cport_info_t *cportinfo;
14434 	sata_drive_info_t *sdinfo;
14435 	uint32_t event_flags;
14436 	int rval;
14437 
14438 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14439 	    "Processing port %d link event(s)", saddr->cport);
14440 
14441 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14442 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14443 	event_flags = cportinfo->cport_event_flags;
14444 
14445 	/* Reset event flags first */
14446 	cportinfo->cport_event_flags &=
14447 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
14448 
14449 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
14450 	if ((cportinfo->cport_state &
14451 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14452 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14453 		    cport_mutex);
14454 		return;
14455 	}
14456 
14457 	/*
14458 	 * For the sanity sake get current port state.
14459 	 * Set device address only. Other sata_device fields should be
14460 	 * set by HBA driver.
14461 	 */
14462 	sata_device.satadev_rev = SATA_DEVICE_REV;
14463 	sata_device.satadev_addr = *saddr;
14464 	/*
14465 	 * We have to exit mutex, because the HBA probe port function may
14466 	 * block on its own mutex.
14467 	 */
14468 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14469 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14470 	    (SATA_DIP(sata_hba_inst), &sata_device);
14471 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14472 	sata_update_port_info(sata_hba_inst, &sata_device);
14473 	if (rval != SATA_SUCCESS) {
14474 		/* Something went wrong? Fail the port */
14475 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14476 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14477 		    cport_mutex);
14478 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14479 		    "SATA port %d probing failed",
14480 		    saddr->cport));
14481 		/*
14482 		 * We may want to release device info structure, but
14483 		 * it is not necessary.
14484 		 */
14485 		return;
14486 	} else {
14487 		/* port probed successfully */
14488 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14489 	}
14490 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
14491 
14492 		if ((sata_device.satadev_scr.sstatus &
14493 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
14494 			/* Ignore event */
14495 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14496 			    "Ignoring port %d link established event - "
14497 			    "link down",
14498 			    saddr->cport);
14499 			goto linklost;
14500 		}
14501 
14502 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14503 		    "Processing port %d link established event",
14504 		    saddr->cport);
14505 
14506 		/*
14507 		 * For the sanity sake check if a device is attached - check
14508 		 * return state of a port probing.
14509 		 */
14510 		if (sata_device.satadev_type != SATA_DTYPE_NONE &&
14511 		    sata_device.satadev_type != SATA_DTYPE_PMULT) {
14512 			/*
14513 			 * HBA port probe indicated that there is a device
14514 			 * attached. Check if the framework had device info
14515 			 * structure attached for this device.
14516 			 */
14517 			if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
14518 				ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) !=
14519 				    NULL);
14520 
14521 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14522 				if ((sdinfo->satadrv_type &
14523 				    SATA_VALID_DEV_TYPE) != 0) {
14524 					/*
14525 					 * Dev info structure is present.
14526 					 * If dev_type is set to known type in
14527 					 * the framework's drive info struct
14528 					 * then the device existed before and
14529 					 * the link was probably lost
14530 					 * momentarily - in such case
14531 					 * we may want to check device
14532 					 * identity.
14533 					 * Identity check is not supported now.
14534 					 *
14535 					 * Link established event
14536 					 * triggers device reset event.
14537 					 */
14538 					(SATA_CPORTINFO_DRV_INFO(cportinfo))->
14539 					    satadrv_event_flags |=
14540 					    SATA_EVNT_DEVICE_RESET;
14541 				}
14542 			} else if (cportinfo->cport_dev_type ==
14543 			    SATA_DTYPE_NONE) {
14544 				/*
14545 				 * We got new device attached! If HBA does not
14546 				 * generate device attached events, trigger it
14547 				 * here.
14548 				 */
14549 				if (!(SATA_FEATURES(sata_hba_inst) &
14550 				    SATA_CTLF_HOTPLUG)) {
14551 					cportinfo->cport_event_flags |=
14552 					    SATA_EVNT_DEVICE_ATTACHED;
14553 				}
14554 			}
14555 			/* Reset link lost timeout */
14556 			cportinfo->cport_link_lost_time = 0;
14557 		}
14558 	}
14559 linklost:
14560 	if (event_flags & SATA_EVNT_LINK_LOST) {
14561 		if ((sata_device.satadev_scr.sstatus &
14562 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
14563 			/* Ignore event */
14564 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14565 			    "Ignoring port %d link lost event - link is up",
14566 			    saddr->cport);
14567 			goto done;
14568 		}
14569 #ifdef SATA_DEBUG
14570 		if (cportinfo->cport_link_lost_time == 0) {
14571 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14572 			    "Processing port %d link lost event",
14573 			    saddr->cport);
14574 		}
14575 #endif
14576 		/*
14577 		 * When HBA cannot generate device attached/detached events,
14578 		 * we need to track link lost time and eventually generate
14579 		 * device detach event.
14580 		 */
14581 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
14582 			/* We are tracking link lost time */
14583 			if (cportinfo->cport_link_lost_time == 0) {
14584 				/* save current time (lbolt value) */
14585 				cportinfo->cport_link_lost_time =
14586 				    ddi_get_lbolt();
14587 				/* just keep link lost event */
14588 				cportinfo->cport_event_flags |=
14589 				    SATA_EVNT_LINK_LOST;
14590 			} else {
14591 				clock_t cur_time = ddi_get_lbolt();
14592 				if ((cur_time -
14593 				    cportinfo->cport_link_lost_time) >=
14594 				    drv_usectohz(
14595 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
14596 					/* trigger device detach event */
14597 					cportinfo->cport_event_flags |=
14598 					    SATA_EVNT_DEVICE_DETACHED;
14599 					cportinfo->cport_link_lost_time = 0;
14600 					SATADBG1(SATA_DBG_EVENTS,
14601 					    sata_hba_inst,
14602 					    "Triggering port %d "
14603 					    "device detached event",
14604 					    saddr->cport);
14605 				} else {
14606 					/* keep link lost event */
14607 					cportinfo->cport_event_flags |=
14608 					    SATA_EVNT_LINK_LOST;
14609 				}
14610 			}
14611 		}
14612 		/*
14613 		 * We could change port state to disable/delay access to
14614 		 * the attached device until the link is recovered.
14615 		 */
14616 	}
14617 done:
14618 	event_flags = cportinfo->cport_event_flags;
14619 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14620 	if (event_flags != 0) {
14621 		mutex_enter(&sata_hba_inst->satahba_mutex);
14622 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14623 		mutex_exit(&sata_hba_inst->satahba_mutex);
14624 		mutex_enter(&sata_mutex);
14625 		sata_event_pending |= SATA_EVNT_MAIN;
14626 		mutex_exit(&sata_mutex);
14627 	}
14628 }
14629 
14630 /*
14631  * Device Detached Event processing.
14632  * Port is probed to find if a device is really gone. If so,
14633  * the device info structure is detached from the SATA port info structure
14634  * and released.
14635  * Port status is updated.
14636  *
14637  * NOTE: Process cports event only, no port multiplier ports.
14638  */
14639 static void
14640 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst,
14641     sata_address_t *saddr)
14642 {
14643 	sata_cport_info_t *cportinfo;
14644 	sata_drive_info_t *sdevinfo;
14645 	sata_device_t sata_device;
14646 	dev_info_t *tdip;
14647 	int rval;
14648 
14649 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14650 	    "Processing port %d device detached", saddr->cport);
14651 
14652 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14653 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14654 	/* Clear event flag */
14655 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
14656 
14657 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
14658 	if ((cportinfo->cport_state &
14659 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14660 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14661 		    cport_mutex);
14662 		return;
14663 	}
14664 	/* For sanity, re-probe the port */
14665 	sata_device.satadev_rev = SATA_DEVICE_REV;
14666 	sata_device.satadev_addr = *saddr;
14667 
14668 	/*
14669 	 * We have to exit mutex, because the HBA probe port function may
14670 	 * block on its own mutex.
14671 	 */
14672 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14673 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14674 	    (SATA_DIP(sata_hba_inst), &sata_device);
14675 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14676 	sata_update_port_info(sata_hba_inst, &sata_device);
14677 	if (rval != SATA_SUCCESS) {
14678 		/* Something went wrong? Fail the port */
14679 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14680 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14681 		    cport_mutex);
14682 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14683 		    "SATA port %d probing failed",
14684 		    saddr->cport));
14685 		/*
14686 		 * We may want to release device info structure, but
14687 		 * it is not necessary.
14688 		 */
14689 		return;
14690 	} else {
14691 		/* port probed successfully */
14692 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14693 	}
14694 	/*
14695 	 * Check if a device is still attached. For sanity, check also
14696 	 * link status - if no link, there is no device.
14697 	 */
14698 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
14699 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
14700 	    SATA_DTYPE_NONE) {
14701 		/*
14702 		 * Device is still attached - ignore detach event.
14703 		 */
14704 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14705 		    cport_mutex);
14706 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14707 		    "Ignoring detach - device still attached to port %d",
14708 		    sata_device.satadev_addr.cport);
14709 		return;
14710 	}
14711 	/*
14712 	 * We need to detach and release device info structure here
14713 	 */
14714 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14715 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14716 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14717 		(void) kmem_free((void *)sdevinfo,
14718 		    sizeof (sata_drive_info_t));
14719 	}
14720 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14721 	/*
14722 	 * Device cannot be reached anymore, even if the target node may be
14723 	 * still present.
14724 	 */
14725 
14726 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14727 	sata_log(sata_hba_inst, CE_WARN, "SATA device detached at port %d",
14728 	    sata_device.satadev_addr.cport);
14729 
14730 	/*
14731 	 * Try to offline a device and remove target node if it still exists
14732 	 */
14733 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
14734 	if (tdip != NULL) {
14735 		/*
14736 		 * Target node exists.  Unconfigure device then remove
14737 		 * the target node (one ndi operation).
14738 		 */
14739 		if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
14740 			/*
14741 			 * PROBLEM - no device, but target node remained
14742 			 * This happens when the file was open or node was
14743 			 * waiting for resources.
14744 			 */
14745 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14746 			    "sata_process_device_detached: "
14747 			    "Failed to remove target node for "
14748 			    "detached SATA device."));
14749 			/*
14750 			 * Set target node state to DEVI_DEVICE_REMOVED.
14751 			 * But re-check first that the node still exists.
14752 			 */
14753 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
14754 			    saddr->cport);
14755 			if (tdip != NULL) {
14756 				sata_set_device_removed(tdip);
14757 				/*
14758 				 * Instruct event daemon to retry the
14759 				 * cleanup later.
14760 				 */
14761 				sata_set_target_node_cleanup(sata_hba_inst,
14762 				    &sata_device.satadev_addr);
14763 			}
14764 		}
14765 	}
14766 	/*
14767 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14768 	 * with the hint: SE_HINT_REMOVE
14769 	 */
14770 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
14771 }
14772 
14773 
14774 /*
14775  * Device Attached Event processing.
14776  * Port state is checked to verify that a device is really attached. If so,
14777  * the device info structure is created and attached to the SATA port info
14778  * structure.
14779  *
14780  * If attached device cannot be identified or set-up, the retry for the
14781  * attach processing is set-up. Subsequent daemon run would try again to
14782  * identify the device, until the time limit is reached
14783  * (SATA_DEV_IDENTIFY_TIMEOUT).
14784  *
14785  * This function cannot be called in interrupt context (it may sleep).
14786  *
14787  * NOTE: Process cports event only, no port multiplier ports.
14788  */
14789 static void
14790 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst,
14791     sata_address_t *saddr)
14792 {
14793 	sata_cport_info_t *cportinfo;
14794 	sata_drive_info_t *sdevinfo;
14795 	sata_device_t sata_device;
14796 	dev_info_t *tdip;
14797 	uint32_t event_flags;
14798 	int rval;
14799 
14800 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14801 	    "Processing port %d device attached", saddr->cport);
14802 
14803 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14804 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14805 
14806 	/* Clear attach event flag first */
14807 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
14808 
14809 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
14810 	if ((cportinfo->cport_state &
14811 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14812 		cportinfo->cport_dev_attach_time = 0;
14813 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14814 		    cport_mutex);
14815 		return;
14816 	}
14817 
14818 	/*
14819 	 * If the sata_drive_info structure is found attached to the port info,
14820 	 * despite the fact the device was removed and now it is re-attached,
14821 	 * the old drive info structure was not removed.
14822 	 * Arbitrarily release device info structure.
14823 	 */
14824 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14825 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14826 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14827 		(void) kmem_free((void *)sdevinfo,
14828 		    sizeof (sata_drive_info_t));
14829 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14830 		    "Arbitrarily detaching old device info.", NULL);
14831 	}
14832 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14833 
14834 	/* For sanity, re-probe the port */
14835 	sata_device.satadev_rev = SATA_DEVICE_REV;
14836 	sata_device.satadev_addr = *saddr;
14837 
14838 	/*
14839 	 * We have to exit mutex, because the HBA probe port function may
14840 	 * block on its own mutex.
14841 	 */
14842 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14843 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14844 	    (SATA_DIP(sata_hba_inst), &sata_device);
14845 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14846 	sata_update_port_info(sata_hba_inst, &sata_device);
14847 	if (rval != SATA_SUCCESS) {
14848 		/* Something went wrong? Fail the port */
14849 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14850 		cportinfo->cport_dev_attach_time = 0;
14851 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14852 		    cport_mutex);
14853 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14854 		    "SATA port %d probing failed",
14855 		    saddr->cport));
14856 		return;
14857 	} else {
14858 		/* port probed successfully */
14859 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14860 	}
14861 	/*
14862 	 * Check if a device is still attached. For sanity, check also
14863 	 * link status - if no link, there is no device.
14864 	 */
14865 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
14866 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
14867 	    SATA_DTYPE_NONE) {
14868 		/*
14869 		 * No device - ignore attach event.
14870 		 */
14871 		cportinfo->cport_dev_attach_time = 0;
14872 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14873 		    cport_mutex);
14874 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14875 		    "Ignoring attach - no device connected to port %d",
14876 		    sata_device.satadev_addr.cport);
14877 		return;
14878 	}
14879 
14880 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14881 	/*
14882 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14883 	 * with the hint: SE_HINT_INSERT
14884 	 */
14885 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
14886 
14887 	/*
14888 	 * Port reprobing will take care of the creation of the device
14889 	 * info structure and determination of the device type.
14890 	 */
14891 	sata_device.satadev_addr = *saddr;
14892 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
14893 	    SATA_DEV_IDENTIFY_NORETRY);
14894 
14895 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14896 	    cport_mutex);
14897 	if ((cportinfo->cport_state & SATA_STATE_READY) &&
14898 	    (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) {
14899 		/* Some device is attached to the port */
14900 		if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) {
14901 			/*
14902 			 * A device was not successfully attached.
14903 			 * Track retry time for device identification.
14904 			 */
14905 			if (cportinfo->cport_dev_attach_time != 0) {
14906 				clock_t cur_time = ddi_get_lbolt();
14907 				/*
14908 				 * If the retry time limit was not exceeded,
14909 				 * reinstate attach event.
14910 				 */
14911 				if ((cur_time -
14912 				    cportinfo->cport_dev_attach_time) <
14913 				    drv_usectohz(
14914 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
14915 					/* OK, restore attach event */
14916 					cportinfo->cport_event_flags |=
14917 					    SATA_EVNT_DEVICE_ATTACHED;
14918 				} else {
14919 					/* Timeout - cannot identify device */
14920 					cportinfo->cport_dev_attach_time = 0;
14921 					sata_log(sata_hba_inst,
14922 					    CE_WARN,
14923 					    "Could not identify SATA device "
14924 					    "at port %d",
14925 					    saddr->cport);
14926 				}
14927 			} else {
14928 				/*
14929 				 * Start tracking time for device
14930 				 * identification.
14931 				 * Save current time (lbolt value).
14932 				 */
14933 				cportinfo->cport_dev_attach_time =
14934 				    ddi_get_lbolt();
14935 				/* Restore attach event */
14936 				cportinfo->cport_event_flags |=
14937 				    SATA_EVNT_DEVICE_ATTACHED;
14938 			}
14939 		} else {
14940 			/*
14941 			 * If device was successfully attached, the subsequent
14942 			 * action depends on a state of the
14943 			 * sata_auto_online variable. If it is set to zero.
14944 			 * an explicit 'configure' command will be needed to
14945 			 * configure it. If its value is non-zero, we will
14946 			 * attempt to online (configure) the device.
14947 			 * First, log the message indicating that a device
14948 			 * was attached.
14949 			 */
14950 			cportinfo->cport_dev_attach_time = 0;
14951 			sata_log(sata_hba_inst, CE_WARN,
14952 			    "SATA device detected at port %d", saddr->cport);
14953 
14954 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14955 				sata_drive_info_t new_sdinfo;
14956 
14957 				/* Log device info data */
14958 				new_sdinfo = *(SATA_CPORTINFO_DRV_INFO(
14959 				    cportinfo));
14960 				sata_show_drive_info(sata_hba_inst,
14961 				    &new_sdinfo);
14962 			}
14963 
14964 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14965 			    saddr->cport)->cport_mutex);
14966 
14967 			/*
14968 			 * Make sure that there is no target node for that
14969 			 * device. If so, release it. It should not happen,
14970 			 * unless we had problem removing the node when
14971 			 * device was detached.
14972 			 */
14973 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
14974 			    saddr->cport);
14975 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14976 			    saddr->cport)->cport_mutex);
14977 			if (tdip != NULL) {
14978 
14979 #ifdef SATA_DEBUG
14980 				if ((cportinfo->cport_event_flags &
14981 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
14982 					sata_log(sata_hba_inst, CE_WARN,
14983 					    "sata_process_device_attached: "
14984 					    "old device target node exists!");
14985 #endif
14986 				/*
14987 				 * target node exists - try to unconfigure
14988 				 * device and remove the node.
14989 				 */
14990 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14991 				    saddr->cport)->cport_mutex);
14992 				rval = ndi_devi_offline(tdip,
14993 				    NDI_DEVI_REMOVE);
14994 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14995 				    saddr->cport)->cport_mutex);
14996 
14997 				if (rval == NDI_SUCCESS) {
14998 					cportinfo->cport_event_flags &=
14999 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
15000 					cportinfo->cport_tgtnode_clean = B_TRUE;
15001 				} else {
15002 					/*
15003 					 * PROBLEM - the target node remained
15004 					 * and it belongs to a previously
15005 					 * attached device.
15006 					 * This happens when the file was open
15007 					 * or the node was waiting for
15008 					 * resources at the time the
15009 					 * associated device was removed.
15010 					 * Instruct event daemon to retry the
15011 					 * cleanup later.
15012 					 */
15013 					sata_log(sata_hba_inst,
15014 					    CE_WARN,
15015 					    "Application(s) accessing "
15016 					    "previously attached SATA "
15017 					    "device have to release "
15018 					    "it before newly inserted "
15019 					    "device can be made accessible.",
15020 					    saddr->cport);
15021 					cportinfo->cport_event_flags |=
15022 					    SATA_EVNT_TARGET_NODE_CLEANUP;
15023 					cportinfo->cport_tgtnode_clean =
15024 					    B_FALSE;
15025 				}
15026 			}
15027 			if (sata_auto_online != 0) {
15028 				cportinfo->cport_event_flags |=
15029 				    SATA_EVNT_AUTOONLINE_DEVICE;
15030 			}
15031 
15032 		}
15033 	} else {
15034 		cportinfo->cport_dev_attach_time = 0;
15035 	}
15036 
15037 	event_flags = cportinfo->cport_event_flags;
15038 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15039 	if (event_flags != 0) {
15040 		mutex_enter(&sata_hba_inst->satahba_mutex);
15041 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15042 		mutex_exit(&sata_hba_inst->satahba_mutex);
15043 		mutex_enter(&sata_mutex);
15044 		sata_event_pending |= SATA_EVNT_MAIN;
15045 		mutex_exit(&sata_mutex);
15046 	}
15047 }
15048 
15049 
15050 /*
15051  * Device Target Node Cleanup Event processing.
15052  * If the target node associated with a sata port device is in
15053  * DEVI_DEVICE_REMOVED state, an attempt is made to remove it.
15054  * If the target node cannot be removed, the event flag is left intact,
15055  * so that event daemon may re-run this function later.
15056  *
15057  * This function cannot be called in interrupt context (it may sleep).
15058  *
15059  * NOTE: Processes cport events only, not port multiplier ports.
15060  */
15061 static void
15062 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
15063     sata_address_t *saddr)
15064 {
15065 	sata_cport_info_t *cportinfo;
15066 	dev_info_t *tdip;
15067 
15068 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15069 	    "Processing port %d device target node cleanup", saddr->cport);
15070 
15071 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15072 
15073 	/*
15074 	 * Check if there is target node for that device and it is in the
15075 	 * DEVI_DEVICE_REMOVED state. If so, release it.
15076 	 */
15077 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
15078 	if (tdip != NULL) {
15079 		/*
15080 		 * target node exists - check if it is target node of
15081 		 * a removed device.
15082 		 */
15083 		if (sata_check_device_removed(tdip) == B_TRUE) {
15084 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15085 			    "sata_process_target_node_cleanup: "
15086 			    "old device target node exists!", NULL);
15087 			/*
15088 			 * Unconfigure and remove the target node
15089 			 */
15090 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) ==
15091 			    NDI_SUCCESS) {
15092 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15093 				    saddr->cport)->cport_mutex);
15094 				cportinfo->cport_event_flags &=
15095 				    ~SATA_EVNT_TARGET_NODE_CLEANUP;
15096 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15097 				    saddr->cport)->cport_mutex);
15098 				return;
15099 			}
15100 			/*
15101 			 * Event daemon will retry the cleanup later.
15102 			 */
15103 			mutex_enter(&sata_hba_inst->satahba_mutex);
15104 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15105 			mutex_exit(&sata_hba_inst->satahba_mutex);
15106 			mutex_enter(&sata_mutex);
15107 			sata_event_pending |= SATA_EVNT_MAIN;
15108 			mutex_exit(&sata_mutex);
15109 		}
15110 	} else {
15111 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15112 		    saddr->cport)->cport_mutex);
15113 		cportinfo->cport_event_flags &=
15114 		    ~SATA_EVNT_TARGET_NODE_CLEANUP;
15115 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15116 		    saddr->cport)->cport_mutex);
15117 	}
15118 }
15119 
15120 /*
15121  * Device AutoOnline Event processing.
15122  * If attached device is to be onlined, an attempt is made to online this
15123  * device, but only if there is no lingering (old) target node present.
15124  * If the device cannot be onlined, the event flag is left intact,
15125  * so that event daemon may re-run this function later.
15126  *
15127  * This function cannot be called in interrupt context (it may sleep).
15128  *
15129  * NOTE: Processes cport events only, not port multiplier ports.
15130  */
15131 static void
15132 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst,
15133     sata_address_t *saddr)
15134 {
15135 	sata_cport_info_t *cportinfo;
15136 	sata_drive_info_t *sdinfo;
15137 	sata_device_t sata_device;
15138 	dev_info_t *tdip;
15139 
15140 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15141 	    "Processing port %d attached device auto-onlining", saddr->cport);
15142 
15143 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15144 
15145 	/*
15146 	 * Check if device is present and recognized. If not, reset event.
15147 	 */
15148 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15149 	if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
15150 		/* Nothing to online */
15151 		cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
15152 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15153 		    saddr->cport)->cport_mutex);
15154 		return;
15155 	}
15156 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15157 
15158 	/*
15159 	 * Check if there is target node for this device and if it is in the
15160 	 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep
15161 	 * the event for later processing.
15162 	 */
15163 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
15164 	if (tdip != NULL) {
15165 		/*
15166 		 * target node exists - check if it is target node of
15167 		 * a removed device.
15168 		 */
15169 		if (sata_check_device_removed(tdip) == B_TRUE) {
15170 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15171 			    "sata_process_device_autoonline: "
15172 			    "old device target node exists!", NULL);
15173 			/*
15174 			 * Event daemon will retry device onlining later.
15175 			 */
15176 			mutex_enter(&sata_hba_inst->satahba_mutex);
15177 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15178 			mutex_exit(&sata_hba_inst->satahba_mutex);
15179 			mutex_enter(&sata_mutex);
15180 			sata_event_pending |= SATA_EVNT_MAIN;
15181 			mutex_exit(&sata_mutex);
15182 			return;
15183 		}
15184 		/*
15185 		 * If the target node is not in the 'removed" state, assume
15186 		 * that it belongs to this device. There is nothing more to do,
15187 		 * but reset the event.
15188 		 */
15189 	} else {
15190 
15191 		/*
15192 		 * Try to online the device
15193 		 * If there is any reset-related event, remove it. We are
15194 		 * configuring the device and no state restoring is needed.
15195 		 */
15196 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15197 		    saddr->cport)->cport_mutex);
15198 		sata_device.satadev_addr = *saddr;
15199 		if (saddr->qual == SATA_ADDR_CPORT)
15200 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
15201 		else
15202 			sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
15203 		sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
15204 		if (sdinfo != NULL) {
15205 			if (sdinfo->satadrv_event_flags &
15206 			    (SATA_EVNT_DEVICE_RESET |
15207 			    SATA_EVNT_INPROC_DEVICE_RESET))
15208 				sdinfo->satadrv_event_flags = 0;
15209 			sdinfo->satadrv_event_flags |=
15210 			    SATA_EVNT_CLEAR_DEVICE_RESET;
15211 
15212 			/* Need to create a new target node. */
15213 			cportinfo->cport_tgtnode_clean = B_TRUE;
15214 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15215 			    saddr->cport)->cport_mutex);
15216 			tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
15217 			    sata_hba_inst, &sata_device.satadev_addr);
15218 			if (tdip == NULL) {
15219 				/*
15220 				 * Configure (onlining) failed.
15221 				 * We will NOT retry
15222 				 */
15223 				SATA_LOG_D((sata_hba_inst, CE_WARN,
15224 				    "sata_process_device_autoonline: "
15225 				    "configuring SATA device at port %d failed",
15226 				    saddr->cport));
15227 			}
15228 		} else {
15229 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15230 			    saddr->cport)->cport_mutex);
15231 		}
15232 
15233 	}
15234 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15235 	cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
15236 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15237 	    saddr->cport)->cport_mutex);
15238 }
15239 
15240 
15241 static void
15242 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr,
15243     int hint)
15244 {
15245 	char ap[MAXPATHLEN];
15246 	nvlist_t *ev_attr_list = NULL;
15247 	int err;
15248 
15249 	/* Allocate and build sysevent attribute list */
15250 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP);
15251 	if (err != 0) {
15252 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15253 		    "sata_gen_sysevent: "
15254 		    "cannot allocate memory for sysevent attributes\n"));
15255 		return;
15256 	}
15257 	/* Add hint attribute */
15258 	err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint));
15259 	if (err != 0) {
15260 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15261 		    "sata_gen_sysevent: "
15262 		    "failed to add DR_HINT attr for sysevent"));
15263 		nvlist_free(ev_attr_list);
15264 		return;
15265 	}
15266 	/*
15267 	 * Add AP attribute.
15268 	 * Get controller pathname and convert it into AP pathname by adding
15269 	 * a target number.
15270 	 */
15271 	(void) snprintf(ap, MAXPATHLEN, "/devices");
15272 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap));
15273 	(void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d",
15274 	    SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual));
15275 
15276 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap);
15277 	if (err != 0) {
15278 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15279 		    "sata_gen_sysevent: "
15280 		    "failed to add DR_AP_ID attr for sysevent"));
15281 		nvlist_free(ev_attr_list);
15282 		return;
15283 	}
15284 
15285 	/* Generate/log sysevent */
15286 	err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR,
15287 	    ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP);
15288 	if (err != DDI_SUCCESS) {
15289 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15290 		    "sata_gen_sysevent: "
15291 		    "cannot log sysevent, err code %x\n", err));
15292 	}
15293 
15294 	nvlist_free(ev_attr_list);
15295 }
15296 
15297 
15298 
15299 
15300 /*
15301  * Set DEVI_DEVICE_REMOVED state in the SATA device target node.
15302  */
15303 static void
15304 sata_set_device_removed(dev_info_t *tdip)
15305 {
15306 	int circ;
15307 
15308 	ASSERT(tdip != NULL);
15309 
15310 	ndi_devi_enter(tdip, &circ);
15311 	mutex_enter(&DEVI(tdip)->devi_lock);
15312 	DEVI_SET_DEVICE_REMOVED(tdip);
15313 	mutex_exit(&DEVI(tdip)->devi_lock);
15314 	ndi_devi_exit(tdip, circ);
15315 }
15316 
15317 
15318 /*
15319  * Set internal event instructing event daemon to try
15320  * to perform the target node cleanup.
15321  */
15322 static void
15323 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
15324     sata_address_t *saddr)
15325 {
15326 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15327 	SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |=
15328 	    SATA_EVNT_TARGET_NODE_CLEANUP;
15329 	SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_tgtnode_clean =
15330 	    B_FALSE;
15331 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15332 	mutex_enter(&sata_hba_inst->satahba_mutex);
15333 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15334 	mutex_exit(&sata_hba_inst->satahba_mutex);
15335 	mutex_enter(&sata_mutex);
15336 	sata_event_pending |= SATA_EVNT_MAIN;
15337 	mutex_exit(&sata_mutex);
15338 }
15339 
15340 
15341 /*
15342  * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state,
15343  * i.e. check if the target node state indicates that it belongs to a removed
15344  * device.
15345  *
15346  * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state,
15347  * B_FALSE otherwise.
15348  *
15349  * NOTE: No port multiplier support.
15350  */
15351 static boolean_t
15352 sata_check_device_removed(dev_info_t *tdip)
15353 {
15354 	ASSERT(tdip != NULL);
15355 
15356 	if (DEVI_IS_DEVICE_REMOVED(tdip))
15357 		return (B_TRUE);
15358 	else
15359 		return (B_FALSE);
15360 }
15361 
15362 /* ************************ FAULT INJECTTION **************************** */
15363 
15364 #ifdef SATA_INJECT_FAULTS
15365 
15366 static	uint32_t sata_fault_count = 0;
15367 static	uint32_t sata_fault_suspend_count = 0;
15368 
15369 /*
15370  * Inject sata pkt fault
15371  * It modifies returned values of the sata packet.
15372  * First argument is the pointer to the executed sata packet.
15373  * The second argument specifies SATA command to be affected (not all commands
15374  * are instrumented).
15375  * Third argument is a pointer to a value returned by the HBA tran_start
15376  * function.
15377  * Fourth argument specifies injected error. Injected sata packet faults
15378  * are the satapkt_reason values.
15379  * SATA_PKT_BUSY		-1	Not completed, busy
15380  * SATA_PKT_DEV_ERROR		1	Device reported error
15381  * SATA_PKT_QUEUE_FULL		2	Not accepted, queue full
15382  * SATA_PKT_PORT_ERROR		3	Not completed, port error
15383  * SATA_PKT_CMD_UNSUPPORTED	4	Cmd unsupported
15384  * SATA_PKT_ABORTED		5	Aborted by request
15385  * SATA_PKT_TIMEOUT		6	Operation timeut
15386  * SATA_PKT_RESET		7	Aborted by reset request
15387  *
15388  * sata_inject_fault_count variable specifies number of times in row the
15389  * error is injected. Value of -1 specifies permanent fault, ie. every time
15390  * the fault injection pointnis reached, the fault is injected and anu pause
15391  * between fault injection specified by sata_inject_fault_pause_count is
15392  * ignored).
15393  *
15394  * sata_inject_fault_pause_count variable specifies number of times a fault
15395  * injection is bypassed (pause between fault injections).
15396  * If set to 0, a fault is injected only a number of times specified by
15397  * sata_inject_fault_count.
15398  *
15399  * The fault counts are static, so for periodic errors they have to be manually
15400  * reset to start repetition sequence from scratch.
15401  * If the original value returned by the HBA tran_start function is not
15402  * SATA_TRAN_ACCEPTED and pkt reason is not SATA_PKT_COMPLETED, no error
15403  * is injected (to avoid masking real problems);
15404  *
15405  * NOTE: In its current incarnation, this function should be invoked only for
15406  * commands executed in SYNCHRONOUS mode.
15407  */
15408 
15409 
15410 static	void
15411 sata_inject_pkt_fault(sata_pkt_t *spkt, uint8_t cmd, int *rval,
15412     int fault)
15413 {
15414 	if (fault == 0)
15415 		return;
15416 	if (sata_inject_fault_count == 0)
15417 		return;
15418 
15419 	if (spkt->satapkt_cmd.satacmd_cmd_reg != cmd)
15420 		return;
15421 
15422 	if (*rval != SATA_TRAN_ACCEPTED ||
15423 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
15424 		sata_fault_count = 0;
15425 		sata_fault_suspend_count = 0;
15426 		return;
15427 	}
15428 	if (sata_fault_count == 0 && sata_fault_suspend_count != 0) {
15429 		/* Pause in the injection */
15430 		sata_fault_suspend_count -= 1;
15431 		return;
15432 	}
15433 
15434 	if (sata_fault_count == 0 && sata_fault_suspend_count == 0) {
15435 		/*
15436 		 * Init inject fault cycle. If fault count is set to -1,
15437 		 * it is a permanent fault.
15438 		 */
15439 		if (sata_inject_fault_count != -1) {
15440 			sata_fault_count = sata_inject_fault_count;
15441 			sata_fault_suspend_count =
15442 			    sata_inject_fault_pause_count;
15443 			if (sata_fault_suspend_count == 0)
15444 				sata_inject_fault_count = 0;
15445 		}
15446 	}
15447 
15448 	if (sata_fault_count != 0)
15449 		sata_fault_count -= 1;
15450 
15451 	switch (fault) {
15452 	case SATA_PKT_BUSY:
15453 		*rval = SATA_TRAN_BUSY;
15454 		spkt->satapkt_reason = SATA_PKT_BUSY;
15455 		break;
15456 
15457 	case SATA_PKT_QUEUE_FULL:
15458 		*rval = SATA_TRAN_QUEUE_FULL;
15459 		spkt->satapkt_reason = SATA_PKT_QUEUE_FULL;
15460 		break;
15461 
15462 	case SATA_PKT_CMD_UNSUPPORTED:
15463 		*rval = SATA_TRAN_CMD_UNSUPPORTED;
15464 		spkt->satapkt_reason = SATA_PKT_CMD_UNSUPPORTED;
15465 		break;
15466 
15467 	case SATA_PKT_PORT_ERROR:
15468 		/* This is "rejected" command */
15469 		*rval = SATA_TRAN_PORT_ERROR;
15470 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
15471 		/* Additional error setup could be done here - port state */
15472 		break;
15473 
15474 	case SATA_PKT_DEV_ERROR:
15475 		spkt->satapkt_reason = SATA_PKT_DEV_ERROR;
15476 		/*
15477 		 * Additional error setup could be done here
15478 		 */
15479 		break;
15480 
15481 	case SATA_PKT_ABORTED:
15482 		spkt->satapkt_reason = SATA_PKT_ABORTED;
15483 		break;
15484 
15485 	case SATA_PKT_TIMEOUT:
15486 		spkt->satapkt_reason = SATA_PKT_TIMEOUT;
15487 		/* Additional error setup could be done here */
15488 		break;
15489 
15490 	case SATA_PKT_RESET:
15491 		spkt->satapkt_reason = SATA_PKT_RESET;
15492 		/*
15493 		 * Additional error setup could be done here - device reset
15494 		 */
15495 		break;
15496 
15497 	default:
15498 		break;
15499 	}
15500 }
15501 
15502 #endif
15503