xref: /titanic_44/usr/src/uts/common/io/sata/impl/sata.c (revision e79c98e6c943cb3032f272714ff4ce6137d40394)
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 
57 /*
58  * Flags enabling selected SATA HBA framework functionality
59  */
60 #define	SATA_ENABLE_QUEUING		1
61 #define	SATA_ENABLE_NCQ			2
62 #define	SATA_ENABLE_PROCESS_EVENTS	4
63 int sata_func_enable =
64 	SATA_ENABLE_PROCESS_EVENTS | SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ;
65 
66 /*
67  * Global variable setting default maximum queue depth (NCQ or TCQ)
68  * Note:minimum queue depth is 1
69  */
70 int sata_max_queue_depth = SATA_MAX_QUEUE_DEPTH; /* max NCQ/TCQ queue depth */
71 
72 /*
73  * Currently used default NCQ/TCQ queue depth. It is set-up during the driver
74  * initialization, using value from sata_max_queue_depth
75  * It is adjusted to minimum supported by the controller and by the device,
76  * if queueing is enabled.
77  */
78 static	int sata_current_max_qdepth;
79 
80 /*
81  * Global variable determining the default behavior after device hotpluggin.
82  * If non-zero, the hotplugged device is onlined (if possible) without explicit
83  * IOCTL request (AP_CONFIGURE).
84  * If zero, hotplugged device is identified, but not onlined.
85  * Enabling (AP_CONNECT) device port with an attached device does not result
86  * in device onlining regardless of the flag setting
87  */
88 int sata_auto_online = 0;
89 
90 #ifdef SATA_DEBUG
91 
92 #define	SATA_LOG_D(args)	sata_log args
93 uint64_t mbuf_count = 0;
94 uint64_t mbuffail_count = 0;
95 
96 sata_atapi_cmd_t sata_atapi_trace[64];
97 uint32_t sata_atapi_trace_index = 0;
98 int sata_atapi_trace_save = 1;
99 static	void sata_save_atapi_trace(sata_pkt_txlate_t *, int);
100 #define	SATAATAPITRACE(spx, count)	if (sata_atapi_trace_save) \
101     sata_save_atapi_trace(spx, count);
102 
103 #else
104 #define	SATA_LOG_D(arg)
105 #define	SATAATAPITRACE(spx, count)
106 #endif
107 
108 #if 0
109 static void
110 sata_test_atapi_packet_command(sata_hba_inst_t *, int);
111 #endif
112 #define	LEGACY_HWID_LEN	64	/* Model (40) + Serial (20) + pad */
113 
114 
115 /*
116  * SATA cb_ops functions
117  */
118 static 	int sata_hba_open(dev_t *, int, int, cred_t *);
119 static 	int sata_hba_close(dev_t, int, int, cred_t *);
120 static 	int sata_hba_ioctl(dev_t, int, intptr_t, int, cred_t *,	int *);
121 
122 /*
123  * SCSA required entry points
124  */
125 static	int sata_scsi_tgt_init(dev_info_t *, dev_info_t *,
126     scsi_hba_tran_t *, struct scsi_device *);
127 static	int sata_scsi_tgt_probe(struct scsi_device *,
128     int (*callback)(void));
129 static void sata_scsi_tgt_free(dev_info_t *, dev_info_t *,
130     scsi_hba_tran_t *, struct scsi_device *);
131 static 	int sata_scsi_start(struct scsi_address *, struct scsi_pkt *);
132 static 	int sata_scsi_abort(struct scsi_address *, struct scsi_pkt *);
133 static 	int sata_scsi_reset(struct scsi_address *, int);
134 static 	int sata_scsi_getcap(struct scsi_address *, char *, int);
135 static 	int sata_scsi_setcap(struct scsi_address *, char *, int, int);
136 static 	struct scsi_pkt *sata_scsi_init_pkt(struct scsi_address *,
137     struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(caddr_t),
138     caddr_t);
139 static 	void sata_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *);
140 static 	void sata_scsi_dmafree(struct scsi_address *, struct scsi_pkt *);
141 static 	void sata_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *);
142 
143 /*
144  * SATA HBA interface functions are defined in sata_hba.h header file
145  */
146 
147 /* Event processing functions */
148 static	void sata_event_daemon(void *);
149 static	void sata_event_thread_control(int);
150 static	void sata_process_controller_events(sata_hba_inst_t *sata_hba_inst);
151 static	void sata_process_device_reset(sata_hba_inst_t *, sata_address_t *);
152 static	void sata_process_port_failed_event(sata_hba_inst_t *,
153     sata_address_t *);
154 static	void sata_process_port_link_events(sata_hba_inst_t *,
155     sata_address_t *);
156 static	void sata_process_device_detached(sata_hba_inst_t *, sata_address_t *);
157 static	void sata_process_device_attached(sata_hba_inst_t *, sata_address_t *);
158 static	void sata_process_port_pwr_change(sata_hba_inst_t *, sata_address_t *);
159 static	void sata_process_cntrl_pwr_level_change(sata_hba_inst_t *);
160 static	void sata_process_target_node_cleanup(sata_hba_inst_t *,
161     sata_address_t *);
162 static	void sata_process_device_autoonline(sata_hba_inst_t *,
163     sata_address_t *saddr);
164 
165 /*
166  * Local translation functions
167  */
168 static	int sata_txlt_inquiry(sata_pkt_txlate_t *);
169 static	int sata_txlt_test_unit_ready(sata_pkt_txlate_t *);
170 static	int sata_txlt_start_stop_unit(sata_pkt_txlate_t *);
171 static	int sata_txlt_read_capacity(sata_pkt_txlate_t *);
172 static	int sata_txlt_request_sense(sata_pkt_txlate_t *);
173 static 	int sata_txlt_read(sata_pkt_txlate_t *);
174 static 	int sata_txlt_write(sata_pkt_txlate_t *);
175 static 	int sata_txlt_log_sense(sata_pkt_txlate_t *);
176 static 	int sata_txlt_log_select(sata_pkt_txlate_t *);
177 static 	int sata_txlt_mode_sense(sata_pkt_txlate_t *);
178 static 	int sata_txlt_mode_select(sata_pkt_txlate_t *);
179 static 	int sata_txlt_synchronize_cache(sata_pkt_txlate_t *);
180 static 	int sata_txlt_write_buffer(sata_pkt_txlate_t *);
181 static 	int sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *);
182 
183 static 	int sata_hba_start(sata_pkt_txlate_t *, int *);
184 static	int sata_txlt_invalid_command(sata_pkt_txlate_t *);
185 static	int sata_txlt_lba_out_of_range(sata_pkt_txlate_t *);
186 static 	void sata_txlt_rw_completion(sata_pkt_t *);
187 static 	void sata_txlt_nodata_cmd_completion(sata_pkt_t *);
188 static 	void sata_txlt_download_mcode_cmd_completion(sata_pkt_t *);
189 static 	int sata_emul_rw_completion(sata_pkt_txlate_t *);
190 static 	struct scsi_extended_sense *sata_immediate_error_response(
191     sata_pkt_txlate_t *, int);
192 static	struct scsi_extended_sense *sata_arq_sense(sata_pkt_txlate_t *);
193 
194 static 	int sata_txlt_atapi(sata_pkt_txlate_t *);
195 static 	void sata_txlt_atapi_completion(sata_pkt_t *);
196 
197 /*
198  * Local functions for ioctl
199  */
200 static	int32_t sata_get_port_num(sata_hba_inst_t *,  struct devctl_iocdata *);
201 static	void sata_cfgadm_state(sata_hba_inst_t *, int32_t,
202     devctl_ap_state_t *);
203 static	dev_info_t *sata_get_target_dip(dev_info_t *, int32_t);
204 static	dev_info_t *sata_get_scsi_target_dip(dev_info_t *, sata_address_t *);
205 static	dev_info_t *sata_devt_to_devinfo(dev_t);
206 static	int sata_ioctl_connect(sata_hba_inst_t *, sata_device_t *);
207 static	int sata_ioctl_disconnect(sata_hba_inst_t *, sata_device_t *);
208 static	int sata_ioctl_configure(sata_hba_inst_t *, sata_device_t *);
209 static	int sata_ioctl_unconfigure(sata_hba_inst_t *, sata_device_t *);
210 static	int sata_ioctl_activate(sata_hba_inst_t *, sata_device_t *);
211 static	int sata_ioctl_deactivate(sata_hba_inst_t *, sata_device_t *);
212 static	int sata_ioctl_reset_port(sata_hba_inst_t *, sata_device_t *);
213 static	int sata_ioctl_reset_device(sata_hba_inst_t *, sata_device_t *);
214 static	int sata_ioctl_reset_all(sata_hba_inst_t *);
215 static	int sata_ioctl_port_self_test(sata_hba_inst_t *, sata_device_t *);
216 static	int sata_ioctl_get_device_path(sata_hba_inst_t *, sata_device_t *,
217     sata_ioctl_data_t *, int mode);
218 static	int sata_ioctl_get_ap_type(sata_hba_inst_t *, sata_device_t *,
219     sata_ioctl_data_t *, int mode);
220 static	int sata_ioctl_get_model_info(sata_hba_inst_t *, sata_device_t *,
221     sata_ioctl_data_t *, int mode);
222 static	int sata_ioctl_get_revfirmware_info(sata_hba_inst_t *, sata_device_t *,
223     sata_ioctl_data_t *, int mode);
224 static	int sata_ioctl_get_serialnumber_info(sata_hba_inst_t *,
225     sata_device_t *, sata_ioctl_data_t *, int mode);
226 
227 /*
228  * Local functions
229  */
230 static 	void sata_remove_hba_instance(dev_info_t *);
231 static 	int sata_validate_sata_hba_tran(dev_info_t *, sata_hba_tran_t *);
232 static 	void sata_probe_ports(sata_hba_inst_t *);
233 static 	int sata_reprobe_port(sata_hba_inst_t *, sata_device_t *, int);
234 static 	int sata_add_device(dev_info_t *, sata_hba_inst_t *, int cport,
235     int pmport);
236 static 	dev_info_t *sata_create_target_node(dev_info_t *, sata_hba_inst_t *,
237     sata_address_t *);
238 static 	int sata_validate_scsi_address(sata_hba_inst_t *,
239     struct scsi_address *, sata_device_t *);
240 static 	int sata_validate_sata_address(sata_hba_inst_t *, int, int, int);
241 static	sata_pkt_t *sata_pkt_alloc(sata_pkt_txlate_t *, int (*)(caddr_t));
242 static	void sata_pkt_free(sata_pkt_txlate_t *);
243 static	int sata_dma_buf_setup(sata_pkt_txlate_t *, int, int (*)(caddr_t),
244     caddr_t, ddi_dma_attr_t *);
245 static	int sata_probe_device(sata_hba_inst_t *, sata_device_t *);
246 static	sata_drive_info_t *sata_get_device_info(sata_hba_inst_t *,
247     sata_device_t *);
248 static 	int sata_identify_device(sata_hba_inst_t *, sata_drive_info_t *);
249 static	struct buf *sata_alloc_local_buffer(sata_pkt_txlate_t *, int);
250 static 	void sata_free_local_buffer(sata_pkt_txlate_t *);
251 static 	uint64_t sata_check_capacity(sata_drive_info_t *);
252 void 	sata_adjust_dma_attr(sata_drive_info_t *, ddi_dma_attr_t *,
253     ddi_dma_attr_t *);
254 static 	int sata_fetch_device_identify_data(sata_hba_inst_t *,
255     sata_drive_info_t *);
256 static	void sata_update_port_info(sata_hba_inst_t *, sata_device_t *);
257 static	void sata_update_port_scr(sata_port_scr_t *, sata_device_t *);
258 static	int sata_set_dma_mode(sata_hba_inst_t *, sata_drive_info_t *);
259 static	int sata_set_cache_mode(sata_hba_inst_t *, sata_drive_info_t *, int);
260 static	int sata_set_rmsn(sata_hba_inst_t *, sata_drive_info_t *, int);
261 static	int sata_set_drive_features(sata_hba_inst_t *,
262     sata_drive_info_t *, int flag);
263 static	void sata_init_write_cache_mode(sata_drive_info_t *sdinfo);
264 static	int sata_initialize_device(sata_hba_inst_t *, sata_drive_info_t *);
265 static	void sata_identdev_to_inquiry(sata_hba_inst_t *, sata_drive_info_t *,
266     uint8_t *);
267 static	int sata_get_atapi_inquiry_data(sata_hba_inst_t *, sata_address_t *,
268     struct scsi_inquiry *);
269 static	int sata_build_msense_page_1(sata_drive_info_t *, int, uint8_t *);
270 static	int sata_build_msense_page_8(sata_drive_info_t *, int, uint8_t *);
271 static	int sata_build_msense_page_1a(sata_drive_info_t *, int, uint8_t *);
272 static	int sata_build_msense_page_1c(sata_drive_info_t *, int, uint8_t *);
273 static	int sata_mode_select_page_8(sata_pkt_txlate_t *,
274     struct mode_cache_scsi3 *, int, int *, int *, int *);
275 static	int sata_mode_select_page_1c(sata_pkt_txlate_t *,
276     struct mode_info_excpt_page *, int, int *, int *, int *);
277 static	int sata_build_msense_page_30(sata_drive_info_t *, int, uint8_t *);
278 static	int sata_mode_select_page_30(sata_pkt_txlate_t *,
279     struct mode_acoustic_management *, int, int *, int *, int *);
280 
281 static	int sata_build_lsense_page_0(sata_drive_info_t *, uint8_t *);
282 static	int sata_build_lsense_page_10(sata_drive_info_t *, uint8_t *,
283     sata_hba_inst_t *);
284 static	int sata_build_lsense_page_2f(sata_drive_info_t *, uint8_t *,
285     sata_hba_inst_t *);
286 static	int sata_build_lsense_page_30(sata_drive_info_t *, uint8_t *,
287     sata_hba_inst_t *);
288 static	void sata_save_drive_settings(sata_drive_info_t *);
289 static	void sata_show_drive_info(sata_hba_inst_t *, sata_drive_info_t *);
290 static	void sata_log(sata_hba_inst_t *, uint_t, char *fmt, ...);
291 static	int sata_fetch_smart_return_status(sata_hba_inst_t *,
292     sata_drive_info_t *);
293 static	int sata_fetch_smart_data(sata_hba_inst_t *, sata_drive_info_t *,
294     struct smart_data *);
295 static	int sata_smart_selftest_log(sata_hba_inst_t *,
296     sata_drive_info_t *,
297     struct smart_selftest_log *);
298 static	int sata_ext_smart_selftest_read_log(sata_hba_inst_t *,
299     sata_drive_info_t *, struct smart_ext_selftest_log *, uint16_t);
300 static	int sata_smart_read_log(sata_hba_inst_t *, sata_drive_info_t *,
301     uint8_t *, uint8_t, uint8_t);
302 static	int sata_read_log_ext_directory(sata_hba_inst_t *, sata_drive_info_t *,
303     struct read_log_ext_directory *);
304 static	void sata_gen_sysevent(sata_hba_inst_t *, sata_address_t *, int);
305 static	void sata_xlate_errors(sata_pkt_txlate_t *);
306 static	void sata_decode_device_error(sata_pkt_txlate_t *,
307     struct scsi_extended_sense *);
308 static	void sata_set_device_removed(dev_info_t *);
309 static	boolean_t sata_check_device_removed(dev_info_t *);
310 static	void sata_set_target_node_cleanup(sata_hba_inst_t *, sata_address_t *);
311 static	int sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *,
312     sata_drive_info_t *);
313 static	int sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *,
314     sata_drive_info_t *);
315 static	void sata_atapi_packet_cmd_setup(sata_cmd_t *, sata_drive_info_t *);
316 static	void sata_fixed_sense_data_preset(struct scsi_extended_sense *);
317 static  void sata_target_devid_register(dev_info_t *, sata_drive_info_t *);
318 static  int sata_check_modser(char *, int);
319 
320 
321 
322 /*
323  * SATA Framework will ignore SATA HBA driver cb_ops structure and
324  * register following one with SCSA framework.
325  * Open & close are provided, so scsi framework will not use its own
326  */
327 static struct cb_ops sata_cb_ops = {
328 	sata_hba_open,			/* open */
329 	sata_hba_close,			/* close */
330 	nodev,				/* strategy */
331 	nodev,				/* print */
332 	nodev,				/* dump */
333 	nodev,				/* read */
334 	nodev,				/* write */
335 	sata_hba_ioctl,			/* ioctl */
336 	nodev,				/* devmap */
337 	nodev,				/* mmap */
338 	nodev,				/* segmap */
339 	nochpoll,			/* chpoll */
340 	ddi_prop_op,			/* cb_prop_op */
341 	0,				/* streamtab */
342 	D_NEW | D_MP,			/* cb_flag */
343 	CB_REV,				/* rev */
344 	nodev,				/* aread */
345 	nodev				/* awrite */
346 };
347 
348 
349 extern struct mod_ops mod_miscops;
350 extern uchar_t	scsi_cdb_size[];
351 
352 static struct modlmisc modlmisc = {
353 	&mod_miscops,			/* Type of module */
354 	"SATA Module v%I%"		/* module name */
355 };
356 
357 
358 static struct modlinkage modlinkage = {
359 	MODREV_1,
360 	(void *)&modlmisc,
361 	NULL
362 };
363 
364 /*
365  * Default sata pkt timeout. Used when a target driver scsi_pkt time is zero,
366  * i.e. when scsi_pkt has not timeout specified.
367  */
368 static int sata_default_pkt_time = 60;	/* 60 seconds */
369 
370 /*
371  * Intermediate buffer device access attributes - they are required,
372  * but not necessarily used.
373  */
374 static ddi_device_acc_attr_t sata_acc_attr = {
375 	DDI_DEVICE_ATTR_V0,
376 	DDI_STRUCTURE_LE_ACC,
377 	DDI_STRICTORDER_ACC
378 };
379 
380 
381 /*
382  * Mutexes protecting structures in multithreaded operations.
383  * Because events are relatively rare, a single global mutex protecting
384  * data structures should be sufficient. To increase performance, add
385  * separate mutex per each sata port and use global mutex only to protect
386  * common data structures.
387  */
388 static	kmutex_t sata_mutex;		/* protects sata_hba_list */
389 static	kmutex_t sata_log_mutex;	/* protects log */
390 
391 static 	char sata_log_buf[256];
392 
393 /* Default write cache setting for SATA hard disks */
394 int	sata_write_cache = 1;		/* enabled */
395 
396 /* Default write cache setting for SATA ATAPI CD/DVD */
397 int 	sata_atapicdvd_write_cache = 1; /* enabled */
398 
399 /*
400  * Linked list of HBA instances
401  */
402 static 	sata_hba_inst_t *sata_hba_list = NULL;
403 static 	sata_hba_inst_t *sata_hba_list_tail = NULL;
404 /*
405  * Pointer to per-instance SATA HBA soft structure is stored in sata_hba_tran
406  * structure and in sata soft state.
407  */
408 
409 /*
410  * Event daemon related variables
411  */
412 static 	kmutex_t sata_event_mutex;
413 static 	kcondvar_t sata_event_cv;
414 static 	kthread_t *sata_event_thread = NULL;
415 static 	int sata_event_thread_terminate = 0;
416 static 	int sata_event_pending = 0;
417 static 	int sata_event_thread_active = 0;
418 extern 	pri_t minclsyspri;
419 
420 /*
421  * NCQ error recovery command
422  */
423 static const sata_cmd_t sata_rle_cmd = {
424 	SATA_CMD_REV,
425 	NULL,
426 	{
427 		SATA_DIR_READ
428 	},
429 	ATA_ADDR_LBA48,
430 	0,
431 	0,
432 	0,
433 	0,
434 	0,
435 	1,
436 	READ_LOG_EXT_NCQ_ERROR_RECOVERY,
437 	0,
438 	0,
439 	0,
440 	SATAC_READ_LOG_EXT,
441 	0,
442 	0,
443 	0,
444 };
445 
446 /*
447  * ATAPI error recovery CDB
448  */
449 static const uint8_t sata_rqsense_cdb[SATA_ATAPI_RQSENSE_CDB_LEN] = {
450 	SCMD_REQUEST_SENSE,
451 	0,			/* Only fixed RQ format is supported */
452 	0,
453 	0,
454 	SATA_ATAPI_MIN_RQSENSE_LEN, /* Less data may be returned */
455 	0
456 };
457 
458 
459 /* Warlock directives */
460 
461 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran))
462 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_device))
463 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_ops))
464 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_extended_sense))
465 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_arq_status))
466 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_attr))
467 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", ddi_dma_cookie_t))
468 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", devctl_ap_state))
469 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", dev_info::devi_state))
470 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_list))
471 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_list))
472 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_next))
473 _NOTE(MUTEX_PROTECTS_DATA(sata_mutex, sata_hba_inst::satahba_prev))
474 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", \
475     sata_hba_inst::satahba_scsi_tran))
476 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_tran))
477 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_hba_inst::satahba_dip))
478 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_hba_inst::satahba_attached))
479 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_hba_inst::satahba_dev_port))
480 _NOTE(MUTEX_PROTECTS_DATA(sata_hba_inst::satahba_mutex,
481     sata_hba_inst::satahba_event_flags))
482 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
483     sata_cport_info::cport_devp))
484 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_devp))
485 _NOTE(SCHEME_PROTECTS_DATA("Scheme", sata_cport_info::cport_addr))
486 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
487     sata_cport_info::cport_dev_type))
488 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_dev_type))
489 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
490     sata_cport_info::cport_state))
491 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_cport_info::cport_state))
492 _NOTE(MUTEX_PROTECTS_DATA(sata_cport_info::cport_mutex, \
493     sata_pmport_info::pmport_state))
494 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_state))
495 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_dev_type))
496 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmport_info::pmport_sata_drive))
497 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_dev_port))
498 _NOTE(DATA_READABLE_WITHOUT_LOCK(sata_pmult_info::pmult_num_dev_ports))
499 #ifdef SATA_DEBUG
500 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuf_count))
501 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mbuffail_count))
502 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace))
503 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", sata_atapi_trace_index))
504 #endif
505 
506 /* End of warlock directives */
507 
508 /* ************** loadable module configuration functions ************** */
509 
510 int
511 _init()
512 {
513 	int rval;
514 
515 	mutex_init(&sata_mutex, NULL, MUTEX_DRIVER, NULL);
516 	mutex_init(&sata_event_mutex, NULL, MUTEX_DRIVER, NULL);
517 	mutex_init(&sata_log_mutex, NULL, MUTEX_DRIVER, NULL);
518 	cv_init(&sata_event_cv, NULL, CV_DRIVER, NULL);
519 	if ((rval = mod_install(&modlinkage)) != 0) {
520 #ifdef SATA_DEBUG
521 		cmn_err(CE_WARN, "sata: _init: mod_install failed\n");
522 #endif
523 		mutex_destroy(&sata_log_mutex);
524 		cv_destroy(&sata_event_cv);
525 		mutex_destroy(&sata_event_mutex);
526 		mutex_destroy(&sata_mutex);
527 	}
528 	return (rval);
529 }
530 
531 int
532 _fini()
533 {
534 	int rval;
535 
536 	if ((rval = mod_remove(&modlinkage)) != 0)
537 		return (rval);
538 
539 	mutex_destroy(&sata_log_mutex);
540 	cv_destroy(&sata_event_cv);
541 	mutex_destroy(&sata_event_mutex);
542 	mutex_destroy(&sata_mutex);
543 	return (rval);
544 }
545 
546 int
547 _info(struct modinfo *modinfop)
548 {
549 	return (mod_info(&modlinkage, modinfop));
550 }
551 
552 
553 
554 /* ********************* SATA HBA entry points ********************* */
555 
556 
557 /*
558  * Called by SATA HBA from _init().
559  * Registers HBA driver instance/sata framework pair with scsi framework, by
560  * calling scsi_hba_init().
561  *
562  * SATA HBA driver cb_ops are ignored - SATA HBA framework cb_ops are used
563  * instead. SATA HBA framework cb_ops pointer overwrites SATA HBA driver
564  * cb_ops pointer in SATA HBA driver dev_ops structure.
565  * SATA HBA framework cb_ops supplies cb_open cb_close and cb_ioctl vectors.
566  *
567  * Return status of the scsi_hba_init() is returned to a calling SATA HBA
568  * driver.
569  */
570 int
571 sata_hba_init(struct modlinkage *modlp)
572 {
573 	int rval;
574 	struct dev_ops *hba_ops;
575 
576 	SATADBG1(SATA_DBG_HBA_IF, NULL,
577 	    "sata_hba_init: name %s \n",
578 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
579 	/*
580 	 * Fill-up cb_ops and dev_ops when necessary
581 	 */
582 	hba_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
583 	/*
584 	 * Provide pointer to SATA dev_ops
585 	 */
586 	hba_ops->devo_cb_ops = &sata_cb_ops;
587 
588 	/*
589 	 * Register SATA HBA with SCSI framework
590 	 */
591 	if ((rval = scsi_hba_init(modlp)) != 0) {
592 		SATADBG1(SATA_DBG_HBA_IF, NULL,
593 		    "sata_hba_init: scsi hba init failed\n", NULL);
594 		return (rval);
595 	}
596 
597 	return (0);
598 }
599 
600 
601 /* HBA attach stages */
602 #define	HBA_ATTACH_STAGE_SATA_HBA_INST	1
603 #define	HBA_ATTACH_STAGE_SCSI_ATTACHED	2
604 #define	HBA_ATTACH_STAGE_SETUP		4
605 #define	HBA_ATTACH_STAGE_LINKED		8
606 
607 
608 /*
609  *
610  * Called from SATA HBA driver's attach routine to attach an instance of
611  * the HBA.
612  *
613  * For DDI_ATTACH command:
614  * sata_hba_inst structure is allocated here and initialized with pointers to
615  * SATA framework implementation of required scsi tran functions.
616  * The scsi_tran's tran_hba_private field is used by SATA Framework to point
617  * to the soft structure (sata_hba_inst) allocated by SATA framework for
618  * SATA HBA instance related data.
619  * The scsi_tran's tran_hba_private field is used by SATA framework to
620  * store a pointer to per-HBA-instance of sata_hba_inst structure.
621  * The sata_hba_inst structure is cross-linked to scsi tran structure.
622  * Among other info, a pointer to sata_hba_tran structure is stored in
623  * sata_hba_inst. The sata_hba_inst structures for different HBA instances are
624  * linked together into the list, pointed to by sata_hba_list.
625  * On the first HBA instance attach the sata event thread is initialized.
626  * Attachment points are created for all SATA ports of the HBA being attached.
627  * All HBA instance's SATA ports are probed and type of plugged devices is
628  * determined. For each device of a supported type, a target node is created.
629  *
630  * DDI_SUCCESS is returned when attachment process is successful,
631  * DDI_FAILURE is returned otherwise.
632  *
633  * For DDI_RESUME command:
634  * Not implemented at this time (postponed until phase 2 of the development).
635  */
636 int
637 sata_hba_attach(dev_info_t *dip, sata_hba_tran_t *sata_tran,
638     ddi_attach_cmd_t cmd)
639 {
640 	sata_hba_inst_t	*sata_hba_inst;
641 	scsi_hba_tran_t *scsi_tran = NULL;
642 	int hba_attach_state = 0;
643 	char taskq_name[MAXPATHLEN];
644 
645 	SATADBG3(SATA_DBG_HBA_IF, NULL,
646 	    "sata_hba_attach: node %s (%s%d)\n",
647 	    ddi_node_name(dip), ddi_driver_name(dip),
648 	    ddi_get_instance(dip));
649 
650 	if (cmd == DDI_RESUME) {
651 		/*
652 		 * Postponed until phase 2 of the development
653 		 */
654 		return (DDI_FAILURE);
655 	}
656 
657 	if (cmd != DDI_ATTACH) {
658 		return (DDI_FAILURE);
659 	}
660 
661 	/* cmd == DDI_ATTACH */
662 
663 	if (sata_validate_sata_hba_tran(dip, sata_tran) != SATA_SUCCESS) {
664 		SATA_LOG_D((NULL, CE_WARN,
665 		    "sata_hba_attach: invalid sata_hba_tran"));
666 		return (DDI_FAILURE);
667 	}
668 	/*
669 	 * Allocate and initialize SCSI tran structure.
670 	 * SATA copy of tran_bus_config is provided to create port nodes.
671 	 */
672 	scsi_tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
673 	if (scsi_tran == NULL)
674 		return (DDI_FAILURE);
675 	/*
676 	 * Allocate soft structure for SATA HBA instance.
677 	 * There is a separate softstate for each HBA instance.
678 	 */
679 	sata_hba_inst = kmem_zalloc(sizeof (struct sata_hba_inst), KM_SLEEP);
680 	ASSERT(sata_hba_inst != NULL); /* this should not fail */
681 	mutex_init(&sata_hba_inst->satahba_mutex, NULL, MUTEX_DRIVER, NULL);
682 	hba_attach_state |= HBA_ATTACH_STAGE_SATA_HBA_INST;
683 
684 	/*
685 	 * scsi_trans's tran_hba_private is used by SATA Framework to point to
686 	 * soft structure allocated by SATA framework for
687 	 * SATA HBA instance related data.
688 	 */
689 	scsi_tran->tran_hba_private	= sata_hba_inst;
690 	scsi_tran->tran_tgt_private	= NULL;
691 
692 	scsi_tran->tran_tgt_init	= sata_scsi_tgt_init;
693 	scsi_tran->tran_tgt_probe	= sata_scsi_tgt_probe;
694 	scsi_tran->tran_tgt_free	= sata_scsi_tgt_free;
695 
696 	scsi_tran->tran_start		= sata_scsi_start;
697 	scsi_tran->tran_reset		= sata_scsi_reset;
698 	scsi_tran->tran_abort		= sata_scsi_abort;
699 	scsi_tran->tran_getcap		= sata_scsi_getcap;
700 	scsi_tran->tran_setcap		= sata_scsi_setcap;
701 	scsi_tran->tran_init_pkt	= sata_scsi_init_pkt;
702 	scsi_tran->tran_destroy_pkt	= sata_scsi_destroy_pkt;
703 
704 	scsi_tran->tran_dmafree		= sata_scsi_dmafree;
705 	scsi_tran->tran_sync_pkt	= sata_scsi_sync_pkt;
706 
707 	scsi_tran->tran_reset_notify	= NULL;
708 	scsi_tran->tran_get_bus_addr	= NULL;
709 	scsi_tran->tran_quiesce		= NULL;
710 	scsi_tran->tran_unquiesce	= NULL;
711 	scsi_tran->tran_bus_reset	= NULL;
712 
713 	if (scsi_hba_attach_setup(dip, sata_tran->sata_tran_hba_dma_attr,
714 	    scsi_tran, 0) != DDI_SUCCESS) {
715 #ifdef SATA_DEBUG
716 		cmn_err(CE_WARN, "?SATA: %s%d hba scsi attach failed",
717 		    ddi_driver_name(dip), ddi_get_instance(dip));
718 #endif
719 		goto fail;
720 	}
721 	hba_attach_state |= HBA_ATTACH_STAGE_SCSI_ATTACHED;
722 
723 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "sata")) {
724 		if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
725 		    "sata", 1) != DDI_PROP_SUCCESS) {
726 			SATA_LOG_D((NULL, CE_WARN, "sata_hba_attach: "
727 			    "failed to create hba sata prop"));
728 			goto fail;
729 		}
730 	}
731 
732 	/*
733 	 * Save pointers in hba instance soft state.
734 	 */
735 	sata_hba_inst->satahba_scsi_tran = scsi_tran;
736 	sata_hba_inst->satahba_tran = sata_tran;
737 	sata_hba_inst->satahba_dip = dip;
738 
739 	/*
740 	 * Create a task queue to handle emulated commands completion
741 	 * Use node name, dash, instance number as the queue name.
742 	 */
743 	taskq_name[0] = '\0';
744 	(void) strlcat(taskq_name, DEVI(dip)->devi_node_name,
745 	    sizeof (taskq_name));
746 	(void) snprintf(taskq_name + strlen(taskq_name),
747 	    sizeof (taskq_name) - strlen(taskq_name),
748 	    "-%d", DEVI(dip)->devi_instance);
749 	sata_hba_inst->satahba_taskq = taskq_create(taskq_name, 1,
750 	    minclsyspri, 1, sata_tran->sata_tran_hba_num_cports,
751 	    TASKQ_DYNAMIC);
752 
753 	hba_attach_state |= HBA_ATTACH_STAGE_SETUP;
754 
755 	/*
756 	 * Create events thread if not created yet.
757 	 */
758 	sata_event_thread_control(1);
759 
760 	/*
761 	 * Link this hba instance into the list.
762 	 */
763 	mutex_enter(&sata_mutex);
764 
765 	if (sata_hba_list == NULL) {
766 		/*
767 		 * The first instance of HBA is attached.
768 		 * Set current/active default maximum NCQ/TCQ queue depth for
769 		 * all SATA devices. It is done here and now, to eliminate the
770 		 * possibility of the dynamic, programatic modification of the
771 		 * queue depth via global (and public) sata_max_queue_depth
772 		 * variable (this would require special handling in HBA drivers)
773 		 */
774 		sata_current_max_qdepth = sata_max_queue_depth;
775 		if (sata_current_max_qdepth > 32)
776 			sata_current_max_qdepth = 32;
777 		else if (sata_current_max_qdepth < 1)
778 			sata_current_max_qdepth = 1;
779 	}
780 
781 	sata_hba_inst->satahba_next = NULL;
782 	sata_hba_inst->satahba_prev = sata_hba_list_tail;
783 	if (sata_hba_list == NULL) {
784 		sata_hba_list = sata_hba_inst;
785 	}
786 	if (sata_hba_list_tail != NULL) {
787 		sata_hba_list_tail->satahba_next = sata_hba_inst;
788 	}
789 	sata_hba_list_tail = sata_hba_inst;
790 	mutex_exit(&sata_mutex);
791 	hba_attach_state |= HBA_ATTACH_STAGE_LINKED;
792 
793 	/*
794 	 * Create SATA HBA devctl minor node for sata_hba_open, close, ioctl
795 	 * SATA HBA driver should not use its own open/close entry points.
796 	 *
797 	 * Make sure that instance number doesn't overflow
798 	 * when forming minor numbers.
799 	 */
800 	ASSERT(ddi_get_instance(dip) <= (L_MAXMIN >> INST_MINOR_SHIFT));
801 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
802 	    INST2DEVCTL(ddi_get_instance(dip)),
803 	    DDI_NT_SATA_NEXUS, 0) != DDI_SUCCESS) {
804 #ifdef SATA_DEBUG
805 		cmn_err(CE_WARN, "sata_hba_attach: "
806 		    "cannot create devctl minor node");
807 #endif
808 		goto fail;
809 	}
810 
811 
812 	/*
813 	 * Set-up kstats here, if necessary.
814 	 * (postponed until phase 2 of the development).
815 	 */
816 
817 
818 	/*
819 	 * Probe controller ports. This operation will describe a current
820 	 * controller/port/multipliers/device configuration and will create
821 	 * attachment points.
822 	 * We may end-up with just a controller with no devices attached.
823 	 * For the ports with a supported device attached, device target nodes
824 	 * are created and devices are initialized.
825 	 */
826 	sata_probe_ports(sata_hba_inst);
827 
828 	sata_hba_inst->satahba_attached = 1;
829 	return (DDI_SUCCESS);
830 
831 fail:
832 	if (hba_attach_state & HBA_ATTACH_STAGE_LINKED) {
833 		(void) sata_remove_hba_instance(dip);
834 		if (sata_hba_list == NULL)
835 			sata_event_thread_control(0);
836 	}
837 
838 	if (hba_attach_state & HBA_ATTACH_STAGE_SETUP) {
839 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
840 		taskq_destroy(sata_hba_inst->satahba_taskq);
841 	}
842 
843 	if (hba_attach_state & HBA_ATTACH_STAGE_SCSI_ATTACHED)
844 		(void) scsi_hba_detach(dip);
845 
846 	if (hba_attach_state & HBA_ATTACH_STAGE_SATA_HBA_INST) {
847 		mutex_destroy(&sata_hba_inst->satahba_mutex);
848 		kmem_free((void *)sata_hba_inst,
849 		    sizeof (struct sata_hba_inst));
850 		scsi_hba_tran_free(scsi_tran);
851 	}
852 
853 	sata_log(NULL, CE_WARN, "?SATA: %s%d hba attach failed",
854 	    ddi_driver_name(dip), ddi_get_instance(dip));
855 
856 	return (DDI_FAILURE);
857 }
858 
859 
860 /*
861  * Called by SATA HBA from to detach an instance of the driver.
862  *
863  * For DDI_DETACH command:
864  * Free local structures allocated for SATA HBA instance during
865  * sata_hba_attach processing.
866  *
867  * Returns DDI_SUCCESS when HBA was detached, DDI_FAILURE otherwise.
868  *
869  * For DDI_SUSPEND command:
870  * Not implemented at this time (postponed until phase 2 of the development)
871  * Returnd DDI_SUCCESS.
872  *
873  * When the last HBA instance is detached, the event daemon is terminated.
874  *
875  * NOTE: cport support only, no port multiplier support.
876  */
877 int
878 sata_hba_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
879 {
880 	dev_info_t	*tdip;
881 	sata_hba_inst_t	*sata_hba_inst;
882 	scsi_hba_tran_t *scsi_hba_tran;
883 	sata_cport_info_t *cportinfo;
884 	sata_drive_info_t *sdinfo;
885 	int ncport;
886 
887 	SATADBG3(SATA_DBG_HBA_IF, NULL, "sata_hba_detach: node %s (%s%d)\n",
888 	    ddi_node_name(dip), ddi_driver_name(dip), ddi_get_instance(dip));
889 
890 	switch (cmd) {
891 	case DDI_DETACH:
892 
893 		if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
894 			return (DDI_FAILURE);
895 
896 		sata_hba_inst = scsi_hba_tran->tran_hba_private;
897 		if (sata_hba_inst == NULL)
898 			return (DDI_FAILURE);
899 
900 		if (scsi_hba_detach(dip) == DDI_FAILURE) {
901 			sata_hba_inst->satahba_attached = 1;
902 			return (DDI_FAILURE);
903 		}
904 
905 		/*
906 		 * Free all target nodes - at this point
907 		 * devices should be at least offlined
908 		 * otherwise scsi_hba_detach() should not be called.
909 		 */
910 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
911 		    ncport++) {
912 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
913 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
914 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
915 				if (sdinfo != NULL) {
916 					tdip = sata_get_target_dip(dip,
917 					    ncport);
918 					if (tdip != NULL) {
919 						if (ndi_devi_offline(tdip,
920 						    NDI_DEVI_REMOVE) !=
921 						    NDI_SUCCESS) {
922 							SATA_LOG_D((
923 							    sata_hba_inst,
924 							    CE_WARN,
925 							    "sata_hba_detach: "
926 							    "Target node not "
927 							    "removed !"));
928 							return (DDI_FAILURE);
929 						}
930 					}
931 				}
932 			}
933 		}
934 		/*
935 		 * Disable sata event daemon processing for this HBA
936 		 */
937 		sata_hba_inst->satahba_attached = 0;
938 
939 		/*
940 		 * Remove event daemon thread, if it is last HBA instance.
941 		 */
942 
943 		mutex_enter(&sata_mutex);
944 		if (sata_hba_list->satahba_next == NULL) {
945 			mutex_exit(&sata_mutex);
946 			sata_event_thread_control(0);
947 			mutex_enter(&sata_mutex);
948 		}
949 		mutex_exit(&sata_mutex);
950 
951 		/* Remove this HBA instance from the HBA list */
952 		sata_remove_hba_instance(dip);
953 
954 		/*
955 		 * At this point there should be no target nodes attached.
956 		 * Detach and destroy device and port info structures.
957 		 */
958 		for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst);
959 		    ncport++) {
960 			cportinfo = SATA_CPORT_INFO(sata_hba_inst, ncport);
961 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
962 				sdinfo =
963 				    cportinfo->cport_devp.cport_sata_drive;
964 				if (sdinfo != NULL) {
965 					/* Release device structure */
966 					kmem_free(sdinfo,
967 					    sizeof (sata_drive_info_t));
968 				}
969 				/* Release cport info */
970 				mutex_destroy(&cportinfo->cport_mutex);
971 				kmem_free(cportinfo,
972 				    sizeof (sata_cport_info_t));
973 			}
974 		}
975 
976 		scsi_hba_tran_free(sata_hba_inst->satahba_scsi_tran);
977 
978 		(void) ddi_prop_remove(DDI_DEV_T_ANY, dip, "sata");
979 
980 		taskq_destroy(sata_hba_inst->satahba_taskq);
981 
982 		mutex_destroy(&sata_hba_inst->satahba_mutex);
983 		kmem_free((void *)sata_hba_inst,
984 		    sizeof (struct sata_hba_inst));
985 
986 		return (DDI_SUCCESS);
987 
988 	case DDI_SUSPEND:
989 		/*
990 		 * Postponed until phase 2
991 		 */
992 		return (DDI_FAILURE);
993 
994 	default:
995 		return (DDI_FAILURE);
996 	}
997 }
998 
999 
1000 /*
1001  * Called by an HBA drive from _fini() routine.
1002  * Unregisters SATA HBA instance/SATA framework pair from the scsi framework.
1003  */
1004 void
1005 sata_hba_fini(struct modlinkage *modlp)
1006 {
1007 	SATADBG1(SATA_DBG_HBA_IF, NULL,
1008 	    "sata_hba_fini: name %s\n",
1009 	    ((struct modldrv *)(modlp->ml_linkage[0]))->drv_linkinfo);
1010 
1011 	scsi_hba_fini(modlp);
1012 }
1013 
1014 
1015 /*
1016  * Default open and close routine for sata_hba framework.
1017  *
1018  */
1019 /*
1020  * Open devctl node.
1021  *
1022  * Returns:
1023  * 0 if node was open successfully, error code otherwise.
1024  *
1025  *
1026  */
1027 
1028 static int
1029 sata_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1030 {
1031 #ifndef __lock_lint
1032 	_NOTE(ARGUNUSED(credp))
1033 #endif
1034 	int rv = 0;
1035 	dev_info_t *dip;
1036 	scsi_hba_tran_t *scsi_hba_tran;
1037 	sata_hba_inst_t	*sata_hba_inst;
1038 
1039 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_open: entered", NULL);
1040 
1041 	if (otyp != OTYP_CHR)
1042 		return (EINVAL);
1043 
1044 	dip = sata_devt_to_devinfo(*devp);
1045 	if (dip == NULL)
1046 		return (ENXIO);
1047 
1048 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1049 		return (ENXIO);
1050 
1051 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1052 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1053 		return (ENXIO);
1054 
1055 	mutex_enter(&sata_mutex);
1056 	if (flags & FEXCL) {
1057 		if (sata_hba_inst->satahba_open_flag != 0) {
1058 			rv = EBUSY;
1059 		} else {
1060 			sata_hba_inst->satahba_open_flag =
1061 			    SATA_DEVCTL_EXOPENED;
1062 		}
1063 	} else {
1064 		if (sata_hba_inst->satahba_open_flag == SATA_DEVCTL_EXOPENED) {
1065 			rv = EBUSY;
1066 		} else {
1067 			sata_hba_inst->satahba_open_flag =
1068 			    SATA_DEVCTL_SOPENED;
1069 		}
1070 	}
1071 	mutex_exit(&sata_mutex);
1072 
1073 	return (rv);
1074 }
1075 
1076 
1077 /*
1078  * Close devctl node.
1079  * Returns:
1080  * 0 if node was closed successfully, error code otherwise.
1081  *
1082  */
1083 
1084 static int
1085 sata_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
1086 {
1087 #ifndef __lock_lint
1088 	_NOTE(ARGUNUSED(credp))
1089 	_NOTE(ARGUNUSED(flag))
1090 #endif
1091 	dev_info_t *dip;
1092 	scsi_hba_tran_t *scsi_hba_tran;
1093 	sata_hba_inst_t	*sata_hba_inst;
1094 
1095 	SATADBG1(SATA_DBG_IOCTL_IF, NULL, "sata_hba_close: entered", NULL);
1096 
1097 	if (otyp != OTYP_CHR)
1098 		return (EINVAL);
1099 
1100 	dip = sata_devt_to_devinfo(dev);
1101 	if (dip == NULL)
1102 		return (ENXIO);
1103 
1104 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1105 		return (ENXIO);
1106 
1107 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1108 	if (sata_hba_inst == NULL || sata_hba_inst->satahba_attached == 0)
1109 		return (ENXIO);
1110 
1111 	mutex_enter(&sata_mutex);
1112 	sata_hba_inst->satahba_open_flag = 0;
1113 	mutex_exit(&sata_mutex);
1114 	return (0);
1115 }
1116 
1117 
1118 
1119 /*
1120  * Standard IOCTL commands for SATA hotplugging.
1121  * Implemented DEVCTL_AP commands:
1122  * DEVCTL_AP_CONNECT
1123  * DEVCTL_AP_DISCONNECT
1124  * DEVCTL_AP_CONFIGURE
1125  * DEVCTL_UNCONFIGURE
1126  * DEVCTL_AP_CONTROL
1127  *
1128  * Commands passed to default ndi ioctl handler:
1129  * DEVCTL_DEVICE_GETSTATE
1130  * DEVCTL_DEVICE_ONLINE
1131  * DEVCTL_DEVICE_OFFLINE
1132  * DEVCTL_DEVICE_REMOVE
1133  * DEVCTL_DEVICE_INSERT
1134  * DEVCTL_BUS_GETSTATE
1135  *
1136  * All other cmds are passed to HBA if it provide ioctl handler, or failed
1137  * if not.
1138  *
1139  * Returns:
1140  * 0 if successful,
1141  * error code if operation failed.
1142  *
1143  * NOTE: Port Multiplier is not supported.
1144  *
1145  */
1146 
1147 static int
1148 sata_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1149     int *rvalp)
1150 {
1151 #ifndef __lock_lint
1152 	_NOTE(ARGUNUSED(credp))
1153 	_NOTE(ARGUNUSED(rvalp))
1154 #endif
1155 	int rv = 0;
1156 	int32_t	comp_port = -1;
1157 	dev_info_t *dip;
1158 	devctl_ap_state_t ap_state;
1159 	struct devctl_iocdata *dcp = NULL;
1160 	scsi_hba_tran_t *scsi_hba_tran;
1161 	sata_hba_inst_t *sata_hba_inst;
1162 	sata_device_t sata_device;
1163 	sata_cport_info_t *cportinfo;
1164 	int cport, pmport, qual;
1165 	int rval = SATA_SUCCESS;
1166 
1167 	dip = sata_devt_to_devinfo(dev);
1168 	if (dip == NULL)
1169 		return (ENXIO);
1170 
1171 	if ((scsi_hba_tran = ddi_get_driver_private(dip)) == NULL)
1172 		return (ENXIO);
1173 
1174 	sata_hba_inst = scsi_hba_tran->tran_hba_private;
1175 	if (sata_hba_inst == NULL)
1176 		return (ENXIO);
1177 
1178 	if (sata_hba_inst->satahba_tran == NULL)
1179 		return (ENXIO);
1180 
1181 	switch (cmd) {
1182 
1183 	case DEVCTL_DEVICE_GETSTATE:
1184 	case DEVCTL_DEVICE_ONLINE:
1185 	case DEVCTL_DEVICE_OFFLINE:
1186 	case DEVCTL_DEVICE_REMOVE:
1187 	case DEVCTL_BUS_GETSTATE:
1188 		/*
1189 		 * There may be more cases that we want to pass to default
1190 		 * handler rather than fail them.
1191 		 */
1192 		return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
1193 	}
1194 
1195 	/* read devctl ioctl data */
1196 	if (cmd != DEVCTL_AP_CONTROL) {
1197 		if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
1198 			return (EFAULT);
1199 
1200 		if ((comp_port = sata_get_port_num(sata_hba_inst, dcp)) ==
1201 		    -1) {
1202 			if (dcp)
1203 				ndi_dc_freehdl(dcp);
1204 			return (EINVAL);
1205 		}
1206 
1207 		cport = SCSI_TO_SATA_CPORT(comp_port);
1208 		pmport = SCSI_TO_SATA_PMPORT(comp_port);
1209 		/* Only cport is considered now, i.e. SATA_ADDR_CPORT */
1210 		qual = SATA_ADDR_CPORT;
1211 		if (sata_validate_sata_address(sata_hba_inst, cport, pmport,
1212 		    qual) != 0) {
1213 			ndi_dc_freehdl(dcp);
1214 			return (EINVAL);
1215 		}
1216 
1217 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1218 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1219 		    cport_mutex);
1220 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1221 			/*
1222 			 * Cannot process ioctl request now. Come back later.
1223 			 */
1224 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1225 			    cport_mutex);
1226 			ndi_dc_freehdl(dcp);
1227 			return (EBUSY);
1228 		}
1229 		/* Block event processing for this port */
1230 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1231 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1232 
1233 		sata_device.satadev_addr.cport = cport;
1234 		sata_device.satadev_addr.pmport = pmport;
1235 		sata_device.satadev_addr.qual = qual;
1236 		sata_device.satadev_rev = SATA_DEVICE_REV;
1237 	}
1238 
1239 	switch (cmd) {
1240 
1241 	case DEVCTL_AP_DISCONNECT:
1242 
1243 		/*
1244 		 * Normally, cfgadm sata plugin will try to offline
1245 		 * (unconfigure) device before this request. Nevertheless,
1246 		 * if a device is still configured, we need to
1247 		 * attempt to offline and unconfigure device first, and we will
1248 		 * deactivate the port regardless of the unconfigure
1249 		 * operation results.
1250 		 *
1251 		 */
1252 		rv = sata_ioctl_disconnect(sata_hba_inst, &sata_device);
1253 
1254 		break;
1255 
1256 	case DEVCTL_AP_UNCONFIGURE:
1257 
1258 		/*
1259 		 * The unconfigure operation uses generic nexus operation to
1260 		 * offline a device. It leaves a target device node attached.
1261 		 * and obviously sata_drive_info attached as well, because
1262 		 * from the hardware point of view nothing has changed.
1263 		 */
1264 		rv = sata_ioctl_unconfigure(sata_hba_inst, &sata_device);
1265 		break;
1266 
1267 	case DEVCTL_AP_CONNECT:
1268 	{
1269 		/*
1270 		 * The sata cfgadm pluging will invoke this operation only if
1271 		 * port was found in the disconnect state (failed state
1272 		 * is also treated as the disconnected state).
1273 		 * If port activation is successful and a device is found
1274 		 * attached to the port, the initialization sequence is
1275 		 * executed to probe the port and attach
1276 		 * a device structure to a port structure. The device is not
1277 		 * set in configured state (system-wise) by this operation.
1278 		 */
1279 
1280 		rv = sata_ioctl_connect(sata_hba_inst, &sata_device);
1281 
1282 		break;
1283 	}
1284 
1285 	case DEVCTL_AP_CONFIGURE:
1286 	{
1287 		/*
1288 		 * A port may be in an active or shutdown state.
1289 		 * If port is in a failed state, operation is aborted.
1290 		 * If a port is in a shutdown state, sata_tran_port_activate()
1291 		 * is invoked prior to any other operation.
1292 		 *
1293 		 * Onlining the device involves creating a new target node.
1294 		 * If there is an old target node present (belonging to
1295 		 * previously removed device), the operation is aborted - the
1296 		 * old node has to be released and removed before configure
1297 		 * operation is attempted.
1298 		 */
1299 
1300 		rv = sata_ioctl_configure(sata_hba_inst, &sata_device);
1301 
1302 		break;
1303 	}
1304 
1305 	case DEVCTL_AP_GETSTATE:
1306 
1307 		sata_cfgadm_state(sata_hba_inst, comp_port, &ap_state);
1308 
1309 		ap_state.ap_last_change = (time_t)-1;
1310 		ap_state.ap_error_code = 0;
1311 		ap_state.ap_in_transition = 0;
1312 
1313 		/* Copy the return AP-state information to the user space */
1314 		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS) {
1315 			rv = EFAULT;
1316 		}
1317 		break;
1318 
1319 	case DEVCTL_AP_CONTROL:
1320 	{
1321 		/*
1322 		 * Generic devctl for hardware specific functionality
1323 		 */
1324 		sata_ioctl_data_t	ioc;
1325 
1326 		ASSERT(dcp == NULL);
1327 
1328 		/* Copy in user ioctl data first */
1329 #ifdef _MULTI_DATAMODEL
1330 		if (ddi_model_convert_from(mode & FMODELS) ==
1331 		    DDI_MODEL_ILP32) {
1332 
1333 			sata_ioctl_data_32_t	ioc32;
1334 
1335 			if (ddi_copyin((void *)arg, (void *)&ioc32,
1336 			    sizeof (ioc32), mode) != 0) {
1337 				rv = EFAULT;
1338 				break;
1339 			}
1340 			ioc.cmd 	= (uint_t)ioc32.cmd;
1341 			ioc.port	= (uint_t)ioc32.port;
1342 			ioc.get_size	= (uint_t)ioc32.get_size;
1343 			ioc.buf		= (caddr_t)(uintptr_t)ioc32.buf;
1344 			ioc.bufsiz	= (uint_t)ioc32.bufsiz;
1345 			ioc.misc_arg	= (uint_t)ioc32.misc_arg;
1346 		} else
1347 #endif /* _MULTI_DATAMODEL */
1348 		if (ddi_copyin((void *)arg, (void *)&ioc, sizeof (ioc),
1349 		    mode) != 0) {
1350 			return (EFAULT);
1351 		}
1352 
1353 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
1354 		    "sata_hba_ioctl: DEVCTL_AP_CONTROL "
1355 		    "cmd 0x%x, port 0x%x", ioc.cmd, ioc.port);
1356 
1357 		/*
1358 		 * To avoid BE/LE and 32/64 issues, a get_size always returns
1359 		 * a 32-bit number.
1360 		 */
1361 		if (ioc.get_size != 0 && ioc.bufsiz != (sizeof (uint32_t))) {
1362 			return (EINVAL);
1363 		}
1364 		/* validate address */
1365 		cport = SCSI_TO_SATA_CPORT(ioc.port);
1366 		pmport = SCSI_TO_SATA_PMPORT(ioc.port);
1367 		qual = SCSI_TO_SATA_ADDR_QUAL(ioc.port);
1368 
1369 		/* Override address qualifier - handle cport only for now */
1370 		qual = SATA_ADDR_CPORT;
1371 
1372 		if (sata_validate_sata_address(sata_hba_inst, cport,
1373 		    pmport, qual) != 0)
1374 			return (EINVAL);
1375 
1376 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
1377 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1378 		    cport_mutex);
1379 		/* Is the port locked by event processing daemon ? */
1380 		if (cportinfo->cport_event_flags & SATA_EVNT_LOCK_PORT_BUSY) {
1381 			/*
1382 			 * Cannot process ioctl request now. Come back later
1383 			 */
1384 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
1385 			    cport_mutex);
1386 			return (EBUSY);
1387 		}
1388 		/* Block event processing for this port */
1389 		cportinfo->cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
1390 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1391 
1392 
1393 		sata_device.satadev_addr.cport = cport;
1394 		sata_device.satadev_addr.pmport = pmport;
1395 		sata_device.satadev_addr.qual = qual;
1396 		sata_device.satadev_rev = SATA_DEVICE_REV;
1397 
1398 		switch (ioc.cmd) {
1399 
1400 		case SATA_CFGA_RESET_PORT:
1401 			/*
1402 			 * There is no protection for configured device.
1403 			 */
1404 			rv = sata_ioctl_reset_port(sata_hba_inst, &sata_device);
1405 			break;
1406 
1407 		case SATA_CFGA_RESET_DEVICE:
1408 			/*
1409 			 * There is no protection for configured device.
1410 			 */
1411 			rv = sata_ioctl_reset_device(sata_hba_inst,
1412 			    &sata_device);
1413 			break;
1414 
1415 		case SATA_CFGA_RESET_ALL:
1416 			/*
1417 			 * There is no protection for configured devices.
1418 			 */
1419 			rv = sata_ioctl_reset_all(sata_hba_inst);
1420 			/*
1421 			 * We return here, because common return is for
1422 			 * a single port operation - we have already unlocked
1423 			 * all ports and no dc handle was allocated.
1424 			 */
1425 			return (rv);
1426 
1427 		case SATA_CFGA_PORT_DEACTIVATE:
1428 			/*
1429 			 * Arbitrarily unconfigure attached device, if any.
1430 			 * Even if the unconfigure fails, proceed with the
1431 			 * port deactivation.
1432 			 */
1433 			rv = sata_ioctl_deactivate(sata_hba_inst, &sata_device);
1434 
1435 			break;
1436 
1437 		case SATA_CFGA_PORT_ACTIVATE:
1438 
1439 			rv = sata_ioctl_activate(sata_hba_inst, &sata_device);
1440 			break;
1441 
1442 		case SATA_CFGA_PORT_SELF_TEST:
1443 
1444 			rv = sata_ioctl_port_self_test(sata_hba_inst,
1445 			    &sata_device);
1446 			break;
1447 
1448 		case SATA_CFGA_GET_DEVICE_PATH:
1449 			if (qual == SATA_ADDR_CPORT)
1450 				sata_device.satadev_addr.qual =
1451 				    SATA_ADDR_DCPORT;
1452 			else
1453 				sata_device.satadev_addr.qual =
1454 				    SATA_ADDR_DPMPORT;
1455 			rv = sata_ioctl_get_device_path(sata_hba_inst,
1456 			    &sata_device, &ioc, mode);
1457 			break;
1458 
1459 		case SATA_CFGA_GET_AP_TYPE:
1460 
1461 			rv = sata_ioctl_get_ap_type(sata_hba_inst,
1462 			    &sata_device, &ioc, mode);
1463 			break;
1464 
1465 		case SATA_CFGA_GET_MODEL_INFO:
1466 
1467 			rv = sata_ioctl_get_model_info(sata_hba_inst,
1468 			    &sata_device, &ioc, mode);
1469 			break;
1470 
1471 		case SATA_CFGA_GET_REVFIRMWARE_INFO:
1472 
1473 			rv = sata_ioctl_get_revfirmware_info(sata_hba_inst,
1474 			    &sata_device, &ioc, mode);
1475 			break;
1476 
1477 		case SATA_CFGA_GET_SERIALNUMBER_INFO:
1478 
1479 			rv = sata_ioctl_get_serialnumber_info(sata_hba_inst,
1480 			    &sata_device, &ioc, mode);
1481 			break;
1482 
1483 		default:
1484 			rv = EINVAL;
1485 			break;
1486 
1487 		} /* End of DEVCTL_AP_CONTROL cmd switch */
1488 
1489 		break;
1490 	}
1491 
1492 	default:
1493 	{
1494 		/*
1495 		 * If we got here, we got an IOCTL that SATA HBA Framework
1496 		 * does not recognize. Pass ioctl to HBA driver, in case
1497 		 * it could process it.
1498 		 */
1499 		sata_hba_tran_t *sata_tran = sata_hba_inst->satahba_tran;
1500 		dev_info_t	*mydip = SATA_DIP(sata_hba_inst);
1501 
1502 		SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1503 		    "IOCTL 0x%2x not supported in SATA framework, "
1504 		    "passthrough to HBA", cmd);
1505 
1506 		if (sata_tran->sata_tran_ioctl == NULL) {
1507 			rv = EINVAL;
1508 			break;
1509 		}
1510 		rval = (*sata_tran->sata_tran_ioctl)(mydip, cmd, arg);
1511 		if (rval != 0) {
1512 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
1513 			    "IOCTL 0x%2x failed in HBA", cmd);
1514 			rv = rval;
1515 		}
1516 		break;
1517 	}
1518 
1519 	} /* End of main IOCTL switch */
1520 
1521 	if (dcp) {
1522 		ndi_dc_freehdl(dcp);
1523 	}
1524 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1525 	cportinfo->cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
1526 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
1527 
1528 	return (rv);
1529 }
1530 
1531 
1532 /*
1533  * Create error retrieval sata packet
1534  *
1535  * A sata packet is allocated and set-up to contain specified error retrieval
1536  * command and appropriate dma-able data buffer.
1537  * No association with any scsi packet is made and no callback routine is
1538  * specified.
1539  *
1540  * Returns a pointer to sata packet upon successfull packet creation.
1541  * Returns NULL, if packet cannot be created.
1542  */
1543 sata_pkt_t *
1544 sata_get_error_retrieval_pkt(dev_info_t *dip, sata_device_t *sata_device,
1545     int pkt_type)
1546 {
1547 	sata_hba_inst_t	*sata_hba_inst;
1548 	sata_pkt_txlate_t *spx;
1549 	sata_pkt_t *spkt;
1550 	sata_drive_info_t *sdinfo;
1551 
1552 	mutex_enter(&sata_mutex);
1553 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
1554 	    sata_hba_inst = sata_hba_inst->satahba_next) {
1555 		if (SATA_DIP(sata_hba_inst) == dip)
1556 			break;
1557 	}
1558 	mutex_exit(&sata_mutex);
1559 	ASSERT(sata_hba_inst != NULL);
1560 
1561 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
1562 	if (sdinfo == NULL) {
1563 		sata_log(sata_hba_inst, CE_WARN,
1564 		    "sata: error recovery request for non-attached device at "
1565 		    "cport %d", sata_device->satadev_addr.cport);
1566 		return (NULL);
1567 	}
1568 
1569 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
1570 	spx->txlt_sata_hba_inst = sata_hba_inst;
1571 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
1572 	spkt = sata_pkt_alloc(spx, NULL);
1573 	if (spkt == NULL) {
1574 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
1575 		return (NULL);
1576 	}
1577 	/* address is needed now */
1578 	spkt->satapkt_device.satadev_addr = sata_device->satadev_addr;
1579 
1580 	switch (pkt_type) {
1581 	case SATA_ERR_RETR_PKT_TYPE_NCQ:
1582 		if (sata_ncq_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS)
1583 			return (spkt);
1584 		break;
1585 
1586 	case SATA_ERR_RETR_PKT_TYPE_ATAPI:
1587 		if (sata_atapi_err_ret_cmd_setup(spx, sdinfo) == SATA_SUCCESS)
1588 			return (spkt);
1589 		break;
1590 
1591 	default:
1592 		break;
1593 	}
1594 
1595 	sata_pkt_free(spx);
1596 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1597 	return (NULL);
1598 
1599 }
1600 
1601 
1602 /*
1603  * Free error retrieval sata packet
1604  *
1605  * Free sata packet and any associated resources allocated previously by
1606  * sata_get_error_retrieval_pkt().
1607  *
1608  * Void return.
1609  */
1610 void
1611 sata_free_error_retrieval_pkt(sata_pkt_t *sata_pkt)
1612 {
1613 	sata_pkt_txlate_t *spx =
1614 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
1615 
1616 	ASSERT(sata_pkt != NULL);
1617 
1618 	sata_free_local_buffer(spx);
1619 	sata_pkt_free(spx);
1620 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
1621 
1622 }
1623 
1624 
1625 /* ****************** SCSA required entry points *********************** */
1626 
1627 /*
1628  * Implementation of scsi tran_tgt_init.
1629  * sata_scsi_tgt_init() initializes scsi_device structure
1630  *
1631  * If successful, DDI_SUCCESS is returned.
1632  * DDI_FAILURE is returned if addressed device does not exist
1633  */
1634 
1635 static int
1636 sata_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1637     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1638 {
1639 #ifndef __lock_lint
1640 	_NOTE(ARGUNUSED(hba_dip))
1641 	_NOTE(ARGUNUSED(tgt_dip))
1642 #endif
1643 	sata_device_t		sata_device;
1644 	sata_drive_info_t	*sdinfo;
1645 	struct sata_id		*sid;
1646 	sata_hba_inst_t		*sata_hba_inst;
1647 	char			model[SATA_ID_MODEL_LEN + 1];
1648 	char			fw[SATA_ID_FW_LEN + 1];
1649 	char			*vid, *pid;
1650 	int			i;
1651 
1652 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
1653 
1654 	/* Validate scsi device address */
1655 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
1656 	    &sata_device) != 0)
1657 		return (DDI_FAILURE);
1658 
1659 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1660 	    sata_device.satadev_addr.cport)));
1661 
1662 	/* sata_device now contains a valid sata address */
1663 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
1664 	if (sdinfo == NULL) {
1665 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1666 		    sata_device.satadev_addr.cport)));
1667 		return (DDI_FAILURE);
1668 	}
1669 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1670 	    sata_device.satadev_addr.cport)));
1671 
1672 	/*
1673 	 * Check if we need to create a legacy devid (i.e cmdk style) for
1674 	 * the target disks.
1675 	 *
1676 	 * HBA devinfo node will have the property "use-cmdk-devid-format"
1677 	 * if we need to create cmdk-style devid for all the disk devices
1678 	 * attached to this controller. This property may have been set
1679 	 * from HBA driver's .conf file or by the HBA driver in its
1680 	 * attach(9F) function.
1681 	 */
1682 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
1683 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
1684 	    "use-cmdk-devid-format", 0) == 1)) {
1685 		/* register a legacy devid for this target node */
1686 		sata_target_devid_register(tgt_dip, sdinfo);
1687 	}
1688 
1689 
1690 	/*
1691 	 * 'Identify Device Data' does not always fit in standard SCSI
1692 	 * INQUIRY data, so establish INQUIRY_* properties with full-form
1693 	 * of information.
1694 	 */
1695 	sid = &sdinfo->satadrv_id;
1696 #ifdef	_LITTLE_ENDIAN
1697 	swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
1698 	swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
1699 #else	/* _LITTLE_ENDIAN */
1700 	bcopy(sid->ai_model, model, SATA_ID_MODEL_LEN);
1701 	bcopy(sid->ai_fw, fw, SATA_ID_FW_LEN);
1702 #endif	/* _LITTLE_ENDIAN */
1703 	model[SATA_ID_MODEL_LEN] = 0;
1704 	fw[SATA_ID_FW_LEN] = 0;
1705 
1706 	/* split model into into vid/pid */
1707 	for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++)
1708 		if ((*pid == ' ') || (*pid == '\t'))
1709 			break;
1710 	if (i < SATA_ID_MODEL_LEN) {
1711 		vid = model;
1712 		*pid++ = 0;		/* terminate vid, establish pid */
1713 	} else {
1714 		vid = NULL;		/* vid will stay "ATA     " */
1715 		pid = model;		/* model is all pid */
1716 	}
1717 
1718 	if (vid)
1719 		(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_VENDOR_ID,
1720 		    vid, strlen(vid));
1721 	if (pid)
1722 		(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_PRODUCT_ID,
1723 		    pid, strlen(pid));
1724 	(void) scsi_hba_prop_update_inqstring(sd, INQUIRY_REVISION_ID,
1725 	    fw, strlen(fw));
1726 
1727 	return (DDI_SUCCESS);
1728 }
1729 
1730 /*
1731  * Implementation of scsi tran_tgt_probe.
1732  * Probe target, by calling default scsi routine scsi_hba_probe()
1733  */
1734 static int
1735 sata_scsi_tgt_probe(struct scsi_device *sd, int (*callback)(void))
1736 {
1737 	sata_hba_inst_t *sata_hba_inst =
1738 	    (sata_hba_inst_t *)(sd->sd_address.a_hba_tran->tran_hba_private);
1739 	int rval;
1740 
1741 	rval = scsi_hba_probe(sd, callback);
1742 
1743 	if (rval == SCSIPROBE_EXISTS) {
1744 		/*
1745 		 * Set property "pm-capable" on the target device node, so that
1746 		 * the target driver will not try to fetch scsi cycle counters
1747 		 * before enabling device power-management.
1748 		 */
1749 		if ((ddi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
1750 		    "pm-capable", 1)) != DDI_PROP_SUCCESS) {
1751 			sata_log(sata_hba_inst, CE_WARN,
1752 			    "SATA device at port %d: "
1753 			    "will not be power-managed ",
1754 			    SCSI_TO_SATA_CPORT(sd->sd_address.a_target));
1755 			SATA_LOG_D((sata_hba_inst, CE_WARN,
1756 			    "failure updating pm-capable property"));
1757 		}
1758 	}
1759 	return (rval);
1760 }
1761 
1762 /*
1763  * Implementation of scsi tran_tgt_free.
1764  * Release all resources allocated for scsi_device
1765  */
1766 static void
1767 sata_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1768     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1769 {
1770 #ifndef __lock_lint
1771 	_NOTE(ARGUNUSED(hba_dip))
1772 #endif
1773 	sata_device_t		sata_device;
1774 	sata_drive_info_t	*sdinfo;
1775 	sata_hba_inst_t		*sata_hba_inst;
1776 	ddi_devid_t		devid;
1777 
1778 	sata_hba_inst = (sata_hba_inst_t *)(hba_tran->tran_hba_private);
1779 
1780 	/* Validate scsi device address */
1781 	if (sata_validate_scsi_address(sata_hba_inst, &sd->sd_address,
1782 	    &sata_device) != 0)
1783 		return;
1784 
1785 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1786 	    sata_device.satadev_addr.cport)));
1787 
1788 	/* sata_device now should contain a valid sata address */
1789 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
1790 	if (sdinfo == NULL) {
1791 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1792 		    sata_device.satadev_addr.cport)));
1793 		return;
1794 	}
1795 	/*
1796 	 * We did not allocate any resources in sata_scsi_tgt_init()
1797 	 * other than few properties.
1798 	 * Free them.
1799 	 */
1800 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1801 	    sata_device.satadev_addr.cport)));
1802 	(void) ndi_prop_remove(DDI_DEV_T_NONE, tgt_dip, "pm-capable");
1803 
1804 	/*
1805 	 * If devid was previously created but not freed up from
1806 	 * sd(7D) driver (i.e during detach(9F)) then do it here.
1807 	 */
1808 	if ((sdinfo->satadrv_type == SATA_DTYPE_ATADISK) &&
1809 	    (ddi_getprop(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
1810 	    "use-cmdk-devid-format", 0) == 1) &&
1811 	    (ddi_devid_get(tgt_dip, &devid) == DDI_SUCCESS)) {
1812 		ddi_devid_unregister(tgt_dip);
1813 		ddi_devid_free(devid);
1814 	}
1815 }
1816 
1817 /*
1818  * Implementation of scsi tran_init_pkt
1819  * Upon successful return, scsi pkt buffer has DMA resources allocated.
1820  *
1821  * It seems that we should always allocate pkt, even if the address is
1822  * for non-existing device - just use some default for dma_attr.
1823  * The reason is that there is no way to communicate this to a caller here.
1824  * Subsequent call to sata_scsi_start may fail appropriately.
1825  * Simply returning NULL does not seem to discourage a target driver...
1826  *
1827  * Returns a pointer to initialized scsi_pkt, or NULL otherwise.
1828  */
1829 static struct scsi_pkt *
1830 sata_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
1831     struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
1832     int (*callback)(caddr_t), caddr_t arg)
1833 {
1834 	sata_hba_inst_t *sata_hba_inst =
1835 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
1836 	dev_info_t *dip = SATA_DIP(sata_hba_inst);
1837 	sata_device_t sata_device;
1838 	sata_drive_info_t *sdinfo;
1839 	sata_pkt_txlate_t *spx;
1840 	ddi_dma_attr_t cur_dma_attr;
1841 	int rval;
1842 	boolean_t new_pkt = TRUE;
1843 
1844 	ASSERT(ap->a_hba_tran->tran_hba_dip == dip);
1845 
1846 	/*
1847 	 * We need to translate the address, even if it could be
1848 	 * a bogus one, for a non-existing device
1849 	 */
1850 	sata_device.satadev_addr.qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
1851 	sata_device.satadev_addr.cport = SCSI_TO_SATA_CPORT(ap->a_target);
1852 	sata_device.satadev_addr.pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
1853 	sata_device.satadev_rev = SATA_DEVICE_REV;
1854 
1855 	if (pkt == NULL) {
1856 		/*
1857 		 * Have to allocate a brand new scsi packet.
1858 		 * We need to operate with auto request sense enabled.
1859 		 */
1860 		pkt = scsi_hba_pkt_alloc(dip, ap, cmdlen,
1861 		    MAX(statuslen, sizeof (struct scsi_arq_status)),
1862 		    tgtlen, sizeof (sata_pkt_txlate_t), callback, arg);
1863 
1864 		if (pkt == NULL)
1865 			return (NULL);
1866 
1867 		/* Fill scsi packet structure */
1868 		pkt->pkt_comp		= (void (*)())NULL;
1869 		pkt->pkt_time		= 0;
1870 		pkt->pkt_resid		= 0;
1871 		pkt->pkt_statistics	= 0;
1872 		pkt->pkt_reason		= 0;
1873 
1874 		/*
1875 		 * pkt_hba_private will point to sata pkt txlate structure
1876 		 */
1877 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
1878 		bzero(spx, sizeof (sata_pkt_txlate_t));
1879 
1880 		spx->txlt_scsi_pkt = pkt;
1881 		spx->txlt_sata_hba_inst = sata_hba_inst;
1882 
1883 		/* Allocate sata_pkt */
1884 		spx->txlt_sata_pkt = sata_pkt_alloc(spx, callback);
1885 		if (spx->txlt_sata_pkt == NULL) {
1886 			/* Could not allocate sata pkt */
1887 			scsi_hba_pkt_free(ap, pkt);
1888 			return (NULL);
1889 		}
1890 		/* Set sata address */
1891 		spx->txlt_sata_pkt->satapkt_device.satadev_addr =
1892 		    sata_device.satadev_addr;
1893 		spx->txlt_sata_pkt->satapkt_device.satadev_rev =
1894 		    sata_device.satadev_rev;
1895 
1896 		if ((bp == NULL) || (bp->b_bcount == 0))
1897 			return (pkt);
1898 
1899 		spx->txlt_total_residue = bp->b_bcount;
1900 	} else {
1901 		new_pkt = FALSE;
1902 		/*
1903 		 * Packet was preallocated/initialized by previous call
1904 		 */
1905 		spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
1906 
1907 		if ((bp == NULL) || (bp->b_bcount == 0)) {
1908 			return (pkt);
1909 		}
1910 		ASSERT(spx->txlt_buf_dma_handle != NULL);
1911 
1912 		/* Pkt is available already: spx->txlt_scsi_pkt == pkt; */
1913 	}
1914 
1915 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
1916 
1917 	/*
1918 	 * We use an adjusted version of the dma_attr, to account
1919 	 * for device addressing limitations.
1920 	 * sata_adjust_dma_attr() will handle sdinfo == NULL which may
1921 	 * happen when a device is not yet configured.
1922 	 */
1923 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
1924 	    sata_device.satadev_addr.cport)));
1925 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
1926 	    &spx->txlt_sata_pkt->satapkt_device);
1927 	/* NULL sdinfo may be passsed to sata_adjust_dma_attr() */
1928 	sata_adjust_dma_attr(sdinfo,
1929 	    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
1930 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
1931 	    sata_device.satadev_addr.cport)));
1932 	/*
1933 	 * Allocate necessary DMA resources for the packet's data buffer
1934 	 * NOTE:
1935 	 * In case of read/write commands, DMA resource allocation here is
1936 	 * based on the premise that the transfer length specified in
1937 	 * the read/write scsi cdb will match exactly DMA resources -
1938 	 * returning correct packet residue is crucial.
1939 	 */
1940 	if ((rval = sata_dma_buf_setup(spx, flags, callback, arg,
1941 	    &cur_dma_attr)) != DDI_SUCCESS) {
1942 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
1943 		sata_pkt_free(spx);
1944 		/*
1945 		 * If a DMA allocation request fails with
1946 		 * DDI_DMA_NOMAPPING, indicate the error by calling
1947 		 * bioerror(9F) with bp and an error code of EFAULT.
1948 		 * If a DMA allocation request fails with
1949 		 * DDI_DMA_TOOBIG, indicate the error by calling
1950 		 * bioerror(9F) with bp and an error code of EINVAL.
1951 		 */
1952 		switch (rval) {
1953 		case DDI_DMA_NORESOURCES:
1954 			bioerror(bp, 0);
1955 			break;
1956 		case DDI_DMA_NOMAPPING:
1957 		case DDI_DMA_BADATTR:
1958 			bioerror(bp, EFAULT);
1959 			break;
1960 		case DDI_DMA_TOOBIG:
1961 		default:
1962 			bioerror(bp, EINVAL);
1963 			break;
1964 		}
1965 		if (new_pkt == TRUE)
1966 			scsi_hba_pkt_free(ap, pkt);
1967 		return (NULL);
1968 	}
1969 	/* Set number of bytes that are not yet accounted for */
1970 	pkt->pkt_resid = spx->txlt_total_residue;
1971 	ASSERT(pkt->pkt_resid >= 0);
1972 
1973 	return (pkt);
1974 }
1975 
1976 /*
1977  * Implementation of scsi tran_start.
1978  * Translate scsi cmd into sata operation and return status.
1979  * ATAPI CDBs are passed to ATAPI devices - the device determines what commands
1980  * are supported.
1981  * For SATA hard disks, supported scsi commands:
1982  * SCMD_INQUIRY
1983  * SCMD_TEST_UNIT_READY
1984  * SCMD_START_STOP
1985  * SCMD_READ_CAPACITY
1986  * SCMD_REQUEST_SENSE
1987  * SCMD_LOG_SENSE_G1
1988  * SCMD_LOG_SELECT_G1
1989  * SCMD_MODE_SENSE	(specific pages)
1990  * SCMD_MODE_SENSE_G1	(specific pages)
1991  * SCMD_MODE_SELECT	(specific pages)
1992  * SCMD_MODE_SELECT_G1	(specific pages)
1993  * SCMD_SYNCHRONIZE_CACHE
1994  * SCMD_SYNCHRONIZE_CACHE_G1
1995  * SCMD_READ
1996  * SCMD_READ_G1
1997  * SCMD_READ_G4
1998  * SCMD_READ_G5
1999  * SCMD_WRITE
2000  * SCMD_WRITE_BUFFER
2001  * SCMD_WRITE_G1
2002  * SCMD_WRITE_G4
2003  * SCMD_WRITE_G5
2004  * SCMD_SEEK		(noop)
2005  * SCMD_SDIAG
2006  *
2007  * All other commands are rejected as unsupported.
2008  *
2009  * Returns:
2010  * TRAN_ACCEPT if command was executed successfully or accepted by HBA driver
2011  * for execution. TRAN_ACCEPT may be returned also if device was removed but
2012  * a callback could be scheduled.
2013  * TRAN_BADPKT if cmd was directed to invalid address.
2014  * TRAN_FATAL_ERROR is command was rejected due to hardware error, including
2015  * some unspecified error. TRAN_FATAL_ERROR may be also returned if a device
2016  * was removed and there was no callback specified in scsi pkt.
2017  * TRAN_BUSY if command could not be executed becasue HBA driver or SATA
2018  * framework was busy performing some other operation(s).
2019  *
2020  */
2021 static int
2022 sata_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
2023 {
2024 	sata_hba_inst_t *sata_hba_inst =
2025 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2026 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2027 	sata_drive_info_t *sdinfo;
2028 	struct buf *bp;
2029 	int cport;
2030 	int rval;
2031 
2032 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2033 	    "sata_scsi_start: cmd 0x%02x\n", pkt->pkt_cdbp[0]);
2034 
2035 	ASSERT(spx != NULL &&
2036 	    spx->txlt_scsi_pkt == pkt && spx->txlt_sata_pkt != NULL);
2037 
2038 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
2039 
2040 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2041 	sdinfo = sata_get_device_info(sata_hba_inst,
2042 	    &spx->txlt_sata_pkt->satapkt_device);
2043 	if (sdinfo == NULL ||
2044 	    SATA_CPORT_INFO(sata_hba_inst, cport)->cport_tgtnode_clean ==
2045 	    B_FALSE) {
2046 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2047 		pkt->pkt_reason = CMD_DEV_GONE;
2048 		/*
2049 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2050 		 * only in callback function (for normal requests) and
2051 		 * in the dump code path.
2052 		 * So, if the callback is available, we need to do
2053 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2054 		 */
2055 		if (pkt->pkt_comp != NULL) {
2056 			/* scsi callback required */
2057 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2058 			    (task_func_t *)pkt->pkt_comp,
2059 			    (void *)pkt, TQ_SLEEP) == NULL)
2060 				/* Scheduling the callback failed */
2061 				return (TRAN_BUSY);
2062 			return (TRAN_ACCEPT);
2063 		}
2064 		/* No callback available */
2065 		return (TRAN_FATAL_ERROR);
2066 	}
2067 
2068 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
2069 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2070 		rval = sata_txlt_atapi(spx);
2071 		SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2072 		    "sata_scsi_start atapi: rval %d\n", rval);
2073 		return (rval);
2074 	}
2075 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
2076 
2077 	/* ATA Disk commands processing starts here */
2078 
2079 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2080 
2081 	switch (pkt->pkt_cdbp[0]) {
2082 
2083 	case SCMD_INQUIRY:
2084 		/* Mapped to identify device */
2085 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2086 			bp_mapin(bp);
2087 		rval = sata_txlt_inquiry(spx);
2088 		break;
2089 
2090 	case SCMD_TEST_UNIT_READY:
2091 		/*
2092 		 * SAT "SATA to ATA Translation" doc specifies translation
2093 		 * to ATA CHECK POWER MODE.
2094 		 */
2095 		rval = sata_txlt_test_unit_ready(spx);
2096 		break;
2097 
2098 	case SCMD_START_STOP:
2099 		/* Mapping depends on the command */
2100 		rval = sata_txlt_start_stop_unit(spx);
2101 		break;
2102 
2103 	case SCMD_READ_CAPACITY:
2104 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2105 			bp_mapin(bp);
2106 		rval = sata_txlt_read_capacity(spx);
2107 		break;
2108 
2109 	case SCMD_REQUEST_SENSE:
2110 		/*
2111 		 * Always No Sense, since we force ARQ
2112 		 */
2113 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2114 			bp_mapin(bp);
2115 		rval = sata_txlt_request_sense(spx);
2116 		break;
2117 
2118 	case SCMD_LOG_SENSE_G1:
2119 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2120 			bp_mapin(bp);
2121 		rval = sata_txlt_log_sense(spx);
2122 		break;
2123 
2124 	case SCMD_LOG_SELECT_G1:
2125 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2126 			bp_mapin(bp);
2127 		rval = sata_txlt_log_select(spx);
2128 		break;
2129 
2130 	case SCMD_MODE_SENSE:
2131 	case SCMD_MODE_SENSE_G1:
2132 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2133 			bp_mapin(bp);
2134 		rval = sata_txlt_mode_sense(spx);
2135 		break;
2136 
2137 
2138 	case SCMD_MODE_SELECT:
2139 	case SCMD_MODE_SELECT_G1:
2140 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2141 			bp_mapin(bp);
2142 		rval = sata_txlt_mode_select(spx);
2143 		break;
2144 
2145 	case SCMD_SYNCHRONIZE_CACHE:
2146 	case SCMD_SYNCHRONIZE_CACHE_G1:
2147 		rval = sata_txlt_synchronize_cache(spx);
2148 		break;
2149 
2150 	case SCMD_READ:
2151 	case SCMD_READ_G1:
2152 	case SCMD_READ_G4:
2153 	case SCMD_READ_G5:
2154 		rval = sata_txlt_read(spx);
2155 		break;
2156 	case SCMD_WRITE_BUFFER:
2157 		if (bp != NULL && (bp->b_flags & (B_PHYS | B_PAGEIO)))
2158 			bp_mapin(bp);
2159 		rval = sata_txlt_write_buffer(spx);
2160 		break;
2161 
2162 	case SCMD_WRITE:
2163 	case SCMD_WRITE_G1:
2164 	case SCMD_WRITE_G4:
2165 	case SCMD_WRITE_G5:
2166 		rval = sata_txlt_write(spx);
2167 		break;
2168 
2169 	case SCMD_SEEK:
2170 		rval = sata_txlt_nodata_cmd_immediate(spx);
2171 		break;
2172 
2173 		/* Other cases will be filed later */
2174 		/* postponed until phase 2 of the development */
2175 	default:
2176 		rval = sata_txlt_invalid_command(spx);
2177 		break;
2178 	}
2179 
2180 	SATADBG1(SATA_DBG_SCSI_IF, sata_hba_inst,
2181 	    "sata_scsi_start: rval %d\n", rval);
2182 
2183 	return (rval);
2184 }
2185 
2186 /*
2187  * Implementation of scsi tran_abort.
2188  * Abort specific pkt or all packets.
2189  *
2190  * Returns 1 if one or more packets were aborted, returns 0 otherwise
2191  *
2192  * May be called from an interrupt level.
2193  */
2194 static int
2195 sata_scsi_abort(struct scsi_address *ap, struct scsi_pkt *scsi_pkt)
2196 {
2197 	sata_hba_inst_t *sata_hba_inst =
2198 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2199 	sata_device_t	sata_device;
2200 	sata_pkt_t	*sata_pkt;
2201 
2202 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2203 	    "sata_scsi_abort: %s at target: 0x%x\n",
2204 	    scsi_pkt == NULL ? "all packets" : "one pkt", ap->a_target);
2205 
2206 	/* Validate address */
2207 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0)
2208 		/* Invalid address */
2209 		return (0);
2210 
2211 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2212 	    sata_device.satadev_addr.cport)));
2213 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2214 		/* invalid address */
2215 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2216 		    sata_device.satadev_addr.cport)));
2217 		return (0);
2218 	}
2219 	if (scsi_pkt == NULL) {
2220 		/*
2221 		 * Abort all packets.
2222 		 * Although we do not have specific packet, we still need
2223 		 * dummy packet structure to pass device address to HBA.
2224 		 * Allocate one, without sleeping. Fail if pkt cannot be
2225 		 * allocated.
2226 		 */
2227 		sata_pkt = kmem_zalloc(sizeof (sata_pkt_t), KM_NOSLEEP);
2228 		if (sata_pkt == NULL) {
2229 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2230 			    sata_device.satadev_addr.cport)));
2231 			SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_pkt_abort: "
2232 			    "could not allocate sata_pkt"));
2233 			return (0);
2234 		}
2235 		sata_pkt->satapkt_rev = SATA_PKT_REV;
2236 		sata_pkt->satapkt_device = sata_device;
2237 		sata_pkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
2238 	} else {
2239 		if (scsi_pkt->pkt_ha_private == NULL) {
2240 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2241 			    sata_device.satadev_addr.cport)));
2242 			return (0); /* Bad scsi pkt */
2243 		}
2244 		/* extract pointer to sata pkt */
2245 		sata_pkt = ((sata_pkt_txlate_t *)scsi_pkt->pkt_ha_private)->
2246 		    txlt_sata_pkt;
2247 	}
2248 
2249 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2250 	    sata_device.satadev_addr.cport)));
2251 	/* Send abort request to HBA */
2252 	if ((*SATA_ABORT_FUNC(sata_hba_inst))
2253 	    (SATA_DIP(sata_hba_inst), sata_pkt,
2254 	    scsi_pkt == NULL ? SATA_ABORT_ALL_PACKETS : SATA_ABORT_PACKET) ==
2255 	    SATA_SUCCESS) {
2256 		if (scsi_pkt == NULL)
2257 			kmem_free(sata_pkt, sizeof (sata_pkt_t));
2258 		/* Success */
2259 		return (1);
2260 	}
2261 	/* Else, something did not go right */
2262 	if (scsi_pkt == NULL)
2263 		kmem_free(sata_pkt, sizeof (sata_pkt_t));
2264 	/* Failure */
2265 	return (0);
2266 }
2267 
2268 
2269 /*
2270  * Implementation of scsi tran_reset.
2271  * RESET_ALL request is translated into port reset.
2272  * RESET_TARGET requests is translated into a device reset,
2273  * RESET_LUN request is accepted only for LUN 0 and translated into
2274  * device reset.
2275  * The target reset should cause all HBA active and queued packets to
2276  * be terminated and returned with pkt reason SATA_PKT_RESET prior to
2277  * the return. HBA should report reset event for the device.
2278  *
2279  * Returns 1 upon success, 0 upon failure.
2280  */
2281 static int
2282 sata_scsi_reset(struct scsi_address *ap, int level)
2283 {
2284 	sata_hba_inst_t	*sata_hba_inst =
2285 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2286 	sata_device_t	sata_device;
2287 	int		val;
2288 
2289 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2290 	    "sata_scsi_reset: level %d target: 0x%x\n",
2291 	    level, ap->a_target);
2292 
2293 	/* Validate address */
2294 	val = sata_validate_scsi_address(sata_hba_inst, ap, &sata_device);
2295 	if (val == -1)
2296 		/* Invalid address */
2297 		return (0);
2298 
2299 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2300 	    sata_device.satadev_addr.cport)));
2301 	if (sata_get_device_info(sata_hba_inst, &sata_device) == NULL) {
2302 		/* invalid address */
2303 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2304 		    sata_device.satadev_addr.cport)));
2305 		return (0);
2306 	}
2307 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2308 	    sata_device.satadev_addr.cport)));
2309 	if (level == RESET_ALL) {
2310 		/* port reset - cport only */
2311 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
2312 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2313 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2314 			return (1);
2315 		else
2316 			return (0);
2317 
2318 	} else if (val == 0 &&
2319 	    (level == RESET_TARGET || level == RESET_LUN)) {
2320 		/* reset device (device attached) */
2321 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
2322 		    (SATA_DIP(sata_hba_inst), &sata_device) == SATA_SUCCESS)
2323 			return (1);
2324 		else
2325 			return (0);
2326 	}
2327 	return (0);
2328 }
2329 
2330 
2331 /*
2332  * Implementation of scsi tran_getcap (get transport/device capabilities).
2333  * Supported capabilities for SATA hard disks:
2334  * auto-rqsense		(always supported)
2335  * tagged-qing		(supported if HBA supports it)
2336  * untagged-qing	(could be supported if disk supports it, but because
2337  *			 caching behavior allowing untagged queuing actually
2338  *			 results in reduced performance.  sd tries to throttle
2339  *			 back to only 3 outstanding commands, which may
2340  *			 work for real SCSI disks, but with read ahead
2341  *			 caching, having more than 1 outstanding command
2342  *			 results in cache thrashing.)
2343  * sector_size
2344  * dma_max
2345  * interconnect-type	(INTERCONNECT_SATA)
2346  *
2347  * Supported capabilities for ATAPI devices (CD/DVD):
2348  * auto-rqsense		(always supported)
2349  * sector_size
2350  * dma_max
2351  * interconnect-type	(INTERCONNECT_SATA)
2352  *
2353  * Request for other capabilities is rejected as unsupported.
2354  *
2355  * Returns supported capability value, or -1 if capability is unsuppported or
2356  * the address is invalid - no device.
2357  */
2358 
2359 static int
2360 sata_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
2361 {
2362 
2363 	sata_hba_inst_t 	*sata_hba_inst =
2364 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2365 	sata_device_t		sata_device;
2366 	sata_drive_info_t	*sdinfo;
2367 	ddi_dma_attr_t		adj_dma_attr;
2368 	int 			rval;
2369 
2370 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2371 	    "sata_scsi_getcap: target: 0x%x, cap: %s\n",
2372 	    ap->a_target, cap);
2373 
2374 	/*
2375 	 * We want to process the capabilities on per port granularity.
2376 	 * So, we are specifically restricting ourselves to whom != 0
2377 	 * to exclude the controller wide handling.
2378 	 */
2379 	if (cap == NULL || whom == 0)
2380 		return (-1);
2381 
2382 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2383 		/* Invalid address */
2384 		return (-1);
2385 	}
2386 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2387 	    sata_device.satadev_addr.cport)));
2388 	if ((sdinfo = sata_get_device_info(sata_hba_inst, &sata_device)) ==
2389 	    NULL) {
2390 		/* invalid address */
2391 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2392 		    sata_device.satadev_addr.cport)));
2393 		return (-1);
2394 	}
2395 
2396 	switch (scsi_hba_lookup_capstr(cap)) {
2397 	case SCSI_CAP_ARQ:
2398 		rval = 1;		/* ARQ supported, turned on */
2399 		break;
2400 
2401 	case SCSI_CAP_SECTOR_SIZE:
2402 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK)
2403 			rval = SATA_DISK_SECTOR_SIZE;	/* fixed size */
2404 		else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD)
2405 			rval = SATA_ATAPI_SECTOR_SIZE;
2406 		else rval = -1;
2407 		break;
2408 
2409 	/*
2410 	 * untagged queuing cause a performance inversion because of
2411 	 * the way sd operates.  Because of this reason we do not
2412 	 * use it when available.
2413 	 */
2414 	case SCSI_CAP_UNTAGGED_QING:
2415 		if (sdinfo->satadrv_features_enabled &
2416 		    SATA_DEV_F_E_UNTAGGED_QING)
2417 			rval = 1;	/* Untagged queuing available */
2418 		else
2419 			rval = -1;	/* Untagged queuing not available */
2420 		break;
2421 
2422 	case SCSI_CAP_TAGGED_QING:
2423 		if ((sdinfo->satadrv_features_enabled &
2424 		    SATA_DEV_F_E_TAGGED_QING) &&
2425 		    (sdinfo->satadrv_max_queue_depth > 1))
2426 			rval = 1;	/* Tagged queuing available */
2427 		else
2428 			rval = -1;	/* Tagged queuing not available */
2429 		break;
2430 
2431 	case SCSI_CAP_DMA_MAX:
2432 		sata_adjust_dma_attr(sdinfo, SATA_DMA_ATTR(sata_hba_inst),
2433 		    &adj_dma_attr);
2434 		rval = (int)adj_dma_attr.dma_attr_maxxfer;
2435 		/* We rely on the fact that dma_attr_maxxfer < 0x80000000 */
2436 		break;
2437 
2438 	case SCSI_CAP_INTERCONNECT_TYPE:
2439 		rval = INTERCONNECT_SATA;	/* SATA interconnect type */
2440 		break;
2441 
2442 	default:
2443 		rval = -1;
2444 		break;
2445 	}
2446 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2447 	    sata_device.satadev_addr.cport)));
2448 	return (rval);
2449 }
2450 
2451 /*
2452  * Implementation of scsi tran_setcap
2453  *
2454  * Only SCSI_CAP_UNTAGGED_QING and  SCSI_CAP_TAGGED_QING are changeable.
2455  *
2456  */
2457 static int
2458 sata_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
2459 {
2460 	sata_hba_inst_t	*sata_hba_inst =
2461 	    (sata_hba_inst_t *)(ap->a_hba_tran->tran_hba_private);
2462 	sata_device_t	sata_device;
2463 	sata_drive_info_t	*sdinfo;
2464 	int		rval;
2465 
2466 	SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
2467 	    "sata_scsi_setcap: target: 0x%x, cap: %s\n", ap->a_target, cap);
2468 
2469 	/*
2470 	 * We want to process the capabilities on per port granularity.
2471 	 * So, we are specifically restricting ourselves to whom != 0
2472 	 * to exclude the controller wide handling.
2473 	 */
2474 	if (cap == NULL || whom == 0) {
2475 		return (-1);
2476 	}
2477 
2478 	if (sata_validate_scsi_address(sata_hba_inst, ap, &sata_device) != 0) {
2479 		/* Invalid address */
2480 		return (-1);
2481 	}
2482 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
2483 	    sata_device.satadev_addr.cport)));
2484 	if ((sdinfo = sata_get_device_info(sata_hba_inst,
2485 	    &sata_device)) == NULL) {
2486 		/* invalid address */
2487 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2488 		    sata_device.satadev_addr.cport)));
2489 		return (-1);
2490 	}
2491 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
2492 	    sata_device.satadev_addr.cport)));
2493 
2494 	switch (scsi_hba_lookup_capstr(cap)) {
2495 	case SCSI_CAP_ARQ:
2496 	case SCSI_CAP_SECTOR_SIZE:
2497 	case SCSI_CAP_DMA_MAX:
2498 	case SCSI_CAP_INTERCONNECT_TYPE:
2499 		rval = 0;
2500 		break;
2501 	case SCSI_CAP_UNTAGGED_QING:
2502 		if (SATA_QDEPTH(sata_hba_inst) > 1) {
2503 			rval = 1;
2504 			if (value == 1) {
2505 				sdinfo->satadrv_features_enabled |=
2506 				    SATA_DEV_F_E_UNTAGGED_QING;
2507 			} else if (value == 0) {
2508 				sdinfo->satadrv_features_enabled &=
2509 				    ~SATA_DEV_F_E_UNTAGGED_QING;
2510 			} else {
2511 				rval = -1;
2512 			}
2513 		} else {
2514 			rval = 0;
2515 		}
2516 		break;
2517 	case SCSI_CAP_TAGGED_QING:
2518 		/* This can TCQ or NCQ */
2519 		if (sata_func_enable & SATA_ENABLE_QUEUING &&
2520 		    ((sdinfo->satadrv_features_support & SATA_DEV_F_TCQ &&
2521 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_QCMD) ||
2522 		    (sata_func_enable & SATA_ENABLE_NCQ &&
2523 		    sdinfo->satadrv_features_support & SATA_DEV_F_NCQ &&
2524 		    SATA_FEATURES(sata_hba_inst) & SATA_CTLF_NCQ)) &&
2525 		    (sdinfo->satadrv_max_queue_depth > 1)) {
2526 			rval = 1;
2527 			if (value == 1) {
2528 				sdinfo->satadrv_features_enabled |=
2529 				    SATA_DEV_F_E_TAGGED_QING;
2530 			} else if (value == 0) {
2531 				sdinfo->satadrv_features_enabled &=
2532 				    ~SATA_DEV_F_E_TAGGED_QING;
2533 			} else {
2534 				rval = -1;
2535 			}
2536 		} else {
2537 			rval = 0;
2538 		}
2539 		break;
2540 	default:
2541 		rval = -1;
2542 		break;
2543 	}
2544 	return (rval);
2545 }
2546 
2547 /*
2548  * Implementations of scsi tran_destroy_pkt.
2549  * Free resources allocated by sata_scsi_init_pkt()
2550  */
2551 static void
2552 sata_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2553 {
2554 	sata_pkt_txlate_t *spx;
2555 
2556 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2557 
2558 	if (spx->txlt_buf_dma_handle != NULL) {
2559 		if (spx->txlt_tmp_buf != NULL)  {
2560 			ASSERT(spx->txlt_tmp_buf_handle != 0);
2561 			/*
2562 			 * Intermediate DMA buffer was allocated.
2563 			 * Free allocated buffer and associated access handle.
2564 			 */
2565 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
2566 			spx->txlt_tmp_buf = NULL;
2567 		}
2568 		/*
2569 		 * Free DMA resources - cookies and handles
2570 		 */
2571 		if (spx->txlt_dma_cookie_list != NULL) {
2572 			if (spx->txlt_dma_cookie_list !=
2573 			    &spx->txlt_dma_cookie) {
2574 				(void) kmem_free(spx->txlt_dma_cookie_list,
2575 				    spx->txlt_dma_cookie_list_len *
2576 				    sizeof (ddi_dma_cookie_t));
2577 				spx->txlt_dma_cookie_list = NULL;
2578 			}
2579 		}
2580 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
2581 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
2582 	}
2583 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
2584 	sata_pkt_free(spx);
2585 
2586 	scsi_hba_pkt_free(ap, pkt);
2587 }
2588 
2589 /*
2590  * Implementation of scsi tran_dmafree.
2591  * Free DMA resources allocated by sata_scsi_init_pkt()
2592  */
2593 
2594 static void
2595 sata_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
2596 {
2597 #ifndef __lock_lint
2598 	_NOTE(ARGUNUSED(ap))
2599 #endif
2600 	sata_pkt_txlate_t *spx;
2601 
2602 	ASSERT(pkt != NULL);
2603 	spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2604 
2605 	if (spx->txlt_buf_dma_handle != NULL) {
2606 		if (spx->txlt_tmp_buf != NULL)  {
2607 			/*
2608 			 * Intermediate DMA buffer was allocated.
2609 			 * Free allocated buffer and associated access handle.
2610 			 */
2611 			ddi_dma_mem_free(&spx->txlt_tmp_buf_handle);
2612 			spx->txlt_tmp_buf = NULL;
2613 		}
2614 		/*
2615 		 * Free DMA resources - cookies and handles
2616 		 */
2617 		/* ASSERT(spx->txlt_dma_cookie_list != NULL); */
2618 		if (spx->txlt_dma_cookie_list != NULL) {
2619 			if (spx->txlt_dma_cookie_list !=
2620 			    &spx->txlt_dma_cookie) {
2621 				(void) kmem_free(spx->txlt_dma_cookie_list,
2622 				    spx->txlt_dma_cookie_list_len *
2623 				    sizeof (ddi_dma_cookie_t));
2624 				spx->txlt_dma_cookie_list = NULL;
2625 			}
2626 		}
2627 		(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
2628 		(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
2629 		spx->txlt_buf_dma_handle = NULL;
2630 	}
2631 }
2632 
2633 /*
2634  * Implementation of scsi tran_sync_pkt.
2635  *
2636  * The assumption below is that pkt is unique - there is no need to check ap
2637  *
2638  * Synchronize DMA buffer and, if the intermediate buffer is used, copy data
2639  * into/from the real buffer.
2640  */
2641 static void
2642 sata_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2643 {
2644 #ifndef __lock_lint
2645 	_NOTE(ARGUNUSED(ap))
2646 #endif
2647 	int rval;
2648 	sata_pkt_txlate_t *spx = (sata_pkt_txlate_t *)pkt->pkt_ha_private;
2649 	struct buf *bp;
2650 	int direction;
2651 
2652 	ASSERT(spx != NULL);
2653 	if (spx->txlt_buf_dma_handle != NULL) {
2654 		direction = spx->txlt_sata_pkt->
2655 		    satapkt_cmd.satacmd_flags.sata_data_direction;
2656 		if (spx->txlt_sata_pkt != NULL &&
2657 		    direction != SATA_DIR_NODATA_XFER) {
2658 			if (spx->txlt_tmp_buf != NULL) {
2659 				/* Intermediate DMA buffer used */
2660 				bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
2661 
2662 				if (direction & SATA_DIR_WRITE) {
2663 					bcopy(bp->b_un.b_addr,
2664 					    spx->txlt_tmp_buf, bp->b_bcount);
2665 				}
2666 			}
2667 			/* Sync the buffer for device or for CPU */
2668 			rval = ddi_dma_sync(spx->txlt_buf_dma_handle,   0, 0,
2669 			    (direction & SATA_DIR_WRITE) ?
2670 			    DDI_DMA_SYNC_FORDEV :  DDI_DMA_SYNC_FORCPU);
2671 			ASSERT(rval == DDI_SUCCESS);
2672 			if (spx->txlt_tmp_buf != NULL &&
2673 			    !(direction & SATA_DIR_WRITE)) {
2674 				/* Intermediate DMA buffer used for read */
2675 				bcopy(spx->txlt_tmp_buf,
2676 				    bp->b_un.b_addr, bp->b_bcount);
2677 			}
2678 
2679 		}
2680 	}
2681 }
2682 
2683 
2684 
2685 /* *******************  SATA - SCSI Translation functions **************** */
2686 /*
2687  * SCSI to SATA pkt and command translation and SATA to SCSI status/error
2688  * translation.
2689  */
2690 
2691 /*
2692  * Checks if a device exists and can be access and translates common
2693  * scsi_pkt data to sata_pkt data.
2694  *
2695  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_CMPLT if device exists and
2696  * sata_pkt was set-up.
2697  * Returns TRAN_ACCEPT and scsi pkt_reason CMD_DEV_GONE if device does not
2698  * exist and pkt_comp callback was scheduled.
2699  * Returns other TRAN_XXXXX values when error occured and command should be
2700  * rejected with the returned TRAN_XXXXX value.
2701  *
2702  * This function should be called with port mutex held.
2703  */
2704 static int
2705 sata_txlt_generic_pkt_info(sata_pkt_txlate_t *spx)
2706 {
2707 	sata_drive_info_t *sdinfo;
2708 	sata_device_t sata_device;
2709 	const struct sata_cmd_flags sata_initial_cmd_flags = {
2710 		SATA_DIR_NODATA_XFER,
2711 		/* all other values to 0/FALSE */
2712 	};
2713 	/*
2714 	 * Pkt_reason has to be set if the pkt_comp callback is invoked,
2715 	 * and that implies TRAN_ACCEPT return value. Any other returned value
2716 	 * indicates that the scsi packet was not accepted (the reason will not
2717 	 * be checked by the scsi traget driver).
2718 	 * To make debugging easier, we set pkt_reason to know value here.
2719 	 * It may be changed later when different completion reason is
2720 	 * determined.
2721 	 */
2722 	spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
2723 
2724 	/* Validate address */
2725 	switch (sata_validate_scsi_address(spx->txlt_sata_hba_inst,
2726 	    &spx->txlt_scsi_pkt->pkt_address, &sata_device)) {
2727 
2728 	case -1:
2729 		/* Invalid address or invalid device type */
2730 		return (TRAN_BADPKT);
2731 	case 1:
2732 		/* valid address but no device - it has disappeared ? */
2733 		spx->txlt_scsi_pkt->pkt_reason = CMD_DEV_GONE;
2734 		/*
2735 		 * The sd target driver is checking CMD_DEV_GONE pkt_reason
2736 		 * only in callback function (for normal requests) and
2737 		 * in the dump code path.
2738 		 * So, if the callback is available, we need to do
2739 		 * the callback rather than returning TRAN_FATAL_ERROR here.
2740 		 */
2741 		if (spx->txlt_scsi_pkt->pkt_comp != NULL) {
2742 			/* scsi callback required */
2743 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2744 			    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2745 			    (void *)spx->txlt_scsi_pkt,
2746 			    TQ_SLEEP) == NULL)
2747 				/* Scheduling the callback failed */
2748 				return (TRAN_BUSY);
2749 
2750 			return (TRAN_ACCEPT);
2751 		}
2752 		return (TRAN_FATAL_ERROR);
2753 	default:
2754 		/* all OK */
2755 		break;
2756 	}
2757 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
2758 	    &spx->txlt_sata_pkt->satapkt_device);
2759 
2760 	/*
2761 	 * If device is in reset condition, reject the packet with
2762 	 * TRAN_BUSY, unless:
2763 	 * 1. system is panicking (dumping)
2764 	 * In such case only one thread is running and there is no way to
2765 	 * process reset.
2766 	 * 2. cfgadm operation is is progress (internal APCTL lock is set)
2767 	 * Some cfgadm operations involve drive commands, so reset condition
2768 	 * needs to be ignored for IOCTL operations.
2769 	 */
2770 	if ((sdinfo->satadrv_event_flags &
2771 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) != 0) {
2772 
2773 		if (!ddi_in_panic() &&
2774 		    ((SATA_CPORT_EVENT_FLAGS(spx->txlt_sata_hba_inst,
2775 		    sata_device.satadev_addr.cport) &
2776 		    SATA_APCTL_LOCK_PORT_BUSY) == 0)) {
2777 			spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
2778 			SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
2779 			    "sata_scsi_start: rejecting command because "
2780 			    "of device reset state\n", NULL);
2781 			return (TRAN_BUSY);
2782 		}
2783 	}
2784 
2785 	/*
2786 	 * Fix the dev_type in the sata_pkt->satapkt_device. It was not set by
2787 	 * sata_scsi_pkt_init() because pkt init had to work also with
2788 	 * non-existing devices.
2789 	 * Now we know that the packet was set-up for a real device, so its
2790 	 * type is known.
2791 	 */
2792 	spx->txlt_sata_pkt->satapkt_device.satadev_type = sdinfo->satadrv_type;
2793 
2794 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags = sata_initial_cmd_flags;
2795 	if ((SATA_CPORT_INFO(spx->txlt_sata_hba_inst,
2796 	    sata_device.satadev_addr.cport)->cport_event_flags &
2797 	    SATA_APCTL_LOCK_PORT_BUSY) != 0) {
2798 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
2799 		    sata_ignore_dev_reset = B_TRUE;
2800 	}
2801 	/*
2802 	 * At this point the generic translation routine determined that the
2803 	 * scsi packet should be accepted. Packet completion reason may be
2804 	 * changed later when a different completion reason is determined.
2805 	 */
2806 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
2807 
2808 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) != 0) {
2809 		/* Synchronous execution */
2810 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH |
2811 		    SATA_OPMODE_POLLING;
2812 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
2813 		    sata_ignore_dev_reset = ddi_in_panic();
2814 	} else {
2815 		/* Asynchronous execution */
2816 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_ASYNCH |
2817 		    SATA_OPMODE_INTERRUPTS;
2818 	}
2819 	/* Convert queuing information */
2820 	if (spx->txlt_scsi_pkt->pkt_flags & FLAG_STAG)
2821 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_stag =
2822 		    B_TRUE;
2823 	else if (spx->txlt_scsi_pkt->pkt_flags &
2824 	    (FLAG_OTAG | FLAG_HTAG | FLAG_HEAD))
2825 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.sata_queue_otag =
2826 		    B_TRUE;
2827 
2828 	/* Always limit pkt time */
2829 	if (spx->txlt_scsi_pkt->pkt_time == 0)
2830 		spx->txlt_sata_pkt->satapkt_time = sata_default_pkt_time;
2831 	else
2832 		/* Pass on scsi_pkt time */
2833 		spx->txlt_sata_pkt->satapkt_time =
2834 		    spx->txlt_scsi_pkt->pkt_time;
2835 
2836 	return (TRAN_ACCEPT);
2837 }
2838 
2839 
2840 /*
2841  * Translate ATA Identify Device data to SCSI Inquiry data.
2842  * This function may be called only for ATA devices.
2843  * This function should not be called for ATAPI devices - they
2844  * respond directly to SCSI Inquiry command.
2845  *
2846  * SATA Identify Device data has to be valid in sata_rive_info.
2847  * Buffer has to accomodate the inquiry length (36 bytes).
2848  *
2849  * This function should be called with a port mutex held.
2850  */
2851 static	void
2852 sata_identdev_to_inquiry(sata_hba_inst_t *sata_hba_inst,
2853     sata_drive_info_t *sdinfo, uint8_t *buf)
2854 {
2855 
2856 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
2857 	struct sata_id *sid = &sdinfo->satadrv_id;
2858 
2859 	/* Start with a nice clean slate */
2860 	bzero((void *)inq, sizeof (struct scsi_inquiry));
2861 
2862 	/*
2863 	 * Rely on the dev_type for setting paripheral qualifier.
2864 	 * Assume that  DTYPE_RODIRECT applies to CD/DVD R/W devices.
2865 	 * It could be that DTYPE_OPTICAL could also qualify in the future.
2866 	 * ATAPI Inquiry may provide more data to the target driver.
2867 	 */
2868 	inq->inq_dtype = sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
2869 	    DTYPE_DIRECT : DTYPE_RODIRECT; /* DTYPE_UNKNOWN; */
2870 
2871 	inq->inq_rmb = sid->ai_config & SATA_REM_MEDIA ? 1 : 0;
2872 	inq->inq_qual = 0;	/* Device type qualifier (obsolete in SCSI3? */
2873 	inq->inq_iso = 0;	/* ISO version */
2874 	inq->inq_ecma = 0;	/* ECMA version */
2875 	inq->inq_ansi = 3;	/* ANSI version - SCSI 3 */
2876 	inq->inq_aenc = 0;	/* Async event notification cap. */
2877 	inq->inq_trmiop = 0;	/* Supports TERMINATE I/O PROC msg - NO */
2878 	inq->inq_normaca = 0;	/* setting NACA bit supported - NO */
2879 	inq->inq_rdf = RDF_SCSI2; /* Response data format- SPC-3 */
2880 	inq->inq_len = 31;	/* Additional length */
2881 	inq->inq_dualp = 0;	/* dual port device - NO */
2882 	inq->inq_reladdr = 0;	/* Supports relative addressing - NO */
2883 	inq->inq_sync = 0;	/* Supports synchronous data xfers - NO */
2884 	inq->inq_linked = 0;	/* Supports linked commands - NO */
2885 				/*
2886 				 * Queuing support - controller has to
2887 				 * support some sort of command queuing.
2888 				 */
2889 	if (SATA_QDEPTH(sata_hba_inst) > 1)
2890 		inq->inq_cmdque = 1; /* Supports command queueing - YES */
2891 	else
2892 		inq->inq_cmdque = 0; /* Supports command queueing - NO */
2893 	inq->inq_sftre = 0;	/* Supports Soft Reset option - NO ??? */
2894 	inq->inq_wbus32 = 0;	/* Supports 32 bit wide data xfers - NO */
2895 	inq->inq_wbus16 = 0;	/* Supports 16 bit wide data xfers - NO */
2896 
2897 #ifdef	_LITTLE_ENDIAN
2898 	/* Swap text fields to match SCSI format */
2899 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
2900 	swab(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
2901 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
2902 		swab(sid->ai_fw, inq->inq_revision, 4);	/* Revision level */
2903 	else
2904 		swab(&sid->ai_fw[4], inq->inq_revision, 4);	/* Rev. level */
2905 #else	/* _LITTLE_ENDIAN */
2906 	bcopy("ATA     ", inq->inq_vid, 8);		/* Vendor ID */
2907 	bcopy(sid->ai_model, inq->inq_pid, 16);		/* Product ID */
2908 	if (strncmp(&sid->ai_fw[4], "    ", 4) == 0)
2909 		bcopy(sid->ai_fw, inq->inq_revision, 4); /* Revision level */
2910 	else
2911 		bcopy(&sid->ai_fw[4], inq->inq_revision, 4); /* Rev. level */
2912 #endif	/* _LITTLE_ENDIAN */
2913 }
2914 
2915 
2916 /*
2917  * Scsi response set up for invalid command (command not supported)
2918  *
2919  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
2920  */
2921 static int
2922 sata_txlt_invalid_command(sata_pkt_txlate_t *spx)
2923 {
2924 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
2925 	struct scsi_extended_sense *sense;
2926 
2927 	scsipkt->pkt_reason = CMD_CMPLT;
2928 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
2929 	    STATE_SENT_CMD | STATE_GOT_STATUS;
2930 
2931 	*scsipkt->pkt_scbp = STATUS_CHECK;
2932 
2933 	sense = sata_arq_sense(spx);
2934 	sense->es_key = KEY_ILLEGAL_REQUEST;
2935 	sense->es_add_code = SD_SCSI_ASC_INVALID_COMMAND_CODE;
2936 
2937 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
2938 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
2939 
2940 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
2941 	    scsipkt->pkt_comp != NULL)
2942 		/* scsi callback required */
2943 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2944 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2945 		    (void *)spx->txlt_scsi_pkt,
2946 		    TQ_SLEEP) == NULL)
2947 			/* Scheduling the callback failed */
2948 			return (TRAN_BUSY);
2949 	return (TRAN_ACCEPT);
2950 }
2951 
2952 /*
2953  * Scsi response setup for
2954  * emulated non-data command that requires no action/return data
2955  *
2956  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
2957  */
2958 static 	int
2959 sata_txlt_nodata_cmd_immediate(sata_pkt_txlate_t *spx)
2960 {
2961 	int rval;
2962 
2963 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
2964 
2965 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
2966 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
2967 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
2968 		return (rval);
2969 	}
2970 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
2971 
2972 	spx->txlt_scsi_pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
2973 	    STATE_SENT_CMD | STATE_GOT_STATUS;
2974 	spx->txlt_scsi_pkt->pkt_reason = CMD_CMPLT;
2975 	*(spx->txlt_scsi_pkt->pkt_scbp) = STATUS_GOOD;
2976 
2977 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
2978 	    "Scsi_pkt completion reason %x\n",
2979 	    spx->txlt_scsi_pkt->pkt_reason);
2980 
2981 	if ((spx->txlt_scsi_pkt->pkt_flags & FLAG_NOINTR) == 0 &&
2982 	    spx->txlt_scsi_pkt->pkt_comp != NULL)
2983 		/* scsi callback required */
2984 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
2985 		    (task_func_t *)spx->txlt_scsi_pkt->pkt_comp,
2986 		    (void *)spx->txlt_scsi_pkt,
2987 		    TQ_SLEEP) == NULL)
2988 			/* Scheduling the callback failed */
2989 			return (TRAN_BUSY);
2990 	return (TRAN_ACCEPT);
2991 }
2992 
2993 
2994 /*
2995  * SATA translate command: Inquiry / Identify Device
2996  * Use cached Identify Device data for now, rather than issuing actual
2997  * Device Identify cmd request. If device is detached and re-attached,
2998  * asynchromous event processing should fetch and refresh Identify Device
2999  * data.
3000  * Two VPD pages are supported now:
3001  * Vital Product Data page
3002  * Unit Serial Number page
3003  *
3004  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3005  */
3006 
3007 #define	EVPD			1	/* Extended Vital Product Data flag */
3008 #define	CMDDT			2	/* Command Support Data - Obsolete */
3009 #define	INQUIRY_SUP_VPD_PAGE	0	/* Supported VDP Pages Page COde */
3010 #define	INQUIRY_USN_PAGE	0x80	/* Unit Serial Number Page Code */
3011 #define	INQUIRY_DEV_IDENTIFICATION_PAGE 0x83 /* Not needed yet */
3012 
3013 static int
3014 sata_txlt_inquiry(sata_pkt_txlate_t *spx)
3015 {
3016 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3017 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3018 	sata_drive_info_t *sdinfo;
3019 	struct scsi_extended_sense *sense;
3020 	int count;
3021 	uint8_t *p;
3022 	int i, j;
3023 	uint8_t page_buf[0xff]; /* Max length */
3024 	int rval;
3025 
3026 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3027 
3028 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
3029 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
3030 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3031 		return (rval);
3032 	}
3033 
3034 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
3035 	    &spx->txlt_sata_pkt->satapkt_device);
3036 
3037 	ASSERT(sdinfo != NULL);
3038 
3039 	scsipkt->pkt_reason = CMD_CMPLT;
3040 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3041 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3042 
3043 	/* Reject not supported request */
3044 	if (scsipkt->pkt_cdbp[1] & CMDDT) { /* No support for this bit */
3045 		*scsipkt->pkt_scbp = STATUS_CHECK;
3046 		sense = sata_arq_sense(spx);
3047 		sense->es_key = KEY_ILLEGAL_REQUEST;
3048 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3049 		goto done;
3050 	}
3051 
3052 	/* Valid Inquiry request */
3053 	*scsipkt->pkt_scbp = STATUS_GOOD;
3054 
3055 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3056 
3057 		/*
3058 		 * Because it is fully emulated command storing data
3059 		 * programatically in the specified buffer, release
3060 		 * preallocated DMA resources before storing data in the buffer,
3061 		 * so no unwanted DMA sync would take place.
3062 		 */
3063 		sata_scsi_dmafree(NULL, scsipkt);
3064 
3065 		if (!(scsipkt->pkt_cdbp[1] & EVPD)) {
3066 			/* Standard Inquiry Data request */
3067 			struct scsi_inquiry inq;
3068 			unsigned int bufsize;
3069 
3070 			sata_identdev_to_inquiry(spx->txlt_sata_hba_inst,
3071 			    sdinfo, (uint8_t *)&inq);
3072 			/* Copy no more than requested */
3073 			count = MIN(bp->b_bcount,
3074 			    sizeof (struct scsi_inquiry));
3075 			bufsize = scsipkt->pkt_cdbp[4];
3076 			bufsize |= scsipkt->pkt_cdbp[3] << 8;
3077 			count = MIN(count, bufsize);
3078 			bcopy(&inq, bp->b_un.b_addr, count);
3079 
3080 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
3081 			scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3082 			    bufsize - count : 0;
3083 		} else {
3084 			/*
3085 			 * peripheral_qualifier = 0;
3086 			 *
3087 			 * We are dealing only with HD and will be
3088 			 * dealing with CD/DVD devices soon
3089 			 */
3090 			uint8_t peripheral_device_type =
3091 			    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
3092 			    DTYPE_DIRECT : DTYPE_RODIRECT;
3093 
3094 			switch ((uint_t)scsipkt->pkt_cdbp[2]) {
3095 			case INQUIRY_SUP_VPD_PAGE:
3096 				/*
3097 				 * Request for suported Vital Product Data
3098 				 * pages - assuming only 2 page codes
3099 				 * supported
3100 				 */
3101 				page_buf[0] = peripheral_device_type;
3102 				page_buf[1] = INQUIRY_SUP_VPD_PAGE;
3103 				page_buf[2] = 0;
3104 				page_buf[3] = 2; /* page length */
3105 				page_buf[4] = INQUIRY_SUP_VPD_PAGE;
3106 				page_buf[5] = INQUIRY_USN_PAGE;
3107 				/* Copy no more than requested */
3108 				count = MIN(bp->b_bcount, 6);
3109 				bcopy(page_buf, bp->b_un.b_addr, count);
3110 				break;
3111 			case INQUIRY_USN_PAGE:
3112 				/*
3113 				 * Request for Unit Serial Number page
3114 				 */
3115 				page_buf[0] = peripheral_device_type;
3116 				page_buf[1] = INQUIRY_USN_PAGE;
3117 				page_buf[2] = 0;
3118 				page_buf[3] = 20; /* remaining page length */
3119 				p = (uint8_t *)(sdinfo->satadrv_id.ai_drvser);
3120 #ifdef	_LITTLE_ENDIAN
3121 				swab(p, &page_buf[4], 20);
3122 #else
3123 				bcopy(p, &page_buf[4], 20);
3124 #endif
3125 				for (i = 0; i < 20; i++) {
3126 					if (page_buf[4 + i] == '\0' ||
3127 					    page_buf[4 + i] == '\040') {
3128 						break;
3129 					}
3130 				}
3131 				/*
3132 				 * 'i' contains string length.
3133 				 *
3134 				 * Least significant character of the serial
3135 				 * number shall appear as the last byte,
3136 				 * according to SBC-3 spec.
3137 				 */
3138 				p = &page_buf[20 + 4 - 1];
3139 				for (j = i; j > 0; j--, p--) {
3140 					*p = *(p - 20 + i);
3141 				}
3142 				p = &page_buf[4];
3143 				for (j = 20 - i; j > 0; j--) {
3144 					*p++ = '\040';
3145 				}
3146 				count = MIN(bp->b_bcount, 24);
3147 				bcopy(page_buf, bp->b_un.b_addr, count);
3148 				break;
3149 
3150 			case INQUIRY_DEV_IDENTIFICATION_PAGE:
3151 				/*
3152 				 * We may want to implement this page, when
3153 				 * identifiers are common for SATA devices
3154 				 * But not now.
3155 				 */
3156 				/*FALLTHROUGH*/
3157 
3158 			default:
3159 				/* Request for unsupported VPD page */
3160 				*scsipkt->pkt_scbp = STATUS_CHECK;
3161 				sense = sata_arq_sense(spx);
3162 				sense->es_key = KEY_ILLEGAL_REQUEST;
3163 				sense->es_add_code =
3164 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3165 				goto done;
3166 			}
3167 		}
3168 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3169 		scsipkt->pkt_resid = scsipkt->pkt_cdbp[4] > count ?
3170 		    scsipkt->pkt_cdbp[4] - count : 0;
3171 	}
3172 done:
3173 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3174 
3175 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3176 	    "Scsi_pkt completion reason %x\n",
3177 	    scsipkt->pkt_reason);
3178 
3179 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3180 	    scsipkt->pkt_comp != NULL) {
3181 		/* scsi callback required */
3182 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3183 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3184 		    TQ_SLEEP) == NULL)
3185 			/* Scheduling the callback failed */
3186 			return (TRAN_BUSY);
3187 	}
3188 	return (TRAN_ACCEPT);
3189 }
3190 
3191 /*
3192  * SATA translate command: Request Sense.
3193  * Emulated command (ATA version for SATA hard disks)
3194  * Always NO SENSE, because any sense data should be reported by ARQ sense.
3195  *
3196  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3197  */
3198 static int
3199 sata_txlt_request_sense(sata_pkt_txlate_t *spx)
3200 {
3201 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3202 	struct scsi_extended_sense sense;
3203 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3204 	int rval;
3205 
3206 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3207 
3208 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
3209 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
3210 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3211 		return (rval);
3212 	}
3213 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3214 
3215 
3216 	scsipkt->pkt_reason = CMD_CMPLT;
3217 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3218 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3219 	*scsipkt->pkt_scbp = STATUS_GOOD;
3220 
3221 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3222 		/*
3223 		 * Because it is fully emulated command storing data
3224 		 * programatically in the specified buffer, release
3225 		 * preallocated DMA resources before storing data in the buffer,
3226 		 * so no unwanted DMA sync would take place.
3227 		 */
3228 		int count = MIN(bp->b_bcount,
3229 		    sizeof (struct scsi_extended_sense));
3230 		sata_scsi_dmafree(NULL, scsipkt);
3231 		bzero(&sense, sizeof (struct scsi_extended_sense));
3232 		sense.es_valid = 0;	/* Valid LBA */
3233 		sense.es_class = 7;	/* Response code 0x70 - current err */
3234 		sense.es_key = KEY_NO_SENSE;
3235 		sense.es_add_len = 6;	/* Additional length */
3236 		/* Copy no more than requested */
3237 		bcopy(&sense, bp->b_un.b_addr, count);
3238 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3239 		scsipkt->pkt_resid = 0;
3240 	}
3241 
3242 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3243 	    "Scsi_pkt completion reason %x\n",
3244 	    scsipkt->pkt_reason);
3245 
3246 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3247 	    scsipkt->pkt_comp != NULL)
3248 		/* scsi callback required */
3249 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3250 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3251 		    TQ_SLEEP) == NULL)
3252 			/* Scheduling the callback failed */
3253 			return (TRAN_BUSY);
3254 	return (TRAN_ACCEPT);
3255 }
3256 
3257 /*
3258  * SATA translate command: Test Unit Ready
3259  * At the moment this is an emulated command (ATA version for SATA hard disks).
3260  * May be translated into Check Power Mode command in the future
3261  *
3262  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3263  */
3264 static int
3265 sata_txlt_test_unit_ready(sata_pkt_txlate_t *spx)
3266 {
3267 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3268 	struct scsi_extended_sense *sense;
3269 	int power_state;
3270 	int rval;
3271 
3272 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3273 
3274 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
3275 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
3276 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3277 		return (rval);
3278 	}
3279 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3280 
3281 	/* At this moment, emulate it rather than execute anything */
3282 	power_state = SATA_PWRMODE_ACTIVE;
3283 
3284 	scsipkt->pkt_reason = CMD_CMPLT;
3285 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3286 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3287 
3288 	switch (power_state) {
3289 	case SATA_PWRMODE_ACTIVE:
3290 	case SATA_PWRMODE_IDLE:
3291 		*scsipkt->pkt_scbp = STATUS_GOOD;
3292 		break;
3293 	default:
3294 		/* PWR mode standby */
3295 		*scsipkt->pkt_scbp = STATUS_CHECK;
3296 		sense = sata_arq_sense(spx);
3297 		sense->es_key = KEY_NOT_READY;
3298 		sense->es_add_code = SD_SCSI_ASC_LU_NOT_READY;
3299 		break;
3300 	}
3301 
3302 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3303 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3304 
3305 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3306 	    scsipkt->pkt_comp != NULL)
3307 		/* scsi callback required */
3308 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3309 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3310 		    TQ_SLEEP) == NULL)
3311 			/* Scheduling the callback failed */
3312 			return (TRAN_BUSY);
3313 
3314 	return (TRAN_ACCEPT);
3315 }
3316 
3317 
3318 /*
3319  * SATA translate command: Start Stop Unit
3320  * Translation depends on a command:
3321  *	Start Unit translated into Idle Immediate
3322  *	Stop Unit translated into Standby Immediate
3323  *	Unload Media / NOT SUPPORTED YET
3324  *	Load Media / NOT SUPPROTED YET
3325  * Power condition bits are ignored, so is Immediate bit
3326  * Requesting synchronous execution.
3327  *
3328  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
3329  * appropriate values in scsi_pkt fields.
3330  */
3331 static int
3332 sata_txlt_start_stop_unit(sata_pkt_txlate_t *spx)
3333 {
3334 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3335 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
3336 	struct scsi_extended_sense *sense;
3337 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
3338 	int cport = SATA_TXLT_CPORT(spx);
3339 	int rval;
3340 	int synch;
3341 
3342 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3343 	    "sata_txlt_start_stop_unit: %d\n", scsipkt->pkt_scbp[4] & 1);
3344 
3345 	mutex_enter(&SATA_CPORT_MUTEX(shi, cport));
3346 
3347 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
3348 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
3349 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3350 		return (rval);
3351 	}
3352 
3353 	if (scsipkt->pkt_cdbp[4] & 2) {
3354 		/* Load/Unload Media - invalid request */
3355 		*scsipkt->pkt_scbp = STATUS_CHECK;
3356 		sense = sata_arq_sense(spx);
3357 		sense->es_key = KEY_ILLEGAL_REQUEST;
3358 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3359 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3360 
3361 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3362 		    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3363 
3364 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3365 		    scsipkt->pkt_comp != NULL)
3366 			/* scsi callback required */
3367 			if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3368 			    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3369 			    TQ_SLEEP) == NULL)
3370 				/* Scheduling the callback failed */
3371 				return (TRAN_BUSY);
3372 
3373 		return (TRAN_ACCEPT);
3374 	}
3375 	scmd->satacmd_addr_type = 0;
3376 	scmd->satacmd_sec_count_lsb = 0;
3377 	scmd->satacmd_lba_low_lsb = 0;
3378 	scmd->satacmd_lba_mid_lsb = 0;
3379 	scmd->satacmd_lba_high_lsb = 0;
3380 	scmd->satacmd_features_reg = 0;
3381 	scmd->satacmd_device_reg = 0;
3382 	scmd->satacmd_status_reg = 0;
3383 	if (scsipkt->pkt_cdbp[4] & 1) {
3384 		/* Start Unit */
3385 		scmd->satacmd_cmd_reg = SATAC_IDLE_IM;
3386 	} else {
3387 		/* Stop Unit */
3388 		scmd->satacmd_cmd_reg = SATAC_STANDBY_IM;
3389 	}
3390 
3391 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
3392 		/* Need to set-up a callback function */
3393 		spx->txlt_sata_pkt->satapkt_comp =
3394 		    sata_txlt_nodata_cmd_completion;
3395 		synch = FALSE;
3396 	} else {
3397 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
3398 		synch = TRUE;
3399 	}
3400 
3401 	/* Transfer command to HBA */
3402 	if (sata_hba_start(spx, &rval) != 0) {
3403 		/* Pkt not accepted for execution */
3404 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3405 		return (rval);
3406 	}
3407 
3408 	/*
3409 	 * If execution is non-synchronous,
3410 	 * a callback function will handle potential errors, translate
3411 	 * the response and will do a callback to a target driver.
3412 	 * If it was synchronous, check execution status using the same
3413 	 * framework callback.
3414 	 */
3415 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
3416 	if (synch) {
3417 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3418 		    "synchronous execution status %x\n",
3419 		    spx->txlt_sata_pkt->satapkt_reason);
3420 
3421 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
3422 	}
3423 	return (TRAN_ACCEPT);
3424 
3425 }
3426 
3427 
3428 /*
3429  * SATA translate command:  Read Capacity.
3430  * Emulated command for SATA disks.
3431  * Capacity is retrieved from cached Idenifty Device data.
3432  * Identify Device data shows effective disk capacity, not the native
3433  * capacity, which may be limitted by Set Max Address command.
3434  * This is ATA version for SATA hard disks.
3435  *
3436  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3437  */
3438 static int
3439 sata_txlt_read_capacity(sata_pkt_txlate_t *spx)
3440 {
3441 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3442 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3443 	sata_drive_info_t *sdinfo;
3444 	uint64_t val;
3445 	uchar_t *rbuf;
3446 	int rval;
3447 
3448 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3449 	    "sata_txlt_read_capacity: ", NULL);
3450 
3451 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3452 
3453 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
3454 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
3455 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3456 		return (rval);
3457 	}
3458 
3459 	scsipkt->pkt_reason = CMD_CMPLT;
3460 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3461 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3462 	*scsipkt->pkt_scbp = STATUS_GOOD;
3463 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3464 		/*
3465 		 * Because it is fully emulated command storing data
3466 		 * programatically in the specified buffer, release
3467 		 * preallocated DMA resources before storing data in the buffer,
3468 		 * so no unwanted DMA sync would take place.
3469 		 */
3470 		sata_scsi_dmafree(NULL, scsipkt);
3471 
3472 		sdinfo = sata_get_device_info(
3473 		    spx->txlt_sata_hba_inst,
3474 		    &spx->txlt_sata_pkt->satapkt_device);
3475 		/* Last logical block address */
3476 		val = sdinfo->satadrv_capacity - 1;
3477 		rbuf = (uchar_t *)bp->b_un.b_addr;
3478 		/* Need to swap endians to match scsi format */
3479 		rbuf[0] = (val >> 24) & 0xff;
3480 		rbuf[1] = (val >> 16) & 0xff;
3481 		rbuf[2] = (val >> 8) & 0xff;
3482 		rbuf[3] = val & 0xff;
3483 		/* block size - always 512 bytes, for now */
3484 		rbuf[4] = 0;
3485 		rbuf[5] = 0;
3486 		rbuf[6] = 0x02;
3487 		rbuf[7] = 0;
3488 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3489 		scsipkt->pkt_resid = 0;
3490 
3491 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst, "%d\n",
3492 		    sdinfo->satadrv_capacity -1);
3493 	}
3494 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3495 	/*
3496 	 * If a callback was requested, do it now.
3497 	 */
3498 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3499 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3500 
3501 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3502 	    scsipkt->pkt_comp != NULL)
3503 		/* scsi callback required */
3504 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3505 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3506 		    TQ_SLEEP) == NULL)
3507 			/* Scheduling the callback failed */
3508 			return (TRAN_BUSY);
3509 
3510 	return (TRAN_ACCEPT);
3511 }
3512 
3513 /*
3514  * SATA translate command: Mode Sense.
3515  * Translated into appropriate SATA command or emulated.
3516  * Saved Values Page Control (03) are not supported.
3517  *
3518  * NOTE: only caching mode sense page is currently implemented.
3519  *
3520  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3521  */
3522 
3523 static int
3524 sata_txlt_mode_sense(sata_pkt_txlate_t *spx)
3525 {
3526 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
3527 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3528 	sata_drive_info_t *sdinfo;
3529 	sata_id_t *sata_id;
3530 	struct scsi_extended_sense *sense;
3531 	int 		len, bdlen, count, alc_len;
3532 	int		pc;	/* Page Control code */
3533 	uint8_t		*buf;	/* mode sense buffer */
3534 	int		rval;
3535 
3536 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3537 	    "sata_txlt_mode_sense, pc %x page code 0x%02x\n",
3538 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
3539 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
3540 
3541 	buf = kmem_zalloc(1024, KM_SLEEP);
3542 
3543 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3544 
3545 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
3546 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
3547 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3548 		kmem_free(buf, 1024);
3549 		return (rval);
3550 	}
3551 
3552 	scsipkt->pkt_reason = CMD_CMPLT;
3553 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3554 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3555 
3556 	pc = scsipkt->pkt_cdbp[2] >> 6;
3557 
3558 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
3559 		/*
3560 		 * Because it is fully emulated command storing data
3561 		 * programatically in the specified buffer, release
3562 		 * preallocated DMA resources before storing data in the buffer,
3563 		 * so no unwanted DMA sync would take place.
3564 		 */
3565 		sata_scsi_dmafree(NULL, scsipkt);
3566 
3567 		len = 0;
3568 		bdlen = 0;
3569 		if (!(scsipkt->pkt_cdbp[1] & 8)) {
3570 			if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE_G1 &&
3571 			    (scsipkt->pkt_cdbp[0] & 0x10))
3572 				bdlen = 16;
3573 			else
3574 				bdlen = 8;
3575 		}
3576 		/* Build mode parameter header */
3577 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3578 			/* 4-byte mode parameter header */
3579 			buf[len++] = 0;   	/* mode data length */
3580 			buf[len++] = 0;		/* medium type */
3581 			buf[len++] = 0;		/* dev-specific param */
3582 			buf[len++] = bdlen;	/* Block Descriptor length */
3583 		} else {
3584 			/* 8-byte mode parameter header */
3585 			buf[len++] = 0;		/* mode data length */
3586 			buf[len++] = 0;
3587 			buf[len++] = 0;		/* medium type */
3588 			buf[len++] = 0;		/* dev-specific param */
3589 			if (bdlen == 16)
3590 				buf[len++] = 1;	/* long lba descriptor */
3591 			else
3592 				buf[len++] = 0;
3593 			buf[len++] = 0;
3594 			buf[len++] = 0;		/* Block Descriptor length */
3595 			buf[len++] = bdlen;
3596 		}
3597 
3598 		sdinfo = sata_get_device_info(
3599 		    spx->txlt_sata_hba_inst,
3600 		    &spx->txlt_sata_pkt->satapkt_device);
3601 
3602 		/* Build block descriptor only if not disabled (DBD) */
3603 		if ((scsipkt->pkt_cdbp[1] & 0x08) == 0) {
3604 			/* Block descriptor - direct-access device format */
3605 			if (bdlen == 8) {
3606 				/* build regular block descriptor */
3607 				buf[len++] =
3608 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
3609 				buf[len++] =
3610 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
3611 				buf[len++] =
3612 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
3613 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
3614 				buf[len++] = 0; /* density code */
3615 				buf[len++] = 0;
3616 				if (sdinfo->satadrv_type ==
3617 				    SATA_DTYPE_ATADISK)
3618 					buf[len++] = 2;
3619 				else
3620 					/* ATAPI */
3621 					buf[len++] = 8;
3622 				buf[len++] = 0;
3623 			} else if (bdlen == 16) {
3624 				/* Long LBA Accepted */
3625 				/* build long lba block descriptor */
3626 #ifndef __lock_lint
3627 				buf[len++] =
3628 				    (sdinfo->satadrv_capacity >> 56) & 0xff;
3629 				buf[len++] =
3630 				    (sdinfo->satadrv_capacity >> 48) & 0xff;
3631 				buf[len++] =
3632 				    (sdinfo->satadrv_capacity >> 40) & 0xff;
3633 				buf[len++] =
3634 				    (sdinfo->satadrv_capacity >> 32) & 0xff;
3635 #endif
3636 				buf[len++] =
3637 				    (sdinfo->satadrv_capacity >> 24) & 0xff;
3638 				buf[len++] =
3639 				    (sdinfo->satadrv_capacity >> 16) & 0xff;
3640 				buf[len++] =
3641 				    (sdinfo->satadrv_capacity >> 8) & 0xff;
3642 				buf[len++] = sdinfo->satadrv_capacity & 0xff;
3643 				buf[len++] = 0;
3644 				buf[len++] = 0; /* density code */
3645 				buf[len++] = 0;
3646 				buf[len++] = 0;
3647 				if (sdinfo->satadrv_type ==
3648 				    SATA_DTYPE_ATADISK)
3649 					buf[len++] = 2;
3650 				else
3651 					/* ATAPI */
3652 					buf[len++] = 8;
3653 				buf[len++] = 0;
3654 			}
3655 		}
3656 
3657 		sata_id = &sdinfo->satadrv_id;
3658 
3659 		/*
3660 		 * Add requested pages.
3661 		 * Page 3 and 4 are obsolete and we are not supporting them.
3662 		 * We deal now with:
3663 		 * caching (read/write cache control).
3664 		 * We should eventually deal with following mode pages:
3665 		 * error recovery  (0x01),
3666 		 * power condition (0x1a),
3667 		 * exception control page (enables SMART) (0x1c),
3668 		 * enclosure management (ses),
3669 		 * protocol-specific port mode (port control).
3670 		 */
3671 		switch (scsipkt->pkt_cdbp[2] & 0x3f) {
3672 		case MODEPAGE_RW_ERRRECOV:
3673 			/* DAD_MODE_ERR_RECOV */
3674 			/* R/W recovery */
3675 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
3676 			break;
3677 		case MODEPAGE_CACHING:
3678 			/* DAD_MODE_CACHE */
3679 			/* Reject not supported request for saved parameters */
3680 			if (pc == 3) {
3681 				*scsipkt->pkt_scbp = STATUS_CHECK;
3682 				sense = sata_arq_sense(spx);
3683 				sense->es_key = KEY_ILLEGAL_REQUEST;
3684 				sense->es_add_code =
3685 				    SD_SCSI_ASC_SAVING_PARAMS_NOT_SUPPORTED;
3686 				goto done;
3687 			}
3688 
3689 			/* caching */
3690 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
3691 			break;
3692 		case MODEPAGE_INFO_EXCPT:
3693 			/* exception cntrl */
3694 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
3695 				len += sata_build_msense_page_1c(sdinfo, pc,
3696 				    buf+len);
3697 			}
3698 			else
3699 				goto err;
3700 			break;
3701 		case MODEPAGE_POWER_COND:
3702 			/* DAD_MODE_POWER_COND */
3703 			/* power condition */
3704 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
3705 			break;
3706 
3707 		case MODEPAGE_ACOUSTIC_MANAG:
3708 			/* acoustic management */
3709 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
3710 			break;
3711 		case MODEPAGE_ALLPAGES:
3712 			/* all pages */
3713 			len += sata_build_msense_page_1(sdinfo, pc, buf+len);
3714 			len += sata_build_msense_page_8(sdinfo, pc, buf+len);
3715 			len += sata_build_msense_page_1a(sdinfo, pc, buf+len);
3716 			if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
3717 				len += sata_build_msense_page_1c(sdinfo, pc,
3718 				    buf+len);
3719 			}
3720 			len += sata_build_msense_page_30(sdinfo, pc, buf+len);
3721 			break;
3722 		default:
3723 		err:
3724 			/* Invalid request */
3725 			*scsipkt->pkt_scbp = STATUS_CHECK;
3726 			sense = sata_arq_sense(spx);
3727 			sense->es_key = KEY_ILLEGAL_REQUEST;
3728 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3729 			goto done;
3730 		}
3731 
3732 		/* fix total mode data length */
3733 		if (spx->txlt_scsi_pkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3734 			/* 4-byte mode parameter header */
3735 			buf[0] = len - 1;   	/* mode data length */
3736 		} else {
3737 			buf[0] = (len -2) >> 8;
3738 			buf[1] = (len -2) & 0xff;
3739 		}
3740 
3741 
3742 		/* Check allocation length */
3743 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SENSE) {
3744 			alc_len = scsipkt->pkt_cdbp[4];
3745 		} else {
3746 			alc_len = scsipkt->pkt_cdbp[7];
3747 			alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
3748 		}
3749 		/*
3750 		 * We do not check for possible parameters truncation
3751 		 * (alc_len < len) assuming that the target driver works
3752 		 * correctly. Just avoiding overrun.
3753 		 * Copy no more than requested and possible, buffer-wise.
3754 		 */
3755 		count = MIN(alc_len, len);
3756 		count = MIN(bp->b_bcount, count);
3757 		bcopy(buf, bp->b_un.b_addr, count);
3758 
3759 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3760 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
3761 	}
3762 	*scsipkt->pkt_scbp = STATUS_GOOD;
3763 done:
3764 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3765 	(void) kmem_free(buf, 1024);
3766 
3767 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3768 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
3769 
3770 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3771 	    scsipkt->pkt_comp != NULL)
3772 		/* scsi callback required */
3773 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
3774 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
3775 		    TQ_SLEEP) == NULL)
3776 			/* Scheduling the callback failed */
3777 			return (TRAN_BUSY);
3778 
3779 	return (TRAN_ACCEPT);
3780 }
3781 
3782 
3783 /*
3784  * SATA translate command: Mode Select.
3785  * Translated into appropriate SATA command or emulated.
3786  * Saving parameters is not supported.
3787  * Changing device capacity is not supported (although theoretically
3788  * possible by executing SET FEATURES/SET MAX ADDRESS)
3789  *
3790  * Assumption is that the target driver is working correctly.
3791  *
3792  * More than one SATA command may be executed to perform operations specified
3793  * by mode select pages. The first error terminates further execution.
3794  * Operations performed successully are not backed-up in such case.
3795  *
3796  * NOTE: only caching mode select page is implemented.
3797  * Caching setup is remembered so it could be re-stored in case of
3798  * an unexpected device reset.
3799  *
3800  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
3801  */
3802 
3803 static int
3804 sata_txlt_mode_select(sata_pkt_txlate_t *spx)
3805 {
3806 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
3807 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
3808 	struct scsi_extended_sense *sense;
3809 	int len, pagelen, count, pllen;
3810 	uint8_t *buf;	/* mode select buffer */
3811 	int rval, stat;
3812 	uint_t nointr_flag;
3813 	int dmod = 0;
3814 
3815 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
3816 	    "sata_txlt_mode_select, pc %x page code 0x%02x\n",
3817 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
3818 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
3819 
3820 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
3821 
3822 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
3823 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
3824 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3825 		return (rval);
3826 	}
3827 
3828 	rval = TRAN_ACCEPT;
3829 
3830 	scsipkt->pkt_reason = CMD_CMPLT;
3831 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
3832 	    STATE_SENT_CMD | STATE_GOT_STATUS;
3833 
3834 	/* Reject not supported request */
3835 	if (! (scsipkt->pkt_cdbp[1] & 0x10)) { /* No support for PF bit = 0 */
3836 		*scsipkt->pkt_scbp = STATUS_CHECK;
3837 		sense = sata_arq_sense(spx);
3838 		sense->es_key = KEY_ILLEGAL_REQUEST;
3839 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3840 		goto done;
3841 	}
3842 
3843 	if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
3844 		pllen = scsipkt->pkt_cdbp[4];
3845 	} else {
3846 		pllen = scsipkt->pkt_cdbp[7];
3847 		pllen = (pllen << 8) | scsipkt->pkt_cdbp[7];
3848 	}
3849 
3850 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
3851 
3852 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount && pllen != 0) {
3853 		buf = (uint8_t *)bp->b_un.b_addr;
3854 		count = MIN(bp->b_bcount, pllen);
3855 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
3856 		scsipkt->pkt_resid = 0;
3857 		pllen = count;
3858 
3859 		/*
3860 		 * Check the header to skip the block descriptor(s) - we
3861 		 * do not support setting device capacity.
3862 		 * Existing macros do not recognize long LBA dscriptor,
3863 		 * hence manual calculation.
3864 		 */
3865 		if (scsipkt->pkt_cdbp[0] == SCMD_MODE_SELECT) {
3866 			/* 6-bytes CMD, 4 bytes header */
3867 			if (count <= 4)
3868 				goto done;		/* header only */
3869 			len = buf[3] + 4;
3870 		} else {
3871 			/* 10-bytes CMD, 8 bytes header */
3872 			if (count <= 8)
3873 				goto done;		/* header only */
3874 			len = buf[6];
3875 			len = (len << 8) + buf[7] + 8;
3876 		}
3877 		if (len >= count)
3878 			goto done;	/* header + descriptor(s) only */
3879 
3880 		pllen -= len;		/* remaining data length */
3881 
3882 		/*
3883 		 * We may be executing SATA command and want to execute it
3884 		 * in SYNCH mode, regardless of scsi_pkt setting.
3885 		 * Save scsi_pkt setting and indicate SYNCH mode
3886 		 */
3887 		nointr_flag = scsipkt->pkt_flags & FLAG_NOINTR;
3888 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
3889 		    scsipkt->pkt_comp != NULL) {
3890 			scsipkt->pkt_flags |= FLAG_NOINTR;
3891 		}
3892 		spx->txlt_sata_pkt->satapkt_op_mode = SATA_OPMODE_SYNCH;
3893 
3894 		/*
3895 		 * len is now the offset to a first mode select page
3896 		 * Process all pages
3897 		 */
3898 		while (pllen > 0) {
3899 			switch ((int)buf[len]) {
3900 			case MODEPAGE_CACHING:
3901 				/* No support for SP (saving) */
3902 				if (scsipkt->pkt_cdbp[1] & 0x01) {
3903 					*scsipkt->pkt_scbp = STATUS_CHECK;
3904 					sense = sata_arq_sense(spx);
3905 					sense->es_key = KEY_ILLEGAL_REQUEST;
3906 					sense->es_add_code =
3907 					    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
3908 					goto done;
3909 				}
3910 				stat = sata_mode_select_page_8(spx,
3911 				    (struct mode_cache_scsi3 *)&buf[len],
3912 				    pllen, &pagelen, &rval, &dmod);
3913 				/*
3914 				 * The pagelen value indicates the number of
3915 				 * parameter bytes already processed.
3916 				 * The rval is the return value from
3917 				 * sata_tran_start().
3918 				 * The stat indicates the overall status of
3919 				 * the operation(s).
3920 				 */
3921 				if (stat != SATA_SUCCESS)
3922 					/*
3923 					 * Page processing did not succeed -
3924 					 * all error info is already set-up,
3925 					 * just return
3926 					 */
3927 					pllen = 0; /* this breaks the loop */
3928 				else {
3929 					len += pagelen;
3930 					pllen -= pagelen;
3931 				}
3932 				break;
3933 
3934 			case MODEPAGE_INFO_EXCPT:
3935 				stat = sata_mode_select_page_1c(spx,
3936 				    (struct mode_info_excpt_page *)&buf[len],
3937 				    pllen, &pagelen, &rval, &dmod);
3938 				/*
3939 				 * The pagelen value indicates the number of
3940 				 * parameter bytes already processed.
3941 				 * The rval is the return value from
3942 				 * sata_tran_start().
3943 				 * The stat indicates the overall status of
3944 				 * the operation(s).
3945 				 */
3946 				if (stat != SATA_SUCCESS)
3947 					/*
3948 					 * Page processing did not succeed -
3949 					 * all error info is already set-up,
3950 					 * just return
3951 					 */
3952 					pllen = 0; /* this breaks the loop */
3953 				else {
3954 					len += pagelen;
3955 					pllen -= pagelen;
3956 				}
3957 				break;
3958 
3959 			case MODEPAGE_ACOUSTIC_MANAG:
3960 				stat = sata_mode_select_page_30(spx,
3961 				    (struct mode_acoustic_management *)
3962 				    &buf[len], pllen, &pagelen, &rval, &dmod);
3963 				/*
3964 				 * The pagelen value indicates the number of
3965 				 * parameter bytes already processed.
3966 				 * The rval is the return value from
3967 				 * sata_tran_start().
3968 				 * The stat indicates the overall status of
3969 				 * the operation(s).
3970 				 */
3971 				if (stat != SATA_SUCCESS)
3972 					/*
3973 					 * Page processing did not succeed -
3974 					 * all error info is already set-up,
3975 					 * just return
3976 					 */
3977 					pllen = 0; /* this breaks the loop */
3978 				else {
3979 					len += pagelen;
3980 					pllen -= pagelen;
3981 				}
3982 
3983 				break;
3984 			default:
3985 				*scsipkt->pkt_scbp = STATUS_CHECK;
3986 				sense = sata_arq_sense(spx);
3987 				sense->es_key = KEY_ILLEGAL_REQUEST;
3988 				sense->es_add_code =
3989 				    SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
3990 				goto done;
3991 			}
3992 		}
3993 	}
3994 done:
3995 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
3996 	/*
3997 	 * If device parameters were modified, fetch and store the new
3998 	 * Identify Device data. Since port mutex could have been released
3999 	 * for accessing HBA driver, we need to re-check device existence.
4000 	 */
4001 	if (dmod != 0) {
4002 		sata_drive_info_t new_sdinfo, *sdinfo;
4003 		int rv = 0;
4004 
4005 		/*
4006 		 * Following statement has to be changed if this function is
4007 		 * used for devices other than SATA hard disks.
4008 		 */
4009 		new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
4010 
4011 		new_sdinfo.satadrv_addr =
4012 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr;
4013 		rv = sata_fetch_device_identify_data(spx->txlt_sata_hba_inst,
4014 		    &new_sdinfo);
4015 
4016 		mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4017 		/*
4018 		 * Since port mutex could have been released when
4019 		 * accessing HBA driver, we need to re-check that the
4020 		 * framework still holds the device info structure.
4021 		 */
4022 		sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4023 		    &spx->txlt_sata_pkt->satapkt_device);
4024 		if (sdinfo != NULL) {
4025 			/*
4026 			 * Device still has info structure in the
4027 			 * sata framework. Copy newly fetched info
4028 			 */
4029 			if (rv == 0) {
4030 				sdinfo->satadrv_id = new_sdinfo.satadrv_id;
4031 				sata_save_drive_settings(sdinfo);
4032 			} else {
4033 				/*
4034 				 * Could not fetch new data - invalidate
4035 				 * sata_drive_info. That makes device
4036 				 * unusable.
4037 				 */
4038 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
4039 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
4040 			}
4041 		}
4042 		if (rv != 0 || sdinfo == NULL) {
4043 			/*
4044 			 * This changes the overall mode select completion
4045 			 * reason to a failed one !!!!!
4046 			 */
4047 			*scsipkt->pkt_scbp = STATUS_CHECK;
4048 			sense = sata_arq_sense(spx);
4049 			scsipkt->pkt_reason = CMD_INCOMPLETE;
4050 			rval = TRAN_ACCEPT;
4051 		}
4052 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4053 	}
4054 	/* Restore the scsi pkt flags */
4055 	scsipkt->pkt_flags &= ~FLAG_NOINTR;
4056 	scsipkt->pkt_flags |= nointr_flag;
4057 
4058 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4059 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4060 
4061 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4062 	    scsipkt->pkt_comp != NULL)
4063 		/* scsi callback required */
4064 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4065 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4066 		    TQ_SLEEP) == NULL)
4067 			/* Scheduling the callback failed */
4068 			return (TRAN_BUSY);
4069 
4070 	return (rval);
4071 }
4072 
4073 
4074 
4075 /*
4076  * Translate command: Log Sense
4077  */
4078 static 	int
4079 sata_txlt_log_sense(sata_pkt_txlate_t *spx)
4080 {
4081 	struct scsi_pkt	*scsipkt = spx->txlt_scsi_pkt;
4082 	struct buf	*bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4083 	sata_drive_info_t *sdinfo;
4084 	struct scsi_extended_sense *sense;
4085 	int 		len, count, alc_len;
4086 	int		pc;	/* Page Control code */
4087 	int		page_code;	/* Page code */
4088 	uint8_t		*buf;	/* log sense buffer */
4089 	int		rval;
4090 #define	MAX_LOG_SENSE_PAGE_SIZE	512
4091 
4092 	SATADBG2(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4093 	    "sata_txlt_log_sense, pc 0x%x, page code 0x%x\n",
4094 	    spx->txlt_scsi_pkt->pkt_cdbp[2] >> 6,
4095 	    spx->txlt_scsi_pkt->pkt_cdbp[2] & 0x3f);
4096 
4097 	buf = kmem_zalloc(MAX_LOG_SENSE_PAGE_SIZE, KM_SLEEP);
4098 
4099 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4100 
4101 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
4102 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
4103 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4104 		kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4105 		return (rval);
4106 	}
4107 
4108 	scsipkt->pkt_reason = CMD_CMPLT;
4109 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4110 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4111 
4112 	pc = scsipkt->pkt_cdbp[2] >> 6;
4113 	page_code = scsipkt->pkt_cdbp[2] & 0x3f;
4114 
4115 	/* Reject not supported request for all but cumulative values */
4116 	switch (pc) {
4117 	case PC_CUMULATIVE_VALUES:
4118 		break;
4119 	default:
4120 		*scsipkt->pkt_scbp = STATUS_CHECK;
4121 		sense = sata_arq_sense(spx);
4122 		sense->es_key = KEY_ILLEGAL_REQUEST;
4123 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4124 		goto done;
4125 	}
4126 
4127 	switch (page_code) {
4128 	case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
4129 	case PAGE_CODE_SELF_TEST_RESULTS:
4130 	case PAGE_CODE_INFORMATION_EXCEPTIONS:
4131 	case PAGE_CODE_SMART_READ_DATA:
4132 		break;
4133 	default:
4134 		*scsipkt->pkt_scbp = STATUS_CHECK;
4135 		sense = sata_arq_sense(spx);
4136 		sense->es_key = KEY_ILLEGAL_REQUEST;
4137 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4138 		goto done;
4139 	}
4140 
4141 	if (bp != NULL && bp->b_un.b_addr && bp->b_bcount) {
4142 		/*
4143 		 * Because log sense uses local buffers for data retrieval from
4144 		 * the devices and sets the data programatically in the
4145 		 * original specified buffer, release preallocated DMA
4146 		 * resources before storing data in the original buffer,
4147 		 * so no unwanted DMA sync would take place.
4148 		 */
4149 		sata_id_t *sata_id;
4150 
4151 		sata_scsi_dmafree(NULL, scsipkt);
4152 
4153 		len = 0;
4154 
4155 		/* Build log parameter header */
4156 		buf[len++] = page_code;	/* page code as in the CDB */
4157 		buf[len++] = 0;		/* reserved */
4158 		buf[len++] = 0;		/* Zero out page length for now (MSB) */
4159 		buf[len++] = 0;		/* (LSB) */
4160 
4161 		sdinfo = sata_get_device_info(
4162 		    spx->txlt_sata_hba_inst,
4163 		    &spx->txlt_sata_pkt->satapkt_device);
4164 
4165 
4166 		/*
4167 		 * Add requested pages.
4168 		 */
4169 		switch (page_code) {
4170 		case PAGE_CODE_GET_SUPPORTED_LOG_PAGES:
4171 			len = sata_build_lsense_page_0(sdinfo, buf + len);
4172 			break;
4173 		case PAGE_CODE_SELF_TEST_RESULTS:
4174 			sata_id = &sdinfo->satadrv_id;
4175 			if ((! (sata_id->ai_cmdset84 &
4176 			    SATA_SMART_SELF_TEST_SUPPORTED)) ||
4177 			    (! (sata_id->ai_features87 &
4178 			    SATA_SMART_SELF_TEST_SUPPORTED))) {
4179 				*scsipkt->pkt_scbp = STATUS_CHECK;
4180 				sense = sata_arq_sense(spx);
4181 				sense->es_key = KEY_ILLEGAL_REQUEST;
4182 				sense->es_add_code =
4183 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4184 
4185 				goto done;
4186 			}
4187 			len = sata_build_lsense_page_10(sdinfo, buf + len,
4188 			    spx->txlt_sata_hba_inst);
4189 			break;
4190 		case PAGE_CODE_INFORMATION_EXCEPTIONS:
4191 			sata_id = &sdinfo->satadrv_id;
4192 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
4193 				*scsipkt->pkt_scbp = STATUS_CHECK;
4194 				sense = sata_arq_sense(spx);
4195 				sense->es_key = KEY_ILLEGAL_REQUEST;
4196 				sense->es_add_code =
4197 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4198 
4199 				goto done;
4200 			}
4201 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
4202 				*scsipkt->pkt_scbp = STATUS_CHECK;
4203 				sense = sata_arq_sense(spx);
4204 				sense->es_key = KEY_ABORTED_COMMAND;
4205 				sense->es_add_code =
4206 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
4207 				sense->es_qual_code =
4208 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
4209 
4210 				goto done;
4211 			}
4212 
4213 			len = sata_build_lsense_page_2f(sdinfo, buf + len,
4214 			    spx->txlt_sata_hba_inst);
4215 			break;
4216 		case PAGE_CODE_SMART_READ_DATA:
4217 			sata_id = &sdinfo->satadrv_id;
4218 			if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
4219 				*scsipkt->pkt_scbp = STATUS_CHECK;
4220 				sense = sata_arq_sense(spx);
4221 				sense->es_key = KEY_ILLEGAL_REQUEST;
4222 				sense->es_add_code =
4223 				    SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4224 
4225 				goto done;
4226 			}
4227 			if (! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
4228 				*scsipkt->pkt_scbp = STATUS_CHECK;
4229 				sense = sata_arq_sense(spx);
4230 				sense->es_key = KEY_ABORTED_COMMAND;
4231 				sense->es_add_code =
4232 				    SCSI_ASC_ATA_DEV_FEAT_NOT_ENABLED;
4233 				sense->es_qual_code =
4234 				    SCSI_ASCQ_ATA_DEV_FEAT_NOT_ENABLED;
4235 
4236 				goto done;
4237 			}
4238 
4239 			/* This page doesn't include a page header */
4240 			len = sata_build_lsense_page_30(sdinfo, buf,
4241 			    spx->txlt_sata_hba_inst);
4242 			goto no_header;
4243 		default:
4244 			/* Invalid request */
4245 			*scsipkt->pkt_scbp = STATUS_CHECK;
4246 			sense = sata_arq_sense(spx);
4247 			sense->es_key = KEY_ILLEGAL_REQUEST;
4248 			sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4249 			goto done;
4250 		}
4251 
4252 		/* set parameter log sense data length */
4253 		buf[2] = len >> 8;	/* log sense length (MSB) */
4254 		buf[3] = len & 0xff;	/* log sense length (LSB) */
4255 
4256 		len += SCSI_LOG_PAGE_HDR_LEN;
4257 		ASSERT(len <= MAX_LOG_SENSE_PAGE_SIZE);
4258 
4259 no_header:
4260 		/* Check allocation length */
4261 		alc_len = scsipkt->pkt_cdbp[7];
4262 		alc_len = (len << 8) | scsipkt->pkt_cdbp[8];
4263 
4264 		/*
4265 		 * We do not check for possible parameters truncation
4266 		 * (alc_len < len) assuming that the target driver works
4267 		 * correctly. Just avoiding overrun.
4268 		 * Copy no more than requested and possible, buffer-wise.
4269 		 */
4270 		count = MIN(alc_len, len);
4271 		count = MIN(bp->b_bcount, count);
4272 		bcopy(buf, bp->b_un.b_addr, count);
4273 
4274 		scsipkt->pkt_state |= STATE_XFERRED_DATA;
4275 		scsipkt->pkt_resid = alc_len > count ? alc_len - count : 0;
4276 	}
4277 	*scsipkt->pkt_scbp = STATUS_GOOD;
4278 done:
4279 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4280 	(void) kmem_free(buf, MAX_LOG_SENSE_PAGE_SIZE);
4281 
4282 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4283 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
4284 
4285 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4286 	    scsipkt->pkt_comp != NULL)
4287 		/* scsi callback required */
4288 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4289 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4290 		    TQ_SLEEP) == NULL)
4291 			/* Scheduling the callback failed */
4292 			return (TRAN_BUSY);
4293 
4294 	return (TRAN_ACCEPT);
4295 }
4296 
4297 /*
4298  * Translate command: Log Select
4299  * Not implemented at this time - returns invalid command response.
4300  */
4301 static 	int
4302 sata_txlt_log_select(sata_pkt_txlate_t *spx)
4303 {
4304 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4305 	    "sata_txlt_log_select\n", NULL);
4306 
4307 	return (sata_txlt_invalid_command(spx));
4308 }
4309 
4310 
4311 /*
4312  * Translate command: Read (various types).
4313  * Translated into appropriate type of ATA READ command
4314  * for SATA hard disks.
4315  * Both the device capabilities and requested operation mode are
4316  * considered.
4317  *
4318  * Following scsi cdb fields are ignored:
4319  * rdprotect, dpo, fua, fua_nv, group_number.
4320  *
4321  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
4322  * enable variable sata_func_enable), the capability of the controller and
4323  * capability of a device are checked and if both support queueing, read
4324  * request will be translated to READ_DMA_QUEUEING or READ_DMA_QUEUEING_EXT
4325  * command rather than plain READ_XXX command.
4326  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
4327  * both the controller and device suport such functionality, the read
4328  * request will be translated to READ_FPDMA_QUEUED command.
4329  * In both cases the maximum queue depth is derived as minimum of:
4330  * HBA capability,device capability and sata_max_queue_depth variable setting.
4331  * The value passed to HBA driver is decremented by 1, because only 5 bits are
4332  * used to pass max queue depth value, and the maximum possible queue depth
4333  * is 32.
4334  *
4335  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4336  * appropriate values in scsi_pkt fields.
4337  */
4338 static int
4339 sata_txlt_read(sata_pkt_txlate_t *spx)
4340 {
4341 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4342 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4343 	sata_drive_info_t *sdinfo;
4344 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4345 	int cport = SATA_TXLT_CPORT(spx);
4346 	uint16_t sec_count;
4347 	uint64_t lba;
4348 	int rval;
4349 	int synch;
4350 
4351 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4352 
4353 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
4354 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
4355 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4356 		return (rval);
4357 	}
4358 
4359 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4360 	    &spx->txlt_sata_pkt->satapkt_device);
4361 
4362 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
4363 	/*
4364 	 * Extract LBA and sector count from scsi CDB.
4365 	 */
4366 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
4367 	case SCMD_READ:
4368 		/* 6-byte scsi read cmd : 0x08 */
4369 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
4370 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
4371 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4372 		sec_count = scsipkt->pkt_cdbp[4];
4373 		/* sec_count 0 will be interpreted as 256 by a device */
4374 		break;
4375 	case SCMD_READ_G1:
4376 		/* 10-bytes scsi read command : 0x28 */
4377 		lba = scsipkt->pkt_cdbp[2];
4378 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4379 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4380 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4381 		sec_count = scsipkt->pkt_cdbp[7];
4382 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4383 		break;
4384 	case SCMD_READ_G5:
4385 		/* 12-bytes scsi read command : 0xA8 */
4386 		lba = scsipkt->pkt_cdbp[2];
4387 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4388 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4389 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4390 		sec_count = scsipkt->pkt_cdbp[6];
4391 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
4392 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4393 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
4394 		break;
4395 	case SCMD_READ_G4:
4396 		/* 16-bytes scsi read command : 0x88 */
4397 		lba = scsipkt->pkt_cdbp[2];
4398 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4399 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4400 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4401 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
4402 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
4403 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
4404 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
4405 		sec_count = scsipkt->pkt_cdbp[10];
4406 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
4407 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
4408 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
4409 		break;
4410 	default:
4411 		/* Unsupported command */
4412 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4413 		return (sata_txlt_invalid_command(spx));
4414 	}
4415 
4416 	/*
4417 	 * Check if specified address exceeds device capacity
4418 	 */
4419 	if ((lba >= sdinfo->satadrv_capacity) ||
4420 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
4421 		/* LBA out of range */
4422 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4423 		return (sata_txlt_lba_out_of_range(spx));
4424 	}
4425 
4426 	/*
4427 	 * For zero-length transfer, emulate good completion of the command
4428 	 * (reasons for rejecting the command were already checked).
4429 	 * No DMA resources were allocated.
4430 	 */
4431 	if (spx->txlt_dma_cookie_list == NULL) {
4432 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4433 		return (sata_emul_rw_completion(spx));
4434 	}
4435 
4436 	/*
4437 	 * Build cmd block depending on the device capability and
4438 	 * requested operation mode.
4439 	 * Do not bother with non-dma mode - we are working only with
4440 	 * devices supporting DMA.
4441 	 */
4442 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
4443 	scmd->satacmd_device_reg = SATA_ADH_LBA;
4444 	scmd->satacmd_cmd_reg = SATAC_READ_DMA;
4445 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
4446 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4447 		scmd->satacmd_cmd_reg = SATAC_READ_DMA_EXT;
4448 		scmd->satacmd_sec_count_msb = sec_count >> 8;
4449 #ifndef __lock_lint
4450 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
4451 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
4452 		scmd->satacmd_lba_high_msb = lba >> 40;
4453 #endif
4454 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
4455 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
4456 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
4457 	}
4458 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
4459 	scmd->satacmd_lba_low_lsb = lba & 0xff;
4460 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
4461 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
4462 	scmd->satacmd_features_reg = 0;
4463 	scmd->satacmd_status_reg = 0;
4464 	scmd->satacmd_error_reg = 0;
4465 
4466 	/*
4467 	 * Check if queueing commands should be used and switch
4468 	 * to appropriate command if possible
4469 	 */
4470 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
4471 		boolean_t using_queuing;
4472 
4473 		/* Queuing supported by controller and device? */
4474 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
4475 		    (sdinfo->satadrv_features_support &
4476 		    SATA_DEV_F_NCQ) &&
4477 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4478 		    SATA_CTLF_NCQ)) {
4479 			using_queuing = B_TRUE;
4480 
4481 			/* NCQ supported - use FPDMA READ */
4482 			scmd->satacmd_cmd_reg =
4483 			    SATAC_READ_FPDMA_QUEUED;
4484 			scmd->satacmd_features_reg_ext =
4485 			    scmd->satacmd_sec_count_msb;
4486 			scmd->satacmd_sec_count_msb = 0;
4487 		} else if ((sdinfo->satadrv_features_support &
4488 		    SATA_DEV_F_TCQ) &&
4489 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4490 		    SATA_CTLF_QCMD)) {
4491 			using_queuing = B_TRUE;
4492 
4493 			/* Legacy queueing */
4494 			if (sdinfo->satadrv_features_support &
4495 			    SATA_DEV_F_LBA48) {
4496 				scmd->satacmd_cmd_reg =
4497 				    SATAC_READ_DMA_QUEUED_EXT;
4498 				scmd->satacmd_features_reg_ext =
4499 				    scmd->satacmd_sec_count_msb;
4500 				scmd->satacmd_sec_count_msb = 0;
4501 			} else {
4502 				scmd->satacmd_cmd_reg =
4503 				    SATAC_READ_DMA_QUEUED;
4504 			}
4505 		} else	/* NCQ nor legacy queuing not supported */
4506 			using_queuing = B_FALSE;
4507 
4508 		/*
4509 		 * If queuing, the sector count goes in the features register
4510 		 * and the secount count will contain the tag.
4511 		 */
4512 		if (using_queuing) {
4513 			scmd->satacmd_features_reg =
4514 			    scmd->satacmd_sec_count_lsb;
4515 			scmd->satacmd_sec_count_lsb = 0;
4516 			scmd->satacmd_flags.sata_queued = B_TRUE;
4517 
4518 			/* Set-up maximum queue depth */
4519 			scmd->satacmd_flags.sata_max_queue_depth =
4520 			    sdinfo->satadrv_max_queue_depth - 1;
4521 		} else if (sdinfo->satadrv_features_enabled &
4522 		    SATA_DEV_F_E_UNTAGGED_QING) {
4523 			/*
4524 			 * Although NCQ/TCQ is not enabled, untagged queuing
4525 			 * may be still used.
4526 			 * Set-up the maximum untagged queue depth.
4527 			 * Use controller's queue depth from sata_hba_tran.
4528 			 * SATA HBA drivers may ignore this value and rely on
4529 			 * the internal limits.For drivers that do not
4530 			 * ignore untaged queue depth, limit the value to
4531 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
4532 			 * largest value that can be passed via
4533 			 * satacmd_flags.sata_max_queue_depth.
4534 			 */
4535 			scmd->satacmd_flags.sata_max_queue_depth =
4536 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
4537 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
4538 
4539 		} else {
4540 			scmd->satacmd_flags.sata_max_queue_depth = 0;
4541 		}
4542 	} else
4543 		scmd->satacmd_flags.sata_max_queue_depth = 0;
4544 
4545 	SATADBG3(SATA_DBG_HBA_IF, spx->txlt_sata_hba_inst,
4546 	    "sata_txlt_read cmd 0x%2x, lba %llx, sec count %x\n",
4547 	    scmd->satacmd_cmd_reg, lba, sec_count);
4548 
4549 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4550 		/* Need callback function */
4551 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
4552 		synch = FALSE;
4553 	} else
4554 		synch = TRUE;
4555 
4556 	/* Transfer command to HBA */
4557 	if (sata_hba_start(spx, &rval) != 0) {
4558 		/* Pkt not accepted for execution */
4559 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4560 		return (rval);
4561 	}
4562 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4563 	/*
4564 	 * If execution is non-synchronous,
4565 	 * a callback function will handle potential errors, translate
4566 	 * the response and will do a callback to a target driver.
4567 	 * If it was synchronous, check execution status using the same
4568 	 * framework callback.
4569 	 */
4570 	if (synch) {
4571 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4572 		    "synchronous execution status %x\n",
4573 		    spx->txlt_sata_pkt->satapkt_reason);
4574 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
4575 	}
4576 	return (TRAN_ACCEPT);
4577 }
4578 
4579 
4580 /*
4581  * SATA translate command: Write (various types)
4582  * Translated into appropriate type of ATA WRITE command
4583  * for SATA hard disks.
4584  * Both the device capabilities and requested operation mode are
4585  * considered.
4586  *
4587  * Following scsi cdb fields are ignored:
4588  * rwprotect, dpo, fua, fua_nv, group_number.
4589  *
4590  * If SATA_ENABLE_QUEUING flag is set (in the global SATA HBA framework
4591  * enable variable sata_func_enable), the capability of the controller and
4592  * capability of a device are checked and if both support queueing, write
4593  * request will be translated to WRITE_DMA_QUEUEING or WRITE_DMA_QUEUEING_EXT
4594  * command rather than plain WRITE_XXX command.
4595  * If SATA_ENABLE_NCQ flag is set in addition to SATA_ENABLE_QUEUING flag and
4596  * both the controller and device suport such functionality, the write
4597  * request will be translated to WRITE_FPDMA_QUEUED command.
4598  * In both cases the maximum queue depth is derived as minimum of:
4599  * HBA capability,device capability and sata_max_queue_depth variable setting.
4600  * The value passed to HBA driver is decremented by 1, because only 5 bits are
4601  * used to pass max queue depth value, and the maximum possible queue depth
4602  * is 32.
4603  *
4604  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
4605  * appropriate values in scsi_pkt fields.
4606  */
4607 static int
4608 sata_txlt_write(sata_pkt_txlate_t *spx)
4609 {
4610 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4611 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4612 	sata_drive_info_t *sdinfo;
4613 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4614 	int cport = SATA_TXLT_CPORT(spx);
4615 	uint16_t sec_count;
4616 	uint64_t lba;
4617 	int rval;
4618 	int synch;
4619 
4620 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4621 
4622 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
4623 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
4624 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4625 		return (rval);
4626 	}
4627 
4628 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
4629 	    &spx->txlt_sata_pkt->satapkt_device);
4630 
4631 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4632 	/*
4633 	 * Extract LBA and sector count from scsi CDB
4634 	 */
4635 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
4636 	case SCMD_WRITE:
4637 		/* 6-byte scsi read cmd : 0x0A */
4638 		lba = (scsipkt->pkt_cdbp[1] & 0x1f);
4639 		lba = (lba << 8) | scsipkt->pkt_cdbp[2];
4640 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4641 		sec_count = scsipkt->pkt_cdbp[4];
4642 		/* sec_count 0 will be interpreted as 256 by a device */
4643 		break;
4644 	case SCMD_WRITE_G1:
4645 		/* 10-bytes scsi write command : 0x2A */
4646 		lba = scsipkt->pkt_cdbp[2];
4647 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4648 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4649 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4650 		sec_count = scsipkt->pkt_cdbp[7];
4651 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4652 		break;
4653 	case SCMD_WRITE_G5:
4654 		/* 12-bytes scsi read command : 0xAA */
4655 		lba = scsipkt->pkt_cdbp[2];
4656 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4657 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4658 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4659 		sec_count = scsipkt->pkt_cdbp[6];
4660 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[7];
4661 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[8];
4662 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[9];
4663 		break;
4664 	case SCMD_WRITE_G4:
4665 		/* 16-bytes scsi write command : 0x8A */
4666 		lba = scsipkt->pkt_cdbp[2];
4667 		lba = (lba << 8) | scsipkt->pkt_cdbp[3];
4668 		lba = (lba << 8) | scsipkt->pkt_cdbp[4];
4669 		lba = (lba << 8) | scsipkt->pkt_cdbp[5];
4670 		lba = (lba << 8) | scsipkt->pkt_cdbp[6];
4671 		lba = (lba << 8) | scsipkt->pkt_cdbp[7];
4672 		lba = (lba << 8) | scsipkt->pkt_cdbp[8];
4673 		lba = (lba << 8) | scsipkt->pkt_cdbp[9];
4674 		sec_count = scsipkt->pkt_cdbp[10];
4675 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[11];
4676 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[12];
4677 		sec_count = (sec_count << 8) | scsipkt->pkt_cdbp[13];
4678 		break;
4679 	default:
4680 		/* Unsupported command */
4681 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4682 		return (sata_txlt_invalid_command(spx));
4683 	}
4684 
4685 	/*
4686 	 * Check if specified address and length exceeds device capacity
4687 	 */
4688 	if ((lba >= sdinfo->satadrv_capacity) ||
4689 	    ((lba + sec_count) > sdinfo->satadrv_capacity)) {
4690 		/* LBA out of range */
4691 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4692 		return (sata_txlt_lba_out_of_range(spx));
4693 	}
4694 
4695 	/*
4696 	 * For zero-length transfer, emulate good completion of the command
4697 	 * (reasons for rejecting the command were already checked).
4698 	 * No DMA resources were allocated.
4699 	 */
4700 	if (spx->txlt_dma_cookie_list == NULL) {
4701 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4702 		return (sata_emul_rw_completion(spx));
4703 	}
4704 
4705 	/*
4706 	 * Build cmd block depending on the device capability and
4707 	 * requested operation mode.
4708 	 * Do not bother with non-dma mode- we are working only with
4709 	 * devices supporting DMA.
4710 	 */
4711 	scmd->satacmd_addr_type = ATA_ADDR_LBA;
4712 	scmd->satacmd_device_reg = SATA_ADH_LBA;
4713 	scmd->satacmd_cmd_reg = SATAC_WRITE_DMA;
4714 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
4715 		scmd->satacmd_addr_type = ATA_ADDR_LBA48;
4716 		scmd->satacmd_cmd_reg = SATAC_WRITE_DMA_EXT;
4717 		scmd->satacmd_sec_count_msb = sec_count >> 8;
4718 		scmd->satacmd_lba_low_msb = (lba >> 24) & 0xff;
4719 #ifndef __lock_lint
4720 		scmd->satacmd_lba_mid_msb = (lba >> 32) & 0xff;
4721 		scmd->satacmd_lba_high_msb = lba >> 40;
4722 #endif
4723 	} else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28) {
4724 		scmd->satacmd_addr_type = ATA_ADDR_LBA28;
4725 		scmd->satacmd_device_reg = SATA_ADH_LBA | ((lba >> 24) & 0xf);
4726 	}
4727 	scmd->satacmd_sec_count_lsb = sec_count & 0xff;
4728 	scmd->satacmd_lba_low_lsb = lba & 0xff;
4729 	scmd->satacmd_lba_mid_lsb = (lba >> 8) & 0xff;
4730 	scmd->satacmd_lba_high_lsb = (lba >> 16) & 0xff;
4731 	scmd->satacmd_features_reg = 0;
4732 	scmd->satacmd_status_reg = 0;
4733 	scmd->satacmd_error_reg = 0;
4734 
4735 	/*
4736 	 * Check if queueing commands should be used and switch
4737 	 * to appropriate command if possible
4738 	 */
4739 	if (sata_func_enable & SATA_ENABLE_QUEUING) {
4740 		boolean_t using_queuing;
4741 
4742 		/* Queuing supported by controller and device? */
4743 		if ((sata_func_enable & SATA_ENABLE_NCQ) &&
4744 		    (sdinfo->satadrv_features_support &
4745 		    SATA_DEV_F_NCQ) &&
4746 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4747 		    SATA_CTLF_NCQ)) {
4748 			using_queuing = B_TRUE;
4749 
4750 			/* NCQ supported - use FPDMA WRITE */
4751 			scmd->satacmd_cmd_reg =
4752 			    SATAC_WRITE_FPDMA_QUEUED;
4753 			scmd->satacmd_features_reg_ext =
4754 			    scmd->satacmd_sec_count_msb;
4755 			scmd->satacmd_sec_count_msb = 0;
4756 		} else if ((sdinfo->satadrv_features_support &
4757 		    SATA_DEV_F_TCQ) &&
4758 		    (SATA_FEATURES(spx->txlt_sata_hba_inst) &
4759 		    SATA_CTLF_QCMD)) {
4760 			using_queuing = B_TRUE;
4761 
4762 			/* Legacy queueing */
4763 			if (sdinfo->satadrv_features_support &
4764 			    SATA_DEV_F_LBA48) {
4765 				scmd->satacmd_cmd_reg =
4766 				    SATAC_WRITE_DMA_QUEUED_EXT;
4767 				scmd->satacmd_features_reg_ext =
4768 				    scmd->satacmd_sec_count_msb;
4769 				scmd->satacmd_sec_count_msb = 0;
4770 			} else {
4771 				scmd->satacmd_cmd_reg =
4772 				    SATAC_WRITE_DMA_QUEUED;
4773 			}
4774 		} else	/*  NCQ nor legacy queuing not supported */
4775 			using_queuing = B_FALSE;
4776 
4777 		if (using_queuing) {
4778 			scmd->satacmd_features_reg =
4779 			    scmd->satacmd_sec_count_lsb;
4780 			scmd->satacmd_sec_count_lsb = 0;
4781 			scmd->satacmd_flags.sata_queued = B_TRUE;
4782 			/* Set-up maximum queue depth */
4783 			scmd->satacmd_flags.sata_max_queue_depth =
4784 			    sdinfo->satadrv_max_queue_depth - 1;
4785 		} else if (sdinfo->satadrv_features_enabled &
4786 		    SATA_DEV_F_E_UNTAGGED_QING) {
4787 			/*
4788 			 * Although NCQ/TCQ is not enabled, untagged queuing
4789 			 * may be still used.
4790 			 * Set-up the maximum untagged queue depth.
4791 			 * Use controller's queue depth from sata_hba_tran.
4792 			 * SATA HBA drivers may ignore this value and rely on
4793 			 * the internal limits. For drivera that do not
4794 			 * ignore untaged queue depth, limit the value to
4795 			 * SATA_MAX_QUEUE_DEPTH (32), as this is the
4796 			 * largest value that can be passed via
4797 			 * satacmd_flags.sata_max_queue_depth.
4798 			 */
4799 			scmd->satacmd_flags.sata_max_queue_depth =
4800 			    SATA_QDEPTH(shi) <= SATA_MAX_QUEUE_DEPTH ?
4801 			    SATA_QDEPTH(shi) - 1: SATA_MAX_QUEUE_DEPTH - 1;
4802 
4803 		} else {
4804 			scmd->satacmd_flags.sata_max_queue_depth = 0;
4805 		}
4806 	} else
4807 		scmd->satacmd_flags.sata_max_queue_depth = 0;
4808 
4809 	SATADBG3(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4810 	    "sata_txlt_write cmd 0x%2x, lba %llx, sec count %x\n",
4811 	    scmd->satacmd_cmd_reg, lba, sec_count);
4812 
4813 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
4814 		/* Need callback function */
4815 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_rw_completion;
4816 		synch = FALSE;
4817 	} else
4818 		synch = TRUE;
4819 
4820 	/* Transfer command to HBA */
4821 	if (sata_hba_start(spx, &rval) != 0) {
4822 		/* Pkt not accepted for execution */
4823 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4824 		return (rval);
4825 	}
4826 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4827 
4828 	/*
4829 	 * If execution is non-synchronous,
4830 	 * a callback function will handle potential errors, translate
4831 	 * the response and will do a callback to a target driver.
4832 	 * If it was synchronous, check execution status using the same
4833 	 * framework callback.
4834 	 */
4835 	if (synch) {
4836 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4837 		    "synchronous execution status %x\n",
4838 		    spx->txlt_sata_pkt->satapkt_reason);
4839 		sata_txlt_rw_completion(spx->txlt_sata_pkt);
4840 	}
4841 	return (TRAN_ACCEPT);
4842 }
4843 
4844 
4845 /*
4846  * Implements SCSI SBC WRITE BUFFER command download microcode option
4847  */
4848 static int
4849 sata_txlt_write_buffer(sata_pkt_txlate_t *spx)
4850 {
4851 #define	WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE			4
4852 #define	WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE			5
4853 
4854 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4855 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
4856 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
4857 	struct scsi_extended_sense *sense;
4858 	int rval, mode, sector_count;
4859 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
4860 	int cport = SATA_TXLT_CPORT(spx);
4861 	boolean_t synch;
4862 
4863 	synch = (spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH) != 0;
4864 	mode = scsipkt->pkt_cdbp[1] & 0x1f;
4865 
4866 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4867 	    "sata_txlt_write_buffer, mode 0x%x\n", mode);
4868 
4869 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
4870 
4871 	if ((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) {
4872 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4873 		return (rval);
4874 	}
4875 
4876 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_WRITE;
4877 
4878 	scsipkt->pkt_reason = CMD_CMPLT;
4879 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
4880 	    STATE_SENT_CMD | STATE_GOT_STATUS;
4881 
4882 	/*
4883 	 * The SCSI to ATA translation specification only calls
4884 	 * for WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE.
4885 	 * WB_DOWNLOAD_MICROC_AND_REVERT_MODE is implemented, but
4886 	 * ATA 8 (draft) got rid of download microcode for temp
4887 	 * and it is even optional for ATA 7, so it may be aborted.
4888 	 * WB_DOWNLOAD_MICROCODE_WITH_OFFSET is not implemented as
4889 	 * it is not specified and the buffer offset for SCSI is a 16-bit
4890 	 * value in bytes, but for ATA it is a 16-bit offset in 512 byte
4891 	 * sectors.  Thus the offset really doesn't buy us anything.
4892 	 * If and when ATA 8 is stabilized and the SCSI to ATA specification
4893 	 * is revised, this can be revisisted.
4894 	 */
4895 	/* Reject not supported request */
4896 	switch (mode) {
4897 	case WB_DOWNLOAD_MICROCODE_AND_REVERT_MODE:
4898 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_TEMP;
4899 		break;
4900 	case WB_DOWNLOAD_MICROCODE_AND_SAVE_MODE:
4901 		scmd->satacmd_features_reg = SATA_DOWNLOAD_MCODE_SAVE;
4902 		break;
4903 	default:
4904 		goto bad_param;
4905 	}
4906 
4907 	*scsipkt->pkt_scbp = STATUS_GOOD;	/* Presumed outcome */
4908 
4909 	scmd->satacmd_cmd_reg = SATAC_DOWNLOAD_MICROCODE;
4910 	if ((bp->b_bcount % SATA_DISK_SECTOR_SIZE) != 0)
4911 		goto bad_param;
4912 	sector_count = bp->b_bcount / SATA_DISK_SECTOR_SIZE;
4913 	scmd->satacmd_sec_count_lsb = (uint8_t)sector_count;
4914 	scmd->satacmd_lba_low_lsb = ((uint16_t)sector_count) >> 8;
4915 	scmd->satacmd_lba_mid_lsb = 0;
4916 	scmd->satacmd_lba_high_lsb = 0;
4917 	scmd->satacmd_device_reg = 0;
4918 	spx->txlt_sata_pkt->satapkt_comp =
4919 	    sata_txlt_download_mcode_cmd_completion;
4920 	scmd->satacmd_addr_type = 0;
4921 
4922 	/* Transfer command to HBA */
4923 	if (sata_hba_start(spx, &rval) != 0) {
4924 		/* Pkt not accepted for execution */
4925 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4926 		return (rval);
4927 	}
4928 
4929 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
4930 	/*
4931 	 * If execution is non-synchronous,
4932 	 * a callback function will handle potential errors, translate
4933 	 * the response and will do a callback to a target driver.
4934 	 * If it was synchronous, check execution status using the same
4935 	 * framework callback.
4936 	 */
4937 	if (synch) {
4938 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
4939 		    "synchronous execution\n", NULL);
4940 		/* Calling pre-set completion routine */
4941 		(*spx->txlt_sata_pkt->satapkt_comp)(spx->txlt_sata_pkt);
4942 	}
4943 	return (TRAN_ACCEPT);
4944 
4945 bad_param:
4946 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
4947 	*scsipkt->pkt_scbp = STATUS_CHECK;
4948 	sense = sata_arq_sense(spx);
4949 	sense->es_key = KEY_ILLEGAL_REQUEST;
4950 	sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
4951 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
4952 	    scsipkt->pkt_comp != NULL) {
4953 		/* scsi callback required */
4954 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
4955 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
4956 		    TQ_SLEEP) == 0) {
4957 			/* Scheduling the callback failed */
4958 			rval = TRAN_BUSY;
4959 		}
4960 	}
4961 	return (rval);
4962 }
4963 
4964 
4965 /*
4966  * Retry identify device when command returns SATA_INCOMPLETE_DATA
4967  * after doing a firmware download.
4968  */
4969 static void
4970 sata_retry_identify_device(void *arg)
4971 {
4972 #define	DOWNLOAD_WAIT_TIME_SECS	60
4973 #define	DOWNLOAD_WAIT_INTERVAL_SECS	1
4974 	int rval;
4975 	int retry_cnt;
4976 	sata_pkt_t *sata_pkt = (sata_pkt_t *)arg;
4977 	sata_pkt_txlate_t *spx =
4978 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
4979 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
4980 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
4981 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
4982 	sata_drive_info_t *sdinfo;
4983 
4984 	/*
4985 	 * Before returning good status, probe device.
4986 	 * Device probing will get IDENTIFY DEVICE data, if possible.
4987 	 * The assumption is that the new microcode is applied by the
4988 	 * device. It is a caller responsibility to verify this.
4989 	 */
4990 	for (retry_cnt = 0;
4991 	    retry_cnt < DOWNLOAD_WAIT_TIME_SECS / DOWNLOAD_WAIT_INTERVAL_SECS;
4992 	    retry_cnt++) {
4993 		rval = sata_probe_device(sata_hba_inst, &sata_device);
4994 
4995 		if (rval == SATA_SUCCESS) { /* Set default features */
4996 			sdinfo = sata_get_device_info(sata_hba_inst,
4997 			    &sata_device);
4998 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
4999 			    SATA_SUCCESS) {
5000 				/* retry */
5001 				(void) sata_initialize_device(sata_hba_inst,
5002 				    sdinfo);
5003 			}
5004 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5005 			    scsipkt->pkt_comp != NULL)
5006 				(*scsipkt->pkt_comp)(scsipkt);
5007 			return;
5008 		} else if (rval == SATA_RETRY) {
5009 			delay(drv_usectohz(1000000 *
5010 			    DOWNLOAD_WAIT_INTERVAL_SECS));
5011 			continue;
5012 		} else	/* failed - no reason to retry */
5013 			break;
5014 	}
5015 
5016 	/*
5017 	 * Something went wrong, device probing failed.
5018 	 */
5019 	SATA_LOG_D((sata_hba_inst, CE_WARN,
5020 	    "Cannot probe device after downloading microcode\n"));
5021 
5022 	/* Reset device to force retrying the probe. */
5023 	(void) (*SATA_RESET_DPORT_FUNC(sata_hba_inst))
5024 	    (SATA_DIP(sata_hba_inst), &sata_device);
5025 
5026 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5027 	    scsipkt->pkt_comp != NULL)
5028 		(*scsipkt->pkt_comp)(scsipkt);
5029 }
5030 
5031 /*
5032  * Translate completion status of download microcode command.
5033  * pkt completion_reason is checked to determine the completion status.
5034  * Do scsi callback if necessary (FLAG_NOINTR == 0)
5035  *
5036  * Note: this function may be called also for synchronously executed
5037  * command.
5038  * This function may be used only if scsi_pkt is non-NULL.
5039  */
5040 static void
5041 sata_txlt_download_mcode_cmd_completion(sata_pkt_t *sata_pkt)
5042 {
5043 	sata_pkt_txlate_t *spx =
5044 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
5045 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5046 	struct scsi_extended_sense *sense;
5047 	sata_drive_info_t *sdinfo;
5048 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
5049 	sata_device_t sata_device = spx->txlt_sata_pkt->satapkt_device;
5050 	int rval;
5051 
5052 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5053 	    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
5054 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5055 		scsipkt->pkt_reason = CMD_CMPLT;
5056 
5057 		rval = sata_probe_device(sata_hba_inst, &sata_device);
5058 
5059 		if (rval == SATA_SUCCESS) { /* Set default features */
5060 			sdinfo = sata_get_device_info(sata_hba_inst,
5061 			    &sata_device);
5062 			if (sata_initialize_device(sata_hba_inst, sdinfo) !=
5063 			    SATA_SUCCESS) {
5064 				/* retry */
5065 				(void) sata_initialize_device(sata_hba_inst,
5066 				    sdinfo);
5067 			}
5068 			if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5069 			    scsipkt->pkt_comp != NULL)
5070 				(*scsipkt->pkt_comp)(scsipkt);
5071 		} else {
5072 			(void) ddi_taskq_dispatch(
5073 			    (ddi_taskq_t *)SATA_TXLT_TASKQ(spx),
5074 			    sata_retry_identify_device,
5075 			    (void *)sata_pkt, TQ_NOSLEEP);
5076 		}
5077 
5078 
5079 	} else {
5080 		/* Something went wrong, microcode download command failed */
5081 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5082 		*scsipkt->pkt_scbp = STATUS_CHECK;
5083 		sense = sata_arq_sense(spx);
5084 		switch (sata_pkt->satapkt_reason) {
5085 		case SATA_PKT_PORT_ERROR:
5086 			/*
5087 			 * We have no device data. Assume no data transfered.
5088 			 */
5089 			sense->es_key = KEY_HARDWARE_ERROR;
5090 			break;
5091 
5092 		case SATA_PKT_DEV_ERROR:
5093 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5094 			    SATA_STATUS_ERR) {
5095 				/*
5096 				 * determine dev error reason from error
5097 				 * reg content
5098 				 */
5099 				sata_decode_device_error(spx, sense);
5100 				break;
5101 			}
5102 			/* No extended sense key - no info available */
5103 			break;
5104 
5105 		case SATA_PKT_TIMEOUT:
5106 			/* scsipkt->pkt_reason = CMD_TIMEOUT; */
5107 			scsipkt->pkt_reason = CMD_INCOMPLETE;
5108 			/* No extended sense key ? */
5109 			break;
5110 
5111 		case SATA_PKT_ABORTED:
5112 			scsipkt->pkt_reason = CMD_ABORTED;
5113 			/* No extended sense key ? */
5114 			break;
5115 
5116 		case SATA_PKT_RESET:
5117 			/* pkt aborted by an explicit reset from a host */
5118 			scsipkt->pkt_reason = CMD_RESET;
5119 			break;
5120 
5121 		default:
5122 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5123 			    "sata_txlt_nodata_cmd_completion: "
5124 			    "invalid packet completion reason %d",
5125 			    sata_pkt->satapkt_reason));
5126 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5127 			break;
5128 		}
5129 
5130 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5131 		    "scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5132 
5133 		if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5134 		    scsipkt->pkt_comp != NULL)
5135 			/* scsi callback required */
5136 			(*scsipkt->pkt_comp)(scsipkt);
5137 	}
5138 }
5139 
5140 
5141 
5142 
5143 /*
5144  * Translate command: Synchronize Cache.
5145  * Translates into Flush Cache command for SATA hard disks.
5146  *
5147  * Returns TRAN_ACCEPT or code returned by sata_hba_start() and
5148  * appropriate values in scsi_pkt fields.
5149  */
5150 static 	int
5151 sata_txlt_synchronize_cache(sata_pkt_txlate_t *spx)
5152 {
5153 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
5154 	sata_hba_inst_t *shi = SATA_TXLT_HBA_INST(spx);
5155 	int cport = SATA_TXLT_CPORT(spx);
5156 	int rval;
5157 	int synch;
5158 
5159 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
5160 
5161 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
5162 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
5163 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
5164 		return (rval);
5165 	}
5166 
5167 	scmd->satacmd_addr_type = 0;
5168 	scmd->satacmd_cmd_reg = SATAC_FLUSH_CACHE;
5169 	scmd->satacmd_device_reg = 0;
5170 	scmd->satacmd_sec_count_lsb = 0;
5171 	scmd->satacmd_lba_low_lsb = 0;
5172 	scmd->satacmd_lba_mid_lsb = 0;
5173 	scmd->satacmd_lba_high_lsb = 0;
5174 	scmd->satacmd_features_reg = 0;
5175 	scmd->satacmd_status_reg = 0;
5176 	scmd->satacmd_error_reg = 0;
5177 
5178 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5179 	    "sata_txlt_synchronize_cache\n", NULL);
5180 
5181 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5182 		/* Need to set-up a callback function */
5183 		spx->txlt_sata_pkt->satapkt_comp =
5184 		    sata_txlt_nodata_cmd_completion;
5185 		synch = FALSE;
5186 	} else
5187 		synch = TRUE;
5188 
5189 	/* Transfer command to HBA */
5190 	if (sata_hba_start(spx, &rval) != 0) {
5191 		/* Pkt not accepted for execution */
5192 		mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5193 		return (rval);
5194 	}
5195 	mutex_exit(&SATA_CPORT_MUTEX(shi, cport));
5196 
5197 	/*
5198 	 * If execution non-synchronous, it had to be completed
5199 	 * a callback function will handle potential errors, translate
5200 	 * the response and will do a callback to a target driver.
5201 	 * If it was synchronous, check status, using the same
5202 	 * framework callback.
5203 	 */
5204 	if (synch) {
5205 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5206 		    "synchronous execution status %x\n",
5207 		    spx->txlt_sata_pkt->satapkt_reason);
5208 		sata_txlt_nodata_cmd_completion(spx->txlt_sata_pkt);
5209 	}
5210 	return (TRAN_ACCEPT);
5211 }
5212 
5213 
5214 /*
5215  * Send pkt to SATA HBA driver
5216  *
5217  * This function may be called only if the operation is requested by scsi_pkt,
5218  * i.e. scsi_pkt is not NULL.
5219  *
5220  * This function has to be called with cport mutex held. It does release
5221  * the mutex when it calls HBA driver sata_tran_start function and
5222  * re-acquires it afterwards.
5223  *
5224  * If return value is 0, pkt was accepted, -1 otherwise
5225  * rval is set to appropriate sata_scsi_start return value.
5226  *
5227  * Note 1:If HBA driver returns value other than TRAN_ACCEPT, it should not
5228  * have called the sata_pkt callback function for this packet.
5229  *
5230  * The scsi callback has to be performed by the caller of this routine.
5231  *
5232  * Note 2: No port multiplier support for now.
5233  */
5234 static int
5235 sata_hba_start(sata_pkt_txlate_t *spx, int *rval)
5236 {
5237 	int stat, cport;
5238 	sata_hba_inst_t *sata_hba_inst = spx->txlt_sata_hba_inst;
5239 	sata_drive_info_t *sdinfo;
5240 	sata_device_t *sata_device;
5241 	uint8_t cmd;
5242 	struct sata_cmd_flags cmd_flags;
5243 
5244 	ASSERT(spx->txlt_sata_pkt != NULL);
5245 
5246 	cport = SATA_TXLT_CPORT(spx);
5247 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5248 
5249 	sdinfo = sata_get_device_info(sata_hba_inst,
5250 	    &spx->txlt_sata_pkt->satapkt_device);
5251 	ASSERT(sdinfo != NULL);
5252 
5253 	/* Clear device reset state? */
5254 	if (sdinfo->satadrv_event_flags & SATA_EVNT_CLEAR_DEVICE_RESET) {
5255 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags.
5256 		    sata_clear_dev_reset = B_TRUE;
5257 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_CLEAR_DEVICE_RESET;
5258 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5259 		    "sata_hba_start: clearing device reset state\n", NULL);
5260 	}
5261 	cmd = spx->txlt_sata_pkt->satapkt_cmd.satacmd_cmd_reg;
5262 	cmd_flags = spx->txlt_sata_pkt->satapkt_cmd.satacmd_flags;
5263 	sata_device = &spx->txlt_sata_pkt->satapkt_device;
5264 
5265 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5266 
5267 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5268 	    "Sata cmd 0x%2x\n", cmd);
5269 
5270 	stat = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
5271 	    spx->txlt_sata_pkt);
5272 
5273 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5274 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
5275 	/*
5276 	 * If sata pkt was accepted and executed in asynchronous mode, i.e.
5277 	 * with the sata callback, the sata_pkt could be already destroyed
5278 	 * by the time we check ther return status from the hba_start()
5279 	 * function, because sata_scsi_destroy_pkt() could have been already
5280 	 * called (perhaps in the interrupt context). So, in such case, there
5281 	 * should be no references to it. In other cases, sata_pkt still
5282 	 * exists.
5283 	 */
5284 	switch (stat) {
5285 	case SATA_TRAN_ACCEPTED:
5286 		/*
5287 		 * pkt accepted for execution.
5288 		 * If it was executed synchronously, it is already completed
5289 		 * and pkt completion_reason indicates completion status.
5290 		 */
5291 		*rval = TRAN_ACCEPT;
5292 		return (0);
5293 
5294 	case SATA_TRAN_QUEUE_FULL:
5295 		/*
5296 		 * Controller detected queue full condition.
5297 		 */
5298 		SATADBG1(SATA_DBG_HBA_IF, sata_hba_inst,
5299 		    "sata_hba_start: queue full\n", NULL);
5300 
5301 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
5302 		*spx->txlt_scsi_pkt->pkt_scbp = STATUS_QFULL;
5303 
5304 		*rval = TRAN_BUSY;
5305 		break;
5306 
5307 	case SATA_TRAN_PORT_ERROR:
5308 		/*
5309 		 * Communication/link with device or general port error
5310 		 * detected before pkt execution begun.
5311 		 */
5312 		if (spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
5313 		    SATA_ADDR_CPORT ||
5314 		    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual ==
5315 		    SATA_ADDR_DCPORT)
5316 			sata_log(sata_hba_inst, CE_CONT,
5317 			    "SATA port %d error",
5318 			    sata_device->satadev_addr.cport);
5319 		else
5320 			sata_log(sata_hba_inst, CE_CONT,
5321 			    "SATA port %d pmport %d error\n",
5322 			    sata_device->satadev_addr.cport,
5323 			    sata_device->satadev_addr.pmport);
5324 
5325 		/*
5326 		 * Update the port/device structure.
5327 		 * sata_pkt should be still valid. Since port error is
5328 		 * returned, sata_device content should reflect port
5329 		 * state - it means, that sata address have been changed,
5330 		 * because original packet's sata address refered to a device
5331 		 * attached to some port.
5332 		 */
5333 		sata_update_port_info(sata_hba_inst, sata_device);
5334 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
5335 		*rval = TRAN_FATAL_ERROR;
5336 		break;
5337 
5338 	case SATA_TRAN_CMD_UNSUPPORTED:
5339 		/*
5340 		 * Command rejected by HBA as unsupported. It was HBA driver
5341 		 * that rejected the command, command was not sent to
5342 		 * an attached device.
5343 		 */
5344 		if ((sdinfo != NULL) &&
5345 		    (sdinfo->satadrv_state & SATA_DSTATE_RESET))
5346 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5347 			    "sat_hba_start: cmd 0x%2x rejected "
5348 			    "with SATA_TRAN_CMD_UNSUPPORTED status\n", cmd);
5349 
5350 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5351 		(void) sata_txlt_invalid_command(spx);
5352 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
5353 
5354 		*rval = TRAN_ACCEPT;
5355 		break;
5356 
5357 	case SATA_TRAN_BUSY:
5358 		/*
5359 		 * Command rejected by HBA because other operation prevents
5360 		 * accepting the packet, or device is in RESET condition.
5361 		 */
5362 		if (sdinfo != NULL) {
5363 			sdinfo->satadrv_state =
5364 			    spx->txlt_sata_pkt->satapkt_device.satadev_state;
5365 
5366 			if (sdinfo->satadrv_state & SATA_DSTATE_RESET) {
5367 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5368 				    "sata_hba_start: cmd 0x%2x rejected "
5369 				    "because of device reset condition\n",
5370 				    cmd);
5371 			} else {
5372 				SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
5373 				    "sata_hba_start: cmd 0x%2x rejected "
5374 				    "with SATA_TRAN_BUSY status\n",
5375 				    cmd);
5376 			}
5377 		}
5378 		spx->txlt_scsi_pkt->pkt_reason = CMD_INCOMPLETE;
5379 		*rval = TRAN_BUSY;
5380 		break;
5381 
5382 	default:
5383 		/* Unrecognized HBA response */
5384 		SATA_LOG_D((sata_hba_inst, CE_WARN,
5385 		    "sata_hba_start: unrecognized HBA response "
5386 		    "to cmd : 0x%2x resp 0x%x", cmd, rval));
5387 		spx->txlt_scsi_pkt->pkt_reason = CMD_TRAN_ERR;
5388 		*rval = TRAN_FATAL_ERROR;
5389 		break;
5390 	}
5391 
5392 	/*
5393 	 * If we got here, the packet was rejected.
5394 	 * Check if we need to remember reset state clearing request
5395 	 */
5396 	if (cmd_flags.sata_clear_dev_reset) {
5397 		/*
5398 		 * Check if device is still configured - it may have
5399 		 * disapeared from the configuration
5400 		 */
5401 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
5402 		if (sdinfo != NULL) {
5403 			/*
5404 			 * Restore the flag that requests clearing of
5405 			 * the device reset state,
5406 			 * so the next sata packet may carry it to HBA.
5407 			 */
5408 			sdinfo->satadrv_event_flags |=
5409 			    SATA_EVNT_CLEAR_DEVICE_RESET;
5410 		}
5411 	}
5412 	return (-1);
5413 }
5414 
5415 /*
5416  * Scsi response setup for invalid LBA
5417  *
5418  * Returns TRAN_ACCEPT and appropriate values in scsi_pkt fields.
5419  */
5420 static int
5421 sata_txlt_lba_out_of_range(sata_pkt_txlate_t *spx)
5422 {
5423 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5424 	struct scsi_extended_sense *sense;
5425 
5426 	scsipkt->pkt_reason = CMD_CMPLT;
5427 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5428 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5429 	*scsipkt->pkt_scbp = STATUS_CHECK;
5430 
5431 	*scsipkt->pkt_scbp = STATUS_CHECK;
5432 	sense = sata_arq_sense(spx);
5433 	sense->es_key = KEY_ILLEGAL_REQUEST;
5434 	sense->es_add_code = SD_SCSI_ASC_LBA_OUT_OF_RANGE;
5435 
5436 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5437 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5438 
5439 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5440 	    scsipkt->pkt_comp != NULL)
5441 		/* scsi callback required */
5442 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5443 		    (task_func_t *)scsipkt->pkt_comp, (void *) scsipkt,
5444 		    TQ_SLEEP) == NULL)
5445 			/* Scheduling the callback failed */
5446 			return (TRAN_BUSY);
5447 	return (TRAN_ACCEPT);
5448 }
5449 
5450 
5451 /*
5452  * Analyze device status and error registers and translate them into
5453  * appropriate scsi sense codes.
5454  * NOTE: non-packet commands only for now
5455  */
5456 static void
5457 sata_decode_device_error(sata_pkt_txlate_t *spx,
5458     struct scsi_extended_sense *sense)
5459 {
5460 	uint8_t err_reg = spx->txlt_sata_pkt->satapkt_cmd.satacmd_error_reg;
5461 
5462 	ASSERT(sense != NULL);
5463 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
5464 	    SATA_STATUS_ERR);
5465 
5466 
5467 	if (err_reg & SATA_ERROR_ICRC) {
5468 		sense->es_key = KEY_ABORTED_COMMAND;
5469 		sense->es_add_code = 0x08; /* Communication failure */
5470 		return;
5471 	}
5472 
5473 	if (err_reg & SATA_ERROR_UNC) {
5474 		sense->es_key = KEY_MEDIUM_ERROR;
5475 		/* Information bytes (LBA) need to be set by a caller */
5476 		return;
5477 	}
5478 
5479 	/* ADD HERE: MC error bit handling for ATAPI CD/DVD */
5480 	if (err_reg & (SATA_ERROR_MCR | SATA_ERROR_NM)) {
5481 		sense->es_key = KEY_UNIT_ATTENTION;
5482 		sense->es_add_code = 0x3a; /* No media present */
5483 		return;
5484 	}
5485 
5486 	if (err_reg & SATA_ERROR_IDNF) {
5487 		if (err_reg & SATA_ERROR_ABORT) {
5488 			sense->es_key = KEY_ABORTED_COMMAND;
5489 		} else {
5490 			sense->es_key = KEY_ILLEGAL_REQUEST;
5491 			sense->es_add_code = 0x21; /* LBA out of range */
5492 		}
5493 		return;
5494 	}
5495 
5496 	if (err_reg & SATA_ERROR_ABORT) {
5497 		ASSERT(spx->txlt_sata_pkt != NULL);
5498 		sense->es_key = KEY_ABORTED_COMMAND;
5499 		return;
5500 	}
5501 }
5502 
5503 /*
5504  * Extract error LBA from sata_pkt.satapkt_cmd register fields
5505  */
5506 static void
5507 sata_extract_error_lba(sata_pkt_txlate_t *spx, uint64_t *lba)
5508 {
5509 	sata_cmd_t *sata_cmd = &spx->txlt_sata_pkt->satapkt_cmd;
5510 
5511 	*lba = 0;
5512 	if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA48) {
5513 		*lba = sata_cmd->satacmd_lba_high_msb;
5514 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_msb;
5515 		*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_msb;
5516 	} else if (sata_cmd->satacmd_addr_type == ATA_ADDR_LBA28) {
5517 		*lba = sata_cmd->satacmd_device_reg & 0xf;
5518 	}
5519 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_high_lsb;
5520 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_mid_lsb;
5521 	*lba = (*lba << 8) | sata_cmd->satacmd_lba_low_lsb;
5522 }
5523 
5524 /*
5525  * This is fixed sense format - if LBA exceeds the info field size,
5526  * no valid info will be returned (valid bit in extended sense will
5527  * be set to 0).
5528  */
5529 static struct scsi_extended_sense *
5530 sata_arq_sense(sata_pkt_txlate_t *spx)
5531 {
5532 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5533 	struct scsi_arq_status *arqs;
5534 	struct scsi_extended_sense *sense;
5535 
5536 	/* Fill ARQ sense data */
5537 	scsipkt->pkt_state |= STATE_ARQ_DONE;
5538 	arqs = (struct scsi_arq_status *)scsipkt->pkt_scbp;
5539 	*(uchar_t *)&arqs->sts_status = STATUS_CHECK;
5540 	*(uchar_t *)&arqs->sts_rqpkt_status = STATUS_GOOD;
5541 	arqs->sts_rqpkt_reason = CMD_CMPLT;
5542 	arqs->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5543 	    STATE_XFERRED_DATA | STATE_SENT_CMD | STATE_GOT_STATUS;
5544 	arqs->sts_rqpkt_resid = 0;
5545 	sense = &arqs->sts_sensedata;
5546 	bzero(sense, sizeof (struct scsi_extended_sense));
5547 	sata_fixed_sense_data_preset(sense);
5548 	return (sense);
5549 }
5550 
5551 
5552 /*
5553  * Emulated SATA Read/Write command completion for zero-length requests.
5554  * This request always succedes, so in synchronous mode it always returns
5555  * TRAN_ACCEPT, and in non-synchronous mode it may return TRAN_BUSY if the
5556  * callback cannot be scheduled.
5557  */
5558 static int
5559 sata_emul_rw_completion(sata_pkt_txlate_t *spx)
5560 {
5561 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5562 
5563 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5564 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5565 	scsipkt->pkt_reason = CMD_CMPLT;
5566 	*scsipkt->pkt_scbp = STATUS_GOOD;
5567 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
5568 		/* scsi callback required - have to schedule it */
5569 		if (taskq_dispatch(SATA_TXLT_TASKQ(spx),
5570 		    (task_func_t *)scsipkt->pkt_comp,
5571 		    (void *)scsipkt, TQ_SLEEP) == NULL)
5572 			/* Scheduling the callback failed */
5573 			return (TRAN_BUSY);
5574 	}
5575 	return (TRAN_ACCEPT);
5576 }
5577 
5578 
5579 /*
5580  * Translate completion status of SATA read/write commands into scsi response.
5581  * pkt completion_reason is checked to determine the completion status.
5582  * Do scsi callback if necessary.
5583  *
5584  * Note: this function may be called also for synchronously executed
5585  * commands.
5586  * This function may be used only if scsi_pkt is non-NULL.
5587  */
5588 static void
5589 sata_txlt_rw_completion(sata_pkt_t *sata_pkt)
5590 {
5591 	sata_pkt_txlate_t *spx =
5592 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
5593 	sata_cmd_t *scmd = &sata_pkt->satapkt_cmd;
5594 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5595 	struct scsi_extended_sense *sense;
5596 	uint64_t lba;
5597 	struct buf *bp;
5598 	int rval;
5599 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5600 		/* Normal completion */
5601 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5602 		    STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
5603 		scsipkt->pkt_reason = CMD_CMPLT;
5604 		*scsipkt->pkt_scbp = STATUS_GOOD;
5605 		if (spx->txlt_tmp_buf != NULL) {
5606 			/* Temporary buffer was used */
5607 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
5608 			if (bp->b_flags & B_READ) {
5609 				rval = ddi_dma_sync(
5610 				    spx->txlt_buf_dma_handle, 0, 0,
5611 				    DDI_DMA_SYNC_FORCPU);
5612 				ASSERT(rval == DDI_SUCCESS);
5613 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
5614 				    bp->b_bcount);
5615 			}
5616 		}
5617 	} else {
5618 		/*
5619 		 * Something went wrong - analyze return
5620 		 */
5621 		scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5622 		    STATE_SENT_CMD | STATE_GOT_STATUS;
5623 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5624 		*scsipkt->pkt_scbp = STATUS_CHECK;
5625 		sense = sata_arq_sense(spx);
5626 		ASSERT(sense != NULL);
5627 
5628 		/*
5629 		 * SATA_PKT_DEV_ERROR is the only case where we may be able to
5630 		 * extract from device registers the failing LBA.
5631 		 */
5632 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
5633 			if ((scmd->satacmd_addr_type == ATA_ADDR_LBA48) &&
5634 			    (scmd->satacmd_lba_mid_msb != 0 ||
5635 			    scmd->satacmd_lba_high_msb != 0)) {
5636 				/*
5637 				 * We have problem reporting this cmd LBA
5638 				 * in fixed sense data format, because of
5639 				 * the size of the scsi LBA fields.
5640 				 */
5641 				sense->es_valid = 0;
5642 			} else {
5643 				sata_extract_error_lba(spx, &lba);
5644 				sense->es_info_1 = (lba & 0xFF000000) >> 24;
5645 				sense->es_info_2 = (lba & 0xFF0000) >> 16;
5646 				sense->es_info_3 = (lba & 0xFF00) >> 8;
5647 				sense->es_info_4 = lba & 0xFF;
5648 			}
5649 		} else {
5650 			/* Invalid extended sense info */
5651 			sense->es_valid = 0;
5652 		}
5653 
5654 		switch (sata_pkt->satapkt_reason) {
5655 		case SATA_PKT_PORT_ERROR:
5656 			/* We may want to handle DEV GONE state as well */
5657 			/*
5658 			 * We have no device data. Assume no data transfered.
5659 			 */
5660 			sense->es_key = KEY_HARDWARE_ERROR;
5661 			break;
5662 
5663 		case SATA_PKT_DEV_ERROR:
5664 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5665 			    SATA_STATUS_ERR) {
5666 				/*
5667 				 * determine dev error reason from error
5668 				 * reg content
5669 				 */
5670 				sata_decode_device_error(spx, sense);
5671 				if (sense->es_key == KEY_MEDIUM_ERROR) {
5672 					switch (scmd->satacmd_cmd_reg) {
5673 					case SATAC_READ_DMA:
5674 					case SATAC_READ_DMA_EXT:
5675 					case SATAC_READ_DMA_QUEUED:
5676 					case SATAC_READ_DMA_QUEUED_EXT:
5677 					case SATAC_READ_FPDMA_QUEUED:
5678 						/* Unrecovered read error */
5679 						sense->es_add_code =
5680 						    SD_SCSI_ASC_UNREC_READ_ERR;
5681 						break;
5682 					case SATAC_WRITE_DMA:
5683 					case SATAC_WRITE_DMA_EXT:
5684 					case SATAC_WRITE_DMA_QUEUED:
5685 					case SATAC_WRITE_DMA_QUEUED_EXT:
5686 					case SATAC_WRITE_FPDMA_QUEUED:
5687 						/* Write error */
5688 						sense->es_add_code =
5689 						    SD_SCSI_ASC_WRITE_ERR;
5690 						break;
5691 					default:
5692 						/* Internal error */
5693 						SATA_LOG_D((
5694 						    spx->txlt_sata_hba_inst,
5695 						    CE_WARN,
5696 						    "sata_txlt_rw_completion :"
5697 						    "internal error - invalid "
5698 						    "command 0x%2x",
5699 						    scmd->satacmd_cmd_reg));
5700 						break;
5701 					}
5702 				}
5703 				break;
5704 			}
5705 			/* No extended sense key - no info available */
5706 			scsipkt->pkt_reason = CMD_INCOMPLETE;
5707 			break;
5708 
5709 		case SATA_PKT_TIMEOUT:
5710 			/* scsipkt->pkt_reason = CMD_TIMEOUT; */
5711 			scsipkt->pkt_reason = CMD_INCOMPLETE;
5712 			/* No extended sense key ? */
5713 			break;
5714 
5715 		case SATA_PKT_ABORTED:
5716 			scsipkt->pkt_reason = CMD_ABORTED;
5717 			/* No extended sense key ? */
5718 			break;
5719 
5720 		case SATA_PKT_RESET:
5721 			scsipkt->pkt_reason = CMD_RESET;
5722 			break;
5723 
5724 		default:
5725 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5726 			    "sata_txlt_rw_completion: "
5727 			    "invalid packet completion reason"));
5728 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5729 			break;
5730 		}
5731 	}
5732 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5733 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5734 
5735 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5736 	    scsipkt->pkt_comp != NULL)
5737 		/* scsi callback required */
5738 		(*scsipkt->pkt_comp)(scsipkt);
5739 }
5740 
5741 
5742 /*
5743  * Translate completion status of non-data commands (i.e. commands returning
5744  * no data).
5745  * pkt completion_reason is checked to determine the completion status.
5746  * Do scsi callback if necessary (FLAG_NOINTR == 0)
5747  *
5748  * Note: this function may be called also for synchronously executed
5749  * commands.
5750  * This function may be used only if scsi_pkt is non-NULL.
5751  */
5752 
5753 static 	void
5754 sata_txlt_nodata_cmd_completion(sata_pkt_t *sata_pkt)
5755 {
5756 	sata_pkt_txlate_t *spx =
5757 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
5758 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
5759 	struct scsi_extended_sense *sense;
5760 
5761 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
5762 	    STATE_SENT_CMD | STATE_GOT_STATUS;
5763 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
5764 		/* Normal completion */
5765 		scsipkt->pkt_reason = CMD_CMPLT;
5766 		*scsipkt->pkt_scbp = STATUS_GOOD;
5767 	} else {
5768 		/* Something went wrong */
5769 		scsipkt->pkt_reason = CMD_INCOMPLETE;
5770 		*scsipkt->pkt_scbp = STATUS_CHECK;
5771 		sense = sata_arq_sense(spx);
5772 		switch (sata_pkt->satapkt_reason) {
5773 		case SATA_PKT_PORT_ERROR:
5774 			/*
5775 			 * We have no device data. Assume no data transfered.
5776 			 */
5777 			sense->es_key = KEY_HARDWARE_ERROR;
5778 			break;
5779 
5780 		case SATA_PKT_DEV_ERROR:
5781 			if (sata_pkt->satapkt_cmd.satacmd_status_reg &
5782 			    SATA_STATUS_ERR) {
5783 				/*
5784 				 * determine dev error reason from error
5785 				 * reg content
5786 				 */
5787 				sata_decode_device_error(spx, sense);
5788 				break;
5789 			}
5790 			/* No extended sense key - no info available */
5791 			break;
5792 
5793 		case SATA_PKT_TIMEOUT:
5794 			/* scsipkt->pkt_reason = CMD_TIMEOUT; */
5795 			scsipkt->pkt_reason = CMD_INCOMPLETE;
5796 			/* No extended sense key ? */
5797 			break;
5798 
5799 		case SATA_PKT_ABORTED:
5800 			scsipkt->pkt_reason = CMD_ABORTED;
5801 			/* No extended sense key ? */
5802 			break;
5803 
5804 		case SATA_PKT_RESET:
5805 			/* pkt aborted by an explicit reset from a host */
5806 			scsipkt->pkt_reason = CMD_RESET;
5807 			break;
5808 
5809 		default:
5810 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
5811 			    "sata_txlt_nodata_cmd_completion: "
5812 			    "invalid packet completion reason %d",
5813 			    sata_pkt->satapkt_reason));
5814 			scsipkt->pkt_reason = CMD_TRAN_ERR;
5815 			break;
5816 		}
5817 
5818 	}
5819 	SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
5820 	    "Scsi_pkt completion reason %x\n", scsipkt->pkt_reason);
5821 
5822 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
5823 	    scsipkt->pkt_comp != NULL)
5824 		/* scsi callback required */
5825 		(*scsipkt->pkt_comp)(scsipkt);
5826 }
5827 
5828 
5829 /*
5830  * Build Mode sense R/W recovery page
5831  * NOT IMPLEMENTED
5832  */
5833 
5834 static int
5835 sata_build_msense_page_1(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5836 {
5837 #ifndef __lock_lint
5838 	_NOTE(ARGUNUSED(sdinfo))
5839 	_NOTE(ARGUNUSED(pcntrl))
5840 	_NOTE(ARGUNUSED(buf))
5841 #endif
5842 	return (0);
5843 }
5844 
5845 /*
5846  * Build Mode sense caching page  -  scsi-3 implementation.
5847  * Page length distinguishes previous format from scsi-3 format.
5848  * buf must have space for 0x12 bytes.
5849  * Only DRA (disable read ahead ) and WCE (write cache enable) are changeable.
5850  *
5851  */
5852 static int
5853 sata_build_msense_page_8(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5854 {
5855 	struct mode_cache_scsi3 *page = (struct mode_cache_scsi3 *)buf;
5856 	sata_id_t *sata_id = &sdinfo->satadrv_id;
5857 
5858 	/*
5859 	 * Most of the fields are set to 0, being not supported and/or disabled
5860 	 */
5861 	bzero(buf, PAGELENGTH_DAD_MODE_CACHE_SCSI3);
5862 
5863 	/* Saved paramters not supported */
5864 	if (pcntrl == 3)
5865 		return (0);
5866 	if (pcntrl == 0 || pcntrl == 2) {
5867 		/*
5868 		 * For now treat current and default parameters as same
5869 		 * That may have to change, if target driver will complain
5870 		 */
5871 		page->mode_page.code = MODEPAGE_CACHING;	/* PS = 0 */
5872 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
5873 
5874 		if ((sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) &&
5875 		    !(sata_id->ai_features85 & SATA_LOOK_AHEAD)) {
5876 			page->dra = 1;		/* Read Ahead disabled */
5877 			page->rcd = 1;		/* Read Cache disabled */
5878 		}
5879 		if ((sata_id->ai_cmdset82 & SATA_WRITE_CACHE) &&
5880 		    (sata_id->ai_features85 & SATA_WRITE_CACHE))
5881 			page->wce = 1;		/* Write Cache enabled */
5882 	} else {
5883 		/* Changeable parameters */
5884 		page->mode_page.code = MODEPAGE_CACHING;
5885 		page->mode_page.length = PAGELENGTH_DAD_MODE_CACHE_SCSI3;
5886 		if (sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) {
5887 			page->dra = 1;
5888 			page->rcd = 1;
5889 		}
5890 		if (sata_id->ai_cmdset82 & SATA_WRITE_CACHE)
5891 			page->wce = 1;
5892 	}
5893 	return (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
5894 	    sizeof (struct mode_page));
5895 }
5896 
5897 /*
5898  * Build Mode sense exception cntrl page
5899  */
5900 static int
5901 sata_build_msense_page_1c(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5902 {
5903 	struct mode_info_excpt_page *page = (struct mode_info_excpt_page *)buf;
5904 	sata_id_t *sata_id = &sdinfo->satadrv_id;
5905 
5906 	/*
5907 	 * Most of the fields are set to 0, being not supported and/or disabled
5908 	 */
5909 	bzero(buf, PAGELENGTH_INFO_EXCPT);
5910 
5911 	page->mode_page.code = MODEPAGE_INFO_EXCPT;
5912 	page->mode_page.length = PAGELENGTH_INFO_EXCPT;
5913 
5914 	/* Indicate that this is page is saveable */
5915 	page->mode_page.ps = 1;
5916 
5917 	/*
5918 	 * We will return the same data for default, current and saved page.
5919 	 * The only changeable bit is dexcpt and that bit is required
5920 	 * by the ATA specification to be preserved across power cycles.
5921 	 */
5922 	if (pcntrl != 1) {
5923 		page->dexcpt = !(sata_id->ai_features85 & SATA_SMART_SUPPORTED);
5924 		page->mrie = MRIE_ONLY_ON_REQUEST;
5925 	}
5926 	else
5927 		page->dexcpt = 1;	/* Only changeable parameter */
5928 
5929 	return (PAGELENGTH_INFO_EXCPT + sizeof (struct mode_info_excpt_page));
5930 }
5931 
5932 
5933 static int
5934 sata_build_msense_page_30(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
5935 {
5936 	struct mode_acoustic_management *page =
5937 	    (struct mode_acoustic_management *)buf;
5938 	sata_id_t *sata_id = &sdinfo->satadrv_id;
5939 
5940 	/*
5941 	 * Most of the fields are set to 0, being not supported and/or disabled
5942 	 */
5943 	bzero(buf, PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT);
5944 
5945 	switch (pcntrl) {
5946 	case P_CNTRL_DEFAULT:
5947 		/*  default paramters not supported */
5948 		return (0);
5949 
5950 	case P_CNTRL_CURRENT:
5951 	case P_CNTRL_SAVED:
5952 		/* Saved and current are supported and are identical */
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 feature is supported */
5959 		/* If feature is not supported */
5960 		if (!(sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT)) {
5961 			page->acoustic_manag_enable =
5962 			    ACOUSTIC_DISABLED;
5963 		} else {
5964 			page->acoustic_manag_enable =
5965 			    ((sata_id->ai_features86 & SATA_ACOUSTIC_MGMT)
5966 			    != 0);
5967 			/* Word 94 inidicates the value */
5968 #ifdef	_LITTLE_ENDIAN
5969 			page->acoustic_manag_level =
5970 			    (uchar_t)sata_id->ai_acoustic;
5971 			page->vendor_recommended_value =
5972 			    sata_id->ai_acoustic >> 8;
5973 #else
5974 			page->acoustic_manag_level =
5975 			    sata_id->ai_acoustic >> 8;
5976 			page->vendor_recommended_value =
5977 			    (uchar_t)sata_id->ai_acoustic;
5978 #endif
5979 		}
5980 		break;
5981 
5982 	case P_CNTRL_CHANGEABLE:
5983 		page->mode_page.code = MODEPAGE_ACOUSTIC_MANAG;
5984 		page->mode_page.length =
5985 		    PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT;
5986 		page->mode_page.ps = 1;
5987 
5988 		/* Word 83 indicates if the feature is supported */
5989 		if (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT) {
5990 			page->acoustic_manag_enable =
5991 			    ACOUSTIC_ENABLED;
5992 			page->acoustic_manag_level = 0xff;
5993 		}
5994 		break;
5995 	}
5996 	return (PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
5997 	    sizeof (struct mode_page));
5998 }
5999 
6000 
6001 /*
6002  * Build Mode sense power condition page
6003  * NOT IMPLEMENTED.
6004  */
6005 static int
6006 sata_build_msense_page_1a(sata_drive_info_t *sdinfo, int pcntrl, uint8_t *buf)
6007 {
6008 #ifndef __lock_lint
6009 	_NOTE(ARGUNUSED(sdinfo))
6010 	_NOTE(ARGUNUSED(pcntrl))
6011 	_NOTE(ARGUNUSED(buf))
6012 #endif
6013 	return (0);
6014 }
6015 
6016 
6017 /*
6018  * Process mode select caching page 8 (scsi3 format only).
6019  * Read Ahead (same as read cache) and Write Cache may be turned on and off
6020  * if these features are supported by the device. If these features are not
6021  * supported, quietly ignore them.
6022  * This function fails only if the SET FEATURE command sent to
6023  * the device fails. The page format is not varified, assuming that the
6024  * target driver operates correctly - if parameters length is too short,
6025  * we just drop the page.
6026  * Two command may be sent if both Read Cache/Read Ahead and Write Cache
6027  * setting have to be changed.
6028  * SET FEATURE command is executed synchronously, i.e. we wait here until
6029  * it is completed, regardless of the scsi pkt directives.
6030  *
6031  * Note: Mode Select Caching page RCD and DRA bits are tied together, i.e.
6032  * changing DRA will change RCD.
6033  *
6034  * More than one SATA command may be executed to perform operations specified
6035  * by mode select pages. The first error terminates further execution.
6036  * Operations performed successully are not backed-up in such case.
6037  *
6038  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
6039  * If operation resulted in changing device setup, dmod flag should be set to
6040  * one (1). If parameters were not changed, dmod flag should be set to 0.
6041  * Upon return, if operation required sending command to the device, the rval
6042  * should be set to the value returned by sata_hba_start. If operation
6043  * did not require device access, rval should be set to TRAN_ACCEPT.
6044  * The pagelen should be set to the length of the page.
6045  *
6046  * This function has to be called with a port mutex held.
6047  *
6048  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6049  */
6050 int
6051 sata_mode_select_page_8(sata_pkt_txlate_t *spx, struct mode_cache_scsi3 *page,
6052     int parmlen, int *pagelen, int *rval, int *dmod)
6053 {
6054 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6055 	sata_drive_info_t *sdinfo;
6056 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6057 	sata_id_t *sata_id;
6058 	struct scsi_extended_sense *sense;
6059 	int wce, dra;	/* Current settings */
6060 
6061 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6062 	    &spx->txlt_sata_pkt->satapkt_device);
6063 	sata_id = &sdinfo->satadrv_id;
6064 	*dmod = 0;
6065 
6066 	/* Verify parameters length. If too short, drop it */
6067 	if (PAGELENGTH_DAD_MODE_CACHE_SCSI3 +
6068 	    sizeof (struct mode_page) < parmlen) {
6069 		*scsipkt->pkt_scbp = STATUS_CHECK;
6070 		sense = sata_arq_sense(spx);
6071 		sense->es_key = KEY_ILLEGAL_REQUEST;
6072 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6073 		*pagelen = parmlen;
6074 		*rval = TRAN_ACCEPT;
6075 		return (SATA_FAILURE);
6076 	}
6077 
6078 	*pagelen = PAGELENGTH_DAD_MODE_CACHE_SCSI3 + sizeof (struct mode_page);
6079 
6080 	/*
6081 	 * We can manipulate only write cache and read ahead
6082 	 * (read cache) setting.
6083 	 */
6084 	if (!(sata_id->ai_cmdset82 & SATA_LOOK_AHEAD) &&
6085 	    !(sata_id->ai_cmdset82 & SATA_WRITE_CACHE)) {
6086 		/*
6087 		 * None of the features is supported - ignore
6088 		 */
6089 		*rval = TRAN_ACCEPT;
6090 		return (SATA_SUCCESS);
6091 	}
6092 
6093 	/* Current setting of Read Ahead (and Read Cache) */
6094 	if (sata_id->ai_features85 & SATA_LOOK_AHEAD)
6095 		dra = 0;	/* 0 == not disabled */
6096 	else
6097 		dra = 1;
6098 	/* Current setting of Write Cache */
6099 	if (sata_id->ai_features85 & SATA_WRITE_CACHE)
6100 		wce = 1;
6101 	else
6102 		wce = 0;
6103 
6104 	if (page->dra == dra && page->wce == wce && page->rcd == dra) {
6105 		/* nothing to do */
6106 		*rval = TRAN_ACCEPT;
6107 		return (SATA_SUCCESS);
6108 	}
6109 	/*
6110 	 * Need to flip some setting
6111 	 * Set-up Internal SET FEATURES command(s)
6112 	 */
6113 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6114 	scmd->satacmd_addr_type = 0;
6115 	scmd->satacmd_device_reg = 0;
6116 	scmd->satacmd_status_reg = 0;
6117 	scmd->satacmd_error_reg = 0;
6118 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
6119 	if (page->dra != dra || page->rcd != dra) {
6120 		/* Need to flip read ahead setting */
6121 		if (dra == 0)
6122 			/* Disable read ahead / read cache */
6123 			scmd->satacmd_features_reg =
6124 			    SATAC_SF_DISABLE_READ_AHEAD;
6125 		else
6126 			/* Enable read ahead  / read cache */
6127 			scmd->satacmd_features_reg =
6128 			    SATAC_SF_ENABLE_READ_AHEAD;
6129 
6130 		/* Transfer command to HBA */
6131 		if (sata_hba_start(spx, rval) != 0)
6132 			/*
6133 			 * Pkt not accepted for execution.
6134 			 */
6135 			return (SATA_FAILURE);
6136 
6137 		*dmod = 1;
6138 
6139 		/* Now process return */
6140 		if (spx->txlt_sata_pkt->satapkt_reason !=
6141 		    SATA_PKT_COMPLETED) {
6142 			goto failure;	/* Terminate */
6143 		}
6144 	}
6145 
6146 	/* Note that the packet is not removed, so it could be re-used */
6147 	if (page->wce != wce) {
6148 		/* Need to flip Write Cache setting */
6149 		if (page->wce == 1)
6150 			/* Enable write cache */
6151 			scmd->satacmd_features_reg =
6152 			    SATAC_SF_ENABLE_WRITE_CACHE;
6153 		else
6154 			/* Disable write cache */
6155 			scmd->satacmd_features_reg =
6156 			    SATAC_SF_DISABLE_WRITE_CACHE;
6157 
6158 		/* Transfer command to HBA */
6159 		if (sata_hba_start(spx, rval) != 0)
6160 			/*
6161 			 * Pkt not accepted for execution.
6162 			 */
6163 			return (SATA_FAILURE);
6164 
6165 		*dmod = 1;
6166 
6167 		/* Now process return */
6168 		if (spx->txlt_sata_pkt->satapkt_reason !=
6169 		    SATA_PKT_COMPLETED) {
6170 			goto failure;
6171 		}
6172 	}
6173 	return (SATA_SUCCESS);
6174 
6175 failure:
6176 	sata_xlate_errors(spx);
6177 
6178 	return (SATA_FAILURE);
6179 }
6180 
6181 /*
6182  * Process mode select informational exceptions control page 0x1c
6183  *
6184  * The only changeable bit is dexcpt (disable exceptions).
6185  * MRIE (method of reporting informational exceptions) must be
6186  * "only on request".
6187  *
6188  * Return SATA_SUCCESS if operation succeeded, SATA_FAILURE otherwise.
6189  * If operation resulted in changing device setup, dmod flag should be set to
6190  * one (1). If parameters were not changed, dmod flag should be set to 0.
6191  * Upon return, if operation required sending command to the device, the rval
6192  * should be set to the value returned by sata_hba_start. If operation
6193  * did not require device access, rval should be set to TRAN_ACCEPT.
6194  * The pagelen should be set to the length of the page.
6195  *
6196  * This function has to be called with a port mutex held.
6197  *
6198  * Returns SATA_SUCCESS if operation was successful, SATA_FAILURE otherwise.
6199  */
6200 static	int
6201 sata_mode_select_page_1c(
6202 	sata_pkt_txlate_t *spx,
6203 	struct mode_info_excpt_page *page,
6204 	int parmlen,
6205 	int *pagelen,
6206 	int *rval,
6207 	int *dmod)
6208 {
6209 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6210 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6211 	sata_drive_info_t *sdinfo;
6212 	sata_id_t *sata_id;
6213 	struct scsi_extended_sense *sense;
6214 
6215 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6216 	    &spx->txlt_sata_pkt->satapkt_device);
6217 	sata_id = &sdinfo->satadrv_id;
6218 
6219 	*dmod = 0;
6220 
6221 	/* Verify parameters length. If too short, drop it */
6222 	if (((PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page)) < parmlen) ||
6223 	    page->perf || page->test || (page->mrie != MRIE_ONLY_ON_REQUEST)) {
6224 		*scsipkt->pkt_scbp = STATUS_CHECK;
6225 		sense = sata_arq_sense(spx);
6226 		sense->es_key = KEY_ILLEGAL_REQUEST;
6227 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6228 		*pagelen = parmlen;
6229 		*rval = TRAN_ACCEPT;
6230 		return (SATA_FAILURE);
6231 	}
6232 
6233 	*pagelen = PAGELENGTH_INFO_EXCPT + sizeof (struct mode_page);
6234 
6235 	if (! (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED)) {
6236 		*scsipkt->pkt_scbp = STATUS_CHECK;
6237 		sense = sata_arq_sense(spx);
6238 		sense->es_key = KEY_ILLEGAL_REQUEST;
6239 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_CDB;
6240 		*pagelen = parmlen;
6241 		*rval = TRAN_ACCEPT;
6242 		return (SATA_FAILURE);
6243 	}
6244 
6245 	/* If already in the state requested, we are done */
6246 	if (page->dexcpt == ! (sata_id->ai_features85 & SATA_SMART_ENABLED)) {
6247 		/* nothing to do */
6248 		*rval = TRAN_ACCEPT;
6249 		return (SATA_SUCCESS);
6250 	}
6251 
6252 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6253 
6254 	/* Build SMART_ENABLE or SMART_DISABLE command */
6255 	scmd->satacmd_addr_type = 0;		/* N/A */
6256 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
6257 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
6258 	scmd->satacmd_features_reg = page->dexcpt ?
6259 	    SATA_SMART_DISABLE_OPS : SATA_SMART_ENABLE_OPS;
6260 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
6261 	scmd->satacmd_cmd_reg = SATAC_SMART;
6262 
6263 	/* Transfer command to HBA */
6264 	if (sata_hba_start(spx, rval) != 0)
6265 		/*
6266 		 * Pkt not accepted for execution.
6267 		 */
6268 		return (SATA_FAILURE);
6269 
6270 	*dmod = 1;	/* At least may have been modified */
6271 
6272 	/* Now process return */
6273 	if (spx->txlt_sata_pkt->satapkt_reason == SATA_PKT_COMPLETED)
6274 		return (SATA_SUCCESS);
6275 
6276 	/* Packet did not complete successfully */
6277 	sata_xlate_errors(spx);
6278 
6279 	return (SATA_FAILURE);
6280 }
6281 
6282 int
6283 sata_mode_select_page_30(sata_pkt_txlate_t *spx, struct
6284     mode_acoustic_management *page, int parmlen, int *pagelen,
6285     int *rval, int *dmod)
6286 {
6287 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6288 	sata_drive_info_t *sdinfo;
6289 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6290 	sata_id_t *sata_id;
6291 	struct scsi_extended_sense *sense;
6292 
6293 	sdinfo = sata_get_device_info(spx->txlt_sata_hba_inst,
6294 	    &spx->txlt_sata_pkt->satapkt_device);
6295 	sata_id = &sdinfo->satadrv_id;
6296 	*dmod = 0;
6297 
6298 	/* If parmlen is too short or the feature is not supported, drop it */
6299 	if (((PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6300 	    sizeof (struct mode_page)) < parmlen) ||
6301 	    (! (sata_id->ai_cmdset83 & SATA_ACOUSTIC_MGMT))) {
6302 		*scsipkt->pkt_scbp = STATUS_CHECK;
6303 		sense = sata_arq_sense(spx);
6304 		sense->es_key = KEY_ILLEGAL_REQUEST;
6305 		sense->es_add_code = SD_SCSI_ASC_INVALID_FIELD_IN_PARAMS_LIST;
6306 		*pagelen = parmlen;
6307 		*rval = TRAN_ACCEPT;
6308 		return (SATA_FAILURE);
6309 	}
6310 
6311 	*pagelen = PAGELENGTH_DAD_MODE_ACOUSTIC_MANAGEMENT +
6312 	    sizeof (struct mode_page);
6313 
6314 	/*
6315 	 * We can enable and disable acoustice management and
6316 	 * set the acoustic management level.
6317 	 */
6318 
6319 	/*
6320 	 * Set-up Internal SET FEATURES command(s)
6321 	 */
6322 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
6323 	scmd->satacmd_addr_type = 0;
6324 	scmd->satacmd_device_reg = 0;
6325 	scmd->satacmd_status_reg = 0;
6326 	scmd->satacmd_error_reg = 0;
6327 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
6328 	if (page->acoustic_manag_enable) {
6329 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_ACOUSTIC;
6330 		scmd->satacmd_sec_count_lsb = page->acoustic_manag_level;
6331 	} else {	/* disabling acoustic management */
6332 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_ACOUSTIC;
6333 	}
6334 
6335 	/* Transfer command to HBA */
6336 	if (sata_hba_start(spx, rval) != 0)
6337 		/*
6338 		 * Pkt not accepted for execution.
6339 		 */
6340 		return (SATA_FAILURE);
6341 
6342 	/* Now process return */
6343 	if (spx->txlt_sata_pkt->satapkt_reason != SATA_PKT_COMPLETED) {
6344 		sata_xlate_errors(spx);
6345 		return (SATA_FAILURE);
6346 	}
6347 
6348 	*dmod = 1;
6349 
6350 	return (SATA_SUCCESS);
6351 }
6352 
6353 
6354 
6355 
6356 /*
6357  * sata_build_lsense_page0() is used to create the
6358  * SCSI LOG SENSE page 0 (supported log pages)
6359  *
6360  * Currently supported pages are 0, 0x10, 0x2f and 0x30
6361  * (supported log pages, self-test results, informational exceptions
6362  *  and Sun vendor specific ATA SMART data).
6363  *
6364  * Takes a sata_drive_info t * and the address of a buffer
6365  * in which to create the page information.
6366  *
6367  * Returns the number of bytes valid in the buffer.
6368  */
6369 static	int
6370 sata_build_lsense_page_0(sata_drive_info_t *sdinfo, uint8_t *buf)
6371 {
6372 	struct log_parameter *lpp = (struct log_parameter *)buf;
6373 	uint8_t *page_ptr = (uint8_t *)lpp->param_values;
6374 	int num_pages_supported = 1; /* Always have GET_SUPPORTED_LOG_PAGES */
6375 	sata_id_t *sata_id = &sdinfo->satadrv_id;
6376 
6377 	lpp->param_code[0] = 0;
6378 	lpp->param_code[1] = 0;
6379 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
6380 	*page_ptr++ = PAGE_CODE_GET_SUPPORTED_LOG_PAGES;
6381 
6382 	if (sata_id->ai_cmdset82 & SATA_SMART_SUPPORTED) {
6383 		if (sata_id->ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) {
6384 			*page_ptr++ = PAGE_CODE_SELF_TEST_RESULTS;
6385 			++num_pages_supported;
6386 		}
6387 		*page_ptr++ = PAGE_CODE_INFORMATION_EXCEPTIONS;
6388 		++num_pages_supported;
6389 		*page_ptr++ = PAGE_CODE_SMART_READ_DATA;
6390 		++num_pages_supported;
6391 	}
6392 
6393 	lpp->param_len = num_pages_supported;
6394 
6395 	return ((&lpp->param_values[0] - (uint8_t *)lpp) +
6396 	    num_pages_supported);
6397 }
6398 
6399 /*
6400  * sata_build_lsense_page_10() is used to create the
6401  * SCSI LOG SENSE page 0x10 (self-test results)
6402  *
6403  * Takes a sata_drive_info t * and the address of a buffer
6404  * in which to create the page information as well as a sata_hba_inst_t *.
6405  *
6406  * Returns the number of bytes valid in the buffer.
6407  */
6408 static	int
6409 sata_build_lsense_page_10(
6410 	sata_drive_info_t *sdinfo,
6411 	uint8_t *buf,
6412 	sata_hba_inst_t *sata_hba_inst)
6413 {
6414 	struct log_parameter *lpp = (struct log_parameter *)buf;
6415 	int rval;
6416 
6417 	if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48) {
6418 		struct smart_ext_selftest_log *ext_selftest_log;
6419 
6420 		ext_selftest_log = kmem_zalloc(
6421 		    sizeof (struct smart_ext_selftest_log), KM_SLEEP);
6422 
6423 		rval = sata_ext_smart_selftest_read_log(sata_hba_inst, sdinfo,
6424 		    ext_selftest_log, 0);
6425 		if (rval == 0) {
6426 			int index, start_index;
6427 			struct smart_ext_selftest_log_entry *entry;
6428 			static const struct smart_ext_selftest_log_entry empty =
6429 			    {0};
6430 			uint16_t block_num;
6431 			int count;
6432 			boolean_t only_one_block = B_FALSE;
6433 
6434 			index = ext_selftest_log->
6435 			    smart_ext_selftest_log_index[0];
6436 			index |= ext_selftest_log->
6437 			    smart_ext_selftest_log_index[1] << 8;
6438 			if (index == 0)
6439 				goto out;
6440 
6441 			--index;	/* Correct for 0 origin */
6442 			start_index = index;	/* remember where we started */
6443 			block_num = index / ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6444 			if (block_num != 0) {
6445 				rval = sata_ext_smart_selftest_read_log(
6446 				    sata_hba_inst, sdinfo, ext_selftest_log,
6447 				    block_num);
6448 				if (rval != 0)
6449 					goto out;
6450 			}
6451 			index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6452 			entry =
6453 			    &ext_selftest_log->
6454 			    smart_ext_selftest_log_entries[index];
6455 
6456 			for (count = 1;
6457 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
6458 			    ++count) {
6459 				uint8_t status;
6460 				uint8_t code;
6461 				uint8_t sense_key;
6462 				uint8_t add_sense_code;
6463 				uint8_t add_sense_code_qual;
6464 
6465 				/* If this is an unused entry, we are done */
6466 				if (bcmp(entry, &empty, sizeof (empty)) == 0) {
6467 					/* Broken firmware on some disks */
6468 					if (index + 1 ==
6469 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK) {
6470 						--entry;
6471 						--index;
6472 						if (bcmp(entry, &empty,
6473 						    sizeof (empty)) == 0)
6474 							goto out;
6475 					} else
6476 						goto out;
6477 				}
6478 
6479 				if (only_one_block &&
6480 				    start_index == index)
6481 					goto out;
6482 
6483 				lpp->param_code[0] = 0;
6484 				lpp->param_code[1] = count;
6485 				lpp->param_ctrl_flags =
6486 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
6487 				lpp->param_len =
6488 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
6489 
6490 				status = entry->smart_ext_selftest_log_status;
6491 				status >>= 4;
6492 				switch (status) {
6493 				case 0:
6494 				default:
6495 					sense_key = KEY_NO_SENSE;
6496 					add_sense_code =
6497 					    SD_SCSI_ASC_NO_ADD_SENSE;
6498 					add_sense_code_qual = 0;
6499 					break;
6500 				case 1:
6501 					sense_key = KEY_ABORTED_COMMAND;
6502 					add_sense_code =
6503 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6504 					add_sense_code_qual = SCSI_COMPONENT_81;
6505 					break;
6506 				case 2:
6507 					sense_key = KEY_ABORTED_COMMAND;
6508 					add_sense_code =
6509 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6510 					add_sense_code_qual = SCSI_COMPONENT_82;
6511 					break;
6512 				case 3:
6513 					sense_key = KEY_ABORTED_COMMAND;
6514 					add_sense_code =
6515 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6516 					add_sense_code_qual = SCSI_COMPONENT_83;
6517 					break;
6518 				case 4:
6519 					sense_key = KEY_HARDWARE_ERROR;
6520 					add_sense_code =
6521 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6522 					add_sense_code_qual = SCSI_COMPONENT_84;
6523 					break;
6524 				case 5:
6525 					sense_key = KEY_HARDWARE_ERROR;
6526 					add_sense_code =
6527 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6528 					add_sense_code_qual = SCSI_COMPONENT_85;
6529 					break;
6530 				case 6:
6531 					sense_key = KEY_HARDWARE_ERROR;
6532 					add_sense_code =
6533 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6534 					add_sense_code_qual = SCSI_COMPONENT_86;
6535 					break;
6536 				case 7:
6537 					sense_key = KEY_MEDIUM_ERROR;
6538 					add_sense_code =
6539 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6540 					add_sense_code_qual = SCSI_COMPONENT_87;
6541 					break;
6542 				case 8:
6543 					sense_key = KEY_HARDWARE_ERROR;
6544 					add_sense_code =
6545 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6546 					add_sense_code_qual = SCSI_COMPONENT_88;
6547 					break;
6548 				}
6549 				code = 0;	/* unspecified */
6550 				status |= (code << 4);
6551 				lpp->param_values[0] = status;
6552 				lpp->param_values[1] = 0; /* unspecified */
6553 				lpp->param_values[2] = entry->
6554 				    smart_ext_selftest_log_timestamp[1];
6555 				lpp->param_values[3] = entry->
6556 				    smart_ext_selftest_log_timestamp[0];
6557 				if (status != 0) {
6558 					lpp->param_values[4] = 0;
6559 					lpp->param_values[5] = 0;
6560 					lpp->param_values[6] = entry->
6561 					    smart_ext_selftest_log_failing_lba
6562 					    [5];
6563 					lpp->param_values[7] = entry->
6564 					    smart_ext_selftest_log_failing_lba
6565 					    [4];
6566 					lpp->param_values[8] = entry->
6567 					    smart_ext_selftest_log_failing_lba
6568 					    [3];
6569 					lpp->param_values[9] = entry->
6570 					    smart_ext_selftest_log_failing_lba
6571 					    [2];
6572 					lpp->param_values[10] = entry->
6573 					    smart_ext_selftest_log_failing_lba
6574 					    [1];
6575 					lpp->param_values[11] = entry->
6576 					    smart_ext_selftest_log_failing_lba
6577 					    [0];
6578 				} else {	/* No bad block address */
6579 					lpp->param_values[4] = 0xff;
6580 					lpp->param_values[5] = 0xff;
6581 					lpp->param_values[6] = 0xff;
6582 					lpp->param_values[7] = 0xff;
6583 					lpp->param_values[8] = 0xff;
6584 					lpp->param_values[9] = 0xff;
6585 					lpp->param_values[10] = 0xff;
6586 					lpp->param_values[11] = 0xff;
6587 				}
6588 
6589 				lpp->param_values[12] = sense_key;
6590 				lpp->param_values[13] = add_sense_code;
6591 				lpp->param_values[14] = add_sense_code_qual;
6592 				lpp->param_values[15] = 0; /* undefined */
6593 
6594 				lpp = (struct log_parameter *)
6595 				    (((uint8_t *)lpp) +
6596 				    SCSI_LOG_PARAM_HDR_LEN +
6597 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
6598 
6599 				--index;	/* Back up to previous entry */
6600 				if (index < 0) {
6601 					if (block_num > 0) {
6602 						--block_num;
6603 					} else {
6604 						struct read_log_ext_directory
6605 						    logdir;
6606 
6607 						rval =
6608 						    sata_read_log_ext_directory(
6609 						    sata_hba_inst, sdinfo,
6610 						    &logdir);
6611 						if (rval == -1)
6612 							goto out;
6613 						if ((logdir.read_log_ext_vers
6614 						    [0] == 0) &&
6615 						    (logdir.read_log_ext_vers
6616 						    [1] == 0))
6617 							goto out;
6618 						block_num =
6619 						    logdir.read_log_ext_nblks
6620 						    [EXT_SMART_SELFTEST_LOG_PAGE
6621 						    - 1][0];
6622 						block_num |= logdir.
6623 						    read_log_ext_nblks
6624 						    [EXT_SMART_SELFTEST_LOG_PAGE
6625 						    - 1][1] << 8;
6626 						--block_num;
6627 						only_one_block =
6628 						    (block_num == 0);
6629 					}
6630 					rval = sata_ext_smart_selftest_read_log(
6631 					    sata_hba_inst, sdinfo,
6632 					    ext_selftest_log, block_num);
6633 					if (rval != 0)
6634 						goto out;
6635 
6636 					index =
6637 					    ENTRIES_PER_EXT_SELFTEST_LOG_BLK -
6638 					    1;
6639 				}
6640 				index %= ENTRIES_PER_EXT_SELFTEST_LOG_BLK;
6641 				entry = &ext_selftest_log->
6642 				    smart_ext_selftest_log_entries[index];
6643 			}
6644 		}
6645 out:
6646 		kmem_free(ext_selftest_log,
6647 		    sizeof (struct smart_ext_selftest_log));
6648 	} else {
6649 		struct smart_selftest_log *selftest_log;
6650 
6651 		selftest_log = kmem_zalloc(sizeof (struct smart_selftest_log),
6652 		    KM_SLEEP);
6653 
6654 		rval = sata_smart_selftest_log(sata_hba_inst, sdinfo,
6655 		    selftest_log);
6656 
6657 		if (rval == 0) {
6658 			int index;
6659 			int count;
6660 			struct smart_selftest_log_entry *entry;
6661 			static const struct smart_selftest_log_entry empty =
6662 			    { 0 };
6663 
6664 			index = selftest_log->smart_selftest_log_index;
6665 			if (index == 0)
6666 				goto done;
6667 			--index;	/* Correct for 0 origin */
6668 			entry = &selftest_log->
6669 			    smart_selftest_log_entries[index];
6670 			for (count = 1;
6671 			    count <= SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS;
6672 			    ++count) {
6673 				uint8_t status;
6674 				uint8_t code;
6675 				uint8_t sense_key;
6676 				uint8_t add_sense_code;
6677 				uint8_t add_sense_code_qual;
6678 
6679 				if (bcmp(entry, &empty, sizeof (empty)) == 0)
6680 					goto done;
6681 
6682 				lpp->param_code[0] = 0;
6683 				lpp->param_code[1] = count;
6684 				lpp->param_ctrl_flags =
6685 				    LOG_CTRL_LP | LOG_CTRL_LBIN;
6686 				lpp->param_len =
6687 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN;
6688 
6689 				status = entry->smart_selftest_log_status;
6690 				status >>= 4;
6691 				switch (status) {
6692 				case 0:
6693 				default:
6694 					sense_key = KEY_NO_SENSE;
6695 					add_sense_code =
6696 					    SD_SCSI_ASC_NO_ADD_SENSE;
6697 					break;
6698 				case 1:
6699 					sense_key = KEY_ABORTED_COMMAND;
6700 					add_sense_code =
6701 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6702 					add_sense_code_qual = SCSI_COMPONENT_81;
6703 					break;
6704 				case 2:
6705 					sense_key = KEY_ABORTED_COMMAND;
6706 					add_sense_code =
6707 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6708 					add_sense_code_qual = SCSI_COMPONENT_82;
6709 					break;
6710 				case 3:
6711 					sense_key = KEY_ABORTED_COMMAND;
6712 					add_sense_code =
6713 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6714 					add_sense_code_qual = SCSI_COMPONENT_83;
6715 					break;
6716 				case 4:
6717 					sense_key = KEY_HARDWARE_ERROR;
6718 					add_sense_code =
6719 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6720 					add_sense_code_qual = SCSI_COMPONENT_84;
6721 					break;
6722 				case 5:
6723 					sense_key = KEY_HARDWARE_ERROR;
6724 					add_sense_code =
6725 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6726 					add_sense_code_qual = SCSI_COMPONENT_85;
6727 					break;
6728 				case 6:
6729 					sense_key = KEY_HARDWARE_ERROR;
6730 					add_sense_code =
6731 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6732 					add_sense_code_qual = SCSI_COMPONENT_86;
6733 					break;
6734 				case 7:
6735 					sense_key = KEY_MEDIUM_ERROR;
6736 					add_sense_code =
6737 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6738 					add_sense_code_qual = SCSI_COMPONENT_87;
6739 					break;
6740 				case 8:
6741 					sense_key = KEY_HARDWARE_ERROR;
6742 					add_sense_code =
6743 					    DIAGNOSTIC_FAILURE_ON_COMPONENT;
6744 					add_sense_code_qual = SCSI_COMPONENT_88;
6745 					break;
6746 				}
6747 				code = 0;	/* unspecified */
6748 				status |= (code << 4);
6749 				lpp->param_values[0] = status;
6750 				lpp->param_values[1] = 0; /* unspecified */
6751 				lpp->param_values[2] = entry->
6752 				    smart_selftest_log_timestamp[1];
6753 				lpp->param_values[3] = entry->
6754 				    smart_selftest_log_timestamp[0];
6755 				if (status != 0) {
6756 					lpp->param_values[4] = 0;
6757 					lpp->param_values[5] = 0;
6758 					lpp->param_values[6] = 0;
6759 					lpp->param_values[7] = 0;
6760 					lpp->param_values[8] = entry->
6761 					    smart_selftest_log_failing_lba[3];
6762 					lpp->param_values[9] = entry->
6763 					    smart_selftest_log_failing_lba[2];
6764 					lpp->param_values[10] = entry->
6765 					    smart_selftest_log_failing_lba[1];
6766 					lpp->param_values[11] = entry->
6767 					    smart_selftest_log_failing_lba[0];
6768 				} else {	/* No block address */
6769 					lpp->param_values[4] = 0xff;
6770 					lpp->param_values[5] = 0xff;
6771 					lpp->param_values[6] = 0xff;
6772 					lpp->param_values[7] = 0xff;
6773 					lpp->param_values[8] = 0xff;
6774 					lpp->param_values[9] = 0xff;
6775 					lpp->param_values[10] = 0xff;
6776 					lpp->param_values[11] = 0xff;
6777 				}
6778 				lpp->param_values[12] = sense_key;
6779 				lpp->param_values[13] = add_sense_code;
6780 				lpp->param_values[14] = add_sense_code_qual;
6781 				lpp->param_values[15] = 0; /* undefined */
6782 
6783 				lpp = (struct log_parameter *)
6784 				    (((uint8_t *)lpp) +
6785 				    SCSI_LOG_PARAM_HDR_LEN +
6786 				    SCSI_LOG_SENSE_SELFTEST_PARAM_LEN);
6787 				--index;	/* back up to previous entry */
6788 				if (index < 0) {
6789 					index =
6790 					    NUM_SMART_SELFTEST_LOG_ENTRIES - 1;
6791 				}
6792 				entry = &selftest_log->
6793 				    smart_selftest_log_entries[index];
6794 			}
6795 		}
6796 done:
6797 		kmem_free(selftest_log, sizeof (struct smart_selftest_log));
6798 	}
6799 
6800 	return ((SCSI_LOG_PARAM_HDR_LEN + SCSI_LOG_SENSE_SELFTEST_PARAM_LEN) *
6801 	    SCSI_ENTRIES_IN_LOG_SENSE_SELFTEST_RESULTS);
6802 }
6803 
6804 /*
6805  * sata_build_lsense_page_2f() is used to create the
6806  * SCSI LOG SENSE page 0x10 (informational exceptions)
6807  *
6808  * Takes a sata_drive_info t * and the address of a buffer
6809  * in which to create the page information as well as a sata_hba_inst_t *.
6810  *
6811  * Returns the number of bytes valid in the buffer.
6812  */
6813 static	int
6814 sata_build_lsense_page_2f(
6815 	sata_drive_info_t *sdinfo,
6816 	uint8_t *buf,
6817 	sata_hba_inst_t *sata_hba_inst)
6818 {
6819 	struct log_parameter *lpp = (struct log_parameter *)buf;
6820 	int rval;
6821 	uint8_t *smart_data;
6822 	uint8_t temp;
6823 	sata_id_t *sata_id;
6824 #define	SMART_NO_TEMP	0xff
6825 
6826 	lpp->param_code[0] = 0;
6827 	lpp->param_code[1] = 0;
6828 	lpp->param_ctrl_flags = LOG_CTRL_LP | LOG_CTRL_LBIN;
6829 
6830 	/* Now get the SMART status w.r.t. threshold exceeded */
6831 	rval = sata_fetch_smart_return_status(sata_hba_inst, sdinfo);
6832 	switch (rval) {
6833 	case 1:
6834 		lpp->param_values[0] = SCSI_PREDICTED_FAILURE;
6835 		lpp->param_values[1] = SCSI_GENERAL_HD_FAILURE;
6836 		break;
6837 	case 0:
6838 	case -1:	/* failed to get data */
6839 		lpp->param_values[0] = 0;	/* No failure predicted */
6840 		lpp->param_values[1] = 0;
6841 		break;
6842 #if defined(SATA_DEBUG)
6843 	default:
6844 		cmn_err(CE_PANIC, "sata_build_lsense_page_2f bad return value");
6845 		/* NOTREACHED */
6846 #endif
6847 	}
6848 
6849 	sata_id = &sdinfo->satadrv_id;
6850 	if (! (sata_id->ai_sctsupport & SATA_SCT_CMD_TRANS_SUP))
6851 		temp = SMART_NO_TEMP;
6852 	else {
6853 		/* Now get the temperature */
6854 		smart_data = kmem_zalloc(512, KM_SLEEP);
6855 		rval = sata_smart_read_log(sata_hba_inst, sdinfo, smart_data,
6856 		    SCT_STATUS_LOG_PAGE, 1);
6857 		if (rval == -1)
6858 			temp = SMART_NO_TEMP;
6859 		else {
6860 			temp = smart_data[200];
6861 			if (temp & 0x80) {
6862 				if (temp & 0x7f)
6863 					temp = 0;
6864 				else
6865 					temp = SMART_NO_TEMP;
6866 			}
6867 		}
6868 		kmem_free(smart_data, 512);
6869 	}
6870 
6871 	lpp->param_values[2] = temp;	/* most recent temperature */
6872 	lpp->param_values[3] = 0;	/* required vendor specific byte */
6873 
6874 	lpp->param_len = SCSI_INFO_EXCEPTIONS_PARAM_LEN;
6875 
6876 
6877 	return (SCSI_INFO_EXCEPTIONS_PARAM_LEN + SCSI_LOG_PARAM_HDR_LEN);
6878 }
6879 
6880 /*
6881  * sata_build_lsense_page_30() is used to create the
6882  * SCSI LOG SENSE page 0x30 (Sun's vendor specific page for ATA SMART data).
6883  *
6884  * Takes a sata_drive_info t * and the address of a buffer
6885  * in which to create the page information as well as a sata_hba_inst_t *.
6886  *
6887  * Returns the number of bytes valid in the buffer.
6888  */
6889 static int
6890 sata_build_lsense_page_30(
6891 	sata_drive_info_t *sdinfo,
6892 	uint8_t *buf,
6893 	sata_hba_inst_t *sata_hba_inst)
6894 {
6895 	struct smart_data *smart_data = (struct smart_data *)buf;
6896 	int rval;
6897 
6898 	/* Now do the SMART READ DATA */
6899 	rval = sata_fetch_smart_data(sata_hba_inst, sdinfo, smart_data);
6900 	if (rval == -1)
6901 		return (0);
6902 
6903 	return (sizeof (struct smart_data));
6904 }
6905 
6906 /* ************************** ATAPI-SPECIFIC FUNCTIONS ********************** */
6907 
6908 /*
6909  * Start command for ATAPI device.
6910  * This function processes scsi_pkt requests.
6911  * Only CD/DVD devices are supported.
6912  * Most commands are packet without any translation into Packet Command.
6913  * Some may be trapped and executed as SATA commands (not clear which one).
6914  *
6915  * Returns TRAN_ACCEPT if command is accepted for execution (or completed
6916  * execution).
6917  * Returns other TRAN_XXXX codes if command is not accepted or completed
6918  * (see return values for sata_hba_start()).
6919  *
6920  * Note:
6921  * Inquiry cdb format differs between transport version 2 and 3.
6922  * However, the transport version 3 devices that were checked did not adhere
6923  * to the specification (ignored MSB of the allocation length). Therefore,
6924  * the transport version is not checked, but Inquiry allocation length is
6925  * truncated to 255 bytes if the original allocation length set-up by the
6926  * target driver is greater than 255 bytes.
6927  */
6928 static int
6929 sata_txlt_atapi(sata_pkt_txlate_t *spx)
6930 {
6931 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
6932 	sata_cmd_t *scmd = &spx->txlt_sata_pkt->satapkt_cmd;
6933 	struct buf *bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
6934 	sata_hba_inst_t *sata_hba = SATA_TXLT_HBA_INST(spx);
6935 	sata_drive_info_t *sdinfo = sata_get_device_info(sata_hba,
6936 	    &spx->txlt_sata_pkt->satapkt_device);
6937 	int cport = SATA_TXLT_CPORT(spx);
6938 	int cdblen;
6939 	int rval;
6940 	int synch;
6941 	union scsi_cdb *cdbp = (union scsi_cdb *)scsipkt->pkt_cdbp;
6942 
6943 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
6944 
6945 	if (((rval = sata_txlt_generic_pkt_info(spx)) != TRAN_ACCEPT) ||
6946 	    (spx->txlt_scsi_pkt->pkt_reason == CMD_DEV_GONE)) {
6947 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6948 		return (rval);
6949 	}
6950 
6951 	/*
6952 	 * ATAPI device executes some ATA commands in addition to MMC command
6953 	 * set. These ATA commands may be executed by the regular SATA
6954 	 * translation functions. None needs to be captured now.
6955 	 * Other commands belong to MMC command set and are delivered
6956 	 * to ATAPI device via Packet Command.
6957 	 */
6958 
6959 	/* Check the size of cdb */
6960 	cdblen = scsi_cdb_size[GETGROUP(cdbp)];
6961 	if (cdblen > sdinfo->satadrv_atapi_cdb_len) {
6962 		sata_log(NULL, CE_WARN,
6963 		    "sata: invalid ATAPI cdb length %d",
6964 		    scsipkt->pkt_cdblen);
6965 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
6966 		return (TRAN_BADPKT);
6967 	}
6968 
6969 	SATAATAPITRACE(spx, cdblen);
6970 
6971 	/*
6972 	 * For non-read/write commands we need to
6973 	 * map buffer
6974 	 */
6975 	switch ((uint_t)scsipkt->pkt_cdbp[0]) {
6976 	case SCMD_READ:
6977 	case SCMD_READ_G1:
6978 	case SCMD_READ_G5:
6979 	case SCMD_READ_G4:
6980 	case SCMD_WRITE:
6981 	case SCMD_WRITE_G1:
6982 	case SCMD_WRITE_G5:
6983 	case SCMD_WRITE_G4:
6984 		break;
6985 	default:
6986 		if (bp != NULL) {
6987 			if (bp->b_flags & (B_PHYS | B_PAGEIO))
6988 				bp_mapin(bp);
6989 		}
6990 		break;
6991 	}
6992 	/*
6993 	 * scmd->satacmd_flags.sata_data_direction default -
6994 	 * SATA_DIR_NODATA_XFER - is set by
6995 	 * sata_txlt_generic_pkt_info().
6996 	 */
6997 	if (scmd->satacmd_bp) {
6998 		if (scmd->satacmd_bp->b_flags & B_READ) {
6999 			scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7000 		} else {
7001 			scmd->satacmd_flags.sata_data_direction =
7002 			    SATA_DIR_WRITE;
7003 		}
7004 	}
7005 
7006 	/*
7007 	 * Set up ATAPI packet command.
7008 	 */
7009 
7010 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7011 
7012 	/* Copy cdb into sata_cmd */
7013 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7014 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7015 	bcopy(cdbp, scmd->satacmd_acdb, cdblen);
7016 
7017 	/* See note in the command header */
7018 	if (scmd->satacmd_acdb[0] == SCMD_INQUIRY) {
7019 		if (scmd->satacmd_acdb[3] != 0)
7020 			scmd->satacmd_acdb[4] = 255;
7021 	}
7022 
7023 #ifdef SATA_DEBUG
7024 	if (sata_debug_flags & SATA_DBG_ATAPI) {
7025 		uint8_t *p = scmd->satacmd_acdb;
7026 		char buf[3 * SATA_ATAPI_MAX_CDB_LEN];
7027 
7028 		(void) snprintf(buf, SATA_ATAPI_MAX_CDB_LEN,
7029 		    "%02x %02x %02x %02x %02x %02x %02x %02x "
7030 		    "%2x %02x %02x %02x %02x %02x %02x %02x",
7031 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7032 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7033 		buf[(3 * SATA_ATAPI_MAX_CDB_LEN) - 1] = '\0';
7034 		cmn_err(CE_NOTE, "ATAPI cdb: %s\n", buf);
7035 	}
7036 #endif
7037 
7038 	/*
7039 	 * Preset request sense data to NO SENSE.
7040 	 * If there is no way to get error information via Request Sense,
7041 	 * the packet request sense data would not have to be modified by HBA,
7042 	 * but it could be returned as is.
7043 	 */
7044 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
7045 	sata_fixed_sense_data_preset(
7046 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7047 
7048 	if (!(spx->txlt_sata_pkt->satapkt_op_mode & SATA_OPMODE_SYNCH)) {
7049 		/* Need callback function */
7050 		spx->txlt_sata_pkt->satapkt_comp = sata_txlt_atapi_completion;
7051 		synch = FALSE;
7052 	} else
7053 		synch = TRUE;
7054 
7055 	/* Transfer command to HBA */
7056 	if (sata_hba_start(spx, &rval) != 0) {
7057 		/* Pkt not accepted for execution */
7058 		mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
7059 		return (rval);
7060 	}
7061 	mutex_exit(&SATA_CPORT_MUTEX(sata_hba, cport));
7062 	/*
7063 	 * If execution is non-synchronous,
7064 	 * a callback function will handle potential errors, translate
7065 	 * the response and will do a callback to a target driver.
7066 	 * If it was synchronous, use the same framework callback to check
7067 	 * an execution status.
7068 	 */
7069 	if (synch) {
7070 		SATADBG1(SATA_DBG_SCSI_IF, spx->txlt_sata_hba_inst,
7071 		    "synchronous execution status %x\n",
7072 		    spx->txlt_sata_pkt->satapkt_reason);
7073 		sata_txlt_atapi_completion(spx->txlt_sata_pkt);
7074 	}
7075 	return (TRAN_ACCEPT);
7076 }
7077 
7078 
7079 /*
7080  * ATAPI Packet command completion.
7081  *
7082  * Failure of the command passed via Packet command are considered device
7083  * error. SATA HBA driver would have to retrieve error data (via Request
7084  * Sense command delivered via error retrieval sata packet) and copy it
7085  * to satacmd_rqsense array. From there, it is moved into scsi pkt sense data.
7086  */
7087 static void
7088 sata_txlt_atapi_completion(sata_pkt_t *sata_pkt)
7089 {
7090 	sata_pkt_txlate_t *spx =
7091 	    (sata_pkt_txlate_t *)sata_pkt->satapkt_framework_private;
7092 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
7093 	struct scsi_extended_sense *sense;
7094 	struct buf *bp;
7095 	int rval;
7096 
7097 #ifdef SATA_DEBUG
7098 	uint8_t *rqsp = sata_pkt->satapkt_cmd.satacmd_rqsense;
7099 #endif
7100 
7101 	scsipkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
7102 	    STATE_SENT_CMD | STATE_GOT_STATUS;
7103 
7104 	if (sata_pkt->satapkt_reason == SATA_PKT_COMPLETED) {
7105 		/* Normal completion */
7106 		if (sata_pkt->satapkt_cmd.satacmd_bp != NULL)
7107 			scsipkt->pkt_state |= STATE_XFERRED_DATA;
7108 		scsipkt->pkt_reason = CMD_CMPLT;
7109 		*scsipkt->pkt_scbp = STATUS_GOOD;
7110 		if (spx->txlt_tmp_buf != NULL) {
7111 			/* Temporary buffer was used */
7112 			bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
7113 			if (bp->b_flags & B_READ) {
7114 				rval = ddi_dma_sync(
7115 				    spx->txlt_buf_dma_handle, 0, 0,
7116 				    DDI_DMA_SYNC_FORCPU);
7117 				ASSERT(rval == DDI_SUCCESS);
7118 				bcopy(spx->txlt_tmp_buf, bp->b_un.b_addr,
7119 				    bp->b_bcount);
7120 			}
7121 		}
7122 	} else {
7123 		/*
7124 		 * Something went wrong - analyze return
7125 		 */
7126 		*scsipkt->pkt_scbp = STATUS_CHECK;
7127 		sense = sata_arq_sense(spx);
7128 
7129 		if (sata_pkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7130 			scsipkt->pkt_reason = CMD_INCOMPLETE;
7131 			/*
7132 			 * We may not have ARQ data if there was a double
7133 			 * error. But sense data in sata packet was pre-set
7134 			 * with NO SENSE so it is valid even if HBA could
7135 			 * not retrieve a real sense data.
7136 			 * Just copy this sense data into scsi pkt sense area.
7137 			 */
7138 			bcopy(sata_pkt->satapkt_cmd.satacmd_rqsense, sense,
7139 			    SATA_ATAPI_MIN_RQSENSE_LEN);
7140 #ifdef SATA_DEBUG
7141 			if (sata_debug_flags & SATA_DBG_SCSI_IF) {
7142 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7143 				    "sata_txlt_atapi_completion: %02x\n"
7144 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
7145 				    "          %02x %02x %02x %02x %02x %02x "
7146 				    "          %02x %02x %02x %02x %02x %02x\n",
7147 				    scsipkt->pkt_reason,
7148 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7149 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7150 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7151 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7152 				    rqsp[16], rqsp[17]);
7153 			}
7154 #endif
7155 		} else {
7156 			switch (sata_pkt->satapkt_reason) {
7157 			case SATA_PKT_PORT_ERROR:
7158 				/*
7159 				 * We have no device data.
7160 				 */
7161 				scsipkt->pkt_reason = CMD_INCOMPLETE;
7162 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
7163 				    STATE_GOT_TARGET | STATE_SENT_CMD |
7164 				    STATE_GOT_STATUS);
7165 				sense->es_key = KEY_HARDWARE_ERROR;
7166 
7167 				/* No extended sense key - no info available */
7168 				scsipkt->pkt_reason = CMD_INCOMPLETE;
7169 				break;
7170 
7171 			case SATA_PKT_TIMEOUT:
7172 				/* scsipkt->pkt_reason = CMD_TIMEOUT; */
7173 				/* No extended sense key */
7174 				/*
7175 				 * Need to check if HARDWARE_ERROR/
7176 				 * TIMEOUT_ON_LOGICAL_UNIT 4/3E/2 would be more
7177 				 * appropriate.
7178 				 */
7179 				break;
7180 
7181 			case SATA_PKT_ABORTED:
7182 				scsipkt->pkt_reason = CMD_ABORTED;
7183 				/* Should we set key COMMAND_ABPRTED? */
7184 				break;
7185 
7186 			case SATA_PKT_RESET:
7187 				scsipkt->pkt_reason = CMD_RESET;
7188 				/*
7189 				 * May be we should set Unit Attention /
7190 				 * Reset. Perhaps the same should be
7191 				 * returned for disks....
7192 				 */
7193 				sense->es_key = KEY_UNIT_ATTENTION;
7194 				sense->es_add_code = SD_SCSI_ASC_RESET;
7195 				break;
7196 
7197 			default:
7198 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
7199 				    "sata_txlt_atapi_completion: "
7200 				    "invalid packet completion reason"));
7201 				scsipkt->pkt_reason = CMD_TRAN_ERR;
7202 				scsipkt->pkt_state &= ~(STATE_GOT_BUS |
7203 				    STATE_GOT_TARGET | STATE_SENT_CMD |
7204 				    STATE_GOT_STATUS);
7205 				break;
7206 			}
7207 		}
7208 	}
7209 
7210 	SATAATAPITRACE(spx, 0);
7211 
7212 	if ((scsipkt->pkt_flags & FLAG_NOINTR) == 0 &&
7213 	    scsipkt->pkt_comp != NULL) {
7214 		/* scsi callback required */
7215 		(*scsipkt->pkt_comp)(scsipkt);
7216 	}
7217 }
7218 
7219 /*
7220  * Set up error retrieval sata command for ATAPI Packet Command error data
7221  * recovery.
7222  *
7223  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
7224  * returns SATA_FAILURE otherwise.
7225  */
7226 
7227 static int
7228 sata_atapi_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
7229 {
7230 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
7231 	sata_cmd_t *scmd;
7232 	struct buf *bp;
7233 
7234 	/*
7235 	 * Allocate dma-able buffer error data.
7236 	 * Buffer allocation will take care of buffer alignment and other DMA
7237 	 * attributes.
7238 	 */
7239 	bp = sata_alloc_local_buffer(spx, SATA_ATAPI_MIN_RQSENSE_LEN);
7240 	if (bp == NULL) {
7241 		SATADBG1(SATA_DBG_ATAPI, spx->txlt_sata_hba_inst,
7242 		    "sata_get_err_retrieval_pkt: "
7243 		    "cannot allocate buffer for error data", NULL);
7244 		return (SATA_FAILURE);
7245 	}
7246 	bp_mapin(bp); /* make data buffer accessible */
7247 
7248 	/* Operation modes are up to the caller */
7249 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7250 
7251 	/* Synchronous mode, no callback - may be changed by the caller */
7252 	spkt->satapkt_comp = NULL;
7253 	spkt->satapkt_time = sata_default_pkt_time;
7254 
7255 	scmd = &spkt->satapkt_cmd;
7256 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7257 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7258 
7259 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7260 
7261 	/*
7262 	 * Set-up acdb. Request Sense CDB (packet command content) is
7263 	 * not in DMA-able buffer. Its handling is HBA-specific (how
7264 	 * it is transfered into packet FIS).
7265 	 */
7266 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7267 	bcopy(sata_rqsense_cdb, scmd->satacmd_acdb, SATA_ATAPI_RQSENSE_CDB_LEN);
7268 	/* Following zeroing of pad bytes may not be necessary */
7269 	bzero(&scmd->satacmd_acdb[SATA_ATAPI_RQSENSE_CDB_LEN],
7270 	    sdinfo->satadrv_atapi_cdb_len - SATA_ATAPI_RQSENSE_CDB_LEN);
7271 
7272 	/*
7273 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
7274 	 * before accessing it. Handle is in usual place in translate struct.
7275 	 */
7276 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
7277 
7278 	/*
7279 	 * Preset request sense data to NO SENSE.
7280 	 * Here it is redundant, only for a symetry with scsi-originated
7281 	 * packets. It should not be used for anything but debugging.
7282 	 */
7283 	bzero(scmd->satacmd_rqsense, SATA_ATAPI_RQSENSE_LEN);
7284 	sata_fixed_sense_data_preset(
7285 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7286 
7287 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7288 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7289 
7290 	return (SATA_SUCCESS);
7291 }
7292 
7293 /*
7294  * Set-up ATAPI packet command.
7295  * Data transfer direction has to be set-up in sata_cmd structure prior to
7296  * calling this function.
7297  *
7298  * Returns void
7299  */
7300 
7301 static void
7302 sata_atapi_packet_cmd_setup(sata_cmd_t *scmd, sata_drive_info_t *sdinfo)
7303 {
7304 	scmd->satacmd_addr_type = 0;		/* N/A */
7305 	scmd->satacmd_sec_count_lsb = 0;	/* no tag */
7306 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
7307 	scmd->satacmd_lba_mid_lsb = (uint8_t)SATA_ATAPI_MAX_BYTES_PER_DRQ;
7308 	scmd->satacmd_lba_high_lsb =
7309 	    (uint8_t)(SATA_ATAPI_MAX_BYTES_PER_DRQ >> 8);
7310 	scmd->satacmd_cmd_reg = SATAC_PACKET;	/* Command */
7311 
7312 	/*
7313 	 * We want all data to be transfered via DMA.
7314 	 * But specify it only if drive supports DMA and DMA mode is
7315 	 * selected - some drives are sensitive about it.
7316 	 * Hopefully it wil work for all drives....
7317 	 */
7318 	if (sdinfo->satadrv_settings & SATA_DEV_DMA)
7319 		scmd->satacmd_features_reg = SATA_ATAPI_F_DMA;
7320 
7321 	/*
7322 	 * Features register requires special care for devices that use
7323 	 * Serial ATA bridge - they need an explicit specification of
7324 	 * the data transfer direction for Packet DMA commands.
7325 	 * Setting this bit is harmless if DMA is not used.
7326 	 *
7327 	 * Many drives do not implement word 80, specifying what ATA/ATAPI
7328 	 * spec they follow.
7329 	 * We are arbitrarily following the latest SerialATA 2.6 spec,
7330 	 * which uses ATA/ATAPI 6 specification for Identify Data, unless
7331 	 * ATA/ATAPI-7 support is explicitly indicated.
7332 	 */
7333 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
7334 	    sdinfo->satadrv_id.ai_majorversion != 0xffff &&
7335 	    (sdinfo->satadrv_id.ai_majorversion & SATA_MAJVER_7) != 0) {
7336 		/*
7337 		 * Specification of major version is valid and version 7
7338 		 * is supported. It does automatically imply that all
7339 		 * spec features are supported. For now, we assume that
7340 		 * DMADIR setting is valid. ATA/ATAPI7 spec is incomplete.
7341 		 */
7342 		if ((sdinfo->satadrv_id.ai_dirdma &
7343 		    SATA_ATAPI_ID_DMADIR_REQ) != 0) {
7344 			if (scmd->satacmd_flags.sata_data_direction ==
7345 			    SATA_DIR_READ)
7346 			scmd->satacmd_features_reg |=
7347 			    SATA_ATAPI_F_DATA_DIR_READ;
7348 		}
7349 	}
7350 }
7351 
7352 
7353 #ifdef SATA_DEBUG
7354 
7355 /* Display 18 bytes of Inquiry data */
7356 static void
7357 sata_show_inqry_data(uint8_t *buf)
7358 {
7359 	struct scsi_inquiry *inq = (struct scsi_inquiry *)buf;
7360 	uint8_t *p;
7361 
7362 	cmn_err(CE_NOTE, "Inquiry data:");
7363 	cmn_err(CE_NOTE, "device type %x", inq->inq_dtype);
7364 	cmn_err(CE_NOTE, "removable media %x", inq->inq_rmb);
7365 	cmn_err(CE_NOTE, "version %x", inq->inq_ansi);
7366 	cmn_err(CE_NOTE, "ATAPI transport version %d",
7367 	    SATA_ATAPI_TRANS_VERSION(inq));
7368 	cmn_err(CE_NOTE, "response data format %d, aenc %d",
7369 	    inq->inq_rdf, inq->inq_aenc);
7370 	cmn_err(CE_NOTE, " additional length %d", inq->inq_len);
7371 	cmn_err(CE_NOTE, "tpgs %d", inq->inq_tpgs);
7372 	p = (uint8_t *)inq->inq_vid;
7373 	cmn_err(CE_NOTE, "vendor id (binary): %02x %02x %02x %02x "
7374 	    "%02x %02x %02x %02x",
7375 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
7376 	p = (uint8_t *)inq->inq_vid;
7377 	cmn_err(CE_NOTE, "vendor id: %c %c %c %c %c %c %c %c",
7378 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
7379 
7380 	p = (uint8_t *)inq->inq_pid;
7381 	cmn_err(CE_NOTE, "product id (binary): %02x %02x %02x %02x "
7382 	    "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
7383 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7384 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7385 	p = (uint8_t *)inq->inq_pid;
7386 	cmn_err(CE_NOTE, "product id: %c %c %c %c %c %c %c %c "
7387 	    "%c %c %c %c %c %c %c %c",
7388 	    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
7389 	    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
7390 
7391 	p = (uint8_t *)inq->inq_revision;
7392 	cmn_err(CE_NOTE, "revision (binary): %02x %02x %02x %02x",
7393 	    p[0], p[1], p[2], p[3]);
7394 	p = (uint8_t *)inq->inq_revision;
7395 	cmn_err(CE_NOTE, "revision: %c %c %c %c",
7396 	    p[0], p[1], p[2], p[3]);
7397 
7398 }
7399 
7400 
7401 static void
7402 sata_save_atapi_trace(sata_pkt_txlate_t *spx, int count)
7403 {
7404 	struct scsi_pkt *scsi_pkt = spx->txlt_scsi_pkt;
7405 
7406 	if (scsi_pkt == NULL)
7407 		return;
7408 	if (count != 0) {
7409 		/* saving cdb */
7410 		bzero(sata_atapi_trace[sata_atapi_trace_index].acdb,
7411 		    SATA_ATAPI_MAX_CDB_LEN);
7412 		bcopy(scsi_pkt->pkt_cdbp,
7413 		    sata_atapi_trace[sata_atapi_trace_index].acdb, count);
7414 	} else {
7415 		bcopy(&((struct scsi_arq_status *)scsi_pkt->pkt_scbp)->
7416 		    sts_sensedata,
7417 		    sata_atapi_trace[sata_atapi_trace_index].arqs,
7418 		    SATA_ATAPI_MIN_RQSENSE_LEN);
7419 		sata_atapi_trace[sata_atapi_trace_index].scsi_pkt_reason =
7420 		    scsi_pkt->pkt_reason;
7421 		sata_atapi_trace[sata_atapi_trace_index].sata_pkt_reason =
7422 		    spx->txlt_sata_pkt->satapkt_reason;
7423 
7424 		if (++sata_atapi_trace_index >= 64)
7425 			sata_atapi_trace_index = 0;
7426 	}
7427 }
7428 
7429 #endif
7430 
7431 /*
7432  * Fetch inquiry data from ATAPI device
7433  * Returns SATA_SUCCESS if operation was successfull, SATA_FAILURE otherwise.
7434  *
7435  * Note:
7436  * inqb pointer does not point to a DMA-able buffer. It is a local buffer
7437  * where the caller expects to see the inquiry data.
7438  *
7439  */
7440 
7441 static int
7442 sata_get_atapi_inquiry_data(sata_hba_inst_t *sata_hba,
7443     sata_address_t *saddr, struct scsi_inquiry *inq)
7444 {
7445 	sata_pkt_txlate_t *spx;
7446 	sata_pkt_t *spkt;
7447 	struct buf *bp;
7448 	sata_drive_info_t *sdinfo;
7449 	sata_cmd_t *scmd;
7450 	int rval;
7451 	uint8_t *rqsp;
7452 #ifdef SATA_DEBUG
7453 	char msg_buf[MAXPATHLEN];
7454 #endif
7455 
7456 	ASSERT(sata_hba != NULL);
7457 
7458 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
7459 	spx->txlt_sata_hba_inst = sata_hba;
7460 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
7461 	spkt = sata_pkt_alloc(spx, NULL);
7462 	if (spkt == NULL) {
7463 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7464 		return (SATA_FAILURE);
7465 	}
7466 	/* address is needed now */
7467 	spkt->satapkt_device.satadev_addr = *saddr;
7468 
7469 	/* scsi_inquiry size buffer */
7470 	bp = sata_alloc_local_buffer(spx, sizeof (struct scsi_inquiry));
7471 	if (bp == NULL) {
7472 		sata_pkt_free(spx);
7473 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7474 		SATA_LOG_D((sata_hba, CE_WARN,
7475 		    "sata_get_atapi_inquiry_data: "
7476 		    "cannot allocate data buffer"));
7477 		return (SATA_FAILURE);
7478 	}
7479 	bp_mapin(bp); /* make data buffer accessible */
7480 
7481 	scmd = &spkt->satapkt_cmd;
7482 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7483 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7484 
7485 	/* Use synchronous mode */
7486 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7487 	spkt->satapkt_comp = NULL;
7488 	spkt->satapkt_time = sata_default_pkt_time;
7489 
7490 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
7491 
7492 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7493 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7494 
7495 	mutex_enter(&(SATA_TXLT_CPORT_MUTEX(spx)));
7496 	sdinfo = sata_get_device_info(sata_hba,
7497 	    &spx->txlt_sata_pkt->satapkt_device);
7498 	if (sdinfo == NULL) {
7499 		/* we have to be carefull about the disapearing device */
7500 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7501 		rval = SATA_FAILURE;
7502 		goto cleanup;
7503 	}
7504 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7505 
7506 	/*
7507 	 * Set-up acdb. This works for atapi transport version 2 and later.
7508 	 */
7509 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7510 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7511 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
7512 	scmd->satacmd_acdb[1] = 0x00;
7513 	scmd->satacmd_acdb[2] = 0x00;
7514 	scmd->satacmd_acdb[3] = 0x00;
7515 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
7516 	scmd->satacmd_acdb[5] = 0x00;
7517 
7518 	sata_fixed_sense_data_preset(
7519 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7520 
7521 	/* Transfer command to HBA */
7522 	if (sata_hba_start(spx, &rval) != 0) {
7523 		/* Pkt not accepted for execution */
7524 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
7525 		    "sata_get_atapi_inquiry_data: "
7526 		    "Packet not accepted for execution - ret: %02x", rval);
7527 		mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7528 		rval = SATA_FAILURE;
7529 		goto cleanup;
7530 	}
7531 	mutex_exit(&(SATA_TXLT_CPORT_MUTEX(spx)));
7532 
7533 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
7534 		SATADBG1(SATA_DBG_ATAPI, sata_hba,
7535 		    "sata_get_atapi_inquiry_data: "
7536 		    "Packet completed successfully - ret: %02x", rval);
7537 		/*
7538 		 * Sync buffer. Handle is in usual place in translate struct.
7539 		 * Normal completion - copy data into caller's buffer
7540 		 */
7541 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
7542 		    DDI_DMA_SYNC_FORCPU);
7543 		ASSERT(rval == DDI_SUCCESS);
7544 		bcopy(bp->b_un.b_addr, (uint8_t *)inq,
7545 		    sizeof (struct scsi_inquiry));
7546 #ifdef SATA_DEBUG
7547 		if (sata_debug_flags & SATA_DBG_ATAPI) {
7548 			sata_show_inqry_data((uint8_t *)inq);
7549 		}
7550 #endif
7551 		rval = SATA_SUCCESS;
7552 	} else {
7553 		/*
7554 		 * Something went wrong - analyze return - check rqsense data
7555 		 */
7556 		rval = SATA_FAILURE;
7557 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7558 			/*
7559 			 * ARQ data hopefull show something other than NO SENSE
7560 			 */
7561 			rqsp = scmd->satacmd_rqsense;
7562 #ifdef SATA_DEBUG
7563 			if (sata_debug_flags & SATA_DBG_ATAPI) {
7564 				msg_buf[0] = '\0';
7565 				(void) snprintf(msg_buf, MAXPATHLEN,
7566 				    "ATAPI packet completion reason: %02x\n"
7567 				    "RQSENSE:  %02x %02x %02x %02x %02x %02x\n"
7568 				    "          %02x %02x %02x %02x %02x %02x\n"
7569 				    "          %02x %02x %02x %02x %02x %02x",
7570 				    spkt->satapkt_reason,
7571 				    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7572 				    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7573 				    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7574 				    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7575 				    rqsp[16], rqsp[17]);
7576 				sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7577 				    "%s", msg_buf);
7578 			}
7579 #endif
7580 		} else {
7581 			switch (spkt->satapkt_reason) {
7582 			case SATA_PKT_PORT_ERROR:
7583 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7584 				    "sata_get_atapi_inquiry_data: "
7585 				    "packet reason: port error", NULL);
7586 				break;
7587 
7588 			case SATA_PKT_TIMEOUT:
7589 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7590 				    "sata_get_atapi_inquiry_data: "
7591 				    "packet reason: timeout", NULL);
7592 				break;
7593 
7594 			case SATA_PKT_ABORTED:
7595 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7596 				    "sata_get_atapi_inquiry_data: "
7597 				    "packet reason: aborted", NULL);
7598 				break;
7599 
7600 			case SATA_PKT_RESET:
7601 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7602 				    "sata_get_atapi_inquiry_data: "
7603 				    "packet reason: reset\n", NULL);
7604 				break;
7605 			default:
7606 				SATADBG1(SATA_DBG_ATAPI, sata_hba,
7607 				    "sata_get_atapi_inquiry_data: "
7608 				    "invalid packet reason: %02x\n",
7609 				    spkt->satapkt_reason);
7610 				break;
7611 			}
7612 		}
7613 	}
7614 cleanup:
7615 	sata_free_local_buffer(spx);
7616 	sata_pkt_free(spx);
7617 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
7618 	return (rval);
7619 }
7620 
7621 
7622 
7623 
7624 
7625 #if 0
7626 #ifdef SATA_DEBUG
7627 
7628 /*
7629  * Test ATAPI packet command.
7630  * Single threaded test: send packet command in synch mode, process completion
7631  *
7632  */
7633 static void
7634 sata_test_atapi_packet_command(sata_hba_inst_t *sata_hba_inst, int cport)
7635 {
7636 	sata_pkt_txlate_t *spx;
7637 	sata_pkt_t *spkt;
7638 	struct buf *bp;
7639 	sata_device_t sata_device;
7640 	sata_drive_info_t *sdinfo;
7641 	sata_cmd_t *scmd;
7642 	int rval;
7643 	uint8_t *rqsp;
7644 
7645 	ASSERT(sata_hba_inst != NULL);
7646 	sata_device.satadev_addr.cport = cport;
7647 	sata_device.satadev_addr.pmport = 0;
7648 	sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
7649 	sata_device.satadev_rev = SATA_DEVICE_REV;
7650 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7651 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
7652 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7653 	if (sdinfo == NULL) {
7654 		sata_log(sata_hba_inst, CE_WARN,
7655 		    "sata_test_atapi_packet_command: "
7656 		    "no device info for cport %d",
7657 		    sata_device.satadev_addr.cport);
7658 		return;
7659 	}
7660 
7661 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
7662 	spx->txlt_sata_hba_inst = sata_hba_inst;
7663 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
7664 	spkt = sata_pkt_alloc(spx, NULL);
7665 	if (spkt == NULL) {
7666 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7667 		return;
7668 	}
7669 	/* address is needed now */
7670 	spkt->satapkt_device.satadev_addr = sata_device.satadev_addr;
7671 
7672 	/* 1024k buffer */
7673 	bp = sata_alloc_local_buffer(spx, 1024);
7674 	if (bp == NULL) {
7675 		sata_pkt_free(spx);
7676 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
7677 		sata_log(sata_hba_inst, CE_WARN,
7678 		    "sata_test_atapi_packet_command: "
7679 		    "cannot allocate data buffer");
7680 		return;
7681 	}
7682 	bp_mapin(bp); /* make data buffer accessible */
7683 
7684 	scmd = &spkt->satapkt_cmd;
7685 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
7686 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
7687 
7688 	/* Use synchronous mode */
7689 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
7690 
7691 	/* Synchronous mode, no callback - may be changed by the caller */
7692 	spkt->satapkt_comp = NULL;
7693 	spkt->satapkt_time = sata_default_pkt_time;
7694 
7695 	/* Issue inquiry command - 6 bytes cdb, data transfer, read */
7696 
7697 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
7698 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
7699 
7700 	sata_atapi_packet_cmd_setup(scmd, sdinfo);
7701 
7702 	/* Set-up acdb. */
7703 	scmd->satacmd_acdb_len = sdinfo->satadrv_atapi_cdb_len;
7704 	bzero(scmd->satacmd_acdb, SATA_ATAPI_MAX_CDB_LEN);
7705 	scmd->satacmd_acdb[0] = 0x12;	/* Inquiry */
7706 	scmd->satacmd_acdb[1] = 0x00;
7707 	scmd->satacmd_acdb[2] = 0x00;
7708 	scmd->satacmd_acdb[3] = 0x00;
7709 	scmd->satacmd_acdb[4] = sizeof (struct scsi_inquiry);
7710 	scmd->satacmd_acdb[5] = 0x00;
7711 
7712 	sata_fixed_sense_data_preset(
7713 	    (struct scsi_extended_sense *)scmd->satacmd_rqsense);
7714 
7715 	/* Transfer command to HBA */
7716 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7717 	if (sata_hba_start(spx, &rval) != 0) {
7718 		/* Pkt not accepted for execution */
7719 		sata_log(sata_hba_inst, CE_WARN,
7720 		    "sata_test_atapi_packet_command: "
7721 		    "Packet not accepted for execution - ret: %02x", rval);
7722 		mutex_exit(
7723 		    &SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7724 		goto cleanup;
7725 	}
7726 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
7727 
7728 	/*
7729 	 * Sync buffer. Handle is in usual place in translate struct.
7730 	 */
7731 	rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
7732 	    DDI_DMA_SYNC_FORCPU);
7733 	ASSERT(rval == DDI_SUCCESS);
7734 	if (spkt->satapkt_reason == SATA_PKT_COMPLETED) {
7735 		sata_log(sata_hba_inst, CE_WARN,
7736 		    "sata_test_atapi_packet_command: "
7737 		    "Packet completed successfully");
7738 		/*
7739 		 * Normal completion - show inquiry data
7740 		 */
7741 		sata_show_inqry_data((uint8_t *)bp->b_un.b_addr);
7742 	} else {
7743 		/*
7744 		 * Something went wrong - analyze return - check rqsense data
7745 		 */
7746 		if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR) {
7747 			/*
7748 			 * ARQ data hopefull show something other than NO SENSE
7749 			 */
7750 			rqsp = scmd->satacmd_rqsense;
7751 			sata_log(spx->txlt_sata_hba_inst, CE_WARN,
7752 			    "ATAPI packet completion reason: %02x\n"
7753 			    "RQSENSE:  %02x %02x %02x %02x %02x %02x "
7754 			    "          %02x %02x %02x %02x %02x %02x "
7755 			    "          %02x %02x %02x %02x %02x %02x\n",
7756 			    spkt->satapkt_reason,
7757 			    rqsp[0], rqsp[1], rqsp[2], rqsp[3],
7758 			    rqsp[4], rqsp[5], rqsp[6], rqsp[7],
7759 			    rqsp[8], rqsp[9], rqsp[10], rqsp[11],
7760 			    rqsp[12], rqsp[13], rqsp[14], rqsp[15],
7761 			    rqsp[16], rqsp[17]);
7762 		} else {
7763 			switch (spkt->satapkt_reason) {
7764 			case SATA_PKT_PORT_ERROR:
7765 				sata_log(sata_hba_inst, CE_WARN,
7766 				    "sata_test_atapi_packet_command: "
7767 				    "packet reason: port error\n");
7768 				break;
7769 
7770 			case SATA_PKT_TIMEOUT:
7771 				sata_log(sata_hba_inst, CE_WARN,
7772 				    "sata_test_atapi_packet_command: "
7773 				    "packet reason: timeout\n");
7774 				break;
7775 
7776 			case SATA_PKT_ABORTED:
7777 				sata_log(sata_hba_inst, CE_WARN,
7778 				    "sata_test_atapi_packet_command: "
7779 				    "packet reason: aborted\n");
7780 				break;
7781 
7782 			case SATA_PKT_RESET:
7783 				sata_log(sata_hba_inst, CE_WARN,
7784 				    "sata_test_atapi_packet_command: "
7785 				    "packet reason: reset\n");
7786 				break;
7787 			default:
7788 				sata_log(sata_hba_inst, CE_WARN,
7789 				    "sata_test_atapi_packet_command: "
7790 				    "invalid packet reason: %02x\n",
7791 				    spkt->satapkt_reason);
7792 				break;
7793 			}
7794 		}
7795 	}
7796 cleanup:
7797 	sata_free_local_buffer(spx);
7798 	sata_pkt_free(spx);
7799 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
7800 }
7801 
7802 #endif /* SATA_DEBUG */
7803 #endif /* 1 */
7804 
7805 
7806 /* ************************** LOCAL HELPER FUNCTIONS *********************** */
7807 
7808 /*
7809  * Validate sata_tran info
7810  * SATA_FAILURE returns if structure is inconsistent or structure revision
7811  * does not match one used by the framework.
7812  *
7813  * Returns SATA_SUCCESS if sata_hba_tran has matching revision and contains
7814  * required function pointers.
7815  * Returns SATA_FAILURE otherwise.
7816  */
7817 static int
7818 sata_validate_sata_hba_tran(dev_info_t *dip, sata_hba_tran_t *sata_tran)
7819 {
7820 	/*
7821 	 * SATA_TRAN_HBA_REV is the current (highest) revision number
7822 	 * of the SATA interface.
7823 	 */
7824 	if (sata_tran->sata_tran_hba_rev > SATA_TRAN_HBA_REV) {
7825 		sata_log(NULL, CE_WARN,
7826 		    "sata: invalid sata_hba_tran version %d for driver %s",
7827 		    sata_tran->sata_tran_hba_rev, ddi_driver_name(dip));
7828 		return (SATA_FAILURE);
7829 	}
7830 
7831 	if (dip != sata_tran->sata_tran_hba_dip) {
7832 		SATA_LOG_D((NULL, CE_WARN,
7833 		    "sata: inconsistent sata_tran_hba_dip "
7834 		    "%p / %p", sata_tran->sata_tran_hba_dip, dip));
7835 		return (SATA_FAILURE);
7836 	}
7837 
7838 	if (sata_tran->sata_tran_probe_port == NULL ||
7839 	    sata_tran->sata_tran_start == NULL ||
7840 	    sata_tran->sata_tran_abort == NULL ||
7841 	    sata_tran->sata_tran_reset_dport == NULL ||
7842 	    sata_tran->sata_tran_hotplug_ops == NULL ||
7843 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_activate == NULL ||
7844 	    sata_tran->sata_tran_hotplug_ops->sata_tran_port_deactivate ==
7845 	    NULL) {
7846 		SATA_LOG_D((NULL, CE_WARN, "sata: sata_hba_tran missing "
7847 		    "required functions"));
7848 	}
7849 	return (SATA_SUCCESS);
7850 }
7851 
7852 /*
7853  * Remove HBA instance from sata_hba_list.
7854  */
7855 static void
7856 sata_remove_hba_instance(dev_info_t *dip)
7857 {
7858 	sata_hba_inst_t	*sata_hba_inst;
7859 
7860 	mutex_enter(&sata_mutex);
7861 	for (sata_hba_inst = sata_hba_list;
7862 	    sata_hba_inst != (struct sata_hba_inst *)NULL;
7863 	    sata_hba_inst = sata_hba_inst->satahba_next) {
7864 		if (sata_hba_inst->satahba_dip == dip)
7865 			break;
7866 	}
7867 
7868 	if (sata_hba_inst == (struct sata_hba_inst *)NULL) {
7869 #ifdef SATA_DEBUG
7870 		cmn_err(CE_WARN, "sata_remove_hba_instance: "
7871 		    "unknown HBA instance\n");
7872 #endif
7873 		ASSERT(FALSE);
7874 	}
7875 	if (sata_hba_inst == sata_hba_list) {
7876 		sata_hba_list = sata_hba_inst->satahba_next;
7877 		if (sata_hba_list) {
7878 			sata_hba_list->satahba_prev =
7879 			    (struct sata_hba_inst *)NULL;
7880 		}
7881 		if (sata_hba_inst == sata_hba_list_tail) {
7882 			sata_hba_list_tail = NULL;
7883 		}
7884 	} else if (sata_hba_inst == sata_hba_list_tail) {
7885 		sata_hba_list_tail = sata_hba_inst->satahba_prev;
7886 		if (sata_hba_list_tail) {
7887 			sata_hba_list_tail->satahba_next =
7888 			    (struct sata_hba_inst *)NULL;
7889 		}
7890 	} else {
7891 		sata_hba_inst->satahba_prev->satahba_next =
7892 		    sata_hba_inst->satahba_next;
7893 		sata_hba_inst->satahba_next->satahba_prev =
7894 		    sata_hba_inst->satahba_prev;
7895 	}
7896 	mutex_exit(&sata_mutex);
7897 }
7898 
7899 
7900 
7901 
7902 
7903 /*
7904  * Probe all SATA ports of the specified HBA instance.
7905  * The assumption is that there are no target and attachment point minor nodes
7906  * created by the boot subsystems, so we do not need to prune device tree.
7907  *
7908  * This function is called only from sata_hba_attach(). It does not have to
7909  * be protected by controller mutex, because the hba_attached flag is not set
7910  * yet and no one would be touching this HBA instance other than this thread.
7911  * Determines if port is active and what type of the device is attached
7912  * (if any). Allocates necessary structures for each port.
7913  *
7914  * An AP (Attachement Point) node is created for each SATA device port even
7915  * when there is no device attached.
7916  */
7917 
7918 static 	void
7919 sata_probe_ports(sata_hba_inst_t *sata_hba_inst)
7920 {
7921 	dev_info_t		*dip = SATA_DIP(sata_hba_inst);
7922 	int			ncport, npmport;
7923 	sata_cport_info_t 	*cportinfo;
7924 	sata_drive_info_t	*drive;
7925 	sata_pmult_info_t	*pminfo;
7926 	sata_pmport_info_t 	*pmportinfo;
7927 	sata_device_t		sata_device;
7928 	int			rval;
7929 	dev_t			minor_number;
7930 	char			name[16];
7931 	clock_t			start_time, cur_time;
7932 
7933 	/*
7934 	 * Probe controller ports first, to find port status and
7935 	 * any port multiplier attached.
7936 	 */
7937 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
7938 		/* allocate cport structure */
7939 		cportinfo = kmem_zalloc(sizeof (sata_cport_info_t), KM_SLEEP);
7940 		ASSERT(cportinfo != NULL);
7941 		mutex_init(&cportinfo->cport_mutex, NULL, MUTEX_DRIVER, NULL);
7942 
7943 		mutex_enter(&cportinfo->cport_mutex);
7944 
7945 		cportinfo->cport_addr.cport = ncport;
7946 		cportinfo->cport_addr.pmport = 0;
7947 		cportinfo->cport_addr.qual = SATA_ADDR_CPORT;
7948 		cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
7949 		cportinfo->cport_state |= SATA_STATE_PROBING;
7950 		SATA_CPORT_INFO(sata_hba_inst, ncport) = cportinfo;
7951 
7952 		/*
7953 		 * Regardless if a port is usable or not, create
7954 		 * an attachment point
7955 		 */
7956 		mutex_exit(&cportinfo->cport_mutex);
7957 		minor_number =
7958 		    SATA_MAKE_AP_MINOR(ddi_get_instance(dip), ncport, 0, 0);
7959 		(void) sprintf(name, "%d", ncport);
7960 		if (ddi_create_minor_node(dip, name, S_IFCHR,
7961 		    minor_number, DDI_NT_SATA_ATTACHMENT_POINT, 0) !=
7962 		    DDI_SUCCESS) {
7963 			sata_log(sata_hba_inst, CE_WARN, "sata_hba_attach: "
7964 			    "cannot create SATA attachment point for port %d",
7965 			    ncport);
7966 		}
7967 
7968 		/* Probe port */
7969 		start_time = ddi_get_lbolt();
7970 	reprobe_cport:
7971 		sata_device.satadev_addr.cport = ncport;
7972 		sata_device.satadev_addr.pmport = 0;
7973 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
7974 		sata_device.satadev_rev = SATA_DEVICE_REV;
7975 
7976 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
7977 		    (dip, &sata_device);
7978 
7979 		mutex_enter(&cportinfo->cport_mutex);
7980 		sata_update_port_scr(&cportinfo->cport_scr, &sata_device);
7981 		if (rval != SATA_SUCCESS) {
7982 			/* Something went wrong? Fail the port */
7983 			cportinfo->cport_state = SATA_PSTATE_FAILED;
7984 			mutex_exit(&cportinfo->cport_mutex);
7985 			continue;
7986 		}
7987 		cportinfo->cport_state &= ~SATA_STATE_PROBING;
7988 		cportinfo->cport_state |= SATA_STATE_PROBED;
7989 		cportinfo->cport_dev_type = sata_device.satadev_type;
7990 
7991 		cportinfo->cport_state |= SATA_STATE_READY;
7992 		if (cportinfo->cport_dev_type == SATA_DTYPE_NONE) {
7993 			mutex_exit(&cportinfo->cport_mutex);
7994 			continue;
7995 		}
7996 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
7997 			/*
7998 			 * There is some device attached.
7999 			 * Allocate device info structure
8000 			 */
8001 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) == NULL) {
8002 				mutex_exit(&cportinfo->cport_mutex);
8003 				SATA_CPORTINFO_DRV_INFO(cportinfo) =
8004 				    kmem_zalloc(sizeof (sata_drive_info_t),
8005 				    KM_SLEEP);
8006 				mutex_enter(&cportinfo->cport_mutex);
8007 			}
8008 			drive = SATA_CPORTINFO_DRV_INFO(cportinfo);
8009 			drive->satadrv_addr = cportinfo->cport_addr;
8010 			drive->satadrv_addr.qual = SATA_ADDR_DCPORT;
8011 			drive->satadrv_type = cportinfo->cport_dev_type;
8012 			drive->satadrv_state = SATA_STATE_UNKNOWN;
8013 
8014 			mutex_exit(&cportinfo->cport_mutex);
8015 			if (sata_add_device(dip, sata_hba_inst, ncport, 0) !=
8016 			    SATA_SUCCESS) {
8017 				/*
8018 				 * Plugged device was not correctly identified.
8019 				 * Retry, within a SATA_DEV_IDENTIFY_TIMEOUT
8020 				 */
8021 				cur_time = ddi_get_lbolt();
8022 				if ((cur_time - start_time) <
8023 				    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
8024 					/* sleep for a while */
8025 					delay(drv_usectohz(
8026 					    SATA_DEV_IDENTIFY_RTR_DLY));
8027 					goto reprobe_cport;
8028 				}
8029 			}
8030 		} else {
8031 			mutex_exit(&cportinfo->cport_mutex);
8032 			ASSERT(cportinfo->cport_dev_type == SATA_DTYPE_PMULT);
8033 			pminfo = kmem_zalloc(sizeof (sata_pmult_info_t),
8034 			    KM_SLEEP);
8035 			mutex_enter(&cportinfo->cport_mutex);
8036 			ASSERT(pminfo != NULL);
8037 			SATA_CPORTINFO_PMULT_INFO(cportinfo) = pminfo;
8038 			pminfo->pmult_addr.cport = cportinfo->cport_addr.cport;
8039 			pminfo->pmult_addr.pmport = SATA_PMULT_HOSTPORT;
8040 			pminfo->pmult_addr.qual = SATA_ADDR_PMPORT;
8041 			pminfo->pmult_num_dev_ports =
8042 			    sata_device.satadev_add_info;
8043 			mutex_init(&pminfo->pmult_mutex, NULL, MUTEX_DRIVER,
8044 			    NULL);
8045 			pminfo->pmult_state = SATA_STATE_PROBING;
8046 			mutex_exit(&cportinfo->cport_mutex);
8047 
8048 			/* Probe Port Multiplier ports */
8049 			for (npmport = 0;
8050 			    npmport < pminfo->pmult_num_dev_ports;
8051 			    npmport++) {
8052 				pmportinfo = kmem_zalloc(
8053 				    sizeof (sata_pmport_info_t), KM_SLEEP);
8054 				mutex_enter(&cportinfo->cport_mutex);
8055 				ASSERT(pmportinfo != NULL);
8056 				pmportinfo->pmport_addr.cport = ncport;
8057 				pmportinfo->pmport_addr.pmport = npmport;
8058 				pmportinfo->pmport_addr.qual =
8059 				    SATA_ADDR_PMPORT;
8060 				pminfo->pmult_dev_port[npmport] = pmportinfo;
8061 
8062 				mutex_init(&pmportinfo->pmport_mutex, NULL,
8063 				    MUTEX_DRIVER, NULL);
8064 
8065 				mutex_exit(&cportinfo->cport_mutex);
8066 
8067 				/* Create an attachment point */
8068 				minor_number = SATA_MAKE_AP_MINOR(
8069 				    ddi_get_instance(dip), ncport, npmport, 1);
8070 				(void) sprintf(name, "%d.%d", ncport, npmport);
8071 				if (ddi_create_minor_node(dip, name, S_IFCHR,
8072 				    minor_number, DDI_NT_SATA_ATTACHMENT_POINT,
8073 				    0) != DDI_SUCCESS) {
8074 					sata_log(sata_hba_inst, CE_WARN,
8075 					    "sata_hba_attach: "
8076 					    "cannot create SATA attachment "
8077 					    "point for port %d pmult port %d",
8078 					    ncport, npmport);
8079 				}
8080 
8081 				start_time = ddi_get_lbolt();
8082 			reprobe_pmport:
8083 				sata_device.satadev_addr.pmport = npmport;
8084 				sata_device.satadev_addr.qual =
8085 				    SATA_ADDR_PMPORT;
8086 
8087 				rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8088 				    (dip, &sata_device);
8089 				mutex_enter(&cportinfo->cport_mutex);
8090 
8091 				/* sata_update_port_info() */
8092 				sata_update_port_scr(&pmportinfo->pmport_scr,
8093 				    &sata_device);
8094 
8095 				if (rval != SATA_SUCCESS) {
8096 					pmportinfo->pmport_state =
8097 					    SATA_PSTATE_FAILED;
8098 					mutex_exit(&cportinfo->cport_mutex);
8099 					continue;
8100 				}
8101 				pmportinfo->pmport_state &=
8102 				    ~SATA_STATE_PROBING;
8103 				pmportinfo->pmport_state |= SATA_STATE_PROBED;
8104 				pmportinfo->pmport_dev_type =
8105 				    sata_device.satadev_type;
8106 
8107 				pmportinfo->pmport_state |= SATA_STATE_READY;
8108 				if (pmportinfo->pmport_dev_type ==
8109 				    SATA_DTYPE_NONE) {
8110 					mutex_exit(&cportinfo->cport_mutex);
8111 					continue;
8112 				}
8113 				/* Port multipliers cannot be chained */
8114 				ASSERT(pmportinfo->pmport_dev_type !=
8115 				    SATA_DTYPE_PMULT);
8116 				/*
8117 				 * There is something attached to Port
8118 				 * Multiplier device port
8119 				 * Allocate device info structure
8120 				 */
8121 				if (pmportinfo->pmport_sata_drive == NULL) {
8122 					mutex_exit(&cportinfo->cport_mutex);
8123 					pmportinfo->pmport_sata_drive =
8124 					    kmem_zalloc(
8125 					    sizeof (sata_drive_info_t),
8126 					    KM_SLEEP);
8127 					mutex_enter(&cportinfo->cport_mutex);
8128 				}
8129 				drive = pmportinfo->pmport_sata_drive;
8130 				drive->satadrv_addr.cport =
8131 				    pmportinfo->pmport_addr.cport;
8132 				drive->satadrv_addr.pmport = npmport;
8133 				drive->satadrv_addr.qual = SATA_ADDR_DPMPORT;
8134 				drive->satadrv_type = pmportinfo->
8135 				    pmport_dev_type;
8136 				drive->satadrv_state = SATA_STATE_UNKNOWN;
8137 
8138 				mutex_exit(&cportinfo->cport_mutex);
8139 				if (sata_add_device(dip, sata_hba_inst, ncport,
8140 				    npmport) != SATA_SUCCESS) {
8141 					/*
8142 					 * Plugged device was not correctly
8143 					 * identified. Retry, within the
8144 					 * SATA_DEV_IDENTIFY_TIMEOUT
8145 					 */
8146 					cur_time = ddi_get_lbolt();
8147 					if ((cur_time - start_time) <
8148 					    drv_usectohz(
8149 					    SATA_DEV_IDENTIFY_TIMEOUT)) {
8150 						/* sleep for a while */
8151 						delay(drv_usectohz(
8152 						    SATA_DEV_IDENTIFY_RTR_DLY));
8153 						goto reprobe_pmport;
8154 					}
8155 				}
8156 			}
8157 			pmportinfo->pmport_state =
8158 			    SATA_STATE_PROBED | SATA_STATE_READY;
8159 		}
8160 	}
8161 }
8162 
8163 /*
8164  * Add SATA device for specified HBA instance & port (SCSI target
8165  * device nodes).
8166  * This function is called (indirectly) only from sata_hba_attach().
8167  * A target node is created when there is a supported type device attached,
8168  * but may be removed if it cannot be put online.
8169  *
8170  * This function cannot be called from an interrupt context.
8171  *
8172  * ONLY DISK TARGET NODES ARE CREATED NOW
8173  *
8174  * Returns SATA_SUCCESS when port/device was fully processed, SATA_FAILURE when
8175  * device identification failed - adding a device could be retried.
8176  *
8177  */
8178 static 	int
8179 sata_add_device(dev_info_t *pdip, sata_hba_inst_t *sata_hba_inst, int cport,
8180     int pmport)
8181 {
8182 	sata_cport_info_t 	*cportinfo;
8183 	sata_pmult_info_t	*pminfo;
8184 	sata_pmport_info_t	*pmportinfo;
8185 	dev_info_t		*cdip;		/* child dip */
8186 	sata_device_t		sata_device;
8187 	int			rval;
8188 
8189 
8190 
8191 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
8192 	ASSERT(cportinfo->cport_dev_type != SATA_DTYPE_NONE);
8193 	mutex_enter(&cportinfo->cport_mutex);
8194 	/*
8195 	 * Some device is attached to a controller port.
8196 	 * We rely on controllers distinquishing between no-device,
8197 	 * attached port multiplier and other kind of attached device.
8198 	 * We need to get Identify Device data and determine
8199 	 * positively the dev type before trying to attach
8200 	 * the target driver.
8201 	 */
8202 	sata_device.satadev_rev = SATA_DEVICE_REV;
8203 	if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
8204 		/*
8205 		 * Not port multiplier.
8206 		 */
8207 		sata_device.satadev_addr = cportinfo->cport_addr;
8208 		sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
8209 		mutex_exit(&cportinfo->cport_mutex);
8210 
8211 		rval = sata_probe_device(sata_hba_inst, &sata_device);
8212 		if (rval != SATA_SUCCESS ||
8213 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN)
8214 			return (SATA_FAILURE);
8215 
8216 		mutex_enter(&cportinfo->cport_mutex);
8217 		sata_show_drive_info(sata_hba_inst,
8218 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
8219 
8220 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
8221 			/*
8222 			 * Could not determine device type or
8223 			 * a device is not supported.
8224 			 * Degrade this device to unknown.
8225 			 */
8226 			cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8227 			mutex_exit(&cportinfo->cport_mutex);
8228 			return (SATA_SUCCESS);
8229 		}
8230 		cportinfo->cport_dev_type = sata_device.satadev_type;
8231 		cportinfo->cport_tgtnode_clean = B_TRUE;
8232 		mutex_exit(&cportinfo->cport_mutex);
8233 
8234 		/*
8235 		 * Initialize device to the desired state. Even if it
8236 		 * fails, the device will still attach but syslog
8237 		 * will show the warning.
8238 		 */
8239 		if (sata_initialize_device(sata_hba_inst,
8240 		    SATA_CPORTINFO_DRV_INFO(cportinfo)) != SATA_SUCCESS)
8241 			/* Retry */
8242 			(void) sata_initialize_device(sata_hba_inst,
8243 			    SATA_CPORTINFO_DRV_INFO(cportinfo));
8244 
8245 		cdip = sata_create_target_node(pdip, sata_hba_inst,
8246 		    &sata_device.satadev_addr);
8247 		mutex_enter(&cportinfo->cport_mutex);
8248 		if (cdip == NULL) {
8249 			/*
8250 			 * Attaching target node failed.
8251 			 * We retain sata_drive_info structure...
8252 			 */
8253 			mutex_exit(&cportinfo->cport_mutex);
8254 			return (SATA_SUCCESS);
8255 		}
8256 		(SATA_CPORTINFO_DRV_INFO(cportinfo))->
8257 		    satadrv_state = SATA_STATE_READY;
8258 	} else {
8259 		/* This must be Port Multiplier type */
8260 		if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
8261 			SATA_LOG_D((sata_hba_inst, CE_WARN,
8262 			    "sata_add_device: "
8263 			    "unrecognized dev type %x",
8264 			    cportinfo->cport_dev_type));
8265 			mutex_exit(&cportinfo->cport_mutex);
8266 			return (SATA_SUCCESS);
8267 		}
8268 		pminfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
8269 		pmportinfo = pminfo->pmult_dev_port[pmport];
8270 		sata_device.satadev_addr = pmportinfo->pmport_addr;
8271 		sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
8272 		mutex_exit(&cportinfo->cport_mutex);
8273 
8274 		rval = sata_probe_device(sata_hba_inst, &sata_device);
8275 		if (rval != SATA_SUCCESS ||
8276 		    sata_device.satadev_type == SATA_DTYPE_UNKNOWN) {
8277 			return (SATA_FAILURE);
8278 		}
8279 		mutex_enter(&cportinfo->cport_mutex);
8280 		sata_show_drive_info(sata_hba_inst,
8281 		    SATA_CPORTINFO_DRV_INFO(cportinfo));
8282 
8283 		if ((sata_device.satadev_type & SATA_VALID_DEV_TYPE) == 0) {
8284 			/*
8285 			 * Could not determine device type.
8286 			 * Degrade this device to unknown.
8287 			 */
8288 			pmportinfo->pmport_dev_type = SATA_DTYPE_UNKNOWN;
8289 			mutex_exit(&cportinfo->cport_mutex);
8290 			return (SATA_SUCCESS);
8291 		}
8292 		pmportinfo->pmport_dev_type = sata_device.satadev_type;
8293 		pmportinfo->pmport_tgtnode_clean = B_TRUE;
8294 		mutex_exit(&cportinfo->cport_mutex);
8295 
8296 		/*
8297 		 * Initialize device to the desired state.
8298 		 * Even if it fails, the device will still
8299 		 * attach but syslog will show the warning.
8300 		 */
8301 		if (sata_initialize_device(sata_hba_inst,
8302 		    pmportinfo->pmport_sata_drive) != SATA_SUCCESS)
8303 			/* Retry */
8304 			(void) sata_initialize_device(sata_hba_inst,
8305 			    pmportinfo->pmport_sata_drive);
8306 
8307 		cdip = sata_create_target_node(pdip, sata_hba_inst,
8308 		    &sata_device.satadev_addr);
8309 		mutex_enter(&cportinfo->cport_mutex);
8310 		if (cdip == NULL) {
8311 			/*
8312 			 * Attaching target node failed.
8313 			 * We retain sata_drive_info structure...
8314 			 */
8315 			mutex_exit(&cportinfo->cport_mutex);
8316 			return (SATA_SUCCESS);
8317 		}
8318 		pmportinfo->pmport_sata_drive->satadrv_state |=
8319 		    SATA_STATE_READY;
8320 	}
8321 	mutex_exit(&cportinfo->cport_mutex);
8322 	return (SATA_SUCCESS);
8323 }
8324 
8325 
8326 
8327 /*
8328  * Create scsi target node for attached device, create node properties and
8329  * attach the node.
8330  * The node could be removed if the device onlining fails.
8331  *
8332  * A dev_info_t pointer is returned if operation is successful, NULL is
8333  * returned otherwise.
8334  *
8335  * No port multiplier support.
8336  */
8337 
8338 static dev_info_t *
8339 sata_create_target_node(dev_info_t *dip, sata_hba_inst_t *sata_hba_inst,
8340 			sata_address_t *sata_addr)
8341 {
8342 	dev_info_t *cdip = NULL;
8343 	int rval;
8344 	char *nname = NULL;
8345 	char **compatible = NULL;
8346 	int ncompatible;
8347 	struct scsi_inquiry inq;
8348 	sata_device_t sata_device;
8349 	sata_drive_info_t *sdinfo;
8350 	int target;
8351 	int i;
8352 
8353 	sata_device.satadev_rev = SATA_DEVICE_REV;
8354 	sata_device.satadev_addr = *sata_addr;
8355 
8356 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, sata_addr->cport)));
8357 
8358 	sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
8359 
8360 	target = SATA_TO_SCSI_TARGET(sata_addr->cport,
8361 	    sata_addr->pmport, sata_addr->qual);
8362 
8363 	if (sdinfo == NULL) {
8364 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8365 		    sata_addr->cport)));
8366 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8367 		    "sata_create_target_node: no sdinfo for target %x",
8368 		    target));
8369 		return (NULL);
8370 	}
8371 
8372 	/*
8373 	 * create or get scsi inquiry data, expected by
8374 	 * scsi_hba_nodename_compatible_get()
8375 	 * SATA hard disks get Identify Data translated into Inguiry Data.
8376 	 * ATAPI devices respond directly to Inquiry request.
8377 	 */
8378 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
8379 		sata_identdev_to_inquiry(sata_hba_inst, sdinfo,
8380 		    (uint8_t *)&inq);
8381 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8382 		    sata_addr->cport)));
8383 	} else { /* Assume supported ATAPI device */
8384 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8385 		    sata_addr->cport)));
8386 		if (sata_get_atapi_inquiry_data(sata_hba_inst, sata_addr,
8387 		    &inq) == SATA_FAILURE)
8388 			return (NULL);
8389 		/*
8390 		 * Save supported ATAPI transport version
8391 		 */
8392 		sdinfo->satadrv_atapi_trans_ver =
8393 		    SATA_ATAPI_TRANS_VERSION(&inq);
8394 	}
8395 
8396 	/* determine the node name and compatible */
8397 	scsi_hba_nodename_compatible_get(&inq, NULL,
8398 	    inq.inq_dtype, NULL, &nname, &compatible, &ncompatible);
8399 
8400 #ifdef SATA_DEBUG
8401 	if (sata_debug_flags & SATA_DBG_NODES) {
8402 		if (nname == NULL) {
8403 			cmn_err(CE_NOTE, "sata_create_target_node: "
8404 			    "cannot determine nodename for target %d\n",
8405 			    target);
8406 		} else {
8407 			cmn_err(CE_WARN, "sata_create_target_node: "
8408 			    "target %d nodename: %s\n", target, nname);
8409 		}
8410 		if (compatible == NULL) {
8411 			cmn_err(CE_WARN,
8412 			    "sata_create_target_node: no compatible name\n");
8413 		} else {
8414 			for (i = 0; i < ncompatible; i++) {
8415 				cmn_err(CE_WARN, "sata_create_target_node: "
8416 				    "compatible name: %s\n", compatible[i]);
8417 			}
8418 		}
8419 	}
8420 #endif
8421 
8422 	/* if nodename can't be determined, log error and exit */
8423 	if (nname == NULL) {
8424 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8425 		    "sata_create_target_node: cannot determine nodename "
8426 		    "for target %d\n", target));
8427 		scsi_hba_nodename_compatible_free(nname, compatible);
8428 		return (NULL);
8429 	}
8430 	/*
8431 	 * Create scsi target node
8432 	 */
8433 	ndi_devi_alloc_sleep(dip, nname, (pnode_t)DEVI_SID_NODEID, &cdip);
8434 	rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
8435 	    "device-type", "scsi");
8436 
8437 	if (rval != DDI_PROP_SUCCESS) {
8438 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8439 		    "updating device_type prop failed %d", rval));
8440 		goto fail;
8441 	}
8442 
8443 	/*
8444 	 * Create target node properties: target & lun
8445 	 */
8446 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "target", target);
8447 	if (rval != DDI_PROP_SUCCESS) {
8448 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8449 		    "updating target prop failed %d", rval));
8450 		goto fail;
8451 	}
8452 	rval = ndi_prop_update_int(DDI_DEV_T_NONE, cdip, "lun", 0);
8453 	if (rval != DDI_PROP_SUCCESS) {
8454 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8455 		    "updating target prop failed %d", rval));
8456 		goto fail;
8457 	}
8458 
8459 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
8460 		/*
8461 		 * Add "variant" property
8462 		 */
8463 		rval = ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
8464 		    "variant", "atapi");
8465 		if (rval != DDI_PROP_SUCCESS) {
8466 			SATA_LOG_D((sata_hba_inst, CE_WARN,
8467 			    "sata_create_target_node: variant atapi "
8468 			    "property could not be created: %d", rval));
8469 			goto fail;
8470 		}
8471 	}
8472 	/* decorate the node with compatible */
8473 	if (ndi_prop_update_string_array(DDI_DEV_T_NONE, cdip, "compatible",
8474 	    compatible, ncompatible) != DDI_PROP_SUCCESS) {
8475 		SATA_LOG_D((sata_hba_inst, CE_WARN,
8476 		    "sata_create_target_node: FAIL compatible props cdip 0x%p",
8477 		    (void *)cdip));
8478 		goto fail;
8479 	}
8480 
8481 
8482 	/*
8483 	 * Now, try to attach the driver. If probing of the device fails,
8484 	 * the target node may be removed
8485 	 */
8486 	rval = ndi_devi_online(cdip, NDI_ONLINE_ATTACH);
8487 
8488 	scsi_hba_nodename_compatible_free(nname, compatible);
8489 
8490 	if (rval == NDI_SUCCESS)
8491 		return (cdip);
8492 
8493 	/* target node was removed - are we sure? */
8494 	return (NULL);
8495 
8496 fail:
8497 	scsi_hba_nodename_compatible_free(nname, compatible);
8498 	ddi_prop_remove_all(cdip);
8499 	rval = ndi_devi_free(cdip);
8500 	if (rval != NDI_SUCCESS) {
8501 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_create_target_node: "
8502 		    "node removal failed %d", rval));
8503 	}
8504 	sata_log(sata_hba_inst, CE_WARN, "sata_create_target_node: "
8505 	    "cannot create target node for SATA device at port %d",
8506 	    sata_addr->cport);
8507 	return (NULL);
8508 }
8509 
8510 
8511 
8512 /*
8513  * Re-probe sata port, check for a device and attach info
8514  * structures when necessary. Identify Device data is fetched, if possible.
8515  * Assumption: sata address is already validated.
8516  * SATA_SUCCESS is returned if port is re-probed sucessfully, regardless of
8517  * the presence of a device and its type.
8518  *
8519  * flag arg specifies that the function should try multiple times to identify
8520  * device type and to initialize it, or it should return immediately on failure.
8521  * SATA_DEV_IDENTIFY_RETRY - retry
8522  * SATA_DEV_IDENTIFY_NORETRY - no retry
8523  *
8524  * SATA_FAILURE is returned if one of the operations failed.
8525  *
8526  * This function cannot be called in interrupt context - it may sleep.
8527  */
8528 static int
8529 sata_reprobe_port(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device,
8530     int flag)
8531 {
8532 	sata_cport_info_t *cportinfo;
8533 	sata_drive_info_t *sdinfo;
8534 	boolean_t init_device = B_FALSE;
8535 	int prev_device_type = SATA_DTYPE_NONE;
8536 	int prev_device_settings = 0;
8537 	clock_t start_time;
8538 	int retry = B_FALSE;
8539 	int rval;
8540 
8541 	/* We only care about host sata cport for now */
8542 	cportinfo = SATA_CPORT_INFO(sata_hba_inst,
8543 	    sata_device->satadev_addr.cport);
8544 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8545 	if (sdinfo != NULL) {
8546 		/*
8547 		 * We are re-probing port with a previously attached device.
8548 		 * Save previous device type and settings
8549 		 */
8550 		prev_device_type = cportinfo->cport_dev_type;
8551 		prev_device_settings = sdinfo->satadrv_settings;
8552 	}
8553 	if (flag == SATA_DEV_IDENTIFY_RETRY) {
8554 		start_time = ddi_get_lbolt();
8555 		retry = B_TRUE;
8556 	}
8557 retry_probe:
8558 
8559 	/* probe port */
8560 	mutex_enter(&cportinfo->cport_mutex);
8561 	cportinfo->cport_state &= ~SATA_PORT_STATE_CLEAR_MASK;
8562 	cportinfo->cport_state |= SATA_STATE_PROBING;
8563 	mutex_exit(&cportinfo->cport_mutex);
8564 
8565 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
8566 	    (SATA_DIP(sata_hba_inst), sata_device);
8567 
8568 	mutex_enter(&cportinfo->cport_mutex);
8569 	if (rval != SATA_SUCCESS) {
8570 		cportinfo->cport_state = SATA_PSTATE_FAILED;
8571 		mutex_exit(&cportinfo->cport_mutex);
8572 		SATA_LOG_D((sata_hba_inst, CE_WARN, "sata_reprobe_port: "
8573 		    "SATA port %d probing failed",
8574 		    cportinfo->cport_addr.cport));
8575 		return (SATA_FAILURE);
8576 	}
8577 
8578 	/*
8579 	 * update sata port state and set device type
8580 	 */
8581 	sata_update_port_info(sata_hba_inst, sata_device);
8582 	cportinfo->cport_state &= ~SATA_STATE_PROBING;
8583 
8584 	/*
8585 	 * Sanity check - Port is active? Is the link active?
8586 	 * Is there any device attached?
8587 	 */
8588 	if ((cportinfo->cport_state &
8589 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) ||
8590 	    (cportinfo->cport_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
8591 	    SATA_PORT_DEVLINK_UP) {
8592 		/*
8593 		 * Port in non-usable state or no link active/no device.
8594 		 * Free info structure if necessary (direct attached drive
8595 		 * only, for now!
8596 		 */
8597 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8598 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8599 		/* Add here differentiation for device attached or not */
8600 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8601 		mutex_exit(&cportinfo->cport_mutex);
8602 		if (sdinfo != NULL)
8603 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8604 		return (SATA_SUCCESS);
8605 	}
8606 
8607 	cportinfo->cport_state |= SATA_STATE_READY;
8608 	cportinfo->cport_dev_type = sata_device->satadev_type;
8609 	sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8610 
8611 	/*
8612 	 * If we are re-probing the port, there may be
8613 	 * sata_drive_info structure attached
8614 	 * (or sata_pm_info, if PMult is supported).
8615 	 */
8616 	if (sata_device->satadev_type == SATA_DTYPE_NONE) {
8617 		/*
8618 		 * There is no device, so remove device info structure,
8619 		 * if necessary. Direct attached drive only!
8620 		 */
8621 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
8622 		cportinfo->cport_dev_type = SATA_DTYPE_NONE;
8623 		if (sdinfo != NULL) {
8624 			kmem_free(sdinfo, sizeof (sata_drive_info_t));
8625 			sata_log(sata_hba_inst, CE_WARN,
8626 			    "SATA device detached "
8627 			    "from port %d", cportinfo->cport_addr.cport);
8628 		}
8629 		mutex_exit(&cportinfo->cport_mutex);
8630 		return (SATA_SUCCESS);
8631 	}
8632 
8633 	if (sata_device->satadev_type != SATA_DTYPE_PMULT) {
8634 		if (sdinfo == NULL) {
8635 			/*
8636 			 * There is some device attached, but there is
8637 			 * no sata_drive_info structure - allocate one
8638 			 */
8639 			mutex_exit(&cportinfo->cport_mutex);
8640 			sdinfo = kmem_zalloc(
8641 			    sizeof (sata_drive_info_t), KM_SLEEP);
8642 			mutex_enter(&cportinfo->cport_mutex);
8643 			/*
8644 			 * Recheck, that the port state did not change when we
8645 			 * released mutex.
8646 			 */
8647 			if (cportinfo->cport_state & SATA_STATE_READY) {
8648 				SATA_CPORTINFO_DRV_INFO(cportinfo) = sdinfo;
8649 				sdinfo->satadrv_addr = cportinfo->cport_addr;
8650 				sdinfo->satadrv_addr.qual = SATA_ADDR_DCPORT;
8651 				sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
8652 				sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
8653 			} else {
8654 				/*
8655 				 * Port is not in ready state, we
8656 				 * cannot attach a device.
8657 				 */
8658 				mutex_exit(&cportinfo->cport_mutex);
8659 				kmem_free(sdinfo, sizeof (sata_drive_info_t));
8660 				return (SATA_SUCCESS);
8661 			}
8662 			/*
8663 			 * Since we are adding device, presumably new one,
8664 			 * indicate that it  should be initalized,
8665 			 * as well as some internal framework states).
8666 			 */
8667 			init_device = B_TRUE;
8668 		}
8669 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8670 		sata_device->satadev_addr.qual = sdinfo->satadrv_addr.qual;
8671 	} else {
8672 		/*
8673 		 * The device is a port multiplier - not handled now.
8674 		 */
8675 		cportinfo->cport_dev_type = SATA_DTYPE_UNKNOWN;
8676 		mutex_exit(&cportinfo->cport_mutex);
8677 		return (SATA_SUCCESS);
8678 	}
8679 	mutex_exit(&cportinfo->cport_mutex);
8680 	/*
8681 	 * Figure out what kind of device we are really
8682 	 * dealing with.
8683 	 */
8684 	rval = sata_probe_device(sata_hba_inst, sata_device);
8685 
8686 	if (rval == SATA_SUCCESS) {
8687 		/*
8688 		 * If we are dealing with the same type of a device as before,
8689 		 * restore its settings flags.
8690 		 */
8691 		if (sata_device->satadev_type == prev_device_type)
8692 			sdinfo->satadrv_settings = prev_device_settings;
8693 
8694 		/* Set initial device features, if necessary */
8695 		if (init_device == B_TRUE) {
8696 			rval = sata_initialize_device(sata_hba_inst, sdinfo);
8697 		}
8698 		if (rval == SATA_SUCCESS)
8699 			return (rval);
8700 	}
8701 
8702 	if (retry) {
8703 		clock_t cur_time = ddi_get_lbolt();
8704 		/*
8705 		 * A device was not successfully identified or initialized.
8706 		 * Track retry time for device identification.
8707 		 */
8708 		if ((cur_time - start_time) <
8709 		    drv_usectohz(SATA_DEV_IDENTIFY_TIMEOUT)) {
8710 			/* sleep for a while */
8711 			delay(drv_usectohz(SATA_DEV_IDENTIFY_RTR_DLY));
8712 			goto retry_probe;
8713 		}
8714 	}
8715 	return (rval);
8716 }
8717 
8718 /*
8719  * Initialize device
8720  * Specified device is initialized to a default state.
8721  *
8722  * Returns SATA_SUCCESS if all device features are set successfully,
8723  * SATA_FAILURE otherwise
8724  */
8725 static int
8726 sata_initialize_device(sata_hba_inst_t *sata_hba_inst,
8727     sata_drive_info_t *sdinfo)
8728 {
8729 	int rval;
8730 
8731 	sata_save_drive_settings(sdinfo);
8732 
8733 	sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
8734 
8735 	sata_init_write_cache_mode(sdinfo);
8736 
8737 	rval = sata_set_drive_features(sata_hba_inst, sdinfo, 0);
8738 
8739 	/* Determine current data transfer mode */
8740 	if ((sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT) == 0) {
8741 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
8742 	} else if ((sdinfo->satadrv_id.ai_validinfo &
8743 	    SATA_VALIDINFO_88) != 0 &&
8744 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SEL_MASK) != 0) {
8745 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
8746 	} else if ((sdinfo->satadrv_id.ai_dworddma &
8747 	    SATA_MDMA_SEL_MASK) != 0) {
8748 		sdinfo->satadrv_settings |= SATA_DEV_DMA;
8749 	} else
8750 		/* DMA supported, not no DMA transfer mode is selected !? */
8751 		sdinfo->satadrv_settings &= ~SATA_DEV_DMA;
8752 
8753 	return (rval);
8754 }
8755 
8756 
8757 /*
8758  * Initialize write cache mode.
8759  *
8760  * The default write cache setting for SATA HDD is provided by sata_write_cache
8761  * static variable. ATAPI CD/DVDs devices have write cache default is
8762  * determined by sata_atapicdvd_write_cache static variable.
8763  * 1 - enable
8764  * 0 - disable
8765  * any other value - current drive setting
8766  *
8767  * Although there is not reason to disable write cache on CD/DVD devices,
8768  * the default setting control is provided for the maximun flexibility.
8769  *
8770  * In the future, it may be overridden by the
8771  * disk-write-cache-enable property setting, if it is defined.
8772  * Returns SATA_SUCCESS if all device features are set successfully,
8773  * SATA_FAILURE otherwise.
8774  */
8775 static void
8776 sata_init_write_cache_mode(sata_drive_info_t *sdinfo)
8777 {
8778 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
8779 		if (sata_write_cache == 1)
8780 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
8781 		else if (sata_write_cache == 0)
8782 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
8783 		/*
8784 		 * When sata_write_cache value is not 0 or 1,
8785 		 * a current setting of the drive's write cache is used.
8786 		 */
8787 	} else { /* Assume ATAPI CD/DVD device */
8788 		if (sata_atapicdvd_write_cache == 1)
8789 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
8790 		else if (sata_atapicdvd_write_cache == 0)
8791 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
8792 		/*
8793 		 * When sata_write_cache value is not 0 or 1,
8794 		 * a current setting of the drive's write cache is used.
8795 		 */
8796 	}
8797 }
8798 
8799 
8800 /*
8801  * Validate sata address.
8802  * Specified cport, pmport and qualifier has to match
8803  * passed sata_scsi configuration info.
8804  * The presence of an attached device is not verified.
8805  *
8806  * Returns 0 when address is valid, -1 otherwise.
8807  */
8808 static int
8809 sata_validate_sata_address(sata_hba_inst_t *sata_hba_inst, int cport,
8810 	int pmport, int qual)
8811 {
8812 	if (qual == SATA_ADDR_DCPORT && pmport != 0)
8813 		goto invalid_address;
8814 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
8815 		goto invalid_address;
8816 	if ((qual == SATA_ADDR_DPMPORT || qual == SATA_ADDR_PMPORT) &&
8817 	    ((SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) != SATA_DTYPE_PMULT) ||
8818 	    (SATA_PMULT_INFO(sata_hba_inst, cport) == NULL) ||
8819 	    (pmport >= SATA_NUM_PMPORTS(sata_hba_inst, cport))))
8820 		goto invalid_address;
8821 
8822 	return (0);
8823 
8824 invalid_address:
8825 	return (-1);
8826 
8827 }
8828 
8829 /*
8830  * Validate scsi address
8831  * SCSI target address is translated into SATA cport/pmport and compared
8832  * with a controller port/device configuration. LUN has to be 0.
8833  * Returns 0 if a scsi target refers to an attached device,
8834  * returns 1 if address is valid but device is not attached,
8835  * returns -1 if bad address or device is of an unsupported type.
8836  * Upon return sata_device argument is set.
8837  */
8838 static int
8839 sata_validate_scsi_address(sata_hba_inst_t *sata_hba_inst,
8840 	struct scsi_address *ap, sata_device_t *sata_device)
8841 {
8842 	int cport, pmport, qual, rval;
8843 
8844 	rval = -1;	/* Invalid address */
8845 	if (ap->a_lun != 0)
8846 		goto out;
8847 
8848 	qual = SCSI_TO_SATA_ADDR_QUAL(ap->a_target);
8849 	cport = SCSI_TO_SATA_CPORT(ap->a_target);
8850 	pmport = SCSI_TO_SATA_PMPORT(ap->a_target);
8851 
8852 	if (qual != SATA_ADDR_DCPORT && qual != SATA_ADDR_DPMPORT)
8853 		goto out;
8854 
8855 	if (sata_validate_sata_address(sata_hba_inst, cport, pmport, qual) ==
8856 	    0) {
8857 
8858 		sata_cport_info_t *cportinfo;
8859 		sata_pmult_info_t *pmultinfo;
8860 		sata_drive_info_t *sdinfo = NULL;
8861 
8862 		rval = 1;	/* Valid sata address */
8863 
8864 		cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
8865 		if (qual == SATA_ADDR_DCPORT) {
8866 			if (cportinfo == NULL ||
8867 			    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
8868 				goto out;
8869 
8870 			if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT ||
8871 			    (cportinfo->cport_dev_type &
8872 			    SATA_VALID_DEV_TYPE) == 0) {
8873 				rval = -1;
8874 				goto out;
8875 			}
8876 			sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
8877 
8878 		} else if (qual == SATA_ADDR_DPMPORT) {
8879 			pmultinfo = SATA_CPORTINFO_PMULT_INFO(cportinfo);
8880 			if (pmultinfo == NULL) {
8881 				rval = -1;
8882 				goto out;
8883 			}
8884 			if (SATA_PMPORT_INFO(sata_hba_inst, cport, pmport) ==
8885 			    NULL ||
8886 			    SATA_PMPORT_DEV_TYPE(sata_hba_inst, cport,
8887 			    pmport) == SATA_DTYPE_NONE)
8888 				goto out;
8889 
8890 			sdinfo = SATA_PMPORT_DRV_INFO(sata_hba_inst, cport,
8891 			    pmport);
8892 		} else {
8893 			rval = -1;
8894 			goto out;
8895 		}
8896 		if ((sdinfo == NULL) ||
8897 		    (sdinfo->satadrv_type & SATA_VALID_DEV_TYPE) == 0)
8898 			goto out;
8899 
8900 		sata_device->satadev_type = sdinfo->satadrv_type;
8901 		sata_device->satadev_addr.qual = qual;
8902 		sata_device->satadev_addr.cport = cport;
8903 		sata_device->satadev_addr.pmport = pmport;
8904 		sata_device->satadev_rev = SATA_DEVICE_REV_1;
8905 		return (0);
8906 	}
8907 out:
8908 	if (rval == 1) {
8909 		SATADBG2(SATA_DBG_SCSI_IF, sata_hba_inst,
8910 		    "sata_validate_scsi_address: no valid target %x lun %x",
8911 		    ap->a_target, ap->a_lun);
8912 	}
8913 	return (rval);
8914 }
8915 
8916 /*
8917  * Find dip corresponding to passed device number
8918  *
8919  * Returns NULL if invalid device number is passed or device cannot be found,
8920  * Returns dip is device is found.
8921  */
8922 static dev_info_t *
8923 sata_devt_to_devinfo(dev_t dev)
8924 {
8925 	dev_info_t *dip;
8926 #ifndef __lock_lint
8927 	struct devnames *dnp;
8928 	major_t major = getmajor(dev);
8929 	int instance = SATA_MINOR2INSTANCE(getminor(dev));
8930 
8931 	if (major >= devcnt)
8932 		return (NULL);
8933 
8934 	dnp = &devnamesp[major];
8935 	LOCK_DEV_OPS(&(dnp->dn_lock));
8936 	dip = dnp->dn_head;
8937 	while (dip && (ddi_get_instance(dip) != instance)) {
8938 		dip = ddi_get_next(dip);
8939 	}
8940 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
8941 #endif
8942 
8943 	return (dip);
8944 }
8945 
8946 
8947 /*
8948  * Probe device.
8949  * This function issues Identify Device command and initializes local
8950  * sata_drive_info structure if the device can be identified.
8951  * The device type is determined by examining Identify Device
8952  * command response.
8953  * If the sata_hba_inst has linked drive info structure for this
8954  * device address, the Identify Device data is stored into sata_drive_info
8955  * structure linked to the port info structure.
8956  *
8957  * sata_device has to refer to the valid sata port(s) for HBA described
8958  * by sata_hba_inst structure.
8959  *
8960  * Returns:
8961  *	SATA_SUCCESS if device type was successfully probed and port-linked
8962  *		drive info structure was updated;
8963  * 	SATA_FAILURE if there is no device, or device was not probed
8964  *		successully;
8965  *	SATA_RETRY if device probe can be retried later.
8966  * If a device cannot be identified, sata_device's dev_state and dev_type
8967  * fields are set to unknown.
8968  * There are no retries in this function. Any retries should be managed by
8969  * the caller.
8970  */
8971 
8972 
8973 static int
8974 sata_probe_device(sata_hba_inst_t *sata_hba_inst, sata_device_t *sata_device)
8975 {
8976 	sata_drive_info_t *sdinfo;
8977 	sata_drive_info_t new_sdinfo;	/* local drive info struct */
8978 	int rval;
8979 
8980 	ASSERT((SATA_CPORT_STATE(sata_hba_inst,
8981 	    sata_device->satadev_addr.cport) &
8982 	    (SATA_STATE_PROBED | SATA_STATE_READY)) != 0);
8983 
8984 	sata_device->satadev_type = SATA_DTYPE_NONE;
8985 
8986 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
8987 	    sata_device->satadev_addr.cport)));
8988 
8989 	/* Get pointer to port-linked sata device info structure */
8990 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
8991 	if (sdinfo != NULL) {
8992 		sdinfo->satadrv_state &=
8993 		    ~(SATA_STATE_PROBED | SATA_STATE_READY);
8994 		sdinfo->satadrv_state |= SATA_STATE_PROBING;
8995 	} else {
8996 		/* No device to probe */
8997 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
8998 		    sata_device->satadev_addr.cport)));
8999 		sata_device->satadev_type = SATA_DTYPE_NONE;
9000 		sata_device->satadev_state = SATA_STATE_UNKNOWN;
9001 		return (SATA_FAILURE);
9002 	}
9003 	/*
9004 	 * Need to issue both types of identify device command and
9005 	 * determine device type by examining retreived data/status.
9006 	 * First, ATA Identify Device.
9007 	 */
9008 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
9009 	new_sdinfo.satadrv_addr = sata_device->satadev_addr;
9010 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9011 	    sata_device->satadev_addr.cport)));
9012 	new_sdinfo.satadrv_type = SATA_DTYPE_ATADISK;
9013 	rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
9014 	if (rval == SATA_RETRY) {
9015 		/* We may try to check for ATAPI device */
9016 		if (SATA_FEATURES(sata_hba_inst) & SATA_CTLF_ATAPI) {
9017 			/*
9018 			 * HBA supports ATAPI - try to issue Identify Packet
9019 			 * Device command.
9020 			 */
9021 			new_sdinfo.satadrv_type = SATA_DTYPE_ATAPICD;
9022 			rval = sata_identify_device(sata_hba_inst, &new_sdinfo);
9023 		}
9024 	}
9025 	if (rval == SATA_SUCCESS) {
9026 		/*
9027 		 * Got something responding positively to ATA Identify Device
9028 		 * or to Identify Packet Device cmd.
9029 		 * Save last used device type.
9030 		 */
9031 		sata_device->satadev_type = new_sdinfo.satadrv_type;
9032 
9033 		/* save device info, if possible */
9034 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9035 		    sata_device->satadev_addr.cport)));
9036 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9037 		if (sdinfo == NULL) {
9038 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9039 			    sata_device->satadev_addr.cport)));
9040 			return (SATA_FAILURE);
9041 		}
9042 		/*
9043 		 * Copy drive info into the port-linked drive info structure.
9044 		 */
9045 		*sdinfo = new_sdinfo;
9046 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
9047 		sdinfo->satadrv_state |= SATA_STATE_PROBED;
9048 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
9049 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
9050 			    sata_device->satadev_addr.cport) =
9051 			    sdinfo->satadrv_type;
9052 		else /* SATA_ADDR_DPMPORT */
9053 			SATA_PMPORT_DEV_TYPE(sata_hba_inst,
9054 			    sata_device->satadev_addr.cport,
9055 			    sata_device->satadev_addr.pmport) =
9056 			    sdinfo->satadrv_type;
9057 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9058 		    sata_device->satadev_addr.cport)));
9059 		return (SATA_SUCCESS);
9060 	}
9061 
9062 	/*
9063 	 * It may be SATA_RETRY or SATA_FAILURE return.
9064 	 * Looks like we cannot determine the device type at this time.
9065 	 */
9066 	mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
9067 	    sata_device->satadev_addr.cport)));
9068 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
9069 	if (sdinfo != NULL) {
9070 		sata_device->satadev_type = SATA_DTYPE_UNKNOWN;
9071 		sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9072 		sdinfo->satadrv_state &= ~SATA_STATE_PROBING;
9073 		sdinfo->satadrv_state = SATA_STATE_PROBED;
9074 		if (sata_device->satadev_addr.qual == SATA_ADDR_DCPORT)
9075 			SATA_CPORT_DEV_TYPE(sata_hba_inst,
9076 			    sata_device->satadev_addr.cport) =
9077 			    SATA_DTYPE_UNKNOWN;
9078 		else {
9079 			/* SATA_ADDR_DPMPORT */
9080 			if ((SATA_PMULT_INFO(sata_hba_inst,
9081 			    sata_device->satadev_addr.cport) != NULL) &&
9082 			    (SATA_PMPORT_INFO(sata_hba_inst,
9083 			    sata_device->satadev_addr.cport,
9084 			    sata_device->satadev_addr.pmport) != NULL))
9085 				SATA_PMPORT_DEV_TYPE(sata_hba_inst,
9086 				    sata_device->satadev_addr.cport,
9087 				    sata_device->satadev_addr.pmport) =
9088 				    SATA_DTYPE_UNKNOWN;
9089 		}
9090 	}
9091 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
9092 	    sata_device->satadev_addr.cport)));
9093 	return (rval);
9094 }
9095 
9096 
9097 /*
9098  * Get pointer to sata_drive_info structure.
9099  *
9100  * The sata_device has to contain address (cport, pmport and qualifier) for
9101  * specified sata_scsi structure.
9102  *
9103  * Returns NULL if device address is not valid for this HBA configuration.
9104  * Otherwise, returns a pointer to sata_drive_info structure.
9105  *
9106  * This function should be called with a port mutex held.
9107  */
9108 static sata_drive_info_t *
9109 sata_get_device_info(sata_hba_inst_t *sata_hba_inst,
9110     sata_device_t *sata_device)
9111 {
9112 	uint8_t cport = sata_device->satadev_addr.cport;
9113 	uint8_t pmport = sata_device->satadev_addr.pmport;
9114 	uint8_t qual = sata_device->satadev_addr.qual;
9115 
9116 	if (cport >= SATA_NUM_CPORTS(sata_hba_inst))
9117 		return (NULL);
9118 
9119 	if (!(SATA_CPORT_STATE(sata_hba_inst, cport) &
9120 	    (SATA_STATE_PROBED | SATA_STATE_READY)))
9121 		/* Port not probed yet */
9122 		return (NULL);
9123 
9124 	if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) == SATA_DTYPE_NONE)
9125 		return (NULL);
9126 
9127 	if (qual == SATA_ADDR_DCPORT) {
9128 		/* Request for a device on a controller port */
9129 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) ==
9130 		    SATA_DTYPE_PMULT)
9131 			/* Port multiplier attached */
9132 			return (NULL);
9133 		return (SATA_CPORT_DRV_INFO(sata_hba_inst, cport));
9134 	}
9135 	if (qual == SATA_ADDR_DPMPORT) {
9136 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
9137 		    SATA_DTYPE_PMULT)
9138 			return (NULL);
9139 
9140 		if (pmport > SATA_NUM_PMPORTS(sata_hba_inst, cport))
9141 			return (NULL);
9142 
9143 		return (SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport));
9144 	}
9145 
9146 	/* we should not get here */
9147 	return (NULL);
9148 }
9149 
9150 
9151 /*
9152  * sata_identify_device.
9153  * Send Identify Device command to SATA HBA driver.
9154  * If command executes successfully, update sata_drive_info structure pointed
9155  * to by sdinfo argument, including Identify Device data.
9156  * If command fails, invalidate data in sata_drive_info.
9157  *
9158  * Cannot be called from interrupt level.
9159  *
9160  * Returns:
9161  * SATA_SUCCESS if the device was identified as a supported device,
9162  * SATA_RETRY if the device was not identified but could be retried,
9163  * SATA_FAILURE if the device was not identified and identify attempt
9164  *	should not be retried.
9165  */
9166 static int
9167 sata_identify_device(sata_hba_inst_t *sata_hba_inst,
9168     sata_drive_info_t *sdinfo)
9169 {
9170 	uint16_t cfg_word;
9171 	int rval;
9172 
9173 	/* fetch device identify data */
9174 	if ((rval = sata_fetch_device_identify_data(sata_hba_inst,
9175 	    sdinfo)) != 0)
9176 		goto fail_unknown;
9177 
9178 	cfg_word = sdinfo->satadrv_id.ai_config;
9179 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK &&
9180 	    (cfg_word & SATA_ATA_TYPE_MASK) != SATA_ATA_TYPE) {
9181 		/* Change device type to reflect Identify Device data */
9182 		if (((cfg_word & SATA_ATAPI_TYPE_MASK) ==
9183 		    SATA_ATAPI_TYPE) &&
9184 		    ((cfg_word & SATA_ATAPI_ID_DEV_TYPE) ==
9185 		    SATA_ATAPI_CDROM_DEV)) {
9186 			sdinfo->satadrv_type = SATA_DTYPE_ATAPICD;
9187 		} else {
9188 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9189 		}
9190 	} else if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD &&
9191 	    (((cfg_word & SATA_ATAPI_TYPE_MASK) != SATA_ATAPI_TYPE) ||
9192 	    ((cfg_word & SATA_ATAPI_ID_DEV_TYPE) != SATA_ATAPI_CDROM_DEV))) {
9193 		/* Change device type to reflect Identify Device data ! */
9194 		if ((sdinfo->satadrv_id.ai_config & SATA_ATA_TYPE_MASK) ==
9195 		    SATA_ATA_TYPE) {
9196 			sdinfo->satadrv_type = SATA_DTYPE_ATADISK;
9197 		} else {
9198 			sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9199 		}
9200 	}
9201 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9202 		if (sdinfo->satadrv_capacity == 0) {
9203 			/* Non-LBA disk. Too bad... */
9204 			sata_log(sata_hba_inst, CE_WARN,
9205 			    "SATA disk device at port %d does not support LBA",
9206 			    sdinfo->satadrv_addr.cport);
9207 			rval = SATA_FAILURE;
9208 			goto fail_unknown;
9209 		}
9210 	}
9211 #if 0
9212 	/* Left for historical reason */
9213 	/*
9214 	 * Some initial version of SATA spec indicated that at least
9215 	 * UDMA mode 4 has to be supported. It is not metioned in
9216 	 * SerialATA 2.6, so this restriction is removed.
9217 	 */
9218 	/* Check for Ultra DMA modes 6 through 0 being supported */
9219 	for (i = 6; i >= 0; --i) {
9220 		if (sdinfo->satadrv_id.ai_ultradma & (1 << i))
9221 			break;
9222 	}
9223 
9224 	/*
9225 	 * At least UDMA 4 mode has to be supported. If mode 4 or
9226 	 * higher are not supported by the device, fail this
9227 	 * device.
9228 	 */
9229 	if (i < 4) {
9230 		/* No required Ultra DMA mode supported */
9231 		sata_log(sata_hba_inst, CE_WARN,
9232 		    "SATA disk device at port %d does not support UDMA "
9233 		    "mode 4 or higher", sdinfo->satadrv_addr.cport);
9234 		SATA_LOG_D((sata_hba_inst, CE_WARN,
9235 		    "mode 4 or higher required, %d supported", i));
9236 		rval = SATA_FAILURE;
9237 		goto fail_unknown;
9238 	}
9239 #endif
9240 
9241 	return (SATA_SUCCESS);
9242 
9243 fail_unknown:
9244 	/* Invalidate sata_drive_info ? */
9245 	sdinfo->satadrv_type = SATA_DTYPE_UNKNOWN;
9246 	sdinfo->satadrv_state = SATA_STATE_UNKNOWN;
9247 	return (rval);
9248 }
9249 
9250 /*
9251  * Log/display device information
9252  */
9253 static void
9254 sata_show_drive_info(sata_hba_inst_t *sata_hba_inst,
9255     sata_drive_info_t *sdinfo)
9256 {
9257 	int valid_version;
9258 	char msg_buf[MAXPATHLEN];
9259 	int i;
9260 
9261 	/* Show HBA path */
9262 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), msg_buf);
9263 
9264 	cmn_err(CE_CONT, "?%s :\n", msg_buf);
9265 
9266 	if (sdinfo->satadrv_type == SATA_DTYPE_UNKNOWN) {
9267 		(void) sprintf(msg_buf,
9268 		    "Unsupported SATA device type (cfg 0x%x) at ",
9269 		    sdinfo->satadrv_id.ai_config);
9270 	} else {
9271 		(void) sprintf(msg_buf, "SATA %s device at",
9272 		    sdinfo->satadrv_type == SATA_DTYPE_ATADISK ?
9273 		    "disk":"CD/DVD (ATAPI)");
9274 	}
9275 	if (sdinfo->satadrv_addr.qual == SATA_ADDR_DCPORT)
9276 		cmn_err(CE_CONT, "?\t%s port %d\n",
9277 		    msg_buf, sdinfo->satadrv_addr.cport);
9278 	else
9279 		cmn_err(CE_CONT, "?\t%s port %d pmport %d\n",
9280 		    msg_buf, sdinfo->satadrv_addr.cport,
9281 		    sdinfo->satadrv_addr.pmport);
9282 
9283 	bcopy(&sdinfo->satadrv_id.ai_model, msg_buf,
9284 	    sizeof (sdinfo->satadrv_id.ai_model));
9285 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_model));
9286 	msg_buf[sizeof (sdinfo->satadrv_id.ai_model)] = '\0';
9287 	cmn_err(CE_CONT, "?\tmodel %s\n", msg_buf);
9288 
9289 	bcopy(&sdinfo->satadrv_id.ai_fw, msg_buf,
9290 	    sizeof (sdinfo->satadrv_id.ai_fw));
9291 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_fw));
9292 	msg_buf[sizeof (sdinfo->satadrv_id.ai_fw)] = '\0';
9293 	cmn_err(CE_CONT, "?\tfirmware %s\n", msg_buf);
9294 
9295 	bcopy(&sdinfo->satadrv_id.ai_drvser, msg_buf,
9296 	    sizeof (sdinfo->satadrv_id.ai_drvser));
9297 	swab(msg_buf, msg_buf, sizeof (sdinfo->satadrv_id.ai_drvser));
9298 	msg_buf[sizeof (sdinfo->satadrv_id.ai_drvser)] = '\0';
9299 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9300 		cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
9301 	} else {
9302 		/* Assuming ATAPI CD/DVD */
9303 		/*
9304 		 * SOme drives do not implement serial number and may
9305 		 * violate the spec by provinding spaces rather than zeros
9306 		 * in serial number field. Scan the buffer to detect it.
9307 		 */
9308 		for (i = 0; i < sizeof (sdinfo->satadrv_id.ai_drvser); i++) {
9309 			if (msg_buf[i] != '\0' && msg_buf[i] != ' ')
9310 				break;
9311 		}
9312 		if (i == sizeof (sdinfo->satadrv_id.ai_drvser)) {
9313 			cmn_err(CE_CONT, "?\tserial number - none\n");
9314 		} else {
9315 			cmn_err(CE_CONT, "?\tserial number %s\n", msg_buf);
9316 		}
9317 	}
9318 
9319 #ifdef SATA_DEBUG
9320 	if (sdinfo->satadrv_id.ai_majorversion != 0 &&
9321 	    sdinfo->satadrv_id.ai_majorversion != 0xffff) {
9322 		int i;
9323 		for (i = 14; i >= 2; i--) {
9324 			if (sdinfo->satadrv_id.ai_majorversion & (1 << i)) {
9325 				valid_version = i;
9326 				break;
9327 			}
9328 		}
9329 		cmn_err(CE_CONT,
9330 		    "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
9331 		    valid_version,
9332 		    sdinfo->satadrv_id.ai_majorversion,
9333 		    sdinfo->satadrv_id.ai_minorversion);
9334 	}
9335 #endif
9336 	/* Log some info */
9337 	cmn_err(CE_CONT, "?\tsupported features:\n");
9338 	msg_buf[0] = '\0';
9339 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9340 		if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA48)
9341 			(void) strlcat(msg_buf, "48-bit LBA, ", MAXPATHLEN);
9342 		else if (sdinfo->satadrv_features_support & SATA_DEV_F_LBA28)
9343 			(void) strlcat(msg_buf, "28-bit LBA, ", MAXPATHLEN);
9344 	}
9345 	if (sdinfo->satadrv_features_support & SATA_DEV_F_DMA)
9346 		(void) strlcat(msg_buf, "DMA", MAXPATHLEN);
9347 	if (sdinfo->satadrv_features_support & SATA_DEV_F_NCQ)
9348 		(void) strlcat(msg_buf, ", Native Command Queueing",
9349 		    MAXPATHLEN);
9350 	if (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)
9351 		(void) strlcat(msg_buf, ", Legacy Tagged Queuing", MAXPATHLEN);
9352 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_SMART_SUPPORTED) &&
9353 	    (sdinfo->satadrv_id.ai_features85 & SATA_SMART_ENABLED))
9354 		(void) strlcat(msg_buf, ", SMART", MAXPATHLEN);
9355 	if ((sdinfo->satadrv_id.ai_cmdset84 & SATA_SMART_SELF_TEST_SUPPORTED) &&
9356 	    (sdinfo->satadrv_id.ai_features87 & SATA_SMART_SELF_TEST_SUPPORTED))
9357 		(void) strlcat(msg_buf, ", SMART self-test", MAXPATHLEN);
9358 	cmn_err(CE_CONT, "?\t %s\n", msg_buf);
9359 	if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA2)
9360 		cmn_err(CE_CONT, "?\tSATA Gen2 signaling speed (3.0Gbps)\n");
9361 	else if (sdinfo->satadrv_features_support & SATA_DEV_F_SATA1)
9362 		cmn_err(CE_CONT, "?\tSATA Gen1 signaling speed (1.5Gbps)\n");
9363 	if (sdinfo->satadrv_features_support &
9364 	    (SATA_DEV_F_TCQ | SATA_DEV_F_NCQ)) {
9365 		msg_buf[0] = '\0';
9366 		(void) snprintf(msg_buf, MAXPATHLEN,
9367 		    "Supported queue depth %d",
9368 		    sdinfo->satadrv_queue_depth);
9369 		if (!(sata_func_enable &
9370 		    (SATA_ENABLE_QUEUING | SATA_ENABLE_NCQ)))
9371 			(void) strlcat(msg_buf,
9372 			    " - queueing disabled globally", MAXPATHLEN);
9373 		else if (sdinfo->satadrv_queue_depth >
9374 		    sdinfo->satadrv_max_queue_depth) {
9375 			(void) snprintf(&msg_buf[strlen(msg_buf)],
9376 			    MAXPATHLEN - strlen(msg_buf), ", limited to %d",
9377 			    (int)sdinfo->satadrv_max_queue_depth);
9378 		}
9379 		cmn_err(CE_CONT, "?\t%s\n", msg_buf);
9380 	}
9381 
9382 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9383 #ifdef __i386
9384 		(void) sprintf(msg_buf, "\tcapacity = %llu sectors\n",
9385 		    sdinfo->satadrv_capacity);
9386 #else
9387 		(void) sprintf(msg_buf, "\tcapacity = %lu sectors\n",
9388 		    sdinfo->satadrv_capacity);
9389 #endif
9390 		cmn_err(CE_CONT, "?%s", msg_buf);
9391 	}
9392 }
9393 
9394 
9395 /*
9396  * sata_save_drive_settings extracts current setting of the device and stores
9397  * it for future reference, in case the device setup would need to be restored
9398  * after the device reset.
9399  *
9400  * For all devices read ahead and write cache settings are saved, if the
9401  * device supports these features at all.
9402  * For ATAPI devices the Removable Media Status Notification setting is saved.
9403  */
9404 static void
9405 sata_save_drive_settings(sata_drive_info_t *sdinfo)
9406 {
9407 	if ((sdinfo->satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) ||
9408 	    (sdinfo->satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) {
9409 
9410 		/* Current setting of Read Ahead (and Read Cache) */
9411 		if (sdinfo->satadrv_id.ai_features85 & SATA_LOOK_AHEAD)
9412 			sdinfo->satadrv_settings |= SATA_DEV_READ_AHEAD;
9413 		else
9414 			sdinfo->satadrv_settings &= ~SATA_DEV_READ_AHEAD;
9415 
9416 		/* Current setting of Write Cache */
9417 		if (sdinfo->satadrv_id.ai_features85 & SATA_WRITE_CACHE)
9418 			sdinfo->satadrv_settings |= SATA_DEV_WRITE_CACHE;
9419 		else
9420 			sdinfo->satadrv_settings &= ~SATA_DEV_WRITE_CACHE;
9421 	}
9422 
9423 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
9424 		if (sdinfo->satadrv_id.ai_cmdset83 & SATA_RM_STATUS_NOTIFIC)
9425 			sdinfo->satadrv_settings |= SATA_DEV_RMSN;
9426 		else
9427 			sdinfo->satadrv_settings &= ~SATA_DEV_RMSN;
9428 	}
9429 }
9430 
9431 
9432 /*
9433  * sata_check_capacity function determines a disk capacity
9434  * and addressing mode (LBA28/LBA48) by examining a disk identify device data.
9435  *
9436  * NOTE: CHS mode is not supported! If a device does not support LBA,
9437  * this function is not called.
9438  *
9439  * Returns device capacity in number of blocks, i.e. largest addressable LBA+1
9440  */
9441 static uint64_t
9442 sata_check_capacity(sata_drive_info_t *sdinfo)
9443 {
9444 	uint64_t capacity = 0;
9445 	int i;
9446 
9447 	if (sdinfo->satadrv_type != SATA_DTYPE_ATADISK ||
9448 	    !sdinfo->satadrv_id.ai_cap & SATA_LBA_SUPPORT)
9449 		/* Capacity valid only for LBA-addressable disk devices */
9450 		return (0);
9451 
9452 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) &&
9453 	    (sdinfo->satadrv_id.ai_cmdset83 & SATA_EXT48) &&
9454 	    (sdinfo->satadrv_id.ai_features86 & SATA_EXT48)) {
9455 		/* LBA48 mode supported and enabled */
9456 		sdinfo->satadrv_features_support |= SATA_DEV_F_LBA48 |
9457 		    SATA_DEV_F_LBA28;
9458 		for (i = 3;  i >= 0;  --i) {
9459 			capacity <<= 16;
9460 			capacity += sdinfo->satadrv_id.ai_addrsecxt[i];
9461 		}
9462 	} else {
9463 		capacity = sdinfo->satadrv_id.ai_addrsec[1];
9464 		capacity <<= 16;
9465 		capacity += sdinfo->satadrv_id.ai_addrsec[0];
9466 		if (capacity >= 0x1000000)
9467 			/* LBA28 mode */
9468 			sdinfo->satadrv_features_support |= SATA_DEV_F_LBA28;
9469 	}
9470 	return (capacity);
9471 }
9472 
9473 
9474 /*
9475  * Allocate consistent buffer for DMA transfer
9476  *
9477  * Cannot be called from interrupt level or with mutex held - it may sleep.
9478  *
9479  * Returns pointer to allocated buffer structure, or NULL if allocation failed.
9480  */
9481 static struct buf *
9482 sata_alloc_local_buffer(sata_pkt_txlate_t *spx, int len)
9483 {
9484 	struct scsi_address ap;
9485 	struct buf *bp;
9486 	ddi_dma_attr_t	cur_dma_attr;
9487 
9488 	ASSERT(spx->txlt_sata_pkt != NULL);
9489 	ap.a_hba_tran = spx->txlt_sata_hba_inst->satahba_scsi_tran;
9490 	ap.a_target = SATA_TO_SCSI_TARGET(
9491 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.cport,
9492 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.pmport,
9493 	    spx->txlt_sata_pkt->satapkt_device.satadev_addr.qual);
9494 	ap.a_lun = 0;
9495 
9496 	bp = scsi_alloc_consistent_buf(&ap, NULL, len,
9497 	    B_READ, SLEEP_FUNC, NULL);
9498 
9499 	if (bp != NULL) {
9500 		/* Allocate DMA resources for this buffer */
9501 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = bp;
9502 		/*
9503 		 * We use a local version of the dma_attr, to account
9504 		 * for a device addressing limitations.
9505 		 * sata_adjust_dma_attr() will handle sdinfo == NULL which
9506 		 * will cause dma attributes to be adjusted to a lowest
9507 		 * acceptable level.
9508 		 */
9509 		sata_adjust_dma_attr(NULL,
9510 		    SATA_DMA_ATTR(spx->txlt_sata_hba_inst), &cur_dma_attr);
9511 
9512 		if (sata_dma_buf_setup(spx, PKT_CONSISTENT,
9513 		    SLEEP_FUNC, NULL, &cur_dma_attr) != DDI_SUCCESS) {
9514 			scsi_free_consistent_buf(bp);
9515 			spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
9516 			bp = NULL;
9517 		}
9518 	}
9519 	return (bp);
9520 }
9521 
9522 /*
9523  * Release local buffer (consistent buffer for DMA transfer) allocated
9524  * via sata_alloc_local_buffer().
9525  */
9526 static void
9527 sata_free_local_buffer(sata_pkt_txlate_t *spx)
9528 {
9529 	ASSERT(spx->txlt_sata_pkt != NULL);
9530 	ASSERT(spx->txlt_dma_cookie_list != NULL);
9531 	ASSERT(spx->txlt_dma_cookie_list_len != 0);
9532 	ASSERT(spx->txlt_buf_dma_handle != NULL);
9533 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp != NULL);
9534 
9535 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 0;
9536 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list = NULL;
9537 
9538 	/* Free DMA resources */
9539 	(void) ddi_dma_unbind_handle(spx->txlt_buf_dma_handle);
9540 	ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
9541 	spx->txlt_buf_dma_handle = 0;
9542 
9543 	if (spx->txlt_dma_cookie_list != &spx->txlt_dma_cookie) {
9544 		kmem_free(spx->txlt_dma_cookie_list,
9545 		    spx->txlt_dma_cookie_list_len * sizeof (ddi_dma_cookie_t));
9546 		spx->txlt_dma_cookie_list = NULL;
9547 		spx->txlt_dma_cookie_list_len = 0;
9548 	}
9549 	/* Free buffer */
9550 	scsi_free_consistent_buf(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp);
9551 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp = NULL;
9552 }
9553 
9554 
9555 
9556 
9557 /*
9558  * Allocate sata_pkt
9559  * Pkt structure version and embedded strcutures version are initialized.
9560  * sata_pkt and sata_pkt_txlate structures are cross-linked.
9561  *
9562  * Since this may be called in interrupt context by sata_scsi_init_pkt,
9563  * callback argument determines if it can sleep or not.
9564  * Hence, it should not be called from interrupt context.
9565  *
9566  * If successful, non-NULL pointer to a sata pkt is returned.
9567  * Upon failure, NULL pointer is returned.
9568  */
9569 static sata_pkt_t *
9570 sata_pkt_alloc(sata_pkt_txlate_t *spx, int (*callback)(caddr_t))
9571 {
9572 	sata_pkt_t *spkt;
9573 	int kmsflag;
9574 
9575 	kmsflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP;
9576 	spkt = kmem_zalloc(sizeof (sata_pkt_t), kmsflag);
9577 	if (spkt == NULL) {
9578 		SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9579 		    "sata_pkt_alloc: failed"));
9580 		return (NULL);
9581 	}
9582 	spkt->satapkt_rev = SATA_PKT_REV;
9583 	spkt->satapkt_cmd.satacmd_rev = SATA_CMD_REV;
9584 	spkt->satapkt_device.satadev_rev = SATA_DEVICE_REV;
9585 	spkt->satapkt_framework_private = spx;
9586 	spx->txlt_sata_pkt = spkt;
9587 	return (spkt);
9588 }
9589 
9590 /*
9591  * Free sata pkt allocated via sata_pkt_alloc()
9592  */
9593 static void
9594 sata_pkt_free(sata_pkt_txlate_t *spx)
9595 {
9596 	ASSERT(spx->txlt_sata_pkt != NULL);
9597 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp == NULL);
9598 	kmem_free(spx->txlt_sata_pkt, sizeof (sata_pkt_t));
9599 	spx->txlt_sata_pkt = NULL;
9600 }
9601 
9602 
9603 /*
9604  * Adjust DMA attributes.
9605  * SCSI cmds block count is up to 24 bits, SATA cmd block count vary
9606  * from 8 bits to 16 bits, depending on a command being used.
9607  * Limiting max block count arbitrarily to 256 for all read/write
9608  * commands may affects performance, so check both the device and
9609  * controller capability before adjusting dma attributes.
9610  */
9611 void
9612 sata_adjust_dma_attr(sata_drive_info_t *sdinfo, ddi_dma_attr_t *dma_attr,
9613     ddi_dma_attr_t *adj_dma_attr)
9614 {
9615 	uint32_t count_max;
9616 
9617 	/* Copy original attributes */
9618 	*adj_dma_attr = *dma_attr;
9619 	/*
9620 	 * Things to consider: device addressing capability,
9621 	 * "excessive" controller DMA capabilities.
9622 	 * If a device is being probed/initialized, there are
9623 	 * no device info - use default limits then.
9624 	 */
9625 	if (sdinfo == NULL) {
9626 		count_max = dma_attr->dma_attr_granular * 0x100;
9627 		if (dma_attr->dma_attr_count_max > count_max)
9628 			adj_dma_attr->dma_attr_count_max = count_max;
9629 		if (dma_attr->dma_attr_maxxfer > count_max)
9630 			adj_dma_attr->dma_attr_maxxfer = count_max;
9631 		return;
9632 	}
9633 
9634 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
9635 		if (sdinfo->satadrv_features_support & (SATA_DEV_F_LBA48)) {
9636 			/*
9637 			 * 16-bit sector count may be used - we rely on
9638 			 * the assumption that only read and write cmds
9639 			 * will request more than 256 sectors worth of data
9640 			 */
9641 			count_max = adj_dma_attr->dma_attr_granular * 0x10000;
9642 		} else {
9643 			/*
9644 			 * 8-bit sector count will be used - default limits
9645 			 * for dma attributes
9646 			 */
9647 			count_max = adj_dma_attr->dma_attr_granular * 0x100;
9648 		}
9649 		/*
9650 		 * Adjust controler dma attributes, if necessary
9651 		 */
9652 		if (dma_attr->dma_attr_count_max > count_max)
9653 			adj_dma_attr->dma_attr_count_max = count_max;
9654 		if (dma_attr->dma_attr_maxxfer > count_max)
9655 			adj_dma_attr->dma_attr_maxxfer = count_max;
9656 	}
9657 }
9658 
9659 
9660 /*
9661  * Allocate DMA resources for the buffer
9662  * This function handles initial DMA resource allocation as well as
9663  * DMA window shift and may be called repeatedly for the same DMA window
9664  * until all DMA cookies in the DMA window are processed.
9665  * To guarantee that there is always a coherent set of cookies to process
9666  * by SATA HBA driver (observing alignment, device granularity, etc.),
9667  * the number of slots for DMA cookies is equal to lesser of  a number of
9668  * cookies in a DMA window and a max number of scatter/gather entries.
9669  *
9670  * Returns DDI_SUCCESS upon successful operation.
9671  * Return failure code of a failing command or DDI_FAILURE when
9672  * internal cleanup failed.
9673  */
9674 static int
9675 sata_dma_buf_setup(sata_pkt_txlate_t *spx, int flags,
9676     int (*callback)(caddr_t), caddr_t arg,
9677     ddi_dma_attr_t *cur_dma_attr)
9678 {
9679 	int	rval;
9680 	off_t	offset;
9681 	size_t	size;
9682 	int	max_sg_len, req_len, i;
9683 	uint_t	dma_flags;
9684 	struct buf	*bp;
9685 	uint64_t	cur_txfer_len;
9686 
9687 
9688 	ASSERT(spx->txlt_sata_pkt != NULL);
9689 	bp = spx->txlt_sata_pkt->satapkt_cmd.satacmd_bp;
9690 	ASSERT(bp != NULL);
9691 
9692 
9693 	if (spx->txlt_buf_dma_handle == NULL) {
9694 		/*
9695 		 * No DMA resources allocated so far - this is a first call
9696 		 * for this sata pkt.
9697 		 */
9698 		rval = ddi_dma_alloc_handle(SATA_DIP(spx->txlt_sata_hba_inst),
9699 		    cur_dma_attr, callback, arg, &spx->txlt_buf_dma_handle);
9700 
9701 		if (rval != DDI_SUCCESS) {
9702 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9703 			    "sata_dma_buf_setup: no buf DMA resources %x",
9704 			    rval));
9705 			return (rval);
9706 		}
9707 
9708 		if (bp->b_flags & B_READ)
9709 			dma_flags = DDI_DMA_READ;
9710 		else
9711 			dma_flags = DDI_DMA_WRITE;
9712 
9713 		if (flags & PKT_CONSISTENT)
9714 			dma_flags |= DDI_DMA_CONSISTENT;
9715 
9716 		if (flags & PKT_DMA_PARTIAL)
9717 			dma_flags |= DDI_DMA_PARTIAL;
9718 
9719 		/*
9720 		 * Check buffer alignment and size against dma attributes
9721 		 * Consider dma_attr_align only. There may be requests
9722 		 * with the size lower than device granularity, but they
9723 		 * will not read/write from/to the device, so no adjustment
9724 		 * is necessary. The dma_attr_minxfer theoretically should
9725 		 * be considered, but no HBA driver is checking it.
9726 		 */
9727 		if (IS_P2ALIGNED(bp->b_un.b_addr,
9728 		    cur_dma_attr->dma_attr_align)) {
9729 			rval = ddi_dma_buf_bind_handle(
9730 			    spx->txlt_buf_dma_handle,
9731 			    bp, dma_flags, callback, arg,
9732 			    &spx->txlt_dma_cookie,
9733 			    &spx->txlt_curwin_num_dma_cookies);
9734 		} else { /* Buffer is not aligned */
9735 
9736 			int	(*ddicallback)(caddr_t);
9737 			size_t	bufsz;
9738 
9739 			/* Check id sleeping is allowed */
9740 			ddicallback = (callback == NULL_FUNC) ?
9741 			    DDI_DMA_DONTWAIT : DDI_DMA_SLEEP;
9742 
9743 			SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
9744 			    "mis-aligned buffer: addr=0x%p, cnt=%lu",
9745 			    (void *)bp->b_un.b_addr, bp->b_bcount);
9746 
9747 			if (bp->b_flags & (B_PAGEIO|B_PHYS))
9748 				/*
9749 				 * CPU will need to access data in the buffer
9750 				 * (for copying) so map it.
9751 				 */
9752 				bp_mapin(bp);
9753 
9754 			ASSERT(spx->txlt_tmp_buf == NULL);
9755 
9756 			/* Buffer may be padded by ddi_dma_mem_alloc()! */
9757 			rval = ddi_dma_mem_alloc(
9758 			    spx->txlt_buf_dma_handle,
9759 			    bp->b_bcount,
9760 			    &sata_acc_attr,
9761 			    DDI_DMA_STREAMING,
9762 			    ddicallback, NULL,
9763 			    &spx->txlt_tmp_buf,
9764 			    &bufsz,
9765 			    &spx->txlt_tmp_buf_handle);
9766 
9767 			if (rval != DDI_SUCCESS) {
9768 				/* DMA mapping failed */
9769 				(void) ddi_dma_free_handle(
9770 				    &spx->txlt_buf_dma_handle);
9771 				spx->txlt_buf_dma_handle = NULL;
9772 #ifdef SATA_DEBUG
9773 				mbuffail_count++;
9774 #endif
9775 				SATADBG1(SATA_DBG_DMA_SETUP,
9776 				    spx->txlt_sata_hba_inst,
9777 				    "sata_dma_buf_setup: "
9778 				    "buf dma mem alloc failed %x\n", rval);
9779 				return (rval);
9780 			}
9781 			ASSERT(IS_P2ALIGNED(spx->txlt_tmp_buf,
9782 			    cur_dma_attr->dma_attr_align));
9783 
9784 #ifdef SATA_DEBUG
9785 			mbuf_count++;
9786 
9787 			if (bp->b_bcount != bufsz)
9788 				/*
9789 				 * This will require special handling, because
9790 				 * DMA cookies will be based on the temporary
9791 				 * buffer size, not the original buffer
9792 				 * b_bcount, so the residue may have to
9793 				 * be counted differently.
9794 				 */
9795 				SATADBG2(SATA_DBG_DMA_SETUP,
9796 				    spx->txlt_sata_hba_inst,
9797 				    "sata_dma_buf_setup: bp size %x != "
9798 				    "bufsz %x\n", bp->b_bcount, bufsz);
9799 #endif
9800 			if (dma_flags & DDI_DMA_WRITE) {
9801 				/*
9802 				 * Write operation - copy data into
9803 				 * an aligned temporary buffer. Buffer will be
9804 				 * synced for device by ddi_dma_addr_bind_handle
9805 				 */
9806 				bcopy(bp->b_un.b_addr, spx->txlt_tmp_buf,
9807 				    bp->b_bcount);
9808 			}
9809 
9810 			rval = ddi_dma_addr_bind_handle(
9811 			    spx->txlt_buf_dma_handle,
9812 			    NULL,
9813 			    spx->txlt_tmp_buf,
9814 			    bufsz, dma_flags, ddicallback, 0,
9815 			    &spx->txlt_dma_cookie,
9816 			    &spx->txlt_curwin_num_dma_cookies);
9817 		}
9818 
9819 		switch (rval) {
9820 		case DDI_DMA_PARTIAL_MAP:
9821 			SATADBG1(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
9822 			    "sata_dma_buf_setup: DMA Partial Map\n", NULL);
9823 			/*
9824 			 * Partial DMA mapping.
9825 			 * Retrieve number of DMA windows for this request.
9826 			 */
9827 			if (ddi_dma_numwin(spx->txlt_buf_dma_handle,
9828 			    &spx->txlt_num_dma_win) != DDI_SUCCESS) {
9829 				if (spx->txlt_tmp_buf != NULL) {
9830 					ddi_dma_mem_free(
9831 					    &spx->txlt_tmp_buf_handle);
9832 					spx->txlt_tmp_buf = NULL;
9833 				}
9834 				(void) ddi_dma_unbind_handle(
9835 				    spx->txlt_buf_dma_handle);
9836 				(void) ddi_dma_free_handle(
9837 				    &spx->txlt_buf_dma_handle);
9838 				spx->txlt_buf_dma_handle = NULL;
9839 				SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9840 				    "sata_dma_buf_setup: numwin failed\n"));
9841 				return (DDI_FAILURE);
9842 			}
9843 			SATADBG2(SATA_DBG_DMA_SETUP,
9844 			    spx->txlt_sata_hba_inst,
9845 			    "sata_dma_buf_setup: windows: %d, cookies: %d\n",
9846 			    spx->txlt_num_dma_win,
9847 			    spx->txlt_curwin_num_dma_cookies);
9848 			spx->txlt_cur_dma_win = 0;
9849 			break;
9850 
9851 		case DDI_DMA_MAPPED:
9852 			/* DMA fully mapped */
9853 			spx->txlt_num_dma_win = 1;
9854 			spx->txlt_cur_dma_win = 0;
9855 			SATADBG1(SATA_DBG_DMA_SETUP,
9856 			    spx->txlt_sata_hba_inst,
9857 			    "sata_dma_buf_setup: windows: 1 "
9858 			    "cookies: %d\n", spx->txlt_curwin_num_dma_cookies);
9859 			break;
9860 
9861 		default:
9862 			/* DMA mapping failed */
9863 			if (spx->txlt_tmp_buf != NULL) {
9864 				ddi_dma_mem_free(
9865 				    &spx->txlt_tmp_buf_handle);
9866 				spx->txlt_tmp_buf = NULL;
9867 			}
9868 			(void) ddi_dma_free_handle(&spx->txlt_buf_dma_handle);
9869 			spx->txlt_buf_dma_handle = NULL;
9870 			SATA_LOG_D((spx->txlt_sata_hba_inst, CE_WARN,
9871 			    "sata_dma_buf_setup: buf dma handle binding "
9872 			    "failed %x\n", rval));
9873 			return (rval);
9874 		}
9875 		spx->txlt_curwin_processed_dma_cookies = 0;
9876 		spx->txlt_dma_cookie_list = NULL;
9877 	} else {
9878 		/*
9879 		 * DMA setup is reused. Check if we need to process more
9880 		 * cookies in current window, or to get next window, if any.
9881 		 */
9882 
9883 		ASSERT(spx->txlt_curwin_processed_dma_cookies <=
9884 		    spx->txlt_curwin_num_dma_cookies);
9885 
9886 		if (spx->txlt_curwin_processed_dma_cookies ==
9887 		    spx->txlt_curwin_num_dma_cookies) {
9888 			/*
9889 			 * All cookies from current DMA window were processed.
9890 			 * Get next DMA window.
9891 			 */
9892 			spx->txlt_cur_dma_win++;
9893 			if (spx->txlt_cur_dma_win < spx->txlt_num_dma_win) {
9894 				(void) ddi_dma_getwin(spx->txlt_buf_dma_handle,
9895 				    spx->txlt_cur_dma_win, &offset, &size,
9896 				    &spx->txlt_dma_cookie,
9897 				    &spx->txlt_curwin_num_dma_cookies);
9898 				spx->txlt_curwin_processed_dma_cookies = 0;
9899 			} else {
9900 				/* No more windows! End of request! */
9901 				/* What to do? - panic for now */
9902 				ASSERT(spx->txlt_cur_dma_win >=
9903 				    spx->txlt_num_dma_win);
9904 
9905 				spx->txlt_curwin_num_dma_cookies = 0;
9906 				spx->txlt_curwin_processed_dma_cookies = 0;
9907 				spx->txlt_sata_pkt->
9908 				    satapkt_cmd.satacmd_num_dma_cookies = 0;
9909 				return (DDI_SUCCESS);
9910 			}
9911 		}
9912 	}
9913 	/* There better be at least one DMA cookie outstanding */
9914 	ASSERT((spx->txlt_curwin_num_dma_cookies -
9915 	    spx->txlt_curwin_processed_dma_cookies) > 0);
9916 
9917 	if (spx->txlt_dma_cookie_list == &spx->txlt_dma_cookie) {
9918 		/* The default cookie slot was used in previous run */
9919 		ASSERT(spx->txlt_curwin_processed_dma_cookies == 0);
9920 		spx->txlt_dma_cookie_list = NULL;
9921 		spx->txlt_dma_cookie_list_len = 0;
9922 	}
9923 	if (spx->txlt_curwin_processed_dma_cookies == 0) {
9924 		/*
9925 		 * Processing a new DMA window - set-up dma cookies list.
9926 		 * We may reuse previously allocated cookie array if it is
9927 		 * possible.
9928 		 */
9929 		if (spx->txlt_dma_cookie_list != NULL &&
9930 		    spx->txlt_dma_cookie_list_len <
9931 		    spx->txlt_curwin_num_dma_cookies) {
9932 			/*
9933 			 * New DMA window contains more cookies than
9934 			 * the previous one. We need larger cookie list - free
9935 			 * the old one.
9936 			 */
9937 			(void) kmem_free(spx->txlt_dma_cookie_list,
9938 			    spx->txlt_dma_cookie_list_len *
9939 			    sizeof (ddi_dma_cookie_t));
9940 			spx->txlt_dma_cookie_list = NULL;
9941 			spx->txlt_dma_cookie_list_len = 0;
9942 		}
9943 		if (spx->txlt_dma_cookie_list == NULL) {
9944 			/*
9945 			 * Calculate lesser of number of cookies in this
9946 			 * DMA window and number of s/g entries.
9947 			 */
9948 			max_sg_len = cur_dma_attr->dma_attr_sgllen;
9949 			req_len = MIN(max_sg_len,
9950 			    spx->txlt_curwin_num_dma_cookies);
9951 
9952 			/* Allocate new dma cookie array if necessary */
9953 			if (req_len == 1) {
9954 				/* Only one cookie - no need for a list */
9955 				spx->txlt_dma_cookie_list =
9956 				    &spx->txlt_dma_cookie;
9957 				spx->txlt_dma_cookie_list_len = 1;
9958 			} else {
9959 				/*
9960 				 * More than one cookie - try to allocate space.
9961 				 */
9962 				spx->txlt_dma_cookie_list = kmem_zalloc(
9963 				    sizeof (ddi_dma_cookie_t) * req_len,
9964 				    callback == NULL_FUNC ? KM_NOSLEEP :
9965 				    KM_SLEEP);
9966 				if (spx->txlt_dma_cookie_list == NULL) {
9967 					SATADBG1(SATA_DBG_DMA_SETUP,
9968 					    spx->txlt_sata_hba_inst,
9969 					    "sata_dma_buf_setup: cookie list "
9970 					    "allocation failed\n", NULL);
9971 					/*
9972 					 * We could not allocate space for
9973 					 * neccessary number of dma cookies in
9974 					 * this window, so we fail this request.
9975 					 * Next invocation would try again to
9976 					 * allocate space for cookie list.
9977 					 * Note:Packet residue was not modified.
9978 					 */
9979 					return (DDI_DMA_NORESOURCES);
9980 				} else {
9981 					spx->txlt_dma_cookie_list_len = req_len;
9982 				}
9983 			}
9984 		}
9985 		/*
9986 		 * Fetch DMA cookies into cookie list in sata_pkt_txlate.
9987 		 * First cookie was already fetched.
9988 		 */
9989 		*(&spx->txlt_dma_cookie_list[0]) = spx->txlt_dma_cookie;
9990 		cur_txfer_len =
9991 		    (uint64_t)spx->txlt_dma_cookie_list[0].dmac_size;
9992 		spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies = 1;
9993 		spx->txlt_curwin_processed_dma_cookies++;
9994 		for (i = 1; (i < spx->txlt_dma_cookie_list_len) &&
9995 		    (i < spx->txlt_curwin_num_dma_cookies); i++) {
9996 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
9997 			    &spx->txlt_dma_cookie_list[i]);
9998 			cur_txfer_len +=
9999 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
10000 			spx->txlt_curwin_processed_dma_cookies++;
10001 			spx->txlt_sata_pkt->
10002 			    satapkt_cmd.satacmd_num_dma_cookies += 1;
10003 		}
10004 	} else {
10005 		SATADBG2(SATA_DBG_DMA_SETUP, spx->txlt_sata_hba_inst,
10006 		    "sata_dma_buf_setup: sliding within DMA window, "
10007 		    "cur cookie %d, total cookies %d\n",
10008 		    spx->txlt_curwin_processed_dma_cookies,
10009 		    spx->txlt_curwin_num_dma_cookies);
10010 
10011 		/*
10012 		 * Not all cookies from the current dma window were used because
10013 		 * of s/g limitation.
10014 		 * There is no need to re-size the list - it was set at
10015 		 * optimal size, or only default entry is used (s/g = 1).
10016 		 */
10017 		if (spx->txlt_dma_cookie_list == NULL) {
10018 			spx->txlt_dma_cookie_list = &spx->txlt_dma_cookie;
10019 			spx->txlt_dma_cookie_list_len = 1;
10020 		}
10021 		/*
10022 		 * Since we are processing remaining cookies in a DMA window,
10023 		 * there may be less of them than the number of entries in the
10024 		 * current dma cookie list.
10025 		 */
10026 		req_len = MIN(spx->txlt_dma_cookie_list_len,
10027 		    (spx->txlt_curwin_num_dma_cookies -
10028 		    spx->txlt_curwin_processed_dma_cookies));
10029 
10030 		/* Fetch the next batch of cookies */
10031 		for (i = 0, cur_txfer_len = 0; i < req_len; i++) {
10032 			ddi_dma_nextcookie(spx->txlt_buf_dma_handle,
10033 			    &spx->txlt_dma_cookie_list[i]);
10034 			cur_txfer_len +=
10035 			    (uint64_t)spx->txlt_dma_cookie_list[i].dmac_size;
10036 			spx->txlt_sata_pkt->
10037 			    satapkt_cmd.satacmd_num_dma_cookies++;
10038 			spx->txlt_curwin_processed_dma_cookies++;
10039 		}
10040 	}
10041 
10042 	ASSERT(spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies > 0);
10043 
10044 	/* Point sata_cmd to the cookie list */
10045 	spx->txlt_sata_pkt->satapkt_cmd.satacmd_dma_cookie_list =
10046 	    &spx->txlt_dma_cookie_list[0];
10047 
10048 	/* Remember number of DMA cookies passed in sata packet */
10049 	spx->txlt_num_dma_cookies =
10050 	    spx->txlt_sata_pkt->satapkt_cmd.satacmd_num_dma_cookies;
10051 
10052 	ASSERT(cur_txfer_len != 0);
10053 	if (cur_txfer_len <= bp->b_bcount)
10054 		spx->txlt_total_residue -= cur_txfer_len;
10055 	else {
10056 		/*
10057 		 * Temporary DMA buffer has been padded by
10058 		 * ddi_dma_mem_alloc()!
10059 		 * This requires special handling, because DMA cookies are
10060 		 * based on the temporary buffer size, not the b_bcount,
10061 		 * and we have extra bytes to transfer - but the packet
10062 		 * residue has to stay correct because we will copy only
10063 		 * the requested number of bytes.
10064 		 */
10065 		spx->txlt_total_residue -= bp->b_bcount;
10066 	}
10067 
10068 	return (DDI_SUCCESS);
10069 }
10070 
10071 
10072 /*
10073  * Fetch Device Identify data.
10074  * Send DEVICE IDENTIFY or IDENTIFY PACKET DEVICE (depending on a device type)
10075  * command to a device and get the device identify data.
10076  * The device_info structure has to be set to device type (for selecting proper
10077  * device identify command).
10078  *
10079  * Returns:
10080  * SATA_SUCCESS if cmd succeeded
10081  * SATA_RETRY if cmd was rejected and could be retried,
10082  * SATA_FAILURE if cmd failed and should not be retried (port error)
10083  *
10084  * Cannot be called in an interrupt context.
10085  */
10086 
10087 static int
10088 sata_fetch_device_identify_data(sata_hba_inst_t *sata_hba_inst,
10089     sata_drive_info_t *sdinfo)
10090 {
10091 	struct buf *bp;
10092 	sata_pkt_t *spkt;
10093 	sata_cmd_t *scmd;
10094 	sata_pkt_txlate_t *spx;
10095 	int rval;
10096 
10097 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10098 	spx->txlt_sata_hba_inst = sata_hba_inst;
10099 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
10100 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10101 	if (spkt == NULL) {
10102 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10103 		return (SATA_RETRY); /* may retry later */
10104 	}
10105 	/* address is needed now */
10106 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10107 
10108 	/*
10109 	 * Allocate buffer for Identify Data return data
10110 	 */
10111 	bp = sata_alloc_local_buffer(spx, sizeof (sata_id_t));
10112 	if (bp == NULL) {
10113 		sata_pkt_free(spx);
10114 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
10115 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10116 		    "sata_fetch_device_identify_data: "
10117 		    "cannot allocate buffer for ID"));
10118 		return (SATA_RETRY); /* may retry later */
10119 	}
10120 
10121 	/* Fill sata_pkt */
10122 	sdinfo->satadrv_state = SATA_STATE_PROBING;
10123 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10124 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10125 	/* Synchronous mode, no callback */
10126 	spkt->satapkt_comp = NULL;
10127 	/* Timeout 30s */
10128 	spkt->satapkt_time = sata_default_pkt_time;
10129 
10130 	scmd = &spkt->satapkt_cmd;
10131 	scmd->satacmd_bp = bp;
10132 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
10133 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10134 
10135 	/* Build Identify Device cmd in the sata_pkt */
10136 	scmd->satacmd_addr_type = 0;		/* N/A */
10137 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
10138 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
10139 	scmd->satacmd_lba_mid_lsb = 0;		/* N/A */
10140 	scmd->satacmd_lba_high_lsb = 0;		/* N/A */
10141 	scmd->satacmd_features_reg = 0;		/* N/A */
10142 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
10143 	if (sdinfo->satadrv_type == SATA_DTYPE_ATAPICD) {
10144 		/* Identify Packet Device cmd */
10145 		scmd->satacmd_cmd_reg = SATAC_ID_PACKET_DEVICE;
10146 	} else {
10147 		/* Identify Device cmd - mandatory for all other devices */
10148 		scmd->satacmd_cmd_reg = SATAC_ID_DEVICE;
10149 	}
10150 
10151 	/* Send pkt to SATA HBA driver */
10152 	rval = (*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt);
10153 	if (rval == SATA_TRAN_ACCEPTED &&
10154 	    spkt->satapkt_reason == SATA_PKT_COMPLETED) {
10155 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
10156 		    DDI_DMA_SYNC_FORKERNEL);
10157 		ASSERT(rval == DDI_SUCCESS);
10158 		if ((((sata_id_t *)(bp->b_un.b_addr))->ai_config &
10159 		    SATA_INCOMPLETE_DATA) == SATA_INCOMPLETE_DATA) {
10160 			SATA_LOG_D((sata_hba_inst, CE_WARN,
10161 			    "SATA disk device at port %d - "
10162 			    "partial Identify Data",
10163 			    sdinfo->satadrv_addr.cport));
10164 			rval = SATA_RETRY; /* may retry later */
10165 			goto fail;
10166 		}
10167 		/* Update sata_drive_info */
10168 		bcopy(bp->b_un.b_addr, &sdinfo->satadrv_id,
10169 		    sizeof (sata_id_t));
10170 
10171 		sdinfo->satadrv_features_support = 0;
10172 		if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
10173 			/*
10174 			 * Retrieve capacity (disks only) and addressing mode
10175 			 */
10176 			sdinfo->satadrv_capacity = sata_check_capacity(sdinfo);
10177 		} else {
10178 			/*
10179 			 * For ATAPI devices one would have to issue
10180 			 * Get Capacity cmd for media capacity. Not here.
10181 			 */
10182 			sdinfo->satadrv_capacity = 0;
10183 			/*
10184 			 * Check what cdb length is supported
10185 			 */
10186 			if ((sdinfo->satadrv_id.ai_config &
10187 			    SATA_ATAPI_ID_PKT_SZ) == SATA_ATAPI_ID_PKT_16B)
10188 				sdinfo->satadrv_atapi_cdb_len = 16;
10189 			else
10190 				sdinfo->satadrv_atapi_cdb_len = 12;
10191 		}
10192 		/* Setup supported features flags */
10193 		if (sdinfo->satadrv_id.ai_cap & SATA_DMA_SUPPORT)
10194 			sdinfo->satadrv_features_support |= SATA_DEV_F_DMA;
10195 
10196 		/* Check for SATA GEN and NCQ support */
10197 		if (sdinfo->satadrv_id.ai_satacap != 0 &&
10198 		    sdinfo->satadrv_id.ai_satacap != 0xffff) {
10199 			/* SATA compliance */
10200 			if (sdinfo->satadrv_id.ai_satacap & SATA_NCQ)
10201 				sdinfo->satadrv_features_support |=
10202 				    SATA_DEV_F_NCQ;
10203 			if (sdinfo->satadrv_id.ai_satacap &
10204 			    (SATA_1_SPEED | SATA_2_SPEED)) {
10205 				if (sdinfo->satadrv_id.ai_satacap &
10206 				    SATA_2_SPEED)
10207 					sdinfo->satadrv_features_support |=
10208 					    SATA_DEV_F_SATA2;
10209 				if (sdinfo->satadrv_id.ai_satacap &
10210 				    SATA_1_SPEED)
10211 					sdinfo->satadrv_features_support |=
10212 					    SATA_DEV_F_SATA1;
10213 			} else {
10214 				sdinfo->satadrv_features_support |=
10215 				    SATA_DEV_F_SATA1;
10216 			}
10217 		}
10218 		if ((sdinfo->satadrv_id.ai_cmdset83 & SATA_RW_DMA_QUEUED_CMD) &&
10219 		    (sdinfo->satadrv_id.ai_features86 & SATA_RW_DMA_QUEUED_CMD))
10220 			sdinfo->satadrv_features_support |= SATA_DEV_F_TCQ;
10221 
10222 		sdinfo->satadrv_queue_depth = sdinfo->satadrv_id.ai_qdepth;
10223 		if ((sdinfo->satadrv_features_support & SATA_DEV_F_NCQ) ||
10224 		    (sdinfo->satadrv_features_support & SATA_DEV_F_TCQ)) {
10225 			++sdinfo->satadrv_queue_depth;
10226 			/* Adjust according to controller capabilities */
10227 			sdinfo->satadrv_max_queue_depth = MIN(
10228 			    sdinfo->satadrv_queue_depth,
10229 			    SATA_QDEPTH(sata_hba_inst));
10230 			/* Adjust according to global queue depth limit */
10231 			sdinfo->satadrv_max_queue_depth = MIN(
10232 			    sdinfo->satadrv_max_queue_depth,
10233 			    sata_current_max_qdepth);
10234 			if (sdinfo->satadrv_max_queue_depth == 0)
10235 				sdinfo->satadrv_max_queue_depth = 1;
10236 		} else
10237 			sdinfo->satadrv_max_queue_depth = 1;
10238 
10239 		rval = SATA_SUCCESS;
10240 	} else {
10241 		/*
10242 		 * Woops, no Identify Data.
10243 		 */
10244 		if (rval == SATA_TRAN_BUSY || rval == SATA_TRAN_QUEUE_FULL) {
10245 			rval = SATA_RETRY; /* may retry later */
10246 		} else if (rval == SATA_TRAN_ACCEPTED) {
10247 			if (spkt->satapkt_reason == SATA_PKT_DEV_ERROR ||
10248 			    spkt->satapkt_reason == SATA_PKT_ABORTED ||
10249 			    spkt->satapkt_reason == SATA_PKT_TIMEOUT ||
10250 			    spkt->satapkt_reason == SATA_PKT_RESET)
10251 				rval = SATA_RETRY; /* may retry later */
10252 			else
10253 				rval = SATA_FAILURE;
10254 		} else {
10255 			rval = SATA_FAILURE;
10256 		}
10257 	}
10258 fail:
10259 	/* Free allocated resources */
10260 	sata_free_local_buffer(spx);
10261 	sata_pkt_free(spx);
10262 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
10263 
10264 	return (rval);
10265 }
10266 
10267 
10268 /*
10269  * Some devices may not come-up with default DMA mode (UDMA or MWDMA).
10270  * UDMA mode is checked first, followed by MWDMA mode.
10271  * set correctly, so this function is setting it to the highest supported level.
10272  * Older SATA spec required that the device supports at least DMA 4 mode and
10273  * UDMA mode is selected.  It is not mentioned in SerialATA 2.6, so this
10274  * restriction has been removed.
10275  *
10276  * Returns SATA_SUCCESS if proper DMA mode is selected or no DMA is supported.
10277  * Returns SATA_FAILURE if proper DMA mode could not be selected.
10278  *
10279  * NOTE: This function should be called only if DMA mode is supported.
10280  */
10281 static int
10282 sata_set_dma_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo)
10283 {
10284 	sata_pkt_t *spkt;
10285 	sata_cmd_t *scmd;
10286 	sata_pkt_txlate_t *spx;
10287 	int i, mode;
10288 	uint8_t subcmd;
10289 	int rval = SATA_SUCCESS;
10290 
10291 	ASSERT(sdinfo != NULL);
10292 	ASSERT(sata_hba_inst != NULL);
10293 
10294 	if ((sdinfo->satadrv_id.ai_validinfo & SATA_VALIDINFO_88) != 0 &&
10295 	    (sdinfo->satadrv_id.ai_ultradma & SATA_UDMA_SUP_MASK) != 0) {
10296 		/* Find highest Ultra DMA mode supported */
10297 		for (mode = 6; mode >= 0; --mode) {
10298 			if (sdinfo->satadrv_id.ai_ultradma & (1 << mode))
10299 				break;
10300 		}
10301 #if 0
10302 		/* Left for historical reasons */
10303 		/*
10304 		 * Some initial version of SATA spec indicated that at least
10305 		 * UDMA mode 4 has to be supported. It is not mentioned in
10306 		 * SerialATA 2.6, so this restriction is removed.
10307 		 */
10308 		if (mode < 4)
10309 			return (SATA_FAILURE);
10310 #endif
10311 		/* Find UDMA mode currently selected */
10312 		for (i = 6; i >= 0; --i) {
10313 			if (sdinfo->satadrv_id.ai_ultradma & (1 << (i + 8)))
10314 				break;
10315 		}
10316 		if (i >= mode)
10317 			/* Nothing to do */
10318 			return (SATA_SUCCESS);
10319 
10320 		subcmd = SATAC_TRANSFER_MODE_ULTRA_DMA;
10321 
10322 	} else if ((sdinfo->satadrv_id.ai_dworddma & SATA_MDMA_SUP_MASK) != 0) {
10323 		/* Find highest MultiWord DMA mode supported */
10324 		for (mode = 2; mode >= 0; --mode) {
10325 			if (sdinfo->satadrv_id.ai_dworddma & (1 << mode))
10326 				break;
10327 		}
10328 		/* Find highest MultiWord DMA mode selected */
10329 		for (i = 2; i >= 0; --i) {
10330 			if (sdinfo->satadrv_id.ai_dworddma & (1 << (i + 8)))
10331 				break;
10332 		}
10333 		if (i >= mode)
10334 			/* Nothing to do */
10335 			return (SATA_SUCCESS);
10336 
10337 		subcmd = SATAC_TRANSFER_MODE_MULTI_WORD_DMA;
10338 	} else
10339 		return (SATA_SUCCESS);
10340 
10341 	/*
10342 	 * Set DMA mode via SET FEATURES COMMAND.
10343 	 * Prepare packet for SET FEATURES COMMAND.
10344 	 */
10345 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10346 	spx->txlt_sata_hba_inst = sata_hba_inst;
10347 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10348 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10349 	if (spkt == NULL) {
10350 		SATA_LOG_D((sata_hba_inst, CE_WARN,
10351 		    "sata_set_dma_mode: could not set DMA mode %", mode));
10352 		rval = SATA_FAILURE;
10353 		goto done;
10354 	}
10355 	/* Fill sata_pkt */
10356 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10357 	/* Timeout 30s */
10358 	spkt->satapkt_time = sata_default_pkt_time;
10359 	/* Synchronous mode, no callback, interrupts */
10360 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10361 	spkt->satapkt_comp = NULL;
10362 	scmd = &spkt->satapkt_cmd;
10363 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10364 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10365 	scmd->satacmd_addr_type = 0;
10366 	scmd->satacmd_device_reg = 0;
10367 	scmd->satacmd_status_reg = 0;
10368 	scmd->satacmd_error_reg = 0;
10369 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10370 	scmd->satacmd_features_reg = SATAC_SF_TRANSFER_MODE;
10371 	scmd->satacmd_sec_count_lsb = subcmd | mode;
10372 
10373 	/* Transfer command to HBA */
10374 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
10375 	    spkt) != SATA_TRAN_ACCEPTED ||
10376 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
10377 		/* Pkt execution failed */
10378 		rval = SATA_FAILURE;
10379 	}
10380 done:
10381 
10382 	/* Free allocated resources */
10383 	if (spkt != NULL)
10384 		sata_pkt_free(spx);
10385 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10386 
10387 	return (rval);
10388 }
10389 
10390 
10391 /*
10392  * Set device caching mode.
10393  * One of the following operations should be specified:
10394  * SATAC_SF_ENABLE_READ_AHEAD
10395  * SATAC_SF_DISABLE_READ_AHEAD
10396  * SATAC_SF_ENABLE_WRITE_CACHE
10397  * SATAC_SF_DISABLE_WRITE_CACHE
10398  *
10399  * If operation fails, system log messgage is emitted.
10400  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
10401  */
10402 
10403 static int
10404 sata_set_cache_mode(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
10405     int cache_op)
10406 {
10407 	sata_pkt_t *spkt;
10408 	sata_cmd_t *scmd;
10409 	sata_pkt_txlate_t *spx;
10410 	int rval = SATA_SUCCESS;
10411 	char *infop;
10412 
10413 	ASSERT(sdinfo != NULL);
10414 	ASSERT(sata_hba_inst != NULL);
10415 	ASSERT(cache_op == SATAC_SF_ENABLE_READ_AHEAD ||
10416 	    cache_op == SATAC_SF_DISABLE_READ_AHEAD ||
10417 	    cache_op == SATAC_SF_ENABLE_WRITE_CACHE ||
10418 	    cache_op == SATAC_SF_DISABLE_WRITE_CACHE);
10419 
10420 
10421 	/* Prepare packet for SET FEATURES COMMAND */
10422 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10423 	spx->txlt_sata_hba_inst = sata_hba_inst;
10424 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10425 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10426 	if (spkt == NULL) {
10427 		rval = SATA_FAILURE;
10428 		goto failure;
10429 	}
10430 	/* Fill sata_pkt */
10431 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10432 	/* Timeout 30s */
10433 	spkt->satapkt_time = sata_default_pkt_time;
10434 	/* Synchronous mode, no callback, interrupts */
10435 	spkt->satapkt_op_mode =
10436 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10437 	spkt->satapkt_comp = NULL;
10438 	scmd = &spkt->satapkt_cmd;
10439 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10440 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10441 	scmd->satacmd_addr_type = 0;
10442 	scmd->satacmd_device_reg = 0;
10443 	scmd->satacmd_status_reg = 0;
10444 	scmd->satacmd_error_reg = 0;
10445 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10446 	scmd->satacmd_features_reg = cache_op;
10447 
10448 	/* Transfer command to HBA */
10449 	if (((*SATA_START_FUNC(sata_hba_inst))(
10450 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
10451 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
10452 		/* Pkt execution failed */
10453 		switch (cache_op) {
10454 		case SATAC_SF_ENABLE_READ_AHEAD:
10455 			infop = "enabling read ahead failed";
10456 			break;
10457 		case SATAC_SF_DISABLE_READ_AHEAD:
10458 			infop = "disabling read ahead failed";
10459 			break;
10460 		case SATAC_SF_ENABLE_WRITE_CACHE:
10461 			infop = "enabling write cache failed";
10462 			break;
10463 		case SATAC_SF_DISABLE_WRITE_CACHE:
10464 			infop = "disabling write cache failed";
10465 			break;
10466 		}
10467 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
10468 		rval = SATA_FAILURE;
10469 	}
10470 failure:
10471 	/* Free allocated resources */
10472 	if (spkt != NULL)
10473 		sata_pkt_free(spx);
10474 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10475 	return (rval);
10476 }
10477 
10478 /*
10479  * Set Removable Media Status Notification (enable/disable)
10480  * state == 0 , disable
10481  * state != 0 , enable
10482  *
10483  * If operation fails, system log messgage is emitted.
10484  * Returns SATA_SUCCESS when the operation succeeds, SATA_FAILURE otherwise.
10485  */
10486 
10487 static int
10488 sata_set_rmsn(sata_hba_inst_t *sata_hba_inst, sata_drive_info_t *sdinfo,
10489     int state)
10490 {
10491 	sata_pkt_t *spkt;
10492 	sata_cmd_t *scmd;
10493 	sata_pkt_txlate_t *spx;
10494 	int rval = SATA_SUCCESS;
10495 	char *infop;
10496 
10497 	ASSERT(sdinfo != NULL);
10498 	ASSERT(sata_hba_inst != NULL);
10499 
10500 	/* Prepare packet for SET FEATURES COMMAND */
10501 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
10502 	spx->txlt_sata_hba_inst = sata_hba_inst;
10503 	spx->txlt_scsi_pkt = NULL;	/* No scsi pkt involved */
10504 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
10505 	if (spkt == NULL) {
10506 		rval = SATA_FAILURE;
10507 		goto failure;
10508 	}
10509 	/* Fill sata_pkt */
10510 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
10511 	/* Timeout 30s */
10512 	spkt->satapkt_time = sata_default_pkt_time;
10513 	/* Synchronous mode, no callback, interrupts */
10514 	spkt->satapkt_op_mode =
10515 	    SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
10516 	spkt->satapkt_comp = NULL;
10517 	scmd = &spkt->satapkt_cmd;
10518 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
10519 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
10520 	scmd->satacmd_addr_type = 0;
10521 	scmd->satacmd_device_reg = 0;
10522 	scmd->satacmd_status_reg = 0;
10523 	scmd->satacmd_error_reg = 0;
10524 	scmd->satacmd_cmd_reg = SATAC_SET_FEATURES;
10525 	if (state == 0)
10526 		scmd->satacmd_features_reg = SATAC_SF_DISABLE_RMSN;
10527 	else
10528 		scmd->satacmd_features_reg = SATAC_SF_ENABLE_RMSN;
10529 
10530 	/* Transfer command to HBA */
10531 	if (((*SATA_START_FUNC(sata_hba_inst))(
10532 	    SATA_DIP(sata_hba_inst), spkt) != SATA_TRAN_ACCEPTED) ||
10533 	    (spkt->satapkt_reason != SATA_PKT_COMPLETED)) {
10534 		/* Pkt execution failed */
10535 		if (state == 0)
10536 			infop = "disabling Removable Media Status "
10537 			    "Notification failed";
10538 		else
10539 			infop = "enabling Removable Media Status "
10540 			    "Notification failed";
10541 
10542 		SATA_LOG_D((sata_hba_inst, CE_WARN, "%s", infop));
10543 		rval = SATA_FAILURE;
10544 	}
10545 failure:
10546 	/* Free allocated resources */
10547 	if (spkt != NULL)
10548 		sata_pkt_free(spx);
10549 	(void) kmem_free(spx, sizeof (sata_pkt_txlate_t));
10550 	return (rval);
10551 }
10552 
10553 
10554 /*
10555  * Update port SCR block
10556  */
10557 static void
10558 sata_update_port_scr(sata_port_scr_t *port_scr, sata_device_t *device)
10559 {
10560 	port_scr->sstatus = device->satadev_scr.sstatus;
10561 	port_scr->serror = device->satadev_scr.serror;
10562 	port_scr->scontrol = device->satadev_scr.scontrol;
10563 	port_scr->sactive = device->satadev_scr.sactive;
10564 	port_scr->snotific = device->satadev_scr.snotific;
10565 }
10566 
10567 /*
10568  * Update state and copy port ss* values from passed sata_device structure.
10569  * sata_address is validated - if not valid, nothing is changed in sata_scsi
10570  * configuration struct.
10571  *
10572  * SATA_PSTATE_SHUTDOWN in port state is not reset to 0 by this function
10573  * regardless of the state in device argument.
10574  *
10575  * Port mutex should be held while calling this function.
10576  */
10577 static void
10578 sata_update_port_info(sata_hba_inst_t *sata_hba_inst,
10579 	sata_device_t *sata_device)
10580 {
10581 	ASSERT(mutex_owned(&SATA_CPORT_MUTEX(sata_hba_inst,
10582 	    sata_device->satadev_addr.cport)));
10583 
10584 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT ||
10585 	    sata_device->satadev_addr.qual == SATA_ADDR_DCPORT) {
10586 
10587 		sata_cport_info_t *cportinfo;
10588 
10589 		if (SATA_NUM_CPORTS(sata_hba_inst) <=
10590 		    sata_device->satadev_addr.cport)
10591 			return;
10592 
10593 		cportinfo = SATA_CPORT_INFO(sata_hba_inst,
10594 		    sata_device->satadev_addr.cport);
10595 		sata_update_port_scr(&cportinfo->cport_scr, sata_device);
10596 
10597 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
10598 		cportinfo->cport_state &= ~(SATA_PSTATE_PWRON |
10599 		    SATA_PSTATE_PWROFF | SATA_PSTATE_FAILED);
10600 		cportinfo->cport_state |=
10601 		    sata_device->satadev_state & SATA_PSTATE_VALID;
10602 	} else {
10603 		sata_pmport_info_t *pmportinfo;
10604 
10605 		if ((sata_device->satadev_addr.qual != SATA_ADDR_PMPORT) ||
10606 		    (sata_device->satadev_addr.qual != SATA_ADDR_DPMPORT) ||
10607 		    SATA_NUM_PMPORTS(sata_hba_inst,
10608 		    sata_device->satadev_addr.cport) <
10609 		    sata_device->satadev_addr.pmport)
10610 			return;
10611 
10612 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst,
10613 		    sata_device->satadev_addr.cport,
10614 		    sata_device->satadev_addr.pmport);
10615 		sata_update_port_scr(&pmportinfo->pmport_scr, sata_device);
10616 
10617 		/* Preserve SATA_PSTATE_SHUTDOWN flag */
10618 		pmportinfo->pmport_state &=
10619 		    ~(SATA_PSTATE_PWRON | SATA_PSTATE_PWROFF |
10620 		    SATA_PSTATE_FAILED);
10621 		pmportinfo->pmport_state |=
10622 		    sata_device->satadev_state & SATA_PSTATE_VALID;
10623 	}
10624 }
10625 
10626 
10627 
10628 /*
10629  * Extract SATA port specification from an IOCTL argument.
10630  *
10631  * This function return the port the user land send us as is, unless it
10632  * cannot retrieve port spec, then -1 is returned.
10633  *
10634  * Note: Only cport  - no port multiplier port.
10635  */
10636 static int32_t
10637 sata_get_port_num(sata_hba_inst_t *sata_hba_inst, struct devctl_iocdata *dcp)
10638 {
10639 	int32_t port;
10640 
10641 	/* Extract port number from nvpair in dca structure  */
10642 	if (nvlist_lookup_int32(ndi_dc_get_ap_data(dcp), "port", &port) != 0) {
10643 		SATA_LOG_D((sata_hba_inst, CE_NOTE,
10644 		    "sata_get_port_num: invalid port spec 0x%x in ioctl",
10645 		    port));
10646 		port = -1;
10647 	}
10648 
10649 	return (port);
10650 }
10651 
10652 /*
10653  * Get dev_info_t pointer to the device node pointed to by port argument.
10654  * NOTE: target argument is a value used in ioctls to identify
10655  * the AP - it is not a sata_address.
10656  * It is a combination of cport, pmport and address qualifier, encodded same
10657  * way as a scsi target number.
10658  * At this moment it carries only cport number.
10659  *
10660  * No PMult hotplug support.
10661  *
10662  * Returns dev_info_t pointer if target device was found, NULL otherwise.
10663  */
10664 
10665 static dev_info_t *
10666 sata_get_target_dip(dev_info_t *dip, int32_t port)
10667 {
10668 	dev_info_t	*cdip = NULL;
10669 	int		target, tgt;
10670 	int		ncport;
10671 	int 		circ;
10672 
10673 	ncport = port & SATA_CFGA_CPORT_MASK;
10674 	target = SATA_TO_SCSI_TARGET(ncport, 0, SATA_ADDR_DCPORT);
10675 
10676 	ndi_devi_enter(dip, &circ);
10677 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
10678 		dev_info_t *next = ddi_get_next_sibling(cdip);
10679 
10680 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
10681 		    DDI_PROP_DONTPASS, "target", -1);
10682 		if (tgt == -1) {
10683 			/*
10684 			 * This is actually an error condition, but not
10685 			 * a fatal one. Just continue the search.
10686 			 */
10687 			cdip = next;
10688 			continue;
10689 		}
10690 
10691 		if (tgt == target)
10692 			break;
10693 
10694 		cdip = next;
10695 	}
10696 	ndi_devi_exit(dip, circ);
10697 
10698 	return (cdip);
10699 }
10700 
10701 /*
10702  * Get dev_info_t pointer to the device node pointed to by port argument.
10703  * NOTE: target argument is a value used in ioctls to identify
10704  * the AP - it is not a sata_address.
10705  * It is a combination of cport, pmport and address qualifier, encoded same
10706  * way as a scsi target number.
10707  * At this moment it carries only cport number.
10708  *
10709  * No PMult hotplug support.
10710  *
10711  * Returns dev_info_t pointer if target device was found, NULL otherwise.
10712  */
10713 
10714 static dev_info_t *
10715 sata_get_scsi_target_dip(dev_info_t *dip, sata_address_t *saddr)
10716 {
10717 	dev_info_t	*cdip = NULL;
10718 	int		target, tgt;
10719 	int 		circ;
10720 
10721 	target = SATA_TO_SCSI_TARGET(saddr->cport, saddr->pmport, saddr->qual);
10722 
10723 	ndi_devi_enter(dip, &circ);
10724 	for (cdip = ddi_get_child(dip); cdip != NULL; ) {
10725 		dev_info_t *next = ddi_get_next_sibling(cdip);
10726 
10727 		tgt = ddi_prop_get_int(DDI_DEV_T_ANY, cdip,
10728 		    DDI_PROP_DONTPASS, "target", -1);
10729 		if (tgt == -1) {
10730 			/*
10731 			 * This is actually an error condition, but not
10732 			 * a fatal one. Just continue the search.
10733 			 */
10734 			cdip = next;
10735 			continue;
10736 		}
10737 
10738 		if (tgt == target)
10739 			break;
10740 
10741 		cdip = next;
10742 	}
10743 	ndi_devi_exit(dip, circ);
10744 
10745 	return (cdip);
10746 }
10747 
10748 /*
10749  * Process sata port disconnect request.
10750  * Normally, cfgadm sata plugin will try to offline (unconfigure) the device
10751  * before this request. Nevertheless, if a device is still configured,
10752  * we need to attempt to offline and unconfigure device.
10753  * Regardless of the unconfigure operation results the port is marked as
10754  * deactivated and no access to the attached device is possible.
10755  * If the target node remains because unconfigure operation failed, its state
10756  * will be set to DEVICE_REMOVED, preventing it to be used again when a device
10757  * is inserted/re-inserted. The event daemon will repeatedly try to unconfigure
10758  * the device and remove old target node.
10759  *
10760  * This function invokes sata_hba_inst->satahba_tran->
10761  * sata_tran_hotplug_ops->sata_tran_port_deactivate().
10762  * If successful, the device structure (if any) attached to the specified port
10763  * is removed and state of the port marked appropriately.
10764  * Failure of the port_deactivate may keep port in the physically active state,
10765  * or may fail the port.
10766  *
10767  * NOTE: Port multiplier code is not completed nor tested.
10768  */
10769 
10770 static int
10771 sata_ioctl_disconnect(sata_hba_inst_t *sata_hba_inst,
10772     sata_device_t *sata_device)
10773 {
10774 	sata_drive_info_t *sdinfo = NULL;
10775 	sata_cport_info_t *cportinfo = NULL;
10776 	sata_pmport_info_t *pmportinfo = NULL;
10777 	sata_pmult_info_t *pmultinfo = NULL;
10778 	dev_info_t *tdip;
10779 	int cport, pmport, qual;
10780 	int rval = SATA_SUCCESS;
10781 	int rv = 0;
10782 
10783 	cport = sata_device->satadev_addr.cport;
10784 	pmport = sata_device->satadev_addr.pmport;
10785 	qual = sata_device->satadev_addr.qual;
10786 
10787 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
10788 
10789 	/*
10790 	 * DEVCTL_AP_DISCONNECT invokes sata_hba_inst->satahba_tran->
10791 	 * sata_tran_hotplug_ops->sata_tran_port_deactivate().
10792 	 * Do the sanity check.
10793 	 */
10794 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL) {
10795 		/* No physical port deactivation supported. */
10796 		return (EINVAL);
10797 	}
10798 
10799 	/* Check the current state of the port */
10800 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
10801 	    (SATA_DIP(sata_hba_inst), sata_device);
10802 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10803 	sata_update_port_info(sata_hba_inst, sata_device);
10804 	if (rval != SATA_SUCCESS ||
10805 	    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
10806 		/* Device port status is unknown or it is in failed state */
10807 		if (qual == SATA_ADDR_PMPORT) {
10808 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
10809 			    SATA_PSTATE_FAILED;
10810 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
10811 			    "sata_hba_ioctl: connect: failed to deactivate "
10812 			    "SATA port %d", cport);
10813 		} else {
10814 			SATA_CPORT_STATE(sata_hba_inst, cport) =
10815 			    SATA_PSTATE_FAILED;
10816 			SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
10817 			    "sata_hba_ioctl: connect: failed to deactivate "
10818 			    "SATA port %d:%d", cport, pmport);
10819 		}
10820 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
10821 		    cport)->cport_mutex);
10822 		return (EIO);
10823 	}
10824 	/*
10825 	 * Set port's dev_state to not ready - this will disable
10826 	 * an access to a potentially attached device.
10827 	 */
10828 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
10829 	if (qual == SATA_ADDR_PMPORT) {
10830 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
10831 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
10832 			sdinfo = pmportinfo->pmport_sata_drive;
10833 			ASSERT(sdinfo != NULL);
10834 		}
10835 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
10836 	} else {
10837 		/* Assuming cport */
10838 
10839 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
10840 			if (cportinfo->cport_dev_type == SATA_DTYPE_PMULT) {
10841 				pmultinfo =
10842 				    cportinfo->cport_devp.cport_sata_pmult;
10843 				ASSERT(pmultinfo != NULL);
10844 			} else if (cportinfo->cport_dev_type &
10845 			    SATA_VALID_DEV_TYPE) {
10846 				sdinfo = cportinfo->cport_devp.cport_sata_drive;
10847 				ASSERT(sdinfo != NULL);
10848 			}
10849 		}
10850 		cportinfo->cport_state &= ~SATA_STATE_READY;
10851 	}
10852 	if (sdinfo != NULL) {
10853 		if ((sdinfo->satadrv_type & (SATA_VALID_DEV_TYPE)) != 0) {
10854 			/*
10855 			 * If a target node exists, try to offline
10856 			 * a device and remove target node.
10857 			 */
10858 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
10859 			    cport)->cport_mutex);
10860 			/* We are addressing attached device, not a port */
10861 			sata_device->satadev_addr.qual =
10862 			    sdinfo->satadrv_addr.qual;
10863 			tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
10864 			    &sata_device->satadev_addr);
10865 			if (tdip != NULL && ndi_devi_offline(tdip,
10866 			    NDI_DEVI_REMOVE) != NDI_SUCCESS) {
10867 				/*
10868 				 * Problem
10869 				 * The target node remained attached.
10870 				 * This happens when the device file was open
10871 				 * or a node was waiting for resources.
10872 				 * Cannot do anything about it.
10873 				 */
10874 				if (qual == SATA_ADDR_CPORT) {
10875 					SATA_LOG_D((sata_hba_inst, CE_WARN,
10876 					    "sata_hba_ioctl: disconnect: could "
10877 					    "not unconfigure device before "
10878 					    "disconnecting the SATA port %d",
10879 					    cport));
10880 				} else {
10881 					SATA_LOG_D((sata_hba_inst, CE_WARN,
10882 					    "sata_hba_ioctl: disconnect: could "
10883 					    "not unconfigure device before "
10884 					    "disconnecting the SATA port %d:%d",
10885 					    cport, pmport));
10886 				}
10887 				/*
10888 				 * Set DEVICE REMOVED state in the target
10889 				 * node. It will prevent access to the device
10890 				 * even when a new device is attached, until
10891 				 * the old target node is released, removed and
10892 				 * recreated for a new  device.
10893 				 */
10894 				sata_set_device_removed(tdip);
10895 
10896 				/*
10897 				 * Instruct event daemon to try the target
10898 				 * node cleanup later.
10899 				 */
10900 				sata_set_target_node_cleanup(
10901 				    sata_hba_inst, &sata_device->satadev_addr);
10902 			}
10903 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
10904 			    cport)->cport_mutex);
10905 		}
10906 
10907 		/* Remove and release sata_drive info structure. */
10908 		if (pmportinfo != NULL) {
10909 			SATA_PMPORT_DRV_INFO(sata_hba_inst, cport, pmport) =
10910 			    NULL;
10911 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
10912 		} else {
10913 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
10914 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
10915 		}
10916 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
10917 	}
10918 #if 0
10919 	else if (pmultinfo != NULL) {
10920 		/*
10921 		 * Port Multiplier itself needs special handling.
10922 		 * All device ports need to be processed here!
10923 		 */
10924 	}
10925 #endif
10926 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10927 	/* Just ask HBA driver to deactivate port */
10928 	/*	sata_device->satadev_addr.qual = SATA_ADDR_DCPORT; */
10929 
10930 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
10931 	    (SATA_DIP(sata_hba_inst), sata_device);
10932 
10933 	/*
10934 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
10935 	 * without the hint (to force listener to investivate the state).
10936 	 */
10937 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
10938 	    SE_NO_HINT);
10939 
10940 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10941 	sata_update_port_info(sata_hba_inst, sata_device);
10942 
10943 	if (rval != SATA_SUCCESS) {
10944 		/*
10945 		 * Port deactivation failure - do not
10946 		 * change port state unless the state
10947 		 * returned by HBA indicates a port failure.
10948 		 * NOTE: device structures were released, so devices now are
10949 		 * invisible! Port reset is needed to re-enumerate devices.
10950 		 */
10951 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
10952 			if (pmportinfo != NULL)
10953 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
10954 			else
10955 				cportinfo->cport_state = SATA_PSTATE_FAILED;
10956 			rv = EIO;
10957 		}
10958 	} else {
10959 		/*
10960 		 * Deactivation succeded. From now on the sata framework
10961 		 * will not care what is happening to the device, until
10962 		 * the port is activated again.
10963 		 */
10964 		cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
10965 	}
10966 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
10967 	return (rv);
10968 }
10969 
10970 
10971 
10972 /*
10973  * Process sata port connect request
10974  * The sata cfgadm pluging will invoke this operation only if port was found
10975  * in the disconnect state (failed state is also treated as the disconnected
10976  * state).
10977  * DEVCTL_AP_CONNECT would invoke  sata_hba_inst->satahba_tran->
10978  * sata_tran_hotplug_ops->sata_tran_port_activate().
10979  * If successful and a device is found attached to the port,
10980  * the initialization sequence is executed to attach a device structure to
10981  * a port structure. The state of the port and a device would be set
10982  * appropriately.
10983  * The device is not set in configured state (system-wise) by this operation.
10984  *
10985  * Note, that activating the port may generate link events,
10986  * so it is important that following processing and the
10987  * event processing does not interfere with each other!
10988  *
10989  * This operation may remove port failed state and will
10990  * try to make port active and in good standing.
10991  *
10992  * NOTE: Port multiplier code is not completed nor tested.
10993  */
10994 
10995 static int
10996 sata_ioctl_connect(sata_hba_inst_t *sata_hba_inst,
10997     sata_device_t *sata_device)
10998 {
10999 	int cport, pmport, qual;
11000 	int rv = 0;
11001 
11002 	cport = sata_device->satadev_addr.cport;
11003 	pmport = sata_device->satadev_addr.pmport;
11004 	qual = sata_device->satadev_addr.qual;
11005 
11006 	ASSERT(qual == SATA_ADDR_CPORT || qual == SATA_ADDR_PMPORT);
11007 
11008 	/*
11009 	 * DEVCTL_AP_CONNECT would invoke sata_hba_inst->
11010 	 * satahba_tran->sata_tran_hotplug_ops->sata_tran_port_activate().
11011 	 * Perform sanity check now.
11012 	 */
11013 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL) {
11014 		/* No physical port activation supported. */
11015 		return (EINVAL);
11016 	}
11017 
11018 	/* Just ask HBA driver to activate port */
11019 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11020 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11021 		/*
11022 		 * Port activation failure.
11023 		 */
11024 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11025 		    cport)->cport_mutex);
11026 		sata_update_port_info(sata_hba_inst, sata_device);
11027 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11028 			if (qual == SATA_ADDR_DCPORT) {
11029 				SATA_CPORT_STATE(sata_hba_inst, cport) =
11030 				    SATA_PSTATE_FAILED;
11031 				SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
11032 				    "sata_hba_ioctl: connect: failed to "
11033 				    "activate SATA port %d", cport);
11034 			} else { /* port multiplier device port */
11035 				SATA_PMPORT_STATE(sata_hba_inst, cport,
11036 				    pmport) = SATA_PSTATE_FAILED;
11037 				SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
11038 				    "sata_hba_ioctl: connect: failed to "
11039 				    "activate SATA port %d:%d", cport, pmport);
11040 
11041 			}
11042 		}
11043 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11044 		    cport)->cport_mutex);
11045 		SATADBG2(SATA_DBG_IOCTL_IF, sata_hba_inst,
11046 		    "sata_hba_ioctl: connect: failed to activate SATA "
11047 		    "port %d:%d", cport, pmport);
11048 		return (EIO);
11049 	}
11050 
11051 	/* Virgin port state - will be updated by the port re-probe. */
11052 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11053 	if (qual == SATA_ADDR_CPORT)
11054 		SATA_CPORT_STATE(sata_hba_inst, cport) = 0;
11055 	else /* port multiplier device port */
11056 		SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) = 0;
11057 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11058 
11059 	/*
11060 	 * Probe the port to find its state and attached device.
11061 	 */
11062 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11063 	    SATA_DEV_IDENTIFY_RETRY) == SATA_FAILURE)
11064 		rv = EIO;
11065 
11066 	/*
11067 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11068 	 * without the hint
11069 	 */
11070 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11071 	    SE_NO_HINT);
11072 
11073 	/*
11074 	 * If there is a device attached to the port, emit
11075 	 * a message.
11076 	 */
11077 	if (qual == SATA_ADDR_CPORT) {
11078 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport) !=
11079 		    SATA_DTYPE_NONE)
11080 			sata_log(sata_hba_inst, CE_WARN,
11081 			    "SATA device detected at port %d", cport);
11082 	} else { /* port multiplier device port */
11083 		if (SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) !=
11084 		    SATA_DTYPE_NONE)
11085 			sata_log(sata_hba_inst, CE_WARN,
11086 			    "SATA device detected at port %d:%d",
11087 			    cport, pmport);
11088 	}
11089 
11090 	return (rv);
11091 }
11092 
11093 
11094 /*
11095  * Process sata device unconfigure request.
11096  * The unconfigure operation uses generic nexus operation to
11097  * offline a device. It leaves a target device node attached.
11098  * and obviously sata_drive_info attached as well, because
11099  * from the hardware point of view nothing has changed.
11100  */
11101 static int
11102 sata_ioctl_unconfigure(sata_hba_inst_t *sata_hba_inst,
11103     sata_device_t *sata_device)
11104 {
11105 	int rv = 0;
11106 	dev_info_t *tdip;
11107 
11108 	/* We are addressing attached device, not a port */
11109 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
11110 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11111 	else if (sata_device->satadev_addr.qual == SATA_ADDR_PMPORT)
11112 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11113 
11114 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11115 	    &sata_device->satadev_addr)) != NULL) {
11116 
11117 		if (ndi_devi_offline(tdip, NDI_UNCONFIG) != NDI_SUCCESS) {
11118 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11119 			    "sata_hba_ioctl: unconfigure: "
11120 			    "failed to unconfigure device at SATA port %d:%d",
11121 			    sata_device->satadev_addr.cport,
11122 			    sata_device->satadev_addr.pmport));
11123 			rv = EIO;
11124 		}
11125 		/*
11126 		 * The target node devi_state should be marked with
11127 		 * DEVI_DEVICE_OFFLINE by ndi_devi_offline().
11128 		 * This would be the indication for cfgadm that
11129 		 * the AP node occupant state is 'unconfigured'.
11130 		 */
11131 
11132 	} else {
11133 		/*
11134 		 * This would indicate a failure on the part of cfgadm
11135 		 * to detect correct state of the node prior to this
11136 		 * call - one cannot unconfigure non-existing device.
11137 		 */
11138 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11139 		    "sata_hba_ioctl: unconfigure: "
11140 		    "attempt to unconfigure non-existing device "
11141 		    "at SATA port %d:%d",
11142 		    sata_device->satadev_addr.cport,
11143 		    sata_device->satadev_addr.pmport));
11144 		rv = ENXIO;
11145 	}
11146 	return (rv);
11147 }
11148 
11149 /*
11150  * Process sata device configure request
11151  * If port is in a failed state, operation is aborted - one has to use
11152  * an explicit connect or port activate request to try to get a port into
11153  * non-failed mode. Port reset wil also work in such situation.
11154  * If the port is in disconnected (shutdown) state, the connect operation is
11155  * attempted prior to any other action.
11156  * When port is in the active state, there is a device attached and the target
11157  * node exists, a device was most likely offlined.
11158  * If target node does not exist, a new target node is created. In both cases
11159  * an attempt is made to online (configure) the device.
11160  *
11161  * NOTE: Port multiplier code is not completed nor tested.
11162  */
11163 static int
11164 sata_ioctl_configure(sata_hba_inst_t *sata_hba_inst,
11165     sata_device_t *sata_device)
11166 {
11167 	int cport, pmport, qual;
11168 	int rval;
11169 	boolean_t target = TRUE;
11170 	sata_cport_info_t *cportinfo;
11171 	sata_pmport_info_t *pmportinfo = NULL;
11172 	dev_info_t *tdip;
11173 	sata_drive_info_t *sdinfo;
11174 
11175 	cport = sata_device->satadev_addr.cport;
11176 	pmport = sata_device->satadev_addr.pmport;
11177 	qual = sata_device->satadev_addr.qual;
11178 
11179 	/* Get current port state */
11180 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
11181 	    (SATA_DIP(sata_hba_inst), sata_device);
11182 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11183 	sata_update_port_info(sata_hba_inst, sata_device);
11184 
11185 	if (rval != SATA_SUCCESS ||
11186 	    (sata_device->satadev_state & SATA_PSTATE_FAILED) != 0) {
11187 		/* Obviously, device on a failed port is not visible */
11188 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11189 		return (ENXIO);
11190 	}
11191 
11192 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11193 	if (qual == SATA_ADDR_PMPORT)
11194 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11195 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11196 
11197 	if ((sata_device->satadev_state & SATA_PSTATE_SHUTDOWN) != 0) {
11198 		/* need to activate port */
11199 		target = FALSE;
11200 
11201 		/* Sanity check */
11202 		if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
11203 			return (ENXIO);
11204 
11205 		/* Just let HBA driver to activate port */
11206 		if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11207 		    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11208 			/*
11209 			 * Port activation failure - do not change port state
11210 			 * unless the state returned by HBA indicates a port
11211 			 * failure.
11212 			 */
11213 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11214 			    cport)->cport_mutex);
11215 			sata_update_port_info(sata_hba_inst, sata_device);
11216 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11217 				if (qual == SATA_ADDR_PMPORT)
11218 					pmportinfo->pmport_state =
11219 					    SATA_PSTATE_FAILED;
11220 				else
11221 					cportinfo->cport_state =
11222 					    SATA_PSTATE_FAILED;
11223 			}
11224 			mutex_exit(&SATA_CPORT_INFO(
11225 			    sata_hba_inst, cport)->cport_mutex);
11226 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11227 			    "sata_hba_ioctl: configure: "
11228 			    "failed to activate SATA port %d:%d",
11229 			    cport, pmport));
11230 			return (EIO);
11231 		}
11232 		/*
11233 		 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11234 		 * without the hint.
11235 		 */
11236 		sata_gen_sysevent(sata_hba_inst,
11237 		    &sata_device->satadev_addr, SE_NO_HINT);
11238 
11239 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11240 		    cport_mutex);
11241 		/* Virgin port state */
11242 		if (qual == SATA_ADDR_PMPORT)
11243 			pmportinfo->pmport_state = 0;
11244 		else
11245 			cportinfo->cport_state = 0;
11246 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11247 	}
11248 	/*
11249 	 * Always reprobe port, to get current device info.
11250 	 */
11251 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11252 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
11253 		return (EIO);
11254 
11255 	if (qual == SATA_ADDR_PMPORT) {
11256 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
11257 		    target == FALSE) {
11258 			/*
11259 			 * That's the transition from "inactive" port
11260 			 * to active one with device attached.
11261 			 */
11262 			sata_log(sata_hba_inst, CE_WARN,
11263 			    "SATA device detected at port %d:%d",
11264 			    cport, pmport);
11265 		}
11266 	} else {
11267 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE &&
11268 		    target == FALSE) {
11269 			/*
11270 			 * When PM is attached to the cport and cport is
11271 			 * activated, every PM device port needs to be reprobed.
11272 			 * We need to emit message for all devices detected
11273 			 * at port multiplier's device ports.
11274 			 * Add such code here.
11275 			 * For now, just inform about device attached to
11276 			 * cport.
11277 			 */
11278 			sata_log(sata_hba_inst, CE_WARN,
11279 			    "SATA device detected at port %d", cport);
11280 		}
11281 	}
11282 
11283 	/*
11284 	 * This is where real configuration operation starts.
11285 	 *
11286 	 * When PM is attached to the cport and cport is activated,
11287 	 * devices attached PM device ports may have to be configured
11288 	 * explicitly. This may change when port multiplier is supported.
11289 	 * For now, configure only disks and other valid target devices.
11290 	 */
11291 	if (!(sata_device->satadev_type & SATA_VALID_DEV_TYPE)) {
11292 		return (ENXIO);		/* No device to configure */
11293 	}
11294 
11295 	/*
11296 	 * Here we may have a device in reset condition,
11297 	 * but because we are just configuring it, there is
11298 	 * no need to process the reset other than just
11299 	 * to clear device reset condition in the HBA driver.
11300 	 * Setting the flag SATA_EVNT_CLEAR_DEVICE_RESET will
11301 	 * cause a first command sent the HBA driver with the request
11302 	 * to clear device reset condition.
11303 	 */
11304 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11305 	if (qual == SATA_ADDR_PMPORT)
11306 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11307 	else
11308 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11309 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
11310 	if (sdinfo == NULL) {
11311 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11312 		return (ENXIO);
11313 	}
11314 	if (sdinfo->satadrv_event_flags &
11315 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
11316 		sdinfo->satadrv_event_flags = 0;
11317 		sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
11318 	}
11319 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11320 
11321 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11322 	    &sata_device->satadev_addr)) != NULL) {
11323 		/*
11324 		 * Target node exists. Verify, that it belongs
11325 		 * to existing, attached device and not to
11326 		 * a removed device.
11327 		 */
11328 		if (sata_check_device_removed(tdip) == B_TRUE) {
11329 			if (qual == SATA_ADDR_DPMPORT)
11330 				sata_log(sata_hba_inst, CE_WARN,
11331 				    "SATA device at port %d cannot be "
11332 				    "configured. "
11333 				    "Application(s) accessing "
11334 				    "previously attached device "
11335 				    "have to release it before newly "
11336 				    "inserted device can be made accessible.",
11337 				    cport);
11338 			else
11339 				sata_log(sata_hba_inst, CE_WARN,
11340 				    "SATA device at port %d:%d cannot be"
11341 				    "configured. "
11342 				    "Application(s) accessing "
11343 				    "previously attached device "
11344 				    "have to release it before newly "
11345 				    "inserted device can be made accessible.",
11346 				    cport, pmport);
11347 			return (EIO);
11348 		}
11349 		/*
11350 		 * Device was not removed and re-inserted.
11351 		 * Try to online it.
11352 		 */
11353 		if (ndi_devi_online(tdip, 0) != NDI_SUCCESS) {
11354 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11355 			    "sata_hba_ioctl: configure: "
11356 			    "onlining device at SATA port "
11357 			    "%d:%d failed", cport, pmport));
11358 			return (EIO);
11359 		}
11360 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11361 		    cport)->cport_mutex);
11362 
11363 		if (qual == SATA_ADDR_DPMPORT)
11364 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
11365 		else
11366 			cportinfo-> cport_tgtnode_clean = B_TRUE;
11367 
11368 		mutex_exit(&SATA_CPORT_INFO(
11369 		    sata_hba_inst, cport)->cport_mutex);
11370 	} else {
11371 		/*
11372 		 * No target node - need to create a new target node.
11373 		 */
11374 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11375 		    cport_mutex);
11376 		if (qual == SATA_ADDR_DPMPORT)
11377 			pmportinfo->pmport_tgtnode_clean = B_TRUE;
11378 		else
11379 			cportinfo-> cport_tgtnode_clean = B_TRUE;
11380 
11381 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11382 		    cport_mutex);
11383 		tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
11384 		    sata_hba_inst, &sata_device->satadev_addr);
11385 		if (tdip == NULL) {
11386 			/* Configure operation failed */
11387 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11388 			    "sata_hba_ioctl: configure: "
11389 			    "configuring SATA device at port %d:%d "
11390 			    "failed", cport, pmport));
11391 			return (EIO);
11392 		}
11393 	}
11394 	return (0);
11395 }
11396 
11397 
11398 /*
11399  * Process ioctl deactivate port request.
11400  * Arbitrarily unconfigure attached device, if any.
11401  * Even if the unconfigure fails, proceed with the
11402  * port deactivation.
11403  *
11404  * NOTE: Port Multiplier code is not completed and tested.
11405  */
11406 
11407 static int
11408 sata_ioctl_deactivate(sata_hba_inst_t *sata_hba_inst,
11409     sata_device_t *sata_device)
11410 {
11411 	int cport, pmport, qual;
11412 	int rval, rv = 0;
11413 	sata_cport_info_t *cportinfo;
11414 	sata_pmport_info_t *pmportinfo = NULL;
11415 	dev_info_t *tdip;
11416 	sata_drive_info_t *sdinfo = NULL;
11417 
11418 	/* Sanity check */
11419 	if (SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst) == NULL)
11420 		return (ENOTSUP);
11421 
11422 	cport = sata_device->satadev_addr.cport;
11423 	pmport = sata_device->satadev_addr.pmport;
11424 	qual = sata_device->satadev_addr.qual;
11425 
11426 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11427 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11428 	if (qual == SATA_ADDR_CPORT) {
11429 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11430 		if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
11431 			/*
11432 			 * For now, assume that port multiplier is not
11433 			 * supported, i.e. deal only with valid devices
11434 			 */
11435 			if ((cportinfo->cport_dev_type &
11436 			    SATA_VALID_DEV_TYPE) != 0)
11437 				sdinfo = sata_get_device_info(sata_hba_inst,
11438 				    sata_device);
11439 			/*
11440 			 * If attached device is a port multiplier, we will
11441 			 * have to unconfigure all devices attached to the
11442 			 * port multiplier. Add this code here.
11443 			 */
11444 		}
11445 		cportinfo->cport_state &= ~SATA_STATE_READY;
11446 	} else {
11447 		/* Port multiplier device port */
11448 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11449 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11450 		if (pmportinfo->pmport_dev_type != SATA_DTYPE_NONE &&
11451 		    (pmportinfo->pmport_dev_type & SATA_VALID_DEV_TYPE) != 0)
11452 			sdinfo = sata_get_device_info(sata_hba_inst,
11453 			    sata_device);
11454 		pmportinfo->pmport_state &= ~SATA_STATE_READY;
11455 	}
11456 
11457 	if (sdinfo != NULL) {
11458 		/*
11459 		 * If a target node exists, try to offline a device and
11460 		 * to remove a target node.
11461 		 */
11462 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11463 		    cport_mutex);
11464 		tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
11465 		    &sata_device->satadev_addr);
11466 		if (tdip != NULL) {
11467 			/* target node exist */
11468 			SATADBG1(SATA_DBG_IOCTL_IF, sata_hba_inst,
11469 			    "sata_hba_ioctl: port deactivate: "
11470 			    "target node exists.", NULL);
11471 
11472 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) !=
11473 			    NDI_SUCCESS) {
11474 				SATA_LOG_D((sata_hba_inst, CE_WARN,
11475 				    "sata_hba_ioctl: port deactivate: "
11476 				    "failed to unconfigure device at port "
11477 				    "%d:%d before deactivating the port",
11478 				    cport, pmport));
11479 				/*
11480 				 * Set DEVICE REMOVED state in the target
11481 				 * node. It will prevent an access to
11482 				 * the device even when a new device is
11483 				 * attached, until the old target node is
11484 				 * released, removed and recreated for a new
11485 				 * device.
11486 				 */
11487 				sata_set_device_removed(tdip);
11488 
11489 				/*
11490 				 * Instruct the event daemon to try the
11491 				 * target node cleanup later.
11492 				 */
11493 				sata_set_target_node_cleanup(sata_hba_inst,
11494 				    &sata_device->satadev_addr);
11495 			}
11496 		}
11497 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11498 		    cport_mutex);
11499 		/*
11500 		 * In any case, remove and release sata_drive_info
11501 		 * structure.
11502 		 */
11503 		if (qual == SATA_ADDR_CPORT) {
11504 			SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
11505 			cportinfo->cport_dev_type = SATA_DTYPE_NONE;
11506 		} else { /* port multiplier device port */
11507 			SATA_PMPORTINFO_DRV_INFO(pmportinfo) = NULL;
11508 			pmportinfo->pmport_dev_type = SATA_DTYPE_NONE;
11509 		}
11510 		(void) kmem_free((void *)sdinfo, sizeof (sata_drive_info_t));
11511 	}
11512 	if (qual == SATA_ADDR_CPORT) {
11513 		cportinfo->cport_state &= ~(SATA_STATE_PROBED |
11514 		    SATA_STATE_PROBING);
11515 	} else { /* port multiplier device port */
11516 		pmportinfo->pmport_state &= ~(SATA_STATE_PROBED |
11517 		    SATA_STATE_PROBING);
11518 	}
11519 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11520 
11521 	/* Just let HBA driver to deactivate port */
11522 	sata_device->satadev_addr.qual = qual;
11523 	rval = (*SATA_PORT_DEACTIVATE_FUNC(sata_hba_inst))
11524 	    (SATA_DIP(sata_hba_inst), sata_device);
11525 
11526 	/*
11527 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11528 	 * without the hint
11529 	 */
11530 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11531 	    SE_NO_HINT);
11532 
11533 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11534 	sata_update_port_info(sata_hba_inst, sata_device);
11535 	if (qual == SATA_ADDR_CPORT) {
11536 		if (rval != SATA_SUCCESS) {
11537 			/*
11538 			 * Port deactivation failure - do not change port state
11539 			 * unless the state returned by HBA indicates a port
11540 			 * failure.
11541 			 */
11542 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11543 				SATA_CPORT_STATE(sata_hba_inst, cport) =
11544 				    SATA_PSTATE_FAILED;
11545 			}
11546 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11547 			    "sata_hba_ioctl: port deactivate: "
11548 			    "cannot deactivate SATA port %d", cport));
11549 			rv = EIO;
11550 		} else {
11551 			cportinfo->cport_state |= SATA_PSTATE_SHUTDOWN;
11552 		}
11553 	} else {
11554 		if (rval != SATA_SUCCESS) {
11555 			if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11556 				SATA_PMPORT_STATE(sata_hba_inst, cport,
11557 				    pmport) = SATA_PSTATE_FAILED;
11558 			}
11559 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11560 			    "sata_hba_ioctl: port deactivate: "
11561 			    "cannot deactivate SATA port %d:%d",
11562 			    cport, pmport));
11563 			rv = EIO;
11564 		} else {
11565 			pmportinfo->pmport_state |= SATA_PSTATE_SHUTDOWN;
11566 		}
11567 	}
11568 
11569 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11570 
11571 	return (rv);
11572 }
11573 
11574 /*
11575  * Process ioctl port activate request.
11576  *
11577  * NOTE: Port multiplier code is not completed nor tested.
11578  */
11579 static int
11580 sata_ioctl_activate(sata_hba_inst_t *sata_hba_inst,
11581     sata_device_t *sata_device)
11582 {
11583 	int cport, pmport, qual;
11584 	sata_cport_info_t *cportinfo;
11585 	sata_pmport_info_t *pmportinfo = NULL;
11586 	boolean_t dev_existed = TRUE;
11587 
11588 	/* Sanity check */
11589 	if (SATA_PORT_ACTIVATE_FUNC(sata_hba_inst) == NULL)
11590 		return (ENOTSUP);
11591 
11592 	cport = sata_device->satadev_addr.cport;
11593 	pmport = sata_device->satadev_addr.pmport;
11594 	qual = sata_device->satadev_addr.qual;
11595 
11596 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11597 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, cport);
11598 	if (qual == SATA_ADDR_PMPORT) {
11599 		pmportinfo = SATA_PMPORT_INFO(sata_hba_inst, cport, pmport);
11600 		if (pmportinfo->pmport_state & SATA_PSTATE_SHUTDOWN ||
11601 		    pmportinfo->pmport_dev_type == SATA_DTYPE_NONE)
11602 			dev_existed = FALSE;
11603 	} else { /* cport */
11604 		if (cportinfo->cport_state & SATA_PSTATE_SHUTDOWN ||
11605 		    cportinfo->cport_dev_type == SATA_DTYPE_NONE)
11606 			dev_existed = FALSE;
11607 	}
11608 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11609 
11610 	/* Just let HBA driver to activate port, if necessary */
11611 	if ((*SATA_PORT_ACTIVATE_FUNC(sata_hba_inst))
11612 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11613 		/*
11614 		 * Port activation failure - do not change port state unless
11615 		 * the state returned by HBA indicates a port failure.
11616 		 */
11617 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
11618 		    cport)->cport_mutex);
11619 		sata_update_port_info(sata_hba_inst, sata_device);
11620 		if (sata_device->satadev_state & SATA_PSTATE_FAILED) {
11621 			if (qual == SATA_ADDR_PMPORT)
11622 				pmportinfo->pmport_state = SATA_PSTATE_FAILED;
11623 			else
11624 				cportinfo->cport_state = SATA_PSTATE_FAILED;
11625 
11626 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
11627 			    cport)->cport_mutex);
11628 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11629 			    "sata_hba_ioctl: port activate: cannot activate "
11630 			    "SATA port %d:%d", cport, pmport));
11631 			return (EIO);
11632 		}
11633 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11634 	}
11635 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11636 	if (qual == SATA_ADDR_PMPORT)
11637 		pmportinfo->pmport_state &= ~SATA_PSTATE_SHUTDOWN;
11638 	else
11639 		cportinfo->cport_state &= ~SATA_PSTATE_SHUTDOWN;
11640 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11641 
11642 	/*
11643 	 * Re-probe port to find its current state and possibly attached device.
11644 	 * Port re-probing may change the cportinfo device type if device is
11645 	 * found attached.
11646 	 * If port probing failed, the device type would be set to
11647 	 * SATA_DTYPE_NONE.
11648 	 */
11649 	(void) sata_reprobe_port(sata_hba_inst, sata_device,
11650 	    SATA_DEV_IDENTIFY_RETRY);
11651 
11652 	/*
11653 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
11654 	 * without the hint.
11655 	 */
11656 	sata_gen_sysevent(sata_hba_inst, &sata_device->satadev_addr,
11657 	    SE_NO_HINT);
11658 
11659 	if (dev_existed == FALSE) {
11660 		if (qual == SATA_ADDR_PMPORT &&
11661 		    pmportinfo->pmport_dev_type != SATA_DTYPE_NONE) {
11662 			/*
11663 			 * That's the transition from the "inactive" port state
11664 			 * or the active port without a device attached to the
11665 			 * active port state with a device attached.
11666 			 */
11667 			sata_log(sata_hba_inst, CE_WARN,
11668 			    "SATA device detected at port %d:%d",
11669 			    cport, pmport);
11670 		} else if (qual == SATA_ADDR_CPORT &&
11671 		    cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
11672 			/*
11673 			 * That's the transition from the "inactive" port state
11674 			 * or the active port without a device attached to the
11675 			 * active port state with a device attached.
11676 			 */
11677 			if (cportinfo->cport_dev_type != SATA_DTYPE_PMULT) {
11678 				sata_log(sata_hba_inst, CE_WARN,
11679 				    "SATA device detected at port %d", cport);
11680 			} else {
11681 				sata_log(sata_hba_inst, CE_WARN,
11682 				    "SATA port multiplier detected at port %d",
11683 				    cport);
11684 				/*
11685 				 * Because the detected device is a port
11686 				 * multiplier, we need to reprobe every device
11687 				 * port on the port multiplier and show every
11688 				 * device found attached.
11689 				 * Add this code here.
11690 				 */
11691 			}
11692 		}
11693 	}
11694 	return (0);
11695 }
11696 
11697 
11698 
11699 /*
11700  * Process ioctl reset port request.
11701  *
11702  * NOTE: Port multiplier code is not completed nor tested.
11703  */
11704 static int
11705 sata_ioctl_reset_port(sata_hba_inst_t *sata_hba_inst,
11706     sata_device_t *sata_device)
11707 {
11708 	int cport, pmport, qual;
11709 	int rv = 0;
11710 
11711 	cport = sata_device->satadev_addr.cport;
11712 	pmport = sata_device->satadev_addr.pmport;
11713 	qual = sata_device->satadev_addr.qual;
11714 
11715 	/* Sanity check */
11716 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
11717 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11718 		    "sata_hba_ioctl: sata_hba_tran missing required "
11719 		    "function sata_tran_reset_dport"));
11720 		return (ENOTSUP);
11721 	}
11722 
11723 	/* Ask HBA to reset port */
11724 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst),
11725 	    sata_device) != SATA_SUCCESS) {
11726 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11727 		    "sata_hba_ioctl: reset port: failed %d:%d",
11728 		    cport, pmport));
11729 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11730 		    cport_mutex);
11731 		sata_update_port_info(sata_hba_inst, sata_device);
11732 		if (qual == SATA_ADDR_CPORT)
11733 			SATA_CPORT_STATE(sata_hba_inst, cport) =
11734 			    SATA_PSTATE_FAILED;
11735 		else
11736 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
11737 			    SATA_PSTATE_FAILED;
11738 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11739 		    cport_mutex);
11740 		rv = EIO;
11741 	}
11742 	/*
11743 	 * Beacuse the port was reset, it should be probed and
11744 	 * attached device reinitialized. At this point the
11745 	 * port state is unknown - it's state is HBA-specific.
11746 	 * Re-probe port to get its state.
11747 	 */
11748 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11749 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS) {
11750 		rv = EIO;
11751 	}
11752 	return (rv);
11753 }
11754 
11755 /*
11756  * Process ioctl reset device request.
11757  *
11758  * NOTE: Port multiplier code is not completed nor tested.
11759  */
11760 static int
11761 sata_ioctl_reset_device(sata_hba_inst_t *sata_hba_inst,
11762     sata_device_t *sata_device)
11763 {
11764 	sata_drive_info_t *sdinfo;
11765 	int cport, pmport, qual;
11766 	int rv = 0;
11767 
11768 	/* Sanity check */
11769 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
11770 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11771 		    "sata_hba_ioctl: sata_hba_tran missing required "
11772 		    "function sata_tran_reset_dport"));
11773 		return (ENOTSUP);
11774 	}
11775 
11776 	cport = sata_device->satadev_addr.cport;
11777 	pmport = sata_device->satadev_addr.pmport;
11778 	qual = sata_device->satadev_addr.qual;
11779 
11780 	if (qual == SATA_ADDR_CPORT)
11781 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
11782 	else
11783 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
11784 
11785 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11786 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
11787 	if (sdinfo == NULL) {
11788 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11789 		return (EINVAL);
11790 	}
11791 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11792 
11793 	/* Ask HBA to reset device */
11794 	if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
11795 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11796 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11797 		    "sata_hba_ioctl: reset device: failed at port %d:%d",
11798 		    cport, pmport));
11799 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11800 		    cport_mutex);
11801 		sata_update_port_info(sata_hba_inst, sata_device);
11802 		/*
11803 		 * Device info structure remains attached. Another device reset
11804 		 * or port disconnect/connect and re-probing is
11805 		 * needed to change it's state
11806 		 */
11807 		sdinfo->satadrv_state &= ~SATA_STATE_READY;
11808 		sdinfo->satadrv_state |= SATA_DSTATE_FAILED;
11809 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->cport_mutex);
11810 		rv = EIO;
11811 	}
11812 	/*
11813 	 * If attached device was a port multiplier, some extra processing
11814 	 * may be needed, to bring it back (if port re-probing did not handle
11815 	 * it). Add such code here.
11816 	 */
11817 	return (rv);
11818 }
11819 
11820 
11821 /*
11822  * Process ioctl reset all request.
11823  *
11824  * NOTE: Port multiplier code is not completed nor tested.
11825  */
11826 static int
11827 sata_ioctl_reset_all(sata_hba_inst_t *sata_hba_inst)
11828 {
11829 	sata_device_t sata_device;
11830 	int rv = 0;
11831 	int tcport;
11832 	int tpmport = 0;
11833 
11834 	sata_device.satadev_rev = SATA_DEVICE_REV;
11835 
11836 	/*
11837 	 * There is no protection here for configured devices.
11838 	 */
11839 	/* Sanity check */
11840 	if (SATA_RESET_DPORT_FUNC(sata_hba_inst) == NULL) {
11841 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11842 		    "sata_hba_ioctl: sata_hba_tran missing required "
11843 		    "function sata_tran_reset_dport"));
11844 		return (ENOTSUP);
11845 	}
11846 
11847 	/*
11848 	 * Need to lock all ports, not just one.
11849 	 * If any port is locked by event processing, fail the whole operation.
11850 	 * One port is already locked, but for simplicity lock it again.
11851 	 */
11852 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
11853 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11854 		    cport_mutex);
11855 		if (((SATA_CPORT_INFO(sata_hba_inst, tcport)->
11856 		    cport_event_flags) & SATA_EVNT_LOCK_PORT_BUSY) != 0) {
11857 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11858 			    cport_mutex);
11859 			rv = EBUSY;
11860 			break;
11861 		} else {
11862 			SATA_CPORT_INFO(sata_hba_inst, tcport)->
11863 			    cport_event_flags |= SATA_APCTL_LOCK_PORT_BUSY;
11864 			/*
11865 			 * If there is a port multiplier attached, we may need
11866 			 * to lock its port as well. If so, add such code here.
11867 			 */
11868 		}
11869 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11870 		    cport_mutex);
11871 	}
11872 
11873 	if (rv == 0) {
11874 		/*
11875 		 * All cports were successfully locked.
11876 		 * Reset main SATA controller only for now - no PMult.
11877 		 */
11878 		sata_device.satadev_addr.qual = SATA_ADDR_CNTRL;
11879 
11880 		if ((*SATA_RESET_DPORT_FUNC(sata_hba_inst))
11881 		    (SATA_DIP(sata_hba_inst), &sata_device) != SATA_SUCCESS) {
11882 			SATA_LOG_D((sata_hba_inst, CE_WARN,
11883 			    "sata_hba_ioctl: reset controller failed"));
11884 			return (EIO);
11885 		}
11886 		/*
11887 		 * Because ports were reset, port states are unknown.
11888 		 * They should be re-probed to get their state and
11889 		 * attached devices should be reinitialized.
11890 		 * Add code here to re-probe port multiplier device ports.
11891 		 */
11892 		for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst);
11893 		    tcport++) {
11894 			sata_device.satadev_addr.cport = tcport;
11895 			sata_device.satadev_addr.pmport = tpmport;
11896 			sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
11897 
11898 			if (sata_reprobe_port(sata_hba_inst, &sata_device,
11899 			    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
11900 				rv = EIO;
11901 		}
11902 	}
11903 	/*
11904 	 * Unlock all ports
11905 	 */
11906 	for (tcport = 0; tcport < SATA_NUM_CPORTS(sata_hba_inst); tcport++) {
11907 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11908 		    cport_mutex);
11909 		SATA_CPORT_INFO(sata_hba_inst, tcport)->
11910 		    cport_event_flags &= ~SATA_APCTL_LOCK_PORT_BUSY;
11911 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, tcport)->
11912 		    cport_mutex);
11913 	}
11914 
11915 	/*
11916 	 * This operation returns EFAULT if either reset
11917 	 * controller failed or a re-probing of any ports failed.
11918 	 */
11919 	return (rv);
11920 }
11921 
11922 
11923 /*
11924  * Process ioctl port self test request.
11925  *
11926  * NOTE: Port multiplier code is not completed nor tested.
11927  */
11928 static int
11929 sata_ioctl_port_self_test(sata_hba_inst_t *sata_hba_inst,
11930     sata_device_t *sata_device)
11931 {
11932 	int cport, pmport, qual;
11933 	int rv = 0;
11934 
11935 	/* Sanity check */
11936 	if (SATA_SELFTEST_FUNC(sata_hba_inst) == NULL)
11937 		return (ENOTSUP);
11938 
11939 	cport = sata_device->satadev_addr.cport;
11940 	pmport = sata_device->satadev_addr.pmport;
11941 	qual = sata_device->satadev_addr.qual;
11942 
11943 	/*
11944 	 * There is no protection here for a configured
11945 	 * device attached to this port.
11946 	 */
11947 
11948 	if ((*SATA_SELFTEST_FUNC(sata_hba_inst))
11949 	    (SATA_DIP(sata_hba_inst), sata_device) != SATA_SUCCESS) {
11950 		SATA_LOG_D((sata_hba_inst, CE_WARN,
11951 		    "sata_hba_ioctl: port selftest: "
11952 		    "failed port %d:%d", cport, pmport));
11953 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11954 		    cport_mutex);
11955 		sata_update_port_info(sata_hba_inst, sata_device);
11956 		if (qual == SATA_ADDR_CPORT)
11957 			SATA_CPORT_STATE(sata_hba_inst, cport) =
11958 			    SATA_PSTATE_FAILED;
11959 		else /* port ultiplier device port */
11960 			SATA_PMPORT_STATE(sata_hba_inst, cport, pmport) =
11961 			    SATA_PSTATE_FAILED;
11962 
11963 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, cport)->
11964 		    cport_mutex);
11965 		return (EIO);
11966 	}
11967 	/*
11968 	 * Beacuse the port was reset in the course of testing, it should be
11969 	 * re-probed and attached device state should be restored. At this
11970 	 * point the port state is unknown - it's state is HBA-specific.
11971 	 * Force port re-probing to get it into a known state.
11972 	 */
11973 	if (sata_reprobe_port(sata_hba_inst, sata_device,
11974 	    SATA_DEV_IDENTIFY_RETRY) != SATA_SUCCESS)
11975 		rv = EIO;
11976 	return (rv);
11977 }
11978 
11979 
11980 /*
11981  * sata_cfgadm_state:
11982  * Use the sata port state and state of the target node to figure out
11983  * the cfgadm_state.
11984  *
11985  * The port argument is a value with encoded cport,
11986  * pmport and address qualifier, in the same manner as a scsi target number.
11987  * SCSI_TO_SATA_CPORT macro extracts cport number,
11988  * SCSI_TO_SATA_PMPORT extracts pmport number and
11989  * SCSI_TO_SATA_ADDR_QUAL extracts port mulitplier qualifier flag.
11990  *
11991  * For now, support is for cports only - no port multiplier device ports.
11992  */
11993 
11994 static void
11995 sata_cfgadm_state(sata_hba_inst_t *sata_hba_inst, int32_t port,
11996     devctl_ap_state_t *ap_state)
11997 {
11998 	uint16_t	cport;
11999 	int		port_state;
12000 
12001 	/* Cport only */
12002 	cport = SCSI_TO_SATA_CPORT(port);
12003 
12004 	port_state = SATA_CPORT_STATE(sata_hba_inst, cport);
12005 	if (port_state & SATA_PSTATE_SHUTDOWN ||
12006 	    port_state & SATA_PSTATE_FAILED) {
12007 		ap_state->ap_rstate = AP_RSTATE_DISCONNECTED;
12008 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12009 		if (port_state & SATA_PSTATE_FAILED)
12010 			ap_state->ap_condition = AP_COND_FAILED;
12011 		else
12012 			ap_state->ap_condition = AP_COND_UNKNOWN;
12013 
12014 		return;
12015 	}
12016 
12017 	/* Need to check pmult device port here as well, when supported */
12018 
12019 	/* Port is enabled and ready */
12020 
12021 	switch (SATA_CPORT_DEV_TYPE(sata_hba_inst, cport)) {
12022 	case SATA_DTYPE_NONE:
12023 	{
12024 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12025 		ap_state->ap_condition = AP_COND_OK;
12026 		/* No device attached */
12027 		ap_state->ap_rstate = AP_RSTATE_EMPTY;
12028 		break;
12029 	}
12030 	case SATA_DTYPE_UNKNOWN:
12031 	case SATA_DTYPE_ATAPINONCD:
12032 	case SATA_DTYPE_PMULT:	/* Until PMult is supported */
12033 	case SATA_DTYPE_ATADISK:
12034 	case SATA_DTYPE_ATAPICD:
12035 	{
12036 		dev_info_t *tdip = NULL;
12037 		dev_info_t *dip = NULL;
12038 		int circ;
12039 
12040 		dip = SATA_DIP(sata_hba_inst);
12041 		tdip = sata_get_target_dip(dip, port);
12042 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
12043 		if (tdip != NULL) {
12044 			ndi_devi_enter(dip, &circ);
12045 			mutex_enter(&(DEVI(tdip)->devi_lock));
12046 			if (DEVI_IS_DEVICE_REMOVED(tdip)) {
12047 				/*
12048 				 * There could be the case where previously
12049 				 * configured and opened device was removed
12050 				 * and unknown device was plugged.
12051 				 * In such case we want to show a device, and
12052 				 * its configured or unconfigured state but
12053 				 * indicate unusable condition untill the
12054 				 * old target node is released and removed.
12055 				 */
12056 				ap_state->ap_condition = AP_COND_UNUSABLE;
12057 			} else {
12058 				ap_state->ap_condition = AP_COND_OK;
12059 			}
12060 			if ((DEVI_IS_DEVICE_OFFLINE(tdip)) ||
12061 			    (DEVI_IS_DEVICE_DOWN(tdip))) {
12062 				ap_state->ap_ostate =
12063 				    AP_OSTATE_UNCONFIGURED;
12064 			} else {
12065 				ap_state->ap_ostate =
12066 				    AP_OSTATE_CONFIGURED;
12067 			}
12068 			mutex_exit(&(DEVI(tdip)->devi_lock));
12069 			ndi_devi_exit(dip, circ);
12070 		} else {
12071 			ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12072 			ap_state->ap_condition = AP_COND_UNKNOWN;
12073 		}
12074 		break;
12075 	}
12076 	default:
12077 		ap_state->ap_rstate = AP_RSTATE_CONNECTED;
12078 		ap_state->ap_ostate = AP_OSTATE_UNCONFIGURED;
12079 		ap_state->ap_condition = AP_COND_UNKNOWN;
12080 		/*
12081 		 * This is actually internal error condition (non fatal),
12082 		 * because we have already checked all defined device types.
12083 		 */
12084 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12085 		    "sata_cfgadm_state: Internal error: "
12086 		    "unknown device type"));
12087 		break;
12088 	}
12089 }
12090 
12091 
12092 /*
12093  * Process ioctl get device path request.
12094  *
12095  * NOTE: Port multiplier code is not completed nor tested.
12096  */
12097 static int
12098 sata_ioctl_get_device_path(sata_hba_inst_t *sata_hba_inst,
12099     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12100 {
12101 	char path[MAXPATHLEN];
12102 	uint32_t size;
12103 	dev_info_t *tdip;
12104 
12105 	(void) strcpy(path, "/devices");
12106 	if ((tdip = sata_get_scsi_target_dip(SATA_DIP(sata_hba_inst),
12107 	    &sata_device->satadev_addr)) == NULL) {
12108 		/*
12109 		 * No such device. If this is a request for a size, do not
12110 		 * return EINVAL for non-existing target, because cfgadm
12111 		 * will then indicate a meaningless ioctl failure.
12112 		 * If this is a request for a path, indicate invalid
12113 		 * argument.
12114 		 */
12115 		if (ioc->get_size == 0)
12116 			return (EINVAL);
12117 	} else {
12118 		(void) ddi_pathname(tdip, path + strlen(path));
12119 	}
12120 	size = strlen(path) + 1;
12121 
12122 	if (ioc->get_size != 0) {
12123 		if (ddi_copyout((void *)&size, ioc->buf, ioc->bufsiz,
12124 		    mode) != 0)
12125 			return (EFAULT);
12126 	} else {
12127 		if (ioc->bufsiz != size)
12128 			return (EINVAL);
12129 
12130 		else if (ddi_copyout((void *)&path, ioc->buf, ioc->bufsiz,
12131 		    mode) != 0)
12132 			return (EFAULT);
12133 	}
12134 	return (0);
12135 }
12136 
12137 /*
12138  * Process ioctl get attachment point type request.
12139  *
12140  * NOTE: Port multiplier code is not completed nor tested.
12141  */
12142 static	int
12143 sata_ioctl_get_ap_type(sata_hba_inst_t *sata_hba_inst,
12144     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12145 {
12146 	uint32_t	type_len;
12147 	const char	*ap_type;
12148 	int		dev_type;
12149 
12150 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12151 		dev_type = SATA_CPORT_DEV_TYPE(sata_hba_inst,
12152 		    sata_device->satadev_addr.cport);
12153 	else /* pmport */
12154 		dev_type = SATA_PMPORT_DEV_TYPE(sata_hba_inst,
12155 		    sata_device->satadev_addr.cport,
12156 		    sata_device->satadev_addr.pmport);
12157 
12158 	switch (dev_type) {
12159 	case SATA_DTYPE_NONE:
12160 		ap_type = "port";
12161 		break;
12162 
12163 	case SATA_DTYPE_ATADISK:
12164 		ap_type = "disk";
12165 		break;
12166 
12167 	case SATA_DTYPE_ATAPICD:
12168 		ap_type = "cd/dvd";
12169 		break;
12170 
12171 	case SATA_DTYPE_PMULT:
12172 		ap_type = "pmult";
12173 		break;
12174 
12175 	case SATA_DTYPE_UNKNOWN:
12176 		ap_type = "unknown";
12177 		break;
12178 
12179 	default:
12180 		ap_type = "unsupported";
12181 		break;
12182 
12183 	} /* end of dev_type switch */
12184 
12185 	type_len = strlen(ap_type) + 1;
12186 
12187 	if (ioc->get_size) {
12188 		if (ddi_copyout((void *)&type_len, ioc->buf, ioc->bufsiz,
12189 		    mode) != 0)
12190 			return (EFAULT);
12191 	} else {
12192 		if (ioc->bufsiz != type_len)
12193 			return (EINVAL);
12194 
12195 		if (ddi_copyout((void *)ap_type, ioc->buf,
12196 		    ioc->bufsiz, mode) != 0)
12197 			return (EFAULT);
12198 	}
12199 	return (0);
12200 
12201 }
12202 
12203 /*
12204  * Process ioctl get device model info request.
12205  * This operation should return to cfgadm the device model
12206  * information string
12207  *
12208  * NOTE: Port multiplier code is not completed nor tested.
12209  */
12210 static	int
12211 sata_ioctl_get_model_info(sata_hba_inst_t *sata_hba_inst,
12212     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12213 {
12214 	sata_drive_info_t *sdinfo;
12215 	uint32_t info_len;
12216 	char ap_info[SATA_ID_MODEL_LEN + 1];
12217 
12218 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12219 	    sata_device->satadev_addr.cport)->cport_mutex);
12220 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12221 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
12222 	else /* port multiplier */
12223 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
12224 
12225 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12226 	if (sdinfo == NULL) {
12227 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12228 		    sata_device->satadev_addr.cport)->cport_mutex);
12229 		return (EINVAL);
12230 	}
12231 
12232 #ifdef	_LITTLE_ENDIAN
12233 	swab(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
12234 #else	/* _LITTLE_ENDIAN */
12235 	bcopy(sdinfo->satadrv_id.ai_model, ap_info, SATA_ID_MODEL_LEN);
12236 #endif	/* _LITTLE_ENDIAN */
12237 
12238 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12239 	    sata_device->satadev_addr.cport)->cport_mutex);
12240 
12241 	ap_info[SATA_ID_MODEL_LEN] = '\0';
12242 
12243 	info_len = strlen(ap_info) + 1;
12244 
12245 	if (ioc->get_size) {
12246 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12247 		    mode) != 0)
12248 			return (EFAULT);
12249 	} else {
12250 		if (ioc->bufsiz < info_len)
12251 			return (EINVAL);
12252 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12253 		    mode) != 0)
12254 			return (EFAULT);
12255 	}
12256 	return (0);
12257 }
12258 
12259 
12260 /*
12261  * Process ioctl get device firmware revision info request.
12262  * This operation should return to cfgadm the device firmware revision
12263  * information string
12264  *
12265  * NOTE: Port multiplier code is not completed nor tested.
12266  */
12267 static	int
12268 sata_ioctl_get_revfirmware_info(sata_hba_inst_t *sata_hba_inst,
12269     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12270 {
12271 	sata_drive_info_t *sdinfo;
12272 	uint32_t info_len;
12273 	char ap_info[SATA_ID_FW_LEN + 1];
12274 
12275 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12276 	    sata_device->satadev_addr.cport)->cport_mutex);
12277 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12278 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
12279 	else /* port multiplier */
12280 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
12281 
12282 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12283 	if (sdinfo == NULL) {
12284 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12285 		    sata_device->satadev_addr.cport)->cport_mutex);
12286 		return (EINVAL);
12287 	}
12288 
12289 #ifdef	_LITTLE_ENDIAN
12290 	swab(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
12291 #else	/* _LITTLE_ENDIAN */
12292 	bcopy(sdinfo->satadrv_id.ai_fw, ap_info, SATA_ID_FW_LEN);
12293 #endif	/* _LITTLE_ENDIAN */
12294 
12295 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12296 	    sata_device->satadev_addr.cport)->cport_mutex);
12297 
12298 	ap_info[SATA_ID_FW_LEN] = '\0';
12299 
12300 	info_len = strlen(ap_info) + 1;
12301 
12302 	if (ioc->get_size) {
12303 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12304 		    mode) != 0)
12305 			return (EFAULT);
12306 	} else {
12307 		if (ioc->bufsiz < info_len)
12308 			return (EINVAL);
12309 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12310 		    mode) != 0)
12311 			return (EFAULT);
12312 	}
12313 	return (0);
12314 }
12315 
12316 
12317 /*
12318  * Process ioctl get device serial number info request.
12319  * This operation should return to cfgadm the device serial number string.
12320  *
12321  * NOTE: Port multiplier code is not completed nor tested.
12322  */
12323 static	int
12324 sata_ioctl_get_serialnumber_info(sata_hba_inst_t *sata_hba_inst,
12325     sata_device_t *sata_device, sata_ioctl_data_t *ioc, int mode)
12326 {
12327 	sata_drive_info_t *sdinfo;
12328 	uint32_t info_len;
12329 	char ap_info[SATA_ID_SERIAL_LEN + 1];
12330 
12331 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
12332 	    sata_device->satadev_addr.cport)->cport_mutex);
12333 	if (sata_device->satadev_addr.qual == SATA_ADDR_CPORT)
12334 		sata_device->satadev_addr.qual = SATA_ADDR_DCPORT;
12335 	else /* port multiplier */
12336 		sata_device->satadev_addr.qual = SATA_ADDR_DPMPORT;
12337 
12338 	sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
12339 	if (sdinfo == NULL) {
12340 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12341 		    sata_device->satadev_addr.cport)->cport_mutex);
12342 		return (EINVAL);
12343 	}
12344 
12345 #ifdef	_LITTLE_ENDIAN
12346 	swab(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
12347 #else	/* _LITTLE_ENDIAN */
12348 	bcopy(sdinfo->satadrv_id.ai_drvser, ap_info, SATA_ID_SERIAL_LEN);
12349 #endif	/* _LITTLE_ENDIAN */
12350 
12351 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
12352 	    sata_device->satadev_addr.cport)->cport_mutex);
12353 
12354 	ap_info[SATA_ID_SERIAL_LEN] = '\0';
12355 
12356 	info_len = strlen(ap_info) + 1;
12357 
12358 	if (ioc->get_size) {
12359 		if (ddi_copyout((void *)&info_len, ioc->buf, ioc->bufsiz,
12360 		    mode) != 0)
12361 			return (EFAULT);
12362 	} else {
12363 		if (ioc->bufsiz < info_len)
12364 			return (EINVAL);
12365 		if (ddi_copyout((void *)ap_info, ioc->buf, ioc->bufsiz,
12366 		    mode) != 0)
12367 			return (EFAULT);
12368 	}
12369 	return (0);
12370 }
12371 
12372 
12373 /*
12374  * Preset scsi extended sense data (to NO SENSE)
12375  * First 18 bytes of the sense data are preset to current valid sense
12376  * with a key NO SENSE data.
12377  *
12378  * Returns void
12379  */
12380 static void
12381 sata_fixed_sense_data_preset(struct scsi_extended_sense *sense)
12382 {
12383 	sense->es_valid = 1;		/* Valid sense */
12384 	sense->es_class = CLASS_EXTENDED_SENSE;	/* 0x70 - current err */
12385 	sense->es_key = KEY_NO_SENSE;
12386 	sense->es_info_1 = 0;
12387 	sense->es_info_2 = 0;
12388 	sense->es_info_3 = 0;
12389 	sense->es_info_4 = 0;
12390 	sense->es_add_len = 10;	/* Additional length - replace with a def */
12391 	sense->es_cmd_info[0] = 0;
12392 	sense->es_cmd_info[1] = 0;
12393 	sense->es_cmd_info[2] = 0;
12394 	sense->es_cmd_info[3] = 0;
12395 	sense->es_add_code = 0;
12396 	sense->es_qual_code = 0;
12397 }
12398 
12399 /*
12400  * Register a legacy cmdk-style devid for the target (disk) device.
12401  *
12402  * Note: This function is called only when the HBA devinfo node has the
12403  * property "use-cmdk-devid-format" set. This property indicates that
12404  * devid compatible with old cmdk (target) driver is to be generated
12405  * for any target device attached to this controller. This will take
12406  * precedence over the devid generated by sd (target) driver.
12407  * This function is derived from cmdk_devid_setup() function in cmdk.c.
12408  */
12409 static void
12410 sata_target_devid_register(dev_info_t *dip, sata_drive_info_t *sdinfo)
12411 {
12412 	char	*hwid;
12413 	int	modlen;
12414 	int	serlen;
12415 	int	rval;
12416 	ddi_devid_t	devid;
12417 
12418 	/*
12419 	 * device ID is a concatanation of model number, "=", serial number.
12420 	 */
12421 	hwid = kmem_zalloc(LEGACY_HWID_LEN, KM_SLEEP);
12422 	bcopy(&sdinfo->satadrv_id.ai_model, hwid,
12423 	    sizeof (sdinfo->satadrv_id.ai_model));
12424 	swab(hwid, hwid, sizeof (sdinfo->satadrv_id.ai_model));
12425 	modlen = sata_check_modser(hwid, sizeof (sdinfo->satadrv_id.ai_model));
12426 	if (modlen == 0)
12427 		goto err;
12428 	hwid[modlen++] = '=';
12429 	bcopy(&sdinfo->satadrv_id.ai_drvser, &hwid[modlen],
12430 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12431 	swab(&hwid[modlen], &hwid[modlen],
12432 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12433 	serlen = sata_check_modser(&hwid[modlen],
12434 	    sizeof (sdinfo->satadrv_id.ai_drvser));
12435 	if (serlen == 0)
12436 		goto err;
12437 	hwid[modlen + serlen] = 0; /* terminate the hwid string */
12438 
12439 	/* initialize/register devid */
12440 	if ((rval = ddi_devid_init(dip, DEVID_ATA_SERIAL,
12441 	    (ushort_t)(modlen + serlen), hwid, &devid)) == DDI_SUCCESS)
12442 		rval = ddi_devid_register(dip, devid);
12443 
12444 	if (rval != DDI_SUCCESS)
12445 		cmn_err(CE_WARN, "sata: failed to create devid for the disk"
12446 		    " on port %d", sdinfo->satadrv_addr.cport);
12447 err:
12448 	kmem_free(hwid, LEGACY_HWID_LEN);
12449 }
12450 
12451 /*
12452  * valid model/serial string must contain a non-zero non-space characters.
12453  * trim trailing spaces/NULLs.
12454  */
12455 static int
12456 sata_check_modser(char *buf, int buf_len)
12457 {
12458 	boolean_t ret;
12459 	char *s;
12460 	int i;
12461 	int tb;
12462 	char ch;
12463 
12464 	ret = B_FALSE;
12465 	s = buf;
12466 	for (i = 0; i < buf_len; i++) {
12467 		ch = *s++;
12468 		if (ch != ' ' && ch != '\0')
12469 			tb = i + 1;
12470 		if (ch != ' ' && ch != '\0' && ch != '0')
12471 			ret = B_TRUE;
12472 	}
12473 
12474 	if (ret == B_FALSE)
12475 		return (0); /* invalid string */
12476 
12477 	return (tb); /* return length */
12478 }
12479 
12480 /*
12481  * sata_set_drive_features function compares current device features setting
12482  * with the saved device features settings and, if there is a difference,
12483  * it restores device features setting to the previously saved state.
12484  * It also arbitrarily tries to select the highest supported DMA mode.
12485  * Device Identify or Identify Packet Device data has to be current.
12486  * At the moment read ahead and write cache are considered for all devices.
12487  * For atapi devices, Removable Media Status Notification is set in addition
12488  * to common features.
12489  *
12490  * This function cannot be called in the interrupt context (it may sleep).
12491  *
12492  * The input argument sdinfo should point to the drive info structure
12493  * to be updated after features are set. Note, that only
12494  * device (packet) identify data is updated, not the flags indicating the
12495  * supported features.
12496  *
12497  * Returns TRUE if successful or there was nothing to do. Device Identify data
12498  * in the drive info structure pointed to by the sdinfo argumens is updated
12499  * even when no features were set or changed.
12500  *
12501  * Returns FALSE if device features could not be set.
12502  *
12503  * Note: This function may fail the port, making it inaccessible.
12504  * In such case the explicit port disconnect/connect or physical device
12505  * detach/attach is required to re-evaluate port state again.
12506  */
12507 
12508 static int
12509 sata_set_drive_features(sata_hba_inst_t *sata_hba_inst,
12510     sata_drive_info_t *sdinfo, int restore)
12511 {
12512 	int rval = SATA_SUCCESS;
12513 	sata_drive_info_t new_sdinfo;
12514 	char *finfo = "sata_set_drive_features: cannot";
12515 	char *finfox;
12516 	int cache_op;
12517 
12518 	bzero(&new_sdinfo, sizeof (sata_drive_info_t));
12519 	new_sdinfo.satadrv_addr = sdinfo->satadrv_addr;
12520 	new_sdinfo.satadrv_type = sdinfo->satadrv_type;
12521 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
12522 		/*
12523 		 * Cannot get device identification - retry later
12524 		 */
12525 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12526 		    "%s fetch device identify data\n", finfo));
12527 		return (SATA_FAILURE);
12528 	}
12529 	finfox = (restore != 0) ? " restore device features" :
12530 	    " initialize device features\n";
12531 
12532 	if (sdinfo->satadrv_type == SATA_DTYPE_ATADISK) {
12533 		/* Arbitrarily set UDMA mode */
12534 		if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
12535 		    SATA_SUCCESS) {
12536 			SATA_LOG_D((sata_hba_inst, CE_WARN,
12537 			    "%s set UDMA mode\n", finfo));
12538 			return (SATA_FAILURE);
12539 		}
12540 	} else { /* Assume SATA ATAPI CD/DVD */
12541 		/*  Set Removable Media Status Notification, if necessary */
12542 		if ((new_sdinfo.satadrv_id.ai_cmdset83 &
12543 		    SATA_RM_STATUS_NOTIFIC) != 0 && restore != 0) {
12544 			if (((sdinfo->satadrv_settings & SATA_DEV_RMSN) &&
12545 			    (!(new_sdinfo.satadrv_id.ai_features86 &
12546 			    SATA_RM_STATUS_NOTIFIC))) ||
12547 			    ((!(sdinfo->satadrv_settings & SATA_DEV_RMSN)) &&
12548 			    (new_sdinfo.satadrv_id.ai_features86 &
12549 			    SATA_RM_STATUS_NOTIFIC))) {
12550 				/* Current setting does not match saved one */
12551 				if (sata_set_rmsn(sata_hba_inst, sdinfo,
12552 				    sdinfo->satadrv_settings &
12553 				    SATA_DEV_RMSN) != SATA_SUCCESS)
12554 					rval = SATA_FAILURE;
12555 			}
12556 		}
12557 		/*
12558 		 * We have to set Multiword DMA or UDMA, if it is supported, as
12559 		 * we want to use DMA transfer mode whenever possible.
12560 		 * Some devices require explicit setting of the DMA mode.
12561 		 */
12562 		if (new_sdinfo.satadrv_id.ai_cap & SATA_DMA_SUPPORT) {
12563 			/* Set highest supported DMA mode */
12564 			if (sata_set_dma_mode(sata_hba_inst, &new_sdinfo) !=
12565 			    SATA_SUCCESS) {
12566 				SATA_LOG_D((sata_hba_inst, CE_WARN,
12567 				    "%s set UDMA mode\n", finfo));
12568 				rval = SATA_FAILURE;
12569 			}
12570 		}
12571 	}
12572 
12573 	if (!(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_LOOK_AHEAD) &&
12574 	    !(new_sdinfo.satadrv_id.ai_cmdset82 & SATA_WRITE_CACHE)) {
12575 		/* None of the features is supported - do nothing */
12576 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12577 		    "settable features not supported\n", NULL);
12578 		goto update_sdinfo;
12579 	}
12580 
12581 	if (((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) &&
12582 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD)) &&
12583 	    ((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) &&
12584 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
12585 		/* Nothing to do */
12586 		SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12587 		    "no device features to set\n", NULL);
12588 		goto update_sdinfo;
12589 	}
12590 
12591 	if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_LOOK_AHEAD) &&
12592 	    (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD))) {
12593 		if (sdinfo->satadrv_settings & SATA_DEV_READ_AHEAD) {
12594 			/* Enable read ahead / read cache */
12595 			cache_op = SATAC_SF_ENABLE_READ_AHEAD;
12596 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12597 			    "enabling read cache\n", NULL);
12598 		} else {
12599 			/* Disable read ahead  / read cache */
12600 			cache_op = SATAC_SF_DISABLE_READ_AHEAD;
12601 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12602 			    "disabling read cache\n", NULL);
12603 		}
12604 
12605 		/* Try to set read cache mode */
12606 		if (sata_set_cache_mode(sata_hba_inst, &new_sdinfo,
12607 		    cache_op) != SATA_SUCCESS) {
12608 			/* Pkt execution failed */
12609 			rval = SATA_FAILURE;
12610 		}
12611 	}
12612 
12613 	if (!((new_sdinfo.satadrv_id.ai_features85 & SATA_WRITE_CACHE) &&
12614 	    (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE))) {
12615 		if (sdinfo->satadrv_settings & SATA_DEV_WRITE_CACHE) {
12616 			/* Enable write cache */
12617 			cache_op = SATAC_SF_ENABLE_WRITE_CACHE;
12618 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12619 			    "enabling write cache\n", NULL);
12620 		} else {
12621 			/* Disable write cache */
12622 			cache_op = SATAC_SF_DISABLE_WRITE_CACHE;
12623 			SATADBG1(SATA_DBG_DEV_SETTINGS, sata_hba_inst,
12624 			    "disabling write cache\n", NULL);
12625 		}
12626 		/* Try to set write cache mode */
12627 		if (sata_set_cache_mode(sata_hba_inst, &new_sdinfo,
12628 		    cache_op) != SATA_SUCCESS) {
12629 			/* Pkt execution failed */
12630 			rval = SATA_FAILURE;
12631 		}
12632 	}
12633 
12634 	if (rval == SATA_FAILURE)
12635 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12636 		    "%s %s", finfo, finfox));
12637 update_sdinfo:
12638 	/*
12639 	 * We need to fetch Device Identify data again
12640 	 */
12641 	if (sata_fetch_device_identify_data(sata_hba_inst, &new_sdinfo) != 0) {
12642 		/*
12643 		 * Cannot get device identification - retry later
12644 		 */
12645 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12646 		    "%s cannot re-fetch device identify data\n"));
12647 		rval = SATA_FAILURE;
12648 	}
12649 	/* Copy device sata info. */
12650 	sdinfo->satadrv_id = new_sdinfo.satadrv_id;
12651 
12652 	return (rval);
12653 }
12654 
12655 
12656 /*
12657  *
12658  * Returns 1 if threshold exceeded, 0 if threshold not exceeded, -1 if
12659  * unable to determine.
12660  *
12661  * Cannot be called in an interrupt context.
12662  *
12663  * Called by sata_build_lsense_page_2f()
12664  */
12665 
12666 static int
12667 sata_fetch_smart_return_status(sata_hba_inst_t *sata_hba_inst,
12668     sata_drive_info_t *sdinfo)
12669 {
12670 	sata_pkt_t *spkt;
12671 	sata_cmd_t *scmd;
12672 	sata_pkt_txlate_t *spx;
12673 	int rval;
12674 
12675 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12676 	spx->txlt_sata_hba_inst = sata_hba_inst;
12677 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12678 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12679 	if (spkt == NULL) {
12680 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12681 		return (-1);
12682 	}
12683 	/* address is needed now */
12684 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12685 
12686 
12687 	/* Fill sata_pkt */
12688 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12689 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12690 	/* Synchronous mode, no callback */
12691 	spkt->satapkt_comp = NULL;
12692 	/* Timeout 30s */
12693 	spkt->satapkt_time = sata_default_pkt_time;
12694 
12695 	scmd = &spkt->satapkt_cmd;
12696 	scmd->satacmd_flags.sata_special_regs = B_TRUE;
12697 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_NODATA_XFER;
12698 
12699 	/* Set up which registers need to be returned */
12700 	scmd->satacmd_flags.sata_copy_out_lba_mid_lsb = B_TRUE;
12701 	scmd->satacmd_flags.sata_copy_out_lba_high_lsb = B_TRUE;
12702 
12703 	/* Build SMART_RETURN_STATUS cmd in the sata_pkt */
12704 	scmd->satacmd_addr_type = 0;		/* N/A */
12705 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
12706 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
12707 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
12708 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
12709 	scmd->satacmd_features_reg = SATA_SMART_RETURN_STATUS;
12710 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12711 	scmd->satacmd_cmd_reg = SATAC_SMART;
12712 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12713 	    sdinfo->satadrv_addr.cport)));
12714 
12715 
12716 	/* Send pkt to SATA HBA driver */
12717 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12718 	    SATA_TRAN_ACCEPTED ||
12719 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12720 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12721 		    sdinfo->satadrv_addr.cport)));
12722 		/*
12723 		 * Whoops, no SMART RETURN STATUS
12724 		 */
12725 		rval = -1;
12726 	} else {
12727 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12728 		    sdinfo->satadrv_addr.cport)));
12729 		if (scmd->satacmd_error_reg & SATA_ERROR_ABORT) {
12730 			rval = -1;
12731 			goto fail;
12732 		}
12733 		if (scmd->satacmd_status_reg & SATA_STATUS_ERR) {
12734 			rval = -1;
12735 			goto fail;
12736 		}
12737 		if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_1) &&
12738 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_2))
12739 			rval = 0;
12740 		else if ((scmd->satacmd_lba_mid_lsb == SMART_MAGIC_VAL_3) &&
12741 		    (scmd->satacmd_lba_high_lsb == SMART_MAGIC_VAL_4))
12742 			rval = 1;
12743 		else {
12744 			rval = -1;
12745 			goto fail;
12746 		}
12747 	}
12748 fail:
12749 	/* Free allocated resources */
12750 	sata_pkt_free(spx);
12751 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12752 
12753 	return (rval);
12754 }
12755 
12756 /*
12757  *
12758  * Returns 0 if succeeded, -1 otherwise
12759  *
12760  * Cannot be called in an interrupt context.
12761  *
12762  */
12763 static int
12764 sata_fetch_smart_data(
12765 	sata_hba_inst_t *sata_hba_inst,
12766 	sata_drive_info_t *sdinfo,
12767 	struct smart_data *smart_data)
12768 {
12769 	sata_pkt_t *spkt;
12770 	sata_cmd_t *scmd;
12771 	sata_pkt_txlate_t *spx;
12772 	int rval;
12773 
12774 #if ! defined(lint)
12775 	ASSERT(sizeof (struct smart_data) == 512);
12776 #endif
12777 
12778 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12779 	spx->txlt_sata_hba_inst = sata_hba_inst;
12780 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12781 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12782 	if (spkt == NULL) {
12783 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12784 		return (-1);
12785 	}
12786 	/* address is needed now */
12787 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12788 
12789 
12790 	/* Fill sata_pkt */
12791 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12792 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12793 	/* Synchronous mode, no callback */
12794 	spkt->satapkt_comp = NULL;
12795 	/* Timeout 30s */
12796 	spkt->satapkt_time = sata_default_pkt_time;
12797 
12798 	scmd = &spkt->satapkt_cmd;
12799 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
12800 
12801 	/*
12802 	 * Allocate buffer for SMART data
12803 	 */
12804 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
12805 	    sizeof (struct smart_data));
12806 	if (scmd->satacmd_bp == NULL) {
12807 		sata_pkt_free(spx);
12808 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12809 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12810 		    "sata_fetch_smart_data: "
12811 		    "cannot allocate buffer"));
12812 		return (-1);
12813 	}
12814 
12815 
12816 	/* Build SMART_READ_DATA cmd in the sata_pkt */
12817 	scmd->satacmd_addr_type = 0;		/* N/A */
12818 	scmd->satacmd_sec_count_lsb = 0;	/* N/A */
12819 	scmd->satacmd_lba_low_lsb = 0;		/* N/A */
12820 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
12821 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
12822 	scmd->satacmd_features_reg = SATA_SMART_READ_DATA;
12823 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12824 	scmd->satacmd_cmd_reg = SATAC_SMART;
12825 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12826 	    sdinfo->satadrv_addr.cport)));
12827 
12828 	/* Send pkt to SATA HBA driver */
12829 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12830 	    SATA_TRAN_ACCEPTED ||
12831 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12832 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12833 		    sdinfo->satadrv_addr.cport)));
12834 		/*
12835 		 * Whoops, no SMART DATA available
12836 		 */
12837 		rval = -1;
12838 		goto fail;
12839 	} else {
12840 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12841 		    sdinfo->satadrv_addr.cport)));
12842 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
12843 		    DDI_DMA_SYNC_FORKERNEL);
12844 		ASSERT(rval == DDI_SUCCESS);
12845 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)smart_data,
12846 		    sizeof (struct smart_data));
12847 	}
12848 
12849 fail:
12850 	/* Free allocated resources */
12851 	sata_free_local_buffer(spx);
12852 	sata_pkt_free(spx);
12853 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12854 
12855 	return (rval);
12856 }
12857 
12858 /*
12859  * Used by LOG SENSE page 0x10
12860  *
12861  * return 0 for success, -1 otherwise
12862  *
12863  */
12864 static int
12865 sata_ext_smart_selftest_read_log(
12866 	sata_hba_inst_t *sata_hba_inst,
12867 	sata_drive_info_t *sdinfo,
12868 	struct smart_ext_selftest_log *ext_selftest_log,
12869 	uint16_t block_num)
12870 {
12871 	sata_pkt_txlate_t *spx;
12872 	sata_pkt_t *spkt;
12873 	sata_cmd_t *scmd;
12874 	int rval;
12875 
12876 #if ! defined(lint)
12877 	ASSERT(sizeof (struct smart_ext_selftest_log) == 512);
12878 #endif
12879 
12880 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12881 	spx->txlt_sata_hba_inst = sata_hba_inst;
12882 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12883 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12884 	if (spkt == NULL) {
12885 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12886 		return (-1);
12887 	}
12888 	/* address is needed now */
12889 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12890 
12891 
12892 	/* Fill sata_pkt */
12893 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12894 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
12895 	/* Synchronous mode, no callback */
12896 	spkt->satapkt_comp = NULL;
12897 	/* Timeout 30s */
12898 	spkt->satapkt_time = sata_default_pkt_time;
12899 
12900 	scmd = &spkt->satapkt_cmd;
12901 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
12902 
12903 	/*
12904 	 * Allocate buffer for SMART extended self-test log
12905 	 */
12906 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
12907 	    sizeof (struct smart_ext_selftest_log));
12908 	if (scmd->satacmd_bp == NULL) {
12909 		sata_pkt_free(spx);
12910 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12911 		SATA_LOG_D((sata_hba_inst, CE_WARN,
12912 		    "sata_ext_smart_selftest_log: "
12913 		    "cannot allocate buffer"));
12914 		return (-1);
12915 	}
12916 
12917 	/* Build READ LOG EXT w/ extended self-test log cmd in the sata_pkt */
12918 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
12919 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of selftest log */
12920 	scmd->satacmd_sec_count_msb = 0;	/* One sector of selftest log */
12921 	scmd->satacmd_lba_low_lsb = EXT_SMART_SELFTEST_LOG_PAGE;
12922 	scmd->satacmd_lba_low_msb = 0;
12923 	scmd->satacmd_lba_mid_lsb = block_num & 0xff;
12924 	scmd->satacmd_lba_mid_msb = block_num >> 8;
12925 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
12926 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
12927 
12928 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
12929 	    sdinfo->satadrv_addr.cport)));
12930 
12931 	/* Send pkt to SATA HBA driver */
12932 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
12933 	    SATA_TRAN_ACCEPTED ||
12934 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
12935 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12936 		    sdinfo->satadrv_addr.cport)));
12937 
12938 		/*
12939 		 * Whoops, no SMART selftest log info available
12940 		 */
12941 		rval = -1;
12942 		goto fail;
12943 	} else {
12944 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
12945 		    sdinfo->satadrv_addr.cport)));
12946 
12947 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
12948 		    DDI_DMA_SYNC_FORKERNEL);
12949 		ASSERT(rval == DDI_SUCCESS);
12950 		bcopy(scmd->satacmd_bp->b_un.b_addr,
12951 		    (uint8_t *)ext_selftest_log,
12952 		    sizeof (struct smart_ext_selftest_log));
12953 		rval = 0;
12954 	}
12955 
12956 fail:
12957 	/* Free allocated resources */
12958 	sata_free_local_buffer(spx);
12959 	sata_pkt_free(spx);
12960 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
12961 
12962 	return (rval);
12963 }
12964 
12965 /*
12966  * Returns 0 for success, -1 otherwise
12967  *
12968  * SMART self-test log data is returned in buffer pointed to by selftest_log
12969  */
12970 static int
12971 sata_smart_selftest_log(
12972 	sata_hba_inst_t *sata_hba_inst,
12973 	sata_drive_info_t *sdinfo,
12974 	struct smart_selftest_log *selftest_log)
12975 {
12976 	sata_pkt_t *spkt;
12977 	sata_cmd_t *scmd;
12978 	sata_pkt_txlate_t *spx;
12979 	int rval;
12980 
12981 #if ! defined(lint)
12982 	ASSERT(sizeof (struct smart_selftest_log) == 512);
12983 #endif
12984 
12985 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
12986 	spx->txlt_sata_hba_inst = sata_hba_inst;
12987 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
12988 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
12989 	if (spkt == NULL) {
12990 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
12991 		return (-1);
12992 	}
12993 	/* address is needed now */
12994 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12995 
12996 
12997 	/* Fill sata_pkt */
12998 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
12999 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13000 	/* Synchronous mode, no callback */
13001 	spkt->satapkt_comp = NULL;
13002 	/* Timeout 30s */
13003 	spkt->satapkt_time = sata_default_pkt_time;
13004 
13005 	scmd = &spkt->satapkt_cmd;
13006 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13007 
13008 	/*
13009 	 * Allocate buffer for SMART SELFTEST LOG
13010 	 */
13011 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13012 	    sizeof (struct smart_selftest_log));
13013 	if (scmd->satacmd_bp == NULL) {
13014 		sata_pkt_free(spx);
13015 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13016 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13017 		    "sata_smart_selftest_log: "
13018 		    "cannot allocate buffer"));
13019 		return (-1);
13020 	}
13021 
13022 	/* Build SMART_READ_LOG cmd in the sata_pkt */
13023 	scmd->satacmd_addr_type = 0;		/* N/A */
13024 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of SMART log */
13025 	scmd->satacmd_lba_low_lsb = SMART_SELFTEST_LOG_PAGE;
13026 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13027 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13028 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
13029 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13030 	scmd->satacmd_cmd_reg = SATAC_SMART;
13031 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13032 	    sdinfo->satadrv_addr.cport)));
13033 
13034 	/* Send pkt to SATA HBA driver */
13035 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13036 	    SATA_TRAN_ACCEPTED ||
13037 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13038 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13039 		    sdinfo->satadrv_addr.cport)));
13040 		/*
13041 		 * Whoops, no SMART DATA available
13042 		 */
13043 		rval = -1;
13044 		goto fail;
13045 	} else {
13046 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13047 		    sdinfo->satadrv_addr.cport)));
13048 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13049 		    DDI_DMA_SYNC_FORKERNEL);
13050 		ASSERT(rval == DDI_SUCCESS);
13051 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)selftest_log,
13052 		    sizeof (struct smart_selftest_log));
13053 		rval = 0;
13054 	}
13055 
13056 fail:
13057 	/* Free allocated resources */
13058 	sata_free_local_buffer(spx);
13059 	sata_pkt_free(spx);
13060 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13061 
13062 	return (rval);
13063 }
13064 
13065 
13066 /*
13067  * Returns 0 for success, -1 otherwise
13068  *
13069  * SMART READ LOG data is returned in buffer pointed to by smart_log
13070  */
13071 static int
13072 sata_smart_read_log(
13073 	sata_hba_inst_t *sata_hba_inst,
13074 	sata_drive_info_t *sdinfo,
13075 	uint8_t *smart_log,		/* where the data should be returned */
13076 	uint8_t which_log,		/* which log should be returned */
13077 	uint8_t log_size)		/* # of 512 bytes in log */
13078 {
13079 	sata_pkt_t *spkt;
13080 	sata_cmd_t *scmd;
13081 	sata_pkt_txlate_t *spx;
13082 	int rval;
13083 
13084 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13085 	spx->txlt_sata_hba_inst = sata_hba_inst;
13086 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13087 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13088 	if (spkt == NULL) {
13089 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13090 		return (-1);
13091 	}
13092 	/* address is needed now */
13093 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13094 
13095 
13096 	/* Fill sata_pkt */
13097 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13098 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13099 	/* Synchronous mode, no callback */
13100 	spkt->satapkt_comp = NULL;
13101 	/* Timeout 30s */
13102 	spkt->satapkt_time = sata_default_pkt_time;
13103 
13104 	scmd = &spkt->satapkt_cmd;
13105 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13106 
13107 	/*
13108 	 * Allocate buffer for SMART READ LOG
13109 	 */
13110 	scmd->satacmd_bp = sata_alloc_local_buffer(spx, log_size * 512);
13111 	if (scmd->satacmd_bp == NULL) {
13112 		sata_pkt_free(spx);
13113 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13114 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13115 		    "sata_smart_read_log: " "cannot allocate buffer"));
13116 		return (-1);
13117 	}
13118 
13119 	/* Build SMART_READ_LOG cmd in the sata_pkt */
13120 	scmd->satacmd_addr_type = 0;		/* N/A */
13121 	scmd->satacmd_sec_count_lsb = log_size;	/* what the caller asked for */
13122 	scmd->satacmd_lba_low_lsb = which_log;	/* which log page */
13123 	scmd->satacmd_lba_mid_lsb = SMART_MAGIC_VAL_1;
13124 	scmd->satacmd_lba_high_lsb = SMART_MAGIC_VAL_2;
13125 	scmd->satacmd_features_reg = SATA_SMART_READ_LOG;
13126 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13127 	scmd->satacmd_cmd_reg = SATAC_SMART;
13128 
13129 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13130 	    sdinfo->satadrv_addr.cport)));
13131 
13132 	/* Send pkt to SATA HBA driver */
13133 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13134 	    SATA_TRAN_ACCEPTED ||
13135 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13136 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13137 		    sdinfo->satadrv_addr.cport)));
13138 
13139 		/*
13140 		 * Whoops, no SMART DATA available
13141 		 */
13142 		rval = -1;
13143 		goto fail;
13144 	} else {
13145 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13146 		    sdinfo->satadrv_addr.cport)));
13147 
13148 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13149 		    DDI_DMA_SYNC_FORKERNEL);
13150 		ASSERT(rval == DDI_SUCCESS);
13151 		bcopy(scmd->satacmd_bp->b_un.b_addr, smart_log, log_size * 512);
13152 		rval = 0;
13153 	}
13154 
13155 fail:
13156 	/* Free allocated resources */
13157 	sata_free_local_buffer(spx);
13158 	sata_pkt_free(spx);
13159 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13160 
13161 	return (rval);
13162 }
13163 
13164 /*
13165  * Used by LOG SENSE page 0x10
13166  *
13167  * return 0 for success, -1 otherwise
13168  *
13169  */
13170 static int
13171 sata_read_log_ext_directory(
13172 	sata_hba_inst_t *sata_hba_inst,
13173 	sata_drive_info_t *sdinfo,
13174 	struct read_log_ext_directory *logdir)
13175 {
13176 	sata_pkt_txlate_t *spx;
13177 	sata_pkt_t *spkt;
13178 	sata_cmd_t *scmd;
13179 	int rval;
13180 
13181 #if ! defined(lint)
13182 	ASSERT(sizeof (struct read_log_ext_directory) == 512);
13183 #endif
13184 
13185 	spx = kmem_zalloc(sizeof (sata_pkt_txlate_t), KM_SLEEP);
13186 	spx->txlt_sata_hba_inst = sata_hba_inst;
13187 	spx->txlt_scsi_pkt = NULL;		/* No scsi pkt involved */
13188 	spkt = sata_pkt_alloc(spx, SLEEP_FUNC);
13189 	if (spkt == NULL) {
13190 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13191 		return (-1);
13192 	}
13193 
13194 	/* Fill sata_pkt */
13195 	spkt->satapkt_device.satadev_addr = sdinfo->satadrv_addr;
13196 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13197 	/* Synchronous mode, no callback */
13198 	spkt->satapkt_comp = NULL;
13199 	/* Timeout 30s */
13200 	spkt->satapkt_time = sata_default_pkt_time;
13201 
13202 	scmd = &spkt->satapkt_cmd;
13203 	scmd->satacmd_flags.sata_data_direction = SATA_DIR_READ;
13204 
13205 	/*
13206 	 * Allocate buffer for SMART READ LOG EXTENDED command
13207 	 */
13208 	scmd->satacmd_bp = sata_alloc_local_buffer(spx,
13209 	    sizeof (struct read_log_ext_directory));
13210 	if (scmd->satacmd_bp == NULL) {
13211 		sata_pkt_free(spx);
13212 		kmem_free(spx, sizeof (sata_pkt_txlate_t));
13213 		SATA_LOG_D((sata_hba_inst, CE_WARN,
13214 		    "sata_read_log_ext_directory: "
13215 		    "cannot allocate buffer"));
13216 		return (-1);
13217 	}
13218 
13219 	/* Build READ LOG EXT w/ log directory cmd in the  sata_pkt */
13220 	scmd->satacmd_addr_type = ATA_ADDR_LBA48;
13221 	scmd->satacmd_sec_count_lsb = 1;	/* One sector of directory */
13222 	scmd->satacmd_sec_count_msb = 0;	/* One sector of directory */
13223 	scmd->satacmd_lba_low_lsb = READ_LOG_EXT_LOG_DIRECTORY;
13224 	scmd->satacmd_lba_low_msb = 0;
13225 	scmd->satacmd_lba_mid_lsb = 0;
13226 	scmd->satacmd_lba_mid_msb = 0;
13227 	scmd->satacmd_device_reg = 0;		/* Always device 0 */
13228 	scmd->satacmd_cmd_reg = SATAC_READ_LOG_EXT;
13229 
13230 	mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst,
13231 	    sdinfo->satadrv_addr.cport)));
13232 
13233 	/* Send pkt to SATA HBA driver */
13234 	if ((*SATA_START_FUNC(sata_hba_inst))(SATA_DIP(sata_hba_inst), spkt) !=
13235 	    SATA_TRAN_ACCEPTED ||
13236 	    spkt->satapkt_reason != SATA_PKT_COMPLETED) {
13237 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13238 		    sdinfo->satadrv_addr.cport)));
13239 		/*
13240 		 * Whoops, no SMART selftest log info available
13241 		 */
13242 		rval = -1;
13243 		goto fail;
13244 	} else {
13245 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst,
13246 		    sdinfo->satadrv_addr.cport)));
13247 		rval = ddi_dma_sync(spx->txlt_buf_dma_handle, 0, 0,
13248 		    DDI_DMA_SYNC_FORKERNEL);
13249 		ASSERT(rval == DDI_SUCCESS);
13250 		bcopy(scmd->satacmd_bp->b_un.b_addr, (uint8_t *)logdir,
13251 		    sizeof (struct read_log_ext_directory));
13252 		rval = 0;
13253 	}
13254 
13255 fail:
13256 	/* Free allocated resources */
13257 	sata_free_local_buffer(spx);
13258 	sata_pkt_free(spx);
13259 	kmem_free(spx, sizeof (sata_pkt_txlate_t));
13260 
13261 	return (rval);
13262 }
13263 
13264 /*
13265  * Set up error retrieval sata command for NCQ command error data
13266  * recovery.
13267  *
13268  * Returns SATA_SUCCESS when data buffer is allocated and packet set-up,
13269  * returns SATA_FAILURE otherwise.
13270  */
13271 static int
13272 sata_ncq_err_ret_cmd_setup(sata_pkt_txlate_t *spx, sata_drive_info_t *sdinfo)
13273 {
13274 #ifndef __lock_lint
13275 	_NOTE(ARGUNUSED(sdinfo))
13276 #endif
13277 
13278 	sata_pkt_t *spkt = spx->txlt_sata_pkt;
13279 	sata_cmd_t *scmd;
13280 	struct buf *bp;
13281 
13282 	/* Operation modes are up to the caller */
13283 	spkt->satapkt_op_mode = SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS;
13284 
13285 	/* Synchronous mode, no callback - may be changed by the caller */
13286 	spkt->satapkt_comp = NULL;
13287 	spkt->satapkt_time = sata_default_pkt_time;
13288 
13289 	scmd = &spkt->satapkt_cmd;
13290 	bcopy(&sata_rle_cmd, scmd, sizeof (sata_cmd_t));
13291 	scmd->satacmd_flags.sata_ignore_dev_reset = B_TRUE;
13292 
13293 	/*
13294 	 * Allocate dma_able buffer error data.
13295 	 * Buffer allocation will take care of buffer alignment and other DMA
13296 	 * attributes.
13297 	 */
13298 	bp = sata_alloc_local_buffer(spx,
13299 	    sizeof (struct sata_ncq_error_recovery_page));
13300 	if (bp == NULL)
13301 		return (SATA_FAILURE);
13302 
13303 	bp_mapin(bp); /* make data buffer accessible */
13304 	scmd->satacmd_bp = bp;
13305 
13306 	/*
13307 	 * Set-up pointer to the buffer handle, so HBA can sync buffer
13308 	 * before accessing it. Handle is in usual place in translate struct.
13309 	 */
13310 	scmd->satacmd_err_ret_buf_handle = &spx->txlt_buf_dma_handle;
13311 
13312 	ASSERT(scmd->satacmd_num_dma_cookies != 0);
13313 	ASSERT(scmd->satacmd_dma_cookie_list != NULL);
13314 
13315 	return (SATA_SUCCESS);
13316 }
13317 
13318 /*
13319  * sata_xlate_errors() is used to translate (S)ATA error
13320  * information to SCSI information returned in the SCSI
13321  * packet.
13322  */
13323 static void
13324 sata_xlate_errors(sata_pkt_txlate_t *spx)
13325 {
13326 	struct scsi_pkt *scsipkt = spx->txlt_scsi_pkt;
13327 	struct scsi_extended_sense *sense;
13328 
13329 	scsipkt->pkt_reason = CMD_INCOMPLETE;
13330 	*scsipkt->pkt_scbp = STATUS_CHECK;
13331 	sense = sata_arq_sense(spx);
13332 
13333 	switch (spx->txlt_sata_pkt->satapkt_reason) {
13334 	case SATA_PKT_PORT_ERROR:
13335 		/*
13336 		 * We have no device data. Assume no data transfered.
13337 		 */
13338 		sense->es_key = KEY_HARDWARE_ERROR;
13339 		break;
13340 
13341 	case SATA_PKT_DEV_ERROR:
13342 		if (spx->txlt_sata_pkt->satapkt_cmd.satacmd_status_reg &
13343 		    SATA_STATUS_ERR) {
13344 			/*
13345 			 * determine dev error reason from error
13346 			 * reg content
13347 			 */
13348 			sata_decode_device_error(spx, sense);
13349 			break;
13350 		}
13351 		/* No extended sense key - no info available */
13352 		break;
13353 
13354 	case SATA_PKT_TIMEOUT:
13355 		/*
13356 		 * scsipkt->pkt_reason = CMD_TIMEOUT; This causes problems.
13357 		 */
13358 		scsipkt->pkt_reason = CMD_INCOMPLETE;
13359 		/* No extended sense key */
13360 		break;
13361 
13362 	case SATA_PKT_ABORTED:
13363 		scsipkt->pkt_reason = CMD_ABORTED;
13364 		/* No extended sense key */
13365 		break;
13366 
13367 	case SATA_PKT_RESET:
13368 		/*
13369 		 * pkt aborted either by an explicit reset request from
13370 		 * a host, or due to error recovery
13371 		 */
13372 		scsipkt->pkt_reason = CMD_RESET;
13373 		break;
13374 
13375 	default:
13376 		scsipkt->pkt_reason = CMD_TRAN_ERR;
13377 		break;
13378 	}
13379 }
13380 
13381 
13382 
13383 
13384 /*
13385  * Log sata message
13386  * dev pathname msg line preceeds the logged message.
13387  */
13388 
13389 static	void
13390 sata_log(sata_hba_inst_t *sata_hba_inst, uint_t level, char *fmt, ...)
13391 {
13392 	char pathname[128];
13393 	dev_info_t *dip;
13394 	va_list ap;
13395 
13396 	mutex_enter(&sata_log_mutex);
13397 
13398 	va_start(ap, fmt);
13399 	(void) vsprintf(sata_log_buf, fmt, ap);
13400 	va_end(ap);
13401 
13402 	if (sata_hba_inst != NULL) {
13403 		dip = SATA_DIP(sata_hba_inst);
13404 		(void) ddi_pathname(dip, pathname);
13405 	} else {
13406 		pathname[0] = 0;
13407 	}
13408 	if (level == CE_CONT) {
13409 		if (sata_debug_flags == 0)
13410 			cmn_err(level, "?%s:\n %s\n", pathname, sata_log_buf);
13411 		else
13412 			cmn_err(level, "%s:\n %s\n", pathname, sata_log_buf);
13413 	} else
13414 		cmn_err(level, "%s:\n %s", pathname, sata_log_buf);
13415 
13416 	mutex_exit(&sata_log_mutex);
13417 }
13418 
13419 
13420 /* ******** Asynchronous HBA events handling & hotplugging support ******** */
13421 
13422 /*
13423  * Start or terminate the thread, depending on flag arg and current state
13424  */
13425 static void
13426 sata_event_thread_control(int startstop)
13427 {
13428 	static 	int sata_event_thread_terminating = 0;
13429 	static 	int sata_event_thread_starting = 0;
13430 	int i;
13431 
13432 	mutex_enter(&sata_event_mutex);
13433 
13434 	if (startstop == 0 && (sata_event_thread_starting == 1 ||
13435 	    sata_event_thread_terminating == 1)) {
13436 		mutex_exit(&sata_event_mutex);
13437 		return;
13438 	}
13439 	if (startstop == 1 && sata_event_thread_starting == 1) {
13440 		mutex_exit(&sata_event_mutex);
13441 		return;
13442 	}
13443 	if (startstop == 1 && sata_event_thread_terminating == 1) {
13444 		sata_event_thread_starting = 1;
13445 		/* wait til terminate operation completes */
13446 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
13447 		while (sata_event_thread_terminating == 1) {
13448 			if (i-- <= 0) {
13449 				sata_event_thread_starting = 0;
13450 				mutex_exit(&sata_event_mutex);
13451 #ifdef SATA_DEBUG
13452 				cmn_err(CE_WARN, "sata_event_thread_control: "
13453 				    "timeout waiting for thread to terminate");
13454 #endif
13455 				return;
13456 			}
13457 			mutex_exit(&sata_event_mutex);
13458 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
13459 			mutex_enter(&sata_event_mutex);
13460 		}
13461 	}
13462 	if (startstop == 1) {
13463 		if (sata_event_thread == NULL) {
13464 			sata_event_thread = thread_create(NULL, 0,
13465 			    (void (*)())sata_event_daemon,
13466 			    &sata_hba_list, 0, &p0, TS_RUN, minclsyspri);
13467 		}
13468 		sata_event_thread_starting = 0;
13469 		mutex_exit(&sata_event_mutex);
13470 		return;
13471 	}
13472 
13473 	/*
13474 	 * If we got here, thread may need to be terminated
13475 	 */
13476 	if (sata_event_thread != NULL) {
13477 		int i;
13478 		/* Signal event thread to go away */
13479 		sata_event_thread_terminating = 1;
13480 		sata_event_thread_terminate = 1;
13481 		cv_signal(&sata_event_cv);
13482 		/*
13483 		 * Wait til daemon terminates.
13484 		 */
13485 		i = SATA_EVNT_DAEMON_TERM_WAIT/SATA_EVNT_DAEMON_TERM_TIMEOUT;
13486 		while (sata_event_thread_terminate == 1) {
13487 			mutex_exit(&sata_event_mutex);
13488 			if (i-- <= 0) {
13489 				/* Daemon did not go away !!! */
13490 #ifdef SATA_DEBUG
13491 				cmn_err(CE_WARN, "sata_event_thread_control: "
13492 				    "cannot terminate event daemon thread");
13493 #endif
13494 				mutex_enter(&sata_event_mutex);
13495 				break;
13496 			}
13497 			delay(drv_usectohz(SATA_EVNT_DAEMON_TERM_TIMEOUT));
13498 			mutex_enter(&sata_event_mutex);
13499 		}
13500 		sata_event_thread_terminating = 0;
13501 	}
13502 	ASSERT(sata_event_thread_terminating == 0);
13503 	ASSERT(sata_event_thread_starting == 0);
13504 	mutex_exit(&sata_event_mutex);
13505 }
13506 
13507 
13508 /*
13509  * SATA HBA event notification function.
13510  * Events reported by SATA HBA drivers per HBA instance relate to a change in
13511  * a port and/or device state or a controller itself.
13512  * Events for different addresses/addr types cannot be combined.
13513  * A warning message is generated for each event type.
13514  * Events are not processed by this function, so only the
13515  * event flag(s)is set for an affected entity and the event thread is
13516  * waken up. Event daemon thread processes all events.
13517  *
13518  * NOTE: Since more than one event may be reported at the same time, one
13519  * cannot determine a sequence of events when opposite event are reported, eg.
13520  * LINK_LOST and LINK_ESTABLISHED. Actual port status during event processing
13521  * is taking precedence over reported events, i.e. may cause ignoring some
13522  * events.
13523  */
13524 #define	SATA_EVENT_MAX_MSG_LENGTH	79
13525 
13526 void
13527 sata_hba_event_notify(dev_info_t *dip, sata_device_t *sata_device, int event)
13528 {
13529 	sata_hba_inst_t *sata_hba_inst = NULL;
13530 	sata_address_t *saddr;
13531 	sata_drive_info_t *sdinfo;
13532 	sata_port_stats_t *pstats;
13533 	int cport, pmport;
13534 	char buf1[SATA_EVENT_MAX_MSG_LENGTH + 1];
13535 	char buf2[SATA_EVENT_MAX_MSG_LENGTH + 1];
13536 	char *lcp;
13537 	static char *err_msg_evnt_1 =
13538 	    "sata_hba_event_notify: invalid port event 0x%x ";
13539 	static char *err_msg_evnt_2 =
13540 	    "sata_hba_event_notify: invalid device event 0x%x ";
13541 	int linkevent;
13542 
13543 	/*
13544 	 * There is a possibility that an event will be generated on HBA
13545 	 * that has not completed attachment or is detaching.
13546 	 * HBA driver should prevent this, but just in case it does not,
13547 	 * we need to ignore events for such HBA.
13548 	 */
13549 	mutex_enter(&sata_mutex);
13550 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13551 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13552 		if (SATA_DIP(sata_hba_inst) == dip)
13553 			if (sata_hba_inst->satahba_attached == 1)
13554 				break;
13555 	}
13556 	mutex_exit(&sata_mutex);
13557 	if (sata_hba_inst == NULL)
13558 		/* HBA not attached */
13559 		return;
13560 
13561 	ASSERT(sata_device != NULL);
13562 
13563 	/*
13564 	 * Validate address before - do not proceed with invalid address.
13565 	 */
13566 	saddr = &sata_device->satadev_addr;
13567 	if (saddr->cport >= SATA_NUM_CPORTS(sata_hba_inst))
13568 		return;
13569 	if (saddr->qual == SATA_ADDR_PMPORT ||
13570 	    saddr->qual == SATA_ADDR_DPMPORT)
13571 		/* Port Multiplier not supported yet */
13572 		return;
13573 
13574 	cport = saddr->cport;
13575 	pmport = saddr->pmport;
13576 
13577 	buf1[0] = buf2[0] = '\0';
13578 
13579 	/*
13580 	 * Events refer to devices, ports and controllers - each has
13581 	 * unique address. Events for different addresses cannot be combined.
13582 	 */
13583 	if (saddr->qual & (SATA_ADDR_CPORT | SATA_ADDR_PMPORT)) {
13584 
13585 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13586 
13587 		/* qualify this event(s) */
13588 		if ((event & SATA_EVNT_PORT_EVENTS) == 0) {
13589 			/* Invalid event for the device port */
13590 			(void) sprintf(buf2, err_msg_evnt_1,
13591 			    event & SATA_EVNT_PORT_EVENTS);
13592 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13593 			goto event_info;
13594 		}
13595 		if (saddr->qual == SATA_ADDR_CPORT) {
13596 			/* Controller's device port event */
13597 
13598 			(SATA_CPORT_INFO(sata_hba_inst, cport))->
13599 			    cport_event_flags |=
13600 			    event & SATA_EVNT_PORT_EVENTS;
13601 			pstats =
13602 			    &(SATA_CPORT_INFO(sata_hba_inst, cport))->
13603 			    cport_stats;
13604 		} else {
13605 			/* Port multiplier's device port event */
13606 			(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
13607 			    pmport_event_flags |=
13608 			    event & SATA_EVNT_PORT_EVENTS;
13609 			pstats =
13610 			    &(SATA_PMPORT_INFO(sata_hba_inst, cport, pmport))->
13611 			    pmport_stats;
13612 		}
13613 
13614 		/*
13615 		 * Add to statistics and log the message. We have to do it
13616 		 * here rather than in the event daemon, because there may be
13617 		 * multiple events occuring before they are processed.
13618 		 */
13619 		linkevent = event &
13620 		    (SATA_EVNT_LINK_LOST | SATA_EVNT_LINK_ESTABLISHED);
13621 		if (linkevent) {
13622 			if (linkevent == (SATA_EVNT_LINK_LOST |
13623 			    SATA_EVNT_LINK_ESTABLISHED)) {
13624 				/* This is likely event combination */
13625 				(void) strlcat(buf1, "link lost/established, ",
13626 				    SATA_EVENT_MAX_MSG_LENGTH);
13627 
13628 				if (pstats->link_lost < 0xffffffffffffffffULL)
13629 					pstats->link_lost++;
13630 				if (pstats->link_established <
13631 				    0xffffffffffffffffULL)
13632 					pstats->link_established++;
13633 				linkevent = 0;
13634 			} else if (linkevent & SATA_EVNT_LINK_LOST) {
13635 				(void) strlcat(buf1, "link lost, ",
13636 				    SATA_EVENT_MAX_MSG_LENGTH);
13637 
13638 				if (pstats->link_lost < 0xffffffffffffffffULL)
13639 					pstats->link_lost++;
13640 			} else {
13641 				(void) strlcat(buf1, "link established, ",
13642 				    SATA_EVENT_MAX_MSG_LENGTH);
13643 				if (pstats->link_established <
13644 				    0xffffffffffffffffULL)
13645 					pstats->link_established++;
13646 			}
13647 		}
13648 		if (event & SATA_EVNT_DEVICE_ATTACHED) {
13649 			(void) strlcat(buf1, "device attached, ",
13650 			    SATA_EVENT_MAX_MSG_LENGTH);
13651 			if (pstats->device_attached < 0xffffffffffffffffULL)
13652 				pstats->device_attached++;
13653 		}
13654 		if (event & SATA_EVNT_DEVICE_DETACHED) {
13655 			(void) strlcat(buf1, "device detached, ",
13656 			    SATA_EVENT_MAX_MSG_LENGTH);
13657 			if (pstats->device_detached < 0xffffffffffffffffULL)
13658 				pstats->device_detached++;
13659 		}
13660 		if (event & SATA_EVNT_PWR_LEVEL_CHANGED) {
13661 			SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
13662 			    "port %d power level changed", cport);
13663 			if (pstats->port_pwr_changed < 0xffffffffffffffffULL)
13664 				pstats->port_pwr_changed++;
13665 		}
13666 
13667 		if ((event & ~SATA_EVNT_PORT_EVENTS) != 0) {
13668 			/* There should be no other events for this address */
13669 			(void) sprintf(buf2, err_msg_evnt_1,
13670 			    event & ~SATA_EVNT_PORT_EVENTS);
13671 		}
13672 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13673 
13674 	} else if (saddr->qual & (SATA_ADDR_DCPORT | SATA_ADDR_DPMPORT)) {
13675 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13676 
13677 		/* qualify this event */
13678 		if ((event & SATA_EVNT_DEVICE_RESET) == 0) {
13679 			/* Invalid event for a device */
13680 			(void) sprintf(buf2, err_msg_evnt_2,
13681 			    event & SATA_EVNT_DEVICE_RESET);
13682 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13683 			goto event_info;
13684 		}
13685 		/* drive event */
13686 		sdinfo = sata_get_device_info(sata_hba_inst, sata_device);
13687 		if (sdinfo != NULL) {
13688 			if (event & SATA_EVNT_DEVICE_RESET) {
13689 				(void) strlcat(buf1, "device reset, ",
13690 				    SATA_EVENT_MAX_MSG_LENGTH);
13691 				if (sdinfo->satadrv_stats.drive_reset <
13692 				    0xffffffffffffffffULL)
13693 					sdinfo->satadrv_stats.drive_reset++;
13694 				sdinfo->satadrv_event_flags |=
13695 				    SATA_EVNT_DEVICE_RESET;
13696 			}
13697 		}
13698 		if ((event & ~SATA_EVNT_DEVICE_RESET) != 0) {
13699 			/* Invalid event for a device */
13700 			(void) sprintf(buf2, err_msg_evnt_2,
13701 			    event & ~SATA_EVNT_DRIVE_EVENTS);
13702 		}
13703 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, cport)));
13704 	} else {
13705 		if (saddr->qual != SATA_ADDR_NULL) {
13706 			/* Wrong address qualifier */
13707 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13708 			    "sata_hba_event_notify: invalid address 0x%x",
13709 			    *(uint32_t *)saddr));
13710 			return;
13711 		}
13712 		if ((event & SATA_EVNT_CONTROLLER_EVENTS) == 0 ||
13713 		    (event & ~SATA_EVNT_CONTROLLER_EVENTS) != 0) {
13714 			/* Invalid event for the controller */
13715 			SATA_LOG_D((sata_hba_inst, CE_WARN,
13716 			    "sata_hba_event_notify: invalid event 0x%x for "
13717 			    "controller",
13718 			    event & SATA_EVNT_CONTROLLER_EVENTS));
13719 			return;
13720 		}
13721 		buf1[0] = '\0';
13722 		/* This may be a frequent and not interesting event */
13723 		SATADBG1(SATA_DBG_EVENTS, sata_hba_inst,
13724 		    "controller power level changed\n", NULL);
13725 
13726 		mutex_enter(&sata_hba_inst->satahba_mutex);
13727 		if (sata_hba_inst->satahba_stats.ctrl_pwr_change <
13728 		    0xffffffffffffffffULL)
13729 			sata_hba_inst->satahba_stats.ctrl_pwr_change++;
13730 
13731 		sata_hba_inst->satahba_event_flags |=
13732 		    SATA_EVNT_PWR_LEVEL_CHANGED;
13733 		mutex_exit(&sata_hba_inst->satahba_mutex);
13734 	}
13735 	/*
13736 	 * If we got here, there is something to do with this HBA
13737 	 * instance.
13738 	 */
13739 	mutex_enter(&sata_hba_inst->satahba_mutex);
13740 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
13741 	mutex_exit(&sata_hba_inst->satahba_mutex);
13742 	mutex_enter(&sata_mutex);
13743 	sata_event_pending |= SATA_EVNT_MAIN;	/* global event indicator */
13744 	mutex_exit(&sata_mutex);
13745 
13746 	/* Tickle event thread */
13747 	mutex_enter(&sata_event_mutex);
13748 	if (sata_event_thread_active == 0)
13749 		cv_signal(&sata_event_cv);
13750 	mutex_exit(&sata_event_mutex);
13751 
13752 event_info:
13753 	if (buf1[0] != '\0') {
13754 		lcp = strrchr(buf1, ',');
13755 		if (lcp != NULL)
13756 			*lcp = '\0';
13757 	}
13758 	if (saddr->qual == SATA_ADDR_CPORT ||
13759 	    saddr->qual == SATA_ADDR_DCPORT) {
13760 		if (buf1[0] != '\0') {
13761 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
13762 			    cport, buf1);
13763 		}
13764 		if (buf2[0] != '\0') {
13765 			sata_log(sata_hba_inst, CE_NOTE, "port %d: %s\n",
13766 			    cport, buf2);
13767 		}
13768 	} else if (saddr->qual == SATA_ADDR_PMPORT ||
13769 	    saddr->qual == SATA_ADDR_DPMPORT) {
13770 		if (buf1[0] != '\0') {
13771 			sata_log(sata_hba_inst, CE_NOTE,
13772 			    "port %d pmport %d: %s\n", cport, pmport, buf1);
13773 		}
13774 		if (buf2[0] != '\0') {
13775 			sata_log(sata_hba_inst, CE_NOTE,
13776 			    "port %d pmport %d: %s\n", cport, pmport, buf2);
13777 		}
13778 	}
13779 }
13780 
13781 
13782 /*
13783  * Event processing thread.
13784  * Arg is a pointer to the sata_hba_list pointer.
13785  * It is not really needed, because sata_hba_list is global and static
13786  */
13787 static void
13788 sata_event_daemon(void *arg)
13789 {
13790 #ifndef __lock_lint
13791 	_NOTE(ARGUNUSED(arg))
13792 #endif
13793 	sata_hba_inst_t *sata_hba_inst;
13794 	clock_t lbolt;
13795 
13796 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13797 	    "SATA event daemon started\n", NULL);
13798 loop:
13799 	/*
13800 	 * Process events here. Walk through all registered HBAs
13801 	 */
13802 	mutex_enter(&sata_mutex);
13803 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13804 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13805 		ASSERT(sata_hba_inst != NULL);
13806 		mutex_enter(&sata_hba_inst->satahba_mutex);
13807 		if (sata_hba_inst->satahba_attached != 1 ||
13808 		    (sata_hba_inst->satahba_event_flags &
13809 		    SATA_EVNT_SKIP) != 0) {
13810 			mutex_exit(&sata_hba_inst->satahba_mutex);
13811 			continue;
13812 		}
13813 		if (sata_hba_inst->satahba_event_flags & SATA_EVNT_MAIN) {
13814 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_SKIP;
13815 			mutex_exit(&sata_hba_inst->satahba_mutex);
13816 			mutex_exit(&sata_mutex);
13817 			/* Got the controller with pending event */
13818 			sata_process_controller_events(sata_hba_inst);
13819 			/*
13820 			 * Since global mutex was released, there is a
13821 			 * possibility that HBA list has changed, so start
13822 			 * over from the top. Just processed controller
13823 			 * will be passed-over because of the SKIP flag.
13824 			 */
13825 			goto loop;
13826 		}
13827 		mutex_exit(&sata_hba_inst->satahba_mutex);
13828 	}
13829 	/* Clear SKIP flag in all controllers */
13830 	for (sata_hba_inst = sata_hba_list; sata_hba_inst != NULL;
13831 	    sata_hba_inst = sata_hba_inst->satahba_next) {
13832 		mutex_enter(&sata_hba_inst->satahba_mutex);
13833 		sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_SKIP;
13834 		mutex_exit(&sata_hba_inst->satahba_mutex);
13835 	}
13836 	mutex_exit(&sata_mutex);
13837 
13838 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13839 	    "SATA EVENT DAEMON suspending itself", NULL);
13840 
13841 #ifdef SATA_DEBUG
13842 	if ((sata_func_enable & SATA_ENABLE_PROCESS_EVENTS) == 0) {
13843 		sata_log(sata_hba_inst, CE_WARN,
13844 		    "SATA EVENTS PROCESSING DISABLED\n");
13845 		thread_exit(); /* Daemon will not run again */
13846 	}
13847 #endif
13848 	mutex_enter(&sata_event_mutex);
13849 	sata_event_thread_active = 0;
13850 	mutex_exit(&sata_event_mutex);
13851 	/*
13852 	 * Go to sleep/suspend itself and wake up either because new event or
13853 	 * wait timeout. Exit if there is a termination request (driver
13854 	 * unload).
13855 	 */
13856 	do {
13857 		lbolt = ddi_get_lbolt();
13858 		lbolt += drv_usectohz(SATA_EVNT_DAEMON_SLEEP_TIME);
13859 		mutex_enter(&sata_event_mutex);
13860 		(void) cv_timedwait(&sata_event_cv, &sata_event_mutex, lbolt);
13861 
13862 		if (sata_event_thread_active != 0) {
13863 			mutex_exit(&sata_event_mutex);
13864 			continue;
13865 		}
13866 
13867 		/* Check if it is time to go away */
13868 		if (sata_event_thread_terminate == 1) {
13869 			/*
13870 			 * It is up to the thread setting above flag to make
13871 			 * sure that this thread is not killed prematurely.
13872 			 */
13873 			sata_event_thread_terminate = 0;
13874 			sata_event_thread = NULL;
13875 			mutex_exit(&sata_event_mutex);
13876 			SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13877 			    "SATA_EVENT_DAEMON_TERMINATING", NULL);
13878 			thread_exit();  { _NOTE(NOT_REACHED) }
13879 		}
13880 		mutex_exit(&sata_event_mutex);
13881 	} while (!(sata_event_pending & SATA_EVNT_MAIN));
13882 
13883 	mutex_enter(&sata_event_mutex);
13884 	sata_event_thread_active = 1;
13885 	mutex_exit(&sata_event_mutex);
13886 
13887 	mutex_enter(&sata_mutex);
13888 	sata_event_pending &= ~SATA_EVNT_MAIN;
13889 	mutex_exit(&sata_mutex);
13890 
13891 	SATADBG1(SATA_DBG_EVENTS_DAEMON, NULL,
13892 	    "SATA EVENT DAEMON READY TO PROCESS EVENT", NULL);
13893 
13894 	goto loop;
13895 }
13896 
13897 /*
13898  * Specific HBA instance event processing.
13899  *
13900  * NOTE: At the moment, device event processing is limited to hard disks
13901  * only.
13902  * cports only are supported - no pmports.
13903  */
13904 static void
13905 sata_process_controller_events(sata_hba_inst_t *sata_hba_inst)
13906 {
13907 	int ncport;
13908 	uint32_t event_flags;
13909 	sata_address_t *saddr;
13910 
13911 	SATADBG1(SATA_DBG_EVENTS_CNTRL, sata_hba_inst,
13912 	    "Processing controller %d event(s)",
13913 	    ddi_get_instance(SATA_DIP(sata_hba_inst)));
13914 
13915 	mutex_enter(&sata_hba_inst->satahba_mutex);
13916 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_MAIN;
13917 	event_flags = sata_hba_inst->satahba_event_flags;
13918 	mutex_exit(&sata_hba_inst->satahba_mutex);
13919 	/*
13920 	 * Process controller power change first
13921 	 * HERE
13922 	 */
13923 	if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED)
13924 		sata_process_cntrl_pwr_level_change(sata_hba_inst);
13925 
13926 	/*
13927 	 * Search through ports/devices to identify affected port/device.
13928 	 * We may have to process events for more than one port/device.
13929 	 */
13930 	for (ncport = 0; ncport < SATA_NUM_CPORTS(sata_hba_inst); ncport++) {
13931 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
13932 		event_flags = (SATA_CPORT_INFO(sata_hba_inst, ncport))->
13933 		    cport_event_flags;
13934 		/* Check if port was locked by IOCTL processing */
13935 		if (event_flags & SATA_APCTL_LOCK_PORT_BUSY) {
13936 			/*
13937 			 * We ignore port events because port is busy
13938 			 * with AP control processing. Set again
13939 			 * controller and main event flag, so that
13940 			 * events may be processed by the next daemon
13941 			 * run.
13942 			 */
13943 			mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
13944 			mutex_enter(&sata_hba_inst->satahba_mutex);
13945 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
13946 			mutex_exit(&sata_hba_inst->satahba_mutex);
13947 			mutex_enter(&sata_mutex);
13948 			sata_event_pending |= SATA_EVNT_MAIN;
13949 			mutex_exit(&sata_mutex);
13950 			SATADBG1(SATA_DBG_EVENTS_PROCPST, sata_hba_inst,
13951 			    "Event processing postponed until "
13952 			    "AP control processing completes",
13953 			    NULL);
13954 			/* Check other ports */
13955 			continue;
13956 		} else {
13957 			/*
13958 			 * Set BSY flag so that AP control would not
13959 			 * interfere with events processing for
13960 			 * this port.
13961 			 */
13962 			(SATA_CPORT_INFO(sata_hba_inst, ncport))->
13963 			    cport_event_flags |= SATA_EVNT_LOCK_PORT_BUSY;
13964 		}
13965 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
13966 
13967 		saddr = &(SATA_CPORT_INFO(sata_hba_inst, ncport))->cport_addr;
13968 
13969 		if ((event_flags &
13970 		    (SATA_EVNT_PORT_EVENTS | SATA_EVNT_DRIVE_EVENTS)) != 0) {
13971 			/*
13972 			 * Got port event.
13973 			 * We need some hierarchy of event processing as they
13974 			 * are affecting each other:
13975 			 * 1. port failed
13976 			 * 2. device detached/attached
13977 			 * 3. link events - link events may trigger device
13978 			 *    detached or device attached events in some
13979 			 *    circumstances.
13980 			 * 4. port power level changed
13981 			 */
13982 			if (event_flags & SATA_EVNT_PORT_FAILED) {
13983 				sata_process_port_failed_event(sata_hba_inst,
13984 				    saddr);
13985 			}
13986 			if (event_flags & SATA_EVNT_DEVICE_DETACHED) {
13987 				sata_process_device_detached(sata_hba_inst,
13988 				    saddr);
13989 			}
13990 			if (event_flags & SATA_EVNT_DEVICE_ATTACHED) {
13991 				sata_process_device_attached(sata_hba_inst,
13992 				    saddr);
13993 			}
13994 			if (event_flags &
13995 			    (SATA_EVNT_LINK_ESTABLISHED |
13996 			    SATA_EVNT_LINK_LOST)) {
13997 				sata_process_port_link_events(sata_hba_inst,
13998 				    saddr);
13999 			}
14000 			if (event_flags & SATA_EVNT_PWR_LEVEL_CHANGED) {
14001 				sata_process_port_pwr_change(sata_hba_inst,
14002 				    saddr);
14003 			}
14004 			if (event_flags & SATA_EVNT_TARGET_NODE_CLEANUP) {
14005 				sata_process_target_node_cleanup(
14006 				    sata_hba_inst, saddr);
14007 			}
14008 			if (event_flags & SATA_EVNT_AUTOONLINE_DEVICE) {
14009 				sata_process_device_autoonline(
14010 				    sata_hba_inst, saddr);
14011 			}
14012 		}
14013 		if (SATA_CPORT_DEV_TYPE(sata_hba_inst, ncport) !=
14014 		    SATA_DTYPE_NONE) {
14015 			/* May have device event */
14016 			sata_process_device_reset(sata_hba_inst, saddr);
14017 		}
14018 		mutex_enter(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14019 		/* Release PORT_BUSY flag */
14020 		(SATA_CPORT_INFO(sata_hba_inst, ncport))->
14021 		    cport_event_flags &= ~SATA_EVNT_LOCK_PORT_BUSY;
14022 		mutex_exit(&(SATA_CPORT_MUTEX(sata_hba_inst, ncport)));
14023 
14024 	} /* End of loop through the controller SATA ports */
14025 }
14026 
14027 /*
14028  * Process HBA power level change reported by HBA driver.
14029  * Not implemented at this time - event is ignored.
14030  */
14031 static void
14032 sata_process_cntrl_pwr_level_change(sata_hba_inst_t *sata_hba_inst)
14033 {
14034 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14035 	    "Processing controller power level change", NULL);
14036 
14037 	/* Ignoring it for now */
14038 	mutex_enter(&sata_hba_inst->satahba_mutex);
14039 	sata_hba_inst->satahba_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
14040 	mutex_exit(&sata_hba_inst->satahba_mutex);
14041 }
14042 
14043 /*
14044  * Process port power level change reported by HBA driver.
14045  * Not implemented at this time - event is ignored.
14046  */
14047 static void
14048 sata_process_port_pwr_change(sata_hba_inst_t *sata_hba_inst,
14049     sata_address_t *saddr)
14050 {
14051 	sata_cport_info_t *cportinfo;
14052 
14053 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14054 	    "Processing port power level change", NULL);
14055 
14056 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14057 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14058 	/* Reset event flag */
14059 	cportinfo->cport_event_flags &= ~SATA_EVNT_PWR_LEVEL_CHANGED;
14060 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14061 }
14062 
14063 /*
14064  * Process port failure reported by HBA driver.
14065  * cports support only - no pmports.
14066  */
14067 static void
14068 sata_process_port_failed_event(sata_hba_inst_t *sata_hba_inst,
14069     sata_address_t *saddr)
14070 {
14071 	sata_cport_info_t *cportinfo;
14072 
14073 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14074 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14075 	/* Reset event flag first */
14076 	cportinfo->cport_event_flags &= ~SATA_EVNT_PORT_FAILED;
14077 	/* If the port is in SHUTDOWN or FAILED state, ignore this event. */
14078 	if ((cportinfo->cport_state &
14079 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0) {
14080 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14081 		    cport_mutex);
14082 		return;
14083 	}
14084 	/* Fail the port */
14085 	cportinfo->cport_state = SATA_PSTATE_FAILED;
14086 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14087 	sata_log(sata_hba_inst, CE_WARN, "SATA port %d failed", saddr->cport);
14088 }
14089 
14090 /*
14091  * Device Reset Event processing.
14092  * The seqeunce is managed by 3 stage flags:
14093  * - reset event reported,
14094  * - reset event being processed,
14095  * - request to clear device reset state.
14096  */
14097 static void
14098 sata_process_device_reset(sata_hba_inst_t *sata_hba_inst,
14099     sata_address_t *saddr)
14100 {
14101 	sata_drive_info_t old_sdinfo; /* local copy of the drive info */
14102 	sata_drive_info_t *sdinfo;
14103 	sata_cport_info_t *cportinfo;
14104 	sata_device_t sata_device;
14105 	int rval;
14106 
14107 	/* We only care about host sata cport for now */
14108 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14109 
14110 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14111 
14112 	/* If the port is in SHUTDOWN or FAILED state, ignore reset event. */
14113 	if ((cportinfo->cport_state &
14114 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14115 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14116 		    cport_mutex);
14117 		return;
14118 	}
14119 
14120 	if ((SATA_CPORT_DEV_TYPE(sata_hba_inst, saddr->cport) &
14121 	    SATA_VALID_DEV_TYPE) == 0) {
14122 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14123 		    cport_mutex);
14124 		return;
14125 	}
14126 	sdinfo = SATA_CPORT_DRV_INFO(sata_hba_inst, saddr->cport);
14127 	if (sdinfo == NULL) {
14128 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14129 		    cport_mutex);
14130 		return;
14131 	}
14132 
14133 	if ((sdinfo->satadrv_event_flags &
14134 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) == 0) {
14135 		/* Nothing to do */
14136 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14137 		    cport_mutex);
14138 		return;
14139 	}
14140 #ifdef SATA_DEBUG
14141 	if ((sdinfo->satadrv_event_flags &
14142 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) ==
14143 	    (SATA_EVNT_DEVICE_RESET | SATA_EVNT_INPROC_DEVICE_RESET)) {
14144 		/* Something is weird - new device reset event */
14145 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14146 		    "Overlapping device reset events!", NULL);
14147 	}
14148 #endif
14149 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14150 	    "Processing port %d device reset", saddr->cport);
14151 
14152 	/* Clear event flag */
14153 	sdinfo->satadrv_event_flags &= ~SATA_EVNT_DEVICE_RESET;
14154 
14155 	/* It seems that we always need to check the port state first */
14156 	sata_device.satadev_rev = SATA_DEVICE_REV;
14157 	sata_device.satadev_addr = *saddr;
14158 	/*
14159 	 * We have to exit mutex, because the HBA probe port function may
14160 	 * block on its own mutex.
14161 	 */
14162 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14163 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14164 	    (SATA_DIP(sata_hba_inst), &sata_device);
14165 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14166 	sata_update_port_info(sata_hba_inst, &sata_device);
14167 	if (rval != SATA_SUCCESS) {
14168 		/* Something went wrong? Fail the port */
14169 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14170 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14171 		    cport_mutex);
14172 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14173 		    "SATA port %d probing failed",
14174 		    saddr->cport));
14175 		return;
14176 	}
14177 	if ((sata_device.satadev_scr.sstatus  &
14178 	    SATA_PORT_DEVLINK_UP_MASK) !=
14179 	    SATA_PORT_DEVLINK_UP ||
14180 	    sata_device.satadev_type == SATA_DTYPE_NONE) {
14181 		/*
14182 		 * No device to process, anymore. Some other event processing
14183 		 * would or have already performed port info cleanup.
14184 		 * To be safe (HBA may need it), request clearing device
14185 		 * reset condition.
14186 		 */
14187 		sdinfo->satadrv_event_flags = 0;
14188 		sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
14189 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14190 		    cport_mutex);
14191 		return;
14192 	}
14193 
14194 	/* Mark device reset processing as active */
14195 	sdinfo->satadrv_event_flags |= SATA_EVNT_INPROC_DEVICE_RESET;
14196 
14197 	old_sdinfo = *sdinfo;	/* local copy of the drive info */
14198 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14199 
14200 	if (sata_set_drive_features(sata_hba_inst, &old_sdinfo, 1) ==
14201 	    SATA_FAILURE) {
14202 		/*
14203 		 * Restoring drive setting failed.
14204 		 * Probe the port first, to check if the port state has changed
14205 		 */
14206 		sata_device.satadev_rev = SATA_DEVICE_REV;
14207 		sata_device.satadev_addr = *saddr;
14208 		sata_device.satadev_addr.qual = SATA_ADDR_CPORT;
14209 		/* probe port */
14210 		rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14211 		    (SATA_DIP(sata_hba_inst), &sata_device);
14212 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14213 		    cport_mutex);
14214 		if (rval == SATA_SUCCESS &&
14215 		    (sata_device.satadev_state &
14216 		    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) == 0 &&
14217 		    (sata_device.satadev_scr.sstatus  &
14218 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP &&
14219 		    sata_device.satadev_type != SATA_DTYPE_NONE) {
14220 			/*
14221 			 * We may retry this a bit later - in-process reset
14222 			 * condition should be already set.
14223 			 */
14224 			if ((cportinfo->cport_dev_type &
14225 			    SATA_VALID_DEV_TYPE) != 0 &&
14226 			    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14227 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14228 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14229 				    saddr->cport)->cport_mutex);
14230 				mutex_enter(&sata_hba_inst->satahba_mutex);
14231 				sata_hba_inst->satahba_event_flags |=
14232 				    SATA_EVNT_MAIN;
14233 				mutex_exit(&sata_hba_inst->satahba_mutex);
14234 				mutex_enter(&sata_mutex);
14235 				sata_event_pending |= SATA_EVNT_MAIN;
14236 				mutex_exit(&sata_mutex);
14237 				return;
14238 			}
14239 		} else {
14240 			/*
14241 			 * No point of retrying - some other event processing
14242 			 * would or already did port info cleanup.
14243 			 * To be safe (HBA may need it),
14244 			 * request clearing device reset condition.
14245 			 */
14246 			sdinfo->satadrv_event_flags = 0;
14247 			sdinfo->satadrv_event_flags |=
14248 			    SATA_EVNT_CLEAR_DEVICE_RESET;
14249 		}
14250 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14251 		    cport_mutex);
14252 		return;
14253 	}
14254 
14255 	/*
14256 	 * Raise the flag indicating that the next sata command could
14257 	 * be sent with SATA_CLEAR_DEV_RESET_STATE flag, if no new device
14258 	 * reset is reported.
14259 	 */
14260 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14261 	if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) != 0 &&
14262 	    SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14263 		sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14264 		sdinfo->satadrv_event_flags &= ~SATA_EVNT_INPROC_DEVICE_RESET;
14265 		sdinfo->satadrv_event_flags |= SATA_EVNT_CLEAR_DEVICE_RESET;
14266 	}
14267 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14268 }
14269 
14270 
14271 /*
14272  * Port Link Events processing.
14273  * Every link established event may involve device reset (due to
14274  * COMRESET signal, equivalent of the hard reset) so arbitrarily
14275  * set device reset event for an attached device (if any).
14276  * If the port is in SHUTDOWN or FAILED state, ignore link events.
14277  *
14278  * The link established event processing varies, depending on the state
14279  * of the target node, HBA hotplugging capabilities, state of the port.
14280  * If the link is not active, the link established event is ignored.
14281  * If HBA cannot detect device attachment and there is no target node,
14282  * the link established event triggers device attach event processing.
14283  * Else, link established event triggers device reset event processing.
14284  *
14285  * The link lost event processing varies, depending on a HBA hotplugging
14286  * capability and the state of the port (link active or not active).
14287  * If the link is active, the lost link event is ignored.
14288  * If HBA cannot detect device removal, the lost link event triggers
14289  * device detached event processing after link lost timeout.
14290  * Else, the event is ignored.
14291  *
14292  * NOTE: Only cports are processed for now, i.e. no port multiplier ports
14293  */
14294 static void
14295 sata_process_port_link_events(sata_hba_inst_t *sata_hba_inst,
14296     sata_address_t *saddr)
14297 {
14298 	sata_device_t sata_device;
14299 	sata_cport_info_t *cportinfo;
14300 	sata_drive_info_t *sdinfo;
14301 	uint32_t event_flags;
14302 	int rval;
14303 
14304 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14305 	    "Processing port %d link event(s)", saddr->cport);
14306 
14307 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14308 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14309 	event_flags = cportinfo->cport_event_flags;
14310 
14311 	/* Reset event flags first */
14312 	cportinfo->cport_event_flags &=
14313 	    ~(SATA_EVNT_LINK_ESTABLISHED | SATA_EVNT_LINK_LOST);
14314 
14315 	/* If the port is in SHUTDOWN or FAILED state, ignore link events. */
14316 	if ((cportinfo->cport_state &
14317 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14318 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14319 		    cport_mutex);
14320 		return;
14321 	}
14322 
14323 	/*
14324 	 * For the sanity sake get current port state.
14325 	 * Set device address only. Other sata_device fields should be
14326 	 * set by HBA driver.
14327 	 */
14328 	sata_device.satadev_rev = SATA_DEVICE_REV;
14329 	sata_device.satadev_addr = *saddr;
14330 	/*
14331 	 * We have to exit mutex, because the HBA probe port function may
14332 	 * block on its own mutex.
14333 	 */
14334 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14335 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14336 	    (SATA_DIP(sata_hba_inst), &sata_device);
14337 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14338 	sata_update_port_info(sata_hba_inst, &sata_device);
14339 	if (rval != SATA_SUCCESS) {
14340 		/* Something went wrong? Fail the port */
14341 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14342 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14343 		    cport_mutex);
14344 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14345 		    "SATA port %d probing failed",
14346 		    saddr->cport));
14347 		/*
14348 		 * We may want to release device info structure, but
14349 		 * it is not necessary.
14350 		 */
14351 		return;
14352 	} else {
14353 		/* port probed successfully */
14354 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14355 	}
14356 	if (event_flags & SATA_EVNT_LINK_ESTABLISHED) {
14357 
14358 		if ((sata_device.satadev_scr.sstatus &
14359 		    SATA_PORT_DEVLINK_UP_MASK) != SATA_PORT_DEVLINK_UP) {
14360 			/* Ignore event */
14361 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14362 			    "Ignoring port %d link established event - "
14363 			    "link down",
14364 			    saddr->cport);
14365 			goto linklost;
14366 		}
14367 
14368 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14369 		    "Processing port %d link established event",
14370 		    saddr->cport);
14371 
14372 		/*
14373 		 * For the sanity sake check if a device is attached - check
14374 		 * return state of a port probing.
14375 		 */
14376 		if (sata_device.satadev_type != SATA_DTYPE_NONE &&
14377 		    sata_device.satadev_type != SATA_DTYPE_PMULT) {
14378 			/*
14379 			 * HBA port probe indicated that there is a device
14380 			 * attached. Check if the framework had device info
14381 			 * structure attached for this device.
14382 			 */
14383 			if (cportinfo->cport_dev_type != SATA_DTYPE_NONE) {
14384 				ASSERT(SATA_CPORTINFO_DRV_INFO(cportinfo) !=
14385 				    NULL);
14386 
14387 				sdinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14388 				if ((sdinfo->satadrv_type &
14389 				    SATA_VALID_DEV_TYPE) != 0) {
14390 					/*
14391 					 * Dev info structure is present.
14392 					 * If dev_type is set to known type in
14393 					 * the framework's drive info struct
14394 					 * then the device existed before and
14395 					 * the link was probably lost
14396 					 * momentarily - in such case
14397 					 * we may want to check device
14398 					 * identity.
14399 					 * Identity check is not supported now.
14400 					 *
14401 					 * Link established event
14402 					 * triggers device reset event.
14403 					 */
14404 					(SATA_CPORTINFO_DRV_INFO(cportinfo))->
14405 					    satadrv_event_flags |=
14406 					    SATA_EVNT_DEVICE_RESET;
14407 				}
14408 			} else if (cportinfo->cport_dev_type ==
14409 			    SATA_DTYPE_NONE) {
14410 				/*
14411 				 * We got new device attached! If HBA does not
14412 				 * generate device attached events, trigger it
14413 				 * here.
14414 				 */
14415 				if (!(SATA_FEATURES(sata_hba_inst) &
14416 				    SATA_CTLF_HOTPLUG)) {
14417 					cportinfo->cport_event_flags |=
14418 					    SATA_EVNT_DEVICE_ATTACHED;
14419 				}
14420 			}
14421 			/* Reset link lost timeout */
14422 			cportinfo->cport_link_lost_time = 0;
14423 		}
14424 	}
14425 linklost:
14426 	if (event_flags & SATA_EVNT_LINK_LOST) {
14427 		if ((sata_device.satadev_scr.sstatus &
14428 		    SATA_PORT_DEVLINK_UP_MASK) == SATA_PORT_DEVLINK_UP) {
14429 			/* Ignore event */
14430 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14431 			    "Ignoring port %d link lost event - link is up",
14432 			    saddr->cport);
14433 			goto done;
14434 		}
14435 #ifdef SATA_DEBUG
14436 		if (cportinfo->cport_link_lost_time == 0) {
14437 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14438 			    "Processing port %d link lost event",
14439 			    saddr->cport);
14440 		}
14441 #endif
14442 		/*
14443 		 * When HBA cannot generate device attached/detached events,
14444 		 * we need to track link lost time and eventually generate
14445 		 * device detach event.
14446 		 */
14447 		if (!(SATA_FEATURES(sata_hba_inst) & SATA_CTLF_HOTPLUG)) {
14448 			/* We are tracking link lost time */
14449 			if (cportinfo->cport_link_lost_time == 0) {
14450 				/* save current time (lbolt value) */
14451 				cportinfo->cport_link_lost_time =
14452 				    ddi_get_lbolt();
14453 				/* just keep link lost event */
14454 				cportinfo->cport_event_flags |=
14455 				    SATA_EVNT_LINK_LOST;
14456 			} else {
14457 				clock_t cur_time = ddi_get_lbolt();
14458 				if ((cur_time -
14459 				    cportinfo->cport_link_lost_time) >=
14460 				    drv_usectohz(
14461 				    SATA_EVNT_LINK_LOST_TIMEOUT)) {
14462 					/* trigger device detach event */
14463 					cportinfo->cport_event_flags |=
14464 					    SATA_EVNT_DEVICE_DETACHED;
14465 					cportinfo->cport_link_lost_time = 0;
14466 					SATADBG1(SATA_DBG_EVENTS,
14467 					    sata_hba_inst,
14468 					    "Triggering port %d "
14469 					    "device detached event",
14470 					    saddr->cport);
14471 				} else {
14472 					/* keep link lost event */
14473 					cportinfo->cport_event_flags |=
14474 					    SATA_EVNT_LINK_LOST;
14475 				}
14476 			}
14477 		}
14478 		/*
14479 		 * We could change port state to disable/delay access to
14480 		 * the attached device until the link is recovered.
14481 		 */
14482 	}
14483 done:
14484 	event_flags = cportinfo->cport_event_flags;
14485 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14486 	if (event_flags != 0) {
14487 		mutex_enter(&sata_hba_inst->satahba_mutex);
14488 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14489 		mutex_exit(&sata_hba_inst->satahba_mutex);
14490 		mutex_enter(&sata_mutex);
14491 		sata_event_pending |= SATA_EVNT_MAIN;
14492 		mutex_exit(&sata_mutex);
14493 	}
14494 }
14495 
14496 /*
14497  * Device Detached Event processing.
14498  * Port is probed to find if a device is really gone. If so,
14499  * the device info structure is detached from the SATA port info structure
14500  * and released.
14501  * Port status is updated.
14502  *
14503  * NOTE: Process cports event only, no port multiplier ports.
14504  */
14505 static void
14506 sata_process_device_detached(sata_hba_inst_t *sata_hba_inst,
14507     sata_address_t *saddr)
14508 {
14509 	sata_cport_info_t *cportinfo;
14510 	sata_drive_info_t *sdevinfo;
14511 	sata_device_t sata_device;
14512 	dev_info_t *tdip;
14513 	int rval;
14514 
14515 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14516 	    "Processing port %d device detached", saddr->cport);
14517 
14518 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14519 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14520 	/* Clear event flag */
14521 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_DETACHED;
14522 
14523 	/* If the port is in SHUTDOWN or FAILED state, ignore detach event. */
14524 	if ((cportinfo->cport_state &
14525 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14526 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14527 		    cport_mutex);
14528 		return;
14529 	}
14530 	/* For sanity, re-probe the port */
14531 	sata_device.satadev_rev = SATA_DEVICE_REV;
14532 	sata_device.satadev_addr = *saddr;
14533 
14534 	/*
14535 	 * We have to exit mutex, because the HBA probe port function may
14536 	 * block on its own mutex.
14537 	 */
14538 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14539 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14540 	    (SATA_DIP(sata_hba_inst), &sata_device);
14541 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14542 	sata_update_port_info(sata_hba_inst, &sata_device);
14543 	if (rval != SATA_SUCCESS) {
14544 		/* Something went wrong? Fail the port */
14545 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14546 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14547 		    cport_mutex);
14548 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14549 		    "SATA port %d probing failed",
14550 		    saddr->cport));
14551 		/*
14552 		 * We may want to release device info structure, but
14553 		 * it is not necessary.
14554 		 */
14555 		return;
14556 	} else {
14557 		/* port probed successfully */
14558 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14559 	}
14560 	/*
14561 	 * Check if a device is still attached. For sanity, check also
14562 	 * link status - if no link, there is no device.
14563 	 */
14564 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) ==
14565 	    SATA_PORT_DEVLINK_UP && sata_device.satadev_type !=
14566 	    SATA_DTYPE_NONE) {
14567 		/*
14568 		 * Device is still attached - ignore detach event.
14569 		 */
14570 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14571 		    cport_mutex);
14572 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14573 		    "Ignoring detach - device still attached to port %d",
14574 		    sata_device.satadev_addr.cport);
14575 		return;
14576 	}
14577 	/*
14578 	 * We need to detach and release device info structure here
14579 	 */
14580 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14581 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14582 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14583 		(void) kmem_free((void *)sdevinfo,
14584 		    sizeof (sata_drive_info_t));
14585 	}
14586 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14587 	/*
14588 	 * Device cannot be reached anymore, even if the target node may be
14589 	 * still present.
14590 	 */
14591 
14592 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14593 	sata_log(sata_hba_inst, CE_WARN, "SATA device detached at port %d",
14594 	    sata_device.satadev_addr.cport);
14595 
14596 	/*
14597 	 * Try to offline a device and remove target node if it still exists
14598 	 */
14599 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
14600 	if (tdip != NULL) {
14601 		/*
14602 		 * Target node exists.  Unconfigure device then remove
14603 		 * the target node (one ndi operation).
14604 		 */
14605 		if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) != NDI_SUCCESS) {
14606 			/*
14607 			 * PROBLEM - no device, but target node remained
14608 			 * This happens when the file was open or node was
14609 			 * waiting for resources.
14610 			 */
14611 			SATA_LOG_D((sata_hba_inst, CE_WARN,
14612 			    "sata_process_device_detached: "
14613 			    "Failed to remove target node for "
14614 			    "detached SATA device."));
14615 			/*
14616 			 * Set target node state to DEVI_DEVICE_REMOVED.
14617 			 * But re-check first that the node still exists.
14618 			 */
14619 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
14620 			    saddr->cport);
14621 			if (tdip != NULL) {
14622 				sata_set_device_removed(tdip);
14623 				/*
14624 				 * Instruct event daemon to retry the
14625 				 * cleanup later.
14626 				 */
14627 				sata_set_target_node_cleanup(sata_hba_inst,
14628 				    &sata_device.satadev_addr);
14629 			}
14630 		}
14631 	}
14632 	/*
14633 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14634 	 * with the hint: SE_HINT_REMOVE
14635 	 */
14636 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_REMOVE);
14637 }
14638 
14639 
14640 /*
14641  * Device Attached Event processing.
14642  * Port state is checked to verify that a device is really attached. If so,
14643  * the device info structure is created and attached to the SATA port info
14644  * structure.
14645  *
14646  * If attached device cannot be identified or set-up, the retry for the
14647  * attach processing is set-up. Subsequent daemon run would try again to
14648  * identify the device, until the time limit is reached
14649  * (SATA_DEV_IDENTIFY_TIMEOUT).
14650  *
14651  * This function cannot be called in interrupt context (it may sleep).
14652  *
14653  * NOTE: Process cports event only, no port multiplier ports.
14654  */
14655 static void
14656 sata_process_device_attached(sata_hba_inst_t *sata_hba_inst,
14657     sata_address_t *saddr)
14658 {
14659 	sata_cport_info_t *cportinfo;
14660 	sata_drive_info_t *sdevinfo;
14661 	sata_device_t sata_device;
14662 	dev_info_t *tdip;
14663 	uint32_t event_flags;
14664 	int rval;
14665 
14666 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14667 	    "Processing port %d device attached", saddr->cport);
14668 
14669 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14670 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14671 
14672 	/* Clear attach event flag first */
14673 	cportinfo->cport_event_flags &= ~SATA_EVNT_DEVICE_ATTACHED;
14674 
14675 	/* If the port is in SHUTDOWN or FAILED state, ignore event. */
14676 	if ((cportinfo->cport_state &
14677 	    (SATA_PSTATE_SHUTDOWN | SATA_PSTATE_FAILED)) != 0) {
14678 		cportinfo->cport_dev_attach_time = 0;
14679 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14680 		    cport_mutex);
14681 		return;
14682 	}
14683 
14684 	/*
14685 	 * If the sata_drive_info structure is found attached to the port info,
14686 	 * despite the fact the device was removed and now it is re-attached,
14687 	 * the old drive info structure was not removed.
14688 	 * Arbitrarily release device info structure.
14689 	 */
14690 	if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14691 		sdevinfo = SATA_CPORTINFO_DRV_INFO(cportinfo);
14692 		SATA_CPORTINFO_DRV_INFO(cportinfo) = NULL;
14693 		(void) kmem_free((void *)sdevinfo,
14694 		    sizeof (sata_drive_info_t));
14695 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14696 		    "Arbitrarily detaching old device info.", NULL);
14697 	}
14698 	cportinfo->cport_dev_type = SATA_DTYPE_NONE;
14699 
14700 	/* For sanity, re-probe the port */
14701 	sata_device.satadev_rev = SATA_DEVICE_REV;
14702 	sata_device.satadev_addr = *saddr;
14703 
14704 	/*
14705 	 * We have to exit mutex, because the HBA probe port function may
14706 	 * block on its own mutex.
14707 	 */
14708 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14709 	rval = (*SATA_PROBE_PORT_FUNC(sata_hba_inst))
14710 	    (SATA_DIP(sata_hba_inst), &sata_device);
14711 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14712 	sata_update_port_info(sata_hba_inst, &sata_device);
14713 	if (rval != SATA_SUCCESS) {
14714 		/* Something went wrong? Fail the port */
14715 		cportinfo->cport_state = SATA_PSTATE_FAILED;
14716 		cportinfo->cport_dev_attach_time = 0;
14717 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14718 		    cport_mutex);
14719 		SATA_LOG_D((sata_hba_inst, CE_WARN,
14720 		    "SATA port %d probing failed",
14721 		    saddr->cport));
14722 		return;
14723 	} else {
14724 		/* port probed successfully */
14725 		cportinfo->cport_state |= SATA_STATE_PROBED | SATA_STATE_READY;
14726 	}
14727 	/*
14728 	 * Check if a device is still attached. For sanity, check also
14729 	 * link status - if no link, there is no device.
14730 	 */
14731 	if ((sata_device.satadev_scr.sstatus & SATA_PORT_DEVLINK_UP_MASK) !=
14732 	    SATA_PORT_DEVLINK_UP || sata_device.satadev_type ==
14733 	    SATA_DTYPE_NONE) {
14734 		/*
14735 		 * No device - ignore attach event.
14736 		 */
14737 		cportinfo->cport_dev_attach_time = 0;
14738 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14739 		    cport_mutex);
14740 		SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14741 		    "Ignoring attach - no device connected to port %d",
14742 		    sata_device.satadev_addr.cport);
14743 		return;
14744 	}
14745 
14746 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14747 	/*
14748 	 * Generate sysevent - EC_DR / ESC_DR_AP_STATE_CHANGE
14749 	 * with the hint: SE_HINT_INSERT
14750 	 */
14751 	sata_gen_sysevent(sata_hba_inst, saddr, SE_HINT_INSERT);
14752 
14753 	/*
14754 	 * Port reprobing will take care of the creation of the device
14755 	 * info structure and determination of the device type.
14756 	 */
14757 	sata_device.satadev_addr = *saddr;
14758 	(void) sata_reprobe_port(sata_hba_inst, &sata_device,
14759 	    SATA_DEV_IDENTIFY_NORETRY);
14760 
14761 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->
14762 	    cport_mutex);
14763 	if ((cportinfo->cport_state & SATA_STATE_READY) &&
14764 	    (cportinfo->cport_dev_type != SATA_DTYPE_NONE)) {
14765 		/* Some device is attached to the port */
14766 		if (cportinfo->cport_dev_type == SATA_DTYPE_UNKNOWN) {
14767 			/*
14768 			 * A device was not successfully attached.
14769 			 * Track retry time for device identification.
14770 			 */
14771 			if (cportinfo->cport_dev_attach_time != 0) {
14772 				clock_t cur_time = ddi_get_lbolt();
14773 				/*
14774 				 * If the retry time limit was not exceeded,
14775 				 * reinstate attach event.
14776 				 */
14777 				if ((cur_time -
14778 				    cportinfo->cport_dev_attach_time) <
14779 				    drv_usectohz(
14780 				    SATA_DEV_IDENTIFY_TIMEOUT)) {
14781 					/* OK, restore attach event */
14782 					cportinfo->cport_event_flags |=
14783 					    SATA_EVNT_DEVICE_ATTACHED;
14784 				} else {
14785 					/* Timeout - cannot identify device */
14786 					cportinfo->cport_dev_attach_time = 0;
14787 					sata_log(sata_hba_inst,
14788 					    CE_WARN,
14789 					    "Cannot identify SATA device "
14790 					    "at port %d - device will not be "
14791 					    "attached.",
14792 					    saddr->cport);
14793 				}
14794 			} else {
14795 				/*
14796 				 * Start tracking time for device
14797 				 * identification.
14798 				 * Save current time (lbolt value).
14799 				 */
14800 				cportinfo->cport_dev_attach_time =
14801 				    ddi_get_lbolt();
14802 				/* Restore attach event */
14803 				cportinfo->cport_event_flags |=
14804 				    SATA_EVNT_DEVICE_ATTACHED;
14805 			}
14806 		} else {
14807 			/*
14808 			 * If device was successfully attached, the subsequent
14809 			 * action depends on a state of the
14810 			 * sata_auto_online variable. If it is set to zero.
14811 			 * an explicit 'configure' command will be needed to
14812 			 * configure it. If its value is non-zero, we will
14813 			 * attempt to online (configure) the device.
14814 			 * First, log the message indicating that a device
14815 			 * was attached.
14816 			 */
14817 			cportinfo->cport_dev_attach_time = 0;
14818 			sata_log(sata_hba_inst, CE_WARN,
14819 			    "SATA device detected at port %d", saddr->cport);
14820 
14821 			if (SATA_CPORTINFO_DRV_INFO(cportinfo) != NULL) {
14822 				sata_drive_info_t new_sdinfo;
14823 
14824 				/* Log device info data */
14825 				new_sdinfo = *(SATA_CPORTINFO_DRV_INFO(
14826 				    cportinfo));
14827 				sata_show_drive_info(sata_hba_inst,
14828 				    &new_sdinfo);
14829 			}
14830 
14831 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14832 			    saddr->cport)->cport_mutex);
14833 
14834 			/*
14835 			 * Make sure that there is no target node for that
14836 			 * device. If so, release it. It should not happen,
14837 			 * unless we had problem removing the node when
14838 			 * device was detached.
14839 			 */
14840 			tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst),
14841 			    saddr->cport);
14842 			mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14843 			    saddr->cport)->cport_mutex);
14844 			if (tdip != NULL) {
14845 
14846 #ifdef SATA_DEBUG
14847 				if ((cportinfo->cport_event_flags &
14848 				    SATA_EVNT_TARGET_NODE_CLEANUP) == 0)
14849 					sata_log(sata_hba_inst, CE_WARN,
14850 					    "sata_process_device_attached: "
14851 					    "old device target node exists!");
14852 #endif
14853 				/*
14854 				 * target node exists - try to unconfigure
14855 				 * device and remove the node.
14856 				 */
14857 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14858 				    saddr->cport)->cport_mutex);
14859 				rval = ndi_devi_offline(tdip,
14860 				    NDI_DEVI_REMOVE);
14861 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14862 				    saddr->cport)->cport_mutex);
14863 
14864 				if (rval == NDI_SUCCESS) {
14865 					cportinfo->cport_event_flags &=
14866 					    ~SATA_EVNT_TARGET_NODE_CLEANUP;
14867 					cportinfo->cport_tgtnode_clean = B_TRUE;
14868 				} else {
14869 					/*
14870 					 * PROBLEM - the target node remained
14871 					 * and it belongs to a previously
14872 					 * attached device.
14873 					 * This happens when the file was open
14874 					 * or the node was waiting for
14875 					 * resources at the time the
14876 					 * associated device was removed.
14877 					 * Instruct event daemon to retry the
14878 					 * cleanup later.
14879 					 */
14880 					sata_log(sata_hba_inst,
14881 					    CE_WARN,
14882 					    "Application(s) accessing "
14883 					    "previously attached SATA "
14884 					    "device have to release "
14885 					    "it before newly inserted "
14886 					    "device can be made accessible.",
14887 					    saddr->cport);
14888 					cportinfo->cport_event_flags |=
14889 					    SATA_EVNT_TARGET_NODE_CLEANUP;
14890 					cportinfo->cport_tgtnode_clean =
14891 					    B_FALSE;
14892 				}
14893 			}
14894 			if (sata_auto_online != 0) {
14895 				cportinfo->cport_event_flags |=
14896 				    SATA_EVNT_AUTOONLINE_DEVICE;
14897 			}
14898 
14899 		}
14900 	} else {
14901 		cportinfo->cport_dev_attach_time = 0;
14902 	}
14903 
14904 	event_flags = cportinfo->cport_event_flags;
14905 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
14906 	if (event_flags != 0) {
14907 		mutex_enter(&sata_hba_inst->satahba_mutex);
14908 		sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14909 		mutex_exit(&sata_hba_inst->satahba_mutex);
14910 		mutex_enter(&sata_mutex);
14911 		sata_event_pending |= SATA_EVNT_MAIN;
14912 		mutex_exit(&sata_mutex);
14913 	}
14914 }
14915 
14916 
14917 /*
14918  * Device Target Node Cleanup Event processing.
14919  * If the target node associated with a sata port device is in
14920  * DEVI_DEVICE_REMOVED state, an attempt is made to remove it.
14921  * If the target node cannot be removed, the event flag is left intact,
14922  * so that event daemon may re-run this function later.
14923  *
14924  * This function cannot be called in interrupt context (it may sleep).
14925  *
14926  * NOTE: Processes cport events only, not port multiplier ports.
14927  */
14928 static void
14929 sata_process_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
14930     sata_address_t *saddr)
14931 {
14932 	sata_cport_info_t *cportinfo;
14933 	dev_info_t *tdip;
14934 
14935 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14936 	    "Processing port %d device target node cleanup", saddr->cport);
14937 
14938 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
14939 
14940 	/*
14941 	 * Check if there is target node for that device and it is in the
14942 	 * DEVI_DEVICE_REMOVED state. If so, release it.
14943 	 */
14944 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
14945 	if (tdip != NULL) {
14946 		/*
14947 		 * target node exists - check if it is target node of
14948 		 * a removed device.
14949 		 */
14950 		if (sata_check_device_removed(tdip) == B_TRUE) {
14951 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
14952 			    "sata_process_target_node_cleanup: "
14953 			    "old device target node exists!", NULL);
14954 			/*
14955 			 * Unconfigure and remove the target node
14956 			 */
14957 			if (ndi_devi_offline(tdip, NDI_DEVI_REMOVE) ==
14958 			    NDI_SUCCESS) {
14959 				mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14960 				    saddr->cport)->cport_mutex);
14961 				cportinfo->cport_event_flags &=
14962 				    ~SATA_EVNT_TARGET_NODE_CLEANUP;
14963 				mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14964 				    saddr->cport)->cport_mutex);
14965 				return;
14966 			}
14967 			/*
14968 			 * Event daemon will retry the cleanup later.
14969 			 */
14970 			mutex_enter(&sata_hba_inst->satahba_mutex);
14971 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
14972 			mutex_exit(&sata_hba_inst->satahba_mutex);
14973 			mutex_enter(&sata_mutex);
14974 			sata_event_pending |= SATA_EVNT_MAIN;
14975 			mutex_exit(&sata_mutex);
14976 		}
14977 	} else {
14978 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
14979 		    saddr->cport)->cport_mutex);
14980 		cportinfo->cport_event_flags &=
14981 		    ~SATA_EVNT_TARGET_NODE_CLEANUP;
14982 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
14983 		    saddr->cport)->cport_mutex);
14984 	}
14985 }
14986 
14987 /*
14988  * Device AutoOnline Event processing.
14989  * If attached device is to be onlined, an attempt is made to online this
14990  * device, but only if there is no lingering (old) target node present.
14991  * If the device cannot be onlined, the event flag is left intact,
14992  * so that event daemon may re-run this function later.
14993  *
14994  * This function cannot be called in interrupt context (it may sleep).
14995  *
14996  * NOTE: Processes cport events only, not port multiplier ports.
14997  */
14998 static void
14999 sata_process_device_autoonline(sata_hba_inst_t *sata_hba_inst,
15000     sata_address_t *saddr)
15001 {
15002 	sata_cport_info_t *cportinfo;
15003 	sata_drive_info_t *sdinfo;
15004 	sata_device_t sata_device;
15005 	dev_info_t *tdip;
15006 
15007 	SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15008 	    "Processing port %d attached device auto-onlining", saddr->cport);
15009 
15010 	cportinfo = SATA_CPORT_INFO(sata_hba_inst, saddr->cport);
15011 
15012 	/*
15013 	 * Check if device is present and recognized. If not, reset event.
15014 	 */
15015 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15016 	if ((cportinfo->cport_dev_type & SATA_VALID_DEV_TYPE) == 0) {
15017 		/* Nothing to online */
15018 		cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
15019 		mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15020 		    saddr->cport)->cport_mutex);
15021 		return;
15022 	}
15023 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15024 
15025 	/*
15026 	 * Check if there is target node for this device and if it is in the
15027 	 * DEVI_DEVICE_REMOVED state. If so, abort onlining but keep
15028 	 * the event for later processing.
15029 	 */
15030 	tdip = sata_get_target_dip(SATA_DIP(sata_hba_inst), saddr->cport);
15031 	if (tdip != NULL) {
15032 		/*
15033 		 * target node exists - check if it is target node of
15034 		 * a removed device.
15035 		 */
15036 		if (sata_check_device_removed(tdip) == B_TRUE) {
15037 			SATADBG1(SATA_DBG_EVENTS_PROC, sata_hba_inst,
15038 			    "sata_process_device_autoonline: "
15039 			    "old device target node exists!", NULL);
15040 			/*
15041 			 * Event daemon will retry device onlining later.
15042 			 */
15043 			mutex_enter(&sata_hba_inst->satahba_mutex);
15044 			sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15045 			mutex_exit(&sata_hba_inst->satahba_mutex);
15046 			mutex_enter(&sata_mutex);
15047 			sata_event_pending |= SATA_EVNT_MAIN;
15048 			mutex_exit(&sata_mutex);
15049 			return;
15050 		}
15051 		/*
15052 		 * If the target node is not in the 'removed" state, assume
15053 		 * that it belongs to this device. There is nothing more to do,
15054 		 * but reset the event.
15055 		 */
15056 	} else {
15057 
15058 		/*
15059 		 * Try to online the device
15060 		 * If there is any reset-related event, remove it. We are
15061 		 * configuring the device and no state restoring is needed.
15062 		 */
15063 		mutex_enter(&SATA_CPORT_INFO(sata_hba_inst,
15064 		    saddr->cport)->cport_mutex);
15065 		sata_device.satadev_addr = *saddr;
15066 		if (saddr->qual == SATA_ADDR_CPORT)
15067 			sata_device.satadev_addr.qual = SATA_ADDR_DCPORT;
15068 		else
15069 			sata_device.satadev_addr.qual = SATA_ADDR_DPMPORT;
15070 		sdinfo = sata_get_device_info(sata_hba_inst, &sata_device);
15071 		if (sdinfo != NULL) {
15072 			if (sdinfo->satadrv_event_flags &
15073 			    (SATA_EVNT_DEVICE_RESET |
15074 			    SATA_EVNT_INPROC_DEVICE_RESET))
15075 				sdinfo->satadrv_event_flags = 0;
15076 			sdinfo->satadrv_event_flags |=
15077 			    SATA_EVNT_CLEAR_DEVICE_RESET;
15078 
15079 			/* Need to create a new target node. */
15080 			cportinfo->cport_tgtnode_clean = B_TRUE;
15081 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15082 			    saddr->cport)->cport_mutex);
15083 			tdip = sata_create_target_node(SATA_DIP(sata_hba_inst),
15084 			    sata_hba_inst, &sata_device.satadev_addr);
15085 			if (tdip == NULL) {
15086 				/*
15087 				 * Configure (onlining) failed.
15088 				 * We will NOT retry
15089 				 */
15090 				SATA_LOG_D((sata_hba_inst, CE_WARN,
15091 				    "sata_process_device_autoonline: "
15092 				    "configuring SATA device at port %d failed",
15093 				    saddr->cport));
15094 			}
15095 		} else {
15096 			mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15097 			    saddr->cport)->cport_mutex);
15098 		}
15099 
15100 	}
15101 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15102 	cportinfo->cport_event_flags &= ~SATA_EVNT_AUTOONLINE_DEVICE;
15103 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst,
15104 	    saddr->cport)->cport_mutex);
15105 }
15106 
15107 
15108 static void
15109 sata_gen_sysevent(sata_hba_inst_t *sata_hba_inst, sata_address_t *saddr,
15110     int hint)
15111 {
15112 	char ap[MAXPATHLEN];
15113 	nvlist_t *ev_attr_list = NULL;
15114 	int err;
15115 
15116 	/* Allocate and build sysevent attribute list */
15117 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, DDI_NOSLEEP);
15118 	if (err != 0) {
15119 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15120 		    "sata_gen_sysevent: "
15121 		    "cannot allocate memory for sysevent attributes\n"));
15122 		return;
15123 	}
15124 	/* Add hint attribute */
15125 	err = nvlist_add_string(ev_attr_list, DR_HINT, SE_HINT2STR(hint));
15126 	if (err != 0) {
15127 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15128 		    "sata_gen_sysevent: "
15129 		    "failed to add DR_HINT attr for sysevent"));
15130 		nvlist_free(ev_attr_list);
15131 		return;
15132 	}
15133 	/*
15134 	 * Add AP attribute.
15135 	 * Get controller pathname and convert it into AP pathname by adding
15136 	 * a target number.
15137 	 */
15138 	(void) snprintf(ap, MAXPATHLEN, "/devices");
15139 	(void) ddi_pathname(SATA_DIP(sata_hba_inst), ap + strlen(ap));
15140 	(void) snprintf(ap + strlen(ap), MAXPATHLEN - strlen(ap), ":%d",
15141 	    SATA_MAKE_AP_NUMBER(saddr->cport, saddr->pmport, saddr->qual));
15142 
15143 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, ap);
15144 	if (err != 0) {
15145 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15146 		    "sata_gen_sysevent: "
15147 		    "failed to add DR_AP_ID attr for sysevent"));
15148 		nvlist_free(ev_attr_list);
15149 		return;
15150 	}
15151 
15152 	/* Generate/log sysevent */
15153 	err = ddi_log_sysevent(SATA_DIP(sata_hba_inst), DDI_VENDOR_SUNW, EC_DR,
15154 	    ESC_DR_AP_STATE_CHANGE, ev_attr_list, NULL, DDI_NOSLEEP);
15155 	if (err != DDI_SUCCESS) {
15156 		SATA_LOG_D((sata_hba_inst, CE_WARN,
15157 		    "sata_gen_sysevent: "
15158 		    "cannot log sysevent, err code %x\n", err));
15159 	}
15160 
15161 	nvlist_free(ev_attr_list);
15162 }
15163 
15164 
15165 
15166 
15167 /*
15168  * Set DEVI_DEVICE_REMOVED state in the SATA device target node.
15169  */
15170 static void
15171 sata_set_device_removed(dev_info_t *tdip)
15172 {
15173 	int circ;
15174 
15175 	ASSERT(tdip != NULL);
15176 
15177 	ndi_devi_enter(tdip, &circ);
15178 	mutex_enter(&DEVI(tdip)->devi_lock);
15179 	DEVI_SET_DEVICE_REMOVED(tdip);
15180 	mutex_exit(&DEVI(tdip)->devi_lock);
15181 	ndi_devi_exit(tdip, circ);
15182 }
15183 
15184 
15185 /*
15186  * Set internal event instructing event daemon to try
15187  * to perform the target node cleanup.
15188  */
15189 static void
15190 sata_set_target_node_cleanup(sata_hba_inst_t *sata_hba_inst,
15191     sata_address_t *saddr)
15192 {
15193 	mutex_enter(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15194 	SATA_CPORT_EVENT_FLAGS(sata_hba_inst, saddr->cport) |=
15195 	    SATA_EVNT_TARGET_NODE_CLEANUP;
15196 	SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_tgtnode_clean =
15197 	    B_FALSE;
15198 	mutex_exit(&SATA_CPORT_INFO(sata_hba_inst, saddr->cport)->cport_mutex);
15199 	mutex_enter(&sata_hba_inst->satahba_mutex);
15200 	sata_hba_inst->satahba_event_flags |= SATA_EVNT_MAIN;
15201 	mutex_exit(&sata_hba_inst->satahba_mutex);
15202 	mutex_enter(&sata_mutex);
15203 	sata_event_pending |= SATA_EVNT_MAIN;
15204 	mutex_exit(&sata_mutex);
15205 }
15206 
15207 
15208 /*
15209  * Check if the SATA device target node is in DEVI_DEVICE_REMOVED state,
15210  * i.e. check if the target node state indicates that it belongs to a removed
15211  * device.
15212  *
15213  * Returns B_TRUE if the target node is in DEVI_DEVICE_REMOVED state,
15214  * B_FALSE otherwise.
15215  *
15216  * NOTE: No port multiplier support.
15217  */
15218 static boolean_t
15219 sata_check_device_removed(dev_info_t *tdip)
15220 {
15221 	ASSERT(tdip != NULL);
15222 
15223 	if (DEVI_IS_DEVICE_REMOVED(tdip))
15224 		return (B_TRUE);
15225 	else
15226 		return (B_FALSE);
15227 }
15228